blob: baaa3ae75182f613316545104acc9f403b28e98d [file] [log] [blame]
QUICHE teama6ef0a62019-03-07 20:34:33 -05001// 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/third_party/quiche/src/quic/core/quic_utils.h"
6
vasilvv872e7a32019-03-12 16:42:44 -07007#include <string>
8
QUICHE teama6ef0a62019-03-07 20:34:33 -05009#include "net/third_party/quiche/src/quic/core/crypto/crypto_protocol.h"
10#include "net/third_party/quiche/src/quic/core/quic_connection_id.h"
QUICHE teama6ef0a62019-03-07 20:34:33 -050011#include "net/third_party/quiche/src/quic/platform/api/quic_test.h"
12#include "net/third_party/quiche/src/quic/test_tools/quic_test_utils.h"
bnc4e9283d2019-12-17 07:08:57 -080013#include "net/third_party/quiche/src/common/platform/api/quiche_arraysize.h"
dmcardlecf0bfcf2019-12-13 08:08:21 -080014#include "net/third_party/quiche/src/common/platform/api/quiche_string_piece.h"
QUICHE teama6ef0a62019-03-07 20:34:33 -050015
16namespace quic {
17namespace test {
18namespace {
19
20class QuicUtilsTest : public QuicTest {};
21
22TEST_F(QuicUtilsTest, DetermineAddressChangeType) {
vasilvvc48c8712019-03-11 13:38:16 -070023 const std::string kIPv4String1 = "1.2.3.4";
24 const std::string kIPv4String2 = "1.2.3.5";
25 const std::string kIPv4String3 = "1.1.3.5";
26 const std::string kIPv6String1 = "2001:700:300:1800::f";
27 const std::string kIPv6String2 = "2001:700:300:1800:1:1:1:f";
QUICHE teama6ef0a62019-03-07 20:34:33 -050028 QuicSocketAddress old_address;
29 QuicSocketAddress new_address;
30 QuicIpAddress address;
31
32 EXPECT_EQ(NO_CHANGE,
33 QuicUtils::DetermineAddressChangeType(old_address, new_address));
34 ASSERT_TRUE(address.FromString(kIPv4String1));
35 old_address = QuicSocketAddress(address, 1234);
36 EXPECT_EQ(NO_CHANGE,
37 QuicUtils::DetermineAddressChangeType(old_address, new_address));
38 new_address = QuicSocketAddress(address, 1234);
39 EXPECT_EQ(NO_CHANGE,
40 QuicUtils::DetermineAddressChangeType(old_address, new_address));
41
42 new_address = QuicSocketAddress(address, 5678);
43 EXPECT_EQ(PORT_CHANGE,
44 QuicUtils::DetermineAddressChangeType(old_address, new_address));
45 ASSERT_TRUE(address.FromString(kIPv6String1));
46 old_address = QuicSocketAddress(address, 1234);
47 new_address = QuicSocketAddress(address, 5678);
48 EXPECT_EQ(PORT_CHANGE,
49 QuicUtils::DetermineAddressChangeType(old_address, new_address));
50
51 ASSERT_TRUE(address.FromString(kIPv4String1));
52 old_address = QuicSocketAddress(address, 1234);
53 ASSERT_TRUE(address.FromString(kIPv6String1));
54 new_address = QuicSocketAddress(address, 1234);
55 EXPECT_EQ(IPV4_TO_IPV6_CHANGE,
56 QuicUtils::DetermineAddressChangeType(old_address, new_address));
57
58 old_address = QuicSocketAddress(address, 1234);
59 ASSERT_TRUE(address.FromString(kIPv4String1));
60 new_address = QuicSocketAddress(address, 1234);
61 EXPECT_EQ(IPV6_TO_IPV4_CHANGE,
62 QuicUtils::DetermineAddressChangeType(old_address, new_address));
63
64 ASSERT_TRUE(address.FromString(kIPv6String2));
65 new_address = QuicSocketAddress(address, 1234);
66 EXPECT_EQ(IPV6_TO_IPV6_CHANGE,
67 QuicUtils::DetermineAddressChangeType(old_address, new_address));
68
69 ASSERT_TRUE(address.FromString(kIPv4String1));
70 old_address = QuicSocketAddress(address, 1234);
71 ASSERT_TRUE(address.FromString(kIPv4String2));
72 new_address = QuicSocketAddress(address, 1234);
73 EXPECT_EQ(IPV4_SUBNET_CHANGE,
74 QuicUtils::DetermineAddressChangeType(old_address, new_address));
75 ASSERT_TRUE(address.FromString(kIPv4String3));
76 new_address = QuicSocketAddress(address, 1234);
77 EXPECT_EQ(IPV4_TO_IPV4_CHANGE,
78 QuicUtils::DetermineAddressChangeType(old_address, new_address));
79}
80
81QuicUint128 IncrementalHashReference(const void* data, size_t len) {
82 // The two constants are defined as part of the hash algorithm.
83 // see http://www.isthe.com/chongo/tech/comp/fnv/
84 // hash = 144066263297769815596495629667062367629
85 QuicUint128 hash = MakeQuicUint128(UINT64_C(7809847782465536322),
86 UINT64_C(7113472399480571277));
87 // kPrime = 309485009821345068724781371
88 const QuicUint128 kPrime = MakeQuicUint128(16777216, 315);
89 const uint8_t* octets = reinterpret_cast<const uint8_t*>(data);
90 for (size_t i = 0; i < len; ++i) {
91 hash = hash ^ MakeQuicUint128(0, octets[i]);
92 hash = hash * kPrime;
93 }
94 return hash;
95}
96
97TEST_F(QuicUtilsTest, ReferenceTest) {
98 std::vector<uint8_t> data(32);
99 for (size_t i = 0; i < data.size(); ++i) {
100 data[i] = i % 255;
101 }
102 EXPECT_EQ(IncrementalHashReference(data.data(), data.size()),
dmcardlecf0bfcf2019-12-13 08:08:21 -0800103 QuicUtils::FNV1a_128_Hash(quiche::QuicheStringPiece(
QUICHE teama6ef0a62019-03-07 20:34:33 -0500104 reinterpret_cast<const char*>(data.data()), data.size())));
105}
106
107TEST_F(QuicUtilsTest, IsUnackable) {
108 for (size_t i = FIRST_PACKET_STATE; i <= LAST_PACKET_STATE; ++i) {
109 if (i == NEVER_SENT || i == ACKED || i == UNACKABLE) {
110 EXPECT_FALSE(QuicUtils::IsAckable(static_cast<SentPacketState>(i)));
111 } else {
112 EXPECT_TRUE(QuicUtils::IsAckable(static_cast<SentPacketState>(i)));
113 }
114 }
115}
116
117TEST_F(QuicUtilsTest, RetransmissionTypeToPacketState) {
118 for (size_t i = FIRST_TRANSMISSION_TYPE; i <= LAST_TRANSMISSION_TYPE; ++i) {
119 if (i == NOT_RETRANSMISSION) {
120 continue;
121 }
122 SentPacketState state = QuicUtils::RetransmissionTypeToPacketState(
123 static_cast<TransmissionType>(i));
124 if (i == HANDSHAKE_RETRANSMISSION) {
125 EXPECT_EQ(HANDSHAKE_RETRANSMITTED, state);
126 } else if (i == LOSS_RETRANSMISSION) {
127 EXPECT_EQ(LOST, state);
128 } else if (i == ALL_UNACKED_RETRANSMISSION ||
129 i == ALL_INITIAL_RETRANSMISSION) {
130 EXPECT_EQ(UNACKABLE, state);
131 } else if (i == TLP_RETRANSMISSION) {
132 EXPECT_EQ(TLP_RETRANSMITTED, state);
133 } else if (i == RTO_RETRANSMISSION) {
134 EXPECT_EQ(RTO_RETRANSMITTED, state);
fayang73d0ac42019-10-31 12:45:31 -0700135 } else if (i == PTO_RETRANSMISSION) {
136 EXPECT_EQ(PTO_RETRANSMITTED, state);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500137 } else if (i == PROBING_RETRANSMISSION) {
138 EXPECT_EQ(PROBE_RETRANSMITTED, state);
139 } else {
140 DCHECK(false)
141 << "No corresponding packet state according to transmission type: "
142 << i;
143 }
144 }
145}
146
147TEST_F(QuicUtilsTest, IsIetfPacketHeader) {
148 // IETF QUIC short header
149 uint8_t first_byte = 0;
150 EXPECT_TRUE(QuicUtils::IsIetfPacketHeader(first_byte));
151 EXPECT_TRUE(QuicUtils::IsIetfPacketShortHeader(first_byte));
152
153 // IETF QUIC long header
154 first_byte |= (FLAGS_LONG_HEADER | FLAGS_DEMULTIPLEXING_BIT);
155 EXPECT_TRUE(QuicUtils::IsIetfPacketHeader(first_byte));
156 EXPECT_FALSE(QuicUtils::IsIetfPacketShortHeader(first_byte));
157
158 // IETF QUIC long header, version negotiation.
159 first_byte = 0;
160 first_byte |= FLAGS_LONG_HEADER;
161 EXPECT_TRUE(QuicUtils::IsIetfPacketHeader(first_byte));
162 EXPECT_FALSE(QuicUtils::IsIetfPacketShortHeader(first_byte));
163
164 // GQUIC
165 first_byte = 0;
166 first_byte |= PACKET_PUBLIC_FLAGS_8BYTE_CONNECTION_ID;
167 EXPECT_FALSE(QuicUtils::IsIetfPacketHeader(first_byte));
168 EXPECT_FALSE(QuicUtils::IsIetfPacketShortHeader(first_byte));
169}
170
dschinaziadc75072019-08-19 10:54:45 -0700171TEST_F(QuicUtilsTest, ReplacementConnectionIdIsDeterministic) {
172 // Verify that two equal connection IDs get the same replacement.
173 QuicConnectionId connection_id64a = TestConnectionId(33);
174 QuicConnectionId connection_id64b = TestConnectionId(33);
175 EXPECT_EQ(connection_id64a, connection_id64b);
176 EXPECT_EQ(QuicUtils::CreateReplacementConnectionId(connection_id64a),
177 QuicUtils::CreateReplacementConnectionId(connection_id64b));
178 QuicConnectionId connection_id72a = TestConnectionIdNineBytesLong(42);
179 QuicConnectionId connection_id72b = TestConnectionIdNineBytesLong(42);
180 EXPECT_EQ(connection_id72a, connection_id72b);
181 EXPECT_EQ(QuicUtils::CreateReplacementConnectionId(connection_id72a),
182 QuicUtils::CreateReplacementConnectionId(connection_id72b));
183}
184
185TEST_F(QuicUtilsTest, ReplacementConnectionIdLengthIsCorrect) {
186 // Verify that all lengths get replaced by kQuicDefaultConnectionIdLength.
dschinaziffa83552019-12-17 11:00:23 -0800187 SetQuicRestartFlag(quic_allow_very_long_connection_ids, true);
188 const char connection_id_bytes[255] = {};
dschinaziadc75072019-08-19 10:54:45 -0700189 for (uint8_t i = 0; i < sizeof(connection_id_bytes) - 1; ++i) {
190 QuicConnectionId connection_id(connection_id_bytes, i);
191 QuicConnectionId replacement_connection_id =
192 QuicUtils::CreateReplacementConnectionId(connection_id);
193 EXPECT_EQ(kQuicDefaultConnectionIdLength,
194 replacement_connection_id.length());
195 }
196}
197
198TEST_F(QuicUtilsTest, ReplacementConnectionIdHasEntropy) {
199 // Make sure all these test connection IDs have different replacements.
200 for (uint64_t i = 0; i < 256; ++i) {
201 QuicConnectionId connection_id_i = TestConnectionId(i);
202 EXPECT_NE(connection_id_i,
203 QuicUtils::CreateReplacementConnectionId(connection_id_i));
204 for (uint64_t j = i + 1; j <= 256; ++j) {
205 QuicConnectionId connection_id_j = TestConnectionId(j);
206 EXPECT_NE(connection_id_i, connection_id_j);
207 EXPECT_NE(QuicUtils::CreateReplacementConnectionId(connection_id_i),
208 QuicUtils::CreateReplacementConnectionId(connection_id_j));
209 }
210 }
211}
212
QUICHE teama6ef0a62019-03-07 20:34:33 -0500213TEST_F(QuicUtilsTest, RandomConnectionId) {
214 MockRandom random(33);
215 QuicConnectionId connection_id = QuicUtils::CreateRandomConnectionId(&random);
216 EXPECT_EQ(connection_id.length(), sizeof(uint64_t));
vasilvvc48c8712019-03-11 13:38:16 -0700217 char connection_id_bytes[sizeof(uint64_t)];
bnc4e9283d2019-12-17 07:08:57 -0800218 random.RandBytes(connection_id_bytes, QUICHE_ARRAYSIZE(connection_id_bytes));
vasilvvc48c8712019-03-11 13:38:16 -0700219 EXPECT_EQ(connection_id,
220 QuicConnectionId(static_cast<char*>(connection_id_bytes),
bnc4e9283d2019-12-17 07:08:57 -0800221 QUICHE_ARRAYSIZE(connection_id_bytes)));
QUICHE teama6ef0a62019-03-07 20:34:33 -0500222 EXPECT_NE(connection_id, EmptyQuicConnectionId());
223 EXPECT_NE(connection_id, TestConnectionId());
224 EXPECT_NE(connection_id, TestConnectionId(1));
QUICHE teamc65d1d12019-03-19 20:58:04 -0700225 EXPECT_NE(connection_id, TestConnectionIdNineBytesLong(1));
226 EXPECT_EQ(QuicUtils::CreateRandomConnectionId().length(),
227 kQuicDefaultConnectionIdLength);
228}
229
230TEST_F(QuicUtilsTest, RandomConnectionIdVariableLength) {
231 MockRandom random(1337);
232 const uint8_t connection_id_length = 9;
233 QuicConnectionId connection_id =
234 QuicUtils::CreateRandomConnectionId(connection_id_length, &random);
235 EXPECT_EQ(connection_id.length(), connection_id_length);
236 char connection_id_bytes[connection_id_length];
bnc4e9283d2019-12-17 07:08:57 -0800237 random.RandBytes(connection_id_bytes, QUICHE_ARRAYSIZE(connection_id_bytes));
QUICHE teamc65d1d12019-03-19 20:58:04 -0700238 EXPECT_EQ(connection_id,
239 QuicConnectionId(static_cast<char*>(connection_id_bytes),
bnc4e9283d2019-12-17 07:08:57 -0800240 QUICHE_ARRAYSIZE(connection_id_bytes)));
QUICHE teamc65d1d12019-03-19 20:58:04 -0700241 EXPECT_NE(connection_id, EmptyQuicConnectionId());
242 EXPECT_NE(connection_id, TestConnectionId());
243 EXPECT_NE(connection_id, TestConnectionId(1));
244 EXPECT_NE(connection_id, TestConnectionIdNineBytesLong(1));
245 EXPECT_EQ(QuicUtils::CreateRandomConnectionId(connection_id_length).length(),
246 connection_id_length);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500247}
248
249TEST_F(QuicUtilsTest, VariableLengthConnectionId) {
250 EXPECT_FALSE(
fayang8265a2a2019-10-16 11:23:51 -0700251 QuicUtils::VariableLengthConnectionIdAllowedForVersion(QUIC_VERSION_43));
QUICHE teama6ef0a62019-03-07 20:34:33 -0500252 EXPECT_TRUE(QuicUtils::IsConnectionIdValidForVersion(
fayang8265a2a2019-10-16 11:23:51 -0700253 QuicUtils::CreateZeroConnectionId(QUIC_VERSION_43), QUIC_VERSION_43));
QUICHE teama6ef0a62019-03-07 20:34:33 -0500254 EXPECT_TRUE(QuicUtils::IsConnectionIdValidForVersion(
255 QuicUtils::CreateZeroConnectionId(QUIC_VERSION_99), QUIC_VERSION_99));
fayang8265a2a2019-10-16 11:23:51 -0700256 EXPECT_NE(QuicUtils::CreateZeroConnectionId(QUIC_VERSION_43),
QUICHE teama6ef0a62019-03-07 20:34:33 -0500257 EmptyQuicConnectionId());
QUICHE team8e2e4532019-03-14 14:37:56 -0700258 EXPECT_EQ(QuicUtils::CreateZeroConnectionId(QUIC_VERSION_99),
259 EmptyQuicConnectionId());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500260 EXPECT_FALSE(QuicUtils::IsConnectionIdValidForVersion(EmptyQuicConnectionId(),
fayang8265a2a2019-10-16 11:23:51 -0700261 QUIC_VERSION_43));
QUICHE teama6ef0a62019-03-07 20:34:33 -0500262}
263
264TEST_F(QuicUtilsTest, StatelessResetToken) {
265 QuicConnectionId connection_id1a = test::TestConnectionId(1);
266 QuicConnectionId connection_id1b = test::TestConnectionId(1);
267 QuicConnectionId connection_id2 = test::TestConnectionId(2);
268 QuicUint128 token1a = QuicUtils::GenerateStatelessResetToken(connection_id1a);
269 QuicUint128 token1b = QuicUtils::GenerateStatelessResetToken(connection_id1b);
270 QuicUint128 token2 = QuicUtils::GenerateStatelessResetToken(connection_id2);
271 EXPECT_EQ(token1a, token1b);
272 EXPECT_NE(token1a, token2);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500273}
274
275} // namespace
276} // namespace test
277} // namespace quic