config_parser_unittest.cc 168 KB

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