libdhcp++_unittest.cc 40 KB

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