config_parser_unittest.cc 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380
  1. // Copyright (C) 2012-2013 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. #include <config.h>
  15. #include <config/ccsession.h>
  16. #include <dhcp/libdhcp++.h>
  17. #include <dhcp/option6_ia.h>
  18. #include <dhcp/iface_mgr.h>
  19. #include <dhcp6/config_parser.h>
  20. #include <dhcp6/dhcp6_srv.h>
  21. #include <dhcpsrv/cfgmgr.h>
  22. #include <dhcpsrv/subnet.h>
  23. #include <boost/foreach.hpp>
  24. #include <gtest/gtest.h>
  25. #include <fstream>
  26. #include <iostream>
  27. #include <sstream>
  28. #include <arpa/inet.h>
  29. using namespace std;
  30. using namespace isc;
  31. using namespace isc::dhcp;
  32. using namespace isc::asiolink;
  33. using namespace isc::data;
  34. using namespace isc::config;
  35. namespace {
  36. class Dhcp6ParserTest : public ::testing::Test {
  37. public:
  38. Dhcp6ParserTest() :rcode_(-1), srv_(0) {
  39. // srv_(0) means to not open any sockets. We don't want to
  40. // deal with sockets here, just check if configuration handling
  41. // is sane.
  42. const IfaceMgr::IfaceCollection& ifaces = IfaceMgr::instance().getIfaces();
  43. // There must be some interface detected
  44. if (ifaces.empty()) {
  45. // We can't use ASSERT in constructor
  46. ADD_FAILURE() << "No interfaces detected.";
  47. }
  48. valid_iface_ = ifaces.begin()->getName();
  49. bogus_iface_ = "nonexisting0";
  50. if (IfaceMgr::instance().getIface(bogus_iface_)) {
  51. ADD_FAILURE() << "The '" << bogus_iface_ << "' exists on this system"
  52. << " while the test assumes that it doesn't, to execute"
  53. << " some negative scenarios. Can't continue this test.";
  54. }
  55. }
  56. ~Dhcp6ParserTest() {
  57. // Reset configuration database after each test.
  58. resetConfiguration();
  59. };
  60. // Checks if config_result (result of DHCP server configuration) has
  61. // expected code (0 for success, other for failures).
  62. // Also stores result in rcode_ and comment_.
  63. void checkResult(ConstElementPtr status, int expected_code) {
  64. ASSERT_TRUE(status);
  65. comment_ = parseAnswer(rcode_, status);
  66. EXPECT_EQ(expected_code, rcode_);
  67. }
  68. /// @brief Create the simple configuration with single option.
  69. ///
  70. /// This function allows to set one of the parameters that configure
  71. /// option value. These parameters are: "name", "code", "data" and
  72. /// "csv-format".
  73. ///
  74. /// @param param_value string holiding option parameter value to be
  75. /// injected into the configuration string.
  76. /// @param parameter name of the parameter to be configured with
  77. /// param value.
  78. std::string createConfigWithOption(const std::string& param_value,
  79. const std::string& parameter) {
  80. std::map<std::string, std::string> params;
  81. if (parameter == "name") {
  82. params["name"] = param_value;
  83. params["space"] = "dhcp6";
  84. params["code"] = "38";
  85. params["data"] = "AB CDEF0105";
  86. params["csv-format"] = "False";
  87. } else if (parameter == "space") {
  88. params["name"] = "subscriber-id";
  89. params["space"] = param_value;
  90. params["code"] = "38";
  91. params["data"] = "AB CDEF0105";
  92. params["csv-format"] = "False";
  93. } else if (parameter == "code") {
  94. params["name"] = "subscriber-id";
  95. params["space"] = "dhcp6";
  96. params["code"] = param_value;
  97. params["data"] = "AB CDEF0105";
  98. params["csv-format"] = "False";
  99. } else if (parameter == "data") {
  100. params["name"] = "subscriber-id";
  101. params["space"] = "dhcp6";
  102. params["code"] = "38";
  103. params["data"] = param_value;
  104. params["csv-format"] = "False";
  105. } else if (parameter == "csv-format") {
  106. params["name"] = "subscriber-id";
  107. params["space"] = "dhcp6";
  108. params["code"] = "38";
  109. params["data"] = "AB CDEF0105";
  110. params["csv-format"] = param_value;
  111. }
  112. return (createConfigWithOption(params));
  113. }
  114. std::string createConfigWithOption(const std::map<std::string,
  115. std::string>& params)
  116. {
  117. std::ostringstream stream;
  118. stream << "{ \"interface\": [ \"all\" ],"
  119. "\"preferred-lifetime\": 3000,"
  120. "\"rebind-timer\": 2000, "
  121. "\"renew-timer\": 1000, "
  122. "\"subnet6\": [ { "
  123. " \"pool\": [ \"2001:db8:1::/80\" ],"
  124. " \"subnet\": \"2001:db8:1::/64\", "
  125. " \"option-data\": [ {";
  126. bool first = true;
  127. typedef std::pair<std::string, std::string> ParamPair;
  128. BOOST_FOREACH(ParamPair param, params) {
  129. if (!first) {
  130. stream << ", ";
  131. } else {
  132. // cppcheck-suppress unreadVariable
  133. first = false;
  134. }
  135. if (param.first == "name") {
  136. stream << "\"name\": \"" << param.second << "\"";
  137. } else if (param.first == "space") {
  138. stream << "\"space\": \"" << param.second << "\"";
  139. } else if (param.first == "code") {
  140. stream << "\"code\": " << param.second;;
  141. } else if (param.first == "data") {
  142. stream << "\"data\": \"" << param.second << "\"";
  143. } else if (param.first == "csv-format") {
  144. stream << "\"csv-format\": " << param.second;
  145. }
  146. }
  147. stream <<
  148. " } ]"
  149. " } ],"
  150. "\"valid-lifetime\": 4000 }";
  151. return (stream.str());
  152. }
  153. /// @brief Reset configuration database.
  154. ///
  155. /// This function resets configuration data base by
  156. /// removing all subnets and option-data. Reset must
  157. /// be performed after each test to make sure that
  158. /// contents of the database do not affect result of
  159. /// subsequent tests.
  160. void resetConfiguration() {
  161. ConstElementPtr status;
  162. string config = "{ \"interface\": [ \"all\" ],"
  163. "\"preferred-lifetime\": 3000,"
  164. "\"rebind-timer\": 2000, "
  165. "\"renew-timer\": 1000, "
  166. "\"valid-lifetime\": 4000, "
  167. "\"subnet6\": [ ], "
  168. "\"option-def\": [ ], "
  169. "\"option-data\": [ ] }";
  170. try {
  171. ElementPtr json = Element::fromJSON(config);
  172. status = configureDhcp6Server(srv_, json);
  173. } catch (const std::exception& ex) {
  174. FAIL() << "Fatal error: unable to reset configuration database"
  175. << " after the test. The following configuration was used"
  176. << " to reset database: " << std::endl
  177. << config << std::endl
  178. << " and the following error message was returned:"
  179. << ex.what() << std::endl;
  180. }
  181. // status object must not be NULL
  182. if (!status) {
  183. FAIL() << "Fatal error: unable to reset configuration database"
  184. << " after the test. Configuration function returned"
  185. << " NULL pointer" << std::endl;
  186. }
  187. comment_ = parseAnswer(rcode_, status);
  188. // returned value should be 0 (configuration success)
  189. if (rcode_ != 0) {
  190. FAIL() << "Fatal error: unable to reset configuration database"
  191. << " after the test. Configuration function returned"
  192. << " error code " << rcode_ << std::endl;
  193. }
  194. }
  195. /// @brief Test invalid option parameter value.
  196. ///
  197. /// This test function constructs the simple configuration
  198. /// string and injects invalid option configuration into it.
  199. /// It expects that parser will fail with provided option code.
  200. ///
  201. /// @param param_value string holding invalid option parameter value
  202. /// to be injected into configuration string.
  203. /// @param parameter name of the parameter to be configured with
  204. /// param_value (can be any of "name", "code", "data")
  205. void testInvalidOptionParam(const std::string& param_value,
  206. const std::string& parameter) {
  207. ConstElementPtr x;
  208. std::string config = createConfigWithOption(param_value, parameter);
  209. ElementPtr json = Element::fromJSON(config);
  210. EXPECT_NO_THROW(x = configureDhcp6Server(srv_, json));
  211. ASSERT_TRUE(x);
  212. comment_ = parseAnswer(rcode_, x);
  213. ASSERT_EQ(1, rcode_);
  214. }
  215. /// @brief Test option against given code and data.
  216. ///
  217. /// @param option_desc option descriptor that carries the option to
  218. /// be tested.
  219. /// @param expected_code expected code of the option.
  220. /// @param expected_data expected data in the option.
  221. /// @param expected_data_len length of the reference data.
  222. /// @param extra_data if true extra data is allowed in an option
  223. /// after tested data.
  224. void testOption(const Subnet::OptionDescriptor& option_desc,
  225. uint16_t expected_code, const uint8_t* expected_data,
  226. size_t expected_data_len,
  227. bool extra_data = false) {
  228. // Check if option descriptor contains valid option pointer.
  229. ASSERT_TRUE(option_desc.option);
  230. // Verify option type.
  231. EXPECT_EQ(expected_code, option_desc.option->getType());
  232. // We may have many different option types being created. Some of them
  233. // have dedicated classes derived from Option class. In such case if
  234. // we want to verify the option contents against expected_data we have
  235. // to prepare raw buffer with the contents of the option. The easiest
  236. // way is to call pack() which will prepare on-wire data.
  237. util::OutputBuffer buf(option_desc.option->getData().size());
  238. option_desc.option->pack(buf);
  239. if (extra_data) {
  240. // The length of the buffer must be at least equal to size of the
  241. // reference data but it can sometimes be greater than that. This is
  242. // because some options carry suboptions that increase the overall
  243. // length.
  244. ASSERT_GE(buf.getLength() - option_desc.option->getHeaderLen(),
  245. expected_data_len);
  246. } else {
  247. ASSERT_EQ(buf.getLength() - option_desc.option->getHeaderLen(),
  248. expected_data_len);
  249. }
  250. // Verify that the data is correct. Do not verify suboptions and a header.
  251. const uint8_t* data = static_cast<const uint8_t*>(buf.getData());
  252. EXPECT_EQ(0, memcmp(expected_data, data + option_desc.option->getHeaderLen(),
  253. expected_data_len));
  254. }
  255. Dhcpv6Srv srv_;
  256. int rcode_;
  257. ConstElementPtr comment_;
  258. string valid_iface_;
  259. string bogus_iface_;
  260. };
  261. // Goal of this test is a verification if a very simple config update
  262. // with just a bumped version number. That's the simplest possible
  263. // config update.
  264. TEST_F(Dhcp6ParserTest, version) {
  265. ConstElementPtr x;
  266. EXPECT_NO_THROW(x = configureDhcp6Server(srv_,
  267. Element::fromJSON("{\"version\": 0}")));
  268. // returned value must be 0 (configuration accepted)
  269. ASSERT_TRUE(x);
  270. comment_ = parseAnswer(rcode_, x);
  271. EXPECT_EQ(0, rcode_);
  272. }
  273. /// The goal of this test is to verify that the code accepts only
  274. /// valid commands and malformed or unsupported parameters are rejected.
  275. TEST_F(Dhcp6ParserTest, bogusCommand) {
  276. ConstElementPtr x;
  277. EXPECT_NO_THROW(x = configureDhcp6Server(srv_,
  278. Element::fromJSON("{\"bogus\": 5}")));
  279. // returned value must be 1 (configuration parse error)
  280. ASSERT_TRUE(x);
  281. comment_ = parseAnswer(rcode_, x);
  282. EXPECT_EQ(1, rcode_);
  283. }
  284. /// The goal of this test is to verify if configuration without any
  285. /// subnets defined can be accepted.
  286. TEST_F(Dhcp6ParserTest, emptySubnet) {
  287. ConstElementPtr status;
  288. EXPECT_NO_THROW(status = configureDhcp6Server(srv_,
  289. Element::fromJSON("{ \"interface\": [ \"all\" ],"
  290. "\"preferred-lifetime\": 3000,"
  291. "\"rebind-timer\": 2000, "
  292. "\"renew-timer\": 1000, "
  293. "\"subnet6\": [ ], "
  294. "\"valid-lifetime\": 4000 }")));
  295. // returned value should be 0 (success)
  296. ASSERT_TRUE(status);
  297. comment_ = parseAnswer(rcode_, status);
  298. EXPECT_EQ(0, rcode_);
  299. }
  300. /// The goal of this test is to verify if defined subnet uses global
  301. /// parameter timer definitions.
  302. TEST_F(Dhcp6ParserTest, subnetGlobalDefaults) {
  303. ConstElementPtr status;
  304. string config = "{ \"interface\": [ \"all\" ],"
  305. "\"preferred-lifetime\": 3000,"
  306. "\"rebind-timer\": 2000, "
  307. "\"renew-timer\": 1000, "
  308. "\"subnet6\": [ { "
  309. " \"pool\": [ \"2001:db8:1::1 - 2001:db8:1::ffff\" ],"
  310. " \"subnet\": \"2001:db8:1::/64\" } ],"
  311. "\"valid-lifetime\": 4000 }";
  312. cout << config << endl;
  313. ElementPtr json = Element::fromJSON(config);
  314. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  315. // check if returned status is OK
  316. ASSERT_TRUE(status);
  317. comment_ = parseAnswer(rcode_, status);
  318. EXPECT_EQ(0, rcode_);
  319. // Now check if the configuration was indeed handled and we have
  320. // expected pool configured.
  321. Subnet6Ptr subnet = CfgMgr::instance().getSubnet6(IOAddress("2001:db8:1::5"));
  322. ASSERT_TRUE(subnet);
  323. EXPECT_EQ(1000, subnet->getT1());
  324. EXPECT_EQ(2000, subnet->getT2());
  325. EXPECT_EQ(3000, subnet->getPreferred());
  326. EXPECT_EQ(4000, subnet->getValid());
  327. }
  328. // This test checks if it is possible to override global values
  329. // on a per subnet basis.
  330. TEST_F(Dhcp6ParserTest, subnetLocal) {
  331. ConstElementPtr status;
  332. string config = "{ \"interface\": [ \"all\" ],"
  333. "\"preferred-lifetime\": 3000,"
  334. "\"rebind-timer\": 2000, "
  335. "\"renew-timer\": 1000, "
  336. "\"subnet6\": [ { "
  337. " \"pool\": [ \"2001:db8:1::1 - 2001:db8:1::ffff\" ],"
  338. " \"renew-timer\": 1, "
  339. " \"rebind-timer\": 2, "
  340. " \"preferred-lifetime\": 3,"
  341. " \"valid-lifetime\": 4,"
  342. " \"subnet\": \"2001:db8:1::/64\" } ],"
  343. "\"valid-lifetime\": 4000 }";
  344. cout << config << endl;
  345. ElementPtr json = Element::fromJSON(config);
  346. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  347. // returned value should be 0 (configuration success)
  348. ASSERT_TRUE(status);
  349. comment_ = parseAnswer(rcode_, status);
  350. EXPECT_EQ(0, rcode_);
  351. Subnet6Ptr subnet = CfgMgr::instance().getSubnet6(IOAddress("2001:db8:1::5"));
  352. ASSERT_TRUE(subnet);
  353. EXPECT_EQ(1, subnet->getT1());
  354. EXPECT_EQ(2, subnet->getT2());
  355. EXPECT_EQ(3, subnet->getPreferred());
  356. EXPECT_EQ(4, subnet->getValid());
  357. }
  358. // This test checks if it is possible to define a subnet with an
  359. // interface defined.
  360. TEST_F(Dhcp6ParserTest, subnetInterface) {
  361. ConstElementPtr status;
  362. // There should be at least one interface
  363. string config = "{ \"interface\": [ \"all\" ],"
  364. "\"preferred-lifetime\": 3000,"
  365. "\"rebind-timer\": 2000, "
  366. "\"renew-timer\": 1000, "
  367. "\"subnet6\": [ { "
  368. " \"pool\": [ \"2001:db8:1::1 - 2001:db8:1::ffff\" ],"
  369. " \"interface\": \"" + valid_iface_ + "\","
  370. " \"subnet\": \"2001:db8:1::/64\" } ],"
  371. "\"valid-lifetime\": 4000 }";
  372. cout << config << endl;
  373. ElementPtr json = Element::fromJSON(config);
  374. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  375. // returned value should be 0 (configuration success)
  376. ASSERT_TRUE(status);
  377. comment_ = parseAnswer(rcode_, status);
  378. EXPECT_EQ(0, rcode_);
  379. Subnet6Ptr subnet = CfgMgr::instance().getSubnet6(IOAddress("2001:db8:1::5"));
  380. ASSERT_TRUE(subnet);
  381. EXPECT_EQ(valid_iface_, subnet->getIface());
  382. }
  383. // This test checks if invalid interface name will be rejected in
  384. // Subnet6 definition.
  385. TEST_F(Dhcp6ParserTest, subnetInterfaceBogus) {
  386. ConstElementPtr status;
  387. // There should be at least one interface
  388. string config = "{ \"interface\": [ \"all\" ],"
  389. "\"preferred-lifetime\": 3000,"
  390. "\"rebind-timer\": 2000, "
  391. "\"renew-timer\": 1000, "
  392. "\"subnet6\": [ { "
  393. " \"pool\": [ \"2001:db8:1::1 - 2001:db8:1::ffff\" ],"
  394. " \"interface\": \"" + bogus_iface_ + "\","
  395. " \"subnet\": \"2001:db8:1::/64\" } ],"
  396. "\"valid-lifetime\": 4000 }";
  397. cout << config << endl;
  398. ElementPtr json = Element::fromJSON(config);
  399. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  400. // returned value should be 1 (configuration error)
  401. ASSERT_TRUE(status);
  402. comment_ = parseAnswer(rcode_, status);
  403. EXPECT_EQ(1, rcode_);
  404. Subnet6Ptr subnet = CfgMgr::instance().getSubnet6(IOAddress("2001:db8:1::5"));
  405. EXPECT_FALSE(subnet);
  406. }
  407. // This test checks if it is not allowed to define global interface
  408. // parameter.
  409. TEST_F(Dhcp6ParserTest, interfaceGlobal) {
  410. ConstElementPtr status;
  411. string config = "{ \"interface\": [ \"all\" ],"
  412. "\"preferred-lifetime\": 3000,"
  413. "\"rebind-timer\": 2000, "
  414. "\"renew-timer\": 1000, "
  415. "\"interface\": \"" + valid_iface_ + "\"," // Not valid. Can be defined in subnet only
  416. "\"subnet6\": [ { "
  417. " \"pool\": [ \"2001:db8:1::1 - 2001:db8:1::ffff\" ],"
  418. " \"subnet\": \"2001:db8:1::/64\" } ],"
  419. "\"valid-lifetime\": 4000 }";
  420. cout << config << endl;
  421. ElementPtr json = Element::fromJSON(config);
  422. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  423. // returned value should be 1 (parse error)
  424. ASSERT_TRUE(status);
  425. comment_ = parseAnswer(rcode_, status);
  426. EXPECT_EQ(1, rcode_);
  427. }
  428. // Test verifies that a subnet with pool values that do not belong to that
  429. // pool are rejected.
  430. TEST_F(Dhcp6ParserTest, poolOutOfSubnet) {
  431. ConstElementPtr status;
  432. string config = "{ \"interface\": [ \"all\" ],"
  433. "\"preferred-lifetime\": 3000,"
  434. "\"rebind-timer\": 2000, "
  435. "\"renew-timer\": 1000, "
  436. "\"subnet6\": [ { "
  437. " \"pool\": [ \"4001:db8:1::/80\" ],"
  438. " \"subnet\": \"2001:db8:1::/64\" } ],"
  439. "\"valid-lifetime\": 4000 }";
  440. cout << config << endl;
  441. ElementPtr json = Element::fromJSON(config);
  442. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  443. // returned value must be 1 (values error)
  444. // as the pool does not belong to that subnet
  445. ASSERT_TRUE(status);
  446. comment_ = parseAnswer(rcode_, status);
  447. EXPECT_EQ(1, rcode_);
  448. }
  449. // Goal of this test is to verify if pools can be defined
  450. // using prefix/length notation. There is no separate test for min-max
  451. // notation as it was tested in several previous tests.
  452. TEST_F(Dhcp6ParserTest, poolPrefixLen) {
  453. ConstElementPtr x;
  454. string config = "{ \"interface\": [ \"all\" ],"
  455. "\"preferred-lifetime\": 3000,"
  456. "\"rebind-timer\": 2000, "
  457. "\"renew-timer\": 1000, "
  458. "\"subnet6\": [ { "
  459. " \"pool\": [ \"2001:db8:1::/80\" ],"
  460. " \"subnet\": \"2001:db8:1::/64\" } ],"
  461. "\"valid-lifetime\": 4000 }";
  462. cout << config << endl;
  463. ElementPtr json = Element::fromJSON(config);
  464. EXPECT_NO_THROW(x = configureDhcp6Server(srv_, json));
  465. // returned value must be 1 (configuration parse error)
  466. ASSERT_TRUE(x);
  467. comment_ = parseAnswer(rcode_, x);
  468. EXPECT_EQ(0, rcode_);
  469. Subnet6Ptr subnet = CfgMgr::instance().getSubnet6(IOAddress("2001:db8:1::5"));
  470. ASSERT_TRUE(subnet);
  471. EXPECT_EQ(1000, subnet->getT1());
  472. EXPECT_EQ(2000, subnet->getT2());
  473. EXPECT_EQ(3000, subnet->getPreferred());
  474. EXPECT_EQ(4000, subnet->getValid());
  475. }
  476. // The goal of this test is to check whether an option definition
  477. // that defines an option carrying an IPv6 address can be created.
  478. TEST_F(Dhcp6ParserTest, optionDefIpv6Address) {
  479. // Configuration string.
  480. std::string config =
  481. "{ \"option-def\": [ {"
  482. " \"name\": \"foo\","
  483. " \"code\": 100,"
  484. " \"type\": \"ipv6-address\","
  485. " \"array\": False,"
  486. " \"record-types\": \"\","
  487. " \"space\": \"isc\""
  488. " } ]"
  489. "}";
  490. ElementPtr json = Element::fromJSON(config);
  491. // Make sure that the particular option definition does not exist.
  492. OptionDefinitionPtr def = CfgMgr::instance().getOptionDef("isc", 100);
  493. ASSERT_FALSE(def);
  494. // Use the configuration string to create new option definition.
  495. ConstElementPtr status;
  496. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  497. ASSERT_TRUE(status);
  498. // The option definition should now be available in the CfgMgr.
  499. def = CfgMgr::instance().getOptionDef("isc", 100);
  500. ASSERT_TRUE(def);
  501. // Verify that the option definition data is valid.
  502. EXPECT_EQ("foo", def->getName());
  503. EXPECT_EQ(100, def->getCode());
  504. EXPECT_FALSE(def->getArrayType());
  505. EXPECT_EQ(OPT_IPV6_ADDRESS_TYPE, def->getType());
  506. }
  507. // The goal of this test is to check whether an option definiiton
  508. // that defines an option carrying a record of data fields can
  509. // be created.
  510. TEST_F(Dhcp6ParserTest, optionDefRecord) {
  511. // Configuration string.
  512. std::string config =
  513. "{ \"option-def\": [ {"
  514. " \"name\": \"foo\","
  515. " \"code\": 100,"
  516. " \"type\": \"record\","
  517. " \"array\": False,"
  518. " \"record-types\": \"uint16, ipv4-address, ipv6-address, string\","
  519. " \"space\": \"isc\""
  520. " } ]"
  521. "}";
  522. ElementPtr json = Element::fromJSON(config);
  523. // Make sure that the particular option definition does not exist.
  524. OptionDefinitionPtr def = CfgMgr::instance().getOptionDef("isc", 100);
  525. ASSERT_FALSE(def);
  526. // Use the configuration string to create new option definition.
  527. ConstElementPtr status;
  528. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  529. ASSERT_TRUE(status);
  530. checkResult(status, 0);
  531. // The option definition should now be available in the CfgMgr.
  532. def = CfgMgr::instance().getOptionDef("isc", 100);
  533. ASSERT_TRUE(def);
  534. // Check the option data.
  535. EXPECT_EQ("foo", def->getName());
  536. EXPECT_EQ(100, def->getCode());
  537. EXPECT_EQ(OPT_RECORD_TYPE, def->getType());
  538. EXPECT_FALSE(def->getArrayType());
  539. // The option comprises the record of data fields. Verify that all
  540. // fields are present and they are of the expected types.
  541. const OptionDefinition::RecordFieldsCollection& record_fields =
  542. def->getRecordFields();
  543. ASSERT_EQ(4, record_fields.size());
  544. EXPECT_EQ(OPT_UINT16_TYPE, record_fields[0]);
  545. EXPECT_EQ(OPT_IPV4_ADDRESS_TYPE, record_fields[1]);
  546. EXPECT_EQ(OPT_IPV6_ADDRESS_TYPE, record_fields[2]);
  547. EXPECT_EQ(OPT_STRING_TYPE, record_fields[3]);
  548. }
  549. // The goal of this test is to verify that multiple option definitions
  550. // can be created.
  551. TEST_F(Dhcp6ParserTest, optionDefMultiple) {
  552. // Configuration string.
  553. std::string config =
  554. "{ \"option-def\": [ {"
  555. " \"name\": \"foo\","
  556. " \"code\": 100,"
  557. " \"type\": \"uint32\","
  558. " \"array\": False,"
  559. " \"record-types\": \"\","
  560. " \"space\": \"isc\""
  561. " },"
  562. " {"
  563. " \"name\": \"foo-2\","
  564. " \"code\": 101,"
  565. " \"type\": \"ipv4-address\","
  566. " \"array\": False,"
  567. " \"record-types\": \"\","
  568. " \"space\": \"isc\""
  569. " } ]"
  570. "}";
  571. ElementPtr json = Element::fromJSON(config);
  572. // Make sure that the option definitions do not exist yet.
  573. ASSERT_FALSE(CfgMgr::instance().getOptionDef("isc", 100));
  574. ASSERT_FALSE(CfgMgr::instance().getOptionDef("isc", 101));
  575. // Use the configuration string to create new option definitions.
  576. ConstElementPtr status;
  577. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  578. ASSERT_TRUE(status);
  579. checkResult(status, 0);
  580. // Check the first definition we have created.
  581. OptionDefinitionPtr def1 = CfgMgr::instance().getOptionDef("isc", 100);
  582. ASSERT_TRUE(def1);
  583. // Check the option data.
  584. EXPECT_EQ("foo", def1->getName());
  585. EXPECT_EQ(100, def1->getCode());
  586. EXPECT_EQ(OPT_UINT32_TYPE, def1->getType());
  587. EXPECT_FALSE(def1->getArrayType());
  588. // Check the second option definition we have created.
  589. OptionDefinitionPtr def2 = CfgMgr::instance().getOptionDef("isc", 101);
  590. ASSERT_TRUE(def2);
  591. // Check the option data.
  592. EXPECT_EQ("foo-2", def2->getName());
  593. EXPECT_EQ(101, def2->getCode());
  594. EXPECT_EQ(OPT_IPV4_ADDRESS_TYPE, def2->getType());
  595. EXPECT_FALSE(def2->getArrayType());
  596. }
  597. // The goal of this test is to verify that the duplicated option
  598. // definition is not accepted.
  599. TEST_F(Dhcp6ParserTest, optionDefDuplicate) {
  600. // Configuration string. Both option definitions have
  601. // the same code and belong to the same option space.
  602. // This configuration should not be accepted.
  603. std::string config =
  604. "{ \"option-def\": [ {"
  605. " \"name\": \"foo\","
  606. " \"code\": 100,"
  607. " \"type\": \"uint32\","
  608. " \"array\": False,"
  609. " \"record-types\": \"\","
  610. " \"space\": \"isc\""
  611. " },"
  612. " {"
  613. " \"name\": \"foo-2\","
  614. " \"code\": 100,"
  615. " \"type\": \"ipv4-address\","
  616. " \"array\": False,"
  617. " \"record-types\": \"\","
  618. " \"space\": \"isc\""
  619. " } ]"
  620. "}";
  621. ElementPtr json = Element::fromJSON(config);
  622. // Make sure that the option definition does not exist yet.
  623. ASSERT_FALSE(CfgMgr::instance().getOptionDef("isc", 100));
  624. // Use the configuration string to create new option definitions.
  625. ConstElementPtr status;
  626. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  627. ASSERT_TRUE(status);
  628. checkResult(status, 1);
  629. }
  630. // The goal of this test is to verify that the option definition
  631. // comprising an array of uint32 values can be created.
  632. TEST_F(Dhcp6ParserTest, optionDefArray) {
  633. // Configuration string. Created option definition should
  634. // comprise an array of uint32 values.
  635. std::string config =
  636. "{ \"option-def\": [ {"
  637. " \"name\": \"foo\","
  638. " \"code\": 100,"
  639. " \"type\": \"uint32\","
  640. " \"array\": True,"
  641. " \"record-types\": \"\","
  642. " \"space\": \"isc\""
  643. " } ]"
  644. "}";
  645. ElementPtr json = Element::fromJSON(config);
  646. // Make sure that the particular option definition does not exist.
  647. OptionDefinitionPtr def = CfgMgr::instance().getOptionDef("isc", 100);
  648. ASSERT_FALSE(def);
  649. // Use the configuration string to create new option definition.
  650. ConstElementPtr status;
  651. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  652. ASSERT_TRUE(status);
  653. checkResult(status, 0);
  654. // The option definition should now be available in the CfgMgr.
  655. def = CfgMgr::instance().getOptionDef("isc", 100);
  656. ASSERT_TRUE(def);
  657. // Check the option data.
  658. EXPECT_EQ("foo", def->getName());
  659. EXPECT_EQ(100, def->getCode());
  660. EXPECT_EQ(OPT_UINT32_TYPE, def->getType());
  661. EXPECT_TRUE(def->getArrayType());
  662. }
  663. /// The purpose of this test is to verify that the option definition
  664. /// with invalid name is not accepted.
  665. TEST_F(Dhcp6ParserTest, optionDefInvalidName) {
  666. // Configuration string. The option name is invalid as it
  667. // contains the % character.
  668. std::string config =
  669. "{ \"option-def\": [ {"
  670. " \"name\": \"invalid%name\","
  671. " \"code\": 100,"
  672. " \"type\": \"string\","
  673. " \"array\": False,"
  674. " \"record-types\": \"\","
  675. " \"space\": \"isc\""
  676. " } ]"
  677. "}";
  678. ElementPtr json = Element::fromJSON(config);
  679. // Use the configuration string to create new option definition.
  680. ConstElementPtr status;
  681. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  682. ASSERT_TRUE(status);
  683. // Expecting parsing error (error code 1).
  684. checkResult(status, 1);
  685. }
  686. /// The purpose of this test is to verify that the option definition
  687. /// with invalid type is not accepted.
  688. TEST_F(Dhcp6ParserTest, optionDefInvalidType) {
  689. // Configuration string. The option type is invalid. It is
  690. // "sting" instead of "string".
  691. std::string config =
  692. "{ \"option-def\": [ {"
  693. " \"name\": \"foo\","
  694. " \"code\": 100,"
  695. " \"type\": \"sting\","
  696. " \"array\": False,"
  697. " \"record-types\": \"\","
  698. " \"space\": \"isc\""
  699. " } ]"
  700. "}";
  701. ElementPtr json = Element::fromJSON(config);
  702. // Use the configuration string to create new option definition.
  703. ConstElementPtr status;
  704. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  705. ASSERT_TRUE(status);
  706. // Expecting parsing error (error code 1).
  707. checkResult(status, 1);
  708. }
  709. /// The purpose of this test is to verify that the option definition
  710. /// with invalid type is not accepted.
  711. TEST_F(Dhcp6ParserTest, optionDefInvalidRecordType) {
  712. // Configuration string. The third of the record fields
  713. // is invalid. It is "sting" instead of "string".
  714. std::string config =
  715. "{ \"option-def\": [ {"
  716. " \"name\": \"foo\","
  717. " \"code\": 100,"
  718. " \"type\": \"record\","
  719. " \"array\": False,"
  720. " \"record-types\": \"uint32,uint8,sting\","
  721. " \"space\": \"isc\""
  722. " } ]"
  723. "}";
  724. ElementPtr json = Element::fromJSON(config);
  725. // Use the configuration string to create new option definition.
  726. ConstElementPtr status;
  727. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  728. ASSERT_TRUE(status);
  729. // Expecting parsing error (error code 1).
  730. checkResult(status, 1);
  731. }
  732. /// The purpose of this test is to verify that it is not allowed
  733. /// to override the standard option (that belongs to dhcp6 option
  734. /// space) and that it is allowed to define option in the dhcp6
  735. /// option space that has a code which is not used by any of the
  736. /// standard options.
  737. TEST_F(Dhcp6ParserTest, optionStandardDefOverride) {
  738. // Configuration string. The option code 100 is unassigned
  739. // so it can be used for a custom option definition in
  740. // dhcp6 option space.
  741. std::string config =
  742. "{ \"option-def\": [ {"
  743. " \"name\": \"foo\","
  744. " \"code\": 100,"
  745. " \"type\": \"string\","
  746. " \"array\": False,"
  747. " \"record-types\": \"\","
  748. " \"space\": \"dhcp6\""
  749. " } ]"
  750. "}";
  751. ElementPtr json = Element::fromJSON(config);
  752. OptionDefinitionPtr def = CfgMgr::instance().getOptionDef("dhcp6", 100);
  753. ASSERT_FALSE(def);
  754. // Use the configuration string to create new option definition.
  755. ConstElementPtr status;
  756. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  757. ASSERT_TRUE(status);
  758. checkResult(status, 0);
  759. // The option definition should now be available in the CfgMgr.
  760. def = CfgMgr::instance().getOptionDef("dhcp6", 100);
  761. ASSERT_TRUE(def);
  762. // Check the option data.
  763. EXPECT_EQ("foo", def->getName());
  764. EXPECT_EQ(100, def->getCode());
  765. EXPECT_EQ(OPT_STRING_TYPE, def->getType());
  766. EXPECT_FALSE(def->getArrayType());
  767. // The combination of option space and code is
  768. // invalid. The 'dhcp6' option space groups
  769. // standard options and the code 3 is reserved
  770. // for one of them.
  771. config =
  772. "{ \"option-def\": [ {"
  773. " \"name\": \"foo\","
  774. " \"code\": 3,"
  775. " \"type\": \"string\","
  776. " \"array\": False,"
  777. " \"record-types\": \"\","
  778. " \"space\": \"dhcp6\""
  779. " } ]"
  780. "}";
  781. json = Element::fromJSON(config);
  782. // Use the configuration string to create new option definition.
  783. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  784. ASSERT_TRUE(status);
  785. // Expecting parsing error (error code 1).
  786. checkResult(status, 1);
  787. }
  788. // Goal of this test is to verify that global option
  789. // data is configured for the subnet if the subnet
  790. // configuration does not include options configuration.
  791. TEST_F(Dhcp6ParserTest, optionDataDefaults) {
  792. ConstElementPtr x;
  793. string config = "{ \"interface\": [ \"all\" ],"
  794. "\"preferred-lifetime\": 3000,"
  795. "\"rebind-timer\": 2000,"
  796. "\"renew-timer\": 1000,"
  797. "\"option-data\": [ {"
  798. " \"name\": \"subscriber-id\","
  799. " \"space\": \"dhcp6\","
  800. " \"code\": 38,"
  801. " \"data\": \"AB CDEF0105\","
  802. " \"csv-format\": False"
  803. " },"
  804. " {"
  805. " \"name\": \"preference\","
  806. " \"space\": \"dhcp6\","
  807. " \"code\": 7,"
  808. " \"data\": \"01\","
  809. " \"csv-format\": True"
  810. " } ],"
  811. "\"subnet6\": [ { "
  812. " \"pool\": [ \"2001:db8:1::/80\" ],"
  813. " \"subnet\": \"2001:db8:1::/64\""
  814. " } ],"
  815. "\"valid-lifetime\": 4000 }";
  816. ElementPtr json = Element::fromJSON(config);
  817. EXPECT_NO_THROW(x = configureDhcp6Server(srv_, json));
  818. ASSERT_TRUE(x);
  819. comment_ = parseAnswer(rcode_, x);
  820. ASSERT_EQ(0, rcode_);
  821. Subnet6Ptr subnet = CfgMgr::instance().getSubnet6(IOAddress("2001:db8:1::5"));
  822. ASSERT_TRUE(subnet);
  823. Subnet::OptionContainerPtr options = subnet->getOptionDescriptors("dhcp6");
  824. ASSERT_EQ(2, options->size());
  825. // Get the search index. Index #1 is to search using option code.
  826. const Subnet::OptionContainerTypeIndex& idx = options->get<1>();
  827. // Get the options for specified index. Expecting one option to be
  828. // returned but in theory we may have multiple options with the same
  829. // code so we get the range.
  830. std::pair<Subnet::OptionContainerTypeIndex::const_iterator,
  831. Subnet::OptionContainerTypeIndex::const_iterator> range =
  832. idx.equal_range(D6O_SUBSCRIBER_ID);
  833. // Expect single option with the code equal to 38.
  834. ASSERT_EQ(1, std::distance(range.first, range.second));
  835. const uint8_t subid_expected[] = {
  836. 0xAB, 0xCD, 0xEF, 0x01, 0x05
  837. };
  838. // Check if option is valid in terms of code and carried data.
  839. testOption(*range.first, D6O_SUBSCRIBER_ID, subid_expected,
  840. sizeof(subid_expected));
  841. range = idx.equal_range(D6O_PREFERENCE);
  842. ASSERT_EQ(1, std::distance(range.first, range.second));
  843. // Do another round of testing with second option.
  844. const uint8_t pref_expected[] = {
  845. 0x01
  846. };
  847. testOption(*range.first, D6O_PREFERENCE, pref_expected,
  848. sizeof(pref_expected));
  849. // Check that options with other option codes are not returned.
  850. for (uint16_t code = 47; code < 57; ++code) {
  851. range = idx.equal_range(code);
  852. EXPECT_EQ(0, std::distance(range.first, range.second));
  853. }
  854. }
  855. /// The goal of this test is to verify that two options having the same
  856. /// option code can be added to different option spaces.
  857. TEST_F(Dhcp6ParserTest, optionDataTwoSpaces) {
  858. // This configuration string is to configure two options
  859. // sharing the code 56 and having different definitions
  860. // and belonging to the different option spaces.
  861. // The option definition must be provided for the
  862. // option that belongs to the 'isc' option space.
  863. // The definition is not required for the option that
  864. // belongs to the 'dhcp6' option space as it is the
  865. // standard option.
  866. string config = "{ \"interface\": [ \"all\" ],"
  867. "\"rebind-timer\": 2000,"
  868. "\"renew-timer\": 1000,"
  869. "\"option-data\": [ {"
  870. " \"name\": \"subscriber-id\","
  871. " \"space\": \"dhcp6\","
  872. " \"code\": 38,"
  873. " \"data\": \"AB CDEF0105\","
  874. " \"csv-format\": False"
  875. " },"
  876. " {"
  877. " \"name\": \"foo\","
  878. " \"space\": \"isc\","
  879. " \"code\": 38,"
  880. " \"data\": \"1234\","
  881. " \"csv-format\": True"
  882. " } ],"
  883. "\"option-def\": [ {"
  884. " \"name\": \"foo\","
  885. " \"code\": 38,"
  886. " \"type\": \"uint32\","
  887. " \"array\": False,"
  888. " \"record-types\": \"\","
  889. " \"space\": \"isc\""
  890. " } ],"
  891. "\"subnet6\": [ { "
  892. " \"pool\": [ \"2001:db8:1::/80\" ],"
  893. " \"subnet\": \"2001:db8:1::/64\""
  894. " } ]"
  895. "}";
  896. ConstElementPtr status;
  897. ElementPtr json = Element::fromJSON(config);
  898. EXPECT_NO_THROW(status = configureDhcp6Server(srv_, json));
  899. ASSERT_TRUE(status);
  900. checkResult(status, 0);
  901. // Options should be now availabe for the subnet.
  902. Subnet6Ptr subnet = CfgMgr::instance().getSubnet6(IOAddress("2001:db8:1::5"));
  903. ASSERT_TRUE(subnet);
  904. // Try to get the option from the space dhcp6.
  905. Subnet::OptionDescriptor desc1 = subnet->getOptionDescriptor("dhcp6", 38);
  906. ASSERT_TRUE(desc1.option);
  907. EXPECT_EQ(38, desc1.option->getType());
  908. // Try to get the option from the space isc.
  909. Subnet::OptionDescriptor desc2 = subnet->getOptionDescriptor("isc", 38);
  910. ASSERT_TRUE(desc2.option);
  911. EXPECT_EQ(38, desc1.option->getType());
  912. // Try to get the non-existing option from the non-existing
  913. // option space and expect that option is not returned.
  914. Subnet::OptionDescriptor desc3 = subnet->getOptionDescriptor("non-existing", 38);
  915. ASSERT_FALSE(desc3.option);
  916. }
  917. // The goal of this test is to verify options configuration
  918. // for a single subnet. In particular this test checks
  919. // that local options configuration overrides global
  920. // option setting.
  921. TEST_F(Dhcp6ParserTest, optionDataInSingleSubnet) {
  922. ConstElementPtr x;
  923. string config = "{ \"interface\": [ \"all\" ],"
  924. "\"preferred-lifetime\": 3000,"
  925. "\"rebind-timer\": 2000, "
  926. "\"renew-timer\": 1000, "
  927. "\"option-data\": [ {"
  928. " \"name\": \"subscriber-id\","
  929. " \"space\": \"dhcp6\","
  930. " \"code\": 38,"
  931. " \"data\": \"AB\","
  932. " \"csv-format\": False"
  933. " } ],"
  934. "\"subnet6\": [ { "
  935. " \"pool\": [ \"2001:db8:1::/80\" ],"
  936. " \"subnet\": \"2001:db8:1::/64\", "
  937. " \"option-data\": [ {"
  938. " \"name\": \"subscriber-id\","
  939. " \"space\": \"dhcp6\","
  940. " \"code\": 38,"
  941. " \"data\": \"AB CDEF0105\","
  942. " \"csv-format\": False"
  943. " },"
  944. " {"
  945. " \"name\": \"preference\","
  946. " \"space\": \"dhcp6\","
  947. " \"code\": 7,"
  948. " \"data\": \"01\","
  949. " \"csv-format\": False"
  950. " } ]"
  951. " } ],"
  952. "\"valid-lifetime\": 4000 }";
  953. ElementPtr json = Element::fromJSON(config);
  954. EXPECT_NO_THROW(x = configureDhcp6Server(srv_, json));
  955. ASSERT_TRUE(x);
  956. comment_ = parseAnswer(rcode_, x);
  957. ASSERT_EQ(0, rcode_);
  958. Subnet6Ptr subnet = CfgMgr::instance().getSubnet6(IOAddress("2001:db8:1::5"));
  959. ASSERT_TRUE(subnet);
  960. Subnet::OptionContainerPtr options = subnet->getOptionDescriptors("dhcp6");
  961. ASSERT_EQ(2, options->size());
  962. // Get the search index. Index #1 is to search using option code.
  963. const Subnet::OptionContainerTypeIndex& idx = options->get<1>();
  964. // Get the options for specified index. Expecting one option to be
  965. // returned but in theory we may have multiple options with the same
  966. // code so we get the range.
  967. std::pair<Subnet::OptionContainerTypeIndex::const_iterator,
  968. Subnet::OptionContainerTypeIndex::const_iterator> range =
  969. idx.equal_range(D6O_SUBSCRIBER_ID);
  970. // Expect single option with the code equal to 38.
  971. ASSERT_EQ(1, std::distance(range.first, range.second));
  972. const uint8_t subid_expected[] = {
  973. 0xAB, 0xCD, 0xEF, 0x01, 0x05
  974. };
  975. // Check if option is valid in terms of code and carried data.
  976. testOption(*range.first, D6O_SUBSCRIBER_ID, subid_expected,
  977. sizeof(subid_expected));
  978. range = idx.equal_range(D6O_PREFERENCE);
  979. ASSERT_EQ(1, std::distance(range.first, range.second));
  980. // Do another round of testing with second option.
  981. const uint8_t pref_expected[] = {
  982. 0x01
  983. };
  984. testOption(*range.first, D6O_PREFERENCE, pref_expected,
  985. sizeof(pref_expected));
  986. }
  987. // Goal of this test is to verify options configuration
  988. // for multiple subnets.
  989. TEST_F(Dhcp6ParserTest, optionDataInMultipleSubnets) {
  990. ConstElementPtr x;
  991. string config = "{ \"interface\": [ \"all\" ],"
  992. "\"preferred-lifetime\": 3000,"
  993. "\"rebind-timer\": 2000, "
  994. "\"renew-timer\": 1000, "
  995. "\"subnet6\": [ { "
  996. " \"pool\": [ \"2001:db8:1::/80\" ],"
  997. " \"subnet\": \"2001:db8:1::/64\", "
  998. " \"option-data\": [ {"
  999. " \"name\": \"subscriber-id\","
  1000. " \"space\": \"dhcp6\","
  1001. " \"code\": 38,"
  1002. " \"data\": \"0102030405060708090A\","
  1003. " \"csv-format\": False"
  1004. " } ]"
  1005. " },"
  1006. " {"
  1007. " \"pool\": [ \"2001:db8:2::/80\" ],"
  1008. " \"subnet\": \"2001:db8:2::/64\", "
  1009. " \"option-data\": [ {"
  1010. " \"name\": \"user-class\","
  1011. " \"space\": \"dhcp6\","
  1012. " \"code\": 15,"
  1013. " \"data\": \"FFFEFDFCFB\","
  1014. " \"csv-format\": False"
  1015. " } ]"
  1016. " } ],"
  1017. "\"valid-lifetime\": 4000 }";
  1018. ElementPtr json = Element::fromJSON(config);
  1019. EXPECT_NO_THROW(x = configureDhcp6Server(srv_, json));
  1020. ASSERT_TRUE(x);
  1021. comment_ = parseAnswer(rcode_, x);
  1022. ASSERT_EQ(0, rcode_);
  1023. Subnet6Ptr subnet1 = CfgMgr::instance().getSubnet6(IOAddress("2001:db8:1::5"));
  1024. ASSERT_TRUE(subnet1);
  1025. Subnet::OptionContainerPtr options1 = subnet1->getOptionDescriptors("dhcp6");
  1026. ASSERT_EQ(1, options1->size());
  1027. // Get the search index. Index #1 is to search using option code.
  1028. const Subnet::OptionContainerTypeIndex& idx1 = options1->get<1>();
  1029. // Get the options for specified index. Expecting one option to be
  1030. // returned but in theory we may have multiple options with the same
  1031. // code so we get the range.
  1032. std::pair<Subnet::OptionContainerTypeIndex::const_iterator,
  1033. Subnet::OptionContainerTypeIndex::const_iterator> range1 =
  1034. idx1.equal_range(D6O_SUBSCRIBER_ID);
  1035. // Expect single option with the code equal to 38.
  1036. ASSERT_EQ(1, std::distance(range1.first, range1.second));
  1037. const uint8_t subid_expected[] = {
  1038. 0x01, 0x02, 0x03, 0x04, 0x05,
  1039. 0x06, 0x07, 0x08, 0x09, 0x0A
  1040. };
  1041. // Check if option is valid in terms of code and carried data.
  1042. testOption(*range1.first, D6O_SUBSCRIBER_ID, subid_expected,
  1043. sizeof(subid_expected));
  1044. // Test another subnet in the same way.
  1045. Subnet6Ptr subnet2 = CfgMgr::instance().getSubnet6(IOAddress("2001:db8:2::4"));
  1046. ASSERT_TRUE(subnet2);
  1047. Subnet::OptionContainerPtr options2 = subnet2->getOptionDescriptors("dhcp6");
  1048. ASSERT_EQ(1, options2->size());
  1049. const Subnet::OptionContainerTypeIndex& idx2 = options2->get<1>();
  1050. std::pair<Subnet::OptionContainerTypeIndex::const_iterator,
  1051. Subnet::OptionContainerTypeIndex::const_iterator> range2 =
  1052. idx2.equal_range(D6O_USER_CLASS);
  1053. ASSERT_EQ(1, std::distance(range2.first, range2.second));
  1054. const uint8_t user_class_expected[] = {
  1055. 0xFF, 0xFE, 0xFD, 0xFC, 0xFB
  1056. };
  1057. testOption(*range2.first, D6O_USER_CLASS, user_class_expected,
  1058. sizeof(user_class_expected));
  1059. }
  1060. // Verify that empty option name is rejected in the configuration.
  1061. TEST_F(Dhcp6ParserTest, optionNameEmpty) {
  1062. // Empty option names not allowed.
  1063. testInvalidOptionParam("", "name");
  1064. }
  1065. // Verify that empty option name with spaces is rejected
  1066. // in the configuration.
  1067. TEST_F(Dhcp6ParserTest, optionNameSpaces) {
  1068. // Spaces in option names not allowed.
  1069. testInvalidOptionParam("option foo", "name");
  1070. }
  1071. // Verify that negative option code is rejected in the configuration.
  1072. TEST_F(Dhcp6ParserTest, optionCodeNegative) {
  1073. // Check negative option code -4. This should fail too.
  1074. testInvalidOptionParam("-4", "code");
  1075. }
  1076. // Verify that out of bounds option code is rejected in the configuration.
  1077. TEST_F(Dhcp6ParserTest, optionCodeNonUint16) {
  1078. // The valid option codes are uint16_t values so passing
  1079. // uint16_t maximum value incremented by 1 should result
  1080. // in failure.
  1081. testInvalidOptionParam("65536", "code");
  1082. }
  1083. // Verify that out of bounds option code is rejected in the configuration.
  1084. TEST_F(Dhcp6ParserTest, optionCodeHighNonUint16) {
  1085. // Another check for uint16_t overflow but this time
  1086. // let's pass even greater option code value.
  1087. testInvalidOptionParam("70000", "code");
  1088. }
  1089. // Verify that zero option code is rejected in the configuration.
  1090. TEST_F(Dhcp6ParserTest, optionCodeZero) {
  1091. // Option code 0 is reserved and should not be accepted
  1092. // by configuration parser.
  1093. testInvalidOptionParam("0", "code");
  1094. }
  1095. // Verify that option data which contains non hexadecimal characters
  1096. // is rejected by the configuration.
  1097. TEST_F(Dhcp6ParserTest, optionDataInvalidChar) {
  1098. // Option code 0 is reserved and should not be accepted
  1099. // by configuration parser.
  1100. testInvalidOptionParam("01020R", "data");
  1101. }
  1102. // Verify that option data containins '0x' prefix is rejected
  1103. // by the configuration.
  1104. TEST_F(Dhcp6ParserTest, optionDataUnexpectedPrefix) {
  1105. // Option code 0 is reserved and should not be accepted
  1106. // by configuration parser.
  1107. testInvalidOptionParam("0x0102", "data");
  1108. }
  1109. // Verify that option data consisting od an odd number of
  1110. // hexadecimal digits is rejected in the configuration.
  1111. TEST_F(Dhcp6ParserTest, optionDataOddLength) {
  1112. // Option code 0 is reserved and should not be accepted
  1113. // by configuration parser.
  1114. testInvalidOptionParam("123", "data");
  1115. }
  1116. // Verify that either lower or upper case characters are allowed
  1117. // to specify the option data.
  1118. TEST_F(Dhcp6ParserTest, optionDataLowerCase) {
  1119. ConstElementPtr x;
  1120. std::string config = createConfigWithOption("0a0b0C0D", "data");
  1121. ElementPtr json = Element::fromJSON(config);
  1122. EXPECT_NO_THROW(x = configureDhcp6Server(srv_, json));
  1123. ASSERT_TRUE(x);
  1124. comment_ = parseAnswer(rcode_, x);
  1125. ASSERT_EQ(0, rcode_);
  1126. Subnet6Ptr subnet = CfgMgr::instance().getSubnet6(IOAddress("2001:db8:1::5"));
  1127. ASSERT_TRUE(subnet);
  1128. Subnet::OptionContainerPtr options = subnet->getOptionDescriptors("dhcp6");
  1129. ASSERT_EQ(1, options->size());
  1130. // Get the search index. Index #1 is to search using option code.
  1131. const Subnet::OptionContainerTypeIndex& idx = options->get<1>();
  1132. // Get the options for specified index. Expecting one option to be
  1133. // returned but in theory we may have multiple options with the same
  1134. // code so we get the range.
  1135. std::pair<Subnet::OptionContainerTypeIndex::const_iterator,
  1136. Subnet::OptionContainerTypeIndex::const_iterator> range =
  1137. idx.equal_range(D6O_SUBSCRIBER_ID);
  1138. // Expect single option with the code equal to 38.
  1139. ASSERT_EQ(1, std::distance(range.first, range.second));
  1140. const uint8_t subid_expected[] = {
  1141. 0x0A, 0x0B, 0x0C, 0x0D
  1142. };
  1143. // Check if option is valid in terms of code and carried data.
  1144. testOption(*range.first, D6O_SUBSCRIBER_ID, subid_expected,
  1145. sizeof(subid_expected));
  1146. }
  1147. // Verify that specific option object is returned for standard
  1148. // option which has dedicated option class derived from Option.
  1149. TEST_F(Dhcp6ParserTest, stdOptionData) {
  1150. ConstElementPtr x;
  1151. std::map<std::string, std::string> params;
  1152. params["name"] = "ia-na";
  1153. params["space"] = "dhcp6";
  1154. // Option code 3 means OPTION_IA_NA.
  1155. params["code"] = "3";
  1156. params["data"] = "12345, 6789, 1516";
  1157. params["csv-format"] = "True";
  1158. std::string config = createConfigWithOption(params);
  1159. ElementPtr json = Element::fromJSON(config);
  1160. EXPECT_NO_THROW(x = configureDhcp6Server(srv_, json));
  1161. ASSERT_TRUE(x);
  1162. comment_ = parseAnswer(rcode_, x);
  1163. ASSERT_EQ(0, rcode_);
  1164. Subnet6Ptr subnet = CfgMgr::instance().getSubnet6(IOAddress("2001:db8:1::5"));
  1165. ASSERT_TRUE(subnet);
  1166. Subnet::OptionContainerPtr options = subnet->getOptionDescriptors("dhcp6");
  1167. ASSERT_EQ(1, options->size());
  1168. // Get the search index. Index #1 is to search using option code.
  1169. const Subnet::OptionContainerTypeIndex& idx = options->get<1>();
  1170. // Get the options for specified index. Expecting one option to be
  1171. // returned but in theory we may have multiple options with the same
  1172. // code so we get the range.
  1173. std::pair<Subnet::OptionContainerTypeIndex::const_iterator,
  1174. Subnet::OptionContainerTypeIndex::const_iterator> range =
  1175. idx.equal_range(D6O_IA_NA);
  1176. // Expect single option with the code equal to IA_NA option code.
  1177. ASSERT_EQ(1, std::distance(range.first, range.second));
  1178. // The actual pointer to the option is held in the option field
  1179. // in the structure returned.
  1180. OptionPtr option = range.first->option;
  1181. ASSERT_TRUE(option);
  1182. // Option object returned for here is expected to be Option6IA
  1183. // which is derived from Option. This class is dedicated to
  1184. // represent standard option IA_NA.
  1185. boost::shared_ptr<Option6IA> optionIA =
  1186. boost::dynamic_pointer_cast<Option6IA>(option);
  1187. // If cast is unsuccessful than option returned was of a
  1188. // differnt type than Option6IA. This is wrong.
  1189. ASSERT_TRUE(optionIA);
  1190. // If cast was successful we may use accessors exposed by
  1191. // Option6IA to validate that the content of this option
  1192. // has been set correctly.
  1193. EXPECT_EQ(12345, optionIA->getIAID());
  1194. EXPECT_EQ(6789, optionIA->getT1());
  1195. EXPECT_EQ(1516, optionIA->getT2());
  1196. }
  1197. };