blob: 9faec9a85abb39dfaaf639a89bc91f4ab95c514e [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]8b37a092012-10-18 21:53:498#include "net/quic/crypto/crypto_framer.h"
[email protected]6f54ab32013-03-02 17:43:359#include "net/quic/crypto/crypto_handshake.h"
[email protected]872edd9e2013-01-16 08:51:1510#include "net/quic/crypto/crypto_utils.h"
[email protected]5351cc4b2013-03-03 07:22:4111#include "net/quic/crypto/null_encrypter.h"
[email protected]4df69842013-02-27 06:32:1612#include "net/quic/crypto/quic_decrypter.h"
13#include "net/quic/crypto/quic_encrypter.h"
[email protected]5351cc4b2013-03-03 07:22:4114#include "net/quic/quic_packet_creator.h"
[email protected]8b37a092012-10-18 21:53:4915
16using std::max;
17using std::min;
18using std::string;
[email protected]cff7b7b2013-01-11 08:49:0719using testing::_;
[email protected]8b37a092012-10-18 21:53:4920
21namespace net {
22namespace test {
23
24MockFramerVisitor::MockFramerVisitor() {
25 // By default, we want to accept packets.
[email protected]cff7b7b2013-01-11 08:49:0726 ON_CALL(*this, OnPacketHeader(_))
[email protected]8b37a092012-10-18 21:53:4927 .WillByDefault(testing::Return(true));
28}
29
[email protected]044ac2b2012-11-13 21:41:0630MockFramerVisitor::~MockFramerVisitor() {
31}
[email protected]8b37a092012-10-18 21:53:4932
33bool NoOpFramerVisitor::OnPacketHeader(const QuicPacketHeader& header) {
34 return true;
35}
36
[email protected]9db443912013-02-25 05:27:0337FramerVisitorCapturingFrames::FramerVisitorCapturingFrames() : frame_count_(0) {
[email protected]134e5c32012-12-12 19:20:3638}
39
[email protected]9db443912013-02-25 05:27:0340FramerVisitorCapturingFrames::~FramerVisitorCapturingFrames() {
[email protected]26f3f8e2012-12-13 21:07:1941}
42
[email protected]9db443912013-02-25 05:27:0343bool FramerVisitorCapturingFrames::OnPacketHeader(
[email protected]4e6f0ed2012-11-02 22:15:3844 const QuicPacketHeader& header) {
45 header_ = header;
[email protected]9db443912013-02-25 05:27:0346 frame_count_ = 0;
[email protected]4e6f0ed2012-11-02 22:15:3847 return true;
48}
49
[email protected]9db443912013-02-25 05:27:0350void FramerVisitorCapturingFrames::OnStreamFrame(const QuicStreamFrame& frame) {
51 // TODO(ianswett): Own the underlying string, so it will not exist outside
52 // this callback.
53 stream_frames_.push_back(frame);
54 ++frame_count_;
[email protected]26f3f8e2012-12-13 21:07:1955}
56
[email protected]9db443912013-02-25 05:27:0357void FramerVisitorCapturingFrames::OnAckFrame(const QuicAckFrame& frame) {
58 ack_.reset(new QuicAckFrame(frame));
59 ++frame_count_;
60}
61
62void FramerVisitorCapturingFrames::OnCongestionFeedbackFrame(
[email protected]26f3f8e2012-12-13 21:07:1963 const QuicCongestionFeedbackFrame& frame) {
64 feedback_.reset(new QuicCongestionFeedbackFrame(frame));
[email protected]9db443912013-02-25 05:27:0365 ++frame_count_;
66}
67
68void FramerVisitorCapturingFrames::OnRstStreamFrame(
69 const QuicRstStreamFrame& frame) {
70 rst_.reset(new QuicRstStreamFrame(frame));
71 ++frame_count_;
72}
73
74void FramerVisitorCapturingFrames::OnConnectionCloseFrame(
75 const QuicConnectionCloseFrame& frame) {
76 close_.reset(new QuicConnectionCloseFrame(frame));
77 ++frame_count_;
78}
79
80void FramerVisitorCapturingFrames::OnGoAwayFrame(const QuicGoAwayFrame& frame) {
81 goaway_.reset(new QuicGoAwayFrame(frame));
82 ++frame_count_;
[email protected]4e6f0ed2012-11-02 22:15:3883}
84
[email protected]fee17f72013-02-03 07:47:4185FramerVisitorCapturingPublicReset::FramerVisitorCapturingPublicReset() {
86}
87
88FramerVisitorCapturingPublicReset::~FramerVisitorCapturingPublicReset() {
89}
90
91void FramerVisitorCapturingPublicReset::OnPublicResetPacket(
92 const QuicPublicResetPacket& public_reset) {
93 public_reset_packet_ = public_reset;
94}
95
[email protected]8d659e22013-01-19 04:26:1096MockConnectionVisitor::MockConnectionVisitor() {
97}
98
99MockConnectionVisitor::~MockConnectionVisitor() {
100}
101
[email protected]9c0b1352012-11-04 00:03:27102MockHelper::MockHelper() {
103}
104
105MockHelper::~MockHelper() {
106}
107
[email protected]97693d12012-11-16 16:05:00108const QuicClock* MockHelper::GetClock() const {
[email protected]9c0b1352012-11-04 00:03:27109 return &clock_;
110}
111
[email protected]9558c5d32012-12-22 00:08:14112QuicRandom* MockHelper::GetRandomGenerator() {
113 return &random_generator_;
114}
115
[email protected]044ac2b2012-11-13 21:41:06116MockConnection::MockConnection(QuicGuid guid, IPEndPoint address)
[email protected]e13201d82012-12-12 05:00:32117 : QuicConnection(guid, address, new MockHelper()),
118 helper_(helper()) {
[email protected]044ac2b2012-11-13 21:41:06119}
120
[email protected]872edd9e2013-01-16 08:51:15121MockConnection::MockConnection(QuicGuid guid,
122 IPEndPoint address,
123 QuicConnectionHelperInterface* helper)
124 : QuicConnection(guid, address, helper),
125 helper_(helper) {
126}
127
[email protected]044ac2b2012-11-13 21:41:06128MockConnection::~MockConnection() {
129}
130
131PacketSavingConnection::PacketSavingConnection(QuicGuid guid,
132 IPEndPoint address)
133 : MockConnection(guid, address) {
134}
135
136PacketSavingConnection::~PacketSavingConnection() {
[email protected]701bc892013-01-17 04:51:54137 STLDeleteElements(&packets_);
[email protected]044ac2b2012-11-13 21:41:06138}
139
[email protected]fee17f72013-02-03 07:47:41140bool PacketSavingConnection::SendOrQueuePacket(
141 QuicPacketSequenceNumber sequence_number,
142 QuicPacket* packet,
[email protected]9db443912013-02-25 05:27:03143 QuicPacketEntropyHash hash) {
[email protected]044ac2b2012-11-13 21:41:06144 packets_.push_back(packet);
145 return true;
146}
147
148MockSession::MockSession(QuicConnection* connection, bool is_server)
149 : QuicSession(connection, is_server) {
[email protected]cff7b7b2013-01-11 08:49:07150 ON_CALL(*this, WriteData(_, _, _, _))
151 .WillByDefault(testing::Return(QuicConsumedData(0, false)));
[email protected]044ac2b2012-11-13 21:41:06152}
153
154MockSession::~MockSession() {
155}
156
[email protected]fee17f72013-02-03 07:47:41157MockSendAlgorithm::MockSendAlgorithm() {
[email protected]8d659e22013-01-19 04:26:10158}
159
[email protected]fee17f72013-02-03 07:47:41160MockSendAlgorithm::~MockSendAlgorithm() {
[email protected]8d659e22013-01-19 04:26:10161}
162
[email protected]8b37a092012-10-18 21:53:49163namespace {
164
165string HexDumpWithMarks(const char* data, int length,
166 const bool* marks, int mark_length) {
167 static const char kHexChars[] = "0123456789abcdef";
168 static const int kColumns = 4;
169
170 const int kSizeLimit = 1024;
171 if (length > kSizeLimit || mark_length > kSizeLimit) {
172 LOG(ERROR) << "Only dumping first " << kSizeLimit << " bytes.";
173 length = min(length, kSizeLimit);
174 mark_length = min(mark_length, kSizeLimit);
175 }
176
177 string hex;
178 for (const char* row = data; length > 0;
179 row += kColumns, length -= kColumns) {
180 for (const char *p = row; p < row + 4; ++p) {
181 if (p < row + length) {
182 const bool mark =
183 (marks && (p - data) < mark_length && marks[p - data]);
184 hex += mark ? '*' : ' ';
185 hex += kHexChars[(*p & 0xf0) >> 4];
186 hex += kHexChars[*p & 0x0f];
187 hex += mark ? '*' : ' ';
188 } else {
189 hex += " ";
190 }
191 }
192 hex = hex + " ";
193
194 for (const char *p = row; p < row + 4 && p < row + length; ++p)
195 hex += (*p >= 0x20 && *p <= 0x7f) ? (*p) : '.';
196
197 hex = hex + '\n';
198 }
199 return hex;
200}
201
202} // namespace
203
204void CompareCharArraysWithHexError(
205 const string& description,
206 const char* actual,
207 const int actual_len,
208 const char* expected,
209 const int expected_len) {
210 const int min_len = min(actual_len, expected_len);
211 const int max_len = max(actual_len, expected_len);
212 scoped_array<bool> marks(new bool[max_len]);
213 bool identical = (actual_len == expected_len);
214 for (int i = 0; i < min_len; ++i) {
215 if (actual[i] != expected[i]) {
216 marks[i] = true;
217 identical = false;
218 } else {
219 marks[i] = false;
220 }
221 }
222 for (int i = min_len; i < max_len; ++i) {
223 marks[i] = true;
224 }
225 if (identical) return;
226 ADD_FAILURE()
227 << "Description:\n"
228 << description
229 << "\n\nExpected:\n"
230 << HexDumpWithMarks(expected, expected_len, marks.get(), max_len)
231 << "\nActual:\n"
232 << HexDumpWithMarks(actual, actual_len, marks.get(), max_len);
233}
234
235void CompareQuicDataWithHexError(
236 const string& description,
237 QuicData* actual,
238 QuicData* expected) {
239 CompareCharArraysWithHexError(
240 description,
241 actual->data(), actual->length(),
242 expected->data(), expected->length());
243}
244
[email protected]d3d15bf2013-01-30 02:51:54245static QuicPacket* ConstructPacketFromHandshakeMessage(
246 QuicGuid guid,
247 const CryptoHandshakeMessage& message) {
[email protected]8b37a092012-10-18 21:53:49248 CryptoFramer crypto_framer;
[email protected]dc2cc742012-10-21 13:56:13249 scoped_ptr<QuicData> data(crypto_framer.ConstructHandshakeMessage(message));
[email protected]5351cc4b2013-03-03 07:22:41250 QuicFramer quic_framer(kQuicVersion1,
251 QuicDecrypter::Create(kNULL),
[email protected]8b37a092012-10-18 21:53:49252 QuicEncrypter::Create(kNULL));
253
254 QuicPacketHeader header;
[email protected]c995c572013-01-18 05:43:20255 header.public_header.guid = guid;
[email protected]9db443912013-02-25 05:27:03256 header.public_header.reset_flag = false;
257 header.public_header.version_flag = false;
[email protected]8b37a092012-10-18 21:53:49258 header.packet_sequence_number = 1;
[email protected]9db443912013-02-25 05:27:03259 header.entropy_flag = false;
260 header.entropy_hash = 0;
261 header.fec_flag = false;
262 header.fec_entropy_flag = false;
[email protected]8b37a092012-10-18 21:53:49263 header.fec_group = 0;
264
[email protected]be24ab22012-10-22 03:01:52265 QuicStreamFrame stream_frame(kCryptoStreamId, false, 0,
[email protected]044ac2b2012-11-13 21:41:06266 data->AsStringPiece());
[email protected]8b37a092012-10-18 21:53:49267
[email protected]be24ab22012-10-22 03:01:52268 QuicFrame frame(&stream_frame);
269 QuicFrames frames;
270 frames.push_back(frame);
[email protected]9db443912013-02-25 05:27:03271 return quic_framer.ConstructFrameDataPacket(header, frames).packet;
[email protected]8b37a092012-10-18 21:53:49272}
273
[email protected]d3d15bf2013-01-30 02:51:54274QuicPacket* ConstructHandshakePacket(QuicGuid guid, CryptoTag tag) {
275 CryptoHandshakeMessage message;
276 message.tag = tag;
277 return ConstructPacketFromHandshakeMessage(guid, message);
278}
279
[email protected]872edd9e2013-01-16 08:51:15280QuicPacket* ConstructClientHelloPacket(QuicGuid guid,
281 const QuicClock* clock,
[email protected]41d6b172013-01-29 16:10:57282 QuicRandom* random_generator,
283 const string& server_hostname) {
[email protected]6f54ab32013-03-02 17:43:35284 QuicCryptoClientConfig config;
285 config.SetDefaults();
[email protected]872edd9e2013-01-16 08:51:15286 string nonce;
287 CryptoUtils::GenerateNonce(clock, random_generator, &nonce);
288
289 CryptoHandshakeMessage message;
[email protected]6f54ab32013-03-02 17:43:35290 config.FillClientHello(nonce, server_hostname, &message);
[email protected]d3d15bf2013-01-30 02:51:54291 return ConstructPacketFromHandshakeMessage(guid, message);
292}
[email protected]872edd9e2013-01-16 08:51:15293
[email protected]d3d15bf2013-01-30 02:51:54294QuicPacket* ConstructServerHelloPacket(QuicGuid guid,
295 const QuicClock* clock,
296 QuicRandom* random_generator) {
[email protected]d3d15bf2013-01-30 02:51:54297 string nonce;
298 CryptoUtils::GenerateNonce(clock, random_generator, &nonce);
[email protected]872edd9e2013-01-16 08:51:15299
[email protected]6f54ab32013-03-02 17:43:35300 CryptoHandshakeMessage dummy_client_hello, server_hello;
301 QuicCryptoServerConfig server_config;
302 server_config.AddTestingConfig(random_generator, clock);
303 server_config.ProcessClientHello(dummy_client_hello, nonce, &server_hello);
304 return ConstructPacketFromHandshakeMessage(guid, server_hello);
[email protected]872edd9e2013-01-16 08:51:15305}
306
[email protected]5351cc4b2013-03-03 07:22:41307size_t GetPacketLengthForOneStream(bool include_version, size_t payload) {
308 return NullEncrypter().GetCiphertextSize(payload) +
309 QuicPacketCreator::StreamFramePacketOverhead(1, include_version);
310}
311
[email protected]9db443912013-02-25 05:27:03312QuicPacketEntropyHash TestEntropyCalculator::ReceivedEntropyHash(
313 QuicPacketSequenceNumber sequence_number) const {
314 return 1u;
315}
316
[email protected]8b37a092012-10-18 21:53:49317} // namespace test
[email protected]8b37a092012-10-18 21:53:49318} // namespace net