blob: 727c5446c8f0d75dee96f3ac0b91a64d2c84d5ce [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]701bc892013-01-17 04:51:547#include "base/stl_util.h"
[email protected]b12764d2013-12-02 22:28:308#include "base/strings/string_number_conversions.h"
[email protected]8b37a092012-10-18 21:53:499#include "net/quic/crypto/crypto_framer.h"
[email protected]6f54ab32013-03-02 17:43:3510#include "net/quic/crypto/crypto_handshake.h"
[email protected]872edd9e2013-01-16 08:51:1511#include "net/quic/crypto/crypto_utils.h"
[email protected]5351cc4b2013-03-03 07:22:4112#include "net/quic/crypto/null_encrypter.h"
[email protected]4df69842013-02-27 06:32:1613#include "net/quic/crypto/quic_decrypter.h"
14#include "net/quic/crypto/quic_encrypter.h"
[email protected]a57e0272013-04-26 07:31:4715#include "net/quic/quic_framer.h"
[email protected]5351cc4b2013-03-03 07:22:4116#include "net/quic/quic_packet_creator.h"
[email protected]79d13dcb2014-02-05 07:23:1317#include "net/quic/quic_utils.h"
[email protected]cbd731e2013-10-24 00:20:3918#include "net/quic/test_tools/quic_connection_peer.h"
[email protected]c244c5a12013-05-07 20:55:0419#include "net/spdy/spdy_frame_builder.h"
[email protected]8b37a092012-10-18 21:53:4920
[email protected]c244c5a12013-05-07 20:55:0421using base::StringPiece;
[email protected]8b37a092012-10-18 21:53:4922using std::max;
23using std::min;
24using std::string;
[email protected]cff7b7b2013-01-11 08:49:0725using testing::_;
[email protected]64c12232014-03-26 05:43:5926using testing::AnyNumber;
[email protected]8b37a092012-10-18 21:53:4927
28namespace net {
29namespace test {
[email protected]965dbe62013-08-09 21:34:3130namespace {
31
32// No-op alarm implementation used by MockHelper.
33class TestAlarm : public QuicAlarm {
34 public:
35 explicit TestAlarm(QuicAlarm::Delegate* delegate)
36 : QuicAlarm(delegate) {
37 }
38
39 virtual void SetImpl() OVERRIDE {}
40 virtual void CancelImpl() OVERRIDE {}
41};
42
43} // namespace
[email protected]8b37a092012-10-18 21:53:4944
[email protected]fb35b0a2014-04-15 21:06:4945QuicAckFrame MakeAckFrame(QuicPacketSequenceNumber largest_observed,
46 QuicPacketSequenceNumber least_unacked) {
47 QuicAckFrame ack;
48 ack.received_info.largest_observed = largest_observed;
49 ack.received_info.entropy_hash = 0;
50 ack.sent_info.least_unacked = least_unacked;
51 ack.sent_info.entropy_hash = 0;
52 return ack;
53}
54
[email protected]aa7e4ef2014-05-28 03:53:1555QuicAckFrame MakeAckFrameWithNackRanges(
56 size_t num_nack_ranges, QuicPacketSequenceNumber least_unacked) {
57 QuicAckFrame ack = MakeAckFrame(2 * num_nack_ranges + least_unacked,
58 least_unacked);
59 // Add enough missing packets to get num_nack_ranges nack ranges.
60 for (QuicPacketSequenceNumber i = 1; i < 2 * num_nack_ranges; i += 2) {
61 ack.received_info.missing_packets.insert(least_unacked + i);
62 }
63 return ack;
64}
65
[email protected]9cda5fd2014-06-03 10:20:2866SerializedPacket BuildUnsizedDataPacket(QuicFramer* framer,
67 const QuicPacketHeader& header,
68 const QuicFrames& frames) {
69 const size_t max_plaintext_size = framer->GetMaxPlaintextSize(kMaxPacketSize);
70 size_t packet_size = GetPacketHeaderSize(header);
71 for (size_t i = 0; i < frames.size(); ++i) {
72 DCHECK_LE(packet_size, max_plaintext_size);
73 bool first_frame = i == 0;
74 bool last_frame = i == frames.size() - 1;
75 const size_t frame_size = framer->GetSerializedFrameLength(
76 frames[i], max_plaintext_size - packet_size, first_frame, last_frame,
77 header.is_in_fec_group,
78 header.public_header.sequence_number_length);
79 DCHECK(frame_size);
80 packet_size += frame_size;
81 }
82 return framer->BuildDataPacket(header, frames, packet_size);
83}
84
[email protected]8b37a092012-10-18 21:53:4985MockFramerVisitor::MockFramerVisitor() {
86 // By default, we want to accept packets.
[email protected]14e8106c2013-03-14 16:25:3387 ON_CALL(*this, OnProtocolVersionMismatch(_))
88 .WillByDefault(testing::Return(false));
89
90 // By default, we want to accept packets.
[email protected]ec86d5462013-11-17 16:04:4991 ON_CALL(*this, OnUnauthenticatedHeader(_))
92 .WillByDefault(testing::Return(true));
93
[email protected]066d8182014-01-04 02:02:4594 ON_CALL(*this, OnUnauthenticatedPublicHeader(_))
95 .WillByDefault(testing::Return(true));
96
[email protected]cff7b7b2013-01-11 08:49:0797 ON_CALL(*this, OnPacketHeader(_))
[email protected]8b37a092012-10-18 21:53:4998 .WillByDefault(testing::Return(true));
[email protected]a57e0272013-04-26 07:31:4799
100 ON_CALL(*this, OnStreamFrame(_))
101 .WillByDefault(testing::Return(true));
102
103 ON_CALL(*this, OnAckFrame(_))
104 .WillByDefault(testing::Return(true));
105
106 ON_CALL(*this, OnCongestionFeedbackFrame(_))
107 .WillByDefault(testing::Return(true));
108
[email protected]93dd91f2014-02-27 00:09:03109 ON_CALL(*this, OnStopWaitingFrame(_))
110 .WillByDefault(testing::Return(true));
111
[email protected]d8c522112014-04-23 09:23:25112 ON_CALL(*this, OnPingFrame(_))
113 .WillByDefault(testing::Return(true));
114
[email protected]a57e0272013-04-26 07:31:47115 ON_CALL(*this, OnRstStreamFrame(_))
116 .WillByDefault(testing::Return(true));
117
118 ON_CALL(*this, OnConnectionCloseFrame(_))
119 .WillByDefault(testing::Return(true));
120
121 ON_CALL(*this, OnGoAwayFrame(_))
122 .WillByDefault(testing::Return(true));
[email protected]8b37a092012-10-18 21:53:49123}
124
[email protected]044ac2b2012-11-13 21:41:06125MockFramerVisitor::~MockFramerVisitor() {
126}
[email protected]8b37a092012-10-18 21:53:49127
[email protected]48878092013-07-26 14:51:56128bool NoOpFramerVisitor::OnProtocolVersionMismatch(QuicVersion version) {
[email protected]14e8106c2013-03-14 16:25:33129 return false;
130}
131
[email protected]066d8182014-01-04 02:02:45132bool NoOpFramerVisitor::OnUnauthenticatedPublicHeader(
133 const QuicPacketPublicHeader& header) {
134 return true;
135}
136
[email protected]ec86d5462013-11-17 16:04:49137bool NoOpFramerVisitor::OnUnauthenticatedHeader(
138 const QuicPacketHeader& header) {
139 return true;
140}
141
[email protected]8b37a092012-10-18 21:53:49142bool NoOpFramerVisitor::OnPacketHeader(const QuicPacketHeader& header) {
143 return true;
144}
145
[email protected]a57e0272013-04-26 07:31:47146bool NoOpFramerVisitor::OnStreamFrame(const QuicStreamFrame& frame) {
147 return true;
148}
149
150bool NoOpFramerVisitor::OnAckFrame(const QuicAckFrame& frame) {
151 return true;
152}
153
154bool NoOpFramerVisitor::OnCongestionFeedbackFrame(
155 const QuicCongestionFeedbackFrame& frame) {
156 return true;
157}
158
[email protected]93dd91f2014-02-27 00:09:03159bool NoOpFramerVisitor::OnStopWaitingFrame(
160 const QuicStopWaitingFrame& frame) {
161 return true;
162}
163
[email protected]d8c522112014-04-23 09:23:25164bool NoOpFramerVisitor::OnPingFrame(const QuicPingFrame& frame) {
165 return true;
166}
167
[email protected]a57e0272013-04-26 07:31:47168bool NoOpFramerVisitor::OnRstStreamFrame(
169 const QuicRstStreamFrame& frame) {
170 return true;
171}
172
173bool NoOpFramerVisitor::OnConnectionCloseFrame(
174 const QuicConnectionCloseFrame& frame) {
175 return true;
176}
177
178bool NoOpFramerVisitor::OnGoAwayFrame(const QuicGoAwayFrame& frame) {
179 return true;
180}
181
[email protected]cb23a922014-02-20 17:42:38182bool NoOpFramerVisitor::OnWindowUpdateFrame(
183 const QuicWindowUpdateFrame& frame) {
184 return true;
185}
186
187bool NoOpFramerVisitor::OnBlockedFrame(const QuicBlockedFrame& frame) {
188 return true;
189}
190
[email protected]8d659e22013-01-19 04:26:10191MockConnectionVisitor::MockConnectionVisitor() {
192}
193
194MockConnectionVisitor::~MockConnectionVisitor() {
195}
196
[email protected]9c0b1352012-11-04 00:03:27197MockHelper::MockHelper() {
198}
199
200MockHelper::~MockHelper() {
201}
202
[email protected]97693d12012-11-16 16:05:00203const QuicClock* MockHelper::GetClock() const {
[email protected]9c0b1352012-11-04 00:03:27204 return &clock_;
205}
206
[email protected]9558c5d32012-12-22 00:08:14207QuicRandom* MockHelper::GetRandomGenerator() {
208 return &random_generator_;
209}
210
[email protected]965dbe62013-08-09 21:34:31211QuicAlarm* MockHelper::CreateAlarm(QuicAlarm::Delegate* delegate) {
212 return new TestAlarm(delegate);
213}
214
[email protected]fe053f92013-04-23 20:18:55215void MockHelper::AdvanceTime(QuicTime::Delta delta) {
216 clock_.AdvanceTime(delta);
217}
218
[email protected]c05a6d222013-12-16 19:42:03219MockConnection::MockConnection(bool is_server)
[email protected]3aa9ca72014-02-27 19:39:43220 : QuicConnection(kTestConnectionId,
[email protected]300ccd52014-01-25 08:00:19221 IPEndPoint(TestPeerIPAddress(), kTestPort),
[email protected]c05a6d222013-12-16 19:42:03222 new testing::NiceMock<MockHelper>(),
[email protected]cbd731e2013-10-24 00:20:39223 new testing::NiceMock<MockPacketWriter>(),
[email protected]ce7bb1412014-05-17 15:51:33224 is_server, QuicSupportedVersions()),
[email protected]c05a6d222013-12-16 19:42:03225 writer_(QuicConnectionPeer::GetWriter(this)),
226 helper_(helper()) {
227}
228
229MockConnection::MockConnection(IPEndPoint address,
230 bool is_server)
[email protected]3aa9ca72014-02-27 19:39:43231 : QuicConnection(kTestConnectionId, address,
[email protected]c05a6d222013-12-16 19:42:03232 new testing::NiceMock<MockHelper>(),
233 new testing::NiceMock<MockPacketWriter>(),
[email protected]ce7bb1412014-05-17 15:51:33234 is_server, QuicSupportedVersions()),
[email protected]2cfc6bb82013-10-27 03:40:44235 writer_(QuicConnectionPeer::GetWriter(this)),
236 helper_(helper()) {
[email protected]044ac2b2012-11-13 21:41:06237}
238
[email protected]3aa9ca72014-02-27 19:39:43239MockConnection::MockConnection(QuicConnectionId connection_id,
[email protected]14e8106c2013-03-14 16:25:33240 bool is_server)
[email protected]3aa9ca72014-02-27 19:39:43241 : QuicConnection(connection_id,
242 IPEndPoint(TestPeerIPAddress(), kTestPort),
[email protected]c05a6d222013-12-16 19:42:03243 new testing::NiceMock<MockHelper>(),
244 new testing::NiceMock<MockPacketWriter>(),
[email protected]ce7bb1412014-05-17 15:51:33245 is_server, QuicSupportedVersions()),
[email protected]c05a6d222013-12-16 19:42:03246 writer_(QuicConnectionPeer::GetWriter(this)),
247 helper_(helper()) {
[email protected]872edd9e2013-01-16 08:51:15248}
249
[email protected]4d640792013-12-18 22:21:08250MockConnection::MockConnection(bool is_server,
251 const QuicVersionVector& supported_versions)
[email protected]3aa9ca72014-02-27 19:39:43252 : QuicConnection(kTestConnectionId,
[email protected]300ccd52014-01-25 08:00:19253 IPEndPoint(TestPeerIPAddress(), kTestPort),
[email protected]4d640792013-12-18 22:21:08254 new testing::NiceMock<MockHelper>(),
255 new testing::NiceMock<MockPacketWriter>(),
[email protected]ce7bb1412014-05-17 15:51:33256 is_server, supported_versions),
[email protected]4d640792013-12-18 22:21:08257 writer_(QuicConnectionPeer::GetWriter(this)),
258 helper_(helper()) {
259}
260
[email protected]044ac2b2012-11-13 21:41:06261MockConnection::~MockConnection() {
262}
263
[email protected]fe053f92013-04-23 20:18:55264void MockConnection::AdvanceTime(QuicTime::Delta delta) {
[email protected]fe053f92013-04-23 20:18:55265 static_cast<MockHelper*>(helper())->AdvanceTime(delta);
266}
267
[email protected]c05a6d222013-12-16 19:42:03268PacketSavingConnection::PacketSavingConnection(bool is_server)
269 : MockConnection(is_server) {
[email protected]044ac2b2012-11-13 21:41:06270}
271
[email protected]4d640792013-12-18 22:21:08272PacketSavingConnection::PacketSavingConnection(
273 bool is_server,
274 const QuicVersionVector& supported_versions)
275 : MockConnection(is_server, supported_versions) {
276}
277
[email protected]044ac2b2012-11-13 21:41:06278PacketSavingConnection::~PacketSavingConnection() {
[email protected]701bc892013-01-17 04:51:54279 STLDeleteElements(&packets_);
[email protected]2532de12013-05-09 12:29:33280 STLDeleteElements(&encrypted_packets_);
[email protected]044ac2b2012-11-13 21:41:06281}
282
[email protected]fee17f72013-02-03 07:47:41283bool PacketSavingConnection::SendOrQueuePacket(
[email protected]8ba81212013-05-03 13:11:48284 EncryptionLevel level,
[email protected]ec86d5462013-11-17 16:04:49285 const SerializedPacket& packet,
286 TransmissionType transmission_type) {
287 packets_.push_back(packet.packet);
[email protected]c5e1aca2014-01-30 04:03:04288 QuicEncryptedPacket* encrypted = QuicConnectionPeer::GetFramer(this)->
289 EncryptPacket(level, packet.sequence_number, *packet.packet);
[email protected]2532de12013-05-09 12:29:33290 encrypted_packets_.push_back(encrypted);
[email protected]044ac2b2012-11-13 21:41:06291 return true;
292}
293
[email protected]c05a6d222013-12-16 19:42:03294MockSession::MockSession(QuicConnection* connection)
[email protected]ce7bb1412014-05-17 15:51:33295 : QuicSession(connection, kInitialFlowControlWindowForTest,
296 DefaultQuicConfig()) {
[email protected]bbb10072014-06-13 07:41:59297 ON_CALL(*this, WritevData(_, _, _, _, _, _))
[email protected]cff7b7b2013-01-11 08:49:07298 .WillByDefault(testing::Return(QuicConsumedData(0, false)));
[email protected]044ac2b2012-11-13 21:41:06299}
300
301MockSession::~MockSession() {
302}
303
[email protected]ce7bb1412014-05-17 15:51:33304TestSession::TestSession(QuicConnection* connection, const QuicConfig& config)
305 : QuicSession(connection, kInitialFlowControlWindowForTest, config),
306 crypto_stream_(NULL) {}
[email protected]2532de12013-05-09 12:29:33307
[email protected]b064310782013-05-30 21:12:17308TestSession::~TestSession() {}
[email protected]2532de12013-05-09 12:29:33309
310void TestSession::SetCryptoStream(QuicCryptoStream* stream) {
311 crypto_stream_ = stream;
312}
313
314QuicCryptoStream* TestSession::GetCryptoStream() {
315 return crypto_stream_;
316}
317
[email protected]90f62f092014-03-24 02:41:23318TestClientSession::TestClientSession(QuicConnection* connection,
319 const QuicConfig& config)
[email protected]ce7bb1412014-05-17 15:51:33320 : QuicClientSessionBase(connection, kInitialFlowControlWindowForTest,
321 config),
[email protected]90f62f092014-03-24 02:41:23322 crypto_stream_(NULL) {
[email protected]64c12232014-03-26 05:43:59323 EXPECT_CALL(*this, OnProofValid(_)).Times(AnyNumber());
[email protected]90f62f092014-03-24 02:41:23324}
325
326TestClientSession::~TestClientSession() {}
327
328void TestClientSession::SetCryptoStream(QuicCryptoStream* stream) {
329 crypto_stream_ = stream;
330}
331
332QuicCryptoStream* TestClientSession::GetCryptoStream() {
333 return crypto_stream_;
334}
335
[email protected]cbd731e2013-10-24 00:20:39336MockPacketWriter::MockPacketWriter() {
337}
338
339MockPacketWriter::~MockPacketWriter() {
340}
341
[email protected]fee17f72013-02-03 07:47:41342MockSendAlgorithm::MockSendAlgorithm() {
[email protected]8d659e22013-01-19 04:26:10343}
344
[email protected]fee17f72013-02-03 07:47:41345MockSendAlgorithm::~MockSendAlgorithm() {
[email protected]8d659e22013-01-19 04:26:10346}
347
[email protected]3aa9ca72014-02-27 19:39:43348MockLossAlgorithm::MockLossAlgorithm() {
349}
350
351MockLossAlgorithm::~MockLossAlgorithm() {
352}
353
[email protected]97cf3022013-09-05 14:30:16354MockAckNotifierDelegate::MockAckNotifierDelegate() {
355}
356
357MockAckNotifierDelegate::~MockAckNotifierDelegate() {
358}
359
[email protected]8b37a092012-10-18 21:53:49360namespace {
361
362string HexDumpWithMarks(const char* data, int length,
363 const bool* marks, int mark_length) {
364 static const char kHexChars[] = "0123456789abcdef";
365 static const int kColumns = 4;
366
367 const int kSizeLimit = 1024;
368 if (length > kSizeLimit || mark_length > kSizeLimit) {
369 LOG(ERROR) << "Only dumping first " << kSizeLimit << " bytes.";
370 length = min(length, kSizeLimit);
371 mark_length = min(mark_length, kSizeLimit);
372 }
373
374 string hex;
375 for (const char* row = data; length > 0;
376 row += kColumns, length -= kColumns) {
377 for (const char *p = row; p < row + 4; ++p) {
378 if (p < row + length) {
379 const bool mark =
380 (marks && (p - data) < mark_length && marks[p - data]);
381 hex += mark ? '*' : ' ';
382 hex += kHexChars[(*p & 0xf0) >> 4];
383 hex += kHexChars[*p & 0x0f];
384 hex += mark ? '*' : ' ';
385 } else {
386 hex += " ";
387 }
388 }
389 hex = hex + " ";
390
391 for (const char *p = row; p < row + 4 && p < row + length; ++p)
392 hex += (*p >= 0x20 && *p <= 0x7f) ? (*p) : '.';
393
394 hex = hex + '\n';
395 }
396 return hex;
397}
398
399} // namespace
400
[email protected]300ccd52014-01-25 08:00:19401IPAddressNumber TestPeerIPAddress() { return Loopback4(); }
402
[email protected]b007e632013-10-28 08:39:25403QuicVersion QuicVersionMax() { return QuicSupportedVersions().front(); }
404
405QuicVersion QuicVersionMin() { return QuicSupportedVersions().back(); }
406
[email protected]c05a6d222013-12-16 19:42:03407IPAddressNumber Loopback4() {
[email protected]300ccd52014-01-25 08:00:19408 IPAddressNumber addr;
409 CHECK(ParseIPLiteralToNumber("127.0.0.1", &addr));
[email protected]c05a6d222013-12-16 19:42:03410 return addr;
411}
412
[email protected]730b35d72014-06-05 03:23:22413IPAddressNumber Loopback6() {
414 IPAddressNumber addr;
415 CHECK(ParseIPLiteralToNumber("::1", &addr));
416 return addr;
417}
418
[email protected]9bb57c72014-03-31 20:36:04419void GenerateBody(string* body, int length) {
420 body->clear();
421 body->reserve(length);
422 for (int i = 0; i < length; ++i) {
423 body->append(1, static_cast<char>(32 + i % (126 - 32)));
424 }
425}
426
[email protected]ffc34bf2014-03-07 02:42:02427QuicEncryptedPacket* ConstructEncryptedPacket(
428 QuicConnectionId connection_id,
429 bool version_flag,
430 bool reset_flag,
431 QuicPacketSequenceNumber sequence_number,
432 const string& data) {
433 QuicPacketHeader header;
434 header.public_header.connection_id = connection_id;
435 header.public_header.connection_id_length = PACKET_8BYTE_CONNECTION_ID;
436 header.public_header.version_flag = version_flag;
437 header.public_header.reset_flag = reset_flag;
438 header.public_header.sequence_number_length = PACKET_6BYTE_SEQUENCE_NUMBER;
439 header.packet_sequence_number = sequence_number;
440 header.entropy_flag = false;
441 header.entropy_hash = 0;
442 header.fec_flag = false;
443 header.is_in_fec_group = NOT_IN_FEC_GROUP;
444 header.fec_group = 0;
445 QuicStreamFrame stream_frame(1, false, 0, MakeIOVector(data));
446 QuicFrame frame(&stream_frame);
447 QuicFrames frames;
448 frames.push_back(frame);
449 QuicFramer framer(QuicSupportedVersions(), QuicTime::Zero(), false);
450 scoped_ptr<QuicPacket> packet(
[email protected]9cda5fd2014-06-03 10:20:28451 BuildUnsizedDataPacket(&framer, header, frames).packet);
[email protected]ffc34bf2014-03-07 02:42:02452 EXPECT_TRUE(packet != NULL);
453 QuicEncryptedPacket* encrypted = framer.EncryptPacket(ENCRYPTION_NONE,
454 sequence_number,
455 *packet);
456 EXPECT_TRUE(encrypted != NULL);
457 return encrypted;
458}
459
[email protected]8b37a092012-10-18 21:53:49460void CompareCharArraysWithHexError(
461 const string& description,
462 const char* actual,
463 const int actual_len,
464 const char* expected,
465 const int expected_len) {
[email protected]b007e632013-10-28 08:39:25466 EXPECT_EQ(actual_len, expected_len);
[email protected]8b37a092012-10-18 21:53:49467 const int min_len = min(actual_len, expected_len);
468 const int max_len = max(actual_len, expected_len);
[email protected]4356f0f2013-04-07 00:58:17469 scoped_ptr<bool[]> marks(new bool[max_len]);
[email protected]8b37a092012-10-18 21:53:49470 bool identical = (actual_len == expected_len);
471 for (int i = 0; i < min_len; ++i) {
472 if (actual[i] != expected[i]) {
473 marks[i] = true;
474 identical = false;
475 } else {
476 marks[i] = false;
477 }
478 }
479 for (int i = min_len; i < max_len; ++i) {
480 marks[i] = true;
481 }
482 if (identical) return;
483 ADD_FAILURE()
484 << "Description:\n"
485 << description
486 << "\n\nExpected:\n"
487 << HexDumpWithMarks(expected, expected_len, marks.get(), max_len)
488 << "\nActual:\n"
489 << HexDumpWithMarks(actual, actual_len, marks.get(), max_len);
490}
491
[email protected]b12764d2013-12-02 22:28:30492bool DecodeHexString(const base::StringPiece& hex, std::string* bytes) {
493 bytes->clear();
494 if (hex.empty())
495 return true;
496 std::vector<uint8> v;
497 if (!base::HexStringToBytes(hex.as_string(), &v))
498 return false;
499 if (!v.empty())
500 bytes->assign(reinterpret_cast<const char*>(&v[0]), v.size());
501 return true;
502}
503
[email protected]d3d15bf2013-01-30 02:51:54504static QuicPacket* ConstructPacketFromHandshakeMessage(
[email protected]3aa9ca72014-02-27 19:39:43505 QuicConnectionId connection_id,
[email protected]14e8106c2013-03-14 16:25:33506 const CryptoHandshakeMessage& message,
507 bool should_include_version) {
[email protected]8b37a092012-10-18 21:53:49508 CryptoFramer crypto_framer;
[email protected]dc2cc742012-10-21 13:56:13509 scoped_ptr<QuicData> data(crypto_framer.ConstructHandshakeMessage(message));
[email protected]b007e632013-10-28 08:39:25510 QuicFramer quic_framer(QuicSupportedVersions(), QuicTime::Zero(), false);
[email protected]8b37a092012-10-18 21:53:49511
512 QuicPacketHeader header;
[email protected]3aa9ca72014-02-27 19:39:43513 header.public_header.connection_id = connection_id;
[email protected]9db443912013-02-25 05:27:03514 header.public_header.reset_flag = false;
[email protected]14e8106c2013-03-14 16:25:33515 header.public_header.version_flag = should_include_version;
[email protected]8b37a092012-10-18 21:53:49516 header.packet_sequence_number = 1;
[email protected]9db443912013-02-25 05:27:03517 header.entropy_flag = false;
518 header.entropy_hash = 0;
519 header.fec_flag = false;
[email protected]8b37a092012-10-18 21:53:49520 header.fec_group = 0;
521
[email protected]be24ab22012-10-22 03:01:52522 QuicStreamFrame stream_frame(kCryptoStreamId, false, 0,
[email protected]5dafdb62013-11-14 01:24:26523 MakeIOVector(data->AsStringPiece()));
[email protected]8b37a092012-10-18 21:53:49524
[email protected]be24ab22012-10-22 03:01:52525 QuicFrame frame(&stream_frame);
526 QuicFrames frames;
527 frames.push_back(frame);
[email protected]9cda5fd2014-06-03 10:20:28528 return BuildUnsizedDataPacket(&quic_framer, header, frames).packet;
[email protected]8b37a092012-10-18 21:53:49529}
530
[email protected]3aa9ca72014-02-27 19:39:43531QuicPacket* ConstructHandshakePacket(QuicConnectionId connection_id,
532 QuicTag tag) {
[email protected]d3d15bf2013-01-30 02:51:54533 CryptoHandshakeMessage message;
[email protected]ccc66e8a2013-03-26 08:26:14534 message.set_tag(tag);
[email protected]3aa9ca72014-02-27 19:39:43535 return ConstructPacketFromHandshakeMessage(connection_id, message, false);
[email protected]d3d15bf2013-01-30 02:51:54536}
537
[email protected]ea825e02013-08-21 18:12:45538size_t GetPacketLengthForOneStream(
539 QuicVersion version,
540 bool include_version,
541 QuicSequenceNumberLength sequence_number_length,
542 InFecGroup is_in_fec_group,
543 size_t* payload_length) {
[email protected]f62262b2013-07-05 20:57:30544 *payload_length = 1;
545 const size_t stream_length =
[email protected]5dafdb62013-11-14 01:24:26546 NullEncrypter().GetCiphertextSize(*payload_length) +
[email protected]b064310782013-05-30 21:12:17547 QuicPacketCreator::StreamFramePacketOverhead(
[email protected]3aa9ca72014-02-27 19:39:43548 version, PACKET_8BYTE_CONNECTION_ID, include_version,
[email protected]aa7e4ef2014-05-28 03:53:15549 sequence_number_length, 0u, is_in_fec_group);
[email protected]5dafdb62013-11-14 01:24:26550 const size_t ack_length = NullEncrypter().GetCiphertextSize(
[email protected]8e01c062013-10-31 07:35:31551 QuicFramer::GetMinAckFrameSize(
552 version, sequence_number_length, PACKET_1BYTE_SEQUENCE_NUMBER)) +
[email protected]3aa9ca72014-02-27 19:39:43553 GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID, include_version,
[email protected]ea825e02013-08-21 18:12:45554 sequence_number_length, is_in_fec_group);
[email protected]f62262b2013-07-05 20:57:30555 if (stream_length < ack_length) {
556 *payload_length = 1 + ack_length - stream_length;
557 }
558
[email protected]5dafdb62013-11-14 01:24:26559 return NullEncrypter().GetCiphertextSize(*payload_length) +
[email protected]f62262b2013-07-05 20:57:30560 QuicPacketCreator::StreamFramePacketOverhead(
[email protected]3aa9ca72014-02-27 19:39:43561 version, PACKET_8BYTE_CONNECTION_ID, include_version,
[email protected]aa7e4ef2014-05-28 03:53:15562 sequence_number_length, 0u, is_in_fec_group);
[email protected]5351cc4b2013-03-03 07:22:41563}
564
[email protected]8e01c062013-10-31 07:35:31565TestEntropyCalculator::TestEntropyCalculator() { }
566
567TestEntropyCalculator::~TestEntropyCalculator() { }
568
[email protected]48878092013-07-26 14:51:56569QuicPacketEntropyHash TestEntropyCalculator::EntropyHash(
[email protected]9db443912013-02-25 05:27:03570 QuicPacketSequenceNumber sequence_number) const {
571 return 1u;
572}
573
[email protected]8e01c062013-10-31 07:35:31574MockEntropyCalculator::MockEntropyCalculator() { }
575
576MockEntropyCalculator::~MockEntropyCalculator() { }
577
[email protected]b064310782013-05-30 21:12:17578QuicConfig DefaultQuicConfig() {
579 QuicConfig config;
580 config.SetDefaults();
581 return config;
582}
583
[email protected]4d640792013-12-18 22:21:08584QuicVersionVector SupportedVersions(QuicVersion version) {
585 QuicVersionVector versions;
586 versions.push_back(version);
587 return versions;
588}
589
[email protected]8b37a092012-10-18 21:53:49590} // namespace test
[email protected]8b37a092012-10-18 21:53:49591} // namespace net