QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 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/third_party/quiche/src/quic/core/quic_utils.h" |
| 6 | |
vasilvv | 872e7a3 | 2019-03-12 16:42:44 -0700 | [diff] [blame] | 7 | #include <string> |
| 8 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 9 | #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" |
| 11 | #include "net/third_party/quiche/src/quic/platform/api/quic_arraysize.h" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 12 | #include "net/third_party/quiche/src/quic/platform/api/quic_test.h" |
| 13 | #include "net/third_party/quiche/src/quic/test_tools/quic_test_utils.h" |
| 14 | |
| 15 | namespace quic { |
| 16 | namespace test { |
| 17 | namespace { |
| 18 | |
| 19 | class QuicUtilsTest : public QuicTest {}; |
| 20 | |
| 21 | TEST_F(QuicUtilsTest, DetermineAddressChangeType) { |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 22 | const std::string kIPv4String1 = "1.2.3.4"; |
| 23 | const std::string kIPv4String2 = "1.2.3.5"; |
| 24 | const std::string kIPv4String3 = "1.1.3.5"; |
| 25 | const std::string kIPv6String1 = "2001:700:300:1800::f"; |
| 26 | const std::string kIPv6String2 = "2001:700:300:1800:1:1:1:f"; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 27 | QuicSocketAddress old_address; |
| 28 | QuicSocketAddress new_address; |
| 29 | QuicIpAddress address; |
| 30 | |
| 31 | EXPECT_EQ(NO_CHANGE, |
| 32 | QuicUtils::DetermineAddressChangeType(old_address, new_address)); |
| 33 | ASSERT_TRUE(address.FromString(kIPv4String1)); |
| 34 | old_address = QuicSocketAddress(address, 1234); |
| 35 | EXPECT_EQ(NO_CHANGE, |
| 36 | QuicUtils::DetermineAddressChangeType(old_address, new_address)); |
| 37 | new_address = QuicSocketAddress(address, 1234); |
| 38 | EXPECT_EQ(NO_CHANGE, |
| 39 | QuicUtils::DetermineAddressChangeType(old_address, new_address)); |
| 40 | |
| 41 | new_address = QuicSocketAddress(address, 5678); |
| 42 | EXPECT_EQ(PORT_CHANGE, |
| 43 | QuicUtils::DetermineAddressChangeType(old_address, new_address)); |
| 44 | ASSERT_TRUE(address.FromString(kIPv6String1)); |
| 45 | old_address = QuicSocketAddress(address, 1234); |
| 46 | new_address = QuicSocketAddress(address, 5678); |
| 47 | EXPECT_EQ(PORT_CHANGE, |
| 48 | QuicUtils::DetermineAddressChangeType(old_address, new_address)); |
| 49 | |
| 50 | ASSERT_TRUE(address.FromString(kIPv4String1)); |
| 51 | old_address = QuicSocketAddress(address, 1234); |
| 52 | ASSERT_TRUE(address.FromString(kIPv6String1)); |
| 53 | new_address = QuicSocketAddress(address, 1234); |
| 54 | EXPECT_EQ(IPV4_TO_IPV6_CHANGE, |
| 55 | QuicUtils::DetermineAddressChangeType(old_address, new_address)); |
| 56 | |
| 57 | old_address = QuicSocketAddress(address, 1234); |
| 58 | ASSERT_TRUE(address.FromString(kIPv4String1)); |
| 59 | new_address = QuicSocketAddress(address, 1234); |
| 60 | EXPECT_EQ(IPV6_TO_IPV4_CHANGE, |
| 61 | QuicUtils::DetermineAddressChangeType(old_address, new_address)); |
| 62 | |
| 63 | ASSERT_TRUE(address.FromString(kIPv6String2)); |
| 64 | new_address = QuicSocketAddress(address, 1234); |
| 65 | EXPECT_EQ(IPV6_TO_IPV6_CHANGE, |
| 66 | QuicUtils::DetermineAddressChangeType(old_address, new_address)); |
| 67 | |
| 68 | ASSERT_TRUE(address.FromString(kIPv4String1)); |
| 69 | old_address = QuicSocketAddress(address, 1234); |
| 70 | ASSERT_TRUE(address.FromString(kIPv4String2)); |
| 71 | new_address = QuicSocketAddress(address, 1234); |
| 72 | EXPECT_EQ(IPV4_SUBNET_CHANGE, |
| 73 | QuicUtils::DetermineAddressChangeType(old_address, new_address)); |
| 74 | ASSERT_TRUE(address.FromString(kIPv4String3)); |
| 75 | new_address = QuicSocketAddress(address, 1234); |
| 76 | EXPECT_EQ(IPV4_TO_IPV4_CHANGE, |
| 77 | QuicUtils::DetermineAddressChangeType(old_address, new_address)); |
| 78 | } |
| 79 | |
| 80 | QuicUint128 IncrementalHashReference(const void* data, size_t len) { |
| 81 | // The two constants are defined as part of the hash algorithm. |
| 82 | // see http://www.isthe.com/chongo/tech/comp/fnv/ |
| 83 | // hash = 144066263297769815596495629667062367629 |
| 84 | QuicUint128 hash = MakeQuicUint128(UINT64_C(7809847782465536322), |
| 85 | UINT64_C(7113472399480571277)); |
| 86 | // kPrime = 309485009821345068724781371 |
| 87 | const QuicUint128 kPrime = MakeQuicUint128(16777216, 315); |
| 88 | const uint8_t* octets = reinterpret_cast<const uint8_t*>(data); |
| 89 | for (size_t i = 0; i < len; ++i) { |
| 90 | hash = hash ^ MakeQuicUint128(0, octets[i]); |
| 91 | hash = hash * kPrime; |
| 92 | } |
| 93 | return hash; |
| 94 | } |
| 95 | |
| 96 | TEST_F(QuicUtilsTest, ReferenceTest) { |
| 97 | std::vector<uint8_t> data(32); |
| 98 | for (size_t i = 0; i < data.size(); ++i) { |
| 99 | data[i] = i % 255; |
| 100 | } |
| 101 | EXPECT_EQ(IncrementalHashReference(data.data(), data.size()), |
| 102 | QuicUtils::FNV1a_128_Hash(QuicStringPiece( |
| 103 | reinterpret_cast<const char*>(data.data()), data.size()))); |
| 104 | } |
| 105 | |
| 106 | TEST_F(QuicUtilsTest, IsUnackable) { |
| 107 | for (size_t i = FIRST_PACKET_STATE; i <= LAST_PACKET_STATE; ++i) { |
| 108 | if (i == NEVER_SENT || i == ACKED || i == UNACKABLE) { |
| 109 | EXPECT_FALSE(QuicUtils::IsAckable(static_cast<SentPacketState>(i))); |
| 110 | } else { |
| 111 | EXPECT_TRUE(QuicUtils::IsAckable(static_cast<SentPacketState>(i))); |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | TEST_F(QuicUtilsTest, RetransmissionTypeToPacketState) { |
| 117 | for (size_t i = FIRST_TRANSMISSION_TYPE; i <= LAST_TRANSMISSION_TYPE; ++i) { |
| 118 | if (i == NOT_RETRANSMISSION) { |
| 119 | continue; |
| 120 | } |
| 121 | SentPacketState state = QuicUtils::RetransmissionTypeToPacketState( |
| 122 | static_cast<TransmissionType>(i)); |
| 123 | if (i == HANDSHAKE_RETRANSMISSION) { |
| 124 | EXPECT_EQ(HANDSHAKE_RETRANSMITTED, state); |
| 125 | } else if (i == LOSS_RETRANSMISSION) { |
| 126 | EXPECT_EQ(LOST, state); |
| 127 | } else if (i == ALL_UNACKED_RETRANSMISSION || |
| 128 | i == ALL_INITIAL_RETRANSMISSION) { |
| 129 | EXPECT_EQ(UNACKABLE, state); |
| 130 | } else if (i == TLP_RETRANSMISSION) { |
| 131 | EXPECT_EQ(TLP_RETRANSMITTED, state); |
| 132 | } else if (i == RTO_RETRANSMISSION) { |
| 133 | EXPECT_EQ(RTO_RETRANSMITTED, state); |
| 134 | } else if (i == PROBING_RETRANSMISSION) { |
| 135 | EXPECT_EQ(PROBE_RETRANSMITTED, state); |
| 136 | } else { |
| 137 | DCHECK(false) |
| 138 | << "No corresponding packet state according to transmission type: " |
| 139 | << i; |
| 140 | } |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | TEST_F(QuicUtilsTest, IsIetfPacketHeader) { |
| 145 | // IETF QUIC short header |
| 146 | uint8_t first_byte = 0; |
| 147 | EXPECT_TRUE(QuicUtils::IsIetfPacketHeader(first_byte)); |
| 148 | EXPECT_TRUE(QuicUtils::IsIetfPacketShortHeader(first_byte)); |
| 149 | |
| 150 | // IETF QUIC long header |
| 151 | first_byte |= (FLAGS_LONG_HEADER | FLAGS_DEMULTIPLEXING_BIT); |
| 152 | EXPECT_TRUE(QuicUtils::IsIetfPacketHeader(first_byte)); |
| 153 | EXPECT_FALSE(QuicUtils::IsIetfPacketShortHeader(first_byte)); |
| 154 | |
| 155 | // IETF QUIC long header, version negotiation. |
| 156 | first_byte = 0; |
| 157 | first_byte |= FLAGS_LONG_HEADER; |
| 158 | EXPECT_TRUE(QuicUtils::IsIetfPacketHeader(first_byte)); |
| 159 | EXPECT_FALSE(QuicUtils::IsIetfPacketShortHeader(first_byte)); |
| 160 | |
| 161 | // GQUIC |
| 162 | first_byte = 0; |
| 163 | first_byte |= PACKET_PUBLIC_FLAGS_8BYTE_CONNECTION_ID; |
| 164 | EXPECT_FALSE(QuicUtils::IsIetfPacketHeader(first_byte)); |
| 165 | EXPECT_FALSE(QuicUtils::IsIetfPacketShortHeader(first_byte)); |
| 166 | } |
| 167 | |
dschinazi | adc7507 | 2019-08-19 10:54:45 -0700 | [diff] [blame] | 168 | TEST_F(QuicUtilsTest, ReplacementConnectionIdIsDeterministic) { |
| 169 | // Verify that two equal connection IDs get the same replacement. |
| 170 | QuicConnectionId connection_id64a = TestConnectionId(33); |
| 171 | QuicConnectionId connection_id64b = TestConnectionId(33); |
| 172 | EXPECT_EQ(connection_id64a, connection_id64b); |
| 173 | EXPECT_EQ(QuicUtils::CreateReplacementConnectionId(connection_id64a), |
| 174 | QuicUtils::CreateReplacementConnectionId(connection_id64b)); |
| 175 | QuicConnectionId connection_id72a = TestConnectionIdNineBytesLong(42); |
| 176 | QuicConnectionId connection_id72b = TestConnectionIdNineBytesLong(42); |
| 177 | EXPECT_EQ(connection_id72a, connection_id72b); |
| 178 | EXPECT_EQ(QuicUtils::CreateReplacementConnectionId(connection_id72a), |
| 179 | QuicUtils::CreateReplacementConnectionId(connection_id72b)); |
| 180 | } |
| 181 | |
| 182 | TEST_F(QuicUtilsTest, ReplacementConnectionIdLengthIsCorrect) { |
| 183 | // Verify that all lengths get replaced by kQuicDefaultConnectionIdLength. |
| 184 | const char connection_id_bytes[kQuicMaxConnectionIdAllVersionsLength] = {}; |
| 185 | for (uint8_t i = 0; i < sizeof(connection_id_bytes) - 1; ++i) { |
| 186 | QuicConnectionId connection_id(connection_id_bytes, i); |
| 187 | QuicConnectionId replacement_connection_id = |
| 188 | QuicUtils::CreateReplacementConnectionId(connection_id); |
| 189 | EXPECT_EQ(kQuicDefaultConnectionIdLength, |
| 190 | replacement_connection_id.length()); |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | TEST_F(QuicUtilsTest, ReplacementConnectionIdHasEntropy) { |
| 195 | // Make sure all these test connection IDs have different replacements. |
| 196 | for (uint64_t i = 0; i < 256; ++i) { |
| 197 | QuicConnectionId connection_id_i = TestConnectionId(i); |
| 198 | EXPECT_NE(connection_id_i, |
| 199 | QuicUtils::CreateReplacementConnectionId(connection_id_i)); |
| 200 | for (uint64_t j = i + 1; j <= 256; ++j) { |
| 201 | QuicConnectionId connection_id_j = TestConnectionId(j); |
| 202 | EXPECT_NE(connection_id_i, connection_id_j); |
| 203 | EXPECT_NE(QuicUtils::CreateReplacementConnectionId(connection_id_i), |
| 204 | QuicUtils::CreateReplacementConnectionId(connection_id_j)); |
| 205 | } |
| 206 | } |
| 207 | } |
| 208 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 209 | TEST_F(QuicUtilsTest, RandomConnectionId) { |
| 210 | MockRandom random(33); |
| 211 | QuicConnectionId connection_id = QuicUtils::CreateRandomConnectionId(&random); |
| 212 | EXPECT_EQ(connection_id.length(), sizeof(uint64_t)); |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 213 | char connection_id_bytes[sizeof(uint64_t)]; |
| 214 | random.RandBytes(connection_id_bytes, QUIC_ARRAYSIZE(connection_id_bytes)); |
| 215 | EXPECT_EQ(connection_id, |
| 216 | QuicConnectionId(static_cast<char*>(connection_id_bytes), |
| 217 | QUIC_ARRAYSIZE(connection_id_bytes))); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 218 | EXPECT_NE(connection_id, EmptyQuicConnectionId()); |
| 219 | EXPECT_NE(connection_id, TestConnectionId()); |
| 220 | EXPECT_NE(connection_id, TestConnectionId(1)); |
QUICHE team | c65d1d1 | 2019-03-19 20:58:04 -0700 | [diff] [blame] | 221 | EXPECT_NE(connection_id, TestConnectionIdNineBytesLong(1)); |
| 222 | EXPECT_EQ(QuicUtils::CreateRandomConnectionId().length(), |
| 223 | kQuicDefaultConnectionIdLength); |
| 224 | } |
| 225 | |
| 226 | TEST_F(QuicUtilsTest, RandomConnectionIdVariableLength) { |
| 227 | MockRandom random(1337); |
| 228 | const uint8_t connection_id_length = 9; |
| 229 | QuicConnectionId connection_id = |
| 230 | QuicUtils::CreateRandomConnectionId(connection_id_length, &random); |
| 231 | EXPECT_EQ(connection_id.length(), connection_id_length); |
| 232 | char connection_id_bytes[connection_id_length]; |
| 233 | random.RandBytes(connection_id_bytes, QUIC_ARRAYSIZE(connection_id_bytes)); |
| 234 | EXPECT_EQ(connection_id, |
| 235 | QuicConnectionId(static_cast<char*>(connection_id_bytes), |
| 236 | QUIC_ARRAYSIZE(connection_id_bytes))); |
| 237 | EXPECT_NE(connection_id, EmptyQuicConnectionId()); |
| 238 | EXPECT_NE(connection_id, TestConnectionId()); |
| 239 | EXPECT_NE(connection_id, TestConnectionId(1)); |
| 240 | EXPECT_NE(connection_id, TestConnectionIdNineBytesLong(1)); |
| 241 | EXPECT_EQ(QuicUtils::CreateRandomConnectionId(connection_id_length).length(), |
| 242 | connection_id_length); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | TEST_F(QuicUtilsTest, VariableLengthConnectionId) { |
| 246 | EXPECT_FALSE( |
fayang | 8265a2a | 2019-10-16 11:23:51 -0700 | [diff] [blame] | 247 | QuicUtils::VariableLengthConnectionIdAllowedForVersion(QUIC_VERSION_43)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 248 | EXPECT_TRUE(QuicUtils::IsConnectionIdValidForVersion( |
fayang | 8265a2a | 2019-10-16 11:23:51 -0700 | [diff] [blame] | 249 | QuicUtils::CreateZeroConnectionId(QUIC_VERSION_43), QUIC_VERSION_43)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 250 | EXPECT_TRUE(QuicUtils::IsConnectionIdValidForVersion( |
| 251 | QuicUtils::CreateZeroConnectionId(QUIC_VERSION_99), QUIC_VERSION_99)); |
fayang | 8265a2a | 2019-10-16 11:23:51 -0700 | [diff] [blame] | 252 | EXPECT_NE(QuicUtils::CreateZeroConnectionId(QUIC_VERSION_43), |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 253 | EmptyQuicConnectionId()); |
QUICHE team | 8e2e453 | 2019-03-14 14:37:56 -0700 | [diff] [blame] | 254 | EXPECT_EQ(QuicUtils::CreateZeroConnectionId(QUIC_VERSION_99), |
| 255 | EmptyQuicConnectionId()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 256 | EXPECT_FALSE(QuicUtils::IsConnectionIdValidForVersion(EmptyQuicConnectionId(), |
fayang | 8265a2a | 2019-10-16 11:23:51 -0700 | [diff] [blame] | 257 | QUIC_VERSION_43)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | TEST_F(QuicUtilsTest, StatelessResetToken) { |
| 261 | QuicConnectionId connection_id1a = test::TestConnectionId(1); |
| 262 | QuicConnectionId connection_id1b = test::TestConnectionId(1); |
| 263 | QuicConnectionId connection_id2 = test::TestConnectionId(2); |
| 264 | QuicUint128 token1a = QuicUtils::GenerateStatelessResetToken(connection_id1a); |
| 265 | QuicUint128 token1b = QuicUtils::GenerateStatelessResetToken(connection_id1b); |
| 266 | QuicUint128 token2 = QuicUtils::GenerateStatelessResetToken(connection_id2); |
| 267 | EXPECT_EQ(token1a, token1b); |
| 268 | EXPECT_NE(token1a, token2); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | } // namespace |
| 272 | } // namespace test |
| 273 | } // namespace quic |