pkt6_unittest.cc 55 KB

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