Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(88)

Side by Side Diff: net/quic/quic_protocol_test.cc

Issue 2193073003: Move shared files in net/quic/ into net/quic/core/ (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: io_thread_unittest.cc Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « net/quic/quic_protocol.cc ('k') | net/quic/quic_received_packet_manager.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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.
4
5 #include "net/quic/quic_protocol.h"
6
7 #include <sstream>
8
9 #include "base/stl_util.h"
10 #include "net/quic/quic_flags.h"
11 #include "net/quic/quic_utils.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13
14 namespace net {
15 namespace test {
16 namespace {
17
18 TEST(QuicProtocolTest, AdjustErrorForVersion) {
19 ASSERT_EQ(14, QUIC_STREAM_LAST_ERROR)
20 << "Any additions to QuicRstStreamErrorCode require an addition to "
21 << "AdjustErrorForVersion and this associated test.";
22
23 // If we ever add different RST codes, we should have a test akin to the
24 // following.
25 // EXPECT_EQ(QUIC_RST_ACKNOWLEDGEMENT, AdjustErrorForVersion(
26 // QUIC_RST_ACKNOWLEDGEMENT,
27 // QUIC_VERSION_28));
28 }
29
30 TEST(QuicProtocolTest, MakeQuicTag) {
31 QuicTag tag = MakeQuicTag('A', 'B', 'C', 'D');
32 char bytes[4];
33 memcpy(bytes, &tag, 4);
34 EXPECT_EQ('A', bytes[0]);
35 EXPECT_EQ('B', bytes[1]);
36 EXPECT_EQ('C', bytes[2]);
37 EXPECT_EQ('D', bytes[3]);
38 }
39
40 TEST(QuicProtocolTest, IsAawaitingPacket) {
41 QuicAckFrame ack_frame;
42 ack_frame.largest_observed = 10u;
43 EXPECT_TRUE(IsAwaitingPacket(ack_frame, 11u, 0u));
44 EXPECT_FALSE(IsAwaitingPacket(ack_frame, 1u, 0u));
45
46 ack_frame.packets.Add(10);
47 EXPECT_TRUE(IsAwaitingPacket(ack_frame, 10u, 0u));
48
49 QuicAckFrame ack_frame1;
50 ack_frame1.missing = false;
51 ack_frame1.largest_observed = 10u;
52 ack_frame1.packets.Add(1, 11);
53 EXPECT_TRUE(IsAwaitingPacket(ack_frame1, 11u, 0u));
54 EXPECT_FALSE(IsAwaitingPacket(ack_frame1, 1u, 0u));
55
56 ack_frame1.packets.Remove(10);
57 EXPECT_TRUE(IsAwaitingPacket(ack_frame1, 10u, 0u));
58
59 QuicAckFrame ack_frame2;
60 ack_frame2.missing = false;
61 ack_frame2.largest_observed = 100u;
62 ack_frame2.packets.Add(21, 100);
63 EXPECT_FALSE(IsAwaitingPacket(ack_frame2, 11u, 20u));
64 EXPECT_FALSE(IsAwaitingPacket(ack_frame2, 80u, 20u));
65 EXPECT_TRUE(IsAwaitingPacket(ack_frame2, 101u, 20u));
66
67 ack_frame2.packets.Remove(50);
68 EXPECT_TRUE(IsAwaitingPacket(ack_frame2, 50u, 20u));
69 }
70
71 TEST(QuicProtocolTest, QuicVersionToQuicTag) {
72 // If you add a new version to the QuicVersion enum you will need to add a new
73 // case to QuicVersionToQuicTag, otherwise this test will fail.
74
75 // TODO(rtenneti): Enable checking of Log(ERROR) messages.
76 #if 0
77 // Any logs would indicate an unsupported version which we don't expect.
78 ScopedMockLog log(kDoNotCaptureLogsYet);
79 EXPECT_CALL(log, Log(_, _, _)).Times(0);
80 log.StartCapturingLogs();
81 #endif
82
83 // Explicitly test a specific version.
84 EXPECT_EQ(MakeQuicTag('Q', '0', '3', '0'),
85 QuicVersionToQuicTag(QUIC_VERSION_30));
86
87 // Loop over all supported versions and make sure that we never hit the
88 // default case (i.e. all supported versions should be successfully converted
89 // to valid QuicTags).
90 for (size_t i = 0; i < arraysize(kSupportedQuicVersions); ++i) {
91 QuicVersion version = kSupportedQuicVersions[i];
92 EXPECT_LT(0u, QuicVersionToQuicTag(version));
93 }
94 }
95
96 TEST(QuicProtocolTest, QuicVersionToQuicTagUnsupported) {
97 // TODO(rtenneti): Enable checking of Log(ERROR) messages.
98 #if 0
99 // TODO(rjshade): Change to DFATAL once we actually support multiple versions,
100 // and QuicConnectionTest::SendVersionNegotiationPacket can be changed to use
101 // mis-matched versions rather than relying on QUIC_VERSION_UNSUPPORTED.
102 ScopedMockLog log(kDoNotCaptureLogsYet);
103 EXPECT_CALL(log, Log(base_logging::ERROR, _, "Unsupported QuicVersion: 0"))
104 .Times(1);
105 log.StartCapturingLogs();
106 #endif
107
108 EXPECT_EQ(0u, QuicVersionToQuicTag(QUIC_VERSION_UNSUPPORTED));
109 }
110
111 TEST(QuicProtocolTest, QuicTagToQuicVersion) {
112 // If you add a new version to the QuicVersion enum you will need to add a new
113 // case to QuicTagToQuicVersion, otherwise this test will fail.
114
115 // TODO(rtenneti): Enable checking of Log(ERROR) messages.
116 #if 0
117 // Any logs would indicate an unsupported version which we don't expect.
118 ScopedMockLog log(kDoNotCaptureLogsYet);
119 EXPECT_CALL(log, Log(_, _, _)).Times(0);
120 log.StartCapturingLogs();
121 #endif
122
123 // Explicitly test specific versions.
124 EXPECT_EQ(QUIC_VERSION_30,
125 QuicTagToQuicVersion(MakeQuicTag('Q', '0', '3', '0')));
126
127 for (size_t i = 0; i < arraysize(kSupportedQuicVersions); ++i) {
128 QuicVersion version = kSupportedQuicVersions[i];
129
130 // Get the tag from the version (we can loop over QuicVersions easily).
131 QuicTag tag = QuicVersionToQuicTag(version);
132 EXPECT_LT(0u, tag);
133
134 // Now try converting back.
135 QuicVersion tag_to_quic_version = QuicTagToQuicVersion(tag);
136 EXPECT_EQ(version, tag_to_quic_version);
137 EXPECT_NE(QUIC_VERSION_UNSUPPORTED, tag_to_quic_version);
138 }
139 }
140
141 TEST(QuicProtocolTest, QuicTagToQuicVersionUnsupported) {
142 // TODO(rtenneti): Enable checking of Log(ERROR) messages.
143 #if 0
144 ScopedMockLog log(kDoNotCaptureLogsYet);
145 #ifndef NDEBUG
146 EXPECT_CALL(log,
147 Log(base_logging::INFO, _, "Unsupported QuicTag version: FAKE"))
148 .Times(1);
149 #endif
150 log.StartCapturingLogs();
151 #endif
152
153 EXPECT_EQ(QUIC_VERSION_UNSUPPORTED,
154 QuicTagToQuicVersion(MakeQuicTag('F', 'A', 'K', 'E')));
155 }
156
157 TEST(QuicProtocolTest, QuicVersionToString) {
158 EXPECT_EQ("QUIC_VERSION_30", QuicVersionToString(QUIC_VERSION_30));
159 EXPECT_EQ("QUIC_VERSION_UNSUPPORTED",
160 QuicVersionToString(QUIC_VERSION_UNSUPPORTED));
161
162 QuicVersion single_version[] = {QUIC_VERSION_30};
163 QuicVersionVector versions_vector;
164 for (size_t i = 0; i < arraysize(single_version); ++i) {
165 versions_vector.push_back(single_version[i]);
166 }
167 EXPECT_EQ("QUIC_VERSION_30", QuicVersionVectorToString(versions_vector));
168
169 QuicVersion multiple_versions[] = {QUIC_VERSION_UNSUPPORTED, QUIC_VERSION_30};
170 versions_vector.clear();
171 for (size_t i = 0; i < arraysize(multiple_versions); ++i) {
172 versions_vector.push_back(multiple_versions[i]);
173 }
174 EXPECT_EQ("QUIC_VERSION_UNSUPPORTED,QUIC_VERSION_30",
175 QuicVersionVectorToString(versions_vector));
176
177 // Make sure that all supported versions are present in QuicVersionToString.
178 for (size_t i = 0; i < arraysize(kSupportedQuicVersions); ++i) {
179 QuicVersion version = kSupportedQuicVersions[i];
180 EXPECT_NE("QUIC_VERSION_UNSUPPORTED", QuicVersionToString(version));
181 }
182 }
183
184 TEST(QuicProtocolTest, AckFrameToString) {
185 QuicAckFrame frame;
186 frame.entropy_hash = 1;
187 frame.largest_observed = 2;
188 frame.ack_delay_time = QuicTime::Delta::FromMicroseconds(3);
189 frame.packets.Add(4);
190 frame.packets.Add(5);
191 frame.received_packet_times = {
192 {6, QuicTime::Zero() + QuicTime::Delta::FromMicroseconds(7)}};
193 std::ostringstream stream;
194 stream << frame;
195 EXPECT_EQ(
196 "{ entropy_hash: 1, largest_observed: 2, ack_delay_time: 3, "
197 "packets: [ 4 5 ], is_truncated: 0, received_packets: [ 6 at 7 ] }\n",
198 stream.str());
199 }
200
201 TEST(QuicProtocolTest, PaddingFrameToString) {
202 QuicPaddingFrame frame;
203 frame.num_padding_bytes = 1;
204 std::ostringstream stream;
205 stream << frame;
206 EXPECT_EQ("{ num_padding_bytes: 1 }\n", stream.str());
207 }
208
209 TEST(QuicProtocolTest, RstStreamFrameToString) {
210 QuicRstStreamFrame frame;
211 frame.stream_id = 1;
212 frame.error_code = QUIC_STREAM_CANCELLED;
213 std::ostringstream stream;
214 stream << frame;
215 EXPECT_EQ("{ stream_id: 1, error_code: 6 }\n", stream.str());
216 }
217
218 TEST(QuicProtocolTest, ConnectionCloseFrameToString) {
219 QuicConnectionCloseFrame frame;
220 frame.error_code = QUIC_NETWORK_IDLE_TIMEOUT;
221 frame.error_details = "No recent network activity.";
222 std::ostringstream stream;
223 stream << frame;
224 EXPECT_EQ(
225 "{ error_code: 25, error_details: 'No recent network activity.' }\n",
226 stream.str());
227 }
228
229 TEST(QuicProtocolTest, GoAwayFrameToString) {
230 QuicGoAwayFrame frame;
231 frame.error_code = QUIC_NETWORK_IDLE_TIMEOUT;
232 frame.last_good_stream_id = 2;
233 frame.reason_phrase = "Reason";
234 std::ostringstream stream;
235 stream << frame;
236 EXPECT_EQ(
237 "{ error_code: 25, last_good_stream_id: 2, reason_phrase: 'Reason' }\n",
238 stream.str());
239 }
240
241 TEST(QuicProtocolTest, WindowUpdateFrameToString) {
242 QuicWindowUpdateFrame frame;
243 std::ostringstream stream;
244 frame.stream_id = 1;
245 frame.byte_offset = 2;
246 stream << frame;
247 EXPECT_EQ("{ stream_id: 1, byte_offset: 2 }\n", stream.str());
248 }
249
250 TEST(QuicProtocolTest, BlockedFrameToString) {
251 QuicBlockedFrame frame;
252 frame.stream_id = 1;
253 std::ostringstream stream;
254 stream << frame;
255 EXPECT_EQ("{ stream_id: 1 }\n", stream.str());
256 }
257
258 TEST(QuicProtocolTest, StreamFrameToString) {
259 QuicStreamFrame frame;
260 frame.stream_id = 1;
261 frame.fin = false;
262 frame.offset = 2;
263 frame.data_length = 3;
264 std::ostringstream stream;
265 stream << frame;
266 EXPECT_EQ("{ stream_id: 1, fin: 0, offset: 2, length: 3 }\n", stream.str());
267 }
268
269 TEST(QuicProtocolTest, StopWaitingFrameToString) {
270 QuicStopWaitingFrame frame;
271 frame.entropy_hash = 1;
272 frame.least_unacked = 2;
273 std::ostringstream stream;
274 stream << frame;
275 EXPECT_EQ("{ entropy_hash: 1, least_unacked: 2 }\n", stream.str());
276 }
277
278 TEST(QuicProtocolTest, PathCloseFrameToString) {
279 QuicPathCloseFrame frame;
280 frame.path_id = 1;
281 std::ostringstream stream;
282 stream << frame;
283 EXPECT_EQ("{ path_id: 1 }\n", stream.str());
284 }
285
286 TEST(QuicProtocolTest, FilterSupportedVersions) {
287 QuicVersionVector all_versions = {
288 QUIC_VERSION_30, QUIC_VERSION_31, QUIC_VERSION_32, QUIC_VERSION_33,
289 QUIC_VERSION_34, QUIC_VERSION_35, QUIC_VERSION_36};
290
291 FLAGS_quic_enable_version_35 = false;
292 FLAGS_quic_enable_version_36 = false;
293
294 QuicVersionVector filtered_versions = FilterSupportedVersions(all_versions);
295 ASSERT_EQ(5u, filtered_versions.size());
296 EXPECT_EQ(QUIC_VERSION_30, filtered_versions[0]);
297 EXPECT_EQ(QUIC_VERSION_31, filtered_versions[1]);
298 EXPECT_EQ(QUIC_VERSION_32, filtered_versions[2]);
299 EXPECT_EQ(QUIC_VERSION_33, filtered_versions[3]);
300 EXPECT_EQ(QUIC_VERSION_34, filtered_versions[4]);
301 }
302
303 // Tests that a queue contains the expected data after calls to Add().
304 TEST(PacketNumberQueueTest, AddRange) {
305 PacketNumberQueue queue;
306 queue.Add(1, 51);
307 queue.Add(53);
308
309 EXPECT_FALSE(queue.Contains(0));
310 for (int i = 1; i < 51; ++i) {
311 EXPECT_TRUE(queue.Contains(i));
312 }
313 EXPECT_FALSE(queue.Contains(51));
314 EXPECT_FALSE(queue.Contains(52));
315 EXPECT_TRUE(queue.Contains(53));
316 EXPECT_FALSE(queue.Contains(54));
317 EXPECT_EQ(51u, queue.NumPacketsSlow());
318 EXPECT_EQ(1u, queue.Min());
319 EXPECT_EQ(53u, queue.Max());
320
321 queue.Add(70);
322 EXPECT_EQ(70u, queue.Max());
323 }
324
325 // Tests that a queue contains the expected data after calls to Remove().
326 TEST(PacketNumberQueueTest, Removal) {
327 PacketNumberQueue queue;
328 queue.Add(0, 100);
329
330 EXPECT_TRUE(queue.RemoveUpTo(51));
331 EXPECT_FALSE(queue.RemoveUpTo(51));
332 queue.Remove(53);
333
334 EXPECT_FALSE(queue.Contains(0));
335 for (int i = 1; i < 51; ++i) {
336 EXPECT_FALSE(queue.Contains(i));
337 }
338 EXPECT_TRUE(queue.Contains(51));
339 EXPECT_TRUE(queue.Contains(52));
340 EXPECT_FALSE(queue.Contains(53));
341 EXPECT_TRUE(queue.Contains(54));
342 EXPECT_EQ(48u, queue.NumPacketsSlow());
343 EXPECT_EQ(51u, queue.Min());
344 EXPECT_EQ(99u, queue.Max());
345
346 queue.Remove(51);
347 EXPECT_EQ(52u, queue.Min());
348 queue.Remove(99);
349 EXPECT_EQ(98u, queue.Max());
350 }
351
352 // Tests that a queue is empty when all of its elements are removed.
353 TEST(PacketNumberQueueTest, Empty) {
354 PacketNumberQueue queue;
355 EXPECT_TRUE(queue.Empty());
356 EXPECT_EQ(0u, queue.NumPacketsSlow());
357
358 queue.Add(1, 100);
359 EXPECT_TRUE(queue.RemoveUpTo(100));
360 EXPECT_TRUE(queue.Empty());
361 EXPECT_EQ(0u, queue.NumPacketsSlow());
362 }
363
364 // Tests that logging the state of a PacketNumberQueue does not crash.
365 TEST(PacketNumberQueueTest, LogDoesNotCrash) {
366 std::ostringstream oss;
367 PacketNumberQueue queue;
368 oss << queue;
369
370 queue.Add(1);
371 queue.Add(50, 100);
372 oss << queue;
373 }
374
375 // Tests that the iterators returned from a packet queue iterate over the queue.
376 TEST(PacketNumberQueueTest, Iterators) {
377 PacketNumberQueue queue;
378 queue.Add(1, 100);
379
380 const std::vector<QuicPacketNumber> actual_numbers(queue.begin(),
381 queue.end());
382 const std::vector<Interval<QuicPacketNumber>> actual_intervals(
383 queue.begin_intervals(), queue.end_intervals());
384
385 std::vector<QuicPacketNumber> expected_numbers;
386 for (int i = 1; i < 100; ++i) {
387 expected_numbers.push_back(i);
388 }
389
390 std::vector<Interval<QuicPacketNumber>> expected_intervals;
391 expected_intervals.push_back(Interval<QuicPacketNumber>(1, 100));
392
393 EXPECT_EQ(expected_intervals, actual_intervals);
394 EXPECT_EQ(expected_numbers, actual_numbers);
395
396 PacketNumberQueue::const_iterator it_low = queue.lower_bound(10);
397 EXPECT_EQ(10u, *it_low);
398
399 it_low = queue.lower_bound(101);
400 EXPECT_TRUE(queue.end() == it_low);
401 }
402
403 TEST(PacketNumberQueueTest, IntervalLengthAndRemoveInterval) {
404 PacketNumberQueue queue;
405 queue.Add(1, 10);
406 queue.Add(20, 30);
407 queue.Add(40, 50);
408 EXPECT_EQ(3u, queue.NumIntervals());
409 EXPECT_EQ(10u, queue.LastIntervalLength());
410 queue.Remove(9, 21);
411 EXPECT_EQ(3u, queue.NumIntervals());
412 EXPECT_FALSE(queue.Contains(9));
413 EXPECT_FALSE(queue.Contains(20));
414 }
415
416 TEST(PacketNumberQueueTest, Complement) {
417 PacketNumberQueue queue;
418 queue.Add(1, 10);
419 queue.Add(12, 20);
420 queue.Add(22, 30);
421 queue.Complement();
422 EXPECT_EQ(2u, queue.NumIntervals());
423 EXPECT_TRUE(queue.Contains(10));
424 EXPECT_TRUE(queue.Contains(11));
425 EXPECT_TRUE(queue.Contains(20));
426 EXPECT_TRUE(queue.Contains(21));
427 }
428
429 } // namespace
430 } // namespace test
431 } // namespace net
OLDNEW
« no previous file with comments | « net/quic/quic_protocol.cc ('k') | net/quic/quic_received_packet_manager.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698