blob: 8f7feebc8c78c7dea2307f1e7ddfda0d64d9fdaf [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]872edd9e2013-01-16 08:51:159#include "net/quic/crypto/crypto_utils.h"
[email protected]8b37a092012-10-18 21:53:4910
11using std::max;
12using std::min;
13using std::string;
[email protected]cff7b7b2013-01-11 08:49:0714using testing::_;
[email protected]8b37a092012-10-18 21:53:4915
16namespace net {
17namespace test {
18
19MockFramerVisitor::MockFramerVisitor() {
20 // By default, we want to accept packets.
[email protected]cff7b7b2013-01-11 08:49:0721 ON_CALL(*this, OnPacketHeader(_))
[email protected]8b37a092012-10-18 21:53:4922 .WillByDefault(testing::Return(true));
23}
24
[email protected]044ac2b2012-11-13 21:41:0625MockFramerVisitor::~MockFramerVisitor() {
26}
[email protected]8b37a092012-10-18 21:53:4927
28bool NoOpFramerVisitor::OnPacketHeader(const QuicPacketHeader& header) {
29 return true;
30}
31
[email protected]134e5c32012-12-12 19:20:3632FramerVisitorCapturingAcks::FramerVisitorCapturingAcks() {
33}
34
[email protected]26f3f8e2012-12-13 21:07:1935FramerVisitorCapturingAcks::~FramerVisitorCapturingAcks() {
36}
37
[email protected]4e6f0ed2012-11-02 22:15:3838bool FramerVisitorCapturingAcks::OnPacketHeader(
39 const QuicPacketHeader& header) {
40 header_ = header;
41 return true;
42}
43
44void FramerVisitorCapturingAcks::OnAckFrame(const QuicAckFrame& frame) {
[email protected]26f3f8e2012-12-13 21:07:1945 ack_.reset(new QuicAckFrame(frame));
46}
47
48void FramerVisitorCapturingAcks::OnCongestionFeedbackFrame(
49 const QuicCongestionFeedbackFrame& frame) {
50 feedback_.reset(new QuicCongestionFeedbackFrame(frame));
[email protected]4e6f0ed2012-11-02 22:15:3851}
52
[email protected]8d659e22013-01-19 04:26:1053MockConnectionVisitor::MockConnectionVisitor() {
54}
55
56MockConnectionVisitor::~MockConnectionVisitor() {
57}
58
[email protected]9c0b1352012-11-04 00:03:2759MockHelper::MockHelper() {
60}
61
62MockHelper::~MockHelper() {
63}
64
[email protected]97693d12012-11-16 16:05:0065const QuicClock* MockHelper::GetClock() const {
[email protected]9c0b1352012-11-04 00:03:2766 return &clock_;
67}
68
[email protected]9558c5d32012-12-22 00:08:1469QuicRandom* MockHelper::GetRandomGenerator() {
70 return &random_generator_;
71}
72
[email protected]044ac2b2012-11-13 21:41:0673MockConnection::MockConnection(QuicGuid guid, IPEndPoint address)
[email protected]e13201d82012-12-12 05:00:3274 : QuicConnection(guid, address, new MockHelper()),
75 helper_(helper()) {
[email protected]044ac2b2012-11-13 21:41:0676}
77
[email protected]872edd9e2013-01-16 08:51:1578MockConnection::MockConnection(QuicGuid guid,
79 IPEndPoint address,
80 QuicConnectionHelperInterface* helper)
81 : QuicConnection(guid, address, helper),
82 helper_(helper) {
83}
84
[email protected]044ac2b2012-11-13 21:41:0685MockConnection::~MockConnection() {
86}
87
88PacketSavingConnection::PacketSavingConnection(QuicGuid guid,
89 IPEndPoint address)
90 : MockConnection(guid, address) {
91}
92
93PacketSavingConnection::~PacketSavingConnection() {
[email protected]701bc892013-01-17 04:51:5494 STLDeleteElements(&packets_);
[email protected]044ac2b2012-11-13 21:41:0695}
96
97bool PacketSavingConnection::SendPacket(QuicPacketSequenceNumber number,
98 QuicPacket* packet,
[email protected]c995c572013-01-18 05:43:2099 bool should_retransmit,
[email protected]044ac2b2012-11-13 21:41:06100 bool force,
[email protected]c995c572013-01-18 05:43:20101 bool is_retransmission) {
[email protected]044ac2b2012-11-13 21:41:06102 packets_.push_back(packet);
103 return true;
104}
105
106MockSession::MockSession(QuicConnection* connection, bool is_server)
107 : QuicSession(connection, is_server) {
[email protected]cff7b7b2013-01-11 08:49:07108 ON_CALL(*this, WriteData(_, _, _, _))
109 .WillByDefault(testing::Return(QuicConsumedData(0, false)));
[email protected]044ac2b2012-11-13 21:41:06110}
111
112MockSession::~MockSession() {
113}
114
[email protected]8d659e22013-01-19 04:26:10115MockScheduler::MockScheduler()
116 : QuicSendScheduler(NULL, kFixRate) {
117}
118
119MockScheduler::~MockScheduler() {
120}
121
[email protected]8b37a092012-10-18 21:53:49122namespace {
123
124string HexDumpWithMarks(const char* data, int length,
125 const bool* marks, int mark_length) {
126 static const char kHexChars[] = "0123456789abcdef";
127 static const int kColumns = 4;
128
129 const int kSizeLimit = 1024;
130 if (length > kSizeLimit || mark_length > kSizeLimit) {
131 LOG(ERROR) << "Only dumping first " << kSizeLimit << " bytes.";
132 length = min(length, kSizeLimit);
133 mark_length = min(mark_length, kSizeLimit);
134 }
135
136 string hex;
137 for (const char* row = data; length > 0;
138 row += kColumns, length -= kColumns) {
139 for (const char *p = row; p < row + 4; ++p) {
140 if (p < row + length) {
141 const bool mark =
142 (marks && (p - data) < mark_length && marks[p - data]);
143 hex += mark ? '*' : ' ';
144 hex += kHexChars[(*p & 0xf0) >> 4];
145 hex += kHexChars[*p & 0x0f];
146 hex += mark ? '*' : ' ';
147 } else {
148 hex += " ";
149 }
150 }
151 hex = hex + " ";
152
153 for (const char *p = row; p < row + 4 && p < row + length; ++p)
154 hex += (*p >= 0x20 && *p <= 0x7f) ? (*p) : '.';
155
156 hex = hex + '\n';
157 }
158 return hex;
159}
160
161} // namespace
162
163void CompareCharArraysWithHexError(
164 const string& description,
165 const char* actual,
166 const int actual_len,
167 const char* expected,
168 const int expected_len) {
169 const int min_len = min(actual_len, expected_len);
170 const int max_len = max(actual_len, expected_len);
171 scoped_array<bool> marks(new bool[max_len]);
172 bool identical = (actual_len == expected_len);
173 for (int i = 0; i < min_len; ++i) {
174 if (actual[i] != expected[i]) {
175 marks[i] = true;
176 identical = false;
177 } else {
178 marks[i] = false;
179 }
180 }
181 for (int i = min_len; i < max_len; ++i) {
182 marks[i] = true;
183 }
184 if (identical) return;
185 ADD_FAILURE()
186 << "Description:\n"
187 << description
188 << "\n\nExpected:\n"
189 << HexDumpWithMarks(expected, expected_len, marks.get(), max_len)
190 << "\nActual:\n"
191 << HexDumpWithMarks(actual, actual_len, marks.get(), max_len);
192}
193
194void CompareQuicDataWithHexError(
195 const string& description,
196 QuicData* actual,
197 QuicData* expected) {
198 CompareCharArraysWithHexError(
199 description,
200 actual->data(), actual->length(),
201 expected->data(), expected->length());
202}
203
204QuicPacket* ConstructHandshakePacket(QuicGuid guid, CryptoTag tag) {
205 CryptoHandshakeMessage message;
206 message.tag = tag;
207 CryptoFramer crypto_framer;
[email protected]dc2cc742012-10-21 13:56:13208 scoped_ptr<QuicData> data(crypto_framer.ConstructHandshakeMessage(message));
[email protected]8b37a092012-10-18 21:53:49209 QuicFramer quic_framer(QuicDecrypter::Create(kNULL),
210 QuicEncrypter::Create(kNULL));
211
212 QuicPacketHeader header;
[email protected]c995c572013-01-18 05:43:20213 header.public_header.guid = guid;
214 header.public_header.flags = PACKET_PUBLIC_FLAGS_NONE;
[email protected]8b37a092012-10-18 21:53:49215 header.packet_sequence_number = 1;
[email protected]c995c572013-01-18 05:43:20216 header.private_flags = PACKET_PRIVATE_FLAGS_NONE;
[email protected]8b37a092012-10-18 21:53:49217 header.fec_group = 0;
218
[email protected]be24ab22012-10-22 03:01:52219 QuicStreamFrame stream_frame(kCryptoStreamId, false, 0,
[email protected]044ac2b2012-11-13 21:41:06220 data->AsStringPiece());
[email protected]8b37a092012-10-18 21:53:49221
[email protected]be24ab22012-10-22 03:01:52222 QuicFrame frame(&stream_frame);
223 QuicFrames frames;
224 frames.push_back(frame);
[email protected]610a7e942012-12-18 00:21:39225 return quic_framer.ConstructFrameDataPacket(header, frames);
[email protected]8b37a092012-10-18 21:53:49226}
227
[email protected]872edd9e2013-01-16 08:51:15228QuicPacket* ConstructClientHelloPacket(QuicGuid guid,
229 const QuicClock* clock,
[email protected]41d6b172013-01-29 16:10:57230 QuicRandom* random_generator,
231 const string& server_hostname) {
[email protected]872edd9e2013-01-16 08:51:15232 QuicClientCryptoConfig config;
233 config.SetDefaults();
234 string nonce;
235 CryptoUtils::GenerateNonce(clock, random_generator, &nonce);
236
237 CryptoHandshakeMessage message;
[email protected]41d6b172013-01-29 16:10:57238 CryptoUtils::FillClientHelloMessage(config, nonce, server_hostname,
239 &message);
[email protected]872edd9e2013-01-16 08:51:15240 CryptoFramer crypto_framer;
241 scoped_ptr<QuicData> data(crypto_framer.ConstructHandshakeMessage(message));
242 QuicFramer quic_framer(QuicDecrypter::Create(kNULL),
243 QuicEncrypter::Create(kNULL));
244
245 QuicPacketHeader header;
[email protected]c995c572013-01-18 05:43:20246 header.public_header.guid = guid;
247 header.public_header.flags = PACKET_PUBLIC_FLAGS_NONE;
[email protected]872edd9e2013-01-16 08:51:15248 header.packet_sequence_number = 1;
[email protected]c995c572013-01-18 05:43:20249 header.private_flags = PACKET_PRIVATE_FLAGS_NONE;
[email protected]872edd9e2013-01-16 08:51:15250 header.fec_group = 0;
251
252 QuicStreamFrame stream_frame(kCryptoStreamId, false, 0,
253 data->AsStringPiece());
254
255 QuicFrame frame(&stream_frame);
256 QuicFrames frames;
257 frames.push_back(frame);
258 return quic_framer.ConstructFrameDataPacket(header, frames);
259}
260
[email protected]8b37a092012-10-18 21:53:49261} // namespace test
[email protected]8b37a092012-10-18 21:53:49262} // namespace net