config_parser_unittest.cc 187 KB

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