Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / net / quic / test_tools / quic_test_utils.cc
blob7412d30f207cef78c3c14016d43bd6da918b4201
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"
7 #include "base/sha1.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;
26 using std::max;
27 using std::min;
28 using std::string;
29 using testing::Invoke;
30 using testing::_;
32 namespace net {
33 namespace test {
34 namespace {
36 // No-op alarm implementation used by MockHelper.
37 class TestAlarm : public QuicAlarm {
38 public:
39 explicit TestAlarm(QuicAlarm::Delegate* delegate)
40 : QuicAlarm(delegate) {
43 void SetImpl() override {}
44 void CancelImpl() override {}
47 } // namespace
49 QuicAckFrame MakeAckFrame(QuicPacketNumber largest_observed) {
50 QuicAckFrame ack;
51 ack.largest_observed = largest_observed;
52 ack.entropy_hash = 0;
53 return ack;
56 QuicAckFrame MakeAckFrameWithNackRanges(size_t num_nack_ranges,
57 QuicPacketNumber 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 (QuicPacketNumber i = 1; i < 2 * num_nack_ranges; i += 2) {
61 ack.missing_packets.Add(least_unacked + i);
63 return ack;
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, header.public_header.packet_number_length);
78 DCHECK(frame_size);
79 packet_size += frame_size;
81 return BuildUnsizedDataPacket(framer, header, frames, packet_size);
84 QuicPacket* BuildUnsizedDataPacket(QuicFramer* framer,
85 const QuicPacketHeader& header,
86 const QuicFrames& frames,
87 size_t packet_size) {
88 char* buffer = new char[packet_size];
89 scoped_ptr<QuicPacket> packet(
90 framer->BuildDataPacket(header, frames, buffer, packet_size));
91 DCHECK(packet.get() != nullptr);
92 // Now I have to re-construct the data packet with data ownership.
93 return new QuicPacket(buffer, packet->length(), true,
94 header.public_header.connection_id_length,
95 header.public_header.version_flag,
96 header.public_header.packet_number_length);
99 uint64 SimpleRandom::RandUint64() {
100 unsigned char hash[base::kSHA1Length];
101 base::SHA1HashBytes(reinterpret_cast<unsigned char*>(&seed_), sizeof(seed_),
102 hash);
103 memcpy(&seed_, hash, sizeof(seed_));
104 return seed_;
107 MockFramerVisitor::MockFramerVisitor() {
108 // By default, we want to accept packets.
109 ON_CALL(*this, OnProtocolVersionMismatch(_))
110 .WillByDefault(testing::Return(false));
112 // By default, we want to accept packets.
113 ON_CALL(*this, OnUnauthenticatedHeader(_))
114 .WillByDefault(testing::Return(true));
116 ON_CALL(*this, OnUnauthenticatedPublicHeader(_))
117 .WillByDefault(testing::Return(true));
119 ON_CALL(*this, OnPacketHeader(_))
120 .WillByDefault(testing::Return(true));
122 ON_CALL(*this, OnStreamFrame(_))
123 .WillByDefault(testing::Return(true));
125 ON_CALL(*this, OnAckFrame(_))
126 .WillByDefault(testing::Return(true));
128 ON_CALL(*this, OnStopWaitingFrame(_))
129 .WillByDefault(testing::Return(true));
131 ON_CALL(*this, OnPingFrame(_))
132 .WillByDefault(testing::Return(true));
134 ON_CALL(*this, OnRstStreamFrame(_))
135 .WillByDefault(testing::Return(true));
137 ON_CALL(*this, OnConnectionCloseFrame(_))
138 .WillByDefault(testing::Return(true));
140 ON_CALL(*this, OnGoAwayFrame(_))
141 .WillByDefault(testing::Return(true));
144 MockFramerVisitor::~MockFramerVisitor() {
147 bool NoOpFramerVisitor::OnProtocolVersionMismatch(QuicVersion version) {
148 return false;
151 bool NoOpFramerVisitor::OnUnauthenticatedPublicHeader(
152 const QuicPacketPublicHeader& header) {
153 return true;
156 bool NoOpFramerVisitor::OnUnauthenticatedHeader(
157 const QuicPacketHeader& header) {
158 return true;
161 bool NoOpFramerVisitor::OnPacketHeader(const QuicPacketHeader& header) {
162 return true;
165 bool NoOpFramerVisitor::OnStreamFrame(const QuicStreamFrame& frame) {
166 return true;
169 bool NoOpFramerVisitor::OnAckFrame(const QuicAckFrame& frame) {
170 return true;
173 bool NoOpFramerVisitor::OnStopWaitingFrame(
174 const QuicStopWaitingFrame& frame) {
175 return true;
178 bool NoOpFramerVisitor::OnPingFrame(const QuicPingFrame& frame) {
179 return true;
182 bool NoOpFramerVisitor::OnRstStreamFrame(
183 const QuicRstStreamFrame& frame) {
184 return true;
187 bool NoOpFramerVisitor::OnConnectionCloseFrame(
188 const QuicConnectionCloseFrame& frame) {
189 return true;
192 bool NoOpFramerVisitor::OnGoAwayFrame(const QuicGoAwayFrame& frame) {
193 return true;
196 bool NoOpFramerVisitor::OnWindowUpdateFrame(
197 const QuicWindowUpdateFrame& frame) {
198 return true;
201 bool NoOpFramerVisitor::OnBlockedFrame(const QuicBlockedFrame& frame) {
202 return true;
205 MockConnectionVisitor::MockConnectionVisitor() {
208 MockConnectionVisitor::~MockConnectionVisitor() {
211 MockHelper::MockHelper() {
214 MockHelper::~MockHelper() {
217 const QuicClock* MockHelper::GetClock() const {
218 return &clock_;
221 QuicRandom* MockHelper::GetRandomGenerator() {
222 return &random_generator_;
225 QuicAlarm* MockHelper::CreateAlarm(QuicAlarm::Delegate* delegate) {
226 return new TestAlarm(delegate);
229 void MockHelper::AdvanceTime(QuicTime::Delta delta) {
230 clock_.AdvanceTime(delta);
233 QuicPacketWriter* NiceMockPacketWriterFactory::Create(
234 QuicConnection* /*connection*/) const {
235 return new testing::NiceMock<MockPacketWriter>();
238 MockConnection::MockConnection(Perspective perspective)
239 : MockConnection(perspective,
240 /* is_secure= */ false) {
243 MockConnection::MockConnection(Perspective perspective, bool is_secure)
244 : MockConnection(kTestConnectionId,
245 IPEndPoint(TestPeerIPAddress(), kTestPort),
246 perspective,
247 is_secure,
248 QuicSupportedVersions()) {
251 MockConnection::MockConnection(IPEndPoint address, Perspective perspective)
252 : MockConnection(kTestConnectionId,
253 address,
254 perspective,
255 /* is_secure= */ false,
256 QuicSupportedVersions()) {
259 MockConnection::MockConnection(QuicConnectionId connection_id,
260 Perspective perspective)
261 : MockConnection(connection_id,
262 perspective,
263 /* is_secure= */ false) {
266 MockConnection::MockConnection(QuicConnectionId connection_id,
267 Perspective perspective,
268 bool is_secure)
269 : MockConnection(connection_id,
270 IPEndPoint(TestPeerIPAddress(), kTestPort),
271 perspective,
272 is_secure,
273 QuicSupportedVersions()) {
276 MockConnection::MockConnection(Perspective perspective,
277 const QuicVersionVector& supported_versions)
278 : MockConnection(kTestConnectionId,
279 IPEndPoint(TestPeerIPAddress(), kTestPort),
280 perspective,
281 /* is_secure= */ false,
282 supported_versions) {
285 MockConnection::MockConnection(QuicConnectionId connection_id,
286 IPEndPoint address,
287 Perspective perspective,
288 bool is_secure,
289 const QuicVersionVector& supported_versions)
290 : QuicConnection(connection_id,
291 address,
292 new testing::NiceMock<MockHelper>(),
293 NiceMockPacketWriterFactory(),
294 /* owns_writer= */ true,
295 perspective,
296 is_secure,
297 supported_versions),
298 helper_(helper()) {
299 ON_CALL(*this, OnError(_))
300 .WillByDefault(
301 Invoke(this, &PacketSavingConnection::QuicConnection_OnError));
304 MockConnection::~MockConnection() {
307 void MockConnection::AdvanceTime(QuicTime::Delta delta) {
308 static_cast<MockHelper*>(helper())->AdvanceTime(delta);
311 PacketSavingConnection::PacketSavingConnection(Perspective perspective)
312 : MockConnection(perspective) {
315 PacketSavingConnection::PacketSavingConnection(
316 Perspective perspective,
317 const QuicVersionVector& supported_versions)
318 : MockConnection(perspective, supported_versions) {
321 PacketSavingConnection::~PacketSavingConnection() {
322 STLDeleteElements(&encrypted_packets_);
325 void PacketSavingConnection::SendOrQueuePacket(QueuedPacket packet) {
326 if (!packet.serialized_packet.packet->owns_buffer()) {
327 scoped_ptr<QuicEncryptedPacket> encrypted_deleter(
328 packet.serialized_packet.packet);
329 packet.serialized_packet.packet = packet.serialized_packet.packet->Clone();
331 encrypted_packets_.push_back(packet.serialized_packet.packet);
332 // Transfer ownership of the packet to the SentPacketManager and the
333 // ack notifier to the AckNotifierManager.
334 sent_packet_manager_.OnPacketSent(
335 &packet.serialized_packet, 0, QuicTime::Zero(), 1000,
336 NOT_RETRANSMISSION, HAS_RETRANSMITTABLE_DATA);
339 MockQuicSpdySession::MockQuicSpdySession(QuicConnection* connection)
340 : QuicSpdySession(connection, DefaultQuicConfig()) {
341 crypto_stream_.reset(new QuicCryptoStream(this));
342 Initialize();
343 ON_CALL(*this, WritevData(_, _, _, _, _, _))
344 .WillByDefault(testing::Return(QuicConsumedData(0, false)));
347 MockQuicSpdySession::~MockQuicSpdySession() {
350 TestQuicSpdyServerSession::TestQuicSpdyServerSession(
351 QuicConnection* connection,
352 const QuicConfig& config,
353 const QuicCryptoServerConfig* crypto_config)
354 : QuicSpdySession(connection, config) {
355 crypto_stream_.reset(new QuicCryptoServerStream(crypto_config, this));
356 Initialize();
359 TestQuicSpdyServerSession::~TestQuicSpdyServerSession() {
362 QuicCryptoServerStream* TestQuicSpdyServerSession::GetCryptoStream() {
363 return crypto_stream_.get();
366 TestQuicSpdyClientSession::TestQuicSpdyClientSession(
367 QuicConnection* connection,
368 const QuicConfig& config,
369 const QuicServerId& server_id,
370 QuicCryptoClientConfig* crypto_config)
371 : QuicClientSessionBase(connection, config) {
372 crypto_stream_.reset(new QuicCryptoClientStream(
373 server_id, this, CryptoTestUtils::ProofVerifyContextForTesting(),
374 crypto_config));
375 Initialize();
378 TestQuicSpdyClientSession::~TestQuicSpdyClientSession() {
381 QuicCryptoClientStream* TestQuicSpdyClientSession::GetCryptoStream() {
382 return crypto_stream_.get();
385 MockPacketWriter::MockPacketWriter() {
386 ON_CALL(*this, GetMaxPacketSize(_))
387 .WillByDefault(testing::Return(kMaxPacketSize));
390 MockPacketWriter::~MockPacketWriter() {
393 MockSendAlgorithm::MockSendAlgorithm() {
396 MockSendAlgorithm::~MockSendAlgorithm() {
399 MockLossAlgorithm::MockLossAlgorithm() {
402 MockLossAlgorithm::~MockLossAlgorithm() {
405 MockAckNotifierDelegate::MockAckNotifierDelegate() {
408 MockAckNotifierDelegate::~MockAckNotifierDelegate() {
411 MockNetworkChangeVisitor::MockNetworkChangeVisitor() {
414 MockNetworkChangeVisitor::~MockNetworkChangeVisitor() {
417 namespace {
419 string HexDumpWithMarks(const char* data, int length,
420 const bool* marks, int mark_length) {
421 static const char kHexChars[] = "0123456789abcdef";
422 static const int kColumns = 4;
424 const int kSizeLimit = 1024;
425 if (length > kSizeLimit || mark_length > kSizeLimit) {
426 LOG(ERROR) << "Only dumping first " << kSizeLimit << " bytes.";
427 length = min(length, kSizeLimit);
428 mark_length = min(mark_length, kSizeLimit);
431 string hex;
432 for (const char* row = data; length > 0;
433 row += kColumns, length -= kColumns) {
434 for (const char *p = row; p < row + 4; ++p) {
435 if (p < row + length) {
436 const bool mark =
437 (marks && (p - data) < mark_length && marks[p - data]);
438 hex += mark ? '*' : ' ';
439 hex += kHexChars[(*p & 0xf0) >> 4];
440 hex += kHexChars[*p & 0x0f];
441 hex += mark ? '*' : ' ';
442 } else {
443 hex += " ";
446 hex = hex + " ";
448 for (const char* p = row; p < row + 4 && p < row + length; ++p) {
449 hex += (*p >= 0x20 && *p <= 0x7f) ? (*p) : '.';
452 hex = hex + '\n';
454 return hex;
457 } // namespace
459 IPAddressNumber TestPeerIPAddress() { return Loopback4(); }
461 QuicVersion QuicVersionMax() { return QuicSupportedVersions().front(); }
463 QuicVersion QuicVersionMin() { return QuicSupportedVersions().back(); }
465 IPAddressNumber Loopback4() {
466 IPAddressNumber addr;
467 CHECK(ParseIPLiteralToNumber("127.0.0.1", &addr));
468 return addr;
471 IPAddressNumber Loopback6() {
472 IPAddressNumber addr;
473 CHECK(ParseIPLiteralToNumber("::1", &addr));
474 return addr;
477 IPAddressNumber Any4() {
478 IPAddressNumber any4;
479 CHECK(net::ParseIPLiteralToNumber("0.0.0.0", &any4));
480 return any4;
483 void GenerateBody(string* body, int length) {
484 body->clear();
485 body->reserve(length);
486 for (int i = 0; i < length; ++i) {
487 body->append(1, static_cast<char>(32 + i % (126 - 32)));
491 QuicEncryptedPacket* ConstructEncryptedPacket(QuicConnectionId connection_id,
492 bool version_flag,
493 bool reset_flag,
494 QuicPacketNumber packet_number,
495 const string& data) {
496 return ConstructEncryptedPacket(
497 connection_id, version_flag, reset_flag, packet_number, data,
498 PACKET_8BYTE_CONNECTION_ID, PACKET_6BYTE_PACKET_NUMBER);
501 QuicEncryptedPacket* ConstructEncryptedPacket(
502 QuicConnectionId connection_id,
503 bool version_flag,
504 bool reset_flag,
505 QuicPacketNumber packet_number,
506 const string& data,
507 QuicConnectionIdLength connection_id_length,
508 QuicPacketNumberLength packet_number_length) {
509 return ConstructEncryptedPacket(connection_id, version_flag, reset_flag,
510 packet_number, data, connection_id_length,
511 packet_number_length, nullptr);
514 QuicEncryptedPacket* ConstructEncryptedPacket(
515 QuicConnectionId connection_id,
516 bool version_flag,
517 bool reset_flag,
518 QuicPacketNumber packet_number,
519 const string& data,
520 QuicConnectionIdLength connection_id_length,
521 QuicPacketNumberLength packet_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.packet_number_length = packet_number_length;
529 header.packet_packet_number = packet_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);
537 QuicFrames frames;
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, packet_number, *packet, buffer, kMaxPacketSize));
548 EXPECT_TRUE(encrypted != nullptr);
549 return encrypted->Clone();
552 QuicEncryptedPacket* ConstructMisFramedEncryptedPacket(
553 QuicConnectionId connection_id,
554 bool version_flag,
555 bool reset_flag,
556 QuicPacketNumber packet_number,
557 const string& data,
558 QuicConnectionIdLength connection_id_length,
559 QuicPacketNumberLength packet_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.packet_number_length = packet_number_length;
567 header.packet_packet_number = packet_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);
575 QuicFrames frames;
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, packet_number_length)] = 0xFF;
589 char buffer[kMaxPacketSize];
590 scoped_ptr<QuicEncryptedPacket> encrypted(framer.EncryptPayload(
591 ENCRYPTION_NONE, packet_number, *packet, buffer, kMaxPacketSize));
592 EXPECT_TRUE(encrypted != nullptr);
593 return encrypted->Clone();
596 void CompareCharArraysWithHexError(
597 const string& description,
598 const char* actual,
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]) {
609 marks[i] = true;
610 identical = false;
611 } else {
612 marks[i] = false;
615 for (int i = min_len; i < max_len; ++i) {
616 marks[i] = true;
618 if (identical) return;
619 ADD_FAILURE()
620 << "Description:\n"
621 << description
622 << "\n\nExpected:\n"
623 << HexDumpWithMarks(expected, expected_len, marks.get(), max_len)
624 << "\nActual:\n"
625 << HexDumpWithMarks(actual, actual_len, marks.get(), max_len);
628 bool DecodeHexString(const base::StringPiece& hex, std::string* bytes) {
629 bytes->clear();
630 if (hex.empty())
631 return true;
632 std::vector<uint8> v;
633 if (!base::HexStringToBytes(hex.as_string(), &v))
634 return false;
635 if (!v.empty())
636 bytes->assign(reinterpret_cast<const char*>(&v[0]), v.size());
637 return true;
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_packet_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);
663 QuicFrames frames;
664 frames.push_back(frame);
665 return BuildUnsizedDataPacket(&quic_framer, header, frames);
668 QuicPacket* ConstructHandshakePacket(QuicConnectionId connection_id,
669 QuicTag tag) {
670 CryptoHandshakeMessage message;
671 message.set_tag(tag);
672 return ConstructPacketFromHandshakeMessage(connection_id, message, false);
675 size_t GetPacketLengthForOneStream(QuicVersion version,
676 bool include_version,
677 QuicConnectionIdLength connection_id_length,
678 QuicPacketNumberLength packet_number_length,
679 InFecGroup is_in_fec_group,
680 size_t* payload_length) {
681 *payload_length = 1;
682 const size_t stream_length =
683 NullEncrypter().GetCiphertextSize(*payload_length) +
684 QuicPacketCreator::StreamFramePacketOverhead(
685 PACKET_8BYTE_CONNECTION_ID, include_version, packet_number_length, 0u,
686 is_in_fec_group);
687 const size_t ack_length =
688 NullEncrypter().GetCiphertextSize(
689 QuicFramer::GetMinAckFrameSize(PACKET_1BYTE_PACKET_NUMBER)) +
690 GetPacketHeaderSize(connection_id_length, include_version,
691 packet_number_length, is_in_fec_group);
692 if (stream_length < ack_length) {
693 *payload_length = 1 + ack_length - stream_length;
696 return NullEncrypter().GetCiphertextSize(*payload_length) +
697 QuicPacketCreator::StreamFramePacketOverhead(
698 connection_id_length, include_version, packet_number_length, 0u,
699 is_in_fec_group);
702 TestEntropyCalculator::TestEntropyCalculator() {}
704 TestEntropyCalculator::~TestEntropyCalculator() {}
706 QuicPacketEntropyHash TestEntropyCalculator::EntropyHash(
707 QuicPacketNumber packet_number) const {
708 return 1u;
711 MockEntropyCalculator::MockEntropyCalculator() {}
713 MockEntropyCalculator::~MockEntropyCalculator() {}
715 QuicConfig DefaultQuicConfig() {
716 QuicConfig config;
717 config.SetInitialStreamFlowControlWindowToSend(
718 kInitialStreamFlowControlWindowForTest);
719 config.SetInitialSessionFlowControlWindowToSend(
720 kInitialSessionFlowControlWindowForTest);
721 return config;
724 QuicConfig DefaultQuicConfigStatelessRejects() {
725 QuicConfig config = DefaultQuicConfig();
726 QuicTagVector copt;
727 copt.push_back(kSREJ);
728 config.SetConnectionOptionsToSend(copt);
729 return config;
732 QuicVersionVector SupportedVersions(QuicVersion version) {
733 QuicVersionVector versions;
734 versions.push_back(version);
735 return versions;
738 TestWriterFactory::TestWriterFactory() : current_writer_(nullptr) {}
739 TestWriterFactory::~TestWriterFactory() {}
741 QuicPacketWriter* TestWriterFactory::Create(QuicPacketWriter* writer,
742 QuicConnection* connection) {
743 return new PerConnectionPacketWriter(this, writer, connection);
746 void TestWriterFactory::OnPacketSent(WriteResult result) {
747 if (current_writer_ != nullptr && result.status == WRITE_STATUS_ERROR) {
748 current_writer_->connection()->OnWriteError(result.error_code);
749 current_writer_ = nullptr;
753 void TestWriterFactory::Unregister(PerConnectionPacketWriter* writer) {
754 if (current_writer_ == writer) {
755 current_writer_ = nullptr;
759 TestWriterFactory::PerConnectionPacketWriter::PerConnectionPacketWriter(
760 TestWriterFactory* factory,
761 QuicPacketWriter* writer,
762 QuicConnection* connection)
763 : QuicPerConnectionPacketWriter(writer, connection),
764 factory_(factory) {
767 TestWriterFactory::PerConnectionPacketWriter::~PerConnectionPacketWriter() {
768 factory_->Unregister(this);
771 WriteResult TestWriterFactory::PerConnectionPacketWriter::WritePacket(
772 const char* buffer,
773 size_t buf_len,
774 const IPAddressNumber& self_address,
775 const IPEndPoint& peer_address) {
776 // A DCHECK(factory_current_writer_ == nullptr) would be wrong here -- this
777 // class may be used in a setting where connection()->OnPacketSent() is called
778 // in a different way, so TestWriterFactory::OnPacketSent might never be
779 // called.
780 factory_->current_writer_ = this;
781 return tools::QuicPerConnectionPacketWriter::WritePacket(
782 buffer, buf_len, self_address, peer_address);
785 MockQuicConnectionDebugVisitor::MockQuicConnectionDebugVisitor() {
788 MockQuicConnectionDebugVisitor::~MockQuicConnectionDebugVisitor() {
791 void CreateClientSessionForTest(QuicServerId server_id,
792 bool supports_stateless_rejects,
793 QuicTime::Delta connection_start_time,
794 QuicCryptoClientConfig* crypto_client_config,
795 PacketSavingConnection** client_connection,
796 TestQuicSpdyClientSession** client_session) {
797 CHECK(crypto_client_config);
798 CHECK(client_connection);
799 CHECK(client_session);
800 CHECK(!connection_start_time.IsZero())
801 << "Connections must start at non-zero times, otherwise the "
802 << "strike-register will be unhappy.";
804 QuicConfig config = supports_stateless_rejects
805 ? DefaultQuicConfigStatelessRejects()
806 : DefaultQuicConfig();
807 *client_connection = new PacketSavingConnection(Perspective::IS_CLIENT);
808 *client_session = new TestQuicSpdyClientSession(
809 *client_connection, config, server_id, crypto_client_config);
810 (*client_connection)->AdvanceTime(connection_start_time);
813 void CreateServerSessionForTest(QuicServerId server_id,
814 QuicTime::Delta connection_start_time,
815 QuicCryptoServerConfig* server_crypto_config,
816 PacketSavingConnection** server_connection,
817 TestQuicSpdyServerSession** server_session) {
818 CHECK(server_crypto_config);
819 CHECK(server_connection);
820 CHECK(server_session);
821 CHECK(!connection_start_time.IsZero())
822 << "Connections must start at non-zero times, otherwise the "
823 << "strike-register will be unhappy.";
825 *server_connection = new PacketSavingConnection(Perspective::IS_SERVER);
826 *server_session = new TestQuicSpdyServerSession(
827 *server_connection, DefaultQuicConfig(), server_crypto_config);
829 // We advance the clock initially because the default time is zero and the
830 // strike register worries that we've just overflowed a uint32 time.
831 (*server_connection)->AdvanceTime(connection_start_time);
834 } // namespace test
835 } // namespace net