blob: 7d24b2f3f9be7b3bcac6b5a52d042880215feaea [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
45MockFramerVisitor::MockFramerVisitor() {
46 // By default, we want to accept packets.
[email protected]14e8106c2013-03-14 16:25:3347 ON_CALL(*this, OnProtocolVersionMismatch(_))
48 .WillByDefault(testing::Return(false));
49
50 // By default, we want to accept packets.
[email protected]ec86d5462013-11-17 16:04:4951 ON_CALL(*this, OnUnauthenticatedHeader(_))
52 .WillByDefault(testing::Return(true));
53
[email protected]066d8182014-01-04 02:02:4554 ON_CALL(*this, OnUnauthenticatedPublicHeader(_))
55 .WillByDefault(testing::Return(true));
56
[email protected]cff7b7b2013-01-11 08:49:0757 ON_CALL(*this, OnPacketHeader(_))
[email protected]8b37a092012-10-18 21:53:4958 .WillByDefault(testing::Return(true));
[email protected]a57e0272013-04-26 07:31:4759
60 ON_CALL(*this, OnStreamFrame(_))
61 .WillByDefault(testing::Return(true));
62
63 ON_CALL(*this, OnAckFrame(_))
64 .WillByDefault(testing::Return(true));
65
66 ON_CALL(*this, OnCongestionFeedbackFrame(_))
67 .WillByDefault(testing::Return(true));
68
[email protected]93dd91f2014-02-27 00:09:0369 ON_CALL(*this, OnStopWaitingFrame(_))
70 .WillByDefault(testing::Return(true));
71
[email protected]a57e0272013-04-26 07:31:4772 ON_CALL(*this, OnRstStreamFrame(_))
73 .WillByDefault(testing::Return(true));
74
75 ON_CALL(*this, OnConnectionCloseFrame(_))
76 .WillByDefault(testing::Return(true));
77
78 ON_CALL(*this, OnGoAwayFrame(_))
79 .WillByDefault(testing::Return(true));
[email protected]8b37a092012-10-18 21:53:4980}
81
[email protected]044ac2b2012-11-13 21:41:0682MockFramerVisitor::~MockFramerVisitor() {
83}
[email protected]8b37a092012-10-18 21:53:4984
[email protected]48878092013-07-26 14:51:5685bool NoOpFramerVisitor::OnProtocolVersionMismatch(QuicVersion version) {
[email protected]14e8106c2013-03-14 16:25:3386 return false;
87}
88
[email protected]066d8182014-01-04 02:02:4589bool NoOpFramerVisitor::OnUnauthenticatedPublicHeader(
90 const QuicPacketPublicHeader& header) {
91 return true;
92}
93
[email protected]ec86d5462013-11-17 16:04:4994bool NoOpFramerVisitor::OnUnauthenticatedHeader(
95 const QuicPacketHeader& header) {
96 return true;
97}
98
[email protected]8b37a092012-10-18 21:53:4999bool NoOpFramerVisitor::OnPacketHeader(const QuicPacketHeader& header) {
100 return true;
101}
102
[email protected]a57e0272013-04-26 07:31:47103bool NoOpFramerVisitor::OnStreamFrame(const QuicStreamFrame& frame) {
104 return true;
105}
106
107bool NoOpFramerVisitor::OnAckFrame(const QuicAckFrame& frame) {
108 return true;
109}
110
111bool NoOpFramerVisitor::OnCongestionFeedbackFrame(
112 const QuicCongestionFeedbackFrame& frame) {
113 return true;
114}
115
[email protected]93dd91f2014-02-27 00:09:03116bool NoOpFramerVisitor::OnStopWaitingFrame(
117 const QuicStopWaitingFrame& frame) {
118 return true;
119}
120
[email protected]a57e0272013-04-26 07:31:47121bool NoOpFramerVisitor::OnRstStreamFrame(
122 const QuicRstStreamFrame& frame) {
123 return true;
124}
125
126bool NoOpFramerVisitor::OnConnectionCloseFrame(
127 const QuicConnectionCloseFrame& frame) {
128 return true;
129}
130
131bool NoOpFramerVisitor::OnGoAwayFrame(const QuicGoAwayFrame& frame) {
132 return true;
133}
134
[email protected]cb23a922014-02-20 17:42:38135bool NoOpFramerVisitor::OnWindowUpdateFrame(
136 const QuicWindowUpdateFrame& frame) {
137 return true;
138}
139
140bool NoOpFramerVisitor::OnBlockedFrame(const QuicBlockedFrame& frame) {
141 return true;
142}
143
[email protected]9db443912013-02-25 05:27:03144FramerVisitorCapturingFrames::FramerVisitorCapturingFrames() : frame_count_(0) {
[email protected]134e5c32012-12-12 19:20:36145}
146
[email protected]9db443912013-02-25 05:27:03147FramerVisitorCapturingFrames::~FramerVisitorCapturingFrames() {
[email protected]0ac0c5b2013-11-20 05:55:58148 Reset();
149}
150
151void FramerVisitorCapturingFrames::Reset() {
[email protected]583bcbcf2013-10-28 01:51:15152 STLDeleteElements(&stream_data_);
[email protected]0ac0c5b2013-11-20 05:55:58153 stream_frames_.clear();
154 frame_count_ = 0;
155 ack_.reset();
156 feedback_.reset();
157 rst_.reset();
158 close_.reset();
159 goaway_.reset();
160 version_negotiation_packet_.reset();
[email protected]26f3f8e2012-12-13 21:07:19161}
162
[email protected]9db443912013-02-25 05:27:03163bool FramerVisitorCapturingFrames::OnPacketHeader(
[email protected]4e6f0ed2012-11-02 22:15:38164 const QuicPacketHeader& header) {
165 header_ = header;
[email protected]9db443912013-02-25 05:27:03166 frame_count_ = 0;
[email protected]4e6f0ed2012-11-02 22:15:38167 return true;
168}
169
[email protected]a57e0272013-04-26 07:31:47170bool FramerVisitorCapturingFrames::OnStreamFrame(const QuicStreamFrame& frame) {
[email protected]583bcbcf2013-10-28 01:51:15171 // Make a copy of the frame and store a copy of underlying string, since
172 // frame.data may not exist outside this callback.
[email protected]5dafdb62013-11-14 01:24:26173 stream_data_.push_back(frame.GetDataAsString());
[email protected]583bcbcf2013-10-28 01:51:15174 QuicStreamFrame frame_copy = frame;
[email protected]5dafdb62013-11-14 01:24:26175 frame_copy.data.Clear();
176 frame_copy.data.Append(const_cast<char*>(stream_data_.back()->data()),
177 stream_data_.back()->size());
[email protected]583bcbcf2013-10-28 01:51:15178 stream_frames_.push_back(frame_copy);
[email protected]9db443912013-02-25 05:27:03179 ++frame_count_;
[email protected]a57e0272013-04-26 07:31:47180 return true;
[email protected]26f3f8e2012-12-13 21:07:19181}
182
[email protected]a57e0272013-04-26 07:31:47183bool FramerVisitorCapturingFrames::OnAckFrame(const QuicAckFrame& frame) {
[email protected]9db443912013-02-25 05:27:03184 ack_.reset(new QuicAckFrame(frame));
185 ++frame_count_;
[email protected]a57e0272013-04-26 07:31:47186 return true;
[email protected]9db443912013-02-25 05:27:03187}
188
[email protected]a57e0272013-04-26 07:31:47189bool FramerVisitorCapturingFrames::OnCongestionFeedbackFrame(
[email protected]26f3f8e2012-12-13 21:07:19190 const QuicCongestionFeedbackFrame& frame) {
191 feedback_.reset(new QuicCongestionFeedbackFrame(frame));
[email protected]9db443912013-02-25 05:27:03192 ++frame_count_;
[email protected]a57e0272013-04-26 07:31:47193 return true;
[email protected]9db443912013-02-25 05:27:03194}
195
[email protected]93dd91f2014-02-27 00:09:03196bool FramerVisitorCapturingFrames::OnStopWaitingFrame(
197 const QuicStopWaitingFrame& frame) {
198 stop_waiting_.reset(new QuicStopWaitingFrame(frame));
199 ++frame_count_;
200 return true;
201}
202
[email protected]a57e0272013-04-26 07:31:47203bool FramerVisitorCapturingFrames::OnRstStreamFrame(
[email protected]9db443912013-02-25 05:27:03204 const QuicRstStreamFrame& frame) {
205 rst_.reset(new QuicRstStreamFrame(frame));
206 ++frame_count_;
[email protected]a57e0272013-04-26 07:31:47207 return true;
[email protected]9db443912013-02-25 05:27:03208}
209
[email protected]a57e0272013-04-26 07:31:47210bool FramerVisitorCapturingFrames::OnConnectionCloseFrame(
[email protected]9db443912013-02-25 05:27:03211 const QuicConnectionCloseFrame& frame) {
212 close_.reset(new QuicConnectionCloseFrame(frame));
213 ++frame_count_;
[email protected]a57e0272013-04-26 07:31:47214 return true;
[email protected]9db443912013-02-25 05:27:03215}
216
[email protected]a57e0272013-04-26 07:31:47217bool FramerVisitorCapturingFrames::OnGoAwayFrame(const QuicGoAwayFrame& frame) {
[email protected]9db443912013-02-25 05:27:03218 goaway_.reset(new QuicGoAwayFrame(frame));
219 ++frame_count_;
[email protected]a57e0272013-04-26 07:31:47220 return true;
[email protected]4e6f0ed2012-11-02 22:15:38221}
222
[email protected]14e8106c2013-03-14 16:25:33223void FramerVisitorCapturingFrames::OnVersionNegotiationPacket(
224 const QuicVersionNegotiationPacket& packet) {
225 version_negotiation_packet_.reset(new QuicVersionNegotiationPacket(packet));
226 frame_count_ = 0;
227}
228
[email protected]fee17f72013-02-03 07:47:41229FramerVisitorCapturingPublicReset::FramerVisitorCapturingPublicReset() {
230}
231
232FramerVisitorCapturingPublicReset::~FramerVisitorCapturingPublicReset() {
233}
234
235void FramerVisitorCapturingPublicReset::OnPublicResetPacket(
236 const QuicPublicResetPacket& public_reset) {
237 public_reset_packet_ = public_reset;
238}
239
[email protected]8d659e22013-01-19 04:26:10240MockConnectionVisitor::MockConnectionVisitor() {
241}
242
243MockConnectionVisitor::~MockConnectionVisitor() {
244}
245
[email protected]9c0b1352012-11-04 00:03:27246MockHelper::MockHelper() {
247}
248
249MockHelper::~MockHelper() {
250}
251
[email protected]97693d12012-11-16 16:05:00252const QuicClock* MockHelper::GetClock() const {
[email protected]9c0b1352012-11-04 00:03:27253 return &clock_;
254}
255
[email protected]9558c5d32012-12-22 00:08:14256QuicRandom* MockHelper::GetRandomGenerator() {
257 return &random_generator_;
258}
259
[email protected]965dbe62013-08-09 21:34:31260QuicAlarm* MockHelper::CreateAlarm(QuicAlarm::Delegate* delegate) {
261 return new TestAlarm(delegate);
262}
263
[email protected]fe053f92013-04-23 20:18:55264void MockHelper::AdvanceTime(QuicTime::Delta delta) {
265 clock_.AdvanceTime(delta);
266}
267
[email protected]c05a6d222013-12-16 19:42:03268MockConnection::MockConnection(bool is_server)
[email protected]3aa9ca72014-02-27 19:39:43269 : QuicConnection(kTestConnectionId,
[email protected]300ccd52014-01-25 08:00:19270 IPEndPoint(TestPeerIPAddress(), kTestPort),
[email protected]c05a6d222013-12-16 19:42:03271 new testing::NiceMock<MockHelper>(),
[email protected]cbd731e2013-10-24 00:20:39272 new testing::NiceMock<MockPacketWriter>(),
[email protected]b007e632013-10-28 08:39:25273 is_server, QuicSupportedVersions()),
[email protected]c05a6d222013-12-16 19:42:03274 writer_(QuicConnectionPeer::GetWriter(this)),
275 helper_(helper()) {
276}
277
278MockConnection::MockConnection(IPEndPoint address,
279 bool is_server)
[email protected]3aa9ca72014-02-27 19:39:43280 : QuicConnection(kTestConnectionId, address,
[email protected]c05a6d222013-12-16 19:42:03281 new testing::NiceMock<MockHelper>(),
282 new testing::NiceMock<MockPacketWriter>(),
283 is_server, QuicSupportedVersions()),
[email protected]2cfc6bb82013-10-27 03:40:44284 writer_(QuicConnectionPeer::GetWriter(this)),
285 helper_(helper()) {
[email protected]044ac2b2012-11-13 21:41:06286}
287
[email protected]3aa9ca72014-02-27 19:39:43288MockConnection::MockConnection(QuicConnectionId connection_id,
[email protected]14e8106c2013-03-14 16:25:33289 bool is_server)
[email protected]3aa9ca72014-02-27 19:39:43290 : QuicConnection(connection_id,
291 IPEndPoint(TestPeerIPAddress(), kTestPort),
[email protected]c05a6d222013-12-16 19:42:03292 new testing::NiceMock<MockHelper>(),
293 new testing::NiceMock<MockPacketWriter>(),
294 is_server, QuicSupportedVersions()),
295 writer_(QuicConnectionPeer::GetWriter(this)),
296 helper_(helper()) {
[email protected]872edd9e2013-01-16 08:51:15297}
298
[email protected]4d640792013-12-18 22:21:08299MockConnection::MockConnection(bool is_server,
300 const QuicVersionVector& supported_versions)
[email protected]3aa9ca72014-02-27 19:39:43301 : QuicConnection(kTestConnectionId,
[email protected]300ccd52014-01-25 08:00:19302 IPEndPoint(TestPeerIPAddress(), kTestPort),
[email protected]4d640792013-12-18 22:21:08303 new testing::NiceMock<MockHelper>(),
304 new testing::NiceMock<MockPacketWriter>(),
305 is_server, supported_versions),
306 writer_(QuicConnectionPeer::GetWriter(this)),
307 helper_(helper()) {
308}
309
[email protected]044ac2b2012-11-13 21:41:06310MockConnection::~MockConnection() {
311}
312
[email protected]fe053f92013-04-23 20:18:55313void MockConnection::AdvanceTime(QuicTime::Delta delta) {
[email protected]fe053f92013-04-23 20:18:55314 static_cast<MockHelper*>(helper())->AdvanceTime(delta);
315}
316
[email protected]c05a6d222013-12-16 19:42:03317PacketSavingConnection::PacketSavingConnection(bool is_server)
318 : MockConnection(is_server) {
[email protected]044ac2b2012-11-13 21:41:06319}
320
[email protected]4d640792013-12-18 22:21:08321PacketSavingConnection::PacketSavingConnection(
322 bool is_server,
323 const QuicVersionVector& supported_versions)
324 : MockConnection(is_server, supported_versions) {
325}
326
[email protected]044ac2b2012-11-13 21:41:06327PacketSavingConnection::~PacketSavingConnection() {
[email protected]701bc892013-01-17 04:51:54328 STLDeleteElements(&packets_);
[email protected]2532de12013-05-09 12:29:33329 STLDeleteElements(&encrypted_packets_);
[email protected]044ac2b2012-11-13 21:41:06330}
331
[email protected]fee17f72013-02-03 07:47:41332bool PacketSavingConnection::SendOrQueuePacket(
[email protected]8ba81212013-05-03 13:11:48333 EncryptionLevel level,
[email protected]ec86d5462013-11-17 16:04:49334 const SerializedPacket& packet,
335 TransmissionType transmission_type) {
336 packets_.push_back(packet.packet);
[email protected]c5e1aca2014-01-30 04:03:04337 QuicEncryptedPacket* encrypted = QuicConnectionPeer::GetFramer(this)->
338 EncryptPacket(level, packet.sequence_number, *packet.packet);
[email protected]2532de12013-05-09 12:29:33339 encrypted_packets_.push_back(encrypted);
[email protected]044ac2b2012-11-13 21:41:06340 return true;
341}
342
[email protected]c05a6d222013-12-16 19:42:03343MockSession::MockSession(QuicConnection* connection)
344 : QuicSession(connection, DefaultQuicConfig()) {
[email protected]93dd91f2014-02-27 00:09:03345 ON_CALL(*this, WritevData(_, _, _, _, _))
[email protected]cff7b7b2013-01-11 08:49:07346 .WillByDefault(testing::Return(QuicConsumedData(0, false)));
[email protected]044ac2b2012-11-13 21:41:06347}
348
349MockSession::~MockSession() {
350}
351
[email protected]899951652013-05-16 12:52:39352TestSession::TestSession(QuicConnection* connection,
[email protected]c05a6d222013-12-16 19:42:03353 const QuicConfig& config)
354 : QuicSession(connection, config),
[email protected]2532de12013-05-09 12:29:33355 crypto_stream_(NULL) {
356}
357
[email protected]b064310782013-05-30 21:12:17358TestSession::~TestSession() {}
[email protected]2532de12013-05-09 12:29:33359
360void TestSession::SetCryptoStream(QuicCryptoStream* stream) {
361 crypto_stream_ = stream;
362}
363
364QuicCryptoStream* TestSession::GetCryptoStream() {
365 return crypto_stream_;
366}
367
[email protected]90f62f092014-03-24 02:41:23368TestClientSession::TestClientSession(QuicConnection* connection,
369 const QuicConfig& config)
370 : QuicClientSessionBase(connection, config),
371 crypto_stream_(NULL) {
[email protected]64c12232014-03-26 05:43:59372 EXPECT_CALL(*this, OnProofValid(_)).Times(AnyNumber());
[email protected]90f62f092014-03-24 02:41:23373}
374
375TestClientSession::~TestClientSession() {}
376
377void TestClientSession::SetCryptoStream(QuicCryptoStream* stream) {
378 crypto_stream_ = stream;
379}
380
381QuicCryptoStream* TestClientSession::GetCryptoStream() {
382 return crypto_stream_;
383}
384
[email protected]cbd731e2013-10-24 00:20:39385MockPacketWriter::MockPacketWriter() {
386}
387
388MockPacketWriter::~MockPacketWriter() {
389}
390
[email protected]fee17f72013-02-03 07:47:41391MockSendAlgorithm::MockSendAlgorithm() {
[email protected]8d659e22013-01-19 04:26:10392}
393
[email protected]fee17f72013-02-03 07:47:41394MockSendAlgorithm::~MockSendAlgorithm() {
[email protected]8d659e22013-01-19 04:26:10395}
396
[email protected]3aa9ca72014-02-27 19:39:43397MockLossAlgorithm::MockLossAlgorithm() {
398}
399
400MockLossAlgorithm::~MockLossAlgorithm() {
401}
402
[email protected]97cf3022013-09-05 14:30:16403MockAckNotifierDelegate::MockAckNotifierDelegate() {
404}
405
406MockAckNotifierDelegate::~MockAckNotifierDelegate() {
407}
408
[email protected]8b37a092012-10-18 21:53:49409namespace {
410
411string HexDumpWithMarks(const char* data, int length,
412 const bool* marks, int mark_length) {
413 static const char kHexChars[] = "0123456789abcdef";
414 static const int kColumns = 4;
415
416 const int kSizeLimit = 1024;
417 if (length > kSizeLimit || mark_length > kSizeLimit) {
418 LOG(ERROR) << "Only dumping first " << kSizeLimit << " bytes.";
419 length = min(length, kSizeLimit);
420 mark_length = min(mark_length, kSizeLimit);
421 }
422
423 string hex;
424 for (const char* row = data; length > 0;
425 row += kColumns, length -= kColumns) {
426 for (const char *p = row; p < row + 4; ++p) {
427 if (p < row + length) {
428 const bool mark =
429 (marks && (p - data) < mark_length && marks[p - data]);
430 hex += mark ? '*' : ' ';
431 hex += kHexChars[(*p & 0xf0) >> 4];
432 hex += kHexChars[*p & 0x0f];
433 hex += mark ? '*' : ' ';
434 } else {
435 hex += " ";
436 }
437 }
438 hex = hex + " ";
439
440 for (const char *p = row; p < row + 4 && p < row + length; ++p)
441 hex += (*p >= 0x20 && *p <= 0x7f) ? (*p) : '.';
442
443 hex = hex + '\n';
444 }
445 return hex;
446}
447
448} // namespace
449
[email protected]300ccd52014-01-25 08:00:19450IPAddressNumber TestPeerIPAddress() { return Loopback4(); }
451
[email protected]b007e632013-10-28 08:39:25452QuicVersion QuicVersionMax() { return QuicSupportedVersions().front(); }
453
454QuicVersion QuicVersionMin() { return QuicSupportedVersions().back(); }
455
[email protected]c05a6d222013-12-16 19:42:03456IPAddressNumber Loopback4() {
[email protected]300ccd52014-01-25 08:00:19457 IPAddressNumber addr;
458 CHECK(ParseIPLiteralToNumber("127.0.0.1", &addr));
[email protected]c05a6d222013-12-16 19:42:03459 return addr;
460}
461
[email protected]ffc34bf2014-03-07 02:42:02462QuicEncryptedPacket* ConstructEncryptedPacket(
463 QuicConnectionId connection_id,
464 bool version_flag,
465 bool reset_flag,
466 QuicPacketSequenceNumber sequence_number,
467 const string& data) {
468 QuicPacketHeader header;
469 header.public_header.connection_id = connection_id;
470 header.public_header.connection_id_length = PACKET_8BYTE_CONNECTION_ID;
471 header.public_header.version_flag = version_flag;
472 header.public_header.reset_flag = reset_flag;
473 header.public_header.sequence_number_length = PACKET_6BYTE_SEQUENCE_NUMBER;
474 header.packet_sequence_number = sequence_number;
475 header.entropy_flag = false;
476 header.entropy_hash = 0;
477 header.fec_flag = false;
478 header.is_in_fec_group = NOT_IN_FEC_GROUP;
479 header.fec_group = 0;
480 QuicStreamFrame stream_frame(1, false, 0, MakeIOVector(data));
481 QuicFrame frame(&stream_frame);
482 QuicFrames frames;
483 frames.push_back(frame);
484 QuicFramer framer(QuicSupportedVersions(), QuicTime::Zero(), false);
485 scoped_ptr<QuicPacket> packet(
486 framer.BuildUnsizedDataPacket(header, frames).packet);
487 EXPECT_TRUE(packet != NULL);
488 QuicEncryptedPacket* encrypted = framer.EncryptPacket(ENCRYPTION_NONE,
489 sequence_number,
490 *packet);
491 EXPECT_TRUE(encrypted != NULL);
492 return encrypted;
493}
494
[email protected]8b37a092012-10-18 21:53:49495void CompareCharArraysWithHexError(
496 const string& description,
497 const char* actual,
498 const int actual_len,
499 const char* expected,
500 const int expected_len) {
[email protected]b007e632013-10-28 08:39:25501 EXPECT_EQ(actual_len, expected_len);
[email protected]8b37a092012-10-18 21:53:49502 const int min_len = min(actual_len, expected_len);
503 const int max_len = max(actual_len, expected_len);
[email protected]4356f0f2013-04-07 00:58:17504 scoped_ptr<bool[]> marks(new bool[max_len]);
[email protected]8b37a092012-10-18 21:53:49505 bool identical = (actual_len == expected_len);
506 for (int i = 0; i < min_len; ++i) {
507 if (actual[i] != expected[i]) {
508 marks[i] = true;
509 identical = false;
510 } else {
511 marks[i] = false;
512 }
513 }
514 for (int i = min_len; i < max_len; ++i) {
515 marks[i] = true;
516 }
517 if (identical) return;
518 ADD_FAILURE()
519 << "Description:\n"
520 << description
521 << "\n\nExpected:\n"
522 << HexDumpWithMarks(expected, expected_len, marks.get(), max_len)
523 << "\nActual:\n"
524 << HexDumpWithMarks(actual, actual_len, marks.get(), max_len);
525}
526
[email protected]b12764d2013-12-02 22:28:30527bool DecodeHexString(const base::StringPiece& hex, std::string* bytes) {
528 bytes->clear();
529 if (hex.empty())
530 return true;
531 std::vector<uint8> v;
532 if (!base::HexStringToBytes(hex.as_string(), &v))
533 return false;
534 if (!v.empty())
535 bytes->assign(reinterpret_cast<const char*>(&v[0]), v.size());
536 return true;
537}
538
[email protected]d3d15bf2013-01-30 02:51:54539static QuicPacket* ConstructPacketFromHandshakeMessage(
[email protected]3aa9ca72014-02-27 19:39:43540 QuicConnectionId connection_id,
[email protected]14e8106c2013-03-14 16:25:33541 const CryptoHandshakeMessage& message,
542 bool should_include_version) {
[email protected]8b37a092012-10-18 21:53:49543 CryptoFramer crypto_framer;
[email protected]dc2cc742012-10-21 13:56:13544 scoped_ptr<QuicData> data(crypto_framer.ConstructHandshakeMessage(message));
[email protected]b007e632013-10-28 08:39:25545 QuicFramer quic_framer(QuicSupportedVersions(), QuicTime::Zero(), false);
[email protected]8b37a092012-10-18 21:53:49546
547 QuicPacketHeader header;
[email protected]3aa9ca72014-02-27 19:39:43548 header.public_header.connection_id = connection_id;
[email protected]9db443912013-02-25 05:27:03549 header.public_header.reset_flag = false;
[email protected]14e8106c2013-03-14 16:25:33550 header.public_header.version_flag = should_include_version;
[email protected]8b37a092012-10-18 21:53:49551 header.packet_sequence_number = 1;
[email protected]9db443912013-02-25 05:27:03552 header.entropy_flag = false;
553 header.entropy_hash = 0;
554 header.fec_flag = false;
[email protected]8b37a092012-10-18 21:53:49555 header.fec_group = 0;
556
[email protected]be24ab22012-10-22 03:01:52557 QuicStreamFrame stream_frame(kCryptoStreamId, false, 0,
[email protected]5dafdb62013-11-14 01:24:26558 MakeIOVector(data->AsStringPiece()));
[email protected]8b37a092012-10-18 21:53:49559
[email protected]be24ab22012-10-22 03:01:52560 QuicFrame frame(&stream_frame);
561 QuicFrames frames;
562 frames.push_back(frame);
[email protected]3e60db82013-08-05 19:43:06563 return quic_framer.BuildUnsizedDataPacket(header, frames).packet;
[email protected]8b37a092012-10-18 21:53:49564}
565
[email protected]3aa9ca72014-02-27 19:39:43566QuicPacket* ConstructHandshakePacket(QuicConnectionId connection_id,
567 QuicTag tag) {
[email protected]d3d15bf2013-01-30 02:51:54568 CryptoHandshakeMessage message;
[email protected]ccc66e8a2013-03-26 08:26:14569 message.set_tag(tag);
[email protected]3aa9ca72014-02-27 19:39:43570 return ConstructPacketFromHandshakeMessage(connection_id, message, false);
[email protected]d3d15bf2013-01-30 02:51:54571}
572
[email protected]ea825e02013-08-21 18:12:45573size_t GetPacketLengthForOneStream(
574 QuicVersion version,
575 bool include_version,
576 QuicSequenceNumberLength sequence_number_length,
577 InFecGroup is_in_fec_group,
578 size_t* payload_length) {
[email protected]f62262b2013-07-05 20:57:30579 *payload_length = 1;
580 const size_t stream_length =
[email protected]5dafdb62013-11-14 01:24:26581 NullEncrypter().GetCiphertextSize(*payload_length) +
[email protected]b064310782013-05-30 21:12:17582 QuicPacketCreator::StreamFramePacketOverhead(
[email protected]3aa9ca72014-02-27 19:39:43583 version, PACKET_8BYTE_CONNECTION_ID, include_version,
[email protected]ea825e02013-08-21 18:12:45584 sequence_number_length, is_in_fec_group);
[email protected]5dafdb62013-11-14 01:24:26585 const size_t ack_length = NullEncrypter().GetCiphertextSize(
[email protected]8e01c062013-10-31 07:35:31586 QuicFramer::GetMinAckFrameSize(
587 version, sequence_number_length, PACKET_1BYTE_SEQUENCE_NUMBER)) +
[email protected]3aa9ca72014-02-27 19:39:43588 GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID, include_version,
[email protected]ea825e02013-08-21 18:12:45589 sequence_number_length, is_in_fec_group);
[email protected]f62262b2013-07-05 20:57:30590 if (stream_length < ack_length) {
591 *payload_length = 1 + ack_length - stream_length;
592 }
593
[email protected]5dafdb62013-11-14 01:24:26594 return NullEncrypter().GetCiphertextSize(*payload_length) +
[email protected]f62262b2013-07-05 20:57:30595 QuicPacketCreator::StreamFramePacketOverhead(
[email protected]3aa9ca72014-02-27 19:39:43596 version, PACKET_8BYTE_CONNECTION_ID, include_version,
[email protected]ea825e02013-08-21 18:12:45597 sequence_number_length, is_in_fec_group);
[email protected]5351cc4b2013-03-03 07:22:41598}
599
[email protected]8e01c062013-10-31 07:35:31600TestEntropyCalculator::TestEntropyCalculator() { }
601
602TestEntropyCalculator::~TestEntropyCalculator() { }
603
[email protected]48878092013-07-26 14:51:56604QuicPacketEntropyHash TestEntropyCalculator::EntropyHash(
[email protected]9db443912013-02-25 05:27:03605 QuicPacketSequenceNumber sequence_number) const {
606 return 1u;
607}
608
[email protected]8e01c062013-10-31 07:35:31609MockEntropyCalculator::MockEntropyCalculator() { }
610
611MockEntropyCalculator::~MockEntropyCalculator() { }
612
[email protected]b064310782013-05-30 21:12:17613QuicConfig DefaultQuicConfig() {
614 QuicConfig config;
615 config.SetDefaults();
616 return config;
617}
618
[email protected]4d640792013-12-18 22:21:08619QuicVersionVector SupportedVersions(QuicVersion version) {
620 QuicVersionVector versions;
621 versions.push_back(version);
622 return versions;
623}
624
[email protected]8b37a092012-10-18 21:53:49625} // namespace test
[email protected]8b37a092012-10-18 21:53:49626} // namespace net