| 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 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 51 using QuicDispatcher::write_blocked_list; | 51 using QuicDispatcher::write_blocked_list; |
| 52 }; | 52 }; |
| 53 | 53 |
| 54 // A Connection class which unregisters the session from the dispatcher | 54 // A Connection class which unregisters the session from the dispatcher |
| 55 // when sending connection close. | 55 // when sending connection close. |
| 56 // It'd be slightly more realistic to do this from the Session but it would | 56 // It'd be slightly more realistic to do this from the Session but it would |
| 57 // involve a lot more mocking. | 57 // involve a lot more mocking. |
| 58 class MockServerConnection : public MockConnection { | 58 class MockServerConnection : public MockConnection { |
| 59 public: | 59 public: |
| 60 MockServerConnection(QuicGuid guid, | 60 MockServerConnection(QuicGuid guid, |
| 61 IPEndPoint address, | |
| 62 int fd, | |
| 63 EpollServer* eps, | |
| 64 QuicDispatcher* dispatcher) | 61 QuicDispatcher* dispatcher) |
| 65 : MockConnection(guid, address, fd, eps, true), | 62 : MockConnection(guid, true), |
| 66 dispatcher_(dispatcher) { | 63 dispatcher_(dispatcher) { |
| 67 } | 64 } |
| 68 void UnregisterOnConnectionClosed() { | 65 void UnregisterOnConnectionClosed() { |
| 69 LOG(ERROR) << "Unregistering " << guid(); | 66 LOG(ERROR) << "Unregistering " << guid(); |
| 70 dispatcher_->OnConnectionClosed(guid(), QUIC_NO_ERROR); | 67 dispatcher_->OnConnectionClosed(guid(), QUIC_NO_ERROR); |
| 71 } | 68 } |
| 72 private: | 69 private: |
| 73 QuicDispatcher* dispatcher_; | 70 QuicDispatcher* dispatcher_; |
| 74 }; | 71 }; |
| 75 | 72 |
| 76 QuicSession* CreateSession(QuicDispatcher* dispatcher, | 73 QuicSession* CreateSession(QuicDispatcher* dispatcher, |
| 77 QuicGuid guid, | 74 QuicGuid guid, |
| 78 const IPEndPoint& addr, | 75 const IPEndPoint& addr, |
| 79 MockSession** session, | 76 MockSession** session) { |
| 80 EpollServer* eps) { | 77 MockServerConnection* connection = new MockServerConnection(guid, dispatcher); |
| 81 MockServerConnection* connection = | |
| 82 new MockServerConnection(guid, addr, 0, eps, dispatcher); | |
| 83 *session = new MockSession(connection, true); | 78 *session = new MockSession(connection, true); |
| 84 ON_CALL(*connection, SendConnectionClose(_)).WillByDefault( | 79 ON_CALL(*connection, SendConnectionClose(_)).WillByDefault( |
| 85 WithoutArgs(Invoke( | 80 WithoutArgs(Invoke( |
| 86 connection, &MockServerConnection::UnregisterOnConnectionClosed))); | 81 connection, &MockServerConnection::UnregisterOnConnectionClosed))); |
| 87 EXPECT_CALL(*reinterpret_cast<MockConnection*>((*session)->connection()), | 82 EXPECT_CALL(*reinterpret_cast<MockConnection*>((*session)->connection()), |
| 88 ProcessUdpPacket(_, addr, _)); | 83 ProcessUdpPacket(_, addr, _)); |
| 89 | 84 |
| 90 return *session; | 85 return *session; |
| 91 } | 86 } |
| 92 | 87 |
| (...skipping 23 matching lines...) Expand all Loading... |
| 116 const string& data) { | 111 const string& data) { |
| 117 dispatcher_.ProcessPacket( | 112 dispatcher_.ProcessPacket( |
| 118 IPEndPoint(), addr, guid, has_version_flag, | 113 IPEndPoint(), addr, guid, has_version_flag, |
| 119 QuicEncryptedPacket(data.data(), data.length())); | 114 QuicEncryptedPacket(data.data(), data.length())); |
| 120 } | 115 } |
| 121 | 116 |
| 122 void ValidatePacket(const QuicEncryptedPacket& packet) { | 117 void ValidatePacket(const QuicEncryptedPacket& packet) { |
| 123 EXPECT_TRUE(packet.AsStringPiece().find(data_) != StringPiece::npos); | 118 EXPECT_TRUE(packet.AsStringPiece().find(data_) != StringPiece::npos); |
| 124 } | 119 } |
| 125 | 120 |
| 126 IPAddressNumber Loopback4() { | |
| 127 net::IPAddressNumber addr; | |
| 128 CHECK(net::ParseIPLiteralToNumber("127.0.0.1", &addr)); | |
| 129 return addr; | |
| 130 } | |
| 131 | |
| 132 EpollServer eps_; | 121 EpollServer eps_; |
| 133 QuicConfig config_; | 122 QuicConfig config_; |
| 134 QuicCryptoServerConfig crypto_config_; | 123 QuicCryptoServerConfig crypto_config_; |
| 135 TestDispatcher dispatcher_; | 124 TestDispatcher dispatcher_; |
| 136 MockSession* session1_; | 125 MockSession* session1_; |
| 137 MockSession* session2_; | 126 MockSession* session2_; |
| 138 string data_; | 127 string data_; |
| 139 }; | 128 }; |
| 140 | 129 |
| 141 TEST_F(QuicDispatcherTest, ProcessPackets) { | 130 TEST_F(QuicDispatcherTest, ProcessPackets) { |
| 142 IPEndPoint addr(Loopback4(), 1); | 131 IPEndPoint addr(net::test::Loopback4(), 1); |
| 143 | 132 |
| 144 EXPECT_CALL(dispatcher_, CreateQuicSession(1, _, addr)) | 133 EXPECT_CALL(dispatcher_, CreateQuicSession(1, _, addr)) |
| 145 .WillOnce(testing::Return(CreateSession( | 134 .WillOnce(testing::Return(CreateSession( |
| 146 &dispatcher_, 1, addr, &session1_, &eps_))); | 135 &dispatcher_, 1, addr, &session1_))); |
| 147 ProcessPacket(addr, 1, true, "foo"); | 136 ProcessPacket(addr, 1, true, "foo"); |
| 148 | 137 |
| 149 EXPECT_CALL(dispatcher_, CreateQuicSession(2, _, addr)) | 138 EXPECT_CALL(dispatcher_, CreateQuicSession(2, _, addr)) |
| 150 .WillOnce(testing::Return(CreateSession( | 139 .WillOnce(testing::Return(CreateSession( |
| 151 &dispatcher_, 2, addr, &session2_, &eps_))); | 140 &dispatcher_, 2, addr, &session2_))); |
| 152 ProcessPacket(addr, 2, true, "bar"); | 141 ProcessPacket(addr, 2, true, "bar"); |
| 153 | 142 |
| 154 data_ = "eep"; | 143 data_ = "eep"; |
| 155 EXPECT_CALL(*reinterpret_cast<MockConnection*>(session1_->connection()), | 144 EXPECT_CALL(*reinterpret_cast<MockConnection*>(session1_->connection()), |
| 156 ProcessUdpPacket(_, _, _)).Times(1). | 145 ProcessUdpPacket(_, _, _)).Times(1). |
| 157 WillOnce(testing::WithArgs<2>(Invoke( | 146 WillOnce(testing::WithArgs<2>(Invoke( |
| 158 this, &QuicDispatcherTest::ValidatePacket))); | 147 this, &QuicDispatcherTest::ValidatePacket))); |
| 159 ProcessPacket(addr, 1, false, "eep"); | 148 ProcessPacket(addr, 1, false, "eep"); |
| 160 } | 149 } |
| 161 | 150 |
| 162 TEST_F(QuicDispatcherTest, Shutdown) { | 151 TEST_F(QuicDispatcherTest, Shutdown) { |
| 163 IPEndPoint addr(Loopback4(), 1); | 152 IPEndPoint addr(net::test::Loopback4(), 1); |
| 164 | 153 |
| 165 EXPECT_CALL(dispatcher_, CreateQuicSession(_, _, addr)) | 154 EXPECT_CALL(dispatcher_, CreateQuicSession(_, _, addr)) |
| 166 .WillOnce(testing::Return(CreateSession( | 155 .WillOnce(testing::Return(CreateSession( |
| 167 &dispatcher_, 1, addr, &session1_, &eps_))); | 156 &dispatcher_, 1, addr, &session1_))); |
| 168 | 157 |
| 169 ProcessPacket(addr, 1, true, "foo"); | 158 ProcessPacket(addr, 1, true, "foo"); |
| 170 | 159 |
| 171 EXPECT_CALL(*reinterpret_cast<MockConnection*>(session1_->connection()), | 160 EXPECT_CALL(*reinterpret_cast<MockConnection*>(session1_->connection()), |
| 172 SendConnectionClose(QUIC_PEER_GOING_AWAY)); | 161 SendConnectionClose(QUIC_PEER_GOING_AWAY)); |
| 173 | 162 |
| 174 dispatcher_.Shutdown(); | 163 dispatcher_.Shutdown(); |
| 175 } | 164 } |
| 176 | 165 |
| 177 class MockTimeWaitListManager : public QuicTimeWaitListManager { | 166 class MockTimeWaitListManager : public QuicTimeWaitListManager { |
| (...skipping 10 matching lines...) Expand all Loading... |
| 188 }; | 177 }; |
| 189 | 178 |
| 190 TEST_F(QuicDispatcherTest, TimeWaitListManager) { | 179 TEST_F(QuicDispatcherTest, TimeWaitListManager) { |
| 191 MockTimeWaitListManager* time_wait_list_manager = | 180 MockTimeWaitListManager* time_wait_list_manager = |
| 192 new MockTimeWaitListManager( | 181 new MockTimeWaitListManager( |
| 193 QuicDispatcherPeer::GetWriter(&dispatcher_), &eps_); | 182 QuicDispatcherPeer::GetWriter(&dispatcher_), &eps_); |
| 194 // dispatcher takes the ownership of time_wait_list_manager. | 183 // dispatcher takes the ownership of time_wait_list_manager. |
| 195 QuicDispatcherPeer::SetTimeWaitListManager(&dispatcher_, | 184 QuicDispatcherPeer::SetTimeWaitListManager(&dispatcher_, |
| 196 time_wait_list_manager); | 185 time_wait_list_manager); |
| 197 // Create a new session. | 186 // Create a new session. |
| 198 IPEndPoint addr(Loopback4(), 1); | 187 IPEndPoint addr(net::test::Loopback4(), 1); |
| 199 QuicGuid guid = 1; | 188 QuicGuid guid = 1; |
| 200 EXPECT_CALL(dispatcher_, CreateQuicSession(guid, _, addr)) | 189 EXPECT_CALL(dispatcher_, CreateQuicSession(guid, _, addr)) |
| 201 .WillOnce(testing::Return(CreateSession( | 190 .WillOnce(testing::Return(CreateSession( |
| 202 &dispatcher_, guid, addr, &session1_, &eps_))); | 191 &dispatcher_, guid, addr, &session1_))); |
| 203 ProcessPacket(addr, guid, true, "foo"); | 192 ProcessPacket(addr, guid, true, "foo"); |
| 204 | 193 |
| 205 // Close the connection by sending public reset packet. | 194 // Close the connection by sending public reset packet. |
| 206 QuicPublicResetPacket packet; | 195 QuicPublicResetPacket packet; |
| 207 packet.public_header.guid = guid; | 196 packet.public_header.guid = guid; |
| 208 packet.public_header.reset_flag = true; | 197 packet.public_header.reset_flag = true; |
| 209 packet.public_header.version_flag = false; | 198 packet.public_header.version_flag = false; |
| 210 packet.rejected_sequence_number = 19191; | 199 packet.rejected_sequence_number = 19191; |
| 211 packet.nonce_proof = 132232; | 200 packet.nonce_proof = 132232; |
| 212 scoped_ptr<QuicEncryptedPacket> encrypted( | 201 scoped_ptr<QuicEncryptedPacket> encrypted( |
| (...skipping 17 matching lines...) Expand all Loading... |
| 230 } | 219 } |
| 231 | 220 |
| 232 TEST_F(QuicDispatcherTest, StrayPacketToTimeWaitListManager) { | 221 TEST_F(QuicDispatcherTest, StrayPacketToTimeWaitListManager) { |
| 233 MockTimeWaitListManager* time_wait_list_manager = | 222 MockTimeWaitListManager* time_wait_list_manager = |
| 234 new MockTimeWaitListManager( | 223 new MockTimeWaitListManager( |
| 235 QuicDispatcherPeer::GetWriter(&dispatcher_), &eps_); | 224 QuicDispatcherPeer::GetWriter(&dispatcher_), &eps_); |
| 236 // dispatcher takes the ownership of time_wait_list_manager. | 225 // dispatcher takes the ownership of time_wait_list_manager. |
| 237 QuicDispatcherPeer::SetTimeWaitListManager(&dispatcher_, | 226 QuicDispatcherPeer::SetTimeWaitListManager(&dispatcher_, |
| 238 time_wait_list_manager); | 227 time_wait_list_manager); |
| 239 | 228 |
| 240 IPEndPoint addr(Loopback4(), 1); | 229 IPEndPoint addr(net::test::Loopback4(), 1); |
| 241 QuicGuid guid = 1; | 230 QuicGuid guid = 1; |
| 242 // Dispatcher forwards all packets for this guid to the time wait list | 231 // Dispatcher forwards all packets for this guid to the time wait list |
| 243 // manager. | 232 // manager. |
| 244 EXPECT_CALL(dispatcher_, CreateQuicSession(_, _, _)).Times(0); | 233 EXPECT_CALL(dispatcher_, CreateQuicSession(_, _, _)).Times(0); |
| 245 EXPECT_CALL(*time_wait_list_manager, ProcessPacket(_, _, guid, _)).Times(1); | 234 EXPECT_CALL(*time_wait_list_manager, ProcessPacket(_, _, guid, _)).Times(1); |
| 246 string data = "foo"; | 235 string data = "foo"; |
| 247 ProcessPacket(addr, guid, false, "foo"); | 236 ProcessPacket(addr, guid, false, "foo"); |
| 248 } | 237 } |
| 249 | 238 |
| 250 class QuicWriteBlockedListTest : public QuicDispatcherTest { | 239 class QuicWriteBlockedListTest : public QuicDispatcherTest { |
| 251 public: | 240 public: |
| 252 virtual void SetUp() { | 241 virtual void SetUp() { |
| 253 IPEndPoint addr(Loopback4(), 1); | 242 IPEndPoint addr(net::test::Loopback4(), 1); |
| 254 | 243 |
| 255 EXPECT_CALL(dispatcher_, CreateQuicSession(_, _, addr)) | 244 EXPECT_CALL(dispatcher_, CreateQuicSession(_, _, addr)) |
| 256 .WillOnce(testing::Return(CreateSession( | 245 .WillOnce(testing::Return(CreateSession( |
| 257 &dispatcher_, 1, addr, &session1_, &eps_))); | 246 &dispatcher_, 1, addr, &session1_))); |
| 258 ProcessPacket(addr, 1, true, "foo"); | 247 ProcessPacket(addr, 1, true, "foo"); |
| 259 | 248 |
| 260 EXPECT_CALL(dispatcher_, CreateQuicSession(_, _, addr)) | 249 EXPECT_CALL(dispatcher_, CreateQuicSession(_, _, addr)) |
| 261 .WillOnce(testing::Return(CreateSession( | 250 .WillOnce(testing::Return(CreateSession( |
| 262 &dispatcher_, 2, addr, &session2_, &eps_))); | 251 &dispatcher_, 2, addr, &session2_))); |
| 263 ProcessPacket(addr, 2, true, "bar"); | 252 ProcessPacket(addr, 2, true, "bar"); |
| 264 | 253 |
| 265 blocked_list_ = dispatcher_.write_blocked_list(); | 254 blocked_list_ = dispatcher_.write_blocked_list(); |
| 266 } | 255 } |
| 267 | 256 |
| 268 virtual void TearDown() { | 257 virtual void TearDown() { |
| 269 EXPECT_CALL(*connection1(), SendConnectionClose(QUIC_PEER_GOING_AWAY)); | 258 EXPECT_CALL(*connection1(), SendConnectionClose(QUIC_PEER_GOING_AWAY)); |
| 270 EXPECT_CALL(*connection2(), SendConnectionClose(QUIC_PEER_GOING_AWAY)); | 259 EXPECT_CALL(*connection2(), SendConnectionClose(QUIC_PEER_GOING_AWAY)); |
| 271 dispatcher_.Shutdown(); | 260 dispatcher_.Shutdown(); |
| 272 } | 261 } |
| (...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 390 | 379 |
| 391 // And we'll resume where we left off when we get another call. | 380 // And we'll resume where we left off when we get another call. |
| 392 EXPECT_CALL(*connection2(), OnCanWrite()); | 381 EXPECT_CALL(*connection2(), OnCanWrite()); |
| 393 dispatcher_.OnCanWrite(); | 382 dispatcher_.OnCanWrite(); |
| 394 } | 383 } |
| 395 | 384 |
| 396 } // namespace | 385 } // namespace |
| 397 } // namespace test | 386 } // namespace test |
| 398 } // namespace tools | 387 } // namespace tools |
| 399 } // namespace net | 388 } // namespace net |
| OLD | NEW |