blob: fc451060446d4cb5c0326712b0913189ef349b12 [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"
[email protected]a57e0272013-04-26 07:31:4716#include "net/quic/quic_framer.h"
[email protected]5351cc4b2013-03-03 07:22:4117#include "net/quic/quic_packet_creator.h"
[email protected]79d13dcb2014-02-05 07:23:1318#include "net/quic/quic_utils.h"
[email protected]cbd731e2013-10-24 00:20:3919#include "net/quic/test_tools/quic_connection_peer.h"
[email protected]c244c5a12013-05-07 20:55:0420#include "net/spdy/spdy_frame_builder.h"
[email protected]8b37a092012-10-18 21:53:4921
[email protected]c244c5a12013-05-07 20:55:0422using base::StringPiece;
[email protected]8b37a092012-10-18 21:53:4923using std::max;
24using std::min;
25using std::string;
[email protected]64c12232014-03-26 05:43:5926using testing::AnyNumber;
[email protected]bc356fe2014-06-19 11:14:1427using testing::_;
[email protected]8b37a092012-10-18 21:53:4928
29namespace net {
30namespace test {
[email protected]965dbe62013-08-09 21:34:3131namespace {
32
33// No-op alarm implementation used by MockHelper.
34class TestAlarm : public QuicAlarm {
35 public:
36 explicit TestAlarm(QuicAlarm::Delegate* delegate)
37 : QuicAlarm(delegate) {
38 }
39
dchengb03027d2014-10-21 12:00:2040 void SetImpl() override {}
41 void CancelImpl() override {}
[email protected]965dbe62013-08-09 21:34:3142};
43
44} // namespace
[email protected]8b37a092012-10-18 21:53:4945
[email protected]310d37b2014-08-02 06:15:3746QuicAckFrame MakeAckFrame(QuicPacketSequenceNumber largest_observed) {
[email protected]fb35b0a2014-04-15 21:06:4947 QuicAckFrame ack;
[email protected]310d37b2014-08-02 06:15:3748 ack.largest_observed = largest_observed;
49 ack.entropy_hash = 0;
[email protected]fb35b0a2014-04-15 21:06:4950 return ack;
51}
52
[email protected]aa7e4ef2014-05-28 03:53:1553QuicAckFrame MakeAckFrameWithNackRanges(
54 size_t num_nack_ranges, QuicPacketSequenceNumber least_unacked) {
[email protected]310d37b2014-08-02 06:15:3755 QuicAckFrame ack = MakeAckFrame(2 * num_nack_ranges + least_unacked);
[email protected]aa7e4ef2014-05-28 03:53:1556 // Add enough missing packets to get num_nack_ranges nack ranges.
57 for (QuicPacketSequenceNumber i = 1; i < 2 * num_nack_ranges; i += 2) {
[email protected]310d37b2014-08-02 06:15:3758 ack.missing_packets.insert(least_unacked + i);
[email protected]aa7e4ef2014-05-28 03:53:1559 }
60 return ack;
61}
62
rtennetib6ac61a52015-02-11 20:20:5263QuicPacket* BuildUnsizedDataPacket(QuicFramer* framer,
64 const QuicPacketHeader& header,
65 const QuicFrames& frames) {
[email protected]9cda5fd2014-06-03 10:20:2866 const size_t max_plaintext_size = framer->GetMaxPlaintextSize(kMaxPacketSize);
67 size_t packet_size = GetPacketHeaderSize(header);
68 for (size_t i = 0; i < frames.size(); ++i) {
69 DCHECK_LE(packet_size, max_plaintext_size);
70 bool first_frame = i == 0;
71 bool last_frame = i == frames.size() - 1;
72 const size_t frame_size = framer->GetSerializedFrameLength(
73 frames[i], max_plaintext_size - packet_size, first_frame, last_frame,
74 header.is_in_fec_group,
75 header.public_header.sequence_number_length);
76 DCHECK(frame_size);
77 packet_size += frame_size;
78 }
79 return framer->BuildDataPacket(header, frames, packet_size);
80}
81
[email protected]a5b98172014-06-18 07:01:5982uint64 SimpleRandom::RandUint64() {
83 unsigned char hash[base::kSHA1Length];
84 base::SHA1HashBytes(reinterpret_cast<unsigned char*>(&seed_), sizeof(seed_),
85 hash);
86 memcpy(&seed_, hash, sizeof(seed_));
87 return seed_;
88}
89
[email protected]8b37a092012-10-18 21:53:4990MockFramerVisitor::MockFramerVisitor() {
91 // By default, we want to accept packets.
[email protected]14e8106c2013-03-14 16:25:3392 ON_CALL(*this, OnProtocolVersionMismatch(_))
93 .WillByDefault(testing::Return(false));
94
95 // By default, we want to accept packets.
[email protected]ec86d5462013-11-17 16:04:4996 ON_CALL(*this, OnUnauthenticatedHeader(_))
97 .WillByDefault(testing::Return(true));
98
[email protected]066d8182014-01-04 02:02:4599 ON_CALL(*this, OnUnauthenticatedPublicHeader(_))
100 .WillByDefault(testing::Return(true));
101
[email protected]cff7b7b2013-01-11 08:49:07102 ON_CALL(*this, OnPacketHeader(_))
[email protected]8b37a092012-10-18 21:53:49103 .WillByDefault(testing::Return(true));
[email protected]a57e0272013-04-26 07:31:47104
105 ON_CALL(*this, OnStreamFrame(_))
106 .WillByDefault(testing::Return(true));
107
108 ON_CALL(*this, OnAckFrame(_))
109 .WillByDefault(testing::Return(true));
110
[email protected]93dd91f2014-02-27 00:09:03111 ON_CALL(*this, OnStopWaitingFrame(_))
112 .WillByDefault(testing::Return(true));
113
[email protected]d8c522112014-04-23 09:23:25114 ON_CALL(*this, OnPingFrame(_))
115 .WillByDefault(testing::Return(true));
116
[email protected]a57e0272013-04-26 07:31:47117 ON_CALL(*this, OnRstStreamFrame(_))
118 .WillByDefault(testing::Return(true));
119
120 ON_CALL(*this, OnConnectionCloseFrame(_))
121 .WillByDefault(testing::Return(true));
122
123 ON_CALL(*this, OnGoAwayFrame(_))
124 .WillByDefault(testing::Return(true));
[email protected]8b37a092012-10-18 21:53:49125}
126
[email protected]044ac2b2012-11-13 21:41:06127MockFramerVisitor::~MockFramerVisitor() {
128}
[email protected]8b37a092012-10-18 21:53:49129
[email protected]48878092013-07-26 14:51:56130bool NoOpFramerVisitor::OnProtocolVersionMismatch(QuicVersion version) {
[email protected]14e8106c2013-03-14 16:25:33131 return false;
132}
133
[email protected]066d8182014-01-04 02:02:45134bool NoOpFramerVisitor::OnUnauthenticatedPublicHeader(
135 const QuicPacketPublicHeader& header) {
136 return true;
137}
138
[email protected]ec86d5462013-11-17 16:04:49139bool NoOpFramerVisitor::OnUnauthenticatedHeader(
140 const QuicPacketHeader& header) {
141 return true;
142}
143
[email protected]8b37a092012-10-18 21:53:49144bool NoOpFramerVisitor::OnPacketHeader(const QuicPacketHeader& header) {
145 return true;
146}
147
[email protected]a57e0272013-04-26 07:31:47148bool NoOpFramerVisitor::OnStreamFrame(const QuicStreamFrame& frame) {
149 return true;
150}
151
152bool NoOpFramerVisitor::OnAckFrame(const QuicAckFrame& frame) {
153 return true;
154}
155
[email protected]93dd91f2014-02-27 00:09:03156bool NoOpFramerVisitor::OnStopWaitingFrame(
157 const QuicStopWaitingFrame& frame) {
158 return true;
159}
160
[email protected]d8c522112014-04-23 09:23:25161bool NoOpFramerVisitor::OnPingFrame(const QuicPingFrame& frame) {
162 return true;
163}
164
[email protected]a57e0272013-04-26 07:31:47165bool NoOpFramerVisitor::OnRstStreamFrame(
166 const QuicRstStreamFrame& frame) {
167 return true;
168}
169
170bool NoOpFramerVisitor::OnConnectionCloseFrame(
171 const QuicConnectionCloseFrame& frame) {
172 return true;
173}
174
175bool NoOpFramerVisitor::OnGoAwayFrame(const QuicGoAwayFrame& frame) {
176 return true;
177}
178
[email protected]cb23a922014-02-20 17:42:38179bool NoOpFramerVisitor::OnWindowUpdateFrame(
180 const QuicWindowUpdateFrame& frame) {
181 return true;
182}
183
184bool NoOpFramerVisitor::OnBlockedFrame(const QuicBlockedFrame& frame) {
185 return true;
186}
187
[email protected]8d659e22013-01-19 04:26:10188MockConnectionVisitor::MockConnectionVisitor() {
189}
190
191MockConnectionVisitor::~MockConnectionVisitor() {
192}
193
[email protected]9c0b1352012-11-04 00:03:27194MockHelper::MockHelper() {
195}
196
197MockHelper::~MockHelper() {
198}
199
[email protected]97693d12012-11-16 16:05:00200const QuicClock* MockHelper::GetClock() const {
[email protected]9c0b1352012-11-04 00:03:27201 return &clock_;
202}
203
[email protected]9558c5d32012-12-22 00:08:14204QuicRandom* MockHelper::GetRandomGenerator() {
205 return &random_generator_;
206}
207
[email protected]965dbe62013-08-09 21:34:31208QuicAlarm* MockHelper::CreateAlarm(QuicAlarm::Delegate* delegate) {
209 return new TestAlarm(delegate);
210}
211
[email protected]fe053f92013-04-23 20:18:55212void MockHelper::AdvanceTime(QuicTime::Delta delta) {
213 clock_.AdvanceTime(delta);
214}
215
rtenneti3fe4ebbc2014-11-16 16:43:47216QuicPacketWriter* NiceMockPacketWriterFactory::Create(
217 QuicConnection* /*connection*/) const {
218 return new testing::NiceMock<MockPacketWriter>();
219}
[email protected]6d515822014-08-22 01:58:06220
[email protected]c05a6d222013-12-16 19:42:03221MockConnection::MockConnection(bool is_server)
[email protected]3aa9ca72014-02-27 19:39:43222 : QuicConnection(kTestConnectionId,
[email protected]300ccd52014-01-25 08:00:19223 IPEndPoint(TestPeerIPAddress(), kTestPort),
[email protected]c05a6d222013-12-16 19:42:03224 new testing::NiceMock<MockHelper>(),
[email protected]6d515822014-08-22 01:58:06225 NiceMockPacketWriterFactory(),
226 /* owns_writer= */ true,
rtennetie3779d832014-11-14 02:08:17227 is_server,
rtenneti8696cf92014-11-14 21:12:12228 /* is_secure= */ false,
229 QuicSupportedVersions()),
230 helper_(helper()) {
231}
232
233MockConnection::MockConnection(bool is_server, bool is_secure)
234 : QuicConnection(kTestConnectionId,
235 IPEndPoint(TestPeerIPAddress(), kTestPort),
236 new testing::NiceMock<MockHelper>(),
237 NiceMockPacketWriterFactory(),
238 /* owns_writer= */ true,
239 is_server,
240 is_secure,
rtennetie3779d832014-11-14 02:08:17241 QuicSupportedVersions()),
[email protected]c05a6d222013-12-16 19:42:03242 helper_(helper()) {
243}
244
245MockConnection::MockConnection(IPEndPoint address,
246 bool is_server)
[email protected]3aa9ca72014-02-27 19:39:43247 : QuicConnection(kTestConnectionId, address,
[email protected]c05a6d222013-12-16 19:42:03248 new testing::NiceMock<MockHelper>(),
[email protected]6d515822014-08-22 01:58:06249 NiceMockPacketWriterFactory(),
250 /* owns_writer= */ true,
rtennetie3779d832014-11-14 02:08:17251 is_server,
rtenneti8696cf92014-11-14 21:12:12252 /* is_secure= */ false,
rtennetie3779d832014-11-14 02:08:17253 QuicSupportedVersions()),
[email protected]2cfc6bb82013-10-27 03:40:44254 helper_(helper()) {
[email protected]044ac2b2012-11-13 21:41:06255}
256
[email protected]3aa9ca72014-02-27 19:39:43257MockConnection::MockConnection(QuicConnectionId connection_id,
[email protected]14e8106c2013-03-14 16:25:33258 bool is_server)
[email protected]3aa9ca72014-02-27 19:39:43259 : QuicConnection(connection_id,
260 IPEndPoint(TestPeerIPAddress(), kTestPort),
[email protected]c05a6d222013-12-16 19:42:03261 new testing::NiceMock<MockHelper>(),
[email protected]6d515822014-08-22 01:58:06262 NiceMockPacketWriterFactory(),
263 /* owns_writer= */ true,
rtennetie3779d832014-11-14 02:08:17264 is_server,
rtenneti8696cf92014-11-14 21:12:12265 /* is_secure= */ false,
rtennetie3779d832014-11-14 02:08:17266 QuicSupportedVersions()),
[email protected]c05a6d222013-12-16 19:42:03267 helper_(helper()) {
[email protected]872edd9e2013-01-16 08:51:15268}
269
[email protected]4d640792013-12-18 22:21:08270MockConnection::MockConnection(bool is_server,
271 const QuicVersionVector& supported_versions)
[email protected]3aa9ca72014-02-27 19:39:43272 : QuicConnection(kTestConnectionId,
[email protected]300ccd52014-01-25 08:00:19273 IPEndPoint(TestPeerIPAddress(), kTestPort),
[email protected]4d640792013-12-18 22:21:08274 new testing::NiceMock<MockHelper>(),
[email protected]6d515822014-08-22 01:58:06275 NiceMockPacketWriterFactory(),
276 /* owns_writer= */ true,
rtennetie3779d832014-11-14 02:08:17277 is_server,
rtenneti8696cf92014-11-14 21:12:12278 /* is_secure= */ false,
rtennetie3779d832014-11-14 02:08:17279 supported_versions),
[email protected]4d640792013-12-18 22:21:08280 helper_(helper()) {
281}
282
[email protected]044ac2b2012-11-13 21:41:06283MockConnection::~MockConnection() {
284}
285
[email protected]fe053f92013-04-23 20:18:55286void MockConnection::AdvanceTime(QuicTime::Delta delta) {
[email protected]fe053f92013-04-23 20:18:55287 static_cast<MockHelper*>(helper())->AdvanceTime(delta);
288}
289
[email protected]c05a6d222013-12-16 19:42:03290PacketSavingConnection::PacketSavingConnection(bool is_server)
291 : MockConnection(is_server) {
[email protected]044ac2b2012-11-13 21:41:06292}
293
[email protected]4d640792013-12-18 22:21:08294PacketSavingConnection::PacketSavingConnection(
295 bool is_server,
296 const QuicVersionVector& supported_versions)
297 : MockConnection(is_server, supported_versions) {
298}
299
[email protected]044ac2b2012-11-13 21:41:06300PacketSavingConnection::~PacketSavingConnection() {
[email protected]2532de12013-05-09 12:29:33301 STLDeleteElements(&encrypted_packets_);
[email protected]044ac2b2012-11-13 21:41:06302}
303
rtenneti31e9fd62014-09-16 05:22:15304void PacketSavingConnection::SendOrQueuePacket(QueuedPacket packet) {
rtennetib6ac61a52015-02-11 20:20:52305 encrypted_packets_.push_back(packet.serialized_packet.packet);
rtenneti31e9fd62014-09-16 05:22:15306 // Transfer ownership of the packet to the SentPacketManager and the
307 // ack notifier to the AckNotifierManager.
rtennetia4dcff92014-09-29 18:16:08308 sent_packet_manager_.OnPacketSent(
309 &packet.serialized_packet, 0, QuicTime::Zero(), 1000,
310 NOT_RETRANSMISSION, HAS_RETRANSMITTABLE_DATA);
[email protected]044ac2b2012-11-13 21:41:06311}
312
[email protected]c05a6d222013-12-16 19:42:03313MockSession::MockSession(QuicConnection* connection)
rtenneti8696cf92014-11-14 21:12:12314 : QuicSession(connection, DefaultQuicConfig()) {
[email protected]ccb34212014-07-18 09:27:50315 InitializeSession();
[email protected]bbb10072014-06-13 07:41:59316 ON_CALL(*this, WritevData(_, _, _, _, _, _))
[email protected]cff7b7b2013-01-11 08:49:07317 .WillByDefault(testing::Return(QuicConsumedData(0, false)));
[email protected]044ac2b2012-11-13 21:41:06318}
319
320MockSession::~MockSession() {
321}
322
[email protected]ce7bb1412014-05-17 15:51:33323TestSession::TestSession(QuicConnection* connection, const QuicConfig& config)
rtenneti8696cf92014-11-14 21:12:12324 : QuicSession(connection, config),
rtenneti4a5df262014-11-07 00:43:58325 crypto_stream_(nullptr) {
[email protected]ccb34212014-07-18 09:27:50326 InitializeSession();
327}
[email protected]2532de12013-05-09 12:29:33328
[email protected]b064310782013-05-30 21:12:17329TestSession::~TestSession() {}
[email protected]2532de12013-05-09 12:29:33330
331void TestSession::SetCryptoStream(QuicCryptoStream* stream) {
332 crypto_stream_ = stream;
333}
334
335QuicCryptoStream* TestSession::GetCryptoStream() {
336 return crypto_stream_;
337}
338
[email protected]90f62f092014-03-24 02:41:23339TestClientSession::TestClientSession(QuicConnection* connection,
340 const QuicConfig& config)
rtenneti8696cf92014-11-14 21:12:12341 : QuicClientSessionBase(connection, config),
rtennetibe635732014-10-02 22:51:42342 crypto_stream_(nullptr) {
[email protected]ccb34212014-07-18 09:27:50343 EXPECT_CALL(*this, OnProofValid(_)).Times(AnyNumber());
344 InitializeSession();
[email protected]90f62f092014-03-24 02:41:23345}
346
347TestClientSession::~TestClientSession() {}
348
349void TestClientSession::SetCryptoStream(QuicCryptoStream* stream) {
350 crypto_stream_ = stream;
351}
352
353QuicCryptoStream* TestClientSession::GetCryptoStream() {
354 return crypto_stream_;
355}
356
[email protected]cbd731e2013-10-24 00:20:39357MockPacketWriter::MockPacketWriter() {
358}
359
360MockPacketWriter::~MockPacketWriter() {
361}
362
[email protected]fee17f72013-02-03 07:47:41363MockSendAlgorithm::MockSendAlgorithm() {
[email protected]8d659e22013-01-19 04:26:10364}
365
[email protected]fee17f72013-02-03 07:47:41366MockSendAlgorithm::~MockSendAlgorithm() {
[email protected]8d659e22013-01-19 04:26:10367}
368
[email protected]3aa9ca72014-02-27 19:39:43369MockLossAlgorithm::MockLossAlgorithm() {
370}
371
372MockLossAlgorithm::~MockLossAlgorithm() {
373}
374
[email protected]97cf3022013-09-05 14:30:16375MockAckNotifierDelegate::MockAckNotifierDelegate() {
376}
377
378MockAckNotifierDelegate::~MockAckNotifierDelegate() {
379}
380
[email protected]a692ad9d2014-07-18 21:35:24381MockNetworkChangeVisitor::MockNetworkChangeVisitor() {
382}
383
384MockNetworkChangeVisitor::~MockNetworkChangeVisitor() {
385}
386
[email protected]8b37a092012-10-18 21:53:49387namespace {
388
389string HexDumpWithMarks(const char* data, int length,
390 const bool* marks, int mark_length) {
391 static const char kHexChars[] = "0123456789abcdef";
392 static const int kColumns = 4;
393
394 const int kSizeLimit = 1024;
395 if (length > kSizeLimit || mark_length > kSizeLimit) {
396 LOG(ERROR) << "Only dumping first " << kSizeLimit << " bytes.";
397 length = min(length, kSizeLimit);
398 mark_length = min(mark_length, kSizeLimit);
399 }
400
401 string hex;
402 for (const char* row = data; length > 0;
403 row += kColumns, length -= kColumns) {
404 for (const char *p = row; p < row + 4; ++p) {
405 if (p < row + length) {
406 const bool mark =
407 (marks && (p - data) < mark_length && marks[p - data]);
408 hex += mark ? '*' : ' ';
409 hex += kHexChars[(*p & 0xf0) >> 4];
410 hex += kHexChars[*p & 0x0f];
411 hex += mark ? '*' : ' ';
412 } else {
413 hex += " ";
414 }
415 }
416 hex = hex + " ";
417
418 for (const char *p = row; p < row + 4 && p < row + length; ++p)
419 hex += (*p >= 0x20 && *p <= 0x7f) ? (*p) : '.';
420
421 hex = hex + '\n';
422 }
423 return hex;
424}
425
426} // namespace
427
[email protected]300ccd52014-01-25 08:00:19428IPAddressNumber TestPeerIPAddress() { return Loopback4(); }
429
[email protected]b007e632013-10-28 08:39:25430QuicVersion QuicVersionMax() { return QuicSupportedVersions().front(); }
431
432QuicVersion QuicVersionMin() { return QuicSupportedVersions().back(); }
433
[email protected]c05a6d222013-12-16 19:42:03434IPAddressNumber Loopback4() {
[email protected]300ccd52014-01-25 08:00:19435 IPAddressNumber addr;
436 CHECK(ParseIPLiteralToNumber("127.0.0.1", &addr));
[email protected]c05a6d222013-12-16 19:42:03437 return addr;
438}
439
[email protected]730b35d72014-06-05 03:23:22440IPAddressNumber Loopback6() {
441 IPAddressNumber addr;
442 CHECK(ParseIPLiteralToNumber("::1", &addr));
443 return addr;
444}
445
[email protected]9bb57c72014-03-31 20:36:04446void GenerateBody(string* body, int length) {
447 body->clear();
448 body->reserve(length);
449 for (int i = 0; i < length; ++i) {
450 body->append(1, static_cast<char>(32 + i % (126 - 32)));
451 }
452}
453
[email protected]ffc34bf2014-03-07 02:42:02454QuicEncryptedPacket* ConstructEncryptedPacket(
455 QuicConnectionId connection_id,
456 bool version_flag,
457 bool reset_flag,
458 QuicPacketSequenceNumber sequence_number,
459 const string& data) {
460 QuicPacketHeader header;
461 header.public_header.connection_id = connection_id;
462 header.public_header.connection_id_length = PACKET_8BYTE_CONNECTION_ID;
463 header.public_header.version_flag = version_flag;
464 header.public_header.reset_flag = reset_flag;
465 header.public_header.sequence_number_length = PACKET_6BYTE_SEQUENCE_NUMBER;
466 header.packet_sequence_number = sequence_number;
467 header.entropy_flag = false;
468 header.entropy_hash = 0;
469 header.fec_flag = false;
470 header.is_in_fec_group = NOT_IN_FEC_GROUP;
471 header.fec_group = 0;
472 QuicStreamFrame stream_frame(1, false, 0, MakeIOVector(data));
473 QuicFrame frame(&stream_frame);
474 QuicFrames frames;
475 frames.push_back(frame);
476 QuicFramer framer(QuicSupportedVersions(), QuicTime::Zero(), false);
477 scoped_ptr<QuicPacket> packet(
rtennetib6ac61a52015-02-11 20:20:52478 BuildUnsizedDataPacket(&framer, header, frames));
rtennetibe635732014-10-02 22:51:42479 EXPECT_TRUE(packet != nullptr);
[email protected]ffc34bf2014-03-07 02:42:02480 QuicEncryptedPacket* encrypted = framer.EncryptPacket(ENCRYPTION_NONE,
481 sequence_number,
482 *packet);
rtennetibe635732014-10-02 22:51:42483 EXPECT_TRUE(encrypted != nullptr);
[email protected]ffc34bf2014-03-07 02:42:02484 return encrypted;
485}
486
[email protected]8b37a092012-10-18 21:53:49487void CompareCharArraysWithHexError(
488 const string& description,
489 const char* actual,
490 const int actual_len,
491 const char* expected,
492 const int expected_len) {
[email protected]b007e632013-10-28 08:39:25493 EXPECT_EQ(actual_len, expected_len);
[email protected]8b37a092012-10-18 21:53:49494 const int min_len = min(actual_len, expected_len);
495 const int max_len = max(actual_len, expected_len);
[email protected]4356f0f2013-04-07 00:58:17496 scoped_ptr<bool[]> marks(new bool[max_len]);
[email protected]8b37a092012-10-18 21:53:49497 bool identical = (actual_len == expected_len);
498 for (int i = 0; i < min_len; ++i) {
499 if (actual[i] != expected[i]) {
500 marks[i] = true;
501 identical = false;
502 } else {
503 marks[i] = false;
504 }
505 }
506 for (int i = min_len; i < max_len; ++i) {
507 marks[i] = true;
508 }
509 if (identical) return;
510 ADD_FAILURE()
511 << "Description:\n"
512 << description
513 << "\n\nExpected:\n"
514 << HexDumpWithMarks(expected, expected_len, marks.get(), max_len)
515 << "\nActual:\n"
516 << HexDumpWithMarks(actual, actual_len, marks.get(), max_len);
517}
518
[email protected]b12764d2013-12-02 22:28:30519bool DecodeHexString(const base::StringPiece& hex, std::string* bytes) {
520 bytes->clear();
521 if (hex.empty())
522 return true;
523 std::vector<uint8> v;
524 if (!base::HexStringToBytes(hex.as_string(), &v))
525 return false;
526 if (!v.empty())
527 bytes->assign(reinterpret_cast<const char*>(&v[0]), v.size());
528 return true;
529}
530
[email protected]d3d15bf2013-01-30 02:51:54531static QuicPacket* ConstructPacketFromHandshakeMessage(
[email protected]3aa9ca72014-02-27 19:39:43532 QuicConnectionId connection_id,
[email protected]14e8106c2013-03-14 16:25:33533 const CryptoHandshakeMessage& message,
534 bool should_include_version) {
[email protected]8b37a092012-10-18 21:53:49535 CryptoFramer crypto_framer;
[email protected]dc2cc742012-10-21 13:56:13536 scoped_ptr<QuicData> data(crypto_framer.ConstructHandshakeMessage(message));
[email protected]b007e632013-10-28 08:39:25537 QuicFramer quic_framer(QuicSupportedVersions(), QuicTime::Zero(), false);
[email protected]8b37a092012-10-18 21:53:49538
539 QuicPacketHeader header;
[email protected]3aa9ca72014-02-27 19:39:43540 header.public_header.connection_id = connection_id;
[email protected]9db443912013-02-25 05:27:03541 header.public_header.reset_flag = false;
[email protected]14e8106c2013-03-14 16:25:33542 header.public_header.version_flag = should_include_version;
[email protected]8b37a092012-10-18 21:53:49543 header.packet_sequence_number = 1;
[email protected]9db443912013-02-25 05:27:03544 header.entropy_flag = false;
545 header.entropy_hash = 0;
546 header.fec_flag = false;
[email protected]8b37a092012-10-18 21:53:49547 header.fec_group = 0;
548
[email protected]be24ab22012-10-22 03:01:52549 QuicStreamFrame stream_frame(kCryptoStreamId, false, 0,
[email protected]5dafdb62013-11-14 01:24:26550 MakeIOVector(data->AsStringPiece()));
[email protected]8b37a092012-10-18 21:53:49551
[email protected]be24ab22012-10-22 03:01:52552 QuicFrame frame(&stream_frame);
553 QuicFrames frames;
554 frames.push_back(frame);
rtennetib6ac61a52015-02-11 20:20:52555 return BuildUnsizedDataPacket(&quic_framer, header, frames);
[email protected]8b37a092012-10-18 21:53:49556}
557
[email protected]3aa9ca72014-02-27 19:39:43558QuicPacket* ConstructHandshakePacket(QuicConnectionId connection_id,
559 QuicTag tag) {
[email protected]d3d15bf2013-01-30 02:51:54560 CryptoHandshakeMessage message;
[email protected]ccc66e8a2013-03-26 08:26:14561 message.set_tag(tag);
[email protected]3aa9ca72014-02-27 19:39:43562 return ConstructPacketFromHandshakeMessage(connection_id, message, false);
[email protected]d3d15bf2013-01-30 02:51:54563}
564
[email protected]ea825e02013-08-21 18:12:45565size_t GetPacketLengthForOneStream(
566 QuicVersion version,
567 bool include_version,
rtenneti23186682014-10-30 01:49:33568 QuicConnectionIdLength connection_id_length,
[email protected]ea825e02013-08-21 18:12:45569 QuicSequenceNumberLength sequence_number_length,
570 InFecGroup is_in_fec_group,
571 size_t* payload_length) {
[email protected]f62262b2013-07-05 20:57:30572 *payload_length = 1;
573 const size_t stream_length =
[email protected]5dafdb62013-11-14 01:24:26574 NullEncrypter().GetCiphertextSize(*payload_length) +
[email protected]b064310782013-05-30 21:12:17575 QuicPacketCreator::StreamFramePacketOverhead(
[email protected]310d37b2014-08-02 06:15:37576 PACKET_8BYTE_CONNECTION_ID, include_version,
[email protected]aa7e4ef2014-05-28 03:53:15577 sequence_number_length, 0u, is_in_fec_group);
[email protected]5dafdb62013-11-14 01:24:26578 const size_t ack_length = NullEncrypter().GetCiphertextSize(
[email protected]8e01c062013-10-31 07:35:31579 QuicFramer::GetMinAckFrameSize(
[email protected]310d37b2014-08-02 06:15:37580 sequence_number_length, PACKET_1BYTE_SEQUENCE_NUMBER)) +
rtenneti23186682014-10-30 01:49:33581 GetPacketHeaderSize(connection_id_length, include_version,
[email protected]ea825e02013-08-21 18:12:45582 sequence_number_length, is_in_fec_group);
[email protected]f62262b2013-07-05 20:57:30583 if (stream_length < ack_length) {
584 *payload_length = 1 + ack_length - stream_length;
585 }
586
[email protected]5dafdb62013-11-14 01:24:26587 return NullEncrypter().GetCiphertextSize(*payload_length) +
[email protected]f62262b2013-07-05 20:57:30588 QuicPacketCreator::StreamFramePacketOverhead(
rtenneti23186682014-10-30 01:49:33589 connection_id_length, include_version,
[email protected]aa7e4ef2014-05-28 03:53:15590 sequence_number_length, 0u, is_in_fec_group);
[email protected]5351cc4b2013-03-03 07:22:41591}
592
[email protected]a5b98172014-06-18 07:01:59593TestEntropyCalculator::TestEntropyCalculator() {}
[email protected]8e01c062013-10-31 07:35:31594
[email protected]a5b98172014-06-18 07:01:59595TestEntropyCalculator::~TestEntropyCalculator() {}
[email protected]8e01c062013-10-31 07:35:31596
[email protected]48878092013-07-26 14:51:56597QuicPacketEntropyHash TestEntropyCalculator::EntropyHash(
[email protected]9db443912013-02-25 05:27:03598 QuicPacketSequenceNumber sequence_number) const {
599 return 1u;
600}
601
[email protected]a5b98172014-06-18 07:01:59602MockEntropyCalculator::MockEntropyCalculator() {}
[email protected]8e01c062013-10-31 07:35:31603
[email protected]a5b98172014-06-18 07:01:59604MockEntropyCalculator::~MockEntropyCalculator() {}
[email protected]8e01c062013-10-31 07:35:31605
[email protected]b064310782013-05-30 21:12:17606QuicConfig DefaultQuicConfig() {
607 QuicConfig config;
[email protected]7d561352014-06-20 09:09:21608 config.SetInitialStreamFlowControlWindowToSend(
609 kInitialStreamFlowControlWindowForTest);
610 config.SetInitialSessionFlowControlWindowToSend(
611 kInitialSessionFlowControlWindowForTest);
[email protected]b064310782013-05-30 21:12:17612 return config;
613}
614
[email protected]4d640792013-12-18 22:21:08615QuicVersionVector SupportedVersions(QuicVersion version) {
616 QuicVersionVector versions;
617 versions.push_back(version);
618 return versions;
619}
620
rtennetibe635732014-10-02 22:51:42621TestWriterFactory::TestWriterFactory() : current_writer_(nullptr) {}
[email protected]6d515822014-08-22 01:58:06622TestWriterFactory::~TestWriterFactory() {}
623
624QuicPacketWriter* TestWriterFactory::Create(QuicServerPacketWriter* writer,
625 QuicConnection* connection) {
626 return new PerConnectionPacketWriter(this, writer, connection);
627}
628
629void TestWriterFactory::OnPacketSent(WriteResult result) {
rtennetibe635732014-10-02 22:51:42630 if (current_writer_ != nullptr && result.status == WRITE_STATUS_ERROR) {
[email protected]ca4e0d92014-08-22 16:33:22631 current_writer_->connection()->OnWriteError(result.error_code);
rtennetibe635732014-10-02 22:51:42632 current_writer_ = nullptr;
[email protected]6d515822014-08-22 01:58:06633 }
634}
635
636void TestWriterFactory::Unregister(PerConnectionPacketWriter* writer) {
637 if (current_writer_ == writer) {
rtennetibe635732014-10-02 22:51:42638 current_writer_ = nullptr;
[email protected]6d515822014-08-22 01:58:06639 }
640}
641
642TestWriterFactory::PerConnectionPacketWriter::PerConnectionPacketWriter(
643 TestWriterFactory* factory,
644 QuicServerPacketWriter* writer,
645 QuicConnection* connection)
646 : QuicPerConnectionPacketWriter(writer, connection),
647 factory_(factory) {
648}
649
650TestWriterFactory::PerConnectionPacketWriter::~PerConnectionPacketWriter() {
651 factory_->Unregister(this);
652}
653
654WriteResult TestWriterFactory::PerConnectionPacketWriter::WritePacket(
655 const char* buffer,
656 size_t buf_len,
657 const IPAddressNumber& self_address,
658 const IPEndPoint& peer_address) {
rtennetibe635732014-10-02 22:51:42659 // A DCHECK(factory_current_writer_ == nullptr) would be wrong here -- this
660 // class may be used in a setting where connection()->OnPacketSent() is called
661 // in a different way, so TestWriterFactory::OnPacketSent might never be
662 // called.
[email protected]6d515822014-08-22 01:58:06663 factory_->current_writer_ = this;
664 return QuicPerConnectionPacketWriter::WritePacket(buffer,
665 buf_len,
666 self_address,
667 peer_address);
668}
669
[email protected]8b37a092012-10-18 21:53:49670} // namespace test
[email protected]8b37a092012-10-18 21:53:49671} // namespace net