libdhcp++_unittest.cc 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264
  1. // Copyright (C) 2011-2015 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/docsis3_option_defs.h>
  18. #include <dhcp/libdhcp++.h>
  19. #include <dhcp/option4_addrlst.h>
  20. #include <dhcp/option4_client_fqdn.h>
  21. #include <dhcp/option6_addrlst.h>
  22. #include <dhcp/option6_client_fqdn.h>
  23. #include <dhcp/option6_ia.h>
  24. #include <dhcp/option6_iaaddr.h>
  25. #include <dhcp/option6_iaprefix.h>
  26. #include <dhcp/option_custom.h>
  27. #include <dhcp/option_int.h>
  28. #include <dhcp/option_int_array.h>
  29. #include <dhcp/option_string.h>
  30. #include <dhcp/option_vendor.h>
  31. #include <dhcp/option_vendor_class.h>
  32. #include <util/buffer.h>
  33. #include <util/encode/hex.h>
  34. #include <boost/pointer_cast.hpp>
  35. #include <gtest/gtest.h>
  36. #include <iostream>
  37. #include <sstream>
  38. #include <arpa/inet.h>
  39. using namespace std;
  40. using namespace isc;
  41. using namespace isc::dhcp;
  42. using namespace isc::util;
  43. namespace {
  44. // DHCPv6 suboptions of Vendor Options Option.
  45. /// @todo move to src/lib/dhcp/docsis3_option_defs.h once #3194 is merged.
  46. const uint16_t OPTION_CMTS_CAPS = 1025;
  47. const uint16_t OPTION_CM_MAC = 1026;
  48. class LibDhcpTest : public ::testing::Test {
  49. public:
  50. LibDhcpTest() { }
  51. /// @brief Generic factory function to create any option.
  52. ///
  53. /// Generic factory function to create any option.
  54. ///
  55. /// @param u universe (V4 or V6)
  56. /// @param type option-type
  57. /// @param buf option-buffer
  58. static OptionPtr genericOptionFactory(Option::Universe u, uint16_t type,
  59. const OptionBuffer& buf) {
  60. return (OptionPtr(new Option(u, type, buf)));
  61. }
  62. /// @brief Test DHCPv4 option definition.
  63. ///
  64. /// This function tests if option definition for standard
  65. /// option has been initialized correctly.
  66. ///
  67. /// @param code option code.
  68. /// @param begin iterator pointing at beginning of a buffer to
  69. /// be used to create option instance.
  70. /// @param end iterator pointing at end of a buffer to be
  71. /// used to create option instance.
  72. /// @param expected_type type of the option created by the
  73. /// factory function returned by the option definition.
  74. /// @param encapsulates name of the option space being encapsulated
  75. /// by the option.
  76. static void testStdOptionDefs4(const uint16_t code,
  77. const OptionBufferConstIter begin,
  78. const OptionBufferConstIter end,
  79. const std::type_info& expected_type,
  80. const std::string& encapsulates = "") {
  81. // Use V4 universe.
  82. testStdOptionDefs(Option::V4, code, begin, end, expected_type,
  83. encapsulates);
  84. }
  85. /// @brief Test DHCPv6 option definition.
  86. ///
  87. /// This function tests if option definition for standard
  88. /// option has been initialized correctly.
  89. ///
  90. /// @param code option code.
  91. /// @param begin iterator pointing at beginning of a buffer to
  92. /// be used to create option instance.
  93. /// @param end iterator pointing at end of a buffer to be
  94. /// used to create option instance.
  95. /// @param expected_type type of the option created by the
  96. /// factory function returned by the option definition.
  97. /// @param encapsulates name of the option space being encapsulated
  98. /// by the option.
  99. static void testStdOptionDefs6(const uint16_t code,
  100. const OptionBufferConstIter begin,
  101. const OptionBufferConstIter end,
  102. const std::type_info& expected_type,
  103. const std::string& encapsulates = "") {
  104. // Use V6 universe.
  105. testStdOptionDefs(Option::V6, code, begin, end, expected_type,
  106. encapsulates);
  107. }
  108. /// @brief Create a sample DHCPv4 option 43 with suboptions.
  109. static OptionBuffer createVendorOption() {
  110. const uint8_t opt_data[] = {
  111. 0x2B, 0x0D, // Vendor-Specific Information (CableLabs)
  112. // Suboptions start here...
  113. 0x02, 0x05, // Device Type Option (length = 5)
  114. 'D', 'u', 'm', 'm', 'y',
  115. 0x04, 0x04, // Serial Number Option (length = 4)
  116. 0x42, 0x52, 0x32, 0x32 // Serial number
  117. };
  118. return (OptionBuffer(opt_data, opt_data + sizeof(opt_data)));
  119. }
  120. /// @brief Create a sample DHCPv4 option 82 with suboptions.
  121. static OptionBuffer createAgentInformationOption() {
  122. const uint8_t opt_data[] = {
  123. 0x52, 0x0E, // Agent Information Option (length = 14)
  124. // Suboptions start here...
  125. 0x01, 0x04, // Agent Circuit ID (length = 4)
  126. 0x20, 0x00, 0x00, 0x02, // ID
  127. 0x02, 0x06, // Agent Remote ID
  128. 0x20, 0xE5, 0x2A, 0xB8, 0x15, 0x14 // ID
  129. };
  130. return (OptionBuffer(opt_data, opt_data + sizeof(opt_data)));
  131. }
  132. private:
  133. /// @brief Test DHCPv4 or DHCPv6 option definition.
  134. ///
  135. /// This function tests if option definition for standard
  136. /// option has been initialized correctly.
  137. ///
  138. /// @param code option code.
  139. /// @param begin iterator pointing at beginning of a buffer to
  140. /// be used to create option instance.
  141. /// @param end iterator pointing at end of a buffer to be
  142. /// used to create option instance.
  143. /// @param expected_type type of the option created by the
  144. /// factory function returned by the option definition.
  145. /// @param encapsulates name of the option space being encapsulated
  146. /// by the option.
  147. static void testStdOptionDefs(const Option::Universe u,
  148. const uint16_t code,
  149. const OptionBufferConstIter begin,
  150. const OptionBufferConstIter end,
  151. const std::type_info& expected_type,
  152. const std::string& encapsulates) {
  153. // Get all option definitions, we will use them to extract
  154. // the definition for a particular option code.
  155. // We don't have to initialize option definitions here because they
  156. // are initialized in the class's constructor.
  157. OptionDefContainer options = LibDHCP::getOptionDefs(u);
  158. // Get the container index #1. This one allows for searching
  159. // option definitions using option code.
  160. const OptionDefContainerTypeIndex& idx = options.get<1>();
  161. // Get 'all' option definitions for a particular option code.
  162. // For standard options we expect that the range returned
  163. // will contain single option as their codes are unique.
  164. OptionDefContainerTypeRange range = idx.equal_range(code);
  165. ASSERT_EQ(1, std::distance(range.first, range.second))
  166. << "Standard option definition for the code " << code
  167. << " has not been found.";
  168. // If we have single option definition returned, the
  169. // first iterator holds it.
  170. OptionDefinitionPtr def = *(range.first);
  171. // It should not happen that option definition is NULL but
  172. // let's make sure (test should take things like that into
  173. // account).
  174. ASSERT_TRUE(def) << "Option definition for the code "
  175. << code << " is NULL.";
  176. // Check that option definition is valid.
  177. ASSERT_NO_THROW(def->validate())
  178. << "Option definition for the option code " << code
  179. << " is invalid";
  180. // Check that the valid encapsulated option space name
  181. // has been specified.
  182. EXPECT_EQ(encapsulates, def->getEncapsulatedSpace());
  183. OptionPtr option;
  184. // Create the option.
  185. ASSERT_NO_THROW(option = def->optionFactory(u, code, begin, end))
  186. << "Option creation failed for option code " << code;
  187. // Make sure it is not NULL.
  188. ASSERT_TRUE(option);
  189. // And the actual object type is the one that we expect.
  190. // Note that for many options there are dedicated classes
  191. // derived from Option class to represent them.
  192. EXPECT_TRUE(typeid(*option) == expected_type)
  193. << "Invalid class returned for option code " << code;
  194. }
  195. };
  196. // The DHCPv6 options in the wire format, used by multiple tests.
  197. const uint8_t v6packed[] = {
  198. 0, 1, 0, 5, 100, 101, 102, 103, 104, // CLIENT_ID (9 bytes)
  199. 0, 2, 0, 3, 105, 106, 107, // SERVER_ID (7 bytes)
  200. 0, 14, 0, 0, // RAPID_COMMIT (0 bytes)
  201. 0, 6, 0, 4, 108, 109, 110, 111, // ORO (8 bytes)
  202. 0, 8, 0, 2, 112, 113, // ELAPSED_TIME (6 bytes)
  203. // Vendor Specific Information Option starts here
  204. 0x00, 0x11, // VSI Option Code
  205. 0x00, 0x16, // VSI Option Length
  206. 0x00, 0x00, 0x11, 0x8B, // Enterprise ID
  207. 0x04, 0x01, // CMTS Capabilities Option
  208. 0x00, 0x04, // Length
  209. 0x01, 0x02,
  210. 0x03, 0x00, // DOCSIS Version Number
  211. 0x04, 0x02, // CM MAC Address Suboption
  212. 0x00, 0x06, // Length
  213. 0x74, 0x56, 0x12, 0x29, 0x97, 0xD0, // Actual MAC Address
  214. };
  215. TEST_F(LibDhcpTest, optionFactory) {
  216. OptionBuffer buf;
  217. // Factory functions for specific options must be registered before
  218. // they can be used to create options instances. Otherwise exception
  219. // is rised.
  220. EXPECT_THROW(LibDHCP::optionFactory(Option::V4, DHO_SUBNET_MASK, buf),
  221. isc::BadValue);
  222. // Let's register some factory functions (two v4 and one v6 function).
  223. // Registration may trigger exception if function for the specified
  224. // option has been registered already.
  225. ASSERT_NO_THROW(
  226. LibDHCP::OptionFactoryRegister(Option::V4, DHO_SUBNET_MASK,
  227. &LibDhcpTest::genericOptionFactory);
  228. );
  229. ASSERT_NO_THROW(
  230. LibDHCP::OptionFactoryRegister(Option::V4, DHO_TIME_OFFSET,
  231. &LibDhcpTest::genericOptionFactory);
  232. );
  233. ASSERT_NO_THROW(
  234. LibDHCP::OptionFactoryRegister(Option::V6, D6O_CLIENTID,
  235. &LibDhcpTest::genericOptionFactory);
  236. );
  237. // Invoke factory functions for all options (check if registration
  238. // was successful).
  239. OptionPtr opt_subnet_mask;
  240. opt_subnet_mask = LibDHCP::optionFactory(Option::V4,
  241. DHO_SUBNET_MASK,
  242. buf);
  243. // Check if non-NULL DHO_SUBNET_MASK option pointer has been returned.
  244. ASSERT_TRUE(opt_subnet_mask);
  245. // Validate if type and universe is correct.
  246. EXPECT_EQ(Option::V4, opt_subnet_mask->getUniverse());
  247. EXPECT_EQ(DHO_SUBNET_MASK, opt_subnet_mask->getType());
  248. // Expect that option does not have content..
  249. EXPECT_EQ(0, opt_subnet_mask->len() - opt_subnet_mask->getHeaderLen());
  250. // Fill the time offset buffer with 4 bytes of data. Each byte set to 1.
  251. OptionBuffer time_offset_buf(4, 1);
  252. OptionPtr opt_time_offset;
  253. opt_time_offset = LibDHCP::optionFactory(Option::V4,
  254. DHO_TIME_OFFSET,
  255. time_offset_buf);
  256. // Check if non-NULL DHO_TIME_OFFSET option pointer has been returned.
  257. ASSERT_TRUE(opt_time_offset);
  258. // Validate if option length, type and universe is correct.
  259. EXPECT_EQ(Option::V4, opt_time_offset->getUniverse());
  260. EXPECT_EQ(DHO_TIME_OFFSET, opt_time_offset->getType());
  261. EXPECT_EQ(time_offset_buf.size(),
  262. opt_time_offset->len() - opt_time_offset->getHeaderLen());
  263. // Validate data in the option.
  264. EXPECT_TRUE(std::equal(time_offset_buf.begin(), time_offset_buf.end(),
  265. opt_time_offset->getData().begin()));
  266. // Fill the client id buffer with 20 bytes of data. Each byte set to 2.
  267. OptionBuffer clientid_buf(20, 2);
  268. OptionPtr opt_clientid;
  269. opt_clientid = LibDHCP::optionFactory(Option::V6,
  270. D6O_CLIENTID,
  271. clientid_buf);
  272. // Check if non-NULL D6O_CLIENTID option pointer has been returned.
  273. ASSERT_TRUE(opt_clientid);
  274. // Validate if option length, type and universe is correct.
  275. EXPECT_EQ(Option::V6, opt_clientid->getUniverse());
  276. EXPECT_EQ(D6O_CLIENTID, opt_clientid->getType());
  277. EXPECT_EQ(clientid_buf.size(), opt_clientid->len() - opt_clientid->getHeaderLen());
  278. // Validate data in the option.
  279. EXPECT_TRUE(std::equal(clientid_buf.begin(), clientid_buf.end(),
  280. opt_clientid->getData().begin()));
  281. }
  282. TEST_F(LibDhcpTest, packOptions6) {
  283. OptionBuffer buf(512);
  284. isc::dhcp::OptionCollection opts; // list of options
  285. // generate content for options
  286. for (int i = 0; i < 64; i++) {
  287. buf[i]=i+100;
  288. }
  289. OptionPtr opt1(new Option(Option::V6, 1, buf.begin() + 0, buf.begin() + 5));
  290. OptionPtr opt2(new Option(Option::V6, 2, buf.begin() + 5, buf.begin() + 8));
  291. OptionPtr opt3(new Option(Option::V6, 14, buf.begin() + 8, buf.begin() + 8));
  292. OptionPtr opt4(new Option(Option::V6, 6, buf.begin() + 8, buf.begin() + 12));
  293. OptionPtr opt5(new Option(Option::V6, 8, buf.begin() + 12, buf.begin() + 14));
  294. OptionPtr cm_mac(new Option(Option::V6, OPTION_CM_MAC,
  295. OptionBuffer(v6packed + 54, v6packed + 60)));
  296. OptionPtr cmts_caps(new Option(Option::V6, OPTION_CMTS_CAPS,
  297. OptionBuffer(v6packed + 46, v6packed + 50)));
  298. boost::shared_ptr<OptionInt<uint32_t> >
  299. vsi(new OptionInt<uint32_t>(Option::V6, D6O_VENDOR_OPTS, 4491));
  300. vsi->addOption(cm_mac);
  301. vsi->addOption(cmts_caps);
  302. opts.insert(make_pair(opt1->getType(), opt1));
  303. opts.insert(make_pair(opt1->getType(), opt2));
  304. opts.insert(make_pair(opt1->getType(), opt3));
  305. opts.insert(make_pair(opt1->getType(), opt4));
  306. opts.insert(make_pair(opt1->getType(), opt5));
  307. opts.insert(make_pair(opt1->getType(), vsi));
  308. OutputBuffer assembled(512);
  309. EXPECT_NO_THROW(LibDHCP::packOptions(assembled, opts));
  310. EXPECT_EQ(sizeof(v6packed), assembled.getLength());
  311. EXPECT_EQ(0, memcmp(assembled.getData(), v6packed, sizeof(v6packed)));
  312. }
  313. TEST_F(LibDhcpTest, unpackOptions6) {
  314. // just couple of random options
  315. // Option is used as a simple option implementation
  316. // More advanced uses are validated in tests dedicated for
  317. // specific derived classes.
  318. isc::dhcp::OptionCollection options; // list of options
  319. OptionBuffer buf(512);
  320. memcpy(&buf[0], v6packed, sizeof(v6packed));
  321. EXPECT_NO_THROW ({
  322. LibDHCP::unpackOptions6(OptionBuffer(buf.begin(), buf.begin() + sizeof(v6packed)),
  323. "dhcp6", options);
  324. });
  325. EXPECT_EQ(options.size(), 6); // there should be 5 options
  326. isc::dhcp::OptionCollection::const_iterator x = options.find(1);
  327. ASSERT_FALSE(x == options.end()); // option 1 should exist
  328. EXPECT_EQ(1, x->second->getType()); // this should be option 1
  329. ASSERT_EQ(9, x->second->len()); // it should be of length 9
  330. ASSERT_EQ(5, x->second->getData().size());
  331. EXPECT_EQ(0, memcmp(&x->second->getData()[0], v6packed + 4, 5)); // data len=5
  332. x = options.find(2);
  333. ASSERT_FALSE(x == options.end()); // option 2 should exist
  334. EXPECT_EQ(2, x->second->getType()); // this should be option 2
  335. ASSERT_EQ(7, x->second->len()); // it should be of length 7
  336. ASSERT_EQ(3, x->second->getData().size());
  337. EXPECT_EQ(0, memcmp(&x->second->getData()[0], v6packed + 13, 3)); // data len=3
  338. x = options.find(14);
  339. ASSERT_FALSE(x == options.end()); // option 14 should exist
  340. EXPECT_EQ(14, x->second->getType()); // this should be option 14
  341. ASSERT_EQ(4, x->second->len()); // it should be of length 4
  342. EXPECT_EQ(0, x->second->getData().size()); // data len = 0
  343. x = options.find(6);
  344. ASSERT_FALSE(x == options.end()); // option 6 should exist
  345. EXPECT_EQ(6, x->second->getType()); // this should be option 6
  346. ASSERT_EQ(8, x->second->len()); // it should be of length 8
  347. // Option with code 6 is the OPTION_ORO. This option is
  348. // represented by the OptionIntArray<uint16_t> class which
  349. // comprises the set of uint16_t values. We need to cast the
  350. // returned pointer to this type to get values stored in it.
  351. boost::shared_ptr<OptionIntArray<uint16_t> > opt_oro =
  352. boost::dynamic_pointer_cast<OptionIntArray<uint16_t> >(x->second);
  353. // This value will be NULL if cast was unsuccessful. This is the case
  354. // when returned option has different type than expected.
  355. ASSERT_TRUE(opt_oro);
  356. // Get set of uint16_t values.
  357. std::vector<uint16_t> opts = opt_oro->getValues();
  358. // Prepare the refrence data.
  359. std::vector<uint16_t> expected_opts;
  360. expected_opts.push_back(0x6C6D); // equivalent to: 108, 109
  361. expected_opts.push_back(0x6E6F); // equivalent to 110, 111
  362. ASSERT_EQ(expected_opts.size(), opts.size());
  363. // Validated if option has been unpacked correctly.
  364. EXPECT_TRUE(std::equal(expected_opts.begin(), expected_opts.end(),
  365. opts.begin()));
  366. x = options.find(8);
  367. ASSERT_FALSE(x == options.end()); // option 8 should exist
  368. EXPECT_EQ(8, x->second->getType()); // this should be option 8
  369. ASSERT_EQ(6, x->second->len()); // it should be of length 9
  370. // Option with code 8 is OPTION_ELAPSED_TIME. This option is
  371. // represented by Option6Int<uint16_t> value that holds single
  372. // uint16_t value.
  373. boost::shared_ptr<OptionInt<uint16_t> > opt_elapsed_time =
  374. boost::dynamic_pointer_cast<OptionInt<uint16_t> >(x->second);
  375. // This value will be NULL if cast was unsuccessful. This is the case
  376. // when returned option has different type than expected.
  377. ASSERT_TRUE(opt_elapsed_time);
  378. // Returned value should be equivalent to two byte values: 112, 113
  379. EXPECT_EQ(0x7071, opt_elapsed_time->getValue());
  380. // Check if Vendor Specific Information Option along with suboptions
  381. // have been parsed correctly.
  382. x = options.find(D6O_VENDOR_OPTS);
  383. EXPECT_FALSE(x == options.end());
  384. EXPECT_EQ(D6O_VENDOR_OPTS, x->second->getType());
  385. EXPECT_EQ(26, x->second->len());
  386. // CM MAC Address Option
  387. OptionPtr cm_mac = x->second->getOption(OPTION_CM_MAC);
  388. ASSERT_TRUE(cm_mac);
  389. EXPECT_EQ(OPTION_CM_MAC, cm_mac->getType());
  390. ASSERT_EQ(10, cm_mac->len());
  391. EXPECT_EQ(0, memcmp(&cm_mac->getData()[0], v6packed + 54, 6));
  392. // CMTS Capabilities
  393. OptionPtr cmts_caps = x->second->getOption(OPTION_CMTS_CAPS);
  394. ASSERT_TRUE(cmts_caps);
  395. EXPECT_EQ(OPTION_CMTS_CAPS, cmts_caps->getType());
  396. ASSERT_EQ(8, cmts_caps->len());
  397. EXPECT_EQ(0, memcmp(&cmts_caps->getData()[0], v6packed + 46, 4));
  398. x = options.find(0);
  399. EXPECT_TRUE(x == options.end()); // option 0 not found
  400. x = options.find(256); // 256 is htons(1) on little endians. Worth checking
  401. EXPECT_TRUE(x == options.end()); // option 1 not found
  402. x = options.find(7);
  403. EXPECT_TRUE(x == options.end()); // option 2 not found
  404. x = options.find(32000);
  405. EXPECT_TRUE(x == options.end()); // option 32000 not found */
  406. }
  407. /// V4 Options being used to test pack/unpack operations.
  408. /// These are variable length options only so as there
  409. /// is no restriction on the data length being carried by them.
  410. /// For simplicity, we assign data of the length 3 for each
  411. /// of them.
  412. static uint8_t v4_opts[] = {
  413. 12, 3, 0, 1, 2, // Hostname
  414. 60, 3, 10, 11, 12, // Class Id
  415. 14, 3, 20, 21, 22, // Merit Dump File
  416. 254, 3, 30, 31, 32, // Reserved
  417. 128, 3, 40, 41, 42, // Vendor specific
  418. 0x52, 0x19, // RAI
  419. 0x01, 0x04, 0x20, 0x00, 0x00, 0x02, // Agent Circuit ID
  420. 0x02, 0x06, 0x20, 0xE5, 0x2A, 0xB8, 0x15, 0x14, // Agent Remote ID
  421. 0x09, 0x09, 0x00, 0x00, 0x11, 0x8B, 0x04, // Vendor Specific Information
  422. 0x01, 0x02, 0x03, 0x00 // Vendor Specific Information continued
  423. };
  424. TEST_F(LibDhcpTest, packOptions4) {
  425. vector<uint8_t> payload[5];
  426. for (int i = 0; i < 5; i++) {
  427. payload[i].resize(3);
  428. payload[i][0] = i*10;
  429. payload[i][1] = i*10+1;
  430. payload[i][2] = i*10+2;
  431. }
  432. OptionPtr opt1(new Option(Option::V4, 12, payload[0]));
  433. OptionPtr opt2(new Option(Option::V4, 60, payload[1]));
  434. OptionPtr opt3(new Option(Option::V4, 14, payload[2]));
  435. OptionPtr opt4(new Option(Option::V4,254, payload[3]));
  436. OptionPtr opt5(new Option(Option::V4,128, payload[4]));
  437. // Add RAI option, which comprises 3 sub-options.
  438. // Get the option definition for RAI option. This option is represented
  439. // by OptionCustom which requires a definition to be passed to
  440. // the constructor.
  441. OptionDefinitionPtr rai_def = LibDHCP::getOptionDef(Option::V4,
  442. DHO_DHCP_AGENT_OPTIONS);
  443. ASSERT_TRUE(rai_def);
  444. // Create RAI option.
  445. OptionCustomPtr rai(new OptionCustom(*rai_def, Option::V4));
  446. // The sub-options are created using the bits of v4_opts buffer because
  447. // we want to use this buffer as a reference to verify that produced
  448. // option in on-wire format is correct.
  449. // Create Ciruit ID sub-option and add to RAI.
  450. OptionPtr circuit_id(new Option(Option::V4, RAI_OPTION_AGENT_CIRCUIT_ID,
  451. OptionBuffer(v4_opts + 29,
  452. v4_opts + 33)));
  453. rai->addOption(circuit_id);
  454. // Create Remote ID option and add to RAI.
  455. OptionPtr remote_id(new Option(Option::V4, RAI_OPTION_REMOTE_ID,
  456. OptionBuffer(v4_opts + 35, v4_opts + 41)));
  457. rai->addOption(remote_id);
  458. // Create Vendor Specific Information and add to RAI.
  459. OptionPtr vsi(new Option(Option::V4, RAI_OPTION_VSI,
  460. OptionBuffer(v4_opts + 43, v4_opts + 52)));
  461. rai->addOption(vsi);
  462. isc::dhcp::OptionCollection opts; // list of options
  463. // Note that we insert each option under the same option code into
  464. // the map. This gurantees that options are packed in the same order
  465. // they were added. Otherwise, options would get sorted by code and
  466. // the resulting buffer wouldn't match with the reference buffer.
  467. opts.insert(make_pair(opt1->getType(), opt1));
  468. opts.insert(make_pair(opt1->getType(), opt2));
  469. opts.insert(make_pair(opt1->getType(), opt3));
  470. opts.insert(make_pair(opt1->getType(), opt4));
  471. opts.insert(make_pair(opt1->getType(), opt5));
  472. opts.insert(make_pair(opt1->getType(), rai));
  473. OutputBuffer buf(100);
  474. EXPECT_NO_THROW(LibDHCP::packOptions(buf, opts));
  475. ASSERT_EQ(buf.getLength(), sizeof(v4_opts));
  476. EXPECT_EQ(0, memcmp(v4_opts, buf.getData(), sizeof(v4_opts)));
  477. }
  478. TEST_F(LibDhcpTest, unpackOptions4) {
  479. vector<uint8_t> v4packed(v4_opts, v4_opts + sizeof(v4_opts));
  480. isc::dhcp::OptionCollection options; // list of options
  481. ASSERT_NO_THROW(
  482. LibDHCP::unpackOptions4(v4packed, "dhcp4", options);
  483. );
  484. isc::dhcp::OptionCollection::const_iterator x = options.find(12);
  485. ASSERT_FALSE(x == options.end()); // option 1 should exist
  486. // Option 12 holds a string so let's cast it to an appropriate type.
  487. OptionStringPtr option12 = boost::static_pointer_cast<OptionString>(x->second);
  488. ASSERT_TRUE(option12);
  489. EXPECT_EQ(12, option12->getType()); // this should be option 12
  490. ASSERT_EQ(3, option12->getValue().length()); // it should be of length 3
  491. EXPECT_EQ(5, option12->len()); // total option length 5
  492. EXPECT_EQ(0, memcmp(&option12->getValue()[0], v4_opts + 2, 3)); // data len=3
  493. x = options.find(60);
  494. ASSERT_FALSE(x == options.end()); // option 2 should exist
  495. EXPECT_EQ(60, x->second->getType()); // this should be option 60
  496. ASSERT_EQ(3, x->second->getData().size()); // it should be of length 3
  497. EXPECT_EQ(5, x->second->len()); // total option length 5
  498. EXPECT_EQ(0, memcmp(&x->second->getData()[0], v4_opts + 7, 3)); // data len=3
  499. x = options.find(14);
  500. ASSERT_FALSE(x == options.end()); // option 3 should exist
  501. OptionStringPtr option14 = boost::static_pointer_cast<OptionString>(x->second);
  502. ASSERT_TRUE(option14);
  503. EXPECT_EQ(14, option14->getType()); // this should be option 14
  504. ASSERT_EQ(3, option14->getValue().length()); // it should be of length 3
  505. EXPECT_EQ(5, option14->len()); // total option length 5
  506. EXPECT_EQ(0, memcmp(&option14->getValue()[0], v4_opts + 12, 3)); // data len=3
  507. x = options.find(254);
  508. ASSERT_FALSE(x == options.end()); // option 3 should exist
  509. EXPECT_EQ(254, x->second->getType()); // this should be option 254
  510. ASSERT_EQ(3, x->second->getData().size()); // it should be of length 3
  511. EXPECT_EQ(5, x->second->len()); // total option length 5
  512. EXPECT_EQ(0, memcmp(&x->second->getData()[0], v4_opts + 17, 3)); // data len=3
  513. x = options.find(128);
  514. ASSERT_FALSE(x == options.end()); // option 3 should exist
  515. EXPECT_EQ(128, x->second->getType()); // this should be option 254
  516. ASSERT_EQ(3, x->second->getData().size()); // it should be of length 3
  517. EXPECT_EQ(5, x->second->len()); // total option length 5
  518. EXPECT_EQ(0, memcmp(&x->second->getData()[0], v4_opts + 22, 3)); // data len=3
  519. // Checking DHCP Relay Agent Information Option.
  520. x = options.find(DHO_DHCP_AGENT_OPTIONS);
  521. ASSERT_FALSE(x == options.end());
  522. EXPECT_EQ(DHO_DHCP_AGENT_OPTIONS, x->second->getType());
  523. // RAI is represented by OptionCustom.
  524. OptionCustomPtr rai = boost::dynamic_pointer_cast<OptionCustom>(x->second);
  525. ASSERT_TRUE(rai);
  526. // RAI should have 3 sub-options: Circuit ID, Agent Remote ID, Vendor
  527. // Specific Information option. Note that by parsing these suboptions we
  528. // are checking that unpackOptions4 differentiates between standard option
  529. // space called "dhcp4" and other option spaces. These sub-options do not
  530. // belong to standard option space and should be parsed using different
  531. // option definitions.
  532. // @todo Currently, definitions for option space "dhcp-agent-options-space"
  533. // are not defined. Therefore all suboptions will be represented here by
  534. // the generic Option class.
  535. // Check that Circuit ID option is among parsed options.
  536. OptionPtr rai_option = rai->getOption(RAI_OPTION_AGENT_CIRCUIT_ID);
  537. ASSERT_TRUE(rai_option);
  538. EXPECT_EQ(RAI_OPTION_AGENT_CIRCUIT_ID, rai_option->getType());
  539. ASSERT_EQ(6, rai_option->len());
  540. EXPECT_EQ(0, memcmp(&rai_option->getData()[0], v4_opts + 29, 4));
  541. // Check that Remote ID option is among parsed options.
  542. rai_option = rai->getOption(RAI_OPTION_REMOTE_ID);
  543. ASSERT_TRUE(rai_option);
  544. EXPECT_EQ(RAI_OPTION_REMOTE_ID, rai_option->getType());
  545. ASSERT_EQ(8, rai_option->len());
  546. EXPECT_EQ(0, memcmp(&rai_option->getData()[0], v4_opts + 35, 6));
  547. // Check that Vendor Specific Information option is among parsed options.
  548. rai_option = rai->getOption(RAI_OPTION_VSI);
  549. ASSERT_TRUE(rai_option);
  550. EXPECT_EQ(RAI_OPTION_VSI, rai_option->getType());
  551. ASSERT_EQ(11, rai_option->len());
  552. EXPECT_EQ(0, memcmp(&rai_option->getData()[0], v4_opts + 43, 9));
  553. // Make sure, that option other than those above is not present.
  554. EXPECT_FALSE(rai->getOption(10));
  555. // Check the same for the global option space.
  556. x = options.find(0);
  557. EXPECT_TRUE(x == options.end()); // option 0 not found
  558. x = options.find(1);
  559. EXPECT_TRUE(x == options.end()); // option 1 not found
  560. x = options.find(2);
  561. EXPECT_TRUE(x == options.end()); // option 2 not found
  562. }
  563. TEST_F(LibDhcpTest, isStandardOption4) {
  564. // Get all option codes that are not occupied by standard options.
  565. const uint16_t unassigned_codes[] = { 84, 96, 102, 103, 104, 105, 106, 107, 108,
  566. 109, 110, 111, 115, 126, 127, 147, 148, 149,
  567. 178, 179, 180, 181, 182, 183, 184, 185, 186,
  568. 187, 188, 189, 190, 191, 192, 193, 194, 195,
  569. 196, 197, 198, 199, 200, 201, 202, 203, 204,
  570. 205, 206, 207, 214, 215, 216, 217, 218, 219,
  571. 222, 223, 224, 225, 226, 227, 228, 229, 230,
  572. 231, 232, 233, 234, 235, 236, 237, 238, 239,
  573. 240, 241, 242, 243, 244, 245, 246, 247, 248,
  574. 249, 250, 251, 252, 253, 254 };
  575. const size_t unassigned_num = sizeof(unassigned_codes) / sizeof(unassigned_codes[0]);
  576. // Try all possible option codes.
  577. for (size_t i = 0; i < 256; ++i) {
  578. // Some ranges of option codes are unassigned and thus the isStandardOption
  579. // should return false for them.
  580. bool check_unassigned = false;
  581. // Check the array of unassigned options to find out whether option code
  582. // is assigned to standard option or unassigned.
  583. for (size_t j = 0; j < unassigned_num; ++j) {
  584. // If option code is found within the array of unassigned options
  585. // we the isStandardOption function should return false.
  586. if (unassigned_codes[j] == i) {
  587. check_unassigned = true;
  588. EXPECT_FALSE(LibDHCP::isStandardOption(Option::V4,
  589. unassigned_codes[j]))
  590. << "Test failed for option code " << unassigned_codes[j];
  591. break;
  592. }
  593. }
  594. // If the option code belongs to the standard option then the
  595. // isStandardOption should return true.
  596. if (!check_unassigned) {
  597. EXPECT_TRUE(LibDHCP::isStandardOption(Option::V4, i))
  598. << "Test failed for the option code " << i;
  599. }
  600. }
  601. }
  602. TEST_F(LibDhcpTest, isStandardOption6) {
  603. // All option codes in the range from 0 to 78 (except 10 and 35)
  604. // identify the standard options.
  605. for (uint16_t code = 0; code < 79; ++code) {
  606. if (code != 10 && code != 35) {
  607. EXPECT_TRUE(LibDHCP::isStandardOption(Option::V6, code))
  608. << "Test failed for option code " << code;
  609. }
  610. }
  611. // Check the option codes 10 and 35. They are unassigned.
  612. EXPECT_FALSE(LibDHCP::isStandardOption(Option::V6, 10));
  613. EXPECT_FALSE(LibDHCP::isStandardOption(Option::V6, 35));
  614. // Check a range of option codes above 78. Those are option codes
  615. // identifying non-standard options.
  616. for (uint16_t code = 79; code < 512; ++code) {
  617. EXPECT_FALSE(LibDHCP::isStandardOption(Option::V6, code))
  618. << "Test failed for option code " << code;
  619. }
  620. }
  621. TEST_F(LibDhcpTest, stdOptionDefs4) {
  622. // Create a buffer that holds dummy option data.
  623. // It will be used to create most of the options.
  624. std::vector<uint8_t> buf(48, 1);
  625. OptionBufferConstIter begin = buf.begin();
  626. OptionBufferConstIter end = buf.end();
  627. LibDhcpTest::testStdOptionDefs4(DHO_SUBNET_MASK, begin, end,
  628. typeid(OptionCustom));
  629. LibDhcpTest::testStdOptionDefs4(DHO_TIME_OFFSET, begin, begin + 4,
  630. typeid(OptionInt<int32_t>));
  631. LibDhcpTest::testStdOptionDefs4(DHO_ROUTERS, begin, end,
  632. typeid(Option4AddrLst));
  633. LibDhcpTest::testStdOptionDefs4(DHO_TIME_SERVERS, begin, end,
  634. typeid(Option4AddrLst));
  635. LibDhcpTest::testStdOptionDefs4(DHO_NAME_SERVERS, begin, end,
  636. typeid(OptionCustom));
  637. LibDhcpTest::testStdOptionDefs4(DHO_DOMAIN_NAME_SERVERS, begin, end,
  638. typeid(Option4AddrLst));
  639. LibDhcpTest::testStdOptionDefs4(DHO_LOG_SERVERS, begin, end,
  640. typeid(Option4AddrLst));
  641. LibDhcpTest::testStdOptionDefs4(DHO_COOKIE_SERVERS, begin, end,
  642. typeid(Option4AddrLst));
  643. LibDhcpTest::testStdOptionDefs4(DHO_LPR_SERVERS, begin, end,
  644. typeid(Option4AddrLst));
  645. LibDhcpTest::testStdOptionDefs4(DHO_IMPRESS_SERVERS, begin, end,
  646. typeid(Option4AddrLst));
  647. LibDhcpTest::testStdOptionDefs4(DHO_RESOURCE_LOCATION_SERVERS, begin, end,
  648. typeid(Option4AddrLst));
  649. LibDhcpTest::testStdOptionDefs4(DHO_HOST_NAME, begin, end,
  650. typeid(OptionString));
  651. LibDhcpTest::testStdOptionDefs4(DHO_BOOT_SIZE, begin, begin + 2,
  652. typeid(OptionInt<uint16_t>));
  653. LibDhcpTest::testStdOptionDefs4(DHO_MERIT_DUMP, begin, end,
  654. typeid(OptionString));
  655. LibDhcpTest::testStdOptionDefs4(DHO_DOMAIN_NAME, begin, end,
  656. typeid(OptionString));
  657. LibDhcpTest::testStdOptionDefs4(DHO_SWAP_SERVER, begin, end,
  658. typeid(OptionCustom));
  659. LibDhcpTest::testStdOptionDefs4(DHO_ROOT_PATH, begin, end,
  660. typeid(OptionString));
  661. LibDhcpTest::testStdOptionDefs4(DHO_EXTENSIONS_PATH, begin, end,
  662. typeid(OptionString));
  663. LibDhcpTest::testStdOptionDefs4(DHO_IP_FORWARDING, begin, end,
  664. typeid(OptionCustom));
  665. LibDhcpTest::testStdOptionDefs4(DHO_NON_LOCAL_SOURCE_ROUTING, begin, end,
  666. typeid(OptionCustom));
  667. LibDhcpTest::testStdOptionDefs4(DHO_POLICY_FILTER, begin, end,
  668. typeid(Option4AddrLst));
  669. LibDhcpTest::testStdOptionDefs4(DHO_MAX_DGRAM_REASSEMBLY, begin, begin + 2,
  670. typeid(OptionInt<uint16_t>));
  671. LibDhcpTest::testStdOptionDefs4(DHO_DEFAULT_IP_TTL, begin, begin + 1,
  672. typeid(OptionInt<uint8_t>));
  673. LibDhcpTest::testStdOptionDefs4(DHO_PATH_MTU_AGING_TIMEOUT, begin, begin + 4,
  674. typeid(OptionInt<uint32_t>));
  675. LibDhcpTest::testStdOptionDefs4(DHO_PATH_MTU_PLATEAU_TABLE, begin, begin + 10,
  676. typeid(OptionIntArray<uint16_t>));
  677. LibDhcpTest::testStdOptionDefs4(DHO_INTERFACE_MTU, begin, begin + 2,
  678. typeid(OptionInt<uint16_t>));
  679. LibDhcpTest::testStdOptionDefs4(DHO_ALL_SUBNETS_LOCAL, begin, end,
  680. typeid(OptionCustom));
  681. LibDhcpTest::testStdOptionDefs4(DHO_BROADCAST_ADDRESS, begin, end,
  682. typeid(OptionCustom));
  683. LibDhcpTest::testStdOptionDefs4(DHO_PERFORM_MASK_DISCOVERY, begin, end,
  684. typeid(OptionCustom));
  685. LibDhcpTest::testStdOptionDefs4(DHO_MASK_SUPPLIER, begin, end,
  686. typeid(OptionCustom));
  687. LibDhcpTest::testStdOptionDefs4(DHO_ROUTER_DISCOVERY, begin, end,
  688. typeid(OptionCustom));
  689. LibDhcpTest::testStdOptionDefs4(DHO_ROUTER_SOLICITATION_ADDRESS, begin, end,
  690. typeid(OptionCustom));
  691. LibDhcpTest::testStdOptionDefs4(DHO_STATIC_ROUTES, begin, end,
  692. typeid(Option4AddrLst));
  693. LibDhcpTest::testStdOptionDefs4(DHO_TRAILER_ENCAPSULATION, begin, end,
  694. typeid(OptionCustom));
  695. LibDhcpTest::testStdOptionDefs4(DHO_ARP_CACHE_TIMEOUT, begin, begin + 4,
  696. typeid(OptionInt<uint32_t>));
  697. LibDhcpTest::testStdOptionDefs4(DHO_IEEE802_3_ENCAPSULATION, begin, end,
  698. typeid(OptionCustom));
  699. LibDhcpTest::testStdOptionDefs4(DHO_DEFAULT_TCP_TTL, begin, begin + 1,
  700. typeid(OptionInt<uint8_t>));
  701. LibDhcpTest::testStdOptionDefs4(DHO_TCP_KEEPALIVE_INTERVAL, begin,
  702. begin + 4, typeid(OptionInt<uint32_t>));
  703. LibDhcpTest::testStdOptionDefs4(DHO_TCP_KEEPALIVE_GARBAGE, begin, begin + 1,
  704. typeid(OptionCustom));
  705. LibDhcpTest::testStdOptionDefs4(DHO_NIS_DOMAIN, begin, end,
  706. typeid(OptionString));
  707. LibDhcpTest::testStdOptionDefs4(DHO_NIS_SERVERS, begin, end,
  708. typeid(Option4AddrLst));
  709. LibDhcpTest::testStdOptionDefs4(DHO_NTP_SERVERS, begin, end,
  710. typeid(Option4AddrLst));
  711. // The following option requires well formed buffer to be created from.
  712. // Not just a dummy one. This buffer includes some suboptions.
  713. OptionBuffer vendor_opts_buf = createVendorOption();
  714. LibDhcpTest::testStdOptionDefs4(DHO_VENDOR_ENCAPSULATED_OPTIONS,
  715. vendor_opts_buf.begin(),
  716. vendor_opts_buf.end(),
  717. typeid(OptionCustom),
  718. "vendor-encapsulated-options-space");
  719. LibDhcpTest::testStdOptionDefs4(DHO_NETBIOS_NAME_SERVERS, begin, end,
  720. typeid(Option4AddrLst));
  721. LibDhcpTest::testStdOptionDefs4(DHO_NETBIOS_DD_SERVER, begin, end,
  722. typeid(Option4AddrLst));
  723. LibDhcpTest::testStdOptionDefs4(DHO_NETBIOS_NODE_TYPE, begin, begin + 1,
  724. typeid(OptionInt<uint8_t>));
  725. LibDhcpTest::testStdOptionDefs4(DHO_NETBIOS_SCOPE, begin, end,
  726. typeid(OptionString));
  727. LibDhcpTest::testStdOptionDefs4(DHO_FONT_SERVERS, begin, end,
  728. typeid(Option4AddrLst));
  729. LibDhcpTest::testStdOptionDefs4(DHO_X_DISPLAY_MANAGER, begin, end,
  730. typeid(Option4AddrLst));
  731. LibDhcpTest::testStdOptionDefs4(DHO_DHCP_REQUESTED_ADDRESS, begin, end,
  732. typeid(OptionCustom));
  733. LibDhcpTest::testStdOptionDefs4(DHO_DHCP_LEASE_TIME, begin, begin + 4,
  734. typeid(OptionInt<uint32_t>));
  735. LibDhcpTest::testStdOptionDefs4(DHO_DHCP_OPTION_OVERLOAD, begin, begin + 1,
  736. typeid(OptionInt<uint8_t>));
  737. LibDhcpTest::testStdOptionDefs4(DHO_DHCP_MESSAGE_TYPE, begin, begin + 1,
  738. typeid(OptionInt<uint8_t>));
  739. LibDhcpTest::testStdOptionDefs4(DHO_DHCP_SERVER_IDENTIFIER, begin, end,
  740. typeid(OptionCustom));
  741. LibDhcpTest::testStdOptionDefs4(DHO_DHCP_PARAMETER_REQUEST_LIST, begin, end,
  742. typeid(OptionUint8Array));
  743. LibDhcpTest::testStdOptionDefs4(DHO_DHCP_MESSAGE, begin, end,
  744. typeid(OptionString));
  745. LibDhcpTest::testStdOptionDefs4(DHO_DHCP_MAX_MESSAGE_SIZE, begin, begin + 2,
  746. typeid(OptionInt<uint16_t>));
  747. LibDhcpTest::testStdOptionDefs4(DHO_DHCP_RENEWAL_TIME, begin, begin + 4,
  748. typeid(OptionInt<uint32_t>));
  749. LibDhcpTest::testStdOptionDefs4(DHO_DHCP_REBINDING_TIME, begin, begin + 4,
  750. typeid(OptionInt<uint32_t>));
  751. LibDhcpTest::testStdOptionDefs4(DHO_VENDOR_CLASS_IDENTIFIER, begin, end,
  752. typeid(OptionString));
  753. LibDhcpTest::testStdOptionDefs4(DHO_DHCP_CLIENT_IDENTIFIER, begin, end,
  754. typeid(Option));
  755. LibDhcpTest::testStdOptionDefs4(DHO_NWIP_DOMAIN_NAME, begin, end,
  756. typeid(OptionString));
  757. LibDhcpTest::testStdOptionDefs4(DHO_NWIP_SUBOPTIONS, begin, end,
  758. typeid(Option));
  759. LibDhcpTest::testStdOptionDefs4(DHO_TFTP_SERVER_NAME, begin, end,
  760. typeid(OptionString));
  761. LibDhcpTest::testStdOptionDefs4(DHO_BOOT_FILE_NAME, begin, end,
  762. typeid(OptionString));
  763. LibDhcpTest::testStdOptionDefs4(DHO_USER_CLASS, begin, end,
  764. typeid(Option));
  765. LibDhcpTest::testStdOptionDefs4(DHO_FQDN, begin, begin + 3,
  766. typeid(Option4ClientFqdn));
  767. // The following option requires well formed buffer to be created from.
  768. // Not just a dummy one. This buffer includes some suboptions.
  769. OptionBuffer agent_info_buf = createAgentInformationOption();
  770. LibDhcpTest::testStdOptionDefs4(DHO_DHCP_AGENT_OPTIONS,
  771. agent_info_buf.begin(),
  772. agent_info_buf.end(),
  773. typeid(OptionCustom),
  774. "dhcp-agent-options-space");
  775. LibDhcpTest::testStdOptionDefs4(DHO_AUTHENTICATE, begin, end,
  776. typeid(Option));
  777. LibDhcpTest::testStdOptionDefs4(DHO_CLIENT_LAST_TRANSACTION_TIME,
  778. begin, begin + 4,
  779. typeid(OptionInt<uint32_t>));
  780. LibDhcpTest::testStdOptionDefs4(DHO_ASSOCIATED_IP, begin, end,
  781. typeid(Option4AddrLst));
  782. LibDhcpTest::testStdOptionDefs4(DHO_SUBNET_SELECTION, begin, end,
  783. typeid(OptionCustom));
  784. LibDhcpTest::testStdOptionDefs4(DHO_DOMAIN_SEARCH, begin, end,
  785. typeid(Option));
  786. // V-I Vendor option requires specially crafted data.
  787. const char vivco_data[] = {
  788. 1, 2, 3, 4, // enterprise id
  789. 3, 1, 2, 3 // first byte is opaque data length, the rest is opaque data
  790. };
  791. std::vector<uint8_t> vivco_buf(vivco_data, vivco_data + sizeof(vivco_data));
  792. LibDhcpTest::testStdOptionDefs4(DHO_VIVCO_SUBOPTIONS, vivco_buf.begin(),
  793. vivco_buf.end(), typeid(OptionVendorClass));
  794. LibDhcpTest::testStdOptionDefs4(DHO_VIVSO_SUBOPTIONS, begin, end,
  795. typeid(OptionVendor));
  796. }
  797. // Test that definitions of standard options have been initialized
  798. // correctly.
  799. // @todo Only limited number of option definitions are now created
  800. // This test have to be extended once all option definitions are
  801. // created.
  802. TEST_F(LibDhcpTest, stdOptionDefs6) {
  803. // Create a buffer that holds dummy option data.
  804. // It will be used to create most of the options.
  805. std::vector<uint8_t> buf(48, 1);
  806. OptionBufferConstIter begin = buf.begin();
  807. OptionBufferConstIter end = buf.end();
  808. // Prepare buffer holding an array of FQDNs.
  809. const char data[] = {
  810. 8, 109, 121, 100, 111, 109, 97, 105, 110, // "mydomain"
  811. 7, 101, 120, 97, 109, 112, 108, 101, // "example"
  812. 3, 99, 111, 109, // "com"
  813. 0,
  814. 7, 101, 120, 97, 109, 112, 108, 101, // "example"
  815. 3, 99, 111, 109, // "com"
  816. 0
  817. };
  818. // Initialize a vector with the FQDN data.
  819. std::vector<uint8_t> fqdn_buf(data, data + sizeof(data));
  820. // Prepare buffer holding a vendor option
  821. const char vopt_data[] = {
  822. 1, 2, 3, 4, // enterprise=0x1020304
  823. 0, 100, // type=100
  824. 0, 6, // length=6
  825. 102, 111, 111, 98, 97, 114 // data="foobar"
  826. };
  827. // Initialize a vector with the suboption data.
  828. std::vector<uint8_t> vopt_buf(vopt_data, vopt_data + sizeof(vopt_data));
  829. // The CLIENT_FQDN holds a uint8_t value and FQDN. We have
  830. // to add the uint8_t value to it and then append the buffer
  831. // holding some valid FQDN.
  832. std::vector<uint8_t> client_fqdn_buf(1);
  833. client_fqdn_buf.insert(client_fqdn_buf.end(), fqdn_buf.begin(),
  834. fqdn_buf.end());
  835. // Initialize test buffer for Vendor Class option.
  836. const char vclass_data[] = {
  837. 0x00, 0x01, 0x02, 0x03,
  838. 0x00, 0x01, 0x02
  839. };
  840. std::vector<uint8_t> vclass_buf(vclass_data,
  841. vclass_data + sizeof(vclass_data));;
  842. // The actual test starts here for all supported option codes.
  843. LibDhcpTest::testStdOptionDefs6(D6O_CLIENTID, begin, end,
  844. typeid(Option));
  845. LibDhcpTest::testStdOptionDefs6(D6O_SERVERID, begin, end,
  846. typeid(Option));
  847. LibDhcpTest::testStdOptionDefs6(D6O_IA_NA, begin, end,
  848. typeid(Option6IA));
  849. LibDhcpTest::testStdOptionDefs6(D6O_IA_TA, begin, begin + 4,
  850. typeid(OptionInt<uint32_t>));
  851. LibDhcpTest::testStdOptionDefs6(D6O_IAADDR, begin, end,
  852. typeid(Option6IAAddr));
  853. LibDhcpTest::testStdOptionDefs6(D6O_ORO, begin, end,
  854. typeid(OptionIntArray<uint16_t>));
  855. LibDhcpTest::testStdOptionDefs6(D6O_PREFERENCE, begin, begin + 1,
  856. typeid(OptionInt<uint8_t>));
  857. LibDhcpTest::testStdOptionDefs6(D6O_ELAPSED_TIME, begin, begin + 2,
  858. typeid(OptionInt<uint16_t>));
  859. LibDhcpTest::testStdOptionDefs6(D6O_RELAY_MSG, begin, end,
  860. typeid(Option));
  861. LibDhcpTest::testStdOptionDefs6(D6O_STATUS_CODE, begin, end,
  862. typeid(OptionCustom));
  863. LibDhcpTest::testStdOptionDefs6(D6O_RAPID_COMMIT, begin, end,
  864. typeid(Option));
  865. LibDhcpTest::testStdOptionDefs6(D6O_USER_CLASS, begin, end,
  866. typeid(Option));
  867. LibDhcpTest::testStdOptionDefs6(D6O_VENDOR_CLASS, vclass_buf.begin(),
  868. vclass_buf.end(),
  869. typeid(OptionVendorClass));
  870. LibDhcpTest::testStdOptionDefs6(D6O_VENDOR_OPTS, vopt_buf.begin(),
  871. vopt_buf.end(),
  872. typeid(OptionVendor),
  873. "vendor-opts-space");
  874. LibDhcpTest::testStdOptionDefs6(D6O_INTERFACE_ID, begin, end,
  875. typeid(Option));
  876. LibDhcpTest::testStdOptionDefs6(D6O_RECONF_MSG, begin, begin + 1,
  877. typeid(OptionInt<uint8_t>));
  878. LibDhcpTest::testStdOptionDefs6(D6O_RECONF_ACCEPT, begin, end,
  879. typeid(Option));
  880. LibDhcpTest::testStdOptionDefs6(D6O_SIP_SERVERS_DNS, fqdn_buf.begin(),
  881. fqdn_buf.end(),
  882. typeid(OptionCustom));
  883. LibDhcpTest::testStdOptionDefs6(D6O_SIP_SERVERS_ADDR, begin, end,
  884. typeid(Option6AddrLst));
  885. LibDhcpTest::testStdOptionDefs6(D6O_NAME_SERVERS, begin, end,
  886. typeid(Option6AddrLst));
  887. LibDhcpTest::testStdOptionDefs6(D6O_DOMAIN_SEARCH, fqdn_buf.begin(),
  888. fqdn_buf.end(), typeid(OptionCustom));
  889. LibDhcpTest::testStdOptionDefs6(D6O_IA_PD, begin, end,
  890. typeid(Option6IA));
  891. LibDhcpTest::testStdOptionDefs6(D6O_IAPREFIX, begin, begin + 25,
  892. typeid(Option6IAPrefix));
  893. LibDhcpTest::testStdOptionDefs6(D6O_NIS_SERVERS, begin, end,
  894. typeid(Option6AddrLst));
  895. LibDhcpTest::testStdOptionDefs6(D6O_NISP_SERVERS, begin, end,
  896. typeid(Option6AddrLst));
  897. LibDhcpTest::testStdOptionDefs6(D6O_NIS_DOMAIN_NAME, fqdn_buf.begin(),
  898. fqdn_buf.end(),
  899. typeid(OptionCustom));
  900. LibDhcpTest::testStdOptionDefs6(D6O_NISP_DOMAIN_NAME, fqdn_buf.begin(),
  901. fqdn_buf.end(),
  902. typeid(OptionCustom));
  903. LibDhcpTest::testStdOptionDefs6(D6O_SNTP_SERVERS, begin, end,
  904. typeid(Option6AddrLst));
  905. LibDhcpTest::testStdOptionDefs6(D6O_INFORMATION_REFRESH_TIME,
  906. begin, begin + 4,
  907. typeid(OptionInt<uint32_t>));
  908. LibDhcpTest::testStdOptionDefs6(D6O_BCMCS_SERVER_D, fqdn_buf.begin(),
  909. fqdn_buf.end(),
  910. typeid(OptionCustom));
  911. LibDhcpTest::testStdOptionDefs6(D6O_BCMCS_SERVER_A, begin, end,
  912. typeid(Option6AddrLst));
  913. LibDhcpTest::testStdOptionDefs6(D6O_GEOCONF_CIVIC, begin, end,
  914. typeid(OptionCustom));
  915. LibDhcpTest::testStdOptionDefs6(D6O_REMOTE_ID, begin, end,
  916. typeid(OptionCustom));
  917. LibDhcpTest::testStdOptionDefs6(D6O_SUBSCRIBER_ID, begin, end,
  918. typeid(Option));
  919. LibDhcpTest::testStdOptionDefs6(D6O_CLIENT_FQDN, client_fqdn_buf.begin(),
  920. client_fqdn_buf.end(),
  921. typeid(Option6ClientFqdn));
  922. LibDhcpTest::testStdOptionDefs6(D6O_PANA_AGENT, begin, end,
  923. typeid(Option6AddrLst));
  924. LibDhcpTest::testStdOptionDefs6(D6O_PANA_AGENT, begin, end,
  925. typeid(Option6AddrLst));
  926. LibDhcpTest::testStdOptionDefs6(D6O_NEW_POSIX_TIMEZONE, begin, end,
  927. typeid(OptionString));
  928. LibDhcpTest::testStdOptionDefs6(D6O_NEW_TZDB_TIMEZONE, begin, end,
  929. typeid(OptionString));
  930. LibDhcpTest::testStdOptionDefs6(D6O_ERO, begin, end,
  931. typeid(OptionIntArray<uint16_t>));
  932. LibDhcpTest::testStdOptionDefs6(D6O_LQ_QUERY, begin, end,
  933. typeid(OptionCustom));
  934. LibDhcpTest::testStdOptionDefs6(D6O_CLIENT_DATA, begin, end,
  935. typeid(Option));
  936. LibDhcpTest::testStdOptionDefs6(D6O_CLT_TIME, begin, begin + 4,
  937. typeid(OptionInt<uint32_t>));
  938. LibDhcpTest::testStdOptionDefs6(D6O_LQ_RELAY_DATA, begin, end,
  939. typeid(OptionCustom));
  940. LibDhcpTest::testStdOptionDefs6(D6O_LQ_CLIENT_LINK, begin, end,
  941. typeid(Option6AddrLst));
  942. LibDhcpTest::testStdOptionDefs6(D6O_RSOO, begin, end,
  943. typeid(OptionCustom),
  944. "rsoo-opts");
  945. LibDhcpTest::testStdOptionDefs6(D6O_ERP_LOCAL_DOMAIN_NAME,
  946. fqdn_buf.begin(), fqdn_buf.end(),
  947. typeid(OptionCustom));
  948. }
  949. // This test checks if the DHCPv6 option definition can be searched by
  950. // an option name.
  951. TEST_F(LibDhcpTest, getOptionDefByName6) {
  952. // Get all definitions.
  953. const OptionDefContainer& defs = LibDHCP::getOptionDefs(Option::V6);
  954. // For each definition try to find it using option name.
  955. for (OptionDefContainer::const_iterator def = defs.begin();
  956. def != defs.end(); ++def) {
  957. OptionDefinitionPtr def_by_name =
  958. LibDHCP::getOptionDef(Option::V6, (*def)->getName());
  959. ASSERT_TRUE(def_by_name);
  960. ASSERT_TRUE(**def == *def_by_name);
  961. }
  962. }
  963. // This test checks if the DHCPv4 option definition can be searched by
  964. // an option name.
  965. TEST_F(LibDhcpTest, getOptionDefByName4) {
  966. // Get all definitions.
  967. const OptionDefContainer& defs = LibDHCP::getOptionDefs(Option::V4);
  968. // For each definition try to find it using option name.
  969. for (OptionDefContainer::const_iterator def = defs.begin();
  970. def != defs.end(); ++def) {
  971. OptionDefinitionPtr def_by_name =
  972. LibDHCP::getOptionDef(Option::V4, (*def)->getName());
  973. ASSERT_TRUE(def_by_name);
  974. ASSERT_TRUE(**def == *def_by_name);
  975. }
  976. }
  977. // This test checks if the definition of the DHCPv6 vendor option can
  978. // be searched by option name.
  979. TEST_F(LibDhcpTest, getVendorOptionDefByName6) {
  980. const OptionDefContainer* defs =
  981. LibDHCP::getVendorOption6Defs(VENDOR_ID_CABLE_LABS);
  982. ASSERT_TRUE(defs != NULL);
  983. for (OptionDefContainer::const_iterator def = defs->begin();
  984. def != defs->end(); ++def) {
  985. OptionDefinitionPtr def_by_name =
  986. LibDHCP::getVendorOptionDef(Option::V6, VENDOR_ID_CABLE_LABS,
  987. (*def)->getName());
  988. ASSERT_TRUE(def_by_name);
  989. ASSERT_TRUE(**def == *def_by_name);
  990. }
  991. }
  992. // This test checks if the definition of the DHCPv4 vendor option can
  993. // be searched by option name.
  994. TEST_F(LibDhcpTest, getVendorOptionDefByName4) {
  995. const OptionDefContainer* defs =
  996. LibDHCP::getVendorOption4Defs(VENDOR_ID_CABLE_LABS);
  997. ASSERT_TRUE(defs != NULL);
  998. for (OptionDefContainer::const_iterator def = defs->begin();
  999. def != defs->end(); ++def) {
  1000. OptionDefinitionPtr def_by_name =
  1001. LibDHCP::getVendorOptionDef(Option::V4, VENDOR_ID_CABLE_LABS,
  1002. (*def)->getName());
  1003. ASSERT_TRUE(def_by_name);
  1004. ASSERT_TRUE(**def == *def_by_name);
  1005. }
  1006. }
  1007. // tests whether v6 vendor-class option can be parsed properly.
  1008. TEST_F(LibDhcpTest, vendorClass6) {
  1009. isc::dhcp::OptionCollection options; // Will store parsed option here
  1010. // Exported from wireshark: vendor-class option with enterprise-id = 4491
  1011. // and a single data entry containing "eRouter1.0"
  1012. string vendor_class_hex = "001000100000118b000a65526f75746572312e30";
  1013. OptionBuffer bin;
  1014. // Decode the hex string and store it in bin (which happens
  1015. // to be OptionBuffer format)
  1016. isc::util::encode::decodeHex(vendor_class_hex, bin);
  1017. ASSERT_NO_THROW ({
  1018. LibDHCP::unpackOptions6(bin, "dhcp6", options);
  1019. });
  1020. EXPECT_EQ(options.size(), 1); // There should be 1 option.
  1021. // Option vendor-class should be there
  1022. ASSERT_FALSE(options.find(D6O_VENDOR_CLASS) == options.end());
  1023. // It should be of type OptionVendorClass
  1024. boost::shared_ptr<OptionVendorClass> vclass =
  1025. boost::dynamic_pointer_cast<OptionVendorClass>(options.begin()->second);
  1026. ASSERT_TRUE(vclass);
  1027. // Let's investigate if the option content is correct
  1028. // 3 fields expected: vendor-id, data-len and data
  1029. EXPECT_EQ(4491, vclass->getVendorId());
  1030. EXPECT_EQ(20, vclass->len());
  1031. ASSERT_EQ(1, vclass->getTuplesNum());
  1032. EXPECT_EQ("eRouter1.0", vclass->getTuple(0).getText());
  1033. }
  1034. } // end of anonymous space