| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 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/tools/quic/quic_time_wait_list_manager.h" | 5 #include "net/tools/quic/quic_time_wait_list_manager.h" |
| 6 | 6 |
| 7 #include <errno.h> | 7 #include <errno.h> |
| 8 | 8 |
| 9 #include "net/quic/crypto/crypto_protocol.h" | 9 #include "net/quic/crypto/crypto_protocol.h" |
| 10 #include "net/quic/crypto/null_encrypter.h" | 10 #include "net/quic/crypto/null_encrypter.h" |
| 11 #include "net/quic/crypto/quic_decrypter.h" | 11 #include "net/quic/crypto/quic_decrypter.h" |
| 12 #include "net/quic/crypto/quic_encrypter.h" | 12 #include "net/quic/crypto/quic_encrypter.h" |
| 13 #include "net/quic/quic_connection_helper.h" |
| 13 #include "net/quic/quic_data_reader.h" | 14 #include "net/quic/quic_data_reader.h" |
| 14 #include "net/quic/quic_flags.h" | 15 #include "net/quic/quic_flags.h" |
| 15 #include "net/quic/quic_framer.h" | 16 #include "net/quic/quic_framer.h" |
| 16 #include "net/quic/quic_packet_writer.h" | 17 #include "net/quic/quic_packet_writer.h" |
| 17 #include "net/quic/quic_protocol.h" | 18 #include "net/quic/quic_protocol.h" |
| 18 #include "net/quic/quic_utils.h" | 19 #include "net/quic/quic_utils.h" |
| 19 #include "net/quic/test_tools/quic_test_utils.h" | 20 #include "net/quic/test_tools/quic_test_utils.h" |
| 21 #include "net/tools/quic/quic_epoll_connection_helper.h" |
| 20 #include "net/tools/quic/test_tools/mock_epoll_server.h" | 22 #include "net/tools/quic/test_tools/mock_epoll_server.h" |
| 21 #include "net/tools/quic/test_tools/quic_test_utils.h" | 23 #include "net/tools/quic/test_tools/quic_test_utils.h" |
| 22 #include "testing/gmock/include/gmock/gmock.h" | 24 #include "testing/gmock/include/gmock/gmock.h" |
| 23 #include "testing/gtest/include/gtest/gtest.h" | 25 #include "testing/gtest/include/gtest/gtest.h" |
| 24 | 26 |
| 25 using net::test::BuildUnsizedDataPacket; | 27 using net::test::BuildUnsizedDataPacket; |
| 26 using net::test::NoOpFramerVisitor; | 28 using net::test::NoOpFramerVisitor; |
| 27 using net::test::QuicVersionMax; | 29 using net::test::QuicVersionMax; |
| 28 using net::test::QuicVersionMin; | 30 using net::test::QuicVersionMin; |
| 29 using net::test::ValueRestore; | 31 using net::test::ValueRestore; |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 62 }; | 64 }; |
| 63 | 65 |
| 64 class QuicTimeWaitListManagerPeer { | 66 class QuicTimeWaitListManagerPeer { |
| 65 public: | 67 public: |
| 66 static bool ShouldSendResponse(QuicTimeWaitListManager* manager, | 68 static bool ShouldSendResponse(QuicTimeWaitListManager* manager, |
| 67 int received_packet_count) { | 69 int received_packet_count) { |
| 68 return manager->ShouldSendResponse(received_packet_count); | 70 return manager->ShouldSendResponse(received_packet_count); |
| 69 } | 71 } |
| 70 | 72 |
| 71 static QuicTime::Delta time_wait_period(QuicTimeWaitListManager* manager) { | 73 static QuicTime::Delta time_wait_period(QuicTimeWaitListManager* manager) { |
| 72 return manager->kTimeWaitPeriod_; | 74 return manager->time_wait_period_; |
| 73 } | 75 } |
| 74 | 76 |
| 75 static QuicVersion GetQuicVersionFromConnectionId( | 77 static QuicVersion GetQuicVersionFromConnectionId( |
| 76 QuicTimeWaitListManager* manager, | 78 QuicTimeWaitListManager* manager, |
| 77 QuicConnectionId connection_id) { | 79 QuicConnectionId connection_id) { |
| 78 return manager->GetQuicVersionFromConnectionId(connection_id); | 80 return manager->GetQuicVersionFromConnectionId(connection_id); |
| 79 } | 81 } |
| 80 }; | 82 }; |
| 81 | 83 |
| 82 namespace { | 84 namespace { |
| 83 | 85 |
| 84 class MockFakeTimeEpollServer : public FakeTimeEpollServer { | 86 class MockFakeTimeEpollServer : public FakeTimeEpollServer { |
| 85 public: | 87 public: |
| 86 MOCK_METHOD2(RegisterAlarm, void(int64 timeout_in_us, | 88 MOCK_METHOD2(RegisterAlarm, void(int64 timeout_in_us, |
| 87 EpollAlarmCallbackInterface* alarm)); | 89 EpollAlarmCallbackInterface* alarm)); |
| 88 }; | 90 }; |
| 89 | 91 |
| 90 class QuicTimeWaitListManagerTest : public ::testing::Test { | 92 class QuicTimeWaitListManagerTest : public ::testing::Test { |
| 91 protected: | 93 protected: |
| 92 QuicTimeWaitListManagerTest() | 94 QuicTimeWaitListManagerTest() |
| 93 : time_wait_list_manager_(&writer_, | 95 : helper_(&epoll_server_), |
| 96 time_wait_list_manager_(&writer_, |
| 94 &visitor_, | 97 &visitor_, |
| 95 &epoll_server_, | 98 &helper_, |
| 96 QuicSupportedVersions()), | 99 QuicSupportedVersions()), |
| 97 framer_(QuicSupportedVersions(), | 100 framer_(QuicSupportedVersions(), |
| 98 QuicTime::Zero(), | 101 QuicTime::Zero(), |
| 99 Perspective::IS_SERVER), | 102 Perspective::IS_SERVER), |
| 100 connection_id_(45), | 103 connection_id_(45), |
| 101 client_address_(net::test::TestPeerIPAddress(), kTestPort), | 104 client_address_(net::test::TestPeerIPAddress(), kTestPort), |
| 102 writer_is_blocked_(false) {} | 105 writer_is_blocked_(false) {} |
| 103 | 106 |
| 104 ~QuicTimeWaitListManagerTest() override {} | 107 ~QuicTimeWaitListManagerTest() override {} |
| 105 | 108 |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 159 BuildUnsizedDataPacket(&framer_, header, frames)); | 162 BuildUnsizedDataPacket(&framer_, header, frames)); |
| 160 EXPECT_TRUE(packet != nullptr); | 163 EXPECT_TRUE(packet != nullptr); |
| 161 QuicEncryptedPacket* encrypted = framer_.EncryptPacket(ENCRYPTION_NONE, | 164 QuicEncryptedPacket* encrypted = framer_.EncryptPacket(ENCRYPTION_NONE, |
| 162 sequence_number, | 165 sequence_number, |
| 163 *packet); | 166 *packet); |
| 164 EXPECT_TRUE(encrypted != nullptr); | 167 EXPECT_TRUE(encrypted != nullptr); |
| 165 return encrypted; | 168 return encrypted; |
| 166 } | 169 } |
| 167 | 170 |
| 168 NiceMock<MockFakeTimeEpollServer> epoll_server_; | 171 NiceMock<MockFakeTimeEpollServer> epoll_server_; |
| 172 QuicEpollConnectionHelper helper_; |
| 169 StrictMock<MockPacketWriter> writer_; | 173 StrictMock<MockPacketWriter> writer_; |
| 170 StrictMock<MockQuicServerSessionVisitor> visitor_; | 174 StrictMock<MockQuicServerSessionVisitor> visitor_; |
| 171 QuicTimeWaitListManager time_wait_list_manager_; | 175 QuicTimeWaitListManager time_wait_list_manager_; |
| 172 QuicFramer framer_; | 176 QuicFramer framer_; |
| 173 QuicConnectionId connection_id_; | 177 QuicConnectionId connection_id_; |
| 174 IPEndPoint server_address_; | 178 IPEndPoint server_address_; |
| 175 IPEndPoint client_address_; | 179 IPEndPoint client_address_; |
| 176 bool writer_is_blocked_; | 180 bool writer_is_blocked_; |
| 177 }; | 181 }; |
| 178 | 182 |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 221 | 225 |
| 222 TEST_F(QuicTimeWaitListManagerTest, CheckConnectionIdInTimeWait) { | 226 TEST_F(QuicTimeWaitListManagerTest, CheckConnectionIdInTimeWait) { |
| 223 EXPECT_FALSE(IsConnectionIdInTimeWait(connection_id_)); | 227 EXPECT_FALSE(IsConnectionIdInTimeWait(connection_id_)); |
| 224 EXPECT_CALL(visitor_, OnConnectionAddedToTimeWaitList(connection_id_)); | 228 EXPECT_CALL(visitor_, OnConnectionAddedToTimeWaitList(connection_id_)); |
| 225 AddConnectionId(connection_id_); | 229 AddConnectionId(connection_id_); |
| 226 EXPECT_EQ(1u, time_wait_list_manager_.num_connections()); | 230 EXPECT_EQ(1u, time_wait_list_manager_.num_connections()); |
| 227 EXPECT_TRUE(IsConnectionIdInTimeWait(connection_id_)); | 231 EXPECT_TRUE(IsConnectionIdInTimeWait(connection_id_)); |
| 228 } | 232 } |
| 229 | 233 |
| 230 TEST_F(QuicTimeWaitListManagerTest, SendConnectionClose) { | 234 TEST_F(QuicTimeWaitListManagerTest, SendConnectionClose) { |
| 231 size_t kConnectionCloseLength = 100; | 235 const size_t kConnectionCloseLength = 100; |
| 232 EXPECT_CALL(visitor_, OnConnectionAddedToTimeWaitList(connection_id_)); | 236 EXPECT_CALL(visitor_, OnConnectionAddedToTimeWaitList(connection_id_)); |
| 233 AddConnectionId( | 237 AddConnectionId( |
| 234 connection_id_, | 238 connection_id_, |
| 235 QuicVersionMax(), | 239 QuicVersionMax(), |
| 236 new QuicEncryptedPacket( | 240 new QuicEncryptedPacket( |
| 237 new char[kConnectionCloseLength], kConnectionCloseLength, true)); | 241 new char[kConnectionCloseLength], kConnectionCloseLength, true)); |
| 238 const int kRandomSequenceNumber = 1; | 242 const int kRandomSequenceNumber = 1; |
| 239 EXPECT_CALL(writer_, WritePacket(_, kConnectionCloseLength, | 243 EXPECT_CALL(writer_, WritePacket(_, kConnectionCloseLength, |
| 240 server_address_.address(), | 244 server_address_.address(), |
| 241 client_address_)) | 245 client_address_)) |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 276 EXPECT_FALSE(QuicTimeWaitListManagerPeer::ShouldSendResponse( | 280 EXPECT_FALSE(QuicTimeWaitListManagerPeer::ShouldSendResponse( |
| 277 &time_wait_list_manager_, sequence_number)); | 281 &time_wait_list_manager_, sequence_number)); |
| 278 } | 282 } |
| 279 } | 283 } |
| 280 } | 284 } |
| 281 | 285 |
| 282 TEST_F(QuicTimeWaitListManagerTest, CleanUpOldConnectionIds) { | 286 TEST_F(QuicTimeWaitListManagerTest, CleanUpOldConnectionIds) { |
| 283 const size_t kConnectionIdCount = 100; | 287 const size_t kConnectionIdCount = 100; |
| 284 const size_t kOldConnectionIdCount = 31; | 288 const size_t kOldConnectionIdCount = 31; |
| 285 | 289 |
| 286 // Add connection_ids such that their expiry time is kTimeWaitPeriod_. | 290 // Add connection_ids such that their expiry time is time_wait_period_. |
| 287 epoll_server_.set_now_in_usec(0); | 291 epoll_server_.set_now_in_usec(0); |
| 288 for (size_t connection_id = 1; connection_id <= kOldConnectionIdCount; | 292 for (size_t connection_id = 1; connection_id <= kOldConnectionIdCount; |
| 289 ++connection_id) { | 293 ++connection_id) { |
| 290 EXPECT_CALL(visitor_, OnConnectionAddedToTimeWaitList(connection_id)); | 294 EXPECT_CALL(visitor_, OnConnectionAddedToTimeWaitList(connection_id)); |
| 291 AddConnectionId(connection_id); | 295 AddConnectionId(connection_id); |
| 292 } | 296 } |
| 293 EXPECT_EQ(kOldConnectionIdCount, time_wait_list_manager_.num_connections()); | 297 EXPECT_EQ(kOldConnectionIdCount, time_wait_list_manager_.num_connections()); |
| 294 | 298 |
| 295 // Add remaining connection_ids such that their add time is | 299 // Add remaining connection_ids such that their add time is |
| 296 // 2 * kTimeWaitPeriod. | 300 // 2 * time_wait_period_. |
| 297 const QuicTime::Delta time_wait_period = | 301 const QuicTime::Delta time_wait_period = |
| 298 QuicTimeWaitListManagerPeer::time_wait_period(&time_wait_list_manager_); | 302 QuicTimeWaitListManagerPeer::time_wait_period(&time_wait_list_manager_); |
| 299 epoll_server_.set_now_in_usec(time_wait_period.ToMicroseconds()); | 303 epoll_server_.set_now_in_usec(time_wait_period.ToMicroseconds()); |
| 300 for (size_t connection_id = kOldConnectionIdCount + 1; | 304 for (size_t connection_id = kOldConnectionIdCount + 1; |
| 301 connection_id <= kConnectionIdCount; ++connection_id) { | 305 connection_id <= kConnectionIdCount; ++connection_id) { |
| 302 EXPECT_CALL(visitor_, OnConnectionAddedToTimeWaitList(connection_id)); | 306 EXPECT_CALL(visitor_, OnConnectionAddedToTimeWaitList(connection_id)); |
| 303 AddConnectionId(connection_id); | 307 AddConnectionId(connection_id); |
| 304 } | 308 } |
| 305 EXPECT_EQ(kConnectionIdCount, time_wait_list_manager_.num_connections()); | 309 EXPECT_EQ(kConnectionIdCount, time_wait_list_manager_.num_connections()); |
| 306 | 310 |
| (...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 411 &time_wait_list_manager_, kConnectionId1)); | 415 &time_wait_list_manager_, kConnectionId1)); |
| 412 EXPECT_EQ(QuicVersionMax(), | 416 EXPECT_EQ(QuicVersionMax(), |
| 413 QuicTimeWaitListManagerPeer::GetQuicVersionFromConnectionId( | 417 QuicTimeWaitListManagerPeer::GetQuicVersionFromConnectionId( |
| 414 &time_wait_list_manager_, kConnectionId2)); | 418 &time_wait_list_manager_, kConnectionId2)); |
| 415 EXPECT_EQ(QuicVersionMax(), | 419 EXPECT_EQ(QuicVersionMax(), |
| 416 QuicTimeWaitListManagerPeer::GetQuicVersionFromConnectionId( | 420 QuicTimeWaitListManagerPeer::GetQuicVersionFromConnectionId( |
| 417 &time_wait_list_manager_, kConnectionId3)); | 421 &time_wait_list_manager_, kConnectionId3)); |
| 418 } | 422 } |
| 419 | 423 |
| 420 TEST_F(QuicTimeWaitListManagerTest, AddConnectionIdTwice) { | 424 TEST_F(QuicTimeWaitListManagerTest, AddConnectionIdTwice) { |
| 421 // Add connection_ids such that their expiry time is kTimeWaitPeriod_. | 425 // Add connection_ids such that their expiry time is time_wait_period_. |
| 422 epoll_server_.set_now_in_usec(0); | 426 epoll_server_.set_now_in_usec(0); |
| 423 EXPECT_CALL(visitor_, OnConnectionAddedToTimeWaitList(connection_id_)); | 427 EXPECT_CALL(visitor_, OnConnectionAddedToTimeWaitList(connection_id_)); |
| 424 AddConnectionId(connection_id_); | 428 AddConnectionId(connection_id_); |
| 425 EXPECT_TRUE(IsConnectionIdInTimeWait(connection_id_)); | 429 EXPECT_TRUE(IsConnectionIdInTimeWait(connection_id_)); |
| 426 size_t kConnectionCloseLength = 100; | 430 const size_t kConnectionCloseLength = 100; |
| 427 AddConnectionId( | 431 AddConnectionId( |
| 428 connection_id_, | 432 connection_id_, |
| 429 QuicVersionMax(), | 433 QuicVersionMax(), |
| 430 new QuicEncryptedPacket( | 434 new QuicEncryptedPacket( |
| 431 new char[kConnectionCloseLength], kConnectionCloseLength, true)); | 435 new char[kConnectionCloseLength], kConnectionCloseLength, true)); |
| 432 EXPECT_TRUE(IsConnectionIdInTimeWait(connection_id_)); | 436 EXPECT_TRUE(IsConnectionIdInTimeWait(connection_id_)); |
| 433 EXPECT_EQ(1u, time_wait_list_manager_.num_connections()); | 437 EXPECT_EQ(1u, time_wait_list_manager_.num_connections()); |
| 434 | 438 |
| 435 EXPECT_CALL(writer_, WritePacket(_, | 439 EXPECT_CALL(writer_, WritePacket(_, |
| 436 kConnectionCloseLength, | 440 kConnectionCloseLength, |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 523 time_wait_list_manager_.num_connections()); | 527 time_wait_list_manager_.num_connections()); |
| 524 EXPECT_FALSE(IsConnectionIdInTimeWait(id_to_evict)); | 528 EXPECT_FALSE(IsConnectionIdInTimeWait(id_to_evict)); |
| 525 EXPECT_TRUE(IsConnectionIdInTimeWait(current_connection_id)); | 529 EXPECT_TRUE(IsConnectionIdInTimeWait(current_connection_id)); |
| 526 } | 530 } |
| 527 } | 531 } |
| 528 | 532 |
| 529 } // namespace | 533 } // namespace |
| 530 } // namespace test | 534 } // namespace test |
| 531 } // namespace tools | 535 } // namespace tools |
| 532 } // namespace net | 536 } // namespace net |
| OLD | NEW |