OLD | NEW |
| (Empty) |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "net/quic/quic_utils.h" | |
6 | |
7 #include "net/quic/crypto/crypto_protocol.h" | |
8 #include "net/quic/quic_flags.h" | |
9 #include "testing/gtest/include/gtest/gtest.h" | |
10 | |
11 using base::StringPiece; | |
12 using std::string; | |
13 | |
14 namespace net { | |
15 namespace test { | |
16 namespace { | |
17 | |
18 TEST(QuicUtilsTest, StreamErrorToString) { | |
19 EXPECT_STREQ("QUIC_BAD_APPLICATION_PAYLOAD", | |
20 QuicUtils::StreamErrorToString(QUIC_BAD_APPLICATION_PAYLOAD)); | |
21 } | |
22 | |
23 TEST(QuicUtilsTest, ErrorToString) { | |
24 EXPECT_STREQ("QUIC_NO_ERROR", QuicUtils::ErrorToString(QUIC_NO_ERROR)); | |
25 } | |
26 | |
27 TEST(QuicUtilsTest, TagToString) { | |
28 EXPECT_EQ("SCFG", QuicUtils::TagToString(kSCFG)); | |
29 EXPECT_EQ("SNO ", QuicUtils::TagToString(kServerNonceTag)); | |
30 EXPECT_EQ("CRT ", QuicUtils::TagToString(kCertificateTag)); | |
31 EXPECT_EQ("CHLO", QuicUtils::TagToString(MakeQuicTag('C', 'H', 'L', 'O'))); | |
32 // A tag that contains a non-printing character will be printed as a decimal | |
33 // number. | |
34 EXPECT_EQ("525092931", | |
35 QuicUtils::TagToString(MakeQuicTag('C', 'H', 'L', '\x1f'))); | |
36 } | |
37 | |
38 TEST(QuicUtilsTest, ParseQuicConnectionOptions) { | |
39 QuicTagVector empty_options = QuicUtils::ParseQuicConnectionOptions(""); | |
40 EXPECT_EQ(0ul, empty_options.size()); | |
41 | |
42 QuicTagVector parsed_options = | |
43 QuicUtils::ParseQuicConnectionOptions("TIMER,TBBR,REJ"); | |
44 QuicTagVector expected_options; | |
45 expected_options.push_back(kTIME); | |
46 expected_options.push_back(kTBBR); | |
47 expected_options.push_back(kREJ); | |
48 EXPECT_EQ(expected_options, parsed_options); | |
49 } | |
50 | |
51 TEST(QuicUtilsTest, DetermineAddressChangeType) { | |
52 const string kIPv4String1 = "1.2.3.4"; | |
53 const string kIPv4String2 = "1.2.3.5"; | |
54 const string kIPv4String3 = "1.1.3.5"; | |
55 const string kIPv6String1 = "2001:700:300:1800::f"; | |
56 const string kIPv6String2 = "2001:700:300:1800:1:1:1:f"; | |
57 IPEndPoint old_address; | |
58 IPEndPoint new_address; | |
59 IPAddress address; | |
60 | |
61 EXPECT_EQ(NO_CHANGE, | |
62 QuicUtils::DetermineAddressChangeType(old_address, new_address)); | |
63 ASSERT_TRUE(address.AssignFromIPLiteral(kIPv4String1)); | |
64 old_address = IPEndPoint(address, 1234); | |
65 EXPECT_EQ(NO_CHANGE, | |
66 QuicUtils::DetermineAddressChangeType(old_address, new_address)); | |
67 new_address = IPEndPoint(address, 1234); | |
68 EXPECT_EQ(NO_CHANGE, | |
69 QuicUtils::DetermineAddressChangeType(old_address, new_address)); | |
70 | |
71 new_address = IPEndPoint(address, 5678); | |
72 EXPECT_EQ(PORT_CHANGE, | |
73 QuicUtils::DetermineAddressChangeType(old_address, new_address)); | |
74 ASSERT_TRUE(address.AssignFromIPLiteral(kIPv6String1)); | |
75 old_address = IPEndPoint(address, 1234); | |
76 new_address = IPEndPoint(address, 5678); | |
77 EXPECT_EQ(PORT_CHANGE, | |
78 QuicUtils::DetermineAddressChangeType(old_address, new_address)); | |
79 | |
80 ASSERT_TRUE(address.AssignFromIPLiteral(kIPv4String1)); | |
81 old_address = IPEndPoint(address, 1234); | |
82 ASSERT_TRUE(address.AssignFromIPLiteral(kIPv6String1)); | |
83 new_address = IPEndPoint(address, 1234); | |
84 EXPECT_EQ(IPV4_TO_IPV6_CHANGE, | |
85 QuicUtils::DetermineAddressChangeType(old_address, new_address)); | |
86 | |
87 old_address = IPEndPoint(address, 1234); | |
88 ASSERT_TRUE(address.AssignFromIPLiteral(kIPv4String1)); | |
89 new_address = IPEndPoint(address, 1234); | |
90 EXPECT_EQ(IPV6_TO_IPV4_CHANGE, | |
91 QuicUtils::DetermineAddressChangeType(old_address, new_address)); | |
92 | |
93 ASSERT_TRUE(address.AssignFromIPLiteral(kIPv6String2)); | |
94 new_address = IPEndPoint(address, 1234); | |
95 EXPECT_EQ(IPV6_TO_IPV6_CHANGE, | |
96 QuicUtils::DetermineAddressChangeType(old_address, new_address)); | |
97 | |
98 ASSERT_TRUE(address.AssignFromIPLiteral(kIPv4String1)); | |
99 old_address = IPEndPoint(address, 1234); | |
100 ASSERT_TRUE(address.AssignFromIPLiteral(kIPv4String2)); | |
101 new_address = IPEndPoint(address, 1234); | |
102 EXPECT_EQ(IPV4_SUBNET_CHANGE, | |
103 QuicUtils::DetermineAddressChangeType(old_address, new_address)); | |
104 ASSERT_TRUE(address.AssignFromIPLiteral(kIPv4String3)); | |
105 new_address = IPEndPoint(address, 1234); | |
106 EXPECT_EQ(UNSPECIFIED_CHANGE, | |
107 QuicUtils::DetermineAddressChangeType(old_address, new_address)); | |
108 } | |
109 | |
110 uint128 IncrementalHashReference(const void* data, size_t len) { | |
111 // The two constants are defined as part of the hash algorithm. | |
112 // see http://www.isthe.com/chongo/tech/comp/fnv/ | |
113 // hash = 144066263297769815596495629667062367629 | |
114 uint128 hash = | |
115 uint128(UINT64_C(7809847782465536322), UINT64_C(7113472399480571277)); | |
116 // kPrime = 309485009821345068724781371 | |
117 const uint128 kPrime(16777216, 315); | |
118 const uint8_t* octets = reinterpret_cast<const uint8_t*>(data); | |
119 for (size_t i = 0; i < len; ++i) { | |
120 hash = hash ^ uint128(0, octets[i]); | |
121 hash = hash * kPrime; | |
122 } | |
123 return hash; | |
124 } | |
125 | |
126 TEST(QuicUtilsHashTest, ReferenceTest) { | |
127 std::vector<uint8_t> data(32); | |
128 for (size_t i = 0; i < data.size(); ++i) { | |
129 data[i] = i % 255; | |
130 } | |
131 EXPECT_EQ(IncrementalHashReference(data.data(), data.size()), | |
132 QuicUtils::FNV1a_128_Hash( | |
133 reinterpret_cast<const char*>(data.data()), data.size())); | |
134 } | |
135 | |
136 TEST(QuicUtilsTest, HexDump) { | |
137 // Verify output of the HexDump method is as expected. | |
138 char packet[] = { | |
139 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x2c, 0x20, 0x51, 0x55, 0x49, 0x43, 0x21, | |
140 0x20, 0x54, 0x68, 0x69, 0x73, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, | |
141 0x20, 0x73, 0x68, 0x6f, 0x75, 0x6c, 0x64, 0x20, 0x62, 0x65, 0x20, 0x6c, | |
142 0x6f, 0x6e, 0x67, 0x20, 0x65, 0x6e, 0x6f, 0x75, 0x67, 0x68, 0x20, 0x74, | |
143 0x6f, 0x20, 0x73, 0x70, 0x61, 0x6e, 0x20, 0x6d, 0x75, 0x6c, 0x74, 0x69, | |
144 0x70, 0x6c, 0x65, 0x20, 0x6c, 0x69, 0x6e, 0x65, 0x73, 0x20, 0x6f, 0x66, | |
145 0x20, 0x6f, 0x75, 0x74, 0x70, 0x75, 0x74, 0x2e, 0x01, 0x02, 0x03, 0x00, | |
146 }; | |
147 EXPECT_EQ( | |
148 QuicUtils::HexDump(packet), | |
149 "0x0000: 4865 6c6c 6f2c 2051 5549 4321 2054 6869 Hello,.QUIC!.Thi\n" | |
150 "0x0010: 7320 7374 7269 6e67 2073 686f 756c 6420 s.string.should.\n" | |
151 "0x0020: 6265 206c 6f6e 6720 656e 6f75 6768 2074 be.long.enough.t\n" | |
152 "0x0030: 6f20 7370 616e 206d 756c 7469 706c 6520 o.span.multiple.\n" | |
153 "0x0040: 6c69 6e65 7320 6f66 206f 7574 7075 742e lines.of.output.\n" | |
154 "0x0050: 0102 03 ...\n"); | |
155 } | |
156 | |
157 } // namespace | |
158 } // namespace test | |
159 } // namespace net | |
OLD | NEW |