| 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 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 98 virtual ~QuicDispatcherTest() {} | 98 virtual ~QuicDispatcherTest() {} |
| 99 | 99 |
| 100 MockConnection* connection1() { | 100 MockConnection* connection1() { |
| 101 return reinterpret_cast<MockConnection*>(session1_->connection()); | 101 return reinterpret_cast<MockConnection*>(session1_->connection()); |
| 102 } | 102 } |
| 103 | 103 |
| 104 MockConnection* connection2() { | 104 MockConnection* connection2() { |
| 105 return reinterpret_cast<MockConnection*>(session2_->connection()); | 105 return reinterpret_cast<MockConnection*>(session2_->connection()); |
| 106 } | 106 } |
| 107 | 107 |
| 108 QuicEncryptedPacket* ConstructEncryptedPacket( |
| 109 QuicGuid guid, |
| 110 bool version_flag, |
| 111 bool reset_flag, |
| 112 QuicPacketSequenceNumber sequence_number, |
| 113 const string& data) { |
| 114 QuicPacketHeader header; |
| 115 header.public_header.guid = guid; |
| 116 header.public_header.guid_length = PACKET_8BYTE_GUID; |
| 117 header.public_header.version_flag = version_flag; |
| 118 header.public_header.reset_flag = reset_flag; |
| 119 header.public_header.sequence_number_length = PACKET_6BYTE_SEQUENCE_NUMBER; |
| 120 header.packet_sequence_number = sequence_number; |
| 121 header.entropy_flag = false; |
| 122 header.entropy_hash = 0; |
| 123 header.fec_flag = false; |
| 124 header.is_in_fec_group = NOT_IN_FEC_GROUP; |
| 125 header.fec_group = 0; |
| 126 QuicStreamFrame stream_frame(1, false, 0, MakeIOVector(data)); |
| 127 QuicFrame frame(&stream_frame); |
| 128 QuicFrames frames; |
| 129 frames.push_back(frame); |
| 130 QuicFramer framer(QuicSupportedVersions(), QuicTime::Zero(), false); |
| 131 scoped_ptr<QuicPacket> packet( |
| 132 framer.BuildUnsizedDataPacket(header, frames).packet); |
| 133 EXPECT_TRUE(packet != NULL); |
| 134 QuicEncryptedPacket* encrypted = framer.EncryptPacket(ENCRYPTION_NONE, |
| 135 sequence_number, |
| 136 *packet); |
| 137 EXPECT_TRUE(encrypted != NULL); |
| 138 data_ = string(encrypted->data(), encrypted->length()); |
| 139 return encrypted; |
| 140 } |
| 141 |
| 108 void ProcessPacket(IPEndPoint addr, | 142 void ProcessPacket(IPEndPoint addr, |
| 109 QuicGuid guid, | 143 QuicGuid guid, |
| 110 bool has_version_flag, | 144 bool has_version_flag, |
| 111 const string& data) { | 145 const string& data) { |
| 112 dispatcher_.ProcessPacket( | 146 scoped_ptr<QuicEncryptedPacket> packet( |
| 113 IPEndPoint(), addr, guid, has_version_flag, | 147 ConstructEncryptedPacket(guid, has_version_flag, false, 1, data)); |
| 114 QuicEncryptedPacket(data.data(), data.length())); | 148 dispatcher_.ProcessPacket(IPEndPoint(), addr, *packet.get()); |
| 115 } | 149 } |
| 116 | 150 |
| 117 void ValidatePacket(const QuicEncryptedPacket& packet) { | 151 void ValidatePacket(const QuicEncryptedPacket& packet) { |
| 118 EXPECT_TRUE(packet.AsStringPiece().find(data_) != StringPiece::npos); | 152 EXPECT_EQ(data_.length(), packet.AsStringPiece().length()); |
| 153 EXPECT_EQ(data_, packet.AsStringPiece()); |
| 119 } | 154 } |
| 120 | 155 |
| 121 EpollServer eps_; | 156 EpollServer eps_; |
| 122 QuicConfig config_; | 157 QuicConfig config_; |
| 123 QuicCryptoServerConfig crypto_config_; | 158 QuicCryptoServerConfig crypto_config_; |
| 124 TestDispatcher dispatcher_; | 159 TestDispatcher dispatcher_; |
| 125 MockSession* session1_; | 160 MockSession* session1_; |
| 126 MockSession* session2_; | 161 MockSession* session2_; |
| 127 string data_; | 162 string data_; |
| 128 }; | 163 }; |
| 129 | 164 |
| 130 TEST_F(QuicDispatcherTest, ProcessPackets) { | 165 TEST_F(QuicDispatcherTest, ProcessPackets) { |
| 131 IPEndPoint addr(net::test::Loopback4(), 1); | 166 IPEndPoint addr(net::test::Loopback4(), 1); |
| 132 | 167 |
| 133 EXPECT_CALL(dispatcher_, CreateQuicSession(1, _, addr)) | 168 EXPECT_CALL(dispatcher_, CreateQuicSession(1, _, addr)) |
| 134 .WillOnce(testing::Return(CreateSession( | 169 .WillOnce(testing::Return(CreateSession( |
| 135 &dispatcher_, 1, addr, &session1_))); | 170 &dispatcher_, 1, addr, &session1_))); |
| 136 ProcessPacket(addr, 1, true, "foo"); | 171 ProcessPacket(addr, 1, true, "foo"); |
| 137 | 172 |
| 138 EXPECT_CALL(dispatcher_, CreateQuicSession(2, _, addr)) | 173 EXPECT_CALL(dispatcher_, CreateQuicSession(2, _, addr)) |
| 139 .WillOnce(testing::Return(CreateSession( | 174 .WillOnce(testing::Return(CreateSession( |
| 140 &dispatcher_, 2, addr, &session2_))); | 175 &dispatcher_, 2, addr, &session2_))); |
| 141 ProcessPacket(addr, 2, true, "bar"); | 176 ProcessPacket(addr, 2, true, "bar"); |
| 142 | 177 |
| 143 data_ = "eep"; | |
| 144 EXPECT_CALL(*reinterpret_cast<MockConnection*>(session1_->connection()), | 178 EXPECT_CALL(*reinterpret_cast<MockConnection*>(session1_->connection()), |
| 145 ProcessUdpPacket(_, _, _)).Times(1). | 179 ProcessUdpPacket(_, _, _)).Times(1). |
| 146 WillOnce(testing::WithArgs<2>(Invoke( | 180 WillOnce(testing::WithArgs<2>(Invoke( |
| 147 this, &QuicDispatcherTest::ValidatePacket))); | 181 this, &QuicDispatcherTest::ValidatePacket))); |
| 148 ProcessPacket(addr, 1, false, "eep"); | 182 ProcessPacket(addr, 1, false, "eep"); |
| 149 } | 183 } |
| 150 | 184 |
| 151 TEST_F(QuicDispatcherTest, Shutdown) { | 185 TEST_F(QuicDispatcherTest, Shutdown) { |
| 152 IPEndPoint addr(net::test::Loopback4(), 1); | 186 IPEndPoint addr(net::test::Loopback4(), 1); |
| 153 | 187 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 166 class MockTimeWaitListManager : public QuicTimeWaitListManager { | 200 class MockTimeWaitListManager : public QuicTimeWaitListManager { |
| 167 public: | 201 public: |
| 168 MockTimeWaitListManager(QuicPacketWriter* writer, | 202 MockTimeWaitListManager(QuicPacketWriter* writer, |
| 169 EpollServer* eps) | 203 EpollServer* eps) |
| 170 : QuicTimeWaitListManager(writer, eps, QuicSupportedVersions()) { | 204 : QuicTimeWaitListManager(writer, eps, QuicSupportedVersions()) { |
| 171 } | 205 } |
| 172 | 206 |
| 173 MOCK_METHOD4(ProcessPacket, void(const IPEndPoint& server_address, | 207 MOCK_METHOD4(ProcessPacket, void(const IPEndPoint& server_address, |
| 174 const IPEndPoint& client_address, | 208 const IPEndPoint& client_address, |
| 175 QuicGuid guid, | 209 QuicGuid guid, |
| 176 const QuicEncryptedPacket& packet)); | 210 QuicPacketSequenceNumber sequence_number)); |
| 177 }; | 211 }; |
| 178 | 212 |
| 179 TEST_F(QuicDispatcherTest, TimeWaitListManager) { | 213 TEST_F(QuicDispatcherTest, TimeWaitListManager) { |
| 180 MockTimeWaitListManager* time_wait_list_manager = | 214 MockTimeWaitListManager* time_wait_list_manager = |
| 181 new MockTimeWaitListManager( | 215 new MockTimeWaitListManager( |
| 182 QuicDispatcherPeer::GetWriter(&dispatcher_), &eps_); | 216 QuicDispatcherPeer::GetWriter(&dispatcher_), &eps_); |
| 183 // dispatcher takes the ownership of time_wait_list_manager. | 217 // dispatcher takes the ownership of time_wait_list_manager. |
| 184 QuicDispatcherPeer::SetTimeWaitListManager(&dispatcher_, | 218 QuicDispatcherPeer::SetTimeWaitListManager(&dispatcher_, |
| 185 time_wait_list_manager); | 219 time_wait_list_manager); |
| 186 // Create a new session. | 220 // Create a new session. |
| (...skipping 15 matching lines...) Expand all Loading... |
| 202 QuicFramer::BuildPublicResetPacket(packet)); | 236 QuicFramer::BuildPublicResetPacket(packet)); |
| 203 EXPECT_CALL(*session1_, OnConnectionClosed(QUIC_PUBLIC_RESET, true)).Times(1) | 237 EXPECT_CALL(*session1_, OnConnectionClosed(QUIC_PUBLIC_RESET, true)).Times(1) |
| 204 .WillOnce(WithoutArgs(Invoke( | 238 .WillOnce(WithoutArgs(Invoke( |
| 205 reinterpret_cast<MockServerConnection*>(session1_->connection()), | 239 reinterpret_cast<MockServerConnection*>(session1_->connection()), |
| 206 &MockServerConnection::UnregisterOnConnectionClosed))); | 240 &MockServerConnection::UnregisterOnConnectionClosed))); |
| 207 EXPECT_CALL(*reinterpret_cast<MockConnection*>(session1_->connection()), | 241 EXPECT_CALL(*reinterpret_cast<MockConnection*>(session1_->connection()), |
| 208 ProcessUdpPacket(_, _, _)) | 242 ProcessUdpPacket(_, _, _)) |
| 209 .WillOnce(Invoke( | 243 .WillOnce(Invoke( |
| 210 reinterpret_cast<MockConnection*>(session1_->connection()), | 244 reinterpret_cast<MockConnection*>(session1_->connection()), |
| 211 &MockConnection::ReallyProcessUdpPacket)); | 245 &MockConnection::ReallyProcessUdpPacket)); |
| 212 dispatcher_.ProcessPacket(IPEndPoint(), addr, guid, true, *encrypted); | 246 dispatcher_.ProcessPacket(IPEndPoint(), addr, *encrypted); |
| 213 EXPECT_TRUE(time_wait_list_manager->IsGuidInTimeWait(guid)); | 247 EXPECT_TRUE(time_wait_list_manager->IsGuidInTimeWait(guid)); |
| 214 | 248 |
| 215 // Dispatcher forwards subsequent packets for this guid to the time wait list | 249 // Dispatcher forwards subsequent packets for this guid to the time wait list |
| 216 // manager. | 250 // manager. |
| 217 EXPECT_CALL(*time_wait_list_manager, ProcessPacket(_, _, guid, _)).Times(1); | 251 EXPECT_CALL(*time_wait_list_manager, ProcessPacket(_, _, guid, _)).Times(1); |
| 218 ProcessPacket(addr, guid, true, "foo"); | 252 ProcessPacket(addr, guid, true, "foo"); |
| 219 } | 253 } |
| 220 | 254 |
| 221 TEST_F(QuicDispatcherTest, StrayPacketToTimeWaitListManager) { | 255 TEST_F(QuicDispatcherTest, StrayPacketToTimeWaitListManager) { |
| 222 MockTimeWaitListManager* time_wait_list_manager = | 256 MockTimeWaitListManager* time_wait_list_manager = |
| (...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 379 | 413 |
| 380 // And we'll resume where we left off when we get another call. | 414 // And we'll resume where we left off when we get another call. |
| 381 EXPECT_CALL(*connection2(), OnCanWrite()); | 415 EXPECT_CALL(*connection2(), OnCanWrite()); |
| 382 dispatcher_.OnCanWrite(); | 416 dispatcher_.OnCanWrite(); |
| 383 } | 417 } |
| 384 | 418 |
| 385 } // namespace | 419 } // namespace |
| 386 } // namespace test | 420 } // namespace test |
| 387 } // namespace tools | 421 } // namespace tools |
| 388 } // namespace net | 422 } // namespace net |
| OLD | NEW |