config_parser_unittest.cc 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770
  1. // Copyright (C) 2012 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 <dhcp4/dhcp4_srv.h>
  18. #include <dhcp4/config_parser.h>
  19. #include <config/ccsession.h>
  20. #include <dhcpsrv/subnet.h>
  21. #include <dhcpsrv/cfgmgr.h>
  22. #include <boost/foreach.hpp>
  23. #include <iostream>
  24. #include <fstream>
  25. #include <sstream>
  26. #include <limits.h>
  27. using namespace std;
  28. using namespace isc;
  29. using namespace isc::dhcp;
  30. using namespace isc::asiolink;
  31. using namespace isc::data;
  32. using namespace isc::config;
  33. namespace isc {
  34. namespace dhcp {
  35. extern Uint32Storage uint32_defaults;
  36. }
  37. }
  38. namespace {
  39. class Dhcp4ParserTest : public ::testing::Test {
  40. public:
  41. Dhcp4ParserTest()
  42. :rcode_(-1) {
  43. // Open port 0 means to not do anything at all. We don't want to
  44. // deal with sockets here, just check if configuration handling
  45. // is sane.
  46. srv_ = new Dhcpv4Srv(0);
  47. }
  48. // Checks if global parameter of name have expected_value
  49. void checkGlobalUint32(string name, uint32_t expected_value) {
  50. Uint32Storage::const_iterator it = uint32_defaults.find(name);
  51. if (it == uint32_defaults.end()) {
  52. ADD_FAILURE() << "Expected uint32 with name " << name
  53. << " not found";
  54. return;
  55. }
  56. EXPECT_EQ(expected_value, it->second);
  57. }
  58. // Checks if config_result (result of DHCP server configuration) has
  59. // expected code (0 for success, other for failures).
  60. // Also stores result in rcode_ and comment_.
  61. void checkResult(ConstElementPtr status, int expected_code) {
  62. ASSERT_TRUE(status);
  63. comment_ = parseAnswer(rcode_, status);
  64. EXPECT_EQ(expected_code, rcode_);
  65. }
  66. ~Dhcp4ParserTest() {
  67. resetConfiguration();
  68. delete srv_;
  69. };
  70. /// @brief Create the simple configuration with single option.
  71. ///
  72. /// This function allows to set one of the parameters that configure
  73. /// option value. These parameters are: "name", "code" and "data".
  74. ///
  75. /// @param param_value string holiding option parameter value to be
  76. /// injected into the configuration string.
  77. /// @param parameter name of the parameter to be configured with
  78. /// param value.
  79. /// @return configuration string containing custom values of parameters
  80. /// describing an option.
  81. std::string createConfigWithOption(const std::string& param_value,
  82. const std::string& parameter) {
  83. std::map<std::string, std::string> params;
  84. if (parameter == "name") {
  85. params["name"] = param_value;
  86. params["code"] = "56";
  87. params["data"] = "AB CDEF0105";
  88. } else if (parameter == "code") {
  89. params["name"] = "option_foo";
  90. params["code"] = param_value;
  91. params["data"] = "AB CDEF0105";
  92. } else if (parameter == "data") {
  93. params["name"] = "option_foo";
  94. params["code"] = "56";
  95. params["data"] = param_value;
  96. }
  97. return (createConfigWithOption(params));
  98. }
  99. /// @brief Create simple configuration with single option.
  100. ///
  101. /// This function creates a configuration for a single option with
  102. /// custom values for all parameters that describe the option.
  103. ///
  104. /// @params params map holding parameters and their values.
  105. /// @return configuration string containing custom values of parameters
  106. /// describing an option.
  107. std::string createConfigWithOption(const std::map<std::string, std::string>& params) {
  108. std::ostringstream stream;
  109. stream << "{ \"interface\": [ \"all\" ],"
  110. "\"rebind-timer\": 2000, "
  111. "\"renew-timer\": 1000, "
  112. "\"subnet4\": [ { "
  113. " \"pool\": [ \"192.0.2.1 - 192.0.2.100\" ],"
  114. " \"subnet\": \"192.0.2.0/24\", "
  115. " \"option-data\": [ {";
  116. bool first = true;
  117. typedef std::pair<std::string, std::string> ParamPair;
  118. BOOST_FOREACH(ParamPair param, params) {
  119. if (!first) {
  120. stream << ", ";
  121. } else {
  122. // cppcheck-suppress unreadVariable
  123. first = false;
  124. }
  125. if (param.first == "name") {
  126. stream << "\"name\": \"" << param.second << "\"";
  127. } else if (param.first == "code") {
  128. stream << "\"code\": " << param.second << "";
  129. } else if (param.first == "data") {
  130. stream << "\"data\": \"" << param.second << "\"";
  131. }
  132. }
  133. stream <<
  134. " } ]"
  135. " } ],"
  136. "\"valid-lifetime\": 4000 }";
  137. return (stream.str());
  138. }
  139. /// @brief Test invalid option parameter value.
  140. ///
  141. /// This test function constructs the simple configuration
  142. /// string and injects invalid option configuration into it.
  143. /// It expects that parser will fail with provided option code.
  144. ///
  145. /// @param param_value string holding invalid option parameter value
  146. /// to be injected into configuration string.
  147. /// @param parameter name of the parameter to be configured with
  148. /// param_value (can be any of "name", "code", "data")
  149. void testInvalidOptionParam(const std::string& param_value,
  150. const std::string& parameter) {
  151. ConstElementPtr x;
  152. std::string config = createConfigWithOption(param_value, parameter);
  153. ElementPtr json = Element::fromJSON(config);
  154. EXPECT_NO_THROW(x = configureDhcp4Server(*srv_, json));
  155. ASSERT_TRUE(x);
  156. comment_ = parseAnswer(rcode_, x);
  157. ASSERT_EQ(1, rcode_);
  158. }
  159. /// @brief Test option against given code and data.
  160. ///
  161. /// @param option_desc option descriptor that carries the option to
  162. /// be tested.
  163. /// @param expected_code expected code of the option.
  164. /// @param expected_data expected data in the option.
  165. /// @param expected_data_len length of the reference data.
  166. /// @param extra_data if true extra data is allowed in an option
  167. /// after tested data.
  168. void testOption(const Subnet::OptionDescriptor& option_desc,
  169. uint16_t expected_code, const uint8_t* expected_data,
  170. size_t expected_data_len,
  171. bool extra_data = false) {
  172. // Check if option descriptor contains valid option pointer.
  173. ASSERT_TRUE(option_desc.option);
  174. // Verify option type.
  175. EXPECT_EQ(expected_code, option_desc.option->getType());
  176. // We may have many different option types being created. Some of them
  177. // have dedicated classes derived from Option class. In such case if
  178. // we want to verify the option contents against expected_data we have
  179. // to prepare raw buffer with the contents of the option. The easiest
  180. // way is to call pack() which will prepare on-wire data.
  181. util::OutputBuffer buf(option_desc.option->getData().size());
  182. option_desc.option->pack(buf);
  183. if (extra_data) {
  184. // The length of the buffer must be at least equal to size of the
  185. // reference data but it can sometimes be greater than that. This is
  186. // because some options carry suboptions that increase the overall
  187. // length.
  188. ASSERT_GE(buf.getLength() - option_desc.option->getHeaderLen(),
  189. expected_data_len);
  190. } else {
  191. ASSERT_EQ(buf.getLength() - option_desc.option->getHeaderLen(),
  192. expected_data_len);
  193. }
  194. // Verify that the data is correct. Do not verify suboptions and a header.
  195. const uint8_t* data = static_cast<const uint8_t*>(buf.getData());
  196. EXPECT_EQ(0, memcmp(expected_data, data + option_desc.option->getHeaderLen(),
  197. expected_data_len));
  198. }
  199. /// @brief Reset configuration database.
  200. ///
  201. /// This function resets configuration data base by
  202. /// removing all subnets and option-data. Reset must
  203. /// be performed after each test to make sure that
  204. /// contents of the database do not affect result of
  205. /// subsequent tests.
  206. void resetConfiguration() {
  207. ConstElementPtr status;
  208. string config = "{ \"interface\": [ \"all\" ],"
  209. "\"rebind-timer\": 2000, "
  210. "\"renew-timer\": 1000, "
  211. "\"valid-lifetime\": 4000, "
  212. "\"subnet4\": [ ], "
  213. "\"option-data\": [ ] }";
  214. try {
  215. ElementPtr json = Element::fromJSON(config);
  216. status = configureDhcp4Server(*srv_, json);
  217. } catch (const std::exception& ex) {
  218. FAIL() << "Fatal error: unable to reset configuration database"
  219. << " after the test. The following configuration was used"
  220. << " to reset database: " << std::endl
  221. << config << std::endl
  222. << " and the following error message was returned:"
  223. << ex.what() << std::endl;
  224. }
  225. // status object must not be NULL
  226. if (!status) {
  227. FAIL() << "Fatal error: unable to reset configuration database"
  228. << " after the test. Configuration function returned"
  229. << " NULL pointer" << std::endl;
  230. }
  231. comment_ = parseAnswer(rcode_, status);
  232. // returned value should be 0 (configuration success)
  233. if (rcode_ != 0) {
  234. FAIL() << "Fatal error: unable to reset configuration database"
  235. << " after the test. Configuration function returned"
  236. << " error code " << rcode_ << std::endl;
  237. }
  238. }
  239. Dhcpv4Srv* srv_;
  240. int rcode_;
  241. ConstElementPtr comment_;
  242. };
  243. // Goal of this test is a verification if a very simple config update
  244. // with just a bumped version number. That's the simplest possible
  245. // config update.
  246. TEST_F(Dhcp4ParserTest, version) {
  247. ConstElementPtr x;
  248. EXPECT_NO_THROW(x = configureDhcp4Server(*srv_,
  249. Element::fromJSON("{\"version\": 0}")));
  250. // returned value must be 0 (configuration accepted)
  251. checkResult(x, 0);
  252. }
  253. /// The goal of this test is to verify that the code accepts only
  254. /// valid commands and malformed or unsupported parameters are rejected.
  255. TEST_F(Dhcp4ParserTest, bogusCommand) {
  256. ConstElementPtr x;
  257. EXPECT_NO_THROW(x = configureDhcp4Server(*srv_,
  258. Element::fromJSON("{\"bogus\": 5}")));
  259. // returned value must be 1 (configuration parse error)
  260. checkResult(x, 1);
  261. }
  262. /// The goal of this test is to verify if wrongly defined subnet will
  263. /// be rejected. Properly defined subnet must include at least one
  264. /// pool definition.
  265. TEST_F(Dhcp4ParserTest, emptySubnet) {
  266. ConstElementPtr status;
  267. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_,
  268. Element::fromJSON("{ \"interface\": [ \"all\" ],"
  269. "\"rebind-timer\": 2000, "
  270. "\"renew-timer\": 1000, "
  271. "\"subnet4\": [ ], "
  272. "\"valid-lifetime\": 4000 }")));
  273. // returned value should be 0 (success)
  274. checkResult(status, 0);
  275. checkGlobalUint32("rebind-timer", 2000);
  276. checkGlobalUint32("renew-timer", 1000);
  277. checkGlobalUint32("valid-lifetime", 4000);
  278. }
  279. /// The goal of this test is to verify if defined subnet uses global
  280. /// parameter timer definitions.
  281. TEST_F(Dhcp4ParserTest, subnetGlobalDefaults) {
  282. ConstElementPtr status;
  283. string config = "{ \"interface\": [ \"all\" ],"
  284. "\"rebind-timer\": 2000, "
  285. "\"renew-timer\": 1000, "
  286. "\"subnet4\": [ { "
  287. " \"pool\": [ \"192.0.2.1 - 192.0.2.100\" ],"
  288. " \"subnet\": \"192.0.2.0/24\" } ],"
  289. "\"valid-lifetime\": 4000 }";
  290. cout << config << endl;
  291. ElementPtr json = Element::fromJSON(config);
  292. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json));
  293. // check if returned status is OK
  294. checkResult(status, 0);
  295. // Now check if the configuration was indeed handled and we have
  296. // expected pool configured.
  297. Subnet4Ptr subnet = CfgMgr::instance().getSubnet4(IOAddress("192.0.2.200"));
  298. ASSERT_TRUE(subnet);
  299. EXPECT_EQ(1000, subnet->getT1());
  300. EXPECT_EQ(2000, subnet->getT2());
  301. EXPECT_EQ(4000, subnet->getValid());
  302. }
  303. // This test checks if it is possible to override global values
  304. // on a per subnet basis.
  305. TEST_F(Dhcp4ParserTest, subnetLocal) {
  306. ConstElementPtr status;
  307. string config = "{ \"interface\": [ \"all\" ],"
  308. "\"rebind-timer\": 2000, "
  309. "\"renew-timer\": 1000, "
  310. "\"subnet4\": [ { "
  311. " \"pool\": [ \"192.0.2.1 - 192.0.2.100\" ],"
  312. " \"renew-timer\": 1, "
  313. " \"rebind-timer\": 2, "
  314. " \"valid-lifetime\": 4,"
  315. " \"subnet\": \"192.0.2.0/24\" } ],"
  316. "\"valid-lifetime\": 4000 }";
  317. cout << config << endl;
  318. ElementPtr json = Element::fromJSON(config);
  319. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json));
  320. // returned value should be 0 (configuration success)
  321. checkResult(status, 0);
  322. Subnet4Ptr subnet = CfgMgr::instance().getSubnet4(IOAddress("192.0.2.200"));
  323. ASSERT_TRUE(subnet);
  324. EXPECT_EQ(1, subnet->getT1());
  325. EXPECT_EQ(2, subnet->getT2());
  326. EXPECT_EQ(4, subnet->getValid());
  327. }
  328. // Test verifies that a subnet with pool values that do not belong to that
  329. // pool are rejected.
  330. TEST_F(Dhcp4ParserTest, poolOutOfSubnet) {
  331. ConstElementPtr status;
  332. string config = "{ \"interface\": [ \"all\" ],"
  333. "\"rebind-timer\": 2000, "
  334. "\"renew-timer\": 1000, "
  335. "\"subnet4\": [ { "
  336. " \"pool\": [ \"192.0.4.0/28\" ],"
  337. " \"subnet\": \"192.0.2.0/24\" } ],"
  338. "\"valid-lifetime\": 4000 }";
  339. cout << config << endl;
  340. ElementPtr json = Element::fromJSON(config);
  341. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json));
  342. // returned value must be 2 (values error)
  343. // as the pool does not belong to that subnet
  344. checkResult(status, 2);
  345. }
  346. // Goal of this test is to verify if pools can be defined
  347. // using prefix/length notation. There is no separate test for min-max
  348. // notation as it was tested in several previous tests.
  349. TEST_F(Dhcp4ParserTest, poolPrefixLen) {
  350. ConstElementPtr status;
  351. string config = "{ \"interface\": [ \"all\" ],"
  352. "\"rebind-timer\": 2000, "
  353. "\"renew-timer\": 1000, "
  354. "\"subnet4\": [ { "
  355. " \"pool\": [ \"192.0.2.128/28\" ],"
  356. " \"subnet\": \"192.0.2.0/24\" } ],"
  357. "\"valid-lifetime\": 4000 }";
  358. cout << config << endl;
  359. ElementPtr json = Element::fromJSON(config);
  360. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_, json));
  361. // returned value must be 0 (configuration accepted)
  362. checkResult(status, 0);
  363. Subnet4Ptr subnet = CfgMgr::instance().getSubnet4(IOAddress("192.0.2.200"));
  364. ASSERT_TRUE(subnet);
  365. EXPECT_EQ(1000, subnet->getT1());
  366. EXPECT_EQ(2000, subnet->getT2());
  367. EXPECT_EQ(4000, subnet->getValid());
  368. }
  369. // Goal of this test is to verify that global option
  370. // data is configured for the subnet if the subnet
  371. // configuration does not include options configuration.
  372. TEST_F(Dhcp4ParserTest, optionDataDefaults) {
  373. ConstElementPtr x;
  374. string config = "{ \"interface\": [ \"all\" ],"
  375. "\"rebind-timer\": 2000,"
  376. "\"renew-timer\": 1000,"
  377. "\"option-data\": [ {"
  378. " \"name\": \"option_foo\","
  379. " \"code\": 56,"
  380. " \"data\": \"AB CDEF0105\""
  381. " },"
  382. " {"
  383. " \"name\": \"option_foo2\","
  384. " \"code\": 23,"
  385. " \"data\": \"01\""
  386. " } ],"
  387. "\"subnet4\": [ { "
  388. " \"pool\": [ \"192.0.2.1 - 192.0.2.100\" ],"
  389. " \"subnet\": \"192.0.2.0/24\""
  390. " } ],"
  391. "\"valid-lifetime\": 4000 }";
  392. ElementPtr json = Element::fromJSON(config);
  393. EXPECT_NO_THROW(x = configureDhcp4Server(*srv_, json));
  394. ASSERT_TRUE(x);
  395. comment_ = parseAnswer(rcode_, x);
  396. ASSERT_EQ(0, rcode_);
  397. Subnet4Ptr subnet = CfgMgr::instance().getSubnet4(IOAddress("192.0.2.200"));
  398. ASSERT_TRUE(subnet);
  399. const Subnet::OptionContainer& options = subnet->getOptions();
  400. ASSERT_EQ(2, options.size());
  401. // Get the search index. Index #1 is to search using option code.
  402. const Subnet::OptionContainerTypeIndex& idx = options.get<1>();
  403. // Get the options for specified index. Expecting one option to be
  404. // returned but in theory we may have multiple options with the same
  405. // code so we get the range.
  406. std::pair<Subnet::OptionContainerTypeIndex::const_iterator,
  407. Subnet::OptionContainerTypeIndex::const_iterator> range =
  408. idx.equal_range(56);
  409. // Expect single option with the code equal to 56.
  410. ASSERT_EQ(1, std::distance(range.first, range.second));
  411. const uint8_t foo_expected[] = {
  412. 0xAB, 0xCD, 0xEF, 0x01, 0x05
  413. };
  414. // Check if option is valid in terms of code and carried data.
  415. testOption(*range.first, 56, foo_expected, sizeof(foo_expected));
  416. range = idx.equal_range(23);
  417. ASSERT_EQ(1, std::distance(range.first, range.second));
  418. // Do another round of testing with second option.
  419. const uint8_t foo2_expected[] = {
  420. 0x01
  421. };
  422. testOption(*range.first, 23, foo2_expected, sizeof(foo2_expected));
  423. }
  424. // Goal of this test is to verify options configuration
  425. // for a single subnet. In particular this test checks
  426. // that local options configuration overrides global
  427. // option setting.
  428. TEST_F(Dhcp4ParserTest, optionDataInSingleSubnet) {
  429. ConstElementPtr x;
  430. string config = "{ \"interface\": [ \"all\" ],"
  431. "\"rebind-timer\": 2000, "
  432. "\"renew-timer\": 1000, "
  433. "\"option-data\": [ {"
  434. " \"name\": \"option_foo\","
  435. " \"code\": 56,"
  436. " \"data\": \"AB\""
  437. " } ],"
  438. "\"subnet4\": [ { "
  439. " \"pool\": [ \"192.0.2.1 - 192.0.2.100\" ],"
  440. " \"subnet\": \"192.0.2.0/24\", "
  441. " \"option-data\": [ {"
  442. " \"name\": \"option_foo\","
  443. " \"code\": 56,"
  444. " \"data\": \"AB CDEF0105\""
  445. " },"
  446. " {"
  447. " \"name\": \"option_foo2\","
  448. " \"code\": 23,"
  449. " \"data\": \"01\""
  450. " } ]"
  451. " } ],"
  452. "\"valid-lifetime\": 4000 }";
  453. ElementPtr json = Element::fromJSON(config);
  454. EXPECT_NO_THROW(x = configureDhcp4Server(*srv_, json));
  455. ASSERT_TRUE(x);
  456. comment_ = parseAnswer(rcode_, x);
  457. ASSERT_EQ(0, rcode_);
  458. Subnet4Ptr subnet = CfgMgr::instance().getSubnet4(IOAddress("192.0.2.24"));
  459. ASSERT_TRUE(subnet);
  460. const Subnet::OptionContainer& options = subnet->getOptions();
  461. ASSERT_EQ(2, options.size());
  462. // Get the search index. Index #1 is to search using option code.
  463. const Subnet::OptionContainerTypeIndex& idx = options.get<1>();
  464. // Get the options for specified index. Expecting one option to be
  465. // returned but in theory we may have multiple options with the same
  466. // code so we get the range.
  467. std::pair<Subnet::OptionContainerTypeIndex::const_iterator,
  468. Subnet::OptionContainerTypeIndex::const_iterator> range =
  469. idx.equal_range(56);
  470. // Expect single option with the code equal to 100.
  471. ASSERT_EQ(1, std::distance(range.first, range.second));
  472. const uint8_t foo_expected[] = {
  473. 0xAB, 0xCD, 0xEF, 0x01, 0x05
  474. };
  475. // Check if option is valid in terms of code and carried data.
  476. testOption(*range.first, 56, foo_expected, sizeof(foo_expected));
  477. range = idx.equal_range(23);
  478. ASSERT_EQ(1, std::distance(range.first, range.second));
  479. // Do another round of testing with second option.
  480. const uint8_t foo2_expected[] = {
  481. 0x01
  482. };
  483. testOption(*range.first, 23, foo2_expected, sizeof(foo2_expected));
  484. }
  485. // Goal of this test is to verify options configuration
  486. // for multiple subnets.
  487. TEST_F(Dhcp4ParserTest, optionDataInMultipleSubnets) {
  488. ConstElementPtr x;
  489. string config = "{ \"interface\": [ \"all\" ],"
  490. "\"rebind-timer\": 2000, "
  491. "\"renew-timer\": 1000, "
  492. "\"subnet4\": [ { "
  493. " \"pool\": [ \"192.0.2.1 - 192.0.2.100\" ],"
  494. " \"subnet\": \"192.0.2.0/24\", "
  495. " \"option-data\": [ {"
  496. " \"name\": \"option_foo\","
  497. " \"code\": 56,"
  498. " \"data\": \"0102030405060708090A\""
  499. " } ]"
  500. " },"
  501. " {"
  502. " \"pool\": [ \"192.0.3.101 - 192.0.3.150\" ],"
  503. " \"subnet\": \"192.0.3.0/24\", "
  504. " \"option-data\": [ {"
  505. " \"name\": \"option_foo2\","
  506. " \"code\": 23,"
  507. " \"data\": \"FF\""
  508. " } ]"
  509. " } ],"
  510. "\"valid-lifetime\": 4000 }";
  511. ElementPtr json = Element::fromJSON(config);
  512. EXPECT_NO_THROW(x = configureDhcp4Server(*srv_, json));
  513. ASSERT_TRUE(x);
  514. comment_ = parseAnswer(rcode_, x);
  515. ASSERT_EQ(0, rcode_);
  516. Subnet4Ptr subnet1 = CfgMgr::instance().getSubnet4(IOAddress("192.0.2.100"));
  517. ASSERT_TRUE(subnet1);
  518. const Subnet::OptionContainer& options1 = subnet1->getOptions();
  519. ASSERT_EQ(1, options1.size());
  520. // Get the search index. Index #1 is to search using option code.
  521. const Subnet::OptionContainerTypeIndex& idx1 = options1.get<1>();
  522. // Get the options for specified index. Expecting one option to be
  523. // returned but in theory we may have multiple options with the same
  524. // code so we get the range.
  525. std::pair<Subnet::OptionContainerTypeIndex::const_iterator,
  526. Subnet::OptionContainerTypeIndex::const_iterator> range1 =
  527. idx1.equal_range(56);
  528. // Expect single option with the code equal to 56.
  529. ASSERT_EQ(1, std::distance(range1.first, range1.second));
  530. const uint8_t foo_expected[] = {
  531. 0x01, 0x02, 0x03, 0x04, 0x05,
  532. 0x06, 0x07, 0x08, 0x09, 0x0A
  533. };
  534. // Check if option is valid in terms of code and carried data.
  535. testOption(*range1.first, 56, foo_expected, sizeof(foo_expected));
  536. // Test another subnet in the same way.
  537. Subnet4Ptr subnet2 = CfgMgr::instance().getSubnet4(IOAddress("192.0.3.102"));
  538. ASSERT_TRUE(subnet2);
  539. const Subnet::OptionContainer& options2 = subnet2->getOptions();
  540. ASSERT_EQ(1, options2.size());
  541. const Subnet::OptionContainerTypeIndex& idx2 = options2.get<1>();
  542. std::pair<Subnet::OptionContainerTypeIndex::const_iterator,
  543. Subnet::OptionContainerTypeIndex::const_iterator> range2 =
  544. idx2.equal_range(23);
  545. ASSERT_EQ(1, std::distance(range2.first, range2.second));
  546. const uint8_t foo2_expected[] = { 0xFF };
  547. testOption(*range2.first, 23, foo2_expected, sizeof(foo2_expected));
  548. }
  549. // Verify that empty option name is rejected in the configuration.
  550. TEST_F(Dhcp4ParserTest, optionNameEmpty) {
  551. // Empty option names not allowed.
  552. testInvalidOptionParam("", "name");
  553. }
  554. // Verify that empty option name with spaces is rejected
  555. // in the configuration.
  556. TEST_F(Dhcp4ParserTest, optionNameSpaces) {
  557. // Spaces in option names not allowed.
  558. testInvalidOptionParam("option foo", "name");
  559. }
  560. // Verify that negative option code is rejected in the configuration.
  561. TEST_F(Dhcp4ParserTest, optionCodeNegative) {
  562. // Check negative option code -4. This should fail too.
  563. testInvalidOptionParam("-4", "code");
  564. }
  565. // Verify that out of bounds option code is rejected in the configuration.
  566. TEST_F(Dhcp4ParserTest, optionCodeNonUint8) {
  567. // The valid option codes are uint16_t values so passing
  568. // uint16_t maximum value incremented by 1 should result
  569. // in failure.
  570. testInvalidOptionParam("257", "code");
  571. }
  572. // Verify that zero option code is rejected in the configuration.
  573. TEST_F(Dhcp4ParserTest, optionCodeZero) {
  574. // Option code 0 is reserved and should not be accepted
  575. // by configuration parser.
  576. testInvalidOptionParam("0", "code");
  577. }
  578. // Verify that option data which contains non hexadecimal characters
  579. // is rejected by the configuration.
  580. TEST_F(Dhcp4ParserTest, optionDataInvalidChar) {
  581. // Option code 0 is reserved and should not be accepted
  582. // by configuration parser.
  583. testInvalidOptionParam("01020R", "data");
  584. }
  585. // Verify that option data containins '0x' prefix is rejected
  586. // by the configuration.
  587. TEST_F(Dhcp4ParserTest, optionDataUnexpectedPrefix) {
  588. // Option code 0 is reserved and should not be accepted
  589. // by configuration parser.
  590. testInvalidOptionParam("0x0102", "data");
  591. }
  592. // Verify that option data consisting od an odd number of
  593. // hexadecimal digits is rejected in the configuration.
  594. TEST_F(Dhcp4ParserTest, optionDataOddLength) {
  595. // Option code 0 is reserved and should not be accepted
  596. // by configuration parser.
  597. testInvalidOptionParam("123", "data");
  598. }
  599. // Verify that either lower or upper case characters are allowed
  600. // to specify the option data.
  601. TEST_F(Dhcp4ParserTest, optionDataLowerCase) {
  602. ConstElementPtr x;
  603. std::string config = createConfigWithOption("0a0b0C0D", "data");
  604. ElementPtr json = Element::fromJSON(config);
  605. EXPECT_NO_THROW(x = configureDhcp4Server(*srv_, json));
  606. ASSERT_TRUE(x);
  607. comment_ = parseAnswer(rcode_, x);
  608. ASSERT_EQ(0, rcode_);
  609. Subnet4Ptr subnet = CfgMgr::instance().getSubnet4(IOAddress("192.0.2.5"));
  610. ASSERT_TRUE(subnet);
  611. const Subnet::OptionContainer& options = subnet->getOptions();
  612. ASSERT_EQ(1, options.size());
  613. // Get the search index. Index #1 is to search using option code.
  614. const Subnet::OptionContainerTypeIndex& idx = options.get<1>();
  615. // Get the options for specified index. Expecting one option to be
  616. // returned but in theory we may have multiple options with the same
  617. // code so we get the range.
  618. std::pair<Subnet::OptionContainerTypeIndex::const_iterator,
  619. Subnet::OptionContainerTypeIndex::const_iterator> range =
  620. idx.equal_range(56);
  621. // Expect single option with the code equal to 100.
  622. ASSERT_EQ(1, std::distance(range.first, range.second));
  623. const uint8_t foo_expected[] = {
  624. 0x0A, 0x0B, 0x0C, 0x0D
  625. };
  626. // Check if option is valid in terms of code and carried data.
  627. testOption(*range.first, 56, foo_expected, sizeof(foo_expected));
  628. }
  629. /// This test checks if Uint32Parser can really parse the whole range
  630. /// and properly err of out of range values. As we can't call Uint32Parser
  631. /// directly, we are exploiting the fact that it is used to parse global
  632. /// parameter renew-timer and the results are stored in uint32_defaults.
  633. TEST_F(Dhcp4ParserTest, DISABLED_Uint32Parser) {
  634. ConstElementPtr status;
  635. // CASE 1: 0 - minimum value, should work
  636. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_,
  637. Element::fromJSON("{\"version\": 0,"
  638. "\"renew-timer\": 0}")));
  639. // returned value must be ok (0 is a proper value)
  640. checkResult(status, 0);
  641. checkGlobalUint32("renew-timer", 0);
  642. // CASE 2: 4294967295U (UINT_MAX) should work as well
  643. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_,
  644. Element::fromJSON("{\"version\": 0,"
  645. "\"renew-timer\": 4294967295}")));
  646. // returned value must be ok (0 is a proper value)
  647. checkResult(status, 0);
  648. checkGlobalUint32("renew-timer", 4294967295U);
  649. // CASE 3: 4294967296U (UINT_MAX + 1) should not work
  650. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_,
  651. Element::fromJSON("{\"version\": 0,"
  652. "\"renew-timer\": 4294967296}")));
  653. // returned value must be rejected (1 configuration error)
  654. checkResult(status, 1);
  655. // CASE 4: -1 (UINT_MIN -1 ) should not work
  656. EXPECT_NO_THROW(status = configureDhcp4Server(*srv_,
  657. Element::fromJSON("{\"version\": 0,"
  658. "\"renew-timer\": -1}")));
  659. // returned value must be rejected (1 configuration error)
  660. checkResult(status, 1);
  661. }
  662. };