blob: 88845af456bee0601e7ba946030823fa5127d8e6 [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>
bnc463f2352019-10-10 04:49:34 -070012#include <utility>
QUICHE teama6ef0a62019-03-07 20:34:33 -050013#include <vector>
14
15#include "net/third_party/quiche/src/quic/core/crypto/null_decrypter.h"
16#include "net/third_party/quiche/src/quic/core/crypto/null_encrypter.h"
17#include "net/third_party/quiche/src/quic/core/crypto/quic_decrypter.h"
18#include "net/third_party/quiche/src/quic/core/crypto/quic_encrypter.h"
19#include "net/third_party/quiche/src/quic/core/quic_connection_id.h"
dschinazicf5b1e22019-07-17 18:35:17 -070020#include "net/third_party/quiche/src/quic/core/quic_error_codes.h"
QUICHE teama6ef0a62019-03-07 20:34:33 -050021#include "net/third_party/quiche/src/quic/core/quic_packets.h"
22#include "net/third_party/quiche/src/quic/core/quic_types.h"
23#include "net/third_party/quiche/src/quic/core/quic_utils.h"
dschinazi243eabc2019-08-05 16:15:29 -070024#include "net/third_party/quiche/src/quic/core/quic_versions.h"
QUICHE teama6ef0a62019-03-07 20:34:33 -050025#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"
QUICHE teama6ef0a62019-03-07 20:34:33 -050030#include "net/third_party/quiche/src/quic/test_tools/quic_framer_peer.h"
31#include "net/third_party/quiche/src/quic/test_tools/quic_test_utils.h"
32#include "net/third_party/quiche/src/quic/test_tools/simple_data_producer.h"
bnc4e9283d2019-12-17 07:08:57 -080033#include "net/third_party/quiche/src/common/platform/api/quiche_arraysize.h"
dmcardlecf0bfcf2019-12-13 08:08:21 -080034#include "net/third_party/quiche/src/common/platform/api/quiche_string_piece.h"
35#include "net/third_party/quiche/src/common/platform/api/quiche_text_utils.h"
dmcardle8f7df532020-01-07 13:28:57 -080036#include "net/third_party/quiche/src/common/test_tools/quiche_test_utils.h"
QUICHE teama6ef0a62019-03-07 20:34:33 -050037
38using testing::_;
39using testing::Return;
40using testing::Truly;
41
42namespace quic {
43namespace test {
44namespace {
45
46const uint64_t kEpoch = UINT64_C(1) << 32;
47const uint64_t kMask = kEpoch - 1;
48
49const QuicUint128 kTestStatelessResetToken = 1010101; // 0x0F69B5
50
51// Use fields in which each byte is distinct to ensure that every byte is
52// framed correctly. The values are otherwise arbitrary.
53QuicConnectionId FramerTestConnectionId() {
54 return TestConnectionId(UINT64_C(0xFEDCBA9876543210));
55}
56
57QuicConnectionId FramerTestConnectionIdPlusOne() {
58 return TestConnectionId(UINT64_C(0xFEDCBA9876543211));
59}
60
QUICHE team8e2e4532019-03-14 14:37:56 -070061QuicConnectionId FramerTestConnectionIdNineBytes() {
62 char connection_id_bytes[9] = {0xFE, 0xDC, 0xBA, 0x98, 0x76,
63 0x54, 0x32, 0x10, 0x42};
64 return QuicConnectionId(connection_id_bytes, sizeof(connection_id_bytes));
65}
66
QUICHE teama6ef0a62019-03-07 20:34:33 -050067const QuicPacketNumber kPacketNumber = QuicPacketNumber(UINT64_C(0x12345678));
68const QuicPacketNumber kSmallLargestObserved =
69 QuicPacketNumber(UINT16_C(0x1234));
70const QuicPacketNumber kSmallMissingPacket = QuicPacketNumber(UINT16_C(0x1233));
71const QuicPacketNumber kLeastUnacked = QuicPacketNumber(UINT64_C(0x012345670));
72const QuicStreamId kStreamId = UINT64_C(0x01020304);
73// Note that the high 4 bits of the stream offset must be less than 0x40
74// in order to ensure that the value can be encoded using VarInt62 encoding.
75const QuicStreamOffset kStreamOffset = UINT64_C(0x3A98FEDC32107654);
76const QuicPublicResetNonceProof kNonceProof = UINT64_C(0xABCDEF0123456789);
77
78// In testing that we can ack the full range of packets...
79// This is the largest packet number that can be represented in IETF QUIC
80// varint62 format.
81const QuicPacketNumber kLargestIetfLargestObserved =
82 QuicPacketNumber(UINT64_C(0x3fffffffffffffff));
83// Encodings for the two bits in a VarInt62 that
84// describe the length of the VarInt61. For binary packet
85// formats in this file, the convention is to code the
86// first byte as
87// kVarInt62FourBytes + 0x<value_in_that_byte>
88const uint8_t kVarInt62OneByte = 0x00;
89const uint8_t kVarInt62TwoBytes = 0x40;
90const uint8_t kVarInt62FourBytes = 0x80;
91const uint8_t kVarInt62EightBytes = 0xc0;
92
93class TestEncrypter : public QuicEncrypter {
94 public:
95 ~TestEncrypter() override {}
dmcardlecf0bfcf2019-12-13 08:08:21 -080096 bool SetKey(quiche::QuicheStringPiece /*key*/) override { return true; }
97 bool SetNoncePrefix(quiche::QuicheStringPiece /*nonce_prefix*/) override {
dschinazi17d42422019-06-18 16:35:07 -070098 return true;
99 }
dmcardlecf0bfcf2019-12-13 08:08:21 -0800100 bool SetIV(quiche::QuicheStringPiece /*iv*/) override { return true; }
101 bool SetHeaderProtectionKey(quiche::QuicheStringPiece /*key*/) override {
102 return true;
103 }
QUICHE teama6ef0a62019-03-07 20:34:33 -0500104 bool EncryptPacket(uint64_t packet_number,
dmcardlecf0bfcf2019-12-13 08:08:21 -0800105 quiche::QuicheStringPiece associated_data,
106 quiche::QuicheStringPiece plaintext,
QUICHE teama6ef0a62019-03-07 20:34:33 -0500107 char* output,
108 size_t* output_length,
dschinazi17d42422019-06-18 16:35:07 -0700109 size_t /*max_output_length*/) override {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500110 packet_number_ = QuicPacketNumber(packet_number);
vasilvvc48c8712019-03-11 13:38:16 -0700111 associated_data_ = std::string(associated_data);
112 plaintext_ = std::string(plaintext);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500113 memcpy(output, plaintext.data(), plaintext.length());
114 *output_length = plaintext.length();
115 return true;
116 }
dschinazi17d42422019-06-18 16:35:07 -0700117 std::string GenerateHeaderProtectionMask(
dmcardlecf0bfcf2019-12-13 08:08:21 -0800118 quiche::QuicheStringPiece /*sample*/) override {
QUICHE team2d187972019-03-19 16:23:47 -0700119 return std::string(5, 0);
120 }
QUICHE teama6ef0a62019-03-07 20:34:33 -0500121 size_t GetKeySize() const override { return 0; }
122 size_t GetNoncePrefixSize() const override { return 0; }
123 size_t GetIVSize() const override { return 0; }
124 size_t GetMaxPlaintextSize(size_t ciphertext_size) const override {
125 return ciphertext_size;
126 }
127 size_t GetCiphertextSize(size_t plaintext_size) const override {
128 return plaintext_size;
129 }
dmcardlecf0bfcf2019-12-13 08:08:21 -0800130 quiche::QuicheStringPiece GetKey() const override {
131 return quiche::QuicheStringPiece();
132 }
133 quiche::QuicheStringPiece GetNoncePrefix() const override {
134 return quiche::QuicheStringPiece();
135 }
QUICHE teama6ef0a62019-03-07 20:34:33 -0500136
137 QuicPacketNumber packet_number_;
vasilvvc48c8712019-03-11 13:38:16 -0700138 std::string associated_data_;
139 std::string plaintext_;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500140};
141
142class TestDecrypter : public QuicDecrypter {
143 public:
144 ~TestDecrypter() override {}
dmcardlecf0bfcf2019-12-13 08:08:21 -0800145 bool SetKey(quiche::QuicheStringPiece /*key*/) override { return true; }
146 bool SetNoncePrefix(quiche::QuicheStringPiece /*nonce_prefix*/) override {
dschinazi17d42422019-06-18 16:35:07 -0700147 return true;
148 }
dmcardlecf0bfcf2019-12-13 08:08:21 -0800149 bool SetIV(quiche::QuicheStringPiece /*iv*/) override { return true; }
150 bool SetHeaderProtectionKey(quiche::QuicheStringPiece /*key*/) override {
151 return true;
152 }
153 bool SetPreliminaryKey(quiche::QuicheStringPiece /*key*/) override {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500154 QUIC_BUG << "should not be called";
155 return false;
156 }
dschinazi17d42422019-06-18 16:35:07 -0700157 bool SetDiversificationNonce(const DiversificationNonce& /*key*/) override {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500158 return true;
159 }
160 bool DecryptPacket(uint64_t packet_number,
dmcardlecf0bfcf2019-12-13 08:08:21 -0800161 quiche::QuicheStringPiece associated_data,
162 quiche::QuicheStringPiece ciphertext,
QUICHE teama6ef0a62019-03-07 20:34:33 -0500163 char* output,
164 size_t* output_length,
dschinazi17d42422019-06-18 16:35:07 -0700165 size_t /*max_output_length*/) override {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500166 packet_number_ = QuicPacketNumber(packet_number);
vasilvvc48c8712019-03-11 13:38:16 -0700167 associated_data_ = std::string(associated_data);
168 ciphertext_ = std::string(ciphertext);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500169 memcpy(output, ciphertext.data(), ciphertext.length());
170 *output_length = ciphertext.length();
171 return true;
172 }
QUICHE team2d187972019-03-19 16:23:47 -0700173 std::string GenerateHeaderProtectionMask(
dschinazi17d42422019-06-18 16:35:07 -0700174 QuicDataReader* /*sample_reader*/) override {
QUICHE team2d187972019-03-19 16:23:47 -0700175 return std::string(5, 0);
176 }
QUICHE teama6ef0a62019-03-07 20:34:33 -0500177 size_t GetKeySize() const override { return 0; }
nharper965e5922019-09-23 22:33:54 -0700178 size_t GetNoncePrefixSize() const override { return 0; }
QUICHE teama6ef0a62019-03-07 20:34:33 -0500179 size_t GetIVSize() const override { return 0; }
dmcardlecf0bfcf2019-12-13 08:08:21 -0800180 quiche::QuicheStringPiece GetKey() const override {
181 return quiche::QuicheStringPiece();
182 }
183 quiche::QuicheStringPiece GetNoncePrefix() const override {
184 return quiche::QuicheStringPiece();
185 }
QUICHE teama6ef0a62019-03-07 20:34:33 -0500186 // Use a distinct value starting with 0xFFFFFF, which is never used by TLS.
187 uint32_t cipher_id() const override { return 0xFFFFFFF2; }
188 QuicPacketNumber packet_number_;
vasilvvc48c8712019-03-11 13:38:16 -0700189 std::string associated_data_;
190 std::string ciphertext_;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500191};
192
193class TestQuicVisitor : public QuicFramerVisitorInterface {
194 public:
195 TestQuicVisitor()
196 : error_count_(0),
197 version_mismatch_(0),
198 packet_count_(0),
199 frame_count_(0),
200 complete_packets_(0),
201 accept_packet_(true),
202 accept_public_header_(true) {}
203
204 ~TestQuicVisitor() override {}
205
206 void OnError(QuicFramer* f) override {
207 QUIC_DLOG(INFO) << "QuicFramer Error: " << QuicErrorCodeToString(f->error())
208 << " (" << f->error() << ")";
209 ++error_count_;
210 }
211
212 void OnPacket() override {}
213
214 void OnPublicResetPacket(const QuicPublicResetPacket& packet) override {
vasilvv0fc587f2019-09-06 13:33:08 -0700215 public_reset_packet_ = std::make_unique<QuicPublicResetPacket>((packet));
fkastenholza3660102019-08-28 05:19:24 -0700216 EXPECT_EQ(0u, framer_->current_received_frame_type());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500217 }
218
219 void OnVersionNegotiationPacket(
220 const QuicVersionNegotiationPacket& packet) override {
221 version_negotiation_packet_ =
vasilvv0fc587f2019-09-06 13:33:08 -0700222 std::make_unique<QuicVersionNegotiationPacket>((packet));
fkastenholza3660102019-08-28 05:19:24 -0700223 EXPECT_EQ(0u, framer_->current_received_frame_type());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500224 }
225
dschinazi244f6dc2019-05-06 15:45:16 -0700226 void OnRetryPacket(QuicConnectionId original_connection_id,
227 QuicConnectionId new_connection_id,
dschinazi278efae2020-01-28 17:03:09 -0800228 quiche::QuicheStringPiece retry_token,
229 quiche::QuicheStringPiece retry_integrity_tag,
230 quiche::QuicheStringPiece retry_without_tag) override {
dschinazi2dd75ee2020-01-29 09:21:37 -0800231 on_retry_packet_called_ = true;
dschinazi244f6dc2019-05-06 15:45:16 -0700232 retry_original_connection_id_ =
vasilvv0fc587f2019-09-06 13:33:08 -0700233 std::make_unique<QuicConnectionId>(original_connection_id);
dschinazi244f6dc2019-05-06 15:45:16 -0700234 retry_new_connection_id_ =
vasilvv0fc587f2019-09-06 13:33:08 -0700235 std::make_unique<QuicConnectionId>(new_connection_id);
236 retry_token_ = std::make_unique<std::string>(std::string(retry_token));
dschinazi278efae2020-01-28 17:03:09 -0800237 retry_token_integrity_tag_ =
238 std::make_unique<std::string>(std::string(retry_integrity_tag));
239 retry_without_tag_ =
240 std::make_unique<std::string>(std::string(retry_without_tag));
fkastenholza3660102019-08-28 05:19:24 -0700241 EXPECT_EQ(0u, framer_->current_received_frame_type());
dschinazi244f6dc2019-05-06 15:45:16 -0700242 }
243
fayang8aba1ff2019-06-21 12:00:54 -0700244 bool OnProtocolVersionMismatch(ParsedQuicVersion received_version) override {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500245 QUIC_DLOG(INFO) << "QuicFramer Version Mismatch, version: "
246 << received_version;
247 ++version_mismatch_;
fkastenholza3660102019-08-28 05:19:24 -0700248 EXPECT_EQ(0u, framer_->current_received_frame_type());
dschinazi48ac9192019-07-31 00:07:26 -0700249 return false;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500250 }
251
252 bool OnUnauthenticatedPublicHeader(const QuicPacketHeader& header) override {
vasilvv0fc587f2019-09-06 13:33:08 -0700253 header_ = std::make_unique<QuicPacketHeader>((header));
fkastenholza3660102019-08-28 05:19:24 -0700254 EXPECT_EQ(0u, framer_->current_received_frame_type());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500255 return accept_public_header_;
256 }
257
dschinazi17d42422019-06-18 16:35:07 -0700258 bool OnUnauthenticatedHeader(const QuicPacketHeader& /*header*/) override {
fkastenholza3660102019-08-28 05:19:24 -0700259 EXPECT_EQ(0u, framer_->current_received_frame_type());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500260 return true;
261 }
262
fkastenholza3660102019-08-28 05:19:24 -0700263 void OnDecryptedPacket(EncryptionLevel /*level*/) override {
264 EXPECT_EQ(0u, framer_->current_received_frame_type());
265 }
QUICHE teama6ef0a62019-03-07 20:34:33 -0500266
267 bool OnPacketHeader(const QuicPacketHeader& header) override {
268 ++packet_count_;
vasilvv0fc587f2019-09-06 13:33:08 -0700269 header_ = std::make_unique<QuicPacketHeader>((header));
fkastenholza3660102019-08-28 05:19:24 -0700270 EXPECT_EQ(0u, framer_->current_received_frame_type());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500271 return accept_packet_;
272 }
273
274 void OnCoalescedPacket(const QuicEncryptedPacket& packet) override {
dschinazi4b5a68a2019-08-15 15:45:36 -0700275 coalesced_packets_.push_back(packet.Clone());
276 }
277
278 void OnUndecryptablePacket(const QuicEncryptedPacket& packet,
279 EncryptionLevel decryption_level,
280 bool has_decryption_key) override {
281 undecryptable_packets_.push_back(packet.Clone());
282 undecryptable_decryption_levels_.push_back(decryption_level);
283 undecryptable_has_decryption_keys_.push_back(has_decryption_key);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500284 }
285
286 bool OnStreamFrame(const QuicStreamFrame& frame) override {
287 ++frame_count_;
288 // Save a copy of the data so it is valid after the packet is processed.
vasilvvc48c8712019-03-11 13:38:16 -0700289 std::string* string_data =
290 new std::string(frame.data_buffer, frame.data_length);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500291 stream_data_.push_back(QuicWrapUnique(string_data));
vasilvv0fc587f2019-09-06 13:33:08 -0700292 stream_frames_.push_back(std::make_unique<QuicStreamFrame>(
QUICHE teama6ef0a62019-03-07 20:34:33 -0500293 frame.stream_id, frame.fin, frame.offset, *string_data));
fkastenholza3660102019-08-28 05:19:24 -0700294 if (VersionHasIetfQuicFrames(transport_version_)) {
295 // Low order bits of type encode flags, ignore them for this test.
296 EXPECT_TRUE(IS_IETF_STREAM_FRAME(framer_->current_received_frame_type()));
297 } else {
298 EXPECT_EQ(0u, framer_->current_received_frame_type());
299 }
QUICHE teama6ef0a62019-03-07 20:34:33 -0500300 return true;
301 }
302
303 bool OnCryptoFrame(const QuicCryptoFrame& frame) override {
304 ++frame_count_;
305 // Save a copy of the data so it is valid after the packet is processed.
vasilvvc48c8712019-03-11 13:38:16 -0700306 std::string* string_data =
307 new std::string(frame.data_buffer, frame.data_length);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500308 crypto_data_.push_back(QuicWrapUnique(string_data));
vasilvv0fc587f2019-09-06 13:33:08 -0700309 crypto_frames_.push_back(std::make_unique<QuicCryptoFrame>(
renjietang15dfaa82020-01-03 16:13:38 -0800310 frame.level, frame.offset, *string_data));
fkastenholza3660102019-08-28 05:19:24 -0700311 if (VersionHasIetfQuicFrames(transport_version_)) {
312 EXPECT_EQ(IETF_CRYPTO, framer_->current_received_frame_type());
313 } else {
314 EXPECT_EQ(0u, framer_->current_received_frame_type());
315 }
QUICHE teama6ef0a62019-03-07 20:34:33 -0500316 return true;
317 }
318
319 bool OnAckFrameStart(QuicPacketNumber largest_acked,
320 QuicTime::Delta ack_delay_time) override {
321 ++frame_count_;
322 QuicAckFrame ack_frame;
323 ack_frame.largest_acked = largest_acked;
324 ack_frame.ack_delay_time = ack_delay_time;
vasilvv0fc587f2019-09-06 13:33:08 -0700325 ack_frames_.push_back(std::make_unique<QuicAckFrame>(ack_frame));
fkastenholza3660102019-08-28 05:19:24 -0700326 if (VersionHasIetfQuicFrames(transport_version_)) {
327 EXPECT_EQ(IETF_ACK, framer_->current_received_frame_type());
328 } else {
329 EXPECT_EQ(0u, framer_->current_received_frame_type());
330 }
QUICHE teama6ef0a62019-03-07 20:34:33 -0500331 return true;
332 }
333
334 bool OnAckRange(QuicPacketNumber start, QuicPacketNumber end) override {
335 DCHECK(!ack_frames_.empty());
336 ack_frames_[ack_frames_.size() - 1]->packets.AddRange(start, end);
fkastenholza3660102019-08-28 05:19:24 -0700337 if (VersionHasIetfQuicFrames(transport_version_)) {
338 EXPECT_EQ(IETF_ACK, framer_->current_received_frame_type());
339 } else {
340 EXPECT_EQ(0u, framer_->current_received_frame_type());
341 }
QUICHE teama6ef0a62019-03-07 20:34:33 -0500342 return true;
343 }
344
345 bool OnAckTimestamp(QuicPacketNumber packet_number,
346 QuicTime timestamp) override {
347 ack_frames_[ack_frames_.size() - 1]->received_packet_times.push_back(
348 std::make_pair(packet_number, timestamp));
fkastenholza3660102019-08-28 05:19:24 -0700349 EXPECT_EQ(0u, framer_->current_received_frame_type());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500350 return true;
351 }
352
353 bool OnAckFrameEnd(QuicPacketNumber /*start*/) override { return true; }
354
355 bool OnStopWaitingFrame(const QuicStopWaitingFrame& frame) override {
356 ++frame_count_;
vasilvv0fc587f2019-09-06 13:33:08 -0700357 stop_waiting_frames_.push_back(
358 std::make_unique<QuicStopWaitingFrame>(frame));
fkastenholza3660102019-08-28 05:19:24 -0700359 EXPECT_EQ(0u, framer_->current_received_frame_type());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500360 return true;
361 }
362
363 bool OnPaddingFrame(const QuicPaddingFrame& frame) override {
vasilvv0fc587f2019-09-06 13:33:08 -0700364 padding_frames_.push_back(std::make_unique<QuicPaddingFrame>(frame));
fkastenholza3660102019-08-28 05:19:24 -0700365 if (VersionHasIetfQuicFrames(transport_version_)) {
366 EXPECT_EQ(IETF_PADDING, framer_->current_received_frame_type());
367 } else {
368 EXPECT_EQ(0u, framer_->current_received_frame_type());
369 }
QUICHE teama6ef0a62019-03-07 20:34:33 -0500370 return true;
371 }
372
373 bool OnPingFrame(const QuicPingFrame& frame) override {
374 ++frame_count_;
vasilvv0fc587f2019-09-06 13:33:08 -0700375 ping_frames_.push_back(std::make_unique<QuicPingFrame>(frame));
fkastenholza3660102019-08-28 05:19:24 -0700376 if (VersionHasIetfQuicFrames(transport_version_)) {
377 EXPECT_EQ(IETF_PING, framer_->current_received_frame_type());
378 } else {
379 EXPECT_EQ(0u, framer_->current_received_frame_type());
380 }
QUICHE teama6ef0a62019-03-07 20:34:33 -0500381 return true;
382 }
383
384 bool OnMessageFrame(const QuicMessageFrame& frame) override {
385 ++frame_count_;
386 message_frames_.push_back(
vasilvv0fc587f2019-09-06 13:33:08 -0700387 std::make_unique<QuicMessageFrame>(frame.data, frame.message_length));
fkastenholza3660102019-08-28 05:19:24 -0700388 if (VersionHasIetfQuicFrames(transport_version_)) {
dschinazicd86dd12019-11-14 10:11:13 -0800389 EXPECT_TRUE(IETF_EXTENSION_MESSAGE_NO_LENGTH_V99 ==
fkastenholza3660102019-08-28 05:19:24 -0700390 framer_->current_received_frame_type() ||
dschinazicd86dd12019-11-14 10:11:13 -0800391 IETF_EXTENSION_MESSAGE_V99 ==
fkastenholza3660102019-08-28 05:19:24 -0700392 framer_->current_received_frame_type());
393 } else {
394 EXPECT_EQ(0u, framer_->current_received_frame_type());
395 }
QUICHE teama6ef0a62019-03-07 20:34:33 -0500396 return true;
397 }
398
fayang01062942020-01-22 07:23:23 -0800399 bool OnHandshakeDoneFrame(const QuicHandshakeDoneFrame& frame) override {
400 ++frame_count_;
401 handshake_done_frames_.push_back(
402 std::make_unique<QuicHandshakeDoneFrame>(frame));
403 DCHECK(VersionHasIetfQuicFrames(transport_version_));
404 EXPECT_EQ(IETF_HANDSHAKE_DONE, framer_->current_received_frame_type());
405 return true;
406 }
407
QUICHE teama6ef0a62019-03-07 20:34:33 -0500408 void OnPacketComplete() override { ++complete_packets_; }
409
410 bool OnRstStreamFrame(const QuicRstStreamFrame& frame) override {
411 rst_stream_frame_ = frame;
fkastenholza3660102019-08-28 05:19:24 -0700412 if (VersionHasIetfQuicFrames(transport_version_)) {
413 EXPECT_EQ(IETF_RST_STREAM, framer_->current_received_frame_type());
414 } else {
415 EXPECT_EQ(0u, framer_->current_received_frame_type());
416 }
QUICHE teama6ef0a62019-03-07 20:34:33 -0500417 return true;
418 }
419
420 bool OnConnectionCloseFrame(const QuicConnectionCloseFrame& frame) override {
421 connection_close_frame_ = frame;
fkastenholza3660102019-08-28 05:19:24 -0700422 if (VersionHasIetfQuicFrames(transport_version_)) {
423 EXPECT_NE(GOOGLE_QUIC_CONNECTION_CLOSE, frame.close_type);
424 if (frame.close_type == IETF_QUIC_TRANSPORT_CONNECTION_CLOSE) {
425 EXPECT_EQ(IETF_CONNECTION_CLOSE,
426 framer_->current_received_frame_type());
427 } else {
428 EXPECT_EQ(IETF_APPLICATION_CLOSE,
429 framer_->current_received_frame_type());
430 }
431 } else {
432 EXPECT_EQ(0u, framer_->current_received_frame_type());
433 }
QUICHE teama6ef0a62019-03-07 20:34:33 -0500434 return true;
435 }
436
QUICHE teama6ef0a62019-03-07 20:34:33 -0500437 bool OnStopSendingFrame(const QuicStopSendingFrame& frame) override {
438 stop_sending_frame_ = frame;
fkastenholza3660102019-08-28 05:19:24 -0700439 EXPECT_EQ(IETF_STOP_SENDING, framer_->current_received_frame_type());
440 EXPECT_TRUE(VersionHasIetfQuicFrames(transport_version_));
QUICHE teama6ef0a62019-03-07 20:34:33 -0500441 return true;
442 }
443
444 bool OnPathChallengeFrame(const QuicPathChallengeFrame& frame) override {
445 path_challenge_frame_ = frame;
fkastenholza3660102019-08-28 05:19:24 -0700446 EXPECT_EQ(IETF_PATH_CHALLENGE, framer_->current_received_frame_type());
447 EXPECT_TRUE(VersionHasIetfQuicFrames(transport_version_));
QUICHE teama6ef0a62019-03-07 20:34:33 -0500448 return true;
449 }
450
451 bool OnPathResponseFrame(const QuicPathResponseFrame& frame) override {
452 path_response_frame_ = frame;
fkastenholza3660102019-08-28 05:19:24 -0700453 EXPECT_EQ(IETF_PATH_RESPONSE, framer_->current_received_frame_type());
454 EXPECT_TRUE(VersionHasIetfQuicFrames(transport_version_));
QUICHE teama6ef0a62019-03-07 20:34:33 -0500455 return true;
456 }
457
458 bool OnGoAwayFrame(const QuicGoAwayFrame& frame) override {
459 goaway_frame_ = frame;
fkastenholza3660102019-08-28 05:19:24 -0700460 EXPECT_FALSE(VersionHasIetfQuicFrames(transport_version_));
461 EXPECT_EQ(0u, framer_->current_received_frame_type());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500462 return true;
463 }
464
fkastenholz3c4eabf2019-04-22 07:49:59 -0700465 bool OnMaxStreamsFrame(const QuicMaxStreamsFrame& frame) override {
466 max_streams_frame_ = frame;
fkastenholza3660102019-08-28 05:19:24 -0700467 EXPECT_TRUE(VersionHasIetfQuicFrames(transport_version_));
468 EXPECT_TRUE(IETF_MAX_STREAMS_UNIDIRECTIONAL ==
469 framer_->current_received_frame_type() ||
470 IETF_MAX_STREAMS_BIDIRECTIONAL ==
471 framer_->current_received_frame_type());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500472 return true;
473 }
474
fkastenholz3c4eabf2019-04-22 07:49:59 -0700475 bool OnStreamsBlockedFrame(const QuicStreamsBlockedFrame& frame) override {
476 streams_blocked_frame_ = frame;
fkastenholza3660102019-08-28 05:19:24 -0700477 EXPECT_TRUE(VersionHasIetfQuicFrames(transport_version_));
478 EXPECT_TRUE(IETF_STREAMS_BLOCKED_UNIDIRECTIONAL ==
479 framer_->current_received_frame_type() ||
480 IETF_STREAMS_BLOCKED_BIDIRECTIONAL ==
481 framer_->current_received_frame_type());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500482 return true;
483 }
484
485 bool OnWindowUpdateFrame(const QuicWindowUpdateFrame& frame) override {
486 window_update_frame_ = frame;
fkastenholza3660102019-08-28 05:19:24 -0700487 if (VersionHasIetfQuicFrames(transport_version_)) {
488 EXPECT_TRUE(IETF_MAX_DATA == framer_->current_received_frame_type() ||
489 IETF_MAX_STREAM_DATA ==
490 framer_->current_received_frame_type());
491 } else {
492 EXPECT_EQ(0u, framer_->current_received_frame_type());
493 }
QUICHE teama6ef0a62019-03-07 20:34:33 -0500494 return true;
495 }
496
497 bool OnBlockedFrame(const QuicBlockedFrame& frame) override {
498 blocked_frame_ = frame;
fkastenholza3660102019-08-28 05:19:24 -0700499 if (VersionHasIetfQuicFrames(transport_version_)) {
ianswett2f077442019-12-12 11:51:24 -0800500 EXPECT_TRUE(IETF_DATA_BLOCKED == framer_->current_received_frame_type() ||
501 IETF_STREAM_DATA_BLOCKED ==
fkastenholza3660102019-08-28 05:19:24 -0700502 framer_->current_received_frame_type());
503 } else {
504 EXPECT_EQ(0u, framer_->current_received_frame_type());
505 }
QUICHE teama6ef0a62019-03-07 20:34:33 -0500506 return true;
507 }
508
509 bool OnNewConnectionIdFrame(const QuicNewConnectionIdFrame& frame) override {
510 new_connection_id_ = frame;
fkastenholza3660102019-08-28 05:19:24 -0700511 EXPECT_EQ(IETF_NEW_CONNECTION_ID, framer_->current_received_frame_type());
512 EXPECT_TRUE(VersionHasIetfQuicFrames(transport_version_));
QUICHE teama6ef0a62019-03-07 20:34:33 -0500513 return true;
514 }
515
516 bool OnRetireConnectionIdFrame(
517 const QuicRetireConnectionIdFrame& frame) override {
fkastenholza3660102019-08-28 05:19:24 -0700518 EXPECT_EQ(IETF_RETIRE_CONNECTION_ID,
519 framer_->current_received_frame_type());
520 EXPECT_TRUE(VersionHasIetfQuicFrames(transport_version_));
QUICHE teama6ef0a62019-03-07 20:34:33 -0500521 retire_connection_id_ = frame;
522 return true;
523 }
524
525 bool OnNewTokenFrame(const QuicNewTokenFrame& frame) override {
526 new_token_ = frame;
fkastenholza3660102019-08-28 05:19:24 -0700527 EXPECT_EQ(IETF_NEW_TOKEN, framer_->current_received_frame_type());
528 EXPECT_TRUE(VersionHasIetfQuicFrames(transport_version_));
QUICHE teama6ef0a62019-03-07 20:34:33 -0500529 return true;
530 }
531
532 bool IsValidStatelessResetToken(QuicUint128 token) const override {
fkastenholza3660102019-08-28 05:19:24 -0700533 EXPECT_EQ(0u, framer_->current_received_frame_type());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500534 return token == kTestStatelessResetToken;
535 }
536
537 void OnAuthenticatedIetfStatelessResetPacket(
538 const QuicIetfStatelessResetPacket& packet) override {
539 stateless_reset_packet_ =
vasilvv0fc587f2019-09-06 13:33:08 -0700540 std::make_unique<QuicIetfStatelessResetPacket>(packet);
fkastenholza3660102019-08-28 05:19:24 -0700541 EXPECT_EQ(0u, framer_->current_received_frame_type());
542 }
543
544 void set_framer(QuicFramer* framer) {
545 framer_ = framer;
546 transport_version_ = framer->transport_version();
QUICHE teama6ef0a62019-03-07 20:34:33 -0500547 }
548
549 // Counters from the visitor_ callbacks.
550 int error_count_;
551 int version_mismatch_;
552 int packet_count_;
553 int frame_count_;
554 int complete_packets_;
555 bool accept_packet_;
556 bool accept_public_header_;
557
558 std::unique_ptr<QuicPacketHeader> header_;
559 std::unique_ptr<QuicPublicResetPacket> public_reset_packet_;
560 std::unique_ptr<QuicIetfStatelessResetPacket> stateless_reset_packet_;
561 std::unique_ptr<QuicVersionNegotiationPacket> version_negotiation_packet_;
dschinazi244f6dc2019-05-06 15:45:16 -0700562 std::unique_ptr<QuicConnectionId> retry_original_connection_id_;
563 std::unique_ptr<QuicConnectionId> retry_new_connection_id_;
564 std::unique_ptr<std::string> retry_token_;
dschinazi278efae2020-01-28 17:03:09 -0800565 std::unique_ptr<std::string> retry_token_integrity_tag_;
566 std::unique_ptr<std::string> retry_without_tag_;
dschinazi2dd75ee2020-01-29 09:21:37 -0800567 bool on_retry_packet_called_ = false;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500568 std::vector<std::unique_ptr<QuicStreamFrame>> stream_frames_;
569 std::vector<std::unique_ptr<QuicCryptoFrame>> crypto_frames_;
570 std::vector<std::unique_ptr<QuicAckFrame>> ack_frames_;
571 std::vector<std::unique_ptr<QuicStopWaitingFrame>> stop_waiting_frames_;
572 std::vector<std::unique_ptr<QuicPaddingFrame>> padding_frames_;
573 std::vector<std::unique_ptr<QuicPingFrame>> ping_frames_;
574 std::vector<std::unique_ptr<QuicMessageFrame>> message_frames_;
fayang01062942020-01-22 07:23:23 -0800575 std::vector<std::unique_ptr<QuicHandshakeDoneFrame>> handshake_done_frames_;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500576 std::vector<std::unique_ptr<QuicEncryptedPacket>> coalesced_packets_;
dschinazi4b5a68a2019-08-15 15:45:36 -0700577 std::vector<std::unique_ptr<QuicEncryptedPacket>> undecryptable_packets_;
578 std::vector<EncryptionLevel> undecryptable_decryption_levels_;
579 std::vector<bool> undecryptable_has_decryption_keys_;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500580 QuicRstStreamFrame rst_stream_frame_;
581 QuicConnectionCloseFrame connection_close_frame_;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500582 QuicStopSendingFrame stop_sending_frame_;
583 QuicGoAwayFrame goaway_frame_;
584 QuicPathChallengeFrame path_challenge_frame_;
585 QuicPathResponseFrame path_response_frame_;
586 QuicWindowUpdateFrame window_update_frame_;
587 QuicBlockedFrame blocked_frame_;
fkastenholz3c4eabf2019-04-22 07:49:59 -0700588 QuicStreamsBlockedFrame streams_blocked_frame_;
589 QuicMaxStreamsFrame max_streams_frame_;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500590 QuicNewConnectionIdFrame new_connection_id_;
591 QuicRetireConnectionIdFrame retire_connection_id_;
592 QuicNewTokenFrame new_token_;
vasilvvc48c8712019-03-11 13:38:16 -0700593 std::vector<std::unique_ptr<std::string>> stream_data_;
594 std::vector<std::unique_ptr<std::string>> crypto_data_;
fkastenholza3660102019-08-28 05:19:24 -0700595 QuicTransportVersion transport_version_;
596 QuicFramer* framer_;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500597};
598
599// Simple struct for defining a packet's content, and associated
600// parse error.
601struct PacketFragment {
vasilvvc48c8712019-03-11 13:38:16 -0700602 std::string error_if_missing;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500603 std::vector<unsigned char> fragment;
604};
605
606using PacketFragments = std::vector<struct PacketFragment>;
607
QUICHE teama6ef0a62019-03-07 20:34:33 -0500608class QuicFramerTest : public QuicTestWithParam<ParsedQuicVersion> {
609 public:
610 QuicFramerTest()
611 : encrypter_(new test::TestEncrypter()),
612 decrypter_(new test::TestDecrypter()),
613 version_(GetParam()),
614 start_(QuicTime::Zero() + QuicTime::Delta::FromMicroseconds(0x10)),
nharperf5e68452019-05-29 17:24:18 -0700615 framer_(AllSupportedVersions(),
QUICHE teama6ef0a62019-03-07 20:34:33 -0500616 start_,
617 Perspective::IS_SERVER,
618 kQuicDefaultConnectionIdLength) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500619 framer_.set_version(version_);
zhongyi546cc452019-04-12 15:27:49 -0700620 if (framer_.version().KnowsWhichDecrypterToUse()) {
621 framer_.InstallDecrypter(ENCRYPTION_INITIAL,
622 std::unique_ptr<QuicDecrypter>(decrypter_));
623 } else {
624 framer_.SetDecrypter(ENCRYPTION_INITIAL,
625 std::unique_ptr<QuicDecrypter>(decrypter_));
626 }
QUICHE team6987b4a2019-03-15 16:23:04 -0700627 framer_.SetEncrypter(ENCRYPTION_INITIAL,
QUICHE teama6ef0a62019-03-07 20:34:33 -0500628 std::unique_ptr<QuicEncrypter>(encrypter_));
629
630 framer_.set_visitor(&visitor_);
631 framer_.InferPacketHeaderTypeFromVersion();
fkastenholza3660102019-08-28 05:19:24 -0700632 visitor_.set_framer(&framer_);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500633 }
634
zhongyi546cc452019-04-12 15:27:49 -0700635 void SetDecrypterLevel(EncryptionLevel level) {
636 if (!framer_.version().KnowsWhichDecrypterToUse()) {
637 return;
638 }
639 decrypter_ = new TestDecrypter();
640 framer_.InstallDecrypter(level, std::unique_ptr<QuicDecrypter>(decrypter_));
641 }
642
QUICHE teama6ef0a62019-03-07 20:34:33 -0500643 // Helper function to get unsigned char representation of the handshake
rcha702be22019-08-30 15:20:12 -0700644 // protocol byte at position |pos| of the current QUIC version number.
645 unsigned char GetQuicVersionByte(int pos) {
646 return (CreateQuicVersionLabel(version_) >> 8 * (3 - pos)) & 0xff;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500647 }
648
649 bool CheckEncryption(QuicPacketNumber packet_number, QuicPacket* packet) {
650 if (packet_number != encrypter_->packet_number_) {
651 QUIC_LOG(ERROR) << "Encrypted incorrect packet number. expected "
652 << packet_number
653 << " actual: " << encrypter_->packet_number_;
654 return false;
655 }
656 if (packet->AssociatedData(framer_.transport_version()) !=
657 encrypter_->associated_data_) {
658 QUIC_LOG(ERROR) << "Encrypted incorrect associated data. expected "
659 << packet->AssociatedData(framer_.transport_version())
660 << " actual: " << encrypter_->associated_data_;
661 return false;
662 }
663 if (packet->Plaintext(framer_.transport_version()) !=
664 encrypter_->plaintext_) {
665 QUIC_LOG(ERROR) << "Encrypted incorrect plaintext data. expected "
666 << packet->Plaintext(framer_.transport_version())
667 << " actual: " << encrypter_->plaintext_;
668 return false;
669 }
670 return true;
671 }
672
673 bool CheckDecryption(const QuicEncryptedPacket& encrypted,
674 bool includes_version,
675 bool includes_diversification_nonce,
676 QuicConnectionIdLength destination_connection_id_length,
677 QuicConnectionIdLength source_connection_id_length) {
678 return CheckDecryption(
679 encrypted, includes_version, includes_diversification_nonce,
680 destination_connection_id_length, source_connection_id_length,
681 VARIABLE_LENGTH_INTEGER_LENGTH_0, 0, VARIABLE_LENGTH_INTEGER_LENGTH_0);
682 }
683
684 bool CheckDecryption(
685 const QuicEncryptedPacket& encrypted,
686 bool includes_version,
687 bool includes_diversification_nonce,
688 QuicConnectionIdLength destination_connection_id_length,
689 QuicConnectionIdLength source_connection_id_length,
690 QuicVariableLengthIntegerLength retry_token_length_length,
691 size_t retry_token_length,
692 QuicVariableLengthIntegerLength length_length) {
693 if (visitor_.header_->packet_number != decrypter_->packet_number_) {
694 QUIC_LOG(ERROR) << "Decrypted incorrect packet number. expected "
695 << visitor_.header_->packet_number
696 << " actual: " << decrypter_->packet_number_;
697 return false;
698 }
dmcardlecf0bfcf2019-12-13 08:08:21 -0800699 quiche::QuicheStringPiece associated_data =
QUICHE teama6ef0a62019-03-07 20:34:33 -0500700 QuicFramer::GetAssociatedDataFromEncryptedPacket(
701 framer_.transport_version(), encrypted,
702 destination_connection_id_length, source_connection_id_length,
703 includes_version, includes_diversification_nonce,
704 PACKET_4BYTE_PACKET_NUMBER, retry_token_length_length,
705 retry_token_length, length_length);
706 if (associated_data != decrypter_->associated_data_) {
707 QUIC_LOG(ERROR) << "Decrypted incorrect associated data. expected "
dmcardlecf0bfcf2019-12-13 08:08:21 -0800708 << quiche::QuicheTextUtils::HexEncode(associated_data)
QUICHE teama6ef0a62019-03-07 20:34:33 -0500709 << " actual: "
dmcardlecf0bfcf2019-12-13 08:08:21 -0800710 << quiche::QuicheTextUtils::HexEncode(
711 decrypter_->associated_data_);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500712 return false;
713 }
dmcardlecf0bfcf2019-12-13 08:08:21 -0800714 quiche::QuicheStringPiece ciphertext(
QUICHE teama6ef0a62019-03-07 20:34:33 -0500715 encrypted.AsStringPiece().substr(GetStartOfEncryptedData(
716 framer_.transport_version(), destination_connection_id_length,
717 source_connection_id_length, includes_version,
718 includes_diversification_nonce, PACKET_4BYTE_PACKET_NUMBER,
719 retry_token_length_length, retry_token_length, length_length)));
720 if (ciphertext != decrypter_->ciphertext_) {
721 QUIC_LOG(ERROR) << "Decrypted incorrect ciphertext data. expected "
dmcardlecf0bfcf2019-12-13 08:08:21 -0800722 << quiche::QuicheTextUtils::HexEncode(ciphertext)
723 << " actual: "
724 << quiche::QuicheTextUtils::HexEncode(
725 decrypter_->ciphertext_)
QUICHE teama6ef0a62019-03-07 20:34:33 -0500726 << " associated data: "
dmcardlecf0bfcf2019-12-13 08:08:21 -0800727 << quiche::QuicheTextUtils::HexEncode(associated_data);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500728 return false;
729 }
730 return true;
731 }
732
733 char* AsChars(unsigned char* data) { return reinterpret_cast<char*>(data); }
734
735 // Creates a new QuicEncryptedPacket by concatenating the various
736 // packet fragments in |fragments|.
737 std::unique_ptr<QuicEncryptedPacket> AssemblePacketFromFragments(
738 const PacketFragments& fragments) {
dschinazi66dea072019-04-09 11:41:06 -0700739 char* buffer = new char[kMaxOutgoingPacketSize + 1];
QUICHE teama6ef0a62019-03-07 20:34:33 -0500740 size_t len = 0;
741 for (const auto& fragment : fragments) {
742 memcpy(buffer + len, fragment.fragment.data(), fragment.fragment.size());
743 len += fragment.fragment.size();
744 }
vasilvv0fc587f2019-09-06 13:33:08 -0700745 return std::make_unique<QuicEncryptedPacket>(buffer, len, true);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500746 }
747
748 void CheckFramingBoundaries(const PacketFragments& fragments,
749 QuicErrorCode error_code) {
750 std::unique_ptr<QuicEncryptedPacket> packet(
751 AssemblePacketFromFragments(fragments));
752 // Check all the various prefixes of |packet| for the expected
753 // parse error and error code.
754 for (size_t i = 0; i < packet->length(); ++i) {
vasilvvc48c8712019-03-11 13:38:16 -0700755 std::string expected_error;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500756 size_t len = 0;
757 for (const auto& fragment : fragments) {
758 len += fragment.fragment.size();
759 if (i < len) {
760 expected_error = fragment.error_if_missing;
761 break;
762 }
763 }
764
765 if (expected_error.empty())
766 continue;
767
768 CheckProcessingFails(*packet, i, expected_error, error_code);
769 }
770 }
771
772 void CheckProcessingFails(const QuicEncryptedPacket& packet,
773 size_t len,
vasilvvc48c8712019-03-11 13:38:16 -0700774 std::string expected_error,
QUICHE teama6ef0a62019-03-07 20:34:33 -0500775 QuicErrorCode error_code) {
776 QuicEncryptedPacket encrypted(packet.data(), len, false);
777 EXPECT_FALSE(framer_.ProcessPacket(encrypted)) << "len: " << len;
778 EXPECT_EQ(expected_error, framer_.detailed_error()) << "len: " << len;
779 EXPECT_EQ(error_code, framer_.error()) << "len: " << len;
780 }
781
782 void CheckProcessingFails(unsigned char* packet,
783 size_t len,
vasilvvc48c8712019-03-11 13:38:16 -0700784 std::string expected_error,
QUICHE teama6ef0a62019-03-07 20:34:33 -0500785 QuicErrorCode error_code) {
786 QuicEncryptedPacket encrypted(AsChars(packet), len, false);
787 EXPECT_FALSE(framer_.ProcessPacket(encrypted)) << "len: " << len;
788 EXPECT_EQ(expected_error, framer_.detailed_error()) << "len: " << len;
789 EXPECT_EQ(error_code, framer_.error()) << "len: " << len;
790 }
791
792 // Checks if the supplied string matches data in the supplied StreamFrame.
vasilvvc48c8712019-03-11 13:38:16 -0700793 void CheckStreamFrameData(std::string str, QuicStreamFrame* frame) {
794 EXPECT_EQ(str, std::string(frame->data_buffer, frame->data_length));
QUICHE teama6ef0a62019-03-07 20:34:33 -0500795 }
796
797 void CheckCalculatePacketNumber(uint64_t expected_packet_number,
798 QuicPacketNumber last_packet_number) {
799 uint64_t wire_packet_number = expected_packet_number & kMask;
800 EXPECT_EQ(expected_packet_number,
801 QuicFramerPeer::CalculatePacketNumberFromWire(
802 &framer_, PACKET_4BYTE_PACKET_NUMBER, last_packet_number,
803 wire_packet_number))
804 << "last_packet_number: " << last_packet_number
805 << " wire_packet_number: " << wire_packet_number;
806 }
807
808 std::unique_ptr<QuicPacket> BuildDataPacket(const QuicPacketHeader& header,
809 const QuicFrames& frames) {
810 return BuildUnsizedDataPacket(&framer_, header, frames);
811 }
812
813 std::unique_ptr<QuicPacket> BuildDataPacket(const QuicPacketHeader& header,
814 const QuicFrames& frames,
815 size_t packet_size) {
816 return BuildUnsizedDataPacket(&framer_, header, frames, packet_size);
817 }
818
819 // N starts at 1.
820 QuicStreamId GetNthStreamid(QuicTransportVersion transport_version,
821 Perspective perspective,
822 bool bidirectional,
823 int n) {
824 if (bidirectional) {
825 return QuicUtils::GetFirstBidirectionalStreamId(transport_version,
826 perspective) +
827 ((n - 1) * QuicUtils::StreamIdDelta(transport_version));
828 }
829 // Unidirectional
830 return QuicUtils::GetFirstUnidirectionalStreamId(transport_version,
831 perspective) +
832 ((n - 1) * QuicUtils::StreamIdDelta(transport_version));
833 }
834
835 test::TestEncrypter* encrypter_;
836 test::TestDecrypter* decrypter_;
837 ParsedQuicVersion version_;
838 QuicTime start_;
839 QuicFramer framer_;
840 test::TestQuicVisitor visitor_;
841 SimpleBufferAllocator allocator_;
842};
843
844// Multiple test cases of QuicFramerTest use byte arrays to define packets for
845// testing, and these byte arrays contain the QUIC version. This macro explodes
846// the 32-bit version into four bytes in network order. Since it uses methods of
847// QuicFramerTest, it is only valid to use this in a QuicFramerTest.
rcha702be22019-08-30 15:20:12 -0700848#define QUIC_VERSION_BYTES \
849 GetQuicVersionByte(0), GetQuicVersionByte(1), GetQuicVersionByte(2), \
850 GetQuicVersionByte(3)
QUICHE teama6ef0a62019-03-07 20:34:33 -0500851
852// Run all framer tests with all supported versions of QUIC.
nharperf5e68452019-05-29 17:24:18 -0700853INSTANTIATE_TEST_SUITE_P(QuicFramerTests,
854 QuicFramerTest,
dschinazi98fc8d02019-09-17 23:34:49 -0700855 ::testing::ValuesIn(AllSupportedVersions()),
856 ::testing::PrintToStringParamName());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500857
858TEST_P(QuicFramerTest, CalculatePacketNumberFromWireNearEpochStart) {
859 // A few quick manual sanity checks.
860 CheckCalculatePacketNumber(UINT64_C(1), QuicPacketNumber());
861 CheckCalculatePacketNumber(kEpoch + 1, QuicPacketNumber(kMask));
862 CheckCalculatePacketNumber(kEpoch, QuicPacketNumber(kMask));
863 for (uint64_t j = 0; j < 10; j++) {
864 CheckCalculatePacketNumber(j, QuicPacketNumber());
865 CheckCalculatePacketNumber(kEpoch - 1 - j, QuicPacketNumber());
866 }
867
868 // Cases where the last number was close to the start of the range.
869 for (QuicPacketNumber last = QuicPacketNumber(1); last < QuicPacketNumber(10);
870 last++) {
871 // Small numbers should not wrap (even if they're out of order).
872 for (uint64_t j = 0; j < 10; j++) {
873 CheckCalculatePacketNumber(j, last);
874 }
875
876 // Large numbers should not wrap either (because we're near 0 already).
877 for (uint64_t j = 0; j < 10; j++) {
878 CheckCalculatePacketNumber(kEpoch - 1 - j, last);
879 }
880 }
881}
882
883TEST_P(QuicFramerTest, CalculatePacketNumberFromWireNearEpochEnd) {
884 // Cases where the last number was close to the end of the range
885 for (uint64_t i = 0; i < 10; i++) {
886 QuicPacketNumber last = QuicPacketNumber(kEpoch - i);
887
888 // Small numbers should wrap.
889 for (uint64_t j = 0; j < 10; j++) {
890 CheckCalculatePacketNumber(kEpoch + j, last);
891 }
892
893 // Large numbers should not (even if they're out of order).
894 for (uint64_t j = 0; j < 10; j++) {
895 CheckCalculatePacketNumber(kEpoch - 1 - j, last);
896 }
897 }
898}
899
900// Next check where we're in a non-zero epoch to verify we handle
901// reverse wrapping, too.
902TEST_P(QuicFramerTest, CalculatePacketNumberFromWireNearPrevEpoch) {
903 const uint64_t prev_epoch = 1 * kEpoch;
904 const uint64_t cur_epoch = 2 * kEpoch;
905 // Cases where the last number was close to the start of the range
906 for (uint64_t i = 0; i < 10; i++) {
907 QuicPacketNumber last = QuicPacketNumber(cur_epoch + i);
908 // Small number should not wrap (even if they're out of order).
909 for (uint64_t j = 0; j < 10; j++) {
910 CheckCalculatePacketNumber(cur_epoch + j, last);
911 }
912
913 // But large numbers should reverse wrap.
914 for (uint64_t j = 0; j < 10; j++) {
915 uint64_t num = kEpoch - 1 - j;
916 CheckCalculatePacketNumber(prev_epoch + num, last);
917 }
918 }
919}
920
921TEST_P(QuicFramerTest, CalculatePacketNumberFromWireNearNextEpoch) {
922 const uint64_t cur_epoch = 2 * kEpoch;
923 const uint64_t next_epoch = 3 * kEpoch;
924 // Cases where the last number was close to the end of the range
925 for (uint64_t i = 0; i < 10; i++) {
926 QuicPacketNumber last = QuicPacketNumber(next_epoch - 1 - i);
927
928 // Small numbers should wrap.
929 for (uint64_t j = 0; j < 10; j++) {
930 CheckCalculatePacketNumber(next_epoch + j, last);
931 }
932
933 // but large numbers should not (even if they're out of order).
934 for (uint64_t j = 0; j < 10; j++) {
935 uint64_t num = kEpoch - 1 - j;
936 CheckCalculatePacketNumber(cur_epoch + num, last);
937 }
938 }
939}
940
941TEST_P(QuicFramerTest, CalculatePacketNumberFromWireNearNextMax) {
942 const uint64_t max_number = std::numeric_limits<uint64_t>::max();
943 const uint64_t max_epoch = max_number & ~kMask;
944
945 // Cases where the last number was close to the end of the range
946 for (uint64_t i = 0; i < 10; i++) {
947 // Subtract 1, because the expected next packet number is 1 more than the
948 // last packet number.
949 QuicPacketNumber last = QuicPacketNumber(max_number - i - 1);
950
951 // Small numbers should not wrap, because they have nowhere to go.
952 for (uint64_t j = 0; j < 10; j++) {
953 CheckCalculatePacketNumber(max_epoch + j, last);
954 }
955
956 // Large numbers should not wrap either.
957 for (uint64_t j = 0; j < 10; j++) {
958 uint64_t num = kEpoch - 1 - j;
959 CheckCalculatePacketNumber(max_epoch + num, last);
960 }
961 }
962}
963
964TEST_P(QuicFramerTest, EmptyPacket) {
965 char packet[] = {0x00};
966 QuicEncryptedPacket encrypted(packet, 0, false);
967 EXPECT_FALSE(framer_.ProcessPacket(encrypted));
bncf6f82b12019-10-30 07:01:01 -0700968 EXPECT_THAT(framer_.error(), IsError(QUIC_INVALID_PACKET_HEADER));
QUICHE teama6ef0a62019-03-07 20:34:33 -0500969}
970
971TEST_P(QuicFramerTest, LargePacket) {
zhongyi546cc452019-04-12 15:27:49 -0700972 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500973 // clang-format off
dschinazie8d7fa72019-04-05 14:44:40 -0700974 unsigned char packet[kMaxIncomingPacketSize + 1] = {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500975 // public flags (8 byte connection_id)
976 0x28,
977 // connection_id
978 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
979 // packet number
980 0x78, 0x56, 0x34, 0x12,
981 // private flags
982 0x00,
983 };
dschinazie8d7fa72019-04-05 14:44:40 -0700984 unsigned char packet46[kMaxIncomingPacketSize + 1] = {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500985 // type (short header 4 byte packet number)
986 0x43,
987 // connection_id
988 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
989 // packet number
990 0x78, 0x56, 0x34, 0x12,
991 };
992 // clang-format on
993 unsigned char* p = packet;
bnc4e9283d2019-12-17 07:08:57 -0800994 size_t p_size = QUICHE_ARRAYSIZE(packet);
fayang36825da2019-08-21 14:01:27 -0700995 if (framer_.transport_version() > QUIC_VERSION_43) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500996 p = packet46;
bnc4e9283d2019-12-17 07:08:57 -0800997 p_size = QUICHE_ARRAYSIZE(packet46);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500998 }
999
1000 const size_t header_size = GetPacketHeaderSize(
1001 framer_.transport_version(), PACKET_8BYTE_CONNECTION_ID,
1002 PACKET_0BYTE_CONNECTION_ID, !kIncludeVersion,
1003 !kIncludeDiversificationNonce, PACKET_4BYTE_PACKET_NUMBER,
1004 VARIABLE_LENGTH_INTEGER_LENGTH_0, 0, VARIABLE_LENGTH_INTEGER_LENGTH_0);
1005
dschinazie8d7fa72019-04-05 14:44:40 -07001006 memset(p + header_size, 0, kMaxIncomingPacketSize - header_size);
QUICHE teama6ef0a62019-03-07 20:34:33 -05001007
1008 QuicEncryptedPacket encrypted(AsChars(p), p_size, false);
dschinazie8d7fa72019-04-05 14:44:40 -07001009 EXPECT_FALSE(framer_.ProcessPacket(encrypted));
QUICHE teama6ef0a62019-03-07 20:34:33 -05001010
1011 ASSERT_TRUE(visitor_.header_.get());
1012 // Make sure we've parsed the packet header, so we can send an error.
1013 EXPECT_EQ(FramerTestConnectionId(),
1014 visitor_.header_->destination_connection_id);
1015 // Make sure the correct error is propagated.
bncf6f82b12019-10-30 07:01:01 -07001016 EXPECT_THAT(framer_.error(), IsError(QUIC_PACKET_TOO_LARGE));
dschinazie8d7fa72019-04-05 14:44:40 -07001017 EXPECT_EQ("Packet too large.", framer_.detailed_error());
QUICHE teama6ef0a62019-03-07 20:34:33 -05001018}
1019
1020TEST_P(QuicFramerTest, PacketHeader) {
1021 if (framer_.transport_version() > QUIC_VERSION_43) {
1022 return;
1023 }
1024
1025 // clang-format off
1026 PacketFragments packet = {
1027 // public flags (8 byte connection_id)
1028 {"Unable to read public flags.",
1029 {0x28}},
1030 // connection_id
1031 {"Unable to read ConnectionId.",
1032 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
1033 // packet number
1034 {"Unable to read packet number.",
1035 {0x12, 0x34, 0x56, 0x78}},
1036 };
1037 // clang-format on
1038
1039 PacketFragments& fragments = packet;
1040
1041 std::unique_ptr<QuicEncryptedPacket> encrypted(
1042 AssemblePacketFromFragments(fragments));
1043
1044 EXPECT_FALSE(framer_.ProcessPacket(*encrypted));
bncf6f82b12019-10-30 07:01:01 -07001045 EXPECT_THAT(framer_.error(), IsError(QUIC_MISSING_PAYLOAD));
QUICHE teama6ef0a62019-03-07 20:34:33 -05001046 ASSERT_TRUE(visitor_.header_.get());
1047 EXPECT_EQ(FramerTestConnectionId(),
1048 visitor_.header_->destination_connection_id);
1049 EXPECT_FALSE(visitor_.header_->reset_flag);
1050 EXPECT_FALSE(visitor_.header_->version_flag);
1051 EXPECT_EQ(kPacketNumber, visitor_.header_->packet_number);
1052
1053 CheckFramingBoundaries(fragments, QUIC_INVALID_PACKET_HEADER);
fayangccbab732019-05-13 10:11:25 -07001054
1055 PacketHeaderFormat format;
fayange3f2f7b2019-09-19 17:01:57 -07001056 QuicLongHeaderType long_packet_type = INVALID_PACKET_TYPE;
fayangccbab732019-05-13 10:11:25 -07001057 bool version_flag;
dschinazib42a8c52019-05-30 09:45:01 -07001058 QuicConnectionId destination_connection_id, source_connection_id;
fayangccbab732019-05-13 10:11:25 -07001059 QuicVersionLabel version_label;
1060 std::string detailed_error;
dschinazibf0413d2019-10-07 14:19:53 -07001061 bool retry_token_present, use_length_prefix;
dmcardlecf0bfcf2019-12-13 08:08:21 -08001062 quiche::QuicheStringPiece retry_token;
dschinazibf0413d2019-10-07 14:19:53 -07001063 ParsedQuicVersion parsed_version = UnsupportedQuicVersion();
1064 const QuicErrorCode error_code = QuicFramer::ParsePublicHeaderDispatcher(
1065 *encrypted, kQuicDefaultConnectionIdLength, &format, &long_packet_type,
1066 &version_flag, &use_length_prefix, &version_label, &parsed_version,
1067 &destination_connection_id, &source_connection_id, &retry_token_present,
1068 &retry_token, &detailed_error);
1069 EXPECT_FALSE(retry_token_present);
1070 EXPECT_FALSE(use_length_prefix);
bncf6f82b12019-10-30 07:01:01 -07001071 EXPECT_THAT(error_code, IsQuicNoError());
fayangccbab732019-05-13 10:11:25 -07001072 EXPECT_EQ(GOOGLE_QUIC_PACKET, format);
1073 EXPECT_FALSE(version_flag);
dschinazib42a8c52019-05-30 09:45:01 -07001074 EXPECT_EQ(kQuicDefaultConnectionIdLength, destination_connection_id.length());
fayangccbab732019-05-13 10:11:25 -07001075 EXPECT_EQ(FramerTestConnectionId(), destination_connection_id);
dschinazib42a8c52019-05-30 09:45:01 -07001076 EXPECT_EQ(EmptyQuicConnectionId(), source_connection_id);
QUICHE teama6ef0a62019-03-07 20:34:33 -05001077}
1078
1079TEST_P(QuicFramerTest, LongPacketHeader) {
1080 // clang-format off
QUICHE teama6ef0a62019-03-07 20:34:33 -05001081 PacketFragments packet46 = {
dschinazibf0413d2019-10-07 14:19:53 -07001082 // type (long header with packet type ZERO_RTT)
dschinazi48ac9192019-07-31 00:07:26 -07001083 {"Unable to read first byte.",
dschinazibf0413d2019-10-07 14:19:53 -07001084 {0xD3}},
QUICHE teama6ef0a62019-03-07 20:34:33 -05001085 // version tag
1086 {"Unable to read protocol version.",
1087 {QUIC_VERSION_BYTES}},
1088 // connection_id length
1089 {"Unable to read ConnectionId length.",
1090 {0x50}},
1091 // connection_id
dschinazi48ac9192019-07-31 00:07:26 -07001092 {"Unable to read destination connection ID.",
QUICHE teama6ef0a62019-03-07 20:34:33 -05001093 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
1094 // packet number
1095 {"Unable to read packet number.",
1096 {0x12, 0x34, 0x56, 0x78}},
1097 };
1098 // clang-format on
1099
1100 if (framer_.transport_version() <= QUIC_VERSION_43 ||
1101 QuicVersionHasLongHeaderLengths(framer_.transport_version())) {
1102 return;
1103 }
1104
dschinazibf0413d2019-10-07 14:19:53 -07001105 SetDecrypterLevel(ENCRYPTION_ZERO_RTT);
QUICHE teama6ef0a62019-03-07 20:34:33 -05001106 std::unique_ptr<QuicEncryptedPacket> encrypted(
fayang36825da2019-08-21 14:01:27 -07001107 AssemblePacketFromFragments(packet46));
QUICHE teama6ef0a62019-03-07 20:34:33 -05001108
1109 EXPECT_FALSE(framer_.ProcessPacket(*encrypted));
bncf6f82b12019-10-30 07:01:01 -07001110 EXPECT_THAT(framer_.error(), IsError(QUIC_MISSING_PAYLOAD));
QUICHE teama6ef0a62019-03-07 20:34:33 -05001111 ASSERT_TRUE(visitor_.header_.get());
1112 EXPECT_EQ(FramerTestConnectionId(),
1113 visitor_.header_->destination_connection_id);
1114 EXPECT_FALSE(visitor_.header_->reset_flag);
1115 EXPECT_TRUE(visitor_.header_->version_flag);
1116 EXPECT_EQ(kPacketNumber, visitor_.header_->packet_number);
1117
fayang36825da2019-08-21 14:01:27 -07001118 CheckFramingBoundaries(packet46, QUIC_INVALID_PACKET_HEADER);
fayangccbab732019-05-13 10:11:25 -07001119
1120 PacketHeaderFormat format;
fayange3f2f7b2019-09-19 17:01:57 -07001121 QuicLongHeaderType long_packet_type = INVALID_PACKET_TYPE;
fayangccbab732019-05-13 10:11:25 -07001122 bool version_flag;
dschinazib42a8c52019-05-30 09:45:01 -07001123 QuicConnectionId destination_connection_id, source_connection_id;
fayangccbab732019-05-13 10:11:25 -07001124 QuicVersionLabel version_label;
1125 std::string detailed_error;
dschinazibf0413d2019-10-07 14:19:53 -07001126 bool retry_token_present, use_length_prefix;
dmcardlecf0bfcf2019-12-13 08:08:21 -08001127 quiche::QuicheStringPiece retry_token;
dschinazibf0413d2019-10-07 14:19:53 -07001128 ParsedQuicVersion parsed_version = UnsupportedQuicVersion();
1129 const QuicErrorCode error_code = QuicFramer::ParsePublicHeaderDispatcher(
1130 *encrypted, kQuicDefaultConnectionIdLength, &format, &long_packet_type,
1131 &version_flag, &use_length_prefix, &version_label, &parsed_version,
1132 &destination_connection_id, &source_connection_id, &retry_token_present,
1133 &retry_token, &detailed_error);
bncf6f82b12019-10-30 07:01:01 -07001134 EXPECT_THAT(error_code, IsQuicNoError());
dschinazibf0413d2019-10-07 14:19:53 -07001135 EXPECT_EQ("", detailed_error);
1136 EXPECT_FALSE(retry_token_present);
1137 EXPECT_FALSE(use_length_prefix);
fayangccbab732019-05-13 10:11:25 -07001138 EXPECT_EQ(IETF_QUIC_LONG_HEADER_PACKET, format);
1139 EXPECT_TRUE(version_flag);
dschinazib42a8c52019-05-30 09:45:01 -07001140 EXPECT_EQ(kQuicDefaultConnectionIdLength, destination_connection_id.length());
fayangccbab732019-05-13 10:11:25 -07001141 EXPECT_EQ(FramerTestConnectionId(), destination_connection_id);
dschinazib42a8c52019-05-30 09:45:01 -07001142 EXPECT_EQ(EmptyQuicConnectionId(), source_connection_id);
1143}
1144
1145TEST_P(QuicFramerTest, LongPacketHeaderWithBothConnectionIds) {
1146 if (framer_.transport_version() <= QUIC_VERSION_43) {
1147 // This test requires an IETF long header.
1148 return;
1149 }
dschinazib42a8c52019-05-30 09:45:01 -07001150 SetDecrypterLevel(ENCRYPTION_ZERO_RTT);
dschinazib42a8c52019-05-30 09:45:01 -07001151 // clang-format off
1152 unsigned char packet[] = {
1153 // public flags (long header with packet type ZERO_RTT_PROTECTED and
1154 // 4-byte packet number)
fayang36825da2019-08-21 14:01:27 -07001155 0xD3,
dschinazib42a8c52019-05-30 09:45:01 -07001156 // version
1157 QUIC_VERSION_BYTES,
1158 // connection ID lengths
1159 0x55,
1160 // destination connection ID
1161 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
1162 // source connection ID
1163 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x11,
dschinazibf0413d2019-10-07 14:19:53 -07001164 // packet number
1165 0x12, 0x34, 0x56, 0x00,
1166 // padding frame
1167 0x00,
1168 };
1169 unsigned char packet49[] = {
1170 // public flags (long header with packet type ZERO_RTT_PROTECTED and
1171 // 4-byte packet number)
1172 0xD3,
1173 // version
1174 QUIC_VERSION_BYTES,
1175 // destination connection ID length
1176 0x08,
1177 // destination connection ID
1178 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
1179 // source connection ID length
1180 0x08,
1181 // source connection ID
1182 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x11,
dschinazib42a8c52019-05-30 09:45:01 -07001183 // long header packet length
1184 0x05,
1185 // packet number
1186 0x12, 0x34, 0x56, 0x00,
1187 // padding frame
1188 0x00,
1189 };
1190 // clang-format on
1191
dschinazibf0413d2019-10-07 14:19:53 -07001192 unsigned char* p = packet;
bnc4e9283d2019-12-17 07:08:57 -08001193 size_t p_length = QUICHE_ARRAYSIZE(packet);
dschinazibf0413d2019-10-07 14:19:53 -07001194 if (framer_.transport_version() >= QUIC_VERSION_49) {
1195 p = packet49;
bnc4e9283d2019-12-17 07:08:57 -08001196 p_length = QUICHE_ARRAYSIZE(packet49);
dschinazibf0413d2019-10-07 14:19:53 -07001197 }
1198
1199 QuicEncryptedPacket encrypted(AsChars(p), p_length, false);
dschinazib42a8c52019-05-30 09:45:01 -07001200 PacketHeaderFormat format = GOOGLE_QUIC_PACKET;
fayange3f2f7b2019-09-19 17:01:57 -07001201 QuicLongHeaderType long_packet_type = INVALID_PACKET_TYPE;
dschinazib42a8c52019-05-30 09:45:01 -07001202 bool version_flag = false;
1203 QuicConnectionId destination_connection_id, source_connection_id;
1204 QuicVersionLabel version_label = 0;
1205 std::string detailed_error = "";
dschinazibf0413d2019-10-07 14:19:53 -07001206 bool retry_token_present, use_length_prefix;
dmcardlecf0bfcf2019-12-13 08:08:21 -08001207 quiche::QuicheStringPiece retry_token;
dschinazibf0413d2019-10-07 14:19:53 -07001208 ParsedQuicVersion parsed_version = UnsupportedQuicVersion();
1209 const QuicErrorCode error_code = QuicFramer::ParsePublicHeaderDispatcher(
1210 encrypted, kQuicDefaultConnectionIdLength, &format, &long_packet_type,
1211 &version_flag, &use_length_prefix, &version_label, &parsed_version,
1212 &destination_connection_id, &source_connection_id, &retry_token_present,
1213 &retry_token, &detailed_error);
bncf6f82b12019-10-30 07:01:01 -07001214 EXPECT_THAT(error_code, IsQuicNoError());
dschinazibf0413d2019-10-07 14:19:53 -07001215 EXPECT_FALSE(retry_token_present);
1216 EXPECT_EQ(framer_.version().HasLengthPrefixedConnectionIds(),
1217 use_length_prefix);
dschinazib42a8c52019-05-30 09:45:01 -07001218 EXPECT_EQ("", detailed_error);
1219 EXPECT_EQ(IETF_QUIC_LONG_HEADER_PACKET, format);
1220 EXPECT_TRUE(version_flag);
1221 EXPECT_EQ(FramerTestConnectionId(), destination_connection_id);
1222 EXPECT_EQ(FramerTestConnectionIdPlusOne(), source_connection_id);
QUICHE teama6ef0a62019-03-07 20:34:33 -05001223}
1224
dschinazi243eabc2019-08-05 16:15:29 -07001225TEST_P(QuicFramerTest, ParsePublicHeader) {
dschinazi243eabc2019-08-05 16:15:29 -07001226 // clang-format off
1227 unsigned char packet[] = {
1228 // public flags (version included, 8-byte connection ID,
1229 // 4-byte packet number)
1230 0x29,
1231 // connection_id
1232 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
1233 // version
1234 QUIC_VERSION_BYTES,
1235 // packet number
1236 0x12, 0x34, 0x56, 0x78,
1237 // padding frame
1238 0x00,
1239 };
fayang36825da2019-08-21 14:01:27 -07001240 unsigned char packet46[] = {
1241 // public flags (long header with packet type HANDSHAKE and
1242 // 4-byte packet number)
1243 0xE3,
1244 // version
1245 QUIC_VERSION_BYTES,
1246 // connection ID lengths
1247 0x50,
1248 // destination connection ID
1249 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
1250 // long header packet length
1251 0x05,
1252 // packet number
1253 0x12, 0x34, 0x56, 0x78,
1254 // padding frame
1255 0x00,
dschinazi243eabc2019-08-05 16:15:29 -07001256 };
dschinazic73506e2019-09-20 13:26:46 -07001257 unsigned char packet49[] = {
dschinazi243eabc2019-08-05 16:15:29 -07001258 // public flags (long header with packet type HANDSHAKE and
1259 // 4-byte packet number)
1260 0xE3,
1261 // version
1262 QUIC_VERSION_BYTES,
1263 // destination connection ID length
1264 0x08,
1265 // destination connection ID
1266 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
1267 // source connection ID length
1268 0x00,
1269 // long header packet length
1270 0x05,
1271 // packet number
1272 0x12, 0x34, 0x56, 0x78,
1273 // padding frame
1274 0x00,
1275 };
1276 // clang-format on
1277 unsigned char* p = packet;
bnc4e9283d2019-12-17 07:08:57 -08001278 size_t p_length = QUICHE_ARRAYSIZE(packet);
dschinazic73506e2019-09-20 13:26:46 -07001279 if (framer_.transport_version() >= QUIC_VERSION_49) {
1280 p = packet49;
bnc4e9283d2019-12-17 07:08:57 -08001281 p_length = QUICHE_ARRAYSIZE(packet49);
fayang36825da2019-08-21 14:01:27 -07001282 } else if (framer_.transport_version() >= QUIC_VERSION_46) {
1283 p = packet46;
bnc4e9283d2019-12-17 07:08:57 -08001284 p_length = QUICHE_ARRAYSIZE(packet46);
dschinazi243eabc2019-08-05 16:15:29 -07001285 }
1286
1287 uint8_t first_byte = 0x33;
1288 PacketHeaderFormat format = GOOGLE_QUIC_PACKET;
1289 bool version_present = false, has_length_prefix = false;
1290 QuicVersionLabel version_label = 0;
1291 ParsedQuicVersion parsed_version = UnsupportedQuicVersion();
1292 QuicConnectionId destination_connection_id = EmptyQuicConnectionId(),
1293 source_connection_id = EmptyQuicConnectionId();
1294 QuicLongHeaderType long_packet_type = INVALID_PACKET_TYPE;
1295 QuicVariableLengthIntegerLength retry_token_length_length =
1296 VARIABLE_LENGTH_INTEGER_LENGTH_4;
dmcardlecf0bfcf2019-12-13 08:08:21 -08001297 quiche::QuicheStringPiece retry_token;
dschinazi243eabc2019-08-05 16:15:29 -07001298 std::string detailed_error = "foobar";
1299
1300 QuicDataReader reader(AsChars(p), p_length);
1301 const QuicErrorCode parse_error = QuicFramer::ParsePublicHeader(
1302 &reader, kQuicDefaultConnectionIdLength,
1303 /*ietf_format=*/
1304 VersionHasIetfInvariantHeader(framer_.transport_version()), &first_byte,
1305 &format, &version_present, &has_length_prefix, &version_label,
1306 &parsed_version, &destination_connection_id, &source_connection_id,
1307 &long_packet_type, &retry_token_length_length, &retry_token,
1308 &detailed_error);
bncf6f82b12019-10-30 07:01:01 -07001309 EXPECT_THAT(parse_error, IsQuicNoError());
dschinazi243eabc2019-08-05 16:15:29 -07001310 EXPECT_EQ("", detailed_error);
1311 EXPECT_EQ(p[0], first_byte);
1312 EXPECT_TRUE(version_present);
1313 EXPECT_EQ(framer_.version().HasLengthPrefixedConnectionIds(),
1314 has_length_prefix);
1315 EXPECT_EQ(CreateQuicVersionLabel(framer_.version()), version_label);
1316 EXPECT_EQ(framer_.version(), parsed_version);
1317 EXPECT_EQ(FramerTestConnectionId(), destination_connection_id);
1318 EXPECT_EQ(EmptyQuicConnectionId(), source_connection_id);
1319 EXPECT_EQ(VARIABLE_LENGTH_INTEGER_LENGTH_0, retry_token_length_length);
dmcardlecf0bfcf2019-12-13 08:08:21 -08001320 EXPECT_EQ(quiche::QuicheStringPiece(), retry_token);
dschinazi243eabc2019-08-05 16:15:29 -07001321 if (VersionHasIetfInvariantHeader(framer_.transport_version())) {
1322 EXPECT_EQ(IETF_QUIC_LONG_HEADER_PACKET, format);
1323 EXPECT_EQ(HANDSHAKE, long_packet_type);
1324 } else {
1325 EXPECT_EQ(GOOGLE_QUIC_PACKET, format);
1326 }
1327}
1328
dschinazi81eb4e02019-09-27 17:12:17 -07001329TEST_P(QuicFramerTest, ParsePublicHeaderProxBadSourceConnectionIdLength) {
1330 if (!framer_.version().HasLengthPrefixedConnectionIds()) {
1331 return;
1332 }
dschinazi81eb4e02019-09-27 17:12:17 -07001333 // clang-format off
1334 unsigned char packet[] = {
1335 // public flags (long header with packet type HANDSHAKE and
1336 // 4-byte packet number)
1337 0xE3,
1338 // version
1339 'P', 'R', 'O', 'X',
1340 // destination connection ID length
1341 0x08,
1342 // destination connection ID
1343 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
1344 // source connection ID length (bogus)
1345 0xEE,
1346 // long header packet length
1347 0x05,
1348 // packet number
1349 0x12, 0x34, 0x56, 0x78,
1350 // padding frame
1351 0x00,
1352 };
1353 // clang-format on
1354 unsigned char* p = packet;
bnc4e9283d2019-12-17 07:08:57 -08001355 size_t p_length = QUICHE_ARRAYSIZE(packet);
dschinazi81eb4e02019-09-27 17:12:17 -07001356
1357 uint8_t first_byte = 0x33;
1358 PacketHeaderFormat format = GOOGLE_QUIC_PACKET;
1359 bool version_present = false, has_length_prefix = false;
1360 QuicVersionLabel version_label = 0;
1361 ParsedQuicVersion parsed_version = UnsupportedQuicVersion();
1362 QuicConnectionId destination_connection_id = EmptyQuicConnectionId(),
1363 source_connection_id = EmptyQuicConnectionId();
1364 QuicLongHeaderType long_packet_type = INVALID_PACKET_TYPE;
1365 QuicVariableLengthIntegerLength retry_token_length_length =
1366 VARIABLE_LENGTH_INTEGER_LENGTH_4;
dmcardlecf0bfcf2019-12-13 08:08:21 -08001367 quiche::QuicheStringPiece retry_token;
dschinazi81eb4e02019-09-27 17:12:17 -07001368 std::string detailed_error = "foobar";
1369
1370 QuicDataReader reader(AsChars(p), p_length);
1371 const QuicErrorCode parse_error = QuicFramer::ParsePublicHeader(
1372 &reader, kQuicDefaultConnectionIdLength,
1373 /*ietf_format=*/true, &first_byte, &format, &version_present,
1374 &has_length_prefix, &version_label, &parsed_version,
1375 &destination_connection_id, &source_connection_id, &long_packet_type,
1376 &retry_token_length_length, &retry_token, &detailed_error);
bncf6f82b12019-10-30 07:01:01 -07001377 EXPECT_THAT(parse_error, IsQuicNoError());
dschinazi81eb4e02019-09-27 17:12:17 -07001378 EXPECT_EQ("", detailed_error);
1379 EXPECT_EQ(p[0], first_byte);
1380 EXPECT_TRUE(version_present);
1381 EXPECT_TRUE(has_length_prefix);
1382 EXPECT_EQ(0x50524F58u, version_label); // "PROX"
1383 EXPECT_EQ(UnsupportedQuicVersion(), parsed_version);
1384 EXPECT_EQ(FramerTestConnectionId(), destination_connection_id);
1385 EXPECT_EQ(EmptyQuicConnectionId(), source_connection_id);
1386 EXPECT_EQ(VARIABLE_LENGTH_INTEGER_LENGTH_0, retry_token_length_length);
dmcardlecf0bfcf2019-12-13 08:08:21 -08001387 EXPECT_EQ(quiche::QuicheStringPiece(), retry_token);
dschinazi81eb4e02019-09-27 17:12:17 -07001388 EXPECT_EQ(IETF_QUIC_LONG_HEADER_PACKET, format);
1389}
1390
dschinazi346b7ce2019-06-05 01:38:18 -07001391TEST_P(QuicFramerTest, ClientConnectionIdFromShortHeaderToClient) {
dschinazi346b7ce2019-06-05 01:38:18 -07001392 if (!framer_.version().SupportsClientConnectionIds()) {
1393 return;
1394 }
1395 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
1396 QuicFramerPeer::SetLastSerializedServerConnectionId(&framer_,
1397 TestConnectionId(0x33));
1398 QuicFramerPeer::SetPerspective(&framer_, Perspective::IS_CLIENT);
1399 framer_.SetExpectedClientConnectionIdLength(kQuicDefaultConnectionIdLength);
1400 // clang-format off
1401 unsigned char packet[] = {
1402 // type (short header, 4 byte packet number)
1403 0x43,
1404 // connection_id
1405 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
1406 // packet number
1407 0x13, 0x37, 0x42, 0x33,
1408 // padding frame
1409 0x00,
1410 };
1411 // clang-format on
bnc4e9283d2019-12-17 07:08:57 -08001412 QuicEncryptedPacket encrypted(AsChars(packet), QUICHE_ARRAYSIZE(packet),
1413 false);
dschinazi346b7ce2019-06-05 01:38:18 -07001414 EXPECT_TRUE(framer_.ProcessPacket(encrypted));
bncf6f82b12019-10-30 07:01:01 -07001415 EXPECT_THAT(framer_.error(), IsQuicNoError());
dschinazi346b7ce2019-06-05 01:38:18 -07001416 EXPECT_EQ("", framer_.detailed_error());
1417 ASSERT_TRUE(visitor_.header_.get());
1418 EXPECT_EQ(FramerTestConnectionId(),
1419 visitor_.header_->destination_connection_id);
1420 EXPECT_EQ(TestConnectionId(0x33), visitor_.header_->source_connection_id);
1421}
1422
1423// In short header packets from client to server, the client connection ID
1424// is omitted, but the framer adds it to the header struct using its
1425// last serialized client connection ID. This test ensures that this
1426// mechanism behaves as expected.
1427TEST_P(QuicFramerTest, ClientConnectionIdFromShortHeaderToServer) {
dschinazi346b7ce2019-06-05 01:38:18 -07001428 if (!framer_.version().SupportsClientConnectionIds()) {
1429 return;
1430 }
1431 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
1432 QuicFramerPeer::SetLastSerializedClientConnectionId(&framer_,
1433 TestConnectionId(0x33));
1434 QuicFramerPeer::SetPerspective(&framer_, Perspective::IS_SERVER);
1435 // clang-format off
1436 unsigned char packet[] = {
1437 // type (short header, 4 byte packet number)
1438 0x43,
1439 // connection_id
1440 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
1441 // packet number
1442 0x13, 0x37, 0x42, 0x33,
1443 // padding frame
1444 0x00,
1445 };
1446 // clang-format on
bnc4e9283d2019-12-17 07:08:57 -08001447 QuicEncryptedPacket encrypted(AsChars(packet), QUICHE_ARRAYSIZE(packet),
1448 false);
dschinazi346b7ce2019-06-05 01:38:18 -07001449 EXPECT_TRUE(framer_.ProcessPacket(encrypted));
bncf6f82b12019-10-30 07:01:01 -07001450 EXPECT_THAT(framer_.error(), IsQuicNoError());
dschinazi346b7ce2019-06-05 01:38:18 -07001451 EXPECT_EQ("", framer_.detailed_error());
1452 ASSERT_TRUE(visitor_.header_.get());
1453 EXPECT_EQ(FramerTestConnectionId(),
1454 visitor_.header_->destination_connection_id);
1455 EXPECT_EQ(TestConnectionId(0x33), visitor_.header_->source_connection_id);
1456}
1457
QUICHE teama6ef0a62019-03-07 20:34:33 -05001458TEST_P(QuicFramerTest, PacketHeaderWith0ByteConnectionId) {
zhongyi546cc452019-04-12 15:27:49 -07001459 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
dschinazi7b9278c2019-05-20 07:36:21 -07001460 QuicFramerPeer::SetLastSerializedServerConnectionId(&framer_,
1461 FramerTestConnectionId());
QUICHE teama6ef0a62019-03-07 20:34:33 -05001462 QuicFramerPeer::SetPerspective(&framer_, Perspective::IS_CLIENT);
1463
1464 // clang-format off
1465 PacketFragments packet = {
1466 // public flags (0 byte connection_id)
1467 {"Unable to read public flags.",
1468 {0x20}},
1469 // connection_id
1470 // packet number
1471 {"Unable to read packet number.",
1472 {0x12, 0x34, 0x56, 0x78}},
1473 };
1474
QUICHE teama6ef0a62019-03-07 20:34:33 -05001475 PacketFragments packet46 = {
1476 // type (short header, 4 byte packet number)
dschinazi48ac9192019-07-31 00:07:26 -07001477 {"Unable to read first byte.",
QUICHE teama6ef0a62019-03-07 20:34:33 -05001478 {0x43}},
1479 // connection_id
1480 // packet number
1481 {"Unable to read packet number.",
1482 {0x12, 0x34, 0x56, 0x78}},
1483 };
nharper55fa6132019-05-07 19:37:21 -07001484
1485 PacketFragments packet_hp = {
1486 // type (short header, 4 byte packet number)
dschinazi48ac9192019-07-31 00:07:26 -07001487 {"Unable to read first byte.",
nharper55fa6132019-05-07 19:37:21 -07001488 {0x43}},
1489 // connection_id
1490 // packet number
1491 {"",
1492 {0x12, 0x34, 0x56, 0x78}},
1493 };
QUICHE teama6ef0a62019-03-07 20:34:33 -05001494 // clang-format on
1495
1496 PacketFragments& fragments =
nharper55fa6132019-05-07 19:37:21 -07001497 framer_.version().HasHeaderProtection()
1498 ? packet_hp
fayang36825da2019-08-21 14:01:27 -07001499 : (framer_.transport_version() >= QUIC_VERSION_46 ? packet46
1500 : packet);
QUICHE teama6ef0a62019-03-07 20:34:33 -05001501 std::unique_ptr<QuicEncryptedPacket> encrypted(
1502 AssemblePacketFromFragments(fragments));
1503 EXPECT_FALSE(framer_.ProcessPacket(*encrypted));
bncf6f82b12019-10-30 07:01:01 -07001504 EXPECT_THAT(framer_.error(), IsError(QUIC_MISSING_PAYLOAD));
QUICHE teama6ef0a62019-03-07 20:34:33 -05001505 ASSERT_TRUE(visitor_.header_.get());
dschinazi5e1a7b22019-07-31 12:23:21 -07001506 EXPECT_EQ(FramerTestConnectionId(), visitor_.header_->source_connection_id);
QUICHE teama6ef0a62019-03-07 20:34:33 -05001507 EXPECT_FALSE(visitor_.header_->reset_flag);
1508 EXPECT_FALSE(visitor_.header_->version_flag);
1509 EXPECT_EQ(kPacketNumber, visitor_.header_->packet_number);
1510
1511 CheckFramingBoundaries(fragments, QUIC_INVALID_PACKET_HEADER);
1512}
1513
1514TEST_P(QuicFramerTest, PacketHeaderWithVersionFlag) {
zhongyi546cc452019-04-12 15:27:49 -07001515 SetDecrypterLevel(ENCRYPTION_ZERO_RTT);
QUICHE teama6ef0a62019-03-07 20:34:33 -05001516 // clang-format off
1517 PacketFragments packet = {
1518 // public flags (0 byte connection_id)
1519 {"Unable to read public flags.",
1520 {0x29}},
1521 // connection_id
1522 {"Unable to read ConnectionId.",
1523 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
1524 // version tag
1525 {"Unable to read protocol version.",
1526 {QUIC_VERSION_BYTES}},
1527 // packet number
1528 {"Unable to read packet number.",
1529 {0x12, 0x34, 0x56, 0x78}},
1530 };
1531
QUICHE teama6ef0a62019-03-07 20:34:33 -05001532 PacketFragments packet46 = {
1533 // type (long header with packet type ZERO_RTT_PROTECTED and 4 bytes
1534 // packet number)
dschinazi48ac9192019-07-31 00:07:26 -07001535 {"Unable to read first byte.",
QUICHE teama6ef0a62019-03-07 20:34:33 -05001536 {0xD3}},
1537 // version tag
1538 {"Unable to read protocol version.",
1539 {QUIC_VERSION_BYTES}},
1540 // connection_id length
1541 {"Unable to read ConnectionId length.",
1542 {0x50}},
1543 // connection_id
dschinazi48ac9192019-07-31 00:07:26 -07001544 {"Unable to read destination connection ID.",
QUICHE teama6ef0a62019-03-07 20:34:33 -05001545 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
1546 // packet number
1547 {"Unable to read packet number.",
1548 {0x12, 0x34, 0x56, 0x78}},
1549 };
1550
dschinazic73506e2019-09-20 13:26:46 -07001551 PacketFragments packet49 = {
QUICHE teama6ef0a62019-03-07 20:34:33 -05001552 // type (long header with packet type ZERO_RTT_PROTECTED and 4 bytes
1553 // packet number)
dschinazi48ac9192019-07-31 00:07:26 -07001554 {"Unable to read first byte.",
QUICHE teama6ef0a62019-03-07 20:34:33 -05001555 {0xD3}},
1556 // version tag
1557 {"Unable to read protocol version.",
1558 {QUIC_VERSION_BYTES}},
dschinazi48ac9192019-07-31 00:07:26 -07001559 // destination connection ID length
1560 {"Unable to read destination connection ID.",
1561 {0x08}},
1562 // destination connection ID
1563 {"Unable to read destination connection ID.",
QUICHE teama6ef0a62019-03-07 20:34:33 -05001564 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
dschinazi48ac9192019-07-31 00:07:26 -07001565 // source connection ID length
1566 {"Unable to read source connection ID.",
1567 {0x00}},
QUICHE teama6ef0a62019-03-07 20:34:33 -05001568 // long header packet length
1569 {"Unable to read long header payload length.",
1570 {0x04}},
1571 // packet number
1572 {"Long header payload length longer than packet.",
1573 {0x12, 0x34, 0x56, 0x78}},
1574 };
1575 // clang-format on
1576
1577 PacketFragments& fragments =
dschinazic73506e2019-09-20 13:26:46 -07001578 framer_.transport_version() >= QUIC_VERSION_49
1579 ? packet49
fayang36825da2019-08-21 14:01:27 -07001580 : (framer_.transport_version() >= QUIC_VERSION_46 ? packet46
1581 : packet);
QUICHE teama6ef0a62019-03-07 20:34:33 -05001582 std::unique_ptr<QuicEncryptedPacket> encrypted(
1583 AssemblePacketFromFragments(fragments));
1584 EXPECT_FALSE(framer_.ProcessPacket(*encrypted));
bncf6f82b12019-10-30 07:01:01 -07001585 EXPECT_THAT(framer_.error(), IsError(QUIC_MISSING_PAYLOAD));
QUICHE teama6ef0a62019-03-07 20:34:33 -05001586 ASSERT_TRUE(visitor_.header_.get());
1587 EXPECT_EQ(FramerTestConnectionId(),
1588 visitor_.header_->destination_connection_id);
1589 EXPECT_FALSE(visitor_.header_->reset_flag);
1590 EXPECT_TRUE(visitor_.header_->version_flag);
1591 EXPECT_EQ(GetParam(), visitor_.header_->version);
1592 EXPECT_EQ(kPacketNumber, visitor_.header_->packet_number);
1593
1594 CheckFramingBoundaries(fragments, QUIC_INVALID_PACKET_HEADER);
1595}
1596
1597TEST_P(QuicFramerTest, PacketHeaderWith4BytePacketNumber) {
zhongyi546cc452019-04-12 15:27:49 -07001598 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
QUICHE teama6ef0a62019-03-07 20:34:33 -05001599 QuicFramerPeer::SetLargestPacketNumber(&framer_, kPacketNumber - 2);
1600
1601 // clang-format off
1602 PacketFragments packet = {
1603 // public flags (8 byte connection_id and 4 byte packet number)
1604 {"Unable to read public flags.",
1605 {0x28}},
1606 // connection_id
1607 {"Unable to read ConnectionId.",
1608 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
1609 // packet number
1610 {"Unable to read packet number.",
1611 {0x12, 0x34, 0x56, 0x78}},
1612 };
1613
QUICHE teama6ef0a62019-03-07 20:34:33 -05001614 PacketFragments packet46 = {
1615 // type (short header, 4 byte packet number)
dschinazi48ac9192019-07-31 00:07:26 -07001616 {"Unable to read first byte.",
QUICHE teama6ef0a62019-03-07 20:34:33 -05001617 {0x43}},
1618 // connection_id
dschinazi48ac9192019-07-31 00:07:26 -07001619 {"Unable to read destination connection ID.",
QUICHE teama6ef0a62019-03-07 20:34:33 -05001620 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
1621 // packet number
1622 {"Unable to read packet number.",
1623 {0x12, 0x34, 0x56, 0x78}},
1624 };
nharper55fa6132019-05-07 19:37:21 -07001625
1626 PacketFragments packet_hp = {
1627 // type (short header, 4 byte packet number)
dschinazi48ac9192019-07-31 00:07:26 -07001628 {"Unable to read first byte.",
nharper55fa6132019-05-07 19:37:21 -07001629 {0x43}},
1630 // connection_id
dschinazi48ac9192019-07-31 00:07:26 -07001631 {"Unable to read destination connection ID.",
nharper55fa6132019-05-07 19:37:21 -07001632 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
1633 // packet number
1634 {"",
1635 {0x12, 0x34, 0x56, 0x78}},
1636 };
QUICHE teama6ef0a62019-03-07 20:34:33 -05001637 // clang-format on
1638
1639 PacketFragments& fragments =
nharper55fa6132019-05-07 19:37:21 -07001640 framer_.version().HasHeaderProtection()
1641 ? packet_hp
fayang36825da2019-08-21 14:01:27 -07001642 : (framer_.transport_version() >= QUIC_VERSION_46 ? packet46
1643 : packet);
QUICHE teama6ef0a62019-03-07 20:34:33 -05001644 std::unique_ptr<QuicEncryptedPacket> encrypted(
1645 AssemblePacketFromFragments(fragments));
1646 EXPECT_FALSE(framer_.ProcessPacket(*encrypted));
bncf6f82b12019-10-30 07:01:01 -07001647 EXPECT_THAT(framer_.error(), IsError(QUIC_MISSING_PAYLOAD));
QUICHE teama6ef0a62019-03-07 20:34:33 -05001648 ASSERT_TRUE(visitor_.header_.get());
1649 EXPECT_EQ(FramerTestConnectionId(),
1650 visitor_.header_->destination_connection_id);
1651 EXPECT_FALSE(visitor_.header_->reset_flag);
1652 EXPECT_FALSE(visitor_.header_->version_flag);
1653 EXPECT_EQ(kPacketNumber, visitor_.header_->packet_number);
1654
1655 CheckFramingBoundaries(fragments, QUIC_INVALID_PACKET_HEADER);
1656}
1657
1658TEST_P(QuicFramerTest, PacketHeaderWith2BytePacketNumber) {
zhongyi546cc452019-04-12 15:27:49 -07001659 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
QUICHE teama6ef0a62019-03-07 20:34:33 -05001660 QuicFramerPeer::SetLargestPacketNumber(&framer_, kPacketNumber - 2);
1661
1662 // clang-format off
1663 PacketFragments packet = {
1664 // public flags (8 byte connection_id and 2 byte packet number)
1665 {"Unable to read public flags.",
1666 {0x18}},
1667 // connection_id
1668 {"Unable to read ConnectionId.",
1669 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
1670 // packet number
1671 {"Unable to read packet number.",
1672 {0x56, 0x78}},
1673 };
1674
QUICHE teama6ef0a62019-03-07 20:34:33 -05001675 PacketFragments packet46 = {
1676 // type (short header, 2 byte packet number)
dschinazi48ac9192019-07-31 00:07:26 -07001677 {"Unable to read first byte.",
QUICHE teama6ef0a62019-03-07 20:34:33 -05001678 {0x41}},
1679 // connection_id
dschinazi48ac9192019-07-31 00:07:26 -07001680 {"Unable to read destination connection ID.",
QUICHE teama6ef0a62019-03-07 20:34:33 -05001681 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
1682 // packet number
1683 {"Unable to read packet number.",
1684 {0x56, 0x78}},
1685 };
nharper55fa6132019-05-07 19:37:21 -07001686
1687 PacketFragments packet_hp = {
1688 // type (short header, 2 byte packet number)
dschinazi48ac9192019-07-31 00:07:26 -07001689 {"Unable to read first byte.",
nharper55fa6132019-05-07 19:37:21 -07001690 {0x41}},
1691 // connection_id
dschinazi48ac9192019-07-31 00:07:26 -07001692 {"Unable to read destination connection ID.",
nharper55fa6132019-05-07 19:37:21 -07001693 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
1694 // packet number
1695 {"",
1696 {0x56, 0x78}},
1697 // padding
1698 {"", {0x00, 0x00}},
1699 };
QUICHE teama6ef0a62019-03-07 20:34:33 -05001700 // clang-format on
1701
1702 PacketFragments& fragments =
nharper55fa6132019-05-07 19:37:21 -07001703 framer_.version().HasHeaderProtection()
1704 ? packet_hp
fayang36825da2019-08-21 14:01:27 -07001705 : (framer_.transport_version() >= QUIC_VERSION_46 ? packet46
1706 : packet);
QUICHE teama6ef0a62019-03-07 20:34:33 -05001707 std::unique_ptr<QuicEncryptedPacket> encrypted(
1708 AssemblePacketFromFragments(fragments));
nharper55fa6132019-05-07 19:37:21 -07001709 if (framer_.version().HasHeaderProtection()) {
1710 EXPECT_TRUE(framer_.ProcessPacket(*encrypted));
bncf6f82b12019-10-30 07:01:01 -07001711 EXPECT_THAT(framer_.error(), IsQuicNoError());
nharper55fa6132019-05-07 19:37:21 -07001712 } else {
1713 EXPECT_FALSE(framer_.ProcessPacket(*encrypted));
bncf6f82b12019-10-30 07:01:01 -07001714 EXPECT_THAT(framer_.error(), IsError(QUIC_MISSING_PAYLOAD));
nharper55fa6132019-05-07 19:37:21 -07001715 }
QUICHE teama6ef0a62019-03-07 20:34:33 -05001716 ASSERT_TRUE(visitor_.header_.get());
1717 EXPECT_EQ(FramerTestConnectionId(),
1718 visitor_.header_->destination_connection_id);
1719 EXPECT_FALSE(visitor_.header_->reset_flag);
1720 EXPECT_FALSE(visitor_.header_->version_flag);
1721 EXPECT_EQ(PACKET_2BYTE_PACKET_NUMBER, visitor_.header_->packet_number_length);
1722 EXPECT_EQ(kPacketNumber, visitor_.header_->packet_number);
1723
1724 CheckFramingBoundaries(fragments, QUIC_INVALID_PACKET_HEADER);
1725}
1726
1727TEST_P(QuicFramerTest, PacketHeaderWith1BytePacketNumber) {
zhongyi546cc452019-04-12 15:27:49 -07001728 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
QUICHE teama6ef0a62019-03-07 20:34:33 -05001729 QuicFramerPeer::SetLargestPacketNumber(&framer_, kPacketNumber - 2);
1730
1731 // clang-format off
1732 PacketFragments packet = {
1733 // public flags (8 byte connection_id and 1 byte packet number)
1734 {"Unable to read public flags.",
1735 {0x08}},
1736 // connection_id
1737 {"Unable to read ConnectionId.",
1738 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
1739 // packet number
1740 {"Unable to read packet number.",
1741 {0x78}},
1742 };
1743
QUICHE teama6ef0a62019-03-07 20:34:33 -05001744 PacketFragments packet46 = {
1745 // type (8 byte connection_id and 1 byte packet number)
dschinazi48ac9192019-07-31 00:07:26 -07001746 {"Unable to read first byte.",
QUICHE teama6ef0a62019-03-07 20:34:33 -05001747 {0x40}},
1748 // connection_id
dschinazi48ac9192019-07-31 00:07:26 -07001749 {"Unable to read destination connection ID.",
QUICHE teama6ef0a62019-03-07 20:34:33 -05001750 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
1751 // packet number
1752 {"Unable to read packet number.",
1753 {0x78}},
1754 };
1755
nharper55fa6132019-05-07 19:37:21 -07001756 PacketFragments packet_hp = {
1757 // type (8 byte connection_id and 1 byte packet number)
dschinazi48ac9192019-07-31 00:07:26 -07001758 {"Unable to read first byte.",
nharper55fa6132019-05-07 19:37:21 -07001759 {0x40}},
1760 // connection_id
dschinazi48ac9192019-07-31 00:07:26 -07001761 {"Unable to read destination connection ID.",
nharper55fa6132019-05-07 19:37:21 -07001762 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
1763 // packet number
1764 {"",
1765 {0x78}},
1766 // padding
1767 {"", {0x00, 0x00, 0x00}},
1768 };
1769
QUICHE teama6ef0a62019-03-07 20:34:33 -05001770 // clang-format on
1771
1772 PacketFragments& fragments =
nharper55fa6132019-05-07 19:37:21 -07001773 framer_.version().HasHeaderProtection()
1774 ? packet_hp
fayang36825da2019-08-21 14:01:27 -07001775 : (framer_.transport_version() >= QUIC_VERSION_46 ? packet46
1776 : packet);
QUICHE teama6ef0a62019-03-07 20:34:33 -05001777 std::unique_ptr<QuicEncryptedPacket> encrypted(
1778 AssemblePacketFromFragments(fragments));
nharper55fa6132019-05-07 19:37:21 -07001779 if (framer_.version().HasHeaderProtection()) {
1780 EXPECT_TRUE(framer_.ProcessPacket(*encrypted));
bncf6f82b12019-10-30 07:01:01 -07001781 EXPECT_THAT(framer_.error(), IsQuicNoError());
nharper55fa6132019-05-07 19:37:21 -07001782 } else {
1783 EXPECT_FALSE(framer_.ProcessPacket(*encrypted));
bncf6f82b12019-10-30 07:01:01 -07001784 EXPECT_THAT(framer_.error(), IsError(QUIC_MISSING_PAYLOAD));
nharper55fa6132019-05-07 19:37:21 -07001785 }
QUICHE teama6ef0a62019-03-07 20:34:33 -05001786 ASSERT_TRUE(visitor_.header_.get());
1787 EXPECT_EQ(FramerTestConnectionId(),
1788 visitor_.header_->destination_connection_id);
1789 EXPECT_FALSE(visitor_.header_->reset_flag);
1790 EXPECT_FALSE(visitor_.header_->version_flag);
1791 EXPECT_EQ(PACKET_1BYTE_PACKET_NUMBER, visitor_.header_->packet_number_length);
1792 EXPECT_EQ(kPacketNumber, visitor_.header_->packet_number);
1793
1794 CheckFramingBoundaries(fragments, QUIC_INVALID_PACKET_HEADER);
1795}
1796
1797TEST_P(QuicFramerTest, PacketNumberDecreasesThenIncreases) {
zhongyi546cc452019-04-12 15:27:49 -07001798 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
QUICHE teama6ef0a62019-03-07 20:34:33 -05001799 // Test the case when a packet is received from the past and future packet
1800 // numbers are still calculated relative to the largest received packet.
1801 QuicPacketHeader header;
1802 header.destination_connection_id = FramerTestConnectionId();
1803 header.reset_flag = false;
1804 header.version_flag = false;
1805 header.packet_number = kPacketNumber - 2;
1806
1807 QuicFrames frames = {QuicFrame(QuicPaddingFrame())};
1808 QuicFramerPeer::SetPerspective(&framer_, Perspective::IS_CLIENT);
1809 std::unique_ptr<QuicPacket> data(BuildDataPacket(header, frames));
1810 ASSERT_TRUE(data != nullptr);
1811
1812 QuicEncryptedPacket encrypted(data->data(), data->length(), false);
1813 QuicFramerPeer::SetPerspective(&framer_, Perspective::IS_SERVER);
1814 EXPECT_TRUE(framer_.ProcessPacket(encrypted));
1815 ASSERT_TRUE(visitor_.header_.get());
1816 EXPECT_EQ(FramerTestConnectionId(),
1817 visitor_.header_->destination_connection_id);
1818 EXPECT_EQ(PACKET_4BYTE_PACKET_NUMBER, visitor_.header_->packet_number_length);
1819 EXPECT_EQ(kPacketNumber - 2, visitor_.header_->packet_number);
1820
1821 // Receive a 1 byte packet number.
1822 header.packet_number = kPacketNumber;
1823 header.packet_number_length = PACKET_1BYTE_PACKET_NUMBER;
1824 QuicFramerPeer::SetPerspective(&framer_, Perspective::IS_CLIENT);
1825 data = BuildDataPacket(header, frames);
1826 QuicEncryptedPacket encrypted1(data->data(), data->length(), false);
1827 QuicFramerPeer::SetPerspective(&framer_, Perspective::IS_SERVER);
1828 EXPECT_TRUE(framer_.ProcessPacket(encrypted1));
1829 ASSERT_TRUE(visitor_.header_.get());
1830 EXPECT_EQ(FramerTestConnectionId(),
1831 visitor_.header_->destination_connection_id);
1832 EXPECT_EQ(PACKET_1BYTE_PACKET_NUMBER, visitor_.header_->packet_number_length);
1833 EXPECT_EQ(kPacketNumber, visitor_.header_->packet_number);
1834
1835 // Process a 2 byte packet number 256 packets ago.
1836 header.packet_number = kPacketNumber - 256;
1837 header.packet_number_length = PACKET_2BYTE_PACKET_NUMBER;
1838 QuicFramerPeer::SetPerspective(&framer_, Perspective::IS_CLIENT);
1839 data = BuildDataPacket(header, frames);
1840 QuicEncryptedPacket encrypted2(data->data(), data->length(), false);
1841 QuicFramerPeer::SetPerspective(&framer_, Perspective::IS_SERVER);
1842 EXPECT_TRUE(framer_.ProcessPacket(encrypted2));
1843 ASSERT_TRUE(visitor_.header_.get());
1844 EXPECT_EQ(FramerTestConnectionId(),
1845 visitor_.header_->destination_connection_id);
1846 EXPECT_EQ(PACKET_2BYTE_PACKET_NUMBER, visitor_.header_->packet_number_length);
1847 EXPECT_EQ(kPacketNumber - 256, visitor_.header_->packet_number);
1848
1849 // Process another 1 byte packet number and ensure it works.
1850 header.packet_number = kPacketNumber - 1;
1851 header.packet_number_length = PACKET_1BYTE_PACKET_NUMBER;
1852 QuicFramerPeer::SetPerspective(&framer_, Perspective::IS_CLIENT);
1853 data = BuildDataPacket(header, frames);
1854 QuicEncryptedPacket encrypted3(data->data(), data->length(), false);
1855 QuicFramerPeer::SetPerspective(&framer_, Perspective::IS_SERVER);
1856 EXPECT_TRUE(framer_.ProcessPacket(encrypted3));
1857 ASSERT_TRUE(visitor_.header_.get());
1858 EXPECT_EQ(FramerTestConnectionId(),
1859 visitor_.header_->destination_connection_id);
1860 EXPECT_EQ(PACKET_1BYTE_PACKET_NUMBER, visitor_.header_->packet_number_length);
1861 EXPECT_EQ(kPacketNumber - 1, visitor_.header_->packet_number);
1862}
1863
1864TEST_P(QuicFramerTest, PacketWithDiversificationNonce) {
zhongyi546cc452019-04-12 15:27:49 -07001865 SetDecrypterLevel(ENCRYPTION_ZERO_RTT);
QUICHE teama6ef0a62019-03-07 20:34:33 -05001866 // clang-format off
1867 unsigned char packet[] = {
1868 // public flags: includes nonce flag
1869 0x2C,
1870 // connection_id
1871 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
1872 // nonce
1873 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
1874 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
1875 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
1876 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
1877 // packet number
1878 0x12, 0x34, 0x56, 0x78,
1879
1880 // frame type (padding)
1881 0x00,
1882 0x00, 0x00, 0x00, 0x00
1883 };
1884
QUICHE teama6ef0a62019-03-07 20:34:33 -05001885 unsigned char packet46[] = {
1886 // type: Long header with packet type ZERO_RTT_PROTECTED and 1 byte packet
1887 // number.
1888 0xD0,
1889 // version tag
1890 QUIC_VERSION_BYTES,
1891 // connection_id length
1892 0x05,
1893 // connection_id
1894 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
1895 // packet number
1896 0x78,
1897 // nonce
1898 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
1899 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
1900 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
1901 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
1902
1903 // frame type (padding)
1904 0x00,
1905 0x00, 0x00, 0x00, 0x00
1906 };
1907
dschinazic73506e2019-09-20 13:26:46 -07001908 unsigned char packet49[] = {
QUICHE teama6ef0a62019-03-07 20:34:33 -05001909 // type: Long header with packet type ZERO_RTT_PROTECTED and 1 byte packet
1910 // number.
1911 0xD0,
1912 // version tag
1913 QUIC_VERSION_BYTES,
dschinazi48ac9192019-07-31 00:07:26 -07001914 // destination connection ID length
1915 0x00,
1916 // source connection ID length
1917 0x08,
1918 // source connection ID
QUICHE teama6ef0a62019-03-07 20:34:33 -05001919 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
1920 // long header packet length
1921 0x26,
1922 // packet number
1923 0x78,
1924 // nonce
1925 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
1926 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
1927 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
1928 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
1929
1930 // frame type (padding)
1931 0x00,
1932 0x00, 0x00, 0x00, 0x00
1933 };
1934 // clang-format on
1935
1936 if (framer_.version().handshake_protocol != PROTOCOL_QUIC_CRYPTO) {
1937 return;
1938 }
1939
1940 unsigned char* p = packet;
bnc4e9283d2019-12-17 07:08:57 -08001941 size_t p_size = QUICHE_ARRAYSIZE(packet);
dschinazic73506e2019-09-20 13:26:46 -07001942 if (framer_.transport_version() >= QUIC_VERSION_49) {
1943 p = packet49;
bnc4e9283d2019-12-17 07:08:57 -08001944 p_size = QUICHE_ARRAYSIZE(packet49);
fayang36825da2019-08-21 14:01:27 -07001945 } else if (framer_.transport_version() >= QUIC_VERSION_46) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05001946 p = packet46;
bnc4e9283d2019-12-17 07:08:57 -08001947 p_size = QUICHE_ARRAYSIZE(packet46);
QUICHE teama6ef0a62019-03-07 20:34:33 -05001948 }
1949
1950 QuicEncryptedPacket encrypted(AsChars(p), p_size, false);
1951 QuicFramerPeer::SetPerspective(&framer_, Perspective::IS_CLIENT);
1952 EXPECT_TRUE(framer_.ProcessPacket(encrypted));
1953 ASSERT_TRUE(visitor_.header_->nonce != nullptr);
1954 for (char i = 0; i < 32; ++i) {
1955 EXPECT_EQ(i, (*visitor_.header_->nonce)[static_cast<size_t>(i)]);
1956 }
1957 EXPECT_EQ(1u, visitor_.padding_frames_.size());
1958 EXPECT_EQ(5, visitor_.padding_frames_[0]->num_padding_bytes);
1959}
1960
1961TEST_P(QuicFramerTest, LargePublicFlagWithMismatchedVersions) {
1962 // clang-format off
1963 unsigned char packet[] = {
1964 // public flags (8 byte connection_id, version flag and an unknown flag)
1965 0x29,
1966 // connection_id
1967 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
1968 // version tag
1969 'Q', '0', '0', '0',
1970 // packet number
1971 0x12, 0x34, 0x56, 0x78,
1972
1973 // frame type (padding frame)
1974 0x00,
1975 0x00, 0x00, 0x00, 0x00
1976 };
1977
fayang36825da2019-08-21 14:01:27 -07001978 unsigned char packet46[] = {
nharper55fa6132019-05-07 19:37:21 -07001979 // type (long header, ZERO_RTT_PROTECTED, 4-byte packet number)
1980 0xD3,
1981 // version tag
1982 'Q', '0', '0', '0',
1983 // connection_id length
1984 0x50,
1985 // connection_id
1986 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
1987 // packet number
1988 0x12, 0x34, 0x56, 0x78,
1989
1990 // frame type (padding frame)
1991 0x00,
1992 0x00, 0x00, 0x00, 0x00
1993 };
dschinazi48ac9192019-07-31 00:07:26 -07001994
dschinazic73506e2019-09-20 13:26:46 -07001995 unsigned char packet49[] = {
dschinazi48ac9192019-07-31 00:07:26 -07001996 // type (long header, ZERO_RTT_PROTECTED, 4-byte packet number)
1997 0xD3,
1998 // version tag
1999 'Q', '0', '0', '0',
2000 // destination connection ID length
2001 0x08,
2002 // destination connection ID
2003 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
2004 // source connection ID length
2005 0x00,
2006 // packet number
2007 0x12, 0x34, 0x56, 0x78,
2008
2009 // frame type (padding frame)
2010 0x00,
2011 0x00, 0x00, 0x00, 0x00
2012 };
QUICHE teama6ef0a62019-03-07 20:34:33 -05002013 // clang-format on
2014
nharper55fa6132019-05-07 19:37:21 -07002015 unsigned char* p = packet;
bnc4e9283d2019-12-17 07:08:57 -08002016 size_t p_size = QUICHE_ARRAYSIZE(packet);
dschinazic73506e2019-09-20 13:26:46 -07002017 if (framer_.transport_version() >= QUIC_VERSION_49) {
2018 p = packet49;
bnc4e9283d2019-12-17 07:08:57 -08002019 p_size = QUICHE_ARRAYSIZE(packet49);
fayang36825da2019-08-21 14:01:27 -07002020 } else if (framer_.transport_version() >= QUIC_VERSION_46) {
2021 p = packet46;
bnc4e9283d2019-12-17 07:08:57 -08002022 p_size = QUICHE_ARRAYSIZE(packet46);
nharper55fa6132019-05-07 19:37:21 -07002023 }
2024 QuicEncryptedPacket encrypted(AsChars(p), p_size, false);
QUICHE teama6ef0a62019-03-07 20:34:33 -05002025 EXPECT_TRUE(framer_.ProcessPacket(encrypted));
bncf6f82b12019-10-30 07:01:01 -07002026 EXPECT_THAT(framer_.error(), IsQuicNoError());
QUICHE teama6ef0a62019-03-07 20:34:33 -05002027 ASSERT_TRUE(visitor_.header_.get());
2028 EXPECT_EQ(0, visitor_.frame_count_);
2029 EXPECT_EQ(1, visitor_.version_mismatch_);
QUICHE teama6ef0a62019-03-07 20:34:33 -05002030}
2031
2032TEST_P(QuicFramerTest, PaddingFrame) {
zhongyi546cc452019-04-12 15:27:49 -07002033 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
QUICHE teama6ef0a62019-03-07 20:34:33 -05002034 // clang-format off
2035 unsigned char packet[] = {
2036 // public flags (8 byte connection_id)
2037 0x28,
2038 // connection_id
2039 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
2040 // packet number
2041 0x12, 0x34, 0x56, 0x78,
2042
2043 // paddings
2044 0x00, 0x00,
2045 // frame type (stream frame with fin)
2046 0xFF,
2047 // stream id
2048 0x01, 0x02, 0x03, 0x04,
2049 // offset
2050 0x3A, 0x98, 0xFE, 0xDC,
2051 0x32, 0x10, 0x76, 0x54,
2052 // data length
2053 0x00, 0x0c,
2054 // data
2055 'h', 'e', 'l', 'l',
2056 'o', ' ', 'w', 'o',
2057 'r', 'l', 'd', '!',
2058 // paddings
2059 0x00, 0x00,
2060 };
2061
QUICHE teama6ef0a62019-03-07 20:34:33 -05002062 unsigned char packet46[] = {
2063 // type (short header, 4 byte packet number)
2064 0x43,
2065 // connection_id
2066 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
2067 // packet number
2068 0x12, 0x34, 0x56, 0x78,
2069
2070 // paddings
2071 0x00, 0x00,
2072 // frame type (stream frame with fin)
2073 0xFF,
2074 // stream id
2075 0x01, 0x02, 0x03, 0x04,
2076 // offset
2077 0x3A, 0x98, 0xFE, 0xDC,
2078 0x32, 0x10, 0x76, 0x54,
2079 // data length
2080 0x00, 0x0c,
2081 // data
2082 'h', 'e', 'l', 'l',
2083 'o', ' ', 'w', 'o',
2084 'r', 'l', 'd', '!',
2085 // paddings
2086 0x00, 0x00,
2087 };
2088
2089 unsigned char packet99[] = {
2090 // type (short header, 4 byte packet number)
2091 0x43,
2092 // connection_id
2093 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
2094 // packet number
2095 0x12, 0x34, 0x56, 0x78,
2096
2097 // paddings
2098 0x00, 0x00,
2099 // frame type - IETF_STREAM with FIN, LEN, and OFFSET bits set.
2100 0x08 | 0x01 | 0x02 | 0x04,
2101
2102 // stream id
2103 kVarInt62FourBytes + 0x01, 0x02, 0x03, 0x04,
2104 // offset
2105 kVarInt62EightBytes + 0x3A, 0x98, 0xFE, 0xDC,
2106 0x32, 0x10, 0x76, 0x54,
2107 // data length
2108 kVarInt62OneByte + 0x0c,
2109 // data
2110 'h', 'e', 'l', 'l',
2111 'o', ' ', 'w', 'o',
2112 'r', 'l', 'd', '!',
2113 // paddings
2114 0x00, 0x00,
2115 };
2116 // clang-format on
2117
2118 unsigned char* p = packet;
bnc4e9283d2019-12-17 07:08:57 -08002119 size_t p_size = QUICHE_ARRAYSIZE(packet);
fkastenholz305e1732019-06-18 05:01:22 -07002120 if (VersionHasIetfQuicFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05002121 p = packet99;
bnc4e9283d2019-12-17 07:08:57 -08002122 p_size = QUICHE_ARRAYSIZE(packet99);
fayang36825da2019-08-21 14:01:27 -07002123 } else if (framer_.transport_version() >= QUIC_VERSION_46) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05002124 p = packet46;
bnc4e9283d2019-12-17 07:08:57 -08002125 p_size = QUICHE_ARRAYSIZE(packet46);
QUICHE teama6ef0a62019-03-07 20:34:33 -05002126 }
2127
2128 QuicEncryptedPacket encrypted(AsChars(p), p_size, false);
2129 EXPECT_TRUE(framer_.ProcessPacket(encrypted));
bncf6f82b12019-10-30 07:01:01 -07002130 EXPECT_THAT(framer_.error(), IsQuicNoError());
QUICHE teama6ef0a62019-03-07 20:34:33 -05002131 ASSERT_TRUE(visitor_.header_.get());
2132 EXPECT_TRUE(CheckDecryption(
2133 encrypted, !kIncludeVersion, !kIncludeDiversificationNonce,
2134 PACKET_8BYTE_CONNECTION_ID, PACKET_0BYTE_CONNECTION_ID));
2135
2136 ASSERT_EQ(1u, visitor_.stream_frames_.size());
2137 EXPECT_EQ(0u, visitor_.ack_frames_.size());
2138 EXPECT_EQ(2u, visitor_.padding_frames_.size());
2139 EXPECT_EQ(2, visitor_.padding_frames_[0]->num_padding_bytes);
2140 EXPECT_EQ(2, visitor_.padding_frames_[1]->num_padding_bytes);
2141 EXPECT_EQ(kStreamId, visitor_.stream_frames_[0]->stream_id);
2142 EXPECT_TRUE(visitor_.stream_frames_[0]->fin);
2143 EXPECT_EQ(kStreamOffset, visitor_.stream_frames_[0]->offset);
2144 CheckStreamFrameData("hello world!", visitor_.stream_frames_[0].get());
2145}
2146
2147TEST_P(QuicFramerTest, StreamFrame) {
zhongyi546cc452019-04-12 15:27:49 -07002148 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
QUICHE teama6ef0a62019-03-07 20:34:33 -05002149 // clang-format off
2150 PacketFragments packet = {
2151 // public flags (8 byte connection_id)
2152 {"",
2153 {0x28}},
2154 // connection_id
2155 {"",
2156 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
2157 // packet number
2158 {"",
2159 {0x12, 0x34, 0x56, 0x78}},
2160 // frame type (stream frame with fin)
2161 {"",
2162 {0xFF}},
2163 // stream id
2164 {"Unable to read stream_id.",
2165 {0x01, 0x02, 0x03, 0x04}},
2166 // offset
2167 {"Unable to read offset.",
2168 {0x3A, 0x98, 0xFE, 0xDC,
2169 0x32, 0x10, 0x76, 0x54}},
2170 {"Unable to read frame data.",
2171 {
2172 // data length
2173 0x00, 0x0c,
2174 // data
2175 'h', 'e', 'l', 'l',
2176 'o', ' ', 'w', 'o',
2177 'r', 'l', 'd', '!'}},
2178 };
2179
QUICHE teama6ef0a62019-03-07 20:34:33 -05002180 PacketFragments packet46 = {
2181 // type (short header, 4 byte packet number)
2182 {"",
2183 {0x43}},
2184 // connection_id
2185 {"",
2186 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
2187 // packet number
2188 {"",
2189 {0x12, 0x34, 0x56, 0x78}},
2190 // frame type (stream frame with fin)
2191 {"",
2192 {0xFF}},
2193 // stream id
2194 {"Unable to read stream_id.",
2195 {0x01, 0x02, 0x03, 0x04}},
2196 // offset
2197 {"Unable to read offset.",
2198 {0x3A, 0x98, 0xFE, 0xDC,
2199 0x32, 0x10, 0x76, 0x54}},
2200 {"Unable to read frame data.",
2201 {
2202 // data length
2203 0x00, 0x0c,
2204 // data
2205 'h', 'e', 'l', 'l',
2206 'o', ' ', 'w', 'o',
2207 'r', 'l', 'd', '!'}},
2208 };
2209
2210 PacketFragments packet99 = {
2211 // type (short header, 4 byte packet number)
2212 {"",
2213 {0x43}},
2214 // connection_id
2215 {"",
2216 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
2217 // packet number
2218 {"",
2219 {0x12, 0x34, 0x56, 0x78}},
2220 // frame type - IETF_STREAM with FIN, LEN, and OFFSET bits set.
2221 {"",
2222 { 0x08 | 0x01 | 0x02 | 0x04 }},
2223 // stream id
2224 {"Unable to read stream_id.",
2225 {kVarInt62FourBytes + 0x01, 0x02, 0x03, 0x04}},
2226 // offset
2227 {"Unable to read stream data offset.",
2228 {kVarInt62EightBytes + 0x3A, 0x98, 0xFE, 0xDC,
2229 0x32, 0x10, 0x76, 0x54}},
2230 // data length
2231 {"Unable to read stream data length.",
2232 {kVarInt62OneByte + 0x0c}},
2233 // data
2234 {"Unable to read frame data.",
2235 { 'h', 'e', 'l', 'l',
2236 'o', ' ', 'w', 'o',
2237 'r', 'l', 'd', '!'}},
2238 };
2239 // clang-format on
2240
2241 PacketFragments& fragments =
fkastenholz305e1732019-06-18 05:01:22 -07002242 VersionHasIetfQuicFrames(framer_.transport_version())
QUICHE teama6ef0a62019-03-07 20:34:33 -05002243 ? packet99
fayang36825da2019-08-21 14:01:27 -07002244 : (framer_.transport_version() >= QUIC_VERSION_46 ? packet46
2245 : packet);
QUICHE teama6ef0a62019-03-07 20:34:33 -05002246 std::unique_ptr<QuicEncryptedPacket> encrypted(
2247 AssemblePacketFromFragments(fragments));
2248 EXPECT_TRUE(framer_.ProcessPacket(*encrypted));
2249
bncf6f82b12019-10-30 07:01:01 -07002250 EXPECT_THAT(framer_.error(), IsQuicNoError());
QUICHE teama6ef0a62019-03-07 20:34:33 -05002251 ASSERT_TRUE(visitor_.header_.get());
2252 EXPECT_TRUE(CheckDecryption(
2253 *encrypted, !kIncludeVersion, !kIncludeDiversificationNonce,
2254 PACKET_8BYTE_CONNECTION_ID, PACKET_0BYTE_CONNECTION_ID));
2255
2256 ASSERT_EQ(1u, visitor_.stream_frames_.size());
2257 EXPECT_EQ(0u, visitor_.ack_frames_.size());
2258 EXPECT_EQ(kStreamId, visitor_.stream_frames_[0]->stream_id);
2259 EXPECT_TRUE(visitor_.stream_frames_[0]->fin);
2260 EXPECT_EQ(kStreamOffset, visitor_.stream_frames_[0]->offset);
2261 CheckStreamFrameData("hello world!", visitor_.stream_frames_[0].get());
2262
2263 CheckFramingBoundaries(fragments, QUIC_INVALID_STREAM_DATA);
2264}
2265
2266// Test an empty (no data) stream frame.
2267TEST_P(QuicFramerTest, EmptyStreamFrame) {
2268 // Only the IETF QUIC spec explicitly says that empty
2269 // stream frames are supported.
fkastenholz305e1732019-06-18 05:01:22 -07002270 if (!VersionHasIetfQuicFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05002271 return;
2272 }
zhongyi546cc452019-04-12 15:27:49 -07002273 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
QUICHE teama6ef0a62019-03-07 20:34:33 -05002274 // clang-format off
2275 PacketFragments packet = {
2276 // type (short header, 4 byte packet number)
2277 {"",
2278 {0x43}},
2279 // connection_id
2280 {"",
2281 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
2282 // packet number
2283 {"",
2284 {0x12, 0x34, 0x56, 0x78}},
2285 // frame type - IETF_STREAM with FIN, LEN, and OFFSET bits set.
2286 {"",
2287 { 0x08 | 0x01 | 0x02 | 0x04 }},
2288 // stream id
2289 {"Unable to read stream_id.",
2290 {kVarInt62FourBytes + 0x01, 0x02, 0x03, 0x04}},
2291 // offset
2292 {"Unable to read stream data offset.",
2293 {kVarInt62EightBytes + 0x3A, 0x98, 0xFE, 0xDC,
2294 0x32, 0x10, 0x76, 0x54}},
2295 // data length
2296 {"Unable to read stream data length.",
2297 {kVarInt62OneByte + 0x00}},
2298 };
2299 // clang-format on
2300
2301 std::unique_ptr<QuicEncryptedPacket> encrypted(
2302 AssemblePacketFromFragments(packet));
2303 EXPECT_TRUE(framer_.ProcessPacket(*encrypted));
2304
bncf6f82b12019-10-30 07:01:01 -07002305 EXPECT_THAT(framer_.error(), IsQuicNoError());
QUICHE teama6ef0a62019-03-07 20:34:33 -05002306 ASSERT_TRUE(visitor_.header_.get());
2307 EXPECT_TRUE(CheckDecryption(
2308 *encrypted, !kIncludeVersion, !kIncludeDiversificationNonce,
2309 PACKET_8BYTE_CONNECTION_ID, PACKET_0BYTE_CONNECTION_ID));
2310
2311 ASSERT_EQ(1u, visitor_.stream_frames_.size());
2312 EXPECT_EQ(0u, visitor_.ack_frames_.size());
2313 EXPECT_EQ(kStreamId, visitor_.stream_frames_[0]->stream_id);
2314 EXPECT_TRUE(visitor_.stream_frames_[0]->fin);
2315 EXPECT_EQ(kStreamOffset, visitor_.stream_frames_[0]->offset);
2316 EXPECT_EQ(visitor_.stream_frames_[0].get()->data_length, 0u);
2317
2318 CheckFramingBoundaries(packet, QUIC_INVALID_STREAM_DATA);
2319}
2320
2321TEST_P(QuicFramerTest, MissingDiversificationNonce) {
QUICHE team8e2e4532019-03-14 14:37:56 -07002322 if (framer_.version().handshake_protocol != PROTOCOL_QUIC_CRYPTO) {
2323 // TLS does not use diversification nonces.
2324 return;
2325 }
QUICHE teama6ef0a62019-03-07 20:34:33 -05002326 QuicFramerPeer::SetPerspective(&framer_, Perspective::IS_CLIENT);
QUICHE teama6ef0a62019-03-07 20:34:33 -05002327 decrypter_ = new test::TestDecrypter();
zhongyi546cc452019-04-12 15:27:49 -07002328 if (framer_.version().KnowsWhichDecrypterToUse()) {
vasilvv0fc587f2019-09-06 13:33:08 -07002329 framer_.InstallDecrypter(
2330 ENCRYPTION_INITIAL,
2331 std::make_unique<NullDecrypter>(Perspective::IS_CLIENT));
zhongyi546cc452019-04-12 15:27:49 -07002332 framer_.InstallDecrypter(ENCRYPTION_ZERO_RTT,
2333 std::unique_ptr<QuicDecrypter>(decrypter_));
2334 } else {
vasilvv0fc587f2019-09-06 13:33:08 -07002335 framer_.SetDecrypter(ENCRYPTION_INITIAL, std::make_unique<NullDecrypter>(
2336 Perspective::IS_CLIENT));
zhongyi546cc452019-04-12 15:27:49 -07002337 framer_.SetAlternativeDecrypter(
2338 ENCRYPTION_ZERO_RTT, std::unique_ptr<QuicDecrypter>(decrypter_), false);
2339 }
QUICHE teama6ef0a62019-03-07 20:34:33 -05002340
2341 // clang-format off
2342 unsigned char packet[] = {
2343 // public flags (8 byte connection_id)
2344 0x28,
2345 // connection_id
2346 0x10, 0x32, 0x54, 0x76, 0x98, 0xBA, 0xDC, 0xFE,
2347 // packet number
2348 0x12, 0x34, 0x56, 0x78,
QUICHE team8e2e4532019-03-14 14:37:56 -07002349 // padding frame
2350 0x00,
QUICHE teama6ef0a62019-03-07 20:34:33 -05002351 };
2352
QUICHE team8e2e4532019-03-14 14:37:56 -07002353 unsigned char packet46[] = {
2354 // type (long header, ZERO_RTT_PROTECTED, 4-byte packet number)
2355 0xD3,
2356 // version tag
2357 QUIC_VERSION_BYTES,
2358 // connection_id length
2359 0x05,
2360 // connection_id
2361 0x10, 0x32, 0x54, 0x76, 0x98, 0xBA, 0xDC, 0xFE,
2362 // packet number
2363 0x12, 0x34, 0x56, 0x78,
2364 // padding frame
2365 0x00,
2366 };
2367
dschinazic73506e2019-09-20 13:26:46 -07002368 unsigned char packet49[] = {
QUICHE team8e2e4532019-03-14 14:37:56 -07002369 // type (long header, ZERO_RTT_PROTECTED, 4-byte packet number)
2370 0xD3,
2371 // version tag
2372 QUIC_VERSION_BYTES,
dschinazi48ac9192019-07-31 00:07:26 -07002373 // destination connection ID length
2374 0x00,
2375 // source connection ID length
2376 0x08,
2377 // source connection ID
QUICHE team8e2e4532019-03-14 14:37:56 -07002378 0x10, 0x32, 0x54, 0x76, 0x98, 0xBA, 0xDC, 0xFE,
2379 // IETF long header payload length
2380 0x05,
2381 // packet number
2382 0x12, 0x34, 0x56, 0x78,
2383 // padding frame
2384 0x00,
QUICHE teama6ef0a62019-03-07 20:34:33 -05002385 };
2386 // clang-format on
2387
2388 unsigned char* p = packet;
bnc4e9283d2019-12-17 07:08:57 -08002389 size_t p_length = QUICHE_ARRAYSIZE(packet);
dschinazic73506e2019-09-20 13:26:46 -07002390 if (framer_.transport_version() >= QUIC_VERSION_49) {
2391 p = packet49;
bnc4e9283d2019-12-17 07:08:57 -08002392 p_length = QUICHE_ARRAYSIZE(packet49);
fayangf36e29d2019-06-06 14:03:40 -07002393 } else if (framer_.transport_version() >= QUIC_VERSION_46) {
QUICHE team8e2e4532019-03-14 14:37:56 -07002394 p = packet46;
bnc4e9283d2019-12-17 07:08:57 -08002395 p_length = QUICHE_ARRAYSIZE(packet46);
QUICHE teama6ef0a62019-03-07 20:34:33 -05002396 }
QUICHE team8e2e4532019-03-14 14:37:56 -07002397 QuicEncryptedPacket encrypted(AsChars(p), p_length, false);
QUICHE teama6ef0a62019-03-07 20:34:33 -05002398 EXPECT_FALSE(framer_.ProcessPacket(encrypted));
nharper55fa6132019-05-07 19:37:21 -07002399 if (framer_.version().HasHeaderProtection()) {
bncf6f82b12019-10-30 07:01:01 -07002400 EXPECT_THAT(framer_.error(), IsError(QUIC_DECRYPTION_FAILURE));
nharper55fa6132019-05-07 19:37:21 -07002401 EXPECT_EQ("Unable to decrypt header protection.", framer_.detailed_error());
fayang36825da2019-08-21 14:01:27 -07002402 } else if (framer_.transport_version() >= QUIC_VERSION_46) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05002403 // Cannot read diversification nonce.
bncf6f82b12019-10-30 07:01:01 -07002404 EXPECT_THAT(framer_.error(), IsError(QUIC_INVALID_PACKET_HEADER));
QUICHE team8e2e4532019-03-14 14:37:56 -07002405 EXPECT_EQ("Unable to read nonce.", framer_.detailed_error());
QUICHE teama6ef0a62019-03-07 20:34:33 -05002406 } else {
bncf6f82b12019-10-30 07:01:01 -07002407 EXPECT_THAT(framer_.error(), IsError(QUIC_DECRYPTION_FAILURE));
QUICHE teama6ef0a62019-03-07 20:34:33 -05002408 }
2409}
2410
2411TEST_P(QuicFramerTest, StreamFrame3ByteStreamId) {
2412 if (framer_.transport_version() > QUIC_VERSION_43) {
2413 // This test is nonsensical for IETF Quic.
2414 return;
2415 }
2416 // clang-format off
2417 PacketFragments packet = {
2418 // public flags (8 byte connection_id)
2419 {"",
2420 {0x28}},
2421 // connection_id
2422 {"",
2423 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
2424 // packet number
2425 {"",
2426 {0x12, 0x34, 0x56, 0x78}},
2427 // frame type (stream frame with fin)
2428 {"",
2429 {0xFE}},
2430 // stream id
2431 {"Unable to read stream_id.",
2432 {0x02, 0x03, 0x04}},
2433 // offset
2434 {"Unable to read offset.",
2435 {0x3A, 0x98, 0xFE, 0xDC,
2436 0x32, 0x10, 0x76, 0x54}},
2437 {"Unable to read frame data.",
2438 {
2439 // data length
2440 0x00, 0x0c,
2441 // data
2442 'h', 'e', 'l', 'l',
2443 'o', ' ', 'w', 'o',
2444 'r', 'l', 'd', '!'}},
2445 };
2446 // clang-format on
2447
2448 PacketFragments& fragments = packet;
2449 std::unique_ptr<QuicEncryptedPacket> encrypted(
2450 AssemblePacketFromFragments(fragments));
2451 EXPECT_TRUE(framer_.ProcessPacket(*encrypted));
2452
bncf6f82b12019-10-30 07:01:01 -07002453 EXPECT_THAT(framer_.error(), IsQuicNoError());
QUICHE teama6ef0a62019-03-07 20:34:33 -05002454 ASSERT_TRUE(visitor_.header_.get());
2455 EXPECT_TRUE(CheckDecryption(
2456 *encrypted, !kIncludeVersion, !kIncludeDiversificationNonce,
2457 PACKET_8BYTE_CONNECTION_ID, PACKET_0BYTE_CONNECTION_ID));
2458
2459 ASSERT_EQ(1u, visitor_.stream_frames_.size());
2460 EXPECT_EQ(0u, visitor_.ack_frames_.size());
2461 // Stream ID should be the last 3 bytes of kStreamId.
2462 EXPECT_EQ(0x00FFFFFF & kStreamId, visitor_.stream_frames_[0]->stream_id);
2463 EXPECT_TRUE(visitor_.stream_frames_[0]->fin);
2464 EXPECT_EQ(kStreamOffset, visitor_.stream_frames_[0]->offset);
2465 CheckStreamFrameData("hello world!", visitor_.stream_frames_[0].get());
2466
2467 CheckFramingBoundaries(fragments, QUIC_INVALID_STREAM_DATA);
2468}
2469
2470TEST_P(QuicFramerTest, StreamFrame2ByteStreamId) {
zhongyi546cc452019-04-12 15:27:49 -07002471 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
QUICHE teama6ef0a62019-03-07 20:34:33 -05002472 // clang-format off
2473 PacketFragments packet = {
2474 // public flags (8 byte connection_id)
2475 {"",
2476 {0x28}},
2477 // connection_id
2478 {"",
2479 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
2480 // packet number
2481 {"",
2482 {0x12, 0x34, 0x56, 0x78}},
2483 // frame type (stream frame with fin)
2484 {"",
2485 {0xFD}},
2486 // stream id
2487 {"Unable to read stream_id.",
2488 {0x03, 0x04}},
2489 // offset
2490 {"Unable to read offset.",
2491 {0x3A, 0x98, 0xFE, 0xDC,
2492 0x32, 0x10, 0x76, 0x54}},
2493 {"Unable to read frame data.",
2494 {
2495 // data length
2496 0x00, 0x0c,
2497 // data
2498 'h', 'e', 'l', 'l',
2499 'o', ' ', 'w', 'o',
2500 'r', 'l', 'd', '!'}},
2501 };
2502
QUICHE teama6ef0a62019-03-07 20:34:33 -05002503 PacketFragments packet46 = {
2504 // type (short header, 4 byte packet number)
2505 {"",
2506 {0x43}},
2507 // connection_id
2508 {"",
2509 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
2510 // packet number
2511 {"",
2512 {0x12, 0x34, 0x56, 0x78}},
2513 // frame type (stream frame with fin)
2514 {"",
2515 {0xFD}},
2516 // stream id
2517 {"Unable to read stream_id.",
2518 {0x03, 0x04}},
2519 // offset
2520 {"Unable to read offset.",
2521 {0x3A, 0x98, 0xFE, 0xDC,
2522 0x32, 0x10, 0x76, 0x54}},
2523 {"Unable to read frame data.",
2524 {
2525 // data length
2526 0x00, 0x0c,
2527 // data
2528 'h', 'e', 'l', 'l',
2529 'o', ' ', 'w', 'o',
2530 'r', 'l', 'd', '!'}},
2531 };
2532
2533 PacketFragments packet99 = {
2534 // type (short header, 4 byte packet number)
2535 {"",
2536 {0x43}},
2537 // connection_id
2538 {"",
2539 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
2540 // packet number
2541 {"",
2542 {0x12, 0x34, 0x56, 0x78}},
2543 // frame type (IETF_STREAM frame with LEN, FIN, and OFFSET bits set)
2544 {"",
2545 {0x08 | 0x01 | 0x02 | 0x04}},
2546 // stream id
2547 {"Unable to read stream_id.",
2548 {kVarInt62TwoBytes + 0x03, 0x04}},
2549 // offset
2550 {"Unable to read stream data offset.",
2551 {kVarInt62EightBytes + 0x3A, 0x98, 0xFE, 0xDC,
2552 0x32, 0x10, 0x76, 0x54}},
2553 // data length
2554 {"Unable to read stream data length.",
2555 {kVarInt62OneByte + 0x0c}},
2556 // data
2557 {"Unable to read frame data.",
2558 { 'h', 'e', 'l', 'l',
2559 'o', ' ', 'w', 'o',
2560 'r', 'l', 'd', '!'}},
2561 };
2562 // clang-format on
2563
2564 PacketFragments& fragments =
fkastenholz305e1732019-06-18 05:01:22 -07002565 VersionHasIetfQuicFrames(framer_.transport_version())
QUICHE teama6ef0a62019-03-07 20:34:33 -05002566 ? packet99
fayang36825da2019-08-21 14:01:27 -07002567 : (framer_.transport_version() >= QUIC_VERSION_46 ? packet46
2568 : packet);
QUICHE teama6ef0a62019-03-07 20:34:33 -05002569 std::unique_ptr<QuicEncryptedPacket> encrypted(
2570 AssemblePacketFromFragments(fragments));
2571 EXPECT_TRUE(framer_.ProcessPacket(*encrypted));
2572
bncf6f82b12019-10-30 07:01:01 -07002573 EXPECT_THAT(framer_.error(), IsQuicNoError());
QUICHE teama6ef0a62019-03-07 20:34:33 -05002574 ASSERT_TRUE(visitor_.header_.get());
2575 EXPECT_TRUE(CheckDecryption(
2576 *encrypted, !kIncludeVersion, !kIncludeDiversificationNonce,
2577 PACKET_8BYTE_CONNECTION_ID, PACKET_0BYTE_CONNECTION_ID));
2578
2579 ASSERT_EQ(1u, visitor_.stream_frames_.size());
2580 EXPECT_EQ(0u, visitor_.ack_frames_.size());
2581 // Stream ID should be the last 2 bytes of kStreamId.
2582 EXPECT_EQ(0x0000FFFF & kStreamId, visitor_.stream_frames_[0]->stream_id);
2583 EXPECT_TRUE(visitor_.stream_frames_[0]->fin);
2584 EXPECT_EQ(kStreamOffset, visitor_.stream_frames_[0]->offset);
2585 CheckStreamFrameData("hello world!", visitor_.stream_frames_[0].get());
2586
2587 CheckFramingBoundaries(fragments, QUIC_INVALID_STREAM_DATA);
2588}
2589
2590TEST_P(QuicFramerTest, StreamFrame1ByteStreamId) {
zhongyi546cc452019-04-12 15:27:49 -07002591 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
QUICHE teama6ef0a62019-03-07 20:34:33 -05002592 // clang-format off
2593 PacketFragments packet = {
2594 // public flags (8 byte connection_id)
2595 {"",
2596 {0x28}},
2597 // connection_id
2598 {"",
2599 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
2600 // packet number
2601 {"",
2602 {0x12, 0x34, 0x56, 0x78}},
2603 // frame type (stream frame with fin)
2604 {"",
2605 {0xFC}},
2606 // stream id
2607 {"Unable to read stream_id.",
2608 {0x04}},
2609 // offset
2610 {"Unable to read offset.",
2611 {0x3A, 0x98, 0xFE, 0xDC,
2612 0x32, 0x10, 0x76, 0x54}},
2613 {"Unable to read frame data.",
2614 {
2615 // data length
2616 0x00, 0x0c,
2617 // data
2618 'h', 'e', 'l', 'l',
2619 'o', ' ', 'w', 'o',
2620 'r', 'l', 'd', '!'}},
2621 };
2622
QUICHE teama6ef0a62019-03-07 20:34:33 -05002623 PacketFragments packet46 = {
2624 // type (short header, 4 byte packet number)
2625 {"",
2626 {0x43}},
2627 // connection_id
2628 {"",
2629 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
2630 // packet number
2631 {"",
2632 {0x12, 0x34, 0x56, 0x78}},
2633 // frame type (stream frame with fin)
2634 {"",
2635 {0xFC}},
2636 // stream id
2637 {"Unable to read stream_id.",
2638 {0x04}},
2639 // offset
2640 {"Unable to read offset.",
2641 {0x3A, 0x98, 0xFE, 0xDC,
2642 0x32, 0x10, 0x76, 0x54}},
2643 {"Unable to read frame data.",
2644 {
2645 // data length
2646 0x00, 0x0c,
2647 // data
2648 'h', 'e', 'l', 'l',
2649 'o', ' ', 'w', 'o',
2650 'r', 'l', 'd', '!'}},
2651 };
2652
2653 PacketFragments packet99 = {
2654 // type (short header, 4 byte packet number)
2655 {"",
2656 {0x43}},
2657 // connection_id
2658 {"",
2659 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
2660 // packet number
2661 {"",
2662 {0x12, 0x34, 0x56, 0x78}},
2663 // frame type (IETF_STREAM frame with LEN, FIN, and OFFSET bits set)
2664 {"",
2665 {0x08 | 0x01 | 0x02 | 0x04}},
2666 // stream id
2667 {"Unable to read stream_id.",
2668 {kVarInt62OneByte + 0x04}},
2669 // offset
2670 {"Unable to read stream data offset.",
2671 {kVarInt62EightBytes + 0x3A, 0x98, 0xFE, 0xDC,
2672 0x32, 0x10, 0x76, 0x54}},
2673 // data length
2674 {"Unable to read stream data length.",
2675 {kVarInt62OneByte + 0x0c}},
2676 // data
2677 {"Unable to read frame data.",
2678 { 'h', 'e', 'l', 'l',
2679 'o', ' ', 'w', 'o',
2680 'r', 'l', 'd', '!'}},
2681 };
2682 // clang-format on
2683
2684 PacketFragments& fragments =
fkastenholz305e1732019-06-18 05:01:22 -07002685 VersionHasIetfQuicFrames(framer_.transport_version())
QUICHE teama6ef0a62019-03-07 20:34:33 -05002686 ? packet99
fayang36825da2019-08-21 14:01:27 -07002687 : (framer_.transport_version() >= QUIC_VERSION_46 ? packet46
2688 : packet);
QUICHE teama6ef0a62019-03-07 20:34:33 -05002689 std::unique_ptr<QuicEncryptedPacket> encrypted(
2690 AssemblePacketFromFragments(fragments));
2691 EXPECT_TRUE(framer_.ProcessPacket(*encrypted));
2692
bncf6f82b12019-10-30 07:01:01 -07002693 EXPECT_THAT(framer_.error(), IsQuicNoError());
QUICHE teama6ef0a62019-03-07 20:34:33 -05002694 ASSERT_TRUE(visitor_.header_.get());
2695 EXPECT_TRUE(CheckDecryption(
2696 *encrypted, !kIncludeVersion, !kIncludeDiversificationNonce,
2697 PACKET_8BYTE_CONNECTION_ID, PACKET_0BYTE_CONNECTION_ID));
2698
2699 ASSERT_EQ(1u, visitor_.stream_frames_.size());
2700 EXPECT_EQ(0u, visitor_.ack_frames_.size());
2701 // Stream ID should be the last 1 byte of kStreamId.
2702 EXPECT_EQ(0x000000FF & kStreamId, visitor_.stream_frames_[0]->stream_id);
2703 EXPECT_TRUE(visitor_.stream_frames_[0]->fin);
2704 EXPECT_EQ(kStreamOffset, visitor_.stream_frames_[0]->offset);
2705 CheckStreamFrameData("hello world!", visitor_.stream_frames_[0].get());
2706
2707 CheckFramingBoundaries(fragments, QUIC_INVALID_STREAM_DATA);
2708}
2709
2710TEST_P(QuicFramerTest, StreamFrameWithVersion) {
fkastenholz305e1732019-06-18 05:01:22 -07002711 // If IETF frames are in use then we must also have the IETF
2712 // header invariants.
2713 if (VersionHasIetfQuicFrames(framer_.transport_version())) {
2714 DCHECK(VersionHasIetfInvariantHeader(framer_.transport_version()));
2715 }
2716
zhongyi546cc452019-04-12 15:27:49 -07002717 SetDecrypterLevel(ENCRYPTION_ZERO_RTT);
QUICHE teama6ef0a62019-03-07 20:34:33 -05002718 // clang-format off
2719 PacketFragments packet = {
2720 // public flags (version, 8 byte connection_id)
2721 {"",
2722 {0x29}},
2723 // connection_id
2724 {"",
2725 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
2726 // version tag
2727 {"",
2728 {QUIC_VERSION_BYTES}},
2729 // packet number
2730 {"",
2731 {0x12, 0x34, 0x56, 0x78}},
2732 // frame type (stream frame with fin)
2733 {"",
2734 {0xFE}},
2735 // stream id
2736 {"Unable to read stream_id.",
2737 {0x02, 0x03, 0x04}},
2738 // offset
2739 {"Unable to read offset.",
2740 {0x3A, 0x98, 0xFE, 0xDC,
2741 0x32, 0x10, 0x76, 0x54}},
2742 {"Unable to read frame data.",
2743 {
2744 // data length
2745 0x00, 0x0c,
2746 // data
2747 'h', 'e', 'l', 'l',
2748 'o', ' ', 'w', 'o',
2749 'r', 'l', 'd', '!'}},
2750 };
2751
QUICHE teama6ef0a62019-03-07 20:34:33 -05002752 PacketFragments packet46 = {
2753 // public flags (long header with packet type ZERO_RTT_PROTECTED and
2754 // 4-byte packet number)
2755 {"",
2756 {0xD3}},
2757 // version tag
2758 {"",
2759 {QUIC_VERSION_BYTES}},
2760 // connection_id length
2761 {"",
2762 {0x50}},
2763 // connection_id
2764 {"",
2765 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
2766 // packet number
2767 {"",
2768 {0x12, 0x34, 0x56, 0x78}},
2769 // frame type (stream frame with fin)
2770 {"",
2771 {0xFE}},
2772 // stream id
2773 {"Unable to read stream_id.",
2774 {0x02, 0x03, 0x04}},
2775 // offset
2776 {"Unable to read offset.",
2777 {0x3A, 0x98, 0xFE, 0xDC,
2778 0x32, 0x10, 0x76, 0x54}},
2779 {"Unable to read frame data.",
2780 {
2781 // data length
2782 0x00, 0x0c,
2783 // data
2784 'h', 'e', 'l', 'l',
2785 'o', ' ', 'w', 'o',
2786 'r', 'l', 'd', '!'}},
2787 };
2788
dschinazic73506e2019-09-20 13:26:46 -07002789 PacketFragments packet49 = {
2790 // public flags (long header with packet type ZERO_RTT_PROTECTED and
2791 // 4-byte packet number)
2792 {"",
2793 {0xD3}},
2794 // version tag
2795 {"",
2796 {QUIC_VERSION_BYTES}},
2797 // destination connection ID length
2798 {"",
2799 {0x08}},
2800 // destination connection ID
2801 {"",
2802 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
2803 // source connection ID length
2804 {"",
2805 {0x00}},
2806 // long header packet length
2807 {"",
2808 {0x1E}},
2809 // packet number
2810 {"",
2811 {0x12, 0x34, 0x56, 0x78}},
2812 // frame type (stream frame with fin)
2813 {"",
2814 {0xFE}},
2815 // stream id
2816 {"Long header payload length longer than packet.",
2817 {0x02, 0x03, 0x04}},
2818 // offset
2819 {"Long header payload length longer than packet.",
2820 {0x3A, 0x98, 0xFE, 0xDC,
2821 0x32, 0x10, 0x76, 0x54}},
2822 {"Long header payload length longer than packet.",
2823 {
2824 // data length
2825 0x00, 0x0c,
2826 // data
2827 'h', 'e', 'l', 'l',
2828 'o', ' ', 'w', 'o',
2829 'r', 'l', 'd', '!'}},
2830 };
2831
QUICHE teama6ef0a62019-03-07 20:34:33 -05002832 PacketFragments packet99 = {
2833 // public flags (long header with packet type ZERO_RTT_PROTECTED and
2834 // 4-byte packet number)
2835 {"",
2836 {0xD3}},
2837 // version tag
2838 {"",
2839 {QUIC_VERSION_BYTES}},
dschinazi48ac9192019-07-31 00:07:26 -07002840 // destination connection ID length
QUICHE teama6ef0a62019-03-07 20:34:33 -05002841 {"",
dschinazi48ac9192019-07-31 00:07:26 -07002842 {0x08}},
2843 // destination connection ID
QUICHE teama6ef0a62019-03-07 20:34:33 -05002844 {"",
2845 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
dschinazi48ac9192019-07-31 00:07:26 -07002846 // source connection ID length
2847 {"",
2848 {0x00}},
QUICHE teama6ef0a62019-03-07 20:34:33 -05002849 // long header packet length
2850 {"",
2851 {0x1E}},
2852 // packet number
2853 {"",
2854 {0x12, 0x34, 0x56, 0x78}},
2855 // frame type (IETF_STREAM frame with FIN, LEN, and OFFSET bits set)
2856 {"",
2857 {0x08 | 0x01 | 0x02 | 0x04}},
2858 // stream id
2859 {"Long header payload length longer than packet.",
2860 {kVarInt62FourBytes + 0x00, 0x02, 0x03, 0x04}},
2861 // offset
2862 {"Long header payload length longer than packet.",
2863 {kVarInt62EightBytes + 0x3A, 0x98, 0xFE, 0xDC,
2864 0x32, 0x10, 0x76, 0x54}},
2865 // data length
2866 {"Long header payload length longer than packet.",
2867 {kVarInt62OneByte + 0x0c}},
2868 // data
2869 {"Long header payload length longer than packet.",
2870 { 'h', 'e', 'l', 'l',
2871 'o', ' ', 'w', 'o',
2872 'r', 'l', 'd', '!'}},
2873 };
2874 // clang-format on
2875
2876 QuicVariableLengthIntegerLength retry_token_length_length =
2877 VARIABLE_LENGTH_INTEGER_LENGTH_0;
2878 size_t retry_token_length = 0;
2879 QuicVariableLengthIntegerLength length_length =
2880 QuicVersionHasLongHeaderLengths(framer_.transport_version())
2881 ? VARIABLE_LENGTH_INTEGER_LENGTH_1
2882 : VARIABLE_LENGTH_INTEGER_LENGTH_0;
2883
2884 PacketFragments& fragments =
fkastenholz305e1732019-06-18 05:01:22 -07002885 VersionHasIetfQuicFrames(framer_.transport_version())
QUICHE teama6ef0a62019-03-07 20:34:33 -05002886 ? packet99
dschinazic73506e2019-09-20 13:26:46 -07002887 : (framer_.transport_version() >= QUIC_VERSION_49
2888 ? packet49
2889 : (framer_.transport_version() >= QUIC_VERSION_46 ? packet46
2890 : packet));
QUICHE teama6ef0a62019-03-07 20:34:33 -05002891 std::unique_ptr<QuicEncryptedPacket> encrypted(
2892 AssemblePacketFromFragments(fragments));
2893 EXPECT_TRUE(framer_.ProcessPacket(*encrypted));
2894
bncf6f82b12019-10-30 07:01:01 -07002895 EXPECT_THAT(framer_.error(), IsQuicNoError());
QUICHE teama6ef0a62019-03-07 20:34:33 -05002896 ASSERT_TRUE(visitor_.header_.get());
2897 EXPECT_TRUE(CheckDecryption(
2898 *encrypted, kIncludeVersion, !kIncludeDiversificationNonce,
2899 PACKET_8BYTE_CONNECTION_ID, PACKET_0BYTE_CONNECTION_ID,
2900 retry_token_length_length, retry_token_length, length_length));
2901
2902 ASSERT_EQ(1u, visitor_.stream_frames_.size());
2903 EXPECT_EQ(0u, visitor_.ack_frames_.size());
2904 // Stream ID should be the last 3 bytes of kStreamId.
2905 EXPECT_EQ(0x00FFFFFF & kStreamId, visitor_.stream_frames_[0]->stream_id);
2906 EXPECT_TRUE(visitor_.stream_frames_[0]->fin);
2907 EXPECT_EQ(kStreamOffset, visitor_.stream_frames_[0]->offset);
2908 CheckStreamFrameData("hello world!", visitor_.stream_frames_[0].get());
2909
2910 CheckFramingBoundaries(fragments,
dschinazic73506e2019-09-20 13:26:46 -07002911 framer_.transport_version() >= QUIC_VERSION_49
QUICHE teama6ef0a62019-03-07 20:34:33 -05002912 ? QUIC_INVALID_PACKET_HEADER
2913 : QUIC_INVALID_STREAM_DATA);
2914}
2915
2916TEST_P(QuicFramerTest, RejectPacket) {
zhongyi546cc452019-04-12 15:27:49 -07002917 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
QUICHE teama6ef0a62019-03-07 20:34:33 -05002918 visitor_.accept_packet_ = false;
2919
2920 // clang-format off
2921 unsigned char packet[] = {
2922 // public flags (8 byte connection_id)
2923 0x28,
2924 // connection_id
2925 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
2926 // packet number
2927 0x12, 0x34, 0x56, 0x78,
2928
2929 // frame type (stream frame with fin)
2930 0xFF,
2931 // stream id
2932 0x01, 0x02, 0x03, 0x04,
2933 // offset
2934 0x3A, 0x98, 0xFE, 0xDC,
2935 0x32, 0x10, 0x76, 0x54,
2936 // data length
2937 0x00, 0x0c,
2938 // data
2939 'h', 'e', 'l', 'l',
2940 'o', ' ', 'w', 'o',
2941 'r', 'l', 'd', '!',
2942 };
2943
QUICHE teama6ef0a62019-03-07 20:34:33 -05002944 unsigned char packet46[] = {
2945 // type (short header, 4 byte packet number)
2946 0x43,
2947 // connection_id
2948 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
2949 // packet number
2950 0x12, 0x34, 0x56, 0x78,
2951
2952 // frame type (STREAM Frame with FIN, LEN, and OFFSET bits set)
2953 0x10 | 0x01 | 0x02 | 0x04,
2954 // stream id
2955 kVarInt62FourBytes + 0x01, 0x02, 0x03, 0x04,
2956 // offset
2957 kVarInt62EightBytes + 0x3A, 0x98, 0xFE, 0xDC,
2958 0x32, 0x10, 0x76, 0x54,
2959 // data length
2960 kVarInt62OneByte + 0x0c,
2961 // data
2962 'h', 'e', 'l', 'l',
2963 'o', ' ', 'w', 'o',
2964 'r', 'l', 'd', '!',
2965 };
2966 // clang-format on
2967
2968 unsigned char* p = packet;
fayang36825da2019-08-21 14:01:27 -07002969 if (framer_.transport_version() >= QUIC_VERSION_46) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05002970 p = packet46;
QUICHE teama6ef0a62019-03-07 20:34:33 -05002971 }
2972 QuicEncryptedPacket encrypted(AsChars(p),
2973 framer_.transport_version() > QUIC_VERSION_43
bnc4e9283d2019-12-17 07:08:57 -08002974 ? QUICHE_ARRAYSIZE(packet46)
2975 : QUICHE_ARRAYSIZE(packet),
QUICHE teama6ef0a62019-03-07 20:34:33 -05002976 false);
2977 EXPECT_TRUE(framer_.ProcessPacket(encrypted));
2978
bncf6f82b12019-10-30 07:01:01 -07002979 EXPECT_THAT(framer_.error(), IsQuicNoError());
QUICHE teama6ef0a62019-03-07 20:34:33 -05002980 ASSERT_TRUE(visitor_.header_.get());
2981 EXPECT_TRUE(CheckDecryption(
2982 encrypted, !kIncludeVersion, !kIncludeDiversificationNonce,
2983 PACKET_8BYTE_CONNECTION_ID, PACKET_0BYTE_CONNECTION_ID));
2984
2985 ASSERT_EQ(0u, visitor_.stream_frames_.size());
2986 EXPECT_EQ(0u, visitor_.ack_frames_.size());
2987}
2988
2989TEST_P(QuicFramerTest, RejectPublicHeader) {
2990 visitor_.accept_public_header_ = false;
2991
2992 // clang-format off
2993 unsigned char packet[] = {
2994 // public flags (8 byte connection_id)
2995 0x28,
2996 // connection_id
2997 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
2998 };
2999
QUICHE teama6ef0a62019-03-07 20:34:33 -05003000 unsigned char packet46[] = {
3001 // type (short header, 1 byte packet number)
3002 0x40,
3003 // connection_id
3004 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
3005 // packet number
3006 0x01,
3007 };
3008 // clang-format on
3009
bnc4e9283d2019-12-17 07:08:57 -08003010 QuicEncryptedPacket encrypted(framer_.transport_version() >= QUIC_VERSION_46
3011 ? AsChars(packet46)
3012 : AsChars(packet),
3013 framer_.transport_version() >= QUIC_VERSION_46
3014 ? QUICHE_ARRAYSIZE(packet46)
3015 : QUICHE_ARRAYSIZE(packet),
3016 false);
QUICHE teama6ef0a62019-03-07 20:34:33 -05003017 EXPECT_TRUE(framer_.ProcessPacket(encrypted));
3018
bncf6f82b12019-10-30 07:01:01 -07003019 EXPECT_THAT(framer_.error(), IsQuicNoError());
QUICHE teama6ef0a62019-03-07 20:34:33 -05003020 ASSERT_TRUE(visitor_.header_.get());
3021 EXPECT_FALSE(visitor_.header_->packet_number.IsInitialized());
3022}
3023
3024TEST_P(QuicFramerTest, AckFrameOneAckBlock) {
zhongyi546cc452019-04-12 15:27:49 -07003025 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
QUICHE teama6ef0a62019-03-07 20:34:33 -05003026 // clang-format off
3027 PacketFragments packet = {
3028 // public flags (8 byte connection_id)
3029 {"",
3030 {0x2C}},
3031 // connection_id
3032 {"",
3033 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
3034 // packet number
3035 {"",
3036 {0x12, 0x34, 0x56, 0x78}},
3037 // frame type (ack frame)
3038 // (one ack block, 2 byte largest observed, 2 byte block length)
3039 {"",
3040 {0x45}},
3041 // largest acked
3042 {"Unable to read largest acked.",
3043 {0x12, 0x34}},
3044 // Zero delta time.
3045 {"Unable to read ack delay time.",
3046 {0x00, 0x00}},
3047 // first ack block length.
3048 {"Unable to read first ack block length.",
3049 {0x12, 0x34}},
3050 // num timestamps.
3051 {"Unable to read num received packets.",
3052 {0x00}}
3053 };
3054
QUICHE teama6ef0a62019-03-07 20:34:33 -05003055 PacketFragments packet46 = {
3056 // type (short packet, 4 byte packet number)
3057 {"",
3058 {0x43}},
3059 // connection_id
3060 {"",
3061 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
3062 // packet number
3063 {"",
3064 {0x12, 0x34, 0x56, 0x78}},
3065 // frame type (ack frame)
3066 // (one ack block, 2 byte largest observed, 2 byte block length)
3067 {"",
3068 {0x45}},
3069 // largest acked
3070 {"Unable to read largest acked.",
3071 {0x12, 0x34}},
3072 // Zero delta time.
3073 {"Unable to read ack delay time.",
3074 {0x00, 0x00}},
3075 // first ack block length.
3076 {"Unable to read first ack block length.",
3077 {0x12, 0x34}},
3078 // num timestamps.
3079 {"Unable to read num received packets.",
3080 {0x00}}
3081 };
3082
3083 PacketFragments packet99 = {
3084 // type (short packet, 4 byte packet number)
3085 {"",
3086 {0x43}},
3087 // connection_id
3088 {"",
3089 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
3090 // packet number
3091 {"",
3092 {0x12, 0x34, 0x56, 0x78}},
3093 // frame type (IETF_ACK)
3094 // (one ack block, 2 byte largest observed, 2 byte block length)
3095 // IETF-Quic ignores the bit-fields in the ack type, all of
3096 // that information is encoded elsewhere in the frame.
3097 {"",
3098 {0x02}},
3099 // largest acked
3100 {"Unable to read largest acked.",
3101 {kVarInt62TwoBytes + 0x12, 0x34}},
3102 // Zero delta time.
3103 {"Unable to read ack delay time.",
3104 {kVarInt62OneByte + 0x00}},
3105 // Ack block count (0 -- no blocks after the first)
3106 {"Unable to read ack block count.",
3107 {kVarInt62OneByte + 0x00}},
3108 // first ack block length - 1.
3109 // IETF Quic defines the ack block's value as the "number of
3110 // packets that preceed the largest packet number in the block"
3111 // which for the 1st ack block is the largest acked field,
3112 // above. This means that if we are acking just packet 0x1234
3113 // then the 1st ack block will be 0.
3114 {"Unable to read first ack block length.",
3115 {kVarInt62TwoBytes + 0x12, 0x33}}
3116 };
3117 // clang-format on
3118
3119 PacketFragments& fragments =
fkastenholz305e1732019-06-18 05:01:22 -07003120 VersionHasIetfQuicFrames(framer_.transport_version())
QUICHE teama6ef0a62019-03-07 20:34:33 -05003121 ? packet99
fayang36825da2019-08-21 14:01:27 -07003122 : (framer_.transport_version() >= QUIC_VERSION_46 ? packet46
3123 : packet);
QUICHE teama6ef0a62019-03-07 20:34:33 -05003124 std::unique_ptr<QuicEncryptedPacket> encrypted(
3125 AssemblePacketFromFragments(fragments));
3126 EXPECT_TRUE(framer_.ProcessPacket(*encrypted));
3127
bncf6f82b12019-10-30 07:01:01 -07003128 EXPECT_THAT(framer_.error(), IsQuicNoError());
QUICHE teama6ef0a62019-03-07 20:34:33 -05003129 ASSERT_TRUE(visitor_.header_.get());
3130 EXPECT_TRUE(CheckDecryption(
3131 *encrypted, !kIncludeVersion, !kIncludeDiversificationNonce,
3132 PACKET_8BYTE_CONNECTION_ID, PACKET_0BYTE_CONNECTION_ID));
3133
3134 EXPECT_EQ(0u, visitor_.stream_frames_.size());
3135 ASSERT_EQ(1u, visitor_.ack_frames_.size());
3136 const QuicAckFrame& frame = *visitor_.ack_frames_[0];
3137 EXPECT_EQ(kSmallLargestObserved, LargestAcked(frame));
3138 ASSERT_EQ(4660u, frame.packets.NumPacketsSlow());
3139
3140 CheckFramingBoundaries(fragments, QUIC_INVALID_ACK_DATA);
3141}
3142
3143// This test checks that the ack frame processor correctly identifies
3144// and handles the case where the first ack block is larger than the
3145// largest_acked packet.
3146TEST_P(QuicFramerTest, FirstAckFrameUnderflow) {
zhongyi546cc452019-04-12 15:27:49 -07003147 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
QUICHE teama6ef0a62019-03-07 20:34:33 -05003148 // clang-format off
3149 PacketFragments packet = {
3150 // public flags (8 byte connection_id)
3151 {"",
3152 {0x2C}},
3153 // connection_id
3154 {"",
3155 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
3156 // packet number
3157 {"",
3158 {0x12, 0x34, 0x56, 0x78}},
3159 // frame type (ack frame)
3160 // (one ack block, 2 byte largest observed, 2 byte block length)
3161 {"",
3162 {0x45}},
3163 // largest acked
3164 {"Unable to read largest acked.",
3165 {0x12, 0x34}},
3166 // Zero delta time.
3167 {"Unable to read ack delay time.",
3168 {0x00, 0x00}},
3169 // first ack block length.
3170 {"Unable to read first ack block length.",
3171 {0x88, 0x88}},
3172 // num timestamps.
3173 {"Underflow with first ack block length 34952 largest acked is 4660.",
3174 {0x00}}
3175 };
3176
QUICHE teama6ef0a62019-03-07 20:34:33 -05003177 PacketFragments packet46 = {
3178 // type (short header, 4 byte packet number)
3179 {"",
3180 {0x43}},
3181 // connection_id
3182 {"",
3183 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
3184 // packet number
3185 {"",
3186 {0x12, 0x34, 0x56, 0x78}},
3187 // frame type (ack frame)
3188 // (one ack block, 2 byte largest observed, 2 byte block length)
3189 {"",
3190 {0x45}},
3191 // largest acked
3192 {"Unable to read largest acked.",
3193 {0x12, 0x34}},
3194 // Zero delta time.
3195 {"Unable to read ack delay time.",
3196 {0x00, 0x00}},
3197 // first ack block length.
3198 {"Unable to read first ack block length.",
3199 {0x88, 0x88}},
3200 // num timestamps.
3201 {"Underflow with first ack block length 34952 largest acked is 4660.",
3202 {0x00}}
3203 };
3204
3205 PacketFragments packet99 = {
3206 // type (short header, 4 byte packet number)
3207 {"",
3208 {0x43}},
3209 // connection_id
3210 {"",
3211 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
3212 // packet number
3213 {"",
3214 {0x12, 0x34, 0x56, 0x78}},
3215 // frame type (IETF_ACK)
3216 {"",
3217 {0x02}},
3218 // largest acked
3219 {"Unable to read largest acked.",
3220 {kVarInt62TwoBytes + 0x12, 0x34}},
3221 // Zero delta time.
3222 {"Unable to read ack delay time.",
3223 {kVarInt62OneByte + 0x00}},
3224 // Ack block count (0 -- no blocks after the first)
3225 {"Unable to read ack block count.",
3226 {kVarInt62OneByte + 0x00}},
3227 // first ack block length.
3228 {"Unable to read first ack block length.",
3229 {kVarInt62TwoBytes + 0x28, 0x88}}
3230 };
3231 // clang-format on
3232
3233 PacketFragments& fragments =
fkastenholz305e1732019-06-18 05:01:22 -07003234 VersionHasIetfQuicFrames(framer_.transport_version())
QUICHE teama6ef0a62019-03-07 20:34:33 -05003235 ? packet99
fayang36825da2019-08-21 14:01:27 -07003236 : (framer_.transport_version() >= QUIC_VERSION_46 ? packet46
3237 : packet);
QUICHE teama6ef0a62019-03-07 20:34:33 -05003238 std::unique_ptr<QuicEncryptedPacket> encrypted(
3239 AssemblePacketFromFragments(fragments));
3240 EXPECT_FALSE(framer_.ProcessPacket(*encrypted));
3241 CheckFramingBoundaries(fragments, QUIC_INVALID_ACK_DATA);
3242}
3243
3244// This test checks that the ack frame processor correctly identifies
3245// and handles the case where the third ack block's gap is larger than the
3246// available space in the ack range.
3247TEST_P(QuicFramerTest, ThirdAckBlockUnderflowGap) {
fkastenholz305e1732019-06-18 05:01:22 -07003248 if (!VersionHasIetfQuicFrames(framer_.transport_version())) {
fkastenholzc5e0c8c2019-09-30 10:16:30 -07003249 // Test originally written for development of IETF QUIC. The test may
3250 // also apply to Google QUIC. If so, the test should be extended to
3251 // include Google QUIC (frame formats, etc). See b/141858819.
QUICHE teama6ef0a62019-03-07 20:34:33 -05003252 return;
3253 }
zhongyi546cc452019-04-12 15:27:49 -07003254 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
QUICHE teama6ef0a62019-03-07 20:34:33 -05003255 // clang-format off
3256 PacketFragments packet99 = {
3257 // type (short header, 4 byte packet number)
3258 {"",
3259 {0x43}},
3260 // connection_id
3261 {"",
3262 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
3263 // packet number
3264 {"",
3265 {0x12, 0x34, 0x56, 0x78}},
3266 // frame type (IETF_ACK frame)
3267 {"",
3268 {0x02}},
3269 // largest acked
3270 {"Unable to read largest acked.",
3271 {kVarInt62OneByte + 63}},
3272 // Zero delta time.
3273 {"Unable to read ack delay time.",
3274 {kVarInt62OneByte + 0x00}},
3275 // Ack block count (2 -- 2 blocks after the first)
3276 {"Unable to read ack block count.",
3277 {kVarInt62OneByte + 0x02}},
3278 // first ack block length.
3279 {"Unable to read first ack block length.",
3280 {kVarInt62OneByte + 13}}, // Ack 14 packets, range 50..63 (inclusive)
3281
3282 {"Unable to read gap block value.",
3283 {kVarInt62OneByte + 9}}, // Gap 10 packets, 40..49 (inclusive)
3284 {"Unable to read ack block value.",
3285 {kVarInt62OneByte + 9}}, // Ack 10 packets, 30..39 (inclusive)
3286 {"Unable to read gap block value.",
3287 {kVarInt62OneByte + 29}}, // A gap of 30 packets (0..29 inclusive)
3288 // should be too big, leaving no room
3289 // for the ack.
3290 {"Underflow with gap block length 30 previous ack block start is 30.",
3291 {kVarInt62OneByte + 10}}, // Don't care
3292 };
3293 // clang-format on
3294
3295 std::unique_ptr<QuicEncryptedPacket> encrypted(
3296 AssemblePacketFromFragments(packet99));
3297 EXPECT_FALSE(framer_.ProcessPacket(*encrypted));
3298 EXPECT_EQ(
3299 framer_.detailed_error(),
3300 "Underflow with gap block length 30 previous ack block start is 30.");
3301 CheckFramingBoundaries(packet99, QUIC_INVALID_ACK_DATA);
3302}
3303
3304// This test checks that the ack frame processor correctly identifies
3305// and handles the case where the third ack block's length is larger than the
3306// available space in the ack range.
3307TEST_P(QuicFramerTest, ThirdAckBlockUnderflowAck) {
fkastenholz305e1732019-06-18 05:01:22 -07003308 if (!VersionHasIetfQuicFrames(framer_.transport_version())) {
fkastenholzc5e0c8c2019-09-30 10:16:30 -07003309 // Test originally written for development of IETF QUIC. The test may
3310 // also apply to Google QUIC. If so, the test should be extended to
3311 // include Google QUIC (frame formats, etc). See b/141858819.
QUICHE teama6ef0a62019-03-07 20:34:33 -05003312 return;
3313 }
zhongyi546cc452019-04-12 15:27:49 -07003314 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
QUICHE teama6ef0a62019-03-07 20:34:33 -05003315 // clang-format off
3316 PacketFragments packet99 = {
3317 // type (short header, 4 byte packet number)
3318 {"",
3319 {0x43}},
3320 // connection_id
3321 {"",
3322 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
3323 // packet number
3324 {"",
3325 {0x12, 0x34, 0x56, 0x78}},
3326 // frame type (IETF_ACK frame)
3327 {"",
3328 {0x02}},
3329 // largest acked
3330 {"Unable to read largest acked.",
3331 {kVarInt62OneByte + 63}},
3332 // Zero delta time.
3333 {"Unable to read ack delay time.",
3334 {kVarInt62OneByte + 0x00}},
3335 // Ack block count (2 -- 2 blocks after the first)
3336 {"Unable to read ack block count.",
3337 {kVarInt62OneByte + 0x02}},
3338 // first ack block length.
3339 {"Unable to read first ack block length.",
3340 {kVarInt62OneByte + 13}}, // only 50 packet numbers "left"
3341
3342 {"Unable to read gap block value.",
3343 {kVarInt62OneByte + 10}}, // Only 40 packet numbers left
3344 {"Unable to read ack block value.",
3345 {kVarInt62OneByte + 10}}, // only 30 packet numbers left.
3346 {"Unable to read gap block value.",
3347 {kVarInt62OneByte + 1}}, // Gap is OK, 29 packet numbers left
3348 {"Unable to read ack block value.",
3349 {kVarInt62OneByte + 30}}, // Use up all 30, should be an error
3350 };
3351 // clang-format on
3352
3353 std::unique_ptr<QuicEncryptedPacket> encrypted(
3354 AssemblePacketFromFragments(packet99));
3355 EXPECT_FALSE(framer_.ProcessPacket(*encrypted));
3356 EXPECT_EQ(framer_.detailed_error(),
3357 "Underflow with ack block length 31 latest ack block end is 25.");
3358 CheckFramingBoundaries(packet99, QUIC_INVALID_ACK_DATA);
3359}
3360
3361// Tests a variety of ack block wrap scenarios. For example, if the
3362// N-1th block causes packet 0 to be acked, then a gap would wrap
3363// around to 0x3fffffff ffffffff... Make sure we detect this
3364// condition.
3365TEST_P(QuicFramerTest, AckBlockUnderflowGapWrap) {
fkastenholz305e1732019-06-18 05:01:22 -07003366 if (!VersionHasIetfQuicFrames(framer_.transport_version())) {
fkastenholzc5e0c8c2019-09-30 10:16:30 -07003367 // Test originally written for development of IETF QUIC. The test may
3368 // also apply to Google QUIC. If so, the test should be extended to
3369 // include Google QUIC (frame formats, etc). See b/141858819.
QUICHE teama6ef0a62019-03-07 20:34:33 -05003370 return;
3371 }
zhongyi546cc452019-04-12 15:27:49 -07003372 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
QUICHE teama6ef0a62019-03-07 20:34:33 -05003373 // clang-format off
3374 PacketFragments packet99 = {
3375 // type (short header, 4 byte packet number)
3376 {"",
3377 {0x43}},
3378 // connection_id
3379 {"",
3380 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
3381 // packet number
3382 {"",
3383 {0x12, 0x34, 0x56, 0x78}},
3384 // frame type (IETF_ACK frame)
3385 {"",
3386 {0x02}},
3387 // largest acked
3388 {"Unable to read largest acked.",
3389 {kVarInt62OneByte + 10}},
3390 // Zero delta time.
3391 {"Unable to read ack delay time.",
3392 {kVarInt62OneByte + 0x00}},
3393 // Ack block count (1 -- 1 blocks after the first)
3394 {"Unable to read ack block count.",
3395 {kVarInt62OneByte + 1}},
3396 // first ack block length.
3397 {"Unable to read first ack block length.",
3398 {kVarInt62OneByte + 9}}, // Ack packets 1..10 (inclusive)
3399
3400 {"Unable to read gap block value.",
3401 {kVarInt62OneByte + 1}}, // Gap of 2 packets (-1...0), should wrap
3402 {"Underflow with gap block length 2 previous ack block start is 1.",
3403 {kVarInt62OneByte + 9}}, // irrelevant
3404 };
3405 // clang-format on
3406
3407 std::unique_ptr<QuicEncryptedPacket> encrypted(
3408 AssemblePacketFromFragments(packet99));
3409 EXPECT_FALSE(framer_.ProcessPacket(*encrypted));
3410 EXPECT_EQ(framer_.detailed_error(),
3411 "Underflow with gap block length 2 previous ack block start is 1.");
3412 CheckFramingBoundaries(packet99, QUIC_INVALID_ACK_DATA);
3413}
3414
3415// As AckBlockUnderflowGapWrap, but in this test, it's the ack
3416// component of the ack-block that causes the wrap, not the gap.
3417TEST_P(QuicFramerTest, AckBlockUnderflowAckWrap) {
fkastenholz305e1732019-06-18 05:01:22 -07003418 if (!VersionHasIetfQuicFrames(framer_.transport_version())) {
fkastenholzc5e0c8c2019-09-30 10:16:30 -07003419 // Test originally written for development of IETF QUIC. The test may
3420 // also apply to Google QUIC. If so, the test should be extended to
3421 // include Google QUIC (frame formats, etc). See b/141858819.
QUICHE teama6ef0a62019-03-07 20:34:33 -05003422 return;
3423 }
zhongyi546cc452019-04-12 15:27:49 -07003424 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
QUICHE teama6ef0a62019-03-07 20:34:33 -05003425 // clang-format off
3426 PacketFragments packet99 = {
3427 // type (short header, 4 byte packet number)
3428 {"",
3429 {0x43}},
3430 // connection_id
3431 {"",
3432 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
3433 // packet number
3434 {"",
3435 {0x12, 0x34, 0x56, 0x78}},
3436 // frame type (IETF_ACK frame)
3437 {"",
3438 {0x02}},
3439 // largest acked
3440 {"Unable to read largest acked.",
3441 {kVarInt62OneByte + 10}},
3442 // Zero delta time.
3443 {"Unable to read ack delay time.",
3444 {kVarInt62OneByte + 0x00}},
3445 // Ack block count (1 -- 1 blocks after the first)
3446 {"Unable to read ack block count.",
3447 {kVarInt62OneByte + 1}},
3448 // first ack block length.
3449 {"Unable to read first ack block length.",
3450 {kVarInt62OneByte + 6}}, // Ack packets 4..10 (inclusive)
3451
3452 {"Unable to read gap block value.",
3453 {kVarInt62OneByte + 1}}, // Gap of 2 packets (2..3)
3454 {"Unable to read ack block value.",
3455 {kVarInt62OneByte + 9}}, // Should wrap.
3456 };
3457 // clang-format on
3458
3459 std::unique_ptr<QuicEncryptedPacket> encrypted(
3460 AssemblePacketFromFragments(packet99));
3461 EXPECT_FALSE(framer_.ProcessPacket(*encrypted));
3462 EXPECT_EQ(framer_.detailed_error(),
3463 "Underflow with ack block length 10 latest ack block end is 1.");
3464 CheckFramingBoundaries(packet99, QUIC_INVALID_ACK_DATA);
3465}
3466
3467// An ack block that acks the entire range, 1...0x3fffffffffffffff
3468TEST_P(QuicFramerTest, AckBlockAcksEverything) {
fkastenholz305e1732019-06-18 05:01:22 -07003469 if (!VersionHasIetfQuicFrames(framer_.transport_version())) {
fkastenholzc5e0c8c2019-09-30 10:16:30 -07003470 // Test originally written for development of IETF QUIC. The test may
3471 // also apply to Google QUIC. If so, the test should be extended to
3472 // include Google QUIC (frame formats, etc). See b/141858819.
QUICHE teama6ef0a62019-03-07 20:34:33 -05003473 return;
3474 }
zhongyi546cc452019-04-12 15:27:49 -07003475 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
QUICHE teama6ef0a62019-03-07 20:34:33 -05003476 // clang-format off
3477 PacketFragments packet99 = {
3478 // type (short header, 4 byte packet number)
3479 {"",
3480 {0x43}},
3481 // connection_id
3482 {"",
3483 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
3484 // packet number
3485 {"",
3486 {0x12, 0x34, 0x56, 0x78}},
3487 // frame type (IETF_ACK frame)
3488 {"",
3489 {0x02}},
3490 // largest acked
3491 {"Unable to read largest acked.",
3492 {kVarInt62EightBytes + 0x3f, 0xff, 0xff, 0xff,
3493 0xff, 0xff, 0xff, 0xff}},
3494 // Zero delta time.
3495 {"Unable to read ack delay time.",
3496 {kVarInt62OneByte + 0x00}},
3497 // Ack block count No additional blocks
3498 {"Unable to read ack block count.",
3499 {kVarInt62OneByte + 0}},
3500 // first ack block length.
3501 {"Unable to read first ack block length.",
3502 {kVarInt62EightBytes + 0x3f, 0xff, 0xff, 0xff,
3503 0xff, 0xff, 0xff, 0xfe}},
3504 };
3505 // clang-format on
3506
3507 std::unique_ptr<QuicEncryptedPacket> encrypted(
3508 AssemblePacketFromFragments(packet99));
3509 EXPECT_TRUE(framer_.ProcessPacket(*encrypted));
3510 EXPECT_EQ(1u, visitor_.ack_frames_.size());
3511 const QuicAckFrame& frame = *visitor_.ack_frames_[0];
3512 EXPECT_EQ(1u, frame.packets.NumIntervals());
3513 EXPECT_EQ(kLargestIetfLargestObserved, LargestAcked(frame));
3514 EXPECT_EQ(kLargestIetfLargestObserved.ToUint64(),
3515 frame.packets.NumPacketsSlow());
3516}
3517
3518// This test looks for a malformed ack where
3519// - There is a largest-acked value (that is, the frame is acking
3520// something,
3521// - But the length of the first ack block is 0 saying that no frames
3522// are being acked with the largest-acked value or there are no
3523// additional ack blocks.
3524//
3525TEST_P(QuicFramerTest, AckFrameFirstAckBlockLengthZero) {
fkastenholz305e1732019-06-18 05:01:22 -07003526 if (VersionHasIetfQuicFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05003527 // Not applicable to version 99 -- first ack block contains the
3528 // number of packets that preceed the largest_acked packet.
3529 // A value of 0 means no packets preceed --- that the block's
3530 // length is 1. Therefore the condition that this test checks can
3531 // not arise.
3532 return;
3533 }
nharper9bb83462019-05-01 10:53:22 -07003534 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
QUICHE teama6ef0a62019-03-07 20:34:33 -05003535
3536 // clang-format off
3537 PacketFragments packet = {
3538 // public flags (8 byte connection_id)
3539 {"",
3540 { 0x2C }},
3541 // connection_id
3542 {"",
3543 { 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10 }},
3544 // packet number
3545 {"",
3546 { 0x12, 0x34, 0x56, 0x78 }},
3547
3548 // frame type (ack frame)
3549 // (more than one ack block, 2 byte largest observed, 2 byte block length)
3550 {"",
3551 { 0x65 }},
3552 // largest acked
3553 {"Unable to read largest acked.",
3554 { 0x12, 0x34 }},
3555 // Zero delta time.
3556 {"Unable to read ack delay time.",
3557 { 0x00, 0x00 }},
3558 // num ack blocks ranges.
3559 {"Unable to read num of ack blocks.",
3560 { 0x01 }},
3561 // first ack block length.
3562 {"Unable to read first ack block length.",
3563 { 0x00, 0x00 }},
3564 // gap to next block.
3565 { "First block length is zero.",
3566 { 0x01 }},
3567 // ack block length.
3568 { "First block length is zero.",
3569 { 0x0e, 0xaf }},
3570 // Number of timestamps.
3571 { "First block length is zero.",
3572 { 0x00 }},
3573 };
3574
QUICHE teama6ef0a62019-03-07 20:34:33 -05003575 PacketFragments packet46 = {
3576 // type (short header, 4 byte packet number)
3577 {"",
3578 { 0x43 }},
3579 // connection_id
3580 {"",
3581 { 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10 }},
3582 // packet number
3583 {"",
3584 { 0x12, 0x34, 0x56, 0x78 }},
3585
3586 // frame type (ack frame)
3587 // (more than one ack block, 2 byte largest observed, 2 byte block length)
3588 {"",
3589 { 0x65 }},
3590 // largest acked
3591 {"Unable to read largest acked.",
3592 { 0x12, 0x34 }},
3593 // Zero delta time.
3594 {"Unable to read ack delay time.",
3595 { 0x00, 0x00 }},
3596 // num ack blocks ranges.
3597 {"Unable to read num of ack blocks.",
3598 { 0x01 }},
3599 // first ack block length.
3600 {"Unable to read first ack block length.",
3601 { 0x00, 0x00 }},
3602 // gap to next block.
3603 { "First block length is zero.",
3604 { 0x01 }},
3605 // ack block length.
3606 { "First block length is zero.",
3607 { 0x0e, 0xaf }},
3608 // Number of timestamps.
3609 { "First block length is zero.",
3610 { 0x00 }},
3611 };
3612
3613 // clang-format on
3614 PacketFragments& fragments =
fayang36825da2019-08-21 14:01:27 -07003615 framer_.transport_version() >= QUIC_VERSION_46 ? packet46 : packet;
QUICHE teama6ef0a62019-03-07 20:34:33 -05003616
3617 std::unique_ptr<QuicEncryptedPacket> encrypted(
3618 AssemblePacketFromFragments(fragments));
3619
3620 EXPECT_FALSE(framer_.ProcessPacket(*encrypted));
bncf6f82b12019-10-30 07:01:01 -07003621 EXPECT_THAT(framer_.error(), IsError(QUIC_INVALID_ACK_DATA));
QUICHE teama6ef0a62019-03-07 20:34:33 -05003622
3623 ASSERT_TRUE(visitor_.header_.get());
3624 EXPECT_TRUE(CheckDecryption(
3625 *encrypted, !kIncludeVersion, !kIncludeDiversificationNonce,
3626 PACKET_8BYTE_CONNECTION_ID, PACKET_0BYTE_CONNECTION_ID));
3627
3628 EXPECT_EQ(0u, visitor_.stream_frames_.size());
3629 ASSERT_EQ(1u, visitor_.ack_frames_.size());
3630
3631 CheckFramingBoundaries(fragments, QUIC_INVALID_ACK_DATA);
3632}
3633
3634TEST_P(QuicFramerTest, AckFrameOneAckBlockMaxLength) {
zhongyi546cc452019-04-12 15:27:49 -07003635 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
QUICHE teama6ef0a62019-03-07 20:34:33 -05003636 // clang-format off
3637 PacketFragments packet = {
3638 // public flags (8 byte connection_id)
3639 {"",
3640 {0x2C}},
3641 // connection_id
3642 {"",
3643 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
3644 // packet number
3645 {"",
3646 {0x12, 0x34, 0x56, 0x78}},
3647 // frame type (ack frame)
3648 // (one ack block, 4 byte largest observed, 2 byte block length)
3649 {"",
3650 {0x49}},
3651 // largest acked
3652 {"Unable to read largest acked.",
3653 {0x12, 0x34, 0x56, 0x78}},
3654 // Zero delta time.
3655 {"Unable to read ack delay time.",
3656 {0x00, 0x00}},
3657 // first ack block length.
3658 {"Unable to read first ack block length.",
3659 {0x12, 0x34}},
3660 // num timestamps.
3661 {"Unable to read num received packets.",
3662 {0x00}}
3663 };
3664
QUICHE teama6ef0a62019-03-07 20:34:33 -05003665 PacketFragments packet46 = {
3666 // type (short header, 4 byte packet number)
3667 {"",
3668 {0x43}},
3669 // connection_id
3670 {"",
3671 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
3672 // packet number
3673 {"",
3674 {0x56, 0x78, 0x9A, 0xBC}},
3675 // frame type (ack frame)
3676 // (one ack block, 4 byte largest observed, 2 byte block length)
3677 {"",
3678 {0x49}},
3679 // largest acked
3680 {"Unable to read largest acked.",
3681 {0x12, 0x34, 0x56, 0x78}},
3682 // Zero delta time.
3683 {"Unable to read ack delay time.",
3684 {0x00, 0x00}},
3685 // first ack block length.
3686 {"Unable to read first ack block length.",
3687 {0x12, 0x34}},
3688 // num timestamps.
3689 {"Unable to read num received packets.",
3690 {0x00}}
3691 };
3692
3693 PacketFragments packet99 = {
3694 // type (short header, 4 byte packet number)
3695 {"",
3696 {0x43}},
3697 // connection_id
3698 {"",
3699 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
3700 // packet number
3701 {"",
3702 {0x56, 0x78, 0x9A, 0xBC}},
3703 // frame type (IETF_ACK frame)
3704 {"",
3705 {0x02}},
3706 // largest acked
3707 {"Unable to read largest acked.",
3708 {kVarInt62FourBytes + 0x12, 0x34, 0x56, 0x78}},
3709 // Zero delta time.
3710 {"Unable to read ack delay time.",
3711 {kVarInt62OneByte + 0x00}},
3712 // Number of ack blocks after first
3713 {"Unable to read ack block count.",
3714 {kVarInt62OneByte + 0x00}},
3715 // first ack block length.
3716 {"Unable to read first ack block length.",
3717 {kVarInt62TwoBytes + 0x12, 0x33}}
3718 };
3719 // clang-format on
3720
3721 PacketFragments& fragments =
fkastenholz305e1732019-06-18 05:01:22 -07003722 VersionHasIetfQuicFrames(framer_.transport_version())
QUICHE teama6ef0a62019-03-07 20:34:33 -05003723 ? packet99
fayang36825da2019-08-21 14:01:27 -07003724 : (framer_.transport_version() >= QUIC_VERSION_46 ? packet46
3725 : packet);
QUICHE teama6ef0a62019-03-07 20:34:33 -05003726 std::unique_ptr<QuicEncryptedPacket> encrypted(
3727 AssemblePacketFromFragments(fragments));
3728 EXPECT_TRUE(framer_.ProcessPacket(*encrypted));
3729
bncf6f82b12019-10-30 07:01:01 -07003730 EXPECT_THAT(framer_.error(), IsQuicNoError());
QUICHE teama6ef0a62019-03-07 20:34:33 -05003731 ASSERT_TRUE(visitor_.header_.get());
3732 EXPECT_TRUE(CheckDecryption(
3733 *encrypted, !kIncludeVersion, !kIncludeDiversificationNonce,
3734 PACKET_8BYTE_CONNECTION_ID, PACKET_0BYTE_CONNECTION_ID));
3735
3736 EXPECT_EQ(0u, visitor_.stream_frames_.size());
3737 ASSERT_EQ(1u, visitor_.ack_frames_.size());
3738 const QuicAckFrame& frame = *visitor_.ack_frames_[0];
3739 EXPECT_EQ(kPacketNumber, LargestAcked(frame));
3740 ASSERT_EQ(4660u, frame.packets.NumPacketsSlow());
3741
3742 CheckFramingBoundaries(fragments, QUIC_INVALID_ACK_DATA);
3743}
3744
3745// Tests ability to handle multiple ackblocks after the first ack
3746// block. Non-version-99 tests include multiple timestamps as well.
3747TEST_P(QuicFramerTest, AckFrameTwoTimeStampsMultipleAckBlocks) {
zhongyi546cc452019-04-12 15:27:49 -07003748 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
QUICHE teama6ef0a62019-03-07 20:34:33 -05003749 // clang-format off
3750 PacketFragments packet = {
3751 // public flags (8 byte connection_id)
3752 {"",
3753 { 0x2C }},
3754 // connection_id
3755 {"",
3756 { 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10 }},
3757 // packet number
3758 {"",
3759 { 0x12, 0x34, 0x56, 0x78 }},
3760
3761 // frame type (ack frame)
3762 // (more than one ack block, 2 byte largest observed, 2 byte block length)
3763 {"",
3764 { 0x65 }},
3765 // largest acked
3766 {"Unable to read largest acked.",
3767 { 0x12, 0x34 }},
3768 // Zero delta time.
3769 {"Unable to read ack delay time.",
3770 { 0x00, 0x00 }},
3771 // num ack blocks ranges.
3772 {"Unable to read num of ack blocks.",
3773 { 0x04 }},
3774 // first ack block length.
3775 {"Unable to read first ack block length.",
3776 { 0x00, 0x01 }},
3777 // gap to next block.
3778 { "Unable to read gap to next ack block.",
3779 { 0x01 }},
3780 // ack block length.
3781 { "Unable to ack block length.",
3782 { 0x0e, 0xaf }},
3783 // gap to next block.
3784 { "Unable to read gap to next ack block.",
3785 { 0xff }},
3786 // ack block length.
3787 { "Unable to ack block length.",
3788 { 0x00, 0x00 }},
3789 // gap to next block.
3790 { "Unable to read gap to next ack block.",
3791 { 0x91 }},
3792 // ack block length.
3793 { "Unable to ack block length.",
3794 { 0x01, 0xea }},
3795 // gap to next block.
3796 { "Unable to read gap to next ack block.",
3797 { 0x05 }},
3798 // ack block length.
3799 { "Unable to ack block length.",
3800 { 0x00, 0x04 }},
3801 // Number of timestamps.
3802 { "Unable to read num received packets.",
3803 { 0x02 }},
3804 // Delta from largest observed.
3805 { "Unable to read sequence delta in received packets.",
3806 { 0x01 }},
3807 // Delta time.
3808 { "Unable to read time delta in received packets.",
3809 { 0x76, 0x54, 0x32, 0x10 }},
3810 // Delta from largest observed.
3811 { "Unable to read sequence delta in received packets.",
3812 { 0x02 }},
3813 // Delta time.
3814 { "Unable to read incremental time delta in received packets.",
3815 { 0x32, 0x10 }},
3816 };
3817
QUICHE teama6ef0a62019-03-07 20:34:33 -05003818 PacketFragments packet46 = {
3819 // type (short header, 4 byte packet number)
3820 {"",
3821 { 0x43 }},
3822 // connection_id
3823 {"",
3824 { 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10 }},
3825 // packet number
3826 {"",
3827 { 0x12, 0x34, 0x56, 0x78 }},
3828
3829 // frame type (ack frame)
3830 // (more than one ack block, 2 byte largest observed, 2 byte block length)
3831 {"",
3832 { 0x65 }},
3833 // largest acked
3834 {"Unable to read largest acked.",
3835 { 0x12, 0x34 }},
3836 // Zero delta time.
3837 {"Unable to read ack delay time.",
3838 { 0x00, 0x00 }},
3839 // num ack blocks ranges.
3840 {"Unable to read num of ack blocks.",
3841 { 0x04 }},
3842 // first ack block length.
3843 {"Unable to read first ack block length.",
3844 { 0x00, 0x01 }},
3845 // gap to next block.
3846 { "Unable to read gap to next ack block.",
3847 { 0x01 }},
3848 // ack block length.
3849 { "Unable to ack block length.",
3850 { 0x0e, 0xaf }},
3851 // gap to next block.
3852 { "Unable to read gap to next ack block.",
3853 { 0xff }},
3854 // ack block length.
3855 { "Unable to ack block length.",
3856 { 0x00, 0x00 }},
3857 // gap to next block.
3858 { "Unable to read gap to next ack block.",
3859 { 0x91 }},
3860 // ack block length.
3861 { "Unable to ack block length.",
3862 { 0x01, 0xea }},
3863 // gap to next block.
3864 { "Unable to read gap to next ack block.",
3865 { 0x05 }},
3866 // ack block length.
3867 { "Unable to ack block length.",
3868 { 0x00, 0x04 }},
3869 // Number of timestamps.
3870 { "Unable to read num received packets.",
3871 { 0x02 }},
3872 // Delta from largest observed.
3873 { "Unable to read sequence delta in received packets.",
3874 { 0x01 }},
3875 // Delta time.
3876 { "Unable to read time delta in received packets.",
3877 { 0x76, 0x54, 0x32, 0x10 }},
3878 // Delta from largest observed.
3879 { "Unable to read sequence delta in received packets.",
3880 { 0x02 }},
3881 // Delta time.
3882 { "Unable to read incremental time delta in received packets.",
3883 { 0x32, 0x10 }},
3884 };
3885
3886 PacketFragments packet99 = {
3887 // type (short header, 4 byte packet number)
3888 {"",
3889 { 0x43 }},
3890 // connection_id
3891 {"",
3892 { 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10 }},
3893 // packet number
3894 {"",
3895 { 0x12, 0x34, 0x56, 0x78 }},
3896
3897 // frame type (IETF_ACK frame)
3898 {"",
3899 { 0x02 }},
3900 // largest acked
3901 {"Unable to read largest acked.",
3902 { kVarInt62TwoBytes + 0x12, 0x34 }}, // = 4660
3903 // Zero delta time.
3904 {"Unable to read ack delay time.",
3905 { kVarInt62OneByte + 0x00 }},
3906 // number of additional ack blocks
3907 {"Unable to read ack block count.",
3908 { kVarInt62OneByte + 0x03 }},
3909 // first ack block length.
3910 {"Unable to read first ack block length.",
3911 { kVarInt62OneByte + 0x00 }}, // 1st block length = 1
3912
3913 // Additional ACK Block #1
3914 // gap to next block.
3915 { "Unable to read gap block value.",
3916 { kVarInt62OneByte + 0x00 }}, // gap of 1 packet
3917 // ack block length.
3918 { "Unable to read ack block value.",
3919 { kVarInt62TwoBytes + 0x0e, 0xae }}, // 3759
3920
3921 // pre-version-99 test includes an ack block of 0 length. this
3922 // can not happen in version 99. ergo the second block is not
3923 // present in the v99 test and the gap length of the next block
3924 // is the sum of the two gaps in the pre-version-99 tests.
3925 // Additional ACK Block #2
3926 // gap to next block.
3927 { "Unable to read gap block value.",
3928 { kVarInt62TwoBytes + 0x01, 0x8f }}, // Gap is 400 (0x190) pkts
3929 // ack block length.
3930 { "Unable to read ack block value.",
3931 { kVarInt62TwoBytes + 0x01, 0xe9 }}, // block is 389 (x1ea) pkts
3932
3933 // Additional ACK Block #3
3934 // gap to next block.
3935 { "Unable to read gap block value.",
3936 { kVarInt62OneByte + 0x04 }}, // Gap is 5 packets.
3937 // ack block length.
3938 { "Unable to read ack block value.",
3939 { kVarInt62OneByte + 0x03 }}, // block is 3 packets.
3940 };
3941
3942 // clang-format on
3943 PacketFragments& fragments =
fkastenholz305e1732019-06-18 05:01:22 -07003944 VersionHasIetfQuicFrames(framer_.transport_version())
QUICHE teama6ef0a62019-03-07 20:34:33 -05003945 ? packet99
fayang36825da2019-08-21 14:01:27 -07003946 : (framer_.transport_version() >= QUIC_VERSION_46 ? packet46
3947 : packet);
QUICHE teama6ef0a62019-03-07 20:34:33 -05003948
3949 std::unique_ptr<QuicEncryptedPacket> encrypted(
3950 AssemblePacketFromFragments(fragments));
3951
3952 framer_.set_process_timestamps(true);
3953 EXPECT_TRUE(framer_.ProcessPacket(*encrypted));
3954
bncf6f82b12019-10-30 07:01:01 -07003955 EXPECT_THAT(framer_.error(), IsQuicNoError());
QUICHE teama6ef0a62019-03-07 20:34:33 -05003956 ASSERT_TRUE(visitor_.header_.get());
3957 EXPECT_TRUE(CheckDecryption(
3958 *encrypted, !kIncludeVersion, !kIncludeDiversificationNonce,
3959 PACKET_8BYTE_CONNECTION_ID, PACKET_0BYTE_CONNECTION_ID));
3960
3961 EXPECT_EQ(0u, visitor_.stream_frames_.size());
3962 ASSERT_EQ(1u, visitor_.ack_frames_.size());
3963 const QuicAckFrame& frame = *visitor_.ack_frames_[0];
3964 EXPECT_EQ(kSmallLargestObserved, LargestAcked(frame));
3965 ASSERT_EQ(4254u, frame.packets.NumPacketsSlow());
3966 EXPECT_EQ(4u, frame.packets.NumIntervals());
fkastenholz305e1732019-06-18 05:01:22 -07003967 if (VersionHasIetfQuicFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05003968 EXPECT_EQ(0u, frame.received_packet_times.size());
3969 } else {
3970 EXPECT_EQ(2u, frame.received_packet_times.size());
3971 }
3972 CheckFramingBoundaries(fragments, QUIC_INVALID_ACK_DATA);
3973}
3974
3975TEST_P(QuicFramerTest, AckFrameTimeStampDeltaTooHigh) {
nharper9bb83462019-05-01 10:53:22 -07003976 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
QUICHE teama6ef0a62019-03-07 20:34:33 -05003977 // clang-format off
3978 unsigned char packet[] = {
3979 // public flags (8 byte connection_id)
3980 0x28,
3981 // connection_id
3982 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
3983 // packet number
3984 0x12, 0x34, 0x56, 0x78,
3985
3986 // frame type (ack frame)
3987 // (no ack blocks, 1 byte largest observed, 1 byte block length)
3988 0x40,
3989 // largest acked
3990 0x01,
3991 // Zero delta time.
3992 0x00, 0x00,
3993 // first ack block length.
3994 0x01,
3995 // num timestamps.
3996 0x01,
3997 // Delta from largest observed.
3998 0x01,
3999 // Delta time.
4000 0x10, 0x32, 0x54, 0x76,
4001 };
4002
QUICHE teama6ef0a62019-03-07 20:34:33 -05004003 unsigned char packet46[] = {
4004 // type (short header, 4 byte packet number)
4005 0x43,
4006 // connection_id
4007 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
4008 // packet number
4009 0x12, 0x34, 0x56, 0x78,
4010
4011 // frame type (ack frame)
4012 // (no ack blocks, 1 byte largest observed, 1 byte block length)
4013 0x40,
4014 // largest acked
4015 0x01,
4016 // Zero delta time.
4017 0x00, 0x00,
4018 // first ack block length.
4019 0x01,
4020 // num timestamps.
4021 0x01,
4022 // Delta from largest observed.
4023 0x01,
4024 // Delta time.
4025 0x10, 0x32, 0x54, 0x76,
4026 };
4027 // clang-format on
fkastenholz305e1732019-06-18 05:01:22 -07004028 if (VersionHasIetfQuicFrames(framer_.transport_version())) {
fkastenholzc5e0c8c2019-09-30 10:16:30 -07004029 // ACK Timestamp is not a feature of IETF QUIC.
QUICHE teama6ef0a62019-03-07 20:34:33 -05004030 return;
4031 }
4032 QuicEncryptedPacket encrypted(
fayang36825da2019-08-21 14:01:27 -07004033 AsChars(framer_.transport_version() >= QUIC_VERSION_46 ? packet46
4034 : packet),
bnc4e9283d2019-12-17 07:08:57 -08004035 QUICHE_ARRAYSIZE(packet), false);
QUICHE teama6ef0a62019-03-07 20:34:33 -05004036 EXPECT_FALSE(framer_.ProcessPacket(encrypted));
dmcardlecf0bfcf2019-12-13 08:08:21 -08004037 EXPECT_TRUE(quiche::QuicheTextUtils::StartsWith(
QUICHE teama6ef0a62019-03-07 20:34:33 -05004038 framer_.detailed_error(), "delta_from_largest_observed too high"));
4039}
4040
4041TEST_P(QuicFramerTest, AckFrameTimeStampSecondDeltaTooHigh) {
nharper9bb83462019-05-01 10:53:22 -07004042 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
QUICHE teama6ef0a62019-03-07 20:34:33 -05004043 // clang-format off
4044 unsigned char packet[] = {
4045 // public flags (8 byte connection_id)
4046 0x28,
4047 // connection_id
4048 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
4049 // packet number
4050 0x12, 0x34, 0x56, 0x78,
4051
4052 // frame type (ack frame)
4053 // (no ack blocks, 1 byte largest observed, 1 byte block length)
4054 0x40,
4055 // largest acked
4056 0x03,
4057 // Zero delta time.
4058 0x00, 0x00,
4059 // first ack block length.
4060 0x03,
4061 // num timestamps.
4062 0x02,
4063 // Delta from largest observed.
4064 0x01,
4065 // Delta time.
4066 0x10, 0x32, 0x54, 0x76,
4067 // Delta from largest observed.
4068 0x03,
4069 // Delta time.
4070 0x10, 0x32,
4071 };
4072
QUICHE teama6ef0a62019-03-07 20:34:33 -05004073 unsigned char packet46[] = {
4074 // type (short header, 4 byte packet number)
4075 0x43,
4076 // connection_id
4077 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
4078 // packet number
4079 0x12, 0x34, 0x56, 0x78,
4080
4081 // frame type (ack frame)
4082 // (no ack blocks, 1 byte largest observed, 1 byte block length)
4083 0x40,
4084 // largest acked
4085 0x03,
4086 // Zero delta time.
4087 0x00, 0x00,
4088 // first ack block length.
4089 0x03,
4090 // num timestamps.
4091 0x02,
4092 // Delta from largest observed.
4093 0x01,
4094 // Delta time.
4095 0x10, 0x32, 0x54, 0x76,
4096 // Delta from largest observed.
4097 0x03,
4098 // Delta time.
4099 0x10, 0x32,
4100 };
4101 // clang-format on
fkastenholz305e1732019-06-18 05:01:22 -07004102 if (VersionHasIetfQuicFrames(framer_.transport_version())) {
fkastenholzc5e0c8c2019-09-30 10:16:30 -07004103 // ACK Timestamp is not a feature of IETF QUIC.
QUICHE teama6ef0a62019-03-07 20:34:33 -05004104 return;
4105 }
4106 QuicEncryptedPacket encrypted(
fayang36825da2019-08-21 14:01:27 -07004107 AsChars(framer_.transport_version() >= QUIC_VERSION_46 ? packet46
4108 : packet),
bnc4e9283d2019-12-17 07:08:57 -08004109 QUICHE_ARRAYSIZE(packet), false);
QUICHE teama6ef0a62019-03-07 20:34:33 -05004110 EXPECT_FALSE(framer_.ProcessPacket(encrypted));
dmcardlecf0bfcf2019-12-13 08:08:21 -08004111 EXPECT_TRUE(quiche::QuicheTextUtils::StartsWith(
QUICHE teama6ef0a62019-03-07 20:34:33 -05004112 framer_.detailed_error(), "delta_from_largest_observed too high"));
4113}
4114
4115TEST_P(QuicFramerTest, NewStopWaitingFrame) {
fkastenholz305e1732019-06-18 05:01:22 -07004116 if (VersionHasIetfQuicFrames(version_.transport_version)) {
fkastenholzc5e0c8c2019-09-30 10:16:30 -07004117 // The Stop Waiting frame is not in IETF QUIC
QUICHE teama6ef0a62019-03-07 20:34:33 -05004118 return;
4119 }
nharper9bb83462019-05-01 10:53:22 -07004120 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
QUICHE teama6ef0a62019-03-07 20:34:33 -05004121 // clang-format off
4122 PacketFragments packet = {
4123 // public flags (8 byte connection_id)
4124 {"",
4125 {0x2C}},
4126 // connection_id
4127 {"",
4128 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
4129 // packet number
4130 {"",
4131 {0x12, 0x34, 0x56, 0x78}},
4132 // frame type (stop waiting frame)
4133 {"",
4134 {0x06}},
4135 // least packet number awaiting an ack, delta from packet number.
4136 {"Unable to read least unacked delta.",
4137 {0x00, 0x00, 0x00, 0x08}}
4138 };
4139
QUICHE teama6ef0a62019-03-07 20:34:33 -05004140 PacketFragments packet46 = {
4141 // type (short header, 4 byte packet number)
4142 {"",
4143 {0x43}},
4144 // connection_id
4145 {"",
4146 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
4147 // packet number
4148 {"",
4149 {0x12, 0x34, 0x56, 0x78}},
4150 // frame type (stop waiting frame)
4151 {"",
4152 {0x06}},
4153 // least packet number awaiting an ack, delta from packet number.
4154 {"Unable to read least unacked delta.",
4155 {0x00, 0x00, 0x00, 0x08}}
4156 };
4157 // clang-format on
4158
4159 PacketFragments& fragments =
fayang36825da2019-08-21 14:01:27 -07004160 framer_.transport_version() >= QUIC_VERSION_46 ? packet46 : packet;
QUICHE teama6ef0a62019-03-07 20:34:33 -05004161
4162 std::unique_ptr<QuicEncryptedPacket> encrypted(
4163 AssemblePacketFromFragments(fragments));
ianswett97b690b2019-05-02 15:12:43 -07004164 if (GetQuicReloadableFlag(quic_do_not_accept_stop_waiting) &&
fayang36825da2019-08-21 14:01:27 -07004165 version_.transport_version >= QUIC_VERSION_46) {
ianswett97b690b2019-05-02 15:12:43 -07004166 EXPECT_FALSE(framer_.ProcessPacket(*encrypted));
bncf6f82b12019-10-30 07:01:01 -07004167 EXPECT_THAT(framer_.error(), IsError(QUIC_INVALID_STOP_WAITING_DATA));
ianswett97b690b2019-05-02 15:12:43 -07004168 EXPECT_EQ("STOP WAITING not supported in version 44+.",
4169 framer_.detailed_error());
4170 return;
4171 }
4172
QUICHE teama6ef0a62019-03-07 20:34:33 -05004173 EXPECT_TRUE(framer_.ProcessPacket(*encrypted));
4174
bncf6f82b12019-10-30 07:01:01 -07004175 EXPECT_THAT(framer_.error(), IsQuicNoError());
QUICHE teama6ef0a62019-03-07 20:34:33 -05004176 ASSERT_TRUE(visitor_.header_.get());
4177 EXPECT_TRUE(CheckDecryption(
4178 *encrypted, !kIncludeVersion, !kIncludeDiversificationNonce,
4179 PACKET_8BYTE_CONNECTION_ID, PACKET_0BYTE_CONNECTION_ID));
4180
4181 EXPECT_EQ(0u, visitor_.stream_frames_.size());
4182 ASSERT_EQ(1u, visitor_.stop_waiting_frames_.size());
4183 const QuicStopWaitingFrame& frame = *visitor_.stop_waiting_frames_[0];
4184 EXPECT_EQ(kLeastUnacked, frame.least_unacked);
4185
4186 CheckFramingBoundaries(fragments, QUIC_INVALID_STOP_WAITING_DATA);
4187}
4188
4189TEST_P(QuicFramerTest, InvalidNewStopWaitingFrame) {
fkastenholzc5e0c8c2019-09-30 10:16:30 -07004190 // The Stop Waiting frame is not in IETF QUIC
fkastenholz305e1732019-06-18 05:01:22 -07004191 if (VersionHasIetfQuicFrames(version_.transport_version) ||
ianswett97b690b2019-05-02 15:12:43 -07004192 (GetQuicReloadableFlag(quic_do_not_accept_stop_waiting) &&
fayang36825da2019-08-21 14:01:27 -07004193 version_.transport_version >= QUIC_VERSION_46)) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05004194 return;
4195 }
nharper9bb83462019-05-01 10:53:22 -07004196 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
QUICHE teama6ef0a62019-03-07 20:34:33 -05004197 // clang-format off
4198 unsigned char packet[] = {
4199 // public flags (8 byte connection_id)
4200 0x2C,
4201 // connection_id
4202 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
4203 // packet number
4204 0x12, 0x34, 0x56, 0x78,
4205 // frame type (stop waiting frame)
4206 0x06,
4207 // least packet number awaiting an ack, delta from packet number.
4208 0x13, 0x34, 0x56, 0x78,
4209 0x9A, 0xA8,
4210 };
4211
QUICHE teama6ef0a62019-03-07 20:34:33 -05004212 unsigned char packet46[] = {
4213 // type (short header, 4 byte packet number)
4214 0x43,
4215 // connection_id
4216 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
4217 // packet number
4218 0x12, 0x34, 0x56, 0x78,
4219 // frame type (stop waiting frame)
4220 0x06,
4221 // least packet number awaiting an ack, delta from packet number.
4222 0x57, 0x78, 0x9A, 0xA8,
4223 };
4224 // clang-format on
4225
4226 QuicEncryptedPacket encrypted(
fayang36825da2019-08-21 14:01:27 -07004227 AsChars(framer_.transport_version() >= QUIC_VERSION_46 ? packet46
4228 : packet),
bnc4e9283d2019-12-17 07:08:57 -08004229 framer_.transport_version() > QUIC_VERSION_43 ? QUICHE_ARRAYSIZE(packet46)
4230 : QUICHE_ARRAYSIZE(packet),
QUICHE teama6ef0a62019-03-07 20:34:33 -05004231 false);
4232 EXPECT_FALSE(framer_.ProcessPacket(encrypted));
bncf6f82b12019-10-30 07:01:01 -07004233 EXPECT_THAT(framer_.error(), IsError(QUIC_INVALID_STOP_WAITING_DATA));
QUICHE teama6ef0a62019-03-07 20:34:33 -05004234 EXPECT_EQ("Invalid unacked delta.", framer_.detailed_error());
4235}
4236
4237TEST_P(QuicFramerTest, RstStreamFrame) {
zhongyi546cc452019-04-12 15:27:49 -07004238 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
QUICHE teama6ef0a62019-03-07 20:34:33 -05004239 // clang-format off
4240 PacketFragments packet = {
4241 // public flags (8 byte connection_id)
4242 {"",
4243 {0x28}},
4244 // connection_id
4245 {"",
4246 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
4247 // packet number
4248 {"",
4249 {0x12, 0x34, 0x56, 0x78}},
4250 // frame type (rst stream frame)
4251 {"",
4252 {0x01}},
4253 // stream id
4254 {"Unable to read stream_id.",
4255 {0x01, 0x02, 0x03, 0x04}},
4256 // sent byte offset
4257 {"Unable to read rst stream sent byte offset.",
4258 {0x3A, 0x98, 0xFE, 0xDC,
4259 0x32, 0x10, 0x76, 0x54}},
4260 // error code
4261 {"Unable to read rst stream error code.",
4262 {0x00, 0x00, 0x00, 0x01}}
4263 };
4264
QUICHE teama6ef0a62019-03-07 20:34:33 -05004265 PacketFragments packet46 = {
4266 // type (short header, 4 byte packet number)
4267 {"",
4268 {0x43}},
4269 // connection_id
4270 {"",
4271 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
4272 // packet number
4273 {"",
4274 {0x12, 0x34, 0x56, 0x78}},
4275 // frame type (rst stream frame)
4276 {"",
4277 {0x01}},
4278 // stream id
4279 {"Unable to read stream_id.",
4280 {0x01, 0x02, 0x03, 0x04}},
4281 // sent byte offset
4282 {"Unable to read rst stream sent byte offset.",
4283 {0x3A, 0x98, 0xFE, 0xDC,
4284 0x32, 0x10, 0x76, 0x54}},
4285 // error code
4286 {"Unable to read rst stream error code.",
4287 {0x00, 0x00, 0x00, 0x01}}
4288 };
4289
4290 PacketFragments packet99 = {
4291 // type (short header, 4 byte packet number)
4292 {"",
4293 {0x43}},
4294 // connection_id
4295 {"",
4296 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
4297 // packet number
4298 {"",
4299 {0x12, 0x34, 0x56, 0x78}},
4300 // frame type (IETF_RST_STREAM frame)
4301 {"",
4302 {0x04}},
4303 // stream id
4304 {"Unable to read rst stream stream id.",
4305 {kVarInt62FourBytes + 0x01, 0x02, 0x03, 0x04}},
4306 // application error code
4307 {"Unable to read rst stream error code.",
fkastenholz07300e52019-07-16 11:51:37 -07004308 {kVarInt62OneByte + 0x01}},
QUICHE teama6ef0a62019-03-07 20:34:33 -05004309 // Final Offset
4310 {"Unable to read rst stream sent byte offset.",
4311 {kVarInt62EightBytes + 0x3a, 0x98, 0xFE, 0xDC, 0x32, 0x10, 0x76, 0x54}}
4312 };
4313 // clang-format on
4314
4315 PacketFragments& fragments =
fkastenholz305e1732019-06-18 05:01:22 -07004316 VersionHasIetfQuicFrames(framer_.transport_version())
QUICHE teama6ef0a62019-03-07 20:34:33 -05004317 ? packet99
fayang36825da2019-08-21 14:01:27 -07004318 : (framer_.transport_version() >= QUIC_VERSION_46 ? packet46
4319 : packet);
QUICHE teama6ef0a62019-03-07 20:34:33 -05004320 std::unique_ptr<QuicEncryptedPacket> encrypted(
4321 AssemblePacketFromFragments(fragments));
4322 EXPECT_TRUE(framer_.ProcessPacket(*encrypted));
4323
bncf6f82b12019-10-30 07:01:01 -07004324 EXPECT_THAT(framer_.error(), IsQuicNoError());
QUICHE teama6ef0a62019-03-07 20:34:33 -05004325 ASSERT_TRUE(visitor_.header_.get());
4326 EXPECT_TRUE(CheckDecryption(
4327 *encrypted, !kIncludeVersion, !kIncludeDiversificationNonce,
4328 PACKET_8BYTE_CONNECTION_ID, PACKET_0BYTE_CONNECTION_ID));
4329
4330 EXPECT_EQ(kStreamId, visitor_.rst_stream_frame_.stream_id);
4331 EXPECT_EQ(0x01, visitor_.rst_stream_frame_.error_code);
4332 EXPECT_EQ(kStreamOffset, visitor_.rst_stream_frame_.byte_offset);
4333 CheckFramingBoundaries(fragments, QUIC_INVALID_RST_STREAM_DATA);
4334}
4335
4336TEST_P(QuicFramerTest, ConnectionCloseFrame) {
zhongyi546cc452019-04-12 15:27:49 -07004337 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
QUICHE teama6ef0a62019-03-07 20:34:33 -05004338 // clang-format off
4339 PacketFragments packet = {
4340 // public flags (8 byte connection_id)
4341 {"",
4342 {0x28}},
4343 // connection_id
4344 {"",
4345 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
4346 // packet number
4347 {"",
4348 {0x12, 0x34, 0x56, 0x78}},
4349 // frame type (connection close frame)
4350 {"",
4351 {0x02}},
4352 // error code
4353 {"Unable to read connection close error code.",
4354 {0x00, 0x00, 0x00, 0x11}},
4355 {"Unable to read connection close error details.",
4356 {
4357 // error details length
4358 0x0, 0x0d,
4359 // error details
4360 'b', 'e', 'c', 'a',
4361 'u', 's', 'e', ' ',
4362 'I', ' ', 'c', 'a',
4363 'n'}
4364 }
4365 };
4366
QUICHE teama6ef0a62019-03-07 20:34:33 -05004367 PacketFragments packet46 = {
4368 // type (short header, 4 byte packet number)
4369 {"",
4370 {0x43}},
4371 // connection_id
4372 {"",
4373 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
4374 // packet number
4375 {"",
4376 {0x12, 0x34, 0x56, 0x78}},
4377 // frame type (connection close frame)
4378 {"",
4379 {0x02}},
4380 // error code
4381 {"Unable to read connection close error code.",
4382 {0x00, 0x00, 0x00, 0x11}},
4383 {"Unable to read connection close error details.",
4384 {
4385 // error details length
4386 0x0, 0x0d,
4387 // error details
4388 'b', 'e', 'c', 'a',
4389 'u', 's', 'e', ' ',
4390 'I', ' ', 'c', 'a',
4391 'n'}
4392 }
4393 };
4394
4395 PacketFragments packet99 = {
4396 // type (short header, 4 byte packet number)
4397 {"",
4398 {0x43}},
4399 // connection_id
4400 {"",
4401 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
4402 // packet number
4403 {"",
4404 {0x12, 0x34, 0x56, 0x78}},
fkastenholzb4dade72019-08-05 06:54:20 -07004405 // frame type (IETF Transport CONNECTION_CLOSE frame)
QUICHE teama6ef0a62019-03-07 20:34:33 -05004406 {"",
4407 {0x1c}},
4408 // error code
4409 {"Unable to read connection close error code.",
fkastenholzd57d3f92019-07-16 09:05:17 -07004410 {kVarInt62TwoBytes + 0x00, 0x11}},
QUICHE teama6ef0a62019-03-07 20:34:33 -05004411 {"Unable to read connection close frame type.",
4412 {kVarInt62TwoBytes + 0x12, 0x34 }},
4413 {"Unable to read connection close error details.",
4414 {
4415 // error details length
4416 kVarInt62OneByte + 0x0d,
4417 // error details
4418 'b', 'e', 'c', 'a',
4419 'u', 's', 'e', ' ',
4420 'I', ' ', 'c', 'a',
4421 'n'}
4422 }
4423 };
4424 // clang-format on
4425
4426 PacketFragments& fragments =
fkastenholz305e1732019-06-18 05:01:22 -07004427 VersionHasIetfQuicFrames(framer_.transport_version())
QUICHE teama6ef0a62019-03-07 20:34:33 -05004428 ? packet99
fayang36825da2019-08-21 14:01:27 -07004429 : (framer_.transport_version() >= QUIC_VERSION_46 ? packet46
4430 : packet);
QUICHE teama6ef0a62019-03-07 20:34:33 -05004431 std::unique_ptr<QuicEncryptedPacket> encrypted(
4432 AssemblePacketFromFragments(fragments));
4433 EXPECT_TRUE(framer_.ProcessPacket(*encrypted));
4434
bncf6f82b12019-10-30 07:01:01 -07004435 EXPECT_THAT(framer_.error(), IsQuicNoError());
QUICHE teama6ef0a62019-03-07 20:34:33 -05004436 ASSERT_TRUE(visitor_.header_.get());
4437 EXPECT_TRUE(CheckDecryption(
4438 *encrypted, !kIncludeVersion, !kIncludeDiversificationNonce,
4439 PACKET_8BYTE_CONNECTION_ID, PACKET_0BYTE_CONNECTION_ID));
4440
4441 EXPECT_EQ(0u, visitor_.stream_frames_.size());
zhongyiba6354a2019-04-10 16:49:25 -07004442 EXPECT_EQ(0x11u, static_cast<unsigned>(
4443 visitor_.connection_close_frame_.quic_error_code));
QUICHE teama6ef0a62019-03-07 20:34:33 -05004444 EXPECT_EQ("because I can", visitor_.connection_close_frame_.error_details);
fkastenholz305e1732019-06-18 05:01:22 -07004445 if (VersionHasIetfQuicFrames(framer_.transport_version())) {
fkastenholze9d71a82019-04-09 05:12:13 -07004446 EXPECT_EQ(0x1234u,
4447 visitor_.connection_close_frame_.transport_close_frame_type);
bncf6f82b12019-10-30 07:01:01 -07004448 EXPECT_THAT(visitor_.connection_close_frame_.extracted_error_code,
4449 IsError(QUIC_IETF_GQUIC_ERROR_MISSING));
fkastenholza14a7ae2019-08-07 05:21:22 -07004450 } else {
4451 // For Google QUIC closes, the error code is copied into
4452 // extracted_error_code.
4453 EXPECT_EQ(0x11u,
4454 static_cast<unsigned>(
4455 visitor_.connection_close_frame_.extracted_error_code));
fkastenholzb4dade72019-08-05 06:54:20 -07004456 }
4457
4458 ASSERT_EQ(0u, visitor_.ack_frames_.size());
4459
4460 CheckFramingBoundaries(fragments, QUIC_INVALID_CONNECTION_CLOSE_DATA);
4461}
4462
4463// As above, but checks that for Google-QUIC, if there happens
4464// to be an ErrorCode string at the start of the details, it is
4465// NOT extracted/parsed/folded/spindled/and/mutilated.
4466TEST_P(QuicFramerTest, ConnectionCloseFrameWithExtractedInfoIgnoreGCuic) {
4467 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
4468
4469 // clang-format off
4470 PacketFragments packet = {
4471 // public flags (8 byte connection_id)
4472 {"",
4473 {0x28}},
4474 // connection_id
4475 {"",
4476 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
4477 // packet number
4478 {"",
4479 {0x12, 0x34, 0x56, 0x78}},
4480 // frame type (connection close frame)
4481 {"",
4482 {0x02}},
4483 // error code
4484 {"Unable to read connection close error code.",
4485 {0x00, 0x00, 0x00, 0x11}},
4486 {"Unable to read connection close error details.",
4487 {
4488 // error details length
4489 0x0, 0x13,
4490 // error details
4491 '1', '7', '7', '6',
4492 '7', ':', 'b', 'e',
4493 'c', 'a', 'u', 's',
4494 'e', ' ', 'I', ' ',
4495 'c', 'a', 'n'}
4496 }
4497 };
4498
fkastenholzb4dade72019-08-05 06:54:20 -07004499 PacketFragments packet46 = {
4500 // type (short header, 4 byte packet number)
4501 {"",
4502 {0x43}},
4503 // connection_id
4504 {"",
4505 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
4506 // packet number
4507 {"",
4508 {0x12, 0x34, 0x56, 0x78}},
4509 // frame type (connection close frame)
4510 {"",
4511 {0x02}},
4512 // error code
4513 {"Unable to read connection close error code.",
4514 {0x00, 0x00, 0x00, 0x11}},
4515 {"Unable to read connection close error details.",
4516 {
4517 // error details length
4518 0x0, 0x13,
4519 // error details
4520 '1', '7', '7', '6',
4521 '7', ':', 'b', 'e',
4522 'c', 'a', 'u', 's',
4523 'e', ' ', 'I', ' ',
4524 'c', 'a', 'n'}
4525 }
4526 };
4527
4528 PacketFragments packet99 = {
4529 // type (short header, 4 byte packet number)
4530 {"",
4531 {0x43}},
4532 // connection_id
4533 {"",
4534 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
4535 // packet number
4536 {"",
4537 {0x12, 0x34, 0x56, 0x78}},
4538 // frame type (IETF Transport CONNECTION_CLOSE frame)
4539 {"",
4540 {0x1c}},
4541 // error code
4542 {"Unable to read connection close error code.",
4543 {kVarInt62OneByte + 0x11}},
4544 {"Unable to read connection close frame type.",
4545 {kVarInt62TwoBytes + 0x12, 0x34 }},
4546 {"Unable to read connection close error details.",
4547 {
4548 // error details length
4549 kVarInt62OneByte + 0x13,
4550 // error details
4551 '1', '7', '7', '6',
4552 '7', ':', 'b', 'e',
4553 'c', 'a', 'u', 's',
4554 'e', ' ', 'I', ' ',
4555 'c', 'a', 'n'}
4556 }
4557 };
4558 // clang-format on
4559
4560 PacketFragments& fragments =
4561 VersionHasIetfQuicFrames(framer_.transport_version())
4562 ? packet99
fayang36825da2019-08-21 14:01:27 -07004563 : (framer_.transport_version() >= QUIC_VERSION_46 ? packet46
4564 : packet);
fkastenholzb4dade72019-08-05 06:54:20 -07004565 std::unique_ptr<QuicEncryptedPacket> encrypted(
4566 AssemblePacketFromFragments(fragments));
4567 EXPECT_TRUE(framer_.ProcessPacket(*encrypted));
4568
bncf6f82b12019-10-30 07:01:01 -07004569 EXPECT_THAT(framer_.error(), IsQuicNoError());
fkastenholzb4dade72019-08-05 06:54:20 -07004570 ASSERT_TRUE(visitor_.header_.get());
4571 EXPECT_TRUE(CheckDecryption(
4572 *encrypted, !kIncludeVersion, !kIncludeDiversificationNonce,
4573 PACKET_8BYTE_CONNECTION_ID, PACKET_0BYTE_CONNECTION_ID));
4574
4575 EXPECT_EQ(0u, visitor_.stream_frames_.size());
4576 EXPECT_EQ(0x11u, static_cast<unsigned>(
4577 visitor_.connection_close_frame_.quic_error_code));
fkastenholz488a4622019-08-26 06:24:46 -07004578
fkastenholzb4dade72019-08-05 06:54:20 -07004579 if (VersionHasIetfQuicFrames(framer_.transport_version())) {
4580 EXPECT_EQ(0x1234u,
4581 visitor_.connection_close_frame_.transport_close_frame_type);
4582 EXPECT_EQ(17767u, visitor_.connection_close_frame_.extracted_error_code);
fkastenholz488a4622019-08-26 06:24:46 -07004583 EXPECT_EQ("because I can", visitor_.connection_close_frame_.error_details);
fkastenholzb4dade72019-08-05 06:54:20 -07004584 } else {
fkastenholza14a7ae2019-08-07 05:21:22 -07004585 EXPECT_EQ(0x11u, visitor_.connection_close_frame_.extracted_error_code);
fkastenholz488a4622019-08-26 06:24:46 -07004586 // Error code is not prepended in GQUIC, so it is not removed and should
4587 // remain in the reason phrase.
4588 EXPECT_EQ("17767:because I can",
4589 visitor_.connection_close_frame_.error_details);
QUICHE teama6ef0a62019-03-07 20:34:33 -05004590 }
4591
4592 ASSERT_EQ(0u, visitor_.ack_frames_.size());
4593
4594 CheckFramingBoundaries(fragments, QUIC_INVALID_CONNECTION_CLOSE_DATA);
4595}
4596
fkastenholz04bd4f32019-04-16 12:24:38 -07004597// Test the CONNECTION_CLOSE/Application variant.
QUICHE teama6ef0a62019-03-07 20:34:33 -05004598TEST_P(QuicFramerTest, ApplicationCloseFrame) {
fkastenholz305e1732019-06-18 05:01:22 -07004599 if (!VersionHasIetfQuicFrames(framer_.transport_version())) {
fkastenholzc5e0c8c2019-09-30 10:16:30 -07004600 // This frame is only in IETF QUIC.
QUICHE teama6ef0a62019-03-07 20:34:33 -05004601 return;
4602 }
zhongyi546cc452019-04-12 15:27:49 -07004603 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
QUICHE teama6ef0a62019-03-07 20:34:33 -05004604
4605 // clang-format off
4606 PacketFragments packet99 = {
4607 // type (short header, 4 byte packet number)
4608 {"",
4609 {0x43}},
4610 // connection_id
4611 {"",
4612 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
4613 // packet number
4614 {"",
4615 {0x12, 0x34, 0x56, 0x78}},
fkastenholz72f509b2019-04-10 09:17:49 -07004616 // frame type (IETF_CONNECTION_CLOSE/Application frame)
QUICHE teama6ef0a62019-03-07 20:34:33 -05004617 {"",
4618 {0x1d}},
4619 // error code
fkastenholz72f509b2019-04-10 09:17:49 -07004620 {"Unable to read connection close error code.",
fkastenholzd57d3f92019-07-16 09:05:17 -07004621 {kVarInt62TwoBytes + 0x00, 0x11}},
fkastenholz72f509b2019-04-10 09:17:49 -07004622 {"Unable to read connection close error details.",
QUICHE teama6ef0a62019-03-07 20:34:33 -05004623 {
4624 // error details length
4625 kVarInt62OneByte + 0x0d,
4626 // error details
4627 'b', 'e', 'c', 'a',
4628 'u', 's', 'e', ' ',
4629 'I', ' ', 'c', 'a',
4630 'n'}
4631 }
4632 };
4633 // clang-format on
4634
4635 std::unique_ptr<QuicEncryptedPacket> encrypted(
4636 AssemblePacketFromFragments(packet99));
4637 EXPECT_TRUE(framer_.ProcessPacket(*encrypted));
4638
bncf6f82b12019-10-30 07:01:01 -07004639 EXPECT_THAT(framer_.error(), IsQuicNoError());
QUICHE teama6ef0a62019-03-07 20:34:33 -05004640 ASSERT_TRUE(visitor_.header_.get());
4641 EXPECT_TRUE(CheckDecryption(
4642 *encrypted, !kIncludeVersion, !kIncludeDiversificationNonce,
4643 PACKET_8BYTE_CONNECTION_ID, PACKET_0BYTE_CONNECTION_ID));
4644
4645 EXPECT_EQ(0u, visitor_.stream_frames_.size());
4646
fkastenholz72f509b2019-04-10 09:17:49 -07004647 EXPECT_EQ(IETF_QUIC_APPLICATION_CONNECTION_CLOSE,
4648 visitor_.connection_close_frame_.close_type);
4649 EXPECT_EQ(122u, visitor_.connection_close_frame_.extracted_error_code);
fkastenholz88d08f42019-09-06 07:38:04 -07004650 EXPECT_EQ(0x11u, visitor_.connection_close_frame_.quic_error_code);
fkastenholz72f509b2019-04-10 09:17:49 -07004651 EXPECT_EQ("because I can", visitor_.connection_close_frame_.error_details);
QUICHE teama6ef0a62019-03-07 20:34:33 -05004652
4653 ASSERT_EQ(0u, visitor_.ack_frames_.size());
4654
fkastenholz04bd4f32019-04-16 12:24:38 -07004655 CheckFramingBoundaries(packet99, QUIC_INVALID_CONNECTION_CLOSE_DATA);
QUICHE teama6ef0a62019-03-07 20:34:33 -05004656}
4657
fkastenholzb4dade72019-08-05 06:54:20 -07004658// Check that we can extract an error code from an application close.
4659TEST_P(QuicFramerTest, ApplicationCloseFrameExtract) {
4660 if (!VersionHasIetfQuicFrames(framer_.transport_version())) {
fkastenholzc5e0c8c2019-09-30 10:16:30 -07004661 // This frame is only in IETF QUIC.
fkastenholzb4dade72019-08-05 06:54:20 -07004662 return;
4663 }
4664 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
4665
4666 // clang-format off
4667 PacketFragments packet99 = {
4668 // type (short header, 4 byte packet number)
4669 {"",
4670 {0x43}},
4671 // connection_id
4672 {"",
4673 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
4674 // packet number
4675 {"",
4676 {0x12, 0x34, 0x56, 0x78}},
4677 // frame type (IETF_CONNECTION_CLOSE/Application frame)
4678 {"",
4679 {0x1d}},
4680 // error code
4681 {"Unable to read connection close error code.",
4682 {kVarInt62OneByte + 0x11}},
4683 {"Unable to read connection close error details.",
4684 {
4685 // error details length
4686 kVarInt62OneByte + 0x13,
4687 // error details
4688 '1', '7', '7', '6',
4689 '7', ':', 'b', 'e',
4690 'c', 'a', 'u', 's',
4691 'e', ' ', 'I', ' ',
4692 'c', 'a', 'n'}
4693 }
4694 };
4695 // clang-format on
4696
4697 std::unique_ptr<QuicEncryptedPacket> encrypted(
4698 AssemblePacketFromFragments(packet99));
4699 EXPECT_TRUE(framer_.ProcessPacket(*encrypted));
4700
bncf6f82b12019-10-30 07:01:01 -07004701 EXPECT_THAT(framer_.error(), IsQuicNoError());
fkastenholzb4dade72019-08-05 06:54:20 -07004702 ASSERT_TRUE(visitor_.header_.get());
4703 EXPECT_TRUE(CheckDecryption(
4704 *encrypted, !kIncludeVersion, !kIncludeDiversificationNonce,
4705 PACKET_8BYTE_CONNECTION_ID, PACKET_0BYTE_CONNECTION_ID));
4706
4707 EXPECT_EQ(0u, visitor_.stream_frames_.size());
4708
4709 EXPECT_EQ(IETF_QUIC_APPLICATION_CONNECTION_CLOSE,
4710 visitor_.connection_close_frame_.close_type);
4711 EXPECT_EQ(17767u, visitor_.connection_close_frame_.extracted_error_code);
4712 EXPECT_EQ(0x11u, visitor_.connection_close_frame_.quic_error_code);
fkastenholz488a4622019-08-26 06:24:46 -07004713 EXPECT_EQ("because I can", visitor_.connection_close_frame_.error_details);
fkastenholzb4dade72019-08-05 06:54:20 -07004714
4715 ASSERT_EQ(0u, visitor_.ack_frames_.size());
4716
4717 CheckFramingBoundaries(packet99, QUIC_INVALID_CONNECTION_CLOSE_DATA);
4718}
4719
QUICHE teama6ef0a62019-03-07 20:34:33 -05004720TEST_P(QuicFramerTest, GoAwayFrame) {
fkastenholz305e1732019-06-18 05:01:22 -07004721 if (VersionHasIetfQuicFrames(framer_.transport_version())) {
fkastenholzc5e0c8c2019-09-30 10:16:30 -07004722 // This frame is not in IETF QUIC.
QUICHE teama6ef0a62019-03-07 20:34:33 -05004723 return;
4724 }
nharper9bb83462019-05-01 10:53:22 -07004725 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
QUICHE teama6ef0a62019-03-07 20:34:33 -05004726 // clang-format off
4727 PacketFragments packet = {
4728 // public flags (8 byte connection_id)
4729 {"",
4730 {0x28}},
4731 // connection_id
4732 {"",
4733 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
4734 // packet number
4735 {"",
4736 {0x12, 0x34, 0x56, 0x78}},
4737 // frame type (go away frame)
4738 {"",
4739 {0x03}},
4740 // error code
4741 {"Unable to read go away error code.",
4742 {0x00, 0x00, 0x00, 0x09}},
4743 // stream id
4744 {"Unable to read last good stream id.",
4745 {0x01, 0x02, 0x03, 0x04}},
4746 // stream id
4747 {"Unable to read goaway reason.",
4748 {
4749 // error details length
4750 0x0, 0x0d,
4751 // error details
4752 'b', 'e', 'c', 'a',
4753 'u', 's', 'e', ' ',
4754 'I', ' ', 'c', 'a',
4755 'n'}
4756 }
4757 };
4758
QUICHE teama6ef0a62019-03-07 20:34:33 -05004759 PacketFragments packet46 = {
4760 // type (short header, 4 byte packet number)
4761 {"",
4762 {0x43}},
4763 // connection_id
4764 {"",
4765 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
4766 // packet number
4767 {"",
4768 {0x12, 0x34, 0x56, 0x78}},
4769 // frame type (go away frame)
4770 {"",
4771 {0x03}},
4772 // error code
4773 {"Unable to read go away error code.",
4774 {0x00, 0x00, 0x00, 0x09}},
4775 // stream id
4776 {"Unable to read last good stream id.",
4777 {0x01, 0x02, 0x03, 0x04}},
4778 // stream id
4779 {"Unable to read goaway reason.",
4780 {
4781 // error details length
4782 0x0, 0x0d,
4783 // error details
4784 'b', 'e', 'c', 'a',
4785 'u', 's', 'e', ' ',
4786 'I', ' ', 'c', 'a',
4787 'n'}
4788 }
4789 };
4790 // clang-format on
4791
4792 PacketFragments& fragments =
fayang36825da2019-08-21 14:01:27 -07004793 framer_.transport_version() >= QUIC_VERSION_46 ? packet46 : packet;
QUICHE teama6ef0a62019-03-07 20:34:33 -05004794 std::unique_ptr<QuicEncryptedPacket> encrypted(
4795 AssemblePacketFromFragments(fragments));
4796 EXPECT_TRUE(framer_.ProcessPacket(*encrypted));
4797
bncf6f82b12019-10-30 07:01:01 -07004798 EXPECT_THAT(framer_.error(), IsQuicNoError());
QUICHE teama6ef0a62019-03-07 20:34:33 -05004799 ASSERT_TRUE(visitor_.header_.get());
4800 EXPECT_TRUE(CheckDecryption(
4801 *encrypted, !kIncludeVersion, !kIncludeDiversificationNonce,
4802 PACKET_8BYTE_CONNECTION_ID, PACKET_0BYTE_CONNECTION_ID));
4803
4804 EXPECT_EQ(kStreamId, visitor_.goaway_frame_.last_good_stream_id);
fkastenholz88d08f42019-09-06 07:38:04 -07004805 EXPECT_EQ(0x9u, visitor_.goaway_frame_.error_code);
QUICHE teama6ef0a62019-03-07 20:34:33 -05004806 EXPECT_EQ("because I can", visitor_.goaway_frame_.reason_phrase);
4807
4808 CheckFramingBoundaries(fragments, QUIC_INVALID_GOAWAY_DATA);
4809}
4810
4811TEST_P(QuicFramerTest, WindowUpdateFrame) {
fkastenholz305e1732019-06-18 05:01:22 -07004812 if (VersionHasIetfQuicFrames(framer_.transport_version())) {
fkastenholzc5e0c8c2019-09-30 10:16:30 -07004813 // This frame is not in IETF QUIC, see MaxDataFrame and MaxStreamDataFrame
4814 // for IETF QUIC equivalents.
QUICHE teama6ef0a62019-03-07 20:34:33 -05004815 return;
4816 }
nharper9bb83462019-05-01 10:53:22 -07004817 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
QUICHE teama6ef0a62019-03-07 20:34:33 -05004818 // clang-format off
4819 PacketFragments packet = {
4820 // public flags (8 byte connection_id)
4821 {"",
4822 {0x28}},
4823 // connection_id
4824 {"",
4825 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
4826 // packet number
4827 {"",
4828 {0x12, 0x34, 0x56, 0x78}},
4829 // frame type (window update frame)
4830 {"",
4831 {0x04}},
4832 // stream id
4833 {"Unable to read stream_id.",
4834 {0x01, 0x02, 0x03, 0x04}},
4835 // byte offset
4836 {"Unable to read window byte_offset.",
4837 {0x3A, 0x98, 0xFE, 0xDC,
4838 0x32, 0x10, 0x76, 0x54}},
4839 };
4840
QUICHE teama6ef0a62019-03-07 20:34:33 -05004841 PacketFragments packet46 = {
4842 // type (short header, 4 byte packet number)
4843 {"",
4844 {0x43}},
4845 // connection_id
4846 {"",
4847 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
4848 // packet number
4849 {"",
4850 {0x12, 0x34, 0x56, 0x78}},
4851 // frame type (window update frame)
4852 {"",
4853 {0x04}},
4854 // stream id
4855 {"Unable to read stream_id.",
4856 {0x01, 0x02, 0x03, 0x04}},
4857 // byte offset
4858 {"Unable to read window byte_offset.",
4859 {0x3A, 0x98, 0xFE, 0xDC,
4860 0x32, 0x10, 0x76, 0x54}},
4861 };
4862
4863 // clang-format on
4864
4865 PacketFragments& fragments =
fayang36825da2019-08-21 14:01:27 -07004866 framer_.transport_version() >= QUIC_VERSION_46 ? packet46 : packet;
QUICHE teama6ef0a62019-03-07 20:34:33 -05004867 std::unique_ptr<QuicEncryptedPacket> encrypted(
4868 AssemblePacketFromFragments(fragments));
4869 EXPECT_TRUE(framer_.ProcessPacket(*encrypted));
4870
bncf6f82b12019-10-30 07:01:01 -07004871 EXPECT_THAT(framer_.error(), IsQuicNoError());
QUICHE teama6ef0a62019-03-07 20:34:33 -05004872 ASSERT_TRUE(visitor_.header_.get());
4873 EXPECT_TRUE(CheckDecryption(
4874 *encrypted, !kIncludeVersion, !kIncludeDiversificationNonce,
4875 PACKET_8BYTE_CONNECTION_ID, PACKET_0BYTE_CONNECTION_ID));
4876
4877 EXPECT_EQ(kStreamId, visitor_.window_update_frame_.stream_id);
renjietangd088eab2019-11-21 14:54:41 -08004878 EXPECT_EQ(kStreamOffset, visitor_.window_update_frame_.max_data);
QUICHE teama6ef0a62019-03-07 20:34:33 -05004879
4880 CheckFramingBoundaries(fragments, QUIC_INVALID_WINDOW_UPDATE_DATA);
4881}
4882
4883TEST_P(QuicFramerTest, MaxDataFrame) {
fkastenholz305e1732019-06-18 05:01:22 -07004884 if (!VersionHasIetfQuicFrames(framer_.transport_version())) {
fkastenholzc5e0c8c2019-09-30 10:16:30 -07004885 // This frame is available only in IETF QUIC.
QUICHE teama6ef0a62019-03-07 20:34:33 -05004886 return;
4887 }
zhongyi546cc452019-04-12 15:27:49 -07004888 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
QUICHE teama6ef0a62019-03-07 20:34:33 -05004889 // clang-format off
4890 PacketFragments packet99 = {
4891 // type (short header, 4 byte packet number)
4892 {"",
4893 {0x43}},
4894 // connection_id
4895 {"",
4896 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
4897 // packet number
4898 {"",
4899 {0x12, 0x34, 0x56, 0x78}},
4900 // frame type (IETF_MAX_DATA frame)
4901 {"",
4902 {0x10}},
4903 // byte offset
4904 {"Can not read MAX_DATA byte-offset",
4905 {kVarInt62EightBytes + 0x3A, 0x98, 0xFE, 0xDC,
4906 0x32, 0x10, 0x76, 0x54}},
4907 };
4908 // clang-format on
4909
4910 std::unique_ptr<QuicEncryptedPacket> encrypted(
4911 AssemblePacketFromFragments(packet99));
4912 EXPECT_TRUE(framer_.ProcessPacket(*encrypted));
4913
bncf6f82b12019-10-30 07:01:01 -07004914 EXPECT_THAT(framer_.error(), IsQuicNoError());
QUICHE teama6ef0a62019-03-07 20:34:33 -05004915 ASSERT_TRUE(visitor_.header_.get());
4916 EXPECT_TRUE(CheckDecryption(
4917 *encrypted, !kIncludeVersion, !kIncludeDiversificationNonce,
4918 PACKET_8BYTE_CONNECTION_ID, PACKET_0BYTE_CONNECTION_ID));
4919
4920 EXPECT_EQ(QuicUtils::GetInvalidStreamId(framer_.transport_version()),
4921 visitor_.window_update_frame_.stream_id);
renjietangd088eab2019-11-21 14:54:41 -08004922 EXPECT_EQ(kStreamOffset, visitor_.window_update_frame_.max_data);
QUICHE teama6ef0a62019-03-07 20:34:33 -05004923
4924 CheckFramingBoundaries(packet99, QUIC_INVALID_MAX_DATA_FRAME_DATA);
4925}
4926
4927TEST_P(QuicFramerTest, MaxStreamDataFrame) {
fkastenholz305e1732019-06-18 05:01:22 -07004928 if (!VersionHasIetfQuicFrames(framer_.transport_version())) {
fkastenholzc5e0c8c2019-09-30 10:16:30 -07004929 // This frame available only in IETF QUIC.
QUICHE teama6ef0a62019-03-07 20:34:33 -05004930 return;
4931 }
zhongyi546cc452019-04-12 15:27:49 -07004932 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
QUICHE teama6ef0a62019-03-07 20:34:33 -05004933 // clang-format off
4934 PacketFragments packet99 = {
4935 // type (short header, 4 byte packet number)
4936 {"",
4937 {0x43}},
4938 // connection_id
4939 {"",
4940 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
4941 // packet number
4942 {"",
4943 {0x12, 0x34, 0x56, 0x78}},
4944 // frame type (IETF_MAX_STREAM_DATA frame)
4945 {"",
4946 {0x11}},
4947 // stream id
4948 {"Can not read MAX_STREAM_DATA stream id",
4949 {kVarInt62FourBytes + 0x01, 0x02, 0x03, 0x04}},
4950 // byte offset
4951 {"Can not read MAX_STREAM_DATA byte-count",
4952 {kVarInt62EightBytes + 0x3A, 0x98, 0xFE, 0xDC,
4953 0x32, 0x10, 0x76, 0x54}},
4954 };
4955 // clang-format on
4956
4957 std::unique_ptr<QuicEncryptedPacket> encrypted(
4958 AssemblePacketFromFragments(packet99));
4959 EXPECT_TRUE(framer_.ProcessPacket(*encrypted));
4960
bncf6f82b12019-10-30 07:01:01 -07004961 EXPECT_THAT(framer_.error(), IsQuicNoError());
QUICHE teama6ef0a62019-03-07 20:34:33 -05004962 ASSERT_TRUE(visitor_.header_.get());
4963 EXPECT_TRUE(CheckDecryption(
4964 *encrypted, !kIncludeVersion, !kIncludeDiversificationNonce,
4965 PACKET_8BYTE_CONNECTION_ID, PACKET_0BYTE_CONNECTION_ID));
4966
4967 EXPECT_EQ(kStreamId, visitor_.window_update_frame_.stream_id);
renjietangd088eab2019-11-21 14:54:41 -08004968 EXPECT_EQ(kStreamOffset, visitor_.window_update_frame_.max_data);
QUICHE teama6ef0a62019-03-07 20:34:33 -05004969
4970 CheckFramingBoundaries(packet99, QUIC_INVALID_MAX_STREAM_DATA_FRAME_DATA);
4971}
4972
4973TEST_P(QuicFramerTest, BlockedFrame) {
zhongyi546cc452019-04-12 15:27:49 -07004974 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
QUICHE teama6ef0a62019-03-07 20:34:33 -05004975 // clang-format off
4976 PacketFragments packet = {
4977 // public flags (8 byte connection_id)
4978 {"",
4979 {0x28}},
4980 // connection_id
4981 {"",
4982 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
4983 // packet number
4984 {"",
4985 {0x12, 0x34, 0x56, 0x78}},
4986 // frame type (blocked frame)
4987 {"",
4988 {0x05}},
4989 // stream id
4990 {"Unable to read stream_id.",
4991 {0x01, 0x02, 0x03, 0x04}},
4992 };
4993
QUICHE teama6ef0a62019-03-07 20:34:33 -05004994 PacketFragments packet46 = {
4995 // type (short header, 4 byte packet number)
4996 {"",
4997 {0x43}},
4998 // connection_id
4999 {"",
5000 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
5001 // packet number
5002 {"",
5003 {0x12, 0x34, 0x56, 0x78}},
5004 // frame type (blocked frame)
5005 {"",
5006 {0x05}},
5007 // stream id
5008 {"Unable to read stream_id.",
5009 {0x01, 0x02, 0x03, 0x04}},
5010 };
5011
5012 PacketFragments packet99 = {
5013 // type (short header, 4 byte packet number)
5014 {"",
5015 {0x43}},
5016 // connection_id
5017 {"",
5018 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
5019 // packet number
5020 {"",
5021 {0x12, 0x34, 0x56, 0x78}},
5022 // frame type (IETF_STREAM_BLOCKED frame)
5023 {"",
5024 {0x15}},
5025 // stream id
5026 {"Can not read stream blocked stream id.",
5027 {kVarInt62FourBytes + 0x01, 0x02, 0x03, 0x04}},
5028 // Offset
5029 {"Can not read stream blocked offset.",
5030 {kVarInt62EightBytes + 0x3a, 0x98, 0xFE, 0xDC, 0x32, 0x10, 0x76, 0x54}},
5031 };
5032 // clang-format on
5033
5034 PacketFragments& fragments =
fkastenholz305e1732019-06-18 05:01:22 -07005035 VersionHasIetfQuicFrames(framer_.transport_version())
QUICHE teama6ef0a62019-03-07 20:34:33 -05005036 ? packet99
fayang36825da2019-08-21 14:01:27 -07005037 : (framer_.transport_version() >= QUIC_VERSION_46 ? packet46
5038 : packet);
QUICHE teama6ef0a62019-03-07 20:34:33 -05005039 std::unique_ptr<QuicEncryptedPacket> encrypted(
5040 AssemblePacketFromFragments(fragments));
5041 EXPECT_TRUE(framer_.ProcessPacket(*encrypted));
5042
bncf6f82b12019-10-30 07:01:01 -07005043 EXPECT_THAT(framer_.error(), IsQuicNoError());
QUICHE teama6ef0a62019-03-07 20:34:33 -05005044 ASSERT_TRUE(visitor_.header_.get());
5045 EXPECT_TRUE(CheckDecryption(
5046 *encrypted, !kIncludeVersion, !kIncludeDiversificationNonce,
5047 PACKET_8BYTE_CONNECTION_ID, PACKET_0BYTE_CONNECTION_ID));
5048
fkastenholz305e1732019-06-18 05:01:22 -07005049 if (VersionHasIetfQuicFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05005050 EXPECT_EQ(kStreamOffset, visitor_.blocked_frame_.offset);
5051 } else {
5052 EXPECT_EQ(0u, visitor_.blocked_frame_.offset);
5053 }
5054 EXPECT_EQ(kStreamId, visitor_.blocked_frame_.stream_id);
5055
fkastenholz305e1732019-06-18 05:01:22 -07005056 if (VersionHasIetfQuicFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05005057 CheckFramingBoundaries(fragments, QUIC_INVALID_STREAM_BLOCKED_DATA);
5058 } else {
5059 CheckFramingBoundaries(fragments, QUIC_INVALID_BLOCKED_DATA);
5060 }
5061}
5062
5063TEST_P(QuicFramerTest, PingFrame) {
zhongyi546cc452019-04-12 15:27:49 -07005064 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
QUICHE teama6ef0a62019-03-07 20:34:33 -05005065 // clang-format off
5066 unsigned char packet[] = {
5067 // public flags (8 byte connection_id)
5068 0x28,
5069 // connection_id
5070 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
5071 // packet number
5072 0x12, 0x34, 0x56, 0x78,
5073
5074 // frame type (ping frame)
5075 0x07,
5076 };
5077
QUICHE teama6ef0a62019-03-07 20:34:33 -05005078 unsigned char packet46[] = {
5079 // type (short header, 4 byte packet number)
5080 0x43,
5081 // connection_id
5082 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
5083 // packet number
5084 0x12, 0x34, 0x56, 0x78,
5085
5086 // frame type
5087 0x07,
5088 };
5089
5090 unsigned char packet99[] = {
5091 // type (short header, 4 byte packet number)
5092 0x43,
5093 // connection_id
5094 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
5095 // packet number
5096 0x12, 0x34, 0x56, 0x78,
5097
5098 // frame type (IETF_PING frame)
5099 0x01,
5100 };
5101 // clang-format on
5102
5103 QuicEncryptedPacket encrypted(
fkastenholz305e1732019-06-18 05:01:22 -07005104 AsChars(VersionHasIetfQuicFrames(framer_.transport_version())
QUICHE teama6ef0a62019-03-07 20:34:33 -05005105 ? packet99
fayang36825da2019-08-21 14:01:27 -07005106 : (framer_.transport_version() >= QUIC_VERSION_46 ? packet46
5107 : packet)),
fkastenholz305e1732019-06-18 05:01:22 -07005108 VersionHasIetfQuicFrames(framer_.transport_version())
bnc4e9283d2019-12-17 07:08:57 -08005109 ? QUICHE_ARRAYSIZE(packet99)
fayang36825da2019-08-21 14:01:27 -07005110 : (framer_.transport_version() >= QUIC_VERSION_46
bnc4e9283d2019-12-17 07:08:57 -08005111 ? QUICHE_ARRAYSIZE(packet46)
5112 : QUICHE_ARRAYSIZE(packet)),
QUICHE teama6ef0a62019-03-07 20:34:33 -05005113 false);
5114 EXPECT_TRUE(framer_.ProcessPacket(encrypted));
5115
bncf6f82b12019-10-30 07:01:01 -07005116 EXPECT_THAT(framer_.error(), IsQuicNoError());
QUICHE teama6ef0a62019-03-07 20:34:33 -05005117 ASSERT_TRUE(visitor_.header_.get());
5118 EXPECT_TRUE(CheckDecryption(
5119 encrypted, !kIncludeVersion, !kIncludeDiversificationNonce,
5120 PACKET_8BYTE_CONNECTION_ID, PACKET_0BYTE_CONNECTION_ID));
5121
5122 EXPECT_EQ(1u, visitor_.ping_frames_.size());
5123
5124 // No need to check the PING frame boundaries because it has no payload.
5125}
5126
fayang01062942020-01-22 07:23:23 -08005127TEST_P(QuicFramerTest, HandshakeDoneFrame) {
5128 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
5129 // clang-format off
5130 unsigned char packet[] = {
5131 // type (short header, 4 byte packet number)
5132 0x43,
5133 // connection_id
5134 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
5135 // packet number
5136 0x12, 0x34, 0x56, 0x78,
5137
5138 // frame type (Handshake done frame)
5139 0x1e,
5140 };
5141 // clang-format on
5142
5143 if (!VersionHasIetfQuicFrames(framer_.transport_version())) {
5144 return;
5145 }
5146
5147 QuicEncryptedPacket encrypted(AsChars(packet), QUICHE_ARRAYSIZE(packet),
5148 false);
5149 EXPECT_TRUE(framer_.ProcessPacket(encrypted));
5150
5151 EXPECT_THAT(framer_.error(), IsQuicNoError());
5152 ASSERT_TRUE(visitor_.header_.get());
5153 EXPECT_TRUE(CheckDecryption(
5154 encrypted, !kIncludeVersion, !kIncludeDiversificationNonce,
5155 PACKET_8BYTE_CONNECTION_ID, PACKET_0BYTE_CONNECTION_ID));
5156
5157 EXPECT_EQ(1u, visitor_.handshake_done_frames_.size());
5158}
5159
QUICHE teama6ef0a62019-03-07 20:34:33 -05005160TEST_P(QuicFramerTest, MessageFrame) {
dschinazicd86dd12019-11-14 10:11:13 -08005161 if (!VersionSupportsMessageFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05005162 return;
5163 }
zhongyi546cc452019-04-12 15:27:49 -07005164 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
QUICHE teama6ef0a62019-03-07 20:34:33 -05005165 // clang-format off
QUICHE teama6ef0a62019-03-07 20:34:33 -05005166 PacketFragments packet46 = {
5167 // type (short header, 4 byte packet number)
5168 {"",
5169 {0x43}},
5170 // connection_id
5171 {"",
5172 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
5173 // packet number
5174 {"",
5175 {0x12, 0x34, 0x56, 0x78}},
5176 // message frame type.
5177 {"",
5178 { 0x21 }},
5179 // message length
5180 {"Unable to read message length",
5181 {0x07}},
5182 // message data
5183 {"Unable to read message data",
5184 {'m', 'e', 's', 's', 'a', 'g', 'e'}},
5185 // message frame no length.
5186 {"",
5187 { 0x20 }},
5188 // message data
5189 {{},
5190 {'m', 'e', 's', 's', 'a', 'g', 'e', '2'}},
5191 };
dschinazicd86dd12019-11-14 10:11:13 -08005192 PacketFragments packet99 = {
5193 // type (short header, 4 byte packet number)
5194 {"",
5195 {0x43}},
5196 // connection_id
5197 {"",
5198 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
5199 // packet number
5200 {"",
5201 {0x12, 0x34, 0x56, 0x78}},
5202 // message frame type.
5203 {"",
5204 { 0x31 }},
5205 // message length
5206 {"Unable to read message length",
5207 {0x07}},
5208 // message data
5209 {"Unable to read message data",
5210 {'m', 'e', 's', 's', 'a', 'g', 'e'}},
5211 // message frame no length.
5212 {"",
5213 { 0x30 }},
5214 // message data
5215 {{},
5216 {'m', 'e', 's', 's', 'a', 'g', 'e', '2'}},
5217 };
QUICHE teama6ef0a62019-03-07 20:34:33 -05005218 // clang-format on
5219
dschinazicd86dd12019-11-14 10:11:13 -08005220 std::unique_ptr<QuicEncryptedPacket> encrypted;
5221 if (VersionHasIetfQuicFrames(framer_.transport_version())) {
5222 encrypted = AssemblePacketFromFragments(packet99);
5223 } else {
5224 encrypted = AssemblePacketFromFragments(packet46);
5225 }
QUICHE teama6ef0a62019-03-07 20:34:33 -05005226 EXPECT_TRUE(framer_.ProcessPacket(*encrypted));
5227
bncf6f82b12019-10-30 07:01:01 -07005228 EXPECT_THAT(framer_.error(), IsQuicNoError());
QUICHE teama6ef0a62019-03-07 20:34:33 -05005229 ASSERT_TRUE(visitor_.header_.get());
5230 EXPECT_TRUE(CheckDecryption(
5231 *encrypted, !kIncludeVersion, !kIncludeDiversificationNonce,
5232 PACKET_8BYTE_CONNECTION_ID, PACKET_0BYTE_CONNECTION_ID));
5233
5234 ASSERT_EQ(2u, visitor_.message_frames_.size());
5235 EXPECT_EQ(7u, visitor_.message_frames_[0]->message_length);
5236 EXPECT_EQ(8u, visitor_.message_frames_[1]->message_length);
5237
dschinazicd86dd12019-11-14 10:11:13 -08005238 if (VersionHasIetfQuicFrames(framer_.transport_version())) {
5239 CheckFramingBoundaries(packet99, QUIC_INVALID_MESSAGE_DATA);
5240 } else {
5241 CheckFramingBoundaries(packet46, QUIC_INVALID_MESSAGE_DATA);
5242 }
QUICHE teama6ef0a62019-03-07 20:34:33 -05005243}
5244
5245TEST_P(QuicFramerTest, PublicResetPacketV33) {
5246 // clang-format off
5247 PacketFragments packet = {
5248 // public flags (public reset, 8 byte connection_id)
5249 {"",
5250 {0x0A}},
5251 // connection_id
5252 {"",
5253 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
5254 {"Unable to read reset message.",
5255 {
5256 // message tag (kPRST)
5257 'P', 'R', 'S', 'T',
5258 // num_entries (2) + padding
5259 0x02, 0x00, 0x00, 0x00,
5260 // tag kRNON
5261 'R', 'N', 'O', 'N',
5262 // end offset 8
5263 0x08, 0x00, 0x00, 0x00,
5264 // tag kRSEQ
5265 'R', 'S', 'E', 'Q',
5266 // end offset 16
5267 0x10, 0x00, 0x00, 0x00,
5268 // nonce proof
5269 0x89, 0x67, 0x45, 0x23,
5270 0x01, 0xEF, 0xCD, 0xAB,
5271 // rejected packet number
5272 0xBC, 0x9A, 0x78, 0x56,
5273 0x34, 0x12, 0x00, 0x00,
5274 }
5275 }
5276 };
5277 // clang-format on
5278 if (framer_.transport_version() > QUIC_VERSION_43) {
5279 return;
5280 }
5281
5282 std::unique_ptr<QuicEncryptedPacket> encrypted(
5283 AssemblePacketFromFragments(packet));
5284 EXPECT_TRUE(framer_.ProcessPacket(*encrypted));
bncf6f82b12019-10-30 07:01:01 -07005285 ASSERT_THAT(framer_.error(), IsQuicNoError());
QUICHE teama6ef0a62019-03-07 20:34:33 -05005286 ASSERT_TRUE(visitor_.public_reset_packet_.get());
5287 EXPECT_EQ(FramerTestConnectionId(),
5288 visitor_.public_reset_packet_->connection_id);
5289 EXPECT_EQ(kNonceProof, visitor_.public_reset_packet_->nonce_proof);
5290 EXPECT_EQ(
5291 IpAddressFamily::IP_UNSPEC,
5292 visitor_.public_reset_packet_->client_address.host().address_family());
5293
5294 CheckFramingBoundaries(packet, QUIC_INVALID_PUBLIC_RST_PACKET);
5295}
5296
5297TEST_P(QuicFramerTest, PublicResetPacket) {
5298 QuicFramerPeer::SetPerspective(&framer_, Perspective::IS_CLIENT);
5299
5300 // clang-format off
5301 PacketFragments packet = {
5302 // public flags (public reset, 8 byte connection_id)
5303 {"",
5304 {0x0E}},
5305 // connection_id
5306 {"",
5307 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
5308 {"Unable to read reset message.",
5309 {
5310 // message tag (kPRST)
5311 'P', 'R', 'S', 'T',
5312 // num_entries (2) + padding
5313 0x02, 0x00, 0x00, 0x00,
5314 // tag kRNON
5315 'R', 'N', 'O', 'N',
5316 // end offset 8
5317 0x08, 0x00, 0x00, 0x00,
5318 // tag kRSEQ
5319 'R', 'S', 'E', 'Q',
5320 // end offset 16
5321 0x10, 0x00, 0x00, 0x00,
5322 // nonce proof
5323 0x89, 0x67, 0x45, 0x23,
5324 0x01, 0xEF, 0xCD, 0xAB,
5325 // rejected packet number
5326 0xBC, 0x9A, 0x78, 0x56,
5327 0x34, 0x12, 0x00, 0x00,
5328 }
5329 }
5330 };
5331 // clang-format on
5332
5333 if (framer_.transport_version() > QUIC_VERSION_43) {
5334 return;
5335 }
5336
5337 std::unique_ptr<QuicEncryptedPacket> encrypted(
5338 AssemblePacketFromFragments(packet));
5339 EXPECT_TRUE(framer_.ProcessPacket(*encrypted));
bncf6f82b12019-10-30 07:01:01 -07005340 ASSERT_THAT(framer_.error(), IsQuicNoError());
QUICHE teama6ef0a62019-03-07 20:34:33 -05005341 ASSERT_TRUE(visitor_.public_reset_packet_.get());
5342 EXPECT_EQ(FramerTestConnectionId(),
5343 visitor_.public_reset_packet_->connection_id);
5344 EXPECT_EQ(kNonceProof, visitor_.public_reset_packet_->nonce_proof);
5345 EXPECT_EQ(
5346 IpAddressFamily::IP_UNSPEC,
5347 visitor_.public_reset_packet_->client_address.host().address_family());
5348
5349 CheckFramingBoundaries(packet, QUIC_INVALID_PUBLIC_RST_PACKET);
5350}
5351
5352TEST_P(QuicFramerTest, PublicResetPacketWithTrailingJunk) {
5353 // clang-format off
5354 unsigned char packet[] = {
5355 // public flags (public reset, 8 byte connection_id)
5356 0x0A,
5357 // connection_id
5358 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
5359 // message tag (kPRST)
5360 'P', 'R', 'S', 'T',
5361 // num_entries (2) + padding
5362 0x02, 0x00, 0x00, 0x00,
5363 // tag kRNON
5364 'R', 'N', 'O', 'N',
5365 // end offset 8
5366 0x08, 0x00, 0x00, 0x00,
5367 // tag kRSEQ
5368 'R', 'S', 'E', 'Q',
5369 // end offset 16
5370 0x10, 0x00, 0x00, 0x00,
5371 // nonce proof
5372 0x89, 0x67, 0x45, 0x23,
5373 0x01, 0xEF, 0xCD, 0xAB,
5374 // rejected packet number
5375 0xBC, 0x9A, 0x78, 0x56,
5376 0x34, 0x12, 0x00, 0x00,
5377 // trailing junk
5378 'j', 'u', 'n', 'k',
5379 };
5380 // clang-format on
5381 if (framer_.transport_version() > QUIC_VERSION_43) {
5382 return;
5383 }
5384
bnc4e9283d2019-12-17 07:08:57 -08005385 QuicEncryptedPacket encrypted(AsChars(packet), QUICHE_ARRAYSIZE(packet),
5386 false);
QUICHE teama6ef0a62019-03-07 20:34:33 -05005387 EXPECT_FALSE(framer_.ProcessPacket(encrypted));
bncf54082a2019-11-27 10:19:47 -08005388 ASSERT_THAT(framer_.error(), IsError(QUIC_INVALID_PUBLIC_RST_PACKET));
QUICHE teama6ef0a62019-03-07 20:34:33 -05005389 EXPECT_EQ("Unable to read reset message.", framer_.detailed_error());
5390}
5391
5392TEST_P(QuicFramerTest, PublicResetPacketWithClientAddress) {
5393 // clang-format off
5394 PacketFragments packet = {
5395 // public flags (public reset, 8 byte connection_id)
5396 {"",
5397 {0x0A}},
5398 // connection_id
5399 {"",
5400 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
5401 {"Unable to read reset message.",
5402 {
5403 // message tag (kPRST)
5404 'P', 'R', 'S', 'T',
5405 // num_entries (2) + padding
5406 0x03, 0x00, 0x00, 0x00,
5407 // tag kRNON
5408 'R', 'N', 'O', 'N',
5409 // end offset 8
5410 0x08, 0x00, 0x00, 0x00,
5411 // tag kRSEQ
5412 'R', 'S', 'E', 'Q',
5413 // end offset 16
5414 0x10, 0x00, 0x00, 0x00,
5415 // tag kCADR
5416 'C', 'A', 'D', 'R',
5417 // end offset 24
5418 0x18, 0x00, 0x00, 0x00,
5419 // nonce proof
5420 0x89, 0x67, 0x45, 0x23,
5421 0x01, 0xEF, 0xCD, 0xAB,
5422 // rejected packet number
5423 0xBC, 0x9A, 0x78, 0x56,
5424 0x34, 0x12, 0x00, 0x00,
5425 // client address: 4.31.198.44:443
5426 0x02, 0x00,
5427 0x04, 0x1F, 0xC6, 0x2C,
5428 0xBB, 0x01,
5429 }
5430 }
5431 };
5432 // clang-format on
5433 if (framer_.transport_version() > QUIC_VERSION_43) {
5434 return;
5435 }
5436
5437 std::unique_ptr<QuicEncryptedPacket> encrypted(
5438 AssemblePacketFromFragments(packet));
5439 EXPECT_TRUE(framer_.ProcessPacket(*encrypted));
bncf6f82b12019-10-30 07:01:01 -07005440 ASSERT_THAT(framer_.error(), IsQuicNoError());
QUICHE teama6ef0a62019-03-07 20:34:33 -05005441 ASSERT_TRUE(visitor_.public_reset_packet_.get());
5442 EXPECT_EQ(FramerTestConnectionId(),
5443 visitor_.public_reset_packet_->connection_id);
5444 EXPECT_EQ(kNonceProof, visitor_.public_reset_packet_->nonce_proof);
5445 EXPECT_EQ("4.31.198.44",
5446 visitor_.public_reset_packet_->client_address.host().ToString());
5447 EXPECT_EQ(443, visitor_.public_reset_packet_->client_address.port());
5448
5449 CheckFramingBoundaries(packet, QUIC_INVALID_PUBLIC_RST_PACKET);
5450}
5451
5452TEST_P(QuicFramerTest, IetfStatelessResetPacket) {
5453 // clang-format off
5454 unsigned char packet[] = {
5455 // type (short packet, 1 byte packet number)
5456 0x50,
QUICHE teama6ef0a62019-03-07 20:34:33 -05005457 // Random bytes
5458 0x01, 0x11, 0x02, 0x22, 0x03, 0x33, 0x04, 0x44,
5459 0x01, 0x11, 0x02, 0x22, 0x03, 0x33, 0x04, 0x44,
5460 0x01, 0x11, 0x02, 0x22, 0x03, 0x33, 0x04, 0x44,
5461 0x01, 0x11, 0x02, 0x22, 0x03, 0x33, 0x04, 0x44,
5462 // stateless reset token
5463 0xB5, 0x69, 0x0F, 0x00, 0x00, 0x00, 0x00, 0x00,
5464 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
5465 };
5466 // clang-format on
5467 if (framer_.transport_version() <= QUIC_VERSION_43) {
5468 return;
5469 }
5470 QuicFramerPeer::SetPerspective(&framer_, Perspective::IS_CLIENT);
dschinazib953d022019-08-01 18:05:58 -07005471 QuicFramerPeer::SetLastSerializedServerConnectionId(&framer_,
5472 TestConnectionId(0x33));
QUICHE teama6ef0a62019-03-07 20:34:33 -05005473 decrypter_ = new test::TestDecrypter();
zhongyi546cc452019-04-12 15:27:49 -07005474 if (framer_.version().KnowsWhichDecrypterToUse()) {
vasilvv0fc587f2019-09-06 13:33:08 -07005475 framer_.InstallDecrypter(
5476 ENCRYPTION_INITIAL,
5477 std::make_unique<NullDecrypter>(Perspective::IS_CLIENT));
zhongyi546cc452019-04-12 15:27:49 -07005478 framer_.InstallDecrypter(ENCRYPTION_ZERO_RTT,
5479 std::unique_ptr<QuicDecrypter>(decrypter_));
5480 } else {
vasilvv0fc587f2019-09-06 13:33:08 -07005481 framer_.SetDecrypter(ENCRYPTION_INITIAL, std::make_unique<NullDecrypter>(
5482 Perspective::IS_CLIENT));
zhongyi546cc452019-04-12 15:27:49 -07005483 framer_.SetAlternativeDecrypter(
5484 ENCRYPTION_ZERO_RTT, std::unique_ptr<QuicDecrypter>(decrypter_), false);
5485 }
QUICHE teama6ef0a62019-03-07 20:34:33 -05005486 // This packet cannot be decrypted because diversification nonce is missing.
bnc4e9283d2019-12-17 07:08:57 -08005487 QuicEncryptedPacket encrypted(AsChars(packet), QUICHE_ARRAYSIZE(packet),
5488 false);
QUICHE teama6ef0a62019-03-07 20:34:33 -05005489 EXPECT_TRUE(framer_.ProcessPacket(encrypted));
bncf6f82b12019-10-30 07:01:01 -07005490 ASSERT_THAT(framer_.error(), IsQuicNoError());
QUICHE teama6ef0a62019-03-07 20:34:33 -05005491 ASSERT_TRUE(visitor_.stateless_reset_packet_.get());
5492 EXPECT_EQ(kTestStatelessResetToken,
5493 visitor_.stateless_reset_packet_->stateless_reset_token);
5494}
5495
5496TEST_P(QuicFramerTest, IetfStatelessResetPacketInvalidStatelessResetToken) {
5497 // clang-format off
5498 unsigned char packet[] = {
5499 // type (short packet, 1 byte packet number)
5500 0x50,
dschinazib953d022019-08-01 18:05:58 -07005501 // Random bytes
5502 0x01, 0x11, 0x02, 0x22, 0x03, 0x33, 0x04, 0x44,
5503 0x01, 0x11, 0x02, 0x22, 0x03, 0x33, 0x04, 0x44,
5504 0x01, 0x11, 0x02, 0x22, 0x03, 0x33, 0x04, 0x44,
5505 0x01, 0x11, 0x02, 0x22, 0x03, 0x33, 0x04, 0x44,
QUICHE teama6ef0a62019-03-07 20:34:33 -05005506 // stateless reset token
5507 0xB6, 0x69, 0x0F, 0x00, 0x00, 0x00, 0x00, 0x00,
5508 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
5509 };
5510 // clang-format on
5511 if (framer_.transport_version() <= QUIC_VERSION_43) {
5512 return;
5513 }
dschinazib953d022019-08-01 18:05:58 -07005514 QuicFramerPeer::SetLastSerializedServerConnectionId(&framer_,
5515 TestConnectionId(0x33));
QUICHE teama6ef0a62019-03-07 20:34:33 -05005516 QuicFramerPeer::SetPerspective(&framer_, Perspective::IS_CLIENT);
QUICHE teama6ef0a62019-03-07 20:34:33 -05005517 decrypter_ = new test::TestDecrypter();
zhongyi546cc452019-04-12 15:27:49 -07005518 if (framer_.version().KnowsWhichDecrypterToUse()) {
vasilvv0fc587f2019-09-06 13:33:08 -07005519 framer_.InstallDecrypter(
5520 ENCRYPTION_INITIAL,
5521 std::make_unique<NullDecrypter>(Perspective::IS_CLIENT));
zhongyi546cc452019-04-12 15:27:49 -07005522 framer_.InstallDecrypter(ENCRYPTION_ZERO_RTT,
5523 std::unique_ptr<QuicDecrypter>(decrypter_));
5524 } else {
vasilvv0fc587f2019-09-06 13:33:08 -07005525 framer_.SetDecrypter(ENCRYPTION_INITIAL, std::make_unique<NullDecrypter>(
5526 Perspective::IS_CLIENT));
zhongyi546cc452019-04-12 15:27:49 -07005527 framer_.SetAlternativeDecrypter(
5528 ENCRYPTION_ZERO_RTT, std::unique_ptr<QuicDecrypter>(decrypter_), false);
5529 }
QUICHE teama6ef0a62019-03-07 20:34:33 -05005530 // This packet cannot be decrypted because diversification nonce is missing.
bnc4e9283d2019-12-17 07:08:57 -08005531 QuicEncryptedPacket encrypted(AsChars(packet), QUICHE_ARRAYSIZE(packet),
5532 false);
QUICHE teama6ef0a62019-03-07 20:34:33 -05005533 EXPECT_FALSE(framer_.ProcessPacket(encrypted));
bncf6f82b12019-10-30 07:01:01 -07005534 EXPECT_THAT(framer_.error(), IsError(QUIC_DECRYPTION_FAILURE));
QUICHE teama6ef0a62019-03-07 20:34:33 -05005535 ASSERT_FALSE(visitor_.stateless_reset_packet_);
5536}
5537
dschinazi072da7c2019-05-07 17:57:42 -07005538TEST_P(QuicFramerTest, VersionNegotiationPacketClient) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05005539 // clang-format off
5540 PacketFragments packet = {
5541 // public flags (version, 8 byte connection_id)
5542 {"",
5543 {0x29}},
5544 // connection_id
5545 {"",
5546 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
5547 // version tag
5548 {"Unable to read supported version in negotiation.",
5549 {QUIC_VERSION_BYTES,
5550 'Q', '2', '.', '0'}},
5551 };
5552
fayang36825da2019-08-21 14:01:27 -07005553 PacketFragments packet46 = {
QUICHE teama6ef0a62019-03-07 20:34:33 -05005554 // type (long header)
5555 {"",
5556 {0x8F}},
5557 // version tag
5558 {"",
5559 {0x00, 0x00, 0x00, 0x00}},
5560 {"",
5561 {0x05}},
5562 // connection_id
5563 {"",
5564 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
5565 // Supported versions
5566 {"Unable to read supported version in negotiation.",
5567 {QUIC_VERSION_BYTES,
5568 'Q', '2', '.', '0'}},
5569 };
dschinazi48ac9192019-07-31 00:07:26 -07005570
dschinazic73506e2019-09-20 13:26:46 -07005571 PacketFragments packet49 = {
dschinazi48ac9192019-07-31 00:07:26 -07005572 // type (long header)
5573 {"",
5574 {0x8F}},
5575 // version tag
5576 {"",
5577 {0x00, 0x00, 0x00, 0x00}},
5578 {"",
5579 {0x08}},
5580 // connection_id
5581 {"",
5582 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
5583 {"",
5584 {0x00}},
5585 // Supported versions
5586 {"Unable to read supported version in negotiation.",
5587 {QUIC_VERSION_BYTES,
5588 'Q', '2', '.', '0'}},
5589 };
QUICHE teama6ef0a62019-03-07 20:34:33 -05005590 // clang-format on
5591
5592 QuicFramerPeer::SetPerspective(&framer_, Perspective::IS_CLIENT);
5593
5594 PacketFragments& fragments =
dschinazic73506e2019-09-20 13:26:46 -07005595 framer_.transport_version() >= QUIC_VERSION_49
5596 ? packet49
fayang36825da2019-08-21 14:01:27 -07005597 : framer_.transport_version() > QUIC_VERSION_43 ? packet46 : packet;
QUICHE teama6ef0a62019-03-07 20:34:33 -05005598 std::unique_ptr<QuicEncryptedPacket> encrypted(
5599 AssemblePacketFromFragments(fragments));
5600 EXPECT_TRUE(framer_.ProcessPacket(*encrypted));
bncf6f82b12019-10-30 07:01:01 -07005601 ASSERT_THAT(framer_.error(), IsQuicNoError());
QUICHE teama6ef0a62019-03-07 20:34:33 -05005602 ASSERT_TRUE(visitor_.version_negotiation_packet_.get());
nharper4fd11052019-06-04 14:23:22 -07005603 EXPECT_EQ(1u, visitor_.version_negotiation_packet_->versions.size());
QUICHE teama6ef0a62019-03-07 20:34:33 -05005604 EXPECT_EQ(GetParam(), visitor_.version_negotiation_packet_->versions[0]);
5605
5606 // Remove the last version from the packet so that every truncated
5607 // version of the packet is invalid, otherwise checking boundaries
5608 // is annoyingly complicated.
5609 for (size_t i = 0; i < 4; ++i) {
5610 fragments.back().fragment.pop_back();
5611 }
5612 CheckFramingBoundaries(fragments, QUIC_INVALID_VERSION_NEGOTIATION_PACKET);
5613}
5614
dschinazi072da7c2019-05-07 17:57:42 -07005615TEST_P(QuicFramerTest, VersionNegotiationPacketServer) {
fayang36825da2019-08-21 14:01:27 -07005616 if (framer_.transport_version() <= QUIC_VERSION_43) {
dschinazi072da7c2019-05-07 17:57:42 -07005617 return;
5618 }
5619
5620 QuicFramerPeer::SetPerspective(&framer_, Perspective::IS_SERVER);
5621 // clang-format off
5622 unsigned char packet[] = {
5623 // public flags (long header with all ignored bits set)
5624 0xFF,
5625 // version
5626 0x00, 0x00, 0x00, 0x00,
5627 // connection ID lengths
5628 0x50,
5629 // destination connection ID
5630 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x11,
5631 // supported versions
5632 QUIC_VERSION_BYTES,
5633 'Q', '2', '.', '0',
5634 };
dschinazi48ac9192019-07-31 00:07:26 -07005635 unsigned char packet2[] = {
5636 // public flags (long header with all ignored bits set)
5637 0xFF,
5638 // version
5639 0x00, 0x00, 0x00, 0x00,
5640 // destination connection ID length
5641 0x08,
5642 // destination connection ID
5643 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x11,
5644 // source connection ID length
5645 0x00,
5646 // supported versions
5647 QUIC_VERSION_BYTES,
5648 'Q', '2', '.', '0',
5649 };
dschinazi072da7c2019-05-07 17:57:42 -07005650 // clang-format on
dschinazi48ac9192019-07-31 00:07:26 -07005651 unsigned char* p = packet;
bnc4e9283d2019-12-17 07:08:57 -08005652 size_t p_length = QUICHE_ARRAYSIZE(packet);
dschinazi48ac9192019-07-31 00:07:26 -07005653 if (framer_.version().HasLengthPrefixedConnectionIds()) {
5654 p = packet2;
bnc4e9283d2019-12-17 07:08:57 -08005655 p_length = QUICHE_ARRAYSIZE(packet2);
dschinazi48ac9192019-07-31 00:07:26 -07005656 }
dschinazi072da7c2019-05-07 17:57:42 -07005657
dschinazi48ac9192019-07-31 00:07:26 -07005658 QuicEncryptedPacket encrypted(AsChars(p), p_length, false);
dschinazi072da7c2019-05-07 17:57:42 -07005659 EXPECT_FALSE(framer_.ProcessPacket(encrypted));
bncf6f82b12019-10-30 07:01:01 -07005660 EXPECT_THAT(framer_.error(),
5661 IsError(QUIC_INVALID_VERSION_NEGOTIATION_PACKET));
dschinazi072da7c2019-05-07 17:57:42 -07005662 EXPECT_EQ("Server received version negotiation packet.",
5663 framer_.detailed_error());
5664 EXPECT_FALSE(visitor_.version_negotiation_packet_.get());
5665}
5666
QUICHE teama6ef0a62019-03-07 20:34:33 -05005667TEST_P(QuicFramerTest, OldVersionNegotiationPacket) {
5668 // clang-format off
5669 PacketFragments packet = {
5670 // public flags (version, 8 byte connection_id)
5671 {"",
5672 {0x2D}},
5673 // connection_id
5674 {"",
5675 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
5676 // version tag
5677 {"Unable to read supported version in negotiation.",
5678 {QUIC_VERSION_BYTES,
5679 'Q', '2', '.', '0'}},
5680 };
5681 // clang-format on
5682
5683 if (framer_.transport_version() > QUIC_VERSION_43) {
5684 return;
5685 }
5686
5687 QuicFramerPeer::SetPerspective(&framer_, Perspective::IS_CLIENT);
5688
5689 std::unique_ptr<QuicEncryptedPacket> encrypted(
5690 AssemblePacketFromFragments(packet));
5691 EXPECT_TRUE(framer_.ProcessPacket(*encrypted));
bncf6f82b12019-10-30 07:01:01 -07005692 ASSERT_THAT(framer_.error(), IsQuicNoError());
QUICHE teama6ef0a62019-03-07 20:34:33 -05005693 ASSERT_TRUE(visitor_.version_negotiation_packet_.get());
nharper4fd11052019-06-04 14:23:22 -07005694 EXPECT_EQ(1u, visitor_.version_negotiation_packet_->versions.size());
QUICHE teama6ef0a62019-03-07 20:34:33 -05005695 EXPECT_EQ(GetParam(), visitor_.version_negotiation_packet_->versions[0]);
5696
5697 // Remove the last version from the packet so that every truncated
5698 // version of the packet is invalid, otherwise checking boundaries
5699 // is annoyingly complicated.
5700 for (size_t i = 0; i < 4; ++i) {
5701 packet.back().fragment.pop_back();
5702 }
5703 CheckFramingBoundaries(packet, QUIC_INVALID_VERSION_NEGOTIATION_PACKET);
5704}
5705
dschinazi244f6dc2019-05-06 15:45:16 -07005706TEST_P(QuicFramerTest, ParseIetfRetryPacket) {
5707 if (!framer_.version().SupportsRetry()) {
5708 return;
5709 }
5710 // IETF RETRY is only sent from client to server.
5711 QuicFramerPeer::SetPerspective(&framer_, Perspective::IS_CLIENT);
5712 // clang-format off
5713 unsigned char packet[] = {
5714 // public flags (long header with packet type RETRY and ODCIL=8)
5715 0xF5,
5716 // version
5717 QUIC_VERSION_BYTES,
5718 // connection ID lengths
5719 0x05,
5720 // source connection ID
5721 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x11,
5722 // original destination connection ID
5723 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
5724 // retry token
5725 'H', 'e', 'l', 'l', 'o', ' ', 't', 'h', 'i', 's',
5726 ' ', 'i', 's', ' ', 'R', 'E', 'T', 'R', 'Y', '!',
5727 };
dschinazic73506e2019-09-20 13:26:46 -07005728 unsigned char packet49[] = {
dschinazi48ac9192019-07-31 00:07:26 -07005729 // public flags (long header with packet type RETRY)
5730 0xF0,
5731 // version
5732 QUIC_VERSION_BYTES,
5733 // destination connection ID length
5734 0x00,
5735 // source connection ID length
5736 0x08,
5737 // source connection ID
5738 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x11,
5739 // original destination connection ID length
5740 0x08,
5741 // original destination connection ID
5742 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
5743 // retry token
5744 'H', 'e', 'l', 'l', 'o', ' ', 't', 'h', 'i', 's',
5745 ' ', 'i', 's', ' ', 'R', 'E', 'T', 'R', 'Y', '!',
5746 };
dschinazi278efae2020-01-28 17:03:09 -08005747 unsigned char packet_with_tag[] = {
5748 // public flags (long header with packet type RETRY)
5749 0xF0,
5750 // version
5751 QUIC_VERSION_BYTES,
5752 // destination connection ID length
5753 0x00,
5754 // source connection ID length
5755 0x08,
5756 // source connection ID
5757 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x11,
5758 // retry token
5759 'H', 'e', 'l', 'l', 'o', ' ', 't', 'h', 'i', 's',
5760 ' ', 'i', 's', ' ', 'R', 'E', 'T', 'R', 'Y', '!',
5761 // retry token integrity tag
5762 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
5763 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
5764 };
dschinazi244f6dc2019-05-06 15:45:16 -07005765 // clang-format on
5766
dschinazi48ac9192019-07-31 00:07:26 -07005767 unsigned char* p = packet;
bnc4e9283d2019-12-17 07:08:57 -08005768 size_t p_length = QUICHE_ARRAYSIZE(packet);
dschinazi278efae2020-01-28 17:03:09 -08005769 if (framer_.version().HasRetryIntegrityTag()) {
5770 p = packet_with_tag;
5771 p_length = QUICHE_ARRAYSIZE(packet_with_tag);
5772 } else if (framer_.transport_version() >= QUIC_VERSION_49) {
dschinazic73506e2019-09-20 13:26:46 -07005773 p = packet49;
bnc4e9283d2019-12-17 07:08:57 -08005774 p_length = QUICHE_ARRAYSIZE(packet49);
dschinazi48ac9192019-07-31 00:07:26 -07005775 }
5776 QuicEncryptedPacket encrypted(AsChars(p), p_length, false);
dschinazi244f6dc2019-05-06 15:45:16 -07005777 EXPECT_TRUE(framer_.ProcessPacket(encrypted));
5778
bncf6f82b12019-10-30 07:01:01 -07005779 EXPECT_THAT(framer_.error(), IsQuicNoError());
dschinazi244f6dc2019-05-06 15:45:16 -07005780 ASSERT_TRUE(visitor_.header_.get());
5781
dschinazi2dd75ee2020-01-29 09:21:37 -08005782 ASSERT_TRUE(visitor_.on_retry_packet_called_);
dschinazi244f6dc2019-05-06 15:45:16 -07005783 ASSERT_TRUE(visitor_.retry_new_connection_id_.get());
5784 ASSERT_TRUE(visitor_.retry_token_.get());
5785
dschinazi278efae2020-01-28 17:03:09 -08005786 if (framer_.version().HasRetryIntegrityTag()) {
5787 ASSERT_TRUE(visitor_.retry_token_integrity_tag_.get());
5788 static const unsigned char expected_integrity_tag[16] = {
5789 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
5790 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
5791 };
5792 quiche::test::CompareCharArraysWithHexError(
5793 "retry integrity tag", visitor_.retry_token_integrity_tag_->data(),
5794 visitor_.retry_token_integrity_tag_->length(),
5795 reinterpret_cast<const char*>(expected_integrity_tag),
5796 QUICHE_ARRAYSIZE(expected_integrity_tag));
5797 ASSERT_TRUE(visitor_.retry_without_tag_.get());
5798 quiche::test::CompareCharArraysWithHexError(
5799 "retry without tag", visitor_.retry_without_tag_->data(),
5800 visitor_.retry_without_tag_->length(),
5801 reinterpret_cast<const char*>(packet_with_tag), 35);
5802 } else {
5803 ASSERT_TRUE(visitor_.retry_original_connection_id_.get());
5804 EXPECT_EQ(FramerTestConnectionId(),
5805 *visitor_.retry_original_connection_id_.get());
5806 }
5807
dschinazi244f6dc2019-05-06 15:45:16 -07005808 EXPECT_EQ(FramerTestConnectionIdPlusOne(),
5809 *visitor_.retry_new_connection_id_.get());
5810 EXPECT_EQ("Hello this is RETRY!", *visitor_.retry_token_.get());
dschinazi244f6dc2019-05-06 15:45:16 -07005811
dschinazi2dd75ee2020-01-29 09:21:37 -08005812 // IETF RETRY is only sent from client to server, the rest of this test
5813 // ensures that the server correctly drops them without acting on them.
dschinazi244f6dc2019-05-06 15:45:16 -07005814 QuicFramerPeer::SetPerspective(&framer_, Perspective::IS_SERVER);
dschinazi2dd75ee2020-01-29 09:21:37 -08005815 // Reset our visitor state to default settings.
5816 visitor_.retry_original_connection_id_.reset();
5817 visitor_.retry_new_connection_id_.reset();
5818 visitor_.retry_token_.reset();
5819 visitor_.retry_token_integrity_tag_.reset();
5820 visitor_.retry_without_tag_.reset();
5821 visitor_.on_retry_packet_called_ = false;
dschinazi244f6dc2019-05-06 15:45:16 -07005822
dschinazi244f6dc2019-05-06 15:45:16 -07005823 EXPECT_FALSE(framer_.ProcessPacket(encrypted));
5824
bncf6f82b12019-10-30 07:01:01 -07005825 EXPECT_THAT(framer_.error(), IsError(QUIC_INVALID_PACKET_HEADER));
dschinazi244f6dc2019-05-06 15:45:16 -07005826 EXPECT_EQ("Client-initiated RETRY is invalid.", framer_.detailed_error());
dschinazi2dd75ee2020-01-29 09:21:37 -08005827
5828 EXPECT_FALSE(visitor_.on_retry_packet_called_);
5829 EXPECT_FALSE(visitor_.retry_new_connection_id_.get());
5830 EXPECT_FALSE(visitor_.retry_token_.get());
5831 EXPECT_FALSE(visitor_.retry_token_integrity_tag_.get());
5832 EXPECT_FALSE(visitor_.retry_without_tag_.get());
dschinazi244f6dc2019-05-06 15:45:16 -07005833}
5834
QUICHE teama6ef0a62019-03-07 20:34:33 -05005835TEST_P(QuicFramerTest, BuildPaddingFramePacket) {
QUICHE team2252b702019-05-14 23:55:14 -04005836 QuicFramerPeer::SetPerspective(&framer_, Perspective::IS_CLIENT);
QUICHE teama6ef0a62019-03-07 20:34:33 -05005837 QuicPacketHeader header;
5838 header.destination_connection_id = FramerTestConnectionId();
5839 header.reset_flag = false;
5840 header.version_flag = false;
5841 header.packet_number = kPacketNumber;
5842
5843 QuicFrames frames = {QuicFrame(QuicPaddingFrame())};
5844
5845 // clang-format off
dschinazi66dea072019-04-09 11:41:06 -07005846 unsigned char packet[kMaxOutgoingPacketSize] = {
QUICHE teama6ef0a62019-03-07 20:34:33 -05005847 // public flags (8 byte connection_id)
QUICHE team2252b702019-05-14 23:55:14 -04005848 0x2C,
QUICHE teama6ef0a62019-03-07 20:34:33 -05005849 // connection_id
5850 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
5851 // packet number
5852 0x12, 0x34, 0x56, 0x78,
5853
5854 // frame type (padding frame)
5855 0x00,
5856 0x00, 0x00, 0x00, 0x00
5857 };
5858
dschinazi66dea072019-04-09 11:41:06 -07005859 unsigned char packet46[kMaxOutgoingPacketSize] = {
QUICHE teama6ef0a62019-03-07 20:34:33 -05005860 // type (short header, 4 byte packet number)
5861 0x43,
5862 // connection_id
5863 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
5864 // packet number
5865 0x12, 0x34, 0x56, 0x78,
5866
5867 // frame type (padding frame)
5868 0x00,
5869 0x00, 0x00, 0x00, 0x00
5870 };
5871
dschinazi66dea072019-04-09 11:41:06 -07005872 unsigned char packet99[kMaxOutgoingPacketSize] = {
QUICHE teama6ef0a62019-03-07 20:34:33 -05005873 // type (short header, 4 byte packet number)
5874 0x43,
5875 // connection_id
5876 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
5877 // packet number
5878 0x12, 0x34, 0x56, 0x78,
5879
5880 // frame type (padding frame)
5881 0x00,
5882 0x00, 0x00, 0x00, 0x00
5883 };
5884 // clang-format on
5885
5886 unsigned char* p = packet;
fkastenholz305e1732019-06-18 05:01:22 -07005887 if (VersionHasIetfQuicFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05005888 p = packet99;
fayang36825da2019-08-21 14:01:27 -07005889 } else if (framer_.transport_version() >= QUIC_VERSION_46) {
fayang374888f2019-05-31 06:47:21 -07005890 p = packet46;
QUICHE teama6ef0a62019-03-07 20:34:33 -05005891 }
5892
5893 uint64_t header_size = GetPacketHeaderSize(
5894 framer_.transport_version(), PACKET_8BYTE_CONNECTION_ID,
5895 PACKET_0BYTE_CONNECTION_ID, !kIncludeVersion,
5896 !kIncludeDiversificationNonce, PACKET_4BYTE_PACKET_NUMBER,
5897 VARIABLE_LENGTH_INTEGER_LENGTH_0, 0, VARIABLE_LENGTH_INTEGER_LENGTH_0);
dschinazi66dea072019-04-09 11:41:06 -07005898 memset(p + header_size + 1, 0x00, kMaxOutgoingPacketSize - header_size - 1);
QUICHE teama6ef0a62019-03-07 20:34:33 -05005899
5900 std::unique_ptr<QuicPacket> data(BuildDataPacket(header, frames));
5901 ASSERT_TRUE(data != nullptr);
5902
dmcardle8f7df532020-01-07 13:28:57 -08005903 quiche::test::CompareCharArraysWithHexError(
QUICHE teama6ef0a62019-03-07 20:34:33 -05005904 "constructed packet", data->data(), data->length(), AsChars(p),
bnc4e9283d2019-12-17 07:08:57 -08005905 framer_.transport_version() > QUIC_VERSION_43 ? QUICHE_ARRAYSIZE(packet46)
5906 : QUICHE_ARRAYSIZE(packet));
QUICHE teama6ef0a62019-03-07 20:34:33 -05005907}
5908
5909TEST_P(QuicFramerTest, BuildStreamFramePacketWithNewPaddingFrame) {
QUICHE team2252b702019-05-14 23:55:14 -04005910 QuicFramerPeer::SetPerspective(&framer_, Perspective::IS_CLIENT);
QUICHE teama6ef0a62019-03-07 20:34:33 -05005911 QuicPacketHeader header;
5912 header.destination_connection_id = FramerTestConnectionId();
5913 header.reset_flag = false;
5914 header.version_flag = false;
5915 header.packet_number = kPacketNumber;
5916 QuicStreamFrame stream_frame(kStreamId, true, kStreamOffset,
dmcardlecf0bfcf2019-12-13 08:08:21 -08005917 quiche::QuicheStringPiece("hello world!"));
QUICHE teama6ef0a62019-03-07 20:34:33 -05005918 QuicPaddingFrame padding_frame(2);
5919 QuicFrames frames = {QuicFrame(padding_frame), QuicFrame(stream_frame),
5920 QuicFrame(padding_frame)};
5921
5922 // clang-format off
5923 unsigned char packet[] = {
5924 // public flags (8 byte connection_id)
QUICHE team2252b702019-05-14 23:55:14 -04005925 0x2C,
QUICHE teama6ef0a62019-03-07 20:34:33 -05005926 // connection_id
5927 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
5928 // packet number
5929 0x12, 0x34, 0x56, 0x78,
5930
5931 // paddings
5932 0x00, 0x00,
5933 // frame type (stream frame with fin)
5934 0xFF,
5935 // stream id
5936 0x01, 0x02, 0x03, 0x04,
5937 // offset
5938 0x3A, 0x98, 0xFE, 0xDC,
5939 0x32, 0x10, 0x76, 0x54,
5940 // data length
5941 0x00, 0x0c,
5942 // data
5943 'h', 'e', 'l', 'l',
5944 'o', ' ', 'w', 'o',
5945 'r', 'l', 'd', '!',
5946 // paddings
5947 0x00, 0x00,
5948 };
5949
QUICHE teama6ef0a62019-03-07 20:34:33 -05005950 unsigned char packet46[] = {
5951 // type (short header, 4 byte packet number)
5952 0x43,
5953 // connection_id
5954 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
5955 // packet number
5956 0x12, 0x34, 0x56, 0x78,
5957
5958 // paddings
5959 0x00, 0x00,
5960 // frame type (stream frame with fin)
5961 0xFF,
5962 // stream id
5963 0x01, 0x02, 0x03, 0x04,
5964 // offset
5965 0x3A, 0x98, 0xFE, 0xDC,
5966 0x32, 0x10, 0x76, 0x54,
5967 // data length
5968 0x00, 0x0c,
5969 // data
5970 'h', 'e', 'l', 'l',
5971 'o', ' ', 'w', 'o',
5972 'r', 'l', 'd', '!',
5973 // paddings
5974 0x00, 0x00,
5975 };
5976
5977 unsigned char packet99[] = {
5978 // type (short header, 4 byte packet number)
5979 0x43,
5980 // connection_id
5981 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
5982 // packet number
5983 0x12, 0x34, 0x56, 0x78,
5984
5985 // paddings
5986 0x00, 0x00,
5987 // frame type (IETF_STREAM with FIN, LEN, and OFFSET bits set)
5988 0x08 | 0x01 | 0x02 | 0x04,
5989 // stream id
5990 kVarInt62FourBytes + 0x01, 0x02, 0x03, 0x04,
5991 // offset
5992 kVarInt62EightBytes + 0x3A, 0x98, 0xFE, 0xDC,
5993 0x32, 0x10, 0x76, 0x54,
5994 // data length
5995 kVarInt62OneByte + 0x0c,
5996 // data
5997 'h', 'e', 'l', 'l',
5998 'o', ' ', 'w', 'o',
5999 'r', 'l', 'd', '!',
6000 // paddings
6001 0x00, 0x00,
6002 };
6003 // clang-format on
6004
6005 std::unique_ptr<QuicPacket> data(BuildDataPacket(header, frames));
6006 ASSERT_TRUE(data != nullptr);
6007
6008 unsigned char* p = packet;
bnc4e9283d2019-12-17 07:08:57 -08006009 size_t p_size = QUICHE_ARRAYSIZE(packet);
fkastenholz305e1732019-06-18 05:01:22 -07006010 if (VersionHasIetfQuicFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05006011 p = packet99;
bnc4e9283d2019-12-17 07:08:57 -08006012 p_size = QUICHE_ARRAYSIZE(packet99);
fayang36825da2019-08-21 14:01:27 -07006013 } else if (framer_.transport_version() >= QUIC_VERSION_46) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05006014 p = packet46;
bnc4e9283d2019-12-17 07:08:57 -08006015 p_size = QUICHE_ARRAYSIZE(packet46);
QUICHE teama6ef0a62019-03-07 20:34:33 -05006016 }
6017 QuicEncryptedPacket encrypted(AsChars(p), p_size, false);
6018
dmcardle8f7df532020-01-07 13:28:57 -08006019 quiche::test::CompareCharArraysWithHexError(
6020 "constructed packet", data->data(), data->length(), AsChars(p), p_size);
QUICHE teama6ef0a62019-03-07 20:34:33 -05006021}
6022
6023TEST_P(QuicFramerTest, Build4ByteSequenceNumberPaddingFramePacket) {
QUICHE team2252b702019-05-14 23:55:14 -04006024 QuicFramerPeer::SetPerspective(&framer_, Perspective::IS_CLIENT);
QUICHE teama6ef0a62019-03-07 20:34:33 -05006025 QuicPacketHeader header;
6026 header.destination_connection_id = FramerTestConnectionId();
6027 header.reset_flag = false;
6028 header.version_flag = false;
6029 header.packet_number_length = PACKET_4BYTE_PACKET_NUMBER;
6030 header.packet_number = kPacketNumber;
6031
6032 QuicFrames frames = {QuicFrame(QuicPaddingFrame())};
6033
6034 // clang-format off
dschinazi66dea072019-04-09 11:41:06 -07006035 unsigned char packet[kMaxOutgoingPacketSize] = {
QUICHE teama6ef0a62019-03-07 20:34:33 -05006036 // public flags (8 byte connection_id and 4 byte packet number)
QUICHE team2252b702019-05-14 23:55:14 -04006037 0x2C,
QUICHE teama6ef0a62019-03-07 20:34:33 -05006038 // connection_id
6039 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
6040 // packet number
6041 0x12, 0x34, 0x56, 0x78,
6042
6043 // frame type (padding frame)
6044 0x00,
6045 0x00, 0x00, 0x00, 0x00
6046 };
6047
dschinazi66dea072019-04-09 11:41:06 -07006048 unsigned char packet46[kMaxOutgoingPacketSize] = {
QUICHE teama6ef0a62019-03-07 20:34:33 -05006049 // type (short header, 4 byte packet number)
6050 0x43,
6051 // connection_id
6052 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
6053 // packet number
6054 0x12, 0x34, 0x56, 0x78,
6055
6056 // frame type (padding frame)
6057 0x00,
6058 0x00, 0x00, 0x00, 0x00
6059 };
6060
dschinazi66dea072019-04-09 11:41:06 -07006061 unsigned char packet99[kMaxOutgoingPacketSize] = {
QUICHE teama6ef0a62019-03-07 20:34:33 -05006062 // type (short header, 4 byte packet number)
6063 0x43,
6064 // connection_id
6065 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
6066 // packet number
6067 0x12, 0x34, 0x56, 0x78,
6068
6069 // frame type (padding frame)
6070 0x00,
6071 0x00, 0x00, 0x00, 0x00
6072 };
6073 // clang-format on
6074
6075 unsigned char* p = packet;
fkastenholz305e1732019-06-18 05:01:22 -07006076 if (VersionHasIetfQuicFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05006077 p = packet99;
fayang36825da2019-08-21 14:01:27 -07006078 } else if (framer_.transport_version() >= QUIC_VERSION_46) {
fayang374888f2019-05-31 06:47:21 -07006079 p = packet46;
QUICHE teama6ef0a62019-03-07 20:34:33 -05006080 }
6081
6082 uint64_t header_size = GetPacketHeaderSize(
6083 framer_.transport_version(), PACKET_8BYTE_CONNECTION_ID,
6084 PACKET_0BYTE_CONNECTION_ID, !kIncludeVersion,
6085 !kIncludeDiversificationNonce, PACKET_4BYTE_PACKET_NUMBER,
6086 VARIABLE_LENGTH_INTEGER_LENGTH_0, 0, VARIABLE_LENGTH_INTEGER_LENGTH_0);
dschinazi66dea072019-04-09 11:41:06 -07006087 memset(p + header_size + 1, 0x00, kMaxOutgoingPacketSize - header_size - 1);
QUICHE teama6ef0a62019-03-07 20:34:33 -05006088
6089 std::unique_ptr<QuicPacket> data(BuildDataPacket(header, frames));
6090 ASSERT_TRUE(data != nullptr);
6091
dmcardle8f7df532020-01-07 13:28:57 -08006092 quiche::test::CompareCharArraysWithHexError(
QUICHE teama6ef0a62019-03-07 20:34:33 -05006093 "constructed packet", data->data(), data->length(), AsChars(p),
bnc4e9283d2019-12-17 07:08:57 -08006094 framer_.transport_version() > QUIC_VERSION_43 ? QUICHE_ARRAYSIZE(packet46)
6095 : QUICHE_ARRAYSIZE(packet));
QUICHE teama6ef0a62019-03-07 20:34:33 -05006096}
6097
6098TEST_P(QuicFramerTest, Build2ByteSequenceNumberPaddingFramePacket) {
QUICHE team2252b702019-05-14 23:55:14 -04006099 QuicFramerPeer::SetPerspective(&framer_, Perspective::IS_CLIENT);
QUICHE teama6ef0a62019-03-07 20:34:33 -05006100 QuicPacketHeader header;
6101 header.destination_connection_id = FramerTestConnectionId();
6102 header.reset_flag = false;
6103 header.version_flag = false;
6104 header.packet_number_length = PACKET_2BYTE_PACKET_NUMBER;
6105 header.packet_number = kPacketNumber;
6106
6107 QuicFrames frames = {QuicFrame(QuicPaddingFrame())};
6108
6109 // clang-format off
dschinazi66dea072019-04-09 11:41:06 -07006110 unsigned char packet[kMaxOutgoingPacketSize] = {
QUICHE teama6ef0a62019-03-07 20:34:33 -05006111 // public flags (8 byte connection_id and 2 byte packet number)
QUICHE team2252b702019-05-14 23:55:14 -04006112 0x1C,
QUICHE teama6ef0a62019-03-07 20:34:33 -05006113 // connection_id
6114 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
6115 // packet number
6116 0x56, 0x78,
6117
6118 // frame type (padding frame)
6119 0x00,
6120 0x00, 0x00, 0x00, 0x00
6121 };
6122
dschinazi66dea072019-04-09 11:41:06 -07006123 unsigned char packet46[kMaxOutgoingPacketSize] = {
QUICHE teama6ef0a62019-03-07 20:34:33 -05006124 // type (short header, 2 byte packet number)
6125 0x41,
6126 // connection_id
6127 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
6128 // packet number
6129 0x56, 0x78,
6130
6131 // frame type (padding frame)
6132 0x00,
6133 0x00, 0x00, 0x00, 0x00
6134 };
6135
dschinazi66dea072019-04-09 11:41:06 -07006136 unsigned char packet99[kMaxOutgoingPacketSize] = {
QUICHE teama6ef0a62019-03-07 20:34:33 -05006137 // type (short header, 2 byte packet number)
6138 0x41,
6139 // connection_id
6140 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
6141 // packet number
6142 0x56, 0x78,
6143
6144 // frame type (padding frame)
6145 0x00,
6146 0x00, 0x00, 0x00, 0x00
6147 };
6148 // clang-format on
6149
6150 unsigned char* p = packet;
fkastenholz305e1732019-06-18 05:01:22 -07006151 if (VersionHasIetfQuicFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05006152 p = packet99;
fayang36825da2019-08-21 14:01:27 -07006153 } else if (framer_.transport_version() >= QUIC_VERSION_46) {
fayang374888f2019-05-31 06:47:21 -07006154 p = packet46;
QUICHE teama6ef0a62019-03-07 20:34:33 -05006155 }
6156
6157 uint64_t header_size = GetPacketHeaderSize(
6158 framer_.transport_version(), PACKET_8BYTE_CONNECTION_ID,
6159 PACKET_0BYTE_CONNECTION_ID, !kIncludeVersion,
6160 !kIncludeDiversificationNonce, PACKET_2BYTE_PACKET_NUMBER,
6161 VARIABLE_LENGTH_INTEGER_LENGTH_0, 0, VARIABLE_LENGTH_INTEGER_LENGTH_0);
dschinazi66dea072019-04-09 11:41:06 -07006162 memset(p + header_size + 1, 0x00, kMaxOutgoingPacketSize - header_size - 1);
QUICHE teama6ef0a62019-03-07 20:34:33 -05006163
6164 std::unique_ptr<QuicPacket> data(BuildDataPacket(header, frames));
6165 ASSERT_TRUE(data != nullptr);
6166
dmcardle8f7df532020-01-07 13:28:57 -08006167 quiche::test::CompareCharArraysWithHexError(
QUICHE teama6ef0a62019-03-07 20:34:33 -05006168 "constructed packet", data->data(), data->length(), AsChars(p),
bnc4e9283d2019-12-17 07:08:57 -08006169 framer_.transport_version() > QUIC_VERSION_43 ? QUICHE_ARRAYSIZE(packet46)
6170 : QUICHE_ARRAYSIZE(packet));
QUICHE teama6ef0a62019-03-07 20:34:33 -05006171}
6172
6173TEST_P(QuicFramerTest, Build1ByteSequenceNumberPaddingFramePacket) {
QUICHE team2252b702019-05-14 23:55:14 -04006174 QuicFramerPeer::SetPerspective(&framer_, Perspective::IS_CLIENT);
QUICHE teama6ef0a62019-03-07 20:34:33 -05006175 QuicPacketHeader header;
6176 header.destination_connection_id = FramerTestConnectionId();
6177 header.reset_flag = false;
6178 header.version_flag = false;
6179 header.packet_number_length = PACKET_1BYTE_PACKET_NUMBER;
6180 header.packet_number = kPacketNumber;
6181
6182 QuicFrames frames = {QuicFrame(QuicPaddingFrame())};
6183
6184 // clang-format off
dschinazi66dea072019-04-09 11:41:06 -07006185 unsigned char packet[kMaxOutgoingPacketSize] = {
QUICHE teama6ef0a62019-03-07 20:34:33 -05006186 // public flags (8 byte connection_id and 1 byte packet number)
QUICHE team2252b702019-05-14 23:55:14 -04006187 0x0C,
QUICHE teama6ef0a62019-03-07 20:34:33 -05006188 // connection_id
6189 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
6190 // packet number
6191 0x78,
6192
6193 // frame type (padding frame)
6194 0x00,
6195 0x00, 0x00, 0x00, 0x00
6196 };
6197
dschinazi66dea072019-04-09 11:41:06 -07006198 unsigned char packet46[kMaxOutgoingPacketSize] = {
QUICHE teama6ef0a62019-03-07 20:34:33 -05006199 // type (short header, 1 byte packet number)
6200 0x40,
6201 // connection_id
6202 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
6203 // packet number
6204 0x78,
6205
6206 // frame type (padding frame)
6207 0x00,
6208 0x00, 0x00, 0x00, 0x00
6209 };
6210
dschinazi66dea072019-04-09 11:41:06 -07006211 unsigned char packet99[kMaxOutgoingPacketSize] = {
QUICHE teama6ef0a62019-03-07 20:34:33 -05006212 // type (short header, 1 byte packet number)
6213 0x40,
6214 // connection_id
6215 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
6216 // packet number
6217 0x78,
6218
6219 // frame type (padding frame)
6220 0x00,
6221 0x00, 0x00, 0x00, 0x00
6222 };
6223 // clang-format on
6224
6225 unsigned char* p = packet;
fkastenholz305e1732019-06-18 05:01:22 -07006226 if (VersionHasIetfQuicFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05006227 p = packet99;
fayang36825da2019-08-21 14:01:27 -07006228 } else if (framer_.transport_version() >= QUIC_VERSION_46) {
fayang374888f2019-05-31 06:47:21 -07006229 p = packet46;
QUICHE teama6ef0a62019-03-07 20:34:33 -05006230 }
6231
6232 uint64_t header_size = GetPacketHeaderSize(
6233 framer_.transport_version(), PACKET_8BYTE_CONNECTION_ID,
6234 PACKET_0BYTE_CONNECTION_ID, !kIncludeVersion,
6235 !kIncludeDiversificationNonce, PACKET_1BYTE_PACKET_NUMBER,
6236 VARIABLE_LENGTH_INTEGER_LENGTH_0, 0, VARIABLE_LENGTH_INTEGER_LENGTH_0);
dschinazi66dea072019-04-09 11:41:06 -07006237 memset(p + header_size + 1, 0x00, kMaxOutgoingPacketSize - header_size - 1);
QUICHE teama6ef0a62019-03-07 20:34:33 -05006238
6239 std::unique_ptr<QuicPacket> data(BuildDataPacket(header, frames));
6240 ASSERT_TRUE(data != nullptr);
6241
dmcardle8f7df532020-01-07 13:28:57 -08006242 quiche::test::CompareCharArraysWithHexError(
QUICHE teama6ef0a62019-03-07 20:34:33 -05006243 "constructed packet", data->data(), data->length(), AsChars(p),
bnc4e9283d2019-12-17 07:08:57 -08006244 framer_.transport_version() > QUIC_VERSION_43 ? QUICHE_ARRAYSIZE(packet46)
6245 : QUICHE_ARRAYSIZE(packet));
QUICHE teama6ef0a62019-03-07 20:34:33 -05006246}
6247
6248TEST_P(QuicFramerTest, BuildStreamFramePacket) {
QUICHE team2252b702019-05-14 23:55:14 -04006249 QuicFramerPeer::SetPerspective(&framer_, Perspective::IS_CLIENT);
QUICHE teama6ef0a62019-03-07 20:34:33 -05006250 QuicPacketHeader header;
6251 header.destination_connection_id = FramerTestConnectionId();
6252 header.reset_flag = false;
6253 header.version_flag = false;
6254 header.packet_number = kPacketNumber;
6255 if (QuicVersionHasLongHeaderLengths(framer_.transport_version())) {
6256 header.length_length = VARIABLE_LENGTH_INTEGER_LENGTH_2;
6257 }
6258
6259 QuicStreamFrame stream_frame(kStreamId, true, kStreamOffset,
dmcardlecf0bfcf2019-12-13 08:08:21 -08006260 quiche::QuicheStringPiece("hello world!"));
QUICHE teama6ef0a62019-03-07 20:34:33 -05006261
6262 QuicFrames frames = {QuicFrame(stream_frame)};
6263
6264 // clang-format off
6265 unsigned char packet[] = {
6266 // public flags (8 byte connection_id)
QUICHE team2252b702019-05-14 23:55:14 -04006267 0x2C,
QUICHE teama6ef0a62019-03-07 20:34:33 -05006268 // connection_id
6269 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
6270 // packet number
6271 0x12, 0x34, 0x56, 0x78,
6272
6273 // frame type (stream frame with fin and no length)
6274 0xDF,
6275 // stream id
6276 0x01, 0x02, 0x03, 0x04,
6277 // offset
6278 0x3A, 0x98, 0xFE, 0xDC,
6279 0x32, 0x10, 0x76, 0x54,
6280 // data
6281 'h', 'e', 'l', 'l',
6282 'o', ' ', 'w', 'o',
6283 'r', 'l', 'd', '!',
6284 };
6285
QUICHE teama6ef0a62019-03-07 20:34:33 -05006286 unsigned char packet46[] = {
6287 // type (short header, 4 byte packet number)
6288 0x43,
6289 // connection_id
6290 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
6291 // packet number
6292 0x12, 0x34, 0x56, 0x78,
6293
6294 // frame type (stream frame with fin and no length)
6295 0xDF,
6296 // stream id
6297 0x01, 0x02, 0x03, 0x04,
6298 // offset
6299 0x3A, 0x98, 0xFE, 0xDC,
6300 0x32, 0x10, 0x76, 0x54,
6301 // data
6302 'h', 'e', 'l', 'l',
6303 'o', ' ', 'w', 'o',
6304 'r', 'l', 'd', '!',
6305 };
6306
6307 unsigned char packet99[] = {
6308 // type (short header, 4 byte packet number)
6309 0x43,
6310 // connection_id
6311 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
6312 // packet number
6313 0x12, 0x34, 0x56, 0x78,
6314
6315 // frame type (IETF_STREAM frame with FIN and OFFSET, no length)
6316 0x08 | 0x01 | 0x04,
6317 // stream id
6318 kVarInt62FourBytes + 0x01, 0x02, 0x03, 0x04,
6319 // offset
6320 kVarInt62EightBytes + 0x3A, 0x98, 0xFE, 0xDC,
6321 0x32, 0x10, 0x76, 0x54,
6322 // data
6323 'h', 'e', 'l', 'l',
6324 'o', ' ', 'w', 'o',
6325 'r', 'l', 'd', '!',
6326 };
6327 // clang-format on
6328
6329 std::unique_ptr<QuicPacket> data(BuildDataPacket(header, frames));
6330 ASSERT_TRUE(data != nullptr);
6331
6332 unsigned char* p = packet;
bnc4e9283d2019-12-17 07:08:57 -08006333 size_t p_size = QUICHE_ARRAYSIZE(packet);
fkastenholz305e1732019-06-18 05:01:22 -07006334 if (VersionHasIetfQuicFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05006335 p = packet99;
bnc4e9283d2019-12-17 07:08:57 -08006336 p_size = QUICHE_ARRAYSIZE(packet99);
fayang36825da2019-08-21 14:01:27 -07006337 } else if (framer_.transport_version() >= QUIC_VERSION_46) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05006338 p = packet46;
bnc4e9283d2019-12-17 07:08:57 -08006339 p_size = QUICHE_ARRAYSIZE(packet46);
QUICHE teama6ef0a62019-03-07 20:34:33 -05006340 }
dmcardle8f7df532020-01-07 13:28:57 -08006341 quiche::test::CompareCharArraysWithHexError(
6342 "constructed packet", data->data(), data->length(), AsChars(p), p_size);
QUICHE teama6ef0a62019-03-07 20:34:33 -05006343}
6344
6345TEST_P(QuicFramerTest, BuildStreamFramePacketWithVersionFlag) {
6346 QuicPacketHeader header;
6347 header.destination_connection_id = FramerTestConnectionId();
6348 header.reset_flag = false;
6349 header.version_flag = true;
6350 if (framer_.transport_version() > QUIC_VERSION_43) {
6351 header.long_packet_type = ZERO_RTT_PROTECTED;
6352 }
6353 header.packet_number = kPacketNumber;
6354 if (QuicVersionHasLongHeaderLengths(framer_.transport_version())) {
6355 header.length_length = VARIABLE_LENGTH_INTEGER_LENGTH_2;
6356 }
6357
6358 QuicStreamFrame stream_frame(kStreamId, true, kStreamOffset,
dmcardlecf0bfcf2019-12-13 08:08:21 -08006359 quiche::QuicheStringPiece("hello world!"));
QUICHE teama6ef0a62019-03-07 20:34:33 -05006360 QuicFrames frames = {QuicFrame(stream_frame)};
6361
6362 // clang-format off
6363 unsigned char packet[] = {
6364 // public flags (version, 8 byte connection_id)
6365 0x2D,
6366 // connection_id
6367 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
6368 // version tag
6369 QUIC_VERSION_BYTES,
6370 // packet number
6371 0x12, 0x34, 0x56, 0x78,
6372
6373 // frame type (stream frame with fin and no length)
6374 0xDF,
6375 // stream id
6376 0x01, 0x02, 0x03, 0x04,
6377 // offset
6378 0x3A, 0x98, 0xFE, 0xDC, 0x32, 0x10, 0x76, 0x54,
6379 // data
6380 'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', '!',
6381 };
6382
QUICHE teama6ef0a62019-03-07 20:34:33 -05006383 unsigned char packet46[] = {
6384 // type (long header with packet type ZERO_RTT_PROTECTED)
6385 0xD3,
6386 // version tag
6387 QUIC_VERSION_BYTES,
6388 // connection_id length
6389 0x50,
6390 // connection_id
6391 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
6392 // packet number
6393 0x12, 0x34, 0x56, 0x78,
6394
6395 // frame type (stream frame with fin and no length)
6396 0xDF,
6397 // stream id
6398 0x01, 0x02, 0x03, 0x04,
6399 // offset
6400 0x3A, 0x98, 0xFE, 0xDC, 0x32, 0x10, 0x76, 0x54,
6401 // data
6402 'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', '!',
6403 };
6404
dschinazic73506e2019-09-20 13:26:46 -07006405 unsigned char packet49[] = {
6406 // type (long header with packet type ZERO_RTT_PROTECTED)
6407 0xD3,
6408 // version tag
6409 QUIC_VERSION_BYTES,
6410 // destination connection ID length
6411 0x08,
6412 // destination connection ID
6413 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
6414 // source connection ID length
6415 0x00,
6416 // length
6417 0x40, 0x1D,
6418 // packet number
6419 0x12, 0x34, 0x56, 0x78,
6420
6421 // frame type (stream frame with fin and no length)
6422 0xDF,
6423 // stream id
6424 0x01, 0x02, 0x03, 0x04,
6425 // offset
6426 0x3A, 0x98, 0xFE, 0xDC, 0x32, 0x10, 0x76, 0x54,
6427 // data
6428 'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', '!',
6429 };
6430
QUICHE teama6ef0a62019-03-07 20:34:33 -05006431 unsigned char packet99[] = {
6432 // type (long header with packet type ZERO_RTT_PROTECTED)
6433 0xD3,
6434 // version tag
6435 QUIC_VERSION_BYTES,
dschinazi48ac9192019-07-31 00:07:26 -07006436 // destination connection ID length
6437 0x08,
6438 // destination connection ID
QUICHE teama6ef0a62019-03-07 20:34:33 -05006439 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
dschinazi48ac9192019-07-31 00:07:26 -07006440 // source connection ID length
6441 0x00,
QUICHE teama6ef0a62019-03-07 20:34:33 -05006442 // length
6443 0x40, 0x1D,
6444 // packet number
6445 0x12, 0x34, 0x56, 0x78,
6446
6447 // frame type (IETF_STREAM frame with fin and offset, no length)
6448 0x08 | 0x01 | 0x04,
6449 // stream id
6450 kVarInt62FourBytes + 0x01, 0x02, 0x03, 0x04,
6451 // offset
6452 kVarInt62EightBytes + 0x3A, 0x98, 0xFE, 0xDC, 0x32, 0x10, 0x76, 0x54,
6453 // data
6454 'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', '!',
6455 };
6456 // clang-format on
6457
6458 QuicFramerPeer::SetPerspective(&framer_, Perspective::IS_CLIENT);
6459 std::unique_ptr<QuicPacket> data(BuildDataPacket(header, frames));
6460 ASSERT_TRUE(data != nullptr);
6461
6462 unsigned char* p = packet;
bnc4e9283d2019-12-17 07:08:57 -08006463 size_t p_size = QUICHE_ARRAYSIZE(packet);
fkastenholz305e1732019-06-18 05:01:22 -07006464 if (VersionHasIetfQuicFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05006465 p = packet99;
bnc4e9283d2019-12-17 07:08:57 -08006466 p_size = QUICHE_ARRAYSIZE(packet99);
dschinazic73506e2019-09-20 13:26:46 -07006467 } else if (framer_.transport_version() >= QUIC_VERSION_49) {
6468 p = packet49;
bnc4e9283d2019-12-17 07:08:57 -08006469 p_size = QUICHE_ARRAYSIZE(packet49);
fayang36825da2019-08-21 14:01:27 -07006470 } else if (framer_.transport_version() >= QUIC_VERSION_46) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05006471 p = packet46;
bnc4e9283d2019-12-17 07:08:57 -08006472 p_size = QUICHE_ARRAYSIZE(packet46);
QUICHE teama6ef0a62019-03-07 20:34:33 -05006473 }
dmcardle8f7df532020-01-07 13:28:57 -08006474 quiche::test::CompareCharArraysWithHexError(
6475 "constructed packet", data->data(), data->length(), AsChars(p), p_size);
QUICHE teama6ef0a62019-03-07 20:34:33 -05006476}
6477
6478TEST_P(QuicFramerTest, BuildCryptoFramePacket) {
fkastenholzceae8372019-06-12 12:22:22 -07006479 if (!QuicVersionUsesCryptoFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05006480 return;
6481 }
QUICHE team2252b702019-05-14 23:55:14 -04006482 QuicFramerPeer::SetPerspective(&framer_, Perspective::IS_CLIENT);
QUICHE teama6ef0a62019-03-07 20:34:33 -05006483 QuicPacketHeader header;
6484 header.destination_connection_id = FramerTestConnectionId();
6485 header.reset_flag = false;
6486 header.version_flag = false;
6487 header.packet_number = kPacketNumber;
6488
6489 SimpleDataProducer data_producer;
6490 framer_.set_data_producer(&data_producer);
6491
dmcardlecf0bfcf2019-12-13 08:08:21 -08006492 quiche::QuicheStringPiece crypto_frame_contents("hello world!");
QUICHE team6987b4a2019-03-15 16:23:04 -07006493 QuicCryptoFrame crypto_frame(ENCRYPTION_INITIAL, kStreamOffset,
QUICHE teama6ef0a62019-03-07 20:34:33 -05006494 crypto_frame_contents.length());
QUICHE team6987b4a2019-03-15 16:23:04 -07006495 data_producer.SaveCryptoData(ENCRYPTION_INITIAL, kStreamOffset,
QUICHE teama6ef0a62019-03-07 20:34:33 -05006496 crypto_frame_contents);
6497
6498 QuicFrames frames = {QuicFrame(&crypto_frame)};
6499
6500 // clang-format off
nharper107ba5f2019-07-02 21:33:39 -07006501 unsigned char packet48[] = {
6502 // type (short header, 4 byte packet number)
6503 0x43,
6504 // connection_id
6505 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
6506 // packet number
6507 0x12, 0x34, 0x56, 0x78,
6508
6509 // frame type (QuicFrameType CRYPTO_FRAME)
6510 0x08,
6511 // offset
6512 kVarInt62EightBytes + 0x3A, 0x98, 0xFE, 0xDC,
6513 0x32, 0x10, 0x76, 0x54,
6514 // length
6515 kVarInt62OneByte + 12,
6516 // data
6517 'h', 'e', 'l', 'l',
6518 'o', ' ', 'w', 'o',
6519 'r', 'l', 'd', '!',
6520 };
6521
6522 unsigned char packet99[] = {
QUICHE teama6ef0a62019-03-07 20:34:33 -05006523 // type (short header, 4 byte packet number)
6524 0x43,
6525 // connection_id
6526 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
6527 // packet number
6528 0x12, 0x34, 0x56, 0x78,
6529
6530 // frame type (IETF_CRYPTO frame)
6531 0x06,
6532 // offset
6533 kVarInt62EightBytes + 0x3A, 0x98, 0xFE, 0xDC,
6534 0x32, 0x10, 0x76, 0x54,
6535 // length
6536 kVarInt62OneByte + 12,
6537 // data
6538 'h', 'e', 'l', 'l',
6539 'o', ' ', 'w', 'o',
6540 'r', 'l', 'd', '!',
6541 };
6542 // clang-format on
6543
nharper107ba5f2019-07-02 21:33:39 -07006544 unsigned char* packet = packet48;
bnc4e9283d2019-12-17 07:08:57 -08006545 size_t packet_size = QUICHE_ARRAYSIZE(packet48);
nharper107ba5f2019-07-02 21:33:39 -07006546 if (framer_.transport_version() == QUIC_VERSION_99) {
6547 packet = packet99;
bnc4e9283d2019-12-17 07:08:57 -08006548 packet_size = QUICHE_ARRAYSIZE(packet99);
nharper107ba5f2019-07-02 21:33:39 -07006549 }
QUICHE teama6ef0a62019-03-07 20:34:33 -05006550
6551 std::unique_ptr<QuicPacket> data(BuildDataPacket(header, frames));
6552 ASSERT_TRUE(data != nullptr);
dmcardle8f7df532020-01-07 13:28:57 -08006553 quiche::test::CompareCharArraysWithHexError("constructed packet",
6554 data->data(), data->length(),
6555 AsChars(packet), packet_size);
QUICHE teama6ef0a62019-03-07 20:34:33 -05006556}
6557
6558TEST_P(QuicFramerTest, CryptoFrame) {
renjietang15dfaa82020-01-03 16:13:38 -08006559 if (!QuicVersionUsesCryptoFrames(framer_.transport_version())) {
nharper107ba5f2019-07-02 21:33:39 -07006560 // CRYPTO frames aren't supported prior to v48.
QUICHE teama6ef0a62019-03-07 20:34:33 -05006561 return;
6562 }
zhongyi546cc452019-04-12 15:27:49 -07006563 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
QUICHE teama6ef0a62019-03-07 20:34:33 -05006564
6565 // clang-format off
nharper107ba5f2019-07-02 21:33:39 -07006566 PacketFragments packet48 = {
6567 // type (short header, 4 byte packet number)
6568 {"",
6569 {0x43}},
6570 // connection_id
6571 {"",
6572 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
6573 // packet number
6574 {"",
6575 {0x12, 0x34, 0x56, 0x78}},
6576 // frame type (QuicFrameType CRYPTO_FRAME)
6577 {"",
6578 {0x08}},
6579 // offset
6580 {"",
6581 {kVarInt62EightBytes + 0x3A, 0x98, 0xFE, 0xDC,
6582 0x32, 0x10, 0x76, 0x54}},
6583 // data length
6584 {"Invalid data length.",
6585 {kVarInt62OneByte + 12}},
6586 // data
6587 {"Unable to read frame data.",
6588 {'h', 'e', 'l', 'l',
6589 'o', ' ', 'w', 'o',
6590 'r', 'l', 'd', '!'}},
6591 };
6592
6593 PacketFragments packet99 = {
QUICHE teama6ef0a62019-03-07 20:34:33 -05006594 // type (short header, 4 byte packet number)
6595 {"",
6596 {0x43}},
6597 // connection_id
6598 {"",
6599 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
6600 // packet number
6601 {"",
6602 {0x12, 0x34, 0x56, 0x78}},
6603 // frame type (IETF_CRYPTO frame)
6604 {"",
6605 {0x06}},
6606 // offset
6607 {"",
6608 {kVarInt62EightBytes + 0x3A, 0x98, 0xFE, 0xDC,
6609 0x32, 0x10, 0x76, 0x54}},
6610 // data length
6611 {"Invalid data length.",
6612 {kVarInt62OneByte + 12}},
6613 // data
6614 {"Unable to read frame data.",
6615 {'h', 'e', 'l', 'l',
6616 'o', ' ', 'w', 'o',
6617 'r', 'l', 'd', '!'}},
6618 };
6619 // clang-format on
6620
nharper107ba5f2019-07-02 21:33:39 -07006621 PacketFragments& fragments =
6622 framer_.transport_version() == QUIC_VERSION_99 ? packet99 : packet48;
QUICHE teama6ef0a62019-03-07 20:34:33 -05006623 std::unique_ptr<QuicEncryptedPacket> encrypted(
nharper107ba5f2019-07-02 21:33:39 -07006624 AssemblePacketFromFragments(fragments));
QUICHE teama6ef0a62019-03-07 20:34:33 -05006625 EXPECT_TRUE(framer_.ProcessPacket(*encrypted));
6626
bncf6f82b12019-10-30 07:01:01 -07006627 EXPECT_THAT(framer_.error(), IsQuicNoError());
QUICHE teama6ef0a62019-03-07 20:34:33 -05006628 ASSERT_TRUE(visitor_.header_.get());
6629 EXPECT_TRUE(CheckDecryption(
6630 *encrypted, !kIncludeVersion, !kIncludeDiversificationNonce,
6631 PACKET_8BYTE_CONNECTION_ID, PACKET_0BYTE_CONNECTION_ID));
6632 ASSERT_EQ(1u, visitor_.crypto_frames_.size());
6633 QuicCryptoFrame* frame = visitor_.crypto_frames_[0].get();
renjietang15dfaa82020-01-03 16:13:38 -08006634 EXPECT_EQ(ENCRYPTION_FORWARD_SECURE, frame->level);
QUICHE teama6ef0a62019-03-07 20:34:33 -05006635 EXPECT_EQ(kStreamOffset, frame->offset);
vasilvvc48c8712019-03-11 13:38:16 -07006636 EXPECT_EQ("hello world!",
6637 std::string(frame->data_buffer, frame->data_length));
QUICHE teama6ef0a62019-03-07 20:34:33 -05006638
nharper107ba5f2019-07-02 21:33:39 -07006639 CheckFramingBoundaries(fragments, QUIC_INVALID_FRAME_DATA);
QUICHE teama6ef0a62019-03-07 20:34:33 -05006640}
6641
6642TEST_P(QuicFramerTest, BuildVersionNegotiationPacket) {
dschinazi1ac22cc2019-06-25 11:47:50 -07006643 SetQuicFlag(FLAGS_quic_disable_version_negotiation_grease_randomness, true);
QUICHE teama6ef0a62019-03-07 20:34:33 -05006644 // clang-format off
6645 unsigned char packet[] = {
6646 // public flags (version, 8 byte connection_id)
6647 0x0D,
6648 // connection_id
6649 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
dschinazi1ac22cc2019-06-25 11:47:50 -07006650 // supported versions
6651 0xDA, 0x5A, 0x3A, 0x3A,
QUICHE teama6ef0a62019-03-07 20:34:33 -05006652 QUIC_VERSION_BYTES,
6653 };
fayang36825da2019-08-21 14:01:27 -07006654 unsigned char packet46[] = {
QUICHE teama6ef0a62019-03-07 20:34:33 -05006655 // type (long header)
dschinazi0366de92019-06-18 20:00:27 -07006656 0xC0,
QUICHE teama6ef0a62019-03-07 20:34:33 -05006657 // version tag
6658 0x00, 0x00, 0x00, 0x00,
6659 // connection_id length
6660 0x05,
6661 // connection_id
6662 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
dschinazi1ac22cc2019-06-25 11:47:50 -07006663 // supported versions
6664 0xDA, 0x5A, 0x3A, 0x3A,
QUICHE teama6ef0a62019-03-07 20:34:33 -05006665 QUIC_VERSION_BYTES,
6666 };
dschinazic73506e2019-09-20 13:26:46 -07006667 unsigned char packet49[] = {
dschinazi48ac9192019-07-31 00:07:26 -07006668 // type (long header)
6669 0xC0,
6670 // version tag
6671 0x00, 0x00, 0x00, 0x00,
6672 // destination connection ID length
6673 0x00,
6674 // source connection ID length
6675 0x08,
6676 // source connection ID
6677 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
6678 // supported versions
6679 0xDA, 0x5A, 0x3A, 0x3A,
6680 QUIC_VERSION_BYTES,
6681 };
QUICHE teama6ef0a62019-03-07 20:34:33 -05006682 // clang-format on
6683 unsigned char* p = packet;
bnc4e9283d2019-12-17 07:08:57 -08006684 size_t p_size = QUICHE_ARRAYSIZE(packet);
dschinazic73506e2019-09-20 13:26:46 -07006685 if (framer_.transport_version() >= QUIC_VERSION_49) {
6686 p = packet49;
bnc4e9283d2019-12-17 07:08:57 -08006687 p_size = QUICHE_ARRAYSIZE(packet49);
dschinazi48ac9192019-07-31 00:07:26 -07006688 } else if (framer_.transport_version() > QUIC_VERSION_43) {
fayang36825da2019-08-21 14:01:27 -07006689 p = packet46;
bnc4e9283d2019-12-17 07:08:57 -08006690 p_size = QUICHE_ARRAYSIZE(packet46);
QUICHE teama6ef0a62019-03-07 20:34:33 -05006691 }
6692
6693 QuicConnectionId connection_id = FramerTestConnectionId();
6694 std::unique_ptr<QuicEncryptedPacket> data(
dschinazib417d602019-05-29 13:08:45 -07006695 QuicFramer::BuildVersionNegotiationPacket(
6696 connection_id, EmptyQuicConnectionId(),
6697 framer_.transport_version() > QUIC_VERSION_43,
dschinazi48ac9192019-07-31 00:07:26 -07006698 framer_.version().HasLengthPrefixedConnectionIds(),
QUICHE teama6ef0a62019-03-07 20:34:33 -05006699 SupportedVersions(GetParam())));
dmcardle8f7df532020-01-07 13:28:57 -08006700 quiche::test::CompareCharArraysWithHexError(
6701 "constructed packet", data->data(), data->length(), AsChars(p), p_size);
QUICHE teama6ef0a62019-03-07 20:34:33 -05006702}
6703
dschinazib417d602019-05-29 13:08:45 -07006704TEST_P(QuicFramerTest, BuildVersionNegotiationPacketWithClientConnectionId) {
dschinazi48ac9192019-07-31 00:07:26 -07006705 if (!framer_.version().SupportsClientConnectionIds()) {
dschinazib417d602019-05-29 13:08:45 -07006706 return;
6707 }
6708
dschinazi1ac22cc2019-06-25 11:47:50 -07006709 SetQuicFlag(FLAGS_quic_disable_version_negotiation_grease_randomness, true);
dschinazib417d602019-05-29 13:08:45 -07006710
dschinazib417d602019-05-29 13:08:45 -07006711 // clang-format off
6712 unsigned char packet[] = {
6713 // type (long header)
dschinazi0366de92019-06-18 20:00:27 -07006714 0xC0,
dschinazib417d602019-05-29 13:08:45 -07006715 // version tag
6716 0x00, 0x00, 0x00, 0x00,
dschinazib417d602019-05-29 13:08:45 -07006717 // client/destination connection ID
dschinazi48ac9192019-07-31 00:07:26 -07006718 0x08,
dschinazib417d602019-05-29 13:08:45 -07006719 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x11,
6720 // server/source connection ID
dschinazi48ac9192019-07-31 00:07:26 -07006721 0x08,
dschinazib417d602019-05-29 13:08:45 -07006722 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
dschinazi1ac22cc2019-06-25 11:47:50 -07006723 // supported versions
6724 0xDA, 0x5A, 0x3A, 0x3A,
dschinazib417d602019-05-29 13:08:45 -07006725 QUIC_VERSION_BYTES,
6726 };
6727 // clang-format on
6728
6729 QuicConnectionId server_connection_id = FramerTestConnectionId();
6730 QuicConnectionId client_connection_id = FramerTestConnectionIdPlusOne();
6731 std::unique_ptr<QuicEncryptedPacket> data(
dschinazi48ac9192019-07-31 00:07:26 -07006732 QuicFramer::BuildVersionNegotiationPacket(
6733 server_connection_id, client_connection_id, true, true,
6734 SupportedVersions(GetParam())));
dmcardle8f7df532020-01-07 13:28:57 -08006735 quiche::test::CompareCharArraysWithHexError(
6736 "constructed packet", data->data(), data->length(), AsChars(packet),
6737 QUICHE_ARRAYSIZE(packet));
dschinazib417d602019-05-29 13:08:45 -07006738}
6739
QUICHE teama6ef0a62019-03-07 20:34:33 -05006740TEST_P(QuicFramerTest, BuildAckFramePacketOneAckBlock) {
QUICHE team2252b702019-05-14 23:55:14 -04006741 QuicFramerPeer::SetPerspective(&framer_, Perspective::IS_CLIENT);
QUICHE teama6ef0a62019-03-07 20:34:33 -05006742 QuicPacketHeader header;
6743 header.destination_connection_id = FramerTestConnectionId();
6744 header.reset_flag = false;
6745 header.version_flag = false;
6746 header.packet_number = kPacketNumber;
6747
6748 // Use kSmallLargestObserved to make this test finished in a short time.
6749 QuicAckFrame ack_frame = InitAckFrame(kSmallLargestObserved);
6750 ack_frame.ack_delay_time = QuicTime::Delta::Zero();
6751
6752 QuicFrames frames = {QuicFrame(&ack_frame)};
6753
6754 // clang-format off
6755 unsigned char packet[] = {
6756 // public flags (8 byte connection_id)
QUICHE team2252b702019-05-14 23:55:14 -04006757 0x2C,
QUICHE teama6ef0a62019-03-07 20:34:33 -05006758 // connection_id
6759 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
6760 // packet number
6761 0x12, 0x34, 0x56, 0x78,
6762
6763 // frame type (ack frame)
6764 // (no ack blocks, 2 byte largest observed, 2 byte block length)
6765 0x45,
6766 // largest acked
6767 0x12, 0x34,
6768 // Zero delta time.
6769 0x00, 0x00,
6770 // first ack block length.
6771 0x12, 0x34,
6772 // num timestamps.
6773 0x00,
6774 };
6775
QUICHE teama6ef0a62019-03-07 20:34:33 -05006776 unsigned char packet46[] = {
6777 // type (short header, 4 byte packet number)
6778 0x43,
6779 // connection_id
6780 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
6781 // packet number
6782 0x12, 0x34, 0x56, 0x78,
6783
6784 // frame type (ack frame)
6785 // (no ack blocks, 2 byte largest observed, 2 byte block length)
6786 0x45,
6787 // largest acked
6788 0x12, 0x34,
6789 // Zero delta time.
6790 0x00, 0x00,
6791 // first ack block length.
6792 0x12, 0x34,
6793 // num timestamps.
6794 0x00,
6795 };
6796
6797 unsigned char packet99[] = {
6798 // type (short header, 4 byte packet number)
6799 0x43,
6800 // connection_id
6801 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
6802 // packet number
6803 0x12, 0x34, 0x56, 0x78,
6804
6805 // frame type (IETF_ACK frame)
6806 0x02,
6807 // largest acked
6808 kVarInt62TwoBytes + 0x12, 0x34,
6809 // Zero delta time.
6810 kVarInt62OneByte + 0x00,
6811 // Number of additional ack blocks.
6812 kVarInt62OneByte + 0x00,
6813 // first ack block length.
6814 kVarInt62TwoBytes + 0x12, 0x33,
6815 };
6816 // clang-format on
6817 unsigned char* p = packet;
bnc4e9283d2019-12-17 07:08:57 -08006818 size_t p_size = QUICHE_ARRAYSIZE(packet);
fkastenholz305e1732019-06-18 05:01:22 -07006819 if (VersionHasIetfQuicFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05006820 p = packet99;
bnc4e9283d2019-12-17 07:08:57 -08006821 p_size = QUICHE_ARRAYSIZE(packet99);
fayang36825da2019-08-21 14:01:27 -07006822 } else if (framer_.transport_version() >= QUIC_VERSION_46) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05006823 p = packet46;
bnc4e9283d2019-12-17 07:08:57 -08006824 p_size = QUICHE_ARRAYSIZE(packet46);
QUICHE teama6ef0a62019-03-07 20:34:33 -05006825 }
6826
6827 std::unique_ptr<QuicPacket> data(BuildDataPacket(header, frames));
6828 ASSERT_TRUE(data != nullptr);
dmcardle8f7df532020-01-07 13:28:57 -08006829 quiche::test::CompareCharArraysWithHexError(
6830 "constructed packet", data->data(), data->length(), AsChars(p), p_size);
QUICHE teama6ef0a62019-03-07 20:34:33 -05006831}
6832
6833TEST_P(QuicFramerTest, BuildAckFramePacketOneAckBlockMaxLength) {
QUICHE team2252b702019-05-14 23:55:14 -04006834 QuicFramerPeer::SetPerspective(&framer_, Perspective::IS_CLIENT);
QUICHE teama6ef0a62019-03-07 20:34:33 -05006835 QuicPacketHeader header;
6836 header.destination_connection_id = FramerTestConnectionId();
6837 header.reset_flag = false;
6838 header.version_flag = false;
6839 header.packet_number = kPacketNumber;
6840
6841 QuicAckFrame ack_frame = InitAckFrame(kPacketNumber);
6842 ack_frame.ack_delay_time = QuicTime::Delta::Zero();
6843
6844 QuicFrames frames = {QuicFrame(&ack_frame)};
6845
6846 // clang-format off
6847 unsigned char packet[] = {
6848 // public flags (8 byte connection_id)
QUICHE team2252b702019-05-14 23:55:14 -04006849 0x2C,
QUICHE teama6ef0a62019-03-07 20:34:33 -05006850 // connection_id
6851 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
6852 // packet number
6853 0x12, 0x34, 0x56, 0x78,
6854
6855 // frame type (ack frame)
6856 // (no ack blocks, 4 byte largest observed, 4 byte block length)
6857 0x4A,
6858 // largest acked
6859 0x12, 0x34, 0x56, 0x78,
6860 // Zero delta time.
6861 0x00, 0x00,
6862 // first ack block length.
6863 0x12, 0x34, 0x56, 0x78,
6864 // num timestamps.
6865 0x00,
6866 };
6867
QUICHE teama6ef0a62019-03-07 20:34:33 -05006868 unsigned char packet46[] = {
6869 // type (short header, 4 byte packet number)
6870 0x43,
6871 // connection_id
6872 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
6873 // packet number
6874 0x12, 0x34, 0x56, 0x78,
6875
6876 // frame type (ack frame)
6877 // (no ack blocks, 4 byte largest observed, 4 byte block length)
6878 0x4A,
6879 // largest acked
6880 0x12, 0x34, 0x56, 0x78,
6881 // Zero delta time.
6882 0x00, 0x00,
6883 // first ack block length.
6884 0x12, 0x34, 0x56, 0x78,
6885 // num timestamps.
6886 0x00,
6887 };
6888
6889
6890 unsigned char packet99[] = {
6891 // type (short header, 4 byte packet number)
6892 0x43,
6893 // connection_id
6894 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
6895 // packet number
6896 0x12, 0x34, 0x56, 0x78,
6897
6898 // frame type (IETF_ACK frame)
6899 0x02,
6900 // largest acked
6901 kVarInt62FourBytes + 0x12, 0x34, 0x56, 0x78,
6902 // Zero delta time.
6903 kVarInt62OneByte + 0x00,
6904 // Nr. of additional ack blocks
6905 kVarInt62OneByte + 0x00,
6906 // first ack block length.
6907 kVarInt62FourBytes + 0x12, 0x34, 0x56, 0x77,
6908 };
6909 // clang-format on
6910 unsigned char* p = packet;
bnc4e9283d2019-12-17 07:08:57 -08006911 size_t p_size = QUICHE_ARRAYSIZE(packet);
fkastenholz305e1732019-06-18 05:01:22 -07006912 if (VersionHasIetfQuicFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05006913 p = packet99;
bnc4e9283d2019-12-17 07:08:57 -08006914 p_size = QUICHE_ARRAYSIZE(packet99);
fayang36825da2019-08-21 14:01:27 -07006915 } else if (framer_.transport_version() >= QUIC_VERSION_46) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05006916 p = packet46;
bnc4e9283d2019-12-17 07:08:57 -08006917 p_size = QUICHE_ARRAYSIZE(packet46);
QUICHE teama6ef0a62019-03-07 20:34:33 -05006918 }
6919
6920 std::unique_ptr<QuicPacket> data(BuildDataPacket(header, frames));
6921 ASSERT_TRUE(data != nullptr);
dmcardle8f7df532020-01-07 13:28:57 -08006922 quiche::test::CompareCharArraysWithHexError(
6923 "constructed packet", data->data(), data->length(), AsChars(p), p_size);
QUICHE teama6ef0a62019-03-07 20:34:33 -05006924}
6925
6926TEST_P(QuicFramerTest, BuildAckFramePacketMultipleAckBlocks) {
QUICHE team2252b702019-05-14 23:55:14 -04006927 QuicFramerPeer::SetPerspective(&framer_, Perspective::IS_CLIENT);
QUICHE teama6ef0a62019-03-07 20:34:33 -05006928 QuicPacketHeader header;
6929 header.destination_connection_id = FramerTestConnectionId();
6930 header.reset_flag = false;
6931 header.version_flag = false;
6932 header.packet_number = kPacketNumber;
6933
6934 // Use kSmallLargestObserved to make this test finished in a short time.
6935 QuicAckFrame ack_frame =
6936 InitAckFrame({{QuicPacketNumber(1), QuicPacketNumber(5)},
6937 {QuicPacketNumber(10), QuicPacketNumber(500)},
6938 {QuicPacketNumber(900), kSmallMissingPacket},
6939 {kSmallMissingPacket + 1, kSmallLargestObserved + 1}});
6940 ack_frame.ack_delay_time = QuicTime::Delta::Zero();
6941
6942 QuicFrames frames = {QuicFrame(&ack_frame)};
6943
6944 // clang-format off
6945 unsigned char packet[] = {
6946 // public flags (8 byte connection_id)
QUICHE team2252b702019-05-14 23:55:14 -04006947 0x2C,
QUICHE teama6ef0a62019-03-07 20:34:33 -05006948 // connection_id
6949 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
6950 // packet number
6951 0x12, 0x34, 0x56, 0x78,
6952
6953 // frame type (ack frame)
6954 // (has ack blocks, 2 byte largest observed, 2 byte block length)
6955 0x65,
6956 // largest acked
6957 0x12, 0x34,
6958 // Zero delta time.
6959 0x00, 0x00,
6960 // num ack blocks ranges.
6961 0x04,
6962 // first ack block length.
6963 0x00, 0x01,
6964 // gap to next block.
6965 0x01,
6966 // ack block length.
6967 0x0e, 0xaf,
6968 // gap to next block.
6969 0xff,
6970 // ack block length.
6971 0x00, 0x00,
6972 // gap to next block.
6973 0x91,
6974 // ack block length.
6975 0x01, 0xea,
6976 // gap to next block.
6977 0x05,
6978 // ack block length.
6979 0x00, 0x04,
6980 // num timestamps.
6981 0x00,
6982 };
6983
QUICHE teama6ef0a62019-03-07 20:34:33 -05006984 unsigned char packet46[] = {
6985 // type (short header, 4 byte packet number)
6986 0x43,
6987 // connection_id
6988 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
6989 // packet number
6990 0x12, 0x34, 0x56, 0x78,
6991
6992 // frame type (ack frame)
6993 // (has ack blocks, 2 byte largest observed, 2 byte block length)
6994 0x65,
6995 // largest acked
6996 0x12, 0x34,
6997 // Zero delta time.
6998 0x00, 0x00,
6999 // num ack blocks ranges.
7000 0x04,
7001 // first ack block length.
7002 0x00, 0x01,
7003 // gap to next block.
7004 0x01,
7005 // ack block length.
7006 0x0e, 0xaf,
7007 // gap to next block.
7008 0xff,
7009 // ack block length.
7010 0x00, 0x00,
7011 // gap to next block.
7012 0x91,
7013 // ack block length.
7014 0x01, 0xea,
7015 // gap to next block.
7016 0x05,
7017 // ack block length.
7018 0x00, 0x04,
7019 // num timestamps.
7020 0x00,
7021 };
7022
7023 unsigned char packet99[] = {
7024 // type (short header, 4 byte packet number)
7025 0x43,
7026 // connection_id
7027 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
7028 // packet number
7029 0x12, 0x34, 0x56, 0x78,
7030
7031 // frame type (IETF_ACK frame)
7032 0x02,
7033 // largest acked
7034 kVarInt62TwoBytes + 0x12, 0x34,
7035 // Zero delta time.
7036 kVarInt62OneByte + 0x00,
7037 // num additional ack blocks.
7038 kVarInt62OneByte + 0x03,
7039 // first ack block length.
7040 kVarInt62OneByte + 0x00,
7041
7042 // gap to next block.
7043 kVarInt62OneByte + 0x00,
7044 // ack block length.
7045 kVarInt62TwoBytes + 0x0e, 0xae,
7046
7047 // gap to next block.
7048 kVarInt62TwoBytes + 0x01, 0x8f,
7049 // ack block length.
7050 kVarInt62TwoBytes + 0x01, 0xe9,
7051
7052 // gap to next block.
7053 kVarInt62OneByte + 0x04,
7054 // ack block length.
7055 kVarInt62OneByte + 0x03,
7056 };
7057 // clang-format on
7058 unsigned char* p = packet;
bnc4e9283d2019-12-17 07:08:57 -08007059 size_t p_size = QUICHE_ARRAYSIZE(packet);
fkastenholz305e1732019-06-18 05:01:22 -07007060 if (VersionHasIetfQuicFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05007061 p = packet99;
bnc4e9283d2019-12-17 07:08:57 -08007062 p_size = QUICHE_ARRAYSIZE(packet99);
fayang36825da2019-08-21 14:01:27 -07007063 } else if (framer_.transport_version() >= QUIC_VERSION_46) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05007064 p = packet46;
bnc4e9283d2019-12-17 07:08:57 -08007065 p_size = QUICHE_ARRAYSIZE(packet46);
QUICHE teama6ef0a62019-03-07 20:34:33 -05007066 }
7067
7068 std::unique_ptr<QuicPacket> data(BuildDataPacket(header, frames));
7069 ASSERT_TRUE(data != nullptr);
7070
dmcardle8f7df532020-01-07 13:28:57 -08007071 quiche::test::CompareCharArraysWithHexError(
7072 "constructed packet", data->data(), data->length(), AsChars(p), p_size);
QUICHE teama6ef0a62019-03-07 20:34:33 -05007073}
7074
7075TEST_P(QuicFramerTest, BuildAckFramePacketMaxAckBlocks) {
QUICHE team2252b702019-05-14 23:55:14 -04007076 QuicFramerPeer::SetPerspective(&framer_, Perspective::IS_CLIENT);
QUICHE teama6ef0a62019-03-07 20:34:33 -05007077 QuicPacketHeader header;
7078 header.destination_connection_id = FramerTestConnectionId();
7079 header.reset_flag = false;
7080 header.version_flag = false;
7081 header.packet_number = kPacketNumber;
7082
7083 // Use kSmallLargestObservedto make this test finished in a short time.
7084 QuicAckFrame ack_frame;
7085 ack_frame.largest_acked = kSmallLargestObserved;
7086 ack_frame.ack_delay_time = QuicTime::Delta::Zero();
7087 // 300 ack blocks.
7088 for (size_t i = 2; i < 2 * 300; i += 2) {
7089 ack_frame.packets.Add(QuicPacketNumber(i));
7090 }
7091 ack_frame.packets.AddRange(QuicPacketNumber(600), kSmallLargestObserved + 1);
7092
7093 QuicFrames frames = {QuicFrame(&ack_frame)};
7094
7095 // clang-format off
7096 unsigned char packet[] = {
7097 // public flags (8 byte connection_id)
QUICHE team2252b702019-05-14 23:55:14 -04007098 0x2C,
QUICHE teama6ef0a62019-03-07 20:34:33 -05007099 // connection_id
7100 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
7101 // packet number
7102 0x12, 0x34, 0x56, 0x78,
7103 // frame type (ack frame)
7104 // (has ack blocks, 2 byte largest observed, 2 byte block length)
7105 0x65,
7106 // largest acked
7107 0x12, 0x34,
7108 // Zero delta time.
7109 0x00, 0x00,
7110 // num ack blocks ranges.
7111 0xff,
7112 // first ack block length.
7113 0x0f, 0xdd,
7114 // 255 = 4 * 63 + 3
7115 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
7116 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
7117 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
7118 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
7119 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
7120 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
7121 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
7122 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
7123 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
7124 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
7125
7126 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
7127 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
7128 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
7129 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
7130 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
7131 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
7132 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
7133 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
7134 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
7135 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
7136
7137 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
7138 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
7139 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
7140 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
7141 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
7142 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
7143 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
7144 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
7145 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
7146 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
7147
7148 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
7149 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
7150 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
7151 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
7152 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
7153 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
7154 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
7155 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
7156 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
7157 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
7158
7159 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
7160 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
7161 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
7162 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
7163 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
7164 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
7165 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
7166 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
7167 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
7168 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
7169
7170 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
7171 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
7172 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
7173 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
7174 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
7175 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
7176 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
7177 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
7178 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
7179 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
7180
7181 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
7182 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
7183 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
7184 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
7185 // num timestamps.
7186 0x00,
7187 };
7188
QUICHE teama6ef0a62019-03-07 20:34:33 -05007189 unsigned char packet46[] = {
7190 // type (short header, 4 byte packet number)
7191 0x43,
7192 // connection_id
7193 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
7194 // packet number
7195 0x12, 0x34, 0x56, 0x78,
7196 // frame type (ack frame)
7197 // (has ack blocks, 2 byte largest observed, 2 byte block length)
7198 0x65,
7199 // largest acked
7200 0x12, 0x34,
7201 // Zero delta time.
7202 0x00, 0x00,
7203 // num ack blocks ranges.
7204 0xff,
7205 // first ack block length.
7206 0x0f, 0xdd,
7207 // 255 = 4 * 63 + 3
7208 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
7209 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
7210 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
7211 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
7212 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
7213 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
7214 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
7215 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
7216 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
7217 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
7218
7219 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
7220 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
7221 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
7222 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
7223 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
7224 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
7225 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
7226 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
7227 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
7228 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
7229
7230 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
7231 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
7232 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
7233 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
7234 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
7235 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
7236 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
7237 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
7238 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
7239 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
7240
7241 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
7242 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
7243 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
7244 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
7245 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
7246 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
7247 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
7248 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
7249 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
7250 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
7251
7252 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
7253 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
7254 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
7255 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
7256 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
7257 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
7258 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
7259 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
7260 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
7261 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
7262
7263 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
7264 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
7265 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
7266 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
7267 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
7268 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
7269 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
7270 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
7271 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
7272 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
7273
7274 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
7275 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
7276 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
7277 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01,
7278 // num timestamps.
7279 0x00,
7280 };
7281
7282 unsigned char packet99[] = {
7283 // type (short header, 4 byte packet number)
7284 0x43,
7285 // connection_id
7286 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
7287 // packet number
7288 0x12, 0x34, 0x56, 0x78,
7289 // frame type (IETF_ACK frame)
7290 0x02,
7291 // largest acked
7292 kVarInt62TwoBytes + 0x12, 0x34,
7293 // Zero delta time.
7294 kVarInt62OneByte + 0x00,
7295 // num ack blocks ranges.
7296 kVarInt62TwoBytes + 0x01, 0x2b,
7297 // first ack block length.
7298 kVarInt62TwoBytes + 0x0f, 0xdc,
7299 // 255 added blocks of gap_size == 1, ack_size == 1
7300#define V99AddedBLOCK kVarInt62OneByte + 0x00, kVarInt62OneByte + 0x00
7301 V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK,
7302 V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK,
7303 V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK,
7304 V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK,
7305 V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK,
7306 V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK,
7307 V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK,
7308 V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK,
7309 V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK,
7310 V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK,
7311 V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK,
7312 V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK,
7313 V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK,
7314 V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK,
7315 V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK,
7316 V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK,
7317 V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK,
7318 V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK,
7319 V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK,
7320 V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK,
7321
7322 V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK,
7323 V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK,
7324 V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK,
7325 V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK,
7326 V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK,
7327 V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK,
7328 V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK,
7329 V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK,
7330 V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK,
7331 V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK,
7332 V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK,
7333 V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK,
7334 V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK,
7335 V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK,
7336 V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK,
7337 V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK,
7338 V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK,
7339 V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK,
7340 V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK,
7341 V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK,
7342
7343 V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK,
7344 V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK,
7345 V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK,
7346 V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK,
7347 V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK,
7348 V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK,
7349 V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK,
7350 V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK,
7351 V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK,
7352 V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK,
7353 V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK,
7354 V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK,
7355 V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK,
7356 V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK,
7357 V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK,
7358 V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK,
7359 V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK,
7360 V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK,
7361 V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK,
7362 V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK, V99AddedBLOCK,
7363
7364#undef V99AddedBLOCK
7365 };
7366 // clang-format on
7367 unsigned char* p = packet;
bnc4e9283d2019-12-17 07:08:57 -08007368 size_t p_size = QUICHE_ARRAYSIZE(packet);
fkastenholz305e1732019-06-18 05:01:22 -07007369 if (VersionHasIetfQuicFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05007370 p = packet99;
bnc4e9283d2019-12-17 07:08:57 -08007371 p_size = QUICHE_ARRAYSIZE(packet99);
fayang36825da2019-08-21 14:01:27 -07007372 } else if (framer_.transport_version() >= QUIC_VERSION_46) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05007373 p = packet46;
bnc4e9283d2019-12-17 07:08:57 -08007374 p_size = QUICHE_ARRAYSIZE(packet46);
QUICHE teama6ef0a62019-03-07 20:34:33 -05007375 }
7376
7377 std::unique_ptr<QuicPacket> data(BuildDataPacket(header, frames));
7378 ASSERT_TRUE(data != nullptr);
7379
dmcardle8f7df532020-01-07 13:28:57 -08007380 quiche::test::CompareCharArraysWithHexError(
7381 "constructed packet", data->data(), data->length(), AsChars(p), p_size);
QUICHE teama6ef0a62019-03-07 20:34:33 -05007382}
7383
7384TEST_P(QuicFramerTest, BuildNewStopWaitingPacket) {
7385 if (version_.transport_version > QUIC_VERSION_43) {
7386 return;
7387 }
QUICHE team2252b702019-05-14 23:55:14 -04007388 QuicFramerPeer::SetPerspective(&framer_, Perspective::IS_CLIENT);
QUICHE teama6ef0a62019-03-07 20:34:33 -05007389 QuicPacketHeader header;
7390 header.destination_connection_id = FramerTestConnectionId();
7391 header.reset_flag = false;
7392 header.version_flag = false;
7393 header.packet_number = kPacketNumber;
7394
7395 QuicStopWaitingFrame stop_waiting_frame;
7396 stop_waiting_frame.least_unacked = kLeastUnacked;
7397
7398 QuicFrames frames = {QuicFrame(stop_waiting_frame)};
7399
7400 // clang-format off
7401 unsigned char packet[] = {
7402 // public flags (8 byte connection_id)
QUICHE team2252b702019-05-14 23:55:14 -04007403 0x2C,
QUICHE teama6ef0a62019-03-07 20:34:33 -05007404 // connection_id
7405 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
7406 // packet number
7407 0x12, 0x34, 0x56, 0x78,
7408
7409 // frame type (stop waiting frame)
7410 0x06,
7411 // least packet number awaiting an ack, delta from packet number.
7412 0x00, 0x00, 0x00, 0x08,
7413 };
7414
7415 // clang-format on
7416
7417 std::unique_ptr<QuicPacket> data(BuildDataPacket(header, frames));
7418 ASSERT_TRUE(data != nullptr);
7419
dmcardle8f7df532020-01-07 13:28:57 -08007420 quiche::test::CompareCharArraysWithHexError(
7421 "constructed packet", data->data(), data->length(), AsChars(packet),
7422 QUICHE_ARRAYSIZE(packet));
QUICHE teama6ef0a62019-03-07 20:34:33 -05007423}
7424
7425TEST_P(QuicFramerTest, BuildRstFramePacketQuic) {
QUICHE team2252b702019-05-14 23:55:14 -04007426 QuicFramerPeer::SetPerspective(&framer_, Perspective::IS_CLIENT);
QUICHE teama6ef0a62019-03-07 20:34:33 -05007427 QuicPacketHeader header;
7428 header.destination_connection_id = FramerTestConnectionId();
7429 header.reset_flag = false;
7430 header.version_flag = false;
7431 header.packet_number = kPacketNumber;
7432
7433 QuicRstStreamFrame rst_frame;
7434 rst_frame.stream_id = kStreamId;
fkastenholz305e1732019-06-18 05:01:22 -07007435 if (VersionHasIetfQuicFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05007436 rst_frame.ietf_error_code = 0x01;
7437 } else {
7438 rst_frame.error_code = static_cast<QuicRstStreamErrorCode>(0x05060708);
7439 }
7440 rst_frame.byte_offset = 0x0807060504030201;
7441
7442 // clang-format off
7443 unsigned char packet[] = {
7444 // public flags (8 byte connection_id)
QUICHE team2252b702019-05-14 23:55:14 -04007445 0x2C,
QUICHE teama6ef0a62019-03-07 20:34:33 -05007446 // connection_id
7447 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
7448 // packet number
7449 0x12, 0x34, 0x56, 0x78,
7450
7451 // frame type (rst stream frame)
7452 0x01,
7453 // stream id
7454 0x01, 0x02, 0x03, 0x04,
7455 // sent byte offset
7456 0x08, 0x07, 0x06, 0x05,
7457 0x04, 0x03, 0x02, 0x01,
7458 // error code
7459 0x05, 0x06, 0x07, 0x08,
7460 };
7461
QUICHE teama6ef0a62019-03-07 20:34:33 -05007462 unsigned char packet46[] = {
7463 // type (short packet, 4 byte packet number)
7464 0x43,
7465 // connection_id
7466 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
7467 // packet number
7468 0x12, 0x34, 0x56, 0x78,
7469
7470 // frame type (rst stream frame)
7471 0x01,
7472 // stream id
7473 0x01, 0x02, 0x03, 0x04,
7474 // sent byte offset
7475 0x08, 0x07, 0x06, 0x05,
7476 0x04, 0x03, 0x02, 0x01,
7477 // error code
7478 0x05, 0x06, 0x07, 0x08,
7479 };
7480
7481 unsigned char packet99[] = {
7482 // type (short packet, 4 byte packet number)
7483 0x43,
7484 // connection_id
7485 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
7486 // packet number
7487 0x12, 0x34, 0x56, 0x78,
7488
7489 // frame type (IETF_RST_STREAM frame)
7490 0x04,
7491 // stream id
7492 kVarInt62FourBytes + 0x01, 0x02, 0x03, 0x04,
fkastenholz07300e52019-07-16 11:51:37 -07007493 // error code
7494 kVarInt62OneByte + 0x01,
QUICHE teama6ef0a62019-03-07 20:34:33 -05007495 // sent byte offset
7496 kVarInt62EightBytes + 0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01
7497 };
7498 // clang-format on
7499
7500 QuicFrames frames = {QuicFrame(&rst_frame)};
7501
7502 std::unique_ptr<QuicPacket> data(BuildDataPacket(header, frames));
7503 ASSERT_TRUE(data != nullptr);
7504
7505 unsigned char* p = packet;
bnc4e9283d2019-12-17 07:08:57 -08007506 size_t p_size = QUICHE_ARRAYSIZE(packet);
fkastenholz305e1732019-06-18 05:01:22 -07007507 if (VersionHasIetfQuicFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05007508 p = packet99;
bnc4e9283d2019-12-17 07:08:57 -08007509 p_size = QUICHE_ARRAYSIZE(packet99);
fayang36825da2019-08-21 14:01:27 -07007510 } else if (framer_.transport_version() >= QUIC_VERSION_46) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05007511 p = packet46;
bnc4e9283d2019-12-17 07:08:57 -08007512 p_size = QUICHE_ARRAYSIZE(packet46);
QUICHE teama6ef0a62019-03-07 20:34:33 -05007513 }
7514 QuicEncryptedPacket encrypted(AsChars(p), p_size, false);
7515
dmcardle8f7df532020-01-07 13:28:57 -08007516 quiche::test::CompareCharArraysWithHexError(
7517 "constructed packet", data->data(), data->length(), AsChars(p), p_size);
QUICHE teama6ef0a62019-03-07 20:34:33 -05007518}
7519
7520TEST_P(QuicFramerTest, BuildCloseFramePacket) {
QUICHE team2252b702019-05-14 23:55:14 -04007521 QuicFramerPeer::SetPerspective(&framer_, Perspective::IS_CLIENT);
QUICHE teama6ef0a62019-03-07 20:34:33 -05007522 QuicPacketHeader header;
7523 header.destination_connection_id = FramerTestConnectionId();
7524 header.reset_flag = false;
7525 header.version_flag = false;
7526 header.packet_number = kPacketNumber;
7527
fkastenholz2d55b912019-09-10 15:57:17 -07007528 QuicConnectionCloseFrame close_frame(
7529 framer_.transport_version(),
7530 static_cast<QuicErrorCode>(
7531 VersionHasIetfQuicFrames(framer_.transport_version()) ? 0x11
7532 : 0x05060708),
7533 "because I can", 0x05);
7534 close_frame.extracted_error_code = QUIC_IETF_GQUIC_ERROR_MISSING;
QUICHE teama6ef0a62019-03-07 20:34:33 -05007535 QuicFrames frames = {QuicFrame(&close_frame)};
7536
7537 // clang-format off
7538 unsigned char packet[] = {
7539 // public flags (8 byte connection_id)
QUICHE team2252b702019-05-14 23:55:14 -04007540 0x2C,
QUICHE teama6ef0a62019-03-07 20:34:33 -05007541 // connection_id
7542 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
7543 // packet number
7544 0x12, 0x34, 0x56, 0x78,
7545
7546 // frame type (connection close frame)
7547 0x02,
7548 // error code
7549 0x05, 0x06, 0x07, 0x08,
7550 // error details length
7551 0x00, 0x0d,
7552 // error details
7553 'b', 'e', 'c', 'a',
7554 'u', 's', 'e', ' ',
7555 'I', ' ', 'c', 'a',
7556 'n',
7557 };
7558
QUICHE teama6ef0a62019-03-07 20:34:33 -05007559 unsigned char packet46[] = {
7560 // type (short header, 4 byte packet number)
7561 0x43,
7562 // connection_id
7563 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
7564 // packet number
7565 0x12, 0x34, 0x56, 0x78,
7566
7567 // frame type (connection close frame)
7568 0x02,
7569 // error code
7570 0x05, 0x06, 0x07, 0x08,
7571 // error details length
7572 0x00, 0x0d,
7573 // error details
7574 'b', 'e', 'c', 'a',
7575 'u', 's', 'e', ' ',
7576 'I', ' ', 'c', 'a',
7577 'n',
7578 };
7579
7580 unsigned char packet99[] = {
7581 // type (short header, 4 byte packet number)
7582 0x43,
7583 // connection_id
7584 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
7585 // packet number
7586 0x12, 0x34, 0x56, 0x78,
7587
7588 // frame type (IETF_CONNECTION_CLOSE frame)
7589 0x1c,
7590 // error code
fkastenholzd57d3f92019-07-16 09:05:17 -07007591 kVarInt62OneByte + 0x11,
QUICHE teama6ef0a62019-03-07 20:34:33 -05007592 // Frame type within the CONNECTION_CLOSE frame
7593 kVarInt62OneByte + 0x05,
7594 // error details length
7595 kVarInt62OneByte + 0x0d,
7596 // error details
7597 'b', 'e', 'c', 'a',
7598 'u', 's', 'e', ' ',
7599 'I', ' ', 'c', 'a',
7600 'n',
7601 };
7602 // clang-format on
7603
7604 unsigned char* p = packet;
bnc4e9283d2019-12-17 07:08:57 -08007605 size_t p_size = QUICHE_ARRAYSIZE(packet);
fkastenholz305e1732019-06-18 05:01:22 -07007606 if (VersionHasIetfQuicFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05007607 p = packet99;
bnc4e9283d2019-12-17 07:08:57 -08007608 p_size = QUICHE_ARRAYSIZE(packet99);
fayang36825da2019-08-21 14:01:27 -07007609 } else if (framer_.transport_version() >= QUIC_VERSION_46) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05007610 p = packet46;
bnc4e9283d2019-12-17 07:08:57 -08007611 p_size = QUICHE_ARRAYSIZE(packet46);
QUICHE teama6ef0a62019-03-07 20:34:33 -05007612 }
7613
7614 std::unique_ptr<QuicPacket> data(BuildDataPacket(header, frames));
7615 ASSERT_TRUE(data != nullptr);
7616
dmcardle8f7df532020-01-07 13:28:57 -08007617 quiche::test::CompareCharArraysWithHexError(
7618 "constructed packet", data->data(), data->length(), AsChars(p), p_size);
QUICHE teama6ef0a62019-03-07 20:34:33 -05007619}
7620
fkastenholzb4dade72019-08-05 06:54:20 -07007621TEST_P(QuicFramerTest, BuildCloseFramePacketExtendedInfo) {
7622 QuicFramerPeer::SetPerspective(&framer_, Perspective::IS_CLIENT);
7623 QuicPacketHeader header;
7624 header.destination_connection_id = FramerTestConnectionId();
7625 header.reset_flag = false;
7626 header.version_flag = false;
7627 header.packet_number = kPacketNumber;
7628
fkastenholz2d55b912019-09-10 15:57:17 -07007629 QuicConnectionCloseFrame close_frame(
7630 framer_.transport_version(),
7631 static_cast<QuicErrorCode>(
7632 VersionHasIetfQuicFrames(framer_.transport_version()) ? 0x11
7633 : 0x05060708),
7634 "because I can", 0x05);
fkastenholzb4dade72019-08-05 06:54:20 -07007635 // Set this so that it is "there" for both Google QUIC and IETF QUIC
7636 // framing. It better not show up for Google QUIC!
7637 close_frame.extracted_error_code = static_cast<QuicErrorCode>(0x4567);
7638
fkastenholzb4dade72019-08-05 06:54:20 -07007639 QuicFrames frames = {QuicFrame(&close_frame)};
7640
7641 // clang-format off
7642 unsigned char packet[] = {
7643 // public flags (8 byte connection_id)
7644 0x2C,
7645 // connection_id
7646 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
7647 // packet number
7648 0x12, 0x34, 0x56, 0x78,
7649
7650 // frame type (connection close frame)
7651 0x02,
7652 // error code
7653 0x05, 0x06, 0x07, 0x08,
7654 // error details length
7655 0x00, 0x0d,
7656 // error details
7657 'b', 'e', 'c', 'a',
7658 'u', 's', 'e', ' ',
7659 'I', ' ', 'c', 'a',
7660 'n',
7661 };
7662
fkastenholzb4dade72019-08-05 06:54:20 -07007663 unsigned char packet46[] = {
7664 // type (short header, 4 byte packet number)
7665 0x43,
7666 // connection_id
7667 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
7668 // packet number
7669 0x12, 0x34, 0x56, 0x78,
7670
7671 // frame type (connection close frame)
7672 0x02,
7673 // error code
7674 0x05, 0x06, 0x07, 0x08,
7675 // error details length
7676 0x00, 0x0d,
7677 // error details
7678 'b', 'e', 'c', 'a',
7679 'u', 's', 'e', ' ',
7680 'I', ' ', 'c', 'a',
7681 'n',
7682 };
7683
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_CONNECTION_CLOSE frame)
7693 0x1c,
7694 // error code
7695 kVarInt62OneByte + 0x11,
7696 // Frame type within the CONNECTION_CLOSE frame
7697 kVarInt62OneByte + 0x05,
7698 // error details length
7699 kVarInt62OneByte + 0x13,
7700 // error details
7701 '1', '7', '7', '6',
7702 '7', ':', 'b', 'e',
7703 'c', 'a', 'u', 's',
7704 'e', ' ', 'I', ' ',
7705 'c', 'a', 'n'
7706 };
7707 // clang-format on
7708
7709 unsigned char* p = packet;
bnc4e9283d2019-12-17 07:08:57 -08007710 size_t p_size = QUICHE_ARRAYSIZE(packet);
fkastenholzb4dade72019-08-05 06:54:20 -07007711 if (VersionHasIetfQuicFrames(framer_.transport_version())) {
7712 p = packet99;
bnc4e9283d2019-12-17 07:08:57 -08007713 p_size = QUICHE_ARRAYSIZE(packet99);
fayang36825da2019-08-21 14:01:27 -07007714 } else if (framer_.transport_version() >= QUIC_VERSION_46) {
fkastenholzb4dade72019-08-05 06:54:20 -07007715 p = packet46;
bnc4e9283d2019-12-17 07:08:57 -08007716 p_size = QUICHE_ARRAYSIZE(packet46);
fkastenholzb4dade72019-08-05 06:54:20 -07007717 }
7718
7719 std::unique_ptr<QuicPacket> data(BuildDataPacket(header, frames));
7720 ASSERT_TRUE(data != nullptr);
7721
dmcardle8f7df532020-01-07 13:28:57 -08007722 quiche::test::CompareCharArraysWithHexError(
7723 "constructed packet", data->data(), data->length(), AsChars(p), p_size);
fkastenholzb4dade72019-08-05 06:54:20 -07007724}
7725
QUICHE teama6ef0a62019-03-07 20:34:33 -05007726TEST_P(QuicFramerTest, BuildTruncatedCloseFramePacket) {
QUICHE team2252b702019-05-14 23:55:14 -04007727 QuicFramerPeer::SetPerspective(&framer_, Perspective::IS_CLIENT);
QUICHE teama6ef0a62019-03-07 20:34:33 -05007728 QuicPacketHeader header;
7729 header.destination_connection_id = FramerTestConnectionId();
7730 header.reset_flag = false;
7731 header.version_flag = false;
7732 header.packet_number = kPacketNumber;
7733
fkastenholz2d55b912019-09-10 15:57:17 -07007734 QuicConnectionCloseFrame close_frame(
7735 framer_.transport_version(),
7736 static_cast<QuicErrorCode>(
7737 VersionHasIetfQuicFrames(framer_.transport_version()) ? 0xa
7738 : 0x05060708),
7739 std::string(2048, 'A'), 0x05);
7740 close_frame.extracted_error_code = QUIC_IETF_GQUIC_ERROR_MISSING;
QUICHE teama6ef0a62019-03-07 20:34:33 -05007741 QuicFrames frames = {QuicFrame(&close_frame)};
7742
7743 // clang-format off
7744 unsigned char packet[] = {
7745 // public flags (8 byte connection_id)
QUICHE team2252b702019-05-14 23:55:14 -04007746 0x2C,
QUICHE teama6ef0a62019-03-07 20:34:33 -05007747 // connection_id
7748 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
7749 // packet number
7750 0x12, 0x34, 0x56, 0x78,
7751
7752 // frame type (connection close frame)
7753 0x02,
7754 // error code
7755 0x05, 0x06, 0x07, 0x08,
7756 // error details length
7757 0x01, 0x00,
7758 // error details (truncated to 256 bytes)
7759 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7760 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7761 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7762 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7763 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7764 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7765 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7766 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7767 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7768 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7769 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7770 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7771 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7772 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7773 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7774 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7775 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7776 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7777 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7778 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7779 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7780 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7781 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7782 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7783 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7784 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7785 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7786 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7787 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7788 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7789 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7790 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7791 };
7792
QUICHE teama6ef0a62019-03-07 20:34:33 -05007793 unsigned char packet46[] = {
7794 // type (short header, 4 byte packet number)
7795 0x43,
7796 // connection_id
7797 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
7798 // packet number
7799 0x12, 0x34, 0x56, 0x78,
7800
7801 // frame type (connection close frame)
7802 0x02,
7803 // error code
7804 0x05, 0x06, 0x07, 0x08,
7805 // error details length
7806 0x01, 0x00,
7807 // error details (truncated to 256 bytes)
7808 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7809 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7810 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7811 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7812 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7813 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7814 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7815 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7816 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7817 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7818 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7819 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7820 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7821 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7822 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7823 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7824 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7825 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7826 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7827 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7828 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7829 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7830 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7831 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7832 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7833 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7834 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7835 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7836 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7837 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7838 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7839 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7840 };
7841
7842 unsigned char packet99[] = {
7843 // type (short header, 4 byte packet number)
7844 0x43,
7845 // connection_id
7846 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
7847 // packet number
7848 0x12, 0x34, 0x56, 0x78,
7849
7850 // frame type (IETF_CONNECTION_CLOSE frame)
7851 0x1c,
7852 // error code
fkastenholzd57d3f92019-07-16 09:05:17 -07007853 kVarInt62OneByte + 0x0a,
QUICHE teama6ef0a62019-03-07 20:34:33 -05007854 // Frame type within the CONNECTION_CLOSE frame
fkastenholz2d55b912019-09-10 15:57:17 -07007855 kVarInt62OneByte + 0x05,
QUICHE teama6ef0a62019-03-07 20:34:33 -05007856 // error details length
7857 kVarInt62TwoBytes + 0x01, 0x00,
7858 // error details (truncated to 256 bytes)
7859 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
7860 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
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 };
7892 // clang-format on
7893
7894 unsigned char* p = packet;
bnc4e9283d2019-12-17 07:08:57 -08007895 size_t p_size = QUICHE_ARRAYSIZE(packet);
fkastenholz305e1732019-06-18 05:01:22 -07007896 if (VersionHasIetfQuicFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05007897 p = packet99;
bnc4e9283d2019-12-17 07:08:57 -08007898 p_size = QUICHE_ARRAYSIZE(packet99);
fayang36825da2019-08-21 14:01:27 -07007899 } else if (framer_.transport_version() >= QUIC_VERSION_46) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05007900 p = packet46;
bnc4e9283d2019-12-17 07:08:57 -08007901 p_size = QUICHE_ARRAYSIZE(packet46);
QUICHE teama6ef0a62019-03-07 20:34:33 -05007902 }
7903
7904 std::unique_ptr<QuicPacket> data(BuildDataPacket(header, frames));
7905 ASSERT_TRUE(data != nullptr);
7906
dmcardle8f7df532020-01-07 13:28:57 -08007907 quiche::test::CompareCharArraysWithHexError(
7908 "constructed packet", data->data(), data->length(), AsChars(p), p_size);
QUICHE teama6ef0a62019-03-07 20:34:33 -05007909}
7910
7911TEST_P(QuicFramerTest, BuildApplicationCloseFramePacket) {
fkastenholz305e1732019-06-18 05:01:22 -07007912 if (!VersionHasIetfQuicFrames(framer_.transport_version())) {
fkastenholzc5e0c8c2019-09-30 10:16:30 -07007913 // This frame is only for IETF QUIC.
QUICHE teama6ef0a62019-03-07 20:34:33 -05007914 return;
7915 }
QUICHE team2252b702019-05-14 23:55:14 -04007916 QuicFramerPeer::SetPerspective(&framer_, Perspective::IS_CLIENT);
QUICHE teama6ef0a62019-03-07 20:34:33 -05007917 QuicPacketHeader header;
7918 header.destination_connection_id = FramerTestConnectionId();
7919 header.reset_flag = false;
7920 header.version_flag = false;
7921 header.packet_number = kPacketNumber;
7922
fkastenholz72f509b2019-04-10 09:17:49 -07007923 QuicConnectionCloseFrame app_close_frame;
fkastenholzd57d3f92019-07-16 09:05:17 -07007924 app_close_frame.application_error_code =
7925 static_cast<uint64_t>(QUIC_INVALID_STREAM_ID);
QUICHE teama6ef0a62019-03-07 20:34:33 -05007926 app_close_frame.error_details = "because I can";
fkastenholz72f509b2019-04-10 09:17:49 -07007927 app_close_frame.close_type = IETF_QUIC_APPLICATION_CONNECTION_CLOSE;
QUICHE teama6ef0a62019-03-07 20:34:33 -05007928
7929 QuicFrames frames = {QuicFrame(&app_close_frame)};
7930
7931 // clang-format off
7932
7933 unsigned char packet99[] = {
7934 // type (short header, 4 byte packet number)
7935 0x43,
7936 // connection_id
7937 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
7938 // packet number
7939 0x12, 0x34, 0x56, 0x78,
7940
7941 // frame type (IETF_APPLICATION_CLOSE frame)
7942 0x1d,
7943 // error code
fkastenholzd57d3f92019-07-16 09:05:17 -07007944 kVarInt62OneByte + 0x11,
QUICHE teama6ef0a62019-03-07 20:34:33 -05007945 // error details length
fkastenholz2d55b912019-09-10 15:57:17 -07007946 kVarInt62OneByte + 0x0f,
7947 // error details, note that it includes an extended error code.
7948 '0', ':', 'b', 'e',
7949 'c', 'a', 'u', 's',
7950 'e', ' ', 'I', ' ',
7951 'c', 'a', 'n',
QUICHE teama6ef0a62019-03-07 20:34:33 -05007952 };
7953 // clang-format on
7954
7955 std::unique_ptr<QuicPacket> data(BuildDataPacket(header, frames));
7956 ASSERT_TRUE(data != nullptr);
7957
dmcardle8f7df532020-01-07 13:28:57 -08007958 quiche::test::CompareCharArraysWithHexError(
7959 "constructed packet", data->data(), data->length(), AsChars(packet99),
7960 QUICHE_ARRAYSIZE(packet99));
QUICHE teama6ef0a62019-03-07 20:34:33 -05007961}
7962
7963TEST_P(QuicFramerTest, BuildTruncatedApplicationCloseFramePacket) {
fkastenholz305e1732019-06-18 05:01:22 -07007964 if (!VersionHasIetfQuicFrames(framer_.transport_version())) {
fkastenholzc5e0c8c2019-09-30 10:16:30 -07007965 // This frame is only for IETF QUIC.
QUICHE teama6ef0a62019-03-07 20:34:33 -05007966 return;
7967 }
QUICHE team2252b702019-05-14 23:55:14 -04007968 QuicFramerPeer::SetPerspective(&framer_, Perspective::IS_CLIENT);
QUICHE teama6ef0a62019-03-07 20:34:33 -05007969 QuicPacketHeader header;
7970 header.destination_connection_id = FramerTestConnectionId();
7971 header.reset_flag = false;
7972 header.version_flag = false;
7973 header.packet_number = kPacketNumber;
7974
fkastenholz72f509b2019-04-10 09:17:49 -07007975 QuicConnectionCloseFrame app_close_frame;
fkastenholzd57d3f92019-07-16 09:05:17 -07007976 app_close_frame.application_error_code =
7977 static_cast<uint64_t>(QUIC_INVALID_STREAM_ID);
vasilvvc48c8712019-03-11 13:38:16 -07007978 app_close_frame.error_details = std::string(2048, 'A');
fkastenholz72f509b2019-04-10 09:17:49 -07007979 app_close_frame.close_type = IETF_QUIC_APPLICATION_CONNECTION_CLOSE;
fkastenholz2d55b912019-09-10 15:57:17 -07007980 // Setting to missing ensures that if it is missing, the extended
7981 // code is not added to the text message.
7982 app_close_frame.extracted_error_code = QUIC_IETF_GQUIC_ERROR_MISSING;
QUICHE teama6ef0a62019-03-07 20:34:33 -05007983
7984 QuicFrames frames = {QuicFrame(&app_close_frame)};
7985
7986 // clang-format off
7987 unsigned char packet99[] = {
7988 // type (short header, 4 byte packet number)
7989 0x43,
7990 // connection_id
7991 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
7992 // packet number
7993 0x12, 0x34, 0x56, 0x78,
7994
7995 // frame type (IETF_APPLICATION_CLOSE frame)
7996 0x1d,
7997 // error code
fkastenholzd57d3f92019-07-16 09:05:17 -07007998 kVarInt62OneByte + 0x11,
QUICHE teama6ef0a62019-03-07 20:34:33 -05007999 // error details length
8000 kVarInt62TwoBytes + 0x01, 0x00,
8001 // error details (truncated to 256 bytes)
8002 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
8003 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
8004 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
8005 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
8006 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
8007 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
8008 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
8009 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
8010 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
8011 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
8012 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
8013 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
8014 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
8015 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
8016 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
8017 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
8018 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
8019 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
8020 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
8021 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
8022 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
8023 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
8024 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
8025 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
8026 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
8027 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
8028 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
8029 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
8030 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
8031 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
8032 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
8033 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
8034 };
8035 // clang-format on
8036
8037 std::unique_ptr<QuicPacket> data(BuildDataPacket(header, frames));
8038 ASSERT_TRUE(data != nullptr);
8039
dmcardle8f7df532020-01-07 13:28:57 -08008040 quiche::test::CompareCharArraysWithHexError(
8041 "constructed packet", data->data(), data->length(), AsChars(packet99),
8042 QUICHE_ARRAYSIZE(packet99));
QUICHE teama6ef0a62019-03-07 20:34:33 -05008043}
8044
8045TEST_P(QuicFramerTest, BuildGoAwayPacket) {
fkastenholz305e1732019-06-18 05:01:22 -07008046 if (VersionHasIetfQuicFrames(framer_.transport_version())) {
fkastenholzc5e0c8c2019-09-30 10:16:30 -07008047 // This frame is only for Google QUIC.
QUICHE teama6ef0a62019-03-07 20:34:33 -05008048 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 QuicGoAwayFrame goaway_frame;
8058 goaway_frame.error_code = static_cast<QuicErrorCode>(0x05060708);
8059 goaway_frame.last_good_stream_id = kStreamId;
8060 goaway_frame.reason_phrase = "because I can";
8061
8062 QuicFrames frames = {QuicFrame(&goaway_frame)};
8063
8064 // clang-format off
8065 unsigned char packet[] = {
8066 // public flags (8 byte connection_id)
QUICHE team2252b702019-05-14 23:55:14 -04008067 0x2C,
QUICHE teama6ef0a62019-03-07 20:34:33 -05008068 // connection_id
8069 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
8070 // packet number
8071 0x12, 0x34, 0x56, 0x78,
8072
8073 // frame type (go away frame)
8074 0x03,
8075 // error code
8076 0x05, 0x06, 0x07, 0x08,
8077 // stream id
8078 0x01, 0x02, 0x03, 0x04,
8079 // error details length
8080 0x00, 0x0d,
8081 // error details
8082 'b', 'e', 'c', 'a',
8083 'u', 's', 'e', ' ',
8084 'I', ' ', 'c', 'a',
8085 'n',
8086 };
8087
QUICHE teama6ef0a62019-03-07 20:34:33 -05008088 unsigned char packet46[] = {
8089 // type (short header, 4 byte packet number)
8090 0x43,
8091 // connection_id
8092 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
8093 // packet number
8094 0x12, 0x34, 0x56, 0x78,
8095
8096 // frame type (go away frame)
8097 0x03,
8098 // error code
8099 0x05, 0x06, 0x07, 0x08,
8100 // stream id
8101 0x01, 0x02, 0x03, 0x04,
8102 // error details length
8103 0x00, 0x0d,
8104 // error details
8105 'b', 'e', 'c', 'a',
8106 'u', 's', 'e', ' ',
8107 'I', ' ', 'c', 'a',
8108 'n',
8109 };
8110
8111 // clang-format on
8112
8113 unsigned char* p = packet;
bnc4e9283d2019-12-17 07:08:57 -08008114 size_t p_size = QUICHE_ARRAYSIZE(packet);
fayang36825da2019-08-21 14:01:27 -07008115 if (framer_.transport_version() > QUIC_VERSION_43) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05008116 p = packet46;
bnc4e9283d2019-12-17 07:08:57 -08008117 p_size = QUICHE_ARRAYSIZE(packet46);
QUICHE teama6ef0a62019-03-07 20:34:33 -05008118 }
8119
8120 std::unique_ptr<QuicPacket> data(BuildDataPacket(header, frames));
8121 ASSERT_TRUE(data != nullptr);
8122
dmcardle8f7df532020-01-07 13:28:57 -08008123 quiche::test::CompareCharArraysWithHexError(
8124 "constructed packet", data->data(), data->length(), AsChars(p), p_size);
QUICHE teama6ef0a62019-03-07 20:34:33 -05008125}
8126
8127TEST_P(QuicFramerTest, BuildTruncatedGoAwayPacket) {
fkastenholz305e1732019-06-18 05:01:22 -07008128 if (VersionHasIetfQuicFrames(framer_.transport_version())) {
fkastenholzc5e0c8c2019-09-30 10:16:30 -07008129 // This frame is only for Google QUIC.
QUICHE teama6ef0a62019-03-07 20:34:33 -05008130 return;
8131 }
QUICHE team2252b702019-05-14 23:55:14 -04008132 QuicFramerPeer::SetPerspective(&framer_, Perspective::IS_CLIENT);
QUICHE teama6ef0a62019-03-07 20:34:33 -05008133 QuicPacketHeader header;
8134 header.destination_connection_id = FramerTestConnectionId();
8135 header.reset_flag = false;
8136 header.version_flag = false;
8137 header.packet_number = kPacketNumber;
8138
8139 QuicGoAwayFrame goaway_frame;
8140 goaway_frame.error_code = static_cast<QuicErrorCode>(0x05060708);
8141 goaway_frame.last_good_stream_id = kStreamId;
vasilvvc48c8712019-03-11 13:38:16 -07008142 goaway_frame.reason_phrase = std::string(2048, 'A');
QUICHE teama6ef0a62019-03-07 20:34:33 -05008143
8144 QuicFrames frames = {QuicFrame(&goaway_frame)};
8145
8146 // clang-format off
8147 unsigned char packet[] = {
8148 // public flags (8 byte connection_id)
QUICHE team2252b702019-05-14 23:55:14 -04008149 0x2C,
QUICHE teama6ef0a62019-03-07 20:34:33 -05008150 // connection_id
8151 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
8152 // packet number
8153 0x12, 0x34, 0x56, 0x78,
8154
8155 // frame type (go away frame)
8156 0x03,
8157 // error code
8158 0x05, 0x06, 0x07, 0x08,
8159 // stream id
8160 0x01, 0x02, 0x03, 0x04,
8161 // error details length
8162 0x01, 0x00,
8163 // error details (truncated to 256 bytes)
8164 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
8165 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
8166 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
8167 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
8168 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
8169 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
8170 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
8171 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
8172 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
8173 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
8174 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
8175 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
8176 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
8177 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
8178 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
8179 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
8180 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
8181 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
8182 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
8183 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
8184 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
8185 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
8186 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
8187 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
8188 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
8189 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
8190 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
8191 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
8192 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
8193 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
8194 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
8195 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
8196 };
8197
QUICHE teama6ef0a62019-03-07 20:34:33 -05008198 unsigned char packet46[] = {
8199 // type (short header, 4 byte packet number)
8200 0x43,
8201 // connection_id
8202 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
8203 // packet number
8204 0x12, 0x34, 0x56, 0x78,
8205
8206 // frame type (go away frame)
8207 0x03,
8208 // error code
8209 0x05, 0x06, 0x07, 0x08,
8210 // stream id
8211 0x01, 0x02, 0x03, 0x04,
8212 // error details length
8213 0x01, 0x00,
8214 // error details (truncated to 256 bytes)
8215 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
8216 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
8217 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
8218 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
8219 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
8220 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
8221 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
8222 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
8223 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
8224 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
8225 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
8226 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
8227 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
8228 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
8229 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
8230 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
8231 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
8232 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
8233 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
8234 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
8235 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
8236 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
8237 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
8238 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
8239 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
8240 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
8241 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
8242 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
8243 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
8244 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
8245 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
8246 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
8247 };
8248 // clang-format on
8249
8250 unsigned char* p = packet;
bnc4e9283d2019-12-17 07:08:57 -08008251 size_t p_size = QUICHE_ARRAYSIZE(packet);
fayang36825da2019-08-21 14:01:27 -07008252 if (framer_.transport_version() > QUIC_VERSION_43) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05008253 p = packet46;
bnc4e9283d2019-12-17 07:08:57 -08008254 p_size = QUICHE_ARRAYSIZE(packet46);
QUICHE teama6ef0a62019-03-07 20:34:33 -05008255 }
8256
8257 std::unique_ptr<QuicPacket> data(BuildDataPacket(header, frames));
8258 ASSERT_TRUE(data != nullptr);
8259
dmcardle8f7df532020-01-07 13:28:57 -08008260 quiche::test::CompareCharArraysWithHexError(
8261 "constructed packet", data->data(), data->length(), AsChars(p), p_size);
QUICHE teama6ef0a62019-03-07 20:34:33 -05008262}
8263
8264TEST_P(QuicFramerTest, BuildWindowUpdatePacket) {
QUICHE team2252b702019-05-14 23:55:14 -04008265 QuicFramerPeer::SetPerspective(&framer_, Perspective::IS_CLIENT);
QUICHE teama6ef0a62019-03-07 20:34:33 -05008266 QuicPacketHeader header;
8267 header.destination_connection_id = FramerTestConnectionId();
8268 header.reset_flag = false;
8269 header.version_flag = false;
8270 header.packet_number = kPacketNumber;
8271
8272 QuicWindowUpdateFrame window_update_frame;
8273 window_update_frame.stream_id = kStreamId;
renjietangd088eab2019-11-21 14:54:41 -08008274 window_update_frame.max_data = 0x1122334455667788;
QUICHE teama6ef0a62019-03-07 20:34:33 -05008275
8276 QuicFrames frames = {QuicFrame(&window_update_frame)};
8277
8278 // clang-format off
8279 unsigned char packet[] = {
8280 // public flags (8 byte connection_id)
QUICHE team2252b702019-05-14 23:55:14 -04008281 0x2C,
QUICHE teama6ef0a62019-03-07 20:34:33 -05008282 // connection_id
8283 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
8284 // packet number
8285 0x12, 0x34, 0x56, 0x78,
8286
8287 // frame type (window update frame)
8288 0x04,
8289 // stream id
8290 0x01, 0x02, 0x03, 0x04,
8291 // byte offset
8292 0x11, 0x22, 0x33, 0x44,
8293 0x55, 0x66, 0x77, 0x88,
8294 };
8295
QUICHE teama6ef0a62019-03-07 20:34:33 -05008296 unsigned char packet46[] = {
8297 // type (short header, 4 byte packet number)
8298 0x43,
8299 // connection_id
8300 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
8301 // packet number
8302 0x12, 0x34, 0x56, 0x78,
8303
8304 // frame type (window update frame)
8305 0x04,
8306 // stream id
8307 0x01, 0x02, 0x03, 0x04,
8308 // byte offset
8309 0x11, 0x22, 0x33, 0x44,
8310 0x55, 0x66, 0x77, 0x88,
8311 };
8312
8313 unsigned char packet99[] = {
8314 // type (short header, 4 byte packet number)
8315 0x43,
8316 // connection_id
8317 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
8318 // packet number
8319 0x12, 0x34, 0x56, 0x78,
8320
8321 // frame type (IETF_MAX_STREAM_DATA frame)
8322 0x11,
8323 // stream id
8324 kVarInt62FourBytes + 0x01, 0x02, 0x03, 0x04,
8325 // byte offset
8326 kVarInt62EightBytes + 0x11, 0x22, 0x33, 0x44,
8327 0x55, 0x66, 0x77, 0x88,
8328 };
8329 // clang-format on
8330
8331 std::unique_ptr<QuicPacket> data(BuildDataPacket(header, frames));
8332 ASSERT_TRUE(data != nullptr);
8333
8334 unsigned char* p = packet;
bnc4e9283d2019-12-17 07:08:57 -08008335 size_t p_size = QUICHE_ARRAYSIZE(packet);
fkastenholz305e1732019-06-18 05:01:22 -07008336 if (VersionHasIetfQuicFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05008337 p = packet99;
bnc4e9283d2019-12-17 07:08:57 -08008338 p_size = QUICHE_ARRAYSIZE(packet99);
fayang36825da2019-08-21 14:01:27 -07008339 } else if (framer_.transport_version() >= QUIC_VERSION_46) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05008340 p = packet46;
bnc4e9283d2019-12-17 07:08:57 -08008341 p_size = QUICHE_ARRAYSIZE(packet46);
QUICHE teama6ef0a62019-03-07 20:34:33 -05008342 }
8343
dmcardle8f7df532020-01-07 13:28:57 -08008344 quiche::test::CompareCharArraysWithHexError(
8345 "constructed packet", data->data(), data->length(), AsChars(p), p_size);
QUICHE teama6ef0a62019-03-07 20:34:33 -05008346}
8347
8348TEST_P(QuicFramerTest, BuildMaxStreamDataPacket) {
fkastenholz305e1732019-06-18 05:01:22 -07008349 if (!VersionHasIetfQuicFrames(framer_.transport_version())) {
fkastenholzc5e0c8c2019-09-30 10:16:30 -07008350 // This frame is only for IETF QUIC.
QUICHE teama6ef0a62019-03-07 20:34:33 -05008351 return;
8352 }
QUICHE team2252b702019-05-14 23:55:14 -04008353 QuicFramerPeer::SetPerspective(&framer_, Perspective::IS_CLIENT);
QUICHE teama6ef0a62019-03-07 20:34:33 -05008354 QuicPacketHeader header;
8355 header.destination_connection_id = FramerTestConnectionId();
8356 header.reset_flag = false;
8357 header.version_flag = false;
8358 header.packet_number = kPacketNumber;
8359
8360 QuicWindowUpdateFrame window_update_frame;
8361 window_update_frame.stream_id = kStreamId;
renjietangd088eab2019-11-21 14:54:41 -08008362 window_update_frame.max_data = 0x1122334455667788;
QUICHE teama6ef0a62019-03-07 20:34:33 -05008363
8364 QuicFrames frames = {QuicFrame(&window_update_frame)};
8365
8366 // clang-format off
8367 unsigned char packet99[] = {
8368 // type (short header, 4 byte packet number)
8369 0x43,
8370 // connection_id
8371 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
8372 // packet number
8373 0x12, 0x34, 0x56, 0x78,
8374
8375 // frame type (IETF_MAX_STREAM_DATA frame)
8376 0x11,
8377 // stream id
8378 kVarInt62FourBytes + 0x01, 0x02, 0x03, 0x04,
8379 // byte offset
8380 kVarInt62EightBytes + 0x11, 0x22, 0x33, 0x44,
8381 0x55, 0x66, 0x77, 0x88,
8382 };
8383 // clang-format on
8384
8385 std::unique_ptr<QuicPacket> data(BuildDataPacket(header, frames));
8386 ASSERT_TRUE(data != nullptr);
8387
dmcardle8f7df532020-01-07 13:28:57 -08008388 quiche::test::CompareCharArraysWithHexError(
8389 "constructed packet", data->data(), data->length(), AsChars(packet99),
8390 QUICHE_ARRAYSIZE(packet99));
QUICHE teama6ef0a62019-03-07 20:34:33 -05008391}
8392
8393TEST_P(QuicFramerTest, BuildMaxDataPacket) {
fkastenholz305e1732019-06-18 05:01:22 -07008394 if (!VersionHasIetfQuicFrames(framer_.transport_version())) {
fkastenholzc5e0c8c2019-09-30 10:16:30 -07008395 // This frame is only for IETF QUIC.
QUICHE teama6ef0a62019-03-07 20:34:33 -05008396 return;
8397 }
QUICHE team2252b702019-05-14 23:55:14 -04008398 QuicFramerPeer::SetPerspective(&framer_, Perspective::IS_CLIENT);
QUICHE teama6ef0a62019-03-07 20:34:33 -05008399 QuicPacketHeader header;
8400 header.destination_connection_id = FramerTestConnectionId();
8401 header.reset_flag = false;
8402 header.version_flag = false;
8403 header.packet_number = kPacketNumber;
8404
8405 QuicWindowUpdateFrame window_update_frame;
8406 window_update_frame.stream_id =
8407 QuicUtils::GetInvalidStreamId(framer_.transport_version());
renjietangd088eab2019-11-21 14:54:41 -08008408 window_update_frame.max_data = 0x1122334455667788;
QUICHE teama6ef0a62019-03-07 20:34:33 -05008409
8410 QuicFrames frames = {QuicFrame(&window_update_frame)};
8411
8412 // clang-format off
8413 unsigned char packet99[] = {
8414 // type (short header, 4 byte packet number)
8415 0x43,
8416 // connection_id
8417 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
8418 // packet number
8419 0x12, 0x34, 0x56, 0x78,
8420
8421 // frame type (IETF_MAX_DATA frame)
8422 0x10,
8423 // byte offset
8424 kVarInt62EightBytes + 0x11, 0x22, 0x33, 0x44,
8425 0x55, 0x66, 0x77, 0x88,
8426 };
8427 // clang-format on
8428
8429 std::unique_ptr<QuicPacket> data(BuildDataPacket(header, frames));
8430 ASSERT_TRUE(data != nullptr);
8431
dmcardle8f7df532020-01-07 13:28:57 -08008432 quiche::test::CompareCharArraysWithHexError(
8433 "constructed packet", data->data(), data->length(), AsChars(packet99),
8434 QUICHE_ARRAYSIZE(packet99));
QUICHE teama6ef0a62019-03-07 20:34:33 -05008435}
8436
8437TEST_P(QuicFramerTest, BuildBlockedPacket) {
QUICHE team2252b702019-05-14 23:55:14 -04008438 QuicFramerPeer::SetPerspective(&framer_, Perspective::IS_CLIENT);
QUICHE teama6ef0a62019-03-07 20:34:33 -05008439 QuicPacketHeader header;
8440 header.destination_connection_id = FramerTestConnectionId();
8441 header.reset_flag = false;
8442 header.version_flag = false;
8443 header.packet_number = kPacketNumber;
8444
8445 QuicBlockedFrame blocked_frame;
fkastenholz305e1732019-06-18 05:01:22 -07008446 if (VersionHasIetfQuicFrames(framer_.transport_version())) {
8447 // For IETF QUIC, the stream ID must be <invalid> for the frame
QUICHE teama6ef0a62019-03-07 20:34:33 -05008448 // to be a BLOCKED frame. if it's valid, it will be a
8449 // STREAM_BLOCKED frame.
8450 blocked_frame.stream_id =
8451 QuicUtils::GetInvalidStreamId(framer_.transport_version());
8452 } else {
8453 blocked_frame.stream_id = kStreamId;
8454 }
8455 blocked_frame.offset = kStreamOffset;
8456
8457 QuicFrames frames = {QuicFrame(&blocked_frame)};
8458
8459 // clang-format off
8460 unsigned char packet[] = {
8461 // public flags (8 byte connection_id)
QUICHE team2252b702019-05-14 23:55:14 -04008462 0x2C,
QUICHE teama6ef0a62019-03-07 20:34:33 -05008463 // connection_id
8464 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
8465 // packet number
8466 0x12, 0x34, 0x56, 0x78,
8467
8468 // frame type (blocked frame)
8469 0x05,
8470 // stream id
8471 0x01, 0x02, 0x03, 0x04,
8472 };
8473
QUICHE teama6ef0a62019-03-07 20:34:33 -05008474 unsigned char packet46[] = {
8475 // type (short packet, 4 byte packet number)
8476 0x43,
8477 // connection_id
8478 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
8479 // packet number
8480 0x12, 0x34, 0x56, 0x78,
8481
8482 // frame type (blocked frame)
8483 0x05,
8484 // stream id
8485 0x01, 0x02, 0x03, 0x04,
8486 };
8487
8488 unsigned char packet99[] = {
8489 // type (short packet, 4 byte packet number)
8490 0x43,
8491 // connection_id
8492 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
8493 // packet number
8494 0x12, 0x34, 0x56, 0x78,
8495
ianswett2f077442019-12-12 11:51:24 -08008496 // frame type (IETF_DATA_BLOCKED frame)
QUICHE teama6ef0a62019-03-07 20:34:33 -05008497 0x14,
8498 // Offset
8499 kVarInt62EightBytes + 0x3a, 0x98, 0xFE, 0xDC, 0x32, 0x10, 0x76, 0x54
8500 };
8501 // clang-format on
8502
8503 std::unique_ptr<QuicPacket> data(BuildDataPacket(header, frames));
8504 ASSERT_TRUE(data != nullptr);
8505
8506 unsigned char* p = packet;
bnc4e9283d2019-12-17 07:08:57 -08008507 size_t p_size = QUICHE_ARRAYSIZE(packet);
fkastenholz305e1732019-06-18 05:01:22 -07008508 if (VersionHasIetfQuicFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05008509 p = packet99;
bnc4e9283d2019-12-17 07:08:57 -08008510 p_size = QUICHE_ARRAYSIZE(packet99);
fayang36825da2019-08-21 14:01:27 -07008511 } else if (framer_.transport_version() >= QUIC_VERSION_46) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05008512 p = packet46;
bnc4e9283d2019-12-17 07:08:57 -08008513 p_size = QUICHE_ARRAYSIZE(packet46);
QUICHE teama6ef0a62019-03-07 20:34:33 -05008514 }
8515
dmcardle8f7df532020-01-07 13:28:57 -08008516 quiche::test::CompareCharArraysWithHexError(
8517 "constructed packet", data->data(), data->length(), AsChars(p), p_size);
QUICHE teama6ef0a62019-03-07 20:34:33 -05008518}
8519
8520TEST_P(QuicFramerTest, BuildPingPacket) {
QUICHE team2252b702019-05-14 23:55:14 -04008521 QuicFramerPeer::SetPerspective(&framer_, Perspective::IS_CLIENT);
QUICHE teama6ef0a62019-03-07 20:34:33 -05008522 QuicPacketHeader header;
8523 header.destination_connection_id = FramerTestConnectionId();
8524 header.reset_flag = false;
8525 header.version_flag = false;
8526 header.packet_number = kPacketNumber;
8527
8528 QuicFrames frames = {QuicFrame(QuicPingFrame())};
8529
8530 // clang-format off
8531 unsigned char packet[] = {
8532 // public flags (8 byte connection_id)
QUICHE team2252b702019-05-14 23:55:14 -04008533 0x2C,
QUICHE teama6ef0a62019-03-07 20:34:33 -05008534 // connection_id
8535 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
8536 // packet number
8537 0x12, 0x34, 0x56, 0x78,
8538
8539 // frame type (ping frame)
8540 0x07,
8541 };
8542
QUICHE teama6ef0a62019-03-07 20:34:33 -05008543 unsigned char packet46[] = {
8544 // type (short header, 4 byte packet number)
8545 0x43,
8546 // connection_id
8547 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
8548 // packet number
8549 0x12, 0x34, 0x56, 0x78,
8550
8551 // frame type
8552 0x07,
8553 };
8554
8555 unsigned char packet99[] = {
8556 // type (short header, 4 byte packet number)
8557 0x43,
8558 // connection_id
8559 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
8560 // packet number
8561 0x12, 0x34, 0x56, 0x78,
8562
8563 // frame type (IETF_PING frame)
8564 0x01,
8565 };
8566 // clang-format on
8567
8568 unsigned char* p = packet;
fkastenholz305e1732019-06-18 05:01:22 -07008569 if (VersionHasIetfQuicFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05008570 p = packet99;
fayang36825da2019-08-21 14:01:27 -07008571 } else if (framer_.transport_version() >= QUIC_VERSION_46) {
fayang374888f2019-05-31 06:47:21 -07008572 p = packet46;
QUICHE teama6ef0a62019-03-07 20:34:33 -05008573 }
8574
8575 std::unique_ptr<QuicPacket> data(BuildDataPacket(header, frames));
8576 ASSERT_TRUE(data != nullptr);
8577
dmcardle8f7df532020-01-07 13:28:57 -08008578 quiche::test::CompareCharArraysWithHexError(
QUICHE teama6ef0a62019-03-07 20:34:33 -05008579 "constructed packet", data->data(), data->length(), AsChars(p),
bnc4e9283d2019-12-17 07:08:57 -08008580 framer_.transport_version() > QUIC_VERSION_43 ? QUICHE_ARRAYSIZE(packet46)
8581 : QUICHE_ARRAYSIZE(packet));
QUICHE teama6ef0a62019-03-07 20:34:33 -05008582}
8583
fayang01062942020-01-22 07:23:23 -08008584TEST_P(QuicFramerTest, BuildHandshakeDonePacket) {
8585 QuicPacketHeader header;
8586 header.destination_connection_id = FramerTestConnectionId();
8587 header.reset_flag = false;
8588 header.version_flag = false;
8589 header.packet_number = kPacketNumber;
8590
8591 QuicFrames frames = {QuicFrame(QuicHandshakeDoneFrame())};
8592
8593 // clang-format off
8594 unsigned char packet[] = {
8595 // type (short header, 4 byte packet number)
8596 0x43,
8597 // connection_id
8598 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
8599 // packet number
8600 0x12, 0x34, 0x56, 0x78,
8601
8602 // frame type (Handshake done frame)
8603 0x1e,
8604 };
8605 // clang-format on
8606 if (!VersionHasIetfQuicFrames(framer_.transport_version())) {
8607 return;
8608 }
8609
8610 std::unique_ptr<QuicPacket> data(BuildDataPacket(header, frames));
8611 ASSERT_TRUE(data != nullptr);
8612
8613 quiche::test::CompareCharArraysWithHexError(
8614 "constructed packet", data->data(), data->length(), AsChars(packet),
8615 QUICHE_ARRAYSIZE(packet));
8616}
8617
QUICHE teama6ef0a62019-03-07 20:34:33 -05008618TEST_P(QuicFramerTest, BuildMessagePacket) {
dschinazicd86dd12019-11-14 10:11:13 -08008619 if (!VersionSupportsMessageFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05008620 return;
8621 }
QUICHE team2252b702019-05-14 23:55:14 -04008622 QuicFramerPeer::SetPerspective(&framer_, Perspective::IS_CLIENT);
QUICHE teama6ef0a62019-03-07 20:34:33 -05008623 QuicPacketHeader header;
8624 header.destination_connection_id = FramerTestConnectionId();
8625 header.reset_flag = false;
8626 header.version_flag = false;
8627 header.packet_number = kPacketNumber;
8628 QuicMemSliceStorage storage(nullptr, 0, nullptr, 0);
8629
wub553a9662019-03-28 20:13:23 -07008630 QuicMessageFrame frame(1, MakeSpan(&allocator_, "message", &storage));
8631 QuicMessageFrame frame2(2, MakeSpan(&allocator_, "message2", &storage));
QUICHE teama6ef0a62019-03-07 20:34:33 -05008632 QuicFrames frames = {QuicFrame(&frame), QuicFrame(&frame2)};
8633
8634 // clang-format off
QUICHE teama6ef0a62019-03-07 20:34:33 -05008635 unsigned char packet46[] = {
8636 // type (short header, 4 byte packet number)
8637 0x43,
8638 // connection_id
8639 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
8640 // packet number
8641 0x12, 0x34, 0x56, 0x78,
8642
8643 // frame type (message frame)
8644 0x21,
8645 // Length
8646 0x07,
8647 // Message Data
8648 'm', 'e', 's', 's', 'a', 'g', 'e',
8649 // frame type (message frame no length)
8650 0x20,
8651 // Message Data
8652 'm', 'e', 's', 's', 'a', 'g', 'e', '2'
8653 };
8654
8655 unsigned char packet99[] = {
8656 // type (short header, 4 byte packet number)
8657 0x43,
8658 // connection_id
8659 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
8660 // packet number
8661 0x12, 0x34, 0x56, 0x78,
8662
8663 // frame type (IETF_MESSAGE frame)
dschinazicd86dd12019-11-14 10:11:13 -08008664 0x31,
QUICHE teama6ef0a62019-03-07 20:34:33 -05008665 // Length
8666 0x07,
8667 // Message Data
8668 'm', 'e', 's', 's', 'a', 'g', 'e',
8669 // frame type (message frame no length)
dschinazicd86dd12019-11-14 10:11:13 -08008670 0x30,
QUICHE teama6ef0a62019-03-07 20:34:33 -05008671 // Message Data
8672 'm', 'e', 's', 's', 'a', 'g', 'e', '2'
8673 };
8674 // clang-format on
8675
fayang36825da2019-08-21 14:01:27 -07008676 unsigned char* p = packet46;
fkastenholz305e1732019-06-18 05:01:22 -07008677 if (VersionHasIetfQuicFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05008678 p = packet99;
QUICHE teama6ef0a62019-03-07 20:34:33 -05008679 }
8680
8681 std::unique_ptr<QuicPacket> data(BuildDataPacket(header, frames));
8682 ASSERT_TRUE(data != nullptr);
8683
dmcardle8f7df532020-01-07 13:28:57 -08008684 quiche::test::CompareCharArraysWithHexError(
8685 "constructed packet", data->data(), data->length(), AsChars(p),
8686 QUICHE_ARRAYSIZE(packet46));
QUICHE teama6ef0a62019-03-07 20:34:33 -05008687}
8688
QUICHE teama6ef0a62019-03-07 20:34:33 -05008689// Test that the MTU discovery packet is serialized correctly as a PING packet.
8690TEST_P(QuicFramerTest, BuildMtuDiscoveryPacket) {
QUICHE team2252b702019-05-14 23:55:14 -04008691 QuicFramerPeer::SetPerspective(&framer_, Perspective::IS_CLIENT);
QUICHE teama6ef0a62019-03-07 20:34:33 -05008692 QuicPacketHeader header;
8693 header.destination_connection_id = FramerTestConnectionId();
8694 header.reset_flag = false;
8695 header.version_flag = false;
8696 header.packet_number = kPacketNumber;
8697
8698 QuicFrames frames = {QuicFrame(QuicMtuDiscoveryFrame())};
8699
8700 // clang-format off
8701 unsigned char packet[] = {
8702 // public flags (8 byte connection_id)
QUICHE team2252b702019-05-14 23:55:14 -04008703 0x2C,
QUICHE teama6ef0a62019-03-07 20:34:33 -05008704 // connection_id
8705 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
8706 // packet number
8707 0x12, 0x34, 0x56, 0x78,
8708
8709 // frame type (ping frame)
8710 0x07,
8711 };
8712
QUICHE teama6ef0a62019-03-07 20:34:33 -05008713 unsigned char packet46[] = {
8714 // type (short header, 4 byte packet number)
8715 0x43,
8716 // connection_id
8717 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
8718 // packet number
8719 0x12, 0x34, 0x56, 0x78,
8720
8721 // frame type
8722 0x07,
8723 };
8724
8725 unsigned char packet99[] = {
8726 // type (short header, 4 byte packet number)
8727 0x43,
8728 // connection_id
8729 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
8730 // packet number
8731 0x12, 0x34, 0x56, 0x78,
8732
8733 // frame type (IETF_PING frame)
8734 0x01,
8735 };
8736 // clang-format on
8737
8738 std::unique_ptr<QuicPacket> data(BuildDataPacket(header, frames));
8739 ASSERT_TRUE(data != nullptr);
8740
8741 unsigned char* p = packet;
fkastenholz305e1732019-06-18 05:01:22 -07008742 if (VersionHasIetfQuicFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05008743 p = packet99;
fayang36825da2019-08-21 14:01:27 -07008744 } else if (framer_.transport_version() >= QUIC_VERSION_46) {
fayang374888f2019-05-31 06:47:21 -07008745 p = packet46;
QUICHE teama6ef0a62019-03-07 20:34:33 -05008746 }
8747
dmcardle8f7df532020-01-07 13:28:57 -08008748 quiche::test::CompareCharArraysWithHexError(
QUICHE teama6ef0a62019-03-07 20:34:33 -05008749 "constructed packet", data->data(), data->length(), AsChars(p),
bnc4e9283d2019-12-17 07:08:57 -08008750 framer_.transport_version() > QUIC_VERSION_43 ? QUICHE_ARRAYSIZE(packet46)
8751 : QUICHE_ARRAYSIZE(packet));
QUICHE teama6ef0a62019-03-07 20:34:33 -05008752}
8753
8754TEST_P(QuicFramerTest, BuildPublicResetPacket) {
8755 QuicPublicResetPacket reset_packet;
8756 reset_packet.connection_id = FramerTestConnectionId();
8757 reset_packet.nonce_proof = kNonceProof;
8758
8759 // clang-format off
8760 unsigned char packet[] = {
8761 // public flags (public reset, 8 byte ConnectionId)
8762 0x0E,
8763 // connection_id
8764 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
8765 // message tag (kPRST)
8766 'P', 'R', 'S', 'T',
8767 // num_entries (1) + padding
8768 0x01, 0x00, 0x00, 0x00,
8769 // tag kRNON
8770 'R', 'N', 'O', 'N',
8771 // end offset 8
8772 0x08, 0x00, 0x00, 0x00,
8773 // nonce proof
8774 0x89, 0x67, 0x45, 0x23,
8775 0x01, 0xEF, 0xCD, 0xAB,
8776 };
8777 // clang-format on
8778
8779 if (framer_.transport_version() > QUIC_VERSION_43) {
8780 return;
8781 }
8782
8783 std::unique_ptr<QuicEncryptedPacket> data(
8784 framer_.BuildPublicResetPacket(reset_packet));
8785 ASSERT_TRUE(data != nullptr);
dmcardle8f7df532020-01-07 13:28:57 -08008786 quiche::test::CompareCharArraysWithHexError(
8787 "constructed packet", data->data(), data->length(), AsChars(packet),
8788 QUICHE_ARRAYSIZE(packet));
QUICHE teama6ef0a62019-03-07 20:34:33 -05008789}
8790
8791TEST_P(QuicFramerTest, BuildPublicResetPacketWithClientAddress) {
8792 QuicPublicResetPacket reset_packet;
8793 reset_packet.connection_id = FramerTestConnectionId();
8794 reset_packet.nonce_proof = kNonceProof;
8795 reset_packet.client_address =
8796 QuicSocketAddress(QuicIpAddress::Loopback4(), 0x1234);
8797
8798 // clang-format off
8799 unsigned char packet[] = {
8800 // public flags (public reset, 8 byte ConnectionId)
8801 0x0E,
8802 // connection_id
8803 0xFE, 0xDC, 0xBA, 0x98,
8804 0x76, 0x54, 0x32, 0x10,
8805 // message tag (kPRST)
8806 'P', 'R', 'S', 'T',
8807 // num_entries (2) + padding
8808 0x02, 0x00, 0x00, 0x00,
8809 // tag kRNON
8810 'R', 'N', 'O', 'N',
8811 // end offset 8
8812 0x08, 0x00, 0x00, 0x00,
8813 // tag kCADR
8814 'C', 'A', 'D', 'R',
8815 // end offset 16
8816 0x10, 0x00, 0x00, 0x00,
8817 // nonce proof
8818 0x89, 0x67, 0x45, 0x23,
8819 0x01, 0xEF, 0xCD, 0xAB,
8820 // client address
8821 0x02, 0x00,
8822 0x7F, 0x00, 0x00, 0x01,
8823 0x34, 0x12,
8824 };
8825 // clang-format on
8826
8827 if (framer_.transport_version() > QUIC_VERSION_43) {
8828 return;
8829 }
8830
8831 std::unique_ptr<QuicEncryptedPacket> data(
8832 framer_.BuildPublicResetPacket(reset_packet));
8833 ASSERT_TRUE(data != nullptr);
8834
dmcardle8f7df532020-01-07 13:28:57 -08008835 quiche::test::CompareCharArraysWithHexError(
8836 "constructed packet", data->data(), data->length(), AsChars(packet),
8837 QUICHE_ARRAYSIZE(packet));
QUICHE teama6ef0a62019-03-07 20:34:33 -05008838}
8839
8840TEST_P(QuicFramerTest, BuildPublicResetPacketWithEndpointId) {
8841 QuicPublicResetPacket reset_packet;
8842 reset_packet.connection_id = FramerTestConnectionId();
8843 reset_packet.nonce_proof = kNonceProof;
8844 reset_packet.endpoint_id = "FakeServerId";
8845
8846 // The tag value map in CryptoHandshakeMessage is a std::map, so the two tags
8847 // in the packet, kRNON and kEPID, have unspecified ordering w.r.t each other.
8848 // clang-format off
8849 unsigned char packet_variant1[] = {
8850 // public flags (public reset, 8 byte ConnectionId)
8851 0x0E,
8852 // connection_id
8853 0xFE, 0xDC, 0xBA, 0x98,
8854 0x76, 0x54, 0x32, 0x10,
8855 // message tag (kPRST)
8856 'P', 'R', 'S', 'T',
8857 // num_entries (2) + padding
8858 0x02, 0x00, 0x00, 0x00,
8859 // tag kRNON
8860 'R', 'N', 'O', 'N',
8861 // end offset 8
8862 0x08, 0x00, 0x00, 0x00,
8863 // tag kEPID
8864 'E', 'P', 'I', 'D',
8865 // end offset 20
8866 0x14, 0x00, 0x00, 0x00,
8867 // nonce proof
8868 0x89, 0x67, 0x45, 0x23,
8869 0x01, 0xEF, 0xCD, 0xAB,
8870 // Endpoint ID
8871 'F', 'a', 'k', 'e', 'S', 'e', 'r', 'v', 'e', 'r', 'I', 'd',
8872 };
8873 unsigned char packet_variant2[] = {
8874 // public flags (public reset, 8 byte ConnectionId)
8875 0x0E,
8876 // connection_id
8877 0xFE, 0xDC, 0xBA, 0x98,
8878 0x76, 0x54, 0x32, 0x10,
8879 // message tag (kPRST)
8880 'P', 'R', 'S', 'T',
8881 // num_entries (2) + padding
8882 0x02, 0x00, 0x00, 0x00,
8883 // tag kEPID
8884 'E', 'P', 'I', 'D',
8885 // end offset 12
8886 0x0C, 0x00, 0x00, 0x00,
8887 // tag kRNON
8888 'R', 'N', 'O', 'N',
8889 // end offset 20
8890 0x14, 0x00, 0x00, 0x00,
8891 // Endpoint ID
8892 'F', 'a', 'k', 'e', 'S', 'e', 'r', 'v', 'e', 'r', 'I', 'd',
8893 // nonce proof
8894 0x89, 0x67, 0x45, 0x23,
8895 0x01, 0xEF, 0xCD, 0xAB,
8896 };
8897 // clang-format on
8898
8899 if (framer_.transport_version() > QUIC_VERSION_43) {
8900 return;
8901 }
8902
8903 std::unique_ptr<QuicEncryptedPacket> data(
8904 framer_.BuildPublicResetPacket(reset_packet));
8905 ASSERT_TRUE(data != nullptr);
8906
8907 // Variant 1 ends with char 'd'. Variant 1 ends with char 0xAB.
8908 if ('d' == data->data()[data->length() - 1]) {
dmcardle8f7df532020-01-07 13:28:57 -08008909 quiche::test::CompareCharArraysWithHexError(
QUICHE teama6ef0a62019-03-07 20:34:33 -05008910 "constructed packet", data->data(), data->length(),
bnc4e9283d2019-12-17 07:08:57 -08008911 AsChars(packet_variant1), QUICHE_ARRAYSIZE(packet_variant1));
QUICHE teama6ef0a62019-03-07 20:34:33 -05008912 } else {
dmcardle8f7df532020-01-07 13:28:57 -08008913 quiche::test::CompareCharArraysWithHexError(
QUICHE teama6ef0a62019-03-07 20:34:33 -05008914 "constructed packet", data->data(), data->length(),
bnc4e9283d2019-12-17 07:08:57 -08008915 AsChars(packet_variant2), QUICHE_ARRAYSIZE(packet_variant2));
QUICHE teama6ef0a62019-03-07 20:34:33 -05008916 }
8917}
8918
8919TEST_P(QuicFramerTest, BuildIetfStatelessResetPacket) {
8920 // clang-format off
fayang36825da2019-08-21 14:01:27 -07008921 unsigned char packet[] = {
QUICHE teama6ef0a62019-03-07 20:34:33 -05008922 // type (short header, 1 byte packet number)
8923 0x70,
8924 // random packet number
8925 0xFE,
8926 // stateless reset token
8927 0xB5, 0x69, 0x0F, 0x00, 0x00, 0x00, 0x00, 0x00,
8928 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
8929 };
8930 // clang-format on
8931
8932 std::unique_ptr<QuicEncryptedPacket> data(
8933 framer_.BuildIetfStatelessResetPacket(FramerTestConnectionId(),
8934 kTestStatelessResetToken));
8935 ASSERT_TRUE(data != nullptr);
8936 // Skip packet number byte which is random in stateless reset packet.
dmcardle8f7df532020-01-07 13:28:57 -08008937 quiche::test::CompareCharArraysWithHexError(
8938 "constructed packet", data->data(), 1, AsChars(packet), 1);
QUICHE teama6ef0a62019-03-07 20:34:33 -05008939 const size_t random_bytes_length =
8940 data->length() - kPacketHeaderTypeSize - sizeof(kTestStatelessResetToken);
8941 EXPECT_EQ(kMinRandomBytesLengthInStatelessReset, random_bytes_length);
8942 // Verify stateless reset token is correct.
dmcardle8f7df532020-01-07 13:28:57 -08008943 quiche::test::CompareCharArraysWithHexError(
QUICHE teama6ef0a62019-03-07 20:34:33 -05008944 "constructed packet",
8945 data->data() + data->length() - sizeof(kTestStatelessResetToken),
8946 sizeof(kTestStatelessResetToken),
bnc4e9283d2019-12-17 07:08:57 -08008947 AsChars(packet) + QUICHE_ARRAYSIZE(packet) -
QUICHE teama6ef0a62019-03-07 20:34:33 -05008948 sizeof(kTestStatelessResetToken),
8949 sizeof(kTestStatelessResetToken));
8950}
8951
8952TEST_P(QuicFramerTest, EncryptPacket) {
8953 QuicPacketNumber packet_number = kPacketNumber;
8954 // clang-format off
8955 unsigned char packet[] = {
8956 // public flags (8 byte connection_id)
8957 0x28,
8958 // connection_id
8959 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
8960 // packet number
8961 0x12, 0x34, 0x56, 0x78,
8962
8963 // redundancy
8964 'a', 'b', 'c', 'd',
8965 'e', 'f', 'g', 'h',
8966 'i', 'j', 'k', 'l',
8967 'm', 'n', 'o', 'p',
8968 };
8969
QUICHE teama6ef0a62019-03-07 20:34:33 -05008970 unsigned char packet46[] = {
8971 // type (short header, 4 byte packet number)
8972 0x43,
8973 // connection_id
8974 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
8975 // packet number
8976 0x12, 0x34, 0x56, 0x78,
8977
8978 // redundancy
8979 'a', 'b', 'c', 'd',
8980 'e', 'f', 'g', 'h',
8981 'i', 'j', 'k', 'l',
8982 'm', 'n', 'o', 'p',
8983 };
8984
nharperc32d8ab2019-10-09 11:09:06 -07008985 unsigned char packet50[] = {
QUICHE teama6ef0a62019-03-07 20:34:33 -05008986 // type (short header, 4 byte packet number)
8987 0x43,
8988 // connection_id
8989 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
8990 // packet number
8991 0x12, 0x34, 0x56, 0x78,
8992
8993 // redundancy
8994 'a', 'b', 'c', 'd',
8995 'e', 'f', 'g', 'h',
8996 'i', 'j', 'k', 'l',
8997 'm', 'n', 'o', 'p',
nharper55fa6132019-05-07 19:37:21 -07008998 'q', 'r', 's', 't',
QUICHE teama6ef0a62019-03-07 20:34:33 -05008999 };
9000 // clang-format on
9001
9002 unsigned char* p = packet;
bnc4e9283d2019-12-17 07:08:57 -08009003 size_t p_size = QUICHE_ARRAYSIZE(packet);
nharperc32d8ab2019-10-09 11:09:06 -07009004 if (framer_.transport_version() >= QUIC_VERSION_50) {
9005 p = packet50;
bnc4e9283d2019-12-17 07:08:57 -08009006 p_size = QUICHE_ARRAYSIZE(packet50);
fayang36825da2019-08-21 14:01:27 -07009007 } else if (framer_.transport_version() >= QUIC_VERSION_46) {
fayang374888f2019-05-31 06:47:21 -07009008 p = packet46;
QUICHE teama6ef0a62019-03-07 20:34:33 -05009009 }
9010
9011 std::unique_ptr<QuicPacket> raw(new QuicPacket(
nharper55fa6132019-05-07 19:37:21 -07009012 AsChars(p), p_size, false, PACKET_8BYTE_CONNECTION_ID,
QUICHE teama6ef0a62019-03-07 20:34:33 -05009013 PACKET_0BYTE_CONNECTION_ID, !kIncludeVersion,
9014 !kIncludeDiversificationNonce, PACKET_4BYTE_PACKET_NUMBER,
9015 VARIABLE_LENGTH_INTEGER_LENGTH_0, 0, VARIABLE_LENGTH_INTEGER_LENGTH_0));
dschinazi66dea072019-04-09 11:41:06 -07009016 char buffer[kMaxOutgoingPacketSize];
QUICHE teama6ef0a62019-03-07 20:34:33 -05009017 size_t encrypted_length = framer_.EncryptPayload(
dschinazi66dea072019-04-09 11:41:06 -07009018 ENCRYPTION_INITIAL, packet_number, *raw, buffer, kMaxOutgoingPacketSize);
QUICHE teama6ef0a62019-03-07 20:34:33 -05009019
9020 ASSERT_NE(0u, encrypted_length);
9021 EXPECT_TRUE(CheckEncryption(packet_number, raw.get()));
9022}
9023
9024TEST_P(QuicFramerTest, EncryptPacketWithVersionFlag) {
nharper55fa6132019-05-07 19:37:21 -07009025 QuicFramerPeer::SetPerspective(&framer_, Perspective::IS_CLIENT);
QUICHE teama6ef0a62019-03-07 20:34:33 -05009026 QuicPacketNumber packet_number = kPacketNumber;
9027 // clang-format off
9028 unsigned char packet[] = {
9029 // public flags (version, 8 byte connection_id)
9030 0x29,
9031 // connection_id
9032 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
9033 // version tag
9034 'Q', '.', '1', '0',
9035 // packet number
9036 0x12, 0x34, 0x56, 0x78,
9037
9038 // redundancy
9039 'a', 'b', 'c', 'd',
9040 'e', 'f', 'g', 'h',
9041 'i', 'j', 'k', 'l',
9042 'm', 'n', 'o', 'p',
9043 };
9044
QUICHE teama6ef0a62019-03-07 20:34:33 -05009045 unsigned char packet46[] = {
9046 // type (long header with packet type ZERO_RTT_PROTECTED)
9047 0xD3,
9048 // version tag
9049 'Q', '.', '1', '0',
9050 // connection_id length
9051 0x50,
9052 // connection_id
9053 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
9054 // packet number
9055 0x12, 0x34, 0x56, 0x78,
9056
9057 // redundancy
9058 'a', 'b', 'c', 'd',
9059 'e', 'f', 'g', 'h',
9060 'i', 'j', 'k', 'l',
9061 'm', 'n', 'o', 'p',
9062 };
9063
nharperc32d8ab2019-10-09 11:09:06 -07009064 unsigned char packet50[] = {
QUICHE teama6ef0a62019-03-07 20:34:33 -05009065 // type (long header with packet type ZERO_RTT_PROTECTED)
9066 0xD3,
9067 // version tag
9068 'Q', '.', '1', '0',
dschinazi48ac9192019-07-31 00:07:26 -07009069 // destination connection ID length
9070 0x08,
9071 // destination connection ID
QUICHE teama6ef0a62019-03-07 20:34:33 -05009072 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
dschinazi48ac9192019-07-31 00:07:26 -07009073 // source connection ID length
9074 0x00,
QUICHE teama6ef0a62019-03-07 20:34:33 -05009075 // packet number
9076 0x12, 0x34, 0x56, 0x78,
9077
9078 // redundancy
9079 'a', 'b', 'c', 'd',
9080 'e', 'f', 'g', 'h',
9081 'i', 'j', 'k', 'l',
9082 'm', 'n', 'o', 'p',
nharper55fa6132019-05-07 19:37:21 -07009083 'q', 'r', 's', 't',
QUICHE teama6ef0a62019-03-07 20:34:33 -05009084 };
9085 // clang-format on
9086
9087 unsigned char* p = packet;
bnc4e9283d2019-12-17 07:08:57 -08009088 size_t p_size = QUICHE_ARRAYSIZE(packet);
fkastenholz305e1732019-06-18 05:01:22 -07009089 // TODO(ianswett): see todo in previous test.
nharperc32d8ab2019-10-09 11:09:06 -07009090 if (framer_.transport_version() >= QUIC_VERSION_50) {
9091 p = packet50;
bnc4e9283d2019-12-17 07:08:57 -08009092 p_size = QUICHE_ARRAYSIZE(packet50);
fayang36825da2019-08-21 14:01:27 -07009093 } else if (framer_.transport_version() >= QUIC_VERSION_46) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05009094 p = packet46;
bnc4e9283d2019-12-17 07:08:57 -08009095 p_size = QUICHE_ARRAYSIZE(packet46);
QUICHE teama6ef0a62019-03-07 20:34:33 -05009096 }
9097
9098 std::unique_ptr<QuicPacket> raw(new QuicPacket(
nharper55fa6132019-05-07 19:37:21 -07009099 AsChars(p), p_size, false, PACKET_8BYTE_CONNECTION_ID,
9100 PACKET_0BYTE_CONNECTION_ID, kIncludeVersion,
9101 !kIncludeDiversificationNonce, PACKET_4BYTE_PACKET_NUMBER,
9102 VARIABLE_LENGTH_INTEGER_LENGTH_0, 0, VARIABLE_LENGTH_INTEGER_LENGTH_0));
dschinazi66dea072019-04-09 11:41:06 -07009103 char buffer[kMaxOutgoingPacketSize];
QUICHE teama6ef0a62019-03-07 20:34:33 -05009104 size_t encrypted_length = framer_.EncryptPayload(
dschinazi66dea072019-04-09 11:41:06 -07009105 ENCRYPTION_INITIAL, packet_number, *raw, buffer, kMaxOutgoingPacketSize);
QUICHE teama6ef0a62019-03-07 20:34:33 -05009106
9107 ASSERT_NE(0u, encrypted_length);
9108 EXPECT_TRUE(CheckEncryption(packet_number, raw.get()));
9109}
9110
9111TEST_P(QuicFramerTest, AckTruncationLargePacket) {
fkastenholz305e1732019-06-18 05:01:22 -07009112 if (VersionHasIetfQuicFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05009113 // This test is not applicable to this version; the range count is
9114 // effectively unlimited
9115 return;
9116 }
nharper9bb83462019-05-01 10:53:22 -07009117 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
QUICHE teama6ef0a62019-03-07 20:34:33 -05009118
9119 QuicPacketHeader header;
9120 header.destination_connection_id = FramerTestConnectionId();
9121 header.reset_flag = false;
9122 header.version_flag = false;
9123 header.packet_number = kPacketNumber;
9124
9125 QuicAckFrame ack_frame;
9126 // Create a packet with just the ack.
9127 ack_frame = MakeAckFrameWithAckBlocks(300, 0u);
9128 QuicFrames frames = {QuicFrame(&ack_frame)};
9129
9130 // Build an ack packet with truncation due to limit in number of nack ranges.
9131 QuicFramerPeer::SetPerspective(&framer_, Perspective::IS_CLIENT);
9132 std::unique_ptr<QuicPacket> raw_ack_packet(BuildDataPacket(header, frames));
9133 ASSERT_TRUE(raw_ack_packet != nullptr);
dschinazi66dea072019-04-09 11:41:06 -07009134 char buffer[kMaxOutgoingPacketSize];
QUICHE teama6ef0a62019-03-07 20:34:33 -05009135 size_t encrypted_length =
QUICHE team6987b4a2019-03-15 16:23:04 -07009136 framer_.EncryptPayload(ENCRYPTION_INITIAL, header.packet_number,
dschinazi66dea072019-04-09 11:41:06 -07009137 *raw_ack_packet, buffer, kMaxOutgoingPacketSize);
QUICHE teama6ef0a62019-03-07 20:34:33 -05009138 ASSERT_NE(0u, encrypted_length);
9139 // Now make sure we can turn our ack packet back into an ack frame.
9140 QuicFramerPeer::SetPerspective(&framer_, Perspective::IS_SERVER);
9141 ASSERT_TRUE(framer_.ProcessPacket(
9142 QuicEncryptedPacket(buffer, encrypted_length, false)));
9143 ASSERT_EQ(1u, visitor_.ack_frames_.size());
9144 QuicAckFrame& processed_ack_frame = *visitor_.ack_frames_[0];
9145 EXPECT_EQ(QuicPacketNumber(600u), LargestAcked(processed_ack_frame));
9146 ASSERT_EQ(256u, processed_ack_frame.packets.NumPacketsSlow());
9147 EXPECT_EQ(QuicPacketNumber(90u), processed_ack_frame.packets.Min());
9148 EXPECT_EQ(QuicPacketNumber(600u), processed_ack_frame.packets.Max());
9149}
9150
9151TEST_P(QuicFramerTest, AckTruncationSmallPacket) {
fkastenholz305e1732019-06-18 05:01:22 -07009152 if (VersionHasIetfQuicFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05009153 // This test is not applicable to this version; the range count is
9154 // effectively unlimited
9155 return;
9156 }
nharper9bb83462019-05-01 10:53:22 -07009157 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
QUICHE teama6ef0a62019-03-07 20:34:33 -05009158
9159 QuicPacketHeader header;
9160 header.destination_connection_id = FramerTestConnectionId();
9161 header.reset_flag = false;
9162 header.version_flag = false;
9163 header.packet_number = kPacketNumber;
9164
9165 // Create a packet with just the ack.
9166 QuicAckFrame ack_frame;
9167 ack_frame = MakeAckFrameWithAckBlocks(300, 0u);
9168 QuicFrames frames = {QuicFrame(&ack_frame)};
9169
9170 // Build an ack packet with truncation due to limit in number of nack ranges.
9171 QuicFramerPeer::SetPerspective(&framer_, Perspective::IS_CLIENT);
9172 std::unique_ptr<QuicPacket> raw_ack_packet(
9173 BuildDataPacket(header, frames, 500));
9174 ASSERT_TRUE(raw_ack_packet != nullptr);
dschinazi66dea072019-04-09 11:41:06 -07009175 char buffer[kMaxOutgoingPacketSize];
QUICHE teama6ef0a62019-03-07 20:34:33 -05009176 size_t encrypted_length =
QUICHE team6987b4a2019-03-15 16:23:04 -07009177 framer_.EncryptPayload(ENCRYPTION_INITIAL, header.packet_number,
dschinazi66dea072019-04-09 11:41:06 -07009178 *raw_ack_packet, buffer, kMaxOutgoingPacketSize);
QUICHE teama6ef0a62019-03-07 20:34:33 -05009179 ASSERT_NE(0u, encrypted_length);
9180 // Now make sure we can turn our ack packet back into an ack frame.
9181 QuicFramerPeer::SetPerspective(&framer_, Perspective::IS_SERVER);
9182 ASSERT_TRUE(framer_.ProcessPacket(
9183 QuicEncryptedPacket(buffer, encrypted_length, false)));
9184 ASSERT_EQ(1u, visitor_.ack_frames_.size());
9185 QuicAckFrame& processed_ack_frame = *visitor_.ack_frames_[0];
9186 EXPECT_EQ(QuicPacketNumber(600u), LargestAcked(processed_ack_frame));
9187 ASSERT_EQ(240u, processed_ack_frame.packets.NumPacketsSlow());
9188 EXPECT_EQ(QuicPacketNumber(122u), processed_ack_frame.packets.Min());
9189 EXPECT_EQ(QuicPacketNumber(600u), processed_ack_frame.packets.Max());
9190}
9191
9192TEST_P(QuicFramerTest, CleanTruncation) {
fkastenholz305e1732019-06-18 05:01:22 -07009193 if (VersionHasIetfQuicFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05009194 // This test is not applicable to this version; the range count is
9195 // effectively unlimited
9196 return;
9197 }
nharper9bb83462019-05-01 10:53:22 -07009198 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
QUICHE teama6ef0a62019-03-07 20:34:33 -05009199
9200 QuicPacketHeader header;
9201 header.destination_connection_id = FramerTestConnectionId();
9202 header.reset_flag = false;
9203 header.version_flag = false;
9204 header.packet_number = kPacketNumber;
9205
9206 QuicAckFrame ack_frame = InitAckFrame(201);
9207
9208 // Create a packet with just the ack.
9209 QuicFrames frames = {QuicFrame(&ack_frame)};
nharperc32d8ab2019-10-09 11:09:06 -07009210 if (framer_.version().HasHeaderProtection()) {
9211 frames.push_back(QuicFrame(QuicPaddingFrame(12)));
9212 }
QUICHE teama6ef0a62019-03-07 20:34:33 -05009213 QuicFramerPeer::SetPerspective(&framer_, Perspective::IS_CLIENT);
9214 std::unique_ptr<QuicPacket> raw_ack_packet(BuildDataPacket(header, frames));
9215 ASSERT_TRUE(raw_ack_packet != nullptr);
9216
dschinazi66dea072019-04-09 11:41:06 -07009217 char buffer[kMaxOutgoingPacketSize];
QUICHE teama6ef0a62019-03-07 20:34:33 -05009218 size_t encrypted_length =
QUICHE team6987b4a2019-03-15 16:23:04 -07009219 framer_.EncryptPayload(ENCRYPTION_INITIAL, header.packet_number,
dschinazi66dea072019-04-09 11:41:06 -07009220 *raw_ack_packet, buffer, kMaxOutgoingPacketSize);
QUICHE teama6ef0a62019-03-07 20:34:33 -05009221 ASSERT_NE(0u, encrypted_length);
9222
9223 // Now make sure we can turn our ack packet back into an ack frame.
9224 QuicFramerPeer::SetPerspective(&framer_, Perspective::IS_SERVER);
9225 ASSERT_TRUE(framer_.ProcessPacket(
9226 QuicEncryptedPacket(buffer, encrypted_length, false)));
9227
9228 // Test for clean truncation of the ack by comparing the length of the
9229 // original packets to the re-serialized packets.
9230 frames.clear();
9231 frames.push_back(QuicFrame(visitor_.ack_frames_[0].get()));
nharperc32d8ab2019-10-09 11:09:06 -07009232 if (framer_.version().HasHeaderProtection()) {
9233 frames.push_back(QuicFrame(*visitor_.padding_frames_[0].get()));
9234 }
QUICHE teama6ef0a62019-03-07 20:34:33 -05009235
9236 size_t original_raw_length = raw_ack_packet->length();
9237 QuicFramerPeer::SetPerspective(&framer_, Perspective::IS_CLIENT);
9238 raw_ack_packet = BuildDataPacket(header, frames);
9239 ASSERT_TRUE(raw_ack_packet != nullptr);
9240 EXPECT_EQ(original_raw_length, raw_ack_packet->length());
9241 ASSERT_TRUE(raw_ack_packet != nullptr);
9242}
9243
9244TEST_P(QuicFramerTest, StopPacketProcessing) {
zhongyi546cc452019-04-12 15:27:49 -07009245 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
QUICHE teama6ef0a62019-03-07 20:34:33 -05009246 // clang-format off
9247 unsigned char packet[] = {
9248 // public flags (8 byte connection_id)
9249 0x28,
9250 // connection_id
9251 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
9252 // packet number
9253 0x12, 0x34, 0x56, 0x78,
9254
9255 // frame type (stream frame with fin)
9256 0xFF,
9257 // stream id
9258 0x01, 0x02, 0x03, 0x04,
9259 // offset
9260 0x3A, 0x98, 0xFE, 0xDC,
9261 0x32, 0x10, 0x76, 0x54,
9262 // data length
9263 0x00, 0x0c,
9264 // data
9265 'h', 'e', 'l', 'l',
9266 'o', ' ', 'w', 'o',
9267 'r', 'l', 'd', '!',
9268
9269 // frame type (ack frame)
9270 0x40,
9271 // least packet number awaiting an ack
9272 0x12, 0x34, 0x56, 0x78,
9273 0x9A, 0xA0,
9274 // largest observed packet number
9275 0x12, 0x34, 0x56, 0x78,
9276 0x9A, 0xBF,
9277 // num missing packets
9278 0x01,
9279 // missing packet
9280 0x12, 0x34, 0x56, 0x78,
9281 0x9A, 0xBE,
9282 };
9283
QUICHE teama6ef0a62019-03-07 20:34:33 -05009284 unsigned char packet46[] = {
9285 // type (short header, 4 byte packet number)
9286 0x43,
9287 // connection_id
9288 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
9289 // packet number
9290 0x12, 0x34, 0x56, 0x78,
9291
9292 // frame type (stream frame with fin)
9293 0xFF,
9294 // stream id
9295 0x01, 0x02, 0x03, 0x04,
9296 // offset
9297 0x3A, 0x98, 0xFE, 0xDC,
9298 0x32, 0x10, 0x76, 0x54,
9299 // data length
9300 0x00, 0x0c,
9301 // data
9302 'h', 'e', 'l', 'l',
9303 'o', ' ', 'w', 'o',
9304 'r', 'l', 'd', '!',
9305
9306 // frame type (ack frame)
9307 0x40,
9308 // least packet number awaiting an ack
9309 0x12, 0x34, 0x56, 0x78,
9310 0x9A, 0xA0,
9311 // largest observed packet number
9312 0x12, 0x34, 0x56, 0x78,
9313 0x9A, 0xBF,
9314 // num missing packets
9315 0x01,
9316 // missing packet
9317 0x12, 0x34, 0x56, 0x78,
9318 0x9A, 0xBE,
9319 };
9320
9321 unsigned char packet99[] = {
9322 // type (short header, 4 byte packet number)
9323 0x43,
9324 // connection_id
9325 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
9326 // packet number
9327 0x12, 0x34, 0x56, 0x78,
9328
9329 // frame type (IETF_STREAM frame with fin, length, and offset bits set)
9330 0x08 | 0x01 | 0x02 | 0x04,
9331 // stream id
9332 kVarInt62FourBytes + 0x01, 0x02, 0x03, 0x04,
9333 // offset
9334 kVarInt62EightBytes + 0x3A, 0x98, 0xFE, 0xDC,
9335 0x32, 0x10, 0x76, 0x54,
9336 // data length
9337 kVarInt62TwoBytes + 0x00, 0x0c,
9338 // data
9339 'h', 'e', 'l', 'l',
9340 'o', ' ', 'w', 'o',
9341 'r', 'l', 'd', '!',
9342
9343 // frame type (ack frame)
9344 0x0d,
9345 // largest observed packet number
9346 kVarInt62FourBytes + 0x12, 0x34, 0x56, 0x78,
9347 // Delta time
9348 kVarInt62OneByte + 0x00,
9349 // Ack Block count
9350 kVarInt62OneByte + 0x01,
9351 // First block size (one packet)
9352 kVarInt62OneByte + 0x00,
9353
9354 // Next gap size & ack. Missing all preceding packets
9355 kVarInt62FourBytes + 0x12, 0x34, 0x56, 0x77,
9356 kVarInt62OneByte + 0x00,
9357 };
9358 // clang-format on
9359
9360 MockFramerVisitor visitor;
9361 framer_.set_visitor(&visitor);
9362 EXPECT_CALL(visitor, OnPacket());
9363 EXPECT_CALL(visitor, OnPacketHeader(_));
9364 EXPECT_CALL(visitor, OnStreamFrame(_)).WillOnce(Return(false));
9365 EXPECT_CALL(visitor, OnPacketComplete());
9366 EXPECT_CALL(visitor, OnUnauthenticatedPublicHeader(_)).WillOnce(Return(true));
9367 EXPECT_CALL(visitor, OnUnauthenticatedHeader(_)).WillOnce(Return(true));
9368 EXPECT_CALL(visitor, OnDecryptedPacket(_));
9369
9370 unsigned char* p = packet;
bnc4e9283d2019-12-17 07:08:57 -08009371 size_t p_size = QUICHE_ARRAYSIZE(packet);
fkastenholz305e1732019-06-18 05:01:22 -07009372 if (VersionHasIetfQuicFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05009373 p = packet99;
bnc4e9283d2019-12-17 07:08:57 -08009374 p_size = QUICHE_ARRAYSIZE(packet99);
fayang36825da2019-08-21 14:01:27 -07009375 } else if (framer_.transport_version() >= QUIC_VERSION_46) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05009376 p = packet46;
bnc4e9283d2019-12-17 07:08:57 -08009377 p_size = QUICHE_ARRAYSIZE(packet46);
QUICHE teama6ef0a62019-03-07 20:34:33 -05009378 }
9379 QuicEncryptedPacket encrypted(AsChars(p), p_size, false);
9380 EXPECT_TRUE(framer_.ProcessPacket(encrypted));
bncf6f82b12019-10-30 07:01:01 -07009381 EXPECT_THAT(framer_.error(), IsQuicNoError());
QUICHE teama6ef0a62019-03-07 20:34:33 -05009382}
9383
9384static char kTestString[] = "At least 20 characters.";
9385static QuicStreamId kTestQuicStreamId = 1;
9386static bool ExpectedStreamFrame(const QuicStreamFrame& frame) {
9387 return (frame.stream_id == kTestQuicStreamId ||
nharper46833c32019-05-15 21:33:05 -07009388 QuicUtils::IsCryptoStreamId(QUIC_VERSION_99, frame.stream_id)) &&
QUICHE teama6ef0a62019-03-07 20:34:33 -05009389 !frame.fin && frame.offset == 0 &&
vasilvvc48c8712019-03-11 13:38:16 -07009390 std::string(frame.data_buffer, frame.data_length) == kTestString;
QUICHE teama6ef0a62019-03-07 20:34:33 -05009391 // FIN is hard-coded false in ConstructEncryptedPacket.
9392 // Offset 0 is hard-coded in ConstructEncryptedPacket.
9393}
9394
9395// Verify that the packet returned by ConstructEncryptedPacket() can be properly
9396// parsed by the framer.
9397TEST_P(QuicFramerTest, ConstructEncryptedPacket) {
9398 // Since we are using ConstructEncryptedPacket, we have to set the framer's
9399 // crypto to be Null.
zhongyi546cc452019-04-12 15:27:49 -07009400 if (framer_.version().KnowsWhichDecrypterToUse()) {
9401 framer_.InstallDecrypter(
9402 ENCRYPTION_FORWARD_SECURE,
vasilvv0fc587f2019-09-06 13:33:08 -07009403 std::make_unique<NullDecrypter>(framer_.perspective()));
zhongyi546cc452019-04-12 15:27:49 -07009404 } else {
vasilvv0fc587f2019-09-06 13:33:08 -07009405 framer_.SetDecrypter(ENCRYPTION_INITIAL, std::make_unique<NullDecrypter>(
9406 framer_.perspective()));
zhongyi546cc452019-04-12 15:27:49 -07009407 }
QUICHE teama6ef0a62019-03-07 20:34:33 -05009408 ParsedQuicVersionVector versions;
9409 versions.push_back(framer_.version());
9410 std::unique_ptr<QuicEncryptedPacket> packet(ConstructEncryptedPacket(
9411 TestConnectionId(), EmptyQuicConnectionId(), false, false,
9412 kTestQuicStreamId, kTestString, CONNECTION_ID_PRESENT,
9413 CONNECTION_ID_ABSENT, PACKET_4BYTE_PACKET_NUMBER, &versions));
9414
9415 MockFramerVisitor visitor;
9416 framer_.set_visitor(&visitor);
9417 EXPECT_CALL(visitor, OnPacket()).Times(1);
9418 EXPECT_CALL(visitor, OnUnauthenticatedPublicHeader(_))
9419 .Times(1)
9420 .WillOnce(Return(true));
9421 EXPECT_CALL(visitor, OnUnauthenticatedHeader(_))
9422 .Times(1)
9423 .WillOnce(Return(true));
9424 EXPECT_CALL(visitor, OnPacketHeader(_)).Times(1).WillOnce(Return(true));
9425 EXPECT_CALL(visitor, OnDecryptedPacket(_)).Times(1);
9426 EXPECT_CALL(visitor, OnError(_)).Times(0);
9427 EXPECT_CALL(visitor, OnStreamFrame(_)).Times(0);
QUICHE teamea740082019-03-11 17:58:43 -07009428 if (!QuicVersionUsesCryptoFrames(framer_.version().transport_version)) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05009429 EXPECT_CALL(visitor, OnStreamFrame(Truly(ExpectedStreamFrame))).Times(1);
9430 } else {
9431 EXPECT_CALL(visitor, OnCryptoFrame(_)).Times(1);
9432 }
9433 EXPECT_CALL(visitor, OnPacketComplete()).Times(1);
9434
9435 EXPECT_TRUE(framer_.ProcessPacket(*packet));
bncf6f82b12019-10-30 07:01:01 -07009436 EXPECT_THAT(framer_.error(), IsQuicNoError());
QUICHE teama6ef0a62019-03-07 20:34:33 -05009437}
9438
9439// Verify that the packet returned by ConstructMisFramedEncryptedPacket()
9440// does cause the framer to return an error.
9441TEST_P(QuicFramerTest, ConstructMisFramedEncryptedPacket) {
9442 // Since we are using ConstructEncryptedPacket, we have to set the framer's
9443 // crypto to be Null.
zhongyi546cc452019-04-12 15:27:49 -07009444 if (framer_.version().KnowsWhichDecrypterToUse()) {
nharper9bb83462019-05-01 10:53:22 -07009445 framer_.InstallDecrypter(
9446 ENCRYPTION_FORWARD_SECURE,
vasilvv0fc587f2019-09-06 13:33:08 -07009447 std::make_unique<NullDecrypter>(framer_.perspective()));
zhongyi546cc452019-04-12 15:27:49 -07009448 } else {
vasilvv0fc587f2019-09-06 13:33:08 -07009449 framer_.SetDecrypter(ENCRYPTION_INITIAL, std::make_unique<NullDecrypter>(
9450 framer_.perspective()));
zhongyi546cc452019-04-12 15:27:49 -07009451 }
QUICHE team6987b4a2019-03-15 16:23:04 -07009452 framer_.SetEncrypter(ENCRYPTION_INITIAL,
vasilvv0fc587f2019-09-06 13:33:08 -07009453 std::make_unique<NullEncrypter>(framer_.perspective()));
QUICHE teama6ef0a62019-03-07 20:34:33 -05009454 std::unique_ptr<QuicEncryptedPacket> packet(ConstructMisFramedEncryptedPacket(
9455 TestConnectionId(), EmptyQuicConnectionId(), false, false,
9456 kTestQuicStreamId, kTestString, CONNECTION_ID_PRESENT,
bnc9711a9e2019-11-01 06:31:51 -07009457 CONNECTION_ID_ABSENT, PACKET_4BYTE_PACKET_NUMBER, framer_.version(),
QUICHE teama6ef0a62019-03-07 20:34:33 -05009458 Perspective::IS_CLIENT));
9459
9460 MockFramerVisitor visitor;
9461 framer_.set_visitor(&visitor);
9462 EXPECT_CALL(visitor, OnPacket()).Times(1);
9463 EXPECT_CALL(visitor, OnUnauthenticatedPublicHeader(_))
9464 .Times(1)
9465 .WillOnce(Return(true));
9466 EXPECT_CALL(visitor, OnUnauthenticatedHeader(_))
9467 .Times(1)
9468 .WillOnce(Return(true));
9469 EXPECT_CALL(visitor, OnPacketHeader(_)).Times(1);
9470 EXPECT_CALL(visitor, OnDecryptedPacket(_)).Times(1);
9471 EXPECT_CALL(visitor, OnError(_)).Times(1);
9472 EXPECT_CALL(visitor, OnStreamFrame(_)).Times(0);
9473 EXPECT_CALL(visitor, OnPacketComplete()).Times(0);
9474
9475 EXPECT_FALSE(framer_.ProcessPacket(*packet));
bncf6f82b12019-10-30 07:01:01 -07009476 EXPECT_THAT(framer_.error(), IsError(QUIC_INVALID_FRAME_DATA));
QUICHE teama6ef0a62019-03-07 20:34:33 -05009477}
9478
QUICHE teama6ef0a62019-03-07 20:34:33 -05009479TEST_P(QuicFramerTest, IetfBlockedFrame) {
fkastenholzc5e0c8c2019-09-30 10:16:30 -07009480 // This frame is only for IETF QUIC.
fkastenholz305e1732019-06-18 05:01:22 -07009481 if (!VersionHasIetfQuicFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05009482 return;
9483 }
zhongyi546cc452019-04-12 15:27:49 -07009484 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
QUICHE teama6ef0a62019-03-07 20:34:33 -05009485
9486 // clang-format off
9487 PacketFragments packet99 = {
9488 // type (short header, 4 byte packet number)
9489 {"",
9490 {0x43}},
9491 // connection_id
9492 {"",
9493 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
9494 // packet number
9495 {"",
9496 {0x12, 0x34, 0x9A, 0xBC}},
ianswett2f077442019-12-12 11:51:24 -08009497 // frame type (IETF_DATA_BLOCKED)
QUICHE teama6ef0a62019-03-07 20:34:33 -05009498 {"",
9499 {0x14}},
9500 // blocked offset
9501 {"Can not read blocked offset.",
9502 {kVarInt62EightBytes + 0x3a, 0x98, 0xFE, 0xDC, 0x32, 0x10, 0x76, 0x54}},
9503 };
9504 // clang-format on
9505
9506 std::unique_ptr<QuicEncryptedPacket> encrypted(
9507 AssemblePacketFromFragments(packet99));
9508 EXPECT_TRUE(framer_.ProcessPacket(*encrypted));
9509
bncf6f82b12019-10-30 07:01:01 -07009510 EXPECT_THAT(framer_.error(), IsQuicNoError());
QUICHE teama6ef0a62019-03-07 20:34:33 -05009511 ASSERT_TRUE(visitor_.header_.get());
9512 EXPECT_TRUE(CheckDecryption(
9513 *encrypted, !kIncludeVersion, !kIncludeDiversificationNonce,
9514 PACKET_8BYTE_CONNECTION_ID, PACKET_0BYTE_CONNECTION_ID));
9515
9516 EXPECT_EQ(kStreamOffset, visitor_.blocked_frame_.offset);
9517
9518 CheckFramingBoundaries(packet99, QUIC_INVALID_BLOCKED_DATA);
9519}
9520
9521TEST_P(QuicFramerTest, BuildIetfBlockedPacket) {
fkastenholzc5e0c8c2019-09-30 10:16:30 -07009522 // This frame is only for IETF QUIC.
fkastenholz305e1732019-06-18 05:01:22 -07009523 if (!VersionHasIetfQuicFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05009524 return;
9525 }
9526
9527 QuicPacketHeader header;
9528 header.destination_connection_id = FramerTestConnectionId();
9529 header.reset_flag = false;
9530 header.version_flag = false;
9531 header.packet_number = kPacketNumber;
9532
9533 QuicBlockedFrame frame;
9534 frame.stream_id = QuicUtils::GetInvalidStreamId(framer_.transport_version());
9535 frame.offset = kStreamOffset;
9536 QuicFrames frames = {QuicFrame(&frame)};
9537
9538 // clang-format off
9539 unsigned char packet99[] = {
9540 // type (short header, 4 byte packet number)
9541 0x43,
9542 // connection_id
9543 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
9544 // packet number
9545 0x12, 0x34, 0x56, 0x78,
9546
ianswett2f077442019-12-12 11:51:24 -08009547 // frame type (IETF_DATA_BLOCKED)
QUICHE teama6ef0a62019-03-07 20:34:33 -05009548 0x14,
9549 // Offset
9550 kVarInt62EightBytes + 0x3a, 0x98, 0xFE, 0xDC, 0x32, 0x10, 0x76, 0x54
9551 };
9552 // clang-format on
9553
9554 std::unique_ptr<QuicPacket> data(BuildDataPacket(header, frames));
9555 ASSERT_TRUE(data != nullptr);
9556
dmcardle8f7df532020-01-07 13:28:57 -08009557 quiche::test::CompareCharArraysWithHexError(
9558 "constructed packet", data->data(), data->length(), AsChars(packet99),
9559 QUICHE_ARRAYSIZE(packet99));
QUICHE teama6ef0a62019-03-07 20:34:33 -05009560}
9561
9562TEST_P(QuicFramerTest, IetfStreamBlockedFrame) {
fkastenholzc5e0c8c2019-09-30 10:16:30 -07009563 // This frame is only for IETF QUIC.
fkastenholz305e1732019-06-18 05:01:22 -07009564 if (!VersionHasIetfQuicFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05009565 return;
9566 }
zhongyi546cc452019-04-12 15:27:49 -07009567 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
QUICHE teama6ef0a62019-03-07 20:34:33 -05009568
9569 // clang-format off
9570 PacketFragments packet99 = {
9571 // type (short header, 4 byte packet number)
9572 {"",
9573 {0x43}},
9574 // connection_id
9575 {"",
9576 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
9577 // packet number
9578 {"",
9579 {0x12, 0x34, 0x9A, 0xBC}},
ianswett2f077442019-12-12 11:51:24 -08009580 // frame type (IETF_STREAM_DATA_BLOCKED)
QUICHE teama6ef0a62019-03-07 20:34:33 -05009581 {"",
9582 {0x15}},
9583 // blocked offset
9584 {"Can not read stream blocked stream id.",
9585 {kVarInt62FourBytes + 0x01, 0x02, 0x03, 0x04}},
9586 {"Can not read stream blocked offset.",
9587 {kVarInt62EightBytes + 0x3a, 0x98, 0xFE, 0xDC, 0x32, 0x10, 0x76, 0x54}},
9588 };
9589 // clang-format on
9590
9591 std::unique_ptr<QuicEncryptedPacket> encrypted(
9592 AssemblePacketFromFragments(packet99));
9593 EXPECT_TRUE(framer_.ProcessPacket(*encrypted));
9594
bncf6f82b12019-10-30 07:01:01 -07009595 EXPECT_THAT(framer_.error(), IsQuicNoError());
QUICHE teama6ef0a62019-03-07 20:34:33 -05009596 ASSERT_TRUE(visitor_.header_.get());
9597 EXPECT_TRUE(CheckDecryption(
9598 *encrypted, !kIncludeVersion, !kIncludeDiversificationNonce,
9599 PACKET_8BYTE_CONNECTION_ID, PACKET_0BYTE_CONNECTION_ID));
9600
9601 EXPECT_EQ(kStreamId, visitor_.blocked_frame_.stream_id);
9602 EXPECT_EQ(kStreamOffset, visitor_.blocked_frame_.offset);
9603
9604 CheckFramingBoundaries(packet99, QUIC_INVALID_STREAM_BLOCKED_DATA);
9605}
9606
9607TEST_P(QuicFramerTest, BuildIetfStreamBlockedPacket) {
fkastenholzc5e0c8c2019-09-30 10:16:30 -07009608 // This frame is only for IETF QUIC.
fkastenholz305e1732019-06-18 05:01:22 -07009609 if (!VersionHasIetfQuicFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05009610 return;
9611 }
9612
9613 QuicPacketHeader header;
9614 header.destination_connection_id = FramerTestConnectionId();
9615 header.reset_flag = false;
9616 header.version_flag = false;
9617 header.packet_number = kPacketNumber;
9618
9619 QuicBlockedFrame frame;
9620 frame.stream_id = kStreamId;
9621 frame.offset = kStreamOffset;
9622 QuicFrames frames = {QuicFrame(&frame)};
9623
9624 // clang-format off
9625 unsigned char packet99[] = {
9626 // type (short header, 4 byte packet number)
9627 0x43,
9628 // connection_id
9629 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
9630 // packet number
9631 0x12, 0x34, 0x56, 0x78,
9632
ianswett2f077442019-12-12 11:51:24 -08009633 // frame type (IETF_STREAM_DATA_BLOCKED)
QUICHE teama6ef0a62019-03-07 20:34:33 -05009634 0x15,
9635 // Stream ID
9636 kVarInt62FourBytes + 0x01, 0x02, 0x03, 0x04,
9637 // Offset
9638 kVarInt62EightBytes + 0x3a, 0x98, 0xFE, 0xDC, 0x32, 0x10, 0x76, 0x54
9639 };
9640 // clang-format on
9641
9642 std::unique_ptr<QuicPacket> data(BuildDataPacket(header, frames));
9643 ASSERT_TRUE(data != nullptr);
9644
dmcardle8f7df532020-01-07 13:28:57 -08009645 quiche::test::CompareCharArraysWithHexError(
9646 "constructed packet", data->data(), data->length(), AsChars(packet99),
9647 QUICHE_ARRAYSIZE(packet99));
QUICHE teama6ef0a62019-03-07 20:34:33 -05009648}
9649
fkastenholz3c4eabf2019-04-22 07:49:59 -07009650TEST_P(QuicFramerTest, BiDiMaxStreamsFrame) {
fkastenholzc5e0c8c2019-09-30 10:16:30 -07009651 // This frame is only for IETF QUIC.
fkastenholz305e1732019-06-18 05:01:22 -07009652 if (!VersionHasIetfQuicFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05009653 return;
9654 }
zhongyi546cc452019-04-12 15:27:49 -07009655 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
QUICHE teama6ef0a62019-03-07 20:34:33 -05009656
9657 // clang-format off
9658 PacketFragments packet99 = {
9659 // type (short header, 4 byte packet number)
9660 {"",
9661 {0x43}},
9662 // connection_id
9663 {"",
9664 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
9665 // packet number
9666 {"",
9667 {0x12, 0x34, 0x9A, 0xBC}},
9668 // frame type (IETF_MAX_STREAMS_BIDIRECTIONAL)
9669 {"",
9670 {0x12}},
9671 // max. streams
9672 {"Can not read MAX_STREAMS stream count.",
9673 {kVarInt62OneByte + 0x03}},
9674 };
9675 // clang-format on
9676
9677 std::unique_ptr<QuicEncryptedPacket> encrypted(
9678 AssemblePacketFromFragments(packet99));
9679 EXPECT_TRUE(framer_.ProcessPacket(*encrypted));
9680
bncf6f82b12019-10-30 07:01:01 -07009681 EXPECT_THAT(framer_.error(), IsQuicNoError());
QUICHE teama6ef0a62019-03-07 20:34:33 -05009682 ASSERT_TRUE(visitor_.header_.get());
9683 EXPECT_TRUE(CheckDecryption(
9684 *encrypted, !kIncludeVersion, !kIncludeDiversificationNonce,
9685 PACKET_8BYTE_CONNECTION_ID, PACKET_0BYTE_CONNECTION_ID));
9686
fkastenholz3c4eabf2019-04-22 07:49:59 -07009687 EXPECT_EQ(3u, visitor_.max_streams_frame_.stream_count);
9688 EXPECT_FALSE(visitor_.max_streams_frame_.unidirectional);
9689 CheckFramingBoundaries(packet99, QUIC_MAX_STREAMS_DATA);
QUICHE teama6ef0a62019-03-07 20:34:33 -05009690}
9691
fkastenholz3c4eabf2019-04-22 07:49:59 -07009692TEST_P(QuicFramerTest, UniDiMaxStreamsFrame) {
fkastenholzc5e0c8c2019-09-30 10:16:30 -07009693 // This frame is only for IETF QUIC.
fkastenholz305e1732019-06-18 05:01:22 -07009694 if (!VersionHasIetfQuicFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05009695 return;
9696 }
zhongyi546cc452019-04-12 15:27:49 -07009697 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
QUICHE teama6ef0a62019-03-07 20:34:33 -05009698
9699 // clang-format off
9700 PacketFragments packet99 = {
9701 // type (short header, 4 byte packet number)
9702 {"",
9703 {0x43}},
9704 // Test runs in client mode, no connection id
9705 // packet number
9706 {"",
9707 {0x12, 0x34, 0x9A, 0xBC}},
fkastenholz3c4eabf2019-04-22 07:49:59 -07009708 // frame type (IETF_MAX_STREAMS_UNIDIRECTIONAL)
QUICHE teama6ef0a62019-03-07 20:34:33 -05009709 {"",
fkastenholz3c4eabf2019-04-22 07:49:59 -07009710 {0x13}},
QUICHE teama6ef0a62019-03-07 20:34:33 -05009711 // max. streams
9712 {"Can not read MAX_STREAMS stream count.",
9713 {kVarInt62OneByte + 0x03}},
9714 };
9715 // clang-format on
9716
9717 std::unique_ptr<QuicEncryptedPacket> encrypted(
9718 AssemblePacketFromFragments(packet99));
9719 QuicFramerPeer::SetPerspective(&framer_, Perspective::IS_CLIENT);
9720 EXPECT_TRUE(framer_.ProcessPacket(*encrypted));
9721
bncf6f82b12019-10-30 07:01:01 -07009722 EXPECT_THAT(framer_.error(), IsQuicNoError());
QUICHE teama6ef0a62019-03-07 20:34:33 -05009723 ASSERT_TRUE(visitor_.header_.get());
9724 EXPECT_TRUE(CheckDecryption(
9725 *encrypted, !kIncludeVersion, !kIncludeDiversificationNonce,
9726 PACKET_0BYTE_CONNECTION_ID, PACKET_0BYTE_CONNECTION_ID));
9727
fkastenholz3c4eabf2019-04-22 07:49:59 -07009728 EXPECT_EQ(3u, visitor_.max_streams_frame_.stream_count);
9729 EXPECT_TRUE(visitor_.max_streams_frame_.unidirectional);
9730 CheckFramingBoundaries(packet99, QUIC_MAX_STREAMS_DATA);
QUICHE teama6ef0a62019-03-07 20:34:33 -05009731}
9732
9733TEST_P(QuicFramerTest, ServerUniDiMaxStreamsFrame) {
fkastenholzc5e0c8c2019-09-30 10:16:30 -07009734 // This frame is only for IETF QUIC.
fkastenholz305e1732019-06-18 05:01:22 -07009735 if (!VersionHasIetfQuicFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05009736 return;
9737 }
zhongyi546cc452019-04-12 15:27:49 -07009738 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
QUICHE teama6ef0a62019-03-07 20:34:33 -05009739
9740 // clang-format off
9741 PacketFragments packet99 = {
9742 // type (short header, 4 byte packet number)
9743 {"",
9744 {0x43}},
9745 // connection_id
9746 {"",
9747 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
9748 // packet number
9749 {"",
9750 {0x12, 0x34, 0x9A, 0xBC}},
9751 // frame type (IETF_MAX_STREAMS_UNIDIRECTIONAL)
9752 {"",
9753 {0x13}},
9754 // max. streams
9755 {"Can not read MAX_STREAMS stream count.",
9756 {kVarInt62OneByte + 0x03}},
9757 };
9758 // clang-format on
9759
9760 std::unique_ptr<QuicEncryptedPacket> encrypted(
9761 AssemblePacketFromFragments(packet99));
9762 EXPECT_TRUE(framer_.ProcessPacket(*encrypted));
9763
bncf6f82b12019-10-30 07:01:01 -07009764 EXPECT_THAT(framer_.error(), IsQuicNoError());
QUICHE teama6ef0a62019-03-07 20:34:33 -05009765 ASSERT_TRUE(visitor_.header_.get());
9766 EXPECT_TRUE(CheckDecryption(
9767 *encrypted, !kIncludeVersion, !kIncludeDiversificationNonce,
9768 PACKET_8BYTE_CONNECTION_ID, PACKET_0BYTE_CONNECTION_ID));
9769
fkastenholz3c4eabf2019-04-22 07:49:59 -07009770 EXPECT_EQ(3u, visitor_.max_streams_frame_.stream_count);
9771 EXPECT_TRUE(visitor_.max_streams_frame_.unidirectional);
9772 CheckFramingBoundaries(packet99, QUIC_MAX_STREAMS_DATA);
QUICHE teama6ef0a62019-03-07 20:34:33 -05009773}
9774
9775TEST_P(QuicFramerTest, ClientUniDiMaxStreamsFrame) {
fkastenholzc5e0c8c2019-09-30 10:16:30 -07009776 // This frame is only for IETF QUIC.
fkastenholz305e1732019-06-18 05:01:22 -07009777 if (!VersionHasIetfQuicFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05009778 return;
9779 }
zhongyi546cc452019-04-12 15:27:49 -07009780 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
QUICHE teama6ef0a62019-03-07 20:34:33 -05009781
9782 // clang-format off
9783 PacketFragments packet99 = {
9784 // type (short header, 4 byte packet number)
9785 {"",
9786 {0x43}},
9787 // Test runs in client mode, no connection id
9788 // packet number
9789 {"",
9790 {0x12, 0x34, 0x9A, 0xBC}},
9791 // frame type (IETF_MAX_STREAMS_UNIDIRECTIONAL)
9792 {"",
9793 {0x13}},
9794 // max. streams
9795 {"Can not read MAX_STREAMS stream count.",
9796 {kVarInt62OneByte + 0x03}},
9797 };
9798 // clang-format on
9799
9800 std::unique_ptr<QuicEncryptedPacket> encrypted(
9801 AssemblePacketFromFragments(packet99));
9802 QuicFramerPeer::SetPerspective(&framer_, Perspective::IS_CLIENT);
9803 EXPECT_TRUE(framer_.ProcessPacket(*encrypted));
9804
bncf6f82b12019-10-30 07:01:01 -07009805 EXPECT_THAT(framer_.error(), IsQuicNoError());
QUICHE teama6ef0a62019-03-07 20:34:33 -05009806 ASSERT_TRUE(visitor_.header_.get());
9807 EXPECT_TRUE(CheckDecryption(
9808 *encrypted, !kIncludeVersion, !kIncludeDiversificationNonce,
9809 PACKET_0BYTE_CONNECTION_ID, PACKET_0BYTE_CONNECTION_ID));
9810
fkastenholz3c4eabf2019-04-22 07:49:59 -07009811 EXPECT_EQ(3u, visitor_.max_streams_frame_.stream_count);
9812 EXPECT_TRUE(visitor_.max_streams_frame_.unidirectional);
9813 CheckFramingBoundaries(packet99, QUIC_MAX_STREAMS_DATA);
QUICHE teama6ef0a62019-03-07 20:34:33 -05009814}
9815
9816// The following four tests ensure that the framer can deserialize a stream
9817// count that is large enough to cause the resulting stream ID to exceed the
9818// current implementation limit(32 bits). The intent is that when this happens,
9819// the stream limit is pegged to the maximum supported value. There are four
9820// tests, for the four combinations of uni- and bi-directional, server- and
9821// client- initiated.
fkastenholz3c4eabf2019-04-22 07:49:59 -07009822TEST_P(QuicFramerTest, BiDiMaxStreamsFrameTooBig) {
fkastenholzc5e0c8c2019-09-30 10:16:30 -07009823 // This frame is only for IETF QUIC.
fkastenholz305e1732019-06-18 05:01:22 -07009824 if (!VersionHasIetfQuicFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05009825 return;
9826 }
zhongyi546cc452019-04-12 15:27:49 -07009827 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
QUICHE teama6ef0a62019-03-07 20:34:33 -05009828
9829 // clang-format off
9830 unsigned char packet99[] = {
9831 // type (short header, 4 byte packet number)
9832 0x43,
9833 // connection_id
9834 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
9835 // packet number
9836 0x12, 0x34, 0x9A, 0xBC,
9837 // frame type (IETF_MAX_STREAMS_BIDIRECTIONAL)
9838 0x12,
9839
9840 // max. streams. Max stream ID allowed is 0xffffffff
9841 // This encodes a count of 0x40000000, leading to stream
9842 // IDs in the range 0x1 00000000 to 0x1 00000003.
9843 kVarInt62EightBytes + 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00
9844 };
9845 // clang-format on
9846
bnc4e9283d2019-12-17 07:08:57 -08009847 QuicEncryptedPacket encrypted(AsChars(packet99), QUICHE_ARRAYSIZE(packet99),
QUICHE teama6ef0a62019-03-07 20:34:33 -05009848 false);
9849 EXPECT_TRUE(framer_.ProcessPacket(encrypted));
bncf6f82b12019-10-30 07:01:01 -07009850 EXPECT_THAT(framer_.error(), IsQuicNoError());
QUICHE teama6ef0a62019-03-07 20:34:33 -05009851 ASSERT_TRUE(visitor_.header_.get());
9852 EXPECT_TRUE(CheckDecryption(
9853 encrypted, !kIncludeVersion, !kIncludeDiversificationNonce,
9854 PACKET_8BYTE_CONNECTION_ID, PACKET_0BYTE_CONNECTION_ID));
9855
fkastenholz465220f2019-04-23 07:56:27 -07009856 EXPECT_EQ(0x40000000u, visitor_.max_streams_frame_.stream_count);
fkastenholz3c4eabf2019-04-22 07:49:59 -07009857 EXPECT_FALSE(visitor_.max_streams_frame_.unidirectional);
QUICHE teama6ef0a62019-03-07 20:34:33 -05009858}
9859
9860TEST_P(QuicFramerTest, ClientBiDiMaxStreamsFrameTooBig) {
fkastenholzc5e0c8c2019-09-30 10:16:30 -07009861 // This frame is only for IETF QUIC.
fkastenholz305e1732019-06-18 05:01:22 -07009862 if (!VersionHasIetfQuicFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05009863 return;
9864 }
zhongyi546cc452019-04-12 15:27:49 -07009865 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
QUICHE teama6ef0a62019-03-07 20:34:33 -05009866
9867 // clang-format off
9868 unsigned char packet99[] = {
9869 // type (short header, 4 byte packet number)
9870 0x43,
9871 // Test runs in client mode, no connection id
9872 // packet number
9873 0x12, 0x34, 0x9A, 0xBC,
9874 // frame type (IETF_MAX_STREAMS_BIDIRECTIONAL)
9875 0x12,
9876
9877 // max. streams. Max stream ID allowed is 0xffffffff
9878 // This encodes a count of 0x40000000, leading to stream
9879 // IDs in the range 0x1 00000000 to 0x1 00000003.
9880 kVarInt62EightBytes + 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00
9881 };
9882 // clang-format on
9883
bnc4e9283d2019-12-17 07:08:57 -08009884 QuicEncryptedPacket encrypted(AsChars(packet99), QUICHE_ARRAYSIZE(packet99),
QUICHE teama6ef0a62019-03-07 20:34:33 -05009885 false);
9886 QuicFramerPeer::SetPerspective(&framer_, Perspective::IS_CLIENT);
9887 EXPECT_TRUE(framer_.ProcessPacket(encrypted));
9888
bncf6f82b12019-10-30 07:01:01 -07009889 EXPECT_THAT(framer_.error(), IsQuicNoError());
QUICHE teama6ef0a62019-03-07 20:34:33 -05009890 ASSERT_TRUE(visitor_.header_.get());
9891 EXPECT_TRUE(CheckDecryption(
9892 encrypted, !kIncludeVersion, !kIncludeDiversificationNonce,
9893 PACKET_0BYTE_CONNECTION_ID, PACKET_0BYTE_CONNECTION_ID));
9894
fkastenholz465220f2019-04-23 07:56:27 -07009895 EXPECT_EQ(0x40000000u, visitor_.max_streams_frame_.stream_count);
fkastenholz3c4eabf2019-04-22 07:49:59 -07009896 EXPECT_FALSE(visitor_.max_streams_frame_.unidirectional);
QUICHE teama6ef0a62019-03-07 20:34:33 -05009897}
9898
9899TEST_P(QuicFramerTest, ServerUniDiMaxStreamsFrameTooBig) {
fkastenholzc5e0c8c2019-09-30 10:16:30 -07009900 // This frame is only for IETF QUIC.
fkastenholz305e1732019-06-18 05:01:22 -07009901 if (!VersionHasIetfQuicFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05009902 return;
9903 }
zhongyi546cc452019-04-12 15:27:49 -07009904 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
QUICHE teama6ef0a62019-03-07 20:34:33 -05009905
9906 // clang-format off
9907 unsigned char packet99[] = {
9908 // type (short header, 4 byte packet number)
9909 0x43,
9910 // connection_id
9911 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
9912 // packet number
9913 0x12, 0x34, 0x9A, 0xBC,
9914 // frame type (IETF_MAX_STREAMS_UNIDIRECTIONAL)
9915 0x13,
9916
9917 // max. streams. Max stream ID allowed is 0xffffffff
9918 // This encodes a count of 0x40000000, leading to stream
9919 // IDs in the range 0x1 00000000 to 0x1 00000003.
9920 kVarInt62EightBytes + 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00
9921 };
9922 // clang-format on
9923
bnc4e9283d2019-12-17 07:08:57 -08009924 QuicEncryptedPacket encrypted(AsChars(packet99), QUICHE_ARRAYSIZE(packet99),
QUICHE teama6ef0a62019-03-07 20:34:33 -05009925 false);
9926 EXPECT_TRUE(framer_.ProcessPacket(encrypted));
9927
bncf6f82b12019-10-30 07:01:01 -07009928 EXPECT_THAT(framer_.error(), IsQuicNoError());
QUICHE teama6ef0a62019-03-07 20:34:33 -05009929 ASSERT_TRUE(visitor_.header_.get());
9930 EXPECT_TRUE(CheckDecryption(
9931 encrypted, !kIncludeVersion, !kIncludeDiversificationNonce,
9932 PACKET_8BYTE_CONNECTION_ID, PACKET_0BYTE_CONNECTION_ID));
9933
fkastenholz465220f2019-04-23 07:56:27 -07009934 EXPECT_EQ(0x40000000u, visitor_.max_streams_frame_.stream_count);
fkastenholz3c4eabf2019-04-22 07:49:59 -07009935 EXPECT_TRUE(visitor_.max_streams_frame_.unidirectional);
QUICHE teama6ef0a62019-03-07 20:34:33 -05009936}
9937
9938TEST_P(QuicFramerTest, ClientUniDiMaxStreamsFrameTooBig) {
fkastenholzc5e0c8c2019-09-30 10:16:30 -07009939 // This frame is only for IETF QUIC.
fkastenholz305e1732019-06-18 05:01:22 -07009940 if (!VersionHasIetfQuicFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05009941 return;
9942 }
zhongyi546cc452019-04-12 15:27:49 -07009943 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
QUICHE teama6ef0a62019-03-07 20:34:33 -05009944
9945 // clang-format off
9946 unsigned char packet99[] = {
9947 // type (short header, 4 byte packet number)
9948 0x43,
9949 // Test runs in client mode, no connection id
9950 // packet number
9951 0x12, 0x34, 0x9A, 0xBC,
9952 // frame type (IETF_MAX_STREAMS_UNDIRECTIONAL)
9953 0x13,
9954
9955 // max. streams. Max stream ID allowed is 0xffffffff
9956 // This encodes a count of 0x40000000, leading to stream
9957 // IDs in the range 0x1 00000000 to 0x1 00000003.
9958 kVarInt62EightBytes + 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00
9959 };
9960 // clang-format on
9961
bnc4e9283d2019-12-17 07:08:57 -08009962 QuicEncryptedPacket encrypted(AsChars(packet99), QUICHE_ARRAYSIZE(packet99),
QUICHE teama6ef0a62019-03-07 20:34:33 -05009963 false);
9964 QuicFramerPeer::SetPerspective(&framer_, Perspective::IS_CLIENT);
9965 EXPECT_TRUE(framer_.ProcessPacket(encrypted));
9966
bncf6f82b12019-10-30 07:01:01 -07009967 EXPECT_THAT(framer_.error(), IsQuicNoError());
QUICHE teama6ef0a62019-03-07 20:34:33 -05009968 ASSERT_TRUE(visitor_.header_.get());
9969 EXPECT_TRUE(CheckDecryption(
9970 encrypted, !kIncludeVersion, !kIncludeDiversificationNonce,
9971 PACKET_0BYTE_CONNECTION_ID, PACKET_0BYTE_CONNECTION_ID));
9972
fkastenholz465220f2019-04-23 07:56:27 -07009973 EXPECT_EQ(0x40000000u, visitor_.max_streams_frame_.stream_count);
fkastenholz3c4eabf2019-04-22 07:49:59 -07009974 EXPECT_TRUE(visitor_.max_streams_frame_.unidirectional);
QUICHE teama6ef0a62019-03-07 20:34:33 -05009975}
9976
fkastenholz3c4eabf2019-04-22 07:49:59 -07009977// Specifically test that count==0 is accepted.
QUICHE teama6ef0a62019-03-07 20:34:33 -05009978TEST_P(QuicFramerTest, MaxStreamsFrameZeroCount) {
fkastenholzc5e0c8c2019-09-30 10:16:30 -07009979 // This frame is only for IETF QUIC.
fkastenholz305e1732019-06-18 05:01:22 -07009980 if (!VersionHasIetfQuicFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -05009981 return;
9982 }
zhongyi546cc452019-04-12 15:27:49 -07009983 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
QUICHE teama6ef0a62019-03-07 20:34:33 -05009984
9985 // clang-format off
9986 unsigned char packet99[] = {
9987 // type (short header, 4 byte packet number)
9988 0x43,
9989 // connection_id
9990 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
9991 // packet number
9992 0x12, 0x34, 0x9A, 0xBC,
9993 // frame type (IETF_MAX_STREAMS_BIDIRECTIONAL)
9994 0x12,
9995 // max. streams == 0.
9996 kVarInt62OneByte + 0x00
9997 };
9998 // clang-format on
9999
bnc4e9283d2019-12-17 07:08:57 -080010000 QuicEncryptedPacket encrypted(AsChars(packet99), QUICHE_ARRAYSIZE(packet99),
QUICHE teama6ef0a62019-03-07 20:34:33 -050010001 false);
fkastenholz3c4eabf2019-04-22 07:49:59 -070010002 EXPECT_TRUE(framer_.ProcessPacket(encrypted));
QUICHE teama6ef0a62019-03-07 20:34:33 -050010003}
10004
10005TEST_P(QuicFramerTest, ServerBiDiStreamsBlockedFrame) {
fkastenholzc5e0c8c2019-09-30 10:16:30 -070010006 // This frame is only for IETF QUIC.
fkastenholz305e1732019-06-18 05:01:22 -070010007 if (!VersionHasIetfQuicFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -050010008 return;
10009 }
zhongyi546cc452019-04-12 15:27:49 -070010010 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
QUICHE teama6ef0a62019-03-07 20:34:33 -050010011
10012 // clang-format off
10013 PacketFragments packet99 = {
10014 // type (short header, 4 byte packet number)
10015 {"",
10016 {0x43}},
10017 // connection_id
10018 {"",
10019 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
10020 // packet number
10021 {"",
10022 {0x12, 0x34, 0x9A, 0xBC}},
fkastenholz3c4eabf2019-04-22 07:49:59 -070010023 // frame type (IETF_MAX_STREAMS_UNIDIRECTIONAL frame)
10024 {"",
10025 {0x13}},
10026 // stream count
10027 {"Can not read MAX_STREAMS stream count.",
10028 {kVarInt62OneByte + 0x00}},
10029 };
10030 // clang-format on
10031
10032 std::unique_ptr<QuicEncryptedPacket> encrypted(
10033 AssemblePacketFromFragments(packet99));
10034 EXPECT_TRUE(framer_.ProcessPacket(*encrypted));
10035
bncf6f82b12019-10-30 07:01:01 -070010036 EXPECT_THAT(framer_.error(), IsQuicNoError());
fkastenholz3c4eabf2019-04-22 07:49:59 -070010037 ASSERT_TRUE(visitor_.header_.get());
10038 EXPECT_TRUE(CheckDecryption(
10039 *encrypted, !kIncludeVersion, !kIncludeDiversificationNonce,
10040 PACKET_8BYTE_CONNECTION_ID, PACKET_0BYTE_CONNECTION_ID));
10041
10042 EXPECT_EQ(0u, visitor_.max_streams_frame_.stream_count);
10043 EXPECT_TRUE(visitor_.max_streams_frame_.unidirectional);
10044
10045 CheckFramingBoundaries(packet99, QUIC_MAX_STREAMS_DATA);
10046}
10047
10048TEST_P(QuicFramerTest, BiDiStreamsBlockedFrame) {
fkastenholzc5e0c8c2019-09-30 10:16:30 -070010049 // This frame is only for IETF QUIC.
fkastenholz305e1732019-06-18 05:01:22 -070010050 if (!VersionHasIetfQuicFrames(framer_.transport_version())) {
fkastenholz3c4eabf2019-04-22 07:49:59 -070010051 return;
10052 }
10053 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
10054
10055 // clang-format off
10056 PacketFragments packet99 = {
10057 // type (short header, 4 byte packet number)
10058 {"",
10059 {0x43}},
10060 // connection_id
10061 {"",
10062 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
10063 // packet number
10064 {"",
10065 {0x12, 0x34, 0x9A, 0xBC}},
QUICHE teama6ef0a62019-03-07 20:34:33 -050010066 // frame type (IETF_STREAMS_BLOCKED_BIDIRECTIONAL frame)
10067 {"",
10068 {0x16}},
10069 // stream id
fkastenholz3c4eabf2019-04-22 07:49:59 -070010070 {"Can not read STREAMS_BLOCKED stream count.",
QUICHE teama6ef0a62019-03-07 20:34:33 -050010071 {kVarInt62OneByte + 0x03}},
10072 };
10073 // clang-format on
10074
10075 std::unique_ptr<QuicEncryptedPacket> encrypted(
10076 AssemblePacketFromFragments(packet99));
10077 EXPECT_TRUE(framer_.ProcessPacket(*encrypted));
10078
bncf6f82b12019-10-30 07:01:01 -070010079 EXPECT_THAT(framer_.error(), IsQuicNoError());
QUICHE teama6ef0a62019-03-07 20:34:33 -050010080 ASSERT_TRUE(visitor_.header_.get());
10081 EXPECT_TRUE(CheckDecryption(
10082 *encrypted, !kIncludeVersion, !kIncludeDiversificationNonce,
10083 PACKET_8BYTE_CONNECTION_ID, PACKET_0BYTE_CONNECTION_ID));
10084
fkastenholz3c4eabf2019-04-22 07:49:59 -070010085 EXPECT_EQ(3u, visitor_.streams_blocked_frame_.stream_count);
10086 EXPECT_FALSE(visitor_.streams_blocked_frame_.unidirectional);
QUICHE teama6ef0a62019-03-07 20:34:33 -050010087
fkastenholz3c4eabf2019-04-22 07:49:59 -070010088 CheckFramingBoundaries(packet99, QUIC_STREAMS_BLOCKED_DATA);
QUICHE teama6ef0a62019-03-07 20:34:33 -050010089}
10090
fkastenholz3c4eabf2019-04-22 07:49:59 -070010091TEST_P(QuicFramerTest, UniDiStreamsBlockedFrame) {
fkastenholzc5e0c8c2019-09-30 10:16:30 -070010092 // This frame is only for IETF QUIC.
fkastenholz305e1732019-06-18 05:01:22 -070010093 if (!VersionHasIetfQuicFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -050010094 return;
10095 }
zhongyi546cc452019-04-12 15:27:49 -070010096 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
QUICHE teama6ef0a62019-03-07 20:34:33 -050010097
10098 // clang-format off
10099 PacketFragments packet99 = {
10100 // type (short header, 4 byte packet number)
10101 {"",
10102 {0x43}},
10103 // connection_id
10104 {"",
10105 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
10106 // packet number
10107 {"",
10108 {0x12, 0x34, 0x9A, 0xBC}},
10109 // frame type (IETF_STREAMS_BLOCKED_UNIDIRECTIONAL frame)
10110 {"",
10111 {0x17}},
10112 // stream id
fkastenholz3c4eabf2019-04-22 07:49:59 -070010113 {"Can not read STREAMS_BLOCKED stream count.",
QUICHE teama6ef0a62019-03-07 20:34:33 -050010114 {kVarInt62OneByte + 0x03}},
10115 };
10116 // clang-format on
10117
10118 std::unique_ptr<QuicEncryptedPacket> encrypted(
10119 AssemblePacketFromFragments(packet99));
10120 EXPECT_TRUE(framer_.ProcessPacket(*encrypted));
10121
bncf6f82b12019-10-30 07:01:01 -070010122 EXPECT_THAT(framer_.error(), IsQuicNoError());
QUICHE teama6ef0a62019-03-07 20:34:33 -050010123 ASSERT_TRUE(visitor_.header_.get());
10124 EXPECT_TRUE(CheckDecryption(
10125 *encrypted, !kIncludeVersion, !kIncludeDiversificationNonce,
10126 PACKET_8BYTE_CONNECTION_ID, PACKET_0BYTE_CONNECTION_ID));
10127
fkastenholz3c4eabf2019-04-22 07:49:59 -070010128 EXPECT_EQ(3u, visitor_.streams_blocked_frame_.stream_count);
10129 EXPECT_TRUE(visitor_.streams_blocked_frame_.unidirectional);
10130 CheckFramingBoundaries(packet99, QUIC_STREAMS_BLOCKED_DATA);
QUICHE teama6ef0a62019-03-07 20:34:33 -050010131}
10132
10133TEST_P(QuicFramerTest, ClientUniDiStreamsBlockedFrame) {
fkastenholzc5e0c8c2019-09-30 10:16:30 -070010134 // This frame is only for IETF QUIC.
fkastenholz305e1732019-06-18 05:01:22 -070010135 if (!VersionHasIetfQuicFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -050010136 return;
10137 }
zhongyi546cc452019-04-12 15:27:49 -070010138 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
QUICHE teama6ef0a62019-03-07 20:34:33 -050010139
10140 // clang-format off
10141 PacketFragments packet99 = {
10142 // type (short header, 4 byte packet number)
10143 {"",
10144 {0x43}},
10145 // Test runs in client mode, no connection id
10146 // packet number
10147 {"",
10148 {0x12, 0x34, 0x9A, 0xBC}},
10149 // frame type (IETF_STREAMS_BLOCKED_UNIDIRECTIONAL frame)
10150 {"",
10151 {0x17}},
10152 // stream id
fkastenholz3c4eabf2019-04-22 07:49:59 -070010153 {"Can not read STREAMS_BLOCKED stream count.",
QUICHE teama6ef0a62019-03-07 20:34:33 -050010154 {kVarInt62OneByte + 0x03}},
10155 };
10156 // clang-format on
10157
10158 std::unique_ptr<QuicEncryptedPacket> encrypted(
10159 AssemblePacketFromFragments(packet99));
10160 QuicFramerPeer::SetPerspective(&framer_, Perspective::IS_CLIENT);
10161 EXPECT_TRUE(framer_.ProcessPacket(*encrypted));
10162
bncf6f82b12019-10-30 07:01:01 -070010163 EXPECT_THAT(framer_.error(), IsQuicNoError());
QUICHE teama6ef0a62019-03-07 20:34:33 -050010164 ASSERT_TRUE(visitor_.header_.get());
10165 EXPECT_TRUE(CheckDecryption(
10166 *encrypted, !kIncludeVersion, !kIncludeDiversificationNonce,
10167 PACKET_0BYTE_CONNECTION_ID, PACKET_0BYTE_CONNECTION_ID));
10168
fkastenholz3c4eabf2019-04-22 07:49:59 -070010169 EXPECT_EQ(3u, visitor_.streams_blocked_frame_.stream_count);
10170 EXPECT_TRUE(visitor_.streams_blocked_frame_.unidirectional);
10171 CheckFramingBoundaries(packet99, QUIC_STREAMS_BLOCKED_DATA);
QUICHE teama6ef0a62019-03-07 20:34:33 -050010172}
10173
10174// Check that when we get a STREAMS_BLOCKED frame that specifies too large
10175// a stream count, we reject with an appropriate error. There is no need to
10176// check for different combinations of Uni/Bi directional and client/server
10177// initiated; the logic does not take these into account.
10178TEST_P(QuicFramerTest, StreamsBlockedFrameTooBig) {
fkastenholzc5e0c8c2019-09-30 10:16:30 -070010179 // This frame is only for IETF QUIC.
fkastenholz305e1732019-06-18 05:01:22 -070010180 if (!VersionHasIetfQuicFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -050010181 return;
10182 }
zhongyi546cc452019-04-12 15:27:49 -070010183 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
QUICHE teama6ef0a62019-03-07 20:34:33 -050010184
10185 // clang-format off
10186 unsigned char packet99[] = {
10187 // type (short header, 4 byte packet number)
10188 0x43,
10189 // Test runs in client mode, no connection id
10190 // packet number
10191 0x12, 0x34, 0x9A, 0xBC,
10192 // frame type (IETF_STREAMS_BLOCKED_BIDIRECTIONAL)
fkastenholz3c4eabf2019-04-22 07:49:59 -070010193 0x16,
QUICHE teama6ef0a62019-03-07 20:34:33 -050010194
10195 // max. streams. Max stream ID allowed is 0xffffffff
10196 // This encodes a count of 0x40000000, leading to stream
10197 // IDs in the range 0x1 00000000 to 0x1 00000003.
10198 kVarInt62EightBytes + 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x01
10199 };
10200 // clang-format on
10201
bnc4e9283d2019-12-17 07:08:57 -080010202 QuicEncryptedPacket encrypted(AsChars(packet99), QUICHE_ARRAYSIZE(packet99),
QUICHE teama6ef0a62019-03-07 20:34:33 -050010203 false);
10204 QuicFramerPeer::SetPerspective(&framer_, Perspective::IS_CLIENT);
10205 EXPECT_FALSE(framer_.ProcessPacket(encrypted));
10206
bncf6f82b12019-10-30 07:01:01 -070010207 EXPECT_THAT(framer_.error(), IsError(QUIC_STREAMS_BLOCKED_DATA));
QUICHE teama6ef0a62019-03-07 20:34:33 -050010208 EXPECT_EQ(framer_.detailed_error(),
10209 "STREAMS_BLOCKED stream count exceeds implementation limit.");
10210}
10211
fkastenholz3c4eabf2019-04-22 07:49:59 -070010212// Specifically test that count==0 is accepted.
QUICHE teama6ef0a62019-03-07 20:34:33 -050010213TEST_P(QuicFramerTest, StreamsBlockedFrameZeroCount) {
fkastenholzc5e0c8c2019-09-30 10:16:30 -070010214 // This frame is only for IETF QUIC.
fkastenholz305e1732019-06-18 05:01:22 -070010215 if (!VersionHasIetfQuicFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -050010216 return;
10217 }
fkastenholz3c4eabf2019-04-22 07:49:59 -070010218
zhongyi546cc452019-04-12 15:27:49 -070010219 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
QUICHE teama6ef0a62019-03-07 20:34:33 -050010220
10221 // clang-format off
fkastenholz3c4eabf2019-04-22 07:49:59 -070010222 PacketFragments packet99 = {
10223 // type (short header, 4 byte packet number)
10224 {"",
10225 {0x43}},
10226 // connection_id
10227 {"",
10228 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
10229 // packet number
10230 {"",
10231 {0x12, 0x34, 0x9A, 0xBC}},
10232 // frame type (IETF_STREAMS_BLOCKED_UNIDIRECTIONAL frame)
10233 {"",
10234 {0x17}},
10235 // stream id
10236 {"Can not read STREAMS_BLOCKED stream count.",
10237 {kVarInt62OneByte + 0x00}},
QUICHE teama6ef0a62019-03-07 20:34:33 -050010238 };
10239 // clang-format on
10240
fkastenholz3c4eabf2019-04-22 07:49:59 -070010241 std::unique_ptr<QuicEncryptedPacket> encrypted(
10242 AssemblePacketFromFragments(packet99));
10243 EXPECT_TRUE(framer_.ProcessPacket(*encrypted));
QUICHE teama6ef0a62019-03-07 20:34:33 -050010244
bncf6f82b12019-10-30 07:01:01 -070010245 EXPECT_THAT(framer_.error(), IsQuicNoError());
fkastenholz3c4eabf2019-04-22 07:49:59 -070010246 ASSERT_TRUE(visitor_.header_.get());
10247 EXPECT_TRUE(CheckDecryption(
10248 *encrypted, !kIncludeVersion, !kIncludeDiversificationNonce,
10249 PACKET_8BYTE_CONNECTION_ID, PACKET_0BYTE_CONNECTION_ID));
10250
10251 EXPECT_EQ(0u, visitor_.streams_blocked_frame_.stream_count);
10252 EXPECT_TRUE(visitor_.streams_blocked_frame_.unidirectional);
10253
10254 CheckFramingBoundaries(packet99, QUIC_STREAMS_BLOCKED_DATA);
QUICHE teama6ef0a62019-03-07 20:34:33 -050010255}
10256
fkastenholz3c4eabf2019-04-22 07:49:59 -070010257TEST_P(QuicFramerTest, BuildBiDiStreamsBlockedPacket) {
fkastenholzc5e0c8c2019-09-30 10:16:30 -070010258 // This frame is only for IETF QUIC.
fkastenholz305e1732019-06-18 05:01:22 -070010259 if (!VersionHasIetfQuicFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -050010260 return;
10261 }
10262
10263 QuicPacketHeader header;
10264 header.destination_connection_id = FramerTestConnectionId();
10265 header.reset_flag = false;
10266 header.version_flag = false;
10267 header.packet_number = kPacketNumber;
10268
fkastenholz3c4eabf2019-04-22 07:49:59 -070010269 QuicStreamsBlockedFrame frame;
10270 frame.stream_count = 3;
10271 frame.unidirectional = false;
QUICHE teama6ef0a62019-03-07 20:34:33 -050010272
10273 QuicFrames frames = {QuicFrame(frame)};
10274
10275 // clang-format off
10276 unsigned char packet99[] = {
10277 // type (short header, 4 byte packet number)
10278 0x43,
10279 // connection_id
10280 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
10281 // packet number
10282 0x12, 0x34, 0x56, 0x78,
10283
10284 // frame type (IETF_STREAMS_BLOCKED_BIDIRECTIONAL frame)
10285 0x16,
10286 // Stream count
10287 kVarInt62OneByte + 0x03
10288 };
10289 // clang-format on
10290
10291 std::unique_ptr<QuicPacket> data(BuildDataPacket(header, frames));
10292 ASSERT_TRUE(data != nullptr);
10293
dmcardle8f7df532020-01-07 13:28:57 -080010294 quiche::test::CompareCharArraysWithHexError(
10295 "constructed packet", data->data(), data->length(), AsChars(packet99),
10296 QUICHE_ARRAYSIZE(packet99));
QUICHE teama6ef0a62019-03-07 20:34:33 -050010297}
10298
fkastenholz3c4eabf2019-04-22 07:49:59 -070010299TEST_P(QuicFramerTest, BuildUniStreamsBlockedPacket) {
fkastenholzc5e0c8c2019-09-30 10:16:30 -070010300 // This frame is only for IETF QUIC.
fkastenholz305e1732019-06-18 05:01:22 -070010301 if (!VersionHasIetfQuicFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -050010302 return;
10303 }
10304
10305 QuicPacketHeader header;
10306 header.destination_connection_id = FramerTestConnectionId();
10307 header.reset_flag = false;
10308 header.version_flag = false;
10309 header.packet_number = kPacketNumber;
10310
fkastenholz3c4eabf2019-04-22 07:49:59 -070010311 QuicStreamsBlockedFrame frame;
10312 frame.stream_count = 3;
10313 frame.unidirectional = true;
10314
QUICHE teama6ef0a62019-03-07 20:34:33 -050010315 QuicFrames frames = {QuicFrame(frame)};
10316
10317 // clang-format off
10318 unsigned char packet99[] = {
10319 // type (short header, 4 byte packet number)
10320 0x43,
10321 // connection_id
10322 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
10323 // packet number
10324 0x12, 0x34, 0x56, 0x78,
10325
10326 // frame type (IETF_STREAMS_BLOCKED_UNIDIRECTIONAL frame)
10327 0x17,
10328 // Stream count
10329 kVarInt62OneByte + 0x03
10330 };
10331 // clang-format on
10332
10333 std::unique_ptr<QuicPacket> data(BuildDataPacket(header, frames));
10334 ASSERT_TRUE(data != nullptr);
10335
dmcardle8f7df532020-01-07 13:28:57 -080010336 quiche::test::CompareCharArraysWithHexError(
10337 "constructed packet", data->data(), data->length(), AsChars(packet99),
10338 QUICHE_ARRAYSIZE(packet99));
QUICHE teama6ef0a62019-03-07 20:34:33 -050010339}
10340
fkastenholz3c4eabf2019-04-22 07:49:59 -070010341TEST_P(QuicFramerTest, BuildBiDiMaxStreamsPacket) {
fkastenholzc5e0c8c2019-09-30 10:16:30 -070010342 // This frame is only for IETF QUIC.
fkastenholz305e1732019-06-18 05:01:22 -070010343 if (!VersionHasIetfQuicFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -050010344 return;
10345 }
10346
10347 QuicPacketHeader header;
10348 header.destination_connection_id = FramerTestConnectionId();
10349 header.reset_flag = false;
10350 header.version_flag = false;
10351 header.packet_number = kPacketNumber;
10352
fkastenholz3c4eabf2019-04-22 07:49:59 -070010353 QuicMaxStreamsFrame frame;
10354 frame.stream_count = 3;
10355 frame.unidirectional = false;
10356
QUICHE teama6ef0a62019-03-07 20:34:33 -050010357 QuicFrames frames = {QuicFrame(frame)};
10358
10359 // clang-format off
10360 unsigned char packet99[] = {
10361 // type (short header, 4 byte packet number)
10362 0x43,
10363 // connection_id
10364 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
10365 // packet number
10366 0x12, 0x34, 0x56, 0x78,
10367
10368 // frame type (IETF_MAX_STREAMS_BIDIRECTIONAL frame)
10369 0x12,
10370 // Stream count
10371 kVarInt62OneByte + 0x03
10372 };
10373 // clang-format on
10374
10375 std::unique_ptr<QuicPacket> data(BuildDataPacket(header, frames));
10376 ASSERT_TRUE(data != nullptr);
10377
dmcardle8f7df532020-01-07 13:28:57 -080010378 quiche::test::CompareCharArraysWithHexError(
10379 "constructed packet", data->data(), data->length(), AsChars(packet99),
10380 QUICHE_ARRAYSIZE(packet99));
QUICHE teama6ef0a62019-03-07 20:34:33 -050010381}
10382
fkastenholz3c4eabf2019-04-22 07:49:59 -070010383TEST_P(QuicFramerTest, BuildUniDiMaxStreamsPacket) {
fkastenholzc5e0c8c2019-09-30 10:16:30 -070010384 // This frame is only for IETF QUIC.
fkastenholz305e1732019-06-18 05:01:22 -070010385 if (!VersionHasIetfQuicFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -050010386 return;
10387 }
10388
10389 // This test runs in client mode.
10390 QuicFramerPeer::SetPerspective(&framer_, Perspective::IS_CLIENT);
10391
10392 QuicPacketHeader header;
10393 header.destination_connection_id = FramerTestConnectionId();
10394 header.reset_flag = false;
10395 header.version_flag = false;
10396 header.packet_number = kPacketNumber;
10397
fkastenholz3c4eabf2019-04-22 07:49:59 -070010398 QuicMaxStreamsFrame frame;
10399 frame.stream_count = 3;
10400 frame.unidirectional = true;
QUICHE teama6ef0a62019-03-07 20:34:33 -050010401
QUICHE teama6ef0a62019-03-07 20:34:33 -050010402 QuicFrames frames = {QuicFrame(frame)};
10403
10404 // clang-format off
10405 unsigned char packet99[] = {
10406 // type (short header, 4 byte packet number)
10407 0x43,
10408 // connection_id
10409 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
10410 // packet number
10411 0x12, 0x34, 0x56, 0x78,
10412
10413 // frame type (IETF_MAX_STREAMS_UNIDIRECTIONAL frame)
10414 0x13,
10415 // Stream count
10416 kVarInt62OneByte + 0x03
10417 };
10418 // clang-format on
10419
10420 std::unique_ptr<QuicPacket> data(BuildDataPacket(header, frames));
10421 ASSERT_TRUE(data != nullptr);
10422
dmcardle8f7df532020-01-07 13:28:57 -080010423 quiche::test::CompareCharArraysWithHexError(
10424 "constructed packet", data->data(), data->length(), AsChars(packet99),
10425 QUICHE_ARRAYSIZE(packet99));
QUICHE teama6ef0a62019-03-07 20:34:33 -050010426}
10427
10428TEST_P(QuicFramerTest, NewConnectionIdFrame) {
fkastenholz305e1732019-06-18 05:01:22 -070010429 if (!VersionHasIetfQuicFrames(framer_.transport_version())) {
fkastenholzc5e0c8c2019-09-30 10:16:30 -070010430 // This frame is only for IETF QUIC.
QUICHE teama6ef0a62019-03-07 20:34:33 -050010431 return;
10432 }
zhongyi546cc452019-04-12 15:27:49 -070010433 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
QUICHE teama6ef0a62019-03-07 20:34:33 -050010434 // clang-format off
10435 PacketFragments packet99 = {
10436 // type (short header, 4 byte packet number)
10437 {"",
10438 {0x43}},
10439 // connection_id
10440 {"",
10441 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
10442 // packet number
10443 {"",
10444 {0x12, 0x34, 0x56, 0x78}},
10445 // frame type (IETF_NEW_CONNECTION_ID frame)
10446 {"",
10447 {0x18}},
10448 // error code
10449 {"Unable to read new connection ID frame sequence number.",
10450 {kVarInt62OneByte + 0x11}},
fkastenholz1c19fc22019-07-12 11:06:19 -070010451 {"Unable to read new connection ID frame retire_prior_to.",
10452 {kVarInt62OneByte + 0x09}},
dschinazicf5b1e22019-07-17 18:35:17 -070010453 {"Unable to read new connection ID frame connection id.",
QUICHE teama6ef0a62019-03-07 20:34:33 -050010454 {0x08}}, // connection ID length
10455 {"Unable to read new connection ID frame connection id.",
10456 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x11}},
10457 {"Can not read new connection ID frame reset token.",
10458 {0xb5, 0x69, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x00,
10459 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}}
10460 };
10461 // clang-format on
10462
10463 std::unique_ptr<QuicEncryptedPacket> encrypted(
10464 AssemblePacketFromFragments(packet99));
10465 EXPECT_TRUE(framer_.ProcessPacket(*encrypted));
10466
bncf6f82b12019-10-30 07:01:01 -070010467 EXPECT_THAT(framer_.error(), IsQuicNoError());
QUICHE teama6ef0a62019-03-07 20:34:33 -050010468 ASSERT_TRUE(visitor_.header_.get());
10469 EXPECT_TRUE(CheckDecryption(
10470 *encrypted, !kIncludeVersion, !kIncludeDiversificationNonce,
10471 PACKET_8BYTE_CONNECTION_ID, PACKET_0BYTE_CONNECTION_ID));
10472
10473 EXPECT_EQ(0u, visitor_.stream_frames_.size());
10474
10475 EXPECT_EQ(FramerTestConnectionIdPlusOne(),
10476 visitor_.new_connection_id_.connection_id);
10477 EXPECT_EQ(0x11u, visitor_.new_connection_id_.sequence_number);
fkastenholz1c19fc22019-07-12 11:06:19 -070010478 EXPECT_EQ(0x09u, visitor_.new_connection_id_.retire_prior_to);
QUICHE teama6ef0a62019-03-07 20:34:33 -050010479 EXPECT_EQ(kTestStatelessResetToken,
10480 visitor_.new_connection_id_.stateless_reset_token);
10481
10482 ASSERT_EQ(0u, visitor_.ack_frames_.size());
10483
10484 CheckFramingBoundaries(packet99, QUIC_INVALID_NEW_CONNECTION_ID_DATA);
10485}
10486
QUICHE team8e2e4532019-03-14 14:37:56 -070010487TEST_P(QuicFramerTest, NewConnectionIdFrameVariableLength) {
fkastenholz305e1732019-06-18 05:01:22 -070010488 if (!VersionHasIetfQuicFrames(framer_.transport_version())) {
fkastenholzc5e0c8c2019-09-30 10:16:30 -070010489 // This frame is only for IETF QUIC.
QUICHE team8e2e4532019-03-14 14:37:56 -070010490 return;
10491 }
zhongyi546cc452019-04-12 15:27:49 -070010492 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
QUICHE team8e2e4532019-03-14 14:37:56 -070010493 // clang-format off
10494 PacketFragments packet99 = {
10495 // type (short header, 4 byte packet number)
10496 {"",
10497 {0x43}},
10498 // connection_id
10499 {"",
10500 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
10501 // packet number
10502 {"",
10503 {0x12, 0x34, 0x56, 0x78}},
10504 // frame type (IETF_NEW_CONNECTION_ID frame)
10505 {"",
10506 {0x18}},
10507 // error code
10508 {"Unable to read new connection ID frame sequence number.",
10509 {kVarInt62OneByte + 0x11}},
fkastenholz1c19fc22019-07-12 11:06:19 -070010510 {"Unable to read new connection ID frame retire_prior_to.",
10511 {kVarInt62OneByte + 0x0a}},
dschinazicf5b1e22019-07-17 18:35:17 -070010512 {"Unable to read new connection ID frame connection id.",
QUICHE team8e2e4532019-03-14 14:37:56 -070010513 {0x09}}, // connection ID length
10514 {"Unable to read new connection ID frame connection id.",
10515 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10, 0x42}},
10516 {"Can not read new connection ID frame reset token.",
10517 {0xb5, 0x69, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x00,
10518 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}}
10519 };
10520 // clang-format on
10521
10522 std::unique_ptr<QuicEncryptedPacket> encrypted(
10523 AssemblePacketFromFragments(packet99));
10524 EXPECT_TRUE(framer_.ProcessPacket(*encrypted));
10525
bncf6f82b12019-10-30 07:01:01 -070010526 EXPECT_THAT(framer_.error(), IsQuicNoError());
QUICHE team8e2e4532019-03-14 14:37:56 -070010527 ASSERT_TRUE(visitor_.header_.get());
10528 EXPECT_TRUE(CheckDecryption(
10529 *encrypted, !kIncludeVersion, !kIncludeDiversificationNonce,
10530 PACKET_8BYTE_CONNECTION_ID, PACKET_0BYTE_CONNECTION_ID));
10531
10532 EXPECT_EQ(0u, visitor_.stream_frames_.size());
10533
10534 EXPECT_EQ(FramerTestConnectionIdNineBytes(),
10535 visitor_.new_connection_id_.connection_id);
10536 EXPECT_EQ(0x11u, visitor_.new_connection_id_.sequence_number);
fkastenholz1c19fc22019-07-12 11:06:19 -070010537 EXPECT_EQ(0x0au, visitor_.new_connection_id_.retire_prior_to);
QUICHE team8e2e4532019-03-14 14:37:56 -070010538 EXPECT_EQ(kTestStatelessResetToken,
10539 visitor_.new_connection_id_.stateless_reset_token);
10540
10541 ASSERT_EQ(0u, visitor_.ack_frames_.size());
10542
10543 CheckFramingBoundaries(packet99, QUIC_INVALID_NEW_CONNECTION_ID_DATA);
10544}
10545
QUICHE team0131a5b2019-03-20 15:23:27 -070010546// Verifies that parsing a NEW_CONNECTION_ID frame with a length above the
10547// specified maximum fails.
10548TEST_P(QuicFramerTest, InvalidLongNewConnectionIdFrame) {
fkastenholz305e1732019-06-18 05:01:22 -070010549 if (!VersionHasIetfQuicFrames(framer_.transport_version())) {
fkastenholzc5e0c8c2019-09-30 10:16:30 -070010550 // The NEW_CONNECTION_ID frame is only for IETF QUIC.
QUICHE team0131a5b2019-03-20 15:23:27 -070010551 return;
10552 }
zhongyi546cc452019-04-12 15:27:49 -070010553 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
QUICHE team0131a5b2019-03-20 15:23:27 -070010554 // clang-format off
10555 PacketFragments packet99 = {
10556 // type (short header, 4 byte packet number)
10557 {"",
10558 {0x43}},
10559 // connection_id
10560 {"",
10561 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
10562 // packet number
10563 {"",
10564 {0x12, 0x34, 0x56, 0x78}},
10565 // frame type (IETF_NEW_CONNECTION_ID frame)
10566 {"",
10567 {0x18}},
10568 // error code
10569 {"Unable to read new connection ID frame sequence number.",
10570 {kVarInt62OneByte + 0x11}},
fkastenholz1c19fc22019-07-12 11:06:19 -070010571 {"Unable to read new connection ID frame retire_prior_to.",
10572 {kVarInt62OneByte + 0x0b}},
dschinazicf5b1e22019-07-17 18:35:17 -070010573 {"Unable to read new connection ID frame connection id.",
dschinazib953d022019-08-01 18:05:58 -070010574 {0x40}}, // connection ID length
QUICHE team0131a5b2019-03-20 15:23:27 -070010575 {"Unable to read new connection ID frame connection id.",
10576 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
10577 0xF0, 0xD2, 0xB4, 0x96, 0x78, 0x5A, 0x3C, 0x1E,
dschinazib953d022019-08-01 18:05:58 -070010578 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
10579 0xF0, 0xD2, 0xB4, 0x96, 0x78, 0x5A, 0x3C, 0x1E,
10580 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
10581 0xF0, 0xD2, 0xB4, 0x96, 0x78, 0x5A, 0x3C, 0x1E,
10582 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
10583 0xF0, 0xD2, 0xB4, 0x96, 0x78, 0x5A, 0x3C, 0x1E}},
QUICHE team0131a5b2019-03-20 15:23:27 -070010584 {"Can not read new connection ID frame reset token.",
10585 {0xb5, 0x69, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x00,
10586 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}}
10587 };
10588 // clang-format on
10589
10590 std::unique_ptr<QuicEncryptedPacket> encrypted(
10591 AssemblePacketFromFragments(packet99));
10592 EXPECT_FALSE(framer_.ProcessPacket(*encrypted));
bncf6f82b12019-10-30 07:01:01 -070010593 EXPECT_THAT(framer_.error(), IsError(QUIC_INVALID_NEW_CONNECTION_ID_DATA));
dschinazi161bb492020-02-05 09:59:38 -080010594 EXPECT_EQ("Invalid new connection ID length for version.",
10595 framer_.detailed_error());
QUICHE team0131a5b2019-03-20 15:23:27 -070010596}
10597
fkastenholz1c19fc22019-07-12 11:06:19 -070010598// Verifies that parsing a NEW_CONNECTION_ID frame with an invalid
10599// retire-prior-to fails.
10600TEST_P(QuicFramerTest, InvalidRetirePriorToNewConnectionIdFrame) {
10601 if (!VersionHasIetfQuicFrames(framer_.transport_version())) {
fkastenholzc5e0c8c2019-09-30 10:16:30 -070010602 // This frame is only for IETF QUIC only.
fkastenholz1c19fc22019-07-12 11:06:19 -070010603 return;
10604 }
10605 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
10606 // clang-format off
10607 PacketFragments packet99 = {
10608 // type (short header, 4 byte packet number)
10609 {"",
10610 {0x43}},
10611 // connection_id
10612 {"",
10613 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
10614 // packet number
10615 {"",
10616 {0x12, 0x34, 0x56, 0x78}},
10617 // frame type (IETF_NEW_CONNECTION_ID frame)
10618 {"",
10619 {0x18}},
10620 // sequence number
10621 {"Unable to read new connection ID frame sequence number.",
10622 {kVarInt62OneByte + 0x11}},
10623 {"Unable to read new connection ID frame retire_prior_to.",
10624 {kVarInt62OneByte + 0x1b}},
10625 {"Unable to read new connection ID frame connection id length.",
10626 {0x08}}, // connection ID length
10627 {"Unable to read new connection ID frame connection id.",
10628 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x11}},
10629 {"Can not read new connection ID frame reset token.",
10630 {0xb5, 0x69, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x00,
10631 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}}
10632 };
10633 // clang-format on
10634
10635 std::unique_ptr<QuicEncryptedPacket> encrypted(
10636 AssemblePacketFromFragments(packet99));
10637 EXPECT_FALSE(framer_.ProcessPacket(*encrypted));
bncf6f82b12019-10-30 07:01:01 -070010638 EXPECT_THAT(framer_.error(), IsError(QUIC_INVALID_NEW_CONNECTION_ID_DATA));
fkastenholz1c19fc22019-07-12 11:06:19 -070010639 EXPECT_EQ("Retire_prior_to > sequence_number.", framer_.detailed_error());
10640}
10641
QUICHE teama6ef0a62019-03-07 20:34:33 -050010642TEST_P(QuicFramerTest, BuildNewConnectionIdFramePacket) {
fkastenholz305e1732019-06-18 05:01:22 -070010643 if (!VersionHasIetfQuicFrames(framer_.transport_version())) {
fkastenholzc5e0c8c2019-09-30 10:16:30 -070010644 // This frame is only for IETF QUIC only.
QUICHE teama6ef0a62019-03-07 20:34:33 -050010645 return;
10646 }
QUICHE team2252b702019-05-14 23:55:14 -040010647 QuicFramerPeer::SetPerspective(&framer_, Perspective::IS_CLIENT);
QUICHE teama6ef0a62019-03-07 20:34:33 -050010648 QuicPacketHeader header;
10649 header.destination_connection_id = FramerTestConnectionId();
10650 header.reset_flag = false;
10651 header.version_flag = false;
10652 header.packet_number = kPacketNumber;
10653
10654 QuicNewConnectionIdFrame frame;
10655 frame.sequence_number = 0x11;
fkastenholz1c19fc22019-07-12 11:06:19 -070010656 frame.retire_prior_to = 0x0c;
QUICHE teama6ef0a62019-03-07 20:34:33 -050010657 // Use this value to force a 4-byte encoded variable length connection ID
10658 // in the frame.
10659 frame.connection_id = FramerTestConnectionIdPlusOne();
10660 frame.stateless_reset_token = kTestStatelessResetToken;
10661
10662 QuicFrames frames = {QuicFrame(&frame)};
10663
10664 // clang-format off
10665 unsigned char packet99[] = {
10666 // type (short header, 4 byte packet number)
10667 0x43,
10668 // connection_id
10669 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
10670 // packet number
10671 0x12, 0x34, 0x56, 0x78,
10672
10673 // frame type (IETF_NEW_CONNECTION_ID frame)
10674 0x18,
10675 // sequence number
10676 kVarInt62OneByte + 0x11,
fkastenholz1c19fc22019-07-12 11:06:19 -070010677 // retire_prior_to
10678 kVarInt62OneByte + 0x0c,
QUICHE teama6ef0a62019-03-07 20:34:33 -050010679 // new connection id length
10680 0x08,
10681 // new connection id
10682 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x11,
10683 // stateless reset token
10684 0xb5, 0x69, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x00,
10685 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
10686 };
10687 // clang-format on
10688
10689 std::unique_ptr<QuicPacket> data(BuildDataPacket(header, frames));
10690 ASSERT_TRUE(data != nullptr);
10691
dmcardle8f7df532020-01-07 13:28:57 -080010692 quiche::test::CompareCharArraysWithHexError(
10693 "constructed packet", data->data(), data->length(), AsChars(packet99),
10694 QUICHE_ARRAYSIZE(packet99));
QUICHE teama6ef0a62019-03-07 20:34:33 -050010695}
10696
10697TEST_P(QuicFramerTest, NewTokenFrame) {
fkastenholz305e1732019-06-18 05:01:22 -070010698 if (!VersionHasIetfQuicFrames(framer_.transport_version())) {
fkastenholzc5e0c8c2019-09-30 10:16:30 -070010699 // This frame is only for IETF QUIC only.
QUICHE teama6ef0a62019-03-07 20:34:33 -050010700 return;
10701 }
zhongyi546cc452019-04-12 15:27:49 -070010702 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
QUICHE teama6ef0a62019-03-07 20:34:33 -050010703 // clang-format off
10704 PacketFragments packet = {
10705 // type (short header, 4 byte packet number)
10706 {"",
10707 {0x43}},
10708 // connection_id
10709 {"",
10710 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
10711 // packet number
10712 {"",
10713 {0x12, 0x34, 0x56, 0x78}},
10714 // frame type (IETF_NEW_TOKEN frame)
10715 {"",
10716 {0x07}},
10717 // Length
10718 {"Unable to read new token length.",
10719 {kVarInt62OneByte + 0x08}},
10720 {"Unable to read new token data.",
10721 {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}}
10722 };
10723 // clang-format on
10724 uint8_t expected_token_value[] = {0x00, 0x01, 0x02, 0x03,
10725 0x04, 0x05, 0x06, 0x07};
10726
10727 std::unique_ptr<QuicEncryptedPacket> encrypted(
10728 AssemblePacketFromFragments(packet));
10729 EXPECT_TRUE(framer_.ProcessPacket(*encrypted));
10730
bncf6f82b12019-10-30 07:01:01 -070010731 EXPECT_THAT(framer_.error(), IsQuicNoError());
QUICHE teama6ef0a62019-03-07 20:34:33 -050010732 ASSERT_TRUE(visitor_.header_.get());
10733 EXPECT_TRUE(CheckDecryption(
10734 *encrypted, !kIncludeVersion, !kIncludeDiversificationNonce,
10735 PACKET_8BYTE_CONNECTION_ID, PACKET_0BYTE_CONNECTION_ID));
10736
10737 EXPECT_EQ(0u, visitor_.stream_frames_.size());
10738
10739 EXPECT_EQ(sizeof(expected_token_value), visitor_.new_token_.token.length());
10740 EXPECT_EQ(0, memcmp(expected_token_value, visitor_.new_token_.token.data(),
10741 sizeof(expected_token_value)));
10742
10743 CheckFramingBoundaries(packet, QUIC_INVALID_NEW_TOKEN);
10744}
10745
10746TEST_P(QuicFramerTest, BuildNewTokenFramePacket) {
fkastenholz305e1732019-06-18 05:01:22 -070010747 if (!VersionHasIetfQuicFrames(framer_.transport_version())) {
fkastenholzc5e0c8c2019-09-30 10:16:30 -070010748 // This frame is only for IETF QUIC only.
QUICHE teama6ef0a62019-03-07 20:34:33 -050010749 return;
10750 }
QUICHE team2252b702019-05-14 23:55:14 -040010751 QuicFramerPeer::SetPerspective(&framer_, Perspective::IS_CLIENT);
QUICHE teama6ef0a62019-03-07 20:34:33 -050010752 QuicPacketHeader header;
10753 header.destination_connection_id = FramerTestConnectionId();
10754 header.reset_flag = false;
10755 header.version_flag = false;
10756 header.packet_number = kPacketNumber;
10757
10758 uint8_t expected_token_value[] = {0x00, 0x01, 0x02, 0x03,
10759 0x04, 0x05, 0x06, 0x07};
10760
vasilvvc48c8712019-03-11 13:38:16 -070010761 QuicNewTokenFrame frame(0, std::string((const char*)(expected_token_value),
10762 sizeof(expected_token_value)));
QUICHE teama6ef0a62019-03-07 20:34:33 -050010763
10764 QuicFrames frames = {QuicFrame(&frame)};
10765
10766 // clang-format off
10767 unsigned char packet[] = {
10768 // type (short header, 4 byte packet number)
10769 0x43,
10770 // connection_id
10771 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
10772 // packet number
10773 0x12, 0x34, 0x56, 0x78,
10774
10775 // frame type (IETF_NEW_TOKEN frame)
10776 0x07,
10777 // Length and token
10778 kVarInt62OneByte + 0x08,
10779 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07
10780 };
10781 // clang-format on
10782
10783 std::unique_ptr<QuicPacket> data(BuildDataPacket(header, frames));
10784 ASSERT_TRUE(data != nullptr);
10785
dmcardle8f7df532020-01-07 13:28:57 -080010786 quiche::test::CompareCharArraysWithHexError(
10787 "constructed packet", data->data(), data->length(), AsChars(packet),
10788 QUICHE_ARRAYSIZE(packet));
QUICHE teama6ef0a62019-03-07 20:34:33 -050010789}
10790
10791TEST_P(QuicFramerTest, IetfStopSendingFrame) {
fkastenholz305e1732019-06-18 05:01:22 -070010792 if (!VersionHasIetfQuicFrames(framer_.transport_version())) {
fkastenholzc5e0c8c2019-09-30 10:16:30 -070010793 // Stop sending frame is IETF QUIC only.
QUICHE teama6ef0a62019-03-07 20:34:33 -050010794 return;
10795 }
zhongyi546cc452019-04-12 15:27:49 -070010796 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
QUICHE teama6ef0a62019-03-07 20:34:33 -050010797
10798 // clang-format off
10799 PacketFragments packet99 = {
10800 // type (short header, 4 byte packet number)
10801 {"",
10802 {0x43}},
10803 // connection_id
10804 {"",
10805 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
10806 // packet number
10807 {"",
10808 {0x12, 0x34, 0x9A, 0xBC}},
10809 // frame type (IETF_STOP_SENDING frame)
10810 {"",
10811 {0x05}},
10812 // stream id
10813 {"Unable to read stop sending stream id.",
10814 {kVarInt62FourBytes + 0x01, 0x02, 0x03, 0x04}},
10815 {"Unable to read stop sending application error code.",
fkastenholz733552e2019-07-16 11:16:58 -070010816 {kVarInt62FourBytes + 0x00, 0x00, 0x76, 0x54}},
QUICHE teama6ef0a62019-03-07 20:34:33 -050010817 };
10818 // clang-format on
10819
10820 std::unique_ptr<QuicEncryptedPacket> encrypted(
10821 AssemblePacketFromFragments(packet99));
10822 EXPECT_TRUE(framer_.ProcessPacket(*encrypted));
10823
bncf6f82b12019-10-30 07:01:01 -070010824 EXPECT_THAT(framer_.error(), IsQuicNoError());
QUICHE teama6ef0a62019-03-07 20:34:33 -050010825 ASSERT_TRUE(visitor_.header_.get());
10826 EXPECT_TRUE(CheckDecryption(
10827 *encrypted, !kIncludeVersion, !kIncludeDiversificationNonce,
10828 PACKET_8BYTE_CONNECTION_ID, PACKET_0BYTE_CONNECTION_ID));
10829
10830 EXPECT_EQ(kStreamId, visitor_.stop_sending_frame_.stream_id);
10831 EXPECT_EQ(0x7654, visitor_.stop_sending_frame_.application_error_code);
10832
10833 CheckFramingBoundaries(packet99, QUIC_INVALID_STOP_SENDING_FRAME_DATA);
10834}
10835
10836TEST_P(QuicFramerTest, BuildIetfStopSendingPacket) {
fkastenholz305e1732019-06-18 05:01:22 -070010837 if (!VersionHasIetfQuicFrames(framer_.transport_version())) {
fkastenholzc5e0c8c2019-09-30 10:16:30 -070010838 // Stop sending frame is IETF QUIC only.
QUICHE teama6ef0a62019-03-07 20:34:33 -050010839 return;
10840 }
10841
10842 QuicPacketHeader header;
10843 header.destination_connection_id = FramerTestConnectionId();
10844 header.reset_flag = false;
10845 header.version_flag = false;
10846 header.packet_number = kPacketNumber;
10847
10848 QuicStopSendingFrame frame;
10849 frame.stream_id = kStreamId;
10850 frame.application_error_code = 0xffff;
10851 QuicFrames frames = {QuicFrame(&frame)};
10852
10853 // clang-format off
10854 unsigned char packet99[] = {
10855 // type (short header, 4 byte packet number)
10856 0x43,
10857 // connection_id
10858 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
10859 // packet number
10860 0x12, 0x34, 0x56, 0x78,
10861
10862 // frame type (IETF_STOP_SENDING frame)
10863 0x05,
10864 // Stream ID
10865 kVarInt62FourBytes + 0x01, 0x02, 0x03, 0x04,
10866 // Application error code
fkastenholz733552e2019-07-16 11:16:58 -070010867 kVarInt62FourBytes + 0x00, 0x00, 0xff, 0xff
QUICHE teama6ef0a62019-03-07 20:34:33 -050010868 };
10869 // clang-format on
10870
10871 std::unique_ptr<QuicPacket> data(BuildDataPacket(header, frames));
10872 ASSERT_TRUE(data != nullptr);
10873
dmcardle8f7df532020-01-07 13:28:57 -080010874 quiche::test::CompareCharArraysWithHexError(
10875 "constructed packet", data->data(), data->length(), AsChars(packet99),
10876 QUICHE_ARRAYSIZE(packet99));
QUICHE teama6ef0a62019-03-07 20:34:33 -050010877}
10878
10879TEST_P(QuicFramerTest, IetfPathChallengeFrame) {
fkastenholz305e1732019-06-18 05:01:22 -070010880 if (!VersionHasIetfQuicFrames(framer_.transport_version())) {
fkastenholzc5e0c8c2019-09-30 10:16:30 -070010881 // Path Challenge frame is IETF QUIC only.
QUICHE teama6ef0a62019-03-07 20:34:33 -050010882 return;
10883 }
zhongyi546cc452019-04-12 15:27:49 -070010884 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
QUICHE teama6ef0a62019-03-07 20:34:33 -050010885
10886 // clang-format off
10887 PacketFragments packet99 = {
10888 // type (short header, 4 byte packet number)
10889 {"",
10890 {0x43}},
10891 // connection_id
10892 {"",
10893 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
10894 // packet number
10895 {"",
10896 {0x12, 0x34, 0x9A, 0xBC}},
10897 // frame type (IETF_PATH_CHALLENGE)
10898 {"",
10899 {0x1a}},
10900 // data
10901 {"Can not read path challenge data.",
10902 {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}},
10903 };
10904 // clang-format on
10905
10906 std::unique_ptr<QuicEncryptedPacket> encrypted(
10907 AssemblePacketFromFragments(packet99));
10908 EXPECT_TRUE(framer_.ProcessPacket(*encrypted));
10909
bncf6f82b12019-10-30 07:01:01 -070010910 EXPECT_THAT(framer_.error(), IsQuicNoError());
QUICHE teama6ef0a62019-03-07 20:34:33 -050010911 ASSERT_TRUE(visitor_.header_.get());
10912 EXPECT_TRUE(CheckDecryption(
10913 *encrypted, !kIncludeVersion, !kIncludeDiversificationNonce,
10914 PACKET_8BYTE_CONNECTION_ID, PACKET_0BYTE_CONNECTION_ID));
10915
10916 EXPECT_EQ(QuicPathFrameBuffer({{0, 1, 2, 3, 4, 5, 6, 7}}),
10917 visitor_.path_challenge_frame_.data_buffer);
10918
10919 CheckFramingBoundaries(packet99, QUIC_INVALID_PATH_CHALLENGE_DATA);
10920}
10921
10922TEST_P(QuicFramerTest, BuildIetfPathChallengePacket) {
fkastenholz305e1732019-06-18 05:01:22 -070010923 if (!VersionHasIetfQuicFrames(framer_.transport_version())) {
fkastenholzc5e0c8c2019-09-30 10:16:30 -070010924 // Path Challenge frame is IETF QUIC only.
QUICHE teama6ef0a62019-03-07 20:34:33 -050010925 return;
10926 }
10927
10928 QuicPacketHeader header;
10929 header.destination_connection_id = FramerTestConnectionId();
10930 header.reset_flag = false;
10931 header.version_flag = false;
10932 header.packet_number = kPacketNumber;
10933
10934 QuicPathChallengeFrame frame;
10935 frame.data_buffer = QuicPathFrameBuffer({{0, 1, 2, 3, 4, 5, 6, 7}});
10936 QuicFrames frames = {QuicFrame(&frame)};
10937
10938 // clang-format off
10939 unsigned char packet99[] = {
10940 // type (short header, 4 byte packet number)
10941 0x43,
10942 // connection_id
10943 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
10944 // packet number
10945 0x12, 0x34, 0x56, 0x78,
10946
10947 // frame type (IETF_PATH_CHALLENGE)
10948 0x1a,
10949 // Data
10950 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07
10951 };
10952 // clang-format on
10953
10954 std::unique_ptr<QuicPacket> data(BuildDataPacket(header, frames));
10955 ASSERT_TRUE(data != nullptr);
10956
dmcardle8f7df532020-01-07 13:28:57 -080010957 quiche::test::CompareCharArraysWithHexError(
10958 "constructed packet", data->data(), data->length(), AsChars(packet99),
10959 QUICHE_ARRAYSIZE(packet99));
QUICHE teama6ef0a62019-03-07 20:34:33 -050010960}
10961
10962TEST_P(QuicFramerTest, IetfPathResponseFrame) {
fkastenholz305e1732019-06-18 05:01:22 -070010963 if (!VersionHasIetfQuicFrames(framer_.transport_version())) {
fkastenholzc5e0c8c2019-09-30 10:16:30 -070010964 // Path response frame is IETF QUIC only.
QUICHE teama6ef0a62019-03-07 20:34:33 -050010965 return;
10966 }
zhongyi546cc452019-04-12 15:27:49 -070010967 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
QUICHE teama6ef0a62019-03-07 20:34:33 -050010968
10969 // clang-format off
10970 PacketFragments packet99 = {
10971 // type (short header, 4 byte packet number)
10972 {"",
10973 {0x43}},
10974 // connection_id
10975 {"",
10976 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
10977 // packet number
10978 {"",
10979 {0x12, 0x34, 0x9A, 0xBC}},
10980 // frame type (IETF_PATH_RESPONSE)
10981 {"",
10982 {0x1b}},
10983 // data
10984 {"Can not read path response data.",
10985 {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}},
10986 };
10987 // clang-format on
10988
10989 std::unique_ptr<QuicEncryptedPacket> encrypted(
10990 AssemblePacketFromFragments(packet99));
10991 EXPECT_TRUE(framer_.ProcessPacket(*encrypted));
10992
bncf6f82b12019-10-30 07:01:01 -070010993 EXPECT_THAT(framer_.error(), IsQuicNoError());
QUICHE teama6ef0a62019-03-07 20:34:33 -050010994 ASSERT_TRUE(visitor_.header_.get());
10995 EXPECT_TRUE(CheckDecryption(
10996 *encrypted, !kIncludeVersion, !kIncludeDiversificationNonce,
10997 PACKET_8BYTE_CONNECTION_ID, PACKET_0BYTE_CONNECTION_ID));
10998
10999 EXPECT_EQ(QuicPathFrameBuffer({{0, 1, 2, 3, 4, 5, 6, 7}}),
11000 visitor_.path_response_frame_.data_buffer);
11001
11002 CheckFramingBoundaries(packet99, QUIC_INVALID_PATH_RESPONSE_DATA);
11003}
11004
11005TEST_P(QuicFramerTest, BuildIetfPathResponsePacket) {
fkastenholz305e1732019-06-18 05:01:22 -070011006 if (!VersionHasIetfQuicFrames(framer_.transport_version())) {
fkastenholzc5e0c8c2019-09-30 10:16:30 -070011007 // Path response frame is IETF QUIC only
QUICHE teama6ef0a62019-03-07 20:34:33 -050011008 return;
11009 }
11010
11011 QuicPacketHeader header;
11012 header.destination_connection_id = FramerTestConnectionId();
11013 header.reset_flag = false;
11014 header.version_flag = false;
11015 header.packet_number = kPacketNumber;
11016
11017 QuicPathResponseFrame frame;
11018 frame.data_buffer = QuicPathFrameBuffer({{0, 1, 2, 3, 4, 5, 6, 7}});
11019 QuicFrames frames = {QuicFrame(&frame)};
11020
11021 // clang-format off
11022 unsigned char packet99[] = {
11023 // type (short header, 4 byte packet number)
11024 0x43,
11025 // connection_id
11026 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
11027 // packet number
11028 0x12, 0x34, 0x56, 0x78,
11029
11030 // frame type (IETF_PATH_RESPONSE)
11031 0x1b,
11032 // Data
11033 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07
11034 };
11035 // clang-format on
11036
11037 std::unique_ptr<QuicPacket> data(BuildDataPacket(header, frames));
11038 ASSERT_TRUE(data != nullptr);
11039
dmcardle8f7df532020-01-07 13:28:57 -080011040 quiche::test::CompareCharArraysWithHexError(
11041 "constructed packet", data->data(), data->length(), AsChars(packet99),
11042 QUICHE_ARRAYSIZE(packet99));
QUICHE teama6ef0a62019-03-07 20:34:33 -050011043}
11044
11045TEST_P(QuicFramerTest, GetRetransmittableControlFrameSize) {
11046 QuicRstStreamFrame rst_stream(1, 3, QUIC_STREAM_CANCELLED, 1024);
11047 EXPECT_EQ(QuicFramer::GetRstStreamFrameSize(framer_.transport_version(),
11048 rst_stream),
11049 QuicFramer::GetRetransmittableControlFrameSize(
11050 framer_.transport_version(), QuicFrame(&rst_stream)));
11051
vasilvvc48c8712019-03-11 13:38:16 -070011052 std::string error_detail(2048, 'e');
wuba0cbbdc2019-09-09 06:40:59 -070011053 QuicConnectionCloseFrame connection_close(
11054 framer_.transport_version(), QUIC_NETWORK_IDLE_TIMEOUT, error_detail,
11055 /*transport_close_frame_type=*/0);
fkastenholz72f509b2019-04-10 09:17:49 -070011056
fkastenholza037b8b2019-05-07 06:00:05 -070011057 EXPECT_EQ(QuicFramer::GetConnectionCloseFrameSize(framer_.transport_version(),
11058 connection_close),
QUICHE teama6ef0a62019-03-07 20:34:33 -050011059 QuicFramer::GetRetransmittableControlFrameSize(
11060 framer_.transport_version(), QuicFrame(&connection_close)));
11061
11062 QuicGoAwayFrame goaway(2, QUIC_PEER_GOING_AWAY, 3, error_detail);
11063 EXPECT_EQ(QuicFramer::GetMinGoAwayFrameSize() + 256,
11064 QuicFramer::GetRetransmittableControlFrameSize(
11065 framer_.transport_version(), QuicFrame(&goaway)));
11066
11067 QuicWindowUpdateFrame window_update(3, 3, 1024);
11068 EXPECT_EQ(QuicFramer::GetWindowUpdateFrameSize(framer_.transport_version(),
11069 window_update),
11070 QuicFramer::GetRetransmittableControlFrameSize(
11071 framer_.transport_version(), QuicFrame(&window_update)));
11072
11073 QuicBlockedFrame blocked(4, 3, 1024);
11074 EXPECT_EQ(
11075 QuicFramer::GetBlockedFrameSize(framer_.transport_version(), blocked),
11076 QuicFramer::GetRetransmittableControlFrameSize(
11077 framer_.transport_version(), QuicFrame(&blocked)));
11078
fkastenholzc5e0c8c2019-09-30 10:16:30 -070011079 // Following frames are IETF QUIC frames only.
fkastenholz305e1732019-06-18 05:01:22 -070011080 if (!VersionHasIetfQuicFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -050011081 return;
11082 }
QUICHE teama6ef0a62019-03-07 20:34:33 -050011083
fkastenholz1c19fc22019-07-12 11:06:19 -070011084 QuicNewConnectionIdFrame new_connection_id(5, TestConnectionId(), 1, 101111,
11085 1);
QUICHE teama6ef0a62019-03-07 20:34:33 -050011086 EXPECT_EQ(QuicFramer::GetNewConnectionIdFrameSize(new_connection_id),
11087 QuicFramer::GetRetransmittableControlFrameSize(
11088 framer_.transport_version(), QuicFrame(&new_connection_id)));
11089
fkastenholz3c4eabf2019-04-22 07:49:59 -070011090 QuicMaxStreamsFrame max_streams(6, 3, /*unidirectional=*/false);
QUICHE teama6ef0a62019-03-07 20:34:33 -050011091 EXPECT_EQ(QuicFramer::GetMaxStreamsFrameSize(framer_.transport_version(),
fkastenholz3c4eabf2019-04-22 07:49:59 -070011092 max_streams),
QUICHE teama6ef0a62019-03-07 20:34:33 -050011093 QuicFramer::GetRetransmittableControlFrameSize(
fkastenholz3c4eabf2019-04-22 07:49:59 -070011094 framer_.transport_version(), QuicFrame(max_streams)));
QUICHE teama6ef0a62019-03-07 20:34:33 -050011095
fkastenholz3c4eabf2019-04-22 07:49:59 -070011096 QuicStreamsBlockedFrame streams_blocked(7, 3, /*unidirectional=*/false);
QUICHE teama6ef0a62019-03-07 20:34:33 -050011097 EXPECT_EQ(QuicFramer::GetStreamsBlockedFrameSize(framer_.transport_version(),
fkastenholz3c4eabf2019-04-22 07:49:59 -070011098 streams_blocked),
QUICHE teama6ef0a62019-03-07 20:34:33 -050011099 QuicFramer::GetRetransmittableControlFrameSize(
fkastenholz3c4eabf2019-04-22 07:49:59 -070011100 framer_.transport_version(), QuicFrame(streams_blocked)));
QUICHE teama6ef0a62019-03-07 20:34:33 -050011101
11102 QuicPathFrameBuffer buffer = {
11103 {0x80, 0x91, 0xa2, 0xb3, 0xc4, 0xd5, 0xe5, 0xf7}};
11104 QuicPathResponseFrame path_response_frame(8, buffer);
11105 EXPECT_EQ(QuicFramer::GetPathResponseFrameSize(path_response_frame),
11106 QuicFramer::GetRetransmittableControlFrameSize(
11107 framer_.transport_version(), QuicFrame(&path_response_frame)));
11108
11109 QuicPathChallengeFrame path_challenge_frame(9, buffer);
11110 EXPECT_EQ(QuicFramer::GetPathChallengeFrameSize(path_challenge_frame),
11111 QuicFramer::GetRetransmittableControlFrameSize(
11112 framer_.transport_version(), QuicFrame(&path_challenge_frame)));
11113
11114 QuicStopSendingFrame stop_sending_frame(10, 3, 20);
11115 EXPECT_EQ(QuicFramer::GetStopSendingFrameSize(stop_sending_frame),
11116 QuicFramer::GetRetransmittableControlFrameSize(
11117 framer_.transport_version(), QuicFrame(&stop_sending_frame)));
11118}
11119
11120// A set of tests to ensure that bad frame-type encodings
11121// are properly detected and handled.
11122// First, four tests to see that unknown frame types generate
11123// a QUIC_INVALID_FRAME_DATA error with detailed information
11124// "Illegal frame type." This regardless of the encoding of the type
11125// (1/2/4/8 bytes).
11126// This only for version 99.
11127TEST_P(QuicFramerTest, IetfFrameTypeEncodingErrorUnknown1Byte) {
fkastenholz305e1732019-06-18 05:01:22 -070011128 if (!VersionHasIetfQuicFrames(framer_.transport_version())) {
fkastenholzc5e0c8c2019-09-30 10:16:30 -070011129 // Only IETF QUIC encodes frame types such that this test is relevant.
QUICHE teama6ef0a62019-03-07 20:34:33 -050011130 return;
11131 }
zhongyi546cc452019-04-12 15:27:49 -070011132 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
QUICHE teama6ef0a62019-03-07 20:34:33 -050011133 // clang-format off
11134 PacketFragments packet = {
11135 // type (short header, 4 byte packet number)
11136 {"",
11137 {0x43}},
11138 // connection_id
11139 {"",
11140 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
11141 // packet number
11142 {"",
11143 {0x12, 0x34, 0x9A, 0xBC}},
11144 // frame type (unknown value, single-byte encoding)
11145 {"",
11146 {0x38}}
11147 };
11148 // clang-format on
11149
11150 std::unique_ptr<QuicEncryptedPacket> encrypted(
11151 AssemblePacketFromFragments(packet));
11152
11153 EXPECT_FALSE(framer_.ProcessPacket(*encrypted));
11154
bncf6f82b12019-10-30 07:01:01 -070011155 EXPECT_THAT(framer_.error(), IsError(QUIC_INVALID_FRAME_DATA));
QUICHE teama6ef0a62019-03-07 20:34:33 -050011156 EXPECT_EQ("Illegal frame type.", framer_.detailed_error());
11157}
11158
11159TEST_P(QuicFramerTest, IetfFrameTypeEncodingErrorUnknown2Bytes) {
fkastenholz305e1732019-06-18 05:01:22 -070011160 if (!VersionHasIetfQuicFrames(framer_.transport_version())) {
fkastenholzc5e0c8c2019-09-30 10:16:30 -070011161 // Only IETF QUIC encodes frame types such that this test is relevant.
QUICHE teama6ef0a62019-03-07 20:34:33 -050011162 return;
11163 }
zhongyi546cc452019-04-12 15:27:49 -070011164 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
QUICHE teama6ef0a62019-03-07 20:34:33 -050011165
11166 // clang-format off
11167 PacketFragments packet = {
11168 // type (short header, 4 byte packet number)
11169 {"",
11170 {0x43}},
11171 // connection_id
11172 {"",
11173 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
11174 // packet number
11175 {"",
11176 {0x12, 0x34, 0x9A, 0xBC}},
11177 // frame type (unknown value, two-byte encoding)
11178 {"",
11179 {kVarInt62TwoBytes + 0x01, 0x38}}
11180 };
11181 // clang-format on
11182
11183 std::unique_ptr<QuicEncryptedPacket> encrypted(
11184 AssemblePacketFromFragments(packet));
11185
11186 EXPECT_FALSE(framer_.ProcessPacket(*encrypted));
11187
bncf6f82b12019-10-30 07:01:01 -070011188 EXPECT_THAT(framer_.error(), IsError(QUIC_INVALID_FRAME_DATA));
QUICHE teama6ef0a62019-03-07 20:34:33 -050011189 EXPECT_EQ("Illegal frame type.", framer_.detailed_error());
11190}
11191
11192TEST_P(QuicFramerTest, IetfFrameTypeEncodingErrorUnknown4Bytes) {
fkastenholz305e1732019-06-18 05:01:22 -070011193 if (!VersionHasIetfQuicFrames(framer_.transport_version())) {
fkastenholzc5e0c8c2019-09-30 10:16:30 -070011194 // Only IETF QUIC encodes frame types such that this test is relevant.
QUICHE teama6ef0a62019-03-07 20:34:33 -050011195 return;
11196 }
zhongyi546cc452019-04-12 15:27:49 -070011197 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
QUICHE teama6ef0a62019-03-07 20:34:33 -050011198
11199 // clang-format off
11200 PacketFragments packet = {
11201 // type (short header, 4 byte packet number)
11202 {"",
11203 {0x43}},
11204 // connection_id
11205 {"",
11206 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
11207 // packet number
11208 {"",
11209 {0x12, 0x34, 0x9A, 0xBC}},
11210 // frame type (unknown value, four-byte encoding)
11211 {"",
11212 {kVarInt62FourBytes + 0x01, 0x00, 0x00, 0x38}}
11213 };
11214 // clang-format on
11215
11216 std::unique_ptr<QuicEncryptedPacket> encrypted(
11217 AssemblePacketFromFragments(packet));
11218
11219 EXPECT_FALSE(framer_.ProcessPacket(*encrypted));
11220
bncf6f82b12019-10-30 07:01:01 -070011221 EXPECT_THAT(framer_.error(), IsError(QUIC_INVALID_FRAME_DATA));
QUICHE teama6ef0a62019-03-07 20:34:33 -050011222 EXPECT_EQ("Illegal frame type.", framer_.detailed_error());
11223}
11224
11225TEST_P(QuicFramerTest, IetfFrameTypeEncodingErrorUnknown8Bytes) {
fkastenholz305e1732019-06-18 05:01:22 -070011226 if (!VersionHasIetfQuicFrames(framer_.transport_version())) {
fkastenholzc5e0c8c2019-09-30 10:16:30 -070011227 // Only IETF QUIC encodes frame types such that this test is relevant.
QUICHE teama6ef0a62019-03-07 20:34:33 -050011228 return;
11229 }
zhongyi546cc452019-04-12 15:27:49 -070011230 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
QUICHE teama6ef0a62019-03-07 20:34:33 -050011231 // clang-format off
11232 PacketFragments packet = {
11233 // type (short header, 4 byte packet number)
11234 {"",
11235 {0x43}},
11236 // connection_id
11237 {"",
11238 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
11239 // packet number
11240 {"",
11241 {0x12, 0x34, 0x9A, 0xBC}},
11242 // frame type (unknown value, eight-byte encoding)
11243 {"",
11244 {kVarInt62EightBytes + 0x01, 0x00, 0x00, 0x01, 0x02, 0x34, 0x56, 0x38}}
11245 };
11246 // clang-format on
11247
11248 std::unique_ptr<QuicEncryptedPacket> encrypted(
11249 AssemblePacketFromFragments(packet));
11250
11251 EXPECT_FALSE(framer_.ProcessPacket(*encrypted));
11252
bncf6f82b12019-10-30 07:01:01 -070011253 EXPECT_THAT(framer_.error(), IsError(QUIC_INVALID_FRAME_DATA));
QUICHE teama6ef0a62019-03-07 20:34:33 -050011254 EXPECT_EQ("Illegal frame type.", framer_.detailed_error());
11255}
11256
11257// Three tests to check that known frame types that are not minimally
11258// encoded generate IETF_QUIC_PROTOCOL_VIOLATION errors with detailed
11259// information "Frame type not minimally encoded."
11260// Look at the frame-type encoded in 2, 4, and 8 bytes.
11261TEST_P(QuicFramerTest, IetfFrameTypeEncodingErrorKnown2Bytes) {
fkastenholz305e1732019-06-18 05:01:22 -070011262 if (!VersionHasIetfQuicFrames(framer_.transport_version())) {
fkastenholzc5e0c8c2019-09-30 10:16:30 -070011263 // Only IETF QUIC encodes frame types such that this test is relevant.
QUICHE teama6ef0a62019-03-07 20:34:33 -050011264 return;
11265 }
zhongyi546cc452019-04-12 15:27:49 -070011266 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
QUICHE teama6ef0a62019-03-07 20:34:33 -050011267
11268 // clang-format off
11269 PacketFragments packet = {
11270 // type (short header, 4 byte packet number)
11271 {"",
11272 {0x43}},
11273 // connection_id
11274 {"",
11275 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
11276 // packet number
11277 {"",
11278 {0x12, 0x34, 0x9A, 0xBC}},
11279 // frame type (Blocked, two-byte encoding)
11280 {"",
11281 {kVarInt62TwoBytes + 0x00, 0x08}}
11282 };
11283 // clang-format on
11284
11285 std::unique_ptr<QuicEncryptedPacket> encrypted(
11286 AssemblePacketFromFragments(packet));
11287
11288 EXPECT_FALSE(framer_.ProcessPacket(*encrypted));
11289
bncf54082a2019-11-27 10:19:47 -080011290 EXPECT_THAT(framer_.error(), IsError(IETF_QUIC_PROTOCOL_VIOLATION));
QUICHE teama6ef0a62019-03-07 20:34:33 -050011291 EXPECT_EQ("Frame type not minimally encoded.", framer_.detailed_error());
11292}
11293
11294TEST_P(QuicFramerTest, IetfFrameTypeEncodingErrorKnown4Bytes) {
fkastenholz305e1732019-06-18 05:01:22 -070011295 if (!VersionHasIetfQuicFrames(framer_.transport_version())) {
fkastenholzc5e0c8c2019-09-30 10:16:30 -070011296 // Only IETF QUIC encodes frame types such that this test is relevant.
QUICHE teama6ef0a62019-03-07 20:34:33 -050011297 return;
11298 }
zhongyi546cc452019-04-12 15:27:49 -070011299 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
QUICHE teama6ef0a62019-03-07 20:34:33 -050011300
11301 // clang-format off
11302 PacketFragments packet = {
11303 // type (short header, 4 byte packet number)
11304 {"",
11305 {0x43}},
11306 // connection_id
11307 {"",
11308 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
11309 // packet number
11310 {"",
11311 {0x12, 0x34, 0x9A, 0xBC}},
11312 // frame type (Blocked, four-byte encoding)
11313 {"",
11314 {kVarInt62FourBytes + 0x00, 0x00, 0x00, 0x08}}
11315 };
11316 // clang-format on
11317
11318 std::unique_ptr<QuicEncryptedPacket> encrypted(
11319 AssemblePacketFromFragments(packet));
11320
11321 EXPECT_FALSE(framer_.ProcessPacket(*encrypted));
11322
bncf54082a2019-11-27 10:19:47 -080011323 EXPECT_THAT(framer_.error(), IsError(IETF_QUIC_PROTOCOL_VIOLATION));
QUICHE teama6ef0a62019-03-07 20:34:33 -050011324 EXPECT_EQ("Frame type not minimally encoded.", framer_.detailed_error());
11325}
11326
11327TEST_P(QuicFramerTest, IetfFrameTypeEncodingErrorKnown8Bytes) {
fkastenholz305e1732019-06-18 05:01:22 -070011328 if (!VersionHasIetfQuicFrames(framer_.transport_version())) {
fkastenholzc5e0c8c2019-09-30 10:16:30 -070011329 // Only IETF QUIC encodes frame types such that this test is relevant.
QUICHE teama6ef0a62019-03-07 20:34:33 -050011330 return;
11331 }
zhongyi546cc452019-04-12 15:27:49 -070011332 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
QUICHE teama6ef0a62019-03-07 20:34:33 -050011333 // clang-format off
11334 PacketFragments packet = {
11335 // type (short header, 4 byte packet number)
11336 {"",
11337 {0x43}},
11338 // connection_id
11339 {"",
11340 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
11341 // packet number
11342 {"",
11343 {0x12, 0x34, 0x9A, 0xBC}},
11344 // frame type (Blocked, eight-byte encoding)
11345 {"",
11346 {kVarInt62EightBytes + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08}}
11347 };
11348 // clang-format on
11349
11350 std::unique_ptr<QuicEncryptedPacket> encrypted(
11351 AssemblePacketFromFragments(packet));
11352
11353 EXPECT_FALSE(framer_.ProcessPacket(*encrypted));
11354
bncf54082a2019-11-27 10:19:47 -080011355 EXPECT_THAT(framer_.error(), IsError(IETF_QUIC_PROTOCOL_VIOLATION));
QUICHE teama6ef0a62019-03-07 20:34:33 -050011356 EXPECT_EQ("Frame type not minimally encoded.", framer_.detailed_error());
11357}
11358
fkastenholzc5e0c8c2019-09-30 10:16:30 -070011359// Tests to check that all known IETF frame types that are not minimally
QUICHE teama6ef0a62019-03-07 20:34:33 -050011360// encoded generate IETF_QUIC_PROTOCOL_VIOLATION errors with detailed
11361// information "Frame type not minimally encoded."
11362// Just look at 2-byte encoding.
11363TEST_P(QuicFramerTest, IetfFrameTypeEncodingErrorKnown2BytesAllTypes) {
fkastenholz305e1732019-06-18 05:01:22 -070011364 if (!VersionHasIetfQuicFrames(framer_.transport_version())) {
fkastenholzc5e0c8c2019-09-30 10:16:30 -070011365 // Only IETF QUIC encodes frame types such that this test is relevant.
QUICHE teama6ef0a62019-03-07 20:34:33 -050011366 return;
11367 }
zhongyi546cc452019-04-12 15:27:49 -070011368 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
QUICHE teama6ef0a62019-03-07 20:34:33 -050011369
11370 // clang-format off
11371 PacketFragments packets[] = {
11372 {
11373 // type (short header, 4 byte packet number)
11374 {"",
11375 {0x43}},
11376 // connection_id
11377 {"",
11378 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
11379 // packet number
11380 {"",
11381 {0x12, 0x34, 0x9A, 0xBC}},
11382 // frame type (two-byte encoding)
11383 {"",
11384 {kVarInt62TwoBytes + 0x00, 0x00}}
11385 },
11386 {
11387 // type (short header, 4 byte packet number)
11388 {"",
11389 {0x43}},
11390 // connection_id
11391 {"",
11392 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
11393 // packet number
11394 {"",
11395 {0x12, 0x34, 0x9A, 0xBC}},
11396 // frame type (two-byte encoding)
11397 {"",
11398 {kVarInt62TwoBytes + 0x00, 0x01}}
11399 },
11400 {
11401 // type (short header, 4 byte packet number)
11402 {"",
11403 {0x43}},
11404 // connection_id
11405 {"",
11406 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
11407 // packet number
11408 {"",
11409 {0x12, 0x34, 0x9A, 0xBC}},
11410 // frame type (two-byte encoding)
11411 {"",
11412 {kVarInt62TwoBytes + 0x00, 0x02}}
11413 },
11414 {
11415 // type (short header, 4 byte packet number)
11416 {"",
11417 {0x43}},
11418 // connection_id
11419 {"",
11420 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
11421 // packet number
11422 {"",
11423 {0x12, 0x34, 0x9A, 0xBC}},
11424 // frame type (two-byte encoding)
11425 {"",
11426 {kVarInt62TwoBytes + 0x00, 0x03}}
11427 },
11428 {
11429 // type (short header, 4 byte packet number)
11430 {"",
11431 {0x43}},
11432 // connection_id
11433 {"",
11434 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
11435 // packet number
11436 {"",
11437 {0x12, 0x34, 0x9A, 0xBC}},
11438 // frame type (two-byte encoding)
11439 {"",
11440 {kVarInt62TwoBytes + 0x00, 0x04}}
11441 },
11442 {
11443 // type (short header, 4 byte packet number)
11444 {"",
11445 {0x43}},
11446 // connection_id
11447 {"",
11448 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
11449 // packet number
11450 {"",
11451 {0x12, 0x34, 0x9A, 0xBC}},
11452 // frame type (two-byte encoding)
11453 {"",
11454 {kVarInt62TwoBytes + 0x00, 0x05}}
11455 },
11456 {
11457 // type (short header, 4 byte packet number)
11458 {"",
11459 {0x43}},
11460 // connection_id
11461 {"",
11462 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
11463 // packet number
11464 {"",
11465 {0x12, 0x34, 0x9A, 0xBC}},
11466 // frame type (two-byte encoding)
11467 {"",
11468 {kVarInt62TwoBytes + 0x00, 0x06}}
11469 },
11470 {
11471 // type (short header, 4 byte packet number)
11472 {"",
11473 {0x43}},
11474 // connection_id
11475 {"",
11476 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
11477 // packet number
11478 {"",
11479 {0x12, 0x34, 0x9A, 0xBC}},
11480 // frame type (two-byte encoding)
11481 {"",
11482 {kVarInt62TwoBytes + 0x00, 0x07}}
11483 },
11484 {
11485 // type (short header, 4 byte packet number)
11486 {"",
11487 {0x43}},
11488 // connection_id
11489 {"",
11490 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
11491 // packet number
11492 {"",
11493 {0x12, 0x34, 0x9A, 0xBC}},
11494 // frame type (two-byte encoding)
11495 {"",
11496 {kVarInt62TwoBytes + 0x00, 0x08}}
11497 },
11498 {
11499 // type (short header, 4 byte packet number)
11500 {"",
11501 {0x43}},
11502 // connection_id
11503 {"",
11504 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
11505 // packet number
11506 {"",
11507 {0x12, 0x34, 0x9A, 0xBC}},
11508 // frame type (two-byte encoding)
11509 {"",
11510 {kVarInt62TwoBytes + 0x00, 0x09}}
11511 },
11512 {
11513 // type (short header, 4 byte packet number)
11514 {"",
11515 {0x43}},
11516 // connection_id
11517 {"",
11518 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
11519 // packet number
11520 {"",
11521 {0x12, 0x34, 0x9A, 0xBC}},
11522 // frame type (two-byte encoding)
11523 {"",
11524 {kVarInt62TwoBytes + 0x00, 0x0a}}
11525 },
11526 {
11527 // type (short header, 4 byte packet number)
11528 {"",
11529 {0x43}},
11530 // connection_id
11531 {"",
11532 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
11533 // packet number
11534 {"",
11535 {0x12, 0x34, 0x9A, 0xBC}},
11536 // frame type (two-byte encoding)
11537 {"",
11538 {kVarInt62TwoBytes + 0x00, 0x0b}}
11539 },
11540 {
11541 // type (short header, 4 byte packet number)
11542 {"",
11543 {0x43}},
11544 // connection_id
11545 {"",
11546 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
11547 // packet number
11548 {"",
11549 {0x12, 0x34, 0x9A, 0xBC}},
11550 // frame type (two-byte encoding)
11551 {"",
11552 {kVarInt62TwoBytes + 0x00, 0x0c}}
11553 },
11554 {
11555 // type (short header, 4 byte packet number)
11556 {"",
11557 {0x43}},
11558 // connection_id
11559 {"",
11560 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
11561 // packet number
11562 {"",
11563 {0x12, 0x34, 0x9A, 0xBC}},
11564 // frame type (two-byte encoding)
11565 {"",
11566 {kVarInt62TwoBytes + 0x00, 0x0d}}
11567 },
11568 {
11569 // type (short header, 4 byte packet number)
11570 {"",
11571 {0x43}},
11572 // connection_id
11573 {"",
11574 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
11575 // packet number
11576 {"",
11577 {0x12, 0x34, 0x9A, 0xBC}},
11578 // frame type (two-byte encoding)
11579 {"",
11580 {kVarInt62TwoBytes + 0x00, 0x0e}}
11581 },
11582 {
11583 // type (short header, 4 byte packet number)
11584 {"",
11585 {0x43}},
11586 // connection_id
11587 {"",
11588 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
11589 // packet number
11590 {"",
11591 {0x12, 0x34, 0x9A, 0xBC}},
11592 // frame type (two-byte encoding)
11593 {"",
11594 {kVarInt62TwoBytes + 0x00, 0x0f}}
11595 },
11596 {
11597 // type (short header, 4 byte packet number)
11598 {"",
11599 {0x43}},
11600 // connection_id
11601 {"",
11602 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
11603 // packet number
11604 {"",
11605 {0x12, 0x34, 0x9A, 0xBC}},
11606 // frame type (two-byte encoding)
11607 {"",
11608 {kVarInt62TwoBytes + 0x00, 0x10}}
11609 },
11610 {
11611 // type (short header, 4 byte packet number)
11612 {"",
11613 {0x43}},
11614 // connection_id
11615 {"",
11616 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
11617 // packet number
11618 {"",
11619 {0x12, 0x34, 0x9A, 0xBC}},
11620 // frame type (two-byte encoding)
11621 {"",
11622 {kVarInt62TwoBytes + 0x00, 0x11}}
11623 },
11624 {
11625 // type (short header, 4 byte packet number)
11626 {"",
11627 {0x43}},
11628 // connection_id
11629 {"",
11630 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
11631 // packet number
11632 {"",
11633 {0x12, 0x34, 0x9A, 0xBC}},
11634 // frame type (two-byte encoding)
11635 {"",
11636 {kVarInt62TwoBytes + 0x00, 0x12}}
11637 },
11638 {
11639 // type (short header, 4 byte packet number)
11640 {"",
11641 {0x43}},
11642 // connection_id
11643 {"",
11644 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
11645 // packet number
11646 {"",
11647 {0x12, 0x34, 0x9A, 0xBC}},
11648 // frame type (two-byte encoding)
11649 {"",
11650 {kVarInt62TwoBytes + 0x00, 0x13}}
11651 },
11652 {
11653 // type (short header, 4 byte packet number)
11654 {"",
11655 {0x43}},
11656 // connection_id
11657 {"",
11658 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
11659 // packet number
11660 {"",
11661 {0x12, 0x34, 0x9A, 0xBC}},
11662 // frame type (two-byte encoding)
11663 {"",
11664 {kVarInt62TwoBytes + 0x00, 0x14}}
11665 },
11666 {
11667 // type (short header, 4 byte packet number)
11668 {"",
11669 {0x43}},
11670 // connection_id
11671 {"",
11672 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
11673 // packet number
11674 {"",
11675 {0x12, 0x34, 0x9A, 0xBC}},
11676 // frame type (two-byte encoding)
11677 {"",
11678 {kVarInt62TwoBytes + 0x00, 0x15}}
11679 },
11680 {
11681 // type (short header, 4 byte packet number)
11682 {"",
11683 {0x43}},
11684 // connection_id
11685 {"",
11686 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
11687 // packet number
11688 {"",
11689 {0x12, 0x34, 0x9A, 0xBC}},
11690 // frame type (two-byte encoding)
11691 {"",
11692 {kVarInt62TwoBytes + 0x00, 0x16}}
11693 },
11694 {
11695 // type (short header, 4 byte packet number)
11696 {"",
11697 {0x43}},
11698 // connection_id
11699 {"",
11700 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
11701 // packet number
11702 {"",
11703 {0x12, 0x34, 0x9A, 0xBC}},
11704 // frame type (two-byte encoding)
11705 {"",
11706 {kVarInt62TwoBytes + 0x00, 0x17}}
11707 },
11708 {
11709 // type (short header, 4 byte packet number)
11710 {"",
11711 {0x43}},
11712 // connection_id
11713 {"",
11714 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
11715 // packet number
11716 {"",
11717 {0x12, 0x34, 0x9A, 0xBC}},
11718 // frame type (two-byte encoding)
11719 {"",
11720 {kVarInt62TwoBytes + 0x00, 0x18}}
11721 },
11722 {
11723 // type (short header, 4 byte packet number)
11724 {"",
11725 {0x43}},
11726 // connection_id
11727 {"",
11728 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
11729 // packet number
11730 {"",
11731 {0x12, 0x34, 0x9A, 0xBC}},
11732 // frame type (two-byte encoding)
11733 {"",
11734 {kVarInt62TwoBytes + 0x00, 0x20}}
11735 },
11736 {
11737 // type (short header, 4 byte packet number)
11738 {"",
11739 {0x43}},
11740 // connection_id
11741 {"",
11742 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
11743 // packet number
11744 {"",
11745 {0x12, 0x34, 0x9A, 0xBC}},
11746 // frame type (two-byte encoding)
11747 {"",
11748 {kVarInt62TwoBytes + 0x00, 0x21}}
11749 },
11750 };
11751 // clang-format on
11752
11753 for (PacketFragments& packet : packets) {
11754 std::unique_ptr<QuicEncryptedPacket> encrypted(
11755 AssemblePacketFromFragments(packet));
11756
11757 EXPECT_FALSE(framer_.ProcessPacket(*encrypted));
11758
bncf54082a2019-11-27 10:19:47 -080011759 EXPECT_THAT(framer_.error(), IsError(IETF_QUIC_PROTOCOL_VIOLATION));
QUICHE teama6ef0a62019-03-07 20:34:33 -050011760 EXPECT_EQ("Frame type not minimally encoded.", framer_.detailed_error());
11761 }
11762}
11763
11764TEST_P(QuicFramerTest, RetireConnectionIdFrame) {
fkastenholz305e1732019-06-18 05:01:22 -070011765 if (!VersionHasIetfQuicFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -050011766 // This frame is only for version 99.
11767 return;
11768 }
zhongyi546cc452019-04-12 15:27:49 -070011769 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
QUICHE teama6ef0a62019-03-07 20:34:33 -050011770 // clang-format off
11771 PacketFragments packet99 = {
11772 // type (short header, 4 byte packet number)
11773 {"",
11774 {0x43}},
11775 // connection_id
11776 {"",
11777 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
11778 // packet number
11779 {"",
11780 {0x12, 0x34, 0x56, 0x78}},
11781 // frame type (IETF_RETIRE_CONNECTION_ID frame)
11782 {"",
11783 {0x19}},
11784 // Sequence number
11785 {"Unable to read retire connection ID frame sequence number.",
11786 {kVarInt62TwoBytes + 0x11, 0x22}}
11787 };
11788 // clang-format on
11789
11790 std::unique_ptr<QuicEncryptedPacket> encrypted(
11791 AssemblePacketFromFragments(packet99));
11792 EXPECT_TRUE(framer_.ProcessPacket(*encrypted));
11793
bncf6f82b12019-10-30 07:01:01 -070011794 EXPECT_THAT(framer_.error(), IsQuicNoError());
QUICHE teama6ef0a62019-03-07 20:34:33 -050011795 ASSERT_TRUE(visitor_.header_.get());
11796 EXPECT_TRUE(CheckDecryption(
11797 *encrypted, !kIncludeVersion, !kIncludeDiversificationNonce,
11798 PACKET_8BYTE_CONNECTION_ID, PACKET_0BYTE_CONNECTION_ID));
11799
11800 EXPECT_EQ(0u, visitor_.stream_frames_.size());
11801
11802 EXPECT_EQ(0x1122u, visitor_.retire_connection_id_.sequence_number);
11803
11804 ASSERT_EQ(0u, visitor_.ack_frames_.size());
11805
11806 CheckFramingBoundaries(packet99, QUIC_INVALID_RETIRE_CONNECTION_ID_DATA);
11807}
11808
11809TEST_P(QuicFramerTest, BuildRetireConnectionIdFramePacket) {
fkastenholz305e1732019-06-18 05:01:22 -070011810 if (!VersionHasIetfQuicFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -050011811 // This frame is only for version 99.
11812 return;
11813 }
QUICHE team2252b702019-05-14 23:55:14 -040011814 QuicFramerPeer::SetPerspective(&framer_, Perspective::IS_CLIENT);
QUICHE teama6ef0a62019-03-07 20:34:33 -050011815 QuicPacketHeader header;
11816 header.destination_connection_id = FramerTestConnectionId();
11817 header.reset_flag = false;
11818 header.version_flag = false;
11819 header.packet_number = kPacketNumber;
11820
11821 QuicRetireConnectionIdFrame frame;
11822 frame.sequence_number = 0x1122;
11823
11824 QuicFrames frames = {QuicFrame(&frame)};
11825
11826 // clang-format off
11827 unsigned char packet99[] = {
11828 // type (short header, 4 byte packet number)
11829 0x43,
11830 // connection_id
11831 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
11832 // packet number
11833 0x12, 0x34, 0x56, 0x78,
11834
11835 // frame type (IETF_RETIRE_CONNECTION_ID frame)
11836 0x19,
11837 // sequence number
11838 kVarInt62TwoBytes + 0x11, 0x22
11839 };
11840 // clang-format on
11841
11842 std::unique_ptr<QuicPacket> data(BuildDataPacket(header, frames));
11843 ASSERT_TRUE(data != nullptr);
11844
dmcardle8f7df532020-01-07 13:28:57 -080011845 quiche::test::CompareCharArraysWithHexError(
11846 "constructed packet", data->data(), data->length(), AsChars(packet99),
11847 QUICHE_ARRAYSIZE(packet99));
QUICHE teama6ef0a62019-03-07 20:34:33 -050011848}
11849
11850TEST_P(QuicFramerTest, AckFrameWithInvalidLargestObserved) {
zhongyi546cc452019-04-12 15:27:49 -070011851 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
QUICHE teama6ef0a62019-03-07 20:34:33 -050011852 // clang-format off
11853 unsigned char packet[] = {
11854 // public flags (8 byte connection_id)
11855 0x2C,
11856 // connection_id
11857 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
11858 // packet number
11859 0x12, 0x34, 0x56, 0x78,
11860
11861 // frame type (ack frame)
11862 0x45,
11863 // largest observed
11864 0x00, 0x00,
11865 // Zero delta time.
11866 0x00, 0x00,
11867 // first ack block length.
11868 0x00, 0x00,
11869 // num timestamps.
11870 0x00
11871 };
11872
QUICHE teama6ef0a62019-03-07 20:34:33 -050011873 unsigned char packet46[] = {
11874 // type (short header, 4 byte packet number)
11875 0x43,
11876 // connection_id
11877 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
11878 // packet number
11879 0x12, 0x34, 0x56, 0x78,
11880
11881 // frame type (ack frame)
11882 0x45,
11883 // largest observed
11884 0x00, 0x00,
11885 // Zero delta time.
11886 0x00, 0x00,
11887 // first ack block length.
11888 0x00, 0x00,
11889 // num timestamps.
11890 0x00
11891 };
11892
11893 unsigned char packet99[] = {
11894 // type (short header, 4 byte packet number)
11895 0x43,
11896 // connection_id
11897 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
11898 // packet number
11899 0x12, 0x34, 0x56, 0x78,
11900
11901 // frame type (IETF_ACK frame)
11902 0x02,
11903 // Largest acked
11904 kVarInt62OneByte + 0x00,
11905 // Zero delta time.
11906 kVarInt62OneByte + 0x00,
11907 // Ack block count 0
11908 kVarInt62OneByte + 0x00,
11909 // First ack block length
11910 kVarInt62OneByte + 0x00,
11911 };
11912 // clang-format on
11913
11914 unsigned char* p = packet;
bnc4e9283d2019-12-17 07:08:57 -080011915 size_t p_size = QUICHE_ARRAYSIZE(packet);
fkastenholz305e1732019-06-18 05:01:22 -070011916 if (VersionHasIetfQuicFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -050011917 p = packet99;
bnc4e9283d2019-12-17 07:08:57 -080011918 p_size = QUICHE_ARRAYSIZE(packet99);
fayang36825da2019-08-21 14:01:27 -070011919 } else if (framer_.transport_version() >= QUIC_VERSION_46) {
fayang374888f2019-05-31 06:47:21 -070011920 p = packet46;
QUICHE teama6ef0a62019-03-07 20:34:33 -050011921 }
11922
11923 QuicEncryptedPacket encrypted(AsChars(p), p_size, false);
11924 EXPECT_FALSE(framer_.ProcessPacket(encrypted));
11925 EXPECT_EQ(framer_.detailed_error(), "Largest acked is 0.");
11926}
11927
11928TEST_P(QuicFramerTest, FirstAckBlockJustUnderFlow) {
zhongyi546cc452019-04-12 15:27:49 -070011929 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
QUICHE teama6ef0a62019-03-07 20:34:33 -050011930 // clang-format off
11931 unsigned char packet[] = {
11932 // public flags (8 byte connection_id)
11933 0x2C,
11934 // connection_id
11935 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
11936 // packet number
11937 0x12, 0x34, 0x56, 0x78,
11938
11939 // frame type (ack frame)
11940 0x45,
11941 // largest observed
11942 0x00, 0x02,
11943 // Zero delta time.
11944 0x00, 0x00,
11945 // first ack block length.
11946 0x00, 0x03,
11947 // num timestamps.
11948 0x00
11949 };
11950
QUICHE teama6ef0a62019-03-07 20:34:33 -050011951 unsigned char packet46[] = {
11952 // type (short header, 4 byte packet number)
11953 0x43,
11954 // connection_id
11955 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
11956 // packet number
11957 0x12, 0x34, 0x56, 0x78,
11958
11959 // frame type (ack frame)
11960 0x45,
11961 // largest observed
11962 0x00, 0x02,
11963 // Zero delta time.
11964 0x00, 0x00,
11965 // first ack block length.
11966 0x00, 0x03,
11967 // num timestamps.
11968 0x00
11969 };
11970
11971 unsigned char packet99[] = {
11972 // type (short header, 4 byte packet number)
11973 0x43,
11974 // connection_id
11975 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
11976 // packet number
11977 0x12, 0x34, 0x56, 0x78,
11978
11979 // frame type (IETF_ACK frame)
11980 0x02,
11981 // Largest acked
11982 kVarInt62OneByte + 0x02,
11983 // Zero delta time.
11984 kVarInt62OneByte + 0x00,
11985 // Ack block count 0
11986 kVarInt62OneByte + 0x00,
11987 // First ack block length
11988 kVarInt62OneByte + 0x02,
11989 };
11990 // clang-format on
11991
11992 unsigned char* p = packet;
bnc4e9283d2019-12-17 07:08:57 -080011993 size_t p_size = QUICHE_ARRAYSIZE(packet);
fkastenholz305e1732019-06-18 05:01:22 -070011994 if (VersionHasIetfQuicFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -050011995 p = packet99;
bnc4e9283d2019-12-17 07:08:57 -080011996 p_size = QUICHE_ARRAYSIZE(packet99);
fayang36825da2019-08-21 14:01:27 -070011997 } else if (framer_.transport_version() >= QUIC_VERSION_46) {
QUICHE teama6ef0a62019-03-07 20:34:33 -050011998 p = packet46;
bnc4e9283d2019-12-17 07:08:57 -080011999 p_size = QUICHE_ARRAYSIZE(packet46);
QUICHE teama6ef0a62019-03-07 20:34:33 -050012000 }
12001
12002 QuicEncryptedPacket encrypted(AsChars(p), p_size, false);
12003 EXPECT_FALSE(framer_.ProcessPacket(encrypted));
12004 EXPECT_EQ(framer_.detailed_error(),
12005 "Underflow with first ack block length 3 largest acked is 2.");
12006}
12007
12008TEST_P(QuicFramerTest, ThirdAckBlockJustUnderflow) {
zhongyi546cc452019-04-12 15:27:49 -070012009 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
QUICHE teama6ef0a62019-03-07 20:34:33 -050012010 // clang-format off
12011 unsigned char packet[] = {
12012 // public flags (8 byte connection_id)
12013 0x2C,
12014 // connection_id
12015 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
12016 // packet number
12017 0x12, 0x34, 0x56, 0x78,
12018
12019 // frame type (ack frame)
12020 0x60,
12021 // largest observed
12022 0x0A,
12023 // Zero delta time.
12024 0x00, 0x00,
12025 // Num of ack blocks
12026 0x02,
12027 // first ack block length.
12028 0x02,
12029 // gap to next block
12030 0x01,
12031 // ack block length
12032 0x01,
12033 // gap to next block
12034 0x01,
12035 // ack block length
12036 0x06,
12037 // num timestamps.
12038 0x00
12039 };
12040
QUICHE teama6ef0a62019-03-07 20:34:33 -050012041 unsigned char packet46[] = {
12042 // type (short header, 4 byte packet number)
12043 0x43,
12044 // connection_id
12045 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
12046 // packet number
12047 0x12, 0x34, 0x56, 0x78,
12048
12049 // frame type (ack frame)
12050 0x60,
12051 // largest observed
12052 0x0A,
12053 // Zero delta time.
12054 0x00, 0x00,
12055 // Num of ack blocks
12056 0x02,
12057 // first ack block length.
12058 0x02,
12059 // gap to next block
12060 0x01,
12061 // ack block length
12062 0x01,
12063 // gap to next block
12064 0x01,
12065 // ack block length
12066 0x06,
12067 // num timestamps.
12068 0x00
12069 };
12070
12071 unsigned char packet99[] = {
12072 // type (short header, 4 byte packet number)
12073 0x43,
12074 // connection_id
12075 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
12076 // packet number
12077 0x12, 0x34, 0x56, 0x78,
12078
12079 // frame type (IETF_ACK frame)
12080 0x02,
12081 // Largest acked
12082 kVarInt62OneByte + 0x0A,
12083 // Zero delta time.
12084 kVarInt62OneByte + 0x00,
12085 // Ack block count 2
12086 kVarInt62OneByte + 0x02,
12087 // First ack block length
12088 kVarInt62OneByte + 0x01,
12089 // gap to next block length
12090 kVarInt62OneByte + 0x00,
12091 // ack block length
12092 kVarInt62OneByte + 0x00,
12093 // gap to next block length
12094 kVarInt62OneByte + 0x00,
12095 // ack block length
12096 kVarInt62OneByte + 0x05,
12097 };
12098 // clang-format on
12099
12100 unsigned char* p = packet;
bnc4e9283d2019-12-17 07:08:57 -080012101 size_t p_size = QUICHE_ARRAYSIZE(packet);
fkastenholz305e1732019-06-18 05:01:22 -070012102 if (VersionHasIetfQuicFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -050012103 p = packet99;
bnc4e9283d2019-12-17 07:08:57 -080012104 p_size = QUICHE_ARRAYSIZE(packet99);
fayang36825da2019-08-21 14:01:27 -070012105 } else if (framer_.transport_version() >= QUIC_VERSION_46) {
QUICHE teama6ef0a62019-03-07 20:34:33 -050012106 p = packet46;
bnc4e9283d2019-12-17 07:08:57 -080012107 p_size = QUICHE_ARRAYSIZE(packet46);
QUICHE teama6ef0a62019-03-07 20:34:33 -050012108 }
12109
12110 QuicEncryptedPacket encrypted(AsChars(p), p_size, false);
12111 EXPECT_FALSE(framer_.ProcessPacket(encrypted));
fkastenholz305e1732019-06-18 05:01:22 -070012112 if (VersionHasIetfQuicFrames(framer_.transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -050012113 EXPECT_EQ(framer_.detailed_error(),
12114 "Underflow with ack block length 6 latest ack block end is 5.");
12115 } else {
12116 EXPECT_EQ(framer_.detailed_error(),
12117 "Underflow with ack block length 6, end of block is 6.");
12118 }
12119}
12120
12121TEST_P(QuicFramerTest, CoalescedPacket) {
12122 if (!QuicVersionHasLongHeaderLengths(framer_.transport_version())) {
12123 return;
12124 }
zhongyi546cc452019-04-12 15:27:49 -070012125 SetDecrypterLevel(ENCRYPTION_ZERO_RTT);
QUICHE teama6ef0a62019-03-07 20:34:33 -050012126 // clang-format off
12127 unsigned char packet[] = {
12128 // first coalesced packet
12129 // public flags (long header with packet type ZERO_RTT_PROTECTED and
12130 // 4-byte packet number)
12131 0xD3,
12132 // version
12133 QUIC_VERSION_BYTES,
12134 // destination connection ID length
dschinazi48ac9192019-07-31 00:07:26 -070012135 0x08,
QUICHE teama6ef0a62019-03-07 20:34:33 -050012136 // destination connection ID
12137 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
dschinazi48ac9192019-07-31 00:07:26 -070012138 // source connection ID length
12139 0x00,
QUICHE teama6ef0a62019-03-07 20:34:33 -050012140 // long header packet length
12141 0x1E,
12142 // packet number
12143 0x12, 0x34, 0x56, 0x78,
dschinazic73506e2019-09-20 13:26:46 -070012144 // frame type (stream frame with fin)
12145 0xFE,
12146 // stream id
12147 0x02, 0x03, 0x04,
12148 // offset
12149 0x3A, 0x98, 0xFE, 0xDC, 0x32, 0x10, 0x76, 0x54,
12150 // data length
12151 0x00, 0x0c,
12152 // data
12153 'h', 'e', 'l', 'l',
12154 'o', ' ', 'w', 'o',
12155 'r', 'l', 'd', '!',
12156 // second coalesced packet
12157 // public flags (long header with packet type ZERO_RTT_PROTECTED and
12158 // 4-byte packet number)
12159 0xD3,
12160 // version
12161 QUIC_VERSION_BYTES,
12162 // destination connection ID length
12163 0x08,
12164 // destination connection ID
12165 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
12166 // source connection ID length
12167 0x00,
12168 // long header packet length
12169 0x1E,
12170 // packet number
12171 0x12, 0x34, 0x56, 0x79,
12172 // frame type (stream frame with fin)
12173 0xFE,
12174 // stream id
12175 0x02, 0x03, 0x04,
12176 // offset
12177 0x3A, 0x98, 0xFE, 0xDC, 0x32, 0x10, 0x76, 0x54,
12178 // data length
12179 0x00, 0x0c,
12180 // data
12181 'H', 'E', 'L', 'L',
12182 'O', '_', 'W', 'O',
12183 'R', 'L', 'D', '?',
12184 };
12185 unsigned char packet99[] = {
12186 // first coalesced packet
12187 // public flags (long header with packet type ZERO_RTT_PROTECTED and
12188 // 4-byte packet number)
12189 0xD3,
12190 // version
12191 QUIC_VERSION_BYTES,
12192 // destination connection ID length
12193 0x08,
12194 // destination connection ID
12195 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
12196 // source connection ID length
12197 0x00,
12198 // long header packet length
12199 0x1E,
12200 // packet number
12201 0x12, 0x34, 0x56, 0x78,
QUICHE teama6ef0a62019-03-07 20:34:33 -050012202 // frame type (IETF_STREAM frame with FIN, LEN, and OFFSET bits set)
12203 0x08 | 0x01 | 0x02 | 0x04,
12204 // stream id
12205 kVarInt62FourBytes + 0x00, 0x02, 0x03, 0x04,
12206 // offset
12207 kVarInt62EightBytes + 0x3A, 0x98, 0xFE, 0xDC,
12208 0x32, 0x10, 0x76, 0x54,
12209 // data length
12210 kVarInt62OneByte + 0x0c,
12211 // data
12212 'h', 'e', 'l', 'l',
12213 'o', ' ', 'w', 'o',
12214 'r', 'l', 'd', '!',
12215 // second coalesced packet
12216 // public flags (long header with packet type ZERO_RTT_PROTECTED and
12217 // 4-byte packet number)
12218 0xD3,
12219 // version
12220 QUIC_VERSION_BYTES,
12221 // destination connection ID length
dschinazi48ac9192019-07-31 00:07:26 -070012222 0x08,
QUICHE teama6ef0a62019-03-07 20:34:33 -050012223 // destination connection ID
12224 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
dschinazi48ac9192019-07-31 00:07:26 -070012225 // source connection ID length
12226 0x00,
QUICHE teama6ef0a62019-03-07 20:34:33 -050012227 // long header packet length
12228 0x1E,
12229 // packet number
12230 0x12, 0x34, 0x56, 0x79,
12231 // frame type (IETF_STREAM frame with FIN, LEN, and OFFSET bits set)
12232 0x08 | 0x01 | 0x02 | 0x04,
12233 // stream id
12234 kVarInt62FourBytes + 0x00, 0x02, 0x03, 0x04,
12235 // offset
12236 kVarInt62EightBytes + 0x3A, 0x98, 0xFE, 0xDC,
12237 0x32, 0x10, 0x76, 0x54,
12238 // data length
12239 kVarInt62OneByte + 0x0c,
12240 // data
12241 'H', 'E', 'L', 'L',
12242 'O', '_', 'W', 'O',
12243 'R', 'L', 'D', '?',
12244 };
12245 // clang-format on
12246
dschinazic73506e2019-09-20 13:26:46 -070012247 unsigned char* p = packet;
bnc4e9283d2019-12-17 07:08:57 -080012248 size_t p_length = QUICHE_ARRAYSIZE(packet);
dschinazic73506e2019-09-20 13:26:46 -070012249 if (framer_.transport_version() == QUIC_VERSION_99) {
12250 p = packet99;
bnc4e9283d2019-12-17 07:08:57 -080012251 p_length = QUICHE_ARRAYSIZE(packet99);
dschinazic73506e2019-09-20 13:26:46 -070012252 }
12253
12254 QuicEncryptedPacket encrypted(AsChars(p), p_length, false);
QUICHE teama6ef0a62019-03-07 20:34:33 -050012255 EXPECT_TRUE(framer_.ProcessPacket(encrypted));
12256
bncf6f82b12019-10-30 07:01:01 -070012257 EXPECT_THAT(framer_.error(), IsQuicNoError());
QUICHE teama6ef0a62019-03-07 20:34:33 -050012258 ASSERT_TRUE(visitor_.header_.get());
12259
12260 ASSERT_EQ(1u, visitor_.stream_frames_.size());
12261 EXPECT_EQ(0u, visitor_.ack_frames_.size());
12262
12263 // Stream ID should be the last 3 bytes of kStreamId.
12264 EXPECT_EQ(0x00FFFFFF & kStreamId, visitor_.stream_frames_[0]->stream_id);
12265 EXPECT_TRUE(visitor_.stream_frames_[0]->fin);
12266 EXPECT_EQ(kStreamOffset, visitor_.stream_frames_[0]->offset);
12267 CheckStreamFrameData("hello world!", visitor_.stream_frames_[0].get());
12268
12269 ASSERT_EQ(visitor_.coalesced_packets_.size(), 1u);
12270 EXPECT_TRUE(framer_.ProcessPacket(*visitor_.coalesced_packets_[0].get()));
12271
bncf6f82b12019-10-30 07:01:01 -070012272 EXPECT_THAT(framer_.error(), IsQuicNoError());
QUICHE teama6ef0a62019-03-07 20:34:33 -050012273 ASSERT_TRUE(visitor_.header_.get());
12274
12275 ASSERT_EQ(2u, visitor_.stream_frames_.size());
12276 EXPECT_EQ(0u, visitor_.ack_frames_.size());
12277
12278 // Stream ID should be the last 3 bytes of kStreamId.
12279 EXPECT_EQ(0x00FFFFFF & kStreamId, visitor_.stream_frames_[1]->stream_id);
12280 EXPECT_TRUE(visitor_.stream_frames_[1]->fin);
12281 EXPECT_EQ(kStreamOffset, visitor_.stream_frames_[1]->offset);
12282 CheckStreamFrameData("HELLO_WORLD?", visitor_.stream_frames_[1].get());
12283}
12284
dschinazi4b5a68a2019-08-15 15:45:36 -070012285TEST_P(QuicFramerTest, UndecryptablePacketWithoutDecrypter) {
12286 QuicFramerPeer::SetPerspective(&framer_, Perspective::IS_CLIENT);
12287
12288 if (!framer_.version().KnowsWhichDecrypterToUse()) {
12289 // We create a bad client decrypter by using initial encryption with a
12290 // bogus connection ID; it should fail to decrypt everything.
12291 QuicConnectionId bogus_connection_id = TestConnectionId(0xbad);
12292 CrypterPair bogus_crypters;
nharperc8d9e402019-09-12 18:30:14 -070012293 CryptoUtils::CreateInitialObfuscators(Perspective::IS_CLIENT,
12294 framer_.version(),
dschinazi4b5a68a2019-08-15 15:45:36 -070012295 bogus_connection_id, &bogus_crypters);
12296 // This removes all other decrypters.
12297 framer_.SetDecrypter(ENCRYPTION_FORWARD_SECURE,
12298 std::move(bogus_crypters.decrypter));
12299 }
12300
dschinazi4b5a68a2019-08-15 15:45:36 -070012301 // clang-format off
12302 unsigned char packet[] = {
12303 // public flags (version included, 8-byte connection ID,
12304 // 4-byte packet number)
12305 0x28,
12306 // connection_id
12307 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
12308 // packet number
12309 0x12, 0x34, 0x56, 0x00,
12310 // padding frames
12311 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
12312 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
12313 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
12314 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
12315 };
fayang36825da2019-08-21 14:01:27 -070012316 unsigned char packet46[] = {
dschinazi4b5a68a2019-08-15 15:45:36 -070012317 // public flags (long header with packet type HANDSHAKE and
12318 // 4-byte packet number)
fayang36825da2019-08-21 14:01:27 -070012319 0xE3,
dschinazi4b5a68a2019-08-15 15:45:36 -070012320 // version
12321 QUIC_VERSION_BYTES,
12322 // connection ID lengths
12323 0x05,
12324 // source connection ID
12325 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
12326 // long header packet length
12327 0x05,
12328 // packet number
12329 0x12, 0x34, 0x56, 0x00,
12330 // padding frames
12331 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
12332 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
12333 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
12334 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
12335 };
dschinazic73506e2019-09-20 13:26:46 -070012336 unsigned char packet49[] = {
dschinazi4b5a68a2019-08-15 15:45:36 -070012337 // public flags (long header with packet type HANDSHAKE and
12338 // 4-byte packet number)
12339 0xE3,
12340 // version
12341 QUIC_VERSION_BYTES,
12342 // destination connection ID length
12343 0x00,
12344 // source connection ID length
12345 0x08,
12346 // source connection ID
12347 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
12348 // long header packet length
12349 0x24,
12350 // packet number
12351 0x12, 0x34, 0x56, 0x00,
12352 // padding frames
12353 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
12354 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
12355 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
12356 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
12357 };
12358 // clang-format on
12359 unsigned char* p = packet;
bnc4e9283d2019-12-17 07:08:57 -080012360 size_t p_length = QUICHE_ARRAYSIZE(packet);
dschinazic73506e2019-09-20 13:26:46 -070012361 if (framer_.transport_version() >= QUIC_VERSION_49) {
12362 p = packet49;
bnc4e9283d2019-12-17 07:08:57 -080012363 p_length = QUICHE_ARRAYSIZE(packet49);
fayang36825da2019-08-21 14:01:27 -070012364 } else if (framer_.transport_version() >= QUIC_VERSION_46) {
12365 p = packet46;
bnc4e9283d2019-12-17 07:08:57 -080012366 p_length = QUICHE_ARRAYSIZE(packet46);
dschinazi4b5a68a2019-08-15 15:45:36 -070012367 }
12368 // First attempt decryption without the handshake crypter.
12369 EXPECT_FALSE(
12370 framer_.ProcessPacket(QuicEncryptedPacket(AsChars(p), p_length, false)));
bncf6f82b12019-10-30 07:01:01 -070012371 EXPECT_THAT(framer_.error(), IsError(QUIC_DECRYPTION_FAILURE));
12372 ASSERT_EQ(1u, visitor_.undecryptable_packets_.size());
12373 ASSERT_EQ(1u, visitor_.undecryptable_decryption_levels_.size());
12374 ASSERT_EQ(1u, visitor_.undecryptable_has_decryption_keys_.size());
dmcardle8f7df532020-01-07 13:28:57 -080012375 quiche::test::CompareCharArraysWithHexError(
bncf6f82b12019-10-30 07:01:01 -070012376 "undecryptable packet", visitor_.undecryptable_packets_[0]->data(),
12377 visitor_.undecryptable_packets_[0]->length(), AsChars(p), p_length);
12378 if (framer_.version().KnowsWhichDecrypterToUse()) {
12379 EXPECT_EQ(ENCRYPTION_HANDSHAKE,
12380 visitor_.undecryptable_decryption_levels_[0]);
12381 }
12382 EXPECT_FALSE(visitor_.undecryptable_has_decryption_keys_[0]);
dschinazi4b5a68a2019-08-15 15:45:36 -070012383}
12384
12385TEST_P(QuicFramerTest, UndecryptablePacketWithDecrypter) {
12386 QuicFramerPeer::SetPerspective(&framer_, Perspective::IS_CLIENT);
12387
12388 // We create a bad client decrypter by using initial encryption with a
12389 // bogus connection ID; it should fail to decrypt everything.
12390 QuicConnectionId bogus_connection_id = TestConnectionId(0xbad);
12391 CrypterPair bad_handshake_crypters;
nharperc8d9e402019-09-12 18:30:14 -070012392 CryptoUtils::CreateInitialObfuscators(Perspective::IS_CLIENT,
12393 framer_.version(), bogus_connection_id,
12394 &bad_handshake_crypters);
dschinazi4b5a68a2019-08-15 15:45:36 -070012395 if (framer_.version().KnowsWhichDecrypterToUse()) {
12396 framer_.InstallDecrypter(ENCRYPTION_HANDSHAKE,
12397 std::move(bad_handshake_crypters.decrypter));
12398 } else {
12399 framer_.SetDecrypter(ENCRYPTION_HANDSHAKE,
12400 std::move(bad_handshake_crypters.decrypter));
12401 }
12402
dschinazi4b5a68a2019-08-15 15:45:36 -070012403 // clang-format off
12404 unsigned char packet[] = {
12405 // public flags (version included, 8-byte connection ID,
12406 // 4-byte packet number)
12407 0x28,
12408 // connection_id
12409 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
12410 // packet number
12411 0x12, 0x34, 0x56, 0x00,
12412 // padding frames
12413 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
12414 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
12415 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
12416 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
12417 };
fayang36825da2019-08-21 14:01:27 -070012418 unsigned char packet46[] = {
dschinazi4b5a68a2019-08-15 15:45:36 -070012419 // public flags (long header with packet type HANDSHAKE and
12420 // 4-byte packet number)
fayang36825da2019-08-21 14:01:27 -070012421 0xE3,
dschinazi4b5a68a2019-08-15 15:45:36 -070012422 // version
12423 QUIC_VERSION_BYTES,
12424 // connection ID lengths
12425 0x05,
12426 // source connection ID
12427 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
12428 // long header packet length
12429 0x05,
12430 // packet number
12431 0x12, 0x34, 0x56, 0x00,
12432 // padding frames
12433 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
12434 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
12435 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
12436 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
12437 };
dschinazic73506e2019-09-20 13:26:46 -070012438 unsigned char packet49[] = {
dschinazi4b5a68a2019-08-15 15:45:36 -070012439 // public flags (long header with packet type HANDSHAKE and
12440 // 4-byte packet number)
12441 0xE3,
12442 // version
12443 QUIC_VERSION_BYTES,
12444 // destination connection ID length
12445 0x00,
12446 // source connection ID length
12447 0x08,
12448 // source connection ID
12449 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
12450 // long header packet length
12451 0x24,
12452 // packet number
12453 0x12, 0x34, 0x56, 0x00,
12454 // padding frames
12455 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
12456 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
12457 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
12458 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
12459 };
12460 // clang-format on
12461 unsigned char* p = packet;
bnc4e9283d2019-12-17 07:08:57 -080012462 size_t p_length = QUICHE_ARRAYSIZE(packet);
dschinazic73506e2019-09-20 13:26:46 -070012463 if (framer_.transport_version() >= QUIC_VERSION_49) {
12464 p = packet49;
bnc4e9283d2019-12-17 07:08:57 -080012465 p_length = QUICHE_ARRAYSIZE(packet49);
fayang36825da2019-08-21 14:01:27 -070012466 } else if (framer_.transport_version() >= QUIC_VERSION_46) {
12467 p = packet46;
bnc4e9283d2019-12-17 07:08:57 -080012468 p_length = QUICHE_ARRAYSIZE(packet46);
dschinazi4b5a68a2019-08-15 15:45:36 -070012469 }
12470
12471 EXPECT_FALSE(
12472 framer_.ProcessPacket(QuicEncryptedPacket(AsChars(p), p_length, false)));
bncf6f82b12019-10-30 07:01:01 -070012473 EXPECT_THAT(framer_.error(), IsError(QUIC_DECRYPTION_FAILURE));
12474 ASSERT_EQ(1u, visitor_.undecryptable_packets_.size());
12475 ASSERT_EQ(1u, visitor_.undecryptable_decryption_levels_.size());
12476 ASSERT_EQ(1u, visitor_.undecryptable_has_decryption_keys_.size());
dmcardle8f7df532020-01-07 13:28:57 -080012477 quiche::test::CompareCharArraysWithHexError(
bncf6f82b12019-10-30 07:01:01 -070012478 "undecryptable packet", visitor_.undecryptable_packets_[0]->data(),
12479 visitor_.undecryptable_packets_[0]->length(), AsChars(p), p_length);
12480 if (framer_.version().KnowsWhichDecrypterToUse()) {
12481 EXPECT_EQ(ENCRYPTION_HANDSHAKE,
12482 visitor_.undecryptable_decryption_levels_[0]);
12483 }
12484 EXPECT_EQ(framer_.version().KnowsWhichDecrypterToUse(),
12485 visitor_.undecryptable_has_decryption_keys_[0]);
dschinazi4b5a68a2019-08-15 15:45:36 -070012486}
12487
12488TEST_P(QuicFramerTest, UndecryptableCoalescedPacket) {
12489 if (!QuicVersionHasLongHeaderLengths(framer_.transport_version())) {
12490 return;
12491 }
12492 ASSERT_TRUE(framer_.version().KnowsWhichDecrypterToUse());
12493 SetDecrypterLevel(ENCRYPTION_ZERO_RTT);
12494 // We create a bad client decrypter by using initial encryption with a
12495 // bogus connection ID; it should fail to decrypt everything.
12496 QuicConnectionId bogus_connection_id = TestConnectionId(0xbad);
12497 CrypterPair bad_handshake_crypters;
nharperc8d9e402019-09-12 18:30:14 -070012498 CryptoUtils::CreateInitialObfuscators(Perspective::IS_CLIENT,
12499 framer_.version(), bogus_connection_id,
12500 &bad_handshake_crypters);
dschinazi4b5a68a2019-08-15 15:45:36 -070012501 framer_.InstallDecrypter(ENCRYPTION_HANDSHAKE,
12502 std::move(bad_handshake_crypters.decrypter));
12503 // clang-format off
12504 unsigned char packet[] = {
12505 // first coalesced packet
12506 // public flags (long header with packet type HANDSHAKE and
12507 // 4-byte packet number)
12508 0xE3,
12509 // version
12510 QUIC_VERSION_BYTES,
12511 // destination connection ID length
12512 0x08,
12513 // destination connection ID
12514 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
12515 // source connection ID length
12516 0x00,
12517 // long header packet length
12518 0x1E,
12519 // packet number
12520 0x12, 0x34, 0x56, 0x78,
dschinazic73506e2019-09-20 13:26:46 -070012521 // frame type (stream frame with fin)
12522 0xFE,
12523 // stream id
12524 0x02, 0x03, 0x04,
12525 // offset
12526 0x3A, 0x98, 0xFE, 0xDC, 0x32, 0x10, 0x76, 0x54,
12527 // data length
12528 0x00, 0x0c,
12529 // data
12530 'h', 'e', 'l', 'l',
12531 'o', ' ', 'w', 'o',
12532 'r', 'l', 'd', '!',
12533 // second coalesced packet
12534 // public flags (long header with packet type ZERO_RTT_PROTECTED and
12535 // 4-byte packet number)
12536 0xD3,
12537 // version
12538 QUIC_VERSION_BYTES,
12539 // destination connection ID length
12540 0x08,
12541 // destination connection ID
12542 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
12543 // source connection ID length
12544 0x00,
12545 // long header packet length
12546 0x1E,
12547 // packet number
12548 0x12, 0x34, 0x56, 0x79,
12549 // frame type (stream frame with fin)
12550 0xFE,
12551 // stream id
12552 0x02, 0x03, 0x04,
12553 // offset
12554 0x3A, 0x98, 0xFE, 0xDC, 0x32, 0x10, 0x76, 0x54,
12555 // data length
12556 0x00, 0x0c,
12557 // data
12558 'H', 'E', 'L', 'L',
12559 'O', '_', 'W', 'O',
12560 'R', 'L', 'D', '?',
12561 };
12562 unsigned char packet99[] = {
12563 // first coalesced packet
12564 // public flags (long header with packet type HANDSHAKE and
12565 // 4-byte packet number)
12566 0xE3,
12567 // version
12568 QUIC_VERSION_BYTES,
12569 // destination connection ID length
12570 0x08,
12571 // destination connection ID
12572 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
12573 // source connection ID length
12574 0x00,
12575 // long header packet length
12576 0x1E,
12577 // packet number
12578 0x12, 0x34, 0x56, 0x78,
dschinazi4b5a68a2019-08-15 15:45:36 -070012579 // frame type (IETF_STREAM frame with FIN, LEN, and OFFSET bits set)
12580 0x08 | 0x01 | 0x02 | 0x04,
12581 // stream id
12582 kVarInt62FourBytes + 0x00, 0x02, 0x03, 0x04,
12583 // offset
12584 kVarInt62EightBytes + 0x3A, 0x98, 0xFE, 0xDC,
12585 0x32, 0x10, 0x76, 0x54,
12586 // data length
12587 kVarInt62OneByte + 0x0c,
12588 // data
12589 'h', 'e', 'l', 'l',
12590 'o', ' ', 'w', 'o',
12591 'r', 'l', 'd', '!',
12592 // second coalesced packet
12593 // public flags (long header with packet type ZERO_RTT_PROTECTED and
12594 // 4-byte packet number)
12595 0xD3,
12596 // version
12597 QUIC_VERSION_BYTES,
12598 // destination connection ID length
12599 0x08,
12600 // destination connection ID
12601 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
12602 // source connection ID length
12603 0x00,
12604 // long header packet length
12605 0x1E,
12606 // packet number
12607 0x12, 0x34, 0x56, 0x79,
12608 // frame type (IETF_STREAM frame with FIN, LEN, and OFFSET bits set)
12609 0x08 | 0x01 | 0x02 | 0x04,
12610 // stream id
12611 kVarInt62FourBytes + 0x00, 0x02, 0x03, 0x04,
12612 // offset
12613 kVarInt62EightBytes + 0x3A, 0x98, 0xFE, 0xDC,
12614 0x32, 0x10, 0x76, 0x54,
12615 // data length
12616 kVarInt62OneByte + 0x0c,
12617 // data
12618 'H', 'E', 'L', 'L',
12619 'O', '_', 'W', 'O',
12620 'R', 'L', 'D', '?',
12621 };
12622 // clang-format on
12623 const size_t length_of_first_coalesced_packet = 46;
12624
dschinazic73506e2019-09-20 13:26:46 -070012625 unsigned char* p = packet;
bnc4e9283d2019-12-17 07:08:57 -080012626 size_t p_length = QUICHE_ARRAYSIZE(packet);
dschinazic73506e2019-09-20 13:26:46 -070012627 if (framer_.transport_version() == QUIC_VERSION_99) {
12628 p = packet99;
bnc4e9283d2019-12-17 07:08:57 -080012629 p_length = QUICHE_ARRAYSIZE(packet99);
dschinazic73506e2019-09-20 13:26:46 -070012630 }
12631
12632 QuicEncryptedPacket encrypted(AsChars(p), p_length, false);
12633
dschinazi4b5a68a2019-08-15 15:45:36 -070012634 EXPECT_FALSE(framer_.ProcessPacket(encrypted));
12635
bncf6f82b12019-10-30 07:01:01 -070012636 EXPECT_THAT(framer_.error(), IsError(QUIC_DECRYPTION_FAILURE));
dschinazi4b5a68a2019-08-15 15:45:36 -070012637
bncf6f82b12019-10-30 07:01:01 -070012638 ASSERT_EQ(1u, visitor_.undecryptable_packets_.size());
12639 ASSERT_EQ(1u, visitor_.undecryptable_decryption_levels_.size());
12640 ASSERT_EQ(1u, visitor_.undecryptable_has_decryption_keys_.size());
12641 // Make sure we only receive the first undecryptable packet and not the
12642 // full packet including the second coalesced packet.
dmcardle8f7df532020-01-07 13:28:57 -080012643 quiche::test::CompareCharArraysWithHexError(
12644 "undecryptable packet", visitor_.undecryptable_packets_[0]->data(),
12645 visitor_.undecryptable_packets_[0]->length(), AsChars(p),
12646 length_of_first_coalesced_packet);
bncf6f82b12019-10-30 07:01:01 -070012647 EXPECT_EQ(ENCRYPTION_HANDSHAKE, visitor_.undecryptable_decryption_levels_[0]);
12648 EXPECT_TRUE(visitor_.undecryptable_has_decryption_keys_[0]);
dschinazi4b5a68a2019-08-15 15:45:36 -070012649
12650 // Make sure the second coalesced packet is parsed correctly.
12651 ASSERT_EQ(visitor_.coalesced_packets_.size(), 1u);
12652 EXPECT_TRUE(framer_.ProcessPacket(*visitor_.coalesced_packets_[0].get()));
12653
12654 ASSERT_TRUE(visitor_.header_.get());
12655
12656 ASSERT_EQ(1u, visitor_.stream_frames_.size());
12657 EXPECT_EQ(0u, visitor_.ack_frames_.size());
12658
12659 // Stream ID should be the last 3 bytes of kStreamId.
12660 EXPECT_EQ(0x00FFFFFF & kStreamId, visitor_.stream_frames_[0]->stream_id);
12661 EXPECT_TRUE(visitor_.stream_frames_[0]->fin);
12662 EXPECT_EQ(kStreamOffset, visitor_.stream_frames_[0]->offset);
12663 CheckStreamFrameData("HELLO_WORLD?", visitor_.stream_frames_[0].get());
12664}
12665
QUICHE teama6ef0a62019-03-07 20:34:33 -050012666TEST_P(QuicFramerTest, MismatchedCoalescedPacket) {
12667 if (!QuicVersionHasLongHeaderLengths(framer_.transport_version())) {
12668 return;
12669 }
zhongyi546cc452019-04-12 15:27:49 -070012670 SetDecrypterLevel(ENCRYPTION_ZERO_RTT);
QUICHE teama6ef0a62019-03-07 20:34:33 -050012671 // clang-format off
12672 unsigned char packet[] = {
12673 // first coalesced packet
12674 // public flags (long header with packet type ZERO_RTT_PROTECTED and
12675 // 4-byte packet number)
12676 0xD3,
12677 // version
12678 QUIC_VERSION_BYTES,
12679 // destination connection ID length
dschinazi48ac9192019-07-31 00:07:26 -070012680 0x08,
QUICHE teama6ef0a62019-03-07 20:34:33 -050012681 // destination connection ID
12682 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
dschinazi48ac9192019-07-31 00:07:26 -070012683 // source connection ID length
12684 0x00,
QUICHE teama6ef0a62019-03-07 20:34:33 -050012685 // long header packet length
12686 0x1E,
12687 // packet number
12688 0x12, 0x34, 0x56, 0x78,
dschinazic73506e2019-09-20 13:26:46 -070012689 // frame type (stream frame with fin)
12690 0xFE,
12691 // stream id
12692 0x02, 0x03, 0x04,
12693 // offset
12694 0x3A, 0x98, 0xFE, 0xDC, 0x32, 0x10, 0x76, 0x54,
12695 // data length
12696 0x00, 0x0c,
12697 // data
12698 'h', 'e', 'l', 'l',
12699 'o', ' ', 'w', 'o',
12700 'r', 'l', 'd', '!',
12701 // second coalesced packet
12702 // public flags (long header with packet type ZERO_RTT_PROTECTED and
12703 // 4-byte packet number)
12704 0xD3,
12705 // version
12706 QUIC_VERSION_BYTES,
12707 // destination connection ID length
12708 0x08,
12709 // destination connection ID
12710 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x11,
12711 // source connection ID length
12712 0x00,
12713 // long header packet length
12714 0x1E,
12715 // packet number
12716 0x12, 0x34, 0x56, 0x79,
12717 // frame type (stream frame with fin)
12718 0xFE,
12719 // stream id
12720 0x02, 0x03, 0x04,
12721 // offset
12722 0x3A, 0x98, 0xFE, 0xDC, 0x32, 0x10, 0x76, 0x54,
12723 // data length
12724 0x00, 0x0c,
12725 // data
12726 'H', 'E', 'L', 'L',
12727 'O', '_', 'W', 'O',
12728 'R', 'L', 'D', '?',
12729 };
12730 unsigned char packet99[] = {
12731 // first coalesced packet
12732 // public flags (long header with packet type ZERO_RTT_PROTECTED and
12733 // 4-byte packet number)
12734 0xD3,
12735 // version
12736 QUIC_VERSION_BYTES,
12737 // destination connection ID length
12738 0x08,
12739 // destination connection ID
12740 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
12741 // source connection ID length
12742 0x00,
12743 // long header packet length
12744 0x1E,
12745 // packet number
12746 0x12, 0x34, 0x56, 0x78,
QUICHE teama6ef0a62019-03-07 20:34:33 -050012747 // frame type (IETF_STREAM frame with FIN, LEN, and OFFSET bits set)
12748 0x08 | 0x01 | 0x02 | 0x04,
12749 // stream id
12750 kVarInt62FourBytes + 0x00, 0x02, 0x03, 0x04,
12751 // offset
12752 kVarInt62EightBytes + 0x3A, 0x98, 0xFE, 0xDC,
12753 0x32, 0x10, 0x76, 0x54,
12754 // data length
12755 kVarInt62OneByte + 0x0c,
12756 // data
12757 'h', 'e', 'l', 'l',
12758 'o', ' ', 'w', 'o',
12759 'r', 'l', 'd', '!',
12760 // second coalesced packet
12761 // public flags (long header with packet type ZERO_RTT_PROTECTED and
12762 // 4-byte packet number)
12763 0xD3,
12764 // version
12765 QUIC_VERSION_BYTES,
12766 // destination connection ID length
dschinazi48ac9192019-07-31 00:07:26 -070012767 0x08,
QUICHE teama6ef0a62019-03-07 20:34:33 -050012768 // destination connection ID
12769 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x11,
dschinazi48ac9192019-07-31 00:07:26 -070012770 // source connection ID length
12771 0x00,
QUICHE teama6ef0a62019-03-07 20:34:33 -050012772 // long header packet length
12773 0x1E,
12774 // packet number
12775 0x12, 0x34, 0x56, 0x79,
12776 // frame type (IETF_STREAM frame with FIN, LEN, and OFFSET bits set)
12777 0x08 | 0x01 | 0x02 | 0x04,
12778 // stream id
12779 kVarInt62FourBytes + 0x00, 0x02, 0x03, 0x04,
12780 // offset
12781 kVarInt62EightBytes + 0x3A, 0x98, 0xFE, 0xDC,
12782 0x32, 0x10, 0x76, 0x54,
12783 // data length
12784 kVarInt62OneByte + 0x0c,
12785 // data
12786 'H', 'E', 'L', 'L',
12787 'O', '_', 'W', 'O',
12788 'R', 'L', 'D', '?',
12789 };
12790 // clang-format on
12791
dschinazic73506e2019-09-20 13:26:46 -070012792 unsigned char* p = packet;
bnc4e9283d2019-12-17 07:08:57 -080012793 size_t p_length = QUICHE_ARRAYSIZE(packet);
dschinazic73506e2019-09-20 13:26:46 -070012794 if (framer_.transport_version() == QUIC_VERSION_99) {
12795 p = packet99;
bnc4e9283d2019-12-17 07:08:57 -080012796 p_length = QUICHE_ARRAYSIZE(packet99);
dschinazic73506e2019-09-20 13:26:46 -070012797 }
12798
12799 QuicEncryptedPacket encrypted(AsChars(p), p_length, false);
12800
QUICHE teama6ef0a62019-03-07 20:34:33 -050012801 EXPECT_QUIC_PEER_BUG(EXPECT_TRUE(framer_.ProcessPacket(encrypted)),
12802 "Server: Received mismatched coalesced header.*");
12803
bncf6f82b12019-10-30 07:01:01 -070012804 EXPECT_THAT(framer_.error(), IsQuicNoError());
QUICHE teama6ef0a62019-03-07 20:34:33 -050012805 ASSERT_TRUE(visitor_.header_.get());
12806
12807 ASSERT_EQ(1u, visitor_.stream_frames_.size());
12808 EXPECT_EQ(0u, visitor_.ack_frames_.size());
12809
12810 // Stream ID should be the last 3 bytes of kStreamId.
12811 EXPECT_EQ(0x00FFFFFF & kStreamId, visitor_.stream_frames_[0]->stream_id);
12812 EXPECT_TRUE(visitor_.stream_frames_[0]->fin);
12813 EXPECT_EQ(kStreamOffset, visitor_.stream_frames_[0]->offset);
12814 CheckStreamFrameData("hello world!", visitor_.stream_frames_[0].get());
12815
12816 ASSERT_EQ(visitor_.coalesced_packets_.size(), 0u);
12817}
12818
12819TEST_P(QuicFramerTest, InvalidCoalescedPacket) {
12820 if (!QuicVersionHasLongHeaderLengths(framer_.transport_version())) {
12821 return;
12822 }
zhongyi546cc452019-04-12 15:27:49 -070012823 SetDecrypterLevel(ENCRYPTION_ZERO_RTT);
QUICHE teama6ef0a62019-03-07 20:34:33 -050012824 // clang-format off
12825 unsigned char packet[] = {
12826 // first coalesced packet
12827 // public flags (long header with packet type ZERO_RTT_PROTECTED and
12828 // 4-byte packet number)
12829 0xD3,
12830 // version
12831 QUIC_VERSION_BYTES,
12832 // destination connection ID length
dschinazi48ac9192019-07-31 00:07:26 -070012833 0x08,
QUICHE teama6ef0a62019-03-07 20:34:33 -050012834 // destination connection ID
12835 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
dschinazi48ac9192019-07-31 00:07:26 -070012836 // source connection ID length
12837 0x00,
QUICHE teama6ef0a62019-03-07 20:34:33 -050012838 // long header packet length
12839 0x1E,
12840 // packet number
12841 0x12, 0x34, 0x56, 0x78,
dschinazic73506e2019-09-20 13:26:46 -070012842 // frame type (stream frame with fin)
12843 0xFE,
12844 // stream id
12845 0x02, 0x03, 0x04,
12846 // offset
12847 0x3A, 0x98, 0xFE, 0xDC, 0x32, 0x10, 0x76, 0x54,
12848 // data length
12849 0x00, 0x0c,
12850 // data
12851 'h', 'e', 'l', 'l',
12852 'o', ' ', 'w', 'o',
12853 'r', 'l', 'd', '!',
12854 // second coalesced packet
12855 // public flags (long header with packet type ZERO_RTT_PROTECTED and
12856 // 4-byte packet number)
12857 0xD3,
12858 // version would be here but we cut off the invalid coalesced header.
12859 };
12860 unsigned char packet99[] = {
12861 // first coalesced packet
12862 // public flags (long header with packet type ZERO_RTT_PROTECTED and
12863 // 4-byte packet number)
12864 0xD3,
12865 // version
12866 QUIC_VERSION_BYTES,
12867 // destination connection ID length
12868 0x08,
12869 // destination connection ID
12870 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
12871 // source connection ID length
12872 0x00,
12873 // long header packet length
12874 0x1E,
12875 // packet number
12876 0x12, 0x34, 0x56, 0x78,
QUICHE teama6ef0a62019-03-07 20:34:33 -050012877 // frame type (IETF_STREAM frame with FIN, LEN, and OFFSET bits set)
12878 0x08 | 0x01 | 0x02 | 0x04,
12879 // stream id
12880 kVarInt62FourBytes + 0x00, 0x02, 0x03, 0x04,
12881 // offset
12882 kVarInt62EightBytes + 0x3A, 0x98, 0xFE, 0xDC,
12883 0x32, 0x10, 0x76, 0x54,
12884 // data length
12885 kVarInt62OneByte + 0x0c,
12886 // data
12887 'h', 'e', 'l', 'l',
12888 'o', ' ', 'w', 'o',
12889 'r', 'l', 'd', '!',
12890 // second coalesced packet
12891 // public flags (long header with packet type ZERO_RTT_PROTECTED and
12892 // 4-byte packet number)
12893 0xD3,
12894 // version would be here but we cut off the invalid coalesced header.
12895 };
12896 // clang-format on
12897
dschinazic73506e2019-09-20 13:26:46 -070012898 unsigned char* p = packet;
bnc4e9283d2019-12-17 07:08:57 -080012899 size_t p_length = QUICHE_ARRAYSIZE(packet);
dschinazic73506e2019-09-20 13:26:46 -070012900 if (framer_.transport_version() == QUIC_VERSION_99) {
12901 p = packet99;
bnc4e9283d2019-12-17 07:08:57 -080012902 p_length = QUICHE_ARRAYSIZE(packet99);
dschinazic73506e2019-09-20 13:26:46 -070012903 }
12904
12905 QuicEncryptedPacket encrypted(AsChars(p), p_length, false);
12906
QUICHE teama6ef0a62019-03-07 20:34:33 -050012907 EXPECT_QUIC_PEER_BUG(EXPECT_TRUE(framer_.ProcessPacket(encrypted)),
12908 "Server: Failed to parse received coalesced header.*");
12909
bncf6f82b12019-10-30 07:01:01 -070012910 EXPECT_THAT(framer_.error(), IsQuicNoError());
QUICHE teama6ef0a62019-03-07 20:34:33 -050012911 ASSERT_TRUE(visitor_.header_.get());
12912
12913 ASSERT_EQ(1u, visitor_.stream_frames_.size());
12914 EXPECT_EQ(0u, visitor_.ack_frames_.size());
12915
12916 // Stream ID should be the last 3 bytes of kStreamId.
12917 EXPECT_EQ(0x00FFFFFF & kStreamId, visitor_.stream_frames_[0]->stream_id);
12918 EXPECT_TRUE(visitor_.stream_frames_[0]->fin);
12919 EXPECT_EQ(kStreamOffset, visitor_.stream_frames_[0]->offset);
12920 CheckStreamFrameData("hello world!", visitor_.stream_frames_[0].get());
12921
12922 ASSERT_EQ(visitor_.coalesced_packets_.size(), 0u);
12923}
12924
dschinazia484f982019-05-23 03:54:44 -070012925// Some IETF implementations send an initial followed by zeroes instead of
12926// padding inside the initial. We need to make sure that we still process
12927// the initial correctly and ignore the zeroes.
12928TEST_P(QuicFramerTest, CoalescedPacketWithZeroesRoundTrip) {
nharperc8d9e402019-09-12 18:30:14 -070012929 if (!QuicVersionHasLongHeaderLengths(framer_.transport_version()) ||
12930 !framer_.version().UsesInitialObfuscators()) {
dschinazia484f982019-05-23 03:54:44 -070012931 return;
12932 }
12933 ASSERT_TRUE(framer_.version().KnowsWhichDecrypterToUse());
12934 QuicConnectionId connection_id = FramerTestConnectionId();
12935 QuicFramerPeer::SetPerspective(&framer_, Perspective::IS_CLIENT);
12936
12937 CrypterPair client_crypters;
nharperc8d9e402019-09-12 18:30:14 -070012938 CryptoUtils::CreateInitialObfuscators(Perspective::IS_CLIENT,
12939 framer_.version(), connection_id,
12940 &client_crypters);
dschinazia484f982019-05-23 03:54:44 -070012941 framer_.SetEncrypter(ENCRYPTION_INITIAL,
12942 std::move(client_crypters.encrypter));
12943
12944 QuicPacketHeader header;
12945 header.destination_connection_id = connection_id;
12946 header.version_flag = true;
12947 header.packet_number = kPacketNumber;
12948 header.packet_number_length = PACKET_4BYTE_PACKET_NUMBER;
12949 header.long_packet_type = INITIAL;
12950 header.length_length = VARIABLE_LENGTH_INTEGER_LENGTH_2;
12951 header.retry_token_length_length = VARIABLE_LENGTH_INTEGER_LENGTH_1;
nharper965e5922019-09-23 22:33:54 -070012952 QuicFrames frames = {QuicFrame(QuicPingFrame()),
12953 QuicFrame(QuicPaddingFrame(3))};
dschinazia484f982019-05-23 03:54:44 -070012954
12955 std::unique_ptr<QuicPacket> data(BuildDataPacket(header, frames));
12956 ASSERT_NE(nullptr, data);
12957
12958 // Add zeroes after the valid initial packet.
12959 unsigned char packet[kMaxOutgoingPacketSize] = {};
12960 size_t encrypted_length =
12961 framer_.EncryptPayload(ENCRYPTION_INITIAL, header.packet_number, *data,
bnc4e9283d2019-12-17 07:08:57 -080012962 AsChars(packet), QUICHE_ARRAYSIZE(packet));
dschinazia484f982019-05-23 03:54:44 -070012963 ASSERT_NE(0u, encrypted_length);
12964
12965 QuicFramerPeer::SetPerspective(&framer_, Perspective::IS_SERVER);
12966 CrypterPair server_crypters;
nharperc8d9e402019-09-12 18:30:14 -070012967 CryptoUtils::CreateInitialObfuscators(Perspective::IS_SERVER,
12968 framer_.version(), connection_id,
12969 &server_crypters);
dschinazia484f982019-05-23 03:54:44 -070012970 framer_.InstallDecrypter(ENCRYPTION_INITIAL,
12971 std::move(server_crypters.decrypter));
12972
12973 // Make sure the first long header initial packet parses correctly.
bnc4e9283d2019-12-17 07:08:57 -080012974 QuicEncryptedPacket encrypted(AsChars(packet), QUICHE_ARRAYSIZE(packet),
12975 false);
dschinazia484f982019-05-23 03:54:44 -070012976
12977 // Make sure we discard the subsequent zeroes.
12978 EXPECT_QUIC_PEER_BUG(EXPECT_TRUE(framer_.ProcessPacket(encrypted)),
dschinazi48ac9192019-07-31 00:07:26 -070012979 "Server: (Failed to parse received|Received mismatched) "
12980 "coalesced header.*");
dschinazia484f982019-05-23 03:54:44 -070012981}
12982
dschinazie0df3f72019-05-06 16:37:51 -070012983TEST_P(QuicFramerTest, ClientReceivesInvalidVersion) {
fayang36825da2019-08-21 14:01:27 -070012984 if (framer_.transport_version() <= QUIC_VERSION_43) {
dschinazie0df3f72019-05-06 16:37:51 -070012985 return;
12986 }
12987 QuicFramerPeer::SetPerspective(&framer_, Perspective::IS_CLIENT);
12988
12989 // clang-format off
12990 unsigned char packet[] = {
12991 // public flags (long header with packet type INITIAL)
fayang36825da2019-08-21 14:01:27 -070012992 0xC3,
dschinazie0df3f72019-05-06 16:37:51 -070012993 // version that is different from the framer's version
12994 'Q', '0', '4', '3',
12995 // connection ID lengths
12996 0x05,
12997 // source connection ID
12998 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
12999 // packet number
13000 0x01,
13001 // padding frame
13002 0x00,
13003 };
13004 // clang-format on
13005
bnc4e9283d2019-12-17 07:08:57 -080013006 QuicEncryptedPacket encrypted(AsChars(packet), QUICHE_ARRAYSIZE(packet),
13007 false);
dschinazie0df3f72019-05-06 16:37:51 -070013008 EXPECT_FALSE(framer_.ProcessPacket(encrypted));
13009
bncf6f82b12019-10-30 07:01:01 -070013010 EXPECT_THAT(framer_.error(), IsError(QUIC_INVALID_VERSION));
dschinazie0df3f72019-05-06 16:37:51 -070013011 EXPECT_EQ("Client received unexpected version.", framer_.detailed_error());
13012}
13013
QUICHE teama6ef0a62019-03-07 20:34:33 -050013014TEST_P(QuicFramerTest, PacketHeaderWithVariableLengthConnectionId) {
dschinazi97da52b2020-01-13 15:44:43 -080013015 if (!framer_.version().AllowsVariableLengthConnectionIds()) {
QUICHE teama6ef0a62019-03-07 20:34:33 -050013016 return;
13017 }
zhongyi546cc452019-04-12 15:27:49 -070013018 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
QUICHE teama6ef0a62019-03-07 20:34:33 -050013019 char connection_id_bytes[9] = {0xFE, 0xDC, 0xBA, 0x98, 0x76,
13020 0x54, 0x32, 0x10, 0x42};
13021 QuicConnectionId connection_id(connection_id_bytes,
13022 sizeof(connection_id_bytes));
13023 QuicFramerPeer::SetLargestPacketNumber(&framer_, kPacketNumber - 2);
dschinazi8ff74822019-05-28 16:37:20 -070013024 QuicFramerPeer::SetExpectedServerConnectionIDLength(&framer_,
13025 connection_id.length());
QUICHE teama6ef0a62019-03-07 20:34:33 -050013026
13027 // clang-format off
13028 PacketFragments packet = {
13029 // type (8 byte connection_id and 1 byte packet number)
dschinazi48ac9192019-07-31 00:07:26 -070013030 {"Unable to read first byte.",
QUICHE teama6ef0a62019-03-07 20:34:33 -050013031 {0x40}},
13032 // connection_id
dschinazi48ac9192019-07-31 00:07:26 -070013033 {"Unable to read destination connection ID.",
QUICHE teama6ef0a62019-03-07 20:34:33 -050013034 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10, 0x42}},
13035 // packet number
13036 {"Unable to read packet number.",
13037 {0x78}},
13038 };
nharper55fa6132019-05-07 19:37:21 -070013039
13040 PacketFragments packet_with_padding = {
13041 // type (8 byte connection_id and 1 byte packet number)
dschinazi48ac9192019-07-31 00:07:26 -070013042 {"Unable to read first byte.",
nharper55fa6132019-05-07 19:37:21 -070013043 {0x40}},
13044 // connection_id
dschinazi48ac9192019-07-31 00:07:26 -070013045 {"Unable to read destination connection ID.",
nharper55fa6132019-05-07 19:37:21 -070013046 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10, 0x42}},
13047 // packet number
13048 {"",
13049 {0x78}},
13050 // padding
13051 {"", {0x00, 0x00, 0x00}},
13052 };
QUICHE teama6ef0a62019-03-07 20:34:33 -050013053 // clang-format on
13054
nharper55fa6132019-05-07 19:37:21 -070013055 PacketFragments& fragments =
13056 framer_.version().HasHeaderProtection() ? packet_with_padding : packet;
QUICHE teama6ef0a62019-03-07 20:34:33 -050013057 std::unique_ptr<QuicEncryptedPacket> encrypted(
nharper55fa6132019-05-07 19:37:21 -070013058 AssemblePacketFromFragments(fragments));
13059 if (framer_.version().HasHeaderProtection()) {
13060 EXPECT_TRUE(framer_.ProcessPacket(*encrypted));
bncf6f82b12019-10-30 07:01:01 -070013061 EXPECT_THAT(framer_.error(), IsQuicNoError());
nharper55fa6132019-05-07 19:37:21 -070013062 } else {
13063 EXPECT_FALSE(framer_.ProcessPacket(*encrypted));
bncf6f82b12019-10-30 07:01:01 -070013064 EXPECT_THAT(framer_.error(), IsError(QUIC_MISSING_PAYLOAD));
nharper55fa6132019-05-07 19:37:21 -070013065 }
QUICHE teama6ef0a62019-03-07 20:34:33 -050013066 ASSERT_TRUE(visitor_.header_.get());
13067 EXPECT_EQ(connection_id, visitor_.header_->destination_connection_id);
13068 EXPECT_FALSE(visitor_.header_->reset_flag);
13069 EXPECT_FALSE(visitor_.header_->version_flag);
13070 EXPECT_EQ(PACKET_1BYTE_PACKET_NUMBER, visitor_.header_->packet_number_length);
13071 EXPECT_EQ(kPacketNumber, visitor_.header_->packet_number);
13072
nharper55fa6132019-05-07 19:37:21 -070013073 CheckFramingBoundaries(fragments, QUIC_INVALID_PACKET_HEADER);
QUICHE teama6ef0a62019-03-07 20:34:33 -050013074}
13075
QUICHE team10b22a12019-03-21 15:31:42 -070013076TEST_P(QuicFramerTest, MultiplePacketNumberSpaces) {
13077 if (framer_.transport_version() < QUIC_VERSION_46) {
13078 return;
13079 }
QUICHE team10b22a12019-03-21 15:31:42 -070013080 framer_.EnableMultiplePacketNumberSpacesSupport();
13081
13082 // clang-format off
13083 unsigned char long_header_packet[] = {
13084 // public flags (long header with packet type ZERO_RTT_PROTECTED and
13085 // 4-byte packet number)
13086 0xD3,
13087 // version
13088 QUIC_VERSION_BYTES,
13089 // destination connection ID length
fayang91475c42019-06-19 08:04:26 -070013090 0x50,
QUICHE team10b22a12019-03-21 15:31:42 -070013091 // destination connection ID
fayang91475c42019-06-19 08:04:26 -070013092 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
QUICHE team10b22a12019-03-21 15:31:42 -070013093 // packet number
13094 0x12, 0x34, 0x56, 0x78,
13095 // padding frame
13096 0x00,
13097 };
13098 unsigned char long_header_packet99[] = {
13099 // public flags (long header with packet type ZERO_RTT_PROTECTED and
13100 // 4-byte packet number)
13101 0xD3,
13102 // version
13103 QUIC_VERSION_BYTES,
13104 // destination connection ID length
dschinazi48ac9192019-07-31 00:07:26 -070013105 0x08,
QUICHE team10b22a12019-03-21 15:31:42 -070013106 // destination connection ID
fayang91475c42019-06-19 08:04:26 -070013107 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
dschinazi48ac9192019-07-31 00:07:26 -070013108 // source connection ID length
13109 0x00,
QUICHE team10b22a12019-03-21 15:31:42 -070013110 // long header packet length
13111 0x05,
13112 // packet number
13113 0x12, 0x34, 0x56, 0x78,
13114 // padding frame
13115 0x00,
13116 };
13117 // clang-format on
13118
zhongyi546cc452019-04-12 15:27:49 -070013119 if (framer_.version().KnowsWhichDecrypterToUse()) {
13120 framer_.InstallDecrypter(ENCRYPTION_ZERO_RTT,
vasilvv0fc587f2019-09-06 13:33:08 -070013121 std::make_unique<TestDecrypter>());
zhongyi546cc452019-04-12 15:27:49 -070013122 framer_.RemoveDecrypter(ENCRYPTION_INITIAL);
13123 } else {
vasilvv0fc587f2019-09-06 13:33:08 -070013124 framer_.SetDecrypter(ENCRYPTION_ZERO_RTT,
13125 std::make_unique<TestDecrypter>());
zhongyi546cc452019-04-12 15:27:49 -070013126 }
QUICHE team10b22a12019-03-21 15:31:42 -070013127 if (!QuicVersionHasLongHeaderLengths(framer_.transport_version())) {
13128 EXPECT_TRUE(framer_.ProcessPacket(
13129 QuicEncryptedPacket(AsChars(long_header_packet),
bnc4e9283d2019-12-17 07:08:57 -080013130 QUICHE_ARRAYSIZE(long_header_packet), false)));
QUICHE team10b22a12019-03-21 15:31:42 -070013131 } else {
13132 EXPECT_TRUE(framer_.ProcessPacket(
13133 QuicEncryptedPacket(AsChars(long_header_packet99),
bnc4e9283d2019-12-17 07:08:57 -080013134 QUICHE_ARRAYSIZE(long_header_packet99), false)));
QUICHE team10b22a12019-03-21 15:31:42 -070013135 }
13136
bncf6f82b12019-10-30 07:01:01 -070013137 EXPECT_THAT(framer_.error(), IsQuicNoError());
QUICHE team10b22a12019-03-21 15:31:42 -070013138 EXPECT_FALSE(
13139 QuicFramerPeer::GetLargestDecryptedPacketNumber(&framer_, INITIAL_DATA)
13140 .IsInitialized());
13141 EXPECT_FALSE(
13142 QuicFramerPeer::GetLargestDecryptedPacketNumber(&framer_, HANDSHAKE_DATA)
13143 .IsInitialized());
13144 EXPECT_EQ(kPacketNumber, QuicFramerPeer::GetLargestDecryptedPacketNumber(
13145 &framer_, APPLICATION_DATA));
13146
13147 // clang-format off
13148 unsigned char short_header_packet[] = {
13149 // type (short header, 1 byte packet number)
13150 0x40,
13151 // connection_id
fayang91475c42019-06-19 08:04:26 -070013152 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
QUICHE team10b22a12019-03-21 15:31:42 -070013153 // packet number
13154 0x79,
13155 // padding frame
nharper55fa6132019-05-07 19:37:21 -070013156 0x00, 0x00, 0x00,
QUICHE team10b22a12019-03-21 15:31:42 -070013157 };
13158 // clang-format on
13159
13160 QuicEncryptedPacket short_header_encrypted(
bnc4e9283d2019-12-17 07:08:57 -080013161 AsChars(short_header_packet), QUICHE_ARRAYSIZE(short_header_packet),
13162 false);
zhongyi546cc452019-04-12 15:27:49 -070013163 if (framer_.version().KnowsWhichDecrypterToUse()) {
13164 framer_.InstallDecrypter(ENCRYPTION_FORWARD_SECURE,
vasilvv0fc587f2019-09-06 13:33:08 -070013165 std::make_unique<TestDecrypter>());
zhongyi546cc452019-04-12 15:27:49 -070013166 framer_.RemoveDecrypter(ENCRYPTION_ZERO_RTT);
13167 } else {
13168 framer_.SetDecrypter(ENCRYPTION_FORWARD_SECURE,
vasilvv0fc587f2019-09-06 13:33:08 -070013169 std::make_unique<TestDecrypter>());
zhongyi546cc452019-04-12 15:27:49 -070013170 }
QUICHE team10b22a12019-03-21 15:31:42 -070013171 EXPECT_TRUE(framer_.ProcessPacket(short_header_encrypted));
13172
bncf6f82b12019-10-30 07:01:01 -070013173 EXPECT_THAT(framer_.error(), IsQuicNoError());
QUICHE team10b22a12019-03-21 15:31:42 -070013174 EXPECT_FALSE(
13175 QuicFramerPeer::GetLargestDecryptedPacketNumber(&framer_, INITIAL_DATA)
13176 .IsInitialized());
13177 EXPECT_FALSE(
13178 QuicFramerPeer::GetLargestDecryptedPacketNumber(&framer_, HANDSHAKE_DATA)
13179 .IsInitialized());
13180 EXPECT_EQ(kPacketNumber + 1, QuicFramerPeer::GetLargestDecryptedPacketNumber(
13181 &framer_, APPLICATION_DATA));
13182}
13183
nharper2ceb97c2019-04-19 11:38:59 -070013184TEST_P(QuicFramerTest, IetfRetryPacketRejected) {
dschinazi244f6dc2019-05-06 15:45:16 -070013185 if (!framer_.version().KnowsWhichDecrypterToUse() ||
13186 framer_.version().SupportsRetry()) {
nharper2ceb97c2019-04-19 11:38:59 -070013187 return;
13188 }
13189
13190 // clang-format off
fayang36825da2019-08-21 14:01:27 -070013191 PacketFragments packet46 = {
nharper9bb83462019-05-01 10:53:22 -070013192 // public flags (IETF Retry packet, 0-length original destination CID)
dschinazi48ac9192019-07-31 00:07:26 -070013193 {"Unable to read first byte.",
nharper9bb83462019-05-01 10:53:22 -070013194 {0xf0}},
13195 // version tag
13196 {"Unable to read protocol version.",
13197 {QUIC_VERSION_BYTES}},
13198 // connection_id length
dschinazi244f6dc2019-05-06 15:45:16 -070013199 {"RETRY not supported in this version.",
nharper2ceb97c2019-04-19 11:38:59 -070013200 {0x00}},
13201 };
13202 // clang-format on
13203
13204 std::unique_ptr<QuicEncryptedPacket> encrypted(
fayang36825da2019-08-21 14:01:27 -070013205 AssemblePacketFromFragments(packet46));
nharper2ceb97c2019-04-19 11:38:59 -070013206
13207 EXPECT_FALSE(framer_.ProcessPacket(*encrypted));
bncf6f82b12019-10-30 07:01:01 -070013208 EXPECT_THAT(framer_.error(), IsError(QUIC_INVALID_PACKET_HEADER));
fayang36825da2019-08-21 14:01:27 -070013209 CheckFramingBoundaries(packet46, QUIC_INVALID_PACKET_HEADER);
nharper2ceb97c2019-04-19 11:38:59 -070013210}
13211
13212TEST_P(QuicFramerTest, RetryPacketRejectedWithMultiplePacketNumberSpaces) {
dschinazi244f6dc2019-05-06 15:45:16 -070013213 if (framer_.transport_version() < QUIC_VERSION_46 ||
13214 framer_.version().SupportsRetry()) {
nharper2ceb97c2019-04-19 11:38:59 -070013215 return;
13216 }
13217 framer_.EnableMultiplePacketNumberSpacesSupport();
13218
13219 // clang-format off
13220 PacketFragments packet = {
13221 // public flags (IETF Retry packet, 0-length original destination CID)
dschinazi48ac9192019-07-31 00:07:26 -070013222 {"Unable to read first byte.",
nharper2ceb97c2019-04-19 11:38:59 -070013223 {0xf0}},
13224 // version tag
13225 {"Unable to read protocol version.",
13226 {QUIC_VERSION_BYTES}},
13227 // connection_id length
dschinazi244f6dc2019-05-06 15:45:16 -070013228 {"RETRY not supported in this version.",
nharper2ceb97c2019-04-19 11:38:59 -070013229 {0x00}},
13230 };
13231 // clang-format on
13232
13233 std::unique_ptr<QuicEncryptedPacket> encrypted(
13234 AssemblePacketFromFragments(packet));
13235
13236 EXPECT_FALSE(framer_.ProcessPacket(*encrypted));
bncf6f82b12019-10-30 07:01:01 -070013237 EXPECT_THAT(framer_.error(), IsError(QUIC_INVALID_PACKET_HEADER));
nharper2ceb97c2019-04-19 11:38:59 -070013238 CheckFramingBoundaries(packet, QUIC_INVALID_PACKET_HEADER);
13239}
13240
nharpera745e392019-04-19 12:05:15 -070013241TEST_P(QuicFramerTest, ProcessPublicHeaderNoVersionInferredType) {
13242 // The framer needs to have Perspective::IS_SERVER and configured to infer the
13243 // packet header type from the packet (not the version). The framer's version
13244 // needs to be one that uses the IETF packet format.
13245 if (!framer_.version().KnowsWhichDecrypterToUse()) {
13246 return;
13247 }
13248 QuicFramerPeer::SetPerspective(&framer_, Perspective::IS_SERVER);
13249
13250 // Prepare a packet that uses the Google QUIC packet header but has no version
13251 // field.
13252
13253 // clang-format off
13254 PacketFragments packet = {
13255 // public flags (1-byte packet number, 8-byte connection_id, no version)
13256 {"Unable to read public flags.",
13257 {0x08}},
13258 // connection_id
13259 {"Unable to read ConnectionId.",
13260 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
13261 // packet number
13262 {"Unable to read packet number.",
13263 {0x01}},
13264 // padding
13265 {"Invalid public header type for expected version.",
13266 {0x00}},
13267 };
13268 // clang-format on
13269
13270 PacketFragments& fragments = packet;
13271
13272 std::unique_ptr<QuicEncryptedPacket> encrypted(
13273 AssemblePacketFromFragments(fragments));
13274
13275 EXPECT_FALSE(framer_.ProcessPacket(*encrypted));
bncf6f82b12019-10-30 07:01:01 -070013276 EXPECT_THAT(framer_.error(), IsError(QUIC_INVALID_PACKET_HEADER));
nharper8b6d63a2019-05-03 10:34:53 -070013277 EXPECT_EQ("Invalid public header type for expected version.",
13278 framer_.detailed_error());
nharpera745e392019-04-19 12:05:15 -070013279 CheckFramingBoundaries(fragments, QUIC_INVALID_PACKET_HEADER);
13280}
13281
nharper3f283562019-05-02 16:37:12 -070013282TEST_P(QuicFramerTest, ProcessMismatchedHeaderVersion) {
13283 // The framer needs to have Perspective::IS_SERVER and configured to infer the
13284 // packet header type from the packet (not the version). The framer's version
13285 // needs to be one that uses the IETF packet format.
13286 if (!framer_.version().KnowsWhichDecrypterToUse()) {
13287 return;
13288 }
13289 QuicFramerPeer::SetPerspective(&framer_, Perspective::IS_SERVER);
13290
13291 // clang-format off
13292 PacketFragments packet = {
dschinazi072da7c2019-05-07 17:57:42 -070013293 // public flags (Google QUIC header with version present)
nharper3f283562019-05-02 16:37:12 -070013294 {"Unable to read public flags.",
13295 {0x09}},
13296 // connection_id
13297 {"Unable to read ConnectionId.",
13298 {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}},
13299 // version tag
13300 {"Unable to read protocol version.",
13301 {QUIC_VERSION_BYTES}},
13302 // packet number
13303 {"Unable to read packet number.",
13304 {0x01}},
13305 };
13306 // clang-format on
13307
13308 std::unique_ptr<QuicEncryptedPacket> encrypted(
13309 AssemblePacketFromFragments(packet));
13310 framer_.ProcessPacket(*encrypted);
13311
13312 EXPECT_FALSE(framer_.ProcessPacket(*encrypted));
bncf6f82b12019-10-30 07:01:01 -070013313 EXPECT_THAT(framer_.error(), IsError(QUIC_INVALID_PACKET_HEADER));
nharper8b6d63a2019-05-03 10:34:53 -070013314 EXPECT_EQ("Invalid public header type for expected version.",
13315 framer_.detailed_error());
nharper3f283562019-05-02 16:37:12 -070013316 CheckFramingBoundaries(packet, QUIC_INVALID_PACKET_HEADER);
13317}
13318
dschinazi48ac9192019-07-31 00:07:26 -070013319TEST_P(QuicFramerTest, WriteClientVersionNegotiationProbePacketOld) {
13320 SetQuicFlag(FLAGS_quic_prober_uses_length_prefixed_connection_ids, false);
dschinazide0f6dc2019-05-15 16:10:11 -070013321 // clang-format off
13322 static const char expected_packet[1200] = {
13323 // IETF long header with fixed bit set, type initial, all-0 encrypted bits.
13324 0xc0,
13325 // Version, part of the IETF space reserved for negotiation.
13326 0xca, 0xba, 0xda, 0xba,
13327 // Destination connection ID length 8, source connection ID length 0.
13328 0x50,
13329 // 8-byte destination connection ID.
13330 0x56, 0x4e, 0x20, 0x70, 0x6c, 0x7a, 0x20, 0x21,
13331 // 8 bytes of zeroes followed by 8 bytes of ones to ensure that this does
13332 // not parse with any known version.
13333 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
13334 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
13335 // 2 bytes of zeroes to pad to 16 byte boundary.
13336 0x00, 0x00,
13337 // A polite greeting in case a human sees this in tcpdump.
13338 0x54, 0x68, 0x69, 0x73, 0x20, 0x70, 0x61, 0x63,
13339 0x6b, 0x65, 0x74, 0x20, 0x6f, 0x6e, 0x6c, 0x79,
13340 0x20, 0x65, 0x78, 0x69, 0x73, 0x74, 0x73, 0x20,
13341 0x74, 0x6f, 0x20, 0x74, 0x72, 0x69, 0x67, 0x67,
13342 0x65, 0x72, 0x20, 0x49, 0x45, 0x54, 0x46, 0x20,
13343 0x51, 0x55, 0x49, 0x43, 0x20, 0x76, 0x65, 0x72,
13344 0x73, 0x69, 0x6f, 0x6e, 0x20, 0x6e, 0x65, 0x67,
13345 0x6f, 0x74, 0x69, 0x61, 0x74, 0x69, 0x6f, 0x6e,
13346 0x2e, 0x20, 0x50, 0x6c, 0x65, 0x61, 0x73, 0x65,
13347 0x20, 0x72, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x64,
13348 0x20, 0x77, 0x69, 0x74, 0x68, 0x20, 0x61, 0x20,
13349 0x56, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x20,
13350 0x4e, 0x65, 0x67, 0x6f, 0x74, 0x69, 0x61, 0x74,
13351 0x69, 0x6f, 0x6e, 0x20, 0x70, 0x61, 0x63, 0x6b,
13352 0x65, 0x74, 0x20, 0x69, 0x6e, 0x64, 0x69, 0x63,
13353 0x61, 0x74, 0x69, 0x6e, 0x67, 0x20, 0x77, 0x68,
13354 0x61, 0x74, 0x20, 0x76, 0x65, 0x72, 0x73, 0x69,
13355 0x6f, 0x6e, 0x73, 0x20, 0x79, 0x6f, 0x75, 0x20,
13356 0x73, 0x75, 0x70, 0x70, 0x6f, 0x72, 0x74, 0x2e,
13357 0x20, 0x54, 0x68, 0x61, 0x6e, 0x6b, 0x20, 0x79,
13358 0x6f, 0x75, 0x20, 0x61, 0x6e, 0x64, 0x20, 0x68,
13359 0x61, 0x76, 0x65, 0x20, 0x61, 0x20, 0x6e, 0x69,
13360 0x63, 0x65, 0x20, 0x64, 0x61, 0x79, 0x2e, 0x00,
13361 };
13362 // clang-format on
13363 char packet[1200];
13364 char destination_connection_id_bytes[] = {0x56, 0x4e, 0x20, 0x70,
13365 0x6c, 0x7a, 0x20, 0x21};
13366 EXPECT_TRUE(QuicFramer::WriteClientVersionNegotiationProbePacket(
13367 packet, sizeof(packet), destination_connection_id_bytes,
13368 sizeof(destination_connection_id_bytes)));
dmcardle8f7df532020-01-07 13:28:57 -080013369 quiche::test::CompareCharArraysWithHexError("constructed packet", packet,
13370 sizeof(packet), expected_packet,
13371 sizeof(expected_packet));
dschinazide0f6dc2019-05-15 16:10:11 -070013372 QuicEncryptedPacket encrypted(reinterpret_cast<const char*>(packet),
13373 sizeof(packet), false);
13374 // Make sure we fail to parse this packet for the version under test.
dschinazide0f6dc2019-05-15 16:10:11 -070013375 if (framer_.transport_version() <= QUIC_VERSION_43) {
13376 // We can only parse the connection ID with an IETF parser.
dschinazi48ac9192019-07-31 00:07:26 -070013377 EXPECT_FALSE(framer_.ProcessPacket(encrypted));
dschinazide0f6dc2019-05-15 16:10:11 -070013378 return;
13379 }
dschinazi48ac9192019-07-31 00:07:26 -070013380 EXPECT_TRUE(framer_.ProcessPacket(encrypted));
dschinazide0f6dc2019-05-15 16:10:11 -070013381 ASSERT_TRUE(visitor_.header_.get());
13382 QuicConnectionId probe_payload_connection_id(
13383 reinterpret_cast<const char*>(destination_connection_id_bytes),
13384 sizeof(destination_connection_id_bytes));
13385 EXPECT_EQ(probe_payload_connection_id,
13386 visitor_.header_.get()->destination_connection_id);
dschinazi30ab6db2019-08-13 14:43:32 -070013387
13388 PacketHeaderFormat format = GOOGLE_QUIC_PACKET;
fayange3f2f7b2019-09-19 17:01:57 -070013389 QuicLongHeaderType long_packet_type = INVALID_PACKET_TYPE;
dschinazi30ab6db2019-08-13 14:43:32 -070013390 bool version_present = false, has_length_prefix = false;
13391 QuicVersionLabel version_label = 0;
13392 ParsedQuicVersion parsed_version = QuicVersionReservedForNegotiation();
13393 QuicConnectionId destination_connection_id = TestConnectionId(0x33);
13394 QuicConnectionId source_connection_id = TestConnectionId(0x34);
13395 bool retry_token_present = true;
dmcardlecf0bfcf2019-12-13 08:08:21 -080013396 quiche::QuicheStringPiece retry_token;
dschinazi30ab6db2019-08-13 14:43:32 -070013397 std::string detailed_error = "foobar";
13398
13399 QuicErrorCode parse_result = QuicFramer::ParsePublicHeaderDispatcher(
fayange3f2f7b2019-09-19 17:01:57 -070013400 encrypted, kQuicDefaultConnectionIdLength, &format, &long_packet_type,
13401 &version_present, &has_length_prefix, &version_label, &parsed_version,
dschinazi30ab6db2019-08-13 14:43:32 -070013402 &destination_connection_id, &source_connection_id, &retry_token_present,
13403 &retry_token, &detailed_error);
bncf6f82b12019-10-30 07:01:01 -070013404 EXPECT_THAT(parse_result, IsQuicNoError());
dschinazi30ab6db2019-08-13 14:43:32 -070013405 EXPECT_EQ(IETF_QUIC_LONG_HEADER_PACKET, format);
13406 EXPECT_TRUE(version_present);
13407 EXPECT_FALSE(has_length_prefix);
13408 EXPECT_EQ(0xcabadaba, version_label);
13409 EXPECT_EQ(QUIC_VERSION_UNSUPPORTED, parsed_version.transport_version);
13410 EXPECT_EQ(probe_payload_connection_id, destination_connection_id);
13411 EXPECT_EQ(EmptyQuicConnectionId(), source_connection_id);
13412 EXPECT_FALSE(retry_token_present);
dmcardlecf0bfcf2019-12-13 08:08:21 -080013413 EXPECT_EQ(quiche::QuicheStringPiece(), retry_token);
dschinazi30ab6db2019-08-13 14:43:32 -070013414 EXPECT_EQ("", detailed_error);
dschinazide0f6dc2019-05-15 16:10:11 -070013415}
13416
dschinazi48ac9192019-07-31 00:07:26 -070013417TEST_P(QuicFramerTest, WriteClientVersionNegotiationProbePacket) {
13418 SetQuicFlag(FLAGS_quic_prober_uses_length_prefixed_connection_ids, true);
13419 // clang-format off
13420 static const char expected_packet[1200] = {
13421 // IETF long header with fixed bit set, type initial, all-0 encrypted bits.
13422 0xc0,
13423 // Version, part of the IETF space reserved for negotiation.
13424 0xca, 0xba, 0xda, 0xda,
13425 // Destination connection ID length 8.
13426 0x08,
13427 // 8-byte destination connection ID.
13428 0x56, 0x4e, 0x20, 0x70, 0x6c, 0x7a, 0x20, 0x21,
13429 // Source connection ID length 0.
13430 0x00,
13431 // 8 bytes of zeroes followed by 8 bytes of ones to ensure that this does
13432 // not parse with any known version.
13433 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
13434 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
13435 // zeroes to pad to 16 byte boundary.
13436 0x00,
13437 // A polite greeting in case a human sees this in tcpdump.
13438 0x54, 0x68, 0x69, 0x73, 0x20, 0x70, 0x61, 0x63,
13439 0x6b, 0x65, 0x74, 0x20, 0x6f, 0x6e, 0x6c, 0x79,
13440 0x20, 0x65, 0x78, 0x69, 0x73, 0x74, 0x73, 0x20,
13441 0x74, 0x6f, 0x20, 0x74, 0x72, 0x69, 0x67, 0x67,
13442 0x65, 0x72, 0x20, 0x49, 0x45, 0x54, 0x46, 0x20,
13443 0x51, 0x55, 0x49, 0x43, 0x20, 0x76, 0x65, 0x72,
13444 0x73, 0x69, 0x6f, 0x6e, 0x20, 0x6e, 0x65, 0x67,
13445 0x6f, 0x74, 0x69, 0x61, 0x74, 0x69, 0x6f, 0x6e,
13446 0x2e, 0x20, 0x50, 0x6c, 0x65, 0x61, 0x73, 0x65,
13447 0x20, 0x72, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x64,
13448 0x20, 0x77, 0x69, 0x74, 0x68, 0x20, 0x61, 0x20,
13449 0x56, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x20,
13450 0x4e, 0x65, 0x67, 0x6f, 0x74, 0x69, 0x61, 0x74,
13451 0x69, 0x6f, 0x6e, 0x20, 0x70, 0x61, 0x63, 0x6b,
13452 0x65, 0x74, 0x20, 0x69, 0x6e, 0x64, 0x69, 0x63,
13453 0x61, 0x74, 0x69, 0x6e, 0x67, 0x20, 0x77, 0x68,
13454 0x61, 0x74, 0x20, 0x76, 0x65, 0x72, 0x73, 0x69,
13455 0x6f, 0x6e, 0x73, 0x20, 0x79, 0x6f, 0x75, 0x20,
13456 0x73, 0x75, 0x70, 0x70, 0x6f, 0x72, 0x74, 0x2e,
13457 0x20, 0x54, 0x68, 0x61, 0x6e, 0x6b, 0x20, 0x79,
13458 0x6f, 0x75, 0x20, 0x61, 0x6e, 0x64, 0x20, 0x68,
13459 0x61, 0x76, 0x65, 0x20, 0x61, 0x20, 0x6e, 0x69,
13460 0x63, 0x65, 0x20, 0x64, 0x61, 0x79, 0x2e, 0x00,
13461 };
13462 // clang-format on
13463 char packet[1200];
13464 char destination_connection_id_bytes[] = {0x56, 0x4e, 0x20, 0x70,
13465 0x6c, 0x7a, 0x20, 0x21};
13466 EXPECT_TRUE(QuicFramer::WriteClientVersionNegotiationProbePacket(
13467 packet, sizeof(packet), destination_connection_id_bytes,
13468 sizeof(destination_connection_id_bytes)));
dmcardle8f7df532020-01-07 13:28:57 -080013469 quiche::test::CompareCharArraysWithHexError("constructed packet", packet,
13470 sizeof(packet), expected_packet,
13471 sizeof(expected_packet));
dschinazi48ac9192019-07-31 00:07:26 -070013472 QuicEncryptedPacket encrypted(reinterpret_cast<const char*>(packet),
13473 sizeof(packet), false);
dschinazic73506e2019-09-20 13:26:46 -070013474 if (!framer_.version().HasLengthPrefixedConnectionIds()) {
13475 // We can only parse the connection ID with a parser expecting
13476 // length-prefixed connection IDs.
dschinazi48ac9192019-07-31 00:07:26 -070013477 EXPECT_FALSE(framer_.ProcessPacket(encrypted));
13478 return;
13479 }
13480 EXPECT_TRUE(framer_.ProcessPacket(encrypted));
13481 ASSERT_TRUE(visitor_.header_.get());
13482 QuicConnectionId probe_payload_connection_id(
13483 reinterpret_cast<const char*>(destination_connection_id_bytes),
13484 sizeof(destination_connection_id_bytes));
13485 EXPECT_EQ(probe_payload_connection_id,
13486 visitor_.header_.get()->destination_connection_id);
13487}
13488
13489TEST_P(QuicFramerTest, DispatcherParseOldClientVersionNegotiationProbePacket) {
13490 // clang-format off
13491 static const char packet[1200] = {
13492 // IETF long header with fixed bit set, type initial, all-0 encrypted bits.
13493 0xc0,
13494 // Version, part of the IETF space reserved for negotiation.
13495 0xca, 0xba, 0xda, 0xba,
13496 // Destination connection ID length 8, source connection ID length 0.
13497 0x50,
13498 // 8-byte destination connection ID.
13499 0x56, 0x4e, 0x20, 0x70, 0x6c, 0x7a, 0x20, 0x21,
13500 // 8 bytes of zeroes followed by 8 bytes of ones to ensure that this does
13501 // not parse with any known version.
13502 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
13503 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
13504 // 2 bytes of zeroes to pad to 16 byte boundary.
13505 0x00, 0x00,
13506 // A polite greeting in case a human sees this in tcpdump.
13507 0x54, 0x68, 0x69, 0x73, 0x20, 0x70, 0x61, 0x63,
13508 0x6b, 0x65, 0x74, 0x20, 0x6f, 0x6e, 0x6c, 0x79,
13509 0x20, 0x65, 0x78, 0x69, 0x73, 0x74, 0x73, 0x20,
13510 0x74, 0x6f, 0x20, 0x74, 0x72, 0x69, 0x67, 0x67,
13511 0x65, 0x72, 0x20, 0x49, 0x45, 0x54, 0x46, 0x20,
13512 0x51, 0x55, 0x49, 0x43, 0x20, 0x76, 0x65, 0x72,
13513 0x73, 0x69, 0x6f, 0x6e, 0x20, 0x6e, 0x65, 0x67,
13514 0x6f, 0x74, 0x69, 0x61, 0x74, 0x69, 0x6f, 0x6e,
13515 0x2e, 0x20, 0x50, 0x6c, 0x65, 0x61, 0x73, 0x65,
13516 0x20, 0x72, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x64,
13517 0x20, 0x77, 0x69, 0x74, 0x68, 0x20, 0x61, 0x20,
13518 0x56, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x20,
13519 0x4e, 0x65, 0x67, 0x6f, 0x74, 0x69, 0x61, 0x74,
13520 0x69, 0x6f, 0x6e, 0x20, 0x70, 0x61, 0x63, 0x6b,
13521 0x65, 0x74, 0x20, 0x69, 0x6e, 0x64, 0x69, 0x63,
13522 0x61, 0x74, 0x69, 0x6e, 0x67, 0x20, 0x77, 0x68,
13523 0x61, 0x74, 0x20, 0x76, 0x65, 0x72, 0x73, 0x69,
13524 0x6f, 0x6e, 0x73, 0x20, 0x79, 0x6f, 0x75, 0x20,
13525 0x73, 0x75, 0x70, 0x70, 0x6f, 0x72, 0x74, 0x2e,
13526 0x20, 0x54, 0x68, 0x61, 0x6e, 0x6b, 0x20, 0x79,
13527 0x6f, 0x75, 0x20, 0x61, 0x6e, 0x64, 0x20, 0x68,
13528 0x61, 0x76, 0x65, 0x20, 0x61, 0x20, 0x6e, 0x69,
13529 0x63, 0x65, 0x20, 0x64, 0x61, 0x79, 0x2e, 0x00,
13530 };
13531 // clang-format on
13532 char expected_destination_connection_id_bytes[] = {0x56, 0x4e, 0x20, 0x70,
13533 0x6c, 0x7a, 0x20, 0x21};
13534 QuicConnectionId expected_destination_connection_id(
13535 reinterpret_cast<const char*>(expected_destination_connection_id_bytes),
13536 sizeof(expected_destination_connection_id_bytes));
13537
13538 QuicEncryptedPacket encrypted(reinterpret_cast<const char*>(packet),
13539 sizeof(packet));
13540 PacketHeaderFormat format = GOOGLE_QUIC_PACKET;
fayange3f2f7b2019-09-19 17:01:57 -070013541 QuicLongHeaderType long_packet_type = INVALID_PACKET_TYPE;
dschinazi48ac9192019-07-31 00:07:26 -070013542 bool version_present = false, has_length_prefix = true;
13543 QuicVersionLabel version_label = 33;
13544 ParsedQuicVersion parsed_version = UnsupportedQuicVersion();
13545 QuicConnectionId destination_connection_id = TestConnectionId(1);
13546 QuicConnectionId source_connection_id = TestConnectionId(2);
13547 bool retry_token_present = true;
dmcardlecf0bfcf2019-12-13 08:08:21 -080013548 quiche::QuicheStringPiece retry_token;
dschinazi48ac9192019-07-31 00:07:26 -070013549 std::string detailed_error = "foobar";
13550 QuicErrorCode header_parse_result = QuicFramer::ParsePublicHeaderDispatcher(
fayange3f2f7b2019-09-19 17:01:57 -070013551 encrypted, kQuicDefaultConnectionIdLength, &format, &long_packet_type,
13552 &version_present, &has_length_prefix, &version_label, &parsed_version,
dschinazi48ac9192019-07-31 00:07:26 -070013553 &destination_connection_id, &source_connection_id, &retry_token_present,
13554 &retry_token, &detailed_error);
bncf6f82b12019-10-30 07:01:01 -070013555 EXPECT_THAT(header_parse_result, IsQuicNoError());
dschinazi48ac9192019-07-31 00:07:26 -070013556 EXPECT_EQ(IETF_QUIC_LONG_HEADER_PACKET, format);
13557 EXPECT_TRUE(version_present);
13558 EXPECT_FALSE(has_length_prefix);
13559 EXPECT_EQ(0xcabadaba, version_label);
13560 EXPECT_EQ(expected_destination_connection_id, destination_connection_id);
13561 EXPECT_EQ(EmptyQuicConnectionId(), source_connection_id);
13562 EXPECT_FALSE(retry_token_present);
13563 EXPECT_EQ("", detailed_error);
13564}
13565
13566TEST_P(QuicFramerTest, DispatcherParseClientVersionNegotiationProbePacket) {
13567 // clang-format off
13568 static const char packet[1200] = {
13569 // IETF long header with fixed bit set, type initial, all-0 encrypted bits.
13570 0xc0,
13571 // Version, part of the IETF space reserved for negotiation.
13572 0xca, 0xba, 0xda, 0xba,
13573 // Destination connection ID length 8.
13574 0x08,
13575 // 8-byte destination connection ID.
13576 0x56, 0x4e, 0x20, 0x70, 0x6c, 0x7a, 0x20, 0x21,
13577 // Source connection ID length 0.
13578 0x00,
13579 // 8 bytes of zeroes followed by 8 bytes of ones to ensure that this does
13580 // not parse with any known version.
13581 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
13582 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
13583 // 1 byte of zeroes to pad to 16 byte boundary.
13584 0x00,
13585 // A polite greeting in case a human sees this in tcpdump.
13586 0x54, 0x68, 0x69, 0x73, 0x20, 0x70, 0x61, 0x63,
13587 0x6b, 0x65, 0x74, 0x20, 0x6f, 0x6e, 0x6c, 0x79,
13588 0x20, 0x65, 0x78, 0x69, 0x73, 0x74, 0x73, 0x20,
13589 0x74, 0x6f, 0x20, 0x74, 0x72, 0x69, 0x67, 0x67,
13590 0x65, 0x72, 0x20, 0x49, 0x45, 0x54, 0x46, 0x20,
13591 0x51, 0x55, 0x49, 0x43, 0x20, 0x76, 0x65, 0x72,
13592 0x73, 0x69, 0x6f, 0x6e, 0x20, 0x6e, 0x65, 0x67,
13593 0x6f, 0x74, 0x69, 0x61, 0x74, 0x69, 0x6f, 0x6e,
13594 0x2e, 0x20, 0x50, 0x6c, 0x65, 0x61, 0x73, 0x65,
13595 0x20, 0x72, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x64,
13596 0x20, 0x77, 0x69, 0x74, 0x68, 0x20, 0x61, 0x20,
13597 0x56, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x20,
13598 0x4e, 0x65, 0x67, 0x6f, 0x74, 0x69, 0x61, 0x74,
13599 0x69, 0x6f, 0x6e, 0x20, 0x70, 0x61, 0x63, 0x6b,
13600 0x65, 0x74, 0x20, 0x69, 0x6e, 0x64, 0x69, 0x63,
13601 0x61, 0x74, 0x69, 0x6e, 0x67, 0x20, 0x77, 0x68,
13602 0x61, 0x74, 0x20, 0x76, 0x65, 0x72, 0x73, 0x69,
13603 0x6f, 0x6e, 0x73, 0x20, 0x79, 0x6f, 0x75, 0x20,
13604 0x73, 0x75, 0x70, 0x70, 0x6f, 0x72, 0x74, 0x2e,
13605 0x20, 0x54, 0x68, 0x61, 0x6e, 0x6b, 0x20, 0x79,
13606 0x6f, 0x75, 0x20, 0x61, 0x6e, 0x64, 0x20, 0x68,
13607 0x61, 0x76, 0x65, 0x20, 0x61, 0x20, 0x6e, 0x69,
13608 0x63, 0x65, 0x20, 0x64, 0x61, 0x79, 0x2e, 0x00,
13609 };
13610 // clang-format on
13611 char expected_destination_connection_id_bytes[] = {0x56, 0x4e, 0x20, 0x70,
13612 0x6c, 0x7a, 0x20, 0x21};
13613 QuicConnectionId expected_destination_connection_id(
13614 reinterpret_cast<const char*>(expected_destination_connection_id_bytes),
13615 sizeof(expected_destination_connection_id_bytes));
13616
13617 QuicEncryptedPacket encrypted(reinterpret_cast<const char*>(packet),
13618 sizeof(packet));
13619 PacketHeaderFormat format = GOOGLE_QUIC_PACKET;
fayange3f2f7b2019-09-19 17:01:57 -070013620 QuicLongHeaderType long_packet_type = INVALID_PACKET_TYPE;
dschinazi48ac9192019-07-31 00:07:26 -070013621 bool version_present = false, has_length_prefix = false;
13622 QuicVersionLabel version_label = 33;
13623 ParsedQuicVersion parsed_version = UnsupportedQuicVersion();
13624 QuicConnectionId destination_connection_id = TestConnectionId(1);
13625 QuicConnectionId source_connection_id = TestConnectionId(2);
13626 bool retry_token_present = true;
dmcardlecf0bfcf2019-12-13 08:08:21 -080013627 quiche::QuicheStringPiece retry_token;
dschinazi48ac9192019-07-31 00:07:26 -070013628 std::string detailed_error = "foobar";
13629 QuicErrorCode header_parse_result = QuicFramer::ParsePublicHeaderDispatcher(
fayange3f2f7b2019-09-19 17:01:57 -070013630 encrypted, kQuicDefaultConnectionIdLength, &format, &long_packet_type,
13631 &version_present, &has_length_prefix, &version_label, &parsed_version,
dschinazi48ac9192019-07-31 00:07:26 -070013632 &destination_connection_id, &source_connection_id, &retry_token_present,
13633 &retry_token, &detailed_error);
bncf6f82b12019-10-30 07:01:01 -070013634 EXPECT_THAT(header_parse_result, IsQuicNoError());
dschinazi48ac9192019-07-31 00:07:26 -070013635 EXPECT_EQ(IETF_QUIC_LONG_HEADER_PACKET, format);
13636 EXPECT_TRUE(version_present);
13637 EXPECT_TRUE(has_length_prefix);
13638 EXPECT_EQ(0xcabadaba, version_label);
13639 EXPECT_EQ(expected_destination_connection_id, destination_connection_id);
13640 EXPECT_EQ(EmptyQuicConnectionId(), source_connection_id);
13641 EXPECT_EQ("", detailed_error);
13642}
13643
13644TEST_P(QuicFramerTest, ParseServerVersionNegotiationProbeResponseOld) {
13645 SetQuicFlag(FLAGS_quic_prober_uses_length_prefixed_connection_ids, false);
dschinazide0f6dc2019-05-15 16:10:11 -070013646 // clang-format off
13647 const char packet[] = {
13648 // IETF long header with fixed bit set, type initial, all-0 encrypted bits.
13649 0xc0,
13650 // Version of 0, indicating version negotiation.
13651 0x00, 0x00, 0x00, 0x00,
13652 // Destination connection ID length 0, source connection ID length 8.
13653 0x05,
13654 // 8-byte source connection ID.
13655 0x56, 0x4e, 0x20, 0x70, 0x6c, 0x7a, 0x20, 0x21,
13656 // A few supported versions.
13657 0xaa, 0xaa, 0xaa, 0xaa,
13658 QUIC_VERSION_BYTES,
13659 };
13660 // clang-format on
13661 char probe_payload_bytes[] = {0x56, 0x4e, 0x20, 0x70, 0x6c, 0x7a, 0x20, 0x21};
dschinazib012d212019-08-01 18:07:26 -070013662 char parsed_probe_payload_bytes[255] = {};
dschinazide0f6dc2019-05-15 16:10:11 -070013663 uint8_t parsed_probe_payload_length = 0;
13664 std::string parse_detailed_error = "";
13665 EXPECT_TRUE(QuicFramer::ParseServerVersionNegotiationProbeResponse(
13666 reinterpret_cast<const char*>(packet), sizeof(packet),
13667 reinterpret_cast<char*>(parsed_probe_payload_bytes),
13668 &parsed_probe_payload_length, &parse_detailed_error));
13669 EXPECT_EQ("", parse_detailed_error);
dmcardle8f7df532020-01-07 13:28:57 -080013670 quiche::test::CompareCharArraysWithHexError(
dschinazi48ac9192019-07-31 00:07:26 -070013671 "parsed probe", parsed_probe_payload_bytes, parsed_probe_payload_length,
13672 probe_payload_bytes, sizeof(probe_payload_bytes));
13673}
13674
13675TEST_P(QuicFramerTest, ParseServerVersionNegotiationProbeResponse) {
13676 SetQuicFlag(FLAGS_quic_prober_uses_length_prefixed_connection_ids, true);
13677 // clang-format off
13678 const char packet[] = {
13679 // IETF long header with fixed bit set, type initial, all-0 encrypted bits.
13680 0xc0,
13681 // Version of 0, indicating version negotiation.
13682 0x00, 0x00, 0x00, 0x00,
13683 // Destination connection ID length 0, source connection ID length 8.
13684 0x00, 0x08,
13685 // 8-byte source connection ID.
13686 0x56, 0x4e, 0x20, 0x70, 0x6c, 0x7a, 0x20, 0x21,
13687 // A few supported versions.
13688 0xaa, 0xaa, 0xaa, 0xaa,
13689 QUIC_VERSION_BYTES,
13690 };
13691 // clang-format on
13692 char probe_payload_bytes[] = {0x56, 0x4e, 0x20, 0x70, 0x6c, 0x7a, 0x20, 0x21};
dschinazib012d212019-08-01 18:07:26 -070013693 char parsed_probe_payload_bytes[255] = {};
dschinazi48ac9192019-07-31 00:07:26 -070013694 uint8_t parsed_probe_payload_length = 0;
13695 std::string parse_detailed_error = "";
13696 EXPECT_TRUE(QuicFramer::ParseServerVersionNegotiationProbeResponse(
13697 reinterpret_cast<const char*>(packet), sizeof(packet),
13698 reinterpret_cast<char*>(parsed_probe_payload_bytes),
13699 &parsed_probe_payload_length, &parse_detailed_error));
13700 EXPECT_EQ("", parse_detailed_error);
dmcardle8f7df532020-01-07 13:28:57 -080013701 quiche::test::CompareCharArraysWithHexError(
dschinazi48ac9192019-07-31 00:07:26 -070013702 "parsed probe", parsed_probe_payload_bytes, parsed_probe_payload_length,
13703 probe_payload_bytes, sizeof(probe_payload_bytes));
dschinazide0f6dc2019-05-15 16:10:11 -070013704}
13705
dschinazi346b7ce2019-06-05 01:38:18 -070013706TEST_P(QuicFramerTest, ClientConnectionIdFromLongHeaderToClient) {
dschinazi7d066ca2019-05-15 17:59:49 -070013707 if (framer_.transport_version() <= QUIC_VERSION_43) {
13708 // This test requires an IETF long header.
13709 return;
13710 }
dschinazi346b7ce2019-06-05 01:38:18 -070013711 SetDecrypterLevel(ENCRYPTION_HANDSHAKE);
dschinazi7d066ca2019-05-15 17:59:49 -070013712 QuicFramerPeer::SetPerspective(&framer_, Perspective::IS_CLIENT);
dschinazi7d066ca2019-05-15 17:59:49 -070013713 // clang-format off
13714 unsigned char packet[] = {
dschinazi346b7ce2019-06-05 01:38:18 -070013715 // public flags (long header with packet type HANDSHAKE and
dschinazi7d066ca2019-05-15 17:59:49 -070013716 // 4-byte packet number)
fayang36825da2019-08-21 14:01:27 -070013717 0xE3,
dschinazi7d066ca2019-05-15 17:59:49 -070013718 // version
13719 QUIC_VERSION_BYTES,
dschinazi346b7ce2019-06-05 01:38:18 -070013720 // connection ID lengths
dschinazi7d066ca2019-05-15 17:59:49 -070013721 0x50,
13722 // destination connection ID
13723 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
13724 // long header packet length
13725 0x05,
13726 // packet number
13727 0x12, 0x34, 0x56, 0x00,
13728 // padding frame
13729 0x00,
13730 };
dschinazic73506e2019-09-20 13:26:46 -070013731 unsigned char packet49[] = {
dschinazi48ac9192019-07-31 00:07:26 -070013732 // public flags (long header with packet type HANDSHAKE and
13733 // 4-byte packet number)
13734 0xE3,
13735 // version
13736 QUIC_VERSION_BYTES,
13737 // destination connection ID length
13738 0x08,
13739 // destination connection ID
13740 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
13741 // source connection ID length
13742 0x00,
13743 // long header packet length
13744 0x05,
13745 // packet number
13746 0x12, 0x34, 0x56, 0x00,
13747 // padding frame
13748 0x00,
13749 };
dschinazi7d066ca2019-05-15 17:59:49 -070013750 // clang-format on
dschinazi48ac9192019-07-31 00:07:26 -070013751 unsigned char* p = packet;
bnc4e9283d2019-12-17 07:08:57 -080013752 size_t p_length = QUICHE_ARRAYSIZE(packet);
dschinazic73506e2019-09-20 13:26:46 -070013753 if (framer_.transport_version() >= QUIC_VERSION_49) {
13754 p = packet49;
bnc4e9283d2019-12-17 07:08:57 -080013755 p_length = QUICHE_ARRAYSIZE(packet49);
dschinazi48ac9192019-07-31 00:07:26 -070013756 }
13757 const bool parse_success =
13758 framer_.ProcessPacket(QuicEncryptedPacket(AsChars(p), p_length, false));
dschinazi97da52b2020-01-13 15:44:43 -080013759 if (!framer_.version().AllowsVariableLengthConnectionIds()) {
dschinazi346b7ce2019-06-05 01:38:18 -070013760 EXPECT_FALSE(parse_success);
bncf6f82b12019-10-30 07:01:01 -070013761 EXPECT_THAT(framer_.error(), IsError(QUIC_INVALID_PACKET_HEADER));
dschinazi7d066ca2019-05-15 17:59:49 -070013762 EXPECT_EQ("Invalid ConnectionId length.", framer_.detailed_error());
dschinazi346b7ce2019-06-05 01:38:18 -070013763 return;
13764 }
dschinazi346b7ce2019-06-05 01:38:18 -070013765 EXPECT_TRUE(parse_success);
bncf6f82b12019-10-30 07:01:01 -070013766 EXPECT_THAT(framer_.error(), IsQuicNoError());
dschinazi346b7ce2019-06-05 01:38:18 -070013767 EXPECT_EQ("", framer_.detailed_error());
13768 ASSERT_TRUE(visitor_.header_.get());
13769 EXPECT_EQ(FramerTestConnectionId(),
13770 visitor_.header_.get()->destination_connection_id);
13771}
13772
13773TEST_P(QuicFramerTest, ClientConnectionIdFromLongHeaderToServer) {
13774 if (framer_.transport_version() <= QUIC_VERSION_43) {
13775 // This test requires an IETF long header.
13776 return;
13777 }
13778 SetDecrypterLevel(ENCRYPTION_HANDSHAKE);
13779 QuicFramerPeer::SetPerspective(&framer_, Perspective::IS_SERVER);
13780 // clang-format off
13781 unsigned char packet[] = {
13782 // public flags (long header with packet type HANDSHAKE and
13783 // 4-byte packet number)
fayang36825da2019-08-21 14:01:27 -070013784 0xE3,
dschinazi346b7ce2019-06-05 01:38:18 -070013785 // version
13786 QUIC_VERSION_BYTES,
13787 // connection ID lengths
13788 0x05,
13789 // source connection ID
13790 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
13791 // long header packet length
13792 0x05,
13793 // packet number
13794 0x12, 0x34, 0x56, 0x00,
13795 // padding frame
13796 0x00,
13797 };
dschinazic73506e2019-09-20 13:26:46 -070013798 unsigned char packet49[] = {
dschinazi48ac9192019-07-31 00:07:26 -070013799 // public flags (long header with packet type HANDSHAKE and
13800 // 4-byte packet number)
13801 0xE3,
13802 // version
13803 QUIC_VERSION_BYTES,
13804 // connection ID lengths
13805 0x00, 0x08,
13806 // source connection ID
13807 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
13808 // long header packet length
13809 0x05,
13810 // packet number
13811 0x12, 0x34, 0x56, 0x00,
13812 // padding frame
13813 0x00,
13814 };
dschinazi346b7ce2019-06-05 01:38:18 -070013815 // clang-format on
dschinazi48ac9192019-07-31 00:07:26 -070013816 unsigned char* p = packet;
bnc4e9283d2019-12-17 07:08:57 -080013817 size_t p_length = QUICHE_ARRAYSIZE(packet);
dschinazic73506e2019-09-20 13:26:46 -070013818 if (framer_.transport_version() >= QUIC_VERSION_49) {
13819 p = packet49;
bnc4e9283d2019-12-17 07:08:57 -080013820 p_length = QUICHE_ARRAYSIZE(packet49);
dschinazi48ac9192019-07-31 00:07:26 -070013821 }
13822 const bool parse_success =
13823 framer_.ProcessPacket(QuicEncryptedPacket(AsChars(p), p_length, false));
dschinazi97da52b2020-01-13 15:44:43 -080013824 if (!framer_.version().AllowsVariableLengthConnectionIds()) {
dschinazi346b7ce2019-06-05 01:38:18 -070013825 EXPECT_FALSE(parse_success);
bncf6f82b12019-10-30 07:01:01 -070013826 EXPECT_THAT(framer_.error(), IsError(QUIC_INVALID_PACKET_HEADER));
dschinazi346b7ce2019-06-05 01:38:18 -070013827 EXPECT_EQ("Invalid ConnectionId length.", framer_.detailed_error());
13828 return;
13829 }
dschinazi5e1a7b22019-07-31 12:23:21 -070013830 if (!framer_.version().SupportsClientConnectionIds()) {
dschinazi346b7ce2019-06-05 01:38:18 -070013831 EXPECT_FALSE(parse_success);
bncf6f82b12019-10-30 07:01:01 -070013832 EXPECT_THAT(framer_.error(), IsError(QUIC_INVALID_PACKET_HEADER));
dschinazi346b7ce2019-06-05 01:38:18 -070013833 EXPECT_EQ("Client connection ID not supported in this version.",
13834 framer_.detailed_error());
13835 return;
13836 }
13837 EXPECT_TRUE(parse_success);
bncf6f82b12019-10-30 07:01:01 -070013838 EXPECT_THAT(framer_.error(), IsQuicNoError());
dschinazi346b7ce2019-06-05 01:38:18 -070013839 EXPECT_EQ("", framer_.detailed_error());
13840 ASSERT_TRUE(visitor_.header_.get());
13841 EXPECT_EQ(FramerTestConnectionId(),
13842 visitor_.header_.get()->source_connection_id);
dschinazi7d066ca2019-05-15 17:59:49 -070013843}
13844
dschinazi334f0232019-05-29 16:08:53 -070013845TEST_P(QuicFramerTest, ProcessAndValidateIetfConnectionIdLengthClient) {
13846 if (framer_.transport_version() <= QUIC_VERSION_43) {
13847 // This test requires an IETF long header.
13848 return;
13849 }
13850 char connection_id_lengths = 0x05;
13851 QuicDataReader reader(&connection_id_lengths, 1);
13852
13853 bool should_update_expected_server_connection_id_length = false;
13854 uint8_t expected_server_connection_id_length = 8;
13855 uint8_t destination_connection_id_length = 0;
13856 uint8_t source_connection_id_length = 8;
13857 std::string detailed_error = "";
13858
13859 EXPECT_TRUE(QuicFramerPeer::ProcessAndValidateIetfConnectionIdLength(
13860 &reader, framer_.version(), Perspective::IS_CLIENT,
13861 should_update_expected_server_connection_id_length,
13862 &expected_server_connection_id_length, &destination_connection_id_length,
13863 &source_connection_id_length, &detailed_error));
13864 EXPECT_EQ(8, expected_server_connection_id_length);
13865 EXPECT_EQ(0, destination_connection_id_length);
13866 EXPECT_EQ(8, source_connection_id_length);
13867 EXPECT_EQ("", detailed_error);
13868
13869 QuicDataReader reader2(&connection_id_lengths, 1);
13870 should_update_expected_server_connection_id_length = true;
13871 expected_server_connection_id_length = 33;
13872 EXPECT_TRUE(QuicFramerPeer::ProcessAndValidateIetfConnectionIdLength(
13873 &reader2, framer_.version(), Perspective::IS_CLIENT,
13874 should_update_expected_server_connection_id_length,
13875 &expected_server_connection_id_length, &destination_connection_id_length,
13876 &source_connection_id_length, &detailed_error));
13877 EXPECT_EQ(8, expected_server_connection_id_length);
13878 EXPECT_EQ(0, destination_connection_id_length);
13879 EXPECT_EQ(8, source_connection_id_length);
13880 EXPECT_EQ("", detailed_error);
13881}
13882
13883TEST_P(QuicFramerTest, ProcessAndValidateIetfConnectionIdLengthServer) {
13884 if (framer_.transport_version() <= QUIC_VERSION_43) {
13885 // This test requires an IETF long header.
13886 return;
13887 }
13888 char connection_id_lengths = 0x50;
13889 QuicDataReader reader(&connection_id_lengths, 1);
13890
13891 bool should_update_expected_server_connection_id_length = false;
13892 uint8_t expected_server_connection_id_length = 8;
13893 uint8_t destination_connection_id_length = 8;
13894 uint8_t source_connection_id_length = 0;
13895 std::string detailed_error = "";
13896
13897 EXPECT_TRUE(QuicFramerPeer::ProcessAndValidateIetfConnectionIdLength(
13898 &reader, framer_.version(), Perspective::IS_SERVER,
13899 should_update_expected_server_connection_id_length,
13900 &expected_server_connection_id_length, &destination_connection_id_length,
13901 &source_connection_id_length, &detailed_error));
13902 EXPECT_EQ(8, expected_server_connection_id_length);
13903 EXPECT_EQ(8, destination_connection_id_length);
13904 EXPECT_EQ(0, source_connection_id_length);
13905 EXPECT_EQ("", detailed_error);
13906
13907 QuicDataReader reader2(&connection_id_lengths, 1);
13908 should_update_expected_server_connection_id_length = true;
13909 expected_server_connection_id_length = 33;
13910 EXPECT_TRUE(QuicFramerPeer::ProcessAndValidateIetfConnectionIdLength(
13911 &reader2, framer_.version(), Perspective::IS_SERVER,
13912 should_update_expected_server_connection_id_length,
13913 &expected_server_connection_id_length, &destination_connection_id_length,
13914 &source_connection_id_length, &detailed_error));
13915 EXPECT_EQ(8, expected_server_connection_id_length);
13916 EXPECT_EQ(8, destination_connection_id_length);
13917 EXPECT_EQ(0, source_connection_id_length);
13918 EXPECT_EQ("", detailed_error);
13919}
13920
fkastenholzb4dade72019-08-05 06:54:20 -070013921TEST_P(QuicFramerTest, TestExtendedErrorCodeParser) {
13922 if (VersionHasIetfQuicFrames(framer_.transport_version())) {
fkastenholzc5e0c8c2019-09-30 10:16:30 -070013923 // Extended error codes only in IETF QUIC
fkastenholzb4dade72019-08-05 06:54:20 -070013924 return;
13925 }
fkastenholz488a4622019-08-26 06:24:46 -070013926 QuicConnectionCloseFrame frame;
13927
13928 frame.error_details = "this has no error code info in it";
13929 MaybeExtractQuicErrorCode(&frame);
bncf6f82b12019-10-30 07:01:01 -070013930 EXPECT_THAT(frame.extracted_error_code,
13931 IsError(QUIC_IETF_GQUIC_ERROR_MISSING));
fkastenholz488a4622019-08-26 06:24:46 -070013932 EXPECT_EQ("this has no error code info in it", frame.error_details);
13933
13934 frame.error_details = "1234this does not have the colon in it";
13935 MaybeExtractQuicErrorCode(&frame);
bncf6f82b12019-10-30 07:01:01 -070013936 EXPECT_THAT(frame.extracted_error_code,
13937 IsError(QUIC_IETF_GQUIC_ERROR_MISSING));
fkastenholz488a4622019-08-26 06:24:46 -070013938 EXPECT_EQ("1234this does not have the colon in it", frame.error_details);
13939
13940 frame.error_details = "1a234:this has a colon, but a malformed error number";
13941 MaybeExtractQuicErrorCode(&frame);
bncf6f82b12019-10-30 07:01:01 -070013942 EXPECT_THAT(frame.extracted_error_code,
13943 IsError(QUIC_IETF_GQUIC_ERROR_MISSING));
fkastenholz488a4622019-08-26 06:24:46 -070013944 EXPECT_EQ("1a234:this has a colon, but a malformed error number",
13945 frame.error_details);
13946
13947 frame.error_details = "1234:this is good";
13948 MaybeExtractQuicErrorCode(&frame);
13949 EXPECT_EQ(1234u, frame.extracted_error_code);
13950 EXPECT_EQ("this is good", frame.error_details);
13951
13952 frame.error_details =
13953 "1234 :this is not good, space between last digit and colon";
13954 MaybeExtractQuicErrorCode(&frame);
bncf6f82b12019-10-30 07:01:01 -070013955 EXPECT_THAT(frame.extracted_error_code,
13956 IsError(QUIC_IETF_GQUIC_ERROR_MISSING));
fkastenholz488a4622019-08-26 06:24:46 -070013957 EXPECT_EQ("1234 :this is not good, space between last digit and colon",
13958 frame.error_details);
13959
13960 frame.error_details = "123456789";
13961 MaybeExtractQuicErrorCode(&frame);
bncf6f82b12019-10-30 07:01:01 -070013962 EXPECT_THAT(
13963 frame.extracted_error_code,
13964 IsError(QUIC_IETF_GQUIC_ERROR_MISSING)); // Not good, all numbers, no :
fkastenholz488a4622019-08-26 06:24:46 -070013965 EXPECT_EQ("123456789", frame.error_details);
13966
13967 frame.error_details = "1234:";
13968 MaybeExtractQuicErrorCode(&frame);
fkastenholzb4dade72019-08-05 06:54:20 -070013969 EXPECT_EQ(1234u,
fkastenholz488a4622019-08-26 06:24:46 -070013970 frame.extracted_error_code); // corner case.
13971 EXPECT_EQ("", frame.error_details);
13972
13973 frame.error_details = "1234:5678";
13974 MaybeExtractQuicErrorCode(&frame);
13975 EXPECT_EQ(1234u,
13976 frame.extracted_error_code); // another corner case.
13977 EXPECT_EQ("5678", frame.error_details);
13978
13979 frame.error_details = "12345 6789:";
13980 MaybeExtractQuicErrorCode(&frame);
bncf6f82b12019-10-30 07:01:01 -070013981 EXPECT_THAT(frame.extracted_error_code,
13982 IsError(QUIC_IETF_GQUIC_ERROR_MISSING)); // Not good
fkastenholz488a4622019-08-26 06:24:46 -070013983 EXPECT_EQ("12345 6789:", frame.error_details);
13984
13985 frame.error_details = ":no numbers, is not good";
13986 MaybeExtractQuicErrorCode(&frame);
bncf6f82b12019-10-30 07:01:01 -070013987 EXPECT_THAT(frame.extracted_error_code,
13988 IsError(QUIC_IETF_GQUIC_ERROR_MISSING));
fkastenholz488a4622019-08-26 06:24:46 -070013989 EXPECT_EQ(":no numbers, is not good", frame.error_details);
13990
13991 frame.error_details = "qwer:also no numbers, is not good";
13992 MaybeExtractQuicErrorCode(&frame);
bncf6f82b12019-10-30 07:01:01 -070013993 EXPECT_THAT(frame.extracted_error_code,
13994 IsError(QUIC_IETF_GQUIC_ERROR_MISSING));
fkastenholz488a4622019-08-26 06:24:46 -070013995 EXPECT_EQ("qwer:also no numbers, is not good", frame.error_details);
13996
13997 frame.error_details = " 1234:this is not good, space before first digit";
13998 MaybeExtractQuicErrorCode(&frame);
bncf6f82b12019-10-30 07:01:01 -070013999 EXPECT_THAT(frame.extracted_error_code,
14000 IsError(QUIC_IETF_GQUIC_ERROR_MISSING));
fkastenholz488a4622019-08-26 06:24:46 -070014001 EXPECT_EQ(" 1234:this is not good, space before first digit",
14002 frame.error_details);
14003
14004 frame.error_details = "1234:";
14005 MaybeExtractQuicErrorCode(&frame);
14006 EXPECT_EQ(1234u,
14007 frame.extracted_error_code); // this is good
14008 EXPECT_EQ("", frame.error_details);
fkastenholzb4dade72019-08-05 06:54:20 -070014009}
14010
fayang3371b092019-12-04 07:08:52 -080014011// Regression test for crbug/1029636.
14012TEST_P(QuicFramerTest, OverlyLargeAckDelay) {
14013 if (!VersionHasIetfQuicFrames(framer_.transport_version())) {
14014 return;
14015 }
14016 SetDecrypterLevel(ENCRYPTION_FORWARD_SECURE);
14017 // clang-format off
14018 unsigned char packet99[] = {
14019 // type (short header, 4 byte packet number)
14020 0x43,
14021 // connection_id
14022 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
14023 // packet number
14024 0x12, 0x34, 0x56, 0x78,
14025
14026 // frame type (IETF_ACK frame)
14027 0x02,
14028 // largest acked
14029 kVarInt62FourBytes + 0x12, 0x34, 0x56, 0x78,
14030 // ack delay time.
14031 kVarInt62EightBytes + 0x31, 0x00, 0x00, 0x00, 0xF3, 0xA0, 0x81, 0xE0,
14032 // Nr. of additional ack blocks
14033 kVarInt62OneByte + 0x00,
14034 // first ack block length.
14035 kVarInt62FourBytes + 0x12, 0x34, 0x56, 0x77,
14036 };
14037 // clang-format on
14038
bnc4e9283d2019-12-17 07:08:57 -080014039 framer_.ProcessPacket(QuicEncryptedPacket(AsChars(packet99),
14040 QUICHE_ARRAYSIZE(packet99), false));
fayang3371b092019-12-04 07:08:52 -080014041 ASSERT_EQ(1u, visitor_.ack_frames_.size());
14042 // Verify ack_delay_time is set correctly.
14043 EXPECT_EQ(QuicTime::Delta::Infinite(),
14044 visitor_.ack_frames_[0]->ack_delay_time);
14045}
14046
QUICHE teama6ef0a62019-03-07 20:34:33 -050014047} // namespace
14048} // namespace test
14049} // namespace quic