Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / net / quic / quic_protocol_test.cc
blobc74d3d108d6804e36d80890e38e80e84830a46e8
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
3 // found in the LICENSE file.
5 #include "net/quic/quic_protocol.h"
7 #include <sstream>
9 #include "base/stl_util.h"
10 #include "testing/gtest/include/gtest/gtest.h"
12 namespace net {
13 namespace test {
14 namespace {
16 TEST(QuicProtocolTest, AdjustErrorForVersion) {
17 ASSERT_EQ(8, QUIC_STREAM_LAST_ERROR)
18 << "Any additions to QuicRstStreamErrorCode require an addition to "
19 << "AdjustErrorForVersion and this associated test.";
21 // If we ever add different RST codes, we should have a test akin to the
22 // following.
23 // EXPECT_EQ(QUIC_RST_ACKNOWLEDGEMENT, AdjustErrorForVersion(
24 // QUIC_RST_ACKNOWLEDGEMENT,
25 // QUIC_VERSION_24));
28 TEST(QuicProtocolTest, MakeQuicTag) {
29 QuicTag tag = MakeQuicTag('A', 'B', 'C', 'D');
30 char bytes[4];
31 memcpy(bytes, &tag, 4);
32 EXPECT_EQ('A', bytes[0]);
33 EXPECT_EQ('B', bytes[1]);
34 EXPECT_EQ('C', bytes[2]);
35 EXPECT_EQ('D', bytes[3]);
38 TEST(QuicProtocolTest, IsAawaitingPacket) {
39 QuicAckFrame ack_frame;
40 ack_frame.largest_observed = 10u;
41 EXPECT_TRUE(IsAwaitingPacket(ack_frame, 11u));
42 EXPECT_FALSE(IsAwaitingPacket(ack_frame, 1u));
44 ack_frame.missing_packets.Add(10);
45 EXPECT_TRUE(IsAwaitingPacket(ack_frame, 10u));
48 TEST(QuicProtocolTest, QuicVersionToQuicTag) {
49 // If you add a new version to the QuicVersion enum you will need to add a new
50 // case to QuicVersionToQuicTag, otherwise this test will fail.
52 // TODO(rtenneti): Enable checking of Log(ERROR) messages.
53 #if 0
54 // Any logs would indicate an unsupported version which we don't expect.
55 ScopedMockLog log(kDoNotCaptureLogsYet);
56 EXPECT_CALL(log, Log(_, _, _)).Times(0);
57 log.StartCapturingLogs();
58 #endif
60 // Explicitly test a specific version.
61 EXPECT_EQ(MakeQuicTag('Q', '0', '2', '5'),
62 QuicVersionToQuicTag(QUIC_VERSION_25));
64 // Loop over all supported versions and make sure that we never hit the
65 // default case (i.e. all supported versions should be successfully converted
66 // to valid QuicTags).
67 for (size_t i = 0; i < arraysize(kSupportedQuicVersions); ++i) {
68 QuicVersion version = kSupportedQuicVersions[i];
69 EXPECT_LT(0u, QuicVersionToQuicTag(version));
73 TEST(QuicProtocolTest, QuicVersionToQuicTagUnsupported) {
74 // TODO(rtenneti): Enable checking of Log(ERROR) messages.
75 #if 0
76 // TODO(rjshade): Change to DFATAL once we actually support multiple versions,
77 // and QuicConnectionTest::SendVersionNegotiationPacket can be changed to use
78 // mis-matched versions rather than relying on QUIC_VERSION_UNSUPPORTED.
79 ScopedMockLog log(kDoNotCaptureLogsYet);
80 EXPECT_CALL(log, Log(ERROR, _, "Unsupported QuicVersion: 0")).Times(1);
81 log.StartCapturingLogs();
82 #endif
84 EXPECT_EQ(0u, QuicVersionToQuicTag(QUIC_VERSION_UNSUPPORTED));
87 TEST(QuicProtocolTest, QuicTagToQuicVersion) {
88 // If you add a new version to the QuicVersion enum you will need to add a new
89 // case to QuicTagToQuicVersion, otherwise this test will fail.
91 // TODO(rtenneti): Enable checking of Log(ERROR) messages.
92 #if 0
93 // Any logs would indicate an unsupported version which we don't expect.
94 ScopedMockLog log(kDoNotCaptureLogsYet);
95 EXPECT_CALL(log, Log(_, _, _)).Times(0);
96 log.StartCapturingLogs();
97 #endif
99 // Explicitly test specific versions.
100 EXPECT_EQ(QUIC_VERSION_25,
101 QuicTagToQuicVersion(MakeQuicTag('Q', '0', '2', '5')));
103 for (size_t i = 0; i < arraysize(kSupportedQuicVersions); ++i) {
104 QuicVersion version = kSupportedQuicVersions[i];
106 // Get the tag from the version (we can loop over QuicVersions easily).
107 QuicTag tag = QuicVersionToQuicTag(version);
108 EXPECT_LT(0u, tag);
110 // Now try converting back.
111 QuicVersion tag_to_quic_version = QuicTagToQuicVersion(tag);
112 EXPECT_EQ(version, tag_to_quic_version);
113 EXPECT_NE(QUIC_VERSION_UNSUPPORTED, tag_to_quic_version);
117 TEST(QuicProtocolTest, QuicTagToQuicVersionUnsupported) {
118 // TODO(rtenneti): Enable checking of Log(ERROR) messages.
119 #if 0
120 ScopedMockLog log(kDoNotCaptureLogsYet);
121 #ifndef NDEBUG
122 EXPECT_CALL(log, Log(INFO, _, "Unsupported QuicTag version: FAKE")).Times(1);
123 #endif
124 log.StartCapturingLogs();
125 #endif
127 EXPECT_EQ(QUIC_VERSION_UNSUPPORTED,
128 QuicTagToQuicVersion(MakeQuicTag('F', 'A', 'K', 'E')));
131 TEST(QuicProtocolTest, QuicVersionToString) {
132 EXPECT_EQ("QUIC_VERSION_25", QuicVersionToString(QUIC_VERSION_25));
133 EXPECT_EQ("QUIC_VERSION_UNSUPPORTED",
134 QuicVersionToString(QUIC_VERSION_UNSUPPORTED));
136 QuicVersion single_version[] = {QUIC_VERSION_25};
137 QuicVersionVector versions_vector;
138 for (size_t i = 0; i < arraysize(single_version); ++i) {
139 versions_vector.push_back(single_version[i]);
141 EXPECT_EQ("QUIC_VERSION_25", QuicVersionVectorToString(versions_vector));
143 QuicVersion multiple_versions[] = {QUIC_VERSION_UNSUPPORTED, QUIC_VERSION_25};
144 versions_vector.clear();
145 for (size_t i = 0; i < arraysize(multiple_versions); ++i) {
146 versions_vector.push_back(multiple_versions[i]);
148 EXPECT_EQ("QUIC_VERSION_UNSUPPORTED,QUIC_VERSION_25",
149 QuicVersionVectorToString(versions_vector));
151 // Make sure that all supported versions are present in QuicVersionToString.
152 for (size_t i = 0; i < arraysize(kSupportedQuicVersions); ++i) {
153 QuicVersion version = kSupportedQuicVersions[i];
154 EXPECT_NE("QUIC_VERSION_UNSUPPORTED", QuicVersionToString(version));
158 // Tests that a queue contains the expected data after calls to Add().
159 TEST(PacketNumberQueueTest, AddRange) {
160 PacketNumberQueue queue;
161 queue.Add(1, 51);
162 queue.Add(53);
164 EXPECT_FALSE(queue.Contains(0));
165 for (int i = 1; i < 51; ++i) {
166 EXPECT_TRUE(queue.Contains(i));
168 EXPECT_FALSE(queue.Contains(51));
169 EXPECT_FALSE(queue.Contains(52));
170 EXPECT_TRUE(queue.Contains(53));
171 EXPECT_FALSE(queue.Contains(54));
172 EXPECT_EQ(51u, queue.NumPacketsSlow());
173 EXPECT_EQ(1u, queue.Min());
174 EXPECT_EQ(53u, queue.Max());
176 queue.Add(70);
177 EXPECT_EQ(70u, queue.Max());
180 // Tests that a queue contains the expected data after calls to Remove().
181 TEST(PacketNumberQueueTest, Removal) {
182 PacketNumberQueue queue;
183 queue.Add(0, 100);
185 EXPECT_TRUE(queue.RemoveUpTo(51));
186 EXPECT_FALSE(queue.RemoveUpTo(51));
187 queue.Remove(53);
189 EXPECT_FALSE(queue.Contains(0));
190 for (int i = 1; i < 51; ++i) {
191 EXPECT_FALSE(queue.Contains(i));
193 EXPECT_TRUE(queue.Contains(51));
194 EXPECT_TRUE(queue.Contains(52));
195 EXPECT_FALSE(queue.Contains(53));
196 EXPECT_TRUE(queue.Contains(54));
197 EXPECT_EQ(48u, queue.NumPacketsSlow());
198 EXPECT_EQ(51u, queue.Min());
199 EXPECT_EQ(99u, queue.Max());
201 queue.Remove(51);
202 EXPECT_EQ(52u, queue.Min());
203 queue.Remove(99);
204 EXPECT_EQ(98u, queue.Max());
207 // Tests that a queue is empty when all of its elements are removed.
208 TEST(PacketNumberQueueTest, Empty) {
209 PacketNumberQueue queue;
210 EXPECT_TRUE(queue.Empty());
211 EXPECT_EQ(0u, queue.NumPacketsSlow());
213 queue.Add(1, 100);
214 EXPECT_TRUE(queue.RemoveUpTo(100));
215 EXPECT_TRUE(queue.Empty());
216 EXPECT_EQ(0u, queue.NumPacketsSlow());
219 // Tests that logging the state of a PacketNumberQueue does not crash.
220 TEST(PacketNumberQueueTest, LogDoesNotCrash) {
221 std::ostringstream oss;
222 PacketNumberQueue queue;
223 oss << queue;
225 queue.Add(1);
226 queue.Add(50, 100);
227 oss << queue;
230 // Tests that the iterators returned from a packet queue iterate over the queue.
231 TEST(PacketNumberQueueTest, Iterators) {
232 PacketNumberQueue queue;
233 queue.Add(1, 100);
235 const std::vector<QuicPacketNumber> actual(queue.begin(), queue.end());
237 std::vector<QuicPacketNumber> expected;
238 for (int i = 1; i < 100; ++i) {
239 expected.push_back(i);
242 EXPECT_EQ(expected, actual);
244 PacketNumberQueue::const_iterator it_low = queue.lower_bound(10);
245 EXPECT_EQ(10u, *it_low);
247 it_low = queue.lower_bound(101);
248 EXPECT_TRUE(queue.end() == it_low);
251 } // namespace
252 } // namespace test
253 } // namespace net