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"
25 using base::StringPiece
;
28 using std::numeric_limits
;
32 using testing::Return
;
38 const QuicPacketSequenceNumber kEpoch
= GG_UINT64_C(1) << 48;
39 const QuicPacketSequenceNumber kMask
= kEpoch
- 1;
41 // Index into the guid offset in the header.
42 const size_t kGuidOffset
= kPublicFlagsSize
;
43 // Index into the version string in the header. (if present).
44 const size_t kVersionOffset
= kGuidOffset
+ PACKET_8BYTE_GUID
;
46 // Size in bytes of the stream frame fields for an arbitrary StreamID and
47 // offset and the last frame in a packet.
48 size_t GetMinStreamFrameSize(QuicVersion version
) {
49 return kQuicFrameTypeSize
+ kQuicMaxStreamIdSize
+ kQuicMaxStreamOffsetSize
;
52 // Index into the sequence number offset in the header.
53 size_t GetSequenceNumberOffset(QuicGuidLength guid_length
,
54 bool include_version
) {
55 return kGuidOffset
+ guid_length
+
56 (include_version
? kQuicVersionSize
: 0);
59 size_t GetSequenceNumberOffset(bool include_version
) {
60 return GetSequenceNumberOffset(PACKET_8BYTE_GUID
, include_version
);
63 // Index into the private flags offset in the data packet header.
64 size_t GetPrivateFlagsOffset(QuicGuidLength guid_length
, bool include_version
) {
65 return GetSequenceNumberOffset(guid_length
, include_version
) +
66 PACKET_6BYTE_SEQUENCE_NUMBER
;
69 size_t GetPrivateFlagsOffset(bool include_version
) {
70 return GetPrivateFlagsOffset(PACKET_8BYTE_GUID
, include_version
);
73 size_t GetPrivateFlagsOffset(bool include_version
,
74 QuicSequenceNumberLength sequence_number_length
) {
75 return GetSequenceNumberOffset(PACKET_8BYTE_GUID
, include_version
) +
76 sequence_number_length
;
79 // Index into the fec group offset in the header.
80 size_t GetFecGroupOffset(QuicGuidLength guid_length
, bool include_version
) {
81 return GetPrivateFlagsOffset(guid_length
, include_version
) +
85 size_t GetFecGroupOffset(bool include_version
) {
86 return GetPrivateFlagsOffset(PACKET_8BYTE_GUID
, include_version
) +
90 size_t GetFecGroupOffset(bool include_version
,
91 QuicSequenceNumberLength sequence_number_length
) {
92 return GetPrivateFlagsOffset(include_version
, sequence_number_length
) +
96 // Index into the nonce proof of the public reset packet.
97 // Public resets always have full guids.
98 const size_t kPublicResetPacketNonceProofOffset
=
99 kGuidOffset
+ PACKET_8BYTE_GUID
;
101 // Index into the rejected sequence number of the public reset packet.
102 const size_t kPublicResetPacketRejectedSequenceNumberOffset
=
103 kPublicResetPacketNonceProofOffset
+ kPublicResetNonceSize
;
105 class TestEncrypter
: public QuicEncrypter
{
107 virtual ~TestEncrypter() {}
108 virtual bool SetKey(StringPiece key
) OVERRIDE
{
111 virtual bool SetNoncePrefix(StringPiece nonce_prefix
) OVERRIDE
{
114 virtual bool Encrypt(StringPiece nonce
,
115 StringPiece associated_data
,
116 StringPiece plaintext
,
117 unsigned char* output
) OVERRIDE
{
118 CHECK(false) << "Not implemented";
121 virtual QuicData
* EncryptPacket(QuicPacketSequenceNumber sequence_number
,
122 StringPiece associated_data
,
123 StringPiece plaintext
) OVERRIDE
{
124 sequence_number_
= sequence_number
;
125 associated_data_
= associated_data
.as_string();
126 plaintext_
= plaintext
.as_string();
127 return new QuicData(plaintext
.data(), plaintext
.length());
129 virtual size_t GetKeySize() const OVERRIDE
{
132 virtual size_t GetNoncePrefixSize() const OVERRIDE
{
135 virtual size_t GetMaxPlaintextSize(size_t ciphertext_size
) const OVERRIDE
{
136 return ciphertext_size
;
138 virtual size_t GetCiphertextSize(size_t plaintext_size
) const OVERRIDE
{
139 return plaintext_size
;
141 virtual StringPiece
GetKey() const OVERRIDE
{
142 return StringPiece();
144 virtual StringPiece
GetNoncePrefix() const OVERRIDE
{
145 return StringPiece();
147 QuicPacketSequenceNumber sequence_number_
;
148 string associated_data_
;
152 class TestDecrypter
: public QuicDecrypter
{
154 virtual ~TestDecrypter() {}
155 virtual bool SetKey(StringPiece key
) OVERRIDE
{
158 virtual bool SetNoncePrefix(StringPiece nonce_prefix
) OVERRIDE
{
161 virtual bool Decrypt(StringPiece nonce
,
162 StringPiece associated_data
,
163 StringPiece ciphertext
,
164 unsigned char* output
,
165 size_t* output_length
) OVERRIDE
{
166 CHECK(false) << "Not implemented";
169 virtual QuicData
* DecryptPacket(QuicPacketSequenceNumber sequence_number
,
170 StringPiece associated_data
,
171 StringPiece ciphertext
) OVERRIDE
{
172 sequence_number_
= sequence_number
;
173 associated_data_
= associated_data
.as_string();
174 ciphertext_
= ciphertext
.as_string();
175 return new QuicData(ciphertext
.data(), ciphertext
.length());
177 virtual StringPiece
GetKey() const OVERRIDE
{
178 return StringPiece();
180 virtual StringPiece
GetNoncePrefix() const OVERRIDE
{
181 return StringPiece();
183 QuicPacketSequenceNumber sequence_number_
;
184 string associated_data_
;
188 class TestQuicVisitor
: public ::net::QuicFramerVisitorInterface
{
192 version_mismatch_(0),
196 complete_packets_(0),
198 accept_packet_(true),
199 accept_public_header_(true) {
202 virtual ~TestQuicVisitor() {
203 STLDeleteElements(&stream_frames_
);
204 STLDeleteElements(&ack_frames_
);
205 STLDeleteElements(&congestion_feedback_frames_
);
206 STLDeleteElements(&fec_data_
);
209 virtual void OnError(QuicFramer
* f
) OVERRIDE
{
210 DVLOG(1) << "QuicFramer Error: " << QuicUtils::ErrorToString(f
->error())
211 << " (" << f
->error() << ")";
215 virtual void OnPacket() OVERRIDE
{}
217 virtual void OnPublicResetPacket(
218 const QuicPublicResetPacket
& packet
) OVERRIDE
{
219 public_reset_packet_
.reset(new QuicPublicResetPacket(packet
));
222 virtual void OnVersionNegotiationPacket(
223 const QuicVersionNegotiationPacket
& packet
) OVERRIDE
{
224 version_negotiation_packet_
.reset(new QuicVersionNegotiationPacket(packet
));
227 virtual void OnRevivedPacket() OVERRIDE
{
231 virtual bool OnProtocolVersionMismatch(QuicVersion version
) OVERRIDE
{
232 DVLOG(1) << "QuicFramer Version Mismatch, version: " << version
;
237 virtual bool OnUnauthenticatedPublicHeader(
238 const QuicPacketPublicHeader
& header
) OVERRIDE
{
239 public_header_
.reset(new QuicPacketPublicHeader(header
));
240 return accept_public_header_
;
243 virtual bool OnUnauthenticatedHeader(
244 const QuicPacketHeader
& header
) OVERRIDE
{
248 virtual bool OnPacketHeader(const QuicPacketHeader
& header
) OVERRIDE
{
250 header_
.reset(new QuicPacketHeader(header
));
251 return accept_packet_
;
254 virtual bool OnStreamFrame(const QuicStreamFrame
& frame
) OVERRIDE
{
256 stream_frames_
.push_back(new QuicStreamFrame(frame
));
260 virtual void OnFecProtectedPayload(StringPiece payload
) OVERRIDE
{
261 fec_protected_payload_
= payload
.as_string();
264 virtual bool OnAckFrame(const QuicAckFrame
& frame
) OVERRIDE
{
266 ack_frames_
.push_back(new QuicAckFrame(frame
));
270 virtual bool OnCongestionFeedbackFrame(
271 const QuicCongestionFeedbackFrame
& frame
) OVERRIDE
{
273 congestion_feedback_frames_
.push_back(
274 new QuicCongestionFeedbackFrame(frame
));
278 virtual void OnFecData(const QuicFecData
& fec
) OVERRIDE
{
280 fec_data_
.push_back(new QuicFecData(fec
));
283 virtual void OnPacketComplete() OVERRIDE
{
287 virtual bool OnRstStreamFrame(const QuicRstStreamFrame
& frame
) OVERRIDE
{
288 rst_stream_frame_
= frame
;
292 virtual bool OnConnectionCloseFrame(
293 const QuicConnectionCloseFrame
& frame
) OVERRIDE
{
294 connection_close_frame_
= frame
;
298 virtual bool OnGoAwayFrame(const QuicGoAwayFrame
& frame
) OVERRIDE
{
299 goaway_frame_
= frame
;
303 // Counters from the visitor_ callbacks.
305 int version_mismatch_
;
309 int complete_packets_
;
310 int revived_packets_
;
312 bool accept_public_header_
;
314 scoped_ptr
<QuicPacketHeader
> header_
;
315 scoped_ptr
<QuicPacketPublicHeader
> public_header_
;
316 scoped_ptr
<QuicPublicResetPacket
> public_reset_packet_
;
317 scoped_ptr
<QuicVersionNegotiationPacket
> version_negotiation_packet_
;
318 vector
<QuicStreamFrame
*> stream_frames_
;
319 vector
<QuicAckFrame
*> ack_frames_
;
320 vector
<QuicCongestionFeedbackFrame
*> congestion_feedback_frames_
;
321 vector
<QuicFecData
*> fec_data_
;
322 string fec_protected_payload_
;
323 QuicRstStreamFrame rst_stream_frame_
;
324 QuicConnectionCloseFrame connection_close_frame_
;
325 QuicGoAwayFrame goaway_frame_
;
328 class QuicFramerTest
: public ::testing::TestWithParam
<QuicVersion
> {
331 : encrypter_(new test::TestEncrypter()),
332 decrypter_(new test::TestDecrypter()),
333 start_(QuicTime::Zero().Add(QuicTime::Delta::FromMicroseconds(0x10))),
334 framer_(QuicSupportedVersions(), start_
, true) {
335 version_
= GetParam();
336 framer_
.set_version(version_
);
337 framer_
.SetDecrypter(decrypter_
);
338 framer_
.SetEncrypter(ENCRYPTION_NONE
, encrypter_
);
339 framer_
.set_visitor(&visitor_
);
340 framer_
.set_received_entropy_calculator(&entropy_calculator_
);
343 // Helper function to get unsigned char representation of digit in the
344 // units place of the current QUIC version number.
345 unsigned char GetQuicVersionDigitOnes() {
346 return static_cast<unsigned char> ('0' + version_
%10);
349 // Helper function to get unsigned char representation of digit in the
350 // tens place of the current QUIC version number.
351 unsigned char GetQuicVersionDigitTens() {
352 return static_cast<unsigned char> ('0' + (version_
/10)%10);
355 bool CheckEncryption(QuicPacketSequenceNumber sequence_number
,
356 QuicPacket
* packet
) {
357 if (sequence_number
!= encrypter_
->sequence_number_
) {
358 LOG(ERROR
) << "Encrypted incorrect packet sequence number. expected "
359 << sequence_number
<< " actual: "
360 << encrypter_
->sequence_number_
;
363 if (packet
->AssociatedData() != encrypter_
->associated_data_
) {
364 LOG(ERROR
) << "Encrypted incorrect associated data. expected "
365 << packet
->AssociatedData() << " actual: "
366 << encrypter_
->associated_data_
;
369 if (packet
->Plaintext() != encrypter_
->plaintext_
) {
370 LOG(ERROR
) << "Encrypted incorrect plaintext data. expected "
371 << packet
->Plaintext() << " actual: "
372 << encrypter_
->plaintext_
;
378 bool CheckDecryption(const QuicEncryptedPacket
& encrypted
,
379 bool includes_version
) {
380 if (visitor_
.header_
->packet_sequence_number
!=
381 decrypter_
->sequence_number_
) {
382 LOG(ERROR
) << "Decrypted incorrect packet sequence number. expected "
383 << visitor_
.header_
->packet_sequence_number
<< " actual: "
384 << decrypter_
->sequence_number_
;
387 if (QuicFramer::GetAssociatedDataFromEncryptedPacket(
388 encrypted
, PACKET_8BYTE_GUID
,
389 includes_version
, PACKET_6BYTE_SEQUENCE_NUMBER
) !=
390 decrypter_
->associated_data_
) {
391 LOG(ERROR
) << "Decrypted incorrect associated data. expected "
392 << QuicFramer::GetAssociatedDataFromEncryptedPacket(
393 encrypted
, PACKET_8BYTE_GUID
,
394 includes_version
, PACKET_6BYTE_SEQUENCE_NUMBER
)
395 << " actual: " << decrypter_
->associated_data_
;
398 StringPiece
ciphertext(encrypted
.AsStringPiece().substr(
399 GetStartOfEncryptedData(PACKET_8BYTE_GUID
, includes_version
,
400 PACKET_6BYTE_SEQUENCE_NUMBER
)));
401 if (ciphertext
!= decrypter_
->ciphertext_
) {
402 LOG(ERROR
) << "Decrypted incorrect ciphertext data. expected "
403 << ciphertext
<< " actual: "
404 << decrypter_
->ciphertext_
;
410 char* AsChars(unsigned char* data
) {
411 return reinterpret_cast<char*>(data
);
414 void CheckProcessingFails(unsigned char* packet
,
416 string expected_error
,
417 QuicErrorCode error_code
) {
418 QuicEncryptedPacket
encrypted(AsChars(packet
), len
, false);
419 EXPECT_FALSE(framer_
.ProcessPacket(encrypted
)) << "len: " << len
;
420 EXPECT_EQ(expected_error
, framer_
.detailed_error()) << "len: " << len
;
421 EXPECT_EQ(error_code
, framer_
.error()) << "len: " << len
;
424 // Checks if the supplied string matches data in the supplied StreamFrame.
425 void CheckStreamFrameData(string str
, QuicStreamFrame
* frame
) {
426 scoped_ptr
<string
> frame_data(frame
->GetDataAsString());
427 EXPECT_EQ(str
, *frame_data
);
430 void CheckStreamFrameBoundaries(unsigned char* packet
,
431 size_t stream_id_size
,
432 bool include_version
) {
433 // Now test framing boundaries
434 for (size_t i
= kQuicFrameTypeSize
;
435 i
< GetMinStreamFrameSize(framer_
.version()); ++i
) {
436 string expected_error
;
437 if (i
< kQuicFrameTypeSize
+ stream_id_size
) {
438 expected_error
= "Unable to read stream_id.";
439 } else if (i
< kQuicFrameTypeSize
+ stream_id_size
+
440 kQuicMaxStreamOffsetSize
) {
441 expected_error
= "Unable to read offset.";
443 expected_error
= "Unable to read frame data.";
445 CheckProcessingFails(
447 i
+ GetPacketHeaderSize(PACKET_8BYTE_GUID
, include_version
,
448 PACKET_6BYTE_SEQUENCE_NUMBER
,
450 expected_error
, QUIC_INVALID_STREAM_DATA
);
454 void CheckCalculatePacketSequenceNumber(
455 QuicPacketSequenceNumber expected_sequence_number
,
456 QuicPacketSequenceNumber last_sequence_number
) {
457 QuicPacketSequenceNumber wire_sequence_number
=
458 expected_sequence_number
& kMask
;
459 QuicFramerPeer::SetLastSequenceNumber(&framer_
, last_sequence_number
);
460 EXPECT_EQ(expected_sequence_number
,
461 QuicFramerPeer::CalculatePacketSequenceNumberFromWire(
462 &framer_
, PACKET_6BYTE_SEQUENCE_NUMBER
, wire_sequence_number
))
463 << "last_sequence_number: " << last_sequence_number
464 << " wire_sequence_number: " << wire_sequence_number
;
467 test::TestEncrypter
* encrypter_
;
468 test::TestDecrypter
* decrypter_
;
469 QuicVersion version_
;
472 test::TestQuicVisitor visitor_
;
473 test::TestEntropyCalculator entropy_calculator_
;
476 // Run all framer tests with all supported versions of QUIC.
477 INSTANTIATE_TEST_CASE_P(QuicFramerTests
,
479 ::testing::ValuesIn(kSupportedQuicVersions
));
481 TEST_P(QuicFramerTest
, CalculatePacketSequenceNumberFromWireNearEpochStart
) {
482 // A few quick manual sanity checks
483 CheckCalculatePacketSequenceNumber(GG_UINT64_C(1), GG_UINT64_C(0));
484 CheckCalculatePacketSequenceNumber(kEpoch
+ 1, kMask
);
485 CheckCalculatePacketSequenceNumber(kEpoch
, kMask
);
487 // Cases where the last number was close to the start of the range
488 for (uint64 last
= 0; last
< 10; last
++) {
489 // Small numbers should not wrap (even if they're out of order).
490 for (uint64 j
= 0; j
< 10; j
++) {
491 CheckCalculatePacketSequenceNumber(j
, last
);
494 // Large numbers should not wrap either (because we're near 0 already).
495 for (uint64 j
= 0; j
< 10; j
++) {
496 CheckCalculatePacketSequenceNumber(kEpoch
- 1 - j
, last
);
501 TEST_P(QuicFramerTest
, CalculatePacketSequenceNumberFromWireNearEpochEnd
) {
502 // Cases where the last number was close to the end of the range
503 for (uint64 i
= 0; i
< 10; i
++) {
504 QuicPacketSequenceNumber last
= kEpoch
- i
;
506 // Small numbers should wrap.
507 for (uint64 j
= 0; j
< 10; j
++) {
508 CheckCalculatePacketSequenceNumber(kEpoch
+ j
, last
);
511 // Large numbers should not (even if they're out of order).
512 for (uint64 j
= 0; j
< 10; j
++) {
513 CheckCalculatePacketSequenceNumber(kEpoch
- 1 - j
, last
);
518 // Next check where we're in a non-zero epoch to verify we handle
519 // reverse wrapping, too.
520 TEST_P(QuicFramerTest
, CalculatePacketSequenceNumberFromWireNearPrevEpoch
) {
521 const uint64 prev_epoch
= 1 * kEpoch
;
522 const uint64 cur_epoch
= 2 * kEpoch
;
523 // Cases where the last number was close to the start of the range
524 for (uint64 i
= 0; i
< 10; i
++) {
525 uint64 last
= cur_epoch
+ i
;
526 // Small number should not wrap (even if they're out of order).
527 for (uint64 j
= 0; j
< 10; j
++) {
528 CheckCalculatePacketSequenceNumber(cur_epoch
+ j
, last
);
531 // But large numbers should reverse wrap.
532 for (uint64 j
= 0; j
< 10; j
++) {
533 uint64 num
= kEpoch
- 1 - j
;
534 CheckCalculatePacketSequenceNumber(prev_epoch
+ num
, last
);
539 TEST_P(QuicFramerTest
, CalculatePacketSequenceNumberFromWireNearNextEpoch
) {
540 const uint64 cur_epoch
= 2 * kEpoch
;
541 const uint64 next_epoch
= 3 * kEpoch
;
542 // Cases where the last number was close to the end of the range
543 for (uint64 i
= 0; i
< 10; i
++) {
544 QuicPacketSequenceNumber last
= next_epoch
- 1 - i
;
546 // Small numbers should wrap.
547 for (uint64 j
= 0; j
< 10; j
++) {
548 CheckCalculatePacketSequenceNumber(next_epoch
+ j
, last
);
551 // but large numbers should not (even if they're out of order).
552 for (uint64 j
= 0; j
< 10; j
++) {
553 uint64 num
= kEpoch
- 1 - j
;
554 CheckCalculatePacketSequenceNumber(cur_epoch
+ num
, last
);
559 TEST_P(QuicFramerTest
, CalculatePacketSequenceNumberFromWireNearNextMax
) {
560 const uint64 max_number
= numeric_limits
<uint64
>::max();
561 const uint64 max_epoch
= max_number
& ~kMask
;
563 // Cases where the last number was close to the end of the range
564 for (uint64 i
= 0; i
< 10; i
++) {
565 // Subtract 1, because the expected next sequence number is 1 more than the
566 // last sequence number.
567 QuicPacketSequenceNumber last
= max_number
- i
- 1;
569 // Small numbers should not wrap, because they have nowhere to go.
570 for (uint64 j
= 0; j
< 10; j
++) {
571 CheckCalculatePacketSequenceNumber(max_epoch
+ j
, last
);
574 // Large numbers should not wrap either.
575 for (uint64 j
= 0; j
< 10; j
++) {
576 uint64 num
= kEpoch
- 1 - j
;
577 CheckCalculatePacketSequenceNumber(max_epoch
+ num
, last
);
582 TEST_P(QuicFramerTest
, EmptyPacket
) {
583 char packet
[] = { 0x00 };
584 QuicEncryptedPacket
encrypted(packet
, 0, false);
585 EXPECT_FALSE(framer_
.ProcessPacket(encrypted
));
586 EXPECT_EQ(QUIC_INVALID_PACKET_HEADER
, framer_
.error());
589 TEST_P(QuicFramerTest
, LargePacket
) {
590 unsigned char packet
[kMaxPacketSize
+ 1] = {
591 // public flags (8 byte guid)
594 0x10, 0x32, 0x54, 0x76,
595 0x98, 0xBA, 0xDC, 0xFE,
596 // packet sequence number
597 0xBC, 0x9A, 0x78, 0x56,
603 memset(packet
+ GetPacketHeaderSize(
604 PACKET_8BYTE_GUID
, !kIncludeVersion
,
605 PACKET_6BYTE_SEQUENCE_NUMBER
, NOT_IN_FEC_GROUP
), 0,
606 kMaxPacketSize
- GetPacketHeaderSize(
607 PACKET_8BYTE_GUID
, !kIncludeVersion
,
608 PACKET_6BYTE_SEQUENCE_NUMBER
, NOT_IN_FEC_GROUP
) + 1);
610 QuicEncryptedPacket
encrypted(AsChars(packet
), arraysize(packet
), false);
611 EXPECT_FALSE(framer_
.ProcessPacket(encrypted
));
613 ASSERT_TRUE(visitor_
.header_
.get());
614 // Make sure we've parsed the packet header, so we can send an error.
615 EXPECT_EQ(GG_UINT64_C(0xFEDCBA9876543210),
616 visitor_
.header_
->public_header
.guid
);
617 // Make sure the correct error is propagated.
618 EXPECT_EQ(QUIC_PACKET_TOO_LARGE
, framer_
.error());
621 TEST_P(QuicFramerTest
, PacketHeader
) {
622 unsigned char packet
[] = {
623 // public flags (8 byte guid)
626 0x10, 0x32, 0x54, 0x76,
627 0x98, 0xBA, 0xDC, 0xFE,
628 // packet sequence number
629 0xBC, 0x9A, 0x78, 0x56,
635 QuicEncryptedPacket
encrypted(AsChars(packet
), arraysize(packet
), false);
636 EXPECT_FALSE(framer_
.ProcessPacket(encrypted
));
637 EXPECT_EQ(QUIC_MISSING_PAYLOAD
, framer_
.error());
638 ASSERT_TRUE(visitor_
.header_
.get());
639 EXPECT_EQ(GG_UINT64_C(0xFEDCBA9876543210),
640 visitor_
.header_
->public_header
.guid
);
641 EXPECT_FALSE(visitor_
.header_
->public_header
.reset_flag
);
642 EXPECT_FALSE(visitor_
.header_
->public_header
.version_flag
);
643 EXPECT_FALSE(visitor_
.header_
->fec_flag
);
644 EXPECT_FALSE(visitor_
.header_
->entropy_flag
);
645 EXPECT_EQ(0, visitor_
.header_
->entropy_hash
);
646 EXPECT_EQ(GG_UINT64_C(0x123456789ABC),
647 visitor_
.header_
->packet_sequence_number
);
648 EXPECT_EQ(NOT_IN_FEC_GROUP
, visitor_
.header_
->is_in_fec_group
);
649 EXPECT_EQ(0x00u
, visitor_
.header_
->fec_group
);
651 // Now test framing boundaries
653 i
< GetPacketHeaderSize(PACKET_8BYTE_GUID
, !kIncludeVersion
,
654 PACKET_6BYTE_SEQUENCE_NUMBER
, NOT_IN_FEC_GROUP
);
656 string expected_error
;
657 if (i
< kGuidOffset
) {
658 expected_error
= "Unable to read public flags.";
659 } else if (i
< GetSequenceNumberOffset(!kIncludeVersion
)) {
660 expected_error
= "Unable to read GUID.";
661 } else if (i
< GetPrivateFlagsOffset(!kIncludeVersion
)) {
662 expected_error
= "Unable to read sequence number.";
663 } else if (i
< GetFecGroupOffset(!kIncludeVersion
)) {
664 expected_error
= "Unable to read private flags.";
666 expected_error
= "Unable to read first fec protected packet offset.";
668 CheckProcessingFails(packet
, i
, expected_error
, QUIC_INVALID_PACKET_HEADER
);
672 TEST_P(QuicFramerTest
, PacketHeaderWith4ByteGuid
) {
673 QuicFramerPeer::SetLastSerializedGuid(&framer_
,
674 GG_UINT64_C(0xFEDCBA9876543210));
676 unsigned char packet
[] = {
677 // public flags (4 byte guid)
680 0x10, 0x32, 0x54, 0x76,
681 // packet sequence number
682 0xBC, 0x9A, 0x78, 0x56,
688 QuicEncryptedPacket
encrypted(AsChars(packet
), arraysize(packet
), false);
689 EXPECT_FALSE(framer_
.ProcessPacket(encrypted
));
690 EXPECT_EQ(QUIC_MISSING_PAYLOAD
, framer_
.error());
691 ASSERT_TRUE(visitor_
.header_
.get());
692 EXPECT_EQ(GG_UINT64_C(0xFEDCBA9876543210),
693 visitor_
.header_
->public_header
.guid
);
694 EXPECT_FALSE(visitor_
.header_
->public_header
.reset_flag
);
695 EXPECT_FALSE(visitor_
.header_
->public_header
.version_flag
);
696 EXPECT_FALSE(visitor_
.header_
->fec_flag
);
697 EXPECT_FALSE(visitor_
.header_
->entropy_flag
);
698 EXPECT_EQ(0, visitor_
.header_
->entropy_hash
);
699 EXPECT_EQ(GG_UINT64_C(0x123456789ABC),
700 visitor_
.header_
->packet_sequence_number
);
701 EXPECT_EQ(NOT_IN_FEC_GROUP
, visitor_
.header_
->is_in_fec_group
);
702 EXPECT_EQ(0x00u
, visitor_
.header_
->fec_group
);
704 // Now test framing boundaries
706 i
< GetPacketHeaderSize(PACKET_4BYTE_GUID
, !kIncludeVersion
,
707 PACKET_6BYTE_SEQUENCE_NUMBER
, NOT_IN_FEC_GROUP
);
709 string expected_error
;
710 if (i
< kGuidOffset
) {
711 expected_error
= "Unable to read public flags.";
712 } else if (i
< GetSequenceNumberOffset(PACKET_4BYTE_GUID
,
714 expected_error
= "Unable to read GUID.";
715 } else if (i
< GetPrivateFlagsOffset(PACKET_4BYTE_GUID
,
717 expected_error
= "Unable to read sequence number.";
718 } else if (i
< GetFecGroupOffset(PACKET_4BYTE_GUID
, !kIncludeVersion
)) {
719 expected_error
= "Unable to read private flags.";
721 expected_error
= "Unable to read first fec protected packet offset.";
723 CheckProcessingFails(packet
, i
, expected_error
, QUIC_INVALID_PACKET_HEADER
);
727 TEST_P(QuicFramerTest
, PacketHeader1ByteGuid
) {
728 QuicFramerPeer::SetLastSerializedGuid(&framer_
,
729 GG_UINT64_C(0xFEDCBA9876543210));
731 unsigned char packet
[] = {
732 // public flags (1 byte guid)
736 // packet sequence number
737 0xBC, 0x9A, 0x78, 0x56,
743 QuicEncryptedPacket
encrypted(AsChars(packet
), arraysize(packet
), false);
744 EXPECT_FALSE(framer_
.ProcessPacket(encrypted
));
745 EXPECT_EQ(QUIC_MISSING_PAYLOAD
, framer_
.error());
746 ASSERT_TRUE(visitor_
.header_
.get());
747 EXPECT_EQ(GG_UINT64_C(0xFEDCBA9876543210),
748 visitor_
.header_
->public_header
.guid
);
749 EXPECT_FALSE(visitor_
.header_
->public_header
.reset_flag
);
750 EXPECT_FALSE(visitor_
.header_
->public_header
.version_flag
);
751 EXPECT_FALSE(visitor_
.header_
->fec_flag
);
752 EXPECT_FALSE(visitor_
.header_
->entropy_flag
);
753 EXPECT_EQ(0, visitor_
.header_
->entropy_hash
);
754 EXPECT_EQ(GG_UINT64_C(0x123456789ABC),
755 visitor_
.header_
->packet_sequence_number
);
756 EXPECT_EQ(NOT_IN_FEC_GROUP
, visitor_
.header_
->is_in_fec_group
);
757 EXPECT_EQ(0x00u
, visitor_
.header_
->fec_group
);
759 // Now test framing boundaries
761 i
< GetPacketHeaderSize(PACKET_1BYTE_GUID
, !kIncludeVersion
,
762 PACKET_6BYTE_SEQUENCE_NUMBER
, NOT_IN_FEC_GROUP
);
764 string expected_error
;
765 if (i
< kGuidOffset
) {
766 expected_error
= "Unable to read public flags.";
767 } else if (i
< GetSequenceNumberOffset(PACKET_1BYTE_GUID
,
769 expected_error
= "Unable to read GUID.";
770 } else if (i
< GetPrivateFlagsOffset(PACKET_1BYTE_GUID
, !kIncludeVersion
)) {
771 expected_error
= "Unable to read sequence number.";
772 } else if (i
< GetFecGroupOffset(PACKET_1BYTE_GUID
, !kIncludeVersion
)) {
773 expected_error
= "Unable to read private flags.";
775 expected_error
= "Unable to read first fec protected packet offset.";
777 CheckProcessingFails(packet
, i
, expected_error
, QUIC_INVALID_PACKET_HEADER
);
781 TEST_P(QuicFramerTest
, PacketHeaderWith0ByteGuid
) {
782 QuicFramerPeer::SetLastSerializedGuid(&framer_
,
783 GG_UINT64_C(0xFEDCBA9876543210));
785 unsigned char packet
[] = {
786 // public flags (0 byte guid)
789 // packet sequence number
790 0xBC, 0x9A, 0x78, 0x56,
796 QuicEncryptedPacket
encrypted(AsChars(packet
), arraysize(packet
), false);
797 EXPECT_FALSE(framer_
.ProcessPacket(encrypted
));
798 EXPECT_EQ(QUIC_MISSING_PAYLOAD
, framer_
.error());
799 ASSERT_TRUE(visitor_
.header_
.get());
800 EXPECT_EQ(GG_UINT64_C(0xFEDCBA9876543210),
801 visitor_
.header_
->public_header
.guid
);
802 EXPECT_FALSE(visitor_
.header_
->public_header
.reset_flag
);
803 EXPECT_FALSE(visitor_
.header_
->public_header
.version_flag
);
804 EXPECT_FALSE(visitor_
.header_
->fec_flag
);
805 EXPECT_FALSE(visitor_
.header_
->entropy_flag
);
806 EXPECT_EQ(0, visitor_
.header_
->entropy_hash
);
807 EXPECT_EQ(GG_UINT64_C(0x123456789ABC),
808 visitor_
.header_
->packet_sequence_number
);
809 EXPECT_EQ(NOT_IN_FEC_GROUP
, visitor_
.header_
->is_in_fec_group
);
810 EXPECT_EQ(0x00u
, visitor_
.header_
->fec_group
);
812 // Now test framing boundaries
814 i
< GetPacketHeaderSize(PACKET_0BYTE_GUID
, !kIncludeVersion
,
815 PACKET_6BYTE_SEQUENCE_NUMBER
, NOT_IN_FEC_GROUP
);
817 string expected_error
;
818 if (i
< kGuidOffset
) {
819 expected_error
= "Unable to read public flags.";
820 } else if (i
< GetSequenceNumberOffset(PACKET_0BYTE_GUID
,
822 expected_error
= "Unable to read GUID.";
823 } else if (i
< GetPrivateFlagsOffset(PACKET_0BYTE_GUID
, !kIncludeVersion
)) {
824 expected_error
= "Unable to read sequence number.";
825 } else if (i
< GetFecGroupOffset(PACKET_0BYTE_GUID
, !kIncludeVersion
)) {
826 expected_error
= "Unable to read private flags.";
828 expected_error
= "Unable to read first fec protected packet offset.";
830 CheckProcessingFails(packet
, i
, expected_error
, QUIC_INVALID_PACKET_HEADER
);
834 TEST_P(QuicFramerTest
, PacketHeaderWithVersionFlag
) {
835 unsigned char packet
[] = {
836 // public flags (version)
839 0x10, 0x32, 0x54, 0x76,
840 0x98, 0xBA, 0xDC, 0xFE,
842 'Q', '0', GetQuicVersionDigitTens(), GetQuicVersionDigitOnes(),
843 // packet sequence number
844 0xBC, 0x9A, 0x78, 0x56,
850 QuicEncryptedPacket
encrypted(AsChars(packet
), arraysize(packet
), false);
851 EXPECT_FALSE(framer_
.ProcessPacket(encrypted
));
852 EXPECT_EQ(QUIC_MISSING_PAYLOAD
, framer_
.error());
853 ASSERT_TRUE(visitor_
.header_
.get());
854 EXPECT_EQ(GG_UINT64_C(0xFEDCBA9876543210),
855 visitor_
.header_
->public_header
.guid
);
856 EXPECT_FALSE(visitor_
.header_
->public_header
.reset_flag
);
857 EXPECT_TRUE(visitor_
.header_
->public_header
.version_flag
);
858 EXPECT_EQ(GetParam(), visitor_
.header_
->public_header
.versions
[0]);
859 EXPECT_FALSE(visitor_
.header_
->fec_flag
);
860 EXPECT_FALSE(visitor_
.header_
->entropy_flag
);
861 EXPECT_EQ(0, visitor_
.header_
->entropy_hash
);
862 EXPECT_EQ(GG_UINT64_C(0x123456789ABC),
863 visitor_
.header_
->packet_sequence_number
);
864 EXPECT_EQ(NOT_IN_FEC_GROUP
, visitor_
.header_
->is_in_fec_group
);
865 EXPECT_EQ(0x00u
, visitor_
.header_
->fec_group
);
867 // Now test framing boundaries
869 i
< GetPacketHeaderSize(PACKET_8BYTE_GUID
, kIncludeVersion
,
870 PACKET_6BYTE_SEQUENCE_NUMBER
, NOT_IN_FEC_GROUP
);
872 string expected_error
;
873 if (i
< kGuidOffset
) {
874 expected_error
= "Unable to read public flags.";
875 } else if (i
< kVersionOffset
) {
876 expected_error
= "Unable to read GUID.";
877 } else if (i
< GetSequenceNumberOffset(kIncludeVersion
)) {
878 expected_error
= "Unable to read protocol version.";
879 } else if (i
< GetPrivateFlagsOffset(kIncludeVersion
)) {
880 expected_error
= "Unable to read sequence number.";
881 } else if (i
< GetFecGroupOffset(kIncludeVersion
)) {
882 expected_error
= "Unable to read private flags.";
884 expected_error
= "Unable to read first fec protected packet offset.";
886 CheckProcessingFails(packet
, i
, expected_error
, QUIC_INVALID_PACKET_HEADER
);
890 TEST_P(QuicFramerTest
, PacketHeaderWith4ByteSequenceNumber
) {
891 QuicFramerPeer::SetLastSequenceNumber(&framer_
,
892 GG_UINT64_C(0x123456789ABA));
894 unsigned char packet
[] = {
895 // public flags (8 byte guid and 4 byte sequence number)
898 0x10, 0x32, 0x54, 0x76,
899 0x98, 0xBA, 0xDC, 0xFE,
900 // packet sequence number
901 0xBC, 0x9A, 0x78, 0x56,
906 QuicEncryptedPacket
encrypted(AsChars(packet
), arraysize(packet
), false);
907 EXPECT_FALSE(framer_
.ProcessPacket(encrypted
));
908 EXPECT_EQ(QUIC_MISSING_PAYLOAD
, framer_
.error());
909 ASSERT_TRUE(visitor_
.header_
.get());
910 EXPECT_EQ(GG_UINT64_C(0xFEDCBA9876543210),
911 visitor_
.header_
->public_header
.guid
);
912 EXPECT_FALSE(visitor_
.header_
->public_header
.reset_flag
);
913 EXPECT_FALSE(visitor_
.header_
->public_header
.version_flag
);
914 EXPECT_FALSE(visitor_
.header_
->fec_flag
);
915 EXPECT_FALSE(visitor_
.header_
->entropy_flag
);
916 EXPECT_EQ(0, visitor_
.header_
->entropy_hash
);
917 EXPECT_EQ(GG_UINT64_C(0x123456789ABC),
918 visitor_
.header_
->packet_sequence_number
);
919 EXPECT_EQ(NOT_IN_FEC_GROUP
, visitor_
.header_
->is_in_fec_group
);
920 EXPECT_EQ(0x00u
, visitor_
.header_
->fec_group
);
922 // Now test framing boundaries
924 i
< GetPacketHeaderSize(PACKET_8BYTE_GUID
, !kIncludeVersion
,
925 PACKET_4BYTE_SEQUENCE_NUMBER
, NOT_IN_FEC_GROUP
);
927 string expected_error
;
928 if (i
< kGuidOffset
) {
929 expected_error
= "Unable to read public flags.";
930 } else if (i
< GetSequenceNumberOffset(!kIncludeVersion
)) {
931 expected_error
= "Unable to read GUID.";
932 } else if (i
< GetPrivateFlagsOffset(!kIncludeVersion
,
933 PACKET_4BYTE_SEQUENCE_NUMBER
)) {
934 expected_error
= "Unable to read sequence number.";
935 } else if (i
< GetFecGroupOffset(!kIncludeVersion
,
936 PACKET_4BYTE_SEQUENCE_NUMBER
)) {
937 expected_error
= "Unable to read private flags.";
939 expected_error
= "Unable to read first fec protected packet offset.";
941 CheckProcessingFails(packet
, i
, expected_error
, QUIC_INVALID_PACKET_HEADER
);
945 TEST_P(QuicFramerTest
, PacketHeaderWith2ByteSequenceNumber
) {
946 QuicFramerPeer::SetLastSequenceNumber(&framer_
,
947 GG_UINT64_C(0x123456789ABA));
949 unsigned char packet
[] = {
950 // public flags (8 byte guid and 2 byte sequence number)
953 0x10, 0x32, 0x54, 0x76,
954 0x98, 0xBA, 0xDC, 0xFE,
955 // packet sequence number
961 QuicEncryptedPacket
encrypted(AsChars(packet
), arraysize(packet
), false);
962 EXPECT_FALSE(framer_
.ProcessPacket(encrypted
));
963 EXPECT_EQ(QUIC_MISSING_PAYLOAD
, framer_
.error());
964 ASSERT_TRUE(visitor_
.header_
.get());
965 EXPECT_EQ(GG_UINT64_C(0xFEDCBA9876543210),
966 visitor_
.header_
->public_header
.guid
);
967 EXPECT_FALSE(visitor_
.header_
->public_header
.reset_flag
);
968 EXPECT_FALSE(visitor_
.header_
->public_header
.version_flag
);
969 EXPECT_FALSE(visitor_
.header_
->fec_flag
);
970 EXPECT_FALSE(visitor_
.header_
->entropy_flag
);
971 EXPECT_EQ(0, visitor_
.header_
->entropy_hash
);
972 EXPECT_EQ(GG_UINT64_C(0x123456789ABC),
973 visitor_
.header_
->packet_sequence_number
);
974 EXPECT_EQ(NOT_IN_FEC_GROUP
, visitor_
.header_
->is_in_fec_group
);
975 EXPECT_EQ(0x00u
, visitor_
.header_
->fec_group
);
977 // Now test framing boundaries
979 i
< GetPacketHeaderSize(PACKET_8BYTE_GUID
, !kIncludeVersion
,
980 PACKET_2BYTE_SEQUENCE_NUMBER
, NOT_IN_FEC_GROUP
);
982 string expected_error
;
983 if (i
< kGuidOffset
) {
984 expected_error
= "Unable to read public flags.";
985 } else if (i
< GetSequenceNumberOffset(!kIncludeVersion
)) {
986 expected_error
= "Unable to read GUID.";
987 } else if (i
< GetPrivateFlagsOffset(!kIncludeVersion
,
988 PACKET_2BYTE_SEQUENCE_NUMBER
)) {
989 expected_error
= "Unable to read sequence number.";
990 } else if (i
< GetFecGroupOffset(!kIncludeVersion
,
991 PACKET_2BYTE_SEQUENCE_NUMBER
)) {
992 expected_error
= "Unable to read private flags.";
994 expected_error
= "Unable to read first fec protected packet offset.";
996 CheckProcessingFails(packet
, i
, expected_error
, QUIC_INVALID_PACKET_HEADER
);
1000 TEST_P(QuicFramerTest
, PacketHeaderWith1ByteSequenceNumber
) {
1001 QuicFramerPeer::SetLastSequenceNumber(&framer_
,
1002 GG_UINT64_C(0x123456789ABA));
1004 unsigned char packet
[] = {
1005 // public flags (8 byte guid and 1 byte sequence number)
1008 0x10, 0x32, 0x54, 0x76,
1009 0x98, 0xBA, 0xDC, 0xFE,
1010 // packet sequence number
1016 QuicEncryptedPacket
encrypted(AsChars(packet
), arraysize(packet
), false);
1017 EXPECT_FALSE(framer_
.ProcessPacket(encrypted
));
1018 EXPECT_EQ(QUIC_MISSING_PAYLOAD
, framer_
.error());
1019 ASSERT_TRUE(visitor_
.header_
.get());
1020 EXPECT_EQ(GG_UINT64_C(0xFEDCBA9876543210),
1021 visitor_
.header_
->public_header
.guid
);
1022 EXPECT_FALSE(visitor_
.header_
->public_header
.reset_flag
);
1023 EXPECT_FALSE(visitor_
.header_
->public_header
.version_flag
);
1024 EXPECT_FALSE(visitor_
.header_
->fec_flag
);
1025 EXPECT_FALSE(visitor_
.header_
->entropy_flag
);
1026 EXPECT_EQ(0, visitor_
.header_
->entropy_hash
);
1027 EXPECT_EQ(GG_UINT64_C(0x123456789ABC),
1028 visitor_
.header_
->packet_sequence_number
);
1029 EXPECT_EQ(NOT_IN_FEC_GROUP
, visitor_
.header_
->is_in_fec_group
);
1030 EXPECT_EQ(0x00u
, visitor_
.header_
->fec_group
);
1032 // Now test framing boundaries
1034 i
< GetPacketHeaderSize(PACKET_8BYTE_GUID
, !kIncludeVersion
,
1035 PACKET_1BYTE_SEQUENCE_NUMBER
, NOT_IN_FEC_GROUP
);
1037 string expected_error
;
1038 if (i
< kGuidOffset
) {
1039 expected_error
= "Unable to read public flags.";
1040 } else if (i
< GetSequenceNumberOffset(!kIncludeVersion
)) {
1041 expected_error
= "Unable to read GUID.";
1042 } else if (i
< GetPrivateFlagsOffset(!kIncludeVersion
,
1043 PACKET_1BYTE_SEQUENCE_NUMBER
)) {
1044 expected_error
= "Unable to read sequence number.";
1045 } else if (i
< GetFecGroupOffset(!kIncludeVersion
,
1046 PACKET_1BYTE_SEQUENCE_NUMBER
)) {
1047 expected_error
= "Unable to read private flags.";
1049 expected_error
= "Unable to read first fec protected packet offset.";
1051 CheckProcessingFails(packet
, i
, expected_error
, QUIC_INVALID_PACKET_HEADER
);
1055 TEST_P(QuicFramerTest
, InvalidPublicFlag
) {
1056 unsigned char packet
[] = {
1057 // public flags, unknown flag at bit 6
1060 0x10, 0x32, 0x54, 0x76,
1061 0x98, 0xBA, 0xDC, 0xFE,
1062 // packet sequence number
1063 0xBC, 0x9A, 0x78, 0x56,
1068 // frame type (padding)
1070 0x00, 0x00, 0x00, 0x00
1072 CheckProcessingFails(packet
,
1074 "Illegal public flags value.",
1075 QUIC_INVALID_PACKET_HEADER
);
1078 TEST_P(QuicFramerTest
, InvalidPublicFlagWithMatchingVersions
) {
1079 unsigned char packet
[] = {
1080 // public flags (8 byte guid and version flag and an unknown flag)
1083 0x10, 0x32, 0x54, 0x76,
1084 0x98, 0xBA, 0xDC, 0xFE,
1086 'Q', '0', GetQuicVersionDigitTens(), GetQuicVersionDigitOnes(),
1087 // packet sequence number
1088 0xBC, 0x9A, 0x78, 0x56,
1093 // frame type (padding)
1095 0x00, 0x00, 0x00, 0x00
1097 CheckProcessingFails(packet
,
1099 "Illegal public flags value.",
1100 QUIC_INVALID_PACKET_HEADER
);
1103 TEST_P(QuicFramerTest
, LargePublicFlagWithMismatchedVersions
) {
1104 unsigned char packet
[] = {
1105 // public flags (8 byte guid, version flag and an unknown flag)
1108 0x10, 0x32, 0x54, 0x76,
1109 0x98, 0xBA, 0xDC, 0xFE,
1112 // packet sequence number
1113 0xBC, 0x9A, 0x78, 0x56,
1118 // frame type (padding frame)
1120 0x00, 0x00, 0x00, 0x00
1122 QuicEncryptedPacket
encrypted(AsChars(packet
), arraysize(packet
), false);
1123 EXPECT_TRUE(framer_
.ProcessPacket(encrypted
));
1124 EXPECT_EQ(QUIC_NO_ERROR
, framer_
.error());
1125 ASSERT_TRUE(visitor_
.header_
.get());
1126 EXPECT_EQ(0, visitor_
.frame_count_
);
1127 EXPECT_EQ(1, visitor_
.version_mismatch_
);
1130 TEST_P(QuicFramerTest
, InvalidPrivateFlag
) {
1131 unsigned char packet
[] = {
1132 // public flags (8 byte guid)
1135 0x10, 0x32, 0x54, 0x76,
1136 0x98, 0xBA, 0xDC, 0xFE,
1137 // packet sequence number
1138 0xBC, 0x9A, 0x78, 0x56,
1143 // frame type (padding)
1145 0x00, 0x00, 0x00, 0x00
1147 CheckProcessingFails(packet
,
1149 "Illegal private flags value.",
1150 QUIC_INVALID_PACKET_HEADER
);
1153 TEST_P(QuicFramerTest
, InvalidFECGroupOffset
) {
1154 unsigned char packet
[] = {
1155 // public flags (8 byte guid)
1158 0x10, 0x32, 0x54, 0x76,
1159 0x98, 0xBA, 0xDC, 0xFE,
1160 // packet sequence number
1161 0x01, 0x00, 0x00, 0x00,
1163 // private flags (fec group)
1165 // first fec protected packet offset
1168 CheckProcessingFails(packet
,
1170 "First fec protected packet offset must be less "
1171 "than the sequence number.",
1172 QUIC_INVALID_PACKET_HEADER
);
1175 TEST_P(QuicFramerTest
, PaddingFrame
) {
1176 unsigned char packet
[] = {
1177 // public flags (8 byte guid)
1180 0x10, 0x32, 0x54, 0x76,
1181 0x98, 0xBA, 0xDC, 0xFE,
1182 // packet sequence number
1183 0xBC, 0x9A, 0x78, 0x56,
1188 // frame type (padding frame)
1190 // Ignored data (which in this case is a stream frame)
1191 // frame type (stream frame with fin)
1194 0x04, 0x03, 0x02, 0x01,
1196 0x54, 0x76, 0x10, 0x32,
1197 0xDC, 0xFE, 0x98, 0xBA,
1206 QuicEncryptedPacket
encrypted(AsChars(packet
), arraysize(packet
), false);
1207 EXPECT_TRUE(framer_
.ProcessPacket(encrypted
));
1208 EXPECT_EQ(QUIC_NO_ERROR
, framer_
.error());
1209 ASSERT_TRUE(visitor_
.header_
.get());
1210 EXPECT_TRUE(CheckDecryption(encrypted
, !kIncludeVersion
));
1212 ASSERT_EQ(0u, visitor_
.stream_frames_
.size());
1213 EXPECT_EQ(0u, visitor_
.ack_frames_
.size());
1214 // A packet with no frames is not acceptable.
1215 CheckProcessingFails(
1217 GetPacketHeaderSize(PACKET_8BYTE_GUID
, !kIncludeVersion
,
1218 PACKET_6BYTE_SEQUENCE_NUMBER
, NOT_IN_FEC_GROUP
),
1219 "Packet has no frames.", QUIC_MISSING_PAYLOAD
);
1222 TEST_P(QuicFramerTest
, StreamFrame
) {
1223 unsigned char packet
[] = {
1224 // public flags (8 byte guid)
1227 0x10, 0x32, 0x54, 0x76,
1228 0x98, 0xBA, 0xDC, 0xFE,
1229 // packet sequence number
1230 0xBC, 0x9A, 0x78, 0x56,
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
));
1253 EXPECT_EQ(QUIC_NO_ERROR
, framer_
.error());
1254 ASSERT_TRUE(visitor_
.header_
.get());
1255 EXPECT_TRUE(CheckDecryption(encrypted
, !kIncludeVersion
));
1257 ASSERT_EQ(1u, visitor_
.stream_frames_
.size());
1258 EXPECT_EQ(0u, visitor_
.ack_frames_
.size());
1259 EXPECT_EQ(static_cast<uint64
>(0x01020304),
1260 visitor_
.stream_frames_
[0]->stream_id
);
1261 EXPECT_TRUE(visitor_
.stream_frames_
[0]->fin
);
1262 EXPECT_EQ(GG_UINT64_C(0xBA98FEDC32107654),
1263 visitor_
.stream_frames_
[0]->offset
);
1264 CheckStreamFrameData("hello world!", visitor_
.stream_frames_
[0]);
1266 // Now test framing boundaries
1267 CheckStreamFrameBoundaries(packet
, kQuicMaxStreamIdSize
, !kIncludeVersion
);
1270 TEST_P(QuicFramerTest
, StreamFrame3ByteStreamId
) {
1271 unsigned char packet
[] = {
1272 // public flags (8 byte guid)
1275 0x10, 0x32, 0x54, 0x76,
1276 0x98, 0xBA, 0xDC, 0xFE,
1277 // packet sequence number
1278 0xBC, 0x9A, 0x78, 0x56,
1283 // frame type (stream frame with fin)
1288 0x54, 0x76, 0x10, 0x32,
1289 0xDC, 0xFE, 0x98, 0xBA,
1298 QuicEncryptedPacket
encrypted(AsChars(packet
), arraysize(packet
), false);
1299 EXPECT_TRUE(framer_
.ProcessPacket(encrypted
));
1301 EXPECT_EQ(QUIC_NO_ERROR
, framer_
.error());
1302 ASSERT_TRUE(visitor_
.header_
.get());
1303 EXPECT_TRUE(CheckDecryption(encrypted
, !kIncludeVersion
));
1305 ASSERT_EQ(1u, visitor_
.stream_frames_
.size());
1306 EXPECT_EQ(0u, visitor_
.ack_frames_
.size());
1307 EXPECT_EQ(GG_UINT64_C(0x00020304),
1308 visitor_
.stream_frames_
[0]->stream_id
);
1309 EXPECT_TRUE(visitor_
.stream_frames_
[0]->fin
);
1310 EXPECT_EQ(GG_UINT64_C(0xBA98FEDC32107654),
1311 visitor_
.stream_frames_
[0]->offset
);
1312 CheckStreamFrameData("hello world!", visitor_
.stream_frames_
[0]);
1314 // Now test framing boundaries
1315 const size_t stream_id_size
= 3;
1316 CheckStreamFrameBoundaries(packet
, stream_id_size
, !kIncludeVersion
);
1319 TEST_P(QuicFramerTest
, StreamFrame2ByteStreamId
) {
1320 unsigned char packet
[] = {
1321 // public flags (8 byte guid)
1324 0x10, 0x32, 0x54, 0x76,
1325 0x98, 0xBA, 0xDC, 0xFE,
1326 // packet sequence number
1327 0xBC, 0x9A, 0x78, 0x56,
1332 // frame type (stream frame with fin)
1337 0x54, 0x76, 0x10, 0x32,
1338 0xDC, 0xFE, 0x98, 0xBA,
1347 QuicEncryptedPacket
encrypted(AsChars(packet
), arraysize(packet
), false);
1348 EXPECT_TRUE(framer_
.ProcessPacket(encrypted
));
1350 EXPECT_EQ(QUIC_NO_ERROR
, framer_
.error());
1351 ASSERT_TRUE(visitor_
.header_
.get());
1352 EXPECT_TRUE(CheckDecryption(encrypted
, !kIncludeVersion
));
1354 ASSERT_EQ(1u, visitor_
.stream_frames_
.size());
1355 EXPECT_EQ(0u, visitor_
.ack_frames_
.size());
1356 EXPECT_EQ(static_cast<uint64
>(0x00000304),
1357 visitor_
.stream_frames_
[0]->stream_id
);
1358 EXPECT_TRUE(visitor_
.stream_frames_
[0]->fin
);
1359 EXPECT_EQ(GG_UINT64_C(0xBA98FEDC32107654),
1360 visitor_
.stream_frames_
[0]->offset
);
1361 CheckStreamFrameData("hello world!", visitor_
.stream_frames_
[0]);
1363 // Now test framing boundaries
1364 const size_t stream_id_size
= 2;
1365 CheckStreamFrameBoundaries(packet
, stream_id_size
, !kIncludeVersion
);
1368 TEST_P(QuicFramerTest
, StreamFrame1ByteStreamId
) {
1369 unsigned char packet
[] = {
1370 // public flags (8 byte guid)
1373 0x10, 0x32, 0x54, 0x76,
1374 0x98, 0xBA, 0xDC, 0xFE,
1375 // packet sequence number
1376 0xBC, 0x9A, 0x78, 0x56,
1381 // frame type (stream frame with fin)
1386 0x54, 0x76, 0x10, 0x32,
1387 0xDC, 0xFE, 0x98, 0xBA,
1396 QuicEncryptedPacket
encrypted(AsChars(packet
), arraysize(packet
), false);
1397 EXPECT_TRUE(framer_
.ProcessPacket(encrypted
));
1399 EXPECT_EQ(QUIC_NO_ERROR
, framer_
.error());
1400 ASSERT_TRUE(visitor_
.header_
.get());
1401 EXPECT_TRUE(CheckDecryption(encrypted
, !kIncludeVersion
));
1403 ASSERT_EQ(1u, visitor_
.stream_frames_
.size());
1404 EXPECT_EQ(0u, visitor_
.ack_frames_
.size());
1405 EXPECT_EQ(static_cast<uint64
>(0x00000004),
1406 visitor_
.stream_frames_
[0]->stream_id
);
1407 EXPECT_TRUE(visitor_
.stream_frames_
[0]->fin
);
1408 EXPECT_EQ(GG_UINT64_C(0xBA98FEDC32107654),
1409 visitor_
.stream_frames_
[0]->offset
);
1410 CheckStreamFrameData("hello world!", visitor_
.stream_frames_
[0]);
1412 // Now test framing boundaries
1413 const size_t stream_id_size
= 1;
1414 CheckStreamFrameBoundaries(packet
, stream_id_size
, !kIncludeVersion
);
1417 TEST_P(QuicFramerTest
, StreamFrameWithVersion
) {
1418 unsigned char packet
[] = {
1419 // public flags (version, 8 byte guid)
1422 0x10, 0x32, 0x54, 0x76,
1423 0x98, 0xBA, 0xDC, 0xFE,
1425 'Q', '0', GetQuicVersionDigitTens(), GetQuicVersionDigitOnes(),
1426 // packet sequence number
1427 0xBC, 0x9A, 0x78, 0x56,
1432 // frame type (stream frame with fin)
1435 0x04, 0x03, 0x02, 0x01,
1437 0x54, 0x76, 0x10, 0x32,
1438 0xDC, 0xFE, 0x98, 0xBA,
1447 QuicEncryptedPacket
encrypted(AsChars(packet
), arraysize(packet
), false);
1448 EXPECT_TRUE(framer_
.ProcessPacket(encrypted
));
1450 EXPECT_EQ(QUIC_NO_ERROR
, framer_
.error());
1451 ASSERT_TRUE(visitor_
.header_
.get());
1452 EXPECT_TRUE(visitor_
.header_
.get()->public_header
.version_flag
);
1453 EXPECT_EQ(GetParam(), visitor_
.header_
.get()->public_header
.versions
[0]);
1454 EXPECT_TRUE(CheckDecryption(encrypted
, kIncludeVersion
));
1456 ASSERT_EQ(1u, visitor_
.stream_frames_
.size());
1457 EXPECT_EQ(0u, visitor_
.ack_frames_
.size());
1458 EXPECT_EQ(static_cast<uint64
>(0x01020304),
1459 visitor_
.stream_frames_
[0]->stream_id
);
1460 EXPECT_TRUE(visitor_
.stream_frames_
[0]->fin
);
1461 EXPECT_EQ(GG_UINT64_C(0xBA98FEDC32107654),
1462 visitor_
.stream_frames_
[0]->offset
);
1463 CheckStreamFrameData("hello world!", visitor_
.stream_frames_
[0]);
1465 // Now test framing boundaries
1466 CheckStreamFrameBoundaries(packet
, kQuicMaxStreamIdSize
, kIncludeVersion
);
1469 TEST_P(QuicFramerTest
, RejectPacket
) {
1470 visitor_
.accept_packet_
= false;
1472 unsigned char packet
[] = {
1473 // public flags (8 byte guid)
1476 0x10, 0x32, 0x54, 0x76,
1477 0x98, 0xBA, 0xDC, 0xFE,
1478 // packet sequence number
1479 0xBC, 0x9A, 0x78, 0x56,
1484 // frame type (stream frame with fin)
1487 0x04, 0x03, 0x02, 0x01,
1489 0x54, 0x76, 0x10, 0x32,
1490 0xDC, 0xFE, 0x98, 0xBA,
1499 QuicEncryptedPacket
encrypted(AsChars(packet
), arraysize(packet
), false);
1500 EXPECT_TRUE(framer_
.ProcessPacket(encrypted
));
1502 EXPECT_EQ(QUIC_NO_ERROR
, framer_
.error());
1503 ASSERT_TRUE(visitor_
.header_
.get());
1504 EXPECT_TRUE(CheckDecryption(encrypted
, !kIncludeVersion
));
1506 ASSERT_EQ(0u, visitor_
.stream_frames_
.size());
1507 EXPECT_EQ(0u, visitor_
.ack_frames_
.size());
1510 TEST_P(QuicFramerTest
, RejectPublicHeader
) {
1511 visitor_
.accept_public_header_
= false;
1513 unsigned char packet
[] = {
1514 // public flags (8 byte guid)
1517 0x10, 0x32, 0x54, 0x76,
1518 0x98, 0xBA, 0xDC, 0xFE,
1521 QuicEncryptedPacket
encrypted(AsChars(packet
), arraysize(packet
), false);
1522 EXPECT_TRUE(framer_
.ProcessPacket(encrypted
));
1524 EXPECT_EQ(QUIC_NO_ERROR
, framer_
.error());
1525 ASSERT_TRUE(visitor_
.public_header_
.get());
1526 ASSERT_FALSE(visitor_
.header_
.get());
1529 TEST_P(QuicFramerTest
, RevivedStreamFrame
) {
1530 unsigned char payload
[] = {
1531 // frame type (stream frame with fin)
1534 0x04, 0x03, 0x02, 0x01,
1536 0x54, 0x76, 0x10, 0x32,
1537 0xDC, 0xFE, 0x98, 0xBA,
1546 QuicPacketHeader header
;
1547 header
.public_header
.guid
= GG_UINT64_C(0xFEDCBA9876543210);
1548 header
.public_header
.reset_flag
= false;
1549 header
.public_header
.version_flag
= false;
1550 header
.fec_flag
= true;
1551 header
.entropy_flag
= true;
1552 header
.packet_sequence_number
= GG_UINT64_C(0x123456789ABC);
1553 header
.fec_group
= 0;
1555 // Do not encrypt the payload because the revived payload is post-encryption.
1556 EXPECT_TRUE(framer_
.ProcessRevivedPacket(&header
,
1557 StringPiece(AsChars(payload
),
1558 arraysize(payload
))));
1560 EXPECT_EQ(QUIC_NO_ERROR
, framer_
.error());
1561 ASSERT_EQ(1, visitor_
.revived_packets_
);
1562 ASSERT_TRUE(visitor_
.header_
.get());
1563 EXPECT_EQ(GG_UINT64_C(0xFEDCBA9876543210),
1564 visitor_
.header_
->public_header
.guid
);
1565 EXPECT_FALSE(visitor_
.header_
->public_header
.reset_flag
);
1566 EXPECT_FALSE(visitor_
.header_
->public_header
.version_flag
);
1567 EXPECT_TRUE(visitor_
.header_
->fec_flag
);
1568 EXPECT_TRUE(visitor_
.header_
->entropy_flag
);
1569 EXPECT_EQ(1 << (header
.packet_sequence_number
% 8),
1570 visitor_
.header_
->entropy_hash
);
1571 EXPECT_EQ(GG_UINT64_C(0x123456789ABC),
1572 visitor_
.header_
->packet_sequence_number
);
1573 EXPECT_EQ(NOT_IN_FEC_GROUP
, visitor_
.header_
->is_in_fec_group
);
1574 EXPECT_EQ(0x00u
, visitor_
.header_
->fec_group
);
1576 ASSERT_EQ(1u, visitor_
.stream_frames_
.size());
1577 EXPECT_EQ(0u, visitor_
.ack_frames_
.size());
1578 EXPECT_EQ(GG_UINT64_C(0x01020304), visitor_
.stream_frames_
[0]->stream_id
);
1579 EXPECT_TRUE(visitor_
.stream_frames_
[0]->fin
);
1580 EXPECT_EQ(GG_UINT64_C(0xBA98FEDC32107654),
1581 visitor_
.stream_frames_
[0]->offset
);
1582 CheckStreamFrameData("hello world!", visitor_
.stream_frames_
[0]);
1585 TEST_P(QuicFramerTest
, StreamFrameInFecGroup
) {
1586 unsigned char packet
[] = {
1587 // public flags (8 byte guid)
1590 0x10, 0x32, 0x54, 0x76,
1591 0x98, 0xBA, 0xDC, 0xFE,
1592 // packet sequence number
1593 0xBC, 0x9A, 0x78, 0x56,
1595 // private flags (fec group)
1597 // first fec protected packet offset
1600 // frame type (stream frame with fin)
1603 0x04, 0x03, 0x02, 0x01,
1605 0x54, 0x76, 0x10, 0x32,
1606 0xDC, 0xFE, 0x98, 0xBA,
1615 QuicEncryptedPacket
encrypted(AsChars(packet
), arraysize(packet
), false);
1616 EXPECT_TRUE(framer_
.ProcessPacket(encrypted
));
1618 EXPECT_EQ(QUIC_NO_ERROR
, framer_
.error());
1619 ASSERT_TRUE(visitor_
.header_
.get());
1620 EXPECT_TRUE(CheckDecryption(encrypted
, !kIncludeVersion
));
1621 EXPECT_EQ(IN_FEC_GROUP
, visitor_
.header_
->is_in_fec_group
);
1622 EXPECT_EQ(GG_UINT64_C(0x341256789ABA),
1623 visitor_
.header_
->fec_group
);
1624 const size_t fec_offset
= GetStartOfFecProtectedData(
1625 PACKET_8BYTE_GUID
, !kIncludeVersion
, PACKET_6BYTE_SEQUENCE_NUMBER
);
1627 string(AsChars(packet
) + fec_offset
, arraysize(packet
) - fec_offset
),
1628 visitor_
.fec_protected_payload_
);
1630 ASSERT_EQ(1u, visitor_
.stream_frames_
.size());
1631 EXPECT_EQ(0u, visitor_
.ack_frames_
.size());
1632 EXPECT_EQ(GG_UINT64_C(0x01020304), visitor_
.stream_frames_
[0]->stream_id
);
1633 EXPECT_TRUE(visitor_
.stream_frames_
[0]->fin
);
1634 EXPECT_EQ(GG_UINT64_C(0xBA98FEDC32107654),
1635 visitor_
.stream_frames_
[0]->offset
);
1636 CheckStreamFrameData("hello world!", visitor_
.stream_frames_
[0]);
1639 TEST_P(QuicFramerTest
, AckFrame
) {
1640 unsigned char packet
[] = {
1641 // public flags (8 byte guid)
1644 0x10, 0x32, 0x54, 0x76,
1645 0x98, 0xBA, 0xDC, 0xFE,
1646 // packet sequence number
1647 0xA8, 0x9A, 0x78, 0x56,
1649 // private flags (entropy)
1652 // frame type (ack frame)
1653 // (has nacks, not truncated, 6 byte largest observed, 1 byte delta)
1655 // entropy hash of sent packets till least awaiting - 1.
1657 // least packet sequence number awaiting an ack, delta from sequence number.
1658 0x08, 0x00, 0x00, 0x00,
1660 // entropy hash of all received packets.
1662 // largest observed packet sequence number
1663 0xBF, 0x9A, 0x78, 0x56,
1667 // num missing packets
1669 // missing packet delta
1671 // 0 more missing packets in range.
1675 QuicEncryptedPacket
encrypted(AsChars(packet
), arraysize(packet
), false);
1676 EXPECT_TRUE(framer_
.ProcessPacket(encrypted
));
1678 EXPECT_EQ(QUIC_NO_ERROR
, framer_
.error());
1679 ASSERT_TRUE(visitor_
.header_
.get());
1680 EXPECT_TRUE(CheckDecryption(encrypted
, !kIncludeVersion
));
1682 EXPECT_EQ(0u, visitor_
.stream_frames_
.size());
1683 ASSERT_EQ(1u, visitor_
.ack_frames_
.size());
1684 const QuicAckFrame
& frame
= *visitor_
.ack_frames_
[0];
1685 EXPECT_EQ(0xAB, frame
.sent_info
.entropy_hash
);
1686 EXPECT_EQ(0xBA, frame
.received_info
.entropy_hash
);
1687 EXPECT_EQ(GG_UINT64_C(0x0123456789ABF), frame
.received_info
.largest_observed
);
1688 ASSERT_EQ(1u, frame
.received_info
.missing_packets
.size());
1689 SequenceNumberSet::const_iterator missing_iter
=
1690 frame
.received_info
.missing_packets
.begin();
1691 EXPECT_EQ(GG_UINT64_C(0x0123456789ABE), *missing_iter
);
1692 EXPECT_EQ(GG_UINT64_C(0x0123456789AA0), frame
.sent_info
.least_unacked
);
1694 const size_t kSentEntropyOffset
= kQuicFrameTypeSize
;
1695 const size_t kLeastUnackedOffset
= kSentEntropyOffset
+ kQuicEntropyHashSize
;
1696 const size_t kReceivedEntropyOffset
= kLeastUnackedOffset
+
1697 PACKET_6BYTE_SEQUENCE_NUMBER
;
1698 const size_t kLargestObservedOffset
= kReceivedEntropyOffset
+
1699 kQuicEntropyHashSize
;
1700 const size_t kMissingDeltaTimeOffset
= kLargestObservedOffset
+
1701 PACKET_6BYTE_SEQUENCE_NUMBER
;
1702 const size_t kNumMissingPacketOffset
= kMissingDeltaTimeOffset
+
1703 kQuicDeltaTimeLargestObservedSize
;
1704 const size_t kMissingPacketsOffset
= kNumMissingPacketOffset
+
1705 kNumberOfMissingPacketsSize
;
1706 const size_t kMissingPacketsRange
= kMissingPacketsOffset
+
1707 PACKET_1BYTE_SEQUENCE_NUMBER
;
1708 // Now test framing boundaries
1709 const size_t ack_frame_size
= kMissingPacketsRange
+
1710 PACKET_1BYTE_SEQUENCE_NUMBER
;
1711 for (size_t i
= kQuicFrameTypeSize
; i
< ack_frame_size
; ++i
) {
1712 string expected_error
;
1713 if (i
< kLeastUnackedOffset
) {
1714 expected_error
= "Unable to read entropy hash for sent packets.";
1715 } else if (i
< kReceivedEntropyOffset
) {
1716 expected_error
= "Unable to read least unacked delta.";
1717 } else if (i
< kLargestObservedOffset
) {
1718 expected_error
= "Unable to read entropy hash for received packets.";
1719 } else if (i
< kMissingDeltaTimeOffset
) {
1720 expected_error
= "Unable to read largest observed.";
1721 } else if (i
< kNumMissingPacketOffset
) {
1722 expected_error
= "Unable to read delta time largest observed.";
1723 } else if (i
< kMissingPacketsOffset
) {
1724 expected_error
= "Unable to read num missing packet ranges.";
1725 } else if (i
< kMissingPacketsRange
) {
1726 expected_error
= "Unable to read missing sequence number delta.";
1728 expected_error
= "Unable to read missing sequence number range.";
1730 CheckProcessingFails(
1732 i
+ GetPacketHeaderSize(PACKET_8BYTE_GUID
, !kIncludeVersion
,
1733 PACKET_6BYTE_SEQUENCE_NUMBER
, NOT_IN_FEC_GROUP
),
1734 expected_error
, QUIC_INVALID_ACK_DATA
);
1738 TEST_P(QuicFramerTest
, AckFrameNoNacks
) {
1739 unsigned char packet
[] = {
1740 // public flags (8 byte guid)
1743 0x10, 0x32, 0x54, 0x76,
1744 0x98, 0xBA, 0xDC, 0xFE,
1745 // packet sequence number
1746 0xA8, 0x9A, 0x78, 0x56,
1748 // private flags (entropy)
1751 // frame type (ack frame)
1752 // (no nacks, not truncated, 6 byte largest observed, 1 byte delta)
1754 // entropy hash of sent packets till least awaiting - 1.
1756 // least packet sequence number awaiting an ack, delta from sequence number.
1757 0x08, 0x00, 0x00, 0x00,
1759 // entropy hash of all received packets.
1761 // largest observed packet sequence number
1762 0xBF, 0x9A, 0x78, 0x56,
1768 QuicEncryptedPacket
encrypted(AsChars(packet
), arraysize(packet
), false);
1769 EXPECT_TRUE(framer_
.ProcessPacket(encrypted
));
1771 EXPECT_EQ(QUIC_NO_ERROR
, framer_
.error());
1772 ASSERT_TRUE(visitor_
.header_
.get());
1773 EXPECT_TRUE(CheckDecryption(encrypted
, !kIncludeVersion
));
1775 EXPECT_EQ(0u, visitor_
.stream_frames_
.size());
1776 ASSERT_EQ(1u, visitor_
.ack_frames_
.size());
1777 QuicAckFrame
* frame
= visitor_
.ack_frames_
[0];
1778 EXPECT_EQ(0xAB, frame
->sent_info
.entropy_hash
);
1779 EXPECT_EQ(0xBA, frame
->received_info
.entropy_hash
);
1780 EXPECT_EQ(GG_UINT64_C(0x0123456789ABF),
1781 frame
->received_info
.largest_observed
);
1782 ASSERT_EQ(0u, frame
->received_info
.missing_packets
.size());
1783 EXPECT_EQ(GG_UINT64_C(0x0123456789AA0), frame
->sent_info
.least_unacked
);
1785 // Verify that the packet re-serializes identically.
1787 frames
.push_back(QuicFrame(frame
));
1788 scoped_ptr
<QuicPacket
> data(
1789 framer_
.BuildUnsizedDataPacket(*visitor_
.header_
, frames
).packet
);
1790 ASSERT_TRUE(data
!= NULL
);
1792 test::CompareCharArraysWithHexError("constructed packet",
1793 data
->data(), data
->length(),
1794 AsChars(packet
), arraysize(packet
));
1797 TEST_P(QuicFramerTest
, AckFrame500Nacks
) {
1798 unsigned char packet
[] = {
1799 // public flags (8 byte guid)
1802 0x10, 0x32, 0x54, 0x76,
1803 0x98, 0xBA, 0xDC, 0xFE,
1804 // packet sequence number
1805 0xA8, 0x9A, 0x78, 0x56,
1807 // private flags (entropy)
1810 // frame type (ack frame)
1811 // (has nacks, not truncated, 6 byte largest observed, 1 byte delta)
1813 // entropy hash of sent packets till least awaiting - 1.
1815 // least packet sequence number awaiting an ack, delta from sequence number.
1816 0x08, 0x00, 0x00, 0x00,
1818 // entropy hash of all received packets.
1820 // largest observed packet sequence number
1821 0xBF, 0x9A, 0x78, 0x56,
1825 // num missing packet ranges
1827 // missing packet delta
1829 // 243 more missing packets in range.
1830 // The ranges are listed in this order so the re-constructed packet matches.
1832 // No gap between ranges
1834 // 255 more missing packets in range.
1838 QuicEncryptedPacket
encrypted(AsChars(packet
), arraysize(packet
), false);
1839 EXPECT_TRUE(framer_
.ProcessPacket(encrypted
));
1841 EXPECT_EQ(QUIC_NO_ERROR
, framer_
.error());
1842 ASSERT_TRUE(visitor_
.header_
.get());
1843 EXPECT_TRUE(CheckDecryption(encrypted
, !kIncludeVersion
));
1845 EXPECT_EQ(0u, visitor_
.stream_frames_
.size());
1846 ASSERT_EQ(1u, visitor_
.ack_frames_
.size());
1847 QuicAckFrame
* frame
= visitor_
.ack_frames_
[0];
1848 EXPECT_EQ(0xAB, frame
->sent_info
.entropy_hash
);
1849 EXPECT_EQ(0xBA, frame
->received_info
.entropy_hash
);
1850 EXPECT_EQ(GG_UINT64_C(0x0123456789ABF),
1851 frame
->received_info
.largest_observed
);
1852 ASSERT_EQ(500u, frame
->received_info
.missing_packets
.size());
1853 SequenceNumberSet::const_iterator first_missing_iter
=
1854 frame
->received_info
.missing_packets
.begin();
1855 EXPECT_EQ(GG_UINT64_C(0x0123456789ABE) - 499, *first_missing_iter
);
1856 SequenceNumberSet::const_reverse_iterator last_missing_iter
=
1857 frame
->received_info
.missing_packets
.rbegin();
1858 EXPECT_EQ(GG_UINT64_C(0x0123456789ABE), *last_missing_iter
);
1859 EXPECT_EQ(GG_UINT64_C(0x0123456789AA0), frame
->sent_info
.least_unacked
);
1861 // Verify that the packet re-serializes identically.
1863 frames
.push_back(QuicFrame(frame
));
1864 scoped_ptr
<QuicPacket
> data(
1865 framer_
.BuildUnsizedDataPacket(*visitor_
.header_
, frames
).packet
);
1866 ASSERT_TRUE(data
!= NULL
);
1868 test::CompareCharArraysWithHexError("constructed packet",
1869 data
->data(), data
->length(),
1870 AsChars(packet
), arraysize(packet
));
1873 TEST_P(QuicFramerTest
, CongestionFeedbackFrameTCP
) {
1874 unsigned char packet
[] = {
1875 // public flags (8 byte guid)
1878 0x10, 0x32, 0x54, 0x76,
1879 0x98, 0xBA, 0xDC, 0xFE,
1880 // packet sequence number
1881 0xBC, 0x9A, 0x78, 0x56,
1886 // frame type (congestion feedback frame)
1888 // congestion feedback type (tcp)
1890 // ack_frame.feedback.tcp.accumulated_number_of_lost_packets
1892 // ack_frame.feedback.tcp.receive_window
1896 QuicEncryptedPacket
encrypted(AsChars(packet
), arraysize(packet
), false);
1897 EXPECT_TRUE(framer_
.ProcessPacket(encrypted
));
1899 EXPECT_EQ(QUIC_NO_ERROR
, framer_
.error());
1900 ASSERT_TRUE(visitor_
.header_
.get());
1901 EXPECT_TRUE(CheckDecryption(encrypted
, !kIncludeVersion
));
1903 EXPECT_EQ(0u, visitor_
.stream_frames_
.size());
1904 ASSERT_EQ(1u, visitor_
.congestion_feedback_frames_
.size());
1905 const QuicCongestionFeedbackFrame
& frame
=
1906 *visitor_
.congestion_feedback_frames_
[0];
1907 ASSERT_EQ(kTCP
, frame
.type
);
1909 frame
.tcp
.accumulated_number_of_lost_packets
);
1910 EXPECT_EQ(0x4030u
, frame
.tcp
.receive_window
);
1912 // Now test framing boundaries
1913 for (size_t i
= kQuicFrameTypeSize
; i
< 6; ++i
) {
1914 string expected_error
;
1916 expected_error
= "Unable to read congestion feedback type.";
1918 expected_error
= "Unable to read accumulated number of lost packets.";
1920 expected_error
= "Unable to read receive window.";
1922 CheckProcessingFails(
1924 i
+ GetPacketHeaderSize(PACKET_8BYTE_GUID
, !kIncludeVersion
,
1925 PACKET_6BYTE_SEQUENCE_NUMBER
, NOT_IN_FEC_GROUP
),
1926 expected_error
, QUIC_INVALID_CONGESTION_FEEDBACK_DATA
);
1930 TEST_P(QuicFramerTest
, CongestionFeedbackFrameInterArrival
) {
1931 unsigned char packet
[] = {
1932 // public flags (8 byte guid)
1935 0x10, 0x32, 0x54, 0x76,
1936 0x98, 0xBA, 0xDC, 0xFE,
1937 // packet sequence number
1938 0xBC, 0x9A, 0x78, 0x56,
1943 // frame type (congestion feedback frame)
1945 // congestion feedback type (inter arrival)
1947 // accumulated_number_of_lost_packets
1949 // num received packets
1951 // lowest sequence number
1952 0xBA, 0x9A, 0x78, 0x56,
1955 0x87, 0x96, 0xA5, 0xB4,
1956 0xC3, 0xD2, 0xE1, 0x07,
1960 0x01, 0x00, 0x00, 0x00,
1961 // sequence delta (skip one packet)
1964 0x02, 0x00, 0x00, 0x00,
1967 QuicEncryptedPacket
encrypted(AsChars(packet
), arraysize(packet
), false);
1968 EXPECT_TRUE(framer_
.ProcessPacket(encrypted
));
1970 EXPECT_EQ(QUIC_NO_ERROR
, framer_
.error());
1971 ASSERT_TRUE(visitor_
.header_
.get());
1972 EXPECT_TRUE(CheckDecryption(encrypted
, !kIncludeVersion
));
1974 EXPECT_EQ(0u, visitor_
.stream_frames_
.size());
1975 ASSERT_EQ(1u, visitor_
.congestion_feedback_frames_
.size());
1976 const QuicCongestionFeedbackFrame
& frame
=
1977 *visitor_
.congestion_feedback_frames_
[0];
1978 ASSERT_EQ(kInterArrival
, frame
.type
);
1979 EXPECT_EQ(0x0302, frame
.inter_arrival
.
1980 accumulated_number_of_lost_packets
);
1981 ASSERT_EQ(3u, frame
.inter_arrival
.received_packet_times
.size());
1982 TimeMap::const_iterator iter
=
1983 frame
.inter_arrival
.received_packet_times
.begin();
1984 EXPECT_EQ(GG_UINT64_C(0x0123456789ABA), iter
->first
);
1985 EXPECT_EQ(GG_INT64_C(0x07E1D2C3B4A59687),
1986 iter
->second
.Subtract(start_
).ToMicroseconds());
1988 EXPECT_EQ(GG_UINT64_C(0x0123456789ABB), iter
->first
);
1989 EXPECT_EQ(GG_INT64_C(0x07E1D2C3B4A59688),
1990 iter
->second
.Subtract(start_
).ToMicroseconds());
1992 EXPECT_EQ(GG_UINT64_C(0x0123456789ABD), iter
->first
);
1993 EXPECT_EQ(GG_INT64_C(0x07E1D2C3B4A59689),
1994 iter
->second
.Subtract(start_
).ToMicroseconds());
1996 // Now test framing boundaries
1997 for (size_t i
= kQuicFrameTypeSize
; i
< 31; ++i
) {
1998 string expected_error
;
2000 expected_error
= "Unable to read congestion feedback type.";
2002 expected_error
= "Unable to read accumulated number of lost packets.";
2004 expected_error
= "Unable to read num received packets.";
2005 } else if (i
< 11) {
2006 expected_error
= "Unable to read smallest received.";
2007 } else if (i
< 19) {
2008 expected_error
= "Unable to read time received.";
2009 } else if (i
< 21) {
2010 expected_error
= "Unable to read sequence delta in received packets.";
2011 } else if (i
< 25) {
2012 expected_error
= "Unable to read time delta in received packets.";
2013 } else if (i
< 27) {
2014 expected_error
= "Unable to read sequence delta in received packets.";
2015 } else if (i
< 31) {
2016 expected_error
= "Unable to read time delta in received packets.";
2018 CheckProcessingFails(
2020 i
+ GetPacketHeaderSize(PACKET_8BYTE_GUID
, !kIncludeVersion
,
2021 PACKET_6BYTE_SEQUENCE_NUMBER
, NOT_IN_FEC_GROUP
),
2022 expected_error
, QUIC_INVALID_CONGESTION_FEEDBACK_DATA
);
2026 TEST_P(QuicFramerTest
, CongestionFeedbackFrameFixRate
) {
2027 unsigned char packet
[] = {
2028 // public flags (8 byte guid)
2031 0x10, 0x32, 0x54, 0x76,
2032 0x98, 0xBA, 0xDC, 0xFE,
2033 // packet sequence number
2034 0xBC, 0x9A, 0x78, 0x56,
2039 // frame type (congestion feedback frame)
2041 // congestion feedback type (fix rate)
2043 // bitrate_in_bytes_per_second;
2044 0x01, 0x02, 0x03, 0x04,
2047 QuicEncryptedPacket
encrypted(AsChars(packet
), arraysize(packet
), false);
2048 EXPECT_TRUE(framer_
.ProcessPacket(encrypted
));
2050 EXPECT_EQ(QUIC_NO_ERROR
, framer_
.error());
2051 ASSERT_TRUE(visitor_
.header_
.get());
2052 EXPECT_TRUE(CheckDecryption(encrypted
, !kIncludeVersion
));
2054 EXPECT_EQ(0u, visitor_
.stream_frames_
.size());
2055 ASSERT_EQ(1u, visitor_
.congestion_feedback_frames_
.size());
2056 const QuicCongestionFeedbackFrame
& frame
=
2057 *visitor_
.congestion_feedback_frames_
[0];
2058 ASSERT_EQ(kFixRate
, frame
.type
);
2059 EXPECT_EQ(static_cast<uint32
>(0x04030201),
2060 frame
.fix_rate
.bitrate
.ToBytesPerSecond());
2062 // Now test framing boundaries
2063 for (size_t i
= kQuicFrameTypeSize
; i
< 6; ++i
) {
2064 string expected_error
;
2066 expected_error
= "Unable to read congestion feedback type.";
2068 expected_error
= "Unable to read bitrate.";
2070 CheckProcessingFails(
2072 i
+ GetPacketHeaderSize(PACKET_8BYTE_GUID
, !kIncludeVersion
,
2073 PACKET_6BYTE_SEQUENCE_NUMBER
, NOT_IN_FEC_GROUP
),
2074 expected_error
, QUIC_INVALID_CONGESTION_FEEDBACK_DATA
);
2078 TEST_P(QuicFramerTest
, CongestionFeedbackFrameInvalidFeedback
) {
2079 unsigned char packet
[] = {
2080 // public flags (8 byte guid)
2083 0x10, 0x32, 0x54, 0x76,
2084 0x98, 0xBA, 0xDC, 0xFE,
2085 // packet sequence number
2086 0xBC, 0x9A, 0x78, 0x56,
2091 // frame type (congestion feedback frame)
2093 // congestion feedback type (invalid)
2097 QuicEncryptedPacket
encrypted(AsChars(packet
), arraysize(packet
), false);
2098 EXPECT_FALSE(framer_
.ProcessPacket(encrypted
));
2099 EXPECT_TRUE(CheckDecryption(encrypted
, !kIncludeVersion
));
2100 EXPECT_EQ(QUIC_INVALID_CONGESTION_FEEDBACK_DATA
, framer_
.error());
2103 TEST_P(QuicFramerTest
, RstStreamFrame
) {
2104 unsigned char packet
[] = {
2105 // public flags (8 byte guid)
2108 0x10, 0x32, 0x54, 0x76,
2109 0x98, 0xBA, 0xDC, 0xFE,
2110 // packet sequence number
2111 0xBC, 0x9A, 0x78, 0x56,
2116 // frame type (rst stream frame)
2119 0x04, 0x03, 0x02, 0x01,
2121 0x01, 0x00, 0x00, 0x00,
2123 // error details length
2132 QuicEncryptedPacket
encrypted(AsChars(packet
), arraysize(packet
), false);
2133 EXPECT_TRUE(framer_
.ProcessPacket(encrypted
));
2135 EXPECT_EQ(QUIC_NO_ERROR
, framer_
.error());
2136 ASSERT_TRUE(visitor_
.header_
.get());
2137 EXPECT_TRUE(CheckDecryption(encrypted
, !kIncludeVersion
));
2139 EXPECT_EQ(GG_UINT64_C(0x01020304), visitor_
.rst_stream_frame_
.stream_id
);
2140 EXPECT_EQ(0x01, visitor_
.rst_stream_frame_
.error_code
);
2141 EXPECT_EQ("because I can", visitor_
.rst_stream_frame_
.error_details
);
2143 // Now test framing boundaries
2144 for (size_t i
= kQuicFrameTypeSize
; i
< 24; ++i
) {
2145 string expected_error
;
2146 if (i
< kQuicFrameTypeSize
+ kQuicMaxStreamIdSize
) {
2147 expected_error
= "Unable to read stream_id.";
2148 } else if (i
< kQuicFrameTypeSize
+ kQuicMaxStreamIdSize
+
2149 kQuicErrorCodeSize
) {
2150 expected_error
= "Unable to read rst stream error code.";
2152 expected_error
= "Unable to read rst stream error details.";
2154 CheckProcessingFails(
2156 i
+ GetPacketHeaderSize(PACKET_8BYTE_GUID
, !kIncludeVersion
,
2157 PACKET_6BYTE_SEQUENCE_NUMBER
, NOT_IN_FEC_GROUP
),
2158 expected_error
, QUIC_INVALID_RST_STREAM_DATA
);
2162 TEST_P(QuicFramerTest
, ConnectionCloseFrame
) {
2163 unsigned char packet
[] = {
2164 // public flags (8 byte guid)
2167 0x10, 0x32, 0x54, 0x76,
2168 0x98, 0xBA, 0xDC, 0xFE,
2169 // packet sequence number
2170 0xBC, 0x9A, 0x78, 0x56,
2175 // frame type (connection close frame)
2178 0x11, 0x00, 0x00, 0x00,
2180 // error details length
2189 QuicEncryptedPacket
encrypted(AsChars(packet
), arraysize(packet
), false);
2190 EXPECT_TRUE(framer_
.ProcessPacket(encrypted
));
2192 EXPECT_EQ(QUIC_NO_ERROR
, framer_
.error());
2193 ASSERT_TRUE(visitor_
.header_
.get());
2194 EXPECT_TRUE(CheckDecryption(encrypted
, !kIncludeVersion
));
2196 EXPECT_EQ(0u, visitor_
.stream_frames_
.size());
2198 EXPECT_EQ(0x11, visitor_
.connection_close_frame_
.error_code
);
2199 EXPECT_EQ("because I can", visitor_
.connection_close_frame_
.error_details
);
2201 ASSERT_EQ(0u, visitor_
.ack_frames_
.size());
2203 // Now test framing boundaries
2204 for (size_t i
= kQuicFrameTypeSize
;
2205 i
< QuicFramer::GetMinConnectionCloseFrameSize(); ++i
) {
2206 string expected_error
;
2207 if (i
< kQuicFrameTypeSize
+ kQuicErrorCodeSize
) {
2208 expected_error
= "Unable to read connection close error code.";
2210 expected_error
= "Unable to read connection close error details.";
2212 CheckProcessingFails(
2214 i
+ GetPacketHeaderSize(PACKET_8BYTE_GUID
, !kIncludeVersion
,
2215 PACKET_6BYTE_SEQUENCE_NUMBER
, NOT_IN_FEC_GROUP
),
2216 expected_error
, QUIC_INVALID_CONNECTION_CLOSE_DATA
);
2220 TEST_P(QuicFramerTest
, GoAwayFrame
) {
2221 unsigned char packet
[] = {
2222 // public flags (8 byte guid)
2225 0x10, 0x32, 0x54, 0x76,
2226 0x98, 0xBA, 0xDC, 0xFE,
2227 // packet sequence number
2228 0xBC, 0x9A, 0x78, 0x56,
2233 // frame type (go away frame)
2236 0x09, 0x00, 0x00, 0x00,
2238 0x04, 0x03, 0x02, 0x01,
2239 // error details length
2248 QuicEncryptedPacket
encrypted(AsChars(packet
), arraysize(packet
), false);
2249 EXPECT_TRUE(framer_
.ProcessPacket(encrypted
));
2251 EXPECT_EQ(QUIC_NO_ERROR
, framer_
.error());
2252 ASSERT_TRUE(visitor_
.header_
.get());
2253 EXPECT_TRUE(CheckDecryption(encrypted
, !kIncludeVersion
));
2255 EXPECT_EQ(GG_UINT64_C(0x01020304),
2256 visitor_
.goaway_frame_
.last_good_stream_id
);
2257 EXPECT_EQ(0x9, visitor_
.goaway_frame_
.error_code
);
2258 EXPECT_EQ("because I can", visitor_
.goaway_frame_
.reason_phrase
);
2260 const size_t reason_size
= arraysize("because I can") - 1;
2261 // Now test framing boundaries
2262 for (size_t i
= kQuicFrameTypeSize
;
2263 i
< QuicFramer::GetMinGoAwayFrameSize() + reason_size
; ++i
) {
2264 string expected_error
;
2265 if (i
< kQuicFrameTypeSize
+ kQuicErrorCodeSize
) {
2266 expected_error
= "Unable to read go away error code.";
2267 } else if (i
< kQuicFrameTypeSize
+ kQuicErrorCodeSize
+
2268 kQuicMaxStreamIdSize
) {
2269 expected_error
= "Unable to read last good stream id.";
2271 expected_error
= "Unable to read goaway reason.";
2273 CheckProcessingFails(
2275 i
+ GetPacketHeaderSize(PACKET_8BYTE_GUID
, !kIncludeVersion
,
2276 PACKET_6BYTE_SEQUENCE_NUMBER
, NOT_IN_FEC_GROUP
),
2277 expected_error
, QUIC_INVALID_GOAWAY_DATA
);
2281 TEST_P(QuicFramerTest
, PublicResetPacket
) {
2282 unsigned char packet
[] = {
2283 // public flags (public reset, 8 byte guid)
2286 0x10, 0x32, 0x54, 0x76,
2287 0x98, 0xBA, 0xDC, 0xFE,
2289 0x89, 0x67, 0x45, 0x23,
2290 0x01, 0xEF, 0xCD, 0xAB,
2291 // rejected sequence number
2292 0xBC, 0x9A, 0x78, 0x56,
2296 QuicEncryptedPacket
encrypted(AsChars(packet
), arraysize(packet
), false);
2297 EXPECT_TRUE(framer_
.ProcessPacket(encrypted
));
2298 ASSERT_EQ(QUIC_NO_ERROR
, framer_
.error());
2299 ASSERT_TRUE(visitor_
.public_reset_packet_
.get());
2300 EXPECT_EQ(GG_UINT64_C(0xFEDCBA9876543210),
2301 visitor_
.public_reset_packet_
->public_header
.guid
);
2302 EXPECT_TRUE(visitor_
.public_reset_packet_
->public_header
.reset_flag
);
2303 EXPECT_FALSE(visitor_
.public_reset_packet_
->public_header
.version_flag
);
2304 EXPECT_EQ(GG_UINT64_C(0xABCDEF0123456789),
2305 visitor_
.public_reset_packet_
->nonce_proof
);
2306 EXPECT_EQ(GG_UINT64_C(0x123456789ABC),
2307 visitor_
.public_reset_packet_
->rejected_sequence_number
);
2309 // Now test framing boundaries
2310 for (size_t i
= 0; i
< GetPublicResetPacketSize(); ++i
) {
2311 string expected_error
;
2312 DVLOG(1) << "iteration: " << i
;
2313 if (i
< kGuidOffset
) {
2314 expected_error
= "Unable to read public flags.";
2315 CheckProcessingFails(packet
, i
, expected_error
,
2316 QUIC_INVALID_PACKET_HEADER
);
2317 } else if (i
< kPublicResetPacketNonceProofOffset
) {
2318 expected_error
= "Unable to read GUID.";
2319 CheckProcessingFails(packet
, i
, expected_error
,
2320 QUIC_INVALID_PACKET_HEADER
);
2321 } else if (i
< kPublicResetPacketRejectedSequenceNumberOffset
) {
2322 expected_error
= "Unable to read nonce proof.";
2323 CheckProcessingFails(packet
, i
, expected_error
,
2324 QUIC_INVALID_PUBLIC_RST_PACKET
);
2326 expected_error
= "Unable to read rejected sequence number.";
2327 CheckProcessingFails(packet
, i
, expected_error
,
2328 QUIC_INVALID_PUBLIC_RST_PACKET
);
2333 TEST_P(QuicFramerTest
, VersionNegotiationPacket
) {
2334 unsigned char packet
[] = {
2335 // public flags (version, 8 byte guid)
2338 0x10, 0x32, 0x54, 0x76,
2339 0x98, 0xBA, 0xDC, 0xFE,
2341 'Q', '0', GetQuicVersionDigitTens(), GetQuicVersionDigitOnes(),
2345 QuicFramerPeer::SetIsServer(&framer_
, false);
2347 QuicEncryptedPacket
encrypted(AsChars(packet
), arraysize(packet
), false);
2348 EXPECT_TRUE(framer_
.ProcessPacket(encrypted
));
2349 ASSERT_EQ(QUIC_NO_ERROR
, framer_
.error());
2350 ASSERT_TRUE(visitor_
.version_negotiation_packet_
.get());
2351 EXPECT_EQ(2u, visitor_
.version_negotiation_packet_
->versions
.size());
2352 EXPECT_EQ(GetParam(), visitor_
.version_negotiation_packet_
->versions
[0]);
2354 for (size_t i
= 0; i
<= kPublicFlagsSize
+ PACKET_8BYTE_GUID
; ++i
) {
2355 string expected_error
;
2356 QuicErrorCode error_code
= QUIC_INVALID_PACKET_HEADER
;
2357 if (i
< kGuidOffset
) {
2358 expected_error
= "Unable to read public flags.";
2359 } else if (i
< kVersionOffset
) {
2360 expected_error
= "Unable to read GUID.";
2362 expected_error
= "Unable to read supported version in negotiation.";
2363 error_code
= QUIC_INVALID_VERSION_NEGOTIATION_PACKET
;
2365 CheckProcessingFails(packet
, i
, expected_error
, error_code
);
2369 TEST_P(QuicFramerTest
, FecPacket
) {
2370 unsigned char packet
[] = {
2371 // public flags (8 byte guid)
2374 0x10, 0x32, 0x54, 0x76,
2375 0x98, 0xBA, 0xDC, 0xFE,
2376 // packet sequence number
2377 0xBC, 0x9A, 0x78, 0x56,
2379 // private flags (fec group & FEC)
2381 // first fec protected packet offset
2391 QuicEncryptedPacket
encrypted(AsChars(packet
), arraysize(packet
), false);
2392 EXPECT_TRUE(framer_
.ProcessPacket(encrypted
));
2394 EXPECT_EQ(QUIC_NO_ERROR
, framer_
.error());
2395 ASSERT_TRUE(visitor_
.header_
.get());
2396 EXPECT_TRUE(CheckDecryption(encrypted
, !kIncludeVersion
));
2398 EXPECT_EQ(0u, visitor_
.stream_frames_
.size());
2399 EXPECT_EQ(0u, visitor_
.ack_frames_
.size());
2400 ASSERT_EQ(1, visitor_
.fec_count_
);
2401 const QuicFecData
& fec_data
= *visitor_
.fec_data_
[0];
2402 EXPECT_EQ(GG_UINT64_C(0x0123456789ABB), fec_data
.fec_group
);
2403 EXPECT_EQ("abcdefghijklmnop", fec_data
.redundancy
);
2406 TEST_P(QuicFramerTest
, BuildPaddingFramePacket
) {
2407 QuicPacketHeader header
;
2408 header
.public_header
.guid
= GG_UINT64_C(0xFEDCBA9876543210);
2409 header
.public_header
.reset_flag
= false;
2410 header
.public_header
.version_flag
= false;
2411 header
.fec_flag
= false;
2412 header
.entropy_flag
= false;
2413 header
.packet_sequence_number
= GG_UINT64_C(0x123456789ABC);
2414 header
.fec_group
= 0;
2416 QuicPaddingFrame padding_frame
;
2419 frames
.push_back(QuicFrame(&padding_frame
));
2421 unsigned char packet
[kMaxPacketSize
] = {
2422 // public flags (8 byte guid)
2425 0x10, 0x32, 0x54, 0x76,
2426 0x98, 0xBA, 0xDC, 0xFE,
2427 // packet sequence number
2428 0xBC, 0x9A, 0x78, 0x56,
2433 // frame type (padding frame)
2435 0x00, 0x00, 0x00, 0x00
2438 uint64 header_size
=
2439 GetPacketHeaderSize(PACKET_8BYTE_GUID
, !kIncludeVersion
,
2440 PACKET_6BYTE_SEQUENCE_NUMBER
, NOT_IN_FEC_GROUP
);
2441 memset(packet
+ header_size
+ 1, 0x00, kMaxPacketSize
- header_size
- 1);
2443 scoped_ptr
<QuicPacket
> data(
2444 framer_
.BuildUnsizedDataPacket(header
, frames
).packet
);
2445 ASSERT_TRUE(data
!= NULL
);
2447 test::CompareCharArraysWithHexError("constructed packet",
2448 data
->data(), data
->length(),
2453 TEST_P(QuicFramerTest
, Build4ByteSequenceNumberPaddingFramePacket
) {
2454 QuicPacketHeader header
;
2455 header
.public_header
.guid
= GG_UINT64_C(0xFEDCBA9876543210);
2456 header
.public_header
.reset_flag
= false;
2457 header
.public_header
.version_flag
= false;
2458 header
.fec_flag
= false;
2459 header
.entropy_flag
= false;
2460 header
.public_header
.sequence_number_length
= PACKET_4BYTE_SEQUENCE_NUMBER
;
2461 header
.packet_sequence_number
= GG_UINT64_C(0x123456789ABC);
2462 header
.fec_group
= 0;
2464 QuicPaddingFrame padding_frame
;
2467 frames
.push_back(QuicFrame(&padding_frame
));
2469 unsigned char packet
[kMaxPacketSize
] = {
2470 // public flags (8 byte guid and 4 byte sequence number)
2473 0x10, 0x32, 0x54, 0x76,
2474 0x98, 0xBA, 0xDC, 0xFE,
2475 // packet sequence number
2476 0xBC, 0x9A, 0x78, 0x56,
2480 // frame type (padding frame)
2482 0x00, 0x00, 0x00, 0x00
2485 uint64 header_size
=
2486 GetPacketHeaderSize(PACKET_8BYTE_GUID
, !kIncludeVersion
,
2487 PACKET_4BYTE_SEQUENCE_NUMBER
, NOT_IN_FEC_GROUP
);
2488 memset(packet
+ header_size
+ 1, 0x00, kMaxPacketSize
- header_size
- 1);
2490 scoped_ptr
<QuicPacket
> data(
2491 framer_
.BuildUnsizedDataPacket(header
, frames
).packet
);
2492 ASSERT_TRUE(data
!= NULL
);
2494 test::CompareCharArraysWithHexError("constructed packet",
2495 data
->data(), data
->length(),
2500 TEST_P(QuicFramerTest
, Build2ByteSequenceNumberPaddingFramePacket
) {
2501 QuicPacketHeader header
;
2502 header
.public_header
.guid
= GG_UINT64_C(0xFEDCBA9876543210);
2503 header
.public_header
.reset_flag
= false;
2504 header
.public_header
.version_flag
= false;
2505 header
.fec_flag
= false;
2506 header
.entropy_flag
= false;
2507 header
.public_header
.sequence_number_length
= PACKET_2BYTE_SEQUENCE_NUMBER
;
2508 header
.packet_sequence_number
= GG_UINT64_C(0x123456789ABC);
2509 header
.fec_group
= 0;
2511 QuicPaddingFrame padding_frame
;
2514 frames
.push_back(QuicFrame(&padding_frame
));
2516 unsigned char packet
[kMaxPacketSize
] = {
2517 // public flags (8 byte guid and 2 byte sequence number)
2520 0x10, 0x32, 0x54, 0x76,
2521 0x98, 0xBA, 0xDC, 0xFE,
2522 // packet sequence number
2527 // frame type (padding frame)
2529 0x00, 0x00, 0x00, 0x00
2532 uint64 header_size
=
2533 GetPacketHeaderSize(PACKET_8BYTE_GUID
, !kIncludeVersion
,
2534 PACKET_2BYTE_SEQUENCE_NUMBER
, NOT_IN_FEC_GROUP
);
2535 memset(packet
+ header_size
+ 1, 0x00, kMaxPacketSize
- header_size
- 1);
2537 scoped_ptr
<QuicPacket
> data(
2538 framer_
.BuildUnsizedDataPacket(header
, frames
).packet
);
2539 ASSERT_TRUE(data
!= NULL
);
2541 test::CompareCharArraysWithHexError("constructed packet",
2542 data
->data(), data
->length(),
2547 TEST_P(QuicFramerTest
, Build1ByteSequenceNumberPaddingFramePacket
) {
2548 QuicPacketHeader header
;
2549 header
.public_header
.guid
= GG_UINT64_C(0xFEDCBA9876543210);
2550 header
.public_header
.reset_flag
= false;
2551 header
.public_header
.version_flag
= false;
2552 header
.fec_flag
= false;
2553 header
.entropy_flag
= false;
2554 header
.public_header
.sequence_number_length
= PACKET_1BYTE_SEQUENCE_NUMBER
;
2555 header
.packet_sequence_number
= GG_UINT64_C(0x123456789ABC);
2556 header
.fec_group
= 0;
2558 QuicPaddingFrame padding_frame
;
2561 frames
.push_back(QuicFrame(&padding_frame
));
2563 unsigned char packet
[kMaxPacketSize
] = {
2564 // public flags (8 byte guid and 1 byte sequence number)
2567 0x10, 0x32, 0x54, 0x76,
2568 0x98, 0xBA, 0xDC, 0xFE,
2569 // packet sequence number
2574 // frame type (padding frame)
2576 0x00, 0x00, 0x00, 0x00
2579 uint64 header_size
=
2580 GetPacketHeaderSize(PACKET_8BYTE_GUID
, !kIncludeVersion
,
2581 PACKET_1BYTE_SEQUENCE_NUMBER
, NOT_IN_FEC_GROUP
);
2582 memset(packet
+ header_size
+ 1, 0x00, kMaxPacketSize
- header_size
- 1);
2584 scoped_ptr
<QuicPacket
> data(
2585 framer_
.BuildUnsizedDataPacket(header
, frames
).packet
);
2586 ASSERT_TRUE(data
!= NULL
);
2588 test::CompareCharArraysWithHexError("constructed packet",
2589 data
->data(), data
->length(),
2594 TEST_P(QuicFramerTest
, BuildStreamFramePacket
) {
2595 QuicPacketHeader header
;
2596 header
.public_header
.guid
= GG_UINT64_C(0xFEDCBA9876543210);
2597 header
.public_header
.reset_flag
= false;
2598 header
.public_header
.version_flag
= false;
2599 header
.fec_flag
= false;
2600 header
.entropy_flag
= true;
2601 header
.packet_sequence_number
= GG_UINT64_C(0x77123456789ABC);
2602 header
.fec_group
= 0;
2604 QuicStreamFrame stream_frame
;
2605 stream_frame
.stream_id
= 0x01020304;
2606 stream_frame
.fin
= true;
2607 stream_frame
.offset
= GG_UINT64_C(0xBA98FEDC32107654);
2608 stream_frame
.data
= MakeIOVector("hello world!");
2611 frames
.push_back(QuicFrame(&stream_frame
));
2613 unsigned char packet
[] = {
2614 // public flags (8 byte guid)
2617 0x10, 0x32, 0x54, 0x76,
2618 0x98, 0xBA, 0xDC, 0xFE,
2619 // packet sequence number
2620 0xBC, 0x9A, 0x78, 0x56,
2622 // private flags (entropy)
2625 // frame type (stream frame with fin and no length)
2628 0x04, 0x03, 0x02, 0x01,
2630 0x54, 0x76, 0x10, 0x32,
2631 0xDC, 0xFE, 0x98, 0xBA,
2638 scoped_ptr
<QuicPacket
> data(
2639 framer_
.BuildUnsizedDataPacket(header
, frames
).packet
);
2640 ASSERT_TRUE(data
!= NULL
);
2642 test::CompareCharArraysWithHexError("constructed packet",
2643 data
->data(), data
->length(),
2644 AsChars(packet
), arraysize(packet
));
2647 TEST_P(QuicFramerTest
, BuildStreamFramePacketWithVersionFlag
) {
2648 QuicPacketHeader header
;
2649 header
.public_header
.guid
= GG_UINT64_C(0xFEDCBA9876543210);
2650 header
.public_header
.reset_flag
= false;
2651 header
.public_header
.version_flag
= true;
2652 header
.fec_flag
= false;
2653 header
.entropy_flag
= true;
2654 header
.packet_sequence_number
= GG_UINT64_C(0x77123456789ABC);
2655 header
.fec_group
= 0;
2657 QuicStreamFrame stream_frame
;
2658 stream_frame
.stream_id
= 0x01020304;
2659 stream_frame
.fin
= true;
2660 stream_frame
.offset
= GG_UINT64_C(0xBA98FEDC32107654);
2661 stream_frame
.data
= MakeIOVector("hello world!");
2664 frames
.push_back(QuicFrame(&stream_frame
));
2666 unsigned char packet
[] = {
2667 // public flags (version, 8 byte guid)
2670 0x10, 0x32, 0x54, 0x76,
2671 0x98, 0xBA, 0xDC, 0xFE,
2673 'Q', '0', GetQuicVersionDigitTens(), GetQuicVersionDigitOnes(),
2674 // packet sequence number
2675 0xBC, 0x9A, 0x78, 0x56,
2677 // private flags (entropy)
2680 // frame type (stream frame with fin and no length)
2683 0x04, 0x03, 0x02, 0x01,
2685 0x54, 0x76, 0x10, 0x32,
2686 0xDC, 0xFE, 0x98, 0xBA,
2693 QuicFramerPeer::SetIsServer(&framer_
, false);
2694 scoped_ptr
<QuicPacket
> data(
2695 framer_
.BuildUnsizedDataPacket(header
, frames
).packet
);
2696 ASSERT_TRUE(data
!= NULL
);
2698 test::CompareCharArraysWithHexError("constructed packet",
2699 data
->data(), data
->length(),
2700 AsChars(packet
), arraysize(packet
));
2703 TEST_P(QuicFramerTest
, BuildVersionNegotiationPacket
) {
2704 QuicPacketPublicHeader header
;
2705 header
.guid
= GG_UINT64_C(0xFEDCBA9876543210);
2706 header
.reset_flag
= false;
2707 header
.version_flag
= true;
2709 unsigned char packet
[] = {
2710 // public flags (version, 8 byte guid)
2713 0x10, 0x32, 0x54, 0x76,
2714 0x98, 0xBA, 0xDC, 0xFE,
2716 'Q', '0', GetQuicVersionDigitTens(), GetQuicVersionDigitOnes(),
2719 QuicVersionVector versions
;
2720 versions
.push_back(GetParam());
2721 scoped_ptr
<QuicEncryptedPacket
> data(
2722 framer_
.BuildVersionNegotiationPacket(header
, versions
));
2724 test::CompareCharArraysWithHexError("constructed packet",
2725 data
->data(), data
->length(),
2726 AsChars(packet
), arraysize(packet
));
2729 TEST_P(QuicFramerTest
, BuildAckFramePacket
) {
2730 QuicPacketHeader header
;
2731 header
.public_header
.guid
= 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
= true;
2736 header
.packet_sequence_number
= GG_UINT64_C(0x770123456789AA8);
2737 header
.fec_group
= 0;
2739 QuicAckFrame ack_frame
;
2740 ack_frame
.received_info
.entropy_hash
= 0x43;
2741 ack_frame
.received_info
.largest_observed
= GG_UINT64_C(0x770123456789ABF);
2742 ack_frame
.received_info
.delta_time_largest_observed
= QuicTime::Delta::Zero();
2743 ack_frame
.received_info
.missing_packets
.insert(
2744 GG_UINT64_C(0x770123456789ABE));
2745 ack_frame
.sent_info
.entropy_hash
= 0x14;
2746 ack_frame
.sent_info
.least_unacked
= GG_UINT64_C(0x770123456789AA0);
2749 frames
.push_back(QuicFrame(&ack_frame
));
2751 unsigned char packet
[] = {
2752 // public flags (8 byte guid)
2755 0x10, 0x32, 0x54, 0x76,
2756 0x98, 0xBA, 0xDC, 0xFE,
2757 // packet sequence number
2758 0xA8, 0x9A, 0x78, 0x56,
2760 // private flags (entropy)
2763 // frame type (ack frame)
2764 // (has nacks, not truncated, 6 byte largest observed, 1 byte delta)
2766 // entropy hash of sent packets till least awaiting - 1.
2768 // least packet sequence number awaiting an ack, delta from sequence number.
2769 0x08, 0x00, 0x00, 0x00,
2771 // entropy hash of all received packets.
2773 // largest observed packet sequence number
2774 0xBF, 0x9A, 0x78, 0x56,
2778 // num missing packet ranges
2780 // missing packet delta
2782 // 0 more missing packets in range.
2786 scoped_ptr
<QuicPacket
> data(
2787 framer_
.BuildUnsizedDataPacket(header
, frames
).packet
);
2788 ASSERT_TRUE(data
!= NULL
);
2790 test::CompareCharArraysWithHexError("constructed packet",
2791 data
->data(), data
->length(),
2792 AsChars(packet
), arraysize(packet
));
2795 TEST_P(QuicFramerTest
, BuildCongestionFeedbackFramePacketTCP
) {
2796 QuicPacketHeader header
;
2797 header
.public_header
.guid
= GG_UINT64_C(0xFEDCBA9876543210);
2798 header
.public_header
.reset_flag
= false;
2799 header
.public_header
.version_flag
= false;
2800 header
.fec_flag
= false;
2801 header
.entropy_flag
= false;
2802 header
.packet_sequence_number
= GG_UINT64_C(0x123456789ABC);
2803 header
.fec_group
= 0;
2805 QuicCongestionFeedbackFrame congestion_feedback_frame
;
2806 congestion_feedback_frame
.type
= kTCP
;
2807 congestion_feedback_frame
.tcp
.accumulated_number_of_lost_packets
= 0x0201;
2808 congestion_feedback_frame
.tcp
.receive_window
= 0x4030;
2811 frames
.push_back(QuicFrame(&congestion_feedback_frame
));
2813 unsigned char packet
[] = {
2814 // public flags (8 byte guid)
2817 0x10, 0x32, 0x54, 0x76,
2818 0x98, 0xBA, 0xDC, 0xFE,
2819 // packet sequence number
2820 0xBC, 0x9A, 0x78, 0x56,
2825 // frame type (congestion feedback frame)
2827 // congestion feedback type (TCP)
2829 // accumulated number of lost packets
2831 // TCP receive window
2835 scoped_ptr
<QuicPacket
> data(
2836 framer_
.BuildUnsizedDataPacket(header
, frames
).packet
);
2837 ASSERT_TRUE(data
!= NULL
);
2839 test::CompareCharArraysWithHexError("constructed packet",
2840 data
->data(), data
->length(),
2841 AsChars(packet
), arraysize(packet
));
2844 TEST_P(QuicFramerTest
, BuildCongestionFeedbackFramePacketInterArrival
) {
2845 QuicPacketHeader header
;
2846 header
.public_header
.guid
= GG_UINT64_C(0xFEDCBA9876543210);
2847 header
.public_header
.reset_flag
= false;
2848 header
.public_header
.version_flag
= false;
2849 header
.fec_flag
= false;
2850 header
.entropy_flag
= false;
2851 header
.packet_sequence_number
= GG_UINT64_C(0x123456789ABC);
2852 header
.fec_group
= 0;
2854 QuicCongestionFeedbackFrame frame
;
2855 frame
.type
= kInterArrival
;
2856 frame
.inter_arrival
.accumulated_number_of_lost_packets
= 0x0302;
2857 frame
.inter_arrival
.received_packet_times
.insert(
2858 make_pair(GG_UINT64_C(0x0123456789ABA),
2859 start_
.Add(QuicTime::Delta::FromMicroseconds(
2860 GG_UINT64_C(0x07E1D2C3B4A59687)))));
2861 frame
.inter_arrival
.received_packet_times
.insert(
2862 make_pair(GG_UINT64_C(0x0123456789ABB),
2863 start_
.Add(QuicTime::Delta::FromMicroseconds(
2864 GG_UINT64_C(0x07E1D2C3B4A59688)))));
2865 frame
.inter_arrival
.received_packet_times
.insert(
2866 make_pair(GG_UINT64_C(0x0123456789ABD),
2867 start_
.Add(QuicTime::Delta::FromMicroseconds(
2868 GG_UINT64_C(0x07E1D2C3B4A59689)))));
2870 frames
.push_back(QuicFrame(&frame
));
2872 unsigned char packet
[] = {
2873 // public flags (8 byte guid)
2876 0x10, 0x32, 0x54, 0x76,
2877 0x98, 0xBA, 0xDC, 0xFE,
2878 // packet sequence number
2879 0xBC, 0x9A, 0x78, 0x56,
2884 // frame type (congestion feedback frame)
2886 // congestion feedback type (inter arrival)
2888 // accumulated_number_of_lost_packets
2890 // num received packets
2892 // lowest sequence number
2893 0xBA, 0x9A, 0x78, 0x56,
2896 0x87, 0x96, 0xA5, 0xB4,
2897 0xC3, 0xD2, 0xE1, 0x07,
2901 0x01, 0x00, 0x00, 0x00,
2902 // sequence delta (skip one packet)
2905 0x02, 0x00, 0x00, 0x00,
2908 scoped_ptr
<QuicPacket
> data(
2909 framer_
.BuildUnsizedDataPacket(header
, frames
).packet
);
2910 ASSERT_TRUE(data
!= NULL
);
2912 test::CompareCharArraysWithHexError("constructed packet",
2913 data
->data(), data
->length(),
2914 AsChars(packet
), arraysize(packet
));
2917 TEST_P(QuicFramerTest
, BuildCongestionFeedbackFramePacketFixRate
) {
2918 QuicPacketHeader header
;
2919 header
.public_header
.guid
= GG_UINT64_C(0xFEDCBA9876543210);
2920 header
.public_header
.reset_flag
= false;
2921 header
.public_header
.version_flag
= false;
2922 header
.fec_flag
= false;
2923 header
.entropy_flag
= false;
2924 header
.packet_sequence_number
= GG_UINT64_C(0x123456789ABC);
2925 header
.fec_group
= 0;
2927 QuicCongestionFeedbackFrame congestion_feedback_frame
;
2928 congestion_feedback_frame
.type
= kFixRate
;
2929 congestion_feedback_frame
.fix_rate
.bitrate
2930 = QuicBandwidth::FromBytesPerSecond(0x04030201);
2933 frames
.push_back(QuicFrame(&congestion_feedback_frame
));
2935 unsigned char packet
[] = {
2936 // public flags (8 byte guid)
2939 0x10, 0x32, 0x54, 0x76,
2940 0x98, 0xBA, 0xDC, 0xFE,
2941 // packet sequence number
2942 0xBC, 0x9A, 0x78, 0x56,
2947 // frame type (congestion feedback frame)
2949 // congestion feedback type (fix rate)
2951 // bitrate_in_bytes_per_second;
2952 0x01, 0x02, 0x03, 0x04,
2955 scoped_ptr
<QuicPacket
> data(
2956 framer_
.BuildUnsizedDataPacket(header
, frames
).packet
);
2957 ASSERT_TRUE(data
!= NULL
);
2959 test::CompareCharArraysWithHexError("constructed packet",
2960 data
->data(), data
->length(),
2961 AsChars(packet
), arraysize(packet
));
2964 TEST_P(QuicFramerTest
, BuildCongestionFeedbackFramePacketInvalidFeedback
) {
2965 QuicPacketHeader header
;
2966 header
.public_header
.guid
= GG_UINT64_C(0xFEDCBA9876543210);
2967 header
.public_header
.reset_flag
= false;
2968 header
.public_header
.version_flag
= false;
2969 header
.fec_flag
= false;
2970 header
.entropy_flag
= false;
2971 header
.packet_sequence_number
= GG_UINT64_C(0x123456789ABC);
2972 header
.fec_group
= 0;
2974 QuicCongestionFeedbackFrame congestion_feedback_frame
;
2975 congestion_feedback_frame
.type
=
2976 static_cast<CongestionFeedbackType
>(kFixRate
+ 1);
2979 frames
.push_back(QuicFrame(&congestion_feedback_frame
));
2981 scoped_ptr
<QuicPacket
> data(
2982 framer_
.BuildUnsizedDataPacket(header
, frames
).packet
);
2983 ASSERT_TRUE(data
== NULL
);
2986 TEST_P(QuicFramerTest
, BuildRstFramePacket
) {
2987 QuicPacketHeader header
;
2988 header
.public_header
.guid
= GG_UINT64_C(0xFEDCBA9876543210);
2989 header
.public_header
.reset_flag
= false;
2990 header
.public_header
.version_flag
= false;
2991 header
.fec_flag
= false;
2992 header
.entropy_flag
= false;
2993 header
.packet_sequence_number
= GG_UINT64_C(0x123456789ABC);
2994 header
.fec_group
= 0;
2996 QuicRstStreamFrame rst_frame
;
2997 rst_frame
.stream_id
= 0x01020304;
2998 rst_frame
.error_code
= static_cast<QuicRstStreamErrorCode
>(0x05060708);
2999 rst_frame
.error_details
= "because I can";
3001 unsigned char packet
[] = {
3002 // public flags (8 byte guid)
3005 0x10, 0x32, 0x54, 0x76,
3006 0x98, 0xBA, 0xDC, 0xFE,
3007 // packet sequence number
3008 0xBC, 0x9A, 0x78, 0x56,
3013 // frame type (rst stream frame)
3016 0x04, 0x03, 0x02, 0x01,
3018 0x08, 0x07, 0x06, 0x05,
3019 // error details length
3029 frames
.push_back(QuicFrame(&rst_frame
));
3031 scoped_ptr
<QuicPacket
> data(
3032 framer_
.BuildUnsizedDataPacket(header
, frames
).packet
);
3033 ASSERT_TRUE(data
!= NULL
);
3035 test::CompareCharArraysWithHexError("constructed packet",
3036 data
->data(), data
->length(),
3037 AsChars(packet
), arraysize(packet
));
3040 TEST_P(QuicFramerTest
, BuildCloseFramePacket
) {
3041 QuicPacketHeader header
;
3042 header
.public_header
.guid
= GG_UINT64_C(0xFEDCBA9876543210);
3043 header
.public_header
.reset_flag
= false;
3044 header
.public_header
.version_flag
= false;
3045 header
.fec_flag
= false;
3046 header
.entropy_flag
= true;
3047 header
.packet_sequence_number
= GG_UINT64_C(0x123456789ABC);
3048 header
.fec_group
= 0;
3050 QuicConnectionCloseFrame close_frame
;
3051 close_frame
.error_code
= static_cast<QuicErrorCode
>(0x05060708);
3052 close_frame
.error_details
= "because I can";
3055 frames
.push_back(QuicFrame(&close_frame
));
3057 unsigned char packet
[] = {
3058 // public flags (8 byte guid)
3061 0x10, 0x32, 0x54, 0x76,
3062 0x98, 0xBA, 0xDC, 0xFE,
3063 // packet sequence number
3064 0xBC, 0x9A, 0x78, 0x56,
3066 // private flags (entropy)
3069 // frame type (connection close frame)
3072 0x08, 0x07, 0x06, 0x05,
3073 // error details length
3082 scoped_ptr
<QuicPacket
> data(
3083 framer_
.BuildUnsizedDataPacket(header
, frames
).packet
);
3084 ASSERT_TRUE(data
!= NULL
);
3086 test::CompareCharArraysWithHexError("constructed packet",
3087 data
->data(), data
->length(),
3088 AsChars(packet
), arraysize(packet
));
3091 TEST_P(QuicFramerTest
, BuildGoAwayPacket
) {
3092 QuicPacketHeader header
;
3093 header
.public_header
.guid
= GG_UINT64_C(0xFEDCBA9876543210);
3094 header
.public_header
.reset_flag
= false;
3095 header
.public_header
.version_flag
= false;
3096 header
.fec_flag
= false;
3097 header
.entropy_flag
= true;
3098 header
.packet_sequence_number
= GG_UINT64_C(0x123456789ABC);
3099 header
.fec_group
= 0;
3101 QuicGoAwayFrame goaway_frame
;
3102 goaway_frame
.error_code
= static_cast<QuicErrorCode
>(0x05060708);
3103 goaway_frame
.last_good_stream_id
= 0x01020304;
3104 goaway_frame
.reason_phrase
= "because I can";
3107 frames
.push_back(QuicFrame(&goaway_frame
));
3109 unsigned char packet
[] = {
3110 // public flags (8 byte guid)
3113 0x10, 0x32, 0x54, 0x76,
3114 0x98, 0xBA, 0xDC, 0xFE,
3115 // packet sequence number
3116 0xBC, 0x9A, 0x78, 0x56,
3118 // private flags(entropy)
3121 // frame type (go away frame)
3124 0x08, 0x07, 0x06, 0x05,
3126 0x04, 0x03, 0x02, 0x01,
3127 // error details length
3136 scoped_ptr
<QuicPacket
> data(
3137 framer_
.BuildUnsizedDataPacket(header
, frames
).packet
);
3138 ASSERT_TRUE(data
!= NULL
);
3140 test::CompareCharArraysWithHexError("constructed packet",
3141 data
->data(), data
->length(),
3142 AsChars(packet
), arraysize(packet
));
3145 TEST_P(QuicFramerTest
, BuildPublicResetPacket
) {
3146 QuicPublicResetPacket reset_packet
;
3147 reset_packet
.public_header
.guid
= GG_UINT64_C(0xFEDCBA9876543210);
3148 reset_packet
.public_header
.reset_flag
= true;
3149 reset_packet
.public_header
.version_flag
= false;
3150 reset_packet
.rejected_sequence_number
= GG_UINT64_C(0x123456789ABC);
3151 reset_packet
.nonce_proof
= GG_UINT64_C(0xABCDEF0123456789);
3153 unsigned char packet
[] = {
3154 // public flags (public reset, 8 byte GUID)
3157 0x10, 0x32, 0x54, 0x76,
3158 0x98, 0xBA, 0xDC, 0xFE,
3160 0x89, 0x67, 0x45, 0x23,
3161 0x01, 0xEF, 0xCD, 0xAB,
3162 // rejected sequence number
3163 0xBC, 0x9A, 0x78, 0x56,
3167 scoped_ptr
<QuicEncryptedPacket
> data(
3168 framer_
.BuildPublicResetPacket(reset_packet
));
3169 ASSERT_TRUE(data
!= NULL
);
3171 test::CompareCharArraysWithHexError("constructed packet",
3172 data
->data(), data
->length(),
3173 AsChars(packet
), arraysize(packet
));
3176 TEST_P(QuicFramerTest
, BuildFecPacket
) {
3177 QuicPacketHeader header
;
3178 header
.public_header
.guid
= GG_UINT64_C(0xFEDCBA9876543210);
3179 header
.public_header
.reset_flag
= false;
3180 header
.public_header
.version_flag
= false;
3181 header
.fec_flag
= true;
3182 header
.entropy_flag
= true;
3183 header
.packet_sequence_number
= (GG_UINT64_C(0x123456789ABC));
3184 header
.is_in_fec_group
= IN_FEC_GROUP
;
3185 header
.fec_group
= GG_UINT64_C(0x123456789ABB);;
3187 QuicFecData fec_data
;
3188 fec_data
.fec_group
= 1;
3189 fec_data
.redundancy
= "abcdefghijklmnop";
3191 unsigned char packet
[] = {
3192 // public flags (8 byte guid)
3195 0x10, 0x32, 0x54, 0x76,
3196 0x98, 0xBA, 0xDC, 0xFE,
3197 // packet sequence number
3198 0xBC, 0x9A, 0x78, 0x56,
3200 // private flags (entropy & fec group & fec packet)
3202 // first fec protected packet offset
3212 scoped_ptr
<QuicPacket
> data(
3213 framer_
.BuildFecPacket(header
, fec_data
).packet
);
3214 ASSERT_TRUE(data
!= NULL
);
3216 test::CompareCharArraysWithHexError("constructed packet",
3217 data
->data(), data
->length(),
3218 AsChars(packet
), arraysize(packet
));
3221 TEST_P(QuicFramerTest
, EncryptPacket
) {
3222 QuicPacketSequenceNumber sequence_number
= GG_UINT64_C(0x123456789ABC);
3223 unsigned char packet
[] = {
3224 // public flags (8 byte guid)
3227 0x10, 0x32, 0x54, 0x76,
3228 0x98, 0xBA, 0xDC, 0xFE,
3229 // packet sequence number
3230 0xBC, 0x9A, 0x78, 0x56,
3232 // private flags (fec group & fec packet)
3234 // first fec protected packet offset
3244 scoped_ptr
<QuicPacket
> raw(
3245 QuicPacket::NewDataPacket(AsChars(packet
), arraysize(packet
), false,
3246 PACKET_8BYTE_GUID
, !kIncludeVersion
,
3247 PACKET_6BYTE_SEQUENCE_NUMBER
));
3248 scoped_ptr
<QuicEncryptedPacket
> encrypted(
3249 framer_
.EncryptPacket(ENCRYPTION_NONE
, sequence_number
, *raw
));
3251 ASSERT_TRUE(encrypted
.get() != NULL
);
3252 EXPECT_TRUE(CheckEncryption(sequence_number
, raw
.get()));
3255 TEST_P(QuicFramerTest
, EncryptPacketWithVersionFlag
) {
3256 QuicPacketSequenceNumber sequence_number
= GG_UINT64_C(0x123456789ABC);
3257 unsigned char packet
[] = {
3258 // public flags (version, 8 byte guid)
3261 0x10, 0x32, 0x54, 0x76,
3262 0x98, 0xBA, 0xDC, 0xFE,
3265 // packet sequence number
3266 0xBC, 0x9A, 0x78, 0x56,
3268 // private flags (fec group & fec flags)
3270 // first fec protected packet offset
3280 scoped_ptr
<QuicPacket
> raw(
3281 QuicPacket::NewDataPacket(AsChars(packet
), arraysize(packet
), false,
3282 PACKET_8BYTE_GUID
, kIncludeVersion
,
3283 PACKET_6BYTE_SEQUENCE_NUMBER
));
3284 scoped_ptr
<QuicEncryptedPacket
> encrypted(
3285 framer_
.EncryptPacket(ENCRYPTION_NONE
, sequence_number
, *raw
));
3287 ASSERT_TRUE(encrypted
.get() != NULL
);
3288 EXPECT_TRUE(CheckEncryption(sequence_number
, raw
.get()));
3291 TEST_P(QuicFramerTest
, Truncation
) {
3292 QuicPacketHeader header
;
3293 header
.public_header
.guid
= GG_UINT64_C(0xFEDCBA9876543210);
3294 header
.public_header
.reset_flag
= false;
3295 header
.public_header
.version_flag
= false;
3296 header
.fec_flag
= false;
3297 header
.entropy_flag
= false;
3298 header
.packet_sequence_number
= GG_UINT64_C(0x123456789ABC);
3299 header
.fec_group
= 0;
3301 QuicAckFrame ack_frame
;
3302 ack_frame
.received_info
.largest_observed
= 601;
3303 ack_frame
.sent_info
.least_unacked
= 0;
3304 for (uint64 i
= 1; i
< ack_frame
.received_info
.largest_observed
; i
+= 2) {
3305 ack_frame
.received_info
.missing_packets
.insert(i
);
3308 // Create a packet with just the ack
3310 frame
.type
= ACK_FRAME
;
3311 frame
.ack_frame
= &ack_frame
;
3313 frames
.push_back(frame
);
3315 scoped_ptr
<QuicPacket
> raw_ack_packet(
3316 framer_
.BuildUnsizedDataPacket(header
, frames
).packet
);
3317 ASSERT_TRUE(raw_ack_packet
!= NULL
);
3319 scoped_ptr
<QuicEncryptedPacket
> ack_packet(
3320 framer_
.EncryptPacket(ENCRYPTION_NONE
, header
.packet_sequence_number
,
3323 // Now make sure we can turn our ack packet back into an ack frame
3324 ASSERT_TRUE(framer_
.ProcessPacket(*ack_packet
));
3325 ASSERT_EQ(1u, visitor_
.ack_frames_
.size());
3326 const QuicAckFrame
& processed_ack_frame
= *visitor_
.ack_frames_
[0];
3327 EXPECT_EQ(0u, processed_ack_frame
.sent_info
.least_unacked
);
3328 EXPECT_TRUE(processed_ack_frame
.received_info
.is_truncated
);
3329 EXPECT_EQ(510u, processed_ack_frame
.received_info
.largest_observed
);
3330 ASSERT_EQ(255u, processed_ack_frame
.received_info
.missing_packets
.size());
3331 SequenceNumberSet::const_iterator missing_iter
=
3332 processed_ack_frame
.received_info
.missing_packets
.begin();
3333 EXPECT_EQ(1u, *missing_iter
);
3334 SequenceNumberSet::const_reverse_iterator last_missing_iter
=
3335 processed_ack_frame
.received_info
.missing_packets
.rbegin();
3336 EXPECT_EQ(509u, *last_missing_iter
);
3339 TEST_P(QuicFramerTest
, CleanTruncation
) {
3340 QuicPacketHeader header
;
3341 header
.public_header
.guid
= GG_UINT64_C(0xFEDCBA9876543210);
3342 header
.public_header
.reset_flag
= false;
3343 header
.public_header
.version_flag
= false;
3344 header
.fec_flag
= false;
3345 header
.entropy_flag
= true;
3346 header
.packet_sequence_number
= GG_UINT64_C(0x123456789ABC);
3347 header
.fec_group
= 0;
3349 QuicAckFrame ack_frame
;
3350 ack_frame
.received_info
.largest_observed
= 201;
3351 ack_frame
.sent_info
.least_unacked
= 0;
3352 for (uint64 i
= 1; i
< ack_frame
.received_info
.largest_observed
; ++i
) {
3353 ack_frame
.received_info
.missing_packets
.insert(i
);
3356 // Create a packet with just the ack
3358 frame
.type
= ACK_FRAME
;
3359 frame
.ack_frame
= &ack_frame
;
3361 frames
.push_back(frame
);
3363 scoped_ptr
<QuicPacket
> raw_ack_packet(
3364 framer_
.BuildUnsizedDataPacket(header
, frames
).packet
);
3365 ASSERT_TRUE(raw_ack_packet
!= NULL
);
3367 scoped_ptr
<QuicEncryptedPacket
> ack_packet(
3368 framer_
.EncryptPacket(ENCRYPTION_NONE
, header
.packet_sequence_number
,
3371 // Now make sure we can turn our ack packet back into an ack frame
3372 ASSERT_TRUE(framer_
.ProcessPacket(*ack_packet
));
3374 // Test for clean truncation of the ack by comparing the length of the
3375 // original packets to the re-serialized packets.
3377 frame
.type
= ACK_FRAME
;
3378 frame
.ack_frame
= visitor_
.ack_frames_
[0];
3379 frames
.push_back(frame
);
3381 size_t original_raw_length
= raw_ack_packet
->length();
3382 raw_ack_packet
.reset(
3383 framer_
.BuildUnsizedDataPacket(header
, frames
).packet
);
3384 ASSERT_TRUE(raw_ack_packet
!= NULL
);
3385 EXPECT_EQ(original_raw_length
, raw_ack_packet
->length());
3386 ASSERT_TRUE(raw_ack_packet
!= NULL
);
3389 TEST_P(QuicFramerTest
, EntropyFlagTest
) {
3390 unsigned char packet
[] = {
3391 // public flags (8 byte guid)
3394 0x10, 0x32, 0x54, 0x76,
3395 0x98, 0xBA, 0xDC, 0xFE,
3396 // packet sequence number
3397 0xBC, 0x9A, 0x78, 0x56,
3399 // private flags (Entropy)
3402 // frame type (stream frame with fin and no length)
3405 0x04, 0x03, 0x02, 0x01,
3407 0x54, 0x76, 0x10, 0x32,
3408 0xDC, 0xFE, 0x98, 0xBA,
3415 QuicEncryptedPacket
encrypted(AsChars(packet
), arraysize(packet
), false);
3416 EXPECT_TRUE(framer_
.ProcessPacket(encrypted
));
3417 EXPECT_EQ(QUIC_NO_ERROR
, framer_
.error());
3418 ASSERT_TRUE(visitor_
.header_
.get());
3419 EXPECT_TRUE(visitor_
.header_
->entropy_flag
);
3420 EXPECT_EQ(1 << 4, visitor_
.header_
->entropy_hash
);
3421 EXPECT_FALSE(visitor_
.header_
->fec_flag
);
3424 TEST_P(QuicFramerTest
, FecEntropyTest
) {
3425 unsigned char packet
[] = {
3426 // public flags (8 byte guid)
3429 0x10, 0x32, 0x54, 0x76,
3430 0x98, 0xBA, 0xDC, 0xFE,
3431 // packet sequence number
3432 0xBC, 0x9A, 0x78, 0x56,
3434 // private flags (Entropy & fec group & FEC)
3436 // first fec protected packet offset
3439 // frame type (stream frame with fin and no length)
3442 0x04, 0x03, 0x02, 0x01,
3444 0x54, 0x76, 0x10, 0x32,
3445 0xDC, 0xFE, 0x98, 0xBA,
3452 QuicEncryptedPacket
encrypted(AsChars(packet
), arraysize(packet
), false);
3453 EXPECT_TRUE(framer_
.ProcessPacket(encrypted
));
3454 EXPECT_EQ(QUIC_NO_ERROR
, framer_
.error());
3455 ASSERT_TRUE(visitor_
.header_
.get());
3456 EXPECT_TRUE(visitor_
.header_
->fec_flag
);
3457 EXPECT_TRUE(visitor_
.header_
->entropy_flag
);
3458 EXPECT_EQ(1 << 4, visitor_
.header_
->entropy_hash
);
3461 TEST_P(QuicFramerTest
, StopPacketProcessing
) {
3462 unsigned char packet
[] = {
3463 // public flags (8 byte guid)
3466 0x10, 0x32, 0x54, 0x76,
3467 0x98, 0xBA, 0xDC, 0xFE,
3468 // packet sequence number
3469 0xBC, 0x9A, 0x78, 0x56,
3474 // frame type (stream frame with fin)
3477 0x04, 0x03, 0x02, 0x01,
3479 0x54, 0x76, 0x10, 0x32,
3480 0xDC, 0xFE, 0x98, 0xBA,
3488 // frame type (ack frame)
3490 // entropy hash of sent packets till least awaiting - 1.
3492 // least packet sequence number awaiting an ack
3493 0xA0, 0x9A, 0x78, 0x56,
3495 // entropy hash of all received packets.
3497 // largest observed packet sequence number
3498 0xBF, 0x9A, 0x78, 0x56,
3500 // num missing packets
3503 0xBE, 0x9A, 0x78, 0x56,
3507 MockFramerVisitor visitor
;
3508 framer_
.set_visitor(&visitor
);
3509 EXPECT_CALL(visitor
, OnPacket());
3510 EXPECT_CALL(visitor
, OnPacketHeader(_
));
3511 EXPECT_CALL(visitor
, OnStreamFrame(_
)).WillOnce(Return(false));
3512 EXPECT_CALL(visitor
, OnAckFrame(_
)).Times(0);
3513 EXPECT_CALL(visitor
, OnPacketComplete());
3514 EXPECT_CALL(visitor
, OnUnauthenticatedPublicHeader(_
)).WillOnce(Return(true));
3516 QuicEncryptedPacket
encrypted(AsChars(packet
), arraysize(packet
), false);
3517 EXPECT_TRUE(framer_
.ProcessPacket(encrypted
));
3518 EXPECT_EQ(QUIC_NO_ERROR
, framer_
.error());