1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "net/quic/quic_protocol.h"
#include <sstream>
#include "base/stl_util.h"
#include "net/quic/quic_utils.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace net {
namespace test {
namespace {
TEST(QuicProtocolTest, AdjustErrorForVersion) {
ASSERT_EQ(14, QUIC_STREAM_LAST_ERROR)
<< "Any additions to QuicRstStreamErrorCode require an addition to "
<< "AdjustErrorForVersion and this associated test.";
// If we ever add different RST codes, we should have a test akin to the
// following.
// EXPECT_EQ(QUIC_RST_ACKNOWLEDGEMENT, AdjustErrorForVersion(
// QUIC_RST_ACKNOWLEDGEMENT,
// QUIC_VERSION_28));
}
TEST(QuicProtocolTest, MakeQuicTag) {
QuicTag tag = MakeQuicTag('A', 'B', 'C', 'D');
char bytes[4];
memcpy(bytes, &tag, 4);
EXPECT_EQ('A', bytes[0]);
EXPECT_EQ('B', bytes[1]);
EXPECT_EQ('C', bytes[2]);
EXPECT_EQ('D', bytes[3]);
}
TEST(QuicProtocolTest, IsAawaitingPacket) {
QuicAckFrame ack_frame;
ack_frame.largest_observed = 10u;
EXPECT_TRUE(IsAwaitingPacket(ack_frame, 11u));
EXPECT_FALSE(IsAwaitingPacket(ack_frame, 1u));
ack_frame.missing_packets.Add(10);
EXPECT_TRUE(IsAwaitingPacket(ack_frame, 10u));
}
TEST(QuicProtocolTest, QuicDeprecatedErrorCodeCount) {
// If you deprecated any QuicErrorCode, you will need to update the
// deprecated QuicErrorCode count. Otherwise this test will fail.
int num_deprecated_errors = 0;
std::string invalid_error_code = "INVALID_ERROR_CODE";
for (int i = 0; i < QUIC_LAST_ERROR; ++i) {
if (QuicUtils::ErrorToString(static_cast<QuicErrorCode>(i)) ==
invalid_error_code) {
++num_deprecated_errors;
}
}
EXPECT_EQ(kDeprecatedQuicErrorCount, num_deprecated_errors);
}
TEST(QuicProtocolTest, QuicVersionToQuicTag) {
// If you add a new version to the QuicVersion enum you will need to add a new
// case to QuicVersionToQuicTag, otherwise this test will fail.
// TODO(rtenneti): Enable checking of Log(ERROR) messages.
#if 0
// Any logs would indicate an unsupported version which we don't expect.
ScopedMockLog log(kDoNotCaptureLogsYet);
EXPECT_CALL(log, Log(_, _, _)).Times(0);
log.StartCapturingLogs();
#endif
// Explicitly test a specific version.
EXPECT_EQ(MakeQuicTag('Q', '0', '2', '5'),
QuicVersionToQuicTag(QUIC_VERSION_25));
// Loop over all supported versions and make sure that we never hit the
// default case (i.e. all supported versions should be successfully converted
// to valid QuicTags).
for (size_t i = 0; i < arraysize(kSupportedQuicVersions); ++i) {
QuicVersion version = kSupportedQuicVersions[i];
EXPECT_LT(0u, QuicVersionToQuicTag(version));
}
}
TEST(QuicProtocolTest, QuicVersionToQuicTagUnsupported) {
// TODO(rtenneti): Enable checking of Log(ERROR) messages.
#if 0
// TODO(rjshade): Change to DFATAL once we actually support multiple versions,
// and QuicConnectionTest::SendVersionNegotiationPacket can be changed to use
// mis-matched versions rather than relying on QUIC_VERSION_UNSUPPORTED.
ScopedMockLog log(kDoNotCaptureLogsYet);
EXPECT_CALL(log, Log(base_logging::ERROR, _, "Unsupported QuicVersion: 0"))
.Times(1);
log.StartCapturingLogs();
#endif
EXPECT_EQ(0u, QuicVersionToQuicTag(QUIC_VERSION_UNSUPPORTED));
}
TEST(QuicProtocolTest, QuicTagToQuicVersion) {
// If you add a new version to the QuicVersion enum you will need to add a new
// case to QuicTagToQuicVersion, otherwise this test will fail.
// TODO(rtenneti): Enable checking of Log(ERROR) messages.
#if 0
// Any logs would indicate an unsupported version which we don't expect.
ScopedMockLog log(kDoNotCaptureLogsYet);
EXPECT_CALL(log, Log(_, _, _)).Times(0);
log.StartCapturingLogs();
#endif
// Explicitly test specific versions.
EXPECT_EQ(QUIC_VERSION_25,
QuicTagToQuicVersion(MakeQuicTag('Q', '0', '2', '5')));
for (size_t i = 0; i < arraysize(kSupportedQuicVersions); ++i) {
QuicVersion version = kSupportedQuicVersions[i];
// Get the tag from the version (we can loop over QuicVersions easily).
QuicTag tag = QuicVersionToQuicTag(version);
EXPECT_LT(0u, tag);
// Now try converting back.
QuicVersion tag_to_quic_version = QuicTagToQuicVersion(tag);
EXPECT_EQ(version, tag_to_quic_version);
EXPECT_NE(QUIC_VERSION_UNSUPPORTED, tag_to_quic_version);
}
}
TEST(QuicProtocolTest, QuicTagToQuicVersionUnsupported) {
// TODO(rtenneti): Enable checking of Log(ERROR) messages.
#if 0
ScopedMockLog log(kDoNotCaptureLogsYet);
#ifndef NDEBUG
EXPECT_CALL(log,
Log(base_logging::INFO, _, "Unsupported QuicTag version: FAKE"))
.Times(1);
#endif
log.StartCapturingLogs();
#endif
EXPECT_EQ(QUIC_VERSION_UNSUPPORTED,
QuicTagToQuicVersion(MakeQuicTag('F', 'A', 'K', 'E')));
}
TEST(QuicProtocolTest, QuicVersionToString) {
EXPECT_EQ("QUIC_VERSION_25", QuicVersionToString(QUIC_VERSION_25));
EXPECT_EQ("QUIC_VERSION_UNSUPPORTED",
QuicVersionToString(QUIC_VERSION_UNSUPPORTED));
QuicVersion single_version[] = {QUIC_VERSION_25};
QuicVersionVector versions_vector;
for (size_t i = 0; i < arraysize(single_version); ++i) {
versions_vector.push_back(single_version[i]);
}
EXPECT_EQ("QUIC_VERSION_25", QuicVersionVectorToString(versions_vector));
QuicVersion multiple_versions[] = {QUIC_VERSION_UNSUPPORTED, QUIC_VERSION_25};
versions_vector.clear();
for (size_t i = 0; i < arraysize(multiple_versions); ++i) {
versions_vector.push_back(multiple_versions[i]);
}
EXPECT_EQ("QUIC_VERSION_UNSUPPORTED,QUIC_VERSION_25",
QuicVersionVectorToString(versions_vector));
// Make sure that all supported versions are present in QuicVersionToString.
for (size_t i = 0; i < arraysize(kSupportedQuicVersions); ++i) {
QuicVersion version = kSupportedQuicVersions[i];
EXPECT_NE("QUIC_VERSION_UNSUPPORTED", QuicVersionToString(version));
}
}
// Tests that a queue contains the expected data after calls to Add().
TEST(PacketNumberQueueTest, AddRange) {
PacketNumberQueue queue;
queue.Add(1, 51);
queue.Add(53);
EXPECT_FALSE(queue.Contains(0));
for (int i = 1; i < 51; ++i) {
EXPECT_TRUE(queue.Contains(i));
}
EXPECT_FALSE(queue.Contains(51));
EXPECT_FALSE(queue.Contains(52));
EXPECT_TRUE(queue.Contains(53));
EXPECT_FALSE(queue.Contains(54));
EXPECT_EQ(51u, queue.NumPacketsSlow());
EXPECT_EQ(1u, queue.Min());
EXPECT_EQ(53u, queue.Max());
queue.Add(70);
EXPECT_EQ(70u, queue.Max());
}
// Tests that a queue contains the expected data after calls to Remove().
TEST(PacketNumberQueueTest, Removal) {
PacketNumberQueue queue;
queue.Add(0, 100);
EXPECT_TRUE(queue.RemoveUpTo(51));
EXPECT_FALSE(queue.RemoveUpTo(51));
queue.Remove(53);
EXPECT_FALSE(queue.Contains(0));
for (int i = 1; i < 51; ++i) {
EXPECT_FALSE(queue.Contains(i));
}
EXPECT_TRUE(queue.Contains(51));
EXPECT_TRUE(queue.Contains(52));
EXPECT_FALSE(queue.Contains(53));
EXPECT_TRUE(queue.Contains(54));
EXPECT_EQ(48u, queue.NumPacketsSlow());
EXPECT_EQ(51u, queue.Min());
EXPECT_EQ(99u, queue.Max());
queue.Remove(51);
EXPECT_EQ(52u, queue.Min());
queue.Remove(99);
EXPECT_EQ(98u, queue.Max());
}
// Tests that a queue is empty when all of its elements are removed.
TEST(PacketNumberQueueTest, Empty) {
PacketNumberQueue queue;
EXPECT_TRUE(queue.Empty());
EXPECT_EQ(0u, queue.NumPacketsSlow());
queue.Add(1, 100);
EXPECT_TRUE(queue.RemoveUpTo(100));
EXPECT_TRUE(queue.Empty());
EXPECT_EQ(0u, queue.NumPacketsSlow());
}
// Tests that logging the state of a PacketNumberQueue does not crash.
TEST(PacketNumberQueueTest, LogDoesNotCrash) {
std::ostringstream oss;
PacketNumberQueue queue;
oss << queue;
queue.Add(1);
queue.Add(50, 100);
oss << queue;
}
// Tests that the iterators returned from a packet queue iterate over the queue.
TEST(PacketNumberQueueTest, Iterators) {
PacketNumberQueue queue;
queue.Add(1, 100);
const std::vector<QuicPacketNumber> actual(queue.begin(), queue.end());
std::vector<QuicPacketNumber> expected;
for (int i = 1; i < 100; ++i) {
expected.push_back(i);
}
EXPECT_EQ(expected, actual);
PacketNumberQueue::const_iterator it_low = queue.lower_bound(10);
EXPECT_EQ(10u, *it_low);
it_low = queue.lower_bound(101);
EXPECT_TRUE(queue.end() == it_low);
}
} // namespace
} // namespace test
} // namespace net
|