| OLD | NEW |
| (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/core/quic_protocol.h" | |
| 6 | |
| 7 #include <sstream> | |
| 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" | |
| 14 | |
| 15 namespace net { | |
| 16 namespace test { | |
| 17 namespace { | |
| 18 | |
| 19 TEST(QuicProtocolTest, MakeQuicTag) { | |
| 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 | |
| 29 TEST(QuicProtocolTest, IsAwaitingPacket) { | |
| 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; | |
| 52 frame.largest_observed = 2; | |
| 53 frame.ack_delay_time = QuicTime::Delta::FromMicroseconds(3); | |
| 54 frame.packets.Add(4); | |
| 55 frame.packets.Add(5); | |
| 56 frame.received_packet_times = { | |
| 57 {6, QuicTime::Zero() + QuicTime::Delta::FromMicroseconds(7)}}; | |
| 58 std::ostringstream stream; | |
| 59 stream << frame; | |
| 60 EXPECT_EQ( | |
| 61 "{ largest_observed: 2, ack_delay_time: 3, " | |
| 62 "packets: [ 4 5 ], received_packets: [ 6 at 7 ] }\n", | |
| 63 stream.str()); | |
| 64 } | |
| 65 | |
| 66 TEST(QuicProtocolTest, PaddingFrameToString) { | |
| 67 QuicPaddingFrame frame; | |
| 68 frame.num_padding_bytes = 1; | |
| 69 std::ostringstream stream; | |
| 70 stream << frame; | |
| 71 EXPECT_EQ("{ num_padding_bytes: 1 }\n", stream.str()); | |
| 72 } | |
| 73 | |
| 74 TEST(QuicProtocolTest, RstStreamFrameToString) { | |
| 75 QuicRstStreamFrame frame; | |
| 76 frame.stream_id = 1; | |
| 77 frame.error_code = QUIC_STREAM_CANCELLED; | |
| 78 std::ostringstream stream; | |
| 79 stream << frame; | |
| 80 EXPECT_EQ("{ stream_id: 1, error_code: 6 }\n", stream.str()); | |
| 81 } | |
| 82 | |
| 83 TEST(QuicProtocolTest, ConnectionCloseFrameToString) { | |
| 84 QuicConnectionCloseFrame frame; | |
| 85 frame.error_code = QUIC_NETWORK_IDLE_TIMEOUT; | |
| 86 frame.error_details = "No recent network activity."; | |
| 87 std::ostringstream stream; | |
| 88 stream << frame; | |
| 89 EXPECT_EQ( | |
| 90 "{ error_code: 25, error_details: 'No recent network activity.' }\n", | |
| 91 stream.str()); | |
| 92 } | |
| 93 | |
| 94 TEST(QuicProtocolTest, GoAwayFrameToString) { | |
| 95 QuicGoAwayFrame frame; | |
| 96 frame.error_code = QUIC_NETWORK_IDLE_TIMEOUT; | |
| 97 frame.last_good_stream_id = 2; | |
| 98 frame.reason_phrase = "Reason"; | |
| 99 std::ostringstream stream; | |
| 100 stream << frame; | |
| 101 EXPECT_EQ( | |
| 102 "{ error_code: 25, last_good_stream_id: 2, reason_phrase: 'Reason' }\n", | |
| 103 stream.str()); | |
| 104 } | |
| 105 | |
| 106 TEST(QuicProtocolTest, WindowUpdateFrameToString) { | |
| 107 QuicWindowUpdateFrame frame; | |
| 108 std::ostringstream stream; | |
| 109 frame.stream_id = 1; | |
| 110 frame.byte_offset = 2; | |
| 111 stream << frame; | |
| 112 EXPECT_EQ("{ stream_id: 1, byte_offset: 2 }\n", stream.str()); | |
| 113 } | |
| 114 | |
| 115 TEST(QuicProtocolTest, BlockedFrameToString) { | |
| 116 QuicBlockedFrame frame; | |
| 117 frame.stream_id = 1; | |
| 118 std::ostringstream stream; | |
| 119 stream << frame; | |
| 120 EXPECT_EQ("{ stream_id: 1 }\n", stream.str()); | |
| 121 } | |
| 122 | |
| 123 TEST(QuicProtocolTest, StreamFrameToString) { | |
| 124 QuicStreamFrame frame; | |
| 125 frame.stream_id = 1; | |
| 126 frame.fin = false; | |
| 127 frame.offset = 2; | |
| 128 frame.data_length = 3; | |
| 129 std::ostringstream stream; | |
| 130 stream << frame; | |
| 131 EXPECT_EQ("{ stream_id: 1, fin: 0, offset: 2, length: 3 }\n", stream.str()); | |
| 132 } | |
| 133 | |
| 134 TEST(QuicProtocolTest, StopWaitingFrameToString) { | |
| 135 QuicStopWaitingFrame frame; | |
| 136 frame.least_unacked = 2; | |
| 137 std::ostringstream stream; | |
| 138 stream << frame; | |
| 139 EXPECT_EQ("{ least_unacked: 2 }\n", stream.str()); | |
| 140 } | |
| 141 | |
| 142 TEST(QuicProtocolTest, PathCloseFrameToString) { | |
| 143 QuicPathCloseFrame frame; | |
| 144 frame.path_id = 1; | |
| 145 std::ostringstream stream; | |
| 146 stream << frame; | |
| 147 EXPECT_EQ("{ path_id: 1 }\n", stream.str()); | |
| 148 } | |
| 149 | |
| 150 // Tests that a queue contains the expected data after calls to Add(). | |
| 151 TEST(PacketNumberQueueTest, AddRange) { | |
| 152 PacketNumberQueue queue; | |
| 153 queue.Add(1, 51); | |
| 154 queue.Add(53); | |
| 155 | |
| 156 EXPECT_FALSE(queue.Contains(0)); | |
| 157 for (int i = 1; i < 51; ++i) { | |
| 158 EXPECT_TRUE(queue.Contains(i)); | |
| 159 } | |
| 160 EXPECT_FALSE(queue.Contains(51)); | |
| 161 EXPECT_FALSE(queue.Contains(52)); | |
| 162 EXPECT_TRUE(queue.Contains(53)); | |
| 163 EXPECT_FALSE(queue.Contains(54)); | |
| 164 EXPECT_EQ(51u, queue.NumPacketsSlow()); | |
| 165 EXPECT_EQ(1u, queue.Min()); | |
| 166 EXPECT_EQ(53u, queue.Max()); | |
| 167 | |
| 168 queue.Add(70); | |
| 169 EXPECT_EQ(70u, queue.Max()); | |
| 170 } | |
| 171 | |
| 172 // Tests that a queue contains the expected data after calls to Remove(). | |
| 173 TEST(PacketNumberQueueTest, Removal) { | |
| 174 PacketNumberQueue queue; | |
| 175 queue.Add(0, 100); | |
| 176 | |
| 177 EXPECT_TRUE(queue.RemoveUpTo(51)); | |
| 178 EXPECT_FALSE(queue.RemoveUpTo(51)); | |
| 179 queue.Remove(53); | |
| 180 | |
| 181 EXPECT_FALSE(queue.Contains(0)); | |
| 182 for (int i = 1; i < 51; ++i) { | |
| 183 EXPECT_FALSE(queue.Contains(i)); | |
| 184 } | |
| 185 EXPECT_TRUE(queue.Contains(51)); | |
| 186 EXPECT_TRUE(queue.Contains(52)); | |
| 187 EXPECT_FALSE(queue.Contains(53)); | |
| 188 EXPECT_TRUE(queue.Contains(54)); | |
| 189 EXPECT_EQ(48u, queue.NumPacketsSlow()); | |
| 190 EXPECT_EQ(51u, queue.Min()); | |
| 191 EXPECT_EQ(99u, queue.Max()); | |
| 192 | |
| 193 queue.Remove(51); | |
| 194 EXPECT_EQ(52u, queue.Min()); | |
| 195 queue.Remove(99); | |
| 196 EXPECT_EQ(98u, queue.Max()); | |
| 197 } | |
| 198 | |
| 199 // Tests that a queue is empty when all of its elements are removed. | |
| 200 TEST(PacketNumberQueueTest, Empty) { | |
| 201 PacketNumberQueue queue; | |
| 202 EXPECT_TRUE(queue.Empty()); | |
| 203 EXPECT_EQ(0u, queue.NumPacketsSlow()); | |
| 204 | |
| 205 queue.Add(1, 100); | |
| 206 EXPECT_TRUE(queue.RemoveUpTo(100)); | |
| 207 EXPECT_TRUE(queue.Empty()); | |
| 208 EXPECT_EQ(0u, queue.NumPacketsSlow()); | |
| 209 } | |
| 210 | |
| 211 // Tests that logging the state of a PacketNumberQueue does not crash. | |
| 212 TEST(PacketNumberQueueTest, LogDoesNotCrash) { | |
| 213 std::ostringstream oss; | |
| 214 PacketNumberQueue queue; | |
| 215 oss << queue; | |
| 216 | |
| 217 queue.Add(1); | |
| 218 queue.Add(50, 100); | |
| 219 oss << queue; | |
| 220 } | |
| 221 | |
| 222 // Tests that the iterators returned from a packet queue iterate over the queue. | |
| 223 TEST(PacketNumberQueueTest, Iterators) { | |
| 224 PacketNumberQueue queue; | |
| 225 queue.Add(1, 100); | |
| 226 | |
| 227 const std::vector<Interval<QuicPacketNumber>> actual_intervals(queue.begin(), | |
| 228 queue.end()); | |
| 229 | |
| 230 std::vector<Interval<QuicPacketNumber>> expected_intervals; | |
| 231 expected_intervals.push_back(Interval<QuicPacketNumber>(1, 100)); | |
| 232 | |
| 233 EXPECT_EQ(expected_intervals, actual_intervals); | |
| 234 } | |
| 235 | |
| 236 TEST(PacketNumberQueueTest, LowerBoundEquals) { | |
| 237 PacketNumberQueue queue; | |
| 238 queue.Add(1, 100); | |
| 239 | |
| 240 PacketNumberQueue::const_iterator it = queue.lower_bound(10); | |
| 241 ASSERT_NE(queue.end(), it); | |
| 242 EXPECT_TRUE(it->Contains(10u)); | |
| 243 | |
| 244 it = queue.lower_bound(101); | |
| 245 EXPECT_TRUE(queue.end() == it); | |
| 246 } | |
| 247 | |
| 248 TEST(PacketNumberQueueTest, LowerBoundGreater) { | |
| 249 PacketNumberQueue queue; | |
| 250 queue.Add(15, 25); | |
| 251 queue.Add(50, 100); | |
| 252 | |
| 253 PacketNumberQueue::const_iterator it = queue.lower_bound(10); | |
| 254 ASSERT_NE(queue.end(), it); | |
| 255 EXPECT_EQ(15u, it->min()); | |
| 256 EXPECT_EQ(25u, it->max()); | |
| 257 } | |
| 258 | |
| 259 TEST(PacketNumberQueueTest, IntervalLengthAndRemoveInterval) { | |
| 260 PacketNumberQueue queue; | |
| 261 queue.Add(1, 10); | |
| 262 queue.Add(20, 30); | |
| 263 queue.Add(40, 50); | |
| 264 EXPECT_EQ(3u, queue.NumIntervals()); | |
| 265 EXPECT_EQ(10u, queue.LastIntervalLength()); | |
| 266 queue.Remove(9, 21); | |
| 267 EXPECT_EQ(3u, queue.NumIntervals()); | |
| 268 EXPECT_FALSE(queue.Contains(9)); | |
| 269 EXPECT_FALSE(queue.Contains(20)); | |
| 270 } | |
| 271 | |
| 272 TEST(PacketNumberQueueTest, Complement) { | |
| 273 PacketNumberQueue queue; | |
| 274 queue.Add(1, 10); | |
| 275 queue.Add(12, 20); | |
| 276 queue.Add(22, 30); | |
| 277 queue.Complement(); | |
| 278 EXPECT_EQ(2u, queue.NumIntervals()); | |
| 279 EXPECT_TRUE(queue.Contains(10)); | |
| 280 EXPECT_TRUE(queue.Contains(11)); | |
| 281 EXPECT_TRUE(queue.Contains(20)); | |
| 282 EXPECT_TRUE(queue.Contains(21)); | |
| 283 } | |
| 284 | |
| 285 } // namespace | |
| 286 } // namespace test | |
| 287 } // namespace net | |
| OLD | NEW |