blob: e6beb988953512dcfabda5bc29000dce67d590a3 [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]8b37a092012-10-18 21:53:4926
27namespace net {
28namespace test {
[email protected]965dbe62013-08-09 21:34:3129namespace {
30
31// No-op alarm implementation used by MockHelper.
32class TestAlarm : public QuicAlarm {
33 public:
34 explicit TestAlarm(QuicAlarm::Delegate* delegate)
35 : QuicAlarm(delegate) {
36 }
37
38 virtual void SetImpl() OVERRIDE {}
39 virtual void CancelImpl() OVERRIDE {}
40};
41
42} // namespace
[email protected]8b37a092012-10-18 21:53:4943
44MockFramerVisitor::MockFramerVisitor() {
45 // By default, we want to accept packets.
[email protected]14e8106c2013-03-14 16:25:3346 ON_CALL(*this, OnProtocolVersionMismatch(_))
47 .WillByDefault(testing::Return(false));
48
49 // By default, we want to accept packets.
[email protected]ec86d5462013-11-17 16:04:4950 ON_CALL(*this, OnUnauthenticatedHeader(_))
51 .WillByDefault(testing::Return(true));
52
[email protected]066d8182014-01-04 02:02:4553 ON_CALL(*this, OnUnauthenticatedPublicHeader(_))
54 .WillByDefault(testing::Return(true));
55
[email protected]cff7b7b2013-01-11 08:49:0756 ON_CALL(*this, OnPacketHeader(_))
[email protected]8b37a092012-10-18 21:53:4957 .WillByDefault(testing::Return(true));
[email protected]a57e0272013-04-26 07:31:4758
59 ON_CALL(*this, OnStreamFrame(_))
60 .WillByDefault(testing::Return(true));
61
62 ON_CALL(*this, OnAckFrame(_))
63 .WillByDefault(testing::Return(true));
64
65 ON_CALL(*this, OnCongestionFeedbackFrame(_))
66 .WillByDefault(testing::Return(true));
67
[email protected]93dd91f2014-02-27 00:09:0368 ON_CALL(*this, OnStopWaitingFrame(_))
69 .WillByDefault(testing::Return(true));
70
[email protected]a57e0272013-04-26 07:31:4771 ON_CALL(*this, OnRstStreamFrame(_))
72 .WillByDefault(testing::Return(true));
73
74 ON_CALL(*this, OnConnectionCloseFrame(_))
75 .WillByDefault(testing::Return(true));
76
77 ON_CALL(*this, OnGoAwayFrame(_))
78 .WillByDefault(testing::Return(true));
[email protected]8b37a092012-10-18 21:53:4979}
80
[email protected]044ac2b2012-11-13 21:41:0681MockFramerVisitor::~MockFramerVisitor() {
82}
[email protected]8b37a092012-10-18 21:53:4983
[email protected]48878092013-07-26 14:51:5684bool NoOpFramerVisitor::OnProtocolVersionMismatch(QuicVersion version) {
[email protected]14e8106c2013-03-14 16:25:3385 return false;
86}
87
[email protected]066d8182014-01-04 02:02:4588bool NoOpFramerVisitor::OnUnauthenticatedPublicHeader(
89 const QuicPacketPublicHeader& header) {
90 return true;
91}
92
[email protected]ec86d5462013-11-17 16:04:4993bool NoOpFramerVisitor::OnUnauthenticatedHeader(
94 const QuicPacketHeader& header) {
95 return true;
96}
97
[email protected]8b37a092012-10-18 21:53:4998bool NoOpFramerVisitor::OnPacketHeader(const QuicPacketHeader& header) {
99 return true;
100}
101
[email protected]a57e0272013-04-26 07:31:47102bool NoOpFramerVisitor::OnStreamFrame(const QuicStreamFrame& frame) {
103 return true;
104}
105
106bool NoOpFramerVisitor::OnAckFrame(const QuicAckFrame& frame) {
107 return true;
108}
109
110bool NoOpFramerVisitor::OnCongestionFeedbackFrame(
111 const QuicCongestionFeedbackFrame& frame) {
112 return true;
113}
114
[email protected]93dd91f2014-02-27 00:09:03115bool NoOpFramerVisitor::OnStopWaitingFrame(
116 const QuicStopWaitingFrame& frame) {
117 return true;
118}
119
[email protected]a57e0272013-04-26 07:31:47120bool NoOpFramerVisitor::OnRstStreamFrame(
121 const QuicRstStreamFrame& frame) {
122 return true;
123}
124
125bool NoOpFramerVisitor::OnConnectionCloseFrame(
126 const QuicConnectionCloseFrame& frame) {
127 return true;
128}
129
130bool NoOpFramerVisitor::OnGoAwayFrame(const QuicGoAwayFrame& frame) {
131 return true;
132}
133
[email protected]cb23a922014-02-20 17:42:38134bool NoOpFramerVisitor::OnWindowUpdateFrame(
135 const QuicWindowUpdateFrame& frame) {
136 return true;
137}
138
139bool NoOpFramerVisitor::OnBlockedFrame(const QuicBlockedFrame& frame) {
140 return true;
141}
142
[email protected]9db443912013-02-25 05:27:03143FramerVisitorCapturingFrames::FramerVisitorCapturingFrames() : frame_count_(0) {
[email protected]134e5c32012-12-12 19:20:36144}
145
[email protected]9db443912013-02-25 05:27:03146FramerVisitorCapturingFrames::~FramerVisitorCapturingFrames() {
[email protected]0ac0c5b2013-11-20 05:55:58147 Reset();
148}
149
150void FramerVisitorCapturingFrames::Reset() {
[email protected]583bcbcf2013-10-28 01:51:15151 STLDeleteElements(&stream_data_);
[email protected]0ac0c5b2013-11-20 05:55:58152 stream_frames_.clear();
153 frame_count_ = 0;
154 ack_.reset();
155 feedback_.reset();
156 rst_.reset();
157 close_.reset();
158 goaway_.reset();
159 version_negotiation_packet_.reset();
[email protected]26f3f8e2012-12-13 21:07:19160}
161
[email protected]9db443912013-02-25 05:27:03162bool FramerVisitorCapturingFrames::OnPacketHeader(
[email protected]4e6f0ed2012-11-02 22:15:38163 const QuicPacketHeader& header) {
164 header_ = header;
[email protected]9db443912013-02-25 05:27:03165 frame_count_ = 0;
[email protected]4e6f0ed2012-11-02 22:15:38166 return true;
167}
168
[email protected]a57e0272013-04-26 07:31:47169bool FramerVisitorCapturingFrames::OnStreamFrame(const QuicStreamFrame& frame) {
[email protected]583bcbcf2013-10-28 01:51:15170 // Make a copy of the frame and store a copy of underlying string, since
171 // frame.data may not exist outside this callback.
[email protected]5dafdb62013-11-14 01:24:26172 stream_data_.push_back(frame.GetDataAsString());
[email protected]583bcbcf2013-10-28 01:51:15173 QuicStreamFrame frame_copy = frame;
[email protected]5dafdb62013-11-14 01:24:26174 frame_copy.data.Clear();
175 frame_copy.data.Append(const_cast<char*>(stream_data_.back()->data()),
176 stream_data_.back()->size());
[email protected]583bcbcf2013-10-28 01:51:15177 stream_frames_.push_back(frame_copy);
[email protected]9db443912013-02-25 05:27:03178 ++frame_count_;
[email protected]a57e0272013-04-26 07:31:47179 return true;
[email protected]26f3f8e2012-12-13 21:07:19180}
181
[email protected]a57e0272013-04-26 07:31:47182bool FramerVisitorCapturingFrames::OnAckFrame(const QuicAckFrame& frame) {
[email protected]9db443912013-02-25 05:27:03183 ack_.reset(new QuicAckFrame(frame));
184 ++frame_count_;
[email protected]a57e0272013-04-26 07:31:47185 return true;
[email protected]9db443912013-02-25 05:27:03186}
187
[email protected]a57e0272013-04-26 07:31:47188bool FramerVisitorCapturingFrames::OnCongestionFeedbackFrame(
[email protected]26f3f8e2012-12-13 21:07:19189 const QuicCongestionFeedbackFrame& frame) {
190 feedback_.reset(new QuicCongestionFeedbackFrame(frame));
[email protected]9db443912013-02-25 05:27:03191 ++frame_count_;
[email protected]a57e0272013-04-26 07:31:47192 return true;
[email protected]9db443912013-02-25 05:27:03193}
194
[email protected]93dd91f2014-02-27 00:09:03195bool FramerVisitorCapturingFrames::OnStopWaitingFrame(
196 const QuicStopWaitingFrame& frame) {
197 stop_waiting_.reset(new QuicStopWaitingFrame(frame));
198 ++frame_count_;
199 return true;
200}
201
[email protected]a57e0272013-04-26 07:31:47202bool FramerVisitorCapturingFrames::OnRstStreamFrame(
[email protected]9db443912013-02-25 05:27:03203 const QuicRstStreamFrame& frame) {
204 rst_.reset(new QuicRstStreamFrame(frame));
205 ++frame_count_;
[email protected]a57e0272013-04-26 07:31:47206 return true;
[email protected]9db443912013-02-25 05:27:03207}
208
[email protected]a57e0272013-04-26 07:31:47209bool FramerVisitorCapturingFrames::OnConnectionCloseFrame(
[email protected]9db443912013-02-25 05:27:03210 const QuicConnectionCloseFrame& frame) {
211 close_.reset(new QuicConnectionCloseFrame(frame));
212 ++frame_count_;
[email protected]a57e0272013-04-26 07:31:47213 return true;
[email protected]9db443912013-02-25 05:27:03214}
215
[email protected]a57e0272013-04-26 07:31:47216bool FramerVisitorCapturingFrames::OnGoAwayFrame(const QuicGoAwayFrame& frame) {
[email protected]9db443912013-02-25 05:27:03217 goaway_.reset(new QuicGoAwayFrame(frame));
218 ++frame_count_;
[email protected]a57e0272013-04-26 07:31:47219 return true;
[email protected]4e6f0ed2012-11-02 22:15:38220}
221
[email protected]14e8106c2013-03-14 16:25:33222void FramerVisitorCapturingFrames::OnVersionNegotiationPacket(
223 const QuicVersionNegotiationPacket& packet) {
224 version_negotiation_packet_.reset(new QuicVersionNegotiationPacket(packet));
225 frame_count_ = 0;
226}
227
[email protected]fee17f72013-02-03 07:47:41228FramerVisitorCapturingPublicReset::FramerVisitorCapturingPublicReset() {
229}
230
231FramerVisitorCapturingPublicReset::~FramerVisitorCapturingPublicReset() {
232}
233
234void FramerVisitorCapturingPublicReset::OnPublicResetPacket(
235 const QuicPublicResetPacket& public_reset) {
236 public_reset_packet_ = public_reset;
237}
238
[email protected]8d659e22013-01-19 04:26:10239MockConnectionVisitor::MockConnectionVisitor() {
240}
241
242MockConnectionVisitor::~MockConnectionVisitor() {
243}
244
[email protected]9c0b1352012-11-04 00:03:27245MockHelper::MockHelper() {
246}
247
248MockHelper::~MockHelper() {
249}
250
[email protected]97693d12012-11-16 16:05:00251const QuicClock* MockHelper::GetClock() const {
[email protected]9c0b1352012-11-04 00:03:27252 return &clock_;
253}
254
[email protected]9558c5d32012-12-22 00:08:14255QuicRandom* MockHelper::GetRandomGenerator() {
256 return &random_generator_;
257}
258
[email protected]965dbe62013-08-09 21:34:31259QuicAlarm* MockHelper::CreateAlarm(QuicAlarm::Delegate* delegate) {
260 return new TestAlarm(delegate);
261}
262
[email protected]fe053f92013-04-23 20:18:55263void MockHelper::AdvanceTime(QuicTime::Delta delta) {
264 clock_.AdvanceTime(delta);
265}
266
[email protected]c05a6d222013-12-16 19:42:03267MockConnection::MockConnection(bool is_server)
[email protected]3aa9ca72014-02-27 19:39:43268 : QuicConnection(kTestConnectionId,
[email protected]300ccd52014-01-25 08:00:19269 IPEndPoint(TestPeerIPAddress(), kTestPort),
[email protected]c05a6d222013-12-16 19:42:03270 new testing::NiceMock<MockHelper>(),
[email protected]cbd731e2013-10-24 00:20:39271 new testing::NiceMock<MockPacketWriter>(),
[email protected]b007e632013-10-28 08:39:25272 is_server, QuicSupportedVersions()),
[email protected]c05a6d222013-12-16 19:42:03273 writer_(QuicConnectionPeer::GetWriter(this)),
274 helper_(helper()) {
275}
276
277MockConnection::MockConnection(IPEndPoint address,
278 bool is_server)
[email protected]3aa9ca72014-02-27 19:39:43279 : QuicConnection(kTestConnectionId, address,
[email protected]c05a6d222013-12-16 19:42:03280 new testing::NiceMock<MockHelper>(),
281 new testing::NiceMock<MockPacketWriter>(),
282 is_server, QuicSupportedVersions()),
[email protected]2cfc6bb82013-10-27 03:40:44283 writer_(QuicConnectionPeer::GetWriter(this)),
284 helper_(helper()) {
[email protected]044ac2b2012-11-13 21:41:06285}
286
[email protected]3aa9ca72014-02-27 19:39:43287MockConnection::MockConnection(QuicConnectionId connection_id,
[email protected]14e8106c2013-03-14 16:25:33288 bool is_server)
[email protected]3aa9ca72014-02-27 19:39:43289 : QuicConnection(connection_id,
290 IPEndPoint(TestPeerIPAddress(), kTestPort),
[email protected]c05a6d222013-12-16 19:42:03291 new testing::NiceMock<MockHelper>(),
292 new testing::NiceMock<MockPacketWriter>(),
293 is_server, QuicSupportedVersions()),
294 writer_(QuicConnectionPeer::GetWriter(this)),
295 helper_(helper()) {
[email protected]872edd9e2013-01-16 08:51:15296}
297
[email protected]4d640792013-12-18 22:21:08298MockConnection::MockConnection(bool is_server,
299 const QuicVersionVector& supported_versions)
[email protected]3aa9ca72014-02-27 19:39:43300 : QuicConnection(kTestConnectionId,
[email protected]300ccd52014-01-25 08:00:19301 IPEndPoint(TestPeerIPAddress(), kTestPort),
[email protected]4d640792013-12-18 22:21:08302 new testing::NiceMock<MockHelper>(),
303 new testing::NiceMock<MockPacketWriter>(),
304 is_server, supported_versions),
305 writer_(QuicConnectionPeer::GetWriter(this)),
306 helper_(helper()) {
307}
308
[email protected]044ac2b2012-11-13 21:41:06309MockConnection::~MockConnection() {
310}
311
[email protected]fe053f92013-04-23 20:18:55312void MockConnection::AdvanceTime(QuicTime::Delta delta) {
[email protected]fe053f92013-04-23 20:18:55313 static_cast<MockHelper*>(helper())->AdvanceTime(delta);
314}
315
[email protected]c05a6d222013-12-16 19:42:03316PacketSavingConnection::PacketSavingConnection(bool is_server)
317 : MockConnection(is_server) {
[email protected]044ac2b2012-11-13 21:41:06318}
319
[email protected]4d640792013-12-18 22:21:08320PacketSavingConnection::PacketSavingConnection(
321 bool is_server,
322 const QuicVersionVector& supported_versions)
323 : MockConnection(is_server, supported_versions) {
324}
325
[email protected]044ac2b2012-11-13 21:41:06326PacketSavingConnection::~PacketSavingConnection() {
[email protected]701bc892013-01-17 04:51:54327 STLDeleteElements(&packets_);
[email protected]2532de12013-05-09 12:29:33328 STLDeleteElements(&encrypted_packets_);
[email protected]044ac2b2012-11-13 21:41:06329}
330
[email protected]fee17f72013-02-03 07:47:41331bool PacketSavingConnection::SendOrQueuePacket(
[email protected]8ba81212013-05-03 13:11:48332 EncryptionLevel level,
[email protected]ec86d5462013-11-17 16:04:49333 const SerializedPacket& packet,
334 TransmissionType transmission_type) {
335 packets_.push_back(packet.packet);
[email protected]c5e1aca2014-01-30 04:03:04336 QuicEncryptedPacket* encrypted = QuicConnectionPeer::GetFramer(this)->
337 EncryptPacket(level, packet.sequence_number, *packet.packet);
[email protected]2532de12013-05-09 12:29:33338 encrypted_packets_.push_back(encrypted);
[email protected]044ac2b2012-11-13 21:41:06339 return true;
340}
341
[email protected]c05a6d222013-12-16 19:42:03342MockSession::MockSession(QuicConnection* connection)
343 : QuicSession(connection, DefaultQuicConfig()) {
[email protected]93dd91f2014-02-27 00:09:03344 ON_CALL(*this, WritevData(_, _, _, _, _))
[email protected]cff7b7b2013-01-11 08:49:07345 .WillByDefault(testing::Return(QuicConsumedData(0, false)));
[email protected]044ac2b2012-11-13 21:41:06346}
347
348MockSession::~MockSession() {
349}
350
[email protected]899951652013-05-16 12:52:39351TestSession::TestSession(QuicConnection* connection,
[email protected]c05a6d222013-12-16 19:42:03352 const QuicConfig& config)
353 : QuicSession(connection, config),
[email protected]2532de12013-05-09 12:29:33354 crypto_stream_(NULL) {
355}
356
[email protected]b064310782013-05-30 21:12:17357TestSession::~TestSession() {}
[email protected]2532de12013-05-09 12:29:33358
359void TestSession::SetCryptoStream(QuicCryptoStream* stream) {
360 crypto_stream_ = stream;
361}
362
363QuicCryptoStream* TestSession::GetCryptoStream() {
364 return crypto_stream_;
365}
366
[email protected]90f62f092014-03-24 02:41:23367TestClientSession::TestClientSession(QuicConnection* connection,
368 const QuicConfig& config)
369 : QuicClientSessionBase(connection, config),
370 crypto_stream_(NULL) {
371}
372
373TestClientSession::~TestClientSession() {}
374
375void TestClientSession::SetCryptoStream(QuicCryptoStream* stream) {
376 crypto_stream_ = stream;
377}
378
379QuicCryptoStream* TestClientSession::GetCryptoStream() {
380 return crypto_stream_;
381}
382
[email protected]cbd731e2013-10-24 00:20:39383MockPacketWriter::MockPacketWriter() {
384}
385
386MockPacketWriter::~MockPacketWriter() {
387}
388
[email protected]fee17f72013-02-03 07:47:41389MockSendAlgorithm::MockSendAlgorithm() {
[email protected]8d659e22013-01-19 04:26:10390}
391
[email protected]fee17f72013-02-03 07:47:41392MockSendAlgorithm::~MockSendAlgorithm() {
[email protected]8d659e22013-01-19 04:26:10393}
394
[email protected]3aa9ca72014-02-27 19:39:43395MockLossAlgorithm::MockLossAlgorithm() {
396}
397
398MockLossAlgorithm::~MockLossAlgorithm() {
399}
400
[email protected]97cf3022013-09-05 14:30:16401MockAckNotifierDelegate::MockAckNotifierDelegate() {
402}
403
404MockAckNotifierDelegate::~MockAckNotifierDelegate() {
405}
406
[email protected]8b37a092012-10-18 21:53:49407namespace {
408
409string HexDumpWithMarks(const char* data, int length,
410 const bool* marks, int mark_length) {
411 static const char kHexChars[] = "0123456789abcdef";
412 static const int kColumns = 4;
413
414 const int kSizeLimit = 1024;
415 if (length > kSizeLimit || mark_length > kSizeLimit) {
416 LOG(ERROR) << "Only dumping first " << kSizeLimit << " bytes.";
417 length = min(length, kSizeLimit);
418 mark_length = min(mark_length, kSizeLimit);
419 }
420
421 string hex;
422 for (const char* row = data; length > 0;
423 row += kColumns, length -= kColumns) {
424 for (const char *p = row; p < row + 4; ++p) {
425 if (p < row + length) {
426 const bool mark =
427 (marks && (p - data) < mark_length && marks[p - data]);
428 hex += mark ? '*' : ' ';
429 hex += kHexChars[(*p & 0xf0) >> 4];
430 hex += kHexChars[*p & 0x0f];
431 hex += mark ? '*' : ' ';
432 } else {
433 hex += " ";
434 }
435 }
436 hex = hex + " ";
437
438 for (const char *p = row; p < row + 4 && p < row + length; ++p)
439 hex += (*p >= 0x20 && *p <= 0x7f) ? (*p) : '.';
440
441 hex = hex + '\n';
442 }
443 return hex;
444}
445
446} // namespace
447
[email protected]300ccd52014-01-25 08:00:19448IPAddressNumber TestPeerIPAddress() { return Loopback4(); }
449
[email protected]b007e632013-10-28 08:39:25450QuicVersion QuicVersionMax() { return QuicSupportedVersions().front(); }
451
452QuicVersion QuicVersionMin() { return QuicSupportedVersions().back(); }
453
[email protected]c05a6d222013-12-16 19:42:03454IPAddressNumber Loopback4() {
[email protected]300ccd52014-01-25 08:00:19455 IPAddressNumber addr;
456 CHECK(ParseIPLiteralToNumber("127.0.0.1", &addr));
[email protected]c05a6d222013-12-16 19:42:03457 return addr;
458}
459
[email protected]ffc34bf2014-03-07 02:42:02460QuicEncryptedPacket* ConstructEncryptedPacket(
461 QuicConnectionId connection_id,
462 bool version_flag,
463 bool reset_flag,
464 QuicPacketSequenceNumber sequence_number,
465 const string& data) {
466 QuicPacketHeader header;
467 header.public_header.connection_id = connection_id;
468 header.public_header.connection_id_length = PACKET_8BYTE_CONNECTION_ID;
469 header.public_header.version_flag = version_flag;
470 header.public_header.reset_flag = reset_flag;
471 header.public_header.sequence_number_length = PACKET_6BYTE_SEQUENCE_NUMBER;
472 header.packet_sequence_number = sequence_number;
473 header.entropy_flag = false;
474 header.entropy_hash = 0;
475 header.fec_flag = false;
476 header.is_in_fec_group = NOT_IN_FEC_GROUP;
477 header.fec_group = 0;
478 QuicStreamFrame stream_frame(1, false, 0, MakeIOVector(data));
479 QuicFrame frame(&stream_frame);
480 QuicFrames frames;
481 frames.push_back(frame);
482 QuicFramer framer(QuicSupportedVersions(), QuicTime::Zero(), false);
483 scoped_ptr<QuicPacket> packet(
484 framer.BuildUnsizedDataPacket(header, frames).packet);
485 EXPECT_TRUE(packet != NULL);
486 QuicEncryptedPacket* encrypted = framer.EncryptPacket(ENCRYPTION_NONE,
487 sequence_number,
488 *packet);
489 EXPECT_TRUE(encrypted != NULL);
490 return encrypted;
491}
492
[email protected]8b37a092012-10-18 21:53:49493void CompareCharArraysWithHexError(
494 const string& description,
495 const char* actual,
496 const int actual_len,
497 const char* expected,
498 const int expected_len) {
[email protected]b007e632013-10-28 08:39:25499 EXPECT_EQ(actual_len, expected_len);
[email protected]8b37a092012-10-18 21:53:49500 const int min_len = min(actual_len, expected_len);
501 const int max_len = max(actual_len, expected_len);
[email protected]4356f0f2013-04-07 00:58:17502 scoped_ptr<bool[]> marks(new bool[max_len]);
[email protected]8b37a092012-10-18 21:53:49503 bool identical = (actual_len == expected_len);
504 for (int i = 0; i < min_len; ++i) {
505 if (actual[i] != expected[i]) {
506 marks[i] = true;
507 identical = false;
508 } else {
509 marks[i] = false;
510 }
511 }
512 for (int i = min_len; i < max_len; ++i) {
513 marks[i] = true;
514 }
515 if (identical) return;
516 ADD_FAILURE()
517 << "Description:\n"
518 << description
519 << "\n\nExpected:\n"
520 << HexDumpWithMarks(expected, expected_len, marks.get(), max_len)
521 << "\nActual:\n"
522 << HexDumpWithMarks(actual, actual_len, marks.get(), max_len);
523}
524
[email protected]b12764d2013-12-02 22:28:30525bool DecodeHexString(const base::StringPiece& hex, std::string* bytes) {
526 bytes->clear();
527 if (hex.empty())
528 return true;
529 std::vector<uint8> v;
530 if (!base::HexStringToBytes(hex.as_string(), &v))
531 return false;
532 if (!v.empty())
533 bytes->assign(reinterpret_cast<const char*>(&v[0]), v.size());
534 return true;
535}
536
[email protected]d3d15bf2013-01-30 02:51:54537static QuicPacket* ConstructPacketFromHandshakeMessage(
[email protected]3aa9ca72014-02-27 19:39:43538 QuicConnectionId connection_id,
[email protected]14e8106c2013-03-14 16:25:33539 const CryptoHandshakeMessage& message,
540 bool should_include_version) {
[email protected]8b37a092012-10-18 21:53:49541 CryptoFramer crypto_framer;
[email protected]dc2cc742012-10-21 13:56:13542 scoped_ptr<QuicData> data(crypto_framer.ConstructHandshakeMessage(message));
[email protected]b007e632013-10-28 08:39:25543 QuicFramer quic_framer(QuicSupportedVersions(), QuicTime::Zero(), false);
[email protected]8b37a092012-10-18 21:53:49544
545 QuicPacketHeader header;
[email protected]3aa9ca72014-02-27 19:39:43546 header.public_header.connection_id = connection_id;
[email protected]9db443912013-02-25 05:27:03547 header.public_header.reset_flag = false;
[email protected]14e8106c2013-03-14 16:25:33548 header.public_header.version_flag = should_include_version;
[email protected]8b37a092012-10-18 21:53:49549 header.packet_sequence_number = 1;
[email protected]9db443912013-02-25 05:27:03550 header.entropy_flag = false;
551 header.entropy_hash = 0;
552 header.fec_flag = false;
[email protected]8b37a092012-10-18 21:53:49553 header.fec_group = 0;
554
[email protected]be24ab22012-10-22 03:01:52555 QuicStreamFrame stream_frame(kCryptoStreamId, false, 0,
[email protected]5dafdb62013-11-14 01:24:26556 MakeIOVector(data->AsStringPiece()));
[email protected]8b37a092012-10-18 21:53:49557
[email protected]be24ab22012-10-22 03:01:52558 QuicFrame frame(&stream_frame);
559 QuicFrames frames;
560 frames.push_back(frame);
[email protected]3e60db82013-08-05 19:43:06561 return quic_framer.BuildUnsizedDataPacket(header, frames).packet;
[email protected]8b37a092012-10-18 21:53:49562}
563
[email protected]3aa9ca72014-02-27 19:39:43564QuicPacket* ConstructHandshakePacket(QuicConnectionId connection_id,
565 QuicTag tag) {
[email protected]d3d15bf2013-01-30 02:51:54566 CryptoHandshakeMessage message;
[email protected]ccc66e8a2013-03-26 08:26:14567 message.set_tag(tag);
[email protected]3aa9ca72014-02-27 19:39:43568 return ConstructPacketFromHandshakeMessage(connection_id, message, false);
[email protected]d3d15bf2013-01-30 02:51:54569}
570
[email protected]ea825e02013-08-21 18:12:45571size_t GetPacketLengthForOneStream(
572 QuicVersion version,
573 bool include_version,
574 QuicSequenceNumberLength sequence_number_length,
575 InFecGroup is_in_fec_group,
576 size_t* payload_length) {
[email protected]f62262b2013-07-05 20:57:30577 *payload_length = 1;
578 const size_t stream_length =
[email protected]5dafdb62013-11-14 01:24:26579 NullEncrypter().GetCiphertextSize(*payload_length) +
[email protected]b064310782013-05-30 21:12:17580 QuicPacketCreator::StreamFramePacketOverhead(
[email protected]3aa9ca72014-02-27 19:39:43581 version, PACKET_8BYTE_CONNECTION_ID, include_version,
[email protected]ea825e02013-08-21 18:12:45582 sequence_number_length, is_in_fec_group);
[email protected]5dafdb62013-11-14 01:24:26583 const size_t ack_length = NullEncrypter().GetCiphertextSize(
[email protected]8e01c062013-10-31 07:35:31584 QuicFramer::GetMinAckFrameSize(
585 version, sequence_number_length, PACKET_1BYTE_SEQUENCE_NUMBER)) +
[email protected]3aa9ca72014-02-27 19:39:43586 GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID, include_version,
[email protected]ea825e02013-08-21 18:12:45587 sequence_number_length, is_in_fec_group);
[email protected]f62262b2013-07-05 20:57:30588 if (stream_length < ack_length) {
589 *payload_length = 1 + ack_length - stream_length;
590 }
591
[email protected]5dafdb62013-11-14 01:24:26592 return NullEncrypter().GetCiphertextSize(*payload_length) +
[email protected]f62262b2013-07-05 20:57:30593 QuicPacketCreator::StreamFramePacketOverhead(
[email protected]3aa9ca72014-02-27 19:39:43594 version, PACKET_8BYTE_CONNECTION_ID, include_version,
[email protected]ea825e02013-08-21 18:12:45595 sequence_number_length, is_in_fec_group);
[email protected]5351cc4b2013-03-03 07:22:41596}
597
[email protected]8e01c062013-10-31 07:35:31598TestEntropyCalculator::TestEntropyCalculator() { }
599
600TestEntropyCalculator::~TestEntropyCalculator() { }
601
[email protected]48878092013-07-26 14:51:56602QuicPacketEntropyHash TestEntropyCalculator::EntropyHash(
[email protected]9db443912013-02-25 05:27:03603 QuicPacketSequenceNumber sequence_number) const {
604 return 1u;
605}
606
[email protected]8e01c062013-10-31 07:35:31607MockEntropyCalculator::MockEntropyCalculator() { }
608
609MockEntropyCalculator::~MockEntropyCalculator() { }
610
[email protected]b064310782013-05-30 21:12:17611QuicConfig DefaultQuicConfig() {
612 QuicConfig config;
613 config.SetDefaults();
614 return config;
615}
616
[email protected]4d640792013-12-18 22:21:08617QuicVersionVector SupportedVersions(QuicVersion version) {
618 QuicVersionVector versions;
619 versions.push_back(version);
620 return versions;
621}
622
[email protected]8b37a092012-10-18 21:53:49623} // namespace test
[email protected]8b37a092012-10-18 21:53:49624} // namespace net