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 | |
| 168 | TEST_F(QuicUtilsTest, RandomConnectionId) { |
| 169 | MockRandom random(33); |
| 170 | QuicConnectionId connection_id = QuicUtils::CreateRandomConnectionId(&random); |
| 171 | EXPECT_EQ(connection_id.length(), sizeof(uint64_t)); |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 172 | char connection_id_bytes[sizeof(uint64_t)]; |
| 173 | random.RandBytes(connection_id_bytes, QUIC_ARRAYSIZE(connection_id_bytes)); |
| 174 | EXPECT_EQ(connection_id, |
| 175 | QuicConnectionId(static_cast<char*>(connection_id_bytes), |
| 176 | QUIC_ARRAYSIZE(connection_id_bytes))); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 177 | EXPECT_NE(connection_id, EmptyQuicConnectionId()); |
| 178 | EXPECT_NE(connection_id, TestConnectionId()); |
| 179 | EXPECT_NE(connection_id, TestConnectionId(1)); |
QUICHE team | c65d1d1 | 2019-03-19 20:58:04 -0700 | [diff] [blame] | 180 | EXPECT_NE(connection_id, TestConnectionIdNineBytesLong(1)); |
| 181 | EXPECT_EQ(QuicUtils::CreateRandomConnectionId().length(), |
| 182 | kQuicDefaultConnectionIdLength); |
| 183 | } |
| 184 | |
| 185 | TEST_F(QuicUtilsTest, RandomConnectionIdVariableLength) { |
| 186 | MockRandom random(1337); |
| 187 | const uint8_t connection_id_length = 9; |
| 188 | QuicConnectionId connection_id = |
| 189 | QuicUtils::CreateRandomConnectionId(connection_id_length, &random); |
| 190 | EXPECT_EQ(connection_id.length(), connection_id_length); |
| 191 | char connection_id_bytes[connection_id_length]; |
| 192 | random.RandBytes(connection_id_bytes, QUIC_ARRAYSIZE(connection_id_bytes)); |
| 193 | EXPECT_EQ(connection_id, |
| 194 | QuicConnectionId(static_cast<char*>(connection_id_bytes), |
| 195 | QUIC_ARRAYSIZE(connection_id_bytes))); |
| 196 | EXPECT_NE(connection_id, EmptyQuicConnectionId()); |
| 197 | EXPECT_NE(connection_id, TestConnectionId()); |
| 198 | EXPECT_NE(connection_id, TestConnectionId(1)); |
| 199 | EXPECT_NE(connection_id, TestConnectionIdNineBytesLong(1)); |
| 200 | EXPECT_EQ(QuicUtils::CreateRandomConnectionId(connection_id_length).length(), |
| 201 | connection_id_length); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | TEST_F(QuicUtilsTest, VariableLengthConnectionId) { |
| 205 | EXPECT_FALSE( |
| 206 | QuicUtils::VariableLengthConnectionIdAllowedForVersion(QUIC_VERSION_39)); |
| 207 | EXPECT_TRUE(QuicUtils::IsConnectionIdValidForVersion( |
| 208 | QuicUtils::CreateZeroConnectionId(QUIC_VERSION_39), QUIC_VERSION_39)); |
| 209 | EXPECT_TRUE(QuicUtils::IsConnectionIdValidForVersion( |
| 210 | QuicUtils::CreateZeroConnectionId(QUIC_VERSION_99), QUIC_VERSION_99)); |
| 211 | EXPECT_NE(QuicUtils::CreateZeroConnectionId(QUIC_VERSION_39), |
| 212 | EmptyQuicConnectionId()); |
QUICHE team | 8e2e453 | 2019-03-14 14:37:56 -0700 | [diff] [blame] | 213 | EXPECT_EQ(QuicUtils::CreateZeroConnectionId(QUIC_VERSION_99), |
| 214 | EmptyQuicConnectionId()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 215 | EXPECT_FALSE(QuicUtils::IsConnectionIdValidForVersion(EmptyQuicConnectionId(), |
| 216 | QUIC_VERSION_39)); |
| 217 | } |
| 218 | |
| 219 | TEST_F(QuicUtilsTest, StatelessResetToken) { |
| 220 | QuicConnectionId connection_id1a = test::TestConnectionId(1); |
| 221 | QuicConnectionId connection_id1b = test::TestConnectionId(1); |
| 222 | QuicConnectionId connection_id2 = test::TestConnectionId(2); |
| 223 | QuicUint128 token1a = QuicUtils::GenerateStatelessResetToken(connection_id1a); |
| 224 | QuicUint128 token1b = QuicUtils::GenerateStatelessResetToken(connection_id1b); |
| 225 | QuicUint128 token2 = QuicUtils::GenerateStatelessResetToken(connection_id2); |
| 226 | EXPECT_EQ(token1a, token1b); |
| 227 | EXPECT_NE(token1a, token2); |
| 228 | EXPECT_EQ(token1a, MakeQuicUint128(0, 1)); |
| 229 | } |
| 230 | |
| 231 | } // namespace |
| 232 | } // namespace test |
| 233 | } // namespace quic |