| 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 | |
| 116 class QuicDispatcherTest : public ::testing::Test { | 100 class QuicDispatcherTest : public ::testing::Test { |
| 117 public: | 101 public: |
| 118 QuicDispatcherTest() | 102 QuicDispatcherTest() |
| 119 : crypto_config_(QuicCryptoServerConfig::TESTING, | 103 : crypto_config_(QuicCryptoServerConfig::TESTING, |
| 120 QuicRandom::GetInstance()), | 104 QuicRandom::GetInstance()), |
| 121 dispatcher_(config_, crypto_config_, &eps_), | 105 dispatcher_(config_, crypto_config_, &eps_), |
| 122 time_wait_list_manager_(nullptr), | 106 time_wait_list_manager_(nullptr), |
| 123 session1_(nullptr), | 107 session1_(nullptr), |
| 124 session2_(nullptr) { | 108 session2_(nullptr) { |
| 125 dispatcher_.Initialize(1); | 109 dispatcher_.Initialize(1); |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 186 CHECK(net::ParseIPLiteralToNumber("0.0.0.0", &any4)); | 170 CHECK(net::ParseIPLiteralToNumber("0.0.0.0", &any4)); |
| 187 server_address_ = IPEndPoint(any4, 5); | 171 server_address_ = IPEndPoint(any4, 5); |
| 188 | 172 |
| 189 EXPECT_CALL(dispatcher_, CreateQuicSession(1, _, client_address)) | 173 EXPECT_CALL(dispatcher_, CreateQuicSession(1, _, client_address)) |
| 190 .WillOnce(testing::Return(CreateSession( | 174 .WillOnce(testing::Return(CreateSession( |
| 191 &dispatcher_, 1, client_address, &session1_))); | 175 &dispatcher_, 1, client_address, &session1_))); |
| 192 ProcessPacket(client_address, 1, true, "foo"); | 176 ProcessPacket(client_address, 1, true, "foo"); |
| 193 EXPECT_EQ(client_address, dispatcher_.current_client_address()); | 177 EXPECT_EQ(client_address, dispatcher_.current_client_address()); |
| 194 EXPECT_EQ(server_address_, dispatcher_.current_server_address()); | 178 EXPECT_EQ(server_address_, dispatcher_.current_server_address()); |
| 195 | 179 |
| 196 | |
| 197 EXPECT_CALL(dispatcher_, CreateQuicSession(2, _, client_address)) | 180 EXPECT_CALL(dispatcher_, CreateQuicSession(2, _, client_address)) |
| 198 .WillOnce(testing::Return(CreateSession( | 181 .WillOnce(testing::Return( |
| 199 &dispatcher_, 2, client_address, &session2_))); | 182 CreateSession(&dispatcher_, 2, client_address, &session2_))); |
| 200 ProcessPacket(client_address, 2, true, "bar"); | 183 ProcessPacket(client_address, 2, true, "bar"); |
| 201 | 184 |
| 202 EXPECT_CALL(*reinterpret_cast<MockConnection*>(session1_->connection()), | 185 EXPECT_CALL(*reinterpret_cast<MockConnection*>(session1_->connection()), |
| 203 ProcessUdpPacket(_, _, _)).Times(1). | 186 ProcessUdpPacket(_, _, _)).Times(1). |
| 204 WillOnce(testing::WithArgs<2>(Invoke( | 187 WillOnce(testing::WithArgs<2>(Invoke( |
| 205 this, &QuicDispatcherTest::ValidatePacket))); | 188 this, &QuicDispatcherTest::ValidatePacket))); |
| 206 ProcessPacket(client_address, 1, false, "eep"); | 189 ProcessPacket(client_address, 1, false, "eep"); |
| 207 } | 190 } |
| 208 | 191 |
| 209 TEST_F(QuicDispatcherTest, Shutdown) { | 192 TEST_F(QuicDispatcherTest, Shutdown) { |
| 210 IPEndPoint client_address(net::test::Loopback4(), 1); | 193 IPEndPoint client_address(net::test::Loopback4(), 1); |
| 211 | 194 |
| 212 EXPECT_CALL(dispatcher_, CreateQuicSession(_, _, client_address)) | 195 EXPECT_CALL(dispatcher_, CreateQuicSession(_, _, client_address)) |
| 213 .WillOnce(testing::Return(CreateSession( | 196 .WillOnce(testing::Return( |
| 214 &dispatcher_, 1, client_address, &session1_))); | 197 CreateSession(&dispatcher_, 1, client_address, &session1_))); |
| 215 | 198 |
| 216 ProcessPacket(client_address, 1, true, "foo"); | 199 ProcessPacket(client_address, 1, true, "foo"); |
| 217 | 200 |
| 218 EXPECT_CALL(*reinterpret_cast<MockConnection*>(session1_->connection()), | 201 EXPECT_CALL(*reinterpret_cast<MockConnection*>(session1_->connection()), |
| 219 SendConnectionClose(QUIC_PEER_GOING_AWAY)); | 202 SendConnectionClose(QUIC_PEER_GOING_AWAY)); |
| 220 | 203 |
| 221 dispatcher_.Shutdown(); | 204 dispatcher_.Shutdown(); |
| 222 } | 205 } |
| 223 | 206 |
| 224 TEST_F(QuicDispatcherTest, TimeWaitListManager) { | 207 TEST_F(QuicDispatcherTest, TimeWaitListManager) { |
| 225 CreateTimeWaitListManager(); | 208 CreateTimeWaitListManager(); |
| 226 | 209 |
| 227 // Create a new session. | 210 // Create a new session. |
| 228 IPEndPoint client_address(net::test::Loopback4(), 1); | 211 IPEndPoint client_address(net::test::Loopback4(), 1); |
| 229 QuicConnectionId connection_id = 1; | 212 QuicConnectionId connection_id = 1; |
| 230 EXPECT_CALL(dispatcher_, CreateQuicSession(connection_id, _, client_address)) | 213 EXPECT_CALL(dispatcher_, CreateQuicSession(connection_id, _, client_address)) |
| 231 .WillOnce(testing::Return(CreateSession( | 214 .WillOnce(testing::Return(CreateSession(&dispatcher_, connection_id, |
| 232 &dispatcher_, connection_id, client_address, &session1_))); | 215 client_address, &session1_))); |
| 233 ProcessPacket(client_address, connection_id, true, "foo"); | 216 ProcessPacket(client_address, connection_id, true, "foo"); |
| 234 | 217 |
| 235 // Close the connection by sending public reset packet. | 218 // Close the connection by sending public reset packet. |
| 236 QuicPublicResetPacket packet; | 219 QuicPublicResetPacket packet; |
| 237 packet.public_header.connection_id = connection_id; | 220 packet.public_header.connection_id = connection_id; |
| 238 packet.public_header.reset_flag = true; | 221 packet.public_header.reset_flag = true; |
| 239 packet.public_header.version_flag = false; | 222 packet.public_header.version_flag = false; |
| 240 packet.rejected_sequence_number = 19191; | 223 packet.rejected_sequence_number = 19191; |
| 241 packet.nonce_proof = 132232; | 224 packet.nonce_proof = 132232; |
| 242 scoped_ptr<QuicEncryptedPacket> encrypted( | 225 scoped_ptr<QuicEncryptedPacket> encrypted( |
| 243 QuicFramer::BuildPublicResetPacket(packet)); | 226 QuicFramer::BuildPublicResetPacket(packet)); |
| 244 EXPECT_CALL(*session1_, OnConnectionClosed(QUIC_PUBLIC_RESET, true)).Times(1) | 227 EXPECT_CALL(*session1_, OnConnectionClosed(QUIC_PUBLIC_RESET, true)).Times(1) |
| 245 .WillOnce(WithoutArgs(Invoke( | 228 .WillOnce(WithoutArgs(Invoke( |
| 246 reinterpret_cast<MockServerConnection*>(session1_->connection()), | 229 reinterpret_cast<MockServerConnection*>(session1_->connection()), |
| 247 &MockServerConnection::UnregisterOnConnectionClosed))); | 230 &MockServerConnection::UnregisterOnConnectionClosed))); |
| 248 EXPECT_CALL(*reinterpret_cast<MockConnection*>(session1_->connection()), | 231 EXPECT_CALL(*reinterpret_cast<MockConnection*>(session1_->connection()), |
| 249 ProcessUdpPacket(_, _, _)) | 232 ProcessUdpPacket(_, _, _)) |
| 250 .WillOnce(Invoke( | 233 .WillOnce(Invoke( |
| 251 reinterpret_cast<MockConnection*>(session1_->connection()), | 234 reinterpret_cast<MockConnection*>(session1_->connection()), |
| 252 &MockConnection::ReallyProcessUdpPacket)); | 235 &MockConnection::ReallyProcessUdpPacket)); |
| 253 dispatcher_.ProcessPacket(IPEndPoint(), client_address, *encrypted); | 236 dispatcher_.ProcessPacket(IPEndPoint(), client_address, *encrypted); |
| 254 EXPECT_TRUE(time_wait_list_manager_->IsConnectionIdInTimeWait(connection_id)); | 237 EXPECT_TRUE(time_wait_list_manager_->IsConnectionIdInTimeWait(connection_id)); |
| 255 | 238 |
| 256 // Dispatcher forwards subsequent packets for this connection_id to the time | 239 // Dispatcher forwards subsequent packets for this connection_id to the time |
| 257 // wait list manager. | 240 // wait list manager. |
| 258 EXPECT_CALL(*time_wait_list_manager_, | 241 EXPECT_CALL(*time_wait_list_manager_, |
| 259 ProcessPacket(_, _, connection_id, _, _)).Times(1); | 242 ProcessPacket(_, _, connection_id, _, _)).Times(1); |
| 243 EXPECT_CALL(*time_wait_list_manager_, AddConnectionIdToTimeWait(_, _, _)) |
| 244 .Times(0); |
| 260 ProcessPacket(client_address, connection_id, true, "foo"); | 245 ProcessPacket(client_address, connection_id, true, "foo"); |
| 261 } | 246 } |
| 262 | 247 |
| 263 TEST_F(QuicDispatcherTest, StrayPacketToTimeWaitListManager) { | 248 TEST_F(QuicDispatcherTest, StrayPacketToTimeWaitListManager) { |
| 264 CreateTimeWaitListManager(); | 249 CreateTimeWaitListManager(); |
| 265 | 250 |
| 266 IPEndPoint client_address(net::test::Loopback4(), 1); | 251 IPEndPoint client_address(net::test::Loopback4(), 1); |
| 267 QuicConnectionId connection_id = 1; | 252 QuicConnectionId connection_id = 1; |
| 268 // Dispatcher forwards all packets for this connection_id to the time wait | 253 // Dispatcher forwards all packets for this connection_id to the time wait |
| 269 // list manager. | 254 // list manager. |
| 270 EXPECT_CALL(dispatcher_, CreateQuicSession(_, _, _)).Times(0); | 255 EXPECT_CALL(dispatcher_, CreateQuicSession(_, _, _)).Times(0); |
| 271 EXPECT_CALL(*time_wait_list_manager_, | 256 EXPECT_CALL(*time_wait_list_manager_, |
| 272 ProcessPacket(_, _, connection_id, _, _)).Times(1); | 257 ProcessPacket(_, _, connection_id, _, _)).Times(1); |
| 258 EXPECT_CALL(*time_wait_list_manager_, AddConnectionIdToTimeWait(_, _, _)) |
| 259 .Times(1); |
| 273 ProcessPacket(client_address, connection_id, false, "data"); | 260 ProcessPacket(client_address, connection_id, false, "data"); |
| 274 } | 261 } |
| 275 | 262 |
| 276 TEST_F(QuicDispatcherTest, ProcessPacketWithBogusPort) { | 263 TEST_F(QuicDispatcherTest, ProcessPacketWithBogusPort) { |
| 277 CreateTimeWaitListManager(); | 264 CreateTimeWaitListManager(); |
| 278 | 265 |
| 279 IPEndPoint client_address(net::test::Loopback4(), 0); | 266 IPEndPoint client_address(net::test::Loopback4(), 0); |
| 280 IPAddressNumber any4; | 267 IPAddressNumber any4; |
| 281 CHECK(net::ParseIPLiteralToNumber("0.0.0.0", &any4)); | 268 CHECK(net::ParseIPLiteralToNumber("0.0.0.0", &any4)); |
| 282 server_address_ = IPEndPoint(any4, 5); | 269 server_address_ = IPEndPoint(any4, 5); |
| 283 | 270 |
| 284 // dispatcher_ should drop this packet. | 271 // dispatcher_ should drop this packet. |
| 285 EXPECT_CALL(dispatcher_, CreateQuicSession(1, _, client_address)).Times(0); | 272 EXPECT_CALL(dispatcher_, CreateQuicSession(1, _, client_address)).Times(0); |
| 286 EXPECT_CALL(*time_wait_list_manager_, ProcessPacket(_, _, _, _, _)).Times(0); | 273 EXPECT_CALL(*time_wait_list_manager_, ProcessPacket(_, _, _, _, _)).Times(0); |
| 274 EXPECT_CALL(*time_wait_list_manager_, AddConnectionIdToTimeWait(_, _, _)) |
| 275 .Times(0); |
| 287 ProcessPacket(client_address, 1, true, "foo"); | 276 ProcessPacket(client_address, 1, true, "foo"); |
| 288 EXPECT_EQ(client_address, dispatcher_.current_client_address()); | 277 EXPECT_EQ(client_address, dispatcher_.current_client_address()); |
| 289 EXPECT_EQ(server_address_, dispatcher_.current_server_address()); | 278 EXPECT_EQ(server_address_, dispatcher_.current_server_address()); |
| 290 } | 279 } |
| 291 | 280 |
| 292 class BlockingWriter : public QuicPacketWriterWrapper { | 281 class BlockingWriter : public QuicPacketWriterWrapper { |
| 293 public: | 282 public: |
| 294 BlockingWriter() : write_blocked_(false) {} | 283 BlockingWriter() : write_blocked_(false) {} |
| 295 | 284 |
| 296 bool IsWriteBlocked() const override { return write_blocked_; } | 285 bool IsWriteBlocked() const override { return write_blocked_; } |
| (...skipping 17 matching lines...) Expand all Loading... |
| 314 public: | 303 public: |
| 315 void SetUp() override { | 304 void SetUp() override { |
| 316 writer_ = new BlockingWriter; | 305 writer_ = new BlockingWriter; |
| 317 QuicDispatcherPeer::SetPacketWriterFactory(&dispatcher_, | 306 QuicDispatcherPeer::SetPacketWriterFactory(&dispatcher_, |
| 318 new TestWriterFactory()); | 307 new TestWriterFactory()); |
| 319 QuicDispatcherPeer::UseWriter(&dispatcher_, writer_); | 308 QuicDispatcherPeer::UseWriter(&dispatcher_, writer_); |
| 320 | 309 |
| 321 IPEndPoint client_address(net::test::Loopback4(), 1); | 310 IPEndPoint client_address(net::test::Loopback4(), 1); |
| 322 | 311 |
| 323 EXPECT_CALL(dispatcher_, CreateQuicSession(_, _, client_address)) | 312 EXPECT_CALL(dispatcher_, CreateQuicSession(_, _, client_address)) |
| 324 .WillOnce(testing::Return(CreateSession( | 313 .WillOnce(testing::Return( |
| 325 &dispatcher_, 1, client_address, &session1_))); | 314 CreateSession(&dispatcher_, 1, client_address, &session1_))); |
| 326 ProcessPacket(client_address, 1, true, "foo"); | 315 ProcessPacket(client_address, 1, true, "foo"); |
| 327 | 316 |
| 328 EXPECT_CALL(dispatcher_, CreateQuicSession(_, _, client_address)) | 317 EXPECT_CALL(dispatcher_, CreateQuicSession(_, _, client_address)) |
| 329 .WillOnce(testing::Return(CreateSession( | 318 .WillOnce(testing::Return( |
| 330 &dispatcher_, 2, client_address, &session2_))); | 319 CreateSession(&dispatcher_, 2, client_address, &session2_))); |
| 331 ProcessPacket(client_address, 2, true, "bar"); | 320 ProcessPacket(client_address, 2, true, "bar"); |
| 332 | 321 |
| 333 blocked_list_ = QuicDispatcherPeer::GetWriteBlockedList(&dispatcher_); | 322 blocked_list_ = QuicDispatcherPeer::GetWriteBlockedList(&dispatcher_); |
| 334 } | 323 } |
| 335 | 324 |
| 336 void TearDown() override { | 325 void TearDown() override { |
| 337 EXPECT_CALL(*connection1(), SendConnectionClose(QUIC_PEER_GOING_AWAY)); | 326 EXPECT_CALL(*connection1(), SendConnectionClose(QUIC_PEER_GOING_AWAY)); |
| 338 EXPECT_CALL(*connection2(), SendConnectionClose(QUIC_PEER_GOING_AWAY)); | 327 EXPECT_CALL(*connection2(), SendConnectionClose(QUIC_PEER_GOING_AWAY)); |
| 339 dispatcher_.Shutdown(); | 328 dispatcher_.Shutdown(); |
| 340 } | 329 } |
| (...skipping 139 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. | 469 // And we'll resume where we left off when we get another call. |
| 481 EXPECT_CALL(*connection2(), OnCanWrite()); | 470 EXPECT_CALL(*connection2(), OnCanWrite()); |
| 482 dispatcher_.OnCanWrite(); | 471 dispatcher_.OnCanWrite(); |
| 483 EXPECT_FALSE(dispatcher_.HasPendingWrites()); | 472 EXPECT_FALSE(dispatcher_.HasPendingWrites()); |
| 484 } | 473 } |
| 485 | 474 |
| 486 } // namespace | 475 } // namespace |
| 487 } // namespace test | 476 } // namespace test |
| 488 } // namespace tools | 477 } // namespace tools |
| 489 } // namespace net | 478 } // namespace net |
| OLD | NEW |