pkt6_unittest.cc 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501
  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 <asiolink/io_address.h>
  16. #include <dhcp/dhcp6.h>
  17. #include <dhcp/option.h>
  18. #include <dhcp/option_custom.h>
  19. #include <dhcp/option6_ia.h>
  20. #include <dhcp/option_int.h>
  21. #include <dhcp/option_int_array.h>
  22. #include <dhcp/option_vendor.h>
  23. #include <dhcp/iface_mgr.h>
  24. #include <dhcp/pkt6.h>
  25. #include <dhcp/hwaddr.h>
  26. #include <dhcp/docsis3_option_defs.h>
  27. #include <dhcp/tests/pkt_captures.h>
  28. #include <util/range_utilities.h>
  29. #include <boost/bind.hpp>
  30. #include <boost/date_time/posix_time/posix_time.hpp>
  31. #include <boost/scoped_ptr.hpp>
  32. #include <util/encode/hex.h>
  33. #include <gtest/gtest.h>
  34. #include <iostream>
  35. #include <sstream>
  36. #include <arpa/inet.h>
  37. using namespace std;
  38. using namespace isc;
  39. using namespace isc::asiolink;
  40. using namespace isc::dhcp;
  41. using boost::scoped_ptr;
  42. namespace {
  43. /// @brief A class which contains a custom callback function to unpack options.
  44. ///
  45. /// This is a class used by the tests which verify that the custom callback
  46. /// functions can be installed to unpack options from a message. When the
  47. /// callback function is called, the executed_ member is set to true to allow
  48. /// verification that the callback was really called. Internally, this class
  49. /// uses libdhcp++ to unpack options so the options parsing algorithm remains
  50. /// unchanged after installation of the callback.
  51. class CustomUnpackCallback {
  52. public:
  53. /// @brief Constructor
  54. ///
  55. /// Marks that callback hasn't been called.
  56. CustomUnpackCallback()
  57. : executed_(false) {
  58. }
  59. /// @brief A callback
  60. ///
  61. /// Contains custom implementation of the callback.
  62. ///
  63. /// @param buf a A buffer holding options in on-wire format.
  64. /// @param option_space A name of the option space encapsulated by the
  65. /// option being parsed.
  66. /// @param [out] options A reference to the collection where parsed options
  67. /// will be stored.
  68. /// @param relay_msg_offset Reference to a size_t structure. If specified,
  69. /// offset to beginning of relay_msg option will be stored in it.
  70. /// @param relay_msg_len reference to a size_t structure. If specified,
  71. /// length of the relay_msg option will be stored in it.
  72. /// @return An offset to the first byte after last parsed option.
  73. size_t execute(const OptionBuffer& buf,
  74. const std::string& option_space,
  75. isc::dhcp::OptionCollection& options,
  76. size_t* relay_msg_offset,
  77. size_t* relay_msg_len) {
  78. // Set the executed_ member to true to allow verification that the
  79. // callback has been actually called.
  80. executed_ = true;
  81. // Use default implementation of the unpack algorithm to parse options.
  82. return (LibDHCP::unpackOptions6(buf, option_space, options,
  83. relay_msg_offset, relay_msg_len));
  84. }
  85. /// A flag which indicates if callback function has been called.
  86. bool executed_;
  87. };
  88. class Pkt6Test : public ::testing::Test {
  89. public:
  90. Pkt6Test() {
  91. }
  92. /// @brief generates an option with given code (and length) and
  93. /// random content
  94. ///
  95. /// @param code option code
  96. /// @param len data length (data will be randomized)
  97. ///
  98. /// @return pointer to the new option
  99. OptionPtr generateRandomOption(uint16_t code, size_t len = 10) {
  100. OptionBuffer data(len);
  101. util::fillRandom(data.begin(), data.end());
  102. return OptionPtr(new Option(Option::V6, code, data));
  103. }
  104. /// @brief Create a wire representation of the test packet and clone it.
  105. ///
  106. /// The purpose of this function is to create a packet to be used to
  107. /// check that packet parsing works correctly. The unpack() function
  108. /// requires that the data_ field of the object holds the data to be
  109. /// parsed. This function creates an on-wire representation of the
  110. /// packet by calling pack(). But, the pack() function stores the
  111. /// on-wire representation into the output buffer (not the data_ field).
  112. /// For this reason, it is not enough to return the packet on which
  113. /// pack() is called. This function returns a clone of this packet
  114. /// which is created using a constructor taking a buffer and buffer
  115. /// length as an input. This constructor is normally used to parse
  116. /// received packets. It stores the packet in a data_ field and
  117. /// therefore unpack() can be called to parse it.
  118. ///
  119. /// @param parent Packet from which the new packet should be created.
  120. Pkt6Ptr packAndClone(Pkt6Ptr& parent) {
  121. OptionPtr opt1(new Option(Option::V6, 1));
  122. OptionPtr opt2(new Option(Option::V6, 2));
  123. OptionPtr opt3(new Option(Option::V6, 100));
  124. // Let's not use zero-length option type 3 as it is IA_NA
  125. parent->addOption(opt1);
  126. parent->addOption(opt2);
  127. parent->addOption(opt3);
  128. EXPECT_NO_THROW(parent->pack());
  129. // Create second packet,based on assembled data from the first one
  130. Pkt6Ptr clone(new Pkt6(static_cast<const uint8_t*>
  131. (parent->getBuffer().getData()),
  132. parent->getBuffer().getLength()));
  133. return (clone);
  134. }
  135. };
  136. TEST_F(Pkt6Test, constructor) {
  137. uint8_t data[] = { 0, 1, 2, 3, 4, 5 };
  138. scoped_ptr<Pkt6> pkt1(new Pkt6(data, sizeof(data)));
  139. EXPECT_EQ(6, pkt1->data_.size());
  140. EXPECT_EQ(0, memcmp( &pkt1->data_[0], data, sizeof(data)));
  141. }
  142. /// @brief returns captured actual SOLICIT packet
  143. ///
  144. /// Captured SOLICIT packet with transid=0x3d79fb and options: client-id,
  145. /// in_na, dns-server, elapsed-time, option-request
  146. /// This code was autogenerated (see src/bin/dhcp6/tests/iface_mgr_unittest.c),
  147. /// but we spent some time to make is less ugly than it used to be.
  148. ///
  149. /// @return pointer to Pkt6 that represents received SOLICIT
  150. Pkt6* capture1() {
  151. Pkt6* pkt;
  152. uint8_t data[98];
  153. data[0] = 1;
  154. data[1] = 1; data[2] = 2; data[3] = 3; data[4] = 0;
  155. data[5] = 1; data[6] = 0; data[7] = 14; data[8] = 0;
  156. data[9] = 1; data[10] = 0; data[11] = 1; data[12] = 21;
  157. data[13] = 158; data[14] = 60; data[15] = 22; data[16] = 0;
  158. data[17] = 30; data[18] = 140; data[19] = 155; data[20] = 115;
  159. data[21] = 73; data[22] = 0; data[23] = 3; data[24] = 0;
  160. data[25] = 40; data[26] = 0; data[27] = 0; data[28] = 0;
  161. data[29] = 1; data[30] = 255; data[31] = 255; data[32] = 255;
  162. data[33] = 255; data[34] = 255; data[35] = 255; data[36] = 255;
  163. data[37] = 255; data[38] = 0; data[39] = 5; data[40] = 0;
  164. data[41] = 24; data[42] = 32; data[43] = 1; data[44] = 13;
  165. data[45] = 184; data[46] = 0; data[47] = 1; data[48] = 0;
  166. data[49] = 0; data[50] = 0; data[51] = 0; data[52] = 0;
  167. data[53] = 0; data[54] = 0; data[55] = 0; data[56] = 18;
  168. data[57] = 52; data[58] = 255; data[59] = 255; data[60] = 255;
  169. data[61] = 255; data[62] = 255; data[63] = 255; data[64] = 255;
  170. data[65] = 255; data[66] = 0; data[67] = 23; data[68] = 0;
  171. data[69] = 16; data[70] = 32; data[71] = 1; data[72] = 13;
  172. data[73] = 184; data[74] = 0; data[75] = 1; data[76] = 0;
  173. data[77] = 0; data[78] = 0; data[79] = 0; data[80] = 0;
  174. data[81] = 0; data[82] = 0; data[83] = 0; data[84] = 221;
  175. data[85] = 221; data[86] = 0; data[87] = 8; data[88] = 0;
  176. data[89] = 2; data[90] = 0; data[91] = 100; data[92] = 0;
  177. data[93] = 6; data[94] = 0; data[95] = 2; data[96] = 0;
  178. data[97] = 23;
  179. pkt = new Pkt6(data, sizeof(data));
  180. pkt->setRemotePort(546);
  181. pkt->setRemoteAddr(IOAddress("fe80::21e:8cff:fe9b:7349"));
  182. pkt->setLocalPort(0);
  183. pkt->setLocalAddr(IOAddress("ff02::1:2"));
  184. pkt->setIndex(2);
  185. pkt->setIface("eth0");
  186. return (pkt);
  187. }
  188. /// @brief creates doubly relayed solicit message
  189. ///
  190. /// This is a traffic capture exported from wireshark. It includes a SOLICIT
  191. /// message that passed through two relays. Each relay include interface-id,
  192. /// remote-id and relay-forw encapsulation. It is especially interesting,
  193. /// because of the following properties:
  194. /// - double encapsulation
  195. /// - first relay inserts relay-msg before extra options
  196. /// - second relay inserts relay-msg after extra options
  197. /// - both relays are from different vendors
  198. /// - interface-id are different for each relay
  199. /// - first relay inserts valid remote-id
  200. /// - second relay inserts remote-id with empty vendor data
  201. /// - the solicit message requests for custom options in ORO
  202. /// - there are option types in RELAY-FORW that do not appear in SOLICIT
  203. /// - there are option types in SOLICT that do not appear in RELAY-FORW
  204. ///
  205. /// RELAY-FORW
  206. /// - relay message option
  207. /// - RELAY-FORW
  208. /// - interface-id option
  209. /// - remote-id option
  210. /// - RELAY-FORW
  211. /// SOLICIT
  212. /// - client-id option
  213. /// - ia_na option
  214. /// - elapsed time
  215. /// - ORO
  216. /// - interface-id option
  217. /// - remote-id option
  218. ///
  219. /// The original capture was posted to dibbler users mailing list.
  220. ///
  221. /// @return created double relayed SOLICIT message
  222. Pkt6* capture2() {
  223. // string exported from Wireshark
  224. string hex_string =
  225. "0c01200108880db800010000000000000000fe80000000000000020021fffe5c"
  226. "18a90009007d0c0000000000000000000000000000000000fe80000000000000"
  227. "020021fffe5c18a9001200154953414d3134342065746820312f312f30352f30"
  228. "310025000400000de900090036016b4fe20001000e0001000118b03341000021"
  229. "5c18a90003000c00000001ffffffffffffffff00080002000000060006001700"
  230. "f200f30012001c4953414d3134347c3239397c697076367c6e743a76703a313a"
  231. "313130002500120000197f0001000118b033410000215c18a9";
  232. std::vector<uint8_t> bin;
  233. // Decode the hex string and store it in bin (which happens
  234. // to be OptionBuffer format)
  235. isc::util::encode::decodeHex(hex_string, bin);
  236. Pkt6* pkt = new Pkt6(&bin[0], bin.size());
  237. pkt->setRemotePort(547);
  238. pkt->setRemoteAddr(IOAddress("fe80::1234"));
  239. pkt->setLocalPort(547);
  240. pkt->setLocalAddr(IOAddress("ff05::1:3"));
  241. pkt->setIndex(2);
  242. pkt->setIface("eth0");
  243. return (pkt);
  244. }
  245. TEST_F(Pkt6Test, unpack_solicit1) {
  246. scoped_ptr<Pkt6> sol(capture1());
  247. ASSERT_NO_THROW(sol->unpack());
  248. // Check for length
  249. EXPECT_EQ(98, sol->len() );
  250. // Check for type
  251. EXPECT_EQ(DHCPV6_SOLICIT, sol->getType() );
  252. // Check that all present options are returned
  253. EXPECT_TRUE(sol->getOption(D6O_CLIENTID)); // client-id is present
  254. EXPECT_TRUE(sol->getOption(D6O_IA_NA)); // IA_NA is present
  255. EXPECT_TRUE(sol->getOption(D6O_ELAPSED_TIME)); // elapsed is present
  256. EXPECT_TRUE(sol->getOption(D6O_NAME_SERVERS));
  257. EXPECT_TRUE(sol->getOption(D6O_ORO));
  258. // Let's check that non-present options are not returned
  259. EXPECT_FALSE(sol->getOption(D6O_SERVERID)); // server-id is missing
  260. EXPECT_FALSE(sol->getOption(D6O_IA_TA));
  261. EXPECT_FALSE(sol->getOption(D6O_IAADDR));
  262. }
  263. TEST_F(Pkt6Test, packUnpack) {
  264. // Create an on-wire representation of the test packet and clone it.
  265. Pkt6Ptr pkt(new Pkt6(DHCPV6_SOLICIT, 0x020304));
  266. Pkt6Ptr clone = packAndClone(pkt);
  267. // Now recreate options list
  268. ASSERT_NO_THROW(clone->unpack());
  269. // transid, message-type should be the same as before
  270. EXPECT_EQ(0x020304, clone->getTransid());
  271. EXPECT_EQ(DHCPV6_SOLICIT, clone->getType());
  272. EXPECT_TRUE(clone->getOption(1));
  273. EXPECT_TRUE(clone->getOption(2));
  274. EXPECT_TRUE(clone->getOption(100));
  275. EXPECT_FALSE(clone->getOption(4));
  276. }
  277. // Checks if the code is able to handle malformed packet
  278. TEST_F(Pkt6Test, unpackMalformed) {
  279. // Get a packet. We're really interested in its on-wire
  280. // representation only.
  281. scoped_ptr<Pkt6> donor(capture1());
  282. // That's our original content. It should be sane.
  283. OptionBuffer orig = donor->data_;
  284. Pkt6Ptr success(new Pkt6(&orig[0], orig.size()));
  285. EXPECT_NO_THROW(success->unpack());
  286. // Insert trailing garbage.
  287. OptionBuffer malform1 = orig;
  288. malform1.push_back(123);
  289. // Let's check a truncated packet. Moderately sane DHCPv6 packet should at
  290. // least have four bytes header. Zero bytes is definitely not a valid one.
  291. OptionBuffer empty(1); // Let's allocate one byte, so we won't be
  292. // dereferencing an empty buffer.
  293. Pkt6Ptr empty_pkt(new Pkt6(&empty[0], 0));
  294. EXPECT_THROW(empty_pkt->unpack(), isc::BadValue);
  295. // Neither is 3 bytes long.
  296. OptionBuffer shorty;
  297. shorty.push_back(DHCPV6_SOLICIT);
  298. shorty.push_back(1);
  299. shorty.push_back(2);
  300. Pkt6Ptr too_short_pkt(new Pkt6(&shorty[0], shorty.size()));
  301. EXPECT_THROW(too_short_pkt->unpack(), isc::BadValue);
  302. // The code should complain about remaining bytes that can't be parsed
  303. // but doesn't do so yet.
  304. Pkt6Ptr trailing_garbage(new Pkt6(&malform1[0], malform1.size()));
  305. EXPECT_NO_THROW(trailing_garbage->unpack());
  306. // A strict approach would assume the code will reject the whole packet,
  307. // but we decided to follow Jon Postel's law and be silent about
  308. // received malformed or truncated options.
  309. // Add an option that is truncated
  310. OptionBuffer malform2 = orig;
  311. malform2.push_back(0);
  312. malform2.push_back(123); // 0, 123 - option code = 123
  313. malform2.push_back(0);
  314. malform2.push_back(1); // 0, 1 - option length = 1
  315. // Option content would go here, but it's missing
  316. Pkt6Ptr trunc_option(new Pkt6(&malform2[0], malform2.size()));
  317. // The unpack() operation should succeed...
  318. EXPECT_NO_THROW(trunc_option->unpack());
  319. // ... but there should be no option 123 as it was malformed.
  320. EXPECT_FALSE(trunc_option->getOption(123));
  321. // Check with truncated length field
  322. Pkt6Ptr trunc_length(new Pkt6(&malform2[0], malform2.size() - 1));
  323. EXPECT_NO_THROW(trunc_length->unpack());
  324. EXPECT_FALSE(trunc_length->getOption(123));
  325. // Check with missing length field
  326. Pkt6Ptr no_length(new Pkt6(&malform2[0], malform2.size() - 2));
  327. EXPECT_NO_THROW(no_length->unpack());
  328. EXPECT_FALSE(no_length->getOption(123));
  329. // Check with truncated type field
  330. Pkt6Ptr trunc_type(new Pkt6(&malform2[0], malform2.size() - 3));
  331. EXPECT_NO_THROW(trunc_type->unpack());
  332. EXPECT_FALSE(trunc_type->getOption(123));
  333. }
  334. // Checks if the code is able to handle a malformed vendor option
  335. TEST_F(Pkt6Test, unpackVendorMalformed) {
  336. // Get a packet. We're really interested in its on-wire
  337. // representation only.
  338. scoped_ptr<Pkt6> donor(capture1());
  339. // Add a vendor option
  340. OptionBuffer orig = donor->data_;
  341. orig.push_back(0); // vendor options
  342. orig.push_back(17);
  343. orig.push_back(0);
  344. size_t len_index = orig.size();
  345. orig.push_back(18); // length=18
  346. orig.push_back(1); // vendor_id=0x1020304
  347. orig.push_back(2);
  348. orig.push_back(3);
  349. orig.push_back(4);
  350. orig.push_back(1); // suboption type=0x101
  351. orig.push_back(1);
  352. orig.push_back(0); // suboption length=3
  353. orig.push_back(3);
  354. orig.push_back(102); // data="foo"
  355. orig.push_back(111);
  356. orig.push_back(111);
  357. orig.push_back(1); // suboption type=0x102
  358. orig.push_back(2);
  359. orig.push_back(0); // suboption length=3
  360. orig.push_back(3);
  361. orig.push_back(99); // data="bar'
  362. orig.push_back(98);
  363. orig.push_back(114);
  364. Pkt6Ptr success(new Pkt6(&orig[0], orig.size()));
  365. EXPECT_NO_THROW(success->unpack());
  366. // Truncated vendor option is not accepted but doesn't throw
  367. vector<uint8_t> shortv = orig;
  368. shortv[len_index] = 20;
  369. Pkt6Ptr too_short_vendor_pkt(new Pkt6(&shortv[0], shortv.size()));
  370. EXPECT_NO_THROW(too_short_vendor_pkt->unpack());
  371. // Truncated option header is not accepted
  372. vector<uint8_t> shorth = orig;
  373. shorth.resize(orig.size() - 4);
  374. shorth[len_index] = 12;
  375. Pkt6Ptr too_short_header_pkt(new Pkt6(&shorth[0], shorth.size()));
  376. EXPECT_THROW(too_short_header_pkt->unpack(), OutOfRange);
  377. // Truncated option data is not accepted
  378. vector<uint8_t> shorto = orig;
  379. shorto.resize(orig.size() - 2);
  380. shorto[len_index] = 16;
  381. Pkt6Ptr too_short_option_pkt(new Pkt6(&shorto[0], shorto.size()));
  382. EXPECT_THROW(too_short_option_pkt->unpack(), OutOfRange);
  383. }
  384. // This test verifies that it is possible to specify custom implementation of
  385. // the option parsing algorithm by installing a callback function.
  386. TEST_F(Pkt6Test, packUnpackWithCallback) {
  387. // Create an on-wire representation of the test packet and clone it.
  388. Pkt6Ptr pkt(new Pkt6(DHCPV6_SOLICIT, 0x020304));
  389. Pkt6Ptr clone = packAndClone(pkt);
  390. // Install the custom callback function. We expect that this function
  391. // will be called to parse options in the packet instead of
  392. // LibDHCP::unpackOptions6.
  393. CustomUnpackCallback cb;
  394. clone->setCallback(boost::bind(&CustomUnpackCallback::execute, &cb,
  395. _1, _2, _3, _4, _5));
  396. // Make sure that the flag which indicates if the callback function has
  397. // been called is not set. Otherwise, our test doesn't make sense.
  398. ASSERT_FALSE(cb.executed_);
  399. // Now recreate options list
  400. ASSERT_NO_THROW(clone->unpack());
  401. // An object which holds a callback should now have a flag set which
  402. // indicates that callback has been called.
  403. EXPECT_TRUE(cb.executed_);
  404. // transid, message-type should be the same as before
  405. EXPECT_EQ(0x020304, clone->getTransid());
  406. EXPECT_EQ(DHCPV6_SOLICIT, clone->getType());
  407. EXPECT_TRUE(clone->getOption(1));
  408. EXPECT_TRUE(clone->getOption(2));
  409. EXPECT_TRUE(clone->getOption(100));
  410. EXPECT_FALSE(clone->getOption(4));
  411. // Reset the indicator to perform another check: uninstall the callback.
  412. cb.executed_ = false;
  413. // By setting the callback to NULL we effectively uninstall the callback.
  414. clone->setCallback(NULL);
  415. // Do another unpack.
  416. ASSERT_NO_THROW(clone->unpack());
  417. // Callback should not be executed.
  418. EXPECT_FALSE(cb.executed_);
  419. }
  420. // This test verifies that options can be added (addOption()), retrieved
  421. // (getOption(), getOptions()) and deleted (delOption()).
  422. TEST_F(Pkt6Test, addGetDelOptions) {
  423. scoped_ptr<Pkt6> parent(new Pkt6(DHCPV6_SOLICIT, random()));
  424. OptionPtr opt1(new Option(Option::V6, 1));
  425. OptionPtr opt2(new Option(Option::V6, 2));
  426. OptionPtr opt3(new Option(Option::V6, 2));
  427. parent->addOption(opt1);
  428. parent->addOption(opt2);
  429. // getOption() test
  430. EXPECT_EQ(opt1, parent->getOption(1));
  431. EXPECT_EQ(opt2, parent->getOption(2));
  432. // Expect NULL
  433. EXPECT_EQ(OptionPtr(), parent->getOption(4));
  434. // Now there are 2 options of type 2
  435. parent->addOption(opt3);
  436. OptionCollection options = parent->getOptions(2);
  437. EXPECT_EQ(2, options.size()); // there should be 2 instances
  438. // Both options must be of type 2 and there must not be
  439. // any other type returned
  440. for (OptionCollection::const_iterator x= options.begin();
  441. x != options.end(); ++x) {
  442. EXPECT_EQ(2, x->second->getType());
  443. }
  444. // Try to get a single option. Normally for singular options
  445. // it is better to use getOption(), but getOptions() must work
  446. // as well
  447. options = parent->getOptions(1);
  448. ASSERT_EQ(1, options.size());
  449. EXPECT_EQ(1, (*options.begin()).second->getType());
  450. EXPECT_EQ(opt1, options.begin()->second);
  451. // Let's delete one of them
  452. EXPECT_EQ(true, parent->delOption(2));
  453. // There still should be the other option 2
  454. EXPECT_NE(OptionPtr(), parent->getOption(2));
  455. // Let's delete the other option 2
  456. EXPECT_EQ(true, parent->delOption(2));
  457. // No more options with type=2
  458. EXPECT_EQ(OptionPtr(), parent->getOption(2));
  459. // Let's try to delete - should fail
  460. EXPECT_TRUE(false == parent->delOption(2));
  461. // Finally try to get a non-existent option
  462. options = parent->getOptions(1234);
  463. EXPECT_EQ(0, options.size());
  464. }
  465. TEST_F(Pkt6Test, Timestamp) {
  466. boost::scoped_ptr<Pkt6> pkt(new Pkt6(DHCPV6_SOLICIT, 0x020304));
  467. // Just after construction timestamp is invalid
  468. ASSERT_TRUE(pkt->getTimestamp().is_not_a_date_time());
  469. // Update packet time.
  470. pkt->updateTimestamp();
  471. // Get updated packet time.
  472. boost::posix_time::ptime ts_packet = pkt->getTimestamp();
  473. // After timestamp is updated it should be date-time.
  474. ASSERT_FALSE(ts_packet.is_not_a_date_time());
  475. // Check current time.
  476. boost::posix_time::ptime ts_now =
  477. boost::posix_time::microsec_clock::universal_time();
  478. // Calculate period between packet time and now.
  479. boost::posix_time::time_period ts_period(ts_packet, ts_now);
  480. // Duration should be positive or zero.
  481. EXPECT_TRUE(ts_period.length().total_microseconds() >= 0);
  482. }
  483. // This test verifies that getName() method returns proper
  484. // packet type names.
  485. TEST_F(Pkt6Test, getName) {
  486. // Check all possible packet types
  487. for (unsigned itype = 0; itype < 256; ++itype) {
  488. uint8_t type = itype;
  489. switch (type) {
  490. case DHCPV6_ADVERTISE:
  491. EXPECT_STREQ("ADVERTISE", Pkt6::getName(type));
  492. break;
  493. case DHCPV6_CONFIRM:
  494. EXPECT_STREQ("CONFIRM", Pkt6::getName(type));
  495. break;
  496. case DHCPV6_DECLINE:
  497. EXPECT_STREQ("DECLINE", Pkt6::getName(type));
  498. break;
  499. case DHCPV6_INFORMATION_REQUEST:
  500. EXPECT_STREQ("INFORMATION_REQUEST",
  501. Pkt6::getName(type));
  502. break;
  503. case DHCPV6_LEASEQUERY:
  504. EXPECT_STREQ("LEASEQUERY", Pkt6::getName(type));
  505. break;
  506. case DHCPV6_LEASEQUERY_REPLY:
  507. EXPECT_STREQ("LEASEQUERY_REPLY", Pkt6::getName(type));
  508. break;
  509. case DHCPV6_REBIND:
  510. EXPECT_STREQ("REBIND", Pkt6::getName(type));
  511. break;
  512. case DHCPV6_RECONFIGURE:
  513. EXPECT_STREQ("RECONFIGURE", Pkt6::getName(type));
  514. break;
  515. case DHCPV6_RELAY_FORW:
  516. EXPECT_STREQ("RELAY_FORWARD", Pkt6::getName(type));
  517. break;
  518. case DHCPV6_RELAY_REPL:
  519. EXPECT_STREQ("RELAY_REPLY", Pkt6::getName(type));
  520. break;
  521. case DHCPV6_RELEASE:
  522. EXPECT_STREQ("RELEASE", Pkt6::getName(type));
  523. break;
  524. case DHCPV6_RENEW:
  525. EXPECT_STREQ("RENEW", Pkt6::getName(type));
  526. break;
  527. case DHCPV6_REPLY:
  528. EXPECT_STREQ("REPLY", Pkt6::getName(type));
  529. break;
  530. case DHCPV6_REQUEST:
  531. EXPECT_STREQ("REQUEST", Pkt6::getName(type));
  532. break;
  533. case DHCPV6_SOLICIT:
  534. EXPECT_STREQ("SOLICIT", Pkt6::getName(type));
  535. break;
  536. default:
  537. EXPECT_STREQ("UNKNOWN", Pkt6::getName(type));
  538. }
  539. }
  540. }
  541. // This test verifies that a fancy solicit that passed through two
  542. // relays can be parsed properly. See capture2() method description
  543. // for details regarding the packet.
  544. TEST_F(Pkt6Test, relayUnpack) {
  545. boost::scoped_ptr<Pkt6> msg(capture2());
  546. EXPECT_NO_THROW(msg->unpack());
  547. EXPECT_EQ(DHCPV6_SOLICIT, msg->getType());
  548. EXPECT_EQ(217, msg->len());
  549. ASSERT_EQ(2, msg->relay_info_.size());
  550. OptionPtr opt;
  551. // Part 1: Check options inserted by the first relay
  552. // There should be 2 options in first relay
  553. EXPECT_EQ(2, msg->relay_info_[0].options_.size());
  554. // There should be interface-id option
  555. ASSERT_TRUE(opt = msg->getRelayOption(D6O_INTERFACE_ID, 0));
  556. OptionBuffer data = opt->getData();
  557. EXPECT_EQ(32, opt->len()); // 28 bytes of data + 4 bytes header
  558. EXPECT_EQ(data.size(), 28);
  559. // That's a strange interface-id, but this is a real life example
  560. EXPECT_TRUE(0 == memcmp("ISAM144|299|ipv6|nt:vp:1:110", &data[0], 28));
  561. // Get the remote-id option
  562. ASSERT_TRUE(opt = msg->getRelayOption(D6O_REMOTE_ID, 0));
  563. EXPECT_EQ(22, opt->len()); // 18 bytes of data + 4 bytes header
  564. boost::shared_ptr<OptionCustom> custom = boost::dynamic_pointer_cast<OptionCustom>(opt);
  565. uint32_t vendor_id = custom->readInteger<uint32_t>(0);
  566. EXPECT_EQ(6527, vendor_id); // 6527 = Panthera Networks
  567. uint8_t expected_remote_id[] = { 0x00, 0x01, 0x00, 0x01, 0x18, 0xb0,
  568. 0x33, 0x41, 0x00, 0x00, 0x21, 0x5c,
  569. 0x18, 0xa9 };
  570. OptionBuffer remote_id = custom->readBinary(1);
  571. ASSERT_EQ(sizeof(expected_remote_id), remote_id.size());
  572. ASSERT_EQ(0, memcmp(expected_remote_id, &remote_id[0], remote_id.size()));
  573. // Part 2: Check options inserted by the second relay
  574. // Get the interface-id from the second relay
  575. ASSERT_TRUE(opt = msg->getRelayOption(D6O_INTERFACE_ID, 1));
  576. data = opt->getData();
  577. EXPECT_EQ(25, opt->len()); // 21 bytes + 4 bytes header
  578. EXPECT_EQ(data.size(), 21);
  579. EXPECT_TRUE(0 == memcmp("ISAM144 eth 1/1/05/01", &data[0], 21));
  580. // Get the remote-id option
  581. ASSERT_TRUE(opt = msg->getRelayOption(D6O_REMOTE_ID, 1));
  582. EXPECT_EQ(8, opt->len());
  583. custom = boost::dynamic_pointer_cast<OptionCustom>(opt);
  584. vendor_id = custom->readInteger<uint32_t>(0);
  585. EXPECT_EQ(3561, vendor_id); // 3561 = Broadband Forum
  586. // @todo: See if we can validate empty remote-id field
  587. // Let's check if there is no leak between options stored in
  588. // the SOLICIT message and the relay.
  589. EXPECT_FALSE(opt = msg->getRelayOption(D6O_IA_NA, 1));
  590. // Part 3: Let's check options in the message itself
  591. // This is not redundant compared to other direct messages tests,
  592. // as we parsed it differently
  593. EXPECT_EQ(DHCPV6_SOLICIT, msg->getType());
  594. EXPECT_EQ(0x6b4fe2, msg->getTransid());
  595. ASSERT_TRUE(opt = msg->getOption(D6O_CLIENTID));
  596. EXPECT_EQ(18, opt->len()); // 14 bytes of data + 4 bytes of header
  597. uint8_t expected_client_id[] = { 0x00, 0x01, 0x00, 0x01, 0x18, 0xb0,
  598. 0x33, 0x41, 0x00, 0x00, 0x21, 0x5c,
  599. 0x18, 0xa9 };
  600. data = opt->getData();
  601. ASSERT_EQ(data.size(), sizeof(expected_client_id));
  602. ASSERT_EQ(0, memcmp(&data[0], expected_client_id, data.size()));
  603. ASSERT_TRUE(opt = msg->getOption(D6O_IA_NA));
  604. boost::shared_ptr<Option6IA> ia =
  605. boost::dynamic_pointer_cast<Option6IA>(opt);
  606. ASSERT_TRUE(ia);
  607. EXPECT_EQ(1, ia->getIAID());
  608. EXPECT_EQ(0xffffffff, ia->getT1());
  609. EXPECT_EQ(0xffffffff, ia->getT2());
  610. ASSERT_TRUE(opt = msg->getOption(D6O_ELAPSED_TIME));
  611. EXPECT_EQ(6, opt->len()); // 2 bytes of data + 4 bytes of header
  612. boost::shared_ptr<OptionInt<uint16_t> > elapsed =
  613. boost::dynamic_pointer_cast<OptionInt<uint16_t> > (opt);
  614. ASSERT_TRUE(elapsed);
  615. EXPECT_EQ(0, elapsed->getValue());
  616. ASSERT_TRUE(opt = msg->getOption(D6O_ORO));
  617. boost::shared_ptr<OptionIntArray<uint16_t> > oro =
  618. boost::dynamic_pointer_cast<OptionIntArray<uint16_t> > (opt);
  619. const std::vector<uint16_t> oro_list = oro->getValues();
  620. EXPECT_EQ(3, oro_list.size());
  621. EXPECT_EQ(23, oro_list[0]);
  622. EXPECT_EQ(242, oro_list[1]);
  623. EXPECT_EQ(243, oro_list[2]);
  624. }
  625. // This test verified that message with relay information can be
  626. // packed and then unpacked.
  627. TEST_F(Pkt6Test, relayPack) {
  628. scoped_ptr<Pkt6> parent(new Pkt6(DHCPV6_ADVERTISE, 0x020304));
  629. Pkt6::RelayInfo relay1;
  630. relay1.msg_type_ = DHCPV6_RELAY_REPL;
  631. relay1.hop_count_ = 17; // not very miningful, but useful for testing
  632. relay1.linkaddr_ = IOAddress("2001:db8::1");
  633. relay1.peeraddr_ = IOAddress("fe80::abcd");
  634. uint8_t relay_opt_data[] = { 1, 2, 3, 4, 5, 6, 7, 8};
  635. vector<uint8_t> relay_data(relay_opt_data,
  636. relay_opt_data + sizeof(relay_opt_data));
  637. OptionPtr optRelay1(new Option(Option::V6, 200, relay_data));
  638. relay1.options_.insert(make_pair(optRelay1->getType(), optRelay1));
  639. OptionPtr opt1(new Option(Option::V6, 100));
  640. OptionPtr opt2(new Option(Option::V6, 101));
  641. OptionPtr opt3(new Option(Option::V6, 102));
  642. // Let's not use zero-length option type 3 as it is IA_NA
  643. parent->addRelayInfo(relay1);
  644. parent->addOption(opt1);
  645. parent->addOption(opt2);
  646. parent->addOption(opt3);
  647. EXPECT_EQ(DHCPV6_ADVERTISE, parent->getType());
  648. EXPECT_NO_THROW(parent->pack());
  649. EXPECT_EQ(Pkt6::DHCPV6_PKT_HDR_LEN
  650. + 3 * Option::OPTION6_HDR_LEN // ADVERTISE
  651. + Pkt6::DHCPV6_RELAY_HDR_LEN // Relay header
  652. + Option::OPTION6_HDR_LEN // Relay-msg
  653. + optRelay1->len(),
  654. parent->len());
  655. // Create second packet,based on assembled data from the first one
  656. scoped_ptr<Pkt6> clone(new Pkt6(static_cast<const uint8_t*>(
  657. parent->getBuffer().getData()),
  658. parent->getBuffer().getLength()));
  659. // Now recreate options list
  660. EXPECT_NO_THROW( clone->unpack() );
  661. // transid, message-type should be the same as before
  662. EXPECT_EQ(parent->getTransid(), parent->getTransid());
  663. EXPECT_EQ(DHCPV6_ADVERTISE, clone->getType());
  664. EXPECT_TRUE( clone->getOption(100));
  665. EXPECT_TRUE( clone->getOption(101));
  666. EXPECT_TRUE( clone->getOption(102));
  667. EXPECT_FALSE(clone->getOption(103));
  668. // Now check relay info
  669. ASSERT_EQ(1, clone->relay_info_.size());
  670. EXPECT_EQ(DHCPV6_RELAY_REPL, clone->relay_info_[0].msg_type_);
  671. EXPECT_EQ(17, clone->relay_info_[0].hop_count_);
  672. EXPECT_EQ("2001:db8::1", clone->relay_info_[0].linkaddr_.toText());
  673. EXPECT_EQ("fe80::abcd", clone->relay_info_[0].peeraddr_.toText());
  674. // There should be exactly one option
  675. EXPECT_EQ(1, clone->relay_info_[0].options_.size());
  676. OptionPtr opt = clone->getRelayOption(200, 0);
  677. EXPECT_TRUE(opt);
  678. EXPECT_EQ(opt->getType() , optRelay1->getType());
  679. EXPECT_EQ(opt->len(), optRelay1->len());
  680. OptionBuffer data = opt->getData();
  681. ASSERT_EQ(data.size(), sizeof(relay_opt_data));
  682. EXPECT_EQ(0,
  683. memcmp(relay_opt_data, relay_opt_data, sizeof(relay_opt_data)));
  684. }
  685. // This test verified that options added by relays to the message can be
  686. // accessed and retrieved properly
  687. TEST_F(Pkt6Test, getAnyRelayOption) {
  688. boost::scoped_ptr<Pkt6> msg(new Pkt6(DHCPV6_ADVERTISE, 0x020304));
  689. msg->addOption(generateRandomOption(300));
  690. // generate options for relay1
  691. Pkt6::RelayInfo relay1;
  692. // generate 3 options with code 200,201,202 and random content
  693. OptionPtr relay1_opt1(generateRandomOption(200));
  694. OptionPtr relay1_opt2(generateRandomOption(201));
  695. OptionPtr relay1_opt3(generateRandomOption(202));
  696. relay1.options_.insert(make_pair(200, relay1_opt1));
  697. relay1.options_.insert(make_pair(201, relay1_opt2));
  698. relay1.options_.insert(make_pair(202, relay1_opt3));
  699. msg->addRelayInfo(relay1);
  700. // generate options for relay2
  701. Pkt6::RelayInfo relay2;
  702. OptionPtr relay2_opt1(new Option(Option::V6, 100));
  703. OptionPtr relay2_opt2(new Option(Option::V6, 101));
  704. OptionPtr relay2_opt3(new Option(Option::V6, 102));
  705. OptionPtr relay2_opt4(new Option(Option::V6, 200));
  706. // the same code as relay1_opt3
  707. relay2.options_.insert(make_pair(100, relay2_opt1));
  708. relay2.options_.insert(make_pair(101, relay2_opt2));
  709. relay2.options_.insert(make_pair(102, relay2_opt3));
  710. relay2.options_.insert(make_pair(200, relay2_opt4));
  711. msg->addRelayInfo(relay2);
  712. // generate options for relay3
  713. Pkt6::RelayInfo relay3;
  714. OptionPtr relay3_opt1(generateRandomOption(200, 7));
  715. relay3.options_.insert(make_pair(200, relay3_opt1));
  716. msg->addRelayInfo(relay3);
  717. // Ok, so we now have a packet that traversed the following network:
  718. // client---relay3---relay2---relay1---server
  719. // First check that the getAnyRelayOption does not confuse client options
  720. // and relay options
  721. // 300 is a client option, present in the message itself.
  722. OptionPtr opt =
  723. msg->getAnyRelayOption(300, Pkt6::RELAY_SEARCH_FROM_CLIENT);
  724. EXPECT_FALSE(opt);
  725. opt = msg->getAnyRelayOption(300, Pkt6::RELAY_SEARCH_FROM_SERVER);
  726. EXPECT_FALSE(opt);
  727. opt = msg->getAnyRelayOption(300, Pkt6::RELAY_GET_FIRST);
  728. EXPECT_FALSE(opt);
  729. opt = msg->getAnyRelayOption(300, Pkt6::RELAY_GET_LAST);
  730. EXPECT_FALSE(opt);
  731. // Option 200 is added in every relay.
  732. // We want to get that one inserted by relay3 (first match, starting from
  733. // closest to the client.
  734. opt = msg->getAnyRelayOption(200, Pkt6::RELAY_SEARCH_FROM_CLIENT);
  735. ASSERT_TRUE(opt);
  736. EXPECT_TRUE(opt->equals(relay3_opt1));
  737. // We want to ge that one inserted by relay1 (first match, starting from
  738. // closest to the server.
  739. opt = msg->getAnyRelayOption(200, Pkt6::RELAY_SEARCH_FROM_SERVER);
  740. ASSERT_TRUE(opt);
  741. EXPECT_TRUE(opt->equals(relay1_opt1));
  742. // We just want option from the first relay (closest to the client)
  743. opt = msg->getAnyRelayOption(200, Pkt6::RELAY_GET_FIRST);
  744. ASSERT_TRUE(opt);
  745. EXPECT_TRUE(opt->equals(relay3_opt1));
  746. // We just want option from the last relay (closest to the server)
  747. opt = msg->getAnyRelayOption(200, Pkt6::RELAY_GET_LAST);
  748. ASSERT_TRUE(opt);
  749. EXPECT_TRUE(opt->equals(relay1_opt1));
  750. // Let's try to ask for something that is inserted by the middle relay
  751. // only.
  752. opt = msg->getAnyRelayOption(100, Pkt6::RELAY_SEARCH_FROM_SERVER);
  753. ASSERT_TRUE(opt);
  754. EXPECT_TRUE(opt->equals(relay2_opt1));
  755. opt = msg->getAnyRelayOption(100, Pkt6::RELAY_SEARCH_FROM_CLIENT);
  756. ASSERT_TRUE(opt);
  757. EXPECT_TRUE(opt->equals(relay2_opt1));
  758. opt = msg->getAnyRelayOption(100, Pkt6::RELAY_GET_FIRST);
  759. EXPECT_FALSE(opt);
  760. opt = msg->getAnyRelayOption(100, Pkt6::RELAY_GET_LAST);
  761. EXPECT_FALSE(opt);
  762. // Finally, try to get an option that does not exist
  763. opt = msg->getAnyRelayOption(500, Pkt6::RELAY_GET_FIRST);
  764. EXPECT_FALSE(opt);
  765. opt = msg->getAnyRelayOption(500, Pkt6::RELAY_GET_LAST);
  766. EXPECT_FALSE(opt);
  767. opt = msg->getAnyRelayOption(500, Pkt6::RELAY_SEARCH_FROM_SERVER);
  768. EXPECT_FALSE(opt);
  769. opt = msg->getAnyRelayOption(500, Pkt6::RELAY_SEARCH_FROM_CLIENT);
  770. EXPECT_FALSE(opt);
  771. }
  772. // Tests whether a packet can be assigned to a class and later
  773. // checked if it belongs to a given class
  774. TEST_F(Pkt6Test, clientClasses) {
  775. Pkt6 pkt(DHCPV6_ADVERTISE, 1234);
  776. // Default values (do not belong to any class)
  777. EXPECT_FALSE(pkt.inClass(DOCSIS3_CLASS_EROUTER));
  778. EXPECT_FALSE(pkt.inClass(DOCSIS3_CLASS_MODEM));
  779. EXPECT_TRUE(pkt.classes_.empty());
  780. // Add to the first class
  781. pkt.addClass(DOCSIS3_CLASS_EROUTER);
  782. EXPECT_TRUE(pkt.inClass(DOCSIS3_CLASS_EROUTER));
  783. EXPECT_FALSE(pkt.inClass(DOCSIS3_CLASS_MODEM));
  784. ASSERT_FALSE(pkt.classes_.empty());
  785. // Add to a second class
  786. pkt.addClass(DOCSIS3_CLASS_MODEM);
  787. EXPECT_TRUE(pkt.inClass(DOCSIS3_CLASS_EROUTER));
  788. EXPECT_TRUE(pkt.inClass(DOCSIS3_CLASS_MODEM));
  789. // Check that it's ok to add to the same class repeatedly
  790. EXPECT_NO_THROW(pkt.addClass("foo"));
  791. EXPECT_NO_THROW(pkt.addClass("foo"));
  792. EXPECT_NO_THROW(pkt.addClass("foo"));
  793. // Check that the packet belongs to 'foo'
  794. EXPECT_TRUE(pkt.inClass("foo"));
  795. }
  796. // Tests whether MAC can be obtained and that MAC sources are not
  797. // confused.
  798. TEST_F(Pkt6Test, getMAC) {
  799. Pkt6 pkt(DHCPV6_ADVERTISE, 1234);
  800. // DHCPv6 packet by default doens't have MAC address specified.
  801. EXPECT_FALSE(pkt.getMAC(HWAddr::HWADDR_SOURCE_ANY));
  802. EXPECT_FALSE(pkt.getMAC(HWAddr::HWADDR_SOURCE_RAW));
  803. // We haven't specified source IPv6 address, so this method should
  804. // fail, too
  805. EXPECT_FALSE(pkt.getMAC(HWAddr::HWADDR_SOURCE_IPV6_LINK_LOCAL));
  806. // Let's check if setting IPv6 address improves the situation.
  807. IOAddress linklocal_eui64("fe80::204:06ff:fe08:0a0c");
  808. pkt.setRemoteAddr(linklocal_eui64);
  809. EXPECT_TRUE(pkt.getMAC(HWAddr::HWADDR_SOURCE_ANY));
  810. EXPECT_TRUE(pkt.getMAC(HWAddr::HWADDR_SOURCE_IPV6_LINK_LOCAL));
  811. EXPECT_TRUE(pkt.getMAC(HWAddr::HWADDR_SOURCE_IPV6_LINK_LOCAL |
  812. HWAddr::HWADDR_SOURCE_RAW));
  813. pkt.setRemoteAddr(IOAddress("::"));
  814. // Let's invent a MAC
  815. const uint8_t hw[] = { 2, 4, 6, 8, 10, 12 }; // MAC
  816. const uint8_t hw_type = 123; // hardware type
  817. HWAddrPtr dummy_hwaddr(new HWAddr(hw, sizeof(hw), hw_type));
  818. // Now let's pretend that we obtained it from raw sockets
  819. pkt.setRemoteHWAddr(dummy_hwaddr);
  820. // Now we should be able to get something
  821. ASSERT_TRUE(pkt.getMAC(HWAddr::HWADDR_SOURCE_ANY));
  822. ASSERT_TRUE(pkt.getMAC(HWAddr::HWADDR_SOURCE_RAW));
  823. EXPECT_TRUE(pkt.getMAC(HWAddr::HWADDR_SOURCE_IPV6_LINK_LOCAL |
  824. HWAddr::HWADDR_SOURCE_RAW));
  825. // Check that the returned MAC is indeed the expected one
  826. ASSERT_TRUE(*dummy_hwaddr == *pkt.getMAC(HWAddr::HWADDR_SOURCE_ANY));
  827. ASSERT_TRUE(*dummy_hwaddr == *pkt.getMAC(HWAddr::HWADDR_SOURCE_RAW));
  828. }
  829. // Test checks whether getMACFromIPv6LinkLocal() returns the hardware (MAC)
  830. // address properly (for direct message).
  831. TEST_F(Pkt6Test, getMACFromIPv6LinkLocal_direct) {
  832. Pkt6 pkt(DHCPV6_ADVERTISE, 1234);
  833. // Let's get the first interface
  834. IfacePtr iface = IfaceMgr::instance().getIface(1);
  835. ASSERT_TRUE(iface);
  836. // and set source interface data properly. getMACFromIPv6LinkLocal attempts
  837. // to use source interface to obtain hardware type
  838. pkt.setIface(iface->getName());
  839. pkt.setIndex(iface->getIndex());
  840. // Note that u and g bits (the least significant ones of the most
  841. // significant byte) have special meaning and must not be set in MAC.
  842. // u bit is always set in EUI-64. g is always cleared.
  843. IOAddress global("2001:db8::204:06ff:fe08:0a:0c");
  844. IOAddress linklocal_eui64("fe80::f204:06ff:fe08:0a0c");
  845. IOAddress linklocal_noneui64("fe80::f204:0608:0a0c:0e10");
  846. // If received from a global address, this method should fail
  847. pkt.setRemoteAddr(global);
  848. EXPECT_FALSE(pkt.getMAC(HWAddr::HWADDR_SOURCE_IPV6_LINK_LOCAL));
  849. // If received from link-local that is EUI-64 based, it should succeed
  850. pkt.setRemoteAddr(linklocal_eui64);
  851. HWAddrPtr found = pkt.getMAC(HWAddr::HWADDR_SOURCE_IPV6_LINK_LOCAL);
  852. ASSERT_TRUE(found);
  853. stringstream tmp;
  854. tmp << "hwtype=" << (int)iface->getHWType() << " f0:04:06:08:0a:0c";
  855. EXPECT_EQ(tmp.str(), found->toText(true));
  856. }
  857. // Test checks whether getMACFromIPv6LinkLocal() returns the hardware (MAC)
  858. // address properly (for relayed message).
  859. TEST_F(Pkt6Test, getMACFromIPv6LinkLocal_singleRelay) {
  860. // Let's create a Solicit first...
  861. Pkt6 pkt(DHCPV6_SOLICIT, 1234);
  862. // ... and pretend it was relayed by a single relay.
  863. Pkt6::RelayInfo info;
  864. pkt.addRelayInfo(info);
  865. ASSERT_EQ(1, pkt.relay_info_.size());
  866. // Let's get the first interface
  867. IfacePtr iface = IfaceMgr::instance().getIface(1);
  868. ASSERT_TRUE(iface);
  869. // and set source interface data properly. getMACFromIPv6LinkLocal attempts
  870. // to use source interface to obtain hardware type
  871. pkt.setIface(iface->getName());
  872. pkt.setIndex(iface->getIndex());
  873. IOAddress global("2001:db8::204:06ff:fe08:0a:0c"); // global address
  874. IOAddress linklocal_noneui64("fe80::f204:0608:0a0c:0e10"); // no fffe
  875. IOAddress linklocal_eui64("fe80::f204:06ff:fe08:0a0c"); // valid EUI-64
  876. // If received from a global address, this method should fail
  877. pkt.relay_info_[0].peeraddr_ = global;
  878. EXPECT_FALSE(pkt.getMAC(HWAddr::HWADDR_SOURCE_IPV6_LINK_LOCAL));
  879. // If received from a link-local that does not use EUI-64, it should fail
  880. pkt.relay_info_[0].peeraddr_ = linklocal_noneui64;
  881. EXPECT_FALSE(pkt.getMAC(HWAddr::HWADDR_SOURCE_IPV6_LINK_LOCAL));
  882. // If received from link-local that is EUI-64 based, it should succeed
  883. pkt.relay_info_[0].peeraddr_ = linklocal_eui64;
  884. HWAddrPtr found = pkt.getMAC(HWAddr::HWADDR_SOURCE_IPV6_LINK_LOCAL);
  885. ASSERT_TRUE(found);
  886. stringstream tmp;
  887. tmp << "hwtype=" << (int)iface->getHWType() << " f0:04:06:08:0a:0c";
  888. EXPECT_EQ(tmp.str(), found->toText(true));
  889. }
  890. // Test checks whether getMACFromIPv6LinkLocal() returns the hardware (MAC)
  891. // address properly (for a message relayed multiple times).
  892. TEST_F(Pkt6Test, getMACFromIPv6LinkLocal_multiRelay) {
  893. // Let's create a Solicit first...
  894. Pkt6 pkt(DHCPV6_SOLICIT, 1234);
  895. // ... and pretend it was relayed via 3 relays. Keep in mind that
  896. // the relays are stored in relay_info_ in the encapsulation order
  897. // rather than in traverse order. The following simulates:
  898. // client --- relay1 --- relay2 --- relay3 --- server
  899. IOAddress linklocal1("fe80::200:ff:fe00:1"); // valid EUI-64
  900. IOAddress linklocal2("fe80::200:ff:fe00:2"); // valid EUI-64
  901. IOAddress linklocal3("fe80::200:ff:fe00:3"); // valid EUI-64
  902. // Let's add info about relay3. This was the last relay, so it added the
  903. // outermost encapsulation layer, so it was parsed first during reception.
  904. // Its peer-addr field contains an address of relay2, so it's useless for
  905. // this method.
  906. Pkt6::RelayInfo info;
  907. info.peeraddr_ = linklocal3;
  908. pkt.addRelayInfo(info);
  909. // Now add info about relay2. Its peer-addr contains an address of the
  910. // previous relay (relay1). Still useless for us.
  911. info.peeraddr_ = linklocal2;
  912. pkt.addRelayInfo(info);
  913. // Finally add the first relay. This is the relay that received the packet
  914. // from the client directly, so its peer-addr field contains an address of
  915. // the client. The method should get that address and build MAC from it.
  916. info.peeraddr_ = linklocal1;
  917. pkt.addRelayInfo(info);
  918. ASSERT_EQ(3, pkt.relay_info_.size());
  919. // Let's get the first interface
  920. IfacePtr iface = IfaceMgr::instance().getIface(1);
  921. ASSERT_TRUE(iface);
  922. // and set source interface data properly. getMACFromIPv6LinkLocal attempts
  923. // to use source interface to obtain hardware type
  924. pkt.setIface(iface->getName());
  925. pkt.setIndex(iface->getIndex());
  926. // The method should return MAC based on the first relay that was closest
  927. HWAddrPtr found = pkt.getMAC(HWAddr::HWADDR_SOURCE_IPV6_LINK_LOCAL);
  928. ASSERT_TRUE(found);
  929. // Let's check the info now.
  930. stringstream tmp;
  931. tmp << "hwtype=" << iface->getHWType() << " 00:00:00:00:00:01";
  932. EXPECT_EQ(tmp.str(), found->toText(true));
  933. }
  934. // Test checks whether getMACFromIPv6RelayOpt() returns the hardware (MAC)
  935. // address properly from a single relayed message.
  936. TEST_F(Pkt6Test, getMACFromIPv6RelayOpt_singleRelay) {
  937. // Let's create a Solicit first...
  938. Pkt6 pkt(DHCPV6_SOLICIT, 1234);
  939. // Packets that are not relayed should fail
  940. EXPECT_FALSE(pkt.getMAC(HWAddr::HWADDR_SOURCE_CLIENT_ADDR_RELAY_OPTION));
  941. // Now pretend it was relayed by a single relay.
  942. Pkt6::RelayInfo info;
  943. // generate options with code 79 and client link layer address
  944. const uint8_t opt_data[] = {
  945. 0x00, 0x01, // Ethertype
  946. 0x0a, 0x1b, 0x0b, 0x01, 0xca, 0xfe // MAC
  947. };
  948. OptionPtr relay_opt(new Option(Option::V6, 79,
  949. OptionBuffer(opt_data, opt_data + sizeof(opt_data))));
  950. info.options_.insert(make_pair(relay_opt->getType(), relay_opt));
  951. pkt.addRelayInfo(info);
  952. ASSERT_EQ(1, pkt.relay_info_.size());
  953. HWAddrPtr found = pkt.getMAC(HWAddr::HWADDR_SOURCE_CLIENT_ADDR_RELAY_OPTION);
  954. ASSERT_TRUE(found);
  955. stringstream tmp;
  956. tmp << "hwtype=1 0a:1b:0b:01:ca:fe";
  957. EXPECT_EQ(tmp.str(), found->toText(true));
  958. }
  959. // Test checks whether getMACFromIPv6RelayOpt() returns the hardware (MAC)
  960. // address properly from a message relayed by multiple servers.
  961. TEST_F(Pkt6Test, getMACFromIPv6RelayOpt_multipleRelay) {
  962. // Let's create a Solicit first...
  963. Pkt6 pkt(DHCPV6_SOLICIT, 1234);
  964. // Now pretend it was relayed two times. The relay closest to the server
  965. // adds link-layer-address information against the RFC, the process fails.
  966. Pkt6::RelayInfo info1;
  967. uint8_t opt_data[] = {
  968. 0x00, 0x01, // Ethertype
  969. 0x1a, 0x30, 0x0b, 0xfa, 0xc0, 0xfe // MAC
  970. };
  971. OptionPtr relay_opt1(new Option(Option::V6, D6O_CLIENT_LINKLAYER_ADDR,
  972. OptionBuffer(opt_data, opt_data + sizeof(opt_data))));
  973. info1.options_.insert(make_pair(relay_opt1->getType(), relay_opt1));
  974. pkt.addRelayInfo(info1);
  975. // Second relay, closest to the client has not implemented RFC6939
  976. Pkt6::RelayInfo info2;
  977. pkt.addRelayInfo(info2);
  978. ASSERT_EQ(2, pkt.relay_info_.size());
  979. EXPECT_FALSE(pkt.getMAC(HWAddr::HWADDR_SOURCE_CLIENT_ADDR_RELAY_OPTION));
  980. // Let's envolve the packet with a third relay (now the closest to the client)
  981. // that inserts the correct client_linklayer_addr option.
  982. Pkt6::RelayInfo info3;
  983. // We reuse the option and modify the MAC to be sure we get the right address
  984. opt_data[2] = 0xfa;
  985. OptionPtr relay_opt3(new Option(Option::V6, D6O_CLIENT_LINKLAYER_ADDR,
  986. OptionBuffer(opt_data, opt_data + sizeof(opt_data))));
  987. info3.options_.insert(make_pair(relay_opt3->getType(), relay_opt3));
  988. pkt.addRelayInfo(info3);
  989. ASSERT_EQ(3, pkt.relay_info_.size());
  990. // Now extract the MAC address from the relayed option
  991. HWAddrPtr found = pkt.getMAC(HWAddr::HWADDR_SOURCE_CLIENT_ADDR_RELAY_OPTION);
  992. ASSERT_TRUE(found);
  993. stringstream tmp;
  994. tmp << "hwtype=1 fa:30:0b:fa:c0:fe";
  995. EXPECT_EQ(tmp.str(), found->toText(true));
  996. }
  997. TEST_F(Pkt6Test, getMACFromDUID) {
  998. Pkt6 pkt(DHCPV6_ADVERTISE, 1234);
  999. // Although MACs are typically 6 bytes long, let's make this test a bit
  1000. // more challenging and use odd MAC lengths.
  1001. uint8_t duid_llt[] = { 0, 1, // type (DUID-LLT)
  1002. 0, 7, // hwtype (7 - just a randomly picked value)
  1003. 1, 2, 3, 4, // timestamp
  1004. 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, 0x10 // MAC address (7 bytes)
  1005. };
  1006. uint8_t duid_ll[] = { 0, 3, // type (DUID-LL)
  1007. 0, 11, // hwtype (11 - just a randomly picked value)
  1008. 0xa, 0xb, 0xc, 0xd, 0xe // MAC address (5 bytes)
  1009. };
  1010. uint8_t duid_en[] = { 0, 2, // type (DUID-EN)
  1011. 1, 2, 3, 4, // enterprise-id
  1012. 0xa, 0xb, 0xc // opaque data
  1013. };
  1014. OptionPtr clientid1(new Option(Option::V6, D6O_CLIENTID, OptionBuffer(
  1015. duid_llt, duid_llt + sizeof(duid_llt))));
  1016. OptionPtr clientid2(new Option(Option::V6, D6O_CLIENTID, OptionBuffer(
  1017. duid_ll, duid_ll + sizeof(duid_ll))));
  1018. OptionPtr clientid3(new Option(Option::V6, D6O_CLIENTID, OptionBuffer(
  1019. duid_en, duid_en + sizeof(duid_en))));
  1020. // Packet does not have any client-id, this call should fail
  1021. EXPECT_FALSE(pkt.getMAC(HWAddr::HWADDR_SOURCE_DUID));
  1022. // Let's test DUID-LLT. This should work.
  1023. pkt.addOption(clientid1);
  1024. HWAddrPtr mac = pkt.getMAC(HWAddr::HWADDR_SOURCE_DUID);
  1025. ASSERT_TRUE(mac);
  1026. EXPECT_EQ("hwtype=7 0a:0b:0c:0d:0e:0f:10", mac->toText(true));
  1027. // Let's test DUID-LL. This should work.
  1028. ASSERT_TRUE(pkt.delOption(D6O_CLIENTID));
  1029. pkt.addOption(clientid2);
  1030. mac = pkt.getMAC(HWAddr::HWADDR_SOURCE_DUID);
  1031. ASSERT_TRUE(mac);
  1032. EXPECT_EQ("hwtype=11 0a:0b:0c:0d:0e", mac->toText(true));
  1033. // Finally, let's try DUID-EN. This should fail, as EN type does not
  1034. // contain any MAC address information.
  1035. ASSERT_TRUE(pkt.delOption(D6O_CLIENTID));
  1036. pkt.addOption(clientid3);
  1037. EXPECT_FALSE(pkt.getMAC(HWAddr::HWADDR_SOURCE_DUID));
  1038. }
  1039. // Test checks whether getMAC(DOCSIS_MODEM) is working properly.
  1040. // We only have a small number of actual traffic captures from
  1041. // cable networks, so the scope of unit-tests is somewhat limited.
  1042. TEST_F(Pkt6Test, getMAC_DOCSIS_Modem) {
  1043. // Let's use a captured traffic. The one we have comes from a
  1044. // modem with MAC address 10:0d:7f:00:07:88.
  1045. Pkt6Ptr pkt = isc::test::PktCaptures::captureDocsisRelayedSolicit();
  1046. ASSERT_NO_THROW(pkt->unpack());
  1047. // The method should return MAC based on the vendor-specific info,
  1048. // suboption 36, which is inserted by the modem itself.
  1049. HWAddrPtr found = pkt->getMAC(HWAddr::HWADDR_SOURCE_DOCSIS_MODEM);
  1050. ASSERT_TRUE(found);
  1051. // Let's check the info.
  1052. EXPECT_EQ("hwtype=1 10:0d:7f:00:07:88", found->toText(true));
  1053. // Now let's remove the option
  1054. OptionVendorPtr vendor = boost::dynamic_pointer_cast<
  1055. OptionVendor>(pkt->getOption(D6O_VENDOR_OPTS));
  1056. ASSERT_TRUE(vendor);
  1057. ASSERT_TRUE(vendor->delOption(DOCSIS3_V6_DEVICE_ID));
  1058. // Ok, there's no more suboption 36. Now getMAC() should fail.
  1059. EXPECT_FALSE(pkt->getMAC(HWAddr::HWADDR_SOURCE_DOCSIS_MODEM));
  1060. }
  1061. // Test checks whether getMAC(DOCSIS_CMTS) is working properly.
  1062. // We only have a small number of actual traffic captures from
  1063. // cable networks, so the scope of unit-tests is somewhat limited.
  1064. TEST_F(Pkt6Test, getMAC_DOCSIS_CMTS) {
  1065. // Let's use a captured traffic. The one we have comes from a
  1066. // modem with MAC address 20:e5:2a:b8:15:14.
  1067. Pkt6Ptr pkt = isc::test::PktCaptures::captureeRouterRelayedSolicit();
  1068. ASSERT_NO_THROW(pkt->unpack());
  1069. // The method should return MAC based on the vendor-specific info,
  1070. // suboption 36, which is inserted by the modem itself.
  1071. HWAddrPtr found = pkt->getMAC(HWAddr::HWADDR_SOURCE_DOCSIS_CMTS);
  1072. ASSERT_TRUE(found);
  1073. // Let's check the info.
  1074. EXPECT_EQ("hwtype=1 20:e5:2a:b8:15:14", found->toText(true));
  1075. // Now let's remove the suboption 1026 that is inserted by the
  1076. // relay.
  1077. OptionVendorPtr vendor = boost::dynamic_pointer_cast<
  1078. OptionVendor>(pkt->getAnyRelayOption(D6O_VENDOR_OPTS,
  1079. isc::dhcp::Pkt6::RELAY_SEARCH_FROM_CLIENT));
  1080. ASSERT_TRUE(vendor);
  1081. EXPECT_TRUE(vendor->delOption(DOCSIS3_V6_CMTS_CM_MAC));
  1082. EXPECT_FALSE(pkt->getMAC(HWAddr::HWADDR_SOURCE_DOCSIS_CMTS));
  1083. }
  1084. // Test checks whether getMACFromRemoteIdRelayOption() returns the hardware (MAC)
  1085. // address properly from a relayed message.
  1086. TEST_F(Pkt6Test, getMACFromRemoteIdRelayOption) {
  1087. // Create a solicit message.
  1088. Pkt6 pkt(DHCPV6_SOLICIT, 1234);
  1089. // This should fail as the message is't relayed yet.
  1090. EXPECT_FALSE(pkt.getMAC(HWAddr::HWADDR_SOURCE_REMOTE_ID));
  1091. // Let's get the first interface
  1092. IfacePtr iface = IfaceMgr::instance().getIface(1);
  1093. ASSERT_TRUE(iface);
  1094. // and set source interface data properly. getMACFromIPv6LinkLocal attempts
  1095. // to use source interface to obtain hardware type
  1096. pkt.setIface(iface->getName());
  1097. pkt.setIndex(iface->getIndex());
  1098. // Generate option data with randomly picked enterprise number and MAC address
  1099. const uint8_t opt_data[] = {
  1100. 1, 2, 3, 4, // enterprise-number
  1101. 0xa, 0xb, 0xc, 0xd, 0xe, 0xf // MAC
  1102. };
  1103. // Create option with number 37 (remote-id relay agent option)
  1104. OptionPtr relay_opt(new Option(Option::V6, D6O_REMOTE_ID,
  1105. OptionBuffer(opt_data, opt_data + sizeof(opt_data))));
  1106. // First simulate relaying message without adding remote-id option
  1107. Pkt6::RelayInfo info;
  1108. pkt.addRelayInfo(info);
  1109. ASSERT_EQ(1, pkt.relay_info_.size());
  1110. // This should fail as the remote-id option isn't there
  1111. EXPECT_FALSE(pkt.getMAC(HWAddr::HWADDR_SOURCE_REMOTE_ID));
  1112. // Now add this option to the relayed message
  1113. info.options_.insert(make_pair(relay_opt->getType(), relay_opt));
  1114. pkt.addRelayInfo(info);
  1115. ASSERT_EQ(2, pkt.relay_info_.size());
  1116. // This should work now
  1117. HWAddrPtr mac = pkt.getMAC(HWAddr::HWADDR_SOURCE_REMOTE_ID);
  1118. ASSERT_TRUE(mac);
  1119. stringstream tmp;
  1120. tmp << "hwtype=" << (int)iface->getHWType() << " 0a:0b:0c:0d:0e:0f";
  1121. EXPECT_EQ(tmp.str(), mac->toText(true));
  1122. }
  1123. // This test verifies that a solicit that passed through two relays is parsed
  1124. // properly. In particular the second relay (outer encapsulation) included RSOO
  1125. // (Relay Supplied Options option). This test checks whether it was parsed
  1126. // properly. See captureRelayed2xRSOO() description for details.
  1127. TEST_F(Pkt6Test, rsoo) {
  1128. Pkt6Ptr msg = test::PktCaptures::captureRelayed2xRSOO();
  1129. EXPECT_NO_THROW(msg->unpack());
  1130. EXPECT_EQ(DHCPV6_SOLICIT, msg->getType());
  1131. EXPECT_EQ(217, msg->len());
  1132. ASSERT_EQ(2, msg->relay_info_.size());
  1133. // There should be an RSOO option in the outermost relay
  1134. OptionPtr opt = msg->getRelayOption(D6O_RSOO, 1);
  1135. ASSERT_TRUE(opt);
  1136. EXPECT_EQ(D6O_RSOO, opt->getType());
  1137. const OptionCollection& rsoo = opt->getOptions();
  1138. ASSERT_EQ(2, rsoo.size());
  1139. OptionPtr rsoo1 = opt->getOption(255);
  1140. OptionPtr rsoo2 = opt->getOption(256);
  1141. ASSERT_TRUE(rsoo1);
  1142. ASSERT_TRUE(rsoo2);
  1143. EXPECT_EQ(8, rsoo1->len()); // 4 bytes of data + header
  1144. EXPECT_EQ(13, rsoo2->len()); // 9 bytes of data + header
  1145. }
  1146. // Verify that the DUID can be extracted from the DHCPv6 packet
  1147. // holding Client Identifier option.
  1148. TEST_F(Pkt6Test, getClientId) {
  1149. // Create a packet.
  1150. Pkt6Ptr pkt(new Pkt6(DHCPV6_SOLICIT, 0x2312));
  1151. // Initially, the packet should hold no DUID.
  1152. EXPECT_FALSE(pkt->getClientId());
  1153. // Create DUID and add it to the packet.
  1154. const uint8_t duid_data[] = { 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 0 };
  1155. OptionBuffer duid_vec(duid_data, duid_data + sizeof(duid_data) - 1);
  1156. pkt->addOption(OptionPtr(new Option(Option::V6, D6O_CLIENTID,
  1157. duid_vec.begin(),
  1158. duid_vec.end())));
  1159. // Simulate the packet transmission over the wire, i.e. create on
  1160. // wire representation of the packet, and then parse it.
  1161. Pkt6Ptr pkt_clone = packAndClone(pkt);
  1162. ASSERT_NO_THROW(pkt_clone->unpack());
  1163. // This time the DUID should be returned.
  1164. DuidPtr duid = pkt_clone->getClientId();
  1165. ASSERT_TRUE(duid);
  1166. // And it should be equal to the one that we used to create
  1167. // the packet.
  1168. EXPECT_TRUE(duid->getDuid() == duid_vec);
  1169. }
  1170. // This test verfies that it is possible to obtain the packet
  1171. // identifiers (DUID, HW Address, transaction id) in the textual
  1172. // format.
  1173. TEST_F(Pkt6Test, makeLabel) {
  1174. DuidPtr duid(new DUID(DUID::fromText("0102020202030303030303")));
  1175. HWAddrPtr hwaddr(new HWAddr(HWAddr::fromText("01:02:03:04:05:06",
  1176. HTYPE_ETHER)));
  1177. // Specify DUID and no HW Address.
  1178. EXPECT_EQ("duid=[01:02:02:02:02:03:03:03:03:03:03], tid=0x123",
  1179. Pkt6::makeLabel(duid, 0x123, HWAddrPtr()));
  1180. // Specify HW Address and no DUID.
  1181. EXPECT_EQ("duid=[no info], [hwtype=1 01:02:03:04:05:06], tid=0x123",
  1182. Pkt6::makeLabel(DuidPtr(), 0x123, hwaddr));
  1183. // Specify both DUID and HW Address.
  1184. EXPECT_EQ("duid=[01:02:02:02:02:03:03:03:03:03:03], "
  1185. "[hwtype=1 01:02:03:04:05:06], tid=0x123",
  1186. Pkt6::makeLabel(duid, 0x123, hwaddr));
  1187. // Specify neither DUID nor HW Address.
  1188. EXPECT_EQ("duid=[no info], tid=0x0",
  1189. Pkt6::makeLabel(DuidPtr(), 0x0, HWAddrPtr()));
  1190. }
  1191. // This test verifies that it is possible to obtain the packet
  1192. // identifiers in the textual format from the packet instance.
  1193. TEST_F(Pkt6Test, getLabel) {
  1194. // Create a packet.
  1195. Pkt6Ptr pkt(new Pkt6(DHCPV6_SOLICIT, 0x2312));
  1196. EXPECT_EQ("duid=[no info], tid=0x2312",
  1197. pkt->getLabel());
  1198. DuidPtr duid(new DUID(DUID::fromText("0102020202030303030303")));
  1199. pkt->addOption(OptionPtr(new Option(Option::V6, D6O_CLIENTID,
  1200. duid->getDuid().begin(),
  1201. duid->getDuid().end())));
  1202. // Simulate the packet transmission over the wire, i.e. create on
  1203. // wire representation of the packet, and then parse it.
  1204. Pkt6Ptr pkt_clone = packAndClone(pkt);
  1205. ASSERT_NO_THROW(pkt_clone->unpack());
  1206. EXPECT_EQ("duid=[01:02:02:02:02:03:03:03:03:03:03], tid=0x2312",
  1207. pkt_clone->getLabel());
  1208. }
  1209. }