config_parser_unittest.cc 169 KB

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