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" |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
90 *session = new MockSession(connection); | 90 *session = new MockSession(connection); |
91 ON_CALL(*connection, SendConnectionClose(_)).WillByDefault( | 91 ON_CALL(*connection, SendConnectionClose(_)).WillByDefault( |
92 WithoutArgs(Invoke( | 92 WithoutArgs(Invoke( |
93 connection, &MockServerConnection::UnregisterOnConnectionClosed))); | 93 connection, &MockServerConnection::UnregisterOnConnectionClosed))); |
94 EXPECT_CALL(*reinterpret_cast<MockConnection*>((*session)->connection()), | 94 EXPECT_CALL(*reinterpret_cast<MockConnection*>((*session)->connection()), |
95 ProcessUdpPacket(_, client_address, _)); | 95 ProcessUdpPacket(_, client_address, _)); |
96 | 96 |
97 return *session; | 97 return *session; |
98 } | 98 } |
99 | 99 |
| 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 |
100 class QuicDispatcherTest : public ::testing::Test { | 116 class QuicDispatcherTest : public ::testing::Test { |
101 public: | 117 public: |
102 QuicDispatcherTest() | 118 QuicDispatcherTest() |
103 : crypto_config_(QuicCryptoServerConfig::TESTING, | 119 : crypto_config_(QuicCryptoServerConfig::TESTING, |
104 QuicRandom::GetInstance()), | 120 QuicRandom::GetInstance()), |
105 dispatcher_(config_, crypto_config_, &eps_), | 121 dispatcher_(config_, crypto_config_, &eps_), |
| 122 time_wait_list_manager_(nullptr), |
106 session1_(nullptr), | 123 session1_(nullptr), |
107 session2_(nullptr) { | 124 session2_(nullptr) { |
108 dispatcher_.Initialize(1); | 125 dispatcher_.Initialize(1); |
109 } | 126 } |
110 | 127 |
111 ~QuicDispatcherTest() override {} | 128 ~QuicDispatcherTest() override {} |
112 | 129 |
113 MockConnection* connection1() { | 130 MockConnection* connection1() { |
114 return reinterpret_cast<MockConnection*>(session1_->connection()); | 131 return reinterpret_cast<MockConnection*>(session1_->connection()); |
115 } | 132 } |
(...skipping 10 matching lines...) Expand all Loading... |
126 connection_id, has_version_flag, false, 1, data)); | 143 connection_id, has_version_flag, false, 1, data)); |
127 data_ = string(packet->data(), packet->length()); | 144 data_ = string(packet->data(), packet->length()); |
128 dispatcher_.ProcessPacket(server_address_, client_address, *packet); | 145 dispatcher_.ProcessPacket(server_address_, client_address, *packet); |
129 } | 146 } |
130 | 147 |
131 void ValidatePacket(const QuicEncryptedPacket& packet) { | 148 void ValidatePacket(const QuicEncryptedPacket& packet) { |
132 EXPECT_EQ(data_.length(), packet.AsStringPiece().length()); | 149 EXPECT_EQ(data_.length(), packet.AsStringPiece().length()); |
133 EXPECT_EQ(data_, packet.AsStringPiece()); | 150 EXPECT_EQ(data_, packet.AsStringPiece()); |
134 } | 151 } |
135 | 152 |
| 153 void CreateTimeWaitListManager() { |
| 154 time_wait_list_manager_ = new MockTimeWaitListManager( |
| 155 QuicDispatcherPeer::GetWriter(&dispatcher_), &dispatcher_, &eps_); |
| 156 // dispatcher takes the ownership of time_wait_list_manager. |
| 157 QuicDispatcherPeer::SetTimeWaitListManager(&dispatcher_, |
| 158 time_wait_list_manager_); |
| 159 } |
| 160 |
136 EpollServer eps_; | 161 EpollServer eps_; |
137 QuicConfig config_; | 162 QuicConfig config_; |
138 QuicCryptoServerConfig crypto_config_; | 163 QuicCryptoServerConfig crypto_config_; |
139 IPEndPoint server_address_; | 164 IPEndPoint server_address_; |
140 TestDispatcher dispatcher_; | 165 TestDispatcher dispatcher_; |
| 166 MockTimeWaitListManager* time_wait_list_manager_; |
141 MockSession* session1_; | 167 MockSession* session1_; |
142 MockSession* session2_; | 168 MockSession* session2_; |
143 string data_; | 169 string data_; |
144 }; | 170 }; |
145 | 171 |
146 TEST_F(QuicDispatcherTest, ProcessPackets) { | 172 TEST_F(QuicDispatcherTest, ProcessPackets) { |
147 IPEndPoint client_address(net::test::Loopback4(), 1); | 173 IPEndPoint client_address(net::test::Loopback4(), 1); |
148 IPAddressNumber any4; | 174 IPAddressNumber any4; |
149 CHECK(net::ParseIPLiteralToNumber("0.0.0.0", &any4)); | 175 CHECK(net::ParseIPLiteralToNumber("0.0.0.0", &any4)); |
150 server_address_ = IPEndPoint(any4, 5); | 176 server_address_ = IPEndPoint(any4, 5); |
(...skipping 26 matching lines...) Expand all Loading... |
177 &dispatcher_, 1, client_address, &session1_))); | 203 &dispatcher_, 1, client_address, &session1_))); |
178 | 204 |
179 ProcessPacket(client_address, 1, true, "foo"); | 205 ProcessPacket(client_address, 1, true, "foo"); |
180 | 206 |
181 EXPECT_CALL(*reinterpret_cast<MockConnection*>(session1_->connection()), | 207 EXPECT_CALL(*reinterpret_cast<MockConnection*>(session1_->connection()), |
182 SendConnectionClose(QUIC_PEER_GOING_AWAY)); | 208 SendConnectionClose(QUIC_PEER_GOING_AWAY)); |
183 | 209 |
184 dispatcher_.Shutdown(); | 210 dispatcher_.Shutdown(); |
185 } | 211 } |
186 | 212 |
187 class MockTimeWaitListManager : public QuicTimeWaitListManager { | 213 TEST_F(QuicDispatcherTest, TimeWaitListManager) { |
188 public: | 214 CreateTimeWaitListManager(); |
189 MockTimeWaitListManager(QuicPacketWriter* writer, | |
190 QuicServerSessionVisitor* visitor, | |
191 EpollServer* eps) | |
192 : QuicTimeWaitListManager(writer, visitor, eps, QuicSupportedVersions()) { | |
193 } | |
194 | 215 |
195 MOCK_METHOD5(ProcessPacket, void(const IPEndPoint& server_address, | |
196 const IPEndPoint& client_address, | |
197 QuicConnectionId connection_id, | |
198 QuicPacketSequenceNumber sequence_number, | |
199 const QuicEncryptedPacket& packet)); | |
200 }; | |
201 | |
202 TEST_F(QuicDispatcherTest, TimeWaitListManager) { | |
203 MockTimeWaitListManager* time_wait_list_manager = | |
204 new MockTimeWaitListManager( | |
205 QuicDispatcherPeer::GetWriter(&dispatcher_), &dispatcher_, &eps_); | |
206 // dispatcher takes the ownership of time_wait_list_manager. | |
207 QuicDispatcherPeer::SetTimeWaitListManager(&dispatcher_, | |
208 time_wait_list_manager); | |
209 // Create a new session. | 216 // Create a new session. |
210 IPEndPoint client_address(net::test::Loopback4(), 1); | 217 IPEndPoint client_address(net::test::Loopback4(), 1); |
211 QuicConnectionId connection_id = 1; | 218 QuicConnectionId connection_id = 1; |
212 EXPECT_CALL(dispatcher_, CreateQuicSession(connection_id, _, client_address)) | 219 EXPECT_CALL(dispatcher_, CreateQuicSession(connection_id, _, client_address)) |
213 .WillOnce(testing::Return(CreateSession( | 220 .WillOnce(testing::Return(CreateSession( |
214 &dispatcher_, connection_id, client_address, &session1_))); | 221 &dispatcher_, connection_id, client_address, &session1_))); |
215 ProcessPacket(client_address, connection_id, true, "foo"); | 222 ProcessPacket(client_address, connection_id, true, "foo"); |
216 | 223 |
217 // Close the connection by sending public reset packet. | 224 // Close the connection by sending public reset packet. |
218 QuicPublicResetPacket packet; | 225 QuicPublicResetPacket packet; |
219 packet.public_header.connection_id = connection_id; | 226 packet.public_header.connection_id = connection_id; |
220 packet.public_header.reset_flag = true; | 227 packet.public_header.reset_flag = true; |
221 packet.public_header.version_flag = false; | 228 packet.public_header.version_flag = false; |
222 packet.rejected_sequence_number = 19191; | 229 packet.rejected_sequence_number = 19191; |
223 packet.nonce_proof = 132232; | 230 packet.nonce_proof = 132232; |
224 scoped_ptr<QuicEncryptedPacket> encrypted( | 231 scoped_ptr<QuicEncryptedPacket> encrypted( |
225 QuicFramer::BuildPublicResetPacket(packet)); | 232 QuicFramer::BuildPublicResetPacket(packet)); |
226 EXPECT_CALL(*session1_, OnConnectionClosed(QUIC_PUBLIC_RESET, true)).Times(1) | 233 EXPECT_CALL(*session1_, OnConnectionClosed(QUIC_PUBLIC_RESET, true)).Times(1) |
227 .WillOnce(WithoutArgs(Invoke( | 234 .WillOnce(WithoutArgs(Invoke( |
228 reinterpret_cast<MockServerConnection*>(session1_->connection()), | 235 reinterpret_cast<MockServerConnection*>(session1_->connection()), |
229 &MockServerConnection::UnregisterOnConnectionClosed))); | 236 &MockServerConnection::UnregisterOnConnectionClosed))); |
230 EXPECT_CALL(*reinterpret_cast<MockConnection*>(session1_->connection()), | 237 EXPECT_CALL(*reinterpret_cast<MockConnection*>(session1_->connection()), |
231 ProcessUdpPacket(_, _, _)) | 238 ProcessUdpPacket(_, _, _)) |
232 .WillOnce(Invoke( | 239 .WillOnce(Invoke( |
233 reinterpret_cast<MockConnection*>(session1_->connection()), | 240 reinterpret_cast<MockConnection*>(session1_->connection()), |
234 &MockConnection::ReallyProcessUdpPacket)); | 241 &MockConnection::ReallyProcessUdpPacket)); |
235 dispatcher_.ProcessPacket(IPEndPoint(), client_address, *encrypted); | 242 dispatcher_.ProcessPacket(IPEndPoint(), client_address, *encrypted); |
236 EXPECT_TRUE(time_wait_list_manager->IsConnectionIdInTimeWait(connection_id)); | 243 EXPECT_TRUE(time_wait_list_manager_->IsConnectionIdInTimeWait(connection_id)); |
237 | 244 |
238 // Dispatcher forwards subsequent packets for this connection_id to the time | 245 // Dispatcher forwards subsequent packets for this connection_id to the time |
239 // wait list manager. | 246 // wait list manager. |
240 EXPECT_CALL(*time_wait_list_manager, | 247 EXPECT_CALL(*time_wait_list_manager_, |
241 ProcessPacket(_, _, connection_id, _, _)).Times(1); | 248 ProcessPacket(_, _, connection_id, _, _)).Times(1); |
242 ProcessPacket(client_address, connection_id, true, "foo"); | 249 ProcessPacket(client_address, connection_id, true, "foo"); |
243 } | 250 } |
244 | 251 |
245 TEST_F(QuicDispatcherTest, StrayPacketToTimeWaitListManager) { | 252 TEST_F(QuicDispatcherTest, StrayPacketToTimeWaitListManager) { |
246 MockTimeWaitListManager* time_wait_list_manager = | 253 CreateTimeWaitListManager(); |
247 new MockTimeWaitListManager( | |
248 QuicDispatcherPeer::GetWriter(&dispatcher_), &dispatcher_, &eps_); | |
249 // dispatcher takes the ownership of time_wait_list_manager. | |
250 QuicDispatcherPeer::SetTimeWaitListManager(&dispatcher_, | |
251 time_wait_list_manager); | |
252 | 254 |
253 IPEndPoint client_address(net::test::Loopback4(), 1); | 255 IPEndPoint client_address(net::test::Loopback4(), 1); |
254 QuicConnectionId connection_id = 1; | 256 QuicConnectionId connection_id = 1; |
255 // 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 |
256 // list manager. | 258 // list manager. |
257 EXPECT_CALL(dispatcher_, CreateQuicSession(_, _, _)).Times(0); | 259 EXPECT_CALL(dispatcher_, CreateQuicSession(_, _, _)).Times(0); |
258 EXPECT_CALL(*time_wait_list_manager, | 260 EXPECT_CALL(*time_wait_list_manager_, |
259 ProcessPacket(_, _, connection_id, _, _)).Times(1); | 261 ProcessPacket(_, _, connection_id, _, _)).Times(1); |
260 string data = "foo"; | 262 string data = "foo"; |
261 ProcessPacket(client_address, connection_id, false, "foo"); | 263 ProcessPacket(client_address, connection_id, false, "foo"); |
262 } | 264 } |
263 | 265 |
| 266 TEST_F(QuicDispatcherTest, ProcessPacketWithBogusPort) { |
| 267 CreateTimeWaitListManager(); |
| 268 |
| 269 IPEndPoint client_address(net::test::Loopback4(), 0); |
| 270 IPAddressNumber any4; |
| 271 CHECK(net::ParseIPLiteralToNumber("0.0.0.0", &any4)); |
| 272 server_address_ = IPEndPoint(any4, 5); |
| 273 |
| 274 EXPECT_CALL(dispatcher_, CreateQuicSession(1, _, client_address)).Times(0); |
| 275 EXPECT_CALL(*time_wait_list_manager_, ProcessPacket(_, _, _, _, _)).Times(0); |
| 276 ProcessPacket(client_address, 1, true, "foo"); |
| 277 EXPECT_EQ(client_address, dispatcher_.current_client_address()); |
| 278 EXPECT_EQ(server_address_, dispatcher_.current_server_address()); |
| 279 } |
| 280 |
264 class BlockingWriter : public QuicPacketWriterWrapper { | 281 class BlockingWriter : public QuicPacketWriterWrapper { |
265 public: | 282 public: |
266 BlockingWriter() : write_blocked_(false) {} | 283 BlockingWriter() : write_blocked_(false) {} |
267 | 284 |
268 bool IsWriteBlocked() const override { return write_blocked_; } | 285 bool IsWriteBlocked() const override { return write_blocked_; } |
269 void SetWritable() override { write_blocked_ = false; } | 286 void SetWritable() override { write_blocked_ = false; } |
270 | 287 |
271 WriteResult WritePacket(const char* buffer, | 288 WriteResult WritePacket(const char* buffer, |
272 size_t buf_len, | 289 size_t buf_len, |
273 const IPAddressNumber& self_client_address, | 290 const IPAddressNumber& self_client_address, |
(...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
452 // And we'll resume where we left off when we get another call. | 469 // And we'll resume where we left off when we get another call. |
453 EXPECT_CALL(*connection2(), OnCanWrite()); | 470 EXPECT_CALL(*connection2(), OnCanWrite()); |
454 dispatcher_.OnCanWrite(); | 471 dispatcher_.OnCanWrite(); |
455 EXPECT_FALSE(dispatcher_.HasPendingWrites()); | 472 EXPECT_FALSE(dispatcher_.HasPendingWrites()); |
456 } | 473 } |
457 | 474 |
458 } // namespace | 475 } // namespace |
459 } // namespace test | 476 } // namespace test |
460 } // namespace tools | 477 } // namespace tools |
461 } // namespace net | 478 } // namespace net |
OLD | NEW |