blob: 99c7511daeeb8ff4cc48e8c3b072f46971ef5ca0 [file] [log] [blame]
QUICHE teama6ef0a62019-03-07 20:34:33 -05001// 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/third_party/quiche/src/quic/test_tools/quic_test_utils.h"
6
7#include <algorithm>
vasilvvc2018482019-04-26 15:47:55 -07008#include <cstdint>
QUICHE teama6ef0a62019-03-07 20:34:33 -05009#include <memory>
10
vasilvvc2018482019-04-26 15:47:55 -070011#include "third_party/boringssl/src/include/openssl/chacha.h"
QUICHE teama6ef0a62019-03-07 20:34:33 -050012#include "third_party/boringssl/src/include/openssl/sha.h"
13#include "net/third_party/quiche/src/quic/core/crypto/crypto_framer.h"
14#include "net/third_party/quiche/src/quic/core/crypto/crypto_handshake.h"
15#include "net/third_party/quiche/src/quic/core/crypto/crypto_utils.h"
16#include "net/third_party/quiche/src/quic/core/crypto/null_encrypter.h"
17#include "net/third_party/quiche/src/quic/core/crypto/quic_decrypter.h"
18#include "net/third_party/quiche/src/quic/core/crypto/quic_encrypter.h"
19#include "net/third_party/quiche/src/quic/core/quic_data_writer.h"
20#include "net/third_party/quiche/src/quic/core/quic_framer.h"
21#include "net/third_party/quiche/src/quic/core/quic_packet_creator.h"
22#include "net/third_party/quiche/src/quic/core/quic_utils.h"
23#include "net/third_party/quiche/src/quic/platform/api/quic_arraysize.h"
24#include "net/third_party/quiche/src/quic/platform/api/quic_endian.h"
25#include "net/third_party/quiche/src/quic/platform/api/quic_flags.h"
26#include "net/third_party/quiche/src/quic/platform/api/quic_logging.h"
27#include "net/third_party/quiche/src/quic/platform/api/quic_ptr_util.h"
28#include "net/third_party/quiche/src/quic/test_tools/crypto_test_utils.h"
29#include "net/third_party/quiche/src/quic/test_tools/quic_config_peer.h"
30#include "net/third_party/quiche/src/quic/test_tools/quic_connection_peer.h"
31#include "net/third_party/quiche/src/spdy/core/spdy_frame_builder.h"
32
33using testing::_;
34using testing::Invoke;
35
36namespace quic {
37namespace test {
38
39QuicConnectionId TestConnectionId() {
40 // Chosen by fair dice roll.
41 // Guaranteed to be random.
42 return TestConnectionId(42);
43}
44
45QuicConnectionId TestConnectionId(uint64_t connection_number) {
46 const uint64_t connection_id64_net =
47 QuicEndian::HostToNet64(connection_number);
48 return QuicConnectionId(reinterpret_cast<const char*>(&connection_id64_net),
49 sizeof(connection_id64_net));
50}
51
QUICHE team8e2e4532019-03-14 14:37:56 -070052QuicConnectionId TestConnectionIdNineBytesLong(uint64_t connection_number) {
53 const uint64_t connection_number_net =
54 QuicEndian::HostToNet64(connection_number);
55 char connection_id_bytes[9] = {};
56 static_assert(
57 sizeof(connection_id_bytes) == 1 + sizeof(connection_number_net),
58 "bad lengths");
59 memcpy(connection_id_bytes + 1, &connection_number_net,
60 sizeof(connection_number_net));
61 return QuicConnectionId(connection_id_bytes, sizeof(connection_id_bytes));
62}
63
QUICHE teama6ef0a62019-03-07 20:34:33 -050064uint64_t TestConnectionIdToUInt64(QuicConnectionId connection_id) {
65 DCHECK_EQ(connection_id.length(), kQuicDefaultConnectionIdLength);
66 uint64_t connection_id64_net = 0;
67 memcpy(&connection_id64_net, connection_id.data(),
68 std::min<size_t>(static_cast<size_t>(connection_id.length()),
69 sizeof(connection_id64_net)));
70 return QuicEndian::NetToHost64(connection_id64_net);
71}
72
73QuicAckFrame InitAckFrame(const std::vector<QuicAckBlock>& ack_blocks) {
74 DCHECK_GT(ack_blocks.size(), 0u);
75
76 QuicAckFrame ack;
77 QuicPacketNumber end_of_previous_block(1);
78 for (const QuicAckBlock& block : ack_blocks) {
79 DCHECK_GE(block.start, end_of_previous_block);
80 DCHECK_GT(block.limit, block.start);
81 ack.packets.AddRange(block.start, block.limit);
82 end_of_previous_block = block.limit;
83 }
84
85 ack.largest_acked = ack.packets.Max();
86
87 return ack;
88}
89
90QuicAckFrame InitAckFrame(uint64_t largest_acked) {
91 return InitAckFrame(QuicPacketNumber(largest_acked));
92}
93
94QuicAckFrame InitAckFrame(QuicPacketNumber largest_acked) {
95 return InitAckFrame({{QuicPacketNumber(1), largest_acked + 1}});
96}
97
98QuicAckFrame MakeAckFrameWithAckBlocks(size_t num_ack_blocks,
99 uint64_t least_unacked) {
100 QuicAckFrame ack;
101 ack.largest_acked = QuicPacketNumber(2 * num_ack_blocks + least_unacked);
102 // Add enough received packets to get num_ack_blocks ack blocks.
103 for (QuicPacketNumber i = QuicPacketNumber(2);
104 i < QuicPacketNumber(2 * num_ack_blocks + 1); i += 2) {
105 ack.packets.Add(i + least_unacked);
106 }
107 return ack;
108}
109
110std::unique_ptr<QuicPacket> BuildUnsizedDataPacket(
111 QuicFramer* framer,
112 const QuicPacketHeader& header,
113 const QuicFrames& frames) {
dschinazi66dea072019-04-09 11:41:06 -0700114 const size_t max_plaintext_size =
115 framer->GetMaxPlaintextSize(kMaxOutgoingPacketSize);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500116 size_t packet_size = GetPacketHeaderSize(framer->transport_version(), header);
117 for (size_t i = 0; i < frames.size(); ++i) {
118 DCHECK_LE(packet_size, max_plaintext_size);
119 bool first_frame = i == 0;
120 bool last_frame = i == frames.size() - 1;
121 const size_t frame_size = framer->GetSerializedFrameLength(
122 frames[i], max_plaintext_size - packet_size, first_frame, last_frame,
123 header.packet_number_length);
124 DCHECK(frame_size);
125 packet_size += frame_size;
126 }
127 return BuildUnsizedDataPacket(framer, header, frames, packet_size);
128}
129
130std::unique_ptr<QuicPacket> BuildUnsizedDataPacket(
131 QuicFramer* framer,
132 const QuicPacketHeader& header,
133 const QuicFrames& frames,
134 size_t packet_size) {
135 char* buffer = new char[packet_size];
136 size_t length = framer->BuildDataPacket(header, frames, buffer, packet_size,
QUICHE team6987b4a2019-03-15 16:23:04 -0700137 ENCRYPTION_INITIAL);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500138 DCHECK_NE(0u, length);
139 // Re-construct the data packet with data ownership.
140 return QuicMakeUnique<QuicPacket>(
141 buffer, length, /* owns_buffer */ true,
142 GetIncludedDestinationConnectionIdLength(header),
143 GetIncludedSourceConnectionIdLength(header), header.version_flag,
144 header.nonce != nullptr, header.packet_number_length,
145 header.retry_token_length_length, header.retry_token.length(),
146 header.length_length);
147}
148
vasilvvc48c8712019-03-11 13:38:16 -0700149std::string Sha1Hash(QuicStringPiece data) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500150 char buffer[SHA_DIGEST_LENGTH];
151 SHA1(reinterpret_cast<const uint8_t*>(data.data()), data.size(),
152 reinterpret_cast<uint8_t*>(buffer));
vasilvvc48c8712019-03-11 13:38:16 -0700153 return std::string(buffer, QUIC_ARRAYSIZE(buffer));
QUICHE teama6ef0a62019-03-07 20:34:33 -0500154}
155
bnc5b3c3be2019-06-25 10:37:09 -0700156bool ClearControlFrame(const QuicFrame& frame) {
157 DeleteFrame(&const_cast<QuicFrame&>(frame));
158 return true;
159}
160
QUICHE teama6ef0a62019-03-07 20:34:33 -0500161uint64_t SimpleRandom::RandUint64() {
vasilvvc2018482019-04-26 15:47:55 -0700162 uint64_t result;
163 RandBytes(&result, sizeof(result));
164 return result;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500165}
166
167void SimpleRandom::RandBytes(void* data, size_t len) {
vasilvvc2018482019-04-26 15:47:55 -0700168 uint8_t* data_bytes = reinterpret_cast<uint8_t*>(data);
169 while (len > 0) {
170 const size_t buffer_left = sizeof(buffer_) - buffer_offset_;
171 const size_t to_copy = std::min(buffer_left, len);
172 memcpy(data_bytes, buffer_ + buffer_offset_, to_copy);
173 data_bytes += to_copy;
174 buffer_offset_ += to_copy;
175 len -= to_copy;
176
177 if (buffer_offset_ == sizeof(buffer_)) {
178 FillBuffer();
179 }
QUICHE teama6ef0a62019-03-07 20:34:33 -0500180 }
181}
182
vasilvvc2018482019-04-26 15:47:55 -0700183void SimpleRandom::FillBuffer() {
184 uint8_t nonce[12];
185 memcpy(nonce, buffer_, sizeof(nonce));
186 CRYPTO_chacha_20(buffer_, buffer_, sizeof(buffer_), key_, nonce, 0);
187 buffer_offset_ = 0;
188}
189
190void SimpleRandom::set_seed(uint64_t seed) {
191 static_assert(sizeof(key_) == SHA256_DIGEST_LENGTH, "Key has to be 256 bits");
192 SHA256(reinterpret_cast<const uint8_t*>(&seed), sizeof(seed), key_);
193
194 memset(buffer_, 0, sizeof(buffer_));
195 FillBuffer();
196}
197
QUICHE teama6ef0a62019-03-07 20:34:33 -0500198MockFramerVisitor::MockFramerVisitor() {
199 // By default, we want to accept packets.
fayang8aba1ff2019-06-21 12:00:54 -0700200 ON_CALL(*this, OnProtocolVersionMismatch(_))
QUICHE teama6ef0a62019-03-07 20:34:33 -0500201 .WillByDefault(testing::Return(false));
202
203 // By default, we want to accept packets.
204 ON_CALL(*this, OnUnauthenticatedHeader(_))
205 .WillByDefault(testing::Return(true));
206
207 ON_CALL(*this, OnUnauthenticatedPublicHeader(_))
208 .WillByDefault(testing::Return(true));
209
210 ON_CALL(*this, OnPacketHeader(_)).WillByDefault(testing::Return(true));
211
212 ON_CALL(*this, OnStreamFrame(_)).WillByDefault(testing::Return(true));
213
214 ON_CALL(*this, OnCryptoFrame(_)).WillByDefault(testing::Return(true));
215
216 ON_CALL(*this, OnStopWaitingFrame(_)).WillByDefault(testing::Return(true));
217
218 ON_CALL(*this, OnPaddingFrame(_)).WillByDefault(testing::Return(true));
219
220 ON_CALL(*this, OnPingFrame(_)).WillByDefault(testing::Return(true));
221
222 ON_CALL(*this, OnRstStreamFrame(_)).WillByDefault(testing::Return(true));
223
224 ON_CALL(*this, OnConnectionCloseFrame(_))
225 .WillByDefault(testing::Return(true));
226
QUICHE teama6ef0a62019-03-07 20:34:33 -0500227 ON_CALL(*this, OnStopSendingFrame(_)).WillByDefault(testing::Return(true));
228
229 ON_CALL(*this, OnPathChallengeFrame(_)).WillByDefault(testing::Return(true));
230
231 ON_CALL(*this, OnPathResponseFrame(_)).WillByDefault(testing::Return(true));
232
233 ON_CALL(*this, OnGoAwayFrame(_)).WillByDefault(testing::Return(true));
fkastenholz3c4eabf2019-04-22 07:49:59 -0700234 ON_CALL(*this, OnMaxStreamsFrame(_)).WillByDefault(testing::Return(true));
235 ON_CALL(*this, OnStreamsBlockedFrame(_)).WillByDefault(testing::Return(true));
QUICHE teama6ef0a62019-03-07 20:34:33 -0500236}
237
238MockFramerVisitor::~MockFramerVisitor() {}
239
fayang8aba1ff2019-06-21 12:00:54 -0700240bool NoOpFramerVisitor::OnProtocolVersionMismatch(
241 ParsedQuicVersion /*version*/) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500242 return false;
243}
244
245bool NoOpFramerVisitor::OnUnauthenticatedPublicHeader(
dschinazi17d42422019-06-18 16:35:07 -0700246 const QuicPacketHeader& /*header*/) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500247 return true;
248}
249
250bool NoOpFramerVisitor::OnUnauthenticatedHeader(
dschinazi17d42422019-06-18 16:35:07 -0700251 const QuicPacketHeader& /*header*/) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500252 return true;
253}
254
dschinazi17d42422019-06-18 16:35:07 -0700255bool NoOpFramerVisitor::OnPacketHeader(const QuicPacketHeader& /*header*/) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500256 return true;
257}
258
dschinazi17d42422019-06-18 16:35:07 -0700259void NoOpFramerVisitor::OnCoalescedPacket(
260 const QuicEncryptedPacket& /*packet*/) {}
QUICHE teama6ef0a62019-03-07 20:34:33 -0500261
dschinazi17d42422019-06-18 16:35:07 -0700262bool NoOpFramerVisitor::OnStreamFrame(const QuicStreamFrame& /*frame*/) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500263 return true;
264}
265
dschinazi17d42422019-06-18 16:35:07 -0700266bool NoOpFramerVisitor::OnCryptoFrame(const QuicCryptoFrame& /*frame*/) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500267 return true;
268}
269
dschinazi17d42422019-06-18 16:35:07 -0700270bool NoOpFramerVisitor::OnAckFrameStart(QuicPacketNumber /*largest_acked*/,
271 QuicTime::Delta /*ack_delay_time*/) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500272 return true;
273}
274
dschinazi17d42422019-06-18 16:35:07 -0700275bool NoOpFramerVisitor::OnAckRange(QuicPacketNumber /*start*/,
276 QuicPacketNumber /*end*/) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500277 return true;
278}
279
dschinazi17d42422019-06-18 16:35:07 -0700280bool NoOpFramerVisitor::OnAckTimestamp(QuicPacketNumber /*packet_number*/,
281 QuicTime /*timestamp*/) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500282 return true;
283}
284
dschinazi17d42422019-06-18 16:35:07 -0700285bool NoOpFramerVisitor::OnAckFrameEnd(QuicPacketNumber /*start*/) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500286 return true;
287}
288
dschinazi17d42422019-06-18 16:35:07 -0700289bool NoOpFramerVisitor::OnStopWaitingFrame(
290 const QuicStopWaitingFrame& /*frame*/) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500291 return true;
292}
293
dschinazi17d42422019-06-18 16:35:07 -0700294bool NoOpFramerVisitor::OnPaddingFrame(const QuicPaddingFrame& /*frame*/) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500295 return true;
296}
297
dschinazi17d42422019-06-18 16:35:07 -0700298bool NoOpFramerVisitor::OnPingFrame(const QuicPingFrame& /*frame*/) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500299 return true;
300}
301
dschinazi17d42422019-06-18 16:35:07 -0700302bool NoOpFramerVisitor::OnRstStreamFrame(const QuicRstStreamFrame& /*frame*/) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500303 return true;
304}
305
306bool NoOpFramerVisitor::OnConnectionCloseFrame(
dschinazi17d42422019-06-18 16:35:07 -0700307 const QuicConnectionCloseFrame& /*frame*/) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500308 return true;
309}
310
QUICHE teama6ef0a62019-03-07 20:34:33 -0500311bool NoOpFramerVisitor::OnNewConnectionIdFrame(
dschinazi17d42422019-06-18 16:35:07 -0700312 const QuicNewConnectionIdFrame& /*frame*/) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500313 return true;
314}
315
316bool NoOpFramerVisitor::OnRetireConnectionIdFrame(
dschinazi17d42422019-06-18 16:35:07 -0700317 const QuicRetireConnectionIdFrame& /*frame*/) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500318 return true;
319}
320
dschinazi17d42422019-06-18 16:35:07 -0700321bool NoOpFramerVisitor::OnNewTokenFrame(const QuicNewTokenFrame& /*frame*/) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500322 return true;
323}
324
dschinazi17d42422019-06-18 16:35:07 -0700325bool NoOpFramerVisitor::OnStopSendingFrame(
326 const QuicStopSendingFrame& /*frame*/) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500327 return true;
328}
329
330bool NoOpFramerVisitor::OnPathChallengeFrame(
dschinazi17d42422019-06-18 16:35:07 -0700331 const QuicPathChallengeFrame& /*frame*/) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500332 return true;
333}
334
335bool NoOpFramerVisitor::OnPathResponseFrame(
dschinazi17d42422019-06-18 16:35:07 -0700336 const QuicPathResponseFrame& /*frame*/) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500337 return true;
338}
339
dschinazi17d42422019-06-18 16:35:07 -0700340bool NoOpFramerVisitor::OnGoAwayFrame(const QuicGoAwayFrame& /*frame*/) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500341 return true;
342}
343
dschinazi17d42422019-06-18 16:35:07 -0700344bool NoOpFramerVisitor::OnMaxStreamsFrame(
345 const QuicMaxStreamsFrame& /*frame*/) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500346 return true;
347}
348
fkastenholz3c4eabf2019-04-22 07:49:59 -0700349bool NoOpFramerVisitor::OnStreamsBlockedFrame(
dschinazi17d42422019-06-18 16:35:07 -0700350 const QuicStreamsBlockedFrame& /*frame*/) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500351 return true;
352}
353
354bool NoOpFramerVisitor::OnWindowUpdateFrame(
dschinazi17d42422019-06-18 16:35:07 -0700355 const QuicWindowUpdateFrame& /*frame*/) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500356 return true;
357}
358
dschinazi17d42422019-06-18 16:35:07 -0700359bool NoOpFramerVisitor::OnBlockedFrame(const QuicBlockedFrame& /*frame*/) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500360 return true;
361}
362
dschinazi17d42422019-06-18 16:35:07 -0700363bool NoOpFramerVisitor::OnMessageFrame(const QuicMessageFrame& /*frame*/) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500364 return true;
365}
366
dschinazi17d42422019-06-18 16:35:07 -0700367bool NoOpFramerVisitor::IsValidStatelessResetToken(
368 QuicUint128 /*token*/) const {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500369 return false;
370}
371
372MockQuicConnectionVisitor::MockQuicConnectionVisitor() {}
373
374MockQuicConnectionVisitor::~MockQuicConnectionVisitor() {}
375
376MockQuicConnectionHelper::MockQuicConnectionHelper() {}
377
378MockQuicConnectionHelper::~MockQuicConnectionHelper() {}
379
380const QuicClock* MockQuicConnectionHelper::GetClock() const {
381 return &clock_;
382}
383
384QuicRandom* MockQuicConnectionHelper::GetRandomGenerator() {
385 return &random_generator_;
386}
387
388QuicAlarm* MockAlarmFactory::CreateAlarm(QuicAlarm::Delegate* delegate) {
389 return new MockAlarmFactory::TestAlarm(
390 QuicArenaScopedPtr<QuicAlarm::Delegate>(delegate));
391}
392
393QuicArenaScopedPtr<QuicAlarm> MockAlarmFactory::CreateAlarm(
394 QuicArenaScopedPtr<QuicAlarm::Delegate> delegate,
395 QuicConnectionArena* arena) {
396 if (arena != nullptr) {
397 return arena->New<TestAlarm>(std::move(delegate));
398 } else {
399 return QuicArenaScopedPtr<TestAlarm>(new TestAlarm(std::move(delegate)));
400 }
401}
402
403QuicBufferAllocator* MockQuicConnectionHelper::GetStreamSendBufferAllocator() {
404 return &buffer_allocator_;
405}
406
407void MockQuicConnectionHelper::AdvanceTime(QuicTime::Delta delta) {
408 clock_.AdvanceTime(delta);
409}
410
411MockQuicConnection::MockQuicConnection(MockQuicConnectionHelper* helper,
412 MockAlarmFactory* alarm_factory,
413 Perspective perspective)
414 : MockQuicConnection(TestConnectionId(),
415 QuicSocketAddress(TestPeerIPAddress(), kTestPort),
416 helper,
417 alarm_factory,
418 perspective,
419 ParsedVersionOfIndex(CurrentSupportedVersions(), 0)) {}
420
421MockQuicConnection::MockQuicConnection(QuicSocketAddress address,
422 MockQuicConnectionHelper* helper,
423 MockAlarmFactory* alarm_factory,
424 Perspective perspective)
425 : MockQuicConnection(TestConnectionId(),
426 address,
427 helper,
428 alarm_factory,
429 perspective,
430 ParsedVersionOfIndex(CurrentSupportedVersions(), 0)) {}
431
432MockQuicConnection::MockQuicConnection(QuicConnectionId connection_id,
433 MockQuicConnectionHelper* helper,
434 MockAlarmFactory* alarm_factory,
435 Perspective perspective)
436 : MockQuicConnection(connection_id,
437 QuicSocketAddress(TestPeerIPAddress(), kTestPort),
438 helper,
439 alarm_factory,
440 perspective,
441 ParsedVersionOfIndex(CurrentSupportedVersions(), 0)) {}
442
443MockQuicConnection::MockQuicConnection(
444 MockQuicConnectionHelper* helper,
445 MockAlarmFactory* alarm_factory,
446 Perspective perspective,
447 const ParsedQuicVersionVector& supported_versions)
448 : MockQuicConnection(TestConnectionId(),
449 QuicSocketAddress(TestPeerIPAddress(), kTestPort),
450 helper,
451 alarm_factory,
452 perspective,
453 supported_versions) {}
454
455MockQuicConnection::MockQuicConnection(
456 QuicConnectionId connection_id,
457 QuicSocketAddress address,
458 MockQuicConnectionHelper* helper,
459 MockAlarmFactory* alarm_factory,
460 Perspective perspective,
461 const ParsedQuicVersionVector& supported_versions)
462 : QuicConnection(connection_id,
463 address,
464 helper,
465 alarm_factory,
466 new testing::NiceMock<MockPacketWriter>(),
467 /* owns_writer= */ true,
468 perspective,
469 supported_versions) {
470 ON_CALL(*this, OnError(_))
471 .WillByDefault(
472 Invoke(this, &PacketSavingConnection::QuicConnection_OnError));
473 ON_CALL(*this, SendCryptoData(_, _, _))
474 .WillByDefault(
475 Invoke(this, &MockQuicConnection::QuicConnection_SendCryptoData));
476
477 SetSelfAddress(QuicSocketAddress(QuicIpAddress::Any4(), 5));
478}
479
480MockQuicConnection::~MockQuicConnection() {}
481
482void MockQuicConnection::AdvanceTime(QuicTime::Delta delta) {
483 static_cast<MockQuicConnectionHelper*>(helper())->AdvanceTime(delta);
484}
485
dschinazi17d42422019-06-18 16:35:07 -0700486bool MockQuicConnection::OnProtocolVersionMismatch(
fayang8aba1ff2019-06-21 12:00:54 -0700487 ParsedQuicVersion /*version*/) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500488 return false;
489}
490
491PacketSavingConnection::PacketSavingConnection(MockQuicConnectionHelper* helper,
492 MockAlarmFactory* alarm_factory,
493 Perspective perspective)
494 : MockQuicConnection(helper, alarm_factory, perspective) {}
495
496PacketSavingConnection::PacketSavingConnection(
497 MockQuicConnectionHelper* helper,
498 MockAlarmFactory* alarm_factory,
499 Perspective perspective,
500 const ParsedQuicVersionVector& supported_versions)
501 : MockQuicConnection(helper,
502 alarm_factory,
503 perspective,
504 supported_versions) {}
505
506PacketSavingConnection::~PacketSavingConnection() {}
507
508void PacketSavingConnection::SendOrQueuePacket(SerializedPacket* packet) {
509 encrypted_packets_.push_back(QuicMakeUnique<QuicEncryptedPacket>(
510 CopyBuffer(*packet), packet->encrypted_length, true));
511 clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(10));
512 // Transfer ownership of the packet to the SentPacketManager and the
513 // ack notifier to the AckNotifierManager.
514 QuicConnectionPeer::GetSentPacketManager(this)->OnPacketSent(
515 packet, QuicPacketNumber(), clock_.ApproximateNow(), NOT_RETRANSMISSION,
516 HAS_RETRANSMITTABLE_DATA);
517}
518
519MockQuicSession::MockQuicSession(QuicConnection* connection)
520 : MockQuicSession(connection, true) {}
521
522MockQuicSession::MockQuicSession(QuicConnection* connection,
523 bool create_mock_crypto_stream)
524 : QuicSession(connection,
525 nullptr,
526 DefaultQuicConfig(),
zhongyi546cc452019-04-12 15:27:49 -0700527 connection->supported_versions()) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500528 if (create_mock_crypto_stream) {
529 crypto_stream_ = QuicMakeUnique<MockQuicCryptoStream>(this);
530 }
531 ON_CALL(*this, WritevData(_, _, _, _, _))
532 .WillByDefault(testing::Return(QuicConsumedData(0, false)));
533}
534
535MockQuicSession::~MockQuicSession() {
536 delete connection();
537}
538
539QuicCryptoStream* MockQuicSession::GetMutableCryptoStream() {
540 return crypto_stream_.get();
541}
542
543const QuicCryptoStream* MockQuicSession::GetCryptoStream() const {
544 return crypto_stream_.get();
545}
546
547void MockQuicSession::SetCryptoStream(QuicCryptoStream* crypto_stream) {
548 crypto_stream_.reset(crypto_stream);
549}
550
551// static
552QuicConsumedData MockQuicSession::ConsumeData(QuicStream* stream,
553 QuicStreamId /*id*/,
554 size_t write_length,
555 QuicStreamOffset offset,
556 StreamSendingState state) {
557 if (write_length > 0) {
558 auto buf = QuicMakeUnique<char[]>(write_length);
559 QuicDataWriter writer(write_length, buf.get(), HOST_BYTE_ORDER);
560 stream->WriteStreamData(offset, write_length, &writer);
561 } else {
562 DCHECK(state != NO_FIN);
563 }
564 return QuicConsumedData(write_length, state != NO_FIN);
565}
566
567MockQuicCryptoStream::MockQuicCryptoStream(QuicSession* session)
568 : QuicCryptoStream(session), params_(new QuicCryptoNegotiatedParameters) {}
569
570MockQuicCryptoStream::~MockQuicCryptoStream() {}
571
572bool MockQuicCryptoStream::encryption_established() const {
573 return false;
574}
575
576bool MockQuicCryptoStream::handshake_confirmed() const {
577 return false;
578}
579
580const QuicCryptoNegotiatedParameters&
581MockQuicCryptoStream::crypto_negotiated_params() const {
582 return *params_;
583}
584
585CryptoMessageParser* MockQuicCryptoStream::crypto_message_parser() {
586 return &crypto_framer_;
587}
588
589MockQuicSpdySession::MockQuicSpdySession(QuicConnection* connection)
590 : MockQuicSpdySession(connection, true) {}
591
592MockQuicSpdySession::MockQuicSpdySession(QuicConnection* connection,
593 bool create_mock_crypto_stream)
594 : QuicSpdySession(connection,
595 nullptr,
596 DefaultQuicConfig(),
597 connection->supported_versions()) {
598 if (create_mock_crypto_stream) {
599 crypto_stream_ = QuicMakeUnique<MockQuicCryptoStream>(this);
600 }
601
602 ON_CALL(*this, WritevData(_, _, _, _, _))
603 .WillByDefault(testing::Return(QuicConsumedData(0, false)));
604}
605
606MockQuicSpdySession::~MockQuicSpdySession() {
607 delete connection();
608}
609
610QuicCryptoStream* MockQuicSpdySession::GetMutableCryptoStream() {
611 return crypto_stream_.get();
612}
613
614const QuicCryptoStream* MockQuicSpdySession::GetCryptoStream() const {
615 return crypto_stream_.get();
616}
617
618void MockQuicSpdySession::SetCryptoStream(QuicCryptoStream* crypto_stream) {
619 crypto_stream_.reset(crypto_stream);
620}
621
622TestQuicSpdyServerSession::TestQuicSpdyServerSession(
623 QuicConnection* connection,
624 const QuicConfig& config,
625 const ParsedQuicVersionVector& supported_versions,
626 const QuicCryptoServerConfig* crypto_config,
627 QuicCompressedCertsCache* compressed_certs_cache)
628 : QuicServerSessionBase(config,
629 supported_versions,
630 connection,
631 &visitor_,
632 &helper_,
633 crypto_config,
634 compressed_certs_cache) {
635 Initialize();
636 ON_CALL(helper_, GenerateConnectionIdForReject(_, _))
637 .WillByDefault(testing::Return(
638 QuicUtils::CreateRandomConnectionId(connection->random_generator())));
639 ON_CALL(helper_, CanAcceptClientHello(_, _, _, _, _))
640 .WillByDefault(testing::Return(true));
641}
642
643TestQuicSpdyServerSession::~TestQuicSpdyServerSession() {
644 delete connection();
645}
646
647QuicCryptoServerStreamBase*
648TestQuicSpdyServerSession::CreateQuicCryptoServerStream(
649 const QuicCryptoServerConfig* crypto_config,
650 QuicCompressedCertsCache* compressed_certs_cache) {
wubd893df12019-05-15 18:48:20 -0700651 return new QuicCryptoServerStream(crypto_config, compressed_certs_cache, this,
652 &helper_);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500653}
654
655void TestQuicSpdyServerSession::OnCryptoHandshakeEvent(
656 CryptoHandshakeEvent event) {
657 QuicSession::OnCryptoHandshakeEvent(event);
658}
659
660QuicCryptoServerStream* TestQuicSpdyServerSession::GetMutableCryptoStream() {
661 return static_cast<QuicCryptoServerStream*>(
662 QuicServerSessionBase::GetMutableCryptoStream());
663}
664
665const QuicCryptoServerStream* TestQuicSpdyServerSession::GetCryptoStream()
666 const {
667 return static_cast<const QuicCryptoServerStream*>(
668 QuicServerSessionBase::GetCryptoStream());
669}
670
671TestQuicSpdyClientSession::TestQuicSpdyClientSession(
672 QuicConnection* connection,
673 const QuicConfig& config,
674 const ParsedQuicVersionVector& supported_versions,
675 const QuicServerId& server_id,
676 QuicCryptoClientConfig* crypto_config)
677 : QuicSpdyClientSessionBase(connection,
678 &push_promise_index_,
679 config,
680 supported_versions) {
681 crypto_stream_ = QuicMakeUnique<QuicCryptoClientStream>(
682 server_id, this, crypto_test_utils::ProofVerifyContextForTesting(),
683 crypto_config, this);
684 Initialize();
685}
686
687TestQuicSpdyClientSession::~TestQuicSpdyClientSession() {}
688
dschinazi17d42422019-06-18 16:35:07 -0700689bool TestQuicSpdyClientSession::IsAuthorized(const std::string& /*authority*/) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500690 return true;
691}
692
693void TestQuicSpdyClientSession::OnCryptoHandshakeEvent(
694 CryptoHandshakeEvent event) {
695 QuicSession::OnCryptoHandshakeEvent(event);
696}
697
698QuicCryptoClientStream* TestQuicSpdyClientSession::GetMutableCryptoStream() {
699 return crypto_stream_.get();
700}
701
702const QuicCryptoClientStream* TestQuicSpdyClientSession::GetCryptoStream()
703 const {
704 return crypto_stream_.get();
705}
706
707TestPushPromiseDelegate::TestPushPromiseDelegate(bool match)
708 : match_(match), rendezvous_fired_(false), rendezvous_stream_(nullptr) {}
709
710bool TestPushPromiseDelegate::CheckVary(
dschinazi17d42422019-06-18 16:35:07 -0700711 const spdy::SpdyHeaderBlock& /*client_request*/,
712 const spdy::SpdyHeaderBlock& /*promise_request*/,
713 const spdy::SpdyHeaderBlock& /*promise_response*/) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500714 QUIC_DVLOG(1) << "match " << match_;
715 return match_;
716}
717
718void TestPushPromiseDelegate::OnRendezvousResult(QuicSpdyStream* stream) {
719 rendezvous_fired_ = true;
720 rendezvous_stream_ = stream;
721}
722
723MockPacketWriter::MockPacketWriter() {
724 ON_CALL(*this, GetMaxPacketSize(_))
dschinazi66dea072019-04-09 11:41:06 -0700725 .WillByDefault(testing::Return(kMaxOutgoingPacketSize));
QUICHE teama6ef0a62019-03-07 20:34:33 -0500726 ON_CALL(*this, IsBatchMode()).WillByDefault(testing::Return(false));
727 ON_CALL(*this, GetNextWriteLocation(_, _))
728 .WillByDefault(testing::Return(nullptr));
729 ON_CALL(*this, Flush())
730 .WillByDefault(testing::Return(WriteResult(WRITE_STATUS_OK, 0)));
731}
732
733MockPacketWriter::~MockPacketWriter() {}
734
735MockSendAlgorithm::MockSendAlgorithm() {}
736
737MockSendAlgorithm::~MockSendAlgorithm() {}
738
739MockLossAlgorithm::MockLossAlgorithm() {}
740
741MockLossAlgorithm::~MockLossAlgorithm() {}
742
743MockAckListener::MockAckListener() {}
744
745MockAckListener::~MockAckListener() {}
746
747MockNetworkChangeVisitor::MockNetworkChangeVisitor() {}
748
749MockNetworkChangeVisitor::~MockNetworkChangeVisitor() {}
750
751namespace {
752
vasilvvc48c8712019-03-11 13:38:16 -0700753std::string HexDumpWithMarks(const char* data,
754 int length,
755 const bool* marks,
756 int mark_length) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500757 static const char kHexChars[] = "0123456789abcdef";
758 static const int kColumns = 4;
759
760 const int kSizeLimit = 1024;
761 if (length > kSizeLimit || mark_length > kSizeLimit) {
762 QUIC_LOG(ERROR) << "Only dumping first " << kSizeLimit << " bytes.";
763 length = std::min(length, kSizeLimit);
764 mark_length = std::min(mark_length, kSizeLimit);
765 }
766
vasilvvc48c8712019-03-11 13:38:16 -0700767 std::string hex;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500768 for (const char* row = data; length > 0;
769 row += kColumns, length -= kColumns) {
770 for (const char* p = row; p < row + 4; ++p) {
771 if (p < row + length) {
772 const bool mark =
773 (marks && (p - data) < mark_length && marks[p - data]);
774 hex += mark ? '*' : ' ';
775 hex += kHexChars[(*p & 0xf0) >> 4];
776 hex += kHexChars[*p & 0x0f];
777 hex += mark ? '*' : ' ';
778 } else {
779 hex += " ";
780 }
781 }
782 hex = hex + " ";
783
784 for (const char* p = row; p < row + 4 && p < row + length; ++p) {
785 hex += (*p >= 0x20 && *p <= 0x7f) ? (*p) : '.';
786 }
787
788 hex = hex + '\n';
789 }
790 return hex;
791}
792
793} // namespace
794
795QuicIpAddress TestPeerIPAddress() {
796 return QuicIpAddress::Loopback4();
797}
798
799ParsedQuicVersion QuicVersionMax() {
800 return AllSupportedVersions().front();
801}
802
803ParsedQuicVersion QuicVersionMin() {
804 return AllSupportedVersions().back();
805}
806
807QuicTransportVersion QuicTransportVersionMax() {
808 return AllSupportedTransportVersions().front();
809}
810
811QuicTransportVersion QuicTransportVersionMin() {
812 return AllSupportedTransportVersions().back();
813}
814
815QuicEncryptedPacket* ConstructEncryptedPacket(
816 QuicConnectionId destination_connection_id,
817 QuicConnectionId source_connection_id,
818 bool version_flag,
819 bool reset_flag,
820 uint64_t packet_number,
vasilvvc48c8712019-03-11 13:38:16 -0700821 const std::string& data) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500822 return ConstructEncryptedPacket(
823 destination_connection_id, source_connection_id, version_flag, reset_flag,
824 packet_number, data, CONNECTION_ID_PRESENT, CONNECTION_ID_ABSENT,
825 PACKET_4BYTE_PACKET_NUMBER);
826}
827
828QuicEncryptedPacket* ConstructEncryptedPacket(
829 QuicConnectionId destination_connection_id,
830 QuicConnectionId source_connection_id,
831 bool version_flag,
832 bool reset_flag,
833 uint64_t packet_number,
vasilvvc48c8712019-03-11 13:38:16 -0700834 const std::string& data,
QUICHE teama6ef0a62019-03-07 20:34:33 -0500835 QuicConnectionIdIncluded destination_connection_id_included,
836 QuicConnectionIdIncluded source_connection_id_included,
837 QuicPacketNumberLength packet_number_length) {
838 return ConstructEncryptedPacket(
839 destination_connection_id, source_connection_id, version_flag, reset_flag,
840 packet_number, data, destination_connection_id_included,
841 source_connection_id_included, packet_number_length, nullptr);
842}
843
844QuicEncryptedPacket* ConstructEncryptedPacket(
845 QuicConnectionId destination_connection_id,
846 QuicConnectionId source_connection_id,
847 bool version_flag,
848 bool reset_flag,
849 uint64_t packet_number,
vasilvvc48c8712019-03-11 13:38:16 -0700850 const std::string& data,
QUICHE teama6ef0a62019-03-07 20:34:33 -0500851 QuicConnectionIdIncluded destination_connection_id_included,
852 QuicConnectionIdIncluded source_connection_id_included,
853 QuicPacketNumberLength packet_number_length,
854 ParsedQuicVersionVector* versions) {
855 return ConstructEncryptedPacket(
856 destination_connection_id, source_connection_id, version_flag, reset_flag,
857 packet_number, data, destination_connection_id_included,
858 source_connection_id_included, packet_number_length, versions,
859 Perspective::IS_CLIENT);
860}
861QuicEncryptedPacket* ConstructEncryptedPacket(
862 QuicConnectionId destination_connection_id,
863 QuicConnectionId source_connection_id,
864 bool version_flag,
865 bool reset_flag,
866 uint64_t packet_number,
vasilvvc48c8712019-03-11 13:38:16 -0700867 const std::string& data,
QUICHE teama6ef0a62019-03-07 20:34:33 -0500868 QuicConnectionIdIncluded destination_connection_id_included,
869 QuicConnectionIdIncluded source_connection_id_included,
870 QuicPacketNumberLength packet_number_length,
871 ParsedQuicVersionVector* versions,
872 Perspective perspective) {
873 QuicPacketHeader header;
874 header.destination_connection_id = destination_connection_id;
875 header.destination_connection_id_included =
876 destination_connection_id_included;
877 header.source_connection_id = source_connection_id;
878 header.source_connection_id_included = source_connection_id_included;
879 header.version_flag = version_flag;
880 header.reset_flag = reset_flag;
881 header.packet_number_length = packet_number_length;
882 header.packet_number = QuicPacketNumber(packet_number);
883 ParsedQuicVersionVector supported_versions = CurrentSupportedVersions();
884 if (!versions) {
885 versions = &supported_versions;
886 }
887 if (QuicVersionHasLongHeaderLengths((*versions)[0].transport_version) &&
888 version_flag) {
889 header.retry_token_length_length = VARIABLE_LENGTH_INTEGER_LENGTH_1;
890 header.length_length = VARIABLE_LENGTH_INTEGER_LENGTH_2;
891 }
892
893 QuicFrames frames;
894 QuicFramer framer(*versions, QuicTime::Zero(), perspective,
895 kQuicDefaultConnectionIdLength);
nharper55fa6132019-05-07 19:37:21 -0700896 ParsedQuicVersion version = (*versions)[0];
897 EncryptionLevel level =
898 header.version_flag ? ENCRYPTION_INITIAL : ENCRYPTION_FORWARD_SECURE;
899 if (version.handshake_protocol == PROTOCOL_TLS1_3 &&
900 level == ENCRYPTION_INITIAL) {
901 CrypterPair crypters;
902 CryptoUtils::CreateTlsInitialCrypters(Perspective::IS_CLIENT,
903 version.transport_version,
904 destination_connection_id, &crypters);
905 framer.SetEncrypter(ENCRYPTION_INITIAL, std::move(crypters.encrypter));
906 if (version.KnowsWhichDecrypterToUse()) {
907 framer.InstallDecrypter(ENCRYPTION_INITIAL,
908 std::move(crypters.decrypter));
909 } else {
910 framer.SetDecrypter(ENCRYPTION_INITIAL, std::move(crypters.decrypter));
911 }
912 }
913 if (!QuicVersionUsesCryptoFrames(version.transport_version)) {
914 QuicFrame frame(
915 QuicStreamFrame(QuicUtils::GetCryptoStreamId(version.transport_version),
916 false, 0, QuicStringPiece(data)));
QUICHE teama6ef0a62019-03-07 20:34:33 -0500917 frames.push_back(frame);
918 } else {
nharper55fa6132019-05-07 19:37:21 -0700919 QuicFrame frame(new QuicCryptoFrame(level, 0, data));
QUICHE teama6ef0a62019-03-07 20:34:33 -0500920 frames.push_back(frame);
921 }
QUICHE team2252b702019-05-14 23:55:14 -0400922 // We need a minimum number of bytes of encrypted payload. This will
923 // guarantee that we have at least that much. (It ignores the overhead of the
924 // stream/crypto framing, so it overpads slightly.)
925 size_t min_plaintext_size =
926 QuicPacketCreator::MinPlaintextPacketSize(version);
927 if (data.length() < min_plaintext_size) {
928 size_t padding_length = min_plaintext_size - data.length();
nharper55fa6132019-05-07 19:37:21 -0700929 frames.push_back(QuicFrame(QuicPaddingFrame(padding_length)));
930 }
QUICHE teama6ef0a62019-03-07 20:34:33 -0500931
932 std::unique_ptr<QuicPacket> packet(
933 BuildUnsizedDataPacket(&framer, header, frames));
934 EXPECT_TRUE(packet != nullptr);
dschinazi66dea072019-04-09 11:41:06 -0700935 char* buffer = new char[kMaxOutgoingPacketSize];
QUICHE teama6ef0a62019-03-07 20:34:33 -0500936 size_t encrypted_length =
QUICHE team6987b4a2019-03-15 16:23:04 -0700937 framer.EncryptPayload(ENCRYPTION_INITIAL, QuicPacketNumber(packet_number),
dschinazi66dea072019-04-09 11:41:06 -0700938 *packet, buffer, kMaxOutgoingPacketSize);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500939 EXPECT_NE(0u, encrypted_length);
940 DeleteFrames(&frames);
941 return new QuicEncryptedPacket(buffer, encrypted_length, true);
942}
943
944QuicReceivedPacket* ConstructReceivedPacket(
945 const QuicEncryptedPacket& encrypted_packet,
946 QuicTime receipt_time) {
947 char* buffer = new char[encrypted_packet.length()];
948 memcpy(buffer, encrypted_packet.data(), encrypted_packet.length());
949 return new QuicReceivedPacket(buffer, encrypted_packet.length(), receipt_time,
950 true);
951}
952
953QuicEncryptedPacket* ConstructMisFramedEncryptedPacket(
954 QuicConnectionId destination_connection_id,
955 QuicConnectionId source_connection_id,
956 bool version_flag,
957 bool reset_flag,
958 uint64_t packet_number,
vasilvvc48c8712019-03-11 13:38:16 -0700959 const std::string& data,
QUICHE teama6ef0a62019-03-07 20:34:33 -0500960 QuicConnectionIdIncluded destination_connection_id_included,
961 QuicConnectionIdIncluded source_connection_id_included,
962 QuicPacketNumberLength packet_number_length,
963 ParsedQuicVersionVector* versions,
964 Perspective perspective) {
965 QuicPacketHeader header;
966 header.destination_connection_id = destination_connection_id;
967 header.destination_connection_id_included =
968 destination_connection_id_included;
969 header.source_connection_id = source_connection_id;
970 header.source_connection_id_included = source_connection_id_included;
971 header.version_flag = version_flag;
972 header.reset_flag = reset_flag;
973 header.packet_number_length = packet_number_length;
974 header.packet_number = QuicPacketNumber(packet_number);
zhongyi546cc452019-04-12 15:27:49 -0700975 if (QuicVersionHasLongHeaderLengths((*versions)[0].transport_version) &&
976 version_flag) {
977 header.retry_token_length_length = VARIABLE_LENGTH_INTEGER_LENGTH_1;
978 header.length_length = VARIABLE_LENGTH_INTEGER_LENGTH_2;
979 }
QUICHE teama6ef0a62019-03-07 20:34:33 -0500980 QuicFrame frame(QuicStreamFrame(1, false, 0, QuicStringPiece(data)));
981 QuicFrames frames;
982 frames.push_back(frame);
nharper55fa6132019-05-07 19:37:21 -0700983 ParsedQuicVersion version =
984 (versions != nullptr ? *versions : AllSupportedVersions())[0];
QUICHE teama6ef0a62019-03-07 20:34:33 -0500985 QuicFramer framer(versions != nullptr ? *versions : AllSupportedVersions(),
986 QuicTime::Zero(), perspective,
987 kQuicDefaultConnectionIdLength);
nharper55fa6132019-05-07 19:37:21 -0700988 if (version.handshake_protocol == PROTOCOL_TLS1_3 && version_flag) {
989 CrypterPair crypters;
990 CryptoUtils::CreateTlsInitialCrypters(Perspective::IS_CLIENT,
991 version.transport_version,
992 destination_connection_id, &crypters);
993 framer.SetEncrypter(ENCRYPTION_INITIAL, std::move(crypters.encrypter));
nharperf5e68452019-05-29 17:24:18 -0700994 framer.InstallDecrypter(ENCRYPTION_INITIAL, std::move(crypters.decrypter));
nharper55fa6132019-05-07 19:37:21 -0700995 }
996 // We need a minimum of 7 bytes of encrypted payload. This will guarantee that
997 // we have at least that much. (It ignores the overhead of the stream/crypto
998 // framing, so it overpads slightly.)
999 if (data.length() < 7) {
1000 size_t padding_length = 7 - data.length();
1001 frames.push_back(QuicFrame(QuicPaddingFrame(padding_length)));
1002 }
QUICHE teama6ef0a62019-03-07 20:34:33 -05001003
1004 std::unique_ptr<QuicPacket> packet(
1005 BuildUnsizedDataPacket(&framer, header, frames));
1006 EXPECT_TRUE(packet != nullptr);
1007
1008 // Now set the frame type to 0x1F, which is an invalid frame type.
1009 reinterpret_cast<unsigned char*>(
1010 packet->mutable_data())[GetStartOfEncryptedData(
1011 framer.transport_version(),
1012 GetIncludedDestinationConnectionIdLength(header),
1013 GetIncludedSourceConnectionIdLength(header), version_flag,
1014 false /* no diversification nonce */, packet_number_length,
zhongyi546cc452019-04-12 15:27:49 -07001015 header.retry_token_length_length, 0, header.length_length)] = 0x1F;
QUICHE teama6ef0a62019-03-07 20:34:33 -05001016
dschinazi66dea072019-04-09 11:41:06 -07001017 char* buffer = new char[kMaxOutgoingPacketSize];
QUICHE teama6ef0a62019-03-07 20:34:33 -05001018 size_t encrypted_length =
QUICHE team6987b4a2019-03-15 16:23:04 -07001019 framer.EncryptPayload(ENCRYPTION_INITIAL, QuicPacketNumber(packet_number),
dschinazi66dea072019-04-09 11:41:06 -07001020 *packet, buffer, kMaxOutgoingPacketSize);
QUICHE teama6ef0a62019-03-07 20:34:33 -05001021 EXPECT_NE(0u, encrypted_length);
1022 return new QuicEncryptedPacket(buffer, encrypted_length, true);
1023}
1024
vasilvvc48c8712019-03-11 13:38:16 -07001025void CompareCharArraysWithHexError(const std::string& description,
QUICHE teama6ef0a62019-03-07 20:34:33 -05001026 const char* actual,
1027 const int actual_len,
1028 const char* expected,
1029 const int expected_len) {
1030 EXPECT_EQ(actual_len, expected_len);
1031 const int min_len = std::min(actual_len, expected_len);
1032 const int max_len = std::max(actual_len, expected_len);
1033 std::unique_ptr<bool[]> marks(new bool[max_len]);
1034 bool identical = (actual_len == expected_len);
1035 for (int i = 0; i < min_len; ++i) {
1036 if (actual[i] != expected[i]) {
1037 marks[i] = true;
1038 identical = false;
1039 } else {
1040 marks[i] = false;
1041 }
1042 }
1043 for (int i = min_len; i < max_len; ++i) {
1044 marks[i] = true;
1045 }
1046 if (identical)
1047 return;
1048 ADD_FAILURE() << "Description:\n"
1049 << description << "\n\nExpected:\n"
1050 << HexDumpWithMarks(expected, expected_len, marks.get(),
1051 max_len)
1052 << "\nActual:\n"
1053 << HexDumpWithMarks(actual, actual_len, marks.get(), max_len);
1054}
1055
QUICHE teama6ef0a62019-03-07 20:34:33 -05001056QuicConfig DefaultQuicConfig() {
1057 QuicConfig config;
1058 config.SetInitialStreamFlowControlWindowToSend(
1059 kInitialStreamFlowControlWindowForTest);
1060 config.SetInitialSessionFlowControlWindowToSend(
1061 kInitialSessionFlowControlWindowForTest);
fkastenholzd3a1de92019-05-15 07:00:07 -07001062 QuicConfigPeer::SetReceivedMaxIncomingBidirectionalStreams(
QUICHE teama6ef0a62019-03-07 20:34:33 -05001063 &config, kDefaultMaxStreamsPerConnection);
1064 // Default enable NSTP.
1065 // This is unnecessary for versions > 44
1066 if (!config.HasClientSentConnectionOption(quic::kNSTP,
1067 quic::Perspective::IS_CLIENT)) {
1068 quic::QuicTagVector connection_options;
1069 connection_options.push_back(quic::kNSTP);
1070 config.SetConnectionOptionsToSend(connection_options);
1071 }
1072 return config;
1073}
1074
QUICHE teama6ef0a62019-03-07 20:34:33 -05001075QuicTransportVersionVector SupportedTransportVersions(
1076 QuicTransportVersion version) {
1077 QuicTransportVersionVector versions;
1078 versions.push_back(version);
1079 return versions;
1080}
1081
1082ParsedQuicVersionVector SupportedVersions(ParsedQuicVersion version) {
1083 ParsedQuicVersionVector versions;
1084 versions.push_back(version);
1085 return versions;
1086}
1087
1088MockQuicConnectionDebugVisitor::MockQuicConnectionDebugVisitor() {}
1089
1090MockQuicConnectionDebugVisitor::~MockQuicConnectionDebugVisitor() {}
1091
1092MockReceivedPacketManager::MockReceivedPacketManager(QuicConnectionStats* stats)
1093 : QuicReceivedPacketManager(stats) {}
1094
1095MockReceivedPacketManager::~MockReceivedPacketManager() {}
1096
QUICHE teama6ef0a62019-03-07 20:34:33 -05001097MockPacketCreatorDelegate::MockPacketCreatorDelegate() {}
1098MockPacketCreatorDelegate::~MockPacketCreatorDelegate() {}
1099
1100MockSessionNotifier::MockSessionNotifier() {}
1101MockSessionNotifier::~MockSessionNotifier() {}
1102
1103void CreateClientSessionForTest(
1104 QuicServerId server_id,
QUICHE teama6ef0a62019-03-07 20:34:33 -05001105 QuicTime::Delta connection_start_time,
1106 const ParsedQuicVersionVector& supported_versions,
1107 MockQuicConnectionHelper* helper,
1108 MockAlarmFactory* alarm_factory,
1109 QuicCryptoClientConfig* crypto_client_config,
1110 PacketSavingConnection** client_connection,
1111 TestQuicSpdyClientSession** client_session) {
1112 CHECK(crypto_client_config);
1113 CHECK(client_connection);
1114 CHECK(client_session);
1115 CHECK(!connection_start_time.IsZero())
1116 << "Connections must start at non-zero times, otherwise the "
1117 << "strike-register will be unhappy.";
1118
wube9dc7da2019-05-22 06:08:54 -07001119 QuicConfig config = DefaultQuicConfig();
QUICHE teama6ef0a62019-03-07 20:34:33 -05001120 *client_connection = new PacketSavingConnection(
1121 helper, alarm_factory, Perspective::IS_CLIENT, supported_versions);
1122 *client_session = new TestQuicSpdyClientSession(*client_connection, config,
1123 supported_versions, server_id,
1124 crypto_client_config);
1125 (*client_connection)->AdvanceTime(connection_start_time);
1126}
1127
1128void CreateServerSessionForTest(
dschinazi17d42422019-06-18 16:35:07 -07001129 QuicServerId /*server_id*/,
QUICHE teama6ef0a62019-03-07 20:34:33 -05001130 QuicTime::Delta connection_start_time,
1131 ParsedQuicVersionVector supported_versions,
1132 MockQuicConnectionHelper* helper,
1133 MockAlarmFactory* alarm_factory,
1134 QuicCryptoServerConfig* server_crypto_config,
1135 QuicCompressedCertsCache* compressed_certs_cache,
1136 PacketSavingConnection** server_connection,
1137 TestQuicSpdyServerSession** server_session) {
1138 CHECK(server_crypto_config);
1139 CHECK(server_connection);
1140 CHECK(server_session);
1141 CHECK(!connection_start_time.IsZero())
1142 << "Connections must start at non-zero times, otherwise the "
1143 << "strike-register will be unhappy.";
1144
1145 *server_connection =
1146 new PacketSavingConnection(helper, alarm_factory, Perspective::IS_SERVER,
1147 ParsedVersionOfIndex(supported_versions, 0));
1148 *server_session = new TestQuicSpdyServerSession(
1149 *server_connection, DefaultQuicConfig(), supported_versions,
1150 server_crypto_config, compressed_certs_cache);
1151
1152 // We advance the clock initially because the default time is zero and the
1153 // strike register worries that we've just overflowed a uint32_t time.
1154 (*server_connection)->AdvanceTime(connection_start_time);
1155}
1156
1157QuicStreamId GetNthClientInitiatedBidirectionalStreamId(
1158 QuicTransportVersion version,
1159 int n) {
dschinazi552accc2019-06-17 17:07:34 -07001160 int num = n;
1161 if (!VersionLacksHeadersStream(version)) {
1162 num++; // + 1 because spdy_session contains headers stream.
1163 }
QUICHE teama6ef0a62019-03-07 20:34:33 -05001164 return QuicUtils::GetFirstBidirectionalStreamId(version,
1165 Perspective::IS_CLIENT) +
dschinazi552accc2019-06-17 17:07:34 -07001166 QuicUtils::StreamIdDelta(version) * num;
QUICHE teama6ef0a62019-03-07 20:34:33 -05001167}
1168
1169QuicStreamId GetNthServerInitiatedBidirectionalStreamId(
1170 QuicTransportVersion version,
1171 int n) {
1172 return QuicUtils::GetFirstBidirectionalStreamId(version,
1173 Perspective::IS_SERVER) +
1174 QuicUtils::StreamIdDelta(version) * n;
1175}
1176
1177QuicStreamId GetNthServerInitiatedUnidirectionalStreamId(
1178 QuicTransportVersion version,
1179 int n) {
1180 return QuicUtils::GetFirstUnidirectionalStreamId(version,
1181 Perspective::IS_SERVER) +
1182 QuicUtils::StreamIdDelta(version) * n;
1183}
1184
renjietang3a1bb802019-06-11 10:42:41 -07001185QuicStreamId GetNthClientInitiatedUnidirectionalStreamId(
1186 QuicTransportVersion version,
1187 int n) {
1188 return QuicUtils::GetFirstUnidirectionalStreamId(version,
1189 Perspective::IS_CLIENT) +
1190 QuicUtils::StreamIdDelta(version) * n;
1191}
1192
QUICHE teama6ef0a62019-03-07 20:34:33 -05001193StreamType DetermineStreamType(QuicStreamId id,
1194 QuicTransportVersion version,
1195 Perspective perspective,
1196 bool is_incoming,
1197 StreamType default_type) {
fkastenholz305e1732019-06-18 05:01:22 -07001198 return VersionHasIetfQuicFrames(version)
QUICHE teama6ef0a62019-03-07 20:34:33 -05001199 ? QuicUtils::GetStreamType(id, perspective, is_incoming)
1200 : default_type;
1201}
1202
1203QuicMemSliceSpan MakeSpan(QuicBufferAllocator* allocator,
1204 QuicStringPiece message_data,
1205 QuicMemSliceStorage* storage) {
1206 if (message_data.length() == 0) {
dschinazi66dea072019-04-09 11:41:06 -07001207 *storage =
1208 QuicMemSliceStorage(nullptr, 0, allocator, kMaxOutgoingPacketSize);
QUICHE teama6ef0a62019-03-07 20:34:33 -05001209 return storage->ToSpan();
1210 }
1211 struct iovec iov = {const_cast<char*>(message_data.data()),
1212 message_data.length()};
dschinazi66dea072019-04-09 11:41:06 -07001213 *storage = QuicMemSliceStorage(&iov, 1, allocator, kMaxOutgoingPacketSize);
QUICHE teama6ef0a62019-03-07 20:34:33 -05001214 return storage->ToSpan();
1215}
1216
1217} // namespace test
1218} // namespace quic