libdhcp++_unittest.cc 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860
  1. // Copyright (C) 2011-2012 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. #include <config.h>
  15. #include <dhcp/dhcp4.h>
  16. #include <dhcp/dhcp6.h>
  17. #include <dhcp/libdhcp++.h>
  18. #include <dhcp/option4_addrlst.h>
  19. #include <dhcp/option6_addrlst.h>
  20. #include <dhcp/option6_ia.h>
  21. #include <dhcp/option6_iaaddr.h>
  22. #include <dhcp/option_custom.h>
  23. #include <dhcp/option_int.h>
  24. #include <dhcp/option_int_array.h>
  25. #include <util/buffer.h>
  26. #include <gtest/gtest.h>
  27. #include <iostream>
  28. #include <sstream>
  29. #include <arpa/inet.h>
  30. using namespace std;
  31. using namespace isc;
  32. using namespace isc::dhcp;
  33. using namespace isc::util;
  34. namespace {
  35. class LibDhcpTest : public ::testing::Test {
  36. public:
  37. LibDhcpTest() { }
  38. /// @brief Generic factory function to create any option.
  39. ///
  40. /// Generic factory function to create any option.
  41. ///
  42. /// @param u universe (V4 or V6)
  43. /// @param type option-type
  44. /// @param buf option-buffer
  45. static OptionPtr genericOptionFactory(Option::Universe u, uint16_t type,
  46. const OptionBuffer& buf) {
  47. Option* option = new Option(u, type, buf);
  48. return OptionPtr(option);
  49. }
  50. /// @brief Test DHCPv4 option definition.
  51. ///
  52. /// This function tests if option definition for standard
  53. /// option has been initialized correctly.
  54. ///
  55. /// @param code option code.
  56. /// @param begin iterator pointing a begining of a buffer to
  57. /// be used to create option instance.
  58. /// @param end iterator pointing an end of a buffer to be
  59. /// used to create option instance.
  60. /// @param expected_type type of the option created by the
  61. /// factory function returned by the option definition.
  62. static void testStdOptionDefs4(const uint16_t code,
  63. const OptionBufferConstIter begin,
  64. const OptionBufferConstIter end,
  65. const std::type_info& expected_type) {
  66. // Use V4 universe.
  67. testStdOptionDefs(Option::V4, code, begin, end, expected_type);
  68. }
  69. /// @brief Test DHCPv6 option definition.
  70. ///
  71. /// This function tests if option definition for standard
  72. /// option has been initialized correctly.
  73. ///
  74. /// @param code option code.
  75. /// @param begin iterator pointing a begining of a buffer to
  76. /// be used to create option instance.
  77. /// @param end iterator pointing an end of a buffer to be
  78. /// used to create option instance.
  79. /// @param expected_type type of the option created by the
  80. /// factory function returned by the option definition.
  81. static void testStdOptionDefs6(const uint16_t code,
  82. const OptionBufferConstIter begin,
  83. const OptionBufferConstIter end,
  84. const std::type_info& expected_type) {
  85. // Use V6 universe.
  86. testStdOptionDefs(Option::V6, code, begin, end, expected_type);
  87. }
  88. private:
  89. /// @brief Test DHCPv4 or DHCPv6 option definition.
  90. ///
  91. /// This function tests if option definition for standard
  92. /// option has been initialized correctly.
  93. ///
  94. /// @param code option code.
  95. /// @param begin iterator pointing a begining of a buffer to
  96. /// be used to create option instance.
  97. /// @param end iterator pointing an end of a buffer to be
  98. /// used to create option instance.
  99. /// @param expected_type type of the option created by the
  100. /// factory function returned by the option definition.
  101. static void testStdOptionDefs(const Option::Universe u,
  102. const uint16_t code,
  103. const OptionBufferConstIter begin,
  104. const OptionBufferConstIter end,
  105. const std::type_info& expected_type) {
  106. // Get all option definitions, we will use them to extract
  107. // the definition for a particular option code.
  108. // We don't have to initialize option definitions here because they
  109. // are initialized in the class's constructor.
  110. OptionDefContainer options = LibDHCP::getOptionDefs(u);
  111. // Get the container index #1. This one allows for searching
  112. // option definitions using option code.
  113. const OptionDefContainerTypeIndex& idx = options.get<1>();
  114. // Get 'all' option definitions for a particular option code.
  115. // For standard options we expect that the range returned
  116. // will contain single option as their codes are unique.
  117. OptionDefContainerTypeRange range = idx.equal_range(code);
  118. ASSERT_EQ(1, std::distance(range.first, range.second))
  119. << "Standard option definition for the code " << code
  120. << " has not been found.";
  121. // If we have single option definition returned, the
  122. // first iterator holds it.
  123. OptionDefinitionPtr def = *(range.first);
  124. // It should not happen that option definition is NULL but
  125. // let's make sure (test should take things like that into
  126. // account).
  127. ASSERT_TRUE(def) << "Option definition for the code "
  128. << code << " is NULL.";
  129. // Check that option definition is valid.
  130. ASSERT_NO_THROW(def->validate())
  131. << "Option definition for the option code " << code
  132. << " is invalid";
  133. OptionPtr option;
  134. // Create the option.
  135. ASSERT_NO_THROW(option = def->optionFactory(u, code, begin, end))
  136. << "Option creation failed for option code " << code;
  137. // Make sure it is not NULL.
  138. ASSERT_TRUE(option);
  139. // And the actual object type is the one that we expect.
  140. // Note that for many options there are dedicated classes
  141. // derived from Option class to represent them.
  142. EXPECT_TRUE(typeid(*option) == expected_type)
  143. << "Invalid class returned for option code " << code;
  144. }
  145. };
  146. // The DHCPv6 options in the wire format, used by multiple tests.
  147. const uint8_t v6packed[] = {
  148. 0, 1, 0, 5, 100, 101, 102, 103, 104, // CLIENT_ID (9 bytes)
  149. 0, 2, 0, 3, 105, 106, 107, // SERVER_ID (7 bytes)
  150. 0, 14, 0, 0, // RAPID_COMMIT (0 bytes)
  151. 0, 6, 0, 4, 108, 109, 110, 111, // ORO (8 bytes)
  152. 0, 8, 0, 2, 112, 113 // ELAPSED_TIME (6 bytes)
  153. };
  154. TEST_F(LibDhcpTest, optionFactory) {
  155. OptionBuffer buf;
  156. // Factory functions for specific options must be registered before
  157. // they can be used to create options instances. Otherwise exception
  158. // is rised.
  159. EXPECT_THROW(LibDHCP::optionFactory(Option::V4, DHO_SUBNET_MASK, buf),
  160. isc::BadValue);
  161. // Let's register some factory functions (two v4 and one v6 function).
  162. // Registration may trigger exception if function for the specified
  163. // option has been registered already.
  164. ASSERT_NO_THROW(
  165. LibDHCP::OptionFactoryRegister(Option::V4, DHO_SUBNET_MASK,
  166. &LibDhcpTest::genericOptionFactory);
  167. );
  168. ASSERT_NO_THROW(
  169. LibDHCP::OptionFactoryRegister(Option::V4, DHO_TIME_OFFSET,
  170. &LibDhcpTest::genericOptionFactory);
  171. );
  172. ASSERT_NO_THROW(
  173. LibDHCP::OptionFactoryRegister(Option::V6, D6O_CLIENTID,
  174. &LibDhcpTest::genericOptionFactory);
  175. );
  176. // Invoke factory functions for all options (check if registration
  177. // was successful).
  178. OptionPtr opt_subnet_mask;
  179. opt_subnet_mask = LibDHCP::optionFactory(Option::V4,
  180. DHO_SUBNET_MASK,
  181. buf);
  182. // Check if non-NULL DHO_SUBNET_MASK option pointer has been returned.
  183. ASSERT_TRUE(opt_subnet_mask);
  184. // Validate if type and universe is correct.
  185. EXPECT_EQ(Option::V4, opt_subnet_mask->getUniverse());
  186. EXPECT_EQ(DHO_SUBNET_MASK, opt_subnet_mask->getType());
  187. // Expect that option does not have content..
  188. EXPECT_EQ(0, opt_subnet_mask->len() - opt_subnet_mask->getHeaderLen());
  189. // Fill the time offset buffer with 4 bytes of data. Each byte set to 1.
  190. OptionBuffer time_offset_buf(4, 1);
  191. OptionPtr opt_time_offset;
  192. opt_time_offset = LibDHCP::optionFactory(Option::V4,
  193. DHO_TIME_OFFSET,
  194. time_offset_buf);
  195. // Check if non-NULL DHO_TIME_OFFSET option pointer has been returned.
  196. ASSERT_TRUE(opt_time_offset);
  197. // Validate if option length, type and universe is correct.
  198. EXPECT_EQ(Option::V4, opt_time_offset->getUniverse());
  199. EXPECT_EQ(DHO_TIME_OFFSET, opt_time_offset->getType());
  200. EXPECT_EQ(time_offset_buf.size(),
  201. opt_time_offset->len() - opt_time_offset->getHeaderLen());
  202. // Validate data in the option.
  203. EXPECT_TRUE(std::equal(time_offset_buf.begin(), time_offset_buf.end(),
  204. opt_time_offset->getData().begin()));
  205. // Fill the client id buffer with 20 bytes of data. Each byte set to 2.
  206. OptionBuffer clientid_buf(20, 2);
  207. OptionPtr opt_clientid;
  208. opt_clientid = LibDHCP::optionFactory(Option::V6,
  209. D6O_CLIENTID,
  210. clientid_buf);
  211. // Check if non-NULL D6O_CLIENTID option pointer has been returned.
  212. ASSERT_TRUE(opt_clientid);
  213. // Validate if option length, type and universe is correct.
  214. EXPECT_EQ(Option::V6, opt_clientid->getUniverse());
  215. EXPECT_EQ(D6O_CLIENTID, opt_clientid->getType());
  216. EXPECT_EQ(clientid_buf.size(), opt_clientid->len() - opt_clientid->getHeaderLen());
  217. // Validate data in the option.
  218. EXPECT_TRUE(std::equal(clientid_buf.begin(), clientid_buf.end(),
  219. opt_clientid->getData().begin()));
  220. }
  221. TEST_F(LibDhcpTest, packOptions6) {
  222. OptionBuffer buf(512);
  223. isc::dhcp::Option::OptionCollection opts; // list of options
  224. // generate content for options
  225. for (int i = 0; i < 64; i++) {
  226. buf[i]=i+100;
  227. }
  228. OptionPtr opt1(new Option(Option::V6, 1, buf.begin() + 0, buf.begin() + 5));
  229. OptionPtr opt2(new Option(Option::V6, 2, buf.begin() + 5, buf.begin() + 8));
  230. OptionPtr opt3(new Option(Option::V6, 14, buf.begin() + 8, buf.begin() + 8));
  231. OptionPtr opt4(new Option(Option::V6, 6, buf.begin() + 8, buf.begin() + 12));
  232. OptionPtr opt5(new Option(Option::V6, 8, buf.begin() + 12, buf.begin() + 14));
  233. opts.insert(pair<int, OptionPtr >(opt1->getType(), opt1));
  234. opts.insert(pair<int, OptionPtr >(opt1->getType(), opt2));
  235. opts.insert(pair<int, OptionPtr >(opt1->getType(), opt3));
  236. opts.insert(pair<int, OptionPtr >(opt1->getType(), opt4));
  237. opts.insert(pair<int, OptionPtr >(opt1->getType(), opt5));
  238. OutputBuffer assembled(512);
  239. EXPECT_NO_THROW(LibDHCP::packOptions6(assembled, opts));
  240. EXPECT_EQ(sizeof(v6packed), assembled.getLength());
  241. EXPECT_EQ(0, memcmp(assembled.getData(), v6packed, sizeof(v6packed)));
  242. }
  243. TEST_F(LibDhcpTest, unpackOptions6) {
  244. // just couple of random options
  245. // Option is used as a simple option implementation
  246. // More advanced uses are validated in tests dedicated for
  247. // specific derived classes.
  248. isc::dhcp::Option::OptionCollection options; // list of options
  249. OptionBuffer buf(512);
  250. memcpy(&buf[0], v6packed, sizeof(v6packed));
  251. EXPECT_NO_THROW ({
  252. LibDHCP::unpackOptions6(OptionBuffer(buf.begin(), buf.begin() + sizeof(v6packed)),
  253. options);
  254. });
  255. EXPECT_EQ(options.size(), 5); // there should be 5 options
  256. isc::dhcp::Option::OptionCollection::const_iterator x = options.find(1);
  257. ASSERT_FALSE(x == options.end()); // option 1 should exist
  258. EXPECT_EQ(1, x->second->getType()); // this should be option 1
  259. ASSERT_EQ(9, x->second->len()); // it should be of length 9
  260. ASSERT_EQ(5, x->second->getData().size());
  261. EXPECT_EQ(0, memcmp(&x->second->getData()[0], v6packed + 4, 5)); // data len=5
  262. x = options.find(2);
  263. ASSERT_FALSE(x == options.end()); // option 2 should exist
  264. EXPECT_EQ(2, x->second->getType()); // this should be option 2
  265. ASSERT_EQ(7, x->second->len()); // it should be of length 7
  266. ASSERT_EQ(3, x->second->getData().size());
  267. EXPECT_EQ(0, memcmp(&x->second->getData()[0], v6packed + 13, 3)); // data len=3
  268. x = options.find(14);
  269. ASSERT_FALSE(x == options.end()); // option 14 should exist
  270. EXPECT_EQ(14, x->second->getType()); // this should be option 14
  271. ASSERT_EQ(4, x->second->len()); // it should be of length 4
  272. EXPECT_EQ(0, x->second->getData().size()); // data len = 0
  273. x = options.find(6);
  274. ASSERT_FALSE(x == options.end()); // option 6 should exist
  275. EXPECT_EQ(6, x->second->getType()); // this should be option 6
  276. ASSERT_EQ(8, x->second->len()); // it should be of length 8
  277. // Option with code 6 is the OPTION_ORO. This option is
  278. // represented by the OptionIntArray<uint16_t> class which
  279. // comprises the set of uint16_t values. We need to cast the
  280. // returned pointer to this type to get values stored in it.
  281. boost::shared_ptr<OptionIntArray<uint16_t> > opt_oro =
  282. boost::dynamic_pointer_cast<OptionIntArray<uint16_t> >(x->second);
  283. // This value will be NULL if cast was unsuccessful. This is the case
  284. // when returned option has different type than expected.
  285. ASSERT_TRUE(opt_oro);
  286. // Get set of uint16_t values.
  287. std::vector<uint16_t> opts = opt_oro->getValues();
  288. // Prepare the refrence data.
  289. std::vector<uint16_t> expected_opts;
  290. expected_opts.push_back(0x6C6D); // equivalent to: 108, 109
  291. expected_opts.push_back(0x6E6F); // equivalent to 110, 111
  292. ASSERT_EQ(expected_opts.size(), opts.size());
  293. // Validated if option has been unpacked correctly.
  294. EXPECT_TRUE(std::equal(expected_opts.begin(), expected_opts.end(),
  295. opts.begin()));
  296. x = options.find(8);
  297. ASSERT_FALSE(x == options.end()); // option 8 should exist
  298. EXPECT_EQ(8, x->second->getType()); // this should be option 8
  299. ASSERT_EQ(6, x->second->len()); // it should be of length 9
  300. // Option with code 8 is OPTION_ELAPSED_TIME. This option is
  301. // represented by Option6Int<uint16_t> value that holds single
  302. // uint16_t value.
  303. boost::shared_ptr<OptionInt<uint16_t> > opt_elapsed_time =
  304. boost::dynamic_pointer_cast<OptionInt<uint16_t> >(x->second);
  305. // This value will be NULL if cast was unsuccessful. This is the case
  306. // when returned option has different type than expected.
  307. ASSERT_TRUE(opt_elapsed_time);
  308. // Returned value should be equivalent to two byte values: 112, 113
  309. EXPECT_EQ(0x7071, opt_elapsed_time->getValue());
  310. x = options.find(0);
  311. EXPECT_TRUE(x == options.end()); // option 0 not found
  312. x = options.find(256); // 256 is htons(1) on little endians. Worth checking
  313. EXPECT_TRUE(x == options.end()); // option 1 not found
  314. x = options.find(7);
  315. EXPECT_TRUE(x == options.end()); // option 2 not found
  316. x = options.find(32000);
  317. EXPECT_TRUE(x == options.end()); // option 32000 not found */
  318. }
  319. /// V4 Options being used to test pack/unpack operations.
  320. /// These are variable length options only so as there
  321. /// is no restriction on the data length being carried by them.
  322. /// For simplicity, we assign data of the length 3 for each
  323. /// of them.
  324. static uint8_t v4Opts[] = {
  325. 12, 3, 0, 1, 2, // Hostname
  326. 60, 3, 10, 11, 12, // Class Id
  327. 14, 3, 20, 21, 22, // Merit Dump File
  328. 254, 3, 30, 31, 32, // Reserved
  329. 128, 3, 40, 41, 42 // Vendor specific
  330. };
  331. TEST_F(LibDhcpTest, packOptions4) {
  332. vector<uint8_t> payload[5];
  333. for (int i = 0; i < 5; i++) {
  334. payload[i].resize(3);
  335. payload[i][0] = i*10;
  336. payload[i][1] = i*10+1;
  337. payload[i][2] = i*10+2;
  338. }
  339. OptionPtr opt1(new Option(Option::V4, 12, payload[0]));
  340. OptionPtr opt2(new Option(Option::V4, 60, payload[1]));
  341. OptionPtr opt3(new Option(Option::V4, 14, payload[2]));
  342. OptionPtr opt4(new Option(Option::V4,254, payload[3]));
  343. OptionPtr opt5(new Option(Option::V4,128, payload[4]));
  344. isc::dhcp::Option::OptionCollection opts; // list of options
  345. opts.insert(make_pair(opt1->getType(), opt1));
  346. opts.insert(make_pair(opt1->getType(), opt2));
  347. opts.insert(make_pair(opt1->getType(), opt3));
  348. opts.insert(make_pair(opt1->getType(), opt4));
  349. opts.insert(make_pair(opt1->getType(), opt5));
  350. vector<uint8_t> expVect(v4Opts, v4Opts + sizeof(v4Opts));
  351. OutputBuffer buf(100);
  352. EXPECT_NO_THROW(LibDHCP::packOptions(buf, opts));
  353. ASSERT_EQ(buf.getLength(), sizeof(v4Opts));
  354. EXPECT_EQ(0, memcmp(v4Opts, buf.getData(), sizeof(v4Opts)));
  355. }
  356. TEST_F(LibDhcpTest, unpackOptions4) {
  357. vector<uint8_t> v4packed(v4Opts, v4Opts + sizeof(v4Opts));
  358. isc::dhcp::Option::OptionCollection options; // list of options
  359. ASSERT_NO_THROW(
  360. LibDHCP::unpackOptions4(v4packed, options);
  361. );
  362. isc::dhcp::Option::OptionCollection::const_iterator x = options.find(12);
  363. ASSERT_FALSE(x == options.end()); // option 1 should exist
  364. EXPECT_EQ(12, x->second->getType()); // this should be option 12
  365. ASSERT_EQ(3, x->second->getData().size()); // it should be of length 3
  366. EXPECT_EQ(5, x->second->len()); // total option length 5
  367. EXPECT_EQ(0, memcmp(&x->second->getData()[0], v4Opts+2, 3)); // data len=3
  368. x = options.find(60);
  369. ASSERT_FALSE(x == options.end()); // option 2 should exist
  370. EXPECT_EQ(60, x->second->getType()); // this should be option 60
  371. ASSERT_EQ(3, x->second->getData().size()); // it should be of length 3
  372. EXPECT_EQ(5, x->second->len()); // total option length 5
  373. EXPECT_EQ(0, memcmp(&x->second->getData()[0], v4Opts+7, 3)); // data len=3
  374. x = options.find(14);
  375. ASSERT_FALSE(x == options.end()); // option 3 should exist
  376. EXPECT_EQ(14, x->second->getType()); // this should be option 14
  377. ASSERT_EQ(3, x->second->getData().size()); // it should be of length 3
  378. EXPECT_EQ(5, x->second->len()); // total option length 5
  379. EXPECT_EQ(0, memcmp(&x->second->getData()[0], v4Opts+12, 3)); // data len=3
  380. x = options.find(254);
  381. ASSERT_FALSE(x == options.end()); // option 3 should exist
  382. EXPECT_EQ(254, x->second->getType()); // this should be option 254
  383. ASSERT_EQ(3, x->second->getData().size()); // it should be of length 3
  384. EXPECT_EQ(5, x->second->len()); // total option length 5
  385. EXPECT_EQ(0, memcmp(&x->second->getData()[0], v4Opts+17, 3)); // data len=3
  386. x = options.find(128);
  387. ASSERT_FALSE(x == options.end()); // option 3 should exist
  388. EXPECT_EQ(128, x->second->getType()); // this should be option 254
  389. ASSERT_EQ(3, x->second->getData().size()); // it should be of length 3
  390. EXPECT_EQ(5, x->second->len()); // total option length 5
  391. EXPECT_EQ(0, memcmp(&x->second->getData()[0], v4Opts+22, 3)); // data len=3
  392. x = options.find(0);
  393. EXPECT_TRUE(x == options.end()); // option 0 not found
  394. x = options.find(1);
  395. EXPECT_TRUE(x == options.end()); // option 1 not found
  396. x = options.find(2);
  397. EXPECT_TRUE(x == options.end()); // option 2 not found
  398. }
  399. TEST_F(LibDhcpTest, stdOptionDefs4) {
  400. // Create a buffer that holds dummy option data.
  401. // It will be used to create most of the options.
  402. std::vector<uint8_t> buf(48, 1);
  403. OptionBufferConstIter begin = buf.begin();
  404. OptionBufferConstIter end = buf.begin();
  405. LibDhcpTest::testStdOptionDefs4(DHO_SUBNET_MASK, begin, end,
  406. typeid(OptionCustom));
  407. LibDhcpTest::testStdOptionDefs4(DHO_TIME_OFFSET, begin, begin + 4,
  408. typeid(OptionInt<uint32_t>));
  409. LibDhcpTest::testStdOptionDefs4(DHO_ROUTERS, begin, end,
  410. typeid(Option4AddrLst));
  411. LibDhcpTest::testStdOptionDefs4(DHO_TIME_SERVERS, begin, end,
  412. typeid(Option4AddrLst));
  413. LibDhcpTest::testStdOptionDefs4(DHO_NAME_SERVERS, begin, end,
  414. typeid(OptionCustom));
  415. LibDhcpTest::testStdOptionDefs4(DHO_DOMAIN_NAME_SERVERS, begin, end,
  416. typeid(Option4AddrLst));
  417. LibDhcpTest::testStdOptionDefs4(DHO_LOG_SERVERS, begin, end,
  418. typeid(Option4AddrLst));
  419. LibDhcpTest::testStdOptionDefs4(DHO_COOKIE_SERVERS, begin, end,
  420. typeid(Option4AddrLst));
  421. LibDhcpTest::testStdOptionDefs4(DHO_LPR_SERVERS, begin, end,
  422. typeid(Option4AddrLst));
  423. LibDhcpTest::testStdOptionDefs4(DHO_IMPRESS_SERVERS, begin, end,
  424. typeid(Option4AddrLst));
  425. LibDhcpTest::testStdOptionDefs4(DHO_RESOURCE_LOCATION_SERVERS, begin, end,
  426. typeid(Option4AddrLst));
  427. LibDhcpTest::testStdOptionDefs4(DHO_HOST_NAME, begin, end,
  428. typeid(OptionCustom));
  429. LibDhcpTest::testStdOptionDefs4(DHO_BOOT_SIZE, begin, begin + 2,
  430. typeid(OptionInt<uint16_t>));
  431. LibDhcpTest::testStdOptionDefs4(DHO_MERIT_DUMP, begin, end,
  432. typeid(OptionCustom));
  433. LibDhcpTest::testStdOptionDefs4(DHO_DOMAIN_NAME, begin, end,
  434. typeid(OptionCustom));
  435. LibDhcpTest::testStdOptionDefs4(DHO_SWAP_SERVER, begin, end,
  436. typeid(OptionCustom));
  437. LibDhcpTest::testStdOptionDefs4(DHO_ROOT_PATH, begin, end,
  438. typeid(OptionCustom));
  439. LibDhcpTest::testStdOptionDefs4(DHO_EXTENSIONS_PATH, begin, end,
  440. typeid(OptionCustom));
  441. LibDhcpTest::testStdOptionDefs4(DHO_IP_FORWARDING, begin, end,
  442. typeid(OptionCustom));
  443. LibDhcpTest::testStdOptionDefs4(DHO_NON_LOCAL_SOURCE_ROUTING, begin, end,
  444. typeid(OptionCustom));
  445. LibDhcpTest::testStdOptionDefs4(DHO_POLICY_FILTER, begin, end,
  446. typeid(Option4AddrLst));
  447. LibDhcpTest::testStdOptionDefs4(DHO_MAX_DGRAM_REASSEMBLY, begin, begin + 2,
  448. typeid(OptionInt<uint16_t>));
  449. LibDhcpTest::testStdOptionDefs4(DHO_DEFAULT_IP_TTL, begin, begin + 1,
  450. typeid(OptionInt<uint8_t>));
  451. LibDhcpTest::testStdOptionDefs4(DHO_PATH_MTU_AGING_TIMEOUT, begin, begin + 4,
  452. typeid(OptionInt<uint32_t>));
  453. LibDhcpTest::testStdOptionDefs4(DHO_PATH_MTU_PLATEAU_TABLE, begin, begin + 10,
  454. typeid(OptionIntArray<uint16_t>));
  455. LibDhcpTest::testStdOptionDefs4(DHO_INTERFACE_MTU, begin, begin + 2,
  456. typeid(OptionInt<uint16_t>));
  457. LibDhcpTest::testStdOptionDefs4(DHO_ALL_SUBNETS_LOCAL, begin, end,
  458. typeid(OptionCustom));
  459. LibDhcpTest::testStdOptionDefs4(DHO_BROADCAST_ADDRESS, begin, end,
  460. typeid(OptionCustom));
  461. LibDhcpTest::testStdOptionDefs4(DHO_PERFORM_MASK_DISCOVERY, begin, end,
  462. typeid(OptionCustom));
  463. LibDhcpTest::testStdOptionDefs4(DHO_MASK_SUPPLIER, begin, end,
  464. typeid(OptionCustom));
  465. LibDhcpTest::testStdOptionDefs4(DHO_ROUTER_DISCOVERY, begin, end,
  466. typeid(OptionCustom));
  467. LibDhcpTest::testStdOptionDefs4(DHO_ROUTER_SOLICITATION_ADDRESS, begin, end,
  468. typeid(OptionCustom));
  469. LibDhcpTest::testStdOptionDefs4(DHO_STATIC_ROUTES, begin, end,
  470. typeid(Option4AddrLst));
  471. LibDhcpTest::testStdOptionDefs4(DHO_TRAILER_ENCAPSULATION, begin, end,
  472. typeid(OptionCustom));
  473. LibDhcpTest::testStdOptionDefs4(DHO_ARP_CACHE_TIMEOUT, begin, begin + 4,
  474. typeid(OptionInt<uint32_t>));
  475. LibDhcpTest::testStdOptionDefs4(DHO_IEEE802_3_ENCAPSULATION, begin, end,
  476. typeid(OptionCustom));
  477. LibDhcpTest::testStdOptionDefs4(DHO_DEFAULT_TCP_TTL, begin, begin + 1,
  478. typeid(OptionInt<uint8_t>));
  479. LibDhcpTest::testStdOptionDefs4(DHO_TCP_KEEPALIVE_INTERVAL, begin, begin + 4,
  480. typeid(OptionInt<uint32_t>));
  481. LibDhcpTest::testStdOptionDefs4(DHO_TCP_KEEPALIVE_GARBAGE, begin, begin + 1,
  482. typeid(OptionCustom));
  483. LibDhcpTest::testStdOptionDefs4(DHO_NIS_DOMAIN, begin, end,
  484. typeid(OptionCustom));
  485. LibDhcpTest::testStdOptionDefs4(DHO_NIS_SERVERS, begin, end,
  486. typeid(Option4AddrLst));
  487. LibDhcpTest::testStdOptionDefs4(DHO_NTP_SERVERS, begin, end,
  488. typeid(Option4AddrLst));
  489. LibDhcpTest::testStdOptionDefs4(DHO_VENDOR_ENCAPSULATED_OPTIONS, begin, end,
  490. typeid(Option));
  491. LibDhcpTest::testStdOptionDefs4(DHO_NETBIOS_NAME_SERVERS, begin, end,
  492. typeid(Option4AddrLst));
  493. LibDhcpTest::testStdOptionDefs4(DHO_NETBIOS_DD_SERVER, begin, end,
  494. typeid(Option4AddrLst));
  495. LibDhcpTest::testStdOptionDefs4(DHO_NETBIOS_NODE_TYPE, begin, begin + 1,
  496. typeid(OptionInt<uint8_t>));
  497. LibDhcpTest::testStdOptionDefs4(DHO_NETBIOS_SCOPE, begin, end,
  498. typeid(OptionCustom));
  499. LibDhcpTest::testStdOptionDefs4(DHO_FONT_SERVERS, begin, end,
  500. typeid(Option4AddrLst));
  501. LibDhcpTest::testStdOptionDefs4(DHO_X_DISPLAY_MANAGER, begin, end,
  502. typeid(Option4AddrLst));
  503. LibDhcpTest::testStdOptionDefs4(DHO_DHCP_REQUESTED_ADDRESS, begin, end,
  504. typeid(OptionCustom));
  505. LibDhcpTest::testStdOptionDefs4(DHO_DHCP_LEASE_TIME, begin, begin + 4,
  506. typeid(OptionInt<uint32_t>));
  507. LibDhcpTest::testStdOptionDefs4(DHO_DHCP_OPTION_OVERLOAD, begin, begin + 1,
  508. typeid(OptionInt<uint8_t>));
  509. LibDhcpTest::testStdOptionDefs4(DHO_DHCP_MESSAGE_TYPE, begin, begin + 1,
  510. typeid(OptionInt<uint8_t>));
  511. LibDhcpTest::testStdOptionDefs4(DHO_DHCP_SERVER_IDENTIFIER, begin, end,
  512. typeid(OptionCustom));
  513. LibDhcpTest::testStdOptionDefs4(DHO_DHCP_PARAMETER_REQUEST_LIST, begin, end,
  514. typeid(Option));
  515. LibDhcpTest::testStdOptionDefs4(DHO_DHCP_MESSAGE, begin, end,
  516. typeid(OptionCustom));
  517. LibDhcpTest::testStdOptionDefs4(DHO_DHCP_MAX_MESSAGE_SIZE, begin, begin + 2,
  518. typeid(OptionInt<uint16_t>));
  519. LibDhcpTest::testStdOptionDefs4(DHO_DHCP_RENEWAL_TIME, begin, begin + 4,
  520. typeid(OptionInt<uint32_t>));
  521. LibDhcpTest::testStdOptionDefs4(DHO_DHCP_REBINDING_TIME, begin, begin + 4,
  522. typeid(OptionInt<uint32_t>));
  523. LibDhcpTest::testStdOptionDefs4(DHO_VENDOR_CLASS_IDENTIFIER, begin, end,
  524. typeid(Option));
  525. LibDhcpTest::testStdOptionDefs4(DHO_DHCP_CLIENT_IDENTIFIER, begin, end,
  526. typeid(Option));
  527. LibDhcpTest::testStdOptionDefs4(DHO_NWIP_DOMAIN_NAME, begin, end,
  528. typeid(OptionCustom));
  529. LibDhcpTest::testStdOptionDefs4(DHO_NWIP_SUBOPTIONS, begin, end,
  530. typeid(Option));
  531. LibDhcpTest::testStdOptionDefs4(DHO_USER_CLASS, begin, end,
  532. typeid(Option));
  533. LibDhcpTest::testStdOptionDefs4(DHO_FQDN, begin, end,
  534. typeid(OptionCustom));
  535. LibDhcpTest::testStdOptionDefs4(DHO_DHCP_AGENT_OPTIONS, begin, end,
  536. typeid(Option));
  537. LibDhcpTest::testStdOptionDefs4(DHO_AUTHENTICATE, begin, end,
  538. typeid(Option));
  539. LibDhcpTest::testStdOptionDefs4(DHO_CLIENT_LAST_TRANSACTION_TIME,
  540. begin, begin + 4,
  541. typeid(OptionInt<uint32_t>));
  542. LibDhcpTest::testStdOptionDefs4(DHO_ASSOCIATED_IP, begin, end,
  543. typeid(Option4AddrLst));
  544. LibDhcpTest::testStdOptionDefs4(DHO_SUBNET_SELECTION, begin, end,
  545. typeid(OptionCustom));
  546. LibDhcpTest::testStdOptionDefs4(DHO_DOMAIN_SEARCH, begin, end,
  547. typeid(Option));
  548. LibDhcpTest::testStdOptionDefs4(DHO_VIVCO_SUBOPTIONS, begin, end,
  549. typeid(Option));
  550. LibDhcpTest::testStdOptionDefs4(DHO_VIVSO_SUBOPTIONS, begin, end,
  551. typeid(Option));
  552. }
  553. // Test that definitions of standard options have been initialized
  554. // correctly.
  555. // @todo Only limited number of option definitions are now created
  556. // This test have to be extended once all option definitions are
  557. // created.
  558. TEST_F(LibDhcpTest, stdOptionDefs6) {
  559. // Create a buffer that holds dummy option data.
  560. // It will be used to create most of the options.
  561. std::vector<uint8_t> buf(48, 1);
  562. OptionBufferConstIter begin = buf.begin();
  563. OptionBufferConstIter end = buf.end();
  564. // Prepare buffer holding an array of FQDNs.
  565. const char data[] = {
  566. 8, 109, 121, 100, 111, 109, 97, 105, 110, // "mydomain"
  567. 7, 101, 120, 97, 109, 112, 108, 101, // "example"
  568. 3, 99, 111, 109, // "com"
  569. 0,
  570. 7, 101, 120, 97, 109, 112, 108, 101, // "example"
  571. 3, 99, 111, 109, // "com"
  572. 0
  573. };
  574. // Initialize a vector with the FQDN data.
  575. std::vector<uint8_t> fqdn_buf(data, data + sizeof(data));
  576. // The CLIENT_FQDN holds a uint8_t value and FQDN. We have
  577. // to add the uint8_t value to it and then append the buffer
  578. // holding some valid FQDN.
  579. std::vector<uint8_t> client_fqdn_buf(1);
  580. client_fqdn_buf.insert(client_fqdn_buf.end(), fqdn_buf.begin(),
  581. fqdn_buf.end());
  582. // The actual test starts here for all supported option codes.
  583. LibDhcpTest::testStdOptionDefs6(D6O_CLIENTID, begin, end,
  584. typeid(Option));
  585. LibDhcpTest::testStdOptionDefs6(D6O_SERVERID, begin, end,
  586. typeid(Option));
  587. LibDhcpTest::testStdOptionDefs6(D6O_IA_NA, begin, end,
  588. typeid(Option6IA));
  589. LibDhcpTest::testStdOptionDefs6(D6O_IA_TA, begin, begin + 4,
  590. typeid(OptionInt<uint32_t>));
  591. LibDhcpTest::testStdOptionDefs6(D6O_IAADDR, begin, end,
  592. typeid(Option6IAAddr));
  593. LibDhcpTest::testStdOptionDefs6(D6O_ORO, begin, end,
  594. typeid(OptionIntArray<uint16_t>));
  595. LibDhcpTest::testStdOptionDefs6(D6O_PREFERENCE, begin, begin + 1,
  596. typeid(OptionInt<uint8_t>));
  597. LibDhcpTest::testStdOptionDefs6(D6O_ELAPSED_TIME, begin, begin + 2,
  598. typeid(OptionInt<uint16_t>));
  599. LibDhcpTest::testStdOptionDefs6(D6O_RELAY_MSG, begin, end,
  600. typeid(Option));
  601. LibDhcpTest::testStdOptionDefs6(D6O_STATUS_CODE, begin, end,
  602. typeid(OptionCustom));
  603. LibDhcpTest::testStdOptionDefs6(D6O_RAPID_COMMIT, begin, end,
  604. typeid(Option));
  605. LibDhcpTest::testStdOptionDefs6(D6O_USER_CLASS, begin, end,
  606. typeid(Option));
  607. LibDhcpTest::testStdOptionDefs6(D6O_VENDOR_CLASS, begin, end,
  608. typeid(OptionCustom));
  609. LibDhcpTest::testStdOptionDefs6(D6O_VENDOR_OPTS, begin, end,
  610. typeid(OptionCustom));
  611. LibDhcpTest::testStdOptionDefs6(D6O_INTERFACE_ID, begin, end,
  612. typeid(Option));
  613. LibDhcpTest::testStdOptionDefs6(D6O_RECONF_MSG, begin, begin + 1,
  614. typeid(OptionInt<uint8_t>));
  615. LibDhcpTest::testStdOptionDefs6(D6O_RECONF_ACCEPT, begin, end,
  616. typeid(Option));
  617. LibDhcpTest::testStdOptionDefs6(D6O_SIP_SERVERS_DNS, fqdn_buf.begin(),
  618. fqdn_buf.end(),
  619. typeid(OptionCustom));
  620. LibDhcpTest::testStdOptionDefs6(D6O_SIP_SERVERS_ADDR, begin, end,
  621. typeid(Option6AddrLst));
  622. LibDhcpTest::testStdOptionDefs6(D6O_NAME_SERVERS, begin, end,
  623. typeid(Option6AddrLst));
  624. LibDhcpTest::testStdOptionDefs6(D6O_DOMAIN_SEARCH, fqdn_buf.begin(),
  625. fqdn_buf.end(), typeid(OptionCustom));
  626. LibDhcpTest::testStdOptionDefs6(D6O_IA_PD, begin, end,
  627. typeid(Option6IA));
  628. LibDhcpTest::testStdOptionDefs6(D6O_IAPREFIX, begin, end,
  629. typeid(OptionCustom));
  630. LibDhcpTest::testStdOptionDefs6(D6O_NIS_SERVERS, begin, end,
  631. typeid(Option6AddrLst));
  632. LibDhcpTest::testStdOptionDefs6(D6O_NISP_SERVERS, begin, end,
  633. typeid(Option6AddrLst));
  634. LibDhcpTest::testStdOptionDefs6(D6O_NIS_DOMAIN_NAME, fqdn_buf.begin(),
  635. fqdn_buf.end(),
  636. typeid(OptionCustom));
  637. LibDhcpTest::testStdOptionDefs6(D6O_NISP_DOMAIN_NAME, fqdn_buf.begin(),
  638. fqdn_buf.end(),
  639. typeid(OptionCustom));
  640. LibDhcpTest::testStdOptionDefs6(D6O_SNTP_SERVERS, begin, end,
  641. typeid(Option6AddrLst));
  642. LibDhcpTest::testStdOptionDefs6(D6O_INFORMATION_REFRESH_TIME,
  643. begin, begin + 4,
  644. typeid(OptionInt<uint32_t>));
  645. LibDhcpTest::testStdOptionDefs6(D6O_BCMCS_SERVER_D, fqdn_buf.begin(),
  646. fqdn_buf.end(),
  647. typeid(OptionCustom));
  648. LibDhcpTest::testStdOptionDefs6(D6O_BCMCS_SERVER_A, begin, end,
  649. typeid(Option6AddrLst));
  650. LibDhcpTest::testStdOptionDefs6(D6O_GEOCONF_CIVIC, begin, end,
  651. typeid(OptionCustom));
  652. LibDhcpTest::testStdOptionDefs6(D6O_REMOTE_ID, begin, end,
  653. typeid(OptionCustom));
  654. LibDhcpTest::testStdOptionDefs6(D6O_SUBSCRIBER_ID, begin, end,
  655. typeid(Option));
  656. LibDhcpTest::testStdOptionDefs6(D6O_CLIENT_FQDN, client_fqdn_buf.begin(),
  657. client_fqdn_buf.end(), typeid(OptionCustom));
  658. LibDhcpTest::testStdOptionDefs6(D6O_PANA_AGENT, begin, end,
  659. typeid(Option6AddrLst));
  660. LibDhcpTest::testStdOptionDefs6(D6O_PANA_AGENT, begin, end,
  661. typeid(Option6AddrLst));
  662. LibDhcpTest::testStdOptionDefs6(D6O_NEW_POSIX_TIMEZONE, begin, end,
  663. typeid(OptionCustom));
  664. LibDhcpTest::testStdOptionDefs6(D6O_NEW_TZDB_TIMEZONE, begin, end,
  665. typeid(OptionCustom));
  666. LibDhcpTest::testStdOptionDefs6(D6O_ERO, begin, end,
  667. typeid(OptionIntArray<uint16_t>));
  668. LibDhcpTest::testStdOptionDefs6(D6O_LQ_QUERY, begin, end,
  669. typeid(OptionCustom));
  670. LibDhcpTest::testStdOptionDefs6(D6O_CLIENT_DATA, begin, end,
  671. typeid(Option));
  672. LibDhcpTest::testStdOptionDefs6(D6O_CLT_TIME, begin, begin + 4,
  673. typeid(OptionInt<uint32_t>));
  674. LibDhcpTest::testStdOptionDefs6(D6O_LQ_RELAY_DATA, begin, end,
  675. typeid(OptionCustom));
  676. LibDhcpTest::testStdOptionDefs6(D6O_LQ_CLIENT_LINK, begin, end,
  677. typeid(Option6AddrLst));
  678. }
  679. }