config_parser_unittest.cc 81 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263
  1. // Copyright (C) 2012-2013 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/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 <dhcpsrv/subnet.h>
  25. #include <dhcpsrv/cfgmgr.h>
  26. #include <hooks/hooks_manager.h>
  27. #include "marker_file.h"
  28. #include "test_libraries.h"
  29. #include <boost/foreach.hpp>
  30. #include <boost/scoped_ptr.hpp>
  31. #include <iostream>
  32. #include <fstream>
  33. #include <sstream>
  34. #include <limits.h>
  35. using namespace isc;
  36. using namespace isc::asiolink;
  37. using namespace isc::config;
  38. using namespace isc::data;
  39. using namespace isc::dhcp;
  40. using namespace isc::dhcp::test;
  41. using namespace isc::hooks;
  42. using namespace std;
  43. namespace {
  44. class Dhcp4ParserTest : public ::testing::Test {
  45. public:
  46. Dhcp4ParserTest()
  47. :rcode_(-1) {
  48. // Open port 0 means to not do anything at all. We don't want to
  49. // deal with sockets here, just check if configuration handling
  50. // is sane.
  51. srv_.reset(new Dhcpv4Srv(0));
  52. CfgMgr::instance().deleteActiveIfaces();
  53. }
  54. // Check that no hooks libraries are loaded. This is a pre-condition for
  55. // a number of tests, so is checked in one place. As this uses an
  56. // ASSERT call - and it is not clear from the documentation that Gtest
  57. // predicates can be used in a constructor - the check is placed in SetUp.
  58. void SetUp() {
  59. std::vector<std::string> libraries = HooksManager::getLibraryNames();
  60. ASSERT_TRUE(libraries.empty());
  61. }
  62. // Checks if global parameter of name have expected_value
  63. void checkGlobalUint32(string name, uint32_t expected_value) {
  64. const Uint32StoragePtr uint32_defaults =
  65. globalContext()->uint32_values_;
  66. try {
  67. uint32_t actual_value = uint32_defaults->getParam(name);
  68. EXPECT_EQ(expected_value, actual_value);
  69. } catch (DhcpConfigError) {
  70. ADD_FAILURE() << "Expected uint32 with name " << name
  71. << " not found";
  72. }
  73. }
  74. // Checks if the result of DHCP server configuration has
  75. // expected code (0 for success, other for failures).
  76. // Also stores result in rcode_ and comment_.
  77. void checkResult(ConstElementPtr status, int expected_code) {
  78. ASSERT_TRUE(status);
  79. comment_ = parseAnswer(rcode_, status);
  80. EXPECT_EQ(expected_code, rcode_);
  81. }
  82. ~Dhcp4ParserTest() {
  83. resetConfiguration();
  84. // ... and delete the hooks library marker files if present
  85. unlink(LOAD_MARKER_FILE);
  86. unlink(UNLOAD_MARKER_FILE);
  87. };
  88. /// @brief Create the simple configuration with single option.
  89. ///
  90. /// This function allows to set one of the parameters that configure
  91. /// option value. These parameters are: "name", "code", "data",
  92. /// "csv-format" and "space".
  93. ///
  94. /// @param param_value string holding option parameter value to be
  95. /// injected into the configuration string.
  96. /// @param parameter name of the parameter to be configured with
  97. /// param value.
  98. /// @return configuration string containing custom values of parameters
  99. /// describing an option.
  100. std::string createConfigWithOption(const std::string& param_value,
  101. const std::string& parameter) {
  102. std::map<std::string, std::string> params;
  103. if (parameter == "name") {
  104. params["name"] = param_value;
  105. params["space"] = "dhcp4";
  106. params["code"] = "56";
  107. params["data"] = "AB CDEF0105";
  108. params["csv-format"] = "False";
  109. } else if (parameter == "space") {
  110. params["name"] = "dhcp-message";
  111. params["space"] = param_value;
  112. params["code"] = "56";
  113. params["data"] = "AB CDEF0105";
  114. params["csv-format"] = "False";
  115. } else if (parameter == "code") {
  116. params["name"] = "dhcp-message";
  117. params["space"] = "dhcp4";
  118. params["code"] = param_value;
  119. params["data"] = "AB CDEF0105";
  120. params["csv-format"] = "False";
  121. } else if (parameter == "data") {
  122. params["name"] = "dhcp-message";
  123. params["space"] = "dhcp4";
  124. params["code"] = "56";
  125. params["data"] = param_value;
  126. params["csv-format"] = "False";
  127. } else if (parameter == "csv-format") {
  128. params["name"] = "dhcp-message";
  129. params["space"] = "dhcp4";
  130. params["code"] = "56";
  131. params["data"] = "AB CDEF0105";
  132. params["csv-format"] = param_value;
  133. }
  134. return (createConfigWithOption(params));
  135. }
  136. /// @brief Create simple configuration with single option.
  137. ///
  138. /// This function creates a configuration for a single option with
  139. /// custom values for all parameters that describe the option.
  140. ///
  141. /// @params params map holding parameters and their values.
  142. /// @return configuration string containing custom values of parameters
  143. /// describing an option.
  144. std::string createConfigWithOption(const std::map<std::string, std::string>& params) {
  145. std::ostringstream stream;
  146. stream << "{ \"interfaces\": [ \"*\" ],"
  147. "\"rebind-timer\": 2000, "
  148. "\"renew-timer\": 1000, "
  149. "\"subnet4\": [ { "
  150. " \"pool\": [ \"192.0.2.1 - 192.0.2.100\" ],"
  151. " \"subnet\": \"192.0.2.0/24\", "
  152. " \"option-data\": [ {";
  153. bool first = true;
  154. typedef std::pair<std::string, std::string> ParamPair;
  155. BOOST_FOREACH(ParamPair param, params) {
  156. if (!first) {
  157. stream << ", ";
  158. } else {
  159. // cppcheck-suppress unreadVariable
  160. first = false;
  161. }
  162. if (param.first == "name") {
  163. stream << "\"name\": \"" << param.second << "\"";
  164. } else if (param.first == "space") {
  165. stream << "\"space\": \"" << param.second << "\"";
  166. } else if (param.first == "code") {
  167. stream << "\"code\": " << param.second << "";
  168. } else if (param.first == "data") {
  169. stream << "\"data\": \"" << param.second << "\"";
  170. } else if (param.first == "csv-format") {
  171. stream << "\"csv-format\": " << param.second;
  172. }
  173. }
  174. stream <<
  175. " } ]"
  176. " } ],"
  177. "\"valid-lifetime\": 4000 }";
  178. return (stream.str());
  179. }
  180. /// @brief Test invalid option parameter value.
  181. ///
  182. /// This test function constructs the simple configuration
  183. /// string and injects invalid option configuration into it.
  184. /// It expects that parser will fail with provided option code.
  185. ///
  186. /// @param param_value string holding invalid option parameter value
  187. /// to be injected into configuration string.
  188. /// @param parameter name of the parameter to be configured with
  189. /// param_value (can be any of "name", "code", "data")
  190. void testInvalidOptionParam(const std::string& param_value,
  191. const std::string& parameter) {
  192. ConstElementPtr x;
  193. std::string config = createConfigWithOption(param_value, parameter);
  194. ElementPtr json = Element::fromJSON(config);
  195. EXPECT_NO_THROW(x = configureDhcp4Server(*srv_, json));
  196. ASSERT_TRUE(x);
  197. comment_ = parseAnswer(rcode_, x);
  198. ASSERT_EQ(1, rcode_);
  199. }
  200. /// @brief Test option against given code and data.
  201. ///
  202. /// @param option_desc option descriptor that carries the option to
  203. /// be tested.
  204. /// @param expected_code expected code of the option.
  205. /// @param expected_data expected data in the option.
  206. /// @param expected_data_len length of the reference data.
  207. /// @param extra_data if true extra data is allowed in an option
  208. /// after tested data.
  209. void testOption(const Subnet::OptionDescriptor& option_desc,
  210. uint16_t expected_code, const uint8_t* expected_data,
  211. size_t expected_data_len,
  212. bool extra_data = false) {
  213. // Check if option descriptor contains valid option pointer.
  214. ASSERT_TRUE(option_desc.option);
  215. // Verify option type.
  216. EXPECT_EQ(expected_code, option_desc.option->getType());
  217. // We may have many different option types being created. Some of them
  218. // have dedicated classes derived from Option class. In such case if
  219. // we want to verify the option contents against expected_data we have
  220. // to prepare raw buffer with the contents of the option. The easiest
  221. // way is to call pack() which will prepare on-wire data.
  222. util::OutputBuffer buf(option_desc.option->getData().size());
  223. option_desc.option->pack(buf);
  224. if (extra_data) {
  225. // The length of the buffer must be at least equal to size of the
  226. // reference data but it can sometimes be greater than that. This is
  227. // because some options carry suboptions that increase the overall
  228. // length.
  229. ASSERT_GE(buf.getLength() - option_desc.option->getHeaderLen(),
  230. expected_data_len);
  231. } else {
  232. ASSERT_EQ(buf.getLength() - option_desc.option->getHeaderLen(),
  233. expected_data_len);
  234. }
  235. // Verify that the data is correct. Do not verify suboptions and a header.
  236. const uint8_t* data = static_cast<const uint8_t*>(buf.getData());
  237. EXPECT_EQ(0, memcmp(expected_data, data + option_desc.option->getHeaderLen(),
  238. expected_data_len));
  239. }
  240. /// @brief Parse and Execute configuration
  241. ///
  242. /// Parses a configuration and executes a configuration of the server.
  243. /// If the operation fails, the current test will register a failure.
  244. ///
  245. /// @param config Configuration to parse
  246. /// @param operation Operation being performed. In the case of an error,
  247. /// the error text will include the string "unable to <operation>.".
  248. ///
  249. /// @return true if the configuration succeeded, false if not. In the
  250. /// latter case, a failure will have been added to the current test.
  251. bool
  252. executeConfiguration(const std::string& config, const char* operation) {
  253. ConstElementPtr status;
  254. try {
  255. ElementPtr json = Element::fromJSON(config);
  256. status = configureDhcp4Server(*srv_, json);
  257. } catch (const std::exception& ex) {
  258. ADD_FAILURE() << "Unable to " << operation << ". "
  259. << "The following configuration was used: " << std::endl
  260. << config << std::endl
  261. << " and the following error message was returned:"
  262. << ex.what() << std::endl;
  263. return (false);
  264. }
  265. // The status object must not be NULL
  266. if (!status) {
  267. ADD_FAILURE() << "Unable to " << operation << ". "
  268. << "The configuration function returned a null pointer.";
  269. return (false);
  270. }
  271. // Store the answer if we need it.
  272. // Returned value should be 0 (configuration success)
  273. comment_ = parseAnswer(rcode_, status);
  274. if (rcode_ != 0) {
  275. string reason = "";
  276. if (comment_) {
  277. reason = string(" (") + comment_->stringValue() + string(")");
  278. }
  279. ADD_FAILURE() << "Unable to " << operation << ". "
  280. << "The configuration function returned error code "
  281. << rcode_ << reason;
  282. return (false);
  283. }
  284. return (true);
  285. }
  286. /// @brief Reset configuration database.
  287. ///
  288. /// This function resets configuration data base by
  289. /// removing all subnets and option-data. Reset must
  290. /// be performed after each test to make sure that
  291. /// contents of the database do not affect result of
  292. /// subsequent tests.
  293. void resetConfiguration() {
  294. string config = "{ \"interfaces\": [ \"*\" ],"
  295. "\"hooks-libraries\": [ ], "
  296. "\"rebind-timer\": 2000, "
  297. "\"renew-timer\": 1000, "
  298. "\"valid-lifetime\": 4000, "
  299. "\"subnet4\": [ ], "
  300. "\"option-def\": [ ], "
  301. "\"option-data\": [ ] }";
  302. static_cast<void>(executeConfiguration(config,
  303. "reset configuration database"));
  304. }
  305. boost::scoped_ptr<Dhcpv4Srv> srv_; // DHCP4 server under test
  306. int rcode_; // Return code from element parsing
  307. ConstElementPtr comment_; // Reason for parse fail
  308. };
  309. // Goal of this test is a verification if a very simple config update
  310. // with just a bumped version number. That's the simplest possible
  311. // config update.
  312. TEST_F(Dhcp4ParserTest, version) {
  313. ConstElementPtr x;
  314. EXPECT_NO_THROW(x = configureDhcp4Server(*srv_,
  315. Element::fromJSON("{\"version\": 0}")));
  316. // returned value must be 0 (configuration accepted)
  317. checkResult(x, 0);
  318. }
  319. /// The goal of this test is to verify that the code accepts only
  320. /// valid commands and malformed or unsupported parameters are rejected.
  321. TEST_F(Dhcp4ParserTest, bogusCommand) {
  322. ConstElementPtr x;
  323. EXPECT_NO_THROW(x = configureDhcp4Server(*srv_,
  324. Element::fromJSON("{\"bogus\": 5}")));
  325. // returned value must be 1 (configuration parse error)
  326. checkResult(x, 1);
  327. }
  328. /// The goal of this test is to verify if wrongly defined subnet will
  329. /// be rejected. Properly defined subnet must include at least one
  330. /// pool definition.
  331. TEST_F(Dhcp4ParserTest, emptySubnet) {
  332. ConstElementPtr status;
  333. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_,
  334. Element::fromJSON("{ \"interfaces\": [ \"*\" ],"
  335. "\"rebind-timer\": 2000, "
  336. "\"renew-timer\": 1000, "
  337. "\"subnet4\": [ ], "
  338. "\"valid-lifetime\": 4000 }")));
  339. // returned value should be 0 (success)
  340. checkResult(status, 0);
  341. checkGlobalUint32("rebind-timer", 2000);
  342. checkGlobalUint32("renew-timer", 1000);
  343. checkGlobalUint32("valid-lifetime", 4000);
  344. }
  345. /// The goal of this test is to verify if defined subnet uses global
  346. /// parameter timer definitions.
  347. TEST_F(Dhcp4ParserTest, subnetGlobalDefaults) {
  348. ConstElementPtr status;
  349. string config = "{ \"interfaces\": [ \"*\" ],"
  350. "\"rebind-timer\": 2000, "
  351. "\"renew-timer\": 1000, "
  352. "\"subnet4\": [ { "
  353. " \"pool\": [ \"192.0.2.1 - 192.0.2.100\" ],"
  354. " \"subnet\": \"192.0.2.0/24\" } ],"
  355. "\"valid-lifetime\": 4000 }";
  356. ElementPtr json = Element::fromJSON(config);
  357. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json));
  358. // check if returned status is OK
  359. checkResult(status, 0);
  360. // Now check if the configuration was indeed handled and we have
  361. // expected pool configured.
  362. Subnet4Ptr subnet = CfgMgr::instance().getSubnet4(IOAddress("192.0.2.200"));
  363. ASSERT_TRUE(subnet);
  364. EXPECT_EQ(1000, subnet->getT1());
  365. EXPECT_EQ(2000, subnet->getT2());
  366. EXPECT_EQ(4000, subnet->getValid());
  367. }
  368. // Checks if the next-server defined as global parameter is taken into
  369. // consideration.
  370. TEST_F(Dhcp4ParserTest, nextServerGlobal) {
  371. ConstElementPtr status;
  372. string config = "{ \"interfaces\": [ \"*\" ],"
  373. "\"rebind-timer\": 2000, "
  374. "\"renew-timer\": 1000, "
  375. "\"next-server\": \"1.2.3.4\", "
  376. "\"subnet4\": [ { "
  377. " \"pool\": [ \"192.0.2.1 - 192.0.2.100\" ],"
  378. " \"subnet\": \"192.0.2.0/24\" } ],"
  379. "\"valid-lifetime\": 4000 }";
  380. ElementPtr json = Element::fromJSON(config);
  381. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json));
  382. // check if returned status is OK
  383. checkResult(status, 0);
  384. // Now check if the configuration was indeed handled and we have
  385. // expected pool configured.
  386. Subnet4Ptr subnet = CfgMgr::instance().getSubnet4(IOAddress("192.0.2.200"));
  387. ASSERT_TRUE(subnet);
  388. EXPECT_EQ("1.2.3.4", subnet->getSiaddr().toText());
  389. }
  390. // Checks if the next-server defined as subnet parameter is taken into
  391. // consideration.
  392. TEST_F(Dhcp4ParserTest, nextServerSubnet) {
  393. ConstElementPtr status;
  394. string config = "{ \"interfaces\": [ \"*\" ],"
  395. "\"rebind-timer\": 2000, "
  396. "\"renew-timer\": 1000, "
  397. "\"subnet4\": [ { "
  398. " \"pool\": [ \"192.0.2.1 - 192.0.2.100\" ],"
  399. " \"next-server\": \"1.2.3.4\", "
  400. " \"subnet\": \"192.0.2.0/24\" } ],"
  401. "\"valid-lifetime\": 4000 }";
  402. ElementPtr json = Element::fromJSON(config);
  403. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json));
  404. // check if returned status is OK
  405. checkResult(status, 0);
  406. // Now check if the configuration was indeed handled and we have
  407. // expected pool configured.
  408. Subnet4Ptr subnet = CfgMgr::instance().getSubnet4(IOAddress("192.0.2.200"));
  409. ASSERT_TRUE(subnet);
  410. EXPECT_EQ("1.2.3.4", subnet->getSiaddr().toText());
  411. }
  412. // Test checks several negative scenarios for next-server configuration: bogus
  413. // address, IPv6 adddress and empty string.
  414. TEST_F(Dhcp4ParserTest, nextServerNegative) {
  415. ConstElementPtr status;
  416. // Config with junk instead of next-server address
  417. string config_bogus1 = "{ \"interfaces\": [ \"*\" ],"
  418. "\"rebind-timer\": 2000, "
  419. "\"renew-timer\": 1000, "
  420. "\"subnet4\": [ { "
  421. " \"pool\": [ \"192.0.2.1 - 192.0.2.100\" ],"
  422. " \"rebind-timer\": 2000, "
  423. " \"renew-timer\": 1000, "
  424. " \"next-server\": \"a.b.c.d\", "
  425. " \"subnet\": \"192.0.2.0/24\" } ],"
  426. "\"valid-lifetime\": 4000 }";
  427. // Config with IPv6 next server address
  428. string config_bogus2 = "{ \"interfaces\": [ \"*\" ],"
  429. "\"rebind-timer\": 2000, "
  430. "\"renew-timer\": 1000, "
  431. "\"subnet4\": [ { "
  432. " \"pool\": [ \"192.0.2.1 - 192.0.2.100\" ],"
  433. " \"rebind-timer\": 2000, "
  434. " \"renew-timer\": 1000, "
  435. " \"next-server\": \"2001:db8::1\", "
  436. " \"subnet\": \"192.0.2.0/24\" } ],"
  437. "\"valid-lifetime\": 4000 }";
  438. // Config with empty next server address
  439. string config_bogus3 = "{ \"interfaces\": [ \"*\" ],"
  440. "\"rebind-timer\": 2000, "
  441. "\"renew-timer\": 1000, "
  442. "\"subnet4\": [ { "
  443. " \"pool\": [ \"192.0.2.1 - 192.0.2.100\" ],"
  444. " \"rebind-timer\": 2000, "
  445. " \"renew-timer\": 1000, "
  446. " \"next-server\": \"\", "
  447. " \"subnet\": \"192.0.2.0/24\" } ],"
  448. "\"valid-lifetime\": 4000 }";
  449. ElementPtr json1 = Element::fromJSON(config_bogus1);
  450. ElementPtr json2 = Element::fromJSON(config_bogus2);
  451. ElementPtr json3 = Element::fromJSON(config_bogus3);
  452. // check if returned status is always a failure
  453. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json1));
  454. checkResult(status, 1);
  455. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json2));
  456. checkResult(status, 1);
  457. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json3));
  458. checkResult(status, 0);
  459. }
  460. // Checks if the next-server defined as global value is overridden by subnet
  461. // specific value.
  462. TEST_F(Dhcp4ParserTest, nextServerOverride) {
  463. ConstElementPtr status;
  464. string config = "{ \"interfaces\": [ \"*\" ],"
  465. "\"rebind-timer\": 2000, "
  466. "\"renew-timer\": 1000, "
  467. "\"next-server\": \"192.0.0.1\", "
  468. "\"subnet4\": [ { "
  469. " \"pool\": [ \"192.0.2.1 - 192.0.2.100\" ],"
  470. " \"next-server\": \"1.2.3.4\", "
  471. " \"subnet\": \"192.0.2.0/24\" } ],"
  472. "\"valid-lifetime\": 4000 }";
  473. ElementPtr json = Element::fromJSON(config);
  474. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json));
  475. // check if returned status is OK
  476. checkResult(status, 0);
  477. // Now check if the configuration was indeed handled and we have
  478. // expected pool configured.
  479. Subnet4Ptr subnet = CfgMgr::instance().getSubnet4(IOAddress("192.0.2.200"));
  480. ASSERT_TRUE(subnet);
  481. EXPECT_EQ("1.2.3.4", subnet->getSiaddr().toText());
  482. }
  483. // This test checks if it is possible to override global values
  484. // on a per subnet basis.
  485. TEST_F(Dhcp4ParserTest, subnetLocal) {
  486. ConstElementPtr status;
  487. string config = "{ \"interfaces\": [ \"*\" ],"
  488. "\"rebind-timer\": 2000, "
  489. "\"renew-timer\": 1000, "
  490. "\"subnet4\": [ { "
  491. " \"pool\": [ \"192.0.2.1 - 192.0.2.100\" ],"
  492. " \"renew-timer\": 1, "
  493. " \"rebind-timer\": 2, "
  494. " \"valid-lifetime\": 4,"
  495. " \"subnet\": \"192.0.2.0/24\" } ],"
  496. "\"valid-lifetime\": 4000 }";
  497. ElementPtr json = Element::fromJSON(config);
  498. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json));
  499. // returned value should be 0 (configuration success)
  500. checkResult(status, 0);
  501. Subnet4Ptr subnet = CfgMgr::instance().getSubnet4(IOAddress("192.0.2.200"));
  502. ASSERT_TRUE(subnet);
  503. EXPECT_EQ(1, subnet->getT1());
  504. EXPECT_EQ(2, subnet->getT2());
  505. EXPECT_EQ(4, subnet->getValid());
  506. }
  507. // Test verifies that a subnet with pool values that do not belong to that
  508. // pool are rejected.
  509. TEST_F(Dhcp4ParserTest, poolOutOfSubnet) {
  510. ConstElementPtr status;
  511. string config = "{ \"interfaces\": [ \"*\" ],"
  512. "\"rebind-timer\": 2000, "
  513. "\"renew-timer\": 1000, "
  514. "\"subnet4\": [ { "
  515. " \"pool\": [ \"192.0.4.0/28\" ],"
  516. " \"subnet\": \"192.0.2.0/24\" } ],"
  517. "\"valid-lifetime\": 4000 }";
  518. ElementPtr json = Element::fromJSON(config);
  519. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json));
  520. // returned value must be 1 (values error)
  521. // as the pool does not belong to that subnet
  522. checkResult(status, 1);
  523. }
  524. // Goal of this test is to verify if pools can be defined
  525. // using prefix/length notation. There is no separate test for min-max
  526. // notation as it was tested in several previous tests.
  527. TEST_F(Dhcp4ParserTest, poolPrefixLen) {
  528. ConstElementPtr status;
  529. string config = "{ \"interfaces\": [ \"*\" ],"
  530. "\"rebind-timer\": 2000, "
  531. "\"renew-timer\": 1000, "
  532. "\"subnet4\": [ { "
  533. " \"pool\": [ \"192.0.2.128/28\" ],"
  534. " \"subnet\": \"192.0.2.0/24\" } ],"
  535. "\"valid-lifetime\": 4000 }";
  536. ElementPtr json = Element::fromJSON(config);
  537. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json));
  538. // returned value must be 0 (configuration accepted)
  539. checkResult(status, 0);
  540. Subnet4Ptr subnet = CfgMgr::instance().getSubnet4(IOAddress("192.0.2.200"));
  541. ASSERT_TRUE(subnet);
  542. EXPECT_EQ(1000, subnet->getT1());
  543. EXPECT_EQ(2000, subnet->getT2());
  544. EXPECT_EQ(4000, subnet->getValid());
  545. }
  546. // The goal of this test is to check whether an option definition
  547. // that defines an option carrying an IPv4 address can be created.
  548. TEST_F(Dhcp4ParserTest, optionDefIpv4Address) {
  549. // Configuration string.
  550. std::string config =
  551. "{ \"option-def\": [ {"
  552. " \"name\": \"foo\","
  553. " \"code\": 100,"
  554. " \"type\": \"ipv4-address\","
  555. " \"array\": False,"
  556. " \"record-types\": \"\","
  557. " \"space\": \"isc\","
  558. " \"encapsulate\": \"\""
  559. " } ]"
  560. "}";
  561. ElementPtr json = Element::fromJSON(config);
  562. // Make sure that the particular option definition does not exist.
  563. OptionDefinitionPtr def = CfgMgr::instance().getOptionDef("isc", 100);
  564. ASSERT_FALSE(def);
  565. // Use the configuration string to create new option definition.
  566. ConstElementPtr status;
  567. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json));
  568. ASSERT_TRUE(status);
  569. checkResult(status, 0);
  570. // The option definition should now be available in the CfgMgr.
  571. def = CfgMgr::instance().getOptionDef("isc", 100);
  572. ASSERT_TRUE(def);
  573. // Verify that the option definition data is valid.
  574. EXPECT_EQ("foo", def->getName());
  575. EXPECT_EQ(100, def->getCode());
  576. EXPECT_FALSE(def->getArrayType());
  577. EXPECT_EQ(OPT_IPV4_ADDRESS_TYPE, def->getType());
  578. EXPECT_TRUE(def->getEncapsulatedSpace().empty());
  579. }
  580. // The goal of this test is to check whether an option definition
  581. // that defines an option carrying a record of data fields can
  582. // be created.
  583. TEST_F(Dhcp4ParserTest, optionDefRecord) {
  584. // Configuration string.
  585. std::string config =
  586. "{ \"option-def\": [ {"
  587. " \"name\": \"foo\","
  588. " \"code\": 100,"
  589. " \"type\": \"record\","
  590. " \"array\": False,"
  591. " \"record-types\": \"uint16, ipv4-address, ipv6-address, string\","
  592. " \"space\": \"isc\","
  593. " \"encapsulate\": \"\""
  594. " } ]"
  595. "}";
  596. ElementPtr json = Element::fromJSON(config);
  597. // Make sure that the particular option definition does not exist.
  598. OptionDefinitionPtr def = CfgMgr::instance().getOptionDef("isc", 100);
  599. ASSERT_FALSE(def);
  600. // Use the configuration string to create new option definition.
  601. ConstElementPtr status;
  602. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json));
  603. ASSERT_TRUE(status);
  604. checkResult(status, 0);
  605. // The option definition should now be available in the CfgMgr.
  606. def = CfgMgr::instance().getOptionDef("isc", 100);
  607. ASSERT_TRUE(def);
  608. // Check the option data.
  609. EXPECT_EQ("foo", def->getName());
  610. EXPECT_EQ(100, def->getCode());
  611. EXPECT_EQ(OPT_RECORD_TYPE, def->getType());
  612. EXPECT_FALSE(def->getArrayType());
  613. EXPECT_TRUE(def->getEncapsulatedSpace().empty());
  614. // The option comprises the record of data fields. Verify that all
  615. // fields are present and they are of the expected types.
  616. const OptionDefinition::RecordFieldsCollection& record_fields =
  617. def->getRecordFields();
  618. ASSERT_EQ(4, record_fields.size());
  619. EXPECT_EQ(OPT_UINT16_TYPE, record_fields[0]);
  620. EXPECT_EQ(OPT_IPV4_ADDRESS_TYPE, record_fields[1]);
  621. EXPECT_EQ(OPT_IPV6_ADDRESS_TYPE, record_fields[2]);
  622. EXPECT_EQ(OPT_STRING_TYPE, record_fields[3]);
  623. }
  624. // The goal of this test is to verify that multiple option definitions
  625. // can be created.
  626. TEST_F(Dhcp4ParserTest, optionDefMultiple) {
  627. // Configuration string.
  628. std::string config =
  629. "{ \"option-def\": [ {"
  630. " \"name\": \"foo\","
  631. " \"code\": 100,"
  632. " \"type\": \"uint32\","
  633. " \"array\": False,"
  634. " \"record-types\": \"\","
  635. " \"space\": \"isc\","
  636. " \"encapsulate\": \"\""
  637. " },"
  638. " {"
  639. " \"name\": \"foo-2\","
  640. " \"code\": 101,"
  641. " \"type\": \"ipv4-address\","
  642. " \"array\": False,"
  643. " \"record-types\": \"\","
  644. " \"space\": \"isc\","
  645. " \"encapsulate\": \"\""
  646. " } ]"
  647. "}";
  648. ElementPtr json = Element::fromJSON(config);
  649. // Make sure that the option definitions do not exist yet.
  650. ASSERT_FALSE(CfgMgr::instance().getOptionDef("isc", 100));
  651. ASSERT_FALSE(CfgMgr::instance().getOptionDef("isc", 101));
  652. // Use the configuration string to create new option definitions.
  653. ConstElementPtr status;
  654. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json));
  655. ASSERT_TRUE(status);
  656. checkResult(status, 0);
  657. // Check the first definition we have created.
  658. OptionDefinitionPtr def1 = CfgMgr::instance().getOptionDef("isc", 100);
  659. ASSERT_TRUE(def1);
  660. // Check the option data.
  661. EXPECT_EQ("foo", def1->getName());
  662. EXPECT_EQ(100, def1->getCode());
  663. EXPECT_EQ(OPT_UINT32_TYPE, def1->getType());
  664. EXPECT_FALSE(def1->getArrayType());
  665. EXPECT_TRUE(def1->getEncapsulatedSpace().empty());
  666. // Check the second option definition we have created.
  667. OptionDefinitionPtr def2 = CfgMgr::instance().getOptionDef("isc", 101);
  668. ASSERT_TRUE(def2);
  669. // Check the option data.
  670. EXPECT_EQ("foo-2", def2->getName());
  671. EXPECT_EQ(101, def2->getCode());
  672. EXPECT_EQ(OPT_IPV4_ADDRESS_TYPE, def2->getType());
  673. EXPECT_FALSE(def2->getArrayType());
  674. EXPECT_TRUE(def2->getEncapsulatedSpace().empty());
  675. }
  676. // The goal of this test is to verify that the duplicated option
  677. // definition is not accepted.
  678. TEST_F(Dhcp4ParserTest, optionDefDuplicate) {
  679. // Configuration string. Both option definitions have
  680. // the same code and belong to the same option space.
  681. // This configuration should not be accepted.
  682. std::string config =
  683. "{ \"option-def\": [ {"
  684. " \"name\": \"foo\","
  685. " \"code\": 100,"
  686. " \"type\": \"uint32\","
  687. " \"array\": False,"
  688. " \"record-types\": \"\","
  689. " \"space\": \"isc\","
  690. " \"encapsulate\": \"\""
  691. " },"
  692. " {"
  693. " \"name\": \"foo-2\","
  694. " \"code\": 100,"
  695. " \"type\": \"ipv4-address\","
  696. " \"array\": False,"
  697. " \"record-types\": \"\","
  698. " \"space\": \"isc\","
  699. " \"encapsulate\": \"\""
  700. " } ]"
  701. "}";
  702. ElementPtr json = Element::fromJSON(config);
  703. // Make sure that the option definition does not exist yet.
  704. ASSERT_FALSE(CfgMgr::instance().getOptionDef("isc", 100));
  705. // Use the configuration string to create new option definitions.
  706. ConstElementPtr status;
  707. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json));
  708. ASSERT_TRUE(status);
  709. checkResult(status, 1);
  710. }
  711. // The goal of this test is to verify that the option definition
  712. // comprising an array of uint32 values can be created.
  713. TEST_F(Dhcp4ParserTest, optionDefArray) {
  714. // Configuration string. Created option definition should
  715. // comprise an array of uint32 values.
  716. std::string config =
  717. "{ \"option-def\": [ {"
  718. " \"name\": \"foo\","
  719. " \"code\": 100,"
  720. " \"type\": \"uint32\","
  721. " \"array\": True,"
  722. " \"record-types\": \"\","
  723. " \"space\": \"isc\","
  724. " \"encapsulate\": \"\""
  725. " } ]"
  726. "}";
  727. ElementPtr json = Element::fromJSON(config);
  728. // Make sure that the particular option definition does not exist.
  729. OptionDefinitionPtr def = CfgMgr::instance().getOptionDef("isc", 100);
  730. ASSERT_FALSE(def);
  731. // Use the configuration string to create new option definition.
  732. ConstElementPtr status;
  733. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json));
  734. ASSERT_TRUE(status);
  735. checkResult(status, 0);
  736. // The option definition should now be available in the CfgMgr.
  737. def = CfgMgr::instance().getOptionDef("isc", 100);
  738. ASSERT_TRUE(def);
  739. // Check the option data.
  740. EXPECT_EQ("foo", def->getName());
  741. EXPECT_EQ(100, def->getCode());
  742. EXPECT_EQ(OPT_UINT32_TYPE, def->getType());
  743. EXPECT_TRUE(def->getArrayType());
  744. EXPECT_TRUE(def->getEncapsulatedSpace().empty());
  745. }
  746. // The purpose of this test to verify that encapsulated option
  747. // space name may be specified.
  748. TEST_F(Dhcp4ParserTest, optionDefEncapsulate) {
  749. // Configuration string. Included the encapsulated
  750. // option space name.
  751. std::string config =
  752. "{ \"option-def\": [ {"
  753. " \"name\": \"foo\","
  754. " \"code\": 100,"
  755. " \"type\": \"uint32\","
  756. " \"array\": False,"
  757. " \"record-types\": \"\","
  758. " \"space\": \"isc\","
  759. " \"encapsulate\": \"sub-opts-space\""
  760. " } ]"
  761. "}";
  762. ElementPtr json = Element::fromJSON(config);
  763. // Make sure that the particular option definition does not exist.
  764. OptionDefinitionPtr def = CfgMgr::instance().getOptionDef("isc", 100);
  765. ASSERT_FALSE(def);
  766. // Use the configuration string to create new option definition.
  767. ConstElementPtr status;
  768. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json));
  769. ASSERT_TRUE(status);
  770. checkResult(status, 0);
  771. // The option definition should now be available in the CfgMgr.
  772. def = CfgMgr::instance().getOptionDef("isc", 100);
  773. ASSERT_TRUE(def);
  774. // Check the option data.
  775. EXPECT_EQ("foo", def->getName());
  776. EXPECT_EQ(100, def->getCode());
  777. EXPECT_EQ(OPT_UINT32_TYPE, def->getType());
  778. EXPECT_FALSE(def->getArrayType());
  779. EXPECT_EQ("sub-opts-space", def->getEncapsulatedSpace());
  780. }
  781. /// The purpose of this test is to verify that the option definition
  782. /// with invalid name is not accepted.
  783. TEST_F(Dhcp4ParserTest, optionDefInvalidName) {
  784. // Configuration string. The option name is invalid as it
  785. // contains the % character.
  786. std::string config =
  787. "{ \"option-def\": [ {"
  788. " \"name\": \"invalid%name\","
  789. " \"code\": 100,"
  790. " \"type\": \"string\","
  791. " \"array\": False,"
  792. " \"record-types\": \"\","
  793. " \"space\": \"isc\","
  794. " \"encapsulate\": \"\""
  795. " } ]"
  796. "}";
  797. ElementPtr json = Element::fromJSON(config);
  798. // Use the configuration string to create new option definition.
  799. ConstElementPtr status;
  800. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json));
  801. ASSERT_TRUE(status);
  802. // Expecting parsing error (error code 1).
  803. checkResult(status, 1);
  804. }
  805. /// The purpose of this test is to verify that the option definition
  806. /// with invalid type is not accepted.
  807. TEST_F(Dhcp4ParserTest, optionDefInvalidType) {
  808. // Configuration string. The option type is invalid. It is
  809. // "sting" instead of "string".
  810. std::string config =
  811. "{ \"option-def\": [ {"
  812. " \"name\": \"foo\","
  813. " \"code\": 100,"
  814. " \"type\": \"sting\","
  815. " \"array\": False,"
  816. " \"record-types\": \"\","
  817. " \"space\": \"isc\","
  818. " \"encapsulate\": \"\""
  819. " } ]"
  820. "}";
  821. ElementPtr json = Element::fromJSON(config);
  822. // Use the configuration string to create new option definition.
  823. ConstElementPtr status;
  824. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json));
  825. ASSERT_TRUE(status);
  826. // Expecting parsing error (error code 1).
  827. checkResult(status, 1);
  828. }
  829. /// The purpose of this test is to verify that the option definition
  830. /// with invalid type is not accepted.
  831. TEST_F(Dhcp4ParserTest, optionDefInvalidRecordType) {
  832. // Configuration string. The third of the record fields
  833. // is invalid. It is "sting" instead of "string".
  834. std::string config =
  835. "{ \"option-def\": [ {"
  836. " \"name\": \"foo\","
  837. " \"code\": 100,"
  838. " \"type\": \"record\","
  839. " \"array\": False,"
  840. " \"record-types\": \"uint32,uint8,sting\","
  841. " \"space\": \"isc\","
  842. " \"encapsulate\": \"\""
  843. " } ]"
  844. "}";
  845. ElementPtr json = Element::fromJSON(config);
  846. // Use the configuration string to create new option definition.
  847. ConstElementPtr status;
  848. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json));
  849. ASSERT_TRUE(status);
  850. // Expecting parsing error (error code 1).
  851. checkResult(status, 1);
  852. }
  853. /// The goal of this test is to verify that the invalid encapsulated
  854. /// option space name is not accepted.
  855. TEST_F(Dhcp4ParserTest, optionDefInvalidEncapsulatedSpace) {
  856. // Configuration string. The encapsulated option space
  857. // name is invalid (% character is not allowed).
  858. std::string config =
  859. "{ \"option-def\": [ {"
  860. " \"name\": \"foo\","
  861. " \"code\": 100,"
  862. " \"type\": \"uint32\","
  863. " \"array\": False,"
  864. " \"record-types\": \"\","
  865. " \"space\": \"isc\","
  866. " \"encapsulate\": \"invalid%space%name\""
  867. " } ]"
  868. "}";
  869. ElementPtr json = Element::fromJSON(config);
  870. // Use the configuration string to create new option definition.
  871. ConstElementPtr status;
  872. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json));
  873. ASSERT_TRUE(status);
  874. // Expecting parsing error (error code 1).
  875. checkResult(status, 1);
  876. }
  877. /// The goal of this test is to verify that the encapsulated
  878. /// option space name can't be specified for the option that
  879. /// comprises an array of data fields.
  880. TEST_F(Dhcp4ParserTest, optionDefEncapsulatedSpaceAndArray) {
  881. // Configuration string. The encapsulated option space
  882. // name is set to non-empty value and the array flag
  883. // is set.
  884. std::string config =
  885. "{ \"option-def\": [ {"
  886. " \"name\": \"foo\","
  887. " \"code\": 100,"
  888. " \"type\": \"uint32\","
  889. " \"array\": True,"
  890. " \"record-types\": \"\","
  891. " \"space\": \"isc\","
  892. " \"encapsulate\": \"valid-space-name\""
  893. " } ]"
  894. "}";
  895. ElementPtr json = Element::fromJSON(config);
  896. // Use the configuration string to create new option definition.
  897. ConstElementPtr status;
  898. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json));
  899. ASSERT_TRUE(status);
  900. // Expecting parsing error (error code 1).
  901. checkResult(status, 1);
  902. }
  903. /// The goal of this test is to verify that the option may not
  904. /// encapsulate option space it belongs to.
  905. TEST_F(Dhcp4ParserTest, optionDefEncapsulateOwnSpace) {
  906. // Configuration string. Option is set to encapsulate
  907. // option space it belongs to.
  908. std::string config =
  909. "{ \"option-def\": [ {"
  910. " \"name\": \"foo\","
  911. " \"code\": 100,"
  912. " \"type\": \"uint32\","
  913. " \"array\": False,"
  914. " \"record-types\": \"\","
  915. " \"space\": \"isc\","
  916. " \"encapsulate\": \"isc\""
  917. " } ]"
  918. "}";
  919. ElementPtr json = Element::fromJSON(config);
  920. // Use the configuration string to create new option definition.
  921. ConstElementPtr status;
  922. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json));
  923. ASSERT_TRUE(status);
  924. // Expecting parsing error (error code 1).
  925. checkResult(status, 1);
  926. }
  927. /// The purpose of this test is to verify that it is not allowed
  928. /// to override the standard option (that belongs to dhcp4 option
  929. /// space) and that it is allowed to define option in the dhcp4
  930. /// option space that has a code which is not used by any of the
  931. /// standard options.
  932. TEST_F(Dhcp4ParserTest, optionStandardDefOverride) {
  933. // Configuration string. The option code 109 is unassigned
  934. // so it can be used for a custom option definition in
  935. // dhcp4 option space.
  936. std::string config =
  937. "{ \"option-def\": [ {"
  938. " \"name\": \"foo\","
  939. " \"code\": 109,"
  940. " \"type\": \"string\","
  941. " \"array\": False,"
  942. " \"record-types\": \"\","
  943. " \"space\": \"dhcp4\","
  944. " \"encapsulate\": \"\""
  945. " } ]"
  946. "}";
  947. ElementPtr json = Element::fromJSON(config);
  948. OptionDefinitionPtr def = CfgMgr::instance().getOptionDef("dhcp4", 109);
  949. ASSERT_FALSE(def);
  950. // Use the configuration string to create new option definition.
  951. ConstElementPtr status;
  952. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json));
  953. ASSERT_TRUE(status);
  954. checkResult(status, 0);
  955. // The option definition should now be available in the CfgMgr.
  956. def = CfgMgr::instance().getOptionDef("dhcp4", 109);
  957. ASSERT_TRUE(def);
  958. // Check the option data.
  959. EXPECT_EQ("foo", def->getName());
  960. EXPECT_EQ(109, def->getCode());
  961. EXPECT_EQ(OPT_STRING_TYPE, def->getType());
  962. EXPECT_FALSE(def->getArrayType());
  963. // The combination of option space and code is
  964. // invalid. The 'dhcp4' option space groups
  965. // standard options and the code 100 is reserved
  966. // for one of them.
  967. config =
  968. "{ \"option-def\": [ {"
  969. " \"name\": \"foo\","
  970. " \"code\": 100,"
  971. " \"type\": \"string\","
  972. " \"array\": False,"
  973. " \"record-types\": \"\","
  974. " \"space\": \"dhcp4\","
  975. " \"encapsulate\": \"\""
  976. " } ]"
  977. "}";
  978. json = Element::fromJSON(config);
  979. // Use the configuration string to create new option definition.
  980. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json));
  981. ASSERT_TRUE(status);
  982. // Expecting parsing error (error code 1).
  983. checkResult(status, 1);
  984. }
  985. // Goal of this test is to verify that global option
  986. // data is configured for the subnet if the subnet
  987. // configuration does not include options configuration.
  988. TEST_F(Dhcp4ParserTest, optionDataDefaults) {
  989. ConstElementPtr x;
  990. string config = "{ \"interfaces\": [ \"*\" ],"
  991. "\"rebind-timer\": 2000,"
  992. "\"renew-timer\": 1000,"
  993. "\"option-data\": [ {"
  994. " \"name\": \"dhcp-message\","
  995. " \"space\": \"dhcp4\","
  996. " \"code\": 56,"
  997. " \"data\": \"AB CDEF0105\","
  998. " \"csv-format\": False"
  999. " },"
  1000. " {"
  1001. " \"name\": \"default-ip-ttl\","
  1002. " \"space\": \"dhcp4\","
  1003. " \"code\": 23,"
  1004. " \"data\": \"01\","
  1005. " \"csv-format\": False"
  1006. " } ],"
  1007. "\"subnet4\": [ { "
  1008. " \"pool\": [ \"192.0.2.1 - 192.0.2.100\" ],"
  1009. " \"subnet\": \"192.0.2.0/24\""
  1010. " } ],"
  1011. "\"valid-lifetime\": 4000 }";
  1012. ElementPtr json = Element::fromJSON(config);
  1013. EXPECT_NO_THROW(x = configureDhcp4Server(*srv_, json));
  1014. ASSERT_TRUE(x);
  1015. comment_ = parseAnswer(rcode_, x);
  1016. ASSERT_EQ(0, rcode_);
  1017. Subnet4Ptr subnet = CfgMgr::instance().getSubnet4(IOAddress("192.0.2.200"));
  1018. ASSERT_TRUE(subnet);
  1019. Subnet::OptionContainerPtr options = subnet->getOptionDescriptors("dhcp4");
  1020. ASSERT_EQ(2, options->size());
  1021. // Get the search index. Index #1 is to search using option code.
  1022. const Subnet::OptionContainerTypeIndex& idx = options->get<1>();
  1023. // Get the options for specified index. Expecting one option to be
  1024. // returned but in theory we may have multiple options with the same
  1025. // code so we get the range.
  1026. std::pair<Subnet::OptionContainerTypeIndex::const_iterator,
  1027. Subnet::OptionContainerTypeIndex::const_iterator> range =
  1028. idx.equal_range(56);
  1029. // Expect single option with the code equal to 56.
  1030. ASSERT_EQ(1, std::distance(range.first, range.second));
  1031. const uint8_t foo_expected[] = {
  1032. 0xAB, 0xCD, 0xEF, 0x01, 0x05
  1033. };
  1034. // Check if option is valid in terms of code and carried data.
  1035. testOption(*range.first, 56, foo_expected, sizeof(foo_expected));
  1036. range = idx.equal_range(23);
  1037. ASSERT_EQ(1, std::distance(range.first, range.second));
  1038. // Do another round of testing with second option.
  1039. const uint8_t foo2_expected[] = {
  1040. 0x01
  1041. };
  1042. testOption(*range.first, 23, foo2_expected, sizeof(foo2_expected));
  1043. }
  1044. /// The goal of this test is to verify that two options having the same
  1045. /// option code can be added to different option spaces.
  1046. TEST_F(Dhcp4ParserTest, optionDataTwoSpaces) {
  1047. // This configuration string is to configure two options
  1048. // sharing the code 56 and having different definitions
  1049. // and belonging to the different option spaces.
  1050. // The option definition must be provided for the
  1051. // option that belongs to the 'isc' option space.
  1052. // The definition is not required for the option that
  1053. // belongs to the 'dhcp4' option space as it is the
  1054. // standard option.
  1055. string config = "{ \"interfaces\": [ \"*\" ],"
  1056. "\"rebind-timer\": 2000,"
  1057. "\"renew-timer\": 1000,"
  1058. "\"option-data\": [ {"
  1059. " \"name\": \"dhcp-message\","
  1060. " \"space\": \"dhcp4\","
  1061. " \"code\": 56,"
  1062. " \"data\": \"AB CDEF0105\","
  1063. " \"csv-format\": False"
  1064. " },"
  1065. " {"
  1066. " \"name\": \"foo\","
  1067. " \"space\": \"isc\","
  1068. " \"code\": 56,"
  1069. " \"data\": \"1234\","
  1070. " \"csv-format\": True"
  1071. " } ],"
  1072. "\"option-def\": [ {"
  1073. " \"name\": \"foo\","
  1074. " \"code\": 56,"
  1075. " \"type\": \"uint32\","
  1076. " \"array\": False,"
  1077. " \"record-types\": \"\","
  1078. " \"space\": \"isc\","
  1079. " \"encapsulate\": \"\""
  1080. " } ],"
  1081. "\"subnet4\": [ { "
  1082. " \"pool\": [ \"192.0.2.1 - 192.0.2.100\" ],"
  1083. " \"subnet\": \"192.0.2.0/24\""
  1084. " } ]"
  1085. "}";
  1086. ConstElementPtr status;
  1087. ElementPtr json = Element::fromJSON(config);
  1088. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json));
  1089. ASSERT_TRUE(status);
  1090. checkResult(status, 0);
  1091. // Options should be now available for the subnet.
  1092. Subnet4Ptr subnet = CfgMgr::instance().getSubnet4(IOAddress("192.0.2.200"));
  1093. ASSERT_TRUE(subnet);
  1094. // Try to get the option from the space dhcp4.
  1095. Subnet::OptionDescriptor desc1 = subnet->getOptionDescriptor("dhcp4", 56);
  1096. ASSERT_TRUE(desc1.option);
  1097. EXPECT_EQ(56, desc1.option->getType());
  1098. // Try to get the option from the space isc.
  1099. Subnet::OptionDescriptor desc2 = subnet->getOptionDescriptor("isc", 56);
  1100. ASSERT_TRUE(desc2.option);
  1101. EXPECT_EQ(56, desc1.option->getType());
  1102. // Try to get the non-existing option from the non-existing
  1103. // option space and expect that option is not returned.
  1104. Subnet::OptionDescriptor desc3 = subnet->getOptionDescriptor("non-existing", 56);
  1105. ASSERT_FALSE(desc3.option);
  1106. }
  1107. // The goal of this test is to verify that it is possible to
  1108. // encapsulate option space containing some options with
  1109. // another option. In this test we create base option that
  1110. // encapsulates option space 'isc' that comprises two other
  1111. // options. Also, for all options their definitions are
  1112. // created.
  1113. TEST_F(Dhcp4ParserTest, optionDataEncapsulate) {
  1114. // @todo DHCP configurations has many dependencies between
  1115. // parameters. First of all, configuration for subnet is
  1116. // inherited from the global values. Thus subnet has to be
  1117. // configured when all global values have been configured.
  1118. // Also, an option can encapsulate another option only
  1119. // if the latter has been configured. For this reason in this
  1120. // test we created two-stage configuration where first we
  1121. // created options that belong to encapsulated option space.
  1122. // In the second stage we add the base option. Also, the Subnet
  1123. // object is configured in the second stage so it is created
  1124. // at the very end (when all other parameters are configured).
  1125. // Starting stage 1. Configure sub-options and their definitions.
  1126. string config = "{ \"interfaces\": [ \"*\" ],"
  1127. "\"rebind-timer\": 2000,"
  1128. "\"renew-timer\": 1000,"
  1129. "\"option-data\": [ {"
  1130. " \"name\": \"foo\","
  1131. " \"space\": \"isc\","
  1132. " \"code\": 1,"
  1133. " \"data\": \"1234\","
  1134. " \"csv-format\": True"
  1135. " },"
  1136. " {"
  1137. " \"name\": \"foo2\","
  1138. " \"space\": \"isc\","
  1139. " \"code\": 2,"
  1140. " \"data\": \"192.168.2.1\","
  1141. " \"csv-format\": True"
  1142. " } ],"
  1143. "\"option-def\": [ {"
  1144. " \"name\": \"foo\","
  1145. " \"code\": 1,"
  1146. " \"type\": \"uint32\","
  1147. " \"array\": False,"
  1148. " \"record-types\": \"\","
  1149. " \"space\": \"isc\","
  1150. " \"encapsulate\": \"\""
  1151. " },"
  1152. " {"
  1153. " \"name\": \"foo2\","
  1154. " \"code\": 2,"
  1155. " \"type\": \"ipv4-address\","
  1156. " \"array\": False,"
  1157. " \"record-types\": \"\","
  1158. " \"space\": \"isc\","
  1159. " \"encapsulate\": \"\""
  1160. " } ]"
  1161. "}";
  1162. ConstElementPtr status;
  1163. ElementPtr json = Element::fromJSON(config);
  1164. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json));
  1165. ASSERT_TRUE(status);
  1166. checkResult(status, 0);
  1167. // Stage 2. Configure base option and a subnet. Please note that
  1168. // the configuration from the stage 2 is repeated because BIND
  1169. // configuration manager sends whole configuration for the lists
  1170. // where at least one element is being modified or added.
  1171. config = "{ \"interfaces\": [ \"*\" ],"
  1172. "\"rebind-timer\": 2000,"
  1173. "\"renew-timer\": 1000,"
  1174. "\"option-data\": [ {"
  1175. " \"name\": \"base-option\","
  1176. " \"space\": \"dhcp4\","
  1177. " \"code\": 222,"
  1178. " \"data\": \"11\","
  1179. " \"csv-format\": True"
  1180. " },"
  1181. " {"
  1182. " \"name\": \"foo\","
  1183. " \"space\": \"isc\","
  1184. " \"code\": 1,"
  1185. " \"data\": \"1234\","
  1186. " \"csv-format\": True"
  1187. " },"
  1188. " {"
  1189. " \"name\": \"foo2\","
  1190. " \"space\": \"isc\","
  1191. " \"code\": 2,"
  1192. " \"data\": \"192.168.2.1\","
  1193. " \"csv-format\": True"
  1194. " } ],"
  1195. "\"option-def\": [ {"
  1196. " \"name\": \"base-option\","
  1197. " \"code\": 222,"
  1198. " \"type\": \"uint8\","
  1199. " \"array\": False,"
  1200. " \"record-types\": \"\","
  1201. " \"space\": \"dhcp4\","
  1202. " \"encapsulate\": \"isc\""
  1203. "},"
  1204. "{"
  1205. " \"name\": \"foo\","
  1206. " \"code\": 1,"
  1207. " \"type\": \"uint32\","
  1208. " \"array\": False,"
  1209. " \"record-types\": \"\","
  1210. " \"space\": \"isc\","
  1211. " \"encapsulate\": \"\""
  1212. " },"
  1213. " {"
  1214. " \"name\": \"foo2\","
  1215. " \"code\": 2,"
  1216. " \"type\": \"ipv4-address\","
  1217. " \"array\": False,"
  1218. " \"record-types\": \"\","
  1219. " \"space\": \"isc\","
  1220. " \"encapsulate\": \"\""
  1221. " } ],"
  1222. "\"subnet4\": [ { "
  1223. " \"pool\": [ \"192.0.2.1 - 192.0.2.100\" ],"
  1224. " \"subnet\": \"192.0.2.0/24\""
  1225. " } ]"
  1226. "}";
  1227. json = Element::fromJSON(config);
  1228. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json));
  1229. ASSERT_TRUE(status);
  1230. checkResult(status, 0);
  1231. // Get the subnet.
  1232. Subnet4Ptr subnet = CfgMgr::instance().getSubnet4(IOAddress("192.0.2.5"));
  1233. ASSERT_TRUE(subnet);
  1234. // We should have one option available.
  1235. Subnet::OptionContainerPtr options = subnet->getOptionDescriptors("dhcp4");
  1236. ASSERT_TRUE(options);
  1237. ASSERT_EQ(1, options->size());
  1238. // Get the option.
  1239. Subnet::OptionDescriptor desc = subnet->getOptionDescriptor("dhcp4", 222);
  1240. EXPECT_TRUE(desc.option);
  1241. EXPECT_EQ(222, desc.option->getType());
  1242. // This opton should comprise two sub-options.
  1243. // One of them is 'foo' with code 1.
  1244. OptionPtr option_foo = desc.option->getOption(1);
  1245. ASSERT_TRUE(option_foo);
  1246. EXPECT_EQ(1, option_foo->getType());
  1247. // ...another one 'foo2' with code 2.
  1248. OptionPtr option_foo2 = desc.option->getOption(2);
  1249. ASSERT_TRUE(option_foo2);
  1250. EXPECT_EQ(2, option_foo2->getType());
  1251. }
  1252. // Goal of this test is to verify options configuration
  1253. // for a single subnet. In particular this test checks
  1254. // that local options configuration overrides global
  1255. // option setting.
  1256. TEST_F(Dhcp4ParserTest, optionDataInSingleSubnet) {
  1257. ConstElementPtr x;
  1258. string config = "{ \"interfaces\": [ \"*\" ],"
  1259. "\"rebind-timer\": 2000, "
  1260. "\"renew-timer\": 1000, "
  1261. "\"option-data\": [ {"
  1262. " \"name\": \"dhcp-message\","
  1263. " \"space\": \"dhcp4\","
  1264. " \"code\": 56,"
  1265. " \"data\": \"AB\","
  1266. " \"csv-format\": False"
  1267. " } ],"
  1268. "\"subnet4\": [ { "
  1269. " \"pool\": [ \"192.0.2.1 - 192.0.2.100\" ],"
  1270. " \"subnet\": \"192.0.2.0/24\", "
  1271. " \"option-data\": [ {"
  1272. " \"name\": \"dhcp-message\","
  1273. " \"space\": \"dhcp4\","
  1274. " \"code\": 56,"
  1275. " \"data\": \"AB CDEF0105\","
  1276. " \"csv-format\": False"
  1277. " },"
  1278. " {"
  1279. " \"name\": \"default-ip-ttl\","
  1280. " \"space\": \"dhcp4\","
  1281. " \"code\": 23,"
  1282. " \"data\": \"01\","
  1283. " \"csv-format\": False"
  1284. " } ]"
  1285. " } ],"
  1286. "\"valid-lifetime\": 4000 }";
  1287. ElementPtr json = Element::fromJSON(config);
  1288. EXPECT_NO_THROW(x = configureDhcp4Server(*srv_, json));
  1289. ASSERT_TRUE(x);
  1290. comment_ = parseAnswer(rcode_, x);
  1291. ASSERT_EQ(0, rcode_);
  1292. Subnet4Ptr subnet = CfgMgr::instance().getSubnet4(IOAddress("192.0.2.24"));
  1293. ASSERT_TRUE(subnet);
  1294. Subnet::OptionContainerPtr options = subnet->getOptionDescriptors("dhcp4");
  1295. ASSERT_EQ(2, options->size());
  1296. // Get the search index. Index #1 is to search using option code.
  1297. const Subnet::OptionContainerTypeIndex& idx = options->get<1>();
  1298. // Get the options for specified index. Expecting one option to be
  1299. // returned but in theory we may have multiple options with the same
  1300. // code so we get the range.
  1301. std::pair<Subnet::OptionContainerTypeIndex::const_iterator,
  1302. Subnet::OptionContainerTypeIndex::const_iterator> range =
  1303. idx.equal_range(56);
  1304. // Expect single option with the code equal to 100.
  1305. ASSERT_EQ(1, std::distance(range.first, range.second));
  1306. const uint8_t foo_expected[] = {
  1307. 0xAB, 0xCD, 0xEF, 0x01, 0x05
  1308. };
  1309. // Check if option is valid in terms of code and carried data.
  1310. testOption(*range.first, 56, foo_expected, sizeof(foo_expected));
  1311. range = idx.equal_range(23);
  1312. ASSERT_EQ(1, std::distance(range.first, range.second));
  1313. // Do another round of testing with second option.
  1314. const uint8_t foo2_expected[] = {
  1315. 0x01
  1316. };
  1317. testOption(*range.first, 23, foo2_expected, sizeof(foo2_expected));
  1318. }
  1319. // Goal of this test is to verify options configuration
  1320. // for multiple subnets.
  1321. TEST_F(Dhcp4ParserTest, optionDataInMultipleSubnets) {
  1322. ConstElementPtr x;
  1323. string config = "{ \"interfaces\": [ \"*\" ],"
  1324. "\"rebind-timer\": 2000, "
  1325. "\"renew-timer\": 1000, "
  1326. "\"subnet4\": [ { "
  1327. " \"pool\": [ \"192.0.2.1 - 192.0.2.100\" ],"
  1328. " \"subnet\": \"192.0.2.0/24\", "
  1329. " \"option-data\": [ {"
  1330. " \"name\": \"dhcp-message\","
  1331. " \"space\": \"dhcp4\","
  1332. " \"code\": 56,"
  1333. " \"data\": \"0102030405060708090A\","
  1334. " \"csv-format\": False"
  1335. " } ]"
  1336. " },"
  1337. " {"
  1338. " \"pool\": [ \"192.0.3.101 - 192.0.3.150\" ],"
  1339. " \"subnet\": \"192.0.3.0/24\", "
  1340. " \"option-data\": [ {"
  1341. " \"name\": \"default-ip-ttl\","
  1342. " \"space\": \"dhcp4\","
  1343. " \"code\": 23,"
  1344. " \"data\": \"FF\","
  1345. " \"csv-format\": False"
  1346. " } ]"
  1347. " } ],"
  1348. "\"valid-lifetime\": 4000 }";
  1349. ElementPtr json = Element::fromJSON(config);
  1350. EXPECT_NO_THROW(x = configureDhcp4Server(*srv_, json));
  1351. ASSERT_TRUE(x);
  1352. comment_ = parseAnswer(rcode_, x);
  1353. ASSERT_EQ(0, rcode_);
  1354. Subnet4Ptr subnet1 = CfgMgr::instance().getSubnet4(IOAddress("192.0.2.100"));
  1355. ASSERT_TRUE(subnet1);
  1356. Subnet::OptionContainerPtr options1 = subnet1->getOptionDescriptors("dhcp4");
  1357. ASSERT_EQ(1, options1->size());
  1358. // Get the search index. Index #1 is to search using option code.
  1359. const Subnet::OptionContainerTypeIndex& idx1 = options1->get<1>();
  1360. // Get the options for specified index. Expecting one option to be
  1361. // returned but in theory we may have multiple options with the same
  1362. // code so we get the range.
  1363. std::pair<Subnet::OptionContainerTypeIndex::const_iterator,
  1364. Subnet::OptionContainerTypeIndex::const_iterator> range1 =
  1365. idx1.equal_range(56);
  1366. // Expect single option with the code equal to 56.
  1367. ASSERT_EQ(1, std::distance(range1.first, range1.second));
  1368. const uint8_t foo_expected[] = {
  1369. 0x01, 0x02, 0x03, 0x04, 0x05,
  1370. 0x06, 0x07, 0x08, 0x09, 0x0A
  1371. };
  1372. // Check if option is valid in terms of code and carried data.
  1373. testOption(*range1.first, 56, foo_expected, sizeof(foo_expected));
  1374. // Test another subnet in the same way.
  1375. Subnet4Ptr subnet2 = CfgMgr::instance().getSubnet4(IOAddress("192.0.3.102"));
  1376. ASSERT_TRUE(subnet2);
  1377. Subnet::OptionContainerPtr options2 = subnet2->getOptionDescriptors("dhcp4");
  1378. ASSERT_EQ(1, options2->size());
  1379. const Subnet::OptionContainerTypeIndex& idx2 = options2->get<1>();
  1380. std::pair<Subnet::OptionContainerTypeIndex::const_iterator,
  1381. Subnet::OptionContainerTypeIndex::const_iterator> range2 =
  1382. idx2.equal_range(23);
  1383. ASSERT_EQ(1, std::distance(range2.first, range2.second));
  1384. const uint8_t foo2_expected[] = { 0xFF };
  1385. testOption(*range2.first, 23, foo2_expected, sizeof(foo2_expected));
  1386. }
  1387. // Verify that empty option name is rejected in the configuration.
  1388. TEST_F(Dhcp4ParserTest, optionNameEmpty) {
  1389. // Empty option names not allowed.
  1390. testInvalidOptionParam("", "name");
  1391. }
  1392. // Verify that empty option name with spaces is rejected
  1393. // in the configuration.
  1394. TEST_F(Dhcp4ParserTest, optionNameSpaces) {
  1395. // Spaces in option names not allowed.
  1396. testInvalidOptionParam("option foo", "name");
  1397. }
  1398. // Verify that negative option code is rejected in the configuration.
  1399. TEST_F(Dhcp4ParserTest, optionCodeNegative) {
  1400. // Check negative option code -4. This should fail too.
  1401. testInvalidOptionParam("-4", "code");
  1402. }
  1403. // Verify that out of bounds option code is rejected in the configuration.
  1404. TEST_F(Dhcp4ParserTest, optionCodeNonUint8) {
  1405. // The valid option codes are uint16_t values so passing
  1406. // uint16_t maximum value incremented by 1 should result
  1407. // in failure.
  1408. testInvalidOptionParam("257", "code");
  1409. }
  1410. // Verify that zero option code is rejected in the configuration.
  1411. TEST_F(Dhcp4ParserTest, optionCodeZero) {
  1412. // Option code 0 is reserved and should not be accepted
  1413. // by configuration parser.
  1414. testInvalidOptionParam("0", "code");
  1415. }
  1416. // Verify that option data which contains non hexadecimal characters
  1417. // is rejected by the configuration.
  1418. TEST_F(Dhcp4ParserTest, optionDataInvalidChar) {
  1419. // Option code 0 is reserved and should not be accepted
  1420. // by configuration parser.
  1421. testInvalidOptionParam("01020R", "data");
  1422. }
  1423. // Verify that option data containing '0x' prefix is rejected
  1424. // by the configuration.
  1425. TEST_F(Dhcp4ParserTest, optionDataUnexpectedPrefix) {
  1426. // Option code 0 is reserved and should not be accepted
  1427. // by configuration parser.
  1428. testInvalidOptionParam("0x0102", "data");
  1429. }
  1430. // Verify that option data consisting od an odd number of
  1431. // hexadecimal digits is rejected in the configuration.
  1432. TEST_F(Dhcp4ParserTest, optionDataOddLength) {
  1433. // Option code 0 is reserved and should not be accepted
  1434. // by configuration parser.
  1435. testInvalidOptionParam("123", "data");
  1436. }
  1437. // Verify that either lower or upper case characters are allowed
  1438. // to specify the option data.
  1439. TEST_F(Dhcp4ParserTest, optionDataLowerCase) {
  1440. ConstElementPtr x;
  1441. std::string config = createConfigWithOption("0a0b0C0D", "data");
  1442. ElementPtr json = Element::fromJSON(config);
  1443. EXPECT_NO_THROW(x = configureDhcp4Server(*srv_, json));
  1444. ASSERT_TRUE(x);
  1445. comment_ = parseAnswer(rcode_, x);
  1446. ASSERT_EQ(0, rcode_);
  1447. Subnet4Ptr subnet = CfgMgr::instance().getSubnet4(IOAddress("192.0.2.5"));
  1448. ASSERT_TRUE(subnet);
  1449. Subnet::OptionContainerPtr options = subnet->getOptionDescriptors("dhcp4");
  1450. ASSERT_EQ(1, options->size());
  1451. // Get the search index. Index #1 is to search using option code.
  1452. const Subnet::OptionContainerTypeIndex& idx = options->get<1>();
  1453. // Get the options for specified index. Expecting one option to be
  1454. // returned but in theory we may have multiple options with the same
  1455. // code so we get the range.
  1456. std::pair<Subnet::OptionContainerTypeIndex::const_iterator,
  1457. Subnet::OptionContainerTypeIndex::const_iterator> range =
  1458. idx.equal_range(56);
  1459. // Expect single option with the code equal to 100.
  1460. ASSERT_EQ(1, std::distance(range.first, range.second));
  1461. const uint8_t foo_expected[] = {
  1462. 0x0A, 0x0B, 0x0C, 0x0D
  1463. };
  1464. // Check if option is valid in terms of code and carried data.
  1465. testOption(*range.first, 56, foo_expected, sizeof(foo_expected));
  1466. }
  1467. // Verify that specific option object is returned for standard
  1468. // option which has dedicated option class derived from Option.
  1469. TEST_F(Dhcp4ParserTest, stdOptionData) {
  1470. ConstElementPtr x;
  1471. std::map<std::string, std::string> params;
  1472. params["name"] = "nis-servers";
  1473. params["space"] = "dhcp4";
  1474. // Option code 41 means nis-servers.
  1475. params["code"] = "41";
  1476. // Specify option values in a CSV (user friendly) format.
  1477. params["data"] = "192.0.2.10, 192.0.2.1, 192.0.2.3";
  1478. params["csv-format"] = "True";
  1479. std::string config = createConfigWithOption(params);
  1480. ElementPtr json = Element::fromJSON(config);
  1481. EXPECT_NO_THROW(x = configureDhcp4Server(*srv_, json));
  1482. ASSERT_TRUE(x);
  1483. comment_ = parseAnswer(rcode_, x);
  1484. ASSERT_EQ(0, rcode_);
  1485. Subnet4Ptr subnet = CfgMgr::instance().getSubnet4(IOAddress("192.0.2.5"));
  1486. ASSERT_TRUE(subnet);
  1487. Subnet::OptionContainerPtr options =
  1488. subnet->getOptionDescriptors("dhcp4");
  1489. ASSERT_TRUE(options);
  1490. ASSERT_EQ(1, options->size());
  1491. // Get the search index. Index #1 is to search using option code.
  1492. const Subnet::OptionContainerTypeIndex& idx = options->get<1>();
  1493. // Get the options for specified index. Expecting one option to be
  1494. // returned but in theory we may have multiple options with the same
  1495. // code so we get the range.
  1496. std::pair<Subnet::OptionContainerTypeIndex::const_iterator,
  1497. Subnet::OptionContainerTypeIndex::const_iterator> range =
  1498. idx.equal_range(DHO_NIS_SERVERS);
  1499. // Expect single option with the code equal to NIS_SERVERS option code.
  1500. ASSERT_EQ(1, std::distance(range.first, range.second));
  1501. // The actual pointer to the option is held in the option field
  1502. // in the structure returned.
  1503. OptionPtr option = range.first->option;
  1504. ASSERT_TRUE(option);
  1505. // Option object returned for here is expected to be Option6IA
  1506. // which is derived from Option. This class is dedicated to
  1507. // represent standard option IA_NA.
  1508. boost::shared_ptr<Option4AddrLst> option_addrs =
  1509. boost::dynamic_pointer_cast<Option4AddrLst>(option);
  1510. // If cast is unsuccessful than option returned was of a
  1511. // different type than Option6IA. This is wrong.
  1512. ASSERT_TRUE(option_addrs);
  1513. // Get addresses from the option.
  1514. Option4AddrLst::AddressContainer addrs = option_addrs->getAddresses();
  1515. // Verify that the addresses have been configured correctly.
  1516. ASSERT_EQ(3, addrs.size());
  1517. EXPECT_EQ("192.0.2.10", addrs[0].toText());
  1518. EXPECT_EQ("192.0.2.1", addrs[1].toText());
  1519. EXPECT_EQ("192.0.2.3", addrs[2].toText());
  1520. }
  1521. /// This test checks if Uint32Parser can really parse the whole range
  1522. /// and properly err of out of range values. As we can't call Uint32Parser
  1523. /// directly, we are exploiting the fact that it is used to parse global
  1524. /// parameter renew-timer and the results are stored in uint32_defaults.
  1525. /// We get the uint32_defaults using a getUint32Defaults functions which
  1526. /// is defined only to access the values from this test.
  1527. TEST_F(Dhcp4ParserTest, DISABLED_Uint32Parser) {
  1528. ConstElementPtr status;
  1529. // CASE 1: 0 - minimum value, should work
  1530. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_,
  1531. Element::fromJSON("{\"version\": 0,"
  1532. "\"renew-timer\": 0}")));
  1533. // returned value must be ok (0 is a proper value)
  1534. checkResult(status, 0);
  1535. checkGlobalUint32("renew-timer", 0);
  1536. // CASE 2: 4294967295U (UINT_MAX) should work as well
  1537. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_,
  1538. Element::fromJSON("{\"version\": 0,"
  1539. "\"renew-timer\": 4294967295}")));
  1540. // returned value must be ok (0 is a proper value)
  1541. checkResult(status, 0);
  1542. checkGlobalUint32("renew-timer", 4294967295U);
  1543. // CASE 3: 4294967296U (UINT_MAX + 1) should not work
  1544. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_,
  1545. Element::fromJSON("{\"version\": 0,"
  1546. "\"renew-timer\": 4294967296}")));
  1547. // returned value must be rejected (1 configuration error)
  1548. checkResult(status, 1);
  1549. // CASE 4: -1 (UINT_MIN -1 ) should not work
  1550. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_,
  1551. Element::fromJSON("{\"version\": 0,"
  1552. "\"renew-timer\": -1}")));
  1553. // returned value must be rejected (1 configuration error)
  1554. checkResult(status, 1);
  1555. }
  1556. // The goal of this test is to verify that the standard option can
  1557. // be configured to encapsulate multiple other options.
  1558. TEST_F(Dhcp4ParserTest, stdOptionDataEncapsulate) {
  1559. // The configuration is two stage process in this test.
  1560. // In the first stahe we create definitions of suboptions
  1561. // that we will add to the base option.
  1562. // Let's create some dummy options: foo and foo2.
  1563. string config = "{ \"interfaces\": [ \"*\" ],"
  1564. "\"rebind-timer\": 2000,"
  1565. "\"renew-timer\": 1000,"
  1566. "\"option-data\": [ {"
  1567. " \"name\": \"foo\","
  1568. " \"space\": \"vendor-encapsulated-options-space\","
  1569. " \"code\": 1,"
  1570. " \"data\": \"1234\","
  1571. " \"csv-format\": True"
  1572. " },"
  1573. " {"
  1574. " \"name\": \"foo2\","
  1575. " \"space\": \"vendor-encapsulated-options-space\","
  1576. " \"code\": 2,"
  1577. " \"data\": \"192.168.2.1\","
  1578. " \"csv-format\": True"
  1579. " } ],"
  1580. "\"option-def\": [ {"
  1581. " \"name\": \"foo\","
  1582. " \"code\": 1,"
  1583. " \"type\": \"uint32\","
  1584. " \"array\": False,"
  1585. " \"record-types\": \"\","
  1586. " \"space\": \"vendor-encapsulated-options-space\","
  1587. " \"encapsulate\": \"\""
  1588. " },"
  1589. " {"
  1590. " \"name\": \"foo2\","
  1591. " \"code\": 2,"
  1592. " \"type\": \"ipv4-address\","
  1593. " \"array\": False,"
  1594. " \"record-types\": \"\","
  1595. " \"space\": \"vendor-encapsulated-options-space\","
  1596. " \"encapsulate\": \"\""
  1597. " } ]"
  1598. "}";
  1599. ConstElementPtr status;
  1600. ElementPtr json = Element::fromJSON(config);
  1601. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json));
  1602. ASSERT_TRUE(status);
  1603. checkResult(status, 0);
  1604. // Once the definitions have been added we can configure the
  1605. // standard option #17. This option comprises an enterprise
  1606. // number and sub options. By convention (introduced in
  1607. // std_option_defs.h) option named 'vendor-opts'
  1608. // encapsulates the option space named 'vendor-opts-space'.
  1609. // We add our dummy options to this option space and thus
  1610. // they should be included as sub-options in the 'vendor-opts'
  1611. // option.
  1612. config = "{ \"interfaces\": [ \"*\" ],"
  1613. "\"rebind-timer\": 2000,"
  1614. "\"renew-timer\": 1000,"
  1615. "\"option-data\": [ {"
  1616. " \"name\": \"vendor-encapsulated-options\","
  1617. " \"space\": \"dhcp4\","
  1618. " \"code\": 43,"
  1619. " \"data\": \"\","
  1620. " \"csv-format\": False"
  1621. " },"
  1622. " {"
  1623. " \"name\": \"foo\","
  1624. " \"space\": \"vendor-encapsulated-options-space\","
  1625. " \"code\": 1,"
  1626. " \"data\": \"1234\","
  1627. " \"csv-format\": True"
  1628. " },"
  1629. " {"
  1630. " \"name\": \"foo2\","
  1631. " \"space\": \"vendor-encapsulated-options-space\","
  1632. " \"code\": 2,"
  1633. " \"data\": \"192.168.2.1\","
  1634. " \"csv-format\": True"
  1635. " } ],"
  1636. "\"option-def\": [ {"
  1637. " \"name\": \"foo\","
  1638. " \"code\": 1,"
  1639. " \"type\": \"uint32\","
  1640. " \"array\": False,"
  1641. " \"record-types\": \"\","
  1642. " \"space\": \"vendor-encapsulated-options-space\","
  1643. " \"encapsulate\": \"\""
  1644. " },"
  1645. " {"
  1646. " \"name\": \"foo2\","
  1647. " \"code\": 2,"
  1648. " \"type\": \"ipv4-address\","
  1649. " \"array\": False,"
  1650. " \"record-types\": \"\","
  1651. " \"space\": \"vendor-encapsulated-options-space\","
  1652. " \"encapsulate\": \"\""
  1653. " } ],"
  1654. "\"subnet4\": [ { "
  1655. " \"pool\": [ \"192.0.2.1 - 192.0.2.100\" ],"
  1656. " \"subnet\": \"192.0.2.0/24\""
  1657. " } ]"
  1658. "}";
  1659. json = Element::fromJSON(config);
  1660. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json));
  1661. ASSERT_TRUE(status);
  1662. checkResult(status, 0);
  1663. // Get the subnet.
  1664. Subnet4Ptr subnet = CfgMgr::instance().getSubnet4(IOAddress("192.0.2.5"));
  1665. ASSERT_TRUE(subnet);
  1666. // We should have one option available.
  1667. Subnet::OptionContainerPtr options = subnet->getOptionDescriptors("dhcp4");
  1668. ASSERT_TRUE(options);
  1669. ASSERT_EQ(1, options->size());
  1670. // Get the option.
  1671. Subnet::OptionDescriptor desc =
  1672. subnet->getOptionDescriptor("dhcp4", DHO_VENDOR_ENCAPSULATED_OPTIONS);
  1673. EXPECT_TRUE(desc.option);
  1674. EXPECT_EQ(DHO_VENDOR_ENCAPSULATED_OPTIONS, desc.option->getType());
  1675. // Option with the code 1 should be added as a sub-option.
  1676. OptionPtr option_foo = desc.option->getOption(1);
  1677. ASSERT_TRUE(option_foo);
  1678. EXPECT_EQ(1, option_foo->getType());
  1679. // This option comprises a single uint32_t value thus it is
  1680. // represented by OptionInt<uint32_t> class. Let's get the
  1681. // object of this type.
  1682. boost::shared_ptr<OptionInt<uint32_t> > option_foo_uint32 =
  1683. boost::dynamic_pointer_cast<OptionInt<uint32_t> >(option_foo);
  1684. ASSERT_TRUE(option_foo_uint32);
  1685. // Validate the value according to the configuration.
  1686. EXPECT_EQ(1234, option_foo_uint32->getValue());
  1687. // Option with the code 2 should be added as a sub-option.
  1688. OptionPtr option_foo2 = desc.option->getOption(2);
  1689. ASSERT_TRUE(option_foo2);
  1690. EXPECT_EQ(2, option_foo2->getType());
  1691. // This option comprises the IPV4 address. Such option is
  1692. // represented by OptionCustom object.
  1693. OptionCustomPtr option_foo2_v4 =
  1694. boost::dynamic_pointer_cast<OptionCustom>(option_foo2);
  1695. ASSERT_TRUE(option_foo2_v4);
  1696. // Get the IP address carried by this option and validate it.
  1697. EXPECT_EQ("192.168.2.1", option_foo2_v4->readAddress().toText());
  1698. // Option with the code 3 should not be added.
  1699. EXPECT_FALSE(desc.option->getOption(3));
  1700. }
  1701. // This test checks if vendor options can be specified in the config file
  1702. // (in hex format), and later retrieved from configured subnet
  1703. TEST_F(Dhcp4ParserTest, vendorOptionsHex) {
  1704. // This configuration string is to configure two options
  1705. // sharing the code 1 and belonging to the different vendor spaces.
  1706. // (different vendor-id values).
  1707. string config = "{ \"interfaces\": [ \"*\" ],"
  1708. "\"rebind-timer\": 2000,"
  1709. "\"renew-timer\": 1000,"
  1710. "\"option-data\": [ {"
  1711. " \"name\": \"option-one\","
  1712. " \"space\": \"vendor-4491\"," // VENDOR_ID_CABLE_LABS = 4491
  1713. " \"code\": 100," // just a random code
  1714. " \"data\": \"AB CDEF0105\","
  1715. " \"csv-format\": False"
  1716. " },"
  1717. " {"
  1718. " \"name\": \"option-two\","
  1719. " \"space\": \"vendor-1234\","
  1720. " \"code\": 100,"
  1721. " \"data\": \"1234\","
  1722. " \"csv-format\": False"
  1723. " } ],"
  1724. "\"subnet4\": [ { "
  1725. " \"pool\": [ \"192.0.2.1-192.0.2.10\" ],"
  1726. " \"subnet\": \"192.0.2.0/24\""
  1727. " } ]"
  1728. "}";
  1729. ConstElementPtr status;
  1730. ElementPtr json = Element::fromJSON(config);
  1731. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json));
  1732. ASSERT_TRUE(status);
  1733. checkResult(status, 0);
  1734. // Options should be now available for the subnet.
  1735. Subnet4Ptr subnet = CfgMgr::instance().getSubnet4(IOAddress("192.0.2.5"));
  1736. ASSERT_TRUE(subnet);
  1737. // Try to get the option from the vendor space 4491
  1738. Subnet::OptionDescriptor desc1 = subnet->getVendorOptionDescriptor(VENDOR_ID_CABLE_LABS, 100);
  1739. ASSERT_TRUE(desc1.option);
  1740. EXPECT_EQ(100, desc1.option->getType());
  1741. // Try to get the option from the vendor space 1234
  1742. Subnet::OptionDescriptor desc2 = subnet->getVendorOptionDescriptor(1234, 100);
  1743. ASSERT_TRUE(desc2.option);
  1744. EXPECT_EQ(100, desc1.option->getType());
  1745. // Try to get the non-existing option from the non-existing
  1746. // option space and expect that option is not returned.
  1747. Subnet::OptionDescriptor desc3 = subnet->getVendorOptionDescriptor(5678, 100);
  1748. ASSERT_FALSE(desc3.option);
  1749. }
  1750. // This test checks if vendor options can be specified in the config file,
  1751. // (in csv format), and later retrieved from configured subnet
  1752. TEST_F(Dhcp4ParserTest, vendorOptionsCsv) {
  1753. // This configuration string is to configure two options
  1754. // sharing the code 1 and belonging to the different vendor spaces.
  1755. // (different vendor-id values).
  1756. string config = "{ \"interfaces\": [ \"*\" ],"
  1757. "\"rebind-timer\": 2000,"
  1758. "\"renew-timer\": 1000,"
  1759. "\"option-data\": [ {"
  1760. " \"name\": \"foo\","
  1761. " \"space\": \"vendor-4491\","
  1762. " \"code\": 100,"
  1763. " \"data\": \"this is a string vendor-opt\","
  1764. " \"csv-format\": True"
  1765. " } ],"
  1766. "\"option-def\": [ {"
  1767. " \"name\": \"foo\","
  1768. " \"code\": 100,"
  1769. " \"type\": \"string\","
  1770. " \"array\": False,"
  1771. " \"record-types\": \"\","
  1772. " \"space\": \"vendor-4491\","
  1773. " \"encapsulate\": \"\""
  1774. " } ],"
  1775. "\"subnet4\": [ { "
  1776. " \"pool\": [ \"192.0.2.1 - 192.0.2.100\" ],"
  1777. " \"subnet\": \"192.0.2.0/24\" "
  1778. " } ]"
  1779. "}";
  1780. ConstElementPtr status;
  1781. ElementPtr json = Element::fromJSON(config);
  1782. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json));
  1783. ASSERT_TRUE(status);
  1784. checkResult(status, 0);
  1785. // Options should be now available for the subnet.
  1786. Subnet4Ptr subnet = CfgMgr::instance().getSubnet4(IOAddress("192.0.2.5"));
  1787. ASSERT_TRUE(subnet);
  1788. // Try to get the option from the vendor space 4491
  1789. Subnet::OptionDescriptor desc1 = subnet->getVendorOptionDescriptor(VENDOR_ID_CABLE_LABS, 100);
  1790. ASSERT_TRUE(desc1.option);
  1791. EXPECT_EQ(100, desc1.option->getType());
  1792. // Try to get the non-existing option from the non-existing
  1793. // option space and expect that option is not returned.
  1794. Subnet::OptionDescriptor desc2 = subnet->getVendorOptionDescriptor(5678, 100);
  1795. ASSERT_FALSE(desc2.option);
  1796. }
  1797. // Tests of the hooks libraries configuration. All tests have the pre-
  1798. // condition (checked in the test fixture's SetUp() method) that no hooks
  1799. // libraries are loaded at the start of the tests.
  1800. // Helper function to return a configuration containing an arbitrary number
  1801. // of hooks libraries.
  1802. std::string
  1803. buildHooksLibrariesConfig(const std::vector<std::string>& libraries) {
  1804. const string quote("\"");
  1805. // Create the first part of the configuration string.
  1806. string config =
  1807. "{ \"interfaces\": [ \"*\" ],"
  1808. "\"hooks-libraries\": [";
  1809. // Append the libraries (separated by commas if needed)
  1810. for (int i = 0; i < libraries.size(); ++i) {
  1811. if (i > 0) {
  1812. config += string(", ");
  1813. }
  1814. config += (quote + libraries[i] + quote);
  1815. }
  1816. // Append the remainder of the configuration.
  1817. config += string(
  1818. "],"
  1819. "\"rebind-timer\": 2000,"
  1820. "\"renew-timer\": 1000,"
  1821. "\"option-data\": [ {"
  1822. " \"name\": \"dhcp-message\","
  1823. " \"space\": \"dhcp4\","
  1824. " \"code\": 56,"
  1825. " \"data\": \"AB CDEF0105\","
  1826. " \"csv-format\": False"
  1827. " },"
  1828. " {"
  1829. " \"name\": \"foo\","
  1830. " \"space\": \"isc\","
  1831. " \"code\": 56,"
  1832. " \"data\": \"1234\","
  1833. " \"csv-format\": True"
  1834. " } ],"
  1835. "\"option-def\": [ {"
  1836. " \"name\": \"foo\","
  1837. " \"code\": 56,"
  1838. " \"type\": \"uint32\","
  1839. " \"array\": False,"
  1840. " \"record-types\": \"\","
  1841. " \"space\": \"isc\","
  1842. " \"encapsulate\": \"\""
  1843. " } ],"
  1844. "\"subnet4\": [ { "
  1845. " \"pool\": [ \"192.0.2.1 - 192.0.2.100\" ],"
  1846. " \"subnet\": \"192.0.2.0/24\""
  1847. " } ]"
  1848. "}");
  1849. return (config);
  1850. }
  1851. // Convenience function for creating hooks library configuration with one or
  1852. // two character string constants.
  1853. std::string
  1854. buildHooksLibrariesConfig(const char* library1 = NULL,
  1855. const char* library2 = NULL) {
  1856. std::vector<std::string> libraries;
  1857. if (library1 != NULL) {
  1858. libraries.push_back(string(library1));
  1859. if (library2 != NULL) {
  1860. libraries.push_back(string(library2));
  1861. }
  1862. }
  1863. return (buildHooksLibrariesConfig(libraries));
  1864. }
  1865. // The goal of this test is to verify the configuration of hooks libraries if
  1866. // none are specified.
  1867. TEST_F(Dhcp4ParserTest, NoHooksLibraries) {
  1868. // Parse a configuration containing no names.
  1869. string config = buildHooksLibrariesConfig();
  1870. if (!executeConfiguration(config,
  1871. "set configuration with no hooks libraries")) {
  1872. FAIL() << "Unable to execute configuration";
  1873. } else {
  1874. // No libraries should be loaded at the end of the test.
  1875. std::vector<std::string> libraries = HooksManager::getLibraryNames();
  1876. EXPECT_TRUE(libraries.empty());
  1877. }
  1878. }
  1879. // Verify parsing fails with one library that will fail validation.
  1880. TEST_F(Dhcp4ParserTest, InvalidLibrary) {
  1881. // Parse a configuration containing a failing library.
  1882. string config = buildHooksLibrariesConfig(NOT_PRESENT_LIBRARY);
  1883. ConstElementPtr status;
  1884. ElementPtr json = Element::fromJSON(config);
  1885. ASSERT_NO_THROW(status = configureDhcp4Server(*srv_, json));
  1886. // The status object must not be NULL
  1887. ASSERT_TRUE(status);
  1888. // Returned value should not be 0
  1889. comment_ = parseAnswer(rcode_, status);
  1890. EXPECT_NE(0, rcode_);
  1891. }
  1892. // Verify the configuration of hooks libraries with two being specified.
  1893. TEST_F(Dhcp4ParserTest, LibrariesSpecified) {
  1894. // Marker files should not be present.
  1895. EXPECT_FALSE(checkMarkerFileExists(LOAD_MARKER_FILE));
  1896. EXPECT_FALSE(checkMarkerFileExists(UNLOAD_MARKER_FILE));
  1897. // Set up the configuration with two libraries and load them.
  1898. string config = buildHooksLibrariesConfig(CALLOUT_LIBRARY_1,
  1899. CALLOUT_LIBRARY_2);
  1900. ASSERT_TRUE(executeConfiguration(config,
  1901. "load two valid libraries"));
  1902. // Expect two libraries to be loaded in the correct order (load marker file
  1903. // is present, no unload marker file).
  1904. std::vector<std::string> libraries = HooksManager::getLibraryNames();
  1905. ASSERT_EQ(2, libraries.size());
  1906. EXPECT_TRUE(checkMarkerFile(LOAD_MARKER_FILE, "12"));
  1907. EXPECT_FALSE(checkMarkerFileExists(UNLOAD_MARKER_FILE));
  1908. // Unload the libraries. The load file should not have changed, but
  1909. // the unload one should indicate the unload() functions have been run.
  1910. config = buildHooksLibrariesConfig();
  1911. ASSERT_TRUE(executeConfiguration(config, "unloading libraries"));
  1912. EXPECT_TRUE(checkMarkerFile(LOAD_MARKER_FILE, "12"));
  1913. EXPECT_TRUE(checkMarkerFile(UNLOAD_MARKER_FILE, "21"));
  1914. // Expect the hooks system to say that none are loaded.
  1915. libraries = HooksManager::getLibraryNames();
  1916. EXPECT_TRUE(libraries.empty());
  1917. }
  1918. // This test verifies that it is possible to select subset of interfaces
  1919. // on which server should listen.
  1920. TEST_F(Dhcp4ParserTest, selectedInterfaces) {
  1921. ConstElementPtr x;
  1922. string config = "{ \"interfaces\": [ \"eth0\", \"eth1\" ],"
  1923. "\"rebind-timer\": 2000, "
  1924. "\"renew-timer\": 1000, "
  1925. "\"valid-lifetime\": 4000 }";
  1926. ElementPtr json = Element::fromJSON(config);
  1927. ConstElementPtr status;
  1928. // Make sure the config manager is clean and there is no hanging
  1929. // interface configuration.
  1930. ASSERT_FALSE(CfgMgr::instance().isActiveIface("eth0"));
  1931. ASSERT_FALSE(CfgMgr::instance().isActiveIface("eth1"));
  1932. ASSERT_FALSE(CfgMgr::instance().isActiveIface("eth2"));
  1933. // Apply configuration.
  1934. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json));
  1935. ASSERT_TRUE(status);
  1936. checkResult(status, 0);
  1937. // eth0 and eth1 were explicitly selected. eth2 was not.
  1938. EXPECT_TRUE(CfgMgr::instance().isActiveIface("eth0"));
  1939. EXPECT_TRUE(CfgMgr::instance().isActiveIface("eth1"));
  1940. EXPECT_FALSE(CfgMgr::instance().isActiveIface("eth2"));
  1941. }
  1942. // This test verifies that it is possible to configure the server in such a way
  1943. // that it listens on all interfaces.
  1944. TEST_F(Dhcp4ParserTest, allInterfaces) {
  1945. ConstElementPtr x;
  1946. // This configuration specifies two interfaces on which server should listen
  1947. // but it also includes asterisk. The asterisk switches server into the
  1948. // mode when it listens on all interfaces regardless of what interface names
  1949. // were specified in the "interfaces" parameter.
  1950. string config = "{ \"interfaces\": [ \"eth0\", \"*\", \"eth1\" ],"
  1951. "\"rebind-timer\": 2000, "
  1952. "\"renew-timer\": 1000, "
  1953. "\"valid-lifetime\": 4000 }";
  1954. ElementPtr json = Element::fromJSON(config);
  1955. ConstElementPtr status;
  1956. // Make sure there is no old configuration.
  1957. ASSERT_FALSE(CfgMgr::instance().isActiveIface("eth0"));
  1958. ASSERT_FALSE(CfgMgr::instance().isActiveIface("eth1"));
  1959. ASSERT_FALSE(CfgMgr::instance().isActiveIface("eth2"));
  1960. // Apply configuration.
  1961. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json));
  1962. ASSERT_TRUE(status);
  1963. checkResult(status, 0);
  1964. // All interfaces should be now active.
  1965. EXPECT_TRUE(CfgMgr::instance().isActiveIface("eth0"));
  1966. EXPECT_TRUE(CfgMgr::instance().isActiveIface("eth1"));
  1967. EXPECT_TRUE(CfgMgr::instance().isActiveIface("eth2"));
  1968. }
  1969. }