1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "net/quic/test_tools/quic_test_utils.h"
#include "base/stl_util.h"
#include "net/quic/crypto/crypto_framer.h"
#include "net/quic/crypto/crypto_handshake.h"
#include "net/quic/crypto/crypto_utils.h"
#include "net/quic/crypto/null_encrypter.h"
#include "net/quic/crypto/quic_decrypter.h"
#include "net/quic/crypto/quic_encrypter.h"
#include "net/quic/quic_framer.h"
#include "net/quic/quic_packet_creator.h"
#include "net/spdy/spdy_frame_builder.h"
using base::StringPiece;
using std::max;
using std::min;
using std::string;
using testing::_;
namespace net {
namespace test {
namespace {
// No-op alarm implementation used by MockHelper.
class TestAlarm : public QuicAlarm {
public:
explicit TestAlarm(QuicAlarm::Delegate* delegate)
: QuicAlarm(delegate) {
}
virtual void SetImpl() OVERRIDE {}
virtual void CancelImpl() OVERRIDE {}
};
} // namespace
MockFramerVisitor::MockFramerVisitor() {
// By default, we want to accept packets.
ON_CALL(*this, OnProtocolVersionMismatch(_))
.WillByDefault(testing::Return(false));
// By default, we want to accept packets.
ON_CALL(*this, OnPacketHeader(_))
.WillByDefault(testing::Return(true));
ON_CALL(*this, OnStreamFrame(_))
.WillByDefault(testing::Return(true));
ON_CALL(*this, OnAckFrame(_))
.WillByDefault(testing::Return(true));
ON_CALL(*this, OnCongestionFeedbackFrame(_))
.WillByDefault(testing::Return(true));
ON_CALL(*this, OnRstStreamFrame(_))
.WillByDefault(testing::Return(true));
ON_CALL(*this, OnConnectionCloseFrame(_))
.WillByDefault(testing::Return(true));
ON_CALL(*this, OnGoAwayFrame(_))
.WillByDefault(testing::Return(true));
}
MockFramerVisitor::~MockFramerVisitor() {
}
bool NoOpFramerVisitor::OnProtocolVersionMismatch(QuicVersion version) {
return false;
}
bool NoOpFramerVisitor::OnPacketHeader(const QuicPacketHeader& header) {
return true;
}
bool NoOpFramerVisitor::OnStreamFrame(const QuicStreamFrame& frame) {
return true;
}
bool NoOpFramerVisitor::OnAckFrame(const QuicAckFrame& frame) {
return true;
}
bool NoOpFramerVisitor::OnCongestionFeedbackFrame(
const QuicCongestionFeedbackFrame& frame) {
return true;
}
bool NoOpFramerVisitor::OnRstStreamFrame(
const QuicRstStreamFrame& frame) {
return true;
}
bool NoOpFramerVisitor::OnConnectionCloseFrame(
const QuicConnectionCloseFrame& frame) {
return true;
}
bool NoOpFramerVisitor::OnGoAwayFrame(const QuicGoAwayFrame& frame) {
return true;
}
FramerVisitorCapturingFrames::FramerVisitorCapturingFrames() : frame_count_(0) {
}
FramerVisitorCapturingFrames::~FramerVisitorCapturingFrames() {
}
bool FramerVisitorCapturingFrames::OnPacketHeader(
const QuicPacketHeader& header) {
header_ = header;
frame_count_ = 0;
return true;
}
bool FramerVisitorCapturingFrames::OnStreamFrame(const QuicStreamFrame& frame) {
// TODO(ianswett): Own the underlying string, so it will not exist outside
// this callback.
stream_frames_.push_back(frame);
++frame_count_;
return true;
}
bool FramerVisitorCapturingFrames::OnAckFrame(const QuicAckFrame& frame) {
ack_.reset(new QuicAckFrame(frame));
++frame_count_;
return true;
}
bool FramerVisitorCapturingFrames::OnCongestionFeedbackFrame(
const QuicCongestionFeedbackFrame& frame) {
feedback_.reset(new QuicCongestionFeedbackFrame(frame));
++frame_count_;
return true;
}
bool FramerVisitorCapturingFrames::OnRstStreamFrame(
const QuicRstStreamFrame& frame) {
rst_.reset(new QuicRstStreamFrame(frame));
++frame_count_;
return true;
}
bool FramerVisitorCapturingFrames::OnConnectionCloseFrame(
const QuicConnectionCloseFrame& frame) {
close_.reset(new QuicConnectionCloseFrame(frame));
++frame_count_;
return true;
}
bool FramerVisitorCapturingFrames::OnGoAwayFrame(const QuicGoAwayFrame& frame) {
goaway_.reset(new QuicGoAwayFrame(frame));
++frame_count_;
return true;
}
void FramerVisitorCapturingFrames::OnVersionNegotiationPacket(
const QuicVersionNegotiationPacket& packet) {
version_negotiation_packet_.reset(new QuicVersionNegotiationPacket(packet));
frame_count_ = 0;
}
FramerVisitorCapturingPublicReset::FramerVisitorCapturingPublicReset() {
}
FramerVisitorCapturingPublicReset::~FramerVisitorCapturingPublicReset() {
}
void FramerVisitorCapturingPublicReset::OnPublicResetPacket(
const QuicPublicResetPacket& public_reset) {
public_reset_packet_ = public_reset;
}
MockConnectionVisitor::MockConnectionVisitor() {
}
MockConnectionVisitor::~MockConnectionVisitor() {
}
MockHelper::MockHelper() {
}
MockHelper::~MockHelper() {
}
const QuicClock* MockHelper::GetClock() const {
return &clock_;
}
QuicRandom* MockHelper::GetRandomGenerator() {
return &random_generator_;
}
QuicAlarm* MockHelper::CreateAlarm(QuicAlarm::Delegate* delegate) {
return new TestAlarm(delegate);
}
void MockHelper::AdvanceTime(QuicTime::Delta delta) {
clock_.AdvanceTime(delta);
}
MockConnection::MockConnection(QuicGuid guid,
IPEndPoint address,
bool is_server)
: QuicConnection(guid, address, new testing::NiceMock<MockHelper>(),
is_server, QuicVersionMax()),
has_mock_helper_(true) {
}
MockConnection::MockConnection(QuicGuid guid,
IPEndPoint address,
QuicConnectionHelperInterface* helper,
bool is_server)
: QuicConnection(guid, address, helper, is_server, QuicVersionMax()),
has_mock_helper_(false) {
}
MockConnection::~MockConnection() {
}
void MockConnection::AdvanceTime(QuicTime::Delta delta) {
CHECK(has_mock_helper_) << "Cannot advance time unless a MockClock is being"
" used";
static_cast<MockHelper*>(helper())->AdvanceTime(delta);
}
PacketSavingConnection::PacketSavingConnection(QuicGuid guid,
IPEndPoint address,
bool is_server)
: MockConnection(guid, address, is_server) {
}
PacketSavingConnection::~PacketSavingConnection() {
STLDeleteElements(&packets_);
STLDeleteElements(&encrypted_packets_);
}
bool PacketSavingConnection::SendOrQueuePacket(
EncryptionLevel level,
QuicPacketSequenceNumber sequence_number,
QuicPacket* packet,
QuicPacketEntropyHash /* entropy_hash */,
HasRetransmittableData /* retransmittable */,
Force /* forced */) {
packets_.push_back(packet);
QuicEncryptedPacket* encrypted =
framer_.EncryptPacket(level, sequence_number, *packet);
encrypted_packets_.push_back(encrypted);
return true;
}
MockSession::MockSession(QuicConnection* connection, bool is_server)
: QuicSession(connection, DefaultQuicConfig(), is_server) {
ON_CALL(*this, WritevData(_, _, _, _, _))
.WillByDefault(testing::Return(QuicConsumedData(0, false)));
}
MockSession::~MockSession() {
}
TestSession::TestSession(QuicConnection* connection,
const QuicConfig& config,
bool is_server)
: QuicSession(connection, config, is_server),
crypto_stream_(NULL) {
}
TestSession::~TestSession() {}
void TestSession::SetCryptoStream(QuicCryptoStream* stream) {
crypto_stream_ = stream;
}
QuicCryptoStream* TestSession::GetCryptoStream() {
return crypto_stream_;
}
MockSendAlgorithm::MockSendAlgorithm() {
}
MockSendAlgorithm::~MockSendAlgorithm() {
}
MockAckNotifierDelegate::MockAckNotifierDelegate() {
}
MockAckNotifierDelegate::~MockAckNotifierDelegate() {
}
namespace {
string HexDumpWithMarks(const char* data, int length,
const bool* marks, int mark_length) {
static const char kHexChars[] = "0123456789abcdef";
static const int kColumns = 4;
const int kSizeLimit = 1024;
if (length > kSizeLimit || mark_length > kSizeLimit) {
LOG(ERROR) << "Only dumping first " << kSizeLimit << " bytes.";
length = min(length, kSizeLimit);
mark_length = min(mark_length, kSizeLimit);
}
string hex;
for (const char* row = data; length > 0;
row += kColumns, length -= kColumns) {
for (const char *p = row; p < row + 4; ++p) {
if (p < row + length) {
const bool mark =
(marks && (p - data) < mark_length && marks[p - data]);
hex += mark ? '*' : ' ';
hex += kHexChars[(*p & 0xf0) >> 4];
hex += kHexChars[*p & 0x0f];
hex += mark ? '*' : ' ';
} else {
hex += " ";
}
}
hex = hex + " ";
for (const char *p = row; p < row + 4 && p < row + length; ++p)
hex += (*p >= 0x20 && *p <= 0x7f) ? (*p) : '.';
hex = hex + '\n';
}
return hex;
}
} // namespace
void CompareCharArraysWithHexError(
const string& description,
const char* actual,
const int actual_len,
const char* expected,
const int expected_len) {
const int min_len = min(actual_len, expected_len);
const int max_len = max(actual_len, expected_len);
scoped_ptr<bool[]> marks(new bool[max_len]);
bool identical = (actual_len == expected_len);
for (int i = 0; i < min_len; ++i) {
if (actual[i] != expected[i]) {
marks[i] = true;
identical = false;
} else {
marks[i] = false;
}
}
for (int i = min_len; i < max_len; ++i) {
marks[i] = true;
}
if (identical) return;
ADD_FAILURE()
<< "Description:\n"
<< description
<< "\n\nExpected:\n"
<< HexDumpWithMarks(expected, expected_len, marks.get(), max_len)
<< "\nActual:\n"
<< HexDumpWithMarks(actual, actual_len, marks.get(), max_len);
}
static QuicPacket* ConstructPacketFromHandshakeMessage(
QuicGuid guid,
const CryptoHandshakeMessage& message,
bool should_include_version) {
CryptoFramer crypto_framer;
scoped_ptr<QuicData> data(crypto_framer.ConstructHandshakeMessage(message));
QuicFramer quic_framer(QuicVersionMax(), QuicTime::Zero(), false);
QuicPacketHeader header;
header.public_header.guid = guid;
header.public_header.reset_flag = false;
header.public_header.version_flag = should_include_version;
header.packet_sequence_number = 1;
header.entropy_flag = false;
header.entropy_hash = 0;
header.fec_flag = false;
header.fec_group = 0;
QuicStreamFrame stream_frame(kCryptoStreamId, false, 0,
data->AsStringPiece());
QuicFrame frame(&stream_frame);
QuicFrames frames;
frames.push_back(frame);
return quic_framer.BuildUnsizedDataPacket(header, frames).packet;
}
QuicPacket* ConstructHandshakePacket(QuicGuid guid, QuicTag tag) {
CryptoHandshakeMessage message;
message.set_tag(tag);
return ConstructPacketFromHandshakeMessage(guid, message, false);
}
size_t GetPacketLengthForOneStream(
QuicVersion version,
bool include_version,
QuicSequenceNumberLength sequence_number_length,
InFecGroup is_in_fec_group,
size_t* payload_length) {
*payload_length = 1;
const size_t stream_length =
NullEncrypter().GetCiphertextSize(*payload_length) +
QuicPacketCreator::StreamFramePacketOverhead(
version, PACKET_8BYTE_GUID, include_version,
sequence_number_length, is_in_fec_group);
const size_t ack_length = NullEncrypter().GetCiphertextSize(
QuicFramer::GetMinAckFrameSize()) +
GetPacketHeaderSize(PACKET_8BYTE_GUID, include_version,
sequence_number_length, is_in_fec_group);
if (stream_length < ack_length) {
*payload_length = 1 + ack_length - stream_length;
}
return NullEncrypter().GetCiphertextSize(*payload_length) +
QuicPacketCreator::StreamFramePacketOverhead(
version, PACKET_8BYTE_GUID, include_version,
sequence_number_length, is_in_fec_group);
}
// Size in bytes of the stream frame fields for an arbitrary StreamID and
// offset and the last frame in a packet.
size_t GetMinStreamFrameSize(QuicVersion version) {
return kQuicFrameTypeSize + kQuicMaxStreamIdSize + kQuicMaxStreamOffsetSize;
}
QuicPacketEntropyHash TestEntropyCalculator::EntropyHash(
QuicPacketSequenceNumber sequence_number) const {
return 1u;
}
QuicConfig DefaultQuicConfig() {
QuicConfig config;
config.SetDefaults();
return config;
}
bool TestDecompressorVisitor::OnDecompressedData(StringPiece data) {
data.AppendToString(&data_);
return true;
}
void TestDecompressorVisitor::OnDecompressionError() {
error_ = true;
}
} // namespace test
} // namespace net
|