| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "net/quic/quic_protocol.h" | 5 #include "net/quic/quic_protocol.h" |
| 6 | 6 |
| 7 #include "base/stl_util.h" | 7 #include "base/stl_util.h" |
| 8 #include "testing/gtest/include/gtest/gtest.h" | 8 #include "testing/gtest/include/gtest/gtest.h" |
| 9 | 9 |
| 10 namespace net { | 10 namespace net { |
| 11 namespace test { | 11 namespace test { |
| 12 namespace { | 12 namespace { |
| 13 | 13 |
| 14 TEST(QuicProtocolTest, AdjustErrorForVersion) { | 14 TEST(QuicProtocolTest, AdjustErrorForVersion) { |
| 15 ASSERT_EQ(8, QUIC_STREAM_LAST_ERROR) | 15 ASSERT_EQ(8, QUIC_STREAM_LAST_ERROR) |
| 16 << "Any additions to QuicRstStreamErrorCode require an addition to " | 16 << "Any additions to QuicRstStreamErrorCode require an addition to " |
| 17 << "AdjustErrorForVersion and this associated test."; | 17 << "AdjustErrorForVersion and this associated test."; |
| 18 | 18 |
| 19 EXPECT_EQ(QUIC_STREAM_NO_ERROR, | 19 EXPECT_EQ( |
| 20 AdjustErrorForVersion(QUIC_RST_FLOW_CONTROL_ACCOUNTING, | 20 QUIC_STREAM_NO_ERROR, |
| 21 QUIC_VERSION_17)); | 21 AdjustErrorForVersion(QUIC_RST_FLOW_CONTROL_ACCOUNTING, QUIC_VERSION_17)); |
| 22 EXPECT_EQ(QUIC_RST_FLOW_CONTROL_ACCOUNTING, AdjustErrorForVersion( | 22 EXPECT_EQ( |
| 23 QUIC_RST_FLOW_CONTROL_ACCOUNTING, | 23 QUIC_RST_FLOW_CONTROL_ACCOUNTING, |
| 24 static_cast<QuicVersion>(QUIC_VERSION_17 + 1))); | 24 AdjustErrorForVersion(QUIC_RST_FLOW_CONTROL_ACCOUNTING, |
| 25 static_cast<QuicVersion>(QUIC_VERSION_17 + 1))); |
| 25 } | 26 } |
| 26 | 27 |
| 27 TEST(QuicProtocolTest, MakeQuicTag) { | 28 TEST(QuicProtocolTest, MakeQuicTag) { |
| 28 QuicTag tag = MakeQuicTag('A', 'B', 'C', 'D'); | 29 QuicTag tag = MakeQuicTag('A', 'B', 'C', 'D'); |
| 29 char bytes[4]; | 30 char bytes[4]; |
| 30 memcpy(bytes, &tag, 4); | 31 memcpy(bytes, &tag, 4); |
| 31 EXPECT_EQ('A', bytes[0]); | 32 EXPECT_EQ('A', bytes[0]); |
| 32 EXPECT_EQ('B', bytes[1]); | 33 EXPECT_EQ('B', bytes[1]); |
| 33 EXPECT_EQ('C', bytes[2]); | 34 EXPECT_EQ('C', bytes[2]); |
| 34 EXPECT_EQ('D', bytes[3]); | 35 EXPECT_EQ('D', bytes[3]); |
| 35 } | 36 } |
| 36 | 37 |
| 37 TEST(QuicProtocolTest, IsAawaitingPacket) { | 38 TEST(QuicProtocolTest, IsAawaitingPacket) { |
| 38 ReceivedPacketInfo received_info; | 39 ReceivedPacketInfo received_info; |
| 39 received_info.largest_observed = 10u; | 40 received_info.largest_observed = 10u; |
| 40 EXPECT_TRUE(IsAwaitingPacket(received_info, 11u)); | 41 EXPECT_TRUE(IsAwaitingPacket(received_info, 11u)); |
| 41 EXPECT_FALSE(IsAwaitingPacket(received_info, 1u)); | 42 EXPECT_FALSE(IsAwaitingPacket(received_info, 1u)); |
| 42 | 43 |
| 43 received_info.missing_packets.insert(10); | 44 received_info.missing_packets.insert(10); |
| 44 EXPECT_TRUE(IsAwaitingPacket(received_info, 10u)); | 45 EXPECT_TRUE(IsAwaitingPacket(received_info, 10u)); |
| 45 } | 46 } |
| 46 | 47 |
| 47 TEST(QuicProtocolTest, InsertMissingPacketsBetween) { | 48 TEST(QuicProtocolTest, InsertMissingPacketsBetween) { |
| 48 ReceivedPacketInfo received_info; | 49 ReceivedPacketInfo received_info; |
| 49 InsertMissingPacketsBetween(&received_info, 4u, 10u); | 50 InsertMissingPacketsBetween(&received_info, 4u, 10u); |
| 50 EXPECT_EQ(6u, received_info.missing_packets.size()); | 51 EXPECT_EQ(6u, received_info.missing_packets.size()); |
| 51 | 52 |
| 52 QuicPacketSequenceNumber i = 4; | 53 QuicPacketSequenceNumber i = 4; |
| 53 for (SequenceNumberSet::iterator it = received_info.missing_packets.begin(); | 54 for (SequenceNumberSet::iterator it = received_info.missing_packets.begin(); |
| 54 it != received_info.missing_packets.end(); ++it, ++i) { | 55 it != received_info.missing_packets.end(); |
| 56 ++it, ++i) { |
| 55 EXPECT_EQ(i, *it); | 57 EXPECT_EQ(i, *it); |
| 56 } | 58 } |
| 57 } | 59 } |
| 58 | 60 |
| 59 TEST(QuicProtocolTest, QuicVersionToQuicTag) { | 61 TEST(QuicProtocolTest, QuicVersionToQuicTag) { |
| 60 // If you add a new version to the QuicVersion enum you will need to add a new | 62 // If you add a new version to the QuicVersion enum you will need to add a new |
| 61 // case to QuicVersionToQuicTag, otherwise this test will fail. | 63 // case to QuicVersionToQuicTag, otherwise this test will fail. |
| 62 | 64 |
| 63 // TODO(rtenneti): Enable checking of Log(ERROR) messages. | 65 // TODO(rtenneti): Enable checking of Log(ERROR) messages. |
| 64 #if 0 | 66 #if 0 |
| 65 // Any logs would indicate an unsupported version which we don't expect. | 67 // Any logs would indicate an unsupported version which we don't expect. |
| 66 ScopedMockLog log(kDoNotCaptureLogsYet); | 68 ScopedMockLog log(kDoNotCaptureLogsYet); |
| 67 EXPECT_CALL(log, Log(_, _, _)).Times(0); | 69 EXPECT_CALL(log, Log(_, _, _)).Times(0); |
| 68 log.StartCapturingLogs(); | 70 log.StartCapturingLogs(); |
| 69 #endif | 71 #endif |
| 70 | 72 |
| 71 // Explicitly test a specific version. | 73 // Explicitly test a specific version. |
| 72 EXPECT_EQ(MakeQuicTag('Q', '0', '1', '6'), | 74 EXPECT_EQ(MakeQuicTag('Q', '0', '1', '6'), |
| 73 QuicVersionToQuicTag(QUIC_VERSION_16)); | 75 QuicVersionToQuicTag(QUIC_VERSION_16)); |
| 74 | 76 |
| 75 // Loop over all supported versions and make sure that we never hit the | 77 // Loop over all supported versions and make sure that we never hit the |
| 76 // default case (i.e. all supported versions should be successfully converted | 78 // default case (i.e. all supported versions should be successfully converted |
| 77 // to valid QuicTags). | 79 // to valid QuicTags). |
| 78 for (size_t i = 0; i < arraysize(kSupportedQuicVersions); ++i) { | 80 for (size_t i = 0; i < arraysize(kSupportedQuicVersions); ++i) { |
| 79 QuicVersion version = kSupportedQuicVersions[i]; | 81 QuicVersion version = kSupportedQuicVersions[i]; |
| 80 EXPECT_LT(0u, QuicVersionToQuicTag(version)); | 82 EXPECT_LT(0u, QuicVersionToQuicTag(version)); |
| 81 } | 83 } |
| 82 } | 84 } |
| 83 | 85 |
| 84 TEST(QuicProtocolTest, QuicVersionToQuicTagUnsupported) { | 86 TEST(QuicProtocolTest, QuicVersionToQuicTagUnsupported) { |
| 85 // TODO(rtenneti): Enable checking of Log(ERROR) messages. | 87 // TODO(rtenneti): Enable checking of Log(ERROR) messages. |
| 86 #if 0 | 88 #if 0 |
| 87 // TODO(rjshade): Change to DFATAL once we actually support multiple versions, | 89 // TODO(rjshade): Change to DFATAL once we actually support multiple versions, |
| 88 // and QuicConnectionTest::SendVersionNegotiationPacket can be changed to use | 90 // and QuicConnectionTest::SendVersionNegotiationPacket can be changed to use |
| 89 // mis-matched versions rather than relying on QUIC_VERSION_UNSUPPORTED. | 91 // mis-matched versions rather than relying on QUIC_VERSION_UNSUPPORTED. |
| 90 ScopedMockLog log(kDoNotCaptureLogsYet); | 92 ScopedMockLog log(kDoNotCaptureLogsYet); |
| 91 EXPECT_CALL(log, Log(ERROR, _, "Unsupported QuicVersion: 0")).Times(1); | 93 EXPECT_CALL(log, Log(ERROR, _, "Unsupported QuicVersion: 0")).Times(1); |
| 92 log.StartCapturingLogs(); | 94 log.StartCapturingLogs(); |
| 93 #endif | 95 #endif |
| 94 | 96 |
| 95 EXPECT_EQ(0u, QuicVersionToQuicTag(QUIC_VERSION_UNSUPPORTED)); | 97 EXPECT_EQ(0u, QuicVersionToQuicTag(QUIC_VERSION_UNSUPPORTED)); |
| 96 } | 98 } |
| 97 | 99 |
| 98 TEST(QuicProtocolTest, QuicTagToQuicVersion) { | 100 TEST(QuicProtocolTest, QuicTagToQuicVersion) { |
| 99 // If you add a new version to the QuicVersion enum you will need to add a new | 101 // If you add a new version to the QuicVersion enum you will need to add a new |
| 100 // case to QuicTagToQuicVersion, otherwise this test will fail. | 102 // case to QuicTagToQuicVersion, otherwise this test will fail. |
| 101 | 103 |
| 102 // TODO(rtenneti): Enable checking of Log(ERROR) messages. | 104 // TODO(rtenneti): Enable checking of Log(ERROR) messages. |
| 103 #if 0 | 105 #if 0 |
| 104 // Any logs would indicate an unsupported version which we don't expect. | 106 // Any logs would indicate an unsupported version which we don't expect. |
| 105 ScopedMockLog log(kDoNotCaptureLogsYet); | 107 ScopedMockLog log(kDoNotCaptureLogsYet); |
| 106 EXPECT_CALL(log, Log(_, _, _)).Times(0); | 108 EXPECT_CALL(log, Log(_, _, _)).Times(0); |
| 107 log.StartCapturingLogs(); | 109 log.StartCapturingLogs(); |
| 108 #endif | 110 #endif |
| 109 | 111 |
| 110 // Explicitly test specific versions. | 112 // Explicitly test specific versions. |
| 111 EXPECT_EQ(QUIC_VERSION_16, | 113 EXPECT_EQ(QUIC_VERSION_16, |
| 112 QuicTagToQuicVersion(MakeQuicTag('Q', '0', '1', '6'))); | 114 QuicTagToQuicVersion(MakeQuicTag('Q', '0', '1', '6'))); |
| 113 | 115 |
| 114 for (size_t i = 0; i < arraysize(kSupportedQuicVersions); ++i) { | 116 for (size_t i = 0; i < arraysize(kSupportedQuicVersions); ++i) { |
| 115 QuicVersion version = kSupportedQuicVersions[i]; | 117 QuicVersion version = kSupportedQuicVersions[i]; |
| 116 | 118 |
| 117 // Get the tag from the version (we can loop over QuicVersions easily). | 119 // Get the tag from the version (we can loop over QuicVersions easily). |
| 118 QuicTag tag = QuicVersionToQuicTag(version); | 120 QuicTag tag = QuicVersionToQuicTag(version); |
| 119 EXPECT_LT(0u, tag); | 121 EXPECT_LT(0u, tag); |
| 120 | 122 |
| 121 // Now try converting back. | 123 // Now try converting back. |
| 122 QuicVersion tag_to_quic_version = QuicTagToQuicVersion(tag); | 124 QuicVersion tag_to_quic_version = QuicTagToQuicVersion(tag); |
| 123 EXPECT_EQ(version, tag_to_quic_version); | 125 EXPECT_EQ(version, tag_to_quic_version); |
| 124 EXPECT_NE(QUIC_VERSION_UNSUPPORTED, tag_to_quic_version); | 126 EXPECT_NE(QUIC_VERSION_UNSUPPORTED, tag_to_quic_version); |
| 125 } | 127 } |
| 126 } | 128 } |
| 127 | 129 |
| 128 TEST(QuicProtocolTest, QuicTagToQuicVersionUnsupported) { | 130 TEST(QuicProtocolTest, QuicTagToQuicVersionUnsupported) { |
| 129 // TODO(rtenneti): Enable checking of Log(ERROR) messages. | 131 // TODO(rtenneti): Enable checking of Log(ERROR) messages. |
| 130 #if 0 | 132 #if 0 |
| 131 ScopedMockLog log(kDoNotCaptureLogsYet); | 133 ScopedMockLog log(kDoNotCaptureLogsYet); |
| 132 #ifndef NDEBUG | 134 #ifndef NDEBUG |
| 133 EXPECT_CALL(log, Log(INFO, _, "Unsupported QuicTag version: FAKE")).Times(1); | 135 EXPECT_CALL(log, Log(INFO, _, "Unsupported QuicTag version: FAKE")).Times(1); |
| 134 #endif | 136 #endif |
| 135 log.StartCapturingLogs(); | 137 log.StartCapturingLogs(); |
| 136 #endif | 138 #endif |
| 137 | 139 |
| 138 EXPECT_EQ(QUIC_VERSION_UNSUPPORTED, | 140 EXPECT_EQ(QUIC_VERSION_UNSUPPORTED, |
| 139 QuicTagToQuicVersion(MakeQuicTag('F', 'A', 'K', 'E'))); | 141 QuicTagToQuicVersion(MakeQuicTag('F', 'A', 'K', 'E'))); |
| (...skipping 22 matching lines...) Expand all Loading... |
| 162 // Make sure that all supported versions are present in QuicVersionToString. | 164 // Make sure that all supported versions are present in QuicVersionToString. |
| 163 for (size_t i = 0; i < arraysize(kSupportedQuicVersions); ++i) { | 165 for (size_t i = 0; i < arraysize(kSupportedQuicVersions); ++i) { |
| 164 QuicVersion version = kSupportedQuicVersions[i]; | 166 QuicVersion version = kSupportedQuicVersions[i]; |
| 165 EXPECT_NE("QUIC_VERSION_UNSUPPORTED", QuicVersionToString(version)); | 167 EXPECT_NE("QUIC_VERSION_UNSUPPORTED", QuicVersionToString(version)); |
| 166 } | 168 } |
| 167 } | 169 } |
| 168 | 170 |
| 169 } // namespace | 171 } // namespace |
| 170 } // namespace test | 172 } // namespace test |
| 171 } // namespace net | 173 } // namespace net |
| OLD | NEW |