config_parser_unittest.cc 168 KB

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