pkt6_unittest.cc 63 KB

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