blob: beb6571f445663f43035d2e04b11684a612d5838 [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/core/quic_framer.h"
6
7#include <algorithm>
8#include <cstdint>
9#include <map>
10#include <memory>
vasilvv872e7a32019-03-12 16:42:44 -070011#include <string>
QUICHE teama6ef0a62019-03-07 20:34:33 -050012#include <vector>
13
14#include "net/third_party/quiche/src/quic/core/crypto/null_decrypter.h"
15#include "net/third_party/quiche/src/quic/core/crypto/null_encrypter.h"
16#include "net/third_party/quiche/src/quic/core/crypto/quic_decrypter.h"
17#include "net/third_party/quiche/src/quic/core/crypto/quic_encrypter.h"
18#include "net/third_party/quiche/src/quic/core/quic_connection_id.h"
dschinazicf5b1e22019-07-17 18:35:17 -070019#include "net/third_party/quiche/src/quic/core/quic_error_codes.h"
QUICHE teama6ef0a62019-03-07 20:34:33 -050020#include "net/third_party/quiche/src/quic/core/quic_packets.h"
21#include "net/third_party/quiche/src/quic/core/quic_types.h"
22#include "net/third_party/quiche/src/quic/core/quic_utils.h"
dschinazi243eabc2019-08-05 16:15:29 -070023#include "net/third_party/quiche/src/quic/core/quic_versions.h"
QUICHE teama6ef0a62019-03-07 20:34:33 -050024#include "net/third_party/quiche/src/quic/platform/api/quic_arraysize.h"
25#include "net/third_party/quiche/src/quic/platform/api/quic_expect_bug.h"
26#include "net/third_party/quiche/src/quic/platform/api/quic_flags.h"
27#include "net/third_party/quiche/src/quic/platform/api/quic_logging.h"
28#include "net/third_party/quiche/src/quic/platform/api/quic_ptr_util.h"
QUICHE teama6ef0a62019-03-07 20:34:33 -050029#include "net/third_party/quiche/src/quic/platform/api/quic_test.h"
30#include "net/third_party/quiche/src/quic/platform/api/quic_text_utils.h"
31#include "net/third_party/quiche/src/quic/test_tools/quic_framer_peer.h"
32#include "net/third_party/quiche/src/quic/test_tools/quic_test_utils.h"
33#include "net/third_party/quiche/src/quic/test_tools/simple_data_producer.h"
34
35using testing::_;
36using testing::Return;
37using testing::Truly;
38
39namespace quic {
40namespace test {
41namespace {
42
43const uint64_t kEpoch = UINT64_C(1) << 32;
44const uint64_t kMask = kEpoch - 1;
45
46const QuicUint128 kTestStatelessResetToken = 1010101; // 0x0F69B5
47
48// Use fields in which each byte is distinct to ensure that every byte is
49// framed correctly. The values are otherwise arbitrary.
50QuicConnectionId FramerTestConnectionId() {
51 return TestConnectionId(UINT64_C(0xFEDCBA9876543210));
52}
53
54QuicConnectionId FramerTestConnectionIdPlusOne() {
55 return TestConnectionId(UINT64_C(0xFEDCBA9876543211));
56}
57
QUICHE team8e2e4532019-03-14 14:37:56 -070058QuicConnectionId FramerTestConnectionIdNineBytes() {
59 char connection_id_bytes[9] = {0xFE, 0xDC, 0xBA, 0x98, 0x76,
60 0x54, 0x32, 0x10, 0x42};
61 return QuicConnectionId(connection_id_bytes, sizeof(connection_id_bytes));
62}
63
QUICHE teama6ef0a62019-03-07 20:34:33 -050064const QuicPacketNumber kPacketNumber = QuicPacketNumber(UINT64_C(0x12345678));
65const QuicPacketNumber kSmallLargestObserved =
66 QuicPacketNumber(UINT16_C(0x1234));
67const QuicPacketNumber kSmallMissingPacket = QuicPacketNumber(UINT16_C(0x1233));
68const QuicPacketNumber kLeastUnacked = QuicPacketNumber(UINT64_C(0x012345670));
69const QuicStreamId kStreamId = UINT64_C(0x01020304);
70// Note that the high 4 bits of the stream offset must be less than 0x40
71// in order to ensure that the value can be encoded using VarInt62 encoding.
72const QuicStreamOffset kStreamOffset = UINT64_C(0x3A98FEDC32107654);
73const QuicPublicResetNonceProof kNonceProof = UINT64_C(0xABCDEF0123456789);
74
75// In testing that we can ack the full range of packets...
76// This is the largest packet number that can be represented in IETF QUIC
77// varint62 format.
78const QuicPacketNumber kLargestIetfLargestObserved =
79 QuicPacketNumber(UINT64_C(0x3fffffffffffffff));
80// Encodings for the two bits in a VarInt62 that
81// describe the length of the VarInt61. For binary packet
82// formats in this file, the convention is to code the
83// first byte as
84// kVarInt62FourBytes + 0x<value_in_that_byte>
85const uint8_t kVarInt62OneByte = 0x00;
86const uint8_t kVarInt62TwoBytes = 0x40;
87const uint8_t kVarInt62FourBytes = 0x80;
88const uint8_t kVarInt62EightBytes = 0xc0;
89
90class TestEncrypter : public QuicEncrypter {
91 public:
92 ~TestEncrypter() override {}
dschinazi17d42422019-06-18 16:35:07 -070093 bool SetKey(QuicStringPiece /*key*/) override { return true; }
94 bool SetNoncePrefix(QuicStringPiece /*nonce_prefix*/) override {
95 return true;
96 }
97 bool SetIV(QuicStringPiece /*iv*/) override { return true; }
98 bool SetHeaderProtectionKey(QuicStringPiece /*key*/) override { return true; }
QUICHE teama6ef0a62019-03-07 20:34:33 -050099 bool EncryptPacket(uint64_t packet_number,
100 QuicStringPiece associated_data,
101 QuicStringPiece plaintext,
102 char* output,
103 size_t* output_length,
dschinazi17d42422019-06-18 16:35:07 -0700104 size_t /*max_output_length*/) override {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500105 packet_number_ = QuicPacketNumber(packet_number);
vasilvvc48c8712019-03-11 13:38:16 -0700106 associated_data_ = std::string(associated_data);
107 plaintext_ = std::string(plaintext);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500108 memcpy(output, plaintext.data(), plaintext.length());
109 *output_length = plaintext.length();
110 return true;
111 }
dschinazi17d42422019-06-18 16:35:07 -0700112 std::string GenerateHeaderProtectionMask(
113 QuicStringPiece /*sample*/) override {
QUICHE team2d187972019-03-19 16:23:47 -0700114 return std::string(5, 0);
115 }
QUICHE teama6ef0a62019-03-07 20:34:33 -0500116 size_t GetKeySize() const override { return 0; }
117 size_t GetNoncePrefixSize() const override { return 0; }
118 size_t GetIVSize() const override { return 0; }
119 size_t GetMaxPlaintextSize(size_t ciphertext_size) const override {
120 return ciphertext_size;
121 }
122 size_t GetCiphertextSize(size_t plaintext_size) const override {
123 return plaintext_size;
124 }
125 QuicStringPiece GetKey() const override { return QuicStringPiece(); }
126 QuicStringPiece GetNoncePrefix() const override { return QuicStringPiece(); }
127
128 QuicPacketNumber packet_number_;
vasilvvc48c8712019-03-11 13:38:16 -0700129 std::string associated_data_;
130 std::string plaintext_;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500131};
132
133class TestDecrypter : public QuicDecrypter {
134 public:
135 ~TestDecrypter() override {}
dschinazi17d42422019-06-18 16:35:07 -0700136 bool SetKey(QuicStringPiece /*key*/) override { return true; }
137 bool SetNoncePrefix(QuicStringPiece /*nonce_prefix*/) override {
138 return true;
139 }
140 bool SetIV(QuicStringPiece /*iv*/) override { return true; }
141 bool SetHeaderProtectionKey(QuicStringPiece /*key*/) override { return true; }
142 bool SetPreliminaryKey(QuicStringPiece /*key*/) override {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500143 QUIC_BUG << "should not be called";
144 return false;
145 }
dschinazi17d42422019-06-18 16:35:07 -0700146 bool SetDiversificationNonce(const DiversificationNonce& /*key*/) override {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500147 return true;
148 }
149 bool DecryptPacket(uint64_t packet_number,
150 QuicStringPiece associated_data,
151 QuicStringPiece ciphertext,
152 char* output,
153 size_t* output_length,
dschinazi17d42422019-06-18 16:35:07 -0700154 size_t /*max_output_length*/) override {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500155 packet_number_ = QuicPacketNumber(packet_number);
vasilvvc48c8712019-03-11 13:38:16 -0700156 associated_data_ = std::string(associated_data);
157 ciphertext_ = std::string(ciphertext);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500158 memcpy(output, ciphertext.data(), ciphertext.length());
159 *output_length = ciphertext.length();
160 return true;
161 }
QUICHE team2d187972019-03-19 16:23:47 -0700162 std::string GenerateHeaderProtectionMask(
dschinazi17d42422019-06-18 16:35:07 -0700163 QuicDataReader* /*sample_reader*/) override {
QUICHE team2d187972019-03-19 16:23:47 -0700164 return std::string(5, 0);
165 }
QUICHE teama6ef0a62019-03-07 20:34:33 -0500166 size_t GetKeySize() const override { return 0; }
167 size_t GetIVSize() const override { return 0; }
168 QuicStringPiece GetKey() const override { return QuicStringPiece(); }
169 QuicStringPiece GetNoncePrefix() const override { return QuicStringPiece(); }
170 // Use a distinct value starting with 0xFFFFFF, which is never used by TLS.
171 uint32_t cipher_id() const override { return 0xFFFFFFF2; }
172 QuicPacketNumber packet_number_;
vasilvvc48c8712019-03-11 13:38:16 -0700173 std::string associated_data_;
174 std::string ciphertext_;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500175};
176
177class TestQuicVisitor : public QuicFramerVisitorInterface {
178 public:
179 TestQuicVisitor()
180 : error_count_(0),
181 version_mismatch_(0),
182 packet_count_(0),
183 frame_count_(0),
184 complete_packets_(0),
185 accept_packet_(true),
186 accept_public_header_(true) {}
187
188 ~TestQuicVisitor() override {}
189
190 void OnError(QuicFramer* f) override {
191 QUIC_DLOG(INFO) << "QuicFramer Error: " << QuicErrorCodeToString(f->error())
192 << " (" << f->error() << ")";
193 ++error_count_;
194 }
195
196 void OnPacket() override {}
197
198 void OnPublicResetPacket(const QuicPublicResetPacket& packet) override {
vasilvv0fc587f2019-09-06 13:33:08 -0700199 public_reset_packet_ = std::make_unique<QuicPublicResetPacket>((packet));
fkastenholza3660102019-08-28 05:19:24 -0700200 EXPECT_EQ(0u, framer_->current_received_frame_type());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500201 }
202
203 void OnVersionNegotiationPacket(
204 const QuicVersionNegotiationPacket& packet) override {
205 version_negotiation_packet_ =
vasilvv0fc587f2019-09-06 13:33:08 -0700206 std::make_unique<QuicVersionNegotiationPacket>((packet));
fkastenholza3660102019-08-28 05:19:24 -0700207 EXPECT_EQ(0u, framer_->current_received_frame_type());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500208 }
209
dschinazi244f6dc2019-05-06 15:45:16 -0700210 void OnRetryPacket(QuicConnectionId original_connection_id,
211 QuicConnectionId new_connection_id,
212 QuicStringPiece retry_token) override {
213 retry_original_connection_id_ =
vasilvv0fc587f2019-09-06 13:33:08 -0700214 std::make_unique<QuicConnectionId>(original_connection_id);
dschinazi244f6dc2019-05-06 15:45:16 -0700215 retry_new_connection_id_ =
vasilvv0fc587f2019-09-06 13:33:08 -0700216 std::make_unique<QuicConnectionId>(new_connection_id);
217 retry_token_ = std::make_unique<std::string>(std::string(retry_token));
fkastenholza3660102019-08-28 05:19:24 -0700218 EXPECT_EQ(0u, framer_->current_received_frame_type());
dschinazi244f6dc2019-05-06 15:45:16 -0700219 }
220
fayang8aba1ff2019-06-21 12:00:54 -0700221 bool OnProtocolVersionMismatch(ParsedQuicVersion received_version) override {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500222 QUIC_DLOG(INFO) << "QuicFramer Version Mismatch, version: "
223 << received_version;
224 ++version_mismatch_;
fkastenholza3660102019-08-28 05:19:24 -0700225 EXPECT_EQ(0u, framer_->current_received_frame_type());
dschinazi48ac9192019-07-31 00:07:26 -0700226 return false;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500227 }
228
229 bool OnUnauthenticatedPublicHeader(const QuicPacketHeader& header) override {
vasilvv0fc587f2019-09-06 13:33:08 -0700230 header_ = std::make_unique<QuicPacketHeader>((header));
fkastenholza3660102019-08-28 05:19:24 -0700231 EXPECT_EQ(0u, framer_->current_received_frame_type());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500232 return accept_public_header_;
233 }
234
dschinazi17d42422019-06-18 16:35:07 -0700235 bool OnUnauthenticatedHeader(const QuicPacketHeader& /*header*/) override {
fkastenholza3660102019-08-28 05:19:24 -0700236 EXPECT_EQ(0u, framer_->current_received_frame_type());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500237 return true;
238 }
239
fkastenholza3660102019-08-28 05:19:24 -0700240 void OnDecryptedPacket(EncryptionLevel /*level*/) override {
241 EXPECT_EQ(0u, framer_->current_received_frame_type());
242 }
QUICHE teama6ef0a62019-03-07 20:34:33 -0500243
244 bool OnPacketHeader(const QuicPacketHeader& header) override {
245 ++packet_count_;
vasilvv0fc587f2019-09-06 13:33:08 -0700246 header_ = std::make_unique<QuicPacketHeader>((header));
fkastenholza3660102019-08-28 05:19:24 -0700247 EXPECT_EQ(0u, framer_->current_received_frame_type());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500248 return accept_packet_;
249 }
250
251 void OnCoalescedPacket(const QuicEncryptedPacket& packet) override {
dschinazi4b5a68a2019-08-15 15:45:36 -0700252 coalesced_packets_.push_back(packet.Clone());
253 }
254
255 void OnUndecryptablePacket(const QuicEncryptedPacket& packet,
256 EncryptionLevel decryption_level,
257 bool has_decryption_key) override {
258 undecryptable_packets_.push_back(packet.Clone());
259 undecryptable_decryption_levels_.push_back(decryption_level);
260 undecryptable_has_decryption_keys_.push_back(has_decryption_key);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500261 }
262
263 bool OnStreamFrame(const QuicStreamFrame& frame) override {
264 ++frame_count_;
265 // Save a copy of the data so it is valid after the packet is processed.
vasilvvc48c8712019-03-11 13:38:16 -0700266 std::string* string_data =
267 new std::string(frame.data_buffer, frame.data_length);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500268 stream_data_.push_back(QuicWrapUnique(string_data));
vasilvv0fc587f2019-09-06 13:33:08 -0700269 stream_frames_.push_back(std::make_unique<QuicStreamFrame>(
QUICHE teama6ef0a62019-03-07 20:34:33 -0500270 frame.stream_id, frame.fin, frame.offset, *string_data));
fkastenholza3660102019-08-28 05:19:24 -0700271 if (VersionHasIetfQuicFrames(transport_version_)) {
272 // Low order bits of type encode flags, ignore them for this test.
273 EXPECT_TRUE(IS_IETF_STREAM_FRAME(framer_->current_received_frame_type()));
274 } else {
275 EXPECT_EQ(0u, framer_->current_received_frame_type());
276 }
QUICHE teama6ef0a62019-03-07 20:34:33 -0500277 return true;
278 }
279
280 bool OnCryptoFrame(const QuicCryptoFrame& frame) override {
281 ++frame_count_;
282 // Save a copy of the data so it is valid after the packet is processed.
vasilvvc48c8712019-03-11 13:38:16 -0700283 std::string* string_data =
284 new std::string(frame.data_buffer, frame.data_length);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500285 crypto_data_.push_back(QuicWrapUnique(string_data));
vasilvv0fc587f2019-09-06 13:33:08 -0700286 crypto_frames_.push_back(std::make_unique<QuicCryptoFrame>(
QUICHE team6987b4a2019-03-15 16:23:04 -0700287 ENCRYPTION_INITIAL, frame.offset, *string_data));
fkastenholza3660102019-08-28 05:19:24 -0700288 if (VersionHasIetfQuicFrames(transport_version_)) {
289 EXPECT_EQ(IETF_CRYPTO, framer_->current_received_frame_type());
290 } else {
291 EXPECT_EQ(0u, framer_->current_received_frame_type());
292 }
QUICHE teama6ef0a62019-03-07 20:34:33 -0500293 return true;
294 }
295
296 bool OnAckFrameStart(QuicPacketNumber largest_acked,
297 QuicTime::Delta ack_delay_time) override {
298 ++frame_count_;
299 QuicAckFrame ack_frame;
300 ack_frame.largest_acked = largest_acked;
301 ack_frame.ack_delay_time = ack_delay_time;
vasilvv0fc587f2019-09-06 13:33:08 -0700302 ack_frames_.push_back(std::make_unique<QuicAckFrame>(ack_frame));
fkastenholza3660102019-08-28 05:19:24 -0700303 if (VersionHasIetfQuicFrames(transport_version_)) {
304 EXPECT_EQ(IETF_ACK, framer_->current_received_frame_type());
305 } else {
306 EXPECT_EQ(0u, framer_->current_received_frame_type());
307 }
QUICHE teama6ef0a62019-03-07 20:34:33 -0500308 return true;
309 }
310
311 bool OnAckRange(QuicPacketNumber start, QuicPacketNumber end) override {
312 DCHECK(!ack_frames_.empty());
313 ack_frames_[ack_frames_.size() - 1]->packets.AddRange(start, end);
fkastenholza3660102019-08-28 05:19:24 -0700314 if (VersionHasIetfQuicFrames(transport_version_)) {
315 EXPECT_EQ(IETF_ACK, framer_->current_received_frame_type());
316 } else {
317 EXPECT_EQ(0u, framer_->current_received_frame_type());
318 }
QUICHE teama6ef0a62019-03-07 20:34:33 -0500319 return true;
320 }
321
322 bool OnAckTimestamp(QuicPacketNumber packet_number,
323 QuicTime timestamp) override {
324 ack_frames_[ack_frames_.size() - 1]->received_packet_times.push_back(
325 std::make_pair(packet_number, timestamp));
fkastenholza3660102019-08-28 05:19:24 -0700326 EXPECT_EQ(0u, framer_->current_received_frame_type());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500327 return true;
328 }
329
330 bool OnAckFrameEnd(QuicPacketNumber /*start*/) override { return true; }
331
332 bool OnStopWaitingFrame(const QuicStopWaitingFrame& frame) override {
333 ++frame_count_;
vasilvv0fc587f2019-09-06 13:33:08 -0700334 stop_waiting_frames_.push_back(
335 std::make_unique<QuicStopWaitingFrame>(frame));
fkastenholza3660102019-08-28 05:19:24 -0700336 EXPECT_EQ(0u, framer_->current_received_frame_type());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500337 return true;
338 }
339
340 bool OnPaddingFrame(const QuicPaddingFrame& frame) override {
vasilvv0fc587f2019-09-06 13:33:08 -0700341 padding_frames_.push_back(std::make_unique<QuicPaddingFrame>(frame));
fkastenholza3660102019-08-28 05:19:24 -0700342 if (VersionHasIetfQuicFrames(transport_version_)) {
343 EXPECT_EQ(IETF_PADDING, framer_->current_received_frame_type());
344 } else {
345 EXPECT_EQ(0u, framer_->current_received_frame_type());
346 }
QUICHE teama6ef0a62019-03-07 20:34:33 -0500347 return true;
348 }
349
350 bool OnPingFrame(const QuicPingFrame& frame) override {
351 ++frame_count_;
vasilvv0fc587f2019-09-06 13:33:08 -0700352 ping_frames_.push_back(std::make_unique<QuicPingFrame>(frame));
fkastenholza3660102019-08-28 05:19:24 -0700353 if (VersionHasIetfQuicFrames(transport_version_)) {
354 EXPECT_EQ(IETF_PING, framer_->current_received_frame_type());
355 } else {
356 EXPECT_EQ(0u, framer_->current_received_frame_type());
357 }
QUICHE teama6ef0a62019-03-07 20:34:33 -0500358 return true;
359 }
360
361 bool OnMessageFrame(const QuicMessageFrame& frame) override {
362 ++frame_count_;
363 message_frames_.push_back(
vasilvv0fc587f2019-09-06 13:33:08 -0700364 std::make_unique<QuicMessageFrame>(frame.data, frame.message_length));
fkastenholza3660102019-08-28 05:19:24 -0700365 if (VersionHasIetfQuicFrames(transport_version_)) {
366 EXPECT_TRUE(IETF_EXTENSION_MESSAGE_NO_LENGTH ==
367 framer_->current_received_frame_type() ||
368 IETF_EXTENSION_MESSAGE ==
369 framer_->current_received_frame_type());
370 } else {
371 EXPECT_EQ(0u, framer_->current_received_frame_type());
372 }
QUICHE teama6ef0a62019-03-07 20:34:33 -0500373 return true;
374 }
375
376 void OnPacketComplete() override { ++complete_packets_; }
377
378 bool OnRstStreamFrame(const QuicRstStreamFrame& frame) override {
379 rst_stream_frame_ = frame;
fkastenholza3660102019-08-28 05:19:24 -0700380 if (VersionHasIetfQuicFrames(transport_version_)) {
381 EXPECT_EQ(IETF_RST_STREAM, framer_->current_received_frame_type());
382 } else {
383 EXPECT_EQ(0u, framer_->current_received_frame_type());
384 }
QUICHE teama6ef0a62019-03-07 20:34:33 -0500385 return true;
386 }
387
388 bool OnConnectionCloseFrame(const QuicConnectionCloseFrame& frame) override {
389 connection_close_frame_ = frame;
fkastenholza3660102019-08-28 05:19:24 -0700390 if (VersionHasIetfQuicFrames(transport_version_)) {
391 EXPECT_NE(GOOGLE_QUIC_CONNECTION_CLOSE, frame.close_type);
392 if (frame.close_type == IETF_QUIC_TRANSPORT_CONNECTION_CLOSE) {
393 EXPECT_EQ(IETF_CONNECTION_CLOSE,
394 framer_->current_received_frame_type());
395 } else {
396 EXPECT_EQ(IETF_APPLICATION_CLOSE,
397 framer_->current_received_frame_type());
398 }
399 } else {
400 EXPECT_EQ(0u, framer_->current_received_frame_type());
401 }
QUICHE teama6ef0a62019-03-07 20:34:33 -0500402 return true;
403 }
404
QUICHE teama6ef0a62019-03-07 20:34:33 -0500405 bool OnStopSendingFrame(const QuicStopSendingFrame& frame) override {
406 stop_sending_frame_ = frame;
fkastenholza3660102019-08-28 05:19:24 -0700407 EXPECT_EQ(IETF_STOP_SENDING, framer_->current_received_frame_type());
408 EXPECT_TRUE(VersionHasIetfQuicFrames(transport_version_));
QUICHE teama6ef0a62019-03-07 20:34:33 -0500409 return true;
410 }
411
412 bool OnPathChallengeFrame(const QuicPathChallengeFrame& frame) override {
413 path_challenge_frame_ = frame;
fkastenholza3660102019-08-28 05:19:24 -0700414 EXPECT_EQ(IETF_PATH_CHALLENGE, framer_->current_received_frame_type());
415 EXPECT_TRUE(VersionHasIetfQuicFrames(transport_version_));
QUICHE teama6ef0a62019-03-07 20:34:33 -0500416 return true;
417 }
418
419 bool OnPathResponseFrame(const QuicPathResponseFrame& frame) override {
420 path_response_frame_ = frame;
fkastenholza3660102019-08-28 05:19:24 -0700421 EXPECT_EQ(IETF_PATH_RESPONSE, framer_->current_received_frame_type());
422 EXPECT_TRUE(VersionHasIetfQuicFrames(transport_version_));
QUICHE teama6ef0a62019-03-07 20:34:33 -0500423 return true;
424 }
425
426 bool OnGoAwayFrame(const QuicGoAwayFrame& frame) override {
427 goaway_frame_ = frame;
fkastenholza3660102019-08-28 05:19:24 -0700428 EXPECT_FALSE(VersionHasIetfQuicFrames(transport_version_));
429 EXPECT_EQ(0u, framer_->current_received_frame_type());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500430 return true;
431 }
432
fkastenholz3c4eabf2019-04-22 07:49:59 -0700433 bool OnMaxStreamsFrame(const QuicMaxStreamsFrame& frame) override {
434 max_streams_frame_ = frame;
fkastenholza3660102019-08-28 05:19:24 -0700435 EXPECT_TRUE(VersionHasIetfQuicFrames(transport_version_));
436 EXPECT_TRUE(IETF_MAX_STREAMS_UNIDIRECTIONAL ==
437 framer_->current_received_frame_type() ||
438 IETF_MAX_STREAMS_BIDIRECTIONAL ==
439 framer_->current_received_frame_type());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500440 return true;
441 }
442
fkastenholz3c4eabf2019-04-22 07:49:59 -0700443 bool OnStreamsBlockedFrame(const QuicStreamsBlockedFrame& frame) override {
444 streams_blocked_frame_ = frame;
fkastenholza3660102019-08-28 05:19:24 -0700445 EXPECT_TRUE(VersionHasIetfQuicFrames(transport_version_));
446 EXPECT_TRUE(IETF_STREAMS_BLOCKED_UNIDIRECTIONAL ==
447 framer_->current_received_frame_type() ||
448 IETF_STREAMS_BLOCKED_BIDIRECTIONAL ==
449 framer_->current_received_frame_type());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500450 return true;
451 }
452
453 bool OnWindowUpdateFrame(const QuicWindowUpdateFrame& frame) override {
454 window_update_frame_ = frame;
fkastenholza3660102019-08-28 05:19:24 -0700455 if (VersionHasIetfQuicFrames(transport_version_)) {
456 EXPECT_TRUE(IETF_MAX_DATA == framer_->current_received_frame_type() ||
457 IETF_MAX_STREAM_DATA ==
458 framer_->current_received_frame_type());
459 } else {
460 EXPECT_EQ(0u, framer_->current_received_frame_type());
461 }
QUICHE teama6ef0a62019-03-07 20:34:33 -0500462 return true;
463 }
464
465 bool OnBlockedFrame(const QuicBlockedFrame& frame) override {
466 blocked_frame_ = frame;
fkastenholza3660102019-08-28 05:19:24 -0700467 if (VersionHasIetfQuicFrames(transport_version_)) {
468 EXPECT_TRUE(IETF_BLOCKED == framer_->current_received_frame_type() ||
469 IETF_STREAM_BLOCKED ==
470 framer_->current_received_frame_type());
471 } else {
472 EXPECT_EQ(0u, framer_->current_received_frame_type());
473 }
QUICHE teama6ef0a62019-03-07 20:34:33 -0500474 return true;
475 }
476
477 bool OnNewConnectionIdFrame(const QuicNewConnectionIdFrame& frame) override {
478 new_connection_id_ = frame;
fkastenholza3660102019-08-28 05:19:24 -0700479 EXPECT_EQ(IETF_NEW_CONNECTION_ID, framer_->current_received_frame_type());
480 EXPECT_TRUE(VersionHasIetfQuicFrames(transport_version_));
QUICHE teama6ef0a62019-03-07 20:34:33 -0500481 return true;
482 }
483
484 bool OnRetireConnectionIdFrame(
485 const QuicRetireConnectionIdFrame& frame) override {
fkastenholza3660102019-08-28 05:19:24 -0700486 EXPECT_EQ(IETF_RETIRE_CONNECTION_ID,
487 framer_->current_received_frame_type());
488 EXPECT_TRUE(VersionHasIetfQuicFrames(transport_version_));
QUICHE teama6ef0a62019-03-07 20:34:33 -0500489 retire_connection_id_ = frame;
490 return true;
491 }
492
493 bool OnNewTokenFrame(const QuicNewTokenFrame& frame) override {
494 new_token_ = frame;
fkastenholza3660102019-08-28 05:19:24 -0700495 EXPECT_EQ(IETF_NEW_TOKEN, framer_->current_received_frame_type());
496 EXPECT_TRUE(VersionHasIetfQuicFrames(transport_version_));
QUICHE teama6ef0a62019-03-07 20:34:33 -0500497 return true;
498 }
499
500 bool IsValidStatelessResetToken(QuicUint128 token) const override {
fkastenholza3660102019-08-28 05:19:24 -0700501 EXPECT_EQ(0u, framer_->current_received_frame_type());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500502 return token == kTestStatelessResetToken;
503 }
504
505 void OnAuthenticatedIetfStatelessResetPacket(
506 const QuicIetfStatelessResetPacket& packet) override {
507 stateless_reset_packet_ =
vasilvv0fc587f2019-09-06 13:33:08 -0700508 std::make_unique<QuicIetfStatelessResetPacket>(packet);
fkastenholza3660102019-08-28 05:19:24 -0700509 EXPECT_EQ(0u, framer_->current_received_frame_type());
510 }
511
512 void set_framer(QuicFramer* framer) {
513 framer_ = framer;
514 transport_version_ = framer->transport_version();
QUICHE teama6ef0a62019-03-07 20:34:33 -0500515 }
516
517 // Counters from the visitor_ callbacks.
518 int error_count_;
519 int version_mismatch_;
520 int packet_count_;
521 int frame_count_;
522 int complete_packets_;
523 bool accept_packet_;
524 bool accept_public_header_;
525
526 std::unique_ptr<QuicPacketHeader> header_;
527 std::unique_ptr<QuicPublicResetPacket> public_reset_packet_;
528 std::unique_ptr<QuicIetfStatelessResetPacket> stateless_reset_packet_;
529 std::unique_ptr<QuicVersionNegotiationPacket> version_negotiation_packet_;
dschinazi244f6dc2019-05-06 15:45:16 -0700530 std::unique_ptr<QuicConnectionId> retry_original_connection_id_;
531 std::unique_ptr<QuicConnectionId> retry_new_connection_id_;
532 std::unique_ptr<std::string> retry_token_;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500533 std::vector<std::unique_ptr<QuicStreamFrame>> stream_frames_;
534 std::vector<std::unique_ptr<QuicCryptoFrame>> crypto_frames_;
535 std::vector<std::unique_ptr<QuicAckFrame>> ack_frames_;
536 std::vector<std::unique_ptr<QuicStopWaitingFrame>> stop_waiting_frames_;
537 std::vector<std::unique_ptr<QuicPaddingFrame>> padding_frames_;
538 std::vector<std::unique_ptr<QuicPingFrame>> ping_frames_;
539 std::vector<std::unique_ptr<QuicMessageFrame>> message_frames_;
540 std::vector<std::unique_ptr<QuicEncryptedPacket>> coalesced_packets_;
dschinazi4b5a68a2019-08-15 15:45:36 -0700541 std::vector<std::unique_ptr<QuicEncryptedPacket>> undecryptable_packets_;
542 std::vector<EncryptionLevel> undecryptable_decryption_levels_;
543 std::vector<bool> undecryptable_has_decryption_keys_;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500544 QuicRstStreamFrame rst_stream_frame_;
545 QuicConnectionCloseFrame connection_close_frame_;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500546 QuicStopSendingFrame stop_sending_frame_;
547 QuicGoAwayFrame goaway_frame_;
548 QuicPathChallengeFrame path_challenge_frame_;
549 QuicPathResponseFrame path_response_frame_;
550 QuicWindowUpdateFrame window_update_frame_;
551 QuicBlockedFrame blocked_frame_;
fkastenholz3c4eabf2019-04-22 07:49:59 -0700552 QuicStreamsBlockedFrame streams_blocked_frame_;
553 QuicMaxStreamsFrame max_streams_frame_;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500554 QuicNewConnectionIdFrame new_connection_id_;
555 QuicRetireConnectionIdFrame retire_connection_id_;
556 QuicNewTokenFrame new_token_;
vasilvvc48c8712019-03-11 13:38:16 -0700557 std::vector<std::unique_ptr<std::string>> stream_data_;
558 std::vector<std::unique_ptr<std::string>> crypto_data_;
fkastenholza3660102019-08-28 05:19:24 -0700559 QuicTransportVersion transport_version_;
560 QuicFramer* framer_;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500561};
562
563// Simple struct for defining a packet's content, and associated
564// parse error.
565struct PacketFragment {
vasilvvc48c8712019-03-11 13:38:16 -0700566 std::string error_if_missing;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500567 std::vector<unsigned char> fragment;
568};
569
570using PacketFragments = std::vector<struct PacketFragment>;
571
QUICHE teama6ef0a62019-03-07 20:34:33 -0500572class QuicFramerTest : public QuicTestWithParam<ParsedQuicVersion> {
573 public:
574 QuicFramerTest()
575 : encrypter_(new test::TestEncrypter()),
576 decrypter_(new test::TestDecrypter()),
577 version_(GetParam()),
578 start_(QuicTime::Zero() + QuicTime::Delta::FromMicroseconds(0x10)),
nharperf5e68452019-05-29 17:24:18 -0700579 framer_(AllSupportedVersions(),
QUICHE teama6ef0a62019-03-07 20:34:33 -0500580 start_,
581 Perspective::IS_SERVER,
582 kQuicDefaultConnectionIdLength) {
rch16df2a52019-09-10 10:50:09 -0700583 SetQuicReloadableFlag(quic_supports_tls_handshake, true);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500584 framer_.set_version(version_);
zhongyi546cc452019-04-12 15:27:49 -0700585 if (framer_.version().KnowsWhichDecrypterToUse()) {
586 framer_.InstallDecrypter(ENCRYPTION_INITIAL,
587 std::unique_ptr<QuicDecrypter>(decrypter_));
588 } else {
589 framer_.SetDecrypter(ENCRYPTION_INITIAL,
590 std::unique_ptr<QuicDecrypter>(decrypter_));
591 }
QUICHE team6987b4a2019-03-15 16:23:04 -0700592 framer_.SetEncrypter(ENCRYPTION_INITIAL,
QUICHE teama6ef0a62019-03-07 20:34:33 -0500593 std::unique_ptr<QuicEncrypter>(encrypter_));
594
595 framer_.set_visitor(&visitor_);
596 framer_.InferPacketHeaderTypeFromVersion();
fkastenholza3660102019-08-28 05:19:24 -0700597 visitor_.set_framer(&framer_);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500598 }
599
zhongyi546cc452019-04-12 15:27:49 -0700600 void SetDecrypterLevel(EncryptionLevel level) {
601 if (!framer_.version().KnowsWhichDecrypterToUse()) {
602 return;
603 }
604 decrypter_ = new TestDecrypter();
605 framer_.InstallDecrypter(level, std::unique_ptr<QuicDecrypter>(decrypter_));
606 }
607
QUICHE teama6ef0a62019-03-07 20:34:33 -0500608 // Helper function to get unsigned char representation of the handshake
rcha702be22019-08-30 15:20:12 -0700609 // protocol byte at position |pos| of the current QUIC version number.
610 unsigned char GetQuicVersionByte(int pos) {
611 return (CreateQuicVersionLabel(version_) >> 8 * (3 - pos)) & 0xff;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500612 }
613
614 bool CheckEncryption(QuicPacketNumber packet_number, QuicPacket* packet) {
615 if (packet_number != encrypter_->packet_number_) {
616 QUIC_LOG(ERROR) << "Encrypted incorrect packet number. expected "
617 << packet_number
618 << " actual: " << encrypter_->packet_number_;
619 return false;
620 }
621 if (packet->AssociatedData(framer_.transport_version()) !=
622 encrypter_->associated_data_) {
623 QUIC_LOG(ERROR) << "Encrypted incorrect associated data. expected "
624 << packet->AssociatedData(framer_.transport_version())
625 << " actual: " << encrypter_->associated_data_;
626 return false;
627 }
628 if (packet->Plaintext(framer_.transport_version()) !=
629 encrypter_->plaintext_) {
630 QUIC_LOG(ERROR) << "Encrypted incorrect plaintext data. expected "
631 << packet->Plaintext(framer_.transport_version())
632 << " actual: " << encrypter_->plaintext_;
633 return false;
634 }
635 return true;
636 }
637
638 bool CheckDecryption(const QuicEncryptedPacket& encrypted,
639 bool includes_version,
640 bool includes_diversification_nonce,
641 QuicConnectionIdLength destination_connection_id_length,
642 QuicConnectionIdLength source_connection_id_length) {
643 return CheckDecryption(
644 encrypted, includes_version, includes_diversification_nonce,
645 destination_connection_id_length, source_connection_id_length,
646 VARIABLE_LENGTH_INTEGER_LENGTH_0, 0, VARIABLE_LENGTH_INTEGER_LENGTH_0);
647 }
648
649 bool CheckDecryption(
650 const QuicEncryptedPacket& encrypted,
651 bool includes_version,
652 bool includes_diversification_nonce,
653 QuicConnectionIdLength destination_connection_id_length,
654 QuicConnectionIdLength source_connection_id_length,
655 QuicVariableLengthIntegerLength retry_token_length_length,
656 size_t retry_token_length,
657 QuicVariableLengthIntegerLength length_length) {
658 if (visitor_.header_->packet_number != decrypter_->packet_number_) {
659 QUIC_LOG(ERROR) << "Decrypted incorrect packet number. expected "
660 << visitor_.header_->packet_number
661 << " actual: " << decrypter_->packet_number_;
662 return false;
663 }
664 QuicStringPiece associated_data =
665 QuicFramer::GetAssociatedDataFromEncryptedPacket(
666 framer_.transport_version(), encrypted,
667 destination_connection_id_length, source_connection_id_length,
668 includes_version, includes_diversification_nonce,
669 PACKET_4BYTE_PACKET_NUMBER, retry_token_length_length,
670 retry_token_length, length_length);
671 if (associated_data != decrypter_->associated_data_) {
672 QUIC_LOG(ERROR) << "Decrypted incorrect associated data. expected "
673 << QuicTextUtils::HexEncode(associated_data)
674 << " actual: "
675 << QuicTextUtils::HexEncode(decrypter_->associated_data_);
676 return false;
677 }
678 QuicStringPiece ciphertext(
679 encrypted.AsStringPiece().substr(GetStartOfEncryptedData(
680 framer_.transport_version(), destination_connection_id_length,
681 source_connection_id_length, includes_version,
682 includes_diversification_nonce, PACKET_4BYTE_PACKET_NUMBER,
683 retry_token_length_length, retry_token_length, length_length)));
684 if (ciphertext != decrypter_->ciphertext_) {
685 QUIC_LOG(ERROR) << "Decrypted incorrect ciphertext data. expected "
686 << QuicTextUtils::HexEncode(ciphertext) << " actual: "
687 << QuicTextUtils::HexEncode(decrypter_->ciphertext_)
688 << " associated data: "
689 << QuicTextUtils::HexEncode(associated_data);
690 return false;
691 }
692 return true;
693 }
694
695 char* AsChars(unsigned char* data) { return reinterpret_cast<char*>(data); }
696
697 // Creates a new QuicEncryptedPacket by concatenating the various
698 // packet fragments in |fragments|.
699 std::unique_ptr<QuicEncryptedPacket> AssemblePacketFromFragments(
700 const PacketFragments& fragments) {
dschinazi66dea072019-04-09 11:41:06 -0700701 char* buffer = new char[kMaxOutgoingPacketSize + 1];
QUICHE teama6ef0a62019-03-07 20:34:33 -0500702 size_t len = 0;
703 for (const auto& fragment : fragments) {
704 memcpy(buffer + len, fragment.fragment.data(), fragment.fragment.size());
705 len += fragment.fragment.size();
706 }
vasilvv0fc587f2019-09-06 13:33:08 -0700707 return std::make_unique<QuicEncryptedPacket>(buffer, len, true);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500708 }
709
710 void CheckFramingBoundaries(const PacketFragments& fragments,
711 QuicErrorCode error_code) {
712 std::unique_ptr<QuicEncryptedPacket> packet(
713 AssemblePacketFromFragments(fragments));
714 // Check all the various prefixes of |packet| for the expected
715 // parse error and error code.
716 for (size_t i = 0; i < packet->length(); ++i) {
vasilvvc48c8712019-03-11 13:38:16 -0700717 std::string expected_error;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500718 size_t len = 0;
719 for (const auto& fragment : fragments) {
720 len += fragment.fragment.size();
721 if (i < len) {
722 expected_error = fragment.error_if_missing;
723 break;
724 }
725 }
726
727 if (expected_error.empty())
728 continue;
729
730 CheckProcessingFails(*packet, i, expected_error, error_code);
731 }
732 }
733
734 void CheckProcessingFails(const QuicEncryptedPacket& packet,
735 size_t len,
vasilvvc48c8712019-03-11 13:38:16 -0700736 std::string expected_error,
QUICHE teama6ef0a62019-03-07 20:34:33 -0500737 QuicErrorCode error_code) {
738 QuicEncryptedPacket encrypted(packet.data(), len, false);
739 EXPECT_FALSE(framer_.ProcessPacket(encrypted)) << "len: " << len;
740 EXPECT_EQ(expected_error, framer_.detailed_error()) << "len: " << len;
741 EXPECT_EQ(error_code, framer_.error()) << "len: " << len;
742 }
743
744 void CheckProcessingFails(unsigned char* packet,
745 size_t len,
vasilvvc48c8712019-03-11 13:38:16 -0700746 std::string expected_error,
QUICHE teama6ef0a62019-03-07 20:34:33 -0500747 QuicErrorCode error_code) {
748 QuicEncryptedPacket encrypted(AsChars(packet), len, false);
749 EXPECT_FALSE(framer_.ProcessPacket(encrypted)) << "len: " << len;
750 EXPECT_EQ(expected_error, framer_.detailed_error()) << "len: " << len;
751 EXPECT_EQ(error_code, framer_.error()) << "len: " << len;
752 }
753
754 // Checks if the supplied string matches data in the supplied StreamFrame.
vasilvvc48c8712019-03-11 13:38:16 -0700755 void CheckStreamFrameData(std::string str, QuicStreamFrame* frame) {
756 EXPECT_EQ(str, std::string(frame->data_buffer, frame->data_length));
QUICHE teama6ef0a62019-03-07 20:34:33 -0500757 }
758
759 void CheckCalculatePacketNumber(uint64_t expected_packet_number,
760 QuicPacketNumber last_packet_number) {
761 uint64_t wire_packet_number = expected_packet_number & kMask;
762 EXPECT_EQ(expected_packet_number,
763 QuicFramerPeer::CalculatePacketNumberFromWire(
764 &framer_, PACKET_4BYTE_PACKET_NUMBER, last_packet_number,
765 wire_packet_number))
766 << "last_packet_number: " << last_packet_number
767 << " wire_packet_number: " << wire_packet_number;
768 }
769
770 std::unique_ptr<QuicPacket> BuildDataPacket(const QuicPacketHeader& header,
771 const QuicFrames& frames) {
772 return BuildUnsizedDataPacket(&framer_, header, frames);
773 }
774
775 std::unique_ptr<QuicPacket> BuildDataPacket(const QuicPacketHeader& header,
776 const QuicFrames& frames,
777 size_t packet_size) {
778 return BuildUnsizedDataPacket(&framer_, header, frames, packet_size);
779 }
780
781 // N starts at 1.
782 QuicStreamId GetNthStreamid(QuicTransportVersion transport_version,
783 Perspective perspective,
784 bool bidirectional,
785 int n) {
786 if (bidirectional) {
787 return QuicUtils::GetFirstBidirectionalStreamId(transport_version,
788 perspective) +
789 ((n - 1) * QuicUtils::StreamIdDelta(transport_version));
790 }
791 // Unidirectional
792 return QuicUtils::GetFirstUnidirectionalStreamId(transport_version,
793 perspective) +
794 ((n - 1) * QuicUtils::StreamIdDelta(transport_version));
795 }
796
797 test::TestEncrypter* encrypter_;
798 test::TestDecrypter* decrypter_;
799 ParsedQuicVersion version_;
800 QuicTime start_;
801 QuicFramer framer_;
802 test::TestQuicVisitor visitor_;
803 SimpleBufferAllocator allocator_;
804};
805
806// Multiple test cases of QuicFramerTest use byte arrays to define packets for
807// testing, and these byte arrays contain the QUIC version. This macro explodes
808// the 32-bit version into four bytes in network order. Since it uses methods of
809// QuicFramerTest, it is only valid to use this in a QuicFramerTest.
rcha702be22019-08-30 15:20:12 -0700810#define QUIC_VERSION_BYTES \
811 GetQuicVersionByte(0), GetQuicVersionByte(1), GetQuicVersionByte(2), \
812 GetQuicVersionByte(3)
QUICHE teama6ef0a62019-03-07 20:34:33 -0500813
814// Run all framer tests with all supported versions of QUIC.
nharperf5e68452019-05-29 17:24:18 -0700815INSTANTIATE_TEST_SUITE_P(QuicFramerTests,
816 QuicFramerTest,
817 ::testing::ValuesIn(AllSupportedVersions()));
QUICHE teama6ef0a62019-03-07 20:34:33 -0500818
819TEST_P(QuicFramerTest, CalculatePacketNumberFromWireNearEpochStart) {
820 // A few quick manual sanity checks.
821 CheckCalculatePacketNumber(UINT64_C(1), QuicPacketNumber());
822 CheckCalculatePacketNumber(kEpoch + 1, QuicPacketNumber(kMask));
823 CheckCalculatePacketNumber(kEpoch, QuicPacketNumber(kMask));
824 for (uint64_t j = 0; j < 10; j++) {
825 CheckCalculatePacketNumber(j, QuicPacketNumber());
826 CheckCalculatePacketNumber(kEpoch - 1 - j, QuicPacketNumber());
827 }
828
829 // Cases where the last number was close to the start of the range.
830 for (QuicPacketNumber last = QuicPacketNumber(1); last < QuicPacketNumber(10);
831 last++) {
832 // Small numbers should not wrap (even if they're out of order).
833 for (uint64_t j = 0; j < 10; j++) {
834 CheckCalculatePacketNumber(j, last);
835 }
836
837 // Large numbers should not wrap either (because we're near 0 already).
838 for (uint64_t j = 0; j < 10; j++) {
839 CheckCalculatePacketNumber(kEpoch - 1 - j, last);
840 }
841 }
842}
843
844TEST_P(QuicFramerTest, CalculatePacketNumberFromWireNearEpochEnd) {
845 // Cases where the last number was close to the end of the range
846 for (uint64_t i = 0; i < 10; i++) {
847 QuicPacketNumber last = QuicPacketNumber(kEpoch - i);
848
849 // Small numbers should wrap.
850 for (uint64_t j = 0; j < 10; j++) {
851 CheckCalculatePacketNumber(kEpoch + j, last);
852 }
853
854 // Large numbers should not (even if they're out of order).
855 for (uint64_t j = 0; j < 10; j++) {
856 CheckCalculatePacketNumber(kEpoch - 1 - j, last);
857 }
858 }
859}
860
861// Next check where we're in a non-zero epoch to verify we handle
862// reverse wrapping, too.
863TEST_P(QuicFramerTest, CalculatePacketNumberFromWireNearPrevEpoch) {
864 const uint64_t prev_epoch = 1 * kEpoch;
865 const uint64_t cur_epoch = 2 * kEpoch;
866 // Cases where the last number was close to the start of the range
867 for (uint64_t i = 0; i < 10; i++) {
868 QuicPacketNumber last = QuicPacketNumber(cur_epoch + i);
869 // Small number should not wrap (even if they're out of order).
870 for (uint64_t j = 0; j < 10; j++) {
871 CheckCalculatePacketNumber(cur_epoch + j, last);
872 }
873
874 // But large numbers should reverse wrap.
875 for (uint64_t j = 0; j < 10; j++) {
876 uint64_t num = kEpoch - 1 - j;
877 CheckCalculatePacketNumber(prev_epoch + num, last);
878 }
879 }
880}
881
882TEST_P(QuicFramerTest, CalculatePacketNumberFromWireNearNextEpoch) {
883 const uint64_t cur_epoch = 2 * kEpoch;
884 const uint64_t next_epoch = 3 * kEpoch;
885 // Cases where the last number was close to the end of the range
886 for (uint64_t i = 0; i < 10; i++) {
887 QuicPacketNumber last = QuicPacketNumber(next_epoch - 1 - i);
888
889 // Small numbers should wrap.
890 for (uint64_t j = 0; j < 10; j++) {
891 CheckCalculatePacketNumber(next_epoch + j, last);
892 }
893
894 // but large numbers should not (even if they're out of order).
895 for (uint64_t j = 0; j < 10; j++) {
896 uint64_t num = kEpoch - 1 - j;
897 CheckCalculatePacketNumber(cur_epoch + num, last);
898 }
899 }
900}
901
902TEST_P(QuicFramerTest, CalculatePacketNumberFromWireNearNextMax) {
903 const uint64_t max_number = std::numeric_limits<uint64_t>::max();
904 const uint64_t max_epoch = max_number & ~kMask;
905
906 // Cases where the last number was close to the end of the range
907 for (uint64_t i = 0; i < 10; i++) {
908 // Subtract 1, because the expected next packet number is 1 more than the
909 // last packet number.
910 QuicPacketNumber last = QuicPacketNumber(max_number - i - 1);
911
912 // Small numbers should not wrap, because they have nowhere to go.
913 for (uint64_t j = 0; j < 10; j++) {
914 CheckCalculatePacketNumber(max_epoch + j, last);
915 }
916
917 // Large numbers should not wrap either.
918 for (uint64_t j = 0; j < 10; j++) {
919 uint64_t num = kEpoch - 1 - j;
920 CheckCalculatePacketNumber(max_epoch + num, last);
921 }
922 }
923}
924
925TEST_P(QuicFramerTest, EmptyPacket) {
926 char packet[] = {0x00};
927 QuicEncryptedPacket encrypted(packet, 0, false);
928 EXPECT_FALSE(framer_.ProcessPacket(encrypted));
929 EXPECT_EQ(QUIC_INVALID_PACKET_HEADER, framer_.error());
930}
931
932TEST_P(QuicFramerTest, LargePacket) {
zhongyi546cc452019-04-12 15:27:49 -0700933 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500934 // clang-format off
dschinazie8d7fa72019-04-05 14:44:40 -0700935 unsigned char packet[kMaxIncomingPacketSize + 1] = {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500936 // public flags (8 byte connection_id)
937 0x28,
938 // connection_id
939 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
940 // packet number
941 0x78, 0x56, 0x34, 0x12,
942 // private flags
943 0x00,
944 };
dschinazie8d7fa72019-04-05 14:44:40 -0700945 unsigned char packet46[kMaxIncomingPacketSize + 1] = {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500946 // type (short header 4 byte packet number)
947 0x43,
948 // connection_id
949 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
950 // packet number
951 0x78, 0x56, 0x34, 0x12,
952 };
953 // clang-format on
954 unsigned char* p = packet;
955 size_t p_size = QUIC_ARRAYSIZE(packet);
fayang36825da2019-08-21 14:01:27 -0700956 if (framer_.transport_version() > QUIC_VERSION_43) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500957 p = packet46;
958 p_size = QUIC_ARRAYSIZE(packet46);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500959 }
960
961 const size_t header_size = GetPacketHeaderSize(
962 framer_.transport_version(), PACKET_8BYTE_CONNECTION_ID,
963 PACKET_0BYTE_CONNECTION_ID, !kIncludeVersion,
964 !kIncludeDiversificationNonce, PACKET_4BYTE_PACKET_NUMBER,
965 VARIABLE_LENGTH_INTEGER_LENGTH_0, 0, VARIABLE_LENGTH_INTEGER_LENGTH_0);
966
dschinazie8d7fa72019-04-05 14:44:40 -0700967 memset(p + header_size, 0, kMaxIncomingPacketSize - header_size);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500968
969 QuicEncryptedPacket encrypted(AsChars(p), p_size, false);
dschinazie8d7fa72019-04-05 14:44:40 -0700970 EXPECT_FALSE(framer_.ProcessPacket(encrypted));
QUICHE teama6ef0a62019-03-07 20:34:33 -0500971
972 ASSERT_TRUE(visitor_.header_.get());
973 // Make sure we've parsed the packet header, so we can send an error.
974 EXPECT_EQ(FramerTestConnectionId(),
975 visitor_.header_->destination_connection_id);
976 // Make sure the correct error is propagated.
977 EXPECT_EQ(QUIC_PACKET_TOO_LARGE, framer_.error());
dschinazie8d7fa72019-04-05 14:44:40 -0700978 EXPECT_EQ("Packet too large.", framer_.detailed_error());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500979}
980
981TEST_P(QuicFramerTest, PacketHeader) {
982 if (framer_.transport_version() > QUIC_VERSION_43) {
983 return;
984 }
985
986 // clang-format off
987 PacketFragments packet = {
988 // public flags (8 byte connection_id)
989 {"Unable to read public flags.",
990 {0x28}},
991 // connection_id
992 {"Unable to read ConnectionId.",
993 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
994 // packet number
995 {"Unable to read packet number.",
996 {0x12, 0x34, 0x56, 0x78}},
997 };
998 // clang-format on
999
1000 PacketFragments& fragments = packet;
1001
1002 std::unique_ptr<QuicEncryptedPacket> encrypted(
1003 AssemblePacketFromFragments(fragments));
1004
1005 EXPECT_FALSE(framer_.ProcessPacket(*encrypted));
1006 EXPECT_EQ(QUIC_MISSING_PAYLOAD, framer_.error());
1007 ASSERT_TRUE(visitor_.header_.get());
1008 EXPECT_EQ(FramerTestConnectionId(),
1009 visitor_.header_->destination_connection_id);
1010 EXPECT_FALSE(visitor_.header_->reset_flag);
1011 EXPECT_FALSE(visitor_.header_->version_flag);
1012 EXPECT_EQ(kPacketNumber, visitor_.header_->packet_number);
1013
1014 CheckFramingBoundaries(fragments, QUIC_INVALID_PACKET_HEADER);
fayangccbab732019-05-13 10:11:25 -07001015
1016 PacketHeaderFormat format;
1017 bool version_flag;
dschinazib42a8c52019-05-30 09:45:01 -07001018 QuicConnectionId destination_connection_id, source_connection_id;
fayangccbab732019-05-13 10:11:25 -07001019 QuicVersionLabel version_label;
1020 std::string detailed_error;
dschinazi48ac9192019-07-31 00:07:26 -07001021 QuicErrorCode error_code;
1022 if (!GetQuicReloadableFlag(quic_use_parse_public_header)) {
1023 error_code = QuicFramer::ProcessPacketDispatcher(
1024 *encrypted, kQuicDefaultConnectionIdLength, &format, &version_flag,
1025 &version_label, &destination_connection_id, &source_connection_id,
1026 &detailed_error);
1027 } else {
1028 bool retry_token_present, use_length_prefix;
1029 QuicStringPiece retry_token;
1030 ParsedQuicVersion parsed_version = UnsupportedQuicVersion();
1031 error_code = QuicFramer::ParsePublicHeaderDispatcher(
1032 *encrypted, kQuicDefaultConnectionIdLength, &format, &version_flag,
1033 &use_length_prefix, &version_label, &parsed_version,
1034 &destination_connection_id, &source_connection_id, &retry_token_present,
1035 &retry_token, &detailed_error);
1036 EXPECT_FALSE(retry_token_present);
1037 EXPECT_FALSE(use_length_prefix);
1038 }
1039 EXPECT_EQ(QUIC_NO_ERROR, error_code);
fayangccbab732019-05-13 10:11:25 -07001040 EXPECT_EQ(GOOGLE_QUIC_PACKET, format);
1041 EXPECT_FALSE(version_flag);
dschinazib42a8c52019-05-30 09:45:01 -07001042 EXPECT_EQ(kQuicDefaultConnectionIdLength, destination_connection_id.length());
fayangccbab732019-05-13 10:11:25 -07001043 EXPECT_EQ(FramerTestConnectionId(), destination_connection_id);
dschinazib42a8c52019-05-30 09:45:01 -07001044 EXPECT_EQ(EmptyQuicConnectionId(), source_connection_id);
QUICHE teama6ef0a62019-03-07 20:34:33 -05001045}
1046
1047TEST_P(QuicFramerTest, LongPacketHeader) {
1048 // clang-format off
QUICHE teama6ef0a62019-03-07 20:34:33 -05001049 PacketFragments packet46 = {
1050 // type (long header with packet type INITIAL)
dschinazi48ac9192019-07-31 00:07:26 -07001051 {"Unable to read first byte.",
QUICHE teama6ef0a62019-03-07 20:34:33 -05001052 {0xC3}},
1053 // version tag
1054 {"Unable to read protocol version.",
1055 {QUIC_VERSION_BYTES}},
1056 // connection_id length
1057 {"Unable to read ConnectionId length.",
1058 {0x50}},
1059 // connection_id
dschinazi48ac9192019-07-31 00:07:26 -07001060 {"Unable to read destination connection ID.",
QUICHE teama6ef0a62019-03-07 20:34:33 -05001061 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
1062 // packet number
1063 {"Unable to read packet number.",
1064 {0x12, 0x34, 0x56, 0x78}},
1065 };
1066 // clang-format on
1067
1068 if (framer_.transport_version() <= QUIC_VERSION_43 ||
1069 QuicVersionHasLongHeaderLengths(framer_.transport_version())) {
1070 return;
1071 }
1072
QUICHE teama6ef0a62019-03-07 20:34:33 -05001073 std::unique_ptr<QuicEncryptedPacket> encrypted(
fayang36825da2019-08-21 14:01:27 -07001074 AssemblePacketFromFragments(packet46));
QUICHE teama6ef0a62019-03-07 20:34:33 -05001075
1076 EXPECT_FALSE(framer_.ProcessPacket(*encrypted));
1077 EXPECT_EQ(QUIC_MISSING_PAYLOAD, framer_.error());
1078 ASSERT_TRUE(visitor_.header_.get());
1079 EXPECT_EQ(FramerTestConnectionId(),
1080 visitor_.header_->destination_connection_id);
1081 EXPECT_FALSE(visitor_.header_->reset_flag);
1082 EXPECT_TRUE(visitor_.header_->version_flag);
1083 EXPECT_EQ(kPacketNumber, visitor_.header_->packet_number);
1084
fayang36825da2019-08-21 14:01:27 -07001085 CheckFramingBoundaries(packet46, QUIC_INVALID_PACKET_HEADER);
fayangccbab732019-05-13 10:11:25 -07001086
1087 PacketHeaderFormat format;
1088 bool version_flag;
dschinazib42a8c52019-05-30 09:45:01 -07001089 QuicConnectionId destination_connection_id, source_connection_id;
fayangccbab732019-05-13 10:11:25 -07001090 QuicVersionLabel version_label;
1091 std::string detailed_error;
dschinazi48ac9192019-07-31 00:07:26 -07001092 QuicErrorCode error_code;
1093 if (!GetQuicReloadableFlag(quic_use_parse_public_header)) {
1094 error_code = QuicFramer::ProcessPacketDispatcher(
1095 *encrypted, kQuicDefaultConnectionIdLength, &format, &version_flag,
1096 &version_label, &destination_connection_id, &source_connection_id,
1097 &detailed_error);
1098 } else {
1099 bool retry_token_present, use_length_prefix;
1100 QuicStringPiece retry_token;
1101 ParsedQuicVersion parsed_version = UnsupportedQuicVersion();
1102 error_code = QuicFramer::ParsePublicHeaderDispatcher(
1103 *encrypted, kQuicDefaultConnectionIdLength, &format, &version_flag,
1104 &use_length_prefix, &version_label, &parsed_version,
1105 &destination_connection_id, &source_connection_id, &retry_token_present,
1106 &retry_token, &detailed_error);
1107 EXPECT_EQ(retry_token_present, framer_.version().SupportsRetry());
1108 EXPECT_FALSE(use_length_prefix);
1109 }
fayangccbab732019-05-13 10:11:25 -07001110 EXPECT_EQ(IETF_QUIC_LONG_HEADER_PACKET, format);
1111 EXPECT_TRUE(version_flag);
dschinazib42a8c52019-05-30 09:45:01 -07001112 EXPECT_EQ(kQuicDefaultConnectionIdLength, destination_connection_id.length());
fayangccbab732019-05-13 10:11:25 -07001113 EXPECT_EQ(FramerTestConnectionId(), destination_connection_id);
dschinazib42a8c52019-05-30 09:45:01 -07001114 EXPECT_EQ(EmptyQuicConnectionId(), source_connection_id);
1115}
1116
1117TEST_P(QuicFramerTest, LongPacketHeaderWithBothConnectionIds) {
1118 if (framer_.transport_version() <= QUIC_VERSION_43) {
1119 // This test requires an IETF long header.
1120 return;
1121 }
dschinazi48ac9192019-07-31 00:07:26 -07001122 SetQuicReloadableFlag(quic_use_parse_public_header, false);
dschinazib42a8c52019-05-30 09:45:01 -07001123 SetDecrypterLevel(ENCRYPTION_ZERO_RTT);
dschinazib42a8c52019-05-30 09:45:01 -07001124 // clang-format off
1125 unsigned char packet[] = {
1126 // public flags (long header with packet type ZERO_RTT_PROTECTED and
1127 // 4-byte packet number)
fayang36825da2019-08-21 14:01:27 -07001128 0xD3,
dschinazib42a8c52019-05-30 09:45:01 -07001129 // version
1130 QUIC_VERSION_BYTES,
1131 // connection ID lengths
1132 0x55,
1133 // destination connection ID
1134 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
1135 // source connection ID
1136 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x11,
1137 // long header packet length
1138 0x05,
1139 // packet number
1140 0x12, 0x34, 0x56, 0x00,
1141 // padding frame
1142 0x00,
1143 };
1144 // clang-format on
1145
1146 QuicEncryptedPacket encrypted(AsChars(packet), QUIC_ARRAYSIZE(packet), false);
1147 PacketHeaderFormat format = GOOGLE_QUIC_PACKET;
1148 bool version_flag = false;
1149 QuicConnectionId destination_connection_id, source_connection_id;
1150 QuicVersionLabel version_label = 0;
1151 std::string detailed_error = "";
dschinazi48ac9192019-07-31 00:07:26 -07001152 QuicErrorCode error_code;
1153 if (!GetQuicReloadableFlag(quic_use_parse_public_header)) {
1154 error_code = QuicFramer::ProcessPacketDispatcher(
1155 encrypted, kQuicDefaultConnectionIdLength, &format, &version_flag,
1156 &version_label, &destination_connection_id, &source_connection_id,
1157 &detailed_error);
1158 } else {
1159 bool retry_token_present, use_length_prefix;
1160 QuicStringPiece retry_token;
1161 ParsedQuicVersion parsed_version = UnsupportedQuicVersion();
1162 error_code = QuicFramer::ParsePublicHeaderDispatcher(
1163 encrypted, kQuicDefaultConnectionIdLength, &format, &version_flag,
1164 &use_length_prefix, &version_label, &parsed_version,
1165 &destination_connection_id, &source_connection_id, &retry_token_present,
1166 &retry_token, &detailed_error);
1167 EXPECT_FALSE(retry_token_present);
1168 EXPECT_FALSE(use_length_prefix);
1169 }
dschinazib42a8c52019-05-30 09:45:01 -07001170 EXPECT_EQ("", detailed_error);
1171 EXPECT_EQ(IETF_QUIC_LONG_HEADER_PACKET, format);
1172 EXPECT_TRUE(version_flag);
1173 EXPECT_EQ(FramerTestConnectionId(), destination_connection_id);
1174 EXPECT_EQ(FramerTestConnectionIdPlusOne(), source_connection_id);
QUICHE teama6ef0a62019-03-07 20:34:33 -05001175}
1176
dschinazi243eabc2019-08-05 16:15:29 -07001177TEST_P(QuicFramerTest, ParsePublicHeader) {
dschinazi243eabc2019-08-05 16:15:29 -07001178 // clang-format off
1179 unsigned char packet[] = {
1180 // public flags (version included, 8-byte connection ID,
1181 // 4-byte packet number)
1182 0x29,
1183 // connection_id
1184 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
1185 // version
1186 QUIC_VERSION_BYTES,
1187 // packet number
1188 0x12, 0x34, 0x56, 0x78,
1189 // padding frame
1190 0x00,
1191 };
fayang36825da2019-08-21 14:01:27 -07001192 unsigned char packet46[] = {
1193 // public flags (long header with packet type HANDSHAKE and
1194 // 4-byte packet number)
1195 0xE3,
1196 // version
1197 QUIC_VERSION_BYTES,
1198 // connection ID lengths
1199 0x50,
1200 // destination connection ID
1201 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
1202 // long header packet length
1203 0x05,
1204 // packet number
1205 0x12, 0x34, 0x56, 0x78,
1206 // padding frame
1207 0x00,
dschinazi243eabc2019-08-05 16:15:29 -07001208 };
1209 unsigned char packet99[] = {
1210 // public flags (long header with packet type HANDSHAKE and
1211 // 4-byte packet number)
1212 0xE3,
1213 // version
1214 QUIC_VERSION_BYTES,
1215 // destination connection ID length
1216 0x08,
1217 // destination connection ID
1218 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
1219 // source connection ID length
1220 0x00,
1221 // long header packet length
1222 0x05,
1223 // packet number
1224 0x12, 0x34, 0x56, 0x78,
1225 // padding frame
1226 0x00,
1227 };
1228 // clang-format on
1229 unsigned char* p = packet;
1230 size_t p_length = QUIC_ARRAYSIZE(packet);
1231 if (framer_.transport_version() == QUIC_VERSION_99) {
1232 p = packet99;
1233 p_length = QUIC_ARRAYSIZE(packet99);
fayang36825da2019-08-21 14:01:27 -07001234 } else if (framer_.transport_version() >= QUIC_VERSION_46) {
1235 p = packet46;
1236 p_length = QUIC_ARRAYSIZE(packet46);
dschinazi243eabc2019-08-05 16:15:29 -07001237 }
1238
1239 uint8_t first_byte = 0x33;
1240 PacketHeaderFormat format = GOOGLE_QUIC_PACKET;
1241 bool version_present = false, has_length_prefix = false;
1242 QuicVersionLabel version_label = 0;
1243 ParsedQuicVersion parsed_version = UnsupportedQuicVersion();
1244 QuicConnectionId destination_connection_id = EmptyQuicConnectionId(),
1245 source_connection_id = EmptyQuicConnectionId();
1246 QuicLongHeaderType long_packet_type = INVALID_PACKET_TYPE;
1247 QuicVariableLengthIntegerLength retry_token_length_length =
1248 VARIABLE_LENGTH_INTEGER_LENGTH_4;
1249 QuicStringPiece retry_token;
1250 std::string detailed_error = "foobar";
1251
1252 QuicDataReader reader(AsChars(p), p_length);
1253 const QuicErrorCode parse_error = QuicFramer::ParsePublicHeader(
1254 &reader, kQuicDefaultConnectionIdLength,
1255 /*ietf_format=*/
1256 VersionHasIetfInvariantHeader(framer_.transport_version()), &first_byte,
1257 &format, &version_present, &has_length_prefix, &version_label,
1258 &parsed_version, &destination_connection_id, &source_connection_id,
1259 &long_packet_type, &retry_token_length_length, &retry_token,
1260 &detailed_error);
1261 EXPECT_EQ(QUIC_NO_ERROR, parse_error);
1262 EXPECT_EQ("", detailed_error);
1263 EXPECT_EQ(p[0], first_byte);
1264 EXPECT_TRUE(version_present);
1265 EXPECT_EQ(framer_.version().HasLengthPrefixedConnectionIds(),
1266 has_length_prefix);
1267 EXPECT_EQ(CreateQuicVersionLabel(framer_.version()), version_label);
1268 EXPECT_EQ(framer_.version(), parsed_version);
1269 EXPECT_EQ(FramerTestConnectionId(), destination_connection_id);
1270 EXPECT_EQ(EmptyQuicConnectionId(), source_connection_id);
1271 EXPECT_EQ(VARIABLE_LENGTH_INTEGER_LENGTH_0, retry_token_length_length);
1272 EXPECT_EQ(QuicStringPiece(), retry_token);
1273 if (VersionHasIetfInvariantHeader(framer_.transport_version())) {
1274 EXPECT_EQ(IETF_QUIC_LONG_HEADER_PACKET, format);
1275 EXPECT_EQ(HANDSHAKE, long_packet_type);
1276 } else {
1277 EXPECT_EQ(GOOGLE_QUIC_PACKET, format);
1278 }
1279}
1280
dschinazi346b7ce2019-06-05 01:38:18 -07001281TEST_P(QuicFramerTest, ClientConnectionIdFromShortHeaderToClient) {
dschinazi346b7ce2019-06-05 01:38:18 -07001282 if (!framer_.version().SupportsClientConnectionIds()) {
1283 return;
1284 }
1285 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
1286 QuicFramerPeer::SetLastSerializedServerConnectionId(&framer_,
1287 TestConnectionId(0x33));
1288 QuicFramerPeer::SetPerspective(&framer_, Perspective::IS_CLIENT);
1289 framer_.SetExpectedClientConnectionIdLength(kQuicDefaultConnectionIdLength);
1290 // clang-format off
1291 unsigned char packet[] = {
1292 // type (short header, 4 byte packet number)
1293 0x43,
1294 // connection_id
1295 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
1296 // packet number
1297 0x13, 0x37, 0x42, 0x33,
1298 // padding frame
1299 0x00,
1300 };
1301 // clang-format on
1302 QuicEncryptedPacket encrypted(AsChars(packet), QUIC_ARRAYSIZE(packet), false);
1303 EXPECT_TRUE(framer_.ProcessPacket(encrypted));
1304 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
1305 EXPECT_EQ("", framer_.detailed_error());
1306 ASSERT_TRUE(visitor_.header_.get());
1307 EXPECT_EQ(FramerTestConnectionId(),
1308 visitor_.header_->destination_connection_id);
1309 EXPECT_EQ(TestConnectionId(0x33), visitor_.header_->source_connection_id);
1310}
1311
1312// In short header packets from client to server, the client connection ID
1313// is omitted, but the framer adds it to the header struct using its
1314// last serialized client connection ID. This test ensures that this
1315// mechanism behaves as expected.
1316TEST_P(QuicFramerTest, ClientConnectionIdFromShortHeaderToServer) {
dschinazi346b7ce2019-06-05 01:38:18 -07001317 if (!framer_.version().SupportsClientConnectionIds()) {
1318 return;
1319 }
1320 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
1321 QuicFramerPeer::SetLastSerializedClientConnectionId(&framer_,
1322 TestConnectionId(0x33));
1323 QuicFramerPeer::SetPerspective(&framer_, Perspective::IS_SERVER);
1324 // clang-format off
1325 unsigned char packet[] = {
1326 // type (short header, 4 byte packet number)
1327 0x43,
1328 // connection_id
1329 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
1330 // packet number
1331 0x13, 0x37, 0x42, 0x33,
1332 // padding frame
1333 0x00,
1334 };
1335 // clang-format on
1336 QuicEncryptedPacket encrypted(AsChars(packet), QUIC_ARRAYSIZE(packet), false);
1337 EXPECT_TRUE(framer_.ProcessPacket(encrypted));
1338 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
1339 EXPECT_EQ("", framer_.detailed_error());
1340 ASSERT_TRUE(visitor_.header_.get());
1341 EXPECT_EQ(FramerTestConnectionId(),
1342 visitor_.header_->destination_connection_id);
1343 EXPECT_EQ(TestConnectionId(0x33), visitor_.header_->source_connection_id);
1344}
1345
QUICHE teama6ef0a62019-03-07 20:34:33 -05001346TEST_P(QuicFramerTest, PacketHeaderWith0ByteConnectionId) {
zhongyi546cc452019-04-12 15:27:49 -07001347 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
dschinazi7b9278c2019-05-20 07:36:21 -07001348 QuicFramerPeer::SetLastSerializedServerConnectionId(&framer_,
1349 FramerTestConnectionId());
QUICHE teama6ef0a62019-03-07 20:34:33 -05001350 QuicFramerPeer::SetPerspective(&framer_, Perspective::IS_CLIENT);
1351
1352 // clang-format off
1353 PacketFragments packet = {
1354 // public flags (0 byte connection_id)
1355 {"Unable to read public flags.",
1356 {0x20}},
1357 // connection_id
1358 // packet number
1359 {"Unable to read packet number.",
1360 {0x12, 0x34, 0x56, 0x78}},
1361 };
1362
QUICHE teama6ef0a62019-03-07 20:34:33 -05001363 PacketFragments packet46 = {
1364 // type (short header, 4 byte packet number)
dschinazi48ac9192019-07-31 00:07:26 -07001365 {"Unable to read first byte.",
QUICHE teama6ef0a62019-03-07 20:34:33 -05001366 {0x43}},
1367 // connection_id
1368 // packet number
1369 {"Unable to read packet number.",
1370 {0x12, 0x34, 0x56, 0x78}},
1371 };
nharper55fa6132019-05-07 19:37:21 -07001372
1373 PacketFragments packet_hp = {
1374 // type (short header, 4 byte packet number)
dschinazi48ac9192019-07-31 00:07:26 -07001375 {"Unable to read first byte.",
nharper55fa6132019-05-07 19:37:21 -07001376 {0x43}},
1377 // connection_id
1378 // packet number
1379 {"",
1380 {0x12, 0x34, 0x56, 0x78}},
1381 };
QUICHE teama6ef0a62019-03-07 20:34:33 -05001382 // clang-format on
1383
1384 PacketFragments& fragments =
nharper55fa6132019-05-07 19:37:21 -07001385 framer_.version().HasHeaderProtection()
1386 ? packet_hp
fayang36825da2019-08-21 14:01:27 -07001387 : (framer_.transport_version() >= QUIC_VERSION_46 ? packet46
1388 : packet);
QUICHE teama6ef0a62019-03-07 20:34:33 -05001389 std::unique_ptr<QuicEncryptedPacket> encrypted(
1390 AssemblePacketFromFragments(fragments));
1391 EXPECT_FALSE(framer_.ProcessPacket(*encrypted));
1392 EXPECT_EQ(QUIC_MISSING_PAYLOAD, framer_.error());
1393 ASSERT_TRUE(visitor_.header_.get());
dschinazi5e1a7b22019-07-31 12:23:21 -07001394 EXPECT_EQ(FramerTestConnectionId(), visitor_.header_->source_connection_id);
QUICHE teama6ef0a62019-03-07 20:34:33 -05001395 EXPECT_FALSE(visitor_.header_->reset_flag);
1396 EXPECT_FALSE(visitor_.header_->version_flag);
1397 EXPECT_EQ(kPacketNumber, visitor_.header_->packet_number);
1398
1399 CheckFramingBoundaries(fragments, QUIC_INVALID_PACKET_HEADER);
1400}
1401
1402TEST_P(QuicFramerTest, PacketHeaderWithVersionFlag) {
zhongyi546cc452019-04-12 15:27:49 -07001403 SetDecrypterLevel(ENCRYPTION_ZERO_RTT);
QUICHE teama6ef0a62019-03-07 20:34:33 -05001404 // clang-format off
1405 PacketFragments packet = {
1406 // public flags (0 byte connection_id)
1407 {"Unable to read public flags.",
1408 {0x29}},
1409 // connection_id
1410 {"Unable to read ConnectionId.",
1411 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
1412 // version tag
1413 {"Unable to read protocol version.",
1414 {QUIC_VERSION_BYTES}},
1415 // packet number
1416 {"Unable to read packet number.",
1417 {0x12, 0x34, 0x56, 0x78}},
1418 };
1419
QUICHE teama6ef0a62019-03-07 20:34:33 -05001420 PacketFragments packet46 = {
1421 // type (long header with packet type ZERO_RTT_PROTECTED and 4 bytes
1422 // packet number)
dschinazi48ac9192019-07-31 00:07:26 -07001423 {"Unable to read first byte.",
QUICHE teama6ef0a62019-03-07 20:34:33 -05001424 {0xD3}},
1425 // version tag
1426 {"Unable to read protocol version.",
1427 {QUIC_VERSION_BYTES}},
1428 // connection_id length
1429 {"Unable to read ConnectionId length.",
1430 {0x50}},
1431 // connection_id
dschinazi48ac9192019-07-31 00:07:26 -07001432 {"Unable to read destination connection ID.",
QUICHE teama6ef0a62019-03-07 20:34:33 -05001433 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
1434 // packet number
1435 {"Unable to read packet number.",
1436 {0x12, 0x34, 0x56, 0x78}},
1437 };
1438
1439 PacketFragments packet99 = {
1440 // type (long header with packet type ZERO_RTT_PROTECTED and 4 bytes
1441 // packet number)
dschinazi48ac9192019-07-31 00:07:26 -07001442 {"Unable to read first byte.",
QUICHE teama6ef0a62019-03-07 20:34:33 -05001443 {0xD3}},
1444 // version tag
1445 {"Unable to read protocol version.",
1446 {QUIC_VERSION_BYTES}},
dschinazi48ac9192019-07-31 00:07:26 -07001447 // destination connection ID length
1448 {"Unable to read destination connection ID.",
1449 {0x08}},
1450 // destination connection ID
1451 {"Unable to read destination connection ID.",
QUICHE teama6ef0a62019-03-07 20:34:33 -05001452 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
dschinazi48ac9192019-07-31 00:07:26 -07001453 // source connection ID length
1454 {"Unable to read source connection ID.",
1455 {0x00}},
QUICHE teama6ef0a62019-03-07 20:34:33 -05001456 // long header packet length
1457 {"Unable to read long header payload length.",
1458 {0x04}},
1459 // packet number
1460 {"Long header payload length longer than packet.",
1461 {0x12, 0x34, 0x56, 0x78}},
1462 };
1463 // clang-format on
1464
1465 PacketFragments& fragments =
1466 framer_.transport_version() == QUIC_VERSION_99
1467 ? packet99
fayang36825da2019-08-21 14:01:27 -07001468 : (framer_.transport_version() >= QUIC_VERSION_46 ? packet46
1469 : packet);
QUICHE teama6ef0a62019-03-07 20:34:33 -05001470 std::unique_ptr<QuicEncryptedPacket> encrypted(
1471 AssemblePacketFromFragments(fragments));
1472 EXPECT_FALSE(framer_.ProcessPacket(*encrypted));
1473 EXPECT_EQ(QUIC_MISSING_PAYLOAD, framer_.error());
1474 ASSERT_TRUE(visitor_.header_.get());
1475 EXPECT_EQ(FramerTestConnectionId(),
1476 visitor_.header_->destination_connection_id);
1477 EXPECT_FALSE(visitor_.header_->reset_flag);
1478 EXPECT_TRUE(visitor_.header_->version_flag);
1479 EXPECT_EQ(GetParam(), visitor_.header_->version);
1480 EXPECT_EQ(kPacketNumber, visitor_.header_->packet_number);
1481
1482 CheckFramingBoundaries(fragments, QUIC_INVALID_PACKET_HEADER);
1483}
1484
1485TEST_P(QuicFramerTest, PacketHeaderWith4BytePacketNumber) {
zhongyi546cc452019-04-12 15:27:49 -07001486 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
QUICHE teama6ef0a62019-03-07 20:34:33 -05001487 QuicFramerPeer::SetLargestPacketNumber(&framer_, kPacketNumber - 2);
1488
1489 // clang-format off
1490 PacketFragments packet = {
1491 // public flags (8 byte connection_id and 4 byte packet number)
1492 {"Unable to read public flags.",
1493 {0x28}},
1494 // connection_id
1495 {"Unable to read ConnectionId.",
1496 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
1497 // packet number
1498 {"Unable to read packet number.",
1499 {0x12, 0x34, 0x56, 0x78}},
1500 };
1501
QUICHE teama6ef0a62019-03-07 20:34:33 -05001502 PacketFragments packet46 = {
1503 // type (short header, 4 byte packet number)
dschinazi48ac9192019-07-31 00:07:26 -07001504 {"Unable to read first byte.",
QUICHE teama6ef0a62019-03-07 20:34:33 -05001505 {0x43}},
1506 // connection_id
dschinazi48ac9192019-07-31 00:07:26 -07001507 {"Unable to read destination connection ID.",
QUICHE teama6ef0a62019-03-07 20:34:33 -05001508 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
1509 // packet number
1510 {"Unable to read packet number.",
1511 {0x12, 0x34, 0x56, 0x78}},
1512 };
nharper55fa6132019-05-07 19:37:21 -07001513
1514 PacketFragments packet_hp = {
1515 // type (short header, 4 byte packet number)
dschinazi48ac9192019-07-31 00:07:26 -07001516 {"Unable to read first byte.",
nharper55fa6132019-05-07 19:37:21 -07001517 {0x43}},
1518 // connection_id
dschinazi48ac9192019-07-31 00:07:26 -07001519 {"Unable to read destination connection ID.",
nharper55fa6132019-05-07 19:37:21 -07001520 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
1521 // packet number
1522 {"",
1523 {0x12, 0x34, 0x56, 0x78}},
1524 };
QUICHE teama6ef0a62019-03-07 20:34:33 -05001525 // clang-format on
1526
1527 PacketFragments& fragments =
nharper55fa6132019-05-07 19:37:21 -07001528 framer_.version().HasHeaderProtection()
1529 ? packet_hp
fayang36825da2019-08-21 14:01:27 -07001530 : (framer_.transport_version() >= QUIC_VERSION_46 ? packet46
1531 : packet);
QUICHE teama6ef0a62019-03-07 20:34:33 -05001532 std::unique_ptr<QuicEncryptedPacket> encrypted(
1533 AssemblePacketFromFragments(fragments));
1534 EXPECT_FALSE(framer_.ProcessPacket(*encrypted));
1535 EXPECT_EQ(QUIC_MISSING_PAYLOAD, framer_.error());
1536 ASSERT_TRUE(visitor_.header_.get());
1537 EXPECT_EQ(FramerTestConnectionId(),
1538 visitor_.header_->destination_connection_id);
1539 EXPECT_FALSE(visitor_.header_->reset_flag);
1540 EXPECT_FALSE(visitor_.header_->version_flag);
1541 EXPECT_EQ(kPacketNumber, visitor_.header_->packet_number);
1542
1543 CheckFramingBoundaries(fragments, QUIC_INVALID_PACKET_HEADER);
1544}
1545
1546TEST_P(QuicFramerTest, PacketHeaderWith2BytePacketNumber) {
zhongyi546cc452019-04-12 15:27:49 -07001547 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
QUICHE teama6ef0a62019-03-07 20:34:33 -05001548 QuicFramerPeer::SetLargestPacketNumber(&framer_, kPacketNumber - 2);
1549
1550 // clang-format off
1551 PacketFragments packet = {
1552 // public flags (8 byte connection_id and 2 byte packet number)
1553 {"Unable to read public flags.",
1554 {0x18}},
1555 // connection_id
1556 {"Unable to read ConnectionId.",
1557 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
1558 // packet number
1559 {"Unable to read packet number.",
1560 {0x56, 0x78}},
1561 };
1562
QUICHE teama6ef0a62019-03-07 20:34:33 -05001563 PacketFragments packet46 = {
1564 // type (short header, 2 byte packet number)
dschinazi48ac9192019-07-31 00:07:26 -07001565 {"Unable to read first byte.",
QUICHE teama6ef0a62019-03-07 20:34:33 -05001566 {0x41}},
1567 // connection_id
dschinazi48ac9192019-07-31 00:07:26 -07001568 {"Unable to read destination connection ID.",
QUICHE teama6ef0a62019-03-07 20:34:33 -05001569 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
1570 // packet number
1571 {"Unable to read packet number.",
1572 {0x56, 0x78}},
1573 };
nharper55fa6132019-05-07 19:37:21 -07001574
1575 PacketFragments packet_hp = {
1576 // type (short header, 2 byte packet number)
dschinazi48ac9192019-07-31 00:07:26 -07001577 {"Unable to read first byte.",
nharper55fa6132019-05-07 19:37:21 -07001578 {0x41}},
1579 // connection_id
dschinazi48ac9192019-07-31 00:07:26 -07001580 {"Unable to read destination connection ID.",
nharper55fa6132019-05-07 19:37:21 -07001581 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
1582 // packet number
1583 {"",
1584 {0x56, 0x78}},
1585 // padding
1586 {"", {0x00, 0x00}},
1587 };
QUICHE teama6ef0a62019-03-07 20:34:33 -05001588 // clang-format on
1589
1590 PacketFragments& fragments =
nharper55fa6132019-05-07 19:37:21 -07001591 framer_.version().HasHeaderProtection()
1592 ? packet_hp
fayang36825da2019-08-21 14:01:27 -07001593 : (framer_.transport_version() >= QUIC_VERSION_46 ? packet46
1594 : packet);
QUICHE teama6ef0a62019-03-07 20:34:33 -05001595 std::unique_ptr<QuicEncryptedPacket> encrypted(
1596 AssemblePacketFromFragments(fragments));
nharper55fa6132019-05-07 19:37:21 -07001597 if (framer_.version().HasHeaderProtection()) {
1598 EXPECT_TRUE(framer_.ProcessPacket(*encrypted));
1599 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
1600 } else {
1601 EXPECT_FALSE(framer_.ProcessPacket(*encrypted));
1602 EXPECT_EQ(QUIC_MISSING_PAYLOAD, framer_.error());
1603 }
QUICHE teama6ef0a62019-03-07 20:34:33 -05001604 ASSERT_TRUE(visitor_.header_.get());
1605 EXPECT_EQ(FramerTestConnectionId(),
1606 visitor_.header_->destination_connection_id);
1607 EXPECT_FALSE(visitor_.header_->reset_flag);
1608 EXPECT_FALSE(visitor_.header_->version_flag);
1609 EXPECT_EQ(PACKET_2BYTE_PACKET_NUMBER, visitor_.header_->packet_number_length);
1610 EXPECT_EQ(kPacketNumber, visitor_.header_->packet_number);
1611
1612 CheckFramingBoundaries(fragments, QUIC_INVALID_PACKET_HEADER);
1613}
1614
1615TEST_P(QuicFramerTest, PacketHeaderWith1BytePacketNumber) {
zhongyi546cc452019-04-12 15:27:49 -07001616 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
QUICHE teama6ef0a62019-03-07 20:34:33 -05001617 QuicFramerPeer::SetLargestPacketNumber(&framer_, kPacketNumber - 2);
1618
1619 // clang-format off
1620 PacketFragments packet = {
1621 // public flags (8 byte connection_id and 1 byte packet number)
1622 {"Unable to read public flags.",
1623 {0x08}},
1624 // connection_id
1625 {"Unable to read ConnectionId.",
1626 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
1627 // packet number
1628 {"Unable to read packet number.",
1629 {0x78}},
1630 };
1631
QUICHE teama6ef0a62019-03-07 20:34:33 -05001632 PacketFragments packet46 = {
1633 // type (8 byte connection_id and 1 byte packet number)
dschinazi48ac9192019-07-31 00:07:26 -07001634 {"Unable to read first byte.",
QUICHE teama6ef0a62019-03-07 20:34:33 -05001635 {0x40}},
1636 // connection_id
dschinazi48ac9192019-07-31 00:07:26 -07001637 {"Unable to read destination connection ID.",
QUICHE teama6ef0a62019-03-07 20:34:33 -05001638 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
1639 // packet number
1640 {"Unable to read packet number.",
1641 {0x78}},
1642 };
1643
nharper55fa6132019-05-07 19:37:21 -07001644 PacketFragments packet_hp = {
1645 // type (8 byte connection_id and 1 byte packet number)
dschinazi48ac9192019-07-31 00:07:26 -07001646 {"Unable to read first byte.",
nharper55fa6132019-05-07 19:37:21 -07001647 {0x40}},
1648 // connection_id
dschinazi48ac9192019-07-31 00:07:26 -07001649 {"Unable to read destination connection ID.",
nharper55fa6132019-05-07 19:37:21 -07001650 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
1651 // packet number
1652 {"",
1653 {0x78}},
1654 // padding
1655 {"", {0x00, 0x00, 0x00}},
1656 };
1657
QUICHE teama6ef0a62019-03-07 20:34:33 -05001658 // clang-format on
1659
1660 PacketFragments& fragments =
nharper55fa6132019-05-07 19:37:21 -07001661 framer_.version().HasHeaderProtection()
1662 ? packet_hp
fayang36825da2019-08-21 14:01:27 -07001663 : (framer_.transport_version() >= QUIC_VERSION_46 ? packet46
1664 : packet);
QUICHE teama6ef0a62019-03-07 20:34:33 -05001665 std::unique_ptr<QuicEncryptedPacket> encrypted(
1666 AssemblePacketFromFragments(fragments));
nharper55fa6132019-05-07 19:37:21 -07001667 if (framer_.version().HasHeaderProtection()) {
1668 EXPECT_TRUE(framer_.ProcessPacket(*encrypted));
1669 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
1670 } else {
1671 EXPECT_FALSE(framer_.ProcessPacket(*encrypted));
1672 EXPECT_EQ(QUIC_MISSING_PAYLOAD, framer_.error());
1673 }
QUICHE teama6ef0a62019-03-07 20:34:33 -05001674 ASSERT_TRUE(visitor_.header_.get());
1675 EXPECT_EQ(FramerTestConnectionId(),
1676 visitor_.header_->destination_connection_id);
1677 EXPECT_FALSE(visitor_.header_->reset_flag);
1678 EXPECT_FALSE(visitor_.header_->version_flag);
1679 EXPECT_EQ(PACKET_1BYTE_PACKET_NUMBER, visitor_.header_->packet_number_length);
1680 EXPECT_EQ(kPacketNumber, visitor_.header_->packet_number);
1681
1682 CheckFramingBoundaries(fragments, QUIC_INVALID_PACKET_HEADER);
1683}
1684
1685TEST_P(QuicFramerTest, PacketNumberDecreasesThenIncreases) {
zhongyi546cc452019-04-12 15:27:49 -07001686 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
QUICHE teama6ef0a62019-03-07 20:34:33 -05001687 // Test the case when a packet is received from the past and future packet
1688 // numbers are still calculated relative to the largest received packet.
1689 QuicPacketHeader header;
1690 header.destination_connection_id = FramerTestConnectionId();
1691 header.reset_flag = false;
1692 header.version_flag = false;
1693 header.packet_number = kPacketNumber - 2;
1694
1695 QuicFrames frames = {QuicFrame(QuicPaddingFrame())};
1696 QuicFramerPeer::SetPerspective(&framer_, Perspective::IS_CLIENT);
1697 std::unique_ptr<QuicPacket> data(BuildDataPacket(header, frames));
1698 ASSERT_TRUE(data != nullptr);
1699
1700 QuicEncryptedPacket encrypted(data->data(), data->length(), false);
1701 QuicFramerPeer::SetPerspective(&framer_, Perspective::IS_SERVER);
1702 EXPECT_TRUE(framer_.ProcessPacket(encrypted));
1703 ASSERT_TRUE(visitor_.header_.get());
1704 EXPECT_EQ(FramerTestConnectionId(),
1705 visitor_.header_->destination_connection_id);
1706 EXPECT_EQ(PACKET_4BYTE_PACKET_NUMBER, visitor_.header_->packet_number_length);
1707 EXPECT_EQ(kPacketNumber - 2, visitor_.header_->packet_number);
1708
1709 // Receive a 1 byte packet number.
1710 header.packet_number = kPacketNumber;
1711 header.packet_number_length = PACKET_1BYTE_PACKET_NUMBER;
1712 QuicFramerPeer::SetPerspective(&framer_, Perspective::IS_CLIENT);
1713 data = BuildDataPacket(header, frames);
1714 QuicEncryptedPacket encrypted1(data->data(), data->length(), false);
1715 QuicFramerPeer::SetPerspective(&framer_, Perspective::IS_SERVER);
1716 EXPECT_TRUE(framer_.ProcessPacket(encrypted1));
1717 ASSERT_TRUE(visitor_.header_.get());
1718 EXPECT_EQ(FramerTestConnectionId(),
1719 visitor_.header_->destination_connection_id);
1720 EXPECT_EQ(PACKET_1BYTE_PACKET_NUMBER, visitor_.header_->packet_number_length);
1721 EXPECT_EQ(kPacketNumber, visitor_.header_->packet_number);
1722
1723 // Process a 2 byte packet number 256 packets ago.
1724 header.packet_number = kPacketNumber - 256;
1725 header.packet_number_length = PACKET_2BYTE_PACKET_NUMBER;
1726 QuicFramerPeer::SetPerspective(&framer_, Perspective::IS_CLIENT);
1727 data = BuildDataPacket(header, frames);
1728 QuicEncryptedPacket encrypted2(data->data(), data->length(), false);
1729 QuicFramerPeer::SetPerspective(&framer_, Perspective::IS_SERVER);
1730 EXPECT_TRUE(framer_.ProcessPacket(encrypted2));
1731 ASSERT_TRUE(visitor_.header_.get());
1732 EXPECT_EQ(FramerTestConnectionId(),
1733 visitor_.header_->destination_connection_id);
1734 EXPECT_EQ(PACKET_2BYTE_PACKET_NUMBER, visitor_.header_->packet_number_length);
1735 EXPECT_EQ(kPacketNumber - 256, visitor_.header_->packet_number);
1736
1737 // Process another 1 byte packet number and ensure it works.
1738 header.packet_number = kPacketNumber - 1;
1739 header.packet_number_length = PACKET_1BYTE_PACKET_NUMBER;
1740 QuicFramerPeer::SetPerspective(&framer_, Perspective::IS_CLIENT);
1741 data = BuildDataPacket(header, frames);
1742 QuicEncryptedPacket encrypted3(data->data(), data->length(), false);
1743 QuicFramerPeer::SetPerspective(&framer_, Perspective::IS_SERVER);
1744 EXPECT_TRUE(framer_.ProcessPacket(encrypted3));
1745 ASSERT_TRUE(visitor_.header_.get());
1746 EXPECT_EQ(FramerTestConnectionId(),
1747 visitor_.header_->destination_connection_id);
1748 EXPECT_EQ(PACKET_1BYTE_PACKET_NUMBER, visitor_.header_->packet_number_length);
1749 EXPECT_EQ(kPacketNumber - 1, visitor_.header_->packet_number);
1750}
1751
1752TEST_P(QuicFramerTest, PacketWithDiversificationNonce) {
zhongyi546cc452019-04-12 15:27:49 -07001753 SetDecrypterLevel(ENCRYPTION_ZERO_RTT);
QUICHE teama6ef0a62019-03-07 20:34:33 -05001754 // clang-format off
1755 unsigned char packet[] = {
1756 // public flags: includes nonce flag
1757 0x2C,
1758 // connection_id
1759 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
1760 // nonce
1761 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
1762 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
1763 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
1764 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
1765 // packet number
1766 0x12, 0x34, 0x56, 0x78,
1767
1768 // frame type (padding)
1769 0x00,
1770 0x00, 0x00, 0x00, 0x00
1771 };
1772
QUICHE teama6ef0a62019-03-07 20:34:33 -05001773 unsigned char packet46[] = {
1774 // type: Long header with packet type ZERO_RTT_PROTECTED and 1 byte packet
1775 // number.
1776 0xD0,
1777 // version tag
1778 QUIC_VERSION_BYTES,
1779 // connection_id length
1780 0x05,
1781 // connection_id
1782 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
1783 // packet number
1784 0x78,
1785 // nonce
1786 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
1787 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
1788 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
1789 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
1790
1791 // frame type (padding)
1792 0x00,
1793 0x00, 0x00, 0x00, 0x00
1794 };
1795
1796 unsigned char packet99[] = {
1797 // type: Long header with packet type ZERO_RTT_PROTECTED and 1 byte packet
1798 // number.
1799 0xD0,
1800 // version tag
1801 QUIC_VERSION_BYTES,
dschinazi48ac9192019-07-31 00:07:26 -07001802 // destination connection ID length
1803 0x00,
1804 // source connection ID length
1805 0x08,
1806 // source connection ID
QUICHE teama6ef0a62019-03-07 20:34:33 -05001807 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
1808 // long header packet length
1809 0x26,
1810 // packet number
1811 0x78,
1812 // nonce
1813 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
1814 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
1815 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
1816 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
1817
1818 // frame type (padding)
1819 0x00,
1820 0x00, 0x00, 0x00, 0x00
1821 };
1822 // clang-format on
1823
1824 if (framer_.version().handshake_protocol != PROTOCOL_QUIC_CRYPTO) {
1825 return;
1826 }
1827
1828 unsigned char* p = packet;
1829 size_t p_size = QUIC_ARRAYSIZE(packet);
1830 if (framer_.transport_version() == QUIC_VERSION_99) {
1831 p = packet99;
1832 p_size = QUIC_ARRAYSIZE(packet99);
fayang36825da2019-08-21 14:01:27 -07001833 } else if (framer_.transport_version() >= QUIC_VERSION_46) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05001834 p = packet46;
1835 p_size = QUIC_ARRAYSIZE(packet46);
QUICHE teama6ef0a62019-03-07 20:34:33 -05001836 }
1837
1838 QuicEncryptedPacket encrypted(AsChars(p), p_size, false);
1839 QuicFramerPeer::SetPerspective(&framer_, Perspective::IS_CLIENT);
1840 EXPECT_TRUE(framer_.ProcessPacket(encrypted));
1841 ASSERT_TRUE(visitor_.header_->nonce != nullptr);
1842 for (char i = 0; i < 32; ++i) {
1843 EXPECT_EQ(i, (*visitor_.header_->nonce)[static_cast<size_t>(i)]);
1844 }
1845 EXPECT_EQ(1u, visitor_.padding_frames_.size());
1846 EXPECT_EQ(5, visitor_.padding_frames_[0]->num_padding_bytes);
1847}
1848
1849TEST_P(QuicFramerTest, LargePublicFlagWithMismatchedVersions) {
1850 // clang-format off
1851 unsigned char packet[] = {
1852 // public flags (8 byte connection_id, version flag and an unknown flag)
1853 0x29,
1854 // connection_id
1855 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
1856 // version tag
1857 'Q', '0', '0', '0',
1858 // packet number
1859 0x12, 0x34, 0x56, 0x78,
1860
1861 // frame type (padding frame)
1862 0x00,
1863 0x00, 0x00, 0x00, 0x00
1864 };
1865
fayang36825da2019-08-21 14:01:27 -07001866 unsigned char packet46[] = {
nharper55fa6132019-05-07 19:37:21 -07001867 // type (long header, ZERO_RTT_PROTECTED, 4-byte packet number)
1868 0xD3,
1869 // version tag
1870 'Q', '0', '0', '0',
1871 // connection_id length
1872 0x50,
1873 // connection_id
1874 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
1875 // packet number
1876 0x12, 0x34, 0x56, 0x78,
1877
1878 // frame type (padding frame)
1879 0x00,
1880 0x00, 0x00, 0x00, 0x00
1881 };
dschinazi48ac9192019-07-31 00:07:26 -07001882
1883 unsigned char packet99[] = {
1884 // type (long header, ZERO_RTT_PROTECTED, 4-byte packet number)
1885 0xD3,
1886 // version tag
1887 'Q', '0', '0', '0',
1888 // destination connection ID length
1889 0x08,
1890 // destination connection ID
1891 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
1892 // source connection ID length
1893 0x00,
1894 // packet number
1895 0x12, 0x34, 0x56, 0x78,
1896
1897 // frame type (padding frame)
1898 0x00,
1899 0x00, 0x00, 0x00, 0x00
1900 };
QUICHE teama6ef0a62019-03-07 20:34:33 -05001901 // clang-format on
1902
nharper55fa6132019-05-07 19:37:21 -07001903 unsigned char* p = packet;
1904 size_t p_size = QUIC_ARRAYSIZE(packet);
dschinazi48ac9192019-07-31 00:07:26 -07001905 if (framer_.transport_version() >= QUIC_VERSION_99) {
1906 p = packet99;
1907 p_size = QUIC_ARRAYSIZE(packet99);
fayang36825da2019-08-21 14:01:27 -07001908 } else if (framer_.transport_version() >= QUIC_VERSION_46) {
1909 p = packet46;
1910 p_size = QUIC_ARRAYSIZE(packet46);
nharper55fa6132019-05-07 19:37:21 -07001911 }
1912 QuicEncryptedPacket encrypted(AsChars(p), p_size, false);
QUICHE teama6ef0a62019-03-07 20:34:33 -05001913 EXPECT_TRUE(framer_.ProcessPacket(encrypted));
1914 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
1915 ASSERT_TRUE(visitor_.header_.get());
1916 EXPECT_EQ(0, visitor_.frame_count_);
1917 EXPECT_EQ(1, visitor_.version_mismatch_);
QUICHE teama6ef0a62019-03-07 20:34:33 -05001918}
1919
1920TEST_P(QuicFramerTest, PaddingFrame) {
zhongyi546cc452019-04-12 15:27:49 -07001921 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
QUICHE teama6ef0a62019-03-07 20:34:33 -05001922 // clang-format off
1923 unsigned char packet[] = {
1924 // public flags (8 byte connection_id)
1925 0x28,
1926 // connection_id
1927 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
1928 // packet number
1929 0x12, 0x34, 0x56, 0x78,
1930
1931 // paddings
1932 0x00, 0x00,
1933 // frame type (stream frame with fin)
1934 0xFF,
1935 // stream id
1936 0x01, 0x02, 0x03, 0x04,
1937 // offset
1938 0x3A, 0x98, 0xFE, 0xDC,
1939 0x32, 0x10, 0x76, 0x54,
1940 // data length
1941 0x00, 0x0c,
1942 // data
1943 'h', 'e', 'l', 'l',
1944 'o', ' ', 'w', 'o',
1945 'r', 'l', 'd', '!',
1946 // paddings
1947 0x00, 0x00,
1948 };
1949
QUICHE teama6ef0a62019-03-07 20:34:33 -05001950 unsigned char packet46[] = {
1951 // type (short header, 4 byte packet number)
1952 0x43,
1953 // connection_id
1954 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
1955 // packet number
1956 0x12, 0x34, 0x56, 0x78,
1957
1958 // paddings
1959 0x00, 0x00,
1960 // frame type (stream frame with fin)
1961 0xFF,
1962 // stream id
1963 0x01, 0x02, 0x03, 0x04,
1964 // offset
1965 0x3A, 0x98, 0xFE, 0xDC,
1966 0x32, 0x10, 0x76, 0x54,
1967 // data length
1968 0x00, 0x0c,
1969 // data
1970 'h', 'e', 'l', 'l',
1971 'o', ' ', 'w', 'o',
1972 'r', 'l', 'd', '!',
1973 // paddings
1974 0x00, 0x00,
1975 };
1976
1977 unsigned char packet99[] = {
1978 // type (short header, 4 byte packet number)
1979 0x43,
1980 // connection_id
1981 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
1982 // packet number
1983 0x12, 0x34, 0x56, 0x78,
1984
1985 // paddings
1986 0x00, 0x00,
1987 // frame type - IETF_STREAM with FIN, LEN, and OFFSET bits set.
1988 0x08 | 0x01 | 0x02 | 0x04,
1989
1990 // stream id
1991 kVarInt62FourBytes + 0x01, 0x02, 0x03, 0x04,
1992 // offset
1993 kVarInt62EightBytes + 0x3A, 0x98, 0xFE, 0xDC,
1994 0x32, 0x10, 0x76, 0x54,
1995 // data length
1996 kVarInt62OneByte + 0x0c,
1997 // data
1998 'h', 'e', 'l', 'l',
1999 'o', ' ', 'w', 'o',
2000 'r', 'l', 'd', '!',
2001 // paddings
2002 0x00, 0x00,
2003 };
2004 // clang-format on
2005
2006 unsigned char* p = packet;
2007 size_t p_size = QUIC_ARRAYSIZE(packet);
fkastenholz305e1732019-06-18 05:01:22 -07002008 if (VersionHasIetfQuicFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05002009 p = packet99;
2010 p_size = QUIC_ARRAYSIZE(packet99);
fayang36825da2019-08-21 14:01:27 -07002011 } else if (framer_.transport_version() >= QUIC_VERSION_46) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05002012 p = packet46;
2013 p_size = QUIC_ARRAYSIZE(packet46);
QUICHE teama6ef0a62019-03-07 20:34:33 -05002014 }
2015
2016 QuicEncryptedPacket encrypted(AsChars(p), p_size, false);
2017 EXPECT_TRUE(framer_.ProcessPacket(encrypted));
2018 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
2019 ASSERT_TRUE(visitor_.header_.get());
2020 EXPECT_TRUE(CheckDecryption(
2021 encrypted, !kIncludeVersion, !kIncludeDiversificationNonce,
2022 PACKET_8BYTE_CONNECTION_ID, PACKET_0BYTE_CONNECTION_ID));
2023
2024 ASSERT_EQ(1u, visitor_.stream_frames_.size());
2025 EXPECT_EQ(0u, visitor_.ack_frames_.size());
2026 EXPECT_EQ(2u, visitor_.padding_frames_.size());
2027 EXPECT_EQ(2, visitor_.padding_frames_[0]->num_padding_bytes);
2028 EXPECT_EQ(2, visitor_.padding_frames_[1]->num_padding_bytes);
2029 EXPECT_EQ(kStreamId, visitor_.stream_frames_[0]->stream_id);
2030 EXPECT_TRUE(visitor_.stream_frames_[0]->fin);
2031 EXPECT_EQ(kStreamOffset, visitor_.stream_frames_[0]->offset);
2032 CheckStreamFrameData("hello world!", visitor_.stream_frames_[0].get());
2033}
2034
2035TEST_P(QuicFramerTest, StreamFrame) {
zhongyi546cc452019-04-12 15:27:49 -07002036 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
QUICHE teama6ef0a62019-03-07 20:34:33 -05002037 // clang-format off
2038 PacketFragments packet = {
2039 // public flags (8 byte connection_id)
2040 {"",
2041 {0x28}},
2042 // connection_id
2043 {"",
2044 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
2045 // packet number
2046 {"",
2047 {0x12, 0x34, 0x56, 0x78}},
2048 // frame type (stream frame with fin)
2049 {"",
2050 {0xFF}},
2051 // stream id
2052 {"Unable to read stream_id.",
2053 {0x01, 0x02, 0x03, 0x04}},
2054 // offset
2055 {"Unable to read offset.",
2056 {0x3A, 0x98, 0xFE, 0xDC,
2057 0x32, 0x10, 0x76, 0x54}},
2058 {"Unable to read frame data.",
2059 {
2060 // data length
2061 0x00, 0x0c,
2062 // data
2063 'h', 'e', 'l', 'l',
2064 'o', ' ', 'w', 'o',
2065 'r', 'l', 'd', '!'}},
2066 };
2067
QUICHE teama6ef0a62019-03-07 20:34:33 -05002068 PacketFragments packet46 = {
2069 // type (short header, 4 byte packet number)
2070 {"",
2071 {0x43}},
2072 // connection_id
2073 {"",
2074 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
2075 // packet number
2076 {"",
2077 {0x12, 0x34, 0x56, 0x78}},
2078 // frame type (stream frame with fin)
2079 {"",
2080 {0xFF}},
2081 // stream id
2082 {"Unable to read stream_id.",
2083 {0x01, 0x02, 0x03, 0x04}},
2084 // offset
2085 {"Unable to read offset.",
2086 {0x3A, 0x98, 0xFE, 0xDC,
2087 0x32, 0x10, 0x76, 0x54}},
2088 {"Unable to read frame data.",
2089 {
2090 // data length
2091 0x00, 0x0c,
2092 // data
2093 'h', 'e', 'l', 'l',
2094 'o', ' ', 'w', 'o',
2095 'r', 'l', 'd', '!'}},
2096 };
2097
2098 PacketFragments packet99 = {
2099 // type (short header, 4 byte packet number)
2100 {"",
2101 {0x43}},
2102 // connection_id
2103 {"",
2104 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
2105 // packet number
2106 {"",
2107 {0x12, 0x34, 0x56, 0x78}},
2108 // frame type - IETF_STREAM with FIN, LEN, and OFFSET bits set.
2109 {"",
2110 { 0x08 | 0x01 | 0x02 | 0x04 }},
2111 // stream id
2112 {"Unable to read stream_id.",
2113 {kVarInt62FourBytes + 0x01, 0x02, 0x03, 0x04}},
2114 // offset
2115 {"Unable to read stream data offset.",
2116 {kVarInt62EightBytes + 0x3A, 0x98, 0xFE, 0xDC,
2117 0x32, 0x10, 0x76, 0x54}},
2118 // data length
2119 {"Unable to read stream data length.",
2120 {kVarInt62OneByte + 0x0c}},
2121 // data
2122 {"Unable to read frame data.",
2123 { 'h', 'e', 'l', 'l',
2124 'o', ' ', 'w', 'o',
2125 'r', 'l', 'd', '!'}},
2126 };
2127 // clang-format on
2128
2129 PacketFragments& fragments =
fkastenholz305e1732019-06-18 05:01:22 -07002130 VersionHasIetfQuicFrames(framer_.transport_version())
QUICHE teama6ef0a62019-03-07 20:34:33 -05002131 ? packet99
fayang36825da2019-08-21 14:01:27 -07002132 : (framer_.transport_version() >= QUIC_VERSION_46 ? packet46
2133 : packet);
QUICHE teama6ef0a62019-03-07 20:34:33 -05002134 std::unique_ptr<QuicEncryptedPacket> encrypted(
2135 AssemblePacketFromFragments(fragments));
2136 EXPECT_TRUE(framer_.ProcessPacket(*encrypted));
2137
2138 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
2139 ASSERT_TRUE(visitor_.header_.get());
2140 EXPECT_TRUE(CheckDecryption(
2141 *encrypted, !kIncludeVersion, !kIncludeDiversificationNonce,
2142 PACKET_8BYTE_CONNECTION_ID, PACKET_0BYTE_CONNECTION_ID));
2143
2144 ASSERT_EQ(1u, visitor_.stream_frames_.size());
2145 EXPECT_EQ(0u, visitor_.ack_frames_.size());
2146 EXPECT_EQ(kStreamId, visitor_.stream_frames_[0]->stream_id);
2147 EXPECT_TRUE(visitor_.stream_frames_[0]->fin);
2148 EXPECT_EQ(kStreamOffset, visitor_.stream_frames_[0]->offset);
2149 CheckStreamFrameData("hello world!", visitor_.stream_frames_[0].get());
2150
2151 CheckFramingBoundaries(fragments, QUIC_INVALID_STREAM_DATA);
2152}
2153
2154// Test an empty (no data) stream frame.
2155TEST_P(QuicFramerTest, EmptyStreamFrame) {
2156 // Only the IETF QUIC spec explicitly says that empty
2157 // stream frames are supported.
fkastenholz305e1732019-06-18 05:01:22 -07002158 if (!VersionHasIetfQuicFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05002159 return;
2160 }
zhongyi546cc452019-04-12 15:27:49 -07002161 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
QUICHE teama6ef0a62019-03-07 20:34:33 -05002162 // clang-format off
2163 PacketFragments packet = {
2164 // type (short header, 4 byte packet number)
2165 {"",
2166 {0x43}},
2167 // connection_id
2168 {"",
2169 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
2170 // packet number
2171 {"",
2172 {0x12, 0x34, 0x56, 0x78}},
2173 // frame type - IETF_STREAM with FIN, LEN, and OFFSET bits set.
2174 {"",
2175 { 0x08 | 0x01 | 0x02 | 0x04 }},
2176 // stream id
2177 {"Unable to read stream_id.",
2178 {kVarInt62FourBytes + 0x01, 0x02, 0x03, 0x04}},
2179 // offset
2180 {"Unable to read stream data offset.",
2181 {kVarInt62EightBytes + 0x3A, 0x98, 0xFE, 0xDC,
2182 0x32, 0x10, 0x76, 0x54}},
2183 // data length
2184 {"Unable to read stream data length.",
2185 {kVarInt62OneByte + 0x00}},
2186 };
2187 // clang-format on
2188
2189 std::unique_ptr<QuicEncryptedPacket> encrypted(
2190 AssemblePacketFromFragments(packet));
2191 EXPECT_TRUE(framer_.ProcessPacket(*encrypted));
2192
2193 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
2194 ASSERT_TRUE(visitor_.header_.get());
2195 EXPECT_TRUE(CheckDecryption(
2196 *encrypted, !kIncludeVersion, !kIncludeDiversificationNonce,
2197 PACKET_8BYTE_CONNECTION_ID, PACKET_0BYTE_CONNECTION_ID));
2198
2199 ASSERT_EQ(1u, visitor_.stream_frames_.size());
2200 EXPECT_EQ(0u, visitor_.ack_frames_.size());
2201 EXPECT_EQ(kStreamId, visitor_.stream_frames_[0]->stream_id);
2202 EXPECT_TRUE(visitor_.stream_frames_[0]->fin);
2203 EXPECT_EQ(kStreamOffset, visitor_.stream_frames_[0]->offset);
2204 EXPECT_EQ(visitor_.stream_frames_[0].get()->data_length, 0u);
2205
2206 CheckFramingBoundaries(packet, QUIC_INVALID_STREAM_DATA);
2207}
2208
2209TEST_P(QuicFramerTest, MissingDiversificationNonce) {
QUICHE team8e2e4532019-03-14 14:37:56 -07002210 if (framer_.version().handshake_protocol != PROTOCOL_QUIC_CRYPTO) {
2211 // TLS does not use diversification nonces.
2212 return;
2213 }
QUICHE teama6ef0a62019-03-07 20:34:33 -05002214 QuicFramerPeer::SetPerspective(&framer_, Perspective::IS_CLIENT);
QUICHE teama6ef0a62019-03-07 20:34:33 -05002215 decrypter_ = new test::TestDecrypter();
zhongyi546cc452019-04-12 15:27:49 -07002216 if (framer_.version().KnowsWhichDecrypterToUse()) {
vasilvv0fc587f2019-09-06 13:33:08 -07002217 framer_.InstallDecrypter(
2218 ENCRYPTION_INITIAL,
2219 std::make_unique<NullDecrypter>(Perspective::IS_CLIENT));
zhongyi546cc452019-04-12 15:27:49 -07002220 framer_.InstallDecrypter(ENCRYPTION_ZERO_RTT,
2221 std::unique_ptr<QuicDecrypter>(decrypter_));
2222 } else {
vasilvv0fc587f2019-09-06 13:33:08 -07002223 framer_.SetDecrypter(ENCRYPTION_INITIAL, std::make_unique<NullDecrypter>(
2224 Perspective::IS_CLIENT));
zhongyi546cc452019-04-12 15:27:49 -07002225 framer_.SetAlternativeDecrypter(
2226 ENCRYPTION_ZERO_RTT, std::unique_ptr<QuicDecrypter>(decrypter_), false);
2227 }
QUICHE teama6ef0a62019-03-07 20:34:33 -05002228
2229 // clang-format off
2230 unsigned char packet[] = {
2231 // public flags (8 byte connection_id)
2232 0x28,
2233 // connection_id
2234 0x10, 0x32, 0x54, 0x76, 0x98, 0xBA, 0xDC, 0xFE,
2235 // packet number
2236 0x12, 0x34, 0x56, 0x78,
QUICHE team8e2e4532019-03-14 14:37:56 -07002237 // padding frame
2238 0x00,
QUICHE teama6ef0a62019-03-07 20:34:33 -05002239 };
2240
QUICHE team8e2e4532019-03-14 14:37:56 -07002241 unsigned char packet46[] = {
2242 // type (long header, ZERO_RTT_PROTECTED, 4-byte packet number)
2243 0xD3,
2244 // version tag
2245 QUIC_VERSION_BYTES,
2246 // connection_id length
2247 0x05,
2248 // connection_id
2249 0x10, 0x32, 0x54, 0x76, 0x98, 0xBA, 0xDC, 0xFE,
2250 // packet number
2251 0x12, 0x34, 0x56, 0x78,
2252 // padding frame
2253 0x00,
2254 };
2255
2256 unsigned char packet99[] = {
2257 // type (long header, ZERO_RTT_PROTECTED, 4-byte packet number)
2258 0xD3,
2259 // version tag
2260 QUIC_VERSION_BYTES,
dschinazi48ac9192019-07-31 00:07:26 -07002261 // destination connection ID length
2262 0x00,
2263 // source connection ID length
2264 0x08,
2265 // source connection ID
QUICHE team8e2e4532019-03-14 14:37:56 -07002266 0x10, 0x32, 0x54, 0x76, 0x98, 0xBA, 0xDC, 0xFE,
2267 // IETF long header payload length
2268 0x05,
2269 // packet number
2270 0x12, 0x34, 0x56, 0x78,
2271 // padding frame
2272 0x00,
QUICHE teama6ef0a62019-03-07 20:34:33 -05002273 };
2274 // clang-format on
2275
2276 unsigned char* p = packet;
QUICHE team8e2e4532019-03-14 14:37:56 -07002277 size_t p_length = QUIC_ARRAYSIZE(packet);
2278 if (framer_.transport_version() == QUIC_VERSION_99) {
2279 p = packet99;
2280 p_length = QUIC_ARRAYSIZE(packet99);
fayangf36e29d2019-06-06 14:03:40 -07002281 } else if (framer_.transport_version() >= QUIC_VERSION_46) {
QUICHE team8e2e4532019-03-14 14:37:56 -07002282 p = packet46;
2283 p_length = QUIC_ARRAYSIZE(packet46);
QUICHE teama6ef0a62019-03-07 20:34:33 -05002284 }
QUICHE team8e2e4532019-03-14 14:37:56 -07002285 QuicEncryptedPacket encrypted(AsChars(p), p_length, false);
QUICHE teama6ef0a62019-03-07 20:34:33 -05002286 EXPECT_FALSE(framer_.ProcessPacket(encrypted));
nharper55fa6132019-05-07 19:37:21 -07002287 if (framer_.version().HasHeaderProtection()) {
2288 EXPECT_EQ(QUIC_DECRYPTION_FAILURE, framer_.error());
2289 EXPECT_EQ("Unable to decrypt header protection.", framer_.detailed_error());
fayang36825da2019-08-21 14:01:27 -07002290 } else if (framer_.transport_version() >= QUIC_VERSION_46) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05002291 // Cannot read diversification nonce.
2292 EXPECT_EQ(QUIC_INVALID_PACKET_HEADER, framer_.error());
QUICHE team8e2e4532019-03-14 14:37:56 -07002293 EXPECT_EQ("Unable to read nonce.", framer_.detailed_error());
QUICHE teama6ef0a62019-03-07 20:34:33 -05002294 } else {
2295 EXPECT_EQ(QUIC_DECRYPTION_FAILURE, framer_.error());
2296 }
2297}
2298
2299TEST_P(QuicFramerTest, StreamFrame3ByteStreamId) {
2300 if (framer_.transport_version() > QUIC_VERSION_43) {
2301 // This test is nonsensical for IETF Quic.
2302 return;
2303 }
2304 // clang-format off
2305 PacketFragments packet = {
2306 // public flags (8 byte connection_id)
2307 {"",
2308 {0x28}},
2309 // connection_id
2310 {"",
2311 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
2312 // packet number
2313 {"",
2314 {0x12, 0x34, 0x56, 0x78}},
2315 // frame type (stream frame with fin)
2316 {"",
2317 {0xFE}},
2318 // stream id
2319 {"Unable to read stream_id.",
2320 {0x02, 0x03, 0x04}},
2321 // offset
2322 {"Unable to read offset.",
2323 {0x3A, 0x98, 0xFE, 0xDC,
2324 0x32, 0x10, 0x76, 0x54}},
2325 {"Unable to read frame data.",
2326 {
2327 // data length
2328 0x00, 0x0c,
2329 // data
2330 'h', 'e', 'l', 'l',
2331 'o', ' ', 'w', 'o',
2332 'r', 'l', 'd', '!'}},
2333 };
2334 // clang-format on
2335
2336 PacketFragments& fragments = packet;
2337 std::unique_ptr<QuicEncryptedPacket> encrypted(
2338 AssemblePacketFromFragments(fragments));
2339 EXPECT_TRUE(framer_.ProcessPacket(*encrypted));
2340
2341 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
2342 ASSERT_TRUE(visitor_.header_.get());
2343 EXPECT_TRUE(CheckDecryption(
2344 *encrypted, !kIncludeVersion, !kIncludeDiversificationNonce,
2345 PACKET_8BYTE_CONNECTION_ID, PACKET_0BYTE_CONNECTION_ID));
2346
2347 ASSERT_EQ(1u, visitor_.stream_frames_.size());
2348 EXPECT_EQ(0u, visitor_.ack_frames_.size());
2349 // Stream ID should be the last 3 bytes of kStreamId.
2350 EXPECT_EQ(0x00FFFFFF & kStreamId, visitor_.stream_frames_[0]->stream_id);
2351 EXPECT_TRUE(visitor_.stream_frames_[0]->fin);
2352 EXPECT_EQ(kStreamOffset, visitor_.stream_frames_[0]->offset);
2353 CheckStreamFrameData("hello world!", visitor_.stream_frames_[0].get());
2354
2355 CheckFramingBoundaries(fragments, QUIC_INVALID_STREAM_DATA);
2356}
2357
2358TEST_P(QuicFramerTest, StreamFrame2ByteStreamId) {
zhongyi546cc452019-04-12 15:27:49 -07002359 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
QUICHE teama6ef0a62019-03-07 20:34:33 -05002360 // clang-format off
2361 PacketFragments packet = {
2362 // public flags (8 byte connection_id)
2363 {"",
2364 {0x28}},
2365 // connection_id
2366 {"",
2367 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
2368 // packet number
2369 {"",
2370 {0x12, 0x34, 0x56, 0x78}},
2371 // frame type (stream frame with fin)
2372 {"",
2373 {0xFD}},
2374 // stream id
2375 {"Unable to read stream_id.",
2376 {0x03, 0x04}},
2377 // offset
2378 {"Unable to read offset.",
2379 {0x3A, 0x98, 0xFE, 0xDC,
2380 0x32, 0x10, 0x76, 0x54}},
2381 {"Unable to read frame data.",
2382 {
2383 // data length
2384 0x00, 0x0c,
2385 // data
2386 'h', 'e', 'l', 'l',
2387 'o', ' ', 'w', 'o',
2388 'r', 'l', 'd', '!'}},
2389 };
2390
QUICHE teama6ef0a62019-03-07 20:34:33 -05002391 PacketFragments packet46 = {
2392 // type (short header, 4 byte packet number)
2393 {"",
2394 {0x43}},
2395 // connection_id
2396 {"",
2397 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
2398 // packet number
2399 {"",
2400 {0x12, 0x34, 0x56, 0x78}},
2401 // frame type (stream frame with fin)
2402 {"",
2403 {0xFD}},
2404 // stream id
2405 {"Unable to read stream_id.",
2406 {0x03, 0x04}},
2407 // offset
2408 {"Unable to read offset.",
2409 {0x3A, 0x98, 0xFE, 0xDC,
2410 0x32, 0x10, 0x76, 0x54}},
2411 {"Unable to read frame data.",
2412 {
2413 // data length
2414 0x00, 0x0c,
2415 // data
2416 'h', 'e', 'l', 'l',
2417 'o', ' ', 'w', 'o',
2418 'r', 'l', 'd', '!'}},
2419 };
2420
2421 PacketFragments packet99 = {
2422 // type (short header, 4 byte packet number)
2423 {"",
2424 {0x43}},
2425 // connection_id
2426 {"",
2427 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
2428 // packet number
2429 {"",
2430 {0x12, 0x34, 0x56, 0x78}},
2431 // frame type (IETF_STREAM frame with LEN, FIN, and OFFSET bits set)
2432 {"",
2433 {0x08 | 0x01 | 0x02 | 0x04}},
2434 // stream id
2435 {"Unable to read stream_id.",
2436 {kVarInt62TwoBytes + 0x03, 0x04}},
2437 // offset
2438 {"Unable to read stream data offset.",
2439 {kVarInt62EightBytes + 0x3A, 0x98, 0xFE, 0xDC,
2440 0x32, 0x10, 0x76, 0x54}},
2441 // data length
2442 {"Unable to read stream data length.",
2443 {kVarInt62OneByte + 0x0c}},
2444 // data
2445 {"Unable to read frame data.",
2446 { 'h', 'e', 'l', 'l',
2447 'o', ' ', 'w', 'o',
2448 'r', 'l', 'd', '!'}},
2449 };
2450 // clang-format on
2451
2452 PacketFragments& fragments =
fkastenholz305e1732019-06-18 05:01:22 -07002453 VersionHasIetfQuicFrames(framer_.transport_version())
QUICHE teama6ef0a62019-03-07 20:34:33 -05002454 ? packet99
fayang36825da2019-08-21 14:01:27 -07002455 : (framer_.transport_version() >= QUIC_VERSION_46 ? packet46
2456 : packet);
QUICHE teama6ef0a62019-03-07 20:34:33 -05002457 std::unique_ptr<QuicEncryptedPacket> encrypted(
2458 AssemblePacketFromFragments(fragments));
2459 EXPECT_TRUE(framer_.ProcessPacket(*encrypted));
2460
2461 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
2462 ASSERT_TRUE(visitor_.header_.get());
2463 EXPECT_TRUE(CheckDecryption(
2464 *encrypted, !kIncludeVersion, !kIncludeDiversificationNonce,
2465 PACKET_8BYTE_CONNECTION_ID, PACKET_0BYTE_CONNECTION_ID));
2466
2467 ASSERT_EQ(1u, visitor_.stream_frames_.size());
2468 EXPECT_EQ(0u, visitor_.ack_frames_.size());
2469 // Stream ID should be the last 2 bytes of kStreamId.
2470 EXPECT_EQ(0x0000FFFF & kStreamId, visitor_.stream_frames_[0]->stream_id);
2471 EXPECT_TRUE(visitor_.stream_frames_[0]->fin);
2472 EXPECT_EQ(kStreamOffset, visitor_.stream_frames_[0]->offset);
2473 CheckStreamFrameData("hello world!", visitor_.stream_frames_[0].get());
2474
2475 CheckFramingBoundaries(fragments, QUIC_INVALID_STREAM_DATA);
2476}
2477
2478TEST_P(QuicFramerTest, StreamFrame1ByteStreamId) {
zhongyi546cc452019-04-12 15:27:49 -07002479 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
QUICHE teama6ef0a62019-03-07 20:34:33 -05002480 // clang-format off
2481 PacketFragments packet = {
2482 // public flags (8 byte connection_id)
2483 {"",
2484 {0x28}},
2485 // connection_id
2486 {"",
2487 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
2488 // packet number
2489 {"",
2490 {0x12, 0x34, 0x56, 0x78}},
2491 // frame type (stream frame with fin)
2492 {"",
2493 {0xFC}},
2494 // stream id
2495 {"Unable to read stream_id.",
2496 {0x04}},
2497 // offset
2498 {"Unable to read offset.",
2499 {0x3A, 0x98, 0xFE, 0xDC,
2500 0x32, 0x10, 0x76, 0x54}},
2501 {"Unable to read frame data.",
2502 {
2503 // data length
2504 0x00, 0x0c,
2505 // data
2506 'h', 'e', 'l', 'l',
2507 'o', ' ', 'w', 'o',
2508 'r', 'l', 'd', '!'}},
2509 };
2510
QUICHE teama6ef0a62019-03-07 20:34:33 -05002511 PacketFragments packet46 = {
2512 // type (short header, 4 byte packet number)
2513 {"",
2514 {0x43}},
2515 // connection_id
2516 {"",
2517 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
2518 // packet number
2519 {"",
2520 {0x12, 0x34, 0x56, 0x78}},
2521 // frame type (stream frame with fin)
2522 {"",
2523 {0xFC}},
2524 // stream id
2525 {"Unable to read stream_id.",
2526 {0x04}},
2527 // offset
2528 {"Unable to read offset.",
2529 {0x3A, 0x98, 0xFE, 0xDC,
2530 0x32, 0x10, 0x76, 0x54}},
2531 {"Unable to read frame data.",
2532 {
2533 // data length
2534 0x00, 0x0c,
2535 // data
2536 'h', 'e', 'l', 'l',
2537 'o', ' ', 'w', 'o',
2538 'r', 'l', 'd', '!'}},
2539 };
2540
2541 PacketFragments packet99 = {
2542 // type (short header, 4 byte packet number)
2543 {"",
2544 {0x43}},
2545 // connection_id
2546 {"",
2547 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
2548 // packet number
2549 {"",
2550 {0x12, 0x34, 0x56, 0x78}},
2551 // frame type (IETF_STREAM frame with LEN, FIN, and OFFSET bits set)
2552 {"",
2553 {0x08 | 0x01 | 0x02 | 0x04}},
2554 // stream id
2555 {"Unable to read stream_id.",
2556 {kVarInt62OneByte + 0x04}},
2557 // offset
2558 {"Unable to read stream data offset.",
2559 {kVarInt62EightBytes + 0x3A, 0x98, 0xFE, 0xDC,
2560 0x32, 0x10, 0x76, 0x54}},
2561 // data length
2562 {"Unable to read stream data length.",
2563 {kVarInt62OneByte + 0x0c}},
2564 // data
2565 {"Unable to read frame data.",
2566 { 'h', 'e', 'l', 'l',
2567 'o', ' ', 'w', 'o',
2568 'r', 'l', 'd', '!'}},
2569 };
2570 // clang-format on
2571
2572 PacketFragments& fragments =
fkastenholz305e1732019-06-18 05:01:22 -07002573 VersionHasIetfQuicFrames(framer_.transport_version())
QUICHE teama6ef0a62019-03-07 20:34:33 -05002574 ? packet99
fayang36825da2019-08-21 14:01:27 -07002575 : (framer_.transport_version() >= QUIC_VERSION_46 ? packet46
2576 : packet);
QUICHE teama6ef0a62019-03-07 20:34:33 -05002577 std::unique_ptr<QuicEncryptedPacket> encrypted(
2578 AssemblePacketFromFragments(fragments));
2579 EXPECT_TRUE(framer_.ProcessPacket(*encrypted));
2580
2581 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
2582 ASSERT_TRUE(visitor_.header_.get());
2583 EXPECT_TRUE(CheckDecryption(
2584 *encrypted, !kIncludeVersion, !kIncludeDiversificationNonce,
2585 PACKET_8BYTE_CONNECTION_ID, PACKET_0BYTE_CONNECTION_ID));
2586
2587 ASSERT_EQ(1u, visitor_.stream_frames_.size());
2588 EXPECT_EQ(0u, visitor_.ack_frames_.size());
2589 // Stream ID should be the last 1 byte of kStreamId.
2590 EXPECT_EQ(0x000000FF & kStreamId, visitor_.stream_frames_[0]->stream_id);
2591 EXPECT_TRUE(visitor_.stream_frames_[0]->fin);
2592 EXPECT_EQ(kStreamOffset, visitor_.stream_frames_[0]->offset);
2593 CheckStreamFrameData("hello world!", visitor_.stream_frames_[0].get());
2594
2595 CheckFramingBoundaries(fragments, QUIC_INVALID_STREAM_DATA);
2596}
2597
2598TEST_P(QuicFramerTest, StreamFrameWithVersion) {
fkastenholz305e1732019-06-18 05:01:22 -07002599 // If IETF frames are in use then we must also have the IETF
2600 // header invariants.
2601 if (VersionHasIetfQuicFrames(framer_.transport_version())) {
2602 DCHECK(VersionHasIetfInvariantHeader(framer_.transport_version()));
2603 }
2604
zhongyi546cc452019-04-12 15:27:49 -07002605 SetDecrypterLevel(ENCRYPTION_ZERO_RTT);
QUICHE teama6ef0a62019-03-07 20:34:33 -05002606 // clang-format off
2607 PacketFragments packet = {
2608 // public flags (version, 8 byte connection_id)
2609 {"",
2610 {0x29}},
2611 // connection_id
2612 {"",
2613 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
2614 // version tag
2615 {"",
2616 {QUIC_VERSION_BYTES}},
2617 // packet number
2618 {"",
2619 {0x12, 0x34, 0x56, 0x78}},
2620 // frame type (stream frame with fin)
2621 {"",
2622 {0xFE}},
2623 // stream id
2624 {"Unable to read stream_id.",
2625 {0x02, 0x03, 0x04}},
2626 // offset
2627 {"Unable to read offset.",
2628 {0x3A, 0x98, 0xFE, 0xDC,
2629 0x32, 0x10, 0x76, 0x54}},
2630 {"Unable to read frame data.",
2631 {
2632 // data length
2633 0x00, 0x0c,
2634 // data
2635 'h', 'e', 'l', 'l',
2636 'o', ' ', 'w', 'o',
2637 'r', 'l', 'd', '!'}},
2638 };
2639
QUICHE teama6ef0a62019-03-07 20:34:33 -05002640 PacketFragments packet46 = {
2641 // public flags (long header with packet type ZERO_RTT_PROTECTED and
2642 // 4-byte packet number)
2643 {"",
2644 {0xD3}},
2645 // version tag
2646 {"",
2647 {QUIC_VERSION_BYTES}},
2648 // connection_id length
2649 {"",
2650 {0x50}},
2651 // connection_id
2652 {"",
2653 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
2654 // packet number
2655 {"",
2656 {0x12, 0x34, 0x56, 0x78}},
2657 // frame type (stream frame with fin)
2658 {"",
2659 {0xFE}},
2660 // stream id
2661 {"Unable to read stream_id.",
2662 {0x02, 0x03, 0x04}},
2663 // offset
2664 {"Unable to read offset.",
2665 {0x3A, 0x98, 0xFE, 0xDC,
2666 0x32, 0x10, 0x76, 0x54}},
2667 {"Unable to read frame data.",
2668 {
2669 // data length
2670 0x00, 0x0c,
2671 // data
2672 'h', 'e', 'l', 'l',
2673 'o', ' ', 'w', 'o',
2674 'r', 'l', 'd', '!'}},
2675 };
2676
2677 PacketFragments packet99 = {
2678 // public flags (long header with packet type ZERO_RTT_PROTECTED and
2679 // 4-byte packet number)
2680 {"",
2681 {0xD3}},
2682 // version tag
2683 {"",
2684 {QUIC_VERSION_BYTES}},
dschinazi48ac9192019-07-31 00:07:26 -07002685 // destination connection ID length
QUICHE teama6ef0a62019-03-07 20:34:33 -05002686 {"",
dschinazi48ac9192019-07-31 00:07:26 -07002687 {0x08}},
2688 // destination connection ID
QUICHE teama6ef0a62019-03-07 20:34:33 -05002689 {"",
2690 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
dschinazi48ac9192019-07-31 00:07:26 -07002691 // source connection ID length
2692 {"",
2693 {0x00}},
QUICHE teama6ef0a62019-03-07 20:34:33 -05002694 // long header packet length
2695 {"",
2696 {0x1E}},
2697 // packet number
2698 {"",
2699 {0x12, 0x34, 0x56, 0x78}},
2700 // frame type (IETF_STREAM frame with FIN, LEN, and OFFSET bits set)
2701 {"",
2702 {0x08 | 0x01 | 0x02 | 0x04}},
2703 // stream id
2704 {"Long header payload length longer than packet.",
2705 {kVarInt62FourBytes + 0x00, 0x02, 0x03, 0x04}},
2706 // offset
2707 {"Long header payload length longer than packet.",
2708 {kVarInt62EightBytes + 0x3A, 0x98, 0xFE, 0xDC,
2709 0x32, 0x10, 0x76, 0x54}},
2710 // data length
2711 {"Long header payload length longer than packet.",
2712 {kVarInt62OneByte + 0x0c}},
2713 // data
2714 {"Long header payload length longer than packet.",
2715 { 'h', 'e', 'l', 'l',
2716 'o', ' ', 'w', 'o',
2717 'r', 'l', 'd', '!'}},
2718 };
2719 // clang-format on
2720
2721 QuicVariableLengthIntegerLength retry_token_length_length =
2722 VARIABLE_LENGTH_INTEGER_LENGTH_0;
2723 size_t retry_token_length = 0;
2724 QuicVariableLengthIntegerLength length_length =
2725 QuicVersionHasLongHeaderLengths(framer_.transport_version())
2726 ? VARIABLE_LENGTH_INTEGER_LENGTH_1
2727 : VARIABLE_LENGTH_INTEGER_LENGTH_0;
2728
2729 PacketFragments& fragments =
fkastenholz305e1732019-06-18 05:01:22 -07002730 VersionHasIetfQuicFrames(framer_.transport_version())
QUICHE teama6ef0a62019-03-07 20:34:33 -05002731 ? packet99
fayang36825da2019-08-21 14:01:27 -07002732 : (framer_.transport_version() >= QUIC_VERSION_46 ? packet46
2733 : packet);
QUICHE teama6ef0a62019-03-07 20:34:33 -05002734 std::unique_ptr<QuicEncryptedPacket> encrypted(
2735 AssemblePacketFromFragments(fragments));
2736 EXPECT_TRUE(framer_.ProcessPacket(*encrypted));
2737
2738 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
2739 ASSERT_TRUE(visitor_.header_.get());
2740 EXPECT_TRUE(CheckDecryption(
2741 *encrypted, kIncludeVersion, !kIncludeDiversificationNonce,
2742 PACKET_8BYTE_CONNECTION_ID, PACKET_0BYTE_CONNECTION_ID,
2743 retry_token_length_length, retry_token_length, length_length));
2744
2745 ASSERT_EQ(1u, visitor_.stream_frames_.size());
2746 EXPECT_EQ(0u, visitor_.ack_frames_.size());
2747 // Stream ID should be the last 3 bytes of kStreamId.
2748 EXPECT_EQ(0x00FFFFFF & kStreamId, visitor_.stream_frames_[0]->stream_id);
2749 EXPECT_TRUE(visitor_.stream_frames_[0]->fin);
2750 EXPECT_EQ(kStreamOffset, visitor_.stream_frames_[0]->offset);
2751 CheckStreamFrameData("hello world!", visitor_.stream_frames_[0].get());
2752
2753 CheckFramingBoundaries(fragments,
2754 framer_.transport_version() == QUIC_VERSION_99
2755 ? QUIC_INVALID_PACKET_HEADER
2756 : QUIC_INVALID_STREAM_DATA);
2757}
2758
2759TEST_P(QuicFramerTest, RejectPacket) {
zhongyi546cc452019-04-12 15:27:49 -07002760 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
QUICHE teama6ef0a62019-03-07 20:34:33 -05002761 visitor_.accept_packet_ = false;
2762
2763 // clang-format off
2764 unsigned char packet[] = {
2765 // public flags (8 byte connection_id)
2766 0x28,
2767 // connection_id
2768 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
2769 // packet number
2770 0x12, 0x34, 0x56, 0x78,
2771
2772 // frame type (stream frame with fin)
2773 0xFF,
2774 // stream id
2775 0x01, 0x02, 0x03, 0x04,
2776 // offset
2777 0x3A, 0x98, 0xFE, 0xDC,
2778 0x32, 0x10, 0x76, 0x54,
2779 // data length
2780 0x00, 0x0c,
2781 // data
2782 'h', 'e', 'l', 'l',
2783 'o', ' ', 'w', 'o',
2784 'r', 'l', 'd', '!',
2785 };
2786
QUICHE teama6ef0a62019-03-07 20:34:33 -05002787 unsigned char packet46[] = {
2788 // type (short header, 4 byte packet number)
2789 0x43,
2790 // connection_id
2791 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
2792 // packet number
2793 0x12, 0x34, 0x56, 0x78,
2794
2795 // frame type (STREAM Frame with FIN, LEN, and OFFSET bits set)
2796 0x10 | 0x01 | 0x02 | 0x04,
2797 // stream id
2798 kVarInt62FourBytes + 0x01, 0x02, 0x03, 0x04,
2799 // offset
2800 kVarInt62EightBytes + 0x3A, 0x98, 0xFE, 0xDC,
2801 0x32, 0x10, 0x76, 0x54,
2802 // data length
2803 kVarInt62OneByte + 0x0c,
2804 // data
2805 'h', 'e', 'l', 'l',
2806 'o', ' ', 'w', 'o',
2807 'r', 'l', 'd', '!',
2808 };
2809 // clang-format on
2810
2811 unsigned char* p = packet;
fayang36825da2019-08-21 14:01:27 -07002812 if (framer_.transport_version() >= QUIC_VERSION_46) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05002813 p = packet46;
QUICHE teama6ef0a62019-03-07 20:34:33 -05002814 }
2815 QuicEncryptedPacket encrypted(AsChars(p),
2816 framer_.transport_version() > QUIC_VERSION_43
fayang36825da2019-08-21 14:01:27 -07002817 ? QUIC_ARRAYSIZE(packet46)
QUICHE teama6ef0a62019-03-07 20:34:33 -05002818 : QUIC_ARRAYSIZE(packet),
2819 false);
2820 EXPECT_TRUE(framer_.ProcessPacket(encrypted));
2821
2822 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
2823 ASSERT_TRUE(visitor_.header_.get());
2824 EXPECT_TRUE(CheckDecryption(
2825 encrypted, !kIncludeVersion, !kIncludeDiversificationNonce,
2826 PACKET_8BYTE_CONNECTION_ID, PACKET_0BYTE_CONNECTION_ID));
2827
2828 ASSERT_EQ(0u, visitor_.stream_frames_.size());
2829 EXPECT_EQ(0u, visitor_.ack_frames_.size());
2830}
2831
2832TEST_P(QuicFramerTest, RejectPublicHeader) {
2833 visitor_.accept_public_header_ = false;
2834
2835 // clang-format off
2836 unsigned char packet[] = {
2837 // public flags (8 byte connection_id)
2838 0x28,
2839 // connection_id
2840 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
2841 };
2842
QUICHE teama6ef0a62019-03-07 20:34:33 -05002843 unsigned char packet46[] = {
2844 // type (short header, 1 byte packet number)
2845 0x40,
2846 // connection_id
2847 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
2848 // packet number
2849 0x01,
2850 };
2851 // clang-format on
2852
2853 QuicEncryptedPacket encrypted(
fayang36825da2019-08-21 14:01:27 -07002854 framer_.transport_version() >= QUIC_VERSION_46 ? AsChars(packet46)
2855 : AsChars(packet),
2856 framer_.transport_version() >= QUIC_VERSION_46 ? QUIC_ARRAYSIZE(packet46)
2857 : QUIC_ARRAYSIZE(packet),
QUICHE teama6ef0a62019-03-07 20:34:33 -05002858 false);
2859 EXPECT_TRUE(framer_.ProcessPacket(encrypted));
2860
2861 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
2862 ASSERT_TRUE(visitor_.header_.get());
2863 EXPECT_FALSE(visitor_.header_->packet_number.IsInitialized());
2864}
2865
2866TEST_P(QuicFramerTest, AckFrameOneAckBlock) {
zhongyi546cc452019-04-12 15:27:49 -07002867 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
QUICHE teama6ef0a62019-03-07 20:34:33 -05002868 // clang-format off
2869 PacketFragments packet = {
2870 // public flags (8 byte connection_id)
2871 {"",
2872 {0x2C}},
2873 // connection_id
2874 {"",
2875 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
2876 // packet number
2877 {"",
2878 {0x12, 0x34, 0x56, 0x78}},
2879 // frame type (ack frame)
2880 // (one ack block, 2 byte largest observed, 2 byte block length)
2881 {"",
2882 {0x45}},
2883 // largest acked
2884 {"Unable to read largest acked.",
2885 {0x12, 0x34}},
2886 // Zero delta time.
2887 {"Unable to read ack delay time.",
2888 {0x00, 0x00}},
2889 // first ack block length.
2890 {"Unable to read first ack block length.",
2891 {0x12, 0x34}},
2892 // num timestamps.
2893 {"Unable to read num received packets.",
2894 {0x00}}
2895 };
2896
QUICHE teama6ef0a62019-03-07 20:34:33 -05002897 PacketFragments packet46 = {
2898 // type (short packet, 4 byte packet number)
2899 {"",
2900 {0x43}},
2901 // connection_id
2902 {"",
2903 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
2904 // packet number
2905 {"",
2906 {0x12, 0x34, 0x56, 0x78}},
2907 // frame type (ack frame)
2908 // (one ack block, 2 byte largest observed, 2 byte block length)
2909 {"",
2910 {0x45}},
2911 // largest acked
2912 {"Unable to read largest acked.",
2913 {0x12, 0x34}},
2914 // Zero delta time.
2915 {"Unable to read ack delay time.",
2916 {0x00, 0x00}},
2917 // first ack block length.
2918 {"Unable to read first ack block length.",
2919 {0x12, 0x34}},
2920 // num timestamps.
2921 {"Unable to read num received packets.",
2922 {0x00}}
2923 };
2924
2925 PacketFragments packet99 = {
2926 // type (short packet, 4 byte packet number)
2927 {"",
2928 {0x43}},
2929 // connection_id
2930 {"",
2931 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
2932 // packet number
2933 {"",
2934 {0x12, 0x34, 0x56, 0x78}},
2935 // frame type (IETF_ACK)
2936 // (one ack block, 2 byte largest observed, 2 byte block length)
2937 // IETF-Quic ignores the bit-fields in the ack type, all of
2938 // that information is encoded elsewhere in the frame.
2939 {"",
2940 {0x02}},
2941 // largest acked
2942 {"Unable to read largest acked.",
2943 {kVarInt62TwoBytes + 0x12, 0x34}},
2944 // Zero delta time.
2945 {"Unable to read ack delay time.",
2946 {kVarInt62OneByte + 0x00}},
2947 // Ack block count (0 -- no blocks after the first)
2948 {"Unable to read ack block count.",
2949 {kVarInt62OneByte + 0x00}},
2950 // first ack block length - 1.
2951 // IETF Quic defines the ack block's value as the "number of
2952 // packets that preceed the largest packet number in the block"
2953 // which for the 1st ack block is the largest acked field,
2954 // above. This means that if we are acking just packet 0x1234
2955 // then the 1st ack block will be 0.
2956 {"Unable to read first ack block length.",
2957 {kVarInt62TwoBytes + 0x12, 0x33}}
2958 };
2959 // clang-format on
2960
2961 PacketFragments& fragments =
fkastenholz305e1732019-06-18 05:01:22 -07002962 VersionHasIetfQuicFrames(framer_.transport_version())
QUICHE teama6ef0a62019-03-07 20:34:33 -05002963 ? packet99
fayang36825da2019-08-21 14:01:27 -07002964 : (framer_.transport_version() >= QUIC_VERSION_46 ? packet46
2965 : packet);
QUICHE teama6ef0a62019-03-07 20:34:33 -05002966 std::unique_ptr<QuicEncryptedPacket> encrypted(
2967 AssemblePacketFromFragments(fragments));
2968 EXPECT_TRUE(framer_.ProcessPacket(*encrypted));
2969
2970 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
2971 ASSERT_TRUE(visitor_.header_.get());
2972 EXPECT_TRUE(CheckDecryption(
2973 *encrypted, !kIncludeVersion, !kIncludeDiversificationNonce,
2974 PACKET_8BYTE_CONNECTION_ID, PACKET_0BYTE_CONNECTION_ID));
2975
2976 EXPECT_EQ(0u, visitor_.stream_frames_.size());
2977 ASSERT_EQ(1u, visitor_.ack_frames_.size());
2978 const QuicAckFrame& frame = *visitor_.ack_frames_[0];
2979 EXPECT_EQ(kSmallLargestObserved, LargestAcked(frame));
2980 ASSERT_EQ(4660u, frame.packets.NumPacketsSlow());
2981
2982 CheckFramingBoundaries(fragments, QUIC_INVALID_ACK_DATA);
2983}
2984
2985// This test checks that the ack frame processor correctly identifies
2986// and handles the case where the first ack block is larger than the
2987// largest_acked packet.
2988TEST_P(QuicFramerTest, FirstAckFrameUnderflow) {
zhongyi546cc452019-04-12 15:27:49 -07002989 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
QUICHE teama6ef0a62019-03-07 20:34:33 -05002990 // clang-format off
2991 PacketFragments packet = {
2992 // public flags (8 byte connection_id)
2993 {"",
2994 {0x2C}},
2995 // connection_id
2996 {"",
2997 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
2998 // packet number
2999 {"",
3000 {0x12, 0x34, 0x56, 0x78}},
3001 // frame type (ack frame)
3002 // (one ack block, 2 byte largest observed, 2 byte block length)
3003 {"",
3004 {0x45}},
3005 // largest acked
3006 {"Unable to read largest acked.",
3007 {0x12, 0x34}},
3008 // Zero delta time.
3009 {"Unable to read ack delay time.",
3010 {0x00, 0x00}},
3011 // first ack block length.
3012 {"Unable to read first ack block length.",
3013 {0x88, 0x88}},
3014 // num timestamps.
3015 {"Underflow with first ack block length 34952 largest acked is 4660.",
3016 {0x00}}
3017 };
3018
QUICHE teama6ef0a62019-03-07 20:34:33 -05003019 PacketFragments packet46 = {
3020 // type (short header, 4 byte packet number)
3021 {"",
3022 {0x43}},
3023 // connection_id
3024 {"",
3025 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
3026 // packet number
3027 {"",
3028 {0x12, 0x34, 0x56, 0x78}},
3029 // frame type (ack frame)
3030 // (one ack block, 2 byte largest observed, 2 byte block length)
3031 {"",
3032 {0x45}},
3033 // largest acked
3034 {"Unable to read largest acked.",
3035 {0x12, 0x34}},
3036 // Zero delta time.
3037 {"Unable to read ack delay time.",
3038 {0x00, 0x00}},
3039 // first ack block length.
3040 {"Unable to read first ack block length.",
3041 {0x88, 0x88}},
3042 // num timestamps.
3043 {"Underflow with first ack block length 34952 largest acked is 4660.",
3044 {0x00}}
3045 };
3046
3047 PacketFragments packet99 = {
3048 // type (short header, 4 byte packet number)
3049 {"",
3050 {0x43}},
3051 // connection_id
3052 {"",
3053 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
3054 // packet number
3055 {"",
3056 {0x12, 0x34, 0x56, 0x78}},
3057 // frame type (IETF_ACK)
3058 {"",
3059 {0x02}},
3060 // largest acked
3061 {"Unable to read largest acked.",
3062 {kVarInt62TwoBytes + 0x12, 0x34}},
3063 // Zero delta time.
3064 {"Unable to read ack delay time.",
3065 {kVarInt62OneByte + 0x00}},
3066 // Ack block count (0 -- no blocks after the first)
3067 {"Unable to read ack block count.",
3068 {kVarInt62OneByte + 0x00}},
3069 // first ack block length.
3070 {"Unable to read first ack block length.",
3071 {kVarInt62TwoBytes + 0x28, 0x88}}
3072 };
3073 // clang-format on
3074
3075 PacketFragments& fragments =
fkastenholz305e1732019-06-18 05:01:22 -07003076 VersionHasIetfQuicFrames(framer_.transport_version())
QUICHE teama6ef0a62019-03-07 20:34:33 -05003077 ? packet99
fayang36825da2019-08-21 14:01:27 -07003078 : (framer_.transport_version() >= QUIC_VERSION_46 ? packet46
3079 : packet);
QUICHE teama6ef0a62019-03-07 20:34:33 -05003080 std::unique_ptr<QuicEncryptedPacket> encrypted(
3081 AssemblePacketFromFragments(fragments));
3082 EXPECT_FALSE(framer_.ProcessPacket(*encrypted));
3083 CheckFramingBoundaries(fragments, QUIC_INVALID_ACK_DATA);
3084}
3085
3086// This test checks that the ack frame processor correctly identifies
3087// and handles the case where the third ack block's gap is larger than the
3088// available space in the ack range.
3089TEST_P(QuicFramerTest, ThirdAckBlockUnderflowGap) {
fkastenholz305e1732019-06-18 05:01:22 -07003090 if (!VersionHasIetfQuicFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05003091 // for now, only v99
3092 return;
3093 }
zhongyi546cc452019-04-12 15:27:49 -07003094 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
QUICHE teama6ef0a62019-03-07 20:34:33 -05003095 // clang-format off
3096 PacketFragments packet99 = {
3097 // type (short header, 4 byte packet number)
3098 {"",
3099 {0x43}},
3100 // connection_id
3101 {"",
3102 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
3103 // packet number
3104 {"",
3105 {0x12, 0x34, 0x56, 0x78}},
3106 // frame type (IETF_ACK frame)
3107 {"",
3108 {0x02}},
3109 // largest acked
3110 {"Unable to read largest acked.",
3111 {kVarInt62OneByte + 63}},
3112 // Zero delta time.
3113 {"Unable to read ack delay time.",
3114 {kVarInt62OneByte + 0x00}},
3115 // Ack block count (2 -- 2 blocks after the first)
3116 {"Unable to read ack block count.",
3117 {kVarInt62OneByte + 0x02}},
3118 // first ack block length.
3119 {"Unable to read first ack block length.",
3120 {kVarInt62OneByte + 13}}, // Ack 14 packets, range 50..63 (inclusive)
3121
3122 {"Unable to read gap block value.",
3123 {kVarInt62OneByte + 9}}, // Gap 10 packets, 40..49 (inclusive)
3124 {"Unable to read ack block value.",
3125 {kVarInt62OneByte + 9}}, // Ack 10 packets, 30..39 (inclusive)
3126 {"Unable to read gap block value.",
3127 {kVarInt62OneByte + 29}}, // A gap of 30 packets (0..29 inclusive)
3128 // should be too big, leaving no room
3129 // for the ack.
3130 {"Underflow with gap block length 30 previous ack block start is 30.",
3131 {kVarInt62OneByte + 10}}, // Don't care
3132 };
3133 // clang-format on
3134
3135 std::unique_ptr<QuicEncryptedPacket> encrypted(
3136 AssemblePacketFromFragments(packet99));
3137 EXPECT_FALSE(framer_.ProcessPacket(*encrypted));
3138 EXPECT_EQ(
3139 framer_.detailed_error(),
3140 "Underflow with gap block length 30 previous ack block start is 30.");
3141 CheckFramingBoundaries(packet99, QUIC_INVALID_ACK_DATA);
3142}
3143
3144// This test checks that the ack frame processor correctly identifies
3145// and handles the case where the third ack block's length is larger than the
3146// available space in the ack range.
3147TEST_P(QuicFramerTest, ThirdAckBlockUnderflowAck) {
fkastenholz305e1732019-06-18 05:01:22 -07003148 if (!VersionHasIetfQuicFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05003149 // for now, only v99
3150 return;
3151 }
zhongyi546cc452019-04-12 15:27:49 -07003152 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
QUICHE teama6ef0a62019-03-07 20:34:33 -05003153 // clang-format off
3154 PacketFragments packet99 = {
3155 // type (short header, 4 byte packet number)
3156 {"",
3157 {0x43}},
3158 // connection_id
3159 {"",
3160 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
3161 // packet number
3162 {"",
3163 {0x12, 0x34, 0x56, 0x78}},
3164 // frame type (IETF_ACK frame)
3165 {"",
3166 {0x02}},
3167 // largest acked
3168 {"Unable to read largest acked.",
3169 {kVarInt62OneByte + 63}},
3170 // Zero delta time.
3171 {"Unable to read ack delay time.",
3172 {kVarInt62OneByte + 0x00}},
3173 // Ack block count (2 -- 2 blocks after the first)
3174 {"Unable to read ack block count.",
3175 {kVarInt62OneByte + 0x02}},
3176 // first ack block length.
3177 {"Unable to read first ack block length.",
3178 {kVarInt62OneByte + 13}}, // only 50 packet numbers "left"
3179
3180 {"Unable to read gap block value.",
3181 {kVarInt62OneByte + 10}}, // Only 40 packet numbers left
3182 {"Unable to read ack block value.",
3183 {kVarInt62OneByte + 10}}, // only 30 packet numbers left.
3184 {"Unable to read gap block value.",
3185 {kVarInt62OneByte + 1}}, // Gap is OK, 29 packet numbers left
3186 {"Unable to read ack block value.",
3187 {kVarInt62OneByte + 30}}, // Use up all 30, should be an error
3188 };
3189 // clang-format on
3190
3191 std::unique_ptr<QuicEncryptedPacket> encrypted(
3192 AssemblePacketFromFragments(packet99));
3193 EXPECT_FALSE(framer_.ProcessPacket(*encrypted));
3194 EXPECT_EQ(framer_.detailed_error(),
3195 "Underflow with ack block length 31 latest ack block end is 25.");
3196 CheckFramingBoundaries(packet99, QUIC_INVALID_ACK_DATA);
3197}
3198
3199// Tests a variety of ack block wrap scenarios. For example, if the
3200// N-1th block causes packet 0 to be acked, then a gap would wrap
3201// around to 0x3fffffff ffffffff... Make sure we detect this
3202// condition.
3203TEST_P(QuicFramerTest, AckBlockUnderflowGapWrap) {
fkastenholz305e1732019-06-18 05:01:22 -07003204 if (!VersionHasIetfQuicFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05003205 // for now, only v99
3206 return;
3207 }
zhongyi546cc452019-04-12 15:27:49 -07003208 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
QUICHE teama6ef0a62019-03-07 20:34:33 -05003209 // clang-format off
3210 PacketFragments packet99 = {
3211 // type (short header, 4 byte packet number)
3212 {"",
3213 {0x43}},
3214 // connection_id
3215 {"",
3216 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
3217 // packet number
3218 {"",
3219 {0x12, 0x34, 0x56, 0x78}},
3220 // frame type (IETF_ACK frame)
3221 {"",
3222 {0x02}},
3223 // largest acked
3224 {"Unable to read largest acked.",
3225 {kVarInt62OneByte + 10}},
3226 // Zero delta time.
3227 {"Unable to read ack delay time.",
3228 {kVarInt62OneByte + 0x00}},
3229 // Ack block count (1 -- 1 blocks after the first)
3230 {"Unable to read ack block count.",
3231 {kVarInt62OneByte + 1}},
3232 // first ack block length.
3233 {"Unable to read first ack block length.",
3234 {kVarInt62OneByte + 9}}, // Ack packets 1..10 (inclusive)
3235
3236 {"Unable to read gap block value.",
3237 {kVarInt62OneByte + 1}}, // Gap of 2 packets (-1...0), should wrap
3238 {"Underflow with gap block length 2 previous ack block start is 1.",
3239 {kVarInt62OneByte + 9}}, // irrelevant
3240 };
3241 // clang-format on
3242
3243 std::unique_ptr<QuicEncryptedPacket> encrypted(
3244 AssemblePacketFromFragments(packet99));
3245 EXPECT_FALSE(framer_.ProcessPacket(*encrypted));
3246 EXPECT_EQ(framer_.detailed_error(),
3247 "Underflow with gap block length 2 previous ack block start is 1.");
3248 CheckFramingBoundaries(packet99, QUIC_INVALID_ACK_DATA);
3249}
3250
3251// As AckBlockUnderflowGapWrap, but in this test, it's the ack
3252// component of the ack-block that causes the wrap, not the gap.
3253TEST_P(QuicFramerTest, AckBlockUnderflowAckWrap) {
fkastenholz305e1732019-06-18 05:01:22 -07003254 if (!VersionHasIetfQuicFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05003255 // for now, only v99
3256 return;
3257 }
zhongyi546cc452019-04-12 15:27:49 -07003258 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
QUICHE teama6ef0a62019-03-07 20:34:33 -05003259 // clang-format off
3260 PacketFragments packet99 = {
3261 // type (short header, 4 byte packet number)
3262 {"",
3263 {0x43}},
3264 // connection_id
3265 {"",
3266 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
3267 // packet number
3268 {"",
3269 {0x12, 0x34, 0x56, 0x78}},
3270 // frame type (IETF_ACK frame)
3271 {"",
3272 {0x02}},
3273 // largest acked
3274 {"Unable to read largest acked.",
3275 {kVarInt62OneByte + 10}},
3276 // Zero delta time.
3277 {"Unable to read ack delay time.",
3278 {kVarInt62OneByte + 0x00}},
3279 // Ack block count (1 -- 1 blocks after the first)
3280 {"Unable to read ack block count.",
3281 {kVarInt62OneByte + 1}},
3282 // first ack block length.
3283 {"Unable to read first ack block length.",
3284 {kVarInt62OneByte + 6}}, // Ack packets 4..10 (inclusive)
3285
3286 {"Unable to read gap block value.",
3287 {kVarInt62OneByte + 1}}, // Gap of 2 packets (2..3)
3288 {"Unable to read ack block value.",
3289 {kVarInt62OneByte + 9}}, // Should wrap.
3290 };
3291 // clang-format on
3292
3293 std::unique_ptr<QuicEncryptedPacket> encrypted(
3294 AssemblePacketFromFragments(packet99));
3295 EXPECT_FALSE(framer_.ProcessPacket(*encrypted));
3296 EXPECT_EQ(framer_.detailed_error(),
3297 "Underflow with ack block length 10 latest ack block end is 1.");
3298 CheckFramingBoundaries(packet99, QUIC_INVALID_ACK_DATA);
3299}
3300
3301// An ack block that acks the entire range, 1...0x3fffffffffffffff
3302TEST_P(QuicFramerTest, AckBlockAcksEverything) {
fkastenholz305e1732019-06-18 05:01:22 -07003303 if (!VersionHasIetfQuicFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05003304 // for now, only v99
3305 return;
3306 }
zhongyi546cc452019-04-12 15:27:49 -07003307 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
QUICHE teama6ef0a62019-03-07 20:34:33 -05003308 // clang-format off
3309 PacketFragments packet99 = {
3310 // type (short header, 4 byte packet number)
3311 {"",
3312 {0x43}},
3313 // connection_id
3314 {"",
3315 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
3316 // packet number
3317 {"",
3318 {0x12, 0x34, 0x56, 0x78}},
3319 // frame type (IETF_ACK frame)
3320 {"",
3321 {0x02}},
3322 // largest acked
3323 {"Unable to read largest acked.",
3324 {kVarInt62EightBytes + 0x3f, 0xff, 0xff, 0xff,
3325 0xff, 0xff, 0xff, 0xff}},
3326 // Zero delta time.
3327 {"Unable to read ack delay time.",
3328 {kVarInt62OneByte + 0x00}},
3329 // Ack block count No additional blocks
3330 {"Unable to read ack block count.",
3331 {kVarInt62OneByte + 0}},
3332 // first ack block length.
3333 {"Unable to read first ack block length.",
3334 {kVarInt62EightBytes + 0x3f, 0xff, 0xff, 0xff,
3335 0xff, 0xff, 0xff, 0xfe}},
3336 };
3337 // clang-format on
3338
3339 std::unique_ptr<QuicEncryptedPacket> encrypted(
3340 AssemblePacketFromFragments(packet99));
3341 EXPECT_TRUE(framer_.ProcessPacket(*encrypted));
3342 EXPECT_EQ(1u, visitor_.ack_frames_.size());
3343 const QuicAckFrame& frame = *visitor_.ack_frames_[0];
3344 EXPECT_EQ(1u, frame.packets.NumIntervals());
3345 EXPECT_EQ(kLargestIetfLargestObserved, LargestAcked(frame));
3346 EXPECT_EQ(kLargestIetfLargestObserved.ToUint64(),
3347 frame.packets.NumPacketsSlow());
3348}
3349
3350// This test looks for a malformed ack where
3351// - There is a largest-acked value (that is, the frame is acking
3352// something,
3353// - But the length of the first ack block is 0 saying that no frames
3354// are being acked with the largest-acked value or there are no
3355// additional ack blocks.
3356//
3357TEST_P(QuicFramerTest, AckFrameFirstAckBlockLengthZero) {
fkastenholz305e1732019-06-18 05:01:22 -07003358 if (VersionHasIetfQuicFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05003359 // Not applicable to version 99 -- first ack block contains the
3360 // number of packets that preceed the largest_acked packet.
3361 // A value of 0 means no packets preceed --- that the block's
3362 // length is 1. Therefore the condition that this test checks can
3363 // not arise.
3364 return;
3365 }
nharper9bb83462019-05-01 10:53:22 -07003366 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
QUICHE teama6ef0a62019-03-07 20:34:33 -05003367
3368 // clang-format off
3369 PacketFragments packet = {
3370 // public flags (8 byte connection_id)
3371 {"",
3372 { 0x2C }},
3373 // connection_id
3374 {"",
3375 { 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10 }},
3376 // packet number
3377 {"",
3378 { 0x12, 0x34, 0x56, 0x78 }},
3379
3380 // frame type (ack frame)
3381 // (more than one ack block, 2 byte largest observed, 2 byte block length)
3382 {"",
3383 { 0x65 }},
3384 // largest acked
3385 {"Unable to read largest acked.",
3386 { 0x12, 0x34 }},
3387 // Zero delta time.
3388 {"Unable to read ack delay time.",
3389 { 0x00, 0x00 }},
3390 // num ack blocks ranges.
3391 {"Unable to read num of ack blocks.",
3392 { 0x01 }},
3393 // first ack block length.
3394 {"Unable to read first ack block length.",
3395 { 0x00, 0x00 }},
3396 // gap to next block.
3397 { "First block length is zero.",
3398 { 0x01 }},
3399 // ack block length.
3400 { "First block length is zero.",
3401 { 0x0e, 0xaf }},
3402 // Number of timestamps.
3403 { "First block length is zero.",
3404 { 0x00 }},
3405 };
3406
QUICHE teama6ef0a62019-03-07 20:34:33 -05003407 PacketFragments packet46 = {
3408 // type (short header, 4 byte packet number)
3409 {"",
3410 { 0x43 }},
3411 // connection_id
3412 {"",
3413 { 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10 }},
3414 // packet number
3415 {"",
3416 { 0x12, 0x34, 0x56, 0x78 }},
3417
3418 // frame type (ack frame)
3419 // (more than one ack block, 2 byte largest observed, 2 byte block length)
3420 {"",
3421 { 0x65 }},
3422 // largest acked
3423 {"Unable to read largest acked.",
3424 { 0x12, 0x34 }},
3425 // Zero delta time.
3426 {"Unable to read ack delay time.",
3427 { 0x00, 0x00 }},
3428 // num ack blocks ranges.
3429 {"Unable to read num of ack blocks.",
3430 { 0x01 }},
3431 // first ack block length.
3432 {"Unable to read first ack block length.",
3433 { 0x00, 0x00 }},
3434 // gap to next block.
3435 { "First block length is zero.",
3436 { 0x01 }},
3437 // ack block length.
3438 { "First block length is zero.",
3439 { 0x0e, 0xaf }},
3440 // Number of timestamps.
3441 { "First block length is zero.",
3442 { 0x00 }},
3443 };
3444
3445 // clang-format on
3446 PacketFragments& fragments =
fayang36825da2019-08-21 14:01:27 -07003447 framer_.transport_version() >= QUIC_VERSION_46 ? packet46 : packet;
QUICHE teama6ef0a62019-03-07 20:34:33 -05003448
3449 std::unique_ptr<QuicEncryptedPacket> encrypted(
3450 AssemblePacketFromFragments(fragments));
3451
3452 EXPECT_FALSE(framer_.ProcessPacket(*encrypted));
3453 EXPECT_EQ(QUIC_INVALID_ACK_DATA, framer_.error());
3454
3455 ASSERT_TRUE(visitor_.header_.get());
3456 EXPECT_TRUE(CheckDecryption(
3457 *encrypted, !kIncludeVersion, !kIncludeDiversificationNonce,
3458 PACKET_8BYTE_CONNECTION_ID, PACKET_0BYTE_CONNECTION_ID));
3459
3460 EXPECT_EQ(0u, visitor_.stream_frames_.size());
3461 ASSERT_EQ(1u, visitor_.ack_frames_.size());
3462
3463 CheckFramingBoundaries(fragments, QUIC_INVALID_ACK_DATA);
3464}
3465
3466TEST_P(QuicFramerTest, AckFrameOneAckBlockMaxLength) {
zhongyi546cc452019-04-12 15:27:49 -07003467 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
QUICHE teama6ef0a62019-03-07 20:34:33 -05003468 // clang-format off
3469 PacketFragments packet = {
3470 // public flags (8 byte connection_id)
3471 {"",
3472 {0x2C}},
3473 // connection_id
3474 {"",
3475 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
3476 // packet number
3477 {"",
3478 {0x12, 0x34, 0x56, 0x78}},
3479 // frame type (ack frame)
3480 // (one ack block, 4 byte largest observed, 2 byte block length)
3481 {"",
3482 {0x49}},
3483 // largest acked
3484 {"Unable to read largest acked.",
3485 {0x12, 0x34, 0x56, 0x78}},
3486 // Zero delta time.
3487 {"Unable to read ack delay time.",
3488 {0x00, 0x00}},
3489 // first ack block length.
3490 {"Unable to read first ack block length.",
3491 {0x12, 0x34}},
3492 // num timestamps.
3493 {"Unable to read num received packets.",
3494 {0x00}}
3495 };
3496
QUICHE teama6ef0a62019-03-07 20:34:33 -05003497 PacketFragments packet46 = {
3498 // type (short header, 4 byte packet number)
3499 {"",
3500 {0x43}},
3501 // connection_id
3502 {"",
3503 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
3504 // packet number
3505 {"",
3506 {0x56, 0x78, 0x9A, 0xBC}},
3507 // frame type (ack frame)
3508 // (one ack block, 4 byte largest observed, 2 byte block length)
3509 {"",
3510 {0x49}},
3511 // largest acked
3512 {"Unable to read largest acked.",
3513 {0x12, 0x34, 0x56, 0x78}},
3514 // Zero delta time.
3515 {"Unable to read ack delay time.",
3516 {0x00, 0x00}},
3517 // first ack block length.
3518 {"Unable to read first ack block length.",
3519 {0x12, 0x34}},
3520 // num timestamps.
3521 {"Unable to read num received packets.",
3522 {0x00}}
3523 };
3524
3525 PacketFragments packet99 = {
3526 // type (short header, 4 byte packet number)
3527 {"",
3528 {0x43}},
3529 // connection_id
3530 {"",
3531 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
3532 // packet number
3533 {"",
3534 {0x56, 0x78, 0x9A, 0xBC}},
3535 // frame type (IETF_ACK frame)
3536 {"",
3537 {0x02}},
3538 // largest acked
3539 {"Unable to read largest acked.",
3540 {kVarInt62FourBytes + 0x12, 0x34, 0x56, 0x78}},
3541 // Zero delta time.
3542 {"Unable to read ack delay time.",
3543 {kVarInt62OneByte + 0x00}},
3544 // Number of ack blocks after first
3545 {"Unable to read ack block count.",
3546 {kVarInt62OneByte + 0x00}},
3547 // first ack block length.
3548 {"Unable to read first ack block length.",
3549 {kVarInt62TwoBytes + 0x12, 0x33}}
3550 };
3551 // clang-format on
3552
3553 PacketFragments& fragments =
fkastenholz305e1732019-06-18 05:01:22 -07003554 VersionHasIetfQuicFrames(framer_.transport_version())
QUICHE teama6ef0a62019-03-07 20:34:33 -05003555 ? packet99
fayang36825da2019-08-21 14:01:27 -07003556 : (framer_.transport_version() >= QUIC_VERSION_46 ? packet46
3557 : packet);
QUICHE teama6ef0a62019-03-07 20:34:33 -05003558 std::unique_ptr<QuicEncryptedPacket> encrypted(
3559 AssemblePacketFromFragments(fragments));
3560 EXPECT_TRUE(framer_.ProcessPacket(*encrypted));
3561
3562 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
3563 ASSERT_TRUE(visitor_.header_.get());
3564 EXPECT_TRUE(CheckDecryption(
3565 *encrypted, !kIncludeVersion, !kIncludeDiversificationNonce,
3566 PACKET_8BYTE_CONNECTION_ID, PACKET_0BYTE_CONNECTION_ID));
3567
3568 EXPECT_EQ(0u, visitor_.stream_frames_.size());
3569 ASSERT_EQ(1u, visitor_.ack_frames_.size());
3570 const QuicAckFrame& frame = *visitor_.ack_frames_[0];
3571 EXPECT_EQ(kPacketNumber, LargestAcked(frame));
3572 ASSERT_EQ(4660u, frame.packets.NumPacketsSlow());
3573
3574 CheckFramingBoundaries(fragments, QUIC_INVALID_ACK_DATA);
3575}
3576
3577// Tests ability to handle multiple ackblocks after the first ack
3578// block. Non-version-99 tests include multiple timestamps as well.
3579TEST_P(QuicFramerTest, AckFrameTwoTimeStampsMultipleAckBlocks) {
zhongyi546cc452019-04-12 15:27:49 -07003580 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
QUICHE teama6ef0a62019-03-07 20:34:33 -05003581 // clang-format off
3582 PacketFragments packet = {
3583 // public flags (8 byte connection_id)
3584 {"",
3585 { 0x2C }},
3586 // connection_id
3587 {"",
3588 { 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10 }},
3589 // packet number
3590 {"",
3591 { 0x12, 0x34, 0x56, 0x78 }},
3592
3593 // frame type (ack frame)
3594 // (more than one ack block, 2 byte largest observed, 2 byte block length)
3595 {"",
3596 { 0x65 }},
3597 // largest acked
3598 {"Unable to read largest acked.",
3599 { 0x12, 0x34 }},
3600 // Zero delta time.
3601 {"Unable to read ack delay time.",
3602 { 0x00, 0x00 }},
3603 // num ack blocks ranges.
3604 {"Unable to read num of ack blocks.",
3605 { 0x04 }},
3606 // first ack block length.
3607 {"Unable to read first ack block length.",
3608 { 0x00, 0x01 }},
3609 // gap to next block.
3610 { "Unable to read gap to next ack block.",
3611 { 0x01 }},
3612 // ack block length.
3613 { "Unable to ack block length.",
3614 { 0x0e, 0xaf }},
3615 // gap to next block.
3616 { "Unable to read gap to next ack block.",
3617 { 0xff }},
3618 // ack block length.
3619 { "Unable to ack block length.",
3620 { 0x00, 0x00 }},
3621 // gap to next block.
3622 { "Unable to read gap to next ack block.",
3623 { 0x91 }},
3624 // ack block length.
3625 { "Unable to ack block length.",
3626 { 0x01, 0xea }},
3627 // gap to next block.
3628 { "Unable to read gap to next ack block.",
3629 { 0x05 }},
3630 // ack block length.
3631 { "Unable to ack block length.",
3632 { 0x00, 0x04 }},
3633 // Number of timestamps.
3634 { "Unable to read num received packets.",
3635 { 0x02 }},
3636 // Delta from largest observed.
3637 { "Unable to read sequence delta in received packets.",
3638 { 0x01 }},
3639 // Delta time.
3640 { "Unable to read time delta in received packets.",
3641 { 0x76, 0x54, 0x32, 0x10 }},
3642 // Delta from largest observed.
3643 { "Unable to read sequence delta in received packets.",
3644 { 0x02 }},
3645 // Delta time.
3646 { "Unable to read incremental time delta in received packets.",
3647 { 0x32, 0x10 }},
3648 };
3649
QUICHE teama6ef0a62019-03-07 20:34:33 -05003650 PacketFragments packet46 = {
3651 // type (short header, 4 byte packet number)
3652 {"",
3653 { 0x43 }},
3654 // connection_id
3655 {"",
3656 { 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10 }},
3657 // packet number
3658 {"",
3659 { 0x12, 0x34, 0x56, 0x78 }},
3660
3661 // frame type (ack frame)
3662 // (more than one ack block, 2 byte largest observed, 2 byte block length)
3663 {"",
3664 { 0x65 }},
3665 // largest acked
3666 {"Unable to read largest acked.",
3667 { 0x12, 0x34 }},
3668 // Zero delta time.
3669 {"Unable to read ack delay time.",
3670 { 0x00, 0x00 }},
3671 // num ack blocks ranges.
3672 {"Unable to read num of ack blocks.",
3673 { 0x04 }},
3674 // first ack block length.
3675 {"Unable to read first ack block length.",
3676 { 0x00, 0x01 }},
3677 // gap to next block.
3678 { "Unable to read gap to next ack block.",
3679 { 0x01 }},
3680 // ack block length.
3681 { "Unable to ack block length.",
3682 { 0x0e, 0xaf }},
3683 // gap to next block.
3684 { "Unable to read gap to next ack block.",
3685 { 0xff }},
3686 // ack block length.
3687 { "Unable to ack block length.",
3688 { 0x00, 0x00 }},
3689 // gap to next block.
3690 { "Unable to read gap to next ack block.",
3691 { 0x91 }},
3692 // ack block length.
3693 { "Unable to ack block length.",
3694 { 0x01, 0xea }},
3695 // gap to next block.
3696 { "Unable to read gap to next ack block.",
3697 { 0x05 }},
3698 // ack block length.
3699 { "Unable to ack block length.",
3700 { 0x00, 0x04 }},
3701 // Number of timestamps.
3702 { "Unable to read num received packets.",
3703 { 0x02 }},
3704 // Delta from largest observed.
3705 { "Unable to read sequence delta in received packets.",
3706 { 0x01 }},
3707 // Delta time.
3708 { "Unable to read time delta in received packets.",
3709 { 0x76, 0x54, 0x32, 0x10 }},
3710 // Delta from largest observed.
3711 { "Unable to read sequence delta in received packets.",
3712 { 0x02 }},
3713 // Delta time.
3714 { "Unable to read incremental time delta in received packets.",
3715 { 0x32, 0x10 }},
3716 };
3717
3718 PacketFragments packet99 = {
3719 // type (short header, 4 byte packet number)
3720 {"",
3721 { 0x43 }},
3722 // connection_id
3723 {"",
3724 { 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10 }},
3725 // packet number
3726 {"",
3727 { 0x12, 0x34, 0x56, 0x78 }},
3728
3729 // frame type (IETF_ACK frame)
3730 {"",
3731 { 0x02 }},
3732 // largest acked
3733 {"Unable to read largest acked.",
3734 { kVarInt62TwoBytes + 0x12, 0x34 }}, // = 4660
3735 // Zero delta time.
3736 {"Unable to read ack delay time.",
3737 { kVarInt62OneByte + 0x00 }},
3738 // number of additional ack blocks
3739 {"Unable to read ack block count.",
3740 { kVarInt62OneByte + 0x03 }},
3741 // first ack block length.
3742 {"Unable to read first ack block length.",
3743 { kVarInt62OneByte + 0x00 }}, // 1st block length = 1
3744
3745 // Additional ACK Block #1
3746 // gap to next block.
3747 { "Unable to read gap block value.",
3748 { kVarInt62OneByte + 0x00 }}, // gap of 1 packet
3749 // ack block length.
3750 { "Unable to read ack block value.",
3751 { kVarInt62TwoBytes + 0x0e, 0xae }}, // 3759
3752
3753 // pre-version-99 test includes an ack block of 0 length. this
3754 // can not happen in version 99. ergo the second block is not
3755 // present in the v99 test and the gap length of the next block
3756 // is the sum of the two gaps in the pre-version-99 tests.
3757 // Additional ACK Block #2
3758 // gap to next block.
3759 { "Unable to read gap block value.",
3760 { kVarInt62TwoBytes + 0x01, 0x8f }}, // Gap is 400 (0x190) pkts
3761 // ack block length.
3762 { "Unable to read ack block value.",
3763 { kVarInt62TwoBytes + 0x01, 0xe9 }}, // block is 389 (x1ea) pkts
3764
3765 // Additional ACK Block #3
3766 // gap to next block.
3767 { "Unable to read gap block value.",
3768 { kVarInt62OneByte + 0x04 }}, // Gap is 5 packets.
3769 // ack block length.
3770 { "Unable to read ack block value.",
3771 { kVarInt62OneByte + 0x03 }}, // block is 3 packets.
3772 };
3773
3774 // clang-format on
3775 PacketFragments& fragments =
fkastenholz305e1732019-06-18 05:01:22 -07003776 VersionHasIetfQuicFrames(framer_.transport_version())
QUICHE teama6ef0a62019-03-07 20:34:33 -05003777 ? packet99
fayang36825da2019-08-21 14:01:27 -07003778 : (framer_.transport_version() >= QUIC_VERSION_46 ? packet46
3779 : packet);
QUICHE teama6ef0a62019-03-07 20:34:33 -05003780
3781 std::unique_ptr<QuicEncryptedPacket> encrypted(
3782 AssemblePacketFromFragments(fragments));
3783
3784 framer_.set_process_timestamps(true);
3785 EXPECT_TRUE(framer_.ProcessPacket(*encrypted));
3786
3787 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
3788 ASSERT_TRUE(visitor_.header_.get());
3789 EXPECT_TRUE(CheckDecryption(
3790 *encrypted, !kIncludeVersion, !kIncludeDiversificationNonce,
3791 PACKET_8BYTE_CONNECTION_ID, PACKET_0BYTE_CONNECTION_ID));
3792
3793 EXPECT_EQ(0u, visitor_.stream_frames_.size());
3794 ASSERT_EQ(1u, visitor_.ack_frames_.size());
3795 const QuicAckFrame& frame = *visitor_.ack_frames_[0];
3796 EXPECT_EQ(kSmallLargestObserved, LargestAcked(frame));
3797 ASSERT_EQ(4254u, frame.packets.NumPacketsSlow());
3798 EXPECT_EQ(4u, frame.packets.NumIntervals());
fkastenholz305e1732019-06-18 05:01:22 -07003799 if (VersionHasIetfQuicFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05003800 EXPECT_EQ(0u, frame.received_packet_times.size());
3801 } else {
3802 EXPECT_EQ(2u, frame.received_packet_times.size());
3803 }
3804 CheckFramingBoundaries(fragments, QUIC_INVALID_ACK_DATA);
3805}
3806
3807TEST_P(QuicFramerTest, AckFrameTimeStampDeltaTooHigh) {
nharper9bb83462019-05-01 10:53:22 -07003808 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
QUICHE teama6ef0a62019-03-07 20:34:33 -05003809 // clang-format off
3810 unsigned char packet[] = {
3811 // public flags (8 byte connection_id)
3812 0x28,
3813 // connection_id
3814 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
3815 // packet number
3816 0x12, 0x34, 0x56, 0x78,
3817
3818 // frame type (ack frame)
3819 // (no ack blocks, 1 byte largest observed, 1 byte block length)
3820 0x40,
3821 // largest acked
3822 0x01,
3823 // Zero delta time.
3824 0x00, 0x00,
3825 // first ack block length.
3826 0x01,
3827 // num timestamps.
3828 0x01,
3829 // Delta from largest observed.
3830 0x01,
3831 // Delta time.
3832 0x10, 0x32, 0x54, 0x76,
3833 };
3834
QUICHE teama6ef0a62019-03-07 20:34:33 -05003835 unsigned char packet46[] = {
3836 // type (short header, 4 byte packet number)
3837 0x43,
3838 // connection_id
3839 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
3840 // packet number
3841 0x12, 0x34, 0x56, 0x78,
3842
3843 // frame type (ack frame)
3844 // (no ack blocks, 1 byte largest observed, 1 byte block length)
3845 0x40,
3846 // largest acked
3847 0x01,
3848 // Zero delta time.
3849 0x00, 0x00,
3850 // first ack block length.
3851 0x01,
3852 // num timestamps.
3853 0x01,
3854 // Delta from largest observed.
3855 0x01,
3856 // Delta time.
3857 0x10, 0x32, 0x54, 0x76,
3858 };
3859 // clang-format on
fkastenholz305e1732019-06-18 05:01:22 -07003860 if (VersionHasIetfQuicFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05003861 return;
3862 }
3863 QuicEncryptedPacket encrypted(
fayang36825da2019-08-21 14:01:27 -07003864 AsChars(framer_.transport_version() >= QUIC_VERSION_46 ? packet46
3865 : packet),
QUICHE teama6ef0a62019-03-07 20:34:33 -05003866 QUIC_ARRAYSIZE(packet), false);
3867 EXPECT_FALSE(framer_.ProcessPacket(encrypted));
3868 EXPECT_TRUE(QuicTextUtils::StartsWith(
3869 framer_.detailed_error(), "delta_from_largest_observed too high"));
3870}
3871
3872TEST_P(QuicFramerTest, AckFrameTimeStampSecondDeltaTooHigh) {
nharper9bb83462019-05-01 10:53:22 -07003873 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
QUICHE teama6ef0a62019-03-07 20:34:33 -05003874 // clang-format off
3875 unsigned char packet[] = {
3876 // public flags (8 byte connection_id)
3877 0x28,
3878 // connection_id
3879 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
3880 // packet number
3881 0x12, 0x34, 0x56, 0x78,
3882
3883 // frame type (ack frame)
3884 // (no ack blocks, 1 byte largest observed, 1 byte block length)
3885 0x40,
3886 // largest acked
3887 0x03,
3888 // Zero delta time.
3889 0x00, 0x00,
3890 // first ack block length.
3891 0x03,
3892 // num timestamps.
3893 0x02,
3894 // Delta from largest observed.
3895 0x01,
3896 // Delta time.
3897 0x10, 0x32, 0x54, 0x76,
3898 // Delta from largest observed.
3899 0x03,
3900 // Delta time.
3901 0x10, 0x32,
3902 };
3903
QUICHE teama6ef0a62019-03-07 20:34:33 -05003904 unsigned char packet46[] = {
3905 // type (short header, 4 byte packet number)
3906 0x43,
3907 // connection_id
3908 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
3909 // packet number
3910 0x12, 0x34, 0x56, 0x78,
3911
3912 // frame type (ack frame)
3913 // (no ack blocks, 1 byte largest observed, 1 byte block length)
3914 0x40,
3915 // largest acked
3916 0x03,
3917 // Zero delta time.
3918 0x00, 0x00,
3919 // first ack block length.
3920 0x03,
3921 // num timestamps.
3922 0x02,
3923 // Delta from largest observed.
3924 0x01,
3925 // Delta time.
3926 0x10, 0x32, 0x54, 0x76,
3927 // Delta from largest observed.
3928 0x03,
3929 // Delta time.
3930 0x10, 0x32,
3931 };
3932 // clang-format on
fkastenholz305e1732019-06-18 05:01:22 -07003933 if (VersionHasIetfQuicFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05003934 return;
3935 }
3936 QuicEncryptedPacket encrypted(
fayang36825da2019-08-21 14:01:27 -07003937 AsChars(framer_.transport_version() >= QUIC_VERSION_46 ? packet46
3938 : packet),
QUICHE teama6ef0a62019-03-07 20:34:33 -05003939 QUIC_ARRAYSIZE(packet), false);
3940 EXPECT_FALSE(framer_.ProcessPacket(encrypted));
3941 EXPECT_TRUE(QuicTextUtils::StartsWith(
3942 framer_.detailed_error(), "delta_from_largest_observed too high"));
3943}
3944
3945TEST_P(QuicFramerTest, NewStopWaitingFrame) {
fkastenholz305e1732019-06-18 05:01:22 -07003946 if (VersionHasIetfQuicFrames(version_.transport_version)) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05003947 return;
3948 }
nharper9bb83462019-05-01 10:53:22 -07003949 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
QUICHE teama6ef0a62019-03-07 20:34:33 -05003950 // clang-format off
3951 PacketFragments packet = {
3952 // public flags (8 byte connection_id)
3953 {"",
3954 {0x2C}},
3955 // connection_id
3956 {"",
3957 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
3958 // packet number
3959 {"",
3960 {0x12, 0x34, 0x56, 0x78}},
3961 // frame type (stop waiting frame)
3962 {"",
3963 {0x06}},
3964 // least packet number awaiting an ack, delta from packet number.
3965 {"Unable to read least unacked delta.",
3966 {0x00, 0x00, 0x00, 0x08}}
3967 };
3968
QUICHE teama6ef0a62019-03-07 20:34:33 -05003969 PacketFragments packet46 = {
3970 // type (short header, 4 byte packet number)
3971 {"",
3972 {0x43}},
3973 // connection_id
3974 {"",
3975 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
3976 // packet number
3977 {"",
3978 {0x12, 0x34, 0x56, 0x78}},
3979 // frame type (stop waiting frame)
3980 {"",
3981 {0x06}},
3982 // least packet number awaiting an ack, delta from packet number.
3983 {"Unable to read least unacked delta.",
3984 {0x00, 0x00, 0x00, 0x08}}
3985 };
3986 // clang-format on
3987
3988 PacketFragments& fragments =
fayang36825da2019-08-21 14:01:27 -07003989 framer_.transport_version() >= QUIC_VERSION_46 ? packet46 : packet;
QUICHE teama6ef0a62019-03-07 20:34:33 -05003990
3991 std::unique_ptr<QuicEncryptedPacket> encrypted(
3992 AssemblePacketFromFragments(fragments));
ianswett97b690b2019-05-02 15:12:43 -07003993 if (GetQuicReloadableFlag(quic_do_not_accept_stop_waiting) &&
fayang36825da2019-08-21 14:01:27 -07003994 version_.transport_version >= QUIC_VERSION_46) {
ianswett97b690b2019-05-02 15:12:43 -07003995 EXPECT_FALSE(framer_.ProcessPacket(*encrypted));
3996 EXPECT_EQ(QUIC_INVALID_STOP_WAITING_DATA, framer_.error());
3997 EXPECT_EQ("STOP WAITING not supported in version 44+.",
3998 framer_.detailed_error());
3999 return;
4000 }
4001
QUICHE teama6ef0a62019-03-07 20:34:33 -05004002 EXPECT_TRUE(framer_.ProcessPacket(*encrypted));
4003
4004 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
4005 ASSERT_TRUE(visitor_.header_.get());
4006 EXPECT_TRUE(CheckDecryption(
4007 *encrypted, !kIncludeVersion, !kIncludeDiversificationNonce,
4008 PACKET_8BYTE_CONNECTION_ID, PACKET_0BYTE_CONNECTION_ID));
4009
4010 EXPECT_EQ(0u, visitor_.stream_frames_.size());
4011 ASSERT_EQ(1u, visitor_.stop_waiting_frames_.size());
4012 const QuicStopWaitingFrame& frame = *visitor_.stop_waiting_frames_[0];
4013 EXPECT_EQ(kLeastUnacked, frame.least_unacked);
4014
4015 CheckFramingBoundaries(fragments, QUIC_INVALID_STOP_WAITING_DATA);
4016}
4017
4018TEST_P(QuicFramerTest, InvalidNewStopWaitingFrame) {
fkastenholz305e1732019-06-18 05:01:22 -07004019 if (VersionHasIetfQuicFrames(version_.transport_version) ||
ianswett97b690b2019-05-02 15:12:43 -07004020 (GetQuicReloadableFlag(quic_do_not_accept_stop_waiting) &&
fayang36825da2019-08-21 14:01:27 -07004021 version_.transport_version >= QUIC_VERSION_46)) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05004022 return;
4023 }
nharper9bb83462019-05-01 10:53:22 -07004024 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
QUICHE teama6ef0a62019-03-07 20:34:33 -05004025 // clang-format off
4026 unsigned char packet[] = {
4027 // public flags (8 byte connection_id)
4028 0x2C,
4029 // connection_id
4030 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
4031 // packet number
4032 0x12, 0x34, 0x56, 0x78,
4033 // frame type (stop waiting frame)
4034 0x06,
4035 // least packet number awaiting an ack, delta from packet number.
4036 0x13, 0x34, 0x56, 0x78,
4037 0x9A, 0xA8,
4038 };
4039
QUICHE teama6ef0a62019-03-07 20:34:33 -05004040 unsigned char packet46[] = {
4041 // type (short header, 4 byte packet number)
4042 0x43,
4043 // connection_id
4044 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
4045 // packet number
4046 0x12, 0x34, 0x56, 0x78,
4047 // frame type (stop waiting frame)
4048 0x06,
4049 // least packet number awaiting an ack, delta from packet number.
4050 0x57, 0x78, 0x9A, 0xA8,
4051 };
4052 // clang-format on
4053
4054 QuicEncryptedPacket encrypted(
fayang36825da2019-08-21 14:01:27 -07004055 AsChars(framer_.transport_version() >= QUIC_VERSION_46 ? packet46
4056 : packet),
4057 framer_.transport_version() > QUIC_VERSION_43 ? QUIC_ARRAYSIZE(packet46)
QUICHE teama6ef0a62019-03-07 20:34:33 -05004058 : QUIC_ARRAYSIZE(packet),
4059 false);
4060 EXPECT_FALSE(framer_.ProcessPacket(encrypted));
4061 EXPECT_EQ(QUIC_INVALID_STOP_WAITING_DATA, framer_.error());
4062 EXPECT_EQ("Invalid unacked delta.", framer_.detailed_error());
4063}
4064
4065TEST_P(QuicFramerTest, RstStreamFrame) {
zhongyi546cc452019-04-12 15:27:49 -07004066 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
QUICHE teama6ef0a62019-03-07 20:34:33 -05004067 // clang-format off
4068 PacketFragments packet = {
4069 // public flags (8 byte connection_id)
4070 {"",
4071 {0x28}},
4072 // connection_id
4073 {"",
4074 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
4075 // packet number
4076 {"",
4077 {0x12, 0x34, 0x56, 0x78}},
4078 // frame type (rst stream frame)
4079 {"",
4080 {0x01}},
4081 // stream id
4082 {"Unable to read stream_id.",
4083 {0x01, 0x02, 0x03, 0x04}},
4084 // sent byte offset
4085 {"Unable to read rst stream sent byte offset.",
4086 {0x3A, 0x98, 0xFE, 0xDC,
4087 0x32, 0x10, 0x76, 0x54}},
4088 // error code
4089 {"Unable to read rst stream error code.",
4090 {0x00, 0x00, 0x00, 0x01}}
4091 };
4092
QUICHE teama6ef0a62019-03-07 20:34:33 -05004093 PacketFragments packet46 = {
4094 // type (short header, 4 byte packet number)
4095 {"",
4096 {0x43}},
4097 // connection_id
4098 {"",
4099 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
4100 // packet number
4101 {"",
4102 {0x12, 0x34, 0x56, 0x78}},
4103 // frame type (rst stream frame)
4104 {"",
4105 {0x01}},
4106 // stream id
4107 {"Unable to read stream_id.",
4108 {0x01, 0x02, 0x03, 0x04}},
4109 // sent byte offset
4110 {"Unable to read rst stream sent byte offset.",
4111 {0x3A, 0x98, 0xFE, 0xDC,
4112 0x32, 0x10, 0x76, 0x54}},
4113 // error code
4114 {"Unable to read rst stream error code.",
4115 {0x00, 0x00, 0x00, 0x01}}
4116 };
4117
4118 PacketFragments packet99 = {
4119 // type (short header, 4 byte packet number)
4120 {"",
4121 {0x43}},
4122 // connection_id
4123 {"",
4124 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
4125 // packet number
4126 {"",
4127 {0x12, 0x34, 0x56, 0x78}},
4128 // frame type (IETF_RST_STREAM frame)
4129 {"",
4130 {0x04}},
4131 // stream id
4132 {"Unable to read rst stream stream id.",
4133 {kVarInt62FourBytes + 0x01, 0x02, 0x03, 0x04}},
4134 // application error code
4135 {"Unable to read rst stream error code.",
fkastenholz07300e52019-07-16 11:51:37 -07004136 {kVarInt62OneByte + 0x01}},
QUICHE teama6ef0a62019-03-07 20:34:33 -05004137 // Final Offset
4138 {"Unable to read rst stream sent byte offset.",
4139 {kVarInt62EightBytes + 0x3a, 0x98, 0xFE, 0xDC, 0x32, 0x10, 0x76, 0x54}}
4140 };
4141 // clang-format on
4142
4143 PacketFragments& fragments =
fkastenholz305e1732019-06-18 05:01:22 -07004144 VersionHasIetfQuicFrames(framer_.transport_version())
QUICHE teama6ef0a62019-03-07 20:34:33 -05004145 ? packet99
fayang36825da2019-08-21 14:01:27 -07004146 : (framer_.transport_version() >= QUIC_VERSION_46 ? packet46
4147 : packet);
QUICHE teama6ef0a62019-03-07 20:34:33 -05004148 std::unique_ptr<QuicEncryptedPacket> encrypted(
4149 AssemblePacketFromFragments(fragments));
4150 EXPECT_TRUE(framer_.ProcessPacket(*encrypted));
4151
4152 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
4153 ASSERT_TRUE(visitor_.header_.get());
4154 EXPECT_TRUE(CheckDecryption(
4155 *encrypted, !kIncludeVersion, !kIncludeDiversificationNonce,
4156 PACKET_8BYTE_CONNECTION_ID, PACKET_0BYTE_CONNECTION_ID));
4157
4158 EXPECT_EQ(kStreamId, visitor_.rst_stream_frame_.stream_id);
4159 EXPECT_EQ(0x01, visitor_.rst_stream_frame_.error_code);
4160 EXPECT_EQ(kStreamOffset, visitor_.rst_stream_frame_.byte_offset);
4161 CheckFramingBoundaries(fragments, QUIC_INVALID_RST_STREAM_DATA);
4162}
4163
4164TEST_P(QuicFramerTest, ConnectionCloseFrame) {
zhongyi546cc452019-04-12 15:27:49 -07004165 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
QUICHE teama6ef0a62019-03-07 20:34:33 -05004166 // clang-format off
4167 PacketFragments packet = {
4168 // public flags (8 byte connection_id)
4169 {"",
4170 {0x28}},
4171 // connection_id
4172 {"",
4173 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
4174 // packet number
4175 {"",
4176 {0x12, 0x34, 0x56, 0x78}},
4177 // frame type (connection close frame)
4178 {"",
4179 {0x02}},
4180 // error code
4181 {"Unable to read connection close error code.",
4182 {0x00, 0x00, 0x00, 0x11}},
4183 {"Unable to read connection close error details.",
4184 {
4185 // error details length
4186 0x0, 0x0d,
4187 // error details
4188 'b', 'e', 'c', 'a',
4189 'u', 's', 'e', ' ',
4190 'I', ' ', 'c', 'a',
4191 'n'}
4192 }
4193 };
4194
QUICHE teama6ef0a62019-03-07 20:34:33 -05004195 PacketFragments packet46 = {
4196 // type (short header, 4 byte packet number)
4197 {"",
4198 {0x43}},
4199 // connection_id
4200 {"",
4201 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
4202 // packet number
4203 {"",
4204 {0x12, 0x34, 0x56, 0x78}},
4205 // frame type (connection close frame)
4206 {"",
4207 {0x02}},
4208 // error code
4209 {"Unable to read connection close error code.",
4210 {0x00, 0x00, 0x00, 0x11}},
4211 {"Unable to read connection close error details.",
4212 {
4213 // error details length
4214 0x0, 0x0d,
4215 // error details
4216 'b', 'e', 'c', 'a',
4217 'u', 's', 'e', ' ',
4218 'I', ' ', 'c', 'a',
4219 'n'}
4220 }
4221 };
4222
4223 PacketFragments packet99 = {
4224 // type (short header, 4 byte packet number)
4225 {"",
4226 {0x43}},
4227 // connection_id
4228 {"",
4229 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
4230 // packet number
4231 {"",
4232 {0x12, 0x34, 0x56, 0x78}},
fkastenholzb4dade72019-08-05 06:54:20 -07004233 // frame type (IETF Transport CONNECTION_CLOSE frame)
QUICHE teama6ef0a62019-03-07 20:34:33 -05004234 {"",
4235 {0x1c}},
4236 // error code
4237 {"Unable to read connection close error code.",
fkastenholzd57d3f92019-07-16 09:05:17 -07004238 {kVarInt62TwoBytes + 0x00, 0x11}},
QUICHE teama6ef0a62019-03-07 20:34:33 -05004239 {"Unable to read connection close frame type.",
4240 {kVarInt62TwoBytes + 0x12, 0x34 }},
4241 {"Unable to read connection close error details.",
4242 {
4243 // error details length
4244 kVarInt62OneByte + 0x0d,
4245 // error details
4246 'b', 'e', 'c', 'a',
4247 'u', 's', 'e', ' ',
4248 'I', ' ', 'c', 'a',
4249 'n'}
4250 }
4251 };
4252 // clang-format on
4253
4254 PacketFragments& fragments =
fkastenholz305e1732019-06-18 05:01:22 -07004255 VersionHasIetfQuicFrames(framer_.transport_version())
QUICHE teama6ef0a62019-03-07 20:34:33 -05004256 ? packet99
fayang36825da2019-08-21 14:01:27 -07004257 : (framer_.transport_version() >= QUIC_VERSION_46 ? packet46
4258 : packet);
QUICHE teama6ef0a62019-03-07 20:34:33 -05004259 std::unique_ptr<QuicEncryptedPacket> encrypted(
4260 AssemblePacketFromFragments(fragments));
4261 EXPECT_TRUE(framer_.ProcessPacket(*encrypted));
4262
4263 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
4264 ASSERT_TRUE(visitor_.header_.get());
4265 EXPECT_TRUE(CheckDecryption(
4266 *encrypted, !kIncludeVersion, !kIncludeDiversificationNonce,
4267 PACKET_8BYTE_CONNECTION_ID, PACKET_0BYTE_CONNECTION_ID));
4268
4269 EXPECT_EQ(0u, visitor_.stream_frames_.size());
zhongyiba6354a2019-04-10 16:49:25 -07004270 EXPECT_EQ(0x11u, static_cast<unsigned>(
4271 visitor_.connection_close_frame_.quic_error_code));
QUICHE teama6ef0a62019-03-07 20:34:33 -05004272 EXPECT_EQ("because I can", visitor_.connection_close_frame_.error_details);
fkastenholz305e1732019-06-18 05:01:22 -07004273 if (VersionHasIetfQuicFrames(framer_.transport_version())) {
fkastenholze9d71a82019-04-09 05:12:13 -07004274 EXPECT_EQ(0x1234u,
4275 visitor_.connection_close_frame_.transport_close_frame_type);
fkastenholzb4dade72019-08-05 06:54:20 -07004276 EXPECT_EQ(QUIC_IETF_GQUIC_ERROR_MISSING,
4277 visitor_.connection_close_frame_.extracted_error_code);
fkastenholza14a7ae2019-08-07 05:21:22 -07004278 } else {
4279 // For Google QUIC closes, the error code is copied into
4280 // extracted_error_code.
4281 EXPECT_EQ(0x11u,
4282 static_cast<unsigned>(
4283 visitor_.connection_close_frame_.extracted_error_code));
fkastenholzb4dade72019-08-05 06:54:20 -07004284 }
4285
4286 ASSERT_EQ(0u, visitor_.ack_frames_.size());
4287
4288 CheckFramingBoundaries(fragments, QUIC_INVALID_CONNECTION_CLOSE_DATA);
4289}
4290
4291// As above, but checks that for Google-QUIC, if there happens
4292// to be an ErrorCode string at the start of the details, it is
4293// NOT extracted/parsed/folded/spindled/and/mutilated.
4294TEST_P(QuicFramerTest, ConnectionCloseFrameWithExtractedInfoIgnoreGCuic) {
4295 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
4296
4297 // clang-format off
4298 PacketFragments packet = {
4299 // public flags (8 byte connection_id)
4300 {"",
4301 {0x28}},
4302 // connection_id
4303 {"",
4304 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
4305 // packet number
4306 {"",
4307 {0x12, 0x34, 0x56, 0x78}},
4308 // frame type (connection close frame)
4309 {"",
4310 {0x02}},
4311 // error code
4312 {"Unable to read connection close error code.",
4313 {0x00, 0x00, 0x00, 0x11}},
4314 {"Unable to read connection close error details.",
4315 {
4316 // error details length
4317 0x0, 0x13,
4318 // error details
4319 '1', '7', '7', '6',
4320 '7', ':', 'b', 'e',
4321 'c', 'a', 'u', 's',
4322 'e', ' ', 'I', ' ',
4323 'c', 'a', 'n'}
4324 }
4325 };
4326
fkastenholzb4dade72019-08-05 06:54:20 -07004327 PacketFragments packet46 = {
4328 // type (short header, 4 byte packet number)
4329 {"",
4330 {0x43}},
4331 // connection_id
4332 {"",
4333 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
4334 // packet number
4335 {"",
4336 {0x12, 0x34, 0x56, 0x78}},
4337 // frame type (connection close frame)
4338 {"",
4339 {0x02}},
4340 // error code
4341 {"Unable to read connection close error code.",
4342 {0x00, 0x00, 0x00, 0x11}},
4343 {"Unable to read connection close error details.",
4344 {
4345 // error details length
4346 0x0, 0x13,
4347 // error details
4348 '1', '7', '7', '6',
4349 '7', ':', 'b', 'e',
4350 'c', 'a', 'u', 's',
4351 'e', ' ', 'I', ' ',
4352 'c', 'a', 'n'}
4353 }
4354 };
4355
4356 PacketFragments packet99 = {
4357 // type (short header, 4 byte packet number)
4358 {"",
4359 {0x43}},
4360 // connection_id
4361 {"",
4362 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
4363 // packet number
4364 {"",
4365 {0x12, 0x34, 0x56, 0x78}},
4366 // frame type (IETF Transport CONNECTION_CLOSE frame)
4367 {"",
4368 {0x1c}},
4369 // error code
4370 {"Unable to read connection close error code.",
4371 {kVarInt62OneByte + 0x11}},
4372 {"Unable to read connection close frame type.",
4373 {kVarInt62TwoBytes + 0x12, 0x34 }},
4374 {"Unable to read connection close error details.",
4375 {
4376 // error details length
4377 kVarInt62OneByte + 0x13,
4378 // error details
4379 '1', '7', '7', '6',
4380 '7', ':', 'b', 'e',
4381 'c', 'a', 'u', 's',
4382 'e', ' ', 'I', ' ',
4383 'c', 'a', 'n'}
4384 }
4385 };
4386 // clang-format on
4387
4388 PacketFragments& fragments =
4389 VersionHasIetfQuicFrames(framer_.transport_version())
4390 ? packet99
fayang36825da2019-08-21 14:01:27 -07004391 : (framer_.transport_version() >= QUIC_VERSION_46 ? packet46
4392 : packet);
fkastenholzb4dade72019-08-05 06:54:20 -07004393 std::unique_ptr<QuicEncryptedPacket> encrypted(
4394 AssemblePacketFromFragments(fragments));
4395 EXPECT_TRUE(framer_.ProcessPacket(*encrypted));
4396
4397 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
4398 ASSERT_TRUE(visitor_.header_.get());
4399 EXPECT_TRUE(CheckDecryption(
4400 *encrypted, !kIncludeVersion, !kIncludeDiversificationNonce,
4401 PACKET_8BYTE_CONNECTION_ID, PACKET_0BYTE_CONNECTION_ID));
4402
4403 EXPECT_EQ(0u, visitor_.stream_frames_.size());
4404 EXPECT_EQ(0x11u, static_cast<unsigned>(
4405 visitor_.connection_close_frame_.quic_error_code));
fkastenholz488a4622019-08-26 06:24:46 -07004406
fkastenholzb4dade72019-08-05 06:54:20 -07004407 if (VersionHasIetfQuicFrames(framer_.transport_version())) {
4408 EXPECT_EQ(0x1234u,
4409 visitor_.connection_close_frame_.transport_close_frame_type);
4410 EXPECT_EQ(17767u, visitor_.connection_close_frame_.extracted_error_code);
fkastenholz488a4622019-08-26 06:24:46 -07004411 EXPECT_EQ("because I can", visitor_.connection_close_frame_.error_details);
fkastenholzb4dade72019-08-05 06:54:20 -07004412 } else {
fkastenholza14a7ae2019-08-07 05:21:22 -07004413 EXPECT_EQ(0x11u, visitor_.connection_close_frame_.extracted_error_code);
fkastenholz488a4622019-08-26 06:24:46 -07004414 // Error code is not prepended in GQUIC, so it is not removed and should
4415 // remain in the reason phrase.
4416 EXPECT_EQ("17767:because I can",
4417 visitor_.connection_close_frame_.error_details);
QUICHE teama6ef0a62019-03-07 20:34:33 -05004418 }
4419
4420 ASSERT_EQ(0u, visitor_.ack_frames_.size());
4421
4422 CheckFramingBoundaries(fragments, QUIC_INVALID_CONNECTION_CLOSE_DATA);
4423}
4424
fkastenholz04bd4f32019-04-16 12:24:38 -07004425// Test the CONNECTION_CLOSE/Application variant.
QUICHE teama6ef0a62019-03-07 20:34:33 -05004426TEST_P(QuicFramerTest, ApplicationCloseFrame) {
fkastenholz305e1732019-06-18 05:01:22 -07004427 if (!VersionHasIetfQuicFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05004428 // This frame does not exist in versions other than 99.
4429 return;
4430 }
zhongyi546cc452019-04-12 15:27:49 -07004431 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
QUICHE teama6ef0a62019-03-07 20:34:33 -05004432
4433 // clang-format off
4434 PacketFragments packet99 = {
4435 // type (short header, 4 byte packet number)
4436 {"",
4437 {0x43}},
4438 // connection_id
4439 {"",
4440 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
4441 // packet number
4442 {"",
4443 {0x12, 0x34, 0x56, 0x78}},
fkastenholz72f509b2019-04-10 09:17:49 -07004444 // frame type (IETF_CONNECTION_CLOSE/Application frame)
QUICHE teama6ef0a62019-03-07 20:34:33 -05004445 {"",
4446 {0x1d}},
4447 // error code
fkastenholz72f509b2019-04-10 09:17:49 -07004448 {"Unable to read connection close error code.",
fkastenholzd57d3f92019-07-16 09:05:17 -07004449 {kVarInt62TwoBytes + 0x00, 0x11}},
fkastenholz72f509b2019-04-10 09:17:49 -07004450 {"Unable to read connection close error details.",
QUICHE teama6ef0a62019-03-07 20:34:33 -05004451 {
4452 // error details length
4453 kVarInt62OneByte + 0x0d,
4454 // error details
4455 'b', 'e', 'c', 'a',
4456 'u', 's', 'e', ' ',
4457 'I', ' ', 'c', 'a',
4458 'n'}
4459 }
4460 };
4461 // clang-format on
4462
4463 std::unique_ptr<QuicEncryptedPacket> encrypted(
4464 AssemblePacketFromFragments(packet99));
4465 EXPECT_TRUE(framer_.ProcessPacket(*encrypted));
4466
4467 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
4468 ASSERT_TRUE(visitor_.header_.get());
4469 EXPECT_TRUE(CheckDecryption(
4470 *encrypted, !kIncludeVersion, !kIncludeDiversificationNonce,
4471 PACKET_8BYTE_CONNECTION_ID, PACKET_0BYTE_CONNECTION_ID));
4472
4473 EXPECT_EQ(0u, visitor_.stream_frames_.size());
4474
fkastenholz72f509b2019-04-10 09:17:49 -07004475 EXPECT_EQ(IETF_QUIC_APPLICATION_CONNECTION_CLOSE,
4476 visitor_.connection_close_frame_.close_type);
4477 EXPECT_EQ(122u, visitor_.connection_close_frame_.extracted_error_code);
fkastenholz88d08f42019-09-06 07:38:04 -07004478 EXPECT_EQ(0x11u, visitor_.connection_close_frame_.quic_error_code);
fkastenholz72f509b2019-04-10 09:17:49 -07004479 EXPECT_EQ("because I can", visitor_.connection_close_frame_.error_details);
QUICHE teama6ef0a62019-03-07 20:34:33 -05004480
4481 ASSERT_EQ(0u, visitor_.ack_frames_.size());
4482
fkastenholz04bd4f32019-04-16 12:24:38 -07004483 CheckFramingBoundaries(packet99, QUIC_INVALID_CONNECTION_CLOSE_DATA);
QUICHE teama6ef0a62019-03-07 20:34:33 -05004484}
4485
fkastenholzb4dade72019-08-05 06:54:20 -07004486// Check that we can extract an error code from an application close.
4487TEST_P(QuicFramerTest, ApplicationCloseFrameExtract) {
4488 if (!VersionHasIetfQuicFrames(framer_.transport_version())) {
4489 // This frame does not exist in versions other than 99.
4490 return;
4491 }
4492 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
4493
4494 // clang-format off
4495 PacketFragments packet99 = {
4496 // type (short header, 4 byte packet number)
4497 {"",
4498 {0x43}},
4499 // connection_id
4500 {"",
4501 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
4502 // packet number
4503 {"",
4504 {0x12, 0x34, 0x56, 0x78}},
4505 // frame type (IETF_CONNECTION_CLOSE/Application frame)
4506 {"",
4507 {0x1d}},
4508 // error code
4509 {"Unable to read connection close error code.",
4510 {kVarInt62OneByte + 0x11}},
4511 {"Unable to read connection close error details.",
4512 {
4513 // error details length
4514 kVarInt62OneByte + 0x13,
4515 // error details
4516 '1', '7', '7', '6',
4517 '7', ':', 'b', 'e',
4518 'c', 'a', 'u', 's',
4519 'e', ' ', 'I', ' ',
4520 'c', 'a', 'n'}
4521 }
4522 };
4523 // clang-format on
4524
4525 std::unique_ptr<QuicEncryptedPacket> encrypted(
4526 AssemblePacketFromFragments(packet99));
4527 EXPECT_TRUE(framer_.ProcessPacket(*encrypted));
4528
4529 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
4530 ASSERT_TRUE(visitor_.header_.get());
4531 EXPECT_TRUE(CheckDecryption(
4532 *encrypted, !kIncludeVersion, !kIncludeDiversificationNonce,
4533 PACKET_8BYTE_CONNECTION_ID, PACKET_0BYTE_CONNECTION_ID));
4534
4535 EXPECT_EQ(0u, visitor_.stream_frames_.size());
4536
4537 EXPECT_EQ(IETF_QUIC_APPLICATION_CONNECTION_CLOSE,
4538 visitor_.connection_close_frame_.close_type);
4539 EXPECT_EQ(17767u, visitor_.connection_close_frame_.extracted_error_code);
4540 EXPECT_EQ(0x11u, visitor_.connection_close_frame_.quic_error_code);
fkastenholz488a4622019-08-26 06:24:46 -07004541 EXPECT_EQ("because I can", visitor_.connection_close_frame_.error_details);
fkastenholzb4dade72019-08-05 06:54:20 -07004542
4543 ASSERT_EQ(0u, visitor_.ack_frames_.size());
4544
4545 CheckFramingBoundaries(packet99, QUIC_INVALID_CONNECTION_CLOSE_DATA);
4546}
4547
QUICHE teama6ef0a62019-03-07 20:34:33 -05004548TEST_P(QuicFramerTest, GoAwayFrame) {
fkastenholz305e1732019-06-18 05:01:22 -07004549 if (VersionHasIetfQuicFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05004550 // This frame is not supported in version 99.
4551 return;
4552 }
nharper9bb83462019-05-01 10:53:22 -07004553 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
QUICHE teama6ef0a62019-03-07 20:34:33 -05004554 // clang-format off
4555 PacketFragments packet = {
4556 // public flags (8 byte connection_id)
4557 {"",
4558 {0x28}},
4559 // connection_id
4560 {"",
4561 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
4562 // packet number
4563 {"",
4564 {0x12, 0x34, 0x56, 0x78}},
4565 // frame type (go away frame)
4566 {"",
4567 {0x03}},
4568 // error code
4569 {"Unable to read go away error code.",
4570 {0x00, 0x00, 0x00, 0x09}},
4571 // stream id
4572 {"Unable to read last good stream id.",
4573 {0x01, 0x02, 0x03, 0x04}},
4574 // stream id
4575 {"Unable to read goaway reason.",
4576 {
4577 // error details length
4578 0x0, 0x0d,
4579 // error details
4580 'b', 'e', 'c', 'a',
4581 'u', 's', 'e', ' ',
4582 'I', ' ', 'c', 'a',
4583 'n'}
4584 }
4585 };
4586
QUICHE teama6ef0a62019-03-07 20:34:33 -05004587 PacketFragments packet46 = {
4588 // type (short header, 4 byte packet number)
4589 {"",
4590 {0x43}},
4591 // connection_id
4592 {"",
4593 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
4594 // packet number
4595 {"",
4596 {0x12, 0x34, 0x56, 0x78}},
4597 // frame type (go away frame)
4598 {"",
4599 {0x03}},
4600 // error code
4601 {"Unable to read go away error code.",
4602 {0x00, 0x00, 0x00, 0x09}},
4603 // stream id
4604 {"Unable to read last good stream id.",
4605 {0x01, 0x02, 0x03, 0x04}},
4606 // stream id
4607 {"Unable to read goaway reason.",
4608 {
4609 // error details length
4610 0x0, 0x0d,
4611 // error details
4612 'b', 'e', 'c', 'a',
4613 'u', 's', 'e', ' ',
4614 'I', ' ', 'c', 'a',
4615 'n'}
4616 }
4617 };
4618 // clang-format on
4619
4620 PacketFragments& fragments =
fayang36825da2019-08-21 14:01:27 -07004621 framer_.transport_version() >= QUIC_VERSION_46 ? packet46 : packet;
QUICHE teama6ef0a62019-03-07 20:34:33 -05004622 std::unique_ptr<QuicEncryptedPacket> encrypted(
4623 AssemblePacketFromFragments(fragments));
4624 EXPECT_TRUE(framer_.ProcessPacket(*encrypted));
4625
4626 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
4627 ASSERT_TRUE(visitor_.header_.get());
4628 EXPECT_TRUE(CheckDecryption(
4629 *encrypted, !kIncludeVersion, !kIncludeDiversificationNonce,
4630 PACKET_8BYTE_CONNECTION_ID, PACKET_0BYTE_CONNECTION_ID));
4631
4632 EXPECT_EQ(kStreamId, visitor_.goaway_frame_.last_good_stream_id);
fkastenholz88d08f42019-09-06 07:38:04 -07004633 EXPECT_EQ(0x9u, visitor_.goaway_frame_.error_code);
QUICHE teama6ef0a62019-03-07 20:34:33 -05004634 EXPECT_EQ("because I can", visitor_.goaway_frame_.reason_phrase);
4635
4636 CheckFramingBoundaries(fragments, QUIC_INVALID_GOAWAY_DATA);
4637}
4638
4639TEST_P(QuicFramerTest, WindowUpdateFrame) {
fkastenholz305e1732019-06-18 05:01:22 -07004640 if (VersionHasIetfQuicFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05004641 // This frame is not in version 99, see MaxDataFrame and MaxStreamDataFrame
4642 // for Version 99 equivalents.
4643 return;
4644 }
nharper9bb83462019-05-01 10:53:22 -07004645 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
QUICHE teama6ef0a62019-03-07 20:34:33 -05004646 // clang-format off
4647 PacketFragments packet = {
4648 // public flags (8 byte connection_id)
4649 {"",
4650 {0x28}},
4651 // connection_id
4652 {"",
4653 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
4654 // packet number
4655 {"",
4656 {0x12, 0x34, 0x56, 0x78}},
4657 // frame type (window update frame)
4658 {"",
4659 {0x04}},
4660 // stream id
4661 {"Unable to read stream_id.",
4662 {0x01, 0x02, 0x03, 0x04}},
4663 // byte offset
4664 {"Unable to read window byte_offset.",
4665 {0x3A, 0x98, 0xFE, 0xDC,
4666 0x32, 0x10, 0x76, 0x54}},
4667 };
4668
QUICHE teama6ef0a62019-03-07 20:34:33 -05004669 PacketFragments packet46 = {
4670 // type (short header, 4 byte packet number)
4671 {"",
4672 {0x43}},
4673 // connection_id
4674 {"",
4675 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
4676 // packet number
4677 {"",
4678 {0x12, 0x34, 0x56, 0x78}},
4679 // frame type (window update frame)
4680 {"",
4681 {0x04}},
4682 // stream id
4683 {"Unable to read stream_id.",
4684 {0x01, 0x02, 0x03, 0x04}},
4685 // byte offset
4686 {"Unable to read window byte_offset.",
4687 {0x3A, 0x98, 0xFE, 0xDC,
4688 0x32, 0x10, 0x76, 0x54}},
4689 };
4690
4691 // clang-format on
4692
4693 PacketFragments& fragments =
fayang36825da2019-08-21 14:01:27 -07004694 framer_.transport_version() >= QUIC_VERSION_46 ? packet46 : packet;
QUICHE teama6ef0a62019-03-07 20:34:33 -05004695 std::unique_ptr<QuicEncryptedPacket> encrypted(
4696 AssemblePacketFromFragments(fragments));
4697 EXPECT_TRUE(framer_.ProcessPacket(*encrypted));
4698
4699 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
4700 ASSERT_TRUE(visitor_.header_.get());
4701 EXPECT_TRUE(CheckDecryption(
4702 *encrypted, !kIncludeVersion, !kIncludeDiversificationNonce,
4703 PACKET_8BYTE_CONNECTION_ID, PACKET_0BYTE_CONNECTION_ID));
4704
4705 EXPECT_EQ(kStreamId, visitor_.window_update_frame_.stream_id);
4706 EXPECT_EQ(kStreamOffset, visitor_.window_update_frame_.byte_offset);
4707
4708 CheckFramingBoundaries(fragments, QUIC_INVALID_WINDOW_UPDATE_DATA);
4709}
4710
4711TEST_P(QuicFramerTest, MaxDataFrame) {
fkastenholz305e1732019-06-18 05:01:22 -07004712 if (!VersionHasIetfQuicFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05004713 // This frame is available only in version 99.
4714 return;
4715 }
zhongyi546cc452019-04-12 15:27:49 -07004716 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
QUICHE teama6ef0a62019-03-07 20:34:33 -05004717 // clang-format off
4718 PacketFragments packet99 = {
4719 // type (short header, 4 byte packet number)
4720 {"",
4721 {0x43}},
4722 // connection_id
4723 {"",
4724 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
4725 // packet number
4726 {"",
4727 {0x12, 0x34, 0x56, 0x78}},
4728 // frame type (IETF_MAX_DATA frame)
4729 {"",
4730 {0x10}},
4731 // byte offset
4732 {"Can not read MAX_DATA byte-offset",
4733 {kVarInt62EightBytes + 0x3A, 0x98, 0xFE, 0xDC,
4734 0x32, 0x10, 0x76, 0x54}},
4735 };
4736 // clang-format on
4737
4738 std::unique_ptr<QuicEncryptedPacket> encrypted(
4739 AssemblePacketFromFragments(packet99));
4740 EXPECT_TRUE(framer_.ProcessPacket(*encrypted));
4741
4742 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
4743 ASSERT_TRUE(visitor_.header_.get());
4744 EXPECT_TRUE(CheckDecryption(
4745 *encrypted, !kIncludeVersion, !kIncludeDiversificationNonce,
4746 PACKET_8BYTE_CONNECTION_ID, PACKET_0BYTE_CONNECTION_ID));
4747
4748 EXPECT_EQ(QuicUtils::GetInvalidStreamId(framer_.transport_version()),
4749 visitor_.window_update_frame_.stream_id);
4750 EXPECT_EQ(kStreamOffset, visitor_.window_update_frame_.byte_offset);
4751
4752 CheckFramingBoundaries(packet99, QUIC_INVALID_MAX_DATA_FRAME_DATA);
4753}
4754
4755TEST_P(QuicFramerTest, MaxStreamDataFrame) {
fkastenholz305e1732019-06-18 05:01:22 -07004756 if (!VersionHasIetfQuicFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05004757 // This frame available only in version 99.
4758 return;
4759 }
zhongyi546cc452019-04-12 15:27:49 -07004760 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
QUICHE teama6ef0a62019-03-07 20:34:33 -05004761 // clang-format off
4762 PacketFragments packet99 = {
4763 // type (short header, 4 byte packet number)
4764 {"",
4765 {0x43}},
4766 // connection_id
4767 {"",
4768 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
4769 // packet number
4770 {"",
4771 {0x12, 0x34, 0x56, 0x78}},
4772 // frame type (IETF_MAX_STREAM_DATA frame)
4773 {"",
4774 {0x11}},
4775 // stream id
4776 {"Can not read MAX_STREAM_DATA stream id",
4777 {kVarInt62FourBytes + 0x01, 0x02, 0x03, 0x04}},
4778 // byte offset
4779 {"Can not read MAX_STREAM_DATA byte-count",
4780 {kVarInt62EightBytes + 0x3A, 0x98, 0xFE, 0xDC,
4781 0x32, 0x10, 0x76, 0x54}},
4782 };
4783 // clang-format on
4784
4785 std::unique_ptr<QuicEncryptedPacket> encrypted(
4786 AssemblePacketFromFragments(packet99));
4787 EXPECT_TRUE(framer_.ProcessPacket(*encrypted));
4788
4789 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
4790 ASSERT_TRUE(visitor_.header_.get());
4791 EXPECT_TRUE(CheckDecryption(
4792 *encrypted, !kIncludeVersion, !kIncludeDiversificationNonce,
4793 PACKET_8BYTE_CONNECTION_ID, PACKET_0BYTE_CONNECTION_ID));
4794
4795 EXPECT_EQ(kStreamId, visitor_.window_update_frame_.stream_id);
4796 EXPECT_EQ(kStreamOffset, visitor_.window_update_frame_.byte_offset);
4797
4798 CheckFramingBoundaries(packet99, QUIC_INVALID_MAX_STREAM_DATA_FRAME_DATA);
4799}
4800
4801TEST_P(QuicFramerTest, BlockedFrame) {
zhongyi546cc452019-04-12 15:27:49 -07004802 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
QUICHE teama6ef0a62019-03-07 20:34:33 -05004803 // clang-format off
4804 PacketFragments packet = {
4805 // public flags (8 byte connection_id)
4806 {"",
4807 {0x28}},
4808 // connection_id
4809 {"",
4810 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
4811 // packet number
4812 {"",
4813 {0x12, 0x34, 0x56, 0x78}},
4814 // frame type (blocked frame)
4815 {"",
4816 {0x05}},
4817 // stream id
4818 {"Unable to read stream_id.",
4819 {0x01, 0x02, 0x03, 0x04}},
4820 };
4821
QUICHE teama6ef0a62019-03-07 20:34:33 -05004822 PacketFragments packet46 = {
4823 // type (short header, 4 byte packet number)
4824 {"",
4825 {0x43}},
4826 // connection_id
4827 {"",
4828 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
4829 // packet number
4830 {"",
4831 {0x12, 0x34, 0x56, 0x78}},
4832 // frame type (blocked frame)
4833 {"",
4834 {0x05}},
4835 // stream id
4836 {"Unable to read stream_id.",
4837 {0x01, 0x02, 0x03, 0x04}},
4838 };
4839
4840 PacketFragments packet99 = {
4841 // type (short header, 4 byte packet number)
4842 {"",
4843 {0x43}},
4844 // connection_id
4845 {"",
4846 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
4847 // packet number
4848 {"",
4849 {0x12, 0x34, 0x56, 0x78}},
4850 // frame type (IETF_STREAM_BLOCKED frame)
4851 {"",
4852 {0x15}},
4853 // stream id
4854 {"Can not read stream blocked stream id.",
4855 {kVarInt62FourBytes + 0x01, 0x02, 0x03, 0x04}},
4856 // Offset
4857 {"Can not read stream blocked offset.",
4858 {kVarInt62EightBytes + 0x3a, 0x98, 0xFE, 0xDC, 0x32, 0x10, 0x76, 0x54}},
4859 };
4860 // clang-format on
4861
4862 PacketFragments& fragments =
fkastenholz305e1732019-06-18 05:01:22 -07004863 VersionHasIetfQuicFrames(framer_.transport_version())
QUICHE teama6ef0a62019-03-07 20:34:33 -05004864 ? packet99
fayang36825da2019-08-21 14:01:27 -07004865 : (framer_.transport_version() >= QUIC_VERSION_46 ? packet46
4866 : packet);
QUICHE teama6ef0a62019-03-07 20:34:33 -05004867 std::unique_ptr<QuicEncryptedPacket> encrypted(
4868 AssemblePacketFromFragments(fragments));
4869 EXPECT_TRUE(framer_.ProcessPacket(*encrypted));
4870
4871 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
4872 ASSERT_TRUE(visitor_.header_.get());
4873 EXPECT_TRUE(CheckDecryption(
4874 *encrypted, !kIncludeVersion, !kIncludeDiversificationNonce,
4875 PACKET_8BYTE_CONNECTION_ID, PACKET_0BYTE_CONNECTION_ID));
4876
fkastenholz305e1732019-06-18 05:01:22 -07004877 if (VersionHasIetfQuicFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05004878 EXPECT_EQ(kStreamOffset, visitor_.blocked_frame_.offset);
4879 } else {
4880 EXPECT_EQ(0u, visitor_.blocked_frame_.offset);
4881 }
4882 EXPECT_EQ(kStreamId, visitor_.blocked_frame_.stream_id);
4883
fkastenholz305e1732019-06-18 05:01:22 -07004884 if (VersionHasIetfQuicFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05004885 CheckFramingBoundaries(fragments, QUIC_INVALID_STREAM_BLOCKED_DATA);
4886 } else {
4887 CheckFramingBoundaries(fragments, QUIC_INVALID_BLOCKED_DATA);
4888 }
4889}
4890
4891TEST_P(QuicFramerTest, PingFrame) {
zhongyi546cc452019-04-12 15:27:49 -07004892 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
QUICHE teama6ef0a62019-03-07 20:34:33 -05004893 // clang-format off
4894 unsigned char packet[] = {
4895 // public flags (8 byte connection_id)
4896 0x28,
4897 // connection_id
4898 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
4899 // packet number
4900 0x12, 0x34, 0x56, 0x78,
4901
4902 // frame type (ping frame)
4903 0x07,
4904 };
4905
QUICHE teama6ef0a62019-03-07 20:34:33 -05004906 unsigned char packet46[] = {
4907 // type (short header, 4 byte packet number)
4908 0x43,
4909 // connection_id
4910 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
4911 // packet number
4912 0x12, 0x34, 0x56, 0x78,
4913
4914 // frame type
4915 0x07,
4916 };
4917
4918 unsigned char packet99[] = {
4919 // type (short header, 4 byte packet number)
4920 0x43,
4921 // connection_id
4922 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
4923 // packet number
4924 0x12, 0x34, 0x56, 0x78,
4925
4926 // frame type (IETF_PING frame)
4927 0x01,
4928 };
4929 // clang-format on
4930
4931 QuicEncryptedPacket encrypted(
fkastenholz305e1732019-06-18 05:01:22 -07004932 AsChars(VersionHasIetfQuicFrames(framer_.transport_version())
QUICHE teama6ef0a62019-03-07 20:34:33 -05004933 ? packet99
fayang36825da2019-08-21 14:01:27 -07004934 : (framer_.transport_version() >= QUIC_VERSION_46 ? packet46
4935 : packet)),
fkastenholz305e1732019-06-18 05:01:22 -07004936 VersionHasIetfQuicFrames(framer_.transport_version())
QUICHE teama6ef0a62019-03-07 20:34:33 -05004937 ? QUIC_ARRAYSIZE(packet99)
fayang36825da2019-08-21 14:01:27 -07004938 : (framer_.transport_version() >= QUIC_VERSION_46
QUICHE teama6ef0a62019-03-07 20:34:33 -05004939 ? QUIC_ARRAYSIZE(packet46)
fayang36825da2019-08-21 14:01:27 -07004940 : QUIC_ARRAYSIZE(packet)),
QUICHE teama6ef0a62019-03-07 20:34:33 -05004941 false);
4942 EXPECT_TRUE(framer_.ProcessPacket(encrypted));
4943
4944 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
4945 ASSERT_TRUE(visitor_.header_.get());
4946 EXPECT_TRUE(CheckDecryption(
4947 encrypted, !kIncludeVersion, !kIncludeDiversificationNonce,
4948 PACKET_8BYTE_CONNECTION_ID, PACKET_0BYTE_CONNECTION_ID));
4949
4950 EXPECT_EQ(1u, visitor_.ping_frames_.size());
4951
4952 // No need to check the PING frame boundaries because it has no payload.
4953}
4954
4955TEST_P(QuicFramerTest, MessageFrame) {
fayang36825da2019-08-21 14:01:27 -07004956 if (framer_.transport_version() <= QUIC_VERSION_43) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05004957 return;
4958 }
zhongyi546cc452019-04-12 15:27:49 -07004959 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
QUICHE teama6ef0a62019-03-07 20:34:33 -05004960 // clang-format off
QUICHE teama6ef0a62019-03-07 20:34:33 -05004961 PacketFragments packet46 = {
4962 // type (short header, 4 byte packet number)
4963 {"",
4964 {0x43}},
4965 // connection_id
4966 {"",
4967 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
4968 // packet number
4969 {"",
4970 {0x12, 0x34, 0x56, 0x78}},
4971 // message frame type.
4972 {"",
4973 { 0x21 }},
4974 // message length
4975 {"Unable to read message length",
4976 {0x07}},
4977 // message data
4978 {"Unable to read message data",
4979 {'m', 'e', 's', 's', 'a', 'g', 'e'}},
4980 // message frame no length.
4981 {"",
4982 { 0x20 }},
4983 // message data
4984 {{},
4985 {'m', 'e', 's', 's', 'a', 'g', 'e', '2'}},
4986 };
4987 // clang-format on
4988
fayang36825da2019-08-21 14:01:27 -07004989 std::unique_ptr<QuicEncryptedPacket> encrypted(
4990 AssemblePacketFromFragments(packet46));
QUICHE teama6ef0a62019-03-07 20:34:33 -05004991 EXPECT_TRUE(framer_.ProcessPacket(*encrypted));
4992
4993 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
4994 ASSERT_TRUE(visitor_.header_.get());
4995 EXPECT_TRUE(CheckDecryption(
4996 *encrypted, !kIncludeVersion, !kIncludeDiversificationNonce,
4997 PACKET_8BYTE_CONNECTION_ID, PACKET_0BYTE_CONNECTION_ID));
4998
4999 ASSERT_EQ(2u, visitor_.message_frames_.size());
5000 EXPECT_EQ(7u, visitor_.message_frames_[0]->message_length);
5001 EXPECT_EQ(8u, visitor_.message_frames_[1]->message_length);
5002
fayang36825da2019-08-21 14:01:27 -07005003 CheckFramingBoundaries(packet46, QUIC_INVALID_MESSAGE_DATA);
QUICHE teama6ef0a62019-03-07 20:34:33 -05005004}
5005
5006TEST_P(QuicFramerTest, PublicResetPacketV33) {
5007 // clang-format off
5008 PacketFragments packet = {
5009 // public flags (public reset, 8 byte connection_id)
5010 {"",
5011 {0x0A}},
5012 // connection_id
5013 {"",
5014 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
5015 {"Unable to read reset message.",
5016 {
5017 // message tag (kPRST)
5018 'P', 'R', 'S', 'T',
5019 // num_entries (2) + padding
5020 0x02, 0x00, 0x00, 0x00,
5021 // tag kRNON
5022 'R', 'N', 'O', 'N',
5023 // end offset 8
5024 0x08, 0x00, 0x00, 0x00,
5025 // tag kRSEQ
5026 'R', 'S', 'E', 'Q',
5027 // end offset 16
5028 0x10, 0x00, 0x00, 0x00,
5029 // nonce proof
5030 0x89, 0x67, 0x45, 0x23,
5031 0x01, 0xEF, 0xCD, 0xAB,
5032 // rejected packet number
5033 0xBC, 0x9A, 0x78, 0x56,
5034 0x34, 0x12, 0x00, 0x00,
5035 }
5036 }
5037 };
5038 // clang-format on
5039 if (framer_.transport_version() > QUIC_VERSION_43) {
5040 return;
5041 }
5042
5043 std::unique_ptr<QuicEncryptedPacket> encrypted(
5044 AssemblePacketFromFragments(packet));
5045 EXPECT_TRUE(framer_.ProcessPacket(*encrypted));
5046 ASSERT_EQ(QUIC_NO_ERROR, framer_.error());
5047 ASSERT_TRUE(visitor_.public_reset_packet_.get());
5048 EXPECT_EQ(FramerTestConnectionId(),
5049 visitor_.public_reset_packet_->connection_id);
5050 EXPECT_EQ(kNonceProof, visitor_.public_reset_packet_->nonce_proof);
5051 EXPECT_EQ(
5052 IpAddressFamily::IP_UNSPEC,
5053 visitor_.public_reset_packet_->client_address.host().address_family());
5054
5055 CheckFramingBoundaries(packet, QUIC_INVALID_PUBLIC_RST_PACKET);
5056}
5057
5058TEST_P(QuicFramerTest, PublicResetPacket) {
5059 QuicFramerPeer::SetPerspective(&framer_, Perspective::IS_CLIENT);
5060
5061 // clang-format off
5062 PacketFragments packet = {
5063 // public flags (public reset, 8 byte connection_id)
5064 {"",
5065 {0x0E}},
5066 // connection_id
5067 {"",
5068 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
5069 {"Unable to read reset message.",
5070 {
5071 // message tag (kPRST)
5072 'P', 'R', 'S', 'T',
5073 // num_entries (2) + padding
5074 0x02, 0x00, 0x00, 0x00,
5075 // tag kRNON
5076 'R', 'N', 'O', 'N',
5077 // end offset 8
5078 0x08, 0x00, 0x00, 0x00,
5079 // tag kRSEQ
5080 'R', 'S', 'E', 'Q',
5081 // end offset 16
5082 0x10, 0x00, 0x00, 0x00,
5083 // nonce proof
5084 0x89, 0x67, 0x45, 0x23,
5085 0x01, 0xEF, 0xCD, 0xAB,
5086 // rejected packet number
5087 0xBC, 0x9A, 0x78, 0x56,
5088 0x34, 0x12, 0x00, 0x00,
5089 }
5090 }
5091 };
5092 // clang-format on
5093
5094 if (framer_.transport_version() > QUIC_VERSION_43) {
5095 return;
5096 }
5097
5098 std::unique_ptr<QuicEncryptedPacket> encrypted(
5099 AssemblePacketFromFragments(packet));
5100 EXPECT_TRUE(framer_.ProcessPacket(*encrypted));
5101 ASSERT_EQ(QUIC_NO_ERROR, framer_.error());
5102 ASSERT_TRUE(visitor_.public_reset_packet_.get());
5103 EXPECT_EQ(FramerTestConnectionId(),
5104 visitor_.public_reset_packet_->connection_id);
5105 EXPECT_EQ(kNonceProof, visitor_.public_reset_packet_->nonce_proof);
5106 EXPECT_EQ(
5107 IpAddressFamily::IP_UNSPEC,
5108 visitor_.public_reset_packet_->client_address.host().address_family());
5109
5110 CheckFramingBoundaries(packet, QUIC_INVALID_PUBLIC_RST_PACKET);
5111}
5112
5113TEST_P(QuicFramerTest, PublicResetPacketWithTrailingJunk) {
5114 // clang-format off
5115 unsigned char packet[] = {
5116 // public flags (public reset, 8 byte connection_id)
5117 0x0A,
5118 // connection_id
5119 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
5120 // message tag (kPRST)
5121 'P', 'R', 'S', 'T',
5122 // num_entries (2) + padding
5123 0x02, 0x00, 0x00, 0x00,
5124 // tag kRNON
5125 'R', 'N', 'O', 'N',
5126 // end offset 8
5127 0x08, 0x00, 0x00, 0x00,
5128 // tag kRSEQ
5129 'R', 'S', 'E', 'Q',
5130 // end offset 16
5131 0x10, 0x00, 0x00, 0x00,
5132 // nonce proof
5133 0x89, 0x67, 0x45, 0x23,
5134 0x01, 0xEF, 0xCD, 0xAB,
5135 // rejected packet number
5136 0xBC, 0x9A, 0x78, 0x56,
5137 0x34, 0x12, 0x00, 0x00,
5138 // trailing junk
5139 'j', 'u', 'n', 'k',
5140 };
5141 // clang-format on
5142 if (framer_.transport_version() > QUIC_VERSION_43) {
5143 return;
5144 }
5145
5146 QuicEncryptedPacket encrypted(AsChars(packet), QUIC_ARRAYSIZE(packet), false);
5147 EXPECT_FALSE(framer_.ProcessPacket(encrypted));
5148 ASSERT_EQ(QUIC_INVALID_PUBLIC_RST_PACKET, framer_.error());
5149 EXPECT_EQ("Unable to read reset message.", framer_.detailed_error());
5150}
5151
5152TEST_P(QuicFramerTest, PublicResetPacketWithClientAddress) {
5153 // clang-format off
5154 PacketFragments packet = {
5155 // public flags (public reset, 8 byte connection_id)
5156 {"",
5157 {0x0A}},
5158 // connection_id
5159 {"",
5160 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
5161 {"Unable to read reset message.",
5162 {
5163 // message tag (kPRST)
5164 'P', 'R', 'S', 'T',
5165 // num_entries (2) + padding
5166 0x03, 0x00, 0x00, 0x00,
5167 // tag kRNON
5168 'R', 'N', 'O', 'N',
5169 // end offset 8
5170 0x08, 0x00, 0x00, 0x00,
5171 // tag kRSEQ
5172 'R', 'S', 'E', 'Q',
5173 // end offset 16
5174 0x10, 0x00, 0x00, 0x00,
5175 // tag kCADR
5176 'C', 'A', 'D', 'R',
5177 // end offset 24
5178 0x18, 0x00, 0x00, 0x00,
5179 // nonce proof
5180 0x89, 0x67, 0x45, 0x23,
5181 0x01, 0xEF, 0xCD, 0xAB,
5182 // rejected packet number
5183 0xBC, 0x9A, 0x78, 0x56,
5184 0x34, 0x12, 0x00, 0x00,
5185 // client address: 4.31.198.44:443
5186 0x02, 0x00,
5187 0x04, 0x1F, 0xC6, 0x2C,
5188 0xBB, 0x01,
5189 }
5190 }
5191 };
5192 // clang-format on
5193 if (framer_.transport_version() > QUIC_VERSION_43) {
5194 return;
5195 }
5196
5197 std::unique_ptr<QuicEncryptedPacket> encrypted(
5198 AssemblePacketFromFragments(packet));
5199 EXPECT_TRUE(framer_.ProcessPacket(*encrypted));
5200 ASSERT_EQ(QUIC_NO_ERROR, framer_.error());
5201 ASSERT_TRUE(visitor_.public_reset_packet_.get());
5202 EXPECT_EQ(FramerTestConnectionId(),
5203 visitor_.public_reset_packet_->connection_id);
5204 EXPECT_EQ(kNonceProof, visitor_.public_reset_packet_->nonce_proof);
5205 EXPECT_EQ("4.31.198.44",
5206 visitor_.public_reset_packet_->client_address.host().ToString());
5207 EXPECT_EQ(443, visitor_.public_reset_packet_->client_address.port());
5208
5209 CheckFramingBoundaries(packet, QUIC_INVALID_PUBLIC_RST_PACKET);
5210}
5211
5212TEST_P(QuicFramerTest, IetfStatelessResetPacket) {
5213 // clang-format off
5214 unsigned char packet[] = {
5215 // type (short packet, 1 byte packet number)
5216 0x50,
QUICHE teama6ef0a62019-03-07 20:34:33 -05005217 // Random bytes
5218 0x01, 0x11, 0x02, 0x22, 0x03, 0x33, 0x04, 0x44,
5219 0x01, 0x11, 0x02, 0x22, 0x03, 0x33, 0x04, 0x44,
5220 0x01, 0x11, 0x02, 0x22, 0x03, 0x33, 0x04, 0x44,
5221 0x01, 0x11, 0x02, 0x22, 0x03, 0x33, 0x04, 0x44,
5222 // stateless reset token
5223 0xB5, 0x69, 0x0F, 0x00, 0x00, 0x00, 0x00, 0x00,
5224 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
5225 };
5226 // clang-format on
5227 if (framer_.transport_version() <= QUIC_VERSION_43) {
5228 return;
5229 }
5230 QuicFramerPeer::SetPerspective(&framer_, Perspective::IS_CLIENT);
dschinazib953d022019-08-01 18:05:58 -07005231 QuicFramerPeer::SetLastSerializedServerConnectionId(&framer_,
5232 TestConnectionId(0x33));
QUICHE teama6ef0a62019-03-07 20:34:33 -05005233 decrypter_ = new test::TestDecrypter();
zhongyi546cc452019-04-12 15:27:49 -07005234 if (framer_.version().KnowsWhichDecrypterToUse()) {
vasilvv0fc587f2019-09-06 13:33:08 -07005235 framer_.InstallDecrypter(
5236 ENCRYPTION_INITIAL,
5237 std::make_unique<NullDecrypter>(Perspective::IS_CLIENT));
zhongyi546cc452019-04-12 15:27:49 -07005238 framer_.InstallDecrypter(ENCRYPTION_ZERO_RTT,
5239 std::unique_ptr<QuicDecrypter>(decrypter_));
5240 } else {
vasilvv0fc587f2019-09-06 13:33:08 -07005241 framer_.SetDecrypter(ENCRYPTION_INITIAL, std::make_unique<NullDecrypter>(
5242 Perspective::IS_CLIENT));
zhongyi546cc452019-04-12 15:27:49 -07005243 framer_.SetAlternativeDecrypter(
5244 ENCRYPTION_ZERO_RTT, std::unique_ptr<QuicDecrypter>(decrypter_), false);
5245 }
QUICHE teama6ef0a62019-03-07 20:34:33 -05005246 // This packet cannot be decrypted because diversification nonce is missing.
5247 QuicEncryptedPacket encrypted(AsChars(packet), QUIC_ARRAYSIZE(packet), false);
5248 EXPECT_TRUE(framer_.ProcessPacket(encrypted));
5249 ASSERT_EQ(QUIC_NO_ERROR, framer_.error());
5250 ASSERT_TRUE(visitor_.stateless_reset_packet_.get());
5251 EXPECT_EQ(kTestStatelessResetToken,
5252 visitor_.stateless_reset_packet_->stateless_reset_token);
5253}
5254
5255TEST_P(QuicFramerTest, IetfStatelessResetPacketInvalidStatelessResetToken) {
5256 // clang-format off
5257 unsigned char packet[] = {
5258 // type (short packet, 1 byte packet number)
5259 0x50,
dschinazib953d022019-08-01 18:05:58 -07005260 // Random bytes
5261 0x01, 0x11, 0x02, 0x22, 0x03, 0x33, 0x04, 0x44,
5262 0x01, 0x11, 0x02, 0x22, 0x03, 0x33, 0x04, 0x44,
5263 0x01, 0x11, 0x02, 0x22, 0x03, 0x33, 0x04, 0x44,
5264 0x01, 0x11, 0x02, 0x22, 0x03, 0x33, 0x04, 0x44,
QUICHE teama6ef0a62019-03-07 20:34:33 -05005265 // stateless reset token
5266 0xB6, 0x69, 0x0F, 0x00, 0x00, 0x00, 0x00, 0x00,
5267 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
5268 };
5269 // clang-format on
5270 if (framer_.transport_version() <= QUIC_VERSION_43) {
5271 return;
5272 }
dschinazib953d022019-08-01 18:05:58 -07005273 QuicFramerPeer::SetLastSerializedServerConnectionId(&framer_,
5274 TestConnectionId(0x33));
QUICHE teama6ef0a62019-03-07 20:34:33 -05005275 QuicFramerPeer::SetPerspective(&framer_, Perspective::IS_CLIENT);
QUICHE teama6ef0a62019-03-07 20:34:33 -05005276 decrypter_ = new test::TestDecrypter();
zhongyi546cc452019-04-12 15:27:49 -07005277 if (framer_.version().KnowsWhichDecrypterToUse()) {
vasilvv0fc587f2019-09-06 13:33:08 -07005278 framer_.InstallDecrypter(
5279 ENCRYPTION_INITIAL,
5280 std::make_unique<NullDecrypter>(Perspective::IS_CLIENT));
zhongyi546cc452019-04-12 15:27:49 -07005281 framer_.InstallDecrypter(ENCRYPTION_ZERO_RTT,
5282 std::unique_ptr<QuicDecrypter>(decrypter_));
5283 } else {
vasilvv0fc587f2019-09-06 13:33:08 -07005284 framer_.SetDecrypter(ENCRYPTION_INITIAL, std::make_unique<NullDecrypter>(
5285 Perspective::IS_CLIENT));
zhongyi546cc452019-04-12 15:27:49 -07005286 framer_.SetAlternativeDecrypter(
5287 ENCRYPTION_ZERO_RTT, std::unique_ptr<QuicDecrypter>(decrypter_), false);
5288 }
QUICHE teama6ef0a62019-03-07 20:34:33 -05005289 // This packet cannot be decrypted because diversification nonce is missing.
5290 QuicEncryptedPacket encrypted(AsChars(packet), QUIC_ARRAYSIZE(packet), false);
5291 EXPECT_FALSE(framer_.ProcessPacket(encrypted));
5292 EXPECT_EQ(QUIC_DECRYPTION_FAILURE, framer_.error());
5293 ASSERT_FALSE(visitor_.stateless_reset_packet_);
5294}
5295
dschinazi072da7c2019-05-07 17:57:42 -07005296TEST_P(QuicFramerTest, VersionNegotiationPacketClient) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05005297 // clang-format off
5298 PacketFragments packet = {
5299 // public flags (version, 8 byte connection_id)
5300 {"",
5301 {0x29}},
5302 // connection_id
5303 {"",
5304 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
5305 // version tag
5306 {"Unable to read supported version in negotiation.",
5307 {QUIC_VERSION_BYTES,
5308 'Q', '2', '.', '0'}},
5309 };
5310
fayang36825da2019-08-21 14:01:27 -07005311 PacketFragments packet46 = {
QUICHE teama6ef0a62019-03-07 20:34:33 -05005312 // type (long header)
5313 {"",
5314 {0x8F}},
5315 // version tag
5316 {"",
5317 {0x00, 0x00, 0x00, 0x00}},
5318 {"",
5319 {0x05}},
5320 // connection_id
5321 {"",
5322 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
5323 // Supported versions
5324 {"Unable to read supported version in negotiation.",
5325 {QUIC_VERSION_BYTES,
5326 'Q', '2', '.', '0'}},
5327 };
dschinazi48ac9192019-07-31 00:07:26 -07005328
5329 PacketFragments packet99 = {
5330 // type (long header)
5331 {"",
5332 {0x8F}},
5333 // version tag
5334 {"",
5335 {0x00, 0x00, 0x00, 0x00}},
5336 {"",
5337 {0x08}},
5338 // connection_id
5339 {"",
5340 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
5341 {"",
5342 {0x00}},
5343 // Supported versions
5344 {"Unable to read supported version in negotiation.",
5345 {QUIC_VERSION_BYTES,
5346 'Q', '2', '.', '0'}},
5347 };
QUICHE teama6ef0a62019-03-07 20:34:33 -05005348 // clang-format on
5349
5350 QuicFramerPeer::SetPerspective(&framer_, Perspective::IS_CLIENT);
5351
5352 PacketFragments& fragments =
dschinazi48ac9192019-07-31 00:07:26 -07005353 framer_.transport_version() >= QUIC_VERSION_99
5354 ? packet99
fayang36825da2019-08-21 14:01:27 -07005355 : framer_.transport_version() > QUIC_VERSION_43 ? packet46 : packet;
QUICHE teama6ef0a62019-03-07 20:34:33 -05005356 std::unique_ptr<QuicEncryptedPacket> encrypted(
5357 AssemblePacketFromFragments(fragments));
5358 EXPECT_TRUE(framer_.ProcessPacket(*encrypted));
5359 ASSERT_EQ(QUIC_NO_ERROR, framer_.error());
5360 ASSERT_TRUE(visitor_.version_negotiation_packet_.get());
nharper4fd11052019-06-04 14:23:22 -07005361 EXPECT_EQ(1u, visitor_.version_negotiation_packet_->versions.size());
QUICHE teama6ef0a62019-03-07 20:34:33 -05005362 EXPECT_EQ(GetParam(), visitor_.version_negotiation_packet_->versions[0]);
5363
5364 // Remove the last version from the packet so that every truncated
5365 // version of the packet is invalid, otherwise checking boundaries
5366 // is annoyingly complicated.
5367 for (size_t i = 0; i < 4; ++i) {
5368 fragments.back().fragment.pop_back();
5369 }
5370 CheckFramingBoundaries(fragments, QUIC_INVALID_VERSION_NEGOTIATION_PACKET);
5371}
5372
dschinazi072da7c2019-05-07 17:57:42 -07005373TEST_P(QuicFramerTest, VersionNegotiationPacketServer) {
fayang36825da2019-08-21 14:01:27 -07005374 if (framer_.transport_version() <= QUIC_VERSION_43) {
dschinazi072da7c2019-05-07 17:57:42 -07005375 return;
5376 }
5377
5378 QuicFramerPeer::SetPerspective(&framer_, Perspective::IS_SERVER);
5379 // clang-format off
5380 unsigned char packet[] = {
5381 // public flags (long header with all ignored bits set)
5382 0xFF,
5383 // version
5384 0x00, 0x00, 0x00, 0x00,
5385 // connection ID lengths
5386 0x50,
5387 // destination connection ID
5388 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x11,
5389 // supported versions
5390 QUIC_VERSION_BYTES,
5391 'Q', '2', '.', '0',
5392 };
dschinazi48ac9192019-07-31 00:07:26 -07005393 unsigned char packet2[] = {
5394 // public flags (long header with all ignored bits set)
5395 0xFF,
5396 // version
5397 0x00, 0x00, 0x00, 0x00,
5398 // destination connection ID length
5399 0x08,
5400 // destination connection ID
5401 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x11,
5402 // source connection ID length
5403 0x00,
5404 // supported versions
5405 QUIC_VERSION_BYTES,
5406 'Q', '2', '.', '0',
5407 };
dschinazi072da7c2019-05-07 17:57:42 -07005408 // clang-format on
dschinazi48ac9192019-07-31 00:07:26 -07005409 unsigned char* p = packet;
5410 size_t p_length = QUIC_ARRAYSIZE(packet);
5411 if (framer_.version().HasLengthPrefixedConnectionIds()) {
5412 p = packet2;
5413 p_length = QUIC_ARRAYSIZE(packet2);
5414 }
dschinazi072da7c2019-05-07 17:57:42 -07005415
dschinazi48ac9192019-07-31 00:07:26 -07005416 QuicEncryptedPacket encrypted(AsChars(p), p_length, false);
dschinazi072da7c2019-05-07 17:57:42 -07005417 EXPECT_FALSE(framer_.ProcessPacket(encrypted));
5418 EXPECT_EQ(QUIC_INVALID_VERSION_NEGOTIATION_PACKET, framer_.error());
5419 EXPECT_EQ("Server received version negotiation packet.",
5420 framer_.detailed_error());
5421 EXPECT_FALSE(visitor_.version_negotiation_packet_.get());
5422}
5423
QUICHE teama6ef0a62019-03-07 20:34:33 -05005424TEST_P(QuicFramerTest, OldVersionNegotiationPacket) {
5425 // clang-format off
5426 PacketFragments packet = {
5427 // public flags (version, 8 byte connection_id)
5428 {"",
5429 {0x2D}},
5430 // connection_id
5431 {"",
5432 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
5433 // version tag
5434 {"Unable to read supported version in negotiation.",
5435 {QUIC_VERSION_BYTES,
5436 'Q', '2', '.', '0'}},
5437 };
5438 // clang-format on
5439
5440 if (framer_.transport_version() > QUIC_VERSION_43) {
5441 return;
5442 }
5443
5444 QuicFramerPeer::SetPerspective(&framer_, Perspective::IS_CLIENT);
5445
5446 std::unique_ptr<QuicEncryptedPacket> encrypted(
5447 AssemblePacketFromFragments(packet));
5448 EXPECT_TRUE(framer_.ProcessPacket(*encrypted));
5449 ASSERT_EQ(QUIC_NO_ERROR, framer_.error());
5450 ASSERT_TRUE(visitor_.version_negotiation_packet_.get());
nharper4fd11052019-06-04 14:23:22 -07005451 EXPECT_EQ(1u, visitor_.version_negotiation_packet_->versions.size());
QUICHE teama6ef0a62019-03-07 20:34:33 -05005452 EXPECT_EQ(GetParam(), visitor_.version_negotiation_packet_->versions[0]);
5453
5454 // Remove the last version from the packet so that every truncated
5455 // version of the packet is invalid, otherwise checking boundaries
5456 // is annoyingly complicated.
5457 for (size_t i = 0; i < 4; ++i) {
5458 packet.back().fragment.pop_back();
5459 }
5460 CheckFramingBoundaries(packet, QUIC_INVALID_VERSION_NEGOTIATION_PACKET);
5461}
5462
dschinazi244f6dc2019-05-06 15:45:16 -07005463TEST_P(QuicFramerTest, ParseIetfRetryPacket) {
5464 if (!framer_.version().SupportsRetry()) {
5465 return;
5466 }
5467 // IETF RETRY is only sent from client to server.
5468 QuicFramerPeer::SetPerspective(&framer_, Perspective::IS_CLIENT);
5469 // clang-format off
5470 unsigned char packet[] = {
5471 // public flags (long header with packet type RETRY and ODCIL=8)
5472 0xF5,
5473 // version
5474 QUIC_VERSION_BYTES,
5475 // connection ID lengths
5476 0x05,
5477 // source connection ID
5478 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x11,
5479 // original destination connection ID
5480 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
5481 // retry token
5482 'H', 'e', 'l', 'l', 'o', ' ', 't', 'h', 'i', 's',
5483 ' ', 'i', 's', ' ', 'R', 'E', 'T', 'R', 'Y', '!',
5484 };
dschinazi48ac9192019-07-31 00:07:26 -07005485 unsigned char packet99[] = {
5486 // public flags (long header with packet type RETRY)
5487 0xF0,
5488 // version
5489 QUIC_VERSION_BYTES,
5490 // destination connection ID length
5491 0x00,
5492 // source connection ID length
5493 0x08,
5494 // source connection ID
5495 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x11,
5496 // original destination connection ID length
5497 0x08,
5498 // original destination connection ID
5499 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
5500 // retry token
5501 'H', 'e', 'l', 'l', 'o', ' ', 't', 'h', 'i', 's',
5502 ' ', 'i', 's', ' ', 'R', 'E', 'T', 'R', 'Y', '!',
5503 };
dschinazi244f6dc2019-05-06 15:45:16 -07005504 // clang-format on
5505
dschinazi48ac9192019-07-31 00:07:26 -07005506 unsigned char* p = packet;
5507 size_t p_length = QUIC_ARRAYSIZE(packet);
5508 if (framer_.transport_version() == QUIC_VERSION_99) {
5509 p = packet99;
5510 p_length = QUIC_ARRAYSIZE(packet99);
5511 }
5512 QuicEncryptedPacket encrypted(AsChars(p), p_length, false);
dschinazi244f6dc2019-05-06 15:45:16 -07005513 EXPECT_TRUE(framer_.ProcessPacket(encrypted));
5514
5515 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
5516 ASSERT_TRUE(visitor_.header_.get());
5517
5518 ASSERT_TRUE(visitor_.retry_original_connection_id_.get());
5519 ASSERT_TRUE(visitor_.retry_new_connection_id_.get());
5520 ASSERT_TRUE(visitor_.retry_token_.get());
5521
5522 EXPECT_EQ(FramerTestConnectionId(),
5523 *visitor_.retry_original_connection_id_.get());
5524 EXPECT_EQ(FramerTestConnectionIdPlusOne(),
5525 *visitor_.retry_new_connection_id_.get());
5526 EXPECT_EQ("Hello this is RETRY!", *visitor_.retry_token_.get());
5527}
5528
5529TEST_P(QuicFramerTest, RejectIetfRetryPacketAsServer) {
5530 if (!framer_.version().SupportsRetry()) {
5531 return;
5532 }
5533 // IETF RETRY is only sent from client to server.
5534 QuicFramerPeer::SetPerspective(&framer_, Perspective::IS_SERVER);
5535 // clang-format off
5536 unsigned char packet[] = {
5537 // public flags (long header with packet type RETRY and ODCIL=8)
5538 0xF5,
5539 // version
5540 QUIC_VERSION_BYTES,
5541 // connection ID lengths
dschinazi48ac9192019-07-31 00:07:26 -07005542 0x00, 0x08,
dschinazi244f6dc2019-05-06 15:45:16 -07005543 // source connection ID
5544 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x11,
5545 // original destination connection ID
5546 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
5547 // retry token
5548 'H', 'e', 'l', 'l', 'o', ' ', 't', 'h', 'i', 's',
5549 ' ', 'i', 's', ' ', 'R', 'E', 'T', 'R', 'Y', '!',
5550 };
5551 // clang-format on
5552
5553 QuicEncryptedPacket encrypted(AsChars(packet), QUIC_ARRAYSIZE(packet), false);
5554 EXPECT_FALSE(framer_.ProcessPacket(encrypted));
5555
5556 EXPECT_EQ(QUIC_INVALID_PACKET_HEADER, framer_.error());
5557 EXPECT_EQ("Client-initiated RETRY is invalid.", framer_.detailed_error());
5558}
5559
QUICHE teama6ef0a62019-03-07 20:34:33 -05005560TEST_P(QuicFramerTest, BuildPaddingFramePacket) {
QUICHE team2252b702019-05-14 23:55:14 -04005561 QuicFramerPeer::SetPerspective(&framer_, Perspective::IS_CLIENT);
QUICHE teama6ef0a62019-03-07 20:34:33 -05005562 QuicPacketHeader header;
5563 header.destination_connection_id = FramerTestConnectionId();
5564 header.reset_flag = false;
5565 header.version_flag = false;
5566 header.packet_number = kPacketNumber;
5567
5568 QuicFrames frames = {QuicFrame(QuicPaddingFrame())};
5569
5570 // clang-format off
dschinazi66dea072019-04-09 11:41:06 -07005571 unsigned char packet[kMaxOutgoingPacketSize] = {
QUICHE teama6ef0a62019-03-07 20:34:33 -05005572 // public flags (8 byte connection_id)
QUICHE team2252b702019-05-14 23:55:14 -04005573 0x2C,
QUICHE teama6ef0a62019-03-07 20:34:33 -05005574 // connection_id
5575 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
5576 // packet number
5577 0x12, 0x34, 0x56, 0x78,
5578
5579 // frame type (padding frame)
5580 0x00,
5581 0x00, 0x00, 0x00, 0x00
5582 };
5583
dschinazi66dea072019-04-09 11:41:06 -07005584 unsigned char packet46[kMaxOutgoingPacketSize] = {
QUICHE teama6ef0a62019-03-07 20:34:33 -05005585 // type (short header, 4 byte packet number)
5586 0x43,
5587 // connection_id
5588 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
5589 // packet number
5590 0x12, 0x34, 0x56, 0x78,
5591
5592 // frame type (padding frame)
5593 0x00,
5594 0x00, 0x00, 0x00, 0x00
5595 };
5596
dschinazi66dea072019-04-09 11:41:06 -07005597 unsigned char packet99[kMaxOutgoingPacketSize] = {
QUICHE teama6ef0a62019-03-07 20:34:33 -05005598 // type (short header, 4 byte packet number)
5599 0x43,
5600 // connection_id
5601 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
5602 // packet number
5603 0x12, 0x34, 0x56, 0x78,
5604
5605 // frame type (padding frame)
5606 0x00,
5607 0x00, 0x00, 0x00, 0x00
5608 };
5609 // clang-format on
5610
5611 unsigned char* p = packet;
fkastenholz305e1732019-06-18 05:01:22 -07005612 if (VersionHasIetfQuicFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05005613 p = packet99;
fayang36825da2019-08-21 14:01:27 -07005614 } else if (framer_.transport_version() >= QUIC_VERSION_46) {
fayang374888f2019-05-31 06:47:21 -07005615 p = packet46;
QUICHE teama6ef0a62019-03-07 20:34:33 -05005616 }
5617
5618 uint64_t header_size = GetPacketHeaderSize(
5619 framer_.transport_version(), PACKET_8BYTE_CONNECTION_ID,
5620 PACKET_0BYTE_CONNECTION_ID, !kIncludeVersion,
5621 !kIncludeDiversificationNonce, PACKET_4BYTE_PACKET_NUMBER,
5622 VARIABLE_LENGTH_INTEGER_LENGTH_0, 0, VARIABLE_LENGTH_INTEGER_LENGTH_0);
dschinazi66dea072019-04-09 11:41:06 -07005623 memset(p + header_size + 1, 0x00, kMaxOutgoingPacketSize - header_size - 1);
QUICHE teama6ef0a62019-03-07 20:34:33 -05005624
5625 std::unique_ptr<QuicPacket> data(BuildDataPacket(header, frames));
5626 ASSERT_TRUE(data != nullptr);
5627
5628 test::CompareCharArraysWithHexError(
5629 "constructed packet", data->data(), data->length(), AsChars(p),
fayang36825da2019-08-21 14:01:27 -07005630 framer_.transport_version() > QUIC_VERSION_43 ? QUIC_ARRAYSIZE(packet46)
QUICHE teama6ef0a62019-03-07 20:34:33 -05005631 : QUIC_ARRAYSIZE(packet));
5632}
5633
5634TEST_P(QuicFramerTest, BuildStreamFramePacketWithNewPaddingFrame) {
QUICHE team2252b702019-05-14 23:55:14 -04005635 QuicFramerPeer::SetPerspective(&framer_, Perspective::IS_CLIENT);
QUICHE teama6ef0a62019-03-07 20:34:33 -05005636 QuicPacketHeader header;
5637 header.destination_connection_id = FramerTestConnectionId();
5638 header.reset_flag = false;
5639 header.version_flag = false;
5640 header.packet_number = kPacketNumber;
5641 QuicStreamFrame stream_frame(kStreamId, true, kStreamOffset,
5642 QuicStringPiece("hello world!"));
5643 QuicPaddingFrame padding_frame(2);
5644 QuicFrames frames = {QuicFrame(padding_frame), QuicFrame(stream_frame),
5645 QuicFrame(padding_frame)};
5646
5647 // clang-format off
5648 unsigned char packet[] = {
5649 // public flags (8 byte connection_id)
QUICHE team2252b702019-05-14 23:55:14 -04005650 0x2C,
QUICHE teama6ef0a62019-03-07 20:34:33 -05005651 // connection_id
5652 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
5653 // packet number
5654 0x12, 0x34, 0x56, 0x78,
5655
5656 // paddings
5657 0x00, 0x00,
5658 // frame type (stream frame with fin)
5659 0xFF,
5660 // stream id
5661 0x01, 0x02, 0x03, 0x04,
5662 // offset
5663 0x3A, 0x98, 0xFE, 0xDC,
5664 0x32, 0x10, 0x76, 0x54,
5665 // data length
5666 0x00, 0x0c,
5667 // data
5668 'h', 'e', 'l', 'l',
5669 'o', ' ', 'w', 'o',
5670 'r', 'l', 'd', '!',
5671 // paddings
5672 0x00, 0x00,
5673 };
5674
QUICHE teama6ef0a62019-03-07 20:34:33 -05005675 unsigned char packet46[] = {
5676 // type (short header, 4 byte packet number)
5677 0x43,
5678 // connection_id
5679 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
5680 // packet number
5681 0x12, 0x34, 0x56, 0x78,
5682
5683 // paddings
5684 0x00, 0x00,
5685 // frame type (stream frame with fin)
5686 0xFF,
5687 // stream id
5688 0x01, 0x02, 0x03, 0x04,
5689 // offset
5690 0x3A, 0x98, 0xFE, 0xDC,
5691 0x32, 0x10, 0x76, 0x54,
5692 // data length
5693 0x00, 0x0c,
5694 // data
5695 'h', 'e', 'l', 'l',
5696 'o', ' ', 'w', 'o',
5697 'r', 'l', 'd', '!',
5698 // paddings
5699 0x00, 0x00,
5700 };
5701
5702 unsigned char packet99[] = {
5703 // type (short header, 4 byte packet number)
5704 0x43,
5705 // connection_id
5706 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
5707 // packet number
5708 0x12, 0x34, 0x56, 0x78,
5709
5710 // paddings
5711 0x00, 0x00,
5712 // frame type (IETF_STREAM with FIN, LEN, and OFFSET bits set)
5713 0x08 | 0x01 | 0x02 | 0x04,
5714 // stream id
5715 kVarInt62FourBytes + 0x01, 0x02, 0x03, 0x04,
5716 // offset
5717 kVarInt62EightBytes + 0x3A, 0x98, 0xFE, 0xDC,
5718 0x32, 0x10, 0x76, 0x54,
5719 // data length
5720 kVarInt62OneByte + 0x0c,
5721 // data
5722 'h', 'e', 'l', 'l',
5723 'o', ' ', 'w', 'o',
5724 'r', 'l', 'd', '!',
5725 // paddings
5726 0x00, 0x00,
5727 };
5728 // clang-format on
5729
5730 std::unique_ptr<QuicPacket> data(BuildDataPacket(header, frames));
5731 ASSERT_TRUE(data != nullptr);
5732
5733 unsigned char* p = packet;
5734 size_t p_size = QUIC_ARRAYSIZE(packet);
fkastenholz305e1732019-06-18 05:01:22 -07005735 if (VersionHasIetfQuicFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05005736 p = packet99;
5737 p_size = QUIC_ARRAYSIZE(packet99);
fayang36825da2019-08-21 14:01:27 -07005738 } else if (framer_.transport_version() >= QUIC_VERSION_46) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05005739 p = packet46;
5740 p_size = QUIC_ARRAYSIZE(packet46);
QUICHE teama6ef0a62019-03-07 20:34:33 -05005741 }
5742 QuicEncryptedPacket encrypted(AsChars(p), p_size, false);
5743
5744 test::CompareCharArraysWithHexError("constructed packet", data->data(),
5745 data->length(), AsChars(p), p_size);
5746}
5747
5748TEST_P(QuicFramerTest, Build4ByteSequenceNumberPaddingFramePacket) {
QUICHE team2252b702019-05-14 23:55:14 -04005749 QuicFramerPeer::SetPerspective(&framer_, Perspective::IS_CLIENT);
QUICHE teama6ef0a62019-03-07 20:34:33 -05005750 QuicPacketHeader header;
5751 header.destination_connection_id = FramerTestConnectionId();
5752 header.reset_flag = false;
5753 header.version_flag = false;
5754 header.packet_number_length = PACKET_4BYTE_PACKET_NUMBER;
5755 header.packet_number = kPacketNumber;
5756
5757 QuicFrames frames = {QuicFrame(QuicPaddingFrame())};
5758
5759 // clang-format off
dschinazi66dea072019-04-09 11:41:06 -07005760 unsigned char packet[kMaxOutgoingPacketSize] = {
QUICHE teama6ef0a62019-03-07 20:34:33 -05005761 // public flags (8 byte connection_id and 4 byte packet number)
QUICHE team2252b702019-05-14 23:55:14 -04005762 0x2C,
QUICHE teama6ef0a62019-03-07 20:34:33 -05005763 // connection_id
5764 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
5765 // packet number
5766 0x12, 0x34, 0x56, 0x78,
5767
5768 // frame type (padding frame)
5769 0x00,
5770 0x00, 0x00, 0x00, 0x00
5771 };
5772
dschinazi66dea072019-04-09 11:41:06 -07005773 unsigned char packet46[kMaxOutgoingPacketSize] = {
QUICHE teama6ef0a62019-03-07 20:34:33 -05005774 // type (short header, 4 byte packet number)
5775 0x43,
5776 // connection_id
5777 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
5778 // packet number
5779 0x12, 0x34, 0x56, 0x78,
5780
5781 // frame type (padding frame)
5782 0x00,
5783 0x00, 0x00, 0x00, 0x00
5784 };
5785
dschinazi66dea072019-04-09 11:41:06 -07005786 unsigned char packet99[kMaxOutgoingPacketSize] = {
QUICHE teama6ef0a62019-03-07 20:34:33 -05005787 // type (short header, 4 byte packet number)
5788 0x43,
5789 // connection_id
5790 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
5791 // packet number
5792 0x12, 0x34, 0x56, 0x78,
5793
5794 // frame type (padding frame)
5795 0x00,
5796 0x00, 0x00, 0x00, 0x00
5797 };
5798 // clang-format on
5799
5800 unsigned char* p = packet;
fkastenholz305e1732019-06-18 05:01:22 -07005801 if (VersionHasIetfQuicFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05005802 p = packet99;
fayang36825da2019-08-21 14:01:27 -07005803 } else if (framer_.transport_version() >= QUIC_VERSION_46) {
fayang374888f2019-05-31 06:47:21 -07005804 p = packet46;
QUICHE teama6ef0a62019-03-07 20:34:33 -05005805 }
5806
5807 uint64_t header_size = GetPacketHeaderSize(
5808 framer_.transport_version(), PACKET_8BYTE_CONNECTION_ID,
5809 PACKET_0BYTE_CONNECTION_ID, !kIncludeVersion,
5810 !kIncludeDiversificationNonce, PACKET_4BYTE_PACKET_NUMBER,
5811 VARIABLE_LENGTH_INTEGER_LENGTH_0, 0, VARIABLE_LENGTH_INTEGER_LENGTH_0);
dschinazi66dea072019-04-09 11:41:06 -07005812 memset(p + header_size + 1, 0x00, kMaxOutgoingPacketSize - header_size - 1);
QUICHE teama6ef0a62019-03-07 20:34:33 -05005813
5814 std::unique_ptr<QuicPacket> data(BuildDataPacket(header, frames));
5815 ASSERT_TRUE(data != nullptr);
5816
5817 test::CompareCharArraysWithHexError(
5818 "constructed packet", data->data(), data->length(), AsChars(p),
fayang36825da2019-08-21 14:01:27 -07005819 framer_.transport_version() > QUIC_VERSION_43 ? QUIC_ARRAYSIZE(packet46)
QUICHE teama6ef0a62019-03-07 20:34:33 -05005820 : QUIC_ARRAYSIZE(packet));
5821}
5822
5823TEST_P(QuicFramerTest, Build2ByteSequenceNumberPaddingFramePacket) {
QUICHE team2252b702019-05-14 23:55:14 -04005824 QuicFramerPeer::SetPerspective(&framer_, Perspective::IS_CLIENT);
QUICHE teama6ef0a62019-03-07 20:34:33 -05005825 QuicPacketHeader header;
5826 header.destination_connection_id = FramerTestConnectionId();
5827 header.reset_flag = false;
5828 header.version_flag = false;
5829 header.packet_number_length = PACKET_2BYTE_PACKET_NUMBER;
5830 header.packet_number = kPacketNumber;
5831
5832 QuicFrames frames = {QuicFrame(QuicPaddingFrame())};
5833
5834 // clang-format off
dschinazi66dea072019-04-09 11:41:06 -07005835 unsigned char packet[kMaxOutgoingPacketSize] = {
QUICHE teama6ef0a62019-03-07 20:34:33 -05005836 // public flags (8 byte connection_id and 2 byte packet number)
QUICHE team2252b702019-05-14 23:55:14 -04005837 0x1C,
QUICHE teama6ef0a62019-03-07 20:34:33 -05005838 // connection_id
5839 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
5840 // packet number
5841 0x56, 0x78,
5842
5843 // frame type (padding frame)
5844 0x00,
5845 0x00, 0x00, 0x00, 0x00
5846 };
5847
dschinazi66dea072019-04-09 11:41:06 -07005848 unsigned char packet46[kMaxOutgoingPacketSize] = {
QUICHE teama6ef0a62019-03-07 20:34:33 -05005849 // type (short header, 2 byte packet number)
5850 0x41,
5851 // connection_id
5852 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
5853 // packet number
5854 0x56, 0x78,
5855
5856 // frame type (padding frame)
5857 0x00,
5858 0x00, 0x00, 0x00, 0x00
5859 };
5860
dschinazi66dea072019-04-09 11:41:06 -07005861 unsigned char packet99[kMaxOutgoingPacketSize] = {
QUICHE teama6ef0a62019-03-07 20:34:33 -05005862 // type (short header, 2 byte packet number)
5863 0x41,
5864 // connection_id
5865 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
5866 // packet number
5867 0x56, 0x78,
5868
5869 // frame type (padding frame)
5870 0x00,
5871 0x00, 0x00, 0x00, 0x00
5872 };
5873 // clang-format on
5874
5875 unsigned char* p = packet;
fkastenholz305e1732019-06-18 05:01:22 -07005876 if (VersionHasIetfQuicFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05005877 p = packet99;
fayang36825da2019-08-21 14:01:27 -07005878 } else if (framer_.transport_version() >= QUIC_VERSION_46) {
fayang374888f2019-05-31 06:47:21 -07005879 p = packet46;
QUICHE teama6ef0a62019-03-07 20:34:33 -05005880 }
5881
5882 uint64_t header_size = GetPacketHeaderSize(
5883 framer_.transport_version(), PACKET_8BYTE_CONNECTION_ID,
5884 PACKET_0BYTE_CONNECTION_ID, !kIncludeVersion,
5885 !kIncludeDiversificationNonce, PACKET_2BYTE_PACKET_NUMBER,
5886 VARIABLE_LENGTH_INTEGER_LENGTH_0, 0, VARIABLE_LENGTH_INTEGER_LENGTH_0);
dschinazi66dea072019-04-09 11:41:06 -07005887 memset(p + header_size + 1, 0x00, kMaxOutgoingPacketSize - header_size - 1);
QUICHE teama6ef0a62019-03-07 20:34:33 -05005888
5889 std::unique_ptr<QuicPacket> data(BuildDataPacket(header, frames));
5890 ASSERT_TRUE(data != nullptr);
5891
5892 test::CompareCharArraysWithHexError(
5893 "constructed packet", data->data(), data->length(), AsChars(p),
fayang36825da2019-08-21 14:01:27 -07005894 framer_.transport_version() > QUIC_VERSION_43 ? QUIC_ARRAYSIZE(packet46)
QUICHE teama6ef0a62019-03-07 20:34:33 -05005895 : QUIC_ARRAYSIZE(packet));
5896}
5897
5898TEST_P(QuicFramerTest, Build1ByteSequenceNumberPaddingFramePacket) {
QUICHE team2252b702019-05-14 23:55:14 -04005899 QuicFramerPeer::SetPerspective(&framer_, Perspective::IS_CLIENT);
QUICHE teama6ef0a62019-03-07 20:34:33 -05005900 QuicPacketHeader header;
5901 header.destination_connection_id = FramerTestConnectionId();
5902 header.reset_flag = false;
5903 header.version_flag = false;
5904 header.packet_number_length = PACKET_1BYTE_PACKET_NUMBER;
5905 header.packet_number = kPacketNumber;
5906
5907 QuicFrames frames = {QuicFrame(QuicPaddingFrame())};
5908
5909 // clang-format off
dschinazi66dea072019-04-09 11:41:06 -07005910 unsigned char packet[kMaxOutgoingPacketSize] = {
QUICHE teama6ef0a62019-03-07 20:34:33 -05005911 // public flags (8 byte connection_id and 1 byte packet number)
QUICHE team2252b702019-05-14 23:55:14 -04005912 0x0C,
QUICHE teama6ef0a62019-03-07 20:34:33 -05005913 // connection_id
5914 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
5915 // packet number
5916 0x78,
5917
5918 // frame type (padding frame)
5919 0x00,
5920 0x00, 0x00, 0x00, 0x00
5921 };
5922
dschinazi66dea072019-04-09 11:41:06 -07005923 unsigned char packet46[kMaxOutgoingPacketSize] = {
QUICHE teama6ef0a62019-03-07 20:34:33 -05005924 // type (short header, 1 byte packet number)
5925 0x40,
5926 // connection_id
5927 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
5928 // packet number
5929 0x78,
5930
5931 // frame type (padding frame)
5932 0x00,
5933 0x00, 0x00, 0x00, 0x00
5934 };
5935
dschinazi66dea072019-04-09 11:41:06 -07005936 unsigned char packet99[kMaxOutgoingPacketSize] = {
QUICHE teama6ef0a62019-03-07 20:34:33 -05005937 // type (short header, 1 byte packet number)
5938 0x40,
5939 // connection_id
5940 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
5941 // packet number
5942 0x78,
5943
5944 // frame type (padding frame)
5945 0x00,
5946 0x00, 0x00, 0x00, 0x00
5947 };
5948 // clang-format on
5949
5950 unsigned char* p = packet;
fkastenholz305e1732019-06-18 05:01:22 -07005951 if (VersionHasIetfQuicFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05005952 p = packet99;
fayang36825da2019-08-21 14:01:27 -07005953 } else if (framer_.transport_version() >= QUIC_VERSION_46) {
fayang374888f2019-05-31 06:47:21 -07005954 p = packet46;
QUICHE teama6ef0a62019-03-07 20:34:33 -05005955 }
5956
5957 uint64_t header_size = GetPacketHeaderSize(
5958 framer_.transport_version(), PACKET_8BYTE_CONNECTION_ID,
5959 PACKET_0BYTE_CONNECTION_ID, !kIncludeVersion,
5960 !kIncludeDiversificationNonce, PACKET_1BYTE_PACKET_NUMBER,
5961 VARIABLE_LENGTH_INTEGER_LENGTH_0, 0, VARIABLE_LENGTH_INTEGER_LENGTH_0);
dschinazi66dea072019-04-09 11:41:06 -07005962 memset(p + header_size + 1, 0x00, kMaxOutgoingPacketSize - header_size - 1);
QUICHE teama6ef0a62019-03-07 20:34:33 -05005963
5964 std::unique_ptr<QuicPacket> data(BuildDataPacket(header, frames));
5965 ASSERT_TRUE(data != nullptr);
5966
5967 test::CompareCharArraysWithHexError(
5968 "constructed packet", data->data(), data->length(), AsChars(p),
fayang36825da2019-08-21 14:01:27 -07005969 framer_.transport_version() > QUIC_VERSION_43 ? QUIC_ARRAYSIZE(packet46)
QUICHE teama6ef0a62019-03-07 20:34:33 -05005970 : QUIC_ARRAYSIZE(packet));
5971}
5972
5973TEST_P(QuicFramerTest, BuildStreamFramePacket) {
QUICHE team2252b702019-05-14 23:55:14 -04005974 QuicFramerPeer::SetPerspective(&framer_, Perspective::IS_CLIENT);
QUICHE teama6ef0a62019-03-07 20:34:33 -05005975 QuicPacketHeader header;
5976 header.destination_connection_id = FramerTestConnectionId();
5977 header.reset_flag = false;
5978 header.version_flag = false;
5979 header.packet_number = kPacketNumber;
5980 if (QuicVersionHasLongHeaderLengths(framer_.transport_version())) {
5981 header.length_length = VARIABLE_LENGTH_INTEGER_LENGTH_2;
5982 }
5983
5984 QuicStreamFrame stream_frame(kStreamId, true, kStreamOffset,
5985 QuicStringPiece("hello world!"));
5986
5987 QuicFrames frames = {QuicFrame(stream_frame)};
5988
5989 // clang-format off
5990 unsigned char packet[] = {
5991 // public flags (8 byte connection_id)
QUICHE team2252b702019-05-14 23:55:14 -04005992 0x2C,
QUICHE teama6ef0a62019-03-07 20:34:33 -05005993 // connection_id
5994 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
5995 // packet number
5996 0x12, 0x34, 0x56, 0x78,
5997
5998 // frame type (stream frame with fin and no length)
5999 0xDF,
6000 // stream id
6001 0x01, 0x02, 0x03, 0x04,
6002 // offset
6003 0x3A, 0x98, 0xFE, 0xDC,
6004 0x32, 0x10, 0x76, 0x54,
6005 // data
6006 'h', 'e', 'l', 'l',
6007 'o', ' ', 'w', 'o',
6008 'r', 'l', 'd', '!',
6009 };
6010
QUICHE teama6ef0a62019-03-07 20:34:33 -05006011 unsigned char packet46[] = {
6012 // type (short header, 4 byte packet number)
6013 0x43,
6014 // connection_id
6015 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
6016 // packet number
6017 0x12, 0x34, 0x56, 0x78,
6018
6019 // frame type (stream frame with fin and no length)
6020 0xDF,
6021 // stream id
6022 0x01, 0x02, 0x03, 0x04,
6023 // offset
6024 0x3A, 0x98, 0xFE, 0xDC,
6025 0x32, 0x10, 0x76, 0x54,
6026 // data
6027 'h', 'e', 'l', 'l',
6028 'o', ' ', 'w', 'o',
6029 'r', 'l', 'd', '!',
6030 };
6031
6032 unsigned char packet99[] = {
6033 // type (short header, 4 byte packet number)
6034 0x43,
6035 // connection_id
6036 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
6037 // packet number
6038 0x12, 0x34, 0x56, 0x78,
6039
6040 // frame type (IETF_STREAM frame with FIN and OFFSET, no length)
6041 0x08 | 0x01 | 0x04,
6042 // stream id
6043 kVarInt62FourBytes + 0x01, 0x02, 0x03, 0x04,
6044 // offset
6045 kVarInt62EightBytes + 0x3A, 0x98, 0xFE, 0xDC,
6046 0x32, 0x10, 0x76, 0x54,
6047 // data
6048 'h', 'e', 'l', 'l',
6049 'o', ' ', 'w', 'o',
6050 'r', 'l', 'd', '!',
6051 };
6052 // clang-format on
6053
6054 std::unique_ptr<QuicPacket> data(BuildDataPacket(header, frames));
6055 ASSERT_TRUE(data != nullptr);
6056
6057 unsigned char* p = packet;
6058 size_t p_size = QUIC_ARRAYSIZE(packet);
fkastenholz305e1732019-06-18 05:01:22 -07006059 if (VersionHasIetfQuicFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05006060 p = packet99;
6061 p_size = QUIC_ARRAYSIZE(packet99);
fayang36825da2019-08-21 14:01:27 -07006062 } else if (framer_.transport_version() >= QUIC_VERSION_46) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05006063 p = packet46;
6064 p_size = QUIC_ARRAYSIZE(packet46);
QUICHE teama6ef0a62019-03-07 20:34:33 -05006065 }
6066 test::CompareCharArraysWithHexError("constructed packet", data->data(),
6067 data->length(), AsChars(p), p_size);
6068}
6069
6070TEST_P(QuicFramerTest, BuildStreamFramePacketWithVersionFlag) {
6071 QuicPacketHeader header;
6072 header.destination_connection_id = FramerTestConnectionId();
6073 header.reset_flag = false;
6074 header.version_flag = true;
6075 if (framer_.transport_version() > QUIC_VERSION_43) {
6076 header.long_packet_type = ZERO_RTT_PROTECTED;
6077 }
6078 header.packet_number = kPacketNumber;
6079 if (QuicVersionHasLongHeaderLengths(framer_.transport_version())) {
6080 header.length_length = VARIABLE_LENGTH_INTEGER_LENGTH_2;
6081 }
6082
6083 QuicStreamFrame stream_frame(kStreamId, true, kStreamOffset,
6084 QuicStringPiece("hello world!"));
6085 QuicFrames frames = {QuicFrame(stream_frame)};
6086
6087 // clang-format off
6088 unsigned char packet[] = {
6089 // public flags (version, 8 byte connection_id)
6090 0x2D,
6091 // connection_id
6092 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
6093 // version tag
6094 QUIC_VERSION_BYTES,
6095 // packet number
6096 0x12, 0x34, 0x56, 0x78,
6097
6098 // frame type (stream frame with fin and no length)
6099 0xDF,
6100 // stream id
6101 0x01, 0x02, 0x03, 0x04,
6102 // offset
6103 0x3A, 0x98, 0xFE, 0xDC, 0x32, 0x10, 0x76, 0x54,
6104 // data
6105 'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', '!',
6106 };
6107
QUICHE teama6ef0a62019-03-07 20:34:33 -05006108 unsigned char packet46[] = {
6109 // type (long header with packet type ZERO_RTT_PROTECTED)
6110 0xD3,
6111 // version tag
6112 QUIC_VERSION_BYTES,
6113 // connection_id length
6114 0x50,
6115 // connection_id
6116 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
6117 // packet number
6118 0x12, 0x34, 0x56, 0x78,
6119
6120 // frame type (stream frame with fin and no length)
6121 0xDF,
6122 // stream id
6123 0x01, 0x02, 0x03, 0x04,
6124 // offset
6125 0x3A, 0x98, 0xFE, 0xDC, 0x32, 0x10, 0x76, 0x54,
6126 // data
6127 'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', '!',
6128 };
6129
6130 unsigned char packet99[] = {
6131 // type (long header with packet type ZERO_RTT_PROTECTED)
6132 0xD3,
6133 // version tag
6134 QUIC_VERSION_BYTES,
dschinazi48ac9192019-07-31 00:07:26 -07006135 // destination connection ID length
6136 0x08,
6137 // destination connection ID
QUICHE teama6ef0a62019-03-07 20:34:33 -05006138 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
dschinazi48ac9192019-07-31 00:07:26 -07006139 // source connection ID length
6140 0x00,
QUICHE teama6ef0a62019-03-07 20:34:33 -05006141 // length
6142 0x40, 0x1D,
6143 // packet number
6144 0x12, 0x34, 0x56, 0x78,
6145
6146 // frame type (IETF_STREAM frame with fin and offset, no length)
6147 0x08 | 0x01 | 0x04,
6148 // stream id
6149 kVarInt62FourBytes + 0x01, 0x02, 0x03, 0x04,
6150 // offset
6151 kVarInt62EightBytes + 0x3A, 0x98, 0xFE, 0xDC, 0x32, 0x10, 0x76, 0x54,
6152 // data
6153 'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', '!',
6154 };
6155 // clang-format on
6156
6157 QuicFramerPeer::SetPerspective(&framer_, Perspective::IS_CLIENT);
6158 std::unique_ptr<QuicPacket> data(BuildDataPacket(header, frames));
6159 ASSERT_TRUE(data != nullptr);
6160
6161 unsigned char* p = packet;
6162 size_t p_size = QUIC_ARRAYSIZE(packet);
fkastenholz305e1732019-06-18 05:01:22 -07006163 if (VersionHasIetfQuicFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05006164 p = packet99;
6165 p_size = QUIC_ARRAYSIZE(packet99);
fayang36825da2019-08-21 14:01:27 -07006166 } else if (framer_.transport_version() >= QUIC_VERSION_46) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05006167 p = packet46;
6168 p_size = QUIC_ARRAYSIZE(packet46);
QUICHE teama6ef0a62019-03-07 20:34:33 -05006169 }
6170 test::CompareCharArraysWithHexError("constructed packet", data->data(),
6171 data->length(), AsChars(p), p_size);
6172}
6173
6174TEST_P(QuicFramerTest, BuildCryptoFramePacket) {
fkastenholzceae8372019-06-12 12:22:22 -07006175 if (!QuicVersionUsesCryptoFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05006176 return;
6177 }
QUICHE team2252b702019-05-14 23:55:14 -04006178 QuicFramerPeer::SetPerspective(&framer_, Perspective::IS_CLIENT);
QUICHE teama6ef0a62019-03-07 20:34:33 -05006179 QuicPacketHeader header;
6180 header.destination_connection_id = FramerTestConnectionId();
6181 header.reset_flag = false;
6182 header.version_flag = false;
6183 header.packet_number = kPacketNumber;
6184
6185 SimpleDataProducer data_producer;
6186 framer_.set_data_producer(&data_producer);
6187
6188 QuicStringPiece crypto_frame_contents("hello world!");
QUICHE team6987b4a2019-03-15 16:23:04 -07006189 QuicCryptoFrame crypto_frame(ENCRYPTION_INITIAL, kStreamOffset,
QUICHE teama6ef0a62019-03-07 20:34:33 -05006190 crypto_frame_contents.length());
QUICHE team6987b4a2019-03-15 16:23:04 -07006191 data_producer.SaveCryptoData(ENCRYPTION_INITIAL, kStreamOffset,
QUICHE teama6ef0a62019-03-07 20:34:33 -05006192 crypto_frame_contents);
6193
6194 QuicFrames frames = {QuicFrame(&crypto_frame)};
6195
6196 // clang-format off
nharper107ba5f2019-07-02 21:33:39 -07006197 unsigned char packet48[] = {
6198 // type (short header, 4 byte packet number)
6199 0x43,
6200 // connection_id
6201 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
6202 // packet number
6203 0x12, 0x34, 0x56, 0x78,
6204
6205 // frame type (QuicFrameType CRYPTO_FRAME)
6206 0x08,
6207 // offset
6208 kVarInt62EightBytes + 0x3A, 0x98, 0xFE, 0xDC,
6209 0x32, 0x10, 0x76, 0x54,
6210 // length
6211 kVarInt62OneByte + 12,
6212 // data
6213 'h', 'e', 'l', 'l',
6214 'o', ' ', 'w', 'o',
6215 'r', 'l', 'd', '!',
6216 };
6217
6218 unsigned char packet99[] = {
QUICHE teama6ef0a62019-03-07 20:34:33 -05006219 // type (short header, 4 byte packet number)
6220 0x43,
6221 // connection_id
6222 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
6223 // packet number
6224 0x12, 0x34, 0x56, 0x78,
6225
6226 // frame type (IETF_CRYPTO frame)
6227 0x06,
6228 // offset
6229 kVarInt62EightBytes + 0x3A, 0x98, 0xFE, 0xDC,
6230 0x32, 0x10, 0x76, 0x54,
6231 // length
6232 kVarInt62OneByte + 12,
6233 // data
6234 'h', 'e', 'l', 'l',
6235 'o', ' ', 'w', 'o',
6236 'r', 'l', 'd', '!',
6237 };
6238 // clang-format on
6239
nharper107ba5f2019-07-02 21:33:39 -07006240 unsigned char* packet = packet48;
6241 size_t packet_size = QUIC_ARRAYSIZE(packet48);
6242 if (framer_.transport_version() == QUIC_VERSION_99) {
6243 packet = packet99;
6244 packet_size = QUIC_ARRAYSIZE(packet99);
6245 }
QUICHE teama6ef0a62019-03-07 20:34:33 -05006246
6247 std::unique_ptr<QuicPacket> data(BuildDataPacket(header, frames));
6248 ASSERT_TRUE(data != nullptr);
6249 test::CompareCharArraysWithHexError("constructed packet", data->data(),
6250 data->length(), AsChars(packet),
6251 packet_size);
6252}
6253
6254TEST_P(QuicFramerTest, CryptoFrame) {
nharper107ba5f2019-07-02 21:33:39 -07006255 if (framer_.transport_version() < QUIC_VERSION_48) {
6256 // CRYPTO frames aren't supported prior to v48.
QUICHE teama6ef0a62019-03-07 20:34:33 -05006257 return;
6258 }
zhongyi546cc452019-04-12 15:27:49 -07006259 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
QUICHE teama6ef0a62019-03-07 20:34:33 -05006260
6261 // clang-format off
nharper107ba5f2019-07-02 21:33:39 -07006262 PacketFragments packet48 = {
6263 // type (short header, 4 byte packet number)
6264 {"",
6265 {0x43}},
6266 // connection_id
6267 {"",
6268 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
6269 // packet number
6270 {"",
6271 {0x12, 0x34, 0x56, 0x78}},
6272 // frame type (QuicFrameType CRYPTO_FRAME)
6273 {"",
6274 {0x08}},
6275 // offset
6276 {"",
6277 {kVarInt62EightBytes + 0x3A, 0x98, 0xFE, 0xDC,
6278 0x32, 0x10, 0x76, 0x54}},
6279 // data length
6280 {"Invalid data length.",
6281 {kVarInt62OneByte + 12}},
6282 // data
6283 {"Unable to read frame data.",
6284 {'h', 'e', 'l', 'l',
6285 'o', ' ', 'w', 'o',
6286 'r', 'l', 'd', '!'}},
6287 };
6288
6289 PacketFragments packet99 = {
QUICHE teama6ef0a62019-03-07 20:34:33 -05006290 // type (short header, 4 byte packet number)
6291 {"",
6292 {0x43}},
6293 // connection_id
6294 {"",
6295 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
6296 // packet number
6297 {"",
6298 {0x12, 0x34, 0x56, 0x78}},
6299 // frame type (IETF_CRYPTO frame)
6300 {"",
6301 {0x06}},
6302 // offset
6303 {"",
6304 {kVarInt62EightBytes + 0x3A, 0x98, 0xFE, 0xDC,
6305 0x32, 0x10, 0x76, 0x54}},
6306 // data length
6307 {"Invalid data length.",
6308 {kVarInt62OneByte + 12}},
6309 // data
6310 {"Unable to read frame data.",
6311 {'h', 'e', 'l', 'l',
6312 'o', ' ', 'w', 'o',
6313 'r', 'l', 'd', '!'}},
6314 };
6315 // clang-format on
6316
nharper107ba5f2019-07-02 21:33:39 -07006317 PacketFragments& fragments =
6318 framer_.transport_version() == QUIC_VERSION_99 ? packet99 : packet48;
QUICHE teama6ef0a62019-03-07 20:34:33 -05006319 std::unique_ptr<QuicEncryptedPacket> encrypted(
nharper107ba5f2019-07-02 21:33:39 -07006320 AssemblePacketFromFragments(fragments));
QUICHE teama6ef0a62019-03-07 20:34:33 -05006321 EXPECT_TRUE(framer_.ProcessPacket(*encrypted));
6322
6323 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
6324 ASSERT_TRUE(visitor_.header_.get());
6325 EXPECT_TRUE(CheckDecryption(
6326 *encrypted, !kIncludeVersion, !kIncludeDiversificationNonce,
6327 PACKET_8BYTE_CONNECTION_ID, PACKET_0BYTE_CONNECTION_ID));
6328 ASSERT_EQ(1u, visitor_.crypto_frames_.size());
6329 QuicCryptoFrame* frame = visitor_.crypto_frames_[0].get();
6330 EXPECT_EQ(kStreamOffset, frame->offset);
vasilvvc48c8712019-03-11 13:38:16 -07006331 EXPECT_EQ("hello world!",
6332 std::string(frame->data_buffer, frame->data_length));
QUICHE teama6ef0a62019-03-07 20:34:33 -05006333
nharper107ba5f2019-07-02 21:33:39 -07006334 CheckFramingBoundaries(fragments, QUIC_INVALID_FRAME_DATA);
QUICHE teama6ef0a62019-03-07 20:34:33 -05006335}
6336
6337TEST_P(QuicFramerTest, BuildVersionNegotiationPacket) {
dschinazi1ac22cc2019-06-25 11:47:50 -07006338 SetQuicReloadableFlag(quic_version_negotiation_grease, true);
6339 SetQuicFlag(FLAGS_quic_disable_version_negotiation_grease_randomness, true);
QUICHE teama6ef0a62019-03-07 20:34:33 -05006340 // clang-format off
6341 unsigned char packet[] = {
6342 // public flags (version, 8 byte connection_id)
6343 0x0D,
6344 // connection_id
6345 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
dschinazi1ac22cc2019-06-25 11:47:50 -07006346 // supported versions
6347 0xDA, 0x5A, 0x3A, 0x3A,
QUICHE teama6ef0a62019-03-07 20:34:33 -05006348 QUIC_VERSION_BYTES,
6349 };
fayang36825da2019-08-21 14:01:27 -07006350 unsigned char packet46[] = {
QUICHE teama6ef0a62019-03-07 20:34:33 -05006351 // type (long header)
dschinazi0366de92019-06-18 20:00:27 -07006352 0xC0,
QUICHE teama6ef0a62019-03-07 20:34:33 -05006353 // version tag
6354 0x00, 0x00, 0x00, 0x00,
6355 // connection_id length
6356 0x05,
6357 // connection_id
6358 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
dschinazi1ac22cc2019-06-25 11:47:50 -07006359 // supported versions
6360 0xDA, 0x5A, 0x3A, 0x3A,
QUICHE teama6ef0a62019-03-07 20:34:33 -05006361 QUIC_VERSION_BYTES,
6362 };
dschinazi48ac9192019-07-31 00:07:26 -07006363 unsigned char packet99[] = {
6364 // type (long header)
6365 0xC0,
6366 // version tag
6367 0x00, 0x00, 0x00, 0x00,
6368 // destination connection ID length
6369 0x00,
6370 // source connection ID length
6371 0x08,
6372 // source connection ID
6373 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
6374 // supported versions
6375 0xDA, 0x5A, 0x3A, 0x3A,
6376 QUIC_VERSION_BYTES,
6377 };
QUICHE teama6ef0a62019-03-07 20:34:33 -05006378 // clang-format on
6379 unsigned char* p = packet;
6380 size_t p_size = QUIC_ARRAYSIZE(packet);
dschinazi48ac9192019-07-31 00:07:26 -07006381 if (framer_.transport_version() >= QUIC_VERSION_99) {
6382 p = packet99;
6383 p_size = QUIC_ARRAYSIZE(packet99);
6384 } else if (framer_.transport_version() > QUIC_VERSION_43) {
fayang36825da2019-08-21 14:01:27 -07006385 p = packet46;
6386 p_size = QUIC_ARRAYSIZE(packet46);
QUICHE teama6ef0a62019-03-07 20:34:33 -05006387 }
6388
6389 QuicConnectionId connection_id = FramerTestConnectionId();
6390 std::unique_ptr<QuicEncryptedPacket> data(
dschinazib417d602019-05-29 13:08:45 -07006391 QuicFramer::BuildVersionNegotiationPacket(
6392 connection_id, EmptyQuicConnectionId(),
6393 framer_.transport_version() > QUIC_VERSION_43,
dschinazi48ac9192019-07-31 00:07:26 -07006394 framer_.version().HasLengthPrefixedConnectionIds(),
QUICHE teama6ef0a62019-03-07 20:34:33 -05006395 SupportedVersions(GetParam())));
6396 test::CompareCharArraysWithHexError("constructed packet", data->data(),
6397 data->length(), AsChars(p), p_size);
6398}
6399
dschinazib417d602019-05-29 13:08:45 -07006400TEST_P(QuicFramerTest, BuildVersionNegotiationPacketWithClientConnectionId) {
dschinazi48ac9192019-07-31 00:07:26 -07006401 if (!framer_.version().SupportsClientConnectionIds()) {
dschinazib417d602019-05-29 13:08:45 -07006402 return;
6403 }
6404
dschinazi1ac22cc2019-06-25 11:47:50 -07006405 SetQuicReloadableFlag(quic_version_negotiation_grease, true);
6406 SetQuicFlag(FLAGS_quic_disable_version_negotiation_grease_randomness, true);
dschinazib417d602019-05-29 13:08:45 -07006407
dschinazib417d602019-05-29 13:08:45 -07006408 // clang-format off
6409 unsigned char packet[] = {
6410 // type (long header)
dschinazi0366de92019-06-18 20:00:27 -07006411 0xC0,
dschinazib417d602019-05-29 13:08:45 -07006412 // version tag
6413 0x00, 0x00, 0x00, 0x00,
dschinazib417d602019-05-29 13:08:45 -07006414 // client/destination connection ID
dschinazi48ac9192019-07-31 00:07:26 -07006415 0x08,
dschinazib417d602019-05-29 13:08:45 -07006416 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x11,
6417 // server/source connection ID
dschinazi48ac9192019-07-31 00:07:26 -07006418 0x08,
dschinazib417d602019-05-29 13:08:45 -07006419 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
dschinazi1ac22cc2019-06-25 11:47:50 -07006420 // supported versions
6421 0xDA, 0x5A, 0x3A, 0x3A,
dschinazib417d602019-05-29 13:08:45 -07006422 QUIC_VERSION_BYTES,
6423 };
6424 // clang-format on
6425
6426 QuicConnectionId server_connection_id = FramerTestConnectionId();
6427 QuicConnectionId client_connection_id = FramerTestConnectionIdPlusOne();
6428 std::unique_ptr<QuicEncryptedPacket> data(
dschinazi48ac9192019-07-31 00:07:26 -07006429 QuicFramer::BuildVersionNegotiationPacket(
6430 server_connection_id, client_connection_id, true, true,
6431 SupportedVersions(GetParam())));
dschinazib417d602019-05-29 13:08:45 -07006432 test::CompareCharArraysWithHexError("constructed packet", data->data(),
6433 data->length(), AsChars(packet),
6434 QUIC_ARRAYSIZE(packet));
6435}
6436
QUICHE teama6ef0a62019-03-07 20:34:33 -05006437TEST_P(QuicFramerTest, BuildAckFramePacketOneAckBlock) {
QUICHE team2252b702019-05-14 23:55:14 -04006438 QuicFramerPeer::SetPerspective(&framer_, Perspective::IS_CLIENT);
QUICHE teama6ef0a62019-03-07 20:34:33 -05006439 QuicPacketHeader header;
6440 header.destination_connection_id = FramerTestConnectionId();
6441 header.reset_flag = false;
6442 header.version_flag = false;
6443 header.packet_number = kPacketNumber;
6444
6445 // Use kSmallLargestObserved to make this test finished in a short time.
6446 QuicAckFrame ack_frame = InitAckFrame(kSmallLargestObserved);
6447 ack_frame.ack_delay_time = QuicTime::Delta::Zero();
6448
6449 QuicFrames frames = {QuicFrame(&ack_frame)};
6450
6451 // clang-format off
6452 unsigned char packet[] = {
6453 // public flags (8 byte connection_id)
QUICHE team2252b702019-05-14 23:55:14 -04006454 0x2C,
QUICHE teama6ef0a62019-03-07 20:34:33 -05006455 // connection_id
6456 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
6457 // packet number
6458 0x12, 0x34, 0x56, 0x78,
6459
6460 // frame type (ack frame)
6461 // (no ack blocks, 2 byte largest observed, 2 byte block length)
6462 0x45,
6463 // largest acked
6464 0x12, 0x34,
6465 // Zero delta time.
6466 0x00, 0x00,
6467 // first ack block length.
6468 0x12, 0x34,
6469 // num timestamps.
6470 0x00,
6471 };
6472
QUICHE teama6ef0a62019-03-07 20:34:33 -05006473 unsigned char packet46[] = {
6474 // type (short header, 4 byte packet number)
6475 0x43,
6476 // connection_id
6477 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
6478 // packet number
6479 0x12, 0x34, 0x56, 0x78,
6480
6481 // frame type (ack frame)
6482 // (no ack blocks, 2 byte largest observed, 2 byte block length)
6483 0x45,
6484 // largest acked
6485 0x12, 0x34,
6486 // Zero delta time.
6487 0x00, 0x00,
6488 // first ack block length.
6489 0x12, 0x34,
6490 // num timestamps.
6491 0x00,
6492 };
6493
6494 unsigned char packet99[] = {
6495 // type (short header, 4 byte packet number)
6496 0x43,
6497 // connection_id
6498 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
6499 // packet number
6500 0x12, 0x34, 0x56, 0x78,
6501
6502 // frame type (IETF_ACK frame)
6503 0x02,
6504 // largest acked
6505 kVarInt62TwoBytes + 0x12, 0x34,
6506 // Zero delta time.
6507 kVarInt62OneByte + 0x00,
6508 // Number of additional ack blocks.
6509 kVarInt62OneByte + 0x00,
6510 // first ack block length.
6511 kVarInt62TwoBytes + 0x12, 0x33,
6512 };
6513 // clang-format on
6514 unsigned char* p = packet;
6515 size_t p_size = QUIC_ARRAYSIZE(packet);
fkastenholz305e1732019-06-18 05:01:22 -07006516 if (VersionHasIetfQuicFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05006517 p = packet99;
6518 p_size = QUIC_ARRAYSIZE(packet99);
fayang36825da2019-08-21 14:01:27 -07006519 } else if (framer_.transport_version() >= QUIC_VERSION_46) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05006520 p = packet46;
6521 p_size = QUIC_ARRAYSIZE(packet46);
QUICHE teama6ef0a62019-03-07 20:34:33 -05006522 }
6523
6524 std::unique_ptr<QuicPacket> data(BuildDataPacket(header, frames));
6525 ASSERT_TRUE(data != nullptr);
6526 test::CompareCharArraysWithHexError("constructed packet", data->data(),
6527 data->length(), AsChars(p), p_size);
6528}
6529
6530TEST_P(QuicFramerTest, BuildAckFramePacketOneAckBlockMaxLength) {
QUICHE team2252b702019-05-14 23:55:14 -04006531 QuicFramerPeer::SetPerspective(&framer_, Perspective::IS_CLIENT);
QUICHE teama6ef0a62019-03-07 20:34:33 -05006532 QuicPacketHeader header;
6533 header.destination_connection_id = FramerTestConnectionId();
6534 header.reset_flag = false;
6535 header.version_flag = false;
6536 header.packet_number = kPacketNumber;
6537
6538 QuicAckFrame ack_frame = InitAckFrame(kPacketNumber);
6539 ack_frame.ack_delay_time = QuicTime::Delta::Zero();
6540
6541 QuicFrames frames = {QuicFrame(&ack_frame)};
6542
6543 // clang-format off
6544 unsigned char packet[] = {
6545 // public flags (8 byte connection_id)
QUICHE team2252b702019-05-14 23:55:14 -04006546 0x2C,
QUICHE teama6ef0a62019-03-07 20:34:33 -05006547 // connection_id
6548 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
6549 // packet number
6550 0x12, 0x34, 0x56, 0x78,
6551
6552 // frame type (ack frame)
6553 // (no ack blocks, 4 byte largest observed, 4 byte block length)
6554 0x4A,
6555 // largest acked
6556 0x12, 0x34, 0x56, 0x78,
6557 // Zero delta time.
6558 0x00, 0x00,
6559 // first ack block length.
6560 0x12, 0x34, 0x56, 0x78,
6561 // num timestamps.
6562 0x00,
6563 };
6564
QUICHE teama6ef0a62019-03-07 20:34:33 -05006565 unsigned char packet46[] = {
6566 // type (short header, 4 byte packet number)
6567 0x43,
6568 // connection_id
6569 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
6570 // packet number
6571 0x12, 0x34, 0x56, 0x78,
6572
6573 // frame type (ack frame)
6574 // (no ack blocks, 4 byte largest observed, 4 byte block length)
6575 0x4A,
6576 // largest acked
6577 0x12, 0x34, 0x56, 0x78,
6578 // Zero delta time.
6579 0x00, 0x00,
6580 // first ack block length.
6581 0x12, 0x34, 0x56, 0x78,
6582 // num timestamps.
6583 0x00,
6584 };
6585
6586
6587 unsigned char packet99[] = {
6588 // type (short header, 4 byte packet number)
6589 0x43,
6590 // connection_id
6591 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
6592 // packet number
6593 0x12, 0x34, 0x56, 0x78,
6594
6595 // frame type (IETF_ACK frame)
6596 0x02,
6597 // largest acked
6598 kVarInt62FourBytes + 0x12, 0x34, 0x56, 0x78,
6599 // Zero delta time.
6600 kVarInt62OneByte + 0x00,
6601 // Nr. of additional ack blocks
6602 kVarInt62OneByte + 0x00,
6603 // first ack block length.
6604 kVarInt62FourBytes + 0x12, 0x34, 0x56, 0x77,
6605 };
6606 // clang-format on
6607 unsigned char* p = packet;
6608 size_t p_size = QUIC_ARRAYSIZE(packet);
fkastenholz305e1732019-06-18 05:01:22 -07006609 if (VersionHasIetfQuicFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05006610 p = packet99;
6611 p_size = QUIC_ARRAYSIZE(packet99);
fayang36825da2019-08-21 14:01:27 -07006612 } else if (framer_.transport_version() >= QUIC_VERSION_46) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05006613 p = packet46;
6614 p_size = QUIC_ARRAYSIZE(packet46);
QUICHE teama6ef0a62019-03-07 20:34:33 -05006615 }
6616
6617 std::unique_ptr<QuicPacket> data(BuildDataPacket(header, frames));
6618 ASSERT_TRUE(data != nullptr);
6619 test::CompareCharArraysWithHexError("constructed packet", data->data(),
6620 data->length(), AsChars(p), p_size);
6621}
6622
6623TEST_P(QuicFramerTest, BuildAckFramePacketMultipleAckBlocks) {
QUICHE team2252b702019-05-14 23:55:14 -04006624 QuicFramerPeer::SetPerspective(&framer_, Perspective::IS_CLIENT);
QUICHE teama6ef0a62019-03-07 20:34:33 -05006625 QuicPacketHeader header;
6626 header.destination_connection_id = FramerTestConnectionId();
6627 header.reset_flag = false;
6628 header.version_flag = false;
6629 header.packet_number = kPacketNumber;
6630
6631 // Use kSmallLargestObserved to make this test finished in a short time.
6632 QuicAckFrame ack_frame =
6633 InitAckFrame({{QuicPacketNumber(1), QuicPacketNumber(5)},
6634 {QuicPacketNumber(10), QuicPacketNumber(500)},
6635 {QuicPacketNumber(900), kSmallMissingPacket},
6636 {kSmallMissingPacket + 1, kSmallLargestObserved + 1}});
6637 ack_frame.ack_delay_time = QuicTime::Delta::Zero();
6638
6639 QuicFrames frames = {QuicFrame(&ack_frame)};
6640
6641 // clang-format off
6642 unsigned char packet[] = {
6643 // public flags (8 byte connection_id)
QUICHE team2252b702019-05-14 23:55:14 -04006644 0x2C,
QUICHE teama6ef0a62019-03-07 20:34:33 -05006645 // connection_id
6646 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
6647 // packet number
6648 0x12, 0x34, 0x56, 0x78,
6649
6650 // frame type (ack frame)
6651 // (has ack blocks, 2 byte largest observed, 2 byte block length)
6652 0x65,
6653 // largest acked
6654 0x12, 0x34,
6655 // Zero delta time.
6656 0x00, 0x00,
6657 // num ack blocks ranges.
6658 0x04,
6659 // first ack block length.
6660 0x00, 0x01,
6661 // gap to next block.
6662 0x01,
6663 // ack block length.
6664 0x0e, 0xaf,
6665 // gap to next block.
6666 0xff,
6667 // ack block length.
6668 0x00, 0x00,
6669 // gap to next block.
6670 0x91,
6671 // ack block length.
6672 0x01, 0xea,
6673 // gap to next block.
6674 0x05,
6675 // ack block length.
6676 0x00, 0x04,
6677 // num timestamps.
6678 0x00,
6679 };
6680
QUICHE teama6ef0a62019-03-07 20:34:33 -05006681 unsigned char packet46[] = {
6682 // type (short header, 4 byte packet number)
6683 0x43,
6684 // connection_id
6685 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
6686 // packet number
6687 0x12, 0x34, 0x56, 0x78,
6688
6689 // frame type (ack frame)
6690 // (has ack blocks, 2 byte largest observed, 2 byte block length)
6691 0x65,
6692 // largest acked
6693 0x12, 0x34,
6694 // Zero delta time.
6695 0x00, 0x00,
6696 // num ack blocks ranges.
6697 0x04,
6698 // first ack block length.
6699 0x00, 0x01,
6700 // gap to next block.
6701 0x01,
6702 // ack block length.
6703 0x0e, 0xaf,
6704 // gap to next block.
6705 0xff,
6706 // ack block length.
6707 0x00, 0x00,
6708 // gap to next block.
6709 0x91,
6710 // ack block length.
6711 0x01, 0xea,
6712 // gap to next block.
6713 0x05,
6714 // ack block length.
6715 0x00, 0x04,
6716 // num timestamps.
6717 0x00,
6718 };
6719
6720 unsigned char packet99[] = {
6721 // type (short header, 4 byte packet number)
6722 0x43,
6723 // connection_id
6724 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
6725 // packet number
6726 0x12, 0x34, 0x56, 0x78,
6727
6728 // frame type (IETF_ACK frame)
6729 0x02,
6730 // largest acked
6731 kVarInt62TwoBytes + 0x12, 0x34,
6732 // Zero delta time.
6733 kVarInt62OneByte + 0x00,
6734 // num additional ack blocks.
6735 kVarInt62OneByte + 0x03,
6736 // first ack block length.
6737 kVarInt62OneByte + 0x00,
6738
6739 // gap to next block.
6740 kVarInt62OneByte + 0x00,
6741 // ack block length.
6742 kVarInt62TwoBytes + 0x0e, 0xae,
6743
6744 // gap to next block.
6745 kVarInt62TwoBytes + 0x01, 0x8f,
6746 // ack block length.
6747 kVarInt62TwoBytes + 0x01, 0xe9,
6748
6749 // gap to next block.
6750 kVarInt62OneByte + 0x04,
6751 // ack block length.
6752 kVarInt62OneByte + 0x03,
6753 };
6754 // clang-format on
6755 unsigned char* p = packet;
6756 size_t p_size = QUIC_ARRAYSIZE(packet);
fkastenholz305e1732019-06-18 05:01:22 -07006757 if (VersionHasIetfQuicFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05006758 p = packet99;
6759 p_size = QUIC_ARRAYSIZE(packet99);
fayang36825da2019-08-21 14:01:27 -07006760 } else if (framer_.transport_version() >= QUIC_VERSION_46) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05006761 p = packet46;
6762 p_size = QUIC_ARRAYSIZE(packet46);
QUICHE teama6ef0a62019-03-07 20:34:33 -05006763 }
6764
6765 std::unique_ptr<QuicPacket> data(BuildDataPacket(header, frames));
6766 ASSERT_TRUE(data != nullptr);
6767
6768 test::CompareCharArraysWithHexError("constructed packet", data->data(),
6769 data->length(), AsChars(p), p_size);
6770}
6771
6772TEST_P(QuicFramerTest, BuildAckFramePacketMaxAckBlocks) {
QUICHE team2252b702019-05-14 23:55:14 -04006773 QuicFramerPeer::SetPerspective(&framer_, Perspective::IS_CLIENT);
QUICHE teama6ef0a62019-03-07 20:34:33 -05006774 QuicPacketHeader header;
6775 header.destination_connection_id = FramerTestConnectionId();
6776 header.reset_flag = false;
6777 header.version_flag = false;
6778 header.packet_number = kPacketNumber;
6779
6780 // Use kSmallLargestObservedto make this test finished in a short time.
6781 QuicAckFrame ack_frame;
6782 ack_frame.largest_acked = kSmallLargestObserved;
6783 ack_frame.ack_delay_time = QuicTime::Delta::Zero();
6784 // 300 ack blocks.
6785 for (size_t i = 2; i < 2 * 300; i += 2) {
6786 ack_frame.packets.Add(QuicPacketNumber(i));
6787 }
6788 ack_frame.packets.AddRange(QuicPacketNumber(600), kSmallLargestObserved + 1);
6789
6790 QuicFrames frames = {QuicFrame(&ack_frame)};
6791
6792 // clang-format off
6793 unsigned char packet[] = {
6794 // public flags (8 byte connection_id)
QUICHE team2252b702019-05-14 23:55:14 -04006795 0x2C,
QUICHE teama6ef0a62019-03-07 20:34:33 -05006796 // connection_id
6797 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
6798 // packet number
6799 0x12, 0x34, 0x56, 0x78,
6800 // frame type (ack frame)
6801 // (has ack blocks, 2 byte largest observed, 2 byte block length)
6802 0x65,
6803 // largest acked
6804 0x12, 0x34,
6805 // Zero delta time.
6806 0x00, 0x00,
6807 // num ack blocks ranges.
6808 0xff,
6809 // first ack block length.
6810 0x0f, 0xdd,
6811 // 255 = 4 * 63 + 3
6812 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
6813 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
6814 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
6815 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
6816 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
6817 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
6818 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
6819 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
6820 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
6821 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
6822
6823 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
6824 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
6825 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
6826 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
6827 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
6828 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
6829 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
6830 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
6831 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
6832 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
6833
6834 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
6835 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
6836 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
6837 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
6838 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
6839 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
6840 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
6841 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
6842 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
6843 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
6844
6845 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
6846 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
6847 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
6848 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
6849 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
6850 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
6851 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
6852 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
6853 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
6854 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
6855
6856 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
6857 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
6858 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
6859 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
6860 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
6861 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
6862 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
6863 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
6864 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
6865 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
6866
6867 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
6868 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
6869 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
6870 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
6871 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
6872 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
6873 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
6874 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
6875 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
6876 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
6877
6878 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
6879 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
6880 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
6881 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
6882 // num timestamps.
6883 0x00,
6884 };
6885
QUICHE teama6ef0a62019-03-07 20:34:33 -05006886 unsigned char packet46[] = {
6887 // type (short header, 4 byte packet number)
6888 0x43,
6889 // connection_id
6890 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
6891 // packet number
6892 0x12, 0x34, 0x56, 0x78,
6893 // frame type (ack frame)
6894 // (has ack blocks, 2 byte largest observed, 2 byte block length)
6895 0x65,
6896 // largest acked
6897 0x12, 0x34,
6898 // Zero delta time.
6899 0x00, 0x00,
6900 // num ack blocks ranges.
6901 0xff,
6902 // first ack block length.
6903 0x0f, 0xdd,
6904 // 255 = 4 * 63 + 3
6905 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
6906 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
6907 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
6908 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
6909 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
6910 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
6911 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
6912 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
6913 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
6914 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
6915
6916 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
6917 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
6918 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
6919 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
6920 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
6921 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
6922 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
6923 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
6924 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
6925 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
6926
6927 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
6928 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
6929 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
6930 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
6931 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
6932 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
6933 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
6934 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
6935 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
6936 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
6937
6938 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
6939 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
6940 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
6941 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
6942 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
6943 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
6944 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
6945 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
6946 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
6947 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
6948
6949 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
6950 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
6951 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
6952 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
6953 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
6954 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
6955 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
6956 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
6957 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
6958 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
6959
6960 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
6961 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
6962 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
6963 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
6964 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
6965 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
6966 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
6967 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
6968 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
6969 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
6970
6971 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
6972 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
6973 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
6974 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
6975 // num timestamps.
6976 0x00,
6977 };
6978
6979 unsigned char packet99[] = {
6980 // type (short header, 4 byte packet number)
6981 0x43,
6982 // connection_id
6983 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
6984 // packet number
6985 0x12, 0x34, 0x56, 0x78,
6986 // frame type (IETF_ACK frame)
6987 0x02,
6988 // largest acked
6989 kVarInt62TwoBytes + 0x12, 0x34,
6990 // Zero delta time.
6991 kVarInt62OneByte + 0x00,
6992 // num ack blocks ranges.
6993 kVarInt62TwoBytes + 0x01, 0x2b,
6994 // first ack block length.
6995 kVarInt62TwoBytes + 0x0f, 0xdc,
6996 // 255 added blocks of gap_size == 1, ack_size == 1
6997#define V99AddedBLOCK kVarInt62OneByte + 0x00, kVarInt62OneByte + 0x00
6998 V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK,
6999 V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK,
7000 V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK,
7001 V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK,
7002 V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK,
7003 V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK,
7004 V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK,
7005 V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK,
7006 V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK,
7007 V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK,
7008 V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK,
7009 V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK,
7010 V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK,
7011 V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK,
7012 V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK,
7013 V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK,
7014 V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK,
7015 V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK,
7016 V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK,
7017 V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK,
7018
7019 V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK,
7020 V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK,
7021 V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK,
7022 V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK,
7023 V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK,
7024 V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK,
7025 V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK,
7026 V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK,
7027 V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK,
7028 V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK,
7029 V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK,
7030 V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK,
7031 V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK,
7032 V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK,
7033 V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK,
7034 V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK,
7035 V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK,
7036 V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK,
7037 V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK,
7038 V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK,
7039
7040 V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK,
7041 V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK,
7042 V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK,
7043 V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK,
7044 V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK,
7045 V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK,
7046 V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK,
7047 V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK,
7048 V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK,
7049 V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK,
7050 V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK,
7051 V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK,
7052 V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK,
7053 V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK,
7054 V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK,
7055 V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK,
7056 V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK,
7057 V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK,
7058 V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK,
7059 V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK,
7060
7061#undef V99AddedBLOCK
7062 };
7063 // clang-format on
7064 unsigned char* p = packet;
7065 size_t p_size = QUIC_ARRAYSIZE(packet);
fkastenholz305e1732019-06-18 05:01:22 -07007066 if (VersionHasIetfQuicFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05007067 p = packet99;
7068 p_size = QUIC_ARRAYSIZE(packet99);
fayang36825da2019-08-21 14:01:27 -07007069 } else if (framer_.transport_version() >= QUIC_VERSION_46) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05007070 p = packet46;
7071 p_size = QUIC_ARRAYSIZE(packet46);
QUICHE teama6ef0a62019-03-07 20:34:33 -05007072 }
7073
7074 std::unique_ptr<QuicPacket> data(BuildDataPacket(header, frames));
7075 ASSERT_TRUE(data != nullptr);
7076
7077 test::CompareCharArraysWithHexError("constructed packet", data->data(),
7078 data->length(), AsChars(p), p_size);
7079}
7080
7081TEST_P(QuicFramerTest, BuildNewStopWaitingPacket) {
7082 if (version_.transport_version > QUIC_VERSION_43) {
7083 return;
7084 }
QUICHE team2252b702019-05-14 23:55:14 -04007085 QuicFramerPeer::SetPerspective(&framer_, Perspective::IS_CLIENT);
QUICHE teama6ef0a62019-03-07 20:34:33 -05007086 QuicPacketHeader header;
7087 header.destination_connection_id = FramerTestConnectionId();
7088 header.reset_flag = false;
7089 header.version_flag = false;
7090 header.packet_number = kPacketNumber;
7091
7092 QuicStopWaitingFrame stop_waiting_frame;
7093 stop_waiting_frame.least_unacked = kLeastUnacked;
7094
7095 QuicFrames frames = {QuicFrame(stop_waiting_frame)};
7096
7097 // clang-format off
7098 unsigned char packet[] = {
7099 // public flags (8 byte connection_id)
QUICHE team2252b702019-05-14 23:55:14 -04007100 0x2C,
QUICHE teama6ef0a62019-03-07 20:34:33 -05007101 // connection_id
7102 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
7103 // packet number
7104 0x12, 0x34, 0x56, 0x78,
7105
7106 // frame type (stop waiting frame)
7107 0x06,
7108 // least packet number awaiting an ack, delta from packet number.
7109 0x00, 0x00, 0x00, 0x08,
7110 };
7111
7112 // clang-format on
7113
7114 std::unique_ptr<QuicPacket> data(BuildDataPacket(header, frames));
7115 ASSERT_TRUE(data != nullptr);
7116
7117 test::CompareCharArraysWithHexError("constructed packet", data->data(),
7118 data->length(), AsChars(packet),
7119 QUIC_ARRAYSIZE(packet));
7120}
7121
7122TEST_P(QuicFramerTest, BuildRstFramePacketQuic) {
QUICHE team2252b702019-05-14 23:55:14 -04007123 QuicFramerPeer::SetPerspective(&framer_, Perspective::IS_CLIENT);
QUICHE teama6ef0a62019-03-07 20:34:33 -05007124 QuicPacketHeader header;
7125 header.destination_connection_id = FramerTestConnectionId();
7126 header.reset_flag = false;
7127 header.version_flag = false;
7128 header.packet_number = kPacketNumber;
7129
7130 QuicRstStreamFrame rst_frame;
7131 rst_frame.stream_id = kStreamId;
fkastenholz305e1732019-06-18 05:01:22 -07007132 if (VersionHasIetfQuicFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05007133 rst_frame.ietf_error_code = 0x01;
7134 } else {
7135 rst_frame.error_code = static_cast<QuicRstStreamErrorCode>(0x05060708);
7136 }
7137 rst_frame.byte_offset = 0x0807060504030201;
7138
7139 // clang-format off
7140 unsigned char packet[] = {
7141 // public flags (8 byte connection_id)
QUICHE team2252b702019-05-14 23:55:14 -04007142 0x2C,
QUICHE teama6ef0a62019-03-07 20:34:33 -05007143 // connection_id
7144 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
7145 // packet number
7146 0x12, 0x34, 0x56, 0x78,
7147
7148 // frame type (rst stream frame)
7149 0x01,
7150 // stream id
7151 0x01, 0x02, 0x03, 0x04,
7152 // sent byte offset
7153 0x08, 0x07, 0x06, 0x05,
7154 0x04, 0x03, 0x02, 0x01,
7155 // error code
7156 0x05, 0x06, 0x07, 0x08,
7157 };
7158
QUICHE teama6ef0a62019-03-07 20:34:33 -05007159 unsigned char packet46[] = {
7160 // type (short packet, 4 byte packet number)
7161 0x43,
7162 // connection_id
7163 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
7164 // packet number
7165 0x12, 0x34, 0x56, 0x78,
7166
7167 // frame type (rst stream frame)
7168 0x01,
7169 // stream id
7170 0x01, 0x02, 0x03, 0x04,
7171 // sent byte offset
7172 0x08, 0x07, 0x06, 0x05,
7173 0x04, 0x03, 0x02, 0x01,
7174 // error code
7175 0x05, 0x06, 0x07, 0x08,
7176 };
7177
7178 unsigned char packet99[] = {
7179 // type (short packet, 4 byte packet number)
7180 0x43,
7181 // connection_id
7182 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
7183 // packet number
7184 0x12, 0x34, 0x56, 0x78,
7185
7186 // frame type (IETF_RST_STREAM frame)
7187 0x04,
7188 // stream id
7189 kVarInt62FourBytes + 0x01, 0x02, 0x03, 0x04,
fkastenholz07300e52019-07-16 11:51:37 -07007190 // error code
7191 kVarInt62OneByte + 0x01,
QUICHE teama6ef0a62019-03-07 20:34:33 -05007192 // sent byte offset
7193 kVarInt62EightBytes + 0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01
7194 };
7195 // clang-format on
7196
7197 QuicFrames frames = {QuicFrame(&rst_frame)};
7198
7199 std::unique_ptr<QuicPacket> data(BuildDataPacket(header, frames));
7200 ASSERT_TRUE(data != nullptr);
7201
7202 unsigned char* p = packet;
7203 size_t p_size = QUIC_ARRAYSIZE(packet);
fkastenholz305e1732019-06-18 05:01:22 -07007204 if (VersionHasIetfQuicFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05007205 p = packet99;
7206 p_size = QUIC_ARRAYSIZE(packet99);
fayang36825da2019-08-21 14:01:27 -07007207 } else if (framer_.transport_version() >= QUIC_VERSION_46) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05007208 p = packet46;
7209 p_size = QUIC_ARRAYSIZE(packet46);
QUICHE teama6ef0a62019-03-07 20:34:33 -05007210 }
7211 QuicEncryptedPacket encrypted(AsChars(p), p_size, false);
7212
7213 test::CompareCharArraysWithHexError("constructed packet", data->data(),
7214 data->length(), AsChars(p), p_size);
7215}
7216
7217TEST_P(QuicFramerTest, BuildCloseFramePacket) {
QUICHE team2252b702019-05-14 23:55:14 -04007218 QuicFramerPeer::SetPerspective(&framer_, Perspective::IS_CLIENT);
QUICHE teama6ef0a62019-03-07 20:34:33 -05007219 QuicPacketHeader header;
7220 header.destination_connection_id = FramerTestConnectionId();
7221 header.reset_flag = false;
7222 header.version_flag = false;
7223 header.packet_number = kPacketNumber;
7224
fkastenholz2d55b912019-09-10 15:57:17 -07007225 QuicConnectionCloseFrame close_frame(
7226 framer_.transport_version(),
7227 static_cast<QuicErrorCode>(
7228 VersionHasIetfQuicFrames(framer_.transport_version()) ? 0x11
7229 : 0x05060708),
7230 "because I can", 0x05);
7231 close_frame.extracted_error_code = QUIC_IETF_GQUIC_ERROR_MISSING;
QUICHE teama6ef0a62019-03-07 20:34:33 -05007232 QuicFrames frames = {QuicFrame(&close_frame)};
7233
7234 // clang-format off
7235 unsigned char packet[] = {
7236 // public flags (8 byte connection_id)
QUICHE team2252b702019-05-14 23:55:14 -04007237 0x2C,
QUICHE teama6ef0a62019-03-07 20:34:33 -05007238 // connection_id
7239 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
7240 // packet number
7241 0x12, 0x34, 0x56, 0x78,
7242
7243 // frame type (connection close frame)
7244 0x02,
7245 // error code
7246 0x05, 0x06, 0x07, 0x08,
7247 // error details length
7248 0x00, 0x0d,
7249 // error details
7250 'b', 'e', 'c', 'a',
7251 'u', 's', 'e', ' ',
7252 'I', ' ', 'c', 'a',
7253 'n',
7254 };
7255
QUICHE teama6ef0a62019-03-07 20:34:33 -05007256 unsigned char packet46[] = {
7257 // type (short header, 4 byte packet number)
7258 0x43,
7259 // connection_id
7260 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
7261 // packet number
7262 0x12, 0x34, 0x56, 0x78,
7263
7264 // frame type (connection close frame)
7265 0x02,
7266 // error code
7267 0x05, 0x06, 0x07, 0x08,
7268 // error details length
7269 0x00, 0x0d,
7270 // error details
7271 'b', 'e', 'c', 'a',
7272 'u', 's', 'e', ' ',
7273 'I', ' ', 'c', 'a',
7274 'n',
7275 };
7276
7277 unsigned char packet99[] = {
7278 // type (short header, 4 byte packet number)
7279 0x43,
7280 // connection_id
7281 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
7282 // packet number
7283 0x12, 0x34, 0x56, 0x78,
7284
7285 // frame type (IETF_CONNECTION_CLOSE frame)
7286 0x1c,
7287 // error code
fkastenholzd57d3f92019-07-16 09:05:17 -07007288 kVarInt62OneByte + 0x11,
QUICHE teama6ef0a62019-03-07 20:34:33 -05007289 // Frame type within the CONNECTION_CLOSE frame
7290 kVarInt62OneByte + 0x05,
7291 // error details length
7292 kVarInt62OneByte + 0x0d,
7293 // error details
7294 'b', 'e', 'c', 'a',
7295 'u', 's', 'e', ' ',
7296 'I', ' ', 'c', 'a',
7297 'n',
7298 };
7299 // clang-format on
7300
7301 unsigned char* p = packet;
7302 size_t p_size = QUIC_ARRAYSIZE(packet);
fkastenholz305e1732019-06-18 05:01:22 -07007303 if (VersionHasIetfQuicFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05007304 p = packet99;
7305 p_size = QUIC_ARRAYSIZE(packet99);
fayang36825da2019-08-21 14:01:27 -07007306 } else if (framer_.transport_version() >= QUIC_VERSION_46) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05007307 p = packet46;
7308 p_size = QUIC_ARRAYSIZE(packet46);
QUICHE teama6ef0a62019-03-07 20:34:33 -05007309 }
7310
7311 std::unique_ptr<QuicPacket> data(BuildDataPacket(header, frames));
7312 ASSERT_TRUE(data != nullptr);
7313
7314 test::CompareCharArraysWithHexError("constructed packet", data->data(),
7315 data->length(), AsChars(p), p_size);
7316}
7317
fkastenholzb4dade72019-08-05 06:54:20 -07007318TEST_P(QuicFramerTest, BuildCloseFramePacketExtendedInfo) {
7319 QuicFramerPeer::SetPerspective(&framer_, Perspective::IS_CLIENT);
7320 QuicPacketHeader header;
7321 header.destination_connection_id = FramerTestConnectionId();
7322 header.reset_flag = false;
7323 header.version_flag = false;
7324 header.packet_number = kPacketNumber;
7325
fkastenholz2d55b912019-09-10 15:57:17 -07007326 QuicConnectionCloseFrame close_frame(
7327 framer_.transport_version(),
7328 static_cast<QuicErrorCode>(
7329 VersionHasIetfQuicFrames(framer_.transport_version()) ? 0x11
7330 : 0x05060708),
7331 "because I can", 0x05);
fkastenholzb4dade72019-08-05 06:54:20 -07007332 // Set this so that it is "there" for both Google QUIC and IETF QUIC
7333 // framing. It better not show up for Google QUIC!
7334 close_frame.extracted_error_code = static_cast<QuicErrorCode>(0x4567);
7335
fkastenholzb4dade72019-08-05 06:54:20 -07007336 QuicFrames frames = {QuicFrame(&close_frame)};
7337
7338 // clang-format off
7339 unsigned char packet[] = {
7340 // public flags (8 byte connection_id)
7341 0x2C,
7342 // connection_id
7343 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
7344 // packet number
7345 0x12, 0x34, 0x56, 0x78,
7346
7347 // frame type (connection close frame)
7348 0x02,
7349 // error code
7350 0x05, 0x06, 0x07, 0x08,
7351 // error details length
7352 0x00, 0x0d,
7353 // error details
7354 'b', 'e', 'c', 'a',
7355 'u', 's', 'e', ' ',
7356 'I', ' ', 'c', 'a',
7357 'n',
7358 };
7359
fkastenholzb4dade72019-08-05 06:54:20 -07007360 unsigned char packet46[] = {
7361 // type (short header, 4 byte packet number)
7362 0x43,
7363 // connection_id
7364 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
7365 // packet number
7366 0x12, 0x34, 0x56, 0x78,
7367
7368 // frame type (connection close frame)
7369 0x02,
7370 // error code
7371 0x05, 0x06, 0x07, 0x08,
7372 // error details length
7373 0x00, 0x0d,
7374 // error details
7375 'b', 'e', 'c', 'a',
7376 'u', 's', 'e', ' ',
7377 'I', ' ', 'c', 'a',
7378 'n',
7379 };
7380
7381 unsigned char packet99[] = {
7382 // type (short header, 4 byte packet number)
7383 0x43,
7384 // connection_id
7385 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
7386 // packet number
7387 0x12, 0x34, 0x56, 0x78,
7388
7389 // frame type (IETF_CONNECTION_CLOSE frame)
7390 0x1c,
7391 // error code
7392 kVarInt62OneByte + 0x11,
7393 // Frame type within the CONNECTION_CLOSE frame
7394 kVarInt62OneByte + 0x05,
7395 // error details length
7396 kVarInt62OneByte + 0x13,
7397 // error details
7398 '1', '7', '7', '6',
7399 '7', ':', 'b', 'e',
7400 'c', 'a', 'u', 's',
7401 'e', ' ', 'I', ' ',
7402 'c', 'a', 'n'
7403 };
7404 // clang-format on
7405
7406 unsigned char* p = packet;
7407 size_t p_size = QUIC_ARRAYSIZE(packet);
7408 if (VersionHasIetfQuicFrames(framer_.transport_version())) {
7409 p = packet99;
7410 p_size = QUIC_ARRAYSIZE(packet99);
fayang36825da2019-08-21 14:01:27 -07007411 } else if (framer_.transport_version() >= QUIC_VERSION_46) {
fkastenholzb4dade72019-08-05 06:54:20 -07007412 p = packet46;
7413 p_size = QUIC_ARRAYSIZE(packet46);
fkastenholzb4dade72019-08-05 06:54:20 -07007414 }
7415
7416 std::unique_ptr<QuicPacket> data(BuildDataPacket(header, frames));
7417 ASSERT_TRUE(data != nullptr);
7418
7419 test::CompareCharArraysWithHexError("constructed packet", data->data(),
7420 data->length(), AsChars(p), p_size);
7421}
7422
QUICHE teama6ef0a62019-03-07 20:34:33 -05007423TEST_P(QuicFramerTest, BuildTruncatedCloseFramePacket) {
QUICHE team2252b702019-05-14 23:55:14 -04007424 QuicFramerPeer::SetPerspective(&framer_, Perspective::IS_CLIENT);
QUICHE teama6ef0a62019-03-07 20:34:33 -05007425 QuicPacketHeader header;
7426 header.destination_connection_id = FramerTestConnectionId();
7427 header.reset_flag = false;
7428 header.version_flag = false;
7429 header.packet_number = kPacketNumber;
7430
fkastenholz2d55b912019-09-10 15:57:17 -07007431 QuicConnectionCloseFrame close_frame(
7432 framer_.transport_version(),
7433 static_cast<QuicErrorCode>(
7434 VersionHasIetfQuicFrames(framer_.transport_version()) ? 0xa
7435 : 0x05060708),
7436 std::string(2048, 'A'), 0x05);
7437 close_frame.extracted_error_code = QUIC_IETF_GQUIC_ERROR_MISSING;
QUICHE teama6ef0a62019-03-07 20:34:33 -05007438 QuicFrames frames = {QuicFrame(&close_frame)};
7439
7440 // clang-format off
7441 unsigned char packet[] = {
7442 // public flags (8 byte connection_id)
QUICHE team2252b702019-05-14 23:55:14 -04007443 0x2C,
QUICHE teama6ef0a62019-03-07 20:34:33 -05007444 // connection_id
7445 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
7446 // packet number
7447 0x12, 0x34, 0x56, 0x78,
7448
7449 // frame type (connection close frame)
7450 0x02,
7451 // error code
7452 0x05, 0x06, 0x07, 0x08,
7453 // error details length
7454 0x01, 0x00,
7455 // error details (truncated to 256 bytes)
7456 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7457 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7458 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7459 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7460 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7461 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7462 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7463 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7464 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7465 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7466 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7467 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7468 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7469 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7470 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7471 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7472 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7473 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7474 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7475 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7476 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7477 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7478 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7479 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7480 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7481 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7482 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7483 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7484 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7485 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7486 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7487 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7488 };
7489
QUICHE teama6ef0a62019-03-07 20:34:33 -05007490 unsigned char packet46[] = {
7491 // type (short header, 4 byte packet number)
7492 0x43,
7493 // connection_id
7494 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
7495 // packet number
7496 0x12, 0x34, 0x56, 0x78,
7497
7498 // frame type (connection close frame)
7499 0x02,
7500 // error code
7501 0x05, 0x06, 0x07, 0x08,
7502 // error details length
7503 0x01, 0x00,
7504 // error details (truncated to 256 bytes)
7505 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7506 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7507 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7508 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7509 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7510 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7511 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7512 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7513 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7514 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7515 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7516 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7517 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7518 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7519 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7520 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7521 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7522 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7523 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7524 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7525 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7526 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7527 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7528 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7529 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7530 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7531 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7532 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7533 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7534 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7535 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7536 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7537 };
7538
7539 unsigned char packet99[] = {
7540 // type (short header, 4 byte packet number)
7541 0x43,
7542 // connection_id
7543 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
7544 // packet number
7545 0x12, 0x34, 0x56, 0x78,
7546
7547 // frame type (IETF_CONNECTION_CLOSE frame)
7548 0x1c,
7549 // error code
fkastenholzd57d3f92019-07-16 09:05:17 -07007550 kVarInt62OneByte + 0x0a,
QUICHE teama6ef0a62019-03-07 20:34:33 -05007551 // Frame type within the CONNECTION_CLOSE frame
fkastenholz2d55b912019-09-10 15:57:17 -07007552 kVarInt62OneByte + 0x05,
QUICHE teama6ef0a62019-03-07 20:34:33 -05007553 // error details length
7554 kVarInt62TwoBytes + 0x01, 0x00,
7555 // error details (truncated to 256 bytes)
7556 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7557 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7558 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7559 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7560 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7561 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7562 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7563 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7564 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7565 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7566 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7567 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7568 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7569 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7570 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7571 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7572 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7573 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7574 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7575 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7576 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7577 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7578 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7579 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7580 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7581 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7582 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7583 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7584 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7585 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7586 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7587 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7588 };
7589 // clang-format on
7590
7591 unsigned char* p = packet;
7592 size_t p_size = QUIC_ARRAYSIZE(packet);
fkastenholz305e1732019-06-18 05:01:22 -07007593 if (VersionHasIetfQuicFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05007594 p = packet99;
7595 p_size = QUIC_ARRAYSIZE(packet99);
fayang36825da2019-08-21 14:01:27 -07007596 } else if (framer_.transport_version() >= QUIC_VERSION_46) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05007597 p = packet46;
7598 p_size = QUIC_ARRAYSIZE(packet46);
QUICHE teama6ef0a62019-03-07 20:34:33 -05007599 }
7600
7601 std::unique_ptr<QuicPacket> data(BuildDataPacket(header, frames));
7602 ASSERT_TRUE(data != nullptr);
7603
7604 test::CompareCharArraysWithHexError("constructed packet", data->data(),
7605 data->length(), AsChars(p), p_size);
7606}
7607
7608TEST_P(QuicFramerTest, BuildApplicationCloseFramePacket) {
fkastenholz305e1732019-06-18 05:01:22 -07007609 if (!VersionHasIetfQuicFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05007610 // Versions other than 99 do not have ApplicationClose
7611 return;
7612 }
QUICHE team2252b702019-05-14 23:55:14 -04007613 QuicFramerPeer::SetPerspective(&framer_, Perspective::IS_CLIENT);
QUICHE teama6ef0a62019-03-07 20:34:33 -05007614 QuicPacketHeader header;
7615 header.destination_connection_id = FramerTestConnectionId();
7616 header.reset_flag = false;
7617 header.version_flag = false;
7618 header.packet_number = kPacketNumber;
7619
fkastenholz72f509b2019-04-10 09:17:49 -07007620 QuicConnectionCloseFrame app_close_frame;
fkastenholzd57d3f92019-07-16 09:05:17 -07007621 app_close_frame.application_error_code =
7622 static_cast<uint64_t>(QUIC_INVALID_STREAM_ID);
QUICHE teama6ef0a62019-03-07 20:34:33 -05007623 app_close_frame.error_details = "because I can";
fkastenholz72f509b2019-04-10 09:17:49 -07007624 app_close_frame.close_type = IETF_QUIC_APPLICATION_CONNECTION_CLOSE;
QUICHE teama6ef0a62019-03-07 20:34:33 -05007625
7626 QuicFrames frames = {QuicFrame(&app_close_frame)};
7627
7628 // clang-format off
7629
7630 unsigned char packet99[] = {
7631 // type (short header, 4 byte packet number)
7632 0x43,
7633 // connection_id
7634 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
7635 // packet number
7636 0x12, 0x34, 0x56, 0x78,
7637
7638 // frame type (IETF_APPLICATION_CLOSE frame)
7639 0x1d,
7640 // error code
fkastenholzd57d3f92019-07-16 09:05:17 -07007641 kVarInt62OneByte + 0x11,
QUICHE teama6ef0a62019-03-07 20:34:33 -05007642 // error details length
fkastenholz2d55b912019-09-10 15:57:17 -07007643 kVarInt62OneByte + 0x0f,
7644 // error details, note that it includes an extended error code.
7645 '0', ':', 'b', 'e',
7646 'c', 'a', 'u', 's',
7647 'e', ' ', 'I', ' ',
7648 'c', 'a', 'n',
QUICHE teama6ef0a62019-03-07 20:34:33 -05007649 };
7650 // clang-format on
7651
7652 std::unique_ptr<QuicPacket> data(BuildDataPacket(header, frames));
7653 ASSERT_TRUE(data != nullptr);
7654
7655 test::CompareCharArraysWithHexError("constructed packet", data->data(),
7656 data->length(), AsChars(packet99),
7657 QUIC_ARRAYSIZE(packet99));
7658}
7659
7660TEST_P(QuicFramerTest, BuildTruncatedApplicationCloseFramePacket) {
fkastenholz305e1732019-06-18 05:01:22 -07007661 if (!VersionHasIetfQuicFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05007662 // Versions other than 99 do not have this frame.
7663 return;
7664 }
QUICHE team2252b702019-05-14 23:55:14 -04007665 QuicFramerPeer::SetPerspective(&framer_, Perspective::IS_CLIENT);
QUICHE teama6ef0a62019-03-07 20:34:33 -05007666 QuicPacketHeader header;
7667 header.destination_connection_id = FramerTestConnectionId();
7668 header.reset_flag = false;
7669 header.version_flag = false;
7670 header.packet_number = kPacketNumber;
7671
fkastenholz72f509b2019-04-10 09:17:49 -07007672 QuicConnectionCloseFrame app_close_frame;
fkastenholzd57d3f92019-07-16 09:05:17 -07007673 app_close_frame.application_error_code =
7674 static_cast<uint64_t>(QUIC_INVALID_STREAM_ID);
vasilvvc48c8712019-03-11 13:38:16 -07007675 app_close_frame.error_details = std::string(2048, 'A');
fkastenholz72f509b2019-04-10 09:17:49 -07007676 app_close_frame.close_type = IETF_QUIC_APPLICATION_CONNECTION_CLOSE;
fkastenholz2d55b912019-09-10 15:57:17 -07007677 // Setting to missing ensures that if it is missing, the extended
7678 // code is not added to the text message.
7679 app_close_frame.extracted_error_code = QUIC_IETF_GQUIC_ERROR_MISSING;
QUICHE teama6ef0a62019-03-07 20:34:33 -05007680
7681 QuicFrames frames = {QuicFrame(&app_close_frame)};
7682
7683 // clang-format off
7684 unsigned char packet99[] = {
7685 // type (short header, 4 byte packet number)
7686 0x43,
7687 // connection_id
7688 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
7689 // packet number
7690 0x12, 0x34, 0x56, 0x78,
7691
7692 // frame type (IETF_APPLICATION_CLOSE frame)
7693 0x1d,
7694 // error code
fkastenholzd57d3f92019-07-16 09:05:17 -07007695 kVarInt62OneByte + 0x11,
QUICHE teama6ef0a62019-03-07 20:34:33 -05007696 // error details length
7697 kVarInt62TwoBytes + 0x01, 0x00,
7698 // error details (truncated to 256 bytes)
7699 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7700 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7701 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7702 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7703 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7704 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7705 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7706 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7707 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7708 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7709 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7710 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7711 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7712 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7713 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7714 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7715 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7716 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7717 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7718 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7719 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7720 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7721 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7722 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7723 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7724 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7725 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7726 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7727 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7728 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7729 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7730 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7731 };
7732 // clang-format on
7733
7734 std::unique_ptr<QuicPacket> data(BuildDataPacket(header, frames));
7735 ASSERT_TRUE(data != nullptr);
7736
7737 test::CompareCharArraysWithHexError("constructed packet", data->data(),
7738 data->length(), AsChars(packet99),
7739 QUIC_ARRAYSIZE(packet99));
7740}
7741
7742TEST_P(QuicFramerTest, BuildGoAwayPacket) {
fkastenholz305e1732019-06-18 05:01:22 -07007743 if (VersionHasIetfQuicFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05007744 // This frame type is not supported in version 99.
7745 return;
7746 }
QUICHE team2252b702019-05-14 23:55:14 -04007747 QuicFramerPeer::SetPerspective(&framer_, Perspective::IS_CLIENT);
QUICHE teama6ef0a62019-03-07 20:34:33 -05007748 QuicPacketHeader header;
7749 header.destination_connection_id = FramerTestConnectionId();
7750 header.reset_flag = false;
7751 header.version_flag = false;
7752 header.packet_number = kPacketNumber;
7753
7754 QuicGoAwayFrame goaway_frame;
7755 goaway_frame.error_code = static_cast<QuicErrorCode>(0x05060708);
7756 goaway_frame.last_good_stream_id = kStreamId;
7757 goaway_frame.reason_phrase = "because I can";
7758
7759 QuicFrames frames = {QuicFrame(&goaway_frame)};
7760
7761 // clang-format off
7762 unsigned char packet[] = {
7763 // public flags (8 byte connection_id)
QUICHE team2252b702019-05-14 23:55:14 -04007764 0x2C,
QUICHE teama6ef0a62019-03-07 20:34:33 -05007765 // connection_id
7766 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
7767 // packet number
7768 0x12, 0x34, 0x56, 0x78,
7769
7770 // frame type (go away frame)
7771 0x03,
7772 // error code
7773 0x05, 0x06, 0x07, 0x08,
7774 // stream id
7775 0x01, 0x02, 0x03, 0x04,
7776 // error details length
7777 0x00, 0x0d,
7778 // error details
7779 'b', 'e', 'c', 'a',
7780 'u', 's', 'e', ' ',
7781 'I', ' ', 'c', 'a',
7782 'n',
7783 };
7784
QUICHE teama6ef0a62019-03-07 20:34:33 -05007785 unsigned char packet46[] = {
7786 // type (short header, 4 byte packet number)
7787 0x43,
7788 // connection_id
7789 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
7790 // packet number
7791 0x12, 0x34, 0x56, 0x78,
7792
7793 // frame type (go away frame)
7794 0x03,
7795 // error code
7796 0x05, 0x06, 0x07, 0x08,
7797 // stream id
7798 0x01, 0x02, 0x03, 0x04,
7799 // error details length
7800 0x00, 0x0d,
7801 // error details
7802 'b', 'e', 'c', 'a',
7803 'u', 's', 'e', ' ',
7804 'I', ' ', 'c', 'a',
7805 'n',
7806 };
7807
7808 // clang-format on
7809
7810 unsigned char* p = packet;
7811 size_t p_size = QUIC_ARRAYSIZE(packet);
fayang36825da2019-08-21 14:01:27 -07007812 if (framer_.transport_version() > QUIC_VERSION_43) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05007813 p = packet46;
7814 p_size = QUIC_ARRAYSIZE(packet46);
QUICHE teama6ef0a62019-03-07 20:34:33 -05007815 }
7816
7817 std::unique_ptr<QuicPacket> data(BuildDataPacket(header, frames));
7818 ASSERT_TRUE(data != nullptr);
7819
7820 test::CompareCharArraysWithHexError("constructed packet", data->data(),
7821 data->length(), AsChars(p), p_size);
7822}
7823
7824TEST_P(QuicFramerTest, BuildTruncatedGoAwayPacket) {
fkastenholz305e1732019-06-18 05:01:22 -07007825 if (VersionHasIetfQuicFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05007826 // This frame type is not supported in version 99.
7827 return;
7828 }
QUICHE team2252b702019-05-14 23:55:14 -04007829 QuicFramerPeer::SetPerspective(&framer_, Perspective::IS_CLIENT);
QUICHE teama6ef0a62019-03-07 20:34:33 -05007830 QuicPacketHeader header;
7831 header.destination_connection_id = FramerTestConnectionId();
7832 header.reset_flag = false;
7833 header.version_flag = false;
7834 header.packet_number = kPacketNumber;
7835
7836 QuicGoAwayFrame goaway_frame;
7837 goaway_frame.error_code = static_cast<QuicErrorCode>(0x05060708);
7838 goaway_frame.last_good_stream_id = kStreamId;
vasilvvc48c8712019-03-11 13:38:16 -07007839 goaway_frame.reason_phrase = std::string(2048, 'A');
QUICHE teama6ef0a62019-03-07 20:34:33 -05007840
7841 QuicFrames frames = {QuicFrame(&goaway_frame)};
7842
7843 // clang-format off
7844 unsigned char packet[] = {
7845 // public flags (8 byte connection_id)
QUICHE team2252b702019-05-14 23:55:14 -04007846 0x2C,
QUICHE teama6ef0a62019-03-07 20:34:33 -05007847 // connection_id
7848 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
7849 // packet number
7850 0x12, 0x34, 0x56, 0x78,
7851
7852 // frame type (go away frame)
7853 0x03,
7854 // error code
7855 0x05, 0x06, 0x07, 0x08,
7856 // stream id
7857 0x01, 0x02, 0x03, 0x04,
7858 // error details length
7859 0x01, 0x00,
7860 // error details (truncated to 256 bytes)
7861 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7862 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7863 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7864 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7865 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7866 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7867 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7868 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7869 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7870 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7871 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7872 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7873 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7874 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7875 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7876 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7877 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7878 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7879 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7880 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7881 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7882 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7883 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7884 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7885 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7886 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7887 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7888 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7889 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7890 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7891 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7892 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7893 };
7894
QUICHE teama6ef0a62019-03-07 20:34:33 -05007895 unsigned char packet46[] = {
7896 // type (short header, 4 byte packet number)
7897 0x43,
7898 // connection_id
7899 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
7900 // packet number
7901 0x12, 0x34, 0x56, 0x78,
7902
7903 // frame type (go away frame)
7904 0x03,
7905 // error code
7906 0x05, 0x06, 0x07, 0x08,
7907 // stream id
7908 0x01, 0x02, 0x03, 0x04,
7909 // error details length
7910 0x01, 0x00,
7911 // error details (truncated to 256 bytes)
7912 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7913 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7914 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7915 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7916 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7917 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7918 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7919 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7920 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7921 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7922 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7923 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7924 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7925 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7926 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7927 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7928 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7929 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7930 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7931 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7932 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7933 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7934 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7935 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7936 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7937 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7938 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7939 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7940 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7941 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7942 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7943 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7944 };
7945 // clang-format on
7946
7947 unsigned char* p = packet;
7948 size_t p_size = QUIC_ARRAYSIZE(packet);
fayang36825da2019-08-21 14:01:27 -07007949 if (framer_.transport_version() > QUIC_VERSION_43) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05007950 p = packet46;
7951 p_size = QUIC_ARRAYSIZE(packet46);
QUICHE teama6ef0a62019-03-07 20:34:33 -05007952 }
7953
7954 std::unique_ptr<QuicPacket> data(BuildDataPacket(header, frames));
7955 ASSERT_TRUE(data != nullptr);
7956
7957 test::CompareCharArraysWithHexError("constructed packet", data->data(),
7958 data->length(), AsChars(p), p_size);
7959}
7960
7961TEST_P(QuicFramerTest, BuildWindowUpdatePacket) {
QUICHE team2252b702019-05-14 23:55:14 -04007962 QuicFramerPeer::SetPerspective(&framer_, Perspective::IS_CLIENT);
QUICHE teama6ef0a62019-03-07 20:34:33 -05007963 QuicPacketHeader header;
7964 header.destination_connection_id = FramerTestConnectionId();
7965 header.reset_flag = false;
7966 header.version_flag = false;
7967 header.packet_number = kPacketNumber;
7968
7969 QuicWindowUpdateFrame window_update_frame;
7970 window_update_frame.stream_id = kStreamId;
7971 window_update_frame.byte_offset = 0x1122334455667788;
7972
7973 QuicFrames frames = {QuicFrame(&window_update_frame)};
7974
7975 // clang-format off
7976 unsigned char packet[] = {
7977 // public flags (8 byte connection_id)
QUICHE team2252b702019-05-14 23:55:14 -04007978 0x2C,
QUICHE teama6ef0a62019-03-07 20:34:33 -05007979 // connection_id
7980 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
7981 // packet number
7982 0x12, 0x34, 0x56, 0x78,
7983
7984 // frame type (window update frame)
7985 0x04,
7986 // stream id
7987 0x01, 0x02, 0x03, 0x04,
7988 // byte offset
7989 0x11, 0x22, 0x33, 0x44,
7990 0x55, 0x66, 0x77, 0x88,
7991 };
7992
QUICHE teama6ef0a62019-03-07 20:34:33 -05007993 unsigned char packet46[] = {
7994 // type (short header, 4 byte packet number)
7995 0x43,
7996 // connection_id
7997 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
7998 // packet number
7999 0x12, 0x34, 0x56, 0x78,
8000
8001 // frame type (window update frame)
8002 0x04,
8003 // stream id
8004 0x01, 0x02, 0x03, 0x04,
8005 // byte offset
8006 0x11, 0x22, 0x33, 0x44,
8007 0x55, 0x66, 0x77, 0x88,
8008 };
8009
8010 unsigned char packet99[] = {
8011 // type (short header, 4 byte packet number)
8012 0x43,
8013 // connection_id
8014 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
8015 // packet number
8016 0x12, 0x34, 0x56, 0x78,
8017
8018 // frame type (IETF_MAX_STREAM_DATA frame)
8019 0x11,
8020 // stream id
8021 kVarInt62FourBytes + 0x01, 0x02, 0x03, 0x04,
8022 // byte offset
8023 kVarInt62EightBytes + 0x11, 0x22, 0x33, 0x44,
8024 0x55, 0x66, 0x77, 0x88,
8025 };
8026 // clang-format on
8027
8028 std::unique_ptr<QuicPacket> data(BuildDataPacket(header, frames));
8029 ASSERT_TRUE(data != nullptr);
8030
8031 unsigned char* p = packet;
8032 size_t p_size = QUIC_ARRAYSIZE(packet);
fkastenholz305e1732019-06-18 05:01:22 -07008033 if (VersionHasIetfQuicFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05008034 p = packet99;
8035 p_size = QUIC_ARRAYSIZE(packet99);
fayang36825da2019-08-21 14:01:27 -07008036 } else if (framer_.transport_version() >= QUIC_VERSION_46) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05008037 p = packet46;
8038 p_size = QUIC_ARRAYSIZE(packet46);
QUICHE teama6ef0a62019-03-07 20:34:33 -05008039 }
8040
8041 test::CompareCharArraysWithHexError("constructed packet", data->data(),
8042 data->length(), AsChars(p), p_size);
8043}
8044
8045TEST_P(QuicFramerTest, BuildMaxStreamDataPacket) {
fkastenholz305e1732019-06-18 05:01:22 -07008046 if (!VersionHasIetfQuicFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05008047 // This frame is available only in this version.
8048 return;
8049 }
QUICHE team2252b702019-05-14 23:55:14 -04008050 QuicFramerPeer::SetPerspective(&framer_, Perspective::IS_CLIENT);
QUICHE teama6ef0a62019-03-07 20:34:33 -05008051 QuicPacketHeader header;
8052 header.destination_connection_id = FramerTestConnectionId();
8053 header.reset_flag = false;
8054 header.version_flag = false;
8055 header.packet_number = kPacketNumber;
8056
8057 QuicWindowUpdateFrame window_update_frame;
8058 window_update_frame.stream_id = kStreamId;
8059 window_update_frame.byte_offset = 0x1122334455667788;
8060
8061 QuicFrames frames = {QuicFrame(&window_update_frame)};
8062
8063 // clang-format off
8064 unsigned char packet99[] = {
8065 // type (short header, 4 byte packet number)
8066 0x43,
8067 // connection_id
8068 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
8069 // packet number
8070 0x12, 0x34, 0x56, 0x78,
8071
8072 // frame type (IETF_MAX_STREAM_DATA frame)
8073 0x11,
8074 // stream id
8075 kVarInt62FourBytes + 0x01, 0x02, 0x03, 0x04,
8076 // byte offset
8077 kVarInt62EightBytes + 0x11, 0x22, 0x33, 0x44,
8078 0x55, 0x66, 0x77, 0x88,
8079 };
8080 // clang-format on
8081
8082 std::unique_ptr<QuicPacket> data(BuildDataPacket(header, frames));
8083 ASSERT_TRUE(data != nullptr);
8084
8085 test::CompareCharArraysWithHexError("constructed packet", data->data(),
8086 data->length(), AsChars(packet99),
8087 QUIC_ARRAYSIZE(packet99));
8088}
8089
8090TEST_P(QuicFramerTest, BuildMaxDataPacket) {
fkastenholz305e1732019-06-18 05:01:22 -07008091 if (!VersionHasIetfQuicFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05008092 // This frame is available only in this version.
8093 return;
8094 }
QUICHE team2252b702019-05-14 23:55:14 -04008095 QuicFramerPeer::SetPerspective(&framer_, Perspective::IS_CLIENT);
QUICHE teama6ef0a62019-03-07 20:34:33 -05008096 QuicPacketHeader header;
8097 header.destination_connection_id = FramerTestConnectionId();
8098 header.reset_flag = false;
8099 header.version_flag = false;
8100 header.packet_number = kPacketNumber;
8101
8102 QuicWindowUpdateFrame window_update_frame;
8103 window_update_frame.stream_id =
8104 QuicUtils::GetInvalidStreamId(framer_.transport_version());
8105 window_update_frame.byte_offset = 0x1122334455667788;
8106
8107 QuicFrames frames = {QuicFrame(&window_update_frame)};
8108
8109 // clang-format off
8110 unsigned char packet99[] = {
8111 // type (short header, 4 byte packet number)
8112 0x43,
8113 // connection_id
8114 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
8115 // packet number
8116 0x12, 0x34, 0x56, 0x78,
8117
8118 // frame type (IETF_MAX_DATA frame)
8119 0x10,
8120 // byte offset
8121 kVarInt62EightBytes + 0x11, 0x22, 0x33, 0x44,
8122 0x55, 0x66, 0x77, 0x88,
8123 };
8124 // clang-format on
8125
8126 std::unique_ptr<QuicPacket> data(BuildDataPacket(header, frames));
8127 ASSERT_TRUE(data != nullptr);
8128
8129 test::CompareCharArraysWithHexError("constructed packet", data->data(),
8130 data->length(), AsChars(packet99),
8131 QUIC_ARRAYSIZE(packet99));
8132}
8133
8134TEST_P(QuicFramerTest, BuildBlockedPacket) {
QUICHE team2252b702019-05-14 23:55:14 -04008135 QuicFramerPeer::SetPerspective(&framer_, Perspective::IS_CLIENT);
QUICHE teama6ef0a62019-03-07 20:34:33 -05008136 QuicPacketHeader header;
8137 header.destination_connection_id = FramerTestConnectionId();
8138 header.reset_flag = false;
8139 header.version_flag = false;
8140 header.packet_number = kPacketNumber;
8141
8142 QuicBlockedFrame blocked_frame;
fkastenholz305e1732019-06-18 05:01:22 -07008143 if (VersionHasIetfQuicFrames(framer_.transport_version())) {
8144 // For IETF QUIC, the stream ID must be <invalid> for the frame
QUICHE teama6ef0a62019-03-07 20:34:33 -05008145 // to be a BLOCKED frame. if it's valid, it will be a
8146 // STREAM_BLOCKED frame.
8147 blocked_frame.stream_id =
8148 QuicUtils::GetInvalidStreamId(framer_.transport_version());
8149 } else {
8150 blocked_frame.stream_id = kStreamId;
8151 }
8152 blocked_frame.offset = kStreamOffset;
8153
8154 QuicFrames frames = {QuicFrame(&blocked_frame)};
8155
8156 // clang-format off
8157 unsigned char packet[] = {
8158 // public flags (8 byte connection_id)
QUICHE team2252b702019-05-14 23:55:14 -04008159 0x2C,
QUICHE teama6ef0a62019-03-07 20:34:33 -05008160 // connection_id
8161 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
8162 // packet number
8163 0x12, 0x34, 0x56, 0x78,
8164
8165 // frame type (blocked frame)
8166 0x05,
8167 // stream id
8168 0x01, 0x02, 0x03, 0x04,
8169 };
8170
QUICHE teama6ef0a62019-03-07 20:34:33 -05008171 unsigned char packet46[] = {
8172 // type (short packet, 4 byte packet number)
8173 0x43,
8174 // connection_id
8175 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
8176 // packet number
8177 0x12, 0x34, 0x56, 0x78,
8178
8179 // frame type (blocked frame)
8180 0x05,
8181 // stream id
8182 0x01, 0x02, 0x03, 0x04,
8183 };
8184
8185 unsigned char packet99[] = {
8186 // type (short packet, 4 byte packet number)
8187 0x43,
8188 // connection_id
8189 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
8190 // packet number
8191 0x12, 0x34, 0x56, 0x78,
8192
8193 // frame type (IETF_BLOCKED frame)
8194 0x14,
8195 // Offset
8196 kVarInt62EightBytes + 0x3a, 0x98, 0xFE, 0xDC, 0x32, 0x10, 0x76, 0x54
8197 };
8198 // clang-format on
8199
8200 std::unique_ptr<QuicPacket> data(BuildDataPacket(header, frames));
8201 ASSERT_TRUE(data != nullptr);
8202
8203 unsigned char* p = packet;
8204 size_t p_size = QUIC_ARRAYSIZE(packet);
fkastenholz305e1732019-06-18 05:01:22 -07008205 if (VersionHasIetfQuicFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05008206 p = packet99;
8207 p_size = QUIC_ARRAYSIZE(packet99);
fayang36825da2019-08-21 14:01:27 -07008208 } else if (framer_.transport_version() >= QUIC_VERSION_46) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05008209 p = packet46;
8210 p_size = QUIC_ARRAYSIZE(packet46);
QUICHE teama6ef0a62019-03-07 20:34:33 -05008211 }
8212
8213 test::CompareCharArraysWithHexError("constructed packet", data->data(),
8214 data->length(), AsChars(p), p_size);
8215}
8216
8217TEST_P(QuicFramerTest, BuildPingPacket) {
QUICHE team2252b702019-05-14 23:55:14 -04008218 QuicFramerPeer::SetPerspective(&framer_, Perspective::IS_CLIENT);
QUICHE teama6ef0a62019-03-07 20:34:33 -05008219 QuicPacketHeader header;
8220 header.destination_connection_id = FramerTestConnectionId();
8221 header.reset_flag = false;
8222 header.version_flag = false;
8223 header.packet_number = kPacketNumber;
8224
8225 QuicFrames frames = {QuicFrame(QuicPingFrame())};
8226
8227 // clang-format off
8228 unsigned char packet[] = {
8229 // public flags (8 byte connection_id)
QUICHE team2252b702019-05-14 23:55:14 -04008230 0x2C,
QUICHE teama6ef0a62019-03-07 20:34:33 -05008231 // connection_id
8232 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
8233 // packet number
8234 0x12, 0x34, 0x56, 0x78,
8235
8236 // frame type (ping frame)
8237 0x07,
8238 };
8239
QUICHE teama6ef0a62019-03-07 20:34:33 -05008240 unsigned char packet46[] = {
8241 // type (short header, 4 byte packet number)
8242 0x43,
8243 // connection_id
8244 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
8245 // packet number
8246 0x12, 0x34, 0x56, 0x78,
8247
8248 // frame type
8249 0x07,
8250 };
8251
8252 unsigned char packet99[] = {
8253 // type (short header, 4 byte packet number)
8254 0x43,
8255 // connection_id
8256 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
8257 // packet number
8258 0x12, 0x34, 0x56, 0x78,
8259
8260 // frame type (IETF_PING frame)
8261 0x01,
8262 };
8263 // clang-format on
8264
8265 unsigned char* p = packet;
fkastenholz305e1732019-06-18 05:01:22 -07008266 if (VersionHasIetfQuicFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05008267 p = packet99;
fayang36825da2019-08-21 14:01:27 -07008268 } else if (framer_.transport_version() >= QUIC_VERSION_46) {
fayang374888f2019-05-31 06:47:21 -07008269 p = packet46;
QUICHE teama6ef0a62019-03-07 20:34:33 -05008270 }
8271
8272 std::unique_ptr<QuicPacket> data(BuildDataPacket(header, frames));
8273 ASSERT_TRUE(data != nullptr);
8274
8275 test::CompareCharArraysWithHexError(
8276 "constructed packet", data->data(), data->length(), AsChars(p),
fayang36825da2019-08-21 14:01:27 -07008277 framer_.transport_version() > QUIC_VERSION_43 ? QUIC_ARRAYSIZE(packet46)
QUICHE teama6ef0a62019-03-07 20:34:33 -05008278 : QUIC_ARRAYSIZE(packet));
8279}
8280
8281TEST_P(QuicFramerTest, BuildMessagePacket) {
fayang36825da2019-08-21 14:01:27 -07008282 if (framer_.transport_version() <= QUIC_VERSION_43) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05008283 return;
8284 }
QUICHE team2252b702019-05-14 23:55:14 -04008285 QuicFramerPeer::SetPerspective(&framer_, Perspective::IS_CLIENT);
QUICHE teama6ef0a62019-03-07 20:34:33 -05008286 QuicPacketHeader header;
8287 header.destination_connection_id = FramerTestConnectionId();
8288 header.reset_flag = false;
8289 header.version_flag = false;
8290 header.packet_number = kPacketNumber;
8291 QuicMemSliceStorage storage(nullptr, 0, nullptr, 0);
8292
wub553a9662019-03-28 20:13:23 -07008293 QuicMessageFrame frame(1, MakeSpan(&allocator_, "message", &storage));
8294 QuicMessageFrame frame2(2, MakeSpan(&allocator_, "message2", &storage));
QUICHE teama6ef0a62019-03-07 20:34:33 -05008295 QuicFrames frames = {QuicFrame(&frame), QuicFrame(&frame2)};
8296
8297 // clang-format off
QUICHE teama6ef0a62019-03-07 20:34:33 -05008298 unsigned char packet46[] = {
8299 // type (short header, 4 byte packet number)
8300 0x43,
8301 // connection_id
8302 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
8303 // packet number
8304 0x12, 0x34, 0x56, 0x78,
8305
8306 // frame type (message frame)
8307 0x21,
8308 // Length
8309 0x07,
8310 // Message Data
8311 'm', 'e', 's', 's', 'a', 'g', 'e',
8312 // frame type (message frame no length)
8313 0x20,
8314 // Message Data
8315 'm', 'e', 's', 's', 'a', 'g', 'e', '2'
8316 };
8317
8318 unsigned char packet99[] = {
8319 // type (short header, 4 byte packet number)
8320 0x43,
8321 // connection_id
8322 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
8323 // packet number
8324 0x12, 0x34, 0x56, 0x78,
8325
8326 // frame type (IETF_MESSAGE frame)
8327 0x21,
8328 // Length
8329 0x07,
8330 // Message Data
8331 'm', 'e', 's', 's', 'a', 'g', 'e',
8332 // frame type (message frame no length)
8333 0x20,
8334 // Message Data
8335 'm', 'e', 's', 's', 'a', 'g', 'e', '2'
8336 };
8337 // clang-format on
8338
fayang36825da2019-08-21 14:01:27 -07008339 unsigned char* p = packet46;
fkastenholz305e1732019-06-18 05:01:22 -07008340 if (VersionHasIetfQuicFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05008341 p = packet99;
QUICHE teama6ef0a62019-03-07 20:34:33 -05008342 }
8343
8344 std::unique_ptr<QuicPacket> data(BuildDataPacket(header, frames));
8345 ASSERT_TRUE(data != nullptr);
8346
8347 test::CompareCharArraysWithHexError("constructed packet", data->data(),
8348 data->length(), AsChars(p),
fayang36825da2019-08-21 14:01:27 -07008349 QUIC_ARRAYSIZE(packet46));
QUICHE teama6ef0a62019-03-07 20:34:33 -05008350}
8351
8352// Test that the connectivity probing packet is serialized correctly as a
8353// padded PING packet.
8354TEST_P(QuicFramerTest, BuildConnectivityProbingPacket) {
QUICHE team2252b702019-05-14 23:55:14 -04008355 QuicFramerPeer::SetPerspective(&framer_, Perspective::IS_CLIENT);
QUICHE teama6ef0a62019-03-07 20:34:33 -05008356 QuicPacketHeader header;
8357 header.destination_connection_id = FramerTestConnectionId();
8358 header.reset_flag = false;
8359 header.version_flag = false;
8360 header.packet_number = kPacketNumber;
8361
8362 // clang-format off
8363 unsigned char packet[] = {
8364 // public flags (8 byte connection_id)
QUICHE team2252b702019-05-14 23:55:14 -04008365 0x2C,
QUICHE teama6ef0a62019-03-07 20:34:33 -05008366 // connection_id
8367 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
8368 // packet number
8369 0x12, 0x34, 0x56, 0x78,
8370
8371 // frame type (ping frame)
8372 0x07,
8373 // frame type (padding frame)
8374 0x00,
8375 0x00, 0x00, 0x00, 0x00
8376 };
8377
QUICHE teama6ef0a62019-03-07 20:34:33 -05008378 unsigned char packet46[] = {
8379 // type (short header, 4 byte packet number)
8380 0x43,
8381 // connection_id
8382 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
8383 // packet number
8384 0x12, 0x34, 0x56, 0x78,
8385
8386 // frame type
8387 0x07,
8388 // frame type (padding frame)
8389 0x00,
8390 0x00, 0x00, 0x00, 0x00
8391 };
8392
8393 unsigned char packet99[] = {
8394 // type (short header, 4 byte packet number)
8395 0x43,
8396 // connection_id
8397 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
8398 // packet number
8399 0x12, 0x34, 0x56, 0x78,
8400
8401 // frame type (IETF_PING frame)
8402 0x01,
8403 // frame type (padding frame)
8404 0x00,
8405 0x00, 0x00, 0x00, 0x00
8406 };
8407 // clang-format on
8408
8409 unsigned char* p = packet;
8410 size_t packet_size = QUIC_ARRAYSIZE(packet);
fkastenholz305e1732019-06-18 05:01:22 -07008411 if (VersionHasIetfQuicFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05008412 p = packet99;
8413 packet_size = QUIC_ARRAYSIZE(packet99);
fayang36825da2019-08-21 14:01:27 -07008414 } else if (framer_.transport_version() >= QUIC_VERSION_46) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05008415 p = packet46;
8416 packet_size = QUIC_ARRAYSIZE(packet46);
QUICHE teama6ef0a62019-03-07 20:34:33 -05008417 }
8418
dschinazi66dea072019-04-09 11:41:06 -07008419 std::unique_ptr<char[]> buffer(new char[kMaxOutgoingPacketSize]);
QUICHE teama6ef0a62019-03-07 20:34:33 -05008420
8421 size_t length = framer_.BuildConnectivityProbingPacket(
QUICHE team6987b4a2019-03-15 16:23:04 -07008422 header, buffer.get(), packet_size, ENCRYPTION_INITIAL);
QUICHE teama6ef0a62019-03-07 20:34:33 -05008423
8424 EXPECT_NE(0u, length);
8425 QuicPacket data(framer_.transport_version(), buffer.release(), length, true,
8426 header);
8427
8428 test::CompareCharArraysWithHexError("constructed packet", data.data(),
8429 data.length(), AsChars(p), packet_size);
8430}
8431
8432// Test that the path challenge connectivity probing packet is serialized
8433// correctly as a padded PATH CHALLENGE packet.
8434TEST_P(QuicFramerTest, BuildPaddedPathChallengePacket) {
fkastenholz305e1732019-06-18 05:01:22 -07008435 if (!VersionHasIetfQuicFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05008436 return;
8437 }
8438
8439 QuicPacketHeader header;
8440 header.destination_connection_id = FramerTestConnectionId();
8441 header.reset_flag = false;
8442 header.version_flag = false;
8443 header.packet_number = kPacketNumber;
8444 QuicPathFrameBuffer payload;
8445
8446 // clang-format off
8447 unsigned char packet[] = {
8448 // type (short header, 4 byte packet number)
8449 0x43,
8450 // connection_id
8451 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
8452 // packet number
8453 0x12, 0x34, 0x56, 0x78,
8454
8455 // Path Challenge Frame type (IETF_PATH_CHALLENGE)
8456 0x1a,
8457 // 8 "random" bytes, MockRandom makes lots of r's
8458 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r',
8459 // frame type (padding frame)
8460 0x00,
8461 0x00, 0x00, 0x00, 0x00
8462 };
8463 // clang-format on
8464
dschinazi66dea072019-04-09 11:41:06 -07008465 std::unique_ptr<char[]> buffer(new char[kMaxOutgoingPacketSize]);
QUICHE teama6ef0a62019-03-07 20:34:33 -05008466 MockRandom randomizer;
8467
8468 size_t length = framer_.BuildPaddedPathChallengePacket(
8469 header, buffer.get(), QUIC_ARRAYSIZE(packet), &payload, &randomizer,
QUICHE team6987b4a2019-03-15 16:23:04 -07008470 ENCRYPTION_INITIAL);
QUICHE teama6ef0a62019-03-07 20:34:33 -05008471 EXPECT_EQ(length, QUIC_ARRAYSIZE(packet));
8472
8473 // Payload has the random bytes that were generated. Copy them into packet,
8474 // above, before checking that the generated packet is correct.
8475 EXPECT_EQ(kQuicPathFrameBufferSize, payload.size());
8476
8477 QuicPacket data(framer_.transport_version(), buffer.release(), length, true,
8478 header);
8479
8480 test::CompareCharArraysWithHexError("constructed packet", data.data(),
8481 data.length(), AsChars(packet),
8482 QUIC_ARRAYSIZE(packet));
8483}
8484
8485// Several tests that the path response connectivity probing packet is
8486// serialized correctly as either a padded and unpadded PATH RESPONSE
8487// packet. Also generates packets with 1 and 3 PATH_RESPONSES in them to
8488// exercised the single- and multiple- payload cases.
8489TEST_P(QuicFramerTest, BuildPathResponsePacket1ResponseUnpadded) {
fkastenholz305e1732019-06-18 05:01:22 -07008490 if (!VersionHasIetfQuicFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05008491 return;
8492 }
8493
8494 QuicPacketHeader header;
8495 header.destination_connection_id = FramerTestConnectionId();
8496 header.reset_flag = false;
8497 header.version_flag = false;
8498 header.packet_number = kPacketNumber;
8499 QuicPathFrameBuffer payload0 = {
8500 {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}};
8501
8502 // Build 1 PATH RESPONSE, not padded
8503 // clang-format off
8504 unsigned char packet[] = {
8505 // type (short header, 4 byte packet number)
8506 0x43,
8507 // connection_id
8508 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
8509 // packet number
8510 0x12, 0x34, 0x56, 0x78,
8511
8512 // Path Response Frame type (IETF_PATH_RESPONSE)
8513 0x1b,
8514 // 8 "random" bytes
8515 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
8516 };
8517 // clang-format on
dschinazi66dea072019-04-09 11:41:06 -07008518 std::unique_ptr<char[]> buffer(new char[kMaxOutgoingPacketSize]);
QUICHE teama6ef0a62019-03-07 20:34:33 -05008519 QuicDeque<QuicPathFrameBuffer> payloads;
8520 payloads.push_back(payload0);
8521 size_t length = framer_.BuildPathResponsePacket(
8522 header, buffer.get(), QUIC_ARRAYSIZE(packet), payloads,
QUICHE team6987b4a2019-03-15 16:23:04 -07008523 /*is_padded=*/false, ENCRYPTION_INITIAL);
QUICHE teama6ef0a62019-03-07 20:34:33 -05008524 EXPECT_EQ(length, QUIC_ARRAYSIZE(packet));
8525 QuicPacket data(framer_.transport_version(), buffer.release(), length, true,
8526 header);
8527
8528 test::CompareCharArraysWithHexError("constructed packet", data.data(),
8529 data.length(), AsChars(packet),
8530 QUIC_ARRAYSIZE(packet));
8531}
8532
8533TEST_P(QuicFramerTest, BuildPathResponsePacket1ResponsePadded) {
fkastenholz305e1732019-06-18 05:01:22 -07008534 if (!VersionHasIetfQuicFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05008535 return;
8536 }
8537
8538 QuicPacketHeader header;
8539 header.destination_connection_id = FramerTestConnectionId();
8540 header.reset_flag = false;
8541 header.version_flag = false;
8542 header.packet_number = kPacketNumber;
8543 QuicPathFrameBuffer payload0 = {
8544 {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}};
8545
8546 // Build 1 PATH RESPONSE, padded
8547 // clang-format off
8548 unsigned char packet[] = {
8549 // type (short header, 4 byte packet number)
8550 0x43,
8551 // connection_id
8552 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
8553 // packet number
8554 0x12, 0x34, 0x56, 0x78,
8555
8556 // Path Response Frame type (IETF_PATH_RESPONSE)
8557 0x1b,
8558 // 8 "random" bytes
8559 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
8560 // Padding type and pad
8561 0x00, 0x00, 0x00, 0x00, 0x00
8562 };
8563 // clang-format on
dschinazi66dea072019-04-09 11:41:06 -07008564 std::unique_ptr<char[]> buffer(new char[kMaxOutgoingPacketSize]);
QUICHE teama6ef0a62019-03-07 20:34:33 -05008565 QuicDeque<QuicPathFrameBuffer> payloads;
8566 payloads.push_back(payload0);
8567 size_t length = framer_.BuildPathResponsePacket(
8568 header, buffer.get(), QUIC_ARRAYSIZE(packet), payloads,
QUICHE team6987b4a2019-03-15 16:23:04 -07008569 /*is_padded=*/true, ENCRYPTION_INITIAL);
QUICHE teama6ef0a62019-03-07 20:34:33 -05008570 EXPECT_EQ(length, QUIC_ARRAYSIZE(packet));
8571 QuicPacket data(framer_.transport_version(), buffer.release(), length, true,
8572 header);
8573
8574 test::CompareCharArraysWithHexError("constructed packet", data.data(),
8575 data.length(), AsChars(packet),
8576 QUIC_ARRAYSIZE(packet));
8577}
8578
8579TEST_P(QuicFramerTest, BuildPathResponsePacket3ResponsesUnpadded) {
fkastenholz305e1732019-06-18 05:01:22 -07008580 if (!VersionHasIetfQuicFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05008581 return;
8582 }
8583
8584 QuicPacketHeader header;
8585 header.destination_connection_id = FramerTestConnectionId();
8586 header.reset_flag = false;
8587 header.version_flag = false;
8588 header.packet_number = kPacketNumber;
8589 QuicPathFrameBuffer payload0 = {
8590 {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}};
8591 QuicPathFrameBuffer payload1 = {
8592 {0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18}};
8593 QuicPathFrameBuffer payload2 = {
8594 {0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28}};
8595
8596 // Build one packet with 3 PATH RESPONSES, no padding
8597 // clang-format off
8598 unsigned char packet[] = {
8599 // type (short header, 4 byte packet number)
8600 0x43,
8601 // connection_id
8602 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
8603 // packet number
8604 0x12, 0x34, 0x56, 0x78,
8605
8606 // 3 path response frames (IETF_PATH_RESPONSE type byte and payload)
8607 0x1b, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
8608 0x1b, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18,
8609 0x1b, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28,
8610 };
8611 // clang-format on
8612
dschinazi66dea072019-04-09 11:41:06 -07008613 std::unique_ptr<char[]> buffer(new char[kMaxOutgoingPacketSize]);
QUICHE teama6ef0a62019-03-07 20:34:33 -05008614 QuicDeque<QuicPathFrameBuffer> payloads;
8615 payloads.push_back(payload0);
8616 payloads.push_back(payload1);
8617 payloads.push_back(payload2);
8618 size_t length = framer_.BuildPathResponsePacket(
8619 header, buffer.get(), QUIC_ARRAYSIZE(packet), payloads,
QUICHE team6987b4a2019-03-15 16:23:04 -07008620 /*is_padded=*/false, ENCRYPTION_INITIAL);
QUICHE teama6ef0a62019-03-07 20:34:33 -05008621 EXPECT_EQ(length, QUIC_ARRAYSIZE(packet));
8622 QuicPacket data(framer_.transport_version(), buffer.release(), length, true,
8623 header);
8624
8625 test::CompareCharArraysWithHexError("constructed packet", data.data(),
8626 data.length(), AsChars(packet),
8627 QUIC_ARRAYSIZE(packet));
8628}
8629
8630TEST_P(QuicFramerTest, BuildPathResponsePacket3ResponsesPadded) {
fkastenholz305e1732019-06-18 05:01:22 -07008631 if (!VersionHasIetfQuicFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05008632 return;
8633 }
8634
8635 QuicPacketHeader header;
8636 header.destination_connection_id = FramerTestConnectionId();
8637 header.reset_flag = false;
8638 header.version_flag = false;
8639 header.packet_number = kPacketNumber;
8640 QuicPathFrameBuffer payload0 = {
8641 {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}};
8642 QuicPathFrameBuffer payload1 = {
8643 {0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18}};
8644 QuicPathFrameBuffer payload2 = {
8645 {0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28}};
8646
8647 // Build one packet with 3 PATH RESPONSES, with padding
8648 // clang-format off
8649 unsigned char packet[] = {
8650 // type (short header, 4 byte packet number)
8651 0x43,
8652 // connection_id
8653 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
8654 // packet number
8655 0x12, 0x34, 0x56, 0x78,
8656
8657 // 3 path response frames (IETF_PATH_RESPONSE byte and payload)
8658 0x1b, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
8659 0x1b, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18,
8660 0x1b, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28,
8661 // Padding
8662 0x00, 0x00, 0x00, 0x00, 0x00
8663 };
8664 // clang-format on
8665
dschinazi66dea072019-04-09 11:41:06 -07008666 std::unique_ptr<char[]> buffer(new char[kMaxOutgoingPacketSize]);
QUICHE teama6ef0a62019-03-07 20:34:33 -05008667 QuicDeque<QuicPathFrameBuffer> payloads;
8668 payloads.push_back(payload0);
8669 payloads.push_back(payload1);
8670 payloads.push_back(payload2);
8671 size_t length = framer_.BuildPathResponsePacket(
8672 header, buffer.get(), QUIC_ARRAYSIZE(packet), payloads,
QUICHE team6987b4a2019-03-15 16:23:04 -07008673 /*is_padded=*/true, ENCRYPTION_INITIAL);
QUICHE teama6ef0a62019-03-07 20:34:33 -05008674 EXPECT_EQ(length, QUIC_ARRAYSIZE(packet));
8675 QuicPacket data(framer_.transport_version(), buffer.release(), length, true,
8676 header);
8677
8678 test::CompareCharArraysWithHexError("constructed packet", data.data(),
8679 data.length(), AsChars(packet),
8680 QUIC_ARRAYSIZE(packet));
8681}
8682
8683// Test that the MTU discovery packet is serialized correctly as a PING packet.
8684TEST_P(QuicFramerTest, BuildMtuDiscoveryPacket) {
QUICHE team2252b702019-05-14 23:55:14 -04008685 QuicFramerPeer::SetPerspective(&framer_, Perspective::IS_CLIENT);
QUICHE teama6ef0a62019-03-07 20:34:33 -05008686 QuicPacketHeader header;
8687 header.destination_connection_id = FramerTestConnectionId();
8688 header.reset_flag = false;
8689 header.version_flag = false;
8690 header.packet_number = kPacketNumber;
8691
8692 QuicFrames frames = {QuicFrame(QuicMtuDiscoveryFrame())};
8693
8694 // clang-format off
8695 unsigned char packet[] = {
8696 // public flags (8 byte connection_id)
QUICHE team2252b702019-05-14 23:55:14 -04008697 0x2C,
QUICHE teama6ef0a62019-03-07 20:34:33 -05008698 // connection_id
8699 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
8700 // packet number
8701 0x12, 0x34, 0x56, 0x78,
8702
8703 // frame type (ping frame)
8704 0x07,
8705 };
8706
QUICHE teama6ef0a62019-03-07 20:34:33 -05008707 unsigned char packet46[] = {
8708 // type (short header, 4 byte packet number)
8709 0x43,
8710 // connection_id
8711 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
8712 // packet number
8713 0x12, 0x34, 0x56, 0x78,
8714
8715 // frame type
8716 0x07,
8717 };
8718
8719 unsigned char packet99[] = {
8720 // type (short header, 4 byte packet number)
8721 0x43,
8722 // connection_id
8723 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
8724 // packet number
8725 0x12, 0x34, 0x56, 0x78,
8726
8727 // frame type (IETF_PING frame)
8728 0x01,
8729 };
8730 // clang-format on
8731
8732 std::unique_ptr<QuicPacket> data(BuildDataPacket(header, frames));
8733 ASSERT_TRUE(data != nullptr);
8734
8735 unsigned char* p = packet;
fkastenholz305e1732019-06-18 05:01:22 -07008736 if (VersionHasIetfQuicFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05008737 p = packet99;
fayang36825da2019-08-21 14:01:27 -07008738 } else if (framer_.transport_version() >= QUIC_VERSION_46) {
fayang374888f2019-05-31 06:47:21 -07008739 p = packet46;
QUICHE teama6ef0a62019-03-07 20:34:33 -05008740 }
8741
8742 test::CompareCharArraysWithHexError(
8743 "constructed packet", data->data(), data->length(), AsChars(p),
fayang36825da2019-08-21 14:01:27 -07008744 framer_.transport_version() > QUIC_VERSION_43 ? QUIC_ARRAYSIZE(packet46)
QUICHE teama6ef0a62019-03-07 20:34:33 -05008745 : QUIC_ARRAYSIZE(packet));
8746}
8747
8748TEST_P(QuicFramerTest, BuildPublicResetPacket) {
8749 QuicPublicResetPacket reset_packet;
8750 reset_packet.connection_id = FramerTestConnectionId();
8751 reset_packet.nonce_proof = kNonceProof;
8752
8753 // clang-format off
8754 unsigned char packet[] = {
8755 // public flags (public reset, 8 byte ConnectionId)
8756 0x0E,
8757 // connection_id
8758 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
8759 // message tag (kPRST)
8760 'P', 'R', 'S', 'T',
8761 // num_entries (1) + padding
8762 0x01, 0x00, 0x00, 0x00,
8763 // tag kRNON
8764 'R', 'N', 'O', 'N',
8765 // end offset 8
8766 0x08, 0x00, 0x00, 0x00,
8767 // nonce proof
8768 0x89, 0x67, 0x45, 0x23,
8769 0x01, 0xEF, 0xCD, 0xAB,
8770 };
8771 // clang-format on
8772
8773 if (framer_.transport_version() > QUIC_VERSION_43) {
8774 return;
8775 }
8776
8777 std::unique_ptr<QuicEncryptedPacket> data(
8778 framer_.BuildPublicResetPacket(reset_packet));
8779 ASSERT_TRUE(data != nullptr);
8780 test::CompareCharArraysWithHexError("constructed packet", data->data(),
8781 data->length(), AsChars(packet),
8782 QUIC_ARRAYSIZE(packet));
8783}
8784
8785TEST_P(QuicFramerTest, BuildPublicResetPacketWithClientAddress) {
8786 QuicPublicResetPacket reset_packet;
8787 reset_packet.connection_id = FramerTestConnectionId();
8788 reset_packet.nonce_proof = kNonceProof;
8789 reset_packet.client_address =
8790 QuicSocketAddress(QuicIpAddress::Loopback4(), 0x1234);
8791
8792 // clang-format off
8793 unsigned char packet[] = {
8794 // public flags (public reset, 8 byte ConnectionId)
8795 0x0E,
8796 // connection_id
8797 0xFE, 0xDC, 0xBA, 0x98,
8798 0x76, 0x54, 0x32, 0x10,
8799 // message tag (kPRST)
8800 'P', 'R', 'S', 'T',
8801 // num_entries (2) + padding
8802 0x02, 0x00, 0x00, 0x00,
8803 // tag kRNON
8804 'R', 'N', 'O', 'N',
8805 // end offset 8
8806 0x08, 0x00, 0x00, 0x00,
8807 // tag kCADR
8808 'C', 'A', 'D', 'R',
8809 // end offset 16
8810 0x10, 0x00, 0x00, 0x00,
8811 // nonce proof
8812 0x89, 0x67, 0x45, 0x23,
8813 0x01, 0xEF, 0xCD, 0xAB,
8814 // client address
8815 0x02, 0x00,
8816 0x7F, 0x00, 0x00, 0x01,
8817 0x34, 0x12,
8818 };
8819 // clang-format on
8820
8821 if (framer_.transport_version() > QUIC_VERSION_43) {
8822 return;
8823 }
8824
8825 std::unique_ptr<QuicEncryptedPacket> data(
8826 framer_.BuildPublicResetPacket(reset_packet));
8827 ASSERT_TRUE(data != nullptr);
8828
8829 test::CompareCharArraysWithHexError("constructed packet", data->data(),
8830 data->length(), AsChars(packet),
8831 QUIC_ARRAYSIZE(packet));
8832}
8833
8834TEST_P(QuicFramerTest, BuildPublicResetPacketWithEndpointId) {
8835 QuicPublicResetPacket reset_packet;
8836 reset_packet.connection_id = FramerTestConnectionId();
8837 reset_packet.nonce_proof = kNonceProof;
8838 reset_packet.endpoint_id = "FakeServerId";
8839
8840 // The tag value map in CryptoHandshakeMessage is a std::map, so the two tags
8841 // in the packet, kRNON and kEPID, have unspecified ordering w.r.t each other.
8842 // clang-format off
8843 unsigned char packet_variant1[] = {
8844 // public flags (public reset, 8 byte ConnectionId)
8845 0x0E,
8846 // connection_id
8847 0xFE, 0xDC, 0xBA, 0x98,
8848 0x76, 0x54, 0x32, 0x10,
8849 // message tag (kPRST)
8850 'P', 'R', 'S', 'T',
8851 // num_entries (2) + padding
8852 0x02, 0x00, 0x00, 0x00,
8853 // tag kRNON
8854 'R', 'N', 'O', 'N',
8855 // end offset 8
8856 0x08, 0x00, 0x00, 0x00,
8857 // tag kEPID
8858 'E', 'P', 'I', 'D',
8859 // end offset 20
8860 0x14, 0x00, 0x00, 0x00,
8861 // nonce proof
8862 0x89, 0x67, 0x45, 0x23,
8863 0x01, 0xEF, 0xCD, 0xAB,
8864 // Endpoint ID
8865 'F', 'a', 'k', 'e', 'S', 'e', 'r', 'v', 'e', 'r', 'I', 'd',
8866 };
8867 unsigned char packet_variant2[] = {
8868 // public flags (public reset, 8 byte ConnectionId)
8869 0x0E,
8870 // connection_id
8871 0xFE, 0xDC, 0xBA, 0x98,
8872 0x76, 0x54, 0x32, 0x10,
8873 // message tag (kPRST)
8874 'P', 'R', 'S', 'T',
8875 // num_entries (2) + padding
8876 0x02, 0x00, 0x00, 0x00,
8877 // tag kEPID
8878 'E', 'P', 'I', 'D',
8879 // end offset 12
8880 0x0C, 0x00, 0x00, 0x00,
8881 // tag kRNON
8882 'R', 'N', 'O', 'N',
8883 // end offset 20
8884 0x14, 0x00, 0x00, 0x00,
8885 // Endpoint ID
8886 'F', 'a', 'k', 'e', 'S', 'e', 'r', 'v', 'e', 'r', 'I', 'd',
8887 // nonce proof
8888 0x89, 0x67, 0x45, 0x23,
8889 0x01, 0xEF, 0xCD, 0xAB,
8890 };
8891 // clang-format on
8892
8893 if (framer_.transport_version() > QUIC_VERSION_43) {
8894 return;
8895 }
8896
8897 std::unique_ptr<QuicEncryptedPacket> data(
8898 framer_.BuildPublicResetPacket(reset_packet));
8899 ASSERT_TRUE(data != nullptr);
8900
8901 // Variant 1 ends with char 'd'. Variant 1 ends with char 0xAB.
8902 if ('d' == data->data()[data->length() - 1]) {
8903 test::CompareCharArraysWithHexError(
8904 "constructed packet", data->data(), data->length(),
8905 AsChars(packet_variant1), QUIC_ARRAYSIZE(packet_variant1));
8906 } else {
8907 test::CompareCharArraysWithHexError(
8908 "constructed packet", data->data(), data->length(),
8909 AsChars(packet_variant2), QUIC_ARRAYSIZE(packet_variant2));
8910 }
8911}
8912
8913TEST_P(QuicFramerTest, BuildIetfStatelessResetPacket) {
8914 // clang-format off
fayang36825da2019-08-21 14:01:27 -07008915 unsigned char packet[] = {
QUICHE teama6ef0a62019-03-07 20:34:33 -05008916 // type (short header, 1 byte packet number)
8917 0x70,
8918 // random packet number
8919 0xFE,
8920 // stateless reset token
8921 0xB5, 0x69, 0x0F, 0x00, 0x00, 0x00, 0x00, 0x00,
8922 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
8923 };
8924 // clang-format on
8925
8926 std::unique_ptr<QuicEncryptedPacket> data(
8927 framer_.BuildIetfStatelessResetPacket(FramerTestConnectionId(),
8928 kTestStatelessResetToken));
8929 ASSERT_TRUE(data != nullptr);
8930 // Skip packet number byte which is random in stateless reset packet.
8931 test::CompareCharArraysWithHexError("constructed packet", data->data(), 1,
fayang36825da2019-08-21 14:01:27 -07008932 AsChars(packet), 1);
QUICHE teama6ef0a62019-03-07 20:34:33 -05008933 const size_t random_bytes_length =
8934 data->length() - kPacketHeaderTypeSize - sizeof(kTestStatelessResetToken);
8935 EXPECT_EQ(kMinRandomBytesLengthInStatelessReset, random_bytes_length);
8936 // Verify stateless reset token is correct.
8937 test::CompareCharArraysWithHexError(
8938 "constructed packet",
8939 data->data() + data->length() - sizeof(kTestStatelessResetToken),
8940 sizeof(kTestStatelessResetToken),
fayang36825da2019-08-21 14:01:27 -07008941 AsChars(packet) + QUIC_ARRAYSIZE(packet) -
QUICHE teama6ef0a62019-03-07 20:34:33 -05008942 sizeof(kTestStatelessResetToken),
8943 sizeof(kTestStatelessResetToken));
8944}
8945
8946TEST_P(QuicFramerTest, EncryptPacket) {
8947 QuicPacketNumber packet_number = kPacketNumber;
8948 // clang-format off
8949 unsigned char packet[] = {
8950 // public flags (8 byte connection_id)
8951 0x28,
8952 // connection_id
8953 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
8954 // packet number
8955 0x12, 0x34, 0x56, 0x78,
8956
8957 // redundancy
8958 'a', 'b', 'c', 'd',
8959 'e', 'f', 'g', 'h',
8960 'i', 'j', 'k', 'l',
8961 'm', 'n', 'o', 'p',
8962 };
8963
QUICHE teama6ef0a62019-03-07 20:34:33 -05008964 unsigned char packet46[] = {
8965 // type (short header, 4 byte packet number)
8966 0x43,
8967 // connection_id
8968 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
8969 // packet number
8970 0x12, 0x34, 0x56, 0x78,
8971
8972 // redundancy
8973 'a', 'b', 'c', 'd',
8974 'e', 'f', 'g', 'h',
8975 'i', 'j', 'k', 'l',
8976 'm', 'n', 'o', 'p',
8977 };
8978
8979 unsigned char packet99[] = {
8980 // type (short header, 4 byte packet number)
8981 0x43,
8982 // connection_id
8983 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
8984 // packet number
8985 0x12, 0x34, 0x56, 0x78,
8986
8987 // redundancy
8988 'a', 'b', 'c', 'd',
8989 'e', 'f', 'g', 'h',
8990 'i', 'j', 'k', 'l',
8991 'm', 'n', 'o', 'p',
nharper55fa6132019-05-07 19:37:21 -07008992 'q', 'r', 's', 't',
QUICHE teama6ef0a62019-03-07 20:34:33 -05008993 };
8994 // clang-format on
8995
8996 unsigned char* p = packet;
nharper55fa6132019-05-07 19:37:21 -07008997 size_t p_size = QUIC_ARRAYSIZE(packet);
QUICHE teama6ef0a62019-03-07 20:34:33 -05008998 if (framer_.transport_version() == QUIC_VERSION_99) {
8999 p = packet99;
nharper55fa6132019-05-07 19:37:21 -07009000 p_size = QUIC_ARRAYSIZE(packet99);
fayang36825da2019-08-21 14:01:27 -07009001 } else if (framer_.transport_version() >= QUIC_VERSION_46) {
fayang374888f2019-05-31 06:47:21 -07009002 p = packet46;
QUICHE teama6ef0a62019-03-07 20:34:33 -05009003 }
9004
9005 std::unique_ptr<QuicPacket> raw(new QuicPacket(
nharper55fa6132019-05-07 19:37:21 -07009006 AsChars(p), p_size, false, PACKET_8BYTE_CONNECTION_ID,
QUICHE teama6ef0a62019-03-07 20:34:33 -05009007 PACKET_0BYTE_CONNECTION_ID, !kIncludeVersion,
9008 !kIncludeDiversificationNonce, PACKET_4BYTE_PACKET_NUMBER,
9009 VARIABLE_LENGTH_INTEGER_LENGTH_0, 0, VARIABLE_LENGTH_INTEGER_LENGTH_0));
dschinazi66dea072019-04-09 11:41:06 -07009010 char buffer[kMaxOutgoingPacketSize];
QUICHE teama6ef0a62019-03-07 20:34:33 -05009011 size_t encrypted_length = framer_.EncryptPayload(
dschinazi66dea072019-04-09 11:41:06 -07009012 ENCRYPTION_INITIAL, packet_number, *raw, buffer, kMaxOutgoingPacketSize);
QUICHE teama6ef0a62019-03-07 20:34:33 -05009013
9014 ASSERT_NE(0u, encrypted_length);
9015 EXPECT_TRUE(CheckEncryption(packet_number, raw.get()));
9016}
9017
9018TEST_P(QuicFramerTest, EncryptPacketWithVersionFlag) {
nharper55fa6132019-05-07 19:37:21 -07009019 QuicFramerPeer::SetPerspective(&framer_, Perspective::IS_CLIENT);
QUICHE teama6ef0a62019-03-07 20:34:33 -05009020 QuicPacketNumber packet_number = kPacketNumber;
9021 // clang-format off
9022 unsigned char packet[] = {
9023 // public flags (version, 8 byte connection_id)
9024 0x29,
9025 // connection_id
9026 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
9027 // version tag
9028 'Q', '.', '1', '0',
9029 // packet number
9030 0x12, 0x34, 0x56, 0x78,
9031
9032 // redundancy
9033 'a', 'b', 'c', 'd',
9034 'e', 'f', 'g', 'h',
9035 'i', 'j', 'k', 'l',
9036 'm', 'n', 'o', 'p',
9037 };
9038
QUICHE teama6ef0a62019-03-07 20:34:33 -05009039 unsigned char packet46[] = {
9040 // type (long header with packet type ZERO_RTT_PROTECTED)
9041 0xD3,
9042 // version tag
9043 'Q', '.', '1', '0',
9044 // connection_id length
9045 0x50,
9046 // connection_id
9047 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
9048 // packet number
9049 0x12, 0x34, 0x56, 0x78,
9050
9051 // redundancy
9052 'a', 'b', 'c', 'd',
9053 'e', 'f', 'g', 'h',
9054 'i', 'j', 'k', 'l',
9055 'm', 'n', 'o', 'p',
9056 };
9057
9058 unsigned char packet99[] = {
9059 // type (long header with packet type ZERO_RTT_PROTECTED)
9060 0xD3,
9061 // version tag
9062 'Q', '.', '1', '0',
dschinazi48ac9192019-07-31 00:07:26 -07009063 // destination connection ID length
9064 0x08,
9065 // destination connection ID
QUICHE teama6ef0a62019-03-07 20:34:33 -05009066 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
dschinazi48ac9192019-07-31 00:07:26 -07009067 // source connection ID length
9068 0x00,
QUICHE teama6ef0a62019-03-07 20:34:33 -05009069 // packet number
9070 0x12, 0x34, 0x56, 0x78,
9071
9072 // redundancy
9073 'a', 'b', 'c', 'd',
9074 'e', 'f', 'g', 'h',
9075 'i', 'j', 'k', 'l',
9076 'm', 'n', 'o', 'p',
nharper55fa6132019-05-07 19:37:21 -07009077 'q', 'r', 's', 't',
QUICHE teama6ef0a62019-03-07 20:34:33 -05009078 };
9079 // clang-format on
9080
9081 unsigned char* p = packet;
nharper55fa6132019-05-07 19:37:21 -07009082 size_t p_size = QUIC_ARRAYSIZE(packet);
fkastenholz305e1732019-06-18 05:01:22 -07009083 // TODO(ianswett): see todo in previous test.
QUICHE teama6ef0a62019-03-07 20:34:33 -05009084 if (framer_.transport_version() == QUIC_VERSION_99) {
9085 p = packet99;
nharper55fa6132019-05-07 19:37:21 -07009086 p_size = QUIC_ARRAYSIZE(packet99);
fayang36825da2019-08-21 14:01:27 -07009087 } else if (framer_.transport_version() >= QUIC_VERSION_46) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05009088 p = packet46;
nharper55fa6132019-05-07 19:37:21 -07009089 p_size = QUIC_ARRAYSIZE(packet46);
QUICHE teama6ef0a62019-03-07 20:34:33 -05009090 }
9091
9092 std::unique_ptr<QuicPacket> raw(new QuicPacket(
nharper55fa6132019-05-07 19:37:21 -07009093 AsChars(p), p_size, false, PACKET_8BYTE_CONNECTION_ID,
9094 PACKET_0BYTE_CONNECTION_ID, kIncludeVersion,
9095 !kIncludeDiversificationNonce, PACKET_4BYTE_PACKET_NUMBER,
9096 VARIABLE_LENGTH_INTEGER_LENGTH_0, 0, VARIABLE_LENGTH_INTEGER_LENGTH_0));
dschinazi66dea072019-04-09 11:41:06 -07009097 char buffer[kMaxOutgoingPacketSize];
QUICHE teama6ef0a62019-03-07 20:34:33 -05009098 size_t encrypted_length = framer_.EncryptPayload(
dschinazi66dea072019-04-09 11:41:06 -07009099 ENCRYPTION_INITIAL, packet_number, *raw, buffer, kMaxOutgoingPacketSize);
QUICHE teama6ef0a62019-03-07 20:34:33 -05009100
9101 ASSERT_NE(0u, encrypted_length);
9102 EXPECT_TRUE(CheckEncryption(packet_number, raw.get()));
9103}
9104
9105TEST_P(QuicFramerTest, AckTruncationLargePacket) {
fkastenholz305e1732019-06-18 05:01:22 -07009106 if (VersionHasIetfQuicFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05009107 // This test is not applicable to this version; the range count is
9108 // effectively unlimited
9109 return;
9110 }
nharper9bb83462019-05-01 10:53:22 -07009111 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
QUICHE teama6ef0a62019-03-07 20:34:33 -05009112
9113 QuicPacketHeader header;
9114 header.destination_connection_id = FramerTestConnectionId();
9115 header.reset_flag = false;
9116 header.version_flag = false;
9117 header.packet_number = kPacketNumber;
9118
9119 QuicAckFrame ack_frame;
9120 // Create a packet with just the ack.
9121 ack_frame = MakeAckFrameWithAckBlocks(300, 0u);
9122 QuicFrames frames = {QuicFrame(&ack_frame)};
9123
9124 // Build an ack packet with truncation due to limit in number of nack ranges.
9125 QuicFramerPeer::SetPerspective(&framer_, Perspective::IS_CLIENT);
9126 std::unique_ptr<QuicPacket> raw_ack_packet(BuildDataPacket(header, frames));
9127 ASSERT_TRUE(raw_ack_packet != nullptr);
dschinazi66dea072019-04-09 11:41:06 -07009128 char buffer[kMaxOutgoingPacketSize];
QUICHE teama6ef0a62019-03-07 20:34:33 -05009129 size_t encrypted_length =
QUICHE team6987b4a2019-03-15 16:23:04 -07009130 framer_.EncryptPayload(ENCRYPTION_INITIAL, header.packet_number,
dschinazi66dea072019-04-09 11:41:06 -07009131 *raw_ack_packet, buffer, kMaxOutgoingPacketSize);
QUICHE teama6ef0a62019-03-07 20:34:33 -05009132 ASSERT_NE(0u, encrypted_length);
9133 // Now make sure we can turn our ack packet back into an ack frame.
9134 QuicFramerPeer::SetPerspective(&framer_, Perspective::IS_SERVER);
9135 ASSERT_TRUE(framer_.ProcessPacket(
9136 QuicEncryptedPacket(buffer, encrypted_length, false)));
9137 ASSERT_EQ(1u, visitor_.ack_frames_.size());
9138 QuicAckFrame& processed_ack_frame = *visitor_.ack_frames_[0];
9139 EXPECT_EQ(QuicPacketNumber(600u), LargestAcked(processed_ack_frame));
9140 ASSERT_EQ(256u, processed_ack_frame.packets.NumPacketsSlow());
9141 EXPECT_EQ(QuicPacketNumber(90u), processed_ack_frame.packets.Min());
9142 EXPECT_EQ(QuicPacketNumber(600u), processed_ack_frame.packets.Max());
9143}
9144
9145TEST_P(QuicFramerTest, AckTruncationSmallPacket) {
fkastenholz305e1732019-06-18 05:01:22 -07009146 if (VersionHasIetfQuicFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05009147 // This test is not applicable to this version; the range count is
9148 // effectively unlimited
9149 return;
9150 }
nharper9bb83462019-05-01 10:53:22 -07009151 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
QUICHE teama6ef0a62019-03-07 20:34:33 -05009152
9153 QuicPacketHeader header;
9154 header.destination_connection_id = FramerTestConnectionId();
9155 header.reset_flag = false;
9156 header.version_flag = false;
9157 header.packet_number = kPacketNumber;
9158
9159 // Create a packet with just the ack.
9160 QuicAckFrame ack_frame;
9161 ack_frame = MakeAckFrameWithAckBlocks(300, 0u);
9162 QuicFrames frames = {QuicFrame(&ack_frame)};
9163
9164 // Build an ack packet with truncation due to limit in number of nack ranges.
9165 QuicFramerPeer::SetPerspective(&framer_, Perspective::IS_CLIENT);
9166 std::unique_ptr<QuicPacket> raw_ack_packet(
9167 BuildDataPacket(header, frames, 500));
9168 ASSERT_TRUE(raw_ack_packet != nullptr);
dschinazi66dea072019-04-09 11:41:06 -07009169 char buffer[kMaxOutgoingPacketSize];
QUICHE teama6ef0a62019-03-07 20:34:33 -05009170 size_t encrypted_length =
QUICHE team6987b4a2019-03-15 16:23:04 -07009171 framer_.EncryptPayload(ENCRYPTION_INITIAL, header.packet_number,
dschinazi66dea072019-04-09 11:41:06 -07009172 *raw_ack_packet, buffer, kMaxOutgoingPacketSize);
QUICHE teama6ef0a62019-03-07 20:34:33 -05009173 ASSERT_NE(0u, encrypted_length);
9174 // Now make sure we can turn our ack packet back into an ack frame.
9175 QuicFramerPeer::SetPerspective(&framer_, Perspective::IS_SERVER);
9176 ASSERT_TRUE(framer_.ProcessPacket(
9177 QuicEncryptedPacket(buffer, encrypted_length, false)));
9178 ASSERT_EQ(1u, visitor_.ack_frames_.size());
9179 QuicAckFrame& processed_ack_frame = *visitor_.ack_frames_[0];
9180 EXPECT_EQ(QuicPacketNumber(600u), LargestAcked(processed_ack_frame));
9181 ASSERT_EQ(240u, processed_ack_frame.packets.NumPacketsSlow());
9182 EXPECT_EQ(QuicPacketNumber(122u), processed_ack_frame.packets.Min());
9183 EXPECT_EQ(QuicPacketNumber(600u), processed_ack_frame.packets.Max());
9184}
9185
9186TEST_P(QuicFramerTest, CleanTruncation) {
fkastenholz305e1732019-06-18 05:01:22 -07009187 if (VersionHasIetfQuicFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05009188 // This test is not applicable to this version; the range count is
9189 // effectively unlimited
9190 return;
9191 }
nharper9bb83462019-05-01 10:53:22 -07009192 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
QUICHE teama6ef0a62019-03-07 20:34:33 -05009193
9194 QuicPacketHeader header;
9195 header.destination_connection_id = FramerTestConnectionId();
9196 header.reset_flag = false;
9197 header.version_flag = false;
9198 header.packet_number = kPacketNumber;
9199
9200 QuicAckFrame ack_frame = InitAckFrame(201);
9201
9202 // Create a packet with just the ack.
9203 QuicFrames frames = {QuicFrame(&ack_frame)};
9204 QuicFramerPeer::SetPerspective(&framer_, Perspective::IS_CLIENT);
9205 std::unique_ptr<QuicPacket> raw_ack_packet(BuildDataPacket(header, frames));
9206 ASSERT_TRUE(raw_ack_packet != nullptr);
9207
dschinazi66dea072019-04-09 11:41:06 -07009208 char buffer[kMaxOutgoingPacketSize];
QUICHE teama6ef0a62019-03-07 20:34:33 -05009209 size_t encrypted_length =
QUICHE team6987b4a2019-03-15 16:23:04 -07009210 framer_.EncryptPayload(ENCRYPTION_INITIAL, header.packet_number,
dschinazi66dea072019-04-09 11:41:06 -07009211 *raw_ack_packet, buffer, kMaxOutgoingPacketSize);
QUICHE teama6ef0a62019-03-07 20:34:33 -05009212 ASSERT_NE(0u, encrypted_length);
9213
9214 // Now make sure we can turn our ack packet back into an ack frame.
9215 QuicFramerPeer::SetPerspective(&framer_, Perspective::IS_SERVER);
9216 ASSERT_TRUE(framer_.ProcessPacket(
9217 QuicEncryptedPacket(buffer, encrypted_length, false)));
9218
9219 // Test for clean truncation of the ack by comparing the length of the
9220 // original packets to the re-serialized packets.
9221 frames.clear();
9222 frames.push_back(QuicFrame(visitor_.ack_frames_[0].get()));
9223
9224 size_t original_raw_length = raw_ack_packet->length();
9225 QuicFramerPeer::SetPerspective(&framer_, Perspective::IS_CLIENT);
9226 raw_ack_packet = BuildDataPacket(header, frames);
9227 ASSERT_TRUE(raw_ack_packet != nullptr);
9228 EXPECT_EQ(original_raw_length, raw_ack_packet->length());
9229 ASSERT_TRUE(raw_ack_packet != nullptr);
9230}
9231
9232TEST_P(QuicFramerTest, StopPacketProcessing) {
zhongyi546cc452019-04-12 15:27:49 -07009233 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
QUICHE teama6ef0a62019-03-07 20:34:33 -05009234 // clang-format off
9235 unsigned char packet[] = {
9236 // public flags (8 byte connection_id)
9237 0x28,
9238 // connection_id
9239 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
9240 // packet number
9241 0x12, 0x34, 0x56, 0x78,
9242
9243 // frame type (stream frame with fin)
9244 0xFF,
9245 // stream id
9246 0x01, 0x02, 0x03, 0x04,
9247 // offset
9248 0x3A, 0x98, 0xFE, 0xDC,
9249 0x32, 0x10, 0x76, 0x54,
9250 // data length
9251 0x00, 0x0c,
9252 // data
9253 'h', 'e', 'l', 'l',
9254 'o', ' ', 'w', 'o',
9255 'r', 'l', 'd', '!',
9256
9257 // frame type (ack frame)
9258 0x40,
9259 // least packet number awaiting an ack
9260 0x12, 0x34, 0x56, 0x78,
9261 0x9A, 0xA0,
9262 // largest observed packet number
9263 0x12, 0x34, 0x56, 0x78,
9264 0x9A, 0xBF,
9265 // num missing packets
9266 0x01,
9267 // missing packet
9268 0x12, 0x34, 0x56, 0x78,
9269 0x9A, 0xBE,
9270 };
9271
QUICHE teama6ef0a62019-03-07 20:34:33 -05009272 unsigned char packet46[] = {
9273 // type (short header, 4 byte packet number)
9274 0x43,
9275 // connection_id
9276 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
9277 // packet number
9278 0x12, 0x34, 0x56, 0x78,
9279
9280 // frame type (stream frame with fin)
9281 0xFF,
9282 // stream id
9283 0x01, 0x02, 0x03, 0x04,
9284 // offset
9285 0x3A, 0x98, 0xFE, 0xDC,
9286 0x32, 0x10, 0x76, 0x54,
9287 // data length
9288 0x00, 0x0c,
9289 // data
9290 'h', 'e', 'l', 'l',
9291 'o', ' ', 'w', 'o',
9292 'r', 'l', 'd', '!',
9293
9294 // frame type (ack frame)
9295 0x40,
9296 // least packet number awaiting an ack
9297 0x12, 0x34, 0x56, 0x78,
9298 0x9A, 0xA0,
9299 // largest observed packet number
9300 0x12, 0x34, 0x56, 0x78,
9301 0x9A, 0xBF,
9302 // num missing packets
9303 0x01,
9304 // missing packet
9305 0x12, 0x34, 0x56, 0x78,
9306 0x9A, 0xBE,
9307 };
9308
9309 unsigned char packet99[] = {
9310 // type (short header, 4 byte packet number)
9311 0x43,
9312 // connection_id
9313 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
9314 // packet number
9315 0x12, 0x34, 0x56, 0x78,
9316
9317 // frame type (IETF_STREAM frame with fin, length, and offset bits set)
9318 0x08 | 0x01 | 0x02 | 0x04,
9319 // stream id
9320 kVarInt62FourBytes + 0x01, 0x02, 0x03, 0x04,
9321 // offset
9322 kVarInt62EightBytes + 0x3A, 0x98, 0xFE, 0xDC,
9323 0x32, 0x10, 0x76, 0x54,
9324 // data length
9325 kVarInt62TwoBytes + 0x00, 0x0c,
9326 // data
9327 'h', 'e', 'l', 'l',
9328 'o', ' ', 'w', 'o',
9329 'r', 'l', 'd', '!',
9330
9331 // frame type (ack frame)
9332 0x0d,
9333 // largest observed packet number
9334 kVarInt62FourBytes + 0x12, 0x34, 0x56, 0x78,
9335 // Delta time
9336 kVarInt62OneByte + 0x00,
9337 // Ack Block count
9338 kVarInt62OneByte + 0x01,
9339 // First block size (one packet)
9340 kVarInt62OneByte + 0x00,
9341
9342 // Next gap size & ack. Missing all preceding packets
9343 kVarInt62FourBytes + 0x12, 0x34, 0x56, 0x77,
9344 kVarInt62OneByte + 0x00,
9345 };
9346 // clang-format on
9347
9348 MockFramerVisitor visitor;
9349 framer_.set_visitor(&visitor);
9350 EXPECT_CALL(visitor, OnPacket());
9351 EXPECT_CALL(visitor, OnPacketHeader(_));
9352 EXPECT_CALL(visitor, OnStreamFrame(_)).WillOnce(Return(false));
9353 EXPECT_CALL(visitor, OnPacketComplete());
9354 EXPECT_CALL(visitor, OnUnauthenticatedPublicHeader(_)).WillOnce(Return(true));
9355 EXPECT_CALL(visitor, OnUnauthenticatedHeader(_)).WillOnce(Return(true));
9356 EXPECT_CALL(visitor, OnDecryptedPacket(_));
9357
9358 unsigned char* p = packet;
9359 size_t p_size = QUIC_ARRAYSIZE(packet);
fkastenholz305e1732019-06-18 05:01:22 -07009360 if (VersionHasIetfQuicFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05009361 p = packet99;
9362 p_size = QUIC_ARRAYSIZE(packet99);
fayang36825da2019-08-21 14:01:27 -07009363 } else if (framer_.transport_version() >= QUIC_VERSION_46) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05009364 p = packet46;
9365 p_size = QUIC_ARRAYSIZE(packet46);
QUICHE teama6ef0a62019-03-07 20:34:33 -05009366 }
9367 QuicEncryptedPacket encrypted(AsChars(p), p_size, false);
9368 EXPECT_TRUE(framer_.ProcessPacket(encrypted));
9369 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
9370}
9371
9372static char kTestString[] = "At least 20 characters.";
9373static QuicStreamId kTestQuicStreamId = 1;
9374static bool ExpectedStreamFrame(const QuicStreamFrame& frame) {
9375 return (frame.stream_id == kTestQuicStreamId ||
nharper46833c32019-05-15 21:33:05 -07009376 QuicUtils::IsCryptoStreamId(QUIC_VERSION_99, frame.stream_id)) &&
QUICHE teama6ef0a62019-03-07 20:34:33 -05009377 !frame.fin && frame.offset == 0 &&
vasilvvc48c8712019-03-11 13:38:16 -07009378 std::string(frame.data_buffer, frame.data_length) == kTestString;
QUICHE teama6ef0a62019-03-07 20:34:33 -05009379 // FIN is hard-coded false in ConstructEncryptedPacket.
9380 // Offset 0 is hard-coded in ConstructEncryptedPacket.
9381}
9382
9383// Verify that the packet returned by ConstructEncryptedPacket() can be properly
9384// parsed by the framer.
9385TEST_P(QuicFramerTest, ConstructEncryptedPacket) {
9386 // Since we are using ConstructEncryptedPacket, we have to set the framer's
9387 // crypto to be Null.
zhongyi546cc452019-04-12 15:27:49 -07009388 if (framer_.version().KnowsWhichDecrypterToUse()) {
9389 framer_.InstallDecrypter(
9390 ENCRYPTION_FORWARD_SECURE,
vasilvv0fc587f2019-09-06 13:33:08 -07009391 std::make_unique<NullDecrypter>(framer_.perspective()));
zhongyi546cc452019-04-12 15:27:49 -07009392 } else {
vasilvv0fc587f2019-09-06 13:33:08 -07009393 framer_.SetDecrypter(ENCRYPTION_INITIAL, std::make_unique<NullDecrypter>(
9394 framer_.perspective()));
zhongyi546cc452019-04-12 15:27:49 -07009395 }
QUICHE teama6ef0a62019-03-07 20:34:33 -05009396 ParsedQuicVersionVector versions;
9397 versions.push_back(framer_.version());
9398 std::unique_ptr<QuicEncryptedPacket> packet(ConstructEncryptedPacket(
9399 TestConnectionId(), EmptyQuicConnectionId(), false, false,
9400 kTestQuicStreamId, kTestString, CONNECTION_ID_PRESENT,
9401 CONNECTION_ID_ABSENT, PACKET_4BYTE_PACKET_NUMBER, &versions));
9402
9403 MockFramerVisitor visitor;
9404 framer_.set_visitor(&visitor);
9405 EXPECT_CALL(visitor, OnPacket()).Times(1);
9406 EXPECT_CALL(visitor, OnUnauthenticatedPublicHeader(_))
9407 .Times(1)
9408 .WillOnce(Return(true));
9409 EXPECT_CALL(visitor, OnUnauthenticatedHeader(_))
9410 .Times(1)
9411 .WillOnce(Return(true));
9412 EXPECT_CALL(visitor, OnPacketHeader(_)).Times(1).WillOnce(Return(true));
9413 EXPECT_CALL(visitor, OnDecryptedPacket(_)).Times(1);
9414 EXPECT_CALL(visitor, OnError(_)).Times(0);
9415 EXPECT_CALL(visitor, OnStreamFrame(_)).Times(0);
QUICHE teamea740082019-03-11 17:58:43 -07009416 if (!QuicVersionUsesCryptoFrames(framer_.version().transport_version)) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05009417 EXPECT_CALL(visitor, OnStreamFrame(Truly(ExpectedStreamFrame))).Times(1);
9418 } else {
9419 EXPECT_CALL(visitor, OnCryptoFrame(_)).Times(1);
9420 }
9421 EXPECT_CALL(visitor, OnPacketComplete()).Times(1);
9422
9423 EXPECT_TRUE(framer_.ProcessPacket(*packet));
9424 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
9425}
9426
9427// Verify that the packet returned by ConstructMisFramedEncryptedPacket()
9428// does cause the framer to return an error.
9429TEST_P(QuicFramerTest, ConstructMisFramedEncryptedPacket) {
9430 // Since we are using ConstructEncryptedPacket, we have to set the framer's
9431 // crypto to be Null.
zhongyi546cc452019-04-12 15:27:49 -07009432 if (framer_.version().KnowsWhichDecrypterToUse()) {
nharper9bb83462019-05-01 10:53:22 -07009433 framer_.InstallDecrypter(
9434 ENCRYPTION_FORWARD_SECURE,
vasilvv0fc587f2019-09-06 13:33:08 -07009435 std::make_unique<NullDecrypter>(framer_.perspective()));
zhongyi546cc452019-04-12 15:27:49 -07009436 } else {
vasilvv0fc587f2019-09-06 13:33:08 -07009437 framer_.SetDecrypter(ENCRYPTION_INITIAL, std::make_unique<NullDecrypter>(
9438 framer_.perspective()));
zhongyi546cc452019-04-12 15:27:49 -07009439 }
QUICHE team6987b4a2019-03-15 16:23:04 -07009440 framer_.SetEncrypter(ENCRYPTION_INITIAL,
vasilvv0fc587f2019-09-06 13:33:08 -07009441 std::make_unique<NullEncrypter>(framer_.perspective()));
QUICHE teama6ef0a62019-03-07 20:34:33 -05009442 ParsedQuicVersionVector versions;
9443 versions.push_back(framer_.version());
9444 std::unique_ptr<QuicEncryptedPacket> packet(ConstructMisFramedEncryptedPacket(
9445 TestConnectionId(), EmptyQuicConnectionId(), false, false,
9446 kTestQuicStreamId, kTestString, CONNECTION_ID_PRESENT,
9447 CONNECTION_ID_ABSENT, PACKET_4BYTE_PACKET_NUMBER, &versions,
9448 Perspective::IS_CLIENT));
9449
9450 MockFramerVisitor visitor;
9451 framer_.set_visitor(&visitor);
9452 EXPECT_CALL(visitor, OnPacket()).Times(1);
9453 EXPECT_CALL(visitor, OnUnauthenticatedPublicHeader(_))
9454 .Times(1)
9455 .WillOnce(Return(true));
9456 EXPECT_CALL(visitor, OnUnauthenticatedHeader(_))
9457 .Times(1)
9458 .WillOnce(Return(true));
9459 EXPECT_CALL(visitor, OnPacketHeader(_)).Times(1);
9460 EXPECT_CALL(visitor, OnDecryptedPacket(_)).Times(1);
9461 EXPECT_CALL(visitor, OnError(_)).Times(1);
9462 EXPECT_CALL(visitor, OnStreamFrame(_)).Times(0);
9463 EXPECT_CALL(visitor, OnPacketComplete()).Times(0);
9464
9465 EXPECT_FALSE(framer_.ProcessPacket(*packet));
9466 EXPECT_EQ(QUIC_INVALID_FRAME_DATA, framer_.error());
9467}
9468
QUICHE teama6ef0a62019-03-07 20:34:33 -05009469TEST_P(QuicFramerTest, IetfBlockedFrame) {
9470 // This test only for version 99.
fkastenholz305e1732019-06-18 05:01:22 -07009471 if (!VersionHasIetfQuicFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05009472 return;
9473 }
zhongyi546cc452019-04-12 15:27:49 -07009474 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
QUICHE teama6ef0a62019-03-07 20:34:33 -05009475
9476 // clang-format off
9477 PacketFragments packet99 = {
9478 // type (short header, 4 byte packet number)
9479 {"",
9480 {0x43}},
9481 // connection_id
9482 {"",
9483 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
9484 // packet number
9485 {"",
9486 {0x12, 0x34, 0x9A, 0xBC}},
9487 // frame type (IETF_BLOCKED)
9488 {"",
9489 {0x14}},
9490 // blocked offset
9491 {"Can not read blocked offset.",
9492 {kVarInt62EightBytes + 0x3a, 0x98, 0xFE, 0xDC, 0x32, 0x10, 0x76, 0x54}},
9493 };
9494 // clang-format on
9495
9496 std::unique_ptr<QuicEncryptedPacket> encrypted(
9497 AssemblePacketFromFragments(packet99));
9498 EXPECT_TRUE(framer_.ProcessPacket(*encrypted));
9499
9500 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
9501 ASSERT_TRUE(visitor_.header_.get());
9502 EXPECT_TRUE(CheckDecryption(
9503 *encrypted, !kIncludeVersion, !kIncludeDiversificationNonce,
9504 PACKET_8BYTE_CONNECTION_ID, PACKET_0BYTE_CONNECTION_ID));
9505
9506 EXPECT_EQ(kStreamOffset, visitor_.blocked_frame_.offset);
9507
9508 CheckFramingBoundaries(packet99, QUIC_INVALID_BLOCKED_DATA);
9509}
9510
9511TEST_P(QuicFramerTest, BuildIetfBlockedPacket) {
9512 // This test only for version 99.
fkastenholz305e1732019-06-18 05:01:22 -07009513 if (!VersionHasIetfQuicFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05009514 return;
9515 }
9516
9517 QuicPacketHeader header;
9518 header.destination_connection_id = FramerTestConnectionId();
9519 header.reset_flag = false;
9520 header.version_flag = false;
9521 header.packet_number = kPacketNumber;
9522
9523 QuicBlockedFrame frame;
9524 frame.stream_id = QuicUtils::GetInvalidStreamId(framer_.transport_version());
9525 frame.offset = kStreamOffset;
9526 QuicFrames frames = {QuicFrame(&frame)};
9527
9528 // clang-format off
9529 unsigned char packet99[] = {
9530 // type (short header, 4 byte packet number)
9531 0x43,
9532 // connection_id
9533 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
9534 // packet number
9535 0x12, 0x34, 0x56, 0x78,
9536
9537 // frame type (IETF_BLOCKED)
9538 0x14,
9539 // Offset
9540 kVarInt62EightBytes + 0x3a, 0x98, 0xFE, 0xDC, 0x32, 0x10, 0x76, 0x54
9541 };
9542 // clang-format on
9543
9544 std::unique_ptr<QuicPacket> data(BuildDataPacket(header, frames));
9545 ASSERT_TRUE(data != nullptr);
9546
9547 test::CompareCharArraysWithHexError("constructed packet", data->data(),
9548 data->length(), AsChars(packet99),
9549 QUIC_ARRAYSIZE(packet99));
9550}
9551
9552TEST_P(QuicFramerTest, IetfStreamBlockedFrame) {
9553 // This test only for version 99.
fkastenholz305e1732019-06-18 05:01:22 -07009554 if (!VersionHasIetfQuicFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05009555 return;
9556 }
zhongyi546cc452019-04-12 15:27:49 -07009557 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
QUICHE teama6ef0a62019-03-07 20:34:33 -05009558
9559 // clang-format off
9560 PacketFragments packet99 = {
9561 // type (short header, 4 byte packet number)
9562 {"",
9563 {0x43}},
9564 // connection_id
9565 {"",
9566 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
9567 // packet number
9568 {"",
9569 {0x12, 0x34, 0x9A, 0xBC}},
9570 // frame type (IETF_STREAM_BLOCKED)
9571 {"",
9572 {0x15}},
9573 // blocked offset
9574 {"Can not read stream blocked stream id.",
9575 {kVarInt62FourBytes + 0x01, 0x02, 0x03, 0x04}},
9576 {"Can not read stream blocked offset.",
9577 {kVarInt62EightBytes + 0x3a, 0x98, 0xFE, 0xDC, 0x32, 0x10, 0x76, 0x54}},
9578 };
9579 // clang-format on
9580
9581 std::unique_ptr<QuicEncryptedPacket> encrypted(
9582 AssemblePacketFromFragments(packet99));
9583 EXPECT_TRUE(framer_.ProcessPacket(*encrypted));
9584
9585 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
9586 ASSERT_TRUE(visitor_.header_.get());
9587 EXPECT_TRUE(CheckDecryption(
9588 *encrypted, !kIncludeVersion, !kIncludeDiversificationNonce,
9589 PACKET_8BYTE_CONNECTION_ID, PACKET_0BYTE_CONNECTION_ID));
9590
9591 EXPECT_EQ(kStreamId, visitor_.blocked_frame_.stream_id);
9592 EXPECT_EQ(kStreamOffset, visitor_.blocked_frame_.offset);
9593
9594 CheckFramingBoundaries(packet99, QUIC_INVALID_STREAM_BLOCKED_DATA);
9595}
9596
9597TEST_P(QuicFramerTest, BuildIetfStreamBlockedPacket) {
9598 // This test only for version 99.
fkastenholz305e1732019-06-18 05:01:22 -07009599 if (!VersionHasIetfQuicFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05009600 return;
9601 }
9602
9603 QuicPacketHeader header;
9604 header.destination_connection_id = FramerTestConnectionId();
9605 header.reset_flag = false;
9606 header.version_flag = false;
9607 header.packet_number = kPacketNumber;
9608
9609 QuicBlockedFrame frame;
9610 frame.stream_id = kStreamId;
9611 frame.offset = kStreamOffset;
9612 QuicFrames frames = {QuicFrame(&frame)};
9613
9614 // clang-format off
9615 unsigned char packet99[] = {
9616 // type (short header, 4 byte packet number)
9617 0x43,
9618 // connection_id
9619 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
9620 // packet number
9621 0x12, 0x34, 0x56, 0x78,
9622
9623 // frame type (IETF_STREAM_BLOCKED)
9624 0x15,
9625 // Stream ID
9626 kVarInt62FourBytes + 0x01, 0x02, 0x03, 0x04,
9627 // Offset
9628 kVarInt62EightBytes + 0x3a, 0x98, 0xFE, 0xDC, 0x32, 0x10, 0x76, 0x54
9629 };
9630 // clang-format on
9631
9632 std::unique_ptr<QuicPacket> data(BuildDataPacket(header, frames));
9633 ASSERT_TRUE(data != nullptr);
9634
9635 test::CompareCharArraysWithHexError("constructed packet", data->data(),
9636 data->length(), AsChars(packet99),
9637 QUIC_ARRAYSIZE(packet99));
9638}
9639
fkastenholz3c4eabf2019-04-22 07:49:59 -07009640TEST_P(QuicFramerTest, BiDiMaxStreamsFrame) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05009641 // This test only for version 99.
fkastenholz305e1732019-06-18 05:01:22 -07009642 if (!VersionHasIetfQuicFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05009643 return;
9644 }
zhongyi546cc452019-04-12 15:27:49 -07009645 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
QUICHE teama6ef0a62019-03-07 20:34:33 -05009646
9647 // clang-format off
9648 PacketFragments packet99 = {
9649 // type (short header, 4 byte packet number)
9650 {"",
9651 {0x43}},
9652 // connection_id
9653 {"",
9654 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
9655 // packet number
9656 {"",
9657 {0x12, 0x34, 0x9A, 0xBC}},
9658 // frame type (IETF_MAX_STREAMS_BIDIRECTIONAL)
9659 {"",
9660 {0x12}},
9661 // max. streams
9662 {"Can not read MAX_STREAMS stream count.",
9663 {kVarInt62OneByte + 0x03}},
9664 };
9665 // clang-format on
9666
9667 std::unique_ptr<QuicEncryptedPacket> encrypted(
9668 AssemblePacketFromFragments(packet99));
9669 EXPECT_TRUE(framer_.ProcessPacket(*encrypted));
9670
9671 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
9672 ASSERT_TRUE(visitor_.header_.get());
9673 EXPECT_TRUE(CheckDecryption(
9674 *encrypted, !kIncludeVersion, !kIncludeDiversificationNonce,
9675 PACKET_8BYTE_CONNECTION_ID, PACKET_0BYTE_CONNECTION_ID));
9676
fkastenholz3c4eabf2019-04-22 07:49:59 -07009677 EXPECT_EQ(3u, visitor_.max_streams_frame_.stream_count);
9678 EXPECT_FALSE(visitor_.max_streams_frame_.unidirectional);
9679 CheckFramingBoundaries(packet99, QUIC_MAX_STREAMS_DATA);
QUICHE teama6ef0a62019-03-07 20:34:33 -05009680}
9681
fkastenholz3c4eabf2019-04-22 07:49:59 -07009682TEST_P(QuicFramerTest, UniDiMaxStreamsFrame) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05009683 // This test only for version 99.
fkastenholz305e1732019-06-18 05:01:22 -07009684 if (!VersionHasIetfQuicFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05009685 return;
9686 }
zhongyi546cc452019-04-12 15:27:49 -07009687 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
QUICHE teama6ef0a62019-03-07 20:34:33 -05009688
9689 // clang-format off
9690 PacketFragments packet99 = {
9691 // type (short header, 4 byte packet number)
9692 {"",
9693 {0x43}},
9694 // Test runs in client mode, no connection id
9695 // packet number
9696 {"",
9697 {0x12, 0x34, 0x9A, 0xBC}},
fkastenholz3c4eabf2019-04-22 07:49:59 -07009698 // frame type (IETF_MAX_STREAMS_UNIDIRECTIONAL)
QUICHE teama6ef0a62019-03-07 20:34:33 -05009699 {"",
fkastenholz3c4eabf2019-04-22 07:49:59 -07009700 {0x13}},
QUICHE teama6ef0a62019-03-07 20:34:33 -05009701 // max. streams
9702 {"Can not read MAX_STREAMS stream count.",
9703 {kVarInt62OneByte + 0x03}},
9704 };
9705 // clang-format on
9706
9707 std::unique_ptr<QuicEncryptedPacket> encrypted(
9708 AssemblePacketFromFragments(packet99));
9709 QuicFramerPeer::SetPerspective(&framer_, Perspective::IS_CLIENT);
9710 EXPECT_TRUE(framer_.ProcessPacket(*encrypted));
9711
9712 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
9713 ASSERT_TRUE(visitor_.header_.get());
9714 EXPECT_TRUE(CheckDecryption(
9715 *encrypted, !kIncludeVersion, !kIncludeDiversificationNonce,
9716 PACKET_0BYTE_CONNECTION_ID, PACKET_0BYTE_CONNECTION_ID));
9717
fkastenholz3c4eabf2019-04-22 07:49:59 -07009718 EXPECT_EQ(3u, visitor_.max_streams_frame_.stream_count);
9719 EXPECT_TRUE(visitor_.max_streams_frame_.unidirectional);
9720 CheckFramingBoundaries(packet99, QUIC_MAX_STREAMS_DATA);
QUICHE teama6ef0a62019-03-07 20:34:33 -05009721}
9722
9723TEST_P(QuicFramerTest, ServerUniDiMaxStreamsFrame) {
9724 // This test only for version 99.
fkastenholz305e1732019-06-18 05:01:22 -07009725 if (!VersionHasIetfQuicFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05009726 return;
9727 }
zhongyi546cc452019-04-12 15:27:49 -07009728 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
QUICHE teama6ef0a62019-03-07 20:34:33 -05009729
9730 // clang-format off
9731 PacketFragments packet99 = {
9732 // type (short header, 4 byte packet number)
9733 {"",
9734 {0x43}},
9735 // connection_id
9736 {"",
9737 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
9738 // packet number
9739 {"",
9740 {0x12, 0x34, 0x9A, 0xBC}},
9741 // frame type (IETF_MAX_STREAMS_UNIDIRECTIONAL)
9742 {"",
9743 {0x13}},
9744 // max. streams
9745 {"Can not read MAX_STREAMS stream count.",
9746 {kVarInt62OneByte + 0x03}},
9747 };
9748 // clang-format on
9749
9750 std::unique_ptr<QuicEncryptedPacket> encrypted(
9751 AssemblePacketFromFragments(packet99));
9752 EXPECT_TRUE(framer_.ProcessPacket(*encrypted));
9753
9754 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
9755 ASSERT_TRUE(visitor_.header_.get());
9756 EXPECT_TRUE(CheckDecryption(
9757 *encrypted, !kIncludeVersion, !kIncludeDiversificationNonce,
9758 PACKET_8BYTE_CONNECTION_ID, PACKET_0BYTE_CONNECTION_ID));
9759
fkastenholz3c4eabf2019-04-22 07:49:59 -07009760 EXPECT_EQ(3u, visitor_.max_streams_frame_.stream_count);
9761 EXPECT_TRUE(visitor_.max_streams_frame_.unidirectional);
9762 CheckFramingBoundaries(packet99, QUIC_MAX_STREAMS_DATA);
QUICHE teama6ef0a62019-03-07 20:34:33 -05009763}
9764
9765TEST_P(QuicFramerTest, ClientUniDiMaxStreamsFrame) {
9766 // This test only for version 99.
fkastenholz305e1732019-06-18 05:01:22 -07009767 if (!VersionHasIetfQuicFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05009768 return;
9769 }
zhongyi546cc452019-04-12 15:27:49 -07009770 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
QUICHE teama6ef0a62019-03-07 20:34:33 -05009771
9772 // clang-format off
9773 PacketFragments packet99 = {
9774 // type (short header, 4 byte packet number)
9775 {"",
9776 {0x43}},
9777 // Test runs in client mode, no connection id
9778 // packet number
9779 {"",
9780 {0x12, 0x34, 0x9A, 0xBC}},
9781 // frame type (IETF_MAX_STREAMS_UNIDIRECTIONAL)
9782 {"",
9783 {0x13}},
9784 // max. streams
9785 {"Can not read MAX_STREAMS stream count.",
9786 {kVarInt62OneByte + 0x03}},
9787 };
9788 // clang-format on
9789
9790 std::unique_ptr<QuicEncryptedPacket> encrypted(
9791 AssemblePacketFromFragments(packet99));
9792 QuicFramerPeer::SetPerspective(&framer_, Perspective::IS_CLIENT);
9793 EXPECT_TRUE(framer_.ProcessPacket(*encrypted));
9794
9795 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
9796 ASSERT_TRUE(visitor_.header_.get());
9797 EXPECT_TRUE(CheckDecryption(
9798 *encrypted, !kIncludeVersion, !kIncludeDiversificationNonce,
9799 PACKET_0BYTE_CONNECTION_ID, PACKET_0BYTE_CONNECTION_ID));
9800
fkastenholz3c4eabf2019-04-22 07:49:59 -07009801 EXPECT_EQ(3u, visitor_.max_streams_frame_.stream_count);
9802 EXPECT_TRUE(visitor_.max_streams_frame_.unidirectional);
9803 CheckFramingBoundaries(packet99, QUIC_MAX_STREAMS_DATA);
QUICHE teama6ef0a62019-03-07 20:34:33 -05009804}
9805
9806// The following four tests ensure that the framer can deserialize a stream
9807// count that is large enough to cause the resulting stream ID to exceed the
9808// current implementation limit(32 bits). The intent is that when this happens,
9809// the stream limit is pegged to the maximum supported value. There are four
9810// tests, for the four combinations of uni- and bi-directional, server- and
9811// client- initiated.
fkastenholz3c4eabf2019-04-22 07:49:59 -07009812TEST_P(QuicFramerTest, BiDiMaxStreamsFrameTooBig) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05009813 // This test only for version 99.
fkastenholz305e1732019-06-18 05:01:22 -07009814 if (!VersionHasIetfQuicFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05009815 return;
9816 }
zhongyi546cc452019-04-12 15:27:49 -07009817 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
QUICHE teama6ef0a62019-03-07 20:34:33 -05009818
9819 // clang-format off
9820 unsigned char packet99[] = {
9821 // type (short header, 4 byte packet number)
9822 0x43,
9823 // connection_id
9824 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
9825 // packet number
9826 0x12, 0x34, 0x9A, 0xBC,
9827 // frame type (IETF_MAX_STREAMS_BIDIRECTIONAL)
9828 0x12,
9829
9830 // max. streams. Max stream ID allowed is 0xffffffff
9831 // This encodes a count of 0x40000000, leading to stream
9832 // IDs in the range 0x1 00000000 to 0x1 00000003.
9833 kVarInt62EightBytes + 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00
9834 };
9835 // clang-format on
9836
9837 QuicEncryptedPacket encrypted(AsChars(packet99), QUIC_ARRAYSIZE(packet99),
9838 false);
9839 EXPECT_TRUE(framer_.ProcessPacket(encrypted));
9840 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
9841 ASSERT_TRUE(visitor_.header_.get());
9842 EXPECT_TRUE(CheckDecryption(
9843 encrypted, !kIncludeVersion, !kIncludeDiversificationNonce,
9844 PACKET_8BYTE_CONNECTION_ID, PACKET_0BYTE_CONNECTION_ID));
9845
fkastenholz465220f2019-04-23 07:56:27 -07009846 EXPECT_EQ(0x40000000u, visitor_.max_streams_frame_.stream_count);
fkastenholz3c4eabf2019-04-22 07:49:59 -07009847 EXPECT_FALSE(visitor_.max_streams_frame_.unidirectional);
QUICHE teama6ef0a62019-03-07 20:34:33 -05009848}
9849
9850TEST_P(QuicFramerTest, ClientBiDiMaxStreamsFrameTooBig) {
9851 // This test only for version 99.
fkastenholz305e1732019-06-18 05:01:22 -07009852 if (!VersionHasIetfQuicFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05009853 return;
9854 }
zhongyi546cc452019-04-12 15:27:49 -07009855 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
QUICHE teama6ef0a62019-03-07 20:34:33 -05009856
9857 // clang-format off
9858 unsigned char packet99[] = {
9859 // type (short header, 4 byte packet number)
9860 0x43,
9861 // Test runs in client mode, no connection id
9862 // packet number
9863 0x12, 0x34, 0x9A, 0xBC,
9864 // frame type (IETF_MAX_STREAMS_BIDIRECTIONAL)
9865 0x12,
9866
9867 // max. streams. Max stream ID allowed is 0xffffffff
9868 // This encodes a count of 0x40000000, leading to stream
9869 // IDs in the range 0x1 00000000 to 0x1 00000003.
9870 kVarInt62EightBytes + 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00
9871 };
9872 // clang-format on
9873
9874 QuicEncryptedPacket encrypted(AsChars(packet99), QUIC_ARRAYSIZE(packet99),
9875 false);
9876 QuicFramerPeer::SetPerspective(&framer_, Perspective::IS_CLIENT);
9877 EXPECT_TRUE(framer_.ProcessPacket(encrypted));
9878
9879 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
9880 ASSERT_TRUE(visitor_.header_.get());
9881 EXPECT_TRUE(CheckDecryption(
9882 encrypted, !kIncludeVersion, !kIncludeDiversificationNonce,
9883 PACKET_0BYTE_CONNECTION_ID, PACKET_0BYTE_CONNECTION_ID));
9884
fkastenholz465220f2019-04-23 07:56:27 -07009885 EXPECT_EQ(0x40000000u, visitor_.max_streams_frame_.stream_count);
fkastenholz3c4eabf2019-04-22 07:49:59 -07009886 EXPECT_FALSE(visitor_.max_streams_frame_.unidirectional);
QUICHE teama6ef0a62019-03-07 20:34:33 -05009887}
9888
9889TEST_P(QuicFramerTest, ServerUniDiMaxStreamsFrameTooBig) {
9890 // This test only for version 99.
fkastenholz305e1732019-06-18 05:01:22 -07009891 if (!VersionHasIetfQuicFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05009892 return;
9893 }
zhongyi546cc452019-04-12 15:27:49 -07009894 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
QUICHE teama6ef0a62019-03-07 20:34:33 -05009895
9896 // clang-format off
9897 unsigned char packet99[] = {
9898 // type (short header, 4 byte packet number)
9899 0x43,
9900 // connection_id
9901 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
9902 // packet number
9903 0x12, 0x34, 0x9A, 0xBC,
9904 // frame type (IETF_MAX_STREAMS_UNIDIRECTIONAL)
9905 0x13,
9906
9907 // max. streams. Max stream ID allowed is 0xffffffff
9908 // This encodes a count of 0x40000000, leading to stream
9909 // IDs in the range 0x1 00000000 to 0x1 00000003.
9910 kVarInt62EightBytes + 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00
9911 };
9912 // clang-format on
9913
9914 QuicEncryptedPacket encrypted(AsChars(packet99), QUIC_ARRAYSIZE(packet99),
9915 false);
9916 EXPECT_TRUE(framer_.ProcessPacket(encrypted));
9917
9918 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
9919 ASSERT_TRUE(visitor_.header_.get());
9920 EXPECT_TRUE(CheckDecryption(
9921 encrypted, !kIncludeVersion, !kIncludeDiversificationNonce,
9922 PACKET_8BYTE_CONNECTION_ID, PACKET_0BYTE_CONNECTION_ID));
9923
fkastenholz465220f2019-04-23 07:56:27 -07009924 EXPECT_EQ(0x40000000u, visitor_.max_streams_frame_.stream_count);
fkastenholz3c4eabf2019-04-22 07:49:59 -07009925 EXPECT_TRUE(visitor_.max_streams_frame_.unidirectional);
QUICHE teama6ef0a62019-03-07 20:34:33 -05009926}
9927
9928TEST_P(QuicFramerTest, ClientUniDiMaxStreamsFrameTooBig) {
9929 // This test only for version 99.
fkastenholz305e1732019-06-18 05:01:22 -07009930 if (!VersionHasIetfQuicFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05009931 return;
9932 }
zhongyi546cc452019-04-12 15:27:49 -07009933 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
QUICHE teama6ef0a62019-03-07 20:34:33 -05009934
9935 // clang-format off
9936 unsigned char packet99[] = {
9937 // type (short header, 4 byte packet number)
9938 0x43,
9939 // Test runs in client mode, no connection id
9940 // packet number
9941 0x12, 0x34, 0x9A, 0xBC,
9942 // frame type (IETF_MAX_STREAMS_UNDIRECTIONAL)
9943 0x13,
9944
9945 // max. streams. Max stream ID allowed is 0xffffffff
9946 // This encodes a count of 0x40000000, leading to stream
9947 // IDs in the range 0x1 00000000 to 0x1 00000003.
9948 kVarInt62EightBytes + 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00
9949 };
9950 // clang-format on
9951
9952 QuicEncryptedPacket encrypted(AsChars(packet99), QUIC_ARRAYSIZE(packet99),
9953 false);
9954 QuicFramerPeer::SetPerspective(&framer_, Perspective::IS_CLIENT);
9955 EXPECT_TRUE(framer_.ProcessPacket(encrypted));
9956
9957 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
9958 ASSERT_TRUE(visitor_.header_.get());
9959 EXPECT_TRUE(CheckDecryption(
9960 encrypted, !kIncludeVersion, !kIncludeDiversificationNonce,
9961 PACKET_0BYTE_CONNECTION_ID, PACKET_0BYTE_CONNECTION_ID));
9962
fkastenholz465220f2019-04-23 07:56:27 -07009963 EXPECT_EQ(0x40000000u, visitor_.max_streams_frame_.stream_count);
fkastenholz3c4eabf2019-04-22 07:49:59 -07009964 EXPECT_TRUE(visitor_.max_streams_frame_.unidirectional);
QUICHE teama6ef0a62019-03-07 20:34:33 -05009965}
9966
fkastenholz3c4eabf2019-04-22 07:49:59 -07009967// Specifically test that count==0 is accepted.
QUICHE teama6ef0a62019-03-07 20:34:33 -05009968TEST_P(QuicFramerTest, MaxStreamsFrameZeroCount) {
9969 // This test only for version 99.
fkastenholz305e1732019-06-18 05:01:22 -07009970 if (!VersionHasIetfQuicFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05009971 return;
9972 }
zhongyi546cc452019-04-12 15:27:49 -07009973 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
QUICHE teama6ef0a62019-03-07 20:34:33 -05009974
9975 // clang-format off
9976 unsigned char packet99[] = {
9977 // type (short header, 4 byte packet number)
9978 0x43,
9979 // connection_id
9980 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
9981 // packet number
9982 0x12, 0x34, 0x9A, 0xBC,
9983 // frame type (IETF_MAX_STREAMS_BIDIRECTIONAL)
9984 0x12,
9985 // max. streams == 0.
9986 kVarInt62OneByte + 0x00
9987 };
9988 // clang-format on
9989
9990 QuicEncryptedPacket encrypted(AsChars(packet99), QUIC_ARRAYSIZE(packet99),
9991 false);
fkastenholz3c4eabf2019-04-22 07:49:59 -07009992 EXPECT_TRUE(framer_.ProcessPacket(encrypted));
QUICHE teama6ef0a62019-03-07 20:34:33 -05009993}
9994
9995TEST_P(QuicFramerTest, ServerBiDiStreamsBlockedFrame) {
9996 // This test only for version 99.
fkastenholz305e1732019-06-18 05:01:22 -07009997 if (!VersionHasIetfQuicFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05009998 return;
9999 }
zhongyi546cc452019-04-12 15:27:49 -070010000 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
QUICHE teama6ef0a62019-03-07 20:34:33 -050010001
10002 // clang-format off
10003 PacketFragments packet99 = {
10004 // type (short header, 4 byte packet number)
10005 {"",
10006 {0x43}},
10007 // connection_id
10008 {"",
10009 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
10010 // packet number
10011 {"",
10012 {0x12, 0x34, 0x9A, 0xBC}},
fkastenholz3c4eabf2019-04-22 07:49:59 -070010013 // frame type (IETF_MAX_STREAMS_UNIDIRECTIONAL frame)
10014 {"",
10015 {0x13}},
10016 // stream count
10017 {"Can not read MAX_STREAMS stream count.",
10018 {kVarInt62OneByte + 0x00}},
10019 };
10020 // clang-format on
10021
10022 std::unique_ptr<QuicEncryptedPacket> encrypted(
10023 AssemblePacketFromFragments(packet99));
10024 EXPECT_TRUE(framer_.ProcessPacket(*encrypted));
10025
10026 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
10027 ASSERT_TRUE(visitor_.header_.get());
10028 EXPECT_TRUE(CheckDecryption(
10029 *encrypted, !kIncludeVersion, !kIncludeDiversificationNonce,
10030 PACKET_8BYTE_CONNECTION_ID, PACKET_0BYTE_CONNECTION_ID));
10031
10032 EXPECT_EQ(0u, visitor_.max_streams_frame_.stream_count);
10033 EXPECT_TRUE(visitor_.max_streams_frame_.unidirectional);
10034
10035 CheckFramingBoundaries(packet99, QUIC_MAX_STREAMS_DATA);
10036}
10037
10038TEST_P(QuicFramerTest, BiDiStreamsBlockedFrame) {
10039 // This test only for version 99.
fkastenholz305e1732019-06-18 05:01:22 -070010040 if (!VersionHasIetfQuicFrames(framer_.transport_version())) {
fkastenholz3c4eabf2019-04-22 07:49:59 -070010041 return;
10042 }
10043 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
10044
10045 // clang-format off
10046 PacketFragments packet99 = {
10047 // type (short header, 4 byte packet number)
10048 {"",
10049 {0x43}},
10050 // connection_id
10051 {"",
10052 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
10053 // packet number
10054 {"",
10055 {0x12, 0x34, 0x9A, 0xBC}},
QUICHE teama6ef0a62019-03-07 20:34:33 -050010056 // frame type (IETF_STREAMS_BLOCKED_BIDIRECTIONAL frame)
10057 {"",
10058 {0x16}},
10059 // stream id
fkastenholz3c4eabf2019-04-22 07:49:59 -070010060 {"Can not read STREAMS_BLOCKED stream count.",
QUICHE teama6ef0a62019-03-07 20:34:33 -050010061 {kVarInt62OneByte + 0x03}},
10062 };
10063 // clang-format on
10064
10065 std::unique_ptr<QuicEncryptedPacket> encrypted(
10066 AssemblePacketFromFragments(packet99));
10067 EXPECT_TRUE(framer_.ProcessPacket(*encrypted));
10068
10069 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
10070 ASSERT_TRUE(visitor_.header_.get());
10071 EXPECT_TRUE(CheckDecryption(
10072 *encrypted, !kIncludeVersion, !kIncludeDiversificationNonce,
10073 PACKET_8BYTE_CONNECTION_ID, PACKET_0BYTE_CONNECTION_ID));
10074
fkastenholz3c4eabf2019-04-22 07:49:59 -070010075 EXPECT_EQ(3u, visitor_.streams_blocked_frame_.stream_count);
10076 EXPECT_FALSE(visitor_.streams_blocked_frame_.unidirectional);
QUICHE teama6ef0a62019-03-07 20:34:33 -050010077
fkastenholz3c4eabf2019-04-22 07:49:59 -070010078 CheckFramingBoundaries(packet99, QUIC_STREAMS_BLOCKED_DATA);
QUICHE teama6ef0a62019-03-07 20:34:33 -050010079}
10080
fkastenholz3c4eabf2019-04-22 07:49:59 -070010081TEST_P(QuicFramerTest, UniDiStreamsBlockedFrame) {
QUICHE teama6ef0a62019-03-07 20:34:33 -050010082 // This test only for version 99.
fkastenholz305e1732019-06-18 05:01:22 -070010083 if (!VersionHasIetfQuicFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -050010084 return;
10085 }
zhongyi546cc452019-04-12 15:27:49 -070010086 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
QUICHE teama6ef0a62019-03-07 20:34:33 -050010087
10088 // clang-format off
10089 PacketFragments packet99 = {
10090 // type (short header, 4 byte packet number)
10091 {"",
10092 {0x43}},
10093 // connection_id
10094 {"",
10095 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
10096 // packet number
10097 {"",
10098 {0x12, 0x34, 0x9A, 0xBC}},
10099 // frame type (IETF_STREAMS_BLOCKED_UNIDIRECTIONAL frame)
10100 {"",
10101 {0x17}},
10102 // stream id
fkastenholz3c4eabf2019-04-22 07:49:59 -070010103 {"Can not read STREAMS_BLOCKED stream count.",
QUICHE teama6ef0a62019-03-07 20:34:33 -050010104 {kVarInt62OneByte + 0x03}},
10105 };
10106 // clang-format on
10107
10108 std::unique_ptr<QuicEncryptedPacket> encrypted(
10109 AssemblePacketFromFragments(packet99));
10110 EXPECT_TRUE(framer_.ProcessPacket(*encrypted));
10111
10112 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
10113 ASSERT_TRUE(visitor_.header_.get());
10114 EXPECT_TRUE(CheckDecryption(
10115 *encrypted, !kIncludeVersion, !kIncludeDiversificationNonce,
10116 PACKET_8BYTE_CONNECTION_ID, PACKET_0BYTE_CONNECTION_ID));
10117
fkastenholz3c4eabf2019-04-22 07:49:59 -070010118 EXPECT_EQ(3u, visitor_.streams_blocked_frame_.stream_count);
10119 EXPECT_TRUE(visitor_.streams_blocked_frame_.unidirectional);
10120 CheckFramingBoundaries(packet99, QUIC_STREAMS_BLOCKED_DATA);
QUICHE teama6ef0a62019-03-07 20:34:33 -050010121}
10122
10123TEST_P(QuicFramerTest, ClientUniDiStreamsBlockedFrame) {
10124 // This test only for version 99.
fkastenholz305e1732019-06-18 05:01:22 -070010125 if (!VersionHasIetfQuicFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -050010126 return;
10127 }
zhongyi546cc452019-04-12 15:27:49 -070010128 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
QUICHE teama6ef0a62019-03-07 20:34:33 -050010129
10130 // clang-format off
10131 PacketFragments packet99 = {
10132 // type (short header, 4 byte packet number)
10133 {"",
10134 {0x43}},
10135 // Test runs in client mode, no connection id
10136 // packet number
10137 {"",
10138 {0x12, 0x34, 0x9A, 0xBC}},
10139 // frame type (IETF_STREAMS_BLOCKED_UNIDIRECTIONAL frame)
10140 {"",
10141 {0x17}},
10142 // stream id
fkastenholz3c4eabf2019-04-22 07:49:59 -070010143 {"Can not read STREAMS_BLOCKED stream count.",
QUICHE teama6ef0a62019-03-07 20:34:33 -050010144 {kVarInt62OneByte + 0x03}},
10145 };
10146 // clang-format on
10147
10148 std::unique_ptr<QuicEncryptedPacket> encrypted(
10149 AssemblePacketFromFragments(packet99));
10150 QuicFramerPeer::SetPerspective(&framer_, Perspective::IS_CLIENT);
10151 EXPECT_TRUE(framer_.ProcessPacket(*encrypted));
10152
10153 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
10154 ASSERT_TRUE(visitor_.header_.get());
10155 EXPECT_TRUE(CheckDecryption(
10156 *encrypted, !kIncludeVersion, !kIncludeDiversificationNonce,
10157 PACKET_0BYTE_CONNECTION_ID, PACKET_0BYTE_CONNECTION_ID));
10158
fkastenholz3c4eabf2019-04-22 07:49:59 -070010159 EXPECT_EQ(3u, visitor_.streams_blocked_frame_.stream_count);
10160 EXPECT_TRUE(visitor_.streams_blocked_frame_.unidirectional);
10161 CheckFramingBoundaries(packet99, QUIC_STREAMS_BLOCKED_DATA);
QUICHE teama6ef0a62019-03-07 20:34:33 -050010162}
10163
10164// Check that when we get a STREAMS_BLOCKED frame that specifies too large
10165// a stream count, we reject with an appropriate error. There is no need to
10166// check for different combinations of Uni/Bi directional and client/server
10167// initiated; the logic does not take these into account.
10168TEST_P(QuicFramerTest, StreamsBlockedFrameTooBig) {
10169 // This test only for version 99.
fkastenholz305e1732019-06-18 05:01:22 -070010170 if (!VersionHasIetfQuicFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -050010171 return;
10172 }
zhongyi546cc452019-04-12 15:27:49 -070010173 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
QUICHE teama6ef0a62019-03-07 20:34:33 -050010174
10175 // clang-format off
10176 unsigned char packet99[] = {
10177 // type (short header, 4 byte packet number)
10178 0x43,
10179 // Test runs in client mode, no connection id
10180 // packet number
10181 0x12, 0x34, 0x9A, 0xBC,
10182 // frame type (IETF_STREAMS_BLOCKED_BIDIRECTIONAL)
fkastenholz3c4eabf2019-04-22 07:49:59 -070010183 0x16,
QUICHE teama6ef0a62019-03-07 20:34:33 -050010184
10185 // max. streams. Max stream ID allowed is 0xffffffff
10186 // This encodes a count of 0x40000000, leading to stream
10187 // IDs in the range 0x1 00000000 to 0x1 00000003.
10188 kVarInt62EightBytes + 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x01
10189 };
10190 // clang-format on
10191
10192 QuicEncryptedPacket encrypted(AsChars(packet99), QUIC_ARRAYSIZE(packet99),
10193 false);
10194 QuicFramerPeer::SetPerspective(&framer_, Perspective::IS_CLIENT);
10195 EXPECT_FALSE(framer_.ProcessPacket(encrypted));
10196
fkastenholz3c4eabf2019-04-22 07:49:59 -070010197 EXPECT_EQ(QUIC_STREAMS_BLOCKED_DATA, framer_.error());
QUICHE teama6ef0a62019-03-07 20:34:33 -050010198 EXPECT_EQ(framer_.detailed_error(),
10199 "STREAMS_BLOCKED stream count exceeds implementation limit.");
10200}
10201
fkastenholz3c4eabf2019-04-22 07:49:59 -070010202// Specifically test that count==0 is accepted.
QUICHE teama6ef0a62019-03-07 20:34:33 -050010203TEST_P(QuicFramerTest, StreamsBlockedFrameZeroCount) {
10204 // This test only for version 99.
fkastenholz305e1732019-06-18 05:01:22 -070010205 if (!VersionHasIetfQuicFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -050010206 return;
10207 }
fkastenholz3c4eabf2019-04-22 07:49:59 -070010208
zhongyi546cc452019-04-12 15:27:49 -070010209 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
QUICHE teama6ef0a62019-03-07 20:34:33 -050010210
10211 // clang-format off
fkastenholz3c4eabf2019-04-22 07:49:59 -070010212 PacketFragments packet99 = {
10213 // type (short header, 4 byte packet number)
10214 {"",
10215 {0x43}},
10216 // connection_id
10217 {"",
10218 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
10219 // packet number
10220 {"",
10221 {0x12, 0x34, 0x9A, 0xBC}},
10222 // frame type (IETF_STREAMS_BLOCKED_UNIDIRECTIONAL frame)
10223 {"",
10224 {0x17}},
10225 // stream id
10226 {"Can not read STREAMS_BLOCKED stream count.",
10227 {kVarInt62OneByte + 0x00}},
QUICHE teama6ef0a62019-03-07 20:34:33 -050010228 };
10229 // clang-format on
10230
fkastenholz3c4eabf2019-04-22 07:49:59 -070010231 std::unique_ptr<QuicEncryptedPacket> encrypted(
10232 AssemblePacketFromFragments(packet99));
10233 EXPECT_TRUE(framer_.ProcessPacket(*encrypted));
QUICHE teama6ef0a62019-03-07 20:34:33 -050010234
fkastenholz3c4eabf2019-04-22 07:49:59 -070010235 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
10236 ASSERT_TRUE(visitor_.header_.get());
10237 EXPECT_TRUE(CheckDecryption(
10238 *encrypted, !kIncludeVersion, !kIncludeDiversificationNonce,
10239 PACKET_8BYTE_CONNECTION_ID, PACKET_0BYTE_CONNECTION_ID));
10240
10241 EXPECT_EQ(0u, visitor_.streams_blocked_frame_.stream_count);
10242 EXPECT_TRUE(visitor_.streams_blocked_frame_.unidirectional);
10243
10244 CheckFramingBoundaries(packet99, QUIC_STREAMS_BLOCKED_DATA);
QUICHE teama6ef0a62019-03-07 20:34:33 -050010245}
10246
fkastenholz3c4eabf2019-04-22 07:49:59 -070010247TEST_P(QuicFramerTest, BuildBiDiStreamsBlockedPacket) {
QUICHE teama6ef0a62019-03-07 20:34:33 -050010248 // This test only for version 99.
fkastenholz305e1732019-06-18 05:01:22 -070010249 if (!VersionHasIetfQuicFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -050010250 return;
10251 }
10252
10253 QuicPacketHeader header;
10254 header.destination_connection_id = FramerTestConnectionId();
10255 header.reset_flag = false;
10256 header.version_flag = false;
10257 header.packet_number = kPacketNumber;
10258
fkastenholz3c4eabf2019-04-22 07:49:59 -070010259 QuicStreamsBlockedFrame frame;
10260 frame.stream_count = 3;
10261 frame.unidirectional = false;
QUICHE teama6ef0a62019-03-07 20:34:33 -050010262
10263 QuicFrames frames = {QuicFrame(frame)};
10264
10265 // clang-format off
10266 unsigned char packet99[] = {
10267 // type (short header, 4 byte packet number)
10268 0x43,
10269 // connection_id
10270 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
10271 // packet number
10272 0x12, 0x34, 0x56, 0x78,
10273
10274 // frame type (IETF_STREAMS_BLOCKED_BIDIRECTIONAL frame)
10275 0x16,
10276 // Stream count
10277 kVarInt62OneByte + 0x03
10278 };
10279 // clang-format on
10280
10281 std::unique_ptr<QuicPacket> data(BuildDataPacket(header, frames));
10282 ASSERT_TRUE(data != nullptr);
10283
10284 test::CompareCharArraysWithHexError("constructed packet", data->data(),
10285 data->length(), AsChars(packet99),
10286 QUIC_ARRAYSIZE(packet99));
10287}
10288
fkastenholz3c4eabf2019-04-22 07:49:59 -070010289TEST_P(QuicFramerTest, BuildUniStreamsBlockedPacket) {
QUICHE teama6ef0a62019-03-07 20:34:33 -050010290 // This test only for version 99.
fkastenholz305e1732019-06-18 05:01:22 -070010291 if (!VersionHasIetfQuicFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -050010292 return;
10293 }
10294
10295 QuicPacketHeader header;
10296 header.destination_connection_id = FramerTestConnectionId();
10297 header.reset_flag = false;
10298 header.version_flag = false;
10299 header.packet_number = kPacketNumber;
10300
fkastenholz3c4eabf2019-04-22 07:49:59 -070010301 QuicStreamsBlockedFrame frame;
10302 frame.stream_count = 3;
10303 frame.unidirectional = true;
10304
QUICHE teama6ef0a62019-03-07 20:34:33 -050010305 QuicFrames frames = {QuicFrame(frame)};
10306
10307 // clang-format off
10308 unsigned char packet99[] = {
10309 // type (short header, 4 byte packet number)
10310 0x43,
10311 // connection_id
10312 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
10313 // packet number
10314 0x12, 0x34, 0x56, 0x78,
10315
10316 // frame type (IETF_STREAMS_BLOCKED_UNIDIRECTIONAL frame)
10317 0x17,
10318 // Stream count
10319 kVarInt62OneByte + 0x03
10320 };
10321 // clang-format on
10322
10323 std::unique_ptr<QuicPacket> data(BuildDataPacket(header, frames));
10324 ASSERT_TRUE(data != nullptr);
10325
10326 test::CompareCharArraysWithHexError("constructed packet", data->data(),
10327 data->length(), AsChars(packet99),
10328 QUIC_ARRAYSIZE(packet99));
10329}
10330
fkastenholz3c4eabf2019-04-22 07:49:59 -070010331TEST_P(QuicFramerTest, BuildBiDiMaxStreamsPacket) {
QUICHE teama6ef0a62019-03-07 20:34:33 -050010332 // This test only for version 99.
fkastenholz305e1732019-06-18 05:01:22 -070010333 if (!VersionHasIetfQuicFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -050010334 return;
10335 }
10336
10337 QuicPacketHeader header;
10338 header.destination_connection_id = FramerTestConnectionId();
10339 header.reset_flag = false;
10340 header.version_flag = false;
10341 header.packet_number = kPacketNumber;
10342
fkastenholz3c4eabf2019-04-22 07:49:59 -070010343 QuicMaxStreamsFrame frame;
10344 frame.stream_count = 3;
10345 frame.unidirectional = false;
10346
QUICHE teama6ef0a62019-03-07 20:34:33 -050010347 QuicFrames frames = {QuicFrame(frame)};
10348
10349 // clang-format off
10350 unsigned char packet99[] = {
10351 // type (short header, 4 byte packet number)
10352 0x43,
10353 // connection_id
10354 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
10355 // packet number
10356 0x12, 0x34, 0x56, 0x78,
10357
10358 // frame type (IETF_MAX_STREAMS_BIDIRECTIONAL frame)
10359 0x12,
10360 // Stream count
10361 kVarInt62OneByte + 0x03
10362 };
10363 // clang-format on
10364
10365 std::unique_ptr<QuicPacket> data(BuildDataPacket(header, frames));
10366 ASSERT_TRUE(data != nullptr);
10367
10368 test::CompareCharArraysWithHexError("constructed packet", data->data(),
10369 data->length(), AsChars(packet99),
10370 QUIC_ARRAYSIZE(packet99));
10371}
10372
fkastenholz3c4eabf2019-04-22 07:49:59 -070010373TEST_P(QuicFramerTest, BuildUniDiMaxStreamsPacket) {
QUICHE teama6ef0a62019-03-07 20:34:33 -050010374 // This test only for version 99.
fkastenholz305e1732019-06-18 05:01:22 -070010375 if (!VersionHasIetfQuicFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -050010376 return;
10377 }
10378
10379 // This test runs in client mode.
10380 QuicFramerPeer::SetPerspective(&framer_, Perspective::IS_CLIENT);
10381
10382 QuicPacketHeader header;
10383 header.destination_connection_id = FramerTestConnectionId();
10384 header.reset_flag = false;
10385 header.version_flag = false;
10386 header.packet_number = kPacketNumber;
10387
fkastenholz3c4eabf2019-04-22 07:49:59 -070010388 QuicMaxStreamsFrame frame;
10389 frame.stream_count = 3;
10390 frame.unidirectional = true;
QUICHE teama6ef0a62019-03-07 20:34:33 -050010391
QUICHE teama6ef0a62019-03-07 20:34:33 -050010392 QuicFrames frames = {QuicFrame(frame)};
10393
10394 // clang-format off
10395 unsigned char packet99[] = {
10396 // type (short header, 4 byte packet number)
10397 0x43,
10398 // connection_id
10399 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
10400 // packet number
10401 0x12, 0x34, 0x56, 0x78,
10402
10403 // frame type (IETF_MAX_STREAMS_UNIDIRECTIONAL frame)
10404 0x13,
10405 // Stream count
10406 kVarInt62OneByte + 0x03
10407 };
10408 // clang-format on
10409
10410 std::unique_ptr<QuicPacket> data(BuildDataPacket(header, frames));
10411 ASSERT_TRUE(data != nullptr);
10412
10413 test::CompareCharArraysWithHexError("constructed packet", data->data(),
10414 data->length(), AsChars(packet99),
10415 QUIC_ARRAYSIZE(packet99));
10416}
10417
10418TEST_P(QuicFramerTest, NewConnectionIdFrame) {
fkastenholz305e1732019-06-18 05:01:22 -070010419 if (!VersionHasIetfQuicFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -050010420 // This frame is only for version 99.
10421 return;
10422 }
zhongyi546cc452019-04-12 15:27:49 -070010423 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
QUICHE teama6ef0a62019-03-07 20:34:33 -050010424 // clang-format off
10425 PacketFragments packet99 = {
10426 // type (short header, 4 byte packet number)
10427 {"",
10428 {0x43}},
10429 // connection_id
10430 {"",
10431 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
10432 // packet number
10433 {"",
10434 {0x12, 0x34, 0x56, 0x78}},
10435 // frame type (IETF_NEW_CONNECTION_ID frame)
10436 {"",
10437 {0x18}},
10438 // error code
10439 {"Unable to read new connection ID frame sequence number.",
10440 {kVarInt62OneByte + 0x11}},
fkastenholz1c19fc22019-07-12 11:06:19 -070010441 {"Unable to read new connection ID frame retire_prior_to.",
10442 {kVarInt62OneByte + 0x09}},
dschinazicf5b1e22019-07-17 18:35:17 -070010443 {"Unable to read new connection ID frame connection id.",
QUICHE teama6ef0a62019-03-07 20:34:33 -050010444 {0x08}}, // connection ID length
10445 {"Unable to read new connection ID frame connection id.",
10446 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x11}},
10447 {"Can not read new connection ID frame reset token.",
10448 {0xb5, 0x69, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x00,
10449 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}}
10450 };
10451 // clang-format on
10452
10453 std::unique_ptr<QuicEncryptedPacket> encrypted(
10454 AssemblePacketFromFragments(packet99));
10455 EXPECT_TRUE(framer_.ProcessPacket(*encrypted));
10456
10457 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
10458 ASSERT_TRUE(visitor_.header_.get());
10459 EXPECT_TRUE(CheckDecryption(
10460 *encrypted, !kIncludeVersion, !kIncludeDiversificationNonce,
10461 PACKET_8BYTE_CONNECTION_ID, PACKET_0BYTE_CONNECTION_ID));
10462
10463 EXPECT_EQ(0u, visitor_.stream_frames_.size());
10464
10465 EXPECT_EQ(FramerTestConnectionIdPlusOne(),
10466 visitor_.new_connection_id_.connection_id);
10467 EXPECT_EQ(0x11u, visitor_.new_connection_id_.sequence_number);
fkastenholz1c19fc22019-07-12 11:06:19 -070010468 EXPECT_EQ(0x09u, visitor_.new_connection_id_.retire_prior_to);
QUICHE teama6ef0a62019-03-07 20:34:33 -050010469 EXPECT_EQ(kTestStatelessResetToken,
10470 visitor_.new_connection_id_.stateless_reset_token);
10471
10472 ASSERT_EQ(0u, visitor_.ack_frames_.size());
10473
10474 CheckFramingBoundaries(packet99, QUIC_INVALID_NEW_CONNECTION_ID_DATA);
10475}
10476
QUICHE team8e2e4532019-03-14 14:37:56 -070010477TEST_P(QuicFramerTest, NewConnectionIdFrameVariableLength) {
fkastenholz305e1732019-06-18 05:01:22 -070010478 if (!VersionHasIetfQuicFrames(framer_.transport_version())) {
QUICHE team9b41c972019-03-21 11:22:48 -070010479 // This frame is only for version 99.
QUICHE team8e2e4532019-03-14 14:37:56 -070010480 return;
10481 }
zhongyi546cc452019-04-12 15:27:49 -070010482 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
QUICHE team8e2e4532019-03-14 14:37:56 -070010483 // clang-format off
10484 PacketFragments packet99 = {
10485 // type (short header, 4 byte packet number)
10486 {"",
10487 {0x43}},
10488 // connection_id
10489 {"",
10490 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
10491 // packet number
10492 {"",
10493 {0x12, 0x34, 0x56, 0x78}},
10494 // frame type (IETF_NEW_CONNECTION_ID frame)
10495 {"",
10496 {0x18}},
10497 // error code
10498 {"Unable to read new connection ID frame sequence number.",
10499 {kVarInt62OneByte + 0x11}},
fkastenholz1c19fc22019-07-12 11:06:19 -070010500 {"Unable to read new connection ID frame retire_prior_to.",
10501 {kVarInt62OneByte + 0x0a}},
dschinazicf5b1e22019-07-17 18:35:17 -070010502 {"Unable to read new connection ID frame connection id.",
QUICHE team8e2e4532019-03-14 14:37:56 -070010503 {0x09}}, // connection ID length
10504 {"Unable to read new connection ID frame connection id.",
10505 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10, 0x42}},
10506 {"Can not read new connection ID frame reset token.",
10507 {0xb5, 0x69, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x00,
10508 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}}
10509 };
10510 // clang-format on
10511
10512 std::unique_ptr<QuicEncryptedPacket> encrypted(
10513 AssemblePacketFromFragments(packet99));
10514 EXPECT_TRUE(framer_.ProcessPacket(*encrypted));
10515
10516 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
10517 ASSERT_TRUE(visitor_.header_.get());
10518 EXPECT_TRUE(CheckDecryption(
10519 *encrypted, !kIncludeVersion, !kIncludeDiversificationNonce,
10520 PACKET_8BYTE_CONNECTION_ID, PACKET_0BYTE_CONNECTION_ID));
10521
10522 EXPECT_EQ(0u, visitor_.stream_frames_.size());
10523
10524 EXPECT_EQ(FramerTestConnectionIdNineBytes(),
10525 visitor_.new_connection_id_.connection_id);
10526 EXPECT_EQ(0x11u, visitor_.new_connection_id_.sequence_number);
fkastenholz1c19fc22019-07-12 11:06:19 -070010527 EXPECT_EQ(0x0au, visitor_.new_connection_id_.retire_prior_to);
QUICHE team8e2e4532019-03-14 14:37:56 -070010528 EXPECT_EQ(kTestStatelessResetToken,
10529 visitor_.new_connection_id_.stateless_reset_token);
10530
10531 ASSERT_EQ(0u, visitor_.ack_frames_.size());
10532
10533 CheckFramingBoundaries(packet99, QUIC_INVALID_NEW_CONNECTION_ID_DATA);
10534}
10535
QUICHE team0131a5b2019-03-20 15:23:27 -070010536// Verifies that parsing a NEW_CONNECTION_ID frame with a length above the
10537// specified maximum fails.
10538TEST_P(QuicFramerTest, InvalidLongNewConnectionIdFrame) {
fkastenholz305e1732019-06-18 05:01:22 -070010539 if (!VersionHasIetfQuicFrames(framer_.transport_version())) {
QUICHE team0131a5b2019-03-20 15:23:27 -070010540 // The NEW_CONNECTION_ID frame is only for version 99.
10541 return;
10542 }
zhongyi546cc452019-04-12 15:27:49 -070010543 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
QUICHE team0131a5b2019-03-20 15:23:27 -070010544 // clang-format off
10545 PacketFragments packet99 = {
10546 // type (short header, 4 byte packet number)
10547 {"",
10548 {0x43}},
10549 // connection_id
10550 {"",
10551 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
10552 // packet number
10553 {"",
10554 {0x12, 0x34, 0x56, 0x78}},
10555 // frame type (IETF_NEW_CONNECTION_ID frame)
10556 {"",
10557 {0x18}},
10558 // error code
10559 {"Unable to read new connection ID frame sequence number.",
10560 {kVarInt62OneByte + 0x11}},
fkastenholz1c19fc22019-07-12 11:06:19 -070010561 {"Unable to read new connection ID frame retire_prior_to.",
10562 {kVarInt62OneByte + 0x0b}},
dschinazicf5b1e22019-07-17 18:35:17 -070010563 {"Unable to read new connection ID frame connection id.",
dschinazib953d022019-08-01 18:05:58 -070010564 {0x40}}, // connection ID length
QUICHE team0131a5b2019-03-20 15:23:27 -070010565 {"Unable to read new connection ID frame connection id.",
10566 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
10567 0xF0, 0xD2, 0xB4, 0x96, 0x78, 0x5A, 0x3C, 0x1E,
dschinazib953d022019-08-01 18:05:58 -070010568 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
10569 0xF0, 0xD2, 0xB4, 0x96, 0x78, 0x5A, 0x3C, 0x1E,
10570 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
10571 0xF0, 0xD2, 0xB4, 0x96, 0x78, 0x5A, 0x3C, 0x1E,
10572 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
10573 0xF0, 0xD2, 0xB4, 0x96, 0x78, 0x5A, 0x3C, 0x1E}},
QUICHE team0131a5b2019-03-20 15:23:27 -070010574 {"Can not read new connection ID frame reset token.",
10575 {0xb5, 0x69, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x00,
10576 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}}
10577 };
10578 // clang-format on
10579
10580 std::unique_ptr<QuicEncryptedPacket> encrypted(
10581 AssemblePacketFromFragments(packet99));
10582 EXPECT_FALSE(framer_.ProcessPacket(*encrypted));
10583 EXPECT_EQ(QUIC_INVALID_NEW_CONNECTION_ID_DATA, framer_.error());
dschinazicf5b1e22019-07-17 18:35:17 -070010584 EXPECT_EQ("Unable to read new connection ID frame connection id.",
10585 framer_.detailed_error());
QUICHE team0131a5b2019-03-20 15:23:27 -070010586}
10587
fkastenholz1c19fc22019-07-12 11:06:19 -070010588// Verifies that parsing a NEW_CONNECTION_ID frame with an invalid
10589// retire-prior-to fails.
10590TEST_P(QuicFramerTest, InvalidRetirePriorToNewConnectionIdFrame) {
10591 if (!VersionHasIetfQuicFrames(framer_.transport_version())) {
10592 // The NEW_CONNECTION_ID frame is only for version 99.
10593 return;
10594 }
10595 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
10596 // clang-format off
10597 PacketFragments packet99 = {
10598 // type (short header, 4 byte packet number)
10599 {"",
10600 {0x43}},
10601 // connection_id
10602 {"",
10603 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
10604 // packet number
10605 {"",
10606 {0x12, 0x34, 0x56, 0x78}},
10607 // frame type (IETF_NEW_CONNECTION_ID frame)
10608 {"",
10609 {0x18}},
10610 // sequence number
10611 {"Unable to read new connection ID frame sequence number.",
10612 {kVarInt62OneByte + 0x11}},
10613 {"Unable to read new connection ID frame retire_prior_to.",
10614 {kVarInt62OneByte + 0x1b}},
10615 {"Unable to read new connection ID frame connection id length.",
10616 {0x08}}, // connection ID length
10617 {"Unable to read new connection ID frame connection id.",
10618 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x11}},
10619 {"Can not read new connection ID frame reset token.",
10620 {0xb5, 0x69, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x00,
10621 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}}
10622 };
10623 // clang-format on
10624
10625 std::unique_ptr<QuicEncryptedPacket> encrypted(
10626 AssemblePacketFromFragments(packet99));
10627 EXPECT_FALSE(framer_.ProcessPacket(*encrypted));
10628 EXPECT_EQ(QUIC_INVALID_NEW_CONNECTION_ID_DATA, framer_.error());
10629 EXPECT_EQ("Retire_prior_to > sequence_number.", framer_.detailed_error());
10630}
10631
QUICHE teama6ef0a62019-03-07 20:34:33 -050010632TEST_P(QuicFramerTest, BuildNewConnectionIdFramePacket) {
fkastenholz305e1732019-06-18 05:01:22 -070010633 if (!VersionHasIetfQuicFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -050010634 // This frame is only for version 99.
10635 return;
10636 }
QUICHE team2252b702019-05-14 23:55:14 -040010637 QuicFramerPeer::SetPerspective(&framer_, Perspective::IS_CLIENT);
QUICHE teama6ef0a62019-03-07 20:34:33 -050010638 QuicPacketHeader header;
10639 header.destination_connection_id = FramerTestConnectionId();
10640 header.reset_flag = false;
10641 header.version_flag = false;
10642 header.packet_number = kPacketNumber;
10643
10644 QuicNewConnectionIdFrame frame;
10645 frame.sequence_number = 0x11;
fkastenholz1c19fc22019-07-12 11:06:19 -070010646 frame.retire_prior_to = 0x0c;
QUICHE teama6ef0a62019-03-07 20:34:33 -050010647 // Use this value to force a 4-byte encoded variable length connection ID
10648 // in the frame.
10649 frame.connection_id = FramerTestConnectionIdPlusOne();
10650 frame.stateless_reset_token = kTestStatelessResetToken;
10651
10652 QuicFrames frames = {QuicFrame(&frame)};
10653
10654 // clang-format off
10655 unsigned char packet99[] = {
10656 // type (short header, 4 byte packet number)
10657 0x43,
10658 // connection_id
10659 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
10660 // packet number
10661 0x12, 0x34, 0x56, 0x78,
10662
10663 // frame type (IETF_NEW_CONNECTION_ID frame)
10664 0x18,
10665 // sequence number
10666 kVarInt62OneByte + 0x11,
fkastenholz1c19fc22019-07-12 11:06:19 -070010667 // retire_prior_to
10668 kVarInt62OneByte + 0x0c,
QUICHE teama6ef0a62019-03-07 20:34:33 -050010669 // new connection id length
10670 0x08,
10671 // new connection id
10672 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x11,
10673 // stateless reset token
10674 0xb5, 0x69, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x00,
10675 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
10676 };
10677 // clang-format on
10678
10679 std::unique_ptr<QuicPacket> data(BuildDataPacket(header, frames));
10680 ASSERT_TRUE(data != nullptr);
10681
10682 test::CompareCharArraysWithHexError("constructed packet", data->data(),
10683 data->length(), AsChars(packet99),
10684 QUIC_ARRAYSIZE(packet99));
10685}
10686
10687TEST_P(QuicFramerTest, NewTokenFrame) {
fkastenholz305e1732019-06-18 05:01:22 -070010688 if (!VersionHasIetfQuicFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -050010689 // This frame is only for version 99.
10690 return;
10691 }
zhongyi546cc452019-04-12 15:27:49 -070010692 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
QUICHE teama6ef0a62019-03-07 20:34:33 -050010693 // clang-format off
10694 PacketFragments packet = {
10695 // type (short header, 4 byte packet number)
10696 {"",
10697 {0x43}},
10698 // connection_id
10699 {"",
10700 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
10701 // packet number
10702 {"",
10703 {0x12, 0x34, 0x56, 0x78}},
10704 // frame type (IETF_NEW_TOKEN frame)
10705 {"",
10706 {0x07}},
10707 // Length
10708 {"Unable to read new token length.",
10709 {kVarInt62OneByte + 0x08}},
10710 {"Unable to read new token data.",
10711 {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}}
10712 };
10713 // clang-format on
10714 uint8_t expected_token_value[] = {0x00, 0x01, 0x02, 0x03,
10715 0x04, 0x05, 0x06, 0x07};
10716
10717 std::unique_ptr<QuicEncryptedPacket> encrypted(
10718 AssemblePacketFromFragments(packet));
10719 EXPECT_TRUE(framer_.ProcessPacket(*encrypted));
10720
10721 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
10722 ASSERT_TRUE(visitor_.header_.get());
10723 EXPECT_TRUE(CheckDecryption(
10724 *encrypted, !kIncludeVersion, !kIncludeDiversificationNonce,
10725 PACKET_8BYTE_CONNECTION_ID, PACKET_0BYTE_CONNECTION_ID));
10726
10727 EXPECT_EQ(0u, visitor_.stream_frames_.size());
10728
10729 EXPECT_EQ(sizeof(expected_token_value), visitor_.new_token_.token.length());
10730 EXPECT_EQ(0, memcmp(expected_token_value, visitor_.new_token_.token.data(),
10731 sizeof(expected_token_value)));
10732
10733 CheckFramingBoundaries(packet, QUIC_INVALID_NEW_TOKEN);
10734}
10735
10736TEST_P(QuicFramerTest, BuildNewTokenFramePacket) {
fkastenholz305e1732019-06-18 05:01:22 -070010737 if (!VersionHasIetfQuicFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -050010738 // This frame is only for version 99.
10739 return;
10740 }
QUICHE team2252b702019-05-14 23:55:14 -040010741 QuicFramerPeer::SetPerspective(&framer_, Perspective::IS_CLIENT);
QUICHE teama6ef0a62019-03-07 20:34:33 -050010742 QuicPacketHeader header;
10743 header.destination_connection_id = FramerTestConnectionId();
10744 header.reset_flag = false;
10745 header.version_flag = false;
10746 header.packet_number = kPacketNumber;
10747
10748 uint8_t expected_token_value[] = {0x00, 0x01, 0x02, 0x03,
10749 0x04, 0x05, 0x06, 0x07};
10750
vasilvvc48c8712019-03-11 13:38:16 -070010751 QuicNewTokenFrame frame(0, std::string((const char*)(expected_token_value),
10752 sizeof(expected_token_value)));
QUICHE teama6ef0a62019-03-07 20:34:33 -050010753
10754 QuicFrames frames = {QuicFrame(&frame)};
10755
10756 // clang-format off
10757 unsigned char packet[] = {
10758 // type (short header, 4 byte packet number)
10759 0x43,
10760 // connection_id
10761 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
10762 // packet number
10763 0x12, 0x34, 0x56, 0x78,
10764
10765 // frame type (IETF_NEW_TOKEN frame)
10766 0x07,
10767 // Length and token
10768 kVarInt62OneByte + 0x08,
10769 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07
10770 };
10771 // clang-format on
10772
10773 std::unique_ptr<QuicPacket> data(BuildDataPacket(header, frames));
10774 ASSERT_TRUE(data != nullptr);
10775
10776 test::CompareCharArraysWithHexError("constructed packet", data->data(),
10777 data->length(), AsChars(packet),
10778 QUIC_ARRAYSIZE(packet));
10779}
10780
10781TEST_P(QuicFramerTest, IetfStopSendingFrame) {
10782 // This test is only for version 99.
fkastenholz305e1732019-06-18 05:01:22 -070010783 if (!VersionHasIetfQuicFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -050010784 return;
10785 }
zhongyi546cc452019-04-12 15:27:49 -070010786 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
QUICHE teama6ef0a62019-03-07 20:34:33 -050010787
10788 // clang-format off
10789 PacketFragments packet99 = {
10790 // type (short header, 4 byte packet number)
10791 {"",
10792 {0x43}},
10793 // connection_id
10794 {"",
10795 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
10796 // packet number
10797 {"",
10798 {0x12, 0x34, 0x9A, 0xBC}},
10799 // frame type (IETF_STOP_SENDING frame)
10800 {"",
10801 {0x05}},
10802 // stream id
10803 {"Unable to read stop sending stream id.",
10804 {kVarInt62FourBytes + 0x01, 0x02, 0x03, 0x04}},
10805 {"Unable to read stop sending application error code.",
fkastenholz733552e2019-07-16 11:16:58 -070010806 {kVarInt62FourBytes + 0x00, 0x00, 0x76, 0x54}},
QUICHE teama6ef0a62019-03-07 20:34:33 -050010807 };
10808 // clang-format on
10809
10810 std::unique_ptr<QuicEncryptedPacket> encrypted(
10811 AssemblePacketFromFragments(packet99));
10812 EXPECT_TRUE(framer_.ProcessPacket(*encrypted));
10813
10814 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
10815 ASSERT_TRUE(visitor_.header_.get());
10816 EXPECT_TRUE(CheckDecryption(
10817 *encrypted, !kIncludeVersion, !kIncludeDiversificationNonce,
10818 PACKET_8BYTE_CONNECTION_ID, PACKET_0BYTE_CONNECTION_ID));
10819
10820 EXPECT_EQ(kStreamId, visitor_.stop_sending_frame_.stream_id);
10821 EXPECT_EQ(0x7654, visitor_.stop_sending_frame_.application_error_code);
10822
10823 CheckFramingBoundaries(packet99, QUIC_INVALID_STOP_SENDING_FRAME_DATA);
10824}
10825
10826TEST_P(QuicFramerTest, BuildIetfStopSendingPacket) {
10827 // This test is only for version 99.
fkastenholz305e1732019-06-18 05:01:22 -070010828 if (!VersionHasIetfQuicFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -050010829 return;
10830 }
10831
10832 QuicPacketHeader header;
10833 header.destination_connection_id = FramerTestConnectionId();
10834 header.reset_flag = false;
10835 header.version_flag = false;
10836 header.packet_number = kPacketNumber;
10837
10838 QuicStopSendingFrame frame;
10839 frame.stream_id = kStreamId;
10840 frame.application_error_code = 0xffff;
10841 QuicFrames frames = {QuicFrame(&frame)};
10842
10843 // clang-format off
10844 unsigned char packet99[] = {
10845 // type (short header, 4 byte packet number)
10846 0x43,
10847 // connection_id
10848 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
10849 // packet number
10850 0x12, 0x34, 0x56, 0x78,
10851
10852 // frame type (IETF_STOP_SENDING frame)
10853 0x05,
10854 // Stream ID
10855 kVarInt62FourBytes + 0x01, 0x02, 0x03, 0x04,
10856 // Application error code
fkastenholz733552e2019-07-16 11:16:58 -070010857 kVarInt62FourBytes + 0x00, 0x00, 0xff, 0xff
QUICHE teama6ef0a62019-03-07 20:34:33 -050010858 };
10859 // clang-format on
10860
10861 std::unique_ptr<QuicPacket> data(BuildDataPacket(header, frames));
10862 ASSERT_TRUE(data != nullptr);
10863
10864 test::CompareCharArraysWithHexError("constructed packet", data->data(),
10865 data->length(), AsChars(packet99),
10866 QUIC_ARRAYSIZE(packet99));
10867}
10868
10869TEST_P(QuicFramerTest, IetfPathChallengeFrame) {
10870 // This test only for version 99.
fkastenholz305e1732019-06-18 05:01:22 -070010871 if (!VersionHasIetfQuicFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -050010872 return;
10873 }
zhongyi546cc452019-04-12 15:27:49 -070010874 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
QUICHE teama6ef0a62019-03-07 20:34:33 -050010875
10876 // clang-format off
10877 PacketFragments packet99 = {
10878 // type (short header, 4 byte packet number)
10879 {"",
10880 {0x43}},
10881 // connection_id
10882 {"",
10883 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
10884 // packet number
10885 {"",
10886 {0x12, 0x34, 0x9A, 0xBC}},
10887 // frame type (IETF_PATH_CHALLENGE)
10888 {"",
10889 {0x1a}},
10890 // data
10891 {"Can not read path challenge data.",
10892 {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}},
10893 };
10894 // clang-format on
10895
10896 std::unique_ptr<QuicEncryptedPacket> encrypted(
10897 AssemblePacketFromFragments(packet99));
10898 EXPECT_TRUE(framer_.ProcessPacket(*encrypted));
10899
10900 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
10901 ASSERT_TRUE(visitor_.header_.get());
10902 EXPECT_TRUE(CheckDecryption(
10903 *encrypted, !kIncludeVersion, !kIncludeDiversificationNonce,
10904 PACKET_8BYTE_CONNECTION_ID, PACKET_0BYTE_CONNECTION_ID));
10905
10906 EXPECT_EQ(QuicPathFrameBuffer({{0, 1, 2, 3, 4, 5, 6, 7}}),
10907 visitor_.path_challenge_frame_.data_buffer);
10908
10909 CheckFramingBoundaries(packet99, QUIC_INVALID_PATH_CHALLENGE_DATA);
10910}
10911
10912TEST_P(QuicFramerTest, BuildIetfPathChallengePacket) {
10913 // This test only for version 99.
fkastenholz305e1732019-06-18 05:01:22 -070010914 if (!VersionHasIetfQuicFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -050010915 return;
10916 }
10917
10918 QuicPacketHeader header;
10919 header.destination_connection_id = FramerTestConnectionId();
10920 header.reset_flag = false;
10921 header.version_flag = false;
10922 header.packet_number = kPacketNumber;
10923
10924 QuicPathChallengeFrame frame;
10925 frame.data_buffer = QuicPathFrameBuffer({{0, 1, 2, 3, 4, 5, 6, 7}});
10926 QuicFrames frames = {QuicFrame(&frame)};
10927
10928 // clang-format off
10929 unsigned char packet99[] = {
10930 // type (short header, 4 byte packet number)
10931 0x43,
10932 // connection_id
10933 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
10934 // packet number
10935 0x12, 0x34, 0x56, 0x78,
10936
10937 // frame type (IETF_PATH_CHALLENGE)
10938 0x1a,
10939 // Data
10940 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07
10941 };
10942 // clang-format on
10943
10944 std::unique_ptr<QuicPacket> data(BuildDataPacket(header, frames));
10945 ASSERT_TRUE(data != nullptr);
10946
10947 test::CompareCharArraysWithHexError("constructed packet", data->data(),
10948 data->length(), AsChars(packet99),
10949 QUIC_ARRAYSIZE(packet99));
10950}
10951
10952TEST_P(QuicFramerTest, IetfPathResponseFrame) {
10953 // This test only for version 99.
fkastenholz305e1732019-06-18 05:01:22 -070010954 if (!VersionHasIetfQuicFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -050010955 return;
10956 }
zhongyi546cc452019-04-12 15:27:49 -070010957 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
QUICHE teama6ef0a62019-03-07 20:34:33 -050010958
10959 // clang-format off
10960 PacketFragments packet99 = {
10961 // type (short header, 4 byte packet number)
10962 {"",
10963 {0x43}},
10964 // connection_id
10965 {"",
10966 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
10967 // packet number
10968 {"",
10969 {0x12, 0x34, 0x9A, 0xBC}},
10970 // frame type (IETF_PATH_RESPONSE)
10971 {"",
10972 {0x1b}},
10973 // data
10974 {"Can not read path response data.",
10975 {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}},
10976 };
10977 // clang-format on
10978
10979 std::unique_ptr<QuicEncryptedPacket> encrypted(
10980 AssemblePacketFromFragments(packet99));
10981 EXPECT_TRUE(framer_.ProcessPacket(*encrypted));
10982
10983 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
10984 ASSERT_TRUE(visitor_.header_.get());
10985 EXPECT_TRUE(CheckDecryption(
10986 *encrypted, !kIncludeVersion, !kIncludeDiversificationNonce,
10987 PACKET_8BYTE_CONNECTION_ID, PACKET_0BYTE_CONNECTION_ID));
10988
10989 EXPECT_EQ(QuicPathFrameBuffer({{0, 1, 2, 3, 4, 5, 6, 7}}),
10990 visitor_.path_response_frame_.data_buffer);
10991
10992 CheckFramingBoundaries(packet99, QUIC_INVALID_PATH_RESPONSE_DATA);
10993}
10994
10995TEST_P(QuicFramerTest, BuildIetfPathResponsePacket) {
10996 // This test only for version 99.
fkastenholz305e1732019-06-18 05:01:22 -070010997 if (!VersionHasIetfQuicFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -050010998 return;
10999 }
11000
11001 QuicPacketHeader header;
11002 header.destination_connection_id = FramerTestConnectionId();
11003 header.reset_flag = false;
11004 header.version_flag = false;
11005 header.packet_number = kPacketNumber;
11006
11007 QuicPathResponseFrame frame;
11008 frame.data_buffer = QuicPathFrameBuffer({{0, 1, 2, 3, 4, 5, 6, 7}});
11009 QuicFrames frames = {QuicFrame(&frame)};
11010
11011 // clang-format off
11012 unsigned char packet99[] = {
11013 // type (short header, 4 byte packet number)
11014 0x43,
11015 // connection_id
11016 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
11017 // packet number
11018 0x12, 0x34, 0x56, 0x78,
11019
11020 // frame type (IETF_PATH_RESPONSE)
11021 0x1b,
11022 // Data
11023 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07
11024 };
11025 // clang-format on
11026
11027 std::unique_ptr<QuicPacket> data(BuildDataPacket(header, frames));
11028 ASSERT_TRUE(data != nullptr);
11029
11030 test::CompareCharArraysWithHexError("constructed packet", data->data(),
11031 data->length(), AsChars(packet99),
11032 QUIC_ARRAYSIZE(packet99));
11033}
11034
11035TEST_P(QuicFramerTest, GetRetransmittableControlFrameSize) {
11036 QuicRstStreamFrame rst_stream(1, 3, QUIC_STREAM_CANCELLED, 1024);
11037 EXPECT_EQ(QuicFramer::GetRstStreamFrameSize(framer_.transport_version(),
11038 rst_stream),
11039 QuicFramer::GetRetransmittableControlFrameSize(
11040 framer_.transport_version(), QuicFrame(&rst_stream)));
11041
vasilvvc48c8712019-03-11 13:38:16 -070011042 std::string error_detail(2048, 'e');
wuba0cbbdc2019-09-09 06:40:59 -070011043 QuicConnectionCloseFrame connection_close(
11044 framer_.transport_version(), QUIC_NETWORK_IDLE_TIMEOUT, error_detail,
11045 /*transport_close_frame_type=*/0);
fkastenholz72f509b2019-04-10 09:17:49 -070011046
fkastenholza037b8b2019-05-07 06:00:05 -070011047 EXPECT_EQ(QuicFramer::GetConnectionCloseFrameSize(framer_.transport_version(),
11048 connection_close),
QUICHE teama6ef0a62019-03-07 20:34:33 -050011049 QuicFramer::GetRetransmittableControlFrameSize(
11050 framer_.transport_version(), QuicFrame(&connection_close)));
11051
11052 QuicGoAwayFrame goaway(2, QUIC_PEER_GOING_AWAY, 3, error_detail);
11053 EXPECT_EQ(QuicFramer::GetMinGoAwayFrameSize() + 256,
11054 QuicFramer::GetRetransmittableControlFrameSize(
11055 framer_.transport_version(), QuicFrame(&goaway)));
11056
11057 QuicWindowUpdateFrame window_update(3, 3, 1024);
11058 EXPECT_EQ(QuicFramer::GetWindowUpdateFrameSize(framer_.transport_version(),
11059 window_update),
11060 QuicFramer::GetRetransmittableControlFrameSize(
11061 framer_.transport_version(), QuicFrame(&window_update)));
11062
11063 QuicBlockedFrame blocked(4, 3, 1024);
11064 EXPECT_EQ(
11065 QuicFramer::GetBlockedFrameSize(framer_.transport_version(), blocked),
11066 QuicFramer::GetRetransmittableControlFrameSize(
11067 framer_.transport_version(), QuicFrame(&blocked)));
11068
fkastenholz305e1732019-06-18 05:01:22 -070011069 if (!VersionHasIetfQuicFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -050011070 return;
11071 }
QUICHE teama6ef0a62019-03-07 20:34:33 -050011072
fkastenholz1c19fc22019-07-12 11:06:19 -070011073 QuicNewConnectionIdFrame new_connection_id(5, TestConnectionId(), 1, 101111,
11074 1);
QUICHE teama6ef0a62019-03-07 20:34:33 -050011075 EXPECT_EQ(QuicFramer::GetNewConnectionIdFrameSize(new_connection_id),
11076 QuicFramer::GetRetransmittableControlFrameSize(
11077 framer_.transport_version(), QuicFrame(&new_connection_id)));
11078
fkastenholz3c4eabf2019-04-22 07:49:59 -070011079 QuicMaxStreamsFrame max_streams(6, 3, /*unidirectional=*/false);
QUICHE teama6ef0a62019-03-07 20:34:33 -050011080 EXPECT_EQ(QuicFramer::GetMaxStreamsFrameSize(framer_.transport_version(),
fkastenholz3c4eabf2019-04-22 07:49:59 -070011081 max_streams),
QUICHE teama6ef0a62019-03-07 20:34:33 -050011082 QuicFramer::GetRetransmittableControlFrameSize(
fkastenholz3c4eabf2019-04-22 07:49:59 -070011083 framer_.transport_version(), QuicFrame(max_streams)));
QUICHE teama6ef0a62019-03-07 20:34:33 -050011084
fkastenholz3c4eabf2019-04-22 07:49:59 -070011085 QuicStreamsBlockedFrame streams_blocked(7, 3, /*unidirectional=*/false);
QUICHE teama6ef0a62019-03-07 20:34:33 -050011086 EXPECT_EQ(QuicFramer::GetStreamsBlockedFrameSize(framer_.transport_version(),
fkastenholz3c4eabf2019-04-22 07:49:59 -070011087 streams_blocked),
QUICHE teama6ef0a62019-03-07 20:34:33 -050011088 QuicFramer::GetRetransmittableControlFrameSize(
fkastenholz3c4eabf2019-04-22 07:49:59 -070011089 framer_.transport_version(), QuicFrame(streams_blocked)));
QUICHE teama6ef0a62019-03-07 20:34:33 -050011090
11091 QuicPathFrameBuffer buffer = {
11092 {0x80, 0x91, 0xa2, 0xb3, 0xc4, 0xd5, 0xe5, 0xf7}};
11093 QuicPathResponseFrame path_response_frame(8, buffer);
11094 EXPECT_EQ(QuicFramer::GetPathResponseFrameSize(path_response_frame),
11095 QuicFramer::GetRetransmittableControlFrameSize(
11096 framer_.transport_version(), QuicFrame(&path_response_frame)));
11097
11098 QuicPathChallengeFrame path_challenge_frame(9, buffer);
11099 EXPECT_EQ(QuicFramer::GetPathChallengeFrameSize(path_challenge_frame),
11100 QuicFramer::GetRetransmittableControlFrameSize(
11101 framer_.transport_version(), QuicFrame(&path_challenge_frame)));
11102
11103 QuicStopSendingFrame stop_sending_frame(10, 3, 20);
11104 EXPECT_EQ(QuicFramer::GetStopSendingFrameSize(stop_sending_frame),
11105 QuicFramer::GetRetransmittableControlFrameSize(
11106 framer_.transport_version(), QuicFrame(&stop_sending_frame)));
11107}
11108
11109// A set of tests to ensure that bad frame-type encodings
11110// are properly detected and handled.
11111// First, four tests to see that unknown frame types generate
11112// a QUIC_INVALID_FRAME_DATA error with detailed information
11113// "Illegal frame type." This regardless of the encoding of the type
11114// (1/2/4/8 bytes).
11115// This only for version 99.
11116TEST_P(QuicFramerTest, IetfFrameTypeEncodingErrorUnknown1Byte) {
11117 // This test only for version 99.
fkastenholz305e1732019-06-18 05:01:22 -070011118 if (!VersionHasIetfQuicFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -050011119 return;
11120 }
zhongyi546cc452019-04-12 15:27:49 -070011121 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
QUICHE teama6ef0a62019-03-07 20:34:33 -050011122 // clang-format off
11123 PacketFragments packet = {
11124 // type (short header, 4 byte packet number)
11125 {"",
11126 {0x43}},
11127 // connection_id
11128 {"",
11129 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
11130 // packet number
11131 {"",
11132 {0x12, 0x34, 0x9A, 0xBC}},
11133 // frame type (unknown value, single-byte encoding)
11134 {"",
11135 {0x38}}
11136 };
11137 // clang-format on
11138
11139 std::unique_ptr<QuicEncryptedPacket> encrypted(
11140 AssemblePacketFromFragments(packet));
11141
11142 EXPECT_FALSE(framer_.ProcessPacket(*encrypted));
11143
11144 EXPECT_EQ(QUIC_INVALID_FRAME_DATA, framer_.error());
11145 EXPECT_EQ("Illegal frame type.", framer_.detailed_error());
11146}
11147
11148TEST_P(QuicFramerTest, IetfFrameTypeEncodingErrorUnknown2Bytes) {
11149 // This test only for version 99.
fkastenholz305e1732019-06-18 05:01:22 -070011150 if (!VersionHasIetfQuicFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -050011151 return;
11152 }
zhongyi546cc452019-04-12 15:27:49 -070011153 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
QUICHE teama6ef0a62019-03-07 20:34:33 -050011154
11155 // clang-format off
11156 PacketFragments packet = {
11157 // type (short header, 4 byte packet number)
11158 {"",
11159 {0x43}},
11160 // connection_id
11161 {"",
11162 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
11163 // packet number
11164 {"",
11165 {0x12, 0x34, 0x9A, 0xBC}},
11166 // frame type (unknown value, two-byte encoding)
11167 {"",
11168 {kVarInt62TwoBytes + 0x01, 0x38}}
11169 };
11170 // clang-format on
11171
11172 std::unique_ptr<QuicEncryptedPacket> encrypted(
11173 AssemblePacketFromFragments(packet));
11174
11175 EXPECT_FALSE(framer_.ProcessPacket(*encrypted));
11176
11177 EXPECT_EQ(QUIC_INVALID_FRAME_DATA, framer_.error());
11178 EXPECT_EQ("Illegal frame type.", framer_.detailed_error());
11179}
11180
11181TEST_P(QuicFramerTest, IetfFrameTypeEncodingErrorUnknown4Bytes) {
11182 // This test only for version 99.
fkastenholz305e1732019-06-18 05:01:22 -070011183 if (!VersionHasIetfQuicFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -050011184 return;
11185 }
zhongyi546cc452019-04-12 15:27:49 -070011186 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
QUICHE teama6ef0a62019-03-07 20:34:33 -050011187
11188 // clang-format off
11189 PacketFragments packet = {
11190 // type (short header, 4 byte packet number)
11191 {"",
11192 {0x43}},
11193 // connection_id
11194 {"",
11195 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
11196 // packet number
11197 {"",
11198 {0x12, 0x34, 0x9A, 0xBC}},
11199 // frame type (unknown value, four-byte encoding)
11200 {"",
11201 {kVarInt62FourBytes + 0x01, 0x00, 0x00, 0x38}}
11202 };
11203 // clang-format on
11204
11205 std::unique_ptr<QuicEncryptedPacket> encrypted(
11206 AssemblePacketFromFragments(packet));
11207
11208 EXPECT_FALSE(framer_.ProcessPacket(*encrypted));
11209
11210 EXPECT_EQ(QUIC_INVALID_FRAME_DATA, framer_.error());
11211 EXPECT_EQ("Illegal frame type.", framer_.detailed_error());
11212}
11213
11214TEST_P(QuicFramerTest, IetfFrameTypeEncodingErrorUnknown8Bytes) {
11215 // This test only for version 99.
fkastenholz305e1732019-06-18 05:01:22 -070011216 if (!VersionHasIetfQuicFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -050011217 return;
11218 }
zhongyi546cc452019-04-12 15:27:49 -070011219 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
QUICHE teama6ef0a62019-03-07 20:34:33 -050011220 // clang-format off
11221 PacketFragments packet = {
11222 // type (short header, 4 byte packet number)
11223 {"",
11224 {0x43}},
11225 // connection_id
11226 {"",
11227 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
11228 // packet number
11229 {"",
11230 {0x12, 0x34, 0x9A, 0xBC}},
11231 // frame type (unknown value, eight-byte encoding)
11232 {"",
11233 {kVarInt62EightBytes + 0x01, 0x00, 0x00, 0x01, 0x02, 0x34, 0x56, 0x38}}
11234 };
11235 // clang-format on
11236
11237 std::unique_ptr<QuicEncryptedPacket> encrypted(
11238 AssemblePacketFromFragments(packet));
11239
11240 EXPECT_FALSE(framer_.ProcessPacket(*encrypted));
11241
11242 EXPECT_EQ(QUIC_INVALID_FRAME_DATA, framer_.error());
11243 EXPECT_EQ("Illegal frame type.", framer_.detailed_error());
11244}
11245
11246// Three tests to check that known frame types that are not minimally
11247// encoded generate IETF_QUIC_PROTOCOL_VIOLATION errors with detailed
11248// information "Frame type not minimally encoded."
11249// Look at the frame-type encoded in 2, 4, and 8 bytes.
11250TEST_P(QuicFramerTest, IetfFrameTypeEncodingErrorKnown2Bytes) {
11251 // This test only for version 99.
fkastenholz305e1732019-06-18 05:01:22 -070011252 if (!VersionHasIetfQuicFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -050011253 return;
11254 }
zhongyi546cc452019-04-12 15:27:49 -070011255 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
QUICHE teama6ef0a62019-03-07 20:34:33 -050011256
11257 // clang-format off
11258 PacketFragments packet = {
11259 // type (short header, 4 byte packet number)
11260 {"",
11261 {0x43}},
11262 // connection_id
11263 {"",
11264 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
11265 // packet number
11266 {"",
11267 {0x12, 0x34, 0x9A, 0xBC}},
11268 // frame type (Blocked, two-byte encoding)
11269 {"",
11270 {kVarInt62TwoBytes + 0x00, 0x08}}
11271 };
11272 // clang-format on
11273
11274 std::unique_ptr<QuicEncryptedPacket> encrypted(
11275 AssemblePacketFromFragments(packet));
11276
11277 EXPECT_FALSE(framer_.ProcessPacket(*encrypted));
11278
11279 EXPECT_EQ(IETF_QUIC_PROTOCOL_VIOLATION, framer_.error());
11280 EXPECT_EQ("Frame type not minimally encoded.", framer_.detailed_error());
11281}
11282
11283TEST_P(QuicFramerTest, IetfFrameTypeEncodingErrorKnown4Bytes) {
11284 // This test only for version 99.
fkastenholz305e1732019-06-18 05:01:22 -070011285 if (!VersionHasIetfQuicFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -050011286 return;
11287 }
zhongyi546cc452019-04-12 15:27:49 -070011288 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
QUICHE teama6ef0a62019-03-07 20:34:33 -050011289
11290 // clang-format off
11291 PacketFragments packet = {
11292 // type (short header, 4 byte packet number)
11293 {"",
11294 {0x43}},
11295 // connection_id
11296 {"",
11297 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
11298 // packet number
11299 {"",
11300 {0x12, 0x34, 0x9A, 0xBC}},
11301 // frame type (Blocked, four-byte encoding)
11302 {"",
11303 {kVarInt62FourBytes + 0x00, 0x00, 0x00, 0x08}}
11304 };
11305 // clang-format on
11306
11307 std::unique_ptr<QuicEncryptedPacket> encrypted(
11308 AssemblePacketFromFragments(packet));
11309
11310 EXPECT_FALSE(framer_.ProcessPacket(*encrypted));
11311
11312 EXPECT_EQ(IETF_QUIC_PROTOCOL_VIOLATION, framer_.error());
11313 EXPECT_EQ("Frame type not minimally encoded.", framer_.detailed_error());
11314}
11315
11316TEST_P(QuicFramerTest, IetfFrameTypeEncodingErrorKnown8Bytes) {
11317 // This test only for version 99.
fkastenholz305e1732019-06-18 05:01:22 -070011318 if (!VersionHasIetfQuicFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -050011319 return;
11320 }
zhongyi546cc452019-04-12 15:27:49 -070011321 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
QUICHE teama6ef0a62019-03-07 20:34:33 -050011322 // clang-format off
11323 PacketFragments packet = {
11324 // type (short header, 4 byte packet number)
11325 {"",
11326 {0x43}},
11327 // connection_id
11328 {"",
11329 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
11330 // packet number
11331 {"",
11332 {0x12, 0x34, 0x9A, 0xBC}},
11333 // frame type (Blocked, eight-byte encoding)
11334 {"",
11335 {kVarInt62EightBytes + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08}}
11336 };
11337 // clang-format on
11338
11339 std::unique_ptr<QuicEncryptedPacket> encrypted(
11340 AssemblePacketFromFragments(packet));
11341
11342 EXPECT_FALSE(framer_.ProcessPacket(*encrypted));
11343
11344 EXPECT_EQ(IETF_QUIC_PROTOCOL_VIOLATION, framer_.error());
11345 EXPECT_EQ("Frame type not minimally encoded.", framer_.detailed_error());
11346}
11347
11348// Tests to check that all known OETF frame types that are not minimally
11349// encoded generate IETF_QUIC_PROTOCOL_VIOLATION errors with detailed
11350// information "Frame type not minimally encoded."
11351// Just look at 2-byte encoding.
11352TEST_P(QuicFramerTest, IetfFrameTypeEncodingErrorKnown2BytesAllTypes) {
11353 // This test only for version 99.
fkastenholz305e1732019-06-18 05:01:22 -070011354 if (!VersionHasIetfQuicFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -050011355 return;
11356 }
zhongyi546cc452019-04-12 15:27:49 -070011357 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
QUICHE teama6ef0a62019-03-07 20:34:33 -050011358
11359 // clang-format off
11360 PacketFragments packets[] = {
11361 {
11362 // type (short header, 4 byte packet number)
11363 {"",
11364 {0x43}},
11365 // connection_id
11366 {"",
11367 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
11368 // packet number
11369 {"",
11370 {0x12, 0x34, 0x9A, 0xBC}},
11371 // frame type (two-byte encoding)
11372 {"",
11373 {kVarInt62TwoBytes + 0x00, 0x00}}
11374 },
11375 {
11376 // type (short header, 4 byte packet number)
11377 {"",
11378 {0x43}},
11379 // connection_id
11380 {"",
11381 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
11382 // packet number
11383 {"",
11384 {0x12, 0x34, 0x9A, 0xBC}},
11385 // frame type (two-byte encoding)
11386 {"",
11387 {kVarInt62TwoBytes + 0x00, 0x01}}
11388 },
11389 {
11390 // type (short header, 4 byte packet number)
11391 {"",
11392 {0x43}},
11393 // connection_id
11394 {"",
11395 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
11396 // packet number
11397 {"",
11398 {0x12, 0x34, 0x9A, 0xBC}},
11399 // frame type (two-byte encoding)
11400 {"",
11401 {kVarInt62TwoBytes + 0x00, 0x02}}
11402 },
11403 {
11404 // type (short header, 4 byte packet number)
11405 {"",
11406 {0x43}},
11407 // connection_id
11408 {"",
11409 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
11410 // packet number
11411 {"",
11412 {0x12, 0x34, 0x9A, 0xBC}},
11413 // frame type (two-byte encoding)
11414 {"",
11415 {kVarInt62TwoBytes + 0x00, 0x03}}
11416 },
11417 {
11418 // type (short header, 4 byte packet number)
11419 {"",
11420 {0x43}},
11421 // connection_id
11422 {"",
11423 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
11424 // packet number
11425 {"",
11426 {0x12, 0x34, 0x9A, 0xBC}},
11427 // frame type (two-byte encoding)
11428 {"",
11429 {kVarInt62TwoBytes + 0x00, 0x04}}
11430 },
11431 {
11432 // type (short header, 4 byte packet number)
11433 {"",
11434 {0x43}},
11435 // connection_id
11436 {"",
11437 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
11438 // packet number
11439 {"",
11440 {0x12, 0x34, 0x9A, 0xBC}},
11441 // frame type (two-byte encoding)
11442 {"",
11443 {kVarInt62TwoBytes + 0x00, 0x05}}
11444 },
11445 {
11446 // type (short header, 4 byte packet number)
11447 {"",
11448 {0x43}},
11449 // connection_id
11450 {"",
11451 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
11452 // packet number
11453 {"",
11454 {0x12, 0x34, 0x9A, 0xBC}},
11455 // frame type (two-byte encoding)
11456 {"",
11457 {kVarInt62TwoBytes + 0x00, 0x06}}
11458 },
11459 {
11460 // type (short header, 4 byte packet number)
11461 {"",
11462 {0x43}},
11463 // connection_id
11464 {"",
11465 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
11466 // packet number
11467 {"",
11468 {0x12, 0x34, 0x9A, 0xBC}},
11469 // frame type (two-byte encoding)
11470 {"",
11471 {kVarInt62TwoBytes + 0x00, 0x07}}
11472 },
11473 {
11474 // type (short header, 4 byte packet number)
11475 {"",
11476 {0x43}},
11477 // connection_id
11478 {"",
11479 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
11480 // packet number
11481 {"",
11482 {0x12, 0x34, 0x9A, 0xBC}},
11483 // frame type (two-byte encoding)
11484 {"",
11485 {kVarInt62TwoBytes + 0x00, 0x08}}
11486 },
11487 {
11488 // type (short header, 4 byte packet number)
11489 {"",
11490 {0x43}},
11491 // connection_id
11492 {"",
11493 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
11494 // packet number
11495 {"",
11496 {0x12, 0x34, 0x9A, 0xBC}},
11497 // frame type (two-byte encoding)
11498 {"",
11499 {kVarInt62TwoBytes + 0x00, 0x09}}
11500 },
11501 {
11502 // type (short header, 4 byte packet number)
11503 {"",
11504 {0x43}},
11505 // connection_id
11506 {"",
11507 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
11508 // packet number
11509 {"",
11510 {0x12, 0x34, 0x9A, 0xBC}},
11511 // frame type (two-byte encoding)
11512 {"",
11513 {kVarInt62TwoBytes + 0x00, 0x0a}}
11514 },
11515 {
11516 // type (short header, 4 byte packet number)
11517 {"",
11518 {0x43}},
11519 // connection_id
11520 {"",
11521 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
11522 // packet number
11523 {"",
11524 {0x12, 0x34, 0x9A, 0xBC}},
11525 // frame type (two-byte encoding)
11526 {"",
11527 {kVarInt62TwoBytes + 0x00, 0x0b}}
11528 },
11529 {
11530 // type (short header, 4 byte packet number)
11531 {"",
11532 {0x43}},
11533 // connection_id
11534 {"",
11535 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
11536 // packet number
11537 {"",
11538 {0x12, 0x34, 0x9A, 0xBC}},
11539 // frame type (two-byte encoding)
11540 {"",
11541 {kVarInt62TwoBytes + 0x00, 0x0c}}
11542 },
11543 {
11544 // type (short header, 4 byte packet number)
11545 {"",
11546 {0x43}},
11547 // connection_id
11548 {"",
11549 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
11550 // packet number
11551 {"",
11552 {0x12, 0x34, 0x9A, 0xBC}},
11553 // frame type (two-byte encoding)
11554 {"",
11555 {kVarInt62TwoBytes + 0x00, 0x0d}}
11556 },
11557 {
11558 // type (short header, 4 byte packet number)
11559 {"",
11560 {0x43}},
11561 // connection_id
11562 {"",
11563 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
11564 // packet number
11565 {"",
11566 {0x12, 0x34, 0x9A, 0xBC}},
11567 // frame type (two-byte encoding)
11568 {"",
11569 {kVarInt62TwoBytes + 0x00, 0x0e}}
11570 },
11571 {
11572 // type (short header, 4 byte packet number)
11573 {"",
11574 {0x43}},
11575 // connection_id
11576 {"",
11577 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
11578 // packet number
11579 {"",
11580 {0x12, 0x34, 0x9A, 0xBC}},
11581 // frame type (two-byte encoding)
11582 {"",
11583 {kVarInt62TwoBytes + 0x00, 0x0f}}
11584 },
11585 {
11586 // type (short header, 4 byte packet number)
11587 {"",
11588 {0x43}},
11589 // connection_id
11590 {"",
11591 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
11592 // packet number
11593 {"",
11594 {0x12, 0x34, 0x9A, 0xBC}},
11595 // frame type (two-byte encoding)
11596 {"",
11597 {kVarInt62TwoBytes + 0x00, 0x10}}
11598 },
11599 {
11600 // type (short header, 4 byte packet number)
11601 {"",
11602 {0x43}},
11603 // connection_id
11604 {"",
11605 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
11606 // packet number
11607 {"",
11608 {0x12, 0x34, 0x9A, 0xBC}},
11609 // frame type (two-byte encoding)
11610 {"",
11611 {kVarInt62TwoBytes + 0x00, 0x11}}
11612 },
11613 {
11614 // type (short header, 4 byte packet number)
11615 {"",
11616 {0x43}},
11617 // connection_id
11618 {"",
11619 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
11620 // packet number
11621 {"",
11622 {0x12, 0x34, 0x9A, 0xBC}},
11623 // frame type (two-byte encoding)
11624 {"",
11625 {kVarInt62TwoBytes + 0x00, 0x12}}
11626 },
11627 {
11628 // type (short header, 4 byte packet number)
11629 {"",
11630 {0x43}},
11631 // connection_id
11632 {"",
11633 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
11634 // packet number
11635 {"",
11636 {0x12, 0x34, 0x9A, 0xBC}},
11637 // frame type (two-byte encoding)
11638 {"",
11639 {kVarInt62TwoBytes + 0x00, 0x13}}
11640 },
11641 {
11642 // type (short header, 4 byte packet number)
11643 {"",
11644 {0x43}},
11645 // connection_id
11646 {"",
11647 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
11648 // packet number
11649 {"",
11650 {0x12, 0x34, 0x9A, 0xBC}},
11651 // frame type (two-byte encoding)
11652 {"",
11653 {kVarInt62TwoBytes + 0x00, 0x14}}
11654 },
11655 {
11656 // type (short header, 4 byte packet number)
11657 {"",
11658 {0x43}},
11659 // connection_id
11660 {"",
11661 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
11662 // packet number
11663 {"",
11664 {0x12, 0x34, 0x9A, 0xBC}},
11665 // frame type (two-byte encoding)
11666 {"",
11667 {kVarInt62TwoBytes + 0x00, 0x15}}
11668 },
11669 {
11670 // type (short header, 4 byte packet number)
11671 {"",
11672 {0x43}},
11673 // connection_id
11674 {"",
11675 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
11676 // packet number
11677 {"",
11678 {0x12, 0x34, 0x9A, 0xBC}},
11679 // frame type (two-byte encoding)
11680 {"",
11681 {kVarInt62TwoBytes + 0x00, 0x16}}
11682 },
11683 {
11684 // type (short header, 4 byte packet number)
11685 {"",
11686 {0x43}},
11687 // connection_id
11688 {"",
11689 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
11690 // packet number
11691 {"",
11692 {0x12, 0x34, 0x9A, 0xBC}},
11693 // frame type (two-byte encoding)
11694 {"",
11695 {kVarInt62TwoBytes + 0x00, 0x17}}
11696 },
11697 {
11698 // type (short header, 4 byte packet number)
11699 {"",
11700 {0x43}},
11701 // connection_id
11702 {"",
11703 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
11704 // packet number
11705 {"",
11706 {0x12, 0x34, 0x9A, 0xBC}},
11707 // frame type (two-byte encoding)
11708 {"",
11709 {kVarInt62TwoBytes + 0x00, 0x18}}
11710 },
11711 {
11712 // type (short header, 4 byte packet number)
11713 {"",
11714 {0x43}},
11715 // connection_id
11716 {"",
11717 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
11718 // packet number
11719 {"",
11720 {0x12, 0x34, 0x9A, 0xBC}},
11721 // frame type (two-byte encoding)
11722 {"",
11723 {kVarInt62TwoBytes + 0x00, 0x20}}
11724 },
11725 {
11726 // type (short header, 4 byte packet number)
11727 {"",
11728 {0x43}},
11729 // connection_id
11730 {"",
11731 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
11732 // packet number
11733 {"",
11734 {0x12, 0x34, 0x9A, 0xBC}},
11735 // frame type (two-byte encoding)
11736 {"",
11737 {kVarInt62TwoBytes + 0x00, 0x21}}
11738 },
11739 };
11740 // clang-format on
11741
11742 for (PacketFragments& packet : packets) {
11743 std::unique_ptr<QuicEncryptedPacket> encrypted(
11744 AssemblePacketFromFragments(packet));
11745
11746 EXPECT_FALSE(framer_.ProcessPacket(*encrypted));
11747
11748 EXPECT_EQ(IETF_QUIC_PROTOCOL_VIOLATION, framer_.error());
11749 EXPECT_EQ("Frame type not minimally encoded.", framer_.detailed_error());
11750 }
11751}
11752
11753TEST_P(QuicFramerTest, RetireConnectionIdFrame) {
fkastenholz305e1732019-06-18 05:01:22 -070011754 if (!VersionHasIetfQuicFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -050011755 // This frame is only for version 99.
11756 return;
11757 }
zhongyi546cc452019-04-12 15:27:49 -070011758 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
QUICHE teama6ef0a62019-03-07 20:34:33 -050011759 // clang-format off
11760 PacketFragments packet99 = {
11761 // type (short header, 4 byte packet number)
11762 {"",
11763 {0x43}},
11764 // connection_id
11765 {"",
11766 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
11767 // packet number
11768 {"",
11769 {0x12, 0x34, 0x56, 0x78}},
11770 // frame type (IETF_RETIRE_CONNECTION_ID frame)
11771 {"",
11772 {0x19}},
11773 // Sequence number
11774 {"Unable to read retire connection ID frame sequence number.",
11775 {kVarInt62TwoBytes + 0x11, 0x22}}
11776 };
11777 // clang-format on
11778
11779 std::unique_ptr<QuicEncryptedPacket> encrypted(
11780 AssemblePacketFromFragments(packet99));
11781 EXPECT_TRUE(framer_.ProcessPacket(*encrypted));
11782
11783 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
11784 ASSERT_TRUE(visitor_.header_.get());
11785 EXPECT_TRUE(CheckDecryption(
11786 *encrypted, !kIncludeVersion, !kIncludeDiversificationNonce,
11787 PACKET_8BYTE_CONNECTION_ID, PACKET_0BYTE_CONNECTION_ID));
11788
11789 EXPECT_EQ(0u, visitor_.stream_frames_.size());
11790
11791 EXPECT_EQ(0x1122u, visitor_.retire_connection_id_.sequence_number);
11792
11793 ASSERT_EQ(0u, visitor_.ack_frames_.size());
11794
11795 CheckFramingBoundaries(packet99, QUIC_INVALID_RETIRE_CONNECTION_ID_DATA);
11796}
11797
11798TEST_P(QuicFramerTest, BuildRetireConnectionIdFramePacket) {
fkastenholz305e1732019-06-18 05:01:22 -070011799 if (!VersionHasIetfQuicFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -050011800 // This frame is only for version 99.
11801 return;
11802 }
QUICHE team2252b702019-05-14 23:55:14 -040011803 QuicFramerPeer::SetPerspective(&framer_, Perspective::IS_CLIENT);
QUICHE teama6ef0a62019-03-07 20:34:33 -050011804 QuicPacketHeader header;
11805 header.destination_connection_id = FramerTestConnectionId();
11806 header.reset_flag = false;
11807 header.version_flag = false;
11808 header.packet_number = kPacketNumber;
11809
11810 QuicRetireConnectionIdFrame frame;
11811 frame.sequence_number = 0x1122;
11812
11813 QuicFrames frames = {QuicFrame(&frame)};
11814
11815 // clang-format off
11816 unsigned char packet99[] = {
11817 // type (short header, 4 byte packet number)
11818 0x43,
11819 // connection_id
11820 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
11821 // packet number
11822 0x12, 0x34, 0x56, 0x78,
11823
11824 // frame type (IETF_RETIRE_CONNECTION_ID frame)
11825 0x19,
11826 // sequence number
11827 kVarInt62TwoBytes + 0x11, 0x22
11828 };
11829 // clang-format on
11830
11831 std::unique_ptr<QuicPacket> data(BuildDataPacket(header, frames));
11832 ASSERT_TRUE(data != nullptr);
11833
11834 test::CompareCharArraysWithHexError("constructed packet", data->data(),
11835 data->length(), AsChars(packet99),
11836 QUIC_ARRAYSIZE(packet99));
11837}
11838
11839TEST_P(QuicFramerTest, AckFrameWithInvalidLargestObserved) {
zhongyi546cc452019-04-12 15:27:49 -070011840 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
QUICHE teama6ef0a62019-03-07 20:34:33 -050011841 // clang-format off
11842 unsigned char packet[] = {
11843 // public flags (8 byte connection_id)
11844 0x2C,
11845 // connection_id
11846 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
11847 // packet number
11848 0x12, 0x34, 0x56, 0x78,
11849
11850 // frame type (ack frame)
11851 0x45,
11852 // largest observed
11853 0x00, 0x00,
11854 // Zero delta time.
11855 0x00, 0x00,
11856 // first ack block length.
11857 0x00, 0x00,
11858 // num timestamps.
11859 0x00
11860 };
11861
QUICHE teama6ef0a62019-03-07 20:34:33 -050011862 unsigned char packet46[] = {
11863 // type (short header, 4 byte packet number)
11864 0x43,
11865 // connection_id
11866 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
11867 // packet number
11868 0x12, 0x34, 0x56, 0x78,
11869
11870 // frame type (ack frame)
11871 0x45,
11872 // largest observed
11873 0x00, 0x00,
11874 // Zero delta time.
11875 0x00, 0x00,
11876 // first ack block length.
11877 0x00, 0x00,
11878 // num timestamps.
11879 0x00
11880 };
11881
11882 unsigned char packet99[] = {
11883 // type (short header, 4 byte packet number)
11884 0x43,
11885 // connection_id
11886 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
11887 // packet number
11888 0x12, 0x34, 0x56, 0x78,
11889
11890 // frame type (IETF_ACK frame)
11891 0x02,
11892 // Largest acked
11893 kVarInt62OneByte + 0x00,
11894 // Zero delta time.
11895 kVarInt62OneByte + 0x00,
11896 // Ack block count 0
11897 kVarInt62OneByte + 0x00,
11898 // First ack block length
11899 kVarInt62OneByte + 0x00,
11900 };
11901 // clang-format on
11902
11903 unsigned char* p = packet;
11904 size_t p_size = QUIC_ARRAYSIZE(packet);
fkastenholz305e1732019-06-18 05:01:22 -070011905 if (VersionHasIetfQuicFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -050011906 p = packet99;
11907 p_size = QUIC_ARRAYSIZE(packet99);
fayang36825da2019-08-21 14:01:27 -070011908 } else if (framer_.transport_version() >= QUIC_VERSION_46) {
fayang374888f2019-05-31 06:47:21 -070011909 p = packet46;
QUICHE teama6ef0a62019-03-07 20:34:33 -050011910 }
11911
11912 QuicEncryptedPacket encrypted(AsChars(p), p_size, false);
11913 EXPECT_FALSE(framer_.ProcessPacket(encrypted));
11914 EXPECT_EQ(framer_.detailed_error(), "Largest acked is 0.");
11915}
11916
11917TEST_P(QuicFramerTest, FirstAckBlockJustUnderFlow) {
zhongyi546cc452019-04-12 15:27:49 -070011918 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
QUICHE teama6ef0a62019-03-07 20:34:33 -050011919 // clang-format off
11920 unsigned char packet[] = {
11921 // public flags (8 byte connection_id)
11922 0x2C,
11923 // connection_id
11924 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
11925 // packet number
11926 0x12, 0x34, 0x56, 0x78,
11927
11928 // frame type (ack frame)
11929 0x45,
11930 // largest observed
11931 0x00, 0x02,
11932 // Zero delta time.
11933 0x00, 0x00,
11934 // first ack block length.
11935 0x00, 0x03,
11936 // num timestamps.
11937 0x00
11938 };
11939
QUICHE teama6ef0a62019-03-07 20:34:33 -050011940 unsigned char packet46[] = {
11941 // type (short header, 4 byte packet number)
11942 0x43,
11943 // connection_id
11944 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
11945 // packet number
11946 0x12, 0x34, 0x56, 0x78,
11947
11948 // frame type (ack frame)
11949 0x45,
11950 // largest observed
11951 0x00, 0x02,
11952 // Zero delta time.
11953 0x00, 0x00,
11954 // first ack block length.
11955 0x00, 0x03,
11956 // num timestamps.
11957 0x00
11958 };
11959
11960 unsigned char packet99[] = {
11961 // type (short header, 4 byte packet number)
11962 0x43,
11963 // connection_id
11964 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
11965 // packet number
11966 0x12, 0x34, 0x56, 0x78,
11967
11968 // frame type (IETF_ACK frame)
11969 0x02,
11970 // Largest acked
11971 kVarInt62OneByte + 0x02,
11972 // Zero delta time.
11973 kVarInt62OneByte + 0x00,
11974 // Ack block count 0
11975 kVarInt62OneByte + 0x00,
11976 // First ack block length
11977 kVarInt62OneByte + 0x02,
11978 };
11979 // clang-format on
11980
11981 unsigned char* p = packet;
11982 size_t p_size = QUIC_ARRAYSIZE(packet);
fkastenholz305e1732019-06-18 05:01:22 -070011983 if (VersionHasIetfQuicFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -050011984 p = packet99;
11985 p_size = QUIC_ARRAYSIZE(packet99);
fayang36825da2019-08-21 14:01:27 -070011986 } else if (framer_.transport_version() >= QUIC_VERSION_46) {
QUICHE teama6ef0a62019-03-07 20:34:33 -050011987 p = packet46;
11988 p_size = QUIC_ARRAYSIZE(packet46);
QUICHE teama6ef0a62019-03-07 20:34:33 -050011989 }
11990
11991 QuicEncryptedPacket encrypted(AsChars(p), p_size, false);
11992 EXPECT_FALSE(framer_.ProcessPacket(encrypted));
11993 EXPECT_EQ(framer_.detailed_error(),
11994 "Underflow with first ack block length 3 largest acked is 2.");
11995}
11996
11997TEST_P(QuicFramerTest, ThirdAckBlockJustUnderflow) {
zhongyi546cc452019-04-12 15:27:49 -070011998 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
QUICHE teama6ef0a62019-03-07 20:34:33 -050011999 // clang-format off
12000 unsigned char packet[] = {
12001 // public flags (8 byte connection_id)
12002 0x2C,
12003 // connection_id
12004 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
12005 // packet number
12006 0x12, 0x34, 0x56, 0x78,
12007
12008 // frame type (ack frame)
12009 0x60,
12010 // largest observed
12011 0x0A,
12012 // Zero delta time.
12013 0x00, 0x00,
12014 // Num of ack blocks
12015 0x02,
12016 // first ack block length.
12017 0x02,
12018 // gap to next block
12019 0x01,
12020 // ack block length
12021 0x01,
12022 // gap to next block
12023 0x01,
12024 // ack block length
12025 0x06,
12026 // num timestamps.
12027 0x00
12028 };
12029
QUICHE teama6ef0a62019-03-07 20:34:33 -050012030 unsigned char packet46[] = {
12031 // type (short header, 4 byte packet number)
12032 0x43,
12033 // connection_id
12034 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
12035 // packet number
12036 0x12, 0x34, 0x56, 0x78,
12037
12038 // frame type (ack frame)
12039 0x60,
12040 // largest observed
12041 0x0A,
12042 // Zero delta time.
12043 0x00, 0x00,
12044 // Num of ack blocks
12045 0x02,
12046 // first ack block length.
12047 0x02,
12048 // gap to next block
12049 0x01,
12050 // ack block length
12051 0x01,
12052 // gap to next block
12053 0x01,
12054 // ack block length
12055 0x06,
12056 // num timestamps.
12057 0x00
12058 };
12059
12060 unsigned char packet99[] = {
12061 // type (short header, 4 byte packet number)
12062 0x43,
12063 // connection_id
12064 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
12065 // packet number
12066 0x12, 0x34, 0x56, 0x78,
12067
12068 // frame type (IETF_ACK frame)
12069 0x02,
12070 // Largest acked
12071 kVarInt62OneByte + 0x0A,
12072 // Zero delta time.
12073 kVarInt62OneByte + 0x00,
12074 // Ack block count 2
12075 kVarInt62OneByte + 0x02,
12076 // First ack block length
12077 kVarInt62OneByte + 0x01,
12078 // gap to next block length
12079 kVarInt62OneByte + 0x00,
12080 // ack block length
12081 kVarInt62OneByte + 0x00,
12082 // gap to next block length
12083 kVarInt62OneByte + 0x00,
12084 // ack block length
12085 kVarInt62OneByte + 0x05,
12086 };
12087 // clang-format on
12088
12089 unsigned char* p = packet;
12090 size_t p_size = QUIC_ARRAYSIZE(packet);
fkastenholz305e1732019-06-18 05:01:22 -070012091 if (VersionHasIetfQuicFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -050012092 p = packet99;
12093 p_size = QUIC_ARRAYSIZE(packet99);
fayang36825da2019-08-21 14:01:27 -070012094 } else if (framer_.transport_version() >= QUIC_VERSION_46) {
QUICHE teama6ef0a62019-03-07 20:34:33 -050012095 p = packet46;
12096 p_size = QUIC_ARRAYSIZE(packet46);
QUICHE teama6ef0a62019-03-07 20:34:33 -050012097 }
12098
12099 QuicEncryptedPacket encrypted(AsChars(p), p_size, false);
12100 EXPECT_FALSE(framer_.ProcessPacket(encrypted));
fkastenholz305e1732019-06-18 05:01:22 -070012101 if (VersionHasIetfQuicFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -050012102 EXPECT_EQ(framer_.detailed_error(),
12103 "Underflow with ack block length 6 latest ack block end is 5.");
12104 } else {
12105 EXPECT_EQ(framer_.detailed_error(),
12106 "Underflow with ack block length 6, end of block is 6.");
12107 }
12108}
12109
12110TEST_P(QuicFramerTest, CoalescedPacket) {
12111 if (!QuicVersionHasLongHeaderLengths(framer_.transport_version())) {
12112 return;
12113 }
zhongyi546cc452019-04-12 15:27:49 -070012114 SetDecrypterLevel(ENCRYPTION_ZERO_RTT);
QUICHE teama6ef0a62019-03-07 20:34:33 -050012115 // clang-format off
12116 unsigned char packet[] = {
12117 // first coalesced packet
12118 // public flags (long header with packet type ZERO_RTT_PROTECTED and
12119 // 4-byte packet number)
12120 0xD3,
12121 // version
12122 QUIC_VERSION_BYTES,
12123 // destination connection ID length
dschinazi48ac9192019-07-31 00:07:26 -070012124 0x08,
QUICHE teama6ef0a62019-03-07 20:34:33 -050012125 // destination connection ID
12126 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
dschinazi48ac9192019-07-31 00:07:26 -070012127 // source connection ID length
12128 0x00,
QUICHE teama6ef0a62019-03-07 20:34:33 -050012129 // long header packet length
12130 0x1E,
12131 // packet number
12132 0x12, 0x34, 0x56, 0x78,
12133 // frame type (IETF_STREAM frame with FIN, LEN, and OFFSET bits set)
12134 0x08 | 0x01 | 0x02 | 0x04,
12135 // stream id
12136 kVarInt62FourBytes + 0x00, 0x02, 0x03, 0x04,
12137 // offset
12138 kVarInt62EightBytes + 0x3A, 0x98, 0xFE, 0xDC,
12139 0x32, 0x10, 0x76, 0x54,
12140 // data length
12141 kVarInt62OneByte + 0x0c,
12142 // data
12143 'h', 'e', 'l', 'l',
12144 'o', ' ', 'w', 'o',
12145 'r', 'l', 'd', '!',
12146 // second coalesced packet
12147 // public flags (long header with packet type ZERO_RTT_PROTECTED and
12148 // 4-byte packet number)
12149 0xD3,
12150 // version
12151 QUIC_VERSION_BYTES,
12152 // destination connection ID length
dschinazi48ac9192019-07-31 00:07:26 -070012153 0x08,
QUICHE teama6ef0a62019-03-07 20:34:33 -050012154 // destination connection ID
12155 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
dschinazi48ac9192019-07-31 00:07:26 -070012156 // source connection ID length
12157 0x00,
QUICHE teama6ef0a62019-03-07 20:34:33 -050012158 // long header packet length
12159 0x1E,
12160 // packet number
12161 0x12, 0x34, 0x56, 0x79,
12162 // frame type (IETF_STREAM frame with FIN, LEN, and OFFSET bits set)
12163 0x08 | 0x01 | 0x02 | 0x04,
12164 // stream id
12165 kVarInt62FourBytes + 0x00, 0x02, 0x03, 0x04,
12166 // offset
12167 kVarInt62EightBytes + 0x3A, 0x98, 0xFE, 0xDC,
12168 0x32, 0x10, 0x76, 0x54,
12169 // data length
12170 kVarInt62OneByte + 0x0c,
12171 // data
12172 'H', 'E', 'L', 'L',
12173 'O', '_', 'W', 'O',
12174 'R', 'L', 'D', '?',
12175 };
12176 // clang-format on
12177
12178 QuicEncryptedPacket encrypted(AsChars(packet), QUIC_ARRAYSIZE(packet), false);
12179 EXPECT_TRUE(framer_.ProcessPacket(encrypted));
12180
12181 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
12182 ASSERT_TRUE(visitor_.header_.get());
12183
12184 ASSERT_EQ(1u, visitor_.stream_frames_.size());
12185 EXPECT_EQ(0u, visitor_.ack_frames_.size());
12186
12187 // Stream ID should be the last 3 bytes of kStreamId.
12188 EXPECT_EQ(0x00FFFFFF & kStreamId, visitor_.stream_frames_[0]->stream_id);
12189 EXPECT_TRUE(visitor_.stream_frames_[0]->fin);
12190 EXPECT_EQ(kStreamOffset, visitor_.stream_frames_[0]->offset);
12191 CheckStreamFrameData("hello world!", visitor_.stream_frames_[0].get());
12192
12193 ASSERT_EQ(visitor_.coalesced_packets_.size(), 1u);
12194 EXPECT_TRUE(framer_.ProcessPacket(*visitor_.coalesced_packets_[0].get()));
12195
12196 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
12197 ASSERT_TRUE(visitor_.header_.get());
12198
12199 ASSERT_EQ(2u, visitor_.stream_frames_.size());
12200 EXPECT_EQ(0u, visitor_.ack_frames_.size());
12201
12202 // Stream ID should be the last 3 bytes of kStreamId.
12203 EXPECT_EQ(0x00FFFFFF & kStreamId, visitor_.stream_frames_[1]->stream_id);
12204 EXPECT_TRUE(visitor_.stream_frames_[1]->fin);
12205 EXPECT_EQ(kStreamOffset, visitor_.stream_frames_[1]->offset);
12206 CheckStreamFrameData("HELLO_WORLD?", visitor_.stream_frames_[1].get());
12207}
12208
dschinazi4b5a68a2019-08-15 15:45:36 -070012209TEST_P(QuicFramerTest, UndecryptablePacketWithoutDecrypter) {
12210 QuicFramerPeer::SetPerspective(&framer_, Perspective::IS_CLIENT);
12211
12212 if (!framer_.version().KnowsWhichDecrypterToUse()) {
12213 // We create a bad client decrypter by using initial encryption with a
12214 // bogus connection ID; it should fail to decrypt everything.
12215 QuicConnectionId bogus_connection_id = TestConnectionId(0xbad);
12216 CrypterPair bogus_crypters;
nharperc8d9e402019-09-12 18:30:14 -070012217 CryptoUtils::CreateInitialObfuscators(Perspective::IS_CLIENT,
12218 framer_.version(),
dschinazi4b5a68a2019-08-15 15:45:36 -070012219 bogus_connection_id, &bogus_crypters);
12220 // This removes all other decrypters.
12221 framer_.SetDecrypter(ENCRYPTION_FORWARD_SECURE,
12222 std::move(bogus_crypters.decrypter));
12223 }
12224
dschinazi4b5a68a2019-08-15 15:45:36 -070012225 // clang-format off
12226 unsigned char packet[] = {
12227 // public flags (version included, 8-byte connection ID,
12228 // 4-byte packet number)
12229 0x28,
12230 // connection_id
12231 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
12232 // packet number
12233 0x12, 0x34, 0x56, 0x00,
12234 // padding frames
12235 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
12236 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
12237 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
12238 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
12239 };
fayang36825da2019-08-21 14:01:27 -070012240 unsigned char packet46[] = {
dschinazi4b5a68a2019-08-15 15:45:36 -070012241 // public flags (long header with packet type HANDSHAKE and
12242 // 4-byte packet number)
fayang36825da2019-08-21 14:01:27 -070012243 0xE3,
dschinazi4b5a68a2019-08-15 15:45:36 -070012244 // version
12245 QUIC_VERSION_BYTES,
12246 // connection ID lengths
12247 0x05,
12248 // source connection ID
12249 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
12250 // long header packet length
12251 0x05,
12252 // packet number
12253 0x12, 0x34, 0x56, 0x00,
12254 // padding frames
12255 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
12256 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
12257 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
12258 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
12259 };
12260 unsigned char packet99[] = {
12261 // public flags (long header with packet type HANDSHAKE and
12262 // 4-byte packet number)
12263 0xE3,
12264 // version
12265 QUIC_VERSION_BYTES,
12266 // destination connection ID length
12267 0x00,
12268 // source connection ID length
12269 0x08,
12270 // source connection ID
12271 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
12272 // long header packet length
12273 0x24,
12274 // packet number
12275 0x12, 0x34, 0x56, 0x00,
12276 // padding frames
12277 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
12278 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
12279 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
12280 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
12281 };
12282 // clang-format on
12283 unsigned char* p = packet;
12284 size_t p_length = QUIC_ARRAYSIZE(packet);
12285 if (framer_.transport_version() == QUIC_VERSION_99) {
12286 p = packet99;
12287 p_length = QUIC_ARRAYSIZE(packet99);
fayang36825da2019-08-21 14:01:27 -070012288 } else if (framer_.transport_version() >= QUIC_VERSION_46) {
12289 p = packet46;
12290 p_length = QUIC_ARRAYSIZE(packet46);
dschinazi4b5a68a2019-08-15 15:45:36 -070012291 }
12292 // First attempt decryption without the handshake crypter.
12293 EXPECT_FALSE(
12294 framer_.ProcessPacket(QuicEncryptedPacket(AsChars(p), p_length, false)));
12295 EXPECT_EQ(QUIC_DECRYPTION_FAILURE, framer_.error());
12296 if (GetQuicRestartFlag(quic_framer_uses_undecryptable_upcall)) {
12297 ASSERT_EQ(1u, visitor_.undecryptable_packets_.size());
12298 ASSERT_EQ(1u, visitor_.undecryptable_decryption_levels_.size());
12299 ASSERT_EQ(1u, visitor_.undecryptable_has_decryption_keys_.size());
12300 CompareCharArraysWithHexError(
12301 "undecryptable packet", visitor_.undecryptable_packets_[0]->data(),
12302 visitor_.undecryptable_packets_[0]->length(), AsChars(p), p_length);
12303 if (framer_.version().KnowsWhichDecrypterToUse()) {
12304 EXPECT_EQ(ENCRYPTION_HANDSHAKE,
12305 visitor_.undecryptable_decryption_levels_[0]);
12306 }
12307 EXPECT_FALSE(visitor_.undecryptable_has_decryption_keys_[0]);
12308 } else {
12309 EXPECT_EQ(0u, visitor_.undecryptable_packets_.size());
12310 EXPECT_EQ(0u, visitor_.undecryptable_decryption_levels_.size());
12311 EXPECT_EQ(0u, visitor_.undecryptable_has_decryption_keys_.size());
12312 }
12313}
12314
12315TEST_P(QuicFramerTest, UndecryptablePacketWithDecrypter) {
12316 QuicFramerPeer::SetPerspective(&framer_, Perspective::IS_CLIENT);
12317
12318 // We create a bad client decrypter by using initial encryption with a
12319 // bogus connection ID; it should fail to decrypt everything.
12320 QuicConnectionId bogus_connection_id = TestConnectionId(0xbad);
12321 CrypterPair bad_handshake_crypters;
nharperc8d9e402019-09-12 18:30:14 -070012322 CryptoUtils::CreateInitialObfuscators(Perspective::IS_CLIENT,
12323 framer_.version(), bogus_connection_id,
12324 &bad_handshake_crypters);
dschinazi4b5a68a2019-08-15 15:45:36 -070012325 if (framer_.version().KnowsWhichDecrypterToUse()) {
12326 framer_.InstallDecrypter(ENCRYPTION_HANDSHAKE,
12327 std::move(bad_handshake_crypters.decrypter));
12328 } else {
12329 framer_.SetDecrypter(ENCRYPTION_HANDSHAKE,
12330 std::move(bad_handshake_crypters.decrypter));
12331 }
12332
dschinazi4b5a68a2019-08-15 15:45:36 -070012333 // clang-format off
12334 unsigned char packet[] = {
12335 // public flags (version included, 8-byte connection ID,
12336 // 4-byte packet number)
12337 0x28,
12338 // connection_id
12339 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
12340 // packet number
12341 0x12, 0x34, 0x56, 0x00,
12342 // padding frames
12343 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
12344 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
12345 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
12346 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
12347 };
fayang36825da2019-08-21 14:01:27 -070012348 unsigned char packet46[] = {
dschinazi4b5a68a2019-08-15 15:45:36 -070012349 // public flags (long header with packet type HANDSHAKE and
12350 // 4-byte packet number)
fayang36825da2019-08-21 14:01:27 -070012351 0xE3,
dschinazi4b5a68a2019-08-15 15:45:36 -070012352 // version
12353 QUIC_VERSION_BYTES,
12354 // connection ID lengths
12355 0x05,
12356 // source connection ID
12357 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
12358 // long header packet length
12359 0x05,
12360 // packet number
12361 0x12, 0x34, 0x56, 0x00,
12362 // padding frames
12363 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
12364 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
12365 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
12366 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
12367 };
12368 unsigned char packet99[] = {
12369 // public flags (long header with packet type HANDSHAKE and
12370 // 4-byte packet number)
12371 0xE3,
12372 // version
12373 QUIC_VERSION_BYTES,
12374 // destination connection ID length
12375 0x00,
12376 // source connection ID length
12377 0x08,
12378 // source connection ID
12379 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
12380 // long header packet length
12381 0x24,
12382 // packet number
12383 0x12, 0x34, 0x56, 0x00,
12384 // padding frames
12385 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
12386 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
12387 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
12388 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
12389 };
12390 // clang-format on
12391 unsigned char* p = packet;
12392 size_t p_length = QUIC_ARRAYSIZE(packet);
12393 if (framer_.transport_version() == QUIC_VERSION_99) {
12394 p = packet99;
12395 p_length = QUIC_ARRAYSIZE(packet99);
fayang36825da2019-08-21 14:01:27 -070012396 } else if (framer_.transport_version() >= QUIC_VERSION_46) {
12397 p = packet46;
12398 p_length = QUIC_ARRAYSIZE(packet46);
dschinazi4b5a68a2019-08-15 15:45:36 -070012399 }
12400
12401 EXPECT_FALSE(
12402 framer_.ProcessPacket(QuicEncryptedPacket(AsChars(p), p_length, false)));
12403 EXPECT_EQ(QUIC_DECRYPTION_FAILURE, framer_.error());
12404 if (GetQuicRestartFlag(quic_framer_uses_undecryptable_upcall)) {
12405 ASSERT_EQ(1u, visitor_.undecryptable_packets_.size());
12406 ASSERT_EQ(1u, visitor_.undecryptable_decryption_levels_.size());
12407 ASSERT_EQ(1u, visitor_.undecryptable_has_decryption_keys_.size());
12408 CompareCharArraysWithHexError(
12409 "undecryptable packet", visitor_.undecryptable_packets_[0]->data(),
12410 visitor_.undecryptable_packets_[0]->length(), AsChars(p), p_length);
12411 if (framer_.version().KnowsWhichDecrypterToUse()) {
12412 EXPECT_EQ(ENCRYPTION_HANDSHAKE,
12413 visitor_.undecryptable_decryption_levels_[0]);
12414 }
12415 EXPECT_EQ(framer_.version().KnowsWhichDecrypterToUse(),
12416 visitor_.undecryptable_has_decryption_keys_[0]);
12417 } else {
12418 EXPECT_EQ(0u, visitor_.undecryptable_packets_.size());
12419 EXPECT_EQ(0u, visitor_.undecryptable_decryption_levels_.size());
12420 EXPECT_EQ(0u, visitor_.undecryptable_has_decryption_keys_.size());
12421 }
12422}
12423
12424TEST_P(QuicFramerTest, UndecryptableCoalescedPacket) {
12425 if (!QuicVersionHasLongHeaderLengths(framer_.transport_version())) {
12426 return;
12427 }
12428 ASSERT_TRUE(framer_.version().KnowsWhichDecrypterToUse());
12429 SetDecrypterLevel(ENCRYPTION_ZERO_RTT);
12430 // We create a bad client decrypter by using initial encryption with a
12431 // bogus connection ID; it should fail to decrypt everything.
12432 QuicConnectionId bogus_connection_id = TestConnectionId(0xbad);
12433 CrypterPair bad_handshake_crypters;
nharperc8d9e402019-09-12 18:30:14 -070012434 CryptoUtils::CreateInitialObfuscators(Perspective::IS_CLIENT,
12435 framer_.version(), bogus_connection_id,
12436 &bad_handshake_crypters);
dschinazi4b5a68a2019-08-15 15:45:36 -070012437 framer_.InstallDecrypter(ENCRYPTION_HANDSHAKE,
12438 std::move(bad_handshake_crypters.decrypter));
12439 // clang-format off
12440 unsigned char packet[] = {
12441 // first coalesced packet
12442 // public flags (long header with packet type HANDSHAKE and
12443 // 4-byte packet number)
12444 0xE3,
12445 // version
12446 QUIC_VERSION_BYTES,
12447 // destination connection ID length
12448 0x08,
12449 // destination connection ID
12450 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
12451 // source connection ID length
12452 0x00,
12453 // long header packet length
12454 0x1E,
12455 // packet number
12456 0x12, 0x34, 0x56, 0x78,
12457 // frame type (IETF_STREAM frame with FIN, LEN, and OFFSET bits set)
12458 0x08 | 0x01 | 0x02 | 0x04,
12459 // stream id
12460 kVarInt62FourBytes + 0x00, 0x02, 0x03, 0x04,
12461 // offset
12462 kVarInt62EightBytes + 0x3A, 0x98, 0xFE, 0xDC,
12463 0x32, 0x10, 0x76, 0x54,
12464 // data length
12465 kVarInt62OneByte + 0x0c,
12466 // data
12467 'h', 'e', 'l', 'l',
12468 'o', ' ', 'w', 'o',
12469 'r', 'l', 'd', '!',
12470 // second coalesced packet
12471 // public flags (long header with packet type ZERO_RTT_PROTECTED and
12472 // 4-byte packet number)
12473 0xD3,
12474 // version
12475 QUIC_VERSION_BYTES,
12476 // destination connection ID length
12477 0x08,
12478 // destination connection ID
12479 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
12480 // source connection ID length
12481 0x00,
12482 // long header packet length
12483 0x1E,
12484 // packet number
12485 0x12, 0x34, 0x56, 0x79,
12486 // frame type (IETF_STREAM frame with FIN, LEN, and OFFSET bits set)
12487 0x08 | 0x01 | 0x02 | 0x04,
12488 // stream id
12489 kVarInt62FourBytes + 0x00, 0x02, 0x03, 0x04,
12490 // offset
12491 kVarInt62EightBytes + 0x3A, 0x98, 0xFE, 0xDC,
12492 0x32, 0x10, 0x76, 0x54,
12493 // data length
12494 kVarInt62OneByte + 0x0c,
12495 // data
12496 'H', 'E', 'L', 'L',
12497 'O', '_', 'W', 'O',
12498 'R', 'L', 'D', '?',
12499 };
12500 // clang-format on
12501 const size_t length_of_first_coalesced_packet = 46;
12502
12503 QuicEncryptedPacket encrypted(AsChars(packet), QUIC_ARRAYSIZE(packet), false);
12504 EXPECT_FALSE(framer_.ProcessPacket(encrypted));
12505
12506 EXPECT_EQ(QUIC_DECRYPTION_FAILURE, framer_.error());
12507
12508 if (GetQuicRestartFlag(quic_framer_uses_undecryptable_upcall)) {
12509 ASSERT_EQ(1u, visitor_.undecryptable_packets_.size());
12510 ASSERT_EQ(1u, visitor_.undecryptable_decryption_levels_.size());
12511 ASSERT_EQ(1u, visitor_.undecryptable_has_decryption_keys_.size());
12512 // Make sure we only receive the first undecryptable packet and not the
12513 // full packet including the second coalesced packet.
12514 CompareCharArraysWithHexError(
12515 "undecryptable packet", visitor_.undecryptable_packets_[0]->data(),
12516 visitor_.undecryptable_packets_[0]->length(), AsChars(packet),
12517 length_of_first_coalesced_packet);
12518 EXPECT_EQ(ENCRYPTION_HANDSHAKE,
12519 visitor_.undecryptable_decryption_levels_[0]);
12520 EXPECT_TRUE(visitor_.undecryptable_has_decryption_keys_[0]);
12521 } else {
12522 EXPECT_EQ(0u, visitor_.undecryptable_packets_.size());
12523 EXPECT_EQ(0u, visitor_.undecryptable_decryption_levels_.size());
12524 EXPECT_EQ(0u, visitor_.undecryptable_has_decryption_keys_.size());
12525 }
12526
12527 // Make sure the second coalesced packet is parsed correctly.
12528 ASSERT_EQ(visitor_.coalesced_packets_.size(), 1u);
12529 EXPECT_TRUE(framer_.ProcessPacket(*visitor_.coalesced_packets_[0].get()));
12530
12531 ASSERT_TRUE(visitor_.header_.get());
12532
12533 ASSERT_EQ(1u, visitor_.stream_frames_.size());
12534 EXPECT_EQ(0u, visitor_.ack_frames_.size());
12535
12536 // Stream ID should be the last 3 bytes of kStreamId.
12537 EXPECT_EQ(0x00FFFFFF & kStreamId, visitor_.stream_frames_[0]->stream_id);
12538 EXPECT_TRUE(visitor_.stream_frames_[0]->fin);
12539 EXPECT_EQ(kStreamOffset, visitor_.stream_frames_[0]->offset);
12540 CheckStreamFrameData("HELLO_WORLD?", visitor_.stream_frames_[0].get());
12541}
12542
QUICHE teama6ef0a62019-03-07 20:34:33 -050012543TEST_P(QuicFramerTest, MismatchedCoalescedPacket) {
12544 if (!QuicVersionHasLongHeaderLengths(framer_.transport_version())) {
12545 return;
12546 }
zhongyi546cc452019-04-12 15:27:49 -070012547 SetDecrypterLevel(ENCRYPTION_ZERO_RTT);
QUICHE teama6ef0a62019-03-07 20:34:33 -050012548 // clang-format off
12549 unsigned char packet[] = {
12550 // first coalesced packet
12551 // public flags (long header with packet type ZERO_RTT_PROTECTED and
12552 // 4-byte packet number)
12553 0xD3,
12554 // version
12555 QUIC_VERSION_BYTES,
12556 // destination connection ID length
dschinazi48ac9192019-07-31 00:07:26 -070012557 0x08,
QUICHE teama6ef0a62019-03-07 20:34:33 -050012558 // destination connection ID
12559 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
dschinazi48ac9192019-07-31 00:07:26 -070012560 // source connection ID length
12561 0x00,
QUICHE teama6ef0a62019-03-07 20:34:33 -050012562 // long header packet length
12563 0x1E,
12564 // packet number
12565 0x12, 0x34, 0x56, 0x78,
12566 // frame type (IETF_STREAM frame with FIN, LEN, and OFFSET bits set)
12567 0x08 | 0x01 | 0x02 | 0x04,
12568 // stream id
12569 kVarInt62FourBytes + 0x00, 0x02, 0x03, 0x04,
12570 // offset
12571 kVarInt62EightBytes + 0x3A, 0x98, 0xFE, 0xDC,
12572 0x32, 0x10, 0x76, 0x54,
12573 // data length
12574 kVarInt62OneByte + 0x0c,
12575 // data
12576 'h', 'e', 'l', 'l',
12577 'o', ' ', 'w', 'o',
12578 'r', 'l', 'd', '!',
12579 // second coalesced packet
12580 // public flags (long header with packet type ZERO_RTT_PROTECTED and
12581 // 4-byte packet number)
12582 0xD3,
12583 // version
12584 QUIC_VERSION_BYTES,
12585 // destination connection ID length
dschinazi48ac9192019-07-31 00:07:26 -070012586 0x08,
QUICHE teama6ef0a62019-03-07 20:34:33 -050012587 // destination connection ID
12588 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x11,
dschinazi48ac9192019-07-31 00:07:26 -070012589 // source connection ID length
12590 0x00,
QUICHE teama6ef0a62019-03-07 20:34:33 -050012591 // long header packet length
12592 0x1E,
12593 // packet number
12594 0x12, 0x34, 0x56, 0x79,
12595 // frame type (IETF_STREAM frame with FIN, LEN, and OFFSET bits set)
12596 0x08 | 0x01 | 0x02 | 0x04,
12597 // stream id
12598 kVarInt62FourBytes + 0x00, 0x02, 0x03, 0x04,
12599 // offset
12600 kVarInt62EightBytes + 0x3A, 0x98, 0xFE, 0xDC,
12601 0x32, 0x10, 0x76, 0x54,
12602 // data length
12603 kVarInt62OneByte + 0x0c,
12604 // data
12605 'H', 'E', 'L', 'L',
12606 'O', '_', 'W', 'O',
12607 'R', 'L', 'D', '?',
12608 };
12609 // clang-format on
12610
12611 QuicEncryptedPacket encrypted(AsChars(packet), QUIC_ARRAYSIZE(packet), false);
12612 EXPECT_QUIC_PEER_BUG(EXPECT_TRUE(framer_.ProcessPacket(encrypted)),
12613 "Server: Received mismatched coalesced header.*");
12614
12615 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
12616 ASSERT_TRUE(visitor_.header_.get());
12617
12618 ASSERT_EQ(1u, visitor_.stream_frames_.size());
12619 EXPECT_EQ(0u, visitor_.ack_frames_.size());
12620
12621 // Stream ID should be the last 3 bytes of kStreamId.
12622 EXPECT_EQ(0x00FFFFFF & kStreamId, visitor_.stream_frames_[0]->stream_id);
12623 EXPECT_TRUE(visitor_.stream_frames_[0]->fin);
12624 EXPECT_EQ(kStreamOffset, visitor_.stream_frames_[0]->offset);
12625 CheckStreamFrameData("hello world!", visitor_.stream_frames_[0].get());
12626
12627 ASSERT_EQ(visitor_.coalesced_packets_.size(), 0u);
12628}
12629
12630TEST_P(QuicFramerTest, InvalidCoalescedPacket) {
12631 if (!QuicVersionHasLongHeaderLengths(framer_.transport_version())) {
12632 return;
12633 }
zhongyi546cc452019-04-12 15:27:49 -070012634 SetDecrypterLevel(ENCRYPTION_ZERO_RTT);
QUICHE teama6ef0a62019-03-07 20:34:33 -050012635 // clang-format off
12636 unsigned char packet[] = {
12637 // first coalesced packet
12638 // public flags (long header with packet type ZERO_RTT_PROTECTED and
12639 // 4-byte packet number)
12640 0xD3,
12641 // version
12642 QUIC_VERSION_BYTES,
12643 // destination connection ID length
dschinazi48ac9192019-07-31 00:07:26 -070012644 0x08,
QUICHE teama6ef0a62019-03-07 20:34:33 -050012645 // destination connection ID
12646 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
dschinazi48ac9192019-07-31 00:07:26 -070012647 // source connection ID length
12648 0x00,
QUICHE teama6ef0a62019-03-07 20:34:33 -050012649 // long header packet length
12650 0x1E,
12651 // packet number
12652 0x12, 0x34, 0x56, 0x78,
12653 // frame type (IETF_STREAM frame with FIN, LEN, and OFFSET bits set)
12654 0x08 | 0x01 | 0x02 | 0x04,
12655 // stream id
12656 kVarInt62FourBytes + 0x00, 0x02, 0x03, 0x04,
12657 // offset
12658 kVarInt62EightBytes + 0x3A, 0x98, 0xFE, 0xDC,
12659 0x32, 0x10, 0x76, 0x54,
12660 // data length
12661 kVarInt62OneByte + 0x0c,
12662 // data
12663 'h', 'e', 'l', 'l',
12664 'o', ' ', 'w', 'o',
12665 'r', 'l', 'd', '!',
12666 // second coalesced packet
12667 // public flags (long header with packet type ZERO_RTT_PROTECTED and
12668 // 4-byte packet number)
12669 0xD3,
12670 // version would be here but we cut off the invalid coalesced header.
12671 };
12672 // clang-format on
12673
12674 QuicEncryptedPacket encrypted(AsChars(packet), QUIC_ARRAYSIZE(packet), false);
12675 EXPECT_QUIC_PEER_BUG(EXPECT_TRUE(framer_.ProcessPacket(encrypted)),
12676 "Server: Failed to parse received coalesced header.*");
12677
12678 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
12679 ASSERT_TRUE(visitor_.header_.get());
12680
12681 ASSERT_EQ(1u, visitor_.stream_frames_.size());
12682 EXPECT_EQ(0u, visitor_.ack_frames_.size());
12683
12684 // Stream ID should be the last 3 bytes of kStreamId.
12685 EXPECT_EQ(0x00FFFFFF & kStreamId, visitor_.stream_frames_[0]->stream_id);
12686 EXPECT_TRUE(visitor_.stream_frames_[0]->fin);
12687 EXPECT_EQ(kStreamOffset, visitor_.stream_frames_[0]->offset);
12688 CheckStreamFrameData("hello world!", visitor_.stream_frames_[0].get());
12689
12690 ASSERT_EQ(visitor_.coalesced_packets_.size(), 0u);
12691}
12692
dschinazia484f982019-05-23 03:54:44 -070012693// Some IETF implementations send an initial followed by zeroes instead of
12694// padding inside the initial. We need to make sure that we still process
12695// the initial correctly and ignore the zeroes.
12696TEST_P(QuicFramerTest, CoalescedPacketWithZeroesRoundTrip) {
nharperc8d9e402019-09-12 18:30:14 -070012697 if (!QuicVersionHasLongHeaderLengths(framer_.transport_version()) ||
12698 !framer_.version().UsesInitialObfuscators()) {
dschinazia484f982019-05-23 03:54:44 -070012699 return;
12700 }
12701 ASSERT_TRUE(framer_.version().KnowsWhichDecrypterToUse());
12702 QuicConnectionId connection_id = FramerTestConnectionId();
12703 QuicFramerPeer::SetPerspective(&framer_, Perspective::IS_CLIENT);
12704
12705 CrypterPair client_crypters;
nharperc8d9e402019-09-12 18:30:14 -070012706 CryptoUtils::CreateInitialObfuscators(Perspective::IS_CLIENT,
12707 framer_.version(), connection_id,
12708 &client_crypters);
dschinazia484f982019-05-23 03:54:44 -070012709 framer_.SetEncrypter(ENCRYPTION_INITIAL,
12710 std::move(client_crypters.encrypter));
12711
12712 QuicPacketHeader header;
12713 header.destination_connection_id = connection_id;
12714 header.version_flag = true;
12715 header.packet_number = kPacketNumber;
12716 header.packet_number_length = PACKET_4BYTE_PACKET_NUMBER;
12717 header.long_packet_type = INITIAL;
12718 header.length_length = VARIABLE_LENGTH_INTEGER_LENGTH_2;
12719 header.retry_token_length_length = VARIABLE_LENGTH_INTEGER_LENGTH_1;
12720 QuicFrames frames = {QuicFrame(QuicPingFrame())};
12721
12722 std::unique_ptr<QuicPacket> data(BuildDataPacket(header, frames));
12723 ASSERT_NE(nullptr, data);
12724
12725 // Add zeroes after the valid initial packet.
12726 unsigned char packet[kMaxOutgoingPacketSize] = {};
12727 size_t encrypted_length =
12728 framer_.EncryptPayload(ENCRYPTION_INITIAL, header.packet_number, *data,
12729 AsChars(packet), QUIC_ARRAYSIZE(packet));
12730 ASSERT_NE(0u, encrypted_length);
12731
12732 QuicFramerPeer::SetPerspective(&framer_, Perspective::IS_SERVER);
12733 CrypterPair server_crypters;
nharperc8d9e402019-09-12 18:30:14 -070012734 CryptoUtils::CreateInitialObfuscators(Perspective::IS_SERVER,
12735 framer_.version(), connection_id,
12736 &server_crypters);
dschinazia484f982019-05-23 03:54:44 -070012737 framer_.InstallDecrypter(ENCRYPTION_INITIAL,
12738 std::move(server_crypters.decrypter));
12739
12740 // Make sure the first long header initial packet parses correctly.
12741 QuicEncryptedPacket encrypted(AsChars(packet), QUIC_ARRAYSIZE(packet), false);
12742
12743 // Make sure we discard the subsequent zeroes.
12744 EXPECT_QUIC_PEER_BUG(EXPECT_TRUE(framer_.ProcessPacket(encrypted)),
dschinazi48ac9192019-07-31 00:07:26 -070012745 "Server: (Failed to parse received|Received mismatched) "
12746 "coalesced header.*");
dschinazia484f982019-05-23 03:54:44 -070012747}
12748
dschinazie0df3f72019-05-06 16:37:51 -070012749TEST_P(QuicFramerTest, ClientReceivesInvalidVersion) {
fayang36825da2019-08-21 14:01:27 -070012750 if (framer_.transport_version() <= QUIC_VERSION_43) {
dschinazie0df3f72019-05-06 16:37:51 -070012751 return;
12752 }
12753 QuicFramerPeer::SetPerspective(&framer_, Perspective::IS_CLIENT);
12754
12755 // clang-format off
12756 unsigned char packet[] = {
12757 // public flags (long header with packet type INITIAL)
fayang36825da2019-08-21 14:01:27 -070012758 0xC3,
dschinazie0df3f72019-05-06 16:37:51 -070012759 // version that is different from the framer's version
12760 'Q', '0', '4', '3',
12761 // connection ID lengths
12762 0x05,
12763 // source connection ID
12764 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
12765 // packet number
12766 0x01,
12767 // padding frame
12768 0x00,
12769 };
12770 // clang-format on
12771
12772 QuicEncryptedPacket encrypted(AsChars(packet), QUIC_ARRAYSIZE(packet), false);
12773 EXPECT_FALSE(framer_.ProcessPacket(encrypted));
12774
12775 EXPECT_EQ(QUIC_INVALID_VERSION, framer_.error());
12776 EXPECT_EQ("Client received unexpected version.", framer_.detailed_error());
12777}
12778
QUICHE teama6ef0a62019-03-07 20:34:33 -050012779TEST_P(QuicFramerTest, PacketHeaderWithVariableLengthConnectionId) {
dschinazib953d022019-08-01 18:05:58 -070012780 if (!QuicUtils::VariableLengthConnectionIdAllowedForVersion(
12781 framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -050012782 return;
12783 }
zhongyi546cc452019-04-12 15:27:49 -070012784 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
QUICHE teama6ef0a62019-03-07 20:34:33 -050012785 char connection_id_bytes[9] = {0xFE, 0xDC, 0xBA, 0x98, 0x76,
12786 0x54, 0x32, 0x10, 0x42};
12787 QuicConnectionId connection_id(connection_id_bytes,
12788 sizeof(connection_id_bytes));
12789 QuicFramerPeer::SetLargestPacketNumber(&framer_, kPacketNumber - 2);
dschinazi8ff74822019-05-28 16:37:20 -070012790 QuicFramerPeer::SetExpectedServerConnectionIDLength(&framer_,
12791 connection_id.length());
QUICHE teama6ef0a62019-03-07 20:34:33 -050012792
12793 // clang-format off
12794 PacketFragments packet = {
12795 // type (8 byte connection_id and 1 byte packet number)
dschinazi48ac9192019-07-31 00:07:26 -070012796 {"Unable to read first byte.",
QUICHE teama6ef0a62019-03-07 20:34:33 -050012797 {0x40}},
12798 // connection_id
dschinazi48ac9192019-07-31 00:07:26 -070012799 {"Unable to read destination connection ID.",
QUICHE teama6ef0a62019-03-07 20:34:33 -050012800 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10, 0x42}},
12801 // packet number
12802 {"Unable to read packet number.",
12803 {0x78}},
12804 };
nharper55fa6132019-05-07 19:37:21 -070012805
12806 PacketFragments packet_with_padding = {
12807 // type (8 byte connection_id and 1 byte packet number)
dschinazi48ac9192019-07-31 00:07:26 -070012808 {"Unable to read first byte.",
nharper55fa6132019-05-07 19:37:21 -070012809 {0x40}},
12810 // connection_id
dschinazi48ac9192019-07-31 00:07:26 -070012811 {"Unable to read destination connection ID.",
nharper55fa6132019-05-07 19:37:21 -070012812 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10, 0x42}},
12813 // packet number
12814 {"",
12815 {0x78}},
12816 // padding
12817 {"", {0x00, 0x00, 0x00}},
12818 };
QUICHE teama6ef0a62019-03-07 20:34:33 -050012819 // clang-format on
12820
nharper55fa6132019-05-07 19:37:21 -070012821 PacketFragments& fragments =
12822 framer_.version().HasHeaderProtection() ? packet_with_padding : packet;
QUICHE teama6ef0a62019-03-07 20:34:33 -050012823 std::unique_ptr<QuicEncryptedPacket> encrypted(
nharper55fa6132019-05-07 19:37:21 -070012824 AssemblePacketFromFragments(fragments));
12825 if (framer_.version().HasHeaderProtection()) {
12826 EXPECT_TRUE(framer_.ProcessPacket(*encrypted));
12827 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
12828 } else {
12829 EXPECT_FALSE(framer_.ProcessPacket(*encrypted));
12830 EXPECT_EQ(QUIC_MISSING_PAYLOAD, framer_.error());
12831 }
QUICHE teama6ef0a62019-03-07 20:34:33 -050012832 ASSERT_TRUE(visitor_.header_.get());
12833 EXPECT_EQ(connection_id, visitor_.header_->destination_connection_id);
12834 EXPECT_FALSE(visitor_.header_->reset_flag);
12835 EXPECT_FALSE(visitor_.header_->version_flag);
12836 EXPECT_EQ(PACKET_1BYTE_PACKET_NUMBER, visitor_.header_->packet_number_length);
12837 EXPECT_EQ(kPacketNumber, visitor_.header_->packet_number);
12838
nharper55fa6132019-05-07 19:37:21 -070012839 CheckFramingBoundaries(fragments, QUIC_INVALID_PACKET_HEADER);
QUICHE teama6ef0a62019-03-07 20:34:33 -050012840}
12841
QUICHE team10b22a12019-03-21 15:31:42 -070012842TEST_P(QuicFramerTest, MultiplePacketNumberSpaces) {
12843 if (framer_.transport_version() < QUIC_VERSION_46) {
12844 return;
12845 }
QUICHE team10b22a12019-03-21 15:31:42 -070012846 framer_.EnableMultiplePacketNumberSpacesSupport();
12847
12848 // clang-format off
12849 unsigned char long_header_packet[] = {
12850 // public flags (long header with packet type ZERO_RTT_PROTECTED and
12851 // 4-byte packet number)
12852 0xD3,
12853 // version
12854 QUIC_VERSION_BYTES,
12855 // destination connection ID length
fayang91475c42019-06-19 08:04:26 -070012856 0x50,
QUICHE team10b22a12019-03-21 15:31:42 -070012857 // destination connection ID
fayang91475c42019-06-19 08:04:26 -070012858 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
QUICHE team10b22a12019-03-21 15:31:42 -070012859 // packet number
12860 0x12, 0x34, 0x56, 0x78,
12861 // padding frame
12862 0x00,
12863 };
12864 unsigned char long_header_packet99[] = {
12865 // public flags (long header with packet type ZERO_RTT_PROTECTED and
12866 // 4-byte packet number)
12867 0xD3,
12868 // version
12869 QUIC_VERSION_BYTES,
12870 // destination connection ID length
dschinazi48ac9192019-07-31 00:07:26 -070012871 0x08,
QUICHE team10b22a12019-03-21 15:31:42 -070012872 // destination connection ID
fayang91475c42019-06-19 08:04:26 -070012873 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
dschinazi48ac9192019-07-31 00:07:26 -070012874 // source connection ID length
12875 0x00,
QUICHE team10b22a12019-03-21 15:31:42 -070012876 // long header packet length
12877 0x05,
12878 // packet number
12879 0x12, 0x34, 0x56, 0x78,
12880 // padding frame
12881 0x00,
12882 };
12883 // clang-format on
12884
zhongyi546cc452019-04-12 15:27:49 -070012885 if (framer_.version().KnowsWhichDecrypterToUse()) {
12886 framer_.InstallDecrypter(ENCRYPTION_ZERO_RTT,
vasilvv0fc587f2019-09-06 13:33:08 -070012887 std::make_unique<TestDecrypter>());
zhongyi546cc452019-04-12 15:27:49 -070012888 framer_.RemoveDecrypter(ENCRYPTION_INITIAL);
12889 } else {
vasilvv0fc587f2019-09-06 13:33:08 -070012890 framer_.SetDecrypter(ENCRYPTION_ZERO_RTT,
12891 std::make_unique<TestDecrypter>());
zhongyi546cc452019-04-12 15:27:49 -070012892 }
QUICHE team10b22a12019-03-21 15:31:42 -070012893 if (!QuicVersionHasLongHeaderLengths(framer_.transport_version())) {
12894 EXPECT_TRUE(framer_.ProcessPacket(
12895 QuicEncryptedPacket(AsChars(long_header_packet),
12896 QUIC_ARRAYSIZE(long_header_packet), false)));
12897 } else {
12898 EXPECT_TRUE(framer_.ProcessPacket(
12899 QuicEncryptedPacket(AsChars(long_header_packet99),
12900 QUIC_ARRAYSIZE(long_header_packet99), false)));
12901 }
12902
12903 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
12904 EXPECT_FALSE(
12905 QuicFramerPeer::GetLargestDecryptedPacketNumber(&framer_, INITIAL_DATA)
12906 .IsInitialized());
12907 EXPECT_FALSE(
12908 QuicFramerPeer::GetLargestDecryptedPacketNumber(&framer_, HANDSHAKE_DATA)
12909 .IsInitialized());
12910 EXPECT_EQ(kPacketNumber, QuicFramerPeer::GetLargestDecryptedPacketNumber(
12911 &framer_, APPLICATION_DATA));
12912
12913 // clang-format off
12914 unsigned char short_header_packet[] = {
12915 // type (short header, 1 byte packet number)
12916 0x40,
12917 // connection_id
fayang91475c42019-06-19 08:04:26 -070012918 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
QUICHE team10b22a12019-03-21 15:31:42 -070012919 // packet number
12920 0x79,
12921 // padding frame
nharper55fa6132019-05-07 19:37:21 -070012922 0x00, 0x00, 0x00,
QUICHE team10b22a12019-03-21 15:31:42 -070012923 };
12924 // clang-format on
12925
12926 QuicEncryptedPacket short_header_encrypted(
12927 AsChars(short_header_packet), QUIC_ARRAYSIZE(short_header_packet), false);
zhongyi546cc452019-04-12 15:27:49 -070012928 if (framer_.version().KnowsWhichDecrypterToUse()) {
12929 framer_.InstallDecrypter(ENCRYPTION_FORWARD_SECURE,
vasilvv0fc587f2019-09-06 13:33:08 -070012930 std::make_unique<TestDecrypter>());
zhongyi546cc452019-04-12 15:27:49 -070012931 framer_.RemoveDecrypter(ENCRYPTION_ZERO_RTT);
12932 } else {
12933 framer_.SetDecrypter(ENCRYPTION_FORWARD_SECURE,
vasilvv0fc587f2019-09-06 13:33:08 -070012934 std::make_unique<TestDecrypter>());
zhongyi546cc452019-04-12 15:27:49 -070012935 }
QUICHE team10b22a12019-03-21 15:31:42 -070012936 EXPECT_TRUE(framer_.ProcessPacket(short_header_encrypted));
12937
12938 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
12939 EXPECT_FALSE(
12940 QuicFramerPeer::GetLargestDecryptedPacketNumber(&framer_, INITIAL_DATA)
12941 .IsInitialized());
12942 EXPECT_FALSE(
12943 QuicFramerPeer::GetLargestDecryptedPacketNumber(&framer_, HANDSHAKE_DATA)
12944 .IsInitialized());
12945 EXPECT_EQ(kPacketNumber + 1, QuicFramerPeer::GetLargestDecryptedPacketNumber(
12946 &framer_, APPLICATION_DATA));
12947}
12948
nharper2ceb97c2019-04-19 11:38:59 -070012949TEST_P(QuicFramerTest, IetfRetryPacketRejected) {
dschinazi244f6dc2019-05-06 15:45:16 -070012950 if (!framer_.version().KnowsWhichDecrypterToUse() ||
12951 framer_.version().SupportsRetry()) {
nharper2ceb97c2019-04-19 11:38:59 -070012952 return;
12953 }
12954
12955 // clang-format off
fayang36825da2019-08-21 14:01:27 -070012956 PacketFragments packet46 = {
nharper9bb83462019-05-01 10:53:22 -070012957 // public flags (IETF Retry packet, 0-length original destination CID)
dschinazi48ac9192019-07-31 00:07:26 -070012958 {"Unable to read first byte.",
nharper9bb83462019-05-01 10:53:22 -070012959 {0xf0}},
12960 // version tag
12961 {"Unable to read protocol version.",
12962 {QUIC_VERSION_BYTES}},
12963 // connection_id length
dschinazi244f6dc2019-05-06 15:45:16 -070012964 {"RETRY not supported in this version.",
nharper2ceb97c2019-04-19 11:38:59 -070012965 {0x00}},
12966 };
12967 // clang-format on
12968
12969 std::unique_ptr<QuicEncryptedPacket> encrypted(
fayang36825da2019-08-21 14:01:27 -070012970 AssemblePacketFromFragments(packet46));
nharper2ceb97c2019-04-19 11:38:59 -070012971
12972 EXPECT_FALSE(framer_.ProcessPacket(*encrypted));
12973 EXPECT_EQ(QUIC_INVALID_PACKET_HEADER, framer_.error());
fayang36825da2019-08-21 14:01:27 -070012974 CheckFramingBoundaries(packet46, QUIC_INVALID_PACKET_HEADER);
nharper2ceb97c2019-04-19 11:38:59 -070012975}
12976
12977TEST_P(QuicFramerTest, RetryPacketRejectedWithMultiplePacketNumberSpaces) {
dschinazi244f6dc2019-05-06 15:45:16 -070012978 if (framer_.transport_version() < QUIC_VERSION_46 ||
12979 framer_.version().SupportsRetry()) {
nharper2ceb97c2019-04-19 11:38:59 -070012980 return;
12981 }
12982 framer_.EnableMultiplePacketNumberSpacesSupport();
12983
12984 // clang-format off
12985 PacketFragments packet = {
12986 // public flags (IETF Retry packet, 0-length original destination CID)
dschinazi48ac9192019-07-31 00:07:26 -070012987 {"Unable to read first byte.",
nharper2ceb97c2019-04-19 11:38:59 -070012988 {0xf0}},
12989 // version tag
12990 {"Unable to read protocol version.",
12991 {QUIC_VERSION_BYTES}},
12992 // connection_id length
dschinazi244f6dc2019-05-06 15:45:16 -070012993 {"RETRY not supported in this version.",
nharper2ceb97c2019-04-19 11:38:59 -070012994 {0x00}},
12995 };
12996 // clang-format on
12997
12998 std::unique_ptr<QuicEncryptedPacket> encrypted(
12999 AssemblePacketFromFragments(packet));
13000
13001 EXPECT_FALSE(framer_.ProcessPacket(*encrypted));
13002 EXPECT_EQ(QUIC_INVALID_PACKET_HEADER, framer_.error());
13003 CheckFramingBoundaries(packet, QUIC_INVALID_PACKET_HEADER);
13004}
13005
nharpera745e392019-04-19 12:05:15 -070013006TEST_P(QuicFramerTest, ProcessPublicHeaderNoVersionInferredType) {
13007 // The framer needs to have Perspective::IS_SERVER and configured to infer the
13008 // packet header type from the packet (not the version). The framer's version
13009 // needs to be one that uses the IETF packet format.
13010 if (!framer_.version().KnowsWhichDecrypterToUse()) {
13011 return;
13012 }
13013 QuicFramerPeer::SetPerspective(&framer_, Perspective::IS_SERVER);
13014
13015 // Prepare a packet that uses the Google QUIC packet header but has no version
13016 // field.
13017
13018 // clang-format off
13019 PacketFragments packet = {
13020 // public flags (1-byte packet number, 8-byte connection_id, no version)
13021 {"Unable to read public flags.",
13022 {0x08}},
13023 // connection_id
13024 {"Unable to read ConnectionId.",
13025 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
13026 // packet number
13027 {"Unable to read packet number.",
13028 {0x01}},
13029 // padding
13030 {"Invalid public header type for expected version.",
13031 {0x00}},
13032 };
13033 // clang-format on
13034
13035 PacketFragments& fragments = packet;
13036
13037 std::unique_ptr<QuicEncryptedPacket> encrypted(
13038 AssemblePacketFromFragments(fragments));
13039
13040 EXPECT_FALSE(framer_.ProcessPacket(*encrypted));
13041 EXPECT_EQ(QUIC_INVALID_PACKET_HEADER, framer_.error());
nharper8b6d63a2019-05-03 10:34:53 -070013042 EXPECT_EQ("Invalid public header type for expected version.",
13043 framer_.detailed_error());
nharpera745e392019-04-19 12:05:15 -070013044 CheckFramingBoundaries(fragments, QUIC_INVALID_PACKET_HEADER);
13045}
13046
nharper3f283562019-05-02 16:37:12 -070013047TEST_P(QuicFramerTest, ProcessMismatchedHeaderVersion) {
13048 // The framer needs to have Perspective::IS_SERVER and configured to infer the
13049 // packet header type from the packet (not the version). The framer's version
13050 // needs to be one that uses the IETF packet format.
13051 if (!framer_.version().KnowsWhichDecrypterToUse()) {
13052 return;
13053 }
13054 QuicFramerPeer::SetPerspective(&framer_, Perspective::IS_SERVER);
13055
13056 // clang-format off
13057 PacketFragments packet = {
dschinazi072da7c2019-05-07 17:57:42 -070013058 // public flags (Google QUIC header with version present)
nharper3f283562019-05-02 16:37:12 -070013059 {"Unable to read public flags.",
13060 {0x09}},
13061 // connection_id
13062 {"Unable to read ConnectionId.",
13063 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
13064 // version tag
13065 {"Unable to read protocol version.",
13066 {QUIC_VERSION_BYTES}},
13067 // packet number
13068 {"Unable to read packet number.",
13069 {0x01}},
13070 };
13071 // clang-format on
13072
13073 std::unique_ptr<QuicEncryptedPacket> encrypted(
13074 AssemblePacketFromFragments(packet));
13075 framer_.ProcessPacket(*encrypted);
13076
13077 EXPECT_FALSE(framer_.ProcessPacket(*encrypted));
13078 EXPECT_EQ(QUIC_INVALID_PACKET_HEADER, framer_.error());
nharper8b6d63a2019-05-03 10:34:53 -070013079 EXPECT_EQ("Invalid public header type for expected version.",
13080 framer_.detailed_error());
nharper3f283562019-05-02 16:37:12 -070013081 CheckFramingBoundaries(packet, QUIC_INVALID_PACKET_HEADER);
13082}
13083
dschinazi48ac9192019-07-31 00:07:26 -070013084TEST_P(QuicFramerTest, WriteClientVersionNegotiationProbePacketOld) {
13085 SetQuicFlag(FLAGS_quic_prober_uses_length_prefixed_connection_ids, false);
dschinazide0f6dc2019-05-15 16:10:11 -070013086 // clang-format off
13087 static const char expected_packet[1200] = {
13088 // IETF long header with fixed bit set, type initial, all-0 encrypted bits.
13089 0xc0,
13090 // Version, part of the IETF space reserved for negotiation.
13091 0xca, 0xba, 0xda, 0xba,
13092 // Destination connection ID length 8, source connection ID length 0.
13093 0x50,
13094 // 8-byte destination connection ID.
13095 0x56, 0x4e, 0x20, 0x70, 0x6c, 0x7a, 0x20, 0x21,
13096 // 8 bytes of zeroes followed by 8 bytes of ones to ensure that this does
13097 // not parse with any known version.
13098 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
13099 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
13100 // 2 bytes of zeroes to pad to 16 byte boundary.
13101 0x00, 0x00,
13102 // A polite greeting in case a human sees this in tcpdump.
13103 0x54, 0x68, 0x69, 0x73, 0x20, 0x70, 0x61, 0x63,
13104 0x6b, 0x65, 0x74, 0x20, 0x6f, 0x6e, 0x6c, 0x79,
13105 0x20, 0x65, 0x78, 0x69, 0x73, 0x74, 0x73, 0x20,
13106 0x74, 0x6f, 0x20, 0x74, 0x72, 0x69, 0x67, 0x67,
13107 0x65, 0x72, 0x20, 0x49, 0x45, 0x54, 0x46, 0x20,
13108 0x51, 0x55, 0x49, 0x43, 0x20, 0x76, 0x65, 0x72,
13109 0x73, 0x69, 0x6f, 0x6e, 0x20, 0x6e, 0x65, 0x67,
13110 0x6f, 0x74, 0x69, 0x61, 0x74, 0x69, 0x6f, 0x6e,
13111 0x2e, 0x20, 0x50, 0x6c, 0x65, 0x61, 0x73, 0x65,
13112 0x20, 0x72, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x64,
13113 0x20, 0x77, 0x69, 0x74, 0x68, 0x20, 0x61, 0x20,
13114 0x56, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x20,
13115 0x4e, 0x65, 0x67, 0x6f, 0x74, 0x69, 0x61, 0x74,
13116 0x69, 0x6f, 0x6e, 0x20, 0x70, 0x61, 0x63, 0x6b,
13117 0x65, 0x74, 0x20, 0x69, 0x6e, 0x64, 0x69, 0x63,
13118 0x61, 0x74, 0x69, 0x6e, 0x67, 0x20, 0x77, 0x68,
13119 0x61, 0x74, 0x20, 0x76, 0x65, 0x72, 0x73, 0x69,
13120 0x6f, 0x6e, 0x73, 0x20, 0x79, 0x6f, 0x75, 0x20,
13121 0x73, 0x75, 0x70, 0x70, 0x6f, 0x72, 0x74, 0x2e,
13122 0x20, 0x54, 0x68, 0x61, 0x6e, 0x6b, 0x20, 0x79,
13123 0x6f, 0x75, 0x20, 0x61, 0x6e, 0x64, 0x20, 0x68,
13124 0x61, 0x76, 0x65, 0x20, 0x61, 0x20, 0x6e, 0x69,
13125 0x63, 0x65, 0x20, 0x64, 0x61, 0x79, 0x2e, 0x00,
13126 };
13127 // clang-format on
13128 char packet[1200];
13129 char destination_connection_id_bytes[] = {0x56, 0x4e, 0x20, 0x70,
13130 0x6c, 0x7a, 0x20, 0x21};
13131 EXPECT_TRUE(QuicFramer::WriteClientVersionNegotiationProbePacket(
13132 packet, sizeof(packet), destination_connection_id_bytes,
13133 sizeof(destination_connection_id_bytes)));
dschinazi48ac9192019-07-31 00:07:26 -070013134 test::CompareCharArraysWithHexError("constructed packet", packet,
13135 sizeof(packet), expected_packet,
13136 sizeof(expected_packet));
dschinazide0f6dc2019-05-15 16:10:11 -070013137 QuicEncryptedPacket encrypted(reinterpret_cast<const char*>(packet),
13138 sizeof(packet), false);
13139 // Make sure we fail to parse this packet for the version under test.
dschinazide0f6dc2019-05-15 16:10:11 -070013140 if (framer_.transport_version() <= QUIC_VERSION_43) {
13141 // We can only parse the connection ID with an IETF parser.
dschinazi48ac9192019-07-31 00:07:26 -070013142 EXPECT_FALSE(framer_.ProcessPacket(encrypted));
dschinazide0f6dc2019-05-15 16:10:11 -070013143 return;
13144 }
dschinazi48ac9192019-07-31 00:07:26 -070013145 EXPECT_TRUE(framer_.ProcessPacket(encrypted));
dschinazide0f6dc2019-05-15 16:10:11 -070013146 ASSERT_TRUE(visitor_.header_.get());
13147 QuicConnectionId probe_payload_connection_id(
13148 reinterpret_cast<const char*>(destination_connection_id_bytes),
13149 sizeof(destination_connection_id_bytes));
13150 EXPECT_EQ(probe_payload_connection_id,
13151 visitor_.header_.get()->destination_connection_id);
dschinazi30ab6db2019-08-13 14:43:32 -070013152
13153 PacketHeaderFormat format = GOOGLE_QUIC_PACKET;
13154 bool version_present = false, has_length_prefix = false;
13155 QuicVersionLabel version_label = 0;
13156 ParsedQuicVersion parsed_version = QuicVersionReservedForNegotiation();
13157 QuicConnectionId destination_connection_id = TestConnectionId(0x33);
13158 QuicConnectionId source_connection_id = TestConnectionId(0x34);
13159 bool retry_token_present = true;
13160 QuicStringPiece retry_token;
13161 std::string detailed_error = "foobar";
13162
13163 QuicErrorCode parse_result = QuicFramer::ParsePublicHeaderDispatcher(
13164 encrypted, kQuicDefaultConnectionIdLength, &format, &version_present,
13165 &has_length_prefix, &version_label, &parsed_version,
13166 &destination_connection_id, &source_connection_id, &retry_token_present,
13167 &retry_token, &detailed_error);
13168 EXPECT_EQ(QUIC_NO_ERROR, parse_result);
13169 EXPECT_EQ(IETF_QUIC_LONG_HEADER_PACKET, format);
13170 EXPECT_TRUE(version_present);
13171 EXPECT_FALSE(has_length_prefix);
13172 EXPECT_EQ(0xcabadaba, version_label);
13173 EXPECT_EQ(QUIC_VERSION_UNSUPPORTED, parsed_version.transport_version);
13174 EXPECT_EQ(probe_payload_connection_id, destination_connection_id);
13175 EXPECT_EQ(EmptyQuicConnectionId(), source_connection_id);
13176 EXPECT_FALSE(retry_token_present);
13177 EXPECT_EQ(QuicStringPiece(), retry_token);
13178 EXPECT_EQ("", detailed_error);
dschinazide0f6dc2019-05-15 16:10:11 -070013179}
13180
dschinazi48ac9192019-07-31 00:07:26 -070013181TEST_P(QuicFramerTest, WriteClientVersionNegotiationProbePacket) {
13182 SetQuicFlag(FLAGS_quic_prober_uses_length_prefixed_connection_ids, true);
13183 // clang-format off
13184 static const char expected_packet[1200] = {
13185 // IETF long header with fixed bit set, type initial, all-0 encrypted bits.
13186 0xc0,
13187 // Version, part of the IETF space reserved for negotiation.
13188 0xca, 0xba, 0xda, 0xda,
13189 // Destination connection ID length 8.
13190 0x08,
13191 // 8-byte destination connection ID.
13192 0x56, 0x4e, 0x20, 0x70, 0x6c, 0x7a, 0x20, 0x21,
13193 // Source connection ID length 0.
13194 0x00,
13195 // 8 bytes of zeroes followed by 8 bytes of ones to ensure that this does
13196 // not parse with any known version.
13197 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
13198 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
13199 // zeroes to pad to 16 byte boundary.
13200 0x00,
13201 // A polite greeting in case a human sees this in tcpdump.
13202 0x54, 0x68, 0x69, 0x73, 0x20, 0x70, 0x61, 0x63,
13203 0x6b, 0x65, 0x74, 0x20, 0x6f, 0x6e, 0x6c, 0x79,
13204 0x20, 0x65, 0x78, 0x69, 0x73, 0x74, 0x73, 0x20,
13205 0x74, 0x6f, 0x20, 0x74, 0x72, 0x69, 0x67, 0x67,
13206 0x65, 0x72, 0x20, 0x49, 0x45, 0x54, 0x46, 0x20,
13207 0x51, 0x55, 0x49, 0x43, 0x20, 0x76, 0x65, 0x72,
13208 0x73, 0x69, 0x6f, 0x6e, 0x20, 0x6e, 0x65, 0x67,
13209 0x6f, 0x74, 0x69, 0x61, 0x74, 0x69, 0x6f, 0x6e,
13210 0x2e, 0x20, 0x50, 0x6c, 0x65, 0x61, 0x73, 0x65,
13211 0x20, 0x72, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x64,
13212 0x20, 0x77, 0x69, 0x74, 0x68, 0x20, 0x61, 0x20,
13213 0x56, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x20,
13214 0x4e, 0x65, 0x67, 0x6f, 0x74, 0x69, 0x61, 0x74,
13215 0x69, 0x6f, 0x6e, 0x20, 0x70, 0x61, 0x63, 0x6b,
13216 0x65, 0x74, 0x20, 0x69, 0x6e, 0x64, 0x69, 0x63,
13217 0x61, 0x74, 0x69, 0x6e, 0x67, 0x20, 0x77, 0x68,
13218 0x61, 0x74, 0x20, 0x76, 0x65, 0x72, 0x73, 0x69,
13219 0x6f, 0x6e, 0x73, 0x20, 0x79, 0x6f, 0x75, 0x20,
13220 0x73, 0x75, 0x70, 0x70, 0x6f, 0x72, 0x74, 0x2e,
13221 0x20, 0x54, 0x68, 0x61, 0x6e, 0x6b, 0x20, 0x79,
13222 0x6f, 0x75, 0x20, 0x61, 0x6e, 0x64, 0x20, 0x68,
13223 0x61, 0x76, 0x65, 0x20, 0x61, 0x20, 0x6e, 0x69,
13224 0x63, 0x65, 0x20, 0x64, 0x61, 0x79, 0x2e, 0x00,
13225 };
13226 // clang-format on
13227 char packet[1200];
13228 char destination_connection_id_bytes[] = {0x56, 0x4e, 0x20, 0x70,
13229 0x6c, 0x7a, 0x20, 0x21};
13230 EXPECT_TRUE(QuicFramer::WriteClientVersionNegotiationProbePacket(
13231 packet, sizeof(packet), destination_connection_id_bytes,
13232 sizeof(destination_connection_id_bytes)));
13233 test::CompareCharArraysWithHexError("constructed packet", packet,
13234 sizeof(packet), expected_packet,
13235 sizeof(expected_packet));
13236 QuicEncryptedPacket encrypted(reinterpret_cast<const char*>(packet),
13237 sizeof(packet), false);
13238 if (framer_.transport_version() < QUIC_VERSION_99) {
13239 // We can only parse the connection ID with a v99 parser.
13240 EXPECT_FALSE(framer_.ProcessPacket(encrypted));
13241 return;
13242 }
13243 EXPECT_TRUE(framer_.ProcessPacket(encrypted));
13244 ASSERT_TRUE(visitor_.header_.get());
13245 QuicConnectionId probe_payload_connection_id(
13246 reinterpret_cast<const char*>(destination_connection_id_bytes),
13247 sizeof(destination_connection_id_bytes));
13248 EXPECT_EQ(probe_payload_connection_id,
13249 visitor_.header_.get()->destination_connection_id);
13250}
13251
13252TEST_P(QuicFramerTest, DispatcherParseOldClientVersionNegotiationProbePacket) {
13253 // clang-format off
13254 static const char packet[1200] = {
13255 // IETF long header with fixed bit set, type initial, all-0 encrypted bits.
13256 0xc0,
13257 // Version, part of the IETF space reserved for negotiation.
13258 0xca, 0xba, 0xda, 0xba,
13259 // Destination connection ID length 8, source connection ID length 0.
13260 0x50,
13261 // 8-byte destination connection ID.
13262 0x56, 0x4e, 0x20, 0x70, 0x6c, 0x7a, 0x20, 0x21,
13263 // 8 bytes of zeroes followed by 8 bytes of ones to ensure that this does
13264 // not parse with any known version.
13265 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
13266 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
13267 // 2 bytes of zeroes to pad to 16 byte boundary.
13268 0x00, 0x00,
13269 // A polite greeting in case a human sees this in tcpdump.
13270 0x54, 0x68, 0x69, 0x73, 0x20, 0x70, 0x61, 0x63,
13271 0x6b, 0x65, 0x74, 0x20, 0x6f, 0x6e, 0x6c, 0x79,
13272 0x20, 0x65, 0x78, 0x69, 0x73, 0x74, 0x73, 0x20,
13273 0x74, 0x6f, 0x20, 0x74, 0x72, 0x69, 0x67, 0x67,
13274 0x65, 0x72, 0x20, 0x49, 0x45, 0x54, 0x46, 0x20,
13275 0x51, 0x55, 0x49, 0x43, 0x20, 0x76, 0x65, 0x72,
13276 0x73, 0x69, 0x6f, 0x6e, 0x20, 0x6e, 0x65, 0x67,
13277 0x6f, 0x74, 0x69, 0x61, 0x74, 0x69, 0x6f, 0x6e,
13278 0x2e, 0x20, 0x50, 0x6c, 0x65, 0x61, 0x73, 0x65,
13279 0x20, 0x72, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x64,
13280 0x20, 0x77, 0x69, 0x74, 0x68, 0x20, 0x61, 0x20,
13281 0x56, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x20,
13282 0x4e, 0x65, 0x67, 0x6f, 0x74, 0x69, 0x61, 0x74,
13283 0x69, 0x6f, 0x6e, 0x20, 0x70, 0x61, 0x63, 0x6b,
13284 0x65, 0x74, 0x20, 0x69, 0x6e, 0x64, 0x69, 0x63,
13285 0x61, 0x74, 0x69, 0x6e, 0x67, 0x20, 0x77, 0x68,
13286 0x61, 0x74, 0x20, 0x76, 0x65, 0x72, 0x73, 0x69,
13287 0x6f, 0x6e, 0x73, 0x20, 0x79, 0x6f, 0x75, 0x20,
13288 0x73, 0x75, 0x70, 0x70, 0x6f, 0x72, 0x74, 0x2e,
13289 0x20, 0x54, 0x68, 0x61, 0x6e, 0x6b, 0x20, 0x79,
13290 0x6f, 0x75, 0x20, 0x61, 0x6e, 0x64, 0x20, 0x68,
13291 0x61, 0x76, 0x65, 0x20, 0x61, 0x20, 0x6e, 0x69,
13292 0x63, 0x65, 0x20, 0x64, 0x61, 0x79, 0x2e, 0x00,
13293 };
13294 // clang-format on
13295 char expected_destination_connection_id_bytes[] = {0x56, 0x4e, 0x20, 0x70,
13296 0x6c, 0x7a, 0x20, 0x21};
13297 QuicConnectionId expected_destination_connection_id(
13298 reinterpret_cast<const char*>(expected_destination_connection_id_bytes),
13299 sizeof(expected_destination_connection_id_bytes));
13300
13301 QuicEncryptedPacket encrypted(reinterpret_cast<const char*>(packet),
13302 sizeof(packet));
13303 PacketHeaderFormat format = GOOGLE_QUIC_PACKET;
13304 bool version_present = false, has_length_prefix = true;
13305 QuicVersionLabel version_label = 33;
13306 ParsedQuicVersion parsed_version = UnsupportedQuicVersion();
13307 QuicConnectionId destination_connection_id = TestConnectionId(1);
13308 QuicConnectionId source_connection_id = TestConnectionId(2);
13309 bool retry_token_present = true;
13310 QuicStringPiece retry_token;
13311 std::string detailed_error = "foobar";
13312 QuicErrorCode header_parse_result = QuicFramer::ParsePublicHeaderDispatcher(
13313 encrypted, kQuicDefaultConnectionIdLength, &format, &version_present,
13314 &has_length_prefix, &version_label, &parsed_version,
13315 &destination_connection_id, &source_connection_id, &retry_token_present,
13316 &retry_token, &detailed_error);
13317 EXPECT_EQ(QUIC_NO_ERROR, header_parse_result);
13318 EXPECT_EQ(IETF_QUIC_LONG_HEADER_PACKET, format);
13319 EXPECT_TRUE(version_present);
13320 EXPECT_FALSE(has_length_prefix);
13321 EXPECT_EQ(0xcabadaba, version_label);
13322 EXPECT_EQ(expected_destination_connection_id, destination_connection_id);
13323 EXPECT_EQ(EmptyQuicConnectionId(), source_connection_id);
13324 EXPECT_FALSE(retry_token_present);
13325 EXPECT_EQ("", detailed_error);
13326}
13327
13328TEST_P(QuicFramerTest, DispatcherParseClientVersionNegotiationProbePacket) {
13329 // clang-format off
13330 static const char packet[1200] = {
13331 // IETF long header with fixed bit set, type initial, all-0 encrypted bits.
13332 0xc0,
13333 // Version, part of the IETF space reserved for negotiation.
13334 0xca, 0xba, 0xda, 0xba,
13335 // Destination connection ID length 8.
13336 0x08,
13337 // 8-byte destination connection ID.
13338 0x56, 0x4e, 0x20, 0x70, 0x6c, 0x7a, 0x20, 0x21,
13339 // Source connection ID length 0.
13340 0x00,
13341 // 8 bytes of zeroes followed by 8 bytes of ones to ensure that this does
13342 // not parse with any known version.
13343 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
13344 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
13345 // 1 byte of zeroes to pad to 16 byte boundary.
13346 0x00,
13347 // A polite greeting in case a human sees this in tcpdump.
13348 0x54, 0x68, 0x69, 0x73, 0x20, 0x70, 0x61, 0x63,
13349 0x6b, 0x65, 0x74, 0x20, 0x6f, 0x6e, 0x6c, 0x79,
13350 0x20, 0x65, 0x78, 0x69, 0x73, 0x74, 0x73, 0x20,
13351 0x74, 0x6f, 0x20, 0x74, 0x72, 0x69, 0x67, 0x67,
13352 0x65, 0x72, 0x20, 0x49, 0x45, 0x54, 0x46, 0x20,
13353 0x51, 0x55, 0x49, 0x43, 0x20, 0x76, 0x65, 0x72,
13354 0x73, 0x69, 0x6f, 0x6e, 0x20, 0x6e, 0x65, 0x67,
13355 0x6f, 0x74, 0x69, 0x61, 0x74, 0x69, 0x6f, 0x6e,
13356 0x2e, 0x20, 0x50, 0x6c, 0x65, 0x61, 0x73, 0x65,
13357 0x20, 0x72, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x64,
13358 0x20, 0x77, 0x69, 0x74, 0x68, 0x20, 0x61, 0x20,
13359 0x56, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x20,
13360 0x4e, 0x65, 0x67, 0x6f, 0x74, 0x69, 0x61, 0x74,
13361 0x69, 0x6f, 0x6e, 0x20, 0x70, 0x61, 0x63, 0x6b,
13362 0x65, 0x74, 0x20, 0x69, 0x6e, 0x64, 0x69, 0x63,
13363 0x61, 0x74, 0x69, 0x6e, 0x67, 0x20, 0x77, 0x68,
13364 0x61, 0x74, 0x20, 0x76, 0x65, 0x72, 0x73, 0x69,
13365 0x6f, 0x6e, 0x73, 0x20, 0x79, 0x6f, 0x75, 0x20,
13366 0x73, 0x75, 0x70, 0x70, 0x6f, 0x72, 0x74, 0x2e,
13367 0x20, 0x54, 0x68, 0x61, 0x6e, 0x6b, 0x20, 0x79,
13368 0x6f, 0x75, 0x20, 0x61, 0x6e, 0x64, 0x20, 0x68,
13369 0x61, 0x76, 0x65, 0x20, 0x61, 0x20, 0x6e, 0x69,
13370 0x63, 0x65, 0x20, 0x64, 0x61, 0x79, 0x2e, 0x00,
13371 };
13372 // clang-format on
13373 char expected_destination_connection_id_bytes[] = {0x56, 0x4e, 0x20, 0x70,
13374 0x6c, 0x7a, 0x20, 0x21};
13375 QuicConnectionId expected_destination_connection_id(
13376 reinterpret_cast<const char*>(expected_destination_connection_id_bytes),
13377 sizeof(expected_destination_connection_id_bytes));
13378
13379 QuicEncryptedPacket encrypted(reinterpret_cast<const char*>(packet),
13380 sizeof(packet));
13381 PacketHeaderFormat format = GOOGLE_QUIC_PACKET;
13382 bool version_present = false, has_length_prefix = false;
13383 QuicVersionLabel version_label = 33;
13384 ParsedQuicVersion parsed_version = UnsupportedQuicVersion();
13385 QuicConnectionId destination_connection_id = TestConnectionId(1);
13386 QuicConnectionId source_connection_id = TestConnectionId(2);
13387 bool retry_token_present = true;
13388 QuicStringPiece retry_token;
13389 std::string detailed_error = "foobar";
13390 QuicErrorCode header_parse_result = QuicFramer::ParsePublicHeaderDispatcher(
13391 encrypted, kQuicDefaultConnectionIdLength, &format, &version_present,
13392 &has_length_prefix, &version_label, &parsed_version,
13393 &destination_connection_id, &source_connection_id, &retry_token_present,
13394 &retry_token, &detailed_error);
13395 EXPECT_EQ(QUIC_NO_ERROR, header_parse_result);
13396 EXPECT_EQ(IETF_QUIC_LONG_HEADER_PACKET, format);
13397 EXPECT_TRUE(version_present);
13398 EXPECT_TRUE(has_length_prefix);
13399 EXPECT_EQ(0xcabadaba, version_label);
13400 EXPECT_EQ(expected_destination_connection_id, destination_connection_id);
13401 EXPECT_EQ(EmptyQuicConnectionId(), source_connection_id);
13402 EXPECT_EQ("", detailed_error);
13403}
13404
13405TEST_P(QuicFramerTest, ParseServerVersionNegotiationProbeResponseOld) {
13406 SetQuicFlag(FLAGS_quic_prober_uses_length_prefixed_connection_ids, false);
dschinazide0f6dc2019-05-15 16:10:11 -070013407 // clang-format off
13408 const char packet[] = {
13409 // IETF long header with fixed bit set, type initial, all-0 encrypted bits.
13410 0xc0,
13411 // Version of 0, indicating version negotiation.
13412 0x00, 0x00, 0x00, 0x00,
13413 // Destination connection ID length 0, source connection ID length 8.
13414 0x05,
13415 // 8-byte source connection ID.
13416 0x56, 0x4e, 0x20, 0x70, 0x6c, 0x7a, 0x20, 0x21,
13417 // A few supported versions.
13418 0xaa, 0xaa, 0xaa, 0xaa,
13419 QUIC_VERSION_BYTES,
13420 };
13421 // clang-format on
13422 char probe_payload_bytes[] = {0x56, 0x4e, 0x20, 0x70, 0x6c, 0x7a, 0x20, 0x21};
dschinazib012d212019-08-01 18:07:26 -070013423 char parsed_probe_payload_bytes[255] = {};
dschinazide0f6dc2019-05-15 16:10:11 -070013424 uint8_t parsed_probe_payload_length = 0;
13425 std::string parse_detailed_error = "";
13426 EXPECT_TRUE(QuicFramer::ParseServerVersionNegotiationProbeResponse(
13427 reinterpret_cast<const char*>(packet), sizeof(packet),
13428 reinterpret_cast<char*>(parsed_probe_payload_bytes),
13429 &parsed_probe_payload_length, &parse_detailed_error));
13430 EXPECT_EQ("", parse_detailed_error);
13431 test::CompareCharArraysWithHexError(
dschinazi48ac9192019-07-31 00:07:26 -070013432 "parsed probe", parsed_probe_payload_bytes, parsed_probe_payload_length,
13433 probe_payload_bytes, sizeof(probe_payload_bytes));
13434}
13435
13436TEST_P(QuicFramerTest, ParseServerVersionNegotiationProbeResponse) {
13437 SetQuicFlag(FLAGS_quic_prober_uses_length_prefixed_connection_ids, true);
13438 // clang-format off
13439 const char packet[] = {
13440 // IETF long header with fixed bit set, type initial, all-0 encrypted bits.
13441 0xc0,
13442 // Version of 0, indicating version negotiation.
13443 0x00, 0x00, 0x00, 0x00,
13444 // Destination connection ID length 0, source connection ID length 8.
13445 0x00, 0x08,
13446 // 8-byte source connection ID.
13447 0x56, 0x4e, 0x20, 0x70, 0x6c, 0x7a, 0x20, 0x21,
13448 // A few supported versions.
13449 0xaa, 0xaa, 0xaa, 0xaa,
13450 QUIC_VERSION_BYTES,
13451 };
13452 // clang-format on
13453 char probe_payload_bytes[] = {0x56, 0x4e, 0x20, 0x70, 0x6c, 0x7a, 0x20, 0x21};
dschinazib012d212019-08-01 18:07:26 -070013454 char parsed_probe_payload_bytes[255] = {};
dschinazi48ac9192019-07-31 00:07:26 -070013455 uint8_t parsed_probe_payload_length = 0;
13456 std::string parse_detailed_error = "";
13457 EXPECT_TRUE(QuicFramer::ParseServerVersionNegotiationProbeResponse(
13458 reinterpret_cast<const char*>(packet), sizeof(packet),
13459 reinterpret_cast<char*>(parsed_probe_payload_bytes),
13460 &parsed_probe_payload_length, &parse_detailed_error));
13461 EXPECT_EQ("", parse_detailed_error);
13462 test::CompareCharArraysWithHexError(
13463 "parsed probe", parsed_probe_payload_bytes, parsed_probe_payload_length,
13464 probe_payload_bytes, sizeof(probe_payload_bytes));
dschinazide0f6dc2019-05-15 16:10:11 -070013465}
13466
dschinaziccbe0e02019-08-13 12:15:00 -070013467// Test the client-side workaround for b/139330014 where an old client expects
13468// no length prefix but receives a length-prefixed response.
13469TEST_P(QuicFramerTest, ParseBadServerVersionNegotiationProbeResponse) {
13470 SetQuicFlag(FLAGS_quic_prober_uses_length_prefixed_connection_ids, false);
13471 // clang-format off
13472 const char packet[] = {
13473 // IETF long header with fixed bit set, type initial, all-0 encrypted bits.
13474 0xc0,
13475 // Version of 0, indicating version negotiation.
13476 0x00, 0x00, 0x00, 0x00,
13477 // Destination connection ID length 0, source connection ID length 8.
13478 0x00, 0x08,
13479 // 8-byte source connection ID.
13480 0x56, 0x4e, 0x20, 0x70, 0x6c, 0x7a, 0x20, 0x21,
13481 // A few supported versions.
13482 0xaa, 0xaa, 0xaa, 0xaa,
13483 QUIC_VERSION_BYTES,
13484 };
13485 // clang-format on
13486 char probe_payload_bytes[] = {0x56, 0x4e, 0x20, 0x70, 0x6c, 0x7a, 0x20, 0x21};
13487 char parsed_probe_payload_bytes[255] = {};
13488 uint8_t parsed_probe_payload_length = 0;
13489 std::string parse_detailed_error = "";
13490 EXPECT_TRUE(QuicFramer::ParseServerVersionNegotiationProbeResponse(
13491 reinterpret_cast<const char*>(packet), sizeof(packet),
13492 reinterpret_cast<char*>(parsed_probe_payload_bytes),
13493 &parsed_probe_payload_length, &parse_detailed_error));
13494 EXPECT_EQ("", parse_detailed_error);
13495 test::CompareCharArraysWithHexError(
13496 "parsed probe", parsed_probe_payload_bytes, parsed_probe_payload_length,
13497 probe_payload_bytes, sizeof(probe_payload_bytes));
13498}
13499
dschinazi346b7ce2019-06-05 01:38:18 -070013500TEST_P(QuicFramerTest, ClientConnectionIdFromLongHeaderToClient) {
dschinazi7d066ca2019-05-15 17:59:49 -070013501 if (framer_.transport_version() <= QUIC_VERSION_43) {
13502 // This test requires an IETF long header.
13503 return;
13504 }
dschinazi346b7ce2019-06-05 01:38:18 -070013505 SetDecrypterLevel(ENCRYPTION_HANDSHAKE);
dschinazi7d066ca2019-05-15 17:59:49 -070013506 QuicFramerPeer::SetPerspective(&framer_, Perspective::IS_CLIENT);
dschinazi7d066ca2019-05-15 17:59:49 -070013507 // clang-format off
13508 unsigned char packet[] = {
dschinazi346b7ce2019-06-05 01:38:18 -070013509 // public flags (long header with packet type HANDSHAKE and
dschinazi7d066ca2019-05-15 17:59:49 -070013510 // 4-byte packet number)
fayang36825da2019-08-21 14:01:27 -070013511 0xE3,
dschinazi7d066ca2019-05-15 17:59:49 -070013512 // version
13513 QUIC_VERSION_BYTES,
dschinazi346b7ce2019-06-05 01:38:18 -070013514 // connection ID lengths
dschinazi7d066ca2019-05-15 17:59:49 -070013515 0x50,
13516 // destination connection ID
13517 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
13518 // long header packet length
13519 0x05,
13520 // packet number
13521 0x12, 0x34, 0x56, 0x00,
13522 // padding frame
13523 0x00,
13524 };
dschinazi48ac9192019-07-31 00:07:26 -070013525 unsigned char packet99[] = {
13526 // public flags (long header with packet type HANDSHAKE and
13527 // 4-byte packet number)
13528 0xE3,
13529 // version
13530 QUIC_VERSION_BYTES,
13531 // destination connection ID length
13532 0x08,
13533 // destination connection ID
13534 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
13535 // source connection ID length
13536 0x00,
13537 // long header packet length
13538 0x05,
13539 // packet number
13540 0x12, 0x34, 0x56, 0x00,
13541 // padding frame
13542 0x00,
13543 };
dschinazi7d066ca2019-05-15 17:59:49 -070013544 // clang-format on
dschinazi48ac9192019-07-31 00:07:26 -070013545 unsigned char* p = packet;
13546 size_t p_length = QUIC_ARRAYSIZE(packet);
13547 if (framer_.transport_version() == QUIC_VERSION_99) {
13548 p = packet99;
13549 p_length = QUIC_ARRAYSIZE(packet99);
13550 }
13551 const bool parse_success =
13552 framer_.ProcessPacket(QuicEncryptedPacket(AsChars(p), p_length, false));
dschinazi7d066ca2019-05-15 17:59:49 -070013553 if (!QuicUtils::VariableLengthConnectionIdAllowedForVersion(
13554 framer_.transport_version())) {
dschinazi346b7ce2019-06-05 01:38:18 -070013555 EXPECT_FALSE(parse_success);
13556 EXPECT_EQ(QUIC_INVALID_PACKET_HEADER, framer_.error());
dschinazi7d066ca2019-05-15 17:59:49 -070013557 EXPECT_EQ("Invalid ConnectionId length.", framer_.detailed_error());
dschinazi346b7ce2019-06-05 01:38:18 -070013558 return;
13559 }
dschinazi346b7ce2019-06-05 01:38:18 -070013560 EXPECT_TRUE(parse_success);
13561 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
13562 EXPECT_EQ("", framer_.detailed_error());
13563 ASSERT_TRUE(visitor_.header_.get());
13564 EXPECT_EQ(FramerTestConnectionId(),
13565 visitor_.header_.get()->destination_connection_id);
13566}
13567
13568TEST_P(QuicFramerTest, ClientConnectionIdFromLongHeaderToServer) {
13569 if (framer_.transport_version() <= QUIC_VERSION_43) {
13570 // This test requires an IETF long header.
13571 return;
13572 }
13573 SetDecrypterLevel(ENCRYPTION_HANDSHAKE);
13574 QuicFramerPeer::SetPerspective(&framer_, Perspective::IS_SERVER);
13575 // clang-format off
13576 unsigned char packet[] = {
13577 // public flags (long header with packet type HANDSHAKE and
13578 // 4-byte packet number)
fayang36825da2019-08-21 14:01:27 -070013579 0xE3,
dschinazi346b7ce2019-06-05 01:38:18 -070013580 // version
13581 QUIC_VERSION_BYTES,
13582 // connection ID lengths
13583 0x05,
13584 // source connection ID
13585 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
13586 // long header packet length
13587 0x05,
13588 // packet number
13589 0x12, 0x34, 0x56, 0x00,
13590 // padding frame
13591 0x00,
13592 };
dschinazi48ac9192019-07-31 00:07:26 -070013593 unsigned char packet99[] = {
13594 // public flags (long header with packet type HANDSHAKE and
13595 // 4-byte packet number)
13596 0xE3,
13597 // version
13598 QUIC_VERSION_BYTES,
13599 // connection ID lengths
13600 0x00, 0x08,
13601 // source connection ID
13602 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
13603 // long header packet length
13604 0x05,
13605 // packet number
13606 0x12, 0x34, 0x56, 0x00,
13607 // padding frame
13608 0x00,
13609 };
dschinazi346b7ce2019-06-05 01:38:18 -070013610 // clang-format on
dschinazi48ac9192019-07-31 00:07:26 -070013611 unsigned char* p = packet;
13612 size_t p_length = QUIC_ARRAYSIZE(packet);
13613 if (framer_.transport_version() == QUIC_VERSION_99) {
13614 p = packet99;
13615 p_length = QUIC_ARRAYSIZE(packet99);
13616 }
13617 const bool parse_success =
13618 framer_.ProcessPacket(QuicEncryptedPacket(AsChars(p), p_length, false));
dschinazi346b7ce2019-06-05 01:38:18 -070013619 if (!QuicUtils::VariableLengthConnectionIdAllowedForVersion(
13620 framer_.transport_version())) {
13621 EXPECT_FALSE(parse_success);
13622 EXPECT_EQ(QUIC_INVALID_PACKET_HEADER, framer_.error());
13623 EXPECT_EQ("Invalid ConnectionId length.", framer_.detailed_error());
13624 return;
13625 }
dschinazi5e1a7b22019-07-31 12:23:21 -070013626 if (!framer_.version().SupportsClientConnectionIds()) {
dschinazi346b7ce2019-06-05 01:38:18 -070013627 EXPECT_FALSE(parse_success);
13628 EXPECT_EQ(QUIC_INVALID_PACKET_HEADER, framer_.error());
13629 EXPECT_EQ("Client connection ID not supported in this version.",
13630 framer_.detailed_error());
13631 return;
13632 }
13633 EXPECT_TRUE(parse_success);
13634 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
13635 EXPECT_EQ("", framer_.detailed_error());
13636 ASSERT_TRUE(visitor_.header_.get());
13637 EXPECT_EQ(FramerTestConnectionId(),
13638 visitor_.header_.get()->source_connection_id);
dschinazi7d066ca2019-05-15 17:59:49 -070013639}
13640
dschinazi334f0232019-05-29 16:08:53 -070013641TEST_P(QuicFramerTest, ProcessAndValidateIetfConnectionIdLengthClient) {
13642 if (framer_.transport_version() <= QUIC_VERSION_43) {
13643 // This test requires an IETF long header.
13644 return;
13645 }
13646 char connection_id_lengths = 0x05;
13647 QuicDataReader reader(&connection_id_lengths, 1);
13648
13649 bool should_update_expected_server_connection_id_length = false;
13650 uint8_t expected_server_connection_id_length = 8;
13651 uint8_t destination_connection_id_length = 0;
13652 uint8_t source_connection_id_length = 8;
13653 std::string detailed_error = "";
13654
13655 EXPECT_TRUE(QuicFramerPeer::ProcessAndValidateIetfConnectionIdLength(
13656 &reader, framer_.version(), Perspective::IS_CLIENT,
13657 should_update_expected_server_connection_id_length,
13658 &expected_server_connection_id_length, &destination_connection_id_length,
13659 &source_connection_id_length, &detailed_error));
13660 EXPECT_EQ(8, expected_server_connection_id_length);
13661 EXPECT_EQ(0, destination_connection_id_length);
13662 EXPECT_EQ(8, source_connection_id_length);
13663 EXPECT_EQ("", detailed_error);
13664
13665 QuicDataReader reader2(&connection_id_lengths, 1);
13666 should_update_expected_server_connection_id_length = true;
13667 expected_server_connection_id_length = 33;
13668 EXPECT_TRUE(QuicFramerPeer::ProcessAndValidateIetfConnectionIdLength(
13669 &reader2, framer_.version(), Perspective::IS_CLIENT,
13670 should_update_expected_server_connection_id_length,
13671 &expected_server_connection_id_length, &destination_connection_id_length,
13672 &source_connection_id_length, &detailed_error));
13673 EXPECT_EQ(8, expected_server_connection_id_length);
13674 EXPECT_EQ(0, destination_connection_id_length);
13675 EXPECT_EQ(8, source_connection_id_length);
13676 EXPECT_EQ("", detailed_error);
13677}
13678
13679TEST_P(QuicFramerTest, ProcessAndValidateIetfConnectionIdLengthServer) {
13680 if (framer_.transport_version() <= QUIC_VERSION_43) {
13681 // This test requires an IETF long header.
13682 return;
13683 }
13684 char connection_id_lengths = 0x50;
13685 QuicDataReader reader(&connection_id_lengths, 1);
13686
13687 bool should_update_expected_server_connection_id_length = false;
13688 uint8_t expected_server_connection_id_length = 8;
13689 uint8_t destination_connection_id_length = 8;
13690 uint8_t source_connection_id_length = 0;
13691 std::string detailed_error = "";
13692
13693 EXPECT_TRUE(QuicFramerPeer::ProcessAndValidateIetfConnectionIdLength(
13694 &reader, framer_.version(), Perspective::IS_SERVER,
13695 should_update_expected_server_connection_id_length,
13696 &expected_server_connection_id_length, &destination_connection_id_length,
13697 &source_connection_id_length, &detailed_error));
13698 EXPECT_EQ(8, expected_server_connection_id_length);
13699 EXPECT_EQ(8, destination_connection_id_length);
13700 EXPECT_EQ(0, source_connection_id_length);
13701 EXPECT_EQ("", detailed_error);
13702
13703 QuicDataReader reader2(&connection_id_lengths, 1);
13704 should_update_expected_server_connection_id_length = true;
13705 expected_server_connection_id_length = 33;
13706 EXPECT_TRUE(QuicFramerPeer::ProcessAndValidateIetfConnectionIdLength(
13707 &reader2, framer_.version(), Perspective::IS_SERVER,
13708 should_update_expected_server_connection_id_length,
13709 &expected_server_connection_id_length, &destination_connection_id_length,
13710 &source_connection_id_length, &detailed_error));
13711 EXPECT_EQ(8, expected_server_connection_id_length);
13712 EXPECT_EQ(8, destination_connection_id_length);
13713 EXPECT_EQ(0, source_connection_id_length);
13714 EXPECT_EQ("", detailed_error);
13715}
13716
fkastenholzb4dade72019-08-05 06:54:20 -070013717TEST_P(QuicFramerTest, TestExtendedErrorCodeParser) {
13718 if (VersionHasIetfQuicFrames(framer_.transport_version())) {
13719 return;
13720 }
fkastenholz488a4622019-08-26 06:24:46 -070013721 QuicConnectionCloseFrame frame;
13722
13723 frame.error_details = "this has no error code info in it";
13724 MaybeExtractQuicErrorCode(&frame);
13725 EXPECT_EQ(QUIC_IETF_GQUIC_ERROR_MISSING, frame.extracted_error_code);
13726 EXPECT_EQ("this has no error code info in it", frame.error_details);
13727
13728 frame.error_details = "1234this does not have the colon in it";
13729 MaybeExtractQuicErrorCode(&frame);
13730 EXPECT_EQ(QUIC_IETF_GQUIC_ERROR_MISSING, frame.extracted_error_code);
13731 EXPECT_EQ("1234this does not have the colon in it", frame.error_details);
13732
13733 frame.error_details = "1a234:this has a colon, but a malformed error number";
13734 MaybeExtractQuicErrorCode(&frame);
13735 EXPECT_EQ(QUIC_IETF_GQUIC_ERROR_MISSING, frame.extracted_error_code);
13736 EXPECT_EQ("1a234:this has a colon, but a malformed error number",
13737 frame.error_details);
13738
13739 frame.error_details = "1234:this is good";
13740 MaybeExtractQuicErrorCode(&frame);
13741 EXPECT_EQ(1234u, frame.extracted_error_code);
13742 EXPECT_EQ("this is good", frame.error_details);
13743
13744 frame.error_details =
13745 "1234 :this is not good, space between last digit and colon";
13746 MaybeExtractQuicErrorCode(&frame);
13747 EXPECT_EQ(QUIC_IETF_GQUIC_ERROR_MISSING, frame.extracted_error_code);
13748 EXPECT_EQ("1234 :this is not good, space between last digit and colon",
13749 frame.error_details);
13750
13751 frame.error_details = "123456789";
13752 MaybeExtractQuicErrorCode(&frame);
fkastenholzb4dade72019-08-05 06:54:20 -070013753 EXPECT_EQ(QUIC_IETF_GQUIC_ERROR_MISSING,
fkastenholz488a4622019-08-26 06:24:46 -070013754 frame.extracted_error_code); // Not good, all numbers, no :
13755 EXPECT_EQ("123456789", frame.error_details);
13756
13757 frame.error_details = "1234:";
13758 MaybeExtractQuicErrorCode(&frame);
fkastenholzb4dade72019-08-05 06:54:20 -070013759 EXPECT_EQ(1234u,
fkastenholz488a4622019-08-26 06:24:46 -070013760 frame.extracted_error_code); // corner case.
13761 EXPECT_EQ("", frame.error_details);
13762
13763 frame.error_details = "1234:5678";
13764 MaybeExtractQuicErrorCode(&frame);
13765 EXPECT_EQ(1234u,
13766 frame.extracted_error_code); // another corner case.
13767 EXPECT_EQ("5678", frame.error_details);
13768
13769 frame.error_details = "12345 6789:";
13770 MaybeExtractQuicErrorCode(&frame);
fkastenholzb4dade72019-08-05 06:54:20 -070013771 EXPECT_EQ(QUIC_IETF_GQUIC_ERROR_MISSING,
fkastenholz488a4622019-08-26 06:24:46 -070013772 frame.extracted_error_code); // Not good
13773 EXPECT_EQ("12345 6789:", frame.error_details);
13774
13775 frame.error_details = ":no numbers, is not good";
13776 MaybeExtractQuicErrorCode(&frame);
13777 EXPECT_EQ(QUIC_IETF_GQUIC_ERROR_MISSING, frame.extracted_error_code);
13778 EXPECT_EQ(":no numbers, is not good", frame.error_details);
13779
13780 frame.error_details = "qwer:also no numbers, is not good";
13781 MaybeExtractQuicErrorCode(&frame);
13782 EXPECT_EQ(QUIC_IETF_GQUIC_ERROR_MISSING, frame.extracted_error_code);
13783 EXPECT_EQ("qwer:also no numbers, is not good", frame.error_details);
13784
13785 frame.error_details = " 1234:this is not good, space before first digit";
13786 MaybeExtractQuicErrorCode(&frame);
13787 EXPECT_EQ(QUIC_IETF_GQUIC_ERROR_MISSING, frame.extracted_error_code);
13788 EXPECT_EQ(" 1234:this is not good, space before first digit",
13789 frame.error_details);
13790
13791 frame.error_details = "1234:";
13792 MaybeExtractQuicErrorCode(&frame);
13793 EXPECT_EQ(1234u,
13794 frame.extracted_error_code); // this is good
13795 EXPECT_EQ("", frame.error_details);
fkastenholzb4dade72019-08-05 06:54:20 -070013796}
13797
QUICHE teama6ef0a62019-03-07 20:34:33 -050013798} // namespace
13799} // namespace test
13800} // namespace quic