config_parser_unittest.cc 179 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924
  1. // Copyright (C) 2012-2016 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this
  5. // file, You can obtain one at http://mozilla.org/MPL/2.0/.
  6. #include <config.h>
  7. #include <cc/command_interpreter.h>
  8. #include <config/module_spec.h>
  9. #include <dhcp/libdhcp++.h>
  10. #include <dhcp/option6_ia.h>
  11. #include <dhcp/iface_mgr.h>
  12. #include <dhcp/option_custom.h>
  13. #include <dhcp/option_int.h>
  14. #include <dhcp/option6_addrlst.h>
  15. #include <dhcp/tests/iface_mgr_test_config.h>
  16. #include <dhcp6/json_config_parser.h>
  17. #include <dhcp6/dhcp6_srv.h>
  18. #include <dhcpsrv/addr_utilities.h>
  19. #include <dhcpsrv/cfgmgr.h>
  20. #include <dhcpsrv/cfg_expiration.h>
  21. #include <dhcpsrv/cfg_hosts.h>
  22. #include <dhcpsrv/subnet.h>
  23. #include <dhcpsrv/subnet_selector.h>
  24. #include <dhcpsrv/testutils/config_result_check.h>
  25. #include <hooks/hooks_manager.h>
  26. #include <defaults.h>
  27. #include "test_data_files_config.h"
  28. #include "test_libraries.h"
  29. #include "marker_file.h"
  30. #include "dhcp6_test_utils.h"
  31. #include <boost/foreach.hpp>
  32. #include <gtest/gtest.h>
  33. #include <fstream>
  34. #include <iostream>
  35. #include <fstream>
  36. #include <sstream>
  37. #include <string>
  38. #include <vector>
  39. #include <arpa/inet.h>
  40. #include <unistd.h>
  41. using namespace isc;
  42. using namespace isc::asiolink;
  43. using namespace isc::config;
  44. using namespace isc::data;
  45. using namespace isc::dhcp;
  46. using namespace isc::dhcp::test;
  47. using namespace isc::hooks;
  48. using namespace std;
  49. namespace {
  50. const char* PARSER_CONFIGS[] = {
  51. // CONFIGURATION 0: one subnet with one pool, no user contexts
  52. "{"
  53. " \"interfaces-config\": {"
  54. " \"interfaces\": [\"*\" ]"
  55. " },"
  56. " \"valid-lifetime\": 4000,"
  57. " \"preferred-lifetime\": 3000,"
  58. " \"rebind-timer\": 2000,"
  59. " \"renew-timer\": 1000,"
  60. " \"subnet6\": [ {"
  61. " \"pools\": [ "
  62. " { \"pool\": \"2001:db8::/64\" }"
  63. " ],"
  64. " \"subnet\": \"2001:db8::/32\""
  65. " } ]"
  66. "}",
  67. // Configuration 1: one pool with empty user context
  68. "{"
  69. " \"interfaces-config\": {"
  70. " \"interfaces\": [\"*\" ]"
  71. " },"
  72. " \"valid-lifetime\": 4000,"
  73. " \"preferred-lifetime\": 3000,"
  74. " \"rebind-timer\": 2000,"
  75. " \"renew-timer\": 1000,"
  76. " \"subnet6\": [ {"
  77. " \"pools\": [ "
  78. " { \"pool\": \"2001:db8::/64\","
  79. " \"user-context\": {"
  80. " }"
  81. " }"
  82. " ],"
  83. " \"subnet\": \"2001:db8::/32\""
  84. " } ]"
  85. "}",
  86. // Configuration 2: one pool with user context containing lw4over6 parameters
  87. "{"
  88. " \"interfaces-config\": {"
  89. " \"interfaces\": [\"*\" ]"
  90. " },"
  91. " \"valid-lifetime\": 4000,"
  92. " \"preferred-lifetime\": 3000,"
  93. " \"rebind-timer\": 2000,"
  94. " \"renew-timer\": 1000,"
  95. " \"subnet6\": [ {"
  96. " \"pools\": [ "
  97. " { \"pool\": \"2001:db8::/64\","
  98. " \"user-context\": {"
  99. " \"lw4over6-sharing-ratio\": 64,"
  100. " \"lw4over6-v4-pool\": \"192.0.2.0/24\","
  101. " \"lw4over6-sysports-exclude\": true,"
  102. " \"lw4over6-bind-prefix-len\": 56"
  103. " }"
  104. " }"
  105. " ],"
  106. " \"subnet\": \"2001:db8::/32\""
  107. " } ]"
  108. "}",
  109. // Configuration 3: pd-pool without any user-context
  110. "{"
  111. " \"interfaces-config\": {"
  112. " \"interfaces\": [\"*\" ]"
  113. " },"
  114. " \"valid-lifetime\": 4000,"
  115. " \"preferred-lifetime\": 3000,"
  116. " \"rebind-timer\": 2000,"
  117. " \"renew-timer\": 1000,"
  118. " \"subnet6\": [ {"
  119. " \"pd-pools\": [ "
  120. " { \"prefix\": \"2001:db8::\","
  121. " \"prefix-len\": 56,"
  122. " \"delegated-len\": 64 }"
  123. " ],"
  124. " \"subnet\": \"2001:db8::/32\""
  125. " } ]"
  126. "}",
  127. // Configuration 4: pd-pool with empty user-context
  128. "{"
  129. " \"interfaces-config\": {"
  130. " \"interfaces\": [\"*\" ]"
  131. " },"
  132. " \"valid-lifetime\": 4000,"
  133. " \"preferred-lifetime\": 3000,"
  134. " \"rebind-timer\": 2000,"
  135. " \"renew-timer\": 1000,"
  136. " \"subnet6\": [ {"
  137. " \"pd-pools\": [ "
  138. " { \"prefix\": \"2001:db8::\","
  139. " \"prefix-len\": 56,"
  140. " \"delegated-len\": 64,"
  141. " \"user-context\": { }"
  142. " }"
  143. " ],"
  144. " \"subnet\": \"2001:db8::/32\""
  145. " } ]"
  146. "}",
  147. // Configuration 5: pd-pool with user-context with lw4over6 parameters
  148. "{"
  149. " \"interfaces-config\": {"
  150. " \"interfaces\": [\"*\" ]"
  151. " },"
  152. " \"valid-lifetime\": 4000,"
  153. " \"preferred-lifetime\": 3000,"
  154. " \"rebind-timer\": 2000,"
  155. " \"renew-timer\": 1000,"
  156. " \"subnet6\": [ {"
  157. " \"pd-pools\": [ "
  158. " { \"prefix\": \"2001:db8::\","
  159. " \"prefix-len\": 56,"
  160. " \"delegated-len\": 64,"
  161. " \"user-context\": {"
  162. " \"lw4over6-sharing-ratio\": 64,"
  163. " \"lw4over6-v4-pool\": \"192.0.2.0/24\","
  164. " \"lw4over6-sysports-exclude\": true,"
  165. " \"lw4over6-bind-prefix-len\": 56"
  166. " }"
  167. " }"
  168. " ],"
  169. " \"subnet\": \"2001:db8::/32\""
  170. " } ]"
  171. "}"
  172. };
  173. std::string specfile(const std::string& name) {
  174. return (std::string(DHCP6_SRC_DIR) + "/" + name);
  175. }
  176. /// @brief Tests that the spec file is valid.
  177. /// Verifies that the DHCP6 configuration specification file is valid.
  178. TEST(Dhcp6SpecTest, basicSpec) {
  179. ASSERT_NO_THROW(isc::config::
  180. moduleSpecFromFile(specfile("dhcp6.spec")));
  181. }
  182. class Dhcp6ParserTest : public ::testing::Test {
  183. protected:
  184. // Check that no hooks libraries are loaded. This is a pre-condition for
  185. // a number of tests, so is checked in one place. As this uses an
  186. // ASSERT call - and it is not clear from the documentation that Gtest
  187. // predicates can be used in a constructor - the check is placed in SetUp.
  188. virtual void SetUp() {
  189. std::vector<std::string> libraries = HooksManager::getLibraryNames();
  190. ASSERT_TRUE(libraries.empty());
  191. }
  192. public:
  193. Dhcp6ParserTest() :rcode_(-1), srv_(0) {
  194. // srv_(0) means to not open any sockets. We don't want to
  195. // deal with sockets here, just check if configuration handling
  196. // is sane.
  197. const IfaceMgr::IfaceCollection& ifaces =
  198. IfaceMgr::instance().getIfaces();
  199. // There must be some interface detected
  200. if (ifaces.empty()) {
  201. // We can't use ASSERT in constructor
  202. ADD_FAILURE() << "No interfaces detected.";
  203. }
  204. valid_iface_ = (*ifaces.begin())->getName();
  205. bogus_iface_ = "nonexisting0";
  206. if (IfaceMgr::instance().getIface(bogus_iface_)) {
  207. ADD_FAILURE() << "The '" << bogus_iface_ << "' exists on this system"
  208. << " while the test assumes that it doesn't, to execute"
  209. << " some negative scenarios. Can't continue this test.";
  210. }
  211. // Reset configuration for each test.
  212. resetConfiguration();
  213. }
  214. ~Dhcp6ParserTest() {
  215. // Reset configuration database after each test.
  216. resetConfiguration();
  217. // ... and delete the hooks library marker files if present
  218. static_cast<void>(remove(LOAD_MARKER_FILE));
  219. static_cast<void>(remove(UNLOAD_MARKER_FILE));
  220. };
  221. // Checks if config_result (result of DHCP server configuration) has
  222. // expected code (0 for success, other for failures).
  223. // Also stores result in rcode_ and comment_.
  224. void checkResult(ConstElementPtr status, int expected_code) {
  225. ASSERT_TRUE(status);
  226. comment_ = parseAnswer(rcode_, status);
  227. EXPECT_EQ(expected_code, rcode_);
  228. }
  229. /// @brief Returns an interface configuration used by the most of the
  230. /// unit tests.
  231. std::string genIfaceConfig() const {
  232. return ("\"interfaces-config\": {"
  233. " \"interfaces\": [ \"*\" ]"
  234. "}");
  235. }
  236. /// @brief Create the simple configuration with single option.
  237. ///
  238. /// This function allows to set one of the parameters that configure
  239. /// option value. These parameters are: "name", "code", "data" and
  240. /// "csv-format".
  241. ///
  242. /// @param param_value string holding option parameter value to be
  243. /// injected into the configuration string.
  244. /// @param parameter name of the parameter to be configured with
  245. /// param value.
  246. std::string createConfigWithOption(const std::string& param_value,
  247. const std::string& parameter) {
  248. std::map<std::string, std::string> params;
  249. if (parameter == "name") {
  250. params["name"] = param_value;
  251. params["space"] = DHCP6_OPTION_SPACE;
  252. params["code"] = "38";
  253. params["data"] = "ABCDEF0105";
  254. params["csv-format"] = "false";
  255. } else if (parameter == "space") {
  256. params["name"] = "subscriber-id";
  257. params["space"] = param_value;
  258. params["code"] = "38";
  259. params["data"] = "ABCDEF0105";
  260. params["csv-format"] = "false";
  261. } else if (parameter == "code") {
  262. params["name"] = "subscriber-id";
  263. params["space"] = DHCP6_OPTION_SPACE;
  264. params["code"] = param_value;
  265. params["data"] = "ABCDEF0105";
  266. params["csv-format"] = "false";
  267. } else if (parameter == "data") {
  268. params["name"] = "subscriber-id";
  269. params["space"] = DHCP6_OPTION_SPACE;
  270. params["code"] = "38";
  271. params["data"] = param_value;
  272. params["csv-format"] = "false";
  273. } else if (parameter == "csv-format") {
  274. params["name"] = "subscriber-id";
  275. params["space"] = DHCP6_OPTION_SPACE;
  276. params["code"] = "38";
  277. params["data"] = "ABCDEF0105";
  278. params["csv-format"] = param_value;
  279. }
  280. return (createConfigWithOption(params));
  281. }
  282. /// @brief Create simple configuration with single option.
  283. ///
  284. /// This function creates a configuration for a single option with
  285. /// custom values for all parameters that describe the option.
  286. ///
  287. /// @params params map holding parameters and their values.
  288. /// @return configuration string containing custom values of parameters
  289. /// describing an option.
  290. std::string createConfigWithOption(const std::map<std::string,
  291. std::string>& params)
  292. {
  293. std::ostringstream stream;
  294. stream << "{ " << genIfaceConfig() << ","
  295. "\"preferred-lifetime\": 3000,"
  296. "\"rebind-timer\": 2000, "
  297. "\"renew-timer\": 1000, "
  298. "\"option-def\": [ {"
  299. " \"name\": \"bool-option\","
  300. " \"code\": 1000,"
  301. " \"type\": \"boolean\","
  302. " \"space\": \"dhcp6\""
  303. "} ],"
  304. "\"subnet6\": [ { "
  305. " \"pools\": [ { \"pool\": \"2001:db8:1::/80\" } ],"
  306. " \"subnet\": \"2001:db8:1::/64\", "
  307. " \"option-data\": [ {";
  308. bool first = true;
  309. typedef std::pair<std::string, std::string> ParamPair;
  310. BOOST_FOREACH(ParamPair param, params) {
  311. if (!first) {
  312. stream << ", ";
  313. } else {
  314. // cppcheck-suppress unreadVariable
  315. first = false;
  316. }
  317. if (param.first == "name") {
  318. stream << "\"name\": \"" << param.second << "\"";
  319. } else if (param.first == "space") {
  320. stream << "\"space\": \"" << param.second << "\"";
  321. } else if (param.first == "code") {
  322. stream << "\"code\": " << param.second;;
  323. } else if (param.first == "data") {
  324. stream << "\"data\": \"" << param.second << "\"";
  325. } else if (param.first == "csv-format") {
  326. stream << "\"csv-format\": " << param.second;
  327. }
  328. }
  329. stream <<
  330. " } ]"
  331. " } ],"
  332. "\"valid-lifetime\": 4000 }";
  333. return (stream.str());
  334. }
  335. /// @brief Returns an option from the subnet.
  336. ///
  337. /// This function returns an option from a subnet to which the
  338. /// specified subnet address belongs. The option is identified
  339. /// by its code.
  340. ///
  341. /// @param subnet_address Address which belongs to the subnet from
  342. /// which the option is to be returned.
  343. /// @param option_code Code of the option to be returned.
  344. /// @param expected_options_count Expected number of options in
  345. /// the particular subnet.
  346. ///
  347. /// @return Descriptor of the option. If the descriptor holds a
  348. /// NULL option pointer, it means that there was no such option
  349. /// in the subnet.
  350. OptionDescriptor
  351. getOptionFromSubnet(const IOAddress& subnet_address,
  352. const uint16_t option_code,
  353. const uint16_t expected_options_count = 1) {
  354. Subnet6Ptr subnet = CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->
  355. selectSubnet(subnet_address, classify_);
  356. if (!subnet) {
  357. /// @todo replace toText() with the use of operator <<.
  358. ADD_FAILURE() << "A subnet for the specified address "
  359. << subnet_address.toText()
  360. << " does not exist in Config Manager";
  361. }
  362. OptionContainerPtr options =
  363. subnet->getCfgOption()->getAll(DHCP6_OPTION_SPACE);
  364. if (expected_options_count != options->size()) {
  365. ADD_FAILURE() << "The number of options in the subnet '"
  366. << subnet_address.toText() << "' is different "
  367. " than expected number of options '"
  368. << expected_options_count << "'";
  369. }
  370. // Get the search index. Index #1 is to search using option code.
  371. const OptionContainerTypeIndex& idx = options->get<1>();
  372. // Get the options for specified index. Expecting one option to be
  373. // returned but in theory we may have multiple options with the same
  374. // code so we get the range.
  375. std::pair<OptionContainerTypeIndex::const_iterator,
  376. OptionContainerTypeIndex::const_iterator> range =
  377. idx.equal_range(option_code);
  378. if (std::distance(range.first, range.second) > 1) {
  379. ADD_FAILURE() << "There is more than one option having the"
  380. " option code '" << option_code << "' in a subnet '"
  381. << subnet_address.toText() << "'. Expected "
  382. " at most one option";
  383. } else if (std::distance(range.first, range.second) == 0) {
  384. return (OptionDescriptor(OptionPtr(), false));
  385. }
  386. return (*range.first);
  387. }
  388. /// @brief Parse and Execute configuration
  389. ///
  390. /// Parses a configuration and executes a configuration of the server.
  391. /// If the operation fails, the current test will register a failure.
  392. ///
  393. /// @param config Configuration to parse
  394. /// @param operation Operation being performed. In the case of an error,
  395. /// the error text will include the string "unable to <operation>.".
  396. ///
  397. /// @return true if the configuration succeeded, false if not. In the
  398. /// latter case, a failure will have been added to the current test.
  399. bool
  400. executeConfiguration(const std::string& config, const char* operation) {
  401. ConstElementPtr json;
  402. ConstElementPtr status;
  403. try {
  404. json = parseJSON(config);
  405. status = configureDhcp6Server(srv_, json);
  406. } catch (const std::exception& ex) {
  407. ADD_FAILURE() << "Unable to " << operation << ". "
  408. << "The following configuration was used: " << std::endl
  409. << config << std::endl
  410. << " and the following error message was returned:"
  411. << ex.what() << std::endl;
  412. return (false);
  413. }
  414. // The status object must not be NULL
  415. if (!status) {
  416. ADD_FAILURE() << "Unable to " << operation << ". "
  417. << "The configuration function returned a null pointer.";
  418. return (false);
  419. }
  420. // Store the answer if we need it.
  421. // Returned value should be 0 (configuration success)
  422. comment_ = parseAnswer(rcode_, status);
  423. if (rcode_ != 0) {
  424. string reason = "";
  425. if (comment_) {
  426. reason = string(" (") + comment_->stringValue() + string(")");
  427. }
  428. ADD_FAILURE() << "Unable to " << operation << ". "
  429. << "The configuration function returned error code "
  430. << rcode_ << reason;
  431. return (false);
  432. }
  433. return (true);
  434. }
  435. /// @brief Reset configuration database.
  436. ///
  437. /// This function resets configuration data base by removing all subnets
  438. /// option-data, and hooks libraries. The reset must be performed after each
  439. /// test to make sure that contents of the database do not affect the
  440. /// results of subsequent tests.
  441. void resetConfiguration() {
  442. string config = "{ \"interfaces-config\": {"
  443. " \"interfaces\": [ ]"
  444. "},"
  445. "\"hooks-libraries\": [ ],"
  446. "\"preferred-lifetime\": 3000,"
  447. "\"rebind-timer\": 2000, "
  448. "\"renew-timer\": 1000, "
  449. "\"valid-lifetime\": 4000, "
  450. "\"subnet6\": [ ], "
  451. "\"dhcp-ddns\": { \"enable-updates\" : false }, "
  452. "\"option-def\": [ ], "
  453. "\"option-data\": [ ] }";
  454. static_cast<void>(executeConfiguration(config,
  455. "reset configuration database"));
  456. // The default setting is to listen on all interfaces. In order to
  457. // properly test interface configuration we disable listening on
  458. // all interfaces before each test and later check that this setting
  459. // has been overridden by the configuration used in the test.
  460. CfgMgr::instance().clear();
  461. // Create fresh context.
  462. globalContext()->copyContext(ParserContext(Option::V6));
  463. }
  464. /// @brief Retrieve an option associated with a host.
  465. ///
  466. /// The option is retrieved from the "dhcp6" option space.
  467. ///
  468. /// @param host Reference to a host for which an option should be retrieved.
  469. /// @param option_code Option code.
  470. /// @tparam ReturnType Type of the pointer object returned.
  471. ///
  472. /// @return Pointer to an option or NULL pointer if not found.
  473. template<typename ReturnType>
  474. ReturnType
  475. retrieveOption(const Host& host, const uint16_t option_code) const {
  476. return (retrieveOption<ReturnType>(host, DHCP6_OPTION_SPACE, option_code));
  477. }
  478. /// @brief Retrieve an option associated with a host.
  479. ///
  480. /// @param host Reference to a host for which an option should be retrieved.
  481. /// @param space Option space from which option should be retrieved.
  482. /// @param option_code Option code.
  483. /// @tparam ReturnType Type of the pointer object returned.
  484. ///
  485. /// @return Pointer to an option or NULL pointer if not found.
  486. template<typename ReturnType>
  487. ReturnType
  488. retrieveOption(const Host& host, const std::string& space,
  489. const uint16_t option_code) const {
  490. ConstCfgOptionPtr cfg_option = host.getCfgOption6();
  491. if (cfg_option) {
  492. OptionDescriptor opt_desc = cfg_option->get(space, option_code);
  493. if (opt_desc.option_) {
  494. return (boost::dynamic_pointer_cast<
  495. typename ReturnType::element_type>(opt_desc.option_));
  496. }
  497. }
  498. return (ReturnType());
  499. }
  500. /// @brief Test invalid option parameter value.
  501. ///
  502. /// This test function constructs the simple configuration
  503. /// string and injects invalid option configuration into it.
  504. /// It expects that parser will fail with provided option code.
  505. ///
  506. /// @param param_value string holding invalid option parameter value
  507. /// to be injected into configuration string.
  508. /// @param parameter name of the parameter to be configured with
  509. /// param_value (can be any of "name", "code", "data")
  510. void testInvalidOptionParam(const std::string& param_value,
  511. const std::string& parameter) {
  512. ConstElementPtr x;
  513. std::string config = createConfigWithOption(param_value, parameter);
  514. ConstElementPtr json = parseDHCP6(config);
  515. EXPECT_NO_THROW(x = configureDhcp6Server(srv_, json));
  516. checkResult(x, 1);
  517. EXPECT_TRUE(errorContainsPosition(x, "<string>"));
  518. CfgMgr::instance().clear();
  519. }
  520. /// @brief Test invalid option parameter value.
  521. ///
  522. /// This test function constructs the simple configuration
  523. /// string and injects invalid option configuration into it.
  524. /// It expects that parser will fail with provided option code.
  525. ///
  526. /// @param params Map of parameters defining an option.
  527. void
  528. testInvalidOptionParam(const std::map<std::string, std::string>& params) {
  529. ConstElementPtr x;
  530. std::string config = createConfigWithOption(params);
  531. ConstElementPtr json = parseDHCP6(config);
  532. EXPECT_NO_THROW(x = configureDhcp6Server(srv_, json));
  533. checkResult(x, 1);
  534. EXPECT_TRUE(errorContainsPosition(x, "<string>"));
  535. CfgMgr::instance().clear();
  536. }
  537. /// @brief Test option against given code and data.
  538. ///
  539. /// @param option_desc option descriptor that carries the option to
  540. /// be tested.
  541. /// @param expected_code expected code of the option.
  542. /// @param expected_data expected data in the option.
  543. /// @param expected_data_len length of the reference data.
  544. /// @param extra_data if true extra data is allowed in an option
  545. /// after tested data.
  546. void testOption(const OptionDescriptor& option_desc,
  547. uint16_t expected_code, const uint8_t* expected_data,
  548. size_t expected_data_len,
  549. bool extra_data = false) {
  550. // Check if option descriptor contains valid option pointer.
  551. ASSERT_TRUE(option_desc.option_);
  552. // Verify option type.
  553. EXPECT_EQ(expected_code, option_desc.option_->getType());
  554. // We may have many different option types being created. Some of them
  555. // have dedicated classes derived from Option class. In such case if
  556. // we want to verify the option contents against expected_data we have
  557. // to prepare raw buffer with the contents of the option. The easiest
  558. // way is to call pack() which will prepare on-wire data.
  559. util::OutputBuffer buf(option_desc.option_->getData().size());
  560. option_desc.option_->pack(buf);
  561. if (extra_data) {
  562. // The length of the buffer must be at least equal to size of the
  563. // reference data but it can sometimes be greater than that. This is
  564. // because some options carry suboptions that increase the overall
  565. // length.
  566. ASSERT_GE(buf.getLength() - option_desc.option_->getHeaderLen(),
  567. expected_data_len);
  568. } else {
  569. ASSERT_EQ(buf.getLength() - option_desc.option_->getHeaderLen(),
  570. expected_data_len);
  571. }
  572. // Verify that the data is correct. Do not verify suboptions and a header.
  573. const uint8_t* data = static_cast<const uint8_t*>(buf.getData());
  574. EXPECT_EQ(0, memcmp(expected_data, data + option_desc.option_->getHeaderLen(),
  575. expected_data_len));
  576. }
  577. /// @brief Test option configuration.
  578. ///
  579. /// This function creates a configuration for a specified option using
  580. /// a map of parameters specified as the argument. The map holds
  581. /// name/value pairs which identifies option's configuration parameters:
  582. /// - name
  583. /// - space
  584. /// - code
  585. /// - data
  586. /// - csv-format.
  587. /// This function applies a new server configuration and checks that the
  588. /// option being configured is inserted into CfgMgr. The raw contents of
  589. /// this option are compared with the binary data specified as expected
  590. /// data passed to this function.
  591. ///
  592. /// @param params Map of parameters defining an option.
  593. /// @param option_code Option code.
  594. /// @param expected_data Array containing binary data expected to be stored
  595. /// in the configured option.
  596. /// @param expected_data_len Length of the array holding reference data.
  597. void testConfiguration(const std::map<std::string, std::string>& params,
  598. const uint16_t option_code,
  599. const uint8_t* expected_data,
  600. const size_t expected_data_len) {
  601. CfgMgr::instance().clear();
  602. std::string config = createConfigWithOption(params);
  603. ASSERT_TRUE(executeConfiguration(config, "parse option configuration"));
  604. // The subnet should now hold one option with the specified code.
  605. OptionDescriptor desc =
  606. getOptionFromSubnet(IOAddress("2001:db8:1::5"), option_code);
  607. ASSERT_TRUE(desc.option_);
  608. testOption(desc, option_code, expected_data, expected_data_len);
  609. CfgMgr::instance().clear();
  610. }
  611. /// @brief Tests the Rapid Commit configuration for a subnet.
  612. ///
  613. /// This test configures the server with a given configuration and
  614. /// verifies if the Rapid Commit has been configured successfully
  615. /// for a subnet.
  616. ///
  617. /// @param config Server configuration, possibly including the
  618. /// 'rapid-commit' parameter.
  619. /// @param exp_rapid_commit Expected value of the Rapid Commit flag
  620. /// within a subnet.
  621. void testRapidCommit(const std::string& config,
  622. const bool exp_rapid_commit) {
  623. // Clear any existing configuration.
  624. CfgMgr::instance().clear();
  625. // Configure the server.
  626. ConstElementPtr json = parseDHCP6(config);
  627. // Make sure that the configuration was successful.
  628. ConstElementPtr status;
  629. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  630. checkResult(status, 0);
  631. // Get the subnet.
  632. Subnet6Ptr subnet = CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->
  633. selectSubnet(IOAddress("2001:db8:1::5"), classify_);
  634. ASSERT_TRUE(subnet);
  635. // Check the Rapid Commit flag for the subnet.
  636. EXPECT_EQ(exp_rapid_commit, subnet->getRapidCommit());
  637. // Clear any existing configuration.
  638. CfgMgr::instance().clear();
  639. }
  640. /// @brief This utility method attempts to configure using specified
  641. /// config and then returns requested pool from requested subnet
  642. ///
  643. /// @param config configuration to be applied
  644. /// @param subnet_index index of the subnet to be returned (0 - the first subnet)
  645. /// @param pool_index index of the pool within a subnet (0 - the first pool)
  646. /// @param type Pool type (TYPE_NA or TYPE_PD)
  647. /// @param pool [out] Pool pointer will be stored here (if found)
  648. void getPool(const std::string& config, size_t subnet_index,
  649. size_t pool_index, Lease::Type type, PoolPtr& pool) {
  650. ConstElementPtr status;
  651. ElementPtr json = Element::fromJSON(config);
  652. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  653. ASSERT_TRUE(status);
  654. checkResult(status, 0);
  655. ConstCfgSubnets6Ptr subnets6 = CfgMgr::instance().getStagingCfg()->getCfgSubnets6();
  656. ASSERT_TRUE(subnets6);
  657. const Subnet6Collection* subnets = subnets6->getAll();
  658. ASSERT_TRUE(subnets);
  659. ASSERT_GE(subnets->size(), subnet_index + 1);
  660. const PoolCollection pools = subnets->at(subnet_index)->getPools(type);
  661. ASSERT_GE(pools.size(), pool_index + 1);
  662. pool = pools.at(pool_index);
  663. EXPECT_TRUE(pool);
  664. }
  665. int rcode_; ///< Return code (see @ref isc::config::parseAnswer)
  666. Dhcpv6Srv srv_; ///< Instance of the Dhcp6Srv used during tests
  667. ConstElementPtr comment_; ///< Comment (see @ref isc::config::parseAnswer)
  668. string valid_iface_; ///< Valid network interface name (present in system)
  669. string bogus_iface_; ///< invalid network interface name (not in system)
  670. isc::dhcp::ClientClasses classify_; ///< used in client classification
  671. };
  672. // Goal of this test is a verification if a very simple config update
  673. // with just a bumped version number. That's the simplest possible
  674. // config update.
  675. TEST_F(Dhcp6ParserTest, version) {
  676. ConstElementPtr json;
  677. ASSERT_NO_THROW(json = parseDHCP6("{\"version\": 0}"));
  678. ConstElementPtr x;
  679. EXPECT_NO_THROW(x = configureDhcp6Server(srv_, json));
  680. // returned value must be 0 (configuration accepted)
  681. checkResult(x, 0);
  682. }
  683. /// The goal of this test is to verify that the code accepts only
  684. /// valid commands and malformed or unsupported parameters are rejected.
  685. TEST_F(Dhcp6ParserTest, bogusCommand) {
  686. ConstElementPtr x;
  687. EXPECT_NO_THROW(x = configureDhcp6Server(srv_,
  688. parseJSON("{\"bogus\": 5}")));
  689. // returned value must be 1 (configuration parse error)
  690. checkResult(x, 1);
  691. // it should be refused by syntax too
  692. EXPECT_THROW(parseDHCP6("{\"bogus\": 5}"), Dhcp6ParseError);
  693. }
  694. /// The goal of this test is to verify if configuration without any
  695. /// subnets defined can be accepted.
  696. TEST_F(Dhcp6ParserTest, emptySubnet) {
  697. ConstElementPtr json;
  698. ASSERT_NO_THROW(json = parseDHCP6("{ " + genIfaceConfig() + ","
  699. "\"preferred-lifetime\": 3000,"
  700. "\"rebind-timer\": 2000, "
  701. "\"renew-timer\": 1000, "
  702. "\"subnet6\": [ ], "
  703. "\"valid-lifetime\": 4000 }"));
  704. ConstElementPtr status;
  705. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  706. // returned value should be 0 (success)
  707. checkResult(status, 0);
  708. }
  709. /// The goal of this test is to verify if defined subnet uses global
  710. /// parameter timer definitions.
  711. TEST_F(Dhcp6ParserTest, subnetGlobalDefaults) {
  712. string config = "{ " + genIfaceConfig() + ","
  713. "\"preferred-lifetime\": 3000,"
  714. "\"rebind-timer\": 2000, "
  715. "\"renew-timer\": 1000, "
  716. "\"subnet6\": [ { "
  717. " \"pools\": [ { \"pool\": \"2001:db8:1::1 - 2001:db8:1::ffff\" } ],"
  718. " \"subnet\": \"2001:db8:1::/64\" } ],"
  719. "\"valid-lifetime\": 4000 }";
  720. ConstElementPtr json;
  721. ASSERT_NO_THROW(json = parseDHCP6(config));
  722. ConstElementPtr status;
  723. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  724. // check if returned status is OK
  725. checkResult(status, 0);
  726. // Now check if the configuration was indeed handled and we have
  727. // expected pool configured.
  728. Subnet6Ptr subnet = CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->
  729. selectSubnet(IOAddress("2001:db8:1::5"), classify_);
  730. ASSERT_TRUE(subnet);
  731. EXPECT_EQ(1000, subnet->getT1());
  732. EXPECT_EQ(2000, subnet->getT2());
  733. EXPECT_EQ(3000, subnet->getPreferred());
  734. EXPECT_EQ(4000, subnet->getValid());
  735. // Check that subnet-id is 1
  736. EXPECT_EQ(1, subnet->getID());
  737. }
  738. // This test checks that multiple subnets can be defined and handled properly.
  739. TEST_F(Dhcp6ParserTest, multipleSubnets) {
  740. ConstElementPtr x;
  741. // Collection of four subnets for which ids should be autogenerated
  742. // - ids are unspecified or set to 0.
  743. string config = "{ " + genIfaceConfig() + ","
  744. "\"preferred-lifetime\": 3000,"
  745. "\"rebind-timer\": 2000, "
  746. "\"renew-timer\": 1000, "
  747. "\"subnet6\": [ { "
  748. " \"pools\": [ { \"pool\": \"2001:db8:1::/80\" } ],"
  749. " \"subnet\": \"2001:db8:1::/64\" "
  750. " },"
  751. " {"
  752. " \"pools\": [ { \"pool\": \"2001:db8:2::/80\" } ],"
  753. " \"subnet\": \"2001:db8:2::/64\", "
  754. " \"id\": 0"
  755. " },"
  756. " {"
  757. " \"pools\": [ { \"pool\": \"2001:db8:3::/80\" } ],"
  758. " \"subnet\": \"2001:db8:3::/64\" "
  759. " },"
  760. " {"
  761. " \"pools\": [ { \"pool\": \"2001:db8:4::/80\" } ],"
  762. " \"subnet\": \"2001:db8:4::/64\" "
  763. " } ],"
  764. "\"valid-lifetime\": 4000 }";
  765. int cnt = 0; // Number of reconfigurations
  766. ConstElementPtr json;
  767. ASSERT_NO_THROW(json = parseDHCP6(config));
  768. do {
  769. EXPECT_NO_THROW(x = configureDhcp6Server(srv_, json));
  770. checkResult(x, 0);
  771. CfgMgr::instance().commit();
  772. const Subnet6Collection* subnets =
  773. CfgMgr::instance().getCurrentCfg()->getCfgSubnets6()->getAll();
  774. ASSERT_TRUE(subnets);
  775. ASSERT_EQ(4, subnets->size()); // We expect 4 subnets
  776. // Check subnet-ids of each subnet (it should be monotonously increasing)
  777. EXPECT_EQ(1, subnets->at(0)->getID());
  778. EXPECT_EQ(2, subnets->at(1)->getID());
  779. EXPECT_EQ(3, subnets->at(2)->getID());
  780. EXPECT_EQ(4, subnets->at(3)->getID());
  781. // Repeat reconfiguration process 10 times and check that the subnet-id
  782. // is set to the same value. Technically, just two iterations would be
  783. // sufficient, but it's nice to have a test that exercises reconfiguration
  784. // a bit.
  785. } while (++cnt < 10);
  786. }
  787. // This checks that it is possible to assign arbitrary ids for subnets.
  788. TEST_F(Dhcp6ParserTest, multipleSubnetsExplicitIDs) {
  789. ConstElementPtr x;
  790. // Four subnets with arbitrary subnet ids.
  791. string config = "{ " + genIfaceConfig() + ","
  792. "\"preferred-lifetime\": 3000,"
  793. "\"rebind-timer\": 2000, "
  794. "\"renew-timer\": 1000, "
  795. "\"subnet6\": [ { "
  796. " \"pools\": [ { \"pool\": \"2001:db8:1::/80\" } ],"
  797. " \"subnet\": \"2001:db8:1::/64\", "
  798. " \"id\": 1024"
  799. " },"
  800. " {"
  801. " \"pools\": [ { \"pool\": \"2001:db8:2::/80\" } ],"
  802. " \"subnet\": \"2001:db8:2::/64\", "
  803. " \"id\": 100"
  804. " },"
  805. " {"
  806. " \"pools\": [ { \"pool\": \"2001:db8:3::/80\" } ],"
  807. " \"subnet\": \"2001:db8:3::/64\", "
  808. " \"id\": 1"
  809. " },"
  810. " {"
  811. " \"pools\": [ { \"pool\": \"2001:db8:4::/80\" } ],"
  812. " \"subnet\": \"2001:db8:4::/64\", "
  813. " \"id\": 34"
  814. " } ],"
  815. "\"valid-lifetime\": 4000 }";
  816. int cnt = 0; // Number of reconfigurations
  817. ConstElementPtr json;
  818. ASSERT_NO_THROW(json = parseDHCP6(config));
  819. do {
  820. EXPECT_NO_THROW(x = configureDhcp6Server(srv_, json));
  821. checkResult(x, 0);
  822. CfgMgr::instance().commit();
  823. const Subnet6Collection* subnets =
  824. CfgMgr::instance().getCurrentCfg()->getCfgSubnets6()->getAll();
  825. ASSERT_TRUE(subnets);
  826. ASSERT_EQ(4, subnets->size()); // We expect 4 subnets
  827. // Check that subnet ids are as expected.
  828. EXPECT_EQ(1024, subnets->at(0)->getID());
  829. EXPECT_EQ(100, subnets->at(1)->getID());
  830. EXPECT_EQ(1, subnets->at(2)->getID());
  831. EXPECT_EQ(34, subnets->at(3)->getID());
  832. // Repeat reconfiguration process 10 times and check that the subnet-id
  833. // is set to the same value.
  834. } while (++cnt < 3);
  835. }
  836. // CHeck that the configuration with two subnets having the same id is rejected.
  837. TEST_F(Dhcp6ParserTest, multipleSubnetsOverlapingIDs) {
  838. ConstElementPtr x;
  839. // Four subnets, two of them have the same id.
  840. string config = "{ " + genIfaceConfig() + ","
  841. "\"preferred-lifetime\": 3000,"
  842. "\"rebind-timer\": 2000, "
  843. "\"renew-timer\": 1000, "
  844. "\"subnet6\": [ { "
  845. " \"pools\": [ { \"pool\": \"2001:db8:1::/80\" } ],"
  846. " \"subnet\": \"2001:db8:1::/64\", "
  847. " \"id\": 1024"
  848. " },"
  849. " {"
  850. " \"pools\": [ { \"pool\": \"2001:db8:2::/80\" } ],"
  851. " \"subnet\": \"2001:db8:2::/64\", "
  852. " \"id\": 100"
  853. " },"
  854. " {"
  855. " \"pools\": [ { \"pool\": \"2001:db8:3::/80\" } ],"
  856. " \"subnet\": \"2001:db8:3::/64\", "
  857. " \"id\": 1024"
  858. " },"
  859. " {"
  860. " \"pools\": [ { \"pool\": \"2001:db8:4::/80\" } ],"
  861. " \"subnet\": \"2001:db8:4::/64\", "
  862. " \"id\": 34"
  863. " } ],"
  864. "\"valid-lifetime\": 4000 }";
  865. ConstElementPtr json;
  866. ASSERT_NO_THROW(json = parseDHCP6(config));
  867. EXPECT_NO_THROW(x = configureDhcp6Server(srv_, json));
  868. checkResult(x, 1);
  869. EXPECT_TRUE(errorContainsPosition(x, "<string>"));
  870. }
  871. // Goal of this test is to verify that a previously configured subnet can be
  872. // deleted in subsequent reconfiguration.
  873. TEST_F(Dhcp6ParserTest, reconfigureRemoveSubnet) {
  874. ConstElementPtr x;
  875. // All four subnets
  876. string config4 = "{ " + genIfaceConfig() + ","
  877. "\"preferred-lifetime\": 3000,"
  878. "\"rebind-timer\": 2000, "
  879. "\"renew-timer\": 1000, "
  880. "\"subnet6\": [ { "
  881. " \"pools\": [ { \"pool\": \"2001:db8:1::/80\" } ],"
  882. " \"subnet\": \"2001:db8:1::/64\", "
  883. " \"id\": 1"
  884. " },"
  885. " {"
  886. " \"pools\": [ { \"pool\": \"2001:db8:2::/80\" } ],"
  887. " \"subnet\": \"2001:db8:2::/64\", "
  888. " \"id\": 2"
  889. " },"
  890. " {"
  891. " \"pools\": [ { \"pool\": \"2001:db8:3::/80\" } ],"
  892. " \"subnet\": \"2001:db8:3::/64\", "
  893. " \"id\": 3"
  894. " },"
  895. " {"
  896. " \"pools\": [ { \"pool\": \"2001:db8:4::/80\" } ],"
  897. " \"subnet\": \"2001:db8:4::/64\", "
  898. " \"id\": 4"
  899. " } ],"
  900. "\"valid-lifetime\": 4000 }";
  901. // Three subnets (the last one removed)
  902. string config_first3 = "{ " + genIfaceConfig() + ","
  903. "\"preferred-lifetime\": 3000,"
  904. "\"rebind-timer\": 2000, "
  905. "\"renew-timer\": 1000, "
  906. "\"subnet6\": [ { "
  907. " \"pools\": [ { \"pool\": \"2001:db8:1::/80\" } ],"
  908. " \"subnet\": \"2001:db8:1::/64\", "
  909. " \"id\": 1"
  910. " },"
  911. " {"
  912. " \"pools\": [ { \"pool\": \"2001:db8:2::/80\" } ],"
  913. " \"subnet\": \"2001:db8:2::/64\", "
  914. " \"id\": 2"
  915. " },"
  916. " {"
  917. " \"pools\": [ { \"pool\": \"2001:db8:3::/80\" } ],"
  918. " \"subnet\": \"2001:db8:3::/64\", "
  919. " \"id\": 3"
  920. " } ],"
  921. "\"valid-lifetime\": 4000 }";
  922. // Second subnet removed
  923. string config_second_removed = "{ " + genIfaceConfig() + ","
  924. "\"preferred-lifetime\": 3000,"
  925. "\"rebind-timer\": 2000, "
  926. "\"renew-timer\": 1000, "
  927. "\"subnet6\": [ { "
  928. " \"pools\": [ { \"pool\": \"2001:db8:1::/80\" } ],"
  929. " \"subnet\": \"2001:db8:1::/64\", "
  930. " \"id\": 1"
  931. " },"
  932. " {"
  933. " \"pools\": [ { \"pool\": \"2001:db8:3::/80\" } ],"
  934. " \"subnet\": \"2001:db8:3::/64\", "
  935. " \"id\": 3"
  936. " },"
  937. " {"
  938. " \"pools\": [ { \"pool\": \"2001:db8:4::/80\" } ],"
  939. " \"subnet\": \"2001:db8:4::/64\", "
  940. " \"id\": 4"
  941. " } ],"
  942. "\"valid-lifetime\": 4000 }";
  943. // CASE 1: Configure 4 subnets, then reconfigure and remove the
  944. // last one.
  945. ConstElementPtr json;
  946. ASSERT_NO_THROW(json = parseDHCP6(config4));
  947. EXPECT_NO_THROW(x = configureDhcp6Server(srv_, json));
  948. checkResult(x, 0);
  949. CfgMgr::instance().commit();
  950. const Subnet6Collection* subnets =
  951. CfgMgr::instance().getCurrentCfg()->getCfgSubnets6()->getAll();
  952. ASSERT_TRUE(subnets);
  953. ASSERT_EQ(4, subnets->size()); // We expect 4 subnets
  954. // Do the reconfiguration (the last subnet is removed)
  955. ASSERT_NO_THROW(json = parseDHCP6(config_first3));
  956. EXPECT_NO_THROW(x = configureDhcp6Server(srv_, json));
  957. checkResult(x, 0);
  958. CfgMgr::instance().commit();
  959. subnets = CfgMgr::instance().getCurrentCfg()->getCfgSubnets6()->getAll();
  960. ASSERT_TRUE(subnets);
  961. ASSERT_EQ(3, subnets->size()); // We expect 3 subnets now (4th is removed)
  962. EXPECT_EQ(1, subnets->at(0)->getID());
  963. EXPECT_EQ(2, subnets->at(1)->getID());
  964. EXPECT_EQ(3, subnets->at(2)->getID());
  965. /// CASE 2: Configure 4 subnets, then reconfigure and remove one
  966. /// from in between (not first, not last)
  967. ASSERT_NO_THROW(json = parseDHCP6(config4));
  968. EXPECT_NO_THROW(x = configureDhcp6Server(srv_, json));
  969. checkResult(x, 0);
  970. CfgMgr::instance().commit();
  971. // Do reconfiguration
  972. ASSERT_NO_THROW(json = parseDHCP6(config_second_removed));
  973. EXPECT_NO_THROW(x = configureDhcp6Server(srv_, json));
  974. checkResult(x, 0);
  975. CfgMgr::instance().commit();
  976. subnets = CfgMgr::instance().getCurrentCfg()->getCfgSubnets6()->getAll();
  977. ASSERT_TRUE(subnets);
  978. ASSERT_EQ(3, subnets->size()); // We expect 4 subnets
  979. EXPECT_EQ(1, subnets->at(0)->getID());
  980. // The second subnet (with subnet-id = 2) is no longer there
  981. EXPECT_EQ(3, subnets->at(1)->getID());
  982. EXPECT_EQ(4, subnets->at(2)->getID());
  983. }
  984. // This test checks if it is possible to override global values
  985. // on a per subnet basis.
  986. TEST_F(Dhcp6ParserTest, subnetLocal) {
  987. string config = "{ " + genIfaceConfig() + ","
  988. "\"preferred-lifetime\": 3000,"
  989. "\"rebind-timer\": 2000, "
  990. "\"renew-timer\": 1000, "
  991. "\"subnet6\": [ { "
  992. " \"pools\": [ { \"pool\": \"2001:db8:1::1 - 2001:db8:1::ffff\" } ],"
  993. " \"renew-timer\": 1, "
  994. " \"rebind-timer\": 2, "
  995. " \"preferred-lifetime\": 3,"
  996. " \"valid-lifetime\": 4,"
  997. " \"subnet\": \"2001:db8:1::/64\" } ],"
  998. "\"valid-lifetime\": 4000 }";
  999. ConstElementPtr json;
  1000. ASSERT_NO_THROW(json = parseDHCP6(config));
  1001. ConstElementPtr status;
  1002. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  1003. // returned value should be 0 (configuration success)
  1004. checkResult(status, 0);
  1005. Subnet6Ptr subnet = CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->
  1006. selectSubnet(IOAddress("2001:db8:1::5"), classify_);
  1007. ASSERT_TRUE(subnet);
  1008. EXPECT_EQ(1, subnet->getT1());
  1009. EXPECT_EQ(2, subnet->getT2());
  1010. EXPECT_EQ(3, subnet->getPreferred());
  1011. EXPECT_EQ(4, subnet->getValid());
  1012. }
  1013. // This test checks if it is possible to define a subnet with an
  1014. // interface defined.
  1015. TEST_F(Dhcp6ParserTest, subnetInterface) {
  1016. // There should be at least one interface
  1017. string config = "{ " + genIfaceConfig() + ","
  1018. "\"preferred-lifetime\": 3000,"
  1019. "\"rebind-timer\": 2000, "
  1020. "\"renew-timer\": 1000, "
  1021. "\"subnet6\": [ { "
  1022. " \"pools\": [ { \"pool\": \"2001:db8:1::1 - 2001:db8:1::ffff\" } ],"
  1023. " \"interface\": \"" + valid_iface_ + "\","
  1024. " \"subnet\": \"2001:db8:1::/64\" } ],"
  1025. "\"valid-lifetime\": 4000 }";
  1026. cout << config << endl;
  1027. ConstElementPtr json;
  1028. ASSERT_NO_THROW(json = parseDHCP6(config));
  1029. ConstElementPtr status;
  1030. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  1031. // returned value should be 0 (configuration success)
  1032. checkResult(status, 0);
  1033. Subnet6Ptr subnet = CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->
  1034. selectSubnet(IOAddress("2001:db8:1::5"), classify_);
  1035. ASSERT_TRUE(subnet);
  1036. EXPECT_EQ(valid_iface_, subnet->getIface());
  1037. }
  1038. // This test checks if invalid interface name will be rejected in
  1039. // Subnet6 definition.
  1040. TEST_F(Dhcp6ParserTest, subnetInterfaceBogus) {
  1041. // There should be at least one interface
  1042. string config = "{ " + genIfaceConfig() + ","
  1043. "\"preferred-lifetime\": 3000,"
  1044. "\"rebind-timer\": 2000, "
  1045. "\"renew-timer\": 1000, "
  1046. "\"subnet6\": [ { "
  1047. " \"pools\": [ { \"pool\": \"2001:db8:1::1 - 2001:db8:1::ffff\" } ],"
  1048. " \"interface\": \"" + bogus_iface_ + "\","
  1049. " \"subnet\": \"2001:db8:1::/64\" } ],"
  1050. "\"valid-lifetime\": 4000 }";
  1051. cout << config << endl;
  1052. ConstElementPtr json;
  1053. ASSERT_NO_THROW(json = parseDHCP6(config));
  1054. ConstElementPtr status;
  1055. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  1056. // returned value should be 1 (configuration error)
  1057. checkResult(status, 1);
  1058. EXPECT_TRUE(errorContainsPosition(status, "<string>"));
  1059. Subnet6Ptr subnet = CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->
  1060. selectSubnet(IOAddress("2001:db8:1::5"), classify_);
  1061. EXPECT_FALSE(subnet);
  1062. }
  1063. // This test checks if it is not allowed to define global interface
  1064. // parameter.
  1065. TEST_F(Dhcp6ParserTest, interfaceGlobal) {
  1066. string config = "{ " + genIfaceConfig() + ","
  1067. "\"preferred-lifetime\": 3000,"
  1068. "\"rebind-timer\": 2000, "
  1069. "\"renew-timer\": 1000, "
  1070. "\"interface\": \"" + valid_iface_ + "\"," // Not valid. Can be defined in subnet only
  1071. "\"subnet6\": [ { "
  1072. " \"pools\": [ { \"pool\": \"2001:db8:1::1 - 2001:db8:1::ffff\" } ],"
  1073. " \"subnet\": \"2001:db8:1::/64\" } ],"
  1074. "\"valid-lifetime\": 4000 }";
  1075. cout << config << endl;
  1076. ConstElementPtr json = parseJSON(config);
  1077. ConstElementPtr status;
  1078. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  1079. // returned value should be 1 (parse error)
  1080. checkResult(status, 1);
  1081. EXPECT_TRUE(errorContainsPosition(status, "<string>"));
  1082. EXPECT_THROW(parseDHCP6(config), Dhcp6ParseError);
  1083. }
  1084. // This test checks if it is possible to define a subnet with an
  1085. // interface-id option defined.
  1086. TEST_F(Dhcp6ParserTest, subnetInterfaceId) {
  1087. const string valid_interface_id = "foobar";
  1088. const string bogus_interface_id = "blah";
  1089. // There should be at least one interface
  1090. const string config = "{ "
  1091. "\"preferred-lifetime\": 3000,"
  1092. "\"rebind-timer\": 2000, "
  1093. "\"renew-timer\": 1000, "
  1094. "\"subnet6\": [ { "
  1095. " \"pools\": [ { \"pool\": \"2001:db8:1::1 - 2001:db8:1::ffff\" } ],"
  1096. " \"interface-id\": \"" + valid_interface_id + "\","
  1097. " \"subnet\": \"2001:db8:1::/64\" } ],"
  1098. "\"valid-lifetime\": 4000 }";
  1099. ConstElementPtr json;
  1100. ASSERT_NO_THROW(json = parseDHCP6(config));
  1101. ConstElementPtr status;
  1102. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  1103. // Returned value should be 0 (configuration success)
  1104. checkResult(status, 0);
  1105. // Try to get a subnet based on bogus interface-id option
  1106. OptionBuffer tmp(bogus_interface_id.begin(), bogus_interface_id.end());
  1107. SubnetSelector selector;
  1108. selector.first_relay_linkaddr_ = IOAddress("5000::1");
  1109. selector.interface_id_.reset(new Option(Option::V6, D6O_INTERFACE_ID, tmp));
  1110. Subnet6Ptr subnet = CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->
  1111. selectSubnet(selector);
  1112. EXPECT_FALSE(subnet);
  1113. // Now try to get subnet for valid interface-id value
  1114. tmp = OptionBuffer(valid_interface_id.begin(), valid_interface_id.end());
  1115. selector.interface_id_.reset(new Option(Option::V6, D6O_INTERFACE_ID, tmp));
  1116. subnet = CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->
  1117. selectSubnet(selector);
  1118. ASSERT_TRUE(subnet);
  1119. EXPECT_TRUE(selector.interface_id_->equals(subnet->getInterfaceId()));
  1120. }
  1121. // This test checks if it is not allowed to define global interface
  1122. // parameter.
  1123. TEST_F(Dhcp6ParserTest, interfaceIdGlobal) {
  1124. const string config = "{ " + genIfaceConfig() + ","
  1125. "\"preferred-lifetime\": 3000,"
  1126. "\"rebind-timer\": 2000, "
  1127. "\"renew-timer\": 1000, "
  1128. "\"interface-id\": \"foobar\"," // Not valid. Can be defined in subnet only
  1129. "\"subnet6\": [ { "
  1130. " \"pools\": [ { \"pool\": \"2001:db8:1::1 - 2001:db8:1::ffff\" } ],"
  1131. " \"subnet\": \"2001:db8:1::/64\" } ],"
  1132. "\"valid-lifetime\": 4000 }";
  1133. ConstElementPtr json = parseJSON(config);
  1134. ConstElementPtr status;
  1135. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  1136. // Returned value should be 1 (parse error)
  1137. checkResult(status, 1);
  1138. EXPECT_TRUE(errorContainsPosition(status, "<string>"));
  1139. EXPECT_THROW(parseDHCP6(config), Dhcp6ParseError);
  1140. }
  1141. // This test checks if it is not possible to define a subnet with an
  1142. // interface (i.e. local subnet) and interface-id (remote subnet) defined.
  1143. TEST_F(Dhcp6ParserTest, subnetInterfaceAndInterfaceId) {
  1144. const string config = "{ \"preferred-lifetime\": 3000,"
  1145. "\"rebind-timer\": 2000, "
  1146. "\"renew-timer\": 1000, "
  1147. "\"subnet6\": [ { "
  1148. " \"pools\": [ { \"pool\": \"2001:db8:1::1 - 2001:db8:1::ffff\" } ],"
  1149. " \"interface\": \"" + valid_iface_ + "\","
  1150. " \"interface-id\": \"foobar\","
  1151. " \"subnet\": \"2001:db8:1::/64\" } ],"
  1152. "\"valid-lifetime\": 4000 }";
  1153. ConstElementPtr json;
  1154. ASSERT_NO_THROW(json = parseDHCP6(config));
  1155. ConstElementPtr status;
  1156. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  1157. // Returned value should be 1 (configuration error)
  1158. checkResult(status, 1);
  1159. EXPECT_TRUE(errorContainsPosition(status, "<string>"));
  1160. }
  1161. // This test checks the configuration of the Rapid Commit option
  1162. // support for the subnet.
  1163. TEST_F(Dhcp6ParserTest, subnetRapidCommit) {
  1164. {
  1165. // rapid-commit implicitly set to false.
  1166. SCOPED_TRACE("Default Rapid Commit setting");
  1167. testRapidCommit("{ \"preferred-lifetime\": 3000,"
  1168. "\"rebind-timer\": 2000, "
  1169. "\"renew-timer\": 1000, "
  1170. "\"subnet6\": [ { "
  1171. " \"pools\": [ { \"pool\": \"2001:db8:1::1 - "
  1172. "2001:db8:1::ffff\" } ],"
  1173. " \"subnet\": \"2001:db8:1::/64\" } ],"
  1174. "\"valid-lifetime\": 4000 }",
  1175. false);
  1176. }
  1177. {
  1178. // rapid-commit explicitly set to true.
  1179. SCOPED_TRACE("Enable Rapid Commit");
  1180. testRapidCommit("{ \"preferred-lifetime\": 3000,"
  1181. "\"rebind-timer\": 2000, "
  1182. "\"renew-timer\": 1000, "
  1183. "\"subnet6\": [ { "
  1184. " \"pools\": [ { \"pool\": \"2001:db8:1::1 - "
  1185. "2001:db8:1::ffff\" } ],"
  1186. " \"rapid-commit\": true,"
  1187. " \"subnet\": \"2001:db8:1::/64\" } ],"
  1188. "\"valid-lifetime\": 4000 }",
  1189. true);
  1190. }
  1191. {
  1192. // rapid-commit explicitly set to false.
  1193. SCOPED_TRACE("Disable Rapid Commit");
  1194. testRapidCommit("{ \"preferred-lifetime\": 3000,"
  1195. "\"rebind-timer\": 2000, "
  1196. "\"renew-timer\": 1000, "
  1197. "\"subnet6\": [ { "
  1198. " \"pools\": [ { \"pool\": \"2001:db8:1::1 - "
  1199. "2001:db8:1::ffff\" } ],"
  1200. " \"rapid-commit\": false,"
  1201. " \"subnet\": \"2001:db8:1::/64\" } ],"
  1202. "\"valid-lifetime\": 4000 }",
  1203. false);
  1204. }
  1205. }
  1206. // This test checks that multiple pools can be defined and handled properly.
  1207. // The test defines 2 subnets, each with 2 pools.
  1208. TEST_F(Dhcp6ParserTest, multiplePools) {
  1209. // Collection with two subnets, each with 2 pools.
  1210. string config = "{ " + genIfaceConfig() + ","
  1211. "\"preferred-lifetime\": 3000,"
  1212. "\"rebind-timer\": 2000, "
  1213. "\"renew-timer\": 1000, "
  1214. "\"subnet6\": [ { "
  1215. " \"pools\": [ "
  1216. " { \"pool\": \"2001:db8:1::/96\" },"
  1217. " { \"pool\": \"2001:db8:1:0:abcd::/112\" }"
  1218. " ],"
  1219. " \"subnet\": \"2001:db8:1::/64\" "
  1220. " },"
  1221. " {"
  1222. " \"pools\": [ "
  1223. " { \"pool\": \"2001:db8:2::1 - 2001:db8:2::ff\" },"
  1224. " { \"pool\": \"2001:db8:2::300 - 2001:db8:2::3ff\" }"
  1225. " ],"
  1226. " \"subnet\": \"2001:db8:2::/64\""
  1227. " } ],"
  1228. "\"valid-lifetime\": 4000 }";
  1229. ConstElementPtr json;
  1230. ASSERT_NO_THROW(json = parseDHCP6(config));
  1231. ConstElementPtr status;
  1232. ASSERT_NO_THROW(status = configureDhcp6Server(srv_, json));
  1233. checkResult(status, 0);
  1234. const Subnet6Collection* subnets =
  1235. CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->getAll();
  1236. ASSERT_TRUE(subnets);
  1237. ASSERT_EQ(2, subnets->size()); // We expect 2 subnets
  1238. // Check the first subnet
  1239. const PoolCollection& pools1 = subnets->at(0)->getPools(Lease::TYPE_NA);
  1240. ASSERT_EQ(2, pools1.size());
  1241. EXPECT_EQ("type=IA_NA, 2001:db8:1::-2001:db8:1::ffff:ffff, delegated_len=128",
  1242. pools1[0]->toText());
  1243. EXPECT_EQ("type=IA_NA, 2001:db8:1:0:abcd::-2001:db8:1:0:abcd::ffff, delegated_len=128",
  1244. pools1[1]->toText());
  1245. // There shouldn't be any TA or PD pools
  1246. EXPECT_TRUE(subnets->at(0)->getPools(Lease::TYPE_TA).empty());
  1247. EXPECT_TRUE(subnets->at(0)->getPools(Lease::TYPE_PD).empty());
  1248. // Check the second subnet
  1249. const PoolCollection& pools2 = subnets->at(1)->getPools(Lease::TYPE_NA);
  1250. ASSERT_EQ(2, pools2.size());
  1251. EXPECT_EQ("type=IA_NA, 2001:db8:2::1-2001:db8:2::ff, delegated_len=128",
  1252. pools2[0]->toText());
  1253. EXPECT_EQ("type=IA_NA, 2001:db8:2::300-2001:db8:2::3ff, delegated_len=128",
  1254. pools2[1]->toText());
  1255. // There shouldn't be any TA or PD pools
  1256. EXPECT_TRUE(subnets->at(0)->getPools(Lease::TYPE_TA).empty());
  1257. EXPECT_TRUE(subnets->at(0)->getPools(Lease::TYPE_PD).empty());
  1258. }
  1259. // Test verifies that a subnet with pool values that do not belong to that
  1260. // pool are rejected.
  1261. TEST_F(Dhcp6ParserTest, poolOutOfSubnet) {
  1262. string config = "{ " + genIfaceConfig() + ","
  1263. "\"preferred-lifetime\": 3000,"
  1264. "\"rebind-timer\": 2000, "
  1265. "\"renew-timer\": 1000, "
  1266. "\"subnet6\": [ { "
  1267. " \"pools\": [ { \"pool\": \"4001:db8:1::/80\" } ],"
  1268. " \"subnet\": \"2001:db8:1::/64\" } ],"
  1269. "\"valid-lifetime\": 4000 }";
  1270. ConstElementPtr json;
  1271. ASSERT_NO_THROW(json = parseDHCP6(config));
  1272. ConstElementPtr status;
  1273. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  1274. // returned value must be 1 (values error)
  1275. // as the pool does not belong to that subnet
  1276. checkResult(status, 1);
  1277. EXPECT_TRUE(errorContainsPosition(status, "<string>"));
  1278. }
  1279. // Goal of this test is to verify if pools can be defined
  1280. // using prefix/length notation. There is no separate test for min-max
  1281. // notation as it was tested in several previous tests.
  1282. // Note this test also verifies that subnets can be configured without
  1283. // prefix delegation pools.
  1284. TEST_F(Dhcp6ParserTest, poolPrefixLen) {
  1285. ConstElementPtr x;
  1286. string config = "{ " + genIfaceConfig() + ","
  1287. "\"preferred-lifetime\": 3000,"
  1288. "\"rebind-timer\": 2000, "
  1289. "\"renew-timer\": 1000, "
  1290. "\"subnet6\": [ { "
  1291. " \"pools\": [ { \"pool\": \"2001:db8:1::/80\" } ],"
  1292. " \"subnet\": \"2001:db8:1::/64\" } ],"
  1293. "\"valid-lifetime\": 4000 }";
  1294. ConstElementPtr json;
  1295. ASSERT_NO_THROW(json = parseDHCP6(config));
  1296. EXPECT_NO_THROW(x = configureDhcp6Server(srv_, json));
  1297. // returned value must be 1 (configuration parse error)
  1298. checkResult(x, 0);
  1299. Subnet6Ptr subnet = CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->
  1300. selectSubnet(IOAddress("2001:db8:1::5"), classify_);
  1301. ASSERT_TRUE(subnet);
  1302. EXPECT_EQ(1000, subnet->getT1());
  1303. EXPECT_EQ(2000, subnet->getT2());
  1304. EXPECT_EQ(3000, subnet->getPreferred());
  1305. EXPECT_EQ(4000, subnet->getValid());
  1306. }
  1307. // Goal of this test is to verify the basic parsing of a prefix delegation
  1308. // pool. It uses a single, valid pd pool.
  1309. TEST_F(Dhcp6ParserTest, pdPoolBasics) {
  1310. ConstElementPtr x;
  1311. // Define a single valid pd pool.
  1312. string config =
  1313. "{ " + genIfaceConfig() + ","
  1314. "\"preferred-lifetime\": 3000,"
  1315. "\"rebind-timer\": 2000, "
  1316. "\"renew-timer\": 1000, "
  1317. "\"subnet6\": [ { "
  1318. " \"subnet\": \"2001:db8:1::/64\","
  1319. " \"pd-pools\": ["
  1320. " { \"prefix\": \"2001:db8:1::\", "
  1321. " \"prefix-len\": 64, "
  1322. " \"delegated-len\": 128"
  1323. " } ],"
  1324. "\"valid-lifetime\": 4000 }"
  1325. "] }";
  1326. // Convert the JSON string into Elements.
  1327. ConstElementPtr json;
  1328. ASSERT_NO_THROW(json = parseDHCP6(config));
  1329. // Verify that DHCP6 configuration processing succeeds.
  1330. // Returned value must be non-empty ConstElementPtr to config result.
  1331. // rcode should be 0 which indicates successful configuration processing.
  1332. EXPECT_NO_THROW(x = configureDhcp6Server(srv_, json));
  1333. checkResult(x, 0);
  1334. // Test that we can retrieve the subnet.
  1335. Subnet6Ptr subnet = CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->
  1336. selectSubnet(IOAddress("2001:db8:1::5"), classify_);
  1337. ASSERT_TRUE(subnet);
  1338. // Fetch the collection of PD pools. It should have 1 entry.
  1339. PoolCollection pc;
  1340. ASSERT_NO_THROW(pc = subnet->getPools(Lease::TYPE_PD));
  1341. EXPECT_EQ(1, pc.size());
  1342. // Get a pointer to the pd pool instance, and verify its contents.
  1343. Pool6Ptr p6;
  1344. ASSERT_NO_THROW(p6 = boost::dynamic_pointer_cast<Pool6>(pc[0]));
  1345. ASSERT_TRUE(p6);
  1346. EXPECT_EQ("2001:db8:1::", p6->getFirstAddress().toText());
  1347. EXPECT_EQ(128, p6->getLength());
  1348. // prefix-len is not directly accessible after pool construction, so
  1349. // verify that it was interpreted correctly by checking the last address
  1350. // value.
  1351. isc::asiolink::IOAddress prefixAddress("2001:db8:1::");
  1352. EXPECT_EQ(lastAddrInPrefix(prefixAddress, 64), p6->getLastAddress());
  1353. }
  1354. // This test verifies that it is possible to specify a prefix pool with an
  1355. // excluded prefix (see RFC6603).
  1356. TEST_F(Dhcp6ParserTest, pdPoolPrefixExclude) {
  1357. ConstElementPtr x;
  1358. // Define a single valid pd pool.
  1359. string config =
  1360. "{ " + genIfaceConfig() + ","
  1361. "\"preferred-lifetime\": 3000,"
  1362. "\"rebind-timer\": 2000, "
  1363. "\"renew-timer\": 1000, "
  1364. "\"subnet6\": [ { "
  1365. " \"subnet\": \"2001:db8:1::/64\","
  1366. " \"pd-pools\": ["
  1367. " { \"prefix\": \"3000::\", "
  1368. " \"prefix-len\": 48, "
  1369. " \"delegated-len\": 64,"
  1370. " \"excluded-prefix\": \"3000:0:0:0:1000::\","
  1371. " \"excluded-prefix-len\": 72"
  1372. " } ],"
  1373. "\"valid-lifetime\": 4000 }"
  1374. "] }";
  1375. // Convert the JSON string into Elements.
  1376. ElementPtr json;
  1377. ASSERT_NO_THROW(json = Element::fromJSON(config));
  1378. // Verify that DHCP6 configuration processing succeeds.
  1379. // Returned value must be non-empty ConstElementPtr to config result.
  1380. // rcode should be 0 which indicates successful configuration processing.
  1381. EXPECT_NO_THROW(x = configureDhcp6Server(srv_, json));
  1382. checkResult(x, 0);
  1383. // Test that we can retrieve the subnet.
  1384. Subnet6Ptr subnet = CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->
  1385. selectSubnet(IOAddress("2001:db8:1::5"), classify_);
  1386. ASSERT_TRUE(subnet);
  1387. // Fetch the collection of PD pools. It should have 1 entry.
  1388. PoolCollection pc;
  1389. ASSERT_NO_THROW(pc = subnet->getPools(Lease::TYPE_PD));
  1390. EXPECT_EQ(1, pc.size());
  1391. // Get a pointer to the pd pool instance, and verify its contents.
  1392. Pool6Ptr p6;
  1393. ASSERT_NO_THROW(p6 = boost::dynamic_pointer_cast<Pool6>(pc[0]));
  1394. ASSERT_TRUE(p6);
  1395. EXPECT_EQ("3000::", p6->getFirstAddress().toText());
  1396. EXPECT_EQ(64, p6->getLength());
  1397. // This pool should have Prefix Exclude option associated.
  1398. Option6PDExcludePtr pd_exclude_option = p6->getPrefixExcludeOption();
  1399. ASSERT_TRUE(pd_exclude_option);
  1400. // Pick a delegated prefix of 3000:0:0:3:1000::/64 which belongs to our
  1401. // pool of 3000::/48. For this prefix obtain a Prefix Exclude option and
  1402. // verify that it is correct.
  1403. EXPECT_EQ("3000:0:0:3:1000::",
  1404. pd_exclude_option->getExcludedPrefix(IOAddress("3000:0:0:3::"), 64).toText());
  1405. EXPECT_EQ(72, static_cast<unsigned>(pd_exclude_option->getExcludedPrefixLength()));
  1406. }
  1407. // Goal of this test is verify that a list of PD pools can be configured.
  1408. // It also verifies that a subnet may be configured with both regular pools
  1409. // and pd pools.
  1410. TEST_F(Dhcp6ParserTest, pdPoolList) {
  1411. ConstElementPtr x;
  1412. // We will configure three pools of prefixes for the subnet. Note that
  1413. // the 3rd prefix is out of the subnet prefix (the prefix doesn't match
  1414. // the subnet prefix).
  1415. const char* prefixes[] = {
  1416. "2001:db8:1:1::",
  1417. "2001:db8:1:2::",
  1418. "3000:1:3::"
  1419. };
  1420. string config =
  1421. "{ " + genIfaceConfig() + ","
  1422. "\"preferred-lifetime\": 3000,"
  1423. "\"rebind-timer\": 2000, "
  1424. "\"renew-timer\": 1000, "
  1425. "\"subnet6\": [ { "
  1426. " \"pools\": [ { \"pool\": \"2001:db8:1:04::/80\" } ],"
  1427. " \"subnet\": \"2001:db8:1::/40\","
  1428. " \"pd-pools\": ["
  1429. " { \"prefix\": \"2001:db8:1:01::\", "
  1430. " \"prefix-len\": 72, "
  1431. " \"delegated-len\": 80"
  1432. " },"
  1433. " { \"prefix\": \"2001:db8:1:02::\", "
  1434. " \"prefix-len\": 72, "
  1435. " \"delegated-len\": 88"
  1436. " },"
  1437. " { \"prefix\": \"3000:1:03::\", "
  1438. " \"prefix-len\": 72, "
  1439. " \"delegated-len\": 96"
  1440. " }"
  1441. "],"
  1442. "\"valid-lifetime\": 4000 }"
  1443. "] }";
  1444. // Convert the JSON string into Elements.
  1445. ConstElementPtr json;
  1446. ASSERT_NO_THROW(json = parseDHCP6(config));
  1447. // Verify that DHCP6 configuration processing succeeds.
  1448. // Returned value must be non-empty ConstElementPtr to config result.
  1449. // rcode should be 0 which indicates successful configuration processing.
  1450. EXPECT_NO_THROW(x = configureDhcp6Server(srv_, json));
  1451. checkResult(x, 0);
  1452. // Test that we can retrieve the subnet.
  1453. Subnet6Ptr subnet = CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->
  1454. selectSubnet(IOAddress("2001:db8:1::5"), classify_);
  1455. ASSERT_TRUE(subnet);
  1456. // Fetch the collection of NA pools. It should have 1 entry.
  1457. PoolCollection pc;
  1458. ASSERT_NO_THROW(pc = subnet->getPools(Lease::TYPE_NA));
  1459. EXPECT_EQ(1, pc.size());
  1460. // Fetch the collection of PD pools. It should have 3 entries.
  1461. ASSERT_NO_THROW(pc = subnet->getPools(Lease::TYPE_PD));
  1462. EXPECT_EQ(3, pc.size());
  1463. // Loop through the pools and verify their contents.
  1464. for (unsigned int i = 0; i < 3; i++) {
  1465. Pool6Ptr p6;
  1466. ASSERT_NO_THROW(p6 = boost::dynamic_pointer_cast<Pool6>(pc[i]));
  1467. ASSERT_TRUE(p6);
  1468. EXPECT_EQ(prefixes[i], p6->getFirstAddress().toText());
  1469. EXPECT_EQ((80 + (i * 8)), p6->getLength());
  1470. }
  1471. }
  1472. // Goal of this test is to verify the a whole prefix can be delegated and that
  1473. // a whole subnet can be delegated.
  1474. TEST_F(Dhcp6ParserTest, subnetAndPrefixDelegated) {
  1475. ConstElementPtr x;
  1476. // Define a single valid pd pool.
  1477. string config =
  1478. "{ " + genIfaceConfig() + ","
  1479. "\"preferred-lifetime\": 3000,"
  1480. "\"rebind-timer\": 2000, "
  1481. "\"renew-timer\": 1000, "
  1482. "\"subnet6\": [ { "
  1483. " \"subnet\": \"2001:db8:1::/64\","
  1484. " \"pd-pools\": ["
  1485. " { \"prefix\": \"2001:db8:1::\", "
  1486. " \"prefix-len\": 64, "
  1487. " \"delegated-len\": 64"
  1488. " } ],"
  1489. "\"valid-lifetime\": 4000 }"
  1490. "] }";
  1491. // Convert the JSON string into Elements.
  1492. ConstElementPtr json;
  1493. ASSERT_NO_THROW(json = parseDHCP6(config));
  1494. // Verify that DHCP6 configuration processing succeeds.
  1495. // Returned value must be non-empty ConstElementPtr to config result.
  1496. // rcode should be 0 which indicates successful configuration processing.
  1497. EXPECT_NO_THROW(x = configureDhcp6Server(srv_, json));
  1498. checkResult(x, 0);
  1499. // Test that we can retrieve the subnet.
  1500. Subnet6Ptr subnet = CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->
  1501. selectSubnet(IOAddress("2001:db8:1::5"), classify_);
  1502. ASSERT_TRUE(subnet);
  1503. // Fetch the collection of PD pools. It should have 1 entry.
  1504. PoolCollection pc;
  1505. ASSERT_NO_THROW(pc = subnet->getPools(Lease::TYPE_PD));
  1506. EXPECT_EQ(1, pc.size());
  1507. // Get a pointer to the pd pool instance, and verify its contents.
  1508. Pool6Ptr p6;
  1509. ASSERT_NO_THROW(p6 = boost::dynamic_pointer_cast<Pool6>(pc[0]));
  1510. ASSERT_TRUE(p6);
  1511. EXPECT_EQ("2001:db8:1::", p6->getFirstAddress().toText());
  1512. EXPECT_EQ(64, p6->getLength());
  1513. // prefix-len is not directly accessible after pool construction, so
  1514. // verify that it was interpreted correctly by checking the last address
  1515. // value.
  1516. isc::asiolink::IOAddress prefixAddress("2001:db8:1::");
  1517. EXPECT_EQ(lastAddrInPrefix(prefixAddress, 64), p6->getLastAddress());
  1518. }
  1519. // Goal of this test is check for proper handling of invalid prefix delegation
  1520. // pool configuration. It uses an array of invalid configurations to check
  1521. // a variety of configuration errors.
  1522. TEST_F(Dhcp6ParserTest, invalidPdPools) {
  1523. ConstElementPtr x;
  1524. const char *config[] = {
  1525. // No prefix.
  1526. "{ \"interfaces-config\": { \"interfaces\": [ ] },"
  1527. "\"preferred-lifetime\": 3000,"
  1528. "\"rebind-timer\": 2000, "
  1529. "\"renew-timer\": 1000, "
  1530. "\"subnet6\": [ { "
  1531. " \"subnet\": \"2001:db8:1::/64\","
  1532. " \"pd-pools\": ["
  1533. " { "
  1534. " \"prefix-len\": 64, "
  1535. " \"delegated-len\": 128"
  1536. " } ],"
  1537. "\"valid-lifetime\": 4000 }"
  1538. "] }",
  1539. // No prefix-len.
  1540. "{ \"interfaces-config\": { \"interfaces\": [ ] },"
  1541. "\"preferred-lifetime\": 3000,"
  1542. "\"rebind-timer\": 2000, "
  1543. "\"renew-timer\": 1000, "
  1544. "\"subnet6\": [ { "
  1545. " \"subnet\": \"2001:db8:1::/64\","
  1546. " \"pd-pools\": ["
  1547. " { \"prefix\": \"2001:db8:1::\", "
  1548. " \"delegated-len\": 128"
  1549. " } ],"
  1550. "\"valid-lifetime\": 4000 }"
  1551. "] }",
  1552. // No delegated-len.
  1553. "{ \"interfaces-config\": { \"interfaces\": [ ] },"
  1554. "\"preferred-lifetime\": 3000,"
  1555. "\"rebind-timer\": 2000, "
  1556. "\"renew-timer\": 1000, "
  1557. "\"subnet6\": [ { "
  1558. " \"subnet\": \"2001:db8:1::/64\","
  1559. " \"pd-pools\": ["
  1560. " { \"prefix\": \"2001:db8:1::\", "
  1561. " \"prefix-len\": 64 "
  1562. " } ],"
  1563. "\"valid-lifetime\": 4000 }"
  1564. "] }",
  1565. // Delegated length is too short.
  1566. "{ \"interfaces-config\": { \"interfaces\": [ ] },"
  1567. "\"preferred-lifetime\": 3000,"
  1568. "\"rebind-timer\": 2000, "
  1569. "\"renew-timer\": 1000, "
  1570. "\"subnet6\": [ { "
  1571. " \"subnet\": \"2001:db8:1::/64\","
  1572. " \"pd-pools\": ["
  1573. " { \"prefix\": \"2001:db8:1::\", "
  1574. " \"prefix-len\": 128, "
  1575. " \"delegated-len\": 64"
  1576. " } ],"
  1577. "\"valid-lifetime\": 4000 }"
  1578. "] }"
  1579. };
  1580. ConstElementPtr json;
  1581. int num_msgs = sizeof(config)/sizeof(char*);
  1582. for (unsigned int i = 0; i < num_msgs; i++) {
  1583. // Convert JSON string to Elements.
  1584. ASSERT_NO_THROW(json = parseDHCP6(config[i]));
  1585. // Configuration processing should fail without a throw.
  1586. ASSERT_NO_THROW(x = configureDhcp6Server(srv_, json));
  1587. // Returned value must be non-empty ConstElementPtr to config result.
  1588. // rcode should be 1 which indicates configuration error.
  1589. checkResult(x, 1);
  1590. EXPECT_TRUE(errorContainsPosition(x, "<string>"));
  1591. }
  1592. }
  1593. // The goal of this test is to check whether an option definition
  1594. // that defines an option carrying an IPv6 address can be created.
  1595. TEST_F(Dhcp6ParserTest, optionDefIpv6Address) {
  1596. // Configuration string.
  1597. std::string config =
  1598. "{ \"option-def\": [ {"
  1599. " \"name\": \"foo\","
  1600. " \"code\": 100,"
  1601. " \"type\": \"ipv6-address\","
  1602. " \"space\": \"isc\""
  1603. " } ]"
  1604. "}";
  1605. ConstElementPtr json;
  1606. ASSERT_NO_THROW(json = parseOPTION_DEF(config));
  1607. // Make sure that the particular option definition does not exist.
  1608. OptionDefinitionPtr def = CfgMgr::instance().getStagingCfg()->
  1609. getCfgOptionDef()->get("isc", 100);
  1610. ASSERT_FALSE(def);
  1611. // Use the configuration string to create new option definition.
  1612. ConstElementPtr status;
  1613. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  1614. ASSERT_TRUE(status);
  1615. checkResult(status, 0);
  1616. // We need to commit option definitions because later in this test we
  1617. // will be checking if they get removed when "option-def" parameter
  1618. // is removed from a configuration.
  1619. LibDHCP::commitRuntimeOptionDefs();
  1620. // The option definition should now be available in the CfgMgr.
  1621. def = CfgMgr::instance().getStagingCfg()->getCfgOptionDef()->get("isc", 100);
  1622. ASSERT_TRUE(def);
  1623. // Verify that the option definition data is valid.
  1624. EXPECT_EQ("foo", def->getName());
  1625. EXPECT_EQ(100, def->getCode());
  1626. EXPECT_FALSE(def->getArrayType());
  1627. EXPECT_EQ(OPT_IPV6_ADDRESS_TYPE, def->getType());
  1628. // The copy of the option definition should be available in the libdhcp++.
  1629. OptionDefinitionPtr def_libdhcp = LibDHCP::getRuntimeOptionDef("isc", 100);
  1630. ASSERT_TRUE(def_libdhcp);
  1631. // Both definitions should be held in distinct pointers but they should
  1632. // be equal.
  1633. EXPECT_TRUE(def_libdhcp != def);
  1634. EXPECT_TRUE(*def_libdhcp == *def);
  1635. // Let's apply empty configuration. This removes the option definitions
  1636. // configuration and should result in removal of the option 100 from the
  1637. // libdhcp++.
  1638. config = "{ }";
  1639. json = parseOPTION_DEF(config);
  1640. ASSERT_NO_THROW(status = configureDhcp6Server(srv_, json));
  1641. checkResult(status, 0);
  1642. EXPECT_FALSE(LibDHCP::getRuntimeOptionDef("isc", 100));
  1643. }
  1644. // The goal of this test is to check whether an option definition
  1645. // that defines an option carrying a record of data fields can
  1646. // be created.
  1647. TEST_F(Dhcp6ParserTest, optionDefRecord) {
  1648. // Configuration string.
  1649. std::string config =
  1650. "{ \"option-def\": [ {"
  1651. " \"name\": \"foo\","
  1652. " \"code\": 100,"
  1653. " \"type\": \"record\","
  1654. " \"record-types\": \"uint16, ipv4-address, ipv6-address, string\","
  1655. " \"space\": \"isc\""
  1656. " } ]"
  1657. "}";
  1658. ConstElementPtr json;
  1659. ASSERT_NO_THROW(json = parseOPTION_DEF(config));
  1660. // Make sure that the particular option definition does not exist.
  1661. OptionDefinitionPtr def = CfgMgr::instance().getStagingCfg()->
  1662. getCfgOptionDef()->get("isc", 100);
  1663. ASSERT_FALSE(def);
  1664. // Use the configuration string to create new option definition.
  1665. ConstElementPtr status;
  1666. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  1667. ASSERT_TRUE(status);
  1668. checkResult(status, 0);
  1669. // The option definition should now be available in the CfgMgr.
  1670. def = CfgMgr::instance().getStagingCfg()->getCfgOptionDef()->get("isc", 100);
  1671. ASSERT_TRUE(def);
  1672. // Check the option data.
  1673. EXPECT_EQ("foo", def->getName());
  1674. EXPECT_EQ(100, def->getCode());
  1675. EXPECT_EQ(OPT_RECORD_TYPE, def->getType());
  1676. EXPECT_FALSE(def->getArrayType());
  1677. // The option comprises the record of data fields. Verify that all
  1678. // fields are present and they are of the expected types.
  1679. const OptionDefinition::RecordFieldsCollection& record_fields =
  1680. def->getRecordFields();
  1681. ASSERT_EQ(4, record_fields.size());
  1682. EXPECT_EQ(OPT_UINT16_TYPE, record_fields[0]);
  1683. EXPECT_EQ(OPT_IPV4_ADDRESS_TYPE, record_fields[1]);
  1684. EXPECT_EQ(OPT_IPV6_ADDRESS_TYPE, record_fields[2]);
  1685. EXPECT_EQ(OPT_STRING_TYPE, record_fields[3]);
  1686. }
  1687. // The goal of this test is to verify that multiple option definitions
  1688. // can be created.
  1689. TEST_F(Dhcp6ParserTest, optionDefMultiple) {
  1690. // Configuration string.
  1691. std::string config =
  1692. "{ \"option-def\": [ {"
  1693. " \"name\": \"foo\","
  1694. " \"code\": 100,"
  1695. " \"type\": \"uint32\","
  1696. " \"space\": \"isc\""
  1697. " },"
  1698. " {"
  1699. " \"name\": \"foo-2\","
  1700. " \"code\": 101,"
  1701. " \"type\": \"ipv4-address\","
  1702. " \"space\": \"isc\""
  1703. " } ]"
  1704. "}";
  1705. ConstElementPtr json;
  1706. ASSERT_NO_THROW(json = parseOPTION_DEF(config));
  1707. // Make sure that the option definitions do not exist yet.
  1708. ASSERT_FALSE(CfgMgr::instance().getStagingCfg()->
  1709. getCfgOptionDef()->get("isc", 100));
  1710. ASSERT_FALSE(CfgMgr::instance().getStagingCfg()->
  1711. getCfgOptionDef()->get("isc", 101));
  1712. // Use the configuration string to create new option definitions.
  1713. ConstElementPtr status;
  1714. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  1715. ASSERT_TRUE(status);
  1716. checkResult(status, 0);
  1717. // Check the first definition we have created.
  1718. OptionDefinitionPtr def1 = CfgMgr::instance().getStagingCfg()->
  1719. getCfgOptionDef()->get("isc", 100);
  1720. ASSERT_TRUE(def1);
  1721. // Check the option data.
  1722. EXPECT_EQ("foo", def1->getName());
  1723. EXPECT_EQ(100, def1->getCode());
  1724. EXPECT_EQ(OPT_UINT32_TYPE, def1->getType());
  1725. EXPECT_FALSE(def1->getArrayType());
  1726. // Check the second option definition we have created.
  1727. OptionDefinitionPtr def2 = CfgMgr::instance().getStagingCfg()->
  1728. getCfgOptionDef()->get("isc", 101);
  1729. ASSERT_TRUE(def2);
  1730. // Check the option data.
  1731. EXPECT_EQ("foo-2", def2->getName());
  1732. EXPECT_EQ(101, def2->getCode());
  1733. EXPECT_EQ(OPT_IPV4_ADDRESS_TYPE, def2->getType());
  1734. EXPECT_FALSE(def2->getArrayType());
  1735. }
  1736. // The goal of this test is to verify that the duplicated option
  1737. // definition is not accepted.
  1738. TEST_F(Dhcp6ParserTest, optionDefDuplicate) {
  1739. // Preconfigure libdhcp++ with option definitions. The new configuration
  1740. // should override it, but when the new configuration fails, it should
  1741. // revert to this original configuration.
  1742. OptionDefSpaceContainer defs;
  1743. OptionDefinitionPtr def(new OptionDefinition("bar", 233, "string"));
  1744. defs.addItem(def, "isc");
  1745. LibDHCP::setRuntimeOptionDefs(defs);
  1746. LibDHCP::commitRuntimeOptionDefs();
  1747. // Configuration string. Both option definitions have
  1748. // the same code and belong to the same option space.
  1749. // This configuration should not be accepted.
  1750. std::string config =
  1751. "{ \"option-def\": [ {"
  1752. " \"name\": \"foo\","
  1753. " \"code\": 100,"
  1754. " \"type\": \"uint32\","
  1755. " \"space\": \"isc\""
  1756. " },"
  1757. " {"
  1758. " \"name\": \"foo-2\","
  1759. " \"code\": 100,"
  1760. " \"type\": \"ipv4-address\","
  1761. " \"space\": \"isc\""
  1762. " } ]"
  1763. "}";
  1764. ConstElementPtr json;
  1765. ASSERT_NO_THROW(json = parseOPTION_DEF(config));
  1766. // Make sure that the option definition does not exist yet.
  1767. ASSERT_FALSE(CfgMgr::instance().getStagingCfg()->
  1768. getCfgOptionDef()->get("isc", 100));
  1769. // Use the configuration string to create new option definitions.
  1770. ConstElementPtr status;
  1771. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  1772. ASSERT_TRUE(status);
  1773. checkResult(status, 1);
  1774. EXPECT_TRUE(errorContainsPosition(status, "<string>"));
  1775. // The new configuration should have inserted option 100, but
  1776. // once configuration failed (on the duplicate option definition)
  1777. // the original configuration in libdhcp++ should be reverted.
  1778. EXPECT_FALSE(LibDHCP::getRuntimeOptionDef("isc", 100));
  1779. def = LibDHCP::getRuntimeOptionDef("isc", 233);
  1780. ASSERT_TRUE(def);
  1781. EXPECT_EQ("bar", def->getName());
  1782. EXPECT_EQ(233, def->getCode());
  1783. }
  1784. // The goal of this test is to verify that the option definition
  1785. // comprising an array of uint32 values can be created.
  1786. TEST_F(Dhcp6ParserTest, optionDefArray) {
  1787. // Configuration string. Created option definition should
  1788. // comprise an array of uint32 values.
  1789. std::string config =
  1790. "{ \"option-def\": [ {"
  1791. " \"name\": \"foo\","
  1792. " \"code\": 100,"
  1793. " \"type\": \"uint32\","
  1794. " \"array\": true,"
  1795. " \"space\": \"isc\""
  1796. " } ]"
  1797. "}";
  1798. ConstElementPtr json;
  1799. ASSERT_NO_THROW(json = parseOPTION_DEF(config));
  1800. // Make sure that the particular option definition does not exist.
  1801. OptionDefinitionPtr def = CfgMgr::instance().getStagingCfg()->
  1802. getCfgOptionDef()->get("isc", 100);
  1803. ASSERT_FALSE(def);
  1804. // Use the configuration string to create new option definition.
  1805. ConstElementPtr status;
  1806. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  1807. ASSERT_TRUE(status);
  1808. checkResult(status, 0);
  1809. // The option definition should now be available in the CfgMgr.
  1810. def = CfgMgr::instance().getStagingCfg()->getCfgOptionDef()->get("isc", 100);
  1811. ASSERT_TRUE(def);
  1812. // Check the option data.
  1813. EXPECT_EQ("foo", def->getName());
  1814. EXPECT_EQ(100, def->getCode());
  1815. EXPECT_EQ(OPT_UINT32_TYPE, def->getType());
  1816. EXPECT_TRUE(def->getArrayType());
  1817. }
  1818. // The purpose of this test to verify that encapsulated option
  1819. // space name may be specified.
  1820. TEST_F(Dhcp6ParserTest, optionDefEncapsulate) {
  1821. // Configuration string. Included the encapsulated
  1822. // option space name.
  1823. std::string config =
  1824. "{ \"option-def\": [ {"
  1825. " \"name\": \"foo\","
  1826. " \"code\": 100,"
  1827. " \"type\": \"uint32\","
  1828. " \"space\": \"isc\","
  1829. " \"encapsulate\": \"sub-opts-space\""
  1830. " } ]"
  1831. "}";
  1832. ConstElementPtr json;
  1833. ASSERT_NO_THROW(json = parseOPTION_DEF(config));
  1834. // Make sure that the particular option definition does not exist.
  1835. OptionDefinitionPtr def = CfgMgr::instance().getStagingCfg()->
  1836. getCfgOptionDef()->get("isc", 100);
  1837. ASSERT_FALSE(def);
  1838. // Use the configuration string to create new option definition.
  1839. ConstElementPtr status;
  1840. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  1841. ASSERT_TRUE(status);
  1842. checkResult(status, 0);
  1843. // The option definition should now be available in the CfgMgr.
  1844. def = CfgMgr::instance().getStagingCfg()->getCfgOptionDef()->get("isc", 100);
  1845. ASSERT_TRUE(def);
  1846. // Check the option data.
  1847. EXPECT_EQ("foo", def->getName());
  1848. EXPECT_EQ(100, def->getCode());
  1849. EXPECT_EQ(OPT_UINT32_TYPE, def->getType());
  1850. EXPECT_FALSE(def->getArrayType());
  1851. EXPECT_EQ("sub-opts-space", def->getEncapsulatedSpace());
  1852. }
  1853. /// The purpose of this test is to verify that the option definition
  1854. /// with invalid name is not accepted.
  1855. TEST_F(Dhcp6ParserTest, optionDefInvalidName) {
  1856. // Configuration string. The option name is invalid as it
  1857. // contains the % character.
  1858. std::string config =
  1859. "{ \"option-def\": [ {"
  1860. " \"name\": \"invalid%name\","
  1861. " \"code\": 100,"
  1862. " \"type\": \"string\","
  1863. " \"space\": \"isc\""
  1864. " } ]"
  1865. "}";
  1866. ConstElementPtr json;
  1867. ASSERT_NO_THROW(json = parseOPTION_DEF(config));
  1868. // Use the configuration string to create new option definition.
  1869. ConstElementPtr status;
  1870. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  1871. ASSERT_TRUE(status);
  1872. // Expecting parsing error (error code 1).
  1873. checkResult(status, 1);
  1874. EXPECT_TRUE(errorContainsPosition(status, "<string>"));
  1875. }
  1876. /// The purpose of this test is to verify that the option definition
  1877. /// with invalid type is not accepted.
  1878. TEST_F(Dhcp6ParserTest, optionDefInvalidType) {
  1879. // Configuration string. The option type is invalid. It is
  1880. // "sting" instead of "string".
  1881. std::string config =
  1882. "{ \"option-def\": [ {"
  1883. " \"name\": \"foo\","
  1884. " \"code\": 100,"
  1885. " \"type\": \"sting\","
  1886. " \"space\": \"isc\""
  1887. " } ]"
  1888. "}";
  1889. ConstElementPtr json;
  1890. ASSERT_NO_THROW(json = parseOPTION_DEF(config));
  1891. // Use the configuration string to create new option definition.
  1892. ConstElementPtr status;
  1893. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  1894. ASSERT_TRUE(status);
  1895. // Expecting parsing error (error code 1).
  1896. checkResult(status, 1);
  1897. EXPECT_TRUE(errorContainsPosition(status, "<string>"));
  1898. }
  1899. /// The purpose of this test is to verify that the option definition
  1900. /// with invalid type is not accepted.
  1901. TEST_F(Dhcp6ParserTest, optionDefInvalidRecordType) {
  1902. // Configuration string. The third of the record fields
  1903. // is invalid. It is "sting" instead of "string".
  1904. std::string config =
  1905. "{ \"option-def\": [ {"
  1906. " \"name\": \"foo\","
  1907. " \"code\": 100,"
  1908. " \"type\": \"record\","
  1909. " \"record-types\": \"uint32,uint8,sting\","
  1910. " \"space\": \"isc\""
  1911. " } ]"
  1912. "}";
  1913. ConstElementPtr json;
  1914. ASSERT_NO_THROW(json = parseOPTION_DEF(config));
  1915. // Use the configuration string to create new option definition.
  1916. ConstElementPtr status;
  1917. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  1918. ASSERT_TRUE(status);
  1919. // Expecting parsing error (error code 1).
  1920. checkResult(status, 1);
  1921. EXPECT_TRUE(errorContainsPosition(status, "<string>"));
  1922. }
  1923. /// The goal of this test is to verify that the invalid encapsulated
  1924. /// option space name is not accepted.
  1925. TEST_F(Dhcp6ParserTest, optionDefInvalidEncapsulatedSpace) {
  1926. // Configuration string. The encapsulated option space
  1927. // name is invalid (% character is not allowed).
  1928. std::string config =
  1929. "{ \"option-def\": [ {"
  1930. " \"name\": \"foo\","
  1931. " \"code\": 100,"
  1932. " \"type\": \"uint32\","
  1933. " \"space\": \"isc\","
  1934. " \"encapsulate\": \"invalid%space%name\""
  1935. " } ]"
  1936. "}";
  1937. ConstElementPtr json;
  1938. ASSERT_NO_THROW(json = parseOPTION_DEF(config));
  1939. // Use the configuration string to create new option definition.
  1940. ConstElementPtr status;
  1941. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  1942. ASSERT_TRUE(status);
  1943. // Expecting parsing error (error code 1).
  1944. checkResult(status, 1);
  1945. EXPECT_TRUE(errorContainsPosition(status, "<string>"));
  1946. }
  1947. /// The goal of this test is to verify that the encapsulated
  1948. /// option space name can't be specified for the option that
  1949. /// comprises an array of data fields.
  1950. TEST_F(Dhcp6ParserTest, optionDefEncapsulatedSpaceAndArray) {
  1951. // Configuration string. The encapsulated option space
  1952. // name is set to non-empty value and the array flag
  1953. // is set.
  1954. std::string config =
  1955. "{ \"option-def\": [ {"
  1956. " \"name\": \"foo\","
  1957. " \"code\": 100,"
  1958. " \"type\": \"uint32\","
  1959. " \"array\": true,"
  1960. " \"space\": \"isc\","
  1961. " \"encapsulate\": \"valid-space-name\""
  1962. " } ]"
  1963. "}";
  1964. ConstElementPtr json;
  1965. ASSERT_NO_THROW(json = parseOPTION_DEF(config));
  1966. // Use the configuration string to create new option definition.
  1967. ConstElementPtr status;
  1968. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  1969. ASSERT_TRUE(status);
  1970. // Expecting parsing error (error code 1).
  1971. checkResult(status, 1);
  1972. EXPECT_TRUE(errorContainsPosition(status, "<string>"));
  1973. }
  1974. /// The goal of this test is to verify that the option may not
  1975. /// encapsulate option space it belongs to.
  1976. TEST_F(Dhcp6ParserTest, optionDefEncapsulateOwnSpace) {
  1977. // Configuration string. Option is set to encapsulate
  1978. // option space it belongs to.
  1979. std::string config =
  1980. "{ \"option-def\": [ {"
  1981. " \"name\": \"foo\","
  1982. " \"code\": 100,"
  1983. " \"type\": \"uint32\","
  1984. " \"space\": \"isc\","
  1985. " \"encapsulate\": \"isc\""
  1986. " } ]"
  1987. "}";
  1988. ConstElementPtr json;
  1989. ASSERT_NO_THROW(json = parseOPTION_DEF(config));
  1990. // Use the configuration string to create new option definition.
  1991. ConstElementPtr status;
  1992. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  1993. ASSERT_TRUE(status);
  1994. // Expecting parsing error (error code 1).
  1995. checkResult(status, 1);
  1996. EXPECT_TRUE(errorContainsPosition(status, "<string>"));
  1997. }
  1998. /// The purpose of this test is to verify that it is not allowed
  1999. /// to override the standard option (that belongs to dhcp6 option
  2000. /// space and has its definition) and that it is allowed to define
  2001. /// option in the dhcp6 option space that has a code which is not
  2002. /// used by any of the standard options.
  2003. TEST_F(Dhcp6ParserTest, optionStandardDefOverride) {
  2004. // Configuration string. The option code 100 is unassigned
  2005. // so it can be used for a custom option definition in
  2006. // dhcp6 option space.
  2007. std::string config =
  2008. "{ \"option-def\": [ {"
  2009. " \"name\": \"foo\","
  2010. " \"code\": 100,"
  2011. " \"type\": \"string\","
  2012. " \"space\": \"dhcp6\""
  2013. " } ]"
  2014. "}";
  2015. ConstElementPtr json;
  2016. ASSERT_NO_THROW(json = parseOPTION_DEF(config));
  2017. OptionDefinitionPtr def = CfgMgr::instance().getStagingCfg()->
  2018. getCfgOptionDef()->get(DHCP6_OPTION_SPACE, 100);
  2019. ASSERT_FALSE(def);
  2020. // Use the configuration string to create new option definition.
  2021. ConstElementPtr status;
  2022. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  2023. ASSERT_TRUE(status);
  2024. checkResult(status, 0);
  2025. // The option definition should now be available in the CfgMgr.
  2026. def = CfgMgr::instance().getStagingCfg()->
  2027. getCfgOptionDef()->get(DHCP6_OPTION_SPACE, 100);
  2028. ASSERT_TRUE(def);
  2029. // Check the option data.
  2030. EXPECT_EQ("foo", def->getName());
  2031. EXPECT_EQ(100, def->getCode());
  2032. EXPECT_EQ(OPT_STRING_TYPE, def->getType());
  2033. EXPECT_FALSE(def->getArrayType());
  2034. // The combination of option space and code is invalid. The 'dhcp6'
  2035. // option space groups standard options and the code 3 is reserved
  2036. // for one of them.
  2037. config =
  2038. "{ \"option-def\": [ {"
  2039. " \"name\": \"foo\","
  2040. " \"code\": 3,"
  2041. " \"type\": \"string\","
  2042. " \"space\": \"dhcp6\""
  2043. " } ]"
  2044. "}";
  2045. json = parseOPTION_DEF(config);
  2046. // Use the configuration string to create new option definition.
  2047. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  2048. ASSERT_TRUE(status);
  2049. // Expecting parsing error (error code 1).
  2050. checkResult(status, 1);
  2051. EXPECT_TRUE(errorContainsPosition(status, "<string>"));
  2052. /// @todo The option 63 is a standard DHCPv6 option. However, at this point
  2053. /// there is no definition for this option in libdhcp++, so it should be
  2054. /// allowed to define it from the configuration interface. This test will
  2055. /// have to be removed once definitions for remaining standard options are
  2056. /// created.
  2057. config =
  2058. "{ \"option-def\": [ {"
  2059. " \"name\": \"geolocation\","
  2060. " \"code\": 63,"
  2061. " \"type\": \"string\","
  2062. " \"space\": \"dhcp6\""
  2063. " } ]"
  2064. "}";
  2065. json = parseOPTION_DEF(config);
  2066. // Use the configuration string to create new option definition.
  2067. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  2068. ASSERT_TRUE(status);
  2069. // Expecting success.
  2070. checkResult(status, 0);
  2071. def = CfgMgr::instance().getStagingCfg()->
  2072. getCfgOptionDef()->get(DHCP6_OPTION_SPACE, 63);
  2073. ASSERT_TRUE(def);
  2074. // Check the option data.
  2075. EXPECT_EQ("geolocation", def->getName());
  2076. EXPECT_EQ(63, def->getCode());
  2077. EXPECT_EQ(OPT_STRING_TYPE, def->getType());
  2078. EXPECT_FALSE(def->getArrayType());
  2079. }
  2080. // Goal of this test is to verify that global option data is configured
  2081. TEST_F(Dhcp6ParserTest, optionDataDefaultsGlobal) {
  2082. ConstElementPtr x;
  2083. string config = "{ " + genIfaceConfig() + ","
  2084. "\"preferred-lifetime\": 3000,"
  2085. "\"rebind-timer\": 2000,"
  2086. "\"renew-timer\": 1000,"
  2087. "\"option-data\": [ {"
  2088. " \"name\": \"subscriber-id\","
  2089. " \"data\": \"ABCDEF0105\","
  2090. " \"csv-format\": false"
  2091. " },"
  2092. " {"
  2093. " \"name\": \"preference\","
  2094. " \"data\": \"01\""
  2095. " } ],"
  2096. "\"subnet6\": [ { "
  2097. " \"pools\": [ { \"pool\": \"2001:db8:1::/80\" } ],"
  2098. " \"subnet\": \"2001:db8:1::/64\""
  2099. " } ],"
  2100. "\"valid-lifetime\": 4000 }";
  2101. ConstElementPtr json;
  2102. ASSERT_NO_THROW(json = parseDHCP6(config));
  2103. EXPECT_NO_THROW(x = configureDhcp6Server(srv_, json));
  2104. checkResult(x, 0);
  2105. // These options are global
  2106. Subnet6Ptr subnet = CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->
  2107. selectSubnet(IOAddress("2001:db8:1::5"), classify_);
  2108. ASSERT_TRUE(subnet);
  2109. OptionContainerPtr options = subnet->getCfgOption()->getAll(DHCP6_OPTION_SPACE);
  2110. ASSERT_EQ(0, options->size());
  2111. options = CfgMgr::instance().getStagingCfg()->getCfgOption()->getAll(DHCP6_OPTION_SPACE);
  2112. ASSERT_EQ(2, options->size());
  2113. // Get the search index. Index #1 is to search using option code.
  2114. const OptionContainerTypeIndex& idx = options->get<1>();
  2115. // Get the options for specified index. Expecting one option to be
  2116. // returned but in theory we may have multiple options with the same
  2117. // code so we get the range.
  2118. std::pair<OptionContainerTypeIndex::const_iterator,
  2119. OptionContainerTypeIndex::const_iterator> range =
  2120. idx.equal_range(D6O_SUBSCRIBER_ID);
  2121. // Expect single option with the code equal to 38.
  2122. ASSERT_EQ(1, std::distance(range.first, range.second));
  2123. const uint8_t subid_expected[] = {
  2124. 0xAB, 0xCD, 0xEF, 0x01, 0x05
  2125. };
  2126. // Check if option is valid in terms of code and carried data.
  2127. testOption(*range.first, D6O_SUBSCRIBER_ID, subid_expected,
  2128. sizeof(subid_expected));
  2129. range = idx.equal_range(D6O_PREFERENCE);
  2130. ASSERT_EQ(1, std::distance(range.first, range.second));
  2131. // Do another round of testing with second option.
  2132. const uint8_t pref_expected[] = {
  2133. 0x01
  2134. };
  2135. testOption(*range.first, D6O_PREFERENCE, pref_expected,
  2136. sizeof(pref_expected));
  2137. // Check that options with other option codes are not returned.
  2138. for (uint16_t code = 47; code < 57; ++code) {
  2139. range = idx.equal_range(code);
  2140. EXPECT_EQ(0, std::distance(range.first, range.second));
  2141. }
  2142. }
  2143. // Goal of this test is to verify that subnet option data is configured
  2144. TEST_F(Dhcp6ParserTest, optionDataDefaultsSubnet) {
  2145. ConstElementPtr x;
  2146. string config = "{ " + genIfaceConfig() + ","
  2147. "\"preferred-lifetime\": 3000,"
  2148. "\"rebind-timer\": 2000,"
  2149. "\"renew-timer\": 1000,"
  2150. "\"subnet6\": [ { "
  2151. " \"pools\": [ { \"pool\": \"2001:db8:1::/80\" } ],"
  2152. " \"subnet\": \"2001:db8:1::/64\","
  2153. " \"option-data\": [ {"
  2154. " \"name\": \"subscriber-id\","
  2155. " \"data\": \"ABCDEF0105\","
  2156. " \"csv-format\": false"
  2157. " },"
  2158. " {"
  2159. " \"name\": \"preference\","
  2160. " \"data\": \"01\""
  2161. " } ]"
  2162. " } ],"
  2163. "\"valid-lifetime\": 4000 }";
  2164. ConstElementPtr json;
  2165. ASSERT_NO_THROW(json = parseDHCP6(config));
  2166. EXPECT_NO_THROW(x = configureDhcp6Server(srv_, json));
  2167. checkResult(x, 0);
  2168. // These options are subnet options
  2169. OptionContainerPtr options =
  2170. CfgMgr::instance().getStagingCfg()->getCfgOption()->getAll(DHCP6_OPTION_SPACE);
  2171. ASSERT_EQ(0, options->size());
  2172. Subnet6Ptr subnet = CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->
  2173. selectSubnet(IOAddress("2001:db8:1::5"), classify_);
  2174. ASSERT_TRUE(subnet);
  2175. options = subnet->getCfgOption()->getAll(DHCP6_OPTION_SPACE);
  2176. ASSERT_EQ(2, options->size());
  2177. // Get the search index. Index #1 is to search using option code.
  2178. const OptionContainerTypeIndex& idx = options->get<1>();
  2179. // Get the options for specified index. Expecting one option to be
  2180. // returned but in theory we may have multiple options with the same
  2181. // code so we get the range.
  2182. std::pair<OptionContainerTypeIndex::const_iterator,
  2183. OptionContainerTypeIndex::const_iterator> range =
  2184. idx.equal_range(D6O_SUBSCRIBER_ID);
  2185. // Expect single option with the code equal to 38.
  2186. ASSERT_EQ(1, std::distance(range.first, range.second));
  2187. const uint8_t subid_expected[] = {
  2188. 0xAB, 0xCD, 0xEF, 0x01, 0x05
  2189. };
  2190. // Check if option is valid in terms of code and carried data.
  2191. testOption(*range.first, D6O_SUBSCRIBER_ID, subid_expected,
  2192. sizeof(subid_expected));
  2193. range = idx.equal_range(D6O_PREFERENCE);
  2194. ASSERT_EQ(1, std::distance(range.first, range.second));
  2195. // Do another round of testing with second option.
  2196. const uint8_t pref_expected[] = {
  2197. 0x01
  2198. };
  2199. testOption(*range.first, D6O_PREFERENCE, pref_expected,
  2200. sizeof(pref_expected));
  2201. // Check that options with other option codes are not returned.
  2202. for (uint16_t code = 47; code < 57; ++code) {
  2203. range = idx.equal_range(code);
  2204. EXPECT_EQ(0, std::distance(range.first, range.second));
  2205. }
  2206. }
  2207. /// The goal of this test is to verify that two options having the same
  2208. /// option code can be added to different option spaces.
  2209. TEST_F(Dhcp6ParserTest, optionDataTwoSpaces) {
  2210. // This configuration string is to configure two options
  2211. // sharing the code 56 and having different definitions
  2212. // and belonging to the different option spaces.
  2213. // The option definition must be provided for the
  2214. // option that belongs to the 'isc' option space.
  2215. // The definition is not required for the option that
  2216. // belongs to the 'dhcp6' option space as it is the
  2217. // standard option.
  2218. string config = "{ " + genIfaceConfig() + ","
  2219. "\"preferred-lifetime\": 3000,"
  2220. "\"valid-lifetime\": 4000,"
  2221. "\"rebind-timer\": 2000,"
  2222. "\"renew-timer\": 1000,"
  2223. "\"option-data\": [ {"
  2224. " \"name\": \"subscriber-id\","
  2225. " \"data\": \"ABCDEF0105\","
  2226. " \"csv-format\": false"
  2227. " },"
  2228. " {"
  2229. " \"name\": \"foo\","
  2230. " \"space\": \"isc\","
  2231. " \"data\": \"1234\""
  2232. " } ],"
  2233. "\"option-def\": [ {"
  2234. " \"name\": \"foo\","
  2235. " \"code\": 38,"
  2236. " \"type\": \"uint32\","
  2237. " \"space\": \"isc\""
  2238. " } ],"
  2239. "\"subnet6\": [ { "
  2240. " \"pools\": [ { \"pool\": \"2001:db8:1::/80\" } ],"
  2241. " \"subnet\": \"2001:db8:1::/64\""
  2242. " } ]"
  2243. "}";
  2244. ConstElementPtr json;
  2245. ASSERT_NO_THROW(json = parseDHCP6(config));
  2246. ConstElementPtr status;
  2247. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  2248. ASSERT_TRUE(status);
  2249. checkResult(status, 0);
  2250. // Options should be now available
  2251. // Try to get the option from the space dhcp6.
  2252. OptionDescriptor desc1 =
  2253. CfgMgr::instance().getStagingCfg()->getCfgOption()->get(DHCP6_OPTION_SPACE, 38);
  2254. ASSERT_TRUE(desc1.option_);
  2255. EXPECT_EQ(38, desc1.option_->getType());
  2256. // Try to get the option from the space isc.
  2257. OptionDescriptor desc2 =
  2258. CfgMgr::instance().getStagingCfg()->getCfgOption()->get("isc", 38);
  2259. ASSERT_TRUE(desc2.option_);
  2260. EXPECT_EQ(38, desc1.option_->getType());
  2261. // Try to get the non-existing option from the non-existing
  2262. // option space and expect that option is not returned.
  2263. OptionDescriptor desc3 = CfgMgr::instance().getStagingCfg()->
  2264. getCfgOption()->get("non-existing", 38);
  2265. ASSERT_FALSE(desc3.option_);
  2266. }
  2267. // The goal of this test is to verify that it is possible to
  2268. // encapsulate option space containing some options with
  2269. // another option. In this test we create base option that
  2270. // encapsulates option space 'isc' that comprises two other
  2271. // options. Also, for all options their definitions are
  2272. // created.
  2273. TEST_F(Dhcp6ParserTest, optionDataEncapsulate) {
  2274. // @todo DHCP configurations has many dependencies between
  2275. // parameters. First of all, configuration for subnet is
  2276. // inherited from the global values. Thus subnet has to be
  2277. // configured when all global values have been configured.
  2278. // Also, an option can encapsulate another option only
  2279. // if the latter has been configured. For this reason in this
  2280. // test we created two-stage configuration where first we
  2281. // created options that belong to encapsulated option space.
  2282. // In the second stage we add the base option. Also, the Subnet
  2283. // object is configured in the second stage so it is created
  2284. // at the very end (when all other parameters are configured).
  2285. // Starting stage 1. Configure sub-options and their definitions.
  2286. string config = "{ " + genIfaceConfig() + ","
  2287. "\"preferred-lifetime\": 3000,"
  2288. "\"valid-lifetime\": 4000,"
  2289. "\"rebind-timer\": 2000,"
  2290. "\"renew-timer\": 1000,"
  2291. "\"option-data\": [ {"
  2292. " \"name\": \"foo\","
  2293. " \"space\": \"isc\","
  2294. " \"data\": \"1234\""
  2295. " },"
  2296. " {"
  2297. " \"name\": \"foo2\","
  2298. " \"space\": \"isc\","
  2299. " \"data\": \"192.168.2.1\""
  2300. " } ],"
  2301. "\"option-def\": [ {"
  2302. " \"name\": \"foo\","
  2303. " \"code\": 110,"
  2304. " \"type\": \"uint32\","
  2305. " \"space\": \"isc\""
  2306. " },"
  2307. " {"
  2308. " \"name\": \"foo2\","
  2309. " \"code\": 111,"
  2310. " \"type\": \"ipv4-address\","
  2311. " \"space\": \"isc\""
  2312. " } ]"
  2313. "}";
  2314. ConstElementPtr json;
  2315. ASSERT_NO_THROW(json = parseDHCP6(config));
  2316. ConstElementPtr status;
  2317. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  2318. ASSERT_TRUE(status);
  2319. checkResult(status, 0);
  2320. CfgMgr::instance().clear();
  2321. // Stage 2. Configure base option and a subnet. Please note that
  2322. // the configuration from the stage 2 is repeated because BIND
  2323. // configuration manager sends whole configuration for the lists
  2324. // where at least one element is being modified or added.
  2325. config = "{ " + genIfaceConfig() + ","
  2326. "\"preferred-lifetime\": 3000,"
  2327. "\"valid-lifetime\": 4000,"
  2328. "\"rebind-timer\": 2000,"
  2329. "\"renew-timer\": 1000,"
  2330. "\"option-data\": [ {"
  2331. " \"name\": \"base-option\","
  2332. " \"data\": \"11\""
  2333. " },"
  2334. " {"
  2335. " \"name\": \"foo\","
  2336. " \"space\": \"isc\","
  2337. " \"data\": \"1234\""
  2338. " },"
  2339. " {"
  2340. " \"name\": \"foo2\","
  2341. " \"space\": \"isc\","
  2342. " \"data\": \"192.168.2.1\""
  2343. " } ],"
  2344. "\"option-def\": [ {"
  2345. " \"name\": \"base-option\","
  2346. " \"code\": 100,"
  2347. " \"type\": \"uint8\","
  2348. " \"space\": \"dhcp6\","
  2349. " \"encapsulate\": \"isc\""
  2350. "},"
  2351. "{"
  2352. " \"name\": \"foo\","
  2353. " \"code\": 110,"
  2354. " \"type\": \"uint32\","
  2355. " \"space\": \"isc\""
  2356. " },"
  2357. " {"
  2358. " \"name\": \"foo2\","
  2359. " \"code\": 111,"
  2360. " \"type\": \"ipv4-address\","
  2361. " \"space\": \"isc\""
  2362. " } ],"
  2363. "\"subnet6\": [ { "
  2364. " \"pools\": [ { \"pool\": \"2001:db8:1::/80\" } ],"
  2365. " \"subnet\": \"2001:db8:1::/64\""
  2366. " } ]"
  2367. "}";
  2368. ASSERT_NO_THROW(json = parseDHCP6(config));
  2369. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  2370. ASSERT_TRUE(status);
  2371. checkResult(status, 0);
  2372. // We should have one option available.
  2373. OptionContainerPtr options =
  2374. CfgMgr::instance().getStagingCfg()->getCfgOption()->getAll(DHCP6_OPTION_SPACE);
  2375. ASSERT_TRUE(options);
  2376. ASSERT_EQ(1, options->size());
  2377. // Get the option.
  2378. OptionDescriptor desc =
  2379. CfgMgr::instance().getStagingCfg()->getCfgOption()->get(DHCP6_OPTION_SPACE, 100);
  2380. EXPECT_TRUE(desc.option_);
  2381. EXPECT_EQ(100, desc.option_->getType());
  2382. // This opton should comprise two sub-options.
  2383. // Onf of them is 'foo' with code 110.
  2384. OptionPtr option_foo = desc.option_->getOption(110);
  2385. ASSERT_TRUE(option_foo);
  2386. EXPECT_EQ(110, option_foo->getType());
  2387. // ...another one 'foo2' with code 111.
  2388. OptionPtr option_foo2 = desc.option_->getOption(111);
  2389. ASSERT_TRUE(option_foo2);
  2390. EXPECT_EQ(111, option_foo2->getType());
  2391. }
  2392. // Goal of this test is to verify options configuration
  2393. // for multiple subnets.
  2394. TEST_F(Dhcp6ParserTest, optionDataInMultipleSubnets) {
  2395. ConstElementPtr x;
  2396. string config = "{ " + genIfaceConfig() + ","
  2397. "\"preferred-lifetime\": 3000,"
  2398. "\"rebind-timer\": 2000, "
  2399. "\"renew-timer\": 1000, "
  2400. "\"subnet6\": [ { "
  2401. " \"pools\": [ { \"pool\": \"2001:db8:1::/80\" } ],"
  2402. " \"subnet\": \"2001:db8:1::/64\", "
  2403. " \"option-data\": [ {"
  2404. " \"name\": \"subscriber-id\","
  2405. " \"data\": \"0102030405060708090A\","
  2406. " \"csv-format\": false"
  2407. " } ]"
  2408. " },"
  2409. " {"
  2410. " \"pools\": [ { \"pool\": \"2001:db8:2::/80\" } ],"
  2411. " \"subnet\": \"2001:db8:2::/64\", "
  2412. " \"option-data\": [ {"
  2413. " \"name\": \"user-class\","
  2414. " \"data\": \"FFFEFDFCFB\","
  2415. " \"csv-format\": false"
  2416. " } ]"
  2417. " } ],"
  2418. "\"valid-lifetime\": 4000 }";
  2419. ConstElementPtr json;
  2420. ASSERT_NO_THROW(json = parseDHCP6(config));
  2421. EXPECT_NO_THROW(x = configureDhcp6Server(srv_, json));
  2422. checkResult(x, 0);
  2423. Subnet6Ptr subnet1 = CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->
  2424. selectSubnet(IOAddress("2001:db8:1::5"), classify_);
  2425. ASSERT_TRUE(subnet1);
  2426. OptionContainerPtr options1 = subnet1->getCfgOption()->getAll(DHCP6_OPTION_SPACE);
  2427. ASSERT_EQ(1, options1->size());
  2428. // Get the search index. Index #1 is to search using option code.
  2429. const OptionContainerTypeIndex& idx1 = options1->get<1>();
  2430. // Get the options for specified index. Expecting one option to be
  2431. // returned but in theory we may have multiple options with the same
  2432. // code so we get the range.
  2433. std::pair<OptionContainerTypeIndex::const_iterator,
  2434. OptionContainerTypeIndex::const_iterator> range1 =
  2435. idx1.equal_range(D6O_SUBSCRIBER_ID);
  2436. // Expect single option with the code equal to 38.
  2437. ASSERT_EQ(1, std::distance(range1.first, range1.second));
  2438. const uint8_t subid_expected[] = {
  2439. 0x01, 0x02, 0x03, 0x04, 0x05,
  2440. 0x06, 0x07, 0x08, 0x09, 0x0A
  2441. };
  2442. // Check if option is valid in terms of code and carried data.
  2443. testOption(*range1.first, D6O_SUBSCRIBER_ID, subid_expected,
  2444. sizeof(subid_expected));
  2445. // Test another subnet in the same way.
  2446. Subnet6Ptr subnet2 = CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->
  2447. selectSubnet(IOAddress("2001:db8:2::4"), classify_);
  2448. ASSERT_TRUE(subnet2);
  2449. OptionContainerPtr options2 = subnet2->getCfgOption()->getAll(DHCP6_OPTION_SPACE);
  2450. ASSERT_EQ(1, options2->size());
  2451. const OptionContainerTypeIndex& idx2 = options2->get<1>();
  2452. std::pair<OptionContainerTypeIndex::const_iterator,
  2453. OptionContainerTypeIndex::const_iterator> range2 =
  2454. idx2.equal_range(D6O_USER_CLASS);
  2455. ASSERT_EQ(1, std::distance(range2.first, range2.second));
  2456. const uint8_t user_class_expected[] = {
  2457. 0xFF, 0xFE, 0xFD, 0xFC, 0xFB
  2458. };
  2459. testOption(*range2.first, D6O_USER_CLASS, user_class_expected,
  2460. sizeof(user_class_expected));
  2461. }
  2462. // This test verifies that it is possible to specify options on
  2463. // pool levels.
  2464. TEST_F(Dhcp6ParserTest, optionDataMultiplePools) {
  2465. ConstElementPtr x;
  2466. string config = "{ " + genIfaceConfig() + ","
  2467. "\"preferred-lifetime\": 3000,"
  2468. "\"rebind-timer\": 2000, "
  2469. "\"renew-timer\": 1000, "
  2470. "\"subnet6\": [ { "
  2471. " \"pools\": [ { "
  2472. " \"pool\": \"2001:db8:1::10 - 2001:db8:1::100\","
  2473. " \"option-data\": [ {"
  2474. " \"name\": \"subscriber-id\","
  2475. " \"data\": \"0102030405060708090A\","
  2476. " \"csv-format\": false"
  2477. " } ]"
  2478. " },"
  2479. " {"
  2480. " \"pool\": \"2001:db8:1::300 - 2001:db8:1::400\","
  2481. " \"option-data\": [ {"
  2482. " \"name\": \"user-class\","
  2483. " \"data\": \"FFFEFDFCFB\","
  2484. " \"csv-format\": false"
  2485. " } ]"
  2486. " } ],"
  2487. " \"pd-pools\": [ { "
  2488. " \"prefix\": \"3000::\","
  2489. " \"prefix-len\": 48,"
  2490. " \"delegated-len\": 64,"
  2491. " \"option-data\": [ {"
  2492. " \"name\": \"subscriber-id\","
  2493. " \"data\": \"112233445566\","
  2494. " \"csv-format\": false"
  2495. " } ]"
  2496. " },"
  2497. " {"
  2498. " \"prefix\": \"3001::\","
  2499. " \"prefix-len\": 48,"
  2500. " \"delegated-len\": 64,"
  2501. " \"option-data\": [ {"
  2502. " \"name\": \"user-class\","
  2503. " \"data\": \"aabbccddee\","
  2504. " \"csv-format\": false"
  2505. " } ]"
  2506. " } ],"
  2507. " \"subnet\": \"2001:db8:1::/64\""
  2508. " } ],"
  2509. "\"valid-lifetime\": 4000 }";
  2510. ConstElementPtr json;
  2511. ASSERT_NO_THROW(json = parseDHCP6(config));
  2512. EXPECT_NO_THROW(x = configureDhcp6Server(srv_, json));
  2513. checkResult(x, 0);
  2514. Subnet6Ptr subnet = CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->
  2515. selectSubnet(IOAddress("2001:db8:1::5"), classify_);
  2516. ASSERT_TRUE(subnet);
  2517. PoolPtr pool = subnet->getPool(Lease::TYPE_PD, IOAddress("3000::"), false);
  2518. ASSERT_TRUE(pool);
  2519. Pool6Ptr pool6 = boost::dynamic_pointer_cast<Pool6>(pool);
  2520. ASSERT_TRUE(pool6);
  2521. OptionContainerPtr options1 = pool6->getCfgOption()->getAll("dhcp6");
  2522. ASSERT_EQ(1, options1->size());
  2523. // Get the search index. Index #1 is to search using option code.
  2524. const OptionContainerTypeIndex& idx1 = options1->get<1>();
  2525. // Get the options for specified index. Expecting one option to be
  2526. // returned but in theory we may have multiple options with the same
  2527. // code so we get the range.
  2528. std::pair<OptionContainerTypeIndex::const_iterator,
  2529. OptionContainerTypeIndex::const_iterator> range1 =
  2530. idx1.equal_range(D6O_SUBSCRIBER_ID);
  2531. // Expect a single Subscriber ID option instance.
  2532. ASSERT_EQ(1, std::distance(range1.first, range1.second));
  2533. const uint8_t subscriber_id_expected[] = {
  2534. 0x11, 0x22, 0x33, 0x44, 0x55, 0x66
  2535. };
  2536. // Check if option is valid in terms of code and carried data.
  2537. testOption(*range1.first, D6O_SUBSCRIBER_ID, subscriber_id_expected,
  2538. sizeof(subscriber_id_expected));
  2539. // Test another pool in the same way.
  2540. pool = subnet->getPool(Lease::TYPE_PD, IOAddress("3001::"), false);
  2541. ASSERT_TRUE(pool);
  2542. pool6 = boost::dynamic_pointer_cast<Pool6>(pool);
  2543. ASSERT_TRUE(pool6);
  2544. OptionContainerPtr options2 = pool6->getCfgOption()->getAll("dhcp6");
  2545. ASSERT_EQ(1, options2->size());
  2546. const OptionContainerTypeIndex& idx2 = options2->get<1>();
  2547. std::pair<OptionContainerTypeIndex::const_iterator,
  2548. OptionContainerTypeIndex::const_iterator> range2 =
  2549. idx2.equal_range(D6O_USER_CLASS);
  2550. ASSERT_EQ(1, std::distance(range2.first, range2.second));
  2551. const uint8_t user_class_expected[] = {
  2552. 0xAA, 0xBB, 0xCC, 0xDD, 0xEE
  2553. };
  2554. testOption(*range2.first, D6O_USER_CLASS, user_class_expected,
  2555. sizeof(user_class_expected));
  2556. // Test options in NA pools.
  2557. pool = subnet->getPool(Lease::TYPE_NA, IOAddress("2001:db8:1::10"));
  2558. ASSERT_TRUE(pool);
  2559. pool6 = boost::dynamic_pointer_cast<Pool6>(pool);
  2560. ASSERT_TRUE(pool6);
  2561. OptionContainerPtr options3 = pool6->getCfgOption()->getAll("dhcp6");
  2562. ASSERT_EQ(1, options3->size());
  2563. const OptionContainerTypeIndex& idx3 = options3->get<1>();
  2564. std::pair<OptionContainerTypeIndex::const_iterator,
  2565. OptionContainerTypeIndex::const_iterator> range3 =
  2566. idx3.equal_range(D6O_SUBSCRIBER_ID);
  2567. ASSERT_EQ(1, std::distance(range3.first, range3.second));
  2568. const uint8_t subscriber_id_expected2[] = {
  2569. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A
  2570. };
  2571. testOption(*range3.first, D6O_SUBSCRIBER_ID, subscriber_id_expected2,
  2572. sizeof(subscriber_id_expected2));
  2573. pool = subnet->getPool(Lease::TYPE_NA, IOAddress("2001:db8:1::300"));
  2574. ASSERT_TRUE(pool);
  2575. pool6 = boost::dynamic_pointer_cast<Pool6>(pool);
  2576. ASSERT_TRUE(pool6);
  2577. OptionContainerPtr options4 = pool6->getCfgOption()->getAll("dhcp6");
  2578. ASSERT_EQ(1, options4->size());
  2579. const OptionContainerTypeIndex& idx4 = options4->get<1>();
  2580. std::pair<OptionContainerTypeIndex::const_iterator,
  2581. OptionContainerTypeIndex::const_iterator> range4 =
  2582. idx4.equal_range(D6O_USER_CLASS);
  2583. ASSERT_EQ(1, std::distance(range4.first, range4.second));
  2584. const uint8_t user_class_expected2[] = {
  2585. 0xFF, 0xFE, 0xFD, 0xFC, 0xFB
  2586. };
  2587. testOption(*range4.first, D6O_USER_CLASS, user_class_expected2,
  2588. sizeof(user_class_expected2));
  2589. }
  2590. // The goal of this test is to check that the option carrying a boolean
  2591. // value can be configured using one of the values: "true", "false", "0"
  2592. // or "1".
  2593. TEST_F(Dhcp6ParserTest, optionDataBoolean) {
  2594. // Create configuration. Use standard option 1000.
  2595. std::map<std::string, std::string> params;
  2596. params["name"] = "bool-option";
  2597. params["space"] = DHCP6_OPTION_SPACE;
  2598. params["code"] = "1000";
  2599. params["data"] = "true";
  2600. params["csv-format"] = "true";
  2601. std::string config = createConfigWithOption(params);
  2602. ASSERT_TRUE(executeConfiguration(config, "parse configuration with a"
  2603. " boolean value"));
  2604. // The subnet should now hold one option with the code 1000.
  2605. OptionDescriptor desc =
  2606. getOptionFromSubnet(IOAddress("2001:db8:1::5"), 1000);
  2607. ASSERT_TRUE(desc.option_);
  2608. // This option should be set to "true", represented as 0x1 in the option
  2609. // buffer.
  2610. uint8_t expected_option_data[] = {
  2611. 0x1
  2612. };
  2613. testConfiguration(params, 1000, expected_option_data,
  2614. sizeof(expected_option_data));
  2615. // Configure the option with the "1" value. This should have the same
  2616. // effect as if "true" was specified.
  2617. params["data"] = "1";
  2618. testConfiguration(params, 1000, expected_option_data,
  2619. sizeof(expected_option_data));
  2620. // The value of "1" with a few leading zeros should work too.
  2621. params["data"] = "00001";
  2622. testConfiguration(params, 1000, expected_option_data,
  2623. sizeof(expected_option_data));
  2624. // Configure the option with the "false" value.
  2625. params["data"] = "false";
  2626. // The option buffer should now hold the value of 0.
  2627. expected_option_data[0] = 0;
  2628. testConfiguration(params, 1000, expected_option_data,
  2629. sizeof(expected_option_data));
  2630. // Specifying "0" should have the same effect as "false".
  2631. params["data"] = "0";
  2632. testConfiguration(params, 1000, expected_option_data,
  2633. sizeof(expected_option_data));
  2634. // The same effect should be for multiple 0 chars.
  2635. params["data"] = "00000";
  2636. testConfiguration(params, 1000, expected_option_data,
  2637. sizeof(expected_option_data));
  2638. // Bogus values should not be accepted.
  2639. params["data"] = "bogus";
  2640. testInvalidOptionParam(params);
  2641. params["data"] = "2";
  2642. testInvalidOptionParam(params);
  2643. // Now let's test that it is possible to use binary format.
  2644. params["data"] = "0";
  2645. params["csv-format"] = "false";
  2646. testConfiguration(params, 1000, expected_option_data,
  2647. sizeof(expected_option_data));
  2648. // The binary 1 should work as well.
  2649. params["data"] = "1";
  2650. expected_option_data[0] = 1;
  2651. testConfiguration(params, 1000, expected_option_data,
  2652. sizeof(expected_option_data));
  2653. // As well as an even number of digits.
  2654. params["data"] = "01";
  2655. testConfiguration(params, 1000, expected_option_data,
  2656. sizeof(expected_option_data));
  2657. }
  2658. // Verify that empty option name is rejected in the configuration.
  2659. TEST_F(Dhcp6ParserTest, optionNameEmpty) {
  2660. // Empty option names not allowed.
  2661. testInvalidOptionParam("", "name");
  2662. }
  2663. // Verify that empty option name with spaces is rejected
  2664. // in the configuration.
  2665. TEST_F(Dhcp6ParserTest, optionNameSpaces) {
  2666. // Spaces in option names not allowed.
  2667. testInvalidOptionParam("option foo", "name");
  2668. }
  2669. // Verify that negative option code is rejected in the configuration.
  2670. TEST_F(Dhcp6ParserTest, optionCodeNegative) {
  2671. // Check negative option code -4. This should fail too.
  2672. testInvalidOptionParam("-4", "code");
  2673. }
  2674. // Verify that out of bounds option code is rejected in the configuration.
  2675. TEST_F(Dhcp6ParserTest, optionCodeNonUint16) {
  2676. // The valid option codes are uint16_t values so passing
  2677. // uint16_t maximum value incremented by 1 should result
  2678. // in failure.
  2679. testInvalidOptionParam("65536", "code");
  2680. }
  2681. // Verify that out of bounds option code is rejected in the configuration.
  2682. TEST_F(Dhcp6ParserTest, optionCodeHighNonUint16) {
  2683. // Another check for uint16_t overflow but this time
  2684. // let's pass even greater option code value.
  2685. testInvalidOptionParam("70000", "code");
  2686. }
  2687. // Verify that zero option code is rejected in the configuration.
  2688. TEST_F(Dhcp6ParserTest, optionCodeZero) {
  2689. // Option code 0 is reserved and should not be accepted
  2690. // by configuration parser.
  2691. testInvalidOptionParam("0", "code");
  2692. }
  2693. // Verify that option data which contains non hexadecimal characters
  2694. // is rejected by the configuration.
  2695. TEST_F(Dhcp6ParserTest, optionDataInvalidChar) {
  2696. // Option code 0 is reserved and should not be accepted
  2697. // by configuration parser.
  2698. testInvalidOptionParam("01020R", "data");
  2699. }
  2700. // Verify that option data containing '0x' prefix is rejected
  2701. // by the configuration.
  2702. TEST_F(Dhcp6ParserTest, optionDataUnexpectedPrefix) {
  2703. // Option code 0 is reserved and should not be accepted
  2704. // by configuration parser.
  2705. testInvalidOptionParam("0x0102", "data");
  2706. }
  2707. // Verify that either lower or upper case characters are allowed
  2708. // to specify the option data.
  2709. TEST_F(Dhcp6ParserTest, optionDataLowerCase) {
  2710. ConstElementPtr x;
  2711. std::string config = createConfigWithOption("0a0b0C0D", "data");
  2712. ConstElementPtr json = parseDHCP6(config);
  2713. EXPECT_NO_THROW(x = configureDhcp6Server(srv_, json));
  2714. checkResult(x, 0);
  2715. Subnet6Ptr subnet = CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->
  2716. selectSubnet(IOAddress("2001:db8:1::5"), classify_);
  2717. ASSERT_TRUE(subnet);
  2718. OptionContainerPtr options = subnet->getCfgOption()->getAll(DHCP6_OPTION_SPACE);
  2719. ASSERT_EQ(1, options->size());
  2720. // Get the search index. Index #1 is to search using option code.
  2721. const OptionContainerTypeIndex& idx = options->get<1>();
  2722. // Get the options for specified index. Expecting one option to be
  2723. // returned but in theory we may have multiple options with the same
  2724. // code so we get the range.
  2725. std::pair<OptionContainerTypeIndex::const_iterator,
  2726. OptionContainerTypeIndex::const_iterator> range =
  2727. idx.equal_range(D6O_SUBSCRIBER_ID);
  2728. // Expect single option with the code equal to 38.
  2729. ASSERT_EQ(1, std::distance(range.first, range.second));
  2730. const uint8_t subid_expected[] = {
  2731. 0x0A, 0x0B, 0x0C, 0x0D
  2732. };
  2733. // Check if option is valid in terms of code and carried data.
  2734. testOption(*range.first, D6O_SUBSCRIBER_ID, subid_expected,
  2735. sizeof(subid_expected));
  2736. }
  2737. // Verify that specific option object is returned for standard
  2738. // option which has dedicated option class derived from Option.
  2739. TEST_F(Dhcp6ParserTest, stdOptionData) {
  2740. ConstElementPtr x;
  2741. std::map<std::string, std::string> params;
  2742. params["name"] = "ia-na";
  2743. params["space"] = DHCP6_OPTION_SPACE;
  2744. // Option code 3 means OPTION_IA_NA.
  2745. params["code"] = "3";
  2746. params["data"] = "12345, 6789, 1516";
  2747. params["csv-format"] = "true";
  2748. std::string config = createConfigWithOption(params);
  2749. ConstElementPtr json = parseDHCP6(config);
  2750. EXPECT_NO_THROW(x = configureDhcp6Server(srv_, json));
  2751. checkResult(x, 0);
  2752. Subnet6Ptr subnet = CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->
  2753. selectSubnet(IOAddress("2001:db8:1::5"), classify_);
  2754. ASSERT_TRUE(subnet);
  2755. OptionContainerPtr options = subnet->getCfgOption()->getAll(DHCP6_OPTION_SPACE);
  2756. ASSERT_EQ(1, options->size());
  2757. // Get the search index. Index #1 is to search using option code.
  2758. const OptionContainerTypeIndex& idx = options->get<1>();
  2759. // Get the options for specified index. Expecting one option to be
  2760. // returned but in theory we may have multiple options with the same
  2761. // code so we get the range.
  2762. std::pair<OptionContainerTypeIndex::const_iterator,
  2763. OptionContainerTypeIndex::const_iterator> range =
  2764. idx.equal_range(D6O_IA_NA);
  2765. // Expect single option with the code equal to IA_NA option code.
  2766. ASSERT_EQ(1, std::distance(range.first, range.second));
  2767. // The actual pointer to the option is held in the option field
  2768. // in the structure returned.
  2769. OptionPtr option = range.first->option_;
  2770. ASSERT_TRUE(option);
  2771. // Option object returned for here is expected to be Option6IA
  2772. // which is derived from Option. This class is dedicated to
  2773. // represent standard option IA_NA.
  2774. boost::shared_ptr<Option6IA> optionIA =
  2775. boost::dynamic_pointer_cast<Option6IA>(option);
  2776. // If cast is unsuccessful than option returned was of a
  2777. // different type than Option6IA. This is wrong.
  2778. ASSERT_TRUE(optionIA);
  2779. // If cast was successful we may use accessors exposed by
  2780. // Option6IA to validate that the content of this option
  2781. // has been set correctly.
  2782. EXPECT_EQ(12345, optionIA->getIAID());
  2783. EXPECT_EQ(6789, optionIA->getT1());
  2784. EXPECT_EQ(1516, optionIA->getT2());
  2785. }
  2786. // This test checks if vendor options can be specified in the config file
  2787. // (in hex format), and later retrieved from configured subnet
  2788. TEST_F(Dhcp6ParserTest, vendorOptionsHex) {
  2789. // This configuration string is to configure two options
  2790. // sharing the code 1 and belonging to the different vendor spaces.
  2791. // (different vendor-id values).
  2792. string config = "{ " + genIfaceConfig() + ","
  2793. "\"preferred-lifetime\": 3000,"
  2794. "\"valid-lifetime\": 4000,"
  2795. "\"rebind-timer\": 2000,"
  2796. "\"renew-timer\": 1000,"
  2797. "\"option-data\": [ {"
  2798. " \"name\": \"option-one\","
  2799. " \"space\": \"vendor-4491\","
  2800. " \"code\": 100,"
  2801. " \"data\": \"ABCDEF0105\","
  2802. " \"csv-format\": false"
  2803. " },"
  2804. " {"
  2805. " \"name\": \"option-two\","
  2806. " \"space\": \"vendor-1234\","
  2807. " \"code\": 100,"
  2808. " \"data\": \"1234\","
  2809. " \"csv-format\": false"
  2810. " } ],"
  2811. "\"subnet6\": [ { "
  2812. " \"pools\": [ { \"pool\": \"2001:db8:1::/80\" } ],"
  2813. " \"subnet\": \"2001:db8:1::/64\""
  2814. " } ]"
  2815. "}";
  2816. ConstElementPtr json;
  2817. ASSERT_NO_THROW(json = parseDHCP6(config));
  2818. ConstElementPtr status;
  2819. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  2820. ASSERT_TRUE(status);
  2821. checkResult(status, 0);
  2822. // Options should be now available
  2823. // Try to get the option from the vendor space 4491
  2824. OptionDescriptor desc1 =
  2825. CfgMgr::instance().getStagingCfg()->getCfgOption()->get(4491, 100);
  2826. ASSERT_TRUE(desc1.option_);
  2827. EXPECT_EQ(100, desc1.option_->getType());
  2828. // Try to get the option from the vendor space 1234
  2829. OptionDescriptor desc2 =
  2830. CfgMgr::instance().getStagingCfg()->getCfgOption()->get(1234, 100);
  2831. ASSERT_TRUE(desc2.option_);
  2832. EXPECT_EQ(100, desc1.option_->getType());
  2833. // Try to get the non-existing option from the non-existing
  2834. // option space and expect that option is not returned.
  2835. OptionDescriptor desc3 =
  2836. CfgMgr::instance().getStagingCfg()->getCfgOption()->get(5678, 38);
  2837. ASSERT_FALSE(desc3.option_);
  2838. }
  2839. // This test checks if vendor options can be specified in the config file,
  2840. // (in csv format), and later retrieved from configured subnet
  2841. TEST_F(Dhcp6ParserTest, vendorOptionsCsv) {
  2842. // This configuration string is to configure two options
  2843. // sharing the code 1 and belonging to the different vendor spaces.
  2844. // (different vendor-id values).
  2845. string config = "{ " + genIfaceConfig() + ","
  2846. "\"preferred-lifetime\": 3000,"
  2847. "\"valid-lifetime\": 4000,"
  2848. "\"rebind-timer\": 2000,"
  2849. "\"renew-timer\": 1000,"
  2850. "\"option-data\": [ {"
  2851. " \"name\": \"foo\","
  2852. " \"space\": \"vendor-4491\","
  2853. " \"code\": 100,"
  2854. " \"data\": \"this is a string vendor-opt\""
  2855. " } ],"
  2856. "\"option-def\": [ {"
  2857. " \"name\": \"foo\","
  2858. " \"code\": 100,"
  2859. " \"type\": \"string\","
  2860. " \"space\": \"vendor-4491\""
  2861. " } ],"
  2862. "\"subnet6\": [ { "
  2863. " \"pools\": [ { \"pool\": \"2001:db8:1::/80\" } ],"
  2864. " \"subnet\": \"2001:db8:1::/64\""
  2865. " } ]"
  2866. "}";
  2867. ConstElementPtr status;
  2868. ConstElementPtr json;
  2869. ASSERT_NO_THROW(json = parseDHCP6(config));
  2870. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  2871. ASSERT_TRUE(status);
  2872. checkResult(status, 0);
  2873. // Options should be now available.
  2874. // Try to get the option from the vendor space 4491
  2875. OptionDescriptor desc1 =
  2876. CfgMgr::instance().getStagingCfg()->getCfgOption()->get(4491, 100);
  2877. ASSERT_TRUE(desc1.option_);
  2878. EXPECT_EQ(100, desc1.option_->getType());
  2879. // Try to get the non-existing option from the non-existing
  2880. // option space and expect that option is not returned.
  2881. OptionDescriptor desc2 =
  2882. CfgMgr::instance().getStagingCfg()->getCfgOption()->get(5678, 100);
  2883. ASSERT_FALSE(desc2.option_);
  2884. }
  2885. /// @todo add tests similar to vendorOptionsCsv and vendorOptionsHex, but for
  2886. /// vendor options defined in a subnet.
  2887. // The goal of this test is to verify that the standard option can
  2888. // be configured to encapsulate multiple other options.
  2889. /// @todo This test is currently disabled because it relies on the option
  2890. /// 17 which is treated differently than all other options. There are no
  2891. /// other standard options used by Kea which would encapsulate other
  2892. /// options and for which values could be configured here.
  2893. TEST_F(Dhcp6ParserTest, DISABLED_stdOptionDataEncapsulate) {
  2894. // The configuration is two stage process in this test.
  2895. // In the first stage we create definitions of suboptions
  2896. // that we will add to the base option.
  2897. // Let's create some dummy options: foo and foo2.
  2898. string config = "{ " + genIfaceConfig() + ","
  2899. "\"preferred-lifetime\": 3000,"
  2900. "\"valid-lifetime\": 4000,"
  2901. "\"rebind-timer\": 2000,"
  2902. "\"renew-timer\": 1000,"
  2903. "\"option-data\": [ {"
  2904. " \"name\": \"foo\","
  2905. " \"space\": \"vendor-opts-space\","
  2906. " \"data\": \"1234\""
  2907. " },"
  2908. " {"
  2909. " \"name\": \"foo2\","
  2910. " \"space\": \"vendor-opts-space\","
  2911. " \"data\": \"192.168.2.1\""
  2912. " } ],"
  2913. "\"option-def\": [ {"
  2914. " \"name\": \"foo\","
  2915. " \"code\": 110,"
  2916. " \"type\": \"uint32\","
  2917. " \"space\": \"vendor-opts-space\""
  2918. " },"
  2919. " {"
  2920. " \"name\": \"foo2\","
  2921. " \"code\": 111,"
  2922. " \"type\": \"ipv4-address\","
  2923. " \"space\": \"vendor-opts-space\""
  2924. " } ]"
  2925. "}";
  2926. ConstElementPtr json;
  2927. ASSERT_NO_THROW(json = parseDHCP6(config));
  2928. ConstElementPtr status;
  2929. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  2930. ASSERT_TRUE(status);
  2931. checkResult(status, 0);
  2932. CfgMgr::instance().clear();
  2933. // Once the definitions have been added we can configure the
  2934. // standard option #17. This option comprises an enterprise
  2935. // number and sub options. By convention (introduced in
  2936. // std_option_defs.h) option named 'vendor-opts'
  2937. // encapsulates the option space named 'vendor-opts-space'.
  2938. // We add our dummy options to this option space and thus
  2939. // they should be included as sub-options in the 'vendor-opts'
  2940. // option.
  2941. config = "{ " + genIfaceConfig() + ","
  2942. "\"rebind-timer\": 2000,"
  2943. "\"renew-timer\": 1000,"
  2944. "\"option-data\": [ {"
  2945. " \"name\": \"vendor-opts\","
  2946. " \"data\": \"1234\""
  2947. " },"
  2948. " {"
  2949. " \"name\": \"foo\","
  2950. " \"space\": \"vendor-opts-space\","
  2951. " \"data\": \"1234\""
  2952. " },"
  2953. " {"
  2954. " \"name\": \"foo2\","
  2955. " \"space\": \"vendor-opts-space\","
  2956. " \"data\": \"192.168.2.1\""
  2957. " } ],"
  2958. "\"option-def\": [ {"
  2959. " \"name\": \"foo\","
  2960. " \"code\": 110,"
  2961. " \"type\": \"uint32\","
  2962. " \"space\": \"vendor-opts-space\""
  2963. " },"
  2964. " {"
  2965. " \"name\": \"foo2\","
  2966. " \"code\": 111,"
  2967. " \"type\": \"ipv4-address\","
  2968. " \"space\": \"vendor-opts-space\""
  2969. " } ],"
  2970. "\"subnet6\": [ { "
  2971. " \"pools\": [ { \"pool\": \"2001:db8:1::/80\" } ],"
  2972. " \"subnet\": \"2001:db8:1::/64\""
  2973. " } ]"
  2974. "}";
  2975. ASSERT_NO_THROW(json = parseDHCP6(config));
  2976. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  2977. ASSERT_TRUE(status);
  2978. checkResult(status, 0);
  2979. // Get the subnet.
  2980. Subnet6Ptr subnet = CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->
  2981. selectSubnet(IOAddress("2001:db8:1::5"), classify_);
  2982. ASSERT_TRUE(subnet);
  2983. // We should have one option available.
  2984. OptionContainerPtr options = subnet->getCfgOption()->getAll(DHCP6_OPTION_SPACE);
  2985. ASSERT_TRUE(options);
  2986. ASSERT_EQ(1, options->size());
  2987. // Get the option.
  2988. OptionDescriptor desc = subnet->getCfgOption()->get(DHCP6_OPTION_SPACE, D6O_VENDOR_OPTS);
  2989. EXPECT_TRUE(desc.option_);
  2990. EXPECT_EQ(D6O_VENDOR_OPTS, desc.option_->getType());
  2991. // Option with the code 110 should be added as a sub-option.
  2992. OptionPtr option_foo = desc.option_->getOption(110);
  2993. ASSERT_TRUE(option_foo);
  2994. EXPECT_EQ(110, option_foo->getType());
  2995. // This option comprises a single uint32_t value thus it is
  2996. // represented by OptionInt<uint32_t> class. Let's get the
  2997. // object of this type.
  2998. boost::shared_ptr<OptionInt<uint32_t> > option_foo_uint32 =
  2999. boost::dynamic_pointer_cast<OptionInt<uint32_t> >(option_foo);
  3000. ASSERT_TRUE(option_foo_uint32);
  3001. // Validate the value according to the configuration.
  3002. EXPECT_EQ(1234, option_foo_uint32->getValue());
  3003. // Option with the code 111 should be added as a sub-option.
  3004. OptionPtr option_foo2 = desc.option_->getOption(111);
  3005. ASSERT_TRUE(option_foo2);
  3006. EXPECT_EQ(111, option_foo2->getType());
  3007. // This option comprises the IPV4 address. Such option is
  3008. // represented by OptionCustom object.
  3009. OptionCustomPtr option_foo2_v4 =
  3010. boost::dynamic_pointer_cast<OptionCustom>(option_foo2);
  3011. ASSERT_TRUE(option_foo2_v4);
  3012. // Get the IP address carried by this option and validate it.
  3013. EXPECT_EQ("192.168.2.1", option_foo2_v4->readAddress().toText());
  3014. // Option with the code 112 should not be added.
  3015. EXPECT_FALSE(desc.option_->getOption(112));
  3016. }
  3017. // Tests of the hooks libraries configuration. All tests have the pre-
  3018. // condition (checked in the test fixture's SetUp() method) that no hooks
  3019. // libraries are loaded at the start of the tests.
  3020. // Helper function to return a configuration containing an arbitrary number
  3021. // of hooks libraries.
  3022. std::string
  3023. buildHooksLibrariesConfig(const std::vector<std::string>& libraries) {
  3024. const string lbrace("{");
  3025. const string rbrace("}");
  3026. const string liblabel("\"library\": ");
  3027. const string quote("\"");
  3028. // Create the first part of the configuration string.
  3029. string config =
  3030. "{ \"interfaces-config\": { \"interfaces\": [] },"
  3031. "\"hooks-libraries\": [";
  3032. // Append the libraries (separated by commas if needed)
  3033. for (unsigned int i = 0; i < libraries.size(); ++i) {
  3034. if (i > 0) {
  3035. config += string(", ");
  3036. }
  3037. config += (lbrace + liblabel + quote + libraries[i] + quote + rbrace);
  3038. }
  3039. // Append the remainder of the configuration.
  3040. config += string(
  3041. "],"
  3042. "\"rebind-timer\": 2000,"
  3043. "\"renew-timer\": 1000,"
  3044. "\"option-data\": [ {"
  3045. " \"name\": \"foo\","
  3046. " \"space\": \"vendor-opts-space\","
  3047. " \"data\": \"1234\""
  3048. " },"
  3049. " {"
  3050. " \"name\": \"foo2\","
  3051. " \"space\": \"vendor-opts-space\","
  3052. " \"data\": \"192.168.2.1\""
  3053. " } ],"
  3054. "\"option-def\": [ {"
  3055. " \"name\": \"foo\","
  3056. " \"code\": 110,"
  3057. " \"type\": \"uint32\","
  3058. " \"space\": \"vendor-opts-space\""
  3059. " },"
  3060. " {"
  3061. " \"name\": \"foo2\","
  3062. " \"code\": 111,"
  3063. " \"type\": \"ipv4-address\","
  3064. " \"space\": \"vendor-opts-space\""
  3065. " } ]"
  3066. "}");
  3067. return (config);
  3068. }
  3069. // Convenience function for creating hooks library configuration with one or
  3070. // two character string constants.
  3071. std::string
  3072. buildHooksLibrariesConfig(const char* library1 = NULL,
  3073. const char* library2 = NULL) {
  3074. std::vector<std::string> libraries;
  3075. if (library1 != NULL) {
  3076. libraries.push_back(string(library1));
  3077. if (library2 != NULL) {
  3078. libraries.push_back(string(library2));
  3079. }
  3080. }
  3081. return (buildHooksLibrariesConfig(libraries));
  3082. }
  3083. // The goal of this test is to verify the configuration of hooks libraries if
  3084. // none are specified.
  3085. TEST_F(Dhcp6ParserTest, NoHooksLibraries) {
  3086. // Parse a configuration containing no names.
  3087. string config = buildHooksLibrariesConfig();
  3088. if (!executeConfiguration(config,
  3089. "set configuration with no hooks libraries")) {
  3090. FAIL() << "Unable to execute configuration";
  3091. } else {
  3092. // No libraries should be loaded at the end of the test.
  3093. std::vector<std::string> libraries = HooksManager::getLibraryNames();
  3094. EXPECT_TRUE(libraries.empty());
  3095. }
  3096. }
  3097. // Verify parsing fails with one library that will fail validation.
  3098. TEST_F(Dhcp6ParserTest, InvalidLibrary) {
  3099. // Parse a configuration containing a failing library.
  3100. string config = buildHooksLibrariesConfig(NOT_PRESENT_LIBRARY);
  3101. ConstElementPtr json;
  3102. ASSERT_NO_THROW(json = parseDHCP6(config));
  3103. ConstElementPtr status;
  3104. ASSERT_NO_THROW(status = configureDhcp6Server(srv_, json));
  3105. // The status object must not be NULL
  3106. ASSERT_TRUE(status);
  3107. // Returned value should not be 0
  3108. comment_ = parseAnswer(rcode_, status);
  3109. EXPECT_NE(0, rcode_);
  3110. }
  3111. // Verify the configuration of hooks libraries with two being specified.
  3112. TEST_F(Dhcp6ParserTest, LibrariesSpecified) {
  3113. // Marker files should not be present.
  3114. EXPECT_FALSE(checkMarkerFileExists(LOAD_MARKER_FILE));
  3115. EXPECT_FALSE(checkMarkerFileExists(UNLOAD_MARKER_FILE));
  3116. // Set up the configuration with two libraries and load them.
  3117. string config = buildHooksLibrariesConfig(CALLOUT_LIBRARY_1,
  3118. CALLOUT_LIBRARY_2);
  3119. ASSERT_TRUE(executeConfiguration(config,
  3120. "load two valid libraries"));
  3121. // Expect two libraries to be loaded in the correct order (load marker file
  3122. // is present, no unload marker file).
  3123. std::vector<std::string> libraries = HooksManager::getLibraryNames();
  3124. ASSERT_EQ(2, libraries.size());
  3125. EXPECT_TRUE(checkMarkerFile(LOAD_MARKER_FILE, "12"));
  3126. EXPECT_FALSE(checkMarkerFileExists(UNLOAD_MARKER_FILE));
  3127. CfgMgr::instance().commit();
  3128. // Unload the libraries. The load file should not have changed, but
  3129. // the unload one should indicate the unload() functions have been run.
  3130. config = buildHooksLibrariesConfig();
  3131. ASSERT_TRUE(executeConfiguration(config, "unloading libraries"));
  3132. EXPECT_TRUE(checkMarkerFile(LOAD_MARKER_FILE, "12"));
  3133. EXPECT_TRUE(checkMarkerFile(UNLOAD_MARKER_FILE, "21"));
  3134. // Expect the hooks system to say that none are loaded.
  3135. libraries = HooksManager::getLibraryNames();
  3136. EXPECT_TRUE(libraries.empty());
  3137. }
  3138. // This test verifies that it is possible to select subset of interfaces on
  3139. // which server should listen.
  3140. TEST_F(Dhcp6ParserTest, selectedInterfaces) {
  3141. IfaceMgrTestConfig test_config(true);
  3142. // Make sure there is no garbage interface configuration in the CfgMgr.
  3143. ASSERT_FALSE(test_config.socketOpen("eth0", AF_INET6));
  3144. ASSERT_FALSE(test_config.socketOpen("eth1", AF_INET6));
  3145. string config = "{ \"interfaces-config\": {"
  3146. " \"interfaces\": [ \"eth0\" ]"
  3147. "},"
  3148. "\"preferred-lifetime\": 3000,"
  3149. "\"rebind-timer\": 2000, "
  3150. "\"renew-timer\": 1000, "
  3151. "\"valid-lifetime\": 4000 }";
  3152. ConstElementPtr json;
  3153. ASSERT_NO_THROW(json = parseDHCP6(config));
  3154. ConstElementPtr status;
  3155. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  3156. // returned value must be 1 (values error)
  3157. // as the pool does not belong to that subnet
  3158. checkResult(status, 0);
  3159. CfgMgr::instance().getStagingCfg()->getCfgIface()->openSockets(AF_INET6, 10000);
  3160. // eth0 and eth1 were explicitly selected. eth2 was not.
  3161. EXPECT_TRUE(test_config.socketOpen("eth0", AF_INET6));
  3162. EXPECT_FALSE(test_config.socketOpen("eth1", AF_INET6));
  3163. }
  3164. // This test verifies that it is possible to configure the server to listen on
  3165. // all interfaces.
  3166. TEST_F(Dhcp6ParserTest, allInterfaces) {
  3167. IfaceMgrTestConfig test_config(true);
  3168. ASSERT_FALSE(test_config.socketOpen("eth0", AF_INET6));
  3169. ASSERT_FALSE(test_config.socketOpen("eth1", AF_INET6));
  3170. // This configuration specifies two interfaces on which server should listen
  3171. // but also includes '*'. This keyword switches server into the
  3172. // mode when it listens on all interfaces regardless of what interface names
  3173. // were specified in the "interfaces" parameter.
  3174. string config = "{ \"interfaces-config\": {"
  3175. " \"interfaces\": [ \"eth0\", \"eth1\", \"*\" ]"
  3176. "},"
  3177. "\"preferred-lifetime\": 3000,"
  3178. "\"rebind-timer\": 2000, "
  3179. "\"renew-timer\": 1000, "
  3180. "\"valid-lifetime\": 4000 }";
  3181. ConstElementPtr json;
  3182. ASSERT_NO_THROW(json = parseDHCP6(config));
  3183. ConstElementPtr status;
  3184. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  3185. checkResult(status, 0);
  3186. CfgMgr::instance().getStagingCfg()->getCfgIface()->openSockets(AF_INET6, 10000);
  3187. // All interfaces should be now active.
  3188. EXPECT_TRUE(test_config.socketOpen("eth0", AF_INET6));
  3189. EXPECT_TRUE(test_config.socketOpen("eth1", AF_INET6));
  3190. }
  3191. // This test checks if it is possible to specify relay information
  3192. TEST_F(Dhcp6ParserTest, subnetRelayInfo) {
  3193. // A config with relay information.
  3194. string config = "{ " + genIfaceConfig() + ","
  3195. "\"rebind-timer\": 2000, "
  3196. "\"renew-timer\": 1000, "
  3197. "\"subnet6\": [ { "
  3198. " \"pools\": [ { \"pool\": \"2001:db8:1::1 - 2001:db8:1::ffff\" } ],"
  3199. " \"relay\": { "
  3200. " \"ip-address\": \"2001:db8:1::abcd\""
  3201. " },"
  3202. " \"subnet\": \"2001:db8:1::/64\" } ],"
  3203. "\"preferred-lifetime\": 3000, "
  3204. "\"valid-lifetime\": 4000 }";
  3205. ConstElementPtr json;
  3206. ASSERT_NO_THROW(json = parseDHCP6(config));
  3207. ConstElementPtr status;
  3208. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  3209. // returned value should be 0 (configuration success)
  3210. checkResult(status, 0);
  3211. Subnet6Ptr subnet = CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->
  3212. selectSubnet(IOAddress("2001:db8:1::1"), classify_);
  3213. ASSERT_TRUE(subnet);
  3214. EXPECT_EQ("2001:db8:1::abcd", subnet->getRelayInfo().addr_.toText());
  3215. }
  3216. // Goal of this test is to verify that multiple subnets can be configured
  3217. // with defined client classes.
  3218. TEST_F(Dhcp6ParserTest, classifySubnets) {
  3219. ConstElementPtr x;
  3220. string config = "{ " + genIfaceConfig() + ","
  3221. "\"preferred-lifetime\": 3000,"
  3222. "\"rebind-timer\": 2000, "
  3223. "\"renew-timer\": 1000, "
  3224. "\"subnet6\": [ { "
  3225. " \"pools\": [ { \"pool\": \"2001:db8:1::/80\" } ],"
  3226. " \"subnet\": \"2001:db8:1::/64\", "
  3227. " \"client-class\": \"alpha\" "
  3228. " },"
  3229. " {"
  3230. " \"pools\": [ { \"pool\": \"2001:db8:2::/80\" } ],"
  3231. " \"subnet\": \"2001:db8:2::/64\", "
  3232. " \"client-class\": \"beta\" "
  3233. " },"
  3234. " {"
  3235. " \"pools\": [ { \"pool\": \"2001:db8:3::/80\" } ],"
  3236. " \"subnet\": \"2001:db8:3::/64\", "
  3237. " \"client-class\": \"gamma\" "
  3238. " },"
  3239. " {"
  3240. " \"pools\": [ { \"pool\": \"2001:db8:4::/80\" } ],"
  3241. " \"subnet\": \"2001:db8:4::/64\" "
  3242. " } ],"
  3243. "\"valid-lifetime\": 4000 }";
  3244. ConstElementPtr json;
  3245. ASSERT_NO_THROW(json = parseDHCP6(config));
  3246. EXPECT_NO_THROW(x = configureDhcp6Server(srv_, json));
  3247. checkResult(x, 0);
  3248. const Subnet6Collection* subnets =
  3249. CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->getAll();
  3250. ASSERT_TRUE(subnets);
  3251. ASSERT_EQ(4, subnets->size()); // We expect 4 subnets
  3252. // Let's check if client belonging to alpha class is supported in subnet[0]
  3253. // and not supported in any other subnet (except subnet[3], which allows
  3254. // everyone).
  3255. ClientClasses classes;
  3256. classes.insert("alpha");
  3257. EXPECT_TRUE (subnets->at(0)->clientSupported(classes));
  3258. EXPECT_FALSE(subnets->at(1)->clientSupported(classes));
  3259. EXPECT_FALSE(subnets->at(2)->clientSupported(classes));
  3260. EXPECT_TRUE (subnets->at(3)->clientSupported(classes));
  3261. // Let's check if client belonging to beta class is supported in subnet[1]
  3262. // and not supported in any other subnet (except subnet[3], which allows
  3263. // everyone).
  3264. classes.clear();
  3265. classes.insert("beta");
  3266. EXPECT_FALSE(subnets->at(0)->clientSupported(classes));
  3267. EXPECT_TRUE (subnets->at(1)->clientSupported(classes));
  3268. EXPECT_FALSE(subnets->at(2)->clientSupported(classes));
  3269. EXPECT_TRUE (subnets->at(3)->clientSupported(classes));
  3270. // Let's check if client belonging to gamma class is supported in subnet[2]
  3271. // and not supported in any other subnet (except subnet[3], which allows
  3272. // everyone).
  3273. classes.clear();
  3274. classes.insert("gamma");
  3275. EXPECT_FALSE(subnets->at(0)->clientSupported(classes));
  3276. EXPECT_FALSE(subnets->at(1)->clientSupported(classes));
  3277. EXPECT_TRUE (subnets->at(2)->clientSupported(classes));
  3278. EXPECT_TRUE (subnets->at(3)->clientSupported(classes));
  3279. // Let's check if client belonging to some other class (not mentioned in
  3280. // the config) is supported only in subnet[3], which allows everyone.
  3281. classes.clear();
  3282. classes.insert("delta");
  3283. EXPECT_FALSE(subnets->at(0)->clientSupported(classes));
  3284. EXPECT_FALSE(subnets->at(1)->clientSupported(classes));
  3285. EXPECT_FALSE(subnets->at(2)->clientSupported(classes));
  3286. EXPECT_TRUE (subnets->at(3)->clientSupported(classes));
  3287. // Finally, let's check class-less client. He should be allowed only in
  3288. // the last subnet, which does not have any class restrictions.
  3289. classes.clear();
  3290. EXPECT_FALSE(subnets->at(0)->clientSupported(classes));
  3291. EXPECT_FALSE(subnets->at(1)->clientSupported(classes));
  3292. EXPECT_FALSE(subnets->at(2)->clientSupported(classes));
  3293. EXPECT_TRUE (subnets->at(3)->clientSupported(classes));
  3294. }
  3295. // This test checks the ability of the server to parse a configuration
  3296. // containing a full, valid dhcp-ddns (D2ClientConfig) entry.
  3297. TEST_F(Dhcp6ParserTest, d2ClientConfig) {
  3298. // Verify that the D2 configuration can be fetched and is set to disabled.
  3299. D2ClientConfigPtr d2_client_config = CfgMgr::instance().getD2ClientConfig();
  3300. EXPECT_FALSE(d2_client_config->getEnableUpdates());
  3301. // Verify that the convenience method agrees.
  3302. ASSERT_FALSE(CfgMgr::instance().ddnsEnabled());
  3303. string config_str = "{ " + genIfaceConfig() + ","
  3304. "\"preferred-lifetime\": 3000,"
  3305. "\"valid-lifetime\": 4000,"
  3306. "\"rebind-timer\": 2000, "
  3307. "\"renew-timer\": 1000, "
  3308. "\"subnet6\": [ { "
  3309. " \"pools\": [ { \"pool\": \"2001:db8:1::/80\" } ],"
  3310. " \"subnet\": \"2001:db8:1::/64\" } ], "
  3311. " \"dhcp-ddns\" : {"
  3312. " \"enable-updates\" : true, "
  3313. " \"server-ip\" : \"3001::1\", "
  3314. " \"server-port\" : 777, "
  3315. " \"sender-ip\" : \"3001::2\", "
  3316. " \"sender-port\" : 778, "
  3317. " \"max-queue-size\" : 2048, "
  3318. " \"ncr-protocol\" : \"UDP\", "
  3319. " \"ncr-format\" : \"JSON\", "
  3320. " \"always-include-fqdn\" : true, "
  3321. " \"allow-client-update\" : true, "
  3322. " \"override-no-update\" : true, "
  3323. " \"override-client-update\" : true, "
  3324. " \"replace-client-name\" : \"when-present\", "
  3325. " \"generated-prefix\" : \"test.prefix\", "
  3326. " \"qualifying-suffix\" : \"test.suffix.\" },"
  3327. "\"valid-lifetime\": 4000 }";
  3328. // Convert the JSON string to configuration elements.
  3329. ConstElementPtr config;
  3330. ASSERT_NO_THROW(config = parseDHCP6(config_str));
  3331. // Pass the configuration in for parsing.
  3332. ConstElementPtr status;
  3333. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, config));
  3334. // check if returned status is OK
  3335. checkResult(status, 0);
  3336. // Verify that DHCP-DDNS updating is enabled.
  3337. EXPECT_TRUE(CfgMgr::instance().ddnsEnabled());
  3338. // Verify that the D2 configuration can be retrieved.
  3339. d2_client_config = CfgMgr::instance().getD2ClientConfig();
  3340. ASSERT_TRUE(d2_client_config);
  3341. // Verify that the configuration values are correct.
  3342. EXPECT_TRUE(d2_client_config->getEnableUpdates());
  3343. EXPECT_EQ("3001::1", d2_client_config->getServerIp().toText());
  3344. EXPECT_EQ(777, d2_client_config->getServerPort());
  3345. EXPECT_EQ("3001::2", d2_client_config->getSenderIp().toText());
  3346. EXPECT_EQ(778, d2_client_config->getSenderPort());
  3347. EXPECT_EQ(2048, d2_client_config->getMaxQueueSize());
  3348. EXPECT_EQ(dhcp_ddns::NCR_UDP, d2_client_config->getNcrProtocol());
  3349. EXPECT_EQ(dhcp_ddns::FMT_JSON, d2_client_config->getNcrFormat());
  3350. EXPECT_TRUE(d2_client_config->getAlwaysIncludeFqdn());
  3351. EXPECT_TRUE(d2_client_config->getOverrideNoUpdate());
  3352. EXPECT_TRUE(d2_client_config->getOverrideClientUpdate());
  3353. EXPECT_EQ(D2ClientConfig::RCM_WHEN_PRESENT, d2_client_config->getReplaceClientNameMode());
  3354. EXPECT_EQ("test.prefix", d2_client_config->getGeneratedPrefix());
  3355. EXPECT_EQ("test.suffix.", d2_client_config->getQualifyingSuffix());
  3356. }
  3357. // This test checks the ability of the server to handle a configuration
  3358. // containing an invalid dhcp-ddns (D2ClientConfig) entry.
  3359. TEST_F(Dhcp6ParserTest, invalidD2ClientConfig) {
  3360. // Configuration string with an invalid D2 client config,
  3361. // "server-ip" is invalid.
  3362. string config_str = "{ " + genIfaceConfig() + ","
  3363. "\"rebind-timer\": 2000, "
  3364. "\"renew-timer\": 1000, "
  3365. "\"subnet6\": [ { "
  3366. " \"pools\": [ { \"pool\": \"2001:db8:1::/80\" } ],"
  3367. " \"subnet\": \"2001:db8:1::/64\" } ], "
  3368. " \"dhcp-ddns\" : {"
  3369. " \"enable-updates\" : true, "
  3370. " \"server-ip\" : \"bogus-value\", "
  3371. " \"server-port\" : 5301, "
  3372. " \"ncr-protocol\" : \"UDP\", "
  3373. " \"ncr-format\" : \"JSON\", "
  3374. " \"always-include-fqdn\" : true, "
  3375. " \"allow-client-update\" : true, "
  3376. " \"override-no-update\" : true, "
  3377. " \"override-client-update\" : true, "
  3378. " \"replace-client-name\" : \"when-present\", "
  3379. " \"generated-prefix\" : \"test.prefix\", "
  3380. " \"qualifying-suffix\" : \"test.suffix.\" },"
  3381. "\"valid-lifetime\": 4000 }";
  3382. // Convert the JSON string to configuration elements.
  3383. ConstElementPtr config;
  3384. ASSERT_NO_THROW(config = parseDHCP6(config_str));
  3385. // Configuration should not throw, but should fail.
  3386. ConstElementPtr status;
  3387. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, config));
  3388. // check if returned status is failed.
  3389. checkResult(status, 1);
  3390. EXPECT_TRUE(errorContainsPosition(status, "<string>"));
  3391. // Verify that the D2 configuration can be fetched and is set to disabled.
  3392. D2ClientConfigPtr d2_client_config = CfgMgr::instance().getD2ClientConfig();
  3393. EXPECT_FALSE(d2_client_config->getEnableUpdates());
  3394. // Verify that the convenience method agrees.
  3395. ASSERT_FALSE(CfgMgr::instance().ddnsEnabled());
  3396. }
  3397. /// @brief Checks if the reservation is in the range of reservations.
  3398. ///
  3399. /// @param resrv Reservation to be searched for.
  3400. /// @param range Range of reservations returned by the @c Host object
  3401. /// in which the reservation will be searched.
  3402. bool reservationExists(const IPv6Resrv& resrv, const IPv6ResrvRange& range) {
  3403. for (IPv6ResrvIterator it = range.first; it != range.second;
  3404. ++it) {
  3405. if (resrv == it->second) {
  3406. return (true);
  3407. }
  3408. }
  3409. return (false);
  3410. }
  3411. // This test verifies that the host reservations can be specified for
  3412. // respective IPv6 subnets.
  3413. TEST_F(Dhcp6ParserTest, reservations) {
  3414. ConstElementPtr x;
  3415. string config = "{ " + genIfaceConfig() + ","
  3416. "\"rebind-timer\": 2000, "
  3417. "\"renew-timer\": 1000, "
  3418. "\"subnet6\": [ "
  3419. " { "
  3420. " \"pools\": [ { \"pool\": \"2001:db8:1::/80\" } ],"
  3421. " \"subnet\": \"2001:db8:1::/64\", "
  3422. " \"id\": 123,"
  3423. " \"reservations\": ["
  3424. " ]"
  3425. " },"
  3426. " {"
  3427. " \"reservations\": ["
  3428. " {"
  3429. " \"duid\": \"01:02:03:04:05:06:07:08:09:0A\","
  3430. " \"ip-addresses\": [ \"2001:db8:2::1234\" ],"
  3431. " \"hostname\": \"\","
  3432. " \"option-data\": ["
  3433. " {"
  3434. " \"name\": \"dns-servers\","
  3435. " \"data\": \"2001:db8:2::1111\""
  3436. " },"
  3437. " {"
  3438. " \"name\": \"preference\","
  3439. " \"data\": \"11\""
  3440. " }"
  3441. " ]"
  3442. " },"
  3443. " {"
  3444. " \"hw-address\": \"01:02:03:04:05:06\","
  3445. " \"ip-addresses\": [ \"2001:db8:2::abcd\" ],"
  3446. " \"hostname\": \"\","
  3447. " \"option-data\": ["
  3448. " {"
  3449. " \"name\": \"dns-servers\","
  3450. " \"data\": \"2001:db8:2::abbc\""
  3451. " },"
  3452. " {"
  3453. " \"name\": \"preference\","
  3454. " \"data\": \"25\""
  3455. " }"
  3456. " ]"
  3457. " }"
  3458. " ],"
  3459. " \"pools\": [ ],"
  3460. " \"subnet\": \"2001:db8:2::/64\", "
  3461. " \"id\": 234"
  3462. " },"
  3463. " {"
  3464. " \"pools\": [ ],"
  3465. " \"subnet\": \"2001:db8:3::/64\", "
  3466. " \"id\": 542,"
  3467. " \"reservations\": ["
  3468. " {"
  3469. " \"duid\": \"0A:09:08:07:06:05:04:03:02:01\","
  3470. " \"prefixes\": [ \"2001:db8:3:2::/96\" ],"
  3471. " \"hostname\": \"\","
  3472. " \"option-data\": ["
  3473. " {"
  3474. " \"name\": \"dns-servers\","
  3475. " \"data\": \"2001:db8:3::3333\""
  3476. " },"
  3477. " {"
  3478. " \"name\": \"preference\","
  3479. " \"data\": \"33\""
  3480. " }"
  3481. " ]"
  3482. " },"
  3483. " {"
  3484. " \"hw-address\": \"06:05:04:03:02:01\","
  3485. " \"prefixes\": [ \"2001:db8:3:1::/96\" ],"
  3486. " \"hostname\": \"\""
  3487. " }"
  3488. " ]"
  3489. " } "
  3490. "], "
  3491. "\"preferred-lifetime\": 3000,"
  3492. "\"valid-lifetime\": 4000 }";
  3493. ConstElementPtr json;
  3494. ASSERT_NO_THROW(json = parseDHCP6(config));
  3495. EXPECT_NO_THROW(x = configureDhcp6Server(srv_, json));
  3496. checkResult(x, 0);
  3497. // Make sure all subnets have been successfully configured. There is no
  3498. // need to sanity check the subnet properties because it should have
  3499. // been already tested by other tests.
  3500. const Subnet6Collection* subnets =
  3501. CfgMgr::instance().getStagingCfg()->getCfgSubnets6()->getAll();
  3502. ASSERT_TRUE(subnets);
  3503. ASSERT_EQ(3, subnets->size());
  3504. // Hosts configuration must be available.
  3505. CfgHostsPtr hosts_cfg = CfgMgr::instance().getStagingCfg()->getCfgHosts();
  3506. ASSERT_TRUE(hosts_cfg);
  3507. // Let's create an object holding hardware address of the host having
  3508. // a reservation in the subnet having id of 234. For simplicity the
  3509. // address is a collection of numbers from 1 to 6.
  3510. std::vector<uint8_t> hwaddr_vec;
  3511. for (unsigned int i = 1; i < 7; ++i) {
  3512. hwaddr_vec.push_back(static_cast<uint8_t>(i));
  3513. }
  3514. HWAddrPtr hwaddr(new HWAddr(hwaddr_vec, HTYPE_ETHER));
  3515. // Retrieve the reservation and sanity check the address reserved.
  3516. ConstHostPtr host = hosts_cfg->get6(234, DuidPtr(), hwaddr);
  3517. ASSERT_TRUE(host);
  3518. IPv6ResrvRange resrv = host->getIPv6Reservations(IPv6Resrv::TYPE_NA);
  3519. ASSERT_EQ(1, std::distance(resrv.first, resrv.second));
  3520. EXPECT_TRUE(reservationExists(IPv6Resrv(IPv6Resrv::TYPE_NA,
  3521. IOAddress("2001:db8:2::abcd")),
  3522. resrv));
  3523. // This reservation should be solely assigned to the subnet 234,
  3524. // and not to other two.
  3525. EXPECT_FALSE(hosts_cfg->get6(123, DuidPtr(), hwaddr));
  3526. EXPECT_FALSE(hosts_cfg->get6(542, DuidPtr(), hwaddr));
  3527. // Check that options are assigned correctly.
  3528. Option6AddrLstPtr opt_dns =
  3529. retrieveOption<Option6AddrLstPtr>(*host, D6O_NAME_SERVERS);
  3530. ASSERT_TRUE(opt_dns);
  3531. Option6AddrLst::AddressContainer dns_addrs = opt_dns->getAddresses();
  3532. ASSERT_EQ(1, dns_addrs.size());
  3533. EXPECT_EQ("2001:db8:2::abbc", dns_addrs[0].toText());
  3534. OptionUint8Ptr opt_prf =
  3535. retrieveOption<OptionUint8Ptr>(*host, D6O_PREFERENCE);
  3536. ASSERT_TRUE(opt_prf);
  3537. EXPECT_EQ(25, static_cast<int>(opt_prf->getValue()));
  3538. // Do the same test for the DUID based reservation.
  3539. std::vector<uint8_t> duid_vec;
  3540. for (unsigned int i = 1; i < 0xb; ++i) {
  3541. duid_vec.push_back(static_cast<uint8_t>(i));
  3542. }
  3543. DuidPtr duid(new DUID(duid_vec));
  3544. host = hosts_cfg->get6(234, duid);
  3545. ASSERT_TRUE(host);
  3546. resrv = host->getIPv6Reservations(IPv6Resrv::TYPE_NA);
  3547. ASSERT_EQ(1, std::distance(resrv.first, resrv.second));
  3548. EXPECT_TRUE(reservationExists(IPv6Resrv(IPv6Resrv::TYPE_NA,
  3549. IOAddress("2001:db8:2::1234")),
  3550. resrv));
  3551. EXPECT_FALSE(hosts_cfg->get6(123, duid));
  3552. EXPECT_FALSE(hosts_cfg->get6(542, duid));
  3553. // Check that options are assigned correctly.
  3554. opt_dns = retrieveOption<Option6AddrLstPtr>(*host, D6O_NAME_SERVERS);
  3555. ASSERT_TRUE(opt_dns);
  3556. dns_addrs = opt_dns->getAddresses();
  3557. ASSERT_EQ(1, dns_addrs.size());
  3558. EXPECT_EQ("2001:db8:2::1111", dns_addrs[0].toText());
  3559. opt_prf = retrieveOption<OptionUint8Ptr>(*host, D6O_PREFERENCE);
  3560. ASSERT_TRUE(opt_prf);
  3561. EXPECT_EQ(11, static_cast<int>(opt_prf->getValue()));
  3562. // The HW address used for one of the reservations in the subnet 542
  3563. // consists of numbers from 6 to 1. So, let's just reverse the order
  3564. // of the address from the previous test.
  3565. hwaddr->hwaddr_.assign(hwaddr_vec.rbegin(), hwaddr_vec.rend());
  3566. host = hosts_cfg->get6(542, DuidPtr(), hwaddr);
  3567. EXPECT_TRUE(host);
  3568. resrv = host->getIPv6Reservations(IPv6Resrv::TYPE_PD);
  3569. ASSERT_EQ(1, std::distance(resrv.first, resrv.second));
  3570. EXPECT_TRUE(reservationExists(IPv6Resrv(IPv6Resrv::TYPE_PD,
  3571. IOAddress("2001:db8:3:1::"),
  3572. 96), resrv));
  3573. // This reservation must not belong to other subnets.
  3574. EXPECT_FALSE(hosts_cfg->get6(123, DuidPtr(), hwaddr));
  3575. EXPECT_FALSE(hosts_cfg->get6(234, DuidPtr(), hwaddr));
  3576. // Repeat the test for the DUID based reservation in this subnet.
  3577. duid.reset(new DUID(std::vector<uint8_t>(duid_vec.rbegin(),
  3578. duid_vec.rend())));
  3579. host = hosts_cfg->get6(542, duid);
  3580. ASSERT_TRUE(host);
  3581. resrv = host->getIPv6Reservations(IPv6Resrv::TYPE_PD);
  3582. ASSERT_EQ(1, std::distance(resrv.first, resrv.second));
  3583. EXPECT_TRUE(reservationExists(IPv6Resrv(IPv6Resrv::TYPE_PD,
  3584. IOAddress("2001:db8:3:2::"),
  3585. 96), resrv));
  3586. EXPECT_FALSE(hosts_cfg->get6(123, duid));
  3587. EXPECT_FALSE(hosts_cfg->get6(234, duid));
  3588. // Check that options are assigned correctly.
  3589. opt_dns = retrieveOption<Option6AddrLstPtr>(*host, D6O_NAME_SERVERS);
  3590. ASSERT_TRUE(opt_dns);
  3591. dns_addrs = opt_dns->getAddresses();
  3592. ASSERT_EQ(1, dns_addrs.size());
  3593. EXPECT_EQ("2001:db8:3::3333", dns_addrs[0].toText());
  3594. opt_prf = retrieveOption<OptionUint8Ptr>(*host, D6O_PREFERENCE);
  3595. ASSERT_TRUE(opt_prf);
  3596. EXPECT_EQ(33, static_cast<int>(opt_prf->getValue()));
  3597. }
  3598. // This test checks that it is possible to configure option data for a
  3599. // host using a user defined option format.
  3600. TEST_F(Dhcp6ParserTest, reservationWithOptionDefinition) {
  3601. ConstElementPtr x;
  3602. // The following configuration contains host declaration in which
  3603. // a non-standard option is used. This option has option definition
  3604. // specified in the configuration.
  3605. string config = "{ " + genIfaceConfig() + ","
  3606. "\"rebind-timer\": 2000, "
  3607. "\"renew-timer\": 1000, "
  3608. "\"option-def\": [ {"
  3609. " \"name\": \"foo\","
  3610. " \"code\": 100,"
  3611. " \"type\": \"uint32\","
  3612. " \"space\": \"isc\""
  3613. "} ],"
  3614. "\"subnet6\": [ "
  3615. " {"
  3616. " \"reservations\": ["
  3617. " {"
  3618. " \"duid\": \"01:02:03:04:05:06:07:08:09:0A\","
  3619. " \"ip-addresses\": [ \"2001:db8:2::1234\" ],"
  3620. " \"hostname\": \"\","
  3621. " \"option-data\": ["
  3622. " {"
  3623. " \"name\": \"foo\","
  3624. " \"data\": \"11\","
  3625. " \"space\": \"isc\""
  3626. " }"
  3627. " ]"
  3628. " }"
  3629. " ],"
  3630. " \"pools\": [ ],"
  3631. " \"subnet\": \"2001:db8:2::/64\", "
  3632. " \"id\": 234"
  3633. " }"
  3634. "],"
  3635. "\"preferred-lifetime\": 3000,"
  3636. "\"valid-lifetime\": 4000 }";
  3637. ConstElementPtr json;
  3638. ASSERT_NO_THROW(json = parseDHCP6(config));
  3639. EXPECT_NO_THROW(x = configureDhcp6Server(srv_, json));
  3640. checkResult(x, 0);
  3641. // Hosts configuration must be available.
  3642. CfgHostsPtr hosts_cfg = CfgMgr::instance().getStagingCfg()->getCfgHosts();
  3643. ASSERT_TRUE(hosts_cfg);
  3644. // Let's create an object holding DUID of the host. For simplicity the
  3645. // address is a collection of numbers from 1 to A.
  3646. std::vector<uint8_t> duid_vec;
  3647. for (unsigned int i = 1; i < 0xB; ++i) {
  3648. duid_vec.push_back(static_cast<uint8_t>(i));
  3649. }
  3650. DuidPtr duid(new DUID(duid_vec));
  3651. // Retrieve the reservation and sanity check the address reserved.
  3652. ConstHostPtr host = hosts_cfg->get6(234, duid);
  3653. ASSERT_TRUE(host);
  3654. // Check if the option has been parsed.
  3655. OptionUint32Ptr opt_foo = retrieveOption<OptionUint32Ptr>(*host, "isc",
  3656. 100);
  3657. ASSERT_TRUE(opt_foo);
  3658. EXPECT_EQ(100, opt_foo->getType());
  3659. EXPECT_EQ(11, opt_foo->getValue());
  3660. }
  3661. // This test verifies that the bogus host reservation would trigger a
  3662. // server configuration error.
  3663. TEST_F(Dhcp6ParserTest, reservationBogus) {
  3664. // Case 1: misspelled "duid" parameter.
  3665. ConstElementPtr x;
  3666. string config = "{ " + genIfaceConfig() + ","
  3667. "\"rebind-timer\": 2000, "
  3668. "\"renew-timer\": 1000, "
  3669. "\"subnet6\": [ "
  3670. " { "
  3671. " \"pools\": [ ],"
  3672. " \"subnet\": \"2001:db8:3::/64\", "
  3673. " \"id\": 542,"
  3674. " \"reservations\": ["
  3675. " {"
  3676. " \"dui\": \"0A:09:08:07:06:05:04:03:02:01\","
  3677. " \"prefixes\": [ \"2001:db8:3:2::/96\" ],"
  3678. " \"hostname\": \"\""
  3679. " }"
  3680. " ]"
  3681. " } "
  3682. "], "
  3683. "\"preferred-lifetime\": 3000,"
  3684. "\"valid-lifetime\": 4000 }";
  3685. ConstElementPtr json = parseJSON(config);
  3686. EXPECT_NO_THROW(x = configureDhcp6Server(srv_, json));
  3687. checkResult(x, 1);
  3688. EXPECT_THROW(parseDHCP6(config), Dhcp6ParseError);
  3689. // Case 2: DUID and HW Address both specified.
  3690. config = "{ " + genIfaceConfig() + ","
  3691. "\"rebind-timer\": 2000, "
  3692. "\"renew-timer\": 1000, "
  3693. "\"subnet6\": [ "
  3694. " { "
  3695. " \"pools\": [ ],"
  3696. " \"subnet\": \"2001:db8:3::/64\", "
  3697. " \"id\": 542,"
  3698. " \"reservations\": ["
  3699. " {"
  3700. " \"hw-address\": \"01:02:03:04:05:06\","
  3701. " \"duid\": \"0A:09:08:07:06:05:04:03:02:01\","
  3702. " \"prefixes\": [ \"2001:db8:3:2::/96\" ],"
  3703. " \"hostname\": \"\""
  3704. " }"
  3705. " ]"
  3706. " } "
  3707. "], "
  3708. "\"preferred-lifetime\": 3000,"
  3709. "\"valid-lifetime\": 4000 }";
  3710. json = parseDHCP6(config);
  3711. // Remove existing configuration, if any.
  3712. CfgMgr::instance().clear();
  3713. EXPECT_NO_THROW(x = configureDhcp6Server(srv_, json));
  3714. checkResult(x, 1);
  3715. // Case 3: Neither ip address nor hostname specified.
  3716. config = "{ " + genIfaceConfig() + ","
  3717. "\"rebind-timer\": 2000, "
  3718. "\"renew-timer\": 1000, "
  3719. "\"subnet6\": [ "
  3720. " { "
  3721. " \"pools\": [ ],"
  3722. " \"subnet\": \"2001:db8:3::/64\", "
  3723. " \"id\": 542,"
  3724. " \"reservations\": ["
  3725. " {"
  3726. " \"duid\": \"0A:09:08:07:06:05:04:03:02:01\""
  3727. " }"
  3728. " ]"
  3729. " } "
  3730. "], "
  3731. "\"preferred-lifetime\": 3000,"
  3732. "\"valid-lifetime\": 4000 }";
  3733. json = parseDHCP6(config);
  3734. // Remove existing configuration, if any.
  3735. CfgMgr::instance().clear();
  3736. EXPECT_NO_THROW(x = configureDhcp6Server(srv_, json));
  3737. checkResult(x, 1);
  3738. // Case 4: Broken specification of option data.
  3739. config = "{ " + genIfaceConfig() + ","
  3740. "\"rebind-timer\": 2000, "
  3741. "\"renew-timer\": 1000, "
  3742. "\"subnet6\": [ "
  3743. " { "
  3744. " \"pools\": [ ],"
  3745. " \"subnet\": \"2001:db8:3::/64\", "
  3746. " \"id\": 542,"
  3747. " \"reservations\": ["
  3748. " {"
  3749. " \"duid\": \"0A:09:08:07:06:05:04:03:02:01\","
  3750. " \"option-data\": ["
  3751. " {"
  3752. " \"name\": \"dns-servers\","
  3753. " \"data\": \"invalid-ip-address\""
  3754. " }"
  3755. " ]"
  3756. " }"
  3757. " ]"
  3758. " } "
  3759. "], "
  3760. "\"preferred-lifetime\": 3000,"
  3761. "\"valid-lifetime\": 4000 }";
  3762. json = parseDHCP6(config);
  3763. // Remove existing configuration, if any.
  3764. CfgMgr::instance().clear();
  3765. EXPECT_NO_THROW(x = configureDhcp6Server(srv_, json));
  3766. checkResult(x, 1);
  3767. }
  3768. /// The goal of this test is to verify that configuration can include
  3769. /// MAC/Hardware sources. This test also checks if the aliases are
  3770. /// handled properly (rfc6939 = client-addr-relay, rfc4649 = remote-id,
  3771. /// rfc4580 = subscriber-id).
  3772. TEST_F(Dhcp6ParserTest, macSources) {
  3773. ConstElementPtr json;
  3774. ASSERT_NO_THROW(json =
  3775. parseDHCP6("{ " + genIfaceConfig() + ","
  3776. "\"mac-sources\": [ \"rfc6939\", \"rfc4649\", \"rfc4580\","
  3777. "\"client-link-addr-option\", \"remote-id\", \"subscriber-id\"],"
  3778. "\"preferred-lifetime\": 3000,"
  3779. "\"rebind-timer\": 2000, "
  3780. "\"renew-timer\": 1000, "
  3781. "\"subnet6\": [ ], "
  3782. "\"valid-lifetime\": 4000 }"));
  3783. ConstElementPtr status;
  3784. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  3785. // returned value should be 0 (success)
  3786. checkResult(status, 0);
  3787. CfgMACSources mac_sources = CfgMgr::instance().getStagingCfg()->getMACSources().get();
  3788. ASSERT_EQ(6, mac_sources.size());
  3789. // Let's check the aliases. They should be recognized to their base methods.
  3790. EXPECT_EQ(HWAddr::HWADDR_SOURCE_CLIENT_ADDR_RELAY_OPTION, mac_sources[0]);
  3791. EXPECT_EQ(HWAddr::HWADDR_SOURCE_REMOTE_ID, mac_sources[1]);
  3792. EXPECT_EQ(HWAddr::HWADDR_SOURCE_SUBSCRIBER_ID, mac_sources[2]);
  3793. // Let's check if the actual methods are recognized properly.
  3794. EXPECT_EQ(HWAddr::HWADDR_SOURCE_CLIENT_ADDR_RELAY_OPTION, mac_sources[3]);
  3795. EXPECT_EQ(HWAddr::HWADDR_SOURCE_REMOTE_ID, mac_sources[4]);
  3796. EXPECT_EQ(HWAddr::HWADDR_SOURCE_SUBSCRIBER_ID, mac_sources[5]);
  3797. }
  3798. /// The goal of this test is to verify that MAC sources configuration can be
  3799. /// empty.
  3800. /// Note the Dhcp6 parser requires the list to NOT be empty?!
  3801. TEST_F(Dhcp6ParserTest, macSourcesEmpty) {
  3802. ConstElementPtr status;
  3803. EXPECT_NO_THROW(status = configureDhcp6Server(srv_,
  3804. parseJSON("{ " + genIfaceConfig() + ","
  3805. "\"mac-sources\": [ ],"
  3806. "\"preferred-lifetime\": 3000,"
  3807. "\"rebind-timer\": 2000, "
  3808. "\"renew-timer\": 1000, "
  3809. "\"subnet6\": [ ], "
  3810. "\"valid-lifetime\": 4000 }")));
  3811. // returned value should be 0 (success)
  3812. checkResult(status, 0);
  3813. CfgMACSources mac_sources = CfgMgr::instance().getStagingCfg()->getMACSources().get();
  3814. EXPECT_EQ(0, mac_sources.size());
  3815. }
  3816. /// The goal of this test is to verify that MAC sources configuration can
  3817. /// only use valid parameters.
  3818. TEST_F(Dhcp6ParserTest, macSourcesBogus) {
  3819. ConstElementPtr json;
  3820. ASSERT_NO_THROW(json = parseDHCP6("{ " + genIfaceConfig() + ","
  3821. "\"mac-sources\": [ \"from-wire\" ],"
  3822. "\"preferred-lifetime\": 3000,"
  3823. "\"rebind-timer\": 2000, "
  3824. "\"renew-timer\": 1000, "
  3825. "\"subnet6\": [ ], "
  3826. "\"valid-lifetime\": 4000 }"));
  3827. ConstElementPtr status;
  3828. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  3829. // returned value should be 1 (failure)
  3830. checkResult(status, 1);
  3831. }
  3832. /// The goal of this test is to verify that Host Reservation modes can be
  3833. /// specified on a per-subnet basis.
  3834. TEST_F(Dhcp6ParserTest, hostReservationPerSubnet) {
  3835. /// - Configuration:
  3836. /// - only addresses (no prefixes)
  3837. /// - 4 subnets with:
  3838. /// - 2001:db8:1::/64 (all reservations enabled)
  3839. /// - 2001:db8:2::/64 (out-of-pool reservations)
  3840. /// - 2001:db8:3::/64 (reservations disabled)
  3841. /// - 2001:db8:3::/64 (reservations not specified)
  3842. const char* HR_CONFIG =
  3843. "{"
  3844. "\"preferred-lifetime\": 3000,"
  3845. "\"rebind-timer\": 2000, "
  3846. "\"renew-timer\": 1000, "
  3847. "\"subnet6\": [ { "
  3848. " \"pools\": [ { \"pool\": \"2001:db8:1::/64\" } ],"
  3849. " \"subnet\": \"2001:db8:1::/48\", "
  3850. " \"reservation-mode\": \"all\""
  3851. " },"
  3852. " {"
  3853. " \"pools\": [ { \"pool\": \"2001:db8:2::/64\" } ],"
  3854. " \"subnet\": \"2001:db8:2::/48\", "
  3855. " \"reservation-mode\": \"out-of-pool\""
  3856. " },"
  3857. " {"
  3858. " \"pools\": [ { \"pool\": \"2001:db8:3::/64\" } ],"
  3859. " \"subnet\": \"2001:db8:3::/48\", "
  3860. " \"reservation-mode\": \"disabled\""
  3861. " },"
  3862. " {"
  3863. " \"pools\": [ { \"pool\": \"2001:db8:4::/64\" } ],"
  3864. " \"subnet\": \"2001:db8:4::/48\" "
  3865. " } ],"
  3866. "\"valid-lifetime\": 4000 }";
  3867. ConstElementPtr json;
  3868. ASSERT_NO_THROW(json = parseDHCP6(HR_CONFIG));
  3869. ConstElementPtr status;
  3870. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  3871. // returned value should be 0 (success)
  3872. checkResult(status, 0);
  3873. CfgMgr::instance().commit();
  3874. // Let's get all subnets and check that there are 4 of them.
  3875. ConstCfgSubnets6Ptr subnets = CfgMgr::instance().getCurrentCfg()->getCfgSubnets6();
  3876. ASSERT_TRUE(subnets);
  3877. const Subnet6Collection* subnet_col = subnets->getAll();
  3878. ASSERT_EQ(4, subnet_col->size()); // We expect 4 subnets
  3879. // Let's check if the parsed subnets have correct HR modes.
  3880. // Subnet 1
  3881. Subnet6Ptr subnet;
  3882. subnet = subnets->selectSubnet(IOAddress("2001:db8:1::1"));
  3883. ASSERT_TRUE(subnet);
  3884. EXPECT_EQ(Subnet::HR_ALL, subnet->getHostReservationMode());
  3885. // Subnet 2
  3886. subnet = subnets->selectSubnet(IOAddress("2001:db8:2::1"));
  3887. ASSERT_TRUE(subnet);
  3888. EXPECT_EQ(Subnet::HR_OUT_OF_POOL, subnet->getHostReservationMode());
  3889. // Subnet 3
  3890. subnet = subnets->selectSubnet(IOAddress("2001:db8:3::1"));
  3891. ASSERT_TRUE(subnet);
  3892. EXPECT_EQ(Subnet::HR_DISABLED, subnet->getHostReservationMode());
  3893. // Subnet 4
  3894. subnet = subnets->selectSubnet(IOAddress("2001:db8:4::1"));
  3895. ASSERT_TRUE(subnet);
  3896. EXPECT_EQ(Subnet::HR_ALL, subnet->getHostReservationMode());
  3897. }
  3898. /// The goal of this test is to verify that configuration can include
  3899. /// Relay Supplied options (specified as numbers).
  3900. TEST_F(Dhcp6ParserTest, rsooNumbers) {
  3901. ConstElementPtr json;
  3902. ASSERT_NO_THROW(json =
  3903. parseDHCP6("{ " + genIfaceConfig() + ","
  3904. "\"relay-supplied-options\": [ \"10\", \"20\", \"30\" ],"
  3905. "\"preferred-lifetime\": 3000,"
  3906. "\"rebind-timer\": 2000, "
  3907. "\"renew-timer\": 1000, "
  3908. "\"subnet6\": [ ], "
  3909. "\"valid-lifetime\": 4000 }"));
  3910. ConstElementPtr status;
  3911. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  3912. // returned value should be 0 (success)
  3913. checkResult(status, 0);
  3914. // The following codes should be enabled now
  3915. EXPECT_TRUE(CfgMgr::instance().getStagingCfg()->getCfgRSOO()->enabled(10));
  3916. EXPECT_TRUE(CfgMgr::instance().getStagingCfg()->getCfgRSOO()->enabled(20));
  3917. EXPECT_TRUE(CfgMgr::instance().getStagingCfg()->getCfgRSOO()->enabled(30));
  3918. // This option is on the IANA list, so it should be allowed all the time
  3919. // (http://www.iana.org/assignments/dhcpv6-parameters/dhcpv6-parameters.xhtml)
  3920. EXPECT_TRUE(CfgMgr::instance().getStagingCfg()->getCfgRSOO()
  3921. ->enabled(D6O_ERP_LOCAL_DOMAIN_NAME));
  3922. // Those options are not enabled
  3923. EXPECT_FALSE(CfgMgr::instance().getStagingCfg()->getCfgRSOO()->enabled(25));
  3924. EXPECT_FALSE(CfgMgr::instance().getStagingCfg()->getCfgRSOO()->enabled(1));
  3925. }
  3926. /// The goal of this test is to verify that configuration can include
  3927. /// Relay Supplied options (specified as names).
  3928. TEST_F(Dhcp6ParserTest, rsooNames) {
  3929. ConstElementPtr json;
  3930. ASSERT_NO_THROW(json =
  3931. parseDHCP6("{ " + genIfaceConfig() + ","
  3932. "\"relay-supplied-options\": [ \"dns-servers\", \"remote-id\" ],"
  3933. "\"preferred-lifetime\": 3000,"
  3934. "\"rebind-timer\": 2000, "
  3935. "\"renew-timer\": 1000, "
  3936. "\"subnet6\": [ ], "
  3937. "\"valid-lifetime\": 4000 }"));
  3938. ConstElementPtr status;
  3939. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  3940. // returned value should be 0 (success)
  3941. checkResult(status, 0);
  3942. for (uint16_t code = 0; code < D6O_NAME_SERVERS; ++code) {
  3943. EXPECT_FALSE(CfgMgr::instance().getStagingCfg()->getCfgRSOO()
  3944. ->enabled(code)) << " for option code " << code;
  3945. }
  3946. // The following code should be enabled now
  3947. EXPECT_TRUE(CfgMgr::instance().getStagingCfg()->getCfgRSOO()
  3948. ->enabled(D6O_NAME_SERVERS));
  3949. for (uint16_t code = D6O_NAME_SERVERS + 1; code < D6O_REMOTE_ID; ++code) {
  3950. EXPECT_FALSE(CfgMgr::instance().getStagingCfg()->getCfgRSOO()
  3951. ->enabled(code)) << " for option code " << code;
  3952. }
  3953. // Check remote-id. It should be enabled.
  3954. EXPECT_TRUE(CfgMgr::instance().getStagingCfg()->getCfgRSOO()
  3955. ->enabled(D6O_REMOTE_ID));
  3956. for (uint16_t code = D6O_REMOTE_ID + 1; code < D6O_ERP_LOCAL_DOMAIN_NAME; ++code) {
  3957. EXPECT_FALSE(CfgMgr::instance().getStagingCfg()->getCfgRSOO()
  3958. ->enabled(code)) << " for option code " << code;
  3959. }
  3960. // This option is on the IANA list, so it should be allowed all the time
  3961. // (http://www.iana.org/assignments/dhcpv6-parameters/dhcpv6-parameters.xhtml)
  3962. EXPECT_TRUE(CfgMgr::instance().getStagingCfg()->getCfgRSOO()
  3963. ->enabled(D6O_ERP_LOCAL_DOMAIN_NAME));
  3964. for (uint16_t code = D6O_ERP_LOCAL_DOMAIN_NAME + 1; code < 300; ++code) {
  3965. EXPECT_FALSE(CfgMgr::instance().getStagingCfg()->getCfgRSOO()
  3966. ->enabled(code)) << " for option code " << code;
  3967. }
  3968. }
  3969. TEST_F(Dhcp6ParserTest, rsooNegativeNumber) {
  3970. ConstElementPtr json;
  3971. ASSERT_NO_THROW(json =
  3972. parseDHCP6("{ " + genIfaceConfig() + ","
  3973. "\"relay-supplied-options\": [ \"80\", \"-2\" ],"
  3974. "\"preferred-lifetime\": 3000,"
  3975. "\"rebind-timer\": 2000, "
  3976. "\"renew-timer\": 1000, "
  3977. "\"subnet6\": [ ], "
  3978. "\"valid-lifetime\": 4000 }"));
  3979. ConstElementPtr status;
  3980. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  3981. // returned value should be 0 (success)
  3982. checkResult(status, 1);
  3983. EXPECT_TRUE(errorContainsPosition(status, "<string>"));
  3984. }
  3985. TEST_F(Dhcp6ParserTest, rsooBogusName) {
  3986. ConstElementPtr json;
  3987. ASSERT_NO_THROW(json =
  3988. parseDHCP6("{ " + genIfaceConfig() + ","
  3989. "\"relay-supplied-options\": [ \"bogus\", \"dns-servers\" ],"
  3990. "\"preferred-lifetime\": 3000,"
  3991. "\"rebind-timer\": 2000, "
  3992. "\"renew-timer\": 1000, "
  3993. "\"subnet6\": [ ], "
  3994. "\"valid-lifetime\": 4000 }"));
  3995. ConstElementPtr status;
  3996. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  3997. // returned value should be 0 (success)
  3998. checkResult(status, 1);
  3999. EXPECT_TRUE(errorContainsPosition(status, "<string>"));
  4000. }
  4001. /// Check that the decline-probation-period value can be set properly.
  4002. TEST_F(Dhcp6ParserTest, declineTimerDefault) {
  4003. string config_txt = "{ " + genIfaceConfig() + ","
  4004. "\"subnet6\": [ ] "
  4005. "}";
  4006. ConstElementPtr config;
  4007. ASSERT_NO_THROW(config = parseDHCP6(config_txt));
  4008. ConstElementPtr status;
  4009. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, config));
  4010. // returned value should be 0 (success)
  4011. checkResult(status, 0);
  4012. // The value of decline-probation-period must be equal to the
  4013. // default value.
  4014. EXPECT_EQ(DEFAULT_DECLINE_PROBATION_PERIOD,
  4015. CfgMgr::instance().getStagingCfg()->getDeclinePeriod());
  4016. }
  4017. /// Check that the decline-probation-period value can be set properly.
  4018. TEST_F(Dhcp6ParserTest, declineTimer) {
  4019. string config = "{ " + genIfaceConfig() + "," +
  4020. "\"decline-probation-period\": 12345,"
  4021. "\"subnet6\": [ ]"
  4022. "}";
  4023. ConstElementPtr json;
  4024. ASSERT_NO_THROW(json = parseDHCP6(config));
  4025. ConstElementPtr status;
  4026. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  4027. // returned value should be 0 (success)
  4028. checkResult(status, 0);
  4029. // The value of decline-probation-period must be equal to the
  4030. // value specified.
  4031. EXPECT_EQ(12345,
  4032. CfgMgr::instance().getStagingCfg()->getDeclinePeriod());
  4033. }
  4034. /// Check that an incorrect decline-probation-period value will be caught.
  4035. TEST_F(Dhcp6ParserTest, declineTimerError) {
  4036. string config = "{ " + genIfaceConfig() + "," +
  4037. "\"decline-probation-period\": \"soon\","
  4038. "\"subnet6\": [ ]"
  4039. "}";
  4040. ConstElementPtr json = parseJSON(config);
  4041. ConstElementPtr status;
  4042. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  4043. // returned value should be 1 (error)
  4044. checkResult(status, 1);
  4045. // Check that the error contains error position.
  4046. EXPECT_TRUE(errorContainsPosition(status, "<string>"));
  4047. // Check that the Dhcp6 parser catches the type error
  4048. EXPECT_THROW(parseDHCP6(config), Dhcp6ParseError);
  4049. }
  4050. // Check that configuration for the expired leases processing may be
  4051. // specified.
  4052. TEST_F(Dhcp6ParserTest, expiredLeasesProcessing) {
  4053. // Create basic configuration with the expiration specific parameters.
  4054. string config = "{ " + genIfaceConfig() + "," +
  4055. "\"expired-leases-processing\": "
  4056. "{"
  4057. " \"reclaim-timer-wait-time\": 20,"
  4058. " \"flush-reclaimed-timer-wait-time\": 35,"
  4059. " \"hold-reclaimed-time\": 1800,"
  4060. " \"max-reclaim-leases\": 50,"
  4061. " \"max-reclaim-time\": 100,"
  4062. " \"unwarned-reclaim-cycles\": 10"
  4063. "},"
  4064. "\"subnet6\": [ ]"
  4065. "}";
  4066. ConstElementPtr json;
  4067. ASSERT_NO_THROW(json = parseDHCP6(config));
  4068. ConstElementPtr status;
  4069. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  4070. // Returned value should be 0 (success)
  4071. checkResult(status, 0);
  4072. // The value of decline-probation-period must be equal to the
  4073. // value specified.
  4074. CfgExpirationPtr cfg = CfgMgr::instance().getStagingCfg()->getCfgExpiration();
  4075. ASSERT_TRUE(cfg);
  4076. // Verify that parameters are correct.
  4077. EXPECT_EQ(20, cfg->getReclaimTimerWaitTime());
  4078. EXPECT_EQ(35, cfg->getFlushReclaimedTimerWaitTime());
  4079. EXPECT_EQ(1800, cfg->getHoldReclaimedTime());
  4080. EXPECT_EQ(50, cfg->getMaxReclaimLeases());
  4081. EXPECT_EQ(100, cfg->getMaxReclaimTime());
  4082. EXPECT_EQ(10, cfg->getUnwarnedReclaimCycles());
  4083. }
  4084. // Check that invalid configuration for the expired leases processing is
  4085. // causing an error.
  4086. TEST_F(Dhcp6ParserTest, expiredLeasesProcessingError) {
  4087. // Create basic configuration with the expiration specific parameters.
  4088. // One of the parameters holds invalid value.
  4089. string config = "{ " + genIfaceConfig() + "," +
  4090. "\"expired-leases-processing\": "
  4091. "{"
  4092. " \"reclaim-timer-wait-time\": -5,"
  4093. " \"flush-reclaimed-timer-wait-time\": 35,"
  4094. " \"hold-reclaimed-time\": 1800,"
  4095. " \"max-reclaim-leases\": 50,"
  4096. " \"max-reclaim-time\": 100,"
  4097. " \"unwarned-reclaim-cycles\": 10"
  4098. "},"
  4099. "\"subnet6\": [ ]"
  4100. "}";
  4101. ConstElementPtr json;
  4102. ASSERT_NO_THROW(json = parseDHCP6(config));
  4103. ConstElementPtr status;
  4104. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  4105. // Returned value should be 0 (error)
  4106. checkResult(status, 1);
  4107. // Check that the error contains error position.
  4108. EXPECT_TRUE(errorContainsPosition(status, "<string>"));
  4109. }
  4110. // Verifies that simple list of valid classes parses and
  4111. // is staged for commit.
  4112. TEST_F(Dhcp6ParserTest, validClientClassDictionary) {
  4113. string config = "{ " + genIfaceConfig() + ","
  4114. "\"preferred-lifetime\": 3000, \n"
  4115. "\"rebind-timer\": 2000, \n"
  4116. "\"renew-timer\": 1000, \n"
  4117. "\"client-classes\" : [ \n"
  4118. " { \n"
  4119. " \"name\": \"one\" \n"
  4120. " }, \n"
  4121. " { \n"
  4122. " \"name\": \"two\" \n"
  4123. " }, \n"
  4124. " { \n"
  4125. " \"name\": \"three\" \n"
  4126. " } \n"
  4127. "], \n"
  4128. "\"subnet6\": [ { \n"
  4129. " \"pools\": [ { \"pool\": \"2001:db8:1::1 - 2001:db8:1::ffff\" } ], \n"
  4130. " \"subnet\": \"2001:db8:1::/64\" } ], \n"
  4131. "\"valid-lifetime\": 4000 } \n";
  4132. ConstElementPtr json;
  4133. ASSERT_NO_THROW(json = parseDHCP6(config));
  4134. ConstElementPtr status;
  4135. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  4136. ASSERT_TRUE(status);
  4137. checkResult(status, 0);
  4138. // We check staging config because CfgMgr::commit hasn't been executed.
  4139. ClientClassDictionaryPtr dictionary;
  4140. dictionary = CfgMgr::instance().getStagingCfg()->getClientClassDictionary();
  4141. ASSERT_TRUE(dictionary);
  4142. EXPECT_EQ(3, dictionary->getClasses()->size());
  4143. // Execute the commit
  4144. ASSERT_NO_THROW(CfgMgr::instance().commit());
  4145. // Verify that after commit, the current config has the correct dictionary
  4146. dictionary = CfgMgr::instance().getCurrentCfg()->getClientClassDictionary();
  4147. ASSERT_TRUE(dictionary);
  4148. EXPECT_EQ(3, dictionary->getClasses()->size());
  4149. }
  4150. // Verifies that a class list containing an invalid
  4151. // class definition causes a configuration error.
  4152. TEST_F(Dhcp6ParserTest, invalidClientClassDictionary) {
  4153. string config = "{ " + genIfaceConfig() + "," +
  4154. "\"valid-lifetime\": 4000, \n"
  4155. "\"rebind-timer\": 2000, \n"
  4156. "\"renew-timer\": 1000, \n"
  4157. "\"client-classes\" : [ \n"
  4158. " { \n"
  4159. " \"name\": \"one\", \n"
  4160. " \"bogus\": \"bad\" \n"
  4161. " } \n"
  4162. "], \n"
  4163. "\"subnet4\": [ { \n"
  4164. " \"pools\": [ { \"pool\": \"192.0.2.1 - 192.0.2.100\" } ], \n"
  4165. " \"subnet\": \"192.0.2.0/24\" \n"
  4166. " } ] \n"
  4167. "} \n";
  4168. ConstElementPtr json = parseJSON(config);
  4169. ConstElementPtr status;
  4170. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  4171. ASSERT_TRUE(status);
  4172. checkResult(status, 1);
  4173. EXPECT_THROW(parseDHCP6(config), Dhcp6ParseError);
  4174. }
  4175. // Test verifies that regular configuration does not provide any user context
  4176. // in the address pool.
  4177. TEST_F(Dhcp6ParserTest, poolUserContextMissing) {
  4178. PoolPtr pool;
  4179. getPool(string(PARSER_CONFIGS[0]), 0, 0, Lease::TYPE_NA, pool);
  4180. ASSERT_TRUE(pool);
  4181. EXPECT_FALSE(pool->getContext());
  4182. }
  4183. // Test verifies that it's possible to specify empty user context in the
  4184. // address pool.
  4185. TEST_F(Dhcp6ParserTest, poolUserContextEmpty) {
  4186. PoolPtr pool;
  4187. getPool(string(PARSER_CONFIGS[1]), 0, 0, Lease::TYPE_NA, pool);
  4188. ASSERT_TRUE(pool);
  4189. ConstElementPtr ctx = pool->getContext();
  4190. ASSERT_TRUE(ctx);
  4191. // The context should be of type map and not contain any parameters.
  4192. EXPECT_EQ(Element::map, ctx->getType());
  4193. EXPECT_EQ(0, ctx->size());
  4194. }
  4195. // Test verifies that it's possible to specify parameters in the user context
  4196. // in the address pool.
  4197. TEST_F(Dhcp6ParserTest, poolUserContextlw4over6) {
  4198. PoolPtr pool;
  4199. getPool(string(PARSER_CONFIGS[2]), 0, 0, Lease::TYPE_NA, pool);
  4200. ASSERT_TRUE(pool);
  4201. ConstElementPtr ctx = pool->getContext();
  4202. ASSERT_TRUE(ctx);
  4203. // The context should be of type map and contain 4 parameters.
  4204. EXPECT_EQ(Element::map, ctx->getType());
  4205. EXPECT_EQ(4, ctx->size());
  4206. ConstElementPtr ratio = ctx->get("lw4over6-sharing-ratio");
  4207. ConstElementPtr v4pool = ctx->get("lw4over6-v4-pool");
  4208. ConstElementPtr exclude = ctx->get("lw4over6-sysports-exclude");
  4209. ConstElementPtr v6len = ctx->get("lw4over6-bind-prefix-len");
  4210. ASSERT_TRUE(ratio);
  4211. ASSERT_EQ(Element::integer, ratio->getType());
  4212. int64_t int_value;
  4213. EXPECT_NO_THROW(ratio->getValue(int_value));
  4214. EXPECT_EQ(64L, int_value);
  4215. ASSERT_TRUE(v4pool);
  4216. ASSERT_EQ(Element::string, v4pool->getType());
  4217. EXPECT_EQ("192.0.2.0/24", v4pool->stringValue());
  4218. ASSERT_TRUE(exclude);
  4219. bool bool_value;
  4220. ASSERT_EQ(Element::boolean, exclude->getType());
  4221. EXPECT_NO_THROW(exclude->getValue(bool_value));
  4222. EXPECT_EQ(true, bool_value);
  4223. ASSERT_TRUE(v6len);
  4224. ASSERT_EQ(Element::integer, v6len->getType());
  4225. EXPECT_NO_THROW(v6len->getValue(int_value));
  4226. EXPECT_EQ(56L, int_value);
  4227. }
  4228. // Test verifies that regular configuration does not provide any user context
  4229. // in the address pool.
  4230. TEST_F(Dhcp6ParserTest, pdPoolUserContextMissing) {
  4231. PoolPtr pool;
  4232. getPool(string(PARSER_CONFIGS[3]), 0, 0, Lease::TYPE_PD, pool);
  4233. ASSERT_TRUE(pool);
  4234. EXPECT_FALSE(pool->getContext());
  4235. }
  4236. // Test verifies that it's possible to specify empty user context in the
  4237. // address pool.
  4238. TEST_F(Dhcp6ParserTest, pdPoolUserContextEmpty) {
  4239. PoolPtr pool;
  4240. getPool(string(PARSER_CONFIGS[4]), 0, 0, Lease::TYPE_PD, pool);
  4241. ASSERT_TRUE(pool);
  4242. ConstElementPtr ctx = pool->getContext();
  4243. ASSERT_TRUE(ctx);
  4244. // The context should be of type map and not contain any parameters.
  4245. EXPECT_EQ(Element::map, ctx->getType());
  4246. EXPECT_EQ(0, ctx->size());
  4247. }
  4248. // Test verifies that it's possible to specify parameters in the user context
  4249. // in the address pool.
  4250. TEST_F(Dhcp6ParserTest, pdPoolUserContextlw4over6) {
  4251. PoolPtr pool;
  4252. getPool(string(PARSER_CONFIGS[5]), 0, 0, Lease::TYPE_PD, pool);
  4253. ASSERT_TRUE(pool);
  4254. ConstElementPtr ctx = pool->getContext();
  4255. ASSERT_TRUE(ctx);
  4256. // The context should be of type map and contain 4 parameters.
  4257. EXPECT_EQ(Element::map, ctx->getType());
  4258. EXPECT_EQ(4, ctx->size());
  4259. ConstElementPtr ratio = ctx->get("lw4over6-sharing-ratio");
  4260. ConstElementPtr v4pool = ctx->get("lw4over6-v4-pool");
  4261. ConstElementPtr exclude = ctx->get("lw4over6-sysports-exclude");
  4262. ConstElementPtr v6len = ctx->get("lw4over6-bind-prefix-len");
  4263. ASSERT_TRUE(ratio);
  4264. ASSERT_EQ(Element::integer, ratio->getType());
  4265. int64_t int_value;
  4266. EXPECT_NO_THROW(ratio->getValue(int_value));
  4267. EXPECT_EQ(64L, int_value);
  4268. ASSERT_TRUE(v4pool);
  4269. ASSERT_EQ(Element::string, v4pool->getType());
  4270. EXPECT_EQ("192.0.2.0/24", v4pool->stringValue());
  4271. ASSERT_TRUE(exclude);
  4272. bool bool_value;
  4273. ASSERT_EQ(Element::boolean, exclude->getType());
  4274. EXPECT_NO_THROW(exclude->getValue(bool_value));
  4275. EXPECT_EQ(true, bool_value);
  4276. ASSERT_TRUE(v6len);
  4277. ASSERT_EQ(Element::integer, v6len->getType());
  4278. EXPECT_NO_THROW(v6len->getValue(int_value));
  4279. EXPECT_EQ(56L, int_value);
  4280. }
  4281. };