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
3 // found in the LICENSE file.
5 #include "net/quic/test_tools/quic_test_utils.h"
8 #include "base/stl_util.h"
9 #include "base/strings/string_number_conversions.h"
10 #include "net/quic/crypto/crypto_framer.h"
11 #include "net/quic/crypto/crypto_handshake.h"
12 #include "net/quic/crypto/crypto_utils.h"
13 #include "net/quic/crypto/null_encrypter.h"
14 #include "net/quic/crypto/quic_decrypter.h"
15 #include "net/quic/crypto/quic_encrypter.h"
16 #include "net/quic/quic_data_writer.h"
17 #include "net/quic/quic_framer.h"
18 #include "net/quic/quic_packet_creator.h"
19 #include "net/quic/quic_utils.h"
20 #include "net/quic/test_tools/crypto_test_utils.h"
21 #include "net/quic/test_tools/quic_connection_peer.h"
22 #include "net/spdy/spdy_frame_builder.h"
23 #include "net/tools/quic/quic_per_connection_packet_writer.h"
25 using base::StringPiece
;
29 using testing::Invoke
;
36 // No-op alarm implementation used by MockHelper.
37 class TestAlarm
: public QuicAlarm
{
39 explicit TestAlarm(QuicAlarm::Delegate
* delegate
)
40 : QuicAlarm(delegate
) {
43 void SetImpl() override
{}
44 void CancelImpl() override
{}
49 QuicAckFrame
MakeAckFrame(QuicPacketSequenceNumber largest_observed
) {
51 ack
.largest_observed
= largest_observed
;
56 QuicAckFrame
MakeAckFrameWithNackRanges(
57 size_t num_nack_ranges
, QuicPacketSequenceNumber least_unacked
) {
58 QuicAckFrame ack
= MakeAckFrame(2 * num_nack_ranges
+ least_unacked
);
59 // Add enough missing packets to get num_nack_ranges nack ranges.
60 for (QuicPacketSequenceNumber i
= 1; i
< 2 * num_nack_ranges
; i
+= 2) {
61 ack
.missing_packets
.insert(least_unacked
+ i
);
66 QuicPacket
* BuildUnsizedDataPacket(QuicFramer
* framer
,
67 const QuicPacketHeader
& header
,
68 const QuicFrames
& frames
) {
69 const size_t max_plaintext_size
= framer
->GetMaxPlaintextSize(kMaxPacketSize
);
70 size_t packet_size
= GetPacketHeaderSize(header
);
71 for (size_t i
= 0; i
< frames
.size(); ++i
) {
72 DCHECK_LE(packet_size
, max_plaintext_size
);
73 bool first_frame
= i
== 0;
74 bool last_frame
= i
== frames
.size() - 1;
75 const size_t frame_size
= framer
->GetSerializedFrameLength(
76 frames
[i
], max_plaintext_size
- packet_size
, first_frame
, last_frame
,
77 header
.is_in_fec_group
,
78 header
.public_header
.sequence_number_length
);
80 packet_size
+= frame_size
;
82 return BuildUnsizedDataPacket(framer
, header
, frames
, packet_size
);
85 QuicPacket
* BuildUnsizedDataPacket(QuicFramer
* framer
,
86 const QuicPacketHeader
& header
,
87 const QuicFrames
& frames
,
89 char* buffer
= new char[packet_size
];
90 scoped_ptr
<QuicPacket
> packet(
91 framer
->BuildDataPacket(header
, frames
, buffer
, packet_size
));
92 DCHECK(packet
.get() != nullptr);
93 // Now I have to re-construct the data packet with data ownership.
94 return new QuicPacket(buffer
, packet
->length(), true,
95 header
.public_header
.connection_id_length
,
96 header
.public_header
.version_flag
,
97 header
.public_header
.sequence_number_length
);
100 uint64
SimpleRandom::RandUint64() {
101 unsigned char hash
[base::kSHA1Length
];
102 base::SHA1HashBytes(reinterpret_cast<unsigned char*>(&seed_
), sizeof(seed_
),
104 memcpy(&seed_
, hash
, sizeof(seed_
));
108 MockFramerVisitor::MockFramerVisitor() {
109 // By default, we want to accept packets.
110 ON_CALL(*this, OnProtocolVersionMismatch(_
))
111 .WillByDefault(testing::Return(false));
113 // By default, we want to accept packets.
114 ON_CALL(*this, OnUnauthenticatedHeader(_
))
115 .WillByDefault(testing::Return(true));
117 ON_CALL(*this, OnUnauthenticatedPublicHeader(_
))
118 .WillByDefault(testing::Return(true));
120 ON_CALL(*this, OnPacketHeader(_
))
121 .WillByDefault(testing::Return(true));
123 ON_CALL(*this, OnStreamFrame(_
))
124 .WillByDefault(testing::Return(true));
126 ON_CALL(*this, OnAckFrame(_
))
127 .WillByDefault(testing::Return(true));
129 ON_CALL(*this, OnStopWaitingFrame(_
))
130 .WillByDefault(testing::Return(true));
132 ON_CALL(*this, OnPingFrame(_
))
133 .WillByDefault(testing::Return(true));
135 ON_CALL(*this, OnRstStreamFrame(_
))
136 .WillByDefault(testing::Return(true));
138 ON_CALL(*this, OnConnectionCloseFrame(_
))
139 .WillByDefault(testing::Return(true));
141 ON_CALL(*this, OnGoAwayFrame(_
))
142 .WillByDefault(testing::Return(true));
145 MockFramerVisitor::~MockFramerVisitor() {
148 bool NoOpFramerVisitor::OnProtocolVersionMismatch(QuicVersion version
) {
152 bool NoOpFramerVisitor::OnUnauthenticatedPublicHeader(
153 const QuicPacketPublicHeader
& header
) {
157 bool NoOpFramerVisitor::OnUnauthenticatedHeader(
158 const QuicPacketHeader
& header
) {
162 bool NoOpFramerVisitor::OnPacketHeader(const QuicPacketHeader
& header
) {
166 bool NoOpFramerVisitor::OnStreamFrame(const QuicStreamFrame
& frame
) {
170 bool NoOpFramerVisitor::OnAckFrame(const QuicAckFrame
& frame
) {
174 bool NoOpFramerVisitor::OnStopWaitingFrame(
175 const QuicStopWaitingFrame
& frame
) {
179 bool NoOpFramerVisitor::OnPingFrame(const QuicPingFrame
& frame
) {
183 bool NoOpFramerVisitor::OnRstStreamFrame(
184 const QuicRstStreamFrame
& frame
) {
188 bool NoOpFramerVisitor::OnConnectionCloseFrame(
189 const QuicConnectionCloseFrame
& frame
) {
193 bool NoOpFramerVisitor::OnGoAwayFrame(const QuicGoAwayFrame
& frame
) {
197 bool NoOpFramerVisitor::OnWindowUpdateFrame(
198 const QuicWindowUpdateFrame
& frame
) {
202 bool NoOpFramerVisitor::OnBlockedFrame(const QuicBlockedFrame
& frame
) {
206 MockConnectionVisitor::MockConnectionVisitor() {
209 MockConnectionVisitor::~MockConnectionVisitor() {
212 MockHelper::MockHelper() {
215 MockHelper::~MockHelper() {
218 const QuicClock
* MockHelper::GetClock() const {
222 QuicRandom
* MockHelper::GetRandomGenerator() {
223 return &random_generator_
;
226 QuicAlarm
* MockHelper::CreateAlarm(QuicAlarm::Delegate
* delegate
) {
227 return new TestAlarm(delegate
);
230 void MockHelper::AdvanceTime(QuicTime::Delta delta
) {
231 clock_
.AdvanceTime(delta
);
234 QuicPacketWriter
* NiceMockPacketWriterFactory::Create(
235 QuicConnection
* /*connection*/) const {
236 return new testing::NiceMock
<MockPacketWriter
>();
239 MockConnection::MockConnection(Perspective perspective
)
240 : MockConnection(perspective
,
241 /* is_secure= */ false) {
244 MockConnection::MockConnection(Perspective perspective
, bool is_secure
)
245 : MockConnection(kTestConnectionId
,
246 IPEndPoint(TestPeerIPAddress(), kTestPort
),
249 QuicSupportedVersions()) {
252 MockConnection::MockConnection(IPEndPoint address
, Perspective perspective
)
253 : MockConnection(kTestConnectionId
,
256 /* is_secure= */ false,
257 QuicSupportedVersions()) {
260 MockConnection::MockConnection(QuicConnectionId connection_id
,
261 Perspective perspective
)
262 : MockConnection(connection_id
,
264 /* is_secure= */ false) {
267 MockConnection::MockConnection(QuicConnectionId connection_id
,
268 Perspective perspective
,
270 : MockConnection(connection_id
,
271 IPEndPoint(TestPeerIPAddress(), kTestPort
),
274 QuicSupportedVersions()) {
277 MockConnection::MockConnection(Perspective perspective
,
278 const QuicVersionVector
& supported_versions
)
279 : MockConnection(kTestConnectionId
,
280 IPEndPoint(TestPeerIPAddress(), kTestPort
),
282 /* is_secure= */ false,
283 supported_versions
) {
286 MockConnection::MockConnection(QuicConnectionId connection_id
,
288 Perspective perspective
,
290 const QuicVersionVector
& supported_versions
)
291 : QuicConnection(connection_id
,
293 new testing::NiceMock
<MockHelper
>(),
294 NiceMockPacketWriterFactory(),
295 /* owns_writer= */ true,
300 ON_CALL(*this, OnError(_
))
302 Invoke(this, &PacketSavingConnection::QuicConnection_OnError
));
305 MockConnection::~MockConnection() {
308 void MockConnection::AdvanceTime(QuicTime::Delta delta
) {
309 static_cast<MockHelper
*>(helper())->AdvanceTime(delta
);
312 PacketSavingConnection::PacketSavingConnection(Perspective perspective
)
313 : MockConnection(perspective
) {
316 PacketSavingConnection::PacketSavingConnection(
317 Perspective perspective
,
318 const QuicVersionVector
& supported_versions
)
319 : MockConnection(perspective
, supported_versions
) {
322 PacketSavingConnection::~PacketSavingConnection() {
323 STLDeleteElements(&encrypted_packets_
);
326 void PacketSavingConnection::SendOrQueuePacket(QueuedPacket packet
) {
327 if (!packet
.serialized_packet
.packet
->owns_buffer()) {
328 scoped_ptr
<QuicEncryptedPacket
> encrypted_deleter(
329 packet
.serialized_packet
.packet
);
330 packet
.serialized_packet
.packet
= packet
.serialized_packet
.packet
->Clone();
332 encrypted_packets_
.push_back(packet
.serialized_packet
.packet
);
333 // Transfer ownership of the packet to the SentPacketManager and the
334 // ack notifier to the AckNotifierManager.
335 sent_packet_manager_
.OnPacketSent(
336 &packet
.serialized_packet
, 0, QuicTime::Zero(), 1000,
337 NOT_RETRANSMISSION
, HAS_RETRANSMITTABLE_DATA
);
340 MockQuicSpdySession::MockQuicSpdySession(QuicConnection
* connection
)
341 : QuicSpdySession(connection
, DefaultQuicConfig()) {
342 crypto_stream_
.reset(new QuicCryptoStream(this));
344 ON_CALL(*this, WritevData(_
, _
, _
, _
, _
, _
))
345 .WillByDefault(testing::Return(QuicConsumedData(0, false)));
348 MockQuicSpdySession::~MockQuicSpdySession() {
351 TestQuicSpdyServerSession::TestQuicSpdyServerSession(
352 QuicConnection
* connection
,
353 const QuicConfig
& config
,
354 const QuicCryptoServerConfig
* crypto_config
)
355 : QuicSpdySession(connection
, config
) {
356 crypto_stream_
.reset(new QuicCryptoServerStream(crypto_config
, this));
360 TestQuicSpdyServerSession::~TestQuicSpdyServerSession() {
363 QuicCryptoServerStream
* TestQuicSpdyServerSession::GetCryptoStream() {
364 return crypto_stream_
.get();
367 TestQuicSpdyClientSession::TestQuicSpdyClientSession(
368 QuicConnection
* connection
,
369 const QuicConfig
& config
,
370 const QuicServerId
& server_id
,
371 QuicCryptoClientConfig
* crypto_config
)
372 : QuicClientSessionBase(connection
, config
) {
373 crypto_stream_
.reset(new QuicCryptoClientStream(
374 server_id
, this, CryptoTestUtils::ProofVerifyContextForTesting(),
379 TestQuicSpdyClientSession::~TestQuicSpdyClientSession() {
382 QuicCryptoClientStream
* TestQuicSpdyClientSession::GetCryptoStream() {
383 return crypto_stream_
.get();
386 MockPacketWriter::MockPacketWriter() {
389 MockPacketWriter::~MockPacketWriter() {
392 MockSendAlgorithm::MockSendAlgorithm() {
395 MockSendAlgorithm::~MockSendAlgorithm() {
398 MockLossAlgorithm::MockLossAlgorithm() {
401 MockLossAlgorithm::~MockLossAlgorithm() {
404 MockAckNotifierDelegate::MockAckNotifierDelegate() {
407 MockAckNotifierDelegate::~MockAckNotifierDelegate() {
410 MockNetworkChangeVisitor::MockNetworkChangeVisitor() {
413 MockNetworkChangeVisitor::~MockNetworkChangeVisitor() {
418 string
HexDumpWithMarks(const char* data
, int length
,
419 const bool* marks
, int mark_length
) {
420 static const char kHexChars
[] = "0123456789abcdef";
421 static const int kColumns
= 4;
423 const int kSizeLimit
= 1024;
424 if (length
> kSizeLimit
|| mark_length
> kSizeLimit
) {
425 LOG(ERROR
) << "Only dumping first " << kSizeLimit
<< " bytes.";
426 length
= min(length
, kSizeLimit
);
427 mark_length
= min(mark_length
, kSizeLimit
);
431 for (const char* row
= data
; length
> 0;
432 row
+= kColumns
, length
-= kColumns
) {
433 for (const char *p
= row
; p
< row
+ 4; ++p
) {
434 if (p
< row
+ length
) {
436 (marks
&& (p
- data
) < mark_length
&& marks
[p
- data
]);
437 hex
+= mark
? '*' : ' ';
438 hex
+= kHexChars
[(*p
& 0xf0) >> 4];
439 hex
+= kHexChars
[*p
& 0x0f];
440 hex
+= mark
? '*' : ' ';
447 for (const char* p
= row
; p
< row
+ 4 && p
< row
+ length
; ++p
) {
448 hex
+= (*p
>= 0x20 && *p
<= 0x7f) ? (*p
) : '.';
458 IPAddressNumber
TestPeerIPAddress() { return Loopback4(); }
460 QuicVersion
QuicVersionMax() { return QuicSupportedVersions().front(); }
462 QuicVersion
QuicVersionMin() { return QuicSupportedVersions().back(); }
464 IPAddressNumber
Loopback4() {
465 IPAddressNumber addr
;
466 CHECK(ParseIPLiteralToNumber("127.0.0.1", &addr
));
470 IPAddressNumber
Loopback6() {
471 IPAddressNumber addr
;
472 CHECK(ParseIPLiteralToNumber("::1", &addr
));
476 IPAddressNumber
Any4() {
477 IPAddressNumber any4
;
478 CHECK(net::ParseIPLiteralToNumber("0.0.0.0", &any4
));
482 void GenerateBody(string
* body
, int length
) {
484 body
->reserve(length
);
485 for (int i
= 0; i
< length
; ++i
) {
486 body
->append(1, static_cast<char>(32 + i
% (126 - 32)));
490 QuicEncryptedPacket
* ConstructEncryptedPacket(
491 QuicConnectionId connection_id
,
494 QuicPacketSequenceNumber sequence_number
,
495 const string
& data
) {
496 return ConstructEncryptedPacket(
497 connection_id
, version_flag
, reset_flag
, sequence_number
, data
,
498 PACKET_8BYTE_CONNECTION_ID
, PACKET_6BYTE_SEQUENCE_NUMBER
);
501 QuicEncryptedPacket
* ConstructEncryptedPacket(
502 QuicConnectionId connection_id
,
505 QuicPacketSequenceNumber sequence_number
,
507 QuicConnectionIdLength connection_id_length
,
508 QuicSequenceNumberLength sequence_number_length
) {
509 return ConstructEncryptedPacket(connection_id
, version_flag
, reset_flag
,
510 sequence_number
, data
, connection_id_length
,
511 sequence_number_length
, nullptr);
514 QuicEncryptedPacket
* ConstructEncryptedPacket(
515 QuicConnectionId connection_id
,
518 QuicPacketSequenceNumber sequence_number
,
520 QuicConnectionIdLength connection_id_length
,
521 QuicSequenceNumberLength sequence_number_length
,
522 QuicVersionVector
* versions
) {
523 QuicPacketHeader header
;
524 header
.public_header
.connection_id
= connection_id
;
525 header
.public_header
.connection_id_length
= connection_id_length
;
526 header
.public_header
.version_flag
= version_flag
;
527 header
.public_header
.reset_flag
= reset_flag
;
528 header
.public_header
.sequence_number_length
= sequence_number_length
;
529 header
.packet_sequence_number
= sequence_number
;
530 header
.entropy_flag
= false;
531 header
.entropy_hash
= 0;
532 header
.fec_flag
= false;
533 header
.is_in_fec_group
= NOT_IN_FEC_GROUP
;
534 header
.fec_group
= 0;
535 QuicStreamFrame
stream_frame(1, false, 0, StringPiece(data
));
536 QuicFrame
frame(&stream_frame
);
538 frames
.push_back(frame
);
539 QuicFramer
framer(versions
!= nullptr ? *versions
: QuicSupportedVersions(),
540 QuicTime::Zero(), Perspective::IS_CLIENT
);
542 scoped_ptr
<QuicPacket
> packet(
543 BuildUnsizedDataPacket(&framer
, header
, frames
));
544 EXPECT_TRUE(packet
!= nullptr);
545 char buffer
[kMaxPacketSize
];
546 scoped_ptr
<QuicEncryptedPacket
> encrypted(framer
.EncryptPayload(
547 ENCRYPTION_NONE
, sequence_number
, *packet
, buffer
, kMaxPacketSize
));
548 EXPECT_TRUE(encrypted
!= nullptr);
549 return encrypted
->Clone();
552 QuicEncryptedPacket
* ConstructMisFramedEncryptedPacket(
553 QuicConnectionId connection_id
,
556 QuicPacketSequenceNumber sequence_number
,
558 QuicConnectionIdLength connection_id_length
,
559 QuicSequenceNumberLength sequence_number_length
,
560 QuicVersionVector
* versions
) {
561 QuicPacketHeader header
;
562 header
.public_header
.connection_id
= connection_id
;
563 header
.public_header
.connection_id_length
= connection_id_length
;
564 header
.public_header
.version_flag
= version_flag
;
565 header
.public_header
.reset_flag
= reset_flag
;
566 header
.public_header
.sequence_number_length
= sequence_number_length
;
567 header
.packet_sequence_number
= sequence_number
;
568 header
.entropy_flag
= false;
569 header
.entropy_hash
= 0;
570 header
.fec_flag
= false;
571 header
.is_in_fec_group
= NOT_IN_FEC_GROUP
;
572 header
.fec_group
= 0;
573 QuicStreamFrame
stream_frame(1, false, 0, StringPiece(data
));
574 QuicFrame
frame(&stream_frame
);
576 frames
.push_back(frame
);
577 QuicFramer
framer(versions
!= nullptr ? *versions
: QuicSupportedVersions(),
578 QuicTime::Zero(), Perspective::IS_CLIENT
);
580 scoped_ptr
<QuicPacket
> packet(
581 BuildUnsizedDataPacket(&framer
, header
, frames
));
582 EXPECT_TRUE(packet
!= nullptr);
584 // Now set the packet's private flags byte to 0xFF, which is an invalid value.
585 reinterpret_cast<unsigned char*>(
586 packet
->mutable_data())[GetStartOfEncryptedData(
587 connection_id_length
, version_flag
, sequence_number_length
)] = 0xFF;
589 char buffer
[kMaxPacketSize
];
590 scoped_ptr
<QuicEncryptedPacket
> encrypted(framer
.EncryptPayload(
591 ENCRYPTION_NONE
, sequence_number
, *packet
, buffer
, kMaxPacketSize
));
592 EXPECT_TRUE(encrypted
!= nullptr);
593 return encrypted
->Clone();
596 void CompareCharArraysWithHexError(
597 const string
& description
,
599 const int actual_len
,
600 const char* expected
,
601 const int expected_len
) {
602 EXPECT_EQ(actual_len
, expected_len
);
603 const int min_len
= min(actual_len
, expected_len
);
604 const int max_len
= max(actual_len
, expected_len
);
605 scoped_ptr
<bool[]> marks(new bool[max_len
]);
606 bool identical
= (actual_len
== expected_len
);
607 for (int i
= 0; i
< min_len
; ++i
) {
608 if (actual
[i
] != expected
[i
]) {
615 for (int i
= min_len
; i
< max_len
; ++i
) {
618 if (identical
) return;
623 << HexDumpWithMarks(expected
, expected_len
, marks
.get(), max_len
)
625 << HexDumpWithMarks(actual
, actual_len
, marks
.get(), max_len
);
628 bool DecodeHexString(const base::StringPiece
& hex
, std::string
* bytes
) {
632 std::vector
<uint8
> v
;
633 if (!base::HexStringToBytes(hex
.as_string(), &v
))
636 bytes
->assign(reinterpret_cast<const char*>(&v
[0]), v
.size());
640 static QuicPacket
* ConstructPacketFromHandshakeMessage(
641 QuicConnectionId connection_id
,
642 const CryptoHandshakeMessage
& message
,
643 bool should_include_version
) {
644 CryptoFramer crypto_framer
;
645 scoped_ptr
<QuicData
> data(crypto_framer
.ConstructHandshakeMessage(message
));
646 QuicFramer
quic_framer(QuicSupportedVersions(), QuicTime::Zero(),
647 Perspective::IS_CLIENT
);
649 QuicPacketHeader header
;
650 header
.public_header
.connection_id
= connection_id
;
651 header
.public_header
.reset_flag
= false;
652 header
.public_header
.version_flag
= should_include_version
;
653 header
.packet_sequence_number
= 1;
654 header
.entropy_flag
= false;
655 header
.entropy_hash
= 0;
656 header
.fec_flag
= false;
657 header
.fec_group
= 0;
659 QuicStreamFrame
stream_frame(kCryptoStreamId
, false, 0,
660 data
->AsStringPiece());
662 QuicFrame
frame(&stream_frame
);
664 frames
.push_back(frame
);
665 return BuildUnsizedDataPacket(&quic_framer
, header
, frames
);
668 QuicPacket
* ConstructHandshakePacket(QuicConnectionId connection_id
,
670 CryptoHandshakeMessage message
;
671 message
.set_tag(tag
);
672 return ConstructPacketFromHandshakeMessage(connection_id
, message
, false);
675 size_t GetPacketLengthForOneStream(
677 bool include_version
,
678 QuicConnectionIdLength connection_id_length
,
679 QuicSequenceNumberLength sequence_number_length
,
680 InFecGroup is_in_fec_group
,
681 size_t* payload_length
) {
683 const size_t stream_length
=
684 NullEncrypter().GetCiphertextSize(*payload_length
) +
685 QuicPacketCreator::StreamFramePacketOverhead(
686 PACKET_8BYTE_CONNECTION_ID
, include_version
,
687 sequence_number_length
, 0u, is_in_fec_group
);
688 const size_t ack_length
=
689 NullEncrypter().GetCiphertextSize(
690 QuicFramer::GetMinAckFrameSize(PACKET_1BYTE_SEQUENCE_NUMBER
)) +
691 GetPacketHeaderSize(connection_id_length
, include_version
,
692 sequence_number_length
, is_in_fec_group
);
693 if (stream_length
< ack_length
) {
694 *payload_length
= 1 + ack_length
- stream_length
;
697 return NullEncrypter().GetCiphertextSize(*payload_length
) +
698 QuicPacketCreator::StreamFramePacketOverhead(
699 connection_id_length
, include_version
,
700 sequence_number_length
, 0u, is_in_fec_group
);
703 TestEntropyCalculator::TestEntropyCalculator() {}
705 TestEntropyCalculator::~TestEntropyCalculator() {}
707 QuicPacketEntropyHash
TestEntropyCalculator::EntropyHash(
708 QuicPacketSequenceNumber sequence_number
) const {
712 MockEntropyCalculator::MockEntropyCalculator() {}
714 MockEntropyCalculator::~MockEntropyCalculator() {}
716 QuicConfig
DefaultQuicConfig() {
718 config
.SetInitialStreamFlowControlWindowToSend(
719 kInitialStreamFlowControlWindowForTest
);
720 config
.SetInitialSessionFlowControlWindowToSend(
721 kInitialSessionFlowControlWindowForTest
);
725 QuicConfig
DefaultQuicConfigStatelessRejects() {
726 QuicConfig config
= DefaultQuicConfig();
728 copt
.push_back(kSREJ
);
729 config
.SetConnectionOptionsToSend(copt
);
733 QuicVersionVector
SupportedVersions(QuicVersion version
) {
734 QuicVersionVector versions
;
735 versions
.push_back(version
);
739 TestWriterFactory::TestWriterFactory() : current_writer_(nullptr) {}
740 TestWriterFactory::~TestWriterFactory() {}
742 QuicPacketWriter
* TestWriterFactory::Create(QuicPacketWriter
* writer
,
743 QuicConnection
* connection
) {
744 return new PerConnectionPacketWriter(this, writer
, connection
);
747 void TestWriterFactory::OnPacketSent(WriteResult result
) {
748 if (current_writer_
!= nullptr && result
.status
== WRITE_STATUS_ERROR
) {
749 current_writer_
->connection()->OnWriteError(result
.error_code
);
750 current_writer_
= nullptr;
754 void TestWriterFactory::Unregister(PerConnectionPacketWriter
* writer
) {
755 if (current_writer_
== writer
) {
756 current_writer_
= nullptr;
760 TestWriterFactory::PerConnectionPacketWriter::PerConnectionPacketWriter(
761 TestWriterFactory
* factory
,
762 QuicPacketWriter
* writer
,
763 QuicConnection
* connection
)
764 : QuicPerConnectionPacketWriter(writer
, connection
),
768 TestWriterFactory::PerConnectionPacketWriter::~PerConnectionPacketWriter() {
769 factory_
->Unregister(this);
772 WriteResult
TestWriterFactory::PerConnectionPacketWriter::WritePacket(
775 const IPAddressNumber
& self_address
,
776 const IPEndPoint
& peer_address
) {
777 // A DCHECK(factory_current_writer_ == nullptr) would be wrong here -- this
778 // class may be used in a setting where connection()->OnPacketSent() is called
779 // in a different way, so TestWriterFactory::OnPacketSent might never be
781 factory_
->current_writer_
= this;
782 return tools::QuicPerConnectionPacketWriter::WritePacket(
783 buffer
, buf_len
, self_address
, peer_address
);
786 MockQuicConnectionDebugVisitor::MockQuicConnectionDebugVisitor() {
789 MockQuicConnectionDebugVisitor::~MockQuicConnectionDebugVisitor() {
792 void CreateClientSessionForTest(QuicServerId server_id
,
793 bool supports_stateless_rejects
,
794 QuicTime::Delta connection_start_time
,
795 QuicCryptoClientConfig
* crypto_client_config
,
796 PacketSavingConnection
** client_connection
,
797 TestQuicSpdyClientSession
** client_session
) {
798 CHECK(crypto_client_config
);
799 CHECK(client_connection
);
800 CHECK(client_session
);
801 CHECK(!connection_start_time
.IsZero())
802 << "Connections must start at non-zero times, otherwise the "
803 << "strike-register will be unhappy.";
805 QuicConfig config
= supports_stateless_rejects
806 ? DefaultQuicConfigStatelessRejects()
807 : DefaultQuicConfig();
808 *client_connection
= new PacketSavingConnection(Perspective::IS_CLIENT
);
809 *client_session
= new TestQuicSpdyClientSession(
810 *client_connection
, config
, server_id
, crypto_client_config
);
811 (*client_connection
)->AdvanceTime(connection_start_time
);
814 void CreateServerSessionForTest(QuicServerId server_id
,
815 QuicTime::Delta connection_start_time
,
816 QuicCryptoServerConfig
* server_crypto_config
,
817 PacketSavingConnection
** server_connection
,
818 TestQuicSpdyServerSession
** server_session
) {
819 CHECK(server_crypto_config
);
820 CHECK(server_connection
);
821 CHECK(server_session
);
822 CHECK(!connection_start_time
.IsZero())
823 << "Connections must start at non-zero times, otherwise the "
824 << "strike-register will be unhappy.";
826 *server_connection
= new PacketSavingConnection(Perspective::IS_SERVER
);
827 *server_session
= new TestQuicSpdyServerSession(
828 *server_connection
, DefaultQuicConfig(), server_crypto_config
);
830 // We advance the clock initially because the default time is zero and the
831 // strike register worries that we've just overflowed a uint32 time.
832 (*server_connection
)->AdvanceTime(connection_start_time
);