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"
13 #include "base/containers/hash_tables.h"
14 #include "base/logging.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "base/stl_util.h"
17 #include "net/quic/crypto/quic_decrypter.h"
18 #include "net/quic/crypto/quic_encrypter.h"
19 #include "net/quic/quic_protocol.h"
20 #include "net/quic/quic_utils.h"
21 #include "net/quic/test_tools/quic_framer_peer.h"
22 #include "net/quic/test_tools/quic_test_utils.h"
23 #include "net/test/gtest_util.h"
26 using base::StringPiece
;
29 using std::numeric_limits
;
33 using testing::Return
;
40 const QuicPacketSequenceNumber kEpoch
= UINT64_C(1) << 48;
41 const QuicPacketSequenceNumber kMask
= kEpoch
- 1;
43 // Index into the connection_id offset in the header.
44 const size_t kConnectionIdOffset
= kPublicFlagsSize
;
45 // Index into the version string in the header. (if present).
46 const size_t kVersionOffset
= kConnectionIdOffset
+ PACKET_8BYTE_CONNECTION_ID
;
48 // Size in bytes of the stream frame fields for an arbitrary StreamID and
49 // offset and the last frame in a packet.
50 size_t GetMinStreamFrameSize() {
51 return kQuicFrameTypeSize
+ kQuicMaxStreamIdSize
+ kQuicMaxStreamOffsetSize
;
54 // Index into the sequence number offset in the header.
55 size_t GetSequenceNumberOffset(QuicConnectionIdLength connection_id_length
,
56 bool include_version
) {
57 return kConnectionIdOffset
+ connection_id_length
+
58 (include_version
? kQuicVersionSize
: 0);
61 size_t GetSequenceNumberOffset(bool include_version
) {
62 return GetSequenceNumberOffset(PACKET_8BYTE_CONNECTION_ID
, include_version
);
65 // Index into the private flags offset in the data packet header.
66 size_t GetPrivateFlagsOffset(QuicConnectionIdLength connection_id_length
,
67 bool include_version
) {
68 return GetSequenceNumberOffset(connection_id_length
, include_version
) +
69 PACKET_6BYTE_SEQUENCE_NUMBER
;
72 size_t GetPrivateFlagsOffset(bool include_version
) {
73 return GetPrivateFlagsOffset(PACKET_8BYTE_CONNECTION_ID
, include_version
);
76 size_t GetPrivateFlagsOffset(bool include_version
,
77 QuicSequenceNumberLength sequence_number_length
) {
78 return GetSequenceNumberOffset(PACKET_8BYTE_CONNECTION_ID
, include_version
) +
79 sequence_number_length
;
82 // Index into the fec group offset in the header.
83 size_t GetFecGroupOffset(QuicConnectionIdLength connection_id_length
,
84 bool include_version
) {
85 return GetPrivateFlagsOffset(connection_id_length
, include_version
) +
89 size_t GetFecGroupOffset(bool include_version
) {
90 return GetPrivateFlagsOffset(PACKET_8BYTE_CONNECTION_ID
, include_version
) +
94 size_t GetFecGroupOffset(bool include_version
,
95 QuicSequenceNumberLength sequence_number_length
) {
96 return GetPrivateFlagsOffset(include_version
, sequence_number_length
) +
100 // Index into the message tag of the public reset packet.
101 // Public resets always have full connection_ids.
102 const size_t kPublicResetPacketMessageTagOffset
=
103 kConnectionIdOffset
+ PACKET_8BYTE_CONNECTION_ID
;
105 class TestEncrypter
: public QuicEncrypter
{
107 ~TestEncrypter() override
{}
108 bool SetKey(StringPiece key
) override
{ return true; }
109 bool SetNoncePrefix(StringPiece nonce_prefix
) override
{ return true; }
110 bool EncryptPacket(QuicPacketSequenceNumber sequence_number
,
111 StringPiece associated_data
,
112 StringPiece plaintext
,
114 size_t* output_length
,
115 size_t max_output_length
) override
{
116 sequence_number_
= sequence_number
;
117 associated_data_
= associated_data
.as_string();
118 plaintext_
= plaintext
.as_string();
119 memcpy(output
, plaintext
.data(), plaintext
.length());
120 *output_length
= plaintext
.length();
123 size_t GetKeySize() const override
{ return 0; }
124 size_t GetNoncePrefixSize() const override
{ return 0; }
125 size_t GetMaxPlaintextSize(size_t ciphertext_size
) const override
{
126 return ciphertext_size
;
128 size_t GetCiphertextSize(size_t plaintext_size
) const override
{
129 return plaintext_size
;
131 StringPiece
GetKey() const override
{ return StringPiece(); }
132 StringPiece
GetNoncePrefix() const override
{ return StringPiece(); }
133 QuicPacketSequenceNumber sequence_number_
;
134 string associated_data_
;
138 class TestDecrypter
: public QuicDecrypter
{
140 ~TestDecrypter() override
{}
141 bool SetKey(StringPiece key
) override
{ return true; }
142 bool SetNoncePrefix(StringPiece nonce_prefix
) override
{ return true; }
143 bool DecryptPacket(QuicPacketSequenceNumber sequence_number
,
144 const StringPiece
& associated_data
,
145 const StringPiece
& ciphertext
,
147 size_t* output_length
,
148 size_t max_output_length
) override
{
149 sequence_number_
= sequence_number
;
150 associated_data_
= associated_data
.as_string();
151 ciphertext_
= ciphertext
.as_string();
152 memcpy(output
, ciphertext
.data(), ciphertext
.length());
153 *output_length
= ciphertext
.length();
156 StringPiece
GetKey() const override
{ return StringPiece(); }
157 StringPiece
GetNoncePrefix() const override
{ return StringPiece(); }
158 QuicPacketSequenceNumber sequence_number_
;
159 string associated_data_
;
163 class TestQuicVisitor
: public QuicFramerVisitorInterface
{
167 version_mismatch_(0),
171 complete_packets_(0),
173 accept_packet_(true),
174 accept_public_header_(true) {
177 ~TestQuicVisitor() override
{
178 STLDeleteElements(&stream_frames_
);
179 STLDeleteElements(&ack_frames_
);
180 STLDeleteElements(&stop_waiting_frames_
);
181 STLDeleteElements(&ping_frames_
);
182 STLDeleteElements(&fec_data_
);
183 STLDeleteElements(&stream_data_
);
184 STLDeleteElements(&fec_data_redundancy_
);
187 void OnError(QuicFramer
* f
) override
{
188 DVLOG(1) << "QuicFramer Error: " << QuicUtils::ErrorToString(f
->error())
189 << " (" << f
->error() << ")";
193 void OnPacket() override
{}
195 void OnPublicResetPacket(const QuicPublicResetPacket
& packet
) override
{
196 public_reset_packet_
.reset(new QuicPublicResetPacket(packet
));
199 void OnVersionNegotiationPacket(
200 const QuicVersionNegotiationPacket
& packet
) override
{
201 version_negotiation_packet_
.reset(new QuicVersionNegotiationPacket(packet
));
204 void OnRevivedPacket() override
{ ++revived_packets_
; }
206 bool OnProtocolVersionMismatch(QuicVersion version
) override
{
207 DVLOG(1) << "QuicFramer Version Mismatch, version: " << version
;
212 bool OnUnauthenticatedPublicHeader(
213 const QuicPacketPublicHeader
& header
) override
{
214 public_header_
.reset(new QuicPacketPublicHeader(header
));
215 return accept_public_header_
;
218 bool OnUnauthenticatedHeader(const QuicPacketHeader
& header
) override
{
222 void OnDecryptedPacket(EncryptionLevel level
) override
{}
224 bool OnPacketHeader(const QuicPacketHeader
& header
) override
{
226 header_
.reset(new QuicPacketHeader(header
));
227 return accept_packet_
;
230 bool OnStreamFrame(const QuicStreamFrame
& frame
) override
{
232 // Save a copy of the data so it is valid after the packet is processed.
233 string
* string_data
= new string();
234 frame
.data
.AppendToString(string_data
);
235 stream_data_
.push_back(string_data
);
236 QuicStreamFrame
* stream_frame
= new QuicStreamFrame(frame
);
237 // Make sure that the stream frame points to this data.
238 stream_frame
->data
= StringPiece(*string_data
);
239 stream_frames_
.push_back(stream_frame
);
243 void OnFecProtectedPayload(StringPiece payload
) override
{
244 fec_protected_payload_
= payload
.as_string();
247 bool OnAckFrame(const QuicAckFrame
& frame
) override
{
249 ack_frames_
.push_back(new QuicAckFrame(frame
));
253 bool OnStopWaitingFrame(const QuicStopWaitingFrame
& frame
) override
{
255 stop_waiting_frames_
.push_back(new QuicStopWaitingFrame(frame
));
259 bool OnPingFrame(const QuicPingFrame
& frame
) override
{
261 ping_frames_
.push_back(new QuicPingFrame(frame
));
265 void OnFecData(const QuicFecData
& fec
) override
{
267 QuicFecData
* fec_data
= new QuicFecData();
268 fec_data
->fec_group
= fec
.fec_group
;
269 // Save a copy of the data so it is valid after the packet is processed.
270 string
* redundancy
= new string(fec
.redundancy
.as_string());
271 fec_data_redundancy_
.push_back(redundancy
);
272 fec_data
->redundancy
= StringPiece(*redundancy
);
273 fec_data_
.push_back(fec_data
);
276 void OnPacketComplete() override
{ ++complete_packets_
; }
278 bool OnRstStreamFrame(const QuicRstStreamFrame
& frame
) override
{
279 rst_stream_frame_
= frame
;
283 bool OnConnectionCloseFrame(const QuicConnectionCloseFrame
& frame
) override
{
284 connection_close_frame_
= frame
;
288 bool OnGoAwayFrame(const QuicGoAwayFrame
& frame
) override
{
289 goaway_frame_
= frame
;
293 bool OnWindowUpdateFrame(const QuicWindowUpdateFrame
& frame
) override
{
294 window_update_frame_
= frame
;
298 bool OnBlockedFrame(const QuicBlockedFrame
& frame
) override
{
299 blocked_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
<QuicStopWaitingFrame
*> stop_waiting_frames_
;
321 vector
<QuicPingFrame
*> ping_frames_
;
322 vector
<QuicFecData
*> fec_data_
;
323 string fec_protected_payload_
;
324 QuicRstStreamFrame rst_stream_frame_
;
325 QuicConnectionCloseFrame connection_close_frame_
;
326 QuicGoAwayFrame goaway_frame_
;
327 QuicWindowUpdateFrame window_update_frame_
;
328 QuicBlockedFrame blocked_frame_
;
329 vector
<string
*> stream_data_
;
330 vector
<string
*> fec_data_redundancy_
;
333 class QuicFramerTest
: public ::testing::TestWithParam
<QuicVersion
> {
336 : encrypter_(new test::TestEncrypter()),
337 decrypter_(new test::TestDecrypter()),
338 start_(QuicTime::Zero().Add(QuicTime::Delta::FromMicroseconds(0x10))),
339 framer_(QuicSupportedVersions(), start_
, Perspective::IS_SERVER
) {
340 version_
= GetParam();
341 framer_
.set_version(version_
);
342 framer_
.SetDecrypter(decrypter_
, ENCRYPTION_NONE
);
343 framer_
.SetEncrypter(ENCRYPTION_NONE
, encrypter_
);
344 framer_
.set_visitor(&visitor_
);
345 framer_
.set_received_entropy_calculator(&entropy_calculator_
);
348 // Helper function to get unsigned char representation of digit in the
349 // units place of the current QUIC version number.
350 unsigned char GetQuicVersionDigitOnes() {
351 return static_cast<unsigned char> ('0' + version_
%10);
354 // Helper function to get unsigned char representation of digit in the
355 // tens place of the current QUIC version number.
356 unsigned char GetQuicVersionDigitTens() {
357 return static_cast<unsigned char> ('0' + (version_
/10)%10);
360 bool CheckEncryption(QuicPacketSequenceNumber sequence_number
,
361 QuicPacket
* packet
) {
362 if (sequence_number
!= encrypter_
->sequence_number_
) {
363 LOG(ERROR
) << "Encrypted incorrect packet sequence number. expected "
364 << sequence_number
<< " actual: "
365 << encrypter_
->sequence_number_
;
368 if (packet
->AssociatedData() != encrypter_
->associated_data_
) {
369 LOG(ERROR
) << "Encrypted incorrect associated data. expected "
370 << packet
->AssociatedData() << " actual: "
371 << encrypter_
->associated_data_
;
374 if (packet
->Plaintext() != encrypter_
->plaintext_
) {
375 LOG(ERROR
) << "Encrypted incorrect plaintext data. expected "
376 << packet
->Plaintext() << " actual: "
377 << encrypter_
->plaintext_
;
383 bool CheckDecryption(const QuicEncryptedPacket
& encrypted
,
384 bool includes_version
) {
385 if (visitor_
.header_
->packet_sequence_number
!=
386 decrypter_
->sequence_number_
) {
387 LOG(ERROR
) << "Decrypted incorrect packet sequence number. expected "
388 << visitor_
.header_
->packet_sequence_number
<< " actual: "
389 << decrypter_
->sequence_number_
;
392 if (QuicFramer::GetAssociatedDataFromEncryptedPacket(
393 encrypted
, PACKET_8BYTE_CONNECTION_ID
,
394 includes_version
, PACKET_6BYTE_SEQUENCE_NUMBER
) !=
395 decrypter_
->associated_data_
) {
396 LOG(ERROR
) << "Decrypted incorrect associated data. expected "
397 << QuicFramer::GetAssociatedDataFromEncryptedPacket(
398 encrypted
, PACKET_8BYTE_CONNECTION_ID
,
399 includes_version
, PACKET_6BYTE_SEQUENCE_NUMBER
)
400 << " actual: " << decrypter_
->associated_data_
;
403 StringPiece
ciphertext(encrypted
.AsStringPiece().substr(
404 GetStartOfEncryptedData(PACKET_8BYTE_CONNECTION_ID
, includes_version
,
405 PACKET_6BYTE_SEQUENCE_NUMBER
)));
406 if (ciphertext
!= decrypter_
->ciphertext_
) {
407 LOG(ERROR
) << "Decrypted incorrect ciphertext data. expected "
408 << ciphertext
<< " actual: "
409 << decrypter_
->ciphertext_
;
415 char* AsChars(unsigned char* data
) {
416 return reinterpret_cast<char*>(data
);
419 void CheckProcessingFails(unsigned char* packet
,
421 string expected_error
,
422 QuicErrorCode error_code
) {
423 QuicEncryptedPacket
encrypted(AsChars(packet
), len
, false);
424 EXPECT_FALSE(framer_
.ProcessPacket(encrypted
)) << "len: " << len
;
425 EXPECT_EQ(expected_error
, framer_
.detailed_error()) << "len: " << len
;
426 EXPECT_EQ(error_code
, framer_
.error()) << "len: " << len
;
429 // Checks if the supplied string matches data in the supplied StreamFrame.
430 void CheckStreamFrameData(string str
, QuicStreamFrame
* frame
) {
431 EXPECT_EQ(str
, frame
->data
);
434 void CheckStreamFrameBoundaries(unsigned char* packet
,
435 size_t stream_id_size
,
436 bool include_version
) {
437 // Now test framing boundaries.
438 for (size_t i
= kQuicFrameTypeSize
; i
< GetMinStreamFrameSize(); ++i
) {
439 string expected_error
;
440 if (i
< kQuicFrameTypeSize
+ stream_id_size
) {
441 expected_error
= "Unable to read stream_id.";
442 } else if (i
< kQuicFrameTypeSize
+ stream_id_size
+
443 kQuicMaxStreamOffsetSize
) {
444 expected_error
= "Unable to read offset.";
446 expected_error
= "Unable to read frame data.";
448 CheckProcessingFails(
450 i
+ GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID
, include_version
,
451 PACKET_6BYTE_SEQUENCE_NUMBER
,
453 expected_error
, QUIC_INVALID_STREAM_DATA
);
457 void CheckCalculatePacketSequenceNumber(
458 QuicPacketSequenceNumber expected_sequence_number
,
459 QuicPacketSequenceNumber last_sequence_number
) {
460 QuicPacketSequenceNumber wire_sequence_number
=
461 expected_sequence_number
& kMask
;
462 QuicFramerPeer::SetLastSequenceNumber(&framer_
, last_sequence_number
);
463 EXPECT_EQ(expected_sequence_number
,
464 QuicFramerPeer::CalculatePacketSequenceNumberFromWire(
465 &framer_
, PACKET_6BYTE_SEQUENCE_NUMBER
, wire_sequence_number
))
466 << "last_sequence_number: " << last_sequence_number
467 << " wire_sequence_number: " << wire_sequence_number
;
470 QuicPacket
* BuildDataPacket(const QuicPacketHeader
& header
,
471 const QuicFrames
& frames
) {
472 return BuildUnsizedDataPacket(&framer_
, header
, frames
);
475 QuicPacket
* BuildDataPacket(const QuicPacketHeader
& header
,
476 const QuicFrames
& frames
,
477 size_t packet_size
) {
478 return BuildUnsizedDataPacket(&framer_
, header
, frames
, packet_size
);
481 test::TestEncrypter
* encrypter_
;
482 test::TestDecrypter
* decrypter_
;
483 QuicVersion version_
;
486 test::TestQuicVisitor visitor_
;
487 test::TestEntropyCalculator entropy_calculator_
;
490 // Run all framer tests with all supported versions of QUIC.
491 INSTANTIATE_TEST_CASE_P(QuicFramerTests
,
493 ::testing::ValuesIn(kSupportedQuicVersions
));
495 TEST_P(QuicFramerTest
, CalculatePacketSequenceNumberFromWireNearEpochStart
) {
496 // A few quick manual sanity checks
497 CheckCalculatePacketSequenceNumber(UINT64_C(1), UINT64_C(0));
498 CheckCalculatePacketSequenceNumber(kEpoch
+ 1, kMask
);
499 CheckCalculatePacketSequenceNumber(kEpoch
, kMask
);
501 // Cases where the last number was close to the start of the range
502 for (uint64 last
= 0; last
< 10; last
++) {
503 // Small numbers should not wrap (even if they're out of order).
504 for (uint64 j
= 0; j
< 10; j
++) {
505 CheckCalculatePacketSequenceNumber(j
, last
);
508 // Large numbers should not wrap either (because we're near 0 already).
509 for (uint64 j
= 0; j
< 10; j
++) {
510 CheckCalculatePacketSequenceNumber(kEpoch
- 1 - j
, last
);
515 TEST_P(QuicFramerTest
, CalculatePacketSequenceNumberFromWireNearEpochEnd
) {
516 // Cases where the last number was close to the end of the range
517 for (uint64 i
= 0; i
< 10; i
++) {
518 QuicPacketSequenceNumber last
= kEpoch
- i
;
520 // Small numbers should wrap.
521 for (uint64 j
= 0; j
< 10; j
++) {
522 CheckCalculatePacketSequenceNumber(kEpoch
+ j
, last
);
525 // Large numbers should not (even if they're out of order).
526 for (uint64 j
= 0; j
< 10; j
++) {
527 CheckCalculatePacketSequenceNumber(kEpoch
- 1 - j
, last
);
532 // Next check where we're in a non-zero epoch to verify we handle
533 // reverse wrapping, too.
534 TEST_P(QuicFramerTest
, CalculatePacketSequenceNumberFromWireNearPrevEpoch
) {
535 const uint64 prev_epoch
= 1 * kEpoch
;
536 const uint64 cur_epoch
= 2 * kEpoch
;
537 // Cases where the last number was close to the start of the range
538 for (uint64 i
= 0; i
< 10; i
++) {
539 uint64 last
= cur_epoch
+ i
;
540 // Small number should not wrap (even if they're out of order).
541 for (uint64 j
= 0; j
< 10; j
++) {
542 CheckCalculatePacketSequenceNumber(cur_epoch
+ j
, last
);
545 // But large numbers should reverse wrap.
546 for (uint64 j
= 0; j
< 10; j
++) {
547 uint64 num
= kEpoch
- 1 - j
;
548 CheckCalculatePacketSequenceNumber(prev_epoch
+ num
, last
);
553 TEST_P(QuicFramerTest
, CalculatePacketSequenceNumberFromWireNearNextEpoch
) {
554 const uint64 cur_epoch
= 2 * kEpoch
;
555 const uint64 next_epoch
= 3 * kEpoch
;
556 // Cases where the last number was close to the end of the range
557 for (uint64 i
= 0; i
< 10; i
++) {
558 QuicPacketSequenceNumber last
= next_epoch
- 1 - i
;
560 // Small numbers should wrap.
561 for (uint64 j
= 0; j
< 10; j
++) {
562 CheckCalculatePacketSequenceNumber(next_epoch
+ j
, last
);
565 // but large numbers should not (even if they're out of order).
566 for (uint64 j
= 0; j
< 10; j
++) {
567 uint64 num
= kEpoch
- 1 - j
;
568 CheckCalculatePacketSequenceNumber(cur_epoch
+ num
, last
);
573 TEST_P(QuicFramerTest
, CalculatePacketSequenceNumberFromWireNearNextMax
) {
574 const uint64 max_number
= numeric_limits
<uint64
>::max();
575 const uint64 max_epoch
= max_number
& ~kMask
;
577 // Cases where the last number was close to the end of the range
578 for (uint64 i
= 0; i
< 10; i
++) {
579 // Subtract 1, because the expected next sequence number is 1 more than the
580 // last sequence number.
581 QuicPacketSequenceNumber last
= max_number
- i
- 1;
583 // Small numbers should not wrap, because they have nowhere to go.
584 for (uint64 j
= 0; j
< 10; j
++) {
585 CheckCalculatePacketSequenceNumber(max_epoch
+ j
, last
);
588 // Large numbers should not wrap either.
589 for (uint64 j
= 0; j
< 10; j
++) {
590 uint64 num
= kEpoch
- 1 - j
;
591 CheckCalculatePacketSequenceNumber(max_epoch
+ num
, last
);
596 TEST_P(QuicFramerTest
, EmptyPacket
) {
597 char packet
[] = { 0x00 };
598 QuicEncryptedPacket
encrypted(packet
, 0, false);
599 EXPECT_FALSE(framer_
.ProcessPacket(encrypted
));
600 EXPECT_EQ(QUIC_INVALID_PACKET_HEADER
, framer_
.error());
603 TEST_P(QuicFramerTest
, LargePacket
) {
604 unsigned char packet
[kMaxPacketSize
+ 1] = {
605 // public flags (8 byte connection_id)
616 // packet sequence number
627 memset(packet
+ GetPacketHeaderSize(
628 PACKET_8BYTE_CONNECTION_ID
, !kIncludeVersion
,
629 PACKET_6BYTE_SEQUENCE_NUMBER
, NOT_IN_FEC_GROUP
), 0,
630 kMaxPacketSize
- GetPacketHeaderSize(
631 PACKET_8BYTE_CONNECTION_ID
, !kIncludeVersion
,
632 PACKET_6BYTE_SEQUENCE_NUMBER
, NOT_IN_FEC_GROUP
) + 1);
634 QuicEncryptedPacket
encrypted(AsChars(packet
), arraysize(packet
), false);
635 EXPECT_FALSE(framer_
.ProcessPacket(encrypted
));
637 ASSERT_TRUE(visitor_
.header_
.get());
638 // Make sure we've parsed the packet header, so we can send an error.
639 EXPECT_EQ(UINT64_C(0xFEDCBA9876543210),
640 visitor_
.header_
->public_header
.connection_id
);
641 // Make sure the correct error is propagated.
642 EXPECT_EQ(QUIC_PACKET_TOO_LARGE
, framer_
.error());
645 TEST_P(QuicFramerTest
, PacketHeader
) {
646 unsigned char packet
[] = {
647 // public flags (8 byte connection_id)
650 0x10, 0x32, 0x54, 0x76,
651 0x98, 0xBA, 0xDC, 0xFE,
652 // packet sequence number
653 0xBC, 0x9A, 0x78, 0x56,
659 QuicEncryptedPacket
encrypted(AsChars(packet
), arraysize(packet
), false);
660 EXPECT_FALSE(framer_
.ProcessPacket(encrypted
));
661 EXPECT_EQ(QUIC_MISSING_PAYLOAD
, framer_
.error());
662 ASSERT_TRUE(visitor_
.header_
.get());
663 EXPECT_EQ(UINT64_C(0xFEDCBA9876543210),
664 visitor_
.header_
->public_header
.connection_id
);
665 EXPECT_FALSE(visitor_
.header_
->public_header
.reset_flag
);
666 EXPECT_FALSE(visitor_
.header_
->public_header
.version_flag
);
667 EXPECT_FALSE(visitor_
.header_
->fec_flag
);
668 EXPECT_FALSE(visitor_
.header_
->entropy_flag
);
669 EXPECT_EQ(0, visitor_
.header_
->entropy_hash
);
670 EXPECT_EQ(UINT64_C(0x123456789ABC),
671 visitor_
.header_
->packet_sequence_number
);
672 EXPECT_EQ(NOT_IN_FEC_GROUP
, visitor_
.header_
->is_in_fec_group
);
673 EXPECT_EQ(0x00u
, visitor_
.header_
->fec_group
);
675 // Now test framing boundaries.
677 i
< GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID
, !kIncludeVersion
,
678 PACKET_6BYTE_SEQUENCE_NUMBER
, NOT_IN_FEC_GROUP
);
680 string expected_error
;
681 if (i
< kConnectionIdOffset
) {
682 expected_error
= "Unable to read public flags.";
683 } else if (i
< GetSequenceNumberOffset(!kIncludeVersion
)) {
684 expected_error
= "Unable to read ConnectionId.";
685 } else if (i
< GetPrivateFlagsOffset(!kIncludeVersion
)) {
686 expected_error
= "Unable to read sequence number.";
687 } else if (i
< GetFecGroupOffset(!kIncludeVersion
)) {
688 expected_error
= "Unable to read private flags.";
690 expected_error
= "Unable to read first fec protected packet offset.";
692 CheckProcessingFails(packet
, i
, expected_error
, QUIC_INVALID_PACKET_HEADER
);
696 TEST_P(QuicFramerTest
, PacketHeaderWith4ByteConnectionId
) {
697 QuicFramerPeer::SetLastSerializedConnectionId(
698 &framer_
, UINT64_C(0xFEDCBA9876543210));
700 unsigned char packet
[] = {
701 // public flags (4 byte connection_id)
704 0x10, 0x32, 0x54, 0x76,
705 // packet sequence number
706 0xBC, 0x9A, 0x78, 0x56,
712 QuicEncryptedPacket
encrypted(AsChars(packet
), arraysize(packet
), false);
713 EXPECT_FALSE(framer_
.ProcessPacket(encrypted
));
714 EXPECT_EQ(QUIC_MISSING_PAYLOAD
, framer_
.error());
715 ASSERT_TRUE(visitor_
.header_
.get());
716 EXPECT_EQ(UINT64_C(0xFEDCBA9876543210),
717 visitor_
.header_
->public_header
.connection_id
);
718 EXPECT_FALSE(visitor_
.header_
->public_header
.reset_flag
);
719 EXPECT_FALSE(visitor_
.header_
->public_header
.version_flag
);
720 EXPECT_FALSE(visitor_
.header_
->fec_flag
);
721 EXPECT_FALSE(visitor_
.header_
->entropy_flag
);
722 EXPECT_EQ(0, visitor_
.header_
->entropy_hash
);
723 EXPECT_EQ(UINT64_C(0x123456789ABC),
724 visitor_
.header_
->packet_sequence_number
);
725 EXPECT_EQ(NOT_IN_FEC_GROUP
, visitor_
.header_
->is_in_fec_group
);
726 EXPECT_EQ(0x00u
, visitor_
.header_
->fec_group
);
728 // Now test framing boundaries.
730 i
< GetPacketHeaderSize(PACKET_4BYTE_CONNECTION_ID
, !kIncludeVersion
,
731 PACKET_6BYTE_SEQUENCE_NUMBER
, NOT_IN_FEC_GROUP
);
733 string expected_error
;
734 if (i
< kConnectionIdOffset
) {
735 expected_error
= "Unable to read public flags.";
736 } else if (i
< GetSequenceNumberOffset(PACKET_4BYTE_CONNECTION_ID
,
738 expected_error
= "Unable to read ConnectionId.";
739 } else if (i
< GetPrivateFlagsOffset(PACKET_4BYTE_CONNECTION_ID
,
741 expected_error
= "Unable to read sequence number.";
742 } else if (i
< GetFecGroupOffset(PACKET_4BYTE_CONNECTION_ID
,
744 expected_error
= "Unable to read private flags.";
746 expected_error
= "Unable to read first fec protected packet offset.";
748 CheckProcessingFails(packet
, i
, expected_error
, QUIC_INVALID_PACKET_HEADER
);
752 TEST_P(QuicFramerTest
, PacketHeader1ByteConnectionId
) {
753 QuicFramerPeer::SetLastSerializedConnectionId(
754 &framer_
, UINT64_C(0xFEDCBA9876543210));
756 unsigned char packet
[] = {
757 // public flags (1 byte connection_id)
761 // packet sequence number
762 0xBC, 0x9A, 0x78, 0x56,
768 QuicEncryptedPacket
encrypted(AsChars(packet
), arraysize(packet
), false);
769 EXPECT_FALSE(framer_
.ProcessPacket(encrypted
));
770 EXPECT_EQ(QUIC_MISSING_PAYLOAD
, framer_
.error());
771 ASSERT_TRUE(visitor_
.header_
.get());
772 EXPECT_EQ(UINT64_C(0xFEDCBA9876543210),
773 visitor_
.header_
->public_header
.connection_id
);
774 EXPECT_FALSE(visitor_
.header_
->public_header
.reset_flag
);
775 EXPECT_FALSE(visitor_
.header_
->public_header
.version_flag
);
776 EXPECT_FALSE(visitor_
.header_
->fec_flag
);
777 EXPECT_FALSE(visitor_
.header_
->entropy_flag
);
778 EXPECT_EQ(0, visitor_
.header_
->entropy_hash
);
779 EXPECT_EQ(UINT64_C(0x123456789ABC),
780 visitor_
.header_
->packet_sequence_number
);
781 EXPECT_EQ(NOT_IN_FEC_GROUP
, visitor_
.header_
->is_in_fec_group
);
782 EXPECT_EQ(0x00u
, visitor_
.header_
->fec_group
);
784 // Now test framing boundaries.
786 i
< GetPacketHeaderSize(PACKET_1BYTE_CONNECTION_ID
, !kIncludeVersion
,
787 PACKET_6BYTE_SEQUENCE_NUMBER
, NOT_IN_FEC_GROUP
);
789 string expected_error
;
790 if (i
< kConnectionIdOffset
) {
791 expected_error
= "Unable to read public flags.";
792 } else if (i
< GetSequenceNumberOffset(PACKET_1BYTE_CONNECTION_ID
,
794 expected_error
= "Unable to read ConnectionId.";
795 } else if (i
< GetPrivateFlagsOffset(PACKET_1BYTE_CONNECTION_ID
,
797 expected_error
= "Unable to read sequence number.";
798 } else if (i
< GetFecGroupOffset(PACKET_1BYTE_CONNECTION_ID
,
800 expected_error
= "Unable to read private flags.";
802 expected_error
= "Unable to read first fec protected packet offset.";
804 CheckProcessingFails(packet
, i
, expected_error
, QUIC_INVALID_PACKET_HEADER
);
808 TEST_P(QuicFramerTest
, PacketHeaderWith0ByteConnectionId
) {
809 QuicFramerPeer::SetLastSerializedConnectionId(
810 &framer_
, UINT64_C(0xFEDCBA9876543210));
812 unsigned char packet
[] = {
813 // public flags (0 byte connection_id)
816 // packet sequence number
817 0xBC, 0x9A, 0x78, 0x56,
823 QuicEncryptedPacket
encrypted(AsChars(packet
), arraysize(packet
), false);
824 EXPECT_FALSE(framer_
.ProcessPacket(encrypted
));
825 EXPECT_EQ(QUIC_MISSING_PAYLOAD
, framer_
.error());
826 ASSERT_TRUE(visitor_
.header_
.get());
827 EXPECT_EQ(UINT64_C(0xFEDCBA9876543210),
828 visitor_
.header_
->public_header
.connection_id
);
829 EXPECT_FALSE(visitor_
.header_
->public_header
.reset_flag
);
830 EXPECT_FALSE(visitor_
.header_
->public_header
.version_flag
);
831 EXPECT_FALSE(visitor_
.header_
->fec_flag
);
832 EXPECT_FALSE(visitor_
.header_
->entropy_flag
);
833 EXPECT_EQ(0, visitor_
.header_
->entropy_hash
);
834 EXPECT_EQ(UINT64_C(0x123456789ABC),
835 visitor_
.header_
->packet_sequence_number
);
836 EXPECT_EQ(NOT_IN_FEC_GROUP
, visitor_
.header_
->is_in_fec_group
);
837 EXPECT_EQ(0x00u
, visitor_
.header_
->fec_group
);
839 // Now test framing boundaries.
841 i
< GetPacketHeaderSize(PACKET_0BYTE_CONNECTION_ID
, !kIncludeVersion
,
842 PACKET_6BYTE_SEQUENCE_NUMBER
, NOT_IN_FEC_GROUP
);
844 string expected_error
;
845 if (i
< kConnectionIdOffset
) {
846 expected_error
= "Unable to read public flags.";
847 } else if (i
< GetSequenceNumberOffset(PACKET_0BYTE_CONNECTION_ID
,
849 expected_error
= "Unable to read ConnectionId.";
850 } else if (i
< GetPrivateFlagsOffset(PACKET_0BYTE_CONNECTION_ID
,
852 expected_error
= "Unable to read sequence number.";
853 } else if (i
< GetFecGroupOffset(PACKET_0BYTE_CONNECTION_ID
,
855 expected_error
= "Unable to read private flags.";
857 expected_error
= "Unable to read first fec protected packet offset.";
859 CheckProcessingFails(packet
, i
, expected_error
, QUIC_INVALID_PACKET_HEADER
);
863 TEST_P(QuicFramerTest
, PacketHeaderWithVersionFlag
) {
864 unsigned char packet
[] = {
865 // public flags (version)
868 0x10, 0x32, 0x54, 0x76,
869 0x98, 0xBA, 0xDC, 0xFE,
871 'Q', '0', GetQuicVersionDigitTens(), GetQuicVersionDigitOnes(),
872 // packet sequence number
873 0xBC, 0x9A, 0x78, 0x56,
879 QuicEncryptedPacket
encrypted(AsChars(packet
), arraysize(packet
), false);
880 EXPECT_FALSE(framer_
.ProcessPacket(encrypted
));
881 EXPECT_EQ(QUIC_MISSING_PAYLOAD
, framer_
.error());
882 ASSERT_TRUE(visitor_
.header_
.get());
883 EXPECT_EQ(UINT64_C(0xFEDCBA9876543210),
884 visitor_
.header_
->public_header
.connection_id
);
885 EXPECT_FALSE(visitor_
.header_
->public_header
.reset_flag
);
886 EXPECT_TRUE(visitor_
.header_
->public_header
.version_flag
);
887 EXPECT_EQ(GetParam(), visitor_
.header_
->public_header
.versions
[0]);
888 EXPECT_FALSE(visitor_
.header_
->fec_flag
);
889 EXPECT_FALSE(visitor_
.header_
->entropy_flag
);
890 EXPECT_EQ(0, visitor_
.header_
->entropy_hash
);
891 EXPECT_EQ(UINT64_C(0x123456789ABC),
892 visitor_
.header_
->packet_sequence_number
);
893 EXPECT_EQ(NOT_IN_FEC_GROUP
, visitor_
.header_
->is_in_fec_group
);
894 EXPECT_EQ(0x00u
, visitor_
.header_
->fec_group
);
896 // Now test framing boundaries.
898 i
< GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID
, kIncludeVersion
,
899 PACKET_6BYTE_SEQUENCE_NUMBER
, NOT_IN_FEC_GROUP
);
901 string expected_error
;
902 if (i
< kConnectionIdOffset
) {
903 expected_error
= "Unable to read public flags.";
904 } else if (i
< kVersionOffset
) {
905 expected_error
= "Unable to read ConnectionId.";
906 } else if (i
< GetSequenceNumberOffset(kIncludeVersion
)) {
907 expected_error
= "Unable to read protocol version.";
908 } else if (i
< GetPrivateFlagsOffset(kIncludeVersion
)) {
909 expected_error
= "Unable to read sequence number.";
910 } else if (i
< GetFecGroupOffset(kIncludeVersion
)) {
911 expected_error
= "Unable to read private flags.";
913 expected_error
= "Unable to read first fec protected packet offset.";
915 CheckProcessingFails(packet
, i
, expected_error
, QUIC_INVALID_PACKET_HEADER
);
919 TEST_P(QuicFramerTest
, PacketHeaderWith4ByteSequenceNumber
) {
920 QuicFramerPeer::SetLastSequenceNumber(&framer_
, UINT64_C(0x123456789ABA));
922 unsigned char packet
[] = {
923 // public flags (8 byte connection_id and 4 byte sequence number)
926 0x10, 0x32, 0x54, 0x76,
927 0x98, 0xBA, 0xDC, 0xFE,
928 // packet sequence number
929 0xBC, 0x9A, 0x78, 0x56,
934 QuicEncryptedPacket
encrypted(AsChars(packet
), arraysize(packet
), false);
935 EXPECT_FALSE(framer_
.ProcessPacket(encrypted
));
936 EXPECT_EQ(QUIC_MISSING_PAYLOAD
, framer_
.error());
937 ASSERT_TRUE(visitor_
.header_
.get());
938 EXPECT_EQ(UINT64_C(0xFEDCBA9876543210),
939 visitor_
.header_
->public_header
.connection_id
);
940 EXPECT_FALSE(visitor_
.header_
->public_header
.reset_flag
);
941 EXPECT_FALSE(visitor_
.header_
->public_header
.version_flag
);
942 EXPECT_FALSE(visitor_
.header_
->fec_flag
);
943 EXPECT_FALSE(visitor_
.header_
->entropy_flag
);
944 EXPECT_EQ(0, visitor_
.header_
->entropy_hash
);
945 EXPECT_EQ(UINT64_C(0x123456789ABC), visitor_
.header_
->packet_sequence_number
);
946 EXPECT_EQ(NOT_IN_FEC_GROUP
, visitor_
.header_
->is_in_fec_group
);
947 EXPECT_EQ(0x00u
, visitor_
.header_
->fec_group
);
949 // Now test framing boundaries.
951 i
< GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID
, !kIncludeVersion
,
952 PACKET_4BYTE_SEQUENCE_NUMBER
, NOT_IN_FEC_GROUP
);
954 string expected_error
;
955 if (i
< kConnectionIdOffset
) {
956 expected_error
= "Unable to read public flags.";
957 } else if (i
< GetSequenceNumberOffset(!kIncludeVersion
)) {
958 expected_error
= "Unable to read ConnectionId.";
959 } else if (i
< GetPrivateFlagsOffset(!kIncludeVersion
,
960 PACKET_4BYTE_SEQUENCE_NUMBER
)) {
961 expected_error
= "Unable to read sequence number.";
962 } else if (i
< GetFecGroupOffset(!kIncludeVersion
,
963 PACKET_4BYTE_SEQUENCE_NUMBER
)) {
964 expected_error
= "Unable to read private flags.";
966 expected_error
= "Unable to read first fec protected packet offset.";
968 CheckProcessingFails(packet
, i
, expected_error
, QUIC_INVALID_PACKET_HEADER
);
972 TEST_P(QuicFramerTest
, PacketHeaderWith2ByteSequenceNumber
) {
973 QuicFramerPeer::SetLastSequenceNumber(&framer_
, UINT64_C(0x123456789ABA));
975 unsigned char packet
[] = {
976 // public flags (8 byte connection_id and 2 byte sequence number)
979 0x10, 0x32, 0x54, 0x76,
980 0x98, 0xBA, 0xDC, 0xFE,
981 // packet sequence number
987 QuicEncryptedPacket
encrypted(AsChars(packet
), arraysize(packet
), false);
988 EXPECT_FALSE(framer_
.ProcessPacket(encrypted
));
989 EXPECT_EQ(QUIC_MISSING_PAYLOAD
, framer_
.error());
990 ASSERT_TRUE(visitor_
.header_
.get());
991 EXPECT_EQ(UINT64_C(0xFEDCBA9876543210),
992 visitor_
.header_
->public_header
.connection_id
);
993 EXPECT_FALSE(visitor_
.header_
->public_header
.reset_flag
);
994 EXPECT_FALSE(visitor_
.header_
->public_header
.version_flag
);
995 EXPECT_FALSE(visitor_
.header_
->fec_flag
);
996 EXPECT_FALSE(visitor_
.header_
->entropy_flag
);
997 EXPECT_EQ(0, visitor_
.header_
->entropy_hash
);
998 EXPECT_EQ(UINT64_C(0x123456789ABC), visitor_
.header_
->packet_sequence_number
);
999 EXPECT_EQ(NOT_IN_FEC_GROUP
, visitor_
.header_
->is_in_fec_group
);
1000 EXPECT_EQ(0x00u
, visitor_
.header_
->fec_group
);
1002 // Now test framing boundaries.
1004 i
< GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID
, !kIncludeVersion
,
1005 PACKET_2BYTE_SEQUENCE_NUMBER
, NOT_IN_FEC_GROUP
);
1007 string expected_error
;
1008 if (i
< kConnectionIdOffset
) {
1009 expected_error
= "Unable to read public flags.";
1010 } else if (i
< GetSequenceNumberOffset(!kIncludeVersion
)) {
1011 expected_error
= "Unable to read ConnectionId.";
1012 } else if (i
< GetPrivateFlagsOffset(!kIncludeVersion
,
1013 PACKET_2BYTE_SEQUENCE_NUMBER
)) {
1014 expected_error
= "Unable to read sequence number.";
1015 } else if (i
< GetFecGroupOffset(!kIncludeVersion
,
1016 PACKET_2BYTE_SEQUENCE_NUMBER
)) {
1017 expected_error
= "Unable to read private flags.";
1019 expected_error
= "Unable to read first fec protected packet offset.";
1021 CheckProcessingFails(packet
, i
, expected_error
, QUIC_INVALID_PACKET_HEADER
);
1025 TEST_P(QuicFramerTest
, PacketHeaderWith1ByteSequenceNumber
) {
1026 QuicFramerPeer::SetLastSequenceNumber(&framer_
, UINT64_C(0x123456789ABA));
1028 unsigned char packet
[] = {
1029 // public flags (8 byte connection_id and 1 byte sequence number)
1032 0x10, 0x32, 0x54, 0x76,
1033 0x98, 0xBA, 0xDC, 0xFE,
1034 // packet sequence number
1040 QuicEncryptedPacket
encrypted(AsChars(packet
), arraysize(packet
), false);
1041 EXPECT_FALSE(framer_
.ProcessPacket(encrypted
));
1042 EXPECT_EQ(QUIC_MISSING_PAYLOAD
, framer_
.error());
1043 ASSERT_TRUE(visitor_
.header_
.get());
1044 EXPECT_EQ(UINT64_C(0xFEDCBA9876543210),
1045 visitor_
.header_
->public_header
.connection_id
);
1046 EXPECT_FALSE(visitor_
.header_
->public_header
.reset_flag
);
1047 EXPECT_FALSE(visitor_
.header_
->public_header
.version_flag
);
1048 EXPECT_FALSE(visitor_
.header_
->fec_flag
);
1049 EXPECT_FALSE(visitor_
.header_
->entropy_flag
);
1050 EXPECT_EQ(0, visitor_
.header_
->entropy_hash
);
1051 EXPECT_EQ(UINT64_C(0x123456789ABC), visitor_
.header_
->packet_sequence_number
);
1052 EXPECT_EQ(NOT_IN_FEC_GROUP
, visitor_
.header_
->is_in_fec_group
);
1053 EXPECT_EQ(0x00u
, visitor_
.header_
->fec_group
);
1055 // Now test framing boundaries.
1057 i
< GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID
, !kIncludeVersion
,
1058 PACKET_1BYTE_SEQUENCE_NUMBER
, NOT_IN_FEC_GROUP
);
1060 string expected_error
;
1061 if (i
< kConnectionIdOffset
) {
1062 expected_error
= "Unable to read public flags.";
1063 } else if (i
< GetSequenceNumberOffset(!kIncludeVersion
)) {
1064 expected_error
= "Unable to read ConnectionId.";
1065 } else if (i
< GetPrivateFlagsOffset(!kIncludeVersion
,
1066 PACKET_1BYTE_SEQUENCE_NUMBER
)) {
1067 expected_error
= "Unable to read sequence number.";
1068 } else if (i
< GetFecGroupOffset(!kIncludeVersion
,
1069 PACKET_1BYTE_SEQUENCE_NUMBER
)) {
1070 expected_error
= "Unable to read private flags.";
1072 expected_error
= "Unable to read first fec protected packet offset.";
1074 CheckProcessingFails(packet
, i
, expected_error
, QUIC_INVALID_PACKET_HEADER
);
1078 TEST_P(QuicFramerTest
, InvalidPublicFlag
) {
1079 unsigned char packet
[] = {
1080 // public flags: all flags set but the public reset flag and version flag.
1083 0x10, 0x32, 0x54, 0x76,
1084 0x98, 0xBA, 0xDC, 0xFE,
1085 // packet sequence number
1086 0xBC, 0x9A, 0x78, 0x56,
1091 // frame type (padding)
1093 0x00, 0x00, 0x00, 0x00
1095 CheckProcessingFails(packet
,
1097 "Illegal public flags value.",
1098 QUIC_INVALID_PACKET_HEADER
);
1100 // Now turn off validation.
1101 framer_
.set_validate_flags(false);
1102 QuicEncryptedPacket
encrypted(AsChars(packet
), arraysize(packet
), false);
1103 EXPECT_TRUE(framer_
.ProcessPacket(encrypted
));
1106 TEST_P(QuicFramerTest
, InvalidPublicFlagWithMatchingVersions
) {
1107 unsigned char packet
[] = {
1108 // public flags (8 byte connection_id and version flag and an unknown flag)
1111 0x10, 0x32, 0x54, 0x76,
1112 0x98, 0xBA, 0xDC, 0xFE,
1114 'Q', '0', GetQuicVersionDigitTens(), GetQuicVersionDigitOnes(),
1115 // packet sequence number
1116 0xBC, 0x9A, 0x78, 0x56,
1121 // frame type (padding)
1123 0x00, 0x00, 0x00, 0x00
1125 CheckProcessingFails(packet
,
1127 "Illegal public flags value.",
1128 QUIC_INVALID_PACKET_HEADER
);
1131 TEST_P(QuicFramerTest
, LargePublicFlagWithMismatchedVersions
) {
1132 unsigned char packet
[] = {
1133 // public flags (8 byte connection_id, version flag and an unknown flag)
1136 0x10, 0x32, 0x54, 0x76,
1137 0x98, 0xBA, 0xDC, 0xFE,
1140 // packet sequence number
1141 0xBC, 0x9A, 0x78, 0x56,
1146 // frame type (padding frame)
1148 0x00, 0x00, 0x00, 0x00
1150 QuicEncryptedPacket
encrypted(AsChars(packet
), arraysize(packet
), false);
1151 EXPECT_TRUE(framer_
.ProcessPacket(encrypted
));
1152 EXPECT_EQ(QUIC_NO_ERROR
, framer_
.error());
1153 ASSERT_TRUE(visitor_
.header_
.get());
1154 EXPECT_EQ(0, visitor_
.frame_count_
);
1155 EXPECT_EQ(1, visitor_
.version_mismatch_
);
1158 TEST_P(QuicFramerTest
, InvalidPrivateFlag
) {
1159 unsigned char packet
[] = {
1160 // public flags (8 byte connection_id)
1163 0x10, 0x32, 0x54, 0x76,
1164 0x98, 0xBA, 0xDC, 0xFE,
1165 // packet sequence number
1166 0xBC, 0x9A, 0x78, 0x56,
1171 // frame type (padding)
1173 0x00, 0x00, 0x00, 0x00
1175 CheckProcessingFails(packet
,
1177 "Illegal private flags value.",
1178 QUIC_INVALID_PACKET_HEADER
);
1181 TEST_P(QuicFramerTest
, InvalidFECGroupOffset
) {
1182 unsigned char packet
[] = {
1183 // public flags (8 byte connection_id)
1186 0x10, 0x32, 0x54, 0x76,
1187 0x98, 0xBA, 0xDC, 0xFE,
1188 // packet sequence number
1189 0x01, 0x00, 0x00, 0x00,
1191 // private flags (fec group)
1193 // first fec protected packet offset
1196 CheckProcessingFails(packet
,
1198 "First fec protected packet offset must be less "
1199 "than the sequence number.",
1200 QUIC_INVALID_PACKET_HEADER
);
1203 TEST_P(QuicFramerTest
, PaddingFrame
) {
1204 unsigned char packet
[] = {
1205 // public flags (8 byte connection_id)
1208 0x10, 0x32, 0x54, 0x76,
1209 0x98, 0xBA, 0xDC, 0xFE,
1210 // packet sequence number
1211 0xBC, 0x9A, 0x78, 0x56,
1216 // frame type (padding frame)
1218 // Ignored data (which in this case is a stream frame)
1219 // frame type (stream frame with fin)
1222 0x04, 0x03, 0x02, 0x01,
1224 0x54, 0x76, 0x10, 0x32,
1225 0xDC, 0xFE, 0x98, 0xBA,
1234 QuicEncryptedPacket
encrypted(AsChars(packet
), arraysize(packet
), false);
1235 EXPECT_TRUE(framer_
.ProcessPacket(encrypted
));
1236 EXPECT_EQ(QUIC_NO_ERROR
, framer_
.error());
1237 ASSERT_TRUE(visitor_
.header_
.get());
1238 EXPECT_TRUE(CheckDecryption(encrypted
, !kIncludeVersion
));
1240 ASSERT_EQ(0u, visitor_
.stream_frames_
.size());
1241 EXPECT_EQ(0u, visitor_
.ack_frames_
.size());
1242 // A packet with no frames is not acceptable.
1243 CheckProcessingFails(
1245 GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID
, !kIncludeVersion
,
1246 PACKET_6BYTE_SEQUENCE_NUMBER
, NOT_IN_FEC_GROUP
),
1247 "Packet has no frames.", QUIC_MISSING_PAYLOAD
);
1250 TEST_P(QuicFramerTest
, StreamFrame
) {
1251 unsigned char packet
[] = {
1252 // public flags (8 byte connection_id)
1255 0x10, 0x32, 0x54, 0x76,
1256 0x98, 0xBA, 0xDC, 0xFE,
1257 // packet sequence number
1258 0xBC, 0x9A, 0x78, 0x56,
1263 // frame type (stream frame with fin)
1266 0x04, 0x03, 0x02, 0x01,
1268 0x54, 0x76, 0x10, 0x32,
1269 0xDC, 0xFE, 0x98, 0xBA,
1278 QuicEncryptedPacket
encrypted(AsChars(packet
), arraysize(packet
), false);
1279 EXPECT_TRUE(framer_
.ProcessPacket(encrypted
));
1281 EXPECT_EQ(QUIC_NO_ERROR
, framer_
.error());
1282 ASSERT_TRUE(visitor_
.header_
.get());
1283 EXPECT_TRUE(CheckDecryption(encrypted
, !kIncludeVersion
));
1285 ASSERT_EQ(1u, visitor_
.stream_frames_
.size());
1286 EXPECT_EQ(0u, visitor_
.ack_frames_
.size());
1287 EXPECT_EQ(static_cast<uint64
>(0x01020304),
1288 visitor_
.stream_frames_
[0]->stream_id
);
1289 EXPECT_TRUE(visitor_
.stream_frames_
[0]->fin
);
1290 EXPECT_EQ(UINT64_C(0xBA98FEDC32107654), visitor_
.stream_frames_
[0]->offset
);
1291 CheckStreamFrameData("hello world!", visitor_
.stream_frames_
[0]);
1293 // Now test framing boundaries.
1294 CheckStreamFrameBoundaries(packet
, kQuicMaxStreamIdSize
, !kIncludeVersion
);
1297 TEST_P(QuicFramerTest
, StreamFrame3ByteStreamId
) {
1298 unsigned char packet
[] = {
1299 // public flags (8 byte connection_id)
1302 0x10, 0x32, 0x54, 0x76,
1303 0x98, 0xBA, 0xDC, 0xFE,
1304 // packet sequence number
1305 0xBC, 0x9A, 0x78, 0x56,
1310 // frame type (stream frame with fin)
1315 0x54, 0x76, 0x10, 0x32,
1316 0xDC, 0xFE, 0x98, 0xBA,
1325 QuicEncryptedPacket
encrypted(AsChars(packet
), arraysize(packet
), false);
1326 EXPECT_TRUE(framer_
.ProcessPacket(encrypted
));
1328 EXPECT_EQ(QUIC_NO_ERROR
, framer_
.error());
1329 ASSERT_TRUE(visitor_
.header_
.get());
1330 EXPECT_TRUE(CheckDecryption(encrypted
, !kIncludeVersion
));
1332 ASSERT_EQ(1u, visitor_
.stream_frames_
.size());
1333 EXPECT_EQ(0u, visitor_
.ack_frames_
.size());
1334 EXPECT_EQ(UINT64_C(0x00020304), visitor_
.stream_frames_
[0]->stream_id
);
1335 EXPECT_TRUE(visitor_
.stream_frames_
[0]->fin
);
1336 EXPECT_EQ(UINT64_C(0xBA98FEDC32107654), visitor_
.stream_frames_
[0]->offset
);
1337 CheckStreamFrameData("hello world!", visitor_
.stream_frames_
[0]);
1339 // Now test framing boundaries.
1340 const size_t stream_id_size
= 3;
1341 CheckStreamFrameBoundaries(packet
, stream_id_size
, !kIncludeVersion
);
1344 TEST_P(QuicFramerTest
, StreamFrame2ByteStreamId
) {
1345 unsigned char packet
[] = {
1346 // public flags (8 byte connection_id)
1349 0x10, 0x32, 0x54, 0x76,
1350 0x98, 0xBA, 0xDC, 0xFE,
1351 // packet sequence number
1352 0xBC, 0x9A, 0x78, 0x56,
1357 // frame type (stream frame with fin)
1362 0x54, 0x76, 0x10, 0x32,
1363 0xDC, 0xFE, 0x98, 0xBA,
1372 QuicEncryptedPacket
encrypted(AsChars(packet
), arraysize(packet
), false);
1373 EXPECT_TRUE(framer_
.ProcessPacket(encrypted
));
1375 EXPECT_EQ(QUIC_NO_ERROR
, framer_
.error());
1376 ASSERT_TRUE(visitor_
.header_
.get());
1377 EXPECT_TRUE(CheckDecryption(encrypted
, !kIncludeVersion
));
1379 ASSERT_EQ(1u, visitor_
.stream_frames_
.size());
1380 EXPECT_EQ(0u, visitor_
.ack_frames_
.size());
1381 EXPECT_EQ(static_cast<uint64
>(0x00000304),
1382 visitor_
.stream_frames_
[0]->stream_id
);
1383 EXPECT_TRUE(visitor_
.stream_frames_
[0]->fin
);
1384 EXPECT_EQ(UINT64_C(0xBA98FEDC32107654), visitor_
.stream_frames_
[0]->offset
);
1385 CheckStreamFrameData("hello world!", visitor_
.stream_frames_
[0]);
1387 // Now test framing boundaries.
1388 const size_t stream_id_size
= 2;
1389 CheckStreamFrameBoundaries(packet
, stream_id_size
, !kIncludeVersion
);
1392 TEST_P(QuicFramerTest
, StreamFrame1ByteStreamId
) {
1393 unsigned char packet
[] = {
1394 // public flags (8 byte connection_id)
1397 0x10, 0x32, 0x54, 0x76,
1398 0x98, 0xBA, 0xDC, 0xFE,
1399 // packet sequence number
1400 0xBC, 0x9A, 0x78, 0x56,
1405 // frame type (stream frame with fin)
1410 0x54, 0x76, 0x10, 0x32,
1411 0xDC, 0xFE, 0x98, 0xBA,
1420 QuicEncryptedPacket
encrypted(AsChars(packet
), arraysize(packet
), false);
1421 EXPECT_TRUE(framer_
.ProcessPacket(encrypted
));
1423 EXPECT_EQ(QUIC_NO_ERROR
, framer_
.error());
1424 ASSERT_TRUE(visitor_
.header_
.get());
1425 EXPECT_TRUE(CheckDecryption(encrypted
, !kIncludeVersion
));
1427 ASSERT_EQ(1u, visitor_
.stream_frames_
.size());
1428 EXPECT_EQ(0u, visitor_
.ack_frames_
.size());
1429 EXPECT_EQ(static_cast<uint64
>(0x00000004),
1430 visitor_
.stream_frames_
[0]->stream_id
);
1431 EXPECT_TRUE(visitor_
.stream_frames_
[0]->fin
);
1432 EXPECT_EQ(UINT64_C(0xBA98FEDC32107654), visitor_
.stream_frames_
[0]->offset
);
1433 CheckStreamFrameData("hello world!", visitor_
.stream_frames_
[0]);
1435 // Now test framing boundaries.
1436 const size_t stream_id_size
= 1;
1437 CheckStreamFrameBoundaries(packet
, stream_id_size
, !kIncludeVersion
);
1440 TEST_P(QuicFramerTest
, StreamFrameWithVersion
) {
1441 unsigned char packet
[] = {
1442 // public flags (version, 8 byte connection_id)
1445 0x10, 0x32, 0x54, 0x76,
1446 0x98, 0xBA, 0xDC, 0xFE,
1448 'Q', '0', GetQuicVersionDigitTens(), GetQuicVersionDigitOnes(),
1449 // packet sequence number
1450 0xBC, 0x9A, 0x78, 0x56,
1455 // frame type (stream frame with fin)
1458 0x04, 0x03, 0x02, 0x01,
1460 0x54, 0x76, 0x10, 0x32,
1461 0xDC, 0xFE, 0x98, 0xBA,
1470 QuicEncryptedPacket
encrypted(AsChars(packet
), arraysize(packet
), false);
1471 EXPECT_TRUE(framer_
.ProcessPacket(encrypted
));
1473 EXPECT_EQ(QUIC_NO_ERROR
, framer_
.error());
1474 ASSERT_TRUE(visitor_
.header_
.get());
1475 EXPECT_TRUE(visitor_
.header_
->public_header
.version_flag
);
1476 EXPECT_EQ(GetParam(), visitor_
.header_
->public_header
.versions
[0]);
1477 EXPECT_TRUE(CheckDecryption(encrypted
, kIncludeVersion
));
1479 ASSERT_EQ(1u, visitor_
.stream_frames_
.size());
1480 EXPECT_EQ(0u, visitor_
.ack_frames_
.size());
1481 EXPECT_EQ(static_cast<uint64
>(0x01020304),
1482 visitor_
.stream_frames_
[0]->stream_id
);
1483 EXPECT_TRUE(visitor_
.stream_frames_
[0]->fin
);
1484 EXPECT_EQ(UINT64_C(0xBA98FEDC32107654), visitor_
.stream_frames_
[0]->offset
);
1485 CheckStreamFrameData("hello world!", visitor_
.stream_frames_
[0]);
1487 // Now test framing boundaries.
1488 CheckStreamFrameBoundaries(packet
, kQuicMaxStreamIdSize
, kIncludeVersion
);
1491 TEST_P(QuicFramerTest
, RejectPacket
) {
1492 visitor_
.accept_packet_
= false;
1494 unsigned char packet
[] = {
1495 // public flags (8 byte connection_id)
1498 0x10, 0x32, 0x54, 0x76,
1499 0x98, 0xBA, 0xDC, 0xFE,
1500 // packet sequence number
1501 0xBC, 0x9A, 0x78, 0x56,
1506 // frame type (stream frame with fin)
1509 0x04, 0x03, 0x02, 0x01,
1511 0x54, 0x76, 0x10, 0x32,
1512 0xDC, 0xFE, 0x98, 0xBA,
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_
.header_
.get());
1526 EXPECT_TRUE(CheckDecryption(encrypted
, !kIncludeVersion
));
1528 ASSERT_EQ(0u, visitor_
.stream_frames_
.size());
1529 EXPECT_EQ(0u, visitor_
.ack_frames_
.size());
1532 TEST_P(QuicFramerTest
, RejectPublicHeader
) {
1533 visitor_
.accept_public_header_
= false;
1535 unsigned char packet
[] = {
1536 // public flags (8 byte connection_id)
1539 0x10, 0x32, 0x54, 0x76,
1540 0x98, 0xBA, 0xDC, 0xFE,
1543 QuicEncryptedPacket
encrypted(AsChars(packet
), arraysize(packet
), false);
1544 EXPECT_TRUE(framer_
.ProcessPacket(encrypted
));
1546 EXPECT_EQ(QUIC_NO_ERROR
, framer_
.error());
1547 ASSERT_TRUE(visitor_
.public_header_
.get());
1548 ASSERT_FALSE(visitor_
.header_
.get());
1551 TEST_P(QuicFramerTest
, RevivedStreamFrame
) {
1552 unsigned char payload
[] = {
1553 // frame type (stream frame with fin)
1556 0x04, 0x03, 0x02, 0x01,
1558 0x54, 0x76, 0x10, 0x32,
1559 0xDC, 0xFE, 0x98, 0xBA,
1568 QuicPacketHeader header
;
1569 header
.public_header
.connection_id
= UINT64_C(0xFEDCBA9876543210);
1570 header
.public_header
.reset_flag
= false;
1571 header
.public_header
.version_flag
= false;
1572 header
.fec_flag
= true;
1573 header
.entropy_flag
= true;
1574 header
.packet_sequence_number
= UINT64_C(0x123456789ABC);
1575 header
.fec_group
= 0;
1577 // Do not encrypt the payload because the revived payload is post-encryption.
1578 EXPECT_TRUE(framer_
.ProcessRevivedPacket(&header
,
1579 StringPiece(AsChars(payload
),
1580 arraysize(payload
))));
1582 EXPECT_EQ(QUIC_NO_ERROR
, framer_
.error());
1583 ASSERT_EQ(1, visitor_
.revived_packets_
);
1584 ASSERT_TRUE(visitor_
.header_
.get());
1585 EXPECT_EQ(UINT64_C(0xFEDCBA9876543210),
1586 visitor_
.header_
->public_header
.connection_id
);
1587 EXPECT_FALSE(visitor_
.header_
->public_header
.reset_flag
);
1588 EXPECT_FALSE(visitor_
.header_
->public_header
.version_flag
);
1589 EXPECT_TRUE(visitor_
.header_
->fec_flag
);
1590 EXPECT_TRUE(visitor_
.header_
->entropy_flag
);
1591 EXPECT_EQ(1 << (header
.packet_sequence_number
% 8),
1592 visitor_
.header_
->entropy_hash
);
1593 EXPECT_EQ(UINT64_C(0x123456789ABC), visitor_
.header_
->packet_sequence_number
);
1594 EXPECT_EQ(NOT_IN_FEC_GROUP
, visitor_
.header_
->is_in_fec_group
);
1595 EXPECT_EQ(0x00u
, visitor_
.header_
->fec_group
);
1597 ASSERT_EQ(1u, visitor_
.stream_frames_
.size());
1598 EXPECT_EQ(0u, visitor_
.ack_frames_
.size());
1599 EXPECT_EQ(UINT64_C(0x01020304), visitor_
.stream_frames_
[0]->stream_id
);
1600 EXPECT_TRUE(visitor_
.stream_frames_
[0]->fin
);
1601 EXPECT_EQ(UINT64_C(0xBA98FEDC32107654), visitor_
.stream_frames_
[0]->offset
);
1602 CheckStreamFrameData("hello world!", visitor_
.stream_frames_
[0]);
1605 TEST_P(QuicFramerTest
, StreamFrameInFecGroup
) {
1606 unsigned char packet
[] = {
1607 // public flags (8 byte connection_id)
1610 0x10, 0x32, 0x54, 0x76,
1611 0x98, 0xBA, 0xDC, 0xFE,
1612 // packet sequence number
1613 0xBC, 0x9A, 0x78, 0x56,
1615 // private flags (fec group)
1617 // first fec protected packet offset
1620 // frame type (stream frame with fin)
1623 0x04, 0x03, 0x02, 0x01,
1625 0x54, 0x76, 0x10, 0x32,
1626 0xDC, 0xFE, 0x98, 0xBA,
1635 QuicEncryptedPacket
encrypted(AsChars(packet
), arraysize(packet
), false);
1636 EXPECT_TRUE(framer_
.ProcessPacket(encrypted
));
1638 EXPECT_EQ(QUIC_NO_ERROR
, framer_
.error());
1639 ASSERT_TRUE(visitor_
.header_
.get());
1640 EXPECT_TRUE(CheckDecryption(encrypted
, !kIncludeVersion
));
1641 EXPECT_EQ(IN_FEC_GROUP
, visitor_
.header_
->is_in_fec_group
);
1642 EXPECT_EQ(UINT64_C(0x341256789ABA), visitor_
.header_
->fec_group
);
1643 const size_t fec_offset
=
1644 GetStartOfFecProtectedData(PACKET_8BYTE_CONNECTION_ID
,
1646 PACKET_6BYTE_SEQUENCE_NUMBER
);
1648 string(AsChars(packet
) + fec_offset
, arraysize(packet
) - fec_offset
),
1649 visitor_
.fec_protected_payload_
);
1651 ASSERT_EQ(1u, visitor_
.stream_frames_
.size());
1652 EXPECT_EQ(0u, visitor_
.ack_frames_
.size());
1653 EXPECT_EQ(UINT64_C(0x01020304), visitor_
.stream_frames_
[0]->stream_id
);
1654 EXPECT_TRUE(visitor_
.stream_frames_
[0]->fin
);
1655 EXPECT_EQ(UINT64_C(0xBA98FEDC32107654), visitor_
.stream_frames_
[0]->offset
);
1656 CheckStreamFrameData("hello world!", visitor_
.stream_frames_
[0]);
1659 TEST_P(QuicFramerTest
, AckFrameTwoTimestamp
) {
1660 unsigned char packet
[] = {
1661 // public flags (8 byte connection_id)
1664 0x10, 0x32, 0x54, 0x76, 0x98, 0xBA, 0xDC, 0xFE,
1665 // packet sequence number
1666 0xA8, 0x9A, 0x78, 0x56, 0x34, 0x12,
1667 // private flags (entropy)
1670 // frame type (ack frame)
1671 // (has nacks, not truncated, 6 byte largest observed, 1 byte delta)
1673 // entropy hash of all received packets.
1675 // largest observed packet sequence number
1676 0xBF, 0x9A, 0x78, 0x56, 0x34, 0x12,
1679 // Number of timestamps.
1681 // Delta from largest observed.
1684 0x10, 0x32, 0x54, 0x76,
1685 // Delta from largest observed.
1689 // num missing packets
1691 // missing packet delta
1693 // 0 more missing packets in range.
1695 // Number of revived packets.
1699 QuicEncryptedPacket
encrypted(AsChars(packet
), arraysize(packet
), false);
1700 EXPECT_TRUE(framer_
.ProcessPacket(encrypted
));
1702 EXPECT_EQ(QUIC_NO_ERROR
, framer_
.error());
1703 ASSERT_TRUE(visitor_
.header_
.get());
1704 EXPECT_TRUE(CheckDecryption(encrypted
, !kIncludeVersion
));
1706 EXPECT_EQ(0u, visitor_
.stream_frames_
.size());
1707 ASSERT_EQ(1u, visitor_
.ack_frames_
.size());
1708 const QuicAckFrame
& frame
= *visitor_
.ack_frames_
[0];
1709 EXPECT_EQ(0xBA, frame
.entropy_hash
);
1710 EXPECT_EQ(UINT64_C(0x0123456789ABF), frame
.largest_observed
);
1711 ASSERT_EQ(1u, frame
.missing_packets
.size());
1712 ASSERT_EQ(2u, frame
.received_packet_times
.size());
1713 SequenceNumberSet::const_iterator missing_iter
=
1714 frame
.missing_packets
.begin();
1715 EXPECT_EQ(UINT64_C(0x0123456789ABE), *missing_iter
);
1717 const size_t kReceivedEntropyOffset
= kQuicFrameTypeSize
;
1718 const size_t kLargestObservedOffset
= kReceivedEntropyOffset
+
1719 kQuicEntropyHashSize
;
1720 const size_t kMissingDeltaTimeOffset
= kLargestObservedOffset
+
1721 PACKET_6BYTE_SEQUENCE_NUMBER
;
1722 const size_t kNumTimestampsOffset
= kMissingDeltaTimeOffset
+
1723 kQuicDeltaTimeLargestObservedSize
;
1724 const size_t kTimestampDeltaLargestObserved1
= kNumTimestampsOffset
+
1725 kQuicNumTimestampsSize
;
1726 const size_t kTimestampTimeDeltaLargestObserved1
=
1727 kTimestampDeltaLargestObserved1
+ 1;
1728 const size_t kTimestampDeltaLargestObserved2
=
1729 kTimestampTimeDeltaLargestObserved1
+ 4;
1730 const size_t kTimestampTimeDeltaLargestObserved2
=
1731 kTimestampDeltaLargestObserved2
+ 1;
1732 const size_t kNumMissingPacketOffset
=
1733 kTimestampTimeDeltaLargestObserved2
+ 2;
1734 const size_t kMissingPacketsOffset
= kNumMissingPacketOffset
+
1735 kNumberOfNackRangesSize
;
1736 const size_t kMissingPacketsRange
= kMissingPacketsOffset
+
1737 PACKET_1BYTE_SEQUENCE_NUMBER
;
1738 const size_t kRevivedPacketsLength
= kMissingPacketsRange
+
1739 PACKET_1BYTE_SEQUENCE_NUMBER
;
1740 // Now test framing boundaries.
1741 const size_t ack_frame_size
= kRevivedPacketsLength
+
1742 PACKET_1BYTE_SEQUENCE_NUMBER
;
1743 for (size_t i
= kQuicFrameTypeSize
; i
< ack_frame_size
; ++i
) {
1744 string expected_error
;
1745 if (i
< kLargestObservedOffset
) {
1746 expected_error
= "Unable to read entropy hash for received packets.";
1747 } else if (i
< kMissingDeltaTimeOffset
) {
1748 expected_error
= "Unable to read largest observed.";
1749 } else if (i
< kNumTimestampsOffset
) {
1750 expected_error
= "Unable to read delta time largest observed.";
1751 } else if (i
< kTimestampDeltaLargestObserved1
) {
1752 expected_error
= "Unable to read num received packets.";
1753 } else if (i
< kTimestampTimeDeltaLargestObserved1
) {
1754 expected_error
= "Unable to read sequence delta in received packets.";
1755 } else if (i
< kTimestampDeltaLargestObserved2
) {
1756 expected_error
= "Unable to read time delta in received packets.";
1757 } else if (i
< kTimestampTimeDeltaLargestObserved2
) {
1758 expected_error
= "Unable to read sequence delta in received packets.";
1759 } else if (i
< kNumMissingPacketOffset
) {
1761 "Unable to read incremental time delta in received packets.";
1762 } else if (i
< kMissingPacketsOffset
) {
1763 expected_error
= "Unable to read num missing packet ranges.";
1764 } else if (i
< kMissingPacketsRange
) {
1765 expected_error
= "Unable to read missing sequence number delta.";
1766 } else if (i
< kRevivedPacketsLength
) {
1767 expected_error
= "Unable to read missing sequence number range.";
1769 expected_error
= "Unable to read num revived packets.";
1771 CheckProcessingFails(
1773 i
+ GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID
, !kIncludeVersion
,
1774 PACKET_6BYTE_SEQUENCE_NUMBER
, NOT_IN_FEC_GROUP
),
1775 expected_error
, QUIC_INVALID_ACK_DATA
);
1780 TEST_P(QuicFramerTest
, AckFrameOneTimestamp
) {
1781 unsigned char packet
[] = {
1782 // public flags (8 byte connection_id)
1785 0x10, 0x32, 0x54, 0x76, 0x98, 0xBA, 0xDC, 0xFE,
1786 // packet sequence number
1787 0xA8, 0x9A, 0x78, 0x56, 0x34, 0x12,
1788 // private flags (entropy)
1791 // frame type (ack frame)
1792 // (has nacks, not truncated, 6 byte largest observed, 1 byte delta)
1794 // entropy hash of all received packets.
1796 // largest observed packet sequence number
1797 0xBF, 0x9A, 0x78, 0x56, 0x34, 0x12,
1800 // Number of timestamps.
1802 // Delta from largest observed.
1805 0x10, 0x32, 0x54, 0x76,
1806 // num missing packets
1808 // missing packet delta
1810 // 0 more missing packets in range.
1812 // Number of revived packets.
1816 QuicEncryptedPacket
encrypted(AsChars(packet
), arraysize(packet
), false);
1817 EXPECT_TRUE(framer_
.ProcessPacket(encrypted
));
1819 EXPECT_EQ(QUIC_NO_ERROR
, framer_
.error());
1820 ASSERT_TRUE(visitor_
.header_
.get());
1821 EXPECT_TRUE(CheckDecryption(encrypted
, !kIncludeVersion
));
1823 EXPECT_EQ(0u, visitor_
.stream_frames_
.size());
1824 ASSERT_EQ(1u, visitor_
.ack_frames_
.size());
1825 const QuicAckFrame
& frame
= *visitor_
.ack_frames_
[0];
1826 EXPECT_EQ(0xBA, frame
.entropy_hash
);
1827 EXPECT_EQ(UINT64_C(0x0123456789ABF), frame
.largest_observed
);
1828 ASSERT_EQ(1u, frame
.missing_packets
.size());
1829 ASSERT_EQ(1u, frame
.received_packet_times
.size());
1830 SequenceNumberSet::const_iterator missing_iter
=
1831 frame
.missing_packets
.begin();
1832 EXPECT_EQ(UINT64_C(0x0123456789ABE), *missing_iter
);
1834 const size_t kReceivedEntropyOffset
= kQuicFrameTypeSize
;
1835 const size_t kLargestObservedOffset
= kReceivedEntropyOffset
+
1836 kQuicEntropyHashSize
;
1837 const size_t kMissingDeltaTimeOffset
= kLargestObservedOffset
+
1838 PACKET_6BYTE_SEQUENCE_NUMBER
;
1839 const size_t kNumTimestampsOffset
= kMissingDeltaTimeOffset
+
1840 kQuicDeltaTimeLargestObservedSize
;
1841 const size_t kTimestampDeltaLargestObserved
= kNumTimestampsOffset
+
1842 kQuicNumTimestampsSize
;
1843 const size_t kTimestampTimeDeltaLargestObserved
=
1844 kTimestampDeltaLargestObserved
+ 1;
1845 const size_t kNumMissingPacketOffset
= kTimestampTimeDeltaLargestObserved
+ 4;
1846 const size_t kMissingPacketsOffset
= kNumMissingPacketOffset
+
1847 kNumberOfNackRangesSize
;
1848 const size_t kMissingPacketsRange
= kMissingPacketsOffset
+
1849 PACKET_1BYTE_SEQUENCE_NUMBER
;
1850 const size_t kRevivedPacketsLength
= kMissingPacketsRange
+
1851 PACKET_1BYTE_SEQUENCE_NUMBER
;
1852 // Now test framing boundaries.
1853 const size_t ack_frame_size
= kRevivedPacketsLength
+
1854 PACKET_1BYTE_SEQUENCE_NUMBER
;
1855 for (size_t i
= kQuicFrameTypeSize
; i
< ack_frame_size
; ++i
) {
1856 string expected_error
;
1857 if (i
< kLargestObservedOffset
) {
1858 expected_error
= "Unable to read entropy hash for received packets.";
1859 } else if (i
< kMissingDeltaTimeOffset
) {
1860 expected_error
= "Unable to read largest observed.";
1861 } else if (i
< kNumTimestampsOffset
) {
1862 expected_error
= "Unable to read delta time largest observed.";
1863 } else if (i
< kTimestampDeltaLargestObserved
) {
1864 expected_error
= "Unable to read num received packets.";
1865 } else if (i
< kTimestampTimeDeltaLargestObserved
) {
1866 expected_error
= "Unable to read sequence delta in received packets.";
1867 } else if (i
< kNumMissingPacketOffset
) {
1868 expected_error
= "Unable to read time delta in received packets.";
1869 } else if (i
< kMissingPacketsOffset
) {
1870 expected_error
= "Unable to read num missing packet ranges.";
1871 } else if (i
< kMissingPacketsRange
) {
1872 expected_error
= "Unable to read missing sequence number delta.";
1873 } else if (i
< kRevivedPacketsLength
) {
1874 expected_error
= "Unable to read missing sequence number range.";
1876 expected_error
= "Unable to read num revived packets.";
1878 CheckProcessingFails(
1880 i
+ GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID
, !kIncludeVersion
,
1881 PACKET_6BYTE_SEQUENCE_NUMBER
, NOT_IN_FEC_GROUP
),
1882 expected_error
, QUIC_INVALID_ACK_DATA
);
1887 TEST_P(QuicFramerTest
, AckFrame
) {
1888 unsigned char packet
[] = {
1889 // public flags (8 byte connection_id)
1892 0x10, 0x32, 0x54, 0x76, 0x98, 0xBA, 0xDC, 0xFE,
1893 // packet sequence number
1894 0xA8, 0x9A, 0x78, 0x56, 0x34, 0x12,
1895 // private flags (entropy)
1898 // frame type (ack frame)
1899 // (has nacks, not truncated, 6 byte largest observed, 1 byte delta)
1901 // entropy hash of all received packets.
1903 // largest observed packet sequence number
1904 0xBF, 0x9A, 0x78, 0x56, 0x34, 0x12,
1907 // Number of timestamps.
1909 // num missing packets
1911 // missing packet delta
1913 // 0 more missing packets in range.
1915 // Number of revived packets.
1919 QuicEncryptedPacket
encrypted(AsChars(packet
), arraysize(packet
), false);
1920 EXPECT_TRUE(framer_
.ProcessPacket(encrypted
));
1922 EXPECT_EQ(QUIC_NO_ERROR
, framer_
.error());
1923 ASSERT_TRUE(visitor_
.header_
.get());
1924 EXPECT_TRUE(CheckDecryption(encrypted
, !kIncludeVersion
));
1926 EXPECT_EQ(0u, visitor_
.stream_frames_
.size());
1927 ASSERT_EQ(1u, visitor_
.ack_frames_
.size());
1928 const QuicAckFrame
& frame
= *visitor_
.ack_frames_
[0];
1929 EXPECT_EQ(0xBA, frame
.entropy_hash
);
1930 EXPECT_EQ(UINT64_C(0x0123456789ABF), frame
.largest_observed
);
1931 ASSERT_EQ(1u, frame
.missing_packets
.size());
1932 SequenceNumberSet::const_iterator missing_iter
=
1933 frame
.missing_packets
.begin();
1934 EXPECT_EQ(UINT64_C(0x0123456789ABE), *missing_iter
);
1936 const size_t kReceivedEntropyOffset
= kQuicFrameTypeSize
;
1937 const size_t kLargestObservedOffset
= kReceivedEntropyOffset
+
1938 kQuicEntropyHashSize
;
1939 const size_t kMissingDeltaTimeOffset
= kLargestObservedOffset
+
1940 PACKET_6BYTE_SEQUENCE_NUMBER
;
1941 const size_t kNumTimestampsOffset
= kMissingDeltaTimeOffset
+
1942 kQuicDeltaTimeLargestObservedSize
;
1943 const size_t kNumMissingPacketOffset
= kNumTimestampsOffset
+
1944 kQuicNumTimestampsSize
;
1945 const size_t kMissingPacketsOffset
= kNumMissingPacketOffset
+
1946 kNumberOfNackRangesSize
;
1947 const size_t kMissingPacketsRange
= kMissingPacketsOffset
+
1948 PACKET_1BYTE_SEQUENCE_NUMBER
;
1949 const size_t kRevivedPacketsLength
= kMissingPacketsRange
+
1950 PACKET_1BYTE_SEQUENCE_NUMBER
;
1951 // Now test framing boundaries.
1952 const size_t ack_frame_size
= kRevivedPacketsLength
+
1953 PACKET_1BYTE_SEQUENCE_NUMBER
;
1954 for (size_t i
= kQuicFrameTypeSize
; i
< ack_frame_size
; ++i
) {
1955 string expected_error
;
1956 if (i
< kLargestObservedOffset
) {
1957 expected_error
= "Unable to read entropy hash for received packets.";
1958 } else if (i
< kMissingDeltaTimeOffset
) {
1959 expected_error
= "Unable to read largest observed.";
1960 } else if (i
< kNumTimestampsOffset
) {
1961 expected_error
= "Unable to read delta time largest observed.";
1962 } else if (i
< kNumMissingPacketOffset
) {
1963 expected_error
= "Unable to read num received packets.";
1964 } else if (i
< kMissingPacketsOffset
) {
1965 expected_error
= "Unable to read num missing packet ranges.";
1966 } else if (i
< kMissingPacketsRange
) {
1967 expected_error
= "Unable to read missing sequence number delta.";
1968 } else if (i
< kRevivedPacketsLength
) {
1969 expected_error
= "Unable to read missing sequence number range.";
1971 expected_error
= "Unable to read num revived packets.";
1973 CheckProcessingFails(
1975 i
+ GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID
, !kIncludeVersion
,
1976 PACKET_6BYTE_SEQUENCE_NUMBER
, NOT_IN_FEC_GROUP
),
1977 expected_error
, QUIC_INVALID_ACK_DATA
);
1981 TEST_P(QuicFramerTest
, AckFrameRevivedPackets
) {
1982 unsigned char packet
[] = {
1983 // public flags (8 byte connection_id)
1986 0x10, 0x32, 0x54, 0x76, 0x98, 0xBA, 0xDC, 0xFE,
1987 // packet sequence number
1988 0xA8, 0x9A, 0x78, 0x56, 0x34, 0x12,
1989 // private flags (entropy)
1992 // frame type (ack frame)
1993 // (has nacks, not truncated, 6 byte largest observed, 1 byte delta)
1995 // entropy hash of all received packets.
1997 // largest observed packet sequence number
1998 0xBF, 0x9A, 0x78, 0x56, 0x34, 0x12,
2001 // num received packets.
2003 // num missing packets
2005 // missing packet delta
2007 // 0 more missing packets in range.
2009 // Number of revived packets.
2011 // Revived packet sequence number.
2012 0xBE, 0x9A, 0x78, 0x56, 0x34, 0x12,
2013 // Number of revived packets.
2017 QuicEncryptedPacket
encrypted(AsChars(packet
), arraysize(packet
), false);
2018 EXPECT_TRUE(framer_
.ProcessPacket(encrypted
));
2020 EXPECT_EQ(QUIC_NO_ERROR
, framer_
.error());
2021 ASSERT_TRUE(visitor_
.header_
.get());
2022 EXPECT_TRUE(CheckDecryption(encrypted
, !kIncludeVersion
));
2024 EXPECT_EQ(0u, visitor_
.stream_frames_
.size());
2025 ASSERT_EQ(1u, visitor_
.ack_frames_
.size());
2026 const QuicAckFrame
& frame
= *visitor_
.ack_frames_
[0];
2027 EXPECT_EQ(0xBA, frame
.entropy_hash
);
2028 EXPECT_EQ(UINT64_C(0x0123456789ABF), frame
.largest_observed
);
2029 ASSERT_EQ(1u, frame
.missing_packets
.size());
2030 SequenceNumberSet::const_iterator missing_iter
=
2031 frame
.missing_packets
.begin();
2032 EXPECT_EQ(UINT64_C(0x0123456789ABE), *missing_iter
);
2034 const size_t kReceivedEntropyOffset
= kQuicFrameTypeSize
;
2035 const size_t kLargestObservedOffset
= kReceivedEntropyOffset
+
2036 kQuicEntropyHashSize
;
2037 const size_t kMissingDeltaTimeOffset
= kLargestObservedOffset
+
2038 PACKET_6BYTE_SEQUENCE_NUMBER
;
2039 const size_t kNumTimestampsOffset
= kMissingDeltaTimeOffset
+
2040 kQuicDeltaTimeLargestObservedSize
;
2041 const size_t kNumMissingPacketOffset
= kNumTimestampsOffset
+
2042 kQuicNumTimestampsSize
;
2043 const size_t kMissingPacketsOffset
= kNumMissingPacketOffset
+
2044 kNumberOfNackRangesSize
;
2045 const size_t kMissingPacketsRange
= kMissingPacketsOffset
+
2046 PACKET_1BYTE_SEQUENCE_NUMBER
;
2047 const size_t kRevivedPacketsLength
= kMissingPacketsRange
+
2048 PACKET_1BYTE_SEQUENCE_NUMBER
;
2049 const size_t kRevivedPacketSequenceNumberLength
= kRevivedPacketsLength
+
2050 PACKET_1BYTE_SEQUENCE_NUMBER
;
2051 // Now test framing boundaries.
2052 const size_t ack_frame_size
= kRevivedPacketSequenceNumberLength
+
2053 PACKET_6BYTE_SEQUENCE_NUMBER
;
2054 for (size_t i
= kQuicFrameTypeSize
; i
< ack_frame_size
; ++i
) {
2055 string expected_error
;
2056 if (i
< kReceivedEntropyOffset
) {
2057 expected_error
= "Unable to read least unacked delta.";
2058 } else if (i
< kLargestObservedOffset
) {
2059 expected_error
= "Unable to read entropy hash for received packets.";
2060 } else if (i
< kMissingDeltaTimeOffset
) {
2061 expected_error
= "Unable to read largest observed.";
2062 } else if (i
< kNumTimestampsOffset
) {
2063 expected_error
= "Unable to read delta time largest observed.";
2064 } else if (i
< kNumMissingPacketOffset
) {
2065 expected_error
= "Unable to read num received packets.";
2066 } else if (i
< kMissingPacketsOffset
) {
2067 expected_error
= "Unable to read num missing packet ranges.";
2068 } else if (i
< kMissingPacketsRange
) {
2069 expected_error
= "Unable to read missing sequence number delta.";
2070 } else if (i
< kRevivedPacketsLength
) {
2071 expected_error
= "Unable to read missing sequence number range.";
2072 } else if (i
< kRevivedPacketSequenceNumberLength
) {
2073 expected_error
= "Unable to read num revived packets.";
2075 expected_error
= "Unable to read revived packet.";
2077 CheckProcessingFails(
2079 i
+ GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID
, !kIncludeVersion
,
2080 PACKET_6BYTE_SEQUENCE_NUMBER
, NOT_IN_FEC_GROUP
),
2081 expected_error
, QUIC_INVALID_ACK_DATA
);
2085 TEST_P(QuicFramerTest
, AckFrameNoNacks
) {
2086 unsigned char packet
[] = {
2087 // public flags (8 byte connection_id)
2090 0x10, 0x32, 0x54, 0x76, 0x98, 0xBA, 0xDC, 0xFE,
2091 // packet sequence number
2092 0xA8, 0x9A, 0x78, 0x56, 0x34, 0x12,
2093 // private flags (entropy)
2096 // frame type (ack frame)
2097 // (no nacks, not truncated, 6 byte largest observed, 1 byte delta)
2099 // entropy hash of all received packets.
2101 // largest observed packet sequence number
2102 0xBF, 0x9A, 0x78, 0x56, 0x34, 0x12,
2105 // Number of received packets.
2109 QuicEncryptedPacket
encrypted(AsChars(packet
), arraysize(packet
), false);
2110 EXPECT_TRUE(framer_
.ProcessPacket(encrypted
));
2112 EXPECT_EQ(QUIC_NO_ERROR
, framer_
.error());
2113 ASSERT_TRUE(visitor_
.header_
.get());
2114 EXPECT_TRUE(CheckDecryption(encrypted
, !kIncludeVersion
));
2116 EXPECT_EQ(0u, visitor_
.stream_frames_
.size());
2117 ASSERT_EQ(1u, visitor_
.ack_frames_
.size());
2118 QuicAckFrame
* frame
= visitor_
.ack_frames_
[0];
2119 EXPECT_EQ(0xBA, frame
->entropy_hash
);
2120 EXPECT_EQ(UINT64_C(0x0123456789ABF), frame
->largest_observed
);
2121 ASSERT_EQ(0u, frame
->missing_packets
.size());
2123 // Verify that the packet re-serializes identically.
2125 frames
.push_back(QuicFrame(frame
));
2126 scoped_ptr
<QuicPacket
> data(BuildDataPacket(*visitor_
.header_
, frames
));
2127 ASSERT_TRUE(data
!= nullptr);
2129 test::CompareCharArraysWithHexError("constructed packet", data
->data(),
2130 data
->length(), AsChars(packet
),
2134 TEST_P(QuicFramerTest
, AckFrame500Nacks
) {
2135 unsigned char packet
[] = {
2136 // public flags (8 byte connection_id)
2139 0x10, 0x32, 0x54, 0x76, 0x98, 0xBA, 0xDC, 0xFE,
2140 // packet sequence number
2141 0xA8, 0x9A, 0x78, 0x56, 0x34, 0x12,
2142 // private flags (entropy)
2145 // frame type (ack frame)
2146 // (has nacks, not truncated, 6 byte largest observed, 1 byte delta)
2148 // entropy hash of all received packets.
2150 // largest observed packet sequence number
2151 0xBF, 0x9A, 0x78, 0x56, 0x34, 0x12,
2154 // No received packets.
2156 // num missing packet ranges
2158 // missing packet delta
2160 // 243 more missing packets in range.
2161 // The ranges are listed in this order so the re-constructed packet
2164 // No gap between ranges
2166 // 255 more missing packets in range.
2168 // No revived packets.
2172 QuicEncryptedPacket
encrypted(AsChars(packet
), arraysize(packet
), false);
2173 EXPECT_TRUE(framer_
.ProcessPacket(encrypted
));
2175 EXPECT_EQ(QUIC_NO_ERROR
, framer_
.error());
2176 ASSERT_TRUE(visitor_
.header_
.get());
2177 EXPECT_TRUE(CheckDecryption(encrypted
, !kIncludeVersion
));
2179 EXPECT_EQ(0u, visitor_
.stream_frames_
.size());
2180 ASSERT_EQ(1u, visitor_
.ack_frames_
.size());
2181 QuicAckFrame
* frame
= visitor_
.ack_frames_
[0];
2182 EXPECT_EQ(0xBA, frame
->entropy_hash
);
2183 EXPECT_EQ(UINT64_C(0x0123456789ABF), frame
->largest_observed
);
2184 EXPECT_EQ(0u, frame
->revived_packets
.size());
2185 ASSERT_EQ(500u, frame
->missing_packets
.size());
2186 SequenceNumberSet::const_iterator first_missing_iter
=
2187 frame
->missing_packets
.begin();
2188 EXPECT_EQ(UINT64_C(0x0123456789ABE) - 499, *first_missing_iter
);
2189 SequenceNumberSet::const_reverse_iterator last_missing_iter
=
2190 frame
->missing_packets
.rbegin();
2191 EXPECT_EQ(UINT64_C(0x0123456789ABE), *last_missing_iter
);
2193 // Verify that the packet re-serializes identically.
2195 frames
.push_back(QuicFrame(frame
));
2196 scoped_ptr
<QuicPacket
> data(BuildDataPacket(*visitor_
.header_
, frames
));
2197 ASSERT_TRUE(data
!= nullptr);
2199 test::CompareCharArraysWithHexError("constructed packet",
2200 data
->data(), data
->length(),
2201 AsChars(packet
), arraysize(packet
));
2204 TEST_P(QuicFramerTest
, StopWaitingFrame
) {
2205 unsigned char packet
[] = {
2206 // public flags (8 byte connection_id)
2209 0x10, 0x32, 0x54, 0x76,
2210 0x98, 0xBA, 0xDC, 0xFE,
2211 // packet sequence number
2212 0xA8, 0x9A, 0x78, 0x56,
2214 // private flags (entropy)
2217 // frame type (ack frame)
2218 // (has nacks, not truncated, 6 byte largest observed, 1 byte delta)
2220 // entropy hash of sent packets till least awaiting - 1.
2222 // least packet sequence number awaiting an ack, delta from sequence number.
2223 0x08, 0x00, 0x00, 0x00,
2227 QuicEncryptedPacket
encrypted(AsChars(packet
), arraysize(packet
), false);
2228 EXPECT_TRUE(framer_
.ProcessPacket(encrypted
));
2230 EXPECT_EQ(QUIC_NO_ERROR
, framer_
.error());
2231 ASSERT_TRUE(visitor_
.header_
.get());
2232 EXPECT_TRUE(CheckDecryption(encrypted
, !kIncludeVersion
));
2234 EXPECT_EQ(0u, visitor_
.stream_frames_
.size());
2235 ASSERT_EQ(1u, visitor_
.stop_waiting_frames_
.size());
2236 const QuicStopWaitingFrame
& frame
= *visitor_
.stop_waiting_frames_
[0];
2237 EXPECT_EQ(0xAB, frame
.entropy_hash
);
2238 EXPECT_EQ(UINT64_C(0x0123456789AA0), frame
.least_unacked
);
2240 const size_t kSentEntropyOffset
= kQuicFrameTypeSize
;
2241 const size_t kLeastUnackedOffset
= kSentEntropyOffset
+ kQuicEntropyHashSize
;
2242 const size_t frame_size
= 7;
2243 for (size_t i
= kQuicFrameTypeSize
; i
< frame_size
; ++i
) {
2244 string expected_error
;
2245 if (i
< kLeastUnackedOffset
) {
2246 expected_error
= "Unable to read entropy hash for sent packets.";
2248 expected_error
= "Unable to read least unacked delta.";
2250 CheckProcessingFails(
2252 i
+ GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID
, !kIncludeVersion
,
2253 PACKET_6BYTE_SEQUENCE_NUMBER
, NOT_IN_FEC_GROUP
),
2254 expected_error
, QUIC_INVALID_STOP_WAITING_DATA
);
2258 TEST_P(QuicFramerTest
, RstStreamFrameQuicVersion24
) {
2259 if (version_
> QUIC_VERSION_24
) {
2260 // QUIC_VERSION_25 removes the error_details field from QuicRstStreamFrame.
2264 unsigned char packet
[] = {
2265 // public flags (8 byte connection_id)
2268 0x10, 0x32, 0x54, 0x76,
2269 0x98, 0xBA, 0xDC, 0xFE,
2270 // packet sequence number
2271 0xBC, 0x9A, 0x78, 0x56,
2276 // frame type (rst stream frame)
2279 0x04, 0x03, 0x02, 0x01,
2282 0x01, 0x02, 0x03, 0x04,
2283 0x05, 0x06, 0x07, 0x08,
2286 0x01, 0x00, 0x00, 0x00,
2288 // error details length
2297 QuicEncryptedPacket
encrypted(AsChars(packet
), arraysize(packet
), false);
2298 EXPECT_TRUE(framer_
.ProcessPacket(encrypted
));
2300 EXPECT_EQ(QUIC_NO_ERROR
, framer_
.error());
2301 ASSERT_TRUE(visitor_
.header_
.get());
2302 EXPECT_TRUE(CheckDecryption(encrypted
, !kIncludeVersion
));
2304 EXPECT_EQ(UINT64_C(0x01020304), visitor_
.rst_stream_frame_
.stream_id
);
2305 EXPECT_EQ(0x01, visitor_
.rst_stream_frame_
.error_code
);
2306 EXPECT_EQ("because I can", visitor_
.rst_stream_frame_
.error_details
);
2307 EXPECT_EQ(UINT64_C(0x0807060504030201),
2308 visitor_
.rst_stream_frame_
.byte_offset
);
2310 // Now test framing boundaries.
2311 for (size_t i
= kQuicFrameTypeSize
;
2312 i
< QuicFramer::GetMinRstStreamFrameSize(); ++i
) {
2313 string expected_error
;
2314 if (i
< kQuicFrameTypeSize
+ kQuicMaxStreamIdSize
) {
2315 expected_error
= "Unable to read stream_id.";
2316 } else if (i
< kQuicFrameTypeSize
+ kQuicMaxStreamIdSize
+
2317 kQuicMaxStreamOffsetSize
) {
2318 expected_error
= "Unable to read rst stream sent byte offset.";
2319 } else if (i
< kQuicFrameTypeSize
+ kQuicMaxStreamIdSize
+
2320 kQuicMaxStreamOffsetSize
+ kQuicErrorCodeSize
) {
2321 expected_error
= "Unable to read rst stream error code.";
2323 expected_error
= "Unable to read rst stream error details.";
2325 CheckProcessingFails(
2327 i
+ GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID
, !kIncludeVersion
,
2328 PACKET_6BYTE_SEQUENCE_NUMBER
, NOT_IN_FEC_GROUP
),
2329 expected_error
, QUIC_INVALID_RST_STREAM_DATA
);
2333 TEST_P(QuicFramerTest
, RstStreamFrameQuic
) {
2334 if (version_
<= QUIC_VERSION_24
) {
2335 // QUIC_VERSION_25 removes the error_details field from QuicRstStreamFrame.
2340 unsigned char packet
[] = {
2341 // public flags (8 byte connection_id)
2344 0x10, 0x32, 0x54, 0x76,
2345 0x98, 0xBA, 0xDC, 0xFE,
2346 // packet sequence number
2347 0xBC, 0x9A, 0x78, 0x56,
2352 // frame type (rst stream frame)
2355 0x04, 0x03, 0x02, 0x01,
2358 0x01, 0x02, 0x03, 0x04,
2359 0x05, 0x06, 0x07, 0x08,
2362 0x01, 0x00, 0x00, 0x00,
2366 QuicEncryptedPacket
encrypted(AsChars(packet
), arraysize(packet
), false);
2367 EXPECT_TRUE(framer_
.ProcessPacket(encrypted
));
2369 EXPECT_EQ(QUIC_NO_ERROR
, framer_
.error());
2370 ASSERT_TRUE(visitor_
.header_
.get());
2371 EXPECT_TRUE(CheckDecryption(encrypted
, !kIncludeVersion
));
2373 EXPECT_EQ(UINT64_C(0x01020304), visitor_
.rst_stream_frame_
.stream_id
);
2374 EXPECT_EQ(0x01, visitor_
.rst_stream_frame_
.error_code
);
2375 EXPECT_EQ(UINT64_C(0x0807060504030201),
2376 visitor_
.rst_stream_frame_
.byte_offset
);
2378 // Now test framing boundaries.
2379 for (size_t i
= kQuicFrameTypeSize
; i
< QuicFramer::GetRstStreamFrameSize();
2381 string expected_error
;
2382 if (i
< kQuicFrameTypeSize
+ kQuicMaxStreamIdSize
) {
2383 expected_error
= "Unable to read stream_id.";
2384 } else if (i
< kQuicFrameTypeSize
+ kQuicMaxStreamIdSize
+
2385 kQuicMaxStreamOffsetSize
) {
2386 expected_error
= "Unable to read rst stream sent byte offset.";
2387 } else if (i
< kQuicFrameTypeSize
+ kQuicMaxStreamIdSize
+
2388 kQuicMaxStreamOffsetSize
+ kQuicErrorCodeSize
) {
2389 expected_error
= "Unable to read rst stream error code.";
2391 CheckProcessingFails(
2393 i
+ GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID
, !kIncludeVersion
,
2394 PACKET_6BYTE_SEQUENCE_NUMBER
, NOT_IN_FEC_GROUP
),
2395 expected_error
, QUIC_INVALID_RST_STREAM_DATA
);
2399 TEST_P(QuicFramerTest
, ConnectionCloseFrame
) {
2400 unsigned char packet
[] = {
2401 // public flags (8 byte connection_id)
2404 0x10, 0x32, 0x54, 0x76,
2405 0x98, 0xBA, 0xDC, 0xFE,
2406 // packet sequence number
2407 0xBC, 0x9A, 0x78, 0x56,
2412 // frame type (connection close frame)
2415 0x11, 0x00, 0x00, 0x00,
2417 // error details length
2426 QuicEncryptedPacket
encrypted(AsChars(packet
), arraysize(packet
), false);
2427 EXPECT_TRUE(framer_
.ProcessPacket(encrypted
));
2429 EXPECT_EQ(QUIC_NO_ERROR
, framer_
.error());
2430 ASSERT_TRUE(visitor_
.header_
.get());
2431 EXPECT_TRUE(CheckDecryption(encrypted
, !kIncludeVersion
));
2433 EXPECT_EQ(0u, visitor_
.stream_frames_
.size());
2435 EXPECT_EQ(0x11, visitor_
.connection_close_frame_
.error_code
);
2436 EXPECT_EQ("because I can", visitor_
.connection_close_frame_
.error_details
);
2438 ASSERT_EQ(0u, visitor_
.ack_frames_
.size());
2440 // Now test framing boundaries.
2441 for (size_t i
= kQuicFrameTypeSize
;
2442 i
< QuicFramer::GetMinConnectionCloseFrameSize(); ++i
) {
2443 string expected_error
;
2444 if (i
< kQuicFrameTypeSize
+ kQuicErrorCodeSize
) {
2445 expected_error
= "Unable to read connection close error code.";
2447 expected_error
= "Unable to read connection close error details.";
2449 CheckProcessingFails(
2451 i
+ GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID
, !kIncludeVersion
,
2452 PACKET_6BYTE_SEQUENCE_NUMBER
, NOT_IN_FEC_GROUP
),
2453 expected_error
, QUIC_INVALID_CONNECTION_CLOSE_DATA
);
2457 TEST_P(QuicFramerTest
, GoAwayFrame
) {
2458 unsigned char packet
[] = {
2459 // public flags (8 byte connection_id)
2462 0x10, 0x32, 0x54, 0x76,
2463 0x98, 0xBA, 0xDC, 0xFE,
2464 // packet sequence number
2465 0xBC, 0x9A, 0x78, 0x56,
2470 // frame type (go away frame)
2473 0x09, 0x00, 0x00, 0x00,
2475 0x04, 0x03, 0x02, 0x01,
2476 // error details length
2485 QuicEncryptedPacket
encrypted(AsChars(packet
), arraysize(packet
), false);
2486 EXPECT_TRUE(framer_
.ProcessPacket(encrypted
));
2488 EXPECT_EQ(QUIC_NO_ERROR
, framer_
.error());
2489 ASSERT_TRUE(visitor_
.header_
.get());
2490 EXPECT_TRUE(CheckDecryption(encrypted
, !kIncludeVersion
));
2492 EXPECT_EQ(UINT64_C(0x01020304), visitor_
.goaway_frame_
.last_good_stream_id
);
2493 EXPECT_EQ(0x9, visitor_
.goaway_frame_
.error_code
);
2494 EXPECT_EQ("because I can", visitor_
.goaway_frame_
.reason_phrase
);
2496 const size_t reason_size
= arraysize("because I can") - 1;
2497 // Now test framing boundaries.
2498 for (size_t i
= kQuicFrameTypeSize
;
2499 i
< QuicFramer::GetMinGoAwayFrameSize() + reason_size
; ++i
) {
2500 string expected_error
;
2501 if (i
< kQuicFrameTypeSize
+ kQuicErrorCodeSize
) {
2502 expected_error
= "Unable to read go away error code.";
2503 } else if (i
< kQuicFrameTypeSize
+ kQuicErrorCodeSize
+
2504 kQuicMaxStreamIdSize
) {
2505 expected_error
= "Unable to read last good stream id.";
2507 expected_error
= "Unable to read goaway reason.";
2509 CheckProcessingFails(
2511 i
+ GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID
, !kIncludeVersion
,
2512 PACKET_6BYTE_SEQUENCE_NUMBER
, NOT_IN_FEC_GROUP
),
2513 expected_error
, QUIC_INVALID_GOAWAY_DATA
);
2517 TEST_P(QuicFramerTest
, WindowUpdateFrame
) {
2518 unsigned char packet
[] = {
2519 // public flags (8 byte connection_id)
2522 0x10, 0x32, 0x54, 0x76,
2523 0x98, 0xBA, 0xDC, 0xFE,
2524 // packet sequence number
2525 0xBC, 0x9A, 0x78, 0x56,
2530 // frame type (window update frame)
2533 0x04, 0x03, 0x02, 0x01,
2535 0x05, 0x06, 0x07, 0x08,
2536 0x09, 0x0a, 0x0b, 0x0c,
2539 QuicEncryptedPacket
encrypted(AsChars(packet
), arraysize(packet
), false);
2541 EXPECT_TRUE(framer_
.ProcessPacket(encrypted
));
2543 EXPECT_EQ(QUIC_NO_ERROR
, framer_
.error());
2544 ASSERT_TRUE(visitor_
.header_
.get());
2545 EXPECT_TRUE(CheckDecryption(encrypted
, !kIncludeVersion
));
2547 EXPECT_EQ(UINT64_C(0x01020304), visitor_
.window_update_frame_
.stream_id
);
2548 EXPECT_EQ(UINT64_C(0x0c0b0a0908070605),
2549 visitor_
.window_update_frame_
.byte_offset
);
2551 // Now test framing boundaries.
2552 for (size_t i
= kQuicFrameTypeSize
;
2553 i
< QuicFramer::GetWindowUpdateFrameSize(); ++i
) {
2554 string expected_error
;
2555 if (i
< kQuicFrameTypeSize
+ kQuicMaxStreamIdSize
) {
2556 expected_error
= "Unable to read stream_id.";
2558 expected_error
= "Unable to read window byte_offset.";
2560 CheckProcessingFails(
2562 i
+ GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID
, !kIncludeVersion
,
2563 PACKET_6BYTE_SEQUENCE_NUMBER
, NOT_IN_FEC_GROUP
),
2564 expected_error
, QUIC_INVALID_WINDOW_UPDATE_DATA
);
2568 TEST_P(QuicFramerTest
, BlockedFrame
) {
2569 unsigned char packet
[] = {
2570 // public flags (8 byte connection_id)
2573 0x10, 0x32, 0x54, 0x76,
2574 0x98, 0xBA, 0xDC, 0xFE,
2575 // packet sequence number
2576 0xBC, 0x9A, 0x78, 0x56,
2581 // frame type (blocked frame)
2584 0x04, 0x03, 0x02, 0x01,
2587 QuicEncryptedPacket
encrypted(AsChars(packet
), arraysize(packet
), false);
2589 EXPECT_TRUE(framer_
.ProcessPacket(encrypted
));
2591 EXPECT_EQ(QUIC_NO_ERROR
, framer_
.error());
2592 ASSERT_TRUE(visitor_
.header_
.get());
2593 EXPECT_TRUE(CheckDecryption(encrypted
, !kIncludeVersion
));
2595 EXPECT_EQ(UINT64_C(0x01020304), visitor_
.blocked_frame_
.stream_id
);
2597 // Now test framing boundaries.
2598 for (size_t i
= kQuicFrameTypeSize
; i
< QuicFramer::GetBlockedFrameSize();
2600 string expected_error
= "Unable to read stream_id.";
2601 CheckProcessingFails(
2603 i
+ GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID
, !kIncludeVersion
,
2604 PACKET_6BYTE_SEQUENCE_NUMBER
, NOT_IN_FEC_GROUP
),
2605 expected_error
, QUIC_INVALID_BLOCKED_DATA
);
2609 TEST_P(QuicFramerTest
, PingFrame
) {
2610 unsigned char packet
[] = {
2611 // public flags (8 byte connection_id)
2614 0x10, 0x32, 0x54, 0x76,
2615 0x98, 0xBA, 0xDC, 0xFE,
2616 // packet sequence number
2617 0xBC, 0x9A, 0x78, 0x56,
2622 // frame type (ping frame)
2626 QuicEncryptedPacket
encrypted(AsChars(packet
), arraysize(packet
), false);
2627 EXPECT_TRUE(framer_
.ProcessPacket(encrypted
));
2629 EXPECT_EQ(QUIC_NO_ERROR
, framer_
.error());
2630 ASSERT_TRUE(visitor_
.header_
.get());
2631 EXPECT_TRUE(CheckDecryption(encrypted
, !kIncludeVersion
));
2633 EXPECT_EQ(1u, visitor_
.ping_frames_
.size());
2635 // No need to check the PING frame boundaries because it has no payload.
2638 TEST_P(QuicFramerTest
, PublicResetPacket
) {
2639 unsigned char packet
[] = {
2640 // public flags (public reset, 8 byte connection_id)
2643 0x10, 0x32, 0x54, 0x76,
2644 0x98, 0xBA, 0xDC, 0xFE,
2645 // message tag (kPRST)
2647 // num_entries (2) + padding
2648 0x02, 0x00, 0x00, 0x00,
2652 0x08, 0x00, 0x00, 0x00,
2656 0x10, 0x00, 0x00, 0x00,
2658 0x89, 0x67, 0x45, 0x23,
2659 0x01, 0xEF, 0xCD, 0xAB,
2660 // rejected sequence number
2661 0xBC, 0x9A, 0x78, 0x56,
2662 0x34, 0x12, 0x00, 0x00,
2665 QuicEncryptedPacket
encrypted(AsChars(packet
), arraysize(packet
), false);
2666 EXPECT_TRUE(framer_
.ProcessPacket(encrypted
));
2667 ASSERT_EQ(QUIC_NO_ERROR
, framer_
.error());
2668 ASSERT_TRUE(visitor_
.public_reset_packet_
.get());
2669 EXPECT_EQ(UINT64_C(0xFEDCBA9876543210),
2670 visitor_
.public_reset_packet_
->public_header
.connection_id
);
2671 EXPECT_TRUE(visitor_
.public_reset_packet_
->public_header
.reset_flag
);
2672 EXPECT_FALSE(visitor_
.public_reset_packet_
->public_header
.version_flag
);
2673 EXPECT_EQ(UINT64_C(0xABCDEF0123456789),
2674 visitor_
.public_reset_packet_
->nonce_proof
);
2675 EXPECT_EQ(UINT64_C(0x123456789ABC),
2676 visitor_
.public_reset_packet_
->rejected_sequence_number
);
2678 visitor_
.public_reset_packet_
->client_address
.address().empty());
2680 // Now test framing boundaries.
2681 for (size_t i
= 0; i
< arraysize(packet
); ++i
) {
2682 string expected_error
;
2683 DVLOG(1) << "iteration: " << i
;
2684 if (i
< kConnectionIdOffset
) {
2685 expected_error
= "Unable to read public flags.";
2686 CheckProcessingFails(packet
, i
, expected_error
,
2687 QUIC_INVALID_PACKET_HEADER
);
2688 } else if (i
< kPublicResetPacketMessageTagOffset
) {
2689 expected_error
= "Unable to read ConnectionId.";
2690 CheckProcessingFails(packet
, i
, expected_error
,
2691 QUIC_INVALID_PACKET_HEADER
);
2693 expected_error
= "Unable to read reset message.";
2694 CheckProcessingFails(packet
, i
, expected_error
,
2695 QUIC_INVALID_PUBLIC_RST_PACKET
);
2700 TEST_P(QuicFramerTest
, PublicResetPacketWithTrailingJunk
) {
2701 unsigned char packet
[] = {
2702 // public flags (public reset, 8 byte connection_id)
2705 0x10, 0x32, 0x54, 0x76,
2706 0x98, 0xBA, 0xDC, 0xFE,
2707 // message tag (kPRST)
2709 // num_entries (2) + padding
2710 0x02, 0x00, 0x00, 0x00,
2714 0x08, 0x00, 0x00, 0x00,
2718 0x10, 0x00, 0x00, 0x00,
2720 0x89, 0x67, 0x45, 0x23,
2721 0x01, 0xEF, 0xCD, 0xAB,
2722 // rejected sequence number
2723 0xBC, 0x9A, 0x78, 0x56,
2724 0x34, 0x12, 0x00, 0x00,
2729 string expected_error
= "Unable to read reset message.";
2730 CheckProcessingFails(packet
, arraysize(packet
), expected_error
,
2731 QUIC_INVALID_PUBLIC_RST_PACKET
);
2734 TEST_P(QuicFramerTest
, PublicResetPacketWithClientAddress
) {
2735 unsigned char packet
[] = {
2736 // public flags (public reset, 8 byte connection_id)
2739 0x10, 0x32, 0x54, 0x76,
2740 0x98, 0xBA, 0xDC, 0xFE,
2741 // message tag (kPRST)
2743 // num_entries (3) + padding
2744 0x03, 0x00, 0x00, 0x00,
2748 0x08, 0x00, 0x00, 0x00,
2752 0x10, 0x00, 0x00, 0x00,
2756 0x18, 0x00, 0x00, 0x00,
2758 0x89, 0x67, 0x45, 0x23,
2759 0x01, 0xEF, 0xCD, 0xAB,
2760 // rejected sequence number
2761 0xBC, 0x9A, 0x78, 0x56,
2762 0x34, 0x12, 0x00, 0x00,
2763 // client address: 4.31.198.44:443
2765 0x04, 0x1F, 0xC6, 0x2C,
2769 QuicEncryptedPacket
encrypted(AsChars(packet
), arraysize(packet
), false);
2770 EXPECT_TRUE(framer_
.ProcessPacket(encrypted
));
2771 ASSERT_EQ(QUIC_NO_ERROR
, framer_
.error());
2772 ASSERT_TRUE(visitor_
.public_reset_packet_
.get());
2773 EXPECT_EQ(UINT64_C(0xFEDCBA9876543210),
2774 visitor_
.public_reset_packet_
->public_header
.connection_id
);
2775 EXPECT_TRUE(visitor_
.public_reset_packet_
->public_header
.reset_flag
);
2776 EXPECT_FALSE(visitor_
.public_reset_packet_
->public_header
.version_flag
);
2777 EXPECT_EQ(UINT64_C(0xABCDEF0123456789),
2778 visitor_
.public_reset_packet_
->nonce_proof
);
2779 EXPECT_EQ(UINT64_C(0x123456789ABC),
2780 visitor_
.public_reset_packet_
->rejected_sequence_number
);
2781 EXPECT_EQ("4.31.198.44",
2782 IPAddressToString(visitor_
.public_reset_packet_
->
2783 client_address
.address()));
2784 EXPECT_EQ(443, visitor_
.public_reset_packet_
->client_address
.port());
2786 // Now test framing boundaries.
2787 for (size_t i
= 0; i
< arraysize(packet
); ++i
) {
2788 string expected_error
;
2789 DVLOG(1) << "iteration: " << i
;
2790 if (i
< kConnectionIdOffset
) {
2791 expected_error
= "Unable to read public flags.";
2792 CheckProcessingFails(packet
, i
, expected_error
,
2793 QUIC_INVALID_PACKET_HEADER
);
2794 } else if (i
< kPublicResetPacketMessageTagOffset
) {
2795 expected_error
= "Unable to read ConnectionId.";
2796 CheckProcessingFails(packet
, i
, expected_error
,
2797 QUIC_INVALID_PACKET_HEADER
);
2799 expected_error
= "Unable to read reset message.";
2800 CheckProcessingFails(packet
, i
, expected_error
,
2801 QUIC_INVALID_PUBLIC_RST_PACKET
);
2806 TEST_P(QuicFramerTest
, VersionNegotiationPacket
) {
2807 unsigned char packet
[] = {
2808 // public flags (version, 8 byte connection_id)
2811 0x10, 0x32, 0x54, 0x76,
2812 0x98, 0xBA, 0xDC, 0xFE,
2814 'Q', '0', GetQuicVersionDigitTens(), GetQuicVersionDigitOnes(),
2818 QuicFramerPeer::SetPerspective(&framer_
, Perspective::IS_CLIENT
);
2820 QuicEncryptedPacket
encrypted(AsChars(packet
), arraysize(packet
), false);
2821 EXPECT_TRUE(framer_
.ProcessPacket(encrypted
));
2822 ASSERT_EQ(QUIC_NO_ERROR
, framer_
.error());
2823 ASSERT_TRUE(visitor_
.version_negotiation_packet_
.get());
2824 EXPECT_EQ(2u, visitor_
.version_negotiation_packet_
->versions
.size());
2825 EXPECT_EQ(GetParam(), visitor_
.version_negotiation_packet_
->versions
[0]);
2827 for (size_t i
= 0; i
<= kPublicFlagsSize
+ PACKET_8BYTE_CONNECTION_ID
; ++i
) {
2828 string expected_error
;
2829 QuicErrorCode error_code
= QUIC_INVALID_PACKET_HEADER
;
2830 if (i
< kConnectionIdOffset
) {
2831 expected_error
= "Unable to read public flags.";
2832 } else if (i
< kVersionOffset
) {
2833 expected_error
= "Unable to read ConnectionId.";
2835 expected_error
= "Unable to read supported version in negotiation.";
2836 error_code
= QUIC_INVALID_VERSION_NEGOTIATION_PACKET
;
2838 CheckProcessingFails(packet
, i
, expected_error
, error_code
);
2842 TEST_P(QuicFramerTest
, FecPacket
) {
2843 unsigned char packet
[] = {
2844 // public flags (8 byte connection_id)
2847 0x10, 0x32, 0x54, 0x76,
2848 0x98, 0xBA, 0xDC, 0xFE,
2849 // packet sequence number
2850 0xBC, 0x9A, 0x78, 0x56,
2852 // private flags (fec group & FEC)
2854 // first fec protected packet offset
2864 QuicEncryptedPacket
encrypted(AsChars(packet
), arraysize(packet
), false);
2865 EXPECT_TRUE(framer_
.ProcessPacket(encrypted
));
2867 EXPECT_EQ(QUIC_NO_ERROR
, framer_
.error());
2868 ASSERT_TRUE(visitor_
.header_
.get());
2869 EXPECT_TRUE(CheckDecryption(encrypted
, !kIncludeVersion
));
2871 EXPECT_EQ(0u, visitor_
.stream_frames_
.size());
2872 EXPECT_EQ(0u, visitor_
.ack_frames_
.size());
2873 ASSERT_EQ(1, visitor_
.fec_count_
);
2874 const QuicFecData
& fec_data
= *visitor_
.fec_data_
[0];
2875 EXPECT_EQ(UINT64_C(0x0123456789ABB), fec_data
.fec_group
);
2876 EXPECT_EQ("abcdefghijklmnop", fec_data
.redundancy
);
2879 TEST_P(QuicFramerTest
, BuildPaddingFramePacket
) {
2880 QuicPacketHeader header
;
2881 header
.public_header
.connection_id
= UINT64_C(0xFEDCBA9876543210);
2882 header
.public_header
.reset_flag
= false;
2883 header
.public_header
.version_flag
= false;
2884 header
.fec_flag
= false;
2885 header
.entropy_flag
= false;
2886 header
.packet_sequence_number
= UINT64_C(0x123456789ABC);
2887 header
.fec_group
= 0;
2889 QuicPaddingFrame padding_frame
;
2892 frames
.push_back(QuicFrame(&padding_frame
));
2894 unsigned char packet
[kMaxPacketSize
] = {
2895 // public flags (8 byte connection_id)
2898 0x10, 0x32, 0x54, 0x76,
2899 0x98, 0xBA, 0xDC, 0xFE,
2900 // packet sequence number
2901 0xBC, 0x9A, 0x78, 0x56,
2906 // frame type (padding frame)
2908 0x00, 0x00, 0x00, 0x00
2911 uint64 header_size
=
2912 GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID
, !kIncludeVersion
,
2913 PACKET_6BYTE_SEQUENCE_NUMBER
, NOT_IN_FEC_GROUP
);
2914 memset(packet
+ header_size
+ 1, 0x00, kMaxPacketSize
- header_size
- 1);
2916 scoped_ptr
<QuicPacket
> data(BuildDataPacket(header
, frames
));
2917 ASSERT_TRUE(data
!= nullptr);
2919 test::CompareCharArraysWithHexError("constructed packet",
2920 data
->data(), data
->length(),
2925 TEST_P(QuicFramerTest
, Build4ByteSequenceNumberPaddingFramePacket
) {
2926 QuicPacketHeader header
;
2927 header
.public_header
.connection_id
= UINT64_C(0xFEDCBA9876543210);
2928 header
.public_header
.reset_flag
= false;
2929 header
.public_header
.version_flag
= false;
2930 header
.fec_flag
= false;
2931 header
.entropy_flag
= false;
2932 header
.public_header
.sequence_number_length
= PACKET_4BYTE_SEQUENCE_NUMBER
;
2933 header
.packet_sequence_number
= UINT64_C(0x123456789ABC);
2934 header
.fec_group
= 0;
2936 QuicPaddingFrame padding_frame
;
2939 frames
.push_back(QuicFrame(&padding_frame
));
2941 unsigned char packet
[kMaxPacketSize
] = {
2942 // public flags (8 byte connection_id and 4 byte sequence number)
2945 0x10, 0x32, 0x54, 0x76,
2946 0x98, 0xBA, 0xDC, 0xFE,
2947 // packet sequence number
2948 0xBC, 0x9A, 0x78, 0x56,
2952 // frame type (padding frame)
2954 0x00, 0x00, 0x00, 0x00
2957 uint64 header_size
=
2958 GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID
, !kIncludeVersion
,
2959 PACKET_4BYTE_SEQUENCE_NUMBER
, NOT_IN_FEC_GROUP
);
2960 memset(packet
+ header_size
+ 1, 0x00, kMaxPacketSize
- header_size
- 1);
2962 scoped_ptr
<QuicPacket
> data(BuildDataPacket(header
, frames
));
2963 ASSERT_TRUE(data
!= nullptr);
2965 test::CompareCharArraysWithHexError("constructed packet",
2966 data
->data(), data
->length(),
2971 TEST_P(QuicFramerTest
, Build2ByteSequenceNumberPaddingFramePacket
) {
2972 QuicPacketHeader header
;
2973 header
.public_header
.connection_id
= UINT64_C(0xFEDCBA9876543210);
2974 header
.public_header
.reset_flag
= false;
2975 header
.public_header
.version_flag
= false;
2976 header
.fec_flag
= false;
2977 header
.entropy_flag
= false;
2978 header
.public_header
.sequence_number_length
= PACKET_2BYTE_SEQUENCE_NUMBER
;
2979 header
.packet_sequence_number
= UINT64_C(0x123456789ABC);
2980 header
.fec_group
= 0;
2982 QuicPaddingFrame padding_frame
;
2985 frames
.push_back(QuicFrame(&padding_frame
));
2987 unsigned char packet
[kMaxPacketSize
] = {
2988 // public flags (8 byte connection_id and 2 byte sequence number)
2991 0x10, 0x32, 0x54, 0x76,
2992 0x98, 0xBA, 0xDC, 0xFE,
2993 // packet sequence number
2998 // frame type (padding frame)
3000 0x00, 0x00, 0x00, 0x00
3003 uint64 header_size
=
3004 GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID
, !kIncludeVersion
,
3005 PACKET_2BYTE_SEQUENCE_NUMBER
, NOT_IN_FEC_GROUP
);
3006 memset(packet
+ header_size
+ 1, 0x00, kMaxPacketSize
- header_size
- 1);
3008 scoped_ptr
<QuicPacket
> data(BuildDataPacket(header
, frames
));
3009 ASSERT_TRUE(data
!= nullptr);
3011 test::CompareCharArraysWithHexError("constructed packet",
3012 data
->data(), data
->length(),
3017 TEST_P(QuicFramerTest
, Build1ByteSequenceNumberPaddingFramePacket
) {
3018 QuicPacketHeader header
;
3019 header
.public_header
.connection_id
= UINT64_C(0xFEDCBA9876543210);
3020 header
.public_header
.reset_flag
= false;
3021 header
.public_header
.version_flag
= false;
3022 header
.fec_flag
= false;
3023 header
.entropy_flag
= false;
3024 header
.public_header
.sequence_number_length
= PACKET_1BYTE_SEQUENCE_NUMBER
;
3025 header
.packet_sequence_number
= UINT64_C(0x123456789ABC);
3026 header
.fec_group
= 0;
3028 QuicPaddingFrame padding_frame
;
3031 frames
.push_back(QuicFrame(&padding_frame
));
3033 unsigned char packet
[kMaxPacketSize
] = {
3034 // public flags (8 byte connection_id and 1 byte sequence number)
3037 0x10, 0x32, 0x54, 0x76,
3038 0x98, 0xBA, 0xDC, 0xFE,
3039 // packet sequence number
3044 // frame type (padding frame)
3046 0x00, 0x00, 0x00, 0x00
3049 uint64 header_size
=
3050 GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID
, !kIncludeVersion
,
3051 PACKET_1BYTE_SEQUENCE_NUMBER
, NOT_IN_FEC_GROUP
);
3052 memset(packet
+ header_size
+ 1, 0x00, kMaxPacketSize
- header_size
- 1);
3054 scoped_ptr
<QuicPacket
> data(BuildDataPacket(header
, frames
));
3055 ASSERT_TRUE(data
!= nullptr);
3057 test::CompareCharArraysWithHexError("constructed packet",
3058 data
->data(), data
->length(),
3063 TEST_P(QuicFramerTest
, BuildStreamFramePacket
) {
3064 QuicPacketHeader header
;
3065 header
.public_header
.connection_id
= UINT64_C(0xFEDCBA9876543210);
3066 header
.public_header
.reset_flag
= false;
3067 header
.public_header
.version_flag
= false;
3068 header
.fec_flag
= false;
3069 header
.entropy_flag
= true;
3070 header
.packet_sequence_number
= UINT64_C(0x77123456789ABC);
3071 header
.fec_group
= 0;
3073 QuicStreamFrame
stream_frame(0x01020304, true, UINT64_C(0xBA98FEDC32107654),
3074 StringPiece("hello world!"));
3077 frames
.push_back(QuicFrame(&stream_frame
));
3079 unsigned char packet
[] = {
3080 // public flags (8 byte connection_id)
3083 0x10, 0x32, 0x54, 0x76,
3084 0x98, 0xBA, 0xDC, 0xFE,
3085 // packet sequence number
3086 0xBC, 0x9A, 0x78, 0x56,
3088 // private flags (entropy)
3091 // frame type (stream frame with fin and no length)
3094 0x04, 0x03, 0x02, 0x01,
3096 0x54, 0x76, 0x10, 0x32,
3097 0xDC, 0xFE, 0x98, 0xBA,
3104 scoped_ptr
<QuicPacket
> data(BuildDataPacket(header
, frames
));
3105 ASSERT_TRUE(data
!= nullptr);
3107 test::CompareCharArraysWithHexError("constructed packet",
3108 data
->data(), data
->length(),
3109 AsChars(packet
), arraysize(packet
));
3112 TEST_P(QuicFramerTest
, BuildStreamFramePacketInFecGroup
) {
3113 QuicPacketHeader header
;
3114 header
.public_header
.connection_id
= UINT64_C(0xFEDCBA9876543210);
3115 header
.public_header
.reset_flag
= false;
3116 header
.public_header
.version_flag
= false;
3117 header
.fec_flag
= false;
3118 header
.entropy_flag
= true;
3119 header
.packet_sequence_number
= UINT64_C(0x77123456789ABC);
3120 header
.is_in_fec_group
= IN_FEC_GROUP
;
3121 header
.fec_group
= UINT64_C(0x77123456789ABC);
3123 QuicStreamFrame
stream_frame(0x01020304, true, UINT64_C(0xBA98FEDC32107654),
3124 StringPiece("hello world!"));
3127 frames
.push_back(QuicFrame(&stream_frame
));
3128 unsigned char packet
[] = {
3129 // public flags (8 byte connection_id)
3132 0x10, 0x32, 0x54, 0x76, 0x98, 0xBA, 0xDC, 0xFE,
3133 // packet sequence number
3134 0xBC, 0x9A, 0x78, 0x56, 0x34, 0x12,
3135 // private flags (entropy, is_in_fec_group)
3139 // frame type (stream frame with fin and data length field)
3142 0x04, 0x03, 0x02, 0x01,
3144 0x54, 0x76, 0x10, 0x32, 0xDC, 0xFE, 0x98, 0xBA,
3145 // data length (since packet is in an FEC group)
3148 'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', '!',
3151 scoped_ptr
<QuicPacket
> data(BuildDataPacket(header
, frames
));
3152 ASSERT_TRUE(data
!= nullptr);
3154 test::CompareCharArraysWithHexError("constructed packet",
3155 data
->data(), data
->length(),
3156 AsChars(packet
), arraysize(packet
));
3159 TEST_P(QuicFramerTest
, BuildStreamFramePacketWithVersionFlag
) {
3160 QuicPacketHeader header
;
3161 header
.public_header
.connection_id
= UINT64_C(0xFEDCBA9876543210);
3162 header
.public_header
.reset_flag
= false;
3163 header
.public_header
.version_flag
= true;
3164 header
.fec_flag
= false;
3165 header
.entropy_flag
= true;
3166 header
.packet_sequence_number
= UINT64_C(0x77123456789ABC);
3167 header
.fec_group
= 0;
3169 QuicStreamFrame
stream_frame(0x01020304, true, UINT64_C(0xBA98FEDC32107654),
3170 StringPiece("hello world!"));
3173 frames
.push_back(QuicFrame(&stream_frame
));
3175 unsigned char packet
[] = {
3176 // public flags (version, 8 byte connection_id)
3190 GetQuicVersionDigitTens(),
3191 GetQuicVersionDigitOnes(),
3192 // packet sequence number
3199 // private flags (entropy)
3202 // frame type (stream frame with fin and no length)
3233 QuicFramerPeer::SetPerspective(&framer_
, Perspective::IS_CLIENT
);
3234 scoped_ptr
<QuicPacket
> data(BuildDataPacket(header
, frames
));
3235 ASSERT_TRUE(data
!= nullptr);
3237 test::CompareCharArraysWithHexError("constructed packet",
3238 data
->data(), data
->length(),
3239 AsChars(packet
), arraysize(packet
));
3242 TEST_P(QuicFramerTest
, BuildVersionNegotiationPacket
) {
3243 QuicPacketPublicHeader header
;
3244 header
.connection_id
= UINT64_C(0xFEDCBA9876543210);
3245 header
.reset_flag
= false;
3246 header
.version_flag
= true;
3248 unsigned char packet
[] = {
3249 // public flags (version, 8 byte connection_id)
3263 GetQuicVersionDigitTens(),
3264 GetQuicVersionDigitOnes(),
3267 QuicVersionVector versions
;
3268 versions
.push_back(GetParam());
3269 scoped_ptr
<QuicEncryptedPacket
> data(
3270 framer_
.BuildVersionNegotiationPacket(header
, versions
));
3272 test::CompareCharArraysWithHexError("constructed packet", data
->data(),
3273 data
->length(), AsChars(packet
),
3277 TEST_P(QuicFramerTest
, BuildAckFramePacket
) {
3278 QuicPacketHeader header
;
3279 header
.public_header
.connection_id
= UINT64_C(0xFEDCBA9876543210);
3280 header
.public_header
.reset_flag
= false;
3281 header
.public_header
.version_flag
= false;
3282 header
.fec_flag
= false;
3283 header
.entropy_flag
= true;
3284 header
.packet_sequence_number
= UINT64_C(0x770123456789AA8);
3285 header
.fec_group
= 0;
3287 QuicAckFrame ack_frame
;
3288 ack_frame
.entropy_hash
= 0x43;
3289 ack_frame
.largest_observed
= UINT64_C(0x770123456789ABF);
3290 ack_frame
.delta_time_largest_observed
= QuicTime::Delta::Zero();
3291 ack_frame
.missing_packets
.insert(UINT64_C(0x770123456789ABE));
3294 frames
.push_back(QuicFrame(&ack_frame
));
3296 unsigned char packet
[] = {
3297 // public flags (8 byte connection_id)
3300 0x10, 0x32, 0x54, 0x76, 0x98, 0xBA, 0xDC, 0xFE,
3301 // packet sequence number
3302 0xA8, 0x9A, 0x78, 0x56, 0x34, 0x12,
3303 // private flags (entropy)
3306 // frame type (ack frame)
3307 // (has nacks, not truncated, 6 byte largest observed, 1 byte delta)
3309 // entropy hash of all received packets.
3311 // largest observed packet sequence number
3312 0xBF, 0x9A, 0x78, 0x56, 0x34, 0x12,
3315 // num received packets.
3317 // num missing packet ranges
3319 // missing packet delta
3321 // 0 more missing packets in range.
3323 // 0 revived packets.
3327 scoped_ptr
<QuicPacket
> data(BuildDataPacket(header
, frames
));
3328 ASSERT_TRUE(data
!= nullptr);
3330 test::CompareCharArraysWithHexError("constructed packet",
3331 data
->data(), data
->length(),
3332 AsChars(packet
), arraysize(packet
));
3335 // TODO(jri): Add test for tuncated packets in which the original ack frame had
3336 // revived packets. (In both the large and small packet cases below).
3338 TEST_P(QuicFramerTest
, BuildTruncatedAckFrameLargePacket
) {
3339 QuicPacketHeader header
;
3340 header
.public_header
.connection_id
= UINT64_C(0xFEDCBA9876543210);
3341 header
.public_header
.reset_flag
= false;
3342 header
.public_header
.version_flag
= false;
3343 header
.fec_flag
= false;
3344 header
.entropy_flag
= true;
3345 header
.packet_sequence_number
= UINT64_C(0x770123456789AA8);
3346 header
.fec_group
= 0;
3348 QuicAckFrame ack_frame
;
3349 // This entropy hash is different from what shows up in the packet below,
3350 // since entropy is recomputed by the framer on ack truncation (by
3351 // TestEntropyCalculator for this test.)
3352 ack_frame
.entropy_hash
= 0x43;
3353 ack_frame
.largest_observed
= 2 * 300;
3354 ack_frame
.delta_time_largest_observed
= QuicTime::Delta::Zero();
3355 for (size_t i
= 1; i
< 2 * 300; i
+= 2) {
3356 ack_frame
.missing_packets
.insert(i
);
3360 frames
.push_back(QuicFrame(&ack_frame
));
3362 unsigned char packet
[] = {
3363 // public flags (8 byte connection_id)
3366 0x10, 0x32, 0x54, 0x76, 0x98, 0xBA, 0xDC, 0xFE,
3367 // packet sequence number
3368 0xA8, 0x9A, 0x78, 0x56, 0x34, 0x12,
3369 // private flags (entropy)
3372 // frame type (ack frame)
3373 // (has nacks, is truncated, 2 byte largest observed, 1 byte delta)
3375 // entropy hash of all received packets, set to 1 by TestEntropyCalculator
3376 // since ack is truncated.
3378 // 2-byte largest observed packet sequence number.
3379 // Expected to be 510 (0x1FE), since only 255 nack ranges can fit.
3383 // num missing packet ranges (limited to 255 by size of this field).
3385 // {missing packet delta, further missing packets in range}
3386 // 6 nack ranges x 42 + 3 nack ranges
3387 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3388 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3389 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3390 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3391 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3392 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3393 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3394 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3395 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3396 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3398 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3399 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3400 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3401 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3402 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3403 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3404 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3405 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3406 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3407 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3409 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3410 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3411 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3412 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3413 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3414 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3415 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3416 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3417 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3418 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3420 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3421 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3422 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3423 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3424 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3425 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3426 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3427 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3428 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3429 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3431 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3432 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3433 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3435 // 0 revived packets.
3439 scoped_ptr
<QuicPacket
> data(BuildDataPacket(header
, frames
));
3440 ASSERT_TRUE(data
!= nullptr);
3442 test::CompareCharArraysWithHexError("constructed packet",
3443 data
->data(), data
->length(),
3444 AsChars(packet
), arraysize(packet
));
3447 TEST_P(QuicFramerTest
, BuildTruncatedAckFrameSmallPacket
) {
3448 QuicPacketHeader header
;
3449 header
.public_header
.connection_id
= UINT64_C(0xFEDCBA9876543210);
3450 header
.public_header
.reset_flag
= false;
3451 header
.public_header
.version_flag
= false;
3452 header
.fec_flag
= false;
3453 header
.entropy_flag
= true;
3454 header
.packet_sequence_number
= UINT64_C(0x770123456789AA8);
3455 header
.fec_group
= 0;
3457 QuicAckFrame ack_frame
;
3458 // This entropy hash is different from what shows up in the packet below,
3459 // since entropy is recomputed by the framer on ack truncation (by
3460 // TestEntropyCalculator for this test.)
3461 ack_frame
.entropy_hash
= 0x43;
3462 ack_frame
.largest_observed
= 2 * 300;
3463 ack_frame
.delta_time_largest_observed
= QuicTime::Delta::Zero();
3464 for (size_t i
= 1; i
< 2 * 300; i
+= 2) {
3465 ack_frame
.missing_packets
.insert(i
);
3469 frames
.push_back(QuicFrame(&ack_frame
));
3471 unsigned char packet
[] = {
3472 // public flags (8 byte connection_id)
3475 0x10, 0x32, 0x54, 0x76, 0x98, 0xBA, 0xDC, 0xFE,
3476 // packet sequence number
3477 0xA8, 0x9A, 0x78, 0x56, 0x34, 0x12,
3478 // private flags (entropy)
3481 // frame type (ack frame)
3482 // (has nacks, is truncated, 2 byte largest observed, 1 byte delta)
3484 // entropy hash of all received packets, set to 1 by TestEntropyCalculator
3485 // since ack is truncated.
3487 // 2-byte largest observed packet sequence number.
3488 // Expected to be 12 (0x0C), since only 6 nack ranges can fit.
3492 // num missing packet ranges (limited to 6 by packet size of 37).
3494 // {missing packet delta, further missing packets in range}
3496 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3497 // 0 revived packets.
3501 scoped_ptr
<QuicPacket
> data(BuildDataPacket(header
, frames
, 37u));
3502 ASSERT_TRUE(data
!= nullptr);
3503 // Expect 1 byte unused since at least 2 bytes are needed to fit more nacks.
3504 EXPECT_EQ(36u, data
->length());
3505 test::CompareCharArraysWithHexError("constructed packet",
3506 data
->data(), data
->length(),
3507 AsChars(packet
), arraysize(packet
));
3510 TEST_P(QuicFramerTest
, BuildStopWaitingPacket
) {
3511 QuicPacketHeader header
;
3512 header
.public_header
.connection_id
= UINT64_C(0xFEDCBA9876543210);
3513 header
.public_header
.reset_flag
= false;
3514 header
.public_header
.version_flag
= false;
3515 header
.fec_flag
= false;
3516 header
.entropy_flag
= true;
3517 header
.packet_sequence_number
= UINT64_C(0x770123456789AA8);
3518 header
.fec_group
= 0;
3520 QuicStopWaitingFrame stop_waiting_frame
;
3521 stop_waiting_frame
.entropy_hash
= 0x14;
3522 stop_waiting_frame
.least_unacked
= UINT64_C(0x770123456789AA0);
3525 frames
.push_back(QuicFrame(&stop_waiting_frame
));
3527 unsigned char packet
[] = {
3528 // public flags (8 byte connection_id)
3531 0x10, 0x32, 0x54, 0x76,
3532 0x98, 0xBA, 0xDC, 0xFE,
3533 // packet sequence number
3534 0xA8, 0x9A, 0x78, 0x56,
3536 // private flags (entropy)
3539 // frame type (stop waiting frame)
3541 // entropy hash of sent packets till least awaiting - 1.
3543 // least packet sequence number awaiting an ack, delta from sequence number.
3544 0x08, 0x00, 0x00, 0x00,
3548 scoped_ptr
<QuicPacket
> data(BuildDataPacket(header
, frames
));
3549 ASSERT_TRUE(data
!= nullptr);
3551 test::CompareCharArraysWithHexError("constructed packet",
3552 data
->data(), data
->length(),
3553 AsChars(packet
), arraysize(packet
));
3556 TEST_P(QuicFramerTest
, BuildRstFramePacketQuicVersion24
) {
3557 if (version_
> QUIC_VERSION_24
) {
3558 // QUIC_VERSION_25 removes the error_details field from QuicRstStreamFrame.
3562 QuicPacketHeader header
;
3563 header
.public_header
.connection_id
= UINT64_C(0xFEDCBA9876543210);
3564 header
.public_header
.reset_flag
= false;
3565 header
.public_header
.version_flag
= false;
3566 header
.fec_flag
= false;
3567 header
.entropy_flag
= false;
3568 header
.packet_sequence_number
= UINT64_C(0x123456789ABC);
3569 header
.fec_group
= 0;
3571 QuicRstStreamFrame rst_frame
;
3572 rst_frame
.stream_id
= 0x01020304;
3573 rst_frame
.error_code
= static_cast<QuicRstStreamErrorCode
>(0x05060708);
3574 rst_frame
.error_details
= "because I can";
3575 rst_frame
.byte_offset
= 0x0807060504030201;
3577 unsigned char packet
[] = {
3578 // public flags (8 byte connection_id)
3581 0x10, 0x32, 0x54, 0x76,
3582 0x98, 0xBA, 0xDC, 0xFE,
3583 // packet sequence number
3584 0xBC, 0x9A, 0x78, 0x56,
3589 // frame type (rst stream frame)
3592 0x04, 0x03, 0x02, 0x01,
3594 0x01, 0x02, 0x03, 0x04,
3595 0x05, 0x06, 0x07, 0x08,
3597 0x08, 0x07, 0x06, 0x05,
3598 // error details length
3608 frames
.push_back(QuicFrame(&rst_frame
));
3610 scoped_ptr
<QuicPacket
> data(BuildDataPacket(header
, frames
));
3611 ASSERT_TRUE(data
!= nullptr);
3613 test::CompareCharArraysWithHexError("constructed packet", data
->data(),
3614 data
->length(), AsChars(packet
),
3618 TEST_P(QuicFramerTest
, BuildRstFramePacketQuic
) {
3619 if (version_
<= QUIC_VERSION_24
) {
3620 // QUIC_VERSION_25 removes the error_details field from QuicRstStreamFrame.
3624 QuicPacketHeader header
;
3625 header
.public_header
.connection_id
= UINT64_C(0xFEDCBA9876543210);
3626 header
.public_header
.reset_flag
= false;
3627 header
.public_header
.version_flag
= false;
3628 header
.fec_flag
= false;
3629 header
.entropy_flag
= false;
3630 header
.packet_sequence_number
= UINT64_C(0x123456789ABC);
3631 header
.fec_group
= 0;
3633 QuicRstStreamFrame rst_frame
;
3634 rst_frame
.stream_id
= 0x01020304;
3635 rst_frame
.error_code
= static_cast<QuicRstStreamErrorCode
>(0x05060708);
3636 rst_frame
.byte_offset
= 0x0807060504030201;
3639 unsigned char packet
[] = {
3640 // public flags (8 byte connection_id)
3643 0x10, 0x32, 0x54, 0x76,
3644 0x98, 0xBA, 0xDC, 0xFE,
3645 // packet sequence number
3646 0xBC, 0x9A, 0x78, 0x56,
3651 // frame type (rst stream frame)
3654 0x04, 0x03, 0x02, 0x01,
3656 0x01, 0x02, 0x03, 0x04,
3657 0x05, 0x06, 0x07, 0x08,
3659 0x08, 0x07, 0x06, 0x05,
3664 frames
.push_back(QuicFrame(&rst_frame
));
3666 scoped_ptr
<QuicPacket
> data(BuildDataPacket(header
, frames
));
3667 ASSERT_TRUE(data
!= nullptr);
3669 test::CompareCharArraysWithHexError("constructed packet", data
->data(),
3670 data
->length(), AsChars(packet
),
3674 TEST_P(QuicFramerTest
, BuildCloseFramePacket
) {
3675 QuicPacketHeader header
;
3676 header
.public_header
.connection_id
= UINT64_C(0xFEDCBA9876543210);
3677 header
.public_header
.reset_flag
= false;
3678 header
.public_header
.version_flag
= false;
3679 header
.fec_flag
= false;
3680 header
.entropy_flag
= true;
3681 header
.packet_sequence_number
= UINT64_C(0x123456789ABC);
3682 header
.fec_group
= 0;
3684 QuicConnectionCloseFrame close_frame
;
3685 close_frame
.error_code
= static_cast<QuicErrorCode
>(0x05060708);
3686 close_frame
.error_details
= "because I can";
3689 frames
.push_back(QuicFrame(&close_frame
));
3691 unsigned char packet
[] = {
3692 // public flags (8 byte connection_id)
3695 0x10, 0x32, 0x54, 0x76,
3696 0x98, 0xBA, 0xDC, 0xFE,
3697 // packet sequence number
3698 0xBC, 0x9A, 0x78, 0x56,
3700 // private flags (entropy)
3703 // frame type (connection close frame)
3706 0x08, 0x07, 0x06, 0x05,
3707 // error details length
3716 scoped_ptr
<QuicPacket
> data(BuildDataPacket(header
, frames
));
3717 ASSERT_TRUE(data
!= nullptr);
3719 test::CompareCharArraysWithHexError("constructed packet",
3720 data
->data(), data
->length(),
3721 AsChars(packet
), arraysize(packet
));
3724 TEST_P(QuicFramerTest
, BuildGoAwayPacket
) {
3725 QuicPacketHeader header
;
3726 header
.public_header
.connection_id
= UINT64_C(0xFEDCBA9876543210);
3727 header
.public_header
.reset_flag
= false;
3728 header
.public_header
.version_flag
= false;
3729 header
.fec_flag
= false;
3730 header
.entropy_flag
= true;
3731 header
.packet_sequence_number
= UINT64_C(0x123456789ABC);
3732 header
.fec_group
= 0;
3734 QuicGoAwayFrame goaway_frame
;
3735 goaway_frame
.error_code
= static_cast<QuicErrorCode
>(0x05060708);
3736 goaway_frame
.last_good_stream_id
= 0x01020304;
3737 goaway_frame
.reason_phrase
= "because I can";
3740 frames
.push_back(QuicFrame(&goaway_frame
));
3742 unsigned char packet
[] = {
3743 // public flags (8 byte connection_id)
3746 0x10, 0x32, 0x54, 0x76,
3747 0x98, 0xBA, 0xDC, 0xFE,
3748 // packet sequence number
3749 0xBC, 0x9A, 0x78, 0x56,
3751 // private flags(entropy)
3754 // frame type (go away frame)
3757 0x08, 0x07, 0x06, 0x05,
3759 0x04, 0x03, 0x02, 0x01,
3760 // error details length
3769 scoped_ptr
<QuicPacket
> data(BuildDataPacket(header
, frames
));
3770 ASSERT_TRUE(data
!= nullptr);
3772 test::CompareCharArraysWithHexError("constructed packet",
3773 data
->data(), data
->length(),
3774 AsChars(packet
), arraysize(packet
));
3777 TEST_P(QuicFramerTest
, BuildWindowUpdatePacket
) {
3778 QuicPacketHeader header
;
3779 header
.public_header
.connection_id
= UINT64_C(0xFEDCBA9876543210);
3780 header
.public_header
.reset_flag
= false;
3781 header
.public_header
.version_flag
= false;
3782 header
.fec_flag
= false;
3783 header
.entropy_flag
= true;
3784 header
.packet_sequence_number
= UINT64_C(0x123456789ABC);
3785 header
.fec_group
= 0;
3787 QuicWindowUpdateFrame window_update_frame
;
3788 window_update_frame
.stream_id
= 0x01020304;
3789 window_update_frame
.byte_offset
= 0x1122334455667788;
3792 frames
.push_back(QuicFrame(&window_update_frame
));
3794 unsigned char packet
[] = {
3795 // public flags (8 byte connection_id)
3798 0x10, 0x32, 0x54, 0x76,
3799 0x98, 0xBA, 0xDC, 0xFE,
3800 // packet sequence number
3801 0xBC, 0x9A, 0x78, 0x56,
3803 // private flags(entropy)
3806 // frame type (window update frame)
3809 0x04, 0x03, 0x02, 0x01,
3811 0x88, 0x77, 0x66, 0x55,
3812 0x44, 0x33, 0x22, 0x11,
3815 scoped_ptr
<QuicPacket
> data(BuildDataPacket(header
, frames
));
3816 ASSERT_TRUE(data
!= nullptr);
3818 test::CompareCharArraysWithHexError("constructed packet", data
->data(),
3819 data
->length(), AsChars(packet
),
3823 TEST_P(QuicFramerTest
, BuildBlockedPacket
) {
3824 QuicPacketHeader header
;
3825 header
.public_header
.connection_id
= UINT64_C(0xFEDCBA9876543210);
3826 header
.public_header
.reset_flag
= false;
3827 header
.public_header
.version_flag
= false;
3828 header
.fec_flag
= false;
3829 header
.entropy_flag
= true;
3830 header
.packet_sequence_number
= UINT64_C(0x123456789ABC);
3831 header
.fec_group
= 0;
3833 QuicBlockedFrame blocked_frame
;
3834 blocked_frame
.stream_id
= 0x01020304;
3837 frames
.push_back(QuicFrame(&blocked_frame
));
3839 unsigned char packet
[] = {
3840 // public flags (8 byte connection_id)
3843 0x10, 0x32, 0x54, 0x76,
3844 0x98, 0xBA, 0xDC, 0xFE,
3845 // packet sequence number
3846 0xBC, 0x9A, 0x78, 0x56,
3848 // private flags(entropy)
3851 // frame type (blocked frame)
3854 0x04, 0x03, 0x02, 0x01,
3857 scoped_ptr
<QuicPacket
> data(BuildDataPacket(header
, frames
));
3858 ASSERT_TRUE(data
!= nullptr);
3860 test::CompareCharArraysWithHexError("constructed packet", data
->data(),
3861 data
->length(), AsChars(packet
),
3865 TEST_P(QuicFramerTest
, BuildPingPacket
) {
3866 QuicPacketHeader header
;
3867 header
.public_header
.connection_id
= UINT64_C(0xFEDCBA9876543210);
3868 header
.public_header
.reset_flag
= false;
3869 header
.public_header
.version_flag
= false;
3870 header
.fec_flag
= false;
3871 header
.entropy_flag
= true;
3872 header
.packet_sequence_number
= UINT64_C(0x123456789ABC);
3873 header
.fec_group
= 0;
3875 QuicPingFrame ping_frame
;
3878 frames
.push_back(QuicFrame(&ping_frame
));
3880 unsigned char packet
[] = {
3881 // public flags (8 byte connection_id)
3884 0x10, 0x32, 0x54, 0x76,
3885 0x98, 0xBA, 0xDC, 0xFE,
3886 // packet sequence number
3887 0xBC, 0x9A, 0x78, 0x56,
3889 // private flags(entropy)
3892 // frame type (ping frame)
3896 scoped_ptr
<QuicPacket
> data(BuildDataPacket(header
, frames
));
3897 ASSERT_TRUE(data
!= nullptr);
3899 test::CompareCharArraysWithHexError("constructed packet", data
->data(),
3900 data
->length(), AsChars(packet
),
3904 TEST_P(QuicFramerTest
, BuildPublicResetPacket
) {
3905 QuicPublicResetPacket reset_packet
;
3906 reset_packet
.public_header
.connection_id
= UINT64_C(0xFEDCBA9876543210);
3907 reset_packet
.public_header
.reset_flag
= true;
3908 reset_packet
.public_header
.version_flag
= false;
3909 reset_packet
.rejected_sequence_number
= UINT64_C(0x123456789ABC);
3910 reset_packet
.nonce_proof
= UINT64_C(0xABCDEF0123456789);
3912 unsigned char packet
[] = {
3913 // public flags (public reset, 8 byte ConnectionId)
3916 0x10, 0x32, 0x54, 0x76,
3917 0x98, 0xBA, 0xDC, 0xFE,
3918 // message tag (kPRST)
3920 // num_entries (2) + padding
3921 0x02, 0x00, 0x00, 0x00,
3925 0x08, 0x00, 0x00, 0x00,
3929 0x10, 0x00, 0x00, 0x00,
3931 0x89, 0x67, 0x45, 0x23,
3932 0x01, 0xEF, 0xCD, 0xAB,
3933 // rejected sequence number
3934 0xBC, 0x9A, 0x78, 0x56,
3935 0x34, 0x12, 0x00, 0x00,
3938 scoped_ptr
<QuicEncryptedPacket
> data(
3939 framer_
.BuildPublicResetPacket(reset_packet
));
3940 ASSERT_TRUE(data
!= nullptr);
3942 test::CompareCharArraysWithHexError("constructed packet",
3943 data
->data(), data
->length(),
3944 AsChars(packet
), arraysize(packet
));
3947 TEST_P(QuicFramerTest
, BuildPublicResetPacketWithClientAddress
) {
3948 QuicPublicResetPacket reset_packet
;
3949 reset_packet
.public_header
.connection_id
= UINT64_C(0xFEDCBA9876543210);
3950 reset_packet
.public_header
.reset_flag
= true;
3951 reset_packet
.public_header
.version_flag
= false;
3952 reset_packet
.rejected_sequence_number
= UINT64_C(0x123456789ABC);
3953 reset_packet
.nonce_proof
= UINT64_C(0xABCDEF0123456789);
3954 reset_packet
.client_address
= IPEndPoint(Loopback4(), 0x1234);
3956 unsigned char packet
[] = {
3957 // public flags (public reset, 8 byte ConnectionId)
3960 0x10, 0x32, 0x54, 0x76,
3961 0x98, 0xBA, 0xDC, 0xFE,
3962 // message tag (kPRST)
3964 // num_entries (3) + padding
3965 0x03, 0x00, 0x00, 0x00,
3969 0x08, 0x00, 0x00, 0x00,
3973 0x10, 0x00, 0x00, 0x00,
3977 0x18, 0x00, 0x00, 0x00,
3979 0x89, 0x67, 0x45, 0x23,
3980 0x01, 0xEF, 0xCD, 0xAB,
3981 // rejected sequence number
3982 0xBC, 0x9A, 0x78, 0x56,
3983 0x34, 0x12, 0x00, 0x00,
3986 0x7F, 0x00, 0x00, 0x01,
3990 scoped_ptr
<QuicEncryptedPacket
> data(
3991 framer_
.BuildPublicResetPacket(reset_packet
));
3992 ASSERT_TRUE(data
!= nullptr);
3994 test::CompareCharArraysWithHexError("constructed packet",
3995 data
->data(), data
->length(),
3996 AsChars(packet
), arraysize(packet
));
3999 TEST_P(QuicFramerTest
, BuildFecPacket
) {
4000 QuicPacketHeader header
;
4001 header
.public_header
.connection_id
= UINT64_C(0xFEDCBA9876543210);
4002 header
.public_header
.reset_flag
= false;
4003 header
.public_header
.version_flag
= false;
4004 header
.fec_flag
= true;
4005 header
.entropy_flag
= true;
4006 header
.packet_sequence_number
= (UINT64_C(0x123456789ABC));
4007 header
.is_in_fec_group
= IN_FEC_GROUP
;
4008 header
.fec_group
= UINT64_C(0x123456789ABB);
4010 QuicFecData fec_data
;
4011 fec_data
.fec_group
= 1;
4012 fec_data
.redundancy
= "abcdefghijklmnop";
4014 unsigned char packet
[] = {
4015 // public flags (8 byte connection_id)
4018 0x10, 0x32, 0x54, 0x76,
4019 0x98, 0xBA, 0xDC, 0xFE,
4020 // packet sequence number
4021 0xBC, 0x9A, 0x78, 0x56,
4023 // private flags (entropy & fec group & fec packet)
4025 // first fec protected packet offset
4035 scoped_ptr
<QuicPacket
> data(framer_
.BuildFecPacket(header
, fec_data
));
4036 ASSERT_TRUE(data
!= nullptr);
4038 test::CompareCharArraysWithHexError("constructed packet",
4039 data
->data(), data
->length(),
4040 AsChars(packet
), arraysize(packet
));
4043 TEST_P(QuicFramerTest
, EncryptPacket
) {
4044 QuicPacketSequenceNumber sequence_number
= UINT64_C(0x123456789ABC);
4045 unsigned char packet
[] = {
4046 // public flags (8 byte connection_id)
4049 0x10, 0x32, 0x54, 0x76,
4050 0x98, 0xBA, 0xDC, 0xFE,
4051 // packet sequence number
4052 0xBC, 0x9A, 0x78, 0x56,
4054 // private flags (fec group & fec packet)
4056 // first fec protected packet offset
4066 scoped_ptr
<QuicPacket
> raw(new QuicPacket(
4067 AsChars(packet
), arraysize(packet
), false, PACKET_8BYTE_CONNECTION_ID
,
4068 !kIncludeVersion
, PACKET_6BYTE_SEQUENCE_NUMBER
));
4069 char buffer
[kMaxPacketSize
];
4070 scoped_ptr
<QuicEncryptedPacket
> encrypted(framer_
.EncryptPacket(
4071 ENCRYPTION_NONE
, sequence_number
, *raw
, buffer
, kMaxPacketSize
));
4073 ASSERT_TRUE(encrypted
.get() != nullptr);
4074 EXPECT_TRUE(CheckEncryption(sequence_number
, raw
.get()));
4077 TEST_P(QuicFramerTest
, EncryptPacketWithVersionFlag
) {
4078 QuicPacketSequenceNumber sequence_number
= UINT64_C(0x123456789ABC);
4079 unsigned char packet
[] = {
4080 // public flags (version, 8 byte connection_id)
4083 0x10, 0x32, 0x54, 0x76,
4084 0x98, 0xBA, 0xDC, 0xFE,
4087 // packet sequence number
4088 0xBC, 0x9A, 0x78, 0x56,
4090 // private flags (fec group & fec flags)
4092 // first fec protected packet offset
4102 scoped_ptr
<QuicPacket
> raw(new QuicPacket(
4103 AsChars(packet
), arraysize(packet
), false, PACKET_8BYTE_CONNECTION_ID
,
4104 kIncludeVersion
, PACKET_6BYTE_SEQUENCE_NUMBER
));
4105 char buffer
[kMaxPacketSize
];
4106 scoped_ptr
<QuicEncryptedPacket
> encrypted(framer_
.EncryptPacket(
4107 ENCRYPTION_NONE
, sequence_number
, *raw
, buffer
, kMaxPacketSize
));
4109 ASSERT_TRUE(encrypted
.get() != nullptr);
4110 EXPECT_TRUE(CheckEncryption(sequence_number
, raw
.get()));
4113 TEST_P(QuicFramerTest
, AckTruncationLargePacket
) {
4114 QuicPacketHeader header
;
4115 header
.public_header
.connection_id
= UINT64_C(0xFEDCBA9876543210);
4116 header
.public_header
.reset_flag
= false;
4117 header
.public_header
.version_flag
= false;
4118 header
.fec_flag
= false;
4119 header
.entropy_flag
= false;
4120 header
.packet_sequence_number
= UINT64_C(0x123456789ABC);
4121 header
.fec_group
= 0;
4123 // Create a packet with just the ack.
4124 QuicAckFrame ack_frame
= MakeAckFrameWithNackRanges(300, 0u);
4126 frame
.type
= ACK_FRAME
;
4127 frame
.ack_frame
= &ack_frame
;
4129 frames
.push_back(frame
);
4131 // Build an ack packet with truncation due to limit in number of nack ranges.
4132 scoped_ptr
<QuicPacket
> raw_ack_packet(BuildDataPacket(header
, frames
));
4133 ASSERT_TRUE(raw_ack_packet
!= nullptr);
4134 char buffer
[kMaxPacketSize
];
4135 scoped_ptr
<QuicEncryptedPacket
> ack_packet(
4136 framer_
.EncryptPacket(ENCRYPTION_NONE
, header
.packet_sequence_number
,
4137 *raw_ack_packet
, buffer
, kMaxPacketSize
));
4138 // Now make sure we can turn our ack packet back into an ack frame.
4139 ASSERT_TRUE(framer_
.ProcessPacket(*ack_packet
));
4140 ASSERT_EQ(1u, visitor_
.ack_frames_
.size());
4141 QuicAckFrame
& processed_ack_frame
= *visitor_
.ack_frames_
[0];
4142 EXPECT_TRUE(processed_ack_frame
.is_truncated
);
4143 EXPECT_EQ(510u, processed_ack_frame
.largest_observed
);
4144 ASSERT_EQ(255u, processed_ack_frame
.missing_packets
.size());
4145 SequenceNumberSet::const_iterator missing_iter
=
4146 processed_ack_frame
.missing_packets
.begin();
4147 EXPECT_EQ(1u, *missing_iter
);
4148 SequenceNumberSet::const_reverse_iterator last_missing_iter
=
4149 processed_ack_frame
.missing_packets
.rbegin();
4150 EXPECT_EQ(509u, *last_missing_iter
);
4153 TEST_P(QuicFramerTest
, AckTruncationSmallPacket
) {
4154 QuicPacketHeader header
;
4155 header
.public_header
.connection_id
= UINT64_C(0xFEDCBA9876543210);
4156 header
.public_header
.reset_flag
= false;
4157 header
.public_header
.version_flag
= false;
4158 header
.fec_flag
= false;
4159 header
.entropy_flag
= false;
4160 header
.packet_sequence_number
= UINT64_C(0x123456789ABC);
4161 header
.fec_group
= 0;
4163 // Create a packet with just the ack.
4164 QuicAckFrame ack_frame
= MakeAckFrameWithNackRanges(300, 0u);
4166 frame
.type
= ACK_FRAME
;
4167 frame
.ack_frame
= &ack_frame
;
4169 frames
.push_back(frame
);
4171 // Build an ack packet with truncation due to limit in number of nack ranges.
4172 scoped_ptr
<QuicPacket
> raw_ack_packet(BuildDataPacket(header
, frames
, 500));
4173 ASSERT_TRUE(raw_ack_packet
!= nullptr);
4174 char buffer
[kMaxPacketSize
];
4175 scoped_ptr
<QuicEncryptedPacket
> ack_packet(
4176 framer_
.EncryptPacket(ENCRYPTION_NONE
, header
.packet_sequence_number
,
4177 *raw_ack_packet
, buffer
, kMaxPacketSize
));
4178 // Now make sure we can turn our ack packet back into an ack frame.
4179 ASSERT_TRUE(framer_
.ProcessPacket(*ack_packet
));
4180 ASSERT_EQ(1u, visitor_
.ack_frames_
.size());
4181 QuicAckFrame
& processed_ack_frame
= *visitor_
.ack_frames_
[0];
4182 EXPECT_TRUE(processed_ack_frame
.is_truncated
);
4183 EXPECT_EQ(476u, processed_ack_frame
.largest_observed
);
4184 ASSERT_EQ(238u, processed_ack_frame
.missing_packets
.size());
4185 SequenceNumberSet::const_iterator missing_iter
=
4186 processed_ack_frame
.missing_packets
.begin();
4187 EXPECT_EQ(1u, *missing_iter
);
4188 SequenceNumberSet::const_reverse_iterator last_missing_iter
=
4189 processed_ack_frame
.missing_packets
.rbegin();
4190 EXPECT_EQ(475u, *last_missing_iter
);
4193 TEST_P(QuicFramerTest
, CleanTruncation
) {
4194 QuicPacketHeader header
;
4195 header
.public_header
.connection_id
= UINT64_C(0xFEDCBA9876543210);
4196 header
.public_header
.reset_flag
= false;
4197 header
.public_header
.version_flag
= false;
4198 header
.fec_flag
= false;
4199 header
.entropy_flag
= true;
4200 header
.packet_sequence_number
= UINT64_C(0x123456789ABC);
4201 header
.fec_group
= 0;
4203 QuicAckFrame ack_frame
;
4204 ack_frame
.largest_observed
= 201;
4205 for (uint64 i
= 1; i
< ack_frame
.largest_observed
; ++i
) {
4206 ack_frame
.missing_packets
.insert(i
);
4209 // Create a packet with just the ack.
4211 frame
.type
= ACK_FRAME
;
4212 frame
.ack_frame
= &ack_frame
;
4214 frames
.push_back(frame
);
4216 scoped_ptr
<QuicPacket
> raw_ack_packet(BuildDataPacket(header
, frames
));
4217 ASSERT_TRUE(raw_ack_packet
!= nullptr);
4219 char buffer
[kMaxPacketSize
];
4220 scoped_ptr
<QuicEncryptedPacket
> ack_packet(
4221 framer_
.EncryptPacket(ENCRYPTION_NONE
, header
.packet_sequence_number
,
4222 *raw_ack_packet
, buffer
, kMaxPacketSize
));
4224 // Now make sure we can turn our ack packet back into an ack frame.
4225 ASSERT_TRUE(framer_
.ProcessPacket(*ack_packet
));
4227 // Test for clean truncation of the ack by comparing the length of the
4228 // original packets to the re-serialized packets.
4230 frame
.type
= ACK_FRAME
;
4231 frame
.ack_frame
= visitor_
.ack_frames_
[0];
4232 frames
.push_back(frame
);
4234 size_t original_raw_length
= raw_ack_packet
->length();
4235 raw_ack_packet
.reset(BuildDataPacket(header
, frames
));
4236 ASSERT_TRUE(raw_ack_packet
!= nullptr);
4237 EXPECT_EQ(original_raw_length
, raw_ack_packet
->length());
4238 ASSERT_TRUE(raw_ack_packet
!= nullptr);
4241 TEST_P(QuicFramerTest
, EntropyFlagTest
) {
4242 unsigned char packet
[] = {
4243 // public flags (8 byte connection_id)
4246 0x10, 0x32, 0x54, 0x76,
4247 0x98, 0xBA, 0xDC, 0xFE,
4248 // packet sequence number
4249 0xBC, 0x9A, 0x78, 0x56,
4251 // private flags (Entropy)
4254 // frame type (stream frame with fin and no length)
4257 0x04, 0x03, 0x02, 0x01,
4259 0x54, 0x76, 0x10, 0x32,
4260 0xDC, 0xFE, 0x98, 0xBA,
4267 QuicEncryptedPacket
encrypted(AsChars(packet
), arraysize(packet
), false);
4268 EXPECT_TRUE(framer_
.ProcessPacket(encrypted
));
4269 EXPECT_EQ(QUIC_NO_ERROR
, framer_
.error());
4270 ASSERT_TRUE(visitor_
.header_
.get());
4271 EXPECT_TRUE(visitor_
.header_
->entropy_flag
);
4272 EXPECT_EQ(1 << 4, visitor_
.header_
->entropy_hash
);
4273 EXPECT_FALSE(visitor_
.header_
->fec_flag
);
4276 TEST_P(QuicFramerTest
, FecEntropyTest
) {
4277 unsigned char packet
[] = {
4278 // public flags (8 byte connection_id)
4281 0x10, 0x32, 0x54, 0x76,
4282 0x98, 0xBA, 0xDC, 0xFE,
4283 // packet sequence number
4284 0xBC, 0x9A, 0x78, 0x56,
4286 // private flags (Entropy & fec group & FEC)
4288 // first fec protected packet offset
4291 // frame type (stream frame with fin and no length)
4294 0x04, 0x03, 0x02, 0x01,
4296 0x54, 0x76, 0x10, 0x32,
4297 0xDC, 0xFE, 0x98, 0xBA,
4304 QuicEncryptedPacket
encrypted(AsChars(packet
), arraysize(packet
), false);
4305 EXPECT_TRUE(framer_
.ProcessPacket(encrypted
));
4306 EXPECT_EQ(QUIC_NO_ERROR
, framer_
.error());
4307 ASSERT_TRUE(visitor_
.header_
.get());
4308 EXPECT_TRUE(visitor_
.header_
->fec_flag
);
4309 EXPECT_TRUE(visitor_
.header_
->entropy_flag
);
4310 EXPECT_EQ(1 << 4, visitor_
.header_
->entropy_hash
);
4313 TEST_P(QuicFramerTest
, StopPacketProcessing
) {
4314 unsigned char packet
[] = {
4315 // public flags (8 byte connection_id)
4318 0x10, 0x32, 0x54, 0x76,
4319 0x98, 0xBA, 0xDC, 0xFE,
4320 // packet sequence number
4321 0xBC, 0x9A, 0x78, 0x56,
4326 // frame type (stream frame with fin)
4329 0x04, 0x03, 0x02, 0x01,
4331 0x54, 0x76, 0x10, 0x32,
4332 0xDC, 0xFE, 0x98, 0xBA,
4340 // frame type (ack frame)
4342 // entropy hash of sent packets till least awaiting - 1.
4344 // least packet sequence number awaiting an ack
4345 0xA0, 0x9A, 0x78, 0x56,
4347 // entropy hash of all received packets.
4349 // largest observed packet sequence number
4350 0xBF, 0x9A, 0x78, 0x56,
4352 // num missing packets
4355 0xBE, 0x9A, 0x78, 0x56,
4359 MockFramerVisitor visitor
;
4360 framer_
.set_visitor(&visitor
);
4361 EXPECT_CALL(visitor
, OnPacket());
4362 EXPECT_CALL(visitor
, OnPacketHeader(_
));
4363 EXPECT_CALL(visitor
, OnStreamFrame(_
)).WillOnce(Return(false));
4364 EXPECT_CALL(visitor
, OnAckFrame(_
)).Times(0);
4365 EXPECT_CALL(visitor
, OnPacketComplete());
4366 EXPECT_CALL(visitor
, OnUnauthenticatedPublicHeader(_
)).WillOnce(Return(true));
4367 EXPECT_CALL(visitor
, OnUnauthenticatedHeader(_
)).WillOnce(Return(true));
4368 EXPECT_CALL(visitor
, OnDecryptedPacket(_
));
4370 QuicEncryptedPacket
encrypted(AsChars(packet
), arraysize(packet
), false);
4371 EXPECT_TRUE(framer_
.ProcessPacket(encrypted
));
4372 EXPECT_EQ(QUIC_NO_ERROR
, framer_
.error());
4375 static char kTestString
[] = "At least 20 characters.";
4376 static QuicStreamId kTestQuicStreamId
= 1;
4377 static bool ExpectedStreamFrame(const QuicStreamFrame
& frame
) {
4378 return frame
.stream_id
== kTestQuicStreamId
&& !frame
.fin
&&
4379 frame
.offset
== 0 && frame
.data
== kTestString
;
4380 // FIN is hard-coded false in ConstructEncryptedPacket.
4381 // Offset 0 is hard-coded in ConstructEncryptedPacket.
4384 // Verify that the packet returned by ConstructEncryptedPacket() can be properly
4385 // parsed by the framer.
4386 TEST_P(QuicFramerTest
, ConstructEncryptedPacket
) {
4387 // Since we are using ConstructEncryptedPacket, we have to set the framer's
4388 // crypto to be Null.
4389 framer_
.SetDecrypter(QuicDecrypter::Create(kNULL
), ENCRYPTION_NONE
);
4390 framer_
.SetEncrypter(ENCRYPTION_NONE
, QuicEncrypter::Create(kNULL
));
4392 scoped_ptr
<QuicEncryptedPacket
> packet(ConstructEncryptedPacket(
4393 42, false, false, kTestQuicStreamId
, kTestString
,
4394 PACKET_8BYTE_CONNECTION_ID
, PACKET_6BYTE_SEQUENCE_NUMBER
));
4396 MockFramerVisitor visitor
;
4397 framer_
.set_visitor(&visitor
);
4398 EXPECT_CALL(visitor
, OnPacket()).Times(1);
4399 EXPECT_CALL(visitor
, OnUnauthenticatedPublicHeader(_
))
4401 .WillOnce(Return(true));
4402 EXPECT_CALL(visitor
, OnUnauthenticatedHeader(_
))
4404 .WillOnce(Return(true));
4405 EXPECT_CALL(visitor
, OnPacketHeader(_
)).Times(1).WillOnce(Return(true));
4406 EXPECT_CALL(visitor
, OnDecryptedPacket(_
)).Times(1);
4407 EXPECT_CALL(visitor
, OnError(_
)).Times(0);
4408 EXPECT_CALL(visitor
, OnStreamFrame(_
)).Times(0);
4409 EXPECT_CALL(visitor
, OnStreamFrame(Truly(ExpectedStreamFrame
))).Times(1);
4410 EXPECT_CALL(visitor
, OnAckFrame(_
)).Times(0);
4411 EXPECT_CALL(visitor
, OnPacketComplete()).Times(1);
4413 EXPECT_TRUE(framer_
.ProcessPacket(*packet
));
4414 EXPECT_EQ(QUIC_NO_ERROR
, framer_
.error());
4417 // Verify that the packet returned by ConstructMisFramedEncryptedPacket()
4418 // does cause the framer to return an error.
4419 TEST_P(QuicFramerTest
, ConstructMisFramedEncryptedPacket
) {
4420 // Since we are using ConstructEncryptedPacket, we have to set the framer's
4421 // crypto to be Null.
4422 framer_
.SetDecrypter(QuicDecrypter::Create(kNULL
), ENCRYPTION_NONE
);
4423 framer_
.SetEncrypter(ENCRYPTION_NONE
, QuicEncrypter::Create(kNULL
));
4425 scoped_ptr
<QuicEncryptedPacket
> packet(ConstructMisFramedEncryptedPacket(
4426 42, false, false, kTestQuicStreamId
, kTestString
,
4427 PACKET_8BYTE_CONNECTION_ID
, PACKET_6BYTE_SEQUENCE_NUMBER
, nullptr));
4429 MockFramerVisitor visitor
;
4430 framer_
.set_visitor(&visitor
);
4431 EXPECT_CALL(visitor
, OnPacket()).Times(1);
4432 EXPECT_CALL(visitor
, OnUnauthenticatedPublicHeader(_
))
4434 .WillOnce(Return(true));
4435 EXPECT_CALL(visitor
, OnUnauthenticatedHeader(_
))
4437 .WillOnce(Return(true));
4438 EXPECT_CALL(visitor
, OnPacketHeader(_
)).Times(0);
4439 EXPECT_CALL(visitor
, OnDecryptedPacket(_
)).Times(1);
4440 EXPECT_CALL(visitor
, OnError(_
)).Times(1);
4441 EXPECT_CALL(visitor
, OnStreamFrame(_
)).Times(0);
4442 EXPECT_CALL(visitor
, OnAckFrame(_
)).Times(0);
4443 EXPECT_CALL(visitor
, OnPacketComplete()).Times(0);
4445 EXPECT_FALSE(framer_
.ProcessPacket(*packet
));
4446 EXPECT_EQ(QUIC_INVALID_PACKET_HEADER
, framer_
.error());