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_server.h" | 5 #include "net/tools/quic/quic_server.h" |
6 | 6 |
7 #include "net/quic/core/crypto/quic_random.h" | 7 #include "net/quic/core/crypto/quic_random.h" |
8 #include "net/quic/core/quic_utils.h" | 8 #include "net/quic/core/quic_utils.h" |
9 #include "net/quic/test_tools/crypto_test_utils.h" | 9 #include "net/quic/test_tools/crypto_test_utils.h" |
10 #include "net/quic/test_tools/mock_quic_dispatcher.h" | 10 #include "net/quic/test_tools/mock_quic_dispatcher.h" |
11 #include "net/tools/quic/quic_epoll_alarm_factory.h" | 11 #include "net/tools/quic/quic_epoll_alarm_factory.h" |
12 #include "net/tools/quic/quic_epoll_connection_helper.h" | 12 #include "net/tools/quic/quic_epoll_connection_helper.h" |
13 #include "net/tools/quic/quic_simple_server_session_helper.h" | 13 #include "net/tools/quic/quic_simple_server_session_helper.h" |
| 14 #include "net/tools/quic/test_tools/quic_server_peer.h" |
14 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
15 | 16 |
16 using ::testing::_; | 17 using ::testing::_; |
17 using net::test::CryptoTestUtils; | 18 using net::test::CryptoTestUtils; |
18 using net::test::MockQuicDispatcher; | 19 using net::test::MockQuicDispatcher; |
19 | 20 |
20 namespace net { | 21 namespace net { |
21 namespace test { | 22 namespace test { |
22 | 23 |
23 namespace { | 24 namespace { |
24 | 25 |
| 26 class MockQuicSimpleDispatcher : public QuicSimpleDispatcher { |
| 27 public: |
| 28 MockQuicSimpleDispatcher( |
| 29 const QuicConfig& config, |
| 30 const QuicCryptoServerConfig* crypto_config, |
| 31 QuicVersionManager* version_manager, |
| 32 std::unique_ptr<QuicConnectionHelperInterface> helper, |
| 33 std::unique_ptr<QuicServerSessionBase::Helper> session_helper, |
| 34 std::unique_ptr<QuicAlarmFactory> alarm_factory) |
| 35 : QuicSimpleDispatcher(config, |
| 36 crypto_config, |
| 37 version_manager, |
| 38 std::move(helper), |
| 39 std::move(session_helper), |
| 40 std::move(alarm_factory)) {} |
| 41 ~MockQuicSimpleDispatcher() override {} |
| 42 |
| 43 MOCK_METHOD0(OnCanWrite, void()); |
| 44 MOCK_CONST_METHOD0(HasPendingWrites, bool()); |
| 45 MOCK_CONST_METHOD0(HasChlosBuffered, bool()); |
| 46 MOCK_METHOD1(ProcessBufferedChlos, void(size_t)); |
| 47 }; |
| 48 |
| 49 class TestQuicServer : public QuicServer { |
| 50 public: |
| 51 TestQuicServer() : QuicServer(CryptoTestUtils::ProofSourceForTesting()) {} |
| 52 |
| 53 ~TestQuicServer() override {} |
| 54 |
| 55 MockQuicSimpleDispatcher* mock_dispatcher() { return mock_dispatcher_; } |
| 56 |
| 57 protected: |
| 58 QuicDispatcher* CreateQuicDispatcher() override { |
| 59 mock_dispatcher_ = new MockQuicSimpleDispatcher( |
| 60 config(), &crypto_config(), version_manager(), |
| 61 std::unique_ptr<QuicEpollConnectionHelper>( |
| 62 new QuicEpollConnectionHelper(epoll_server(), |
| 63 QuicAllocator::BUFFER_POOL)), |
| 64 std::unique_ptr<QuicServerSessionBase::Helper>( |
| 65 new QuicSimpleServerSessionHelper(QuicRandom::GetInstance())), |
| 66 std::unique_ptr<QuicEpollAlarmFactory>( |
| 67 new QuicEpollAlarmFactory(epoll_server()))); |
| 68 return mock_dispatcher_; |
| 69 } |
| 70 |
| 71 MockQuicSimpleDispatcher* mock_dispatcher_; |
| 72 }; |
| 73 |
| 74 class QuicServerEpollInTest : public ::testing::Test { |
| 75 public: |
| 76 QuicServerEpollInTest() |
| 77 : port_(net::test::kTestPort), server_address_(Loopback4(), port_) {} |
| 78 |
| 79 void StartListening() { |
| 80 server_.CreateUDPSocketAndListen(server_address_); |
| 81 ASSERT_TRUE(QuicServerPeer::SetSmallSocket(&server_)); |
| 82 |
| 83 if (!server_.overflow_supported()) { |
| 84 LOG(WARNING) << "Overflow not supported. Not testing."; |
| 85 return; |
| 86 } |
| 87 } |
| 88 |
| 89 protected: |
| 90 int port_; |
| 91 IPEndPoint server_address_; |
| 92 TestQuicServer server_; |
| 93 }; |
| 94 |
| 95 // Tests that if dispatcher has CHLOs waiting for connection creation, EPOLLIN |
| 96 // event should try to create connections for them. And set epoll mask with |
| 97 // EPOLLIN if there are still CHLOs remaining at the end of epoll event. |
| 98 TEST_F(QuicServerEpollInTest, ProcessBufferedCHLOsOnEpollin) { |
| 99 FLAGS_quic_limit_num_new_sessions_per_epoll_loop = true; |
| 100 FLAGS_quic_buffer_packet_till_chlo = true; |
| 101 // Given an EPOLLIN event, try to create session for buffered CHLOs. In first |
| 102 // event, dispatcher can't create session for all of CHLOs. So listener should |
| 103 // register another EPOLLIN event by itself. Even without new packet arrival, |
| 104 // the rest CHLOs should be process in next epoll event. |
| 105 StartListening(); |
| 106 bool more_chlos = true; |
| 107 MockQuicSimpleDispatcher* dispatcher_ = server_.mock_dispatcher(); |
| 108 DCHECK(dispatcher_ != nullptr); |
| 109 EXPECT_CALL(*dispatcher_, OnCanWrite()).Times(testing::AnyNumber()); |
| 110 EXPECT_CALL(*dispatcher_, ProcessBufferedChlos(_)).Times(2); |
| 111 EXPECT_CALL(*dispatcher_, HasPendingWrites()).Times(testing::AnyNumber()); |
| 112 // Expect there are still CHLOs buffered after 1st event. But not any more |
| 113 // after 2nd event. |
| 114 EXPECT_CALL(*dispatcher_, HasChlosBuffered()) |
| 115 .WillOnce(testing::Return(true)) |
| 116 .WillOnce( |
| 117 DoAll(testing::Assign(&more_chlos, false), testing::Return(false))); |
| 118 |
| 119 // Send a packet to trigger epoll event. |
| 120 int fd = socket(AF_INET, SOCK_DGRAM | SOCK_NONBLOCK, IPPROTO_UDP); |
| 121 ASSERT_LT(0, fd); |
| 122 |
| 123 char buf[1024]; |
| 124 memset(buf, 0, arraysize(buf)); |
| 125 sockaddr_storage storage; |
| 126 socklen_t storage_size = sizeof(storage); |
| 127 ASSERT_TRUE(server_address_.ToSockAddr(reinterpret_cast<sockaddr*>(&storage), |
| 128 &storage_size)); |
| 129 int rc = sendto(fd, buf, arraysize(buf), 0, |
| 130 reinterpret_cast<sockaddr*>(&storage), storage_size); |
| 131 if (rc < 0) { |
| 132 DVLOG(1) << errno << " " << strerror(errno); |
| 133 } |
| 134 |
| 135 while (more_chlos) { |
| 136 server_.WaitForEvents(); |
| 137 } |
| 138 } |
| 139 |
25 class QuicServerDispatchPacketTest : public ::testing::Test { | 140 class QuicServerDispatchPacketTest : public ::testing::Test { |
26 public: | 141 public: |
27 QuicServerDispatchPacketTest() | 142 QuicServerDispatchPacketTest() |
28 : crypto_config_("blah", | 143 : crypto_config_("blah", |
29 QuicRandom::GetInstance(), | 144 QuicRandom::GetInstance(), |
30 CryptoTestUtils::ProofSourceForTesting()), | 145 CryptoTestUtils::ProofSourceForTesting()), |
31 version_manager_(AllSupportedVersions()), | 146 version_manager_(AllSupportedVersions()), |
32 dispatcher_( | 147 dispatcher_( |
33 config_, | 148 config_, |
34 &crypto_config_, | 149 &crypto_config_, |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
75 arraysize(valid_packet), | 190 arraysize(valid_packet), |
76 QuicTime::Zero(), false); | 191 QuicTime::Zero(), false); |
77 | 192 |
78 EXPECT_CALL(dispatcher_, ProcessPacket(_, _, _)).Times(1); | 193 EXPECT_CALL(dispatcher_, ProcessPacket(_, _, _)).Times(1); |
79 DispatchPacket(encrypted_valid_packet); | 194 DispatchPacket(encrypted_valid_packet); |
80 } | 195 } |
81 | 196 |
82 } // namespace | 197 } // namespace |
83 } // namespace test | 198 } // namespace test |
84 } // namespace net | 199 } // namespace net |
OLD | NEW |