config_parser_unittest.cc 187 KB

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