config_parser_unittest.cc 160 KB

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