blob: fed9eddce0859d3e0da0fb23867826bc123504cd [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]8b37a092012-10-18 21:53:4955MockFramerVisitor::MockFramerVisitor() {
56 // By default, we want to accept packets.
[email protected]14e8106c2013-03-14 16:25:3357 ON_CALL(*this, OnProtocolVersionMismatch(_))
58 .WillByDefault(testing::Return(false));
59
60 // By default, we want to accept packets.
[email protected]ec86d5462013-11-17 16:04:4961 ON_CALL(*this, OnUnauthenticatedHeader(_))
62 .WillByDefault(testing::Return(true));
63
[email protected]066d8182014-01-04 02:02:4564 ON_CALL(*this, OnUnauthenticatedPublicHeader(_))
65 .WillByDefault(testing::Return(true));
66
[email protected]cff7b7b2013-01-11 08:49:0767 ON_CALL(*this, OnPacketHeader(_))
[email protected]8b37a092012-10-18 21:53:4968 .WillByDefault(testing::Return(true));
[email protected]a57e0272013-04-26 07:31:4769
70 ON_CALL(*this, OnStreamFrame(_))
71 .WillByDefault(testing::Return(true));
72
73 ON_CALL(*this, OnAckFrame(_))
74 .WillByDefault(testing::Return(true));
75
76 ON_CALL(*this, OnCongestionFeedbackFrame(_))
77 .WillByDefault(testing::Return(true));
78
[email protected]93dd91f2014-02-27 00:09:0379 ON_CALL(*this, OnStopWaitingFrame(_))
80 .WillByDefault(testing::Return(true));
81
[email protected]a57e0272013-04-26 07:31:4782 ON_CALL(*this, OnRstStreamFrame(_))
83 .WillByDefault(testing::Return(true));
84
85 ON_CALL(*this, OnConnectionCloseFrame(_))
86 .WillByDefault(testing::Return(true));
87
88 ON_CALL(*this, OnGoAwayFrame(_))
89 .WillByDefault(testing::Return(true));
[email protected]8b37a092012-10-18 21:53:4990}
91
[email protected]044ac2b2012-11-13 21:41:0692MockFramerVisitor::~MockFramerVisitor() {
93}
[email protected]8b37a092012-10-18 21:53:4994
[email protected]48878092013-07-26 14:51:5695bool NoOpFramerVisitor::OnProtocolVersionMismatch(QuicVersion version) {
[email protected]14e8106c2013-03-14 16:25:3396 return false;
97}
98
[email protected]066d8182014-01-04 02:02:4599bool NoOpFramerVisitor::OnUnauthenticatedPublicHeader(
100 const QuicPacketPublicHeader& header) {
101 return true;
102}
103
[email protected]ec86d5462013-11-17 16:04:49104bool NoOpFramerVisitor::OnUnauthenticatedHeader(
105 const QuicPacketHeader& header) {
106 return true;
107}
108
[email protected]8b37a092012-10-18 21:53:49109bool NoOpFramerVisitor::OnPacketHeader(const QuicPacketHeader& header) {
110 return true;
111}
112
[email protected]a57e0272013-04-26 07:31:47113bool NoOpFramerVisitor::OnStreamFrame(const QuicStreamFrame& frame) {
114 return true;
115}
116
117bool NoOpFramerVisitor::OnAckFrame(const QuicAckFrame& frame) {
118 return true;
119}
120
121bool NoOpFramerVisitor::OnCongestionFeedbackFrame(
122 const QuicCongestionFeedbackFrame& frame) {
123 return true;
124}
125
[email protected]93dd91f2014-02-27 00:09:03126bool NoOpFramerVisitor::OnStopWaitingFrame(
127 const QuicStopWaitingFrame& frame) {
128 return true;
129}
130
[email protected]a57e0272013-04-26 07:31:47131bool NoOpFramerVisitor::OnRstStreamFrame(
132 const QuicRstStreamFrame& frame) {
133 return true;
134}
135
136bool NoOpFramerVisitor::OnConnectionCloseFrame(
137 const QuicConnectionCloseFrame& frame) {
138 return true;
139}
140
141bool NoOpFramerVisitor::OnGoAwayFrame(const QuicGoAwayFrame& frame) {
142 return true;
143}
144
[email protected]cb23a922014-02-20 17:42:38145bool NoOpFramerVisitor::OnWindowUpdateFrame(
146 const QuicWindowUpdateFrame& frame) {
147 return true;
148}
149
150bool NoOpFramerVisitor::OnBlockedFrame(const QuicBlockedFrame& frame) {
151 return true;
152}
153
[email protected]9db443912013-02-25 05:27:03154FramerVisitorCapturingFrames::FramerVisitorCapturingFrames() : frame_count_(0) {
[email protected]134e5c32012-12-12 19:20:36155}
156
[email protected]9db443912013-02-25 05:27:03157FramerVisitorCapturingFrames::~FramerVisitorCapturingFrames() {
[email protected]0ac0c5b2013-11-20 05:55:58158 Reset();
159}
160
161void FramerVisitorCapturingFrames::Reset() {
[email protected]583bcbcf2013-10-28 01:51:15162 STLDeleteElements(&stream_data_);
[email protected]0ac0c5b2013-11-20 05:55:58163 stream_frames_.clear();
164 frame_count_ = 0;
165 ack_.reset();
166 feedback_.reset();
167 rst_.reset();
168 close_.reset();
169 goaway_.reset();
170 version_negotiation_packet_.reset();
[email protected]26f3f8e2012-12-13 21:07:19171}
172
[email protected]9db443912013-02-25 05:27:03173bool FramerVisitorCapturingFrames::OnPacketHeader(
[email protected]4e6f0ed2012-11-02 22:15:38174 const QuicPacketHeader& header) {
175 header_ = header;
[email protected]9db443912013-02-25 05:27:03176 frame_count_ = 0;
[email protected]4e6f0ed2012-11-02 22:15:38177 return true;
178}
179
[email protected]a57e0272013-04-26 07:31:47180bool FramerVisitorCapturingFrames::OnStreamFrame(const QuicStreamFrame& frame) {
[email protected]583bcbcf2013-10-28 01:51:15181 // Make a copy of the frame and store a copy of underlying string, since
182 // frame.data may not exist outside this callback.
[email protected]5dafdb62013-11-14 01:24:26183 stream_data_.push_back(frame.GetDataAsString());
[email protected]583bcbcf2013-10-28 01:51:15184 QuicStreamFrame frame_copy = frame;
[email protected]5dafdb62013-11-14 01:24:26185 frame_copy.data.Clear();
186 frame_copy.data.Append(const_cast<char*>(stream_data_.back()->data()),
187 stream_data_.back()->size());
[email protected]583bcbcf2013-10-28 01:51:15188 stream_frames_.push_back(frame_copy);
[email protected]9db443912013-02-25 05:27:03189 ++frame_count_;
[email protected]a57e0272013-04-26 07:31:47190 return true;
[email protected]26f3f8e2012-12-13 21:07:19191}
192
[email protected]a57e0272013-04-26 07:31:47193bool FramerVisitorCapturingFrames::OnAckFrame(const QuicAckFrame& frame) {
[email protected]9db443912013-02-25 05:27:03194 ack_.reset(new QuicAckFrame(frame));
195 ++frame_count_;
[email protected]a57e0272013-04-26 07:31:47196 return true;
[email protected]9db443912013-02-25 05:27:03197}
198
[email protected]a57e0272013-04-26 07:31:47199bool FramerVisitorCapturingFrames::OnCongestionFeedbackFrame(
[email protected]26f3f8e2012-12-13 21:07:19200 const QuicCongestionFeedbackFrame& frame) {
201 feedback_.reset(new QuicCongestionFeedbackFrame(frame));
[email protected]9db443912013-02-25 05:27:03202 ++frame_count_;
[email protected]a57e0272013-04-26 07:31:47203 return true;
[email protected]9db443912013-02-25 05:27:03204}
205
[email protected]93dd91f2014-02-27 00:09:03206bool FramerVisitorCapturingFrames::OnStopWaitingFrame(
207 const QuicStopWaitingFrame& frame) {
208 stop_waiting_.reset(new QuicStopWaitingFrame(frame));
209 ++frame_count_;
210 return true;
211}
212
[email protected]a57e0272013-04-26 07:31:47213bool FramerVisitorCapturingFrames::OnRstStreamFrame(
[email protected]9db443912013-02-25 05:27:03214 const QuicRstStreamFrame& frame) {
215 rst_.reset(new QuicRstStreamFrame(frame));
216 ++frame_count_;
[email protected]a57e0272013-04-26 07:31:47217 return true;
[email protected]9db443912013-02-25 05:27:03218}
219
[email protected]a57e0272013-04-26 07:31:47220bool FramerVisitorCapturingFrames::OnConnectionCloseFrame(
[email protected]9db443912013-02-25 05:27:03221 const QuicConnectionCloseFrame& frame) {
222 close_.reset(new QuicConnectionCloseFrame(frame));
223 ++frame_count_;
[email protected]a57e0272013-04-26 07:31:47224 return true;
[email protected]9db443912013-02-25 05:27:03225}
226
[email protected]a57e0272013-04-26 07:31:47227bool FramerVisitorCapturingFrames::OnGoAwayFrame(const QuicGoAwayFrame& frame) {
[email protected]9db443912013-02-25 05:27:03228 goaway_.reset(new QuicGoAwayFrame(frame));
229 ++frame_count_;
[email protected]a57e0272013-04-26 07:31:47230 return true;
[email protected]4e6f0ed2012-11-02 22:15:38231}
232
[email protected]14e8106c2013-03-14 16:25:33233void FramerVisitorCapturingFrames::OnVersionNegotiationPacket(
234 const QuicVersionNegotiationPacket& packet) {
235 version_negotiation_packet_.reset(new QuicVersionNegotiationPacket(packet));
236 frame_count_ = 0;
237}
238
[email protected]fee17f72013-02-03 07:47:41239FramerVisitorCapturingPublicReset::FramerVisitorCapturingPublicReset() {
240}
241
242FramerVisitorCapturingPublicReset::~FramerVisitorCapturingPublicReset() {
243}
244
245void FramerVisitorCapturingPublicReset::OnPublicResetPacket(
246 const QuicPublicResetPacket& public_reset) {
247 public_reset_packet_ = public_reset;
248}
249
[email protected]8d659e22013-01-19 04:26:10250MockConnectionVisitor::MockConnectionVisitor() {
251}
252
253MockConnectionVisitor::~MockConnectionVisitor() {
254}
255
[email protected]9c0b1352012-11-04 00:03:27256MockHelper::MockHelper() {
257}
258
259MockHelper::~MockHelper() {
260}
261
[email protected]97693d12012-11-16 16:05:00262const QuicClock* MockHelper::GetClock() const {
[email protected]9c0b1352012-11-04 00:03:27263 return &clock_;
264}
265
[email protected]9558c5d32012-12-22 00:08:14266QuicRandom* MockHelper::GetRandomGenerator() {
267 return &random_generator_;
268}
269
[email protected]965dbe62013-08-09 21:34:31270QuicAlarm* MockHelper::CreateAlarm(QuicAlarm::Delegate* delegate) {
271 return new TestAlarm(delegate);
272}
273
[email protected]fe053f92013-04-23 20:18:55274void MockHelper::AdvanceTime(QuicTime::Delta delta) {
275 clock_.AdvanceTime(delta);
276}
277
[email protected]c05a6d222013-12-16 19:42:03278MockConnection::MockConnection(bool is_server)
[email protected]3aa9ca72014-02-27 19:39:43279 : QuicConnection(kTestConnectionId,
[email protected]300ccd52014-01-25 08:00:19280 IPEndPoint(TestPeerIPAddress(), kTestPort),
[email protected]c05a6d222013-12-16 19:42:03281 new testing::NiceMock<MockHelper>(),
[email protected]cbd731e2013-10-24 00:20:39282 new testing::NiceMock<MockPacketWriter>(),
[email protected]9bb57c72014-03-31 20:36:04283 is_server, QuicSupportedVersions(),
284 kInitialFlowControlWindowForTest),
[email protected]c05a6d222013-12-16 19:42:03285 writer_(QuicConnectionPeer::GetWriter(this)),
286 helper_(helper()) {
287}
288
289MockConnection::MockConnection(IPEndPoint address,
290 bool is_server)
[email protected]3aa9ca72014-02-27 19:39:43291 : QuicConnection(kTestConnectionId, address,
[email protected]c05a6d222013-12-16 19:42:03292 new testing::NiceMock<MockHelper>(),
293 new testing::NiceMock<MockPacketWriter>(),
[email protected]9bb57c72014-03-31 20:36:04294 is_server, QuicSupportedVersions(),
295 kInitialFlowControlWindowForTest),
[email protected]2cfc6bb82013-10-27 03:40:44296 writer_(QuicConnectionPeer::GetWriter(this)),
297 helper_(helper()) {
[email protected]044ac2b2012-11-13 21:41:06298}
299
[email protected]3aa9ca72014-02-27 19:39:43300MockConnection::MockConnection(QuicConnectionId connection_id,
[email protected]14e8106c2013-03-14 16:25:33301 bool is_server)
[email protected]3aa9ca72014-02-27 19:39:43302 : QuicConnection(connection_id,
303 IPEndPoint(TestPeerIPAddress(), kTestPort),
[email protected]c05a6d222013-12-16 19:42:03304 new testing::NiceMock<MockHelper>(),
305 new testing::NiceMock<MockPacketWriter>(),
[email protected]9bb57c72014-03-31 20:36:04306 is_server, QuicSupportedVersions(),
307 kInitialFlowControlWindowForTest),
[email protected]c05a6d222013-12-16 19:42:03308 writer_(QuicConnectionPeer::GetWriter(this)),
309 helper_(helper()) {
[email protected]872edd9e2013-01-16 08:51:15310}
311
[email protected]4d640792013-12-18 22:21:08312MockConnection::MockConnection(bool is_server,
313 const QuicVersionVector& supported_versions)
[email protected]3aa9ca72014-02-27 19:39:43314 : QuicConnection(kTestConnectionId,
[email protected]300ccd52014-01-25 08:00:19315 IPEndPoint(TestPeerIPAddress(), kTestPort),
[email protected]4d640792013-12-18 22:21:08316 new testing::NiceMock<MockHelper>(),
317 new testing::NiceMock<MockPacketWriter>(),
[email protected]9bb57c72014-03-31 20:36:04318 is_server, supported_versions,
319 kInitialFlowControlWindowForTest),
[email protected]4d640792013-12-18 22:21:08320 writer_(QuicConnectionPeer::GetWriter(this)),
321 helper_(helper()) {
322}
323
[email protected]044ac2b2012-11-13 21:41:06324MockConnection::~MockConnection() {
325}
326
[email protected]fe053f92013-04-23 20:18:55327void MockConnection::AdvanceTime(QuicTime::Delta delta) {
[email protected]fe053f92013-04-23 20:18:55328 static_cast<MockHelper*>(helper())->AdvanceTime(delta);
329}
330
[email protected]c05a6d222013-12-16 19:42:03331PacketSavingConnection::PacketSavingConnection(bool is_server)
332 : MockConnection(is_server) {
[email protected]044ac2b2012-11-13 21:41:06333}
334
[email protected]4d640792013-12-18 22:21:08335PacketSavingConnection::PacketSavingConnection(
336 bool is_server,
337 const QuicVersionVector& supported_versions)
338 : MockConnection(is_server, supported_versions) {
339}
340
[email protected]044ac2b2012-11-13 21:41:06341PacketSavingConnection::~PacketSavingConnection() {
[email protected]701bc892013-01-17 04:51:54342 STLDeleteElements(&packets_);
[email protected]2532de12013-05-09 12:29:33343 STLDeleteElements(&encrypted_packets_);
[email protected]044ac2b2012-11-13 21:41:06344}
345
[email protected]fee17f72013-02-03 07:47:41346bool PacketSavingConnection::SendOrQueuePacket(
[email protected]8ba81212013-05-03 13:11:48347 EncryptionLevel level,
[email protected]ec86d5462013-11-17 16:04:49348 const SerializedPacket& packet,
349 TransmissionType transmission_type) {
350 packets_.push_back(packet.packet);
[email protected]c5e1aca2014-01-30 04:03:04351 QuicEncryptedPacket* encrypted = QuicConnectionPeer::GetFramer(this)->
352 EncryptPacket(level, packet.sequence_number, *packet.packet);
[email protected]2532de12013-05-09 12:29:33353 encrypted_packets_.push_back(encrypted);
[email protected]044ac2b2012-11-13 21:41:06354 return true;
355}
356
[email protected]c05a6d222013-12-16 19:42:03357MockSession::MockSession(QuicConnection* connection)
358 : QuicSession(connection, DefaultQuicConfig()) {
[email protected]93dd91f2014-02-27 00:09:03359 ON_CALL(*this, WritevData(_, _, _, _, _))
[email protected]cff7b7b2013-01-11 08:49:07360 .WillByDefault(testing::Return(QuicConsumedData(0, false)));
[email protected]044ac2b2012-11-13 21:41:06361}
362
363MockSession::~MockSession() {
364}
365
[email protected]899951652013-05-16 12:52:39366TestSession::TestSession(QuicConnection* connection,
[email protected]c05a6d222013-12-16 19:42:03367 const QuicConfig& config)
368 : QuicSession(connection, config),
[email protected]2532de12013-05-09 12:29:33369 crypto_stream_(NULL) {
370}
371
[email protected]b064310782013-05-30 21:12:17372TestSession::~TestSession() {}
[email protected]2532de12013-05-09 12:29:33373
374void TestSession::SetCryptoStream(QuicCryptoStream* stream) {
375 crypto_stream_ = stream;
376}
377
378QuicCryptoStream* TestSession::GetCryptoStream() {
379 return crypto_stream_;
380}
381
[email protected]90f62f092014-03-24 02:41:23382TestClientSession::TestClientSession(QuicConnection* connection,
383 const QuicConfig& config)
384 : QuicClientSessionBase(connection, config),
385 crypto_stream_(NULL) {
[email protected]64c12232014-03-26 05:43:59386 EXPECT_CALL(*this, OnProofValid(_)).Times(AnyNumber());
[email protected]90f62f092014-03-24 02:41:23387}
388
389TestClientSession::~TestClientSession() {}
390
391void TestClientSession::SetCryptoStream(QuicCryptoStream* stream) {
392 crypto_stream_ = stream;
393}
394
395QuicCryptoStream* TestClientSession::GetCryptoStream() {
396 return crypto_stream_;
397}
398
[email protected]cbd731e2013-10-24 00:20:39399MockPacketWriter::MockPacketWriter() {
400}
401
402MockPacketWriter::~MockPacketWriter() {
403}
404
[email protected]fee17f72013-02-03 07:47:41405MockSendAlgorithm::MockSendAlgorithm() {
[email protected]8d659e22013-01-19 04:26:10406}
407
[email protected]fee17f72013-02-03 07:47:41408MockSendAlgorithm::~MockSendAlgorithm() {
[email protected]8d659e22013-01-19 04:26:10409}
410
[email protected]3aa9ca72014-02-27 19:39:43411MockLossAlgorithm::MockLossAlgorithm() {
412}
413
414MockLossAlgorithm::~MockLossAlgorithm() {
415}
416
[email protected]97cf3022013-09-05 14:30:16417MockAckNotifierDelegate::MockAckNotifierDelegate() {
418}
419
420MockAckNotifierDelegate::~MockAckNotifierDelegate() {
421}
422
[email protected]8b37a092012-10-18 21:53:49423namespace {
424
425string HexDumpWithMarks(const char* data, int length,
426 const bool* marks, int mark_length) {
427 static const char kHexChars[] = "0123456789abcdef";
428 static const int kColumns = 4;
429
430 const int kSizeLimit = 1024;
431 if (length > kSizeLimit || mark_length > kSizeLimit) {
432 LOG(ERROR) << "Only dumping first " << kSizeLimit << " bytes.";
433 length = min(length, kSizeLimit);
434 mark_length = min(mark_length, kSizeLimit);
435 }
436
437 string hex;
438 for (const char* row = data; length > 0;
439 row += kColumns, length -= kColumns) {
440 for (const char *p = row; p < row + 4; ++p) {
441 if (p < row + length) {
442 const bool mark =
443 (marks && (p - data) < mark_length && marks[p - data]);
444 hex += mark ? '*' : ' ';
445 hex += kHexChars[(*p & 0xf0) >> 4];
446 hex += kHexChars[*p & 0x0f];
447 hex += mark ? '*' : ' ';
448 } else {
449 hex += " ";
450 }
451 }
452 hex = hex + " ";
453
454 for (const char *p = row; p < row + 4 && p < row + length; ++p)
455 hex += (*p >= 0x20 && *p <= 0x7f) ? (*p) : '.';
456
457 hex = hex + '\n';
458 }
459 return hex;
460}
461
462} // namespace
463
[email protected]300ccd52014-01-25 08:00:19464IPAddressNumber TestPeerIPAddress() { return Loopback4(); }
465
[email protected]b007e632013-10-28 08:39:25466QuicVersion QuicVersionMax() { return QuicSupportedVersions().front(); }
467
468QuicVersion QuicVersionMin() { return QuicSupportedVersions().back(); }
469
[email protected]c05a6d222013-12-16 19:42:03470IPAddressNumber Loopback4() {
[email protected]300ccd52014-01-25 08:00:19471 IPAddressNumber addr;
472 CHECK(ParseIPLiteralToNumber("127.0.0.1", &addr));
[email protected]c05a6d222013-12-16 19:42:03473 return addr;
474}
475
[email protected]9bb57c72014-03-31 20:36:04476void GenerateBody(string* body, int length) {
477 body->clear();
478 body->reserve(length);
479 for (int i = 0; i < length; ++i) {
480 body->append(1, static_cast<char>(32 + i % (126 - 32)));
481 }
482}
483
[email protected]ffc34bf2014-03-07 02:42:02484QuicEncryptedPacket* ConstructEncryptedPacket(
485 QuicConnectionId connection_id,
486 bool version_flag,
487 bool reset_flag,
488 QuicPacketSequenceNumber sequence_number,
489 const string& data) {
490 QuicPacketHeader header;
491 header.public_header.connection_id = connection_id;
492 header.public_header.connection_id_length = PACKET_8BYTE_CONNECTION_ID;
493 header.public_header.version_flag = version_flag;
494 header.public_header.reset_flag = reset_flag;
495 header.public_header.sequence_number_length = PACKET_6BYTE_SEQUENCE_NUMBER;
496 header.packet_sequence_number = sequence_number;
497 header.entropy_flag = false;
498 header.entropy_hash = 0;
499 header.fec_flag = false;
500 header.is_in_fec_group = NOT_IN_FEC_GROUP;
501 header.fec_group = 0;
502 QuicStreamFrame stream_frame(1, false, 0, MakeIOVector(data));
503 QuicFrame frame(&stream_frame);
504 QuicFrames frames;
505 frames.push_back(frame);
506 QuicFramer framer(QuicSupportedVersions(), QuicTime::Zero(), false);
507 scoped_ptr<QuicPacket> packet(
508 framer.BuildUnsizedDataPacket(header, frames).packet);
509 EXPECT_TRUE(packet != NULL);
510 QuicEncryptedPacket* encrypted = framer.EncryptPacket(ENCRYPTION_NONE,
511 sequence_number,
512 *packet);
513 EXPECT_TRUE(encrypted != NULL);
514 return encrypted;
515}
516
[email protected]8b37a092012-10-18 21:53:49517void CompareCharArraysWithHexError(
518 const string& description,
519 const char* actual,
520 const int actual_len,
521 const char* expected,
522 const int expected_len) {
[email protected]b007e632013-10-28 08:39:25523 EXPECT_EQ(actual_len, expected_len);
[email protected]8b37a092012-10-18 21:53:49524 const int min_len = min(actual_len, expected_len);
525 const int max_len = max(actual_len, expected_len);
[email protected]4356f0f2013-04-07 00:58:17526 scoped_ptr<bool[]> marks(new bool[max_len]);
[email protected]8b37a092012-10-18 21:53:49527 bool identical = (actual_len == expected_len);
528 for (int i = 0; i < min_len; ++i) {
529 if (actual[i] != expected[i]) {
530 marks[i] = true;
531 identical = false;
532 } else {
533 marks[i] = false;
534 }
535 }
536 for (int i = min_len; i < max_len; ++i) {
537 marks[i] = true;
538 }
539 if (identical) return;
540 ADD_FAILURE()
541 << "Description:\n"
542 << description
543 << "\n\nExpected:\n"
544 << HexDumpWithMarks(expected, expected_len, marks.get(), max_len)
545 << "\nActual:\n"
546 << HexDumpWithMarks(actual, actual_len, marks.get(), max_len);
547}
548
[email protected]b12764d2013-12-02 22:28:30549bool DecodeHexString(const base::StringPiece& hex, std::string* bytes) {
550 bytes->clear();
551 if (hex.empty())
552 return true;
553 std::vector<uint8> v;
554 if (!base::HexStringToBytes(hex.as_string(), &v))
555 return false;
556 if (!v.empty())
557 bytes->assign(reinterpret_cast<const char*>(&v[0]), v.size());
558 return true;
559}
560
[email protected]d3d15bf2013-01-30 02:51:54561static QuicPacket* ConstructPacketFromHandshakeMessage(
[email protected]3aa9ca72014-02-27 19:39:43562 QuicConnectionId connection_id,
[email protected]14e8106c2013-03-14 16:25:33563 const CryptoHandshakeMessage& message,
564 bool should_include_version) {
[email protected]8b37a092012-10-18 21:53:49565 CryptoFramer crypto_framer;
[email protected]dc2cc742012-10-21 13:56:13566 scoped_ptr<QuicData> data(crypto_framer.ConstructHandshakeMessage(message));
[email protected]b007e632013-10-28 08:39:25567 QuicFramer quic_framer(QuicSupportedVersions(), QuicTime::Zero(), false);
[email protected]8b37a092012-10-18 21:53:49568
569 QuicPacketHeader header;
[email protected]3aa9ca72014-02-27 19:39:43570 header.public_header.connection_id = connection_id;
[email protected]9db443912013-02-25 05:27:03571 header.public_header.reset_flag = false;
[email protected]14e8106c2013-03-14 16:25:33572 header.public_header.version_flag = should_include_version;
[email protected]8b37a092012-10-18 21:53:49573 header.packet_sequence_number = 1;
[email protected]9db443912013-02-25 05:27:03574 header.entropy_flag = false;
575 header.entropy_hash = 0;
576 header.fec_flag = false;
[email protected]8b37a092012-10-18 21:53:49577 header.fec_group = 0;
578
[email protected]be24ab22012-10-22 03:01:52579 QuicStreamFrame stream_frame(kCryptoStreamId, false, 0,
[email protected]5dafdb62013-11-14 01:24:26580 MakeIOVector(data->AsStringPiece()));
[email protected]8b37a092012-10-18 21:53:49581
[email protected]be24ab22012-10-22 03:01:52582 QuicFrame frame(&stream_frame);
583 QuicFrames frames;
584 frames.push_back(frame);
[email protected]3e60db82013-08-05 19:43:06585 return quic_framer.BuildUnsizedDataPacket(header, frames).packet;
[email protected]8b37a092012-10-18 21:53:49586}
587
[email protected]3aa9ca72014-02-27 19:39:43588QuicPacket* ConstructHandshakePacket(QuicConnectionId connection_id,
589 QuicTag tag) {
[email protected]d3d15bf2013-01-30 02:51:54590 CryptoHandshakeMessage message;
[email protected]ccc66e8a2013-03-26 08:26:14591 message.set_tag(tag);
[email protected]3aa9ca72014-02-27 19:39:43592 return ConstructPacketFromHandshakeMessage(connection_id, message, false);
[email protected]d3d15bf2013-01-30 02:51:54593}
594
[email protected]ea825e02013-08-21 18:12:45595size_t GetPacketLengthForOneStream(
596 QuicVersion version,
597 bool include_version,
598 QuicSequenceNumberLength sequence_number_length,
599 InFecGroup is_in_fec_group,
600 size_t* payload_length) {
[email protected]f62262b2013-07-05 20:57:30601 *payload_length = 1;
602 const size_t stream_length =
[email protected]5dafdb62013-11-14 01:24:26603 NullEncrypter().GetCiphertextSize(*payload_length) +
[email protected]b064310782013-05-30 21:12:17604 QuicPacketCreator::StreamFramePacketOverhead(
[email protected]3aa9ca72014-02-27 19:39:43605 version, PACKET_8BYTE_CONNECTION_ID, include_version,
[email protected]ea825e02013-08-21 18:12:45606 sequence_number_length, is_in_fec_group);
[email protected]5dafdb62013-11-14 01:24:26607 const size_t ack_length = NullEncrypter().GetCiphertextSize(
[email protected]8e01c062013-10-31 07:35:31608 QuicFramer::GetMinAckFrameSize(
609 version, sequence_number_length, PACKET_1BYTE_SEQUENCE_NUMBER)) +
[email protected]3aa9ca72014-02-27 19:39:43610 GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID, include_version,
[email protected]ea825e02013-08-21 18:12:45611 sequence_number_length, is_in_fec_group);
[email protected]f62262b2013-07-05 20:57:30612 if (stream_length < ack_length) {
613 *payload_length = 1 + ack_length - stream_length;
614 }
615
[email protected]5dafdb62013-11-14 01:24:26616 return NullEncrypter().GetCiphertextSize(*payload_length) +
[email protected]f62262b2013-07-05 20:57:30617 QuicPacketCreator::StreamFramePacketOverhead(
[email protected]3aa9ca72014-02-27 19:39:43618 version, PACKET_8BYTE_CONNECTION_ID, include_version,
[email protected]ea825e02013-08-21 18:12:45619 sequence_number_length, is_in_fec_group);
[email protected]5351cc4b2013-03-03 07:22:41620}
621
[email protected]8e01c062013-10-31 07:35:31622TestEntropyCalculator::TestEntropyCalculator() { }
623
624TestEntropyCalculator::~TestEntropyCalculator() { }
625
[email protected]48878092013-07-26 14:51:56626QuicPacketEntropyHash TestEntropyCalculator::EntropyHash(
[email protected]9db443912013-02-25 05:27:03627 QuicPacketSequenceNumber sequence_number) const {
628 return 1u;
629}
630
[email protected]8e01c062013-10-31 07:35:31631MockEntropyCalculator::MockEntropyCalculator() { }
632
633MockEntropyCalculator::~MockEntropyCalculator() { }
634
[email protected]b064310782013-05-30 21:12:17635QuicConfig DefaultQuicConfig() {
636 QuicConfig config;
637 config.SetDefaults();
638 return config;
639}
640
[email protected]4d640792013-12-18 22:21:08641QuicVersionVector SupportedVersions(QuicVersion version) {
642 QuicVersionVector versions;
643 versions.push_back(version);
644 return versions;
645}
646
[email protected]8b37a092012-10-18 21:53:49647} // namespace test
[email protected]8b37a092012-10-18 21:53:49648} // namespace net