blob: 41fb995e5e37f185bdcd218d89b5fc997e676e6f [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>
bnc463f2352019-10-10 04:49:34 -070010#include <utility>
QUICHE teama6ef0a62019-03-07 20:34:33 -050011
vasilvvc2018482019-04-26 15:47:55 -070012#include "third_party/boringssl/src/include/openssl/chacha.h"
QUICHE teama6ef0a62019-03-07 20:34:33 -050013#include "third_party/boringssl/src/include/openssl/sha.h"
14#include "net/third_party/quiche/src/quic/core/crypto/crypto_framer.h"
15#include "net/third_party/quiche/src/quic/core/crypto/crypto_handshake.h"
16#include "net/third_party/quiche/src/quic/core/crypto/crypto_utils.h"
17#include "net/third_party/quiche/src/quic/core/crypto/null_encrypter.h"
18#include "net/third_party/quiche/src/quic/core/crypto/quic_decrypter.h"
19#include "net/third_party/quiche/src/quic/core/crypto/quic_encrypter.h"
dschinazi18f10672020-04-21 16:29:34 -070020#include "net/third_party/quiche/src/quic/core/http/quic_spdy_client_session.h"
vasilvv2b0ab242020-01-07 07:32:09 -080021#include "net/third_party/quiche/src/quic/core/quic_buffer_allocator.h"
dschinazi18f10672020-04-21 16:29:34 -070022#include "net/third_party/quiche/src/quic/core/quic_config.h"
QUICHE teama6ef0a62019-03-07 20:34:33 -050023#include "net/third_party/quiche/src/quic/core/quic_data_writer.h"
24#include "net/third_party/quiche/src/quic/core/quic_framer.h"
25#include "net/third_party/quiche/src/quic/core/quic_packet_creator.h"
vasilvv2b0ab242020-01-07 07:32:09 -080026#include "net/third_party/quiche/src/quic/core/quic_simple_buffer_allocator.h"
renjietang7c239172020-02-21 13:50:39 -080027#include "net/third_party/quiche/src/quic/core/quic_types.h"
QUICHE teama6ef0a62019-03-07 20:34:33 -050028#include "net/third_party/quiche/src/quic/core/quic_utils.h"
dschinazi18f10672020-04-21 16:29:34 -070029#include "net/third_party/quiche/src/quic/core/quic_versions.h"
QUICHE teama6ef0a62019-03-07 20:34:33 -050030#include "net/third_party/quiche/src/quic/platform/api/quic_flags.h"
31#include "net/third_party/quiche/src/quic/platform/api/quic_logging.h"
QUICHE teama6ef0a62019-03-07 20:34:33 -050032#include "net/third_party/quiche/src/quic/test_tools/crypto_test_utils.h"
33#include "net/third_party/quiche/src/quic/test_tools/quic_config_peer.h"
34#include "net/third_party/quiche/src/quic/test_tools/quic_connection_peer.h"
bnc4e9283d2019-12-17 07:08:57 -080035#include "net/third_party/quiche/src/common/platform/api/quiche_arraysize.h"
QUICHE team173c48f2019-11-19 16:34:44 -080036#include "net/third_party/quiche/src/common/platform/api/quiche_endian.h"
QUICHE team6dcf6ab2019-12-11 10:10:51 -080037#include "net/third_party/quiche/src/common/platform/api/quiche_string_piece.h"
QUICHE teama6ef0a62019-03-07 20:34:33 -050038#include "net/third_party/quiche/src/spdy/core/spdy_frame_builder.h"
39
40using testing::_;
41using testing::Invoke;
42
43namespace quic {
44namespace test {
45
46QuicConnectionId TestConnectionId() {
47 // Chosen by fair dice roll.
48 // Guaranteed to be random.
49 return TestConnectionId(42);
50}
51
52QuicConnectionId TestConnectionId(uint64_t connection_number) {
53 const uint64_t connection_id64_net =
QUICHE team173c48f2019-11-19 16:34:44 -080054 quiche::QuicheEndian::HostToNet64(connection_number);
QUICHE teama6ef0a62019-03-07 20:34:33 -050055 return QuicConnectionId(reinterpret_cast<const char*>(&connection_id64_net),
56 sizeof(connection_id64_net));
57}
58
QUICHE team8e2e4532019-03-14 14:37:56 -070059QuicConnectionId TestConnectionIdNineBytesLong(uint64_t connection_number) {
60 const uint64_t connection_number_net =
QUICHE team173c48f2019-11-19 16:34:44 -080061 quiche::QuicheEndian::HostToNet64(connection_number);
QUICHE team8e2e4532019-03-14 14:37:56 -070062 char connection_id_bytes[9] = {};
63 static_assert(
64 sizeof(connection_id_bytes) == 1 + sizeof(connection_number_net),
65 "bad lengths");
66 memcpy(connection_id_bytes + 1, &connection_number_net,
67 sizeof(connection_number_net));
68 return QuicConnectionId(connection_id_bytes, sizeof(connection_id_bytes));
69}
70
QUICHE teama6ef0a62019-03-07 20:34:33 -050071uint64_t TestConnectionIdToUInt64(QuicConnectionId connection_id) {
72 DCHECK_EQ(connection_id.length(), kQuicDefaultConnectionIdLength);
73 uint64_t connection_id64_net = 0;
74 memcpy(&connection_id64_net, connection_id.data(),
75 std::min<size_t>(static_cast<size_t>(connection_id.length()),
76 sizeof(connection_id64_net)));
QUICHE team173c48f2019-11-19 16:34:44 -080077 return quiche::QuicheEndian::NetToHost64(connection_id64_net);
QUICHE teama6ef0a62019-03-07 20:34:33 -050078}
79
dschinazi18f10672020-04-21 16:29:34 -070080std::string TestHostname() {
81 return "test.example.org";
82}
83
84QuicServerId TestServerId() {
85 return QuicServerId(TestHostname(), kTestPort);
86}
87
QUICHE teama6ef0a62019-03-07 20:34:33 -050088QuicAckFrame InitAckFrame(const std::vector<QuicAckBlock>& ack_blocks) {
89 DCHECK_GT(ack_blocks.size(), 0u);
90
91 QuicAckFrame ack;
92 QuicPacketNumber end_of_previous_block(1);
93 for (const QuicAckBlock& block : ack_blocks) {
94 DCHECK_GE(block.start, end_of_previous_block);
95 DCHECK_GT(block.limit, block.start);
96 ack.packets.AddRange(block.start, block.limit);
97 end_of_previous_block = block.limit;
98 }
99
100 ack.largest_acked = ack.packets.Max();
101
102 return ack;
103}
104
105QuicAckFrame InitAckFrame(uint64_t largest_acked) {
106 return InitAckFrame(QuicPacketNumber(largest_acked));
107}
108
109QuicAckFrame InitAckFrame(QuicPacketNumber largest_acked) {
110 return InitAckFrame({{QuicPacketNumber(1), largest_acked + 1}});
111}
112
113QuicAckFrame MakeAckFrameWithAckBlocks(size_t num_ack_blocks,
114 uint64_t least_unacked) {
115 QuicAckFrame ack;
116 ack.largest_acked = QuicPacketNumber(2 * num_ack_blocks + least_unacked);
117 // Add enough received packets to get num_ack_blocks ack blocks.
118 for (QuicPacketNumber i = QuicPacketNumber(2);
119 i < QuicPacketNumber(2 * num_ack_blocks + 1); i += 2) {
120 ack.packets.Add(i + least_unacked);
121 }
122 return ack;
123}
124
fayang91c23d72020-03-09 12:35:05 -0700125QuicAckFrame MakeAckFrameWithGaps(uint64_t gap_size,
126 size_t max_num_gaps,
127 uint64_t largest_acked) {
128 QuicAckFrame ack;
129 ack.largest_acked = QuicPacketNumber(largest_acked);
130 ack.packets.Add(QuicPacketNumber(largest_acked));
131 for (size_t i = 0; i < max_num_gaps; ++i) {
132 if (largest_acked <= gap_size) {
133 break;
134 }
135 largest_acked -= gap_size;
136 ack.packets.Add(QuicPacketNumber(largest_acked));
137 }
138 return ack;
139}
140
nharper965e5922019-09-23 22:33:54 -0700141EncryptionLevel HeaderToEncryptionLevel(const QuicPacketHeader& header) {
142 if (header.form == IETF_QUIC_SHORT_HEADER_PACKET) {
143 return ENCRYPTION_FORWARD_SECURE;
144 } else if (header.form == IETF_QUIC_LONG_HEADER_PACKET) {
145 if (header.long_packet_type == HANDSHAKE) {
146 return ENCRYPTION_HANDSHAKE;
147 } else if (header.long_packet_type == ZERO_RTT_PROTECTED) {
148 return ENCRYPTION_ZERO_RTT;
149 }
150 }
151 return ENCRYPTION_INITIAL;
152}
153
QUICHE teama6ef0a62019-03-07 20:34:33 -0500154std::unique_ptr<QuicPacket> BuildUnsizedDataPacket(
155 QuicFramer* framer,
156 const QuicPacketHeader& header,
157 const QuicFrames& frames) {
dschinazi66dea072019-04-09 11:41:06 -0700158 const size_t max_plaintext_size =
159 framer->GetMaxPlaintextSize(kMaxOutgoingPacketSize);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500160 size_t packet_size = GetPacketHeaderSize(framer->transport_version(), header);
161 for (size_t i = 0; i < frames.size(); ++i) {
162 DCHECK_LE(packet_size, max_plaintext_size);
163 bool first_frame = i == 0;
164 bool last_frame = i == frames.size() - 1;
165 const size_t frame_size = framer->GetSerializedFrameLength(
166 frames[i], max_plaintext_size - packet_size, first_frame, last_frame,
167 header.packet_number_length);
168 DCHECK(frame_size);
169 packet_size += frame_size;
170 }
171 return BuildUnsizedDataPacket(framer, header, frames, packet_size);
172}
173
174std::unique_ptr<QuicPacket> BuildUnsizedDataPacket(
175 QuicFramer* framer,
176 const QuicPacketHeader& header,
177 const QuicFrames& frames,
178 size_t packet_size) {
179 char* buffer = new char[packet_size];
nharper965e5922019-09-23 22:33:54 -0700180 EncryptionLevel level = HeaderToEncryptionLevel(header);
nharperc6b99512019-09-19 11:13:48 -0700181 size_t length =
182 framer->BuildDataPacket(header, frames, buffer, packet_size, level);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500183 DCHECK_NE(0u, length);
184 // Re-construct the data packet with data ownership.
vasilvv0fc587f2019-09-06 13:33:08 -0700185 return std::make_unique<QuicPacket>(
QUICHE teama6ef0a62019-03-07 20:34:33 -0500186 buffer, length, /* owns_buffer */ true,
187 GetIncludedDestinationConnectionIdLength(header),
188 GetIncludedSourceConnectionIdLength(header), header.version_flag,
189 header.nonce != nullptr, header.packet_number_length,
190 header.retry_token_length_length, header.retry_token.length(),
191 header.length_length);
192}
193
QUICHE team6dcf6ab2019-12-11 10:10:51 -0800194std::string Sha1Hash(quiche::QuicheStringPiece data) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500195 char buffer[SHA_DIGEST_LENGTH];
196 SHA1(reinterpret_cast<const uint8_t*>(data.data()), data.size(),
197 reinterpret_cast<uint8_t*>(buffer));
bnc4e9283d2019-12-17 07:08:57 -0800198 return std::string(buffer, QUICHE_ARRAYSIZE(buffer));
QUICHE teama6ef0a62019-03-07 20:34:33 -0500199}
200
bnc5b3c3be2019-06-25 10:37:09 -0700201bool ClearControlFrame(const QuicFrame& frame) {
202 DeleteFrame(&const_cast<QuicFrame&>(frame));
203 return true;
204}
205
QUICHE teama6ef0a62019-03-07 20:34:33 -0500206uint64_t SimpleRandom::RandUint64() {
vasilvvc2018482019-04-26 15:47:55 -0700207 uint64_t result;
208 RandBytes(&result, sizeof(result));
209 return result;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500210}
211
212void SimpleRandom::RandBytes(void* data, size_t len) {
vasilvvc2018482019-04-26 15:47:55 -0700213 uint8_t* data_bytes = reinterpret_cast<uint8_t*>(data);
214 while (len > 0) {
215 const size_t buffer_left = sizeof(buffer_) - buffer_offset_;
216 const size_t to_copy = std::min(buffer_left, len);
217 memcpy(data_bytes, buffer_ + buffer_offset_, to_copy);
218 data_bytes += to_copy;
219 buffer_offset_ += to_copy;
220 len -= to_copy;
221
222 if (buffer_offset_ == sizeof(buffer_)) {
223 FillBuffer();
224 }
QUICHE teama6ef0a62019-03-07 20:34:33 -0500225 }
226}
227
vasilvvc2018482019-04-26 15:47:55 -0700228void SimpleRandom::FillBuffer() {
229 uint8_t nonce[12];
230 memcpy(nonce, buffer_, sizeof(nonce));
231 CRYPTO_chacha_20(buffer_, buffer_, sizeof(buffer_), key_, nonce, 0);
232 buffer_offset_ = 0;
233}
234
235void SimpleRandom::set_seed(uint64_t seed) {
236 static_assert(sizeof(key_) == SHA256_DIGEST_LENGTH, "Key has to be 256 bits");
237 SHA256(reinterpret_cast<const uint8_t*>(&seed), sizeof(seed), key_);
238
239 memset(buffer_, 0, sizeof(buffer_));
240 FillBuffer();
241}
242
QUICHE teama6ef0a62019-03-07 20:34:33 -0500243MockFramerVisitor::MockFramerVisitor() {
244 // By default, we want to accept packets.
fayang8aba1ff2019-06-21 12:00:54 -0700245 ON_CALL(*this, OnProtocolVersionMismatch(_))
QUICHE teama6ef0a62019-03-07 20:34:33 -0500246 .WillByDefault(testing::Return(false));
247
248 // By default, we want to accept packets.
249 ON_CALL(*this, OnUnauthenticatedHeader(_))
250 .WillByDefault(testing::Return(true));
251
252 ON_CALL(*this, OnUnauthenticatedPublicHeader(_))
253 .WillByDefault(testing::Return(true));
254
255 ON_CALL(*this, OnPacketHeader(_)).WillByDefault(testing::Return(true));
256
257 ON_CALL(*this, OnStreamFrame(_)).WillByDefault(testing::Return(true));
258
259 ON_CALL(*this, OnCryptoFrame(_)).WillByDefault(testing::Return(true));
260
261 ON_CALL(*this, OnStopWaitingFrame(_)).WillByDefault(testing::Return(true));
262
263 ON_CALL(*this, OnPaddingFrame(_)).WillByDefault(testing::Return(true));
264
265 ON_CALL(*this, OnPingFrame(_)).WillByDefault(testing::Return(true));
266
267 ON_CALL(*this, OnRstStreamFrame(_)).WillByDefault(testing::Return(true));
268
269 ON_CALL(*this, OnConnectionCloseFrame(_))
270 .WillByDefault(testing::Return(true));
271
QUICHE teama6ef0a62019-03-07 20:34:33 -0500272 ON_CALL(*this, OnStopSendingFrame(_)).WillByDefault(testing::Return(true));
273
274 ON_CALL(*this, OnPathChallengeFrame(_)).WillByDefault(testing::Return(true));
275
276 ON_CALL(*this, OnPathResponseFrame(_)).WillByDefault(testing::Return(true));
277
278 ON_CALL(*this, OnGoAwayFrame(_)).WillByDefault(testing::Return(true));
fkastenholz3c4eabf2019-04-22 07:49:59 -0700279 ON_CALL(*this, OnMaxStreamsFrame(_)).WillByDefault(testing::Return(true));
280 ON_CALL(*this, OnStreamsBlockedFrame(_)).WillByDefault(testing::Return(true));
QUICHE teama6ef0a62019-03-07 20:34:33 -0500281}
282
283MockFramerVisitor::~MockFramerVisitor() {}
284
fayang8aba1ff2019-06-21 12:00:54 -0700285bool NoOpFramerVisitor::OnProtocolVersionMismatch(
286 ParsedQuicVersion /*version*/) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500287 return false;
288}
289
290bool NoOpFramerVisitor::OnUnauthenticatedPublicHeader(
dschinazi17d42422019-06-18 16:35:07 -0700291 const QuicPacketHeader& /*header*/) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500292 return true;
293}
294
295bool NoOpFramerVisitor::OnUnauthenticatedHeader(
dschinazi17d42422019-06-18 16:35:07 -0700296 const QuicPacketHeader& /*header*/) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500297 return true;
298}
299
dschinazi17d42422019-06-18 16:35:07 -0700300bool NoOpFramerVisitor::OnPacketHeader(const QuicPacketHeader& /*header*/) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500301 return true;
302}
303
dschinazi17d42422019-06-18 16:35:07 -0700304void NoOpFramerVisitor::OnCoalescedPacket(
305 const QuicEncryptedPacket& /*packet*/) {}
QUICHE teama6ef0a62019-03-07 20:34:33 -0500306
dschinazi4b5a68a2019-08-15 15:45:36 -0700307void NoOpFramerVisitor::OnUndecryptablePacket(
308 const QuicEncryptedPacket& /*packet*/,
309 EncryptionLevel /*decryption_level*/,
310 bool /*has_decryption_key*/) {}
311
dschinazi17d42422019-06-18 16:35:07 -0700312bool NoOpFramerVisitor::OnStreamFrame(const QuicStreamFrame& /*frame*/) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500313 return true;
314}
315
dschinazi17d42422019-06-18 16:35:07 -0700316bool NoOpFramerVisitor::OnCryptoFrame(const QuicCryptoFrame& /*frame*/) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500317 return true;
318}
319
dschinazi17d42422019-06-18 16:35:07 -0700320bool NoOpFramerVisitor::OnAckFrameStart(QuicPacketNumber /*largest_acked*/,
321 QuicTime::Delta /*ack_delay_time*/) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500322 return true;
323}
324
dschinazi17d42422019-06-18 16:35:07 -0700325bool NoOpFramerVisitor::OnAckRange(QuicPacketNumber /*start*/,
326 QuicPacketNumber /*end*/) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500327 return true;
328}
329
dschinazi17d42422019-06-18 16:35:07 -0700330bool NoOpFramerVisitor::OnAckTimestamp(QuicPacketNumber /*packet_number*/,
331 QuicTime /*timestamp*/) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500332 return true;
333}
334
dschinazi17d42422019-06-18 16:35:07 -0700335bool NoOpFramerVisitor::OnAckFrameEnd(QuicPacketNumber /*start*/) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500336 return true;
337}
338
dschinazi17d42422019-06-18 16:35:07 -0700339bool NoOpFramerVisitor::OnStopWaitingFrame(
340 const QuicStopWaitingFrame& /*frame*/) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500341 return true;
342}
343
dschinazi17d42422019-06-18 16:35:07 -0700344bool NoOpFramerVisitor::OnPaddingFrame(const QuicPaddingFrame& /*frame*/) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500345 return true;
346}
347
dschinazi17d42422019-06-18 16:35:07 -0700348bool NoOpFramerVisitor::OnPingFrame(const QuicPingFrame& /*frame*/) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500349 return true;
350}
351
dschinazi17d42422019-06-18 16:35:07 -0700352bool NoOpFramerVisitor::OnRstStreamFrame(const QuicRstStreamFrame& /*frame*/) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500353 return true;
354}
355
356bool NoOpFramerVisitor::OnConnectionCloseFrame(
dschinazi17d42422019-06-18 16:35:07 -0700357 const QuicConnectionCloseFrame& /*frame*/) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500358 return true;
359}
360
QUICHE teama6ef0a62019-03-07 20:34:33 -0500361bool NoOpFramerVisitor::OnNewConnectionIdFrame(
dschinazi17d42422019-06-18 16:35:07 -0700362 const QuicNewConnectionIdFrame& /*frame*/) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500363 return true;
364}
365
366bool NoOpFramerVisitor::OnRetireConnectionIdFrame(
dschinazi17d42422019-06-18 16:35:07 -0700367 const QuicRetireConnectionIdFrame& /*frame*/) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500368 return true;
369}
370
dschinazi17d42422019-06-18 16:35:07 -0700371bool NoOpFramerVisitor::OnNewTokenFrame(const QuicNewTokenFrame& /*frame*/) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500372 return true;
373}
374
dschinazi17d42422019-06-18 16:35:07 -0700375bool NoOpFramerVisitor::OnStopSendingFrame(
376 const QuicStopSendingFrame& /*frame*/) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500377 return true;
378}
379
380bool NoOpFramerVisitor::OnPathChallengeFrame(
dschinazi17d42422019-06-18 16:35:07 -0700381 const QuicPathChallengeFrame& /*frame*/) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500382 return true;
383}
384
385bool NoOpFramerVisitor::OnPathResponseFrame(
dschinazi17d42422019-06-18 16:35:07 -0700386 const QuicPathResponseFrame& /*frame*/) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500387 return true;
388}
389
dschinazi17d42422019-06-18 16:35:07 -0700390bool NoOpFramerVisitor::OnGoAwayFrame(const QuicGoAwayFrame& /*frame*/) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500391 return true;
392}
393
dschinazi17d42422019-06-18 16:35:07 -0700394bool NoOpFramerVisitor::OnMaxStreamsFrame(
395 const QuicMaxStreamsFrame& /*frame*/) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500396 return true;
397}
398
fkastenholz3c4eabf2019-04-22 07:49:59 -0700399bool NoOpFramerVisitor::OnStreamsBlockedFrame(
dschinazi17d42422019-06-18 16:35:07 -0700400 const QuicStreamsBlockedFrame& /*frame*/) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500401 return true;
402}
403
404bool NoOpFramerVisitor::OnWindowUpdateFrame(
dschinazi17d42422019-06-18 16:35:07 -0700405 const QuicWindowUpdateFrame& /*frame*/) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500406 return true;
407}
408
dschinazi17d42422019-06-18 16:35:07 -0700409bool NoOpFramerVisitor::OnBlockedFrame(const QuicBlockedFrame& /*frame*/) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500410 return true;
411}
412
dschinazi17d42422019-06-18 16:35:07 -0700413bool NoOpFramerVisitor::OnMessageFrame(const QuicMessageFrame& /*frame*/) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500414 return true;
415}
416
fayang01062942020-01-22 07:23:23 -0800417bool NoOpFramerVisitor::OnHandshakeDoneFrame(
418 const QuicHandshakeDoneFrame& /*frame*/) {
419 return true;
420}
421
haoyuewang6a6a0ff2020-06-23 16:32:26 -0700422bool NoOpFramerVisitor::OnAckFrequencyFrame(
423 const QuicAckFrequencyFrame& /*frame*/) {
424 return true;
425}
426
dschinazi17d42422019-06-18 16:35:07 -0700427bool NoOpFramerVisitor::IsValidStatelessResetToken(
428 QuicUint128 /*token*/) const {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500429 return false;
430}
431
432MockQuicConnectionVisitor::MockQuicConnectionVisitor() {}
433
434MockQuicConnectionVisitor::~MockQuicConnectionVisitor() {}
435
436MockQuicConnectionHelper::MockQuicConnectionHelper() {}
437
438MockQuicConnectionHelper::~MockQuicConnectionHelper() {}
439
440const QuicClock* MockQuicConnectionHelper::GetClock() const {
441 return &clock_;
442}
443
444QuicRandom* MockQuicConnectionHelper::GetRandomGenerator() {
445 return &random_generator_;
446}
447
448QuicAlarm* MockAlarmFactory::CreateAlarm(QuicAlarm::Delegate* delegate) {
449 return new MockAlarmFactory::TestAlarm(
450 QuicArenaScopedPtr<QuicAlarm::Delegate>(delegate));
451}
452
453QuicArenaScopedPtr<QuicAlarm> MockAlarmFactory::CreateAlarm(
454 QuicArenaScopedPtr<QuicAlarm::Delegate> delegate,
455 QuicConnectionArena* arena) {
456 if (arena != nullptr) {
457 return arena->New<TestAlarm>(std::move(delegate));
458 } else {
459 return QuicArenaScopedPtr<TestAlarm>(new TestAlarm(std::move(delegate)));
460 }
461}
462
463QuicBufferAllocator* MockQuicConnectionHelper::GetStreamSendBufferAllocator() {
464 return &buffer_allocator_;
465}
466
467void MockQuicConnectionHelper::AdvanceTime(QuicTime::Delta delta) {
468 clock_.AdvanceTime(delta);
469}
470
471MockQuicConnection::MockQuicConnection(MockQuicConnectionHelper* helper,
472 MockAlarmFactory* alarm_factory,
473 Perspective perspective)
474 : MockQuicConnection(TestConnectionId(),
475 QuicSocketAddress(TestPeerIPAddress(), kTestPort),
476 helper,
477 alarm_factory,
478 perspective,
479 ParsedVersionOfIndex(CurrentSupportedVersions(), 0)) {}
480
481MockQuicConnection::MockQuicConnection(QuicSocketAddress address,
482 MockQuicConnectionHelper* helper,
483 MockAlarmFactory* alarm_factory,
484 Perspective perspective)
485 : MockQuicConnection(TestConnectionId(),
486 address,
487 helper,
488 alarm_factory,
489 perspective,
490 ParsedVersionOfIndex(CurrentSupportedVersions(), 0)) {}
491
492MockQuicConnection::MockQuicConnection(QuicConnectionId connection_id,
493 MockQuicConnectionHelper* helper,
494 MockAlarmFactory* alarm_factory,
495 Perspective perspective)
496 : MockQuicConnection(connection_id,
497 QuicSocketAddress(TestPeerIPAddress(), kTestPort),
498 helper,
499 alarm_factory,
500 perspective,
501 ParsedVersionOfIndex(CurrentSupportedVersions(), 0)) {}
502
503MockQuicConnection::MockQuicConnection(
504 MockQuicConnectionHelper* helper,
505 MockAlarmFactory* alarm_factory,
506 Perspective perspective,
507 const ParsedQuicVersionVector& supported_versions)
508 : MockQuicConnection(TestConnectionId(),
509 QuicSocketAddress(TestPeerIPAddress(), kTestPort),
510 helper,
511 alarm_factory,
512 perspective,
513 supported_versions) {}
514
515MockQuicConnection::MockQuicConnection(
516 QuicConnectionId connection_id,
517 QuicSocketAddress address,
518 MockQuicConnectionHelper* helper,
519 MockAlarmFactory* alarm_factory,
520 Perspective perspective,
521 const ParsedQuicVersionVector& supported_versions)
522 : QuicConnection(connection_id,
523 address,
524 helper,
525 alarm_factory,
526 new testing::NiceMock<MockPacketWriter>(),
527 /* owns_writer= */ true,
528 perspective,
529 supported_versions) {
530 ON_CALL(*this, OnError(_))
531 .WillByDefault(
532 Invoke(this, &PacketSavingConnection::QuicConnection_OnError));
533 ON_CALL(*this, SendCryptoData(_, _, _))
534 .WillByDefault(
535 Invoke(this, &MockQuicConnection::QuicConnection_SendCryptoData));
536
537 SetSelfAddress(QuicSocketAddress(QuicIpAddress::Any4(), 5));
538}
539
540MockQuicConnection::~MockQuicConnection() {}
541
542void MockQuicConnection::AdvanceTime(QuicTime::Delta delta) {
543 static_cast<MockQuicConnectionHelper*>(helper())->AdvanceTime(delta);
544}
545
dschinazi17d42422019-06-18 16:35:07 -0700546bool MockQuicConnection::OnProtocolVersionMismatch(
fayang8aba1ff2019-06-21 12:00:54 -0700547 ParsedQuicVersion /*version*/) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500548 return false;
549}
550
551PacketSavingConnection::PacketSavingConnection(MockQuicConnectionHelper* helper,
552 MockAlarmFactory* alarm_factory,
553 Perspective perspective)
554 : MockQuicConnection(helper, alarm_factory, perspective) {}
555
556PacketSavingConnection::PacketSavingConnection(
557 MockQuicConnectionHelper* helper,
558 MockAlarmFactory* alarm_factory,
559 Perspective perspective,
560 const ParsedQuicVersionVector& supported_versions)
561 : MockQuicConnection(helper,
562 alarm_factory,
563 perspective,
564 supported_versions) {}
565
566PacketSavingConnection::~PacketSavingConnection() {}
567
wub8a5dafa2020-05-13 12:30:17 -0700568void PacketSavingConnection::SendOrQueuePacket(SerializedPacket packet) {
vasilvv0fc587f2019-09-06 13:33:08 -0700569 encrypted_packets_.push_back(std::make_unique<QuicEncryptedPacket>(
wub8a5dafa2020-05-13 12:30:17 -0700570 CopyBuffer(packet), packet.encrypted_length, true));
QUICHE teama6ef0a62019-03-07 20:34:33 -0500571 clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(10));
572 // Transfer ownership of the packet to the SentPacketManager and the
573 // ack notifier to the AckNotifierManager.
renjietange7b2cd82020-06-03 16:59:18 -0700574 OnPacketSent(packet.encryption_level, packet.transmission_type);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500575 QuicConnectionPeer::GetSentPacketManager(this)->OnPacketSent(
wub8a5dafa2020-05-13 12:30:17 -0700576 &packet, clock_.ApproximateNow(), NOT_RETRANSMISSION,
QUICHE teama6ef0a62019-03-07 20:34:33 -0500577 HAS_RETRANSMITTABLE_DATA);
578}
579
580MockQuicSession::MockQuicSession(QuicConnection* connection)
581 : MockQuicSession(connection, true) {}
582
583MockQuicSession::MockQuicSession(QuicConnection* connection,
584 bool create_mock_crypto_stream)
585 : QuicSession(connection,
586 nullptr,
587 DefaultQuicConfig(),
renjietang216dc012019-08-27 11:28:27 -0700588 connection->supported_versions(),
589 /*num_expected_unidirectional_static_streams = */ 0) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500590 if (create_mock_crypto_stream) {
vasilvv0fc587f2019-09-06 13:33:08 -0700591 crypto_stream_ = std::make_unique<MockQuicCryptoStream>(this);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500592 }
renjietang41a1b412020-02-27 15:05:14 -0800593 ON_CALL(*this, WritevData(_, _, _, _, _, _))
QUICHE teama6ef0a62019-03-07 20:34:33 -0500594 .WillByDefault(testing::Return(QuicConsumedData(0, false)));
595}
596
597MockQuicSession::~MockQuicSession() {
ianswett6aefa0b2019-12-10 07:26:15 -0800598 DeleteConnection();
QUICHE teama6ef0a62019-03-07 20:34:33 -0500599}
600
601QuicCryptoStream* MockQuicSession::GetMutableCryptoStream() {
602 return crypto_stream_.get();
603}
604
605const QuicCryptoStream* MockQuicSession::GetCryptoStream() const {
606 return crypto_stream_.get();
607}
608
609void MockQuicSession::SetCryptoStream(QuicCryptoStream* crypto_stream) {
610 crypto_stream_.reset(crypto_stream);
611}
612
renjietang41a1b412020-02-27 15:05:14 -0800613QuicConsumedData MockQuicSession::ConsumeData(
614 QuicStreamId id,
615 size_t write_length,
616 QuicStreamOffset offset,
617 StreamSendingState state,
renjietang4d992bf2020-03-03 13:01:55 -0800618 TransmissionType /*type*/,
renjietang41a1b412020-02-27 15:05:14 -0800619 quiche::QuicheOptional<EncryptionLevel> /*level*/) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500620 if (write_length > 0) {
vasilvv0fc587f2019-09-06 13:33:08 -0700621 auto buf = std::make_unique<char[]>(write_length);
renjietang7c239172020-02-21 13:50:39 -0800622 QuicStream* stream = GetOrCreateStream(id);
623 DCHECK(stream);
QUICHE team173c48f2019-11-19 16:34:44 -0800624 QuicDataWriter writer(write_length, buf.get(), quiche::HOST_BYTE_ORDER);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500625 stream->WriteStreamData(offset, write_length, &writer);
626 } else {
627 DCHECK(state != NO_FIN);
628 }
629 return QuicConsumedData(write_length, state != NO_FIN);
630}
631
632MockQuicCryptoStream::MockQuicCryptoStream(QuicSession* session)
633 : QuicCryptoStream(session), params_(new QuicCryptoNegotiatedParameters) {}
634
635MockQuicCryptoStream::~MockQuicCryptoStream() {}
636
637bool MockQuicCryptoStream::encryption_established() const {
638 return false;
639}
640
fayang685367a2020-01-14 10:40:15 -0800641bool MockQuicCryptoStream::one_rtt_keys_available() const {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500642 return false;
643}
644
645const QuicCryptoNegotiatedParameters&
646MockQuicCryptoStream::crypto_negotiated_params() const {
647 return *params_;
648}
649
650CryptoMessageParser* MockQuicCryptoStream::crypto_message_parser() {
651 return &crypto_framer_;
652}
653
654MockQuicSpdySession::MockQuicSpdySession(QuicConnection* connection)
655 : MockQuicSpdySession(connection, true) {}
656
657MockQuicSpdySession::MockQuicSpdySession(QuicConnection* connection,
658 bool create_mock_crypto_stream)
659 : QuicSpdySession(connection,
660 nullptr,
661 DefaultQuicConfig(),
662 connection->supported_versions()) {
663 if (create_mock_crypto_stream) {
vasilvv0fc587f2019-09-06 13:33:08 -0700664 crypto_stream_ = std::make_unique<MockQuicCryptoStream>(this);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500665 }
666
renjietang41a1b412020-02-27 15:05:14 -0800667 ON_CALL(*this, WritevData(_, _, _, _, _, _))
QUICHE teama6ef0a62019-03-07 20:34:33 -0500668 .WillByDefault(testing::Return(QuicConsumedData(0, false)));
wub713afae2020-04-27 07:48:31 -0700669
670 ON_CALL(*this, SendWindowUpdate(_, _))
671 .WillByDefault([this](QuicStreamId id, QuicStreamOffset byte_offset) {
672 return QuicSpdySession::SendWindowUpdate(id, byte_offset);
673 });
674
675 ON_CALL(*this, SendBlocked(_)).WillByDefault([this](QuicStreamId id) {
676 return QuicSpdySession::SendBlocked(id);
677 });
QUICHE teama6ef0a62019-03-07 20:34:33 -0500678}
679
680MockQuicSpdySession::~MockQuicSpdySession() {
ianswett6aefa0b2019-12-10 07:26:15 -0800681 DeleteConnection();
QUICHE teama6ef0a62019-03-07 20:34:33 -0500682}
683
684QuicCryptoStream* MockQuicSpdySession::GetMutableCryptoStream() {
685 return crypto_stream_.get();
686}
687
688const QuicCryptoStream* MockQuicSpdySession::GetCryptoStream() const {
689 return crypto_stream_.get();
690}
691
692void MockQuicSpdySession::SetCryptoStream(QuicCryptoStream* crypto_stream) {
693 crypto_stream_.reset(crypto_stream);
694}
695
renjietang41a1b412020-02-27 15:05:14 -0800696QuicConsumedData MockQuicSpdySession::ConsumeData(
697 QuicStreamId id,
698 size_t write_length,
699 QuicStreamOffset offset,
700 StreamSendingState state,
renjietang4d992bf2020-03-03 13:01:55 -0800701 TransmissionType /*type*/,
renjietang41a1b412020-02-27 15:05:14 -0800702 quiche::QuicheOptional<EncryptionLevel> /*level*/) {
renjietang7c239172020-02-21 13:50:39 -0800703 if (write_length > 0) {
704 auto buf = std::make_unique<char[]>(write_length);
705 QuicStream* stream = GetOrCreateStream(id);
706 DCHECK(stream);
707 QuicDataWriter writer(write_length, buf.get(), quiche::HOST_BYTE_ORDER);
708 stream->WriteStreamData(offset, write_length, &writer);
709 } else {
710 DCHECK(state != NO_FIN);
711 }
712 return QuicConsumedData(write_length, state != NO_FIN);
713}
714
QUICHE teama6ef0a62019-03-07 20:34:33 -0500715TestQuicSpdyServerSession::TestQuicSpdyServerSession(
716 QuicConnection* connection,
717 const QuicConfig& config,
718 const ParsedQuicVersionVector& supported_versions,
719 const QuicCryptoServerConfig* crypto_config,
720 QuicCompressedCertsCache* compressed_certs_cache)
721 : QuicServerSessionBase(config,
722 supported_versions,
723 connection,
724 &visitor_,
725 &helper_,
726 crypto_config,
727 compressed_certs_cache) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500728 ON_CALL(helper_, CanAcceptClientHello(_, _, _, _, _))
729 .WillByDefault(testing::Return(true));
730}
731
732TestQuicSpdyServerSession::~TestQuicSpdyServerSession() {
ianswett6aefa0b2019-12-10 07:26:15 -0800733 DeleteConnection();
QUICHE teama6ef0a62019-03-07 20:34:33 -0500734}
735
nharpere5e28f92020-01-03 14:10:07 -0800736std::unique_ptr<QuicCryptoServerStreamBase>
QUICHE teama6ef0a62019-03-07 20:34:33 -0500737TestQuicSpdyServerSession::CreateQuicCryptoServerStream(
738 const QuicCryptoServerConfig* crypto_config,
739 QuicCompressedCertsCache* compressed_certs_cache) {
nharpere5e28f92020-01-03 14:10:07 -0800740 return CreateCryptoServerStream(crypto_config, compressed_certs_cache, this,
741 &helper_);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500742}
743
nharperf579b5e2020-01-21 14:11:18 -0800744QuicCryptoServerStreamBase*
745TestQuicSpdyServerSession::GetMutableCryptoStream() {
746 return QuicServerSessionBase::GetMutableCryptoStream();
QUICHE teama6ef0a62019-03-07 20:34:33 -0500747}
748
nharperf579b5e2020-01-21 14:11:18 -0800749const QuicCryptoServerStreamBase* TestQuicSpdyServerSession::GetCryptoStream()
QUICHE teama6ef0a62019-03-07 20:34:33 -0500750 const {
nharperf579b5e2020-01-21 14:11:18 -0800751 return QuicServerSessionBase::GetCryptoStream();
QUICHE teama6ef0a62019-03-07 20:34:33 -0500752}
753
754TestQuicSpdyClientSession::TestQuicSpdyClientSession(
755 QuicConnection* connection,
756 const QuicConfig& config,
757 const ParsedQuicVersionVector& supported_versions,
758 const QuicServerId& server_id,
759 QuicCryptoClientConfig* crypto_config)
760 : QuicSpdyClientSessionBase(connection,
761 &push_promise_index_,
762 config,
763 supported_versions) {
nharperac52a862020-06-08 12:41:06 -0700764 // TODO(b/153726130): Consider adding SetServerApplicationStateForResumption
765 // calls in tests and set |has_application_state| to true.
vasilvv0fc587f2019-09-06 13:33:08 -0700766 crypto_stream_ = std::make_unique<QuicCryptoClientStream>(
QUICHE teama6ef0a62019-03-07 20:34:33 -0500767 server_id, this, crypto_test_utils::ProofVerifyContextForTesting(),
renjietangbcc066a2020-04-21 18:05:57 -0700768 crypto_config, this, /*has_application_state = */ false);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500769 Initialize();
770}
771
772TestQuicSpdyClientSession::~TestQuicSpdyClientSession() {}
773
dschinazi17d42422019-06-18 16:35:07 -0700774bool TestQuicSpdyClientSession::IsAuthorized(const std::string& /*authority*/) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500775 return true;
776}
777
QUICHE teama6ef0a62019-03-07 20:34:33 -0500778QuicCryptoClientStream* TestQuicSpdyClientSession::GetMutableCryptoStream() {
779 return crypto_stream_.get();
780}
781
782const QuicCryptoClientStream* TestQuicSpdyClientSession::GetCryptoStream()
783 const {
784 return crypto_stream_.get();
785}
786
787TestPushPromiseDelegate::TestPushPromiseDelegate(bool match)
788 : match_(match), rendezvous_fired_(false), rendezvous_stream_(nullptr) {}
789
790bool TestPushPromiseDelegate::CheckVary(
dschinazi17d42422019-06-18 16:35:07 -0700791 const spdy::SpdyHeaderBlock& /*client_request*/,
792 const spdy::SpdyHeaderBlock& /*promise_request*/,
793 const spdy::SpdyHeaderBlock& /*promise_response*/) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500794 QUIC_DVLOG(1) << "match " << match_;
795 return match_;
796}
797
798void TestPushPromiseDelegate::OnRendezvousResult(QuicSpdyStream* stream) {
799 rendezvous_fired_ = true;
800 rendezvous_stream_ = stream;
801}
802
803MockPacketWriter::MockPacketWriter() {
804 ON_CALL(*this, GetMaxPacketSize(_))
dschinazi66dea072019-04-09 11:41:06 -0700805 .WillByDefault(testing::Return(kMaxOutgoingPacketSize));
QUICHE teama6ef0a62019-03-07 20:34:33 -0500806 ON_CALL(*this, IsBatchMode()).WillByDefault(testing::Return(false));
807 ON_CALL(*this, GetNextWriteLocation(_, _))
wub50d4c712020-05-19 15:48:28 -0700808 .WillByDefault(testing::Return(QuicPacketBuffer()));
QUICHE teama6ef0a62019-03-07 20:34:33 -0500809 ON_CALL(*this, Flush())
810 .WillByDefault(testing::Return(WriteResult(WRITE_STATUS_OK, 0)));
dschinazi42fc2da2020-04-24 16:19:17 -0700811 ON_CALL(*this, SupportsReleaseTime()).WillByDefault(testing::Return(false));
QUICHE teama6ef0a62019-03-07 20:34:33 -0500812}
813
814MockPacketWriter::~MockPacketWriter() {}
815
dschinazi3cf1d472020-04-01 14:37:43 -0700816MockSendAlgorithm::MockSendAlgorithm() {
817 ON_CALL(*this, PacingRate(_))
818 .WillByDefault(testing::Return(QuicBandwidth::Zero()));
819 ON_CALL(*this, BandwidthEstimate())
820 .WillByDefault(testing::Return(QuicBandwidth::Zero()));
821}
QUICHE teama6ef0a62019-03-07 20:34:33 -0500822
823MockSendAlgorithm::~MockSendAlgorithm() {}
824
825MockLossAlgorithm::MockLossAlgorithm() {}
826
827MockLossAlgorithm::~MockLossAlgorithm() {}
828
829MockAckListener::MockAckListener() {}
830
831MockAckListener::~MockAckListener() {}
832
833MockNetworkChangeVisitor::MockNetworkChangeVisitor() {}
834
835MockNetworkChangeVisitor::~MockNetworkChangeVisitor() {}
836
QUICHE teama6ef0a62019-03-07 20:34:33 -0500837QuicIpAddress TestPeerIPAddress() {
838 return QuicIpAddress::Loopback4();
839}
840
841ParsedQuicVersion QuicVersionMax() {
842 return AllSupportedVersions().front();
843}
844
845ParsedQuicVersion QuicVersionMin() {
846 return AllSupportedVersions().back();
847}
848
dschinazic1521a02020-04-07 09:55:08 -0700849void DisableQuicVersionsWithTls() {
dschinazi5a50d932020-06-17 12:43:36 -0700850 for (const ParsedQuicVersion& version : AllSupportedVersionsWithTls()) {
851 QuicDisableVersion(version);
852 }
dschinazic1521a02020-04-07 09:55:08 -0700853}
854
QUICHE teama6ef0a62019-03-07 20:34:33 -0500855QuicEncryptedPacket* ConstructEncryptedPacket(
856 QuicConnectionId destination_connection_id,
857 QuicConnectionId source_connection_id,
858 bool version_flag,
859 bool reset_flag,
860 uint64_t packet_number,
vasilvvc48c8712019-03-11 13:38:16 -0700861 const std::string& data) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500862 return ConstructEncryptedPacket(
863 destination_connection_id, source_connection_id, version_flag, reset_flag,
864 packet_number, data, CONNECTION_ID_PRESENT, CONNECTION_ID_ABSENT,
865 PACKET_4BYTE_PACKET_NUMBER);
866}
867
868QuicEncryptedPacket* ConstructEncryptedPacket(
869 QuicConnectionId destination_connection_id,
870 QuicConnectionId source_connection_id,
871 bool version_flag,
872 bool reset_flag,
873 uint64_t packet_number,
vasilvvc48c8712019-03-11 13:38:16 -0700874 const std::string& data,
QUICHE teama6ef0a62019-03-07 20:34:33 -0500875 QuicConnectionIdIncluded destination_connection_id_included,
876 QuicConnectionIdIncluded source_connection_id_included,
877 QuicPacketNumberLength packet_number_length) {
878 return ConstructEncryptedPacket(
879 destination_connection_id, source_connection_id, version_flag, reset_flag,
880 packet_number, data, destination_connection_id_included,
881 source_connection_id_included, packet_number_length, nullptr);
882}
883
884QuicEncryptedPacket* ConstructEncryptedPacket(
885 QuicConnectionId destination_connection_id,
886 QuicConnectionId source_connection_id,
887 bool version_flag,
888 bool reset_flag,
889 uint64_t packet_number,
vasilvvc48c8712019-03-11 13:38:16 -0700890 const std::string& data,
QUICHE teama6ef0a62019-03-07 20:34:33 -0500891 QuicConnectionIdIncluded destination_connection_id_included,
892 QuicConnectionIdIncluded source_connection_id_included,
893 QuicPacketNumberLength packet_number_length,
894 ParsedQuicVersionVector* versions) {
895 return ConstructEncryptedPacket(
896 destination_connection_id, source_connection_id, version_flag, reset_flag,
fayange3f2f7b2019-09-19 17:01:57 -0700897 packet_number, data, false, destination_connection_id_included,
QUICHE teama6ef0a62019-03-07 20:34:33 -0500898 source_connection_id_included, packet_number_length, versions,
899 Perspective::IS_CLIENT);
900}
fayange3f2f7b2019-09-19 17:01:57 -0700901
QUICHE teama6ef0a62019-03-07 20:34:33 -0500902QuicEncryptedPacket* ConstructEncryptedPacket(
903 QuicConnectionId destination_connection_id,
904 QuicConnectionId source_connection_id,
905 bool version_flag,
906 bool reset_flag,
907 uint64_t packet_number,
vasilvvc48c8712019-03-11 13:38:16 -0700908 const std::string& data,
fayange3f2f7b2019-09-19 17:01:57 -0700909 bool full_padding,
910 QuicConnectionIdIncluded destination_connection_id_included,
911 QuicConnectionIdIncluded source_connection_id_included,
912 QuicPacketNumberLength packet_number_length,
913 ParsedQuicVersionVector* versions) {
914 return ConstructEncryptedPacket(
915 destination_connection_id, source_connection_id, version_flag, reset_flag,
916 packet_number, data, full_padding, destination_connection_id_included,
917 source_connection_id_included, packet_number_length, versions,
918 Perspective::IS_CLIENT);
919}
920
921QuicEncryptedPacket* ConstructEncryptedPacket(
922 QuicConnectionId destination_connection_id,
923 QuicConnectionId source_connection_id,
924 bool version_flag,
925 bool reset_flag,
926 uint64_t packet_number,
927 const std::string& data,
928 bool full_padding,
QUICHE teama6ef0a62019-03-07 20:34:33 -0500929 QuicConnectionIdIncluded destination_connection_id_included,
930 QuicConnectionIdIncluded source_connection_id_included,
931 QuicPacketNumberLength packet_number_length,
932 ParsedQuicVersionVector* versions,
933 Perspective perspective) {
934 QuicPacketHeader header;
935 header.destination_connection_id = destination_connection_id;
936 header.destination_connection_id_included =
937 destination_connection_id_included;
938 header.source_connection_id = source_connection_id;
939 header.source_connection_id_included = source_connection_id_included;
940 header.version_flag = version_flag;
941 header.reset_flag = reset_flag;
942 header.packet_number_length = packet_number_length;
943 header.packet_number = QuicPacketNumber(packet_number);
944 ParsedQuicVersionVector supported_versions = CurrentSupportedVersions();
945 if (!versions) {
946 versions = &supported_versions;
947 }
dschinazi41616842020-01-21 15:46:11 -0800948 EXPECT_FALSE(versions->empty());
dschinaziecad9642019-10-01 10:44:17 -0700949 ParsedQuicVersion version = (*versions)[0];
950 if (QuicVersionHasLongHeaderLengths(version.transport_version) &&
QUICHE teama6ef0a62019-03-07 20:34:33 -0500951 version_flag) {
952 header.retry_token_length_length = VARIABLE_LENGTH_INTEGER_LENGTH_1;
953 header.length_length = VARIABLE_LENGTH_INTEGER_LENGTH_2;
954 }
955
956 QuicFrames frames;
957 QuicFramer framer(*versions, QuicTime::Zero(), perspective,
958 kQuicDefaultConnectionIdLength);
nharper7c075282019-09-18 10:45:32 -0700959 framer.SetInitialObfuscators(destination_connection_id);
nharper55fa6132019-05-07 19:37:21 -0700960 EncryptionLevel level =
961 header.version_flag ? ENCRYPTION_INITIAL : ENCRYPTION_FORWARD_SECURE;
nharper7c075282019-09-18 10:45:32 -0700962 if (level != ENCRYPTION_INITIAL) {
963 framer.SetEncrypter(level, std::make_unique<NullEncrypter>(perspective));
nharper55fa6132019-05-07 19:37:21 -0700964 }
965 if (!QuicVersionUsesCryptoFrames(version.transport_version)) {
966 QuicFrame frame(
967 QuicStreamFrame(QuicUtils::GetCryptoStreamId(version.transport_version),
QUICHE team6dcf6ab2019-12-11 10:10:51 -0800968 false, 0, quiche::QuicheStringPiece(data)));
QUICHE teama6ef0a62019-03-07 20:34:33 -0500969 frames.push_back(frame);
970 } else {
nharper55fa6132019-05-07 19:37:21 -0700971 QuicFrame frame(new QuicCryptoFrame(level, 0, data));
QUICHE teama6ef0a62019-03-07 20:34:33 -0500972 frames.push_back(frame);
973 }
fayange3f2f7b2019-09-19 17:01:57 -0700974 if (full_padding) {
975 frames.push_back(QuicFrame(QuicPaddingFrame(-1)));
976 } else {
977 // We need a minimum number of bytes of encrypted payload. This will
978 // guarantee that we have at least that much. (It ignores the overhead of
979 // the stream/crypto framing, so it overpads slightly.)
980 size_t min_plaintext_size =
981 QuicPacketCreator::MinPlaintextPacketSize(version);
982 if (data.length() < min_plaintext_size) {
983 size_t padding_length = min_plaintext_size - data.length();
984 frames.push_back(QuicFrame(QuicPaddingFrame(padding_length)));
985 }
nharper55fa6132019-05-07 19:37:21 -0700986 }
QUICHE teama6ef0a62019-03-07 20:34:33 -0500987
988 std::unique_ptr<QuicPacket> packet(
989 BuildUnsizedDataPacket(&framer, header, frames));
990 EXPECT_TRUE(packet != nullptr);
dschinazi66dea072019-04-09 11:41:06 -0700991 char* buffer = new char[kMaxOutgoingPacketSize];
QUICHE teama6ef0a62019-03-07 20:34:33 -0500992 size_t encrypted_length =
nharper7c075282019-09-18 10:45:32 -0700993 framer.EncryptPayload(level, QuicPacketNumber(packet_number), *packet,
994 buffer, kMaxOutgoingPacketSize);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500995 EXPECT_NE(0u, encrypted_length);
996 DeleteFrames(&frames);
997 return new QuicEncryptedPacket(buffer, encrypted_length, true);
998}
999
dschinazi42fc2da2020-04-24 16:19:17 -07001000std::unique_ptr<QuicEncryptedPacket> GetUndecryptableEarlyPacket(
1001 const ParsedQuicVersion& version,
1002 const QuicConnectionId& server_connection_id) {
1003 QuicPacketHeader header;
1004 header.destination_connection_id = server_connection_id;
1005 header.destination_connection_id_included = CONNECTION_ID_PRESENT;
1006 header.source_connection_id = EmptyQuicConnectionId();
1007 header.source_connection_id_included = CONNECTION_ID_PRESENT;
1008 if (!version.SupportsClientConnectionIds()) {
1009 header.source_connection_id_included = CONNECTION_ID_ABSENT;
1010 }
1011 header.version_flag = true;
1012 header.reset_flag = false;
1013 header.packet_number_length = PACKET_4BYTE_PACKET_NUMBER;
1014 header.packet_number = QuicPacketNumber(33);
1015 header.long_packet_type = ZERO_RTT_PROTECTED;
1016 if (version.HasLongHeaderLengths()) {
1017 header.retry_token_length_length = VARIABLE_LENGTH_INTEGER_LENGTH_1;
1018 header.length_length = VARIABLE_LENGTH_INTEGER_LENGTH_2;
1019 }
1020
1021 QuicFrames frames;
1022 frames.push_back(QuicFrame(QuicPingFrame()));
1023 frames.push_back(QuicFrame(QuicPaddingFrame(100)));
1024 QuicFramer framer({version}, QuicTime::Zero(), Perspective::IS_CLIENT,
1025 kQuicDefaultConnectionIdLength);
1026 framer.SetInitialObfuscators(server_connection_id);
1027
1028 framer.SetEncrypter(ENCRYPTION_ZERO_RTT,
1029 std::make_unique<NullEncrypter>(Perspective::IS_CLIENT));
1030 std::unique_ptr<QuicPacket> packet(
1031 BuildUnsizedDataPacket(&framer, header, frames));
1032 EXPECT_TRUE(packet != nullptr);
1033 char* buffer = new char[kMaxOutgoingPacketSize];
1034 size_t encrypted_length =
1035 framer.EncryptPayload(ENCRYPTION_ZERO_RTT, header.packet_number, *packet,
1036 buffer, kMaxOutgoingPacketSize);
1037 EXPECT_NE(0u, encrypted_length);
1038 DeleteFrames(&frames);
1039 return std::make_unique<QuicEncryptedPacket>(buffer, encrypted_length,
1040 /*owns_buffer=*/true);
1041}
1042
QUICHE teama6ef0a62019-03-07 20:34:33 -05001043QuicReceivedPacket* ConstructReceivedPacket(
1044 const QuicEncryptedPacket& encrypted_packet,
1045 QuicTime receipt_time) {
1046 char* buffer = new char[encrypted_packet.length()];
1047 memcpy(buffer, encrypted_packet.data(), encrypted_packet.length());
1048 return new QuicReceivedPacket(buffer, encrypted_packet.length(), receipt_time,
1049 true);
1050}
1051
1052QuicEncryptedPacket* ConstructMisFramedEncryptedPacket(
1053 QuicConnectionId destination_connection_id,
1054 QuicConnectionId source_connection_id,
1055 bool version_flag,
1056 bool reset_flag,
1057 uint64_t packet_number,
vasilvvc48c8712019-03-11 13:38:16 -07001058 const std::string& data,
QUICHE teama6ef0a62019-03-07 20:34:33 -05001059 QuicConnectionIdIncluded destination_connection_id_included,
1060 QuicConnectionIdIncluded source_connection_id_included,
1061 QuicPacketNumberLength packet_number_length,
bnc9711a9e2019-11-01 06:31:51 -07001062 ParsedQuicVersion version,
QUICHE teama6ef0a62019-03-07 20:34:33 -05001063 Perspective perspective) {
1064 QuicPacketHeader header;
1065 header.destination_connection_id = destination_connection_id;
1066 header.destination_connection_id_included =
1067 destination_connection_id_included;
1068 header.source_connection_id = source_connection_id;
1069 header.source_connection_id_included = source_connection_id_included;
1070 header.version_flag = version_flag;
1071 header.reset_flag = reset_flag;
1072 header.packet_number_length = packet_number_length;
1073 header.packet_number = QuicPacketNumber(packet_number);
bnc9711a9e2019-11-01 06:31:51 -07001074 if (QuicVersionHasLongHeaderLengths(version.transport_version) &&
zhongyi546cc452019-04-12 15:27:49 -07001075 version_flag) {
1076 header.retry_token_length_length = VARIABLE_LENGTH_INTEGER_LENGTH_1;
1077 header.length_length = VARIABLE_LENGTH_INTEGER_LENGTH_2;
1078 }
QUICHE team6dcf6ab2019-12-11 10:10:51 -08001079 QuicFrame frame(
1080 QuicStreamFrame(1, false, 0, quiche::QuicheStringPiece(data)));
QUICHE teama6ef0a62019-03-07 20:34:33 -05001081 QuicFrames frames;
1082 frames.push_back(frame);
bnc9711a9e2019-11-01 06:31:51 -07001083 QuicFramer framer({version}, QuicTime::Zero(), perspective,
QUICHE teama6ef0a62019-03-07 20:34:33 -05001084 kQuicDefaultConnectionIdLength);
nharper7c075282019-09-18 10:45:32 -07001085 framer.SetInitialObfuscators(destination_connection_id);
1086 EncryptionLevel level =
1087 version_flag ? ENCRYPTION_INITIAL : ENCRYPTION_FORWARD_SECURE;
1088 if (level != ENCRYPTION_INITIAL) {
1089 framer.SetEncrypter(level, std::make_unique<NullEncrypter>(perspective));
nharper55fa6132019-05-07 19:37:21 -07001090 }
1091 // We need a minimum of 7 bytes of encrypted payload. This will guarantee that
1092 // we have at least that much. (It ignores the overhead of the stream/crypto
1093 // framing, so it overpads slightly.)
1094 if (data.length() < 7) {
1095 size_t padding_length = 7 - data.length();
1096 frames.push_back(QuicFrame(QuicPaddingFrame(padding_length)));
1097 }
QUICHE teama6ef0a62019-03-07 20:34:33 -05001098
1099 std::unique_ptr<QuicPacket> packet(
1100 BuildUnsizedDataPacket(&framer, header, frames));
1101 EXPECT_TRUE(packet != nullptr);
1102
1103 // Now set the frame type to 0x1F, which is an invalid frame type.
1104 reinterpret_cast<unsigned char*>(
1105 packet->mutable_data())[GetStartOfEncryptedData(
1106 framer.transport_version(),
1107 GetIncludedDestinationConnectionIdLength(header),
1108 GetIncludedSourceConnectionIdLength(header), version_flag,
1109 false /* no diversification nonce */, packet_number_length,
zhongyi546cc452019-04-12 15:27:49 -07001110 header.retry_token_length_length, 0, header.length_length)] = 0x1F;
QUICHE teama6ef0a62019-03-07 20:34:33 -05001111
dschinazi66dea072019-04-09 11:41:06 -07001112 char* buffer = new char[kMaxOutgoingPacketSize];
QUICHE teama6ef0a62019-03-07 20:34:33 -05001113 size_t encrypted_length =
nharper7c075282019-09-18 10:45:32 -07001114 framer.EncryptPayload(level, QuicPacketNumber(packet_number), *packet,
1115 buffer, kMaxOutgoingPacketSize);
QUICHE teama6ef0a62019-03-07 20:34:33 -05001116 EXPECT_NE(0u, encrypted_length);
1117 return new QuicEncryptedPacket(buffer, encrypted_length, true);
1118}
1119
QUICHE teama6ef0a62019-03-07 20:34:33 -05001120QuicConfig DefaultQuicConfig() {
1121 QuicConfig config;
rchb0451852019-09-11 21:17:01 -07001122 config.SetInitialMaxStreamDataBytesIncomingBidirectionalToSend(
1123 kInitialStreamFlowControlWindowForTest);
1124 config.SetInitialMaxStreamDataBytesOutgoingBidirectionalToSend(
1125 kInitialStreamFlowControlWindowForTest);
1126 config.SetInitialMaxStreamDataBytesUnidirectionalToSend(
1127 kInitialStreamFlowControlWindowForTest);
QUICHE teama6ef0a62019-03-07 20:34:33 -05001128 config.SetInitialStreamFlowControlWindowToSend(
1129 kInitialStreamFlowControlWindowForTest);
1130 config.SetInitialSessionFlowControlWindowToSend(
1131 kInitialSessionFlowControlWindowForTest);
renjietange6d94672020-01-07 10:30:10 -08001132 QuicConfigPeer::SetReceivedMaxBidirectionalStreams(
QUICHE teama6ef0a62019-03-07 20:34:33 -05001133 &config, kDefaultMaxStreamsPerConnection);
1134 // Default enable NSTP.
1135 // This is unnecessary for versions > 44
1136 if (!config.HasClientSentConnectionOption(quic::kNSTP,
1137 quic::Perspective::IS_CLIENT)) {
1138 quic::QuicTagVector connection_options;
1139 connection_options.push_back(quic::kNSTP);
1140 config.SetConnectionOptionsToSend(connection_options);
1141 }
1142 return config;
1143}
1144
QUICHE teama6ef0a62019-03-07 20:34:33 -05001145ParsedQuicVersionVector SupportedVersions(ParsedQuicVersion version) {
1146 ParsedQuicVersionVector versions;
1147 versions.push_back(version);
1148 return versions;
1149}
1150
1151MockQuicConnectionDebugVisitor::MockQuicConnectionDebugVisitor() {}
1152
1153MockQuicConnectionDebugVisitor::~MockQuicConnectionDebugVisitor() {}
1154
1155MockReceivedPacketManager::MockReceivedPacketManager(QuicConnectionStats* stats)
1156 : QuicReceivedPacketManager(stats) {}
1157
1158MockReceivedPacketManager::~MockReceivedPacketManager() {}
1159
QUICHE teama6ef0a62019-03-07 20:34:33 -05001160MockPacketCreatorDelegate::MockPacketCreatorDelegate() {}
1161MockPacketCreatorDelegate::~MockPacketCreatorDelegate() {}
1162
1163MockSessionNotifier::MockSessionNotifier() {}
1164MockSessionNotifier::~MockSessionNotifier() {}
1165
nharper1e32f4f2020-04-20 12:23:01 -07001166// static
1167QuicCryptoClientStream::HandshakerInterface*
1168QuicCryptoClientStreamPeer::GetHandshaker(QuicCryptoClientStream* stream) {
1169 return stream->handshaker_.get();
1170}
1171
QUICHE teama6ef0a62019-03-07 20:34:33 -05001172void CreateClientSessionForTest(
1173 QuicServerId server_id,
QUICHE teama6ef0a62019-03-07 20:34:33 -05001174 QuicTime::Delta connection_start_time,
1175 const ParsedQuicVersionVector& supported_versions,
1176 MockQuicConnectionHelper* helper,
1177 MockAlarmFactory* alarm_factory,
1178 QuicCryptoClientConfig* crypto_client_config,
1179 PacketSavingConnection** client_connection,
1180 TestQuicSpdyClientSession** client_session) {
1181 CHECK(crypto_client_config);
1182 CHECK(client_connection);
1183 CHECK(client_session);
1184 CHECK(!connection_start_time.IsZero())
1185 << "Connections must start at non-zero times, otherwise the "
1186 << "strike-register will be unhappy.";
1187
wube9dc7da2019-05-22 06:08:54 -07001188 QuicConfig config = DefaultQuicConfig();
QUICHE teama6ef0a62019-03-07 20:34:33 -05001189 *client_connection = new PacketSavingConnection(
1190 helper, alarm_factory, Perspective::IS_CLIENT, supported_versions);
1191 *client_session = new TestQuicSpdyClientSession(*client_connection, config,
1192 supported_versions, server_id,
1193 crypto_client_config);
1194 (*client_connection)->AdvanceTime(connection_start_time);
1195}
1196
1197void CreateServerSessionForTest(
dschinazi17d42422019-06-18 16:35:07 -07001198 QuicServerId /*server_id*/,
QUICHE teama6ef0a62019-03-07 20:34:33 -05001199 QuicTime::Delta connection_start_time,
1200 ParsedQuicVersionVector supported_versions,
1201 MockQuicConnectionHelper* helper,
1202 MockAlarmFactory* alarm_factory,
1203 QuicCryptoServerConfig* server_crypto_config,
1204 QuicCompressedCertsCache* compressed_certs_cache,
1205 PacketSavingConnection** server_connection,
1206 TestQuicSpdyServerSession** server_session) {
1207 CHECK(server_crypto_config);
1208 CHECK(server_connection);
1209 CHECK(server_session);
1210 CHECK(!connection_start_time.IsZero())
1211 << "Connections must start at non-zero times, otherwise the "
1212 << "strike-register will be unhappy.";
1213
1214 *server_connection =
1215 new PacketSavingConnection(helper, alarm_factory, Perspective::IS_SERVER,
1216 ParsedVersionOfIndex(supported_versions, 0));
1217 *server_session = new TestQuicSpdyServerSession(
1218 *server_connection, DefaultQuicConfig(), supported_versions,
1219 server_crypto_config, compressed_certs_cache);
wuba89eee32019-11-22 13:02:52 -08001220 (*server_session)->Initialize();
QUICHE teama6ef0a62019-03-07 20:34:33 -05001221
1222 // We advance the clock initially because the default time is zero and the
1223 // strike register worries that we've just overflowed a uint32_t time.
1224 (*server_connection)->AdvanceTime(connection_start_time);
1225}
1226
1227QuicStreamId GetNthClientInitiatedBidirectionalStreamId(
1228 QuicTransportVersion version,
1229 int n) {
dschinazi552accc2019-06-17 17:07:34 -07001230 int num = n;
renjietanga29a96a2019-10-10 12:47:50 -07001231 if (!VersionUsesHttp3(version)) {
renjietang118c8ac2019-07-30 11:43:59 -07001232 num++;
dschinazi552accc2019-06-17 17:07:34 -07001233 }
QUICHE teama6ef0a62019-03-07 20:34:33 -05001234 return QuicUtils::GetFirstBidirectionalStreamId(version,
1235 Perspective::IS_CLIENT) +
dschinazi552accc2019-06-17 17:07:34 -07001236 QuicUtils::StreamIdDelta(version) * num;
QUICHE teama6ef0a62019-03-07 20:34:33 -05001237}
1238
1239QuicStreamId GetNthServerInitiatedBidirectionalStreamId(
1240 QuicTransportVersion version,
1241 int n) {
1242 return QuicUtils::GetFirstBidirectionalStreamId(version,
1243 Perspective::IS_SERVER) +
1244 QuicUtils::StreamIdDelta(version) * n;
1245}
1246
1247QuicStreamId GetNthServerInitiatedUnidirectionalStreamId(
1248 QuicTransportVersion version,
1249 int n) {
1250 return QuicUtils::GetFirstUnidirectionalStreamId(version,
1251 Perspective::IS_SERVER) +
1252 QuicUtils::StreamIdDelta(version) * n;
1253}
1254
renjietang3a1bb802019-06-11 10:42:41 -07001255QuicStreamId GetNthClientInitiatedUnidirectionalStreamId(
1256 QuicTransportVersion version,
1257 int n) {
1258 return QuicUtils::GetFirstUnidirectionalStreamId(version,
1259 Perspective::IS_CLIENT) +
1260 QuicUtils::StreamIdDelta(version) * n;
1261}
1262
QUICHE teama6ef0a62019-03-07 20:34:33 -05001263StreamType DetermineStreamType(QuicStreamId id,
renjietangd262e252020-06-19 15:11:24 -07001264 ParsedQuicVersion version,
QUICHE teama6ef0a62019-03-07 20:34:33 -05001265 Perspective perspective,
1266 bool is_incoming,
1267 StreamType default_type) {
renjietangd262e252020-06-19 15:11:24 -07001268 return version.HasIetfQuicFrames()
1269 ? QuicUtils::GetStreamType(id, perspective, is_incoming, version)
QUICHE teama6ef0a62019-03-07 20:34:33 -05001270 : default_type;
1271}
1272
1273QuicMemSliceSpan MakeSpan(QuicBufferAllocator* allocator,
QUICHE team6dcf6ab2019-12-11 10:10:51 -08001274 quiche::QuicheStringPiece message_data,
QUICHE teama6ef0a62019-03-07 20:34:33 -05001275 QuicMemSliceStorage* storage) {
1276 if (message_data.length() == 0) {
dschinazi66dea072019-04-09 11:41:06 -07001277 *storage =
1278 QuicMemSliceStorage(nullptr, 0, allocator, kMaxOutgoingPacketSize);
QUICHE teama6ef0a62019-03-07 20:34:33 -05001279 return storage->ToSpan();
1280 }
1281 struct iovec iov = {const_cast<char*>(message_data.data()),
1282 message_data.length()};
dschinazi66dea072019-04-09 11:41:06 -07001283 *storage = QuicMemSliceStorage(&iov, 1, allocator, kMaxOutgoingPacketSize);
QUICHE teama6ef0a62019-03-07 20:34:33 -05001284 return storage->ToSpan();
1285}
1286
vasilvv2b0ab242020-01-07 07:32:09 -08001287QuicMemSlice MemSliceFromString(quiche::QuicheStringPiece data) {
1288 static SimpleBufferAllocator* allocator = new SimpleBufferAllocator();
1289 QuicUniqueBufferPtr buffer = MakeUniqueBuffer(allocator, data.size());
1290 memcpy(buffer.get(), data.data(), data.size());
1291 return QuicMemSlice(std::move(buffer), data.size());
1292}
1293
QUICHE teama6ef0a62019-03-07 20:34:33 -05001294} // namespace test
1295} // namespace quic