config_parser_unittest.cc 186 KB

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