libdhcp++_unittest.cc 55 KB

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