blob: 36d0e78d1ee2f62b77e846ff9c42e1320f71137f [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
40 virtual void SetImpl() OVERRIDE {}
41 virtual void CancelImpl() OVERRIDE {}
42};
43
44} // namespace
[email protected]8b37a092012-10-18 21:53:4945
[email protected]fb35b0a2014-04-15 21:06:4946QuicAckFrame MakeAckFrame(QuicPacketSequenceNumber largest_observed,
47 QuicPacketSequenceNumber least_unacked) {
48 QuicAckFrame ack;
49 ack.received_info.largest_observed = largest_observed;
50 ack.received_info.entropy_hash = 0;
51 ack.sent_info.least_unacked = least_unacked;
52 ack.sent_info.entropy_hash = 0;
53 return ack;
54}
55
[email protected]aa7e4ef2014-05-28 03:53:1556QuicAckFrame MakeAckFrameWithNackRanges(
57 size_t num_nack_ranges, QuicPacketSequenceNumber least_unacked) {
58 QuicAckFrame ack = MakeAckFrame(2 * num_nack_ranges + least_unacked,
59 least_unacked);
60 // Add enough missing packets to get num_nack_ranges nack ranges.
61 for (QuicPacketSequenceNumber i = 1; i < 2 * num_nack_ranges; i += 2) {
62 ack.received_info.missing_packets.insert(least_unacked + i);
63 }
64 return ack;
65}
66
[email protected]9cda5fd2014-06-03 10:20:2867SerializedPacket BuildUnsizedDataPacket(QuicFramer* framer,
68 const QuicPacketHeader& header,
69 const QuicFrames& frames) {
70 const size_t max_plaintext_size = framer->GetMaxPlaintextSize(kMaxPacketSize);
71 size_t packet_size = GetPacketHeaderSize(header);
72 for (size_t i = 0; i < frames.size(); ++i) {
73 DCHECK_LE(packet_size, max_plaintext_size);
74 bool first_frame = i == 0;
75 bool last_frame = i == frames.size() - 1;
76 const size_t frame_size = framer->GetSerializedFrameLength(
77 frames[i], max_plaintext_size - packet_size, first_frame, last_frame,
78 header.is_in_fec_group,
79 header.public_header.sequence_number_length);
80 DCHECK(frame_size);
81 packet_size += frame_size;
82 }
83 return framer->BuildDataPacket(header, frames, packet_size);
84}
85
[email protected]a5b98172014-06-18 07:01:5986uint64 SimpleRandom::RandUint64() {
87 unsigned char hash[base::kSHA1Length];
88 base::SHA1HashBytes(reinterpret_cast<unsigned char*>(&seed_), sizeof(seed_),
89 hash);
90 memcpy(&seed_, hash, sizeof(seed_));
91 return seed_;
92}
93
[email protected]8b37a092012-10-18 21:53:4994MockFramerVisitor::MockFramerVisitor() {
95 // By default, we want to accept packets.
[email protected]14e8106c2013-03-14 16:25:3396 ON_CALL(*this, OnProtocolVersionMismatch(_))
97 .WillByDefault(testing::Return(false));
98
99 // By default, we want to accept packets.
[email protected]ec86d5462013-11-17 16:04:49100 ON_CALL(*this, OnUnauthenticatedHeader(_))
101 .WillByDefault(testing::Return(true));
102
[email protected]066d8182014-01-04 02:02:45103 ON_CALL(*this, OnUnauthenticatedPublicHeader(_))
104 .WillByDefault(testing::Return(true));
105
[email protected]cff7b7b2013-01-11 08:49:07106 ON_CALL(*this, OnPacketHeader(_))
[email protected]8b37a092012-10-18 21:53:49107 .WillByDefault(testing::Return(true));
[email protected]a57e0272013-04-26 07:31:47108
109 ON_CALL(*this, OnStreamFrame(_))
110 .WillByDefault(testing::Return(true));
111
112 ON_CALL(*this, OnAckFrame(_))
113 .WillByDefault(testing::Return(true));
114
115 ON_CALL(*this, OnCongestionFeedbackFrame(_))
116 .WillByDefault(testing::Return(true));
117
[email protected]93dd91f2014-02-27 00:09:03118 ON_CALL(*this, OnStopWaitingFrame(_))
119 .WillByDefault(testing::Return(true));
120
[email protected]d8c522112014-04-23 09:23:25121 ON_CALL(*this, OnPingFrame(_))
122 .WillByDefault(testing::Return(true));
123
[email protected]a57e0272013-04-26 07:31:47124 ON_CALL(*this, OnRstStreamFrame(_))
125 .WillByDefault(testing::Return(true));
126
127 ON_CALL(*this, OnConnectionCloseFrame(_))
128 .WillByDefault(testing::Return(true));
129
130 ON_CALL(*this, OnGoAwayFrame(_))
131 .WillByDefault(testing::Return(true));
[email protected]8b37a092012-10-18 21:53:49132}
133
[email protected]044ac2b2012-11-13 21:41:06134MockFramerVisitor::~MockFramerVisitor() {
135}
[email protected]8b37a092012-10-18 21:53:49136
[email protected]48878092013-07-26 14:51:56137bool NoOpFramerVisitor::OnProtocolVersionMismatch(QuicVersion version) {
[email protected]14e8106c2013-03-14 16:25:33138 return false;
139}
140
[email protected]066d8182014-01-04 02:02:45141bool NoOpFramerVisitor::OnUnauthenticatedPublicHeader(
142 const QuicPacketPublicHeader& header) {
143 return true;
144}
145
[email protected]ec86d5462013-11-17 16:04:49146bool NoOpFramerVisitor::OnUnauthenticatedHeader(
147 const QuicPacketHeader& header) {
148 return true;
149}
150
[email protected]8b37a092012-10-18 21:53:49151bool NoOpFramerVisitor::OnPacketHeader(const QuicPacketHeader& header) {
152 return true;
153}
154
[email protected]a57e0272013-04-26 07:31:47155bool NoOpFramerVisitor::OnStreamFrame(const QuicStreamFrame& frame) {
156 return true;
157}
158
159bool NoOpFramerVisitor::OnAckFrame(const QuicAckFrame& frame) {
160 return true;
161}
162
163bool NoOpFramerVisitor::OnCongestionFeedbackFrame(
164 const QuicCongestionFeedbackFrame& frame) {
165 return true;
166}
167
[email protected]93dd91f2014-02-27 00:09:03168bool NoOpFramerVisitor::OnStopWaitingFrame(
169 const QuicStopWaitingFrame& frame) {
170 return true;
171}
172
[email protected]d8c522112014-04-23 09:23:25173bool NoOpFramerVisitor::OnPingFrame(const QuicPingFrame& frame) {
174 return true;
175}
176
[email protected]a57e0272013-04-26 07:31:47177bool NoOpFramerVisitor::OnRstStreamFrame(
178 const QuicRstStreamFrame& frame) {
179 return true;
180}
181
182bool NoOpFramerVisitor::OnConnectionCloseFrame(
183 const QuicConnectionCloseFrame& frame) {
184 return true;
185}
186
187bool NoOpFramerVisitor::OnGoAwayFrame(const QuicGoAwayFrame& frame) {
188 return true;
189}
190
[email protected]cb23a922014-02-20 17:42:38191bool NoOpFramerVisitor::OnWindowUpdateFrame(
192 const QuicWindowUpdateFrame& frame) {
193 return true;
194}
195
196bool NoOpFramerVisitor::OnBlockedFrame(const QuicBlockedFrame& frame) {
197 return true;
198}
199
[email protected]8d659e22013-01-19 04:26:10200MockConnectionVisitor::MockConnectionVisitor() {
201}
202
203MockConnectionVisitor::~MockConnectionVisitor() {
204}
205
[email protected]9c0b1352012-11-04 00:03:27206MockHelper::MockHelper() {
207}
208
209MockHelper::~MockHelper() {
210}
211
[email protected]97693d12012-11-16 16:05:00212const QuicClock* MockHelper::GetClock() const {
[email protected]9c0b1352012-11-04 00:03:27213 return &clock_;
214}
215
[email protected]9558c5d32012-12-22 00:08:14216QuicRandom* MockHelper::GetRandomGenerator() {
217 return &random_generator_;
218}
219
[email protected]965dbe62013-08-09 21:34:31220QuicAlarm* MockHelper::CreateAlarm(QuicAlarm::Delegate* delegate) {
221 return new TestAlarm(delegate);
222}
223
[email protected]fe053f92013-04-23 20:18:55224void MockHelper::AdvanceTime(QuicTime::Delta delta) {
225 clock_.AdvanceTime(delta);
226}
227
[email protected]c05a6d222013-12-16 19:42:03228MockConnection::MockConnection(bool is_server)
[email protected]3aa9ca72014-02-27 19:39:43229 : QuicConnection(kTestConnectionId,
[email protected]300ccd52014-01-25 08:00:19230 IPEndPoint(TestPeerIPAddress(), kTestPort),
[email protected]c05a6d222013-12-16 19:42:03231 new testing::NiceMock<MockHelper>(),
[email protected]cbd731e2013-10-24 00:20:39232 new testing::NiceMock<MockPacketWriter>(),
[email protected]ce7bb1412014-05-17 15:51:33233 is_server, QuicSupportedVersions()),
[email protected]c05a6d222013-12-16 19:42:03234 writer_(QuicConnectionPeer::GetWriter(this)),
235 helper_(helper()) {
236}
237
238MockConnection::MockConnection(IPEndPoint address,
239 bool is_server)
[email protected]3aa9ca72014-02-27 19:39:43240 : QuicConnection(kTestConnectionId, address,
[email protected]c05a6d222013-12-16 19:42:03241 new testing::NiceMock<MockHelper>(),
242 new testing::NiceMock<MockPacketWriter>(),
[email protected]ce7bb1412014-05-17 15:51:33243 is_server, QuicSupportedVersions()),
[email protected]2cfc6bb82013-10-27 03:40:44244 writer_(QuicConnectionPeer::GetWriter(this)),
245 helper_(helper()) {
[email protected]044ac2b2012-11-13 21:41:06246}
247
[email protected]3aa9ca72014-02-27 19:39:43248MockConnection::MockConnection(QuicConnectionId connection_id,
[email protected]14e8106c2013-03-14 16:25:33249 bool is_server)
[email protected]3aa9ca72014-02-27 19:39:43250 : QuicConnection(connection_id,
251 IPEndPoint(TestPeerIPAddress(), kTestPort),
[email protected]c05a6d222013-12-16 19:42:03252 new testing::NiceMock<MockHelper>(),
253 new testing::NiceMock<MockPacketWriter>(),
[email protected]ce7bb1412014-05-17 15:51:33254 is_server, QuicSupportedVersions()),
[email protected]c05a6d222013-12-16 19:42:03255 writer_(QuicConnectionPeer::GetWriter(this)),
256 helper_(helper()) {
[email protected]872edd9e2013-01-16 08:51:15257}
258
[email protected]4d640792013-12-18 22:21:08259MockConnection::MockConnection(bool is_server,
260 const QuicVersionVector& supported_versions)
[email protected]3aa9ca72014-02-27 19:39:43261 : QuicConnection(kTestConnectionId,
[email protected]300ccd52014-01-25 08:00:19262 IPEndPoint(TestPeerIPAddress(), kTestPort),
[email protected]4d640792013-12-18 22:21:08263 new testing::NiceMock<MockHelper>(),
264 new testing::NiceMock<MockPacketWriter>(),
[email protected]ce7bb1412014-05-17 15:51:33265 is_server, supported_versions),
[email protected]4d640792013-12-18 22:21:08266 writer_(QuicConnectionPeer::GetWriter(this)),
267 helper_(helper()) {
268}
269
[email protected]044ac2b2012-11-13 21:41:06270MockConnection::~MockConnection() {
271}
272
[email protected]fe053f92013-04-23 20:18:55273void MockConnection::AdvanceTime(QuicTime::Delta delta) {
[email protected]fe053f92013-04-23 20:18:55274 static_cast<MockHelper*>(helper())->AdvanceTime(delta);
275}
276
[email protected]c05a6d222013-12-16 19:42:03277PacketSavingConnection::PacketSavingConnection(bool is_server)
278 : MockConnection(is_server) {
[email protected]044ac2b2012-11-13 21:41:06279}
280
[email protected]4d640792013-12-18 22:21:08281PacketSavingConnection::PacketSavingConnection(
282 bool is_server,
283 const QuicVersionVector& supported_versions)
284 : MockConnection(is_server, supported_versions) {
285}
286
[email protected]044ac2b2012-11-13 21:41:06287PacketSavingConnection::~PacketSavingConnection() {
[email protected]701bc892013-01-17 04:51:54288 STLDeleteElements(&packets_);
[email protected]2532de12013-05-09 12:29:33289 STLDeleteElements(&encrypted_packets_);
[email protected]044ac2b2012-11-13 21:41:06290}
291
[email protected]fee17f72013-02-03 07:47:41292bool PacketSavingConnection::SendOrQueuePacket(
[email protected]8ba81212013-05-03 13:11:48293 EncryptionLevel level,
[email protected]ec86d5462013-11-17 16:04:49294 const SerializedPacket& packet,
295 TransmissionType transmission_type) {
296 packets_.push_back(packet.packet);
[email protected]c5e1aca2014-01-30 04:03:04297 QuicEncryptedPacket* encrypted = QuicConnectionPeer::GetFramer(this)->
298 EncryptPacket(level, packet.sequence_number, *packet.packet);
[email protected]2532de12013-05-09 12:29:33299 encrypted_packets_.push_back(encrypted);
[email protected]044ac2b2012-11-13 21:41:06300 return true;
301}
302
[email protected]c05a6d222013-12-16 19:42:03303MockSession::MockSession(QuicConnection* connection)
[email protected]a5b98172014-06-18 07:01:59304 : QuicSession(connection, DefaultQuicConfig()) {
[email protected]ccb34212014-07-18 09:27:50305 InitializeSession();
[email protected]bbb10072014-06-13 07:41:59306 ON_CALL(*this, WritevData(_, _, _, _, _, _))
[email protected]cff7b7b2013-01-11 08:49:07307 .WillByDefault(testing::Return(QuicConsumedData(0, false)));
[email protected]044ac2b2012-11-13 21:41:06308}
309
310MockSession::~MockSession() {
311}
312
[email protected]ce7bb1412014-05-17 15:51:33313TestSession::TestSession(QuicConnection* connection, const QuicConfig& config)
[email protected]a5b98172014-06-18 07:01:59314 : QuicSession(connection, config),
[email protected]ccb34212014-07-18 09:27:50315 crypto_stream_(NULL) {
316 InitializeSession();
317}
[email protected]2532de12013-05-09 12:29:33318
[email protected]b064310782013-05-30 21:12:17319TestSession::~TestSession() {}
[email protected]2532de12013-05-09 12:29:33320
321void TestSession::SetCryptoStream(QuicCryptoStream* stream) {
322 crypto_stream_ = stream;
323}
324
325QuicCryptoStream* TestSession::GetCryptoStream() {
326 return crypto_stream_;
327}
328
[email protected]90f62f092014-03-24 02:41:23329TestClientSession::TestClientSession(QuicConnection* connection,
330 const QuicConfig& config)
[email protected]ccb34212014-07-18 09:27:50331 : QuicClientSessionBase(connection, config),
[email protected]90f62f092014-03-24 02:41:23332 crypto_stream_(NULL) {
[email protected]ccb34212014-07-18 09:27:50333 EXPECT_CALL(*this, OnProofValid(_)).Times(AnyNumber());
334 InitializeSession();
[email protected]90f62f092014-03-24 02:41:23335}
336
337TestClientSession::~TestClientSession() {}
338
339void TestClientSession::SetCryptoStream(QuicCryptoStream* stream) {
340 crypto_stream_ = stream;
341}
342
343QuicCryptoStream* TestClientSession::GetCryptoStream() {
344 return crypto_stream_;
345}
346
[email protected]cbd731e2013-10-24 00:20:39347MockPacketWriter::MockPacketWriter() {
348}
349
350MockPacketWriter::~MockPacketWriter() {
351}
352
[email protected]fee17f72013-02-03 07:47:41353MockSendAlgorithm::MockSendAlgorithm() {
[email protected]8d659e22013-01-19 04:26:10354}
355
[email protected]fee17f72013-02-03 07:47:41356MockSendAlgorithm::~MockSendAlgorithm() {
[email protected]8d659e22013-01-19 04:26:10357}
358
[email protected]3aa9ca72014-02-27 19:39:43359MockLossAlgorithm::MockLossAlgorithm() {
360}
361
362MockLossAlgorithm::~MockLossAlgorithm() {
363}
364
[email protected]97cf3022013-09-05 14:30:16365MockAckNotifierDelegate::MockAckNotifierDelegate() {
366}
367
368MockAckNotifierDelegate::~MockAckNotifierDelegate() {
369}
370
[email protected]8b37a092012-10-18 21:53:49371namespace {
372
373string HexDumpWithMarks(const char* data, int length,
374 const bool* marks, int mark_length) {
375 static const char kHexChars[] = "0123456789abcdef";
376 static const int kColumns = 4;
377
378 const int kSizeLimit = 1024;
379 if (length > kSizeLimit || mark_length > kSizeLimit) {
380 LOG(ERROR) << "Only dumping first " << kSizeLimit << " bytes.";
381 length = min(length, kSizeLimit);
382 mark_length = min(mark_length, kSizeLimit);
383 }
384
385 string hex;
386 for (const char* row = data; length > 0;
387 row += kColumns, length -= kColumns) {
388 for (const char *p = row; p < row + 4; ++p) {
389 if (p < row + length) {
390 const bool mark =
391 (marks && (p - data) < mark_length && marks[p - data]);
392 hex += mark ? '*' : ' ';
393 hex += kHexChars[(*p & 0xf0) >> 4];
394 hex += kHexChars[*p & 0x0f];
395 hex += mark ? '*' : ' ';
396 } else {
397 hex += " ";
398 }
399 }
400 hex = hex + " ";
401
402 for (const char *p = row; p < row + 4 && p < row + length; ++p)
403 hex += (*p >= 0x20 && *p <= 0x7f) ? (*p) : '.';
404
405 hex = hex + '\n';
406 }
407 return hex;
408}
409
410} // namespace
411
[email protected]300ccd52014-01-25 08:00:19412IPAddressNumber TestPeerIPAddress() { return Loopback4(); }
413
[email protected]b007e632013-10-28 08:39:25414QuicVersion QuicVersionMax() { return QuicSupportedVersions().front(); }
415
416QuicVersion QuicVersionMin() { return QuicSupportedVersions().back(); }
417
[email protected]c05a6d222013-12-16 19:42:03418IPAddressNumber Loopback4() {
[email protected]300ccd52014-01-25 08:00:19419 IPAddressNumber addr;
420 CHECK(ParseIPLiteralToNumber("127.0.0.1", &addr));
[email protected]c05a6d222013-12-16 19:42:03421 return addr;
422}
423
[email protected]730b35d72014-06-05 03:23:22424IPAddressNumber Loopback6() {
425 IPAddressNumber addr;
426 CHECK(ParseIPLiteralToNumber("::1", &addr));
427 return addr;
428}
429
[email protected]9bb57c72014-03-31 20:36:04430void GenerateBody(string* body, int length) {
431 body->clear();
432 body->reserve(length);
433 for (int i = 0; i < length; ++i) {
434 body->append(1, static_cast<char>(32 + i % (126 - 32)));
435 }
436}
437
[email protected]ffc34bf2014-03-07 02:42:02438QuicEncryptedPacket* ConstructEncryptedPacket(
439 QuicConnectionId connection_id,
440 bool version_flag,
441 bool reset_flag,
442 QuicPacketSequenceNumber sequence_number,
443 const string& data) {
444 QuicPacketHeader header;
445 header.public_header.connection_id = connection_id;
446 header.public_header.connection_id_length = PACKET_8BYTE_CONNECTION_ID;
447 header.public_header.version_flag = version_flag;
448 header.public_header.reset_flag = reset_flag;
449 header.public_header.sequence_number_length = PACKET_6BYTE_SEQUENCE_NUMBER;
450 header.packet_sequence_number = sequence_number;
451 header.entropy_flag = false;
452 header.entropy_hash = 0;
453 header.fec_flag = false;
454 header.is_in_fec_group = NOT_IN_FEC_GROUP;
455 header.fec_group = 0;
456 QuicStreamFrame stream_frame(1, false, 0, MakeIOVector(data));
457 QuicFrame frame(&stream_frame);
458 QuicFrames frames;
459 frames.push_back(frame);
460 QuicFramer framer(QuicSupportedVersions(), QuicTime::Zero(), false);
461 scoped_ptr<QuicPacket> packet(
[email protected]9cda5fd2014-06-03 10:20:28462 BuildUnsizedDataPacket(&framer, header, frames).packet);
[email protected]ffc34bf2014-03-07 02:42:02463 EXPECT_TRUE(packet != NULL);
464 QuicEncryptedPacket* encrypted = framer.EncryptPacket(ENCRYPTION_NONE,
465 sequence_number,
466 *packet);
467 EXPECT_TRUE(encrypted != NULL);
468 return encrypted;
469}
470
[email protected]8b37a092012-10-18 21:53:49471void CompareCharArraysWithHexError(
472 const string& description,
473 const char* actual,
474 const int actual_len,
475 const char* expected,
476 const int expected_len) {
[email protected]b007e632013-10-28 08:39:25477 EXPECT_EQ(actual_len, expected_len);
[email protected]8b37a092012-10-18 21:53:49478 const int min_len = min(actual_len, expected_len);
479 const int max_len = max(actual_len, expected_len);
[email protected]4356f0f2013-04-07 00:58:17480 scoped_ptr<bool[]> marks(new bool[max_len]);
[email protected]8b37a092012-10-18 21:53:49481 bool identical = (actual_len == expected_len);
482 for (int i = 0; i < min_len; ++i) {
483 if (actual[i] != expected[i]) {
484 marks[i] = true;
485 identical = false;
486 } else {
487 marks[i] = false;
488 }
489 }
490 for (int i = min_len; i < max_len; ++i) {
491 marks[i] = true;
492 }
493 if (identical) return;
494 ADD_FAILURE()
495 << "Description:\n"
496 << description
497 << "\n\nExpected:\n"
498 << HexDumpWithMarks(expected, expected_len, marks.get(), max_len)
499 << "\nActual:\n"
500 << HexDumpWithMarks(actual, actual_len, marks.get(), max_len);
501}
502
[email protected]b12764d2013-12-02 22:28:30503bool DecodeHexString(const base::StringPiece& hex, std::string* bytes) {
504 bytes->clear();
505 if (hex.empty())
506 return true;
507 std::vector<uint8> v;
508 if (!base::HexStringToBytes(hex.as_string(), &v))
509 return false;
510 if (!v.empty())
511 bytes->assign(reinterpret_cast<const char*>(&v[0]), v.size());
512 return true;
513}
514
[email protected]d3d15bf2013-01-30 02:51:54515static QuicPacket* ConstructPacketFromHandshakeMessage(
[email protected]3aa9ca72014-02-27 19:39:43516 QuicConnectionId connection_id,
[email protected]14e8106c2013-03-14 16:25:33517 const CryptoHandshakeMessage& message,
518 bool should_include_version) {
[email protected]8b37a092012-10-18 21:53:49519 CryptoFramer crypto_framer;
[email protected]dc2cc742012-10-21 13:56:13520 scoped_ptr<QuicData> data(crypto_framer.ConstructHandshakeMessage(message));
[email protected]b007e632013-10-28 08:39:25521 QuicFramer quic_framer(QuicSupportedVersions(), QuicTime::Zero(), false);
[email protected]8b37a092012-10-18 21:53:49522
523 QuicPacketHeader header;
[email protected]3aa9ca72014-02-27 19:39:43524 header.public_header.connection_id = connection_id;
[email protected]9db443912013-02-25 05:27:03525 header.public_header.reset_flag = false;
[email protected]14e8106c2013-03-14 16:25:33526 header.public_header.version_flag = should_include_version;
[email protected]8b37a092012-10-18 21:53:49527 header.packet_sequence_number = 1;
[email protected]9db443912013-02-25 05:27:03528 header.entropy_flag = false;
529 header.entropy_hash = 0;
530 header.fec_flag = false;
[email protected]8b37a092012-10-18 21:53:49531 header.fec_group = 0;
532
[email protected]be24ab22012-10-22 03:01:52533 QuicStreamFrame stream_frame(kCryptoStreamId, false, 0,
[email protected]5dafdb62013-11-14 01:24:26534 MakeIOVector(data->AsStringPiece()));
[email protected]8b37a092012-10-18 21:53:49535
[email protected]be24ab22012-10-22 03:01:52536 QuicFrame frame(&stream_frame);
537 QuicFrames frames;
538 frames.push_back(frame);
[email protected]9cda5fd2014-06-03 10:20:28539 return BuildUnsizedDataPacket(&quic_framer, header, frames).packet;
[email protected]8b37a092012-10-18 21:53:49540}
541
[email protected]3aa9ca72014-02-27 19:39:43542QuicPacket* ConstructHandshakePacket(QuicConnectionId connection_id,
543 QuicTag tag) {
[email protected]d3d15bf2013-01-30 02:51:54544 CryptoHandshakeMessage message;
[email protected]ccc66e8a2013-03-26 08:26:14545 message.set_tag(tag);
[email protected]3aa9ca72014-02-27 19:39:43546 return ConstructPacketFromHandshakeMessage(connection_id, message, false);
[email protected]d3d15bf2013-01-30 02:51:54547}
548
[email protected]ea825e02013-08-21 18:12:45549size_t GetPacketLengthForOneStream(
550 QuicVersion version,
551 bool include_version,
552 QuicSequenceNumberLength sequence_number_length,
553 InFecGroup is_in_fec_group,
554 size_t* payload_length) {
[email protected]f62262b2013-07-05 20:57:30555 *payload_length = 1;
556 const size_t stream_length =
[email protected]5dafdb62013-11-14 01:24:26557 NullEncrypter().GetCiphertextSize(*payload_length) +
[email protected]b064310782013-05-30 21:12:17558 QuicPacketCreator::StreamFramePacketOverhead(
[email protected]3aa9ca72014-02-27 19:39:43559 version, PACKET_8BYTE_CONNECTION_ID, include_version,
[email protected]aa7e4ef2014-05-28 03:53:15560 sequence_number_length, 0u, is_in_fec_group);
[email protected]5dafdb62013-11-14 01:24:26561 const size_t ack_length = NullEncrypter().GetCiphertextSize(
[email protected]8e01c062013-10-31 07:35:31562 QuicFramer::GetMinAckFrameSize(
563 version, sequence_number_length, PACKET_1BYTE_SEQUENCE_NUMBER)) +
[email protected]3aa9ca72014-02-27 19:39:43564 GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID, include_version,
[email protected]ea825e02013-08-21 18:12:45565 sequence_number_length, is_in_fec_group);
[email protected]f62262b2013-07-05 20:57:30566 if (stream_length < ack_length) {
567 *payload_length = 1 + ack_length - stream_length;
568 }
569
[email protected]5dafdb62013-11-14 01:24:26570 return NullEncrypter().GetCiphertextSize(*payload_length) +
[email protected]f62262b2013-07-05 20:57:30571 QuicPacketCreator::StreamFramePacketOverhead(
[email protected]3aa9ca72014-02-27 19:39:43572 version, PACKET_8BYTE_CONNECTION_ID, include_version,
[email protected]aa7e4ef2014-05-28 03:53:15573 sequence_number_length, 0u, is_in_fec_group);
[email protected]5351cc4b2013-03-03 07:22:41574}
575
[email protected]a5b98172014-06-18 07:01:59576TestEntropyCalculator::TestEntropyCalculator() {}
[email protected]8e01c062013-10-31 07:35:31577
[email protected]a5b98172014-06-18 07:01:59578TestEntropyCalculator::~TestEntropyCalculator() {}
[email protected]8e01c062013-10-31 07:35:31579
[email protected]48878092013-07-26 14:51:56580QuicPacketEntropyHash TestEntropyCalculator::EntropyHash(
[email protected]9db443912013-02-25 05:27:03581 QuicPacketSequenceNumber sequence_number) const {
582 return 1u;
583}
584
[email protected]a5b98172014-06-18 07:01:59585MockEntropyCalculator::MockEntropyCalculator() {}
[email protected]8e01c062013-10-31 07:35:31586
[email protected]a5b98172014-06-18 07:01:59587MockEntropyCalculator::~MockEntropyCalculator() {}
[email protected]8e01c062013-10-31 07:35:31588
[email protected]b064310782013-05-30 21:12:17589QuicConfig DefaultQuicConfig() {
590 QuicConfig config;
591 config.SetDefaults();
[email protected]7d561352014-06-20 09:09:21592 config.SetInitialFlowControlWindowToSend(
593 kInitialSessionFlowControlWindowForTest);
594 config.SetInitialStreamFlowControlWindowToSend(
595 kInitialStreamFlowControlWindowForTest);
596 config.SetInitialSessionFlowControlWindowToSend(
597 kInitialSessionFlowControlWindowForTest);
[email protected]b064310782013-05-30 21:12:17598 return config;
599}
600
[email protected]4d640792013-12-18 22:21:08601QuicVersionVector SupportedVersions(QuicVersion version) {
602 QuicVersionVector versions;
603 versions.push_back(version);
604 return versions;
605}
606
[email protected]8b37a092012-10-18 21:53:49607} // namespace test
[email protected]8b37a092012-10-18 21:53:49608} // namespace net