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/quic/test_tools/quic_test_utils.h" | 5 #include "net/quic/test_tools/quic_test_utils.h" |
6 | 6 |
7 #include "base/sha1.h" | 7 #include "base/sha1.h" |
8 #include "base/stl_util.h" | 8 #include "base/stl_util.h" |
9 #include "base/strings/string_number_conversions.h" | 9 #include "base/strings/string_number_conversions.h" |
10 #include "net/quic/crypto/crypto_framer.h" | 10 #include "net/quic/crypto/crypto_framer.h" |
(...skipping 15 matching lines...) Expand all Loading... |
26 using std::max; | 26 using std::max; |
27 using std::min; | 27 using std::min; |
28 using std::string; | 28 using std::string; |
29 using testing::Invoke; | 29 using testing::Invoke; |
30 using testing::_; | 30 using testing::_; |
31 | 31 |
32 namespace net { | 32 namespace net { |
33 namespace test { | 33 namespace test { |
34 namespace { | 34 namespace { |
35 | 35 |
36 // No-op alarm implementation used by MockHelper. | 36 // No-op alarm implementation used by MockConnectionHelper. |
37 class TestAlarm : public QuicAlarm { | 37 class TestAlarm : public QuicAlarm { |
38 public: | 38 public: |
39 explicit TestAlarm(QuicAlarm::Delegate* delegate) | 39 explicit TestAlarm(QuicAlarm::Delegate* delegate) |
40 : QuicAlarm(delegate) { | 40 : QuicAlarm(delegate) { |
41 } | 41 } |
42 | 42 |
43 void SetImpl() override {} | 43 void SetImpl() override {} |
44 void CancelImpl() override {} | 44 void CancelImpl() override {} |
45 }; | 45 }; |
46 | 46 |
(...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
197 } | 197 } |
198 | 198 |
199 bool NoOpFramerVisitor::OnBlockedFrame(const QuicBlockedFrame& frame) { | 199 bool NoOpFramerVisitor::OnBlockedFrame(const QuicBlockedFrame& frame) { |
200 return true; | 200 return true; |
201 } | 201 } |
202 | 202 |
203 MockConnectionVisitor::MockConnectionVisitor() {} | 203 MockConnectionVisitor::MockConnectionVisitor() {} |
204 | 204 |
205 MockConnectionVisitor::~MockConnectionVisitor() {} | 205 MockConnectionVisitor::~MockConnectionVisitor() {} |
206 | 206 |
207 MockHelper::MockHelper() {} | 207 MockConnectionHelper::MockConnectionHelper() {} |
208 | 208 |
209 MockHelper::~MockHelper() {} | 209 MockConnectionHelper::~MockConnectionHelper() {} |
210 | 210 |
211 const QuicClock* MockHelper::GetClock() const { | 211 const QuicClock* MockConnectionHelper::GetClock() const { |
212 return &clock_; | 212 return &clock_; |
213 } | 213 } |
214 | 214 |
215 QuicRandom* MockHelper::GetRandomGenerator() { | 215 QuicRandom* MockConnectionHelper::GetRandomGenerator() { |
216 return &random_generator_; | 216 return &random_generator_; |
217 } | 217 } |
218 | 218 |
219 QuicAlarm* MockHelper::CreateAlarm(QuicAlarm::Delegate* delegate) { | 219 QuicAlarm* MockConnectionHelper::CreateAlarm(QuicAlarm::Delegate* delegate) { |
220 return new TestAlarm(delegate); | 220 return new TestAlarm(delegate); |
221 } | 221 } |
222 | 222 |
223 void MockHelper::AdvanceTime(QuicTime::Delta delta) { | 223 void MockConnectionHelper::AdvanceTime(QuicTime::Delta delta) { |
224 clock_.AdvanceTime(delta); | 224 clock_.AdvanceTime(delta); |
225 } | 225 } |
226 | 226 |
227 QuicPacketWriter* NiceMockPacketWriterFactory::Create( | 227 QuicPacketWriter* NiceMockPacketWriterFactory::Create( |
228 QuicConnection* /*connection*/) const { | 228 QuicConnection* /*connection*/) const { |
229 return new testing::NiceMock<MockPacketWriter>(); | 229 return new testing::NiceMock<MockPacketWriter>(); |
230 } | 230 } |
231 | 231 |
232 MockConnection::MockConnection(MockHelper* helper, Perspective perspective) | 232 MockConnection::MockConnection(MockConnectionHelper* helper, |
| 233 Perspective perspective) |
233 : MockConnection(kTestConnectionId, | 234 : MockConnection(kTestConnectionId, |
234 IPEndPoint(TestPeerIPAddress(), kTestPort), | 235 IPEndPoint(TestPeerIPAddress(), kTestPort), |
235 helper, | 236 helper, |
236 perspective, | 237 perspective, |
237 QuicSupportedVersions()) {} | 238 QuicSupportedVersions()) {} |
238 | 239 |
239 MockConnection::MockConnection(IPEndPoint address, | 240 MockConnection::MockConnection(IPEndPoint address, |
240 MockHelper* helper, | 241 MockConnectionHelper* helper, |
241 Perspective perspective) | 242 Perspective perspective) |
242 : MockConnection(kTestConnectionId, | 243 : MockConnection(kTestConnectionId, |
243 address, | 244 address, |
244 helper, | 245 helper, |
245 perspective, | 246 perspective, |
246 QuicSupportedVersions()) {} | 247 QuicSupportedVersions()) {} |
247 | 248 |
248 MockConnection::MockConnection(QuicConnectionId connection_id, | 249 MockConnection::MockConnection(QuicConnectionId connection_id, |
249 MockHelper* helper, | 250 MockConnectionHelper* helper, |
250 Perspective perspective) | 251 Perspective perspective) |
251 : MockConnection(connection_id, | 252 : MockConnection(connection_id, |
252 IPEndPoint(TestPeerIPAddress(), kTestPort), | 253 IPEndPoint(TestPeerIPAddress(), kTestPort), |
253 helper, | 254 helper, |
254 perspective, | 255 perspective, |
255 QuicSupportedVersions()) {} | 256 QuicSupportedVersions()) {} |
256 | 257 |
257 MockConnection::MockConnection(MockHelper* helper, | 258 MockConnection::MockConnection(MockConnectionHelper* helper, |
258 Perspective perspective, | 259 Perspective perspective, |
259 const QuicVersionVector& supported_versions) | 260 const QuicVersionVector& supported_versions) |
260 : MockConnection(kTestConnectionId, | 261 : MockConnection(kTestConnectionId, |
261 IPEndPoint(TestPeerIPAddress(), kTestPort), | 262 IPEndPoint(TestPeerIPAddress(), kTestPort), |
262 helper, | 263 helper, |
263 perspective, | 264 perspective, |
264 supported_versions) {} | 265 supported_versions) {} |
265 | 266 |
266 MockConnection::MockConnection(QuicConnectionId connection_id, | 267 MockConnection::MockConnection(QuicConnectionId connection_id, |
267 IPEndPoint address, | 268 IPEndPoint address, |
268 MockHelper* helper, | 269 MockConnectionHelper* helper, |
269 Perspective perspective, | 270 Perspective perspective, |
270 const QuicVersionVector& supported_versions) | 271 const QuicVersionVector& supported_versions) |
271 : QuicConnection(connection_id, | 272 : QuicConnection(connection_id, |
272 address, | 273 address, |
273 helper, | 274 helper, |
274 NiceMockPacketWriterFactory(), | 275 NiceMockPacketWriterFactory(), |
275 /* owns_writer= */ true, | 276 /* owns_writer= */ true, |
276 perspective, | 277 perspective, |
277 supported_versions) { | 278 supported_versions) { |
278 ON_CALL(*this, OnError(_)) | 279 ON_CALL(*this, OnError(_)) |
279 .WillByDefault( | 280 .WillByDefault( |
280 Invoke(this, &PacketSavingConnection::QuicConnection_OnError)); | 281 Invoke(this, &PacketSavingConnection::QuicConnection_OnError)); |
281 } | 282 } |
282 | 283 |
283 MockConnection::~MockConnection() {} | 284 MockConnection::~MockConnection() {} |
284 | 285 |
285 void MockConnection::AdvanceTime(QuicTime::Delta delta) { | 286 void MockConnection::AdvanceTime(QuicTime::Delta delta) { |
286 static_cast<MockHelper*>(helper())->AdvanceTime(delta); | 287 static_cast<MockConnectionHelper*>(helper())->AdvanceTime(delta); |
287 } | 288 } |
288 | 289 |
289 PacketSavingConnection::PacketSavingConnection(MockHelper* helper, | 290 PacketSavingConnection::PacketSavingConnection(MockConnectionHelper* helper, |
290 Perspective perspective) | 291 Perspective perspective) |
291 : MockConnection(helper, perspective) {} | 292 : MockConnection(helper, perspective) {} |
292 | 293 |
293 PacketSavingConnection::PacketSavingConnection( | 294 PacketSavingConnection::PacketSavingConnection( |
294 MockHelper* helper, | 295 MockConnectionHelper* helper, |
295 Perspective perspective, | 296 Perspective perspective, |
296 const QuicVersionVector& supported_versions) | 297 const QuicVersionVector& supported_versions) |
297 : MockConnection(helper, perspective, supported_versions) {} | 298 : MockConnection(helper, perspective, supported_versions) {} |
298 | 299 |
299 PacketSavingConnection::~PacketSavingConnection() { | 300 PacketSavingConnection::~PacketSavingConnection() { |
300 STLDeleteElements(&encrypted_packets_); | 301 STLDeleteElements(&encrypted_packets_); |
301 } | 302 } |
302 | 303 |
303 void PacketSavingConnection::SendOrQueuePacket(QueuedPacket packet) { | 304 void PacketSavingConnection::SendOrQueuePacket(QueuedPacket packet) { |
304 if (!packet.serialized_packet.packet->owns_buffer()) { | 305 if (!packet.serialized_packet.packet->owns_buffer()) { |
(...skipping 196 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
501 QuicStreamFrame stream_frame(1, false, 0, StringPiece(data)); | 502 QuicStreamFrame stream_frame(1, false, 0, StringPiece(data)); |
502 QuicFrame frame(&stream_frame); | 503 QuicFrame frame(&stream_frame); |
503 QuicFrames frames; | 504 QuicFrames frames; |
504 frames.push_back(frame); | 505 frames.push_back(frame); |
505 QuicFramer framer(versions != nullptr ? *versions : QuicSupportedVersions(), | 506 QuicFramer framer(versions != nullptr ? *versions : QuicSupportedVersions(), |
506 QuicTime::Zero(), Perspective::IS_CLIENT); | 507 QuicTime::Zero(), Perspective::IS_CLIENT); |
507 | 508 |
508 scoped_ptr<QuicPacket> packet( | 509 scoped_ptr<QuicPacket> packet( |
509 BuildUnsizedDataPacket(&framer, header, frames)); | 510 BuildUnsizedDataPacket(&framer, header, frames)); |
510 EXPECT_TRUE(packet != nullptr); | 511 EXPECT_TRUE(packet != nullptr); |
511 char buffer[kMaxPacketSize]; | 512 char* buffer = new char[kMaxPacketSize]; |
512 scoped_ptr<QuicEncryptedPacket> encrypted(framer.EncryptPayload( | 513 size_t encrypted_length = framer.EncryptPayload( |
513 ENCRYPTION_NONE, packet_number, *packet, buffer, kMaxPacketSize)); | 514 ENCRYPTION_NONE, packet_number, *packet, buffer, kMaxPacketSize); |
514 EXPECT_TRUE(encrypted != nullptr); | 515 EXPECT_NE(0u, encrypted_length); |
515 return encrypted->Clone(); | 516 return new QuicEncryptedPacket(buffer, encrypted_length, true); |
516 } | 517 } |
517 | 518 |
518 QuicEncryptedPacket* ConstructMisFramedEncryptedPacket( | 519 QuicEncryptedPacket* ConstructMisFramedEncryptedPacket( |
519 QuicConnectionId connection_id, | 520 QuicConnectionId connection_id, |
520 bool version_flag, | 521 bool version_flag, |
521 bool reset_flag, | 522 bool reset_flag, |
522 QuicPacketNumber packet_number, | 523 QuicPacketNumber packet_number, |
523 const string& data, | 524 const string& data, |
524 QuicConnectionIdLength connection_id_length, | 525 QuicConnectionIdLength connection_id_length, |
525 QuicPacketNumberLength packet_number_length, | 526 QuicPacketNumberLength packet_number_length, |
(...skipping 19 matching lines...) Expand all Loading... |
545 | 546 |
546 scoped_ptr<QuicPacket> packet( | 547 scoped_ptr<QuicPacket> packet( |
547 BuildUnsizedDataPacket(&framer, header, frames)); | 548 BuildUnsizedDataPacket(&framer, header, frames)); |
548 EXPECT_TRUE(packet != nullptr); | 549 EXPECT_TRUE(packet != nullptr); |
549 | 550 |
550 // Now set the packet's private flags byte to 0xFF, which is an invalid value. | 551 // Now set the packet's private flags byte to 0xFF, which is an invalid value. |
551 reinterpret_cast<unsigned char*>( | 552 reinterpret_cast<unsigned char*>( |
552 packet->mutable_data())[GetStartOfEncryptedData( | 553 packet->mutable_data())[GetStartOfEncryptedData( |
553 connection_id_length, version_flag, packet_number_length)] = 0xFF; | 554 connection_id_length, version_flag, packet_number_length)] = 0xFF; |
554 | 555 |
555 char buffer[kMaxPacketSize]; | 556 char* buffer = new char[kMaxPacketSize]; |
556 scoped_ptr<QuicEncryptedPacket> encrypted(framer.EncryptPayload( | 557 size_t encrypted_length = framer.EncryptPayload( |
557 ENCRYPTION_NONE, packet_number, *packet, buffer, kMaxPacketSize)); | 558 ENCRYPTION_NONE, packet_number, *packet, buffer, kMaxPacketSize); |
558 EXPECT_TRUE(encrypted != nullptr); | 559 EXPECT_NE(0u, encrypted_length); |
559 return encrypted->Clone(); | 560 return new QuicEncryptedPacket(buffer, encrypted_length, true); |
560 } | 561 } |
561 | 562 |
562 void CompareCharArraysWithHexError( | 563 void CompareCharArraysWithHexError( |
563 const string& description, | 564 const string& description, |
564 const char* actual, | 565 const char* actual, |
565 const int actual_len, | 566 const int actual_len, |
566 const char* expected, | 567 const char* expected, |
567 const int expected_len) { | 568 const int expected_len) { |
568 EXPECT_EQ(actual_len, expected_len); | 569 EXPECT_EQ(actual_len, expected_len); |
569 const int min_len = min(actual_len, expected_len); | 570 const int min_len = min(actual_len, expected_len); |
(...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
748 buffer, buf_len, self_address, peer_address); | 749 buffer, buf_len, self_address, peer_address); |
749 } | 750 } |
750 | 751 |
751 MockQuicConnectionDebugVisitor::MockQuicConnectionDebugVisitor() {} | 752 MockQuicConnectionDebugVisitor::MockQuicConnectionDebugVisitor() {} |
752 | 753 |
753 MockQuicConnectionDebugVisitor::~MockQuicConnectionDebugVisitor() {} | 754 MockQuicConnectionDebugVisitor::~MockQuicConnectionDebugVisitor() {} |
754 | 755 |
755 void CreateClientSessionForTest(QuicServerId server_id, | 756 void CreateClientSessionForTest(QuicServerId server_id, |
756 bool supports_stateless_rejects, | 757 bool supports_stateless_rejects, |
757 QuicTime::Delta connection_start_time, | 758 QuicTime::Delta connection_start_time, |
758 MockHelper* helper, | 759 MockConnectionHelper* helper, |
759 QuicCryptoClientConfig* crypto_client_config, | 760 QuicCryptoClientConfig* crypto_client_config, |
760 PacketSavingConnection** client_connection, | 761 PacketSavingConnection** client_connection, |
761 TestQuicSpdyClientSession** client_session) { | 762 TestQuicSpdyClientSession** client_session) { |
762 CHECK(crypto_client_config); | 763 CHECK(crypto_client_config); |
763 CHECK(client_connection); | 764 CHECK(client_connection); |
764 CHECK(client_session); | 765 CHECK(client_session); |
765 CHECK(!connection_start_time.IsZero()) | 766 CHECK(!connection_start_time.IsZero()) |
766 << "Connections must start at non-zero times, otherwise the " | 767 << "Connections must start at non-zero times, otherwise the " |
767 << "strike-register will be unhappy."; | 768 << "strike-register will be unhappy."; |
768 | 769 |
769 QuicConfig config = supports_stateless_rejects | 770 QuicConfig config = supports_stateless_rejects |
770 ? DefaultQuicConfigStatelessRejects() | 771 ? DefaultQuicConfigStatelessRejects() |
771 : DefaultQuicConfig(); | 772 : DefaultQuicConfig(); |
772 *client_connection = | 773 *client_connection = |
773 new PacketSavingConnection(helper, Perspective::IS_CLIENT); | 774 new PacketSavingConnection(helper, Perspective::IS_CLIENT); |
774 *client_session = new TestQuicSpdyClientSession( | 775 *client_session = new TestQuicSpdyClientSession( |
775 *client_connection, config, server_id, crypto_client_config); | 776 *client_connection, config, server_id, crypto_client_config); |
776 (*client_connection)->AdvanceTime(connection_start_time); | 777 (*client_connection)->AdvanceTime(connection_start_time); |
777 } | 778 } |
778 | 779 |
779 void CreateServerSessionForTest(QuicServerId server_id, | 780 void CreateServerSessionForTest(QuicServerId server_id, |
780 QuicTime::Delta connection_start_time, | 781 QuicTime::Delta connection_start_time, |
781 MockHelper* helper, | 782 MockConnectionHelper* helper, |
782 QuicCryptoServerConfig* server_crypto_config, | 783 QuicCryptoServerConfig* server_crypto_config, |
783 PacketSavingConnection** server_connection, | 784 PacketSavingConnection** server_connection, |
784 TestQuicSpdyServerSession** server_session) { | 785 TestQuicSpdyServerSession** server_session) { |
785 CHECK(server_crypto_config); | 786 CHECK(server_crypto_config); |
786 CHECK(server_connection); | 787 CHECK(server_connection); |
787 CHECK(server_session); | 788 CHECK(server_session); |
788 CHECK(!connection_start_time.IsZero()) | 789 CHECK(!connection_start_time.IsZero()) |
789 << "Connections must start at non-zero times, otherwise the " | 790 << "Connections must start at non-zero times, otherwise the " |
790 << "strike-register will be unhappy."; | 791 << "strike-register will be unhappy."; |
791 | 792 |
792 *server_connection = | 793 *server_connection = |
793 new PacketSavingConnection(helper, Perspective::IS_SERVER); | 794 new PacketSavingConnection(helper, Perspective::IS_SERVER); |
794 *server_session = new TestQuicSpdyServerSession( | 795 *server_session = new TestQuicSpdyServerSession( |
795 *server_connection, DefaultQuicConfig(), server_crypto_config); | 796 *server_connection, DefaultQuicConfig(), server_crypto_config); |
796 | 797 |
797 // We advance the clock initially because the default time is zero and the | 798 // We advance the clock initially because the default time is zero and the |
798 // strike register worries that we've just overflowed a uint32 time. | 799 // strike register worries that we've just overflowed a uint32 time. |
799 (*server_connection)->AdvanceTime(connection_start_time); | 800 (*server_connection)->AdvanceTime(connection_start_time); |
800 } | 801 } |
801 | 802 |
802 QuicStreamId QuicClientDataStreamId(int i) { | 803 QuicStreamId QuicClientDataStreamId(int i) { |
803 return kClientDataStreamId1 + 2 * i; | 804 return kClientDataStreamId1 + 2 * i; |
804 } | 805 } |
805 | 806 |
806 } // namespace test | 807 } // namespace test |
807 } // namespace net | 808 } // namespace net |
OLD | NEW |