QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1 | // Copyright 2018 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_connection_id.h" |
| 6 | |
| 7 | #include <cstdint> |
| 8 | #include <cstring> |
vasilvv | 872e7a3 | 2019-03-12 16:42:44 -0700 | [diff] [blame] | 9 | #include <string> |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 10 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 11 | #include "net/third_party/quiche/src/quic/core/quic_types.h" |
| 12 | #include "net/third_party/quiche/src/quic/platform/api/quic_arraysize.h" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 13 | #include "net/third_party/quiche/src/quic/platform/api/quic_test.h" |
| 14 | #include "net/third_party/quiche/src/quic/test_tools/quic_test_utils.h" |
| 15 | |
| 16 | namespace quic { |
| 17 | |
| 18 | namespace { |
| 19 | |
| 20 | class QuicConnectionIdTest : public QuicTest {}; |
| 21 | |
| 22 | TEST_F(QuicConnectionIdTest, Empty) { |
| 23 | QuicConnectionId connection_id_empty = EmptyQuicConnectionId(); |
| 24 | EXPECT_TRUE(connection_id_empty.IsEmpty()); |
| 25 | } |
| 26 | |
| 27 | TEST_F(QuicConnectionIdTest, DefaultIsEmpty) { |
| 28 | QuicConnectionId connection_id_empty = QuicConnectionId(); |
| 29 | EXPECT_TRUE(connection_id_empty.IsEmpty()); |
| 30 | } |
| 31 | |
| 32 | TEST_F(QuicConnectionIdTest, NotEmpty) { |
| 33 | QuicConnectionId connection_id = test::TestConnectionId(1); |
| 34 | EXPECT_FALSE(connection_id.IsEmpty()); |
| 35 | } |
| 36 | |
| 37 | TEST_F(QuicConnectionIdTest, ZeroIsNotEmpty) { |
| 38 | QuicConnectionId connection_id = test::TestConnectionId(0); |
| 39 | EXPECT_FALSE(connection_id.IsEmpty()); |
| 40 | } |
| 41 | |
| 42 | TEST_F(QuicConnectionIdTest, Data) { |
| 43 | char connection_id_data[kQuicDefaultConnectionIdLength]; |
| 44 | memset(connection_id_data, 0x42, sizeof(connection_id_data)); |
| 45 | QuicConnectionId connection_id1 = |
| 46 | QuicConnectionId(connection_id_data, sizeof(connection_id_data)); |
| 47 | QuicConnectionId connection_id2 = |
| 48 | QuicConnectionId(connection_id_data, sizeof(connection_id_data)); |
| 49 | EXPECT_EQ(connection_id1, connection_id2); |
| 50 | EXPECT_EQ(connection_id1.length(), kQuicDefaultConnectionIdLength); |
| 51 | EXPECT_EQ(connection_id1.data(), connection_id1.mutable_data()); |
| 52 | EXPECT_EQ(0, memcmp(connection_id1.data(), connection_id2.data(), |
| 53 | sizeof(connection_id_data))); |
| 54 | EXPECT_EQ(0, memcmp(connection_id1.data(), connection_id_data, |
| 55 | sizeof(connection_id_data))); |
| 56 | connection_id2.mutable_data()[0] = 0x33; |
| 57 | EXPECT_NE(connection_id1, connection_id2); |
| 58 | static const uint8_t kNewLength = 4; |
| 59 | connection_id2.set_length(kNewLength); |
| 60 | EXPECT_EQ(kNewLength, connection_id2.length()); |
| 61 | } |
| 62 | |
| 63 | TEST_F(QuicConnectionIdTest, DoubleConvert) { |
| 64 | QuicConnectionId connection_id64_1 = test::TestConnectionId(1); |
| 65 | QuicConnectionId connection_id64_2 = test::TestConnectionId(42); |
| 66 | QuicConnectionId connection_id64_3 = |
| 67 | test::TestConnectionId(UINT64_C(0xfedcba9876543210)); |
| 68 | EXPECT_EQ(connection_id64_1, |
| 69 | test::TestConnectionId( |
| 70 | test::TestConnectionIdToUInt64(connection_id64_1))); |
| 71 | EXPECT_EQ(connection_id64_2, |
| 72 | test::TestConnectionId( |
| 73 | test::TestConnectionIdToUInt64(connection_id64_2))); |
| 74 | EXPECT_EQ(connection_id64_3, |
| 75 | test::TestConnectionId( |
| 76 | test::TestConnectionIdToUInt64(connection_id64_3))); |
| 77 | EXPECT_NE(connection_id64_1, connection_id64_2); |
| 78 | EXPECT_NE(connection_id64_1, connection_id64_3); |
| 79 | EXPECT_NE(connection_id64_2, connection_id64_3); |
| 80 | } |
| 81 | |
| 82 | TEST_F(QuicConnectionIdTest, Hash) { |
| 83 | QuicConnectionId connection_id64_1 = test::TestConnectionId(1); |
| 84 | QuicConnectionId connection_id64_1b = test::TestConnectionId(1); |
| 85 | QuicConnectionId connection_id64_2 = test::TestConnectionId(42); |
| 86 | QuicConnectionId connection_id64_3 = |
| 87 | test::TestConnectionId(UINT64_C(0xfedcba9876543210)); |
| 88 | EXPECT_EQ(connection_id64_1.Hash(), connection_id64_1b.Hash()); |
| 89 | EXPECT_NE(connection_id64_1.Hash(), connection_id64_2.Hash()); |
| 90 | EXPECT_NE(connection_id64_1.Hash(), connection_id64_3.Hash()); |
| 91 | EXPECT_NE(connection_id64_2.Hash(), connection_id64_3.Hash()); |
dschinazi | 0fdff8e | 2019-07-18 09:07:39 -0700 | [diff] [blame] | 92 | |
| 93 | // Verify that any two all-zero connection IDs of different lengths never |
| 94 | // have the same hash. |
dschinazi | b012d21 | 2019-08-01 18:07:26 -0700 | [diff] [blame] | 95 | const char connection_id_bytes[kQuicMaxConnectionIdAllVersionsLength] = {}; |
| 96 | for (uint8_t i = 0; i < sizeof(connection_id_bytes) - 1; ++i) { |
dschinazi | 0fdff8e | 2019-07-18 09:07:39 -0700 | [diff] [blame] | 97 | QuicConnectionId connection_id_i(connection_id_bytes, i); |
dschinazi | b012d21 | 2019-08-01 18:07:26 -0700 | [diff] [blame] | 98 | for (uint8_t j = i + 1; j < sizeof(connection_id_bytes); ++j) { |
dschinazi | 0fdff8e | 2019-07-18 09:07:39 -0700 | [diff] [blame] | 99 | QuicConnectionId connection_id_j(connection_id_bytes, j); |
| 100 | EXPECT_NE(connection_id_i.Hash(), connection_id_j.Hash()); |
| 101 | } |
| 102 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 103 | } |
| 104 | |
dschinazi | b324116 | 2019-06-10 17:59:37 -0700 | [diff] [blame] | 105 | TEST_F(QuicConnectionIdTest, AssignAndCopy) { |
| 106 | QuicConnectionId connection_id = test::TestConnectionId(1); |
| 107 | QuicConnectionId connection_id2 = test::TestConnectionId(2); |
| 108 | connection_id = connection_id2; |
| 109 | EXPECT_EQ(connection_id, test::TestConnectionId(2)); |
| 110 | EXPECT_NE(connection_id, test::TestConnectionId(1)); |
| 111 | connection_id = QuicConnectionId(test::TestConnectionId(1)); |
| 112 | EXPECT_EQ(connection_id, test::TestConnectionId(1)); |
| 113 | EXPECT_NE(connection_id, test::TestConnectionId(2)); |
| 114 | } |
| 115 | |
| 116 | TEST_F(QuicConnectionIdTest, ChangeLength) { |
| 117 | QuicConnectionId connection_id64_1 = test::TestConnectionId(1); |
| 118 | QuicConnectionId connection_id64_2 = test::TestConnectionId(2); |
| 119 | QuicConnectionId connection_id136_2 = test::TestConnectionId(2); |
| 120 | connection_id136_2.set_length(17); |
| 121 | memset(connection_id136_2.mutable_data() + 8, 0, 9); |
| 122 | char connection_id136_2_bytes[17] = {0, 0, 0, 0, 0, 0, 0, 2, 0, |
| 123 | 0, 0, 0, 0, 0, 0, 0, 0}; |
| 124 | QuicConnectionId connection_id136_2b(connection_id136_2_bytes, |
| 125 | sizeof(connection_id136_2_bytes)); |
| 126 | EXPECT_EQ(connection_id136_2, connection_id136_2b); |
| 127 | QuicConnectionId connection_id = connection_id64_1; |
| 128 | connection_id.set_length(17); |
| 129 | EXPECT_NE(connection_id64_1, connection_id); |
| 130 | // Check resizing big to small. |
| 131 | connection_id.set_length(8); |
| 132 | EXPECT_EQ(connection_id64_1, connection_id); |
| 133 | // Check resizing small to big. |
| 134 | connection_id.set_length(17); |
| 135 | memset(connection_id.mutable_data(), 0, connection_id.length()); |
| 136 | memcpy(connection_id.mutable_data(), connection_id64_2.data(), |
| 137 | connection_id64_2.length()); |
| 138 | EXPECT_EQ(connection_id136_2, connection_id); |
| 139 | EXPECT_EQ(connection_id136_2b, connection_id); |
| 140 | QuicConnectionId connection_id120(connection_id136_2_bytes, 15); |
| 141 | connection_id.set_length(15); |
| 142 | EXPECT_EQ(connection_id120, connection_id); |
| 143 | // Check resizing big to big. |
| 144 | QuicConnectionId connection_id2 = connection_id120; |
| 145 | connection_id2.set_length(17); |
| 146 | connection_id2.mutable_data()[15] = 0; |
| 147 | connection_id2.mutable_data()[16] = 0; |
| 148 | EXPECT_EQ(connection_id136_2, connection_id2); |
| 149 | EXPECT_EQ(connection_id136_2b, connection_id2); |
| 150 | } |
| 151 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 152 | } // namespace |
| 153 | |
| 154 | } // namespace quic |