blob: 4ab3d0c2de0d1398a5dd581ffd6fea95b200d85f [file] [log] [blame]
[email protected]8b37a092012-10-18 21:53:491// 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.
4
5#include "net/quic/test_tools/quic_test_utils.h"
[email protected]9c0b1352012-11-04 00:03:276
[email protected]a5b98172014-06-18 07:01:597#include "base/sha1.h"
[email protected]701bc892013-01-17 04:51:548#include "base/stl_util.h"
[email protected]b12764d2013-12-02 22:28:309#include "base/strings/string_number_conversions.h"
[email protected]8b37a092012-10-18 21:53:4910#include "net/quic/crypto/crypto_framer.h"
[email protected]6f54ab32013-03-02 17:43:3511#include "net/quic/crypto/crypto_handshake.h"
[email protected]872edd9e2013-01-16 08:51:1512#include "net/quic/crypto/crypto_utils.h"
[email protected]5351cc4b2013-03-03 07:22:4113#include "net/quic/crypto/null_encrypter.h"
[email protected]4df69842013-02-27 06:32:1614#include "net/quic/crypto/quic_decrypter.h"
15#include "net/quic/crypto/quic_encrypter.h"
rtenneti16a20772015-02-17 18:58:4816#include "net/quic/quic_data_writer.h"
[email protected]a57e0272013-04-26 07:31:4717#include "net/quic/quic_framer.h"
[email protected]5351cc4b2013-03-03 07:22:4118#include "net/quic/quic_packet_creator.h"
[email protected]79d13dcb2014-02-05 07:23:1319#include "net/quic/quic_utils.h"
[email protected]cbd731e2013-10-24 00:20:3920#include "net/quic/test_tools/quic_connection_peer.h"
[email protected]c244c5a12013-05-07 20:55:0421#include "net/spdy/spdy_frame_builder.h"
[email protected]8b37a092012-10-18 21:53:4922
[email protected]c244c5a12013-05-07 20:55:0423using base::StringPiece;
[email protected]8b37a092012-10-18 21:53:4924using std::max;
25using std::min;
26using std::string;
[email protected]64c12232014-03-26 05:43:5927using testing::AnyNumber;
[email protected]bc356fe2014-06-19 11:14:1428using testing::_;
[email protected]8b37a092012-10-18 21:53:4929
30namespace net {
31namespace test {
[email protected]965dbe62013-08-09 21:34:3132namespace {
33
34// No-op alarm implementation used by MockHelper.
35class TestAlarm : public QuicAlarm {
36 public:
37 explicit TestAlarm(QuicAlarm::Delegate* delegate)
38 : QuicAlarm(delegate) {
39 }
40
dchengb03027d2014-10-21 12:00:2041 void SetImpl() override {}
42 void CancelImpl() override {}
[email protected]965dbe62013-08-09 21:34:3143};
44
45} // namespace
[email protected]8b37a092012-10-18 21:53:4946
[email protected]310d37b2014-08-02 06:15:3747QuicAckFrame MakeAckFrame(QuicPacketSequenceNumber largest_observed) {
[email protected]fb35b0a2014-04-15 21:06:4948 QuicAckFrame ack;
[email protected]310d37b2014-08-02 06:15:3749 ack.largest_observed = largest_observed;
50 ack.entropy_hash = 0;
[email protected]fb35b0a2014-04-15 21:06:4951 return ack;
52}
53
[email protected]aa7e4ef2014-05-28 03:53:1554QuicAckFrame MakeAckFrameWithNackRanges(
55 size_t num_nack_ranges, QuicPacketSequenceNumber least_unacked) {
[email protected]310d37b2014-08-02 06:15:3756 QuicAckFrame ack = MakeAckFrame(2 * num_nack_ranges + least_unacked);
[email protected]aa7e4ef2014-05-28 03:53:1557 // Add enough missing packets to get num_nack_ranges nack ranges.
58 for (QuicPacketSequenceNumber i = 1; i < 2 * num_nack_ranges; i += 2) {
[email protected]310d37b2014-08-02 06:15:3759 ack.missing_packets.insert(least_unacked + i);
[email protected]aa7e4ef2014-05-28 03:53:1560 }
61 return ack;
62}
63
rtennetib6ac61a52015-02-11 20:20:5264QuicPacket* BuildUnsizedDataPacket(QuicFramer* framer,
65 const QuicPacketHeader& header,
66 const QuicFrames& frames) {
[email protected]9cda5fd2014-06-03 10:20:2867 const size_t max_plaintext_size = framer->GetMaxPlaintextSize(kMaxPacketSize);
68 size_t packet_size = GetPacketHeaderSize(header);
69 for (size_t i = 0; i < frames.size(); ++i) {
70 DCHECK_LE(packet_size, max_plaintext_size);
71 bool first_frame = i == 0;
72 bool last_frame = i == frames.size() - 1;
73 const size_t frame_size = framer->GetSerializedFrameLength(
74 frames[i], max_plaintext_size - packet_size, first_frame, last_frame,
75 header.is_in_fec_group,
76 header.public_header.sequence_number_length);
77 DCHECK(frame_size);
78 packet_size += frame_size;
79 }
rtenneti16a20772015-02-17 18:58:4880 return BuildUnsizedDataPacket(framer, header, frames, packet_size);
81}
82
83QuicPacket* BuildUnsizedDataPacket(QuicFramer* framer,
84 const QuicPacketHeader& header,
85 const QuicFrames& frames,
86 size_t packet_size) {
87 char* buffer = new char[packet_size];
88 scoped_ptr<QuicPacket> packet(
89 framer->BuildDataPacket(header, frames, buffer, packet_size));
90 DCHECK(packet.get() != nullptr);
91 // Now I have to re-construct the data packet with data ownership.
92 return new QuicPacket(buffer, packet->length(), true,
93 header.public_header.connection_id_length,
94 header.public_header.version_flag,
95 header.public_header.sequence_number_length);
[email protected]9cda5fd2014-06-03 10:20:2896}
97
[email protected]a5b98172014-06-18 07:01:5998uint64 SimpleRandom::RandUint64() {
99 unsigned char hash[base::kSHA1Length];
100 base::SHA1HashBytes(reinterpret_cast<unsigned char*>(&seed_), sizeof(seed_),
101 hash);
102 memcpy(&seed_, hash, sizeof(seed_));
103 return seed_;
104}
105
[email protected]8b37a092012-10-18 21:53:49106MockFramerVisitor::MockFramerVisitor() {
107 // By default, we want to accept packets.
[email protected]14e8106c2013-03-14 16:25:33108 ON_CALL(*this, OnProtocolVersionMismatch(_))
109 .WillByDefault(testing::Return(false));
110
111 // By default, we want to accept packets.
[email protected]ec86d5462013-11-17 16:04:49112 ON_CALL(*this, OnUnauthenticatedHeader(_))
113 .WillByDefault(testing::Return(true));
114
[email protected]066d8182014-01-04 02:02:45115 ON_CALL(*this, OnUnauthenticatedPublicHeader(_))
116 .WillByDefault(testing::Return(true));
117
[email protected]cff7b7b2013-01-11 08:49:07118 ON_CALL(*this, OnPacketHeader(_))
[email protected]8b37a092012-10-18 21:53:49119 .WillByDefault(testing::Return(true));
[email protected]a57e0272013-04-26 07:31:47120
121 ON_CALL(*this, OnStreamFrame(_))
122 .WillByDefault(testing::Return(true));
123
124 ON_CALL(*this, OnAckFrame(_))
125 .WillByDefault(testing::Return(true));
126
[email protected]93dd91f2014-02-27 00:09:03127 ON_CALL(*this, OnStopWaitingFrame(_))
128 .WillByDefault(testing::Return(true));
129
[email protected]d8c522112014-04-23 09:23:25130 ON_CALL(*this, OnPingFrame(_))
131 .WillByDefault(testing::Return(true));
132
[email protected]a57e0272013-04-26 07:31:47133 ON_CALL(*this, OnRstStreamFrame(_))
134 .WillByDefault(testing::Return(true));
135
136 ON_CALL(*this, OnConnectionCloseFrame(_))
137 .WillByDefault(testing::Return(true));
138
139 ON_CALL(*this, OnGoAwayFrame(_))
140 .WillByDefault(testing::Return(true));
[email protected]8b37a092012-10-18 21:53:49141}
142
[email protected]044ac2b2012-11-13 21:41:06143MockFramerVisitor::~MockFramerVisitor() {
144}
[email protected]8b37a092012-10-18 21:53:49145
[email protected]48878092013-07-26 14:51:56146bool NoOpFramerVisitor::OnProtocolVersionMismatch(QuicVersion version) {
[email protected]14e8106c2013-03-14 16:25:33147 return false;
148}
149
[email protected]066d8182014-01-04 02:02:45150bool NoOpFramerVisitor::OnUnauthenticatedPublicHeader(
151 const QuicPacketPublicHeader& header) {
152 return true;
153}
154
[email protected]ec86d5462013-11-17 16:04:49155bool NoOpFramerVisitor::OnUnauthenticatedHeader(
156 const QuicPacketHeader& header) {
157 return true;
158}
159
[email protected]8b37a092012-10-18 21:53:49160bool NoOpFramerVisitor::OnPacketHeader(const QuicPacketHeader& header) {
161 return true;
162}
163
[email protected]a57e0272013-04-26 07:31:47164bool NoOpFramerVisitor::OnStreamFrame(const QuicStreamFrame& frame) {
165 return true;
166}
167
168bool NoOpFramerVisitor::OnAckFrame(const QuicAckFrame& frame) {
169 return true;
170}
171
[email protected]93dd91f2014-02-27 00:09:03172bool NoOpFramerVisitor::OnStopWaitingFrame(
173 const QuicStopWaitingFrame& frame) {
174 return true;
175}
176
[email protected]d8c522112014-04-23 09:23:25177bool NoOpFramerVisitor::OnPingFrame(const QuicPingFrame& frame) {
178 return true;
179}
180
[email protected]a57e0272013-04-26 07:31:47181bool NoOpFramerVisitor::OnRstStreamFrame(
182 const QuicRstStreamFrame& frame) {
183 return true;
184}
185
186bool NoOpFramerVisitor::OnConnectionCloseFrame(
187 const QuicConnectionCloseFrame& frame) {
188 return true;
189}
190
191bool NoOpFramerVisitor::OnGoAwayFrame(const QuicGoAwayFrame& frame) {
192 return true;
193}
194
[email protected]cb23a922014-02-20 17:42:38195bool NoOpFramerVisitor::OnWindowUpdateFrame(
196 const QuicWindowUpdateFrame& frame) {
197 return true;
198}
199
200bool NoOpFramerVisitor::OnBlockedFrame(const QuicBlockedFrame& frame) {
201 return true;
202}
203
[email protected]8d659e22013-01-19 04:26:10204MockConnectionVisitor::MockConnectionVisitor() {
205}
206
207MockConnectionVisitor::~MockConnectionVisitor() {
208}
209
[email protected]9c0b1352012-11-04 00:03:27210MockHelper::MockHelper() {
211}
212
213MockHelper::~MockHelper() {
214}
215
[email protected]97693d12012-11-16 16:05:00216const QuicClock* MockHelper::GetClock() const {
[email protected]9c0b1352012-11-04 00:03:27217 return &clock_;
218}
219
[email protected]9558c5d32012-12-22 00:08:14220QuicRandom* MockHelper::GetRandomGenerator() {
221 return &random_generator_;
222}
223
[email protected]965dbe62013-08-09 21:34:31224QuicAlarm* MockHelper::CreateAlarm(QuicAlarm::Delegate* delegate) {
225 return new TestAlarm(delegate);
226}
227
[email protected]fe053f92013-04-23 20:18:55228void MockHelper::AdvanceTime(QuicTime::Delta delta) {
229 clock_.AdvanceTime(delta);
230}
231
rtenneti3fe4ebbc2014-11-16 16:43:47232QuicPacketWriter* NiceMockPacketWriterFactory::Create(
233 QuicConnection* /*connection*/) const {
234 return new testing::NiceMock<MockPacketWriter>();
235}
[email protected]6d515822014-08-22 01:58:06236
[email protected]c05a6d222013-12-16 19:42:03237MockConnection::MockConnection(bool is_server)
[email protected]3aa9ca72014-02-27 19:39:43238 : QuicConnection(kTestConnectionId,
[email protected]300ccd52014-01-25 08:00:19239 IPEndPoint(TestPeerIPAddress(), kTestPort),
[email protected]c05a6d222013-12-16 19:42:03240 new testing::NiceMock<MockHelper>(),
[email protected]6d515822014-08-22 01:58:06241 NiceMockPacketWriterFactory(),
242 /* owns_writer= */ true,
rtennetie3779d832014-11-14 02:08:17243 is_server,
rtenneti8696cf92014-11-14 21:12:12244 /* is_secure= */ false,
245 QuicSupportedVersions()),
246 helper_(helper()) {
247}
248
249MockConnection::MockConnection(bool is_server, bool is_secure)
250 : QuicConnection(kTestConnectionId,
251 IPEndPoint(TestPeerIPAddress(), kTestPort),
252 new testing::NiceMock<MockHelper>(),
253 NiceMockPacketWriterFactory(),
254 /* owns_writer= */ true,
255 is_server,
256 is_secure,
rtennetie3779d832014-11-14 02:08:17257 QuicSupportedVersions()),
[email protected]c05a6d222013-12-16 19:42:03258 helper_(helper()) {
259}
260
261MockConnection::MockConnection(IPEndPoint address,
262 bool is_server)
[email protected]3aa9ca72014-02-27 19:39:43263 : QuicConnection(kTestConnectionId, address,
[email protected]c05a6d222013-12-16 19:42:03264 new testing::NiceMock<MockHelper>(),
[email protected]6d515822014-08-22 01:58:06265 NiceMockPacketWriterFactory(),
266 /* owns_writer= */ true,
rtennetie3779d832014-11-14 02:08:17267 is_server,
rtenneti8696cf92014-11-14 21:12:12268 /* is_secure= */ false,
rtennetie3779d832014-11-14 02:08:17269 QuicSupportedVersions()),
[email protected]2cfc6bb82013-10-27 03:40:44270 helper_(helper()) {
[email protected]044ac2b2012-11-13 21:41:06271}
272
[email protected]3aa9ca72014-02-27 19:39:43273MockConnection::MockConnection(QuicConnectionId connection_id,
[email protected]14e8106c2013-03-14 16:25:33274 bool is_server)
[email protected]3aa9ca72014-02-27 19:39:43275 : QuicConnection(connection_id,
276 IPEndPoint(TestPeerIPAddress(), kTestPort),
[email protected]c05a6d222013-12-16 19:42:03277 new testing::NiceMock<MockHelper>(),
[email protected]6d515822014-08-22 01:58:06278 NiceMockPacketWriterFactory(),
279 /* owns_writer= */ true,
rtennetie3779d832014-11-14 02:08:17280 is_server,
rtenneti8696cf92014-11-14 21:12:12281 /* is_secure= */ false,
rtennetie3779d832014-11-14 02:08:17282 QuicSupportedVersions()),
[email protected]c05a6d222013-12-16 19:42:03283 helper_(helper()) {
[email protected]872edd9e2013-01-16 08:51:15284}
285
[email protected]4d640792013-12-18 22:21:08286MockConnection::MockConnection(bool is_server,
287 const QuicVersionVector& supported_versions)
[email protected]3aa9ca72014-02-27 19:39:43288 : QuicConnection(kTestConnectionId,
[email protected]300ccd52014-01-25 08:00:19289 IPEndPoint(TestPeerIPAddress(), kTestPort),
[email protected]4d640792013-12-18 22:21:08290 new testing::NiceMock<MockHelper>(),
[email protected]6d515822014-08-22 01:58:06291 NiceMockPacketWriterFactory(),
292 /* owns_writer= */ true,
rtennetie3779d832014-11-14 02:08:17293 is_server,
rtenneti8696cf92014-11-14 21:12:12294 /* is_secure= */ false,
rtennetie3779d832014-11-14 02:08:17295 supported_versions),
[email protected]4d640792013-12-18 22:21:08296 helper_(helper()) {
297}
298
[email protected]044ac2b2012-11-13 21:41:06299MockConnection::~MockConnection() {
300}
301
[email protected]fe053f92013-04-23 20:18:55302void MockConnection::AdvanceTime(QuicTime::Delta delta) {
[email protected]fe053f92013-04-23 20:18:55303 static_cast<MockHelper*>(helper())->AdvanceTime(delta);
304}
305
[email protected]c05a6d222013-12-16 19:42:03306PacketSavingConnection::PacketSavingConnection(bool is_server)
307 : MockConnection(is_server) {
[email protected]044ac2b2012-11-13 21:41:06308}
309
[email protected]4d640792013-12-18 22:21:08310PacketSavingConnection::PacketSavingConnection(
311 bool is_server,
312 const QuicVersionVector& supported_versions)
313 : MockConnection(is_server, supported_versions) {
314}
315
[email protected]044ac2b2012-11-13 21:41:06316PacketSavingConnection::~PacketSavingConnection() {
[email protected]2532de12013-05-09 12:29:33317 STLDeleteElements(&encrypted_packets_);
[email protected]044ac2b2012-11-13 21:41:06318}
319
rtenneti31e9fd62014-09-16 05:22:15320void PacketSavingConnection::SendOrQueuePacket(QueuedPacket packet) {
rtennetib6ac61a52015-02-11 20:20:52321 encrypted_packets_.push_back(packet.serialized_packet.packet);
rtenneti31e9fd62014-09-16 05:22:15322 // Transfer ownership of the packet to the SentPacketManager and the
323 // ack notifier to the AckNotifierManager.
rtennetia4dcff92014-09-29 18:16:08324 sent_packet_manager_.OnPacketSent(
325 &packet.serialized_packet, 0, QuicTime::Zero(), 1000,
326 NOT_RETRANSMISSION, HAS_RETRANSMITTABLE_DATA);
[email protected]044ac2b2012-11-13 21:41:06327}
328
[email protected]c05a6d222013-12-16 19:42:03329MockSession::MockSession(QuicConnection* connection)
rtenneti8696cf92014-11-14 21:12:12330 : QuicSession(connection, DefaultQuicConfig()) {
[email protected]ccb34212014-07-18 09:27:50331 InitializeSession();
[email protected]bbb10072014-06-13 07:41:59332 ON_CALL(*this, WritevData(_, _, _, _, _, _))
[email protected]cff7b7b2013-01-11 08:49:07333 .WillByDefault(testing::Return(QuicConsumedData(0, false)));
[email protected]044ac2b2012-11-13 21:41:06334}
335
336MockSession::~MockSession() {
337}
338
[email protected]ce7bb1412014-05-17 15:51:33339TestSession::TestSession(QuicConnection* connection, const QuicConfig& config)
rtenneti8696cf92014-11-14 21:12:12340 : QuicSession(connection, config),
rtenneti4a5df262014-11-07 00:43:58341 crypto_stream_(nullptr) {
[email protected]ccb34212014-07-18 09:27:50342 InitializeSession();
343}
[email protected]2532de12013-05-09 12:29:33344
[email protected]b064310782013-05-30 21:12:17345TestSession::~TestSession() {}
[email protected]2532de12013-05-09 12:29:33346
347void TestSession::SetCryptoStream(QuicCryptoStream* stream) {
348 crypto_stream_ = stream;
349}
350
351QuicCryptoStream* TestSession::GetCryptoStream() {
352 return crypto_stream_;
353}
354
[email protected]90f62f092014-03-24 02:41:23355TestClientSession::TestClientSession(QuicConnection* connection,
356 const QuicConfig& config)
rtenneti8696cf92014-11-14 21:12:12357 : QuicClientSessionBase(connection, config),
rtennetibe635732014-10-02 22:51:42358 crypto_stream_(nullptr) {
[email protected]ccb34212014-07-18 09:27:50359 EXPECT_CALL(*this, OnProofValid(_)).Times(AnyNumber());
360 InitializeSession();
[email protected]90f62f092014-03-24 02:41:23361}
362
363TestClientSession::~TestClientSession() {}
364
365void TestClientSession::SetCryptoStream(QuicCryptoStream* stream) {
366 crypto_stream_ = stream;
367}
368
369QuicCryptoStream* TestClientSession::GetCryptoStream() {
370 return crypto_stream_;
371}
372
[email protected]cbd731e2013-10-24 00:20:39373MockPacketWriter::MockPacketWriter() {
374}
375
376MockPacketWriter::~MockPacketWriter() {
377}
378
[email protected]fee17f72013-02-03 07:47:41379MockSendAlgorithm::MockSendAlgorithm() {
[email protected]8d659e22013-01-19 04:26:10380}
381
[email protected]fee17f72013-02-03 07:47:41382MockSendAlgorithm::~MockSendAlgorithm() {
[email protected]8d659e22013-01-19 04:26:10383}
384
[email protected]3aa9ca72014-02-27 19:39:43385MockLossAlgorithm::MockLossAlgorithm() {
386}
387
388MockLossAlgorithm::~MockLossAlgorithm() {
389}
390
[email protected]97cf3022013-09-05 14:30:16391MockAckNotifierDelegate::MockAckNotifierDelegate() {
392}
393
394MockAckNotifierDelegate::~MockAckNotifierDelegate() {
395}
396
[email protected]a692ad9d2014-07-18 21:35:24397MockNetworkChangeVisitor::MockNetworkChangeVisitor() {
398}
399
400MockNetworkChangeVisitor::~MockNetworkChangeVisitor() {
401}
402
[email protected]8b37a092012-10-18 21:53:49403namespace {
404
405string HexDumpWithMarks(const char* data, int length,
406 const bool* marks, int mark_length) {
407 static const char kHexChars[] = "0123456789abcdef";
408 static const int kColumns = 4;
409
410 const int kSizeLimit = 1024;
411 if (length > kSizeLimit || mark_length > kSizeLimit) {
412 LOG(ERROR) << "Only dumping first " << kSizeLimit << " bytes.";
413 length = min(length, kSizeLimit);
414 mark_length = min(mark_length, kSizeLimit);
415 }
416
417 string hex;
418 for (const char* row = data; length > 0;
419 row += kColumns, length -= kColumns) {
420 for (const char *p = row; p < row + 4; ++p) {
421 if (p < row + length) {
422 const bool mark =
423 (marks && (p - data) < mark_length && marks[p - data]);
424 hex += mark ? '*' : ' ';
425 hex += kHexChars[(*p & 0xf0) >> 4];
426 hex += kHexChars[*p & 0x0f];
427 hex += mark ? '*' : ' ';
428 } else {
429 hex += " ";
430 }
431 }
432 hex = hex + " ";
433
434 for (const char *p = row; p < row + 4 && p < row + length; ++p)
435 hex += (*p >= 0x20 && *p <= 0x7f) ? (*p) : '.';
436
437 hex = hex + '\n';
438 }
439 return hex;
440}
441
442} // namespace
443
[email protected]300ccd52014-01-25 08:00:19444IPAddressNumber TestPeerIPAddress() { return Loopback4(); }
445
[email protected]b007e632013-10-28 08:39:25446QuicVersion QuicVersionMax() { return QuicSupportedVersions().front(); }
447
448QuicVersion QuicVersionMin() { return QuicSupportedVersions().back(); }
449
[email protected]c05a6d222013-12-16 19:42:03450IPAddressNumber Loopback4() {
[email protected]300ccd52014-01-25 08:00:19451 IPAddressNumber addr;
452 CHECK(ParseIPLiteralToNumber("127.0.0.1", &addr));
[email protected]c05a6d222013-12-16 19:42:03453 return addr;
454}
455
[email protected]730b35d72014-06-05 03:23:22456IPAddressNumber Loopback6() {
457 IPAddressNumber addr;
458 CHECK(ParseIPLiteralToNumber("::1", &addr));
459 return addr;
460}
461
[email protected]9bb57c72014-03-31 20:36:04462void GenerateBody(string* body, int length) {
463 body->clear();
464 body->reserve(length);
465 for (int i = 0; i < length; ++i) {
466 body->append(1, static_cast<char>(32 + i % (126 - 32)));
467 }
468}
469
[email protected]ffc34bf2014-03-07 02:42:02470QuicEncryptedPacket* ConstructEncryptedPacket(
471 QuicConnectionId connection_id,
472 bool version_flag,
473 bool reset_flag,
474 QuicPacketSequenceNumber sequence_number,
475 const string& data) {
rtenneti9e0fb502015-03-08 06:07:16476 return ConstructEncryptedPacket(
477 connection_id, version_flag, reset_flag, sequence_number, data,
478 PACKET_8BYTE_CONNECTION_ID, PACKET_6BYTE_SEQUENCE_NUMBER);
479}
480
481QuicEncryptedPacket* ConstructEncryptedPacket(
482 QuicConnectionId connection_id,
483 bool version_flag,
484 bool reset_flag,
485 QuicPacketSequenceNumber sequence_number,
486 const string& data,
487 QuicConnectionIdLength connection_id_length,
488 QuicSequenceNumberLength sequence_number_length) {
[email protected]ffc34bf2014-03-07 02:42:02489 QuicPacketHeader header;
490 header.public_header.connection_id = connection_id;
rtenneti9e0fb502015-03-08 06:07:16491 header.public_header.connection_id_length = connection_id_length;
[email protected]ffc34bf2014-03-07 02:42:02492 header.public_header.version_flag = version_flag;
493 header.public_header.reset_flag = reset_flag;
rtenneti9e0fb502015-03-08 06:07:16494 header.public_header.sequence_number_length = sequence_number_length;
[email protected]ffc34bf2014-03-07 02:42:02495 header.packet_sequence_number = sequence_number;
496 header.entropy_flag = false;
497 header.entropy_hash = 0;
498 header.fec_flag = false;
499 header.is_in_fec_group = NOT_IN_FEC_GROUP;
500 header.fec_group = 0;
501 QuicStreamFrame stream_frame(1, false, 0, MakeIOVector(data));
502 QuicFrame frame(&stream_frame);
503 QuicFrames frames;
504 frames.push_back(frame);
505 QuicFramer framer(QuicSupportedVersions(), QuicTime::Zero(), false);
506 scoped_ptr<QuicPacket> packet(
rtennetib6ac61a52015-02-11 20:20:52507 BuildUnsizedDataPacket(&framer, header, frames));
rtennetibe635732014-10-02 22:51:42508 EXPECT_TRUE(packet != nullptr);
[email protected]ffc34bf2014-03-07 02:42:02509 QuicEncryptedPacket* encrypted = framer.EncryptPacket(ENCRYPTION_NONE,
510 sequence_number,
511 *packet);
rtennetibe635732014-10-02 22:51:42512 EXPECT_TRUE(encrypted != nullptr);
[email protected]ffc34bf2014-03-07 02:42:02513 return encrypted;
514}
515
[email protected]8b37a092012-10-18 21:53:49516void CompareCharArraysWithHexError(
517 const string& description,
518 const char* actual,
519 const int actual_len,
520 const char* expected,
521 const int expected_len) {
[email protected]b007e632013-10-28 08:39:25522 EXPECT_EQ(actual_len, expected_len);
[email protected]8b37a092012-10-18 21:53:49523 const int min_len = min(actual_len, expected_len);
524 const int max_len = max(actual_len, expected_len);
[email protected]4356f0f2013-04-07 00:58:17525 scoped_ptr<bool[]> marks(new bool[max_len]);
[email protected]8b37a092012-10-18 21:53:49526 bool identical = (actual_len == expected_len);
527 for (int i = 0; i < min_len; ++i) {
528 if (actual[i] != expected[i]) {
529 marks[i] = true;
530 identical = false;
531 } else {
532 marks[i] = false;
533 }
534 }
535 for (int i = min_len; i < max_len; ++i) {
536 marks[i] = true;
537 }
538 if (identical) return;
539 ADD_FAILURE()
540 << "Description:\n"
541 << description
542 << "\n\nExpected:\n"
543 << HexDumpWithMarks(expected, expected_len, marks.get(), max_len)
544 << "\nActual:\n"
545 << HexDumpWithMarks(actual, actual_len, marks.get(), max_len);
546}
547
[email protected]b12764d2013-12-02 22:28:30548bool DecodeHexString(const base::StringPiece& hex, std::string* bytes) {
549 bytes->clear();
550 if (hex.empty())
551 return true;
552 std::vector<uint8> v;
553 if (!base::HexStringToBytes(hex.as_string(), &v))
554 return false;
555 if (!v.empty())
556 bytes->assign(reinterpret_cast<const char*>(&v[0]), v.size());
557 return true;
558}
559
[email protected]d3d15bf2013-01-30 02:51:54560static QuicPacket* ConstructPacketFromHandshakeMessage(
[email protected]3aa9ca72014-02-27 19:39:43561 QuicConnectionId connection_id,
[email protected]14e8106c2013-03-14 16:25:33562 const CryptoHandshakeMessage& message,
563 bool should_include_version) {
[email protected]8b37a092012-10-18 21:53:49564 CryptoFramer crypto_framer;
[email protected]dc2cc742012-10-21 13:56:13565 scoped_ptr<QuicData> data(crypto_framer.ConstructHandshakeMessage(message));
[email protected]b007e632013-10-28 08:39:25566 QuicFramer quic_framer(QuicSupportedVersions(), QuicTime::Zero(), false);
[email protected]8b37a092012-10-18 21:53:49567
568 QuicPacketHeader header;
[email protected]3aa9ca72014-02-27 19:39:43569 header.public_header.connection_id = connection_id;
[email protected]9db443912013-02-25 05:27:03570 header.public_header.reset_flag = false;
[email protected]14e8106c2013-03-14 16:25:33571 header.public_header.version_flag = should_include_version;
[email protected]8b37a092012-10-18 21:53:49572 header.packet_sequence_number = 1;
[email protected]9db443912013-02-25 05:27:03573 header.entropy_flag = false;
574 header.entropy_hash = 0;
575 header.fec_flag = false;
[email protected]8b37a092012-10-18 21:53:49576 header.fec_group = 0;
577
[email protected]be24ab22012-10-22 03:01:52578 QuicStreamFrame stream_frame(kCryptoStreamId, false, 0,
[email protected]5dafdb62013-11-14 01:24:26579 MakeIOVector(data->AsStringPiece()));
[email protected]8b37a092012-10-18 21:53:49580
[email protected]be24ab22012-10-22 03:01:52581 QuicFrame frame(&stream_frame);
582 QuicFrames frames;
583 frames.push_back(frame);
rtennetib6ac61a52015-02-11 20:20:52584 return BuildUnsizedDataPacket(&quic_framer, header, frames);
[email protected]8b37a092012-10-18 21:53:49585}
586
[email protected]3aa9ca72014-02-27 19:39:43587QuicPacket* ConstructHandshakePacket(QuicConnectionId connection_id,
588 QuicTag tag) {
[email protected]d3d15bf2013-01-30 02:51:54589 CryptoHandshakeMessage message;
[email protected]ccc66e8a2013-03-26 08:26:14590 message.set_tag(tag);
[email protected]3aa9ca72014-02-27 19:39:43591 return ConstructPacketFromHandshakeMessage(connection_id, message, false);
[email protected]d3d15bf2013-01-30 02:51:54592}
593
[email protected]ea825e02013-08-21 18:12:45594size_t GetPacketLengthForOneStream(
595 QuicVersion version,
596 bool include_version,
rtenneti23186682014-10-30 01:49:33597 QuicConnectionIdLength connection_id_length,
[email protected]ea825e02013-08-21 18:12:45598 QuicSequenceNumberLength sequence_number_length,
599 InFecGroup is_in_fec_group,
600 size_t* payload_length) {
[email protected]f62262b2013-07-05 20:57:30601 *payload_length = 1;
602 const size_t stream_length =
[email protected]5dafdb62013-11-14 01:24:26603 NullEncrypter().GetCiphertextSize(*payload_length) +
[email protected]b064310782013-05-30 21:12:17604 QuicPacketCreator::StreamFramePacketOverhead(
[email protected]310d37b2014-08-02 06:15:37605 PACKET_8BYTE_CONNECTION_ID, include_version,
[email protected]aa7e4ef2014-05-28 03:53:15606 sequence_number_length, 0u, is_in_fec_group);
[email protected]5dafdb62013-11-14 01:24:26607 const size_t ack_length = NullEncrypter().GetCiphertextSize(
[email protected]8e01c062013-10-31 07:35:31608 QuicFramer::GetMinAckFrameSize(
[email protected]310d37b2014-08-02 06:15:37609 sequence_number_length, PACKET_1BYTE_SEQUENCE_NUMBER)) +
rtenneti23186682014-10-30 01:49:33610 GetPacketHeaderSize(connection_id_length, include_version,
[email protected]ea825e02013-08-21 18:12:45611 sequence_number_length, is_in_fec_group);
[email protected]f62262b2013-07-05 20:57:30612 if (stream_length < ack_length) {
613 *payload_length = 1 + ack_length - stream_length;
614 }
615
[email protected]5dafdb62013-11-14 01:24:26616 return NullEncrypter().GetCiphertextSize(*payload_length) +
[email protected]f62262b2013-07-05 20:57:30617 QuicPacketCreator::StreamFramePacketOverhead(
rtenneti23186682014-10-30 01:49:33618 connection_id_length, include_version,
[email protected]aa7e4ef2014-05-28 03:53:15619 sequence_number_length, 0u, is_in_fec_group);
[email protected]5351cc4b2013-03-03 07:22:41620}
621
[email protected]a5b98172014-06-18 07:01:59622TestEntropyCalculator::TestEntropyCalculator() {}
[email protected]8e01c062013-10-31 07:35:31623
[email protected]a5b98172014-06-18 07:01:59624TestEntropyCalculator::~TestEntropyCalculator() {}
[email protected]8e01c062013-10-31 07:35:31625
[email protected]48878092013-07-26 14:51:56626QuicPacketEntropyHash TestEntropyCalculator::EntropyHash(
[email protected]9db443912013-02-25 05:27:03627 QuicPacketSequenceNumber sequence_number) const {
628 return 1u;
629}
630
[email protected]a5b98172014-06-18 07:01:59631MockEntropyCalculator::MockEntropyCalculator() {}
[email protected]8e01c062013-10-31 07:35:31632
[email protected]a5b98172014-06-18 07:01:59633MockEntropyCalculator::~MockEntropyCalculator() {}
[email protected]8e01c062013-10-31 07:35:31634
[email protected]b064310782013-05-30 21:12:17635QuicConfig DefaultQuicConfig() {
636 QuicConfig config;
[email protected]7d561352014-06-20 09:09:21637 config.SetInitialStreamFlowControlWindowToSend(
638 kInitialStreamFlowControlWindowForTest);
639 config.SetInitialSessionFlowControlWindowToSend(
640 kInitialSessionFlowControlWindowForTest);
[email protected]b064310782013-05-30 21:12:17641 return config;
642}
643
[email protected]4d640792013-12-18 22:21:08644QuicVersionVector SupportedVersions(QuicVersion version) {
645 QuicVersionVector versions;
646 versions.push_back(version);
647 return versions;
648}
649
rtennetibe635732014-10-02 22:51:42650TestWriterFactory::TestWriterFactory() : current_writer_(nullptr) {}
[email protected]6d515822014-08-22 01:58:06651TestWriterFactory::~TestWriterFactory() {}
652
653QuicPacketWriter* TestWriterFactory::Create(QuicServerPacketWriter* writer,
654 QuicConnection* connection) {
655 return new PerConnectionPacketWriter(this, writer, connection);
656}
657
658void TestWriterFactory::OnPacketSent(WriteResult result) {
rtennetibe635732014-10-02 22:51:42659 if (current_writer_ != nullptr && result.status == WRITE_STATUS_ERROR) {
[email protected]ca4e0d92014-08-22 16:33:22660 current_writer_->connection()->OnWriteError(result.error_code);
rtennetibe635732014-10-02 22:51:42661 current_writer_ = nullptr;
[email protected]6d515822014-08-22 01:58:06662 }
663}
664
665void TestWriterFactory::Unregister(PerConnectionPacketWriter* writer) {
666 if (current_writer_ == writer) {
rtennetibe635732014-10-02 22:51:42667 current_writer_ = nullptr;
[email protected]6d515822014-08-22 01:58:06668 }
669}
670
671TestWriterFactory::PerConnectionPacketWriter::PerConnectionPacketWriter(
672 TestWriterFactory* factory,
673 QuicServerPacketWriter* writer,
674 QuicConnection* connection)
675 : QuicPerConnectionPacketWriter(writer, connection),
676 factory_(factory) {
677}
678
679TestWriterFactory::PerConnectionPacketWriter::~PerConnectionPacketWriter() {
680 factory_->Unregister(this);
681}
682
683WriteResult TestWriterFactory::PerConnectionPacketWriter::WritePacket(
684 const char* buffer,
685 size_t buf_len,
686 const IPAddressNumber& self_address,
687 const IPEndPoint& peer_address) {
rtennetibe635732014-10-02 22:51:42688 // A DCHECK(factory_current_writer_ == nullptr) would be wrong here -- this
689 // class may be used in a setting where connection()->OnPacketSent() is called
690 // in a different way, so TestWriterFactory::OnPacketSent might never be
691 // called.
[email protected]6d515822014-08-22 01:58:06692 factory_->current_writer_ = this;
693 return QuicPerConnectionPacketWriter::WritePacket(buffer,
694 buf_len,
695 self_address,
696 peer_address);
697}
698
[email protected]8b37a092012-10-18 21:53:49699} // namespace test
[email protected]8b37a092012-10-18 21:53:49700} // namespace net