Delete unused downloads page asset.
[chromium-blink-merge.git] / net / quic / quic_framer_test.cc
blob85328278dd882def915d0cb01aa17798395b7410
1 // 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.
5 #include "net/quic/quic_framer.h"
7 #include <stdint.h>
8 #include <algorithm>
9 #include <map>
10 #include <string>
11 #include <vector>
13 #include "base/containers/hash_tables.h"
14 #include "base/logging.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "base/stl_util.h"
17 #include "net/quic/crypto/quic_decrypter.h"
18 #include "net/quic/crypto/quic_encrypter.h"
19 #include "net/quic/quic_protocol.h"
20 #include "net/quic/quic_utils.h"
21 #include "net/quic/test_tools/quic_framer_peer.h"
22 #include "net/quic/test_tools/quic_test_utils.h"
23 #include "net/test/gtest_util.h"
25 using base::hash_set;
26 using base::StringPiece;
27 using std::make_pair;
28 using std::map;
29 using std::numeric_limits;
30 using std::pair;
31 using std::string;
32 using std::vector;
33 using testing::Return;
34 using testing::Truly;
35 using testing::_;
37 namespace net {
38 namespace test {
40 const QuicPacketNumber kEpoch = UINT64_C(1) << 48;
41 const QuicPacketNumber kMask = kEpoch - 1;
43 // Index into the connection_id offset in the header.
44 const size_t kConnectionIdOffset = kPublicFlagsSize;
45 // Index into the version string in the header. (if present).
46 const size_t kVersionOffset = kConnectionIdOffset + PACKET_8BYTE_CONNECTION_ID;
48 // Size in bytes of the stream frame fields for an arbitrary StreamID and
49 // offset and the last frame in a packet.
50 size_t GetMinStreamFrameSize() {
51 return kQuicFrameTypeSize + kQuicMaxStreamIdSize + kQuicMaxStreamOffsetSize;
54 // Index into the packet number offset in the header.
55 size_t GetPacketNumberOffset(QuicConnectionIdLength connection_id_length,
56 bool include_version) {
57 return kConnectionIdOffset + connection_id_length +
58 (include_version ? kQuicVersionSize : 0);
61 size_t GetPacketNumberOffset(bool include_version) {
62 return GetPacketNumberOffset(PACKET_8BYTE_CONNECTION_ID, include_version);
65 // Index into the private flags offset in the data packet header.
66 size_t GetPrivateFlagsOffset(QuicConnectionIdLength connection_id_length,
67 bool include_version) {
68 return GetPacketNumberOffset(connection_id_length, include_version) +
69 PACKET_6BYTE_PACKET_NUMBER;
72 size_t GetPrivateFlagsOffset(bool include_version) {
73 return GetPrivateFlagsOffset(PACKET_8BYTE_CONNECTION_ID, include_version);
76 size_t GetPrivateFlagsOffset(bool include_version,
77 QuicPacketNumberLength packet_number_length) {
78 return GetPacketNumberOffset(PACKET_8BYTE_CONNECTION_ID, include_version) +
79 packet_number_length;
82 // Index into the fec group offset in the header.
83 size_t GetFecGroupOffset(QuicConnectionIdLength connection_id_length,
84 bool include_version) {
85 return GetPrivateFlagsOffset(connection_id_length, include_version) +
86 kPrivateFlagsSize;
89 size_t GetFecGroupOffset(bool include_version) {
90 return GetPrivateFlagsOffset(PACKET_8BYTE_CONNECTION_ID, include_version) +
91 kPrivateFlagsSize;
94 size_t GetFecGroupOffset(bool include_version,
95 QuicPacketNumberLength packet_number_length) {
96 return GetPrivateFlagsOffset(include_version, packet_number_length) +
97 kPrivateFlagsSize;
100 // Index into the message tag of the public reset packet.
101 // Public resets always have full connection_ids.
102 const size_t kPublicResetPacketMessageTagOffset =
103 kConnectionIdOffset + PACKET_8BYTE_CONNECTION_ID;
105 class TestEncrypter : public QuicEncrypter {
106 public:
107 ~TestEncrypter() override {}
108 bool SetKey(StringPiece key) override { return true; }
109 bool SetNoncePrefix(StringPiece nonce_prefix) override { return true; }
110 bool EncryptPacket(QuicPacketNumber packet_number,
111 StringPiece associated_data,
112 StringPiece plaintext,
113 char* output,
114 size_t* output_length,
115 size_t max_output_length) override {
116 packet_number_ = packet_number;
117 associated_data_ = associated_data.as_string();
118 plaintext_ = plaintext.as_string();
119 memcpy(output, plaintext.data(), plaintext.length());
120 *output_length = plaintext.length();
121 return true;
123 size_t GetKeySize() const override { return 0; }
124 size_t GetNoncePrefixSize() const override { return 0; }
125 size_t GetMaxPlaintextSize(size_t ciphertext_size) const override {
126 return ciphertext_size;
128 size_t GetCiphertextSize(size_t plaintext_size) const override {
129 return plaintext_size;
131 StringPiece GetKey() const override { return StringPiece(); }
132 StringPiece GetNoncePrefix() const override { return StringPiece(); }
133 QuicPacketNumber packet_number_;
134 string associated_data_;
135 string plaintext_;
138 class TestDecrypter : public QuicDecrypter {
139 public:
140 ~TestDecrypter() override {}
141 bool SetKey(StringPiece key) override { return true; }
142 bool SetNoncePrefix(StringPiece nonce_prefix) override { return true; }
143 bool DecryptPacket(QuicPacketNumber packet_number,
144 const StringPiece& associated_data,
145 const StringPiece& ciphertext,
146 char* output,
147 size_t* output_length,
148 size_t max_output_length) override {
149 packet_number_ = packet_number;
150 associated_data_ = associated_data.as_string();
151 ciphertext_ = ciphertext.as_string();
152 memcpy(output, ciphertext.data(), ciphertext.length());
153 *output_length = ciphertext.length();
154 return true;
156 StringPiece GetKey() const override { return StringPiece(); }
157 StringPiece GetNoncePrefix() const override { return StringPiece(); }
158 const char* cipher_name() const override { return "Test"; }
159 // Use a distinct value starting with 0xFFFFFF, which is never used by TLS.
160 uint32 cipher_id() const override { return 0xFFFFFFF2; }
161 QuicPacketNumber packet_number_;
162 string associated_data_;
163 string ciphertext_;
166 class TestQuicVisitor : public QuicFramerVisitorInterface {
167 public:
168 TestQuicVisitor()
169 : error_count_(0),
170 version_mismatch_(0),
171 packet_count_(0),
172 frame_count_(0),
173 fec_count_(0),
174 complete_packets_(0),
175 revived_packets_(0),
176 accept_packet_(true),
177 accept_public_header_(true) {
180 ~TestQuicVisitor() override {
181 STLDeleteElements(&stream_frames_);
182 STLDeleteElements(&ack_frames_);
183 STLDeleteElements(&stop_waiting_frames_);
184 STLDeleteElements(&ping_frames_);
185 STLDeleteElements(&fec_data_);
186 STLDeleteElements(&stream_data_);
187 STLDeleteElements(&fec_data_redundancy_);
190 void OnError(QuicFramer* f) override {
191 DVLOG(1) << "QuicFramer Error: " << QuicUtils::ErrorToString(f->error())
192 << " (" << f->error() << ")";
193 ++error_count_;
196 void OnPacket() override {}
198 void OnPublicResetPacket(const QuicPublicResetPacket& packet) override {
199 public_reset_packet_.reset(new QuicPublicResetPacket(packet));
202 void OnVersionNegotiationPacket(
203 const QuicVersionNegotiationPacket& packet) override {
204 version_negotiation_packet_.reset(new QuicVersionNegotiationPacket(packet));
207 void OnRevivedPacket() override { ++revived_packets_; }
209 bool OnProtocolVersionMismatch(QuicVersion version) override {
210 DVLOG(1) << "QuicFramer Version Mismatch, version: " << version;
211 ++version_mismatch_;
212 return true;
215 bool OnUnauthenticatedPublicHeader(
216 const QuicPacketPublicHeader& header) override {
217 public_header_.reset(new QuicPacketPublicHeader(header));
218 return accept_public_header_;
221 bool OnUnauthenticatedHeader(const QuicPacketHeader& header) override {
222 return true;
225 void OnDecryptedPacket(EncryptionLevel level) override {}
227 bool OnPacketHeader(const QuicPacketHeader& header) override {
228 ++packet_count_;
229 header_.reset(new QuicPacketHeader(header));
230 return accept_packet_;
233 bool OnStreamFrame(const QuicStreamFrame& frame) override {
234 ++frame_count_;
235 // Save a copy of the data so it is valid after the packet is processed.
236 string* string_data = new string();
237 frame.data.AppendToString(string_data);
238 stream_data_.push_back(string_data);
239 QuicStreamFrame* stream_frame = new QuicStreamFrame(frame);
240 // Make sure that the stream frame points to this data.
241 stream_frame->data = StringPiece(*string_data);
242 stream_frames_.push_back(stream_frame);
243 return true;
246 void OnFecProtectedPayload(StringPiece payload) override {
247 fec_protected_payload_ = payload.as_string();
250 bool OnAckFrame(const QuicAckFrame& frame) override {
251 ++frame_count_;
252 ack_frames_.push_back(new QuicAckFrame(frame));
253 return true;
256 bool OnStopWaitingFrame(const QuicStopWaitingFrame& frame) override {
257 ++frame_count_;
258 stop_waiting_frames_.push_back(new QuicStopWaitingFrame(frame));
259 return true;
262 bool OnPingFrame(const QuicPingFrame& frame) override {
263 ++frame_count_;
264 ping_frames_.push_back(new QuicPingFrame(frame));
265 return true;
268 void OnFecData(const QuicFecData& fec) override {
269 ++fec_count_;
270 QuicFecData* fec_data = new QuicFecData();
271 fec_data->fec_group = fec.fec_group;
272 // Save a copy of the data so it is valid after the packet is processed.
273 string* redundancy = new string(fec.redundancy.as_string());
274 fec_data_redundancy_.push_back(redundancy);
275 fec_data->redundancy = StringPiece(*redundancy);
276 fec_data_.push_back(fec_data);
279 void OnPacketComplete() override { ++complete_packets_; }
281 bool OnRstStreamFrame(const QuicRstStreamFrame& frame) override {
282 rst_stream_frame_ = frame;
283 return true;
286 bool OnConnectionCloseFrame(const QuicConnectionCloseFrame& frame) override {
287 connection_close_frame_ = frame;
288 return true;
291 bool OnGoAwayFrame(const QuicGoAwayFrame& frame) override {
292 goaway_frame_ = frame;
293 return true;
296 bool OnWindowUpdateFrame(const QuicWindowUpdateFrame& frame) override {
297 window_update_frame_ = frame;
298 return true;
301 bool OnBlockedFrame(const QuicBlockedFrame& frame) override {
302 blocked_frame_ = frame;
303 return true;
306 // Counters from the visitor_ callbacks.
307 int error_count_;
308 int version_mismatch_;
309 int packet_count_;
310 int frame_count_;
311 int fec_count_;
312 int complete_packets_;
313 int revived_packets_;
314 bool accept_packet_;
315 bool accept_public_header_;
317 scoped_ptr<QuicPacketHeader> header_;
318 scoped_ptr<QuicPacketPublicHeader> public_header_;
319 scoped_ptr<QuicPublicResetPacket> public_reset_packet_;
320 scoped_ptr<QuicVersionNegotiationPacket> version_negotiation_packet_;
321 vector<QuicStreamFrame*> stream_frames_;
322 vector<QuicAckFrame*> ack_frames_;
323 vector<QuicStopWaitingFrame*> stop_waiting_frames_;
324 vector<QuicPingFrame*> ping_frames_;
325 vector<QuicFecData*> fec_data_;
326 string fec_protected_payload_;
327 QuicRstStreamFrame rst_stream_frame_;
328 QuicConnectionCloseFrame connection_close_frame_;
329 QuicGoAwayFrame goaway_frame_;
330 QuicWindowUpdateFrame window_update_frame_;
331 QuicBlockedFrame blocked_frame_;
332 vector<string*> stream_data_;
333 vector<string*> fec_data_redundancy_;
336 class QuicFramerTest : public ::testing::TestWithParam<QuicVersion> {
337 public:
338 QuicFramerTest()
339 : encrypter_(new test::TestEncrypter()),
340 decrypter_(new test::TestDecrypter()),
341 start_(QuicTime::Zero().Add(QuicTime::Delta::FromMicroseconds(0x10))),
342 framer_(QuicSupportedVersions(), start_, Perspective::IS_SERVER) {
343 version_ = GetParam();
344 framer_.set_version(version_);
345 framer_.SetDecrypter(ENCRYPTION_NONE, decrypter_);
346 framer_.SetEncrypter(ENCRYPTION_NONE, encrypter_);
347 framer_.set_visitor(&visitor_);
348 framer_.set_received_entropy_calculator(&entropy_calculator_);
351 // Helper function to get unsigned char representation of digit in the
352 // units place of the current QUIC version number.
353 unsigned char GetQuicVersionDigitOnes() {
354 return static_cast<unsigned char> ('0' + version_%10);
357 // Helper function to get unsigned char representation of digit in the
358 // tens place of the current QUIC version number.
359 unsigned char GetQuicVersionDigitTens() {
360 return static_cast<unsigned char> ('0' + (version_/10)%10);
363 bool CheckEncryption(QuicPacketNumber packet_number, QuicPacket* packet) {
364 if (packet_number != encrypter_->packet_number_) {
365 LOG(ERROR) << "Encrypted incorrect packet number. expected "
366 << packet_number << " actual: " << encrypter_->packet_number_;
367 return false;
369 if (packet->AssociatedData() != encrypter_->associated_data_) {
370 LOG(ERROR) << "Encrypted incorrect associated data. expected "
371 << packet->AssociatedData() << " actual: "
372 << encrypter_->associated_data_;
373 return false;
375 if (packet->Plaintext() != encrypter_->plaintext_) {
376 LOG(ERROR) << "Encrypted incorrect plaintext data. expected "
377 << packet->Plaintext() << " actual: "
378 << encrypter_->plaintext_;
379 return false;
381 return true;
384 bool CheckDecryption(const QuicEncryptedPacket& encrypted,
385 bool includes_version) {
386 if (visitor_.header_->packet_packet_number != decrypter_->packet_number_) {
387 LOG(ERROR) << "Decrypted incorrect packet number. expected "
388 << visitor_.header_->packet_packet_number
389 << " actual: " << decrypter_->packet_number_;
390 return false;
392 if (QuicFramer::GetAssociatedDataFromEncryptedPacket(
393 encrypted, PACKET_8BYTE_CONNECTION_ID, includes_version,
394 PACKET_6BYTE_PACKET_NUMBER) != decrypter_->associated_data_) {
395 LOG(ERROR) << "Decrypted incorrect associated data. expected "
396 << QuicFramer::GetAssociatedDataFromEncryptedPacket(
397 encrypted, PACKET_8BYTE_CONNECTION_ID, includes_version,
398 PACKET_6BYTE_PACKET_NUMBER)
399 << " actual: " << decrypter_->associated_data_;
400 return false;
402 StringPiece ciphertext(encrypted.AsStringPiece().substr(
403 GetStartOfEncryptedData(PACKET_8BYTE_CONNECTION_ID, includes_version,
404 PACKET_6BYTE_PACKET_NUMBER)));
405 if (ciphertext != decrypter_->ciphertext_) {
406 LOG(ERROR) << "Decrypted incorrect ciphertext data. expected "
407 << ciphertext << " actual: "
408 << decrypter_->ciphertext_;
409 return false;
411 return true;
414 char* AsChars(unsigned char* data) {
415 return reinterpret_cast<char*>(data);
418 void CheckProcessingFails(unsigned char* packet,
419 size_t len,
420 string expected_error,
421 QuicErrorCode error_code) {
422 QuicEncryptedPacket encrypted(AsChars(packet), len, false);
423 EXPECT_FALSE(framer_.ProcessPacket(encrypted)) << "len: " << len;
424 EXPECT_EQ(expected_error, framer_.detailed_error()) << "len: " << len;
425 EXPECT_EQ(error_code, framer_.error()) << "len: " << len;
428 // Checks if the supplied string matches data in the supplied StreamFrame.
429 void CheckStreamFrameData(string str, QuicStreamFrame* frame) {
430 EXPECT_EQ(str, frame->data);
433 void CheckStreamFrameBoundaries(unsigned char* packet,
434 size_t stream_id_size,
435 bool include_version) {
436 // Now test framing boundaries.
437 for (size_t i = kQuicFrameTypeSize; i < GetMinStreamFrameSize(); ++i) {
438 string expected_error;
439 if (i < kQuicFrameTypeSize + stream_id_size) {
440 expected_error = "Unable to read stream_id.";
441 } else if (i < kQuicFrameTypeSize + stream_id_size +
442 kQuicMaxStreamOffsetSize) {
443 expected_error = "Unable to read offset.";
444 } else {
445 expected_error = "Unable to read frame data.";
447 CheckProcessingFails(
448 packet,
449 i + GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID, include_version,
450 PACKET_6BYTE_PACKET_NUMBER, NOT_IN_FEC_GROUP),
451 expected_error, QUIC_INVALID_STREAM_DATA);
455 void CheckCalculatePacketNumber(QuicPacketNumber expected_packet_number,
456 QuicPacketNumber last_packet_number) {
457 QuicPacketNumber wire_packet_number = expected_packet_number & kMask;
458 QuicFramerPeer::SetLastPacketNumber(&framer_, last_packet_number);
459 EXPECT_EQ(expected_packet_number,
460 QuicFramerPeer::CalculatePacketNumberFromWire(
461 &framer_, PACKET_6BYTE_PACKET_NUMBER, wire_packet_number))
462 << "last_packet_number: " << last_packet_number
463 << " wire_packet_number: " << wire_packet_number;
466 QuicPacket* BuildDataPacket(const QuicPacketHeader& header,
467 const QuicFrames& frames) {
468 return BuildUnsizedDataPacket(&framer_, header, frames);
471 QuicPacket* BuildDataPacket(const QuicPacketHeader& header,
472 const QuicFrames& frames,
473 size_t packet_size) {
474 return BuildUnsizedDataPacket(&framer_, header, frames, packet_size);
477 test::TestEncrypter* encrypter_;
478 test::TestDecrypter* decrypter_;
479 QuicVersion version_;
480 QuicTime start_;
481 QuicFramer framer_;
482 test::TestQuicVisitor visitor_;
483 test::TestEntropyCalculator entropy_calculator_;
486 // Run all framer tests with all supported versions of QUIC.
487 INSTANTIATE_TEST_CASE_P(QuicFramerTests,
488 QuicFramerTest,
489 ::testing::ValuesIn(kSupportedQuicVersions));
491 TEST_P(QuicFramerTest, CalculatePacketNumberFromWireNearEpochStart) {
492 // A few quick manual sanity checks.
493 CheckCalculatePacketNumber(UINT64_C(1), UINT64_C(0));
494 CheckCalculatePacketNumber(kEpoch + 1, kMask);
495 CheckCalculatePacketNumber(kEpoch, kMask);
497 // Cases where the last number was close to the start of the range.
498 for (uint64 last = 0; last < 10; last++) {
499 // Small numbers should not wrap (even if they're out of order).
500 for (uint64 j = 0; j < 10; j++) {
501 CheckCalculatePacketNumber(j, last);
504 // Large numbers should not wrap either (because we're near 0 already).
505 for (uint64 j = 0; j < 10; j++) {
506 CheckCalculatePacketNumber(kEpoch - 1 - j, last);
511 TEST_P(QuicFramerTest, CalculatePacketNumberFromWireNearEpochEnd) {
512 // Cases where the last number was close to the end of the range
513 for (uint64 i = 0; i < 10; i++) {
514 QuicPacketNumber last = kEpoch - i;
516 // Small numbers should wrap.
517 for (uint64 j = 0; j < 10; j++) {
518 CheckCalculatePacketNumber(kEpoch + j, last);
521 // Large numbers should not (even if they're out of order).
522 for (uint64 j = 0; j < 10; j++) {
523 CheckCalculatePacketNumber(kEpoch - 1 - j, last);
528 // Next check where we're in a non-zero epoch to verify we handle
529 // reverse wrapping, too.
530 TEST_P(QuicFramerTest, CalculatePacketNumberFromWireNearPrevEpoch) {
531 const uint64 prev_epoch = 1 * kEpoch;
532 const uint64 cur_epoch = 2 * kEpoch;
533 // Cases where the last number was close to the start of the range
534 for (uint64 i = 0; i < 10; i++) {
535 uint64 last = cur_epoch + i;
536 // Small number should not wrap (even if they're out of order).
537 for (uint64 j = 0; j < 10; j++) {
538 CheckCalculatePacketNumber(cur_epoch + j, last);
541 // But large numbers should reverse wrap.
542 for (uint64 j = 0; j < 10; j++) {
543 uint64 num = kEpoch - 1 - j;
544 CheckCalculatePacketNumber(prev_epoch + num, last);
549 TEST_P(QuicFramerTest, CalculatePacketNumberFromWireNearNextEpoch) {
550 const uint64 cur_epoch = 2 * kEpoch;
551 const uint64 next_epoch = 3 * kEpoch;
552 // Cases where the last number was close to the end of the range
553 for (uint64 i = 0; i < 10; i++) {
554 QuicPacketNumber last = next_epoch - 1 - i;
556 // Small numbers should wrap.
557 for (uint64 j = 0; j < 10; j++) {
558 CheckCalculatePacketNumber(next_epoch + j, last);
561 // but large numbers should not (even if they're out of order).
562 for (uint64 j = 0; j < 10; j++) {
563 uint64 num = kEpoch - 1 - j;
564 CheckCalculatePacketNumber(cur_epoch + num, last);
569 TEST_P(QuicFramerTest, CalculatePacketNumberFromWireNearNextMax) {
570 const uint64 max_number = numeric_limits<uint64>::max();
571 const uint64 max_epoch = max_number & ~kMask;
573 // Cases where the last number was close to the end of the range
574 for (uint64 i = 0; i < 10; i++) {
575 // Subtract 1, because the expected next packet number is 1 more than the
576 // last packet number.
577 QuicPacketNumber last = max_number - i - 1;
579 // Small numbers should not wrap, because they have nowhere to go.
580 for (uint64 j = 0; j < 10; j++) {
581 CheckCalculatePacketNumber(max_epoch + j, last);
584 // Large numbers should not wrap either.
585 for (uint64 j = 0; j < 10; j++) {
586 uint64 num = kEpoch - 1 - j;
587 CheckCalculatePacketNumber(max_epoch + num, last);
592 TEST_P(QuicFramerTest, EmptyPacket) {
593 char packet[] = { 0x00 };
594 QuicEncryptedPacket encrypted(packet, 0, false);
595 EXPECT_FALSE(framer_.ProcessPacket(encrypted));
596 EXPECT_EQ(QUIC_INVALID_PACKET_HEADER, framer_.error());
599 TEST_P(QuicFramerTest, LargePacket) {
600 unsigned char packet[kMaxPacketSize + 1] = {
601 // public flags (8 byte connection_id)
602 0x3C,
603 // connection_id
604 0x10, 0x32, 0x54, 0x76, 0x98, 0xBA, 0xDC, 0xFE,
605 // packet number
606 0xBC, 0x9A, 0x78, 0x56, 0x34, 0x12,
607 // private flags
608 0x00,
611 memset(packet +
612 GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID, !kIncludeVersion,
613 PACKET_6BYTE_PACKET_NUMBER, NOT_IN_FEC_GROUP),
614 0, kMaxPacketSize - GetPacketHeaderSize(
615 PACKET_8BYTE_CONNECTION_ID, !kIncludeVersion,
616 PACKET_6BYTE_PACKET_NUMBER, NOT_IN_FEC_GROUP) +
619 QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
620 EXPECT_FALSE(framer_.ProcessPacket(encrypted));
622 ASSERT_TRUE(visitor_.header_.get());
623 // Make sure we've parsed the packet header, so we can send an error.
624 EXPECT_EQ(UINT64_C(0xFEDCBA9876543210),
625 visitor_.header_->public_header.connection_id);
626 // Make sure the correct error is propagated.
627 EXPECT_EQ(QUIC_PACKET_TOO_LARGE, framer_.error());
630 TEST_P(QuicFramerTest, PacketHeader) {
631 unsigned char packet[] = {
632 // public flags (8 byte connection_id)
633 0x3C,
634 // connection_id
635 0x10, 0x32, 0x54, 0x76, 0x98, 0xBA, 0xDC, 0xFE,
636 // packet number
637 0xBC, 0x9A, 0x78, 0x56, 0x34, 0x12,
638 // private flags
639 0x00,
642 QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
643 EXPECT_FALSE(framer_.ProcessPacket(encrypted));
644 EXPECT_EQ(QUIC_MISSING_PAYLOAD, framer_.error());
645 ASSERT_TRUE(visitor_.header_.get());
646 EXPECT_EQ(UINT64_C(0xFEDCBA9876543210),
647 visitor_.header_->public_header.connection_id);
648 EXPECT_FALSE(visitor_.header_->public_header.reset_flag);
649 EXPECT_FALSE(visitor_.header_->public_header.version_flag);
650 EXPECT_FALSE(visitor_.header_->fec_flag);
651 EXPECT_FALSE(visitor_.header_->entropy_flag);
652 EXPECT_EQ(0, visitor_.header_->entropy_hash);
653 EXPECT_EQ(UINT64_C(0x123456789ABC), visitor_.header_->packet_packet_number);
654 EXPECT_EQ(NOT_IN_FEC_GROUP, visitor_.header_->is_in_fec_group);
655 EXPECT_EQ(0x00u, visitor_.header_->fec_group);
657 // Now test framing boundaries.
658 for (size_t i = 0;
659 i < GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID, !kIncludeVersion,
660 PACKET_6BYTE_PACKET_NUMBER, NOT_IN_FEC_GROUP);
661 ++i) {
662 string expected_error;
663 if (i < kConnectionIdOffset) {
664 expected_error = "Unable to read public flags.";
665 } else if (i < GetPacketNumberOffset(!kIncludeVersion)) {
666 expected_error = "Unable to read ConnectionId.";
667 } else if (i < GetPrivateFlagsOffset(!kIncludeVersion)) {
668 expected_error = "Unable to read packet number.";
669 } else if (i < GetFecGroupOffset(!kIncludeVersion)) {
670 expected_error = "Unable to read private flags.";
671 } else {
672 expected_error = "Unable to read first fec protected packet offset.";
674 CheckProcessingFails(packet, i, expected_error, QUIC_INVALID_PACKET_HEADER);
678 TEST_P(QuicFramerTest, PacketHeaderWith4ByteConnectionId) {
679 QuicFramerPeer::SetLastSerializedConnectionId(
680 &framer_, UINT64_C(0xFEDCBA9876543210));
682 unsigned char packet[] = {
683 // public flags (4 byte connection_id)
684 0x38,
685 // connection_id
686 0x10, 0x32, 0x54, 0x76,
687 // packet number
688 0xBC, 0x9A, 0x78, 0x56, 0x34, 0x12,
689 // private flags
690 0x00,
693 QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
694 EXPECT_FALSE(framer_.ProcessPacket(encrypted));
695 EXPECT_EQ(QUIC_MISSING_PAYLOAD, framer_.error());
696 ASSERT_TRUE(visitor_.header_.get());
697 EXPECT_EQ(UINT64_C(0xFEDCBA9876543210),
698 visitor_.header_->public_header.connection_id);
699 EXPECT_FALSE(visitor_.header_->public_header.reset_flag);
700 EXPECT_FALSE(visitor_.header_->public_header.version_flag);
701 EXPECT_FALSE(visitor_.header_->fec_flag);
702 EXPECT_FALSE(visitor_.header_->entropy_flag);
703 EXPECT_EQ(0, visitor_.header_->entropy_hash);
704 EXPECT_EQ(UINT64_C(0x123456789ABC), visitor_.header_->packet_packet_number);
705 EXPECT_EQ(NOT_IN_FEC_GROUP, visitor_.header_->is_in_fec_group);
706 EXPECT_EQ(0x00u, visitor_.header_->fec_group);
708 // Now test framing boundaries.
709 for (size_t i = 0;
710 i < GetPacketHeaderSize(PACKET_4BYTE_CONNECTION_ID, !kIncludeVersion,
711 PACKET_6BYTE_PACKET_NUMBER, NOT_IN_FEC_GROUP);
712 ++i) {
713 string expected_error;
714 if (i < kConnectionIdOffset) {
715 expected_error = "Unable to read public flags.";
716 } else if (i < GetPacketNumberOffset(PACKET_4BYTE_CONNECTION_ID,
717 !kIncludeVersion)) {
718 expected_error = "Unable to read ConnectionId.";
719 } else if (i < GetPrivateFlagsOffset(PACKET_4BYTE_CONNECTION_ID,
720 !kIncludeVersion)) {
721 expected_error = "Unable to read packet number.";
722 } else if (i < GetFecGroupOffset(PACKET_4BYTE_CONNECTION_ID,
723 !kIncludeVersion)) {
724 expected_error = "Unable to read private flags.";
725 } else {
726 expected_error = "Unable to read first fec protected packet offset.";
728 CheckProcessingFails(packet, i, expected_error, QUIC_INVALID_PACKET_HEADER);
732 TEST_P(QuicFramerTest, PacketHeader1ByteConnectionId) {
733 QuicFramerPeer::SetLastSerializedConnectionId(
734 &framer_, UINT64_C(0xFEDCBA9876543210));
736 unsigned char packet[] = {
737 // public flags (1 byte connection_id)
738 0x34,
739 // connection_id
740 0x10,
741 // packet number
742 0xBC, 0x9A, 0x78, 0x56, 0x34, 0x12,
743 // private flags
744 0x00,
747 QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
748 EXPECT_FALSE(framer_.ProcessPacket(encrypted));
749 EXPECT_EQ(QUIC_MISSING_PAYLOAD, framer_.error());
750 ASSERT_TRUE(visitor_.header_.get());
751 EXPECT_EQ(UINT64_C(0xFEDCBA9876543210),
752 visitor_.header_->public_header.connection_id);
753 EXPECT_FALSE(visitor_.header_->public_header.reset_flag);
754 EXPECT_FALSE(visitor_.header_->public_header.version_flag);
755 EXPECT_FALSE(visitor_.header_->fec_flag);
756 EXPECT_FALSE(visitor_.header_->entropy_flag);
757 EXPECT_EQ(0, visitor_.header_->entropy_hash);
758 EXPECT_EQ(UINT64_C(0x123456789ABC), visitor_.header_->packet_packet_number);
759 EXPECT_EQ(NOT_IN_FEC_GROUP, visitor_.header_->is_in_fec_group);
760 EXPECT_EQ(0x00u, visitor_.header_->fec_group);
762 // Now test framing boundaries.
763 for (size_t i = 0;
764 i < GetPacketHeaderSize(PACKET_1BYTE_CONNECTION_ID, !kIncludeVersion,
765 PACKET_6BYTE_PACKET_NUMBER, NOT_IN_FEC_GROUP);
766 ++i) {
767 string expected_error;
768 if (i < kConnectionIdOffset) {
769 expected_error = "Unable to read public flags.";
770 } else if (i < GetPacketNumberOffset(PACKET_1BYTE_CONNECTION_ID,
771 !kIncludeVersion)) {
772 expected_error = "Unable to read ConnectionId.";
773 } else if (i < GetPrivateFlagsOffset(PACKET_1BYTE_CONNECTION_ID,
774 !kIncludeVersion)) {
775 expected_error = "Unable to read packet number.";
776 } else if (i < GetFecGroupOffset(PACKET_1BYTE_CONNECTION_ID,
777 !kIncludeVersion)) {
778 expected_error = "Unable to read private flags.";
779 } else {
780 expected_error = "Unable to read first fec protected packet offset.";
782 CheckProcessingFails(packet, i, expected_error, QUIC_INVALID_PACKET_HEADER);
786 TEST_P(QuicFramerTest, PacketHeaderWith0ByteConnectionId) {
787 QuicFramerPeer::SetLastSerializedConnectionId(
788 &framer_, UINT64_C(0xFEDCBA9876543210));
790 unsigned char packet[] = {
791 // public flags (0 byte connection_id)
792 0x30,
793 // connection_id
794 // packet number
795 0xBC, 0x9A, 0x78, 0x56, 0x34, 0x12,
796 // private flags
797 0x00,
800 QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
801 EXPECT_FALSE(framer_.ProcessPacket(encrypted));
802 EXPECT_EQ(QUIC_MISSING_PAYLOAD, framer_.error());
803 ASSERT_TRUE(visitor_.header_.get());
804 EXPECT_EQ(UINT64_C(0xFEDCBA9876543210),
805 visitor_.header_->public_header.connection_id);
806 EXPECT_FALSE(visitor_.header_->public_header.reset_flag);
807 EXPECT_FALSE(visitor_.header_->public_header.version_flag);
808 EXPECT_FALSE(visitor_.header_->fec_flag);
809 EXPECT_FALSE(visitor_.header_->entropy_flag);
810 EXPECT_EQ(0, visitor_.header_->entropy_hash);
811 EXPECT_EQ(UINT64_C(0x123456789ABC), visitor_.header_->packet_packet_number);
812 EXPECT_EQ(NOT_IN_FEC_GROUP, visitor_.header_->is_in_fec_group);
813 EXPECT_EQ(0x00u, visitor_.header_->fec_group);
815 // Now test framing boundaries.
816 for (size_t i = 0;
817 i < GetPacketHeaderSize(PACKET_0BYTE_CONNECTION_ID, !kIncludeVersion,
818 PACKET_6BYTE_PACKET_NUMBER, NOT_IN_FEC_GROUP);
819 ++i) {
820 string expected_error;
821 if (i < kConnectionIdOffset) {
822 expected_error = "Unable to read public flags.";
823 } else if (i < GetPacketNumberOffset(PACKET_0BYTE_CONNECTION_ID,
824 !kIncludeVersion)) {
825 expected_error = "Unable to read ConnectionId.";
826 } else if (i < GetPrivateFlagsOffset(PACKET_0BYTE_CONNECTION_ID,
827 !kIncludeVersion)) {
828 expected_error = "Unable to read packet number.";
829 } else if (i < GetFecGroupOffset(PACKET_0BYTE_CONNECTION_ID,
830 !kIncludeVersion)) {
831 expected_error = "Unable to read private flags.";
832 } else {
833 expected_error = "Unable to read first fec protected packet offset.";
835 CheckProcessingFails(packet, i, expected_error, QUIC_INVALID_PACKET_HEADER);
839 TEST_P(QuicFramerTest, PacketHeaderWithVersionFlag) {
840 unsigned char packet[] = {
841 // public flags (version)
842 0x3D,
843 // connection_id
844 0x10, 0x32, 0x54, 0x76, 0x98, 0xBA, 0xDC, 0xFE,
845 // version tag
846 'Q', '0', GetQuicVersionDigitTens(), GetQuicVersionDigitOnes(),
847 // packet number
848 0xBC, 0x9A, 0x78, 0x56, 0x34, 0x12,
849 // private flags
850 0x00,
853 QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
854 EXPECT_FALSE(framer_.ProcessPacket(encrypted));
855 EXPECT_EQ(QUIC_MISSING_PAYLOAD, framer_.error());
856 ASSERT_TRUE(visitor_.header_.get());
857 EXPECT_EQ(UINT64_C(0xFEDCBA9876543210),
858 visitor_.header_->public_header.connection_id);
859 EXPECT_FALSE(visitor_.header_->public_header.reset_flag);
860 EXPECT_TRUE(visitor_.header_->public_header.version_flag);
861 EXPECT_EQ(GetParam(), visitor_.header_->public_header.versions[0]);
862 EXPECT_FALSE(visitor_.header_->fec_flag);
863 EXPECT_FALSE(visitor_.header_->entropy_flag);
864 EXPECT_EQ(0, visitor_.header_->entropy_hash);
865 EXPECT_EQ(UINT64_C(0x123456789ABC), visitor_.header_->packet_packet_number);
866 EXPECT_EQ(NOT_IN_FEC_GROUP, visitor_.header_->is_in_fec_group);
867 EXPECT_EQ(0x00u, visitor_.header_->fec_group);
869 // Now test framing boundaries.
870 for (size_t i = 0;
871 i < GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID, kIncludeVersion,
872 PACKET_6BYTE_PACKET_NUMBER, NOT_IN_FEC_GROUP);
873 ++i) {
874 string expected_error;
875 if (i < kConnectionIdOffset) {
876 expected_error = "Unable to read public flags.";
877 } else if (i < kVersionOffset) {
878 expected_error = "Unable to read ConnectionId.";
879 } else if (i < GetPacketNumberOffset(kIncludeVersion)) {
880 expected_error = "Unable to read protocol version.";
881 } else if (i < GetPrivateFlagsOffset(kIncludeVersion)) {
882 expected_error = "Unable to read packet number.";
883 } else if (i < GetFecGroupOffset(kIncludeVersion)) {
884 expected_error = "Unable to read private flags.";
885 } else {
886 expected_error = "Unable to read first fec protected packet offset.";
888 CheckProcessingFails(packet, i, expected_error, QUIC_INVALID_PACKET_HEADER);
892 TEST_P(QuicFramerTest, PacketHeaderWith4BytePacketNumber) {
893 QuicFramerPeer::SetLastPacketNumber(&framer_, UINT64_C(0x123456789ABA));
895 unsigned char packet[] = {
896 // public flags (8 byte connection_id and 4 byte packet number)
897 0x2C,
898 // connection_id
899 0x10, 0x32, 0x54, 0x76, 0x98, 0xBA, 0xDC, 0xFE,
900 // packet number
901 0xBC, 0x9A, 0x78, 0x56,
902 // private flags
903 0x00,
906 QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
907 EXPECT_FALSE(framer_.ProcessPacket(encrypted));
908 EXPECT_EQ(QUIC_MISSING_PAYLOAD, framer_.error());
909 ASSERT_TRUE(visitor_.header_.get());
910 EXPECT_EQ(UINT64_C(0xFEDCBA9876543210),
911 visitor_.header_->public_header.connection_id);
912 EXPECT_FALSE(visitor_.header_->public_header.reset_flag);
913 EXPECT_FALSE(visitor_.header_->public_header.version_flag);
914 EXPECT_FALSE(visitor_.header_->fec_flag);
915 EXPECT_FALSE(visitor_.header_->entropy_flag);
916 EXPECT_EQ(0, visitor_.header_->entropy_hash);
917 EXPECT_EQ(UINT64_C(0x123456789ABC), visitor_.header_->packet_packet_number);
918 EXPECT_EQ(NOT_IN_FEC_GROUP, visitor_.header_->is_in_fec_group);
919 EXPECT_EQ(0x00u, visitor_.header_->fec_group);
921 // Now test framing boundaries.
922 for (size_t i = 0;
923 i < GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID, !kIncludeVersion,
924 PACKET_4BYTE_PACKET_NUMBER, NOT_IN_FEC_GROUP);
925 ++i) {
926 string expected_error;
927 if (i < kConnectionIdOffset) {
928 expected_error = "Unable to read public flags.";
929 } else if (i < GetPacketNumberOffset(!kIncludeVersion)) {
930 expected_error = "Unable to read ConnectionId.";
931 } else if (i < GetPrivateFlagsOffset(!kIncludeVersion,
932 PACKET_4BYTE_PACKET_NUMBER)) {
933 expected_error = "Unable to read packet number.";
934 } else if (i < GetFecGroupOffset(!kIncludeVersion,
935 PACKET_4BYTE_PACKET_NUMBER)) {
936 expected_error = "Unable to read private flags.";
937 } else {
938 expected_error = "Unable to read first fec protected packet offset.";
940 CheckProcessingFails(packet, i, expected_error, QUIC_INVALID_PACKET_HEADER);
944 TEST_P(QuicFramerTest, PacketHeaderWith2BytePacketNumber) {
945 QuicFramerPeer::SetLastPacketNumber(&framer_, UINT64_C(0x123456789ABA));
947 unsigned char packet[] = {
948 // public flags (8 byte connection_id and 2 byte packet number)
949 0x1C,
950 // connection_id
951 0x10, 0x32, 0x54, 0x76, 0x98, 0xBA, 0xDC, 0xFE,
952 // packet number
953 0xBC, 0x9A,
954 // private flags
955 0x00,
958 QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
959 EXPECT_FALSE(framer_.ProcessPacket(encrypted));
960 EXPECT_EQ(QUIC_MISSING_PAYLOAD, framer_.error());
961 ASSERT_TRUE(visitor_.header_.get());
962 EXPECT_EQ(UINT64_C(0xFEDCBA9876543210),
963 visitor_.header_->public_header.connection_id);
964 EXPECT_FALSE(visitor_.header_->public_header.reset_flag);
965 EXPECT_FALSE(visitor_.header_->public_header.version_flag);
966 EXPECT_FALSE(visitor_.header_->fec_flag);
967 EXPECT_FALSE(visitor_.header_->entropy_flag);
968 EXPECT_EQ(0, visitor_.header_->entropy_hash);
969 EXPECT_EQ(UINT64_C(0x123456789ABC), visitor_.header_->packet_packet_number);
970 EXPECT_EQ(NOT_IN_FEC_GROUP, visitor_.header_->is_in_fec_group);
971 EXPECT_EQ(0x00u, visitor_.header_->fec_group);
973 // Now test framing boundaries.
974 for (size_t i = 0;
975 i < GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID, !kIncludeVersion,
976 PACKET_2BYTE_PACKET_NUMBER, NOT_IN_FEC_GROUP);
977 ++i) {
978 string expected_error;
979 if (i < kConnectionIdOffset) {
980 expected_error = "Unable to read public flags.";
981 } else if (i < GetPacketNumberOffset(!kIncludeVersion)) {
982 expected_error = "Unable to read ConnectionId.";
983 } else if (i < GetPrivateFlagsOffset(!kIncludeVersion,
984 PACKET_2BYTE_PACKET_NUMBER)) {
985 expected_error = "Unable to read packet number.";
986 } else if (i < GetFecGroupOffset(!kIncludeVersion,
987 PACKET_2BYTE_PACKET_NUMBER)) {
988 expected_error = "Unable to read private flags.";
989 } else {
990 expected_error = "Unable to read first fec protected packet offset.";
992 CheckProcessingFails(packet, i, expected_error, QUIC_INVALID_PACKET_HEADER);
996 TEST_P(QuicFramerTest, PacketHeaderWith1BytePacketNumber) {
997 QuicFramerPeer::SetLastPacketNumber(&framer_, UINT64_C(0x123456789ABA));
999 unsigned char packet[] = {
1000 // public flags (8 byte connection_id and 1 byte packet number)
1001 0x0C,
1002 // connection_id
1003 0x10, 0x32, 0x54, 0x76, 0x98, 0xBA, 0xDC, 0xFE,
1004 // packet number
1005 0xBC,
1006 // private flags
1007 0x00,
1010 QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
1011 EXPECT_FALSE(framer_.ProcessPacket(encrypted));
1012 EXPECT_EQ(QUIC_MISSING_PAYLOAD, framer_.error());
1013 ASSERT_TRUE(visitor_.header_.get());
1014 EXPECT_EQ(UINT64_C(0xFEDCBA9876543210),
1015 visitor_.header_->public_header.connection_id);
1016 EXPECT_FALSE(visitor_.header_->public_header.reset_flag);
1017 EXPECT_FALSE(visitor_.header_->public_header.version_flag);
1018 EXPECT_FALSE(visitor_.header_->fec_flag);
1019 EXPECT_FALSE(visitor_.header_->entropy_flag);
1020 EXPECT_EQ(0, visitor_.header_->entropy_hash);
1021 EXPECT_EQ(UINT64_C(0x123456789ABC), visitor_.header_->packet_packet_number);
1022 EXPECT_EQ(NOT_IN_FEC_GROUP, visitor_.header_->is_in_fec_group);
1023 EXPECT_EQ(0x00u, visitor_.header_->fec_group);
1025 // Now test framing boundaries.
1026 for (size_t i = 0;
1027 i < GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID, !kIncludeVersion,
1028 PACKET_1BYTE_PACKET_NUMBER, NOT_IN_FEC_GROUP);
1029 ++i) {
1030 string expected_error;
1031 if (i < kConnectionIdOffset) {
1032 expected_error = "Unable to read public flags.";
1033 } else if (i < GetPacketNumberOffset(!kIncludeVersion)) {
1034 expected_error = "Unable to read ConnectionId.";
1035 } else if (i < GetPrivateFlagsOffset(!kIncludeVersion,
1036 PACKET_1BYTE_PACKET_NUMBER)) {
1037 expected_error = "Unable to read packet number.";
1038 } else if (i < GetFecGroupOffset(!kIncludeVersion,
1039 PACKET_1BYTE_PACKET_NUMBER)) {
1040 expected_error = "Unable to read private flags.";
1041 } else {
1042 expected_error = "Unable to read first fec protected packet offset.";
1044 CheckProcessingFails(packet, i, expected_error, QUIC_INVALID_PACKET_HEADER);
1048 TEST_P(QuicFramerTest, InvalidPublicFlag) {
1049 unsigned char packet[] = {
1050 // public flags: all flags set but the public reset flag and version flag.
1051 0xFC,
1052 // connection_id
1053 0x10, 0x32, 0x54, 0x76, 0x98, 0xBA, 0xDC, 0xFE,
1054 // packet number
1055 0xBC, 0x9A, 0x78, 0x56, 0x34, 0x12,
1056 // private flags
1057 0x00,
1059 // frame type (padding)
1060 0x00, 0x00, 0x00, 0x00, 0x00};
1061 CheckProcessingFails(packet,
1062 arraysize(packet),
1063 "Illegal public flags value.",
1064 QUIC_INVALID_PACKET_HEADER);
1066 // Now turn off validation.
1067 framer_.set_validate_flags(false);
1068 QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
1069 EXPECT_TRUE(framer_.ProcessPacket(encrypted));
1072 TEST_P(QuicFramerTest, InvalidPublicFlagWithMatchingVersions) {
1073 unsigned char packet[] = {// public flags (8 byte connection_id and version
1074 // flag and an unknown flag)
1075 0x4D,
1076 // connection_id
1077 0x10, 0x32, 0x54, 0x76, 0x98, 0xBA, 0xDC, 0xFE,
1078 // version tag
1079 'Q', '0', GetQuicVersionDigitTens(),
1080 GetQuicVersionDigitOnes(),
1081 // packet number
1082 0xBC, 0x9A, 0x78, 0x56, 0x34, 0x12,
1083 // private flags
1084 0x00,
1086 // frame type (padding)
1087 0x00, 0x00, 0x00, 0x00, 0x00};
1088 CheckProcessingFails(packet,
1089 arraysize(packet),
1090 "Illegal public flags value.",
1091 QUIC_INVALID_PACKET_HEADER);
1094 TEST_P(QuicFramerTest, LargePublicFlagWithMismatchedVersions) {
1095 unsigned char packet[] = {
1096 // public flags (8 byte connection_id, version flag and an unknown flag)
1097 0x7D,
1098 // connection_id
1099 0x10, 0x32, 0x54, 0x76, 0x98, 0xBA, 0xDC, 0xFE,
1100 // version tag
1101 'Q', '0', '0', '0',
1102 // packet number
1103 0xBC, 0x9A, 0x78, 0x56, 0x34, 0x12,
1104 // private flags
1105 0x00,
1107 // frame type (padding frame)
1108 0x00, 0x00, 0x00, 0x00, 0x00};
1109 QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
1110 EXPECT_TRUE(framer_.ProcessPacket(encrypted));
1111 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
1112 ASSERT_TRUE(visitor_.header_.get());
1113 EXPECT_EQ(0, visitor_.frame_count_);
1114 EXPECT_EQ(1, visitor_.version_mismatch_);
1117 TEST_P(QuicFramerTest, InvalidPrivateFlag) {
1118 unsigned char packet[] = {// public flags (8 byte connection_id)
1119 0x3C,
1120 // connection_id
1121 0x10, 0x32, 0x54, 0x76, 0x98, 0xBA, 0xDC, 0xFE,
1122 // packet number
1123 0xBC, 0x9A, 0x78, 0x56, 0x34, 0x12,
1124 // private flags
1125 0x10,
1127 // frame type (padding)
1128 0x00, 0x00, 0x00, 0x00, 0x00};
1129 CheckProcessingFails(packet,
1130 arraysize(packet),
1131 "Illegal private flags value.",
1132 QUIC_INVALID_PACKET_HEADER);
1135 TEST_P(QuicFramerTest, InvalidFECGroupOffset) {
1136 unsigned char packet[] = {// public flags (8 byte connection_id)
1137 0x3C,
1138 // connection_id
1139 0x10, 0x32, 0x54, 0x76, 0x98, 0xBA, 0xDC, 0xFE,
1140 // packet number
1141 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
1142 // private flags (fec group)
1143 0x02,
1144 // first fec protected packet offset
1145 0x10};
1146 CheckProcessingFails(packet, arraysize(packet),
1147 "First fec protected packet offset must be less "
1148 "than the packet number.",
1149 QUIC_INVALID_PACKET_HEADER);
1152 TEST_P(QuicFramerTest, PaddingFrame) {
1153 unsigned char packet[] = {
1154 // public flags (8 byte connection_id)
1155 0x3C,
1156 // connection_id
1157 0x10, 0x32, 0x54, 0x76, 0x98, 0xBA, 0xDC, 0xFE,
1158 // packet number
1159 0xBC, 0x9A, 0x78, 0x56, 0x34, 0x12,
1160 // private flags
1161 0x00,
1163 // frame type (padding frame)
1164 0x00,
1165 // Ignored data (which in this case is a stream frame)
1166 // frame type (stream frame with fin)
1167 0xFF,
1168 // stream id
1169 0x04, 0x03, 0x02, 0x01,
1170 // offset
1171 0x54, 0x76, 0x10, 0x32, 0xDC, 0xFE, 0x98, 0xBA,
1172 // data length
1173 0x0c, 0x00,
1174 // data
1175 'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', '!',
1178 QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
1179 EXPECT_TRUE(framer_.ProcessPacket(encrypted));
1180 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
1181 ASSERT_TRUE(visitor_.header_.get());
1182 EXPECT_TRUE(CheckDecryption(encrypted, !kIncludeVersion));
1184 ASSERT_EQ(0u, visitor_.stream_frames_.size());
1185 EXPECT_EQ(0u, visitor_.ack_frames_.size());
1186 // A packet with no frames is not acceptable.
1187 CheckProcessingFails(
1188 packet, GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID, !kIncludeVersion,
1189 PACKET_6BYTE_PACKET_NUMBER, NOT_IN_FEC_GROUP),
1190 "Packet has no frames.", QUIC_MISSING_PAYLOAD);
1193 TEST_P(QuicFramerTest, StreamFrame) {
1194 unsigned char packet[] = {
1195 // public flags (8 byte connection_id)
1196 0x3C,
1197 // connection_id
1198 0x10, 0x32, 0x54, 0x76, 0x98, 0xBA, 0xDC, 0xFE,
1199 // packet number
1200 0xBC, 0x9A, 0x78, 0x56, 0x34, 0x12,
1201 // private flags
1202 0x00,
1204 // frame type (stream frame with fin)
1205 0xFF,
1206 // stream id
1207 0x04, 0x03, 0x02, 0x01,
1208 // offset
1209 0x54, 0x76, 0x10, 0x32, 0xDC, 0xFE, 0x98, 0xBA,
1210 // data length
1211 0x0c, 0x00,
1212 // data
1213 'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', '!',
1216 QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
1217 EXPECT_TRUE(framer_.ProcessPacket(encrypted));
1219 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
1220 ASSERT_TRUE(visitor_.header_.get());
1221 EXPECT_TRUE(CheckDecryption(encrypted, !kIncludeVersion));
1223 ASSERT_EQ(1u, visitor_.stream_frames_.size());
1224 EXPECT_EQ(0u, visitor_.ack_frames_.size());
1225 EXPECT_EQ(static_cast<uint64>(0x01020304),
1226 visitor_.stream_frames_[0]->stream_id);
1227 EXPECT_TRUE(visitor_.stream_frames_[0]->fin);
1228 EXPECT_EQ(UINT64_C(0xBA98FEDC32107654), visitor_.stream_frames_[0]->offset);
1229 CheckStreamFrameData("hello world!", visitor_.stream_frames_[0]);
1231 // Now test framing boundaries.
1232 CheckStreamFrameBoundaries(packet, kQuicMaxStreamIdSize, !kIncludeVersion);
1235 TEST_P(QuicFramerTest, StreamFrame3ByteStreamId) {
1236 unsigned char packet[] = {
1237 // public flags (8 byte connection_id)
1238 0x3C,
1239 // connection_id
1240 0x10, 0x32, 0x54, 0x76, 0x98, 0xBA, 0xDC, 0xFE,
1241 // packet number
1242 0xBC, 0x9A, 0x78, 0x56, 0x34, 0x12,
1243 // private flags
1244 0x00,
1246 // frame type (stream frame with fin)
1247 0xFE,
1248 // stream id
1249 0x04, 0x03, 0x02,
1250 // offset
1251 0x54, 0x76, 0x10, 0x32, 0xDC, 0xFE, 0x98, 0xBA,
1252 // data length
1253 0x0c, 0x00,
1254 // data
1255 'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', '!',
1258 QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
1259 EXPECT_TRUE(framer_.ProcessPacket(encrypted));
1261 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
1262 ASSERT_TRUE(visitor_.header_.get());
1263 EXPECT_TRUE(CheckDecryption(encrypted, !kIncludeVersion));
1265 ASSERT_EQ(1u, visitor_.stream_frames_.size());
1266 EXPECT_EQ(0u, visitor_.ack_frames_.size());
1267 EXPECT_EQ(UINT64_C(0x00020304), visitor_.stream_frames_[0]->stream_id);
1268 EXPECT_TRUE(visitor_.stream_frames_[0]->fin);
1269 EXPECT_EQ(UINT64_C(0xBA98FEDC32107654), visitor_.stream_frames_[0]->offset);
1270 CheckStreamFrameData("hello world!", visitor_.stream_frames_[0]);
1272 // Now test framing boundaries.
1273 const size_t stream_id_size = 3;
1274 CheckStreamFrameBoundaries(packet, stream_id_size, !kIncludeVersion);
1277 TEST_P(QuicFramerTest, StreamFrame2ByteStreamId) {
1278 unsigned char packet[] = {
1279 // public flags (8 byte connection_id)
1280 0x3C,
1281 // connection_id
1282 0x10, 0x32, 0x54, 0x76, 0x98, 0xBA, 0xDC, 0xFE,
1283 // packet number
1284 0xBC, 0x9A, 0x78, 0x56, 0x34, 0x12,
1285 // private flags
1286 0x00,
1288 // frame type (stream frame with fin)
1289 0xFD,
1290 // stream id
1291 0x04, 0x03,
1292 // offset
1293 0x54, 0x76, 0x10, 0x32, 0xDC, 0xFE, 0x98, 0xBA,
1294 // data length
1295 0x0c, 0x00,
1296 // data
1297 'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', '!',
1300 QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
1301 EXPECT_TRUE(framer_.ProcessPacket(encrypted));
1303 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
1304 ASSERT_TRUE(visitor_.header_.get());
1305 EXPECT_TRUE(CheckDecryption(encrypted, !kIncludeVersion));
1307 ASSERT_EQ(1u, visitor_.stream_frames_.size());
1308 EXPECT_EQ(0u, visitor_.ack_frames_.size());
1309 EXPECT_EQ(static_cast<uint64>(0x00000304),
1310 visitor_.stream_frames_[0]->stream_id);
1311 EXPECT_TRUE(visitor_.stream_frames_[0]->fin);
1312 EXPECT_EQ(UINT64_C(0xBA98FEDC32107654), visitor_.stream_frames_[0]->offset);
1313 CheckStreamFrameData("hello world!", visitor_.stream_frames_[0]);
1315 // Now test framing boundaries.
1316 const size_t stream_id_size = 2;
1317 CheckStreamFrameBoundaries(packet, stream_id_size, !kIncludeVersion);
1320 TEST_P(QuicFramerTest, StreamFrame1ByteStreamId) {
1321 unsigned char packet[] = {
1322 // public flags (8 byte connection_id)
1323 0x3C,
1324 // connection_id
1325 0x10, 0x32, 0x54, 0x76, 0x98, 0xBA, 0xDC, 0xFE,
1326 // packet number
1327 0xBC, 0x9A, 0x78, 0x56, 0x34, 0x12,
1328 // private flags
1329 0x00,
1331 // frame type (stream frame with fin)
1332 0xFC,
1333 // stream id
1334 0x04,
1335 // offset
1336 0x54, 0x76, 0x10, 0x32, 0xDC, 0xFE, 0x98, 0xBA,
1337 // data length
1338 0x0c, 0x00,
1339 // data
1340 'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', '!',
1343 QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
1344 EXPECT_TRUE(framer_.ProcessPacket(encrypted));
1346 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
1347 ASSERT_TRUE(visitor_.header_.get());
1348 EXPECT_TRUE(CheckDecryption(encrypted, !kIncludeVersion));
1350 ASSERT_EQ(1u, visitor_.stream_frames_.size());
1351 EXPECT_EQ(0u, visitor_.ack_frames_.size());
1352 EXPECT_EQ(static_cast<uint64>(0x00000004),
1353 visitor_.stream_frames_[0]->stream_id);
1354 EXPECT_TRUE(visitor_.stream_frames_[0]->fin);
1355 EXPECT_EQ(UINT64_C(0xBA98FEDC32107654), visitor_.stream_frames_[0]->offset);
1356 CheckStreamFrameData("hello world!", visitor_.stream_frames_[0]);
1358 // Now test framing boundaries.
1359 const size_t stream_id_size = 1;
1360 CheckStreamFrameBoundaries(packet, stream_id_size, !kIncludeVersion);
1363 TEST_P(QuicFramerTest, StreamFrameWithVersion) {
1364 unsigned char packet[] = {
1365 // public flags (version, 8 byte connection_id)
1366 0x3D,
1367 // connection_id
1368 0x10, 0x32, 0x54, 0x76, 0x98, 0xBA, 0xDC, 0xFE,
1369 // version tag
1370 'Q', '0', GetQuicVersionDigitTens(), GetQuicVersionDigitOnes(),
1371 // packet number
1372 0xBC, 0x9A, 0x78, 0x56, 0x34, 0x12,
1373 // private flags
1374 0x00,
1376 // frame type (stream frame with fin)
1377 0xFF,
1378 // stream id
1379 0x04, 0x03, 0x02, 0x01,
1380 // offset
1381 0x54, 0x76, 0x10, 0x32, 0xDC, 0xFE, 0x98, 0xBA,
1382 // data length
1383 0x0c, 0x00,
1384 // data
1385 'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', '!',
1388 QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
1389 EXPECT_TRUE(framer_.ProcessPacket(encrypted));
1391 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
1392 ASSERT_TRUE(visitor_.header_.get());
1393 EXPECT_TRUE(visitor_.header_->public_header.version_flag);
1394 EXPECT_EQ(GetParam(), visitor_.header_->public_header.versions[0]);
1395 EXPECT_TRUE(CheckDecryption(encrypted, kIncludeVersion));
1397 ASSERT_EQ(1u, visitor_.stream_frames_.size());
1398 EXPECT_EQ(0u, visitor_.ack_frames_.size());
1399 EXPECT_EQ(static_cast<uint64>(0x01020304),
1400 visitor_.stream_frames_[0]->stream_id);
1401 EXPECT_TRUE(visitor_.stream_frames_[0]->fin);
1402 EXPECT_EQ(UINT64_C(0xBA98FEDC32107654), visitor_.stream_frames_[0]->offset);
1403 CheckStreamFrameData("hello world!", visitor_.stream_frames_[0]);
1405 // Now test framing boundaries.
1406 CheckStreamFrameBoundaries(packet, kQuicMaxStreamIdSize, kIncludeVersion);
1409 TEST_P(QuicFramerTest, RejectPacket) {
1410 visitor_.accept_packet_ = false;
1412 unsigned char packet[] = {
1413 // public flags (8 byte connection_id)
1414 0x3C,
1415 // connection_id
1416 0x10, 0x32, 0x54, 0x76, 0x98, 0xBA, 0xDC, 0xFE,
1417 // packet number
1418 0xBC, 0x9A, 0x78, 0x56, 0x34, 0x12,
1419 // private flags
1420 0x00,
1422 // frame type (stream frame with fin)
1423 0xFF,
1424 // stream id
1425 0x04, 0x03, 0x02, 0x01,
1426 // offset
1427 0x54, 0x76, 0x10, 0x32, 0xDC, 0xFE, 0x98, 0xBA,
1428 // data length
1429 0x0c, 0x00,
1430 // data
1431 'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', '!',
1434 QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
1435 EXPECT_TRUE(framer_.ProcessPacket(encrypted));
1437 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
1438 ASSERT_TRUE(visitor_.header_.get());
1439 EXPECT_TRUE(CheckDecryption(encrypted, !kIncludeVersion));
1441 ASSERT_EQ(0u, visitor_.stream_frames_.size());
1442 EXPECT_EQ(0u, visitor_.ack_frames_.size());
1445 TEST_P(QuicFramerTest, RejectPublicHeader) {
1446 visitor_.accept_public_header_ = false;
1448 unsigned char packet[] = {
1449 // public flags (8 byte connection_id)
1450 0x3C,
1451 // connection_id
1452 0x10, 0x32, 0x54, 0x76,
1453 0x98, 0xBA, 0xDC, 0xFE,
1456 QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
1457 EXPECT_TRUE(framer_.ProcessPacket(encrypted));
1459 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
1460 ASSERT_TRUE(visitor_.public_header_.get());
1461 ASSERT_FALSE(visitor_.header_.get());
1464 TEST_P(QuicFramerTest, RevivedStreamFrame) {
1465 unsigned char payload[] = {
1466 // frame type (stream frame with fin)
1467 0xFF,
1468 // stream id
1469 0x04, 0x03, 0x02, 0x01,
1470 // offset
1471 0x54, 0x76, 0x10, 0x32,
1472 0xDC, 0xFE, 0x98, 0xBA,
1473 // data length
1474 0x0c, 0x00,
1475 // data
1476 'h', 'e', 'l', 'l',
1477 'o', ' ', 'w', 'o',
1478 'r', 'l', 'd', '!',
1481 QuicPacketHeader header;
1482 header.public_header.connection_id = UINT64_C(0xFEDCBA9876543210);
1483 header.public_header.reset_flag = false;
1484 header.public_header.version_flag = false;
1485 header.fec_flag = true;
1486 header.entropy_flag = true;
1487 header.packet_packet_number = UINT64_C(0x123456789ABC);
1488 header.fec_group = 0;
1490 // Do not encrypt the payload because the revived payload is post-encryption.
1491 EXPECT_TRUE(framer_.ProcessRevivedPacket(&header,
1492 StringPiece(AsChars(payload),
1493 arraysize(payload))));
1495 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
1496 ASSERT_EQ(1, visitor_.revived_packets_);
1497 ASSERT_TRUE(visitor_.header_.get());
1498 EXPECT_EQ(UINT64_C(0xFEDCBA9876543210),
1499 visitor_.header_->public_header.connection_id);
1500 EXPECT_FALSE(visitor_.header_->public_header.reset_flag);
1501 EXPECT_FALSE(visitor_.header_->public_header.version_flag);
1502 EXPECT_TRUE(visitor_.header_->fec_flag);
1503 EXPECT_TRUE(visitor_.header_->entropy_flag);
1504 EXPECT_EQ(1 << (header.packet_packet_number % 8),
1505 visitor_.header_->entropy_hash);
1506 EXPECT_EQ(UINT64_C(0x123456789ABC), visitor_.header_->packet_packet_number);
1507 EXPECT_EQ(NOT_IN_FEC_GROUP, visitor_.header_->is_in_fec_group);
1508 EXPECT_EQ(0x00u, visitor_.header_->fec_group);
1510 ASSERT_EQ(1u, visitor_.stream_frames_.size());
1511 EXPECT_EQ(0u, visitor_.ack_frames_.size());
1512 EXPECT_EQ(UINT64_C(0x01020304), visitor_.stream_frames_[0]->stream_id);
1513 EXPECT_TRUE(visitor_.stream_frames_[0]->fin);
1514 EXPECT_EQ(UINT64_C(0xBA98FEDC32107654), visitor_.stream_frames_[0]->offset);
1515 CheckStreamFrameData("hello world!", visitor_.stream_frames_[0]);
1518 TEST_P(QuicFramerTest, StreamFrameInFecGroup) {
1519 unsigned char packet[] = {
1520 // public flags (8 byte connection_id)
1521 0x3C,
1522 // connection_id
1523 0x10, 0x32, 0x54, 0x76, 0x98, 0xBA, 0xDC, 0xFE,
1524 // packet number
1525 0xBC, 0x9A, 0x78, 0x56, 0x12, 0x34,
1526 // private flags (fec group)
1527 0x02,
1528 // first fec protected packet offset
1529 0x02,
1531 // frame type (stream frame with fin)
1532 0xFF,
1533 // stream id
1534 0x04, 0x03, 0x02, 0x01,
1535 // offset
1536 0x54, 0x76, 0x10, 0x32, 0xDC, 0xFE, 0x98, 0xBA,
1537 // data length
1538 0x0c, 0x00,
1539 // data
1540 'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', '!',
1543 QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
1544 EXPECT_TRUE(framer_.ProcessPacket(encrypted));
1546 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
1547 ASSERT_TRUE(visitor_.header_.get());
1548 EXPECT_TRUE(CheckDecryption(encrypted, !kIncludeVersion));
1549 EXPECT_EQ(IN_FEC_GROUP, visitor_.header_->is_in_fec_group);
1550 EXPECT_EQ(UINT64_C(0x341256789ABA), visitor_.header_->fec_group);
1551 const size_t fec_offset = GetStartOfFecProtectedData(
1552 PACKET_8BYTE_CONNECTION_ID, !kIncludeVersion, PACKET_6BYTE_PACKET_NUMBER);
1553 EXPECT_EQ(
1554 string(AsChars(packet) + fec_offset, arraysize(packet) - fec_offset),
1555 visitor_.fec_protected_payload_);
1557 ASSERT_EQ(1u, visitor_.stream_frames_.size());
1558 EXPECT_EQ(0u, visitor_.ack_frames_.size());
1559 EXPECT_EQ(UINT64_C(0x01020304), visitor_.stream_frames_[0]->stream_id);
1560 EXPECT_TRUE(visitor_.stream_frames_[0]->fin);
1561 EXPECT_EQ(UINT64_C(0xBA98FEDC32107654), visitor_.stream_frames_[0]->offset);
1562 CheckStreamFrameData("hello world!", visitor_.stream_frames_[0]);
1565 TEST_P(QuicFramerTest, AckFrameTwoTimestamp) {
1566 unsigned char packet[] = {
1567 // public flags (8 byte connection_id)
1568 0x3C,
1569 // connection_id
1570 0x10, 0x32, 0x54, 0x76, 0x98, 0xBA, 0xDC, 0xFE,
1571 // packet number
1572 0xA8, 0x9A, 0x78, 0x56, 0x34, 0x12,
1573 // private flags (entropy)
1574 0x01,
1576 // frame type (ack frame)
1577 // (has nacks, not truncated, 6 byte largest observed, 1 byte delta)
1578 0x6C,
1579 // entropy hash of all received packets.
1580 0xBA,
1581 // largest observed packet number
1582 0xBF, 0x9A, 0x78, 0x56, 0x34, 0x12,
1583 // Zero delta time.
1584 0x00, 0x00,
1585 // Number of timestamps.
1586 0x02,
1587 // Delta from largest observed.
1588 0x01,
1589 // Delta time.
1590 0x10, 0x32, 0x54, 0x76,
1591 // Delta from largest observed.
1592 0x02,
1593 // Delta time.
1594 0x10, 0x32,
1595 // num missing packets
1596 0x01,
1597 // missing packet delta
1598 0x01,
1599 // 0 more missing packets in range.
1600 0x00,
1601 // Number of revived packets.
1602 0x00,
1605 QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
1606 EXPECT_TRUE(framer_.ProcessPacket(encrypted));
1608 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
1609 ASSERT_TRUE(visitor_.header_.get());
1610 EXPECT_TRUE(CheckDecryption(encrypted, !kIncludeVersion));
1612 EXPECT_EQ(0u, visitor_.stream_frames_.size());
1613 ASSERT_EQ(1u, visitor_.ack_frames_.size());
1614 const QuicAckFrame& frame = *visitor_.ack_frames_[0];
1615 EXPECT_EQ(0xBA, frame.entropy_hash);
1616 EXPECT_EQ(UINT64_C(0x0123456789ABF), frame.largest_observed);
1617 ASSERT_EQ(1u, frame.missing_packets.size());
1618 ASSERT_EQ(2u, frame.received_packet_times.size());
1619 PacketNumberSet::const_iterator missing_iter = frame.missing_packets.begin();
1620 EXPECT_EQ(UINT64_C(0x0123456789ABE), *missing_iter);
1622 const size_t kReceivedEntropyOffset = kQuicFrameTypeSize;
1623 const size_t kLargestObservedOffset = kReceivedEntropyOffset +
1624 kQuicEntropyHashSize;
1625 const size_t kMissingDeltaTimeOffset =
1626 kLargestObservedOffset + PACKET_6BYTE_PACKET_NUMBER;
1627 const size_t kNumTimestampsOffset = kMissingDeltaTimeOffset +
1628 kQuicDeltaTimeLargestObservedSize;
1629 const size_t kTimestampDeltaLargestObserved1 = kNumTimestampsOffset +
1630 kQuicNumTimestampsSize;
1631 const size_t kTimestampTimeDeltaLargestObserved1 =
1632 kTimestampDeltaLargestObserved1 + 1;
1633 const size_t kTimestampDeltaLargestObserved2 =
1634 kTimestampTimeDeltaLargestObserved1 + 4;
1635 const size_t kTimestampTimeDeltaLargestObserved2 =
1636 kTimestampDeltaLargestObserved2 + 1;
1637 const size_t kNumMissingPacketOffset =
1638 kTimestampTimeDeltaLargestObserved2 + 2;
1639 const size_t kMissingPacketsOffset = kNumMissingPacketOffset +
1640 kNumberOfNackRangesSize;
1641 const size_t kMissingPacketsRange =
1642 kMissingPacketsOffset + PACKET_1BYTE_PACKET_NUMBER;
1643 const size_t kRevivedPacketsLength =
1644 kMissingPacketsRange + PACKET_1BYTE_PACKET_NUMBER;
1645 // Now test framing boundaries.
1646 const size_t ack_frame_size =
1647 kRevivedPacketsLength + PACKET_1BYTE_PACKET_NUMBER;
1648 for (size_t i = kQuicFrameTypeSize; i < ack_frame_size; ++i) {
1649 string expected_error;
1650 if (i < kLargestObservedOffset) {
1651 expected_error = "Unable to read entropy hash for received packets.";
1652 } else if (i < kMissingDeltaTimeOffset) {
1653 expected_error = "Unable to read largest observed.";
1654 } else if (i < kNumTimestampsOffset) {
1655 expected_error = "Unable to read delta time largest observed.";
1656 } else if (i < kTimestampDeltaLargestObserved1) {
1657 expected_error = "Unable to read num received packets.";
1658 } else if (i < kTimestampTimeDeltaLargestObserved1) {
1659 expected_error = "Unable to read sequence delta in received packets.";
1660 } else if (i < kTimestampDeltaLargestObserved2) {
1661 expected_error = "Unable to read time delta in received packets.";
1662 } else if (i < kTimestampTimeDeltaLargestObserved2) {
1663 expected_error = "Unable to read sequence delta in received packets.";
1664 } else if (i < kNumMissingPacketOffset) {
1665 expected_error =
1666 "Unable to read incremental time delta in received packets.";
1667 } else if (i < kMissingPacketsOffset) {
1668 expected_error = "Unable to read num missing packet ranges.";
1669 } else if (i < kMissingPacketsRange) {
1670 expected_error = "Unable to read missing packet number delta.";
1671 } else if (i < kRevivedPacketsLength) {
1672 expected_error = "Unable to read missing packet number range.";
1673 } else {
1674 expected_error = "Unable to read num revived packets.";
1676 CheckProcessingFails(
1677 packet,
1678 i + GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID, !kIncludeVersion,
1679 PACKET_6BYTE_PACKET_NUMBER, NOT_IN_FEC_GROUP),
1680 expected_error, QUIC_INVALID_ACK_DATA);
1685 TEST_P(QuicFramerTest, AckFrameOneTimestamp) {
1686 unsigned char packet[] = {
1687 // public flags (8 byte connection_id)
1688 0x3C,
1689 // connection_id
1690 0x10, 0x32, 0x54, 0x76, 0x98, 0xBA, 0xDC, 0xFE,
1691 // packet number
1692 0xA8, 0x9A, 0x78, 0x56, 0x34, 0x12,
1693 // private flags (entropy)
1694 0x01,
1696 // frame type (ack frame)
1697 // (has nacks, not truncated, 6 byte largest observed, 1 byte delta)
1698 0x6C,
1699 // entropy hash of all received packets.
1700 0xBA,
1701 // largest observed packet number
1702 0xBF, 0x9A, 0x78, 0x56, 0x34, 0x12,
1703 // Zero delta time.
1704 0x00, 0x00,
1705 // Number of timestamps.
1706 0x01,
1707 // Delta from largest observed.
1708 0x01,
1709 // Delta time.
1710 0x10, 0x32, 0x54, 0x76,
1711 // num missing packets
1712 0x01,
1713 // missing packet delta
1714 0x01,
1715 // 0 more missing packets in range.
1716 0x00,
1717 // Number of revived packets.
1718 0x00,
1721 QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
1722 EXPECT_TRUE(framer_.ProcessPacket(encrypted));
1724 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
1725 ASSERT_TRUE(visitor_.header_.get());
1726 EXPECT_TRUE(CheckDecryption(encrypted, !kIncludeVersion));
1728 EXPECT_EQ(0u, visitor_.stream_frames_.size());
1729 ASSERT_EQ(1u, visitor_.ack_frames_.size());
1730 const QuicAckFrame& frame = *visitor_.ack_frames_[0];
1731 EXPECT_EQ(0xBA, frame.entropy_hash);
1732 EXPECT_EQ(UINT64_C(0x0123456789ABF), frame.largest_observed);
1733 ASSERT_EQ(1u, frame.missing_packets.size());
1734 ASSERT_EQ(1u, frame.received_packet_times.size());
1735 PacketNumberSet::const_iterator missing_iter = frame.missing_packets.begin();
1736 EXPECT_EQ(UINT64_C(0x0123456789ABE), *missing_iter);
1738 const size_t kReceivedEntropyOffset = kQuicFrameTypeSize;
1739 const size_t kLargestObservedOffset = kReceivedEntropyOffset +
1740 kQuicEntropyHashSize;
1741 const size_t kMissingDeltaTimeOffset =
1742 kLargestObservedOffset + PACKET_6BYTE_PACKET_NUMBER;
1743 const size_t kNumTimestampsOffset = kMissingDeltaTimeOffset +
1744 kQuicDeltaTimeLargestObservedSize;
1745 const size_t kTimestampDeltaLargestObserved = kNumTimestampsOffset +
1746 kQuicNumTimestampsSize;
1747 const size_t kTimestampTimeDeltaLargestObserved =
1748 kTimestampDeltaLargestObserved + 1;
1749 const size_t kNumMissingPacketOffset = kTimestampTimeDeltaLargestObserved + 4;
1750 const size_t kMissingPacketsOffset = kNumMissingPacketOffset +
1751 kNumberOfNackRangesSize;
1752 const size_t kMissingPacketsRange =
1753 kMissingPacketsOffset + PACKET_1BYTE_PACKET_NUMBER;
1754 const size_t kRevivedPacketsLength =
1755 kMissingPacketsRange + PACKET_1BYTE_PACKET_NUMBER;
1756 // Now test framing boundaries.
1757 const size_t ack_frame_size =
1758 kRevivedPacketsLength + PACKET_1BYTE_PACKET_NUMBER;
1759 for (size_t i = kQuicFrameTypeSize; i < ack_frame_size; ++i) {
1760 string expected_error;
1761 if (i < kLargestObservedOffset) {
1762 expected_error = "Unable to read entropy hash for received packets.";
1763 } else if (i < kMissingDeltaTimeOffset) {
1764 expected_error = "Unable to read largest observed.";
1765 } else if (i < kNumTimestampsOffset) {
1766 expected_error = "Unable to read delta time largest observed.";
1767 } else if (i < kTimestampDeltaLargestObserved) {
1768 expected_error = "Unable to read num received packets.";
1769 } else if (i < kTimestampTimeDeltaLargestObserved) {
1770 expected_error = "Unable to read sequence delta in received packets.";
1771 } else if (i < kNumMissingPacketOffset) {
1772 expected_error = "Unable to read time delta in received packets.";
1773 } else if (i < kMissingPacketsOffset) {
1774 expected_error = "Unable to read num missing packet ranges.";
1775 } else if (i < kMissingPacketsRange) {
1776 expected_error = "Unable to read missing packet number delta.";
1777 } else if (i < kRevivedPacketsLength) {
1778 expected_error = "Unable to read missing packet number range.";
1779 } else {
1780 expected_error = "Unable to read num revived packets.";
1782 CheckProcessingFails(
1783 packet,
1784 i + GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID, !kIncludeVersion,
1785 PACKET_6BYTE_PACKET_NUMBER, NOT_IN_FEC_GROUP),
1786 expected_error, QUIC_INVALID_ACK_DATA);
1791 TEST_P(QuicFramerTest, AckFrame) {
1792 unsigned char packet[] = {
1793 // public flags (8 byte connection_id)
1794 0x3C,
1795 // connection_id
1796 0x10, 0x32, 0x54, 0x76, 0x98, 0xBA, 0xDC, 0xFE,
1797 // packet number
1798 0xA8, 0x9A, 0x78, 0x56, 0x34, 0x12,
1799 // private flags (entropy)
1800 0x01,
1802 // frame type (ack frame)
1803 // (has nacks, not truncated, 6 byte largest observed, 1 byte delta)
1804 0x6C,
1805 // entropy hash of all received packets.
1806 0xBA,
1807 // largest observed packet number
1808 0xBF, 0x9A, 0x78, 0x56, 0x34, 0x12,
1809 // Zero delta time.
1810 0x00, 0x00,
1811 // Number of timestamps.
1812 0x00,
1813 // num missing packets
1814 0x01,
1815 // missing packet delta
1816 0x01,
1817 // 0 more missing packets in range.
1818 0x00,
1819 // Number of revived packets.
1820 0x00,
1823 QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
1824 EXPECT_TRUE(framer_.ProcessPacket(encrypted));
1826 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
1827 ASSERT_TRUE(visitor_.header_.get());
1828 EXPECT_TRUE(CheckDecryption(encrypted, !kIncludeVersion));
1830 EXPECT_EQ(0u, visitor_.stream_frames_.size());
1831 ASSERT_EQ(1u, visitor_.ack_frames_.size());
1832 const QuicAckFrame& frame = *visitor_.ack_frames_[0];
1833 EXPECT_EQ(0xBA, frame.entropy_hash);
1834 EXPECT_EQ(UINT64_C(0x0123456789ABF), frame.largest_observed);
1835 ASSERT_EQ(1u, frame.missing_packets.size());
1836 PacketNumberSet::const_iterator missing_iter = frame.missing_packets.begin();
1837 EXPECT_EQ(UINT64_C(0x0123456789ABE), *missing_iter);
1839 const size_t kReceivedEntropyOffset = kQuicFrameTypeSize;
1840 const size_t kLargestObservedOffset = kReceivedEntropyOffset +
1841 kQuicEntropyHashSize;
1842 const size_t kMissingDeltaTimeOffset =
1843 kLargestObservedOffset + PACKET_6BYTE_PACKET_NUMBER;
1844 const size_t kNumTimestampsOffset = kMissingDeltaTimeOffset +
1845 kQuicDeltaTimeLargestObservedSize;
1846 const size_t kNumMissingPacketOffset = kNumTimestampsOffset +
1847 kQuicNumTimestampsSize;
1848 const size_t kMissingPacketsOffset = kNumMissingPacketOffset +
1849 kNumberOfNackRangesSize;
1850 const size_t kMissingPacketsRange =
1851 kMissingPacketsOffset + PACKET_1BYTE_PACKET_NUMBER;
1852 const size_t kRevivedPacketsLength =
1853 kMissingPacketsRange + PACKET_1BYTE_PACKET_NUMBER;
1854 // Now test framing boundaries.
1855 const size_t ack_frame_size =
1856 kRevivedPacketsLength + PACKET_1BYTE_PACKET_NUMBER;
1857 for (size_t i = kQuicFrameTypeSize; i < ack_frame_size; ++i) {
1858 string expected_error;
1859 if (i < kLargestObservedOffset) {
1860 expected_error = "Unable to read entropy hash for received packets.";
1861 } else if (i < kMissingDeltaTimeOffset) {
1862 expected_error = "Unable to read largest observed.";
1863 } else if (i < kNumTimestampsOffset) {
1864 expected_error = "Unable to read delta time largest observed.";
1865 } else if (i < kNumMissingPacketOffset) {
1866 expected_error = "Unable to read num received packets.";
1867 } else if (i < kMissingPacketsOffset) {
1868 expected_error = "Unable to read num missing packet ranges.";
1869 } else if (i < kMissingPacketsRange) {
1870 expected_error = "Unable to read missing packet number delta.";
1871 } else if (i < kRevivedPacketsLength) {
1872 expected_error = "Unable to read missing packet number range.";
1873 } else {
1874 expected_error = "Unable to read num revived packets.";
1876 CheckProcessingFails(
1877 packet,
1878 i + GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID, !kIncludeVersion,
1879 PACKET_6BYTE_PACKET_NUMBER, NOT_IN_FEC_GROUP),
1880 expected_error, QUIC_INVALID_ACK_DATA);
1884 TEST_P(QuicFramerTest, AckFrameRevivedPackets) {
1885 unsigned char packet[] = {
1886 // public flags (8 byte connection_id)
1887 0x3C,
1888 // connection_id
1889 0x10, 0x32, 0x54, 0x76, 0x98, 0xBA, 0xDC, 0xFE,
1890 // packet number
1891 0xA8, 0x9A, 0x78, 0x56, 0x34, 0x12,
1892 // private flags (entropy)
1893 0x01,
1895 // frame type (ack frame)
1896 // (has nacks, not truncated, 6 byte largest observed, 1 byte delta)
1897 0x6C,
1898 // entropy hash of all received packets.
1899 0xBA,
1900 // largest observed packet number
1901 0xBF, 0x9A, 0x78, 0x56, 0x34, 0x12,
1902 // Zero delta time.
1903 0x00, 0x00,
1904 // num received packets.
1905 0x00,
1906 // num missing packets
1907 0x01,
1908 // missing packet delta
1909 0x01,
1910 // 0 more missing packets in range.
1911 0x00,
1912 // Number of revived packets.
1913 0x01,
1914 // Revived packet number.
1915 0xBE, 0x9A, 0x78, 0x56, 0x34, 0x12,
1916 // Number of revived packets.
1917 0x00,
1920 QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
1921 EXPECT_TRUE(framer_.ProcessPacket(encrypted));
1923 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
1924 ASSERT_TRUE(visitor_.header_.get());
1925 EXPECT_TRUE(CheckDecryption(encrypted, !kIncludeVersion));
1927 EXPECT_EQ(0u, visitor_.stream_frames_.size());
1928 ASSERT_EQ(1u, visitor_.ack_frames_.size());
1929 const QuicAckFrame& frame = *visitor_.ack_frames_[0];
1930 EXPECT_EQ(0xBA, frame.entropy_hash);
1931 EXPECT_EQ(UINT64_C(0x0123456789ABF), frame.largest_observed);
1932 ASSERT_EQ(1u, frame.missing_packets.size());
1933 PacketNumberSet::const_iterator missing_iter = frame.missing_packets.begin();
1934 EXPECT_EQ(UINT64_C(0x0123456789ABE), *missing_iter);
1936 const size_t kReceivedEntropyOffset = kQuicFrameTypeSize;
1937 const size_t kLargestObservedOffset = kReceivedEntropyOffset +
1938 kQuicEntropyHashSize;
1939 const size_t kMissingDeltaTimeOffset =
1940 kLargestObservedOffset + PACKET_6BYTE_PACKET_NUMBER;
1941 const size_t kNumTimestampsOffset = kMissingDeltaTimeOffset +
1942 kQuicDeltaTimeLargestObservedSize;
1943 const size_t kNumMissingPacketOffset = kNumTimestampsOffset +
1944 kQuicNumTimestampsSize;
1945 const size_t kMissingPacketsOffset = kNumMissingPacketOffset +
1946 kNumberOfNackRangesSize;
1947 const size_t kMissingPacketsRange =
1948 kMissingPacketsOffset + PACKET_1BYTE_PACKET_NUMBER;
1949 const size_t kRevivedPacketsLength =
1950 kMissingPacketsRange + PACKET_1BYTE_PACKET_NUMBER;
1951 const size_t kRevivedPacketSequenceNumberLength =
1952 kRevivedPacketsLength + PACKET_1BYTE_PACKET_NUMBER;
1953 // Now test framing boundaries.
1954 const size_t ack_frame_size =
1955 kRevivedPacketSequenceNumberLength + PACKET_6BYTE_PACKET_NUMBER;
1956 for (size_t i = kQuicFrameTypeSize; i < ack_frame_size; ++i) {
1957 string expected_error;
1958 if (i < kReceivedEntropyOffset) {
1959 expected_error = "Unable to read least unacked delta.";
1960 } else if (i < kLargestObservedOffset) {
1961 expected_error = "Unable to read entropy hash for received packets.";
1962 } else if (i < kMissingDeltaTimeOffset) {
1963 expected_error = "Unable to read largest observed.";
1964 } else if (i < kNumTimestampsOffset) {
1965 expected_error = "Unable to read delta time largest observed.";
1966 } else if (i < kNumMissingPacketOffset) {
1967 expected_error = "Unable to read num received packets.";
1968 } else if (i < kMissingPacketsOffset) {
1969 expected_error = "Unable to read num missing packet ranges.";
1970 } else if (i < kMissingPacketsRange) {
1971 expected_error = "Unable to read missing packet number delta.";
1972 } else if (i < kRevivedPacketsLength) {
1973 expected_error = "Unable to read missing packet number range.";
1974 } else if (i < kRevivedPacketSequenceNumberLength) {
1975 expected_error = "Unable to read num revived packets.";
1976 } else {
1977 expected_error = "Unable to read revived packet.";
1979 CheckProcessingFails(
1980 packet,
1981 i + GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID, !kIncludeVersion,
1982 PACKET_6BYTE_PACKET_NUMBER, NOT_IN_FEC_GROUP),
1983 expected_error, QUIC_INVALID_ACK_DATA);
1987 TEST_P(QuicFramerTest, AckFrameNoNacks) {
1988 unsigned char packet[] = {
1989 // public flags (8 byte connection_id)
1990 0x3C,
1991 // connection_id
1992 0x10, 0x32, 0x54, 0x76, 0x98, 0xBA, 0xDC, 0xFE,
1993 // packet number
1994 0xA8, 0x9A, 0x78, 0x56, 0x34, 0x12,
1995 // private flags (entropy)
1996 0x01,
1998 // frame type (ack frame)
1999 // (no nacks, not truncated, 6 byte largest observed, 1 byte delta)
2000 0x4C,
2001 // entropy hash of all received packets.
2002 0xBA,
2003 // largest observed packet number
2004 0xBF, 0x9A, 0x78, 0x56, 0x34, 0x12,
2005 // Zero delta time.
2006 0x00, 0x00,
2007 // Number of received packets.
2008 0x00,
2011 QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
2012 EXPECT_TRUE(framer_.ProcessPacket(encrypted));
2014 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
2015 ASSERT_TRUE(visitor_.header_.get());
2016 EXPECT_TRUE(CheckDecryption(encrypted, !kIncludeVersion));
2018 EXPECT_EQ(0u, visitor_.stream_frames_.size());
2019 ASSERT_EQ(1u, visitor_.ack_frames_.size());
2020 QuicAckFrame* frame = visitor_.ack_frames_[0];
2021 EXPECT_EQ(0xBA, frame->entropy_hash);
2022 EXPECT_EQ(UINT64_C(0x0123456789ABF), frame->largest_observed);
2023 ASSERT_EQ(0u, frame->missing_packets.size());
2025 // Verify that the packet re-serializes identically.
2026 QuicFrames frames;
2027 frames.push_back(QuicFrame(frame));
2028 scoped_ptr<QuicPacket> data(BuildDataPacket(*visitor_.header_, frames));
2029 ASSERT_TRUE(data != nullptr);
2031 test::CompareCharArraysWithHexError("constructed packet", data->data(),
2032 data->length(), AsChars(packet),
2033 arraysize(packet));
2036 TEST_P(QuicFramerTest, AckFrame500Nacks) {
2037 unsigned char packet[] = {
2038 // public flags (8 byte connection_id)
2039 0x3C,
2040 // connection_id
2041 0x10, 0x32, 0x54, 0x76, 0x98, 0xBA, 0xDC, 0xFE,
2042 // packet number
2043 0xA8, 0x9A, 0x78, 0x56, 0x34, 0x12,
2044 // private flags (entropy)
2045 0x01,
2047 // frame type (ack frame)
2048 // (has nacks, not truncated, 6 byte largest observed, 1 byte delta)
2049 0x6C,
2050 // entropy hash of all received packets.
2051 0xBA,
2052 // largest observed packet number
2053 0xBF, 0x9A, 0x78, 0x56, 0x34, 0x12,
2054 // Zero delta time.
2055 0x00, 0x00,
2056 // No received packets.
2057 0x00,
2058 // num missing packet ranges
2059 0x02,
2060 // missing packet delta
2061 0x01,
2062 // 243 more missing packets in range.
2063 // The ranges are listed in this order so the re-constructed packet
2064 // matches.
2065 0xF3,
2066 // No gap between ranges
2067 0x00,
2068 // 255 more missing packets in range.
2069 0xFF,
2070 // No revived packets.
2071 0x00,
2074 QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
2075 EXPECT_TRUE(framer_.ProcessPacket(encrypted));
2077 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
2078 ASSERT_TRUE(visitor_.header_.get());
2079 EXPECT_TRUE(CheckDecryption(encrypted, !kIncludeVersion));
2081 EXPECT_EQ(0u, visitor_.stream_frames_.size());
2082 ASSERT_EQ(1u, visitor_.ack_frames_.size());
2083 QuicAckFrame* frame = visitor_.ack_frames_[0];
2084 EXPECT_EQ(0xBA, frame->entropy_hash);
2085 EXPECT_EQ(UINT64_C(0x0123456789ABF), frame->largest_observed);
2086 EXPECT_EQ(0u, frame->revived_packets.size());
2087 ASSERT_EQ(500u, frame->missing_packets.size());
2088 PacketNumberSet::const_iterator first_missing_iter =
2089 frame->missing_packets.begin();
2090 EXPECT_EQ(UINT64_C(0x0123456789ABE) - 499, *first_missing_iter);
2091 PacketNumberSet::const_reverse_iterator last_missing_iter =
2092 frame->missing_packets.rbegin();
2093 EXPECT_EQ(UINT64_C(0x0123456789ABE), *last_missing_iter);
2095 // Verify that the packet re-serializes identically.
2096 QuicFrames frames;
2097 frames.push_back(QuicFrame(frame));
2098 scoped_ptr<QuicPacket> data(BuildDataPacket(*visitor_.header_, frames));
2099 ASSERT_TRUE(data != nullptr);
2101 test::CompareCharArraysWithHexError("constructed packet",
2102 data->data(), data->length(),
2103 AsChars(packet), arraysize(packet));
2106 TEST_P(QuicFramerTest, StopWaitingFrame) {
2107 unsigned char packet[] = {
2108 // public flags (8 byte connection_id)
2109 0x3C,
2110 // connection_id
2111 0x10, 0x32, 0x54, 0x76, 0x98, 0xBA, 0xDC, 0xFE,
2112 // packet number
2113 0xA8, 0x9A, 0x78, 0x56, 0x34, 0x12,
2114 // private flags (entropy)
2115 0x01,
2117 // frame type (ack frame)
2118 // (has nacks, not truncated, 6 byte largest observed, 1 byte delta)
2119 0x06,
2120 // entropy hash of sent packets till least awaiting - 1.
2121 0xAB,
2122 // least packet number awaiting an ack, delta from packet number.
2123 0x08, 0x00, 0x00, 0x00, 0x00, 0x00,
2126 QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
2127 EXPECT_TRUE(framer_.ProcessPacket(encrypted));
2129 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
2130 ASSERT_TRUE(visitor_.header_.get());
2131 EXPECT_TRUE(CheckDecryption(encrypted, !kIncludeVersion));
2133 EXPECT_EQ(0u, visitor_.stream_frames_.size());
2134 ASSERT_EQ(1u, visitor_.stop_waiting_frames_.size());
2135 const QuicStopWaitingFrame& frame = *visitor_.stop_waiting_frames_[0];
2136 EXPECT_EQ(0xAB, frame.entropy_hash);
2137 EXPECT_EQ(UINT64_C(0x0123456789AA0), frame.least_unacked);
2139 const size_t kSentEntropyOffset = kQuicFrameTypeSize;
2140 const size_t kLeastUnackedOffset = kSentEntropyOffset + kQuicEntropyHashSize;
2141 const size_t frame_size = 7;
2142 for (size_t i = kQuicFrameTypeSize; i < frame_size; ++i) {
2143 string expected_error;
2144 if (i < kLeastUnackedOffset) {
2145 expected_error = "Unable to read entropy hash for sent packets.";
2146 } else {
2147 expected_error = "Unable to read least unacked delta.";
2149 CheckProcessingFails(
2150 packet,
2151 i + GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID, !kIncludeVersion,
2152 PACKET_6BYTE_PACKET_NUMBER, NOT_IN_FEC_GROUP),
2153 expected_error, QUIC_INVALID_STOP_WAITING_DATA);
2157 TEST_P(QuicFramerTest, RstStreamFrameQuicVersion24) {
2158 if (version_ > QUIC_VERSION_24) {
2159 // QUIC_VERSION_25 removes the error_details field from QuicRstStreamFrame.
2160 return;
2163 unsigned char packet[] = {
2164 // public flags (8 byte connection_id)
2165 0x3C,
2166 // connection_id
2167 0x10, 0x32, 0x54, 0x76, 0x98, 0xBA, 0xDC, 0xFE,
2168 // packet number
2169 0xBC, 0x9A, 0x78, 0x56, 0x34, 0x12,
2170 // private flags
2171 0x00,
2173 // frame type (rst stream frame)
2174 0x01,
2175 // stream id
2176 0x04, 0x03, 0x02, 0x01,
2178 // sent byte offset
2179 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
2181 // error code
2182 0x01, 0x00, 0x00, 0x00,
2184 // error details length
2185 0x0d, 0x00,
2186 // error details
2187 'b', 'e', 'c', 'a', 'u', 's', 'e', ' ', 'I', ' ', 'c', 'a', 'n',
2190 QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
2191 EXPECT_TRUE(framer_.ProcessPacket(encrypted));
2193 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
2194 ASSERT_TRUE(visitor_.header_.get());
2195 EXPECT_TRUE(CheckDecryption(encrypted, !kIncludeVersion));
2197 EXPECT_EQ(UINT64_C(0x01020304), visitor_.rst_stream_frame_.stream_id);
2198 EXPECT_EQ(0x01, visitor_.rst_stream_frame_.error_code);
2199 EXPECT_EQ("because I can", visitor_.rst_stream_frame_.error_details);
2200 EXPECT_EQ(UINT64_C(0x0807060504030201),
2201 visitor_.rst_stream_frame_.byte_offset);
2203 // Now test framing boundaries.
2204 for (size_t i = kQuicFrameTypeSize;
2205 i < QuicFramer::GetMinRstStreamFrameSize(); ++i) {
2206 string expected_error;
2207 if (i < kQuicFrameTypeSize + kQuicMaxStreamIdSize) {
2208 expected_error = "Unable to read stream_id.";
2209 } else if (i < kQuicFrameTypeSize + kQuicMaxStreamIdSize +
2210 kQuicMaxStreamOffsetSize) {
2211 expected_error = "Unable to read rst stream sent byte offset.";
2212 } else if (i < kQuicFrameTypeSize + kQuicMaxStreamIdSize +
2213 kQuicMaxStreamOffsetSize + kQuicErrorCodeSize) {
2214 expected_error = "Unable to read rst stream error code.";
2215 } else {
2216 expected_error = "Unable to read rst stream error details.";
2218 CheckProcessingFails(
2219 packet,
2220 i + GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID, !kIncludeVersion,
2221 PACKET_6BYTE_PACKET_NUMBER, NOT_IN_FEC_GROUP),
2222 expected_error, QUIC_INVALID_RST_STREAM_DATA);
2226 TEST_P(QuicFramerTest, RstStreamFrameQuic) {
2227 if (version_ <= QUIC_VERSION_24) {
2228 // QUIC_VERSION_25 removes the error_details field from QuicRstStreamFrame.
2229 return;
2232 // clang-format off
2233 unsigned char packet[] = {
2234 // public flags (8 byte connection_id)
2235 0x3C,
2236 // connection_id
2237 0x10, 0x32, 0x54, 0x76,
2238 0x98, 0xBA, 0xDC, 0xFE,
2239 // packet number
2240 0xBC, 0x9A, 0x78, 0x56,
2241 0x34, 0x12,
2242 // private flags
2243 0x00,
2245 // frame type (rst stream frame)
2246 0x01,
2247 // stream id
2248 0x04, 0x03, 0x02, 0x01,
2250 // sent byte offset
2251 0x01, 0x02, 0x03, 0x04,
2252 0x05, 0x06, 0x07, 0x08,
2254 // error code
2255 0x01, 0x00, 0x00, 0x00,
2257 // clang-format on
2259 QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
2260 EXPECT_TRUE(framer_.ProcessPacket(encrypted));
2262 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
2263 ASSERT_TRUE(visitor_.header_.get());
2264 EXPECT_TRUE(CheckDecryption(encrypted, !kIncludeVersion));
2266 EXPECT_EQ(UINT64_C(0x01020304), visitor_.rst_stream_frame_.stream_id);
2267 EXPECT_EQ(0x01, visitor_.rst_stream_frame_.error_code);
2268 EXPECT_EQ(UINT64_C(0x0807060504030201),
2269 visitor_.rst_stream_frame_.byte_offset);
2271 // Now test framing boundaries.
2272 for (size_t i = kQuicFrameTypeSize; i < QuicFramer::GetRstStreamFrameSize();
2273 ++i) {
2274 string expected_error;
2275 if (i < kQuicFrameTypeSize + kQuicMaxStreamIdSize) {
2276 expected_error = "Unable to read stream_id.";
2277 } else if (i < kQuicFrameTypeSize + kQuicMaxStreamIdSize +
2278 kQuicMaxStreamOffsetSize) {
2279 expected_error = "Unable to read rst stream sent byte offset.";
2280 } else if (i < kQuicFrameTypeSize + kQuicMaxStreamIdSize +
2281 kQuicMaxStreamOffsetSize + kQuicErrorCodeSize) {
2282 expected_error = "Unable to read rst stream error code.";
2284 CheckProcessingFails(
2285 packet,
2286 i + GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID, !kIncludeVersion,
2287 PACKET_6BYTE_PACKET_NUMBER, NOT_IN_FEC_GROUP),
2288 expected_error, QUIC_INVALID_RST_STREAM_DATA);
2292 TEST_P(QuicFramerTest, ConnectionCloseFrame) {
2293 unsigned char packet[] = {
2294 // public flags (8 byte connection_id)
2295 0x3C,
2296 // connection_id
2297 0x10, 0x32, 0x54, 0x76, 0x98, 0xBA, 0xDC, 0xFE,
2298 // packet number
2299 0xBC, 0x9A, 0x78, 0x56, 0x34, 0x12,
2300 // private flags
2301 0x00,
2303 // frame type (connection close frame)
2304 0x02,
2305 // error code
2306 0x11, 0x00, 0x00, 0x00,
2308 // error details length
2309 0x0d, 0x00,
2310 // error details
2311 'b', 'e', 'c', 'a', 'u', 's', 'e', ' ', 'I', ' ', 'c', 'a', 'n',
2314 QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
2315 EXPECT_TRUE(framer_.ProcessPacket(encrypted));
2317 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
2318 ASSERT_TRUE(visitor_.header_.get());
2319 EXPECT_TRUE(CheckDecryption(encrypted, !kIncludeVersion));
2321 EXPECT_EQ(0u, visitor_.stream_frames_.size());
2323 EXPECT_EQ(0x11, visitor_.connection_close_frame_.error_code);
2324 EXPECT_EQ("because I can", visitor_.connection_close_frame_.error_details);
2326 ASSERT_EQ(0u, visitor_.ack_frames_.size());
2328 // Now test framing boundaries.
2329 for (size_t i = kQuicFrameTypeSize;
2330 i < QuicFramer::GetMinConnectionCloseFrameSize(); ++i) {
2331 string expected_error;
2332 if (i < kQuicFrameTypeSize + kQuicErrorCodeSize) {
2333 expected_error = "Unable to read connection close error code.";
2334 } else {
2335 expected_error = "Unable to read connection close error details.";
2337 CheckProcessingFails(
2338 packet,
2339 i + GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID, !kIncludeVersion,
2340 PACKET_6BYTE_PACKET_NUMBER, NOT_IN_FEC_GROUP),
2341 expected_error, QUIC_INVALID_CONNECTION_CLOSE_DATA);
2345 TEST_P(QuicFramerTest, GoAwayFrame) {
2346 unsigned char packet[] = {
2347 // public flags (8 byte connection_id)
2348 0x3C,
2349 // connection_id
2350 0x10, 0x32, 0x54, 0x76, 0x98, 0xBA, 0xDC, 0xFE,
2351 // packet number
2352 0xBC, 0x9A, 0x78, 0x56, 0x34, 0x12,
2353 // private flags
2354 0x00,
2356 // frame type (go away frame)
2357 0x03,
2358 // error code
2359 0x09, 0x00, 0x00, 0x00,
2360 // stream id
2361 0x04, 0x03, 0x02, 0x01,
2362 // error details length
2363 0x0d, 0x00,
2364 // error details
2365 'b', 'e', 'c', 'a', 'u', 's', 'e', ' ', 'I', ' ', 'c', 'a', 'n',
2368 QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
2369 EXPECT_TRUE(framer_.ProcessPacket(encrypted));
2371 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
2372 ASSERT_TRUE(visitor_.header_.get());
2373 EXPECT_TRUE(CheckDecryption(encrypted, !kIncludeVersion));
2375 EXPECT_EQ(UINT64_C(0x01020304), visitor_.goaway_frame_.last_good_stream_id);
2376 EXPECT_EQ(0x9, visitor_.goaway_frame_.error_code);
2377 EXPECT_EQ("because I can", visitor_.goaway_frame_.reason_phrase);
2379 const size_t reason_size = arraysize("because I can") - 1;
2380 // Now test framing boundaries.
2381 for (size_t i = kQuicFrameTypeSize;
2382 i < QuicFramer::GetMinGoAwayFrameSize() + reason_size; ++i) {
2383 string expected_error;
2384 if (i < kQuicFrameTypeSize + kQuicErrorCodeSize) {
2385 expected_error = "Unable to read go away error code.";
2386 } else if (i < kQuicFrameTypeSize + kQuicErrorCodeSize +
2387 kQuicMaxStreamIdSize) {
2388 expected_error = "Unable to read last good stream id.";
2389 } else {
2390 expected_error = "Unable to read goaway reason.";
2392 CheckProcessingFails(
2393 packet,
2394 i + GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID, !kIncludeVersion,
2395 PACKET_6BYTE_PACKET_NUMBER, NOT_IN_FEC_GROUP),
2396 expected_error, QUIC_INVALID_GOAWAY_DATA);
2400 TEST_P(QuicFramerTest, WindowUpdateFrame) {
2401 unsigned char packet[] = {
2402 // public flags (8 byte connection_id)
2403 0x3C,
2404 // connection_id
2405 0x10, 0x32, 0x54, 0x76, 0x98, 0xBA, 0xDC, 0xFE,
2406 // packet number
2407 0xBC, 0x9A, 0x78, 0x56, 0x34, 0x12,
2408 // private flags
2409 0x00,
2411 // frame type (window update frame)
2412 0x04,
2413 // stream id
2414 0x04, 0x03, 0x02, 0x01,
2415 // byte offset
2416 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c,
2419 QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
2421 EXPECT_TRUE(framer_.ProcessPacket(encrypted));
2423 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
2424 ASSERT_TRUE(visitor_.header_.get());
2425 EXPECT_TRUE(CheckDecryption(encrypted, !kIncludeVersion));
2427 EXPECT_EQ(UINT64_C(0x01020304), visitor_.window_update_frame_.stream_id);
2428 EXPECT_EQ(UINT64_C(0x0c0b0a0908070605),
2429 visitor_.window_update_frame_.byte_offset);
2431 // Now test framing boundaries.
2432 for (size_t i = kQuicFrameTypeSize;
2433 i < QuicFramer::GetWindowUpdateFrameSize(); ++i) {
2434 string expected_error;
2435 if (i < kQuicFrameTypeSize + kQuicMaxStreamIdSize) {
2436 expected_error = "Unable to read stream_id.";
2437 } else {
2438 expected_error = "Unable to read window byte_offset.";
2440 CheckProcessingFails(
2441 packet,
2442 i + GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID, !kIncludeVersion,
2443 PACKET_6BYTE_PACKET_NUMBER, NOT_IN_FEC_GROUP),
2444 expected_error, QUIC_INVALID_WINDOW_UPDATE_DATA);
2448 TEST_P(QuicFramerTest, BlockedFrame) {
2449 unsigned char packet[] = {
2450 // public flags (8 byte connection_id)
2451 0x3C,
2452 // connection_id
2453 0x10, 0x32, 0x54, 0x76, 0x98, 0xBA, 0xDC, 0xFE,
2454 // packet number
2455 0xBC, 0x9A, 0x78, 0x56, 0x34, 0x12,
2456 // private flags
2457 0x00,
2459 // frame type (blocked frame)
2460 0x05,
2461 // stream id
2462 0x04, 0x03, 0x02, 0x01,
2465 QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
2467 EXPECT_TRUE(framer_.ProcessPacket(encrypted));
2469 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
2470 ASSERT_TRUE(visitor_.header_.get());
2471 EXPECT_TRUE(CheckDecryption(encrypted, !kIncludeVersion));
2473 EXPECT_EQ(UINT64_C(0x01020304), visitor_.blocked_frame_.stream_id);
2475 // Now test framing boundaries.
2476 for (size_t i = kQuicFrameTypeSize; i < QuicFramer::GetBlockedFrameSize();
2477 ++i) {
2478 string expected_error = "Unable to read stream_id.";
2479 CheckProcessingFails(
2480 packet,
2481 i + GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID, !kIncludeVersion,
2482 PACKET_6BYTE_PACKET_NUMBER, NOT_IN_FEC_GROUP),
2483 expected_error, QUIC_INVALID_BLOCKED_DATA);
2487 TEST_P(QuicFramerTest, PingFrame) {
2488 unsigned char packet[] = {
2489 // public flags (8 byte connection_id)
2490 0x3C,
2491 // connection_id
2492 0x10, 0x32, 0x54, 0x76, 0x98, 0xBA, 0xDC, 0xFE,
2493 // packet number
2494 0xBC, 0x9A, 0x78, 0x56, 0x34, 0x12,
2495 // private flags
2496 0x00,
2498 // frame type (ping frame)
2499 0x07,
2502 QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
2503 EXPECT_TRUE(framer_.ProcessPacket(encrypted));
2505 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
2506 ASSERT_TRUE(visitor_.header_.get());
2507 EXPECT_TRUE(CheckDecryption(encrypted, !kIncludeVersion));
2509 EXPECT_EQ(1u, visitor_.ping_frames_.size());
2511 // No need to check the PING frame boundaries because it has no payload.
2514 TEST_P(QuicFramerTest, PublicResetPacket) {
2515 unsigned char packet[] = {
2516 // public flags (public reset, 8 byte connection_id)
2517 0x0E,
2518 // connection_id
2519 0x10, 0x32, 0x54, 0x76, 0x98, 0xBA, 0xDC, 0xFE,
2520 // message tag (kPRST)
2521 'P', 'R', 'S', 'T',
2522 // num_entries (2) + padding
2523 0x02, 0x00, 0x00, 0x00,
2524 // tag kRNON
2525 'R', 'N', 'O', 'N',
2526 // end offset 8
2527 0x08, 0x00, 0x00, 0x00,
2528 // tag kRSEQ
2529 'R', 'S', 'E', 'Q',
2530 // end offset 16
2531 0x10, 0x00, 0x00, 0x00,
2532 // nonce proof
2533 0x89, 0x67, 0x45, 0x23, 0x01, 0xEF, 0xCD, 0xAB,
2534 // rejected packet number
2535 0xBC, 0x9A, 0x78, 0x56, 0x34, 0x12, 0x00, 0x00,
2538 QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
2539 EXPECT_TRUE(framer_.ProcessPacket(encrypted));
2540 ASSERT_EQ(QUIC_NO_ERROR, framer_.error());
2541 ASSERT_TRUE(visitor_.public_reset_packet_.get());
2542 EXPECT_EQ(UINT64_C(0xFEDCBA9876543210),
2543 visitor_.public_reset_packet_->public_header.connection_id);
2544 EXPECT_TRUE(visitor_.public_reset_packet_->public_header.reset_flag);
2545 EXPECT_FALSE(visitor_.public_reset_packet_->public_header.version_flag);
2546 EXPECT_EQ(UINT64_C(0xABCDEF0123456789),
2547 visitor_.public_reset_packet_->nonce_proof);
2548 EXPECT_EQ(UINT64_C(0x123456789ABC),
2549 visitor_.public_reset_packet_->rejected_packet_number);
2550 EXPECT_TRUE(
2551 visitor_.public_reset_packet_->client_address.address().empty());
2553 // Now test framing boundaries.
2554 for (size_t i = 0; i < arraysize(packet); ++i) {
2555 string expected_error;
2556 DVLOG(1) << "iteration: " << i;
2557 if (i < kConnectionIdOffset) {
2558 expected_error = "Unable to read public flags.";
2559 CheckProcessingFails(packet, i, expected_error,
2560 QUIC_INVALID_PACKET_HEADER);
2561 } else if (i < kPublicResetPacketMessageTagOffset) {
2562 expected_error = "Unable to read ConnectionId.";
2563 CheckProcessingFails(packet, i, expected_error,
2564 QUIC_INVALID_PACKET_HEADER);
2565 } else {
2566 expected_error = "Unable to read reset message.";
2567 CheckProcessingFails(packet, i, expected_error,
2568 QUIC_INVALID_PUBLIC_RST_PACKET);
2573 TEST_P(QuicFramerTest, PublicResetPacketWithTrailingJunk) {
2574 unsigned char packet[] = {
2575 // public flags (public reset, 8 byte connection_id)
2576 0x0E,
2577 // connection_id
2578 0x10, 0x32, 0x54, 0x76, 0x98, 0xBA, 0xDC, 0xFE,
2579 // message tag (kPRST)
2580 'P', 'R', 'S', 'T',
2581 // num_entries (2) + padding
2582 0x02, 0x00, 0x00, 0x00,
2583 // tag kRNON
2584 'R', 'N', 'O', 'N',
2585 // end offset 8
2586 0x08, 0x00, 0x00, 0x00,
2587 // tag kRSEQ
2588 'R', 'S', 'E', 'Q',
2589 // end offset 16
2590 0x10, 0x00, 0x00, 0x00,
2591 // nonce proof
2592 0x89, 0x67, 0x45, 0x23, 0x01, 0xEF, 0xCD, 0xAB,
2593 // rejected packet number
2594 0xBC, 0x9A, 0x78, 0x56, 0x34, 0x12, 0x00, 0x00,
2595 // trailing junk
2596 'j', 'u', 'n', 'k',
2599 string expected_error = "Unable to read reset message.";
2600 CheckProcessingFails(packet, arraysize(packet), expected_error,
2601 QUIC_INVALID_PUBLIC_RST_PACKET);
2604 TEST_P(QuicFramerTest, PublicResetPacketWithClientAddress) {
2605 unsigned char packet[] = {
2606 // public flags (public reset, 8 byte connection_id)
2607 0x0E,
2608 // connection_id
2609 0x10, 0x32, 0x54, 0x76, 0x98, 0xBA, 0xDC, 0xFE,
2610 // message tag (kPRST)
2611 'P', 'R', 'S', 'T',
2612 // num_entries (3) + padding
2613 0x03, 0x00, 0x00, 0x00,
2614 // tag kRNON
2615 'R', 'N', 'O', 'N',
2616 // end offset 8
2617 0x08, 0x00, 0x00, 0x00,
2618 // tag kRSEQ
2619 'R', 'S', 'E', 'Q',
2620 // end offset 16
2621 0x10, 0x00, 0x00, 0x00,
2622 // tag kCADR
2623 'C', 'A', 'D', 'R',
2624 // end offset 24
2625 0x18, 0x00, 0x00, 0x00,
2626 // nonce proof
2627 0x89, 0x67, 0x45, 0x23, 0x01, 0xEF, 0xCD, 0xAB,
2628 // rejected packet number
2629 0xBC, 0x9A, 0x78, 0x56, 0x34, 0x12, 0x00, 0x00,
2630 // client address: 4.31.198.44:443
2631 0x02, 0x00, 0x04, 0x1F, 0xC6, 0x2C, 0xBB, 0x01,
2634 QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
2635 EXPECT_TRUE(framer_.ProcessPacket(encrypted));
2636 ASSERT_EQ(QUIC_NO_ERROR, framer_.error());
2637 ASSERT_TRUE(visitor_.public_reset_packet_.get());
2638 EXPECT_EQ(UINT64_C(0xFEDCBA9876543210),
2639 visitor_.public_reset_packet_->public_header.connection_id);
2640 EXPECT_TRUE(visitor_.public_reset_packet_->public_header.reset_flag);
2641 EXPECT_FALSE(visitor_.public_reset_packet_->public_header.version_flag);
2642 EXPECT_EQ(UINT64_C(0xABCDEF0123456789),
2643 visitor_.public_reset_packet_->nonce_proof);
2644 EXPECT_EQ(UINT64_C(0x123456789ABC),
2645 visitor_.public_reset_packet_->rejected_packet_number);
2646 EXPECT_EQ("4.31.198.44",
2647 IPAddressToString(visitor_.public_reset_packet_->
2648 client_address.address()));
2649 EXPECT_EQ(443, visitor_.public_reset_packet_->client_address.port());
2651 // Now test framing boundaries.
2652 for (size_t i = 0; i < arraysize(packet); ++i) {
2653 string expected_error;
2654 DVLOG(1) << "iteration: " << i;
2655 if (i < kConnectionIdOffset) {
2656 expected_error = "Unable to read public flags.";
2657 CheckProcessingFails(packet, i, expected_error,
2658 QUIC_INVALID_PACKET_HEADER);
2659 } else if (i < kPublicResetPacketMessageTagOffset) {
2660 expected_error = "Unable to read ConnectionId.";
2661 CheckProcessingFails(packet, i, expected_error,
2662 QUIC_INVALID_PACKET_HEADER);
2663 } else {
2664 expected_error = "Unable to read reset message.";
2665 CheckProcessingFails(packet, i, expected_error,
2666 QUIC_INVALID_PUBLIC_RST_PACKET);
2671 TEST_P(QuicFramerTest, VersionNegotiationPacket) {
2672 unsigned char packet[] = {
2673 // public flags (version, 8 byte connection_id)
2674 0x3D,
2675 // connection_id
2676 0x10, 0x32, 0x54, 0x76,
2677 0x98, 0xBA, 0xDC, 0xFE,
2678 // version tag
2679 'Q', '0', GetQuicVersionDigitTens(), GetQuicVersionDigitOnes(),
2680 'Q', '2', '.', '0',
2683 QuicFramerPeer::SetPerspective(&framer_, Perspective::IS_CLIENT);
2685 QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
2686 EXPECT_TRUE(framer_.ProcessPacket(encrypted));
2687 ASSERT_EQ(QUIC_NO_ERROR, framer_.error());
2688 ASSERT_TRUE(visitor_.version_negotiation_packet_.get());
2689 EXPECT_EQ(2u, visitor_.version_negotiation_packet_->versions.size());
2690 EXPECT_EQ(GetParam(), visitor_.version_negotiation_packet_->versions[0]);
2692 for (size_t i = 0; i <= kPublicFlagsSize + PACKET_8BYTE_CONNECTION_ID; ++i) {
2693 string expected_error;
2694 QuicErrorCode error_code = QUIC_INVALID_PACKET_HEADER;
2695 if (i < kConnectionIdOffset) {
2696 expected_error = "Unable to read public flags.";
2697 } else if (i < kVersionOffset) {
2698 expected_error = "Unable to read ConnectionId.";
2699 } else {
2700 expected_error = "Unable to read supported version in negotiation.";
2701 error_code = QUIC_INVALID_VERSION_NEGOTIATION_PACKET;
2703 CheckProcessingFails(packet, i, expected_error, error_code);
2707 TEST_P(QuicFramerTest, FecPacket) {
2708 unsigned char packet[] = {
2709 // public flags (8 byte connection_id)
2710 0x3C,
2711 // connection_id
2712 0x10, 0x32, 0x54, 0x76, 0x98, 0xBA, 0xDC, 0xFE,
2713 // packet number
2714 0xBC, 0x9A, 0x78, 0x56, 0x34, 0x12,
2715 // private flags (fec group & FEC)
2716 0x06,
2717 // first fec protected packet offset
2718 0x01,
2720 // redundancy
2721 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
2722 'p',
2725 QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
2726 EXPECT_TRUE(framer_.ProcessPacket(encrypted));
2728 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
2729 ASSERT_TRUE(visitor_.header_.get());
2730 EXPECT_TRUE(CheckDecryption(encrypted, !kIncludeVersion));
2732 EXPECT_EQ(0u, visitor_.stream_frames_.size());
2733 EXPECT_EQ(0u, visitor_.ack_frames_.size());
2734 ASSERT_EQ(1, visitor_.fec_count_);
2735 const QuicFecData& fec_data = *visitor_.fec_data_[0];
2736 EXPECT_EQ(UINT64_C(0x0123456789ABB), fec_data.fec_group);
2737 EXPECT_EQ("abcdefghijklmnop", fec_data.redundancy);
2740 TEST_P(QuicFramerTest, BuildPaddingFramePacket) {
2741 QuicPacketHeader header;
2742 header.public_header.connection_id = UINT64_C(0xFEDCBA9876543210);
2743 header.public_header.reset_flag = false;
2744 header.public_header.version_flag = false;
2745 header.fec_flag = false;
2746 header.entropy_flag = false;
2747 header.packet_packet_number = UINT64_C(0x123456789ABC);
2748 header.fec_group = 0;
2750 QuicPaddingFrame padding_frame;
2752 QuicFrames frames;
2753 frames.push_back(QuicFrame(&padding_frame));
2755 unsigned char packet[kMaxPacketSize] = {// public flags (8 byte connection_id)
2756 0x3C,
2757 // connection_id
2758 0x10, 0x32, 0x54, 0x76, 0x98, 0xBA,
2759 0xDC, 0xFE,
2760 // packet number
2761 0xBC, 0x9A, 0x78, 0x56, 0x34, 0x12,
2762 // private flags
2763 0x00,
2765 // frame type (padding frame)
2766 0x00, 0x00, 0x00, 0x00, 0x00};
2768 uint64 header_size =
2769 GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID, !kIncludeVersion,
2770 PACKET_6BYTE_PACKET_NUMBER, NOT_IN_FEC_GROUP);
2771 memset(packet + header_size + 1, 0x00, kMaxPacketSize - header_size - 1);
2773 scoped_ptr<QuicPacket> data(BuildDataPacket(header, frames));
2774 ASSERT_TRUE(data != nullptr);
2776 test::CompareCharArraysWithHexError("constructed packet",
2777 data->data(), data->length(),
2778 AsChars(packet),
2779 arraysize(packet));
2782 TEST_P(QuicFramerTest, Build4ByteSequenceNumberPaddingFramePacket) {
2783 QuicPacketHeader header;
2784 header.public_header.connection_id = UINT64_C(0xFEDCBA9876543210);
2785 header.public_header.reset_flag = false;
2786 header.public_header.version_flag = false;
2787 header.fec_flag = false;
2788 header.entropy_flag = false;
2789 header.public_header.packet_number_length = PACKET_4BYTE_PACKET_NUMBER;
2790 header.packet_packet_number = UINT64_C(0x123456789ABC);
2791 header.fec_group = 0;
2793 QuicPaddingFrame padding_frame;
2795 QuicFrames frames;
2796 frames.push_back(QuicFrame(&padding_frame));
2798 unsigned char packet[kMaxPacketSize] = {
2799 // public flags (8 byte connection_id and 4 byte packet number)
2800 0x2C,
2801 // connection_id
2802 0x10, 0x32, 0x54, 0x76, 0x98, 0xBA, 0xDC, 0xFE,
2803 // packet number
2804 0xBC, 0x9A, 0x78, 0x56,
2805 // private flags
2806 0x00,
2808 // frame type (padding frame)
2809 0x00, 0x00, 0x00, 0x00, 0x00};
2811 uint64 header_size =
2812 GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID, !kIncludeVersion,
2813 PACKET_4BYTE_PACKET_NUMBER, NOT_IN_FEC_GROUP);
2814 memset(packet + header_size + 1, 0x00, kMaxPacketSize - header_size - 1);
2816 scoped_ptr<QuicPacket> data(BuildDataPacket(header, frames));
2817 ASSERT_TRUE(data != nullptr);
2819 test::CompareCharArraysWithHexError("constructed packet",
2820 data->data(), data->length(),
2821 AsChars(packet),
2822 arraysize(packet));
2825 TEST_P(QuicFramerTest, Build2ByteSequenceNumberPaddingFramePacket) {
2826 QuicPacketHeader header;
2827 header.public_header.connection_id = UINT64_C(0xFEDCBA9876543210);
2828 header.public_header.reset_flag = false;
2829 header.public_header.version_flag = false;
2830 header.fec_flag = false;
2831 header.entropy_flag = false;
2832 header.public_header.packet_number_length = PACKET_2BYTE_PACKET_NUMBER;
2833 header.packet_packet_number = UINT64_C(0x123456789ABC);
2834 header.fec_group = 0;
2836 QuicPaddingFrame padding_frame;
2838 QuicFrames frames;
2839 frames.push_back(QuicFrame(&padding_frame));
2841 unsigned char packet[kMaxPacketSize] = {
2842 // public flags (8 byte connection_id and 2 byte packet number)
2843 0x1C,
2844 // connection_id
2845 0x10, 0x32, 0x54, 0x76, 0x98, 0xBA, 0xDC, 0xFE,
2846 // packet number
2847 0xBC, 0x9A,
2848 // private flags
2849 0x00,
2851 // frame type (padding frame)
2852 0x00, 0x00, 0x00, 0x00, 0x00};
2854 uint64 header_size =
2855 GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID, !kIncludeVersion,
2856 PACKET_2BYTE_PACKET_NUMBER, NOT_IN_FEC_GROUP);
2857 memset(packet + header_size + 1, 0x00, kMaxPacketSize - header_size - 1);
2859 scoped_ptr<QuicPacket> data(BuildDataPacket(header, frames));
2860 ASSERT_TRUE(data != nullptr);
2862 test::CompareCharArraysWithHexError("constructed packet",
2863 data->data(), data->length(),
2864 AsChars(packet),
2865 arraysize(packet));
2868 TEST_P(QuicFramerTest, Build1ByteSequenceNumberPaddingFramePacket) {
2869 QuicPacketHeader header;
2870 header.public_header.connection_id = UINT64_C(0xFEDCBA9876543210);
2871 header.public_header.reset_flag = false;
2872 header.public_header.version_flag = false;
2873 header.fec_flag = false;
2874 header.entropy_flag = false;
2875 header.public_header.packet_number_length = PACKET_1BYTE_PACKET_NUMBER;
2876 header.packet_packet_number = UINT64_C(0x123456789ABC);
2877 header.fec_group = 0;
2879 QuicPaddingFrame padding_frame;
2881 QuicFrames frames;
2882 frames.push_back(QuicFrame(&padding_frame));
2884 unsigned char packet[kMaxPacketSize] = {
2885 // public flags (8 byte connection_id and 1 byte packet number)
2886 0x0C,
2887 // connection_id
2888 0x10, 0x32, 0x54, 0x76, 0x98, 0xBA, 0xDC, 0xFE,
2889 // packet number
2890 0xBC,
2891 // private flags
2892 0x00,
2894 // frame type (padding frame)
2895 0x00, 0x00, 0x00, 0x00, 0x00};
2897 uint64 header_size =
2898 GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID, !kIncludeVersion,
2899 PACKET_1BYTE_PACKET_NUMBER, NOT_IN_FEC_GROUP);
2900 memset(packet + header_size + 1, 0x00, kMaxPacketSize - header_size - 1);
2902 scoped_ptr<QuicPacket> data(BuildDataPacket(header, frames));
2903 ASSERT_TRUE(data != nullptr);
2905 test::CompareCharArraysWithHexError("constructed packet",
2906 data->data(), data->length(),
2907 AsChars(packet),
2908 arraysize(packet));
2911 TEST_P(QuicFramerTest, BuildStreamFramePacket) {
2912 QuicPacketHeader header;
2913 header.public_header.connection_id = UINT64_C(0xFEDCBA9876543210);
2914 header.public_header.reset_flag = false;
2915 header.public_header.version_flag = false;
2916 header.fec_flag = false;
2917 header.entropy_flag = true;
2918 header.packet_packet_number = UINT64_C(0x77123456789ABC);
2919 header.fec_group = 0;
2921 QuicStreamFrame stream_frame(0x01020304, true, UINT64_C(0xBA98FEDC32107654),
2922 StringPiece("hello world!"));
2924 QuicFrames frames;
2925 frames.push_back(QuicFrame(&stream_frame));
2927 unsigned char packet[] = {
2928 // public flags (8 byte connection_id)
2929 0x3C,
2930 // connection_id
2931 0x10, 0x32, 0x54, 0x76, 0x98, 0xBA, 0xDC, 0xFE,
2932 // packet number
2933 0xBC, 0x9A, 0x78, 0x56, 0x34, 0x12,
2934 // private flags (entropy)
2935 0x01,
2937 // frame type (stream frame with fin and no length)
2938 0xDF,
2939 // stream id
2940 0x04, 0x03, 0x02, 0x01,
2941 // offset
2942 0x54, 0x76, 0x10, 0x32, 0xDC, 0xFE, 0x98, 0xBA,
2943 // data
2944 'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', '!',
2947 scoped_ptr<QuicPacket> data(BuildDataPacket(header, frames));
2948 ASSERT_TRUE(data != nullptr);
2950 test::CompareCharArraysWithHexError("constructed packet",
2951 data->data(), data->length(),
2952 AsChars(packet), arraysize(packet));
2955 TEST_P(QuicFramerTest, BuildStreamFramePacketInFecGroup) {
2956 QuicPacketHeader header;
2957 header.public_header.connection_id = UINT64_C(0xFEDCBA9876543210);
2958 header.public_header.reset_flag = false;
2959 header.public_header.version_flag = false;
2960 header.fec_flag = false;
2961 header.entropy_flag = true;
2962 header.packet_packet_number = UINT64_C(0x77123456789ABC);
2963 header.is_in_fec_group = IN_FEC_GROUP;
2964 header.fec_group = UINT64_C(0x77123456789ABC);
2966 QuicStreamFrame stream_frame(0x01020304, true, UINT64_C(0xBA98FEDC32107654),
2967 StringPiece("hello world!"));
2969 QuicFrames frames;
2970 frames.push_back(QuicFrame(&stream_frame));
2971 unsigned char packet[] = {
2972 // public flags (8 byte connection_id)
2973 0x3C,
2974 // connection_id
2975 0x10, 0x32, 0x54, 0x76, 0x98, 0xBA, 0xDC, 0xFE,
2976 // packet number
2977 0xBC, 0x9A, 0x78, 0x56, 0x34, 0x12,
2978 // private flags (entropy, is_in_fec_group)
2979 0x03,
2980 // FEC group
2981 0x00,
2982 // frame type (stream frame with fin and data length field)
2983 0xFF,
2984 // stream id
2985 0x04, 0x03, 0x02, 0x01,
2986 // offset
2987 0x54, 0x76, 0x10, 0x32, 0xDC, 0xFE, 0x98, 0xBA,
2988 // data length (since packet is in an FEC group)
2989 0x0C, 0x00,
2990 // data
2991 'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', '!',
2994 scoped_ptr<QuicPacket> data(BuildDataPacket(header, frames));
2995 ASSERT_TRUE(data != nullptr);
2997 test::CompareCharArraysWithHexError("constructed packet",
2998 data->data(), data->length(),
2999 AsChars(packet), arraysize(packet));
3002 TEST_P(QuicFramerTest, BuildStreamFramePacketWithVersionFlag) {
3003 QuicPacketHeader header;
3004 header.public_header.connection_id = UINT64_C(0xFEDCBA9876543210);
3005 header.public_header.reset_flag = false;
3006 header.public_header.version_flag = true;
3007 header.fec_flag = false;
3008 header.entropy_flag = true;
3009 header.packet_packet_number = UINT64_C(0x77123456789ABC);
3010 header.fec_group = 0;
3012 QuicStreamFrame stream_frame(0x01020304, true, UINT64_C(0xBA98FEDC32107654),
3013 StringPiece("hello world!"));
3015 QuicFrames frames;
3016 frames.push_back(QuicFrame(&stream_frame));
3018 unsigned char packet[] = {
3019 // public flags (version, 8 byte connection_id)
3020 0x3D,
3021 // connection_id
3022 0x10, 0x32, 0x54, 0x76, 0x98, 0xBA, 0xDC, 0xFE,
3023 // version tag
3024 'Q', '0', GetQuicVersionDigitTens(), GetQuicVersionDigitOnes(),
3025 // packet number
3026 0xBC, 0x9A, 0x78, 0x56, 0x34, 0x12,
3027 // private flags (entropy)
3028 0x01,
3030 // frame type (stream frame with fin and no length)
3031 0xDF,
3032 // stream id
3033 0x04, 0x03, 0x02, 0x01,
3034 // offset
3035 0x54, 0x76, 0x10, 0x32, 0xDC, 0xFE, 0x98, 0xBA,
3036 // data
3037 'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', '!',
3040 QuicFramerPeer::SetPerspective(&framer_, Perspective::IS_CLIENT);
3041 scoped_ptr<QuicPacket> data(BuildDataPacket(header, frames));
3042 ASSERT_TRUE(data != nullptr);
3044 test::CompareCharArraysWithHexError("constructed packet",
3045 data->data(), data->length(),
3046 AsChars(packet), arraysize(packet));
3049 TEST_P(QuicFramerTest, BuildVersionNegotiationPacket) {
3050 QuicPacketPublicHeader header;
3051 header.connection_id = UINT64_C(0xFEDCBA9876543210);
3052 header.reset_flag = false;
3053 header.version_flag = true;
3055 unsigned char packet[] = {
3056 // public flags (version, 8 byte connection_id)
3057 0x0D,
3058 // connection_id
3059 0x10,
3060 0x32,
3061 0x54,
3062 0x76,
3063 0x98,
3064 0xBA,
3065 0xDC,
3066 0xFE,
3067 // version tag
3068 'Q',
3069 '0',
3070 GetQuicVersionDigitTens(),
3071 GetQuicVersionDigitOnes(),
3074 QuicVersionVector versions;
3075 versions.push_back(GetParam());
3076 scoped_ptr<QuicEncryptedPacket> data(
3077 framer_.BuildVersionNegotiationPacket(header, versions));
3079 test::CompareCharArraysWithHexError("constructed packet", data->data(),
3080 data->length(), AsChars(packet),
3081 arraysize(packet));
3084 TEST_P(QuicFramerTest, BuildAckFramePacket) {
3085 QuicPacketHeader header;
3086 header.public_header.connection_id = UINT64_C(0xFEDCBA9876543210);
3087 header.public_header.reset_flag = false;
3088 header.public_header.version_flag = false;
3089 header.fec_flag = false;
3090 header.entropy_flag = true;
3091 header.packet_packet_number = UINT64_C(0x770123456789AA8);
3092 header.fec_group = 0;
3094 QuicAckFrame ack_frame;
3095 ack_frame.entropy_hash = 0x43;
3096 ack_frame.largest_observed = UINT64_C(0x770123456789ABF);
3097 ack_frame.delta_time_largest_observed = QuicTime::Delta::Zero();
3098 ack_frame.missing_packets.insert(UINT64_C(0x770123456789ABE));
3100 QuicFrames frames;
3101 frames.push_back(QuicFrame(&ack_frame));
3103 unsigned char packet[] = {
3104 // public flags (8 byte connection_id)
3105 0x3C,
3106 // connection_id
3107 0x10, 0x32, 0x54, 0x76, 0x98, 0xBA, 0xDC, 0xFE,
3108 // packet number
3109 0xA8, 0x9A, 0x78, 0x56, 0x34, 0x12,
3110 // private flags (entropy)
3111 0x01,
3113 // frame type (ack frame)
3114 // (has nacks, not truncated, 6 byte largest observed, 1 byte delta)
3115 0x6C,
3116 // entropy hash of all received packets.
3117 0x43,
3118 // largest observed packet number
3119 0xBF, 0x9A, 0x78, 0x56, 0x34, 0x12,
3120 // Zero delta time.
3121 0x00, 0x00,
3122 // num received packets.
3123 0x00,
3124 // num missing packet ranges
3125 0x01,
3126 // missing packet delta
3127 0x01,
3128 // 0 more missing packets in range.
3129 0x00,
3130 // 0 revived packets.
3131 0x00,
3134 scoped_ptr<QuicPacket> data(BuildDataPacket(header, frames));
3135 ASSERT_TRUE(data != nullptr);
3137 test::CompareCharArraysWithHexError("constructed packet",
3138 data->data(), data->length(),
3139 AsChars(packet), arraysize(packet));
3142 // TODO(jri): Add test for tuncated packets in which the original ack frame had
3143 // revived packets. (In both the large and small packet cases below).
3145 TEST_P(QuicFramerTest, BuildTruncatedAckFrameLargePacket) {
3146 QuicPacketHeader header;
3147 header.public_header.connection_id = UINT64_C(0xFEDCBA9876543210);
3148 header.public_header.reset_flag = false;
3149 header.public_header.version_flag = false;
3150 header.fec_flag = false;
3151 header.entropy_flag = true;
3152 header.packet_packet_number = UINT64_C(0x770123456789AA8);
3153 header.fec_group = 0;
3155 QuicAckFrame ack_frame;
3156 // This entropy hash is different from what shows up in the packet below,
3157 // since entropy is recomputed by the framer on ack truncation (by
3158 // TestEntropyCalculator for this test.)
3159 ack_frame.entropy_hash = 0x43;
3160 ack_frame.largest_observed = 2 * 300;
3161 ack_frame.delta_time_largest_observed = QuicTime::Delta::Zero();
3162 for (size_t i = 1; i < 2 * 300; i += 2) {
3163 ack_frame.missing_packets.insert(i);
3166 QuicFrames frames;
3167 frames.push_back(QuicFrame(&ack_frame));
3169 unsigned char packet[] = {
3170 // public flags (8 byte connection_id)
3171 0x3C,
3172 // connection_id
3173 0x10, 0x32, 0x54, 0x76, 0x98, 0xBA, 0xDC, 0xFE,
3174 // packet number
3175 0xA8, 0x9A, 0x78, 0x56, 0x34, 0x12,
3176 // private flags (entropy)
3177 0x01,
3179 // frame type (ack frame)
3180 // (has nacks, is truncated, 2 byte largest observed, 1 byte delta)
3181 0x74,
3182 // entropy hash of all received packets, set to 1 by TestEntropyCalculator
3183 // since ack is truncated.
3184 0x01,
3185 // 2-byte largest observed packet number.
3186 // Expected to be 510 (0x1FE), since only 255 nack ranges can fit.
3187 0xFE, 0x01,
3188 // Zero delta time.
3189 0x00, 0x00,
3190 // num missing packet ranges (limited to 255 by size of this field).
3191 0xFF,
3192 // {missing packet delta, further missing packets in range}
3193 // 6 nack ranges x 42 + 3 nack ranges
3194 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3195 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3196 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3197 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3198 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3199 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3200 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3201 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3202 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3203 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3205 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3206 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3207 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3208 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3209 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3210 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3211 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3212 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3213 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3214 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3216 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3217 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3218 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3219 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3220 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3221 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3222 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3223 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3224 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3225 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3227 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3228 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3229 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3230 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3231 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3232 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3233 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3234 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3235 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3236 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3238 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3239 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3240 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3242 // 0 revived packets.
3243 0x00,
3246 scoped_ptr<QuicPacket> data(BuildDataPacket(header, frames));
3247 ASSERT_TRUE(data != nullptr);
3249 test::CompareCharArraysWithHexError("constructed packet",
3250 data->data(), data->length(),
3251 AsChars(packet), arraysize(packet));
3254 TEST_P(QuicFramerTest, BuildTruncatedAckFrameSmallPacket) {
3255 QuicPacketHeader header;
3256 header.public_header.connection_id = UINT64_C(0xFEDCBA9876543210);
3257 header.public_header.reset_flag = false;
3258 header.public_header.version_flag = false;
3259 header.fec_flag = false;
3260 header.entropy_flag = true;
3261 header.packet_packet_number = UINT64_C(0x770123456789AA8);
3262 header.fec_group = 0;
3264 QuicAckFrame ack_frame;
3265 // This entropy hash is different from what shows up in the packet below,
3266 // since entropy is recomputed by the framer on ack truncation (by
3267 // TestEntropyCalculator for this test.)
3268 ack_frame.entropy_hash = 0x43;
3269 ack_frame.largest_observed = 2 * 300;
3270 ack_frame.delta_time_largest_observed = QuicTime::Delta::Zero();
3271 for (size_t i = 1; i < 2 * 300; i += 2) {
3272 ack_frame.missing_packets.insert(i);
3275 QuicFrames frames;
3276 frames.push_back(QuicFrame(&ack_frame));
3278 unsigned char packet[] = {
3279 // public flags (8 byte connection_id)
3280 0x3C,
3281 // connection_id
3282 0x10, 0x32, 0x54, 0x76, 0x98, 0xBA, 0xDC, 0xFE,
3283 // packet number
3284 0xA8, 0x9A, 0x78, 0x56, 0x34, 0x12,
3285 // private flags (entropy)
3286 0x01,
3288 // frame type (ack frame)
3289 // (has nacks, is truncated, 2 byte largest observed, 1 byte delta)
3290 0x74,
3291 // entropy hash of all received packets, set to 1 by TestEntropyCalculator
3292 // since ack is truncated.
3293 0x01,
3294 // 2-byte largest observed packet number.
3295 // Expected to be 12 (0x0C), since only 6 nack ranges can fit.
3296 0x0C, 0x00,
3297 // Zero delta time.
3298 0x00, 0x00,
3299 // num missing packet ranges (limited to 6 by packet size of 37).
3300 0x06,
3301 // {missing packet delta, further missing packets in range}
3302 // 6 nack ranges
3303 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3304 // 0 revived packets.
3305 0x00,
3308 scoped_ptr<QuicPacket> data(BuildDataPacket(header, frames, 37u));
3309 ASSERT_TRUE(data != nullptr);
3310 // Expect 1 byte unused since at least 2 bytes are needed to fit more nacks.
3311 EXPECT_EQ(36u, data->length());
3312 test::CompareCharArraysWithHexError("constructed packet",
3313 data->data(), data->length(),
3314 AsChars(packet), arraysize(packet));
3317 TEST_P(QuicFramerTest, BuildStopWaitingPacket) {
3318 QuicPacketHeader header;
3319 header.public_header.connection_id = UINT64_C(0xFEDCBA9876543210);
3320 header.public_header.reset_flag = false;
3321 header.public_header.version_flag = false;
3322 header.fec_flag = false;
3323 header.entropy_flag = true;
3324 header.packet_packet_number = UINT64_C(0x770123456789AA8);
3325 header.fec_group = 0;
3327 QuicStopWaitingFrame stop_waiting_frame;
3328 stop_waiting_frame.entropy_hash = 0x14;
3329 stop_waiting_frame.least_unacked = UINT64_C(0x770123456789AA0);
3331 QuicFrames frames;
3332 frames.push_back(QuicFrame(&stop_waiting_frame));
3334 unsigned char packet[] = {
3335 // public flags (8 byte connection_id)
3336 0x3C,
3337 // connection_id
3338 0x10, 0x32, 0x54, 0x76, 0x98, 0xBA, 0xDC, 0xFE,
3339 // packet number
3340 0xA8, 0x9A, 0x78, 0x56, 0x34, 0x12,
3341 // private flags (entropy)
3342 0x01,
3344 // frame type (stop waiting frame)
3345 0x06,
3346 // entropy hash of sent packets till least awaiting - 1.
3347 0x14,
3348 // least packet number awaiting an ack, delta from packet number.
3349 0x08, 0x00, 0x00, 0x00, 0x00, 0x00,
3352 scoped_ptr<QuicPacket> data(BuildDataPacket(header, frames));
3353 ASSERT_TRUE(data != nullptr);
3355 test::CompareCharArraysWithHexError("constructed packet",
3356 data->data(), data->length(),
3357 AsChars(packet), arraysize(packet));
3360 TEST_P(QuicFramerTest, BuildRstFramePacketQuicVersion24) {
3361 if (version_ > QUIC_VERSION_24) {
3362 // QUIC_VERSION_25 removes the error_details field from QuicRstStreamFrame.
3363 return;
3366 QuicPacketHeader header;
3367 header.public_header.connection_id = UINT64_C(0xFEDCBA9876543210);
3368 header.public_header.reset_flag = false;
3369 header.public_header.version_flag = false;
3370 header.fec_flag = false;
3371 header.entropy_flag = false;
3372 header.packet_packet_number = UINT64_C(0x123456789ABC);
3373 header.fec_group = 0;
3375 QuicRstStreamFrame rst_frame;
3376 rst_frame.stream_id = 0x01020304;
3377 rst_frame.error_code = static_cast<QuicRstStreamErrorCode>(0x05060708);
3378 rst_frame.error_details = "because I can";
3379 rst_frame.byte_offset = 0x0807060504030201;
3381 unsigned char packet[] = {
3382 // public flags (8 byte connection_id)
3383 0x3C,
3384 // connection_id
3385 0x10, 0x32, 0x54, 0x76, 0x98, 0xBA, 0xDC, 0xFE,
3386 // packet number
3387 0xBC, 0x9A, 0x78, 0x56, 0x34, 0x12,
3388 // private flags
3389 0x00,
3391 // frame type (rst stream frame)
3392 0x01,
3393 // stream id
3394 0x04, 0x03, 0x02, 0x01,
3395 // sent byte offset
3396 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
3397 // error code
3398 0x08, 0x07, 0x06, 0x05,
3399 // error details length
3400 0x0d, 0x00,
3401 // error details
3402 'b', 'e', 'c', 'a', 'u', 's', 'e', ' ', 'I', ' ', 'c', 'a', 'n',
3405 QuicFrames frames;
3406 frames.push_back(QuicFrame(&rst_frame));
3408 scoped_ptr<QuicPacket> data(BuildDataPacket(header, frames));
3409 ASSERT_TRUE(data != nullptr);
3411 test::CompareCharArraysWithHexError("constructed packet", data->data(),
3412 data->length(), AsChars(packet),
3413 arraysize(packet));
3416 TEST_P(QuicFramerTest, BuildRstFramePacketQuic) {
3417 if (version_ <= QUIC_VERSION_24) {
3418 // QUIC_VERSION_25 removes the error_details field from QuicRstStreamFrame.
3419 return;
3422 QuicPacketHeader header;
3423 header.public_header.connection_id = UINT64_C(0xFEDCBA9876543210);
3424 header.public_header.reset_flag = false;
3425 header.public_header.version_flag = false;
3426 header.fec_flag = false;
3427 header.entropy_flag = false;
3428 header.packet_packet_number = UINT64_C(0x123456789ABC);
3429 header.fec_group = 0;
3431 QuicRstStreamFrame rst_frame;
3432 rst_frame.stream_id = 0x01020304;
3433 rst_frame.error_code = static_cast<QuicRstStreamErrorCode>(0x05060708);
3434 rst_frame.byte_offset = 0x0807060504030201;
3436 // clang-format off
3437 unsigned char packet[] = {
3438 // public flags (8 byte connection_id)
3439 0x3C,
3440 // connection_id
3441 0x10, 0x32, 0x54, 0x76,
3442 0x98, 0xBA, 0xDC, 0xFE,
3443 // packet number
3444 0xBC, 0x9A, 0x78, 0x56,
3445 0x34, 0x12,
3446 // private flags
3447 0x00,
3449 // frame type (rst stream frame)
3450 0x01,
3451 // stream id
3452 0x04, 0x03, 0x02, 0x01,
3453 // sent byte offset
3454 0x01, 0x02, 0x03, 0x04,
3455 0x05, 0x06, 0x07, 0x08,
3456 // error code
3457 0x08, 0x07, 0x06, 0x05,
3459 // clang-format on
3461 QuicFrames frames;
3462 frames.push_back(QuicFrame(&rst_frame));
3464 scoped_ptr<QuicPacket> data(BuildDataPacket(header, frames));
3465 ASSERT_TRUE(data != nullptr);
3467 test::CompareCharArraysWithHexError("constructed packet", data->data(),
3468 data->length(), AsChars(packet),
3469 arraysize(packet));
3472 TEST_P(QuicFramerTest, BuildCloseFramePacket) {
3473 QuicPacketHeader header;
3474 header.public_header.connection_id = UINT64_C(0xFEDCBA9876543210);
3475 header.public_header.reset_flag = false;
3476 header.public_header.version_flag = false;
3477 header.fec_flag = false;
3478 header.entropy_flag = true;
3479 header.packet_packet_number = UINT64_C(0x123456789ABC);
3480 header.fec_group = 0;
3482 QuicConnectionCloseFrame close_frame;
3483 close_frame.error_code = static_cast<QuicErrorCode>(0x05060708);
3484 close_frame.error_details = "because I can";
3486 QuicFrames frames;
3487 frames.push_back(QuicFrame(&close_frame));
3489 unsigned char packet[] = {
3490 // public flags (8 byte connection_id)
3491 0x3C,
3492 // connection_id
3493 0x10, 0x32, 0x54, 0x76, 0x98, 0xBA, 0xDC, 0xFE,
3494 // packet number
3495 0xBC, 0x9A, 0x78, 0x56, 0x34, 0x12,
3496 // private flags (entropy)
3497 0x01,
3499 // frame type (connection close frame)
3500 0x02,
3501 // error code
3502 0x08, 0x07, 0x06, 0x05,
3503 // error details length
3504 0x0d, 0x00,
3505 // error details
3506 'b', 'e', 'c', 'a', 'u', 's', 'e', ' ', 'I', ' ', 'c', 'a', 'n',
3509 scoped_ptr<QuicPacket> data(BuildDataPacket(header, frames));
3510 ASSERT_TRUE(data != nullptr);
3512 test::CompareCharArraysWithHexError("constructed packet",
3513 data->data(), data->length(),
3514 AsChars(packet), arraysize(packet));
3517 TEST_P(QuicFramerTest, BuildGoAwayPacket) {
3518 QuicPacketHeader header;
3519 header.public_header.connection_id = UINT64_C(0xFEDCBA9876543210);
3520 header.public_header.reset_flag = false;
3521 header.public_header.version_flag = false;
3522 header.fec_flag = false;
3523 header.entropy_flag = true;
3524 header.packet_packet_number = UINT64_C(0x123456789ABC);
3525 header.fec_group = 0;
3527 QuicGoAwayFrame goaway_frame;
3528 goaway_frame.error_code = static_cast<QuicErrorCode>(0x05060708);
3529 goaway_frame.last_good_stream_id = 0x01020304;
3530 goaway_frame.reason_phrase = "because I can";
3532 QuicFrames frames;
3533 frames.push_back(QuicFrame(&goaway_frame));
3535 unsigned char packet[] = {
3536 // public flags (8 byte connection_id)
3537 0x3C,
3538 // connection_id
3539 0x10, 0x32, 0x54, 0x76, 0x98, 0xBA, 0xDC, 0xFE,
3540 // packet number
3541 0xBC, 0x9A, 0x78, 0x56, 0x34, 0x12,
3542 // private flags(entropy)
3543 0x01,
3545 // frame type (go away frame)
3546 0x03,
3547 // error code
3548 0x08, 0x07, 0x06, 0x05,
3549 // stream id
3550 0x04, 0x03, 0x02, 0x01,
3551 // error details length
3552 0x0d, 0x00,
3553 // error details
3554 'b', 'e', 'c', 'a', 'u', 's', 'e', ' ', 'I', ' ', 'c', 'a', 'n',
3557 scoped_ptr<QuicPacket> data(BuildDataPacket(header, frames));
3558 ASSERT_TRUE(data != nullptr);
3560 test::CompareCharArraysWithHexError("constructed packet",
3561 data->data(), data->length(),
3562 AsChars(packet), arraysize(packet));
3565 TEST_P(QuicFramerTest, BuildWindowUpdatePacket) {
3566 QuicPacketHeader header;
3567 header.public_header.connection_id = UINT64_C(0xFEDCBA9876543210);
3568 header.public_header.reset_flag = false;
3569 header.public_header.version_flag = false;
3570 header.fec_flag = false;
3571 header.entropy_flag = true;
3572 header.packet_packet_number = UINT64_C(0x123456789ABC);
3573 header.fec_group = 0;
3575 QuicWindowUpdateFrame window_update_frame;
3576 window_update_frame.stream_id = 0x01020304;
3577 window_update_frame.byte_offset = 0x1122334455667788;
3579 QuicFrames frames;
3580 frames.push_back(QuicFrame(&window_update_frame));
3582 unsigned char packet[] = {
3583 // public flags (8 byte connection_id)
3584 0x3C,
3585 // connection_id
3586 0x10, 0x32, 0x54, 0x76, 0x98, 0xBA, 0xDC, 0xFE,
3587 // packet number
3588 0xBC, 0x9A, 0x78, 0x56, 0x34, 0x12,
3589 // private flags(entropy)
3590 0x01,
3592 // frame type (window update frame)
3593 0x04,
3594 // stream id
3595 0x04, 0x03, 0x02, 0x01,
3596 // byte offset
3597 0x88, 0x77, 0x66, 0x55, 0x44, 0x33, 0x22, 0x11,
3600 scoped_ptr<QuicPacket> data(BuildDataPacket(header, frames));
3601 ASSERT_TRUE(data != nullptr);
3603 test::CompareCharArraysWithHexError("constructed packet", data->data(),
3604 data->length(), AsChars(packet),
3605 arraysize(packet));
3608 TEST_P(QuicFramerTest, BuildBlockedPacket) {
3609 QuicPacketHeader header;
3610 header.public_header.connection_id = UINT64_C(0xFEDCBA9876543210);
3611 header.public_header.reset_flag = false;
3612 header.public_header.version_flag = false;
3613 header.fec_flag = false;
3614 header.entropy_flag = true;
3615 header.packet_packet_number = UINT64_C(0x123456789ABC);
3616 header.fec_group = 0;
3618 QuicBlockedFrame blocked_frame;
3619 blocked_frame.stream_id = 0x01020304;
3621 QuicFrames frames;
3622 frames.push_back(QuicFrame(&blocked_frame));
3624 unsigned char packet[] = {
3625 // public flags (8 byte connection_id)
3626 0x3C,
3627 // connection_id
3628 0x10, 0x32, 0x54, 0x76, 0x98, 0xBA, 0xDC, 0xFE,
3629 // packet number
3630 0xBC, 0x9A, 0x78, 0x56, 0x34, 0x12,
3631 // private flags(entropy)
3632 0x01,
3634 // frame type (blocked frame)
3635 0x05,
3636 // stream id
3637 0x04, 0x03, 0x02, 0x01,
3640 scoped_ptr<QuicPacket> data(BuildDataPacket(header, frames));
3641 ASSERT_TRUE(data != nullptr);
3643 test::CompareCharArraysWithHexError("constructed packet", data->data(),
3644 data->length(), AsChars(packet),
3645 arraysize(packet));
3648 TEST_P(QuicFramerTest, BuildPingPacket) {
3649 QuicPacketHeader header;
3650 header.public_header.connection_id = UINT64_C(0xFEDCBA9876543210);
3651 header.public_header.reset_flag = false;
3652 header.public_header.version_flag = false;
3653 header.fec_flag = false;
3654 header.entropy_flag = true;
3655 header.packet_packet_number = UINT64_C(0x123456789ABC);
3656 header.fec_group = 0;
3658 QuicPingFrame ping_frame;
3660 QuicFrames frames;
3661 frames.push_back(QuicFrame(&ping_frame));
3663 unsigned char packet[] = {
3664 // public flags (8 byte connection_id)
3665 0x3C,
3666 // connection_id
3667 0x10, 0x32, 0x54, 0x76, 0x98, 0xBA, 0xDC, 0xFE,
3668 // packet number
3669 0xBC, 0x9A, 0x78, 0x56, 0x34, 0x12,
3670 // private flags(entropy)
3671 0x01,
3673 // frame type (ping frame)
3674 0x07,
3677 scoped_ptr<QuicPacket> data(BuildDataPacket(header, frames));
3678 ASSERT_TRUE(data != nullptr);
3680 test::CompareCharArraysWithHexError("constructed packet", data->data(),
3681 data->length(), AsChars(packet),
3682 arraysize(packet));
3685 // Test that the MTU discovery packet is serialized correctly as a PING packet.
3686 TEST_P(QuicFramerTest, BuildMtuDiscoveryPacket) {
3687 QuicPacketHeader header;
3688 header.public_header.connection_id = UINT64_C(0xFEDCBA9876543210);
3689 header.public_header.reset_flag = false;
3690 header.public_header.version_flag = false;
3691 header.fec_flag = false;
3692 header.entropy_flag = true;
3693 header.packet_packet_number = UINT64_C(0x123456789ABC);
3694 header.fec_group = 0;
3696 QuicMtuDiscoveryFrame mtu_discovery_frame;
3698 QuicFrames frames;
3699 frames.push_back(QuicFrame(&mtu_discovery_frame));
3701 // clang-format off
3702 unsigned char packet[] = {
3703 // public flags (8 byte connection_id)
3704 0x3C,
3705 // connection_id
3706 0x10, 0x32, 0x54, 0x76,
3707 0x98, 0xBA, 0xDC, 0xFE,
3708 // packet number
3709 0xBC, 0x9A, 0x78, 0x56,
3710 0x34, 0x12,
3711 // private flags(entropy)
3712 0x01,
3714 // frame type (ping frame)
3715 0x07,
3717 // clang-format on
3719 scoped_ptr<QuicPacket> data(BuildDataPacket(header, frames));
3720 ASSERT_TRUE(data != nullptr);
3722 test::CompareCharArraysWithHexError("constructed packet", data->data(),
3723 data->length(), AsChars(packet),
3724 arraysize(packet));
3727 TEST_P(QuicFramerTest, BuildPublicResetPacket) {
3728 QuicPublicResetPacket reset_packet;
3729 reset_packet.public_header.connection_id = UINT64_C(0xFEDCBA9876543210);
3730 reset_packet.public_header.reset_flag = true;
3731 reset_packet.public_header.version_flag = false;
3732 reset_packet.rejected_packet_number = UINT64_C(0x123456789ABC);
3733 reset_packet.nonce_proof = UINT64_C(0xABCDEF0123456789);
3735 unsigned char packet[] = {
3736 // public flags (public reset, 8 byte ConnectionId)
3737 0x0E,
3738 // connection_id
3739 0x10, 0x32, 0x54, 0x76, 0x98, 0xBA, 0xDC, 0xFE,
3740 // message tag (kPRST)
3741 'P', 'R', 'S', 'T',
3742 // num_entries (2) + padding
3743 0x02, 0x00, 0x00, 0x00,
3744 // tag kRNON
3745 'R', 'N', 'O', 'N',
3746 // end offset 8
3747 0x08, 0x00, 0x00, 0x00,
3748 // tag kRSEQ
3749 'R', 'S', 'E', 'Q',
3750 // end offset 16
3751 0x10, 0x00, 0x00, 0x00,
3752 // nonce proof
3753 0x89, 0x67, 0x45, 0x23, 0x01, 0xEF, 0xCD, 0xAB,
3754 // rejected packet number
3755 0xBC, 0x9A, 0x78, 0x56, 0x34, 0x12, 0x00, 0x00,
3758 scoped_ptr<QuicEncryptedPacket> data(
3759 framer_.BuildPublicResetPacket(reset_packet));
3760 ASSERT_TRUE(data != nullptr);
3762 test::CompareCharArraysWithHexError("constructed packet",
3763 data->data(), data->length(),
3764 AsChars(packet), arraysize(packet));
3767 TEST_P(QuicFramerTest, BuildPublicResetPacketWithClientAddress) {
3768 QuicPublicResetPacket reset_packet;
3769 reset_packet.public_header.connection_id = UINT64_C(0xFEDCBA9876543210);
3770 reset_packet.public_header.reset_flag = true;
3771 reset_packet.public_header.version_flag = false;
3772 reset_packet.rejected_packet_number = UINT64_C(0x123456789ABC);
3773 reset_packet.nonce_proof = UINT64_C(0xABCDEF0123456789);
3774 reset_packet.client_address = IPEndPoint(Loopback4(), 0x1234);
3776 unsigned char packet[] = {
3777 // public flags (public reset, 8 byte ConnectionId)
3778 0x0E,
3779 // connection_id
3780 0x10, 0x32, 0x54, 0x76, 0x98, 0xBA, 0xDC, 0xFE,
3781 // message tag (kPRST)
3782 'P', 'R', 'S', 'T',
3783 // num_entries (3) + padding
3784 0x03, 0x00, 0x00, 0x00,
3785 // tag kRNON
3786 'R', 'N', 'O', 'N',
3787 // end offset 8
3788 0x08, 0x00, 0x00, 0x00,
3789 // tag kRSEQ
3790 'R', 'S', 'E', 'Q',
3791 // end offset 16
3792 0x10, 0x00, 0x00, 0x00,
3793 // tag kCADR
3794 'C', 'A', 'D', 'R',
3795 // end offset 24
3796 0x18, 0x00, 0x00, 0x00,
3797 // nonce proof
3798 0x89, 0x67, 0x45, 0x23, 0x01, 0xEF, 0xCD, 0xAB,
3799 // rejected packet number
3800 0xBC, 0x9A, 0x78, 0x56, 0x34, 0x12, 0x00, 0x00,
3801 // client address
3802 0x02, 0x00, 0x7F, 0x00, 0x00, 0x01, 0x34, 0x12,
3805 scoped_ptr<QuicEncryptedPacket> data(
3806 framer_.BuildPublicResetPacket(reset_packet));
3807 ASSERT_TRUE(data != nullptr);
3809 test::CompareCharArraysWithHexError("constructed packet",
3810 data->data(), data->length(),
3811 AsChars(packet), arraysize(packet));
3814 TEST_P(QuicFramerTest, BuildFecPacket) {
3815 QuicPacketHeader header;
3816 header.public_header.connection_id = UINT64_C(0xFEDCBA9876543210);
3817 header.public_header.reset_flag = false;
3818 header.public_header.version_flag = false;
3819 header.fec_flag = true;
3820 header.entropy_flag = true;
3821 header.packet_packet_number = (UINT64_C(0x123456789ABC));
3822 header.is_in_fec_group = IN_FEC_GROUP;
3823 header.fec_group = UINT64_C(0x123456789ABB);
3825 QuicFecData fec_data;
3826 fec_data.fec_group = 1;
3827 fec_data.redundancy = "abcdefghijklmnop";
3829 unsigned char packet[] = {
3830 // public flags (8 byte connection_id)
3831 0x3C,
3832 // connection_id
3833 0x10, 0x32, 0x54, 0x76, 0x98, 0xBA, 0xDC, 0xFE,
3834 // packet number
3835 0xBC, 0x9A, 0x78, 0x56, 0x34, 0x12,
3836 // private flags (entropy & fec group & fec packet)
3837 0x07,
3838 // first fec protected packet offset
3839 0x01,
3841 // redundancy
3842 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
3843 'p',
3846 scoped_ptr<QuicPacket> data(framer_.BuildFecPacket(header, fec_data));
3847 ASSERT_TRUE(data != nullptr);
3849 test::CompareCharArraysWithHexError("constructed packet",
3850 data->data(), data->length(),
3851 AsChars(packet), arraysize(packet));
3854 TEST_P(QuicFramerTest, EncryptPacket) {
3855 QuicPacketNumber packet_number = UINT64_C(0x123456789ABC);
3856 unsigned char packet[] = {
3857 // public flags (8 byte connection_id)
3858 0x3C,
3859 // connection_id
3860 0x10, 0x32, 0x54, 0x76, 0x98, 0xBA, 0xDC, 0xFE,
3861 // packet number
3862 0xBC, 0x9A, 0x78, 0x56, 0x34, 0x12,
3863 // private flags (fec group & fec packet)
3864 0x06,
3865 // first fec protected packet offset
3866 0x01,
3868 // redundancy
3869 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
3870 'p',
3873 scoped_ptr<QuicPacket> raw(new QuicPacket(
3874 AsChars(packet), arraysize(packet), false, PACKET_8BYTE_CONNECTION_ID,
3875 !kIncludeVersion, PACKET_6BYTE_PACKET_NUMBER));
3876 char buffer[kMaxPacketSize];
3877 scoped_ptr<QuicEncryptedPacket> encrypted(framer_.EncryptPayload(
3878 ENCRYPTION_NONE, packet_number, *raw, buffer, kMaxPacketSize));
3880 ASSERT_TRUE(encrypted.get() != nullptr);
3881 EXPECT_TRUE(CheckEncryption(packet_number, raw.get()));
3884 TEST_P(QuicFramerTest, EncryptPacketWithVersionFlag) {
3885 QuicPacketNumber packet_number = UINT64_C(0x123456789ABC);
3886 unsigned char packet[] = {
3887 // public flags (version, 8 byte connection_id)
3888 0x3D,
3889 // connection_id
3890 0x10, 0x32, 0x54, 0x76, 0x98, 0xBA, 0xDC, 0xFE,
3891 // version tag
3892 'Q', '.', '1', '0',
3893 // packet number
3894 0xBC, 0x9A, 0x78, 0x56, 0x34, 0x12,
3895 // private flags (fec group & fec flags)
3896 0x06,
3897 // first fec protected packet offset
3898 0x01,
3900 // redundancy
3901 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
3902 'p',
3905 scoped_ptr<QuicPacket> raw(new QuicPacket(
3906 AsChars(packet), arraysize(packet), false, PACKET_8BYTE_CONNECTION_ID,
3907 kIncludeVersion, PACKET_6BYTE_PACKET_NUMBER));
3908 char buffer[kMaxPacketSize];
3909 scoped_ptr<QuicEncryptedPacket> encrypted(framer_.EncryptPayload(
3910 ENCRYPTION_NONE, packet_number, *raw, buffer, kMaxPacketSize));
3912 ASSERT_TRUE(encrypted.get() != nullptr);
3913 EXPECT_TRUE(CheckEncryption(packet_number, raw.get()));
3916 TEST_P(QuicFramerTest, AckTruncationLargePacket) {
3917 QuicPacketHeader header;
3918 header.public_header.connection_id = UINT64_C(0xFEDCBA9876543210);
3919 header.public_header.reset_flag = false;
3920 header.public_header.version_flag = false;
3921 header.fec_flag = false;
3922 header.entropy_flag = false;
3923 header.packet_packet_number = UINT64_C(0x123456789ABC);
3924 header.fec_group = 0;
3926 // Create a packet with just the ack.
3927 QuicAckFrame ack_frame = MakeAckFrameWithNackRanges(300, 0u);
3928 QuicFrame frame;
3929 frame.type = ACK_FRAME;
3930 frame.ack_frame = &ack_frame;
3931 QuicFrames frames;
3932 frames.push_back(frame);
3934 // Build an ack packet with truncation due to limit in number of nack ranges.
3935 scoped_ptr<QuicPacket> raw_ack_packet(BuildDataPacket(header, frames));
3936 ASSERT_TRUE(raw_ack_packet != nullptr);
3937 char buffer[kMaxPacketSize];
3938 scoped_ptr<QuicEncryptedPacket> ack_packet(
3939 framer_.EncryptPayload(ENCRYPTION_NONE, header.packet_packet_number,
3940 *raw_ack_packet, buffer, kMaxPacketSize));
3941 // Now make sure we can turn our ack packet back into an ack frame.
3942 ASSERT_TRUE(framer_.ProcessPacket(*ack_packet));
3943 ASSERT_EQ(1u, visitor_.ack_frames_.size());
3944 QuicAckFrame& processed_ack_frame = *visitor_.ack_frames_[0];
3945 EXPECT_TRUE(processed_ack_frame.is_truncated);
3946 EXPECT_EQ(510u, processed_ack_frame.largest_observed);
3947 ASSERT_EQ(255u, processed_ack_frame.missing_packets.size());
3948 PacketNumberSet::const_iterator missing_iter =
3949 processed_ack_frame.missing_packets.begin();
3950 EXPECT_EQ(1u, *missing_iter);
3951 PacketNumberSet::const_reverse_iterator last_missing_iter =
3952 processed_ack_frame.missing_packets.rbegin();
3953 EXPECT_EQ(509u, *last_missing_iter);
3956 TEST_P(QuicFramerTest, AckTruncationSmallPacket) {
3957 QuicPacketHeader header;
3958 header.public_header.connection_id = UINT64_C(0xFEDCBA9876543210);
3959 header.public_header.reset_flag = false;
3960 header.public_header.version_flag = false;
3961 header.fec_flag = false;
3962 header.entropy_flag = false;
3963 header.packet_packet_number = UINT64_C(0x123456789ABC);
3964 header.fec_group = 0;
3966 // Create a packet with just the ack.
3967 QuicAckFrame ack_frame = MakeAckFrameWithNackRanges(300, 0u);
3968 QuicFrame frame;
3969 frame.type = ACK_FRAME;
3970 frame.ack_frame = &ack_frame;
3971 QuicFrames frames;
3972 frames.push_back(frame);
3974 // Build an ack packet with truncation due to limit in number of nack ranges.
3975 scoped_ptr<QuicPacket> raw_ack_packet(BuildDataPacket(header, frames, 500));
3976 ASSERT_TRUE(raw_ack_packet != nullptr);
3977 char buffer[kMaxPacketSize];
3978 scoped_ptr<QuicEncryptedPacket> ack_packet(
3979 framer_.EncryptPayload(ENCRYPTION_NONE, header.packet_packet_number,
3980 *raw_ack_packet, buffer, kMaxPacketSize));
3981 // Now make sure we can turn our ack packet back into an ack frame.
3982 ASSERT_TRUE(framer_.ProcessPacket(*ack_packet));
3983 ASSERT_EQ(1u, visitor_.ack_frames_.size());
3984 QuicAckFrame& processed_ack_frame = *visitor_.ack_frames_[0];
3985 EXPECT_TRUE(processed_ack_frame.is_truncated);
3986 EXPECT_EQ(476u, processed_ack_frame.largest_observed);
3987 ASSERT_EQ(238u, processed_ack_frame.missing_packets.size());
3988 PacketNumberSet::const_iterator missing_iter =
3989 processed_ack_frame.missing_packets.begin();
3990 EXPECT_EQ(1u, *missing_iter);
3991 PacketNumberSet::const_reverse_iterator last_missing_iter =
3992 processed_ack_frame.missing_packets.rbegin();
3993 EXPECT_EQ(475u, *last_missing_iter);
3996 TEST_P(QuicFramerTest, CleanTruncation) {
3997 QuicPacketHeader header;
3998 header.public_header.connection_id = UINT64_C(0xFEDCBA9876543210);
3999 header.public_header.reset_flag = false;
4000 header.public_header.version_flag = false;
4001 header.fec_flag = false;
4002 header.entropy_flag = true;
4003 header.packet_packet_number = UINT64_C(0x123456789ABC);
4004 header.fec_group = 0;
4006 QuicAckFrame ack_frame;
4007 ack_frame.largest_observed = 201;
4008 for (uint64 i = 1; i < ack_frame.largest_observed; ++i) {
4009 ack_frame.missing_packets.insert(i);
4012 // Create a packet with just the ack.
4013 QuicFrame frame;
4014 frame.type = ACK_FRAME;
4015 frame.ack_frame = &ack_frame;
4016 QuicFrames frames;
4017 frames.push_back(frame);
4019 scoped_ptr<QuicPacket> raw_ack_packet(BuildDataPacket(header, frames));
4020 ASSERT_TRUE(raw_ack_packet != nullptr);
4022 char buffer[kMaxPacketSize];
4023 scoped_ptr<QuicEncryptedPacket> ack_packet(
4024 framer_.EncryptPayload(ENCRYPTION_NONE, header.packet_packet_number,
4025 *raw_ack_packet, buffer, kMaxPacketSize));
4027 // Now make sure we can turn our ack packet back into an ack frame.
4028 ASSERT_TRUE(framer_.ProcessPacket(*ack_packet));
4030 // Test for clean truncation of the ack by comparing the length of the
4031 // original packets to the re-serialized packets.
4032 frames.clear();
4033 frame.type = ACK_FRAME;
4034 frame.ack_frame = visitor_.ack_frames_[0];
4035 frames.push_back(frame);
4037 size_t original_raw_length = raw_ack_packet->length();
4038 raw_ack_packet.reset(BuildDataPacket(header, frames));
4039 ASSERT_TRUE(raw_ack_packet != nullptr);
4040 EXPECT_EQ(original_raw_length, raw_ack_packet->length());
4041 ASSERT_TRUE(raw_ack_packet != nullptr);
4044 TEST_P(QuicFramerTest, EntropyFlagTest) {
4045 unsigned char packet[] = {
4046 // public flags (8 byte connection_id)
4047 0x3C,
4048 // connection_id
4049 0x10, 0x32, 0x54, 0x76, 0x98, 0xBA, 0xDC, 0xFE,
4050 // packet number
4051 0xBC, 0x9A, 0x78, 0x56, 0x34, 0x12,
4052 // private flags (Entropy)
4053 0x01,
4055 // frame type (stream frame with fin and no length)
4056 0xDF,
4057 // stream id
4058 0x04, 0x03, 0x02, 0x01,
4059 // offset
4060 0x54, 0x76, 0x10, 0x32, 0xDC, 0xFE, 0x98, 0xBA,
4061 // data
4062 'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', '!',
4065 QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
4066 EXPECT_TRUE(framer_.ProcessPacket(encrypted));
4067 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
4068 ASSERT_TRUE(visitor_.header_.get());
4069 EXPECT_TRUE(visitor_.header_->entropy_flag);
4070 EXPECT_EQ(1 << 4, visitor_.header_->entropy_hash);
4071 EXPECT_FALSE(visitor_.header_->fec_flag);
4074 TEST_P(QuicFramerTest, FecEntropyTest) {
4075 unsigned char packet[] = {
4076 // public flags (8 byte connection_id)
4077 0x3C,
4078 // connection_id
4079 0x10, 0x32, 0x54, 0x76, 0x98, 0xBA, 0xDC, 0xFE,
4080 // packet number
4081 0xBC, 0x9A, 0x78, 0x56, 0x34, 0x12,
4082 // private flags (Entropy & fec group & FEC)
4083 0x07,
4084 // first fec protected packet offset
4085 0xFF,
4087 // frame type (stream frame with fin and no length)
4088 0xDF,
4089 // stream id
4090 0x04, 0x03, 0x02, 0x01,
4091 // offset
4092 0x54, 0x76, 0x10, 0x32, 0xDC, 0xFE, 0x98, 0xBA,
4093 // data
4094 'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', '!',
4097 QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
4098 EXPECT_TRUE(framer_.ProcessPacket(encrypted));
4099 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
4100 ASSERT_TRUE(visitor_.header_.get());
4101 EXPECT_TRUE(visitor_.header_->fec_flag);
4102 EXPECT_TRUE(visitor_.header_->entropy_flag);
4103 EXPECT_EQ(1 << 4, visitor_.header_->entropy_hash);
4106 TEST_P(QuicFramerTest, StopPacketProcessing) {
4107 unsigned char packet[] = {
4108 // public flags (8 byte connection_id)
4109 0x3C,
4110 // connection_id
4111 0x10, 0x32, 0x54, 0x76, 0x98, 0xBA, 0xDC, 0xFE,
4112 // packet number
4113 0xBC, 0x9A, 0x78, 0x56, 0x34, 0x12,
4114 // Entropy
4115 0x01,
4117 // frame type (stream frame with fin)
4118 0xFF,
4119 // stream id
4120 0x04, 0x03, 0x02, 0x01,
4121 // offset
4122 0x54, 0x76, 0x10, 0x32, 0xDC, 0xFE, 0x98, 0xBA,
4123 // data length
4124 0x0c, 0x00,
4125 // data
4126 'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', '!',
4128 // frame type (ack frame)
4129 0x40,
4130 // entropy hash of sent packets till least awaiting - 1.
4131 0x14,
4132 // least packet number awaiting an ack
4133 0xA0, 0x9A, 0x78, 0x56, 0x34, 0x12,
4134 // entropy hash of all received packets.
4135 0x43,
4136 // largest observed packet number
4137 0xBF, 0x9A, 0x78, 0x56, 0x34, 0x12,
4138 // num missing packets
4139 0x01,
4140 // missing packet
4141 0xBE, 0x9A, 0x78, 0x56, 0x34, 0x12,
4144 MockFramerVisitor visitor;
4145 framer_.set_visitor(&visitor);
4146 EXPECT_CALL(visitor, OnPacket());
4147 EXPECT_CALL(visitor, OnPacketHeader(_));
4148 EXPECT_CALL(visitor, OnStreamFrame(_)).WillOnce(Return(false));
4149 EXPECT_CALL(visitor, OnAckFrame(_)).Times(0);
4150 EXPECT_CALL(visitor, OnPacketComplete());
4151 EXPECT_CALL(visitor, OnUnauthenticatedPublicHeader(_)).WillOnce(Return(true));
4152 EXPECT_CALL(visitor, OnUnauthenticatedHeader(_)).WillOnce(Return(true));
4153 EXPECT_CALL(visitor, OnDecryptedPacket(_));
4155 QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
4156 EXPECT_TRUE(framer_.ProcessPacket(encrypted));
4157 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
4160 static char kTestString[] = "At least 20 characters.";
4161 static QuicStreamId kTestQuicStreamId = 1;
4162 static bool ExpectedStreamFrame(const QuicStreamFrame& frame) {
4163 return frame.stream_id == kTestQuicStreamId && !frame.fin &&
4164 frame.offset == 0 && frame.data == kTestString;
4165 // FIN is hard-coded false in ConstructEncryptedPacket.
4166 // Offset 0 is hard-coded in ConstructEncryptedPacket.
4169 // Verify that the packet returned by ConstructEncryptedPacket() can be properly
4170 // parsed by the framer.
4171 TEST_P(QuicFramerTest, ConstructEncryptedPacket) {
4172 // Since we are using ConstructEncryptedPacket, we have to set the framer's
4173 // crypto to be Null.
4174 framer_.SetDecrypter(ENCRYPTION_NONE, QuicDecrypter::Create(kNULL));
4175 framer_.SetEncrypter(ENCRYPTION_NONE, QuicEncrypter::Create(kNULL));
4177 scoped_ptr<QuicEncryptedPacket> packet(ConstructEncryptedPacket(
4178 42, false, false, kTestQuicStreamId, kTestString,
4179 PACKET_8BYTE_CONNECTION_ID, PACKET_6BYTE_PACKET_NUMBER));
4181 MockFramerVisitor visitor;
4182 framer_.set_visitor(&visitor);
4183 EXPECT_CALL(visitor, OnPacket()).Times(1);
4184 EXPECT_CALL(visitor, OnUnauthenticatedPublicHeader(_))
4185 .Times(1)
4186 .WillOnce(Return(true));
4187 EXPECT_CALL(visitor, OnUnauthenticatedHeader(_))
4188 .Times(1)
4189 .WillOnce(Return(true));
4190 EXPECT_CALL(visitor, OnPacketHeader(_)).Times(1).WillOnce(Return(true));
4191 EXPECT_CALL(visitor, OnDecryptedPacket(_)).Times(1);
4192 EXPECT_CALL(visitor, OnError(_)).Times(0);
4193 EXPECT_CALL(visitor, OnStreamFrame(_)).Times(0);
4194 EXPECT_CALL(visitor, OnStreamFrame(Truly(ExpectedStreamFrame))).Times(1);
4195 EXPECT_CALL(visitor, OnAckFrame(_)).Times(0);
4196 EXPECT_CALL(visitor, OnPacketComplete()).Times(1);
4198 EXPECT_TRUE(framer_.ProcessPacket(*packet));
4199 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
4202 // Verify that the packet returned by ConstructMisFramedEncryptedPacket()
4203 // does cause the framer to return an error.
4204 TEST_P(QuicFramerTest, ConstructMisFramedEncryptedPacket) {
4205 // Since we are using ConstructEncryptedPacket, we have to set the framer's
4206 // crypto to be Null.
4207 framer_.SetDecrypter(ENCRYPTION_NONE, QuicDecrypter::Create(kNULL));
4208 framer_.SetEncrypter(ENCRYPTION_NONE, QuicEncrypter::Create(kNULL));
4210 scoped_ptr<QuicEncryptedPacket> packet(ConstructMisFramedEncryptedPacket(
4211 42, false, false, kTestQuicStreamId, kTestString,
4212 PACKET_8BYTE_CONNECTION_ID, PACKET_6BYTE_PACKET_NUMBER, nullptr));
4214 MockFramerVisitor visitor;
4215 framer_.set_visitor(&visitor);
4216 EXPECT_CALL(visitor, OnPacket()).Times(1);
4217 EXPECT_CALL(visitor, OnUnauthenticatedPublicHeader(_))
4218 .Times(1)
4219 .WillOnce(Return(true));
4220 EXPECT_CALL(visitor, OnUnauthenticatedHeader(_))
4221 .Times(1)
4222 .WillOnce(Return(true));
4223 EXPECT_CALL(visitor, OnPacketHeader(_)).Times(0);
4224 EXPECT_CALL(visitor, OnDecryptedPacket(_)).Times(1);
4225 EXPECT_CALL(visitor, OnError(_)).Times(1);
4226 EXPECT_CALL(visitor, OnStreamFrame(_)).Times(0);
4227 EXPECT_CALL(visitor, OnAckFrame(_)).Times(0);
4228 EXPECT_CALL(visitor, OnPacketComplete()).Times(0);
4230 EXPECT_FALSE(framer_.ProcessPacket(*packet));
4231 EXPECT_EQ(QUIC_INVALID_PACKET_HEADER, framer_.error());
4234 } // namespace test
4235 } // namespace net