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 const char* cipher_name() const override
{ return "Test"; }
159 // Use a distinct value starting with 0xFFFFFF, which is never used by TLS.
160 uint32
cipher_id() const override
{ return 0xFFFFFFF2; }
161 QuicPacketSequenceNumber sequence_number_
;
162 string associated_data_
;
166 class TestQuicVisitor
: public QuicFramerVisitorInterface
{
170 version_mismatch_(0),
174 complete_packets_(0),
176 accept_packet_(true),
177 accept_public_header_(true) {
180 ~TestQuicVisitor() override
{
181 STLDeleteElements(&stream_frames_
);
182 STLDeleteElements(&ack_frames_
);
183 STLDeleteElements(&stop_waiting_frames_
);
184 STLDeleteElements(&ping_frames_
);
185 STLDeleteElements(&fec_data_
);
186 STLDeleteElements(&stream_data_
);
187 STLDeleteElements(&fec_data_redundancy_
);
190 void OnError(QuicFramer
* f
) override
{
191 DVLOG(1) << "QuicFramer Error: " << QuicUtils::ErrorToString(f
->error())
192 << " (" << f
->error() << ")";
196 void OnPacket() override
{}
198 void OnPublicResetPacket(const QuicPublicResetPacket
& packet
) override
{
199 public_reset_packet_
.reset(new QuicPublicResetPacket(packet
));
202 void OnVersionNegotiationPacket(
203 const QuicVersionNegotiationPacket
& packet
) override
{
204 version_negotiation_packet_
.reset(new QuicVersionNegotiationPacket(packet
));
207 void OnRevivedPacket() override
{ ++revived_packets_
; }
209 bool OnProtocolVersionMismatch(QuicVersion version
) override
{
210 DVLOG(1) << "QuicFramer Version Mismatch, version: " << version
;
215 bool OnUnauthenticatedPublicHeader(
216 const QuicPacketPublicHeader
& header
) override
{
217 public_header_
.reset(new QuicPacketPublicHeader(header
));
218 return accept_public_header_
;
221 bool OnUnauthenticatedHeader(const QuicPacketHeader
& header
) override
{
225 void OnDecryptedPacket(EncryptionLevel level
) override
{}
227 bool OnPacketHeader(const QuicPacketHeader
& header
) override
{
229 header_
.reset(new QuicPacketHeader(header
));
230 return accept_packet_
;
233 bool OnStreamFrame(const QuicStreamFrame
& frame
) override
{
235 // Save a copy of the data so it is valid after the packet is processed.
236 string
* string_data
= new string();
237 frame
.data
.AppendToString(string_data
);
238 stream_data_
.push_back(string_data
);
239 QuicStreamFrame
* stream_frame
= new QuicStreamFrame(frame
);
240 // Make sure that the stream frame points to this data.
241 stream_frame
->data
= StringPiece(*string_data
);
242 stream_frames_
.push_back(stream_frame
);
246 void OnFecProtectedPayload(StringPiece payload
) override
{
247 fec_protected_payload_
= payload
.as_string();
250 bool OnAckFrame(const QuicAckFrame
& frame
) override
{
252 ack_frames_
.push_back(new QuicAckFrame(frame
));
256 bool OnStopWaitingFrame(const QuicStopWaitingFrame
& frame
) override
{
258 stop_waiting_frames_
.push_back(new QuicStopWaitingFrame(frame
));
262 bool OnPingFrame(const QuicPingFrame
& frame
) override
{
264 ping_frames_
.push_back(new QuicPingFrame(frame
));
268 void OnFecData(const QuicFecData
& fec
) override
{
270 QuicFecData
* fec_data
= new QuicFecData();
271 fec_data
->fec_group
= fec
.fec_group
;
272 // Save a copy of the data so it is valid after the packet is processed.
273 string
* redundancy
= new string(fec
.redundancy
.as_string());
274 fec_data_redundancy_
.push_back(redundancy
);
275 fec_data
->redundancy
= StringPiece(*redundancy
);
276 fec_data_
.push_back(fec_data
);
279 void OnPacketComplete() override
{ ++complete_packets_
; }
281 bool OnRstStreamFrame(const QuicRstStreamFrame
& frame
) override
{
282 rst_stream_frame_
= frame
;
286 bool OnConnectionCloseFrame(const QuicConnectionCloseFrame
& frame
) override
{
287 connection_close_frame_
= frame
;
291 bool OnGoAwayFrame(const QuicGoAwayFrame
& frame
) override
{
292 goaway_frame_
= frame
;
296 bool OnWindowUpdateFrame(const QuicWindowUpdateFrame
& frame
) override
{
297 window_update_frame_
= frame
;
301 bool OnBlockedFrame(const QuicBlockedFrame
& frame
) override
{
302 blocked_frame_
= frame
;
306 // Counters from the visitor_ callbacks.
308 int version_mismatch_
;
312 int complete_packets_
;
313 int revived_packets_
;
315 bool accept_public_header_
;
317 scoped_ptr
<QuicPacketHeader
> header_
;
318 scoped_ptr
<QuicPacketPublicHeader
> public_header_
;
319 scoped_ptr
<QuicPublicResetPacket
> public_reset_packet_
;
320 scoped_ptr
<QuicVersionNegotiationPacket
> version_negotiation_packet_
;
321 vector
<QuicStreamFrame
*> stream_frames_
;
322 vector
<QuicAckFrame
*> ack_frames_
;
323 vector
<QuicStopWaitingFrame
*> stop_waiting_frames_
;
324 vector
<QuicPingFrame
*> ping_frames_
;
325 vector
<QuicFecData
*> fec_data_
;
326 string fec_protected_payload_
;
327 QuicRstStreamFrame rst_stream_frame_
;
328 QuicConnectionCloseFrame connection_close_frame_
;
329 QuicGoAwayFrame goaway_frame_
;
330 QuicWindowUpdateFrame window_update_frame_
;
331 QuicBlockedFrame blocked_frame_
;
332 vector
<string
*> stream_data_
;
333 vector
<string
*> fec_data_redundancy_
;
336 class QuicFramerTest
: public ::testing::TestWithParam
<QuicVersion
> {
339 : encrypter_(new test::TestEncrypter()),
340 decrypter_(new test::TestDecrypter()),
341 start_(QuicTime::Zero().Add(QuicTime::Delta::FromMicroseconds(0x10))),
342 framer_(QuicSupportedVersions(), start_
, Perspective::IS_SERVER
) {
343 version_
= GetParam();
344 framer_
.set_version(version_
);
345 framer_
.SetDecrypter(ENCRYPTION_NONE
, decrypter_
);
346 framer_
.SetEncrypter(ENCRYPTION_NONE
, encrypter_
);
347 framer_
.set_visitor(&visitor_
);
348 framer_
.set_received_entropy_calculator(&entropy_calculator_
);
351 // Helper function to get unsigned char representation of digit in the
352 // units place of the current QUIC version number.
353 unsigned char GetQuicVersionDigitOnes() {
354 return static_cast<unsigned char> ('0' + version_
%10);
357 // Helper function to get unsigned char representation of digit in the
358 // tens place of the current QUIC version number.
359 unsigned char GetQuicVersionDigitTens() {
360 return static_cast<unsigned char> ('0' + (version_
/10)%10);
363 bool CheckEncryption(QuicPacketSequenceNumber sequence_number
,
364 QuicPacket
* packet
) {
365 if (sequence_number
!= encrypter_
->sequence_number_
) {
366 LOG(ERROR
) << "Encrypted incorrect packet sequence number. expected "
367 << sequence_number
<< " actual: "
368 << encrypter_
->sequence_number_
;
371 if (packet
->AssociatedData() != encrypter_
->associated_data_
) {
372 LOG(ERROR
) << "Encrypted incorrect associated data. expected "
373 << packet
->AssociatedData() << " actual: "
374 << encrypter_
->associated_data_
;
377 if (packet
->Plaintext() != encrypter_
->plaintext_
) {
378 LOG(ERROR
) << "Encrypted incorrect plaintext data. expected "
379 << packet
->Plaintext() << " actual: "
380 << encrypter_
->plaintext_
;
386 bool CheckDecryption(const QuicEncryptedPacket
& encrypted
,
387 bool includes_version
) {
388 if (visitor_
.header_
->packet_sequence_number
!=
389 decrypter_
->sequence_number_
) {
390 LOG(ERROR
) << "Decrypted incorrect packet sequence number. expected "
391 << visitor_
.header_
->packet_sequence_number
<< " actual: "
392 << decrypter_
->sequence_number_
;
395 if (QuicFramer::GetAssociatedDataFromEncryptedPacket(
396 encrypted
, PACKET_8BYTE_CONNECTION_ID
,
397 includes_version
, PACKET_6BYTE_SEQUENCE_NUMBER
) !=
398 decrypter_
->associated_data_
) {
399 LOG(ERROR
) << "Decrypted incorrect associated data. expected "
400 << QuicFramer::GetAssociatedDataFromEncryptedPacket(
401 encrypted
, PACKET_8BYTE_CONNECTION_ID
,
402 includes_version
, PACKET_6BYTE_SEQUENCE_NUMBER
)
403 << " actual: " << decrypter_
->associated_data_
;
406 StringPiece
ciphertext(encrypted
.AsStringPiece().substr(
407 GetStartOfEncryptedData(PACKET_8BYTE_CONNECTION_ID
, includes_version
,
408 PACKET_6BYTE_SEQUENCE_NUMBER
)));
409 if (ciphertext
!= decrypter_
->ciphertext_
) {
410 LOG(ERROR
) << "Decrypted incorrect ciphertext data. expected "
411 << ciphertext
<< " actual: "
412 << decrypter_
->ciphertext_
;
418 char* AsChars(unsigned char* data
) {
419 return reinterpret_cast<char*>(data
);
422 void CheckProcessingFails(unsigned char* packet
,
424 string expected_error
,
425 QuicErrorCode error_code
) {
426 QuicEncryptedPacket
encrypted(AsChars(packet
), len
, false);
427 EXPECT_FALSE(framer_
.ProcessPacket(encrypted
)) << "len: " << len
;
428 EXPECT_EQ(expected_error
, framer_
.detailed_error()) << "len: " << len
;
429 EXPECT_EQ(error_code
, framer_
.error()) << "len: " << len
;
432 // Checks if the supplied string matches data in the supplied StreamFrame.
433 void CheckStreamFrameData(string str
, QuicStreamFrame
* frame
) {
434 EXPECT_EQ(str
, frame
->data
);
437 void CheckStreamFrameBoundaries(unsigned char* packet
,
438 size_t stream_id_size
,
439 bool include_version
) {
440 // Now test framing boundaries.
441 for (size_t i
= kQuicFrameTypeSize
; i
< GetMinStreamFrameSize(); ++i
) {
442 string expected_error
;
443 if (i
< kQuicFrameTypeSize
+ stream_id_size
) {
444 expected_error
= "Unable to read stream_id.";
445 } else if (i
< kQuicFrameTypeSize
+ stream_id_size
+
446 kQuicMaxStreamOffsetSize
) {
447 expected_error
= "Unable to read offset.";
449 expected_error
= "Unable to read frame data.";
451 CheckProcessingFails(
453 i
+ GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID
, include_version
,
454 PACKET_6BYTE_SEQUENCE_NUMBER
,
456 expected_error
, QUIC_INVALID_STREAM_DATA
);
460 void CheckCalculatePacketSequenceNumber(
461 QuicPacketSequenceNumber expected_sequence_number
,
462 QuicPacketSequenceNumber last_sequence_number
) {
463 QuicPacketSequenceNumber wire_sequence_number
=
464 expected_sequence_number
& kMask
;
465 QuicFramerPeer::SetLastSequenceNumber(&framer_
, last_sequence_number
);
466 EXPECT_EQ(expected_sequence_number
,
467 QuicFramerPeer::CalculatePacketSequenceNumberFromWire(
468 &framer_
, PACKET_6BYTE_SEQUENCE_NUMBER
, wire_sequence_number
))
469 << "last_sequence_number: " << last_sequence_number
470 << " wire_sequence_number: " << wire_sequence_number
;
473 QuicPacket
* BuildDataPacket(const QuicPacketHeader
& header
,
474 const QuicFrames
& frames
) {
475 return BuildUnsizedDataPacket(&framer_
, header
, frames
);
478 QuicPacket
* BuildDataPacket(const QuicPacketHeader
& header
,
479 const QuicFrames
& frames
,
480 size_t packet_size
) {
481 return BuildUnsizedDataPacket(&framer_
, header
, frames
, packet_size
);
484 test::TestEncrypter
* encrypter_
;
485 test::TestDecrypter
* decrypter_
;
486 QuicVersion version_
;
489 test::TestQuicVisitor visitor_
;
490 test::TestEntropyCalculator entropy_calculator_
;
493 // Run all framer tests with all supported versions of QUIC.
494 INSTANTIATE_TEST_CASE_P(QuicFramerTests
,
496 ::testing::ValuesIn(kSupportedQuicVersions
));
498 TEST_P(QuicFramerTest
, CalculatePacketSequenceNumberFromWireNearEpochStart
) {
499 // A few quick manual sanity checks
500 CheckCalculatePacketSequenceNumber(UINT64_C(1), UINT64_C(0));
501 CheckCalculatePacketSequenceNumber(kEpoch
+ 1, kMask
);
502 CheckCalculatePacketSequenceNumber(kEpoch
, kMask
);
504 // Cases where the last number was close to the start of the range
505 for (uint64 last
= 0; last
< 10; last
++) {
506 // Small numbers should not wrap (even if they're out of order).
507 for (uint64 j
= 0; j
< 10; j
++) {
508 CheckCalculatePacketSequenceNumber(j
, last
);
511 // Large numbers should not wrap either (because we're near 0 already).
512 for (uint64 j
= 0; j
< 10; j
++) {
513 CheckCalculatePacketSequenceNumber(kEpoch
- 1 - j
, last
);
518 TEST_P(QuicFramerTest
, CalculatePacketSequenceNumberFromWireNearEpochEnd
) {
519 // Cases where the last number was close to the end of the range
520 for (uint64 i
= 0; i
< 10; i
++) {
521 QuicPacketSequenceNumber last
= kEpoch
- i
;
523 // Small numbers should wrap.
524 for (uint64 j
= 0; j
< 10; j
++) {
525 CheckCalculatePacketSequenceNumber(kEpoch
+ j
, last
);
528 // Large numbers should not (even if they're out of order).
529 for (uint64 j
= 0; j
< 10; j
++) {
530 CheckCalculatePacketSequenceNumber(kEpoch
- 1 - j
, last
);
535 // Next check where we're in a non-zero epoch to verify we handle
536 // reverse wrapping, too.
537 TEST_P(QuicFramerTest
, CalculatePacketSequenceNumberFromWireNearPrevEpoch
) {
538 const uint64 prev_epoch
= 1 * kEpoch
;
539 const uint64 cur_epoch
= 2 * kEpoch
;
540 // Cases where the last number was close to the start of the range
541 for (uint64 i
= 0; i
< 10; i
++) {
542 uint64 last
= cur_epoch
+ i
;
543 // Small number should not wrap (even if they're out of order).
544 for (uint64 j
= 0; j
< 10; j
++) {
545 CheckCalculatePacketSequenceNumber(cur_epoch
+ j
, last
);
548 // But large numbers should reverse wrap.
549 for (uint64 j
= 0; j
< 10; j
++) {
550 uint64 num
= kEpoch
- 1 - j
;
551 CheckCalculatePacketSequenceNumber(prev_epoch
+ num
, last
);
556 TEST_P(QuicFramerTest
, CalculatePacketSequenceNumberFromWireNearNextEpoch
) {
557 const uint64 cur_epoch
= 2 * kEpoch
;
558 const uint64 next_epoch
= 3 * kEpoch
;
559 // Cases where the last number was close to the end of the range
560 for (uint64 i
= 0; i
< 10; i
++) {
561 QuicPacketSequenceNumber last
= next_epoch
- 1 - i
;
563 // Small numbers should wrap.
564 for (uint64 j
= 0; j
< 10; j
++) {
565 CheckCalculatePacketSequenceNumber(next_epoch
+ j
, last
);
568 // but large numbers should not (even if they're out of order).
569 for (uint64 j
= 0; j
< 10; j
++) {
570 uint64 num
= kEpoch
- 1 - j
;
571 CheckCalculatePacketSequenceNumber(cur_epoch
+ num
, last
);
576 TEST_P(QuicFramerTest
, CalculatePacketSequenceNumberFromWireNearNextMax
) {
577 const uint64 max_number
= numeric_limits
<uint64
>::max();
578 const uint64 max_epoch
= max_number
& ~kMask
;
580 // Cases where the last number was close to the end of the range
581 for (uint64 i
= 0; i
< 10; i
++) {
582 // Subtract 1, because the expected next sequence number is 1 more than the
583 // last sequence number.
584 QuicPacketSequenceNumber last
= max_number
- i
- 1;
586 // Small numbers should not wrap, because they have nowhere to go.
587 for (uint64 j
= 0; j
< 10; j
++) {
588 CheckCalculatePacketSequenceNumber(max_epoch
+ j
, last
);
591 // Large numbers should not wrap either.
592 for (uint64 j
= 0; j
< 10; j
++) {
593 uint64 num
= kEpoch
- 1 - j
;
594 CheckCalculatePacketSequenceNumber(max_epoch
+ num
, last
);
599 TEST_P(QuicFramerTest
, EmptyPacket
) {
600 char packet
[] = { 0x00 };
601 QuicEncryptedPacket
encrypted(packet
, 0, false);
602 EXPECT_FALSE(framer_
.ProcessPacket(encrypted
));
603 EXPECT_EQ(QUIC_INVALID_PACKET_HEADER
, framer_
.error());
606 TEST_P(QuicFramerTest
, LargePacket
) {
607 unsigned char packet
[kMaxPacketSize
+ 1] = {
608 // public flags (8 byte connection_id)
619 // packet sequence number
630 memset(packet
+ GetPacketHeaderSize(
631 PACKET_8BYTE_CONNECTION_ID
, !kIncludeVersion
,
632 PACKET_6BYTE_SEQUENCE_NUMBER
, NOT_IN_FEC_GROUP
), 0,
633 kMaxPacketSize
- GetPacketHeaderSize(
634 PACKET_8BYTE_CONNECTION_ID
, !kIncludeVersion
,
635 PACKET_6BYTE_SEQUENCE_NUMBER
, NOT_IN_FEC_GROUP
) + 1);
637 QuicEncryptedPacket
encrypted(AsChars(packet
), arraysize(packet
), false);
638 EXPECT_FALSE(framer_
.ProcessPacket(encrypted
));
640 ASSERT_TRUE(visitor_
.header_
.get());
641 // Make sure we've parsed the packet header, so we can send an error.
642 EXPECT_EQ(UINT64_C(0xFEDCBA9876543210),
643 visitor_
.header_
->public_header
.connection_id
);
644 // Make sure the correct error is propagated.
645 EXPECT_EQ(QUIC_PACKET_TOO_LARGE
, framer_
.error());
648 TEST_P(QuicFramerTest
, PacketHeader
) {
649 unsigned char packet
[] = {
650 // public flags (8 byte connection_id)
653 0x10, 0x32, 0x54, 0x76,
654 0x98, 0xBA, 0xDC, 0xFE,
655 // packet sequence number
656 0xBC, 0x9A, 0x78, 0x56,
662 QuicEncryptedPacket
encrypted(AsChars(packet
), arraysize(packet
), false);
663 EXPECT_FALSE(framer_
.ProcessPacket(encrypted
));
664 EXPECT_EQ(QUIC_MISSING_PAYLOAD
, framer_
.error());
665 ASSERT_TRUE(visitor_
.header_
.get());
666 EXPECT_EQ(UINT64_C(0xFEDCBA9876543210),
667 visitor_
.header_
->public_header
.connection_id
);
668 EXPECT_FALSE(visitor_
.header_
->public_header
.reset_flag
);
669 EXPECT_FALSE(visitor_
.header_
->public_header
.version_flag
);
670 EXPECT_FALSE(visitor_
.header_
->fec_flag
);
671 EXPECT_FALSE(visitor_
.header_
->entropy_flag
);
672 EXPECT_EQ(0, visitor_
.header_
->entropy_hash
);
673 EXPECT_EQ(UINT64_C(0x123456789ABC),
674 visitor_
.header_
->packet_sequence_number
);
675 EXPECT_EQ(NOT_IN_FEC_GROUP
, visitor_
.header_
->is_in_fec_group
);
676 EXPECT_EQ(0x00u
, visitor_
.header_
->fec_group
);
678 // Now test framing boundaries.
680 i
< GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID
, !kIncludeVersion
,
681 PACKET_6BYTE_SEQUENCE_NUMBER
, NOT_IN_FEC_GROUP
);
683 string expected_error
;
684 if (i
< kConnectionIdOffset
) {
685 expected_error
= "Unable to read public flags.";
686 } else if (i
< GetSequenceNumberOffset(!kIncludeVersion
)) {
687 expected_error
= "Unable to read ConnectionId.";
688 } else if (i
< GetPrivateFlagsOffset(!kIncludeVersion
)) {
689 expected_error
= "Unable to read sequence number.";
690 } else if (i
< GetFecGroupOffset(!kIncludeVersion
)) {
691 expected_error
= "Unable to read private flags.";
693 expected_error
= "Unable to read first fec protected packet offset.";
695 CheckProcessingFails(packet
, i
, expected_error
, QUIC_INVALID_PACKET_HEADER
);
699 TEST_P(QuicFramerTest
, PacketHeaderWith4ByteConnectionId
) {
700 QuicFramerPeer::SetLastSerializedConnectionId(
701 &framer_
, UINT64_C(0xFEDCBA9876543210));
703 unsigned char packet
[] = {
704 // public flags (4 byte connection_id)
707 0x10, 0x32, 0x54, 0x76,
708 // packet sequence number
709 0xBC, 0x9A, 0x78, 0x56,
715 QuicEncryptedPacket
encrypted(AsChars(packet
), arraysize(packet
), false);
716 EXPECT_FALSE(framer_
.ProcessPacket(encrypted
));
717 EXPECT_EQ(QUIC_MISSING_PAYLOAD
, framer_
.error());
718 ASSERT_TRUE(visitor_
.header_
.get());
719 EXPECT_EQ(UINT64_C(0xFEDCBA9876543210),
720 visitor_
.header_
->public_header
.connection_id
);
721 EXPECT_FALSE(visitor_
.header_
->public_header
.reset_flag
);
722 EXPECT_FALSE(visitor_
.header_
->public_header
.version_flag
);
723 EXPECT_FALSE(visitor_
.header_
->fec_flag
);
724 EXPECT_FALSE(visitor_
.header_
->entropy_flag
);
725 EXPECT_EQ(0, visitor_
.header_
->entropy_hash
);
726 EXPECT_EQ(UINT64_C(0x123456789ABC),
727 visitor_
.header_
->packet_sequence_number
);
728 EXPECT_EQ(NOT_IN_FEC_GROUP
, visitor_
.header_
->is_in_fec_group
);
729 EXPECT_EQ(0x00u
, visitor_
.header_
->fec_group
);
731 // Now test framing boundaries.
733 i
< GetPacketHeaderSize(PACKET_4BYTE_CONNECTION_ID
, !kIncludeVersion
,
734 PACKET_6BYTE_SEQUENCE_NUMBER
, NOT_IN_FEC_GROUP
);
736 string expected_error
;
737 if (i
< kConnectionIdOffset
) {
738 expected_error
= "Unable to read public flags.";
739 } else if (i
< GetSequenceNumberOffset(PACKET_4BYTE_CONNECTION_ID
,
741 expected_error
= "Unable to read ConnectionId.";
742 } else if (i
< GetPrivateFlagsOffset(PACKET_4BYTE_CONNECTION_ID
,
744 expected_error
= "Unable to read sequence number.";
745 } else if (i
< GetFecGroupOffset(PACKET_4BYTE_CONNECTION_ID
,
747 expected_error
= "Unable to read private flags.";
749 expected_error
= "Unable to read first fec protected packet offset.";
751 CheckProcessingFails(packet
, i
, expected_error
, QUIC_INVALID_PACKET_HEADER
);
755 TEST_P(QuicFramerTest
, PacketHeader1ByteConnectionId
) {
756 QuicFramerPeer::SetLastSerializedConnectionId(
757 &framer_
, UINT64_C(0xFEDCBA9876543210));
759 unsigned char packet
[] = {
760 // public flags (1 byte connection_id)
764 // packet sequence number
765 0xBC, 0x9A, 0x78, 0x56,
771 QuicEncryptedPacket
encrypted(AsChars(packet
), arraysize(packet
), false);
772 EXPECT_FALSE(framer_
.ProcessPacket(encrypted
));
773 EXPECT_EQ(QUIC_MISSING_PAYLOAD
, framer_
.error());
774 ASSERT_TRUE(visitor_
.header_
.get());
775 EXPECT_EQ(UINT64_C(0xFEDCBA9876543210),
776 visitor_
.header_
->public_header
.connection_id
);
777 EXPECT_FALSE(visitor_
.header_
->public_header
.reset_flag
);
778 EXPECT_FALSE(visitor_
.header_
->public_header
.version_flag
);
779 EXPECT_FALSE(visitor_
.header_
->fec_flag
);
780 EXPECT_FALSE(visitor_
.header_
->entropy_flag
);
781 EXPECT_EQ(0, visitor_
.header_
->entropy_hash
);
782 EXPECT_EQ(UINT64_C(0x123456789ABC),
783 visitor_
.header_
->packet_sequence_number
);
784 EXPECT_EQ(NOT_IN_FEC_GROUP
, visitor_
.header_
->is_in_fec_group
);
785 EXPECT_EQ(0x00u
, visitor_
.header_
->fec_group
);
787 // Now test framing boundaries.
789 i
< GetPacketHeaderSize(PACKET_1BYTE_CONNECTION_ID
, !kIncludeVersion
,
790 PACKET_6BYTE_SEQUENCE_NUMBER
, NOT_IN_FEC_GROUP
);
792 string expected_error
;
793 if (i
< kConnectionIdOffset
) {
794 expected_error
= "Unable to read public flags.";
795 } else if (i
< GetSequenceNumberOffset(PACKET_1BYTE_CONNECTION_ID
,
797 expected_error
= "Unable to read ConnectionId.";
798 } else if (i
< GetPrivateFlagsOffset(PACKET_1BYTE_CONNECTION_ID
,
800 expected_error
= "Unable to read sequence number.";
801 } else if (i
< GetFecGroupOffset(PACKET_1BYTE_CONNECTION_ID
,
803 expected_error
= "Unable to read private flags.";
805 expected_error
= "Unable to read first fec protected packet offset.";
807 CheckProcessingFails(packet
, i
, expected_error
, QUIC_INVALID_PACKET_HEADER
);
811 TEST_P(QuicFramerTest
, PacketHeaderWith0ByteConnectionId
) {
812 QuicFramerPeer::SetLastSerializedConnectionId(
813 &framer_
, UINT64_C(0xFEDCBA9876543210));
815 unsigned char packet
[] = {
816 // public flags (0 byte connection_id)
819 // packet sequence number
820 0xBC, 0x9A, 0x78, 0x56,
826 QuicEncryptedPacket
encrypted(AsChars(packet
), arraysize(packet
), false);
827 EXPECT_FALSE(framer_
.ProcessPacket(encrypted
));
828 EXPECT_EQ(QUIC_MISSING_PAYLOAD
, framer_
.error());
829 ASSERT_TRUE(visitor_
.header_
.get());
830 EXPECT_EQ(UINT64_C(0xFEDCBA9876543210),
831 visitor_
.header_
->public_header
.connection_id
);
832 EXPECT_FALSE(visitor_
.header_
->public_header
.reset_flag
);
833 EXPECT_FALSE(visitor_
.header_
->public_header
.version_flag
);
834 EXPECT_FALSE(visitor_
.header_
->fec_flag
);
835 EXPECT_FALSE(visitor_
.header_
->entropy_flag
);
836 EXPECT_EQ(0, visitor_
.header_
->entropy_hash
);
837 EXPECT_EQ(UINT64_C(0x123456789ABC),
838 visitor_
.header_
->packet_sequence_number
);
839 EXPECT_EQ(NOT_IN_FEC_GROUP
, visitor_
.header_
->is_in_fec_group
);
840 EXPECT_EQ(0x00u
, visitor_
.header_
->fec_group
);
842 // Now test framing boundaries.
844 i
< GetPacketHeaderSize(PACKET_0BYTE_CONNECTION_ID
, !kIncludeVersion
,
845 PACKET_6BYTE_SEQUENCE_NUMBER
, NOT_IN_FEC_GROUP
);
847 string expected_error
;
848 if (i
< kConnectionIdOffset
) {
849 expected_error
= "Unable to read public flags.";
850 } else if (i
< GetSequenceNumberOffset(PACKET_0BYTE_CONNECTION_ID
,
852 expected_error
= "Unable to read ConnectionId.";
853 } else if (i
< GetPrivateFlagsOffset(PACKET_0BYTE_CONNECTION_ID
,
855 expected_error
= "Unable to read sequence number.";
856 } else if (i
< GetFecGroupOffset(PACKET_0BYTE_CONNECTION_ID
,
858 expected_error
= "Unable to read private flags.";
860 expected_error
= "Unable to read first fec protected packet offset.";
862 CheckProcessingFails(packet
, i
, expected_error
, QUIC_INVALID_PACKET_HEADER
);
866 TEST_P(QuicFramerTest
, PacketHeaderWithVersionFlag
) {
867 unsigned char packet
[] = {
868 // public flags (version)
871 0x10, 0x32, 0x54, 0x76,
872 0x98, 0xBA, 0xDC, 0xFE,
874 'Q', '0', GetQuicVersionDigitTens(), GetQuicVersionDigitOnes(),
875 // packet sequence number
876 0xBC, 0x9A, 0x78, 0x56,
882 QuicEncryptedPacket
encrypted(AsChars(packet
), arraysize(packet
), false);
883 EXPECT_FALSE(framer_
.ProcessPacket(encrypted
));
884 EXPECT_EQ(QUIC_MISSING_PAYLOAD
, framer_
.error());
885 ASSERT_TRUE(visitor_
.header_
.get());
886 EXPECT_EQ(UINT64_C(0xFEDCBA9876543210),
887 visitor_
.header_
->public_header
.connection_id
);
888 EXPECT_FALSE(visitor_
.header_
->public_header
.reset_flag
);
889 EXPECT_TRUE(visitor_
.header_
->public_header
.version_flag
);
890 EXPECT_EQ(GetParam(), visitor_
.header_
->public_header
.versions
[0]);
891 EXPECT_FALSE(visitor_
.header_
->fec_flag
);
892 EXPECT_FALSE(visitor_
.header_
->entropy_flag
);
893 EXPECT_EQ(0, visitor_
.header_
->entropy_hash
);
894 EXPECT_EQ(UINT64_C(0x123456789ABC),
895 visitor_
.header_
->packet_sequence_number
);
896 EXPECT_EQ(NOT_IN_FEC_GROUP
, visitor_
.header_
->is_in_fec_group
);
897 EXPECT_EQ(0x00u
, visitor_
.header_
->fec_group
);
899 // Now test framing boundaries.
901 i
< GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID
, kIncludeVersion
,
902 PACKET_6BYTE_SEQUENCE_NUMBER
, NOT_IN_FEC_GROUP
);
904 string expected_error
;
905 if (i
< kConnectionIdOffset
) {
906 expected_error
= "Unable to read public flags.";
907 } else if (i
< kVersionOffset
) {
908 expected_error
= "Unable to read ConnectionId.";
909 } else if (i
< GetSequenceNumberOffset(kIncludeVersion
)) {
910 expected_error
= "Unable to read protocol version.";
911 } else if (i
< GetPrivateFlagsOffset(kIncludeVersion
)) {
912 expected_error
= "Unable to read sequence number.";
913 } else if (i
< GetFecGroupOffset(kIncludeVersion
)) {
914 expected_error
= "Unable to read private flags.";
916 expected_error
= "Unable to read first fec protected packet offset.";
918 CheckProcessingFails(packet
, i
, expected_error
, QUIC_INVALID_PACKET_HEADER
);
922 TEST_P(QuicFramerTest
, PacketHeaderWith4ByteSequenceNumber
) {
923 QuicFramerPeer::SetLastSequenceNumber(&framer_
, UINT64_C(0x123456789ABA));
925 unsigned char packet
[] = {
926 // public flags (8 byte connection_id and 4 byte sequence number)
929 0x10, 0x32, 0x54, 0x76,
930 0x98, 0xBA, 0xDC, 0xFE,
931 // packet sequence number
932 0xBC, 0x9A, 0x78, 0x56,
937 QuicEncryptedPacket
encrypted(AsChars(packet
), arraysize(packet
), false);
938 EXPECT_FALSE(framer_
.ProcessPacket(encrypted
));
939 EXPECT_EQ(QUIC_MISSING_PAYLOAD
, framer_
.error());
940 ASSERT_TRUE(visitor_
.header_
.get());
941 EXPECT_EQ(UINT64_C(0xFEDCBA9876543210),
942 visitor_
.header_
->public_header
.connection_id
);
943 EXPECT_FALSE(visitor_
.header_
->public_header
.reset_flag
);
944 EXPECT_FALSE(visitor_
.header_
->public_header
.version_flag
);
945 EXPECT_FALSE(visitor_
.header_
->fec_flag
);
946 EXPECT_FALSE(visitor_
.header_
->entropy_flag
);
947 EXPECT_EQ(0, visitor_
.header_
->entropy_hash
);
948 EXPECT_EQ(UINT64_C(0x123456789ABC), visitor_
.header_
->packet_sequence_number
);
949 EXPECT_EQ(NOT_IN_FEC_GROUP
, visitor_
.header_
->is_in_fec_group
);
950 EXPECT_EQ(0x00u
, visitor_
.header_
->fec_group
);
952 // Now test framing boundaries.
954 i
< GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID
, !kIncludeVersion
,
955 PACKET_4BYTE_SEQUENCE_NUMBER
, NOT_IN_FEC_GROUP
);
957 string expected_error
;
958 if (i
< kConnectionIdOffset
) {
959 expected_error
= "Unable to read public flags.";
960 } else if (i
< GetSequenceNumberOffset(!kIncludeVersion
)) {
961 expected_error
= "Unable to read ConnectionId.";
962 } else if (i
< GetPrivateFlagsOffset(!kIncludeVersion
,
963 PACKET_4BYTE_SEQUENCE_NUMBER
)) {
964 expected_error
= "Unable to read sequence number.";
965 } else if (i
< GetFecGroupOffset(!kIncludeVersion
,
966 PACKET_4BYTE_SEQUENCE_NUMBER
)) {
967 expected_error
= "Unable to read private flags.";
969 expected_error
= "Unable to read first fec protected packet offset.";
971 CheckProcessingFails(packet
, i
, expected_error
, QUIC_INVALID_PACKET_HEADER
);
975 TEST_P(QuicFramerTest
, PacketHeaderWith2ByteSequenceNumber
) {
976 QuicFramerPeer::SetLastSequenceNumber(&framer_
, UINT64_C(0x123456789ABA));
978 unsigned char packet
[] = {
979 // public flags (8 byte connection_id and 2 byte sequence number)
982 0x10, 0x32, 0x54, 0x76,
983 0x98, 0xBA, 0xDC, 0xFE,
984 // packet sequence number
990 QuicEncryptedPacket
encrypted(AsChars(packet
), arraysize(packet
), false);
991 EXPECT_FALSE(framer_
.ProcessPacket(encrypted
));
992 EXPECT_EQ(QUIC_MISSING_PAYLOAD
, framer_
.error());
993 ASSERT_TRUE(visitor_
.header_
.get());
994 EXPECT_EQ(UINT64_C(0xFEDCBA9876543210),
995 visitor_
.header_
->public_header
.connection_id
);
996 EXPECT_FALSE(visitor_
.header_
->public_header
.reset_flag
);
997 EXPECT_FALSE(visitor_
.header_
->public_header
.version_flag
);
998 EXPECT_FALSE(visitor_
.header_
->fec_flag
);
999 EXPECT_FALSE(visitor_
.header_
->entropy_flag
);
1000 EXPECT_EQ(0, visitor_
.header_
->entropy_hash
);
1001 EXPECT_EQ(UINT64_C(0x123456789ABC), visitor_
.header_
->packet_sequence_number
);
1002 EXPECT_EQ(NOT_IN_FEC_GROUP
, visitor_
.header_
->is_in_fec_group
);
1003 EXPECT_EQ(0x00u
, visitor_
.header_
->fec_group
);
1005 // Now test framing boundaries.
1007 i
< GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID
, !kIncludeVersion
,
1008 PACKET_2BYTE_SEQUENCE_NUMBER
, NOT_IN_FEC_GROUP
);
1010 string expected_error
;
1011 if (i
< kConnectionIdOffset
) {
1012 expected_error
= "Unable to read public flags.";
1013 } else if (i
< GetSequenceNumberOffset(!kIncludeVersion
)) {
1014 expected_error
= "Unable to read ConnectionId.";
1015 } else if (i
< GetPrivateFlagsOffset(!kIncludeVersion
,
1016 PACKET_2BYTE_SEQUENCE_NUMBER
)) {
1017 expected_error
= "Unable to read sequence number.";
1018 } else if (i
< GetFecGroupOffset(!kIncludeVersion
,
1019 PACKET_2BYTE_SEQUENCE_NUMBER
)) {
1020 expected_error
= "Unable to read private flags.";
1022 expected_error
= "Unable to read first fec protected packet offset.";
1024 CheckProcessingFails(packet
, i
, expected_error
, QUIC_INVALID_PACKET_HEADER
);
1028 TEST_P(QuicFramerTest
, PacketHeaderWith1ByteSequenceNumber
) {
1029 QuicFramerPeer::SetLastSequenceNumber(&framer_
, UINT64_C(0x123456789ABA));
1031 unsigned char packet
[] = {
1032 // public flags (8 byte connection_id and 1 byte sequence number)
1035 0x10, 0x32, 0x54, 0x76,
1036 0x98, 0xBA, 0xDC, 0xFE,
1037 // packet sequence number
1043 QuicEncryptedPacket
encrypted(AsChars(packet
), arraysize(packet
), false);
1044 EXPECT_FALSE(framer_
.ProcessPacket(encrypted
));
1045 EXPECT_EQ(QUIC_MISSING_PAYLOAD
, framer_
.error());
1046 ASSERT_TRUE(visitor_
.header_
.get());
1047 EXPECT_EQ(UINT64_C(0xFEDCBA9876543210),
1048 visitor_
.header_
->public_header
.connection_id
);
1049 EXPECT_FALSE(visitor_
.header_
->public_header
.reset_flag
);
1050 EXPECT_FALSE(visitor_
.header_
->public_header
.version_flag
);
1051 EXPECT_FALSE(visitor_
.header_
->fec_flag
);
1052 EXPECT_FALSE(visitor_
.header_
->entropy_flag
);
1053 EXPECT_EQ(0, visitor_
.header_
->entropy_hash
);
1054 EXPECT_EQ(UINT64_C(0x123456789ABC), visitor_
.header_
->packet_sequence_number
);
1055 EXPECT_EQ(NOT_IN_FEC_GROUP
, visitor_
.header_
->is_in_fec_group
);
1056 EXPECT_EQ(0x00u
, visitor_
.header_
->fec_group
);
1058 // Now test framing boundaries.
1060 i
< GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID
, !kIncludeVersion
,
1061 PACKET_1BYTE_SEQUENCE_NUMBER
, NOT_IN_FEC_GROUP
);
1063 string expected_error
;
1064 if (i
< kConnectionIdOffset
) {
1065 expected_error
= "Unable to read public flags.";
1066 } else if (i
< GetSequenceNumberOffset(!kIncludeVersion
)) {
1067 expected_error
= "Unable to read ConnectionId.";
1068 } else if (i
< GetPrivateFlagsOffset(!kIncludeVersion
,
1069 PACKET_1BYTE_SEQUENCE_NUMBER
)) {
1070 expected_error
= "Unable to read sequence number.";
1071 } else if (i
< GetFecGroupOffset(!kIncludeVersion
,
1072 PACKET_1BYTE_SEQUENCE_NUMBER
)) {
1073 expected_error
= "Unable to read private flags.";
1075 expected_error
= "Unable to read first fec protected packet offset.";
1077 CheckProcessingFails(packet
, i
, expected_error
, QUIC_INVALID_PACKET_HEADER
);
1081 TEST_P(QuicFramerTest
, InvalidPublicFlag
) {
1082 unsigned char packet
[] = {
1083 // public flags: all flags set but the public reset flag and version flag.
1086 0x10, 0x32, 0x54, 0x76,
1087 0x98, 0xBA, 0xDC, 0xFE,
1088 // packet sequence number
1089 0xBC, 0x9A, 0x78, 0x56,
1094 // frame type (padding)
1096 0x00, 0x00, 0x00, 0x00
1098 CheckProcessingFails(packet
,
1100 "Illegal public flags value.",
1101 QUIC_INVALID_PACKET_HEADER
);
1103 // Now turn off validation.
1104 framer_
.set_validate_flags(false);
1105 QuicEncryptedPacket
encrypted(AsChars(packet
), arraysize(packet
), false);
1106 EXPECT_TRUE(framer_
.ProcessPacket(encrypted
));
1109 TEST_P(QuicFramerTest
, InvalidPublicFlagWithMatchingVersions
) {
1110 unsigned char packet
[] = {
1111 // public flags (8 byte connection_id and version flag and an unknown flag)
1114 0x10, 0x32, 0x54, 0x76,
1115 0x98, 0xBA, 0xDC, 0xFE,
1117 'Q', '0', GetQuicVersionDigitTens(), GetQuicVersionDigitOnes(),
1118 // packet sequence number
1119 0xBC, 0x9A, 0x78, 0x56,
1124 // frame type (padding)
1126 0x00, 0x00, 0x00, 0x00
1128 CheckProcessingFails(packet
,
1130 "Illegal public flags value.",
1131 QUIC_INVALID_PACKET_HEADER
);
1134 TEST_P(QuicFramerTest
, LargePublicFlagWithMismatchedVersions
) {
1135 unsigned char packet
[] = {
1136 // public flags (8 byte connection_id, version flag and an unknown flag)
1139 0x10, 0x32, 0x54, 0x76,
1140 0x98, 0xBA, 0xDC, 0xFE,
1143 // packet sequence number
1144 0xBC, 0x9A, 0x78, 0x56,
1149 // frame type (padding frame)
1151 0x00, 0x00, 0x00, 0x00
1153 QuicEncryptedPacket
encrypted(AsChars(packet
), arraysize(packet
), false);
1154 EXPECT_TRUE(framer_
.ProcessPacket(encrypted
));
1155 EXPECT_EQ(QUIC_NO_ERROR
, framer_
.error());
1156 ASSERT_TRUE(visitor_
.header_
.get());
1157 EXPECT_EQ(0, visitor_
.frame_count_
);
1158 EXPECT_EQ(1, visitor_
.version_mismatch_
);
1161 TEST_P(QuicFramerTest
, InvalidPrivateFlag
) {
1162 unsigned char packet
[] = {
1163 // public flags (8 byte connection_id)
1166 0x10, 0x32, 0x54, 0x76,
1167 0x98, 0xBA, 0xDC, 0xFE,
1168 // packet sequence number
1169 0xBC, 0x9A, 0x78, 0x56,
1174 // frame type (padding)
1176 0x00, 0x00, 0x00, 0x00
1178 CheckProcessingFails(packet
,
1180 "Illegal private flags value.",
1181 QUIC_INVALID_PACKET_HEADER
);
1184 TEST_P(QuicFramerTest
, InvalidFECGroupOffset
) {
1185 unsigned char packet
[] = {
1186 // public flags (8 byte connection_id)
1189 0x10, 0x32, 0x54, 0x76,
1190 0x98, 0xBA, 0xDC, 0xFE,
1191 // packet sequence number
1192 0x01, 0x00, 0x00, 0x00,
1194 // private flags (fec group)
1196 // first fec protected packet offset
1199 CheckProcessingFails(packet
,
1201 "First fec protected packet offset must be less "
1202 "than the sequence number.",
1203 QUIC_INVALID_PACKET_HEADER
);
1206 TEST_P(QuicFramerTest
, PaddingFrame
) {
1207 unsigned char packet
[] = {
1208 // public flags (8 byte connection_id)
1211 0x10, 0x32, 0x54, 0x76,
1212 0x98, 0xBA, 0xDC, 0xFE,
1213 // packet sequence number
1214 0xBC, 0x9A, 0x78, 0x56,
1219 // frame type (padding frame)
1221 // Ignored data (which in this case is a stream frame)
1222 // frame type (stream frame with fin)
1225 0x04, 0x03, 0x02, 0x01,
1227 0x54, 0x76, 0x10, 0x32,
1228 0xDC, 0xFE, 0x98, 0xBA,
1237 QuicEncryptedPacket
encrypted(AsChars(packet
), arraysize(packet
), false);
1238 EXPECT_TRUE(framer_
.ProcessPacket(encrypted
));
1239 EXPECT_EQ(QUIC_NO_ERROR
, framer_
.error());
1240 ASSERT_TRUE(visitor_
.header_
.get());
1241 EXPECT_TRUE(CheckDecryption(encrypted
, !kIncludeVersion
));
1243 ASSERT_EQ(0u, visitor_
.stream_frames_
.size());
1244 EXPECT_EQ(0u, visitor_
.ack_frames_
.size());
1245 // A packet with no frames is not acceptable.
1246 CheckProcessingFails(
1248 GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID
, !kIncludeVersion
,
1249 PACKET_6BYTE_SEQUENCE_NUMBER
, NOT_IN_FEC_GROUP
),
1250 "Packet has no frames.", QUIC_MISSING_PAYLOAD
);
1253 TEST_P(QuicFramerTest
, StreamFrame
) {
1254 unsigned char packet
[] = {
1255 // public flags (8 byte connection_id)
1258 0x10, 0x32, 0x54, 0x76,
1259 0x98, 0xBA, 0xDC, 0xFE,
1260 // packet sequence number
1261 0xBC, 0x9A, 0x78, 0x56,
1266 // frame type (stream frame with fin)
1269 0x04, 0x03, 0x02, 0x01,
1271 0x54, 0x76, 0x10, 0x32,
1272 0xDC, 0xFE, 0x98, 0xBA,
1281 QuicEncryptedPacket
encrypted(AsChars(packet
), arraysize(packet
), false);
1282 EXPECT_TRUE(framer_
.ProcessPacket(encrypted
));
1284 EXPECT_EQ(QUIC_NO_ERROR
, framer_
.error());
1285 ASSERT_TRUE(visitor_
.header_
.get());
1286 EXPECT_TRUE(CheckDecryption(encrypted
, !kIncludeVersion
));
1288 ASSERT_EQ(1u, visitor_
.stream_frames_
.size());
1289 EXPECT_EQ(0u, visitor_
.ack_frames_
.size());
1290 EXPECT_EQ(static_cast<uint64
>(0x01020304),
1291 visitor_
.stream_frames_
[0]->stream_id
);
1292 EXPECT_TRUE(visitor_
.stream_frames_
[0]->fin
);
1293 EXPECT_EQ(UINT64_C(0xBA98FEDC32107654), visitor_
.stream_frames_
[0]->offset
);
1294 CheckStreamFrameData("hello world!", visitor_
.stream_frames_
[0]);
1296 // Now test framing boundaries.
1297 CheckStreamFrameBoundaries(packet
, kQuicMaxStreamIdSize
, !kIncludeVersion
);
1300 TEST_P(QuicFramerTest
, StreamFrame3ByteStreamId
) {
1301 unsigned char packet
[] = {
1302 // public flags (8 byte connection_id)
1305 0x10, 0x32, 0x54, 0x76,
1306 0x98, 0xBA, 0xDC, 0xFE,
1307 // packet sequence number
1308 0xBC, 0x9A, 0x78, 0x56,
1313 // frame type (stream frame with fin)
1318 0x54, 0x76, 0x10, 0x32,
1319 0xDC, 0xFE, 0x98, 0xBA,
1328 QuicEncryptedPacket
encrypted(AsChars(packet
), arraysize(packet
), false);
1329 EXPECT_TRUE(framer_
.ProcessPacket(encrypted
));
1331 EXPECT_EQ(QUIC_NO_ERROR
, framer_
.error());
1332 ASSERT_TRUE(visitor_
.header_
.get());
1333 EXPECT_TRUE(CheckDecryption(encrypted
, !kIncludeVersion
));
1335 ASSERT_EQ(1u, visitor_
.stream_frames_
.size());
1336 EXPECT_EQ(0u, visitor_
.ack_frames_
.size());
1337 EXPECT_EQ(UINT64_C(0x00020304), visitor_
.stream_frames_
[0]->stream_id
);
1338 EXPECT_TRUE(visitor_
.stream_frames_
[0]->fin
);
1339 EXPECT_EQ(UINT64_C(0xBA98FEDC32107654), visitor_
.stream_frames_
[0]->offset
);
1340 CheckStreamFrameData("hello world!", visitor_
.stream_frames_
[0]);
1342 // Now test framing boundaries.
1343 const size_t stream_id_size
= 3;
1344 CheckStreamFrameBoundaries(packet
, stream_id_size
, !kIncludeVersion
);
1347 TEST_P(QuicFramerTest
, StreamFrame2ByteStreamId
) {
1348 unsigned char packet
[] = {
1349 // public flags (8 byte connection_id)
1352 0x10, 0x32, 0x54, 0x76,
1353 0x98, 0xBA, 0xDC, 0xFE,
1354 // packet sequence number
1355 0xBC, 0x9A, 0x78, 0x56,
1360 // frame type (stream frame with fin)
1365 0x54, 0x76, 0x10, 0x32,
1366 0xDC, 0xFE, 0x98, 0xBA,
1375 QuicEncryptedPacket
encrypted(AsChars(packet
), arraysize(packet
), false);
1376 EXPECT_TRUE(framer_
.ProcessPacket(encrypted
));
1378 EXPECT_EQ(QUIC_NO_ERROR
, framer_
.error());
1379 ASSERT_TRUE(visitor_
.header_
.get());
1380 EXPECT_TRUE(CheckDecryption(encrypted
, !kIncludeVersion
));
1382 ASSERT_EQ(1u, visitor_
.stream_frames_
.size());
1383 EXPECT_EQ(0u, visitor_
.ack_frames_
.size());
1384 EXPECT_EQ(static_cast<uint64
>(0x00000304),
1385 visitor_
.stream_frames_
[0]->stream_id
);
1386 EXPECT_TRUE(visitor_
.stream_frames_
[0]->fin
);
1387 EXPECT_EQ(UINT64_C(0xBA98FEDC32107654), visitor_
.stream_frames_
[0]->offset
);
1388 CheckStreamFrameData("hello world!", visitor_
.stream_frames_
[0]);
1390 // Now test framing boundaries.
1391 const size_t stream_id_size
= 2;
1392 CheckStreamFrameBoundaries(packet
, stream_id_size
, !kIncludeVersion
);
1395 TEST_P(QuicFramerTest
, StreamFrame1ByteStreamId
) {
1396 unsigned char packet
[] = {
1397 // public flags (8 byte connection_id)
1400 0x10, 0x32, 0x54, 0x76,
1401 0x98, 0xBA, 0xDC, 0xFE,
1402 // packet sequence number
1403 0xBC, 0x9A, 0x78, 0x56,
1408 // frame type (stream frame with fin)
1413 0x54, 0x76, 0x10, 0x32,
1414 0xDC, 0xFE, 0x98, 0xBA,
1423 QuicEncryptedPacket
encrypted(AsChars(packet
), arraysize(packet
), false);
1424 EXPECT_TRUE(framer_
.ProcessPacket(encrypted
));
1426 EXPECT_EQ(QUIC_NO_ERROR
, framer_
.error());
1427 ASSERT_TRUE(visitor_
.header_
.get());
1428 EXPECT_TRUE(CheckDecryption(encrypted
, !kIncludeVersion
));
1430 ASSERT_EQ(1u, visitor_
.stream_frames_
.size());
1431 EXPECT_EQ(0u, visitor_
.ack_frames_
.size());
1432 EXPECT_EQ(static_cast<uint64
>(0x00000004),
1433 visitor_
.stream_frames_
[0]->stream_id
);
1434 EXPECT_TRUE(visitor_
.stream_frames_
[0]->fin
);
1435 EXPECT_EQ(UINT64_C(0xBA98FEDC32107654), visitor_
.stream_frames_
[0]->offset
);
1436 CheckStreamFrameData("hello world!", visitor_
.stream_frames_
[0]);
1438 // Now test framing boundaries.
1439 const size_t stream_id_size
= 1;
1440 CheckStreamFrameBoundaries(packet
, stream_id_size
, !kIncludeVersion
);
1443 TEST_P(QuicFramerTest
, StreamFrameWithVersion
) {
1444 unsigned char packet
[] = {
1445 // public flags (version, 8 byte connection_id)
1448 0x10, 0x32, 0x54, 0x76,
1449 0x98, 0xBA, 0xDC, 0xFE,
1451 'Q', '0', GetQuicVersionDigitTens(), GetQuicVersionDigitOnes(),
1452 // packet sequence number
1453 0xBC, 0x9A, 0x78, 0x56,
1458 // frame type (stream frame with fin)
1461 0x04, 0x03, 0x02, 0x01,
1463 0x54, 0x76, 0x10, 0x32,
1464 0xDC, 0xFE, 0x98, 0xBA,
1473 QuicEncryptedPacket
encrypted(AsChars(packet
), arraysize(packet
), false);
1474 EXPECT_TRUE(framer_
.ProcessPacket(encrypted
));
1476 EXPECT_EQ(QUIC_NO_ERROR
, framer_
.error());
1477 ASSERT_TRUE(visitor_
.header_
.get());
1478 EXPECT_TRUE(visitor_
.header_
->public_header
.version_flag
);
1479 EXPECT_EQ(GetParam(), visitor_
.header_
->public_header
.versions
[0]);
1480 EXPECT_TRUE(CheckDecryption(encrypted
, kIncludeVersion
));
1482 ASSERT_EQ(1u, visitor_
.stream_frames_
.size());
1483 EXPECT_EQ(0u, visitor_
.ack_frames_
.size());
1484 EXPECT_EQ(static_cast<uint64
>(0x01020304),
1485 visitor_
.stream_frames_
[0]->stream_id
);
1486 EXPECT_TRUE(visitor_
.stream_frames_
[0]->fin
);
1487 EXPECT_EQ(UINT64_C(0xBA98FEDC32107654), visitor_
.stream_frames_
[0]->offset
);
1488 CheckStreamFrameData("hello world!", visitor_
.stream_frames_
[0]);
1490 // Now test framing boundaries.
1491 CheckStreamFrameBoundaries(packet
, kQuicMaxStreamIdSize
, kIncludeVersion
);
1494 TEST_P(QuicFramerTest
, RejectPacket
) {
1495 visitor_
.accept_packet_
= false;
1497 unsigned char packet
[] = {
1498 // public flags (8 byte connection_id)
1501 0x10, 0x32, 0x54, 0x76,
1502 0x98, 0xBA, 0xDC, 0xFE,
1503 // packet sequence number
1504 0xBC, 0x9A, 0x78, 0x56,
1509 // frame type (stream frame with fin)
1512 0x04, 0x03, 0x02, 0x01,
1514 0x54, 0x76, 0x10, 0x32,
1515 0xDC, 0xFE, 0x98, 0xBA,
1524 QuicEncryptedPacket
encrypted(AsChars(packet
), arraysize(packet
), false);
1525 EXPECT_TRUE(framer_
.ProcessPacket(encrypted
));
1527 EXPECT_EQ(QUIC_NO_ERROR
, framer_
.error());
1528 ASSERT_TRUE(visitor_
.header_
.get());
1529 EXPECT_TRUE(CheckDecryption(encrypted
, !kIncludeVersion
));
1531 ASSERT_EQ(0u, visitor_
.stream_frames_
.size());
1532 EXPECT_EQ(0u, visitor_
.ack_frames_
.size());
1535 TEST_P(QuicFramerTest
, RejectPublicHeader
) {
1536 visitor_
.accept_public_header_
= false;
1538 unsigned char packet
[] = {
1539 // public flags (8 byte connection_id)
1542 0x10, 0x32, 0x54, 0x76,
1543 0x98, 0xBA, 0xDC, 0xFE,
1546 QuicEncryptedPacket
encrypted(AsChars(packet
), arraysize(packet
), false);
1547 EXPECT_TRUE(framer_
.ProcessPacket(encrypted
));
1549 EXPECT_EQ(QUIC_NO_ERROR
, framer_
.error());
1550 ASSERT_TRUE(visitor_
.public_header_
.get());
1551 ASSERT_FALSE(visitor_
.header_
.get());
1554 TEST_P(QuicFramerTest
, RevivedStreamFrame
) {
1555 unsigned char payload
[] = {
1556 // frame type (stream frame with fin)
1559 0x04, 0x03, 0x02, 0x01,
1561 0x54, 0x76, 0x10, 0x32,
1562 0xDC, 0xFE, 0x98, 0xBA,
1571 QuicPacketHeader header
;
1572 header
.public_header
.connection_id
= UINT64_C(0xFEDCBA9876543210);
1573 header
.public_header
.reset_flag
= false;
1574 header
.public_header
.version_flag
= false;
1575 header
.fec_flag
= true;
1576 header
.entropy_flag
= true;
1577 header
.packet_sequence_number
= UINT64_C(0x123456789ABC);
1578 header
.fec_group
= 0;
1580 // Do not encrypt the payload because the revived payload is post-encryption.
1581 EXPECT_TRUE(framer_
.ProcessRevivedPacket(&header
,
1582 StringPiece(AsChars(payload
),
1583 arraysize(payload
))));
1585 EXPECT_EQ(QUIC_NO_ERROR
, framer_
.error());
1586 ASSERT_EQ(1, visitor_
.revived_packets_
);
1587 ASSERT_TRUE(visitor_
.header_
.get());
1588 EXPECT_EQ(UINT64_C(0xFEDCBA9876543210),
1589 visitor_
.header_
->public_header
.connection_id
);
1590 EXPECT_FALSE(visitor_
.header_
->public_header
.reset_flag
);
1591 EXPECT_FALSE(visitor_
.header_
->public_header
.version_flag
);
1592 EXPECT_TRUE(visitor_
.header_
->fec_flag
);
1593 EXPECT_TRUE(visitor_
.header_
->entropy_flag
);
1594 EXPECT_EQ(1 << (header
.packet_sequence_number
% 8),
1595 visitor_
.header_
->entropy_hash
);
1596 EXPECT_EQ(UINT64_C(0x123456789ABC), visitor_
.header_
->packet_sequence_number
);
1597 EXPECT_EQ(NOT_IN_FEC_GROUP
, visitor_
.header_
->is_in_fec_group
);
1598 EXPECT_EQ(0x00u
, visitor_
.header_
->fec_group
);
1600 ASSERT_EQ(1u, visitor_
.stream_frames_
.size());
1601 EXPECT_EQ(0u, visitor_
.ack_frames_
.size());
1602 EXPECT_EQ(UINT64_C(0x01020304), visitor_
.stream_frames_
[0]->stream_id
);
1603 EXPECT_TRUE(visitor_
.stream_frames_
[0]->fin
);
1604 EXPECT_EQ(UINT64_C(0xBA98FEDC32107654), visitor_
.stream_frames_
[0]->offset
);
1605 CheckStreamFrameData("hello world!", visitor_
.stream_frames_
[0]);
1608 TEST_P(QuicFramerTest
, StreamFrameInFecGroup
) {
1609 unsigned char packet
[] = {
1610 // public flags (8 byte connection_id)
1613 0x10, 0x32, 0x54, 0x76,
1614 0x98, 0xBA, 0xDC, 0xFE,
1615 // packet sequence number
1616 0xBC, 0x9A, 0x78, 0x56,
1618 // private flags (fec group)
1620 // first fec protected packet offset
1623 // frame type (stream frame with fin)
1626 0x04, 0x03, 0x02, 0x01,
1628 0x54, 0x76, 0x10, 0x32,
1629 0xDC, 0xFE, 0x98, 0xBA,
1638 QuicEncryptedPacket
encrypted(AsChars(packet
), arraysize(packet
), false);
1639 EXPECT_TRUE(framer_
.ProcessPacket(encrypted
));
1641 EXPECT_EQ(QUIC_NO_ERROR
, framer_
.error());
1642 ASSERT_TRUE(visitor_
.header_
.get());
1643 EXPECT_TRUE(CheckDecryption(encrypted
, !kIncludeVersion
));
1644 EXPECT_EQ(IN_FEC_GROUP
, visitor_
.header_
->is_in_fec_group
);
1645 EXPECT_EQ(UINT64_C(0x341256789ABA), visitor_
.header_
->fec_group
);
1646 const size_t fec_offset
=
1647 GetStartOfFecProtectedData(PACKET_8BYTE_CONNECTION_ID
,
1649 PACKET_6BYTE_SEQUENCE_NUMBER
);
1651 string(AsChars(packet
) + fec_offset
, arraysize(packet
) - fec_offset
),
1652 visitor_
.fec_protected_payload_
);
1654 ASSERT_EQ(1u, visitor_
.stream_frames_
.size());
1655 EXPECT_EQ(0u, visitor_
.ack_frames_
.size());
1656 EXPECT_EQ(UINT64_C(0x01020304), visitor_
.stream_frames_
[0]->stream_id
);
1657 EXPECT_TRUE(visitor_
.stream_frames_
[0]->fin
);
1658 EXPECT_EQ(UINT64_C(0xBA98FEDC32107654), visitor_
.stream_frames_
[0]->offset
);
1659 CheckStreamFrameData("hello world!", visitor_
.stream_frames_
[0]);
1662 TEST_P(QuicFramerTest
, AckFrameTwoTimestamp
) {
1663 unsigned char packet
[] = {
1664 // public flags (8 byte connection_id)
1667 0x10, 0x32, 0x54, 0x76, 0x98, 0xBA, 0xDC, 0xFE,
1668 // packet sequence number
1669 0xA8, 0x9A, 0x78, 0x56, 0x34, 0x12,
1670 // private flags (entropy)
1673 // frame type (ack frame)
1674 // (has nacks, not truncated, 6 byte largest observed, 1 byte delta)
1676 // entropy hash of all received packets.
1678 // largest observed packet sequence number
1679 0xBF, 0x9A, 0x78, 0x56, 0x34, 0x12,
1682 // Number of timestamps.
1684 // Delta from largest observed.
1687 0x10, 0x32, 0x54, 0x76,
1688 // Delta from largest observed.
1692 // num missing packets
1694 // missing packet delta
1696 // 0 more missing packets in range.
1698 // Number of revived packets.
1702 QuicEncryptedPacket
encrypted(AsChars(packet
), arraysize(packet
), false);
1703 EXPECT_TRUE(framer_
.ProcessPacket(encrypted
));
1705 EXPECT_EQ(QUIC_NO_ERROR
, framer_
.error());
1706 ASSERT_TRUE(visitor_
.header_
.get());
1707 EXPECT_TRUE(CheckDecryption(encrypted
, !kIncludeVersion
));
1709 EXPECT_EQ(0u, visitor_
.stream_frames_
.size());
1710 ASSERT_EQ(1u, visitor_
.ack_frames_
.size());
1711 const QuicAckFrame
& frame
= *visitor_
.ack_frames_
[0];
1712 EXPECT_EQ(0xBA, frame
.entropy_hash
);
1713 EXPECT_EQ(UINT64_C(0x0123456789ABF), frame
.largest_observed
);
1714 ASSERT_EQ(1u, frame
.missing_packets
.size());
1715 ASSERT_EQ(2u, frame
.received_packet_times
.size());
1716 SequenceNumberSet::const_iterator missing_iter
=
1717 frame
.missing_packets
.begin();
1718 EXPECT_EQ(UINT64_C(0x0123456789ABE), *missing_iter
);
1720 const size_t kReceivedEntropyOffset
= kQuicFrameTypeSize
;
1721 const size_t kLargestObservedOffset
= kReceivedEntropyOffset
+
1722 kQuicEntropyHashSize
;
1723 const size_t kMissingDeltaTimeOffset
= kLargestObservedOffset
+
1724 PACKET_6BYTE_SEQUENCE_NUMBER
;
1725 const size_t kNumTimestampsOffset
= kMissingDeltaTimeOffset
+
1726 kQuicDeltaTimeLargestObservedSize
;
1727 const size_t kTimestampDeltaLargestObserved1
= kNumTimestampsOffset
+
1728 kQuicNumTimestampsSize
;
1729 const size_t kTimestampTimeDeltaLargestObserved1
=
1730 kTimestampDeltaLargestObserved1
+ 1;
1731 const size_t kTimestampDeltaLargestObserved2
=
1732 kTimestampTimeDeltaLargestObserved1
+ 4;
1733 const size_t kTimestampTimeDeltaLargestObserved2
=
1734 kTimestampDeltaLargestObserved2
+ 1;
1735 const size_t kNumMissingPacketOffset
=
1736 kTimestampTimeDeltaLargestObserved2
+ 2;
1737 const size_t kMissingPacketsOffset
= kNumMissingPacketOffset
+
1738 kNumberOfNackRangesSize
;
1739 const size_t kMissingPacketsRange
= kMissingPacketsOffset
+
1740 PACKET_1BYTE_SEQUENCE_NUMBER
;
1741 const size_t kRevivedPacketsLength
= kMissingPacketsRange
+
1742 PACKET_1BYTE_SEQUENCE_NUMBER
;
1743 // Now test framing boundaries.
1744 const size_t ack_frame_size
= kRevivedPacketsLength
+
1745 PACKET_1BYTE_SEQUENCE_NUMBER
;
1746 for (size_t i
= kQuicFrameTypeSize
; i
< ack_frame_size
; ++i
) {
1747 string expected_error
;
1748 if (i
< kLargestObservedOffset
) {
1749 expected_error
= "Unable to read entropy hash for received packets.";
1750 } else if (i
< kMissingDeltaTimeOffset
) {
1751 expected_error
= "Unable to read largest observed.";
1752 } else if (i
< kNumTimestampsOffset
) {
1753 expected_error
= "Unable to read delta time largest observed.";
1754 } else if (i
< kTimestampDeltaLargestObserved1
) {
1755 expected_error
= "Unable to read num received packets.";
1756 } else if (i
< kTimestampTimeDeltaLargestObserved1
) {
1757 expected_error
= "Unable to read sequence delta in received packets.";
1758 } else if (i
< kTimestampDeltaLargestObserved2
) {
1759 expected_error
= "Unable to read time delta in received packets.";
1760 } else if (i
< kTimestampTimeDeltaLargestObserved2
) {
1761 expected_error
= "Unable to read sequence delta in received packets.";
1762 } else if (i
< kNumMissingPacketOffset
) {
1764 "Unable to read incremental time delta in received packets.";
1765 } else if (i
< kMissingPacketsOffset
) {
1766 expected_error
= "Unable to read num missing packet ranges.";
1767 } else if (i
< kMissingPacketsRange
) {
1768 expected_error
= "Unable to read missing sequence number delta.";
1769 } else if (i
< kRevivedPacketsLength
) {
1770 expected_error
= "Unable to read missing sequence number range.";
1772 expected_error
= "Unable to read num revived packets.";
1774 CheckProcessingFails(
1776 i
+ GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID
, !kIncludeVersion
,
1777 PACKET_6BYTE_SEQUENCE_NUMBER
, NOT_IN_FEC_GROUP
),
1778 expected_error
, QUIC_INVALID_ACK_DATA
);
1783 TEST_P(QuicFramerTest
, AckFrameOneTimestamp
) {
1784 unsigned char packet
[] = {
1785 // public flags (8 byte connection_id)
1788 0x10, 0x32, 0x54, 0x76, 0x98, 0xBA, 0xDC, 0xFE,
1789 // packet sequence number
1790 0xA8, 0x9A, 0x78, 0x56, 0x34, 0x12,
1791 // private flags (entropy)
1794 // frame type (ack frame)
1795 // (has nacks, not truncated, 6 byte largest observed, 1 byte delta)
1797 // entropy hash of all received packets.
1799 // largest observed packet sequence number
1800 0xBF, 0x9A, 0x78, 0x56, 0x34, 0x12,
1803 // Number of timestamps.
1805 // Delta from largest observed.
1808 0x10, 0x32, 0x54, 0x76,
1809 // num missing packets
1811 // missing packet delta
1813 // 0 more missing packets in range.
1815 // Number of revived packets.
1819 QuicEncryptedPacket
encrypted(AsChars(packet
), arraysize(packet
), false);
1820 EXPECT_TRUE(framer_
.ProcessPacket(encrypted
));
1822 EXPECT_EQ(QUIC_NO_ERROR
, framer_
.error());
1823 ASSERT_TRUE(visitor_
.header_
.get());
1824 EXPECT_TRUE(CheckDecryption(encrypted
, !kIncludeVersion
));
1826 EXPECT_EQ(0u, visitor_
.stream_frames_
.size());
1827 ASSERT_EQ(1u, visitor_
.ack_frames_
.size());
1828 const QuicAckFrame
& frame
= *visitor_
.ack_frames_
[0];
1829 EXPECT_EQ(0xBA, frame
.entropy_hash
);
1830 EXPECT_EQ(UINT64_C(0x0123456789ABF), frame
.largest_observed
);
1831 ASSERT_EQ(1u, frame
.missing_packets
.size());
1832 ASSERT_EQ(1u, frame
.received_packet_times
.size());
1833 SequenceNumberSet::const_iterator missing_iter
=
1834 frame
.missing_packets
.begin();
1835 EXPECT_EQ(UINT64_C(0x0123456789ABE), *missing_iter
);
1837 const size_t kReceivedEntropyOffset
= kQuicFrameTypeSize
;
1838 const size_t kLargestObservedOffset
= kReceivedEntropyOffset
+
1839 kQuicEntropyHashSize
;
1840 const size_t kMissingDeltaTimeOffset
= kLargestObservedOffset
+
1841 PACKET_6BYTE_SEQUENCE_NUMBER
;
1842 const size_t kNumTimestampsOffset
= kMissingDeltaTimeOffset
+
1843 kQuicDeltaTimeLargestObservedSize
;
1844 const size_t kTimestampDeltaLargestObserved
= kNumTimestampsOffset
+
1845 kQuicNumTimestampsSize
;
1846 const size_t kTimestampTimeDeltaLargestObserved
=
1847 kTimestampDeltaLargestObserved
+ 1;
1848 const size_t kNumMissingPacketOffset
= kTimestampTimeDeltaLargestObserved
+ 4;
1849 const size_t kMissingPacketsOffset
= kNumMissingPacketOffset
+
1850 kNumberOfNackRangesSize
;
1851 const size_t kMissingPacketsRange
= kMissingPacketsOffset
+
1852 PACKET_1BYTE_SEQUENCE_NUMBER
;
1853 const size_t kRevivedPacketsLength
= kMissingPacketsRange
+
1854 PACKET_1BYTE_SEQUENCE_NUMBER
;
1855 // Now test framing boundaries.
1856 const size_t ack_frame_size
= kRevivedPacketsLength
+
1857 PACKET_1BYTE_SEQUENCE_NUMBER
;
1858 for (size_t i
= kQuicFrameTypeSize
; i
< ack_frame_size
; ++i
) {
1859 string expected_error
;
1860 if (i
< kLargestObservedOffset
) {
1861 expected_error
= "Unable to read entropy hash for received packets.";
1862 } else if (i
< kMissingDeltaTimeOffset
) {
1863 expected_error
= "Unable to read largest observed.";
1864 } else if (i
< kNumTimestampsOffset
) {
1865 expected_error
= "Unable to read delta time largest observed.";
1866 } else if (i
< kTimestampDeltaLargestObserved
) {
1867 expected_error
= "Unable to read num received packets.";
1868 } else if (i
< kTimestampTimeDeltaLargestObserved
) {
1869 expected_error
= "Unable to read sequence delta in received packets.";
1870 } else if (i
< kNumMissingPacketOffset
) {
1871 expected_error
= "Unable to read time delta in received packets.";
1872 } else if (i
< kMissingPacketsOffset
) {
1873 expected_error
= "Unable to read num missing packet ranges.";
1874 } else if (i
< kMissingPacketsRange
) {
1875 expected_error
= "Unable to read missing sequence number delta.";
1876 } else if (i
< kRevivedPacketsLength
) {
1877 expected_error
= "Unable to read missing sequence number range.";
1879 expected_error
= "Unable to read num revived packets.";
1881 CheckProcessingFails(
1883 i
+ GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID
, !kIncludeVersion
,
1884 PACKET_6BYTE_SEQUENCE_NUMBER
, NOT_IN_FEC_GROUP
),
1885 expected_error
, QUIC_INVALID_ACK_DATA
);
1890 TEST_P(QuicFramerTest
, AckFrame
) {
1891 unsigned char packet
[] = {
1892 // public flags (8 byte connection_id)
1895 0x10, 0x32, 0x54, 0x76, 0x98, 0xBA, 0xDC, 0xFE,
1896 // packet sequence number
1897 0xA8, 0x9A, 0x78, 0x56, 0x34, 0x12,
1898 // private flags (entropy)
1901 // frame type (ack frame)
1902 // (has nacks, not truncated, 6 byte largest observed, 1 byte delta)
1904 // entropy hash of all received packets.
1906 // largest observed packet sequence number
1907 0xBF, 0x9A, 0x78, 0x56, 0x34, 0x12,
1910 // Number of timestamps.
1912 // num missing packets
1914 // missing packet delta
1916 // 0 more missing packets in range.
1918 // Number of revived packets.
1922 QuicEncryptedPacket
encrypted(AsChars(packet
), arraysize(packet
), false);
1923 EXPECT_TRUE(framer_
.ProcessPacket(encrypted
));
1925 EXPECT_EQ(QUIC_NO_ERROR
, framer_
.error());
1926 ASSERT_TRUE(visitor_
.header_
.get());
1927 EXPECT_TRUE(CheckDecryption(encrypted
, !kIncludeVersion
));
1929 EXPECT_EQ(0u, visitor_
.stream_frames_
.size());
1930 ASSERT_EQ(1u, visitor_
.ack_frames_
.size());
1931 const QuicAckFrame
& frame
= *visitor_
.ack_frames_
[0];
1932 EXPECT_EQ(0xBA, frame
.entropy_hash
);
1933 EXPECT_EQ(UINT64_C(0x0123456789ABF), frame
.largest_observed
);
1934 ASSERT_EQ(1u, frame
.missing_packets
.size());
1935 SequenceNumberSet::const_iterator missing_iter
=
1936 frame
.missing_packets
.begin();
1937 EXPECT_EQ(UINT64_C(0x0123456789ABE), *missing_iter
);
1939 const size_t kReceivedEntropyOffset
= kQuicFrameTypeSize
;
1940 const size_t kLargestObservedOffset
= kReceivedEntropyOffset
+
1941 kQuicEntropyHashSize
;
1942 const size_t kMissingDeltaTimeOffset
= kLargestObservedOffset
+
1943 PACKET_6BYTE_SEQUENCE_NUMBER
;
1944 const size_t kNumTimestampsOffset
= kMissingDeltaTimeOffset
+
1945 kQuicDeltaTimeLargestObservedSize
;
1946 const size_t kNumMissingPacketOffset
= kNumTimestampsOffset
+
1947 kQuicNumTimestampsSize
;
1948 const size_t kMissingPacketsOffset
= kNumMissingPacketOffset
+
1949 kNumberOfNackRangesSize
;
1950 const size_t kMissingPacketsRange
= kMissingPacketsOffset
+
1951 PACKET_1BYTE_SEQUENCE_NUMBER
;
1952 const size_t kRevivedPacketsLength
= kMissingPacketsRange
+
1953 PACKET_1BYTE_SEQUENCE_NUMBER
;
1954 // Now test framing boundaries.
1955 const size_t ack_frame_size
= kRevivedPacketsLength
+
1956 PACKET_1BYTE_SEQUENCE_NUMBER
;
1957 for (size_t i
= kQuicFrameTypeSize
; i
< ack_frame_size
; ++i
) {
1958 string expected_error
;
1959 if (i
< kLargestObservedOffset
) {
1960 expected_error
= "Unable to read entropy hash for received packets.";
1961 } else if (i
< kMissingDeltaTimeOffset
) {
1962 expected_error
= "Unable to read largest observed.";
1963 } else if (i
< kNumTimestampsOffset
) {
1964 expected_error
= "Unable to read delta time largest observed.";
1965 } else if (i
< kNumMissingPacketOffset
) {
1966 expected_error
= "Unable to read num received packets.";
1967 } else if (i
< kMissingPacketsOffset
) {
1968 expected_error
= "Unable to read num missing packet ranges.";
1969 } else if (i
< kMissingPacketsRange
) {
1970 expected_error
= "Unable to read missing sequence number delta.";
1971 } else if (i
< kRevivedPacketsLength
) {
1972 expected_error
= "Unable to read missing sequence number range.";
1974 expected_error
= "Unable to read num revived packets.";
1976 CheckProcessingFails(
1978 i
+ GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID
, !kIncludeVersion
,
1979 PACKET_6BYTE_SEQUENCE_NUMBER
, NOT_IN_FEC_GROUP
),
1980 expected_error
, QUIC_INVALID_ACK_DATA
);
1984 TEST_P(QuicFramerTest
, AckFrameRevivedPackets
) {
1985 unsigned char packet
[] = {
1986 // public flags (8 byte connection_id)
1989 0x10, 0x32, 0x54, 0x76, 0x98, 0xBA, 0xDC, 0xFE,
1990 // packet sequence number
1991 0xA8, 0x9A, 0x78, 0x56, 0x34, 0x12,
1992 // private flags (entropy)
1995 // frame type (ack frame)
1996 // (has nacks, not truncated, 6 byte largest observed, 1 byte delta)
1998 // entropy hash of all received packets.
2000 // largest observed packet sequence number
2001 0xBF, 0x9A, 0x78, 0x56, 0x34, 0x12,
2004 // num received packets.
2006 // num missing packets
2008 // missing packet delta
2010 // 0 more missing packets in range.
2012 // Number of revived packets.
2014 // Revived packet sequence number.
2015 0xBE, 0x9A, 0x78, 0x56, 0x34, 0x12,
2016 // Number of revived packets.
2020 QuicEncryptedPacket
encrypted(AsChars(packet
), arraysize(packet
), false);
2021 EXPECT_TRUE(framer_
.ProcessPacket(encrypted
));
2023 EXPECT_EQ(QUIC_NO_ERROR
, framer_
.error());
2024 ASSERT_TRUE(visitor_
.header_
.get());
2025 EXPECT_TRUE(CheckDecryption(encrypted
, !kIncludeVersion
));
2027 EXPECT_EQ(0u, visitor_
.stream_frames_
.size());
2028 ASSERT_EQ(1u, visitor_
.ack_frames_
.size());
2029 const QuicAckFrame
& frame
= *visitor_
.ack_frames_
[0];
2030 EXPECT_EQ(0xBA, frame
.entropy_hash
);
2031 EXPECT_EQ(UINT64_C(0x0123456789ABF), frame
.largest_observed
);
2032 ASSERT_EQ(1u, frame
.missing_packets
.size());
2033 SequenceNumberSet::const_iterator missing_iter
=
2034 frame
.missing_packets
.begin();
2035 EXPECT_EQ(UINT64_C(0x0123456789ABE), *missing_iter
);
2037 const size_t kReceivedEntropyOffset
= kQuicFrameTypeSize
;
2038 const size_t kLargestObservedOffset
= kReceivedEntropyOffset
+
2039 kQuicEntropyHashSize
;
2040 const size_t kMissingDeltaTimeOffset
= kLargestObservedOffset
+
2041 PACKET_6BYTE_SEQUENCE_NUMBER
;
2042 const size_t kNumTimestampsOffset
= kMissingDeltaTimeOffset
+
2043 kQuicDeltaTimeLargestObservedSize
;
2044 const size_t kNumMissingPacketOffset
= kNumTimestampsOffset
+
2045 kQuicNumTimestampsSize
;
2046 const size_t kMissingPacketsOffset
= kNumMissingPacketOffset
+
2047 kNumberOfNackRangesSize
;
2048 const size_t kMissingPacketsRange
= kMissingPacketsOffset
+
2049 PACKET_1BYTE_SEQUENCE_NUMBER
;
2050 const size_t kRevivedPacketsLength
= kMissingPacketsRange
+
2051 PACKET_1BYTE_SEQUENCE_NUMBER
;
2052 const size_t kRevivedPacketSequenceNumberLength
= kRevivedPacketsLength
+
2053 PACKET_1BYTE_SEQUENCE_NUMBER
;
2054 // Now test framing boundaries.
2055 const size_t ack_frame_size
= kRevivedPacketSequenceNumberLength
+
2056 PACKET_6BYTE_SEQUENCE_NUMBER
;
2057 for (size_t i
= kQuicFrameTypeSize
; i
< ack_frame_size
; ++i
) {
2058 string expected_error
;
2059 if (i
< kReceivedEntropyOffset
) {
2060 expected_error
= "Unable to read least unacked delta.";
2061 } else if (i
< kLargestObservedOffset
) {
2062 expected_error
= "Unable to read entropy hash for received packets.";
2063 } else if (i
< kMissingDeltaTimeOffset
) {
2064 expected_error
= "Unable to read largest observed.";
2065 } else if (i
< kNumTimestampsOffset
) {
2066 expected_error
= "Unable to read delta time largest observed.";
2067 } else if (i
< kNumMissingPacketOffset
) {
2068 expected_error
= "Unable to read num received packets.";
2069 } else if (i
< kMissingPacketsOffset
) {
2070 expected_error
= "Unable to read num missing packet ranges.";
2071 } else if (i
< kMissingPacketsRange
) {
2072 expected_error
= "Unable to read missing sequence number delta.";
2073 } else if (i
< kRevivedPacketsLength
) {
2074 expected_error
= "Unable to read missing sequence number range.";
2075 } else if (i
< kRevivedPacketSequenceNumberLength
) {
2076 expected_error
= "Unable to read num revived packets.";
2078 expected_error
= "Unable to read revived packet.";
2080 CheckProcessingFails(
2082 i
+ GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID
, !kIncludeVersion
,
2083 PACKET_6BYTE_SEQUENCE_NUMBER
, NOT_IN_FEC_GROUP
),
2084 expected_error
, QUIC_INVALID_ACK_DATA
);
2088 TEST_P(QuicFramerTest
, AckFrameNoNacks
) {
2089 unsigned char packet
[] = {
2090 // public flags (8 byte connection_id)
2093 0x10, 0x32, 0x54, 0x76, 0x98, 0xBA, 0xDC, 0xFE,
2094 // packet sequence number
2095 0xA8, 0x9A, 0x78, 0x56, 0x34, 0x12,
2096 // private flags (entropy)
2099 // frame type (ack frame)
2100 // (no nacks, not truncated, 6 byte largest observed, 1 byte delta)
2102 // entropy hash of all received packets.
2104 // largest observed packet sequence number
2105 0xBF, 0x9A, 0x78, 0x56, 0x34, 0x12,
2108 // Number of received packets.
2112 QuicEncryptedPacket
encrypted(AsChars(packet
), arraysize(packet
), false);
2113 EXPECT_TRUE(framer_
.ProcessPacket(encrypted
));
2115 EXPECT_EQ(QUIC_NO_ERROR
, framer_
.error());
2116 ASSERT_TRUE(visitor_
.header_
.get());
2117 EXPECT_TRUE(CheckDecryption(encrypted
, !kIncludeVersion
));
2119 EXPECT_EQ(0u, visitor_
.stream_frames_
.size());
2120 ASSERT_EQ(1u, visitor_
.ack_frames_
.size());
2121 QuicAckFrame
* frame
= visitor_
.ack_frames_
[0];
2122 EXPECT_EQ(0xBA, frame
->entropy_hash
);
2123 EXPECT_EQ(UINT64_C(0x0123456789ABF), frame
->largest_observed
);
2124 ASSERT_EQ(0u, frame
->missing_packets
.size());
2126 // Verify that the packet re-serializes identically.
2128 frames
.push_back(QuicFrame(frame
));
2129 scoped_ptr
<QuicPacket
> data(BuildDataPacket(*visitor_
.header_
, frames
));
2130 ASSERT_TRUE(data
!= nullptr);
2132 test::CompareCharArraysWithHexError("constructed packet", data
->data(),
2133 data
->length(), AsChars(packet
),
2137 TEST_P(QuicFramerTest
, AckFrame500Nacks
) {
2138 unsigned char packet
[] = {
2139 // public flags (8 byte connection_id)
2142 0x10, 0x32, 0x54, 0x76, 0x98, 0xBA, 0xDC, 0xFE,
2143 // packet sequence number
2144 0xA8, 0x9A, 0x78, 0x56, 0x34, 0x12,
2145 // private flags (entropy)
2148 // frame type (ack frame)
2149 // (has nacks, not truncated, 6 byte largest observed, 1 byte delta)
2151 // entropy hash of all received packets.
2153 // largest observed packet sequence number
2154 0xBF, 0x9A, 0x78, 0x56, 0x34, 0x12,
2157 // No received packets.
2159 // num missing packet ranges
2161 // missing packet delta
2163 // 243 more missing packets in range.
2164 // The ranges are listed in this order so the re-constructed packet
2167 // No gap between ranges
2169 // 255 more missing packets in range.
2171 // No revived packets.
2175 QuicEncryptedPacket
encrypted(AsChars(packet
), arraysize(packet
), false);
2176 EXPECT_TRUE(framer_
.ProcessPacket(encrypted
));
2178 EXPECT_EQ(QUIC_NO_ERROR
, framer_
.error());
2179 ASSERT_TRUE(visitor_
.header_
.get());
2180 EXPECT_TRUE(CheckDecryption(encrypted
, !kIncludeVersion
));
2182 EXPECT_EQ(0u, visitor_
.stream_frames_
.size());
2183 ASSERT_EQ(1u, visitor_
.ack_frames_
.size());
2184 QuicAckFrame
* frame
= visitor_
.ack_frames_
[0];
2185 EXPECT_EQ(0xBA, frame
->entropy_hash
);
2186 EXPECT_EQ(UINT64_C(0x0123456789ABF), frame
->largest_observed
);
2187 EXPECT_EQ(0u, frame
->revived_packets
.size());
2188 ASSERT_EQ(500u, frame
->missing_packets
.size());
2189 SequenceNumberSet::const_iterator first_missing_iter
=
2190 frame
->missing_packets
.begin();
2191 EXPECT_EQ(UINT64_C(0x0123456789ABE) - 499, *first_missing_iter
);
2192 SequenceNumberSet::const_reverse_iterator last_missing_iter
=
2193 frame
->missing_packets
.rbegin();
2194 EXPECT_EQ(UINT64_C(0x0123456789ABE), *last_missing_iter
);
2196 // Verify that the packet re-serializes identically.
2198 frames
.push_back(QuicFrame(frame
));
2199 scoped_ptr
<QuicPacket
> data(BuildDataPacket(*visitor_
.header_
, frames
));
2200 ASSERT_TRUE(data
!= nullptr);
2202 test::CompareCharArraysWithHexError("constructed packet",
2203 data
->data(), data
->length(),
2204 AsChars(packet
), arraysize(packet
));
2207 TEST_P(QuicFramerTest
, StopWaitingFrame
) {
2208 unsigned char packet
[] = {
2209 // public flags (8 byte connection_id)
2212 0x10, 0x32, 0x54, 0x76,
2213 0x98, 0xBA, 0xDC, 0xFE,
2214 // packet sequence number
2215 0xA8, 0x9A, 0x78, 0x56,
2217 // private flags (entropy)
2220 // frame type (ack frame)
2221 // (has nacks, not truncated, 6 byte largest observed, 1 byte delta)
2223 // entropy hash of sent packets till least awaiting - 1.
2225 // least packet sequence number awaiting an ack, delta from sequence number.
2226 0x08, 0x00, 0x00, 0x00,
2230 QuicEncryptedPacket
encrypted(AsChars(packet
), arraysize(packet
), false);
2231 EXPECT_TRUE(framer_
.ProcessPacket(encrypted
));
2233 EXPECT_EQ(QUIC_NO_ERROR
, framer_
.error());
2234 ASSERT_TRUE(visitor_
.header_
.get());
2235 EXPECT_TRUE(CheckDecryption(encrypted
, !kIncludeVersion
));
2237 EXPECT_EQ(0u, visitor_
.stream_frames_
.size());
2238 ASSERT_EQ(1u, visitor_
.stop_waiting_frames_
.size());
2239 const QuicStopWaitingFrame
& frame
= *visitor_
.stop_waiting_frames_
[0];
2240 EXPECT_EQ(0xAB, frame
.entropy_hash
);
2241 EXPECT_EQ(UINT64_C(0x0123456789AA0), frame
.least_unacked
);
2243 const size_t kSentEntropyOffset
= kQuicFrameTypeSize
;
2244 const size_t kLeastUnackedOffset
= kSentEntropyOffset
+ kQuicEntropyHashSize
;
2245 const size_t frame_size
= 7;
2246 for (size_t i
= kQuicFrameTypeSize
; i
< frame_size
; ++i
) {
2247 string expected_error
;
2248 if (i
< kLeastUnackedOffset
) {
2249 expected_error
= "Unable to read entropy hash for sent packets.";
2251 expected_error
= "Unable to read least unacked delta.";
2253 CheckProcessingFails(
2255 i
+ GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID
, !kIncludeVersion
,
2256 PACKET_6BYTE_SEQUENCE_NUMBER
, NOT_IN_FEC_GROUP
),
2257 expected_error
, QUIC_INVALID_STOP_WAITING_DATA
);
2261 TEST_P(QuicFramerTest
, RstStreamFrameQuicVersion24
) {
2262 if (version_
> QUIC_VERSION_24
) {
2263 // QUIC_VERSION_25 removes the error_details field from QuicRstStreamFrame.
2267 unsigned char packet
[] = {
2268 // public flags (8 byte connection_id)
2271 0x10, 0x32, 0x54, 0x76,
2272 0x98, 0xBA, 0xDC, 0xFE,
2273 // packet sequence number
2274 0xBC, 0x9A, 0x78, 0x56,
2279 // frame type (rst stream frame)
2282 0x04, 0x03, 0x02, 0x01,
2285 0x01, 0x02, 0x03, 0x04,
2286 0x05, 0x06, 0x07, 0x08,
2289 0x01, 0x00, 0x00, 0x00,
2291 // error details length
2300 QuicEncryptedPacket
encrypted(AsChars(packet
), arraysize(packet
), false);
2301 EXPECT_TRUE(framer_
.ProcessPacket(encrypted
));
2303 EXPECT_EQ(QUIC_NO_ERROR
, framer_
.error());
2304 ASSERT_TRUE(visitor_
.header_
.get());
2305 EXPECT_TRUE(CheckDecryption(encrypted
, !kIncludeVersion
));
2307 EXPECT_EQ(UINT64_C(0x01020304), visitor_
.rst_stream_frame_
.stream_id
);
2308 EXPECT_EQ(0x01, visitor_
.rst_stream_frame_
.error_code
);
2309 EXPECT_EQ("because I can", visitor_
.rst_stream_frame_
.error_details
);
2310 EXPECT_EQ(UINT64_C(0x0807060504030201),
2311 visitor_
.rst_stream_frame_
.byte_offset
);
2313 // Now test framing boundaries.
2314 for (size_t i
= kQuicFrameTypeSize
;
2315 i
< QuicFramer::GetMinRstStreamFrameSize(); ++i
) {
2316 string expected_error
;
2317 if (i
< kQuicFrameTypeSize
+ kQuicMaxStreamIdSize
) {
2318 expected_error
= "Unable to read stream_id.";
2319 } else if (i
< kQuicFrameTypeSize
+ kQuicMaxStreamIdSize
+
2320 kQuicMaxStreamOffsetSize
) {
2321 expected_error
= "Unable to read rst stream sent byte offset.";
2322 } else if (i
< kQuicFrameTypeSize
+ kQuicMaxStreamIdSize
+
2323 kQuicMaxStreamOffsetSize
+ kQuicErrorCodeSize
) {
2324 expected_error
= "Unable to read rst stream error code.";
2326 expected_error
= "Unable to read rst stream error details.";
2328 CheckProcessingFails(
2330 i
+ GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID
, !kIncludeVersion
,
2331 PACKET_6BYTE_SEQUENCE_NUMBER
, NOT_IN_FEC_GROUP
),
2332 expected_error
, QUIC_INVALID_RST_STREAM_DATA
);
2336 TEST_P(QuicFramerTest
, RstStreamFrameQuic
) {
2337 if (version_
<= QUIC_VERSION_24
) {
2338 // QUIC_VERSION_25 removes the error_details field from QuicRstStreamFrame.
2343 unsigned char packet
[] = {
2344 // public flags (8 byte connection_id)
2347 0x10, 0x32, 0x54, 0x76,
2348 0x98, 0xBA, 0xDC, 0xFE,
2349 // packet sequence number
2350 0xBC, 0x9A, 0x78, 0x56,
2355 // frame type (rst stream frame)
2358 0x04, 0x03, 0x02, 0x01,
2361 0x01, 0x02, 0x03, 0x04,
2362 0x05, 0x06, 0x07, 0x08,
2365 0x01, 0x00, 0x00, 0x00,
2369 QuicEncryptedPacket
encrypted(AsChars(packet
), arraysize(packet
), false);
2370 EXPECT_TRUE(framer_
.ProcessPacket(encrypted
));
2372 EXPECT_EQ(QUIC_NO_ERROR
, framer_
.error());
2373 ASSERT_TRUE(visitor_
.header_
.get());
2374 EXPECT_TRUE(CheckDecryption(encrypted
, !kIncludeVersion
));
2376 EXPECT_EQ(UINT64_C(0x01020304), visitor_
.rst_stream_frame_
.stream_id
);
2377 EXPECT_EQ(0x01, visitor_
.rst_stream_frame_
.error_code
);
2378 EXPECT_EQ(UINT64_C(0x0807060504030201),
2379 visitor_
.rst_stream_frame_
.byte_offset
);
2381 // Now test framing boundaries.
2382 for (size_t i
= kQuicFrameTypeSize
; i
< QuicFramer::GetRstStreamFrameSize();
2384 string expected_error
;
2385 if (i
< kQuicFrameTypeSize
+ kQuicMaxStreamIdSize
) {
2386 expected_error
= "Unable to read stream_id.";
2387 } else if (i
< kQuicFrameTypeSize
+ kQuicMaxStreamIdSize
+
2388 kQuicMaxStreamOffsetSize
) {
2389 expected_error
= "Unable to read rst stream sent byte offset.";
2390 } else if (i
< kQuicFrameTypeSize
+ kQuicMaxStreamIdSize
+
2391 kQuicMaxStreamOffsetSize
+ kQuicErrorCodeSize
) {
2392 expected_error
= "Unable to read rst stream error code.";
2394 CheckProcessingFails(
2396 i
+ GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID
, !kIncludeVersion
,
2397 PACKET_6BYTE_SEQUENCE_NUMBER
, NOT_IN_FEC_GROUP
),
2398 expected_error
, QUIC_INVALID_RST_STREAM_DATA
);
2402 TEST_P(QuicFramerTest
, ConnectionCloseFrame
) {
2403 unsigned char packet
[] = {
2404 // public flags (8 byte connection_id)
2407 0x10, 0x32, 0x54, 0x76,
2408 0x98, 0xBA, 0xDC, 0xFE,
2409 // packet sequence number
2410 0xBC, 0x9A, 0x78, 0x56,
2415 // frame type (connection close frame)
2418 0x11, 0x00, 0x00, 0x00,
2420 // error details length
2429 QuicEncryptedPacket
encrypted(AsChars(packet
), arraysize(packet
), false);
2430 EXPECT_TRUE(framer_
.ProcessPacket(encrypted
));
2432 EXPECT_EQ(QUIC_NO_ERROR
, framer_
.error());
2433 ASSERT_TRUE(visitor_
.header_
.get());
2434 EXPECT_TRUE(CheckDecryption(encrypted
, !kIncludeVersion
));
2436 EXPECT_EQ(0u, visitor_
.stream_frames_
.size());
2438 EXPECT_EQ(0x11, visitor_
.connection_close_frame_
.error_code
);
2439 EXPECT_EQ("because I can", visitor_
.connection_close_frame_
.error_details
);
2441 ASSERT_EQ(0u, visitor_
.ack_frames_
.size());
2443 // Now test framing boundaries.
2444 for (size_t i
= kQuicFrameTypeSize
;
2445 i
< QuicFramer::GetMinConnectionCloseFrameSize(); ++i
) {
2446 string expected_error
;
2447 if (i
< kQuicFrameTypeSize
+ kQuicErrorCodeSize
) {
2448 expected_error
= "Unable to read connection close error code.";
2450 expected_error
= "Unable to read connection close error details.";
2452 CheckProcessingFails(
2454 i
+ GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID
, !kIncludeVersion
,
2455 PACKET_6BYTE_SEQUENCE_NUMBER
, NOT_IN_FEC_GROUP
),
2456 expected_error
, QUIC_INVALID_CONNECTION_CLOSE_DATA
);
2460 TEST_P(QuicFramerTest
, GoAwayFrame
) {
2461 unsigned char packet
[] = {
2462 // public flags (8 byte connection_id)
2465 0x10, 0x32, 0x54, 0x76,
2466 0x98, 0xBA, 0xDC, 0xFE,
2467 // packet sequence number
2468 0xBC, 0x9A, 0x78, 0x56,
2473 // frame type (go away frame)
2476 0x09, 0x00, 0x00, 0x00,
2478 0x04, 0x03, 0x02, 0x01,
2479 // error details length
2488 QuicEncryptedPacket
encrypted(AsChars(packet
), arraysize(packet
), false);
2489 EXPECT_TRUE(framer_
.ProcessPacket(encrypted
));
2491 EXPECT_EQ(QUIC_NO_ERROR
, framer_
.error());
2492 ASSERT_TRUE(visitor_
.header_
.get());
2493 EXPECT_TRUE(CheckDecryption(encrypted
, !kIncludeVersion
));
2495 EXPECT_EQ(UINT64_C(0x01020304), visitor_
.goaway_frame_
.last_good_stream_id
);
2496 EXPECT_EQ(0x9, visitor_
.goaway_frame_
.error_code
);
2497 EXPECT_EQ("because I can", visitor_
.goaway_frame_
.reason_phrase
);
2499 const size_t reason_size
= arraysize("because I can") - 1;
2500 // Now test framing boundaries.
2501 for (size_t i
= kQuicFrameTypeSize
;
2502 i
< QuicFramer::GetMinGoAwayFrameSize() + reason_size
; ++i
) {
2503 string expected_error
;
2504 if (i
< kQuicFrameTypeSize
+ kQuicErrorCodeSize
) {
2505 expected_error
= "Unable to read go away error code.";
2506 } else if (i
< kQuicFrameTypeSize
+ kQuicErrorCodeSize
+
2507 kQuicMaxStreamIdSize
) {
2508 expected_error
= "Unable to read last good stream id.";
2510 expected_error
= "Unable to read goaway reason.";
2512 CheckProcessingFails(
2514 i
+ GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID
, !kIncludeVersion
,
2515 PACKET_6BYTE_SEQUENCE_NUMBER
, NOT_IN_FEC_GROUP
),
2516 expected_error
, QUIC_INVALID_GOAWAY_DATA
);
2520 TEST_P(QuicFramerTest
, WindowUpdateFrame
) {
2521 unsigned char packet
[] = {
2522 // public flags (8 byte connection_id)
2525 0x10, 0x32, 0x54, 0x76,
2526 0x98, 0xBA, 0xDC, 0xFE,
2527 // packet sequence number
2528 0xBC, 0x9A, 0x78, 0x56,
2533 // frame type (window update frame)
2536 0x04, 0x03, 0x02, 0x01,
2538 0x05, 0x06, 0x07, 0x08,
2539 0x09, 0x0a, 0x0b, 0x0c,
2542 QuicEncryptedPacket
encrypted(AsChars(packet
), arraysize(packet
), false);
2544 EXPECT_TRUE(framer_
.ProcessPacket(encrypted
));
2546 EXPECT_EQ(QUIC_NO_ERROR
, framer_
.error());
2547 ASSERT_TRUE(visitor_
.header_
.get());
2548 EXPECT_TRUE(CheckDecryption(encrypted
, !kIncludeVersion
));
2550 EXPECT_EQ(UINT64_C(0x01020304), visitor_
.window_update_frame_
.stream_id
);
2551 EXPECT_EQ(UINT64_C(0x0c0b0a0908070605),
2552 visitor_
.window_update_frame_
.byte_offset
);
2554 // Now test framing boundaries.
2555 for (size_t i
= kQuicFrameTypeSize
;
2556 i
< QuicFramer::GetWindowUpdateFrameSize(); ++i
) {
2557 string expected_error
;
2558 if (i
< kQuicFrameTypeSize
+ kQuicMaxStreamIdSize
) {
2559 expected_error
= "Unable to read stream_id.";
2561 expected_error
= "Unable to read window byte_offset.";
2563 CheckProcessingFails(
2565 i
+ GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID
, !kIncludeVersion
,
2566 PACKET_6BYTE_SEQUENCE_NUMBER
, NOT_IN_FEC_GROUP
),
2567 expected_error
, QUIC_INVALID_WINDOW_UPDATE_DATA
);
2571 TEST_P(QuicFramerTest
, BlockedFrame
) {
2572 unsigned char packet
[] = {
2573 // public flags (8 byte connection_id)
2576 0x10, 0x32, 0x54, 0x76,
2577 0x98, 0xBA, 0xDC, 0xFE,
2578 // packet sequence number
2579 0xBC, 0x9A, 0x78, 0x56,
2584 // frame type (blocked frame)
2587 0x04, 0x03, 0x02, 0x01,
2590 QuicEncryptedPacket
encrypted(AsChars(packet
), arraysize(packet
), false);
2592 EXPECT_TRUE(framer_
.ProcessPacket(encrypted
));
2594 EXPECT_EQ(QUIC_NO_ERROR
, framer_
.error());
2595 ASSERT_TRUE(visitor_
.header_
.get());
2596 EXPECT_TRUE(CheckDecryption(encrypted
, !kIncludeVersion
));
2598 EXPECT_EQ(UINT64_C(0x01020304), visitor_
.blocked_frame_
.stream_id
);
2600 // Now test framing boundaries.
2601 for (size_t i
= kQuicFrameTypeSize
; i
< QuicFramer::GetBlockedFrameSize();
2603 string expected_error
= "Unable to read stream_id.";
2604 CheckProcessingFails(
2606 i
+ GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID
, !kIncludeVersion
,
2607 PACKET_6BYTE_SEQUENCE_NUMBER
, NOT_IN_FEC_GROUP
),
2608 expected_error
, QUIC_INVALID_BLOCKED_DATA
);
2612 TEST_P(QuicFramerTest
, PingFrame
) {
2613 unsigned char packet
[] = {
2614 // public flags (8 byte connection_id)
2617 0x10, 0x32, 0x54, 0x76,
2618 0x98, 0xBA, 0xDC, 0xFE,
2619 // packet sequence number
2620 0xBC, 0x9A, 0x78, 0x56,
2625 // frame type (ping frame)
2629 QuicEncryptedPacket
encrypted(AsChars(packet
), arraysize(packet
), false);
2630 EXPECT_TRUE(framer_
.ProcessPacket(encrypted
));
2632 EXPECT_EQ(QUIC_NO_ERROR
, framer_
.error());
2633 ASSERT_TRUE(visitor_
.header_
.get());
2634 EXPECT_TRUE(CheckDecryption(encrypted
, !kIncludeVersion
));
2636 EXPECT_EQ(1u, visitor_
.ping_frames_
.size());
2638 // No need to check the PING frame boundaries because it has no payload.
2641 TEST_P(QuicFramerTest
, PublicResetPacket
) {
2642 unsigned char packet
[] = {
2643 // public flags (public reset, 8 byte connection_id)
2646 0x10, 0x32, 0x54, 0x76,
2647 0x98, 0xBA, 0xDC, 0xFE,
2648 // message tag (kPRST)
2650 // num_entries (2) + padding
2651 0x02, 0x00, 0x00, 0x00,
2655 0x08, 0x00, 0x00, 0x00,
2659 0x10, 0x00, 0x00, 0x00,
2661 0x89, 0x67, 0x45, 0x23,
2662 0x01, 0xEF, 0xCD, 0xAB,
2663 // rejected sequence number
2664 0xBC, 0x9A, 0x78, 0x56,
2665 0x34, 0x12, 0x00, 0x00,
2668 QuicEncryptedPacket
encrypted(AsChars(packet
), arraysize(packet
), false);
2669 EXPECT_TRUE(framer_
.ProcessPacket(encrypted
));
2670 ASSERT_EQ(QUIC_NO_ERROR
, framer_
.error());
2671 ASSERT_TRUE(visitor_
.public_reset_packet_
.get());
2672 EXPECT_EQ(UINT64_C(0xFEDCBA9876543210),
2673 visitor_
.public_reset_packet_
->public_header
.connection_id
);
2674 EXPECT_TRUE(visitor_
.public_reset_packet_
->public_header
.reset_flag
);
2675 EXPECT_FALSE(visitor_
.public_reset_packet_
->public_header
.version_flag
);
2676 EXPECT_EQ(UINT64_C(0xABCDEF0123456789),
2677 visitor_
.public_reset_packet_
->nonce_proof
);
2678 EXPECT_EQ(UINT64_C(0x123456789ABC),
2679 visitor_
.public_reset_packet_
->rejected_sequence_number
);
2681 visitor_
.public_reset_packet_
->client_address
.address().empty());
2683 // Now test framing boundaries.
2684 for (size_t i
= 0; i
< arraysize(packet
); ++i
) {
2685 string expected_error
;
2686 DVLOG(1) << "iteration: " << i
;
2687 if (i
< kConnectionIdOffset
) {
2688 expected_error
= "Unable to read public flags.";
2689 CheckProcessingFails(packet
, i
, expected_error
,
2690 QUIC_INVALID_PACKET_HEADER
);
2691 } else if (i
< kPublicResetPacketMessageTagOffset
) {
2692 expected_error
= "Unable to read ConnectionId.";
2693 CheckProcessingFails(packet
, i
, expected_error
,
2694 QUIC_INVALID_PACKET_HEADER
);
2696 expected_error
= "Unable to read reset message.";
2697 CheckProcessingFails(packet
, i
, expected_error
,
2698 QUIC_INVALID_PUBLIC_RST_PACKET
);
2703 TEST_P(QuicFramerTest
, PublicResetPacketWithTrailingJunk
) {
2704 unsigned char packet
[] = {
2705 // public flags (public reset, 8 byte connection_id)
2708 0x10, 0x32, 0x54, 0x76,
2709 0x98, 0xBA, 0xDC, 0xFE,
2710 // message tag (kPRST)
2712 // num_entries (2) + padding
2713 0x02, 0x00, 0x00, 0x00,
2717 0x08, 0x00, 0x00, 0x00,
2721 0x10, 0x00, 0x00, 0x00,
2723 0x89, 0x67, 0x45, 0x23,
2724 0x01, 0xEF, 0xCD, 0xAB,
2725 // rejected sequence number
2726 0xBC, 0x9A, 0x78, 0x56,
2727 0x34, 0x12, 0x00, 0x00,
2732 string expected_error
= "Unable to read reset message.";
2733 CheckProcessingFails(packet
, arraysize(packet
), expected_error
,
2734 QUIC_INVALID_PUBLIC_RST_PACKET
);
2737 TEST_P(QuicFramerTest
, PublicResetPacketWithClientAddress
) {
2738 unsigned char packet
[] = {
2739 // public flags (public reset, 8 byte connection_id)
2742 0x10, 0x32, 0x54, 0x76,
2743 0x98, 0xBA, 0xDC, 0xFE,
2744 // message tag (kPRST)
2746 // num_entries (3) + padding
2747 0x03, 0x00, 0x00, 0x00,
2751 0x08, 0x00, 0x00, 0x00,
2755 0x10, 0x00, 0x00, 0x00,
2759 0x18, 0x00, 0x00, 0x00,
2761 0x89, 0x67, 0x45, 0x23,
2762 0x01, 0xEF, 0xCD, 0xAB,
2763 // rejected sequence number
2764 0xBC, 0x9A, 0x78, 0x56,
2765 0x34, 0x12, 0x00, 0x00,
2766 // client address: 4.31.198.44:443
2768 0x04, 0x1F, 0xC6, 0x2C,
2772 QuicEncryptedPacket
encrypted(AsChars(packet
), arraysize(packet
), false);
2773 EXPECT_TRUE(framer_
.ProcessPacket(encrypted
));
2774 ASSERT_EQ(QUIC_NO_ERROR
, framer_
.error());
2775 ASSERT_TRUE(visitor_
.public_reset_packet_
.get());
2776 EXPECT_EQ(UINT64_C(0xFEDCBA9876543210),
2777 visitor_
.public_reset_packet_
->public_header
.connection_id
);
2778 EXPECT_TRUE(visitor_
.public_reset_packet_
->public_header
.reset_flag
);
2779 EXPECT_FALSE(visitor_
.public_reset_packet_
->public_header
.version_flag
);
2780 EXPECT_EQ(UINT64_C(0xABCDEF0123456789),
2781 visitor_
.public_reset_packet_
->nonce_proof
);
2782 EXPECT_EQ(UINT64_C(0x123456789ABC),
2783 visitor_
.public_reset_packet_
->rejected_sequence_number
);
2784 EXPECT_EQ("4.31.198.44",
2785 IPAddressToString(visitor_
.public_reset_packet_
->
2786 client_address
.address()));
2787 EXPECT_EQ(443, visitor_
.public_reset_packet_
->client_address
.port());
2789 // Now test framing boundaries.
2790 for (size_t i
= 0; i
< arraysize(packet
); ++i
) {
2791 string expected_error
;
2792 DVLOG(1) << "iteration: " << i
;
2793 if (i
< kConnectionIdOffset
) {
2794 expected_error
= "Unable to read public flags.";
2795 CheckProcessingFails(packet
, i
, expected_error
,
2796 QUIC_INVALID_PACKET_HEADER
);
2797 } else if (i
< kPublicResetPacketMessageTagOffset
) {
2798 expected_error
= "Unable to read ConnectionId.";
2799 CheckProcessingFails(packet
, i
, expected_error
,
2800 QUIC_INVALID_PACKET_HEADER
);
2802 expected_error
= "Unable to read reset message.";
2803 CheckProcessingFails(packet
, i
, expected_error
,
2804 QUIC_INVALID_PUBLIC_RST_PACKET
);
2809 TEST_P(QuicFramerTest
, VersionNegotiationPacket
) {
2810 unsigned char packet
[] = {
2811 // public flags (version, 8 byte connection_id)
2814 0x10, 0x32, 0x54, 0x76,
2815 0x98, 0xBA, 0xDC, 0xFE,
2817 'Q', '0', GetQuicVersionDigitTens(), GetQuicVersionDigitOnes(),
2821 QuicFramerPeer::SetPerspective(&framer_
, Perspective::IS_CLIENT
);
2823 QuicEncryptedPacket
encrypted(AsChars(packet
), arraysize(packet
), false);
2824 EXPECT_TRUE(framer_
.ProcessPacket(encrypted
));
2825 ASSERT_EQ(QUIC_NO_ERROR
, framer_
.error());
2826 ASSERT_TRUE(visitor_
.version_negotiation_packet_
.get());
2827 EXPECT_EQ(2u, visitor_
.version_negotiation_packet_
->versions
.size());
2828 EXPECT_EQ(GetParam(), visitor_
.version_negotiation_packet_
->versions
[0]);
2830 for (size_t i
= 0; i
<= kPublicFlagsSize
+ PACKET_8BYTE_CONNECTION_ID
; ++i
) {
2831 string expected_error
;
2832 QuicErrorCode error_code
= QUIC_INVALID_PACKET_HEADER
;
2833 if (i
< kConnectionIdOffset
) {
2834 expected_error
= "Unable to read public flags.";
2835 } else if (i
< kVersionOffset
) {
2836 expected_error
= "Unable to read ConnectionId.";
2838 expected_error
= "Unable to read supported version in negotiation.";
2839 error_code
= QUIC_INVALID_VERSION_NEGOTIATION_PACKET
;
2841 CheckProcessingFails(packet
, i
, expected_error
, error_code
);
2845 TEST_P(QuicFramerTest
, FecPacket
) {
2846 unsigned char packet
[] = {
2847 // public flags (8 byte connection_id)
2850 0x10, 0x32, 0x54, 0x76,
2851 0x98, 0xBA, 0xDC, 0xFE,
2852 // packet sequence number
2853 0xBC, 0x9A, 0x78, 0x56,
2855 // private flags (fec group & FEC)
2857 // first fec protected packet offset
2867 QuicEncryptedPacket
encrypted(AsChars(packet
), arraysize(packet
), false);
2868 EXPECT_TRUE(framer_
.ProcessPacket(encrypted
));
2870 EXPECT_EQ(QUIC_NO_ERROR
, framer_
.error());
2871 ASSERT_TRUE(visitor_
.header_
.get());
2872 EXPECT_TRUE(CheckDecryption(encrypted
, !kIncludeVersion
));
2874 EXPECT_EQ(0u, visitor_
.stream_frames_
.size());
2875 EXPECT_EQ(0u, visitor_
.ack_frames_
.size());
2876 ASSERT_EQ(1, visitor_
.fec_count_
);
2877 const QuicFecData
& fec_data
= *visitor_
.fec_data_
[0];
2878 EXPECT_EQ(UINT64_C(0x0123456789ABB), fec_data
.fec_group
);
2879 EXPECT_EQ("abcdefghijklmnop", fec_data
.redundancy
);
2882 TEST_P(QuicFramerTest
, BuildPaddingFramePacket
) {
2883 QuicPacketHeader header
;
2884 header
.public_header
.connection_id
= UINT64_C(0xFEDCBA9876543210);
2885 header
.public_header
.reset_flag
= false;
2886 header
.public_header
.version_flag
= false;
2887 header
.fec_flag
= false;
2888 header
.entropy_flag
= false;
2889 header
.packet_sequence_number
= UINT64_C(0x123456789ABC);
2890 header
.fec_group
= 0;
2892 QuicPaddingFrame padding_frame
;
2895 frames
.push_back(QuicFrame(&padding_frame
));
2897 unsigned char packet
[kMaxPacketSize
] = {
2898 // public flags (8 byte connection_id)
2901 0x10, 0x32, 0x54, 0x76,
2902 0x98, 0xBA, 0xDC, 0xFE,
2903 // packet sequence number
2904 0xBC, 0x9A, 0x78, 0x56,
2909 // frame type (padding frame)
2911 0x00, 0x00, 0x00, 0x00
2914 uint64 header_size
=
2915 GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID
, !kIncludeVersion
,
2916 PACKET_6BYTE_SEQUENCE_NUMBER
, NOT_IN_FEC_GROUP
);
2917 memset(packet
+ header_size
+ 1, 0x00, kMaxPacketSize
- header_size
- 1);
2919 scoped_ptr
<QuicPacket
> data(BuildDataPacket(header
, frames
));
2920 ASSERT_TRUE(data
!= nullptr);
2922 test::CompareCharArraysWithHexError("constructed packet",
2923 data
->data(), data
->length(),
2928 TEST_P(QuicFramerTest
, Build4ByteSequenceNumberPaddingFramePacket
) {
2929 QuicPacketHeader header
;
2930 header
.public_header
.connection_id
= UINT64_C(0xFEDCBA9876543210);
2931 header
.public_header
.reset_flag
= false;
2932 header
.public_header
.version_flag
= false;
2933 header
.fec_flag
= false;
2934 header
.entropy_flag
= false;
2935 header
.public_header
.sequence_number_length
= PACKET_4BYTE_SEQUENCE_NUMBER
;
2936 header
.packet_sequence_number
= UINT64_C(0x123456789ABC);
2937 header
.fec_group
= 0;
2939 QuicPaddingFrame padding_frame
;
2942 frames
.push_back(QuicFrame(&padding_frame
));
2944 unsigned char packet
[kMaxPacketSize
] = {
2945 // public flags (8 byte connection_id and 4 byte sequence number)
2948 0x10, 0x32, 0x54, 0x76,
2949 0x98, 0xBA, 0xDC, 0xFE,
2950 // packet sequence number
2951 0xBC, 0x9A, 0x78, 0x56,
2955 // frame type (padding frame)
2957 0x00, 0x00, 0x00, 0x00
2960 uint64 header_size
=
2961 GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID
, !kIncludeVersion
,
2962 PACKET_4BYTE_SEQUENCE_NUMBER
, NOT_IN_FEC_GROUP
);
2963 memset(packet
+ header_size
+ 1, 0x00, kMaxPacketSize
- header_size
- 1);
2965 scoped_ptr
<QuicPacket
> data(BuildDataPacket(header
, frames
));
2966 ASSERT_TRUE(data
!= nullptr);
2968 test::CompareCharArraysWithHexError("constructed packet",
2969 data
->data(), data
->length(),
2974 TEST_P(QuicFramerTest
, Build2ByteSequenceNumberPaddingFramePacket
) {
2975 QuicPacketHeader header
;
2976 header
.public_header
.connection_id
= UINT64_C(0xFEDCBA9876543210);
2977 header
.public_header
.reset_flag
= false;
2978 header
.public_header
.version_flag
= false;
2979 header
.fec_flag
= false;
2980 header
.entropy_flag
= false;
2981 header
.public_header
.sequence_number_length
= PACKET_2BYTE_SEQUENCE_NUMBER
;
2982 header
.packet_sequence_number
= UINT64_C(0x123456789ABC);
2983 header
.fec_group
= 0;
2985 QuicPaddingFrame padding_frame
;
2988 frames
.push_back(QuicFrame(&padding_frame
));
2990 unsigned char packet
[kMaxPacketSize
] = {
2991 // public flags (8 byte connection_id and 2 byte sequence number)
2994 0x10, 0x32, 0x54, 0x76,
2995 0x98, 0xBA, 0xDC, 0xFE,
2996 // packet sequence number
3001 // frame type (padding frame)
3003 0x00, 0x00, 0x00, 0x00
3006 uint64 header_size
=
3007 GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID
, !kIncludeVersion
,
3008 PACKET_2BYTE_SEQUENCE_NUMBER
, NOT_IN_FEC_GROUP
);
3009 memset(packet
+ header_size
+ 1, 0x00, kMaxPacketSize
- header_size
- 1);
3011 scoped_ptr
<QuicPacket
> data(BuildDataPacket(header
, frames
));
3012 ASSERT_TRUE(data
!= nullptr);
3014 test::CompareCharArraysWithHexError("constructed packet",
3015 data
->data(), data
->length(),
3020 TEST_P(QuicFramerTest
, Build1ByteSequenceNumberPaddingFramePacket
) {
3021 QuicPacketHeader header
;
3022 header
.public_header
.connection_id
= UINT64_C(0xFEDCBA9876543210);
3023 header
.public_header
.reset_flag
= false;
3024 header
.public_header
.version_flag
= false;
3025 header
.fec_flag
= false;
3026 header
.entropy_flag
= false;
3027 header
.public_header
.sequence_number_length
= PACKET_1BYTE_SEQUENCE_NUMBER
;
3028 header
.packet_sequence_number
= UINT64_C(0x123456789ABC);
3029 header
.fec_group
= 0;
3031 QuicPaddingFrame padding_frame
;
3034 frames
.push_back(QuicFrame(&padding_frame
));
3036 unsigned char packet
[kMaxPacketSize
] = {
3037 // public flags (8 byte connection_id and 1 byte sequence number)
3040 0x10, 0x32, 0x54, 0x76,
3041 0x98, 0xBA, 0xDC, 0xFE,
3042 // packet sequence number
3047 // frame type (padding frame)
3049 0x00, 0x00, 0x00, 0x00
3052 uint64 header_size
=
3053 GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID
, !kIncludeVersion
,
3054 PACKET_1BYTE_SEQUENCE_NUMBER
, NOT_IN_FEC_GROUP
);
3055 memset(packet
+ header_size
+ 1, 0x00, kMaxPacketSize
- header_size
- 1);
3057 scoped_ptr
<QuicPacket
> data(BuildDataPacket(header
, frames
));
3058 ASSERT_TRUE(data
!= nullptr);
3060 test::CompareCharArraysWithHexError("constructed packet",
3061 data
->data(), data
->length(),
3066 TEST_P(QuicFramerTest
, BuildStreamFramePacket
) {
3067 QuicPacketHeader header
;
3068 header
.public_header
.connection_id
= UINT64_C(0xFEDCBA9876543210);
3069 header
.public_header
.reset_flag
= false;
3070 header
.public_header
.version_flag
= false;
3071 header
.fec_flag
= false;
3072 header
.entropy_flag
= true;
3073 header
.packet_sequence_number
= UINT64_C(0x77123456789ABC);
3074 header
.fec_group
= 0;
3076 QuicStreamFrame
stream_frame(0x01020304, true, UINT64_C(0xBA98FEDC32107654),
3077 StringPiece("hello world!"));
3080 frames
.push_back(QuicFrame(&stream_frame
));
3082 unsigned char packet
[] = {
3083 // public flags (8 byte connection_id)
3086 0x10, 0x32, 0x54, 0x76,
3087 0x98, 0xBA, 0xDC, 0xFE,
3088 // packet sequence number
3089 0xBC, 0x9A, 0x78, 0x56,
3091 // private flags (entropy)
3094 // frame type (stream frame with fin and no length)
3097 0x04, 0x03, 0x02, 0x01,
3099 0x54, 0x76, 0x10, 0x32,
3100 0xDC, 0xFE, 0x98, 0xBA,
3107 scoped_ptr
<QuicPacket
> data(BuildDataPacket(header
, frames
));
3108 ASSERT_TRUE(data
!= nullptr);
3110 test::CompareCharArraysWithHexError("constructed packet",
3111 data
->data(), data
->length(),
3112 AsChars(packet
), arraysize(packet
));
3115 TEST_P(QuicFramerTest
, BuildStreamFramePacketInFecGroup
) {
3116 QuicPacketHeader header
;
3117 header
.public_header
.connection_id
= UINT64_C(0xFEDCBA9876543210);
3118 header
.public_header
.reset_flag
= false;
3119 header
.public_header
.version_flag
= false;
3120 header
.fec_flag
= false;
3121 header
.entropy_flag
= true;
3122 header
.packet_sequence_number
= UINT64_C(0x77123456789ABC);
3123 header
.is_in_fec_group
= IN_FEC_GROUP
;
3124 header
.fec_group
= UINT64_C(0x77123456789ABC);
3126 QuicStreamFrame
stream_frame(0x01020304, true, UINT64_C(0xBA98FEDC32107654),
3127 StringPiece("hello world!"));
3130 frames
.push_back(QuicFrame(&stream_frame
));
3131 unsigned char packet
[] = {
3132 // public flags (8 byte connection_id)
3135 0x10, 0x32, 0x54, 0x76, 0x98, 0xBA, 0xDC, 0xFE,
3136 // packet sequence number
3137 0xBC, 0x9A, 0x78, 0x56, 0x34, 0x12,
3138 // private flags (entropy, is_in_fec_group)
3142 // frame type (stream frame with fin and data length field)
3145 0x04, 0x03, 0x02, 0x01,
3147 0x54, 0x76, 0x10, 0x32, 0xDC, 0xFE, 0x98, 0xBA,
3148 // data length (since packet is in an FEC group)
3151 'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', '!',
3154 scoped_ptr
<QuicPacket
> data(BuildDataPacket(header
, frames
));
3155 ASSERT_TRUE(data
!= nullptr);
3157 test::CompareCharArraysWithHexError("constructed packet",
3158 data
->data(), data
->length(),
3159 AsChars(packet
), arraysize(packet
));
3162 TEST_P(QuicFramerTest
, BuildStreamFramePacketWithVersionFlag
) {
3163 QuicPacketHeader header
;
3164 header
.public_header
.connection_id
= UINT64_C(0xFEDCBA9876543210);
3165 header
.public_header
.reset_flag
= false;
3166 header
.public_header
.version_flag
= true;
3167 header
.fec_flag
= false;
3168 header
.entropy_flag
= true;
3169 header
.packet_sequence_number
= UINT64_C(0x77123456789ABC);
3170 header
.fec_group
= 0;
3172 QuicStreamFrame
stream_frame(0x01020304, true, UINT64_C(0xBA98FEDC32107654),
3173 StringPiece("hello world!"));
3176 frames
.push_back(QuicFrame(&stream_frame
));
3178 unsigned char packet
[] = {
3179 // public flags (version, 8 byte connection_id)
3193 GetQuicVersionDigitTens(),
3194 GetQuicVersionDigitOnes(),
3195 // packet sequence number
3202 // private flags (entropy)
3205 // frame type (stream frame with fin and no length)
3236 QuicFramerPeer::SetPerspective(&framer_
, Perspective::IS_CLIENT
);
3237 scoped_ptr
<QuicPacket
> data(BuildDataPacket(header
, frames
));
3238 ASSERT_TRUE(data
!= nullptr);
3240 test::CompareCharArraysWithHexError("constructed packet",
3241 data
->data(), data
->length(),
3242 AsChars(packet
), arraysize(packet
));
3245 TEST_P(QuicFramerTest
, BuildVersionNegotiationPacket
) {
3246 QuicPacketPublicHeader header
;
3247 header
.connection_id
= UINT64_C(0xFEDCBA9876543210);
3248 header
.reset_flag
= false;
3249 header
.version_flag
= true;
3251 unsigned char packet
[] = {
3252 // public flags (version, 8 byte connection_id)
3266 GetQuicVersionDigitTens(),
3267 GetQuicVersionDigitOnes(),
3270 QuicVersionVector versions
;
3271 versions
.push_back(GetParam());
3272 scoped_ptr
<QuicEncryptedPacket
> data(
3273 framer_
.BuildVersionNegotiationPacket(header
, versions
));
3275 test::CompareCharArraysWithHexError("constructed packet", data
->data(),
3276 data
->length(), AsChars(packet
),
3280 TEST_P(QuicFramerTest
, BuildAckFramePacket
) {
3281 QuicPacketHeader header
;
3282 header
.public_header
.connection_id
= UINT64_C(0xFEDCBA9876543210);
3283 header
.public_header
.reset_flag
= false;
3284 header
.public_header
.version_flag
= false;
3285 header
.fec_flag
= false;
3286 header
.entropy_flag
= true;
3287 header
.packet_sequence_number
= UINT64_C(0x770123456789AA8);
3288 header
.fec_group
= 0;
3290 QuicAckFrame ack_frame
;
3291 ack_frame
.entropy_hash
= 0x43;
3292 ack_frame
.largest_observed
= UINT64_C(0x770123456789ABF);
3293 ack_frame
.delta_time_largest_observed
= QuicTime::Delta::Zero();
3294 ack_frame
.missing_packets
.insert(UINT64_C(0x770123456789ABE));
3297 frames
.push_back(QuicFrame(&ack_frame
));
3299 unsigned char packet
[] = {
3300 // public flags (8 byte connection_id)
3303 0x10, 0x32, 0x54, 0x76, 0x98, 0xBA, 0xDC, 0xFE,
3304 // packet sequence number
3305 0xA8, 0x9A, 0x78, 0x56, 0x34, 0x12,
3306 // private flags (entropy)
3309 // frame type (ack frame)
3310 // (has nacks, not truncated, 6 byte largest observed, 1 byte delta)
3312 // entropy hash of all received packets.
3314 // largest observed packet sequence number
3315 0xBF, 0x9A, 0x78, 0x56, 0x34, 0x12,
3318 // num received packets.
3320 // num missing packet ranges
3322 // missing packet delta
3324 // 0 more missing packets in range.
3326 // 0 revived packets.
3330 scoped_ptr
<QuicPacket
> data(BuildDataPacket(header
, frames
));
3331 ASSERT_TRUE(data
!= nullptr);
3333 test::CompareCharArraysWithHexError("constructed packet",
3334 data
->data(), data
->length(),
3335 AsChars(packet
), arraysize(packet
));
3338 // TODO(jri): Add test for tuncated packets in which the original ack frame had
3339 // revived packets. (In both the large and small packet cases below).
3341 TEST_P(QuicFramerTest
, BuildTruncatedAckFrameLargePacket
) {
3342 QuicPacketHeader header
;
3343 header
.public_header
.connection_id
= UINT64_C(0xFEDCBA9876543210);
3344 header
.public_header
.reset_flag
= false;
3345 header
.public_header
.version_flag
= false;
3346 header
.fec_flag
= false;
3347 header
.entropy_flag
= true;
3348 header
.packet_sequence_number
= UINT64_C(0x770123456789AA8);
3349 header
.fec_group
= 0;
3351 QuicAckFrame ack_frame
;
3352 // This entropy hash is different from what shows up in the packet below,
3353 // since entropy is recomputed by the framer on ack truncation (by
3354 // TestEntropyCalculator for this test.)
3355 ack_frame
.entropy_hash
= 0x43;
3356 ack_frame
.largest_observed
= 2 * 300;
3357 ack_frame
.delta_time_largest_observed
= QuicTime::Delta::Zero();
3358 for (size_t i
= 1; i
< 2 * 300; i
+= 2) {
3359 ack_frame
.missing_packets
.insert(i
);
3363 frames
.push_back(QuicFrame(&ack_frame
));
3365 unsigned char packet
[] = {
3366 // public flags (8 byte connection_id)
3369 0x10, 0x32, 0x54, 0x76, 0x98, 0xBA, 0xDC, 0xFE,
3370 // packet sequence number
3371 0xA8, 0x9A, 0x78, 0x56, 0x34, 0x12,
3372 // private flags (entropy)
3375 // frame type (ack frame)
3376 // (has nacks, is truncated, 2 byte largest observed, 1 byte delta)
3378 // entropy hash of all received packets, set to 1 by TestEntropyCalculator
3379 // since ack is truncated.
3381 // 2-byte largest observed packet sequence number.
3382 // Expected to be 510 (0x1FE), since only 255 nack ranges can fit.
3386 // num missing packet ranges (limited to 255 by size of this field).
3388 // {missing packet delta, further missing packets in range}
3389 // 6 nack ranges x 42 + 3 nack ranges
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,
3397 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,
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,
3408 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,
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,
3419 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,
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,
3430 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,
3434 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3435 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3436 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3438 // 0 revived packets.
3442 scoped_ptr
<QuicPacket
> data(BuildDataPacket(header
, frames
));
3443 ASSERT_TRUE(data
!= nullptr);
3445 test::CompareCharArraysWithHexError("constructed packet",
3446 data
->data(), data
->length(),
3447 AsChars(packet
), arraysize(packet
));
3450 TEST_P(QuicFramerTest
, BuildTruncatedAckFrameSmallPacket
) {
3451 QuicPacketHeader header
;
3452 header
.public_header
.connection_id
= UINT64_C(0xFEDCBA9876543210);
3453 header
.public_header
.reset_flag
= false;
3454 header
.public_header
.version_flag
= false;
3455 header
.fec_flag
= false;
3456 header
.entropy_flag
= true;
3457 header
.packet_sequence_number
= UINT64_C(0x770123456789AA8);
3458 header
.fec_group
= 0;
3460 QuicAckFrame ack_frame
;
3461 // This entropy hash is different from what shows up in the packet below,
3462 // since entropy is recomputed by the framer on ack truncation (by
3463 // TestEntropyCalculator for this test.)
3464 ack_frame
.entropy_hash
= 0x43;
3465 ack_frame
.largest_observed
= 2 * 300;
3466 ack_frame
.delta_time_largest_observed
= QuicTime::Delta::Zero();
3467 for (size_t i
= 1; i
< 2 * 300; i
+= 2) {
3468 ack_frame
.missing_packets
.insert(i
);
3472 frames
.push_back(QuicFrame(&ack_frame
));
3474 unsigned char packet
[] = {
3475 // public flags (8 byte connection_id)
3478 0x10, 0x32, 0x54, 0x76, 0x98, 0xBA, 0xDC, 0xFE,
3479 // packet sequence number
3480 0xA8, 0x9A, 0x78, 0x56, 0x34, 0x12,
3481 // private flags (entropy)
3484 // frame type (ack frame)
3485 // (has nacks, is truncated, 2 byte largest observed, 1 byte delta)
3487 // entropy hash of all received packets, set to 1 by TestEntropyCalculator
3488 // since ack is truncated.
3490 // 2-byte largest observed packet sequence number.
3491 // Expected to be 12 (0x0C), since only 6 nack ranges can fit.
3495 // num missing packet ranges (limited to 6 by packet size of 37).
3497 // {missing packet delta, further missing packets in range}
3499 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3500 // 0 revived packets.
3504 scoped_ptr
<QuicPacket
> data(BuildDataPacket(header
, frames
, 37u));
3505 ASSERT_TRUE(data
!= nullptr);
3506 // Expect 1 byte unused since at least 2 bytes are needed to fit more nacks.
3507 EXPECT_EQ(36u, data
->length());
3508 test::CompareCharArraysWithHexError("constructed packet",
3509 data
->data(), data
->length(),
3510 AsChars(packet
), arraysize(packet
));
3513 TEST_P(QuicFramerTest
, BuildStopWaitingPacket
) {
3514 QuicPacketHeader header
;
3515 header
.public_header
.connection_id
= UINT64_C(0xFEDCBA9876543210);
3516 header
.public_header
.reset_flag
= false;
3517 header
.public_header
.version_flag
= false;
3518 header
.fec_flag
= false;
3519 header
.entropy_flag
= true;
3520 header
.packet_sequence_number
= UINT64_C(0x770123456789AA8);
3521 header
.fec_group
= 0;
3523 QuicStopWaitingFrame stop_waiting_frame
;
3524 stop_waiting_frame
.entropy_hash
= 0x14;
3525 stop_waiting_frame
.least_unacked
= UINT64_C(0x770123456789AA0);
3528 frames
.push_back(QuicFrame(&stop_waiting_frame
));
3530 unsigned char packet
[] = {
3531 // public flags (8 byte connection_id)
3534 0x10, 0x32, 0x54, 0x76,
3535 0x98, 0xBA, 0xDC, 0xFE,
3536 // packet sequence number
3537 0xA8, 0x9A, 0x78, 0x56,
3539 // private flags (entropy)
3542 // frame type (stop waiting frame)
3544 // entropy hash of sent packets till least awaiting - 1.
3546 // least packet sequence number awaiting an ack, delta from sequence number.
3547 0x08, 0x00, 0x00, 0x00,
3551 scoped_ptr
<QuicPacket
> data(BuildDataPacket(header
, frames
));
3552 ASSERT_TRUE(data
!= nullptr);
3554 test::CompareCharArraysWithHexError("constructed packet",
3555 data
->data(), data
->length(),
3556 AsChars(packet
), arraysize(packet
));
3559 TEST_P(QuicFramerTest
, BuildRstFramePacketQuicVersion24
) {
3560 if (version_
> QUIC_VERSION_24
) {
3561 // QUIC_VERSION_25 removes the error_details field from QuicRstStreamFrame.
3565 QuicPacketHeader header
;
3566 header
.public_header
.connection_id
= UINT64_C(0xFEDCBA9876543210);
3567 header
.public_header
.reset_flag
= false;
3568 header
.public_header
.version_flag
= false;
3569 header
.fec_flag
= false;
3570 header
.entropy_flag
= false;
3571 header
.packet_sequence_number
= UINT64_C(0x123456789ABC);
3572 header
.fec_group
= 0;
3574 QuicRstStreamFrame rst_frame
;
3575 rst_frame
.stream_id
= 0x01020304;
3576 rst_frame
.error_code
= static_cast<QuicRstStreamErrorCode
>(0x05060708);
3577 rst_frame
.error_details
= "because I can";
3578 rst_frame
.byte_offset
= 0x0807060504030201;
3580 unsigned char packet
[] = {
3581 // public flags (8 byte connection_id)
3584 0x10, 0x32, 0x54, 0x76,
3585 0x98, 0xBA, 0xDC, 0xFE,
3586 // packet sequence number
3587 0xBC, 0x9A, 0x78, 0x56,
3592 // frame type (rst stream frame)
3595 0x04, 0x03, 0x02, 0x01,
3597 0x01, 0x02, 0x03, 0x04,
3598 0x05, 0x06, 0x07, 0x08,
3600 0x08, 0x07, 0x06, 0x05,
3601 // error details length
3611 frames
.push_back(QuicFrame(&rst_frame
));
3613 scoped_ptr
<QuicPacket
> data(BuildDataPacket(header
, frames
));
3614 ASSERT_TRUE(data
!= nullptr);
3616 test::CompareCharArraysWithHexError("constructed packet", data
->data(),
3617 data
->length(), AsChars(packet
),
3621 TEST_P(QuicFramerTest
, BuildRstFramePacketQuic
) {
3622 if (version_
<= QUIC_VERSION_24
) {
3623 // QUIC_VERSION_25 removes the error_details field from QuicRstStreamFrame.
3627 QuicPacketHeader header
;
3628 header
.public_header
.connection_id
= UINT64_C(0xFEDCBA9876543210);
3629 header
.public_header
.reset_flag
= false;
3630 header
.public_header
.version_flag
= false;
3631 header
.fec_flag
= false;
3632 header
.entropy_flag
= false;
3633 header
.packet_sequence_number
= UINT64_C(0x123456789ABC);
3634 header
.fec_group
= 0;
3636 QuicRstStreamFrame rst_frame
;
3637 rst_frame
.stream_id
= 0x01020304;
3638 rst_frame
.error_code
= static_cast<QuicRstStreamErrorCode
>(0x05060708);
3639 rst_frame
.byte_offset
= 0x0807060504030201;
3642 unsigned char packet
[] = {
3643 // public flags (8 byte connection_id)
3646 0x10, 0x32, 0x54, 0x76,
3647 0x98, 0xBA, 0xDC, 0xFE,
3648 // packet sequence number
3649 0xBC, 0x9A, 0x78, 0x56,
3654 // frame type (rst stream frame)
3657 0x04, 0x03, 0x02, 0x01,
3659 0x01, 0x02, 0x03, 0x04,
3660 0x05, 0x06, 0x07, 0x08,
3662 0x08, 0x07, 0x06, 0x05,
3667 frames
.push_back(QuicFrame(&rst_frame
));
3669 scoped_ptr
<QuicPacket
> data(BuildDataPacket(header
, frames
));
3670 ASSERT_TRUE(data
!= nullptr);
3672 test::CompareCharArraysWithHexError("constructed packet", data
->data(),
3673 data
->length(), AsChars(packet
),
3677 TEST_P(QuicFramerTest
, BuildCloseFramePacket
) {
3678 QuicPacketHeader header
;
3679 header
.public_header
.connection_id
= UINT64_C(0xFEDCBA9876543210);
3680 header
.public_header
.reset_flag
= false;
3681 header
.public_header
.version_flag
= false;
3682 header
.fec_flag
= false;
3683 header
.entropy_flag
= true;
3684 header
.packet_sequence_number
= UINT64_C(0x123456789ABC);
3685 header
.fec_group
= 0;
3687 QuicConnectionCloseFrame close_frame
;
3688 close_frame
.error_code
= static_cast<QuicErrorCode
>(0x05060708);
3689 close_frame
.error_details
= "because I can";
3692 frames
.push_back(QuicFrame(&close_frame
));
3694 unsigned char packet
[] = {
3695 // public flags (8 byte connection_id)
3698 0x10, 0x32, 0x54, 0x76,
3699 0x98, 0xBA, 0xDC, 0xFE,
3700 // packet sequence number
3701 0xBC, 0x9A, 0x78, 0x56,
3703 // private flags (entropy)
3706 // frame type (connection close frame)
3709 0x08, 0x07, 0x06, 0x05,
3710 // error details length
3719 scoped_ptr
<QuicPacket
> data(BuildDataPacket(header
, frames
));
3720 ASSERT_TRUE(data
!= nullptr);
3722 test::CompareCharArraysWithHexError("constructed packet",
3723 data
->data(), data
->length(),
3724 AsChars(packet
), arraysize(packet
));
3727 TEST_P(QuicFramerTest
, BuildGoAwayPacket
) {
3728 QuicPacketHeader header
;
3729 header
.public_header
.connection_id
= UINT64_C(0xFEDCBA9876543210);
3730 header
.public_header
.reset_flag
= false;
3731 header
.public_header
.version_flag
= false;
3732 header
.fec_flag
= false;
3733 header
.entropy_flag
= true;
3734 header
.packet_sequence_number
= UINT64_C(0x123456789ABC);
3735 header
.fec_group
= 0;
3737 QuicGoAwayFrame goaway_frame
;
3738 goaway_frame
.error_code
= static_cast<QuicErrorCode
>(0x05060708);
3739 goaway_frame
.last_good_stream_id
= 0x01020304;
3740 goaway_frame
.reason_phrase
= "because I can";
3743 frames
.push_back(QuicFrame(&goaway_frame
));
3745 unsigned char packet
[] = {
3746 // public flags (8 byte connection_id)
3749 0x10, 0x32, 0x54, 0x76,
3750 0x98, 0xBA, 0xDC, 0xFE,
3751 // packet sequence number
3752 0xBC, 0x9A, 0x78, 0x56,
3754 // private flags(entropy)
3757 // frame type (go away frame)
3760 0x08, 0x07, 0x06, 0x05,
3762 0x04, 0x03, 0x02, 0x01,
3763 // error details length
3772 scoped_ptr
<QuicPacket
> data(BuildDataPacket(header
, frames
));
3773 ASSERT_TRUE(data
!= nullptr);
3775 test::CompareCharArraysWithHexError("constructed packet",
3776 data
->data(), data
->length(),
3777 AsChars(packet
), arraysize(packet
));
3780 TEST_P(QuicFramerTest
, BuildWindowUpdatePacket
) {
3781 QuicPacketHeader header
;
3782 header
.public_header
.connection_id
= UINT64_C(0xFEDCBA9876543210);
3783 header
.public_header
.reset_flag
= false;
3784 header
.public_header
.version_flag
= false;
3785 header
.fec_flag
= false;
3786 header
.entropy_flag
= true;
3787 header
.packet_sequence_number
= UINT64_C(0x123456789ABC);
3788 header
.fec_group
= 0;
3790 QuicWindowUpdateFrame window_update_frame
;
3791 window_update_frame
.stream_id
= 0x01020304;
3792 window_update_frame
.byte_offset
= 0x1122334455667788;
3795 frames
.push_back(QuicFrame(&window_update_frame
));
3797 unsigned char packet
[] = {
3798 // public flags (8 byte connection_id)
3801 0x10, 0x32, 0x54, 0x76,
3802 0x98, 0xBA, 0xDC, 0xFE,
3803 // packet sequence number
3804 0xBC, 0x9A, 0x78, 0x56,
3806 // private flags(entropy)
3809 // frame type (window update frame)
3812 0x04, 0x03, 0x02, 0x01,
3814 0x88, 0x77, 0x66, 0x55,
3815 0x44, 0x33, 0x22, 0x11,
3818 scoped_ptr
<QuicPacket
> data(BuildDataPacket(header
, frames
));
3819 ASSERT_TRUE(data
!= nullptr);
3821 test::CompareCharArraysWithHexError("constructed packet", data
->data(),
3822 data
->length(), AsChars(packet
),
3826 TEST_P(QuicFramerTest
, BuildBlockedPacket
) {
3827 QuicPacketHeader header
;
3828 header
.public_header
.connection_id
= UINT64_C(0xFEDCBA9876543210);
3829 header
.public_header
.reset_flag
= false;
3830 header
.public_header
.version_flag
= false;
3831 header
.fec_flag
= false;
3832 header
.entropy_flag
= true;
3833 header
.packet_sequence_number
= UINT64_C(0x123456789ABC);
3834 header
.fec_group
= 0;
3836 QuicBlockedFrame blocked_frame
;
3837 blocked_frame
.stream_id
= 0x01020304;
3840 frames
.push_back(QuicFrame(&blocked_frame
));
3842 unsigned char packet
[] = {
3843 // public flags (8 byte connection_id)
3846 0x10, 0x32, 0x54, 0x76,
3847 0x98, 0xBA, 0xDC, 0xFE,
3848 // packet sequence number
3849 0xBC, 0x9A, 0x78, 0x56,
3851 // private flags(entropy)
3854 // frame type (blocked frame)
3857 0x04, 0x03, 0x02, 0x01,
3860 scoped_ptr
<QuicPacket
> data(BuildDataPacket(header
, frames
));
3861 ASSERT_TRUE(data
!= nullptr);
3863 test::CompareCharArraysWithHexError("constructed packet", data
->data(),
3864 data
->length(), AsChars(packet
),
3868 TEST_P(QuicFramerTest
, BuildPingPacket
) {
3869 QuicPacketHeader header
;
3870 header
.public_header
.connection_id
= UINT64_C(0xFEDCBA9876543210);
3871 header
.public_header
.reset_flag
= false;
3872 header
.public_header
.version_flag
= false;
3873 header
.fec_flag
= false;
3874 header
.entropy_flag
= true;
3875 header
.packet_sequence_number
= UINT64_C(0x123456789ABC);
3876 header
.fec_group
= 0;
3878 QuicPingFrame ping_frame
;
3881 frames
.push_back(QuicFrame(&ping_frame
));
3883 unsigned char packet
[] = {
3884 // public flags (8 byte connection_id)
3887 0x10, 0x32, 0x54, 0x76,
3888 0x98, 0xBA, 0xDC, 0xFE,
3889 // packet sequence number
3890 0xBC, 0x9A, 0x78, 0x56,
3892 // private flags(entropy)
3895 // frame type (ping frame)
3899 scoped_ptr
<QuicPacket
> data(BuildDataPacket(header
, frames
));
3900 ASSERT_TRUE(data
!= nullptr);
3902 test::CompareCharArraysWithHexError("constructed packet", data
->data(),
3903 data
->length(), AsChars(packet
),
3907 // Test that the MTU discovery packet is serialized correctly as a PING packet.
3908 TEST_P(QuicFramerTest
, BuildMtuDiscoveryPacket
) {
3909 QuicPacketHeader header
;
3910 header
.public_header
.connection_id
= UINT64_C(0xFEDCBA9876543210);
3911 header
.public_header
.reset_flag
= false;
3912 header
.public_header
.version_flag
= false;
3913 header
.fec_flag
= false;
3914 header
.entropy_flag
= true;
3915 header
.packet_sequence_number
= UINT64_C(0x123456789ABC);
3916 header
.fec_group
= 0;
3918 QuicMtuDiscoveryFrame mtu_discovery_frame
;
3921 frames
.push_back(QuicFrame(&mtu_discovery_frame
));
3924 unsigned char packet
[] = {
3925 // public flags (8 byte connection_id)
3928 0x10, 0x32, 0x54, 0x76,
3929 0x98, 0xBA, 0xDC, 0xFE,
3930 // packet sequence number
3931 0xBC, 0x9A, 0x78, 0x56,
3933 // private flags(entropy)
3936 // frame type (ping frame)
3941 scoped_ptr
<QuicPacket
> data(BuildDataPacket(header
, frames
));
3942 ASSERT_TRUE(data
!= nullptr);
3944 test::CompareCharArraysWithHexError("constructed packet", data
->data(),
3945 data
->length(), AsChars(packet
),
3949 TEST_P(QuicFramerTest
, BuildPublicResetPacket
) {
3950 QuicPublicResetPacket reset_packet
;
3951 reset_packet
.public_header
.connection_id
= UINT64_C(0xFEDCBA9876543210);
3952 reset_packet
.public_header
.reset_flag
= true;
3953 reset_packet
.public_header
.version_flag
= false;
3954 reset_packet
.rejected_sequence_number
= UINT64_C(0x123456789ABC);
3955 reset_packet
.nonce_proof
= UINT64_C(0xABCDEF0123456789);
3957 unsigned char packet
[] = {
3958 // public flags (public reset, 8 byte ConnectionId)
3961 0x10, 0x32, 0x54, 0x76,
3962 0x98, 0xBA, 0xDC, 0xFE,
3963 // message tag (kPRST)
3965 // num_entries (2) + padding
3966 0x02, 0x00, 0x00, 0x00,
3970 0x08, 0x00, 0x00, 0x00,
3974 0x10, 0x00, 0x00, 0x00,
3976 0x89, 0x67, 0x45, 0x23,
3977 0x01, 0xEF, 0xCD, 0xAB,
3978 // rejected sequence number
3979 0xBC, 0x9A, 0x78, 0x56,
3980 0x34, 0x12, 0x00, 0x00,
3983 scoped_ptr
<QuicEncryptedPacket
> data(
3984 framer_
.BuildPublicResetPacket(reset_packet
));
3985 ASSERT_TRUE(data
!= nullptr);
3987 test::CompareCharArraysWithHexError("constructed packet",
3988 data
->data(), data
->length(),
3989 AsChars(packet
), arraysize(packet
));
3992 TEST_P(QuicFramerTest
, BuildPublicResetPacketWithClientAddress
) {
3993 QuicPublicResetPacket reset_packet
;
3994 reset_packet
.public_header
.connection_id
= UINT64_C(0xFEDCBA9876543210);
3995 reset_packet
.public_header
.reset_flag
= true;
3996 reset_packet
.public_header
.version_flag
= false;
3997 reset_packet
.rejected_sequence_number
= UINT64_C(0x123456789ABC);
3998 reset_packet
.nonce_proof
= UINT64_C(0xABCDEF0123456789);
3999 reset_packet
.client_address
= IPEndPoint(Loopback4(), 0x1234);
4001 unsigned char packet
[] = {
4002 // public flags (public reset, 8 byte ConnectionId)
4005 0x10, 0x32, 0x54, 0x76,
4006 0x98, 0xBA, 0xDC, 0xFE,
4007 // message tag (kPRST)
4009 // num_entries (3) + padding
4010 0x03, 0x00, 0x00, 0x00,
4014 0x08, 0x00, 0x00, 0x00,
4018 0x10, 0x00, 0x00, 0x00,
4022 0x18, 0x00, 0x00, 0x00,
4024 0x89, 0x67, 0x45, 0x23,
4025 0x01, 0xEF, 0xCD, 0xAB,
4026 // rejected sequence number
4027 0xBC, 0x9A, 0x78, 0x56,
4028 0x34, 0x12, 0x00, 0x00,
4031 0x7F, 0x00, 0x00, 0x01,
4035 scoped_ptr
<QuicEncryptedPacket
> data(
4036 framer_
.BuildPublicResetPacket(reset_packet
));
4037 ASSERT_TRUE(data
!= nullptr);
4039 test::CompareCharArraysWithHexError("constructed packet",
4040 data
->data(), data
->length(),
4041 AsChars(packet
), arraysize(packet
));
4044 TEST_P(QuicFramerTest
, BuildFecPacket
) {
4045 QuicPacketHeader header
;
4046 header
.public_header
.connection_id
= UINT64_C(0xFEDCBA9876543210);
4047 header
.public_header
.reset_flag
= false;
4048 header
.public_header
.version_flag
= false;
4049 header
.fec_flag
= true;
4050 header
.entropy_flag
= true;
4051 header
.packet_sequence_number
= (UINT64_C(0x123456789ABC));
4052 header
.is_in_fec_group
= IN_FEC_GROUP
;
4053 header
.fec_group
= UINT64_C(0x123456789ABB);
4055 QuicFecData fec_data
;
4056 fec_data
.fec_group
= 1;
4057 fec_data
.redundancy
= "abcdefghijklmnop";
4059 unsigned char packet
[] = {
4060 // public flags (8 byte connection_id)
4063 0x10, 0x32, 0x54, 0x76,
4064 0x98, 0xBA, 0xDC, 0xFE,
4065 // packet sequence number
4066 0xBC, 0x9A, 0x78, 0x56,
4068 // private flags (entropy & fec group & fec packet)
4070 // first fec protected packet offset
4080 scoped_ptr
<QuicPacket
> data(framer_
.BuildFecPacket(header
, fec_data
));
4081 ASSERT_TRUE(data
!= nullptr);
4083 test::CompareCharArraysWithHexError("constructed packet",
4084 data
->data(), data
->length(),
4085 AsChars(packet
), arraysize(packet
));
4088 TEST_P(QuicFramerTest
, EncryptPacket
) {
4089 QuicPacketSequenceNumber sequence_number
= UINT64_C(0x123456789ABC);
4090 unsigned char packet
[] = {
4091 // public flags (8 byte connection_id)
4094 0x10, 0x32, 0x54, 0x76,
4095 0x98, 0xBA, 0xDC, 0xFE,
4096 // packet sequence number
4097 0xBC, 0x9A, 0x78, 0x56,
4099 // private flags (fec group & fec packet)
4101 // first fec protected packet offset
4111 scoped_ptr
<QuicPacket
> raw(new QuicPacket(
4112 AsChars(packet
), arraysize(packet
), false, PACKET_8BYTE_CONNECTION_ID
,
4113 !kIncludeVersion
, PACKET_6BYTE_SEQUENCE_NUMBER
));
4114 char buffer
[kMaxPacketSize
];
4115 scoped_ptr
<QuicEncryptedPacket
> encrypted(framer_
.EncryptPayload(
4116 ENCRYPTION_NONE
, sequence_number
, *raw
, buffer
, kMaxPacketSize
));
4118 ASSERT_TRUE(encrypted
.get() != nullptr);
4119 EXPECT_TRUE(CheckEncryption(sequence_number
, raw
.get()));
4122 TEST_P(QuicFramerTest
, EncryptPacketWithVersionFlag
) {
4123 QuicPacketSequenceNumber sequence_number
= UINT64_C(0x123456789ABC);
4124 unsigned char packet
[] = {
4125 // public flags (version, 8 byte connection_id)
4128 0x10, 0x32, 0x54, 0x76,
4129 0x98, 0xBA, 0xDC, 0xFE,
4132 // packet sequence number
4133 0xBC, 0x9A, 0x78, 0x56,
4135 // private flags (fec group & fec flags)
4137 // first fec protected packet offset
4147 scoped_ptr
<QuicPacket
> raw(new QuicPacket(
4148 AsChars(packet
), arraysize(packet
), false, PACKET_8BYTE_CONNECTION_ID
,
4149 kIncludeVersion
, PACKET_6BYTE_SEQUENCE_NUMBER
));
4150 char buffer
[kMaxPacketSize
];
4151 scoped_ptr
<QuicEncryptedPacket
> encrypted(framer_
.EncryptPayload(
4152 ENCRYPTION_NONE
, sequence_number
, *raw
, buffer
, kMaxPacketSize
));
4154 ASSERT_TRUE(encrypted
.get() != nullptr);
4155 EXPECT_TRUE(CheckEncryption(sequence_number
, raw
.get()));
4158 TEST_P(QuicFramerTest
, AckTruncationLargePacket
) {
4159 QuicPacketHeader header
;
4160 header
.public_header
.connection_id
= UINT64_C(0xFEDCBA9876543210);
4161 header
.public_header
.reset_flag
= false;
4162 header
.public_header
.version_flag
= false;
4163 header
.fec_flag
= false;
4164 header
.entropy_flag
= false;
4165 header
.packet_sequence_number
= UINT64_C(0x123456789ABC);
4166 header
.fec_group
= 0;
4168 // Create a packet with just the ack.
4169 QuicAckFrame ack_frame
= MakeAckFrameWithNackRanges(300, 0u);
4171 frame
.type
= ACK_FRAME
;
4172 frame
.ack_frame
= &ack_frame
;
4174 frames
.push_back(frame
);
4176 // Build an ack packet with truncation due to limit in number of nack ranges.
4177 scoped_ptr
<QuicPacket
> raw_ack_packet(BuildDataPacket(header
, frames
));
4178 ASSERT_TRUE(raw_ack_packet
!= nullptr);
4179 char buffer
[kMaxPacketSize
];
4180 scoped_ptr
<QuicEncryptedPacket
> ack_packet(
4181 framer_
.EncryptPayload(ENCRYPTION_NONE
, header
.packet_sequence_number
,
4182 *raw_ack_packet
, buffer
, kMaxPacketSize
));
4183 // Now make sure we can turn our ack packet back into an ack frame.
4184 ASSERT_TRUE(framer_
.ProcessPacket(*ack_packet
));
4185 ASSERT_EQ(1u, visitor_
.ack_frames_
.size());
4186 QuicAckFrame
& processed_ack_frame
= *visitor_
.ack_frames_
[0];
4187 EXPECT_TRUE(processed_ack_frame
.is_truncated
);
4188 EXPECT_EQ(510u, processed_ack_frame
.largest_observed
);
4189 ASSERT_EQ(255u, processed_ack_frame
.missing_packets
.size());
4190 SequenceNumberSet::const_iterator missing_iter
=
4191 processed_ack_frame
.missing_packets
.begin();
4192 EXPECT_EQ(1u, *missing_iter
);
4193 SequenceNumberSet::const_reverse_iterator last_missing_iter
=
4194 processed_ack_frame
.missing_packets
.rbegin();
4195 EXPECT_EQ(509u, *last_missing_iter
);
4198 TEST_P(QuicFramerTest
, AckTruncationSmallPacket
) {
4199 QuicPacketHeader header
;
4200 header
.public_header
.connection_id
= UINT64_C(0xFEDCBA9876543210);
4201 header
.public_header
.reset_flag
= false;
4202 header
.public_header
.version_flag
= false;
4203 header
.fec_flag
= false;
4204 header
.entropy_flag
= false;
4205 header
.packet_sequence_number
= UINT64_C(0x123456789ABC);
4206 header
.fec_group
= 0;
4208 // Create a packet with just the ack.
4209 QuicAckFrame ack_frame
= MakeAckFrameWithNackRanges(300, 0u);
4211 frame
.type
= ACK_FRAME
;
4212 frame
.ack_frame
= &ack_frame
;
4214 frames
.push_back(frame
);
4216 // Build an ack packet with truncation due to limit in number of nack ranges.
4217 scoped_ptr
<QuicPacket
> raw_ack_packet(BuildDataPacket(header
, frames
, 500));
4218 ASSERT_TRUE(raw_ack_packet
!= nullptr);
4219 char buffer
[kMaxPacketSize
];
4220 scoped_ptr
<QuicEncryptedPacket
> ack_packet(
4221 framer_
.EncryptPayload(ENCRYPTION_NONE
, header
.packet_sequence_number
,
4222 *raw_ack_packet
, buffer
, kMaxPacketSize
));
4223 // Now make sure we can turn our ack packet back into an ack frame.
4224 ASSERT_TRUE(framer_
.ProcessPacket(*ack_packet
));
4225 ASSERT_EQ(1u, visitor_
.ack_frames_
.size());
4226 QuicAckFrame
& processed_ack_frame
= *visitor_
.ack_frames_
[0];
4227 EXPECT_TRUE(processed_ack_frame
.is_truncated
);
4228 EXPECT_EQ(476u, processed_ack_frame
.largest_observed
);
4229 ASSERT_EQ(238u, processed_ack_frame
.missing_packets
.size());
4230 SequenceNumberSet::const_iterator missing_iter
=
4231 processed_ack_frame
.missing_packets
.begin();
4232 EXPECT_EQ(1u, *missing_iter
);
4233 SequenceNumberSet::const_reverse_iterator last_missing_iter
=
4234 processed_ack_frame
.missing_packets
.rbegin();
4235 EXPECT_EQ(475u, *last_missing_iter
);
4238 TEST_P(QuicFramerTest
, CleanTruncation
) {
4239 QuicPacketHeader header
;
4240 header
.public_header
.connection_id
= UINT64_C(0xFEDCBA9876543210);
4241 header
.public_header
.reset_flag
= false;
4242 header
.public_header
.version_flag
= false;
4243 header
.fec_flag
= false;
4244 header
.entropy_flag
= true;
4245 header
.packet_sequence_number
= UINT64_C(0x123456789ABC);
4246 header
.fec_group
= 0;
4248 QuicAckFrame ack_frame
;
4249 ack_frame
.largest_observed
= 201;
4250 for (uint64 i
= 1; i
< ack_frame
.largest_observed
; ++i
) {
4251 ack_frame
.missing_packets
.insert(i
);
4254 // Create a packet with just the ack.
4256 frame
.type
= ACK_FRAME
;
4257 frame
.ack_frame
= &ack_frame
;
4259 frames
.push_back(frame
);
4261 scoped_ptr
<QuicPacket
> raw_ack_packet(BuildDataPacket(header
, frames
));
4262 ASSERT_TRUE(raw_ack_packet
!= nullptr);
4264 char buffer
[kMaxPacketSize
];
4265 scoped_ptr
<QuicEncryptedPacket
> ack_packet(
4266 framer_
.EncryptPayload(ENCRYPTION_NONE
, header
.packet_sequence_number
,
4267 *raw_ack_packet
, buffer
, kMaxPacketSize
));
4269 // Now make sure we can turn our ack packet back into an ack frame.
4270 ASSERT_TRUE(framer_
.ProcessPacket(*ack_packet
));
4272 // Test for clean truncation of the ack by comparing the length of the
4273 // original packets to the re-serialized packets.
4275 frame
.type
= ACK_FRAME
;
4276 frame
.ack_frame
= visitor_
.ack_frames_
[0];
4277 frames
.push_back(frame
);
4279 size_t original_raw_length
= raw_ack_packet
->length();
4280 raw_ack_packet
.reset(BuildDataPacket(header
, frames
));
4281 ASSERT_TRUE(raw_ack_packet
!= nullptr);
4282 EXPECT_EQ(original_raw_length
, raw_ack_packet
->length());
4283 ASSERT_TRUE(raw_ack_packet
!= nullptr);
4286 TEST_P(QuicFramerTest
, EntropyFlagTest
) {
4287 unsigned char packet
[] = {
4288 // public flags (8 byte connection_id)
4291 0x10, 0x32, 0x54, 0x76,
4292 0x98, 0xBA, 0xDC, 0xFE,
4293 // packet sequence number
4294 0xBC, 0x9A, 0x78, 0x56,
4296 // private flags (Entropy)
4299 // frame type (stream frame with fin and no length)
4302 0x04, 0x03, 0x02, 0x01,
4304 0x54, 0x76, 0x10, 0x32,
4305 0xDC, 0xFE, 0x98, 0xBA,
4312 QuicEncryptedPacket
encrypted(AsChars(packet
), arraysize(packet
), false);
4313 EXPECT_TRUE(framer_
.ProcessPacket(encrypted
));
4314 EXPECT_EQ(QUIC_NO_ERROR
, framer_
.error());
4315 ASSERT_TRUE(visitor_
.header_
.get());
4316 EXPECT_TRUE(visitor_
.header_
->entropy_flag
);
4317 EXPECT_EQ(1 << 4, visitor_
.header_
->entropy_hash
);
4318 EXPECT_FALSE(visitor_
.header_
->fec_flag
);
4321 TEST_P(QuicFramerTest
, FecEntropyTest
) {
4322 unsigned char packet
[] = {
4323 // public flags (8 byte connection_id)
4326 0x10, 0x32, 0x54, 0x76,
4327 0x98, 0xBA, 0xDC, 0xFE,
4328 // packet sequence number
4329 0xBC, 0x9A, 0x78, 0x56,
4331 // private flags (Entropy & fec group & FEC)
4333 // first fec protected packet offset
4336 // frame type (stream frame with fin and no length)
4339 0x04, 0x03, 0x02, 0x01,
4341 0x54, 0x76, 0x10, 0x32,
4342 0xDC, 0xFE, 0x98, 0xBA,
4349 QuicEncryptedPacket
encrypted(AsChars(packet
), arraysize(packet
), false);
4350 EXPECT_TRUE(framer_
.ProcessPacket(encrypted
));
4351 EXPECT_EQ(QUIC_NO_ERROR
, framer_
.error());
4352 ASSERT_TRUE(visitor_
.header_
.get());
4353 EXPECT_TRUE(visitor_
.header_
->fec_flag
);
4354 EXPECT_TRUE(visitor_
.header_
->entropy_flag
);
4355 EXPECT_EQ(1 << 4, visitor_
.header_
->entropy_hash
);
4358 TEST_P(QuicFramerTest
, StopPacketProcessing
) {
4359 unsigned char packet
[] = {
4360 // public flags (8 byte connection_id)
4363 0x10, 0x32, 0x54, 0x76,
4364 0x98, 0xBA, 0xDC, 0xFE,
4365 // packet sequence number
4366 0xBC, 0x9A, 0x78, 0x56,
4371 // frame type (stream frame with fin)
4374 0x04, 0x03, 0x02, 0x01,
4376 0x54, 0x76, 0x10, 0x32,
4377 0xDC, 0xFE, 0x98, 0xBA,
4385 // frame type (ack frame)
4387 // entropy hash of sent packets till least awaiting - 1.
4389 // least packet sequence number awaiting an ack
4390 0xA0, 0x9A, 0x78, 0x56,
4392 // entropy hash of all received packets.
4394 // largest observed packet sequence number
4395 0xBF, 0x9A, 0x78, 0x56,
4397 // num missing packets
4400 0xBE, 0x9A, 0x78, 0x56,
4404 MockFramerVisitor visitor
;
4405 framer_
.set_visitor(&visitor
);
4406 EXPECT_CALL(visitor
, OnPacket());
4407 EXPECT_CALL(visitor
, OnPacketHeader(_
));
4408 EXPECT_CALL(visitor
, OnStreamFrame(_
)).WillOnce(Return(false));
4409 EXPECT_CALL(visitor
, OnAckFrame(_
)).Times(0);
4410 EXPECT_CALL(visitor
, OnPacketComplete());
4411 EXPECT_CALL(visitor
, OnUnauthenticatedPublicHeader(_
)).WillOnce(Return(true));
4412 EXPECT_CALL(visitor
, OnUnauthenticatedHeader(_
)).WillOnce(Return(true));
4413 EXPECT_CALL(visitor
, OnDecryptedPacket(_
));
4415 QuicEncryptedPacket
encrypted(AsChars(packet
), arraysize(packet
), false);
4416 EXPECT_TRUE(framer_
.ProcessPacket(encrypted
));
4417 EXPECT_EQ(QUIC_NO_ERROR
, framer_
.error());
4420 static char kTestString
[] = "At least 20 characters.";
4421 static QuicStreamId kTestQuicStreamId
= 1;
4422 static bool ExpectedStreamFrame(const QuicStreamFrame
& frame
) {
4423 return frame
.stream_id
== kTestQuicStreamId
&& !frame
.fin
&&
4424 frame
.offset
== 0 && frame
.data
== kTestString
;
4425 // FIN is hard-coded false in ConstructEncryptedPacket.
4426 // Offset 0 is hard-coded in ConstructEncryptedPacket.
4429 // Verify that the packet returned by ConstructEncryptedPacket() can be properly
4430 // parsed by the framer.
4431 TEST_P(QuicFramerTest
, ConstructEncryptedPacket
) {
4432 // Since we are using ConstructEncryptedPacket, we have to set the framer's
4433 // crypto to be Null.
4434 framer_
.SetDecrypter(ENCRYPTION_NONE
, QuicDecrypter::Create(kNULL
));
4435 framer_
.SetEncrypter(ENCRYPTION_NONE
, QuicEncrypter::Create(kNULL
));
4437 scoped_ptr
<QuicEncryptedPacket
> packet(ConstructEncryptedPacket(
4438 42, false, false, kTestQuicStreamId
, kTestString
,
4439 PACKET_8BYTE_CONNECTION_ID
, PACKET_6BYTE_SEQUENCE_NUMBER
));
4441 MockFramerVisitor visitor
;
4442 framer_
.set_visitor(&visitor
);
4443 EXPECT_CALL(visitor
, OnPacket()).Times(1);
4444 EXPECT_CALL(visitor
, OnUnauthenticatedPublicHeader(_
))
4446 .WillOnce(Return(true));
4447 EXPECT_CALL(visitor
, OnUnauthenticatedHeader(_
))
4449 .WillOnce(Return(true));
4450 EXPECT_CALL(visitor
, OnPacketHeader(_
)).Times(1).WillOnce(Return(true));
4451 EXPECT_CALL(visitor
, OnDecryptedPacket(_
)).Times(1);
4452 EXPECT_CALL(visitor
, OnError(_
)).Times(0);
4453 EXPECT_CALL(visitor
, OnStreamFrame(_
)).Times(0);
4454 EXPECT_CALL(visitor
, OnStreamFrame(Truly(ExpectedStreamFrame
))).Times(1);
4455 EXPECT_CALL(visitor
, OnAckFrame(_
)).Times(0);
4456 EXPECT_CALL(visitor
, OnPacketComplete()).Times(1);
4458 EXPECT_TRUE(framer_
.ProcessPacket(*packet
));
4459 EXPECT_EQ(QUIC_NO_ERROR
, framer_
.error());
4462 // Verify that the packet returned by ConstructMisFramedEncryptedPacket()
4463 // does cause the framer to return an error.
4464 TEST_P(QuicFramerTest
, ConstructMisFramedEncryptedPacket
) {
4465 // Since we are using ConstructEncryptedPacket, we have to set the framer's
4466 // crypto to be Null.
4467 framer_
.SetDecrypter(ENCRYPTION_NONE
, QuicDecrypter::Create(kNULL
));
4468 framer_
.SetEncrypter(ENCRYPTION_NONE
, QuicEncrypter::Create(kNULL
));
4470 scoped_ptr
<QuicEncryptedPacket
> packet(ConstructMisFramedEncryptedPacket(
4471 42, false, false, kTestQuicStreamId
, kTestString
,
4472 PACKET_8BYTE_CONNECTION_ID
, PACKET_6BYTE_SEQUENCE_NUMBER
, nullptr));
4474 MockFramerVisitor visitor
;
4475 framer_
.set_visitor(&visitor
);
4476 EXPECT_CALL(visitor
, OnPacket()).Times(1);
4477 EXPECT_CALL(visitor
, OnUnauthenticatedPublicHeader(_
))
4479 .WillOnce(Return(true));
4480 EXPECT_CALL(visitor
, OnUnauthenticatedHeader(_
))
4482 .WillOnce(Return(true));
4483 EXPECT_CALL(visitor
, OnPacketHeader(_
)).Times(0);
4484 EXPECT_CALL(visitor
, OnDecryptedPacket(_
)).Times(1);
4485 EXPECT_CALL(visitor
, OnError(_
)).Times(1);
4486 EXPECT_CALL(visitor
, OnStreamFrame(_
)).Times(0);
4487 EXPECT_CALL(visitor
, OnAckFrame(_
)).Times(0);
4488 EXPECT_CALL(visitor
, OnPacketComplete()).Times(0);
4490 EXPECT_FALSE(framer_
.ProcessPacket(*packet
));
4491 EXPECT_EQ(QUIC_INVALID_PACKET_HEADER
, framer_
.error());