blob: 1fb7f4f93cd165c26c495bcaa7f2111c9bbb4a48 [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]14e8106c2013-03-14 16:25:3326 ON_CALL(*this, OnProtocolVersionMismatch(_))
27 .WillByDefault(testing::Return(false));
28
29 // By default, we want to accept packets.
[email protected]cff7b7b2013-01-11 08:49:0730 ON_CALL(*this, OnPacketHeader(_))
[email protected]8b37a092012-10-18 21:53:4931 .WillByDefault(testing::Return(true));
32}
33
[email protected]044ac2b2012-11-13 21:41:0634MockFramerVisitor::~MockFramerVisitor() {
35}
[email protected]8b37a092012-10-18 21:53:4936
[email protected]14e8106c2013-03-14 16:25:3337bool NoOpFramerVisitor::OnProtocolVersionMismatch(QuicVersionTag version) {
38 return false;
39}
40
[email protected]8b37a092012-10-18 21:53:4941bool NoOpFramerVisitor::OnPacketHeader(const QuicPacketHeader& header) {
42 return true;
43}
44
[email protected]9db443912013-02-25 05:27:0345FramerVisitorCapturingFrames::FramerVisitorCapturingFrames() : frame_count_(0) {
[email protected]134e5c32012-12-12 19:20:3646}
47
[email protected]9db443912013-02-25 05:27:0348FramerVisitorCapturingFrames::~FramerVisitorCapturingFrames() {
[email protected]26f3f8e2012-12-13 21:07:1949}
50
[email protected]9db443912013-02-25 05:27:0351bool FramerVisitorCapturingFrames::OnPacketHeader(
[email protected]4e6f0ed2012-11-02 22:15:3852 const QuicPacketHeader& header) {
53 header_ = header;
[email protected]9db443912013-02-25 05:27:0354 frame_count_ = 0;
[email protected]4e6f0ed2012-11-02 22:15:3855 return true;
56}
57
[email protected]9db443912013-02-25 05:27:0358void FramerVisitorCapturingFrames::OnStreamFrame(const QuicStreamFrame& frame) {
59 // TODO(ianswett): Own the underlying string, so it will not exist outside
60 // this callback.
61 stream_frames_.push_back(frame);
62 ++frame_count_;
[email protected]26f3f8e2012-12-13 21:07:1963}
64
[email protected]9db443912013-02-25 05:27:0365void FramerVisitorCapturingFrames::OnAckFrame(const QuicAckFrame& frame) {
66 ack_.reset(new QuicAckFrame(frame));
67 ++frame_count_;
68}
69
70void FramerVisitorCapturingFrames::OnCongestionFeedbackFrame(
[email protected]26f3f8e2012-12-13 21:07:1971 const QuicCongestionFeedbackFrame& frame) {
72 feedback_.reset(new QuicCongestionFeedbackFrame(frame));
[email protected]9db443912013-02-25 05:27:0373 ++frame_count_;
74}
75
76void FramerVisitorCapturingFrames::OnRstStreamFrame(
77 const QuicRstStreamFrame& frame) {
78 rst_.reset(new QuicRstStreamFrame(frame));
79 ++frame_count_;
80}
81
82void FramerVisitorCapturingFrames::OnConnectionCloseFrame(
83 const QuicConnectionCloseFrame& frame) {
84 close_.reset(new QuicConnectionCloseFrame(frame));
85 ++frame_count_;
86}
87
88void FramerVisitorCapturingFrames::OnGoAwayFrame(const QuicGoAwayFrame& frame) {
89 goaway_.reset(new QuicGoAwayFrame(frame));
90 ++frame_count_;
[email protected]4e6f0ed2012-11-02 22:15:3891}
92
[email protected]14e8106c2013-03-14 16:25:3393void FramerVisitorCapturingFrames::OnVersionNegotiationPacket(
94 const QuicVersionNegotiationPacket& packet) {
95 version_negotiation_packet_.reset(new QuicVersionNegotiationPacket(packet));
96 frame_count_ = 0;
97}
98
[email protected]fee17f72013-02-03 07:47:4199FramerVisitorCapturingPublicReset::FramerVisitorCapturingPublicReset() {
100}
101
102FramerVisitorCapturingPublicReset::~FramerVisitorCapturingPublicReset() {
103}
104
105void FramerVisitorCapturingPublicReset::OnPublicResetPacket(
106 const QuicPublicResetPacket& public_reset) {
107 public_reset_packet_ = public_reset;
108}
109
[email protected]8d659e22013-01-19 04:26:10110MockConnectionVisitor::MockConnectionVisitor() {
111}
112
113MockConnectionVisitor::~MockConnectionVisitor() {
114}
115
[email protected]9c0b1352012-11-04 00:03:27116MockHelper::MockHelper() {
117}
118
119MockHelper::~MockHelper() {
120}
121
[email protected]97693d12012-11-16 16:05:00122const QuicClock* MockHelper::GetClock() const {
[email protected]9c0b1352012-11-04 00:03:27123 return &clock_;
124}
125
[email protected]9558c5d32012-12-22 00:08:14126QuicRandom* MockHelper::GetRandomGenerator() {
127 return &random_generator_;
128}
129
[email protected]14e8106c2013-03-14 16:25:33130MockConnection::MockConnection(QuicGuid guid,
131 IPEndPoint address,
132 bool is_server)
[email protected]6dcd1d5c2013-03-15 07:28:37133 : QuicConnection(guid, address, new MockHelper(), is_server) {
[email protected]044ac2b2012-11-13 21:41:06134}
135
[email protected]872edd9e2013-01-16 08:51:15136MockConnection::MockConnection(QuicGuid guid,
137 IPEndPoint address,
[email protected]14e8106c2013-03-14 16:25:33138 QuicConnectionHelperInterface* helper,
139 bool is_server)
[email protected]6dcd1d5c2013-03-15 07:28:37140 : QuicConnection(guid, address, helper, is_server) {
[email protected]872edd9e2013-01-16 08:51:15141}
142
[email protected]044ac2b2012-11-13 21:41:06143MockConnection::~MockConnection() {
144}
145
146PacketSavingConnection::PacketSavingConnection(QuicGuid guid,
[email protected]14e8106c2013-03-14 16:25:33147 IPEndPoint address,
148 bool is_server)
149 : MockConnection(guid, address, is_server) {
[email protected]044ac2b2012-11-13 21:41:06150}
151
152PacketSavingConnection::~PacketSavingConnection() {
[email protected]701bc892013-01-17 04:51:54153 STLDeleteElements(&packets_);
[email protected]044ac2b2012-11-13 21:41:06154}
155
[email protected]fee17f72013-02-03 07:47:41156bool PacketSavingConnection::SendOrQueuePacket(
157 QuicPacketSequenceNumber sequence_number,
158 QuicPacket* packet,
[email protected]ed3fc15d2013-03-08 18:37:44159 QuicPacketEntropyHash entropy_hash,
[email protected]74bda142013-03-31 02:49:11160 HasRetransmittableData retransmittable) {
[email protected]044ac2b2012-11-13 21:41:06161 packets_.push_back(packet);
162 return true;
163}
164
165MockSession::MockSession(QuicConnection* connection, bool is_server)
166 : QuicSession(connection, is_server) {
[email protected]cff7b7b2013-01-11 08:49:07167 ON_CALL(*this, WriteData(_, _, _, _))
168 .WillByDefault(testing::Return(QuicConsumedData(0, false)));
[email protected]044ac2b2012-11-13 21:41:06169}
170
171MockSession::~MockSession() {
172}
173
[email protected]fee17f72013-02-03 07:47:41174MockSendAlgorithm::MockSendAlgorithm() {
[email protected]8d659e22013-01-19 04:26:10175}
176
[email protected]fee17f72013-02-03 07:47:41177MockSendAlgorithm::~MockSendAlgorithm() {
[email protected]8d659e22013-01-19 04:26:10178}
179
[email protected]8b37a092012-10-18 21:53:49180namespace {
181
182string HexDumpWithMarks(const char* data, int length,
183 const bool* marks, int mark_length) {
184 static const char kHexChars[] = "0123456789abcdef";
185 static const int kColumns = 4;
186
187 const int kSizeLimit = 1024;
188 if (length > kSizeLimit || mark_length > kSizeLimit) {
189 LOG(ERROR) << "Only dumping first " << kSizeLimit << " bytes.";
190 length = min(length, kSizeLimit);
191 mark_length = min(mark_length, kSizeLimit);
192 }
193
194 string hex;
195 for (const char* row = data; length > 0;
196 row += kColumns, length -= kColumns) {
197 for (const char *p = row; p < row + 4; ++p) {
198 if (p < row + length) {
199 const bool mark =
200 (marks && (p - data) < mark_length && marks[p - data]);
201 hex += mark ? '*' : ' ';
202 hex += kHexChars[(*p & 0xf0) >> 4];
203 hex += kHexChars[*p & 0x0f];
204 hex += mark ? '*' : ' ';
205 } else {
206 hex += " ";
207 }
208 }
209 hex = hex + " ";
210
211 for (const char *p = row; p < row + 4 && p < row + length; ++p)
212 hex += (*p >= 0x20 && *p <= 0x7f) ? (*p) : '.';
213
214 hex = hex + '\n';
215 }
216 return hex;
217}
218
219} // namespace
220
221void CompareCharArraysWithHexError(
222 const string& description,
223 const char* actual,
224 const int actual_len,
225 const char* expected,
226 const int expected_len) {
227 const int min_len = min(actual_len, expected_len);
228 const int max_len = max(actual_len, expected_len);
229 scoped_array<bool> marks(new bool[max_len]);
230 bool identical = (actual_len == expected_len);
231 for (int i = 0; i < min_len; ++i) {
232 if (actual[i] != expected[i]) {
233 marks[i] = true;
234 identical = false;
235 } else {
236 marks[i] = false;
237 }
238 }
239 for (int i = min_len; i < max_len; ++i) {
240 marks[i] = true;
241 }
242 if (identical) return;
243 ADD_FAILURE()
244 << "Description:\n"
245 << description
246 << "\n\nExpected:\n"
247 << HexDumpWithMarks(expected, expected_len, marks.get(), max_len)
248 << "\nActual:\n"
249 << HexDumpWithMarks(actual, actual_len, marks.get(), max_len);
250}
251
252void CompareQuicDataWithHexError(
253 const string& description,
254 QuicData* actual,
255 QuicData* expected) {
256 CompareCharArraysWithHexError(
257 description,
258 actual->data(), actual->length(),
259 expected->data(), expected->length());
260}
261
[email protected]d3d15bf2013-01-30 02:51:54262static QuicPacket* ConstructPacketFromHandshakeMessage(
263 QuicGuid guid,
[email protected]14e8106c2013-03-14 16:25:33264 const CryptoHandshakeMessage& message,
265 bool should_include_version) {
[email protected]8b37a092012-10-18 21:53:49266 CryptoFramer crypto_framer;
[email protected]dc2cc742012-10-21 13:56:13267 scoped_ptr<QuicData> data(crypto_framer.ConstructHandshakeMessage(message));
[email protected]5351cc4b2013-03-03 07:22:41268 QuicFramer quic_framer(kQuicVersion1,
269 QuicDecrypter::Create(kNULL),
[email protected]14e8106c2013-03-14 16:25:33270 QuicEncrypter::Create(kNULL),
[email protected]74bda142013-03-31 02:49:11271 QuicTime::Zero(),
[email protected]14e8106c2013-03-14 16:25:33272 false);
[email protected]8b37a092012-10-18 21:53:49273
274 QuicPacketHeader header;
[email protected]c995c572013-01-18 05:43:20275 header.public_header.guid = guid;
[email protected]9db443912013-02-25 05:27:03276 header.public_header.reset_flag = false;
[email protected]14e8106c2013-03-14 16:25:33277 header.public_header.version_flag = should_include_version;
[email protected]8b37a092012-10-18 21:53:49278 header.packet_sequence_number = 1;
[email protected]9db443912013-02-25 05:27:03279 header.entropy_flag = false;
280 header.entropy_hash = 0;
281 header.fec_flag = false;
282 header.fec_entropy_flag = false;
[email protected]8b37a092012-10-18 21:53:49283 header.fec_group = 0;
284
[email protected]be24ab22012-10-22 03:01:52285 QuicStreamFrame stream_frame(kCryptoStreamId, false, 0,
[email protected]044ac2b2012-11-13 21:41:06286 data->AsStringPiece());
[email protected]8b37a092012-10-18 21:53:49287
[email protected]be24ab22012-10-22 03:01:52288 QuicFrame frame(&stream_frame);
289 QuicFrames frames;
290 frames.push_back(frame);
[email protected]9db443912013-02-25 05:27:03291 return quic_framer.ConstructFrameDataPacket(header, frames).packet;
[email protected]8b37a092012-10-18 21:53:49292}
293
[email protected]d3d15bf2013-01-30 02:51:54294QuicPacket* ConstructHandshakePacket(QuicGuid guid, CryptoTag tag) {
295 CryptoHandshakeMessage message;
[email protected]ccc66e8a2013-03-26 08:26:14296 message.set_tag(tag);
[email protected]14e8106c2013-03-14 16:25:33297 return ConstructPacketFromHandshakeMessage(guid, message, false);
[email protected]d3d15bf2013-01-30 02:51:54298}
299
[email protected]5351cc4b2013-03-03 07:22:41300size_t GetPacketLengthForOneStream(bool include_version, size_t payload) {
[email protected]14e8106c2013-03-14 16:25:33301 // TODO(wtc): the hardcoded use of NullEncrypter here seems wrong.
[email protected]5351cc4b2013-03-03 07:22:41302 return NullEncrypter().GetCiphertextSize(payload) +
303 QuicPacketCreator::StreamFramePacketOverhead(1, include_version);
304}
305
[email protected]9db443912013-02-25 05:27:03306QuicPacketEntropyHash TestEntropyCalculator::ReceivedEntropyHash(
307 QuicPacketSequenceNumber sequence_number) const {
308 return 1u;
309}
310
[email protected]8b37a092012-10-18 21:53:49311} // namespace test
[email protected]8b37a092012-10-18 21:53:49312} // namespace net