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 QuicPacketNumber kEpoch
= UINT64_C(1) << 48;
41 const QuicPacketNumber kMask
= kEpoch
- 1;
43 // Index into the connection_id offset in the header.
44 const size_t kConnectionIdOffset
= kPublicFlagsSize
;
45 // Index into the version string in the header. (if present).
46 const size_t kVersionOffset
= kConnectionIdOffset
+ PACKET_8BYTE_CONNECTION_ID
;
48 // Size in bytes of the stream frame fields for an arbitrary StreamID and
49 // offset and the last frame in a packet.
50 size_t GetMinStreamFrameSize() {
51 return kQuicFrameTypeSize
+ kQuicMaxStreamIdSize
+ kQuicMaxStreamOffsetSize
;
54 // Index into the packet number offset in the header.
55 size_t GetPacketNumberOffset(QuicConnectionIdLength connection_id_length
,
56 bool include_version
) {
57 return kConnectionIdOffset
+ connection_id_length
+
58 (include_version
? kQuicVersionSize
: 0);
61 size_t GetPacketNumberOffset(bool include_version
) {
62 return GetPacketNumberOffset(PACKET_8BYTE_CONNECTION_ID
, include_version
);
65 // Index into the private flags offset in the data packet header.
66 size_t GetPrivateFlagsOffset(QuicConnectionIdLength connection_id_length
,
67 bool include_version
) {
68 return GetPacketNumberOffset(connection_id_length
, include_version
) +
69 PACKET_6BYTE_PACKET_NUMBER
;
72 size_t GetPrivateFlagsOffset(bool include_version
) {
73 return GetPrivateFlagsOffset(PACKET_8BYTE_CONNECTION_ID
, include_version
);
76 size_t GetPrivateFlagsOffset(bool include_version
,
77 QuicPacketNumberLength packet_number_length
) {
78 return GetPacketNumberOffset(PACKET_8BYTE_CONNECTION_ID
, include_version
) +
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 QuicPacketNumberLength packet_number_length
) {
96 return GetPrivateFlagsOffset(include_version
, packet_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(QuicPacketNumber packet_number
,
111 StringPiece associated_data
,
112 StringPiece plaintext
,
114 size_t* output_length
,
115 size_t max_output_length
) override
{
116 packet_number_
= packet_number
;
117 associated_data_
= associated_data
.as_string();
118 plaintext_
= plaintext
.as_string();
119 memcpy(output
, plaintext
.data(), plaintext
.length());
120 *output_length
= plaintext
.length();
123 size_t GetKeySize() const override
{ return 0; }
124 size_t GetNoncePrefixSize() const override
{ return 0; }
125 size_t GetMaxPlaintextSize(size_t ciphertext_size
) const override
{
126 return ciphertext_size
;
128 size_t GetCiphertextSize(size_t plaintext_size
) const override
{
129 return plaintext_size
;
131 StringPiece
GetKey() const override
{ return StringPiece(); }
132 StringPiece
GetNoncePrefix() const override
{ return StringPiece(); }
133 QuicPacketNumber packet_number_
;
134 string associated_data_
;
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(QuicPacketNumber packet_number
,
144 const StringPiece
& associated_data
,
145 const StringPiece
& ciphertext
,
147 size_t* output_length
,
148 size_t max_output_length
) override
{
149 packet_number_
= packet_number
;
150 associated_data_
= associated_data
.as_string();
151 ciphertext_
= ciphertext
.as_string();
152 memcpy(output
, ciphertext
.data(), ciphertext
.length());
153 *output_length
= ciphertext
.length();
156 StringPiece
GetKey() const override
{ return StringPiece(); }
157 StringPiece
GetNoncePrefix() const override
{ return StringPiece(); }
158 const char* cipher_name() const override
{ return "Test"; }
159 // Use a distinct value starting with 0xFFFFFF, which is never used by TLS.
160 uint32
cipher_id() const override
{ return 0xFFFFFFF2; }
161 QuicPacketNumber packet_number_
;
162 string associated_data_
;
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(QuicPacketNumber packet_number
, QuicPacket
* packet
) {
364 if (packet_number
!= encrypter_
->packet_number_
) {
365 LOG(ERROR
) << "Encrypted incorrect packet number. expected "
366 << packet_number
<< " actual: " << encrypter_
->packet_number_
;
369 if (packet
->AssociatedData() != encrypter_
->associated_data_
) {
370 LOG(ERROR
) << "Encrypted incorrect associated data. expected "
371 << packet
->AssociatedData() << " actual: "
372 << encrypter_
->associated_data_
;
375 if (packet
->Plaintext() != encrypter_
->plaintext_
) {
376 LOG(ERROR
) << "Encrypted incorrect plaintext data. expected "
377 << packet
->Plaintext() << " actual: "
378 << encrypter_
->plaintext_
;
384 bool CheckDecryption(const QuicEncryptedPacket
& encrypted
,
385 bool includes_version
) {
386 if (visitor_
.header_
->packet_packet_number
!= decrypter_
->packet_number_
) {
387 LOG(ERROR
) << "Decrypted incorrect packet number. expected "
388 << visitor_
.header_
->packet_packet_number
389 << " actual: " << decrypter_
->packet_number_
;
392 if (QuicFramer::GetAssociatedDataFromEncryptedPacket(
393 encrypted
, PACKET_8BYTE_CONNECTION_ID
, includes_version
,
394 PACKET_6BYTE_PACKET_NUMBER
) != decrypter_
->associated_data_
) {
395 LOG(ERROR
) << "Decrypted incorrect associated data. expected "
396 << QuicFramer::GetAssociatedDataFromEncryptedPacket(
397 encrypted
, PACKET_8BYTE_CONNECTION_ID
, includes_version
,
398 PACKET_6BYTE_PACKET_NUMBER
)
399 << " actual: " << decrypter_
->associated_data_
;
402 StringPiece
ciphertext(encrypted
.AsStringPiece().substr(
403 GetStartOfEncryptedData(PACKET_8BYTE_CONNECTION_ID
, includes_version
,
404 PACKET_6BYTE_PACKET_NUMBER
)));
405 if (ciphertext
!= decrypter_
->ciphertext_
) {
406 LOG(ERROR
) << "Decrypted incorrect ciphertext data. expected "
407 << ciphertext
<< " actual: "
408 << decrypter_
->ciphertext_
;
414 char* AsChars(unsigned char* data
) {
415 return reinterpret_cast<char*>(data
);
418 void CheckProcessingFails(unsigned char* packet
,
420 string expected_error
,
421 QuicErrorCode error_code
) {
422 QuicEncryptedPacket
encrypted(AsChars(packet
), len
, false);
423 EXPECT_FALSE(framer_
.ProcessPacket(encrypted
)) << "len: " << len
;
424 EXPECT_EQ(expected_error
, framer_
.detailed_error()) << "len: " << len
;
425 EXPECT_EQ(error_code
, framer_
.error()) << "len: " << len
;
428 // Checks if the supplied string matches data in the supplied StreamFrame.
429 void CheckStreamFrameData(string str
, QuicStreamFrame
* frame
) {
430 EXPECT_EQ(str
, frame
->data
);
433 void CheckStreamFrameBoundaries(unsigned char* packet
,
434 size_t stream_id_size
,
435 bool include_version
) {
436 // Now test framing boundaries.
437 for (size_t i
= kQuicFrameTypeSize
; i
< GetMinStreamFrameSize(); ++i
) {
438 string expected_error
;
439 if (i
< kQuicFrameTypeSize
+ stream_id_size
) {
440 expected_error
= "Unable to read stream_id.";
441 } else if (i
< kQuicFrameTypeSize
+ stream_id_size
+
442 kQuicMaxStreamOffsetSize
) {
443 expected_error
= "Unable to read offset.";
445 expected_error
= "Unable to read frame data.";
447 CheckProcessingFails(
449 i
+ GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID
, include_version
,
450 PACKET_6BYTE_PACKET_NUMBER
, NOT_IN_FEC_GROUP
),
451 expected_error
, QUIC_INVALID_STREAM_DATA
);
455 void CheckCalculatePacketNumber(QuicPacketNumber expected_packet_number
,
456 QuicPacketNumber last_packet_number
) {
457 QuicPacketNumber wire_packet_number
= expected_packet_number
& kMask
;
458 QuicFramerPeer::SetLastPacketNumber(&framer_
, last_packet_number
);
459 EXPECT_EQ(expected_packet_number
,
460 QuicFramerPeer::CalculatePacketNumberFromWire(
461 &framer_
, PACKET_6BYTE_PACKET_NUMBER
, wire_packet_number
))
462 << "last_packet_number: " << last_packet_number
463 << " wire_packet_number: " << wire_packet_number
;
466 QuicPacket
* BuildDataPacket(const QuicPacketHeader
& header
,
467 const QuicFrames
& frames
) {
468 return BuildUnsizedDataPacket(&framer_
, header
, frames
);
471 QuicPacket
* BuildDataPacket(const QuicPacketHeader
& header
,
472 const QuicFrames
& frames
,
473 size_t packet_size
) {
474 return BuildUnsizedDataPacket(&framer_
, header
, frames
, packet_size
);
477 test::TestEncrypter
* encrypter_
;
478 test::TestDecrypter
* decrypter_
;
479 QuicVersion version_
;
482 test::TestQuicVisitor visitor_
;
483 test::TestEntropyCalculator entropy_calculator_
;
486 // Run all framer tests with all supported versions of QUIC.
487 INSTANTIATE_TEST_CASE_P(QuicFramerTests
,
489 ::testing::ValuesIn(kSupportedQuicVersions
));
491 TEST_P(QuicFramerTest
, CalculatePacketNumberFromWireNearEpochStart
) {
492 // A few quick manual sanity checks.
493 CheckCalculatePacketNumber(UINT64_C(1), UINT64_C(0));
494 CheckCalculatePacketNumber(kEpoch
+ 1, kMask
);
495 CheckCalculatePacketNumber(kEpoch
, kMask
);
497 // Cases where the last number was close to the start of the range.
498 for (uint64 last
= 0; last
< 10; last
++) {
499 // Small numbers should not wrap (even if they're out of order).
500 for (uint64 j
= 0; j
< 10; j
++) {
501 CheckCalculatePacketNumber(j
, last
);
504 // Large numbers should not wrap either (because we're near 0 already).
505 for (uint64 j
= 0; j
< 10; j
++) {
506 CheckCalculatePacketNumber(kEpoch
- 1 - j
, last
);
511 TEST_P(QuicFramerTest
, CalculatePacketNumberFromWireNearEpochEnd
) {
512 // Cases where the last number was close to the end of the range
513 for (uint64 i
= 0; i
< 10; i
++) {
514 QuicPacketNumber last
= kEpoch
- i
;
516 // Small numbers should wrap.
517 for (uint64 j
= 0; j
< 10; j
++) {
518 CheckCalculatePacketNumber(kEpoch
+ j
, last
);
521 // Large numbers should not (even if they're out of order).
522 for (uint64 j
= 0; j
< 10; j
++) {
523 CheckCalculatePacketNumber(kEpoch
- 1 - j
, last
);
528 // Next check where we're in a non-zero epoch to verify we handle
529 // reverse wrapping, too.
530 TEST_P(QuicFramerTest
, CalculatePacketNumberFromWireNearPrevEpoch
) {
531 const uint64 prev_epoch
= 1 * kEpoch
;
532 const uint64 cur_epoch
= 2 * kEpoch
;
533 // Cases where the last number was close to the start of the range
534 for (uint64 i
= 0; i
< 10; i
++) {
535 uint64 last
= cur_epoch
+ i
;
536 // Small number should not wrap (even if they're out of order).
537 for (uint64 j
= 0; j
< 10; j
++) {
538 CheckCalculatePacketNumber(cur_epoch
+ j
, last
);
541 // But large numbers should reverse wrap.
542 for (uint64 j
= 0; j
< 10; j
++) {
543 uint64 num
= kEpoch
- 1 - j
;
544 CheckCalculatePacketNumber(prev_epoch
+ num
, last
);
549 TEST_P(QuicFramerTest
, CalculatePacketNumberFromWireNearNextEpoch
) {
550 const uint64 cur_epoch
= 2 * kEpoch
;
551 const uint64 next_epoch
= 3 * kEpoch
;
552 // Cases where the last number was close to the end of the range
553 for (uint64 i
= 0; i
< 10; i
++) {
554 QuicPacketNumber last
= next_epoch
- 1 - i
;
556 // Small numbers should wrap.
557 for (uint64 j
= 0; j
< 10; j
++) {
558 CheckCalculatePacketNumber(next_epoch
+ j
, last
);
561 // but large numbers should not (even if they're out of order).
562 for (uint64 j
= 0; j
< 10; j
++) {
563 uint64 num
= kEpoch
- 1 - j
;
564 CheckCalculatePacketNumber(cur_epoch
+ num
, last
);
569 TEST_P(QuicFramerTest
, CalculatePacketNumberFromWireNearNextMax
) {
570 const uint64 max_number
= numeric_limits
<uint64
>::max();
571 const uint64 max_epoch
= max_number
& ~kMask
;
573 // Cases where the last number was close to the end of the range
574 for (uint64 i
= 0; i
< 10; i
++) {
575 // Subtract 1, because the expected next packet number is 1 more than the
576 // last packet number.
577 QuicPacketNumber last
= max_number
- i
- 1;
579 // Small numbers should not wrap, because they have nowhere to go.
580 for (uint64 j
= 0; j
< 10; j
++) {
581 CheckCalculatePacketNumber(max_epoch
+ j
, last
);
584 // Large numbers should not wrap either.
585 for (uint64 j
= 0; j
< 10; j
++) {
586 uint64 num
= kEpoch
- 1 - j
;
587 CheckCalculatePacketNumber(max_epoch
+ num
, last
);
592 TEST_P(QuicFramerTest
, EmptyPacket
) {
593 char packet
[] = { 0x00 };
594 QuicEncryptedPacket
encrypted(packet
, 0, false);
595 EXPECT_FALSE(framer_
.ProcessPacket(encrypted
));
596 EXPECT_EQ(QUIC_INVALID_PACKET_HEADER
, framer_
.error());
599 TEST_P(QuicFramerTest
, LargePacket
) {
601 unsigned char packet
[kMaxPacketSize
+ 1] = {
602 // public flags (8 byte connection_id)
605 0x10, 0x32, 0x54, 0x76, 0x98, 0xBA, 0xDC, 0xFE,
607 0xBC, 0x9A, 0x78, 0x56, 0x34, 0x12,
612 memset(packet
+ GetPacketHeaderSize(
613 PACKET_8BYTE_CONNECTION_ID
, !kIncludeVersion
,
614 PACKET_6BYTE_PACKET_NUMBER
, NOT_IN_FEC_GROUP
), 0,
615 kMaxPacketSize
- GetPacketHeaderSize(
616 PACKET_8BYTE_CONNECTION_ID
, !kIncludeVersion
,
617 PACKET_6BYTE_PACKET_NUMBER
, NOT_IN_FEC_GROUP
) + 1);
620 QuicEncryptedPacket
encrypted(AsChars(packet
), arraysize(packet
), false);
621 EXPECT_FALSE(framer_
.ProcessPacket(encrypted
));
623 ASSERT_TRUE(visitor_
.header_
.get());
624 // Make sure we've parsed the packet header, so we can send an error.
625 EXPECT_EQ(UINT64_C(0xFEDCBA9876543210),
626 visitor_
.header_
->public_header
.connection_id
);
627 // Make sure the correct error is propagated.
628 EXPECT_EQ(QUIC_PACKET_TOO_LARGE
, framer_
.error());
631 TEST_P(QuicFramerTest
, PacketHeader
) {
633 unsigned char packet
[] = {
634 // public flags (8 byte connection_id)
637 0x10, 0x32, 0x54, 0x76, 0x98, 0xBA, 0xDC, 0xFE,
639 0xBC, 0x9A, 0x78, 0x56, 0x34, 0x12,
645 QuicEncryptedPacket
encrypted(AsChars(packet
), arraysize(packet
), false);
646 EXPECT_FALSE(framer_
.ProcessPacket(encrypted
));
647 EXPECT_EQ(QUIC_MISSING_PAYLOAD
, framer_
.error());
648 ASSERT_TRUE(visitor_
.header_
.get());
649 EXPECT_EQ(UINT64_C(0xFEDCBA9876543210),
650 visitor_
.header_
->public_header
.connection_id
);
651 EXPECT_FALSE(visitor_
.header_
->public_header
.reset_flag
);
652 EXPECT_FALSE(visitor_
.header_
->public_header
.version_flag
);
653 EXPECT_FALSE(visitor_
.header_
->fec_flag
);
654 EXPECT_FALSE(visitor_
.header_
->entropy_flag
);
655 EXPECT_EQ(0, visitor_
.header_
->entropy_hash
);
656 EXPECT_EQ(UINT64_C(0x123456789ABC), visitor_
.header_
->packet_packet_number
);
657 EXPECT_EQ(NOT_IN_FEC_GROUP
, visitor_
.header_
->is_in_fec_group
);
658 EXPECT_EQ(0x00u
, visitor_
.header_
->fec_group
);
660 // Now test framing boundaries.
662 i
< GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID
, !kIncludeVersion
,
663 PACKET_6BYTE_PACKET_NUMBER
, NOT_IN_FEC_GROUP
);
665 string expected_error
;
666 if (i
< kConnectionIdOffset
) {
667 expected_error
= "Unable to read public flags.";
668 } else if (i
< GetPacketNumberOffset(!kIncludeVersion
)) {
669 expected_error
= "Unable to read ConnectionId.";
670 } else if (i
< GetPrivateFlagsOffset(!kIncludeVersion
)) {
671 expected_error
= "Unable to read packet number.";
672 } else if (i
< GetFecGroupOffset(!kIncludeVersion
)) {
673 expected_error
= "Unable to read private flags.";
675 expected_error
= "Unable to read first fec protected packet offset.";
677 CheckProcessingFails(packet
, i
, expected_error
, QUIC_INVALID_PACKET_HEADER
);
681 TEST_P(QuicFramerTest
, PacketHeaderWith4ByteConnectionId
) {
682 QuicFramerPeer::SetLastSerializedConnectionId(
683 &framer_
, UINT64_C(0xFEDCBA9876543210));
686 unsigned char packet
[] = {
687 // public flags (4 byte connection_id)
690 0x10, 0x32, 0x54, 0x76,
692 0xBC, 0x9A, 0x78, 0x56, 0x34, 0x12,
698 QuicEncryptedPacket
encrypted(AsChars(packet
), arraysize(packet
), false);
699 EXPECT_FALSE(framer_
.ProcessPacket(encrypted
));
700 EXPECT_EQ(QUIC_MISSING_PAYLOAD
, framer_
.error());
701 ASSERT_TRUE(visitor_
.header_
.get());
702 EXPECT_EQ(UINT64_C(0xFEDCBA9876543210),
703 visitor_
.header_
->public_header
.connection_id
);
704 EXPECT_FALSE(visitor_
.header_
->public_header
.reset_flag
);
705 EXPECT_FALSE(visitor_
.header_
->public_header
.version_flag
);
706 EXPECT_FALSE(visitor_
.header_
->fec_flag
);
707 EXPECT_FALSE(visitor_
.header_
->entropy_flag
);
708 EXPECT_EQ(0, visitor_
.header_
->entropy_hash
);
709 EXPECT_EQ(UINT64_C(0x123456789ABC), visitor_
.header_
->packet_packet_number
);
710 EXPECT_EQ(NOT_IN_FEC_GROUP
, visitor_
.header_
->is_in_fec_group
);
711 EXPECT_EQ(0x00u
, visitor_
.header_
->fec_group
);
713 // Now test framing boundaries.
715 i
< GetPacketHeaderSize(PACKET_4BYTE_CONNECTION_ID
, !kIncludeVersion
,
716 PACKET_6BYTE_PACKET_NUMBER
, NOT_IN_FEC_GROUP
);
718 string expected_error
;
719 if (i
< kConnectionIdOffset
) {
720 expected_error
= "Unable to read public flags.";
721 } else if (i
< GetPacketNumberOffset(PACKET_4BYTE_CONNECTION_ID
,
723 expected_error
= "Unable to read ConnectionId.";
724 } else if (i
< GetPrivateFlagsOffset(PACKET_4BYTE_CONNECTION_ID
,
726 expected_error
= "Unable to read packet number.";
727 } else if (i
< GetFecGroupOffset(PACKET_4BYTE_CONNECTION_ID
,
729 expected_error
= "Unable to read private flags.";
731 expected_error
= "Unable to read first fec protected packet offset.";
733 CheckProcessingFails(packet
, i
, expected_error
, QUIC_INVALID_PACKET_HEADER
);
737 TEST_P(QuicFramerTest
, PacketHeader1ByteConnectionId
) {
738 QuicFramerPeer::SetLastSerializedConnectionId(
739 &framer_
, UINT64_C(0xFEDCBA9876543210));
742 unsigned char packet
[] = {
743 // public flags (1 byte connection_id)
748 0xBC, 0x9A, 0x78, 0x56, 0x34, 0x12,
754 QuicEncryptedPacket
encrypted(AsChars(packet
), arraysize(packet
), false);
755 EXPECT_FALSE(framer_
.ProcessPacket(encrypted
));
756 EXPECT_EQ(QUIC_MISSING_PAYLOAD
, framer_
.error());
757 ASSERT_TRUE(visitor_
.header_
.get());
758 EXPECT_EQ(UINT64_C(0xFEDCBA9876543210),
759 visitor_
.header_
->public_header
.connection_id
);
760 EXPECT_FALSE(visitor_
.header_
->public_header
.reset_flag
);
761 EXPECT_FALSE(visitor_
.header_
->public_header
.version_flag
);
762 EXPECT_FALSE(visitor_
.header_
->fec_flag
);
763 EXPECT_FALSE(visitor_
.header_
->entropy_flag
);
764 EXPECT_EQ(0, visitor_
.header_
->entropy_hash
);
765 EXPECT_EQ(UINT64_C(0x123456789ABC), visitor_
.header_
->packet_packet_number
);
766 EXPECT_EQ(NOT_IN_FEC_GROUP
, visitor_
.header_
->is_in_fec_group
);
767 EXPECT_EQ(0x00u
, visitor_
.header_
->fec_group
);
769 // Now test framing boundaries.
771 i
< GetPacketHeaderSize(PACKET_1BYTE_CONNECTION_ID
, !kIncludeVersion
,
772 PACKET_6BYTE_PACKET_NUMBER
, NOT_IN_FEC_GROUP
);
774 string expected_error
;
775 if (i
< kConnectionIdOffset
) {
776 expected_error
= "Unable to read public flags.";
777 } else if (i
< GetPacketNumberOffset(PACKET_1BYTE_CONNECTION_ID
,
779 expected_error
= "Unable to read ConnectionId.";
780 } else if (i
< GetPrivateFlagsOffset(PACKET_1BYTE_CONNECTION_ID
,
782 expected_error
= "Unable to read packet number.";
783 } else if (i
< GetFecGroupOffset(PACKET_1BYTE_CONNECTION_ID
,
785 expected_error
= "Unable to read private flags.";
787 expected_error
= "Unable to read first fec protected packet offset.";
789 CheckProcessingFails(packet
, i
, expected_error
, QUIC_INVALID_PACKET_HEADER
);
793 TEST_P(QuicFramerTest
, PacketHeaderWith0ByteConnectionId
) {
794 QuicFramerPeer::SetLastSerializedConnectionId(
795 &framer_
, UINT64_C(0xFEDCBA9876543210));
798 unsigned char packet
[] = {
799 // public flags (0 byte connection_id)
803 0xBC, 0x9A, 0x78, 0x56,
810 QuicEncryptedPacket
encrypted(AsChars(packet
), arraysize(packet
), false);
811 EXPECT_FALSE(framer_
.ProcessPacket(encrypted
));
812 EXPECT_EQ(QUIC_MISSING_PAYLOAD
, framer_
.error());
813 ASSERT_TRUE(visitor_
.header_
.get());
814 EXPECT_EQ(UINT64_C(0xFEDCBA9876543210),
815 visitor_
.header_
->public_header
.connection_id
);
816 EXPECT_FALSE(visitor_
.header_
->public_header
.reset_flag
);
817 EXPECT_FALSE(visitor_
.header_
->public_header
.version_flag
);
818 EXPECT_FALSE(visitor_
.header_
->fec_flag
);
819 EXPECT_FALSE(visitor_
.header_
->entropy_flag
);
820 EXPECT_EQ(0, visitor_
.header_
->entropy_hash
);
821 EXPECT_EQ(UINT64_C(0x123456789ABC), visitor_
.header_
->packet_packet_number
);
822 EXPECT_EQ(NOT_IN_FEC_GROUP
, visitor_
.header_
->is_in_fec_group
);
823 EXPECT_EQ(0x00u
, visitor_
.header_
->fec_group
);
825 // Now test framing boundaries.
827 i
< GetPacketHeaderSize(PACKET_0BYTE_CONNECTION_ID
, !kIncludeVersion
,
828 PACKET_6BYTE_PACKET_NUMBER
, NOT_IN_FEC_GROUP
);
830 string expected_error
;
831 if (i
< kConnectionIdOffset
) {
832 expected_error
= "Unable to read public flags.";
833 } else if (i
< GetPacketNumberOffset(PACKET_0BYTE_CONNECTION_ID
,
835 expected_error
= "Unable to read ConnectionId.";
836 } else if (i
< GetPrivateFlagsOffset(PACKET_0BYTE_CONNECTION_ID
,
838 expected_error
= "Unable to read packet number.";
839 } else if (i
< GetFecGroupOffset(PACKET_0BYTE_CONNECTION_ID
,
841 expected_error
= "Unable to read private flags.";
843 expected_error
= "Unable to read first fec protected packet offset.";
845 CheckProcessingFails(packet
, i
, expected_error
, QUIC_INVALID_PACKET_HEADER
);
849 TEST_P(QuicFramerTest
, PacketHeaderWithVersionFlag
) {
851 unsigned char packet
[] = {
852 // public flags (version)
855 0x10, 0x32, 0x54, 0x76, 0x98, 0xBA, 0xDC, 0xFE,
857 'Q', '0', GetQuicVersionDigitTens(), GetQuicVersionDigitOnes(),
859 0xBC, 0x9A, 0x78, 0x56, 0x34, 0x12,
865 QuicEncryptedPacket
encrypted(AsChars(packet
), arraysize(packet
), false);
866 EXPECT_FALSE(framer_
.ProcessPacket(encrypted
));
867 EXPECT_EQ(QUIC_MISSING_PAYLOAD
, framer_
.error());
868 ASSERT_TRUE(visitor_
.header_
.get());
869 EXPECT_EQ(UINT64_C(0xFEDCBA9876543210),
870 visitor_
.header_
->public_header
.connection_id
);
871 EXPECT_FALSE(visitor_
.header_
->public_header
.reset_flag
);
872 EXPECT_TRUE(visitor_
.header_
->public_header
.version_flag
);
873 EXPECT_EQ(GetParam(), visitor_
.header_
->public_header
.versions
[0]);
874 EXPECT_FALSE(visitor_
.header_
->fec_flag
);
875 EXPECT_FALSE(visitor_
.header_
->entropy_flag
);
876 EXPECT_EQ(0, visitor_
.header_
->entropy_hash
);
877 EXPECT_EQ(UINT64_C(0x123456789ABC), visitor_
.header_
->packet_packet_number
);
878 EXPECT_EQ(NOT_IN_FEC_GROUP
, visitor_
.header_
->is_in_fec_group
);
879 EXPECT_EQ(0x00u
, visitor_
.header_
->fec_group
);
881 // Now test framing boundaries.
883 i
< GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID
, kIncludeVersion
,
884 PACKET_6BYTE_PACKET_NUMBER
, NOT_IN_FEC_GROUP
);
886 string expected_error
;
887 if (i
< kConnectionIdOffset
) {
888 expected_error
= "Unable to read public flags.";
889 } else if (i
< kVersionOffset
) {
890 expected_error
= "Unable to read ConnectionId.";
891 } else if (i
< GetPacketNumberOffset(kIncludeVersion
)) {
892 expected_error
= "Unable to read protocol version.";
893 } else if (i
< GetPrivateFlagsOffset(kIncludeVersion
)) {
894 expected_error
= "Unable to read packet number.";
895 } else if (i
< GetFecGroupOffset(kIncludeVersion
)) {
896 expected_error
= "Unable to read private flags.";
898 expected_error
= "Unable to read first fec protected packet offset.";
900 CheckProcessingFails(packet
, i
, expected_error
, QUIC_INVALID_PACKET_HEADER
);
904 TEST_P(QuicFramerTest
, PacketHeaderWith4BytePacketNumber
) {
905 QuicFramerPeer::SetLastPacketNumber(&framer_
, UINT64_C(0x123456789ABA));
908 unsigned char packet
[] = {
909 // public flags (8 byte connection_id and 4 byte packet number)
912 0x10, 0x32, 0x54, 0x76, 0x98, 0xBA, 0xDC, 0xFE,
914 0xBC, 0x9A, 0x78, 0x56,
920 QuicEncryptedPacket
encrypted(AsChars(packet
), arraysize(packet
), false);
921 EXPECT_FALSE(framer_
.ProcessPacket(encrypted
));
922 EXPECT_EQ(QUIC_MISSING_PAYLOAD
, framer_
.error());
923 ASSERT_TRUE(visitor_
.header_
.get());
924 EXPECT_EQ(UINT64_C(0xFEDCBA9876543210),
925 visitor_
.header_
->public_header
.connection_id
);
926 EXPECT_FALSE(visitor_
.header_
->public_header
.reset_flag
);
927 EXPECT_FALSE(visitor_
.header_
->public_header
.version_flag
);
928 EXPECT_FALSE(visitor_
.header_
->fec_flag
);
929 EXPECT_FALSE(visitor_
.header_
->entropy_flag
);
930 EXPECT_EQ(0, visitor_
.header_
->entropy_hash
);
931 EXPECT_EQ(UINT64_C(0x123456789ABC), visitor_
.header_
->packet_packet_number
);
932 EXPECT_EQ(NOT_IN_FEC_GROUP
, visitor_
.header_
->is_in_fec_group
);
933 EXPECT_EQ(0x00u
, visitor_
.header_
->fec_group
);
935 // Now test framing boundaries.
937 i
< GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID
, !kIncludeVersion
,
938 PACKET_4BYTE_PACKET_NUMBER
, NOT_IN_FEC_GROUP
);
940 string expected_error
;
941 if (i
< kConnectionIdOffset
) {
942 expected_error
= "Unable to read public flags.";
943 } else if (i
< GetPacketNumberOffset(!kIncludeVersion
)) {
944 expected_error
= "Unable to read ConnectionId.";
945 } else if (i
< GetPrivateFlagsOffset(!kIncludeVersion
,
946 PACKET_4BYTE_PACKET_NUMBER
)) {
947 expected_error
= "Unable to read packet number.";
948 } else if (i
< GetFecGroupOffset(!kIncludeVersion
,
949 PACKET_4BYTE_PACKET_NUMBER
)) {
950 expected_error
= "Unable to read private flags.";
952 expected_error
= "Unable to read first fec protected packet offset.";
954 CheckProcessingFails(packet
, i
, expected_error
, QUIC_INVALID_PACKET_HEADER
);
958 TEST_P(QuicFramerTest
, PacketHeaderWith2BytePacketNumber
) {
959 QuicFramerPeer::SetLastPacketNumber(&framer_
, UINT64_C(0x123456789ABA));
962 unsigned char packet
[] = {
963 // public flags (8 byte connection_id and 2 byte packet number)
966 0x10, 0x32, 0x54, 0x76, 0x98, 0xBA, 0xDC, 0xFE,
974 QuicEncryptedPacket
encrypted(AsChars(packet
), arraysize(packet
), false);
975 EXPECT_FALSE(framer_
.ProcessPacket(encrypted
));
976 EXPECT_EQ(QUIC_MISSING_PAYLOAD
, framer_
.error());
977 ASSERT_TRUE(visitor_
.header_
.get());
978 EXPECT_EQ(UINT64_C(0xFEDCBA9876543210),
979 visitor_
.header_
->public_header
.connection_id
);
980 EXPECT_FALSE(visitor_
.header_
->public_header
.reset_flag
);
981 EXPECT_FALSE(visitor_
.header_
->public_header
.version_flag
);
982 EXPECT_FALSE(visitor_
.header_
->fec_flag
);
983 EXPECT_FALSE(visitor_
.header_
->entropy_flag
);
984 EXPECT_EQ(0, visitor_
.header_
->entropy_hash
);
985 EXPECT_EQ(UINT64_C(0x123456789ABC), visitor_
.header_
->packet_packet_number
);
986 EXPECT_EQ(NOT_IN_FEC_GROUP
, visitor_
.header_
->is_in_fec_group
);
987 EXPECT_EQ(0x00u
, visitor_
.header_
->fec_group
);
989 // Now test framing boundaries.
991 i
< GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID
, !kIncludeVersion
,
992 PACKET_2BYTE_PACKET_NUMBER
, NOT_IN_FEC_GROUP
);
994 string expected_error
;
995 if (i
< kConnectionIdOffset
) {
996 expected_error
= "Unable to read public flags.";
997 } else if (i
< GetPacketNumberOffset(!kIncludeVersion
)) {
998 expected_error
= "Unable to read ConnectionId.";
999 } else if (i
< GetPrivateFlagsOffset(!kIncludeVersion
,
1000 PACKET_2BYTE_PACKET_NUMBER
)) {
1001 expected_error
= "Unable to read packet number.";
1002 } else if (i
< GetFecGroupOffset(!kIncludeVersion
,
1003 PACKET_2BYTE_PACKET_NUMBER
)) {
1004 expected_error
= "Unable to read private flags.";
1006 expected_error
= "Unable to read first fec protected packet offset.";
1008 CheckProcessingFails(packet
, i
, expected_error
, QUIC_INVALID_PACKET_HEADER
);
1012 TEST_P(QuicFramerTest
, PacketHeaderWith1BytePacketNumber
) {
1013 QuicFramerPeer::SetLastPacketNumber(&framer_
, UINT64_C(0x123456789ABA));
1016 unsigned char packet
[] = {
1017 // public flags (8 byte connection_id and 1 byte packet number)
1020 0x10, 0x32, 0x54, 0x76, 0x98, 0xBA, 0xDC, 0xFE,
1028 QuicEncryptedPacket
encrypted(AsChars(packet
), arraysize(packet
), false);
1029 EXPECT_FALSE(framer_
.ProcessPacket(encrypted
));
1030 EXPECT_EQ(QUIC_MISSING_PAYLOAD
, framer_
.error());
1031 ASSERT_TRUE(visitor_
.header_
.get());
1032 EXPECT_EQ(UINT64_C(0xFEDCBA9876543210),
1033 visitor_
.header_
->public_header
.connection_id
);
1034 EXPECT_FALSE(visitor_
.header_
->public_header
.reset_flag
);
1035 EXPECT_FALSE(visitor_
.header_
->public_header
.version_flag
);
1036 EXPECT_FALSE(visitor_
.header_
->fec_flag
);
1037 EXPECT_FALSE(visitor_
.header_
->entropy_flag
);
1038 EXPECT_EQ(0, visitor_
.header_
->entropy_hash
);
1039 EXPECT_EQ(UINT64_C(0x123456789ABC), visitor_
.header_
->packet_packet_number
);
1040 EXPECT_EQ(NOT_IN_FEC_GROUP
, visitor_
.header_
->is_in_fec_group
);
1041 EXPECT_EQ(0x00u
, visitor_
.header_
->fec_group
);
1043 // Now test framing boundaries.
1045 i
< GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID
, !kIncludeVersion
,
1046 PACKET_1BYTE_PACKET_NUMBER
, NOT_IN_FEC_GROUP
);
1048 string expected_error
;
1049 if (i
< kConnectionIdOffset
) {
1050 expected_error
= "Unable to read public flags.";
1051 } else if (i
< GetPacketNumberOffset(!kIncludeVersion
)) {
1052 expected_error
= "Unable to read ConnectionId.";
1053 } else if (i
< GetPrivateFlagsOffset(!kIncludeVersion
,
1054 PACKET_1BYTE_PACKET_NUMBER
)) {
1055 expected_error
= "Unable to read packet number.";
1056 } else if (i
< GetFecGroupOffset(!kIncludeVersion
,
1057 PACKET_1BYTE_PACKET_NUMBER
)) {
1058 expected_error
= "Unable to read private flags.";
1060 expected_error
= "Unable to read first fec protected packet offset.";
1062 CheckProcessingFails(packet
, i
, expected_error
, QUIC_INVALID_PACKET_HEADER
);
1066 TEST_P(QuicFramerTest
, InvalidPublicFlag
) {
1068 unsigned char packet
[] = {
1069 // public flags: all flags set but the public reset flag and version flag.
1072 0x10, 0x32, 0x54, 0x76, 0x98, 0xBA, 0xDC, 0xFE,
1074 0xBC, 0x9A, 0x78, 0x56, 0x34, 0x12,
1078 // frame type (padding)
1080 0x00, 0x00, 0x00, 0x00
1084 CheckProcessingFails(packet
,
1086 "Illegal public flags value.",
1087 QUIC_INVALID_PACKET_HEADER
);
1089 // Now turn off validation.
1090 framer_
.set_validate_flags(false);
1091 QuicEncryptedPacket
encrypted(AsChars(packet
), arraysize(packet
), false);
1092 EXPECT_TRUE(framer_
.ProcessPacket(encrypted
));
1095 TEST_P(QuicFramerTest
, InvalidPublicFlagWithMatchingVersions
) {
1097 unsigned char packet
[] = {
1098 // public flags (8 byte connection_id and version flag and an unknown flag)
1101 0x10, 0x32, 0x54, 0x76,
1102 0x98, 0xBA, 0xDC, 0xFE,
1104 'Q', '0', GetQuicVersionDigitTens(), GetQuicVersionDigitOnes(),
1106 0xBC, 0x9A, 0x78, 0x56,
1111 // frame type (padding)
1113 0x00, 0x00, 0x00, 0x00
1116 CheckProcessingFails(packet
,
1118 "Illegal public flags value.",
1119 QUIC_INVALID_PACKET_HEADER
);
1122 TEST_P(QuicFramerTest
, LargePublicFlagWithMismatchedVersions
) {
1124 unsigned char packet
[] = {
1125 // public flags (8 byte connection_id, version flag and an unknown flag)
1128 0x10, 0x32, 0x54, 0x76,
1129 0x98, 0xBA, 0xDC, 0xFE,
1133 0xBC, 0x9A, 0x78, 0x56,
1138 // frame type (padding frame)
1140 0x00, 0x00, 0x00, 0x00
1143 QuicEncryptedPacket
encrypted(AsChars(packet
), arraysize(packet
), false);
1144 EXPECT_TRUE(framer_
.ProcessPacket(encrypted
));
1145 EXPECT_EQ(QUIC_NO_ERROR
, framer_
.error());
1146 ASSERT_TRUE(visitor_
.header_
.get());
1147 EXPECT_EQ(0, visitor_
.frame_count_
);
1148 EXPECT_EQ(1, visitor_
.version_mismatch_
);
1151 TEST_P(QuicFramerTest
, InvalidPrivateFlag
) {
1153 unsigned char packet
[] = {
1154 // public flags (8 byte connection_id)
1157 0x10, 0x32, 0x54, 0x76,
1158 0x98, 0xBA, 0xDC, 0xFE,
1160 0xBC, 0x9A, 0x78, 0x56,
1165 // frame type (padding)
1167 0x00, 0x00, 0x00, 0x00
1170 CheckProcessingFails(packet
,
1172 "Illegal private flags value.",
1173 QUIC_INVALID_PACKET_HEADER
);
1176 TEST_P(QuicFramerTest
, InvalidFECGroupOffset
) {
1178 unsigned char packet
[] = {
1179 // public flags (8 byte connection_id)
1182 0x10, 0x32, 0x54, 0x76,
1183 0x98, 0xBA, 0xDC, 0xFE,
1185 0x01, 0x00, 0x00, 0x00,
1187 // private flags (fec group)
1189 // first fec protected packet offset
1193 CheckProcessingFails(packet
, arraysize(packet
),
1194 "First fec protected packet offset must be less "
1195 "than the packet number.",
1196 QUIC_INVALID_PACKET_HEADER
);
1199 TEST_P(QuicFramerTest
, PaddingFrame
) {
1201 unsigned char packet
[] = {
1202 // public flags (8 byte connection_id)
1205 0x10, 0x32, 0x54, 0x76,
1206 0x98, 0xBA, 0xDC, 0xFE,
1208 0xBC, 0x9A, 0x78, 0x56,
1213 // frame type (padding frame)
1215 // Ignored data (which in this case is a stream frame)
1216 // frame type (stream frame with fin)
1219 0x04, 0x03, 0x02, 0x01,
1221 0x54, 0x76, 0x10, 0x32,
1222 0xDC, 0xFE, 0x98, 0xBA,
1232 QuicEncryptedPacket
encrypted(AsChars(packet
), arraysize(packet
), false);
1233 EXPECT_TRUE(framer_
.ProcessPacket(encrypted
));
1234 EXPECT_EQ(QUIC_NO_ERROR
, framer_
.error());
1235 ASSERT_TRUE(visitor_
.header_
.get());
1236 EXPECT_TRUE(CheckDecryption(encrypted
, !kIncludeVersion
));
1238 ASSERT_EQ(0u, visitor_
.stream_frames_
.size());
1239 EXPECT_EQ(0u, visitor_
.ack_frames_
.size());
1240 // A packet with no frames is not acceptable.
1241 CheckProcessingFails(
1242 packet
, GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID
, !kIncludeVersion
,
1243 PACKET_6BYTE_PACKET_NUMBER
, NOT_IN_FEC_GROUP
),
1244 "Packet has no frames.", QUIC_MISSING_PAYLOAD
);
1247 TEST_P(QuicFramerTest
, StreamFrame
) {
1249 unsigned char packet
[] = {
1250 // public flags (8 byte connection_id)
1253 0x10, 0x32, 0x54, 0x76,
1254 0x98, 0xBA, 0xDC, 0xFE,
1256 0xBC, 0x9A, 0x78, 0x56,
1261 // frame type (stream frame with fin)
1264 0x04, 0x03, 0x02, 0x01,
1266 0x54, 0x76, 0x10, 0x32,
1267 0xDC, 0xFE, 0x98, 0xBA,
1277 QuicEncryptedPacket
encrypted(AsChars(packet
), arraysize(packet
), false);
1278 EXPECT_TRUE(framer_
.ProcessPacket(encrypted
));
1280 EXPECT_EQ(QUIC_NO_ERROR
, framer_
.error());
1281 ASSERT_TRUE(visitor_
.header_
.get());
1282 EXPECT_TRUE(CheckDecryption(encrypted
, !kIncludeVersion
));
1284 ASSERT_EQ(1u, visitor_
.stream_frames_
.size());
1285 EXPECT_EQ(0u, visitor_
.ack_frames_
.size());
1286 EXPECT_EQ(static_cast<uint64
>(0x01020304),
1287 visitor_
.stream_frames_
[0]->stream_id
);
1288 EXPECT_TRUE(visitor_
.stream_frames_
[0]->fin
);
1289 EXPECT_EQ(UINT64_C(0xBA98FEDC32107654), visitor_
.stream_frames_
[0]->offset
);
1290 CheckStreamFrameData("hello world!", visitor_
.stream_frames_
[0]);
1292 // Now test framing boundaries.
1293 CheckStreamFrameBoundaries(packet
, kQuicMaxStreamIdSize
, !kIncludeVersion
);
1296 TEST_P(QuicFramerTest
, StreamFrame3ByteStreamId
) {
1298 unsigned char packet
[] = {
1299 // public flags (8 byte connection_id)
1302 0x10, 0x32, 0x54, 0x76,
1303 0x98, 0xBA, 0xDC, 0xFE,
1305 0xBC, 0x9A, 0x78, 0x56,
1310 // frame type (stream frame with fin)
1315 0x54, 0x76, 0x10, 0x32,
1316 0xDC, 0xFE, 0x98, 0xBA,
1326 QuicEncryptedPacket
encrypted(AsChars(packet
), arraysize(packet
), false);
1327 EXPECT_TRUE(framer_
.ProcessPacket(encrypted
));
1329 EXPECT_EQ(QUIC_NO_ERROR
, framer_
.error());
1330 ASSERT_TRUE(visitor_
.header_
.get());
1331 EXPECT_TRUE(CheckDecryption(encrypted
, !kIncludeVersion
));
1333 ASSERT_EQ(1u, visitor_
.stream_frames_
.size());
1334 EXPECT_EQ(0u, visitor_
.ack_frames_
.size());
1335 EXPECT_EQ(UINT64_C(0x00020304), visitor_
.stream_frames_
[0]->stream_id
);
1336 EXPECT_TRUE(visitor_
.stream_frames_
[0]->fin
);
1337 EXPECT_EQ(UINT64_C(0xBA98FEDC32107654), visitor_
.stream_frames_
[0]->offset
);
1338 CheckStreamFrameData("hello world!", visitor_
.stream_frames_
[0]);
1340 // Now test framing boundaries.
1341 const size_t stream_id_size
= 3;
1342 CheckStreamFrameBoundaries(packet
, stream_id_size
, !kIncludeVersion
);
1345 TEST_P(QuicFramerTest
, StreamFrame2ByteStreamId
) {
1347 unsigned char packet
[] = {
1348 // public flags (8 byte connection_id)
1351 0x10, 0x32, 0x54, 0x76,
1352 0x98, 0xBA, 0xDC, 0xFE,
1354 0xBC, 0x9A, 0x78, 0x56,
1359 // frame type (stream frame with fin)
1364 0x54, 0x76, 0x10, 0x32,
1365 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
) {
1397 unsigned char packet
[] = {
1398 // public flags (8 byte connection_id)
1401 0x10, 0x32, 0x54, 0x76,
1402 0x98, 0xBA, 0xDC, 0xFE,
1404 0xBC, 0x9A, 0x78, 0x56,
1409 // frame type (stream frame with fin)
1414 0x54, 0x76, 0x10, 0x32,
1415 0xDC, 0xFE, 0x98, 0xBA,
1425 QuicEncryptedPacket
encrypted(AsChars(packet
), arraysize(packet
), false);
1426 EXPECT_TRUE(framer_
.ProcessPacket(encrypted
));
1428 EXPECT_EQ(QUIC_NO_ERROR
, framer_
.error());
1429 ASSERT_TRUE(visitor_
.header_
.get());
1430 EXPECT_TRUE(CheckDecryption(encrypted
, !kIncludeVersion
));
1432 ASSERT_EQ(1u, visitor_
.stream_frames_
.size());
1433 EXPECT_EQ(0u, visitor_
.ack_frames_
.size());
1434 EXPECT_EQ(static_cast<uint64
>(0x00000004),
1435 visitor_
.stream_frames_
[0]->stream_id
);
1436 EXPECT_TRUE(visitor_
.stream_frames_
[0]->fin
);
1437 EXPECT_EQ(UINT64_C(0xBA98FEDC32107654), visitor_
.stream_frames_
[0]->offset
);
1438 CheckStreamFrameData("hello world!", visitor_
.stream_frames_
[0]);
1440 // Now test framing boundaries.
1441 const size_t stream_id_size
= 1;
1442 CheckStreamFrameBoundaries(packet
, stream_id_size
, !kIncludeVersion
);
1445 TEST_P(QuicFramerTest
, StreamFrameWithVersion
) {
1447 unsigned char packet
[] = {
1448 // public flags (version, 8 byte connection_id)
1451 0x10, 0x32, 0x54, 0x76,
1452 0x98, 0xBA, 0xDC, 0xFE,
1454 'Q', '0', GetQuicVersionDigitTens(), GetQuicVersionDigitOnes(),
1456 0xBC, 0x9A, 0x78, 0x56,
1461 // frame type (stream frame with fin)
1464 0x04, 0x03, 0x02, 0x01,
1466 0x54, 0x76, 0x10, 0x32,
1467 0xDC, 0xFE, 0x98, 0xBA,
1477 QuicEncryptedPacket
encrypted(AsChars(packet
), arraysize(packet
), false);
1478 EXPECT_TRUE(framer_
.ProcessPacket(encrypted
));
1480 EXPECT_EQ(QUIC_NO_ERROR
, framer_
.error());
1481 ASSERT_TRUE(visitor_
.header_
.get());
1482 EXPECT_TRUE(visitor_
.header_
->public_header
.version_flag
);
1483 EXPECT_EQ(GetParam(), visitor_
.header_
->public_header
.versions
[0]);
1484 EXPECT_TRUE(CheckDecryption(encrypted
, kIncludeVersion
));
1486 ASSERT_EQ(1u, visitor_
.stream_frames_
.size());
1487 EXPECT_EQ(0u, visitor_
.ack_frames_
.size());
1488 EXPECT_EQ(static_cast<uint64
>(0x01020304),
1489 visitor_
.stream_frames_
[0]->stream_id
);
1490 EXPECT_TRUE(visitor_
.stream_frames_
[0]->fin
);
1491 EXPECT_EQ(UINT64_C(0xBA98FEDC32107654), visitor_
.stream_frames_
[0]->offset
);
1492 CheckStreamFrameData("hello world!", visitor_
.stream_frames_
[0]);
1494 // Now test framing boundaries.
1495 CheckStreamFrameBoundaries(packet
, kQuicMaxStreamIdSize
, kIncludeVersion
);
1498 TEST_P(QuicFramerTest
, RejectPacket
) {
1499 visitor_
.accept_packet_
= false;
1502 unsigned char packet
[] = {
1503 // public flags (8 byte connection_id)
1506 0x10, 0x32, 0x54, 0x76,
1507 0x98, 0xBA, 0xDC, 0xFE,
1509 0xBC, 0x9A, 0x78, 0x56,
1514 // frame type (stream frame with fin)
1517 0x04, 0x03, 0x02, 0x01,
1519 0x54, 0x76, 0x10, 0x32,
1520 0xDC, 0xFE, 0x98, 0xBA,
1530 QuicEncryptedPacket
encrypted(AsChars(packet
), arraysize(packet
), false);
1531 EXPECT_TRUE(framer_
.ProcessPacket(encrypted
));
1533 EXPECT_EQ(QUIC_NO_ERROR
, framer_
.error());
1534 ASSERT_TRUE(visitor_
.header_
.get());
1535 EXPECT_TRUE(CheckDecryption(encrypted
, !kIncludeVersion
));
1537 ASSERT_EQ(0u, visitor_
.stream_frames_
.size());
1538 EXPECT_EQ(0u, visitor_
.ack_frames_
.size());
1541 TEST_P(QuicFramerTest
, RejectPublicHeader
) {
1542 visitor_
.accept_public_header_
= false;
1545 unsigned char packet
[] = {
1546 // public flags (8 byte connection_id)
1549 0x10, 0x32, 0x54, 0x76,
1550 0x98, 0xBA, 0xDC, 0xFE,
1554 QuicEncryptedPacket
encrypted(AsChars(packet
), arraysize(packet
), false);
1555 EXPECT_TRUE(framer_
.ProcessPacket(encrypted
));
1557 EXPECT_EQ(QUIC_NO_ERROR
, framer_
.error());
1558 ASSERT_TRUE(visitor_
.public_header_
.get());
1559 ASSERT_FALSE(visitor_
.header_
.get());
1562 TEST_P(QuicFramerTest
, RevivedStreamFrame
) {
1564 unsigned char payload
[] = {
1565 // frame type (stream frame with fin)
1568 0x04, 0x03, 0x02, 0x01,
1570 0x54, 0x76, 0x10, 0x32,
1571 0xDC, 0xFE, 0x98, 0xBA,
1581 QuicPacketHeader header
;
1582 header
.public_header
.connection_id
= UINT64_C(0xFEDCBA9876543210);
1583 header
.public_header
.reset_flag
= false;
1584 header
.public_header
.version_flag
= false;
1585 header
.fec_flag
= true;
1586 header
.entropy_flag
= true;
1587 header
.packet_packet_number
= UINT64_C(0x123456789ABC);
1588 header
.fec_group
= 0;
1590 // Do not encrypt the payload because the revived payload is post-encryption.
1591 EXPECT_TRUE(framer_
.ProcessRevivedPacket(&header
,
1592 StringPiece(AsChars(payload
),
1593 arraysize(payload
))));
1595 EXPECT_EQ(QUIC_NO_ERROR
, framer_
.error());
1596 ASSERT_EQ(1, visitor_
.revived_packets_
);
1597 ASSERT_TRUE(visitor_
.header_
.get());
1598 EXPECT_EQ(UINT64_C(0xFEDCBA9876543210),
1599 visitor_
.header_
->public_header
.connection_id
);
1600 EXPECT_FALSE(visitor_
.header_
->public_header
.reset_flag
);
1601 EXPECT_FALSE(visitor_
.header_
->public_header
.version_flag
);
1602 EXPECT_TRUE(visitor_
.header_
->fec_flag
);
1603 EXPECT_TRUE(visitor_
.header_
->entropy_flag
);
1604 EXPECT_EQ(1 << (header
.packet_packet_number
% 8),
1605 visitor_
.header_
->entropy_hash
);
1606 EXPECT_EQ(UINT64_C(0x123456789ABC), visitor_
.header_
->packet_packet_number
);
1607 EXPECT_EQ(NOT_IN_FEC_GROUP
, visitor_
.header_
->is_in_fec_group
);
1608 EXPECT_EQ(0x00u
, visitor_
.header_
->fec_group
);
1610 ASSERT_EQ(1u, visitor_
.stream_frames_
.size());
1611 EXPECT_EQ(0u, visitor_
.ack_frames_
.size());
1612 EXPECT_EQ(UINT64_C(0x01020304), visitor_
.stream_frames_
[0]->stream_id
);
1613 EXPECT_TRUE(visitor_
.stream_frames_
[0]->fin
);
1614 EXPECT_EQ(UINT64_C(0xBA98FEDC32107654), visitor_
.stream_frames_
[0]->offset
);
1615 CheckStreamFrameData("hello world!", visitor_
.stream_frames_
[0]);
1618 TEST_P(QuicFramerTest
, StreamFrameInFecGroup
) {
1620 unsigned char packet
[] = {
1621 // public flags (8 byte connection_id)
1624 0x10, 0x32, 0x54, 0x76,
1625 0x98, 0xBA, 0xDC, 0xFE,
1627 0xBC, 0x9A, 0x78, 0x56,
1629 // private flags (fec group)
1631 // first fec protected packet offset
1634 // frame type (stream frame with fin)
1637 0x04, 0x03, 0x02, 0x01,
1639 0x54, 0x76, 0x10, 0x32,
1640 0xDC, 0xFE, 0x98, 0xBA,
1650 QuicEncryptedPacket
encrypted(AsChars(packet
), arraysize(packet
), false);
1651 EXPECT_TRUE(framer_
.ProcessPacket(encrypted
));
1653 EXPECT_EQ(QUIC_NO_ERROR
, framer_
.error());
1654 ASSERT_TRUE(visitor_
.header_
.get());
1655 EXPECT_TRUE(CheckDecryption(encrypted
, !kIncludeVersion
));
1656 EXPECT_EQ(IN_FEC_GROUP
, visitor_
.header_
->is_in_fec_group
);
1657 EXPECT_EQ(UINT64_C(0x341256789ABA), visitor_
.header_
->fec_group
);
1658 const size_t fec_offset
= GetStartOfFecProtectedData(
1659 PACKET_8BYTE_CONNECTION_ID
, !kIncludeVersion
, PACKET_6BYTE_PACKET_NUMBER
);
1661 string(AsChars(packet
) + fec_offset
, arraysize(packet
) - fec_offset
),
1662 visitor_
.fec_protected_payload_
);
1664 ASSERT_EQ(1u, visitor_
.stream_frames_
.size());
1665 EXPECT_EQ(0u, visitor_
.ack_frames_
.size());
1666 EXPECT_EQ(UINT64_C(0x01020304), visitor_
.stream_frames_
[0]->stream_id
);
1667 EXPECT_TRUE(visitor_
.stream_frames_
[0]->fin
);
1668 EXPECT_EQ(UINT64_C(0xBA98FEDC32107654), visitor_
.stream_frames_
[0]->offset
);
1669 CheckStreamFrameData("hello world!", visitor_
.stream_frames_
[0]);
1672 TEST_P(QuicFramerTest
, AckFrameTwoTimestamp
) {
1674 unsigned char packet
[] = {
1675 // public flags (8 byte connection_id)
1678 0x10, 0x32, 0x54, 0x76, 0x98, 0xBA, 0xDC, 0xFE,
1680 0xA8, 0x9A, 0x78, 0x56, 0x34, 0x12,
1681 // private flags (entropy)
1684 // frame type (ack frame)
1685 // (has nacks, not truncated, 6 byte largest observed, 1 byte delta)
1687 // entropy hash of all received packets.
1689 // largest observed packet number
1690 0xBF, 0x9A, 0x78, 0x56, 0x34, 0x12,
1693 // Number of timestamps.
1695 // Delta from largest observed.
1698 0x10, 0x32, 0x54, 0x76,
1699 // Delta from largest observed.
1703 // num missing packets
1705 // missing packet delta
1707 // 0 more missing packets in range.
1709 // Number of revived packets.
1714 QuicEncryptedPacket
encrypted(AsChars(packet
), arraysize(packet
), false);
1715 EXPECT_TRUE(framer_
.ProcessPacket(encrypted
));
1717 EXPECT_EQ(QUIC_NO_ERROR
, framer_
.error());
1718 ASSERT_TRUE(visitor_
.header_
.get());
1719 EXPECT_TRUE(CheckDecryption(encrypted
, !kIncludeVersion
));
1721 EXPECT_EQ(0u, visitor_
.stream_frames_
.size());
1722 ASSERT_EQ(1u, visitor_
.ack_frames_
.size());
1723 const QuicAckFrame
& frame
= *visitor_
.ack_frames_
[0];
1724 EXPECT_EQ(0xBA, frame
.entropy_hash
);
1725 EXPECT_EQ(UINT64_C(0x0123456789ABF), frame
.largest_observed
);
1726 ASSERT_EQ(1u, frame
.missing_packets
.size());
1727 ASSERT_EQ(2u, frame
.received_packet_times
.size());
1728 PacketNumberSet::const_iterator missing_iter
= frame
.missing_packets
.begin();
1729 EXPECT_EQ(UINT64_C(0x0123456789ABE), *missing_iter
);
1731 const size_t kReceivedEntropyOffset
= kQuicFrameTypeSize
;
1732 const size_t kLargestObservedOffset
= kReceivedEntropyOffset
+
1733 kQuicEntropyHashSize
;
1734 const size_t kMissingDeltaTimeOffset
=
1735 kLargestObservedOffset
+ PACKET_6BYTE_PACKET_NUMBER
;
1736 const size_t kNumTimestampsOffset
=
1737 kMissingDeltaTimeOffset
+ kQuicDeltaTimeLargestObservedSize
;
1738 const size_t kTimestampDeltaLargestObserved1
=
1739 kNumTimestampsOffset
+ kQuicNumTimestampsSize
;
1740 const size_t kTimestampTimeDeltaLargestObserved1
=
1741 kTimestampDeltaLargestObserved1
+ 1;
1742 const size_t kTimestampDeltaLargestObserved2
=
1743 kTimestampTimeDeltaLargestObserved1
+ 4;
1744 const size_t kTimestampTimeDeltaLargestObserved2
=
1745 kTimestampDeltaLargestObserved2
+ 1;
1746 const size_t kNumMissingPacketOffset
=
1747 kTimestampTimeDeltaLargestObserved2
+ 2;
1748 const size_t kMissingPacketsOffset
=
1749 kNumMissingPacketOffset
+ kNumberOfNackRangesSize
;
1750 const size_t kMissingPacketsRange
=
1751 kMissingPacketsOffset
+ PACKET_1BYTE_PACKET_NUMBER
;
1752 const size_t kRevivedPacketsLength
=
1753 kMissingPacketsRange
+ PACKET_1BYTE_PACKET_NUMBER
;
1754 // Now test framing boundaries.
1755 const size_t ack_frame_size
=
1756 kRevivedPacketsLength
+ PACKET_1BYTE_PACKET_NUMBER
;
1757 for (size_t i
= kQuicFrameTypeSize
; i
< ack_frame_size
; ++i
) {
1758 string expected_error
;
1759 if (i
< kLargestObservedOffset
) {
1760 expected_error
= "Unable to read entropy hash for received packets.";
1761 } else if (i
< kMissingDeltaTimeOffset
) {
1762 expected_error
= "Unable to read largest observed.";
1763 } else if (i
< kNumTimestampsOffset
) {
1764 expected_error
= "Unable to read delta time largest observed.";
1765 } else if (i
< kTimestampDeltaLargestObserved1
) {
1766 expected_error
= "Unable to read num received packets.";
1767 } else if (i
< kTimestampTimeDeltaLargestObserved1
) {
1768 expected_error
= "Unable to read sequence delta in received packets.";
1769 } else if (i
< kTimestampDeltaLargestObserved2
) {
1770 expected_error
= "Unable to read time delta in received packets.";
1771 } else if (i
< kTimestampTimeDeltaLargestObserved2
) {
1772 expected_error
= "Unable to read sequence delta in received packets.";
1773 } else if (i
< kNumMissingPacketOffset
) {
1775 "Unable to read incremental time delta in received packets.";
1776 } else if (i
< kMissingPacketsOffset
) {
1777 expected_error
= "Unable to read num missing packet ranges.";
1778 } else if (i
< kMissingPacketsRange
) {
1779 expected_error
= "Unable to read missing packet number delta.";
1780 } else if (i
< kRevivedPacketsLength
) {
1781 expected_error
= "Unable to read missing packet number range.";
1783 expected_error
= "Unable to read num revived packets.";
1785 CheckProcessingFails(
1787 i
+ GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID
, !kIncludeVersion
,
1788 PACKET_6BYTE_PACKET_NUMBER
, NOT_IN_FEC_GROUP
),
1789 expected_error
, QUIC_INVALID_ACK_DATA
);
1793 TEST_P(QuicFramerTest
, AckFrameOneTimestamp
) {
1795 unsigned char packet
[] = {
1796 // public flags (8 byte connection_id)
1799 0x10, 0x32, 0x54, 0x76, 0x98, 0xBA, 0xDC, 0xFE,
1801 0xA8, 0x9A, 0x78, 0x56, 0x34, 0x12,
1802 // private flags (entropy)
1805 // frame type (ack frame)
1806 // (has nacks, not truncated, 6 byte largest observed, 1 byte delta)
1808 // entropy hash of all received packets.
1810 // largest observed packet number
1811 0xBF, 0x9A, 0x78, 0x56, 0x34, 0x12,
1814 // Number of timestamps.
1816 // Delta from largest observed.
1819 0x10, 0x32, 0x54, 0x76,
1820 // num missing packets
1822 // missing packet delta
1824 // 0 more missing packets in range.
1826 // Number of revived packets.
1831 QuicEncryptedPacket
encrypted(AsChars(packet
), arraysize(packet
), false);
1832 EXPECT_TRUE(framer_
.ProcessPacket(encrypted
));
1834 EXPECT_EQ(QUIC_NO_ERROR
, framer_
.error());
1835 ASSERT_TRUE(visitor_
.header_
.get());
1836 EXPECT_TRUE(CheckDecryption(encrypted
, !kIncludeVersion
));
1838 EXPECT_EQ(0u, visitor_
.stream_frames_
.size());
1839 ASSERT_EQ(1u, visitor_
.ack_frames_
.size());
1840 const QuicAckFrame
& frame
= *visitor_
.ack_frames_
[0];
1841 EXPECT_EQ(0xBA, frame
.entropy_hash
);
1842 EXPECT_EQ(UINT64_C(0x0123456789ABF), frame
.largest_observed
);
1843 ASSERT_EQ(1u, frame
.missing_packets
.size());
1844 ASSERT_EQ(1u, frame
.received_packet_times
.size());
1845 PacketNumberSet::const_iterator missing_iter
= frame
.missing_packets
.begin();
1846 EXPECT_EQ(UINT64_C(0x0123456789ABE), *missing_iter
);
1848 const size_t kReceivedEntropyOffset
= kQuicFrameTypeSize
;
1849 const size_t kLargestObservedOffset
=
1850 kReceivedEntropyOffset
+ kQuicEntropyHashSize
;
1851 const size_t kMissingDeltaTimeOffset
=
1852 kLargestObservedOffset
+ PACKET_6BYTE_PACKET_NUMBER
;
1853 const size_t kNumTimestampsOffset
=
1854 kMissingDeltaTimeOffset
+ kQuicDeltaTimeLargestObservedSize
;
1855 const size_t kTimestampDeltaLargestObserved
=
1856 kNumTimestampsOffset
+ kQuicNumTimestampsSize
;
1857 const size_t kTimestampTimeDeltaLargestObserved
=
1858 kTimestampDeltaLargestObserved
+ 1;
1859 const size_t kNumMissingPacketOffset
= kTimestampTimeDeltaLargestObserved
+ 4;
1860 const size_t kMissingPacketsOffset
=
1861 kNumMissingPacketOffset
+ kNumberOfNackRangesSize
;
1862 const size_t kMissingPacketsRange
=
1863 kMissingPacketsOffset
+ PACKET_1BYTE_PACKET_NUMBER
;
1864 const size_t kRevivedPacketsLength
=
1865 kMissingPacketsRange
+ PACKET_1BYTE_PACKET_NUMBER
;
1866 // Now test framing boundaries.
1867 const size_t ack_frame_size
=
1868 kRevivedPacketsLength
+ PACKET_1BYTE_PACKET_NUMBER
;
1869 for (size_t i
= kQuicFrameTypeSize
; i
< ack_frame_size
; ++i
) {
1870 string expected_error
;
1871 if (i
< kLargestObservedOffset
) {
1872 expected_error
= "Unable to read entropy hash for received packets.";
1873 } else if (i
< kMissingDeltaTimeOffset
) {
1874 expected_error
= "Unable to read largest observed.";
1875 } else if (i
< kNumTimestampsOffset
) {
1876 expected_error
= "Unable to read delta time largest observed.";
1877 } else if (i
< kTimestampDeltaLargestObserved
) {
1878 expected_error
= "Unable to read num received packets.";
1879 } else if (i
< kTimestampTimeDeltaLargestObserved
) {
1880 expected_error
= "Unable to read sequence delta in received packets.";
1881 } else if (i
< kNumMissingPacketOffset
) {
1882 expected_error
= "Unable to read time delta in received packets.";
1883 } else if (i
< kMissingPacketsOffset
) {
1884 expected_error
= "Unable to read num missing packet ranges.";
1885 } else if (i
< kMissingPacketsRange
) {
1886 expected_error
= "Unable to read missing packet number delta.";
1887 } else if (i
< kRevivedPacketsLength
) {
1888 expected_error
= "Unable to read missing packet number range.";
1890 expected_error
= "Unable to read num revived packets.";
1892 CheckProcessingFails(
1894 i
+ GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID
, !kIncludeVersion
,
1895 PACKET_6BYTE_PACKET_NUMBER
, NOT_IN_FEC_GROUP
),
1896 expected_error
, QUIC_INVALID_ACK_DATA
);
1900 TEST_P(QuicFramerTest
, AckFrame
) {
1902 unsigned char packet
[] = {
1903 // public flags (8 byte connection_id)
1906 0x10, 0x32, 0x54, 0x76, 0x98, 0xBA, 0xDC, 0xFE,
1908 0xA8, 0x9A, 0x78, 0x56, 0x34, 0x12,
1909 // private flags (entropy)
1912 // frame type (ack frame)
1913 // (has nacks, not truncated, 6 byte largest observed, 1 byte delta)
1915 // entropy hash of all received packets.
1917 // largest observed packet number
1918 0xBF, 0x9A, 0x78, 0x56, 0x34, 0x12,
1921 // Number of timestamps.
1923 // num missing packets
1925 // missing packet delta
1927 // 0 more missing packets in range.
1929 // Number of revived packets.
1934 QuicEncryptedPacket
encrypted(AsChars(packet
), arraysize(packet
), false);
1935 EXPECT_TRUE(framer_
.ProcessPacket(encrypted
));
1937 EXPECT_EQ(QUIC_NO_ERROR
, framer_
.error());
1938 ASSERT_TRUE(visitor_
.header_
.get());
1939 EXPECT_TRUE(CheckDecryption(encrypted
, !kIncludeVersion
));
1941 EXPECT_EQ(0u, visitor_
.stream_frames_
.size());
1942 ASSERT_EQ(1u, visitor_
.ack_frames_
.size());
1943 const QuicAckFrame
& frame
= *visitor_
.ack_frames_
[0];
1944 EXPECT_EQ(0xBA, frame
.entropy_hash
);
1945 EXPECT_EQ(UINT64_C(0x0123456789ABF), frame
.largest_observed
);
1946 ASSERT_EQ(1u, frame
.missing_packets
.size());
1947 PacketNumberSet::const_iterator missing_iter
= frame
.missing_packets
.begin();
1948 EXPECT_EQ(UINT64_C(0x0123456789ABE), *missing_iter
);
1950 const size_t kReceivedEntropyOffset
= kQuicFrameTypeSize
;
1951 const size_t kLargestObservedOffset
= kReceivedEntropyOffset
+
1952 kQuicEntropyHashSize
;
1953 const size_t kMissingDeltaTimeOffset
=
1954 kLargestObservedOffset
+ PACKET_6BYTE_PACKET_NUMBER
;
1955 const size_t kNumTimestampsOffset
= kMissingDeltaTimeOffset
+
1956 kQuicDeltaTimeLargestObservedSize
;
1957 const size_t kNumMissingPacketOffset
= kNumTimestampsOffset
+
1958 kQuicNumTimestampsSize
;
1959 const size_t kMissingPacketsOffset
= kNumMissingPacketOffset
+
1960 kNumberOfNackRangesSize
;
1961 const size_t kMissingPacketsRange
=
1962 kMissingPacketsOffset
+ PACKET_1BYTE_PACKET_NUMBER
;
1963 const size_t kRevivedPacketsLength
=
1964 kMissingPacketsRange
+ PACKET_1BYTE_PACKET_NUMBER
;
1965 // Now test framing boundaries.
1966 const size_t ack_frame_size
=
1967 kRevivedPacketsLength
+ PACKET_1BYTE_PACKET_NUMBER
;
1968 for (size_t i
= kQuicFrameTypeSize
; i
< ack_frame_size
; ++i
) {
1969 string expected_error
;
1970 if (i
< kLargestObservedOffset
) {
1971 expected_error
= "Unable to read entropy hash for received packets.";
1972 } else if (i
< kMissingDeltaTimeOffset
) {
1973 expected_error
= "Unable to read largest observed.";
1974 } else if (i
< kNumTimestampsOffset
) {
1975 expected_error
= "Unable to read delta time largest observed.";
1976 } else if (i
< kNumMissingPacketOffset
) {
1977 expected_error
= "Unable to read num received packets.";
1978 } else if (i
< kMissingPacketsOffset
) {
1979 expected_error
= "Unable to read num missing packet ranges.";
1980 } else if (i
< kMissingPacketsRange
) {
1981 expected_error
= "Unable to read missing packet number delta.";
1982 } else if (i
< kRevivedPacketsLength
) {
1983 expected_error
= "Unable to read missing packet number range.";
1985 expected_error
= "Unable to read num revived packets.";
1987 CheckProcessingFails(
1989 i
+ GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID
, !kIncludeVersion
,
1990 PACKET_6BYTE_PACKET_NUMBER
, NOT_IN_FEC_GROUP
),
1991 expected_error
, QUIC_INVALID_ACK_DATA
);
1995 TEST_P(QuicFramerTest
, AckFrameRevivedPackets
) {
1997 unsigned char packet
[] = {
1998 // public flags (8 byte connection_id)
2001 0x10, 0x32, 0x54, 0x76, 0x98, 0xBA, 0xDC, 0xFE,
2003 0xA8, 0x9A, 0x78, 0x56, 0x34, 0x12,
2004 // private flags (entropy)
2007 // frame type (ack frame)
2008 // (has nacks, not truncated, 6 byte largest observed, 1 byte delta)
2010 // entropy hash of all received packets.
2012 // largest observed packet number
2013 0xBF, 0x9A, 0x78, 0x56, 0x34, 0x12,
2016 // num received packets.
2018 // num missing packets
2020 // missing packet delta
2022 // 0 more missing packets in range.
2024 // Number of revived packets.
2026 // Revived packet number.
2027 0xBE, 0x9A, 0x78, 0x56, 0x34, 0x12,
2028 // Number of revived packets.
2033 QuicEncryptedPacket
encrypted(AsChars(packet
), arraysize(packet
), false);
2034 EXPECT_TRUE(framer_
.ProcessPacket(encrypted
));
2036 EXPECT_EQ(QUIC_NO_ERROR
, framer_
.error());
2037 ASSERT_TRUE(visitor_
.header_
.get());
2038 EXPECT_TRUE(CheckDecryption(encrypted
, !kIncludeVersion
));
2040 EXPECT_EQ(0u, visitor_
.stream_frames_
.size());
2041 ASSERT_EQ(1u, visitor_
.ack_frames_
.size());
2042 const QuicAckFrame
& frame
= *visitor_
.ack_frames_
[0];
2043 EXPECT_EQ(0xBA, frame
.entropy_hash
);
2044 EXPECT_EQ(UINT64_C(0x0123456789ABF), frame
.largest_observed
);
2045 ASSERT_EQ(1u, frame
.missing_packets
.size());
2046 PacketNumberSet::const_iterator missing_iter
= frame
.missing_packets
.begin();
2047 EXPECT_EQ(UINT64_C(0x0123456789ABE), *missing_iter
);
2049 const size_t kReceivedEntropyOffset
= kQuicFrameTypeSize
;
2050 const size_t kLargestObservedOffset
= kReceivedEntropyOffset
+
2051 kQuicEntropyHashSize
;
2052 const size_t kMissingDeltaTimeOffset
=
2053 kLargestObservedOffset
+ PACKET_6BYTE_PACKET_NUMBER
;
2054 const size_t kNumTimestampsOffset
= kMissingDeltaTimeOffset
+
2055 kQuicDeltaTimeLargestObservedSize
;
2056 const size_t kNumMissingPacketOffset
= kNumTimestampsOffset
+
2057 kQuicNumTimestampsSize
;
2058 const size_t kMissingPacketsOffset
= kNumMissingPacketOffset
+
2059 kNumberOfNackRangesSize
;
2060 const size_t kMissingPacketsRange
=
2061 kMissingPacketsOffset
+ PACKET_1BYTE_PACKET_NUMBER
;
2062 const size_t kRevivedPacketsLength
=
2063 kMissingPacketsRange
+ PACKET_1BYTE_PACKET_NUMBER
;
2064 const size_t kRevivedPacketSequenceNumberLength
=
2065 kRevivedPacketsLength
+ PACKET_1BYTE_PACKET_NUMBER
;
2066 // Now test framing boundaries.
2067 const size_t ack_frame_size
=
2068 kRevivedPacketSequenceNumberLength
+ PACKET_6BYTE_PACKET_NUMBER
;
2069 for (size_t i
= kQuicFrameTypeSize
; i
< ack_frame_size
; ++i
) {
2070 string expected_error
;
2071 if (i
< kReceivedEntropyOffset
) {
2072 expected_error
= "Unable to read least unacked delta.";
2073 } else if (i
< kLargestObservedOffset
) {
2074 expected_error
= "Unable to read entropy hash for received packets.";
2075 } else if (i
< kMissingDeltaTimeOffset
) {
2076 expected_error
= "Unable to read largest observed.";
2077 } else if (i
< kNumTimestampsOffset
) {
2078 expected_error
= "Unable to read delta time largest observed.";
2079 } else if (i
< kNumMissingPacketOffset
) {
2080 expected_error
= "Unable to read num received packets.";
2081 } else if (i
< kMissingPacketsOffset
) {
2082 expected_error
= "Unable to read num missing packet ranges.";
2083 } else if (i
< kMissingPacketsRange
) {
2084 expected_error
= "Unable to read missing packet number delta.";
2085 } else if (i
< kRevivedPacketsLength
) {
2086 expected_error
= "Unable to read missing packet number range.";
2087 } else if (i
< kRevivedPacketSequenceNumberLength
) {
2088 expected_error
= "Unable to read num revived packets.";
2090 expected_error
= "Unable to read revived packet.";
2092 CheckProcessingFails(
2094 i
+ GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID
, !kIncludeVersion
,
2095 PACKET_6BYTE_PACKET_NUMBER
, NOT_IN_FEC_GROUP
),
2096 expected_error
, QUIC_INVALID_ACK_DATA
);
2100 TEST_P(QuicFramerTest
, AckFrameNoNacks
) {
2102 unsigned char packet
[] = {
2103 // public flags (8 byte connection_id)
2106 0x10, 0x32, 0x54, 0x76, 0x98, 0xBA, 0xDC, 0xFE,
2108 0xA8, 0x9A, 0x78, 0x56, 0x34, 0x12,
2109 // private flags (entropy)
2112 // frame type (ack frame)
2113 // (no nacks, not truncated, 6 byte largest observed, 1 byte delta)
2115 // entropy hash of all received packets.
2117 // largest observed packet number
2118 0xBF, 0x9A, 0x78, 0x56, 0x34, 0x12,
2121 // Number of received packets.
2126 QuicEncryptedPacket
encrypted(AsChars(packet
), arraysize(packet
), false);
2127 EXPECT_TRUE(framer_
.ProcessPacket(encrypted
));
2129 EXPECT_EQ(QUIC_NO_ERROR
, framer_
.error());
2130 ASSERT_TRUE(visitor_
.header_
.get());
2131 EXPECT_TRUE(CheckDecryption(encrypted
, !kIncludeVersion
));
2133 EXPECT_EQ(0u, visitor_
.stream_frames_
.size());
2134 ASSERT_EQ(1u, visitor_
.ack_frames_
.size());
2135 QuicAckFrame
* frame
= visitor_
.ack_frames_
[0];
2136 EXPECT_EQ(0xBA, frame
->entropy_hash
);
2137 EXPECT_EQ(UINT64_C(0x0123456789ABF), frame
->largest_observed
);
2138 ASSERT_EQ(0u, frame
->missing_packets
.size());
2140 // Verify that the packet re-serializes identically.
2142 frames
.push_back(QuicFrame(frame
));
2143 scoped_ptr
<QuicPacket
> data(BuildDataPacket(*visitor_
.header_
, frames
));
2144 ASSERT_TRUE(data
!= nullptr);
2146 test::CompareCharArraysWithHexError("constructed packet", data
->data(),
2147 data
->length(), AsChars(packet
),
2151 TEST_P(QuicFramerTest
, AckFrame500Nacks
) {
2153 unsigned char packet
[] = {
2154 // public flags (8 byte connection_id)
2157 0x10, 0x32, 0x54, 0x76, 0x98, 0xBA, 0xDC, 0xFE,
2159 0xA8, 0x9A, 0x78, 0x56, 0x34, 0x12,
2160 // private flags (entropy)
2163 // frame type (ack frame)
2164 // (has nacks, not truncated, 6 byte largest observed, 1 byte delta)
2166 // entropy hash of all received packets.
2168 // largest observed packet number
2169 0xBF, 0x9A, 0x78, 0x56, 0x34, 0x12,
2172 // No received packets.
2174 // num missing packet ranges
2176 // missing packet delta
2178 // 243 more missing packets in range.
2179 // The ranges are listed in this order so the re-constructed packet
2182 // No gap between ranges
2184 // 255 more missing packets in range.
2186 // No revived packets.
2191 QuicEncryptedPacket
encrypted(AsChars(packet
), arraysize(packet
), false);
2192 EXPECT_TRUE(framer_
.ProcessPacket(encrypted
));
2194 EXPECT_EQ(QUIC_NO_ERROR
, framer_
.error());
2195 ASSERT_TRUE(visitor_
.header_
.get());
2196 EXPECT_TRUE(CheckDecryption(encrypted
, !kIncludeVersion
));
2198 EXPECT_EQ(0u, visitor_
.stream_frames_
.size());
2199 ASSERT_EQ(1u, visitor_
.ack_frames_
.size());
2200 QuicAckFrame
* frame
= visitor_
.ack_frames_
[0];
2201 EXPECT_EQ(0xBA, frame
->entropy_hash
);
2202 EXPECT_EQ(UINT64_C(0x0123456789ABF), frame
->largest_observed
);
2203 EXPECT_EQ(0u, frame
->revived_packets
.size());
2204 ASSERT_EQ(500u, frame
->missing_packets
.size());
2205 PacketNumberSet::const_iterator first_missing_iter
=
2206 frame
->missing_packets
.begin();
2207 EXPECT_EQ(UINT64_C(0x0123456789ABE) - 499, *first_missing_iter
);
2208 PacketNumberSet::const_reverse_iterator last_missing_iter
=
2209 frame
->missing_packets
.rbegin();
2210 EXPECT_EQ(UINT64_C(0x0123456789ABE), *last_missing_iter
);
2212 // Verify that the packet re-serializes identically.
2214 frames
.push_back(QuicFrame(frame
));
2215 scoped_ptr
<QuicPacket
> data(BuildDataPacket(*visitor_
.header_
, frames
));
2216 ASSERT_TRUE(data
!= nullptr);
2218 test::CompareCharArraysWithHexError("constructed packet",
2219 data
->data(), data
->length(),
2220 AsChars(packet
), arraysize(packet
));
2223 TEST_P(QuicFramerTest
, StopWaitingFrame
) {
2225 unsigned char packet
[] = {
2226 // public flags (8 byte connection_id)
2229 0x10, 0x32, 0x54, 0x76,
2230 0x98, 0xBA, 0xDC, 0xFE,
2232 0xA8, 0x9A, 0x78, 0x56,
2234 // private flags (entropy)
2237 // frame type (ack frame)
2238 // (has nacks, not truncated, 6 byte largest observed, 1 byte delta)
2240 // entropy hash of sent packets till least awaiting - 1.
2242 // least packet number awaiting an ack, delta from packet number.
2243 0x08, 0x00, 0x00, 0x00,
2248 QuicEncryptedPacket
encrypted(AsChars(packet
), arraysize(packet
), false);
2249 EXPECT_TRUE(framer_
.ProcessPacket(encrypted
));
2251 EXPECT_EQ(QUIC_NO_ERROR
, framer_
.error());
2252 ASSERT_TRUE(visitor_
.header_
.get());
2253 EXPECT_TRUE(CheckDecryption(encrypted
, !kIncludeVersion
));
2255 EXPECT_EQ(0u, visitor_
.stream_frames_
.size());
2256 ASSERT_EQ(1u, visitor_
.stop_waiting_frames_
.size());
2257 const QuicStopWaitingFrame
& frame
= *visitor_
.stop_waiting_frames_
[0];
2258 EXPECT_EQ(0xAB, frame
.entropy_hash
);
2259 EXPECT_EQ(UINT64_C(0x0123456789AA0), frame
.least_unacked
);
2261 const size_t kSentEntropyOffset
= kQuicFrameTypeSize
;
2262 const size_t kLeastUnackedOffset
= kSentEntropyOffset
+ kQuicEntropyHashSize
;
2263 const size_t frame_size
= 7;
2264 for (size_t i
= kQuicFrameTypeSize
; i
< frame_size
; ++i
) {
2265 string expected_error
;
2266 if (i
< kLeastUnackedOffset
) {
2267 expected_error
= "Unable to read entropy hash for sent packets.";
2269 expected_error
= "Unable to read least unacked delta.";
2271 CheckProcessingFails(
2273 i
+ GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID
, !kIncludeVersion
,
2274 PACKET_6BYTE_PACKET_NUMBER
, NOT_IN_FEC_GROUP
),
2275 expected_error
, QUIC_INVALID_STOP_WAITING_DATA
);
2279 TEST_P(QuicFramerTest
, RstStreamFrameQuicVersion24
) {
2280 if (version_
> QUIC_VERSION_24
) {
2281 // QUIC_VERSION_25 removes the error_details field from QuicRstStreamFrame.
2286 unsigned char packet
[] = {
2287 // public flags (8 byte connection_id)
2290 0x10, 0x32, 0x54, 0x76,
2291 0x98, 0xBA, 0xDC, 0xFE,
2293 0xBC, 0x9A, 0x78, 0x56,
2298 // frame type (rst stream frame)
2301 0x04, 0x03, 0x02, 0x01,
2304 0x01, 0x02, 0x03, 0x04,
2305 0x05, 0x06, 0x07, 0x08,
2308 0x01, 0x00, 0x00, 0x00,
2310 // error details length
2320 QuicEncryptedPacket
encrypted(AsChars(packet
), arraysize(packet
), false);
2321 EXPECT_TRUE(framer_
.ProcessPacket(encrypted
));
2323 EXPECT_EQ(QUIC_NO_ERROR
, framer_
.error());
2324 ASSERT_TRUE(visitor_
.header_
.get());
2325 EXPECT_TRUE(CheckDecryption(encrypted
, !kIncludeVersion
));
2327 EXPECT_EQ(UINT64_C(0x01020304), visitor_
.rst_stream_frame_
.stream_id
);
2328 EXPECT_EQ(0x01, visitor_
.rst_stream_frame_
.error_code
);
2329 EXPECT_EQ("because I can", visitor_
.rst_stream_frame_
.error_details
);
2330 EXPECT_EQ(UINT64_C(0x0807060504030201),
2331 visitor_
.rst_stream_frame_
.byte_offset
);
2333 // Now test framing boundaries.
2334 for (size_t i
= kQuicFrameTypeSize
;
2335 i
< QuicFramer::GetMinRstStreamFrameSize(); ++i
) {
2336 string expected_error
;
2337 if (i
< kQuicFrameTypeSize
+ kQuicMaxStreamIdSize
) {
2338 expected_error
= "Unable to read stream_id.";
2339 } else if (i
< kQuicFrameTypeSize
+ kQuicMaxStreamIdSize
+
2340 kQuicMaxStreamOffsetSize
) {
2341 expected_error
= "Unable to read rst stream sent byte offset.";
2342 } else if (i
< kQuicFrameTypeSize
+ kQuicMaxStreamIdSize
+
2343 kQuicMaxStreamOffsetSize
+ kQuicErrorCodeSize
) {
2344 expected_error
= "Unable to read rst stream error code.";
2346 expected_error
= "Unable to read rst stream error details.";
2348 CheckProcessingFails(
2350 i
+ GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID
, !kIncludeVersion
,
2351 PACKET_6BYTE_PACKET_NUMBER
, NOT_IN_FEC_GROUP
),
2352 expected_error
, QUIC_INVALID_RST_STREAM_DATA
);
2356 TEST_P(QuicFramerTest
, RstStreamFrameQuic
) {
2357 if (version_
<= QUIC_VERSION_24
) {
2358 // QUIC_VERSION_25 removes the error_details field from QuicRstStreamFrame.
2363 unsigned char packet
[] = {
2364 // public flags (8 byte connection_id)
2367 0x10, 0x32, 0x54, 0x76,
2368 0x98, 0xBA, 0xDC, 0xFE,
2370 0xBC, 0x9A, 0x78, 0x56,
2375 // frame type (rst stream frame)
2378 0x04, 0x03, 0x02, 0x01,
2381 0x01, 0x02, 0x03, 0x04,
2382 0x05, 0x06, 0x07, 0x08,
2385 0x01, 0x00, 0x00, 0x00,
2389 QuicEncryptedPacket
encrypted(AsChars(packet
), arraysize(packet
), false);
2390 EXPECT_TRUE(framer_
.ProcessPacket(encrypted
));
2392 EXPECT_EQ(QUIC_NO_ERROR
, framer_
.error());
2393 ASSERT_TRUE(visitor_
.header_
.get());
2394 EXPECT_TRUE(CheckDecryption(encrypted
, !kIncludeVersion
));
2396 EXPECT_EQ(UINT64_C(0x01020304), visitor_
.rst_stream_frame_
.stream_id
);
2397 EXPECT_EQ(0x01, visitor_
.rst_stream_frame_
.error_code
);
2398 EXPECT_EQ(UINT64_C(0x0807060504030201),
2399 visitor_
.rst_stream_frame_
.byte_offset
);
2401 // Now test framing boundaries.
2402 for (size_t i
= kQuicFrameTypeSize
; i
< QuicFramer::GetRstStreamFrameSize();
2404 string expected_error
;
2405 if (i
< kQuicFrameTypeSize
+ kQuicMaxStreamIdSize
) {
2406 expected_error
= "Unable to read stream_id.";
2407 } else if (i
< kQuicFrameTypeSize
+ kQuicMaxStreamIdSize
+
2408 kQuicMaxStreamOffsetSize
) {
2409 expected_error
= "Unable to read rst stream sent byte offset.";
2410 } else if (i
< kQuicFrameTypeSize
+ kQuicMaxStreamIdSize
+
2411 kQuicMaxStreamOffsetSize
+ kQuicErrorCodeSize
) {
2412 expected_error
= "Unable to read rst stream error code.";
2414 CheckProcessingFails(
2416 i
+ GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID
, !kIncludeVersion
,
2417 PACKET_6BYTE_PACKET_NUMBER
, NOT_IN_FEC_GROUP
),
2418 expected_error
, QUIC_INVALID_RST_STREAM_DATA
);
2422 TEST_P(QuicFramerTest
, ConnectionCloseFrame
) {
2424 unsigned char packet
[] = {
2425 // public flags (8 byte connection_id)
2428 0x10, 0x32, 0x54, 0x76,
2429 0x98, 0xBA, 0xDC, 0xFE,
2431 0xBC, 0x9A, 0x78, 0x56,
2436 // frame type (connection close frame)
2439 0x11, 0x00, 0x00, 0x00,
2441 // error details length
2451 QuicEncryptedPacket
encrypted(AsChars(packet
), arraysize(packet
), false);
2452 EXPECT_TRUE(framer_
.ProcessPacket(encrypted
));
2454 EXPECT_EQ(QUIC_NO_ERROR
, framer_
.error());
2455 ASSERT_TRUE(visitor_
.header_
.get());
2456 EXPECT_TRUE(CheckDecryption(encrypted
, !kIncludeVersion
));
2458 EXPECT_EQ(0u, visitor_
.stream_frames_
.size());
2460 EXPECT_EQ(0x11, visitor_
.connection_close_frame_
.error_code
);
2461 EXPECT_EQ("because I can", visitor_
.connection_close_frame_
.error_details
);
2463 ASSERT_EQ(0u, visitor_
.ack_frames_
.size());
2465 // Now test framing boundaries.
2466 for (size_t i
= kQuicFrameTypeSize
;
2467 i
< QuicFramer::GetMinConnectionCloseFrameSize(); ++i
) {
2468 string expected_error
;
2469 if (i
< kQuicFrameTypeSize
+ kQuicErrorCodeSize
) {
2470 expected_error
= "Unable to read connection close error code.";
2472 expected_error
= "Unable to read connection close error details.";
2474 CheckProcessingFails(
2476 i
+ GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID
, !kIncludeVersion
,
2477 PACKET_6BYTE_PACKET_NUMBER
, NOT_IN_FEC_GROUP
),
2478 expected_error
, QUIC_INVALID_CONNECTION_CLOSE_DATA
);
2482 TEST_P(QuicFramerTest
, GoAwayFrame
) {
2484 unsigned char packet
[] = {
2485 // public flags (8 byte connection_id)
2488 0x10, 0x32, 0x54, 0x76,
2489 0x98, 0xBA, 0xDC, 0xFE,
2491 0xBC, 0x9A, 0x78, 0x56,
2496 // frame type (go away frame)
2499 0x09, 0x00, 0x00, 0x00,
2501 0x04, 0x03, 0x02, 0x01,
2502 // error details length
2512 QuicEncryptedPacket
encrypted(AsChars(packet
), arraysize(packet
), false);
2513 EXPECT_TRUE(framer_
.ProcessPacket(encrypted
));
2515 EXPECT_EQ(QUIC_NO_ERROR
, framer_
.error());
2516 ASSERT_TRUE(visitor_
.header_
.get());
2517 EXPECT_TRUE(CheckDecryption(encrypted
, !kIncludeVersion
));
2519 EXPECT_EQ(UINT64_C(0x01020304), visitor_
.goaway_frame_
.last_good_stream_id
);
2520 EXPECT_EQ(0x9, visitor_
.goaway_frame_
.error_code
);
2521 EXPECT_EQ("because I can", visitor_
.goaway_frame_
.reason_phrase
);
2523 const size_t reason_size
= arraysize("because I can") - 1;
2524 // Now test framing boundaries.
2525 for (size_t i
= kQuicFrameTypeSize
;
2526 i
< QuicFramer::GetMinGoAwayFrameSize() + reason_size
; ++i
) {
2527 string expected_error
;
2528 if (i
< kQuicFrameTypeSize
+ kQuicErrorCodeSize
) {
2529 expected_error
= "Unable to read go away error code.";
2530 } else if (i
< kQuicFrameTypeSize
+ kQuicErrorCodeSize
+
2531 kQuicMaxStreamIdSize
) {
2532 expected_error
= "Unable to read last good stream id.";
2534 expected_error
= "Unable to read goaway reason.";
2536 CheckProcessingFails(
2538 i
+ GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID
, !kIncludeVersion
,
2539 PACKET_6BYTE_PACKET_NUMBER
, NOT_IN_FEC_GROUP
),
2540 expected_error
, QUIC_INVALID_GOAWAY_DATA
);
2544 TEST_P(QuicFramerTest
, WindowUpdateFrame
) {
2546 unsigned char packet
[] = {
2547 // public flags (8 byte connection_id)
2550 0x10, 0x32, 0x54, 0x76,
2551 0x98, 0xBA, 0xDC, 0xFE,
2553 0xBC, 0x9A, 0x78, 0x56,
2558 // frame type (window update frame)
2561 0x04, 0x03, 0x02, 0x01,
2563 0x05, 0x06, 0x07, 0x08,
2564 0x09, 0x0a, 0x0b, 0x0c,
2568 QuicEncryptedPacket
encrypted(AsChars(packet
), arraysize(packet
), false);
2570 EXPECT_TRUE(framer_
.ProcessPacket(encrypted
));
2572 EXPECT_EQ(QUIC_NO_ERROR
, framer_
.error());
2573 ASSERT_TRUE(visitor_
.header_
.get());
2574 EXPECT_TRUE(CheckDecryption(encrypted
, !kIncludeVersion
));
2576 EXPECT_EQ(UINT64_C(0x01020304), visitor_
.window_update_frame_
.stream_id
);
2577 EXPECT_EQ(UINT64_C(0x0c0b0a0908070605),
2578 visitor_
.window_update_frame_
.byte_offset
);
2580 // Now test framing boundaries.
2581 for (size_t i
= kQuicFrameTypeSize
;
2582 i
< QuicFramer::GetWindowUpdateFrameSize(); ++i
) {
2583 string expected_error
;
2584 if (i
< kQuicFrameTypeSize
+ kQuicMaxStreamIdSize
) {
2585 expected_error
= "Unable to read stream_id.";
2587 expected_error
= "Unable to read window byte_offset.";
2589 CheckProcessingFails(
2591 i
+ GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID
, !kIncludeVersion
,
2592 PACKET_6BYTE_PACKET_NUMBER
, NOT_IN_FEC_GROUP
),
2593 expected_error
, QUIC_INVALID_WINDOW_UPDATE_DATA
);
2597 TEST_P(QuicFramerTest
, BlockedFrame
) {
2599 unsigned char packet
[] = {
2600 // public flags (8 byte connection_id)
2603 0x10, 0x32, 0x54, 0x76,
2604 0x98, 0xBA, 0xDC, 0xFE,
2606 0xBC, 0x9A, 0x78, 0x56,
2611 // frame type (blocked frame)
2614 0x04, 0x03, 0x02, 0x01,
2618 QuicEncryptedPacket
encrypted(AsChars(packet
), arraysize(packet
), false);
2620 EXPECT_TRUE(framer_
.ProcessPacket(encrypted
));
2622 EXPECT_EQ(QUIC_NO_ERROR
, framer_
.error());
2623 ASSERT_TRUE(visitor_
.header_
.get());
2624 EXPECT_TRUE(CheckDecryption(encrypted
, !kIncludeVersion
));
2626 EXPECT_EQ(UINT64_C(0x01020304), visitor_
.blocked_frame_
.stream_id
);
2628 // Now test framing boundaries.
2629 for (size_t i
= kQuicFrameTypeSize
; i
< QuicFramer::GetBlockedFrameSize();
2631 string expected_error
= "Unable to read stream_id.";
2632 CheckProcessingFails(
2634 i
+ GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID
, !kIncludeVersion
,
2635 PACKET_6BYTE_PACKET_NUMBER
, NOT_IN_FEC_GROUP
),
2636 expected_error
, QUIC_INVALID_BLOCKED_DATA
);
2640 TEST_P(QuicFramerTest
, PingFrame
) {
2642 unsigned char packet
[] = {
2643 // public flags (8 byte connection_id)
2646 0x10, 0x32, 0x54, 0x76,
2647 0x98, 0xBA, 0xDC, 0xFE,
2649 0xBC, 0x9A, 0x78, 0x56,
2654 // frame type (ping frame)
2659 QuicEncryptedPacket
encrypted(AsChars(packet
), arraysize(packet
), false);
2660 EXPECT_TRUE(framer_
.ProcessPacket(encrypted
));
2662 EXPECT_EQ(QUIC_NO_ERROR
, framer_
.error());
2663 ASSERT_TRUE(visitor_
.header_
.get());
2664 EXPECT_TRUE(CheckDecryption(encrypted
, !kIncludeVersion
));
2666 EXPECT_EQ(1u, visitor_
.ping_frames_
.size());
2668 // No need to check the PING frame boundaries because it has no payload.
2671 TEST_P(QuicFramerTest
, PublicResetPacket
) {
2673 unsigned char packet
[] = {
2674 // public flags (public reset, 8 byte connection_id)
2677 0x10, 0x32, 0x54, 0x76,
2678 0x98, 0xBA, 0xDC, 0xFE,
2679 // message tag (kPRST)
2681 // num_entries (2) + padding
2682 0x02, 0x00, 0x00, 0x00,
2686 0x08, 0x00, 0x00, 0x00,
2690 0x10, 0x00, 0x00, 0x00,
2692 0x89, 0x67, 0x45, 0x23,
2693 0x01, 0xEF, 0xCD, 0xAB,
2694 // rejected packet number
2695 0xBC, 0x9A, 0x78, 0x56,
2696 0x34, 0x12, 0x00, 0x00,
2700 QuicEncryptedPacket
encrypted(AsChars(packet
), arraysize(packet
), false);
2701 EXPECT_TRUE(framer_
.ProcessPacket(encrypted
));
2702 ASSERT_EQ(QUIC_NO_ERROR
, framer_
.error());
2703 ASSERT_TRUE(visitor_
.public_reset_packet_
.get());
2704 EXPECT_EQ(UINT64_C(0xFEDCBA9876543210),
2705 visitor_
.public_reset_packet_
->public_header
.connection_id
);
2706 EXPECT_TRUE(visitor_
.public_reset_packet_
->public_header
.reset_flag
);
2707 EXPECT_FALSE(visitor_
.public_reset_packet_
->public_header
.version_flag
);
2708 EXPECT_EQ(UINT64_C(0xABCDEF0123456789),
2709 visitor_
.public_reset_packet_
->nonce_proof
);
2710 EXPECT_EQ(UINT64_C(0x123456789ABC),
2711 visitor_
.public_reset_packet_
->rejected_packet_number
);
2713 visitor_
.public_reset_packet_
->client_address
.address().empty());
2715 // Now test framing boundaries.
2716 for (size_t i
= 0; i
< arraysize(packet
); ++i
) {
2717 string expected_error
;
2718 DVLOG(1) << "iteration: " << i
;
2719 if (i
< kConnectionIdOffset
) {
2720 expected_error
= "Unable to read public flags.";
2721 CheckProcessingFails(packet
, i
, expected_error
,
2722 QUIC_INVALID_PACKET_HEADER
);
2723 } else if (i
< kPublicResetPacketMessageTagOffset
) {
2724 expected_error
= "Unable to read ConnectionId.";
2725 CheckProcessingFails(packet
, i
, expected_error
,
2726 QUIC_INVALID_PACKET_HEADER
);
2728 expected_error
= "Unable to read reset message.";
2729 CheckProcessingFails(packet
, i
, expected_error
,
2730 QUIC_INVALID_PUBLIC_RST_PACKET
);
2735 TEST_P(QuicFramerTest
, PublicResetPacketWithTrailingJunk
) {
2737 unsigned char packet
[] = {
2738 // public flags (public reset, 8 byte connection_id)
2741 0x10, 0x32, 0x54, 0x76,
2742 0x98, 0xBA, 0xDC, 0xFE,
2743 // message tag (kPRST)
2745 // num_entries (2) + padding
2746 0x02, 0x00, 0x00, 0x00,
2750 0x08, 0x00, 0x00, 0x00,
2754 0x10, 0x00, 0x00, 0x00,
2756 0x89, 0x67, 0x45, 0x23,
2757 0x01, 0xEF, 0xCD, 0xAB,
2758 // rejected packet number
2759 0xBC, 0x9A, 0x78, 0x56,
2760 0x34, 0x12, 0x00, 0x00,
2766 string expected_error
= "Unable to read reset message.";
2767 CheckProcessingFails(packet
, arraysize(packet
), expected_error
,
2768 QUIC_INVALID_PUBLIC_RST_PACKET
);
2771 TEST_P(QuicFramerTest
, PublicResetPacketWithClientAddress
) {
2773 unsigned char packet
[] = {
2774 // public flags (public reset, 8 byte connection_id)
2777 0x10, 0x32, 0x54, 0x76,
2778 0x98, 0xBA, 0xDC, 0xFE,
2779 // message tag (kPRST)
2781 // num_entries (3) + padding
2782 0x03, 0x00, 0x00, 0x00,
2786 0x08, 0x00, 0x00, 0x00,
2790 0x10, 0x00, 0x00, 0x00,
2794 0x18, 0x00, 0x00, 0x00,
2796 0x89, 0x67, 0x45, 0x23,
2797 0x01, 0xEF, 0xCD, 0xAB,
2798 // rejected packet number
2799 0xBC, 0x9A, 0x78, 0x56,
2800 0x34, 0x12, 0x00, 0x00,
2801 // client address: 4.31.198.44:443
2803 0x04, 0x1F, 0xC6, 0x2C,
2808 QuicEncryptedPacket
encrypted(AsChars(packet
), arraysize(packet
), false);
2809 EXPECT_TRUE(framer_
.ProcessPacket(encrypted
));
2810 ASSERT_EQ(QUIC_NO_ERROR
, framer_
.error());
2811 ASSERT_TRUE(visitor_
.public_reset_packet_
.get());
2812 EXPECT_EQ(UINT64_C(0xFEDCBA9876543210),
2813 visitor_
.public_reset_packet_
->public_header
.connection_id
);
2814 EXPECT_TRUE(visitor_
.public_reset_packet_
->public_header
.reset_flag
);
2815 EXPECT_FALSE(visitor_
.public_reset_packet_
->public_header
.version_flag
);
2816 EXPECT_EQ(UINT64_C(0xABCDEF0123456789),
2817 visitor_
.public_reset_packet_
->nonce_proof
);
2818 EXPECT_EQ(UINT64_C(0x123456789ABC),
2819 visitor_
.public_reset_packet_
->rejected_packet_number
);
2820 EXPECT_EQ("4.31.198.44",
2821 IPAddressToString(visitor_
.public_reset_packet_
->
2822 client_address
.address()));
2823 EXPECT_EQ(443, visitor_
.public_reset_packet_
->client_address
.port());
2825 // Now test framing boundaries.
2826 for (size_t i
= 0; i
< arraysize(packet
); ++i
) {
2827 string expected_error
;
2828 DVLOG(1) << "iteration: " << i
;
2829 if (i
< kConnectionIdOffset
) {
2830 expected_error
= "Unable to read public flags.";
2831 CheckProcessingFails(packet
, i
, expected_error
,
2832 QUIC_INVALID_PACKET_HEADER
);
2833 } else if (i
< kPublicResetPacketMessageTagOffset
) {
2834 expected_error
= "Unable to read ConnectionId.";
2835 CheckProcessingFails(packet
, i
, expected_error
,
2836 QUIC_INVALID_PACKET_HEADER
);
2838 expected_error
= "Unable to read reset message.";
2839 CheckProcessingFails(packet
, i
, expected_error
,
2840 QUIC_INVALID_PUBLIC_RST_PACKET
);
2845 TEST_P(QuicFramerTest
, VersionNegotiationPacket
) {
2847 unsigned char packet
[] = {
2848 // public flags (version, 8 byte connection_id)
2851 0x10, 0x32, 0x54, 0x76,
2852 0x98, 0xBA, 0xDC, 0xFE,
2854 'Q', '0', GetQuicVersionDigitTens(), GetQuicVersionDigitOnes(),
2859 QuicFramerPeer::SetPerspective(&framer_
, Perspective::IS_CLIENT
);
2861 QuicEncryptedPacket
encrypted(AsChars(packet
), arraysize(packet
), false);
2862 EXPECT_TRUE(framer_
.ProcessPacket(encrypted
));
2863 ASSERT_EQ(QUIC_NO_ERROR
, framer_
.error());
2864 ASSERT_TRUE(visitor_
.version_negotiation_packet_
.get());
2865 EXPECT_EQ(2u, visitor_
.version_negotiation_packet_
->versions
.size());
2866 EXPECT_EQ(GetParam(), visitor_
.version_negotiation_packet_
->versions
[0]);
2868 for (size_t i
= 0; i
<= kPublicFlagsSize
+ PACKET_8BYTE_CONNECTION_ID
; ++i
) {
2869 string expected_error
;
2870 QuicErrorCode error_code
= QUIC_INVALID_PACKET_HEADER
;
2871 if (i
< kConnectionIdOffset
) {
2872 expected_error
= "Unable to read public flags.";
2873 } else if (i
< kVersionOffset
) {
2874 expected_error
= "Unable to read ConnectionId.";
2876 expected_error
= "Unable to read supported version in negotiation.";
2877 error_code
= QUIC_INVALID_VERSION_NEGOTIATION_PACKET
;
2879 CheckProcessingFails(packet
, i
, expected_error
, error_code
);
2883 TEST_P(QuicFramerTest
, FecPacket
) {
2885 unsigned char packet
[] = {
2886 // public flags (8 byte connection_id)
2889 0x10, 0x32, 0x54, 0x76,
2890 0x98, 0xBA, 0xDC, 0xFE,
2892 0xBC, 0x9A, 0x78, 0x56,
2894 // private flags (fec group & FEC)
2896 // first fec protected packet offset
2907 QuicEncryptedPacket
encrypted(AsChars(packet
), arraysize(packet
), false);
2908 EXPECT_TRUE(framer_
.ProcessPacket(encrypted
));
2910 EXPECT_EQ(QUIC_NO_ERROR
, framer_
.error());
2911 ASSERT_TRUE(visitor_
.header_
.get());
2912 EXPECT_TRUE(CheckDecryption(encrypted
, !kIncludeVersion
));
2914 EXPECT_EQ(0u, visitor_
.stream_frames_
.size());
2915 EXPECT_EQ(0u, visitor_
.ack_frames_
.size());
2916 ASSERT_EQ(1, visitor_
.fec_count_
);
2917 const QuicFecData
& fec_data
= *visitor_
.fec_data_
[0];
2918 EXPECT_EQ(UINT64_C(0x0123456789ABB), fec_data
.fec_group
);
2919 EXPECT_EQ("abcdefghijklmnop", fec_data
.redundancy
);
2922 TEST_P(QuicFramerTest
, BuildPaddingFramePacket
) {
2923 QuicPacketHeader header
;
2924 header
.public_header
.connection_id
= UINT64_C(0xFEDCBA9876543210);
2925 header
.public_header
.reset_flag
= false;
2926 header
.public_header
.version_flag
= false;
2927 header
.fec_flag
= false;
2928 header
.entropy_flag
= false;
2929 header
.packet_packet_number
= UINT64_C(0x123456789ABC);
2930 header
.fec_group
= 0;
2932 QuicPaddingFrame padding_frame
;
2935 frames
.push_back(QuicFrame(&padding_frame
));
2938 unsigned char packet
[kMaxPacketSize
] = {
2939 // public flags (8 byte connection_id)
2942 0x10, 0x32, 0x54, 0x76,
2943 0x98, 0xBA, 0xDC, 0xFE,
2945 0xBC, 0x9A, 0x78, 0x56,
2950 // frame type (padding frame)
2952 0x00, 0x00, 0x00, 0x00
2956 uint64 header_size
=
2957 GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID
, !kIncludeVersion
,
2958 PACKET_6BYTE_PACKET_NUMBER
, NOT_IN_FEC_GROUP
);
2959 memset(packet
+ header_size
+ 1, 0x00, kMaxPacketSize
- header_size
- 1);
2961 scoped_ptr
<QuicPacket
> data(BuildDataPacket(header
, frames
));
2962 ASSERT_TRUE(data
!= nullptr);
2964 test::CompareCharArraysWithHexError("constructed packet",
2965 data
->data(), data
->length(),
2970 TEST_P(QuicFramerTest
, Build4ByteSequenceNumberPaddingFramePacket
) {
2971 QuicPacketHeader header
;
2972 header
.public_header
.connection_id
= UINT64_C(0xFEDCBA9876543210);
2973 header
.public_header
.reset_flag
= false;
2974 header
.public_header
.version_flag
= false;
2975 header
.fec_flag
= false;
2976 header
.entropy_flag
= false;
2977 header
.public_header
.packet_number_length
= PACKET_4BYTE_PACKET_NUMBER
;
2978 header
.packet_packet_number
= UINT64_C(0x123456789ABC);
2979 header
.fec_group
= 0;
2981 QuicPaddingFrame padding_frame
;
2984 frames
.push_back(QuicFrame(&padding_frame
));
2987 unsigned char packet
[kMaxPacketSize
] = {
2988 // public flags (8 byte connection_id and 4 byte packet number)
2991 0x10, 0x32, 0x54, 0x76,
2992 0x98, 0xBA, 0xDC, 0xFE,
2994 0xBC, 0x9A, 0x78, 0x56,
2998 // frame type (padding frame)
3000 0x00, 0x00, 0x00, 0x00
3004 uint64 header_size
=
3005 GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID
, !kIncludeVersion
,
3006 PACKET_4BYTE_PACKET_NUMBER
, NOT_IN_FEC_GROUP
);
3007 memset(packet
+ header_size
+ 1, 0x00, kMaxPacketSize
- header_size
- 1);
3009 scoped_ptr
<QuicPacket
> data(BuildDataPacket(header
, frames
));
3010 ASSERT_TRUE(data
!= nullptr);
3012 test::CompareCharArraysWithHexError("constructed packet",
3013 data
->data(), data
->length(),
3018 TEST_P(QuicFramerTest
, Build2ByteSequenceNumberPaddingFramePacket
) {
3019 QuicPacketHeader header
;
3020 header
.public_header
.connection_id
= UINT64_C(0xFEDCBA9876543210);
3021 header
.public_header
.reset_flag
= false;
3022 header
.public_header
.version_flag
= false;
3023 header
.fec_flag
= false;
3024 header
.entropy_flag
= false;
3025 header
.public_header
.packet_number_length
= PACKET_2BYTE_PACKET_NUMBER
;
3026 header
.packet_packet_number
= UINT64_C(0x123456789ABC);
3027 header
.fec_group
= 0;
3029 QuicPaddingFrame padding_frame
;
3032 frames
.push_back(QuicFrame(&padding_frame
));
3035 unsigned char packet
[kMaxPacketSize
] = {
3036 // public flags (8 byte connection_id and 2 byte packet number)
3039 0x10, 0x32, 0x54, 0x76,
3040 0x98, 0xBA, 0xDC, 0xFE,
3046 // frame type (padding frame)
3048 0x00, 0x00, 0x00, 0x00
3052 uint64 header_size
=
3053 GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID
, !kIncludeVersion
,
3054 PACKET_2BYTE_PACKET_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
, Build1ByteSequenceNumberPaddingFramePacket
) {
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
= false;
3073 header
.public_header
.packet_number_length
= PACKET_1BYTE_PACKET_NUMBER
;
3074 header
.packet_packet_number
= UINT64_C(0x123456789ABC);
3075 header
.fec_group
= 0;
3077 QuicPaddingFrame padding_frame
;
3080 frames
.push_back(QuicFrame(&padding_frame
));
3083 unsigned char packet
[kMaxPacketSize
] = {
3084 // public flags (8 byte connection_id and 1 byte packet number)
3087 0x10, 0x32, 0x54, 0x76,
3088 0x98, 0xBA, 0xDC, 0xFE,
3094 // frame type (padding frame)
3096 0x00, 0x00, 0x00, 0x00
3100 uint64 header_size
=
3101 GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID
, !kIncludeVersion
,
3102 PACKET_1BYTE_PACKET_NUMBER
, NOT_IN_FEC_GROUP
);
3103 memset(packet
+ header_size
+ 1, 0x00, kMaxPacketSize
- header_size
- 1);
3105 scoped_ptr
<QuicPacket
> data(BuildDataPacket(header
, frames
));
3106 ASSERT_TRUE(data
!= nullptr);
3108 test::CompareCharArraysWithHexError("constructed packet",
3109 data
->data(), data
->length(),
3114 TEST_P(QuicFramerTest
, BuildStreamFramePacket
) {
3115 QuicPacketHeader header
;
3116 header
.public_header
.connection_id
= UINT64_C(0xFEDCBA9876543210);
3117 header
.public_header
.reset_flag
= false;
3118 header
.public_header
.version_flag
= false;
3119 header
.fec_flag
= false;
3120 header
.entropy_flag
= true;
3121 header
.packet_packet_number
= UINT64_C(0x77123456789ABC);
3122 header
.fec_group
= 0;
3124 QuicStreamFrame
stream_frame(0x01020304, true, UINT64_C(0xBA98FEDC32107654),
3125 StringPiece("hello world!"));
3128 frames
.push_back(QuicFrame(&stream_frame
));
3131 unsigned char packet
[] = {
3132 // public flags (8 byte connection_id)
3135 0x10, 0x32, 0x54, 0x76,
3136 0x98, 0xBA, 0xDC, 0xFE,
3138 0xBC, 0x9A, 0x78, 0x56,
3140 // private flags (entropy)
3143 // frame type (stream frame with fin and no length)
3146 0x04, 0x03, 0x02, 0x01,
3148 0x54, 0x76, 0x10, 0x32,
3149 0xDC, 0xFE, 0x98, 0xBA,
3157 scoped_ptr
<QuicPacket
> data(BuildDataPacket(header
, frames
));
3158 ASSERT_TRUE(data
!= nullptr);
3160 test::CompareCharArraysWithHexError("constructed packet",
3161 data
->data(), data
->length(),
3162 AsChars(packet
), arraysize(packet
));
3165 TEST_P(QuicFramerTest
, BuildStreamFramePacketInFecGroup
) {
3166 QuicPacketHeader header
;
3167 header
.public_header
.connection_id
= UINT64_C(0xFEDCBA9876543210);
3168 header
.public_header
.reset_flag
= false;
3169 header
.public_header
.version_flag
= false;
3170 header
.fec_flag
= false;
3171 header
.entropy_flag
= true;
3172 header
.packet_packet_number
= UINT64_C(0x77123456789ABC);
3173 header
.is_in_fec_group
= IN_FEC_GROUP
;
3174 header
.fec_group
= UINT64_C(0x77123456789ABC);
3176 QuicStreamFrame
stream_frame(0x01020304, true, UINT64_C(0xBA98FEDC32107654),
3177 StringPiece("hello world!"));
3180 frames
.push_back(QuicFrame(&stream_frame
));
3182 unsigned char packet
[] = {
3183 // public flags (8 byte connection_id)
3186 0x10, 0x32, 0x54, 0x76, 0x98, 0xBA, 0xDC, 0xFE,
3188 0xBC, 0x9A, 0x78, 0x56, 0x34, 0x12,
3189 // private flags (entropy, is_in_fec_group)
3193 // frame type (stream frame with fin and data length field)
3196 0x04, 0x03, 0x02, 0x01,
3198 0x54, 0x76, 0x10, 0x32, 0xDC, 0xFE, 0x98, 0xBA,
3199 // data length (since packet is in an FEC group)
3202 'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', '!',
3206 scoped_ptr
<QuicPacket
> data(BuildDataPacket(header
, frames
));
3207 ASSERT_TRUE(data
!= nullptr);
3209 test::CompareCharArraysWithHexError("constructed packet",
3210 data
->data(), data
->length(),
3211 AsChars(packet
), arraysize(packet
));
3214 TEST_P(QuicFramerTest
, BuildStreamFramePacketWithVersionFlag
) {
3215 QuicPacketHeader header
;
3216 header
.public_header
.connection_id
= UINT64_C(0xFEDCBA9876543210);
3217 header
.public_header
.reset_flag
= false;
3218 header
.public_header
.version_flag
= true;
3219 header
.fec_flag
= false;
3220 header
.entropy_flag
= true;
3221 header
.packet_packet_number
= UINT64_C(0x77123456789ABC);
3222 header
.fec_group
= 0;
3224 QuicStreamFrame
stream_frame(0x01020304, true, UINT64_C(0xBA98FEDC32107654),
3225 StringPiece("hello world!"));
3228 frames
.push_back(QuicFrame(&stream_frame
));
3231 unsigned char packet
[] = {
3232 // public flags (version, 8 byte connection_id)
3246 GetQuicVersionDigitTens(),
3247 GetQuicVersionDigitOnes(),
3255 // private flags (entropy)
3258 // frame type (stream frame with fin and no length)
3290 QuicFramerPeer::SetPerspective(&framer_
, Perspective::IS_CLIENT
);
3291 scoped_ptr
<QuicPacket
> data(BuildDataPacket(header
, frames
));
3292 ASSERT_TRUE(data
!= nullptr);
3294 test::CompareCharArraysWithHexError("constructed packet",
3295 data
->data(), data
->length(),
3296 AsChars(packet
), arraysize(packet
));
3299 TEST_P(QuicFramerTest
, BuildVersionNegotiationPacket
) {
3300 QuicPacketPublicHeader header
;
3301 header
.connection_id
= UINT64_C(0xFEDCBA9876543210);
3302 header
.reset_flag
= false;
3303 header
.version_flag
= true;
3306 unsigned char packet
[] = {
3307 // public flags (version, 8 byte connection_id)
3321 GetQuicVersionDigitTens(),
3322 GetQuicVersionDigitOnes(),
3326 QuicVersionVector versions
;
3327 versions
.push_back(GetParam());
3328 scoped_ptr
<QuicEncryptedPacket
> data(
3329 framer_
.BuildVersionNegotiationPacket(header
, versions
));
3331 test::CompareCharArraysWithHexError("constructed packet", data
->data(),
3332 data
->length(), AsChars(packet
),
3336 TEST_P(QuicFramerTest
, BuildAckFramePacket
) {
3337 QuicPacketHeader header
;
3338 header
.public_header
.connection_id
= UINT64_C(0xFEDCBA9876543210);
3339 header
.public_header
.reset_flag
= false;
3340 header
.public_header
.version_flag
= false;
3341 header
.fec_flag
= false;
3342 header
.entropy_flag
= true;
3343 header
.packet_packet_number
= UINT64_C(0x770123456789AA8);
3344 header
.fec_group
= 0;
3346 QuicAckFrame ack_frame
;
3347 ack_frame
.entropy_hash
= 0x43;
3348 ack_frame
.largest_observed
= UINT64_C(0x770123456789ABF);
3349 ack_frame
.delta_time_largest_observed
= QuicTime::Delta::Zero();
3350 ack_frame
.missing_packets
.insert(UINT64_C(0x770123456789ABE));
3353 frames
.push_back(QuicFrame(&ack_frame
));
3356 unsigned char packet
[] = {
3357 // public flags (8 byte connection_id)
3360 0x10, 0x32, 0x54, 0x76, 0x98, 0xBA, 0xDC, 0xFE,
3362 0xA8, 0x9A, 0x78, 0x56, 0x34, 0x12,
3363 // private flags (entropy)
3366 // frame type (ack frame)
3367 // (has nacks, not truncated, 6 byte largest observed, 1 byte delta)
3369 // entropy hash of all received packets.
3371 // largest observed packet number
3372 0xBF, 0x9A, 0x78, 0x56, 0x34, 0x12,
3375 // num received packets.
3377 // num missing packet ranges
3379 // missing packet delta
3381 // 0 more missing packets in range.
3383 // 0 revived packets.
3388 scoped_ptr
<QuicPacket
> data(BuildDataPacket(header
, frames
));
3389 ASSERT_TRUE(data
!= nullptr);
3391 test::CompareCharArraysWithHexError("constructed packet",
3392 data
->data(), data
->length(),
3393 AsChars(packet
), arraysize(packet
));
3396 // TODO(jri): Add test for tuncated packets in which the original ack frame had
3397 // revived packets. (In both the large and small packet cases below).
3399 TEST_P(QuicFramerTest
, BuildTruncatedAckFrameLargePacket
) {
3400 QuicPacketHeader header
;
3401 header
.public_header
.connection_id
= UINT64_C(0xFEDCBA9876543210);
3402 header
.public_header
.reset_flag
= false;
3403 header
.public_header
.version_flag
= false;
3404 header
.fec_flag
= false;
3405 header
.entropy_flag
= true;
3406 header
.packet_packet_number
= UINT64_C(0x770123456789AA8);
3407 header
.fec_group
= 0;
3409 QuicAckFrame ack_frame
;
3410 // This entropy hash is different from what shows up in the packet below,
3411 // since entropy is recomputed by the framer on ack truncation (by
3412 // TestEntropyCalculator for this test.)
3413 ack_frame
.entropy_hash
= 0x43;
3414 ack_frame
.largest_observed
= 2 * 300;
3415 ack_frame
.delta_time_largest_observed
= QuicTime::Delta::Zero();
3416 for (size_t i
= 1; i
< 2 * 300; i
+= 2) {
3417 ack_frame
.missing_packets
.insert(i
);
3421 frames
.push_back(QuicFrame(&ack_frame
));
3424 unsigned char packet
[] = {
3425 // public flags (8 byte connection_id)
3428 0x10, 0x32, 0x54, 0x76, 0x98, 0xBA, 0xDC, 0xFE,
3430 0xA8, 0x9A, 0x78, 0x56, 0x34, 0x12,
3431 // private flags (entropy)
3434 // frame type (ack frame)
3435 // (has nacks, is truncated, 2 byte largest observed, 1 byte delta)
3437 // entropy hash of all received packets, set to 1 by TestEntropyCalculator
3438 // since ack is truncated.
3440 // 2-byte largest observed packet number.
3441 // Expected to be 510 (0x1FE), since only 255 nack ranges can fit.
3445 // num missing packet ranges (limited to 255 by size of this field).
3447 // {missing packet delta, further missing packets in range}
3448 // 6 nack ranges x 42 + 3 nack ranges
3449 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3450 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3451 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3452 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3453 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3454 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3455 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3456 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3457 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3458 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3460 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3461 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3462 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3463 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3464 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3465 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3466 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3467 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3468 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3469 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3471 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3472 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3473 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3474 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3475 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3476 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3477 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3478 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3479 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3480 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3482 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3483 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3484 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3485 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3486 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3487 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3488 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3489 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3490 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3491 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3493 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3494 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3495 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3497 // 0 revived packets.
3502 scoped_ptr
<QuicPacket
> data(BuildDataPacket(header
, frames
));
3503 ASSERT_TRUE(data
!= nullptr);
3505 test::CompareCharArraysWithHexError("constructed packet",
3506 data
->data(), data
->length(),
3507 AsChars(packet
), arraysize(packet
));
3510 TEST_P(QuicFramerTest
, BuildTruncatedAckFrameSmallPacket
) {
3511 QuicPacketHeader header
;
3512 header
.public_header
.connection_id
= UINT64_C(0xFEDCBA9876543210);
3513 header
.public_header
.reset_flag
= false;
3514 header
.public_header
.version_flag
= false;
3515 header
.fec_flag
= false;
3516 header
.entropy_flag
= true;
3517 header
.packet_packet_number
= UINT64_C(0x770123456789AA8);
3518 header
.fec_group
= 0;
3520 QuicAckFrame ack_frame
;
3521 // This entropy hash is different from what shows up in the packet below,
3522 // since entropy is recomputed by the framer on ack truncation (by
3523 // TestEntropyCalculator for this test.)
3524 ack_frame
.entropy_hash
= 0x43;
3525 ack_frame
.largest_observed
= 2 * 300;
3526 ack_frame
.delta_time_largest_observed
= QuicTime::Delta::Zero();
3527 for (size_t i
= 1; i
< 2 * 300; i
+= 2) {
3528 ack_frame
.missing_packets
.insert(i
);
3532 frames
.push_back(QuicFrame(&ack_frame
));
3535 unsigned char packet
[] = {
3536 // public flags (8 byte connection_id)
3539 0x10, 0x32, 0x54, 0x76, 0x98, 0xBA, 0xDC, 0xFE,
3541 0xA8, 0x9A, 0x78, 0x56, 0x34, 0x12,
3542 // private flags (entropy)
3545 // frame type (ack frame)
3546 // (has nacks, is truncated, 2 byte largest observed, 1 byte delta)
3548 // entropy hash of all received packets, set to 1 by TestEntropyCalculator
3549 // since ack is truncated.
3551 // 2-byte largest observed packet number.
3552 // Expected to be 12 (0x0C), since only 6 nack ranges can fit.
3556 // num missing packet ranges (limited to 6 by packet size of 37).
3558 // {missing packet delta, further missing packets in range}
3560 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
3561 // 0 revived packets.
3566 scoped_ptr
<QuicPacket
> data(BuildDataPacket(header
, frames
, 37u));
3567 ASSERT_TRUE(data
!= nullptr);
3568 // Expect 1 byte unused since at least 2 bytes are needed to fit more nacks.
3569 EXPECT_EQ(36u, data
->length());
3570 test::CompareCharArraysWithHexError("constructed packet",
3571 data
->data(), data
->length(),
3572 AsChars(packet
), arraysize(packet
));
3575 TEST_P(QuicFramerTest
, BuildStopWaitingPacket
) {
3576 QuicPacketHeader header
;
3577 header
.public_header
.connection_id
= UINT64_C(0xFEDCBA9876543210);
3578 header
.public_header
.reset_flag
= false;
3579 header
.public_header
.version_flag
= false;
3580 header
.fec_flag
= false;
3581 header
.entropy_flag
= true;
3582 header
.packet_packet_number
= UINT64_C(0x770123456789AA8);
3583 header
.fec_group
= 0;
3585 QuicStopWaitingFrame stop_waiting_frame
;
3586 stop_waiting_frame
.entropy_hash
= 0x14;
3587 stop_waiting_frame
.least_unacked
= UINT64_C(0x770123456789AA0);
3590 frames
.push_back(QuicFrame(&stop_waiting_frame
));
3593 unsigned char packet
[] = {
3594 // public flags (8 byte connection_id)
3597 0x10, 0x32, 0x54, 0x76,
3598 0x98, 0xBA, 0xDC, 0xFE,
3600 0xA8, 0x9A, 0x78, 0x56,
3602 // private flags (entropy)
3605 // frame type (stop waiting frame)
3607 // entropy hash of sent packets till least awaiting - 1.
3609 // least packet number awaiting an ack, delta from packet number.
3610 0x08, 0x00, 0x00, 0x00,
3615 scoped_ptr
<QuicPacket
> data(BuildDataPacket(header
, frames
));
3616 ASSERT_TRUE(data
!= nullptr);
3618 test::CompareCharArraysWithHexError("constructed packet",
3619 data
->data(), data
->length(),
3620 AsChars(packet
), arraysize(packet
));
3623 TEST_P(QuicFramerTest
, BuildRstFramePacketQuicVersion24
) {
3624 if (version_
> QUIC_VERSION_24
) {
3625 // QUIC_VERSION_25 removes the error_details field from QuicRstStreamFrame.
3629 QuicPacketHeader header
;
3630 header
.public_header
.connection_id
= UINT64_C(0xFEDCBA9876543210);
3631 header
.public_header
.reset_flag
= false;
3632 header
.public_header
.version_flag
= false;
3633 header
.fec_flag
= false;
3634 header
.entropy_flag
= false;
3635 header
.packet_packet_number
= UINT64_C(0x123456789ABC);
3636 header
.fec_group
= 0;
3638 QuicRstStreamFrame rst_frame
;
3639 rst_frame
.stream_id
= 0x01020304;
3640 rst_frame
.error_code
= static_cast<QuicRstStreamErrorCode
>(0x05060708);
3641 rst_frame
.error_details
= "because I can";
3642 rst_frame
.byte_offset
= 0x0807060504030201;
3645 unsigned char packet
[] = {
3646 // public flags (8 byte connection_id)
3649 0x10, 0x32, 0x54, 0x76,
3650 0x98, 0xBA, 0xDC, 0xFE,
3652 0xBC, 0x9A, 0x78, 0x56,
3657 // frame type (rst stream frame)
3660 0x04, 0x03, 0x02, 0x01,
3662 0x01, 0x02, 0x03, 0x04,
3663 0x05, 0x06, 0x07, 0x08,
3665 0x08, 0x07, 0x06, 0x05,
3666 // error details length
3677 frames
.push_back(QuicFrame(&rst_frame
));
3679 scoped_ptr
<QuicPacket
> data(BuildDataPacket(header
, frames
));
3680 ASSERT_TRUE(data
!= nullptr);
3682 test::CompareCharArraysWithHexError("constructed packet", data
->data(),
3683 data
->length(), AsChars(packet
),
3687 TEST_P(QuicFramerTest
, BuildRstFramePacketQuic
) {
3688 if (version_
<= QUIC_VERSION_24
) {
3689 // QUIC_VERSION_25 removes the error_details field from QuicRstStreamFrame.
3693 QuicPacketHeader header
;
3694 header
.public_header
.connection_id
= UINT64_C(0xFEDCBA9876543210);
3695 header
.public_header
.reset_flag
= false;
3696 header
.public_header
.version_flag
= false;
3697 header
.fec_flag
= false;
3698 header
.entropy_flag
= false;
3699 header
.packet_packet_number
= UINT64_C(0x123456789ABC);
3700 header
.fec_group
= 0;
3702 QuicRstStreamFrame rst_frame
;
3703 rst_frame
.stream_id
= 0x01020304;
3704 rst_frame
.error_code
= static_cast<QuicRstStreamErrorCode
>(0x05060708);
3705 rst_frame
.byte_offset
= 0x0807060504030201;
3708 unsigned char packet
[] = {
3709 // public flags (8 byte connection_id)
3712 0x10, 0x32, 0x54, 0x76,
3713 0x98, 0xBA, 0xDC, 0xFE,
3715 0xBC, 0x9A, 0x78, 0x56,
3720 // frame type (rst stream frame)
3723 0x04, 0x03, 0x02, 0x01,
3725 0x01, 0x02, 0x03, 0x04,
3726 0x05, 0x06, 0x07, 0x08,
3728 0x08, 0x07, 0x06, 0x05,
3733 frames
.push_back(QuicFrame(&rst_frame
));
3735 scoped_ptr
<QuicPacket
> data(BuildDataPacket(header
, frames
));
3736 ASSERT_TRUE(data
!= nullptr);
3738 test::CompareCharArraysWithHexError("constructed packet", data
->data(),
3739 data
->length(), AsChars(packet
),
3743 TEST_P(QuicFramerTest
, BuildCloseFramePacket
) {
3744 QuicPacketHeader header
;
3745 header
.public_header
.connection_id
= UINT64_C(0xFEDCBA9876543210);
3746 header
.public_header
.reset_flag
= false;
3747 header
.public_header
.version_flag
= false;
3748 header
.fec_flag
= false;
3749 header
.entropy_flag
= true;
3750 header
.packet_packet_number
= UINT64_C(0x123456789ABC);
3751 header
.fec_group
= 0;
3753 QuicConnectionCloseFrame close_frame
;
3754 close_frame
.error_code
= static_cast<QuicErrorCode
>(0x05060708);
3755 close_frame
.error_details
= "because I can";
3758 frames
.push_back(QuicFrame(&close_frame
));
3761 unsigned char packet
[] = {
3762 // public flags (8 byte connection_id)
3765 0x10, 0x32, 0x54, 0x76,
3766 0x98, 0xBA, 0xDC, 0xFE,
3768 0xBC, 0x9A, 0x78, 0x56,
3770 // private flags (entropy)
3773 // frame type (connection close frame)
3776 0x08, 0x07, 0x06, 0x05,
3777 // error details length
3787 scoped_ptr
<QuicPacket
> data(BuildDataPacket(header
, frames
));
3788 ASSERT_TRUE(data
!= nullptr);
3790 test::CompareCharArraysWithHexError("constructed packet",
3791 data
->data(), data
->length(),
3792 AsChars(packet
), arraysize(packet
));
3795 TEST_P(QuicFramerTest
, BuildGoAwayPacket
) {
3796 QuicPacketHeader header
;
3797 header
.public_header
.connection_id
= UINT64_C(0xFEDCBA9876543210);
3798 header
.public_header
.reset_flag
= false;
3799 header
.public_header
.version_flag
= false;
3800 header
.fec_flag
= false;
3801 header
.entropy_flag
= true;
3802 header
.packet_packet_number
= UINT64_C(0x123456789ABC);
3803 header
.fec_group
= 0;
3805 QuicGoAwayFrame goaway_frame
;
3806 goaway_frame
.error_code
= static_cast<QuicErrorCode
>(0x05060708);
3807 goaway_frame
.last_good_stream_id
= 0x01020304;
3808 goaway_frame
.reason_phrase
= "because I can";
3811 frames
.push_back(QuicFrame(&goaway_frame
));
3814 unsigned char packet
[] = {
3815 // public flags (8 byte connection_id)
3818 0x10, 0x32, 0x54, 0x76,
3819 0x98, 0xBA, 0xDC, 0xFE,
3821 0xBC, 0x9A, 0x78, 0x56,
3823 // private flags(entropy)
3826 // frame type (go away frame)
3829 0x08, 0x07, 0x06, 0x05,
3831 0x04, 0x03, 0x02, 0x01,
3832 // error details length
3842 scoped_ptr
<QuicPacket
> data(BuildDataPacket(header
, frames
));
3843 ASSERT_TRUE(data
!= nullptr);
3845 test::CompareCharArraysWithHexError("constructed packet",
3846 data
->data(), data
->length(),
3847 AsChars(packet
), arraysize(packet
));
3850 TEST_P(QuicFramerTest
, BuildWindowUpdatePacket
) {
3851 QuicPacketHeader header
;
3852 header
.public_header
.connection_id
= UINT64_C(0xFEDCBA9876543210);
3853 header
.public_header
.reset_flag
= false;
3854 header
.public_header
.version_flag
= false;
3855 header
.fec_flag
= false;
3856 header
.entropy_flag
= true;
3857 header
.packet_packet_number
= UINT64_C(0x123456789ABC);
3858 header
.fec_group
= 0;
3860 QuicWindowUpdateFrame window_update_frame
;
3861 window_update_frame
.stream_id
= 0x01020304;
3862 window_update_frame
.byte_offset
= 0x1122334455667788;
3865 frames
.push_back(QuicFrame(&window_update_frame
));
3868 unsigned char packet
[] = {
3869 // public flags (8 byte connection_id)
3872 0x10, 0x32, 0x54, 0x76,
3873 0x98, 0xBA, 0xDC, 0xFE,
3875 0xBC, 0x9A, 0x78, 0x56,
3877 // private flags(entropy)
3880 // frame type (window update frame)
3883 0x04, 0x03, 0x02, 0x01,
3885 0x88, 0x77, 0x66, 0x55,
3886 0x44, 0x33, 0x22, 0x11,
3890 scoped_ptr
<QuicPacket
> data(BuildDataPacket(header
, frames
));
3891 ASSERT_TRUE(data
!= nullptr);
3893 test::CompareCharArraysWithHexError("constructed packet", data
->data(),
3894 data
->length(), AsChars(packet
),
3898 TEST_P(QuicFramerTest
, BuildBlockedPacket
) {
3899 QuicPacketHeader header
;
3900 header
.public_header
.connection_id
= UINT64_C(0xFEDCBA9876543210);
3901 header
.public_header
.reset_flag
= false;
3902 header
.public_header
.version_flag
= false;
3903 header
.fec_flag
= false;
3904 header
.entropy_flag
= true;
3905 header
.packet_packet_number
= UINT64_C(0x123456789ABC);
3906 header
.fec_group
= 0;
3908 QuicBlockedFrame blocked_frame
;
3909 blocked_frame
.stream_id
= 0x01020304;
3912 frames
.push_back(QuicFrame(&blocked_frame
));
3915 unsigned char packet
[] = {
3916 // public flags (8 byte connection_id)
3919 0x10, 0x32, 0x54, 0x76,
3920 0x98, 0xBA, 0xDC, 0xFE,
3922 0xBC, 0x9A, 0x78, 0x56,
3924 // private flags(entropy)
3927 // frame type (blocked frame)
3930 0x04, 0x03, 0x02, 0x01,
3934 scoped_ptr
<QuicPacket
> data(BuildDataPacket(header
, frames
));
3935 ASSERT_TRUE(data
!= nullptr);
3937 test::CompareCharArraysWithHexError("constructed packet", data
->data(),
3938 data
->length(), AsChars(packet
),
3942 TEST_P(QuicFramerTest
, BuildPingPacket
) {
3943 QuicPacketHeader header
;
3944 header
.public_header
.connection_id
= UINT64_C(0xFEDCBA9876543210);
3945 header
.public_header
.reset_flag
= false;
3946 header
.public_header
.version_flag
= false;
3947 header
.fec_flag
= false;
3948 header
.entropy_flag
= true;
3949 header
.packet_packet_number
= UINT64_C(0x123456789ABC);
3950 header
.fec_group
= 0;
3952 QuicPingFrame ping_frame
;
3955 frames
.push_back(QuicFrame(&ping_frame
));
3958 unsigned char packet
[] = {
3959 // public flags (8 byte connection_id)
3962 0x10, 0x32, 0x54, 0x76,
3963 0x98, 0xBA, 0xDC, 0xFE,
3965 0xBC, 0x9A, 0x78, 0x56,
3967 // private flags(entropy)
3970 // frame type (ping frame)
3975 scoped_ptr
<QuicPacket
> data(BuildDataPacket(header
, frames
));
3976 ASSERT_TRUE(data
!= nullptr);
3978 test::CompareCharArraysWithHexError("constructed packet", data
->data(),
3979 data
->length(), AsChars(packet
),
3983 // Test that the MTU discovery packet is serialized correctly as a PING packet.
3984 TEST_P(QuicFramerTest
, BuildMtuDiscoveryPacket
) {
3985 QuicPacketHeader header
;
3986 header
.public_header
.connection_id
= UINT64_C(0xFEDCBA9876543210);
3987 header
.public_header
.reset_flag
= false;
3988 header
.public_header
.version_flag
= false;
3989 header
.fec_flag
= false;
3990 header
.entropy_flag
= true;
3991 header
.packet_packet_number
= UINT64_C(0x123456789ABC);
3992 header
.fec_group
= 0;
3994 QuicMtuDiscoveryFrame mtu_discovery_frame
;
3997 frames
.push_back(QuicFrame(&mtu_discovery_frame
));
4000 unsigned char packet
[] = {
4001 // public flags (8 byte connection_id)
4004 0x10, 0x32, 0x54, 0x76,
4005 0x98, 0xBA, 0xDC, 0xFE,
4007 0xBC, 0x9A, 0x78, 0x56,
4009 // private flags(entropy)
4012 // frame type (ping frame)
4017 scoped_ptr
<QuicPacket
> data(BuildDataPacket(header
, frames
));
4018 ASSERT_TRUE(data
!= nullptr);
4020 test::CompareCharArraysWithHexError("constructed packet", data
->data(),
4021 data
->length(), AsChars(packet
),
4025 TEST_P(QuicFramerTest
, BuildPublicResetPacket
) {
4026 QuicPublicResetPacket reset_packet
;
4027 reset_packet
.public_header
.connection_id
= UINT64_C(0xFEDCBA9876543210);
4028 reset_packet
.public_header
.reset_flag
= true;
4029 reset_packet
.public_header
.version_flag
= false;
4030 reset_packet
.rejected_packet_number
= UINT64_C(0x123456789ABC);
4031 reset_packet
.nonce_proof
= UINT64_C(0xABCDEF0123456789);
4034 unsigned char packet
[] = {
4035 // public flags (public reset, 8 byte ConnectionId)
4038 0x10, 0x32, 0x54, 0x76,
4039 0x98, 0xBA, 0xDC, 0xFE,
4040 // message tag (kPRST)
4042 // num_entries (2) + padding
4043 0x02, 0x00, 0x00, 0x00,
4047 0x08, 0x00, 0x00, 0x00,
4051 0x10, 0x00, 0x00, 0x00,
4053 0x89, 0x67, 0x45, 0x23,
4054 0x01, 0xEF, 0xCD, 0xAB,
4055 // rejected packet number
4056 0xBC, 0x9A, 0x78, 0x56,
4057 0x34, 0x12, 0x00, 0x00,
4061 scoped_ptr
<QuicEncryptedPacket
> data(
4062 framer_
.BuildPublicResetPacket(reset_packet
));
4063 ASSERT_TRUE(data
!= nullptr);
4065 test::CompareCharArraysWithHexError("constructed packet",
4066 data
->data(), data
->length(),
4067 AsChars(packet
), arraysize(packet
));
4070 TEST_P(QuicFramerTest
, BuildPublicResetPacketWithClientAddress
) {
4071 QuicPublicResetPacket reset_packet
;
4072 reset_packet
.public_header
.connection_id
= UINT64_C(0xFEDCBA9876543210);
4073 reset_packet
.public_header
.reset_flag
= true;
4074 reset_packet
.public_header
.version_flag
= false;
4075 reset_packet
.rejected_packet_number
= UINT64_C(0x123456789ABC);
4076 reset_packet
.nonce_proof
= UINT64_C(0xABCDEF0123456789);
4077 reset_packet
.client_address
= IPEndPoint(Loopback4(), 0x1234);
4080 unsigned char packet
[] = {
4081 // public flags (public reset, 8 byte ConnectionId)
4084 0x10, 0x32, 0x54, 0x76,
4085 0x98, 0xBA, 0xDC, 0xFE,
4086 // message tag (kPRST)
4088 // num_entries (3) + padding
4089 0x03, 0x00, 0x00, 0x00,
4093 0x08, 0x00, 0x00, 0x00,
4097 0x10, 0x00, 0x00, 0x00,
4101 0x18, 0x00, 0x00, 0x00,
4103 0x89, 0x67, 0x45, 0x23,
4104 0x01, 0xEF, 0xCD, 0xAB,
4105 // rejected packet number
4106 0xBC, 0x9A, 0x78, 0x56,
4107 0x34, 0x12, 0x00, 0x00,
4110 0x7F, 0x00, 0x00, 0x01,
4115 scoped_ptr
<QuicEncryptedPacket
> data(
4116 framer_
.BuildPublicResetPacket(reset_packet
));
4117 ASSERT_TRUE(data
!= nullptr);
4119 test::CompareCharArraysWithHexError("constructed packet",
4120 data
->data(), data
->length(),
4121 AsChars(packet
), arraysize(packet
));
4124 TEST_P(QuicFramerTest
, BuildFecPacket
) {
4125 QuicPacketHeader header
;
4126 header
.public_header
.connection_id
= UINT64_C(0xFEDCBA9876543210);
4127 header
.public_header
.reset_flag
= false;
4128 header
.public_header
.version_flag
= false;
4129 header
.fec_flag
= true;
4130 header
.entropy_flag
= true;
4131 header
.packet_packet_number
= (UINT64_C(0x123456789ABC));
4132 header
.is_in_fec_group
= IN_FEC_GROUP
;
4133 header
.fec_group
= UINT64_C(0x123456789ABB);
4135 QuicFecData fec_data
;
4136 fec_data
.fec_group
= 1;
4137 fec_data
.redundancy
= "abcdefghijklmnop";
4140 unsigned char packet
[] = {
4141 // public flags (8 byte connection_id)
4144 0x10, 0x32, 0x54, 0x76,
4145 0x98, 0xBA, 0xDC, 0xFE,
4147 0xBC, 0x9A, 0x78, 0x56,
4149 // private flags (entropy & fec group & fec packet)
4151 // first fec protected packet offset
4162 scoped_ptr
<QuicPacket
> data(framer_
.BuildFecPacket(header
, fec_data
));
4163 ASSERT_TRUE(data
!= nullptr);
4165 test::CompareCharArraysWithHexError("constructed packet",
4166 data
->data(), data
->length(),
4167 AsChars(packet
), arraysize(packet
));
4170 TEST_P(QuicFramerTest
, EncryptPacket
) {
4171 QuicPacketNumber packet_number
= UINT64_C(0x123456789ABC);
4173 unsigned char packet
[] = {
4174 // public flags (8 byte connection_id)
4177 0x10, 0x32, 0x54, 0x76,
4178 0x98, 0xBA, 0xDC, 0xFE,
4180 0xBC, 0x9A, 0x78, 0x56,
4182 // private flags (fec group & fec packet)
4184 // first fec protected packet offset
4195 scoped_ptr
<QuicPacket
> raw(new QuicPacket(
4196 AsChars(packet
), arraysize(packet
), false, PACKET_8BYTE_CONNECTION_ID
,
4197 !kIncludeVersion
, PACKET_6BYTE_PACKET_NUMBER
));
4198 char buffer
[kMaxPacketSize
];
4199 scoped_ptr
<QuicEncryptedPacket
> encrypted(framer_
.EncryptPayload(
4200 ENCRYPTION_NONE
, packet_number
, *raw
, buffer
, kMaxPacketSize
));
4202 ASSERT_TRUE(encrypted
.get() != nullptr);
4203 EXPECT_TRUE(CheckEncryption(packet_number
, raw
.get()));
4206 TEST_P(QuicFramerTest
, EncryptPacketWithVersionFlag
) {
4207 QuicPacketNumber packet_number
= UINT64_C(0x123456789ABC);
4209 unsigned char packet
[] = {
4210 // public flags (version, 8 byte connection_id)
4213 0x10, 0x32, 0x54, 0x76,
4214 0x98, 0xBA, 0xDC, 0xFE,
4218 0xBC, 0x9A, 0x78, 0x56,
4220 // private flags (fec group & fec flags)
4222 // first fec protected packet offset
4233 scoped_ptr
<QuicPacket
> raw(new QuicPacket(
4234 AsChars(packet
), arraysize(packet
), false, PACKET_8BYTE_CONNECTION_ID
,
4235 kIncludeVersion
, PACKET_6BYTE_PACKET_NUMBER
));
4236 char buffer
[kMaxPacketSize
];
4237 scoped_ptr
<QuicEncryptedPacket
> encrypted(framer_
.EncryptPayload(
4238 ENCRYPTION_NONE
, packet_number
, *raw
, buffer
, kMaxPacketSize
));
4240 ASSERT_TRUE(encrypted
.get() != nullptr);
4241 EXPECT_TRUE(CheckEncryption(packet_number
, raw
.get()));
4244 TEST_P(QuicFramerTest
, AckTruncationLargePacket
) {
4245 QuicPacketHeader header
;
4246 header
.public_header
.connection_id
= UINT64_C(0xFEDCBA9876543210);
4247 header
.public_header
.reset_flag
= false;
4248 header
.public_header
.version_flag
= false;
4249 header
.fec_flag
= false;
4250 header
.entropy_flag
= false;
4251 header
.packet_packet_number
= UINT64_C(0x123456789ABC);
4252 header
.fec_group
= 0;
4254 // Create a packet with just the ack.
4255 QuicAckFrame ack_frame
= MakeAckFrameWithNackRanges(300, 0u);
4257 frame
.type
= ACK_FRAME
;
4258 frame
.ack_frame
= &ack_frame
;
4260 frames
.push_back(frame
);
4262 // Build an ack packet with truncation due to limit in number of nack ranges.
4263 scoped_ptr
<QuicPacket
> raw_ack_packet(BuildDataPacket(header
, frames
));
4264 ASSERT_TRUE(raw_ack_packet
!= nullptr);
4265 char buffer
[kMaxPacketSize
];
4266 scoped_ptr
<QuicEncryptedPacket
> ack_packet(
4267 framer_
.EncryptPayload(ENCRYPTION_NONE
, header
.packet_packet_number
,
4268 *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
));
4271 ASSERT_EQ(1u, visitor_
.ack_frames_
.size());
4272 QuicAckFrame
& processed_ack_frame
= *visitor_
.ack_frames_
[0];
4273 EXPECT_TRUE(processed_ack_frame
.is_truncated
);
4274 EXPECT_EQ(510u, processed_ack_frame
.largest_observed
);
4275 ASSERT_EQ(255u, processed_ack_frame
.missing_packets
.size());
4276 PacketNumberSet::const_iterator missing_iter
=
4277 processed_ack_frame
.missing_packets
.begin();
4278 EXPECT_EQ(1u, *missing_iter
);
4279 PacketNumberSet::const_reverse_iterator last_missing_iter
=
4280 processed_ack_frame
.missing_packets
.rbegin();
4281 EXPECT_EQ(509u, *last_missing_iter
);
4284 TEST_P(QuicFramerTest
, AckTruncationSmallPacket
) {
4285 QuicPacketHeader header
;
4286 header
.public_header
.connection_id
= UINT64_C(0xFEDCBA9876543210);
4287 header
.public_header
.reset_flag
= false;
4288 header
.public_header
.version_flag
= false;
4289 header
.fec_flag
= false;
4290 header
.entropy_flag
= false;
4291 header
.packet_packet_number
= UINT64_C(0x123456789ABC);
4292 header
.fec_group
= 0;
4294 // Create a packet with just the ack.
4295 QuicAckFrame ack_frame
= MakeAckFrameWithNackRanges(300, 0u);
4297 frame
.type
= ACK_FRAME
;
4298 frame
.ack_frame
= &ack_frame
;
4300 frames
.push_back(frame
);
4302 // Build an ack packet with truncation due to limit in number of nack ranges.
4303 scoped_ptr
<QuicPacket
> raw_ack_packet(BuildDataPacket(header
, frames
, 500));
4304 ASSERT_TRUE(raw_ack_packet
!= nullptr);
4305 char buffer
[kMaxPacketSize
];
4306 scoped_ptr
<QuicEncryptedPacket
> ack_packet(
4307 framer_
.EncryptPayload(ENCRYPTION_NONE
, header
.packet_packet_number
,
4308 *raw_ack_packet
, buffer
, kMaxPacketSize
));
4309 // Now make sure we can turn our ack packet back into an ack frame.
4310 ASSERT_TRUE(framer_
.ProcessPacket(*ack_packet
));
4311 ASSERT_EQ(1u, visitor_
.ack_frames_
.size());
4312 QuicAckFrame
& processed_ack_frame
= *visitor_
.ack_frames_
[0];
4313 EXPECT_TRUE(processed_ack_frame
.is_truncated
);
4314 EXPECT_EQ(476u, processed_ack_frame
.largest_observed
);
4315 ASSERT_EQ(238u, processed_ack_frame
.missing_packets
.size());
4316 PacketNumberSet::const_iterator missing_iter
=
4317 processed_ack_frame
.missing_packets
.begin();
4318 EXPECT_EQ(1u, *missing_iter
);
4319 PacketNumberSet::const_reverse_iterator last_missing_iter
=
4320 processed_ack_frame
.missing_packets
.rbegin();
4321 EXPECT_EQ(475u, *last_missing_iter
);
4324 TEST_P(QuicFramerTest
, CleanTruncation
) {
4325 QuicPacketHeader header
;
4326 header
.public_header
.connection_id
= UINT64_C(0xFEDCBA9876543210);
4327 header
.public_header
.reset_flag
= false;
4328 header
.public_header
.version_flag
= false;
4329 header
.fec_flag
= false;
4330 header
.entropy_flag
= true;
4331 header
.packet_packet_number
= UINT64_C(0x123456789ABC);
4332 header
.fec_group
= 0;
4334 QuicAckFrame ack_frame
;
4335 ack_frame
.largest_observed
= 201;
4336 for (uint64 i
= 1; i
< ack_frame
.largest_observed
; ++i
) {
4337 ack_frame
.missing_packets
.insert(i
);
4340 // Create a packet with just the ack.
4342 frame
.type
= ACK_FRAME
;
4343 frame
.ack_frame
= &ack_frame
;
4345 frames
.push_back(frame
);
4347 scoped_ptr
<QuicPacket
> raw_ack_packet(BuildDataPacket(header
, frames
));
4348 ASSERT_TRUE(raw_ack_packet
!= nullptr);
4350 char buffer
[kMaxPacketSize
];
4351 scoped_ptr
<QuicEncryptedPacket
> ack_packet(
4352 framer_
.EncryptPayload(ENCRYPTION_NONE
, header
.packet_packet_number
,
4353 *raw_ack_packet
, buffer
, kMaxPacketSize
));
4355 // Now make sure we can turn our ack packet back into an ack frame.
4356 ASSERT_TRUE(framer_
.ProcessPacket(*ack_packet
));
4358 // Test for clean truncation of the ack by comparing the length of the
4359 // original packets to the re-serialized packets.
4361 frame
.type
= ACK_FRAME
;
4362 frame
.ack_frame
= visitor_
.ack_frames_
[0];
4363 frames
.push_back(frame
);
4365 size_t original_raw_length
= raw_ack_packet
->length();
4366 raw_ack_packet
.reset(BuildDataPacket(header
, frames
));
4367 ASSERT_TRUE(raw_ack_packet
!= nullptr);
4368 EXPECT_EQ(original_raw_length
, raw_ack_packet
->length());
4369 ASSERT_TRUE(raw_ack_packet
!= nullptr);
4372 TEST_P(QuicFramerTest
, EntropyFlagTest
) {
4374 unsigned char packet
[] = {
4375 // public flags (8 byte connection_id)
4378 0x10, 0x32, 0x54, 0x76,
4379 0x98, 0xBA, 0xDC, 0xFE,
4381 0xBC, 0x9A, 0x78, 0x56,
4383 // private flags (Entropy)
4386 // frame type (stream frame with fin and no length)
4389 0x04, 0x03, 0x02, 0x01,
4391 0x54, 0x76, 0x10, 0x32,
4392 0xDC, 0xFE, 0x98, 0xBA,
4400 QuicEncryptedPacket
encrypted(AsChars(packet
), arraysize(packet
), false);
4401 EXPECT_TRUE(framer_
.ProcessPacket(encrypted
));
4402 EXPECT_EQ(QUIC_NO_ERROR
, framer_
.error());
4403 ASSERT_TRUE(visitor_
.header_
.get());
4404 EXPECT_TRUE(visitor_
.header_
->entropy_flag
);
4405 EXPECT_EQ(1 << 4, visitor_
.header_
->entropy_hash
);
4406 EXPECT_FALSE(visitor_
.header_
->fec_flag
);
4409 TEST_P(QuicFramerTest
, FecEntropyTest
) {
4411 unsigned char packet
[] = {
4412 // public flags (8 byte connection_id)
4415 0x10, 0x32, 0x54, 0x76,
4416 0x98, 0xBA, 0xDC, 0xFE,
4418 0xBC, 0x9A, 0x78, 0x56,
4420 // private flags (Entropy & fec group & FEC)
4422 // first fec protected packet offset
4425 // frame type (stream frame with fin and no length)
4428 0x04, 0x03, 0x02, 0x01,
4430 0x54, 0x76, 0x10, 0x32,
4431 0xDC, 0xFE, 0x98, 0xBA,
4439 QuicEncryptedPacket
encrypted(AsChars(packet
), arraysize(packet
), false);
4440 EXPECT_TRUE(framer_
.ProcessPacket(encrypted
));
4441 EXPECT_EQ(QUIC_NO_ERROR
, framer_
.error());
4442 ASSERT_TRUE(visitor_
.header_
.get());
4443 EXPECT_TRUE(visitor_
.header_
->fec_flag
);
4444 EXPECT_TRUE(visitor_
.header_
->entropy_flag
);
4445 EXPECT_EQ(1 << 4, visitor_
.header_
->entropy_hash
);
4448 TEST_P(QuicFramerTest
, StopPacketProcessing
) {
4450 unsigned char packet
[] = {
4451 // public flags (8 byte connection_id)
4454 0x10, 0x32, 0x54, 0x76,
4455 0x98, 0xBA, 0xDC, 0xFE,
4457 0xBC, 0x9A, 0x78, 0x56,
4462 // frame type (stream frame with fin)
4465 0x04, 0x03, 0x02, 0x01,
4467 0x54, 0x76, 0x10, 0x32,
4468 0xDC, 0xFE, 0x98, 0xBA,
4476 // frame type (ack frame)
4478 // entropy hash of sent packets till least awaiting - 1.
4480 // least packet number awaiting an ack
4481 0xA0, 0x9A, 0x78, 0x56,
4483 // entropy hash of all received packets.
4485 // largest observed packet number
4486 0xBF, 0x9A, 0x78, 0x56,
4488 // num missing packets
4491 0xBE, 0x9A, 0x78, 0x56,
4496 MockFramerVisitor visitor
;
4497 framer_
.set_visitor(&visitor
);
4498 EXPECT_CALL(visitor
, OnPacket());
4499 EXPECT_CALL(visitor
, OnPacketHeader(_
));
4500 EXPECT_CALL(visitor
, OnStreamFrame(_
)).WillOnce(Return(false));
4501 EXPECT_CALL(visitor
, OnAckFrame(_
)).Times(0);
4502 EXPECT_CALL(visitor
, OnPacketComplete());
4503 EXPECT_CALL(visitor
, OnUnauthenticatedPublicHeader(_
)).WillOnce(Return(true));
4504 EXPECT_CALL(visitor
, OnUnauthenticatedHeader(_
)).WillOnce(Return(true));
4505 EXPECT_CALL(visitor
, OnDecryptedPacket(_
));
4507 QuicEncryptedPacket
encrypted(AsChars(packet
), arraysize(packet
), false);
4508 EXPECT_TRUE(framer_
.ProcessPacket(encrypted
));
4509 EXPECT_EQ(QUIC_NO_ERROR
, framer_
.error());
4512 static char kTestString
[] = "At least 20 characters.";
4513 static QuicStreamId kTestQuicStreamId
= 1;
4514 static bool ExpectedStreamFrame(const QuicStreamFrame
& frame
) {
4515 return frame
.stream_id
== kTestQuicStreamId
&& !frame
.fin
&&
4516 frame
.offset
== 0 && frame
.data
== kTestString
;
4517 // FIN is hard-coded false in ConstructEncryptedPacket.
4518 // Offset 0 is hard-coded in ConstructEncryptedPacket.
4521 // Verify that the packet returned by ConstructEncryptedPacket() can be properly
4522 // parsed by the framer.
4523 TEST_P(QuicFramerTest
, ConstructEncryptedPacket
) {
4524 // Since we are using ConstructEncryptedPacket, we have to set the framer's
4525 // crypto to be Null.
4526 framer_
.SetDecrypter(ENCRYPTION_NONE
, QuicDecrypter::Create(kNULL
));
4527 framer_
.SetEncrypter(ENCRYPTION_NONE
, QuicEncrypter::Create(kNULL
));
4529 scoped_ptr
<QuicEncryptedPacket
> packet(ConstructEncryptedPacket(
4530 42, false, false, kTestQuicStreamId
, kTestString
,
4531 PACKET_8BYTE_CONNECTION_ID
, PACKET_6BYTE_PACKET_NUMBER
));
4533 MockFramerVisitor visitor
;
4534 framer_
.set_visitor(&visitor
);
4535 EXPECT_CALL(visitor
, OnPacket()).Times(1);
4536 EXPECT_CALL(visitor
, OnUnauthenticatedPublicHeader(_
))
4538 .WillOnce(Return(true));
4539 EXPECT_CALL(visitor
, OnUnauthenticatedHeader(_
))
4541 .WillOnce(Return(true));
4542 EXPECT_CALL(visitor
, OnPacketHeader(_
)).Times(1).WillOnce(Return(true));
4543 EXPECT_CALL(visitor
, OnDecryptedPacket(_
)).Times(1);
4544 EXPECT_CALL(visitor
, OnError(_
)).Times(0);
4545 EXPECT_CALL(visitor
, OnStreamFrame(_
)).Times(0);
4546 EXPECT_CALL(visitor
, OnStreamFrame(Truly(ExpectedStreamFrame
))).Times(1);
4547 EXPECT_CALL(visitor
, OnAckFrame(_
)).Times(0);
4548 EXPECT_CALL(visitor
, OnPacketComplete()).Times(1);
4550 EXPECT_TRUE(framer_
.ProcessPacket(*packet
));
4551 EXPECT_EQ(QUIC_NO_ERROR
, framer_
.error());
4554 // Verify that the packet returned by ConstructMisFramedEncryptedPacket()
4555 // does cause the framer to return an error.
4556 TEST_P(QuicFramerTest
, ConstructMisFramedEncryptedPacket
) {
4557 // Since we are using ConstructEncryptedPacket, we have to set the framer's
4558 // crypto to be Null.
4559 framer_
.SetDecrypter(ENCRYPTION_NONE
, QuicDecrypter::Create(kNULL
));
4560 framer_
.SetEncrypter(ENCRYPTION_NONE
, QuicEncrypter::Create(kNULL
));
4562 scoped_ptr
<QuicEncryptedPacket
> packet(ConstructMisFramedEncryptedPacket(
4563 42, false, false, kTestQuicStreamId
, kTestString
,
4564 PACKET_8BYTE_CONNECTION_ID
, PACKET_6BYTE_PACKET_NUMBER
, nullptr));
4566 MockFramerVisitor visitor
;
4567 framer_
.set_visitor(&visitor
);
4568 EXPECT_CALL(visitor
, OnPacket()).Times(1);
4569 EXPECT_CALL(visitor
, OnUnauthenticatedPublicHeader(_
))
4571 .WillOnce(Return(true));
4572 EXPECT_CALL(visitor
, OnUnauthenticatedHeader(_
))
4574 .WillOnce(Return(true));
4575 EXPECT_CALL(visitor
, OnPacketHeader(_
)).Times(0);
4576 EXPECT_CALL(visitor
, OnDecryptedPacket(_
)).Times(1);
4577 EXPECT_CALL(visitor
, OnError(_
)).Times(1);
4578 EXPECT_CALL(visitor
, OnStreamFrame(_
)).Times(0);
4579 EXPECT_CALL(visitor
, OnAckFrame(_
)).Times(0);
4580 EXPECT_CALL(visitor
, OnPacketComplete()).Times(0);
4582 EXPECT_FALSE(framer_
.ProcessPacket(*packet
));
4583 EXPECT_EQ(QUIC_INVALID_PACKET_HEADER
, framer_
.error());