libdhcp++_unittest.cc 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253
  1. // Copyright (C) 2011-2014 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. // The CLIENT_FQDN holds a uint8_t value and FQDN. We have
  821. // to add the uint8_t value to it and then append the buffer
  822. // holding some valid FQDN.
  823. std::vector<uint8_t> client_fqdn_buf(1);
  824. client_fqdn_buf.insert(client_fqdn_buf.end(), fqdn_buf.begin(),
  825. fqdn_buf.end());
  826. // Initialize test buffer for Vendor Class option.
  827. const char vclass_data[] = {
  828. 0x00, 0x01, 0x02, 0x03,
  829. 0x00, 0x01, 0x02
  830. };
  831. std::vector<uint8_t> vclass_buf(vclass_data,
  832. vclass_data + sizeof(vclass_data));;
  833. // The actual test starts here for all supported option codes.
  834. LibDhcpTest::testStdOptionDefs6(D6O_CLIENTID, begin, end,
  835. typeid(Option));
  836. LibDhcpTest::testStdOptionDefs6(D6O_SERVERID, begin, end,
  837. typeid(Option));
  838. LibDhcpTest::testStdOptionDefs6(D6O_IA_NA, begin, end,
  839. typeid(Option6IA));
  840. LibDhcpTest::testStdOptionDefs6(D6O_IA_TA, begin, begin + 4,
  841. typeid(OptionInt<uint32_t>));
  842. LibDhcpTest::testStdOptionDefs6(D6O_IAADDR, begin, end,
  843. typeid(Option6IAAddr));
  844. LibDhcpTest::testStdOptionDefs6(D6O_ORO, begin, end,
  845. typeid(OptionIntArray<uint16_t>));
  846. LibDhcpTest::testStdOptionDefs6(D6O_PREFERENCE, begin, begin + 1,
  847. typeid(OptionInt<uint8_t>));
  848. LibDhcpTest::testStdOptionDefs6(D6O_ELAPSED_TIME, begin, begin + 2,
  849. typeid(OptionInt<uint16_t>));
  850. LibDhcpTest::testStdOptionDefs6(D6O_RELAY_MSG, begin, end,
  851. typeid(Option));
  852. LibDhcpTest::testStdOptionDefs6(D6O_STATUS_CODE, begin, end,
  853. typeid(OptionCustom));
  854. LibDhcpTest::testStdOptionDefs6(D6O_RAPID_COMMIT, begin, end,
  855. typeid(Option));
  856. LibDhcpTest::testStdOptionDefs6(D6O_USER_CLASS, begin, end,
  857. typeid(Option));
  858. LibDhcpTest::testStdOptionDefs6(D6O_VENDOR_CLASS, vclass_buf.begin(),
  859. vclass_buf.end(),
  860. typeid(OptionVendorClass));
  861. LibDhcpTest::testStdOptionDefs6(D6O_VENDOR_OPTS, begin, end,
  862. typeid(OptionVendor),
  863. "vendor-opts-space");
  864. LibDhcpTest::testStdOptionDefs6(D6O_INTERFACE_ID, begin, end,
  865. typeid(Option));
  866. LibDhcpTest::testStdOptionDefs6(D6O_RECONF_MSG, begin, begin + 1,
  867. typeid(OptionInt<uint8_t>));
  868. LibDhcpTest::testStdOptionDefs6(D6O_RECONF_ACCEPT, begin, end,
  869. typeid(Option));
  870. LibDhcpTest::testStdOptionDefs6(D6O_SIP_SERVERS_DNS, fqdn_buf.begin(),
  871. fqdn_buf.end(),
  872. typeid(OptionCustom));
  873. LibDhcpTest::testStdOptionDefs6(D6O_SIP_SERVERS_ADDR, begin, end,
  874. typeid(Option6AddrLst));
  875. LibDhcpTest::testStdOptionDefs6(D6O_NAME_SERVERS, begin, end,
  876. typeid(Option6AddrLst));
  877. LibDhcpTest::testStdOptionDefs6(D6O_DOMAIN_SEARCH, fqdn_buf.begin(),
  878. fqdn_buf.end(), typeid(OptionCustom));
  879. LibDhcpTest::testStdOptionDefs6(D6O_IA_PD, begin, end,
  880. typeid(Option6IA));
  881. LibDhcpTest::testStdOptionDefs6(D6O_IAPREFIX, begin, begin + 25,
  882. typeid(Option6IAPrefix));
  883. LibDhcpTest::testStdOptionDefs6(D6O_NIS_SERVERS, begin, end,
  884. typeid(Option6AddrLst));
  885. LibDhcpTest::testStdOptionDefs6(D6O_NISP_SERVERS, begin, end,
  886. typeid(Option6AddrLst));
  887. LibDhcpTest::testStdOptionDefs6(D6O_NIS_DOMAIN_NAME, fqdn_buf.begin(),
  888. fqdn_buf.end(),
  889. typeid(OptionCustom));
  890. LibDhcpTest::testStdOptionDefs6(D6O_NISP_DOMAIN_NAME, fqdn_buf.begin(),
  891. fqdn_buf.end(),
  892. typeid(OptionCustom));
  893. LibDhcpTest::testStdOptionDefs6(D6O_SNTP_SERVERS, begin, end,
  894. typeid(Option6AddrLst));
  895. LibDhcpTest::testStdOptionDefs6(D6O_INFORMATION_REFRESH_TIME,
  896. begin, begin + 4,
  897. typeid(OptionInt<uint32_t>));
  898. LibDhcpTest::testStdOptionDefs6(D6O_BCMCS_SERVER_D, fqdn_buf.begin(),
  899. fqdn_buf.end(),
  900. typeid(OptionCustom));
  901. LibDhcpTest::testStdOptionDefs6(D6O_BCMCS_SERVER_A, begin, end,
  902. typeid(Option6AddrLst));
  903. LibDhcpTest::testStdOptionDefs6(D6O_GEOCONF_CIVIC, begin, end,
  904. typeid(OptionCustom));
  905. LibDhcpTest::testStdOptionDefs6(D6O_REMOTE_ID, begin, end,
  906. typeid(OptionCustom));
  907. LibDhcpTest::testStdOptionDefs6(D6O_SUBSCRIBER_ID, begin, end,
  908. typeid(Option));
  909. LibDhcpTest::testStdOptionDefs6(D6O_CLIENT_FQDN, client_fqdn_buf.begin(),
  910. client_fqdn_buf.end(),
  911. typeid(Option6ClientFqdn));
  912. LibDhcpTest::testStdOptionDefs6(D6O_PANA_AGENT, begin, end,
  913. typeid(Option6AddrLst));
  914. LibDhcpTest::testStdOptionDefs6(D6O_PANA_AGENT, begin, end,
  915. typeid(Option6AddrLst));
  916. LibDhcpTest::testStdOptionDefs6(D6O_NEW_POSIX_TIMEZONE, begin, end,
  917. typeid(OptionString));
  918. LibDhcpTest::testStdOptionDefs6(D6O_NEW_TZDB_TIMEZONE, begin, end,
  919. typeid(OptionString));
  920. LibDhcpTest::testStdOptionDefs6(D6O_ERO, begin, end,
  921. typeid(OptionIntArray<uint16_t>));
  922. LibDhcpTest::testStdOptionDefs6(D6O_LQ_QUERY, begin, end,
  923. typeid(OptionCustom));
  924. LibDhcpTest::testStdOptionDefs6(D6O_CLIENT_DATA, begin, end,
  925. typeid(Option));
  926. LibDhcpTest::testStdOptionDefs6(D6O_CLT_TIME, begin, begin + 4,
  927. typeid(OptionInt<uint32_t>));
  928. LibDhcpTest::testStdOptionDefs6(D6O_LQ_RELAY_DATA, begin, end,
  929. typeid(OptionCustom));
  930. LibDhcpTest::testStdOptionDefs6(D6O_LQ_CLIENT_LINK, begin, end,
  931. typeid(Option6AddrLst));
  932. LibDhcpTest::testStdOptionDefs6(D6O_RSOO, begin, end,
  933. typeid(OptionCustom),
  934. "rsoo-opts");
  935. LibDhcpTest::testStdOptionDefs6(D6O_ERP_LOCAL_DOMAIN_NAME,
  936. fqdn_buf.begin(), fqdn_buf.end(),
  937. typeid(OptionCustom));
  938. }
  939. // This test checks if the DHCPv6 option definition can be searched by
  940. // an option name.
  941. TEST_F(LibDhcpTest, getOptionDefByName6) {
  942. // Get all definitions.
  943. const OptionDefContainer& defs = LibDHCP::getOptionDefs(Option::V6);
  944. // For each definition try to find it using option name.
  945. for (OptionDefContainer::const_iterator def = defs.begin();
  946. def != defs.end(); ++def) {
  947. OptionDefinitionPtr def_by_name =
  948. LibDHCP::getOptionDef(Option::V6, (*def)->getName());
  949. ASSERT_TRUE(def_by_name);
  950. ASSERT_TRUE(**def == *def_by_name);
  951. }
  952. }
  953. // This test checks if the DHCPv4 option definition can be searched by
  954. // an option name.
  955. TEST_F(LibDhcpTest, getOptionDefByName4) {
  956. // Get all definitions.
  957. const OptionDefContainer& defs = LibDHCP::getOptionDefs(Option::V4);
  958. // For each definition try to find it using option name.
  959. for (OptionDefContainer::const_iterator def = defs.begin();
  960. def != defs.end(); ++def) {
  961. OptionDefinitionPtr def_by_name =
  962. LibDHCP::getOptionDef(Option::V4, (*def)->getName());
  963. ASSERT_TRUE(def_by_name);
  964. ASSERT_TRUE(**def == *def_by_name);
  965. }
  966. }
  967. // This test checks if the definition of the DHCPv6 vendor option can
  968. // be searched by option name.
  969. TEST_F(LibDhcpTest, getVendorOptionDefByName6) {
  970. const OptionDefContainer* defs =
  971. LibDHCP::getVendorOption6Defs(VENDOR_ID_CABLE_LABS);
  972. ASSERT_TRUE(defs != NULL);
  973. for (OptionDefContainer::const_iterator def = defs->begin();
  974. def != defs->end(); ++def) {
  975. OptionDefinitionPtr def_by_name =
  976. LibDHCP::getVendorOptionDef(Option::V6, VENDOR_ID_CABLE_LABS,
  977. (*def)->getName());
  978. ASSERT_TRUE(def_by_name);
  979. ASSERT_TRUE(**def == *def_by_name);
  980. }
  981. }
  982. // This test checks if the definition of the DHCPv4 vendor option can
  983. // be searched by option name.
  984. TEST_F(LibDhcpTest, getVendorOptionDefByName4) {
  985. const OptionDefContainer* defs =
  986. LibDHCP::getVendorOption4Defs(VENDOR_ID_CABLE_LABS);
  987. ASSERT_TRUE(defs != NULL);
  988. for (OptionDefContainer::const_iterator def = defs->begin();
  989. def != defs->end(); ++def) {
  990. OptionDefinitionPtr def_by_name =
  991. LibDHCP::getVendorOptionDef(Option::V4, VENDOR_ID_CABLE_LABS,
  992. (*def)->getName());
  993. ASSERT_TRUE(def_by_name);
  994. ASSERT_TRUE(**def == *def_by_name);
  995. }
  996. }
  997. // tests whether v6 vendor-class option can be parsed properly.
  998. TEST_F(LibDhcpTest, vendorClass6) {
  999. isc::dhcp::OptionCollection options; // Will store parsed option here
  1000. // Exported from wireshark: vendor-class option with enterprise-id = 4491
  1001. // and a single data entry containing "eRouter1.0"
  1002. string vendor_class_hex = "001000100000118b000a65526f75746572312e30";
  1003. OptionBuffer bin;
  1004. // Decode the hex string and store it in bin (which happens
  1005. // to be OptionBuffer format)
  1006. isc::util::encode::decodeHex(vendor_class_hex, bin);
  1007. ASSERT_NO_THROW ({
  1008. LibDHCP::unpackOptions6(bin, "dhcp6", options);
  1009. });
  1010. EXPECT_EQ(options.size(), 1); // There should be 1 option.
  1011. // Option vendor-class should be there
  1012. ASSERT_FALSE(options.find(D6O_VENDOR_CLASS) == options.end());
  1013. // It should be of type OptionVendorClass
  1014. boost::shared_ptr<OptionVendorClass> vclass =
  1015. boost::dynamic_pointer_cast<OptionVendorClass>(options.begin()->second);
  1016. ASSERT_TRUE(vclass);
  1017. // Let's investigate if the option content is correct
  1018. // 3 fields expected: vendor-id, data-len and data
  1019. EXPECT_EQ(4491, vclass->getVendorId());
  1020. EXPECT_EQ(20, vclass->len());
  1021. ASSERT_EQ(1, vclass->getTuplesNum());
  1022. EXPECT_EQ("eRouter1.0", vclass->getTuple(0).getText());
  1023. }
  1024. } // end of anonymous space