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"
12 #include "base/containers/hash_tables.h"
13 #include "base/logging.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/port.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"
26 using base::StringPiece;
29 using std::numeric_limits;
33 using testing::Return;
39 const QuicPacketSequenceNumber kEpoch = GG_UINT64_C(1) << 48;
40 const QuicPacketSequenceNumber kMask = kEpoch - 1;
42 // Index into the connection_id offset in the header.
43 const size_t kConnectionIdOffset = kPublicFlagsSize;
44 // Index into the version string in the header. (if present).
45 const size_t kVersionOffset = kConnectionIdOffset + PACKET_8BYTE_CONNECTION_ID;
47 // Size in bytes of the stream frame fields for an arbitrary StreamID and
48 // offset and the last frame in a packet.
49 size_t GetMinStreamFrameSize() {
50 return kQuicFrameTypeSize + kQuicMaxStreamIdSize + kQuicMaxStreamOffsetSize;
53 // Index into the sequence number offset in the header.
54 size_t GetSequenceNumberOffset(QuicConnectionIdLength connection_id_length,
55 bool include_version) {
56 return kConnectionIdOffset + connection_id_length +
57 (include_version ? kQuicVersionSize : 0);
60 size_t GetSequenceNumberOffset(bool include_version) {
61 return GetSequenceNumberOffset(PACKET_8BYTE_CONNECTION_ID, include_version);
64 // Index into the private flags offset in the data packet header.
65 size_t GetPrivateFlagsOffset(QuicConnectionIdLength connection_id_length,
66 bool include_version) {
67 return GetSequenceNumberOffset(connection_id_length, include_version) +
68 PACKET_6BYTE_SEQUENCE_NUMBER;
71 size_t GetPrivateFlagsOffset(bool include_version) {
72 return GetPrivateFlagsOffset(PACKET_8BYTE_CONNECTION_ID, include_version);
75 size_t GetPrivateFlagsOffset(bool include_version,
76 QuicSequenceNumberLength sequence_number_length) {
77 return GetSequenceNumberOffset(PACKET_8BYTE_CONNECTION_ID, include_version) +
78 sequence_number_length;
81 // Index into the fec group offset in the header.
82 size_t GetFecGroupOffset(QuicConnectionIdLength connection_id_length,
83 bool include_version) {
84 return GetPrivateFlagsOffset(connection_id_length, include_version) +
88 size_t GetFecGroupOffset(bool include_version) {
89 return GetPrivateFlagsOffset(PACKET_8BYTE_CONNECTION_ID, include_version) +
93 size_t GetFecGroupOffset(bool include_version,
94 QuicSequenceNumberLength sequence_number_length) {
95 return GetPrivateFlagsOffset(include_version, sequence_number_length) +
99 // Index into the message tag of the public reset packet.
100 // Public resets always have full connection_ids.
101 const size_t kPublicResetPacketMessageTagOffset =
102 kConnectionIdOffset + PACKET_8BYTE_CONNECTION_ID;
104 class TestEncrypter : public QuicEncrypter {
106 virtual ~TestEncrypter() {}
107 virtual bool SetKey(StringPiece key) OVERRIDE {
110 virtual bool SetNoncePrefix(StringPiece nonce_prefix) OVERRIDE {
113 virtual bool Encrypt(StringPiece nonce,
114 StringPiece associated_data,
115 StringPiece plaintext,
116 unsigned char* output) OVERRIDE {
117 CHECK(false) << "Not implemented";
120 virtual QuicData* EncryptPacket(QuicPacketSequenceNumber sequence_number,
121 StringPiece associated_data,
122 StringPiece plaintext) OVERRIDE {
123 sequence_number_ = sequence_number;
124 associated_data_ = associated_data.as_string();
125 plaintext_ = plaintext.as_string();
126 return new QuicData(plaintext.data(), plaintext.length());
128 virtual size_t GetKeySize() const OVERRIDE {
131 virtual size_t GetNoncePrefixSize() const OVERRIDE {
134 virtual size_t GetMaxPlaintextSize(size_t ciphertext_size) const OVERRIDE {
135 return ciphertext_size;
137 virtual size_t GetCiphertextSize(size_t plaintext_size) const OVERRIDE {
138 return plaintext_size;
140 virtual StringPiece GetKey() const OVERRIDE {
141 return StringPiece();
143 virtual StringPiece GetNoncePrefix() const OVERRIDE {
144 return StringPiece();
146 QuicPacketSequenceNumber sequence_number_;
147 string associated_data_;
151 class TestDecrypter : public QuicDecrypter {
153 virtual ~TestDecrypter() {}
154 virtual bool SetKey(StringPiece key) OVERRIDE {
157 virtual bool SetNoncePrefix(StringPiece nonce_prefix) OVERRIDE {
160 virtual bool Decrypt(StringPiece nonce,
161 StringPiece associated_data,
162 StringPiece ciphertext,
163 unsigned char* output,
164 size_t* output_length) OVERRIDE {
165 CHECK(false) << "Not implemented";
168 virtual QuicData* DecryptPacket(QuicPacketSequenceNumber sequence_number,
169 StringPiece associated_data,
170 StringPiece ciphertext) OVERRIDE {
171 sequence_number_ = sequence_number;
172 associated_data_ = associated_data.as_string();
173 ciphertext_ = ciphertext.as_string();
174 return new QuicData(ciphertext.data(), ciphertext.length());
176 virtual StringPiece GetKey() const OVERRIDE {
177 return StringPiece();
179 virtual StringPiece GetNoncePrefix() const OVERRIDE {
180 return StringPiece();
182 QuicPacketSequenceNumber sequence_number_;
183 string associated_data_;
187 class TestQuicVisitor : public ::net::QuicFramerVisitorInterface {
191 version_mismatch_(0),
195 complete_packets_(0),
197 accept_packet_(true),
198 accept_public_header_(true) {
201 virtual ~TestQuicVisitor() {
202 STLDeleteElements(&stream_frames_);
203 STLDeleteElements(&ack_frames_);
204 STLDeleteElements(&congestion_feedback_frames_);
205 STLDeleteElements(&stop_waiting_frames_);
206 STLDeleteElements(&ping_frames_);
207 STLDeleteElements(&fec_data_);
210 virtual void OnError(QuicFramer* f) OVERRIDE {
211 DVLOG(1) << "QuicFramer Error: " << QuicUtils::ErrorToString(f->error())
212 << " (" << f->error() << ")";
216 virtual void OnPacket() OVERRIDE {}
218 virtual void OnPublicResetPacket(
219 const QuicPublicResetPacket& packet) OVERRIDE {
220 public_reset_packet_.reset(new QuicPublicResetPacket(packet));
223 virtual void OnVersionNegotiationPacket(
224 const QuicVersionNegotiationPacket& packet) OVERRIDE {
225 version_negotiation_packet_.reset(new QuicVersionNegotiationPacket(packet));
228 virtual void OnRevivedPacket() OVERRIDE {
232 virtual bool OnProtocolVersionMismatch(QuicVersion version) OVERRIDE {
233 DVLOG(1) << "QuicFramer Version Mismatch, version: " << version;
238 virtual bool OnUnauthenticatedPublicHeader(
239 const QuicPacketPublicHeader& header) OVERRIDE {
240 public_header_.reset(new QuicPacketPublicHeader(header));
241 return accept_public_header_;
244 virtual bool OnUnauthenticatedHeader(
245 const QuicPacketHeader& header) OVERRIDE {
249 virtual void OnDecryptedPacket(EncryptionLevel level) OVERRIDE {}
251 virtual bool OnPacketHeader(const QuicPacketHeader& header) OVERRIDE {
253 header_.reset(new QuicPacketHeader(header));
254 return accept_packet_;
257 virtual bool OnStreamFrame(const QuicStreamFrame& frame) OVERRIDE {
259 stream_frames_.push_back(new QuicStreamFrame(frame));
263 virtual void OnFecProtectedPayload(StringPiece payload) OVERRIDE {
264 fec_protected_payload_ = payload.as_string();
267 virtual bool OnAckFrame(const QuicAckFrame& frame) OVERRIDE {
269 ack_frames_.push_back(new QuicAckFrame(frame));
273 virtual bool OnCongestionFeedbackFrame(
274 const QuicCongestionFeedbackFrame& frame) OVERRIDE {
276 congestion_feedback_frames_.push_back(
277 new QuicCongestionFeedbackFrame(frame));
281 virtual bool OnStopWaitingFrame(const QuicStopWaitingFrame& frame) OVERRIDE {
283 stop_waiting_frames_.push_back(new QuicStopWaitingFrame(frame));
287 virtual bool OnPingFrame(const QuicPingFrame& frame) OVERRIDE {
289 ping_frames_.push_back(new QuicPingFrame(frame));
293 virtual void OnFecData(const QuicFecData& fec) OVERRIDE {
295 fec_data_.push_back(new QuicFecData(fec));
298 virtual void OnPacketComplete() OVERRIDE {
302 virtual bool OnRstStreamFrame(const QuicRstStreamFrame& frame) OVERRIDE {
303 rst_stream_frame_ = frame;
307 virtual bool OnConnectionCloseFrame(
308 const QuicConnectionCloseFrame& frame) OVERRIDE {
309 connection_close_frame_ = frame;
313 virtual bool OnGoAwayFrame(const QuicGoAwayFrame& frame) OVERRIDE {
314 goaway_frame_ = frame;
318 virtual bool OnWindowUpdateFrame(const QuicWindowUpdateFrame& frame)
320 window_update_frame_ = frame;
324 virtual bool OnBlockedFrame(const QuicBlockedFrame& frame) OVERRIDE {
325 blocked_frame_ = frame;
329 // Counters from the visitor_ callbacks.
331 int version_mismatch_;
335 int complete_packets_;
336 int revived_packets_;
338 bool accept_public_header_;
340 scoped_ptr<QuicPacketHeader> header_;
341 scoped_ptr<QuicPacketPublicHeader> public_header_;
342 scoped_ptr<QuicPublicResetPacket> public_reset_packet_;
343 scoped_ptr<QuicVersionNegotiationPacket> version_negotiation_packet_;
344 vector<QuicStreamFrame*> stream_frames_;
345 vector<QuicAckFrame*> ack_frames_;
346 vector<QuicCongestionFeedbackFrame*> congestion_feedback_frames_;
347 vector<QuicStopWaitingFrame*> stop_waiting_frames_;
348 vector<QuicPingFrame*> ping_frames_;
349 vector<QuicFecData*> fec_data_;
350 string fec_protected_payload_;
351 QuicRstStreamFrame rst_stream_frame_;
352 QuicConnectionCloseFrame connection_close_frame_;
353 QuicGoAwayFrame goaway_frame_;
354 QuicWindowUpdateFrame window_update_frame_;
355 QuicBlockedFrame blocked_frame_;
358 class QuicFramerTest : public ::testing::TestWithParam<QuicVersion> {
361 : encrypter_(new test::TestEncrypter()),
362 decrypter_(new test::TestDecrypter()),
363 start_(QuicTime::Zero().Add(QuicTime::Delta::FromMicroseconds(0x10))),
364 framer_(QuicSupportedVersions(), start_, true) {
365 version_ = GetParam();
366 framer_.set_version(version_);
367 framer_.SetDecrypter(decrypter_, ENCRYPTION_NONE);
368 framer_.SetEncrypter(ENCRYPTION_NONE, encrypter_);
369 framer_.set_visitor(&visitor_);
370 framer_.set_received_entropy_calculator(&entropy_calculator_);
373 // Helper function to get unsigned char representation of digit in the
374 // units place of the current QUIC version number.
375 unsigned char GetQuicVersionDigitOnes() {
376 return static_cast<unsigned char> ('0' + version_%10);
379 // Helper function to get unsigned char representation of digit in the
380 // tens place of the current QUIC version number.
381 unsigned char GetQuicVersionDigitTens() {
382 return static_cast<unsigned char> ('0' + (version_/10)%10);
385 bool CheckEncryption(QuicPacketSequenceNumber sequence_number,
386 QuicPacket* packet) {
387 if (sequence_number != encrypter_->sequence_number_) {
388 LOG(ERROR) << "Encrypted incorrect packet sequence number. expected "
389 << sequence_number << " actual: "
390 << encrypter_->sequence_number_;
393 if (packet->AssociatedData() != encrypter_->associated_data_) {
394 LOG(ERROR) << "Encrypted incorrect associated data. expected "
395 << packet->AssociatedData() << " actual: "
396 << encrypter_->associated_data_;
399 if (packet->Plaintext() != encrypter_->plaintext_) {
400 LOG(ERROR) << "Encrypted incorrect plaintext data. expected "
401 << packet->Plaintext() << " actual: "
402 << encrypter_->plaintext_;
408 bool CheckDecryption(const QuicEncryptedPacket& encrypted,
409 bool includes_version) {
410 if (visitor_.header_->packet_sequence_number !=
411 decrypter_->sequence_number_) {
412 LOG(ERROR) << "Decrypted incorrect packet sequence number. expected "
413 << visitor_.header_->packet_sequence_number << " actual: "
414 << decrypter_->sequence_number_;
417 if (QuicFramer::GetAssociatedDataFromEncryptedPacket(
418 encrypted, PACKET_8BYTE_CONNECTION_ID,
419 includes_version, PACKET_6BYTE_SEQUENCE_NUMBER) !=
420 decrypter_->associated_data_) {
421 LOG(ERROR) << "Decrypted incorrect associated data. expected "
422 << QuicFramer::GetAssociatedDataFromEncryptedPacket(
423 encrypted, PACKET_8BYTE_CONNECTION_ID,
424 includes_version, PACKET_6BYTE_SEQUENCE_NUMBER)
425 << " actual: " << decrypter_->associated_data_;
428 StringPiece ciphertext(encrypted.AsStringPiece().substr(
429 GetStartOfEncryptedData(PACKET_8BYTE_CONNECTION_ID, includes_version,
430 PACKET_6BYTE_SEQUENCE_NUMBER)));
431 if (ciphertext != decrypter_->ciphertext_) {
432 LOG(ERROR) << "Decrypted incorrect ciphertext data. expected "
433 << ciphertext << " actual: "
434 << decrypter_->ciphertext_;
440 char* AsChars(unsigned char* data) {
441 return reinterpret_cast<char*>(data);
444 void CheckProcessingFails(unsigned char* packet,
446 string expected_error,
447 QuicErrorCode error_code) {
448 QuicEncryptedPacket encrypted(AsChars(packet), len, false);
449 EXPECT_FALSE(framer_.ProcessPacket(encrypted)) << "len: " << len;
450 EXPECT_EQ(expected_error, framer_.detailed_error()) << "len: " << len;
451 EXPECT_EQ(error_code, framer_.error()) << "len: " << len;
454 // Checks if the supplied string matches data in the supplied StreamFrame.
455 void CheckStreamFrameData(string str, QuicStreamFrame* frame) {
456 scoped_ptr<string> frame_data(frame->GetDataAsString());
457 EXPECT_EQ(str, *frame_data);
460 void CheckStreamFrameBoundaries(unsigned char* packet,
461 size_t stream_id_size,
462 bool include_version) {
463 // Now test framing boundaries
464 for (size_t i = kQuicFrameTypeSize; i < GetMinStreamFrameSize(); ++i) {
465 string expected_error;
466 if (i < kQuicFrameTypeSize + stream_id_size) {
467 expected_error = "Unable to read stream_id.";
468 } else if (i < kQuicFrameTypeSize + stream_id_size +
469 kQuicMaxStreamOffsetSize) {
470 expected_error = "Unable to read offset.";
472 expected_error = "Unable to read frame data.";
474 CheckProcessingFails(
476 i + GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID, include_version,
477 PACKET_6BYTE_SEQUENCE_NUMBER,
479 expected_error, QUIC_INVALID_STREAM_DATA);
483 void CheckCalculatePacketSequenceNumber(
484 QuicPacketSequenceNumber expected_sequence_number,
485 QuicPacketSequenceNumber last_sequence_number) {
486 QuicPacketSequenceNumber wire_sequence_number =
487 expected_sequence_number & kMask;
488 QuicFramerPeer::SetLastSequenceNumber(&framer_, last_sequence_number);
489 EXPECT_EQ(expected_sequence_number,
490 QuicFramerPeer::CalculatePacketSequenceNumberFromWire(
491 &framer_, PACKET_6BYTE_SEQUENCE_NUMBER, wire_sequence_number))
492 << "last_sequence_number: " << last_sequence_number
493 << " wire_sequence_number: " << wire_sequence_number;
496 QuicPacket* BuildDataPacket(const QuicPacketHeader& header,
497 const QuicFrames& frames) {
498 return BuildUnsizedDataPacket(&framer_, header, frames).packet;
501 test::TestEncrypter* encrypter_;
502 test::TestDecrypter* decrypter_;
503 QuicVersion version_;
506 test::TestQuicVisitor visitor_;
507 test::TestEntropyCalculator entropy_calculator_;
510 // Run all framer tests with all supported versions of QUIC.
511 INSTANTIATE_TEST_CASE_P(QuicFramerTests,
513 ::testing::ValuesIn(kSupportedQuicVersions));
515 TEST_P(QuicFramerTest, CalculatePacketSequenceNumberFromWireNearEpochStart) {
516 // A few quick manual sanity checks
517 CheckCalculatePacketSequenceNumber(GG_UINT64_C(1), GG_UINT64_C(0));
518 CheckCalculatePacketSequenceNumber(kEpoch + 1, kMask);
519 CheckCalculatePacketSequenceNumber(kEpoch, kMask);
521 // Cases where the last number was close to the start of the range
522 for (uint64 last = 0; last < 10; last++) {
523 // Small numbers should not wrap (even if they're out of order).
524 for (uint64 j = 0; j < 10; j++) {
525 CheckCalculatePacketSequenceNumber(j, last);
528 // Large numbers should not wrap either (because we're near 0 already).
529 for (uint64 j = 0; j < 10; j++) {
530 CheckCalculatePacketSequenceNumber(kEpoch - 1 - j, last);
535 TEST_P(QuicFramerTest, CalculatePacketSequenceNumberFromWireNearEpochEnd) {
536 // Cases where the last number was close to the end of the range
537 for (uint64 i = 0; i < 10; i++) {
538 QuicPacketSequenceNumber last = kEpoch - i;
540 // Small numbers should wrap.
541 for (uint64 j = 0; j < 10; j++) {
542 CheckCalculatePacketSequenceNumber(kEpoch + j, last);
545 // Large numbers should not (even if they're out of order).
546 for (uint64 j = 0; j < 10; j++) {
547 CheckCalculatePacketSequenceNumber(kEpoch - 1 - j, last);
552 // Next check where we're in a non-zero epoch to verify we handle
553 // reverse wrapping, too.
554 TEST_P(QuicFramerTest, CalculatePacketSequenceNumberFromWireNearPrevEpoch) {
555 const uint64 prev_epoch = 1 * kEpoch;
556 const uint64 cur_epoch = 2 * kEpoch;
557 // Cases where the last number was close to the start of the range
558 for (uint64 i = 0; i < 10; i++) {
559 uint64 last = cur_epoch + i;
560 // Small number should not wrap (even if they're out of order).
561 for (uint64 j = 0; j < 10; j++) {
562 CheckCalculatePacketSequenceNumber(cur_epoch + j, last);
565 // But large numbers should reverse wrap.
566 for (uint64 j = 0; j < 10; j++) {
567 uint64 num = kEpoch - 1 - j;
568 CheckCalculatePacketSequenceNumber(prev_epoch + num, last);
573 TEST_P(QuicFramerTest, CalculatePacketSequenceNumberFromWireNearNextEpoch) {
574 const uint64 cur_epoch = 2 * kEpoch;
575 const uint64 next_epoch = 3 * kEpoch;
576 // Cases where the last number was close to the end of the range
577 for (uint64 i = 0; i < 10; i++) {
578 QuicPacketSequenceNumber last = next_epoch - 1 - i;
580 // Small numbers should wrap.
581 for (uint64 j = 0; j < 10; j++) {
582 CheckCalculatePacketSequenceNumber(next_epoch + j, last);
585 // but large numbers should not (even if they're out of order).
586 for (uint64 j = 0; j < 10; j++) {
587 uint64 num = kEpoch - 1 - j;
588 CheckCalculatePacketSequenceNumber(cur_epoch + num, last);
593 TEST_P(QuicFramerTest, CalculatePacketSequenceNumberFromWireNearNextMax) {
594 const uint64 max_number = numeric_limits<uint64>::max();
595 const uint64 max_epoch = max_number & ~kMask;
597 // Cases where the last number was close to the end of the range
598 for (uint64 i = 0; i < 10; i++) {
599 // Subtract 1, because the expected next sequence number is 1 more than the
600 // last sequence number.
601 QuicPacketSequenceNumber last = max_number - i - 1;
603 // Small numbers should not wrap, because they have nowhere to go.
604 for (uint64 j = 0; j < 10; j++) {
605 CheckCalculatePacketSequenceNumber(max_epoch + j, last);
608 // Large numbers should not wrap either.
609 for (uint64 j = 0; j < 10; j++) {
610 uint64 num = kEpoch - 1 - j;
611 CheckCalculatePacketSequenceNumber(max_epoch + num, last);
616 TEST_P(QuicFramerTest, EmptyPacket) {
617 char packet[] = { 0x00 };
618 QuicEncryptedPacket encrypted(packet, 0, false);
619 EXPECT_FALSE(framer_.ProcessPacket(encrypted));
620 EXPECT_EQ(QUIC_INVALID_PACKET_HEADER, framer_.error());
623 TEST_P(QuicFramerTest, LargePacket) {
624 unsigned char packet[kMaxPacketSize + 1] = {
625 // public flags (8 byte connection_id)
628 0x10, 0x32, 0x54, 0x76,
629 0x98, 0xBA, 0xDC, 0xFE,
630 // packet sequence number
631 0xBC, 0x9A, 0x78, 0x56,
637 memset(packet + GetPacketHeaderSize(
638 PACKET_8BYTE_CONNECTION_ID, !kIncludeVersion,
639 PACKET_6BYTE_SEQUENCE_NUMBER, NOT_IN_FEC_GROUP), 0,
640 kMaxPacketSize - GetPacketHeaderSize(
641 PACKET_8BYTE_CONNECTION_ID, !kIncludeVersion,
642 PACKET_6BYTE_SEQUENCE_NUMBER, NOT_IN_FEC_GROUP) + 1);
644 QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
645 EXPECT_FALSE(framer_.ProcessPacket(encrypted));
647 ASSERT_TRUE(visitor_.header_.get());
648 // Make sure we've parsed the packet header, so we can send an error.
649 EXPECT_EQ(GG_UINT64_C(0xFEDCBA9876543210),
650 visitor_.header_->public_header.connection_id);
651 // Make sure the correct error is propagated.
652 EXPECT_EQ(QUIC_PACKET_TOO_LARGE, framer_.error());
655 TEST_P(QuicFramerTest, PacketHeader) {
656 unsigned char packet[] = {
657 // public flags (8 byte connection_id)
660 0x10, 0x32, 0x54, 0x76,
661 0x98, 0xBA, 0xDC, 0xFE,
662 // packet sequence number
663 0xBC, 0x9A, 0x78, 0x56,
669 QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
670 EXPECT_FALSE(framer_.ProcessPacket(encrypted));
671 EXPECT_EQ(QUIC_MISSING_PAYLOAD, framer_.error());
672 ASSERT_TRUE(visitor_.header_.get());
673 EXPECT_EQ(GG_UINT64_C(0xFEDCBA9876543210),
674 visitor_.header_->public_header.connection_id);
675 EXPECT_FALSE(visitor_.header_->public_header.reset_flag);
676 EXPECT_FALSE(visitor_.header_->public_header.version_flag);
677 EXPECT_FALSE(visitor_.header_->fec_flag);
678 EXPECT_FALSE(visitor_.header_->entropy_flag);
679 EXPECT_EQ(0, visitor_.header_->entropy_hash);
680 EXPECT_EQ(GG_UINT64_C(0x123456789ABC),
681 visitor_.header_->packet_sequence_number);
682 EXPECT_EQ(NOT_IN_FEC_GROUP, visitor_.header_->is_in_fec_group);
683 EXPECT_EQ(0x00u, visitor_.header_->fec_group);
685 // Now test framing boundaries
687 i < GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID, !kIncludeVersion,
688 PACKET_6BYTE_SEQUENCE_NUMBER, NOT_IN_FEC_GROUP);
690 string expected_error;
691 if (i < kConnectionIdOffset) {
692 expected_error = "Unable to read public flags.";
693 } else if (i < GetSequenceNumberOffset(!kIncludeVersion)) {
694 expected_error = "Unable to read ConnectionId.";
695 } else if (i < GetPrivateFlagsOffset(!kIncludeVersion)) {
696 expected_error = "Unable to read sequence number.";
697 } else if (i < GetFecGroupOffset(!kIncludeVersion)) {
698 expected_error = "Unable to read private flags.";
700 expected_error = "Unable to read first fec protected packet offset.";
702 CheckProcessingFails(packet, i, expected_error, QUIC_INVALID_PACKET_HEADER);
706 TEST_P(QuicFramerTest, PacketHeaderWith4ByteConnectionId) {
707 QuicFramerPeer::SetLastSerializedConnectionId(
708 &framer_, GG_UINT64_C(0xFEDCBA9876543210));
710 unsigned char packet[] = {
711 // public flags (4 byte connection_id)
714 0x10, 0x32, 0x54, 0x76,
715 // packet sequence number
716 0xBC, 0x9A, 0x78, 0x56,
722 QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
723 EXPECT_FALSE(framer_.ProcessPacket(encrypted));
724 EXPECT_EQ(QUIC_MISSING_PAYLOAD, framer_.error());
725 ASSERT_TRUE(visitor_.header_.get());
726 EXPECT_EQ(GG_UINT64_C(0xFEDCBA9876543210),
727 visitor_.header_->public_header.connection_id);
728 EXPECT_FALSE(visitor_.header_->public_header.reset_flag);
729 EXPECT_FALSE(visitor_.header_->public_header.version_flag);
730 EXPECT_FALSE(visitor_.header_->fec_flag);
731 EXPECT_FALSE(visitor_.header_->entropy_flag);
732 EXPECT_EQ(0, visitor_.header_->entropy_hash);
733 EXPECT_EQ(GG_UINT64_C(0x123456789ABC),
734 visitor_.header_->packet_sequence_number);
735 EXPECT_EQ(NOT_IN_FEC_GROUP, visitor_.header_->is_in_fec_group);
736 EXPECT_EQ(0x00u, visitor_.header_->fec_group);
738 // Now test framing boundaries
740 i < GetPacketHeaderSize(PACKET_4BYTE_CONNECTION_ID, !kIncludeVersion,
741 PACKET_6BYTE_SEQUENCE_NUMBER, NOT_IN_FEC_GROUP);
743 string expected_error;
744 if (i < kConnectionIdOffset) {
745 expected_error = "Unable to read public flags.";
746 } else if (i < GetSequenceNumberOffset(PACKET_4BYTE_CONNECTION_ID,
748 expected_error = "Unable to read ConnectionId.";
749 } else if (i < GetPrivateFlagsOffset(PACKET_4BYTE_CONNECTION_ID,
751 expected_error = "Unable to read sequence number.";
752 } else if (i < GetFecGroupOffset(PACKET_4BYTE_CONNECTION_ID,
754 expected_error = "Unable to read private flags.";
756 expected_error = "Unable to read first fec protected packet offset.";
758 CheckProcessingFails(packet, i, expected_error, QUIC_INVALID_PACKET_HEADER);
762 TEST_P(QuicFramerTest, PacketHeader1ByteConnectionId) {
763 QuicFramerPeer::SetLastSerializedConnectionId(
764 &framer_, GG_UINT64_C(0xFEDCBA9876543210));
766 unsigned char packet[] = {
767 // public flags (1 byte connection_id)
771 // packet sequence number
772 0xBC, 0x9A, 0x78, 0x56,
778 QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
779 EXPECT_FALSE(framer_.ProcessPacket(encrypted));
780 EXPECT_EQ(QUIC_MISSING_PAYLOAD, framer_.error());
781 ASSERT_TRUE(visitor_.header_.get());
782 EXPECT_EQ(GG_UINT64_C(0xFEDCBA9876543210),
783 visitor_.header_->public_header.connection_id);
784 EXPECT_FALSE(visitor_.header_->public_header.reset_flag);
785 EXPECT_FALSE(visitor_.header_->public_header.version_flag);
786 EXPECT_FALSE(visitor_.header_->fec_flag);
787 EXPECT_FALSE(visitor_.header_->entropy_flag);
788 EXPECT_EQ(0, visitor_.header_->entropy_hash);
789 EXPECT_EQ(GG_UINT64_C(0x123456789ABC),
790 visitor_.header_->packet_sequence_number);
791 EXPECT_EQ(NOT_IN_FEC_GROUP, visitor_.header_->is_in_fec_group);
792 EXPECT_EQ(0x00u, visitor_.header_->fec_group);
794 // Now test framing boundaries
796 i < GetPacketHeaderSize(PACKET_1BYTE_CONNECTION_ID, !kIncludeVersion,
797 PACKET_6BYTE_SEQUENCE_NUMBER, NOT_IN_FEC_GROUP);
799 string expected_error;
800 if (i < kConnectionIdOffset) {
801 expected_error = "Unable to read public flags.";
802 } else if (i < GetSequenceNumberOffset(PACKET_1BYTE_CONNECTION_ID,
804 expected_error = "Unable to read ConnectionId.";
805 } else if (i < GetPrivateFlagsOffset(PACKET_1BYTE_CONNECTION_ID,
807 expected_error = "Unable to read sequence number.";
808 } else if (i < GetFecGroupOffset(PACKET_1BYTE_CONNECTION_ID,
810 expected_error = "Unable to read private flags.";
812 expected_error = "Unable to read first fec protected packet offset.";
814 CheckProcessingFails(packet, i, expected_error, QUIC_INVALID_PACKET_HEADER);
818 TEST_P(QuicFramerTest, PacketHeaderWith0ByteConnectionId) {
819 QuicFramerPeer::SetLastSerializedConnectionId(
820 &framer_, GG_UINT64_C(0xFEDCBA9876543210));
822 unsigned char packet[] = {
823 // public flags (0 byte connection_id)
826 // packet sequence number
827 0xBC, 0x9A, 0x78, 0x56,
833 QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
834 EXPECT_FALSE(framer_.ProcessPacket(encrypted));
835 EXPECT_EQ(QUIC_MISSING_PAYLOAD, framer_.error());
836 ASSERT_TRUE(visitor_.header_.get());
837 EXPECT_EQ(GG_UINT64_C(0xFEDCBA9876543210),
838 visitor_.header_->public_header.connection_id);
839 EXPECT_FALSE(visitor_.header_->public_header.reset_flag);
840 EXPECT_FALSE(visitor_.header_->public_header.version_flag);
841 EXPECT_FALSE(visitor_.header_->fec_flag);
842 EXPECT_FALSE(visitor_.header_->entropy_flag);
843 EXPECT_EQ(0, visitor_.header_->entropy_hash);
844 EXPECT_EQ(GG_UINT64_C(0x123456789ABC),
845 visitor_.header_->packet_sequence_number);
846 EXPECT_EQ(NOT_IN_FEC_GROUP, visitor_.header_->is_in_fec_group);
847 EXPECT_EQ(0x00u, visitor_.header_->fec_group);
849 // Now test framing boundaries
851 i < GetPacketHeaderSize(PACKET_0BYTE_CONNECTION_ID, !kIncludeVersion,
852 PACKET_6BYTE_SEQUENCE_NUMBER, NOT_IN_FEC_GROUP);
854 string expected_error;
855 if (i < kConnectionIdOffset) {
856 expected_error = "Unable to read public flags.";
857 } else if (i < GetSequenceNumberOffset(PACKET_0BYTE_CONNECTION_ID,
859 expected_error = "Unable to read ConnectionId.";
860 } else if (i < GetPrivateFlagsOffset(PACKET_0BYTE_CONNECTION_ID,
862 expected_error = "Unable to read sequence number.";
863 } else if (i < GetFecGroupOffset(PACKET_0BYTE_CONNECTION_ID,
865 expected_error = "Unable to read private flags.";
867 expected_error = "Unable to read first fec protected packet offset.";
869 CheckProcessingFails(packet, i, expected_error, QUIC_INVALID_PACKET_HEADER);
873 TEST_P(QuicFramerTest, PacketHeaderWithVersionFlag) {
874 unsigned char packet[] = {
875 // public flags (version)
878 0x10, 0x32, 0x54, 0x76,
879 0x98, 0xBA, 0xDC, 0xFE,
881 'Q', '0', GetQuicVersionDigitTens(), GetQuicVersionDigitOnes(),
882 // packet sequence number
883 0xBC, 0x9A, 0x78, 0x56,
889 QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
890 EXPECT_FALSE(framer_.ProcessPacket(encrypted));
891 EXPECT_EQ(QUIC_MISSING_PAYLOAD, framer_.error());
892 ASSERT_TRUE(visitor_.header_.get());
893 EXPECT_EQ(GG_UINT64_C(0xFEDCBA9876543210),
894 visitor_.header_->public_header.connection_id);
895 EXPECT_FALSE(visitor_.header_->public_header.reset_flag);
896 EXPECT_TRUE(visitor_.header_->public_header.version_flag);
897 EXPECT_EQ(GetParam(), visitor_.header_->public_header.versions[0]);
898 EXPECT_FALSE(visitor_.header_->fec_flag);
899 EXPECT_FALSE(visitor_.header_->entropy_flag);
900 EXPECT_EQ(0, visitor_.header_->entropy_hash);
901 EXPECT_EQ(GG_UINT64_C(0x123456789ABC),
902 visitor_.header_->packet_sequence_number);
903 EXPECT_EQ(NOT_IN_FEC_GROUP, visitor_.header_->is_in_fec_group);
904 EXPECT_EQ(0x00u, visitor_.header_->fec_group);
906 // Now test framing boundaries
908 i < GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID, kIncludeVersion,
909 PACKET_6BYTE_SEQUENCE_NUMBER, NOT_IN_FEC_GROUP);
911 string expected_error;
912 if (i < kConnectionIdOffset) {
913 expected_error = "Unable to read public flags.";
914 } else if (i < kVersionOffset) {
915 expected_error = "Unable to read ConnectionId.";
916 } else if (i < GetSequenceNumberOffset(kIncludeVersion)) {
917 expected_error = "Unable to read protocol version.";
918 } else if (i < GetPrivateFlagsOffset(kIncludeVersion)) {
919 expected_error = "Unable to read sequence number.";
920 } else if (i < GetFecGroupOffset(kIncludeVersion)) {
921 expected_error = "Unable to read private flags.";
923 expected_error = "Unable to read first fec protected packet offset.";
925 CheckProcessingFails(packet, i, expected_error, QUIC_INVALID_PACKET_HEADER);
929 TEST_P(QuicFramerTest, PacketHeaderWith4ByteSequenceNumber) {
930 QuicFramerPeer::SetLastSequenceNumber(&framer_,
931 GG_UINT64_C(0x123456789ABA));
933 unsigned char packet[] = {
934 // public flags (8 byte connection_id and 4 byte sequence number)
937 0x10, 0x32, 0x54, 0x76,
938 0x98, 0xBA, 0xDC, 0xFE,
939 // packet sequence number
940 0xBC, 0x9A, 0x78, 0x56,
945 QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
946 EXPECT_FALSE(framer_.ProcessPacket(encrypted));
947 EXPECT_EQ(QUIC_MISSING_PAYLOAD, framer_.error());
948 ASSERT_TRUE(visitor_.header_.get());
949 EXPECT_EQ(GG_UINT64_C(0xFEDCBA9876543210),
950 visitor_.header_->public_header.connection_id);
951 EXPECT_FALSE(visitor_.header_->public_header.reset_flag);
952 EXPECT_FALSE(visitor_.header_->public_header.version_flag);
953 EXPECT_FALSE(visitor_.header_->fec_flag);
954 EXPECT_FALSE(visitor_.header_->entropy_flag);
955 EXPECT_EQ(0, visitor_.header_->entropy_hash);
956 EXPECT_EQ(GG_UINT64_C(0x123456789ABC),
957 visitor_.header_->packet_sequence_number);
958 EXPECT_EQ(NOT_IN_FEC_GROUP, visitor_.header_->is_in_fec_group);
959 EXPECT_EQ(0x00u, visitor_.header_->fec_group);
961 // Now test framing boundaries
963 i < GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID, !kIncludeVersion,
964 PACKET_4BYTE_SEQUENCE_NUMBER, NOT_IN_FEC_GROUP);
966 string expected_error;
967 if (i < kConnectionIdOffset) {
968 expected_error = "Unable to read public flags.";
969 } else if (i < GetSequenceNumberOffset(!kIncludeVersion)) {
970 expected_error = "Unable to read ConnectionId.";
971 } else if (i < GetPrivateFlagsOffset(!kIncludeVersion,
972 PACKET_4BYTE_SEQUENCE_NUMBER)) {
973 expected_error = "Unable to read sequence number.";
974 } else if (i < GetFecGroupOffset(!kIncludeVersion,
975 PACKET_4BYTE_SEQUENCE_NUMBER)) {
976 expected_error = "Unable to read private flags.";
978 expected_error = "Unable to read first fec protected packet offset.";
980 CheckProcessingFails(packet, i, expected_error, QUIC_INVALID_PACKET_HEADER);
984 TEST_P(QuicFramerTest, PacketHeaderWith2ByteSequenceNumber) {
985 QuicFramerPeer::SetLastSequenceNumber(&framer_,
986 GG_UINT64_C(0x123456789ABA));
988 unsigned char packet[] = {
989 // public flags (8 byte connection_id and 2 byte sequence number)
992 0x10, 0x32, 0x54, 0x76,
993 0x98, 0xBA, 0xDC, 0xFE,
994 // packet sequence number
1000 QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
1001 EXPECT_FALSE(framer_.ProcessPacket(encrypted));
1002 EXPECT_EQ(QUIC_MISSING_PAYLOAD, framer_.error());
1003 ASSERT_TRUE(visitor_.header_.get());
1004 EXPECT_EQ(GG_UINT64_C(0xFEDCBA9876543210),
1005 visitor_.header_->public_header.connection_id);
1006 EXPECT_FALSE(visitor_.header_->public_header.reset_flag);
1007 EXPECT_FALSE(visitor_.header_->public_header.version_flag);
1008 EXPECT_FALSE(visitor_.header_->fec_flag);
1009 EXPECT_FALSE(visitor_.header_->entropy_flag);
1010 EXPECT_EQ(0, visitor_.header_->entropy_hash);
1011 EXPECT_EQ(GG_UINT64_C(0x123456789ABC),
1012 visitor_.header_->packet_sequence_number);
1013 EXPECT_EQ(NOT_IN_FEC_GROUP, visitor_.header_->is_in_fec_group);
1014 EXPECT_EQ(0x00u, visitor_.header_->fec_group);
1016 // Now test framing boundaries
1018 i < GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID, !kIncludeVersion,
1019 PACKET_2BYTE_SEQUENCE_NUMBER, NOT_IN_FEC_GROUP);
1021 string expected_error;
1022 if (i < kConnectionIdOffset) {
1023 expected_error = "Unable to read public flags.";
1024 } else if (i < GetSequenceNumberOffset(!kIncludeVersion)) {
1025 expected_error = "Unable to read ConnectionId.";
1026 } else if (i < GetPrivateFlagsOffset(!kIncludeVersion,
1027 PACKET_2BYTE_SEQUENCE_NUMBER)) {
1028 expected_error = "Unable to read sequence number.";
1029 } else if (i < GetFecGroupOffset(!kIncludeVersion,
1030 PACKET_2BYTE_SEQUENCE_NUMBER)) {
1031 expected_error = "Unable to read private flags.";
1033 expected_error = "Unable to read first fec protected packet offset.";
1035 CheckProcessingFails(packet, i, expected_error, QUIC_INVALID_PACKET_HEADER);
1039 TEST_P(QuicFramerTest, PacketHeaderWith1ByteSequenceNumber) {
1040 QuicFramerPeer::SetLastSequenceNumber(&framer_,
1041 GG_UINT64_C(0x123456789ABA));
1043 unsigned char packet[] = {
1044 // public flags (8 byte connection_id and 1 byte sequence number)
1047 0x10, 0x32, 0x54, 0x76,
1048 0x98, 0xBA, 0xDC, 0xFE,
1049 // packet sequence number
1055 QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
1056 EXPECT_FALSE(framer_.ProcessPacket(encrypted));
1057 EXPECT_EQ(QUIC_MISSING_PAYLOAD, framer_.error());
1058 ASSERT_TRUE(visitor_.header_.get());
1059 EXPECT_EQ(GG_UINT64_C(0xFEDCBA9876543210),
1060 visitor_.header_->public_header.connection_id);
1061 EXPECT_FALSE(visitor_.header_->public_header.reset_flag);
1062 EXPECT_FALSE(visitor_.header_->public_header.version_flag);
1063 EXPECT_FALSE(visitor_.header_->fec_flag);
1064 EXPECT_FALSE(visitor_.header_->entropy_flag);
1065 EXPECT_EQ(0, visitor_.header_->entropy_hash);
1066 EXPECT_EQ(GG_UINT64_C(0x123456789ABC),
1067 visitor_.header_->packet_sequence_number);
1068 EXPECT_EQ(NOT_IN_FEC_GROUP, visitor_.header_->is_in_fec_group);
1069 EXPECT_EQ(0x00u, visitor_.header_->fec_group);
1071 // Now test framing boundaries
1073 i < GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID, !kIncludeVersion,
1074 PACKET_1BYTE_SEQUENCE_NUMBER, NOT_IN_FEC_GROUP);
1076 string expected_error;
1077 if (i < kConnectionIdOffset) {
1078 expected_error = "Unable to read public flags.";
1079 } else if (i < GetSequenceNumberOffset(!kIncludeVersion)) {
1080 expected_error = "Unable to read ConnectionId.";
1081 } else if (i < GetPrivateFlagsOffset(!kIncludeVersion,
1082 PACKET_1BYTE_SEQUENCE_NUMBER)) {
1083 expected_error = "Unable to read sequence number.";
1084 } else if (i < GetFecGroupOffset(!kIncludeVersion,
1085 PACKET_1BYTE_SEQUENCE_NUMBER)) {
1086 expected_error = "Unable to read private flags.";
1088 expected_error = "Unable to read first fec protected packet offset.";
1090 CheckProcessingFails(packet, i, expected_error, QUIC_INVALID_PACKET_HEADER);
1094 TEST_P(QuicFramerTest, InvalidPublicFlag) {
1095 unsigned char packet[] = {
1096 // public flags: all flags set but the public reset flag and version flag.
1099 0x10, 0x32, 0x54, 0x76,
1100 0x98, 0xBA, 0xDC, 0xFE,
1101 // packet sequence number
1102 0xBC, 0x9A, 0x78, 0x56,
1107 // frame type (padding)
1109 0x00, 0x00, 0x00, 0x00
1111 CheckProcessingFails(packet,
1113 "Illegal public flags value.",
1114 QUIC_INVALID_PACKET_HEADER);
1116 // Now turn off validation.
1117 framer_.set_validate_flags(false);
1118 QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
1119 EXPECT_TRUE(framer_.ProcessPacket(encrypted));
1122 TEST_P(QuicFramerTest, InvalidPublicFlagWithMatchingVersions) {
1123 unsigned char packet[] = {
1124 // public flags (8 byte connection_id and version flag and an unknown flag)
1127 0x10, 0x32, 0x54, 0x76,
1128 0x98, 0xBA, 0xDC, 0xFE,
1130 'Q', '0', GetQuicVersionDigitTens(), GetQuicVersionDigitOnes(),
1131 // packet sequence number
1132 0xBC, 0x9A, 0x78, 0x56,
1137 // frame type (padding)
1139 0x00, 0x00, 0x00, 0x00
1141 CheckProcessingFails(packet,
1143 "Illegal public flags value.",
1144 QUIC_INVALID_PACKET_HEADER);
1147 TEST_P(QuicFramerTest, LargePublicFlagWithMismatchedVersions) {
1148 unsigned char packet[] = {
1149 // public flags (8 byte connection_id, version flag and an unknown flag)
1152 0x10, 0x32, 0x54, 0x76,
1153 0x98, 0xBA, 0xDC, 0xFE,
1156 // packet sequence number
1157 0xBC, 0x9A, 0x78, 0x56,
1162 // frame type (padding frame)
1164 0x00, 0x00, 0x00, 0x00
1166 QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
1167 EXPECT_TRUE(framer_.ProcessPacket(encrypted));
1168 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
1169 ASSERT_TRUE(visitor_.header_.get());
1170 EXPECT_EQ(0, visitor_.frame_count_);
1171 EXPECT_EQ(1, visitor_.version_mismatch_);
1174 TEST_P(QuicFramerTest, InvalidPrivateFlag) {
1175 unsigned char packet[] = {
1176 // public flags (8 byte connection_id)
1179 0x10, 0x32, 0x54, 0x76,
1180 0x98, 0xBA, 0xDC, 0xFE,
1181 // packet sequence number
1182 0xBC, 0x9A, 0x78, 0x56,
1187 // frame type (padding)
1189 0x00, 0x00, 0x00, 0x00
1191 CheckProcessingFails(packet,
1193 "Illegal private flags value.",
1194 QUIC_INVALID_PACKET_HEADER);
1197 TEST_P(QuicFramerTest, InvalidFECGroupOffset) {
1198 unsigned char packet[] = {
1199 // public flags (8 byte connection_id)
1202 0x10, 0x32, 0x54, 0x76,
1203 0x98, 0xBA, 0xDC, 0xFE,
1204 // packet sequence number
1205 0x01, 0x00, 0x00, 0x00,
1207 // private flags (fec group)
1209 // first fec protected packet offset
1212 CheckProcessingFails(packet,
1214 "First fec protected packet offset must be less "
1215 "than the sequence number.",
1216 QUIC_INVALID_PACKET_HEADER);
1219 TEST_P(QuicFramerTest, PaddingFrame) {
1220 unsigned char packet[] = {
1221 // public flags (8 byte connection_id)
1224 0x10, 0x32, 0x54, 0x76,
1225 0x98, 0xBA, 0xDC, 0xFE,
1226 // packet sequence number
1227 0xBC, 0x9A, 0x78, 0x56,
1232 // frame type (padding frame)
1234 // Ignored data (which in this case is a stream frame)
1235 // frame type (stream frame with fin)
1238 0x04, 0x03, 0x02, 0x01,
1240 0x54, 0x76, 0x10, 0x32,
1241 0xDC, 0xFE, 0x98, 0xBA,
1250 QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
1251 EXPECT_TRUE(framer_.ProcessPacket(encrypted));
1252 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
1253 ASSERT_TRUE(visitor_.header_.get());
1254 EXPECT_TRUE(CheckDecryption(encrypted, !kIncludeVersion));
1256 ASSERT_EQ(0u, visitor_.stream_frames_.size());
1257 EXPECT_EQ(0u, visitor_.ack_frames_.size());
1258 // A packet with no frames is not acceptable.
1259 CheckProcessingFails(
1261 GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID, !kIncludeVersion,
1262 PACKET_6BYTE_SEQUENCE_NUMBER, NOT_IN_FEC_GROUP),
1263 "Packet has no frames.", QUIC_MISSING_PAYLOAD);
1266 TEST_P(QuicFramerTest, StreamFrame) {
1267 unsigned char packet[] = {
1268 // public flags (8 byte connection_id)
1271 0x10, 0x32, 0x54, 0x76,
1272 0x98, 0xBA, 0xDC, 0xFE,
1273 // packet sequence number
1274 0xBC, 0x9A, 0x78, 0x56,
1279 // frame type (stream frame with fin)
1282 0x04, 0x03, 0x02, 0x01,
1284 0x54, 0x76, 0x10, 0x32,
1285 0xDC, 0xFE, 0x98, 0xBA,
1294 QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
1295 EXPECT_TRUE(framer_.ProcessPacket(encrypted));
1297 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
1298 ASSERT_TRUE(visitor_.header_.get());
1299 EXPECT_TRUE(CheckDecryption(encrypted, !kIncludeVersion));
1301 ASSERT_EQ(1u, visitor_.stream_frames_.size());
1302 EXPECT_EQ(0u, visitor_.ack_frames_.size());
1303 EXPECT_EQ(static_cast<uint64>(0x01020304),
1304 visitor_.stream_frames_[0]->stream_id);
1305 EXPECT_TRUE(visitor_.stream_frames_[0]->fin);
1306 EXPECT_EQ(GG_UINT64_C(0xBA98FEDC32107654),
1307 visitor_.stream_frames_[0]->offset);
1308 CheckStreamFrameData("hello world!", visitor_.stream_frames_[0]);
1310 // Now test framing boundaries
1311 CheckStreamFrameBoundaries(packet, kQuicMaxStreamIdSize, !kIncludeVersion);
1314 TEST_P(QuicFramerTest, StreamFrame3ByteStreamId) {
1315 unsigned char packet[] = {
1316 // public flags (8 byte connection_id)
1319 0x10, 0x32, 0x54, 0x76,
1320 0x98, 0xBA, 0xDC, 0xFE,
1321 // packet sequence number
1322 0xBC, 0x9A, 0x78, 0x56,
1327 // frame type (stream frame with fin)
1332 0x54, 0x76, 0x10, 0x32,
1333 0xDC, 0xFE, 0x98, 0xBA,
1342 QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
1343 EXPECT_TRUE(framer_.ProcessPacket(encrypted));
1345 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
1346 ASSERT_TRUE(visitor_.header_.get());
1347 EXPECT_TRUE(CheckDecryption(encrypted, !kIncludeVersion));
1349 ASSERT_EQ(1u, visitor_.stream_frames_.size());
1350 EXPECT_EQ(0u, visitor_.ack_frames_.size());
1351 EXPECT_EQ(GG_UINT64_C(0x00020304),
1352 visitor_.stream_frames_[0]->stream_id);
1353 EXPECT_TRUE(visitor_.stream_frames_[0]->fin);
1354 EXPECT_EQ(GG_UINT64_C(0xBA98FEDC32107654),
1355 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 = 3;
1360 CheckStreamFrameBoundaries(packet, stream_id_size, !kIncludeVersion);
1363 TEST_P(QuicFramerTest, StreamFrame2ByteStreamId) {
1364 unsigned char packet[] = {
1365 // public flags (8 byte connection_id)
1368 0x10, 0x32, 0x54, 0x76,
1369 0x98, 0xBA, 0xDC, 0xFE,
1370 // packet sequence number
1371 0xBC, 0x9A, 0x78, 0x56,
1376 // frame type (stream frame with fin)
1381 0x54, 0x76, 0x10, 0x32,
1382 0xDC, 0xFE, 0x98, 0xBA,
1391 QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
1392 EXPECT_TRUE(framer_.ProcessPacket(encrypted));
1394 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
1395 ASSERT_TRUE(visitor_.header_.get());
1396 EXPECT_TRUE(CheckDecryption(encrypted, !kIncludeVersion));
1398 ASSERT_EQ(1u, visitor_.stream_frames_.size());
1399 EXPECT_EQ(0u, visitor_.ack_frames_.size());
1400 EXPECT_EQ(static_cast<uint64>(0x00000304),
1401 visitor_.stream_frames_[0]->stream_id);
1402 EXPECT_TRUE(visitor_.stream_frames_[0]->fin);
1403 EXPECT_EQ(GG_UINT64_C(0xBA98FEDC32107654),
1404 visitor_.stream_frames_[0]->offset);
1405 CheckStreamFrameData("hello world!", visitor_.stream_frames_[0]);
1407 // Now test framing boundaries
1408 const size_t stream_id_size = 2;
1409 CheckStreamFrameBoundaries(packet, stream_id_size, !kIncludeVersion);
1412 TEST_P(QuicFramerTest, StreamFrame1ByteStreamId) {
1413 unsigned char packet[] = {
1414 // public flags (8 byte connection_id)
1417 0x10, 0x32, 0x54, 0x76,
1418 0x98, 0xBA, 0xDC, 0xFE,
1419 // packet sequence number
1420 0xBC, 0x9A, 0x78, 0x56,
1425 // frame type (stream frame with fin)
1430 0x54, 0x76, 0x10, 0x32,
1431 0xDC, 0xFE, 0x98, 0xBA,
1440 QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
1441 EXPECT_TRUE(framer_.ProcessPacket(encrypted));
1443 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
1444 ASSERT_TRUE(visitor_.header_.get());
1445 EXPECT_TRUE(CheckDecryption(encrypted, !kIncludeVersion));
1447 ASSERT_EQ(1u, visitor_.stream_frames_.size());
1448 EXPECT_EQ(0u, visitor_.ack_frames_.size());
1449 EXPECT_EQ(static_cast<uint64>(0x00000004),
1450 visitor_.stream_frames_[0]->stream_id);
1451 EXPECT_TRUE(visitor_.stream_frames_[0]->fin);
1452 EXPECT_EQ(GG_UINT64_C(0xBA98FEDC32107654),
1453 visitor_.stream_frames_[0]->offset);
1454 CheckStreamFrameData("hello world!", visitor_.stream_frames_[0]);
1456 // Now test framing boundaries
1457 const size_t stream_id_size = 1;
1458 CheckStreamFrameBoundaries(packet, stream_id_size, !kIncludeVersion);
1461 TEST_P(QuicFramerTest, StreamFrameWithVersion) {
1462 unsigned char packet[] = {
1463 // public flags (version, 8 byte connection_id)
1466 0x10, 0x32, 0x54, 0x76,
1467 0x98, 0xBA, 0xDC, 0xFE,
1469 'Q', '0', GetQuicVersionDigitTens(), GetQuicVersionDigitOnes(),
1470 // packet sequence number
1471 0xBC, 0x9A, 0x78, 0x56,
1476 // frame type (stream frame with fin)
1479 0x04, 0x03, 0x02, 0x01,
1481 0x54, 0x76, 0x10, 0x32,
1482 0xDC, 0xFE, 0x98, 0xBA,
1491 QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
1492 EXPECT_TRUE(framer_.ProcessPacket(encrypted));
1494 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
1495 ASSERT_TRUE(visitor_.header_.get());
1496 EXPECT_TRUE(visitor_.header_->public_header.version_flag);
1497 EXPECT_EQ(GetParam(), visitor_.header_->public_header.versions[0]);
1498 EXPECT_TRUE(CheckDecryption(encrypted, kIncludeVersion));
1500 ASSERT_EQ(1u, visitor_.stream_frames_.size());
1501 EXPECT_EQ(0u, visitor_.ack_frames_.size());
1502 EXPECT_EQ(static_cast<uint64>(0x01020304),
1503 visitor_.stream_frames_[0]->stream_id);
1504 EXPECT_TRUE(visitor_.stream_frames_[0]->fin);
1505 EXPECT_EQ(GG_UINT64_C(0xBA98FEDC32107654),
1506 visitor_.stream_frames_[0]->offset);
1507 CheckStreamFrameData("hello world!", visitor_.stream_frames_[0]);
1509 // Now test framing boundaries
1510 CheckStreamFrameBoundaries(packet, kQuicMaxStreamIdSize, kIncludeVersion);
1513 TEST_P(QuicFramerTest, RejectPacket) {
1514 visitor_.accept_packet_ = false;
1516 unsigned char packet[] = {
1517 // public flags (8 byte connection_id)
1520 0x10, 0x32, 0x54, 0x76,
1521 0x98, 0xBA, 0xDC, 0xFE,
1522 // packet sequence number
1523 0xBC, 0x9A, 0x78, 0x56,
1528 // frame type (stream frame with fin)
1531 0x04, 0x03, 0x02, 0x01,
1533 0x54, 0x76, 0x10, 0x32,
1534 0xDC, 0xFE, 0x98, 0xBA,
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));
1550 ASSERT_EQ(0u, visitor_.stream_frames_.size());
1551 EXPECT_EQ(0u, visitor_.ack_frames_.size());
1554 TEST_P(QuicFramerTest, RejectPublicHeader) {
1555 visitor_.accept_public_header_ = false;
1557 unsigned char packet[] = {
1558 // public flags (8 byte connection_id)
1561 0x10, 0x32, 0x54, 0x76,
1562 0x98, 0xBA, 0xDC, 0xFE,
1565 QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
1566 EXPECT_TRUE(framer_.ProcessPacket(encrypted));
1568 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
1569 ASSERT_TRUE(visitor_.public_header_.get());
1570 ASSERT_FALSE(visitor_.header_.get());
1573 TEST_P(QuicFramerTest, RevivedStreamFrame) {
1574 unsigned char payload[] = {
1575 // frame type (stream frame with fin)
1578 0x04, 0x03, 0x02, 0x01,
1580 0x54, 0x76, 0x10, 0x32,
1581 0xDC, 0xFE, 0x98, 0xBA,
1590 QuicPacketHeader header;
1591 header.public_header.connection_id = GG_UINT64_C(0xFEDCBA9876543210);
1592 header.public_header.reset_flag = false;
1593 header.public_header.version_flag = false;
1594 header.fec_flag = true;
1595 header.entropy_flag = true;
1596 header.packet_sequence_number = GG_UINT64_C(0x123456789ABC);
1597 header.fec_group = 0;
1599 // Do not encrypt the payload because the revived payload is post-encryption.
1600 EXPECT_TRUE(framer_.ProcessRevivedPacket(&header,
1601 StringPiece(AsChars(payload),
1602 arraysize(payload))));
1604 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
1605 ASSERT_EQ(1, visitor_.revived_packets_);
1606 ASSERT_TRUE(visitor_.header_.get());
1607 EXPECT_EQ(GG_UINT64_C(0xFEDCBA9876543210),
1608 visitor_.header_->public_header.connection_id);
1609 EXPECT_FALSE(visitor_.header_->public_header.reset_flag);
1610 EXPECT_FALSE(visitor_.header_->public_header.version_flag);
1611 EXPECT_TRUE(visitor_.header_->fec_flag);
1612 EXPECT_TRUE(visitor_.header_->entropy_flag);
1613 EXPECT_EQ(1 << (header.packet_sequence_number % 8),
1614 visitor_.header_->entropy_hash);
1615 EXPECT_EQ(GG_UINT64_C(0x123456789ABC),
1616 visitor_.header_->packet_sequence_number);
1617 EXPECT_EQ(NOT_IN_FEC_GROUP, visitor_.header_->is_in_fec_group);
1618 EXPECT_EQ(0x00u, visitor_.header_->fec_group);
1620 ASSERT_EQ(1u, visitor_.stream_frames_.size());
1621 EXPECT_EQ(0u, visitor_.ack_frames_.size());
1622 EXPECT_EQ(GG_UINT64_C(0x01020304), visitor_.stream_frames_[0]->stream_id);
1623 EXPECT_TRUE(visitor_.stream_frames_[0]->fin);
1624 EXPECT_EQ(GG_UINT64_C(0xBA98FEDC32107654),
1625 visitor_.stream_frames_[0]->offset);
1626 CheckStreamFrameData("hello world!", visitor_.stream_frames_[0]);
1629 TEST_P(QuicFramerTest, StreamFrameInFecGroup) {
1630 unsigned char packet[] = {
1631 // public flags (8 byte connection_id)
1634 0x10, 0x32, 0x54, 0x76,
1635 0x98, 0xBA, 0xDC, 0xFE,
1636 // packet sequence number
1637 0xBC, 0x9A, 0x78, 0x56,
1639 // private flags (fec group)
1641 // first fec protected packet offset
1644 // frame type (stream frame with fin)
1647 0x04, 0x03, 0x02, 0x01,
1649 0x54, 0x76, 0x10, 0x32,
1650 0xDC, 0xFE, 0x98, 0xBA,
1659 QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
1660 EXPECT_TRUE(framer_.ProcessPacket(encrypted));
1662 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
1663 ASSERT_TRUE(visitor_.header_.get());
1664 EXPECT_TRUE(CheckDecryption(encrypted, !kIncludeVersion));
1665 EXPECT_EQ(IN_FEC_GROUP, visitor_.header_->is_in_fec_group);
1666 EXPECT_EQ(GG_UINT64_C(0x341256789ABA),
1667 visitor_.header_->fec_group);
1668 const size_t fec_offset =
1669 GetStartOfFecProtectedData(PACKET_8BYTE_CONNECTION_ID,
1671 PACKET_6BYTE_SEQUENCE_NUMBER);
1673 string(AsChars(packet) + fec_offset, arraysize(packet) - fec_offset),
1674 visitor_.fec_protected_payload_);
1676 ASSERT_EQ(1u, visitor_.stream_frames_.size());
1677 EXPECT_EQ(0u, visitor_.ack_frames_.size());
1678 EXPECT_EQ(GG_UINT64_C(0x01020304), visitor_.stream_frames_[0]->stream_id);
1679 EXPECT_TRUE(visitor_.stream_frames_[0]->fin);
1680 EXPECT_EQ(GG_UINT64_C(0xBA98FEDC32107654),
1681 visitor_.stream_frames_[0]->offset);
1682 CheckStreamFrameData("hello world!", visitor_.stream_frames_[0]);
1685 TEST_P(QuicFramerTest, AckFrame) {
1686 unsigned char packet[] = {
1687 // public flags (8 byte connection_id)
1690 0x10, 0x32, 0x54, 0x76,
1691 0x98, 0xBA, 0xDC, 0xFE,
1692 // packet sequence number
1693 0xA8, 0x9A, 0x78, 0x56,
1695 // private flags (entropy)
1698 // frame type (ack frame)
1699 // (has nacks, not truncated, 6 byte largest observed, 1 byte delta)
1701 // entropy hash of all received packets.
1703 // largest observed packet sequence number
1704 0xBF, 0x9A, 0x78, 0x56,
1708 // num missing packets
1710 // missing packet delta
1712 // 0 more missing packets in range.
1714 // Number of revived packets.
1718 QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
1719 EXPECT_TRUE(framer_.ProcessPacket(encrypted));
1721 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
1722 ASSERT_TRUE(visitor_.header_.get());
1723 EXPECT_TRUE(CheckDecryption(encrypted, !kIncludeVersion));
1725 EXPECT_EQ(0u, visitor_.stream_frames_.size());
1726 ASSERT_EQ(1u, visitor_.ack_frames_.size());
1727 const QuicAckFrame& frame = *visitor_.ack_frames_[0];
1728 EXPECT_EQ(0xBA, frame.entropy_hash);
1729 EXPECT_EQ(GG_UINT64_C(0x0123456789ABF), frame.largest_observed);
1730 ASSERT_EQ(1u, frame.missing_packets.size());
1731 SequenceNumberSet::const_iterator missing_iter =
1732 frame.missing_packets.begin();
1733 EXPECT_EQ(GG_UINT64_C(0x0123456789ABE), *missing_iter);
1735 const size_t kReceivedEntropyOffset = kQuicFrameTypeSize;
1736 const size_t kLargestObservedOffset = kReceivedEntropyOffset +
1737 kQuicEntropyHashSize;
1738 const size_t kMissingDeltaTimeOffset = kLargestObservedOffset +
1739 PACKET_6BYTE_SEQUENCE_NUMBER;
1740 const size_t kNumMissingPacketOffset = kMissingDeltaTimeOffset +
1741 kQuicDeltaTimeLargestObservedSize;
1742 const size_t kMissingPacketsOffset = kNumMissingPacketOffset +
1743 kNumberOfNackRangesSize;
1744 const size_t kMissingPacketsRange = kMissingPacketsOffset +
1745 PACKET_1BYTE_SEQUENCE_NUMBER;
1746 const size_t kRevivedPacketsLength = kMissingPacketsRange +
1747 PACKET_1BYTE_SEQUENCE_NUMBER;
1748 // Now test framing boundaries
1749 const size_t ack_frame_size = kRevivedPacketsLength +
1750 PACKET_1BYTE_SEQUENCE_NUMBER;
1751 for (size_t i = kQuicFrameTypeSize; i < ack_frame_size; ++i) {
1752 string expected_error;
1753 if (i < kLargestObservedOffset) {
1754 expected_error = "Unable to read entropy hash for received packets.";
1755 } else if (i < kMissingDeltaTimeOffset) {
1756 expected_error = "Unable to read largest observed.";
1757 } else if (i < kNumMissingPacketOffset) {
1758 expected_error = "Unable to read delta time largest observed.";
1759 } else if (i < kMissingPacketsOffset) {
1760 expected_error = "Unable to read num missing packet ranges.";
1761 } else if (i < kMissingPacketsRange) {
1762 expected_error = "Unable to read missing sequence number delta.";
1763 } else if (i < kRevivedPacketsLength) {
1764 expected_error = "Unable to read missing sequence number range.";
1766 expected_error = "Unable to read num revived packets.";
1768 CheckProcessingFails(
1770 i + GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID, !kIncludeVersion,
1771 PACKET_6BYTE_SEQUENCE_NUMBER, NOT_IN_FEC_GROUP),
1772 expected_error, QUIC_INVALID_ACK_DATA);
1776 TEST_P(QuicFramerTest, AckFrameRevivedPackets) {
1777 unsigned char packet[] = {
1778 // public flags (8 byte connection_id)
1781 0x10, 0x32, 0x54, 0x76,
1782 0x98, 0xBA, 0xDC, 0xFE,
1783 // packet sequence number
1784 0xA8, 0x9A, 0x78, 0x56,
1786 // private flags (entropy)
1789 // frame type (ack frame)
1790 // (has nacks, not truncated, 6 byte largest observed, 1 byte delta)
1792 // entropy hash of all received packets.
1794 // largest observed packet sequence number
1795 0xBF, 0x9A, 0x78, 0x56,
1799 // num missing packets
1801 // missing packet delta
1803 // 0 more missing packets in range.
1805 // Number of revived packets.
1807 // Revived packet sequence number.
1808 0xBE, 0x9A, 0x78, 0x56,
1812 QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
1813 EXPECT_TRUE(framer_.ProcessPacket(encrypted));
1815 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
1816 ASSERT_TRUE(visitor_.header_.get());
1817 EXPECT_TRUE(CheckDecryption(encrypted, !kIncludeVersion));
1819 EXPECT_EQ(0u, visitor_.stream_frames_.size());
1820 ASSERT_EQ(1u, visitor_.ack_frames_.size());
1821 const QuicAckFrame& frame = *visitor_.ack_frames_[0];
1822 EXPECT_EQ(0xBA, frame.entropy_hash);
1823 EXPECT_EQ(GG_UINT64_C(0x0123456789ABF), frame.largest_observed);
1824 ASSERT_EQ(1u, frame.missing_packets.size());
1825 SequenceNumberSet::const_iterator missing_iter =
1826 frame.missing_packets.begin();
1827 EXPECT_EQ(GG_UINT64_C(0x0123456789ABE), *missing_iter);
1829 const size_t kReceivedEntropyOffset = kQuicFrameTypeSize;
1830 const size_t kLargestObservedOffset = kReceivedEntropyOffset +
1831 kQuicEntropyHashSize;
1832 const size_t kMissingDeltaTimeOffset = kLargestObservedOffset +
1833 PACKET_6BYTE_SEQUENCE_NUMBER;
1834 const size_t kNumMissingPacketOffset = kMissingDeltaTimeOffset +
1835 kQuicDeltaTimeLargestObservedSize;
1836 const size_t kMissingPacketsOffset = kNumMissingPacketOffset +
1837 kNumberOfNackRangesSize;
1838 const size_t kMissingPacketsRange = kMissingPacketsOffset +
1839 PACKET_1BYTE_SEQUENCE_NUMBER;
1840 const size_t kRevivedPacketsLength = kMissingPacketsRange +
1841 PACKET_1BYTE_SEQUENCE_NUMBER;
1842 const size_t kRevivedPacketSequenceNumberLength = kRevivedPacketsLength +
1843 PACKET_1BYTE_SEQUENCE_NUMBER;
1844 // Now test framing boundaries
1845 const size_t ack_frame_size = kRevivedPacketSequenceNumberLength +
1846 PACKET_6BYTE_SEQUENCE_NUMBER;
1847 for (size_t i = kQuicFrameTypeSize; i < ack_frame_size; ++i) {
1848 string expected_error;
1849 if (i < kReceivedEntropyOffset) {
1850 expected_error = "Unable to read least unacked delta.";
1851 } else if (i < kLargestObservedOffset) {
1852 expected_error = "Unable to read entropy hash for received packets.";
1853 } else if (i < kMissingDeltaTimeOffset) {
1854 expected_error = "Unable to read largest observed.";
1855 } else if (i < kNumMissingPacketOffset) {
1856 expected_error = "Unable to read delta time largest observed.";
1857 } else if (i < kMissingPacketsOffset) {
1858 expected_error = "Unable to read num missing packet ranges.";
1859 } else if (i < kMissingPacketsRange) {
1860 expected_error = "Unable to read missing sequence number delta.";
1861 } else if (i < kRevivedPacketsLength) {
1862 expected_error = "Unable to read missing sequence number range.";
1863 } else if (i < kRevivedPacketSequenceNumberLength) {
1864 expected_error = "Unable to read num revived packets.";
1866 expected_error = "Unable to read revived packet.";
1868 CheckProcessingFails(
1870 i + GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID, !kIncludeVersion,
1871 PACKET_6BYTE_SEQUENCE_NUMBER, NOT_IN_FEC_GROUP),
1872 expected_error, QUIC_INVALID_ACK_DATA);
1876 TEST_P(QuicFramerTest, AckFrameNoNacks) {
1877 unsigned char packet[] = {
1878 // public flags (8 byte connection_id)
1881 0x10, 0x32, 0x54, 0x76,
1882 0x98, 0xBA, 0xDC, 0xFE,
1883 // packet sequence number
1884 0xA8, 0x9A, 0x78, 0x56,
1886 // private flags (entropy)
1889 // frame type (ack frame)
1890 // (no nacks, not truncated, 6 byte largest observed, 1 byte delta)
1892 // entropy hash of all received packets.
1894 // largest observed packet sequence number
1895 0xBF, 0x9A, 0x78, 0x56,
1901 QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
1902 EXPECT_TRUE(framer_.ProcessPacket(encrypted));
1904 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
1905 ASSERT_TRUE(visitor_.header_.get());
1906 EXPECT_TRUE(CheckDecryption(encrypted, !kIncludeVersion));
1908 EXPECT_EQ(0u, visitor_.stream_frames_.size());
1909 ASSERT_EQ(1u, visitor_.ack_frames_.size());
1910 QuicAckFrame* frame = visitor_.ack_frames_[0];
1911 EXPECT_EQ(0xBA, frame->entropy_hash);
1912 EXPECT_EQ(GG_UINT64_C(0x0123456789ABF), frame->largest_observed);
1913 ASSERT_EQ(0u, frame->missing_packets.size());
1915 // Verify that the packet re-serializes identically.
1917 frames.push_back(QuicFrame(frame));
1918 scoped_ptr<QuicPacket> data(BuildDataPacket(*visitor_.header_, frames));
1919 ASSERT_TRUE(data != NULL);
1921 test::CompareCharArraysWithHexError("constructed packet",
1922 data->data(), data->length(),
1923 AsChars(packet), arraysize(packet));
1926 TEST_P(QuicFramerTest, AckFrame500Nacks) {
1927 unsigned char packet[] = {
1928 // public flags (8 byte connection_id)
1931 0x10, 0x32, 0x54, 0x76,
1932 0x98, 0xBA, 0xDC, 0xFE,
1933 // packet sequence number
1934 0xA8, 0x9A, 0x78, 0x56,
1936 // private flags (entropy)
1939 // frame type (ack frame)
1940 // (has nacks, not truncated, 6 byte largest observed, 1 byte delta)
1942 // entropy hash of all received packets.
1944 // largest observed packet sequence number
1945 0xBF, 0x9A, 0x78, 0x56,
1949 // num missing packet ranges
1951 // missing packet delta
1953 // 243 more missing packets in range.
1954 // The ranges are listed in this order so the re-constructed packet matches.
1956 // No gap between ranges
1958 // 255 more missing packets in range.
1960 // No revived packets.
1964 QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
1965 EXPECT_TRUE(framer_.ProcessPacket(encrypted));
1967 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
1968 ASSERT_TRUE(visitor_.header_.get());
1969 EXPECT_TRUE(CheckDecryption(encrypted, !kIncludeVersion));
1971 EXPECT_EQ(0u, visitor_.stream_frames_.size());
1972 ASSERT_EQ(1u, visitor_.ack_frames_.size());
1973 QuicAckFrame* frame = visitor_.ack_frames_[0];
1974 EXPECT_EQ(0xBA, frame->entropy_hash);
1975 EXPECT_EQ(GG_UINT64_C(0x0123456789ABF), frame->largest_observed);
1976 EXPECT_EQ(0u, frame->revived_packets.size());
1977 ASSERT_EQ(500u, frame->missing_packets.size());
1978 SequenceNumberSet::const_iterator first_missing_iter =
1979 frame->missing_packets.begin();
1980 EXPECT_EQ(GG_UINT64_C(0x0123456789ABE) - 499, *first_missing_iter);
1981 SequenceNumberSet::const_reverse_iterator last_missing_iter =
1982 frame->missing_packets.rbegin();
1983 EXPECT_EQ(GG_UINT64_C(0x0123456789ABE), *last_missing_iter);
1985 // Verify that the packet re-serializes identically.
1987 frames.push_back(QuicFrame(frame));
1988 scoped_ptr<QuicPacket> data(BuildDataPacket(*visitor_.header_, frames));
1989 ASSERT_TRUE(data != NULL);
1991 test::CompareCharArraysWithHexError("constructed packet",
1992 data->data(), data->length(),
1993 AsChars(packet), arraysize(packet));
1996 TEST_P(QuicFramerTest, CongestionFeedbackFrameTCP) {
1997 unsigned char packet[] = {
1998 // public flags (8 byte connection_id)
2001 0x10, 0x32, 0x54, 0x76,
2002 0x98, 0xBA, 0xDC, 0xFE,
2003 // packet sequence number
2004 0xBC, 0x9A, 0x78, 0x56,
2009 // frame type (congestion feedback frame)
2011 // congestion feedback type (tcp)
2013 // ack_frame.feedback.tcp.receive_window
2017 QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
2018 EXPECT_TRUE(framer_.ProcessPacket(encrypted));
2020 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
2021 ASSERT_TRUE(visitor_.header_.get());
2022 EXPECT_TRUE(CheckDecryption(encrypted, !kIncludeVersion));
2024 EXPECT_EQ(0u, visitor_.stream_frames_.size());
2025 ASSERT_EQ(1u, visitor_.congestion_feedback_frames_.size());
2026 const QuicCongestionFeedbackFrame& frame =
2027 *visitor_.congestion_feedback_frames_[0];
2028 ASSERT_EQ(kTCP, frame.type);
2029 EXPECT_EQ(0x4030u, frame.tcp.receive_window);
2031 // Now test framing boundaries
2032 for (size_t i = kQuicFrameTypeSize; i < 4; ++i) {
2033 string expected_error;
2035 expected_error = "Unable to read congestion feedback type.";
2037 expected_error = "Unable to read receive window.";
2039 CheckProcessingFails(
2041 i + GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID, !kIncludeVersion,
2042 PACKET_6BYTE_SEQUENCE_NUMBER, NOT_IN_FEC_GROUP),
2043 expected_error, QUIC_INVALID_CONGESTION_FEEDBACK_DATA);
2047 TEST_P(QuicFramerTest, CongestionFeedbackFrameInvalidFeedback) {
2048 unsigned char packet[] = {
2049 // public flags (8 byte connection_id)
2052 0x10, 0x32, 0x54, 0x76,
2053 0x98, 0xBA, 0xDC, 0xFE,
2054 // packet sequence number
2055 0xBC, 0x9A, 0x78, 0x56,
2060 // frame type (congestion feedback frame)
2062 // congestion feedback type (invalid)
2066 QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
2067 EXPECT_FALSE(framer_.ProcessPacket(encrypted));
2068 EXPECT_TRUE(CheckDecryption(encrypted, !kIncludeVersion));
2069 EXPECT_EQ(QUIC_INVALID_CONGESTION_FEEDBACK_DATA, framer_.error());
2072 TEST_P(QuicFramerTest, StopWaitingFrame) {
2073 unsigned char packet[] = {
2074 // public flags (8 byte connection_id)
2077 0x10, 0x32, 0x54, 0x76,
2078 0x98, 0xBA, 0xDC, 0xFE,
2079 // packet sequence number
2080 0xA8, 0x9A, 0x78, 0x56,
2082 // private flags (entropy)
2085 // frame type (ack frame)
2086 // (has nacks, not truncated, 6 byte largest observed, 1 byte delta)
2088 // entropy hash of sent packets till least awaiting - 1.
2090 // least packet sequence number awaiting an ack, delta from sequence number.
2091 0x08, 0x00, 0x00, 0x00,
2095 QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
2096 EXPECT_TRUE(framer_.ProcessPacket(encrypted));
2098 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
2099 ASSERT_TRUE(visitor_.header_.get());
2100 EXPECT_TRUE(CheckDecryption(encrypted, !kIncludeVersion));
2102 EXPECT_EQ(0u, visitor_.stream_frames_.size());
2103 ASSERT_EQ(1u, visitor_.stop_waiting_frames_.size());
2104 const QuicStopWaitingFrame& frame = *visitor_.stop_waiting_frames_[0];
2105 EXPECT_EQ(0xAB, frame.entropy_hash);
2106 EXPECT_EQ(GG_UINT64_C(0x0123456789AA0), frame.least_unacked);
2108 const size_t kSentEntropyOffset = kQuicFrameTypeSize;
2109 const size_t kLeastUnackedOffset = kSentEntropyOffset + kQuicEntropyHashSize;
2110 const size_t frame_size = 7;
2111 for (size_t i = kQuicFrameTypeSize; i < frame_size; ++i) {
2112 string expected_error;
2113 if (i < kLeastUnackedOffset) {
2114 expected_error = "Unable to read entropy hash for sent packets.";
2116 expected_error = "Unable to read least unacked delta.";
2118 CheckProcessingFails(
2120 i + GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID, !kIncludeVersion,
2121 PACKET_6BYTE_SEQUENCE_NUMBER, NOT_IN_FEC_GROUP),
2122 expected_error, QUIC_INVALID_STOP_WAITING_DATA);
2126 TEST_P(QuicFramerTest, RstStreamFrameQuic) {
2127 unsigned char packet[] = {
2128 // public flags (8 byte connection_id)
2131 0x10, 0x32, 0x54, 0x76,
2132 0x98, 0xBA, 0xDC, 0xFE,
2133 // packet sequence number
2134 0xBC, 0x9A, 0x78, 0x56,
2139 // frame type (rst stream frame)
2142 0x04, 0x03, 0x02, 0x01,
2145 0x01, 0x02, 0x03, 0x04,
2146 0x05, 0x06, 0x07, 0x08,
2149 0x01, 0x00, 0x00, 0x00,
2151 // error details length
2160 QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
2161 EXPECT_TRUE(framer_.ProcessPacket(encrypted));
2163 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
2164 ASSERT_TRUE(visitor_.header_.get());
2165 EXPECT_TRUE(CheckDecryption(encrypted, !kIncludeVersion));
2167 EXPECT_EQ(GG_UINT64_C(0x01020304), visitor_.rst_stream_frame_.stream_id);
2168 EXPECT_EQ(0x01, visitor_.rst_stream_frame_.error_code);
2169 EXPECT_EQ("because I can", visitor_.rst_stream_frame_.error_details);
2170 EXPECT_EQ(GG_UINT64_C(0x0807060504030201),
2171 visitor_.rst_stream_frame_.byte_offset);
2173 // Now test framing boundaries
2174 for (size_t i = kQuicFrameTypeSize;
2175 i < QuicFramer::GetMinRstStreamFrameSize(); ++i) {
2176 string expected_error;
2177 if (i < kQuicFrameTypeSize + kQuicMaxStreamIdSize) {
2178 expected_error = "Unable to read stream_id.";
2179 } else if (i < kQuicFrameTypeSize + kQuicMaxStreamIdSize +
2180 + kQuicMaxStreamOffsetSize) {
2181 expected_error = "Unable to read rst stream sent byte offset.";
2182 } else if (i < kQuicFrameTypeSize + kQuicMaxStreamIdSize +
2183 + kQuicMaxStreamOffsetSize + kQuicErrorCodeSize) {
2184 expected_error = "Unable to read rst stream error code.";
2186 expected_error = "Unable to read rst stream error details.";
2188 CheckProcessingFails(
2190 i + GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID, !kIncludeVersion,
2191 PACKET_6BYTE_SEQUENCE_NUMBER, NOT_IN_FEC_GROUP),
2192 expected_error, QUIC_INVALID_RST_STREAM_DATA);
2196 TEST_P(QuicFramerTest, ConnectionCloseFrame) {
2197 unsigned char packet[] = {
2198 // public flags (8 byte connection_id)
2201 0x10, 0x32, 0x54, 0x76,
2202 0x98, 0xBA, 0xDC, 0xFE,
2203 // packet sequence number
2204 0xBC, 0x9A, 0x78, 0x56,
2209 // frame type (connection close frame)
2212 0x11, 0x00, 0x00, 0x00,
2214 // error details length
2223 QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
2224 EXPECT_TRUE(framer_.ProcessPacket(encrypted));
2226 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
2227 ASSERT_TRUE(visitor_.header_.get());
2228 EXPECT_TRUE(CheckDecryption(encrypted, !kIncludeVersion));
2230 EXPECT_EQ(0u, visitor_.stream_frames_.size());
2232 EXPECT_EQ(0x11, visitor_.connection_close_frame_.error_code);
2233 EXPECT_EQ("because I can", visitor_.connection_close_frame_.error_details);
2235 ASSERT_EQ(0u, visitor_.ack_frames_.size());
2237 // Now test framing boundaries
2238 for (size_t i = kQuicFrameTypeSize;
2239 i < QuicFramer::GetMinConnectionCloseFrameSize(); ++i) {
2240 string expected_error;
2241 if (i < kQuicFrameTypeSize + kQuicErrorCodeSize) {
2242 expected_error = "Unable to read connection close error code.";
2244 expected_error = "Unable to read connection close error details.";
2246 CheckProcessingFails(
2248 i + GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID, !kIncludeVersion,
2249 PACKET_6BYTE_SEQUENCE_NUMBER, NOT_IN_FEC_GROUP),
2250 expected_error, QUIC_INVALID_CONNECTION_CLOSE_DATA);
2254 TEST_P(QuicFramerTest, GoAwayFrame) {
2255 unsigned char packet[] = {
2256 // public flags (8 byte connection_id)
2259 0x10, 0x32, 0x54, 0x76,
2260 0x98, 0xBA, 0xDC, 0xFE,
2261 // packet sequence number
2262 0xBC, 0x9A, 0x78, 0x56,
2267 // frame type (go away frame)
2270 0x09, 0x00, 0x00, 0x00,
2272 0x04, 0x03, 0x02, 0x01,
2273 // error details length
2282 QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
2283 EXPECT_TRUE(framer_.ProcessPacket(encrypted));
2285 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
2286 ASSERT_TRUE(visitor_.header_.get());
2287 EXPECT_TRUE(CheckDecryption(encrypted, !kIncludeVersion));
2289 EXPECT_EQ(GG_UINT64_C(0x01020304),
2290 visitor_.goaway_frame_.last_good_stream_id);
2291 EXPECT_EQ(0x9, visitor_.goaway_frame_.error_code);
2292 EXPECT_EQ("because I can", visitor_.goaway_frame_.reason_phrase);
2294 const size_t reason_size = arraysize("because I can") - 1;
2295 // Now test framing boundaries
2296 for (size_t i = kQuicFrameTypeSize;
2297 i < QuicFramer::GetMinGoAwayFrameSize() + reason_size; ++i) {
2298 string expected_error;
2299 if (i < kQuicFrameTypeSize + kQuicErrorCodeSize) {
2300 expected_error = "Unable to read go away error code.";
2301 } else if (i < kQuicFrameTypeSize + kQuicErrorCodeSize +
2302 kQuicMaxStreamIdSize) {
2303 expected_error = "Unable to read last good stream id.";
2305 expected_error = "Unable to read goaway reason.";
2307 CheckProcessingFails(
2309 i + GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID, !kIncludeVersion,
2310 PACKET_6BYTE_SEQUENCE_NUMBER, NOT_IN_FEC_GROUP),
2311 expected_error, QUIC_INVALID_GOAWAY_DATA);
2315 TEST_P(QuicFramerTest, WindowUpdateFrame) {
2316 unsigned char packet[] = {
2317 // public flags (8 byte connection_id)
2320 0x10, 0x32, 0x54, 0x76,
2321 0x98, 0xBA, 0xDC, 0xFE,
2322 // packet sequence number
2323 0xBC, 0x9A, 0x78, 0x56,
2328 // frame type (window update frame)
2331 0x04, 0x03, 0x02, 0x01,
2333 0x05, 0x06, 0x07, 0x08,
2334 0x09, 0x0a, 0x0b, 0x0c,
2337 QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
2339 EXPECT_TRUE(framer_.ProcessPacket(encrypted));
2341 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
2342 ASSERT_TRUE(visitor_.header_.get());
2343 EXPECT_TRUE(CheckDecryption(encrypted, !kIncludeVersion));
2345 EXPECT_EQ(GG_UINT64_C(0x01020304),
2346 visitor_.window_update_frame_.stream_id);
2347 EXPECT_EQ(GG_UINT64_C(0x0c0b0a0908070605),
2348 visitor_.window_update_frame_.byte_offset);
2350 // Now test framing boundaries
2351 for (size_t i = kQuicFrameTypeSize;
2352 i < QuicFramer::GetWindowUpdateFrameSize(); ++i) {
2353 string expected_error;
2354 if (i < kQuicFrameTypeSize + kQuicMaxStreamIdSize) {
2355 expected_error = "Unable to read stream_id.";
2357 expected_error = "Unable to read window byte_offset.";
2359 CheckProcessingFails(
2361 i + GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID, !kIncludeVersion,
2362 PACKET_6BYTE_SEQUENCE_NUMBER, NOT_IN_FEC_GROUP),
2363 expected_error, QUIC_INVALID_WINDOW_UPDATE_DATA);
2367 TEST_P(QuicFramerTest, BlockedFrame) {
2368 unsigned char packet[] = {
2369 // public flags (8 byte connection_id)
2372 0x10, 0x32, 0x54, 0x76,
2373 0x98, 0xBA, 0xDC, 0xFE,
2374 // packet sequence number
2375 0xBC, 0x9A, 0x78, 0x56,
2380 // frame type (blocked frame)
2383 0x04, 0x03, 0x02, 0x01,
2386 QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
2388 EXPECT_TRUE(framer_.ProcessPacket(encrypted));
2390 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
2391 ASSERT_TRUE(visitor_.header_.get());
2392 EXPECT_TRUE(CheckDecryption(encrypted, !kIncludeVersion));
2394 EXPECT_EQ(GG_UINT64_C(0x01020304),
2395 visitor_.blocked_frame_.stream_id);
2397 // Now test framing boundaries
2398 for (size_t i = kQuicFrameTypeSize; i < QuicFramer::GetBlockedFrameSize();
2400 string expected_error = "Unable to read stream_id.";
2401 CheckProcessingFails(
2403 i + GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID, !kIncludeVersion,
2404 PACKET_6BYTE_SEQUENCE_NUMBER, NOT_IN_FEC_GROUP),
2405 expected_error, QUIC_INVALID_BLOCKED_DATA);
2409 TEST_P(QuicFramerTest, PingFrame) {
2410 if (version_ <= QUIC_VERSION_16) {
2414 unsigned char packet[] = {
2415 // public flags (8 byte connection_id)
2418 0x10, 0x32, 0x54, 0x76,
2419 0x98, 0xBA, 0xDC, 0xFE,
2420 // packet sequence number
2421 0xBC, 0x9A, 0x78, 0x56,
2426 // frame type (ping frame)
2430 QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
2431 EXPECT_TRUE(framer_.ProcessPacket(encrypted));
2433 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
2434 ASSERT_TRUE(visitor_.header_.get());
2435 EXPECT_TRUE(CheckDecryption(encrypted, !kIncludeVersion));
2437 EXPECT_EQ(1u, visitor_.ping_frames_.size());
2439 // No need to check the PING frame boundaries because it has no payload.
2442 TEST_P(QuicFramerTest, PublicResetPacket) {
2443 unsigned char packet[] = {
2444 // public flags (public reset, 8 byte connection_id)
2447 0x10, 0x32, 0x54, 0x76,
2448 0x98, 0xBA, 0xDC, 0xFE,
2449 // message tag (kPRST)
2451 // num_entries (2) + padding
2452 0x02, 0x00, 0x00, 0x00,
2456 0x08, 0x00, 0x00, 0x00,
2460 0x10, 0x00, 0x00, 0x00,
2462 0x89, 0x67, 0x45, 0x23,
2463 0x01, 0xEF, 0xCD, 0xAB,
2464 // rejected sequence number
2465 0xBC, 0x9A, 0x78, 0x56,
2466 0x34, 0x12, 0x00, 0x00,
2469 QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
2470 EXPECT_TRUE(framer_.ProcessPacket(encrypted));
2471 ASSERT_EQ(QUIC_NO_ERROR, framer_.error());
2472 ASSERT_TRUE(visitor_.public_reset_packet_.get());
2473 EXPECT_EQ(GG_UINT64_C(0xFEDCBA9876543210),
2474 visitor_.public_reset_packet_->public_header.connection_id);
2475 EXPECT_TRUE(visitor_.public_reset_packet_->public_header.reset_flag);
2476 EXPECT_FALSE(visitor_.public_reset_packet_->public_header.version_flag);
2477 EXPECT_EQ(GG_UINT64_C(0xABCDEF0123456789),
2478 visitor_.public_reset_packet_->nonce_proof);
2479 EXPECT_EQ(GG_UINT64_C(0x123456789ABC),
2480 visitor_.public_reset_packet_->rejected_sequence_number);
2482 visitor_.public_reset_packet_->client_address.address().empty());
2484 // Now test framing boundaries
2485 for (size_t i = 0; i < arraysize(packet); ++i) {
2486 string expected_error;
2487 DVLOG(1) << "iteration: " << i;
2488 if (i < kConnectionIdOffset) {
2489 expected_error = "Unable to read public flags.";
2490 CheckProcessingFails(packet, i, expected_error,
2491 QUIC_INVALID_PACKET_HEADER);
2492 } else if (i < kPublicResetPacketMessageTagOffset) {
2493 expected_error = "Unable to read ConnectionId.";
2494 CheckProcessingFails(packet, i, expected_error,
2495 QUIC_INVALID_PACKET_HEADER);
2497 expected_error = "Unable to read reset message.";
2498 CheckProcessingFails(packet, i, expected_error,
2499 QUIC_INVALID_PUBLIC_RST_PACKET);
2504 TEST_P(QuicFramerTest, PublicResetPacketWithTrailingJunk) {
2505 unsigned char packet[] = {
2506 // public flags (public reset, 8 byte connection_id)
2509 0x10, 0x32, 0x54, 0x76,
2510 0x98, 0xBA, 0xDC, 0xFE,
2511 // message tag (kPRST)
2513 // num_entries (2) + padding
2514 0x02, 0x00, 0x00, 0x00,
2518 0x08, 0x00, 0x00, 0x00,
2522 0x10, 0x00, 0x00, 0x00,
2524 0x89, 0x67, 0x45, 0x23,
2525 0x01, 0xEF, 0xCD, 0xAB,
2526 // rejected sequence number
2527 0xBC, 0x9A, 0x78, 0x56,
2528 0x34, 0x12, 0x00, 0x00,
2533 string expected_error = "Unable to read reset message.";
2534 CheckProcessingFails(packet, arraysize(packet), expected_error,
2535 QUIC_INVALID_PUBLIC_RST_PACKET);
2538 TEST_P(QuicFramerTest, PublicResetPacketWithClientAddress) {
2539 unsigned char packet[] = {
2540 // public flags (public reset, 8 byte connection_id)
2543 0x10, 0x32, 0x54, 0x76,
2544 0x98, 0xBA, 0xDC, 0xFE,
2545 // message tag (kPRST)
2547 // num_entries (3) + padding
2548 0x03, 0x00, 0x00, 0x00,
2552 0x08, 0x00, 0x00, 0x00,
2556 0x10, 0x00, 0x00, 0x00,
2560 0x18, 0x00, 0x00, 0x00,
2562 0x89, 0x67, 0x45, 0x23,
2563 0x01, 0xEF, 0xCD, 0xAB,
2564 // rejected sequence number
2565 0xBC, 0x9A, 0x78, 0x56,
2566 0x34, 0x12, 0x00, 0x00,
2567 // client address: 4.31.198.44:443
2569 0x04, 0x1F, 0xC6, 0x2C,
2573 QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
2574 EXPECT_TRUE(framer_.ProcessPacket(encrypted));
2575 ASSERT_EQ(QUIC_NO_ERROR, framer_.error());
2576 ASSERT_TRUE(visitor_.public_reset_packet_.get());
2577 EXPECT_EQ(GG_UINT64_C(0xFEDCBA9876543210),
2578 visitor_.public_reset_packet_->public_header.connection_id);
2579 EXPECT_TRUE(visitor_.public_reset_packet_->public_header.reset_flag);
2580 EXPECT_FALSE(visitor_.public_reset_packet_->public_header.version_flag);
2581 EXPECT_EQ(GG_UINT64_C(0xABCDEF0123456789),
2582 visitor_.public_reset_packet_->nonce_proof);
2583 EXPECT_EQ(GG_UINT64_C(0x123456789ABC),
2584 visitor_.public_reset_packet_->rejected_sequence_number);
2585 EXPECT_EQ("4.31.198.44",
2586 IPAddressToString(visitor_.public_reset_packet_->
2587 client_address.address()));
2588 EXPECT_EQ(443, visitor_.public_reset_packet_->client_address.port());
2590 // Now test framing boundaries
2591 for (size_t i = 0; i < arraysize(packet); ++i) {
2592 string expected_error;
2593 DVLOG(1) << "iteration: " << i;
2594 if (i < kConnectionIdOffset) {
2595 expected_error = "Unable to read public flags.";
2596 CheckProcessingFails(packet, i, expected_error,
2597 QUIC_INVALID_PACKET_HEADER);
2598 } else if (i < kPublicResetPacketMessageTagOffset) {
2599 expected_error = "Unable to read ConnectionId.";
2600 CheckProcessingFails(packet, i, expected_error,
2601 QUIC_INVALID_PACKET_HEADER);
2603 expected_error = "Unable to read reset message.";
2604 CheckProcessingFails(packet, i, expected_error,
2605 QUIC_INVALID_PUBLIC_RST_PACKET);
2610 TEST_P(QuicFramerTest, VersionNegotiationPacket) {
2611 unsigned char packet[] = {
2612 // public flags (version, 8 byte connection_id)
2615 0x10, 0x32, 0x54, 0x76,
2616 0x98, 0xBA, 0xDC, 0xFE,
2618 'Q', '0', GetQuicVersionDigitTens(), GetQuicVersionDigitOnes(),
2622 QuicFramerPeer::SetIsServer(&framer_, false);
2624 QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
2625 EXPECT_TRUE(framer_.ProcessPacket(encrypted));
2626 ASSERT_EQ(QUIC_NO_ERROR, framer_.error());
2627 ASSERT_TRUE(visitor_.version_negotiation_packet_.get());
2628 EXPECT_EQ(2u, visitor_.version_negotiation_packet_->versions.size());
2629 EXPECT_EQ(GetParam(), visitor_.version_negotiation_packet_->versions[0]);
2631 for (size_t i = 0; i <= kPublicFlagsSize + PACKET_8BYTE_CONNECTION_ID; ++i) {
2632 string expected_error;
2633 QuicErrorCode error_code = QUIC_INVALID_PACKET_HEADER;
2634 if (i < kConnectionIdOffset) {
2635 expected_error = "Unable to read public flags.";
2636 } else if (i < kVersionOffset) {
2637 expected_error = "Unable to read ConnectionId.";
2639 expected_error = "Unable to read supported version in negotiation.";
2640 error_code = QUIC_INVALID_VERSION_NEGOTIATION_PACKET;
2642 CheckProcessingFails(packet, i, expected_error, error_code);
2646 TEST_P(QuicFramerTest, FecPacket) {
2647 unsigned char packet[] = {
2648 // public flags (8 byte connection_id)
2651 0x10, 0x32, 0x54, 0x76,
2652 0x98, 0xBA, 0xDC, 0xFE,
2653 // packet sequence number
2654 0xBC, 0x9A, 0x78, 0x56,
2656 // private flags (fec group & FEC)
2658 // first fec protected packet offset
2668 QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
2669 EXPECT_TRUE(framer_.ProcessPacket(encrypted));
2671 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
2672 ASSERT_TRUE(visitor_.header_.get());
2673 EXPECT_TRUE(CheckDecryption(encrypted, !kIncludeVersion));
2675 EXPECT_EQ(0u, visitor_.stream_frames_.size());
2676 EXPECT_EQ(0u, visitor_.ack_frames_.size());
2677 ASSERT_EQ(1, visitor_.fec_count_);
2678 const QuicFecData& fec_data = *visitor_.fec_data_[0];
2679 EXPECT_EQ(GG_UINT64_C(0x0123456789ABB), fec_data.fec_group);
2680 EXPECT_EQ("abcdefghijklmnop", fec_data.redundancy);
2683 TEST_P(QuicFramerTest, BuildPaddingFramePacket) {
2684 QuicPacketHeader header;
2685 header.public_header.connection_id = GG_UINT64_C(0xFEDCBA9876543210);
2686 header.public_header.reset_flag = false;
2687 header.public_header.version_flag = false;
2688 header.fec_flag = false;
2689 header.entropy_flag = false;
2690 header.packet_sequence_number = GG_UINT64_C(0x123456789ABC);
2691 header.fec_group = 0;
2693 QuicPaddingFrame padding_frame;
2696 frames.push_back(QuicFrame(&padding_frame));
2698 unsigned char packet[kMaxPacketSize] = {
2699 // public flags (8 byte connection_id)
2702 0x10, 0x32, 0x54, 0x76,
2703 0x98, 0xBA, 0xDC, 0xFE,
2704 // packet sequence number
2705 0xBC, 0x9A, 0x78, 0x56,
2710 // frame type (padding frame)
2712 0x00, 0x00, 0x00, 0x00
2715 uint64 header_size =
2716 GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID, !kIncludeVersion,
2717 PACKET_6BYTE_SEQUENCE_NUMBER, NOT_IN_FEC_GROUP);
2718 memset(packet + header_size + 1, 0x00, kMaxPacketSize - header_size - 1);
2720 scoped_ptr<QuicPacket> data(BuildDataPacket(header, frames));
2721 ASSERT_TRUE(data != NULL);
2723 test::CompareCharArraysWithHexError("constructed packet",
2724 data->data(), data->length(),
2729 TEST_P(QuicFramerTest, Build4ByteSequenceNumberPaddingFramePacket) {
2730 QuicPacketHeader header;
2731 header.public_header.connection_id = GG_UINT64_C(0xFEDCBA9876543210);
2732 header.public_header.reset_flag = false;
2733 header.public_header.version_flag = false;
2734 header.fec_flag = false;
2735 header.entropy_flag = false;
2736 header.public_header.sequence_number_length = PACKET_4BYTE_SEQUENCE_NUMBER;
2737 header.packet_sequence_number = GG_UINT64_C(0x123456789ABC);
2738 header.fec_group = 0;
2740 QuicPaddingFrame padding_frame;
2743 frames.push_back(QuicFrame(&padding_frame));
2745 unsigned char packet[kMaxPacketSize] = {
2746 // public flags (8 byte connection_id and 4 byte sequence number)
2749 0x10, 0x32, 0x54, 0x76,
2750 0x98, 0xBA, 0xDC, 0xFE,
2751 // packet sequence number
2752 0xBC, 0x9A, 0x78, 0x56,
2756 // frame type (padding frame)
2758 0x00, 0x00, 0x00, 0x00
2761 uint64 header_size =
2762 GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID, !kIncludeVersion,
2763 PACKET_4BYTE_SEQUENCE_NUMBER, NOT_IN_FEC_GROUP);
2764 memset(packet + header_size + 1, 0x00, kMaxPacketSize - header_size - 1);
2766 scoped_ptr<QuicPacket> data(BuildDataPacket(header, frames));
2767 ASSERT_TRUE(data != NULL);
2769 test::CompareCharArraysWithHexError("constructed packet",
2770 data->data(), data->length(),
2775 TEST_P(QuicFramerTest, Build2ByteSequenceNumberPaddingFramePacket) {
2776 QuicPacketHeader header;
2777 header.public_header.connection_id = GG_UINT64_C(0xFEDCBA9876543210);
2778 header.public_header.reset_flag = false;
2779 header.public_header.version_flag = false;
2780 header.fec_flag = false;
2781 header.entropy_flag = false;
2782 header.public_header.sequence_number_length = PACKET_2BYTE_SEQUENCE_NUMBER;
2783 header.packet_sequence_number = GG_UINT64_C(0x123456789ABC);
2784 header.fec_group = 0;
2786 QuicPaddingFrame padding_frame;
2789 frames.push_back(QuicFrame(&padding_frame));
2791 unsigned char packet[kMaxPacketSize] = {
2792 // public flags (8 byte connection_id and 2 byte sequence number)
2795 0x10, 0x32, 0x54, 0x76,
2796 0x98, 0xBA, 0xDC, 0xFE,
2797 // packet sequence number
2802 // frame type (padding frame)
2804 0x00, 0x00, 0x00, 0x00
2807 uint64 header_size =
2808 GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID, !kIncludeVersion,
2809 PACKET_2BYTE_SEQUENCE_NUMBER, NOT_IN_FEC_GROUP);
2810 memset(packet + header_size + 1, 0x00, kMaxPacketSize - header_size - 1);
2812 scoped_ptr<QuicPacket> data(BuildDataPacket(header, frames));
2813 ASSERT_TRUE(data != NULL);
2815 test::CompareCharArraysWithHexError("constructed packet",
2816 data->data(), data->length(),
2821 TEST_P(QuicFramerTest, Build1ByteSequenceNumberPaddingFramePacket) {
2822 QuicPacketHeader header;
2823 header.public_header.connection_id = GG_UINT64_C(0xFEDCBA9876543210);
2824 header.public_header.reset_flag = false;
2825 header.public_header.version_flag = false;
2826 header.fec_flag = false;
2827 header.entropy_flag = false;
2828 header.public_header.sequence_number_length = PACKET_1BYTE_SEQUENCE_NUMBER;
2829 header.packet_sequence_number = GG_UINT64_C(0x123456789ABC);
2830 header.fec_group = 0;
2832 QuicPaddingFrame padding_frame;
2835 frames.push_back(QuicFrame(&padding_frame));
2837 unsigned char packet[kMaxPacketSize] = {
2838 // public flags (8 byte connection_id and 1 byte sequence number)
2841 0x10, 0x32, 0x54, 0x76,
2842 0x98, 0xBA, 0xDC, 0xFE,
2843 // packet sequence number
2848 // frame type (padding frame)
2850 0x00, 0x00, 0x00, 0x00
2853 uint64 header_size =
2854 GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID, !kIncludeVersion,
2855 PACKET_1BYTE_SEQUENCE_NUMBER, NOT_IN_FEC_GROUP);
2856 memset(packet + header_size + 1, 0x00, kMaxPacketSize - header_size - 1);
2858 scoped_ptr<QuicPacket> data(BuildDataPacket(header, frames));
2859 ASSERT_TRUE(data != NULL);
2861 test::CompareCharArraysWithHexError("constructed packet",
2862 data->data(), data->length(),
2867 TEST_P(QuicFramerTest, BuildStreamFramePacket) {
2868 QuicPacketHeader header;
2869 header.public_header.connection_id = GG_UINT64_C(0xFEDCBA9876543210);
2870 header.public_header.reset_flag = false;
2871 header.public_header.version_flag = false;
2872 header.fec_flag = false;
2873 header.entropy_flag = true;
2874 header.packet_sequence_number = GG_UINT64_C(0x77123456789ABC);
2875 header.fec_group = 0;
2877 QuicStreamFrame stream_frame;
2878 stream_frame.stream_id = 0x01020304;
2879 stream_frame.fin = true;
2880 stream_frame.offset = GG_UINT64_C(0xBA98FEDC32107654);
2881 stream_frame.data = MakeIOVector("hello world!");
2884 frames.push_back(QuicFrame(&stream_frame));
2886 unsigned char packet[] = {
2887 // public flags (8 byte connection_id)
2890 0x10, 0x32, 0x54, 0x76,
2891 0x98, 0xBA, 0xDC, 0xFE,
2892 // packet sequence number
2893 0xBC, 0x9A, 0x78, 0x56,
2895 // private flags (entropy)
2898 // frame type (stream frame with fin and no length)
2901 0x04, 0x03, 0x02, 0x01,
2903 0x54, 0x76, 0x10, 0x32,
2904 0xDC, 0xFE, 0x98, 0xBA,
2911 scoped_ptr<QuicPacket> data(BuildDataPacket(header, frames));
2912 ASSERT_TRUE(data != NULL);
2914 test::CompareCharArraysWithHexError("constructed packet",
2915 data->data(), data->length(),
2916 AsChars(packet), arraysize(packet));
2919 TEST_P(QuicFramerTest, BuildStreamFramePacketInFecGroup) {
2920 QuicPacketHeader header;
2921 header.public_header.connection_id = GG_UINT64_C(0xFEDCBA9876543210);
2922 header.public_header.reset_flag = false;
2923 header.public_header.version_flag = false;
2924 header.fec_flag = false;
2925 header.entropy_flag = true;
2926 header.packet_sequence_number = GG_UINT64_C(0x77123456789ABC);
2927 header.is_in_fec_group = IN_FEC_GROUP;
2928 header.fec_group = GG_UINT64_C(0x77123456789ABC);
2930 QuicStreamFrame stream_frame;
2931 stream_frame.stream_id = 0x01020304;
2932 stream_frame.fin = true;
2933 stream_frame.offset = GG_UINT64_C(0xBA98FEDC32107654);
2934 stream_frame.data = MakeIOVector("hello world!");
2937 frames.push_back(QuicFrame(&stream_frame));
2938 unsigned char packet[] = {
2939 // public flags (8 byte connection_id)
2942 0x10, 0x32, 0x54, 0x76,
2943 0x98, 0xBA, 0xDC, 0xFE,
2944 // packet sequence number
2945 0xBC, 0x9A, 0x78, 0x56,
2947 // private flags (entropy, is_in_fec_group)
2951 // frame type (stream frame with fin and data length field)
2954 0x04, 0x03, 0x02, 0x01,
2956 0x54, 0x76, 0x10, 0x32,
2957 0xDC, 0xFE, 0x98, 0xBA,
2958 // data length (since packet is in an FEC group)
2966 scoped_ptr<QuicPacket> data(BuildDataPacket(header, frames));
2967 ASSERT_TRUE(data != NULL);
2969 test::CompareCharArraysWithHexError("constructed packet",
2970 data->data(), data->length(),
2971 AsChars(packet), arraysize(packet));
2974 TEST_P(QuicFramerTest, BuildStreamFramePacketWithVersionFlag) {
2975 QuicPacketHeader header;
2976 header.public_header.connection_id = GG_UINT64_C(0xFEDCBA9876543210);
2977 header.public_header.reset_flag = false;
2978 header.public_header.version_flag = true;
2979 header.fec_flag = false;
2980 header.entropy_flag = true;
2981 header.packet_sequence_number = GG_UINT64_C(0x77123456789ABC);
2982 header.fec_group = 0;
2984 QuicStreamFrame stream_frame;
2985 stream_frame.stream_id = 0x01020304;
2986 stream_frame.fin = true;
2987 stream_frame.offset = GG_UINT64_C(0xBA98FEDC32107654);
2988 stream_frame.data = MakeIOVector("hello world!");
2991 frames.push_back(QuicFrame(&stream_frame));
2993 unsigned char packet[] = {
2994 // public flags (version, 8 byte connection_id)
2997 0x10, 0x32, 0x54, 0x76,
2998 0x98, 0xBA, 0xDC, 0xFE,
3000 'Q', '0', GetQuicVersionDigitTens(), GetQuicVersionDigitOnes(),
3001 // packet sequence number
3002 0xBC, 0x9A, 0x78, 0x56,
3004 // private flags (entropy)
3007 // frame type (stream frame with fin and no length)
3010 0x04, 0x03, 0x02, 0x01,
3012 0x54, 0x76, 0x10, 0x32,
3013 0xDC, 0xFE, 0x98, 0xBA,
3020 QuicFramerPeer::SetIsServer(&framer_, false);
3021 scoped_ptr<QuicPacket> data(BuildDataPacket(header, frames));
3022 ASSERT_TRUE(data != NULL);
3024 test::CompareCharArraysWithHexError("constructed packet",
3025 data->data(), data->length(),
3026 AsChars(packet), arraysize(packet));
3029 TEST_P(QuicFramerTest, BuildVersionNegotiationPacket) {
3030 QuicPacketPublicHeader header;
3031 header.connection_id = GG_UINT64_C(0xFEDCBA9876543210);
3032 header.reset_flag = false;
3033 header.version_flag = true;
3035 unsigned char packet[] = {
3036 // public flags (version, 8 byte connection_id)
3039 0x10, 0x32, 0x54, 0x76,
3040 0x98, 0xBA, 0xDC, 0xFE,
3042 'Q', '0', GetQuicVersionDigitTens(), GetQuicVersionDigitOnes(),
3045 QuicVersionVector versions;
3046 versions.push_back(GetParam());
3047 scoped_ptr<QuicEncryptedPacket> data(
3048 framer_.BuildVersionNegotiationPacket(header, versions));
3050 test::CompareCharArraysWithHexError("constructed packet",
3051 data->data(), data->length(),
3052 AsChars(packet), arraysize(packet));
3055 TEST_P(QuicFramerTest, BuildAckFramePacket) {
3056 QuicPacketHeader header;
3057 header.public_header.connection_id = GG_UINT64_C(0xFEDCBA9876543210);
3058 header.public_header.reset_flag = false;
3059 header.public_header.version_flag = false;
3060 header.fec_flag = false;
3061 header.entropy_flag = true;
3062 header.packet_sequence_number = GG_UINT64_C(0x770123456789AA8);
3063 header.fec_group = 0;
3065 QuicAckFrame ack_frame;
3066 ack_frame.entropy_hash = 0x43;
3067 ack_frame.largest_observed = GG_UINT64_C(0x770123456789ABF);
3068 ack_frame.delta_time_largest_observed = QuicTime::Delta::Zero();
3069 ack_frame.missing_packets.insert(
3070 GG_UINT64_C(0x770123456789ABE));
3073 frames.push_back(QuicFrame(&ack_frame));
3075 unsigned char packet[] = {
3076 // public flags (8 byte connection_id)
3079 0x10, 0x32, 0x54, 0x76,
3080 0x98, 0xBA, 0xDC, 0xFE,
3081 // packet sequence number
3082 0xA8, 0x9A, 0x78, 0x56,
3084 // private flags (entropy)
3087 // frame type (ack frame)
3088 // (has nacks, not truncated, 6 byte largest observed, 1 byte delta)
3090 // entropy hash of all received packets.
3092 // largest observed packet sequence number
3093 0xBF, 0x9A, 0x78, 0x56,
3097 // num missing packet ranges
3099 // missing packet delta
3101 // 0 more missing packets in range.
3103 // 0 revived packets.
3107 scoped_ptr<QuicPacket> data(BuildDataPacket(header, frames));
3108 ASSERT_TRUE(data != NULL);
3110 test::CompareCharArraysWithHexError("constructed packet",
3111 data->data(), data->length(),
3112 AsChars(packet), arraysize(packet));
3115 // TODO(jri): Add test for tuncated packets in which the original ack frame had
3116 // revived packets. (In both the large and small packet cases below).
3117 TEST_P(QuicFramerTest, BuildTruncatedAckFrameLargePacket) {
3118 QuicPacketHeader header;
3119 header.public_header.connection_id = GG_UINT64_C(0xFEDCBA9876543210);
3120 header.public_header.reset_flag = false;
3121 header.public_header.version_flag = false;
3122 header.fec_flag = false;
3123 header.entropy_flag = true;
3124 header.packet_sequence_number = GG_UINT64_C(0x770123456789AA8);
3125 header.fec_group = 0;
3127 QuicAckFrame ack_frame;
3128 // This entropy hash is different from what shows up in the packet below,
3129 // since entropy is recomputed by the framer on ack truncation (by
3130 // TestEntropyCalculator for this test.)
3131 ack_frame.entropy_hash = 0x43;
3132 ack_frame.largest_observed = 2 * 300;
3133 ack_frame.delta_time_largest_observed = QuicTime::Delta::Zero();
3134 for (size_t i = 1; i < 2 * 300; i += 2) {
3135 ack_frame.missing_packets.insert(i);
3139 frames.push_back(QuicFrame(&ack_frame));
3141 unsigned char packet[] = {
3142 // public flags (8 byte connection_id)
3145 0x10, 0x32, 0x54, 0x76,
3146 0x98, 0xBA, 0xDC, 0xFE,
3147 // packet sequence number
3148 0xA8, 0x9A, 0x78, 0x56,
3150 // private flags (entropy)
3153 // frame type (ack frame)
3154 // (has nacks, is truncated, 2 byte largest observed, 1 byte delta)
3156 // entropy hash of all received packets, set to 1 by TestEntropyCalculator
3157 // since ack is truncated.
3159 // 2-byte largest observed packet sequence number.
3160 // Expected to be 510 (0x1FE), since only 255 nack ranges can fit.
3164 // num missing packet ranges (limited to 255 by size of this field).
3166 // {missing packet delta, further missing packets in range}
3167 // 6 nack ranges x 42 + 3 nack ranges
3168 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3169 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3170 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3171 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3172 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3173 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3174 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3175 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3176 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3177 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3179 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3180 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3181 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3182 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3183 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3184 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3185 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3186 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3187 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3188 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3190 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3191 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3192 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3193 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
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,
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,
3204 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,
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,
3216 // 0 revived packets.
3220 scoped_ptr<QuicPacket> data(
3221 framer_.BuildDataPacket(header, frames, kMaxPacketSize).packet);
3222 ASSERT_TRUE(data != NULL);
3224 test::CompareCharArraysWithHexError("constructed packet",
3225 data->data(), data->length(),
3226 AsChars(packet), arraysize(packet));
3230 TEST_P(QuicFramerTest, BuildTruncatedAckFrameSmallPacket) {
3231 QuicPacketHeader header;
3232 header.public_header.connection_id = GG_UINT64_C(0xFEDCBA9876543210);
3233 header.public_header.reset_flag = false;
3234 header.public_header.version_flag = false;
3235 header.fec_flag = false;
3236 header.entropy_flag = true;
3237 header.packet_sequence_number = GG_UINT64_C(0x770123456789AA8);
3238 header.fec_group = 0;
3240 QuicAckFrame ack_frame;
3241 // This entropy hash is different from what shows up in the packet below,
3242 // since entropy is recomputed by the framer on ack truncation (by
3243 // TestEntropyCalculator for this test.)
3244 ack_frame.entropy_hash = 0x43;
3245 ack_frame.largest_observed = 2 * 300;
3246 ack_frame.delta_time_largest_observed = QuicTime::Delta::Zero();
3247 for (size_t i = 1; i < 2 * 300; i += 2) {
3248 ack_frame.missing_packets.insert(i);
3252 frames.push_back(QuicFrame(&ack_frame));
3254 unsigned char packet[] = {
3255 // public flags (8 byte connection_id)
3258 0x10, 0x32, 0x54, 0x76,
3259 0x98, 0xBA, 0xDC, 0xFE,
3260 // packet sequence number
3261 0xA8, 0x9A, 0x78, 0x56,
3263 // private flags (entropy)
3266 // frame type (ack frame)
3267 // (has nacks, is truncated, 2 byte largest observed, 1 byte delta)
3269 // entropy hash of all received packets, set to 1 by TestEntropyCalculator
3270 // since ack is truncated.
3272 // 2-byte largest observed packet sequence number.
3273 // Expected to be 12 (0x0C), since only 6 nack ranges can fit.
3277 // num missing packet ranges (limited to 6 by packet size of 37).
3279 // {missing packet delta, further missing packets in range}
3281 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3282 // 0 revived packets.
3286 scoped_ptr<QuicPacket> data(
3287 framer_.BuildDataPacket(header, frames, 37u).packet);
3288 ASSERT_TRUE(data != NULL);
3289 // Expect 1 byte unused since at least 2 bytes are needed to fit more nacks.
3290 EXPECT_EQ(36u, data->length());
3291 test::CompareCharArraysWithHexError("constructed packet",
3292 data->data(), data->length(),
3293 AsChars(packet), arraysize(packet));
3296 TEST_P(QuicFramerTest, BuildCongestionFeedbackFramePacketTCP) {
3297 QuicPacketHeader header;
3298 header.public_header.connection_id = GG_UINT64_C(0xFEDCBA9876543210);
3299 header.public_header.reset_flag = false;
3300 header.public_header.version_flag = false;
3301 header.fec_flag = false;
3302 header.entropy_flag = false;
3303 header.packet_sequence_number = GG_UINT64_C(0x123456789ABC);
3304 header.fec_group = 0;
3306 QuicCongestionFeedbackFrame congestion_feedback_frame;
3307 congestion_feedback_frame.type = kTCP;
3308 congestion_feedback_frame.tcp.receive_window = 0x4030;
3311 frames.push_back(QuicFrame(&congestion_feedback_frame));
3313 unsigned char packet[] = {
3314 // public flags (8 byte connection_id)
3317 0x10, 0x32, 0x54, 0x76,
3318 0x98, 0xBA, 0xDC, 0xFE,
3319 // packet sequence number
3320 0xBC, 0x9A, 0x78, 0x56,
3325 // frame type (congestion feedback frame)
3327 // congestion feedback type (TCP)
3329 // TCP receive window
3333 scoped_ptr<QuicPacket> data(BuildDataPacket(header, frames));
3334 ASSERT_TRUE(data != NULL);
3336 test::CompareCharArraysWithHexError("constructed packet",
3337 data->data(), data->length(),
3338 AsChars(packet), arraysize(packet));
3341 TEST_P(QuicFramerTest, BuildStopWaitingPacket) {
3342 QuicPacketHeader header;
3343 header.public_header.connection_id = GG_UINT64_C(0xFEDCBA9876543210);
3344 header.public_header.reset_flag = false;
3345 header.public_header.version_flag = false;
3346 header.fec_flag = false;
3347 header.entropy_flag = true;
3348 header.packet_sequence_number = GG_UINT64_C(0x770123456789AA8);
3349 header.fec_group = 0;
3351 QuicStopWaitingFrame stop_waiting_frame;
3352 stop_waiting_frame.entropy_hash = 0x14;
3353 stop_waiting_frame.least_unacked = GG_UINT64_C(0x770123456789AA0);
3356 frames.push_back(QuicFrame(&stop_waiting_frame));
3358 unsigned char packet[] = {
3359 // public flags (8 byte connection_id)
3362 0x10, 0x32, 0x54, 0x76,
3363 0x98, 0xBA, 0xDC, 0xFE,
3364 // packet sequence number
3365 0xA8, 0x9A, 0x78, 0x56,
3367 // private flags (entropy)
3370 // frame type (stop waiting frame)
3372 // entropy hash of sent packets till least awaiting - 1.
3374 // least packet sequence number awaiting an ack, delta from sequence number.
3375 0x08, 0x00, 0x00, 0x00,
3379 scoped_ptr<QuicPacket> data(BuildDataPacket(header, frames));
3380 ASSERT_TRUE(data != NULL);
3382 test::CompareCharArraysWithHexError("constructed packet",
3383 data->data(), data->length(),
3384 AsChars(packet), arraysize(packet));
3387 TEST_P(QuicFramerTest, BuildCongestionFeedbackFramePacketInvalidFeedback) {
3388 QuicPacketHeader header;
3389 header.public_header.connection_id = GG_UINT64_C(0xFEDCBA9876543210);
3390 header.public_header.reset_flag = false;
3391 header.public_header.version_flag = false;
3392 header.fec_flag = false;
3393 header.entropy_flag = false;
3394 header.packet_sequence_number = GG_UINT64_C(0x123456789ABC);
3395 header.fec_group = 0;
3397 QuicCongestionFeedbackFrame congestion_feedback_frame;
3398 congestion_feedback_frame.type =
3399 static_cast<CongestionFeedbackType>(kTimestamp + 1);
3402 frames.push_back(QuicFrame(&congestion_feedback_frame));
3404 scoped_ptr<QuicPacket> data;
3406 data.reset(BuildDataPacket(header, frames)),
3407 "AppendCongestionFeedbackFrame failed");
3408 ASSERT_TRUE(data == NULL);
3411 TEST_P(QuicFramerTest, BuildRstFramePacketQuic) {
3412 QuicPacketHeader header;
3413 header.public_header.connection_id = GG_UINT64_C(0xFEDCBA9876543210);
3414 header.public_header.reset_flag = false;
3415 header.public_header.version_flag = false;
3416 header.fec_flag = false;
3417 header.entropy_flag = false;
3418 header.packet_sequence_number = GG_UINT64_C(0x123456789ABC);
3419 header.fec_group = 0;
3421 QuicRstStreamFrame rst_frame;
3422 rst_frame.stream_id = 0x01020304;
3423 rst_frame.error_code = static_cast<QuicRstStreamErrorCode>(0x05060708);
3424 rst_frame.error_details = "because I can";
3425 rst_frame.byte_offset = 0x0807060504030201;
3427 unsigned char packet[] = {
3428 // public flags (8 byte connection_id)
3431 0x10, 0x32, 0x54, 0x76,
3432 0x98, 0xBA, 0xDC, 0xFE,
3433 // packet sequence number
3434 0xBC, 0x9A, 0x78, 0x56,
3439 // frame type (rst stream frame)
3442 0x04, 0x03, 0x02, 0x01,
3444 0x01, 0x02, 0x03, 0x04,
3445 0x05, 0x06, 0x07, 0x08,
3447 0x08, 0x07, 0x06, 0x05,
3448 // error details length
3458 frames.push_back(QuicFrame(&rst_frame));
3460 scoped_ptr<QuicPacket> data(BuildDataPacket(header, frames));
3461 ASSERT_TRUE(data != NULL);
3463 test::CompareCharArraysWithHexError("constructed packet",
3464 data->data(), data->length(),
3465 AsChars(packet), arraysize(packet));
3468 TEST_P(QuicFramerTest, BuildCloseFramePacket) {
3469 QuicPacketHeader header;
3470 header.public_header.connection_id = GG_UINT64_C(0xFEDCBA9876543210);
3471 header.public_header.reset_flag = false;
3472 header.public_header.version_flag = false;
3473 header.fec_flag = false;
3474 header.entropy_flag = true;
3475 header.packet_sequence_number = GG_UINT64_C(0x123456789ABC);
3476 header.fec_group = 0;
3478 QuicConnectionCloseFrame close_frame;
3479 close_frame.error_code = static_cast<QuicErrorCode>(0x05060708);
3480 close_frame.error_details = "because I can";
3483 frames.push_back(QuicFrame(&close_frame));
3485 unsigned char packet[] = {
3486 // public flags (8 byte connection_id)
3489 0x10, 0x32, 0x54, 0x76,
3490 0x98, 0xBA, 0xDC, 0xFE,
3491 // packet sequence number
3492 0xBC, 0x9A, 0x78, 0x56,
3494 // private flags (entropy)
3497 // frame type (connection close frame)
3500 0x08, 0x07, 0x06, 0x05,
3501 // error details length
3510 scoped_ptr<QuicPacket> data(BuildDataPacket(header, frames));
3511 ASSERT_TRUE(data != NULL);
3513 test::CompareCharArraysWithHexError("constructed packet",
3514 data->data(), data->length(),
3515 AsChars(packet), arraysize(packet));
3518 TEST_P(QuicFramerTest, BuildGoAwayPacket) {
3519 QuicPacketHeader header;
3520 header.public_header.connection_id = GG_UINT64_C(0xFEDCBA9876543210);
3521 header.public_header.reset_flag = false;
3522 header.public_header.version_flag = false;
3523 header.fec_flag = false;
3524 header.entropy_flag = true;
3525 header.packet_sequence_number = GG_UINT64_C(0x123456789ABC);
3526 header.fec_group = 0;
3528 QuicGoAwayFrame goaway_frame;
3529 goaway_frame.error_code = static_cast<QuicErrorCode>(0x05060708);
3530 goaway_frame.last_good_stream_id = 0x01020304;
3531 goaway_frame.reason_phrase = "because I can";
3534 frames.push_back(QuicFrame(&goaway_frame));
3536 unsigned char packet[] = {
3537 // public flags (8 byte connection_id)
3540 0x10, 0x32, 0x54, 0x76,
3541 0x98, 0xBA, 0xDC, 0xFE,
3542 // packet sequence number
3543 0xBC, 0x9A, 0x78, 0x56,
3545 // private flags(entropy)
3548 // frame type (go away frame)
3551 0x08, 0x07, 0x06, 0x05,
3553 0x04, 0x03, 0x02, 0x01,
3554 // error details length
3563 scoped_ptr<QuicPacket> data(BuildDataPacket(header, frames));
3564 ASSERT_TRUE(data != NULL);
3566 test::CompareCharArraysWithHexError("constructed packet",
3567 data->data(), data->length(),
3568 AsChars(packet), arraysize(packet));
3571 TEST_P(QuicFramerTest, BuildWindowUpdatePacket) {
3572 QuicPacketHeader header;
3573 header.public_header.connection_id = GG_UINT64_C(0xFEDCBA9876543210);
3574 header.public_header.reset_flag = false;
3575 header.public_header.version_flag = false;
3576 header.fec_flag = false;
3577 header.entropy_flag = true;
3578 header.packet_sequence_number = GG_UINT64_C(0x123456789ABC);
3579 header.fec_group = 0;
3581 QuicWindowUpdateFrame window_update_frame;
3582 window_update_frame.stream_id = 0x01020304;
3583 window_update_frame.byte_offset = 0x1122334455667788;
3586 frames.push_back(QuicFrame(&window_update_frame));
3588 unsigned char packet[] = {
3589 // public flags (8 byte connection_id)
3592 0x10, 0x32, 0x54, 0x76,
3593 0x98, 0xBA, 0xDC, 0xFE,
3594 // packet sequence number
3595 0xBC, 0x9A, 0x78, 0x56,
3597 // private flags(entropy)
3600 // frame type (window update frame)
3603 0x04, 0x03, 0x02, 0x01,
3605 0x88, 0x77, 0x66, 0x55,
3606 0x44, 0x33, 0x22, 0x11,
3609 scoped_ptr<QuicPacket> data(BuildDataPacket(header, frames));
3610 ASSERT_TRUE(data != NULL);
3612 test::CompareCharArraysWithHexError("constructed packet", data->data(),
3613 data->length(), AsChars(packet),
3617 TEST_P(QuicFramerTest, BuildBlockedPacket) {
3618 QuicPacketHeader header;
3619 header.public_header.connection_id = GG_UINT64_C(0xFEDCBA9876543210);
3620 header.public_header.reset_flag = false;
3621 header.public_header.version_flag = false;
3622 header.fec_flag = false;
3623 header.entropy_flag = true;
3624 header.packet_sequence_number = GG_UINT64_C(0x123456789ABC);
3625 header.fec_group = 0;
3627 QuicBlockedFrame blocked_frame;
3628 blocked_frame.stream_id = 0x01020304;
3631 frames.push_back(QuicFrame(&blocked_frame));
3633 unsigned char packet[] = {
3634 // public flags (8 byte connection_id)
3637 0x10, 0x32, 0x54, 0x76,
3638 0x98, 0xBA, 0xDC, 0xFE,
3639 // packet sequence number
3640 0xBC, 0x9A, 0x78, 0x56,
3642 // private flags(entropy)
3645 // frame type (blocked frame)
3648 0x04, 0x03, 0x02, 0x01,
3651 scoped_ptr<QuicPacket> data(BuildDataPacket(header, frames));
3652 ASSERT_TRUE(data != NULL);
3654 test::CompareCharArraysWithHexError("constructed packet", data->data(),
3655 data->length(), AsChars(packet),
3659 TEST_P(QuicFramerTest, BuildPingPacket) {
3660 QuicPacketHeader header;
3661 header.public_header.connection_id = GG_UINT64_C(0xFEDCBA9876543210);
3662 header.public_header.reset_flag = false;
3663 header.public_header.version_flag = false;
3664 header.fec_flag = false;
3665 header.entropy_flag = true;
3666 header.packet_sequence_number = GG_UINT64_C(0x123456789ABC);
3667 header.fec_group = 0;
3669 QuicPingFrame ping_frame;
3672 frames.push_back(QuicFrame(&ping_frame));
3674 unsigned char packet[] = {
3675 // public flags (8 byte connection_id)
3678 0x10, 0x32, 0x54, 0x76,
3679 0x98, 0xBA, 0xDC, 0xFE,
3680 // packet sequence number
3681 0xBC, 0x9A, 0x78, 0x56,
3683 // private flags(entropy)
3686 // frame type (ping frame)
3690 if (version_ >= QUIC_VERSION_18) {
3691 scoped_ptr<QuicPacket> data(BuildDataPacket(header, frames));
3692 ASSERT_TRUE(data != NULL);
3694 test::CompareCharArraysWithHexError("constructed packet", data->data(),
3695 data->length(), AsChars(packet),
3698 string expected_error =
3699 "Attempt to add a PingFrame in " + QuicVersionToString(version_);
3700 EXPECT_DFATAL(BuildDataPacket(header, frames),
3706 TEST_P(QuicFramerTest, BuildPublicResetPacket) {
3707 QuicPublicResetPacket reset_packet;
3708 reset_packet.public_header.connection_id = GG_UINT64_C(0xFEDCBA9876543210);
3709 reset_packet.public_header.reset_flag = true;
3710 reset_packet.public_header.version_flag = false;
3711 reset_packet.rejected_sequence_number = GG_UINT64_C(0x123456789ABC);
3712 reset_packet.nonce_proof = GG_UINT64_C(0xABCDEF0123456789);
3714 unsigned char packet[] = {
3715 // public flags (public reset, 8 byte ConnectionId)
3718 0x10, 0x32, 0x54, 0x76,
3719 0x98, 0xBA, 0xDC, 0xFE,
3720 // message tag (kPRST)
3722 // num_entries (2) + padding
3723 0x02, 0x00, 0x00, 0x00,
3727 0x08, 0x00, 0x00, 0x00,
3731 0x10, 0x00, 0x00, 0x00,
3733 0x89, 0x67, 0x45, 0x23,
3734 0x01, 0xEF, 0xCD, 0xAB,
3735 // rejected sequence number
3736 0xBC, 0x9A, 0x78, 0x56,
3737 0x34, 0x12, 0x00, 0x00,
3740 scoped_ptr<QuicEncryptedPacket> data(
3741 framer_.BuildPublicResetPacket(reset_packet));
3742 ASSERT_TRUE(data != NULL);
3744 test::CompareCharArraysWithHexError("constructed packet",
3745 data->data(), data->length(),
3746 AsChars(packet), arraysize(packet));
3749 TEST_P(QuicFramerTest, BuildPublicResetPacketWithClientAddress) {
3750 QuicPublicResetPacket reset_packet;
3751 reset_packet.public_header.connection_id = GG_UINT64_C(0xFEDCBA9876543210);
3752 reset_packet.public_header.reset_flag = true;
3753 reset_packet.public_header.version_flag = false;
3754 reset_packet.rejected_sequence_number = GG_UINT64_C(0x123456789ABC);
3755 reset_packet.nonce_proof = GG_UINT64_C(0xABCDEF0123456789);
3756 reset_packet.client_address = IPEndPoint(Loopback4(), 0x1234);
3758 unsigned char packet[] = {
3759 // public flags (public reset, 8 byte ConnectionId)
3762 0x10, 0x32, 0x54, 0x76,
3763 0x98, 0xBA, 0xDC, 0xFE,
3764 // message tag (kPRST)
3766 // num_entries (3) + padding
3767 0x03, 0x00, 0x00, 0x00,
3771 0x08, 0x00, 0x00, 0x00,
3775 0x10, 0x00, 0x00, 0x00,
3779 0x18, 0x00, 0x00, 0x00,
3781 0x89, 0x67, 0x45, 0x23,
3782 0x01, 0xEF, 0xCD, 0xAB,
3783 // rejected sequence number
3784 0xBC, 0x9A, 0x78, 0x56,
3785 0x34, 0x12, 0x00, 0x00,
3788 0x7F, 0x00, 0x00, 0x01,
3792 scoped_ptr<QuicEncryptedPacket> data(
3793 framer_.BuildPublicResetPacket(reset_packet));
3794 ASSERT_TRUE(data != NULL);
3796 test::CompareCharArraysWithHexError("constructed packet",
3797 data->data(), data->length(),
3798 AsChars(packet), arraysize(packet));
3801 TEST_P(QuicFramerTest, BuildFecPacket) {
3802 QuicPacketHeader header;
3803 header.public_header.connection_id = GG_UINT64_C(0xFEDCBA9876543210);
3804 header.public_header.reset_flag = false;
3805 header.public_header.version_flag = false;
3806 header.fec_flag = true;
3807 header.entropy_flag = true;
3808 header.packet_sequence_number = (GG_UINT64_C(0x123456789ABC));
3809 header.is_in_fec_group = IN_FEC_GROUP;
3810 header.fec_group = GG_UINT64_C(0x123456789ABB);;
3812 QuicFecData fec_data;
3813 fec_data.fec_group = 1;
3814 fec_data.redundancy = "abcdefghijklmnop";
3816 unsigned char packet[] = {
3817 // public flags (8 byte connection_id)
3820 0x10, 0x32, 0x54, 0x76,
3821 0x98, 0xBA, 0xDC, 0xFE,
3822 // packet sequence number
3823 0xBC, 0x9A, 0x78, 0x56,
3825 // private flags (entropy & fec group & fec packet)
3827 // first fec protected packet offset
3837 scoped_ptr<QuicPacket> data(
3838 framer_.BuildFecPacket(header, fec_data).packet);
3839 ASSERT_TRUE(data != NULL);
3841 test::CompareCharArraysWithHexError("constructed packet",
3842 data->data(), data->length(),
3843 AsChars(packet), arraysize(packet));
3846 TEST_P(QuicFramerTest, EncryptPacket) {
3847 QuicPacketSequenceNumber sequence_number = GG_UINT64_C(0x123456789ABC);
3848 unsigned char packet[] = {
3849 // public flags (8 byte connection_id)
3852 0x10, 0x32, 0x54, 0x76,
3853 0x98, 0xBA, 0xDC, 0xFE,
3854 // packet sequence number
3855 0xBC, 0x9A, 0x78, 0x56,
3857 // private flags (fec group & fec packet)
3859 // first fec protected packet offset
3869 scoped_ptr<QuicPacket> raw(
3870 QuicPacket::NewDataPacket(AsChars(packet), arraysize(packet), false,
3871 PACKET_8BYTE_CONNECTION_ID, !kIncludeVersion,
3872 PACKET_6BYTE_SEQUENCE_NUMBER));
3873 scoped_ptr<QuicEncryptedPacket> encrypted(
3874 framer_.EncryptPacket(ENCRYPTION_NONE, sequence_number, *raw));
3876 ASSERT_TRUE(encrypted.get() != NULL);
3877 EXPECT_TRUE(CheckEncryption(sequence_number, raw.get()));
3880 TEST_P(QuicFramerTest, EncryptPacketWithVersionFlag) {
3881 QuicPacketSequenceNumber sequence_number = GG_UINT64_C(0x123456789ABC);
3882 unsigned char packet[] = {
3883 // public flags (version, 8 byte connection_id)
3886 0x10, 0x32, 0x54, 0x76,
3887 0x98, 0xBA, 0xDC, 0xFE,
3890 // packet sequence number
3891 0xBC, 0x9A, 0x78, 0x56,
3893 // private flags (fec group & fec flags)
3895 // first fec protected packet offset
3905 scoped_ptr<QuicPacket> raw(
3906 QuicPacket::NewDataPacket(AsChars(packet), arraysize(packet), false,
3907 PACKET_8BYTE_CONNECTION_ID, kIncludeVersion,
3908 PACKET_6BYTE_SEQUENCE_NUMBER));
3909 scoped_ptr<QuicEncryptedPacket> encrypted(
3910 framer_.EncryptPacket(ENCRYPTION_NONE, sequence_number, *raw));
3912 ASSERT_TRUE(encrypted.get() != NULL);
3913 EXPECT_TRUE(CheckEncryption(sequence_number, raw.get()));
3916 TEST_P(QuicFramerTest, AckTruncationLargePacket) {
3917 QuicPacketHeader header;
3918 header.public_header.connection_id = GG_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_sequence_number = GG_UINT64_C(0x123456789ABC);
3924 header.fec_group = 0;
3926 // Create a packet with just the ack.
3927 QuicAckFrame ack_frame = MakeAckFrameWithNackRanges(300, 0u);
3929 frame.type = ACK_FRAME;
3930 frame.ack_frame = &ack_frame;
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(
3936 framer_.BuildDataPacket(header, frames, kMaxPacketSize).packet);
3937 ASSERT_TRUE(raw_ack_packet != NULL);
3938 scoped_ptr<QuicEncryptedPacket> ack_packet(
3939 framer_.EncryptPacket(ENCRYPTION_NONE, header.packet_sequence_number,
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 SequenceNumberSet::const_iterator missing_iter =
3949 processed_ack_frame.missing_packets.begin();
3950 EXPECT_EQ(1u, *missing_iter);
3951 SequenceNumberSet::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 = GG_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_sequence_number = GG_UINT64_C(0x123456789ABC);
3964 header.fec_group = 0;
3966 // Create a packet with just the ack.
3967 QuicAckFrame ack_frame = MakeAckFrameWithNackRanges(300, 0u);
3969 frame.type = ACK_FRAME;
3970 frame.ack_frame = &ack_frame;
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(
3976 framer_.BuildDataPacket(header, frames, 500).packet);
3977 ASSERT_TRUE(raw_ack_packet != NULL);
3978 scoped_ptr<QuicEncryptedPacket> ack_packet(
3979 framer_.EncryptPacket(ENCRYPTION_NONE, header.packet_sequence_number,
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 SequenceNumberSet::const_iterator missing_iter =
3989 processed_ack_frame.missing_packets.begin();
3990 EXPECT_EQ(1u, *missing_iter);
3991 SequenceNumberSet::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 = GG_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_sequence_number = GG_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.
4014 frame.type = ACK_FRAME;
4015 frame.ack_frame = &ack_frame;
4017 frames.push_back(frame);
4019 scoped_ptr<QuicPacket> raw_ack_packet(BuildDataPacket(header, frames));
4020 ASSERT_TRUE(raw_ack_packet != NULL);
4022 scoped_ptr<QuicEncryptedPacket> ack_packet(
4023 framer_.EncryptPacket(ENCRYPTION_NONE, header.packet_sequence_number,
4026 // Now make sure we can turn our ack packet back into an ack frame.
4027 ASSERT_TRUE(framer_.ProcessPacket(*ack_packet));
4029 // Test for clean truncation of the ack by comparing the length of the
4030 // original packets to the re-serialized packets.
4032 frame.type = ACK_FRAME;
4033 frame.ack_frame = visitor_.ack_frames_[0];
4034 frames.push_back(frame);
4036 size_t original_raw_length = raw_ack_packet->length();
4037 raw_ack_packet.reset(BuildDataPacket(header, frames));
4038 ASSERT_TRUE(raw_ack_packet != NULL);
4039 EXPECT_EQ(original_raw_length, raw_ack_packet->length());
4040 ASSERT_TRUE(raw_ack_packet != NULL);
4043 TEST_P(QuicFramerTest, EntropyFlagTest) {
4044 unsigned char packet[] = {
4045 // public flags (8 byte connection_id)
4048 0x10, 0x32, 0x54, 0x76,
4049 0x98, 0xBA, 0xDC, 0xFE,
4050 // packet sequence number
4051 0xBC, 0x9A, 0x78, 0x56,
4053 // private flags (Entropy)
4056 // frame type (stream frame with fin and no length)
4059 0x04, 0x03, 0x02, 0x01,
4061 0x54, 0x76, 0x10, 0x32,
4062 0xDC, 0xFE, 0x98, 0xBA,
4069 QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
4070 EXPECT_TRUE(framer_.ProcessPacket(encrypted));
4071 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
4072 ASSERT_TRUE(visitor_.header_.get());
4073 EXPECT_TRUE(visitor_.header_->entropy_flag);
4074 EXPECT_EQ(1 << 4, visitor_.header_->entropy_hash);
4075 EXPECT_FALSE(visitor_.header_->fec_flag);
4078 TEST_P(QuicFramerTest, FecEntropyTest) {
4079 unsigned char packet[] = {
4080 // public flags (8 byte connection_id)
4083 0x10, 0x32, 0x54, 0x76,
4084 0x98, 0xBA, 0xDC, 0xFE,
4085 // packet sequence number
4086 0xBC, 0x9A, 0x78, 0x56,
4088 // private flags (Entropy & fec group & FEC)
4090 // first fec protected packet offset
4093 // frame type (stream frame with fin and no length)
4096 0x04, 0x03, 0x02, 0x01,
4098 0x54, 0x76, 0x10, 0x32,
4099 0xDC, 0xFE, 0x98, 0xBA,
4106 QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
4107 EXPECT_TRUE(framer_.ProcessPacket(encrypted));
4108 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
4109 ASSERT_TRUE(visitor_.header_.get());
4110 EXPECT_TRUE(visitor_.header_->fec_flag);
4111 EXPECT_TRUE(visitor_.header_->entropy_flag);
4112 EXPECT_EQ(1 << 4, visitor_.header_->entropy_hash);
4115 TEST_P(QuicFramerTest, StopPacketProcessing) {
4116 unsigned char packet[] = {
4117 // public flags (8 byte connection_id)
4120 0x10, 0x32, 0x54, 0x76,
4121 0x98, 0xBA, 0xDC, 0xFE,
4122 // packet sequence number
4123 0xBC, 0x9A, 0x78, 0x56,
4128 // frame type (stream frame with fin)
4131 0x04, 0x03, 0x02, 0x01,
4133 0x54, 0x76, 0x10, 0x32,
4134 0xDC, 0xFE, 0x98, 0xBA,
4142 // frame type (ack frame)
4144 // entropy hash of sent packets till least awaiting - 1.
4146 // least packet sequence number awaiting an ack
4147 0xA0, 0x9A, 0x78, 0x56,
4149 // entropy hash of all received packets.
4151 // largest observed packet sequence number
4152 0xBF, 0x9A, 0x78, 0x56,
4154 // num missing packets
4157 0xBE, 0x9A, 0x78, 0x56,
4161 MockFramerVisitor visitor;
4162 framer_.set_visitor(&visitor);
4163 EXPECT_CALL(visitor, OnPacket());
4164 EXPECT_CALL(visitor, OnPacketHeader(_));
4165 EXPECT_CALL(visitor, OnStreamFrame(_)).WillOnce(Return(false));
4166 EXPECT_CALL(visitor, OnAckFrame(_)).Times(0);
4167 EXPECT_CALL(visitor, OnPacketComplete());
4168 EXPECT_CALL(visitor, OnUnauthenticatedPublicHeader(_)).WillOnce(Return(true));
4170 QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
4171 EXPECT_TRUE(framer_.ProcessPacket(encrypted));
4172 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());