OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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_dispatcher.h" | 5 #include "net/tools/quic/quic_dispatcher.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/strings/string_piece.h" | 9 #include "base/strings/string_piece.h" |
10 #include "net/quic/crypto/crypto_handshake.h" | 10 #include "net/quic/crypto/crypto_handshake.h" |
11 #include "net/quic/crypto/quic_crypto_server_config.h" | 11 #include "net/quic/crypto/quic_crypto_server_config.h" |
12 #include "net/quic/crypto/quic_random.h" | 12 #include "net/quic/crypto/quic_random.h" |
| 13 #include "net/quic/quic_connection_helper.h" |
13 #include "net/quic/quic_crypto_stream.h" | 14 #include "net/quic/quic_crypto_stream.h" |
14 #include "net/quic/quic_flags.h" | 15 #include "net/quic/quic_flags.h" |
15 #include "net/quic/quic_utils.h" | 16 #include "net/quic/quic_utils.h" |
16 #include "net/quic/test_tools/quic_test_utils.h" | 17 #include "net/quic/test_tools/quic_test_utils.h" |
17 #include "net/tools/epoll_server/epoll_server.h" | 18 #include "net/tools/epoll_server/epoll_server.h" |
| 19 #include "net/tools/quic/quic_epoll_connection_helper.h" |
18 #include "net/tools/quic/quic_packet_writer_wrapper.h" | 20 #include "net/tools/quic/quic_packet_writer_wrapper.h" |
19 #include "net/tools/quic/quic_time_wait_list_manager.h" | 21 #include "net/tools/quic/quic_time_wait_list_manager.h" |
20 #include "net/tools/quic/test_tools/quic_dispatcher_peer.h" | 22 #include "net/tools/quic/test_tools/quic_dispatcher_peer.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 base::StringPiece; | 27 using base::StringPiece; |
26 using net::EpollServer; | 28 using net::EpollServer; |
27 using net::test::ConstructEncryptedPacket; | 29 using net::test::ConstructEncryptedPacket; |
28 using net::test::MockConnection; | 30 using net::test::MockConnection; |
29 using net::test::MockSession; | 31 using net::test::MockSession; |
30 using net::test::ValueRestore; | 32 using net::test::ValueRestore; |
31 using std::make_pair; | |
32 using std::string; | 33 using std::string; |
33 using testing::DoAll; | 34 using testing::DoAll; |
34 using testing::InSequence; | 35 using testing::InSequence; |
35 using testing::Invoke; | 36 using testing::Invoke; |
36 using testing::WithoutArgs; | 37 using testing::WithoutArgs; |
37 using testing::_; | 38 using testing::_; |
38 | 39 |
39 namespace net { | 40 namespace net { |
40 namespace tools { | 41 namespace tools { |
41 namespace test { | 42 namespace test { |
42 namespace { | 43 namespace { |
43 | 44 |
44 class TestDispatcher : public QuicDispatcher { | 45 class TestDispatcher : public QuicDispatcher { |
45 public: | 46 public: |
46 explicit TestDispatcher(const QuicConfig& config, | 47 explicit TestDispatcher(const QuicConfig& config, |
47 const QuicCryptoServerConfig& crypto_config, | 48 const QuicCryptoServerConfig& crypto_config, |
48 EpollServer* eps) | 49 EpollServer* eps) |
49 : QuicDispatcher(config, | 50 : QuicDispatcher(config, |
50 crypto_config, | 51 crypto_config, |
51 QuicSupportedVersions(), | 52 QuicSupportedVersions(), |
52 new QuicDispatcher::DefaultPacketWriterFactory(), | 53 new QuicDispatcher::DefaultPacketWriterFactory(), |
53 eps) { | 54 new QuicEpollConnectionHelper(eps)) { |
54 } | 55 } |
55 | 56 |
56 MOCK_METHOD3(CreateQuicSession, QuicSession*( | 57 MOCK_METHOD3(CreateQuicSession, QuicSession*( |
57 QuicConnectionId connection_id, | 58 QuicConnectionId connection_id, |
58 const IPEndPoint& server_address, | 59 const IPEndPoint& server_address, |
59 const IPEndPoint& client_address)); | 60 const IPEndPoint& client_address)); |
60 | 61 |
61 using QuicDispatcher::current_server_address; | 62 using QuicDispatcher::current_server_address; |
62 using QuicDispatcher::current_client_address; | 63 using QuicDispatcher::current_client_address; |
63 }; | 64 }; |
(...skipping 26 matching lines...) Expand all Loading... |
90 *session = new MockSession(connection); | 91 *session = new MockSession(connection); |
91 ON_CALL(*connection, SendConnectionClose(_)).WillByDefault( | 92 ON_CALL(*connection, SendConnectionClose(_)).WillByDefault( |
92 WithoutArgs(Invoke( | 93 WithoutArgs(Invoke( |
93 connection, &MockServerConnection::UnregisterOnConnectionClosed))); | 94 connection, &MockServerConnection::UnregisterOnConnectionClosed))); |
94 EXPECT_CALL(*reinterpret_cast<MockConnection*>((*session)->connection()), | 95 EXPECT_CALL(*reinterpret_cast<MockConnection*>((*session)->connection()), |
95 ProcessUdpPacket(_, client_address, _)); | 96 ProcessUdpPacket(_, client_address, _)); |
96 | 97 |
97 return *session; | 98 return *session; |
98 } | 99 } |
99 | 100 |
100 class MockTimeWaitListManager : public QuicTimeWaitListManager { | |
101 public: | |
102 MockTimeWaitListManager(QuicPacketWriter* writer, | |
103 QuicServerSessionVisitor* visitor, | |
104 EpollServer* eps) | |
105 : QuicTimeWaitListManager(writer, visitor, eps, QuicSupportedVersions()) { | |
106 } | |
107 | |
108 MOCK_METHOD5(ProcessPacket, | |
109 void(const IPEndPoint& server_address, | |
110 const IPEndPoint& client_address, | |
111 QuicConnectionId connection_id, | |
112 QuicPacketSequenceNumber sequence_number, | |
113 const QuicEncryptedPacket& packet)); | |
114 }; | |
115 | |
116 class QuicDispatcherTest : public ::testing::Test { | 101 class QuicDispatcherTest : public ::testing::Test { |
117 public: | 102 public: |
118 QuicDispatcherTest() | 103 QuicDispatcherTest() |
119 : crypto_config_(QuicCryptoServerConfig::TESTING, | 104 : helper_(&eps_), |
| 105 crypto_config_(QuicCryptoServerConfig::TESTING, |
120 QuicRandom::GetInstance()), | 106 QuicRandom::GetInstance()), |
121 dispatcher_(config_, crypto_config_, &eps_), | 107 dispatcher_(config_, crypto_config_, &eps_), |
122 time_wait_list_manager_(nullptr), | 108 time_wait_list_manager_(nullptr), |
123 session1_(nullptr), | 109 session1_(nullptr), |
124 session2_(nullptr) { | 110 session2_(nullptr) { |
125 dispatcher_.Initialize(1); | 111 dispatcher_.Initialize(1); |
126 } | 112 } |
127 | 113 |
128 ~QuicDispatcherTest() override {} | 114 ~QuicDispatcherTest() override {} |
129 | 115 |
(...skipping 26 matching lines...) Expand all Loading... |
156 dispatcher_.ProcessPacket(server_address_, client_address, *packet); | 142 dispatcher_.ProcessPacket(server_address_, client_address, *packet); |
157 } | 143 } |
158 | 144 |
159 void ValidatePacket(const QuicEncryptedPacket& packet) { | 145 void ValidatePacket(const QuicEncryptedPacket& packet) { |
160 EXPECT_EQ(data_.length(), packet.AsStringPiece().length()); | 146 EXPECT_EQ(data_.length(), packet.AsStringPiece().length()); |
161 EXPECT_EQ(data_, packet.AsStringPiece()); | 147 EXPECT_EQ(data_, packet.AsStringPiece()); |
162 } | 148 } |
163 | 149 |
164 void CreateTimeWaitListManager() { | 150 void CreateTimeWaitListManager() { |
165 time_wait_list_manager_ = new MockTimeWaitListManager( | 151 time_wait_list_manager_ = new MockTimeWaitListManager( |
166 QuicDispatcherPeer::GetWriter(&dispatcher_), &dispatcher_, &eps_); | 152 QuicDispatcherPeer::GetWriter(&dispatcher_), &dispatcher_, &helper_); |
167 // dispatcher_ takes the ownership of time_wait_list_manager_. | 153 // dispatcher_ takes the ownership of time_wait_list_manager_. |
168 QuicDispatcherPeer::SetTimeWaitListManager(&dispatcher_, | 154 QuicDispatcherPeer::SetTimeWaitListManager(&dispatcher_, |
169 time_wait_list_manager_); | 155 time_wait_list_manager_); |
170 } | 156 } |
171 | 157 |
172 EpollServer eps_; | 158 EpollServer eps_; |
| 159 QuicEpollConnectionHelper helper_; |
173 QuicConfig config_; | 160 QuicConfig config_; |
174 QuicCryptoServerConfig crypto_config_; | 161 QuicCryptoServerConfig crypto_config_; |
175 IPEndPoint server_address_; | 162 IPEndPoint server_address_; |
176 TestDispatcher dispatcher_; | 163 TestDispatcher dispatcher_; |
177 MockTimeWaitListManager* time_wait_list_manager_; | 164 MockTimeWaitListManager* time_wait_list_manager_; |
178 MockSession* session1_; | 165 MockSession* session1_; |
179 MockSession* session2_; | 166 MockSession* session2_; |
180 string data_; | 167 string data_; |
181 }; | 168 }; |
182 | 169 |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
250 .WillOnce(Invoke( | 237 .WillOnce(Invoke( |
251 reinterpret_cast<MockConnection*>(session1_->connection()), | 238 reinterpret_cast<MockConnection*>(session1_->connection()), |
252 &MockConnection::ReallyProcessUdpPacket)); | 239 &MockConnection::ReallyProcessUdpPacket)); |
253 dispatcher_.ProcessPacket(IPEndPoint(), client_address, *encrypted); | 240 dispatcher_.ProcessPacket(IPEndPoint(), client_address, *encrypted); |
254 EXPECT_TRUE(time_wait_list_manager_->IsConnectionIdInTimeWait(connection_id)); | 241 EXPECT_TRUE(time_wait_list_manager_->IsConnectionIdInTimeWait(connection_id)); |
255 | 242 |
256 // Dispatcher forwards subsequent packets for this connection_id to the time | 243 // Dispatcher forwards subsequent packets for this connection_id to the time |
257 // wait list manager. | 244 // wait list manager. |
258 EXPECT_CALL(*time_wait_list_manager_, | 245 EXPECT_CALL(*time_wait_list_manager_, |
259 ProcessPacket(_, _, connection_id, _, _)).Times(1); | 246 ProcessPacket(_, _, connection_id, _, _)).Times(1); |
| 247 EXPECT_CALL(*time_wait_list_manager_, |
| 248 AddConnectionIdToTimeWait(_, _, _)).Times(0); |
260 ProcessPacket(client_address, connection_id, true, "foo"); | 249 ProcessPacket(client_address, connection_id, true, "foo"); |
261 } | 250 } |
262 | 251 |
263 TEST_F(QuicDispatcherTest, StrayPacketToTimeWaitListManager) { | 252 TEST_F(QuicDispatcherTest, StrayPacketToTimeWaitListManager) { |
264 CreateTimeWaitListManager(); | 253 CreateTimeWaitListManager(); |
265 | 254 |
266 IPEndPoint client_address(net::test::Loopback4(), 1); | 255 IPEndPoint client_address(net::test::Loopback4(), 1); |
267 QuicConnectionId connection_id = 1; | 256 QuicConnectionId connection_id = 1; |
268 // Dispatcher forwards all packets for this connection_id to the time wait | 257 // Dispatcher forwards all packets for this connection_id to the time wait |
269 // list manager. | 258 // list manager. |
270 EXPECT_CALL(dispatcher_, CreateQuicSession(_, _, _)).Times(0); | 259 EXPECT_CALL(dispatcher_, CreateQuicSession(_, _, _)).Times(0); |
271 EXPECT_CALL(*time_wait_list_manager_, | 260 EXPECT_CALL(*time_wait_list_manager_, |
272 ProcessPacket(_, _, connection_id, _, _)).Times(1); | 261 ProcessPacket(_, _, connection_id, _, _)).Times(1); |
| 262 EXPECT_CALL(*time_wait_list_manager_, |
| 263 AddConnectionIdToTimeWait(_, _, _)).Times(1); |
273 ProcessPacket(client_address, connection_id, false, "data"); | 264 ProcessPacket(client_address, connection_id, false, "data"); |
274 } | 265 } |
275 | 266 |
276 TEST_F(QuicDispatcherTest, ProcessPacketWithBogusPort) { | 267 TEST_F(QuicDispatcherTest, ProcessPacketWithBogusPort) { |
277 CreateTimeWaitListManager(); | 268 CreateTimeWaitListManager(); |
278 | 269 |
279 IPEndPoint client_address(net::test::Loopback4(), 0); | 270 IPEndPoint client_address(net::test::Loopback4(), 0); |
280 IPAddressNumber any4; | 271 IPAddressNumber any4; |
281 CHECK(net::ParseIPLiteralToNumber("0.0.0.0", &any4)); | 272 CHECK(net::ParseIPLiteralToNumber("0.0.0.0", &any4)); |
282 server_address_ = IPEndPoint(any4, 5); | 273 server_address_ = IPEndPoint(any4, 5); |
283 | 274 |
284 // dispatcher_ should drop this packet. | 275 // dispatcher_ should drop this packet. |
285 EXPECT_CALL(dispatcher_, CreateQuicSession(1, _, client_address)).Times(0); | 276 EXPECT_CALL(dispatcher_, CreateQuicSession(1, _, client_address)).Times(0); |
286 EXPECT_CALL(*time_wait_list_manager_, ProcessPacket(_, _, _, _, _)).Times(0); | 277 EXPECT_CALL(*time_wait_list_manager_, ProcessPacket(_, _, _, _, _)).Times(0); |
| 278 EXPECT_CALL(*time_wait_list_manager_, |
| 279 AddConnectionIdToTimeWait(_, _, _)).Times(0); |
287 ProcessPacket(client_address, 1, true, "foo"); | 280 ProcessPacket(client_address, 1, true, "foo"); |
288 EXPECT_EQ(client_address, dispatcher_.current_client_address()); | 281 EXPECT_EQ(client_address, dispatcher_.current_client_address()); |
289 EXPECT_EQ(server_address_, dispatcher_.current_server_address()); | 282 EXPECT_EQ(server_address_, dispatcher_.current_server_address()); |
290 } | 283 } |
291 | 284 |
292 class BlockingWriter : public QuicPacketWriterWrapper { | 285 class BlockingWriter : public QuicPacketWriterWrapper { |
293 public: | 286 public: |
294 BlockingWriter() : write_blocked_(false) {} | 287 BlockingWriter() : write_blocked_(false) {} |
295 | 288 |
296 bool IsWriteBlocked() const override { return write_blocked_; } | 289 bool IsWriteBlocked() const override { return write_blocked_; } |
(...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
480 // And we'll resume where we left off when we get another call. | 473 // And we'll resume where we left off when we get another call. |
481 EXPECT_CALL(*connection2(), OnCanWrite()); | 474 EXPECT_CALL(*connection2(), OnCanWrite()); |
482 dispatcher_.OnCanWrite(); | 475 dispatcher_.OnCanWrite(); |
483 EXPECT_FALSE(dispatcher_.HasPendingWrites()); | 476 EXPECT_FALSE(dispatcher_.HasPendingWrites()); |
484 } | 477 } |
485 | 478 |
486 } // namespace | 479 } // namespace |
487 } // namespace test | 480 } // namespace test |
488 } // namespace tools | 481 } // namespace tools |
489 } // namespace net | 482 } // namespace net |
OLD | NEW |