config_parser_unittest.cc 172 KB

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