| 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 23 matching lines...) Expand all Loading... |
| 34 using testing::InSequence; | 34 using testing::InSequence; |
| 35 using testing::Invoke; | 35 using testing::Invoke; |
| 36 using testing::WithoutArgs; | 36 using testing::WithoutArgs; |
| 37 using testing::_; | 37 using testing::_; |
| 38 | 38 |
| 39 namespace net { | 39 namespace net { |
| 40 namespace tools { | 40 namespace tools { |
| 41 namespace test { | 41 namespace test { |
| 42 namespace { | 42 namespace { |
| 43 | 43 |
| 44 class TestServerSession : public QuicServerSession { |
| 45 public: |
| 46 TestServerSession(const QuicConfig& config, QuicConnection* connection) |
| 47 : QuicServerSession(config, connection, nullptr) {} |
| 48 ~TestServerSession() override{}; |
| 49 |
| 50 MOCK_METHOD2(OnConnectionClosed, void(QuicErrorCode error, bool from_peer)); |
| 51 MOCK_METHOD1(CreateIncomingDataStream, QuicDataStream*(QuicStreamId id)); |
| 52 MOCK_METHOD0(CreateOutgoingDataStream, QuicDataStream*()); |
| 53 |
| 54 private: |
| 55 DISALLOW_COPY_AND_ASSIGN(TestServerSession); |
| 56 }; |
| 57 |
| 44 class TestDispatcher : public QuicDispatcher { | 58 class TestDispatcher : public QuicDispatcher { |
| 45 public: | 59 public: |
| 46 explicit TestDispatcher(const QuicConfig& config, | 60 explicit TestDispatcher(const QuicConfig& config, |
| 47 const QuicCryptoServerConfig& crypto_config, | 61 const QuicCryptoServerConfig& crypto_config, |
| 48 EpollServer* eps) | 62 EpollServer* eps) |
| 49 : QuicDispatcher(config, | 63 : QuicDispatcher(config, |
| 50 crypto_config, | 64 crypto_config, |
| 51 QuicSupportedVersions(), | 65 QuicSupportedVersions(), |
| 52 new QuicDispatcher::DefaultPacketWriterFactory(), | 66 new QuicDispatcher::DefaultPacketWriterFactory(), |
| 53 eps) { | 67 eps) { |
| 54 } | 68 } |
| 55 | 69 |
| 56 MOCK_METHOD3(CreateQuicSession, QuicSession*( | 70 MOCK_METHOD3(CreateQuicSession, |
| 57 QuicConnectionId connection_id, | 71 QuicServerSession*(QuicConnectionId connection_id, |
| 58 const IPEndPoint& server_address, | 72 const IPEndPoint& server_address, |
| 59 const IPEndPoint& client_address)); | 73 const IPEndPoint& client_address)); |
| 60 | 74 |
| 61 using QuicDispatcher::current_server_address; | 75 using QuicDispatcher::current_server_address; |
| 62 using QuicDispatcher::current_client_address; | 76 using QuicDispatcher::current_client_address; |
| 63 }; | 77 }; |
| 64 | 78 |
| 65 // A Connection class which unregisters the session from the dispatcher | 79 // A Connection class which unregisters the session from the dispatcher |
| 66 // when sending connection close. | 80 // when sending connection close. |
| 67 // It'd be slightly more realistic to do this from the Session but it would | 81 // It'd be slightly more realistic to do this from the Session but it would |
| 68 // involve a lot more mocking. | 82 // involve a lot more mocking. |
| 69 class MockServerConnection : public MockConnection { | 83 class MockServerConnection : public MockConnection { |
| 70 public: | 84 public: |
| 71 MockServerConnection(QuicConnectionId connection_id, | 85 MockServerConnection(QuicConnectionId connection_id, |
| 72 QuicDispatcher* dispatcher) | 86 QuicDispatcher* dispatcher) |
| 73 : MockConnection(connection_id, Perspective::IS_SERVER), | 87 : MockConnection(connection_id, Perspective::IS_SERVER), |
| 74 dispatcher_(dispatcher) {} | 88 dispatcher_(dispatcher) {} |
| 75 | 89 |
| 76 void UnregisterOnConnectionClosed() { | 90 void UnregisterOnConnectionClosed() { |
| 77 LOG(ERROR) << "Unregistering " << connection_id(); | 91 LOG(ERROR) << "Unregistering " << connection_id(); |
| 78 dispatcher_->OnConnectionClosed(connection_id(), QUIC_NO_ERROR); | 92 dispatcher_->OnConnectionClosed(connection_id(), QUIC_NO_ERROR); |
| 79 } | 93 } |
| 94 |
| 80 private: | 95 private: |
| 81 QuicDispatcher* dispatcher_; | 96 QuicDispatcher* dispatcher_; |
| 82 }; | 97 }; |
| 83 | 98 |
| 84 QuicSession* CreateSession(QuicDispatcher* dispatcher, | 99 QuicServerSession* CreateSession(QuicDispatcher* dispatcher, |
| 85 QuicConnectionId connection_id, | 100 const QuicConfig& config, |
| 86 const IPEndPoint& client_address, | 101 QuicConnectionId connection_id, |
| 87 MockSession** session) { | 102 const IPEndPoint& client_address, |
| 103 TestServerSession** session) { |
| 88 MockServerConnection* connection = | 104 MockServerConnection* connection = |
| 89 new MockServerConnection(connection_id, dispatcher); | 105 new MockServerConnection(connection_id, dispatcher); |
| 90 *session = new MockSession(connection); | 106 *session = new TestServerSession(config, connection); |
| 107 connection->set_visitor(*session); |
| 91 ON_CALL(*connection, SendConnectionClose(_)).WillByDefault( | 108 ON_CALL(*connection, SendConnectionClose(_)).WillByDefault( |
| 92 WithoutArgs(Invoke( | 109 WithoutArgs(Invoke( |
| 93 connection, &MockServerConnection::UnregisterOnConnectionClosed))); | 110 connection, &MockServerConnection::UnregisterOnConnectionClosed))); |
| 94 EXPECT_CALL(*reinterpret_cast<MockConnection*>((*session)->connection()), | 111 EXPECT_CALL(*reinterpret_cast<MockConnection*>((*session)->connection()), |
| 95 ProcessUdpPacket(_, client_address, _)); | 112 ProcessUdpPacket(_, client_address, _)); |
| 96 | 113 |
| 97 return *session; | 114 return *session; |
| 98 } | 115 } |
| 99 | 116 |
| 100 class QuicDispatcherTest : public ::testing::Test { | 117 class QuicDispatcherTest : public ::testing::Test { |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 152 QuicDispatcherPeer::SetTimeWaitListManager(&dispatcher_, | 169 QuicDispatcherPeer::SetTimeWaitListManager(&dispatcher_, |
| 153 time_wait_list_manager_); | 170 time_wait_list_manager_); |
| 154 } | 171 } |
| 155 | 172 |
| 156 EpollServer eps_; | 173 EpollServer eps_; |
| 157 QuicConfig config_; | 174 QuicConfig config_; |
| 158 QuicCryptoServerConfig crypto_config_; | 175 QuicCryptoServerConfig crypto_config_; |
| 159 IPEndPoint server_address_; | 176 IPEndPoint server_address_; |
| 160 TestDispatcher dispatcher_; | 177 TestDispatcher dispatcher_; |
| 161 MockTimeWaitListManager* time_wait_list_manager_; | 178 MockTimeWaitListManager* time_wait_list_manager_; |
| 162 MockSession* session1_; | 179 TestServerSession* session1_; |
| 163 MockSession* session2_; | 180 TestServerSession* session2_; |
| 164 string data_; | 181 string data_; |
| 165 }; | 182 }; |
| 166 | 183 |
| 167 TEST_F(QuicDispatcherTest, ProcessPackets) { | 184 TEST_F(QuicDispatcherTest, ProcessPackets) { |
| 168 IPEndPoint client_address(net::test::Loopback4(), 1); | 185 IPEndPoint client_address(net::test::Loopback4(), 1); |
| 169 IPAddressNumber any4; | 186 IPAddressNumber any4; |
| 170 CHECK(net::ParseIPLiteralToNumber("0.0.0.0", &any4)); | 187 CHECK(net::ParseIPLiteralToNumber("0.0.0.0", &any4)); |
| 171 server_address_ = IPEndPoint(any4, 5); | 188 server_address_ = IPEndPoint(any4, 5); |
| 172 | 189 |
| 173 EXPECT_CALL(dispatcher_, CreateQuicSession(1, _, client_address)) | 190 EXPECT_CALL(dispatcher_, CreateQuicSession(1, _, client_address)) |
| 174 .WillOnce(testing::Return(CreateSession( | 191 .WillOnce(testing::Return( |
| 175 &dispatcher_, 1, client_address, &session1_))); | 192 CreateSession(&dispatcher_, config_, 1, client_address, &session1_))); |
| 176 ProcessPacket(client_address, 1, true, "foo"); | 193 ProcessPacket(client_address, 1, true, "foo"); |
| 177 EXPECT_EQ(client_address, dispatcher_.current_client_address()); | 194 EXPECT_EQ(client_address, dispatcher_.current_client_address()); |
| 178 EXPECT_EQ(server_address_, dispatcher_.current_server_address()); | 195 EXPECT_EQ(server_address_, dispatcher_.current_server_address()); |
| 179 | 196 |
| 180 EXPECT_CALL(dispatcher_, CreateQuicSession(2, _, client_address)) | 197 EXPECT_CALL(dispatcher_, CreateQuicSession(2, _, client_address)) |
| 181 .WillOnce(testing::Return( | 198 .WillOnce(testing::Return( |
| 182 CreateSession(&dispatcher_, 2, client_address, &session2_))); | 199 CreateSession(&dispatcher_, config_, 2, client_address, &session2_))); |
| 183 ProcessPacket(client_address, 2, true, "bar"); | 200 ProcessPacket(client_address, 2, true, "bar"); |
| 184 | 201 |
| 185 EXPECT_CALL(*reinterpret_cast<MockConnection*>(session1_->connection()), | 202 EXPECT_CALL(*reinterpret_cast<MockConnection*>(session1_->connection()), |
| 186 ProcessUdpPacket(_, _, _)).Times(1). | 203 ProcessUdpPacket(_, _, _)).Times(1). |
| 187 WillOnce(testing::WithArgs<2>(Invoke( | 204 WillOnce(testing::WithArgs<2>(Invoke( |
| 188 this, &QuicDispatcherTest::ValidatePacket))); | 205 this, &QuicDispatcherTest::ValidatePacket))); |
| 189 ProcessPacket(client_address, 1, false, "eep"); | 206 ProcessPacket(client_address, 1, false, "eep"); |
| 190 } | 207 } |
| 191 | 208 |
| 192 TEST_F(QuicDispatcherTest, Shutdown) { | 209 TEST_F(QuicDispatcherTest, Shutdown) { |
| 193 IPEndPoint client_address(net::test::Loopback4(), 1); | 210 IPEndPoint client_address(net::test::Loopback4(), 1); |
| 194 | 211 |
| 195 EXPECT_CALL(dispatcher_, CreateQuicSession(_, _, client_address)) | 212 EXPECT_CALL(dispatcher_, CreateQuicSession(_, _, client_address)) |
| 196 .WillOnce(testing::Return( | 213 .WillOnce(testing::Return( |
| 197 CreateSession(&dispatcher_, 1, client_address, &session1_))); | 214 CreateSession(&dispatcher_, config_, 1, client_address, &session1_))); |
| 198 | 215 |
| 199 ProcessPacket(client_address, 1, true, "foo"); | 216 ProcessPacket(client_address, 1, true, "foo"); |
| 200 | 217 |
| 201 EXPECT_CALL(*reinterpret_cast<MockConnection*>(session1_->connection()), | 218 EXPECT_CALL(*reinterpret_cast<MockConnection*>(session1_->connection()), |
| 202 SendConnectionClose(QUIC_PEER_GOING_AWAY)); | 219 SendConnectionClose(QUIC_PEER_GOING_AWAY)); |
| 203 | 220 |
| 204 dispatcher_.Shutdown(); | 221 dispatcher_.Shutdown(); |
| 205 } | 222 } |
| 206 | 223 |
| 207 TEST_F(QuicDispatcherTest, TimeWaitListManager) { | 224 TEST_F(QuicDispatcherTest, TimeWaitListManager) { |
| 208 CreateTimeWaitListManager(); | 225 CreateTimeWaitListManager(); |
| 209 | 226 |
| 210 // Create a new session. | 227 // Create a new session. |
| 211 IPEndPoint client_address(net::test::Loopback4(), 1); | 228 IPEndPoint client_address(net::test::Loopback4(), 1); |
| 212 QuicConnectionId connection_id = 1; | 229 QuicConnectionId connection_id = 1; |
| 213 EXPECT_CALL(dispatcher_, CreateQuicSession(connection_id, _, client_address)) | 230 EXPECT_CALL(dispatcher_, CreateQuicSession(connection_id, _, client_address)) |
| 214 .WillOnce(testing::Return(CreateSession(&dispatcher_, connection_id, | 231 .WillOnce(testing::Return(CreateSession( |
| 215 client_address, &session1_))); | 232 &dispatcher_, config_, connection_id, client_address, &session1_))); |
| 216 ProcessPacket(client_address, connection_id, true, "foo"); | 233 ProcessPacket(client_address, connection_id, true, "foo"); |
| 217 | 234 |
| 218 // Close the connection by sending public reset packet. | 235 // Close the connection by sending public reset packet. |
| 219 QuicPublicResetPacket packet; | 236 QuicPublicResetPacket packet; |
| 220 packet.public_header.connection_id = connection_id; | 237 packet.public_header.connection_id = connection_id; |
| 221 packet.public_header.reset_flag = true; | 238 packet.public_header.reset_flag = true; |
| 222 packet.public_header.version_flag = false; | 239 packet.public_header.version_flag = false; |
| 223 packet.rejected_sequence_number = 19191; | 240 packet.rejected_sequence_number = 19191; |
| 224 packet.nonce_proof = 132232; | 241 packet.nonce_proof = 132232; |
| 225 scoped_ptr<QuicEncryptedPacket> encrypted( | 242 scoped_ptr<QuicEncryptedPacket> encrypted( |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 303 public: | 320 public: |
| 304 void SetUp() override { | 321 void SetUp() override { |
| 305 writer_ = new BlockingWriter; | 322 writer_ = new BlockingWriter; |
| 306 QuicDispatcherPeer::SetPacketWriterFactory(&dispatcher_, | 323 QuicDispatcherPeer::SetPacketWriterFactory(&dispatcher_, |
| 307 new TestWriterFactory()); | 324 new TestWriterFactory()); |
| 308 QuicDispatcherPeer::UseWriter(&dispatcher_, writer_); | 325 QuicDispatcherPeer::UseWriter(&dispatcher_, writer_); |
| 309 | 326 |
| 310 IPEndPoint client_address(net::test::Loopback4(), 1); | 327 IPEndPoint client_address(net::test::Loopback4(), 1); |
| 311 | 328 |
| 312 EXPECT_CALL(dispatcher_, CreateQuicSession(_, _, client_address)) | 329 EXPECT_CALL(dispatcher_, CreateQuicSession(_, _, client_address)) |
| 313 .WillOnce(testing::Return( | 330 .WillOnce(testing::Return(CreateSession(&dispatcher_, config_, 1, |
| 314 CreateSession(&dispatcher_, 1, client_address, &session1_))); | 331 client_address, &session1_))); |
| 315 ProcessPacket(client_address, 1, true, "foo"); | 332 ProcessPacket(client_address, 1, true, "foo"); |
| 316 | 333 |
| 317 EXPECT_CALL(dispatcher_, CreateQuicSession(_, _, client_address)) | 334 EXPECT_CALL(dispatcher_, CreateQuicSession(_, _, client_address)) |
| 318 .WillOnce(testing::Return( | 335 .WillOnce(testing::Return(CreateSession(&dispatcher_, config_, 2, |
| 319 CreateSession(&dispatcher_, 2, client_address, &session2_))); | 336 client_address, &session2_))); |
| 320 ProcessPacket(client_address, 2, true, "bar"); | 337 ProcessPacket(client_address, 2, true, "bar"); |
| 321 | 338 |
| 322 blocked_list_ = QuicDispatcherPeer::GetWriteBlockedList(&dispatcher_); | 339 blocked_list_ = QuicDispatcherPeer::GetWriteBlockedList(&dispatcher_); |
| 323 } | 340 } |
| 324 | 341 |
| 325 void TearDown() override { | 342 void TearDown() override { |
| 326 EXPECT_CALL(*connection1(), SendConnectionClose(QUIC_PEER_GOING_AWAY)); | 343 EXPECT_CALL(*connection1(), SendConnectionClose(QUIC_PEER_GOING_AWAY)); |
| 327 EXPECT_CALL(*connection2(), SendConnectionClose(QUIC_PEER_GOING_AWAY)); | 344 EXPECT_CALL(*connection2(), SendConnectionClose(QUIC_PEER_GOING_AWAY)); |
| 328 dispatcher_.Shutdown(); | 345 dispatcher_.Shutdown(); |
| 329 } | 346 } |
| (...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 469 // And we'll resume where we left off when we get another call. | 486 // And we'll resume where we left off when we get another call. |
| 470 EXPECT_CALL(*connection2(), OnCanWrite()); | 487 EXPECT_CALL(*connection2(), OnCanWrite()); |
| 471 dispatcher_.OnCanWrite(); | 488 dispatcher_.OnCanWrite(); |
| 472 EXPECT_FALSE(dispatcher_.HasPendingWrites()); | 489 EXPECT_FALSE(dispatcher_.HasPendingWrites()); |
| 473 } | 490 } |
| 474 | 491 |
| 475 } // namespace | 492 } // namespace |
| 476 } // namespace test | 493 } // namespace test |
| 477 } // namespace tools | 494 } // namespace tools |
| 478 } // namespace net | 495 } // namespace net |
| OLD | NEW |