config_parser_unittest.cc 192 KB

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