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"
9 #include "base/stl_util.h"
10 #include "testing/gtest/include/gtest/gtest.h"
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
23 // EXPECT_EQ(QUIC_RST_ACKNOWLEDGEMENT, AdjustErrorForVersion(
24 // QUIC_RST_ACKNOWLEDGEMENT,
28 TEST(QuicProtocolTest
, MakeQuicTag
) {
29 QuicTag tag
= MakeQuicTag('A', 'B', 'C', 'D');
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.
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();
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.
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();
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.
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();
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
);
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.
120 ScopedMockLog
log(kDoNotCaptureLogsYet
);
122 EXPECT_CALL(log
, Log(INFO
, _
, "Unsupported QuicTag version: FAKE")).Times(1);
124 log
.StartCapturingLogs();
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
;
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());
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
;
185 EXPECT_TRUE(queue
.RemoveUpTo(51));
186 EXPECT_FALSE(queue
.RemoveUpTo(51));
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());
202 EXPECT_EQ(52u, queue
.Min());
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());
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
;
230 // Tests that the iterators returned from a packet queue iterate over the queue.
231 TEST(PacketNumberQueueTest
, Iterators
) {
232 PacketNumberQueue queue
;
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
);