pkt6_unittest.cc 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041
  1. // Copyright (C) 2011-2014 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. #include <config.h>
  15. #include <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/iface_mgr.h>
  23. #include <dhcp/pkt6.h>
  24. #include <dhcp/hwaddr.h>
  25. #include <dhcp/docsis3_option_defs.h>
  26. #include <util/range_utilities.h>
  27. #include <boost/bind.hpp>
  28. #include <boost/date_time/posix_time/posix_time.hpp>
  29. #include <boost/scoped_ptr.hpp>
  30. #include <util/encode/hex.h>
  31. #include <gtest/gtest.h>
  32. #include <iostream>
  33. #include <sstream>
  34. #include <arpa/inet.h>
  35. using namespace std;
  36. using namespace isc;
  37. using namespace isc::asiolink;
  38. using namespace isc::dhcp;
  39. using boost::scoped_ptr;
  40. namespace {
  41. /// @brief A class which contains a custom callback function to unpack options.
  42. ///
  43. /// This is a class used by the tests which verify that the custom callback
  44. /// functions can be installed to unpack options from a message. When the
  45. /// callback function is called, the executed_ member is set to true to allow
  46. /// verification that the callback was really called. Internally, this class
  47. /// uses libdhcp++ to unpack options so the options parsing algorithm remains
  48. /// unchanged after installation of the callback.
  49. class CustomUnpackCallback {
  50. public:
  51. /// @brief Constructor
  52. ///
  53. /// Marks that callback hasn't been called.
  54. CustomUnpackCallback()
  55. : executed_(false) {
  56. }
  57. /// @brief A callback
  58. ///
  59. /// Contains custom implementation of the callback.
  60. ///
  61. /// @param buf a A buffer holding options in on-wire format.
  62. /// @param option_space A name of the option space encapsulated by the
  63. /// option being parsed.
  64. /// @param [out] options A reference to the collection where parsed options
  65. /// will be stored.
  66. /// @param relay_msg_offset Reference to a size_t structure. If specified,
  67. /// offset to beginning of relay_msg option will be stored in it.
  68. /// @param relay_msg_len reference to a size_t structure. If specified,
  69. /// length of the relay_msg option will be stored in it.
  70. /// @return An offset to the first byte after last parsed option.
  71. size_t execute(const OptionBuffer& buf,
  72. const std::string& option_space,
  73. isc::dhcp::OptionCollection& options,
  74. size_t* relay_msg_offset,
  75. size_t* relay_msg_len) {
  76. // Set the executed_ member to true to allow verification that the
  77. // callback has been actually called.
  78. executed_ = true;
  79. // Use default implementation of the unpack algorithm to parse options.
  80. return (LibDHCP::unpackOptions6(buf, option_space, options, relay_msg_offset,
  81. relay_msg_len));
  82. }
  83. /// A flag which indicates if callback function has been called.
  84. bool executed_;
  85. };
  86. class Pkt6Test : public ::testing::Test {
  87. public:
  88. Pkt6Test() {
  89. }
  90. /// @brief generates an option with given code (and length) and random content
  91. ///
  92. /// @param code option code
  93. /// @param len data length (data will be randomized)
  94. ///
  95. /// @return pointer to the new option
  96. OptionPtr generateRandomOption(uint16_t code, size_t len = 10) {
  97. OptionBuffer data(len);
  98. util::fillRandom(data.begin(), data.end());
  99. return OptionPtr(new Option(Option::V6, code, data));
  100. }
  101. /// @brief Create a wire representation of the test packet and clone it.
  102. ///
  103. /// The purpose of this function is to create a packet to be used to
  104. /// check that packet parsing works correctly. The unpack() function
  105. /// requires that the data_ field of the object holds the data to be
  106. /// parsed. This function creates an on-wire representation of the
  107. /// packet by calling pack(). But, the pack() function stores the
  108. /// on-wire representation into the output buffer (not the data_ field).
  109. /// For this reason, it is not enough to return the packet on which
  110. /// pack() is called. This function returns a clone of this packet
  111. /// which is created using a constructor taking a buffer and buffer
  112. /// length as an input. This constructor is normally used to parse
  113. /// received packets. It stores the packet in a data_ field and
  114. /// therefore unpack() can be called to parse it.
  115. Pkt6Ptr packAndClone() {
  116. Pkt6Ptr parent(new Pkt6(DHCPV6_SOLICIT, 0x020304));
  117. OptionPtr opt1(new Option(Option::V6, 1));
  118. OptionPtr opt2(new Option(Option::V6, 2));
  119. OptionPtr opt3(new Option(Option::V6, 100));
  120. // Let's not use zero-length option type 3 as it is IA_NA
  121. parent->addOption(opt1);
  122. parent->addOption(opt2);
  123. parent->addOption(opt3);
  124. EXPECT_EQ(DHCPV6_SOLICIT, parent->getType());
  125. // Calculated length should be 16
  126. EXPECT_EQ(Pkt6::DHCPV6_PKT_HDR_LEN + 3 * Option::OPTION6_HDR_LEN,
  127. parent->len());
  128. EXPECT_NO_THROW(parent->pack());
  129. EXPECT_EQ(Pkt6::DHCPV6_PKT_HDR_LEN + 3 * Option::OPTION6_HDR_LEN,
  130. parent->len());
  131. // Create second packet,based on assembled data from the first one
  132. Pkt6Ptr clone(new Pkt6(static_cast<const uint8_t*>
  133. (parent->getBuffer().getData()),
  134. parent->getBuffer().getLength()));
  135. return (clone);
  136. }
  137. };
  138. TEST_F(Pkt6Test, constructor) {
  139. uint8_t data[] = { 0, 1, 2, 3, 4, 5 };
  140. scoped_ptr<Pkt6> pkt1(new Pkt6(data, sizeof(data)));
  141. EXPECT_EQ(6, pkt1->data_.size());
  142. EXPECT_EQ(0, memcmp( &pkt1->data_[0], data, sizeof(data)));
  143. }
  144. /// @brief returns captured actual SOLICIT packet
  145. ///
  146. /// Captured SOLICIT packet with transid=0x3d79fb and options: client-id,
  147. /// in_na, dns-server, elapsed-time, option-request
  148. /// This code was autogenerated (see src/bin/dhcp6/tests/iface_mgr_unittest.c),
  149. /// but we spent some time to make is less ugly than it used to be.
  150. ///
  151. /// @return pointer to Pkt6 that represents received SOLICIT
  152. Pkt6* capture1() {
  153. Pkt6* pkt;
  154. uint8_t data[98];
  155. data[0] = 1;
  156. data[1] = 1; data[2] = 2; data[3] = 3; data[4] = 0;
  157. data[5] = 1; data[6] = 0; data[7] = 14; data[8] = 0;
  158. data[9] = 1; data[10] = 0; data[11] = 1; data[12] = 21;
  159. data[13] = 158; data[14] = 60; data[15] = 22; data[16] = 0;
  160. data[17] = 30; data[18] = 140; data[19] = 155; data[20] = 115;
  161. data[21] = 73; data[22] = 0; data[23] = 3; data[24] = 0;
  162. data[25] = 40; data[26] = 0; data[27] = 0; data[28] = 0;
  163. data[29] = 1; data[30] = 255; data[31] = 255; data[32] = 255;
  164. data[33] = 255; data[34] = 255; data[35] = 255; data[36] = 255;
  165. data[37] = 255; data[38] = 0; data[39] = 5; data[40] = 0;
  166. data[41] = 24; data[42] = 32; data[43] = 1; data[44] = 13;
  167. data[45] = 184; data[46] = 0; data[47] = 1; data[48] = 0;
  168. data[49] = 0; data[50] = 0; data[51] = 0; data[52] = 0;
  169. data[53] = 0; data[54] = 0; data[55] = 0; data[56] = 18;
  170. data[57] = 52; data[58] = 255; data[59] = 255; data[60] = 255;
  171. data[61] = 255; data[62] = 255; data[63] = 255; data[64] = 255;
  172. data[65] = 255; data[66] = 0; data[67] = 23; data[68] = 0;
  173. data[69] = 16; data[70] = 32; data[71] = 1; data[72] = 13;
  174. data[73] = 184; data[74] = 0; data[75] = 1; data[76] = 0;
  175. data[77] = 0; data[78] = 0; data[79] = 0; data[80] = 0;
  176. data[81] = 0; data[82] = 0; data[83] = 0; data[84] = 221;
  177. data[85] = 221; data[86] = 0; data[87] = 8; data[88] = 0;
  178. data[89] = 2; data[90] = 0; data[91] = 100; data[92] = 0;
  179. data[93] = 6; data[94] = 0; data[95] = 2; data[96] = 0;
  180. data[97] = 23;
  181. pkt = new Pkt6(data, sizeof(data));
  182. pkt->setRemotePort(546);
  183. pkt->setRemoteAddr(IOAddress("fe80::21e:8cff:fe9b:7349"));
  184. pkt->setLocalPort(0);
  185. pkt->setLocalAddr(IOAddress("ff02::1:2"));
  186. pkt->setIndex(2);
  187. pkt->setIface("eth0");
  188. return (pkt);
  189. }
  190. /// @brief creates doubly relayed solicit message
  191. ///
  192. /// This is a traffic capture exported from wireshark. It includes a SOLICIT
  193. /// message that passed through two relays. Each relay include interface-id,
  194. /// remote-id and relay-forw encapsulation. It is especially interesting,
  195. /// because of the following properties:
  196. /// - double encapsulation
  197. /// - first relay inserts relay-msg before extra options
  198. /// - second relay inserts relay-msg after extra options
  199. /// - both relays are from different vendors
  200. /// - interface-id are different for each relay
  201. /// - first relay inserts valid remote-id
  202. /// - second relay inserts remote-id with empty vendor data
  203. /// - the solicit message requests for custom options in ORO
  204. /// - there are option types in RELAY-FORW that do not appear in SOLICIT
  205. /// - there are option types in SOLICT that do not appear in RELAY-FORW
  206. ///
  207. /// RELAY-FORW
  208. /// - relay message option
  209. /// - RELAY-FORW
  210. /// - interface-id option
  211. /// - remote-id option
  212. /// - RELAY-FORW
  213. /// SOLICIT
  214. /// - client-id option
  215. /// - ia_na option
  216. /// - elapsed time
  217. /// - ORO
  218. /// - interface-id option
  219. /// - remote-id option
  220. ///
  221. /// The original capture was posted to dibbler users mailing list.
  222. ///
  223. /// @return created double relayed SOLICIT message
  224. Pkt6* capture2() {
  225. // string exported from Wireshark
  226. string hex_string =
  227. "0c01200108880db800010000000000000000fe80000000000000020021fffe5c18a900"
  228. "09007d0c0000000000000000000000000000000000fe80000000000000020021fffe5c"
  229. "18a9001200154953414d3134342065746820312f312f30352f30310025000400000de9"
  230. "00090036016b4fe20001000e0001000118b033410000215c18a90003000c00000001ff"
  231. "ffffffffffffff00080002000000060006001700f200f30012001c4953414d3134347c"
  232. "3239397c697076367c6e743a76703a313a313130002500120000197f0001000118b033"
  233. "410000215c18a9";
  234. std::vector<uint8_t> bin;
  235. // Decode the hex string and store it in bin (which happens
  236. // to be OptionBuffer format)
  237. isc::util::encode::decodeHex(hex_string, bin);
  238. Pkt6* pkt = new Pkt6(&bin[0], bin.size());
  239. pkt->setRemotePort(547);
  240. pkt->setRemoteAddr(IOAddress("fe80::1234"));
  241. pkt->setLocalPort(547);
  242. pkt->setLocalAddr(IOAddress("ff05::1:3"));
  243. pkt->setIndex(2);
  244. pkt->setIface("eth0");
  245. return (pkt);
  246. }
  247. TEST_F(Pkt6Test, unpack_solicit1) {
  248. scoped_ptr<Pkt6> sol(capture1());
  249. ASSERT_NO_THROW(sol->unpack());
  250. // Check for length
  251. EXPECT_EQ(98, sol->len() );
  252. // Check for type
  253. EXPECT_EQ(DHCPV6_SOLICIT, sol->getType() );
  254. // Check that all present options are returned
  255. EXPECT_TRUE(sol->getOption(D6O_CLIENTID)); // client-id is present
  256. EXPECT_TRUE(sol->getOption(D6O_IA_NA)); // IA_NA is present
  257. EXPECT_TRUE(sol->getOption(D6O_ELAPSED_TIME)); // elapsed is present
  258. EXPECT_TRUE(sol->getOption(D6O_NAME_SERVERS));
  259. EXPECT_TRUE(sol->getOption(D6O_ORO));
  260. // Let's check that non-present options are not returned
  261. EXPECT_FALSE(sol->getOption(D6O_SERVERID)); // server-id is missing
  262. EXPECT_FALSE(sol->getOption(D6O_IA_TA));
  263. EXPECT_FALSE(sol->getOption(D6O_IAADDR));
  264. }
  265. TEST_F(Pkt6Test, packUnpack) {
  266. // Create an on-wire representation of the test packet and clone it.
  267. Pkt6Ptr clone = packAndClone();
  268. // Now recreate options list
  269. ASSERT_NO_THROW(clone->unpack());
  270. // transid, message-type should be the same as before
  271. EXPECT_EQ(0x020304, clone->getTransid());
  272. EXPECT_EQ(DHCPV6_SOLICIT, clone->getType());
  273. EXPECT_TRUE(clone->getOption(1));
  274. EXPECT_TRUE(clone->getOption(2));
  275. EXPECT_TRUE(clone->getOption(100));
  276. EXPECT_FALSE(clone->getOption(4));
  277. }
  278. // Checks if the code is able to handle malformed packet
  279. TEST_F(Pkt6Test, unpackMalformed) {
  280. // Get a packet. We're really interested in its on-wire 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 and 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
  303. // be parsed.
  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. }
  322. // This test verifies that it is possible to specify custom implementation of
  323. // the option parsing algorithm by installing a callback function.
  324. TEST_F(Pkt6Test, packUnpackWithCallback) {
  325. // Create an on-wire representation of the test packet and clone it.
  326. Pkt6Ptr clone = packAndClone();
  327. // Install the custom callback function. We expect that this function
  328. // will be called to parse options in the packet instead of
  329. // LibDHCP::unpackOptions6.
  330. CustomUnpackCallback cb;
  331. clone->setCallback(boost::bind(&CustomUnpackCallback::execute, &cb,
  332. _1, _2, _3, _4, _5));
  333. // Make sure that the flag which indicates if the callback function has
  334. // been called is not set. Otherwise, our test doesn't make sense.
  335. ASSERT_FALSE(cb.executed_);
  336. // Now recreate options list
  337. ASSERT_NO_THROW(clone->unpack());
  338. // An object which holds a callback should now have a flag set which
  339. // indicates that callback has been called.
  340. EXPECT_TRUE(cb.executed_);
  341. // transid, message-type should be the same as before
  342. EXPECT_EQ(0x020304, clone->getTransid());
  343. EXPECT_EQ(DHCPV6_SOLICIT, clone->getType());
  344. EXPECT_TRUE(clone->getOption(1));
  345. EXPECT_TRUE(clone->getOption(2));
  346. EXPECT_TRUE(clone->getOption(100));
  347. EXPECT_FALSE(clone->getOption(4));
  348. // Reset the indicator to perform another check: uninstall the callback.
  349. cb.executed_ = false;
  350. // By setting the callback to NULL we effectively uninstall the callback.
  351. clone->setCallback(NULL);
  352. // Do another unpack.
  353. ASSERT_NO_THROW(clone->unpack());
  354. // Callback should not be executed.
  355. EXPECT_FALSE(cb.executed_);
  356. }
  357. // This test verifies that options can be added (addOption()), retrieved
  358. // (getOption(), getOptions()) and deleted (delOption()).
  359. TEST_F(Pkt6Test, addGetDelOptions) {
  360. scoped_ptr<Pkt6> parent(new Pkt6(DHCPV6_SOLICIT, random()));
  361. OptionPtr opt1(new Option(Option::V6, 1));
  362. OptionPtr opt2(new Option(Option::V6, 2));
  363. OptionPtr opt3(new Option(Option::V6, 2));
  364. parent->addOption(opt1);
  365. parent->addOption(opt2);
  366. // getOption() test
  367. EXPECT_EQ(opt1, parent->getOption(1));
  368. EXPECT_EQ(opt2, parent->getOption(2));
  369. // Expect NULL
  370. EXPECT_EQ(OptionPtr(), parent->getOption(4));
  371. // Now there are 2 options of type 2
  372. parent->addOption(opt3);
  373. OptionCollection options = parent->getOptions(2);
  374. EXPECT_EQ(2, options.size()); // there should be 2 instances
  375. // Both options must be of type 2 and there must not be
  376. // any other type returned
  377. for (OptionCollection::const_iterator x= options.begin();
  378. x != options.end(); ++x) {
  379. EXPECT_EQ(2, x->second->getType());
  380. }
  381. // Try to get a single option. Normally for singular options
  382. // it is better to use getOption(), but getOptions() must work
  383. // as well
  384. options = parent->getOptions(1);
  385. ASSERT_EQ(1, options.size());
  386. EXPECT_EQ(1, (*options.begin()).second->getType());
  387. EXPECT_EQ(opt1, options.begin()->second);
  388. // Let's delete one of them
  389. EXPECT_EQ(true, parent->delOption(2));
  390. // There still should be the other option 2
  391. EXPECT_NE(OptionPtr(), parent->getOption(2));
  392. // Let's delete the other option 2
  393. EXPECT_EQ(true, parent->delOption(2));
  394. // No more options with type=2
  395. EXPECT_EQ(OptionPtr(), parent->getOption(2));
  396. // Let's try to delete - should fail
  397. EXPECT_TRUE(false == parent->delOption(2));
  398. // Finally try to get a non-existent option
  399. options = parent->getOptions(1234);
  400. EXPECT_EQ(0, options.size());
  401. }
  402. TEST_F(Pkt6Test, Timestamp) {
  403. boost::scoped_ptr<Pkt6> pkt(new Pkt6(DHCPV6_SOLICIT, 0x020304));
  404. // Just after construction timestamp is invalid
  405. ASSERT_TRUE(pkt->getTimestamp().is_not_a_date_time());
  406. // Update packet time.
  407. pkt->updateTimestamp();
  408. // Get updated packet time.
  409. boost::posix_time::ptime ts_packet = pkt->getTimestamp();
  410. // After timestamp is updated it should be date-time.
  411. ASSERT_FALSE(ts_packet.is_not_a_date_time());
  412. // Check current time.
  413. boost::posix_time::ptime ts_now =
  414. boost::posix_time::microsec_clock::universal_time();
  415. // Calculate period between packet time and now.
  416. boost::posix_time::time_period ts_period(ts_packet, ts_now);
  417. // Duration should be positive or zero.
  418. EXPECT_TRUE(ts_period.length().total_microseconds() >= 0);
  419. }
  420. // This test verifies that getName() method returns proper
  421. // packet type names.
  422. TEST_F(Pkt6Test, getName) {
  423. // Check all possible packet types
  424. for (int itype = 0; itype < 256; ++itype) {
  425. uint8_t type = itype;
  426. switch (type) {
  427. case DHCPV6_CONFIRM:
  428. EXPECT_STREQ("CONFIRM", Pkt6::getName(type));
  429. break;
  430. case DHCPV6_DECLINE:
  431. EXPECT_STREQ("DECLINE", Pkt6::getName(type));
  432. break;
  433. case DHCPV6_INFORMATION_REQUEST:
  434. EXPECT_STREQ("INFORMATION_REQUEST",
  435. Pkt6::getName(type));
  436. break;
  437. case DHCPV6_REBIND:
  438. EXPECT_STREQ("REBIND", Pkt6::getName(type));
  439. break;
  440. case DHCPV6_RELEASE:
  441. EXPECT_STREQ("RELEASE", Pkt6::getName(type));
  442. break;
  443. case DHCPV6_RENEW:
  444. EXPECT_STREQ("RENEW", Pkt6::getName(type));
  445. break;
  446. case DHCPV6_REQUEST:
  447. EXPECT_STREQ("REQUEST", Pkt6::getName(type));
  448. break;
  449. case DHCPV6_SOLICIT:
  450. EXPECT_STREQ("SOLICIT", Pkt6::getName(type));
  451. break;
  452. default:
  453. EXPECT_STREQ("UNKNOWN", Pkt6::getName(type));
  454. }
  455. }
  456. }
  457. // This test verifies that a fancy solicit that passed through two
  458. // relays can be parsed properly. See capture2() method description
  459. // for details regarding the packet.
  460. TEST_F(Pkt6Test, relayUnpack) {
  461. boost::scoped_ptr<Pkt6> msg(capture2());
  462. EXPECT_NO_THROW(msg->unpack());
  463. EXPECT_EQ(DHCPV6_SOLICIT, msg->getType());
  464. EXPECT_EQ(217, msg->len());
  465. ASSERT_EQ(2, msg->relay_info_.size());
  466. OptionPtr opt;
  467. // Part 1: Check options inserted by the first relay
  468. // There should be 2 options in first relay
  469. EXPECT_EQ(2, msg->relay_info_[0].options_.size());
  470. // There should be interface-id option
  471. ASSERT_TRUE(opt = msg->getRelayOption(D6O_INTERFACE_ID, 0));
  472. OptionBuffer data = opt->getData();
  473. EXPECT_EQ(32, opt->len()); // 28 bytes of data + 4 bytes header
  474. EXPECT_EQ(data.size(), 28);
  475. // That's a strange interface-id, but this is a real life example
  476. EXPECT_TRUE(0 == memcmp("ISAM144|299|ipv6|nt:vp:1:110", &data[0], 28));
  477. // Get the remote-id option
  478. ASSERT_TRUE(opt = msg->getRelayOption(D6O_REMOTE_ID, 0));
  479. EXPECT_EQ(22, opt->len()); // 18 bytes of data + 4 bytes header
  480. boost::shared_ptr<OptionCustom> custom = boost::dynamic_pointer_cast<OptionCustom>(opt);
  481. uint32_t vendor_id = custom->readInteger<uint32_t>(0);
  482. EXPECT_EQ(6527, vendor_id); // 6527 = Panthera Networks
  483. uint8_t expected_remote_id[] = { 0x00, 0x01, 0x00, 0x01, 0x18, 0xb0, 0x33, 0x41, 0x00,
  484. 0x00, 0x21, 0x5c, 0x18, 0xa9 };
  485. OptionBuffer remote_id = custom->readBinary(1);
  486. ASSERT_EQ(sizeof(expected_remote_id), remote_id.size());
  487. ASSERT_EQ(0, memcmp(expected_remote_id, &remote_id[0], remote_id.size()));
  488. // Part 2: Check options inserted by the second relay
  489. // Get the interface-id from the second relay
  490. ASSERT_TRUE(opt = msg->getRelayOption(D6O_INTERFACE_ID, 1));
  491. data = opt->getData();
  492. EXPECT_EQ(25, opt->len()); // 21 bytes + 4 bytes header
  493. EXPECT_EQ(data.size(), 21);
  494. EXPECT_TRUE(0 == memcmp("ISAM144 eth 1/1/05/01", &data[0], 21));
  495. // Get the remote-id option
  496. ASSERT_TRUE(opt = msg->getRelayOption(D6O_REMOTE_ID, 1));
  497. EXPECT_EQ(8, opt->len());
  498. custom = boost::dynamic_pointer_cast<OptionCustom>(opt);
  499. vendor_id = custom->readInteger<uint32_t>(0);
  500. EXPECT_EQ(3561, vendor_id); // 3561 = Broadband Forum
  501. // @todo: See if we can validate empty remote-id field
  502. // Let's check if there is no leak between options stored in
  503. // the SOLICIT message and the relay.
  504. EXPECT_FALSE(opt = msg->getRelayOption(D6O_IA_NA, 1));
  505. // Part 3: Let's check options in the message itself
  506. // This is not redundant compared to other direct messages tests,
  507. // as we parsed it differently
  508. EXPECT_EQ(DHCPV6_SOLICIT, msg->getType());
  509. EXPECT_EQ(0x6b4fe2, msg->getTransid());
  510. ASSERT_TRUE(opt = msg->getOption(D6O_CLIENTID));
  511. EXPECT_EQ(18, opt->len()); // 14 bytes of data + 4 bytes of header
  512. uint8_t expected_client_id[] = { 0x00, 0x01, 0x00, 0x01, 0x18, 0xb0, 0x33, 0x41, 0x00,
  513. 0x00, 0x21, 0x5c, 0x18, 0xa9 };
  514. data = opt->getData();
  515. ASSERT_EQ(data.size(), sizeof(expected_client_id));
  516. ASSERT_EQ(0, memcmp(&data[0], expected_client_id, data.size()));
  517. ASSERT_TRUE(opt = msg->getOption(D6O_IA_NA));
  518. boost::shared_ptr<Option6IA> ia =
  519. boost::dynamic_pointer_cast<Option6IA>(opt);
  520. ASSERT_TRUE(ia);
  521. EXPECT_EQ(1, ia->getIAID());
  522. EXPECT_EQ(0xffffffff, ia->getT1());
  523. EXPECT_EQ(0xffffffff, ia->getT2());
  524. ASSERT_TRUE(opt = msg->getOption(D6O_ELAPSED_TIME));
  525. EXPECT_EQ(6, opt->len()); // 2 bytes of data + 4 bytes of header
  526. boost::shared_ptr<OptionInt<uint16_t> > elapsed =
  527. boost::dynamic_pointer_cast<OptionInt<uint16_t> > (opt);
  528. ASSERT_TRUE(elapsed);
  529. EXPECT_EQ(0, elapsed->getValue());
  530. ASSERT_TRUE(opt = msg->getOption(D6O_ORO));
  531. boost::shared_ptr<OptionIntArray<uint16_t> > oro =
  532. boost::dynamic_pointer_cast<OptionIntArray<uint16_t> > (opt);
  533. const std::vector<uint16_t> oro_list = oro->getValues();
  534. EXPECT_EQ(3, oro_list.size());
  535. EXPECT_EQ(23, oro_list[0]);
  536. EXPECT_EQ(242, oro_list[1]);
  537. EXPECT_EQ(243, oro_list[2]);
  538. }
  539. // This test verified that message with relay information can be
  540. // packed and then unpacked.
  541. TEST_F(Pkt6Test, relayPack) {
  542. scoped_ptr<Pkt6> parent(new Pkt6(DHCPV6_ADVERTISE, 0x020304));
  543. Pkt6::RelayInfo relay1;
  544. relay1.msg_type_ = DHCPV6_RELAY_REPL;
  545. relay1.hop_count_ = 17; // not very miningful, but useful for testing
  546. relay1.linkaddr_ = IOAddress("2001:db8::1");
  547. relay1.peeraddr_ = IOAddress("fe80::abcd");
  548. uint8_t relay_opt_data[] = { 1, 2, 3, 4, 5, 6, 7, 8};
  549. vector<uint8_t> relay_data(relay_opt_data,
  550. relay_opt_data + sizeof(relay_opt_data));
  551. OptionPtr optRelay1(new Option(Option::V6, 200, relay_data));
  552. relay1.options_.insert(make_pair(optRelay1->getType(), optRelay1));
  553. OptionPtr opt1(new Option(Option::V6, 100));
  554. OptionPtr opt2(new Option(Option::V6, 101));
  555. OptionPtr opt3(new Option(Option::V6, 102));
  556. // Let's not use zero-length option type 3 as it is IA_NA
  557. parent->addRelayInfo(relay1);
  558. parent->addOption(opt1);
  559. parent->addOption(opt2);
  560. parent->addOption(opt3);
  561. EXPECT_EQ(DHCPV6_ADVERTISE, parent->getType());
  562. EXPECT_NO_THROW(parent->pack());
  563. EXPECT_EQ(Pkt6::DHCPV6_PKT_HDR_LEN + 3 * Option::OPTION6_HDR_LEN // ADVERTISE
  564. + Pkt6::DHCPV6_RELAY_HDR_LEN // Relay header
  565. + Option::OPTION6_HDR_LEN // Relay-msg
  566. + optRelay1->len(),
  567. parent->len());
  568. // Create second packet,based on assembled data from the first one
  569. scoped_ptr<Pkt6> clone(new Pkt6(static_cast<const uint8_t*>(
  570. parent->getBuffer().getData()),
  571. parent->getBuffer().getLength()));
  572. // Now recreate options list
  573. EXPECT_NO_THROW( clone->unpack() );
  574. // transid, message-type should be the same as before
  575. EXPECT_EQ(parent->getTransid(), parent->getTransid());
  576. EXPECT_EQ(DHCPV6_ADVERTISE, clone->getType());
  577. EXPECT_TRUE( clone->getOption(100));
  578. EXPECT_TRUE( clone->getOption(101));
  579. EXPECT_TRUE( clone->getOption(102));
  580. EXPECT_FALSE(clone->getOption(103));
  581. // Now check relay info
  582. ASSERT_EQ(1, clone->relay_info_.size());
  583. EXPECT_EQ(DHCPV6_RELAY_REPL, clone->relay_info_[0].msg_type_);
  584. EXPECT_EQ(17, clone->relay_info_[0].hop_count_);
  585. EXPECT_EQ("2001:db8::1", clone->relay_info_[0].linkaddr_.toText());
  586. EXPECT_EQ("fe80::abcd", clone->relay_info_[0].peeraddr_.toText());
  587. // There should be exactly one option
  588. EXPECT_EQ(1, clone->relay_info_[0].options_.size());
  589. OptionPtr opt = clone->getRelayOption(200, 0);
  590. EXPECT_TRUE(opt);
  591. EXPECT_EQ(opt->getType() , optRelay1->getType());
  592. EXPECT_EQ(opt->len(), optRelay1->len());
  593. OptionBuffer data = opt->getData();
  594. ASSERT_EQ(data.size(), sizeof(relay_opt_data));
  595. EXPECT_EQ(0, memcmp(relay_opt_data, relay_opt_data, sizeof(relay_opt_data)));
  596. }
  597. // This test verified that options added by relays to the message can be
  598. // accessed and retrieved properly
  599. TEST_F(Pkt6Test, getAnyRelayOption) {
  600. boost::scoped_ptr<Pkt6> msg(new Pkt6(DHCPV6_ADVERTISE, 0x020304));
  601. msg->addOption(generateRandomOption(300));
  602. // generate options for relay1
  603. Pkt6::RelayInfo relay1;
  604. // generate 3 options with code 200,201,202 and random content
  605. OptionPtr relay1_opt1(generateRandomOption(200));
  606. OptionPtr relay1_opt2(generateRandomOption(201));
  607. OptionPtr relay1_opt3(generateRandomOption(202));
  608. relay1.options_.insert(make_pair(200, relay1_opt1));
  609. relay1.options_.insert(make_pair(201, relay1_opt2));
  610. relay1.options_.insert(make_pair(202, relay1_opt3));
  611. msg->addRelayInfo(relay1);
  612. // generate options for relay2
  613. Pkt6::RelayInfo relay2;
  614. OptionPtr relay2_opt1(new Option(Option::V6, 100));
  615. OptionPtr relay2_opt2(new Option(Option::V6, 101));
  616. OptionPtr relay2_opt3(new Option(Option::V6, 102));
  617. OptionPtr relay2_opt4(new Option(Option::V6, 200)); // the same code as relay1_opt3
  618. relay2.options_.insert(make_pair(100, relay2_opt1));
  619. relay2.options_.insert(make_pair(101, relay2_opt2));
  620. relay2.options_.insert(make_pair(102, relay2_opt3));
  621. relay2.options_.insert(make_pair(200, relay2_opt4));
  622. msg->addRelayInfo(relay2);
  623. // generate options for relay3
  624. Pkt6::RelayInfo relay3;
  625. OptionPtr relay3_opt1(generateRandomOption(200, 7));
  626. relay3.options_.insert(make_pair(200, relay3_opt1));
  627. msg->addRelayInfo(relay3);
  628. // Ok, so we now have a packet that traversed the following network:
  629. // client---relay3---relay2---relay1---server
  630. // First check that the getAnyRelayOption does not confuse client options
  631. // and relay options
  632. // 300 is a client option, present in the message itself.
  633. OptionPtr opt = msg->getAnyRelayOption(300, Pkt6::RELAY_SEARCH_FROM_CLIENT);
  634. EXPECT_FALSE(opt);
  635. opt = msg->getAnyRelayOption(300, Pkt6::RELAY_SEARCH_FROM_SERVER);
  636. EXPECT_FALSE(opt);
  637. opt = msg->getAnyRelayOption(300, Pkt6::RELAY_GET_FIRST);
  638. EXPECT_FALSE(opt);
  639. opt = msg->getAnyRelayOption(300, Pkt6::RELAY_GET_LAST);
  640. EXPECT_FALSE(opt);
  641. // Option 200 is added in every relay.
  642. // We want to get that one inserted by relay3 (first match, starting from
  643. // closest to the client.
  644. opt = msg->getAnyRelayOption(200, Pkt6::RELAY_SEARCH_FROM_CLIENT);
  645. ASSERT_TRUE(opt);
  646. EXPECT_TRUE(opt->equal(relay3_opt1));
  647. // We want to ge that one inserted by relay1 (first match, starting from
  648. // closest to the server.
  649. opt = msg->getAnyRelayOption(200, Pkt6::RELAY_SEARCH_FROM_SERVER);
  650. ASSERT_TRUE(opt);
  651. EXPECT_TRUE(opt->equal(relay1_opt1));
  652. // We just want option from the first relay (closest to the client)
  653. opt = msg->getAnyRelayOption(200, Pkt6::RELAY_GET_FIRST);
  654. ASSERT_TRUE(opt);
  655. EXPECT_TRUE(opt->equal(relay3_opt1));
  656. // We just want option from the last relay (closest to the server)
  657. opt = msg->getAnyRelayOption(200, Pkt6::RELAY_GET_LAST);
  658. ASSERT_TRUE(opt);
  659. EXPECT_TRUE(opt->equal(relay1_opt1));
  660. // Let's try to ask for something that is inserted by the middle relay
  661. // only.
  662. opt = msg->getAnyRelayOption(100, Pkt6::RELAY_SEARCH_FROM_SERVER);
  663. ASSERT_TRUE(opt);
  664. EXPECT_TRUE(opt->equal(relay2_opt1));
  665. opt = msg->getAnyRelayOption(100, Pkt6::RELAY_SEARCH_FROM_CLIENT);
  666. ASSERT_TRUE(opt);
  667. EXPECT_TRUE(opt->equal(relay2_opt1));
  668. opt = msg->getAnyRelayOption(100, Pkt6::RELAY_GET_FIRST);
  669. EXPECT_FALSE(opt);
  670. opt = msg->getAnyRelayOption(100, Pkt6::RELAY_GET_LAST);
  671. EXPECT_FALSE(opt);
  672. // Finally, try to get an option that does not exist
  673. opt = msg->getAnyRelayOption(500, Pkt6::RELAY_GET_FIRST);
  674. EXPECT_FALSE(opt);
  675. opt = msg->getAnyRelayOption(500, Pkt6::RELAY_GET_LAST);
  676. EXPECT_FALSE(opt);
  677. opt = msg->getAnyRelayOption(500, Pkt6::RELAY_SEARCH_FROM_SERVER);
  678. EXPECT_FALSE(opt);
  679. opt = msg->getAnyRelayOption(500, Pkt6::RELAY_SEARCH_FROM_CLIENT);
  680. EXPECT_FALSE(opt);
  681. }
  682. // Tests whether a packet can be assigned to a class and later
  683. // checked if it belongs to a given class
  684. TEST_F(Pkt6Test, clientClasses) {
  685. Pkt6 pkt(DHCPV6_ADVERTISE, 1234);
  686. // Default values (do not belong to any class)
  687. EXPECT_FALSE(pkt.inClass(DOCSIS3_CLASS_EROUTER));
  688. EXPECT_FALSE(pkt.inClass(DOCSIS3_CLASS_MODEM));
  689. EXPECT_TRUE(pkt.classes_.empty());
  690. // Add to the first class
  691. pkt.addClass(DOCSIS3_CLASS_EROUTER);
  692. EXPECT_TRUE(pkt.inClass(DOCSIS3_CLASS_EROUTER));
  693. EXPECT_FALSE(pkt.inClass(DOCSIS3_CLASS_MODEM));
  694. ASSERT_FALSE(pkt.classes_.empty());
  695. // Add to a second class
  696. pkt.addClass(DOCSIS3_CLASS_MODEM);
  697. EXPECT_TRUE(pkt.inClass(DOCSIS3_CLASS_EROUTER));
  698. EXPECT_TRUE(pkt.inClass(DOCSIS3_CLASS_MODEM));
  699. // Check that it's ok to add to the same class repeatedly
  700. EXPECT_NO_THROW(pkt.addClass("foo"));
  701. EXPECT_NO_THROW(pkt.addClass("foo"));
  702. EXPECT_NO_THROW(pkt.addClass("foo"));
  703. // Check that the packet belongs to 'foo'
  704. EXPECT_TRUE(pkt.inClass("foo"));
  705. }
  706. // Tests whether MAC can be obtained and that MAC sources are not
  707. // confused.
  708. TEST_F(Pkt6Test, getMAC) {
  709. Pkt6 pkt(DHCPV6_ADVERTISE, 1234);
  710. // DHCPv6 packet by default doens't have MAC address specified.
  711. EXPECT_FALSE(pkt.getMAC(Pkt::HWADDR_SOURCE_ANY));
  712. EXPECT_FALSE(pkt.getMAC(Pkt::HWADDR_SOURCE_RAW));
  713. // We haven't specified source IPv6 address, so this method should fail, too
  714. EXPECT_FALSE(pkt.getMAC(Pkt::HWADDR_SOURCE_IPV6_LINK_LOCAL));
  715. // Let's check if setting IPv6 address improves the situation.
  716. IOAddress linklocal_eui64("fe80::204:06ff:fe08:0a0c");
  717. pkt.setRemoteAddr(linklocal_eui64);
  718. EXPECT_TRUE(pkt.getMAC(Pkt::HWADDR_SOURCE_ANY));
  719. EXPECT_TRUE(pkt.getMAC(Pkt::HWADDR_SOURCE_IPV6_LINK_LOCAL));
  720. EXPECT_TRUE(pkt.getMAC(Pkt::HWADDR_SOURCE_IPV6_LINK_LOCAL |
  721. Pkt::HWADDR_SOURCE_RAW));
  722. pkt.setRemoteAddr(IOAddress("::"));
  723. // Let's invent a MAC
  724. const uint8_t hw[] = { 2, 4, 6, 8, 10, 12 }; // MAC
  725. const uint8_t hw_type = 123; // hardware type
  726. HWAddrPtr dummy_hwaddr(new HWAddr(hw, sizeof(hw), hw_type));
  727. // Now let's pretend that we obtained it from raw sockets
  728. pkt.setRemoteHWAddr(dummy_hwaddr);
  729. // Now we should be able to get something
  730. ASSERT_TRUE(pkt.getMAC(Pkt::HWADDR_SOURCE_ANY));
  731. ASSERT_TRUE(pkt.getMAC(Pkt::HWADDR_SOURCE_RAW));
  732. EXPECT_TRUE(pkt.getMAC(Pkt::HWADDR_SOURCE_IPV6_LINK_LOCAL |
  733. Pkt::HWADDR_SOURCE_RAW));
  734. // Check that the returned MAC is indeed the expected one
  735. ASSERT_TRUE(*dummy_hwaddr == *pkt.getMAC(Pkt::HWADDR_SOURCE_ANY));
  736. ASSERT_TRUE(*dummy_hwaddr == *pkt.getMAC(Pkt::HWADDR_SOURCE_RAW));
  737. }
  738. // Test checks whether getMACFromIPv6LinkLocal() returns the hardware (MAC)
  739. // address properly (for direct message).
  740. TEST_F(Pkt6Test, getMACFromIPv6LinkLocal_direct) {
  741. Pkt6 pkt(DHCPV6_ADVERTISE, 1234);
  742. // Let's get the first interface
  743. Iface* iface = IfaceMgr::instance().getIface(1);
  744. ASSERT_TRUE(iface);
  745. // and set source interface data properly. getMACFromIPv6LinkLocal attempts
  746. // to use source interface to obtain hardware type
  747. pkt.setIface(iface->getName());
  748. pkt.setIndex(iface->getIndex());
  749. IOAddress global("2001:db8::204:06ff:fe08:0a:0c");
  750. IOAddress linklocal_eui64("fe80::204:06ff:fe08:0a0c");
  751. IOAddress linklocal_noneui64("fe80::0204:0608:0a0c:0e10");
  752. // If received from a global address, this method should fail
  753. pkt.setRemoteAddr(global);
  754. EXPECT_FALSE(pkt.getMAC(Pkt::HWADDR_SOURCE_IPV6_LINK_LOCAL));
  755. // If received from link-local that is EUI-64 based, it should succeed
  756. pkt.setRemoteAddr(linklocal_eui64);
  757. HWAddrPtr found = pkt.getMAC(Pkt::HWADDR_SOURCE_IPV6_LINK_LOCAL);
  758. ASSERT_TRUE(found);
  759. stringstream tmp;
  760. tmp << "hwtype=" << (int)iface->getHWType() << " 02:04:06:08:0a:0c";
  761. EXPECT_EQ(tmp.str(), found->toText(true));
  762. }
  763. // Test checks whether getMACFromIPv6LinkLocal() returns the hardware (MAC)
  764. // address properly (for relayed message).
  765. TEST_F(Pkt6Test, getMACFromIPv6LinkLocal_singleRelay) {
  766. // Let's create a Solicit first...
  767. Pkt6 pkt(DHCPV6_SOLICIT, 1234);
  768. // ... and pretend it was relayed by a single relay.
  769. Pkt6::RelayInfo info;
  770. pkt.addRelayInfo(info);
  771. ASSERT_EQ(1, pkt.relay_info_.size());
  772. // Let's get the first interface
  773. Iface* iface = IfaceMgr::instance().getIface(1);
  774. ASSERT_TRUE(iface);
  775. // and set source interface data properly. getMACFromIPv6LinkLocal attempts
  776. // to use source interface to obtain hardware type
  777. pkt.setIface(iface->getName());
  778. pkt.setIndex(iface->getIndex());
  779. IOAddress global("2001:db8::204:06ff:fe08:0a:0c"); // global address
  780. IOAddress linklocal_noneui64("fe80::0204:0608:0a0c:0e10"); // no fffe
  781. IOAddress linklocal_eui64("fe80::204:06ff:fe08:0a0c"); // valid EUI-64
  782. // If received from a global address, this method should fail
  783. pkt.relay_info_[0].peeraddr_ = global;
  784. EXPECT_FALSE(pkt.getMAC(Pkt::HWADDR_SOURCE_IPV6_LINK_LOCAL));
  785. // If received from a link-local that does not use EUI-64, it should fail
  786. pkt.relay_info_[0].peeraddr_ = linklocal_noneui64;
  787. EXPECT_FALSE(pkt.getMAC(Pkt::HWADDR_SOURCE_IPV6_LINK_LOCAL));
  788. // If received from link-local that is EUI-64 based, it should succeed
  789. pkt.relay_info_[0].peeraddr_ = linklocal_eui64;
  790. HWAddrPtr found = pkt.getMAC(Pkt::HWADDR_SOURCE_IPV6_LINK_LOCAL);
  791. ASSERT_TRUE(found);
  792. stringstream tmp;
  793. tmp << "hwtype=" << (int)iface->getHWType() << " 02:04:06:08:0a:0c";
  794. EXPECT_EQ(tmp.str(), found->toText(true));
  795. }
  796. // Test checks whether getMACFromIPv6LinkLocal() returns the hardware (MAC)
  797. // address properly (for a message relayed multiple times).
  798. TEST_F(Pkt6Test, getMACFromIPv6LinkLocal_multiRelay) {
  799. // Let's create a Solicit first...
  800. Pkt6 pkt(DHCPV6_SOLICIT, 1234);
  801. // ... and pretend it was relayed via 3 relays. Keep in mind that the relays
  802. // are stored in relay_info_ in the encapsulation order rather than in
  803. // traverse order. The following simulates:
  804. // client --- relay1 --- relay2 --- relay3 --- server
  805. IOAddress linklocal1("fe80::ff:fe00:1"); // valid EUI-64
  806. IOAddress linklocal2("fe80::ff:fe00:2"); // valid EUI-64
  807. IOAddress linklocal3("fe80::ff:fe00:3"); // valid EUI-64
  808. // Let's add info about relay3. This was the last relay, so it added the
  809. // outermost encapsulation layer, so it was parsed first during reception.
  810. // Its peer-addr field contains an address of relay2, so it's useless for
  811. // this method.
  812. Pkt6::RelayInfo info;
  813. info.peeraddr_ = linklocal3;
  814. pkt.addRelayInfo(info);
  815. // Now add info about relay2. Its peer-addr contains an address of the
  816. // previous relay (relay1). Still useless for us.
  817. info.peeraddr_ = linklocal2;
  818. pkt.addRelayInfo(info);
  819. // Finally add the first relay. This is the relay that received the packet
  820. // from the client directly, so its peer-addr field contains an address of
  821. // the client. The method should get that address and build MAC from it.
  822. info.peeraddr_ = linklocal1;
  823. pkt.addRelayInfo(info);
  824. ASSERT_EQ(3, pkt.relay_info_.size());
  825. // Let's get the first interface
  826. Iface* iface = IfaceMgr::instance().getIface(1);
  827. ASSERT_TRUE(iface);
  828. // and set source interface data properly. getMACFromIPv6LinkLocal attempts
  829. // to use source interface to obtain hardware type
  830. pkt.setIface(iface->getName());
  831. pkt.setIndex(iface->getIndex());
  832. // The method should return MAC based on the first relay that was closest
  833. HWAddrPtr found = pkt.getMAC(Pkt::HWADDR_SOURCE_IPV6_LINK_LOCAL);
  834. ASSERT_TRUE(found);
  835. // Let's check the info now.
  836. stringstream tmp;
  837. tmp << "hwtype=" << iface->getHWType() << " 00:00:00:00:00:01";
  838. EXPECT_EQ(tmp.str(), found->toText(true));
  839. }
  840. }