Initialize UserMetricsRecorder on Windows Ash and Ozone
[chromium-blink-merge.git] / net / tools / quic / quic_time_wait_list_manager_test.cc
blob4e5d7c34c4f269103c57a22789cf5cc87285861c
1 // Copyright 2013 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/tools/quic/quic_time_wait_list_manager.h"
7 #include <errno.h>
9 #include "net/quic/crypto/crypto_protocol.h"
10 #include "net/quic/crypto/null_encrypter.h"
11 #include "net/quic/crypto/quic_decrypter.h"
12 #include "net/quic/crypto/quic_encrypter.h"
13 #include "net/quic/quic_data_reader.h"
14 #include "net/quic/quic_framer.h"
15 #include "net/quic/quic_packet_writer.h"
16 #include "net/quic/quic_protocol.h"
17 #include "net/quic/quic_utils.h"
18 #include "net/quic/test_tools/quic_test_utils.h"
19 #include "net/tools/quic/test_tools/mock_epoll_server.h"
20 #include "net/tools/quic/test_tools/quic_test_utils.h"
21 #include "testing/gmock/include/gmock/gmock.h"
22 #include "testing/gtest/include/gtest/gtest.h"
24 using net::test::BuildUnsizedDataPacket;
25 using net::test::NoOpFramerVisitor;
26 using net::test::QuicVersionMax;
27 using net::test::QuicVersionMin;
28 using testing::Args;
29 using testing::Assign;
30 using testing::DoAll;
31 using testing::Matcher;
32 using testing::MatcherInterface;
33 using testing::NiceMock;
34 using testing::Return;
35 using testing::ReturnPointee;
36 using testing::SetArgPointee;
37 using testing::StrictMock;
38 using testing::Truly;
39 using testing::_;
41 namespace net {
42 namespace tools {
43 namespace test {
45 class FramerVisitorCapturingPublicReset : public NoOpFramerVisitor {
46 public:
47 FramerVisitorCapturingPublicReset() {}
48 ~FramerVisitorCapturingPublicReset() override {}
50 void OnPublicResetPacket(const QuicPublicResetPacket& public_reset) override {
51 public_reset_packet_ = public_reset;
54 const QuicPublicResetPacket public_reset_packet() {
55 return public_reset_packet_;
58 private:
59 QuicPublicResetPacket public_reset_packet_;
62 class QuicTimeWaitListManagerPeer {
63 public:
64 static bool ShouldSendResponse(QuicTimeWaitListManager* manager,
65 int received_packet_count) {
66 return manager->ShouldSendResponse(received_packet_count);
69 static QuicTime::Delta time_wait_period(QuicTimeWaitListManager* manager) {
70 return manager->kTimeWaitPeriod_;
73 static QuicVersion GetQuicVersionFromConnectionId(
74 QuicTimeWaitListManager* manager,
75 QuicConnectionId connection_id) {
76 return manager->GetQuicVersionFromConnectionId(connection_id);
80 namespace {
82 class MockFakeTimeEpollServer : public FakeTimeEpollServer {
83 public:
84 MOCK_METHOD2(RegisterAlarm, void(int64 timeout_in_us,
85 EpollAlarmCallbackInterface* alarm));
88 class QuicTimeWaitListManagerTest : public ::testing::Test {
89 protected:
90 QuicTimeWaitListManagerTest()
91 : time_wait_list_manager_(&writer_, &visitor_,
92 &epoll_server_, QuicSupportedVersions()),
93 framer_(QuicSupportedVersions(), QuicTime::Zero(), true),
94 connection_id_(45),
95 client_address_(net::test::TestPeerIPAddress(), kTestPort),
96 writer_is_blocked_(false) {}
98 ~QuicTimeWaitListManagerTest() override {}
100 void SetUp() override {
101 EXPECT_CALL(writer_, IsWriteBlocked())
102 .WillRepeatedly(ReturnPointee(&writer_is_blocked_));
103 EXPECT_CALL(writer_, IsWriteBlockedDataBuffered())
104 .WillRepeatedly(Return(false));
107 void AddConnectionId(QuicConnectionId connection_id) {
108 AddConnectionId(connection_id, QuicVersionMax(), nullptr);
111 void AddConnectionId(QuicConnectionId connection_id,
112 QuicVersion version,
113 QuicEncryptedPacket* packet) {
114 time_wait_list_manager_.AddConnectionIdToTimeWait(
115 connection_id, version, packet);
118 bool IsConnectionIdInTimeWait(QuicConnectionId connection_id) {
119 return time_wait_list_manager_.IsConnectionIdInTimeWait(connection_id);
122 void ProcessPacket(QuicConnectionId connection_id,
123 QuicPacketSequenceNumber sequence_number) {
124 QuicEncryptedPacket packet(nullptr, 0);
125 time_wait_list_manager_.ProcessPacket(server_address_,
126 client_address_,
127 connection_id,
128 sequence_number,
129 packet);
132 QuicEncryptedPacket* ConstructEncryptedPacket(
133 EncryptionLevel level,
134 QuicConnectionId connection_id,
135 QuicPacketSequenceNumber sequence_number) {
136 QuicPacketHeader header;
137 header.public_header.connection_id = connection_id;
138 header.public_header.connection_id_length = PACKET_8BYTE_CONNECTION_ID;
139 header.public_header.version_flag = false;
140 header.public_header.reset_flag = false;
141 header.public_header.sequence_number_length = PACKET_6BYTE_SEQUENCE_NUMBER;
142 header.packet_sequence_number = sequence_number;
143 header.entropy_flag = false;
144 header.entropy_hash = 0;
145 header.fec_flag = false;
146 header.is_in_fec_group = NOT_IN_FEC_GROUP;
147 header.fec_group = 0;
148 QuicStreamFrame stream_frame(1, false, 0, MakeIOVector("data"));
149 QuicFrame frame(&stream_frame);
150 QuicFrames frames;
151 frames.push_back(frame);
152 scoped_ptr<QuicPacket> packet(
153 BuildUnsizedDataPacket(&framer_, header, frames).packet);
154 EXPECT_TRUE(packet != nullptr);
155 QuicEncryptedPacket* encrypted = framer_.EncryptPacket(ENCRYPTION_NONE,
156 sequence_number,
157 *packet);
158 EXPECT_TRUE(encrypted != nullptr);
159 return encrypted;
162 NiceMock<MockFakeTimeEpollServer> epoll_server_;
163 StrictMock<MockPacketWriter> writer_;
164 StrictMock<MockQuicServerSessionVisitor> visitor_;
165 QuicTimeWaitListManager time_wait_list_manager_;
166 QuicFramer framer_;
167 QuicConnectionId connection_id_;
168 IPEndPoint server_address_;
169 IPEndPoint client_address_;
170 bool writer_is_blocked_;
173 class ValidatePublicResetPacketPredicate
174 : public MatcherInterface<const std::tr1::tuple<const char*, int> > {
175 public:
176 explicit ValidatePublicResetPacketPredicate(QuicConnectionId connection_id,
177 QuicPacketSequenceNumber number)
178 : connection_id_(connection_id), sequence_number_(number) {
181 virtual bool MatchAndExplain(
182 const std::tr1::tuple<const char*, int> packet_buffer,
183 testing::MatchResultListener* /* listener */) const override {
184 FramerVisitorCapturingPublicReset visitor;
185 QuicFramer framer(QuicSupportedVersions(),
186 QuicTime::Zero(),
187 false);
188 framer.set_visitor(&visitor);
189 QuicEncryptedPacket encrypted(std::tr1::get<0>(packet_buffer),
190 std::tr1::get<1>(packet_buffer));
191 framer.ProcessPacket(encrypted);
192 QuicPublicResetPacket packet = visitor.public_reset_packet();
193 return connection_id_ == packet.public_header.connection_id &&
194 packet.public_header.reset_flag && !packet.public_header.version_flag &&
195 sequence_number_ == packet.rejected_sequence_number &&
196 net::test::TestPeerIPAddress() == packet.client_address.address() &&
197 kTestPort == packet.client_address.port();
200 virtual void DescribeTo(::std::ostream* os) const override {}
202 virtual void DescribeNegationTo(::std::ostream* os) const override {}
204 private:
205 QuicConnectionId connection_id_;
206 QuicPacketSequenceNumber sequence_number_;
210 Matcher<const std::tr1::tuple<const char*, int> > PublicResetPacketEq(
211 QuicConnectionId connection_id,
212 QuicPacketSequenceNumber sequence_number) {
213 return MakeMatcher(new ValidatePublicResetPacketPredicate(connection_id,
214 sequence_number));
217 TEST_F(QuicTimeWaitListManagerTest, CheckConnectionIdInTimeWait) {
218 EXPECT_FALSE(IsConnectionIdInTimeWait(connection_id_));
219 AddConnectionId(connection_id_);
220 EXPECT_TRUE(IsConnectionIdInTimeWait(connection_id_));
223 TEST_F(QuicTimeWaitListManagerTest, SendConnectionClose) {
224 size_t kConnectionCloseLength = 100;
225 AddConnectionId(
226 connection_id_,
227 QuicVersionMax(),
228 new QuicEncryptedPacket(
229 new char[kConnectionCloseLength], kConnectionCloseLength, true));
230 const int kRandomSequenceNumber = 1;
231 EXPECT_CALL(writer_, WritePacket(_, kConnectionCloseLength,
232 server_address_.address(),
233 client_address_))
234 .WillOnce(Return(WriteResult(WRITE_STATUS_OK, 1)));
236 ProcessPacket(connection_id_, kRandomSequenceNumber);
239 TEST_F(QuicTimeWaitListManagerTest, SendPublicReset) {
240 AddConnectionId(connection_id_);
241 const int kRandomSequenceNumber = 1;
242 EXPECT_CALL(writer_, WritePacket(_, _,
243 server_address_.address(),
244 client_address_))
245 .With(Args<0, 1>(PublicResetPacketEq(connection_id_,
246 kRandomSequenceNumber)))
247 .WillOnce(Return(WriteResult(WRITE_STATUS_OK, 0)));
249 ProcessPacket(connection_id_, kRandomSequenceNumber);
252 TEST_F(QuicTimeWaitListManagerTest, SendPublicResetWithExponentialBackOff) {
253 AddConnectionId(connection_id_);
254 for (int sequence_number = 1; sequence_number < 101; ++sequence_number) {
255 if ((sequence_number & (sequence_number - 1)) == 0) {
256 EXPECT_CALL(writer_, WritePacket(_, _, _, _))
257 .WillOnce(Return(WriteResult(WRITE_STATUS_OK, 1)));
259 ProcessPacket(connection_id_, sequence_number);
260 // Send public reset with exponential back off.
261 if ((sequence_number & (sequence_number - 1)) == 0) {
262 EXPECT_TRUE(QuicTimeWaitListManagerPeer::ShouldSendResponse(
263 &time_wait_list_manager_, sequence_number));
264 } else {
265 EXPECT_FALSE(QuicTimeWaitListManagerPeer::ShouldSendResponse(
266 &time_wait_list_manager_, sequence_number));
271 TEST_F(QuicTimeWaitListManagerTest, CleanUpOldConnectionIds) {
272 const int kConnectionIdCount = 100;
273 const int kOldConnectionIdCount = 31;
275 // Add connection_ids such that their expiry time is kTimeWaitPeriod_.
276 epoll_server_.set_now_in_usec(0);
277 for (int connection_id = 1;
278 connection_id <= kOldConnectionIdCount;
279 ++connection_id) {
280 AddConnectionId(connection_id);
283 // Add remaining connection_ids such that their add time is
284 // 2 * kTimeWaitPeriod.
285 const QuicTime::Delta time_wait_period =
286 QuicTimeWaitListManagerPeer::time_wait_period(&time_wait_list_manager_);
287 epoll_server_.set_now_in_usec(time_wait_period.ToMicroseconds());
288 for (int connection_id = kOldConnectionIdCount + 1;
289 connection_id <= kConnectionIdCount;
290 ++connection_id) {
291 AddConnectionId(connection_id);
294 QuicTime::Delta offset = QuicTime::Delta::FromMicroseconds(39);
295 // Now set the current time as time_wait_period + offset usecs.
296 epoll_server_.set_now_in_usec(time_wait_period.Add(offset).ToMicroseconds());
297 // After all the old connection_ids are cleaned up, check the next alarm
298 // interval.
299 int64 next_alarm_time = epoll_server_.ApproximateNowInUsec() +
300 time_wait_period.Subtract(offset).ToMicroseconds();
301 EXPECT_CALL(epoll_server_, RegisterAlarm(next_alarm_time, _));
303 time_wait_list_manager_.CleanUpOldConnectionIds();
304 for (int connection_id = 1;
305 connection_id <= kConnectionIdCount;
306 ++connection_id) {
307 EXPECT_EQ(connection_id > kOldConnectionIdCount,
308 IsConnectionIdInTimeWait(connection_id))
309 << "kOldConnectionIdCount: " << kOldConnectionIdCount
310 << " connection_id: " << connection_id;
314 TEST_F(QuicTimeWaitListManagerTest, SendQueuedPackets) {
315 QuicConnectionId connection_id = 1;
316 AddConnectionId(connection_id);
317 QuicPacketSequenceNumber sequence_number = 234;
318 scoped_ptr<QuicEncryptedPacket> packet(ConstructEncryptedPacket(
319 ENCRYPTION_NONE, connection_id, sequence_number));
320 // Let first write through.
321 EXPECT_CALL(writer_, WritePacket(_, _,
322 server_address_.address(),
323 client_address_))
324 .With(Args<0, 1>(PublicResetPacketEq(connection_id,
325 sequence_number)))
326 .WillOnce(Return(WriteResult(WRITE_STATUS_OK, packet->length())));
327 ProcessPacket(connection_id, sequence_number);
329 // write block for the next packet.
330 EXPECT_CALL(writer_, WritePacket(_, _,
331 server_address_.address(),
332 client_address_))
333 .With(Args<0, 1>(PublicResetPacketEq(connection_id,
334 sequence_number)))
335 .WillOnce(DoAll(
336 Assign(&writer_is_blocked_, true),
337 Return(WriteResult(WRITE_STATUS_BLOCKED, EAGAIN))));
338 EXPECT_CALL(visitor_, OnWriteBlocked(&time_wait_list_manager_));
339 ProcessPacket(connection_id, sequence_number);
340 // 3rd packet. No public reset should be sent;
341 ProcessPacket(connection_id, sequence_number);
343 // write packet should not be called since we are write blocked but the
344 // should be queued.
345 QuicConnectionId other_connection_id = 2;
346 AddConnectionId(other_connection_id);
347 QuicPacketSequenceNumber other_sequence_number = 23423;
348 scoped_ptr<QuicEncryptedPacket> other_packet(
349 ConstructEncryptedPacket(
350 ENCRYPTION_NONE, other_connection_id, other_sequence_number));
351 EXPECT_CALL(writer_, WritePacket(_, _, _, _))
352 .Times(0);
353 EXPECT_CALL(visitor_, OnWriteBlocked(&time_wait_list_manager_));
354 ProcessPacket(other_connection_id, other_sequence_number);
356 // Now expect all the write blocked public reset packets to be sent again.
357 writer_is_blocked_ = false;
358 EXPECT_CALL(writer_, WritePacket(_, _,
359 server_address_.address(),
360 client_address_))
361 .With(Args<0, 1>(PublicResetPacketEq(connection_id,
362 sequence_number)))
363 .WillOnce(Return(WriteResult(WRITE_STATUS_OK, packet->length())));
364 EXPECT_CALL(writer_, WritePacket(_, _,
365 server_address_.address(),
366 client_address_))
367 .With(Args<0, 1>(PublicResetPacketEq(other_connection_id,
368 other_sequence_number)))
369 .WillOnce(Return(WriteResult(WRITE_STATUS_OK,
370 other_packet->length())));
371 time_wait_list_manager_.OnCanWrite();
374 TEST_F(QuicTimeWaitListManagerTest, GetQuicVersionFromMap) {
375 const int kConnectionId1 = 123;
376 const int kConnectionId2 = 456;
377 const int kConnectionId3 = 789;
379 AddConnectionId(kConnectionId1, QuicVersionMin(), nullptr);
380 AddConnectionId(kConnectionId2, QuicVersionMax(), nullptr);
381 AddConnectionId(kConnectionId3, QuicVersionMax(), nullptr);
383 EXPECT_EQ(QuicVersionMin(),
384 QuicTimeWaitListManagerPeer::GetQuicVersionFromConnectionId(
385 &time_wait_list_manager_, kConnectionId1));
386 EXPECT_EQ(QuicVersionMax(),
387 QuicTimeWaitListManagerPeer::GetQuicVersionFromConnectionId(
388 &time_wait_list_manager_, kConnectionId2));
389 EXPECT_EQ(QuicVersionMax(),
390 QuicTimeWaitListManagerPeer::GetQuicVersionFromConnectionId(
391 &time_wait_list_manager_, kConnectionId3));
394 TEST_F(QuicTimeWaitListManagerTest, AddConnectionIdTwice) {
395 // Add connection_ids such that their expiry time is kTimeWaitPeriod_.
396 epoll_server_.set_now_in_usec(0);
397 AddConnectionId(connection_id_);
398 EXPECT_TRUE(IsConnectionIdInTimeWait(connection_id_));
399 size_t kConnectionCloseLength = 100;
400 AddConnectionId(
401 connection_id_,
402 QuicVersionMax(),
403 new QuicEncryptedPacket(
404 new char[kConnectionCloseLength], kConnectionCloseLength, true));
405 EXPECT_TRUE(IsConnectionIdInTimeWait(connection_id_));
407 EXPECT_CALL(writer_, WritePacket(_,
408 kConnectionCloseLength,
409 server_address_.address(),
410 client_address_))
411 .WillOnce(Return(WriteResult(WRITE_STATUS_OK, 1)));
413 const int kRandomSequenceNumber = 1;
414 ProcessPacket(connection_id_, kRandomSequenceNumber);
416 const QuicTime::Delta time_wait_period =
417 QuicTimeWaitListManagerPeer::time_wait_period(&time_wait_list_manager_);
419 QuicTime::Delta offset = QuicTime::Delta::FromMicroseconds(39);
420 // Now set the current time as time_wait_period + offset usecs.
421 epoll_server_.set_now_in_usec(time_wait_period.Add(offset).ToMicroseconds());
422 // After the connection_ids are cleaned up, check the next alarm interval.
423 int64 next_alarm_time = epoll_server_.ApproximateNowInUsec() +
424 time_wait_period.ToMicroseconds();
426 EXPECT_CALL(epoll_server_, RegisterAlarm(next_alarm_time, _));
427 time_wait_list_manager_.CleanUpOldConnectionIds();
428 EXPECT_FALSE(IsConnectionIdInTimeWait(connection_id_));
431 TEST_F(QuicTimeWaitListManagerTest, ConnectionIdsOrderedByTime) {
432 // Simple randomization: the values of connection_ids are swapped based on the
433 // current seconds on the clock. If the container is broken, the test will be
434 // 50% flaky.
435 int odd_second = static_cast<int>(epoll_server_.ApproximateNowInUsec()) % 2;
436 EXPECT_TRUE(odd_second == 0 || odd_second == 1);
437 const QuicConnectionId kConnectionId1 = odd_second;
438 const QuicConnectionId kConnectionId2 = 1 - odd_second;
440 // 1 will hash lower than 2, but we add it later. They should come out in the
441 // add order, not hash order.
442 epoll_server_.set_now_in_usec(0);
443 AddConnectionId(kConnectionId1);
444 epoll_server_.set_now_in_usec(10);
445 AddConnectionId(kConnectionId2);
447 const QuicTime::Delta time_wait_period =
448 QuicTimeWaitListManagerPeer::time_wait_period(&time_wait_list_manager_);
449 epoll_server_.set_now_in_usec(time_wait_period.ToMicroseconds() + 1);
451 EXPECT_CALL(epoll_server_, RegisterAlarm(_, _));
453 time_wait_list_manager_.CleanUpOldConnectionIds();
454 EXPECT_FALSE(IsConnectionIdInTimeWait(kConnectionId1));
455 EXPECT_TRUE(IsConnectionIdInTimeWait(kConnectionId2));
457 } // namespace
458 } // namespace test
459 } // namespace tools
460 } // namespace net