config_parser_unittest.cc 133 KB

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