Home | History | Annotate | Download | only in quic
      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.
      4 
      5 #include <algorithm>
      6 #include <map>
      7 #include <string>
      8 #include <vector>
      9 
     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"
     22 
     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::_;
     33 
     34 namespace net {
     35 namespace test {
     36 
     37 const QuicPacketSequenceNumber kEpoch = GG_UINT64_C(1) << 48;
     38 const QuicPacketSequenceNumber kMask = kEpoch - 1;
     39 
     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;
     44 
     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);
     50 }
     51 
     52 size_t GetSequenceNumberOffset(bool include_version) {
     53   return GetSequenceNumberOffset(PACKET_8BYTE_GUID, include_version);
     54 }
     55 
     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;
     60 }
     61 
     62 size_t GetPrivateFlagsOffset(bool include_version) {
     63   return GetPrivateFlagsOffset(PACKET_8BYTE_GUID, include_version);
     64 }
     65 
     66 size_t GetPrivateFlagsOffset(bool include_version,
     67                              QuicSequenceNumberLength sequence_number_length) {
     68   return GetSequenceNumberOffset(PACKET_8BYTE_GUID, include_version) +
     69       sequence_number_length;
     70 }
     71 
     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;
     76 }
     77 
     78 size_t GetFecGroupOffset(bool include_version) {
     79   return GetPrivateFlagsOffset(PACKET_8BYTE_GUID, include_version) +
     80       kPrivateFlagsSize;
     81 }
     82 
     83 size_t GetFecGroupOffset(bool include_version,
     84                          QuicSequenceNumberLength sequence_number_length) {
     85   return GetPrivateFlagsOffset(include_version, sequence_number_length) +
     86       kPrivateFlagsSize;
     87 }
     88 
     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;
     93 
     94 // Index into the rejected sequence number of the public reset packet.
     95 const size_t kPublicResetPacketRejectedSequenceNumberOffset =
     96     kPublicResetPacketNonceProofOffset + kPublicResetNonceSize;
     97 
     98 class TestEncrypter : public QuicEncrypter {
     99  public:
    100   virtual ~TestEncrypter() {}
    101   virtual bool SetKey(StringPiece key) OVERRIDE {
    102     return true;
    103   }
    104   virtual bool SetNoncePrefix(StringPiece nonce_prefix) OVERRIDE {
    105     return true;
    106   }
    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;
    113   }
    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());
    121   }
    122   virtual size_t GetKeySize() const OVERRIDE {
    123     return 0;
    124   }
    125   virtual size_t GetNoncePrefixSize() const OVERRIDE {
    126     return 0;
    127   }
    128   virtual size_t GetMaxPlaintextSize(size_t ciphertext_size) const OVERRIDE {
    129     return ciphertext_size;
    130   }
    131   virtual size_t GetCiphertextSize(size_t plaintext_size) const OVERRIDE {
    132     return plaintext_size;
    133   }
    134   virtual StringPiece GetKey() const OVERRIDE {
    135     return StringPiece();
    136   }
    137   virtual StringPiece GetNoncePrefix() const OVERRIDE {
    138     return StringPiece();
    139   }
    140   QuicPacketSequenceNumber sequence_number_;
    141   string associated_data_;
    142   string plaintext_;
    143 };
    144 
    145 class TestDecrypter : public QuicDecrypter {
    146  public:
    147   virtual ~TestDecrypter() {}
    148   virtual bool SetKey(StringPiece key) OVERRIDE {
    149     return true;
    150   }
    151   virtual bool SetNoncePrefix(StringPiece nonce_prefix) OVERRIDE {
    152     return true;
    153   }
    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;
    161   }
    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());
    169   }
    170   virtual StringPiece GetKey() const OVERRIDE {
    171     return StringPiece();
    172   }
    173   virtual StringPiece GetNoncePrefix() const OVERRIDE {
    174     return StringPiece();
    175   }
    176   QuicPacketSequenceNumber sequence_number_;
    177   string associated_data_;
    178   string ciphertext_;
    179 };
    180 
    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) {
    192   }
    193 
    194   virtual ~TestQuicVisitor() {
    195     STLDeleteElements(&stream_frames_);
    196     STLDeleteElements(&ack_frames_);
    197     STLDeleteElements(&congestion_feedback_frames_);
    198     STLDeleteElements(&fec_data_);
    199   }
    200 
    201   virtual void OnError(QuicFramer* f) OVERRIDE {
    202     DLOG(INFO) << "QuicFramer Error: " << QuicUtils::ErrorToString(f->error())
    203                << " (" << f->error() << ")";
    204     error_count_++;
    205   }
    206 
    207   virtual void OnPacket() OVERRIDE {}
    208 
    209   virtual void OnPublicResetPacket(
    210       const QuicPublicResetPacket& packet) OVERRIDE {
    211     public_reset_packet_.reset(new QuicPublicResetPacket(packet));
    212   }
    213 
    214   virtual void OnVersionNegotiationPacket(
    215       const QuicVersionNegotiationPacket& packet) OVERRIDE {
    216     version_negotiation_packet_.reset(new QuicVersionNegotiationPacket(packet));
    217   }
    218 
    219   virtual void OnRevivedPacket() OVERRIDE {
    220     revived_packets_++;
    221   }
    222 
    223   virtual bool OnProtocolVersionMismatch(QuicVersion version) OVERRIDE {
    224     DLOG(INFO) << "QuicFramer Version Mismatch, version: " << version;
    225     version_mismatch_++;
    226     return true;
    227   }
    228 
    229   virtual bool OnPacketHeader(const QuicPacketHeader& header) OVERRIDE {
    230     packet_count_++;
    231     header_.reset(new QuicPacketHeader(header));
    232     return accept_packet_;
    233   }
    234 
    235   virtual bool OnUnauthenticatedHeader(
    236       const QuicPacketHeader& header) OVERRIDE {
    237     return true;
    238   }
    239 
    240   virtual bool OnStreamFrame(const QuicStreamFrame& frame) OVERRIDE {
    241     frame_count_++;
    242     stream_frames_.push_back(new QuicStreamFrame(frame));
    243     return true;
    244   }
    245 
    246   virtual void OnFecProtectedPayload(StringPiece payload) OVERRIDE {
    247     fec_protected_payload_ = payload.as_string();
    248   }
    249 
    250   virtual bool OnAckFrame(const QuicAckFrame& frame) OVERRIDE {
    251     frame_count_++;
    252     ack_frames_.push_back(new QuicAckFrame(frame));
    253     return true;
    254   }
    255 
    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;
    262   }
    263 
    264   virtual void OnFecData(const QuicFecData& fec) OVERRIDE {
    265     fec_count_++;
    266     fec_data_.push_back(new QuicFecData(fec));
    267   }
    268 
    269   virtual void OnPacketComplete() OVERRIDE {
    270     complete_packets_++;
    271   }
    272 
    273   virtual bool OnRstStreamFrame(const QuicRstStreamFrame& frame) OVERRIDE {
    274     rst_stream_frame_ = frame;
    275     return true;
    276   }
    277 
    278   virtual bool OnConnectionCloseFrame(
    279       const QuicConnectionCloseFrame& frame) OVERRIDE {
    280     connection_close_frame_ = frame;
    281     return true;
    282   }
    283 
    284   virtual bool OnGoAwayFrame(const QuicGoAwayFrame& frame) OVERRIDE {
    285     goaway_frame_ = frame;
    286     return true;
    287   }
    288 
    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_;
    298 
    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_;
    310 };
    311 
    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_);
    325   }
    326 
    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);
    331   }
    332 
    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);
    337   }
    338 
    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;
    346     }
    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;
    352     }
    353     if (packet->Plaintext() != encrypter_->plaintext_) {
    354       LOG(ERROR) << "Encrypted incorrect plaintext data.  expected "
    355                  << packet->Plaintext() << " actual: "
    356                  << encrypter_->plaintext_;
    357       return false;
    358     }
    359     return true;
    360   }
    361 
    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;
    370     }
    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;
    381     }
    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;
    390     }
    391     return true;
    392   }
    393 
    394   char* AsChars(unsigned char* data) {
    395     return reinterpret_cast<char*>(data);
    396   }
    397 
    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;
    406   }
    407 
    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);
    412   }
    413 
    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.";
    428       }
    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);
    435     }
    436   }
    437 
    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;
    449   }
    450 
    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_;
    458 };
    459 
    460 // Run all framer tests with all supported versions of QUIC.
    461 INSTANTIATE_TEST_CASE_P(QuicFramerTests,
    462                         QuicFramerTest,
    463                         ::testing::ValuesIn(kSupportedQuicVersions));
    464 
    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);
    470 
    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);
    476     }
    477 
    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);
    481     }
    482   }
    483 }
    484 
    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;
    489 
    490     // Small numbers should wrap.
    491     for (uint64 j = 0; j < 10; j++) {
    492       CheckCalculatePacketSequenceNumber(kEpoch + j, last);
    493     }
    494 
    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);
    498     }
    499   }
    500 }
    501 
    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);
    513     }
    514 
    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);
    519     }
    520   }
    521 }
    522 
    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;
    529 
    530     // Small numbers should wrap.
    531     for (uint64 j = 0; j < 10; j++) {
    532       CheckCalculatePacketSequenceNumber(next_epoch + j, last);
    533     }
    534 
    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);
    539     }
    540   }
    541 }
    542 
    543 TEST_P(QuicFramerTest, CalculatePacketSequenceNumberFromWireNearNextMax) {
    544   const uint64 max_number = numeric_limits<uint64>::max();
    545   const uint64 max_epoch = max_number & ~kMask;
    546 
    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;
    552 
    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);
    556     }
    557 
    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);
    562     }
    563   }
    564 }
    565 
    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());
    571 }
    572 
    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,
    585   };
    586 
    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);
    593 
    594   QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
    595   EXPECT_FALSE(framer_.ProcessPacket(encrypted));
    596 
    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());
    603 }
    604 
    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,
    617   };
    618 
    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);
    634 
    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.";
    651     }
    652     CheckProcessingFails(packet, i, expected_error, QUIC_INVALID_PACKET_HEADER);
    653   }
    654 }
    655 
    656 TEST_P(QuicFramerTest, PacketHeaderWith4ByteGuid) {
    657   QuicFramerPeer::SetLastSerializedGuid(&framer_,
    658                                         GG_UINT64_C(0xFEDCBA9876543210));
    659 
    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,
    670   };
    671 
    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);
    687 
    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.";
    706     }
    707     CheckProcessingFails(packet, i, expected_error, QUIC_INVALID_PACKET_HEADER);
    708   }
    709 }
    710 
    711 TEST_P(QuicFramerTest, PacketHeader1ByteGuid) {
    712   QuicFramerPeer::SetLastSerializedGuid(&framer_,
    713                                         GG_UINT64_C(0xFEDCBA9876543210));
    714 
    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,
    725   };
    726 
    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);
    742 
    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.";
    760     }
    761     CheckProcessingFails(packet, i, expected_error, QUIC_INVALID_PACKET_HEADER);
    762   }
    763 }
    764 
    765 TEST_P(QuicFramerTest, PacketHeaderWith0ByteGuid) {
    766   QuicFramerPeer::SetLastSerializedGuid(&framer_,
    767                                         GG_UINT64_C(0xFEDCBA9876543210));
    768 
    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,
    778   };
    779 
    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);
    795 
    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.";
    813     }
    814     CheckProcessingFails(packet, i, expected_error, QUIC_INVALID_PACKET_HEADER);
    815   }
    816 }
    817 
    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,
    832   };
    833 
    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);
    850 
    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.";
    869     }
    870     CheckProcessingFails(packet, i, expected_error, QUIC_INVALID_PACKET_HEADER);
    871   }
    872 }
    873 
    874 TEST_P(QuicFramerTest, PacketHeaderWith4ByteSequenceNumber) {
    875   QuicFramerPeer::SetLastSequenceNumber(&framer_,
    876                                         GG_UINT64_C(0x123456789ABA));
    877 
    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,
    888   };
    889 
    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);
    905 
    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.";
    924     }
    925     CheckProcessingFails(packet, i, expected_error, QUIC_INVALID_PACKET_HEADER);
    926   }
    927 }
    928 
    929 TEST_P(QuicFramerTest, PacketHeaderWith2ByteSequenceNumber) {
    930   QuicFramerPeer::SetLastSequenceNumber(&framer_,
    931                                         GG_UINT64_C(0x123456789ABA));
    932 
    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,
    943   };
    944 
    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);
    960 
    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.";
    979     }
    980     CheckProcessingFails(packet, i, expected_error, QUIC_INVALID_PACKET_HEADER);
    981   }
    982 }
    983 
    984 TEST_P(QuicFramerTest, PacketHeaderWith1ByteSequenceNumber) {
    985   QuicFramerPeer::SetLastSequenceNumber(&framer_,
    986                                         GG_UINT64_C(0x123456789ABA));
    987 
    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,
    998   };
    999 
   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);
   1015 
   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.";
   1034     }
   1035     CheckProcessingFails(packet, i, expected_error, QUIC_INVALID_PACKET_HEADER);
   1036   }
   1037 }
   1038 
   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,
   1051 
   1052     // frame type (padding)
   1053     0x00,
   1054     0x00, 0x00, 0x00, 0x00
   1055   };
   1056   CheckProcessingFails(packet,
   1057                        arraysize(packet),
   1058                        "Illegal public flags value.",
   1059                        QUIC_INVALID_PACKET_HEADER);
   1060 };
   1061 
   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,
   1076 
   1077     // frame type (padding)
   1078     0x00,
   1079     0x00, 0x00, 0x00, 0x00
   1080   };
   1081   CheckProcessingFails(packet,
   1082                        arraysize(packet),
   1083                        "Illegal public flags value.",
   1084                        QUIC_INVALID_PACKET_HEADER);
   1085 };
   1086 
   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,
   1101 
   1102     // frame type (padding frame)
   1103     0x00,
   1104     0x00, 0x00, 0x00, 0x00
   1105   };
   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_);
   1112 };
   1113 
   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,
   1126 
   1127     // frame type (padding)
   1128     0x00,
   1129     0x00, 0x00, 0x00, 0x00
   1130   };
   1131   CheckProcessingFails(packet,
   1132                        arraysize(packet),
   1133                        "Illegal private flags value.",
   1134                        QUIC_INVALID_PACKET_HEADER);
   1135 };
   1136 
   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
   1151   };
   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);
   1157 };
   1158 
   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,
   1171 
   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',  '!',
   1188   };
   1189 
   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));
   1195 
   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);
   1204 }
   1205 
   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,
   1218 
   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',  '!',
   1232   };
   1233 
   1234   QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
   1235   EXPECT_TRUE(framer_.ProcessPacket(encrypted));
   1236 
   1237   EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
   1238   ASSERT_TRUE(visitor_.header_.get());
   1239   EXPECT_TRUE(CheckDecryption(encrypted, !kIncludeVersion));
   1240 
   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]);
   1249 
   1250   // Now test framing boundaries
   1251   CheckStreamFrameBoundaries(packet, kQuicMaxStreamIdSize, !kIncludeVersion);
   1252 }
   1253 
   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,
   1266 
   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',  '!',
   1280   };
   1281 
   1282   QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
   1283   EXPECT_TRUE(framer_.ProcessPacket(encrypted));
   1284 
   1285   EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
   1286   ASSERT_TRUE(visitor_.header_.get());
   1287   EXPECT_TRUE(CheckDecryption(encrypted, !kIncludeVersion));
   1288 
   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]);
   1297 
   1298   // Now test framing boundaries
   1299   const size_t stream_id_size = 3;
   1300   CheckStreamFrameBoundaries(packet, stream_id_size, !kIncludeVersion);
   1301 }
   1302 
   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,
   1315 
   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',  '!',
   1329   };
   1330 
   1331   QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
   1332   EXPECT_TRUE(framer_.ProcessPacket(encrypted));
   1333 
   1334   EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
   1335   ASSERT_TRUE(visitor_.header_.get());
   1336   EXPECT_TRUE(CheckDecryption(encrypted, !kIncludeVersion));
   1337 
   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]);
   1346 
   1347   // Now test framing boundaries
   1348   const size_t stream_id_size = 2;
   1349   CheckStreamFrameBoundaries(packet, stream_id_size, !kIncludeVersion);
   1350 }
   1351 
   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,
   1364 
   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',  '!',
   1378   };
   1379 
   1380   QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
   1381   EXPECT_TRUE(framer_.ProcessPacket(encrypted));
   1382 
   1383   EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
   1384   ASSERT_TRUE(visitor_.header_.get());
   1385   EXPECT_TRUE(CheckDecryption(encrypted, !kIncludeVersion));
   1386 
   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]);
   1395 
   1396   // Now test framing boundaries
   1397   const size_t stream_id_size = 1;
   1398   CheckStreamFrameBoundaries(packet, stream_id_size, !kIncludeVersion);
   1399 }
   1400 
   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,
   1415 
   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',  '!',
   1429   };
   1430 
   1431   QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
   1432   EXPECT_TRUE(framer_.ProcessPacket(encrypted));
   1433 
   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));
   1439 
   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]);
   1448 
   1449   // Now test framing boundaries
   1450   CheckStreamFrameBoundaries(packet, kQuicMaxStreamIdSize, kIncludeVersion);
   1451 }
   1452 
   1453 TEST_P(QuicFramerTest, RejectPacket) {
   1454   visitor_.accept_packet_ = false;
   1455 
   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,
   1467 
   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',  '!',
   1481   };
   1482 
   1483   QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
   1484   EXPECT_TRUE(framer_.ProcessPacket(encrypted));
   1485 
   1486   EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
   1487   ASSERT_TRUE(visitor_.header_.get());
   1488   EXPECT_TRUE(CheckDecryption(encrypted, !kIncludeVersion));
   1489 
   1490   ASSERT_EQ(0u, visitor_.stream_frames_.size());
   1491   EXPECT_EQ(0u, visitor_.ack_frames_.size());
   1492 }
   1493 
   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',  '!',
   1509   };
   1510 
   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;
   1519 
   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))));
   1524 
   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);
   1540 
   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]);
   1548 }
   1549 
   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,
   1564 
   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',  '!',
   1578   };
   1579 
   1580   QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
   1581   EXPECT_TRUE(framer_.ProcessPacket(encrypted));
   1582 
   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_);
   1594 
   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]);
   1602 }
   1603 
   1604 TEST_P(QuicFramerTest, AckFrame) {
   1605   unsigned char packet[] = {
   1606     // public flags (8 byte guid)
   1607     0x3C,
   1608     // guid
   1609     0x10, 0x32, 0x54, 0x76,
   1610     0x98, 0xBA, 0xDC, 0xFE,
   1611     // packet sequence number
   1612     0xA8, 0x9A, 0x78, 0x56,
   1613     0x34, 0x12,
   1614     // private flags (entropy)
   1615     0x01,
   1616 
   1617     // frame type (ack frame)
   1618     // (has nacks, not truncated, 6 byte largest observed, 1 byte delta)
   1619     0x6C,
   1620     // entropy hash of sent packets till least awaiting - 1.
   1621     0xAB,
   1622     // least packet sequence number awaiting an ack, delta from sequence number.
   1623     0x08, 0x00, 0x00, 0x00,
   1624     0x00, 0x00,
   1625     // entropy hash of all received packets.
   1626     0xBA,
   1627     // largest observed packet sequence number
   1628     0xBF, 0x9A, 0x78, 0x56,
   1629     0x34, 0x12,
   1630     // Zero delta time.
   1631     0x0, 0x0,
   1632     // num missing packets
   1633     0x01,
   1634     // missing packet delta
   1635     0x01,
   1636     // 0 more missing packets in range.
   1637     0x00,
   1638   };
   1639 
   1640   QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
   1641   EXPECT_TRUE(framer_.ProcessPacket(encrypted));
   1642 
   1643   EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
   1644   ASSERT_TRUE(visitor_.header_.get());
   1645   EXPECT_TRUE(CheckDecryption(encrypted, !kIncludeVersion));
   1646 
   1647   EXPECT_EQ(0u, visitor_.stream_frames_.size());
   1648   ASSERT_EQ(1u, visitor_.ack_frames_.size());
   1649   const QuicAckFrame& frame = *visitor_.ack_frames_[0];
   1650   EXPECT_EQ(0xAB, frame.sent_info.entropy_hash);
   1651   EXPECT_EQ(0xBA, frame.received_info.entropy_hash);
   1652   EXPECT_EQ(GG_UINT64_C(0x0123456789ABF), frame.received_info.largest_observed);
   1653   ASSERT_EQ(1u, frame.received_info.missing_packets.size());
   1654   SequenceNumberSet::const_iterator missing_iter =
   1655       frame.received_info.missing_packets.begin();
   1656   EXPECT_EQ(GG_UINT64_C(0x0123456789ABE), *missing_iter);
   1657   EXPECT_EQ(GG_UINT64_C(0x0123456789AA0), frame.sent_info.least_unacked);
   1658 
   1659   const size_t kSentEntropyOffset = kQuicFrameTypeSize;
   1660   const size_t kLeastUnackedOffset = kSentEntropyOffset + kQuicEntropyHashSize;
   1661   const size_t kReceivedEntropyOffset = kLeastUnackedOffset +
   1662       PACKET_6BYTE_SEQUENCE_NUMBER;
   1663   const size_t kLargestObservedOffset = kReceivedEntropyOffset +
   1664       kQuicEntropyHashSize;
   1665   const size_t kMissingDeltaTimeOffset = kLargestObservedOffset +
   1666       PACKET_6BYTE_SEQUENCE_NUMBER;
   1667   const size_t kNumMissingPacketOffset = kMissingDeltaTimeOffset +
   1668       kQuicDeltaTimeLargestObservedSize;
   1669   const size_t kMissingPacketsOffset = kNumMissingPacketOffset +
   1670       kNumberOfMissingPacketsSize;
   1671   const size_t kMissingPacketsRange = kMissingPacketsOffset +
   1672       PACKET_1BYTE_SEQUENCE_NUMBER;
   1673   // Now test framing boundaries
   1674   const size_t ack_frame_size = kMissingPacketsRange +
   1675       PACKET_1BYTE_SEQUENCE_NUMBER;
   1676   for (size_t i = kQuicFrameTypeSize; i < ack_frame_size; ++i) {
   1677     string expected_error;
   1678     if (i < kLeastUnackedOffset) {
   1679       expected_error = "Unable to read entropy hash for sent packets.";
   1680     } else if (i < kReceivedEntropyOffset) {
   1681       expected_error = "Unable to read least unacked delta.";
   1682     } else if (i < kLargestObservedOffset) {
   1683       expected_error = "Unable to read entropy hash for received packets.";
   1684     } else if (i < kMissingDeltaTimeOffset) {
   1685       expected_error = "Unable to read largest observed.";
   1686     } else if (i < kNumMissingPacketOffset) {
   1687       expected_error = "Unable to read delta time largest observed.";
   1688     } else if (i < kMissingPacketsOffset) {
   1689       expected_error = "Unable to read num missing packet ranges.";
   1690     } else if (i < kMissingPacketsRange) {
   1691       expected_error = "Unable to read missing sequence number delta.";
   1692     } else {
   1693       expected_error = "Unable to read missing sequence number range.";
   1694     }
   1695     CheckProcessingFails(
   1696         packet,
   1697         i + GetPacketHeaderSize(PACKET_8BYTE_GUID, !kIncludeVersion,
   1698                                 PACKET_6BYTE_SEQUENCE_NUMBER, NOT_IN_FEC_GROUP),
   1699         expected_error, QUIC_INVALID_ACK_DATA);
   1700   }
   1701 }
   1702 
   1703 TEST_P(QuicFramerTest, AckFrameNoNacks) {
   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,
   1715 
   1716     // frame type (ack frame)
   1717     // (no nacks, not truncated, 6 byte largest observed, 1 byte delta)
   1718     0x4C,
   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   };
   1732 
   1733   QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
   1734   EXPECT_TRUE(framer_.ProcessPacket(encrypted));
   1735 
   1736   EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
   1737   ASSERT_TRUE(visitor_.header_.get());
   1738   EXPECT_TRUE(CheckDecryption(encrypted, !kIncludeVersion));
   1739 
   1740   EXPECT_EQ(0u, visitor_.stream_frames_.size());
   1741   ASSERT_EQ(1u, visitor_.ack_frames_.size());
   1742   QuicAckFrame* frame = visitor_.ack_frames_[0];
   1743   EXPECT_EQ(0xAB, frame->sent_info.entropy_hash);
   1744   EXPECT_EQ(0xBA, frame->received_info.entropy_hash);
   1745   EXPECT_EQ(GG_UINT64_C(0x0123456789ABF),
   1746             frame->received_info.largest_observed);
   1747   ASSERT_EQ(0u, frame->received_info.missing_packets.size());
   1748   EXPECT_EQ(GG_UINT64_C(0x0123456789AA0), frame->sent_info.least_unacked);
   1749 
   1750   // Verify that the packet re-serializes identically.
   1751   QuicFrames frames;
   1752   frames.push_back(QuicFrame(frame));
   1753   scoped_ptr<QuicPacket> data(
   1754       framer_.BuildUnsizedDataPacket(*visitor_.header_, frames).packet);
   1755   ASSERT_TRUE(data != NULL);
   1756 
   1757   test::CompareCharArraysWithHexError("constructed packet",
   1758                                       data->data(), data->length(),
   1759                                       AsChars(packet), arraysize(packet));
   1760 }
   1761 
   1762 TEST_P(QuicFramerTest, AckFrame500Nacks) {
   1763   unsigned char packet[] = {
   1764     // public flags (8 byte guid)
   1765     0x3C,
   1766     // guid
   1767     0x10, 0x32, 0x54, 0x76,
   1768     0x98, 0xBA, 0xDC, 0xFE,
   1769     // packet sequence number
   1770     0xA8, 0x9A, 0x78, 0x56,
   1771     0x34, 0x12,
   1772     // private flags (entropy)
   1773     0x01,
   1774 
   1775     // frame type (ack frame)
   1776     // (has nacks, not truncated, 6 byte largest observed, 1 byte delta)
   1777     0x6C,
   1778     // entropy hash of sent packets till least awaiting - 1.
   1779     0xAB,
   1780     // least packet sequence number awaiting an ack, delta from sequence number.
   1781     0x08, 0x00, 0x00, 0x00,
   1782     0x00, 0x00,
   1783     // entropy hash of all received packets.
   1784     0xBA,
   1785     // largest observed packet sequence number
   1786     0xBF, 0x9A, 0x78, 0x56,
   1787     0x34, 0x12,
   1788     // Zero delta time.
   1789     0x0, 0x0,
   1790     // num missing packet ranges
   1791     0x02,
   1792     // missing packet delta
   1793     0x01,
   1794     // 243 more missing packets in range.
   1795     // The ranges are listed in this order so the re-constructed packet matches.
   1796     0xF3,
   1797     // No gap between ranges
   1798     0x00,
   1799     // 255 more missing packets in range.
   1800     0xFF,
   1801   };
   1802 
   1803   QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
   1804   EXPECT_TRUE(framer_.ProcessPacket(encrypted));
   1805 
   1806   EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
   1807   ASSERT_TRUE(visitor_.header_.get());
   1808   EXPECT_TRUE(CheckDecryption(encrypted, !kIncludeVersion));
   1809 
   1810   EXPECT_EQ(0u, visitor_.stream_frames_.size());
   1811   ASSERT_EQ(1u, visitor_.ack_frames_.size());
   1812   QuicAckFrame* frame = visitor_.ack_frames_[0];
   1813   EXPECT_EQ(0xAB, frame->sent_info.entropy_hash);
   1814   EXPECT_EQ(0xBA, frame->received_info.entropy_hash);
   1815   EXPECT_EQ(GG_UINT64_C(0x0123456789ABF),
   1816             frame->received_info.largest_observed);
   1817   ASSERT_EQ(500u, frame->received_info.missing_packets.size());
   1818   SequenceNumberSet::const_iterator first_missing_iter =
   1819       frame->received_info.missing_packets.begin();
   1820   EXPECT_EQ(GG_UINT64_C(0x0123456789ABE) - 499, *first_missing_iter);
   1821   SequenceNumberSet::const_reverse_iterator last_missing_iter =
   1822       frame->received_info.missing_packets.rbegin();
   1823   EXPECT_EQ(GG_UINT64_C(0x0123456789ABE), *last_missing_iter);
   1824   EXPECT_EQ(GG_UINT64_C(0x0123456789AA0), frame->sent_info.least_unacked);
   1825 
   1826   // Verify that the packet re-serializes identically.
   1827   QuicFrames frames;
   1828   frames.push_back(QuicFrame(frame));
   1829   scoped_ptr<QuicPacket> data(
   1830       framer_.BuildUnsizedDataPacket(*visitor_.header_, frames).packet);
   1831   ASSERT_TRUE(data != NULL);
   1832 
   1833   test::CompareCharArraysWithHexError("constructed packet",
   1834                                       data->data(), data->length(),
   1835                                       AsChars(packet), arraysize(packet));
   1836 }
   1837 
   1838 TEST_P(QuicFramerTest, CongestionFeedbackFrameTCP) {
   1839   unsigned char packet[] = {
   1840     // public flags (8 byte guid)
   1841     0x3C,
   1842     // guid
   1843     0x10, 0x32, 0x54, 0x76,
   1844     0x98, 0xBA, 0xDC, 0xFE,
   1845     // packet sequence number
   1846     0xBC, 0x9A, 0x78, 0x56,
   1847     0x34, 0x12,
   1848     // private flags
   1849     0x00,
   1850 
   1851     // frame type (congestion feedback frame)
   1852     0x20,
   1853     // congestion feedback type (tcp)
   1854     0x00,
   1855     // ack_frame.feedback.tcp.accumulated_number_of_lost_packets
   1856     0x01, 0x02,
   1857     // ack_frame.feedback.tcp.receive_window
   1858     0x03, 0x04,
   1859   };
   1860 
   1861   QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
   1862   EXPECT_TRUE(framer_.ProcessPacket(encrypted));
   1863 
   1864   EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
   1865   ASSERT_TRUE(visitor_.header_.get());
   1866   EXPECT_TRUE(CheckDecryption(encrypted, !kIncludeVersion));
   1867 
   1868   EXPECT_EQ(0u, visitor_.stream_frames_.size());
   1869   ASSERT_EQ(1u, visitor_.congestion_feedback_frames_.size());
   1870   const QuicCongestionFeedbackFrame& frame =
   1871       *visitor_.congestion_feedback_frames_[0];
   1872   ASSERT_EQ(kTCP, frame.type);
   1873   EXPECT_EQ(0x0201,
   1874             frame.tcp.accumulated_number_of_lost_packets);
   1875   EXPECT_EQ(0x4030u, frame.tcp.receive_window);
   1876 
   1877   // Now test framing boundaries
   1878   for (size_t i = kQuicFrameTypeSize; i < 6; ++i) {
   1879     string expected_error;
   1880     if (i < 2) {
   1881       expected_error = "Unable to read congestion feedback type.";
   1882     } else if (i < 4) {
   1883       expected_error = "Unable to read accumulated number of lost packets.";
   1884     } else if (i < 6) {
   1885       expected_error = "Unable to read receive window.";
   1886     }
   1887     CheckProcessingFails(
   1888         packet,
   1889         i + GetPacketHeaderSize(PACKET_8BYTE_GUID, !kIncludeVersion,
   1890                                 PACKET_6BYTE_SEQUENCE_NUMBER, NOT_IN_FEC_GROUP),
   1891         expected_error, QUIC_INVALID_CONGESTION_FEEDBACK_DATA);
   1892   }
   1893 }
   1894 
   1895 TEST_P(QuicFramerTest, CongestionFeedbackFrameInterArrival) {
   1896   unsigned char packet[] = {
   1897     // public flags (8 byte guid)
   1898     0x3C,
   1899     // guid
   1900     0x10, 0x32, 0x54, 0x76,
   1901     0x98, 0xBA, 0xDC, 0xFE,
   1902     // packet sequence number
   1903     0xBC, 0x9A, 0x78, 0x56,
   1904     0x34, 0x12,
   1905     // private flags
   1906     0x00,
   1907 
   1908     // frame type (congestion feedback frame)
   1909     0x20,
   1910     // congestion feedback type (inter arrival)
   1911     0x01,
   1912     // accumulated_number_of_lost_packets
   1913     0x02, 0x03,
   1914     // num received packets
   1915     0x03,
   1916     // lowest sequence number
   1917     0xBA, 0x9A, 0x78, 0x56,
   1918     0x34, 0x12,
   1919     // receive time
   1920     0x87, 0x96, 0xA5, 0xB4,
   1921     0xC3, 0xD2, 0xE1, 0x07,
   1922     // sequence delta
   1923     0x01, 0x00,
   1924     // time delta
   1925     0x01, 0x00, 0x00, 0x00,
   1926     // sequence delta (skip one packet)
   1927     0x03, 0x00,
   1928     // time delta
   1929     0x02, 0x00, 0x00, 0x00,
   1930   };
   1931 
   1932   QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
   1933   EXPECT_TRUE(framer_.ProcessPacket(encrypted));
   1934 
   1935   EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
   1936   ASSERT_TRUE(visitor_.header_.get());
   1937   EXPECT_TRUE(CheckDecryption(encrypted, !kIncludeVersion));
   1938 
   1939   EXPECT_EQ(0u, visitor_.stream_frames_.size());
   1940   ASSERT_EQ(1u, visitor_.congestion_feedback_frames_.size());
   1941   const QuicCongestionFeedbackFrame& frame =
   1942       *visitor_.congestion_feedback_frames_[0];
   1943   ASSERT_EQ(kInterArrival, frame.type);
   1944   EXPECT_EQ(0x0302, frame.inter_arrival.
   1945             accumulated_number_of_lost_packets);
   1946   ASSERT_EQ(3u, frame.inter_arrival.received_packet_times.size());
   1947   TimeMap::const_iterator iter =
   1948       frame.inter_arrival.received_packet_times.begin();
   1949   EXPECT_EQ(GG_UINT64_C(0x0123456789ABA), iter->first);
   1950   EXPECT_EQ(GG_INT64_C(0x07E1D2C3B4A59687),
   1951             iter->second.Subtract(start_).ToMicroseconds());
   1952   ++iter;
   1953   EXPECT_EQ(GG_UINT64_C(0x0123456789ABB), iter->first);
   1954   EXPECT_EQ(GG_INT64_C(0x07E1D2C3B4A59688),
   1955             iter->second.Subtract(start_).ToMicroseconds());
   1956   ++iter;
   1957   EXPECT_EQ(GG_UINT64_C(0x0123456789ABD), iter->first);
   1958   EXPECT_EQ(GG_INT64_C(0x07E1D2C3B4A59689),
   1959             iter->second.Subtract(start_).ToMicroseconds());
   1960 
   1961   // Now test framing boundaries
   1962   for (size_t i = kQuicFrameTypeSize; i < 31; ++i) {
   1963     string expected_error;
   1964     if (i < 2) {
   1965       expected_error = "Unable to read congestion feedback type.";
   1966     } else if (i < 4) {
   1967       expected_error = "Unable to read accumulated number of lost packets.";
   1968     } else if (i < 5) {
   1969       expected_error = "Unable to read num received packets.";
   1970     } else if (i < 11) {
   1971       expected_error = "Unable to read smallest received.";
   1972     } else if (i < 19) {
   1973       expected_error = "Unable to read time received.";
   1974     } else if (i < 21) {
   1975       expected_error = "Unable to read sequence delta in received packets.";
   1976     } else if (i < 25) {
   1977       expected_error = "Unable to read time delta in received packets.";
   1978     } else if (i < 27) {
   1979       expected_error = "Unable to read sequence delta in received packets.";
   1980     } else if (i < 31) {
   1981       expected_error = "Unable to read time delta in received packets.";
   1982     }
   1983     CheckProcessingFails(
   1984         packet,
   1985         i + GetPacketHeaderSize(PACKET_8BYTE_GUID, !kIncludeVersion,
   1986                                 PACKET_6BYTE_SEQUENCE_NUMBER, NOT_IN_FEC_GROUP),
   1987         expected_error, QUIC_INVALID_CONGESTION_FEEDBACK_DATA);
   1988   }
   1989 }
   1990 
   1991 TEST_P(QuicFramerTest, CongestionFeedbackFrameFixRate) {
   1992   unsigned char packet[] = {
   1993     // public flags (8 byte guid)
   1994     0x3C,
   1995     // guid
   1996     0x10, 0x32, 0x54, 0x76,
   1997     0x98, 0xBA, 0xDC, 0xFE,
   1998     // packet sequence number
   1999     0xBC, 0x9A, 0x78, 0x56,
   2000     0x34, 0x12,
   2001     // private flags
   2002     0x00,
   2003 
   2004     // frame type (congestion feedback frame)
   2005     0x20,
   2006     // congestion feedback type (fix rate)
   2007     0x02,
   2008     // bitrate_in_bytes_per_second;
   2009     0x01, 0x02, 0x03, 0x04,
   2010   };
   2011 
   2012   QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
   2013   EXPECT_TRUE(framer_.ProcessPacket(encrypted));
   2014 
   2015   EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
   2016   ASSERT_TRUE(visitor_.header_.get());
   2017   EXPECT_TRUE(CheckDecryption(encrypted, !kIncludeVersion));
   2018 
   2019   EXPECT_EQ(0u, visitor_.stream_frames_.size());
   2020   ASSERT_EQ(1u, visitor_.congestion_feedback_frames_.size());
   2021   const QuicCongestionFeedbackFrame& frame =
   2022       *visitor_.congestion_feedback_frames_[0];
   2023   ASSERT_EQ(kFixRate, frame.type);
   2024   EXPECT_EQ(static_cast<uint32>(0x04030201),
   2025             frame.fix_rate.bitrate.ToBytesPerSecond());
   2026 
   2027   // Now test framing boundaries
   2028   for (size_t i = kQuicFrameTypeSize; i < 6; ++i) {
   2029     string expected_error;
   2030     if (i < 2) {
   2031       expected_error = "Unable to read congestion feedback type.";
   2032     } else if (i < 6) {
   2033       expected_error = "Unable to read bitrate.";
   2034     }
   2035     CheckProcessingFails(
   2036         packet,
   2037         i + GetPacketHeaderSize(PACKET_8BYTE_GUID, !kIncludeVersion,
   2038                                 PACKET_6BYTE_SEQUENCE_NUMBER, NOT_IN_FEC_GROUP),
   2039         expected_error, QUIC_INVALID_CONGESTION_FEEDBACK_DATA);
   2040   }
   2041 }
   2042 
   2043 TEST_P(QuicFramerTest, CongestionFeedbackFrameInvalidFeedback) {
   2044   unsigned char packet[] = {
   2045     // public flags (8 byte guid)
   2046     0x3C,
   2047     // guid
   2048     0x10, 0x32, 0x54, 0x76,
   2049     0x98, 0xBA, 0xDC, 0xFE,
   2050     // packet sequence number
   2051     0xBC, 0x9A, 0x78, 0x56,
   2052     0x34, 0x12,
   2053     // private flags
   2054     0x00,
   2055 
   2056     // frame type (congestion feedback frame)
   2057     0x20,
   2058     // congestion feedback type (invalid)
   2059     0x03,
   2060   };
   2061 
   2062   QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
   2063   EXPECT_FALSE(framer_.ProcessPacket(encrypted));
   2064   EXPECT_TRUE(CheckDecryption(encrypted, !kIncludeVersion));
   2065   EXPECT_EQ(QUIC_INVALID_CONGESTION_FEEDBACK_DATA, framer_.error());
   2066 }
   2067 
   2068 TEST_P(QuicFramerTest, RstStreamFrame) {
   2069   unsigned char packet[] = {
   2070     // public flags (8 byte guid)
   2071     0x3C,
   2072     // guid
   2073     0x10, 0x32, 0x54, 0x76,
   2074     0x98, 0xBA, 0xDC, 0xFE,
   2075     // packet sequence number
   2076     0xBC, 0x9A, 0x78, 0x56,
   2077     0x34, 0x12,
   2078     // private flags
   2079     0x00,
   2080 
   2081     // frame type (rst stream frame)
   2082     0x01,
   2083     // stream id
   2084     0x04, 0x03, 0x02, 0x01,
   2085     // error code
   2086     0x01, 0x00, 0x00, 0x00,
   2087 
   2088     // error details length
   2089     0x0d, 0x00,
   2090     // error details
   2091     'b',  'e',  'c',  'a',
   2092     'u',  's',  'e',  ' ',
   2093     'I',  ' ',  'c',  'a',
   2094     'n',
   2095   };
   2096 
   2097   QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
   2098   EXPECT_TRUE(framer_.ProcessPacket(encrypted));
   2099 
   2100   EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
   2101   ASSERT_TRUE(visitor_.header_.get());
   2102   EXPECT_TRUE(CheckDecryption(encrypted, !kIncludeVersion));
   2103 
   2104   EXPECT_EQ(GG_UINT64_C(0x01020304), visitor_.rst_stream_frame_.stream_id);
   2105   EXPECT_EQ(0x01, visitor_.rst_stream_frame_.error_code);
   2106   EXPECT_EQ("because I can", visitor_.rst_stream_frame_.error_details);
   2107 
   2108   // Now test framing boundaries
   2109   for (size_t i = kQuicFrameTypeSize; i < 24; ++i) {
   2110     string expected_error;
   2111     if (i < kQuicFrameTypeSize + kQuicMaxStreamIdSize) {
   2112       expected_error = "Unable to read stream_id.";
   2113     } else if (i < kQuicFrameTypeSize + kQuicMaxStreamIdSize +
   2114                kQuicErrorCodeSize) {
   2115       expected_error = "Unable to read rst stream error code.";
   2116     } else {
   2117       expected_error = "Unable to read rst stream error details.";
   2118     }
   2119     CheckProcessingFails(
   2120         packet,
   2121         i + GetPacketHeaderSize(PACKET_8BYTE_GUID, !kIncludeVersion,
   2122                                 PACKET_6BYTE_SEQUENCE_NUMBER, NOT_IN_FEC_GROUP),
   2123         expected_error, QUIC_INVALID_RST_STREAM_DATA);
   2124   }
   2125 }
   2126 
   2127 TEST_P(QuicFramerTest, ConnectionCloseFrame) {
   2128   unsigned char packet[] = {
   2129     // public flags (8 byte guid)
   2130     0x3C,
   2131     // guid
   2132     0x10, 0x32, 0x54, 0x76,
   2133     0x98, 0xBA, 0xDC, 0xFE,
   2134     // packet sequence number
   2135     0xBC, 0x9A, 0x78, 0x56,
   2136     0x34, 0x12,
   2137     // private flags
   2138     0x00,
   2139 
   2140     // frame type (connection close frame)
   2141     0x02,
   2142     // error code
   2143     0x11, 0x00, 0x00, 0x00,
   2144 
   2145     // error details length
   2146     0x0d, 0x00,
   2147     // error details
   2148     'b',  'e',  'c',  'a',
   2149     'u',  's',  'e',  ' ',
   2150     'I',  ' ',  'c',  'a',
   2151     'n',
   2152   };
   2153 
   2154   QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
   2155   EXPECT_TRUE(framer_.ProcessPacket(encrypted));
   2156 
   2157   EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
   2158   ASSERT_TRUE(visitor_.header_.get());
   2159   EXPECT_TRUE(CheckDecryption(encrypted, !kIncludeVersion));
   2160 
   2161   EXPECT_EQ(0u, visitor_.stream_frames_.size());
   2162 
   2163   EXPECT_EQ(0x11, visitor_.connection_close_frame_.error_code);
   2164   EXPECT_EQ("because I can", visitor_.connection_close_frame_.error_details);
   2165 
   2166   ASSERT_EQ(0u, visitor_.ack_frames_.size());
   2167 
   2168   // Now test framing boundaries
   2169   for (size_t i = kQuicFrameTypeSize;
   2170        i < QuicFramer::GetMinConnectionCloseFrameSize(); ++i) {
   2171     string expected_error;
   2172     if (i < kQuicFrameTypeSize + kQuicErrorCodeSize) {
   2173       expected_error = "Unable to read connection close error code.";
   2174     } else {
   2175       expected_error = "Unable to read connection close error details.";
   2176     }
   2177     CheckProcessingFails(
   2178         packet,
   2179         i + GetPacketHeaderSize(PACKET_8BYTE_GUID, !kIncludeVersion,
   2180                                 PACKET_6BYTE_SEQUENCE_NUMBER, NOT_IN_FEC_GROUP),
   2181         expected_error, QUIC_INVALID_CONNECTION_CLOSE_DATA);
   2182   }
   2183 }
   2184 
   2185 TEST_P(QuicFramerTest, GoAwayFrame) {
   2186   unsigned char packet[] = {
   2187     // public flags (8 byte guid)
   2188     0x3C,
   2189     // guid
   2190     0x10, 0x32, 0x54, 0x76,
   2191     0x98, 0xBA, 0xDC, 0xFE,
   2192     // packet sequence number
   2193     0xBC, 0x9A, 0x78, 0x56,
   2194     0x34, 0x12,
   2195     // private flags
   2196     0x00,
   2197 
   2198     // frame type (go away frame)
   2199     0x03,
   2200     // error code
   2201     0x09, 0x00, 0x00, 0x00,
   2202     // stream id
   2203     0x04, 0x03, 0x02, 0x01,
   2204     // error details length
   2205     0x0d, 0x00,
   2206     // error details
   2207     'b',  'e',  'c',  'a',
   2208     'u',  's',  'e',  ' ',
   2209     'I',  ' ',  'c',  'a',
   2210     'n',
   2211   };
   2212 
   2213   QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
   2214   EXPECT_TRUE(framer_.ProcessPacket(encrypted));
   2215 
   2216   EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
   2217   ASSERT_TRUE(visitor_.header_.get());
   2218   EXPECT_TRUE(CheckDecryption(encrypted, !kIncludeVersion));
   2219 
   2220   EXPECT_EQ(GG_UINT64_C(0x01020304),
   2221             visitor_.goaway_frame_.last_good_stream_id);
   2222   EXPECT_EQ(0x9, visitor_.goaway_frame_.error_code);
   2223   EXPECT_EQ("because I can", visitor_.goaway_frame_.reason_phrase);
   2224 
   2225   const size_t reason_size = arraysize("because I can") - 1;
   2226   // Now test framing boundaries
   2227   for (size_t i = kQuicFrameTypeSize;
   2228        i < QuicFramer::GetMinGoAwayFrameSize() + reason_size; ++i) {
   2229     string expected_error;
   2230     if (i < kQuicFrameTypeSize + kQuicErrorCodeSize) {
   2231       expected_error = "Unable to read go away error code.";
   2232     } else if (i < kQuicFrameTypeSize + kQuicErrorCodeSize +
   2233                kQuicMaxStreamIdSize) {
   2234       expected_error = "Unable to read last good stream id.";
   2235     } else {
   2236       expected_error = "Unable to read goaway reason.";
   2237     }
   2238     CheckProcessingFails(
   2239         packet,
   2240         i + GetPacketHeaderSize(PACKET_8BYTE_GUID, !kIncludeVersion,
   2241                                 PACKET_6BYTE_SEQUENCE_NUMBER, NOT_IN_FEC_GROUP),
   2242         expected_error, QUIC_INVALID_GOAWAY_DATA);
   2243   }
   2244 }
   2245 
   2246 TEST_P(QuicFramerTest, PublicResetPacket) {
   2247   unsigned char packet[] = {
   2248     // public flags (public reset, 8 byte guid)
   2249     0x3E,
   2250     // guid
   2251     0x10, 0x32, 0x54, 0x76,
   2252     0x98, 0xBA, 0xDC, 0xFE,
   2253     // nonce proof
   2254     0x89, 0x67, 0x45, 0x23,
   2255     0x01, 0xEF, 0xCD, 0xAB,
   2256     // rejected sequence number
   2257     0xBC, 0x9A, 0x78, 0x56,
   2258     0x34, 0x12,
   2259   };
   2260 
   2261   QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
   2262   EXPECT_TRUE(framer_.ProcessPacket(encrypted));
   2263   ASSERT_EQ(QUIC_NO_ERROR, framer_.error());
   2264   ASSERT_TRUE(visitor_.public_reset_packet_.get());
   2265   EXPECT_EQ(GG_UINT64_C(0xFEDCBA9876543210),
   2266             visitor_.public_reset_packet_->public_header.guid);
   2267   EXPECT_TRUE(visitor_.public_reset_packet_->public_header.reset_flag);
   2268   EXPECT_FALSE(visitor_.public_reset_packet_->public_header.version_flag);
   2269   EXPECT_EQ(GG_UINT64_C(0xABCDEF0123456789),
   2270             visitor_.public_reset_packet_->nonce_proof);
   2271   EXPECT_EQ(GG_UINT64_C(0x123456789ABC),
   2272             visitor_.public_reset_packet_->rejected_sequence_number);
   2273 
   2274   // Now test framing boundaries
   2275   for (size_t i = 0; i < GetPublicResetPacketSize(); ++i) {
   2276     string expected_error;
   2277     DLOG(INFO) << "iteration: " << i;
   2278     if (i < kGuidOffset) {
   2279       expected_error = "Unable to read public flags.";
   2280       CheckProcessingFails(packet, i, expected_error,
   2281                            QUIC_INVALID_PACKET_HEADER);
   2282     } else if (i < kPublicResetPacketNonceProofOffset) {
   2283       expected_error = "Unable to read GUID.";
   2284       CheckProcessingFails(packet, i, expected_error,
   2285                            QUIC_INVALID_PACKET_HEADER);
   2286     } else if (i < kPublicResetPacketRejectedSequenceNumberOffset) {
   2287       expected_error = "Unable to read nonce proof.";
   2288       CheckProcessingFails(packet, i, expected_error,
   2289                            QUIC_INVALID_PUBLIC_RST_PACKET);
   2290     } else {
   2291       expected_error = "Unable to read rejected sequence number.";
   2292       CheckProcessingFails(packet, i, expected_error,
   2293                            QUIC_INVALID_PUBLIC_RST_PACKET);
   2294     }
   2295   }
   2296 }
   2297 
   2298 TEST_P(QuicFramerTest, VersionNegotiationPacket) {
   2299   unsigned char packet[] = {
   2300     // public flags (version, 8 byte guid)
   2301     0x3D,
   2302     // guid
   2303     0x10, 0x32, 0x54, 0x76,
   2304     0x98, 0xBA, 0xDC, 0xFE,
   2305     // version tag
   2306     'Q', '0', GetQuicVersionDigitTens(), GetQuicVersionDigitOnes(),
   2307     'Q', '2', '.', '0',
   2308   };
   2309 
   2310   QuicFramerPeer::SetIsServer(&framer_, false);
   2311 
   2312   QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
   2313   EXPECT_TRUE(framer_.ProcessPacket(encrypted));
   2314   ASSERT_EQ(QUIC_NO_ERROR, framer_.error());
   2315   ASSERT_TRUE(visitor_.version_negotiation_packet_.get());
   2316   EXPECT_EQ(2u, visitor_.version_negotiation_packet_->versions.size());
   2317   EXPECT_EQ(GetParam(), visitor_.version_negotiation_packet_->versions[0]);
   2318 
   2319   for (size_t i = 0; i <= kPublicFlagsSize + PACKET_8BYTE_GUID; ++i) {
   2320     string expected_error;
   2321     QuicErrorCode error_code = QUIC_INVALID_PACKET_HEADER;
   2322     if (i < kGuidOffset) {
   2323       expected_error = "Unable to read public flags.";
   2324     } else if (i < kVersionOffset) {
   2325       expected_error = "Unable to read GUID.";
   2326     } else {
   2327       expected_error = "Unable to read supported version in negotiation.";
   2328       error_code = QUIC_INVALID_VERSION_NEGOTIATION_PACKET;
   2329     }
   2330     CheckProcessingFails(packet, i, expected_error, error_code);
   2331   }
   2332 }
   2333 
   2334 TEST_P(QuicFramerTest, FecPacket) {
   2335   unsigned char packet[] = {
   2336     // public flags (8 byte guid)
   2337     0x3C,
   2338     // guid
   2339     0x10, 0x32, 0x54, 0x76,
   2340     0x98, 0xBA, 0xDC, 0xFE,
   2341     // packet sequence number
   2342     0xBC, 0x9A, 0x78, 0x56,
   2343     0x34, 0x12,
   2344     // private flags (fec group & FEC)
   2345     0x06,
   2346     // first fec protected packet offset
   2347     0x01,
   2348 
   2349     // redundancy
   2350     'a',  'b',  'c',  'd',
   2351     'e',  'f',  'g',  'h',
   2352     'i',  'j',  'k',  'l',
   2353     'm',  'n',  'o',  'p',
   2354   };
   2355 
   2356   QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
   2357   EXPECT_TRUE(framer_.ProcessPacket(encrypted));
   2358 
   2359   EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
   2360   ASSERT_TRUE(visitor_.header_.get());
   2361   EXPECT_TRUE(CheckDecryption(encrypted, !kIncludeVersion));
   2362 
   2363   EXPECT_EQ(0u, visitor_.stream_frames_.size());
   2364   EXPECT_EQ(0u, visitor_.ack_frames_.size());
   2365   ASSERT_EQ(1, visitor_.fec_count_);
   2366   const QuicFecData& fec_data = *visitor_.fec_data_[0];
   2367   EXPECT_EQ(GG_UINT64_C(0x0123456789ABB), fec_data.fec_group);
   2368   EXPECT_EQ("abcdefghijklmnop", fec_data.redundancy);
   2369 }
   2370 
   2371 TEST_P(QuicFramerTest, BuildPaddingFramePacket) {
   2372   QuicPacketHeader header;
   2373   header.public_header.guid = GG_UINT64_C(0xFEDCBA9876543210);
   2374   header.public_header.reset_flag = false;
   2375   header.public_header.version_flag = false;
   2376   header.fec_flag = false;
   2377   header.entropy_flag = false;
   2378   header.packet_sequence_number = GG_UINT64_C(0x123456789ABC);
   2379   header.fec_group = 0;
   2380 
   2381   QuicPaddingFrame padding_frame;
   2382 
   2383   QuicFrames frames;
   2384   frames.push_back(QuicFrame(&padding_frame));
   2385 
   2386   unsigned char packet[kMaxPacketSize] = {
   2387     // public flags (8 byte guid)
   2388     0x3C,
   2389     // guid
   2390     0x10, 0x32, 0x54, 0x76,
   2391     0x98, 0xBA, 0xDC, 0xFE,
   2392     // packet sequence number
   2393     0xBC, 0x9A, 0x78, 0x56,
   2394     0x34, 0x12,
   2395     // private flags
   2396     0x00,
   2397 
   2398     // frame type (padding frame)
   2399     0x00,
   2400     0x00, 0x00, 0x00, 0x00
   2401   };
   2402 
   2403   uint64 header_size =
   2404       GetPacketHeaderSize(PACKET_8BYTE_GUID, !kIncludeVersion,
   2405                           PACKET_6BYTE_SEQUENCE_NUMBER, NOT_IN_FEC_GROUP);
   2406   memset(packet + header_size + 1, 0x00, kMaxPacketSize - header_size - 1);
   2407 
   2408   scoped_ptr<QuicPacket> data(
   2409       framer_.BuildUnsizedDataPacket(header, frames).packet);
   2410   ASSERT_TRUE(data != NULL);
   2411 
   2412   test::CompareCharArraysWithHexError("constructed packet",
   2413                                       data->data(), data->length(),
   2414                                       AsChars(packet),
   2415                                       arraysize(packet));
   2416 }
   2417 
   2418 TEST_P(QuicFramerTest, Build4ByteSequenceNumberPaddingFramePacket) {
   2419   QuicPacketHeader header;
   2420   header.public_header.guid = GG_UINT64_C(0xFEDCBA9876543210);
   2421   header.public_header.reset_flag = false;
   2422   header.public_header.version_flag = false;
   2423   header.fec_flag = false;
   2424   header.entropy_flag = false;
   2425   header.public_header.sequence_number_length = PACKET_4BYTE_SEQUENCE_NUMBER;
   2426   header.packet_sequence_number = GG_UINT64_C(0x123456789ABC);
   2427   header.fec_group = 0;
   2428 
   2429   QuicPaddingFrame padding_frame;
   2430 
   2431   QuicFrames frames;
   2432   frames.push_back(QuicFrame(&padding_frame));
   2433 
   2434   unsigned char packet[kMaxPacketSize] = {
   2435     // public flags (8 byte guid and 4 byte sequence number)
   2436     0x2C,
   2437     // guid
   2438     0x10, 0x32, 0x54, 0x76,
   2439     0x98, 0xBA, 0xDC, 0xFE,
   2440     // packet sequence number
   2441     0xBC, 0x9A, 0x78, 0x56,
   2442     // private flags
   2443     0x00,
   2444 
   2445     // frame type (padding frame)
   2446     0x00,
   2447     0x00, 0x00, 0x00, 0x00
   2448   };
   2449 
   2450   uint64 header_size =
   2451       GetPacketHeaderSize(PACKET_8BYTE_GUID, !kIncludeVersion,
   2452                           PACKET_4BYTE_SEQUENCE_NUMBER, NOT_IN_FEC_GROUP);
   2453   memset(packet + header_size + 1, 0x00, kMaxPacketSize - header_size - 1);
   2454 
   2455   scoped_ptr<QuicPacket> data(
   2456       framer_.BuildUnsizedDataPacket(header, frames).packet);
   2457   ASSERT_TRUE(data != NULL);
   2458 
   2459   test::CompareCharArraysWithHexError("constructed packet",
   2460                                       data->data(), data->length(),
   2461                                       AsChars(packet),
   2462                                       arraysize(packet));
   2463 }
   2464 
   2465 TEST_P(QuicFramerTest, Build2ByteSequenceNumberPaddingFramePacket) {
   2466   QuicPacketHeader header;
   2467   header.public_header.guid = GG_UINT64_C(0xFEDCBA9876543210);
   2468   header.public_header.reset_flag = false;
   2469   header.public_header.version_flag = false;
   2470   header.fec_flag = false;
   2471   header.entropy_flag = false;
   2472   header.public_header.sequence_number_length = PACKET_2BYTE_SEQUENCE_NUMBER;
   2473   header.packet_sequence_number = GG_UINT64_C(0x123456789ABC);
   2474   header.fec_group = 0;
   2475 
   2476   QuicPaddingFrame padding_frame;
   2477 
   2478   QuicFrames frames;
   2479   frames.push_back(QuicFrame(&padding_frame));
   2480 
   2481   unsigned char packet[kMaxPacketSize] = {
   2482     // public flags (8 byte guid and 2 byte sequence number)
   2483     0x1C,
   2484     // guid
   2485     0x10, 0x32, 0x54, 0x76,
   2486     0x98, 0xBA, 0xDC, 0xFE,
   2487     // packet sequence number
   2488     0xBC, 0x9A,
   2489     // private flags
   2490     0x00,
   2491 
   2492     // frame type (padding frame)
   2493     0x00,
   2494     0x00, 0x00, 0x00, 0x00
   2495   };
   2496 
   2497   uint64 header_size =
   2498       GetPacketHeaderSize(PACKET_8BYTE_GUID, !kIncludeVersion,
   2499                           PACKET_2BYTE_SEQUENCE_NUMBER, NOT_IN_FEC_GROUP);
   2500   memset(packet + header_size + 1, 0x00, kMaxPacketSize - header_size - 1);
   2501 
   2502   scoped_ptr<QuicPacket> data(
   2503       framer_.BuildUnsizedDataPacket(header, frames).packet);
   2504   ASSERT_TRUE(data != NULL);
   2505 
   2506   test::CompareCharArraysWithHexError("constructed packet",
   2507                                       data->data(), data->length(),
   2508                                       AsChars(packet),
   2509                                       arraysize(packet));
   2510 }
   2511 
   2512 TEST_P(QuicFramerTest, Build1ByteSequenceNumberPaddingFramePacket) {
   2513   QuicPacketHeader header;
   2514   header.public_header.guid = GG_UINT64_C(0xFEDCBA9876543210);
   2515   header.public_header.reset_flag = false;
   2516   header.public_header.version_flag = false;
   2517   header.fec_flag = false;
   2518   header.entropy_flag = false;
   2519   header.public_header.sequence_number_length = PACKET_1BYTE_SEQUENCE_NUMBER;
   2520   header.packet_sequence_number = GG_UINT64_C(0x123456789ABC);
   2521   header.fec_group = 0;
   2522 
   2523   QuicPaddingFrame padding_frame;
   2524 
   2525   QuicFrames frames;
   2526   frames.push_back(QuicFrame(&padding_frame));
   2527 
   2528   unsigned char packet[kMaxPacketSize] = {
   2529     // public flags (8 byte guid and 1 byte sequence number)
   2530     0x0C,
   2531     // guid
   2532     0x10, 0x32, 0x54, 0x76,
   2533     0x98, 0xBA, 0xDC, 0xFE,
   2534     // packet sequence number
   2535     0xBC,
   2536     // private flags
   2537     0x00,
   2538 
   2539     // frame type (padding frame)
   2540     0x00,
   2541     0x00, 0x00, 0x00, 0x00
   2542   };
   2543 
   2544   uint64 header_size =
   2545       GetPacketHeaderSize(PACKET_8BYTE_GUID, !kIncludeVersion,
   2546                           PACKET_1BYTE_SEQUENCE_NUMBER, NOT_IN_FEC_GROUP);
   2547   memset(packet + header_size + 1, 0x00, kMaxPacketSize - header_size - 1);
   2548 
   2549   scoped_ptr<QuicPacket> data(
   2550       framer_.BuildUnsizedDataPacket(header, frames).packet);
   2551   ASSERT_TRUE(data != NULL);
   2552 
   2553   test::CompareCharArraysWithHexError("constructed packet",
   2554                                       data->data(), data->length(),
   2555                                       AsChars(packet),
   2556                                       arraysize(packet));
   2557 }
   2558 
   2559 TEST_P(QuicFramerTest, BuildStreamFramePacket) {
   2560   QuicPacketHeader header;
   2561   header.public_header.guid = GG_UINT64_C(0xFEDCBA9876543210);
   2562   header.public_header.reset_flag = false;
   2563   header.public_header.version_flag = false;
   2564   header.fec_flag = false;
   2565   header.entropy_flag = true;
   2566   header.packet_sequence_number = GG_UINT64_C(0x77123456789ABC);
   2567   header.fec_group = 0;
   2568 
   2569   QuicStreamFrame stream_frame;
   2570   stream_frame.stream_id = 0x01020304;
   2571   stream_frame.fin = true;
   2572   stream_frame.offset = GG_UINT64_C(0xBA98FEDC32107654);
   2573   stream_frame.data = MakeIOVector("hello world!");
   2574 
   2575   QuicFrames frames;
   2576   frames.push_back(QuicFrame(&stream_frame));
   2577 
   2578   unsigned char packet[] = {
   2579     // public flags (8 byte guid)
   2580     0x3C,
   2581     // guid
   2582     0x10, 0x32, 0x54, 0x76,
   2583     0x98, 0xBA, 0xDC, 0xFE,
   2584     // packet sequence number
   2585     0xBC, 0x9A, 0x78, 0x56,
   2586     0x34, 0x12,
   2587     // private flags (entropy)
   2588     0x01,
   2589 
   2590     // frame type (stream frame with fin and no length)
   2591     0xDF,
   2592     // stream id
   2593     0x04, 0x03, 0x02, 0x01,
   2594     // offset
   2595     0x54, 0x76, 0x10, 0x32,
   2596     0xDC, 0xFE, 0x98, 0xBA,
   2597     // data
   2598     'h',  'e',  'l',  'l',
   2599     'o',  ' ',  'w',  'o',
   2600     'r',  'l',  'd',  '!',
   2601   };
   2602 
   2603   scoped_ptr<QuicPacket> data(
   2604       framer_.BuildUnsizedDataPacket(header, frames).packet);
   2605   ASSERT_TRUE(data != NULL);
   2606 
   2607   test::CompareCharArraysWithHexError("constructed packet",
   2608                                       data->data(), data->length(),
   2609                                       AsChars(packet), arraysize(packet));
   2610 }
   2611 
   2612 TEST_P(QuicFramerTest, BuildStreamFramePacketWithVersionFlag) {
   2613   QuicPacketHeader header;
   2614   header.public_header.guid = GG_UINT64_C(0xFEDCBA9876543210);
   2615   header.public_header.reset_flag = false;
   2616   header.public_header.version_flag = true;
   2617   header.fec_flag = false;
   2618   header.entropy_flag = true;
   2619   header.packet_sequence_number = GG_UINT64_C(0x77123456789ABC);
   2620   header.fec_group = 0;
   2621 
   2622   QuicStreamFrame stream_frame;
   2623   stream_frame.stream_id = 0x01020304;
   2624   stream_frame.fin = true;
   2625   stream_frame.offset = GG_UINT64_C(0xBA98FEDC32107654);
   2626   stream_frame.data = MakeIOVector("hello world!");
   2627 
   2628   QuicFrames frames;
   2629   frames.push_back(QuicFrame(&stream_frame));
   2630 
   2631   unsigned char packet[] = {
   2632     // public flags (version, 8 byte guid)
   2633     0x3D,
   2634     // guid
   2635     0x10, 0x32, 0x54, 0x76,
   2636     0x98, 0xBA, 0xDC, 0xFE,
   2637     // version tag
   2638     'Q', '0', GetQuicVersionDigitTens(), GetQuicVersionDigitOnes(),
   2639     // packet sequence number
   2640     0xBC, 0x9A, 0x78, 0x56,
   2641     0x34, 0x12,
   2642     // private flags (entropy)
   2643     0x01,
   2644 
   2645     // frame type (stream frame with fin and no length)
   2646     0xDF,
   2647     // stream id
   2648     0x04, 0x03, 0x02, 0x01,
   2649     // offset
   2650     0x54, 0x76, 0x10, 0x32,
   2651     0xDC, 0xFE, 0x98, 0xBA,
   2652     // data
   2653     'h',  'e',  'l',  'l',
   2654     'o',  ' ',  'w',  'o',
   2655     'r',  'l',  'd',  '!',
   2656   };
   2657 
   2658   QuicFramerPeer::SetIsServer(&framer_, false);
   2659   scoped_ptr<QuicPacket> data(
   2660       framer_.BuildUnsizedDataPacket(header, frames).packet);
   2661   ASSERT_TRUE(data != NULL);
   2662 
   2663   test::CompareCharArraysWithHexError("constructed packet",
   2664                                       data->data(), data->length(),
   2665                                       AsChars(packet), arraysize(packet));
   2666 }
   2667 
   2668 TEST_P(QuicFramerTest, BuildVersionNegotiationPacket) {
   2669   QuicPacketPublicHeader header;
   2670   header.guid = GG_UINT64_C(0xFEDCBA9876543210);
   2671   header.reset_flag = false;
   2672   header.version_flag = true;
   2673 
   2674   unsigned char packet[] = {
   2675     // public flags (version, 8 byte guid)
   2676     0x3D,
   2677     // guid
   2678     0x10, 0x32, 0x54, 0x76,
   2679     0x98, 0xBA, 0xDC, 0xFE,
   2680     // version tag
   2681     'Q', '0', GetQuicVersionDigitTens(), GetQuicVersionDigitOnes(),
   2682   };
   2683 
   2684   QuicVersionVector versions;
   2685   versions.push_back(GetParam());
   2686   scoped_ptr<QuicEncryptedPacket> data(
   2687       framer_.BuildVersionNegotiationPacket(header, versions));
   2688 
   2689   test::CompareCharArraysWithHexError("constructed packet",
   2690                                       data->data(), data->length(),
   2691                                       AsChars(packet), arraysize(packet));
   2692 }
   2693 
   2694 TEST_P(QuicFramerTest, BuildAckFramePacket) {
   2695   QuicPacketHeader header;
   2696   header.public_header.guid = GG_UINT64_C(0xFEDCBA9876543210);
   2697   header.public_header.reset_flag = false;
   2698   header.public_header.version_flag = false;
   2699   header.fec_flag = false;
   2700   header.entropy_flag = true;
   2701   header.packet_sequence_number = GG_UINT64_C(0x770123456789AA8);
   2702   header.fec_group = 0;
   2703 
   2704   QuicAckFrame ack_frame;
   2705   ack_frame.received_info.entropy_hash = 0x43;
   2706   ack_frame.received_info.largest_observed = GG_UINT64_C(0x770123456789ABF);
   2707   ack_frame.received_info.delta_time_largest_observed = QuicTime::Delta::Zero();
   2708   ack_frame.received_info.missing_packets.insert(
   2709       GG_UINT64_C(0x770123456789ABE));
   2710   ack_frame.sent_info.entropy_hash = 0x14;
   2711   ack_frame.sent_info.least_unacked = GG_UINT64_C(0x770123456789AA0);
   2712 
   2713   QuicFrames frames;
   2714   frames.push_back(QuicFrame(&ack_frame));
   2715 
   2716   unsigned char packet[] = {
   2717     // public flags (8 byte guid)
   2718     0x3C,
   2719     // guid
   2720     0x10, 0x32, 0x54, 0x76,
   2721     0x98, 0xBA, 0xDC, 0xFE,
   2722     // packet sequence number
   2723     0xA8, 0x9A, 0x78, 0x56,
   2724     0x34, 0x12,
   2725     // private flags (entropy)
   2726     0x01,
   2727 
   2728     // frame type (ack frame)
   2729     // (has nacks, not truncated, 6 byte largest observed, 1 byte delta)
   2730     0x6C,
   2731     // entropy hash of sent packets till least awaiting - 1.
   2732     0x14,
   2733     // least packet sequence number awaiting an ack, delta from sequence number.
   2734     0x08, 0x00, 0x00, 0x00,
   2735     0x00, 0x00,
   2736     // entropy hash of all received packets.
   2737     0x43,
   2738     // largest observed packet sequence number
   2739     0xBF, 0x9A, 0x78, 0x56,
   2740     0x34, 0x12,
   2741     // Zero delta time.
   2742     0x0, 0x0,
   2743     // num missing packet ranges
   2744     0x01,
   2745     // missing packet delta
   2746     0x01,
   2747     // 0 more missing packets in range.
   2748     0x00,
   2749   };
   2750 
   2751   scoped_ptr<QuicPacket> data(
   2752       framer_.BuildUnsizedDataPacket(header, frames).packet);
   2753   ASSERT_TRUE(data != NULL);
   2754 
   2755   test::CompareCharArraysWithHexError("constructed packet",
   2756                                       data->data(), data->length(),
   2757                                       AsChars(packet), arraysize(packet));
   2758 }
   2759 
   2760 TEST_P(QuicFramerTest, BuildCongestionFeedbackFramePacketTCP) {
   2761   QuicPacketHeader header;
   2762   header.public_header.guid = GG_UINT64_C(0xFEDCBA9876543210);
   2763   header.public_header.reset_flag = false;
   2764   header.public_header.version_flag = false;
   2765   header.fec_flag = false;
   2766   header.entropy_flag = false;
   2767   header.packet_sequence_number = GG_UINT64_C(0x123456789ABC);
   2768   header.fec_group = 0;
   2769 
   2770   QuicCongestionFeedbackFrame congestion_feedback_frame;
   2771   congestion_feedback_frame.type = kTCP;
   2772   congestion_feedback_frame.tcp.accumulated_number_of_lost_packets = 0x0201;
   2773   congestion_feedback_frame.tcp.receive_window = 0x4030;
   2774 
   2775   QuicFrames frames;
   2776   frames.push_back(QuicFrame(&congestion_feedback_frame));
   2777 
   2778   unsigned char packet[] = {
   2779     // public flags (8 byte guid)
   2780     0x3C,
   2781     // guid
   2782     0x10, 0x32, 0x54, 0x76,
   2783     0x98, 0xBA, 0xDC, 0xFE,
   2784     // packet sequence number
   2785     0xBC, 0x9A, 0x78, 0x56,
   2786     0x34, 0x12,
   2787     // private flags
   2788     0x00,
   2789 
   2790     // frame type (congestion feedback frame)
   2791     0x20,
   2792     // congestion feedback type (TCP)
   2793     0x00,
   2794     // accumulated number of lost packets
   2795     0x01, 0x02,
   2796     // TCP receive window
   2797     0x03, 0x04,
   2798   };
   2799 
   2800   scoped_ptr<QuicPacket> data(
   2801       framer_.BuildUnsizedDataPacket(header, frames).packet);
   2802   ASSERT_TRUE(data != NULL);
   2803 
   2804   test::CompareCharArraysWithHexError("constructed packet",
   2805                                       data->data(), data->length(),
   2806                                       AsChars(packet), arraysize(packet));
   2807 }
   2808 
   2809 TEST_P(QuicFramerTest, BuildCongestionFeedbackFramePacketInterArrival) {
   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 = false;
   2814   header.fec_flag = false;
   2815   header.entropy_flag = false;
   2816   header.packet_sequence_number = GG_UINT64_C(0x123456789ABC);
   2817   header.fec_group = 0;
   2818 
   2819   QuicCongestionFeedbackFrame frame;
   2820   frame.type = kInterArrival;
   2821   frame.inter_arrival.accumulated_number_of_lost_packets = 0x0302;
   2822   frame.inter_arrival.received_packet_times.insert(
   2823       make_pair(GG_UINT64_C(0x0123456789ABA),
   2824                 start_.Add(QuicTime::Delta::FromMicroseconds(
   2825                     GG_UINT64_C(0x07E1D2C3B4A59687)))));
   2826   frame.inter_arrival.received_packet_times.insert(
   2827       make_pair(GG_UINT64_C(0x0123456789ABB),
   2828                 start_.Add(QuicTime::Delta::FromMicroseconds(
   2829                     GG_UINT64_C(0x07E1D2C3B4A59688)))));
   2830   frame.inter_arrival.received_packet_times.insert(
   2831       make_pair(GG_UINT64_C(0x0123456789ABD),
   2832                 start_.Add(QuicTime::Delta::FromMicroseconds(
   2833                     GG_UINT64_C(0x07E1D2C3B4A59689)))));
   2834   QuicFrames frames;
   2835   frames.push_back(QuicFrame(&frame));
   2836 
   2837   unsigned char packet[] = {
   2838     // public flags (8 byte guid)
   2839     0x3C,
   2840     // guid
   2841     0x10, 0x32, 0x54, 0x76,
   2842     0x98, 0xBA, 0xDC, 0xFE,
   2843     // packet sequence number
   2844     0xBC, 0x9A, 0x78, 0x56,
   2845     0x34, 0x12,
   2846     // private flags
   2847     0x00,
   2848 
   2849     // frame type (congestion feedback frame)
   2850     0x20,
   2851     // congestion feedback type (inter arrival)
   2852     0x01,
   2853     // accumulated_number_of_lost_packets
   2854     0x02, 0x03,
   2855     // num received packets
   2856     0x03,
   2857     // lowest sequence number
   2858     0xBA, 0x9A, 0x78, 0x56,
   2859     0x34, 0x12,
   2860     // receive time
   2861     0x87, 0x96, 0xA5, 0xB4,
   2862     0xC3, 0xD2, 0xE1, 0x07,
   2863     // sequence delta
   2864     0x01, 0x00,
   2865     // time delta
   2866     0x01, 0x00, 0x00, 0x00,
   2867     // sequence delta (skip one packet)
   2868     0x03, 0x00,
   2869     // time delta
   2870     0x02, 0x00, 0x00, 0x00,
   2871   };
   2872 
   2873   scoped_ptr<QuicPacket> data(
   2874       framer_.BuildUnsizedDataPacket(header, frames).packet);
   2875   ASSERT_TRUE(data != NULL);
   2876 
   2877   test::CompareCharArraysWithHexError("constructed packet",
   2878                                       data->data(), data->length(),
   2879                                       AsChars(packet), arraysize(packet));
   2880 }
   2881 
   2882 TEST_P(QuicFramerTest, BuildCongestionFeedbackFramePacketFixRate) {
   2883   QuicPacketHeader header;
   2884   header.public_header.guid = GG_UINT64_C(0xFEDCBA9876543210);
   2885   header.public_header.reset_flag = false;
   2886   header.public_header.version_flag = false;
   2887   header.fec_flag = false;
   2888   header.entropy_flag = false;
   2889   header.packet_sequence_number = GG_UINT64_C(0x123456789ABC);
   2890   header.fec_group = 0;
   2891 
   2892   QuicCongestionFeedbackFrame congestion_feedback_frame;
   2893   congestion_feedback_frame.type = kFixRate;
   2894   congestion_feedback_frame.fix_rate.bitrate
   2895       = QuicBandwidth::FromBytesPerSecond(0x04030201);
   2896 
   2897   QuicFrames frames;
   2898   frames.push_back(QuicFrame(&congestion_feedback_frame));
   2899 
   2900   unsigned char packet[] = {
   2901     // public flags (8 byte guid)
   2902     0x3C,
   2903     // guid
   2904     0x10, 0x32, 0x54, 0x76,
   2905     0x98, 0xBA, 0xDC, 0xFE,
   2906     // packet sequence number
   2907     0xBC, 0x9A, 0x78, 0x56,
   2908     0x34, 0x12,
   2909     // private flags
   2910     0x00,
   2911 
   2912     // frame type (congestion feedback frame)
   2913     0x20,
   2914     // congestion feedback type (fix rate)
   2915     0x02,
   2916     // bitrate_in_bytes_per_second;
   2917     0x01, 0x02, 0x03, 0x04,
   2918   };
   2919 
   2920   scoped_ptr<QuicPacket> data(
   2921       framer_.BuildUnsizedDataPacket(header, frames).packet);
   2922   ASSERT_TRUE(data != NULL);
   2923 
   2924   test::CompareCharArraysWithHexError("constructed packet",
   2925                                       data->data(), data->length(),
   2926                                       AsChars(packet), arraysize(packet));
   2927 }
   2928 
   2929 TEST_P(QuicFramerTest, BuildCongestionFeedbackFramePacketInvalidFeedback) {
   2930   QuicPacketHeader header;
   2931   header.public_header.guid = GG_UINT64_C(0xFEDCBA9876543210);
   2932   header.public_header.reset_flag = false;
   2933   header.public_header.version_flag = false;
   2934   header.fec_flag = false;
   2935   header.entropy_flag = false;
   2936   header.packet_sequence_number = GG_UINT64_C(0x123456789ABC);
   2937   header.fec_group = 0;
   2938 
   2939   QuicCongestionFeedbackFrame congestion_feedback_frame;
   2940   congestion_feedback_frame.type =
   2941       static_cast<CongestionFeedbackType>(kFixRate + 1);
   2942 
   2943   QuicFrames frames;
   2944   frames.push_back(QuicFrame(&congestion_feedback_frame));
   2945 
   2946   scoped_ptr<QuicPacket> data(
   2947       framer_.BuildUnsizedDataPacket(header, frames).packet);
   2948   ASSERT_TRUE(data == NULL);
   2949 }
   2950 
   2951 TEST_P(QuicFramerTest, BuildRstFramePacket) {
   2952   QuicPacketHeader header;
   2953   header.public_header.guid = GG_UINT64_C(0xFEDCBA9876543210);
   2954   header.public_header.reset_flag = false;
   2955   header.public_header.version_flag = false;
   2956   header.fec_flag = false;
   2957   header.entropy_flag = false;
   2958   header.packet_sequence_number = GG_UINT64_C(0x123456789ABC);
   2959   header.fec_group = 0;
   2960 
   2961   QuicRstStreamFrame rst_frame;
   2962   rst_frame.stream_id = 0x01020304;
   2963   rst_frame.error_code = static_cast<QuicRstStreamErrorCode>(0x05060708);
   2964   rst_frame.error_details = "because I can";
   2965 
   2966   unsigned char packet[] = {
   2967     // public flags (8 byte guid)
   2968     0x3C,
   2969     // guid
   2970     0x10, 0x32, 0x54, 0x76,
   2971     0x98, 0xBA, 0xDC, 0xFE,
   2972     // packet sequence number
   2973     0xBC, 0x9A, 0x78, 0x56,
   2974     0x34, 0x12,
   2975     // private flags
   2976     0x00,
   2977 
   2978     // frame type (rst stream frame)
   2979     0x01,
   2980     // stream id
   2981     0x04, 0x03, 0x02, 0x01,
   2982     // error code
   2983     0x08, 0x07, 0x06, 0x05,
   2984     // error details length
   2985     0x0d, 0x00,
   2986     // error details
   2987     'b',  'e',  'c',  'a',
   2988     'u',  's',  'e',  ' ',
   2989     'I',  ' ',  'c',  'a',
   2990     'n',
   2991   };
   2992 
   2993   QuicFrames frames;
   2994   frames.push_back(QuicFrame(&rst_frame));
   2995 
   2996   scoped_ptr<QuicPacket> data(
   2997       framer_.BuildUnsizedDataPacket(header, frames).packet);
   2998   ASSERT_TRUE(data != NULL);
   2999 
   3000   test::CompareCharArraysWithHexError("constructed packet",
   3001                                       data->data(), data->length(),
   3002                                       AsChars(packet), arraysize(packet));
   3003 }
   3004 
   3005 TEST_P(QuicFramerTest, BuildCloseFramePacket) {
   3006   QuicPacketHeader header;
   3007   header.public_header.guid = GG_UINT64_C(0xFEDCBA9876543210);
   3008   header.public_header.reset_flag = false;
   3009   header.public_header.version_flag = false;
   3010   header.fec_flag = false;
   3011   header.entropy_flag = true;
   3012   header.packet_sequence_number = GG_UINT64_C(0x123456789ABC);
   3013   header.fec_group = 0;
   3014 
   3015   QuicConnectionCloseFrame close_frame;
   3016   close_frame.error_code = static_cast<QuicErrorCode>(0x05060708);
   3017   close_frame.error_details = "because I can";
   3018 
   3019   QuicFrames frames;
   3020   frames.push_back(QuicFrame(&close_frame));
   3021 
   3022   unsigned char packet[] = {
   3023     // public flags (8 byte guid)
   3024     0x3C,
   3025     // guid
   3026     0x10, 0x32, 0x54, 0x76,
   3027     0x98, 0xBA, 0xDC, 0xFE,
   3028     // packet sequence number
   3029     0xBC, 0x9A, 0x78, 0x56,
   3030     0x34, 0x12,
   3031     // private flags (entropy)
   3032     0x01,
   3033 
   3034     // frame type (connection close frame)
   3035     0x02,
   3036     // error code
   3037     0x08, 0x07, 0x06, 0x05,
   3038     // error details length
   3039     0x0d, 0x00,
   3040     // error details
   3041     'b',  'e',  'c',  'a',
   3042     'u',  's',  'e',  ' ',
   3043     'I',  ' ',  'c',  'a',
   3044     'n',
   3045   };
   3046 
   3047   scoped_ptr<QuicPacket> data(
   3048       framer_.BuildUnsizedDataPacket(header, frames).packet);
   3049   ASSERT_TRUE(data != NULL);
   3050 
   3051   test::CompareCharArraysWithHexError("constructed packet",
   3052                                       data->data(), data->length(),
   3053                                       AsChars(packet), arraysize(packet));
   3054 }
   3055 
   3056 TEST_P(QuicFramerTest, BuildGoAwayPacket) {
   3057   QuicPacketHeader header;
   3058   header.public_header.guid = GG_UINT64_C(0xFEDCBA9876543210);
   3059   header.public_header.reset_flag = false;
   3060   header.public_header.version_flag = false;
   3061   header.fec_flag = false;
   3062   header.entropy_flag = true;
   3063   header.packet_sequence_number = GG_UINT64_C(0x123456789ABC);
   3064   header.fec_group = 0;
   3065 
   3066   QuicGoAwayFrame goaway_frame;
   3067   goaway_frame.error_code = static_cast<QuicErrorCode>(0x05060708);
   3068   goaway_frame.last_good_stream_id = 0x01020304;
   3069   goaway_frame.reason_phrase = "because I can";
   3070 
   3071   QuicFrames frames;
   3072   frames.push_back(QuicFrame(&goaway_frame));
   3073 
   3074   unsigned char packet[] = {
   3075     // public flags (8 byte guid)
   3076     0x3C,
   3077     // guid
   3078     0x10, 0x32, 0x54, 0x76,
   3079     0x98, 0xBA, 0xDC, 0xFE,
   3080     // packet sequence number
   3081     0xBC, 0x9A, 0x78, 0x56,
   3082     0x34, 0x12,
   3083     // private flags(entropy)
   3084     0x01,
   3085 
   3086     // frame type (go away frame)
   3087     0x03,
   3088     // error code
   3089     0x08, 0x07, 0x06, 0x05,
   3090     // stream id
   3091     0x04, 0x03, 0x02, 0x01,
   3092     // error details length
   3093     0x0d, 0x00,
   3094     // error details
   3095     'b',  'e',  'c',  'a',
   3096     'u',  's',  'e',  ' ',
   3097     'I',  ' ',  'c',  'a',
   3098     'n',
   3099   };
   3100 
   3101   scoped_ptr<QuicPacket> data(
   3102       framer_.BuildUnsizedDataPacket(header, frames).packet);
   3103   ASSERT_TRUE(data != NULL);
   3104 
   3105   test::CompareCharArraysWithHexError("constructed packet",
   3106                                       data->data(), data->length(),
   3107                                       AsChars(packet), arraysize(packet));
   3108 }
   3109 
   3110 TEST_P(QuicFramerTest, BuildPublicResetPacket) {
   3111   QuicPublicResetPacket reset_packet;
   3112   reset_packet.public_header.guid = GG_UINT64_C(0xFEDCBA9876543210);
   3113   reset_packet.public_header.reset_flag = true;
   3114   reset_packet.public_header.version_flag = false;
   3115   reset_packet.rejected_sequence_number = GG_UINT64_C(0x123456789ABC);
   3116   reset_packet.nonce_proof = GG_UINT64_C(0xABCDEF0123456789);
   3117 
   3118   unsigned char packet[] = {
   3119     // public flags (public reset, 8 byte GUID)
   3120     0x3E,
   3121     // guid
   3122     0x10, 0x32, 0x54, 0x76,
   3123     0x98, 0xBA, 0xDC, 0xFE,
   3124     // nonce proof
   3125     0x89, 0x67, 0x45, 0x23,
   3126     0x01, 0xEF, 0xCD, 0xAB,
   3127     // rejected sequence number
   3128     0xBC, 0x9A, 0x78, 0x56,
   3129     0x34, 0x12,
   3130   };
   3131 
   3132   scoped_ptr<QuicEncryptedPacket> data(
   3133       framer_.BuildPublicResetPacket(reset_packet));
   3134   ASSERT_TRUE(data != NULL);
   3135 
   3136   test::CompareCharArraysWithHexError("constructed packet",
   3137                                       data->data(), data->length(),
   3138                                       AsChars(packet), arraysize(packet));
   3139 }
   3140 
   3141 TEST_P(QuicFramerTest, BuildFecPacket) {
   3142   QuicPacketHeader header;
   3143   header.public_header.guid = GG_UINT64_C(0xFEDCBA9876543210);
   3144   header.public_header.reset_flag = false;
   3145   header.public_header.version_flag = false;
   3146   header.fec_flag = true;
   3147   header.entropy_flag = true;
   3148   header.packet_sequence_number = (GG_UINT64_C(0x123456789ABC));
   3149   header.is_in_fec_group = IN_FEC_GROUP;
   3150   header.fec_group = GG_UINT64_C(0x123456789ABB);;
   3151 
   3152   QuicFecData fec_data;
   3153   fec_data.fec_group = 1;
   3154   fec_data.redundancy = "abcdefghijklmnop";
   3155 
   3156   unsigned char packet[] = {
   3157     // public flags (8 byte guid)
   3158     0x3C,
   3159     // guid
   3160     0x10, 0x32, 0x54, 0x76,
   3161     0x98, 0xBA, 0xDC, 0xFE,
   3162     // packet sequence number
   3163     0xBC, 0x9A, 0x78, 0x56,
   3164     0x34, 0x12,
   3165     // private flags (entropy & fec group & fec packet)
   3166     0x07,
   3167     // first fec protected packet offset
   3168     0x01,
   3169 
   3170     // redundancy
   3171     'a',  'b',  'c',  'd',
   3172     'e',  'f',  'g',  'h',
   3173     'i',  'j',  'k',  'l',
   3174     'm',  'n',  'o',  'p',
   3175   };
   3176 
   3177   scoped_ptr<QuicPacket> data(
   3178       framer_.BuildFecPacket(header, fec_data).packet);
   3179   ASSERT_TRUE(data != NULL);
   3180 
   3181   test::CompareCharArraysWithHexError("constructed packet",
   3182                                       data->data(), data->length(),
   3183                                       AsChars(packet), arraysize(packet));
   3184 }
   3185 
   3186 TEST_P(QuicFramerTest, EncryptPacket) {
   3187   QuicPacketSequenceNumber sequence_number = GG_UINT64_C(0x123456789ABC);
   3188   unsigned char packet[] = {
   3189     // public flags (8 byte guid)
   3190     0x3C,
   3191     // guid
   3192     0x10, 0x32, 0x54, 0x76,
   3193     0x98, 0xBA, 0xDC, 0xFE,
   3194     // packet sequence number
   3195     0xBC, 0x9A, 0x78, 0x56,
   3196     0x34, 0x12,
   3197     // private flags (fec group & fec packet)
   3198     0x06,
   3199     // first fec protected packet offset
   3200     0x01,
   3201 
   3202     // redundancy
   3203     'a',  'b',  'c',  'd',
   3204     'e',  'f',  'g',  'h',
   3205     'i',  'j',  'k',  'l',
   3206     'm',  'n',  'o',  'p',
   3207   };
   3208 
   3209   scoped_ptr<QuicPacket> raw(
   3210       QuicPacket::NewDataPacket(AsChars(packet), arraysize(packet), false,
   3211                                 PACKET_8BYTE_GUID, !kIncludeVersion,
   3212                                 PACKET_6BYTE_SEQUENCE_NUMBER));
   3213   scoped_ptr<QuicEncryptedPacket> encrypted(
   3214       framer_.EncryptPacket(ENCRYPTION_NONE, sequence_number, *raw));
   3215 
   3216   ASSERT_TRUE(encrypted.get() != NULL);
   3217   EXPECT_TRUE(CheckEncryption(sequence_number, raw.get()));
   3218 }
   3219 
   3220 TEST_P(QuicFramerTest, EncryptPacketWithVersionFlag) {
   3221   QuicPacketSequenceNumber sequence_number = GG_UINT64_C(0x123456789ABC);
   3222   unsigned char packet[] = {
   3223     // public flags (version, 8 byte guid)
   3224     0x3D,
   3225     // guid
   3226     0x10, 0x32, 0x54, 0x76,
   3227     0x98, 0xBA, 0xDC, 0xFE,
   3228     // version tag
   3229     'Q', '.', '1', '0',
   3230     // packet sequence number
   3231     0xBC, 0x9A, 0x78, 0x56,
   3232     0x34, 0x12,
   3233     // private flags (fec group & fec flags)
   3234     0x06,
   3235     // first fec protected packet offset
   3236     0x01,
   3237 
   3238     // redundancy
   3239     'a',  'b',  'c',  'd',
   3240     'e',  'f',  'g',  'h',
   3241     'i',  'j',  'k',  'l',
   3242     'm',  'n',  'o',  'p',
   3243   };
   3244 
   3245   scoped_ptr<QuicPacket> raw(
   3246       QuicPacket::NewDataPacket(AsChars(packet), arraysize(packet), false,
   3247                                 PACKET_8BYTE_GUID, kIncludeVersion,
   3248                                 PACKET_6BYTE_SEQUENCE_NUMBER));
   3249   scoped_ptr<QuicEncryptedPacket> encrypted(
   3250       framer_.EncryptPacket(ENCRYPTION_NONE, sequence_number, *raw));
   3251 
   3252   ASSERT_TRUE(encrypted.get() != NULL);
   3253   EXPECT_TRUE(CheckEncryption(sequence_number, raw.get()));
   3254 }
   3255 
   3256 // TODO(rch): re-enable after https://codereview.chromium.org/11820005/
   3257 // lands.  Currently this is causing valgrind problems, but it should be
   3258 // fixed in the followup CL.
   3259 TEST_P(QuicFramerTest, DISABLED_CalculateLargestReceived) {
   3260   SequenceNumberSet missing;
   3261   missing.insert(1);
   3262   missing.insert(5);
   3263   missing.insert(7);
   3264 
   3265   // These two we just walk to the next gap, and return the largest seen.
   3266   EXPECT_EQ(4u, QuicFramer::CalculateLargestObserved(missing, missing.find(1)));
   3267   EXPECT_EQ(6u, QuicFramer::CalculateLargestObserved(missing, missing.find(5)));
   3268 
   3269   missing.insert(2);
   3270   // For 1, we can't go forward as 2 would be implicitly acked so we return the
   3271   // largest missing packet.
   3272   EXPECT_EQ(1u, QuicFramer::CalculateLargestObserved(missing, missing.find(1)));
   3273   // For 2, we've seen 3 and 4, so can admit to a largest observed.
   3274   EXPECT_EQ(4u, QuicFramer::CalculateLargestObserved(missing, missing.find(2)));
   3275 }
   3276 
   3277 // TODO(rch) enable after landing the revised truncation CL.
   3278 TEST_P(QuicFramerTest, DISABLED_Truncation) {
   3279   QuicPacketHeader header;
   3280   header.public_header.guid = GG_UINT64_C(0xFEDCBA9876543210);
   3281   header.public_header.reset_flag = false;
   3282   header.public_header.version_flag = false;
   3283   header.fec_flag = false;
   3284   header.entropy_flag = false;
   3285   header.packet_sequence_number = GG_UINT64_C(0x123456789ABC);
   3286   header.fec_group = 0;
   3287 
   3288   QuicAckFrame ack_frame;
   3289   ack_frame.received_info.largest_observed = 601;
   3290   ack_frame.sent_info.least_unacked = 0;
   3291   for (uint64 i = 1; i < ack_frame.received_info.largest_observed; i += 2) {
   3292     ack_frame.received_info.missing_packets.insert(i);
   3293   }
   3294 
   3295   // Create a packet with just the ack
   3296   QuicFrame frame;
   3297   frame.type = ACK_FRAME;
   3298   frame.ack_frame = &ack_frame;
   3299   QuicFrames frames;
   3300   frames.push_back(frame);
   3301 
   3302   scoped_ptr<QuicPacket> raw_ack_packet(
   3303       framer_.BuildUnsizedDataPacket(header, frames).packet);
   3304   ASSERT_TRUE(raw_ack_packet != NULL);
   3305 
   3306   scoped_ptr<QuicEncryptedPacket> ack_packet(
   3307       framer_.EncryptPacket(ENCRYPTION_NONE, header.packet_sequence_number,
   3308                             *raw_ack_packet));
   3309 
   3310   // Now make sure we can turn our ack packet back into an ack frame
   3311   ASSERT_TRUE(framer_.ProcessPacket(*ack_packet));
   3312   ASSERT_EQ(1u, visitor_.ack_frames_.size());
   3313   const QuicAckFrame& processed_ack_frame = *visitor_.ack_frames_[0];
   3314   EXPECT_EQ(0u, processed_ack_frame.sent_info.least_unacked);
   3315   EXPECT_TRUE(processed_ack_frame.received_info.is_truncated);
   3316   EXPECT_EQ(510u, processed_ack_frame.received_info.largest_observed);
   3317   ASSERT_EQ(255u, processed_ack_frame.received_info.missing_packets.size());
   3318   SequenceNumberSet::const_iterator missing_iter =
   3319       processed_ack_frame.received_info.missing_packets.begin();
   3320   EXPECT_EQ(1u, *missing_iter);
   3321   SequenceNumberSet::const_reverse_iterator last_missing_iter =
   3322       processed_ack_frame.received_info.missing_packets.rbegin();
   3323   EXPECT_EQ(509u, *last_missing_iter);
   3324 }
   3325 
   3326 TEST_P(QuicFramerTest, CleanTruncation) {
   3327   QuicPacketHeader header;
   3328   header.public_header.guid = GG_UINT64_C(0xFEDCBA9876543210);
   3329   header.public_header.reset_flag = false;
   3330   header.public_header.version_flag = false;
   3331   header.fec_flag = false;
   3332   header.entropy_flag = true;
   3333   header.packet_sequence_number = GG_UINT64_C(0x123456789ABC);
   3334   header.fec_group = 0;
   3335 
   3336   QuicAckFrame ack_frame;
   3337   ack_frame.received_info.largest_observed = 201;
   3338   ack_frame.sent_info.least_unacked = 0;
   3339   for (uint64 i = 1; i < ack_frame.received_info.largest_observed; ++i) {
   3340     ack_frame.received_info.missing_packets.insert(i);
   3341   }
   3342 
   3343   // Create a packet with just the ack
   3344   QuicFrame frame;
   3345   frame.type = ACK_FRAME;
   3346   frame.ack_frame = &ack_frame;
   3347   QuicFrames frames;
   3348   frames.push_back(frame);
   3349 
   3350   scoped_ptr<QuicPacket> raw_ack_packet(
   3351       framer_.BuildUnsizedDataPacket(header, frames).packet);
   3352   ASSERT_TRUE(raw_ack_packet != NULL);
   3353 
   3354   scoped_ptr<QuicEncryptedPacket> ack_packet(
   3355       framer_.EncryptPacket(ENCRYPTION_NONE, header.packet_sequence_number,
   3356                             *raw_ack_packet));
   3357 
   3358   // Now make sure we can turn our ack packet back into an ack frame
   3359   ASSERT_TRUE(framer_.ProcessPacket(*ack_packet));
   3360 
   3361   // Test for clean truncation of the ack by comparing the length of the
   3362   // original packets to the re-serialized packets.
   3363   frames.clear();
   3364   frame.type = ACK_FRAME;
   3365   frame.ack_frame = visitor_.ack_frames_[0];
   3366   frames.push_back(frame);
   3367 
   3368   size_t original_raw_length = raw_ack_packet->length();
   3369   raw_ack_packet.reset(
   3370       framer_.BuildUnsizedDataPacket(header, frames).packet);
   3371   ASSERT_TRUE(raw_ack_packet != NULL);
   3372   EXPECT_EQ(original_raw_length, raw_ack_packet->length());
   3373   ASSERT_TRUE(raw_ack_packet != NULL);
   3374 }
   3375 
   3376 TEST_P(QuicFramerTest, EntropyFlagTest) {
   3377   unsigned char packet[] = {
   3378     // public flags (8 byte guid)
   3379     0x3C,
   3380     // guid
   3381     0x10, 0x32, 0x54, 0x76,
   3382     0x98, 0xBA, 0xDC, 0xFE,
   3383     // packet sequence number
   3384     0xBC, 0x9A, 0x78, 0x56,
   3385     0x34, 0x12,
   3386     // private flags (Entropy)
   3387     0x01,
   3388 
   3389     // frame type (stream frame with fin and no length)
   3390     0xDF,
   3391     // stream id
   3392     0x04, 0x03, 0x02, 0x01,
   3393     // offset
   3394     0x54, 0x76, 0x10, 0x32,
   3395     0xDC, 0xFE, 0x98, 0xBA,
   3396     // data
   3397     'h',  'e',  'l',  'l',
   3398     'o',  ' ',  'w',  'o',
   3399     'r',  'l',  'd',  '!',
   3400   };
   3401 
   3402   QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
   3403   EXPECT_TRUE(framer_.ProcessPacket(encrypted));
   3404   EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
   3405   ASSERT_TRUE(visitor_.header_.get());
   3406   EXPECT_TRUE(visitor_.header_->entropy_flag);
   3407   EXPECT_EQ(1 << 4, visitor_.header_->entropy_hash);
   3408   EXPECT_FALSE(visitor_.header_->fec_flag);
   3409 };
   3410 
   3411 TEST_P(QuicFramerTest, FecEntropyTest) {
   3412   unsigned char packet[] = {
   3413     // public flags (8 byte guid)
   3414     0x3C,
   3415     // guid
   3416     0x10, 0x32, 0x54, 0x76,
   3417     0x98, 0xBA, 0xDC, 0xFE,
   3418     // packet sequence number
   3419     0xBC, 0x9A, 0x78, 0x56,
   3420     0x34, 0x12,
   3421     // private flags (Entropy & fec group & FEC)
   3422     0x07,
   3423     // first fec protected packet offset
   3424     0xFF,
   3425 
   3426     // frame type (stream frame with fin and no length)
   3427     0xDF,
   3428     // stream id
   3429     0x04, 0x03, 0x02, 0x01,
   3430     // offset
   3431     0x54, 0x76, 0x10, 0x32,
   3432     0xDC, 0xFE, 0x98, 0xBA,
   3433     // data
   3434     'h',  'e',  'l',  'l',
   3435     'o',  ' ',  'w',  'o',
   3436     'r',  'l',  'd',  '!',
   3437   };
   3438 
   3439   QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
   3440   EXPECT_TRUE(framer_.ProcessPacket(encrypted));
   3441   EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
   3442   ASSERT_TRUE(visitor_.header_.get());
   3443   EXPECT_TRUE(visitor_.header_->fec_flag);
   3444   EXPECT_TRUE(visitor_.header_->entropy_flag);
   3445   EXPECT_EQ(1 << 4, visitor_.header_->entropy_hash);
   3446 };
   3447 
   3448 TEST_P(QuicFramerTest, StopPacketProcessing) {
   3449   unsigned char packet[] = {
   3450     // public flags (8 byte guid)
   3451     0x3C,
   3452     // guid
   3453     0x10, 0x32, 0x54, 0x76,
   3454     0x98, 0xBA, 0xDC, 0xFE,
   3455     // packet sequence number
   3456     0xBC, 0x9A, 0x78, 0x56,
   3457     0x34, 0x12,
   3458     // Entropy
   3459     0x01,
   3460 
   3461     // frame type (stream frame with fin)
   3462     0xFF,
   3463     // stream id
   3464     0x04, 0x03, 0x02, 0x01,
   3465     // offset
   3466     0x54, 0x76, 0x10, 0x32,
   3467     0xDC, 0xFE, 0x98, 0xBA,
   3468     // data length
   3469     0x0c, 0x00,
   3470     // data
   3471     'h',  'e',  'l',  'l',
   3472     'o',  ' ',  'w',  'o',
   3473     'r',  'l',  'd',  '!',
   3474 
   3475     // frame type (ack frame)
   3476     0x40,
   3477     // entropy hash of sent packets till least awaiting - 1.
   3478     0x14,
   3479     // least packet sequence number awaiting an ack
   3480     0xA0, 0x9A, 0x78, 0x56,
   3481     0x34, 0x12,
   3482     // entropy hash of all received packets.
   3483     0x43,
   3484     // largest observed packet sequence number
   3485     0xBF, 0x9A, 0x78, 0x56,
   3486     0x34, 0x12,
   3487     // num missing packets
   3488     0x01,
   3489     // missing packet
   3490     0xBE, 0x9A, 0x78, 0x56,
   3491     0x34, 0x12,
   3492   };
   3493 
   3494   MockFramerVisitor visitor;
   3495   framer_.set_visitor(&visitor);
   3496   EXPECT_CALL(visitor, OnPacket());
   3497   EXPECT_CALL(visitor, OnPacketHeader(_));
   3498   EXPECT_CALL(visitor, OnStreamFrame(_)).WillOnce(Return(false));
   3499   EXPECT_CALL(visitor, OnAckFrame(_)).Times(0);
   3500   EXPECT_CALL(visitor, OnPacketComplete());
   3501 
   3502   QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
   3503   EXPECT_TRUE(framer_.ProcessPacket(encrypted));
   3504   EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
   3505 }
   3506 
   3507 }  // namespace test
   3508 }  // namespace net
   3509