blob: 0badf39ec0cd58965d72fecd05e698e38ecee336 [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"
[email protected]872edd9e2013-01-16 08:51:158#include "net/quic/crypto/crypto_utils.h"
[email protected]8b37a092012-10-18 21:53:499
10using std::max;
11using std::min;
12using std::string;
[email protected]cff7b7b2013-01-11 08:49:0713using testing::_;
[email protected]8b37a092012-10-18 21:53:4914
15namespace net {
[email protected]8b37a092012-10-18 21:53:4916namespace test {
17
18MockFramerVisitor::MockFramerVisitor() {
19 // By default, we want to accept packets.
[email protected]cff7b7b2013-01-11 08:49:0720 ON_CALL(*this, OnPacketHeader(_))
[email protected]8b37a092012-10-18 21:53:4921 .WillByDefault(testing::Return(true));
22}
23
[email protected]044ac2b2012-11-13 21:41:0624MockFramerVisitor::~MockFramerVisitor() {
25}
[email protected]8b37a092012-10-18 21:53:4926
27bool NoOpFramerVisitor::OnPacketHeader(const QuicPacketHeader& header) {
28 return true;
29}
30
[email protected]134e5c32012-12-12 19:20:3631FramerVisitorCapturingAcks::FramerVisitorCapturingAcks() {
32}
33
[email protected]26f3f8e2012-12-13 21:07:1934FramerVisitorCapturingAcks::~FramerVisitorCapturingAcks() {
35}
36
[email protected]4e6f0ed2012-11-02 22:15:3837bool FramerVisitorCapturingAcks::OnPacketHeader(
38 const QuicPacketHeader& header) {
39 header_ = header;
40 return true;
41}
42
43void FramerVisitorCapturingAcks::OnAckFrame(const QuicAckFrame& frame) {
[email protected]26f3f8e2012-12-13 21:07:1944 ack_.reset(new QuicAckFrame(frame));
45}
46
47void FramerVisitorCapturingAcks::OnCongestionFeedbackFrame(
48 const QuicCongestionFeedbackFrame& frame) {
49 feedback_.reset(new QuicCongestionFeedbackFrame(frame));
[email protected]4e6f0ed2012-11-02 22:15:3850}
51
[email protected]9c0b1352012-11-04 00:03:2752MockHelper::MockHelper() {
53}
54
55MockHelper::~MockHelper() {
56}
57
[email protected]97693d12012-11-16 16:05:0058const QuicClock* MockHelper::GetClock() const {
[email protected]9c0b1352012-11-04 00:03:2759 return &clock_;
60}
61
[email protected]9558c5d32012-12-22 00:08:1462QuicRandom* MockHelper::GetRandomGenerator() {
63 return &random_generator_;
64}
65
[email protected]4e6f0ed2012-11-02 22:15:3866MockConnectionVisitor::MockConnectionVisitor() {
67}
68
69MockConnectionVisitor::~MockConnectionVisitor() {
70}
71
72MockScheduler::MockScheduler()
73 : QuicSendScheduler(NULL, kFixRate) {
74}
75
76MockScheduler::~MockScheduler() {
77}
78
[email protected]044ac2b2012-11-13 21:41:0679MockConnection::MockConnection(QuicGuid guid, IPEndPoint address)
[email protected]e13201d82012-12-12 05:00:3280 : QuicConnection(guid, address, new MockHelper()),
81 helper_(helper()) {
[email protected]044ac2b2012-11-13 21:41:0682}
83
[email protected]872edd9e2013-01-16 08:51:1584MockConnection::MockConnection(QuicGuid guid,
85 IPEndPoint address,
86 QuicConnectionHelperInterface* helper)
87 : QuicConnection(guid, address, helper),
88 helper_(helper) {
89}
90
[email protected]044ac2b2012-11-13 21:41:0691MockConnection::~MockConnection() {
92}
93
94PacketSavingConnection::PacketSavingConnection(QuicGuid guid,
95 IPEndPoint address)
96 : MockConnection(guid, address) {
97}
98
99PacketSavingConnection::~PacketSavingConnection() {
100}
101
102bool PacketSavingConnection::SendPacket(QuicPacketSequenceNumber number,
103 QuicPacket* packet,
104 bool should_resend,
105 bool force,
106 bool is_retransmit) {
107 packets_.push_back(packet);
108 return true;
109}
110
111MockSession::MockSession(QuicConnection* connection, bool is_server)
112 : QuicSession(connection, is_server) {
[email protected]cff7b7b2013-01-11 08:49:07113 ON_CALL(*this, WriteData(_, _, _, _))
114 .WillByDefault(testing::Return(QuicConsumedData(0, false)));
[email protected]044ac2b2012-11-13 21:41:06115}
116
117MockSession::~MockSession() {
118}
119
[email protected]8b37a092012-10-18 21:53:49120namespace {
121
122string HexDumpWithMarks(const char* data, int length,
123 const bool* marks, int mark_length) {
124 static const char kHexChars[] = "0123456789abcdef";
125 static const int kColumns = 4;
126
127 const int kSizeLimit = 1024;
128 if (length > kSizeLimit || mark_length > kSizeLimit) {
129 LOG(ERROR) << "Only dumping first " << kSizeLimit << " bytes.";
130 length = min(length, kSizeLimit);
131 mark_length = min(mark_length, kSizeLimit);
132 }
133
134 string hex;
135 for (const char* row = data; length > 0;
136 row += kColumns, length -= kColumns) {
137 for (const char *p = row; p < row + 4; ++p) {
138 if (p < row + length) {
139 const bool mark =
140 (marks && (p - data) < mark_length && marks[p - data]);
141 hex += mark ? '*' : ' ';
142 hex += kHexChars[(*p & 0xf0) >> 4];
143 hex += kHexChars[*p & 0x0f];
144 hex += mark ? '*' : ' ';
145 } else {
146 hex += " ";
147 }
148 }
149 hex = hex + " ";
150
151 for (const char *p = row; p < row + 4 && p < row + length; ++p)
152 hex += (*p >= 0x20 && *p <= 0x7f) ? (*p) : '.';
153
154 hex = hex + '\n';
155 }
156 return hex;
157}
158
159} // namespace
160
161void CompareCharArraysWithHexError(
162 const string& description,
163 const char* actual,
164 const int actual_len,
165 const char* expected,
166 const int expected_len) {
167 const int min_len = min(actual_len, expected_len);
168 const int max_len = max(actual_len, expected_len);
169 scoped_array<bool> marks(new bool[max_len]);
170 bool identical = (actual_len == expected_len);
171 for (int i = 0; i < min_len; ++i) {
172 if (actual[i] != expected[i]) {
173 marks[i] = true;
174 identical = false;
175 } else {
176 marks[i] = false;
177 }
178 }
179 for (int i = min_len; i < max_len; ++i) {
180 marks[i] = true;
181 }
182 if (identical) return;
183 ADD_FAILURE()
184 << "Description:\n"
185 << description
186 << "\n\nExpected:\n"
187 << HexDumpWithMarks(expected, expected_len, marks.get(), max_len)
188 << "\nActual:\n"
189 << HexDumpWithMarks(actual, actual_len, marks.get(), max_len);
190}
191
192void CompareQuicDataWithHexError(
193 const string& description,
194 QuicData* actual,
195 QuicData* expected) {
196 CompareCharArraysWithHexError(
197 description,
198 actual->data(), actual->length(),
199 expected->data(), expected->length());
200}
201
202QuicPacket* ConstructHandshakePacket(QuicGuid guid, CryptoTag tag) {
203 CryptoHandshakeMessage message;
204 message.tag = tag;
205 CryptoFramer crypto_framer;
[email protected]dc2cc742012-10-21 13:56:13206 scoped_ptr<QuicData> data(crypto_framer.ConstructHandshakeMessage(message));
[email protected]8b37a092012-10-18 21:53:49207 QuicFramer quic_framer(QuicDecrypter::Create(kNULL),
208 QuicEncrypter::Create(kNULL));
209
210 QuicPacketHeader header;
211 header.guid = guid;
[email protected]8b37a092012-10-18 21:53:49212 header.packet_sequence_number = 1;
[email protected]8b37a092012-10-18 21:53:49213 header.flags = PACKET_FLAGS_NONE;
214 header.fec_group = 0;
215
[email protected]be24ab22012-10-22 03:01:52216 QuicStreamFrame stream_frame(kCryptoStreamId, false, 0,
[email protected]044ac2b2012-11-13 21:41:06217 data->AsStringPiece());
[email protected]8b37a092012-10-18 21:53:49218
[email protected]be24ab22012-10-22 03:01:52219 QuicFrame frame(&stream_frame);
220 QuicFrames frames;
221 frames.push_back(frame);
[email protected]610a7e942012-12-18 00:21:39222 return quic_framer.ConstructFrameDataPacket(header, frames);
[email protected]8b37a092012-10-18 21:53:49223}
224
[email protected]872edd9e2013-01-16 08:51:15225QuicPacket* ConstructClientHelloPacket(QuicGuid guid,
226 const QuicClock* clock,
227 QuicRandom* random_generator) {
228 QuicClientCryptoConfig config;
229 config.SetDefaults();
230 string nonce;
231 CryptoUtils::GenerateNonce(clock, random_generator, &nonce);
232
233 CryptoHandshakeMessage message;
234 CryptoUtils::FillClientHelloMessage(config, nonce, &message);
235 CryptoFramer crypto_framer;
236 scoped_ptr<QuicData> data(crypto_framer.ConstructHandshakeMessage(message));
237 QuicFramer quic_framer(QuicDecrypter::Create(kNULL),
238 QuicEncrypter::Create(kNULL));
239
240 QuicPacketHeader header;
241 header.guid = guid;
242 header.packet_sequence_number = 1;
243 header.flags = PACKET_FLAGS_NONE;
244 header.fec_group = 0;
245
246 QuicStreamFrame stream_frame(kCryptoStreamId, false, 0,
247 data->AsStringPiece());
248
249 QuicFrame frame(&stream_frame);
250 QuicFrames frames;
251 frames.push_back(frame);
252 return quic_framer.ConstructFrameDataPacket(header, frames);
253}
254
255
[email protected]8b37a092012-10-18 21:53:49256} // namespace test
[email protected]8b37a092012-10-18 21:53:49257} // namespace net