config_parser_unittest.cc 187 KB

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