config_parser_unittest.cc 28 KB

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