blob: 6fc7170f14dfd546412c1b69e342eae986ad5d5c [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]8b37a092012-10-18 21:53:497#include "net/quic/crypto/crypto_framer.h"
8
9using std::max;
10using std::min;
11using std::string;
12
13namespace net {
[email protected]044ac2b2012-11-13 21:41:0614
[email protected]8b37a092012-10-18 21:53:4915namespace test {
16
17MockFramerVisitor::MockFramerVisitor() {
18 // By default, we want to accept packets.
19 ON_CALL(*this, OnPacketHeader(testing::_))
20 .WillByDefault(testing::Return(true));
21}
22
[email protected]044ac2b2012-11-13 21:41:0623MockFramerVisitor::~MockFramerVisitor() {
24}
[email protected]8b37a092012-10-18 21:53:4925
26bool NoOpFramerVisitor::OnPacketHeader(const QuicPacketHeader& header) {
27 return true;
28}
29
[email protected]134e5c32012-12-12 19:20:3630FramerVisitorCapturingAcks::FramerVisitorCapturingAcks() {
31}
32
[email protected]4e6f0ed2012-11-02 22:15:3833bool FramerVisitorCapturingAcks::OnPacketHeader(
34 const QuicPacketHeader& header) {
35 header_ = header;
36 return true;
37}
38
39void FramerVisitorCapturingAcks::OnAckFrame(const QuicAckFrame& frame) {
40 frame_ = frame;
41}
42
[email protected]9c0b1352012-11-04 00:03:2743MockHelper::MockHelper() {
44}
45
46MockHelper::~MockHelper() {
47}
48
[email protected]97693d12012-11-16 16:05:0049const QuicClock* MockHelper::GetClock() const {
[email protected]9c0b1352012-11-04 00:03:2750 return &clock_;
51}
52
[email protected]4e6f0ed2012-11-02 22:15:3853MockConnectionVisitor::MockConnectionVisitor() {
54}
55
56MockConnectionVisitor::~MockConnectionVisitor() {
57}
58
59MockScheduler::MockScheduler()
60 : QuicSendScheduler(NULL, kFixRate) {
61}
62
63MockScheduler::~MockScheduler() {
64}
65
[email protected]044ac2b2012-11-13 21:41:0666MockConnection::MockConnection(QuicGuid guid, IPEndPoint address)
[email protected]e13201d82012-12-12 05:00:3267 : QuicConnection(guid, address, new MockHelper()),
68 helper_(helper()) {
[email protected]044ac2b2012-11-13 21:41:0669}
70
71MockConnection::~MockConnection() {
72}
73
74PacketSavingConnection::PacketSavingConnection(QuicGuid guid,
75 IPEndPoint address)
76 : MockConnection(guid, address) {
77}
78
79PacketSavingConnection::~PacketSavingConnection() {
80}
81
82bool PacketSavingConnection::SendPacket(QuicPacketSequenceNumber number,
83 QuicPacket* packet,
84 bool should_resend,
85 bool force,
86 bool is_retransmit) {
87 packets_.push_back(packet);
88 return true;
89}
90
91MockSession::MockSession(QuicConnection* connection, bool is_server)
92 : QuicSession(connection, is_server) {
93}
94
95MockSession::~MockSession() {
96}
97
[email protected]8b37a092012-10-18 21:53:4998namespace {
99
100string HexDumpWithMarks(const char* data, int length,
101 const bool* marks, int mark_length) {
102 static const char kHexChars[] = "0123456789abcdef";
103 static const int kColumns = 4;
104
105 const int kSizeLimit = 1024;
106 if (length > kSizeLimit || mark_length > kSizeLimit) {
107 LOG(ERROR) << "Only dumping first " << kSizeLimit << " bytes.";
108 length = min(length, kSizeLimit);
109 mark_length = min(mark_length, kSizeLimit);
110 }
111
112 string hex;
113 for (const char* row = data; length > 0;
114 row += kColumns, length -= kColumns) {
115 for (const char *p = row; p < row + 4; ++p) {
116 if (p < row + length) {
117 const bool mark =
118 (marks && (p - data) < mark_length && marks[p - data]);
119 hex += mark ? '*' : ' ';
120 hex += kHexChars[(*p & 0xf0) >> 4];
121 hex += kHexChars[*p & 0x0f];
122 hex += mark ? '*' : ' ';
123 } else {
124 hex += " ";
125 }
126 }
127 hex = hex + " ";
128
129 for (const char *p = row; p < row + 4 && p < row + length; ++p)
130 hex += (*p >= 0x20 && *p <= 0x7f) ? (*p) : '.';
131
132 hex = hex + '\n';
133 }
134 return hex;
135}
136
137} // namespace
138
139void CompareCharArraysWithHexError(
140 const string& description,
141 const char* actual,
142 const int actual_len,
143 const char* expected,
144 const int expected_len) {
145 const int min_len = min(actual_len, expected_len);
146 const int max_len = max(actual_len, expected_len);
147 scoped_array<bool> marks(new bool[max_len]);
148 bool identical = (actual_len == expected_len);
149 for (int i = 0; i < min_len; ++i) {
150 if (actual[i] != expected[i]) {
151 marks[i] = true;
152 identical = false;
153 } else {
154 marks[i] = false;
155 }
156 }
157 for (int i = min_len; i < max_len; ++i) {
158 marks[i] = true;
159 }
160 if (identical) return;
161 ADD_FAILURE()
162 << "Description:\n"
163 << description
164 << "\n\nExpected:\n"
165 << HexDumpWithMarks(expected, expected_len, marks.get(), max_len)
166 << "\nActual:\n"
167 << HexDumpWithMarks(actual, actual_len, marks.get(), max_len);
168}
169
170void CompareQuicDataWithHexError(
171 const string& description,
172 QuicData* actual,
173 QuicData* expected) {
174 CompareCharArraysWithHexError(
175 description,
176 actual->data(), actual->length(),
177 expected->data(), expected->length());
178}
179
180QuicPacket* ConstructHandshakePacket(QuicGuid guid, CryptoTag tag) {
181 CryptoHandshakeMessage message;
182 message.tag = tag;
183 CryptoFramer crypto_framer;
[email protected]dc2cc742012-10-21 13:56:13184 scoped_ptr<QuicData> data(crypto_framer.ConstructHandshakeMessage(message));
[email protected]8b37a092012-10-18 21:53:49185 QuicFramer quic_framer(QuicDecrypter::Create(kNULL),
186 QuicEncrypter::Create(kNULL));
187
188 QuicPacketHeader header;
189 header.guid = guid;
[email protected]8b37a092012-10-18 21:53:49190 header.packet_sequence_number = 1;
[email protected]8b37a092012-10-18 21:53:49191 header.flags = PACKET_FLAGS_NONE;
192 header.fec_group = 0;
193
[email protected]be24ab22012-10-22 03:01:52194 QuicStreamFrame stream_frame(kCryptoStreamId, false, 0,
[email protected]044ac2b2012-11-13 21:41:06195 data->AsStringPiece());
[email protected]8b37a092012-10-18 21:53:49196
[email protected]be24ab22012-10-22 03:01:52197 QuicFrame frame(&stream_frame);
198 QuicFrames frames;
199 frames.push_back(frame);
[email protected]8b37a092012-10-18 21:53:49200 QuicPacket* packet;
[email protected]082b65b2012-11-10 19:11:31201 quic_framer.ConstructFrameDataPacket(header, frames, &packet);
[email protected]8b37a092012-10-18 21:53:49202 return packet;
203}
204
[email protected]8b37a092012-10-18 21:53:49205} // namespace test
206
207} // namespace net