config_parser_unittest.cc 27 KB

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