Android WebView: add sfntly to deps whitelist.
[chromium-blink-merge.git] / net / quic / quic_framer_test.cc
blob9f882a82cedb4465110ab210ab4fcc1a5ca99797
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 <algorithm>
6 #include <map>
7 #include <string>
8 #include <vector>
10 #include "base/containers/hash_tables.h"
11 #include "base/logging.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/port.h"
14 #include "base/stl_util.h"
15 #include "net/quic/crypto/quic_decrypter.h"
16 #include "net/quic/crypto/quic_encrypter.h"
17 #include "net/quic/quic_framer.h"
18 #include "net/quic/quic_protocol.h"
19 #include "net/quic/quic_utils.h"
20 #include "net/quic/test_tools/quic_framer_peer.h"
21 #include "net/quic/test_tools/quic_test_utils.h"
23 using base::hash_set;
24 using base::StringPiece;
25 using std::make_pair;
26 using std::map;
27 using std::numeric_limits;
28 using std::pair;
29 using std::string;
30 using std::vector;
31 using testing::Return;
32 using testing::_;
34 namespace net {
35 namespace test {
37 const QuicPacketSequenceNumber kEpoch = GG_UINT64_C(1) << 48;
38 const QuicPacketSequenceNumber kMask = kEpoch - 1;
40 // Index into the guid offset in the header.
41 const size_t kGuidOffset = kPublicFlagsSize;
42 // Index into the version string in the header. (if present).
43 const size_t kVersionOffset = kGuidOffset + PACKET_8BYTE_GUID;
45 // Index into the sequence number offset in the header.
46 size_t GetSequenceNumberOffset(QuicGuidLength guid_length,
47 bool include_version) {
48 return kGuidOffset + guid_length +
49 (include_version ? kQuicVersionSize : 0);
52 size_t GetSequenceNumberOffset(bool include_version) {
53 return GetSequenceNumberOffset(PACKET_8BYTE_GUID, include_version);
56 // Index into the private flags offset in the data packet header.
57 size_t GetPrivateFlagsOffset(QuicGuidLength guid_length, bool include_version) {
58 return GetSequenceNumberOffset(guid_length, include_version) +
59 PACKET_6BYTE_SEQUENCE_NUMBER;
62 size_t GetPrivateFlagsOffset(bool include_version) {
63 return GetPrivateFlagsOffset(PACKET_8BYTE_GUID, include_version);
66 size_t GetPrivateFlagsOffset(bool include_version,
67 QuicSequenceNumberLength sequence_number_length) {
68 return GetSequenceNumberOffset(PACKET_8BYTE_GUID, include_version) +
69 sequence_number_length;
72 // Index into the fec group offset in the header.
73 size_t GetFecGroupOffset(QuicGuidLength guid_length, bool include_version) {
74 return GetPrivateFlagsOffset(guid_length, include_version) +
75 kPrivateFlagsSize;
78 size_t GetFecGroupOffset(bool include_version) {
79 return GetPrivateFlagsOffset(PACKET_8BYTE_GUID, include_version) +
80 kPrivateFlagsSize;
83 size_t GetFecGroupOffset(bool include_version,
84 QuicSequenceNumberLength sequence_number_length) {
85 return GetPrivateFlagsOffset(include_version, sequence_number_length) +
86 kPrivateFlagsSize;
89 // Index into the nonce proof of the public reset packet.
90 // Public resets always have full guids.
91 const size_t kPublicResetPacketNonceProofOffset =
92 kGuidOffset + PACKET_8BYTE_GUID;
94 // Index into the rejected sequence number of the public reset packet.
95 const size_t kPublicResetPacketRejectedSequenceNumberOffset =
96 kPublicResetPacketNonceProofOffset + kPublicResetNonceSize;
98 class TestEncrypter : public QuicEncrypter {
99 public:
100 virtual ~TestEncrypter() {}
101 virtual bool SetKey(StringPiece key) OVERRIDE {
102 return true;
104 virtual bool SetNoncePrefix(StringPiece nonce_prefix) OVERRIDE {
105 return true;
107 virtual bool Encrypt(StringPiece nonce,
108 StringPiece associated_data,
109 StringPiece plaintext,
110 unsigned char* output) OVERRIDE {
111 CHECK(false) << "Not implemented";
112 return false;
114 virtual QuicData* EncryptPacket(QuicPacketSequenceNumber sequence_number,
115 StringPiece associated_data,
116 StringPiece plaintext) OVERRIDE {
117 sequence_number_ = sequence_number;
118 associated_data_ = associated_data.as_string();
119 plaintext_ = plaintext.as_string();
120 return new QuicData(plaintext.data(), plaintext.length());
122 virtual size_t GetKeySize() const OVERRIDE {
123 return 0;
125 virtual size_t GetNoncePrefixSize() const OVERRIDE {
126 return 0;
128 virtual size_t GetMaxPlaintextSize(size_t ciphertext_size) const OVERRIDE {
129 return ciphertext_size;
131 virtual size_t GetCiphertextSize(size_t plaintext_size) const OVERRIDE {
132 return plaintext_size;
134 virtual StringPiece GetKey() const OVERRIDE {
135 return StringPiece();
137 virtual StringPiece GetNoncePrefix() const OVERRIDE {
138 return StringPiece();
140 QuicPacketSequenceNumber sequence_number_;
141 string associated_data_;
142 string plaintext_;
145 class TestDecrypter : public QuicDecrypter {
146 public:
147 virtual ~TestDecrypter() {}
148 virtual bool SetKey(StringPiece key) OVERRIDE {
149 return true;
151 virtual bool SetNoncePrefix(StringPiece nonce_prefix) OVERRIDE {
152 return true;
154 virtual bool Decrypt(StringPiece nonce,
155 StringPiece associated_data,
156 StringPiece ciphertext,
157 unsigned char* output,
158 size_t* output_length) OVERRIDE {
159 CHECK(false) << "Not implemented";
160 return false;
162 virtual QuicData* DecryptPacket(QuicPacketSequenceNumber sequence_number,
163 StringPiece associated_data,
164 StringPiece ciphertext) OVERRIDE {
165 sequence_number_ = sequence_number;
166 associated_data_ = associated_data.as_string();
167 ciphertext_ = ciphertext.as_string();
168 return new QuicData(ciphertext.data(), ciphertext.length());
170 virtual StringPiece GetKey() const OVERRIDE {
171 return StringPiece();
173 virtual StringPiece GetNoncePrefix() const OVERRIDE {
174 return StringPiece();
176 QuicPacketSequenceNumber sequence_number_;
177 string associated_data_;
178 string ciphertext_;
181 class TestQuicVisitor : public ::net::QuicFramerVisitorInterface {
182 public:
183 TestQuicVisitor()
184 : error_count_(0),
185 version_mismatch_(0),
186 packet_count_(0),
187 frame_count_(0),
188 fec_count_(0),
189 complete_packets_(0),
190 revived_packets_(0),
191 accept_packet_(true) {
194 virtual ~TestQuicVisitor() {
195 STLDeleteElements(&stream_frames_);
196 STLDeleteElements(&ack_frames_);
197 STLDeleteElements(&congestion_feedback_frames_);
198 STLDeleteElements(&fec_data_);
201 virtual void OnError(QuicFramer* f) OVERRIDE {
202 DLOG(INFO) << "QuicFramer Error: " << QuicUtils::ErrorToString(f->error())
203 << " (" << f->error() << ")";
204 error_count_++;
207 virtual void OnPacket() OVERRIDE {}
209 virtual void OnPublicResetPacket(
210 const QuicPublicResetPacket& packet) OVERRIDE {
211 public_reset_packet_.reset(new QuicPublicResetPacket(packet));
214 virtual void OnVersionNegotiationPacket(
215 const QuicVersionNegotiationPacket& packet) OVERRIDE {
216 version_negotiation_packet_.reset(new QuicVersionNegotiationPacket(packet));
219 virtual void OnRevivedPacket() OVERRIDE {
220 revived_packets_++;
223 virtual bool OnProtocolVersionMismatch(QuicVersion version) OVERRIDE {
224 DLOG(INFO) << "QuicFramer Version Mismatch, version: " << version;
225 version_mismatch_++;
226 return true;
229 virtual bool OnPacketHeader(const QuicPacketHeader& header) OVERRIDE {
230 packet_count_++;
231 header_.reset(new QuicPacketHeader(header));
232 return accept_packet_;
235 virtual bool OnUnauthenticatedHeader(
236 const QuicPacketHeader& header) OVERRIDE {
237 return true;
240 virtual bool OnStreamFrame(const QuicStreamFrame& frame) OVERRIDE {
241 frame_count_++;
242 stream_frames_.push_back(new QuicStreamFrame(frame));
243 return true;
246 virtual void OnFecProtectedPayload(StringPiece payload) OVERRIDE {
247 fec_protected_payload_ = payload.as_string();
250 virtual bool OnAckFrame(const QuicAckFrame& frame) OVERRIDE {
251 frame_count_++;
252 ack_frames_.push_back(new QuicAckFrame(frame));
253 return true;
256 virtual bool OnCongestionFeedbackFrame(
257 const QuicCongestionFeedbackFrame& frame) OVERRIDE {
258 frame_count_++;
259 congestion_feedback_frames_.push_back(
260 new QuicCongestionFeedbackFrame(frame));
261 return true;
264 virtual void OnFecData(const QuicFecData& fec) OVERRIDE {
265 fec_count_++;
266 fec_data_.push_back(new QuicFecData(fec));
269 virtual void OnPacketComplete() OVERRIDE {
270 complete_packets_++;
273 virtual bool OnRstStreamFrame(const QuicRstStreamFrame& frame) OVERRIDE {
274 rst_stream_frame_ = frame;
275 return true;
278 virtual bool OnConnectionCloseFrame(
279 const QuicConnectionCloseFrame& frame) OVERRIDE {
280 connection_close_frame_ = frame;
281 return true;
284 virtual bool OnGoAwayFrame(const QuicGoAwayFrame& frame) OVERRIDE {
285 goaway_frame_ = frame;
286 return true;
289 // Counters from the visitor_ callbacks.
290 int error_count_;
291 int version_mismatch_;
292 int packet_count_;
293 int frame_count_;
294 int fec_count_;
295 int complete_packets_;
296 int revived_packets_;
297 bool accept_packet_;
299 scoped_ptr<QuicPacketHeader> header_;
300 scoped_ptr<QuicPublicResetPacket> public_reset_packet_;
301 scoped_ptr<QuicVersionNegotiationPacket> version_negotiation_packet_;
302 vector<QuicStreamFrame*> stream_frames_;
303 vector<QuicAckFrame*> ack_frames_;
304 vector<QuicCongestionFeedbackFrame*> congestion_feedback_frames_;
305 vector<QuicFecData*> fec_data_;
306 string fec_protected_payload_;
307 QuicRstStreamFrame rst_stream_frame_;
308 QuicConnectionCloseFrame connection_close_frame_;
309 QuicGoAwayFrame goaway_frame_;
312 class QuicFramerTest : public ::testing::TestWithParam<QuicVersion> {
313 public:
314 QuicFramerTest()
315 : encrypter_(new test::TestEncrypter()),
316 decrypter_(new test::TestDecrypter()),
317 start_(QuicTime::Zero().Add(QuicTime::Delta::FromMicroseconds(0x10))),
318 framer_(QuicSupportedVersions(), start_, true) {
319 version_ = GetParam();
320 framer_.set_version(version_);
321 framer_.SetDecrypter(decrypter_);
322 framer_.SetEncrypter(ENCRYPTION_NONE, encrypter_);
323 framer_.set_visitor(&visitor_);
324 framer_.set_received_entropy_calculator(&entropy_calculator_);
327 // Helper function to get unsigned char representation of digit in the
328 // units place of the current QUIC version number.
329 unsigned char GetQuicVersionDigitOnes() {
330 return static_cast<unsigned char> ('0' + version_%10);
333 // Helper function to get unsigned char representation of digit in the
334 // tens place of the current QUIC version number.
335 unsigned char GetQuicVersionDigitTens() {
336 return static_cast<unsigned char> ('0' + (version_/10)%10);
339 bool CheckEncryption(QuicPacketSequenceNumber sequence_number,
340 QuicPacket* packet) {
341 if (sequence_number != encrypter_->sequence_number_) {
342 LOG(ERROR) << "Encrypted incorrect packet sequence number. expected "
343 << sequence_number << " actual: "
344 << encrypter_->sequence_number_;
345 return false;
347 if (packet->AssociatedData() != encrypter_->associated_data_) {
348 LOG(ERROR) << "Encrypted incorrect associated data. expected "
349 << packet->AssociatedData() << " actual: "
350 << encrypter_->associated_data_;
351 return false;
353 if (packet->Plaintext() != encrypter_->plaintext_) {
354 LOG(ERROR) << "Encrypted incorrect plaintext data. expected "
355 << packet->Plaintext() << " actual: "
356 << encrypter_->plaintext_;
357 return false;
359 return true;
362 bool CheckDecryption(const QuicEncryptedPacket& encrypted,
363 bool includes_version) {
364 if (visitor_.header_->packet_sequence_number !=
365 decrypter_->sequence_number_) {
366 LOG(ERROR) << "Decrypted incorrect packet sequence number. expected "
367 << visitor_.header_->packet_sequence_number << " actual: "
368 << decrypter_->sequence_number_;
369 return false;
371 if (QuicFramer::GetAssociatedDataFromEncryptedPacket(
372 encrypted, PACKET_8BYTE_GUID,
373 includes_version, PACKET_6BYTE_SEQUENCE_NUMBER) !=
374 decrypter_->associated_data_) {
375 LOG(ERROR) << "Decrypted incorrect associated data. expected "
376 << QuicFramer::GetAssociatedDataFromEncryptedPacket(
377 encrypted, PACKET_8BYTE_GUID,
378 includes_version, PACKET_6BYTE_SEQUENCE_NUMBER)
379 << " actual: " << decrypter_->associated_data_;
380 return false;
382 StringPiece ciphertext(encrypted.AsStringPiece().substr(
383 GetStartOfEncryptedData(PACKET_8BYTE_GUID, includes_version,
384 PACKET_6BYTE_SEQUENCE_NUMBER)));
385 if (ciphertext != decrypter_->ciphertext_) {
386 LOG(ERROR) << "Decrypted incorrect ciphertext data. expected "
387 << ciphertext << " actual: "
388 << decrypter_->ciphertext_;
389 return false;
391 return true;
394 char* AsChars(unsigned char* data) {
395 return reinterpret_cast<char*>(data);
398 void CheckProcessingFails(unsigned char* packet,
399 size_t len,
400 string expected_error,
401 QuicErrorCode error_code) {
402 QuicEncryptedPacket encrypted(AsChars(packet), len, false);
403 EXPECT_FALSE(framer_.ProcessPacket(encrypted)) << "len: " << len;
404 EXPECT_EQ(expected_error, framer_.detailed_error()) << "len: " << len;
405 EXPECT_EQ(error_code, framer_.error()) << "len: " << len;
408 // Checks if the supplied string matches data in the supplied StreamFrame.
409 void CheckStreamFrameData(string str, QuicStreamFrame* frame) {
410 scoped_ptr<string> frame_data(frame->GetDataAsString());
411 EXPECT_EQ(str, *frame_data);
414 void CheckStreamFrameBoundaries(unsigned char* packet,
415 size_t stream_id_size,
416 bool include_version) {
417 // Now test framing boundaries
418 for (size_t i = kQuicFrameTypeSize;
419 i < GetMinStreamFrameSize(framer_.version()); ++i) {
420 string expected_error;
421 if (i < kQuicFrameTypeSize + stream_id_size) {
422 expected_error = "Unable to read stream_id.";
423 } else if (i < kQuicFrameTypeSize + stream_id_size +
424 kQuicMaxStreamOffsetSize) {
425 expected_error = "Unable to read offset.";
426 } else {
427 expected_error = "Unable to read frame data.";
429 CheckProcessingFails(
430 packet,
431 i + GetPacketHeaderSize(PACKET_8BYTE_GUID, include_version,
432 PACKET_6BYTE_SEQUENCE_NUMBER,
433 NOT_IN_FEC_GROUP),
434 expected_error, QUIC_INVALID_STREAM_DATA);
438 void CheckCalculatePacketSequenceNumber(
439 QuicPacketSequenceNumber expected_sequence_number,
440 QuicPacketSequenceNumber last_sequence_number) {
441 QuicPacketSequenceNumber wire_sequence_number =
442 expected_sequence_number & kMask;
443 QuicFramerPeer::SetLastSequenceNumber(&framer_, last_sequence_number);
444 EXPECT_EQ(expected_sequence_number,
445 QuicFramerPeer::CalculatePacketSequenceNumberFromWire(
446 &framer_, PACKET_6BYTE_SEQUENCE_NUMBER, wire_sequence_number))
447 << "last_sequence_number: " << last_sequence_number
448 << " wire_sequence_number: " << wire_sequence_number;
451 test::TestEncrypter* encrypter_;
452 test::TestDecrypter* decrypter_;
453 QuicVersion version_;
454 QuicTime start_;
455 QuicFramer framer_;
456 test::TestQuicVisitor visitor_;
457 test::TestEntropyCalculator entropy_calculator_;
460 // Run all framer tests with all supported versions of QUIC.
461 INSTANTIATE_TEST_CASE_P(QuicFramerTests,
462 QuicFramerTest,
463 ::testing::ValuesIn(kSupportedQuicVersions));
465 TEST_P(QuicFramerTest, CalculatePacketSequenceNumberFromWireNearEpochStart) {
466 // A few quick manual sanity checks
467 CheckCalculatePacketSequenceNumber(GG_UINT64_C(1), GG_UINT64_C(0));
468 CheckCalculatePacketSequenceNumber(kEpoch + 1, kMask);
469 CheckCalculatePacketSequenceNumber(kEpoch, kMask);
471 // Cases where the last number was close to the start of the range
472 for (uint64 last = 0; last < 10; last++) {
473 // Small numbers should not wrap (even if they're out of order).
474 for (uint64 j = 0; j < 10; j++) {
475 CheckCalculatePacketSequenceNumber(j, last);
478 // Large numbers should not wrap either (because we're near 0 already).
479 for (uint64 j = 0; j < 10; j++) {
480 CheckCalculatePacketSequenceNumber(kEpoch - 1 - j, last);
485 TEST_P(QuicFramerTest, CalculatePacketSequenceNumberFromWireNearEpochEnd) {
486 // Cases where the last number was close to the end of the range
487 for (uint64 i = 0; i < 10; i++) {
488 QuicPacketSequenceNumber last = kEpoch - i;
490 // Small numbers should wrap.
491 for (uint64 j = 0; j < 10; j++) {
492 CheckCalculatePacketSequenceNumber(kEpoch + j, last);
495 // Large numbers should not (even if they're out of order).
496 for (uint64 j = 0; j < 10; j++) {
497 CheckCalculatePacketSequenceNumber(kEpoch - 1 - j, last);
502 // Next check where we're in a non-zero epoch to verify we handle
503 // reverse wrapping, too.
504 TEST_P(QuicFramerTest, CalculatePacketSequenceNumberFromWireNearPrevEpoch) {
505 const uint64 prev_epoch = 1 * kEpoch;
506 const uint64 cur_epoch = 2 * kEpoch;
507 // Cases where the last number was close to the start of the range
508 for (uint64 i = 0; i < 10; i++) {
509 uint64 last = cur_epoch + i;
510 // Small number should not wrap (even if they're out of order).
511 for (uint64 j = 0; j < 10; j++) {
512 CheckCalculatePacketSequenceNumber(cur_epoch + j, last);
515 // But large numbers should reverse wrap.
516 for (uint64 j = 0; j < 10; j++) {
517 uint64 num = kEpoch - 1 - j;
518 CheckCalculatePacketSequenceNumber(prev_epoch + num, last);
523 TEST_P(QuicFramerTest, CalculatePacketSequenceNumberFromWireNearNextEpoch) {
524 const uint64 cur_epoch = 2 * kEpoch;
525 const uint64 next_epoch = 3 * kEpoch;
526 // Cases where the last number was close to the end of the range
527 for (uint64 i = 0; i < 10; i++) {
528 QuicPacketSequenceNumber last = next_epoch - 1 - i;
530 // Small numbers should wrap.
531 for (uint64 j = 0; j < 10; j++) {
532 CheckCalculatePacketSequenceNumber(next_epoch + j, last);
535 // but large numbers should not (even if they're out of order).
536 for (uint64 j = 0; j < 10; j++) {
537 uint64 num = kEpoch - 1 - j;
538 CheckCalculatePacketSequenceNumber(cur_epoch + num, last);
543 TEST_P(QuicFramerTest, CalculatePacketSequenceNumberFromWireNearNextMax) {
544 const uint64 max_number = numeric_limits<uint64>::max();
545 const uint64 max_epoch = max_number & ~kMask;
547 // Cases where the last number was close to the end of the range
548 for (uint64 i = 0; i < 10; i++) {
549 // Subtract 1, because the expected next sequence number is 1 more than the
550 // last sequence number.
551 QuicPacketSequenceNumber last = max_number - i - 1;
553 // Small numbers should not wrap, because they have nowhere to go.
554 for (uint64 j = 0; j < 10; j++) {
555 CheckCalculatePacketSequenceNumber(max_epoch + j, last);
558 // Large numbers should not wrap either.
559 for (uint64 j = 0; j < 10; j++) {
560 uint64 num = kEpoch - 1 - j;
561 CheckCalculatePacketSequenceNumber(max_epoch + num, last);
566 TEST_P(QuicFramerTest, EmptyPacket) {
567 char packet[] = { 0x00 };
568 QuicEncryptedPacket encrypted(packet, 0, false);
569 EXPECT_FALSE(framer_.ProcessPacket(encrypted));
570 EXPECT_EQ(QUIC_INVALID_PACKET_HEADER, framer_.error());
573 TEST_P(QuicFramerTest, LargePacket) {
574 unsigned char packet[kMaxPacketSize + 1] = {
575 // public flags (8 byte guid)
576 0x3C,
577 // guid
578 0x10, 0x32, 0x54, 0x76,
579 0x98, 0xBA, 0xDC, 0xFE,
580 // packet sequence number
581 0xBC, 0x9A, 0x78, 0x56,
582 0x34, 0x12,
583 // private flags
584 0x00,
587 memset(packet + GetPacketHeaderSize(
588 PACKET_8BYTE_GUID, !kIncludeVersion,
589 PACKET_6BYTE_SEQUENCE_NUMBER, NOT_IN_FEC_GROUP), 0,
590 kMaxPacketSize - GetPacketHeaderSize(
591 PACKET_8BYTE_GUID, !kIncludeVersion,
592 PACKET_6BYTE_SEQUENCE_NUMBER, NOT_IN_FEC_GROUP) + 1);
594 QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
595 EXPECT_FALSE(framer_.ProcessPacket(encrypted));
597 ASSERT_TRUE(visitor_.header_.get());
598 // Make sure we've parsed the packet header, so we can send an error.
599 EXPECT_EQ(GG_UINT64_C(0xFEDCBA9876543210),
600 visitor_.header_->public_header.guid);
601 // Make sure the correct error is propagated.
602 EXPECT_EQ(QUIC_PACKET_TOO_LARGE, framer_.error());
605 TEST_P(QuicFramerTest, PacketHeader) {
606 unsigned char packet[] = {
607 // public flags (8 byte guid)
608 0x3C,
609 // guid
610 0x10, 0x32, 0x54, 0x76,
611 0x98, 0xBA, 0xDC, 0xFE,
612 // packet sequence number
613 0xBC, 0x9A, 0x78, 0x56,
614 0x34, 0x12,
615 // private flags
616 0x00,
619 QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
620 EXPECT_FALSE(framer_.ProcessPacket(encrypted));
621 EXPECT_EQ(QUIC_MISSING_PAYLOAD, framer_.error());
622 ASSERT_TRUE(visitor_.header_.get());
623 EXPECT_EQ(GG_UINT64_C(0xFEDCBA9876543210),
624 visitor_.header_->public_header.guid);
625 EXPECT_FALSE(visitor_.header_->public_header.reset_flag);
626 EXPECT_FALSE(visitor_.header_->public_header.version_flag);
627 EXPECT_FALSE(visitor_.header_->fec_flag);
628 EXPECT_FALSE(visitor_.header_->entropy_flag);
629 EXPECT_EQ(0, visitor_.header_->entropy_hash);
630 EXPECT_EQ(GG_UINT64_C(0x123456789ABC),
631 visitor_.header_->packet_sequence_number);
632 EXPECT_EQ(NOT_IN_FEC_GROUP, visitor_.header_->is_in_fec_group);
633 EXPECT_EQ(0x00u, visitor_.header_->fec_group);
635 // Now test framing boundaries
636 for (size_t i = 0;
637 i < GetPacketHeaderSize(PACKET_8BYTE_GUID, !kIncludeVersion,
638 PACKET_6BYTE_SEQUENCE_NUMBER, NOT_IN_FEC_GROUP);
639 ++i) {
640 string expected_error;
641 if (i < kGuidOffset) {
642 expected_error = "Unable to read public flags.";
643 } else if (i < GetSequenceNumberOffset(!kIncludeVersion)) {
644 expected_error = "Unable to read GUID.";
645 } else if (i < GetPrivateFlagsOffset(!kIncludeVersion)) {
646 expected_error = "Unable to read sequence number.";
647 } else if (i < GetFecGroupOffset(!kIncludeVersion)) {
648 expected_error = "Unable to read private flags.";
649 } else {
650 expected_error = "Unable to read first fec protected packet offset.";
652 CheckProcessingFails(packet, i, expected_error, QUIC_INVALID_PACKET_HEADER);
656 TEST_P(QuicFramerTest, PacketHeaderWith4ByteGuid) {
657 QuicFramerPeer::SetLastSerializedGuid(&framer_,
658 GG_UINT64_C(0xFEDCBA9876543210));
660 unsigned char packet[] = {
661 // public flags (4 byte guid)
662 0x38,
663 // guid
664 0x10, 0x32, 0x54, 0x76,
665 // packet sequence number
666 0xBC, 0x9A, 0x78, 0x56,
667 0x34, 0x12,
668 // private flags
669 0x00,
672 QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
673 EXPECT_FALSE(framer_.ProcessPacket(encrypted));
674 EXPECT_EQ(QUIC_MISSING_PAYLOAD, framer_.error());
675 ASSERT_TRUE(visitor_.header_.get());
676 EXPECT_EQ(GG_UINT64_C(0xFEDCBA9876543210),
677 visitor_.header_->public_header.guid);
678 EXPECT_FALSE(visitor_.header_->public_header.reset_flag);
679 EXPECT_FALSE(visitor_.header_->public_header.version_flag);
680 EXPECT_FALSE(visitor_.header_->fec_flag);
681 EXPECT_FALSE(visitor_.header_->entropy_flag);
682 EXPECT_EQ(0, visitor_.header_->entropy_hash);
683 EXPECT_EQ(GG_UINT64_C(0x123456789ABC),
684 visitor_.header_->packet_sequence_number);
685 EXPECT_EQ(NOT_IN_FEC_GROUP, visitor_.header_->is_in_fec_group);
686 EXPECT_EQ(0x00u, visitor_.header_->fec_group);
688 // Now test framing boundaries
689 for (size_t i = 0;
690 i < GetPacketHeaderSize(PACKET_4BYTE_GUID, !kIncludeVersion,
691 PACKET_6BYTE_SEQUENCE_NUMBER, NOT_IN_FEC_GROUP);
692 ++i) {
693 string expected_error;
694 if (i < kGuidOffset) {
695 expected_error = "Unable to read public flags.";
696 } else if (i < GetSequenceNumberOffset(PACKET_4BYTE_GUID,
697 !kIncludeVersion)) {
698 expected_error = "Unable to read GUID.";
699 } else if (i < GetPrivateFlagsOffset(PACKET_4BYTE_GUID,
700 !kIncludeVersion)) {
701 expected_error = "Unable to read sequence number.";
702 } else if (i < GetFecGroupOffset(PACKET_4BYTE_GUID, !kIncludeVersion)) {
703 expected_error = "Unable to read private flags.";
704 } else {
705 expected_error = "Unable to read first fec protected packet offset.";
707 CheckProcessingFails(packet, i, expected_error, QUIC_INVALID_PACKET_HEADER);
711 TEST_P(QuicFramerTest, PacketHeader1ByteGuid) {
712 QuicFramerPeer::SetLastSerializedGuid(&framer_,
713 GG_UINT64_C(0xFEDCBA9876543210));
715 unsigned char packet[] = {
716 // public flags (1 byte guid)
717 0x34,
718 // guid
719 0x10,
720 // packet sequence number
721 0xBC, 0x9A, 0x78, 0x56,
722 0x34, 0x12,
723 // private flags
724 0x00,
727 QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
728 EXPECT_FALSE(framer_.ProcessPacket(encrypted));
729 EXPECT_EQ(QUIC_MISSING_PAYLOAD, framer_.error());
730 ASSERT_TRUE(visitor_.header_.get());
731 EXPECT_EQ(GG_UINT64_C(0xFEDCBA9876543210),
732 visitor_.header_->public_header.guid);
733 EXPECT_FALSE(visitor_.header_->public_header.reset_flag);
734 EXPECT_FALSE(visitor_.header_->public_header.version_flag);
735 EXPECT_FALSE(visitor_.header_->fec_flag);
736 EXPECT_FALSE(visitor_.header_->entropy_flag);
737 EXPECT_EQ(0, visitor_.header_->entropy_hash);
738 EXPECT_EQ(GG_UINT64_C(0x123456789ABC),
739 visitor_.header_->packet_sequence_number);
740 EXPECT_EQ(NOT_IN_FEC_GROUP, visitor_.header_->is_in_fec_group);
741 EXPECT_EQ(0x00u, visitor_.header_->fec_group);
743 // Now test framing boundaries
744 for (size_t i = 0;
745 i < GetPacketHeaderSize(PACKET_1BYTE_GUID, !kIncludeVersion,
746 PACKET_6BYTE_SEQUENCE_NUMBER, NOT_IN_FEC_GROUP);
747 ++i) {
748 string expected_error;
749 if (i < kGuidOffset) {
750 expected_error = "Unable to read public flags.";
751 } else if (i < GetSequenceNumberOffset(PACKET_1BYTE_GUID,
752 !kIncludeVersion)) {
753 expected_error = "Unable to read GUID.";
754 } else if (i < GetPrivateFlagsOffset(PACKET_1BYTE_GUID, !kIncludeVersion)) {
755 expected_error = "Unable to read sequence number.";
756 } else if (i < GetFecGroupOffset(PACKET_1BYTE_GUID, !kIncludeVersion)) {
757 expected_error = "Unable to read private flags.";
758 } else {
759 expected_error = "Unable to read first fec protected packet offset.";
761 CheckProcessingFails(packet, i, expected_error, QUIC_INVALID_PACKET_HEADER);
765 TEST_P(QuicFramerTest, PacketHeaderWith0ByteGuid) {
766 QuicFramerPeer::SetLastSerializedGuid(&framer_,
767 GG_UINT64_C(0xFEDCBA9876543210));
769 unsigned char packet[] = {
770 // public flags (0 byte guid)
771 0x30,
772 // guid
773 // packet sequence number
774 0xBC, 0x9A, 0x78, 0x56,
775 0x34, 0x12,
776 // private flags
777 0x00,
780 QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
781 EXPECT_FALSE(framer_.ProcessPacket(encrypted));
782 EXPECT_EQ(QUIC_MISSING_PAYLOAD, framer_.error());
783 ASSERT_TRUE(visitor_.header_.get());
784 EXPECT_EQ(GG_UINT64_C(0xFEDCBA9876543210),
785 visitor_.header_->public_header.guid);
786 EXPECT_FALSE(visitor_.header_->public_header.reset_flag);
787 EXPECT_FALSE(visitor_.header_->public_header.version_flag);
788 EXPECT_FALSE(visitor_.header_->fec_flag);
789 EXPECT_FALSE(visitor_.header_->entropy_flag);
790 EXPECT_EQ(0, visitor_.header_->entropy_hash);
791 EXPECT_EQ(GG_UINT64_C(0x123456789ABC),
792 visitor_.header_->packet_sequence_number);
793 EXPECT_EQ(NOT_IN_FEC_GROUP, visitor_.header_->is_in_fec_group);
794 EXPECT_EQ(0x00u, visitor_.header_->fec_group);
796 // Now test framing boundaries
797 for (size_t i = 0;
798 i < GetPacketHeaderSize(PACKET_0BYTE_GUID, !kIncludeVersion,
799 PACKET_6BYTE_SEQUENCE_NUMBER, NOT_IN_FEC_GROUP);
800 ++i) {
801 string expected_error;
802 if (i < kGuidOffset) {
803 expected_error = "Unable to read public flags.";
804 } else if (i < GetSequenceNumberOffset(PACKET_0BYTE_GUID,
805 !kIncludeVersion)) {
806 expected_error = "Unable to read GUID.";
807 } else if (i < GetPrivateFlagsOffset(PACKET_0BYTE_GUID, !kIncludeVersion)) {
808 expected_error = "Unable to read sequence number.";
809 } else if (i < GetFecGroupOffset(PACKET_0BYTE_GUID, !kIncludeVersion)) {
810 expected_error = "Unable to read private flags.";
811 } else {
812 expected_error = "Unable to read first fec protected packet offset.";
814 CheckProcessingFails(packet, i, expected_error, QUIC_INVALID_PACKET_HEADER);
818 TEST_P(QuicFramerTest, PacketHeaderWithVersionFlag) {
819 unsigned char packet[] = {
820 // public flags (version)
821 0x3D,
822 // guid
823 0x10, 0x32, 0x54, 0x76,
824 0x98, 0xBA, 0xDC, 0xFE,
825 // version tag
826 'Q', '0', GetQuicVersionDigitTens(), GetQuicVersionDigitOnes(),
827 // packet sequence number
828 0xBC, 0x9A, 0x78, 0x56,
829 0x34, 0x12,
830 // private flags
831 0x00,
834 QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
835 EXPECT_FALSE(framer_.ProcessPacket(encrypted));
836 EXPECT_EQ(QUIC_MISSING_PAYLOAD, framer_.error());
837 ASSERT_TRUE(visitor_.header_.get());
838 EXPECT_EQ(GG_UINT64_C(0xFEDCBA9876543210),
839 visitor_.header_->public_header.guid);
840 EXPECT_FALSE(visitor_.header_->public_header.reset_flag);
841 EXPECT_TRUE(visitor_.header_->public_header.version_flag);
842 EXPECT_EQ(GetParam(), visitor_.header_->public_header.versions[0]);
843 EXPECT_FALSE(visitor_.header_->fec_flag);
844 EXPECT_FALSE(visitor_.header_->entropy_flag);
845 EXPECT_EQ(0, visitor_.header_->entropy_hash);
846 EXPECT_EQ(GG_UINT64_C(0x123456789ABC),
847 visitor_.header_->packet_sequence_number);
848 EXPECT_EQ(NOT_IN_FEC_GROUP, visitor_.header_->is_in_fec_group);
849 EXPECT_EQ(0x00u, visitor_.header_->fec_group);
851 // Now test framing boundaries
852 for (size_t i = 0;
853 i < GetPacketHeaderSize(PACKET_8BYTE_GUID, kIncludeVersion,
854 PACKET_6BYTE_SEQUENCE_NUMBER, NOT_IN_FEC_GROUP);
855 ++i) {
856 string expected_error;
857 if (i < kGuidOffset) {
858 expected_error = "Unable to read public flags.";
859 } else if (i < kVersionOffset) {
860 expected_error = "Unable to read GUID.";
861 } else if (i < GetSequenceNumberOffset(kIncludeVersion)) {
862 expected_error = "Unable to read protocol version.";
863 } else if (i < GetPrivateFlagsOffset(kIncludeVersion)) {
864 expected_error = "Unable to read sequence number.";
865 } else if (i < GetFecGroupOffset(kIncludeVersion)) {
866 expected_error = "Unable to read private flags.";
867 } else {
868 expected_error = "Unable to read first fec protected packet offset.";
870 CheckProcessingFails(packet, i, expected_error, QUIC_INVALID_PACKET_HEADER);
874 TEST_P(QuicFramerTest, PacketHeaderWith4ByteSequenceNumber) {
875 QuicFramerPeer::SetLastSequenceNumber(&framer_,
876 GG_UINT64_C(0x123456789ABA));
878 unsigned char packet[] = {
879 // public flags (8 byte guid and 4 byte sequence number)
880 0x2C,
881 // guid
882 0x10, 0x32, 0x54, 0x76,
883 0x98, 0xBA, 0xDC, 0xFE,
884 // packet sequence number
885 0xBC, 0x9A, 0x78, 0x56,
886 // private flags
887 0x00,
890 QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
891 EXPECT_FALSE(framer_.ProcessPacket(encrypted));
892 EXPECT_EQ(QUIC_MISSING_PAYLOAD, framer_.error());
893 ASSERT_TRUE(visitor_.header_.get());
894 EXPECT_EQ(GG_UINT64_C(0xFEDCBA9876543210),
895 visitor_.header_->public_header.guid);
896 EXPECT_FALSE(visitor_.header_->public_header.reset_flag);
897 EXPECT_FALSE(visitor_.header_->public_header.version_flag);
898 EXPECT_FALSE(visitor_.header_->fec_flag);
899 EXPECT_FALSE(visitor_.header_->entropy_flag);
900 EXPECT_EQ(0, visitor_.header_->entropy_hash);
901 EXPECT_EQ(GG_UINT64_C(0x123456789ABC),
902 visitor_.header_->packet_sequence_number);
903 EXPECT_EQ(NOT_IN_FEC_GROUP, visitor_.header_->is_in_fec_group);
904 EXPECT_EQ(0x00u, visitor_.header_->fec_group);
906 // Now test framing boundaries
907 for (size_t i = 0;
908 i < GetPacketHeaderSize(PACKET_8BYTE_GUID, !kIncludeVersion,
909 PACKET_4BYTE_SEQUENCE_NUMBER, NOT_IN_FEC_GROUP);
910 ++i) {
911 string expected_error;
912 if (i < kGuidOffset) {
913 expected_error = "Unable to read public flags.";
914 } else if (i < GetSequenceNumberOffset(!kIncludeVersion)) {
915 expected_error = "Unable to read GUID.";
916 } else if (i < GetPrivateFlagsOffset(!kIncludeVersion,
917 PACKET_4BYTE_SEQUENCE_NUMBER)) {
918 expected_error = "Unable to read sequence number.";
919 } else if (i < GetFecGroupOffset(!kIncludeVersion,
920 PACKET_4BYTE_SEQUENCE_NUMBER)) {
921 expected_error = "Unable to read private flags.";
922 } else {
923 expected_error = "Unable to read first fec protected packet offset.";
925 CheckProcessingFails(packet, i, expected_error, QUIC_INVALID_PACKET_HEADER);
929 TEST_P(QuicFramerTest, PacketHeaderWith2ByteSequenceNumber) {
930 QuicFramerPeer::SetLastSequenceNumber(&framer_,
931 GG_UINT64_C(0x123456789ABA));
933 unsigned char packet[] = {
934 // public flags (8 byte guid and 2 byte sequence number)
935 0x1C,
936 // guid
937 0x10, 0x32, 0x54, 0x76,
938 0x98, 0xBA, 0xDC, 0xFE,
939 // packet sequence number
940 0xBC, 0x9A,
941 // private flags
942 0x00,
945 QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
946 EXPECT_FALSE(framer_.ProcessPacket(encrypted));
947 EXPECT_EQ(QUIC_MISSING_PAYLOAD, framer_.error());
948 ASSERT_TRUE(visitor_.header_.get());
949 EXPECT_EQ(GG_UINT64_C(0xFEDCBA9876543210),
950 visitor_.header_->public_header.guid);
951 EXPECT_FALSE(visitor_.header_->public_header.reset_flag);
952 EXPECT_FALSE(visitor_.header_->public_header.version_flag);
953 EXPECT_FALSE(visitor_.header_->fec_flag);
954 EXPECT_FALSE(visitor_.header_->entropy_flag);
955 EXPECT_EQ(0, visitor_.header_->entropy_hash);
956 EXPECT_EQ(GG_UINT64_C(0x123456789ABC),
957 visitor_.header_->packet_sequence_number);
958 EXPECT_EQ(NOT_IN_FEC_GROUP, visitor_.header_->is_in_fec_group);
959 EXPECT_EQ(0x00u, visitor_.header_->fec_group);
961 // Now test framing boundaries
962 for (size_t i = 0;
963 i < GetPacketHeaderSize(PACKET_8BYTE_GUID, !kIncludeVersion,
964 PACKET_2BYTE_SEQUENCE_NUMBER, NOT_IN_FEC_GROUP);
965 ++i) {
966 string expected_error;
967 if (i < kGuidOffset) {
968 expected_error = "Unable to read public flags.";
969 } else if (i < GetSequenceNumberOffset(!kIncludeVersion)) {
970 expected_error = "Unable to read GUID.";
971 } else if (i < GetPrivateFlagsOffset(!kIncludeVersion,
972 PACKET_2BYTE_SEQUENCE_NUMBER)) {
973 expected_error = "Unable to read sequence number.";
974 } else if (i < GetFecGroupOffset(!kIncludeVersion,
975 PACKET_2BYTE_SEQUENCE_NUMBER)) {
976 expected_error = "Unable to read private flags.";
977 } else {
978 expected_error = "Unable to read first fec protected packet offset.";
980 CheckProcessingFails(packet, i, expected_error, QUIC_INVALID_PACKET_HEADER);
984 TEST_P(QuicFramerTest, PacketHeaderWith1ByteSequenceNumber) {
985 QuicFramerPeer::SetLastSequenceNumber(&framer_,
986 GG_UINT64_C(0x123456789ABA));
988 unsigned char packet[] = {
989 // public flags (8 byte guid and 1 byte sequence number)
990 0x0C,
991 // guid
992 0x10, 0x32, 0x54, 0x76,
993 0x98, 0xBA, 0xDC, 0xFE,
994 // packet sequence number
995 0xBC,
996 // private flags
997 0x00,
1000 QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
1001 EXPECT_FALSE(framer_.ProcessPacket(encrypted));
1002 EXPECT_EQ(QUIC_MISSING_PAYLOAD, framer_.error());
1003 ASSERT_TRUE(visitor_.header_.get());
1004 EXPECT_EQ(GG_UINT64_C(0xFEDCBA9876543210),
1005 visitor_.header_->public_header.guid);
1006 EXPECT_FALSE(visitor_.header_->public_header.reset_flag);
1007 EXPECT_FALSE(visitor_.header_->public_header.version_flag);
1008 EXPECT_FALSE(visitor_.header_->fec_flag);
1009 EXPECT_FALSE(visitor_.header_->entropy_flag);
1010 EXPECT_EQ(0, visitor_.header_->entropy_hash);
1011 EXPECT_EQ(GG_UINT64_C(0x123456789ABC),
1012 visitor_.header_->packet_sequence_number);
1013 EXPECT_EQ(NOT_IN_FEC_GROUP, visitor_.header_->is_in_fec_group);
1014 EXPECT_EQ(0x00u, visitor_.header_->fec_group);
1016 // Now test framing boundaries
1017 for (size_t i = 0;
1018 i < GetPacketHeaderSize(PACKET_8BYTE_GUID, !kIncludeVersion,
1019 PACKET_1BYTE_SEQUENCE_NUMBER, NOT_IN_FEC_GROUP);
1020 ++i) {
1021 string expected_error;
1022 if (i < kGuidOffset) {
1023 expected_error = "Unable to read public flags.";
1024 } else if (i < GetSequenceNumberOffset(!kIncludeVersion)) {
1025 expected_error = "Unable to read GUID.";
1026 } else if (i < GetPrivateFlagsOffset(!kIncludeVersion,
1027 PACKET_1BYTE_SEQUENCE_NUMBER)) {
1028 expected_error = "Unable to read sequence number.";
1029 } else if (i < GetFecGroupOffset(!kIncludeVersion,
1030 PACKET_1BYTE_SEQUENCE_NUMBER)) {
1031 expected_error = "Unable to read private flags.";
1032 } else {
1033 expected_error = "Unable to read first fec protected packet offset.";
1035 CheckProcessingFails(packet, i, expected_error, QUIC_INVALID_PACKET_HEADER);
1039 TEST_P(QuicFramerTest, InvalidPublicFlag) {
1040 unsigned char packet[] = {
1041 // public flags, unknown flag at bit 6
1042 0x40,
1043 // guid
1044 0x10, 0x32, 0x54, 0x76,
1045 0x98, 0xBA, 0xDC, 0xFE,
1046 // packet sequence number
1047 0xBC, 0x9A, 0x78, 0x56,
1048 0x34, 0x12,
1049 // private flags
1050 0x00,
1052 // frame type (padding)
1053 0x00,
1054 0x00, 0x00, 0x00, 0x00
1056 CheckProcessingFails(packet,
1057 arraysize(packet),
1058 "Illegal public flags value.",
1059 QUIC_INVALID_PACKET_HEADER);
1062 TEST_P(QuicFramerTest, InvalidPublicFlagWithMatchingVersions) {
1063 unsigned char packet[] = {
1064 // public flags (8 byte guid and version flag and an unknown flag)
1065 0x4D,
1066 // guid
1067 0x10, 0x32, 0x54, 0x76,
1068 0x98, 0xBA, 0xDC, 0xFE,
1069 // version tag
1070 'Q', '0', GetQuicVersionDigitTens(), GetQuicVersionDigitOnes(),
1071 // packet sequence number
1072 0xBC, 0x9A, 0x78, 0x56,
1073 0x34, 0x12,
1074 // private flags
1075 0x00,
1077 // frame type (padding)
1078 0x00,
1079 0x00, 0x00, 0x00, 0x00
1081 CheckProcessingFails(packet,
1082 arraysize(packet),
1083 "Illegal public flags value.",
1084 QUIC_INVALID_PACKET_HEADER);
1087 TEST_P(QuicFramerTest, LargePublicFlagWithMismatchedVersions) {
1088 unsigned char packet[] = {
1089 // public flags (8 byte guid, version flag and an unknown flag)
1090 0x7D,
1091 // guid
1092 0x10, 0x32, 0x54, 0x76,
1093 0x98, 0xBA, 0xDC, 0xFE,
1094 // version tag
1095 'Q', '0', '0', '0',
1096 // packet sequence number
1097 0xBC, 0x9A, 0x78, 0x56,
1098 0x34, 0x12,
1099 // private flags
1100 0x00,
1102 // frame type (padding frame)
1103 0x00,
1104 0x00, 0x00, 0x00, 0x00
1106 QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
1107 EXPECT_TRUE(framer_.ProcessPacket(encrypted));
1108 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
1109 ASSERT_TRUE(visitor_.header_.get());
1110 EXPECT_EQ(0, visitor_.frame_count_);
1111 EXPECT_EQ(1, visitor_.version_mismatch_);
1114 TEST_P(QuicFramerTest, InvalidPrivateFlag) {
1115 unsigned char packet[] = {
1116 // public flags (8 byte guid)
1117 0x3C,
1118 // guid
1119 0x10, 0x32, 0x54, 0x76,
1120 0x98, 0xBA, 0xDC, 0xFE,
1121 // packet sequence number
1122 0xBC, 0x9A, 0x78, 0x56,
1123 0x34, 0x12,
1124 // private flags
1125 0x10,
1127 // frame type (padding)
1128 0x00,
1129 0x00, 0x00, 0x00, 0x00
1131 CheckProcessingFails(packet,
1132 arraysize(packet),
1133 "Illegal private flags value.",
1134 QUIC_INVALID_PACKET_HEADER);
1137 TEST_P(QuicFramerTest, InvalidFECGroupOffset) {
1138 unsigned char packet[] = {
1139 // public flags (8 byte guid)
1140 0x3C,
1141 // guid
1142 0x10, 0x32, 0x54, 0x76,
1143 0x98, 0xBA, 0xDC, 0xFE,
1144 // packet sequence number
1145 0x01, 0x00, 0x00, 0x00,
1146 0x00, 0x00,
1147 // private flags (fec group)
1148 0x02,
1149 // first fec protected packet offset
1150 0x10
1152 CheckProcessingFails(packet,
1153 arraysize(packet),
1154 "First fec protected packet offset must be less "
1155 "than the sequence number.",
1156 QUIC_INVALID_PACKET_HEADER);
1159 TEST_P(QuicFramerTest, PaddingFrame) {
1160 unsigned char packet[] = {
1161 // public flags (8 byte guid)
1162 0x3C,
1163 // guid
1164 0x10, 0x32, 0x54, 0x76,
1165 0x98, 0xBA, 0xDC, 0xFE,
1166 // packet sequence number
1167 0xBC, 0x9A, 0x78, 0x56,
1168 0x34, 0x12,
1169 // private flags
1170 0x00,
1172 // frame type (padding frame)
1173 0x00,
1174 // Ignored data (which in this case is a stream frame)
1175 // frame type (stream frame with fin)
1176 0xFF,
1177 // stream id
1178 0x04, 0x03, 0x02, 0x01,
1179 // offset
1180 0x54, 0x76, 0x10, 0x32,
1181 0xDC, 0xFE, 0x98, 0xBA,
1182 // data length
1183 0x0c, 0x00,
1184 // data
1185 'h', 'e', 'l', 'l',
1186 'o', ' ', 'w', 'o',
1187 'r', 'l', 'd', '!',
1190 QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
1191 EXPECT_TRUE(framer_.ProcessPacket(encrypted));
1192 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
1193 ASSERT_TRUE(visitor_.header_.get());
1194 EXPECT_TRUE(CheckDecryption(encrypted, !kIncludeVersion));
1196 ASSERT_EQ(0u, visitor_.stream_frames_.size());
1197 EXPECT_EQ(0u, visitor_.ack_frames_.size());
1198 // A packet with no frames is not acceptable.
1199 CheckProcessingFails(
1200 packet,
1201 GetPacketHeaderSize(PACKET_8BYTE_GUID, !kIncludeVersion,
1202 PACKET_6BYTE_SEQUENCE_NUMBER, NOT_IN_FEC_GROUP),
1203 "Packet has no frames.", QUIC_MISSING_PAYLOAD);
1206 TEST_P(QuicFramerTest, StreamFrame) {
1207 unsigned char packet[] = {
1208 // public flags (8 byte guid)
1209 0x3C,
1210 // guid
1211 0x10, 0x32, 0x54, 0x76,
1212 0x98, 0xBA, 0xDC, 0xFE,
1213 // packet sequence number
1214 0xBC, 0x9A, 0x78, 0x56,
1215 0x34, 0x12,
1216 // private flags
1217 0x00,
1219 // frame type (stream frame with fin)
1220 0xFF,
1221 // stream id
1222 0x04, 0x03, 0x02, 0x01,
1223 // offset
1224 0x54, 0x76, 0x10, 0x32,
1225 0xDC, 0xFE, 0x98, 0xBA,
1226 // data length
1227 0x0c, 0x00,
1228 // data
1229 'h', 'e', 'l', 'l',
1230 'o', ' ', 'w', 'o',
1231 'r', 'l', 'd', '!',
1234 QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
1235 EXPECT_TRUE(framer_.ProcessPacket(encrypted));
1237 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
1238 ASSERT_TRUE(visitor_.header_.get());
1239 EXPECT_TRUE(CheckDecryption(encrypted, !kIncludeVersion));
1241 ASSERT_EQ(1u, visitor_.stream_frames_.size());
1242 EXPECT_EQ(0u, visitor_.ack_frames_.size());
1243 EXPECT_EQ(static_cast<uint64>(0x01020304),
1244 visitor_.stream_frames_[0]->stream_id);
1245 EXPECT_TRUE(visitor_.stream_frames_[0]->fin);
1246 EXPECT_EQ(GG_UINT64_C(0xBA98FEDC32107654),
1247 visitor_.stream_frames_[0]->offset);
1248 CheckStreamFrameData("hello world!", visitor_.stream_frames_[0]);
1250 // Now test framing boundaries
1251 CheckStreamFrameBoundaries(packet, kQuicMaxStreamIdSize, !kIncludeVersion);
1254 TEST_P(QuicFramerTest, StreamFrame3ByteStreamId) {
1255 unsigned char packet[] = {
1256 // public flags (8 byte guid)
1257 0x3C,
1258 // guid
1259 0x10, 0x32, 0x54, 0x76,
1260 0x98, 0xBA, 0xDC, 0xFE,
1261 // packet sequence number
1262 0xBC, 0x9A, 0x78, 0x56,
1263 0x34, 0x12,
1264 // private flags
1265 0x00,
1267 // frame type (stream frame with fin)
1268 0xFE,
1269 // stream id
1270 0x04, 0x03, 0x02,
1271 // offset
1272 0x54, 0x76, 0x10, 0x32,
1273 0xDC, 0xFE, 0x98, 0xBA,
1274 // data length
1275 0x0c, 0x00,
1276 // data
1277 'h', 'e', 'l', 'l',
1278 'o', ' ', 'w', 'o',
1279 'r', 'l', 'd', '!',
1282 QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
1283 EXPECT_TRUE(framer_.ProcessPacket(encrypted));
1285 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
1286 ASSERT_TRUE(visitor_.header_.get());
1287 EXPECT_TRUE(CheckDecryption(encrypted, !kIncludeVersion));
1289 ASSERT_EQ(1u, visitor_.stream_frames_.size());
1290 EXPECT_EQ(0u, visitor_.ack_frames_.size());
1291 EXPECT_EQ(GG_UINT64_C(0x00020304),
1292 visitor_.stream_frames_[0]->stream_id);
1293 EXPECT_TRUE(visitor_.stream_frames_[0]->fin);
1294 EXPECT_EQ(GG_UINT64_C(0xBA98FEDC32107654),
1295 visitor_.stream_frames_[0]->offset);
1296 CheckStreamFrameData("hello world!", visitor_.stream_frames_[0]);
1298 // Now test framing boundaries
1299 const size_t stream_id_size = 3;
1300 CheckStreamFrameBoundaries(packet, stream_id_size, !kIncludeVersion);
1303 TEST_P(QuicFramerTest, StreamFrame2ByteStreamId) {
1304 unsigned char packet[] = {
1305 // public flags (8 byte guid)
1306 0x3C,
1307 // guid
1308 0x10, 0x32, 0x54, 0x76,
1309 0x98, 0xBA, 0xDC, 0xFE,
1310 // packet sequence number
1311 0xBC, 0x9A, 0x78, 0x56,
1312 0x34, 0x12,
1313 // private flags
1314 0x00,
1316 // frame type (stream frame with fin)
1317 0xFD,
1318 // stream id
1319 0x04, 0x03,
1320 // offset
1321 0x54, 0x76, 0x10, 0x32,
1322 0xDC, 0xFE, 0x98, 0xBA,
1323 // data length
1324 0x0c, 0x00,
1325 // data
1326 'h', 'e', 'l', 'l',
1327 'o', ' ', 'w', 'o',
1328 'r', 'l', 'd', '!',
1331 QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
1332 EXPECT_TRUE(framer_.ProcessPacket(encrypted));
1334 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
1335 ASSERT_TRUE(visitor_.header_.get());
1336 EXPECT_TRUE(CheckDecryption(encrypted, !kIncludeVersion));
1338 ASSERT_EQ(1u, visitor_.stream_frames_.size());
1339 EXPECT_EQ(0u, visitor_.ack_frames_.size());
1340 EXPECT_EQ(static_cast<uint64>(0x00000304),
1341 visitor_.stream_frames_[0]->stream_id);
1342 EXPECT_TRUE(visitor_.stream_frames_[0]->fin);
1343 EXPECT_EQ(GG_UINT64_C(0xBA98FEDC32107654),
1344 visitor_.stream_frames_[0]->offset);
1345 CheckStreamFrameData("hello world!", visitor_.stream_frames_[0]);
1347 // Now test framing boundaries
1348 const size_t stream_id_size = 2;
1349 CheckStreamFrameBoundaries(packet, stream_id_size, !kIncludeVersion);
1352 TEST_P(QuicFramerTest, StreamFrame1ByteStreamId) {
1353 unsigned char packet[] = {
1354 // public flags (8 byte guid)
1355 0x3C,
1356 // guid
1357 0x10, 0x32, 0x54, 0x76,
1358 0x98, 0xBA, 0xDC, 0xFE,
1359 // packet sequence number
1360 0xBC, 0x9A, 0x78, 0x56,
1361 0x34, 0x12,
1362 // private flags
1363 0x00,
1365 // frame type (stream frame with fin)
1366 0xFC,
1367 // stream id
1368 0x04,
1369 // offset
1370 0x54, 0x76, 0x10, 0x32,
1371 0xDC, 0xFE, 0x98, 0xBA,
1372 // data length
1373 0x0c, 0x00,
1374 // data
1375 'h', 'e', 'l', 'l',
1376 'o', ' ', 'w', 'o',
1377 'r', 'l', 'd', '!',
1380 QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
1381 EXPECT_TRUE(framer_.ProcessPacket(encrypted));
1383 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
1384 ASSERT_TRUE(visitor_.header_.get());
1385 EXPECT_TRUE(CheckDecryption(encrypted, !kIncludeVersion));
1387 ASSERT_EQ(1u, visitor_.stream_frames_.size());
1388 EXPECT_EQ(0u, visitor_.ack_frames_.size());
1389 EXPECT_EQ(static_cast<uint64>(0x00000004),
1390 visitor_.stream_frames_[0]->stream_id);
1391 EXPECT_TRUE(visitor_.stream_frames_[0]->fin);
1392 EXPECT_EQ(GG_UINT64_C(0xBA98FEDC32107654),
1393 visitor_.stream_frames_[0]->offset);
1394 CheckStreamFrameData("hello world!", visitor_.stream_frames_[0]);
1396 // Now test framing boundaries
1397 const size_t stream_id_size = 1;
1398 CheckStreamFrameBoundaries(packet, stream_id_size, !kIncludeVersion);
1401 TEST_P(QuicFramerTest, StreamFrameWithVersion) {
1402 unsigned char packet[] = {
1403 // public flags (version, 8 byte guid)
1404 0x3D,
1405 // guid
1406 0x10, 0x32, 0x54, 0x76,
1407 0x98, 0xBA, 0xDC, 0xFE,
1408 // version tag
1409 'Q', '0', GetQuicVersionDigitTens(), GetQuicVersionDigitOnes(),
1410 // packet sequence number
1411 0xBC, 0x9A, 0x78, 0x56,
1412 0x34, 0x12,
1413 // private flags
1414 0x00,
1416 // frame type (stream frame with fin)
1417 0xFF,
1418 // stream id
1419 0x04, 0x03, 0x02, 0x01,
1420 // offset
1421 0x54, 0x76, 0x10, 0x32,
1422 0xDC, 0xFE, 0x98, 0xBA,
1423 // data length
1424 0x0c, 0x00,
1425 // data
1426 'h', 'e', 'l', 'l',
1427 'o', ' ', 'w', 'o',
1428 'r', 'l', 'd', '!',
1431 QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
1432 EXPECT_TRUE(framer_.ProcessPacket(encrypted));
1434 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
1435 ASSERT_TRUE(visitor_.header_.get());
1436 EXPECT_TRUE(visitor_.header_.get()->public_header.version_flag);
1437 EXPECT_EQ(GetParam(), visitor_.header_.get()->public_header.versions[0]);
1438 EXPECT_TRUE(CheckDecryption(encrypted, kIncludeVersion));
1440 ASSERT_EQ(1u, visitor_.stream_frames_.size());
1441 EXPECT_EQ(0u, visitor_.ack_frames_.size());
1442 EXPECT_EQ(static_cast<uint64>(0x01020304),
1443 visitor_.stream_frames_[0]->stream_id);
1444 EXPECT_TRUE(visitor_.stream_frames_[0]->fin);
1445 EXPECT_EQ(GG_UINT64_C(0xBA98FEDC32107654),
1446 visitor_.stream_frames_[0]->offset);
1447 CheckStreamFrameData("hello world!", visitor_.stream_frames_[0]);
1449 // Now test framing boundaries
1450 CheckStreamFrameBoundaries(packet, kQuicMaxStreamIdSize, kIncludeVersion);
1453 TEST_P(QuicFramerTest, RejectPacket) {
1454 visitor_.accept_packet_ = false;
1456 unsigned char packet[] = {
1457 // public flags (8 byte guid)
1458 0x3C,
1459 // guid
1460 0x10, 0x32, 0x54, 0x76,
1461 0x98, 0xBA, 0xDC, 0xFE,
1462 // packet sequence number
1463 0xBC, 0x9A, 0x78, 0x56,
1464 0x34, 0x12,
1465 // private flags
1466 0x00,
1468 // frame type (stream frame with fin)
1469 0xFF,
1470 // stream id
1471 0x04, 0x03, 0x02, 0x01,
1472 // offset
1473 0x54, 0x76, 0x10, 0x32,
1474 0xDC, 0xFE, 0x98, 0xBA,
1475 // data length
1476 0x0c, 0x00,
1477 // data
1478 'h', 'e', 'l', 'l',
1479 'o', ' ', 'w', 'o',
1480 'r', 'l', 'd', '!',
1483 QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
1484 EXPECT_TRUE(framer_.ProcessPacket(encrypted));
1486 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
1487 ASSERT_TRUE(visitor_.header_.get());
1488 EXPECT_TRUE(CheckDecryption(encrypted, !kIncludeVersion));
1490 ASSERT_EQ(0u, visitor_.stream_frames_.size());
1491 EXPECT_EQ(0u, visitor_.ack_frames_.size());
1494 TEST_P(QuicFramerTest, RevivedStreamFrame) {
1495 unsigned char payload[] = {
1496 // frame type (stream frame with fin)
1497 0xFF,
1498 // stream id
1499 0x04, 0x03, 0x02, 0x01,
1500 // offset
1501 0x54, 0x76, 0x10, 0x32,
1502 0xDC, 0xFE, 0x98, 0xBA,
1503 // data length
1504 0x0c, 0x00,
1505 // data
1506 'h', 'e', 'l', 'l',
1507 'o', ' ', 'w', 'o',
1508 'r', 'l', 'd', '!',
1511 QuicPacketHeader header;
1512 header.public_header.guid = GG_UINT64_C(0xFEDCBA9876543210);
1513 header.public_header.reset_flag = false;
1514 header.public_header.version_flag = false;
1515 header.fec_flag = true;
1516 header.entropy_flag = true;
1517 header.packet_sequence_number = GG_UINT64_C(0x123456789ABC);
1518 header.fec_group = 0;
1520 // Do not encrypt the payload because the revived payload is post-encryption.
1521 EXPECT_TRUE(framer_.ProcessRevivedPacket(&header,
1522 StringPiece(AsChars(payload),
1523 arraysize(payload))));
1525 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
1526 ASSERT_EQ(1, visitor_.revived_packets_);
1527 ASSERT_TRUE(visitor_.header_.get());
1528 EXPECT_EQ(GG_UINT64_C(0xFEDCBA9876543210),
1529 visitor_.header_->public_header.guid);
1530 EXPECT_FALSE(visitor_.header_->public_header.reset_flag);
1531 EXPECT_FALSE(visitor_.header_->public_header.version_flag);
1532 EXPECT_TRUE(visitor_.header_->fec_flag);
1533 EXPECT_TRUE(visitor_.header_->entropy_flag);
1534 EXPECT_EQ(1 << (header.packet_sequence_number % 8),
1535 visitor_.header_->entropy_hash);
1536 EXPECT_EQ(GG_UINT64_C(0x123456789ABC),
1537 visitor_.header_->packet_sequence_number);
1538 EXPECT_EQ(NOT_IN_FEC_GROUP, visitor_.header_->is_in_fec_group);
1539 EXPECT_EQ(0x00u, visitor_.header_->fec_group);
1541 ASSERT_EQ(1u, visitor_.stream_frames_.size());
1542 EXPECT_EQ(0u, visitor_.ack_frames_.size());
1543 EXPECT_EQ(GG_UINT64_C(0x01020304), visitor_.stream_frames_[0]->stream_id);
1544 EXPECT_TRUE(visitor_.stream_frames_[0]->fin);
1545 EXPECT_EQ(GG_UINT64_C(0xBA98FEDC32107654),
1546 visitor_.stream_frames_[0]->offset);
1547 CheckStreamFrameData("hello world!", visitor_.stream_frames_[0]);
1550 TEST_P(QuicFramerTest, StreamFrameInFecGroup) {
1551 unsigned char packet[] = {
1552 // public flags (8 byte guid)
1553 0x3C,
1554 // guid
1555 0x10, 0x32, 0x54, 0x76,
1556 0x98, 0xBA, 0xDC, 0xFE,
1557 // packet sequence number
1558 0xBC, 0x9A, 0x78, 0x56,
1559 0x12, 0x34,
1560 // private flags (fec group)
1561 0x02,
1562 // first fec protected packet offset
1563 0x02,
1565 // frame type (stream frame with fin)
1566 0xFF,
1567 // stream id
1568 0x04, 0x03, 0x02, 0x01,
1569 // offset
1570 0x54, 0x76, 0x10, 0x32,
1571 0xDC, 0xFE, 0x98, 0xBA,
1572 // data length
1573 0x0c, 0x00,
1574 // data
1575 'h', 'e', 'l', 'l',
1576 'o', ' ', 'w', 'o',
1577 'r', 'l', 'd', '!',
1580 QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
1581 EXPECT_TRUE(framer_.ProcessPacket(encrypted));
1583 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
1584 ASSERT_TRUE(visitor_.header_.get());
1585 EXPECT_TRUE(CheckDecryption(encrypted, !kIncludeVersion));
1586 EXPECT_EQ(IN_FEC_GROUP, visitor_.header_->is_in_fec_group);
1587 EXPECT_EQ(GG_UINT64_C(0x341256789ABA),
1588 visitor_.header_->fec_group);
1589 const size_t fec_offset = GetStartOfFecProtectedData(
1590 PACKET_8BYTE_GUID, !kIncludeVersion, PACKET_6BYTE_SEQUENCE_NUMBER);
1591 EXPECT_EQ(
1592 string(AsChars(packet) + fec_offset, arraysize(packet) - fec_offset),
1593 visitor_.fec_protected_payload_);
1595 ASSERT_EQ(1u, visitor_.stream_frames_.size());
1596 EXPECT_EQ(0u, visitor_.ack_frames_.size());
1597 EXPECT_EQ(GG_UINT64_C(0x01020304), visitor_.stream_frames_[0]->stream_id);
1598 EXPECT_TRUE(visitor_.stream_frames_[0]->fin);
1599 EXPECT_EQ(GG_UINT64_C(0xBA98FEDC32107654),
1600 visitor_.stream_frames_[0]->offset);
1601 CheckStreamFrameData("hello world!", visitor_.stream_frames_[0]);
1604 TEST_P(QuicFramerTest, DISABLED_AckFramev11) {
1605 if (GetParam() > QUIC_VERSION_11) {
1606 return;
1608 unsigned char packet[] = {
1609 // public flags (8 byte guid)
1610 0x3C,
1611 // guid
1612 0x10, 0x32, 0x54, 0x76,
1613 0x98, 0xBA, 0xDC, 0xFE,
1614 // packet sequence number
1615 0xBC, 0x9A, 0x78, 0x56,
1616 0x34, 0x12,
1617 // private flags
1618 0x00,
1620 // frame type (ack frame)
1621 0x40,
1622 // entropy hash of sent packets till least awaiting - 1.
1623 0xAB,
1624 // least packet sequence number awaiting an ack
1625 0xA0, 0x9A, 0x78, 0x56,
1626 0x34, 0x12,
1627 // entropy hash of all received packets.
1628 0xBA,
1629 // largest observed packet sequence number
1630 0xBF, 0x9A, 0x78, 0x56,
1631 0x34, 0x12,
1632 // Infinite delta time.
1633 0xFF, 0xFF, 0xFF, 0xFF,
1634 // num missing packets
1635 0x01,
1636 // missing packet
1637 0xBE, 0x9A, 0x78, 0x56,
1638 0x34, 0x12,
1641 QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
1642 EXPECT_TRUE(framer_.ProcessPacket(encrypted));
1644 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
1645 ASSERT_TRUE(visitor_.header_.get());
1646 EXPECT_TRUE(CheckDecryption(encrypted, !kIncludeVersion));
1648 EXPECT_EQ(0u, visitor_.stream_frames_.size());
1649 ASSERT_EQ(1u, visitor_.ack_frames_.size());
1650 const QuicAckFrame& frame = *visitor_.ack_frames_[0];
1651 EXPECT_EQ(0xAB, frame.sent_info.entropy_hash);
1652 EXPECT_EQ(0xBA, frame.received_info.entropy_hash);
1653 EXPECT_EQ(GG_UINT64_C(0x0123456789ABF), frame.received_info.largest_observed);
1654 ASSERT_EQ(1u, frame.received_info.missing_packets.size());
1655 SequenceNumberSet::const_iterator missing_iter =
1656 frame.received_info.missing_packets.begin();
1657 EXPECT_EQ(GG_UINT64_C(0x0123456789ABE), *missing_iter);
1658 EXPECT_EQ(GG_UINT64_C(0x0123456789AA0), frame.sent_info.least_unacked);
1660 const size_t kSentEntropyOffset = kQuicFrameTypeSize;
1661 const size_t kLeastUnackedOffset = kSentEntropyOffset + kQuicEntropyHashSize;
1662 const size_t kReceivedEntropyOffset = kLeastUnackedOffset +
1663 PACKET_6BYTE_SEQUENCE_NUMBER;
1664 const size_t kLargestObservedOffset = kReceivedEntropyOffset +
1665 kQuicEntropyHashSize;
1666 const size_t kMissingDeltaTimeOffset = kLargestObservedOffset +
1667 PACKET_6BYTE_SEQUENCE_NUMBER;
1668 const size_t kNumMissingPacketOffset = kMissingDeltaTimeOffset +
1669 kQuicv11DeltaTimeLargestObservedSize;
1670 const size_t kMissingPacketsOffset = kNumMissingPacketOffset +
1671 kNumberOfMissingPacketsSize;
1672 // Now test framing boundaries
1673 const size_t missing_packets_size = 1 * PACKET_6BYTE_SEQUENCE_NUMBER;
1674 for (size_t i = kQuicFrameTypeSize;
1675 i < QuicFramer::GetMinAckFrameSizev11() + missing_packets_size; ++i) {
1676 string expected_error;
1677 if (i < kLeastUnackedOffset) {
1678 expected_error = "Unable to read entropy hash for sent packets.";
1679 } else if (i < kReceivedEntropyOffset) {
1680 expected_error = "Unable to read least unacked.";
1681 } else if (i < kLargestObservedOffset) {
1682 expected_error = "Unable to read entropy hash for received packets.";
1683 } else if (i < kMissingDeltaTimeOffset) {
1684 expected_error = "Unable to read largest observed.";
1685 } else if (i < kNumMissingPacketOffset) {
1686 expected_error = "Unable to read delta time largest observed.";
1687 } else if (i < kMissingPacketsOffset) {
1688 expected_error = "Unable to read num missing packets.";
1689 } else {
1690 expected_error = "Unable to read sequence number in missing packets.";
1692 CheckProcessingFails(
1693 packet,
1694 i + GetPacketHeaderSize(PACKET_8BYTE_GUID, !kIncludeVersion,
1695 PACKET_6BYTE_SEQUENCE_NUMBER, NOT_IN_FEC_GROUP),
1696 expected_error, QUIC_INVALID_ACK_DATA);
1700 TEST_P(QuicFramerTest, AckFrame) {
1701 if (GetParam() <= QUIC_VERSION_11) {
1702 return;
1704 unsigned char packet[] = {
1705 // public flags (8 byte guid)
1706 0x3C,
1707 // guid
1708 0x10, 0x32, 0x54, 0x76,
1709 0x98, 0xBA, 0xDC, 0xFE,
1710 // packet sequence number
1711 0xA8, 0x9A, 0x78, 0x56,
1712 0x34, 0x12,
1713 // private flags (entropy)
1714 0x01,
1716 // frame type (ack frame)
1717 // (has nacks, not truncated, 6 byte largest observed, 1 byte delta)
1718 0x6C,
1719 // entropy hash of sent packets till least awaiting - 1.
1720 0xAB,
1721 // least packet sequence number awaiting an ack, delta from sequence number.
1722 0x08, 0x00, 0x00, 0x00,
1723 0x00, 0x00,
1724 // entropy hash of all received packets.
1725 0xBA,
1726 // largest observed packet sequence number
1727 0xBF, 0x9A, 0x78, 0x56,
1728 0x34, 0x12,
1729 // Zero delta time.
1730 0x0, 0x0,
1731 // num missing packets
1732 0x01,
1733 // missing packet delta
1734 0x01,
1735 // 0 more missing packets in range.
1736 0x00,
1739 QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
1740 EXPECT_TRUE(framer_.ProcessPacket(encrypted));
1742 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
1743 ASSERT_TRUE(visitor_.header_.get());
1744 EXPECT_TRUE(CheckDecryption(encrypted, !kIncludeVersion));
1746 EXPECT_EQ(0u, visitor_.stream_frames_.size());
1747 ASSERT_EQ(1u, visitor_.ack_frames_.size());
1748 const QuicAckFrame& frame = *visitor_.ack_frames_[0];
1749 EXPECT_EQ(0xAB, frame.sent_info.entropy_hash);
1750 EXPECT_EQ(0xBA, frame.received_info.entropy_hash);
1751 EXPECT_EQ(GG_UINT64_C(0x0123456789ABF), frame.received_info.largest_observed);
1752 ASSERT_EQ(1u, frame.received_info.missing_packets.size());
1753 SequenceNumberSet::const_iterator missing_iter =
1754 frame.received_info.missing_packets.begin();
1755 EXPECT_EQ(GG_UINT64_C(0x0123456789ABE), *missing_iter);
1756 EXPECT_EQ(GG_UINT64_C(0x0123456789AA0), frame.sent_info.least_unacked);
1758 const size_t kSentEntropyOffset = kQuicFrameTypeSize;
1759 const size_t kLeastUnackedOffset = kSentEntropyOffset + kQuicEntropyHashSize;
1760 const size_t kReceivedEntropyOffset = kLeastUnackedOffset +
1761 PACKET_6BYTE_SEQUENCE_NUMBER;
1762 const size_t kLargestObservedOffset = kReceivedEntropyOffset +
1763 kQuicEntropyHashSize;
1764 const size_t kMissingDeltaTimeOffset = kLargestObservedOffset +
1765 PACKET_6BYTE_SEQUENCE_NUMBER;
1766 const size_t kNumMissingPacketOffset = kMissingDeltaTimeOffset +
1767 kQuicDeltaTimeLargestObservedSize;
1768 const size_t kMissingPacketsOffset = kNumMissingPacketOffset +
1769 kNumberOfMissingPacketsSize;
1770 const size_t kMissingPacketsRange = kMissingPacketsOffset +
1771 PACKET_1BYTE_SEQUENCE_NUMBER;
1772 // Now test framing boundaries
1773 const size_t ack_frame_size = kMissingPacketsRange +
1774 PACKET_1BYTE_SEQUENCE_NUMBER;
1775 for (size_t i = kQuicFrameTypeSize; i < ack_frame_size; ++i) {
1776 string expected_error;
1777 if (i < kLeastUnackedOffset) {
1778 expected_error = "Unable to read entropy hash for sent packets.";
1779 } else if (i < kReceivedEntropyOffset) {
1780 expected_error = "Unable to read least unacked delta.";
1781 } else if (i < kLargestObservedOffset) {
1782 expected_error = "Unable to read entropy hash for received packets.";
1783 } else if (i < kMissingDeltaTimeOffset) {
1784 expected_error = "Unable to read largest observed.";
1785 } else if (i < kNumMissingPacketOffset) {
1786 expected_error = "Unable to read delta time largest observed.";
1787 } else if (i < kMissingPacketsOffset) {
1788 expected_error = "Unable to read num missing packet ranges.";
1789 } else if (i < kMissingPacketsRange) {
1790 expected_error = "Unable to read missing sequence number delta.";
1791 } else {
1792 expected_error = "Unable to read missing sequence number range.";
1794 CheckProcessingFails(
1795 packet,
1796 i + GetPacketHeaderSize(PACKET_8BYTE_GUID, !kIncludeVersion,
1797 PACKET_6BYTE_SEQUENCE_NUMBER, NOT_IN_FEC_GROUP),
1798 expected_error, QUIC_INVALID_ACK_DATA);
1802 TEST_P(QuicFramerTest, AckFrameNoNacks) {
1803 if (GetParam() <= QUIC_VERSION_11) {
1804 return;
1806 unsigned char packet[] = {
1807 // public flags (8 byte guid)
1808 0x3C,
1809 // guid
1810 0x10, 0x32, 0x54, 0x76,
1811 0x98, 0xBA, 0xDC, 0xFE,
1812 // packet sequence number
1813 0xA8, 0x9A, 0x78, 0x56,
1814 0x34, 0x12,
1815 // private flags (entropy)
1816 0x01,
1818 // frame type (ack frame)
1819 // (no nacks, not truncated, 6 byte largest observed, 1 byte delta)
1820 0x4C,
1821 // entropy hash of sent packets till least awaiting - 1.
1822 0xAB,
1823 // least packet sequence number awaiting an ack, delta from sequence number.
1824 0x08, 0x00, 0x00, 0x00,
1825 0x00, 0x00,
1826 // entropy hash of all received packets.
1827 0xBA,
1828 // largest observed packet sequence number
1829 0xBF, 0x9A, 0x78, 0x56,
1830 0x34, 0x12,
1831 // Zero delta time.
1832 0x0, 0x0,
1835 QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
1836 EXPECT_TRUE(framer_.ProcessPacket(encrypted));
1838 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
1839 ASSERT_TRUE(visitor_.header_.get());
1840 EXPECT_TRUE(CheckDecryption(encrypted, !kIncludeVersion));
1842 EXPECT_EQ(0u, visitor_.stream_frames_.size());
1843 ASSERT_EQ(1u, visitor_.ack_frames_.size());
1844 QuicAckFrame* frame = visitor_.ack_frames_[0];
1845 EXPECT_EQ(0xAB, frame->sent_info.entropy_hash);
1846 EXPECT_EQ(0xBA, frame->received_info.entropy_hash);
1847 EXPECT_EQ(GG_UINT64_C(0x0123456789ABF),
1848 frame->received_info.largest_observed);
1849 ASSERT_EQ(0u, frame->received_info.missing_packets.size());
1850 EXPECT_EQ(GG_UINT64_C(0x0123456789AA0), frame->sent_info.least_unacked);
1852 // Verify that the packet re-serializes identically.
1853 QuicFrames frames;
1854 frames.push_back(QuicFrame(frame));
1855 scoped_ptr<QuicPacket> data(
1856 framer_.BuildUnsizedDataPacket(*visitor_.header_, frames).packet);
1857 ASSERT_TRUE(data != NULL);
1859 test::CompareCharArraysWithHexError("constructed packet",
1860 data->data(), data->length(),
1861 AsChars(packet), arraysize(packet));
1864 TEST_P(QuicFramerTest, AckFrame500Nacks) {
1865 if (GetParam() <= QUIC_VERSION_11) {
1866 return;
1868 unsigned char packet[] = {
1869 // public flags (8 byte guid)
1870 0x3C,
1871 // guid
1872 0x10, 0x32, 0x54, 0x76,
1873 0x98, 0xBA, 0xDC, 0xFE,
1874 // packet sequence number
1875 0xA8, 0x9A, 0x78, 0x56,
1876 0x34, 0x12,
1877 // private flags (entropy)
1878 0x01,
1880 // frame type (ack frame)
1881 // (has nacks, not truncated, 6 byte largest observed, 1 byte delta)
1882 0x6C,
1883 // entropy hash of sent packets till least awaiting - 1.
1884 0xAB,
1885 // least packet sequence number awaiting an ack, delta from sequence number.
1886 0x08, 0x00, 0x00, 0x00,
1887 0x00, 0x00,
1888 // entropy hash of all received packets.
1889 0xBA,
1890 // largest observed packet sequence number
1891 0xBF, 0x9A, 0x78, 0x56,
1892 0x34, 0x12,
1893 // Zero delta time.
1894 0x0, 0x0,
1895 // num missing packet ranges
1896 0x02,
1897 // missing packet delta
1898 0x01,
1899 // 243 more missing packets in range.
1900 // The ranges are listed in this order so the re-constructed packet matches.
1901 0xF3,
1902 // No gap between ranges
1903 0x00,
1904 // 255 more missing packets in range.
1905 0xFF,
1908 QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
1909 EXPECT_TRUE(framer_.ProcessPacket(encrypted));
1911 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
1912 ASSERT_TRUE(visitor_.header_.get());
1913 EXPECT_TRUE(CheckDecryption(encrypted, !kIncludeVersion));
1915 EXPECT_EQ(0u, visitor_.stream_frames_.size());
1916 ASSERT_EQ(1u, visitor_.ack_frames_.size());
1917 QuicAckFrame* frame = visitor_.ack_frames_[0];
1918 EXPECT_EQ(0xAB, frame->sent_info.entropy_hash);
1919 EXPECT_EQ(0xBA, frame->received_info.entropy_hash);
1920 EXPECT_EQ(GG_UINT64_C(0x0123456789ABF),
1921 frame->received_info.largest_observed);
1922 ASSERT_EQ(500u, frame->received_info.missing_packets.size());
1923 SequenceNumberSet::const_iterator first_missing_iter =
1924 frame->received_info.missing_packets.begin();
1925 EXPECT_EQ(GG_UINT64_C(0x0123456789ABE) - 499, *first_missing_iter);
1926 SequenceNumberSet::const_reverse_iterator last_missing_iter =
1927 frame->received_info.missing_packets.rbegin();
1928 EXPECT_EQ(GG_UINT64_C(0x0123456789ABE), *last_missing_iter);
1929 EXPECT_EQ(GG_UINT64_C(0x0123456789AA0), frame->sent_info.least_unacked);
1931 // Verify that the packet re-serializes identically.
1932 QuicFrames frames;
1933 frames.push_back(QuicFrame(frame));
1934 scoped_ptr<QuicPacket> data(
1935 framer_.BuildUnsizedDataPacket(*visitor_.header_, frames).packet);
1936 ASSERT_TRUE(data != NULL);
1938 test::CompareCharArraysWithHexError("constructed packet",
1939 data->data(), data->length(),
1940 AsChars(packet), arraysize(packet));
1943 TEST_P(QuicFramerTest, CongestionFeedbackFrameTCP) {
1944 unsigned char packet[] = {
1945 // public flags (8 byte guid)
1946 0x3C,
1947 // guid
1948 0x10, 0x32, 0x54, 0x76,
1949 0x98, 0xBA, 0xDC, 0xFE,
1950 // packet sequence number
1951 0xBC, 0x9A, 0x78, 0x56,
1952 0x34, 0x12,
1953 // private flags
1954 0x00,
1956 // frame type (congestion feedback frame)
1957 0x20,
1958 // congestion feedback type (tcp)
1959 0x00,
1960 // ack_frame.feedback.tcp.accumulated_number_of_lost_packets
1961 0x01, 0x02,
1962 // ack_frame.feedback.tcp.receive_window
1963 0x03, 0x04,
1966 QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
1967 EXPECT_TRUE(framer_.ProcessPacket(encrypted));
1969 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
1970 ASSERT_TRUE(visitor_.header_.get());
1971 EXPECT_TRUE(CheckDecryption(encrypted, !kIncludeVersion));
1973 EXPECT_EQ(0u, visitor_.stream_frames_.size());
1974 ASSERT_EQ(1u, visitor_.congestion_feedback_frames_.size());
1975 const QuicCongestionFeedbackFrame& frame =
1976 *visitor_.congestion_feedback_frames_[0];
1977 ASSERT_EQ(kTCP, frame.type);
1978 EXPECT_EQ(0x0201,
1979 frame.tcp.accumulated_number_of_lost_packets);
1980 EXPECT_EQ(0x4030u, frame.tcp.receive_window);
1982 // Now test framing boundaries
1983 for (size_t i = kQuicFrameTypeSize; i < 6; ++i) {
1984 string expected_error;
1985 if (i < 2) {
1986 expected_error = "Unable to read congestion feedback type.";
1987 } else if (i < 4) {
1988 expected_error = "Unable to read accumulated number of lost packets.";
1989 } else if (i < 6) {
1990 expected_error = "Unable to read receive window.";
1992 CheckProcessingFails(
1993 packet,
1994 i + GetPacketHeaderSize(PACKET_8BYTE_GUID, !kIncludeVersion,
1995 PACKET_6BYTE_SEQUENCE_NUMBER, NOT_IN_FEC_GROUP),
1996 expected_error, QUIC_INVALID_CONGESTION_FEEDBACK_DATA);
2000 TEST_P(QuicFramerTest, CongestionFeedbackFrameInterArrival) {
2001 unsigned char packet[] = {
2002 // public flags (8 byte guid)
2003 0x3C,
2004 // guid
2005 0x10, 0x32, 0x54, 0x76,
2006 0x98, 0xBA, 0xDC, 0xFE,
2007 // packet sequence number
2008 0xBC, 0x9A, 0x78, 0x56,
2009 0x34, 0x12,
2010 // private flags
2011 0x00,
2013 // frame type (congestion feedback frame)
2014 0x20,
2015 // congestion feedback type (inter arrival)
2016 0x01,
2017 // accumulated_number_of_lost_packets
2018 0x02, 0x03,
2019 // num received packets
2020 0x03,
2021 // lowest sequence number
2022 0xBA, 0x9A, 0x78, 0x56,
2023 0x34, 0x12,
2024 // receive time
2025 0x87, 0x96, 0xA5, 0xB4,
2026 0xC3, 0xD2, 0xE1, 0x07,
2027 // sequence delta
2028 0x01, 0x00,
2029 // time delta
2030 0x01, 0x00, 0x00, 0x00,
2031 // sequence delta (skip one packet)
2032 0x03, 0x00,
2033 // time delta
2034 0x02, 0x00, 0x00, 0x00,
2037 QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
2038 EXPECT_TRUE(framer_.ProcessPacket(encrypted));
2040 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
2041 ASSERT_TRUE(visitor_.header_.get());
2042 EXPECT_TRUE(CheckDecryption(encrypted, !kIncludeVersion));
2044 EXPECT_EQ(0u, visitor_.stream_frames_.size());
2045 ASSERT_EQ(1u, visitor_.congestion_feedback_frames_.size());
2046 const QuicCongestionFeedbackFrame& frame =
2047 *visitor_.congestion_feedback_frames_[0];
2048 ASSERT_EQ(kInterArrival, frame.type);
2049 EXPECT_EQ(0x0302, frame.inter_arrival.
2050 accumulated_number_of_lost_packets);
2051 ASSERT_EQ(3u, frame.inter_arrival.received_packet_times.size());
2052 TimeMap::const_iterator iter =
2053 frame.inter_arrival.received_packet_times.begin();
2054 EXPECT_EQ(GG_UINT64_C(0x0123456789ABA), iter->first);
2055 EXPECT_EQ(GG_INT64_C(0x07E1D2C3B4A59687),
2056 iter->second.Subtract(start_).ToMicroseconds());
2057 ++iter;
2058 EXPECT_EQ(GG_UINT64_C(0x0123456789ABB), iter->first);
2059 EXPECT_EQ(GG_INT64_C(0x07E1D2C3B4A59688),
2060 iter->second.Subtract(start_).ToMicroseconds());
2061 ++iter;
2062 EXPECT_EQ(GG_UINT64_C(0x0123456789ABD), iter->first);
2063 EXPECT_EQ(GG_INT64_C(0x07E1D2C3B4A59689),
2064 iter->second.Subtract(start_).ToMicroseconds());
2066 // Now test framing boundaries
2067 for (size_t i = kQuicFrameTypeSize; i < 31; ++i) {
2068 string expected_error;
2069 if (i < 2) {
2070 expected_error = "Unable to read congestion feedback type.";
2071 } else if (i < 4) {
2072 expected_error = "Unable to read accumulated number of lost packets.";
2073 } else if (i < 5) {
2074 expected_error = "Unable to read num received packets.";
2075 } else if (i < 11) {
2076 expected_error = "Unable to read smallest received.";
2077 } else if (i < 19) {
2078 expected_error = "Unable to read time received.";
2079 } else if (i < 21) {
2080 expected_error = "Unable to read sequence delta in received packets.";
2081 } else if (i < 25) {
2082 expected_error = "Unable to read time delta in received packets.";
2083 } else if (i < 27) {
2084 expected_error = "Unable to read sequence delta in received packets.";
2085 } else if (i < 31) {
2086 expected_error = "Unable to read time delta in received packets.";
2088 CheckProcessingFails(
2089 packet,
2090 i + GetPacketHeaderSize(PACKET_8BYTE_GUID, !kIncludeVersion,
2091 PACKET_6BYTE_SEQUENCE_NUMBER, NOT_IN_FEC_GROUP),
2092 expected_error, QUIC_INVALID_CONGESTION_FEEDBACK_DATA);
2096 TEST_P(QuicFramerTest, CongestionFeedbackFrameFixRate) {
2097 unsigned char packet[] = {
2098 // public flags (8 byte guid)
2099 0x3C,
2100 // guid
2101 0x10, 0x32, 0x54, 0x76,
2102 0x98, 0xBA, 0xDC, 0xFE,
2103 // packet sequence number
2104 0xBC, 0x9A, 0x78, 0x56,
2105 0x34, 0x12,
2106 // private flags
2107 0x00,
2109 // frame type (congestion feedback frame)
2110 0x20,
2111 // congestion feedback type (fix rate)
2112 0x02,
2113 // bitrate_in_bytes_per_second;
2114 0x01, 0x02, 0x03, 0x04,
2117 QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
2118 EXPECT_TRUE(framer_.ProcessPacket(encrypted));
2120 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
2121 ASSERT_TRUE(visitor_.header_.get());
2122 EXPECT_TRUE(CheckDecryption(encrypted, !kIncludeVersion));
2124 EXPECT_EQ(0u, visitor_.stream_frames_.size());
2125 ASSERT_EQ(1u, visitor_.congestion_feedback_frames_.size());
2126 const QuicCongestionFeedbackFrame& frame =
2127 *visitor_.congestion_feedback_frames_[0];
2128 ASSERT_EQ(kFixRate, frame.type);
2129 EXPECT_EQ(static_cast<uint32>(0x04030201),
2130 frame.fix_rate.bitrate.ToBytesPerSecond());
2132 // Now test framing boundaries
2133 for (size_t i = kQuicFrameTypeSize; i < 6; ++i) {
2134 string expected_error;
2135 if (i < 2) {
2136 expected_error = "Unable to read congestion feedback type.";
2137 } else if (i < 6) {
2138 expected_error = "Unable to read bitrate.";
2140 CheckProcessingFails(
2141 packet,
2142 i + GetPacketHeaderSize(PACKET_8BYTE_GUID, !kIncludeVersion,
2143 PACKET_6BYTE_SEQUENCE_NUMBER, NOT_IN_FEC_GROUP),
2144 expected_error, QUIC_INVALID_CONGESTION_FEEDBACK_DATA);
2148 TEST_P(QuicFramerTest, CongestionFeedbackFrameInvalidFeedback) {
2149 unsigned char packet[] = {
2150 // public flags (8 byte guid)
2151 0x3C,
2152 // guid
2153 0x10, 0x32, 0x54, 0x76,
2154 0x98, 0xBA, 0xDC, 0xFE,
2155 // packet sequence number
2156 0xBC, 0x9A, 0x78, 0x56,
2157 0x34, 0x12,
2158 // private flags
2159 0x00,
2161 // frame type (congestion feedback frame)
2162 0x20,
2163 // congestion feedback type (invalid)
2164 0x03,
2167 QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
2168 EXPECT_FALSE(framer_.ProcessPacket(encrypted));
2169 EXPECT_TRUE(CheckDecryption(encrypted, !kIncludeVersion));
2170 EXPECT_EQ(QUIC_INVALID_CONGESTION_FEEDBACK_DATA, framer_.error());
2173 TEST_P(QuicFramerTest, RstStreamFrame) {
2174 unsigned char packet[] = {
2175 // public flags (8 byte guid)
2176 0x3C,
2177 // guid
2178 0x10, 0x32, 0x54, 0x76,
2179 0x98, 0xBA, 0xDC, 0xFE,
2180 // packet sequence number
2181 0xBC, 0x9A, 0x78, 0x56,
2182 0x34, 0x12,
2183 // private flags
2184 0x00,
2186 // frame type (rst stream frame)
2187 0x01,
2188 // stream id
2189 0x04, 0x03, 0x02, 0x01,
2190 // error code
2191 0x01, 0x00, 0x00, 0x00,
2193 // error details length
2194 0x0d, 0x00,
2195 // error details
2196 'b', 'e', 'c', 'a',
2197 'u', 's', 'e', ' ',
2198 'I', ' ', 'c', 'a',
2199 'n',
2202 QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
2203 EXPECT_TRUE(framer_.ProcessPacket(encrypted));
2205 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
2206 ASSERT_TRUE(visitor_.header_.get());
2207 EXPECT_TRUE(CheckDecryption(encrypted, !kIncludeVersion));
2209 EXPECT_EQ(GG_UINT64_C(0x01020304), visitor_.rst_stream_frame_.stream_id);
2210 EXPECT_EQ(0x01, visitor_.rst_stream_frame_.error_code);
2211 EXPECT_EQ("because I can", visitor_.rst_stream_frame_.error_details);
2213 // Now test framing boundaries
2214 for (size_t i = kQuicFrameTypeSize; i < 24; ++i) {
2215 string expected_error;
2216 if (i < kQuicFrameTypeSize + kQuicMaxStreamIdSize) {
2217 expected_error = "Unable to read stream_id.";
2218 } else if (i < kQuicFrameTypeSize + kQuicMaxStreamIdSize +
2219 kQuicErrorCodeSize) {
2220 expected_error = "Unable to read rst stream error code.";
2221 } else {
2222 expected_error = "Unable to read rst stream error details.";
2224 CheckProcessingFails(
2225 packet,
2226 i + GetPacketHeaderSize(PACKET_8BYTE_GUID, !kIncludeVersion,
2227 PACKET_6BYTE_SEQUENCE_NUMBER, NOT_IN_FEC_GROUP),
2228 expected_error, QUIC_INVALID_RST_STREAM_DATA);
2232 TEST_P(QuicFramerTest, DISABLED_ConnectionCloseFramev11) {
2233 if (GetParam() > QUIC_VERSION_11) {
2234 return;
2236 unsigned char packet[] = {
2237 // public flags (8 byte guid)
2238 0x3C,
2239 // guid
2240 0x10, 0x32, 0x54, 0x76,
2241 0x98, 0xBA, 0xDC, 0xFE,
2242 // packet sequence number
2243 0xBC, 0x9A, 0x78, 0x56,
2244 0x34, 0x12,
2245 // private flags
2246 0x00,
2248 // frame type (connection close frame)
2249 0x02,
2250 // error code
2251 0x11, 0x00, 0x00, 0x00,
2253 // error details length
2254 0x0d, 0x00,
2255 // error details
2256 'b', 'e', 'c', 'a',
2257 'u', 's', 'e', ' ',
2258 'I', ' ', 'c', 'a',
2259 'n',
2261 // Ack frame.
2262 // entropy hash of sent packets till least awaiting - 1.
2263 0xBF,
2264 // least packet sequence number awaiting an ack
2265 0xA0, 0x9A, 0x78, 0x56,
2266 0x34, 0x12,
2267 // entropy hash of all received packets.
2268 0xEB,
2269 // largest observed packet sequence number
2270 0xBF, 0x9A, 0x78, 0x56,
2271 0x34, 0x12,
2272 // Infinite delta time.
2273 0xFF, 0xFF, 0xFF, 0xFF,
2274 // num missing packets
2275 0x01,
2276 // missing packet
2277 0xBE, 0x9A, 0x78, 0x56,
2278 0x34, 0x12,
2281 QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
2282 EXPECT_TRUE(framer_.ProcessPacket(encrypted));
2284 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
2285 ASSERT_TRUE(visitor_.header_.get());
2286 EXPECT_TRUE(CheckDecryption(encrypted, !kIncludeVersion));
2288 EXPECT_EQ(0u, visitor_.stream_frames_.size());
2290 EXPECT_EQ(0x11, visitor_.connection_close_frame_.error_code);
2291 EXPECT_EQ("because I can", visitor_.connection_close_frame_.error_details);
2293 ASSERT_EQ(1u, visitor_.ack_frames_.size());
2294 const QuicAckFrame& frame = *visitor_.ack_frames_[0];
2295 EXPECT_EQ(0xBF, frame.sent_info.entropy_hash);
2296 EXPECT_EQ(GG_UINT64_C(0x0123456789AA0), frame.sent_info.least_unacked);
2297 EXPECT_EQ(0xEB, frame.received_info.entropy_hash);
2298 EXPECT_EQ(GG_UINT64_C(0x0123456789ABF), frame.received_info.largest_observed);
2299 ASSERT_EQ(1u, frame.received_info.missing_packets.size());
2300 SequenceNumberSet::const_iterator missing_iter =
2301 frame.received_info.missing_packets.begin();
2302 EXPECT_EQ(GG_UINT64_C(0x0123456789ABE), *missing_iter);
2304 // Now test framing boundaries
2305 for (size_t i = kQuicFrameTypeSize;
2306 i < QuicFramer::GetMinConnectionCloseFrameSize(); ++i) {
2307 string expected_error;
2308 if (i < kQuicFrameTypeSize + kQuicErrorCodeSize) {
2309 expected_error = "Unable to read connection close error code.";
2310 } else {
2311 expected_error = "Unable to read connection close error details.";
2313 CheckProcessingFails(
2314 packet,
2315 i + GetPacketHeaderSize(PACKET_8BYTE_GUID, !kIncludeVersion,
2316 PACKET_6BYTE_SEQUENCE_NUMBER, NOT_IN_FEC_GROUP),
2317 expected_error, QUIC_INVALID_CONNECTION_CLOSE_DATA);
2321 TEST_P(QuicFramerTest, ConnectionCloseFrame) {
2322 if (GetParam() <= QUIC_VERSION_11) {
2323 return;
2325 unsigned char packet[] = {
2326 // public flags (8 byte guid)
2327 0x3C,
2328 // guid
2329 0x10, 0x32, 0x54, 0x76,
2330 0x98, 0xBA, 0xDC, 0xFE,
2331 // packet sequence number
2332 0xBC, 0x9A, 0x78, 0x56,
2333 0x34, 0x12,
2334 // private flags
2335 0x00,
2337 // frame type (connection close frame)
2338 0x02,
2339 // error code
2340 0x11, 0x00, 0x00, 0x00,
2342 // error details length
2343 0x0d, 0x00,
2344 // error details
2345 'b', 'e', 'c', 'a',
2346 'u', 's', 'e', ' ',
2347 'I', ' ', 'c', 'a',
2348 'n',
2351 QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
2352 EXPECT_TRUE(framer_.ProcessPacket(encrypted));
2354 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
2355 ASSERT_TRUE(visitor_.header_.get());
2356 EXPECT_TRUE(CheckDecryption(encrypted, !kIncludeVersion));
2358 EXPECT_EQ(0u, visitor_.stream_frames_.size());
2360 EXPECT_EQ(0x11, visitor_.connection_close_frame_.error_code);
2361 EXPECT_EQ("because I can", visitor_.connection_close_frame_.error_details);
2363 ASSERT_EQ(0u, visitor_.ack_frames_.size());
2365 // Now test framing boundaries
2366 for (size_t i = kQuicFrameTypeSize;
2367 i < QuicFramer::GetMinConnectionCloseFrameSize(); ++i) {
2368 string expected_error;
2369 if (i < kQuicFrameTypeSize + kQuicErrorCodeSize) {
2370 expected_error = "Unable to read connection close error code.";
2371 } else {
2372 expected_error = "Unable to read connection close error details.";
2374 CheckProcessingFails(
2375 packet,
2376 i + GetPacketHeaderSize(PACKET_8BYTE_GUID, !kIncludeVersion,
2377 PACKET_6BYTE_SEQUENCE_NUMBER, NOT_IN_FEC_GROUP),
2378 expected_error, QUIC_INVALID_CONNECTION_CLOSE_DATA);
2382 TEST_P(QuicFramerTest, GoAwayFrame) {
2383 unsigned char packet[] = {
2384 // public flags (8 byte guid)
2385 0x3C,
2386 // guid
2387 0x10, 0x32, 0x54, 0x76,
2388 0x98, 0xBA, 0xDC, 0xFE,
2389 // packet sequence number
2390 0xBC, 0x9A, 0x78, 0x56,
2391 0x34, 0x12,
2392 // private flags
2393 0x00,
2395 // frame type (go away frame)
2396 0x03,
2397 // error code
2398 0x09, 0x00, 0x00, 0x00,
2399 // stream id
2400 0x04, 0x03, 0x02, 0x01,
2401 // error details length
2402 0x0d, 0x00,
2403 // error details
2404 'b', 'e', 'c', 'a',
2405 'u', 's', 'e', ' ',
2406 'I', ' ', 'c', 'a',
2407 'n',
2410 QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
2411 EXPECT_TRUE(framer_.ProcessPacket(encrypted));
2413 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
2414 ASSERT_TRUE(visitor_.header_.get());
2415 EXPECT_TRUE(CheckDecryption(encrypted, !kIncludeVersion));
2417 EXPECT_EQ(GG_UINT64_C(0x01020304),
2418 visitor_.goaway_frame_.last_good_stream_id);
2419 EXPECT_EQ(0x9, visitor_.goaway_frame_.error_code);
2420 EXPECT_EQ("because I can", visitor_.goaway_frame_.reason_phrase);
2422 const size_t reason_size = arraysize("because I can") - 1;
2423 // Now test framing boundaries
2424 for (size_t i = kQuicFrameTypeSize;
2425 i < QuicFramer::GetMinGoAwayFrameSize() + reason_size; ++i) {
2426 string expected_error;
2427 if (i < kQuicFrameTypeSize + kQuicErrorCodeSize) {
2428 expected_error = "Unable to read go away error code.";
2429 } else if (i < kQuicFrameTypeSize + kQuicErrorCodeSize +
2430 kQuicMaxStreamIdSize) {
2431 expected_error = "Unable to read last good stream id.";
2432 } else {
2433 expected_error = "Unable to read goaway reason.";
2435 CheckProcessingFails(
2436 packet,
2437 i + GetPacketHeaderSize(PACKET_8BYTE_GUID, !kIncludeVersion,
2438 PACKET_6BYTE_SEQUENCE_NUMBER, NOT_IN_FEC_GROUP),
2439 expected_error, QUIC_INVALID_GOAWAY_DATA);
2443 TEST_P(QuicFramerTest, PublicResetPacket) {
2444 unsigned char packet[] = {
2445 // public flags (public reset, 8 byte guid)
2446 0x3E,
2447 // guid
2448 0x10, 0x32, 0x54, 0x76,
2449 0x98, 0xBA, 0xDC, 0xFE,
2450 // nonce proof
2451 0x89, 0x67, 0x45, 0x23,
2452 0x01, 0xEF, 0xCD, 0xAB,
2453 // rejected sequence number
2454 0xBC, 0x9A, 0x78, 0x56,
2455 0x34, 0x12,
2458 QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
2459 EXPECT_TRUE(framer_.ProcessPacket(encrypted));
2460 ASSERT_EQ(QUIC_NO_ERROR, framer_.error());
2461 ASSERT_TRUE(visitor_.public_reset_packet_.get());
2462 EXPECT_EQ(GG_UINT64_C(0xFEDCBA9876543210),
2463 visitor_.public_reset_packet_->public_header.guid);
2464 EXPECT_TRUE(visitor_.public_reset_packet_->public_header.reset_flag);
2465 EXPECT_FALSE(visitor_.public_reset_packet_->public_header.version_flag);
2466 EXPECT_EQ(GG_UINT64_C(0xABCDEF0123456789),
2467 visitor_.public_reset_packet_->nonce_proof);
2468 EXPECT_EQ(GG_UINT64_C(0x123456789ABC),
2469 visitor_.public_reset_packet_->rejected_sequence_number);
2471 // Now test framing boundaries
2472 for (size_t i = 0; i < GetPublicResetPacketSize(); ++i) {
2473 string expected_error;
2474 DLOG(INFO) << "iteration: " << i;
2475 if (i < kGuidOffset) {
2476 expected_error = "Unable to read public flags.";
2477 CheckProcessingFails(packet, i, expected_error,
2478 QUIC_INVALID_PACKET_HEADER);
2479 } else if (i < kPublicResetPacketNonceProofOffset) {
2480 expected_error = "Unable to read GUID.";
2481 CheckProcessingFails(packet, i, expected_error,
2482 QUIC_INVALID_PACKET_HEADER);
2483 } else if (i < kPublicResetPacketRejectedSequenceNumberOffset) {
2484 expected_error = "Unable to read nonce proof.";
2485 CheckProcessingFails(packet, i, expected_error,
2486 QUIC_INVALID_PUBLIC_RST_PACKET);
2487 } else {
2488 expected_error = "Unable to read rejected sequence number.";
2489 CheckProcessingFails(packet, i, expected_error,
2490 QUIC_INVALID_PUBLIC_RST_PACKET);
2495 TEST_P(QuicFramerTest, VersionNegotiationPacket) {
2496 unsigned char packet[] = {
2497 // public flags (version, 8 byte guid)
2498 0x3D,
2499 // guid
2500 0x10, 0x32, 0x54, 0x76,
2501 0x98, 0xBA, 0xDC, 0xFE,
2502 // version tag
2503 'Q', '0', GetQuicVersionDigitTens(), GetQuicVersionDigitOnes(),
2504 'Q', '2', '.', '0',
2507 QuicFramerPeer::SetIsServer(&framer_, false);
2509 QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
2510 EXPECT_TRUE(framer_.ProcessPacket(encrypted));
2511 ASSERT_EQ(QUIC_NO_ERROR, framer_.error());
2512 ASSERT_TRUE(visitor_.version_negotiation_packet_.get());
2513 EXPECT_EQ(2u, visitor_.version_negotiation_packet_->versions.size());
2514 EXPECT_EQ(GetParam(), visitor_.version_negotiation_packet_->versions[0]);
2516 for (size_t i = 0; i <= kPublicFlagsSize + PACKET_8BYTE_GUID; ++i) {
2517 string expected_error;
2518 QuicErrorCode error_code = QUIC_INVALID_PACKET_HEADER;
2519 if (i < kGuidOffset) {
2520 expected_error = "Unable to read public flags.";
2521 } else if (i < kVersionOffset) {
2522 expected_error = "Unable to read GUID.";
2523 } else {
2524 expected_error = "Unable to read supported version in negotiation.";
2525 error_code = QUIC_INVALID_VERSION_NEGOTIATION_PACKET;
2527 CheckProcessingFails(packet, i, expected_error, error_code);
2531 TEST_P(QuicFramerTest, FecPacket) {
2532 unsigned char packet[] = {
2533 // public flags (8 byte guid)
2534 0x3C,
2535 // guid
2536 0x10, 0x32, 0x54, 0x76,
2537 0x98, 0xBA, 0xDC, 0xFE,
2538 // packet sequence number
2539 0xBC, 0x9A, 0x78, 0x56,
2540 0x34, 0x12,
2541 // private flags (fec group & FEC)
2542 0x06,
2543 // first fec protected packet offset
2544 0x01,
2546 // redundancy
2547 'a', 'b', 'c', 'd',
2548 'e', 'f', 'g', 'h',
2549 'i', 'j', 'k', 'l',
2550 'm', 'n', 'o', 'p',
2553 QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
2554 EXPECT_TRUE(framer_.ProcessPacket(encrypted));
2556 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
2557 ASSERT_TRUE(visitor_.header_.get());
2558 EXPECT_TRUE(CheckDecryption(encrypted, !kIncludeVersion));
2560 EXPECT_EQ(0u, visitor_.stream_frames_.size());
2561 EXPECT_EQ(0u, visitor_.ack_frames_.size());
2562 ASSERT_EQ(1, visitor_.fec_count_);
2563 const QuicFecData& fec_data = *visitor_.fec_data_[0];
2564 EXPECT_EQ(GG_UINT64_C(0x0123456789ABB), fec_data.fec_group);
2565 EXPECT_EQ("abcdefghijklmnop", fec_data.redundancy);
2568 TEST_P(QuicFramerTest, BuildPaddingFramePacket) {
2569 QuicPacketHeader header;
2570 header.public_header.guid = GG_UINT64_C(0xFEDCBA9876543210);
2571 header.public_header.reset_flag = false;
2572 header.public_header.version_flag = false;
2573 header.fec_flag = false;
2574 header.entropy_flag = false;
2575 header.packet_sequence_number = GG_UINT64_C(0x123456789ABC);
2576 header.fec_group = 0;
2578 QuicPaddingFrame padding_frame;
2580 QuicFrames frames;
2581 frames.push_back(QuicFrame(&padding_frame));
2583 unsigned char packet[kMaxPacketSize] = {
2584 // public flags (8 byte guid)
2585 0x3C,
2586 // guid
2587 0x10, 0x32, 0x54, 0x76,
2588 0x98, 0xBA, 0xDC, 0xFE,
2589 // packet sequence number
2590 0xBC, 0x9A, 0x78, 0x56,
2591 0x34, 0x12,
2592 // private flags
2593 0x00,
2595 // frame type (padding frame)
2596 0x00,
2597 0x00, 0x00, 0x00, 0x00
2600 uint64 header_size =
2601 GetPacketHeaderSize(PACKET_8BYTE_GUID, !kIncludeVersion,
2602 PACKET_6BYTE_SEQUENCE_NUMBER, NOT_IN_FEC_GROUP);
2603 memset(packet + header_size + 1, 0x00, kMaxPacketSize - header_size - 1);
2605 scoped_ptr<QuicPacket> data(
2606 framer_.BuildUnsizedDataPacket(header, frames).packet);
2607 ASSERT_TRUE(data != NULL);
2609 test::CompareCharArraysWithHexError("constructed packet",
2610 data->data(), data->length(),
2611 AsChars(packet),
2612 arraysize(packet));
2615 TEST_P(QuicFramerTest, Build4ByteSequenceNumberPaddingFramePacket) {
2616 QuicPacketHeader header;
2617 header.public_header.guid = GG_UINT64_C(0xFEDCBA9876543210);
2618 header.public_header.reset_flag = false;
2619 header.public_header.version_flag = false;
2620 header.fec_flag = false;
2621 header.entropy_flag = false;
2622 header.public_header.sequence_number_length = PACKET_4BYTE_SEQUENCE_NUMBER;
2623 header.packet_sequence_number = GG_UINT64_C(0x123456789ABC);
2624 header.fec_group = 0;
2626 QuicPaddingFrame padding_frame;
2628 QuicFrames frames;
2629 frames.push_back(QuicFrame(&padding_frame));
2631 unsigned char packet[kMaxPacketSize] = {
2632 // public flags (8 byte guid and 4 byte sequence number)
2633 0x2C,
2634 // guid
2635 0x10, 0x32, 0x54, 0x76,
2636 0x98, 0xBA, 0xDC, 0xFE,
2637 // packet sequence number
2638 0xBC, 0x9A, 0x78, 0x56,
2639 // private flags
2640 0x00,
2642 // frame type (padding frame)
2643 0x00,
2644 0x00, 0x00, 0x00, 0x00
2647 uint64 header_size =
2648 GetPacketHeaderSize(PACKET_8BYTE_GUID, !kIncludeVersion,
2649 PACKET_4BYTE_SEQUENCE_NUMBER, NOT_IN_FEC_GROUP);
2650 memset(packet + header_size + 1, 0x00, kMaxPacketSize - header_size - 1);
2652 scoped_ptr<QuicPacket> data(
2653 framer_.BuildUnsizedDataPacket(header, frames).packet);
2654 ASSERT_TRUE(data != NULL);
2656 test::CompareCharArraysWithHexError("constructed packet",
2657 data->data(), data->length(),
2658 AsChars(packet),
2659 arraysize(packet));
2662 TEST_P(QuicFramerTest, Build2ByteSequenceNumberPaddingFramePacket) {
2663 QuicPacketHeader header;
2664 header.public_header.guid = GG_UINT64_C(0xFEDCBA9876543210);
2665 header.public_header.reset_flag = false;
2666 header.public_header.version_flag = false;
2667 header.fec_flag = false;
2668 header.entropy_flag = false;
2669 header.public_header.sequence_number_length = PACKET_2BYTE_SEQUENCE_NUMBER;
2670 header.packet_sequence_number = GG_UINT64_C(0x123456789ABC);
2671 header.fec_group = 0;
2673 QuicPaddingFrame padding_frame;
2675 QuicFrames frames;
2676 frames.push_back(QuicFrame(&padding_frame));
2678 unsigned char packet[kMaxPacketSize] = {
2679 // public flags (8 byte guid and 2 byte sequence number)
2680 0x1C,
2681 // guid
2682 0x10, 0x32, 0x54, 0x76,
2683 0x98, 0xBA, 0xDC, 0xFE,
2684 // packet sequence number
2685 0xBC, 0x9A,
2686 // private flags
2687 0x00,
2689 // frame type (padding frame)
2690 0x00,
2691 0x00, 0x00, 0x00, 0x00
2694 uint64 header_size =
2695 GetPacketHeaderSize(PACKET_8BYTE_GUID, !kIncludeVersion,
2696 PACKET_2BYTE_SEQUENCE_NUMBER, NOT_IN_FEC_GROUP);
2697 memset(packet + header_size + 1, 0x00, kMaxPacketSize - header_size - 1);
2699 scoped_ptr<QuicPacket> data(
2700 framer_.BuildUnsizedDataPacket(header, frames).packet);
2701 ASSERT_TRUE(data != NULL);
2703 test::CompareCharArraysWithHexError("constructed packet",
2704 data->data(), data->length(),
2705 AsChars(packet),
2706 arraysize(packet));
2709 TEST_P(QuicFramerTest, Build1ByteSequenceNumberPaddingFramePacket) {
2710 QuicPacketHeader header;
2711 header.public_header.guid = GG_UINT64_C(0xFEDCBA9876543210);
2712 header.public_header.reset_flag = false;
2713 header.public_header.version_flag = false;
2714 header.fec_flag = false;
2715 header.entropy_flag = false;
2716 header.public_header.sequence_number_length = PACKET_1BYTE_SEQUENCE_NUMBER;
2717 header.packet_sequence_number = GG_UINT64_C(0x123456789ABC);
2718 header.fec_group = 0;
2720 QuicPaddingFrame padding_frame;
2722 QuicFrames frames;
2723 frames.push_back(QuicFrame(&padding_frame));
2725 unsigned char packet[kMaxPacketSize] = {
2726 // public flags (8 byte guid and 1 byte sequence number)
2727 0x0C,
2728 // guid
2729 0x10, 0x32, 0x54, 0x76,
2730 0x98, 0xBA, 0xDC, 0xFE,
2731 // packet sequence number
2732 0xBC,
2733 // private flags
2734 0x00,
2736 // frame type (padding frame)
2737 0x00,
2738 0x00, 0x00, 0x00, 0x00
2741 uint64 header_size =
2742 GetPacketHeaderSize(PACKET_8BYTE_GUID, !kIncludeVersion,
2743 PACKET_1BYTE_SEQUENCE_NUMBER, NOT_IN_FEC_GROUP);
2744 memset(packet + header_size + 1, 0x00, kMaxPacketSize - header_size - 1);
2746 scoped_ptr<QuicPacket> data(
2747 framer_.BuildUnsizedDataPacket(header, frames).packet);
2748 ASSERT_TRUE(data != NULL);
2750 test::CompareCharArraysWithHexError("constructed packet",
2751 data->data(), data->length(),
2752 AsChars(packet),
2753 arraysize(packet));
2756 TEST_P(QuicFramerTest, BuildStreamFramePacket) {
2757 QuicPacketHeader header;
2758 header.public_header.guid = GG_UINT64_C(0xFEDCBA9876543210);
2759 header.public_header.reset_flag = false;
2760 header.public_header.version_flag = false;
2761 header.fec_flag = false;
2762 header.entropy_flag = true;
2763 header.packet_sequence_number = GG_UINT64_C(0x77123456789ABC);
2764 header.fec_group = 0;
2766 QuicStreamFrame stream_frame;
2767 stream_frame.stream_id = 0x01020304;
2768 stream_frame.fin = true;
2769 stream_frame.offset = GG_UINT64_C(0xBA98FEDC32107654);
2770 stream_frame.data = MakeIOVector("hello world!");
2772 QuicFrames frames;
2773 frames.push_back(QuicFrame(&stream_frame));
2775 unsigned char packet[] = {
2776 // public flags (8 byte guid)
2777 0x3C,
2778 // guid
2779 0x10, 0x32, 0x54, 0x76,
2780 0x98, 0xBA, 0xDC, 0xFE,
2781 // packet sequence number
2782 0xBC, 0x9A, 0x78, 0x56,
2783 0x34, 0x12,
2784 // private flags (entropy)
2785 0x01,
2787 // frame type (stream frame with fin and no length)
2788 0xDF,
2789 // stream id
2790 0x04, 0x03, 0x02, 0x01,
2791 // offset
2792 0x54, 0x76, 0x10, 0x32,
2793 0xDC, 0xFE, 0x98, 0xBA,
2794 // data
2795 'h', 'e', 'l', 'l',
2796 'o', ' ', 'w', 'o',
2797 'r', 'l', 'd', '!',
2800 scoped_ptr<QuicPacket> data(
2801 framer_.BuildUnsizedDataPacket(header, frames).packet);
2802 ASSERT_TRUE(data != NULL);
2804 test::CompareCharArraysWithHexError("constructed packet",
2805 data->data(), data->length(),
2806 AsChars(packet), arraysize(packet));
2809 TEST_P(QuicFramerTest, BuildStreamFramePacketWithVersionFlag) {
2810 QuicPacketHeader header;
2811 header.public_header.guid = GG_UINT64_C(0xFEDCBA9876543210);
2812 header.public_header.reset_flag = false;
2813 header.public_header.version_flag = true;
2814 header.fec_flag = false;
2815 header.entropy_flag = true;
2816 header.packet_sequence_number = GG_UINT64_C(0x77123456789ABC);
2817 header.fec_group = 0;
2819 QuicStreamFrame stream_frame;
2820 stream_frame.stream_id = 0x01020304;
2821 stream_frame.fin = true;
2822 stream_frame.offset = GG_UINT64_C(0xBA98FEDC32107654);
2823 stream_frame.data = MakeIOVector("hello world!");
2825 QuicFrames frames;
2826 frames.push_back(QuicFrame(&stream_frame));
2828 unsigned char packet[] = {
2829 // public flags (version, 8 byte guid)
2830 0x3D,
2831 // guid
2832 0x10, 0x32, 0x54, 0x76,
2833 0x98, 0xBA, 0xDC, 0xFE,
2834 // version tag
2835 'Q', '0', GetQuicVersionDigitTens(), GetQuicVersionDigitOnes(),
2836 // packet sequence number
2837 0xBC, 0x9A, 0x78, 0x56,
2838 0x34, 0x12,
2839 // private flags (entropy)
2840 0x01,
2842 // frame type (stream frame with fin and no length)
2843 0xDF,
2844 // stream id
2845 0x04, 0x03, 0x02, 0x01,
2846 // offset
2847 0x54, 0x76, 0x10, 0x32,
2848 0xDC, 0xFE, 0x98, 0xBA,
2849 // data
2850 'h', 'e', 'l', 'l',
2851 'o', ' ', 'w', 'o',
2852 'r', 'l', 'd', '!',
2855 QuicFramerPeer::SetIsServer(&framer_, false);
2856 scoped_ptr<QuicPacket> data(
2857 framer_.BuildUnsizedDataPacket(header, frames).packet);
2858 ASSERT_TRUE(data != NULL);
2860 test::CompareCharArraysWithHexError("constructed packet",
2861 data->data(), data->length(),
2862 AsChars(packet), arraysize(packet));
2865 TEST_P(QuicFramerTest, BuildVersionNegotiationPacket) {
2866 QuicPacketPublicHeader header;
2867 header.guid = GG_UINT64_C(0xFEDCBA9876543210);
2868 header.reset_flag = false;
2869 header.version_flag = true;
2871 unsigned char packet[] = {
2872 // public flags (version, 8 byte guid)
2873 0x3D,
2874 // guid
2875 0x10, 0x32, 0x54, 0x76,
2876 0x98, 0xBA, 0xDC, 0xFE,
2877 // version tag
2878 'Q', '0', GetQuicVersionDigitTens(), GetQuicVersionDigitOnes(),
2881 QuicVersionVector versions;
2882 versions.push_back(GetParam());
2883 scoped_ptr<QuicEncryptedPacket> data(
2884 framer_.BuildVersionNegotiationPacket(header, versions));
2886 test::CompareCharArraysWithHexError("constructed packet",
2887 data->data(), data->length(),
2888 AsChars(packet), arraysize(packet));
2891 TEST_P(QuicFramerTest, DISABLED_BuildAckFramePacketv11) {
2892 if (GetParam() > QUIC_VERSION_11) {
2893 return;
2895 QuicPacketHeader header;
2896 header.public_header.guid = GG_UINT64_C(0xFEDCBA9876543210);
2897 header.public_header.reset_flag = false;
2898 header.public_header.version_flag = false;
2899 header.fec_flag = false;
2900 header.entropy_flag = true;
2901 header.packet_sequence_number = GG_UINT64_C(0x123456789ABC);
2902 header.fec_group = 0;
2904 QuicAckFrame ack_frame;
2905 ack_frame.received_info.entropy_hash = 0x43;
2906 ack_frame.received_info.largest_observed = GG_UINT64_C(0x770123456789ABF);
2907 ack_frame.received_info.delta_time_largest_observed = QuicTime::Delta::Zero();
2908 ack_frame.received_info.missing_packets.insert(
2909 GG_UINT64_C(0x770123456789ABE));
2910 ack_frame.sent_info.entropy_hash = 0x14;
2911 ack_frame.sent_info.least_unacked = GG_UINT64_C(0x770123456789AA0);
2913 QuicFrames frames;
2914 frames.push_back(QuicFrame(&ack_frame));
2916 unsigned char packet[] = {
2917 // public flags (8 byte guid)
2918 0x3C,
2919 // guid
2920 0x10, 0x32, 0x54, 0x76,
2921 0x98, 0xBA, 0xDC, 0xFE,
2922 // packet sequence number
2923 0xBC, 0x9A, 0x78, 0x56,
2924 0x34, 0x12,
2925 // private flags (entropy)
2926 0x01,
2928 // frame type (ack frame)
2929 0x40,
2930 // entropy hash of sent packets till least awaiting - 1.
2931 0x14,
2932 // least packet sequence number awaiting an ack
2933 0xA0, 0x9A, 0x78, 0x56,
2934 0x34, 0x12,
2935 // entropy hash of all received packets.
2936 0x43,
2937 // largest observed packet sequence number
2938 0xBF, 0x9A, 0x78, 0x56,
2939 0x34, 0x12,
2940 // Zero delta time.
2941 0x0, 0x0, 0x0, 0x0,
2942 // num missing packets
2943 0x01,
2944 // missing packet
2945 0xBE, 0x9A, 0x78, 0x56,
2946 0x34, 0x12,
2949 scoped_ptr<QuicPacket> data(
2950 framer_.BuildUnsizedDataPacket(header, frames).packet);
2951 ASSERT_TRUE(data != NULL);
2953 test::CompareCharArraysWithHexError("constructed packet",
2954 data->data(), data->length(),
2955 AsChars(packet), arraysize(packet));
2958 TEST_P(QuicFramerTest, BuildAckFramePacket) {
2959 if (GetParam() <= QUIC_VERSION_11) {
2960 return;
2962 QuicPacketHeader header;
2963 header.public_header.guid = GG_UINT64_C(0xFEDCBA9876543210);
2964 header.public_header.reset_flag = false;
2965 header.public_header.version_flag = false;
2966 header.fec_flag = false;
2967 header.entropy_flag = true;
2968 header.packet_sequence_number = GG_UINT64_C(0x770123456789AA8);
2969 header.fec_group = 0;
2971 QuicAckFrame ack_frame;
2972 ack_frame.received_info.entropy_hash = 0x43;
2973 ack_frame.received_info.largest_observed = GG_UINT64_C(0x770123456789ABF);
2974 ack_frame.received_info.delta_time_largest_observed = QuicTime::Delta::Zero();
2975 ack_frame.received_info.missing_packets.insert(
2976 GG_UINT64_C(0x770123456789ABE));
2977 ack_frame.sent_info.entropy_hash = 0x14;
2978 ack_frame.sent_info.least_unacked = GG_UINT64_C(0x770123456789AA0);
2980 QuicFrames frames;
2981 frames.push_back(QuicFrame(&ack_frame));
2983 unsigned char packet[] = {
2984 // public flags (8 byte guid)
2985 0x3C,
2986 // guid
2987 0x10, 0x32, 0x54, 0x76,
2988 0x98, 0xBA, 0xDC, 0xFE,
2989 // packet sequence number
2990 0xA8, 0x9A, 0x78, 0x56,
2991 0x34, 0x12,
2992 // private flags (entropy)
2993 0x01,
2995 // frame type (ack frame)
2996 // (has nacks, not truncated, 6 byte largest observed, 1 byte delta)
2997 0x6C,
2998 // entropy hash of sent packets till least awaiting - 1.
2999 0x14,
3000 // least packet sequence number awaiting an ack, delta from sequence number.
3001 0x08, 0x00, 0x00, 0x00,
3002 0x00, 0x00,
3003 // entropy hash of all received packets.
3004 0x43,
3005 // largest observed packet sequence number
3006 0xBF, 0x9A, 0x78, 0x56,
3007 0x34, 0x12,
3008 // Zero delta time.
3009 0x0, 0x0,
3010 // num missing packet ranges
3011 0x01,
3012 // missing packet delta
3013 0x01,
3014 // 0 more missing packets in range.
3015 0x00,
3018 scoped_ptr<QuicPacket> data(
3019 framer_.BuildUnsizedDataPacket(header, frames).packet);
3020 ASSERT_TRUE(data != NULL);
3022 test::CompareCharArraysWithHexError("constructed packet",
3023 data->data(), data->length(),
3024 AsChars(packet), arraysize(packet));
3027 TEST_P(QuicFramerTest, BuildCongestionFeedbackFramePacketTCP) {
3028 QuicPacketHeader header;
3029 header.public_header.guid = GG_UINT64_C(0xFEDCBA9876543210);
3030 header.public_header.reset_flag = false;
3031 header.public_header.version_flag = false;
3032 header.fec_flag = false;
3033 header.entropy_flag = false;
3034 header.packet_sequence_number = GG_UINT64_C(0x123456789ABC);
3035 header.fec_group = 0;
3037 QuicCongestionFeedbackFrame congestion_feedback_frame;
3038 congestion_feedback_frame.type = kTCP;
3039 congestion_feedback_frame.tcp.accumulated_number_of_lost_packets = 0x0201;
3040 congestion_feedback_frame.tcp.receive_window = 0x4030;
3042 QuicFrames frames;
3043 frames.push_back(QuicFrame(&congestion_feedback_frame));
3045 unsigned char packet[] = {
3046 // public flags (8 byte guid)
3047 0x3C,
3048 // guid
3049 0x10, 0x32, 0x54, 0x76,
3050 0x98, 0xBA, 0xDC, 0xFE,
3051 // packet sequence number
3052 0xBC, 0x9A, 0x78, 0x56,
3053 0x34, 0x12,
3054 // private flags
3055 0x00,
3057 // frame type (congestion feedback frame)
3058 0x20,
3059 // congestion feedback type (TCP)
3060 0x00,
3061 // accumulated number of lost packets
3062 0x01, 0x02,
3063 // TCP receive window
3064 0x03, 0x04,
3067 scoped_ptr<QuicPacket> data(
3068 framer_.BuildUnsizedDataPacket(header, frames).packet);
3069 ASSERT_TRUE(data != NULL);
3071 test::CompareCharArraysWithHexError("constructed packet",
3072 data->data(), data->length(),
3073 AsChars(packet), arraysize(packet));
3076 TEST_P(QuicFramerTest, BuildCongestionFeedbackFramePacketInterArrival) {
3077 QuicPacketHeader header;
3078 header.public_header.guid = GG_UINT64_C(0xFEDCBA9876543210);
3079 header.public_header.reset_flag = false;
3080 header.public_header.version_flag = false;
3081 header.fec_flag = false;
3082 header.entropy_flag = false;
3083 header.packet_sequence_number = GG_UINT64_C(0x123456789ABC);
3084 header.fec_group = 0;
3086 QuicCongestionFeedbackFrame frame;
3087 frame.type = kInterArrival;
3088 frame.inter_arrival.accumulated_number_of_lost_packets = 0x0302;
3089 frame.inter_arrival.received_packet_times.insert(
3090 make_pair(GG_UINT64_C(0x0123456789ABA),
3091 start_.Add(QuicTime::Delta::FromMicroseconds(
3092 GG_UINT64_C(0x07E1D2C3B4A59687)))));
3093 frame.inter_arrival.received_packet_times.insert(
3094 make_pair(GG_UINT64_C(0x0123456789ABB),
3095 start_.Add(QuicTime::Delta::FromMicroseconds(
3096 GG_UINT64_C(0x07E1D2C3B4A59688)))));
3097 frame.inter_arrival.received_packet_times.insert(
3098 make_pair(GG_UINT64_C(0x0123456789ABD),
3099 start_.Add(QuicTime::Delta::FromMicroseconds(
3100 GG_UINT64_C(0x07E1D2C3B4A59689)))));
3101 QuicFrames frames;
3102 frames.push_back(QuicFrame(&frame));
3104 unsigned char packet[] = {
3105 // public flags (8 byte guid)
3106 0x3C,
3107 // guid
3108 0x10, 0x32, 0x54, 0x76,
3109 0x98, 0xBA, 0xDC, 0xFE,
3110 // packet sequence number
3111 0xBC, 0x9A, 0x78, 0x56,
3112 0x34, 0x12,
3113 // private flags
3114 0x00,
3116 // frame type (congestion feedback frame)
3117 0x20,
3118 // congestion feedback type (inter arrival)
3119 0x01,
3120 // accumulated_number_of_lost_packets
3121 0x02, 0x03,
3122 // num received packets
3123 0x03,
3124 // lowest sequence number
3125 0xBA, 0x9A, 0x78, 0x56,
3126 0x34, 0x12,
3127 // receive time
3128 0x87, 0x96, 0xA5, 0xB4,
3129 0xC3, 0xD2, 0xE1, 0x07,
3130 // sequence delta
3131 0x01, 0x00,
3132 // time delta
3133 0x01, 0x00, 0x00, 0x00,
3134 // sequence delta (skip one packet)
3135 0x03, 0x00,
3136 // time delta
3137 0x02, 0x00, 0x00, 0x00,
3140 scoped_ptr<QuicPacket> data(
3141 framer_.BuildUnsizedDataPacket(header, frames).packet);
3142 ASSERT_TRUE(data != NULL);
3144 test::CompareCharArraysWithHexError("constructed packet",
3145 data->data(), data->length(),
3146 AsChars(packet), arraysize(packet));
3149 TEST_P(QuicFramerTest, BuildCongestionFeedbackFramePacketFixRate) {
3150 QuicPacketHeader header;
3151 header.public_header.guid = GG_UINT64_C(0xFEDCBA9876543210);
3152 header.public_header.reset_flag = false;
3153 header.public_header.version_flag = false;
3154 header.fec_flag = false;
3155 header.entropy_flag = false;
3156 header.packet_sequence_number = GG_UINT64_C(0x123456789ABC);
3157 header.fec_group = 0;
3159 QuicCongestionFeedbackFrame congestion_feedback_frame;
3160 congestion_feedback_frame.type = kFixRate;
3161 congestion_feedback_frame.fix_rate.bitrate
3162 = QuicBandwidth::FromBytesPerSecond(0x04030201);
3164 QuicFrames frames;
3165 frames.push_back(QuicFrame(&congestion_feedback_frame));
3167 unsigned char packet[] = {
3168 // public flags (8 byte guid)
3169 0x3C,
3170 // guid
3171 0x10, 0x32, 0x54, 0x76,
3172 0x98, 0xBA, 0xDC, 0xFE,
3173 // packet sequence number
3174 0xBC, 0x9A, 0x78, 0x56,
3175 0x34, 0x12,
3176 // private flags
3177 0x00,
3179 // frame type (congestion feedback frame)
3180 0x20,
3181 // congestion feedback type (fix rate)
3182 0x02,
3183 // bitrate_in_bytes_per_second;
3184 0x01, 0x02, 0x03, 0x04,
3187 scoped_ptr<QuicPacket> data(
3188 framer_.BuildUnsizedDataPacket(header, frames).packet);
3189 ASSERT_TRUE(data != NULL);
3191 test::CompareCharArraysWithHexError("constructed packet",
3192 data->data(), data->length(),
3193 AsChars(packet), arraysize(packet));
3196 TEST_P(QuicFramerTest, BuildCongestionFeedbackFramePacketInvalidFeedback) {
3197 QuicPacketHeader header;
3198 header.public_header.guid = GG_UINT64_C(0xFEDCBA9876543210);
3199 header.public_header.reset_flag = false;
3200 header.public_header.version_flag = false;
3201 header.fec_flag = false;
3202 header.entropy_flag = false;
3203 header.packet_sequence_number = GG_UINT64_C(0x123456789ABC);
3204 header.fec_group = 0;
3206 QuicCongestionFeedbackFrame congestion_feedback_frame;
3207 congestion_feedback_frame.type =
3208 static_cast<CongestionFeedbackType>(kFixRate + 1);
3210 QuicFrames frames;
3211 frames.push_back(QuicFrame(&congestion_feedback_frame));
3213 scoped_ptr<QuicPacket> data(
3214 framer_.BuildUnsizedDataPacket(header, frames).packet);
3215 ASSERT_TRUE(data == NULL);
3218 TEST_P(QuicFramerTest, BuildRstFramePacket) {
3219 QuicPacketHeader header;
3220 header.public_header.guid = GG_UINT64_C(0xFEDCBA9876543210);
3221 header.public_header.reset_flag = false;
3222 header.public_header.version_flag = false;
3223 header.fec_flag = false;
3224 header.entropy_flag = false;
3225 header.packet_sequence_number = GG_UINT64_C(0x123456789ABC);
3226 header.fec_group = 0;
3228 QuicRstStreamFrame rst_frame;
3229 rst_frame.stream_id = 0x01020304;
3230 rst_frame.error_code = static_cast<QuicRstStreamErrorCode>(0x05060708);
3231 rst_frame.error_details = "because I can";
3233 unsigned char packet[] = {
3234 // public flags (8 byte guid)
3235 0x3C,
3236 // guid
3237 0x10, 0x32, 0x54, 0x76,
3238 0x98, 0xBA, 0xDC, 0xFE,
3239 // packet sequence number
3240 0xBC, 0x9A, 0x78, 0x56,
3241 0x34, 0x12,
3242 // private flags
3243 0x00,
3245 // frame type (rst stream frame)
3246 0x01,
3247 // stream id
3248 0x04, 0x03, 0x02, 0x01,
3249 // error code
3250 0x08, 0x07, 0x06, 0x05,
3251 // error details length
3252 0x0d, 0x00,
3253 // error details
3254 'b', 'e', 'c', 'a',
3255 'u', 's', 'e', ' ',
3256 'I', ' ', 'c', 'a',
3257 'n',
3260 QuicFrames frames;
3261 frames.push_back(QuicFrame(&rst_frame));
3263 scoped_ptr<QuicPacket> data(
3264 framer_.BuildUnsizedDataPacket(header, frames).packet);
3265 ASSERT_TRUE(data != NULL);
3267 test::CompareCharArraysWithHexError("constructed packet",
3268 data->data(), data->length(),
3269 AsChars(packet), arraysize(packet));
3272 TEST_P(QuicFramerTest, DISABLED_BuildCloseFramePacketv11) {
3273 if (GetParam() > QUIC_VERSION_11) {
3274 return;
3276 QuicPacketHeader header;
3277 header.public_header.guid = GG_UINT64_C(0xFEDCBA9876543210);
3278 header.public_header.reset_flag = false;
3279 header.public_header.version_flag = false;
3280 header.fec_flag = false;
3281 header.entropy_flag = true;
3282 header.packet_sequence_number = GG_UINT64_C(0x123456789ABC);
3283 header.fec_group = 0;
3285 QuicConnectionCloseFrame close_frame;
3286 close_frame.error_code = static_cast<QuicErrorCode>(0x05060708);
3287 close_frame.error_details = "because I can";
3289 QuicAckFrame* ack_frame = &close_frame.ack_frame;
3290 ack_frame->received_info.entropy_hash = 0x43;
3291 ack_frame->received_info.largest_observed = GG_UINT64_C(0x0123456789ABF);
3292 ack_frame->received_info.missing_packets.insert(GG_UINT64_C(0x0123456789ABE));
3293 ack_frame->sent_info.entropy_hash = 0xE0;
3294 ack_frame->sent_info.least_unacked = GG_UINT64_C(0x0123456789AA0);
3296 QuicFrames frames;
3297 frames.push_back(QuicFrame(&close_frame));
3299 unsigned char packet[] = {
3300 // public flags (8 byte guid)
3301 0x3C,
3302 // guid
3303 0x10, 0x32, 0x54, 0x76,
3304 0x98, 0xBA, 0xDC, 0xFE,
3305 // packet sequence number
3306 0xBC, 0x9A, 0x78, 0x56,
3307 0x34, 0x12,
3308 // private flags (entropy)
3309 0x01,
3311 // frame type (connection close frame)
3312 0x02,
3313 // error code
3314 0x08, 0x07, 0x06, 0x05,
3315 // error details length
3316 0x0d, 0x00,
3317 // error details
3318 'b', 'e', 'c', 'a',
3319 'u', 's', 'e', ' ',
3320 'I', ' ', 'c', 'a',
3321 'n',
3323 // Ack frame.
3324 // entropy hash of sent packets till least awaiting - 1.
3325 0xE0,
3326 // least packet sequence number awaiting an ack
3327 0xA0, 0x9A, 0x78, 0x56,
3328 0x34, 0x12,
3329 // entropy hash of all received packets.
3330 0x43,
3331 // largest observed packet sequence number
3332 0xBF, 0x9A, 0x78, 0x56,
3333 0x34, 0x12,
3334 // Infinite delta time.
3335 0xFF, 0xFF, 0xFF, 0xFF,
3336 // num missing packets
3337 0x01,
3338 // missing packet
3339 0xBE, 0x9A, 0x78, 0x56,
3340 0x34, 0x12,
3343 scoped_ptr<QuicPacket> data(
3344 framer_.BuildUnsizedDataPacket(header, frames).packet);
3345 ASSERT_TRUE(data != NULL);
3347 test::CompareCharArraysWithHexError("constructed packet",
3348 data->data(), data->length(),
3349 AsChars(packet), arraysize(packet));
3352 TEST_P(QuicFramerTest, BuildCloseFramePacket) {
3353 if (GetParam() <= QUIC_VERSION_11) {
3354 return;
3356 QuicPacketHeader header;
3357 header.public_header.guid = GG_UINT64_C(0xFEDCBA9876543210);
3358 header.public_header.reset_flag = false;
3359 header.public_header.version_flag = false;
3360 header.fec_flag = false;
3361 header.entropy_flag = true;
3362 header.packet_sequence_number = GG_UINT64_C(0x123456789ABC);
3363 header.fec_group = 0;
3365 QuicConnectionCloseFrame close_frame;
3366 close_frame.error_code = static_cast<QuicErrorCode>(0x05060708);
3367 close_frame.error_details = "because I can";
3369 QuicFrames frames;
3370 frames.push_back(QuicFrame(&close_frame));
3372 unsigned char packet[] = {
3373 // public flags (8 byte guid)
3374 0x3C,
3375 // guid
3376 0x10, 0x32, 0x54, 0x76,
3377 0x98, 0xBA, 0xDC, 0xFE,
3378 // packet sequence number
3379 0xBC, 0x9A, 0x78, 0x56,
3380 0x34, 0x12,
3381 // private flags (entropy)
3382 0x01,
3384 // frame type (connection close frame)
3385 0x02,
3386 // error code
3387 0x08, 0x07, 0x06, 0x05,
3388 // error details length
3389 0x0d, 0x00,
3390 // error details
3391 'b', 'e', 'c', 'a',
3392 'u', 's', 'e', ' ',
3393 'I', ' ', 'c', 'a',
3394 'n',
3397 scoped_ptr<QuicPacket> data(
3398 framer_.BuildUnsizedDataPacket(header, frames).packet);
3399 ASSERT_TRUE(data != NULL);
3401 test::CompareCharArraysWithHexError("constructed packet",
3402 data->data(), data->length(),
3403 AsChars(packet), arraysize(packet));
3406 TEST_P(QuicFramerTest, BuildGoAwayPacket) {
3407 QuicPacketHeader header;
3408 header.public_header.guid = GG_UINT64_C(0xFEDCBA9876543210);
3409 header.public_header.reset_flag = false;
3410 header.public_header.version_flag = false;
3411 header.fec_flag = false;
3412 header.entropy_flag = true;
3413 header.packet_sequence_number = GG_UINT64_C(0x123456789ABC);
3414 header.fec_group = 0;
3416 QuicGoAwayFrame goaway_frame;
3417 goaway_frame.error_code = static_cast<QuicErrorCode>(0x05060708);
3418 goaway_frame.last_good_stream_id = 0x01020304;
3419 goaway_frame.reason_phrase = "because I can";
3421 QuicFrames frames;
3422 frames.push_back(QuicFrame(&goaway_frame));
3424 unsigned char packet[] = {
3425 // public flags (8 byte guid)
3426 0x3C,
3427 // guid
3428 0x10, 0x32, 0x54, 0x76,
3429 0x98, 0xBA, 0xDC, 0xFE,
3430 // packet sequence number
3431 0xBC, 0x9A, 0x78, 0x56,
3432 0x34, 0x12,
3433 // private flags(entropy)
3434 0x01,
3436 // frame type (go away frame)
3437 0x03,
3438 // error code
3439 0x08, 0x07, 0x06, 0x05,
3440 // stream id
3441 0x04, 0x03, 0x02, 0x01,
3442 // error details length
3443 0x0d, 0x00,
3444 // error details
3445 'b', 'e', 'c', 'a',
3446 'u', 's', 'e', ' ',
3447 'I', ' ', 'c', 'a',
3448 'n',
3451 scoped_ptr<QuicPacket> data(
3452 framer_.BuildUnsizedDataPacket(header, frames).packet);
3453 ASSERT_TRUE(data != NULL);
3455 test::CompareCharArraysWithHexError("constructed packet",
3456 data->data(), data->length(),
3457 AsChars(packet), arraysize(packet));
3460 TEST_P(QuicFramerTest, BuildPublicResetPacket) {
3461 QuicPublicResetPacket reset_packet;
3462 reset_packet.public_header.guid = GG_UINT64_C(0xFEDCBA9876543210);
3463 reset_packet.public_header.reset_flag = true;
3464 reset_packet.public_header.version_flag = false;
3465 reset_packet.rejected_sequence_number = GG_UINT64_C(0x123456789ABC);
3466 reset_packet.nonce_proof = GG_UINT64_C(0xABCDEF0123456789);
3468 unsigned char packet[] = {
3469 // public flags (public reset, 8 byte GUID)
3470 0x3E,
3471 // guid
3472 0x10, 0x32, 0x54, 0x76,
3473 0x98, 0xBA, 0xDC, 0xFE,
3474 // nonce proof
3475 0x89, 0x67, 0x45, 0x23,
3476 0x01, 0xEF, 0xCD, 0xAB,
3477 // rejected sequence number
3478 0xBC, 0x9A, 0x78, 0x56,
3479 0x34, 0x12,
3482 scoped_ptr<QuicEncryptedPacket> data(
3483 framer_.BuildPublicResetPacket(reset_packet));
3484 ASSERT_TRUE(data != NULL);
3486 test::CompareCharArraysWithHexError("constructed packet",
3487 data->data(), data->length(),
3488 AsChars(packet), arraysize(packet));
3491 TEST_P(QuicFramerTest, BuildFecPacket) {
3492 QuicPacketHeader header;
3493 header.public_header.guid = GG_UINT64_C(0xFEDCBA9876543210);
3494 header.public_header.reset_flag = false;
3495 header.public_header.version_flag = false;
3496 header.fec_flag = true;
3497 header.entropy_flag = true;
3498 header.packet_sequence_number = (GG_UINT64_C(0x123456789ABC));
3499 header.is_in_fec_group = IN_FEC_GROUP;
3500 header.fec_group = GG_UINT64_C(0x123456789ABB);;
3502 QuicFecData fec_data;
3503 fec_data.fec_group = 1;
3504 fec_data.redundancy = "abcdefghijklmnop";
3506 unsigned char packet[] = {
3507 // public flags (8 byte guid)
3508 0x3C,
3509 // guid
3510 0x10, 0x32, 0x54, 0x76,
3511 0x98, 0xBA, 0xDC, 0xFE,
3512 // packet sequence number
3513 0xBC, 0x9A, 0x78, 0x56,
3514 0x34, 0x12,
3515 // private flags (entropy & fec group & fec packet)
3516 0x07,
3517 // first fec protected packet offset
3518 0x01,
3520 // redundancy
3521 'a', 'b', 'c', 'd',
3522 'e', 'f', 'g', 'h',
3523 'i', 'j', 'k', 'l',
3524 'm', 'n', 'o', 'p',
3527 scoped_ptr<QuicPacket> data(
3528 framer_.BuildFecPacket(header, fec_data).packet);
3529 ASSERT_TRUE(data != NULL);
3531 test::CompareCharArraysWithHexError("constructed packet",
3532 data->data(), data->length(),
3533 AsChars(packet), arraysize(packet));
3536 TEST_P(QuicFramerTest, EncryptPacket) {
3537 QuicPacketSequenceNumber sequence_number = GG_UINT64_C(0x123456789ABC);
3538 unsigned char packet[] = {
3539 // public flags (8 byte guid)
3540 0x3C,
3541 // guid
3542 0x10, 0x32, 0x54, 0x76,
3543 0x98, 0xBA, 0xDC, 0xFE,
3544 // packet sequence number
3545 0xBC, 0x9A, 0x78, 0x56,
3546 0x34, 0x12,
3547 // private flags (fec group & fec packet)
3548 0x06,
3549 // first fec protected packet offset
3550 0x01,
3552 // redundancy
3553 'a', 'b', 'c', 'd',
3554 'e', 'f', 'g', 'h',
3555 'i', 'j', 'k', 'l',
3556 'm', 'n', 'o', 'p',
3559 scoped_ptr<QuicPacket> raw(
3560 QuicPacket::NewDataPacket(AsChars(packet), arraysize(packet), false,
3561 PACKET_8BYTE_GUID, !kIncludeVersion,
3562 PACKET_6BYTE_SEQUENCE_NUMBER));
3563 scoped_ptr<QuicEncryptedPacket> encrypted(
3564 framer_.EncryptPacket(ENCRYPTION_NONE, sequence_number, *raw));
3566 ASSERT_TRUE(encrypted.get() != NULL);
3567 EXPECT_TRUE(CheckEncryption(sequence_number, raw.get()));
3570 TEST_P(QuicFramerTest, EncryptPacketWithVersionFlag) {
3571 QuicPacketSequenceNumber sequence_number = GG_UINT64_C(0x123456789ABC);
3572 unsigned char packet[] = {
3573 // public flags (version, 8 byte guid)
3574 0x3D,
3575 // guid
3576 0x10, 0x32, 0x54, 0x76,
3577 0x98, 0xBA, 0xDC, 0xFE,
3578 // version tag
3579 'Q', '.', '1', '0',
3580 // packet sequence number
3581 0xBC, 0x9A, 0x78, 0x56,
3582 0x34, 0x12,
3583 // private flags (fec group & fec flags)
3584 0x06,
3585 // first fec protected packet offset
3586 0x01,
3588 // redundancy
3589 'a', 'b', 'c', 'd',
3590 'e', 'f', 'g', 'h',
3591 'i', 'j', 'k', 'l',
3592 'm', 'n', 'o', 'p',
3595 scoped_ptr<QuicPacket> raw(
3596 QuicPacket::NewDataPacket(AsChars(packet), arraysize(packet), false,
3597 PACKET_8BYTE_GUID, kIncludeVersion,
3598 PACKET_6BYTE_SEQUENCE_NUMBER));
3599 scoped_ptr<QuicEncryptedPacket> encrypted(
3600 framer_.EncryptPacket(ENCRYPTION_NONE, sequence_number, *raw));
3602 ASSERT_TRUE(encrypted.get() != NULL);
3603 EXPECT_TRUE(CheckEncryption(sequence_number, raw.get()));
3606 // TODO(rch): re-enable after https://codereview.chromium.org/11820005/
3607 // lands. Currently this is causing valgrind problems, but it should be
3608 // fixed in the followup CL.
3609 TEST_P(QuicFramerTest, DISABLED_CalculateLargestReceived) {
3610 SequenceNumberSet missing;
3611 missing.insert(1);
3612 missing.insert(5);
3613 missing.insert(7);
3615 // These two we just walk to the next gap, and return the largest seen.
3616 EXPECT_EQ(4u, QuicFramer::CalculateLargestObserved(missing, missing.find(1)));
3617 EXPECT_EQ(6u, QuicFramer::CalculateLargestObserved(missing, missing.find(5)));
3619 missing.insert(2);
3620 // For 1, we can't go forward as 2 would be implicitly acked so we return the
3621 // largest missing packet.
3622 EXPECT_EQ(1u, QuicFramer::CalculateLargestObserved(missing, missing.find(1)));
3623 // For 2, we've seen 3 and 4, so can admit to a largest observed.
3624 EXPECT_EQ(4u, QuicFramer::CalculateLargestObserved(missing, missing.find(2)));
3627 // TODO(rch) enable after landing the revised truncation CL.
3628 TEST_P(QuicFramerTest, DISABLED_Truncation) {
3629 QuicPacketHeader header;
3630 header.public_header.guid = GG_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_sequence_number = GG_UINT64_C(0x123456789ABC);
3636 header.fec_group = 0;
3638 QuicConnectionCloseFrame close_frame;
3639 QuicAckFrame* ack_frame = &close_frame.ack_frame;
3640 close_frame.error_code = static_cast<QuicErrorCode>(0x05);
3641 close_frame.error_details = "because I can";
3642 ack_frame->received_info.largest_observed = 601;
3643 ack_frame->sent_info.least_unacked = 0;
3644 for (uint64 i = 1; i < ack_frame->received_info.largest_observed; i += 2) {
3645 ack_frame->received_info.missing_packets.insert(i);
3648 // Create a packet with just the ack
3649 QuicFrame frame;
3650 frame.type = ACK_FRAME;
3651 frame.ack_frame = ack_frame;
3652 QuicFrames frames;
3653 frames.push_back(frame);
3655 scoped_ptr<QuicPacket> raw_ack_packet(
3656 framer_.BuildUnsizedDataPacket(header, frames).packet);
3657 ASSERT_TRUE(raw_ack_packet != NULL);
3659 scoped_ptr<QuicEncryptedPacket> ack_packet(
3660 framer_.EncryptPacket(ENCRYPTION_NONE, header.packet_sequence_number,
3661 *raw_ack_packet));
3663 // Create a packet with just connection close.
3664 // TODO(ianswett): Remove this section when v11 is retired because v12 no
3665 // longer embeds an ack frame in the connection close frame.
3666 frames.clear();
3667 frame.type = CONNECTION_CLOSE_FRAME;
3668 frame.connection_close_frame = &close_frame;
3669 frames.push_back(frame);
3671 scoped_ptr<QuicPacket> raw_close_packet(
3672 framer_.BuildUnsizedDataPacket(header, frames).packet);
3673 ASSERT_TRUE(raw_close_packet != NULL);
3675 scoped_ptr<QuicEncryptedPacket> close_packet(
3676 framer_.EncryptPacket(ENCRYPTION_NONE, header.packet_sequence_number,
3677 *raw_close_packet));
3679 // Now make sure we can turn our ack packet back into an ack frame
3680 ASSERT_TRUE(framer_.ProcessPacket(*ack_packet));
3681 ASSERT_EQ(1u, visitor_.ack_frames_.size());
3682 const QuicAckFrame& processed_ack_frame = *visitor_.ack_frames_[0];
3683 EXPECT_EQ(0u, processed_ack_frame.sent_info.least_unacked);
3684 if (GetParam() > QUIC_VERSION_11) {
3685 EXPECT_TRUE(processed_ack_frame.received_info.is_truncated);
3686 EXPECT_EQ(510u, processed_ack_frame.received_info.largest_observed);
3687 ASSERT_EQ(255u, processed_ack_frame.received_info.missing_packets.size());
3688 SequenceNumberSet::const_iterator missing_iter =
3689 processed_ack_frame.received_info.missing_packets.begin();
3690 EXPECT_EQ(1u, *missing_iter);
3691 SequenceNumberSet::const_reverse_iterator last_missing_iter =
3692 processed_ack_frame.received_info.missing_packets.rbegin();
3693 EXPECT_EQ(509u, *last_missing_iter);
3696 // And do the same for the close frame.
3697 ASSERT_TRUE(framer_.ProcessPacket(*close_packet));
3700 TEST_P(QuicFramerTest, CleanTruncation) {
3701 QuicPacketHeader header;
3702 header.public_header.guid = GG_UINT64_C(0xFEDCBA9876543210);
3703 header.public_header.reset_flag = false;
3704 header.public_header.version_flag = false;
3705 header.fec_flag = false;
3706 header.entropy_flag = true;
3707 header.packet_sequence_number = GG_UINT64_C(0x123456789ABC);
3708 header.fec_group = 0;
3710 QuicConnectionCloseFrame close_frame;
3711 QuicAckFrame* ack_frame = &close_frame.ack_frame;
3712 close_frame.error_code = static_cast<QuicErrorCode>(0x05);
3713 close_frame.error_details = "because I can";
3714 ack_frame->received_info.largest_observed = 201;
3715 ack_frame->sent_info.least_unacked = 0;
3716 for (uint64 i = 1; i < ack_frame->received_info.largest_observed; ++i) {
3717 ack_frame->received_info.missing_packets.insert(i);
3720 // Create a packet with just the ack
3721 QuicFrame frame;
3722 frame.type = ACK_FRAME;
3723 frame.ack_frame = ack_frame;
3724 QuicFrames frames;
3725 frames.push_back(frame);
3727 scoped_ptr<QuicPacket> raw_ack_packet(
3728 framer_.BuildUnsizedDataPacket(header, frames).packet);
3729 ASSERT_TRUE(raw_ack_packet != NULL);
3731 scoped_ptr<QuicEncryptedPacket> ack_packet(
3732 framer_.EncryptPacket(ENCRYPTION_NONE, header.packet_sequence_number,
3733 *raw_ack_packet));
3735 // Create a packet with just connection close.
3736 frames.clear();
3737 frame.type = CONNECTION_CLOSE_FRAME;
3738 frame.connection_close_frame = &close_frame;
3739 frames.push_back(frame);
3741 scoped_ptr<QuicPacket> raw_close_packet(
3742 framer_.BuildUnsizedDataPacket(header, frames).packet);
3743 ASSERT_TRUE(raw_close_packet != NULL);
3745 scoped_ptr<QuicEncryptedPacket> close_packet(
3746 framer_.EncryptPacket(ENCRYPTION_NONE, header.packet_sequence_number,
3747 *raw_close_packet));
3749 // Now make sure we can turn our ack packet back into an ack frame
3750 ASSERT_TRUE(framer_.ProcessPacket(*ack_packet));
3752 // And do the same for the close frame.
3753 ASSERT_TRUE(framer_.ProcessPacket(*close_packet));
3755 // Test for clean truncation of the ack by comparing the length of the
3756 // original packets to the re-serialized packets.
3757 frames.clear();
3758 frame.type = ACK_FRAME;
3759 frame.ack_frame = visitor_.ack_frames_[0];
3760 frames.push_back(frame);
3762 size_t original_raw_length = raw_ack_packet->length();
3763 raw_ack_packet.reset(
3764 framer_.BuildUnsizedDataPacket(header, frames).packet);
3765 ASSERT_TRUE(raw_ack_packet != NULL);
3766 EXPECT_EQ(original_raw_length, raw_ack_packet->length());
3768 frames.clear();
3769 frame.type = CONNECTION_CLOSE_FRAME;
3770 frame.connection_close_frame = &visitor_.connection_close_frame_;
3771 frames.push_back(frame);
3773 original_raw_length = raw_close_packet->length();
3774 raw_close_packet.reset(
3775 framer_.BuildUnsizedDataPacket(header, frames).packet);
3776 ASSERT_TRUE(raw_ack_packet != NULL);
3777 EXPECT_EQ(original_raw_length, raw_close_packet->length());
3780 TEST_P(QuicFramerTest, EntropyFlagTest) {
3781 unsigned char packet[] = {
3782 // public flags (8 byte guid)
3783 0x3C,
3784 // guid
3785 0x10, 0x32, 0x54, 0x76,
3786 0x98, 0xBA, 0xDC, 0xFE,
3787 // packet sequence number
3788 0xBC, 0x9A, 0x78, 0x56,
3789 0x34, 0x12,
3790 // private flags (Entropy)
3791 0x01,
3793 // frame type (stream frame with fin and no length)
3794 0xDF,
3795 // stream id
3796 0x04, 0x03, 0x02, 0x01,
3797 // offset
3798 0x54, 0x76, 0x10, 0x32,
3799 0xDC, 0xFE, 0x98, 0xBA,
3800 // data
3801 'h', 'e', 'l', 'l',
3802 'o', ' ', 'w', 'o',
3803 'r', 'l', 'd', '!',
3806 QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
3807 EXPECT_TRUE(framer_.ProcessPacket(encrypted));
3808 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
3809 ASSERT_TRUE(visitor_.header_.get());
3810 EXPECT_TRUE(visitor_.header_->entropy_flag);
3811 EXPECT_EQ(1 << 4, visitor_.header_->entropy_hash);
3812 EXPECT_FALSE(visitor_.header_->fec_flag);
3815 TEST_P(QuicFramerTest, FecEntropyTest) {
3816 unsigned char packet[] = {
3817 // public flags (8 byte guid)
3818 0x3C,
3819 // guid
3820 0x10, 0x32, 0x54, 0x76,
3821 0x98, 0xBA, 0xDC, 0xFE,
3822 // packet sequence number
3823 0xBC, 0x9A, 0x78, 0x56,
3824 0x34, 0x12,
3825 // private flags (Entropy & fec group & FEC)
3826 0x07,
3827 // first fec protected packet offset
3828 0xFF,
3830 // frame type (stream frame with fin and no length)
3831 0xDF,
3832 // stream id
3833 0x04, 0x03, 0x02, 0x01,
3834 // offset
3835 0x54, 0x76, 0x10, 0x32,
3836 0xDC, 0xFE, 0x98, 0xBA,
3837 // data
3838 'h', 'e', 'l', 'l',
3839 'o', ' ', 'w', 'o',
3840 'r', 'l', 'd', '!',
3843 QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
3844 EXPECT_TRUE(framer_.ProcessPacket(encrypted));
3845 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
3846 ASSERT_TRUE(visitor_.header_.get());
3847 EXPECT_TRUE(visitor_.header_->fec_flag);
3848 EXPECT_TRUE(visitor_.header_->entropy_flag);
3849 EXPECT_EQ(1 << 4, visitor_.header_->entropy_hash);
3852 TEST_P(QuicFramerTest, StopPacketProcessing) {
3853 unsigned char packet[] = {
3854 // public flags (8 byte guid)
3855 0x3C,
3856 // guid
3857 0x10, 0x32, 0x54, 0x76,
3858 0x98, 0xBA, 0xDC, 0xFE,
3859 // packet sequence number
3860 0xBC, 0x9A, 0x78, 0x56,
3861 0x34, 0x12,
3862 // Entropy
3863 0x01,
3865 // frame type (stream frame with fin)
3866 0xFF,
3867 // stream id
3868 0x04, 0x03, 0x02, 0x01,
3869 // offset
3870 0x54, 0x76, 0x10, 0x32,
3871 0xDC, 0xFE, 0x98, 0xBA,
3872 // data length
3873 0x0c, 0x00,
3874 // data
3875 'h', 'e', 'l', 'l',
3876 'o', ' ', 'w', 'o',
3877 'r', 'l', 'd', '!',
3879 // frame type (ack frame)
3880 0x40,
3881 // entropy hash of sent packets till least awaiting - 1.
3882 0x14,
3883 // least packet sequence number awaiting an ack
3884 0xA0, 0x9A, 0x78, 0x56,
3885 0x34, 0x12,
3886 // entropy hash of all received packets.
3887 0x43,
3888 // largest observed packet sequence number
3889 0xBF, 0x9A, 0x78, 0x56,
3890 0x34, 0x12,
3891 // num missing packets
3892 0x01,
3893 // missing packet
3894 0xBE, 0x9A, 0x78, 0x56,
3895 0x34, 0x12,
3898 MockFramerVisitor visitor;
3899 framer_.set_visitor(&visitor);
3900 EXPECT_CALL(visitor, OnPacket());
3901 EXPECT_CALL(visitor, OnPacketHeader(_));
3902 EXPECT_CALL(visitor, OnStreamFrame(_)).WillOnce(Return(false));
3903 EXPECT_CALL(visitor, OnAckFrame(_)).Times(0);
3904 EXPECT_CALL(visitor, OnPacketComplete());
3906 QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
3907 EXPECT_TRUE(framer_.ProcessPacket(encrypted));
3908 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
3911 TEST_P(QuicFramerTest, DISABLED_ConnectionCloseWithInvalidAck) {
3912 if (GetParam() > QUIC_VERSION_11) {
3913 return;
3915 unsigned char packet[] = {
3916 // public flags (8 byte guid)
3917 0x3C,
3918 // guid
3919 0x10, 0x32, 0x54, 0x76,
3920 0x98, 0xBA, 0xDC, 0xFE,
3921 // packet sequence number
3922 0xBC, 0x9A, 0x78, 0x56,
3923 0x34, 0x12,
3924 // private flags
3925 0x00,
3927 // frame type (connection close frame)
3928 0x02,
3929 // error code
3930 0x11, 0x00, 0x00, 0x00,
3931 // error details length
3932 0x0d, 0x00,
3933 // error details
3934 'b', 'e', 'c', 'a',
3935 'u', 's', 'e', ' ',
3936 'I', ' ', 'c', 'a',
3937 'n',
3939 // Ack frame.
3940 // entropy hash of sent packets till least awaiting - 1.
3941 0xE0,
3942 // least packet sequence number awaiting an ack
3943 0xA0, 0x9A, 0x78, 0x56,
3944 0x34, 0x12,
3945 // entropy hash of all received packets.
3946 0x43,
3947 // largest observed packet sequence number
3948 0xBF, 0x9A, 0x78, 0x56,
3949 0x34, 0x12,
3950 // Infinite delta time.
3951 0xFF, 0xFF, 0xFF, 0xFF,
3952 // num missing packets
3953 0x01,
3954 // missing packet
3955 0xBE, 0x9A, 0x78, 0x56,
3956 0x34, 0x12,
3959 MockFramerVisitor visitor;
3960 framer_.set_visitor(&visitor);
3961 EXPECT_CALL(visitor, OnPacket());
3962 EXPECT_CALL(visitor, OnPacketHeader(_));
3963 EXPECT_CALL(visitor, OnAckFrame(_)).WillOnce(Return(false));
3964 EXPECT_CALL(visitor, OnConnectionCloseFrame(_)).Times(0);
3965 EXPECT_CALL(visitor, OnPacketComplete());
3967 QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
3968 EXPECT_TRUE(framer_.ProcessPacket(encrypted));
3969 EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
3972 } // namespace test
3973 } // namespace net