blob: 0f265a5e197bdde21dc3671186bbc540a5c22dba [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]4df69842013-02-27 06:32:1611#include "net/quic/crypto/quic_decrypter.h"
12#include "net/quic/crypto/quic_encrypter.h"
[email protected]8b37a092012-10-18 21:53:4913
14using std::max;
15using std::min;
16using std::string;
[email protected]cff7b7b2013-01-11 08:49:0717using testing::_;
[email protected]8b37a092012-10-18 21:53:4918
19namespace net {
20namespace test {
21
22MockFramerVisitor::MockFramerVisitor() {
23 // By default, we want to accept packets.
[email protected]cff7b7b2013-01-11 08:49:0724 ON_CALL(*this, OnPacketHeader(_))
[email protected]8b37a092012-10-18 21:53:4925 .WillByDefault(testing::Return(true));
26}
27
[email protected]044ac2b2012-11-13 21:41:0628MockFramerVisitor::~MockFramerVisitor() {
29}
[email protected]8b37a092012-10-18 21:53:4930
31bool NoOpFramerVisitor::OnPacketHeader(const QuicPacketHeader& header) {
32 return true;
33}
34
[email protected]9db443912013-02-25 05:27:0335FramerVisitorCapturingFrames::FramerVisitorCapturingFrames() : frame_count_(0) {
[email protected]134e5c32012-12-12 19:20:3636}
37
[email protected]9db443912013-02-25 05:27:0338FramerVisitorCapturingFrames::~FramerVisitorCapturingFrames() {
[email protected]26f3f8e2012-12-13 21:07:1939}
40
[email protected]9db443912013-02-25 05:27:0341bool FramerVisitorCapturingFrames::OnPacketHeader(
[email protected]4e6f0ed2012-11-02 22:15:3842 const QuicPacketHeader& header) {
43 header_ = header;
[email protected]9db443912013-02-25 05:27:0344 frame_count_ = 0;
[email protected]4e6f0ed2012-11-02 22:15:3845 return true;
46}
47
[email protected]9db443912013-02-25 05:27:0348void FramerVisitorCapturingFrames::OnStreamFrame(const QuicStreamFrame& frame) {
49 // TODO(ianswett): Own the underlying string, so it will not exist outside
50 // this callback.
51 stream_frames_.push_back(frame);
52 ++frame_count_;
[email protected]26f3f8e2012-12-13 21:07:1953}
54
[email protected]9db443912013-02-25 05:27:0355void FramerVisitorCapturingFrames::OnAckFrame(const QuicAckFrame& frame) {
56 ack_.reset(new QuicAckFrame(frame));
57 ++frame_count_;
58}
59
60void FramerVisitorCapturingFrames::OnCongestionFeedbackFrame(
[email protected]26f3f8e2012-12-13 21:07:1961 const QuicCongestionFeedbackFrame& frame) {
62 feedback_.reset(new QuicCongestionFeedbackFrame(frame));
[email protected]9db443912013-02-25 05:27:0363 ++frame_count_;
64}
65
66void FramerVisitorCapturingFrames::OnRstStreamFrame(
67 const QuicRstStreamFrame& frame) {
68 rst_.reset(new QuicRstStreamFrame(frame));
69 ++frame_count_;
70}
71
72void FramerVisitorCapturingFrames::OnConnectionCloseFrame(
73 const QuicConnectionCloseFrame& frame) {
74 close_.reset(new QuicConnectionCloseFrame(frame));
75 ++frame_count_;
76}
77
78void FramerVisitorCapturingFrames::OnGoAwayFrame(const QuicGoAwayFrame& frame) {
79 goaway_.reset(new QuicGoAwayFrame(frame));
80 ++frame_count_;
[email protected]4e6f0ed2012-11-02 22:15:3881}
82
[email protected]fee17f72013-02-03 07:47:4183FramerVisitorCapturingPublicReset::FramerVisitorCapturingPublicReset() {
84}
85
86FramerVisitorCapturingPublicReset::~FramerVisitorCapturingPublicReset() {
87}
88
89void FramerVisitorCapturingPublicReset::OnPublicResetPacket(
90 const QuicPublicResetPacket& public_reset) {
91 public_reset_packet_ = public_reset;
92}
93
[email protected]8d659e22013-01-19 04:26:1094MockConnectionVisitor::MockConnectionVisitor() {
95}
96
97MockConnectionVisitor::~MockConnectionVisitor() {
98}
99
[email protected]9c0b1352012-11-04 00:03:27100MockHelper::MockHelper() {
101}
102
103MockHelper::~MockHelper() {
104}
105
[email protected]97693d12012-11-16 16:05:00106const QuicClock* MockHelper::GetClock() const {
[email protected]9c0b1352012-11-04 00:03:27107 return &clock_;
108}
109
[email protected]9558c5d32012-12-22 00:08:14110QuicRandom* MockHelper::GetRandomGenerator() {
111 return &random_generator_;
112}
113
[email protected]044ac2b2012-11-13 21:41:06114MockConnection::MockConnection(QuicGuid guid, IPEndPoint address)
[email protected]e13201d82012-12-12 05:00:32115 : QuicConnection(guid, address, new MockHelper()),
116 helper_(helper()) {
[email protected]044ac2b2012-11-13 21:41:06117}
118
[email protected]872edd9e2013-01-16 08:51:15119MockConnection::MockConnection(QuicGuid guid,
120 IPEndPoint address,
121 QuicConnectionHelperInterface* helper)
122 : QuicConnection(guid, address, helper),
123 helper_(helper) {
124}
125
[email protected]044ac2b2012-11-13 21:41:06126MockConnection::~MockConnection() {
127}
128
129PacketSavingConnection::PacketSavingConnection(QuicGuid guid,
130 IPEndPoint address)
131 : MockConnection(guid, address) {
132}
133
134PacketSavingConnection::~PacketSavingConnection() {
[email protected]701bc892013-01-17 04:51:54135 STLDeleteElements(&packets_);
[email protected]044ac2b2012-11-13 21:41:06136}
137
[email protected]fee17f72013-02-03 07:47:41138bool PacketSavingConnection::SendOrQueuePacket(
139 QuicPacketSequenceNumber sequence_number,
140 QuicPacket* packet,
[email protected]9db443912013-02-25 05:27:03141 QuicPacketEntropyHash hash) {
[email protected]044ac2b2012-11-13 21:41:06142 packets_.push_back(packet);
143 return true;
144}
145
146MockSession::MockSession(QuicConnection* connection, bool is_server)
147 : QuicSession(connection, is_server) {
[email protected]cff7b7b2013-01-11 08:49:07148 ON_CALL(*this, WriteData(_, _, _, _))
149 .WillByDefault(testing::Return(QuicConsumedData(0, false)));
[email protected]044ac2b2012-11-13 21:41:06150}
151
152MockSession::~MockSession() {
153}
154
[email protected]fee17f72013-02-03 07:47:41155MockSendAlgorithm::MockSendAlgorithm() {
[email protected]8d659e22013-01-19 04:26:10156}
157
[email protected]fee17f72013-02-03 07:47:41158MockSendAlgorithm::~MockSendAlgorithm() {
[email protected]8d659e22013-01-19 04:26:10159}
160
[email protected]8b37a092012-10-18 21:53:49161namespace {
162
163string HexDumpWithMarks(const char* data, int length,
164 const bool* marks, int mark_length) {
165 static const char kHexChars[] = "0123456789abcdef";
166 static const int kColumns = 4;
167
168 const int kSizeLimit = 1024;
169 if (length > kSizeLimit || mark_length > kSizeLimit) {
170 LOG(ERROR) << "Only dumping first " << kSizeLimit << " bytes.";
171 length = min(length, kSizeLimit);
172 mark_length = min(mark_length, kSizeLimit);
173 }
174
175 string hex;
176 for (const char* row = data; length > 0;
177 row += kColumns, length -= kColumns) {
178 for (const char *p = row; p < row + 4; ++p) {
179 if (p < row + length) {
180 const bool mark =
181 (marks && (p - data) < mark_length && marks[p - data]);
182 hex += mark ? '*' : ' ';
183 hex += kHexChars[(*p & 0xf0) >> 4];
184 hex += kHexChars[*p & 0x0f];
185 hex += mark ? '*' : ' ';
186 } else {
187 hex += " ";
188 }
189 }
190 hex = hex + " ";
191
192 for (const char *p = row; p < row + 4 && p < row + length; ++p)
193 hex += (*p >= 0x20 && *p <= 0x7f) ? (*p) : '.';
194
195 hex = hex + '\n';
196 }
197 return hex;
198}
199
200} // namespace
201
202void CompareCharArraysWithHexError(
203 const string& description,
204 const char* actual,
205 const int actual_len,
206 const char* expected,
207 const int expected_len) {
208 const int min_len = min(actual_len, expected_len);
209 const int max_len = max(actual_len, expected_len);
210 scoped_array<bool> marks(new bool[max_len]);
211 bool identical = (actual_len == expected_len);
212 for (int i = 0; i < min_len; ++i) {
213 if (actual[i] != expected[i]) {
214 marks[i] = true;
215 identical = false;
216 } else {
217 marks[i] = false;
218 }
219 }
220 for (int i = min_len; i < max_len; ++i) {
221 marks[i] = true;
222 }
223 if (identical) return;
224 ADD_FAILURE()
225 << "Description:\n"
226 << description
227 << "\n\nExpected:\n"
228 << HexDumpWithMarks(expected, expected_len, marks.get(), max_len)
229 << "\nActual:\n"
230 << HexDumpWithMarks(actual, actual_len, marks.get(), max_len);
231}
232
233void CompareQuicDataWithHexError(
234 const string& description,
235 QuicData* actual,
236 QuicData* expected) {
237 CompareCharArraysWithHexError(
238 description,
239 actual->data(), actual->length(),
240 expected->data(), expected->length());
241}
242
[email protected]d3d15bf2013-01-30 02:51:54243static QuicPacket* ConstructPacketFromHandshakeMessage(
244 QuicGuid guid,
245 const CryptoHandshakeMessage& message) {
[email protected]8b37a092012-10-18 21:53:49246 CryptoFramer crypto_framer;
[email protected]dc2cc742012-10-21 13:56:13247 scoped_ptr<QuicData> data(crypto_framer.ConstructHandshakeMessage(message));
[email protected]8b37a092012-10-18 21:53:49248 QuicFramer quic_framer(QuicDecrypter::Create(kNULL),
249 QuicEncrypter::Create(kNULL));
250
251 QuicPacketHeader header;
[email protected]c995c572013-01-18 05:43:20252 header.public_header.guid = guid;
[email protected]9db443912013-02-25 05:27:03253 header.public_header.reset_flag = false;
254 header.public_header.version_flag = false;
[email protected]8b37a092012-10-18 21:53:49255 header.packet_sequence_number = 1;
[email protected]9db443912013-02-25 05:27:03256 header.entropy_flag = false;
257 header.entropy_hash = 0;
258 header.fec_flag = false;
259 header.fec_entropy_flag = false;
[email protected]8b37a092012-10-18 21:53:49260 header.fec_group = 0;
261
[email protected]be24ab22012-10-22 03:01:52262 QuicStreamFrame stream_frame(kCryptoStreamId, false, 0,
[email protected]044ac2b2012-11-13 21:41:06263 data->AsStringPiece());
[email protected]8b37a092012-10-18 21:53:49264
[email protected]be24ab22012-10-22 03:01:52265 QuicFrame frame(&stream_frame);
266 QuicFrames frames;
267 frames.push_back(frame);
[email protected]9db443912013-02-25 05:27:03268 return quic_framer.ConstructFrameDataPacket(header, frames).packet;
[email protected]8b37a092012-10-18 21:53:49269}
270
[email protected]d3d15bf2013-01-30 02:51:54271QuicPacket* ConstructHandshakePacket(QuicGuid guid, CryptoTag tag) {
272 CryptoHandshakeMessage message;
273 message.tag = tag;
274 return ConstructPacketFromHandshakeMessage(guid, message);
275}
276
[email protected]872edd9e2013-01-16 08:51:15277QuicPacket* ConstructClientHelloPacket(QuicGuid guid,
278 const QuicClock* clock,
[email protected]41d6b172013-01-29 16:10:57279 QuicRandom* random_generator,
280 const string& server_hostname) {
[email protected]6f54ab32013-03-02 17:43:35281 QuicCryptoClientConfig config;
282 config.SetDefaults();
[email protected]872edd9e2013-01-16 08:51:15283 string nonce;
284 CryptoUtils::GenerateNonce(clock, random_generator, &nonce);
285
286 CryptoHandshakeMessage message;
[email protected]6f54ab32013-03-02 17:43:35287 config.FillClientHello(nonce, server_hostname, &message);
[email protected]d3d15bf2013-01-30 02:51:54288 return ConstructPacketFromHandshakeMessage(guid, message);
289}
[email protected]872edd9e2013-01-16 08:51:15290
[email protected]d3d15bf2013-01-30 02:51:54291QuicPacket* ConstructServerHelloPacket(QuicGuid guid,
292 const QuicClock* clock,
293 QuicRandom* random_generator) {
[email protected]d3d15bf2013-01-30 02:51:54294 string nonce;
295 CryptoUtils::GenerateNonce(clock, random_generator, &nonce);
[email protected]872edd9e2013-01-16 08:51:15296
[email protected]6f54ab32013-03-02 17:43:35297 CryptoHandshakeMessage dummy_client_hello, server_hello;
298 QuicCryptoServerConfig server_config;
299 server_config.AddTestingConfig(random_generator, clock);
300 server_config.ProcessClientHello(dummy_client_hello, nonce, &server_hello);
301 return ConstructPacketFromHandshakeMessage(guid, server_hello);
[email protected]872edd9e2013-01-16 08:51:15302}
303
[email protected]9db443912013-02-25 05:27:03304QuicPacketEntropyHash TestEntropyCalculator::ReceivedEntropyHash(
305 QuicPacketSequenceNumber sequence_number) const {
306 return 1u;
307}
308
[email protected]8b37a092012-10-18 21:53:49309} // namespace test
[email protected]8b37a092012-10-18 21:53:49310} // namespace net