config_parser_unittest.cc 46 KB

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