blob: fbba0042ae3aa69af0c73cb1761c74adf9680987 [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]9bb57c72014-03-31 20:36:04273 is_server, QuicSupportedVersions(),
274 kInitialFlowControlWindowForTest),
[email protected]c05a6d222013-12-16 19:42:03275 writer_(QuicConnectionPeer::GetWriter(this)),
276 helper_(helper()) {
277}
278
279MockConnection::MockConnection(IPEndPoint address,
280 bool is_server)
[email protected]3aa9ca72014-02-27 19:39:43281 : QuicConnection(kTestConnectionId, address,
[email protected]c05a6d222013-12-16 19:42:03282 new testing::NiceMock<MockHelper>(),
283 new testing::NiceMock<MockPacketWriter>(),
[email protected]9bb57c72014-03-31 20:36:04284 is_server, QuicSupportedVersions(),
285 kInitialFlowControlWindowForTest),
[email protected]2cfc6bb82013-10-27 03:40:44286 writer_(QuicConnectionPeer::GetWriter(this)),
287 helper_(helper()) {
[email protected]044ac2b2012-11-13 21:41:06288}
289
[email protected]3aa9ca72014-02-27 19:39:43290MockConnection::MockConnection(QuicConnectionId connection_id,
[email protected]14e8106c2013-03-14 16:25:33291 bool is_server)
[email protected]3aa9ca72014-02-27 19:39:43292 : QuicConnection(connection_id,
293 IPEndPoint(TestPeerIPAddress(), kTestPort),
[email protected]c05a6d222013-12-16 19:42:03294 new testing::NiceMock<MockHelper>(),
295 new testing::NiceMock<MockPacketWriter>(),
[email protected]9bb57c72014-03-31 20:36:04296 is_server, QuicSupportedVersions(),
297 kInitialFlowControlWindowForTest),
[email protected]c05a6d222013-12-16 19:42:03298 writer_(QuicConnectionPeer::GetWriter(this)),
299 helper_(helper()) {
[email protected]872edd9e2013-01-16 08:51:15300}
301
[email protected]4d640792013-12-18 22:21:08302MockConnection::MockConnection(bool is_server,
303 const QuicVersionVector& supported_versions)
[email protected]3aa9ca72014-02-27 19:39:43304 : QuicConnection(kTestConnectionId,
[email protected]300ccd52014-01-25 08:00:19305 IPEndPoint(TestPeerIPAddress(), kTestPort),
[email protected]4d640792013-12-18 22:21:08306 new testing::NiceMock<MockHelper>(),
307 new testing::NiceMock<MockPacketWriter>(),
[email protected]9bb57c72014-03-31 20:36:04308 is_server, supported_versions,
309 kInitialFlowControlWindowForTest),
[email protected]4d640792013-12-18 22:21:08310 writer_(QuicConnectionPeer::GetWriter(this)),
311 helper_(helper()) {
312}
313
[email protected]044ac2b2012-11-13 21:41:06314MockConnection::~MockConnection() {
315}
316
[email protected]fe053f92013-04-23 20:18:55317void MockConnection::AdvanceTime(QuicTime::Delta delta) {
[email protected]fe053f92013-04-23 20:18:55318 static_cast<MockHelper*>(helper())->AdvanceTime(delta);
319}
320
[email protected]c05a6d222013-12-16 19:42:03321PacketSavingConnection::PacketSavingConnection(bool is_server)
322 : MockConnection(is_server) {
[email protected]044ac2b2012-11-13 21:41:06323}
324
[email protected]4d640792013-12-18 22:21:08325PacketSavingConnection::PacketSavingConnection(
326 bool is_server,
327 const QuicVersionVector& supported_versions)
328 : MockConnection(is_server, supported_versions) {
329}
330
[email protected]044ac2b2012-11-13 21:41:06331PacketSavingConnection::~PacketSavingConnection() {
[email protected]701bc892013-01-17 04:51:54332 STLDeleteElements(&packets_);
[email protected]2532de12013-05-09 12:29:33333 STLDeleteElements(&encrypted_packets_);
[email protected]044ac2b2012-11-13 21:41:06334}
335
[email protected]fee17f72013-02-03 07:47:41336bool PacketSavingConnection::SendOrQueuePacket(
[email protected]8ba81212013-05-03 13:11:48337 EncryptionLevel level,
[email protected]ec86d5462013-11-17 16:04:49338 const SerializedPacket& packet,
339 TransmissionType transmission_type) {
340 packets_.push_back(packet.packet);
[email protected]c5e1aca2014-01-30 04:03:04341 QuicEncryptedPacket* encrypted = QuicConnectionPeer::GetFramer(this)->
342 EncryptPacket(level, packet.sequence_number, *packet.packet);
[email protected]2532de12013-05-09 12:29:33343 encrypted_packets_.push_back(encrypted);
[email protected]044ac2b2012-11-13 21:41:06344 return true;
345}
346
[email protected]c05a6d222013-12-16 19:42:03347MockSession::MockSession(QuicConnection* connection)
348 : QuicSession(connection, DefaultQuicConfig()) {
[email protected]93dd91f2014-02-27 00:09:03349 ON_CALL(*this, WritevData(_, _, _, _, _))
[email protected]cff7b7b2013-01-11 08:49:07350 .WillByDefault(testing::Return(QuicConsumedData(0, false)));
[email protected]044ac2b2012-11-13 21:41:06351}
352
353MockSession::~MockSession() {
354}
355
[email protected]899951652013-05-16 12:52:39356TestSession::TestSession(QuicConnection* connection,
[email protected]c05a6d222013-12-16 19:42:03357 const QuicConfig& config)
358 : QuicSession(connection, config),
[email protected]2532de12013-05-09 12:29:33359 crypto_stream_(NULL) {
360}
361
[email protected]b064310782013-05-30 21:12:17362TestSession::~TestSession() {}
[email protected]2532de12013-05-09 12:29:33363
364void TestSession::SetCryptoStream(QuicCryptoStream* stream) {
365 crypto_stream_ = stream;
366}
367
368QuicCryptoStream* TestSession::GetCryptoStream() {
369 return crypto_stream_;
370}
371
[email protected]90f62f092014-03-24 02:41:23372TestClientSession::TestClientSession(QuicConnection* connection,
373 const QuicConfig& config)
374 : QuicClientSessionBase(connection, config),
375 crypto_stream_(NULL) {
[email protected]64c12232014-03-26 05:43:59376 EXPECT_CALL(*this, OnProofValid(_)).Times(AnyNumber());
[email protected]90f62f092014-03-24 02:41:23377}
378
379TestClientSession::~TestClientSession() {}
380
381void TestClientSession::SetCryptoStream(QuicCryptoStream* stream) {
382 crypto_stream_ = stream;
383}
384
385QuicCryptoStream* TestClientSession::GetCryptoStream() {
386 return crypto_stream_;
387}
388
[email protected]cbd731e2013-10-24 00:20:39389MockPacketWriter::MockPacketWriter() {
390}
391
392MockPacketWriter::~MockPacketWriter() {
393}
394
[email protected]fee17f72013-02-03 07:47:41395MockSendAlgorithm::MockSendAlgorithm() {
[email protected]8d659e22013-01-19 04:26:10396}
397
[email protected]fee17f72013-02-03 07:47:41398MockSendAlgorithm::~MockSendAlgorithm() {
[email protected]8d659e22013-01-19 04:26:10399}
400
[email protected]3aa9ca72014-02-27 19:39:43401MockLossAlgorithm::MockLossAlgorithm() {
402}
403
404MockLossAlgorithm::~MockLossAlgorithm() {
405}
406
[email protected]97cf3022013-09-05 14:30:16407MockAckNotifierDelegate::MockAckNotifierDelegate() {
408}
409
410MockAckNotifierDelegate::~MockAckNotifierDelegate() {
411}
412
[email protected]8b37a092012-10-18 21:53:49413namespace {
414
415string HexDumpWithMarks(const char* data, int length,
416 const bool* marks, int mark_length) {
417 static const char kHexChars[] = "0123456789abcdef";
418 static const int kColumns = 4;
419
420 const int kSizeLimit = 1024;
421 if (length > kSizeLimit || mark_length > kSizeLimit) {
422 LOG(ERROR) << "Only dumping first " << kSizeLimit << " bytes.";
423 length = min(length, kSizeLimit);
424 mark_length = min(mark_length, kSizeLimit);
425 }
426
427 string hex;
428 for (const char* row = data; length > 0;
429 row += kColumns, length -= kColumns) {
430 for (const char *p = row; p < row + 4; ++p) {
431 if (p < row + length) {
432 const bool mark =
433 (marks && (p - data) < mark_length && marks[p - data]);
434 hex += mark ? '*' : ' ';
435 hex += kHexChars[(*p & 0xf0) >> 4];
436 hex += kHexChars[*p & 0x0f];
437 hex += mark ? '*' : ' ';
438 } else {
439 hex += " ";
440 }
441 }
442 hex = hex + " ";
443
444 for (const char *p = row; p < row + 4 && p < row + length; ++p)
445 hex += (*p >= 0x20 && *p <= 0x7f) ? (*p) : '.';
446
447 hex = hex + '\n';
448 }
449 return hex;
450}
451
452} // namespace
453
[email protected]300ccd52014-01-25 08:00:19454IPAddressNumber TestPeerIPAddress() { return Loopback4(); }
455
[email protected]b007e632013-10-28 08:39:25456QuicVersion QuicVersionMax() { return QuicSupportedVersions().front(); }
457
458QuicVersion QuicVersionMin() { return QuicSupportedVersions().back(); }
459
[email protected]c05a6d222013-12-16 19:42:03460IPAddressNumber Loopback4() {
[email protected]300ccd52014-01-25 08:00:19461 IPAddressNumber addr;
462 CHECK(ParseIPLiteralToNumber("127.0.0.1", &addr));
[email protected]c05a6d222013-12-16 19:42:03463 return addr;
464}
465
[email protected]9bb57c72014-03-31 20:36:04466void GenerateBody(string* body, int length) {
467 body->clear();
468 body->reserve(length);
469 for (int i = 0; i < length; ++i) {
470 body->append(1, static_cast<char>(32 + i % (126 - 32)));
471 }
472}
473
[email protected]ffc34bf2014-03-07 02:42:02474QuicEncryptedPacket* ConstructEncryptedPacket(
475 QuicConnectionId connection_id,
476 bool version_flag,
477 bool reset_flag,
478 QuicPacketSequenceNumber sequence_number,
479 const string& data) {
480 QuicPacketHeader header;
481 header.public_header.connection_id = connection_id;
482 header.public_header.connection_id_length = PACKET_8BYTE_CONNECTION_ID;
483 header.public_header.version_flag = version_flag;
484 header.public_header.reset_flag = reset_flag;
485 header.public_header.sequence_number_length = PACKET_6BYTE_SEQUENCE_NUMBER;
486 header.packet_sequence_number = sequence_number;
487 header.entropy_flag = false;
488 header.entropy_hash = 0;
489 header.fec_flag = false;
490 header.is_in_fec_group = NOT_IN_FEC_GROUP;
491 header.fec_group = 0;
492 QuicStreamFrame stream_frame(1, false, 0, MakeIOVector(data));
493 QuicFrame frame(&stream_frame);
494 QuicFrames frames;
495 frames.push_back(frame);
496 QuicFramer framer(QuicSupportedVersions(), QuicTime::Zero(), false);
497 scoped_ptr<QuicPacket> packet(
498 framer.BuildUnsizedDataPacket(header, frames).packet);
499 EXPECT_TRUE(packet != NULL);
500 QuicEncryptedPacket* encrypted = framer.EncryptPacket(ENCRYPTION_NONE,
501 sequence_number,
502 *packet);
503 EXPECT_TRUE(encrypted != NULL);
504 return encrypted;
505}
506
[email protected]8b37a092012-10-18 21:53:49507void CompareCharArraysWithHexError(
508 const string& description,
509 const char* actual,
510 const int actual_len,
511 const char* expected,
512 const int expected_len) {
[email protected]b007e632013-10-28 08:39:25513 EXPECT_EQ(actual_len, expected_len);
[email protected]8b37a092012-10-18 21:53:49514 const int min_len = min(actual_len, expected_len);
515 const int max_len = max(actual_len, expected_len);
[email protected]4356f0f2013-04-07 00:58:17516 scoped_ptr<bool[]> marks(new bool[max_len]);
[email protected]8b37a092012-10-18 21:53:49517 bool identical = (actual_len == expected_len);
518 for (int i = 0; i < min_len; ++i) {
519 if (actual[i] != expected[i]) {
520 marks[i] = true;
521 identical = false;
522 } else {
523 marks[i] = false;
524 }
525 }
526 for (int i = min_len; i < max_len; ++i) {
527 marks[i] = true;
528 }
529 if (identical) return;
530 ADD_FAILURE()
531 << "Description:\n"
532 << description
533 << "\n\nExpected:\n"
534 << HexDumpWithMarks(expected, expected_len, marks.get(), max_len)
535 << "\nActual:\n"
536 << HexDumpWithMarks(actual, actual_len, marks.get(), max_len);
537}
538
[email protected]b12764d2013-12-02 22:28:30539bool DecodeHexString(const base::StringPiece& hex, std::string* bytes) {
540 bytes->clear();
541 if (hex.empty())
542 return true;
543 std::vector<uint8> v;
544 if (!base::HexStringToBytes(hex.as_string(), &v))
545 return false;
546 if (!v.empty())
547 bytes->assign(reinterpret_cast<const char*>(&v[0]), v.size());
548 return true;
549}
550
[email protected]d3d15bf2013-01-30 02:51:54551static QuicPacket* ConstructPacketFromHandshakeMessage(
[email protected]3aa9ca72014-02-27 19:39:43552 QuicConnectionId connection_id,
[email protected]14e8106c2013-03-14 16:25:33553 const CryptoHandshakeMessage& message,
554 bool should_include_version) {
[email protected]8b37a092012-10-18 21:53:49555 CryptoFramer crypto_framer;
[email protected]dc2cc742012-10-21 13:56:13556 scoped_ptr<QuicData> data(crypto_framer.ConstructHandshakeMessage(message));
[email protected]b007e632013-10-28 08:39:25557 QuicFramer quic_framer(QuicSupportedVersions(), QuicTime::Zero(), false);
[email protected]8b37a092012-10-18 21:53:49558
559 QuicPacketHeader header;
[email protected]3aa9ca72014-02-27 19:39:43560 header.public_header.connection_id = connection_id;
[email protected]9db443912013-02-25 05:27:03561 header.public_header.reset_flag = false;
[email protected]14e8106c2013-03-14 16:25:33562 header.public_header.version_flag = should_include_version;
[email protected]8b37a092012-10-18 21:53:49563 header.packet_sequence_number = 1;
[email protected]9db443912013-02-25 05:27:03564 header.entropy_flag = false;
565 header.entropy_hash = 0;
566 header.fec_flag = false;
[email protected]8b37a092012-10-18 21:53:49567 header.fec_group = 0;
568
[email protected]be24ab22012-10-22 03:01:52569 QuicStreamFrame stream_frame(kCryptoStreamId, false, 0,
[email protected]5dafdb62013-11-14 01:24:26570 MakeIOVector(data->AsStringPiece()));
[email protected]8b37a092012-10-18 21:53:49571
[email protected]be24ab22012-10-22 03:01:52572 QuicFrame frame(&stream_frame);
573 QuicFrames frames;
574 frames.push_back(frame);
[email protected]3e60db82013-08-05 19:43:06575 return quic_framer.BuildUnsizedDataPacket(header, frames).packet;
[email protected]8b37a092012-10-18 21:53:49576}
577
[email protected]3aa9ca72014-02-27 19:39:43578QuicPacket* ConstructHandshakePacket(QuicConnectionId connection_id,
579 QuicTag tag) {
[email protected]d3d15bf2013-01-30 02:51:54580 CryptoHandshakeMessage message;
[email protected]ccc66e8a2013-03-26 08:26:14581 message.set_tag(tag);
[email protected]3aa9ca72014-02-27 19:39:43582 return ConstructPacketFromHandshakeMessage(connection_id, message, false);
[email protected]d3d15bf2013-01-30 02:51:54583}
584
[email protected]ea825e02013-08-21 18:12:45585size_t GetPacketLengthForOneStream(
586 QuicVersion version,
587 bool include_version,
588 QuicSequenceNumberLength sequence_number_length,
589 InFecGroup is_in_fec_group,
590 size_t* payload_length) {
[email protected]f62262b2013-07-05 20:57:30591 *payload_length = 1;
592 const size_t stream_length =
[email protected]5dafdb62013-11-14 01:24:26593 NullEncrypter().GetCiphertextSize(*payload_length) +
[email protected]b064310782013-05-30 21:12:17594 QuicPacketCreator::StreamFramePacketOverhead(
[email protected]3aa9ca72014-02-27 19:39:43595 version, PACKET_8BYTE_CONNECTION_ID, include_version,
[email protected]ea825e02013-08-21 18:12:45596 sequence_number_length, is_in_fec_group);
[email protected]5dafdb62013-11-14 01:24:26597 const size_t ack_length = NullEncrypter().GetCiphertextSize(
[email protected]8e01c062013-10-31 07:35:31598 QuicFramer::GetMinAckFrameSize(
599 version, sequence_number_length, PACKET_1BYTE_SEQUENCE_NUMBER)) +
[email protected]3aa9ca72014-02-27 19:39:43600 GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID, include_version,
[email protected]ea825e02013-08-21 18:12:45601 sequence_number_length, is_in_fec_group);
[email protected]f62262b2013-07-05 20:57:30602 if (stream_length < ack_length) {
603 *payload_length = 1 + ack_length - stream_length;
604 }
605
[email protected]5dafdb62013-11-14 01:24:26606 return NullEncrypter().GetCiphertextSize(*payload_length) +
[email protected]f62262b2013-07-05 20:57:30607 QuicPacketCreator::StreamFramePacketOverhead(
[email protected]3aa9ca72014-02-27 19:39:43608 version, PACKET_8BYTE_CONNECTION_ID, include_version,
[email protected]ea825e02013-08-21 18:12:45609 sequence_number_length, is_in_fec_group);
[email protected]5351cc4b2013-03-03 07:22:41610}
611
[email protected]8e01c062013-10-31 07:35:31612TestEntropyCalculator::TestEntropyCalculator() { }
613
614TestEntropyCalculator::~TestEntropyCalculator() { }
615
[email protected]48878092013-07-26 14:51:56616QuicPacketEntropyHash TestEntropyCalculator::EntropyHash(
[email protected]9db443912013-02-25 05:27:03617 QuicPacketSequenceNumber sequence_number) const {
618 return 1u;
619}
620
[email protected]8e01c062013-10-31 07:35:31621MockEntropyCalculator::MockEntropyCalculator() { }
622
623MockEntropyCalculator::~MockEntropyCalculator() { }
624
[email protected]b064310782013-05-30 21:12:17625QuicConfig DefaultQuicConfig() {
626 QuicConfig config;
627 config.SetDefaults();
628 return config;
629}
630
[email protected]4d640792013-12-18 22:21:08631QuicVersionVector SupportedVersions(QuicVersion version) {
632 QuicVersionVector versions;
633 versions.push_back(version);
634 return versions;
635}
636
[email protected]8b37a092012-10-18 21:53:49637} // namespace test
[email protected]8b37a092012-10-18 21:53:49638} // namespace net