1 // 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.
5 #include "net/quic/test_tools/quic_test_utils.h"
7 #include "base/stl_util.h"
8 #include "base/strings/string_number_conversions.h"
9 #include "net/quic/crypto/crypto_framer.h"
10 #include "net/quic/crypto/crypto_handshake.h"
11 #include "net/quic/crypto/crypto_utils.h"
12 #include "net/quic/crypto/null_encrypter.h"
13 #include "net/quic/crypto/quic_decrypter.h"
14 #include "net/quic/crypto/quic_encrypter.h"
15 #include "net/quic/quic_framer.h"
16 #include "net/quic/quic_packet_creator.h"
17 #include "net/quic/quic_utils.h"
18 #include "net/quic/test_tools/quic_connection_peer.h"
19 #include "net/spdy/spdy_frame_builder.h"
21 using base::StringPiece;
31 // No-op alarm implementation used by MockHelper.
32 class TestAlarm : public QuicAlarm {
34 explicit TestAlarm(QuicAlarm::Delegate* delegate)
35 : QuicAlarm(delegate) {
38 virtual void SetImpl() OVERRIDE {}
39 virtual void CancelImpl() OVERRIDE {}
44 MockFramerVisitor::MockFramerVisitor() {
45 // By default, we want to accept packets.
46 ON_CALL(*this, OnProtocolVersionMismatch(_))
47 .WillByDefault(testing::Return(false));
49 // By default, we want to accept packets.
50 ON_CALL(*this, OnUnauthenticatedHeader(_))
51 .WillByDefault(testing::Return(true));
53 ON_CALL(*this, OnUnauthenticatedPublicHeader(_))
54 .WillByDefault(testing::Return(true));
56 ON_CALL(*this, OnPacketHeader(_))
57 .WillByDefault(testing::Return(true));
59 ON_CALL(*this, OnStreamFrame(_))
60 .WillByDefault(testing::Return(true));
62 ON_CALL(*this, OnAckFrame(_))
63 .WillByDefault(testing::Return(true));
65 ON_CALL(*this, OnCongestionFeedbackFrame(_))
66 .WillByDefault(testing::Return(true));
68 ON_CALL(*this, OnRstStreamFrame(_))
69 .WillByDefault(testing::Return(true));
71 ON_CALL(*this, OnConnectionCloseFrame(_))
72 .WillByDefault(testing::Return(true));
74 ON_CALL(*this, OnGoAwayFrame(_))
75 .WillByDefault(testing::Return(true));
78 MockFramerVisitor::~MockFramerVisitor() {
81 bool NoOpFramerVisitor::OnProtocolVersionMismatch(QuicVersion version) {
85 bool NoOpFramerVisitor::OnUnauthenticatedPublicHeader(
86 const QuicPacketPublicHeader& header) {
90 bool NoOpFramerVisitor::OnUnauthenticatedHeader(
91 const QuicPacketHeader& header) {
95 bool NoOpFramerVisitor::OnPacketHeader(const QuicPacketHeader& header) {
99 bool NoOpFramerVisitor::OnStreamFrame(const QuicStreamFrame& frame) {
103 bool NoOpFramerVisitor::OnAckFrame(const QuicAckFrame& frame) {
107 bool NoOpFramerVisitor::OnCongestionFeedbackFrame(
108 const QuicCongestionFeedbackFrame& frame) {
112 bool NoOpFramerVisitor::OnRstStreamFrame(
113 const QuicRstStreamFrame& frame) {
117 bool NoOpFramerVisitor::OnConnectionCloseFrame(
118 const QuicConnectionCloseFrame& frame) {
122 bool NoOpFramerVisitor::OnGoAwayFrame(const QuicGoAwayFrame& frame) {
126 FramerVisitorCapturingFrames::FramerVisitorCapturingFrames() : frame_count_(0) {
129 FramerVisitorCapturingFrames::~FramerVisitorCapturingFrames() {
133 void FramerVisitorCapturingFrames::Reset() {
134 STLDeleteElements(&stream_data_);
135 stream_frames_.clear();
142 version_negotiation_packet_.reset();
145 bool FramerVisitorCapturingFrames::OnPacketHeader(
146 const QuicPacketHeader& header) {
152 bool FramerVisitorCapturingFrames::OnStreamFrame(const QuicStreamFrame& frame) {
153 // Make a copy of the frame and store a copy of underlying string, since
154 // frame.data may not exist outside this callback.
155 stream_data_.push_back(frame.GetDataAsString());
156 QuicStreamFrame frame_copy = frame;
157 frame_copy.data.Clear();
158 frame_copy.data.Append(const_cast<char*>(stream_data_.back()->data()),
159 stream_data_.back()->size());
160 stream_frames_.push_back(frame_copy);
165 bool FramerVisitorCapturingFrames::OnAckFrame(const QuicAckFrame& frame) {
166 ack_.reset(new QuicAckFrame(frame));
171 bool FramerVisitorCapturingFrames::OnCongestionFeedbackFrame(
172 const QuicCongestionFeedbackFrame& frame) {
173 feedback_.reset(new QuicCongestionFeedbackFrame(frame));
178 bool FramerVisitorCapturingFrames::OnRstStreamFrame(
179 const QuicRstStreamFrame& frame) {
180 rst_.reset(new QuicRstStreamFrame(frame));
185 bool FramerVisitorCapturingFrames::OnConnectionCloseFrame(
186 const QuicConnectionCloseFrame& frame) {
187 close_.reset(new QuicConnectionCloseFrame(frame));
192 bool FramerVisitorCapturingFrames::OnGoAwayFrame(const QuicGoAwayFrame& frame) {
193 goaway_.reset(new QuicGoAwayFrame(frame));
198 void FramerVisitorCapturingFrames::OnVersionNegotiationPacket(
199 const QuicVersionNegotiationPacket& packet) {
200 version_negotiation_packet_.reset(new QuicVersionNegotiationPacket(packet));
204 FramerVisitorCapturingPublicReset::FramerVisitorCapturingPublicReset() {
207 FramerVisitorCapturingPublicReset::~FramerVisitorCapturingPublicReset() {
210 void FramerVisitorCapturingPublicReset::OnPublicResetPacket(
211 const QuicPublicResetPacket& public_reset) {
212 public_reset_packet_ = public_reset;
215 MockConnectionVisitor::MockConnectionVisitor() {
218 MockConnectionVisitor::~MockConnectionVisitor() {
221 MockHelper::MockHelper() {
224 MockHelper::~MockHelper() {
227 const QuicClock* MockHelper::GetClock() const {
231 QuicRandom* MockHelper::GetRandomGenerator() {
232 return &random_generator_;
235 QuicAlarm* MockHelper::CreateAlarm(QuicAlarm::Delegate* delegate) {
236 return new TestAlarm(delegate);
239 void MockHelper::AdvanceTime(QuicTime::Delta delta) {
240 clock_.AdvanceTime(delta);
243 MockConnection::MockConnection(bool is_server)
244 : QuicConnection(kTestGuid,
245 IPEndPoint(TestPeerIPAddress(), kTestPort),
246 new testing::NiceMock<MockHelper>(),
247 new testing::NiceMock<MockPacketWriter>(),
248 is_server, QuicSupportedVersions()),
249 writer_(QuicConnectionPeer::GetWriter(this)),
253 MockConnection::MockConnection(IPEndPoint address,
255 : QuicConnection(kTestGuid, address,
256 new testing::NiceMock<MockHelper>(),
257 new testing::NiceMock<MockPacketWriter>(),
258 is_server, QuicSupportedVersions()),
259 writer_(QuicConnectionPeer::GetWriter(this)),
263 MockConnection::MockConnection(QuicGuid guid,
265 : QuicConnection(guid, IPEndPoint(TestPeerIPAddress(), kTestPort),
266 new testing::NiceMock<MockHelper>(),
267 new testing::NiceMock<MockPacketWriter>(),
268 is_server, QuicSupportedVersions()),
269 writer_(QuicConnectionPeer::GetWriter(this)),
273 MockConnection::MockConnection(bool is_server,
274 const QuicVersionVector& supported_versions)
275 : QuicConnection(kTestGuid,
276 IPEndPoint(TestPeerIPAddress(), kTestPort),
277 new testing::NiceMock<MockHelper>(),
278 new testing::NiceMock<MockPacketWriter>(),
279 is_server, supported_versions),
280 writer_(QuicConnectionPeer::GetWriter(this)),
284 MockConnection::~MockConnection() {
287 void MockConnection::AdvanceTime(QuicTime::Delta delta) {
288 static_cast<MockHelper*>(helper())->AdvanceTime(delta);
291 PacketSavingConnection::PacketSavingConnection(bool is_server)
292 : MockConnection(is_server) {
295 PacketSavingConnection::PacketSavingConnection(
297 const QuicVersionVector& supported_versions)
298 : MockConnection(is_server, supported_versions) {
301 PacketSavingConnection::~PacketSavingConnection() {
302 STLDeleteElements(&packets_);
303 STLDeleteElements(&encrypted_packets_);
306 bool PacketSavingConnection::SendOrQueuePacket(
307 EncryptionLevel level,
308 const SerializedPacket& packet,
309 TransmissionType transmission_type) {
310 packets_.push_back(packet.packet);
311 QuicEncryptedPacket* encrypted = QuicConnectionPeer::GetFramer(this)->
312 EncryptPacket(level, packet.sequence_number, *packet.packet);
313 encrypted_packets_.push_back(encrypted);
317 MockSession::MockSession(QuicConnection* connection)
318 : QuicSession(connection, DefaultQuicConfig()) {
319 ON_CALL(*this, WritevData(_, _, _, _, _, _))
320 .WillByDefault(testing::Return(QuicConsumedData(0, false)));
323 MockSession::~MockSession() {
326 TestSession::TestSession(QuicConnection* connection,
327 const QuicConfig& config)
328 : QuicSession(connection, config),
329 crypto_stream_(NULL) {
332 TestSession::~TestSession() {}
334 void TestSession::SetCryptoStream(QuicCryptoStream* stream) {
335 crypto_stream_ = stream;
338 QuicCryptoStream* TestSession::GetCryptoStream() {
339 return crypto_stream_;
342 MockPacketWriter::MockPacketWriter() {
345 MockPacketWriter::~MockPacketWriter() {
348 MockSendAlgorithm::MockSendAlgorithm() {
351 MockSendAlgorithm::~MockSendAlgorithm() {
354 MockAckNotifierDelegate::MockAckNotifierDelegate() {
357 MockAckNotifierDelegate::~MockAckNotifierDelegate() {
362 string HexDumpWithMarks(const char* data, int length,
363 const bool* marks, int mark_length) {
364 static const char kHexChars[] = "0123456789abcdef";
365 static const int kColumns = 4;
367 const int kSizeLimit = 1024;
368 if (length > kSizeLimit || mark_length > kSizeLimit) {
369 LOG(ERROR) << "Only dumping first " << kSizeLimit << " bytes.";
370 length = min(length, kSizeLimit);
371 mark_length = min(mark_length, kSizeLimit);
375 for (const char* row = data; length > 0;
376 row += kColumns, length -= kColumns) {
377 for (const char *p = row; p < row + 4; ++p) {
378 if (p < row + length) {
380 (marks && (p - data) < mark_length && marks[p - data]);
381 hex += mark ? '*' : ' ';
382 hex += kHexChars[(*p & 0xf0) >> 4];
383 hex += kHexChars[*p & 0x0f];
384 hex += mark ? '*' : ' ';
391 for (const char *p = row; p < row + 4 && p < row + length; ++p)
392 hex += (*p >= 0x20 && *p <= 0x7f) ? (*p) : '.';
401 IPAddressNumber TestPeerIPAddress() { return Loopback4(); }
403 QuicVersion QuicVersionMax() { return QuicSupportedVersions().front(); }
405 QuicVersion QuicVersionMin() { return QuicSupportedVersions().back(); }
407 IPAddressNumber Loopback4() {
408 IPAddressNumber addr;
409 CHECK(ParseIPLiteralToNumber("127.0.0.1", &addr));
413 void CompareCharArraysWithHexError(
414 const string& description,
416 const int actual_len,
417 const char* expected,
418 const int expected_len) {
419 EXPECT_EQ(actual_len, expected_len);
420 const int min_len = min(actual_len, expected_len);
421 const int max_len = max(actual_len, expected_len);
422 scoped_ptr<bool[]> marks(new bool[max_len]);
423 bool identical = (actual_len == expected_len);
424 for (int i = 0; i < min_len; ++i) {
425 if (actual[i] != expected[i]) {
432 for (int i = min_len; i < max_len; ++i) {
435 if (identical) return;
440 << HexDumpWithMarks(expected, expected_len, marks.get(), max_len)
442 << HexDumpWithMarks(actual, actual_len, marks.get(), max_len);
445 bool DecodeHexString(const base::StringPiece& hex, std::string* bytes) {
449 std::vector<uint8> v;
450 if (!base::HexStringToBytes(hex.as_string(), &v))
453 bytes->assign(reinterpret_cast<const char*>(&v[0]), v.size());
457 static QuicPacket* ConstructPacketFromHandshakeMessage(
459 const CryptoHandshakeMessage& message,
460 bool should_include_version) {
461 CryptoFramer crypto_framer;
462 scoped_ptr<QuicData> data(crypto_framer.ConstructHandshakeMessage(message));
463 QuicFramer quic_framer(QuicSupportedVersions(), QuicTime::Zero(), false);
465 QuicPacketHeader header;
466 header.public_header.guid = guid;
467 header.public_header.reset_flag = false;
468 header.public_header.version_flag = should_include_version;
469 header.packet_sequence_number = 1;
470 header.entropy_flag = false;
471 header.entropy_hash = 0;
472 header.fec_flag = false;
473 header.fec_group = 0;
475 QuicStreamFrame stream_frame(kCryptoStreamId, false, 0,
476 MakeIOVector(data->AsStringPiece()));
478 QuicFrame frame(&stream_frame);
480 frames.push_back(frame);
481 return quic_framer.BuildUnsizedDataPacket(header, frames).packet;
484 QuicPacket* ConstructHandshakePacket(QuicGuid guid, QuicTag tag) {
485 CryptoHandshakeMessage message;
486 message.set_tag(tag);
487 return ConstructPacketFromHandshakeMessage(guid, message, false);
490 size_t GetPacketLengthForOneStream(
492 bool include_version,
493 QuicSequenceNumberLength sequence_number_length,
494 InFecGroup is_in_fec_group,
495 size_t* payload_length) {
497 const size_t stream_length =
498 NullEncrypter().GetCiphertextSize(*payload_length) +
499 QuicPacketCreator::StreamFramePacketOverhead(
500 version, PACKET_8BYTE_GUID, include_version,
501 sequence_number_length, is_in_fec_group);
502 const size_t ack_length = NullEncrypter().GetCiphertextSize(
503 QuicFramer::GetMinAckFrameSize(
504 version, sequence_number_length, PACKET_1BYTE_SEQUENCE_NUMBER)) +
505 GetPacketHeaderSize(PACKET_8BYTE_GUID, include_version,
506 sequence_number_length, is_in_fec_group);
507 if (stream_length < ack_length) {
508 *payload_length = 1 + ack_length - stream_length;
511 return NullEncrypter().GetCiphertextSize(*payload_length) +
512 QuicPacketCreator::StreamFramePacketOverhead(
513 version, PACKET_8BYTE_GUID, include_version,
514 sequence_number_length, is_in_fec_group);
517 TestEntropyCalculator::TestEntropyCalculator() { }
519 TestEntropyCalculator::~TestEntropyCalculator() { }
521 QuicPacketEntropyHash TestEntropyCalculator::EntropyHash(
522 QuicPacketSequenceNumber sequence_number) const {
526 MockEntropyCalculator::MockEntropyCalculator() { }
528 MockEntropyCalculator::~MockEntropyCalculator() { }
530 QuicConfig DefaultQuicConfig() {
532 config.SetDefaults();
536 QuicVersionVector SupportedVersions(QuicVersion version) {
537 QuicVersionVector versions;
538 versions.push_back(version);
542 bool TestDecompressorVisitor::OnDecompressedData(StringPiece data) {
543 data.AppendToString(&data_);
547 void TestDecompressorVisitor::OnDecompressionError() {