OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2016 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/core/quic_protocol.h" | 5 #include "net/quic/core/frames/quic_frame.h" |
6 | 6 |
7 #include <sstream> | 7 #include "testing/gmock/include/gmock/gmock.h" |
8 | |
9 #include "base/stl_util.h" | |
10 #include "net/quic/core/quic_flags.h" | |
11 #include "net/quic/core/quic_utils.h" | |
12 #include "net/quic/test_tools/quic_test_utils.h" | |
13 #include "testing/gtest/include/gtest/gtest.h" | 8 #include "testing/gtest/include/gtest/gtest.h" |
14 | 9 |
15 namespace net { | 10 namespace net { |
16 namespace test { | 11 namespace test { |
17 namespace { | 12 namespace { |
18 | 13 |
19 TEST(QuicProtocolTest, MakeQuicTag) { | 14 using testing::_; |
20 QuicTag tag = MakeQuicTag('A', 'B', 'C', 'D'); | |
21 char bytes[4]; | |
22 memcpy(bytes, &tag, 4); | |
23 EXPECT_EQ('A', bytes[0]); | |
24 EXPECT_EQ('B', bytes[1]); | |
25 EXPECT_EQ('C', bytes[2]); | |
26 EXPECT_EQ('D', bytes[3]); | |
27 } | |
28 | 15 |
29 TEST(QuicProtocolTest, IsAwaitingPacket) { | 16 TEST(QuicFramesTest, AckFrameToString) { |
30 QuicAckFrame ack_frame1; | |
31 ack_frame1.largest_observed = 10u; | |
32 ack_frame1.packets.Add(1, 11); | |
33 EXPECT_TRUE(IsAwaitingPacket(ack_frame1, 11u, 0u)); | |
34 EXPECT_FALSE(IsAwaitingPacket(ack_frame1, 1u, 0u)); | |
35 | |
36 ack_frame1.packets.Remove(10); | |
37 EXPECT_TRUE(IsAwaitingPacket(ack_frame1, 10u, 0u)); | |
38 | |
39 QuicAckFrame ack_frame2; | |
40 ack_frame2.largest_observed = 100u; | |
41 ack_frame2.packets.Add(21, 100); | |
42 EXPECT_FALSE(IsAwaitingPacket(ack_frame2, 11u, 20u)); | |
43 EXPECT_FALSE(IsAwaitingPacket(ack_frame2, 80u, 20u)); | |
44 EXPECT_TRUE(IsAwaitingPacket(ack_frame2, 101u, 20u)); | |
45 | |
46 ack_frame2.packets.Remove(50); | |
47 EXPECT_TRUE(IsAwaitingPacket(ack_frame2, 50u, 20u)); | |
48 } | |
49 | |
50 TEST(QuicProtocolTest, AckFrameToString) { | |
51 QuicAckFrame frame; | 17 QuicAckFrame frame; |
52 frame.largest_observed = 2; | 18 frame.largest_observed = 2; |
53 frame.ack_delay_time = QuicTime::Delta::FromMicroseconds(3); | 19 frame.ack_delay_time = QuicTime::Delta::FromMicroseconds(3); |
54 frame.packets.Add(4); | 20 frame.packets.Add(4); |
55 frame.packets.Add(5); | 21 frame.packets.Add(5); |
56 frame.received_packet_times = { | 22 frame.received_packet_times = { |
57 {6, QuicTime::Zero() + QuicTime::Delta::FromMicroseconds(7)}}; | 23 {6, QuicTime::Zero() + QuicTime::Delta::FromMicroseconds(7)}}; |
58 std::ostringstream stream; | 24 std::ostringstream stream; |
59 stream << frame; | 25 stream << frame; |
60 EXPECT_EQ( | 26 EXPECT_EQ( |
61 "{ largest_observed: 2, ack_delay_time: 3, " | 27 "{ largest_observed: 2, ack_delay_time: 3, " |
62 "packets: [ 4 5 ], received_packets: [ 6 at 7 ] }\n", | 28 "packets: [ 4 5 ], received_packets: [ 6 at 7 ] }\n", |
63 stream.str()); | 29 stream.str()); |
64 } | 30 } |
65 | 31 |
66 TEST(QuicProtocolTest, PaddingFrameToString) { | 32 TEST(QuicFramesTest, PaddingFrameToString) { |
67 QuicPaddingFrame frame; | 33 QuicPaddingFrame frame; |
68 frame.num_padding_bytes = 1; | 34 frame.num_padding_bytes = 1; |
69 std::ostringstream stream; | 35 std::ostringstream stream; |
70 stream << frame; | 36 stream << frame; |
71 EXPECT_EQ("{ num_padding_bytes: 1 }\n", stream.str()); | 37 EXPECT_EQ("{ num_padding_bytes: 1 }\n", stream.str()); |
72 } | 38 } |
73 | 39 |
74 TEST(QuicProtocolTest, RstStreamFrameToString) { | 40 TEST(QuicFramesTest, RstStreamFrameToString) { |
75 QuicRstStreamFrame frame; | 41 QuicRstStreamFrame frame; |
76 frame.stream_id = 1; | 42 frame.stream_id = 1; |
77 frame.error_code = QUIC_STREAM_CANCELLED; | 43 frame.error_code = QUIC_STREAM_CANCELLED; |
78 std::ostringstream stream; | 44 std::ostringstream stream; |
79 stream << frame; | 45 stream << frame; |
80 EXPECT_EQ("{ stream_id: 1, error_code: 6 }\n", stream.str()); | 46 EXPECT_EQ("{ stream_id: 1, error_code: 6 }\n", stream.str()); |
81 } | 47 } |
82 | 48 |
83 TEST(QuicProtocolTest, ConnectionCloseFrameToString) { | 49 TEST(QuicFramesTest, ConnectionCloseFrameToString) { |
84 QuicConnectionCloseFrame frame; | 50 QuicConnectionCloseFrame frame; |
85 frame.error_code = QUIC_NETWORK_IDLE_TIMEOUT; | 51 frame.error_code = QUIC_NETWORK_IDLE_TIMEOUT; |
86 frame.error_details = "No recent network activity."; | 52 frame.error_details = "No recent network activity."; |
87 std::ostringstream stream; | 53 std::ostringstream stream; |
88 stream << frame; | 54 stream << frame; |
89 EXPECT_EQ( | 55 EXPECT_EQ( |
90 "{ error_code: 25, error_details: 'No recent network activity.' }\n", | 56 "{ error_code: 25, error_details: 'No recent network activity.' }\n", |
91 stream.str()); | 57 stream.str()); |
92 } | 58 } |
93 | 59 |
94 TEST(QuicProtocolTest, GoAwayFrameToString) { | 60 TEST(QuicFramesTest, GoAwayFrameToString) { |
95 QuicGoAwayFrame frame; | 61 QuicGoAwayFrame frame; |
96 frame.error_code = QUIC_NETWORK_IDLE_TIMEOUT; | 62 frame.error_code = QUIC_NETWORK_IDLE_TIMEOUT; |
97 frame.last_good_stream_id = 2; | 63 frame.last_good_stream_id = 2; |
98 frame.reason_phrase = "Reason"; | 64 frame.reason_phrase = "Reason"; |
99 std::ostringstream stream; | 65 std::ostringstream stream; |
100 stream << frame; | 66 stream << frame; |
101 EXPECT_EQ( | 67 EXPECT_EQ( |
102 "{ error_code: 25, last_good_stream_id: 2, reason_phrase: 'Reason' }\n", | 68 "{ error_code: 25, last_good_stream_id: 2, reason_phrase: 'Reason' }\n", |
103 stream.str()); | 69 stream.str()); |
104 } | 70 } |
105 | 71 |
106 TEST(QuicProtocolTest, WindowUpdateFrameToString) { | 72 TEST(QuicFramesTest, WindowUpdateFrameToString) { |
107 QuicWindowUpdateFrame frame; | 73 QuicWindowUpdateFrame frame; |
108 std::ostringstream stream; | 74 std::ostringstream stream; |
109 frame.stream_id = 1; | 75 frame.stream_id = 1; |
110 frame.byte_offset = 2; | 76 frame.byte_offset = 2; |
111 stream << frame; | 77 stream << frame; |
112 EXPECT_EQ("{ stream_id: 1, byte_offset: 2 }\n", stream.str()); | 78 EXPECT_EQ("{ stream_id: 1, byte_offset: 2 }\n", stream.str()); |
113 } | 79 } |
114 | 80 |
115 TEST(QuicProtocolTest, BlockedFrameToString) { | 81 TEST(QuicFramesTest, BlockedFrameToString) { |
116 QuicBlockedFrame frame; | 82 QuicBlockedFrame frame; |
117 frame.stream_id = 1; | 83 frame.stream_id = 1; |
118 std::ostringstream stream; | 84 std::ostringstream stream; |
119 stream << frame; | 85 stream << frame; |
120 EXPECT_EQ("{ stream_id: 1 }\n", stream.str()); | 86 EXPECT_EQ("{ stream_id: 1 }\n", stream.str()); |
121 } | 87 } |
122 | 88 |
123 TEST(QuicProtocolTest, StreamFrameToString) { | 89 TEST(QuicFramesTest, StreamFrameToString) { |
124 QuicStreamFrame frame; | 90 QuicStreamFrame frame; |
125 frame.stream_id = 1; | 91 frame.stream_id = 1; |
126 frame.fin = false; | 92 frame.fin = false; |
127 frame.offset = 2; | 93 frame.offset = 2; |
128 frame.data_length = 3; | 94 frame.data_length = 3; |
129 std::ostringstream stream; | 95 std::ostringstream stream; |
130 stream << frame; | 96 stream << frame; |
131 EXPECT_EQ("{ stream_id: 1, fin: 0, offset: 2, length: 3 }\n", stream.str()); | 97 EXPECT_EQ("{ stream_id: 1, fin: 0, offset: 2, length: 3 }\n", stream.str()); |
132 } | 98 } |
133 | 99 |
134 TEST(QuicProtocolTest, StopWaitingFrameToString) { | 100 TEST(QuicFramesTest, StopWaitingFrameToString) { |
135 QuicStopWaitingFrame frame; | 101 QuicStopWaitingFrame frame; |
136 frame.least_unacked = 2; | 102 frame.least_unacked = 2; |
137 std::ostringstream stream; | 103 std::ostringstream stream; |
138 stream << frame; | 104 stream << frame; |
139 EXPECT_EQ("{ least_unacked: 2 }\n", stream.str()); | 105 EXPECT_EQ("{ least_unacked: 2 }\n", stream.str()); |
140 } | 106 } |
141 | 107 |
142 TEST(QuicProtocolTest, PathCloseFrameToString) { | 108 TEST(QuicFramesTest, PathCloseFrameToString) { |
143 QuicPathCloseFrame frame; | 109 QuicPathCloseFrame frame; |
144 frame.path_id = 1; | 110 frame.path_id = 1; |
145 std::ostringstream stream; | 111 std::ostringstream stream; |
146 stream << frame; | 112 stream << frame; |
147 EXPECT_EQ("{ path_id: 1 }\n", stream.str()); | 113 EXPECT_EQ("{ path_id: 1 }\n", stream.str()); |
148 } | 114 } |
149 | 115 |
150 TEST(QuicProtocolTest, QuicVersionManager) { | 116 TEST(QuicFramesTest, IsAwaitingPacket) { |
151 QuicFlagSaver flags; | 117 QuicAckFrame ack_frame1; |
152 FLAGS_quic_enable_version_36_v3 = false; | 118 ack_frame1.largest_observed = 10u; |
153 QuicVersionManager manager(AllSupportedVersions()); | 119 ack_frame1.packets.Add(1, 11); |
154 EXPECT_EQ(FilterSupportedVersions(AllSupportedVersions()), | 120 EXPECT_TRUE(IsAwaitingPacket(ack_frame1, 11u, 0u)); |
155 manager.GetSupportedVersions()); | 121 EXPECT_FALSE(IsAwaitingPacket(ack_frame1, 1u, 0u)); |
156 FLAGS_quic_enable_version_36_v3 = true; | 122 |
157 EXPECT_EQ(FilterSupportedVersions(AllSupportedVersions()), | 123 ack_frame1.packets.Remove(10); |
158 manager.GetSupportedVersions()); | 124 EXPECT_TRUE(IsAwaitingPacket(ack_frame1, 10u, 0u)); |
159 EXPECT_EQ(QUIC_VERSION_36, manager.GetSupportedVersions()[0]); | 125 |
160 EXPECT_EQ(QUIC_VERSION_35, manager.GetSupportedVersions()[1]); | 126 QuicAckFrame ack_frame2; |
| 127 ack_frame2.largest_observed = 100u; |
| 128 ack_frame2.packets.Add(21, 100); |
| 129 EXPECT_FALSE(IsAwaitingPacket(ack_frame2, 11u, 20u)); |
| 130 EXPECT_FALSE(IsAwaitingPacket(ack_frame2, 80u, 20u)); |
| 131 EXPECT_TRUE(IsAwaitingPacket(ack_frame2, 101u, 20u)); |
| 132 |
| 133 ack_frame2.packets.Remove(50); |
| 134 EXPECT_TRUE(IsAwaitingPacket(ack_frame2, 50u, 20u)); |
161 } | 135 } |
162 | 136 |
163 // Tests that a queue contains the expected data after calls to Add(). | 137 // Tests that a queue contains the expected data after calls to Add(). |
164 TEST(PacketNumberQueueTest, AddRange) { | 138 TEST(PacketNumberQueueTest, AddRange) { |
165 PacketNumberQueue queue; | 139 PacketNumberQueue queue; |
166 queue.Add(1, 51); | 140 queue.Add(1, 51); |
167 queue.Add(53); | 141 queue.Add(53); |
168 | 142 |
169 EXPECT_FALSE(queue.Contains(0)); | 143 EXPECT_FALSE(queue.Contains(0)); |
170 for (int i = 1; i < 51; ++i) { | 144 for (int i = 1; i < 51; ++i) { |
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
291 EXPECT_EQ(2u, queue.NumIntervals()); | 265 EXPECT_EQ(2u, queue.NumIntervals()); |
292 EXPECT_TRUE(queue.Contains(10)); | 266 EXPECT_TRUE(queue.Contains(10)); |
293 EXPECT_TRUE(queue.Contains(11)); | 267 EXPECT_TRUE(queue.Contains(11)); |
294 EXPECT_TRUE(queue.Contains(20)); | 268 EXPECT_TRUE(queue.Contains(20)); |
295 EXPECT_TRUE(queue.Contains(21)); | 269 EXPECT_TRUE(queue.Contains(21)); |
296 } | 270 } |
297 | 271 |
298 } // namespace | 272 } // namespace |
299 } // namespace test | 273 } // namespace test |
300 } // namespace net | 274 } // namespace net |
OLD | NEW |