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 "net/quic/quic_framer.h"
      6 
      7 #include <algorithm>
      8 #include <map>
      9 #include <string>
     10 #include <vector>
     11 
     12 #include "base/containers/hash_tables.h"
     13 #include "base/logging.h"
     14 #include "base/memory/scoped_ptr.h"
     15 #include "base/port.h"
     16 #include "base/stl_util.h"
     17 #include "net/quic/crypto/quic_decrypter.h"
     18 #include "net/quic/crypto/quic_encrypter.h"
     19 #include "net/quic/quic_protocol.h"
     20 #include "net/quic/quic_utils.h"
     21 #include "net/quic/test_tools/quic_framer_peer.h"
     22 #include "net/quic/test_tools/quic_test_utils.h"
     23 #include "net/test/gtest_util.h"
     24 
     25 using base::hash_set;
     26 using base::StringPiece;
     27 using std::make_pair;
     28 using std::map;
     29 using std::numeric_limits;
     30 using std::pair;
     31 using std::string;
     32 using std::vector;
     33 using testing::Return;
     34 using testing::_;
     35 
     36 namespace net {
     37 namespace test {
     38 
     39 const QuicPacketSequenceNumber kEpoch = GG_UINT64_C(1) << 48;
     40 const QuicPacketSequenceNumber kMask = kEpoch - 1;
     41 
     42 // Index into the connection_id offset in the header.
     43 const size_t kConnectionIdOffset = kPublicFlagsSize;
     44 // Index into the version string in the header. (if present).
     45 const size_t kVersionOffset = kConnectionIdOffset + PACKET_8BYTE_CONNECTION_ID;
     46 
     47 // Size in bytes of the stream frame fields for an arbitrary StreamID and
     48 // offset and the last frame in a packet.
     49 size_t GetMinStreamFrameSize(QuicVersion version) {
     50   return kQuicFrameTypeSize + kQuicMaxStreamIdSize + kQuicMaxStreamOffsetSize;
     51 }
     52 
     53 // Index into the sequence number offset in the header.
     54 size_t GetSequenceNumberOffset(QuicConnectionIdLength connection_id_length,
     55                                bool include_version) {
     56   return kConnectionIdOffset + connection_id_length +
     57       (include_version ? kQuicVersionSize : 0);
     58 }
     59 
     60 size_t GetSequenceNumberOffset(bool include_version) {
     61   return GetSequenceNumberOffset(PACKET_8BYTE_CONNECTION_ID, include_version);
     62 }
     63 
     64 // Index into the private flags offset in the data packet header.
     65 size_t GetPrivateFlagsOffset(QuicConnectionIdLength connection_id_length,
     66                              bool include_version) {
     67   return GetSequenceNumberOffset(connection_id_length, include_version) +
     68       PACKET_6BYTE_SEQUENCE_NUMBER;
     69 }
     70 
     71 size_t GetPrivateFlagsOffset(bool include_version) {
     72   return GetPrivateFlagsOffset(PACKET_8BYTE_CONNECTION_ID, include_version);
     73 }
     74 
     75 size_t GetPrivateFlagsOffset(bool include_version,
     76                              QuicSequenceNumberLength sequence_number_length) {
     77   return GetSequenceNumberOffset(PACKET_8BYTE_CONNECTION_ID, include_version) +
     78       sequence_number_length;
     79 }
     80 
     81 // Index into the fec group offset in the header.
     82 size_t GetFecGroupOffset(QuicConnectionIdLength connection_id_length,
     83                          bool include_version) {
     84   return GetPrivateFlagsOffset(connection_id_length, include_version) +
     85       kPrivateFlagsSize;
     86 }
     87 
     88 size_t GetFecGroupOffset(bool include_version) {
     89   return GetPrivateFlagsOffset(PACKET_8BYTE_CONNECTION_ID, include_version) +
     90       kPrivateFlagsSize;
     91 }
     92 
     93 size_t GetFecGroupOffset(bool include_version,
     94                          QuicSequenceNumberLength sequence_number_length) {
     95   return GetPrivateFlagsOffset(include_version, sequence_number_length) +
     96       kPrivateFlagsSize;
     97 }
     98 
     99 // Index into the message tag of the public reset packet.
    100 // Public resets always have full connection_ids.
    101 const size_t kPublicResetPacketMessageTagOffset =
    102     kConnectionIdOffset + PACKET_8BYTE_CONNECTION_ID;
    103 
    104 class TestEncrypter : public QuicEncrypter {
    105  public:
    106   virtual ~TestEncrypter() {}
    107   virtual bool SetKey(StringPiece key) OVERRIDE {
    108     return true;
    109   }
    110   virtual bool SetNoncePrefix(StringPiece nonce_prefix) OVERRIDE {
    111     return true;
    112   }
    113   virtual bool Encrypt(StringPiece nonce,
    114                        StringPiece associated_data,
    115                        StringPiece plaintext,
    116                        unsigned char* output) OVERRIDE {
    117     CHECK(false) << "Not implemented";
    118     return false;
    119   }
    120   virtual QuicData* EncryptPacket(QuicPacketSequenceNumber sequence_number,
    121                                   StringPiece associated_data,
    122                                   StringPiece plaintext) OVERRIDE {
    123     sequence_number_ = sequence_number;
    124     associated_data_ = associated_data.as_string();
    125     plaintext_ = plaintext.as_string();
    126     return new QuicData(plaintext.data(), plaintext.length());
    127   }
    128   virtual size_t GetKeySize() const OVERRIDE {
    129     return 0;
    130   }
    131   virtual size_t GetNoncePrefixSize() const OVERRIDE {
    132     return 0;
    133   }
    134   virtual size_t GetMaxPlaintextSize(size_t ciphertext_size) const OVERRIDE {
    135     return ciphertext_size;
    136   }
    137   virtual size_t GetCiphertextSize(size_t plaintext_size) const OVERRIDE {
    138     return plaintext_size;
    139   }
    140   virtual StringPiece GetKey() const OVERRIDE {
    141     return StringPiece();
    142   }
    143   virtual StringPiece GetNoncePrefix() const OVERRIDE {
    144     return StringPiece();
    145   }
    146   QuicPacketSequenceNumber sequence_number_;
    147   string associated_data_;
    148   string plaintext_;
    149 };
    150 
    151 class TestDecrypter : public QuicDecrypter {
    152  public:
    153   virtual ~TestDecrypter() {}
    154   virtual bool SetKey(StringPiece key) OVERRIDE {
    155     return true;
    156   }
    157   virtual bool SetNoncePrefix(StringPiece nonce_prefix) OVERRIDE {
    158     return true;
    159   }
    160   virtual bool Decrypt(StringPiece nonce,
    161                        StringPiece associated_data,
    162                        StringPiece ciphertext,
    163                        unsigned char* output,
    164                        size_t* output_length) OVERRIDE {
    165     CHECK(false) << "Not implemented";
    166     return false;
    167   }
    168   virtual QuicData* DecryptPacket(QuicPacketSequenceNumber sequence_number,
    169                                   StringPiece associated_data,
    170                                   StringPiece ciphertext) OVERRIDE {
    171     sequence_number_ = sequence_number;
    172     associated_data_ = associated_data.as_string();
    173     ciphertext_ = ciphertext.as_string();
    174     return new QuicData(ciphertext.data(), ciphertext.length());
    175   }
    176   virtual StringPiece GetKey() const OVERRIDE {
    177     return StringPiece();
    178   }
    179   virtual StringPiece GetNoncePrefix() const OVERRIDE {
    180     return StringPiece();
    181   }
    182   QuicPacketSequenceNumber sequence_number_;
    183   string associated_data_;
    184   string ciphertext_;
    185 };
    186 
    187 class TestQuicVisitor : public ::net::QuicFramerVisitorInterface {
    188  public:
    189   TestQuicVisitor()
    190       : error_count_(0),
    191         version_mismatch_(0),
    192         packet_count_(0),
    193         frame_count_(0),
    194         fec_count_(0),
    195         complete_packets_(0),
    196         revived_packets_(0),
    197         accept_packet_(true),
    198         accept_public_header_(true) {
    199   }
    200 
    201   virtual ~TestQuicVisitor() {
    202     STLDeleteElements(&stream_frames_);
    203     STLDeleteElements(&ack_frames_);
    204     STLDeleteElements(&congestion_feedback_frames_);
    205     STLDeleteElements(&stop_waiting_frames_);
    206     STLDeleteElements(&ping_frames_);
    207     STLDeleteElements(&fec_data_);
    208   }
    209 
    210   virtual void OnError(QuicFramer* f) OVERRIDE {
    211     DVLOG(1) << "QuicFramer Error: " << QuicUtils::ErrorToString(f->error())
    212              << " (" << f->error() << ")";
    213     ++error_count_;
    214   }
    215 
    216   virtual void OnPacket() OVERRIDE {}
    217 
    218   virtual void OnPublicResetPacket(
    219       const QuicPublicResetPacket& packet) OVERRIDE {
    220     public_reset_packet_.reset(new QuicPublicResetPacket(packet));
    221   }
    222 
    223   virtual void OnVersionNegotiationPacket(
    224       const QuicVersionNegotiationPacket& packet) OVERRIDE {
    225     version_negotiation_packet_.reset(new QuicVersionNegotiationPacket(packet));
    226   }
    227 
    228   virtual void OnRevivedPacket() OVERRIDE {
    229     ++revived_packets_;
    230   }
    231 
    232   virtual bool OnProtocolVersionMismatch(QuicVersion version) OVERRIDE {
    233     DVLOG(1) << "QuicFramer Version Mismatch, version: " << version;
    234     ++version_mismatch_;
    235     return true;
    236   }
    237 
    238   virtual bool OnUnauthenticatedPublicHeader(
    239       const QuicPacketPublicHeader& header) OVERRIDE {
    240     public_header_.reset(new QuicPacketPublicHeader(header));
    241     return accept_public_header_;
    242   }
    243 
    244   virtual bool OnUnauthenticatedHeader(
    245       const QuicPacketHeader& header) OVERRIDE {
    246     return true;
    247   }
    248 
    249   virtual void OnDecryptedPacket(EncryptionLevel level) OVERRIDE {}
    250 
    251   virtual bool OnPacketHeader(const QuicPacketHeader& header) OVERRIDE {
    252     ++packet_count_;
    253     header_.reset(new QuicPacketHeader(header));
    254     return accept_packet_;
    255   }
    256 
    257   virtual bool OnStreamFrame(const QuicStreamFrame& frame) OVERRIDE {
    258     ++frame_count_;
    259     stream_frames_.push_back(new QuicStreamFrame(frame));
    260     return true;
    261   }
    262 
    263   virtual void OnFecProtectedPayload(StringPiece payload) OVERRIDE {
    264     fec_protected_payload_ = payload.as_string();
    265   }
    266 
    267   virtual bool OnAckFrame(const QuicAckFrame& frame) OVERRIDE {
    268     ++frame_count_;
    269     ack_frames_.push_back(new QuicAckFrame(frame));
    270     return true;
    271   }
    272 
    273   virtual bool OnCongestionFeedbackFrame(
    274       const QuicCongestionFeedbackFrame& frame) OVERRIDE {
    275     ++frame_count_;
    276     congestion_feedback_frames_.push_back(
    277         new QuicCongestionFeedbackFrame(frame));
    278     return true;
    279   }
    280 
    281   virtual bool OnStopWaitingFrame(const QuicStopWaitingFrame& frame) OVERRIDE {
    282     ++frame_count_;
    283     stop_waiting_frames_.push_back(new QuicStopWaitingFrame(frame));
    284     return true;
    285   }
    286 
    287   virtual bool OnPingFrame(const QuicPingFrame& frame) OVERRIDE {
    288     ++frame_count_;
    289     ping_frames_.push_back(new QuicPingFrame(frame));
    290     return true;
    291   }
    292 
    293   virtual void OnFecData(const QuicFecData& fec) OVERRIDE {
    294     ++fec_count_;
    295     fec_data_.push_back(new QuicFecData(fec));
    296   }
    297 
    298   virtual void OnPacketComplete() OVERRIDE {
    299     ++complete_packets_;
    300   }
    301 
    302   virtual bool OnRstStreamFrame(const QuicRstStreamFrame& frame) OVERRIDE {
    303     rst_stream_frame_ = frame;
    304     return true;
    305   }
    306 
    307   virtual bool OnConnectionCloseFrame(
    308       const QuicConnectionCloseFrame& frame) OVERRIDE {
    309     connection_close_frame_ = frame;
    310     return true;
    311   }
    312 
    313   virtual bool OnGoAwayFrame(const QuicGoAwayFrame& frame) OVERRIDE {
    314     goaway_frame_ = frame;
    315     return true;
    316   }
    317 
    318   virtual bool OnWindowUpdateFrame(const QuicWindowUpdateFrame& frame)
    319       OVERRIDE {
    320     window_update_frame_ = frame;
    321     return true;
    322   }
    323 
    324   virtual bool OnBlockedFrame(const QuicBlockedFrame& frame) OVERRIDE {
    325     blocked_frame_ = frame;
    326     return true;
    327   }
    328 
    329   // Counters from the visitor_ callbacks.
    330   int error_count_;
    331   int version_mismatch_;
    332   int packet_count_;
    333   int frame_count_;
    334   int fec_count_;
    335   int complete_packets_;
    336   int revived_packets_;
    337   bool accept_packet_;
    338   bool accept_public_header_;
    339 
    340   scoped_ptr<QuicPacketHeader> header_;
    341   scoped_ptr<QuicPacketPublicHeader> public_header_;
    342   scoped_ptr<QuicPublicResetPacket> public_reset_packet_;
    343   scoped_ptr<QuicVersionNegotiationPacket> version_negotiation_packet_;
    344   vector<QuicStreamFrame*> stream_frames_;
    345   vector<QuicAckFrame*> ack_frames_;
    346   vector<QuicCongestionFeedbackFrame*> congestion_feedback_frames_;
    347   vector<QuicStopWaitingFrame*> stop_waiting_frames_;
    348   vector<QuicPingFrame*> ping_frames_;
    349   vector<QuicFecData*> fec_data_;
    350   string fec_protected_payload_;
    351   QuicRstStreamFrame rst_stream_frame_;
    352   QuicConnectionCloseFrame connection_close_frame_;
    353   QuicGoAwayFrame goaway_frame_;
    354   QuicWindowUpdateFrame window_update_frame_;
    355   QuicBlockedFrame blocked_frame_;
    356 };
    357 
    358 class QuicFramerTest : public ::testing::TestWithParam<QuicVersion> {
    359  public:
    360   QuicFramerTest()
    361       : encrypter_(new test::TestEncrypter()),
    362         decrypter_(new test::TestDecrypter()),
    363         start_(QuicTime::Zero().Add(QuicTime::Delta::FromMicroseconds(0x10))),
    364         framer_(QuicSupportedVersions(), start_, true) {
    365     version_ = GetParam();
    366     framer_.set_version(version_);
    367     framer_.SetDecrypter(decrypter_, ENCRYPTION_NONE);
    368     framer_.SetEncrypter(ENCRYPTION_NONE, encrypter_);
    369     framer_.set_visitor(&visitor_);
    370     framer_.set_received_entropy_calculator(&entropy_calculator_);
    371   }
    372 
    373   // Helper function to get unsigned char representation of digit in the
    374   // units place of the current QUIC version number.
    375   unsigned char GetQuicVersionDigitOnes() {
    376     return static_cast<unsigned char> ('0' + version_%10);
    377   }
    378 
    379   // Helper function to get unsigned char representation of digit in the
    380   // tens place of the current QUIC version number.
    381   unsigned char GetQuicVersionDigitTens() {
    382     return static_cast<unsigned char> ('0' + (version_/10)%10);
    383   }
    384 
    385   bool CheckEncryption(QuicPacketSequenceNumber sequence_number,
    386                        QuicPacket* packet) {
    387     if (sequence_number != encrypter_->sequence_number_) {
    388       LOG(ERROR) << "Encrypted incorrect packet sequence number.  expected "
    389                  << sequence_number << " actual: "
    390                  << encrypter_->sequence_number_;
    391       return false;
    392     }
    393     if (packet->AssociatedData() != encrypter_->associated_data_) {
    394       LOG(ERROR) << "Encrypted incorrect associated data.  expected "
    395                  << packet->AssociatedData() << " actual: "
    396                  << encrypter_->associated_data_;
    397       return false;
    398     }
    399     if (packet->Plaintext() != encrypter_->plaintext_) {
    400       LOG(ERROR) << "Encrypted incorrect plaintext data.  expected "
    401                  << packet->Plaintext() << " actual: "
    402                  << encrypter_->plaintext_;
    403       return false;
    404     }
    405     return true;
    406   }
    407 
    408   bool CheckDecryption(const QuicEncryptedPacket& encrypted,
    409                        bool includes_version) {
    410     if (visitor_.header_->packet_sequence_number !=
    411         decrypter_->sequence_number_) {
    412       LOG(ERROR) << "Decrypted incorrect packet sequence number.  expected "
    413                  << visitor_.header_->packet_sequence_number << " actual: "
    414                  << decrypter_->sequence_number_;
    415       return false;
    416     }
    417     if (QuicFramer::GetAssociatedDataFromEncryptedPacket(
    418             encrypted, PACKET_8BYTE_CONNECTION_ID,
    419             includes_version, PACKET_6BYTE_SEQUENCE_NUMBER) !=
    420         decrypter_->associated_data_) {
    421       LOG(ERROR) << "Decrypted incorrect associated data.  expected "
    422                  << QuicFramer::GetAssociatedDataFromEncryptedPacket(
    423                      encrypted, PACKET_8BYTE_CONNECTION_ID,
    424                      includes_version, PACKET_6BYTE_SEQUENCE_NUMBER)
    425                  << " actual: " << decrypter_->associated_data_;
    426       return false;
    427     }
    428     StringPiece ciphertext(encrypted.AsStringPiece().substr(
    429         GetStartOfEncryptedData(PACKET_8BYTE_CONNECTION_ID, includes_version,
    430                                 PACKET_6BYTE_SEQUENCE_NUMBER)));
    431     if (ciphertext != decrypter_->ciphertext_) {
    432       LOG(ERROR) << "Decrypted incorrect ciphertext data.  expected "
    433                  << ciphertext << " actual: "
    434                  << decrypter_->ciphertext_;
    435       return false;
    436     }
    437     return true;
    438   }
    439 
    440   char* AsChars(unsigned char* data) {
    441     return reinterpret_cast<char*>(data);
    442   }
    443 
    444   void CheckProcessingFails(unsigned char* packet,
    445                             size_t len,
    446                             string expected_error,
    447                             QuicErrorCode error_code) {
    448     QuicEncryptedPacket encrypted(AsChars(packet), len, false);
    449     EXPECT_FALSE(framer_.ProcessPacket(encrypted)) << "len: " << len;
    450     EXPECT_EQ(expected_error, framer_.detailed_error()) << "len: " << len;
    451     EXPECT_EQ(error_code, framer_.error()) << "len: " << len;
    452   }
    453 
    454   // Checks if the supplied string matches data in the supplied StreamFrame.
    455   void CheckStreamFrameData(string str, QuicStreamFrame* frame) {
    456     scoped_ptr<string> frame_data(frame->GetDataAsString());
    457     EXPECT_EQ(str, *frame_data);
    458   }
    459 
    460   void CheckStreamFrameBoundaries(unsigned char* packet,
    461                                   size_t stream_id_size,
    462                                   bool include_version) {
    463     // Now test framing boundaries
    464     for (size_t i = kQuicFrameTypeSize;
    465          i < GetMinStreamFrameSize(framer_.version()); ++i) {
    466       string expected_error;
    467       if (i < kQuicFrameTypeSize + stream_id_size) {
    468         expected_error = "Unable to read stream_id.";
    469       } else if (i < kQuicFrameTypeSize + stream_id_size +
    470                  kQuicMaxStreamOffsetSize) {
    471         expected_error = "Unable to read offset.";
    472       } else {
    473         expected_error = "Unable to read frame data.";
    474       }
    475       CheckProcessingFails(
    476           packet,
    477           i + GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID, include_version,
    478                                   PACKET_6BYTE_SEQUENCE_NUMBER,
    479                                   NOT_IN_FEC_GROUP),
    480           expected_error, QUIC_INVALID_STREAM_DATA);
    481     }
    482   }
    483 
    484   void CheckCalculatePacketSequenceNumber(
    485       QuicPacketSequenceNumber expected_sequence_number,
    486       QuicPacketSequenceNumber last_sequence_number) {
    487     QuicPacketSequenceNumber wire_sequence_number =
    488         expected_sequence_number & kMask;
    489     QuicFramerPeer::SetLastSequenceNumber(&framer_, last_sequence_number);
    490     EXPECT_EQ(expected_sequence_number,
    491               QuicFramerPeer::CalculatePacketSequenceNumberFromWire(
    492                   &framer_, PACKET_6BYTE_SEQUENCE_NUMBER, wire_sequence_number))
    493         << "last_sequence_number: " << last_sequence_number
    494         << " wire_sequence_number: " << wire_sequence_number;
    495   }
    496 
    497   QuicPacket* BuildDataPacket(const QuicPacketHeader& header,
    498                               const QuicFrames& frames) {
    499     return BuildUnsizedDataPacket(&framer_, header, frames).packet;
    500   }
    501 
    502   test::TestEncrypter* encrypter_;
    503   test::TestDecrypter* decrypter_;
    504   QuicVersion version_;
    505   QuicTime start_;
    506   QuicFramer framer_;
    507   test::TestQuicVisitor visitor_;
    508   test::TestEntropyCalculator entropy_calculator_;
    509 };
    510 
    511 // Run all framer tests with all supported versions of QUIC.
    512 INSTANTIATE_TEST_CASE_P(QuicFramerTests,
    513                         QuicFramerTest,
    514                         ::testing::ValuesIn(kSupportedQuicVersions));
    515 
    516 TEST_P(QuicFramerTest, CalculatePacketSequenceNumberFromWireNearEpochStart) {
    517   // A few quick manual sanity checks
    518   CheckCalculatePacketSequenceNumber(GG_UINT64_C(1), GG_UINT64_C(0));
    519   CheckCalculatePacketSequenceNumber(kEpoch + 1, kMask);
    520   CheckCalculatePacketSequenceNumber(kEpoch, kMask);
    521 
    522   // Cases where the last number was close to the start of the range
    523   for (uint64 last = 0; last < 10; last++) {
    524     // Small numbers should not wrap (even if they're out of order).
    525     for (uint64 j = 0; j < 10; j++) {
    526       CheckCalculatePacketSequenceNumber(j, last);
    527     }
    528 
    529     // Large numbers should not wrap either (because we're near 0 already).
    530     for (uint64 j = 0; j < 10; j++) {
    531       CheckCalculatePacketSequenceNumber(kEpoch - 1 - j, last);
    532     }
    533   }
    534 }
    535 
    536 TEST_P(QuicFramerTest, CalculatePacketSequenceNumberFromWireNearEpochEnd) {
    537   // Cases where the last number was close to the end of the range
    538   for (uint64 i = 0; i < 10; i++) {
    539     QuicPacketSequenceNumber last = kEpoch - i;
    540 
    541     // Small numbers should wrap.
    542     for (uint64 j = 0; j < 10; j++) {
    543       CheckCalculatePacketSequenceNumber(kEpoch + j, last);
    544     }
    545 
    546     // Large numbers should not (even if they're out of order).
    547     for (uint64 j = 0; j < 10; j++) {
    548       CheckCalculatePacketSequenceNumber(kEpoch - 1 - j, last);
    549     }
    550   }
    551 }
    552 
    553 // Next check where we're in a non-zero epoch to verify we handle
    554 // reverse wrapping, too.
    555 TEST_P(QuicFramerTest, CalculatePacketSequenceNumberFromWireNearPrevEpoch) {
    556   const uint64 prev_epoch = 1 * kEpoch;
    557   const uint64 cur_epoch = 2 * kEpoch;
    558   // Cases where the last number was close to the start of the range
    559   for (uint64 i = 0; i < 10; i++) {
    560     uint64 last = cur_epoch + i;
    561     // Small number should not wrap (even if they're out of order).
    562     for (uint64 j = 0; j < 10; j++) {
    563       CheckCalculatePacketSequenceNumber(cur_epoch + j, last);
    564     }
    565 
    566     // But large numbers should reverse wrap.
    567     for (uint64 j = 0; j < 10; j++) {
    568       uint64 num = kEpoch - 1 - j;
    569       CheckCalculatePacketSequenceNumber(prev_epoch + num, last);
    570     }
    571   }
    572 }
    573 
    574 TEST_P(QuicFramerTest, CalculatePacketSequenceNumberFromWireNearNextEpoch) {
    575   const uint64 cur_epoch = 2 * kEpoch;
    576   const uint64 next_epoch = 3 * kEpoch;
    577   // Cases where the last number was close to the end of the range
    578   for (uint64 i = 0; i < 10; i++) {
    579     QuicPacketSequenceNumber last = next_epoch - 1 - i;
    580 
    581     // Small numbers should wrap.
    582     for (uint64 j = 0; j < 10; j++) {
    583       CheckCalculatePacketSequenceNumber(next_epoch + j, last);
    584     }
    585 
    586     // but large numbers should not (even if they're out of order).
    587     for (uint64 j = 0; j < 10; j++) {
    588       uint64 num = kEpoch - 1 - j;
    589       CheckCalculatePacketSequenceNumber(cur_epoch + num, last);
    590     }
    591   }
    592 }
    593 
    594 TEST_P(QuicFramerTest, CalculatePacketSequenceNumberFromWireNearNextMax) {
    595   const uint64 max_number = numeric_limits<uint64>::max();
    596   const uint64 max_epoch = max_number & ~kMask;
    597 
    598   // Cases where the last number was close to the end of the range
    599   for (uint64 i = 0; i < 10; i++) {
    600     // Subtract 1, because the expected next sequence number is 1 more than the
    601     // last sequence number.
    602     QuicPacketSequenceNumber last = max_number - i - 1;
    603 
    604     // Small numbers should not wrap, because they have nowhere to go.
    605     for (uint64 j = 0; j < 10; j++) {
    606       CheckCalculatePacketSequenceNumber(max_epoch + j, last);
    607     }
    608 
    609     // Large numbers should not wrap either.
    610     for (uint64 j = 0; j < 10; j++) {
    611       uint64 num = kEpoch - 1 - j;
    612       CheckCalculatePacketSequenceNumber(max_epoch + num, last);
    613     }
    614   }
    615 }
    616 
    617 TEST_P(QuicFramerTest, EmptyPacket) {
    618   char packet[] = { 0x00 };
    619   QuicEncryptedPacket encrypted(packet, 0, false);
    620   EXPECT_FALSE(framer_.ProcessPacket(encrypted));
    621   EXPECT_EQ(QUIC_INVALID_PACKET_HEADER, framer_.error());
    622 }
    623 
    624 TEST_P(QuicFramerTest, LargePacket) {
    625   unsigned char packet[kMaxPacketSize + 1] = {
    626     // public flags (8 byte connection_id)
    627     0x3C,
    628     // connection_id
    629     0x10, 0x32, 0x54, 0x76,
    630     0x98, 0xBA, 0xDC, 0xFE,
    631     // packet sequence number
    632     0xBC, 0x9A, 0x78, 0x56,
    633     0x34, 0x12,
    634     // private flags
    635     0x00,
    636   };
    637 
    638   memset(packet + GetPacketHeaderSize(
    639              PACKET_8BYTE_CONNECTION_ID, !kIncludeVersion,
    640              PACKET_6BYTE_SEQUENCE_NUMBER, NOT_IN_FEC_GROUP), 0,
    641          kMaxPacketSize - GetPacketHeaderSize(
    642              PACKET_8BYTE_CONNECTION_ID, !kIncludeVersion,
    643              PACKET_6BYTE_SEQUENCE_NUMBER, NOT_IN_FEC_GROUP) + 1);
    644 
    645   QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
    646   EXPECT_FALSE(framer_.ProcessPacket(encrypted));
    647 
    648   ASSERT_TRUE(visitor_.header_.get());
    649   // Make sure we've parsed the packet header, so we can send an error.
    650   EXPECT_EQ(GG_UINT64_C(0xFEDCBA9876543210),
    651             visitor_.header_->public_header.connection_id);
    652   // Make sure the correct error is propagated.
    653   EXPECT_EQ(QUIC_PACKET_TOO_LARGE, framer_.error());
    654 }
    655 
    656 TEST_P(QuicFramerTest, PacketHeader) {
    657   unsigned char packet[] = {
    658     // public flags (8 byte connection_id)
    659     0x3C,
    660     // connection_id
    661     0x10, 0x32, 0x54, 0x76,
    662     0x98, 0xBA, 0xDC, 0xFE,
    663     // packet sequence number
    664     0xBC, 0x9A, 0x78, 0x56,
    665     0x34, 0x12,
    666     // private flags
    667     0x00,
    668   };
    669 
    670   QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
    671   EXPECT_FALSE(framer_.ProcessPacket(encrypted));
    672   EXPECT_EQ(QUIC_MISSING_PAYLOAD, framer_.error());
    673   ASSERT_TRUE(visitor_.header_.get());
    674   EXPECT_EQ(GG_UINT64_C(0xFEDCBA9876543210),
    675             visitor_.header_->public_header.connection_id);
    676   EXPECT_FALSE(visitor_.header_->public_header.reset_flag);
    677   EXPECT_FALSE(visitor_.header_->public_header.version_flag);
    678   EXPECT_FALSE(visitor_.header_->fec_flag);
    679   EXPECT_FALSE(visitor_.header_->entropy_flag);
    680   EXPECT_EQ(0, visitor_.header_->entropy_hash);
    681   EXPECT_EQ(GG_UINT64_C(0x123456789ABC),
    682             visitor_.header_->packet_sequence_number);
    683   EXPECT_EQ(NOT_IN_FEC_GROUP, visitor_.header_->is_in_fec_group);
    684   EXPECT_EQ(0x00u, visitor_.header_->fec_group);
    685 
    686   // Now test framing boundaries
    687   for (size_t i = 0;
    688        i < GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID, !kIncludeVersion,
    689                                PACKET_6BYTE_SEQUENCE_NUMBER, NOT_IN_FEC_GROUP);
    690        ++i) {
    691     string expected_error;
    692     if (i < kConnectionIdOffset) {
    693       expected_error = "Unable to read public flags.";
    694     } else if (i < GetSequenceNumberOffset(!kIncludeVersion)) {
    695       expected_error = "Unable to read ConnectionId.";
    696     } else if (i < GetPrivateFlagsOffset(!kIncludeVersion)) {
    697       expected_error = "Unable to read sequence number.";
    698     } else if (i < GetFecGroupOffset(!kIncludeVersion)) {
    699       expected_error = "Unable to read private flags.";
    700     } else {
    701       expected_error = "Unable to read first fec protected packet offset.";
    702     }
    703     CheckProcessingFails(packet, i, expected_error, QUIC_INVALID_PACKET_HEADER);
    704   }
    705 }
    706 
    707 TEST_P(QuicFramerTest, PacketHeaderWith4ByteConnectionId) {
    708   QuicFramerPeer::SetLastSerializedConnectionId(
    709       &framer_, GG_UINT64_C(0xFEDCBA9876543210));
    710 
    711   unsigned char packet[] = {
    712     // public flags (4 byte connection_id)
    713     0x38,
    714     // connection_id
    715     0x10, 0x32, 0x54, 0x76,
    716     // packet sequence number
    717     0xBC, 0x9A, 0x78, 0x56,
    718     0x34, 0x12,
    719     // private flags
    720     0x00,
    721   };
    722 
    723   QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
    724   EXPECT_FALSE(framer_.ProcessPacket(encrypted));
    725   EXPECT_EQ(QUIC_MISSING_PAYLOAD, framer_.error());
    726   ASSERT_TRUE(visitor_.header_.get());
    727   EXPECT_EQ(GG_UINT64_C(0xFEDCBA9876543210),
    728             visitor_.header_->public_header.connection_id);
    729   EXPECT_FALSE(visitor_.header_->public_header.reset_flag);
    730   EXPECT_FALSE(visitor_.header_->public_header.version_flag);
    731   EXPECT_FALSE(visitor_.header_->fec_flag);
    732   EXPECT_FALSE(visitor_.header_->entropy_flag);
    733   EXPECT_EQ(0, visitor_.header_->entropy_hash);
    734   EXPECT_EQ(GG_UINT64_C(0x123456789ABC),
    735             visitor_.header_->packet_sequence_number);
    736   EXPECT_EQ(NOT_IN_FEC_GROUP, visitor_.header_->is_in_fec_group);
    737   EXPECT_EQ(0x00u, visitor_.header_->fec_group);
    738 
    739   // Now test framing boundaries
    740   for (size_t i = 0;
    741        i < GetPacketHeaderSize(PACKET_4BYTE_CONNECTION_ID, !kIncludeVersion,
    742                                PACKET_6BYTE_SEQUENCE_NUMBER, NOT_IN_FEC_GROUP);
    743        ++i) {
    744     string expected_error;
    745     if (i < kConnectionIdOffset) {
    746       expected_error = "Unable to read public flags.";
    747     } else if (i < GetSequenceNumberOffset(PACKET_4BYTE_CONNECTION_ID,
    748                                            !kIncludeVersion)) {
    749       expected_error = "Unable to read ConnectionId.";
    750     } else if (i < GetPrivateFlagsOffset(PACKET_4BYTE_CONNECTION_ID,
    751                                          !kIncludeVersion)) {
    752       expected_error = "Unable to read sequence number.";
    753     } else if (i < GetFecGroupOffset(PACKET_4BYTE_CONNECTION_ID,
    754                                      !kIncludeVersion)) {
    755       expected_error = "Unable to read private flags.";
    756     } else {
    757       expected_error = "Unable to read first fec protected packet offset.";
    758     }
    759     CheckProcessingFails(packet, i, expected_error, QUIC_INVALID_PACKET_HEADER);
    760   }
    761 }
    762 
    763 TEST_P(QuicFramerTest, PacketHeader1ByteConnectionId) {
    764   QuicFramerPeer::SetLastSerializedConnectionId(
    765       &framer_, GG_UINT64_C(0xFEDCBA9876543210));
    766 
    767   unsigned char packet[] = {
    768     // public flags (1 byte connection_id)
    769     0x34,
    770     // connection_id
    771     0x10,
    772     // packet sequence number
    773     0xBC, 0x9A, 0x78, 0x56,
    774     0x34, 0x12,
    775     // private flags
    776     0x00,
    777   };
    778 
    779   QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
    780   EXPECT_FALSE(framer_.ProcessPacket(encrypted));
    781   EXPECT_EQ(QUIC_MISSING_PAYLOAD, framer_.error());
    782   ASSERT_TRUE(visitor_.header_.get());
    783   EXPECT_EQ(GG_UINT64_C(0xFEDCBA9876543210),
    784             visitor_.header_->public_header.connection_id);
    785   EXPECT_FALSE(visitor_.header_->public_header.reset_flag);
    786   EXPECT_FALSE(visitor_.header_->public_header.version_flag);
    787   EXPECT_FALSE(visitor_.header_->fec_flag);
    788   EXPECT_FALSE(visitor_.header_->entropy_flag);
    789   EXPECT_EQ(0, visitor_.header_->entropy_hash);
    790   EXPECT_EQ(GG_UINT64_C(0x123456789ABC),
    791             visitor_.header_->packet_sequence_number);
    792   EXPECT_EQ(NOT_IN_FEC_GROUP, visitor_.header_->is_in_fec_group);
    793   EXPECT_EQ(0x00u, visitor_.header_->fec_group);
    794 
    795   // Now test framing boundaries
    796   for (size_t i = 0;
    797        i < GetPacketHeaderSize(PACKET_1BYTE_CONNECTION_ID, !kIncludeVersion,
    798                                PACKET_6BYTE_SEQUENCE_NUMBER, NOT_IN_FEC_GROUP);
    799        ++i) {
    800     string expected_error;
    801     if (i < kConnectionIdOffset) {
    802       expected_error = "Unable to read public flags.";
    803     } else if (i < GetSequenceNumberOffset(PACKET_1BYTE_CONNECTION_ID,
    804                                            !kIncludeVersion)) {
    805       expected_error = "Unable to read ConnectionId.";
    806     } else if (i < GetPrivateFlagsOffset(PACKET_1BYTE_CONNECTION_ID,
    807                                          !kIncludeVersion)) {
    808       expected_error = "Unable to read sequence number.";
    809     } else if (i < GetFecGroupOffset(PACKET_1BYTE_CONNECTION_ID,
    810                                      !kIncludeVersion)) {
    811       expected_error = "Unable to read private flags.";
    812     } else {
    813       expected_error = "Unable to read first fec protected packet offset.";
    814     }
    815     CheckProcessingFails(packet, i, expected_error, QUIC_INVALID_PACKET_HEADER);
    816   }
    817 }
    818 
    819 TEST_P(QuicFramerTest, PacketHeaderWith0ByteConnectionId) {
    820   QuicFramerPeer::SetLastSerializedConnectionId(
    821       &framer_, GG_UINT64_C(0xFEDCBA9876543210));
    822 
    823   unsigned char packet[] = {
    824     // public flags (0 byte connection_id)
    825     0x30,
    826     // connection_id
    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.connection_id);
    840   EXPECT_FALSE(visitor_.header_->public_header.reset_flag);
    841   EXPECT_FALSE(visitor_.header_->public_header.version_flag);
    842   EXPECT_FALSE(visitor_.header_->fec_flag);
    843   EXPECT_FALSE(visitor_.header_->entropy_flag);
    844   EXPECT_EQ(0, visitor_.header_->entropy_hash);
    845   EXPECT_EQ(GG_UINT64_C(0x123456789ABC),
    846             visitor_.header_->packet_sequence_number);
    847   EXPECT_EQ(NOT_IN_FEC_GROUP, visitor_.header_->is_in_fec_group);
    848   EXPECT_EQ(0x00u, visitor_.header_->fec_group);
    849 
    850   // Now test framing boundaries
    851   for (size_t i = 0;
    852        i < GetPacketHeaderSize(PACKET_0BYTE_CONNECTION_ID, !kIncludeVersion,
    853                                PACKET_6BYTE_SEQUENCE_NUMBER, NOT_IN_FEC_GROUP);
    854        ++i) {
    855     string expected_error;
    856     if (i < kConnectionIdOffset) {
    857       expected_error = "Unable to read public flags.";
    858     } else if (i < GetSequenceNumberOffset(PACKET_0BYTE_CONNECTION_ID,
    859                                            !kIncludeVersion)) {
    860       expected_error = "Unable to read ConnectionId.";
    861     } else if (i < GetPrivateFlagsOffset(PACKET_0BYTE_CONNECTION_ID,
    862                                          !kIncludeVersion)) {
    863       expected_error = "Unable to read sequence number.";
    864     } else if (i < GetFecGroupOffset(PACKET_0BYTE_CONNECTION_ID,
    865                                      !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, PacketHeaderWithVersionFlag) {
    875   unsigned char packet[] = {
    876     // public flags (version)
    877     0x3D,
    878     // connection_id
    879     0x10, 0x32, 0x54, 0x76,
    880     0x98, 0xBA, 0xDC, 0xFE,
    881     // version tag
    882     'Q', '0', GetQuicVersionDigitTens(), GetQuicVersionDigitOnes(),
    883     // packet sequence number
    884     0xBC, 0x9A, 0x78, 0x56,
    885     0x34, 0x12,
    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.connection_id);
    896   EXPECT_FALSE(visitor_.header_->public_header.reset_flag);
    897   EXPECT_TRUE(visitor_.header_->public_header.version_flag);
    898   EXPECT_EQ(GetParam(), visitor_.header_->public_header.versions[0]);
    899   EXPECT_FALSE(visitor_.header_->fec_flag);
    900   EXPECT_FALSE(visitor_.header_->entropy_flag);
    901   EXPECT_EQ(0, visitor_.header_->entropy_hash);
    902   EXPECT_EQ(GG_UINT64_C(0x123456789ABC),
    903             visitor_.header_->packet_sequence_number);
    904   EXPECT_EQ(NOT_IN_FEC_GROUP, visitor_.header_->is_in_fec_group);
    905   EXPECT_EQ(0x00u, visitor_.header_->fec_group);
    906 
    907   // Now test framing boundaries
    908   for (size_t i = 0;
    909        i < GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID, kIncludeVersion,
    910                                PACKET_6BYTE_SEQUENCE_NUMBER, NOT_IN_FEC_GROUP);
    911        ++i) {
    912     string expected_error;
    913     if (i < kConnectionIdOffset) {
    914       expected_error = "Unable to read public flags.";
    915     } else if (i < kVersionOffset) {
    916       expected_error = "Unable to read ConnectionId.";
    917     } else if (i <  GetSequenceNumberOffset(kIncludeVersion)) {
    918       expected_error = "Unable to read protocol version.";
    919     } else if (i < GetPrivateFlagsOffset(kIncludeVersion)) {
    920       expected_error = "Unable to read sequence number.";
    921     } else if (i < GetFecGroupOffset(kIncludeVersion)) {
    922       expected_error = "Unable to read private flags.";
    923     } else {
    924       expected_error = "Unable to read first fec protected packet offset.";
    925     }
    926     CheckProcessingFails(packet, i, expected_error, QUIC_INVALID_PACKET_HEADER);
    927   }
    928 }
    929 
    930 TEST_P(QuicFramerTest, PacketHeaderWith4ByteSequenceNumber) {
    931   QuicFramerPeer::SetLastSequenceNumber(&framer_,
    932                                         GG_UINT64_C(0x123456789ABA));
    933 
    934   unsigned char packet[] = {
    935     // public flags (8 byte connection_id and 4 byte sequence number)
    936     0x2C,
    937     // connection_id
    938     0x10, 0x32, 0x54, 0x76,
    939     0x98, 0xBA, 0xDC, 0xFE,
    940     // packet sequence number
    941     0xBC, 0x9A, 0x78, 0x56,
    942     // private flags
    943     0x00,
    944   };
    945 
    946   QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
    947   EXPECT_FALSE(framer_.ProcessPacket(encrypted));
    948   EXPECT_EQ(QUIC_MISSING_PAYLOAD, framer_.error());
    949   ASSERT_TRUE(visitor_.header_.get());
    950   EXPECT_EQ(GG_UINT64_C(0xFEDCBA9876543210),
    951             visitor_.header_->public_header.connection_id);
    952   EXPECT_FALSE(visitor_.header_->public_header.reset_flag);
    953   EXPECT_FALSE(visitor_.header_->public_header.version_flag);
    954   EXPECT_FALSE(visitor_.header_->fec_flag);
    955   EXPECT_FALSE(visitor_.header_->entropy_flag);
    956   EXPECT_EQ(0, visitor_.header_->entropy_hash);
    957   EXPECT_EQ(GG_UINT64_C(0x123456789ABC),
    958             visitor_.header_->packet_sequence_number);
    959   EXPECT_EQ(NOT_IN_FEC_GROUP, visitor_.header_->is_in_fec_group);
    960   EXPECT_EQ(0x00u, visitor_.header_->fec_group);
    961 
    962   // Now test framing boundaries
    963   for (size_t i = 0;
    964        i < GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID, !kIncludeVersion,
    965                                PACKET_4BYTE_SEQUENCE_NUMBER, NOT_IN_FEC_GROUP);
    966        ++i) {
    967     string expected_error;
    968     if (i < kConnectionIdOffset) {
    969       expected_error = "Unable to read public flags.";
    970     } else if (i < GetSequenceNumberOffset(!kIncludeVersion)) {
    971       expected_error = "Unable to read ConnectionId.";
    972     } else if (i < GetPrivateFlagsOffset(!kIncludeVersion,
    973                                          PACKET_4BYTE_SEQUENCE_NUMBER)) {
    974       expected_error = "Unable to read sequence number.";
    975     } else if (i < GetFecGroupOffset(!kIncludeVersion,
    976                                      PACKET_4BYTE_SEQUENCE_NUMBER)) {
    977       expected_error = "Unable to read private flags.";
    978     } else {
    979       expected_error = "Unable to read first fec protected packet offset.";
    980     }
    981     CheckProcessingFails(packet, i, expected_error, QUIC_INVALID_PACKET_HEADER);
    982   }
    983 }
    984 
    985 TEST_P(QuicFramerTest, PacketHeaderWith2ByteSequenceNumber) {
    986   QuicFramerPeer::SetLastSequenceNumber(&framer_,
    987                                         GG_UINT64_C(0x123456789ABA));
    988 
    989   unsigned char packet[] = {
    990     // public flags (8 byte connection_id and 2 byte sequence number)
    991     0x1C,
    992     // connection_id
    993     0x10, 0x32, 0x54, 0x76,
    994     0x98, 0xBA, 0xDC, 0xFE,
    995     // packet sequence number
    996     0xBC, 0x9A,
    997     // private flags
    998     0x00,
    999   };
   1000 
   1001   QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
   1002   EXPECT_FALSE(framer_.ProcessPacket(encrypted));
   1003   EXPECT_EQ(QUIC_MISSING_PAYLOAD, framer_.error());
   1004   ASSERT_TRUE(visitor_.header_.get());
   1005   EXPECT_EQ(GG_UINT64_C(0xFEDCBA9876543210),
   1006             visitor_.header_->public_header.connection_id);
   1007   EXPECT_FALSE(visitor_.header_->public_header.reset_flag);
   1008   EXPECT_FALSE(visitor_.header_->public_header.version_flag);
   1009   EXPECT_FALSE(visitor_.header_->fec_flag);
   1010   EXPECT_FALSE(visitor_.header_->entropy_flag);
   1011   EXPECT_EQ(0, visitor_.header_->entropy_hash);
   1012   EXPECT_EQ(GG_UINT64_C(0x123456789ABC),
   1013             visitor_.header_->packet_sequence_number);
   1014   EXPECT_EQ(NOT_IN_FEC_GROUP, visitor_.header_->is_in_fec_group);
   1015   EXPECT_EQ(0x00u, visitor_.header_->fec_group);
   1016 
   1017   // Now test framing boundaries
   1018   for (size_t i = 0;
   1019        i < GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID, !kIncludeVersion,
   1020                                PACKET_2BYTE_SEQUENCE_NUMBER, NOT_IN_FEC_GROUP);
   1021        ++i) {
   1022     string expected_error;
   1023     if (i < kConnectionIdOffset) {
   1024       expected_error = "Unable to read public flags.";
   1025     } else if (i < GetSequenceNumberOffset(!kIncludeVersion)) {
   1026       expected_error = "Unable to read ConnectionId.";
   1027     } else if (i < GetPrivateFlagsOffset(!kIncludeVersion,
   1028                                          PACKET_2BYTE_SEQUENCE_NUMBER)) {
   1029       expected_error = "Unable to read sequence number.";
   1030     } else if (i < GetFecGroupOffset(!kIncludeVersion,
   1031                                      PACKET_2BYTE_SEQUENCE_NUMBER)) {
   1032       expected_error = "Unable to read private flags.";
   1033     } else {
   1034       expected_error = "Unable to read first fec protected packet offset.";
   1035     }
   1036     CheckProcessingFails(packet, i, expected_error, QUIC_INVALID_PACKET_HEADER);
   1037   }
   1038 }
   1039 
   1040 TEST_P(QuicFramerTest, PacketHeaderWith1ByteSequenceNumber) {
   1041   QuicFramerPeer::SetLastSequenceNumber(&framer_,
   1042                                         GG_UINT64_C(0x123456789ABA));
   1043 
   1044   unsigned char packet[] = {
   1045     // public flags (8 byte connection_id and 1 byte sequence number)
   1046     0x0C,
   1047     // connection_id
   1048     0x10, 0x32, 0x54, 0x76,
   1049     0x98, 0xBA, 0xDC, 0xFE,
   1050     // packet sequence number
   1051     0xBC,
   1052     // private flags
   1053     0x00,
   1054   };
   1055 
   1056   QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
   1057   EXPECT_FALSE(framer_.ProcessPacket(encrypted));
   1058   EXPECT_EQ(QUIC_MISSING_PAYLOAD, framer_.error());
   1059   ASSERT_TRUE(visitor_.header_.get());
   1060   EXPECT_EQ(GG_UINT64_C(0xFEDCBA9876543210),
   1061             visitor_.header_->public_header.connection_id);
   1062   EXPECT_FALSE(visitor_.header_->public_header.reset_flag);
   1063   EXPECT_FALSE(visitor_.header_->public_header.version_flag);
   1064   EXPECT_FALSE(visitor_.header_->fec_flag);
   1065   EXPECT_FALSE(visitor_.header_->entropy_flag);
   1066   EXPECT_EQ(0, visitor_.header_->entropy_hash);
   1067   EXPECT_EQ(GG_UINT64_C(0x123456789ABC),
   1068             visitor_.header_->packet_sequence_number);
   1069   EXPECT_EQ(NOT_IN_FEC_GROUP, visitor_.header_->is_in_fec_group);
   1070   EXPECT_EQ(0x00u, visitor_.header_->fec_group);
   1071 
   1072   // Now test framing boundaries
   1073   for (size_t i = 0;
   1074        i < GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID, !kIncludeVersion,
   1075                                PACKET_1BYTE_SEQUENCE_NUMBER, NOT_IN_FEC_GROUP);
   1076        ++i) {
   1077     string expected_error;
   1078     if (i < kConnectionIdOffset) {
   1079       expected_error = "Unable to read public flags.";
   1080     } else if (i < GetSequenceNumberOffset(!kIncludeVersion)) {
   1081       expected_error = "Unable to read ConnectionId.";
   1082     } else if (i < GetPrivateFlagsOffset(!kIncludeVersion,
   1083                                          PACKET_1BYTE_SEQUENCE_NUMBER)) {
   1084       expected_error = "Unable to read sequence number.";
   1085     } else if (i < GetFecGroupOffset(!kIncludeVersion,
   1086                                      PACKET_1BYTE_SEQUENCE_NUMBER)) {
   1087       expected_error = "Unable to read private flags.";
   1088     } else {
   1089       expected_error = "Unable to read first fec protected packet offset.";
   1090     }
   1091     CheckProcessingFails(packet, i, expected_error, QUIC_INVALID_PACKET_HEADER);
   1092   }
   1093 }
   1094 
   1095 TEST_P(QuicFramerTest, InvalidPublicFlag) {
   1096   unsigned char packet[] = {
   1097     // public flags: all flags set but the public reset flag and version flag.
   1098     0xFC,
   1099     // connection_id
   1100     0x10, 0x32, 0x54, 0x76,
   1101     0x98, 0xBA, 0xDC, 0xFE,
   1102     // packet sequence number
   1103     0xBC, 0x9A, 0x78, 0x56,
   1104     0x34, 0x12,
   1105     // private flags
   1106     0x00,
   1107 
   1108     // frame type (padding)
   1109     0x00,
   1110     0x00, 0x00, 0x00, 0x00
   1111   };
   1112   CheckProcessingFails(packet,
   1113                        arraysize(packet),
   1114                        "Illegal public flags value.",
   1115                        QUIC_INVALID_PACKET_HEADER);
   1116 
   1117   // Now turn off validation.
   1118   framer_.set_validate_flags(false);
   1119   QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
   1120   EXPECT_TRUE(framer_.ProcessPacket(encrypted));
   1121 };
   1122 
   1123 TEST_P(QuicFramerTest, InvalidPublicFlagWithMatchingVersions) {
   1124   unsigned char packet[] = {
   1125     // public flags (8 byte connection_id and version flag and an unknown flag)
   1126     0x4D,
   1127     // connection_id
   1128     0x10, 0x32, 0x54, 0x76,
   1129     0x98, 0xBA, 0xDC, 0xFE,
   1130     // version tag
   1131     'Q', '0', GetQuicVersionDigitTens(), GetQuicVersionDigitOnes(),
   1132     // packet sequence number
   1133     0xBC, 0x9A, 0x78, 0x56,
   1134     0x34, 0x12,
   1135     // private flags
   1136     0x00,
   1137 
   1138     // frame type (padding)
   1139     0x00,
   1140     0x00, 0x00, 0x00, 0x00
   1141   };
   1142   CheckProcessingFails(packet,
   1143                        arraysize(packet),
   1144                        "Illegal public flags value.",
   1145                        QUIC_INVALID_PACKET_HEADER);
   1146 };
   1147 
   1148 TEST_P(QuicFramerTest, LargePublicFlagWithMismatchedVersions) {
   1149   unsigned char packet[] = {
   1150     // public flags (8 byte connection_id, version flag and an unknown flag)
   1151     0x7D,
   1152     // connection_id
   1153     0x10, 0x32, 0x54, 0x76,
   1154     0x98, 0xBA, 0xDC, 0xFE,
   1155     // version tag
   1156     'Q', '0', '0', '0',
   1157     // packet sequence number
   1158     0xBC, 0x9A, 0x78, 0x56,
   1159     0x34, 0x12,
   1160     // private flags
   1161     0x00,
   1162 
   1163     // frame type (padding frame)
   1164     0x00,
   1165     0x00, 0x00, 0x00, 0x00
   1166   };
   1167   QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
   1168   EXPECT_TRUE(framer_.ProcessPacket(encrypted));
   1169   EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
   1170   ASSERT_TRUE(visitor_.header_.get());
   1171   EXPECT_EQ(0, visitor_.frame_count_);
   1172   EXPECT_EQ(1, visitor_.version_mismatch_);
   1173 };
   1174 
   1175 TEST_P(QuicFramerTest, InvalidPrivateFlag) {
   1176   unsigned char packet[] = {
   1177     // public flags (8 byte connection_id)
   1178     0x3C,
   1179     // connection_id
   1180     0x10, 0x32, 0x54, 0x76,
   1181     0x98, 0xBA, 0xDC, 0xFE,
   1182     // packet sequence number
   1183     0xBC, 0x9A, 0x78, 0x56,
   1184     0x34, 0x12,
   1185     // private flags
   1186     0x10,
   1187 
   1188     // frame type (padding)
   1189     0x00,
   1190     0x00, 0x00, 0x00, 0x00
   1191   };
   1192   CheckProcessingFails(packet,
   1193                        arraysize(packet),
   1194                        "Illegal private flags value.",
   1195                        QUIC_INVALID_PACKET_HEADER);
   1196 };
   1197 
   1198 TEST_P(QuicFramerTest, InvalidFECGroupOffset) {
   1199   unsigned char packet[] = {
   1200     // public flags (8 byte connection_id)
   1201     0x3C,
   1202     // connection_id
   1203     0x10, 0x32, 0x54, 0x76,
   1204     0x98, 0xBA, 0xDC, 0xFE,
   1205     // packet sequence number
   1206     0x01, 0x00, 0x00, 0x00,
   1207     0x00, 0x00,
   1208     // private flags (fec group)
   1209     0x02,
   1210     // first fec protected packet offset
   1211     0x10
   1212   };
   1213   CheckProcessingFails(packet,
   1214                        arraysize(packet),
   1215                        "First fec protected packet offset must be less "
   1216                        "than the sequence number.",
   1217                        QUIC_INVALID_PACKET_HEADER);
   1218 };
   1219 
   1220 TEST_P(QuicFramerTest, PaddingFrame) {
   1221   unsigned char packet[] = {
   1222     // public flags (8 byte connection_id)
   1223     0x3C,
   1224     // connection_id
   1225     0x10, 0x32, 0x54, 0x76,
   1226     0x98, 0xBA, 0xDC, 0xFE,
   1227     // packet sequence number
   1228     0xBC, 0x9A, 0x78, 0x56,
   1229     0x34, 0x12,
   1230     // private flags
   1231     0x00,
   1232 
   1233     // frame type (padding frame)
   1234     0x00,
   1235     // Ignored data (which in this case is a stream frame)
   1236     // frame type (stream frame with fin)
   1237     0xFF,
   1238     // stream id
   1239     0x04, 0x03, 0x02, 0x01,
   1240     // offset
   1241     0x54, 0x76, 0x10, 0x32,
   1242     0xDC, 0xFE, 0x98, 0xBA,
   1243     // data length
   1244     0x0c, 0x00,
   1245     // data
   1246     'h',  'e',  'l',  'l',
   1247     'o',  ' ',  'w',  'o',
   1248     'r',  'l',  'd',  '!',
   1249   };
   1250 
   1251   QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
   1252   EXPECT_TRUE(framer_.ProcessPacket(encrypted));
   1253   EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
   1254   ASSERT_TRUE(visitor_.header_.get());
   1255   EXPECT_TRUE(CheckDecryption(encrypted, !kIncludeVersion));
   1256 
   1257   ASSERT_EQ(0u, visitor_.stream_frames_.size());
   1258   EXPECT_EQ(0u, visitor_.ack_frames_.size());
   1259   // A packet with no frames is not acceptable.
   1260   CheckProcessingFails(
   1261       packet,
   1262       GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID, !kIncludeVersion,
   1263                           PACKET_6BYTE_SEQUENCE_NUMBER, NOT_IN_FEC_GROUP),
   1264       "Packet has no frames.", QUIC_MISSING_PAYLOAD);
   1265 }
   1266 
   1267 TEST_P(QuicFramerTest, StreamFrame) {
   1268   unsigned char packet[] = {
   1269     // public flags (8 byte connection_id)
   1270     0x3C,
   1271     // connection_id
   1272     0x10, 0x32, 0x54, 0x76,
   1273     0x98, 0xBA, 0xDC, 0xFE,
   1274     // packet sequence number
   1275     0xBC, 0x9A, 0x78, 0x56,
   1276     0x34, 0x12,
   1277     // private flags
   1278     0x00,
   1279 
   1280     // frame type (stream frame with fin)
   1281     0xFF,
   1282     // stream id
   1283     0x04, 0x03, 0x02, 0x01,
   1284     // offset
   1285     0x54, 0x76, 0x10, 0x32,
   1286     0xDC, 0xFE, 0x98, 0xBA,
   1287     // data length
   1288     0x0c, 0x00,
   1289     // data
   1290     'h',  'e',  'l',  'l',
   1291     'o',  ' ',  'w',  'o',
   1292     'r',  'l',  'd',  '!',
   1293   };
   1294 
   1295   QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
   1296   EXPECT_TRUE(framer_.ProcessPacket(encrypted));
   1297 
   1298   EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
   1299   ASSERT_TRUE(visitor_.header_.get());
   1300   EXPECT_TRUE(CheckDecryption(encrypted, !kIncludeVersion));
   1301 
   1302   ASSERT_EQ(1u, visitor_.stream_frames_.size());
   1303   EXPECT_EQ(0u, visitor_.ack_frames_.size());
   1304   EXPECT_EQ(static_cast<uint64>(0x01020304),
   1305             visitor_.stream_frames_[0]->stream_id);
   1306   EXPECT_TRUE(visitor_.stream_frames_[0]->fin);
   1307   EXPECT_EQ(GG_UINT64_C(0xBA98FEDC32107654),
   1308             visitor_.stream_frames_[0]->offset);
   1309   CheckStreamFrameData("hello world!", visitor_.stream_frames_[0]);
   1310 
   1311   // Now test framing boundaries
   1312   CheckStreamFrameBoundaries(packet, kQuicMaxStreamIdSize, !kIncludeVersion);
   1313 }
   1314 
   1315 TEST_P(QuicFramerTest, StreamFrame3ByteStreamId) {
   1316   unsigned char packet[] = {
   1317     // public flags (8 byte connection_id)
   1318     0x3C,
   1319     // connection_id
   1320     0x10, 0x32, 0x54, 0x76,
   1321     0x98, 0xBA, 0xDC, 0xFE,
   1322     // packet sequence number
   1323     0xBC, 0x9A, 0x78, 0x56,
   1324     0x34, 0x12,
   1325     // private flags
   1326     0x00,
   1327 
   1328     // frame type (stream frame with fin)
   1329     0xFE,
   1330     // stream id
   1331     0x04, 0x03, 0x02,
   1332     // offset
   1333     0x54, 0x76, 0x10, 0x32,
   1334     0xDC, 0xFE, 0x98, 0xBA,
   1335     // data length
   1336     0x0c, 0x00,
   1337     // data
   1338     'h',  'e',  'l',  'l',
   1339     'o',  ' ',  'w',  'o',
   1340     'r',  'l',  'd',  '!',
   1341   };
   1342 
   1343   QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
   1344   EXPECT_TRUE(framer_.ProcessPacket(encrypted));
   1345 
   1346   EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
   1347   ASSERT_TRUE(visitor_.header_.get());
   1348   EXPECT_TRUE(CheckDecryption(encrypted, !kIncludeVersion));
   1349 
   1350   ASSERT_EQ(1u, visitor_.stream_frames_.size());
   1351   EXPECT_EQ(0u, visitor_.ack_frames_.size());
   1352   EXPECT_EQ(GG_UINT64_C(0x00020304),
   1353             visitor_.stream_frames_[0]->stream_id);
   1354   EXPECT_TRUE(visitor_.stream_frames_[0]->fin);
   1355   EXPECT_EQ(GG_UINT64_C(0xBA98FEDC32107654),
   1356             visitor_.stream_frames_[0]->offset);
   1357   CheckStreamFrameData("hello world!", visitor_.stream_frames_[0]);
   1358 
   1359   // Now test framing boundaries
   1360   const size_t stream_id_size = 3;
   1361   CheckStreamFrameBoundaries(packet, stream_id_size, !kIncludeVersion);
   1362 }
   1363 
   1364 TEST_P(QuicFramerTest, StreamFrame2ByteStreamId) {
   1365   unsigned char packet[] = {
   1366     // public flags (8 byte connection_id)
   1367     0x3C,
   1368     // connection_id
   1369     0x10, 0x32, 0x54, 0x76,
   1370     0x98, 0xBA, 0xDC, 0xFE,
   1371     // packet sequence number
   1372     0xBC, 0x9A, 0x78, 0x56,
   1373     0x34, 0x12,
   1374     // private flags
   1375     0x00,
   1376 
   1377     // frame type (stream frame with fin)
   1378     0xFD,
   1379     // stream id
   1380     0x04, 0x03,
   1381     // offset
   1382     0x54, 0x76, 0x10, 0x32,
   1383     0xDC, 0xFE, 0x98, 0xBA,
   1384     // data length
   1385     0x0c, 0x00,
   1386     // data
   1387     'h',  'e',  'l',  'l',
   1388     'o',  ' ',  'w',  'o',
   1389     'r',  'l',  'd',  '!',
   1390   };
   1391 
   1392   QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
   1393   EXPECT_TRUE(framer_.ProcessPacket(encrypted));
   1394 
   1395   EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
   1396   ASSERT_TRUE(visitor_.header_.get());
   1397   EXPECT_TRUE(CheckDecryption(encrypted, !kIncludeVersion));
   1398 
   1399   ASSERT_EQ(1u, visitor_.stream_frames_.size());
   1400   EXPECT_EQ(0u, visitor_.ack_frames_.size());
   1401   EXPECT_EQ(static_cast<uint64>(0x00000304),
   1402             visitor_.stream_frames_[0]->stream_id);
   1403   EXPECT_TRUE(visitor_.stream_frames_[0]->fin);
   1404   EXPECT_EQ(GG_UINT64_C(0xBA98FEDC32107654),
   1405             visitor_.stream_frames_[0]->offset);
   1406   CheckStreamFrameData("hello world!", visitor_.stream_frames_[0]);
   1407 
   1408   // Now test framing boundaries
   1409   const size_t stream_id_size = 2;
   1410   CheckStreamFrameBoundaries(packet, stream_id_size, !kIncludeVersion);
   1411 }
   1412 
   1413 TEST_P(QuicFramerTest, StreamFrame1ByteStreamId) {
   1414   unsigned char packet[] = {
   1415     // public flags (8 byte connection_id)
   1416     0x3C,
   1417     // connection_id
   1418     0x10, 0x32, 0x54, 0x76,
   1419     0x98, 0xBA, 0xDC, 0xFE,
   1420     // packet sequence number
   1421     0xBC, 0x9A, 0x78, 0x56,
   1422     0x34, 0x12,
   1423     // private flags
   1424     0x00,
   1425 
   1426     // frame type (stream frame with fin)
   1427     0xFC,
   1428     // stream id
   1429     0x04,
   1430     // offset
   1431     0x54, 0x76, 0x10, 0x32,
   1432     0xDC, 0xFE, 0x98, 0xBA,
   1433     // data length
   1434     0x0c, 0x00,
   1435     // data
   1436     'h',  'e',  'l',  'l',
   1437     'o',  ' ',  'w',  'o',
   1438     'r',  'l',  'd',  '!',
   1439   };
   1440 
   1441   QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
   1442   EXPECT_TRUE(framer_.ProcessPacket(encrypted));
   1443 
   1444   EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
   1445   ASSERT_TRUE(visitor_.header_.get());
   1446   EXPECT_TRUE(CheckDecryption(encrypted, !kIncludeVersion));
   1447 
   1448   ASSERT_EQ(1u, visitor_.stream_frames_.size());
   1449   EXPECT_EQ(0u, visitor_.ack_frames_.size());
   1450   EXPECT_EQ(static_cast<uint64>(0x00000004),
   1451             visitor_.stream_frames_[0]->stream_id);
   1452   EXPECT_TRUE(visitor_.stream_frames_[0]->fin);
   1453   EXPECT_EQ(GG_UINT64_C(0xBA98FEDC32107654),
   1454             visitor_.stream_frames_[0]->offset);
   1455   CheckStreamFrameData("hello world!", visitor_.stream_frames_[0]);
   1456 
   1457   // Now test framing boundaries
   1458   const size_t stream_id_size = 1;
   1459   CheckStreamFrameBoundaries(packet, stream_id_size, !kIncludeVersion);
   1460 }
   1461 
   1462 TEST_P(QuicFramerTest, StreamFrameWithVersion) {
   1463   unsigned char packet[] = {
   1464     // public flags (version, 8 byte connection_id)
   1465     0x3D,
   1466     // connection_id
   1467     0x10, 0x32, 0x54, 0x76,
   1468     0x98, 0xBA, 0xDC, 0xFE,
   1469     // version tag
   1470     'Q', '0', GetQuicVersionDigitTens(), GetQuicVersionDigitOnes(),
   1471     // packet sequence number
   1472     0xBC, 0x9A, 0x78, 0x56,
   1473     0x34, 0x12,
   1474     // private flags
   1475     0x00,
   1476 
   1477     // frame type (stream frame with fin)
   1478     0xFF,
   1479     // stream id
   1480     0x04, 0x03, 0x02, 0x01,
   1481     // offset
   1482     0x54, 0x76, 0x10, 0x32,
   1483     0xDC, 0xFE, 0x98, 0xBA,
   1484     // data length
   1485     0x0c, 0x00,
   1486     // data
   1487     'h',  'e',  'l',  'l',
   1488     'o',  ' ',  'w',  'o',
   1489     'r',  'l',  'd',  '!',
   1490   };
   1491 
   1492   QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
   1493   EXPECT_TRUE(framer_.ProcessPacket(encrypted));
   1494 
   1495   EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
   1496   ASSERT_TRUE(visitor_.header_.get());
   1497   EXPECT_TRUE(visitor_.header_->public_header.version_flag);
   1498   EXPECT_EQ(GetParam(), visitor_.header_->public_header.versions[0]);
   1499   EXPECT_TRUE(CheckDecryption(encrypted, kIncludeVersion));
   1500 
   1501   ASSERT_EQ(1u, visitor_.stream_frames_.size());
   1502   EXPECT_EQ(0u, visitor_.ack_frames_.size());
   1503   EXPECT_EQ(static_cast<uint64>(0x01020304),
   1504             visitor_.stream_frames_[0]->stream_id);
   1505   EXPECT_TRUE(visitor_.stream_frames_[0]->fin);
   1506   EXPECT_EQ(GG_UINT64_C(0xBA98FEDC32107654),
   1507             visitor_.stream_frames_[0]->offset);
   1508   CheckStreamFrameData("hello world!", visitor_.stream_frames_[0]);
   1509 
   1510   // Now test framing boundaries
   1511   CheckStreamFrameBoundaries(packet, kQuicMaxStreamIdSize, kIncludeVersion);
   1512 }
   1513 
   1514 TEST_P(QuicFramerTest, RejectPacket) {
   1515   visitor_.accept_packet_ = false;
   1516 
   1517   unsigned char packet[] = {
   1518     // public flags (8 byte connection_id)
   1519     0x3C,
   1520     // connection_id
   1521     0x10, 0x32, 0x54, 0x76,
   1522     0x98, 0xBA, 0xDC, 0xFE,
   1523     // packet sequence number
   1524     0xBC, 0x9A, 0x78, 0x56,
   1525     0x34, 0x12,
   1526     // private flags
   1527     0x00,
   1528 
   1529     // frame type (stream frame with fin)
   1530     0xFF,
   1531     // stream id
   1532     0x04, 0x03, 0x02, 0x01,
   1533     // offset
   1534     0x54, 0x76, 0x10, 0x32,
   1535     0xDC, 0xFE, 0x98, 0xBA,
   1536     // data length
   1537     0x0c, 0x00,
   1538     // data
   1539     'h',  'e',  'l',  'l',
   1540     'o',  ' ',  'w',  'o',
   1541     'r',  'l',  'd',  '!',
   1542   };
   1543 
   1544   QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
   1545   EXPECT_TRUE(framer_.ProcessPacket(encrypted));
   1546 
   1547   EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
   1548   ASSERT_TRUE(visitor_.header_.get());
   1549   EXPECT_TRUE(CheckDecryption(encrypted, !kIncludeVersion));
   1550 
   1551   ASSERT_EQ(0u, visitor_.stream_frames_.size());
   1552   EXPECT_EQ(0u, visitor_.ack_frames_.size());
   1553 }
   1554 
   1555 TEST_P(QuicFramerTest, RejectPublicHeader) {
   1556   visitor_.accept_public_header_ = false;
   1557 
   1558   unsigned char packet[] = {
   1559     // public flags (8 byte connection_id)
   1560     0x3C,
   1561     // connection_id
   1562     0x10, 0x32, 0x54, 0x76,
   1563     0x98, 0xBA, 0xDC, 0xFE,
   1564   };
   1565 
   1566   QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
   1567   EXPECT_TRUE(framer_.ProcessPacket(encrypted));
   1568 
   1569   EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
   1570   ASSERT_TRUE(visitor_.public_header_.get());
   1571   ASSERT_FALSE(visitor_.header_.get());
   1572 }
   1573 
   1574 TEST_P(QuicFramerTest, RevivedStreamFrame) {
   1575   unsigned char payload[] = {
   1576     // frame type (stream frame with fin)
   1577     0xFF,
   1578     // stream id
   1579     0x04, 0x03, 0x02, 0x01,
   1580     // offset
   1581     0x54, 0x76, 0x10, 0x32,
   1582     0xDC, 0xFE, 0x98, 0xBA,
   1583     // data length
   1584     0x0c, 0x00,
   1585     // data
   1586     'h',  'e',  'l',  'l',
   1587     'o',  ' ',  'w',  'o',
   1588     'r',  'l',  'd',  '!',
   1589   };
   1590 
   1591   QuicPacketHeader header;
   1592   header.public_header.connection_id = GG_UINT64_C(0xFEDCBA9876543210);
   1593   header.public_header.reset_flag = false;
   1594   header.public_header.version_flag = false;
   1595   header.fec_flag = true;
   1596   header.entropy_flag = true;
   1597   header.packet_sequence_number = GG_UINT64_C(0x123456789ABC);
   1598   header.fec_group = 0;
   1599 
   1600   // Do not encrypt the payload because the revived payload is post-encryption.
   1601   EXPECT_TRUE(framer_.ProcessRevivedPacket(&header,
   1602                                            StringPiece(AsChars(payload),
   1603                                                        arraysize(payload))));
   1604 
   1605   EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
   1606   ASSERT_EQ(1, visitor_.revived_packets_);
   1607   ASSERT_TRUE(visitor_.header_.get());
   1608   EXPECT_EQ(GG_UINT64_C(0xFEDCBA9876543210),
   1609             visitor_.header_->public_header.connection_id);
   1610   EXPECT_FALSE(visitor_.header_->public_header.reset_flag);
   1611   EXPECT_FALSE(visitor_.header_->public_header.version_flag);
   1612   EXPECT_TRUE(visitor_.header_->fec_flag);
   1613   EXPECT_TRUE(visitor_.header_->entropy_flag);
   1614   EXPECT_EQ(1 << (header.packet_sequence_number % 8),
   1615             visitor_.header_->entropy_hash);
   1616   EXPECT_EQ(GG_UINT64_C(0x123456789ABC),
   1617             visitor_.header_->packet_sequence_number);
   1618   EXPECT_EQ(NOT_IN_FEC_GROUP, visitor_.header_->is_in_fec_group);
   1619   EXPECT_EQ(0x00u, visitor_.header_->fec_group);
   1620 
   1621   ASSERT_EQ(1u, visitor_.stream_frames_.size());
   1622   EXPECT_EQ(0u, visitor_.ack_frames_.size());
   1623   EXPECT_EQ(GG_UINT64_C(0x01020304), visitor_.stream_frames_[0]->stream_id);
   1624   EXPECT_TRUE(visitor_.stream_frames_[0]->fin);
   1625   EXPECT_EQ(GG_UINT64_C(0xBA98FEDC32107654),
   1626             visitor_.stream_frames_[0]->offset);
   1627   CheckStreamFrameData("hello world!", visitor_.stream_frames_[0]);
   1628 }
   1629 
   1630 TEST_P(QuicFramerTest, StreamFrameInFecGroup) {
   1631   unsigned char packet[] = {
   1632     // public flags (8 byte connection_id)
   1633     0x3C,
   1634     // connection_id
   1635     0x10, 0x32, 0x54, 0x76,
   1636     0x98, 0xBA, 0xDC, 0xFE,
   1637     // packet sequence number
   1638     0xBC, 0x9A, 0x78, 0x56,
   1639     0x12, 0x34,
   1640     // private flags (fec group)
   1641     0x02,
   1642     // first fec protected packet offset
   1643     0x02,
   1644 
   1645     // frame type (stream frame with fin)
   1646     0xFF,
   1647     // stream id
   1648     0x04, 0x03, 0x02, 0x01,
   1649     // offset
   1650     0x54, 0x76, 0x10, 0x32,
   1651     0xDC, 0xFE, 0x98, 0xBA,
   1652     // data length
   1653     0x0c, 0x00,
   1654     // data
   1655     'h',  'e',  'l',  'l',
   1656     'o',  ' ',  'w',  'o',
   1657     'r',  'l',  'd',  '!',
   1658   };
   1659 
   1660   QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
   1661   EXPECT_TRUE(framer_.ProcessPacket(encrypted));
   1662 
   1663   EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
   1664   ASSERT_TRUE(visitor_.header_.get());
   1665   EXPECT_TRUE(CheckDecryption(encrypted, !kIncludeVersion));
   1666   EXPECT_EQ(IN_FEC_GROUP, visitor_.header_->is_in_fec_group);
   1667   EXPECT_EQ(GG_UINT64_C(0x341256789ABA),
   1668             visitor_.header_->fec_group);
   1669   const size_t fec_offset =
   1670       GetStartOfFecProtectedData(PACKET_8BYTE_CONNECTION_ID,
   1671                                  !kIncludeVersion,
   1672                                  PACKET_6BYTE_SEQUENCE_NUMBER);
   1673   EXPECT_EQ(
   1674       string(AsChars(packet) + fec_offset, arraysize(packet) - fec_offset),
   1675       visitor_.fec_protected_payload_);
   1676 
   1677   ASSERT_EQ(1u, visitor_.stream_frames_.size());
   1678   EXPECT_EQ(0u, visitor_.ack_frames_.size());
   1679   EXPECT_EQ(GG_UINT64_C(0x01020304), visitor_.stream_frames_[0]->stream_id);
   1680   EXPECT_TRUE(visitor_.stream_frames_[0]->fin);
   1681   EXPECT_EQ(GG_UINT64_C(0xBA98FEDC32107654),
   1682             visitor_.stream_frames_[0]->offset);
   1683   CheckStreamFrameData("hello world!", visitor_.stream_frames_[0]);
   1684 }
   1685 
   1686 TEST_P(QuicFramerTest, AckFrame15) {
   1687   if (framer_.version() != QUIC_VERSION_15) {
   1688     return;
   1689   }
   1690 
   1691   unsigned char packet[] = {
   1692     // public flags (8 byte connection_id)
   1693     0x3C,
   1694     // connection_id
   1695     0x10, 0x32, 0x54, 0x76,
   1696     0x98, 0xBA, 0xDC, 0xFE,
   1697     // packet sequence number
   1698     0xA8, 0x9A, 0x78, 0x56,
   1699     0x34, 0x12,
   1700     // private flags (entropy)
   1701     0x01,
   1702 
   1703     // frame type (ack frame)
   1704     // (has nacks, not truncated, 6 byte largest observed, 1 byte delta)
   1705     0x6C,
   1706     // entropy hash of sent packets till least awaiting - 1.
   1707     0xAB,
   1708     // least packet sequence number awaiting an ack, delta from sequence number.
   1709     0x08, 0x00, 0x00, 0x00,
   1710     0x00, 0x00,
   1711     // entropy hash of all received packets.
   1712     0xBA,
   1713     // largest observed packet sequence number
   1714     0xBF, 0x9A, 0x78, 0x56,
   1715     0x34, 0x12,
   1716     // Zero delta time.
   1717     0x0, 0x0,
   1718     // num missing packets
   1719     0x01,
   1720     // missing packet delta
   1721     0x01,
   1722     // 0 more missing packets in range.
   1723     0x00,
   1724     // Number of revived packets.
   1725     0x00,
   1726   };
   1727 
   1728   QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
   1729   EXPECT_TRUE(framer_.ProcessPacket(encrypted));
   1730 
   1731   EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
   1732   ASSERT_TRUE(visitor_.header_.get());
   1733   EXPECT_TRUE(CheckDecryption(encrypted, !kIncludeVersion));
   1734 
   1735   EXPECT_EQ(0u, visitor_.stream_frames_.size());
   1736   ASSERT_EQ(1u, visitor_.ack_frames_.size());
   1737   const QuicAckFrame& frame = *visitor_.ack_frames_[0];
   1738   EXPECT_EQ(0xAB, frame.sent_info.entropy_hash);
   1739   EXPECT_EQ(0xBA, frame.received_info.entropy_hash);
   1740   EXPECT_EQ(GG_UINT64_C(0x0123456789ABF), frame.received_info.largest_observed);
   1741   ASSERT_EQ(1u, frame.received_info.missing_packets.size());
   1742   SequenceNumberSet::const_iterator missing_iter =
   1743       frame.received_info.missing_packets.begin();
   1744   EXPECT_EQ(GG_UINT64_C(0x0123456789ABE), *missing_iter);
   1745   EXPECT_EQ(GG_UINT64_C(0x0123456789AA0), frame.sent_info.least_unacked);
   1746 
   1747   const size_t kSentEntropyOffset = kQuicFrameTypeSize;
   1748   const size_t kLeastUnackedOffset = kSentEntropyOffset + kQuicEntropyHashSize;
   1749   const size_t kReceivedEntropyOffset = kLeastUnackedOffset +
   1750       PACKET_6BYTE_SEQUENCE_NUMBER;
   1751   const size_t kLargestObservedOffset = kReceivedEntropyOffset +
   1752       kQuicEntropyHashSize;
   1753   const size_t kMissingDeltaTimeOffset = kLargestObservedOffset +
   1754       PACKET_6BYTE_SEQUENCE_NUMBER;
   1755   const size_t kNumMissingPacketOffset = kMissingDeltaTimeOffset +
   1756       kQuicDeltaTimeLargestObservedSize;
   1757   const size_t kMissingPacketsOffset = kNumMissingPacketOffset +
   1758       kNumberOfNackRangesSize;
   1759   const size_t kMissingPacketsRange = kMissingPacketsOffset +
   1760       PACKET_1BYTE_SEQUENCE_NUMBER;
   1761   const size_t kRevivedPacketsLength = kMissingPacketsRange +
   1762       PACKET_1BYTE_SEQUENCE_NUMBER;
   1763   // Now test framing boundaries
   1764   const size_t ack_frame_size = kRevivedPacketsLength +
   1765       PACKET_1BYTE_SEQUENCE_NUMBER;
   1766   for (size_t i = kQuicFrameTypeSize; i < ack_frame_size; ++i) {
   1767     string expected_error;
   1768     if (i < kLeastUnackedOffset) {
   1769       expected_error = "Unable to read entropy hash for sent packets.";
   1770     } else if (i < kReceivedEntropyOffset) {
   1771       expected_error = "Unable to read least unacked delta.";
   1772     } else if (i < kLargestObservedOffset) {
   1773       expected_error = "Unable to read entropy hash for received packets.";
   1774     } else if (i < kMissingDeltaTimeOffset) {
   1775       expected_error = "Unable to read largest observed.";
   1776     } else if (i < kNumMissingPacketOffset) {
   1777       expected_error = "Unable to read delta time largest observed.";
   1778     } else if (i < kMissingPacketsOffset) {
   1779       expected_error = "Unable to read num missing packet ranges.";
   1780     } else if (i < kMissingPacketsRange) {
   1781       expected_error = "Unable to read missing sequence number delta.";
   1782     } else if (i < kRevivedPacketsLength) {
   1783       expected_error = "Unable to read missing sequence number range.";
   1784     } else {
   1785       expected_error = "Unable to read num revived packets.";
   1786     }
   1787     CheckProcessingFails(
   1788         packet,
   1789         i + GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID, !kIncludeVersion,
   1790                                 PACKET_6BYTE_SEQUENCE_NUMBER, NOT_IN_FEC_GROUP),
   1791         expected_error, QUIC_INVALID_ACK_DATA);
   1792   }
   1793 }
   1794 
   1795 TEST_P(QuicFramerTest, AckFrame) {
   1796   if (framer_.version() <= QUIC_VERSION_15) {
   1797     return;
   1798   }
   1799 
   1800   unsigned char packet[] = {
   1801     // public flags (8 byte connection_id)
   1802     0x3C,
   1803     // connection_id
   1804     0x10, 0x32, 0x54, 0x76,
   1805     0x98, 0xBA, 0xDC, 0xFE,
   1806     // packet sequence number
   1807     0xA8, 0x9A, 0x78, 0x56,
   1808     0x34, 0x12,
   1809     // private flags (entropy)
   1810     0x01,
   1811 
   1812     // frame type (ack frame)
   1813     // (has nacks, not truncated, 6 byte largest observed, 1 byte delta)
   1814     0x6C,
   1815     // entropy hash of all received packets.
   1816     0xBA,
   1817     // largest observed packet sequence number
   1818     0xBF, 0x9A, 0x78, 0x56,
   1819     0x34, 0x12,
   1820     // Zero delta time.
   1821     0x0, 0x0,
   1822     // num missing packets
   1823     0x01,
   1824     // missing packet delta
   1825     0x01,
   1826     // 0 more missing packets in range.
   1827     0x00,
   1828     // Number of revived packets.
   1829     0x00,
   1830   };
   1831 
   1832   QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
   1833   EXPECT_TRUE(framer_.ProcessPacket(encrypted));
   1834 
   1835   EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
   1836   ASSERT_TRUE(visitor_.header_.get());
   1837   EXPECT_TRUE(CheckDecryption(encrypted, !kIncludeVersion));
   1838 
   1839   EXPECT_EQ(0u, visitor_.stream_frames_.size());
   1840   ASSERT_EQ(1u, visitor_.ack_frames_.size());
   1841   const QuicAckFrame& frame = *visitor_.ack_frames_[0];
   1842   EXPECT_EQ(0xBA, frame.received_info.entropy_hash);
   1843   EXPECT_EQ(GG_UINT64_C(0x0123456789ABF), frame.received_info.largest_observed);
   1844   ASSERT_EQ(1u, frame.received_info.missing_packets.size());
   1845   SequenceNumberSet::const_iterator missing_iter =
   1846       frame.received_info.missing_packets.begin();
   1847   EXPECT_EQ(GG_UINT64_C(0x0123456789ABE), *missing_iter);
   1848 
   1849   const size_t kReceivedEntropyOffset = kQuicFrameTypeSize;
   1850   const size_t kLargestObservedOffset = kReceivedEntropyOffset +
   1851       kQuicEntropyHashSize;
   1852   const size_t kMissingDeltaTimeOffset = kLargestObservedOffset +
   1853       PACKET_6BYTE_SEQUENCE_NUMBER;
   1854   const size_t kNumMissingPacketOffset = kMissingDeltaTimeOffset +
   1855       kQuicDeltaTimeLargestObservedSize;
   1856   const size_t kMissingPacketsOffset = kNumMissingPacketOffset +
   1857       kNumberOfNackRangesSize;
   1858   const size_t kMissingPacketsRange = kMissingPacketsOffset +
   1859       PACKET_1BYTE_SEQUENCE_NUMBER;
   1860   const size_t kRevivedPacketsLength = kMissingPacketsRange +
   1861       PACKET_1BYTE_SEQUENCE_NUMBER;
   1862   // Now test framing boundaries
   1863   const size_t ack_frame_size = kRevivedPacketsLength +
   1864       PACKET_1BYTE_SEQUENCE_NUMBER;
   1865   for (size_t i = kQuicFrameTypeSize; i < ack_frame_size; ++i) {
   1866     string expected_error;
   1867     if (i < kLargestObservedOffset) {
   1868       expected_error = "Unable to read entropy hash for received packets.";
   1869     } else if (i < kMissingDeltaTimeOffset) {
   1870       expected_error = "Unable to read largest observed.";
   1871     } else if (i < kNumMissingPacketOffset) {
   1872       expected_error = "Unable to read delta time largest observed.";
   1873     } else if (i < kMissingPacketsOffset) {
   1874       expected_error = "Unable to read num missing packet ranges.";
   1875     } else if (i < kMissingPacketsRange) {
   1876       expected_error = "Unable to read missing sequence number delta.";
   1877     } else if (i < kRevivedPacketsLength) {
   1878       expected_error = "Unable to read missing sequence number range.";
   1879     } else {
   1880       expected_error = "Unable to read num revived packets.";
   1881     }
   1882     CheckProcessingFails(
   1883         packet,
   1884         i + GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID, !kIncludeVersion,
   1885                                 PACKET_6BYTE_SEQUENCE_NUMBER, NOT_IN_FEC_GROUP),
   1886         expected_error, QUIC_INVALID_ACK_DATA);
   1887   }
   1888 }
   1889 
   1890 TEST_P(QuicFramerTest, AckFrameRevivedPackets) {
   1891   if (framer_.version() <= QUIC_VERSION_15) {
   1892     return;
   1893   }
   1894 
   1895   unsigned char packet[] = {
   1896     // public flags (8 byte connection_id)
   1897     0x3C,
   1898     // connection_id
   1899     0x10, 0x32, 0x54, 0x76,
   1900     0x98, 0xBA, 0xDC, 0xFE,
   1901     // packet sequence number
   1902     0xA8, 0x9A, 0x78, 0x56,
   1903     0x34, 0x12,
   1904     // private flags (entropy)
   1905     0x01,
   1906 
   1907     // frame type (ack frame)
   1908     // (has nacks, not truncated, 6 byte largest observed, 1 byte delta)
   1909     0x6C,
   1910     // entropy hash of all received packets.
   1911     0xBA,
   1912     // largest observed packet sequence number
   1913     0xBF, 0x9A, 0x78, 0x56,
   1914     0x34, 0x12,
   1915     // Zero delta time.
   1916     0x0, 0x0,
   1917     // num missing packets
   1918     0x01,
   1919     // missing packet delta
   1920     0x01,
   1921     // 0 more missing packets in range.
   1922     0x00,
   1923     // Number of revived packets.
   1924     0x01,
   1925     // Revived packet sequence number.
   1926     0xBE, 0x9A, 0x78, 0x56,
   1927     0x34, 0x12,
   1928   };
   1929 
   1930   QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
   1931   EXPECT_TRUE(framer_.ProcessPacket(encrypted));
   1932 
   1933   EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
   1934   ASSERT_TRUE(visitor_.header_.get());
   1935   EXPECT_TRUE(CheckDecryption(encrypted, !kIncludeVersion));
   1936 
   1937   EXPECT_EQ(0u, visitor_.stream_frames_.size());
   1938   ASSERT_EQ(1u, visitor_.ack_frames_.size());
   1939   const QuicAckFrame& frame = *visitor_.ack_frames_[0];
   1940   EXPECT_EQ(0xBA, frame.received_info.entropy_hash);
   1941   EXPECT_EQ(GG_UINT64_C(0x0123456789ABF), frame.received_info.largest_observed);
   1942   ASSERT_EQ(1u, frame.received_info.missing_packets.size());
   1943   SequenceNumberSet::const_iterator missing_iter =
   1944       frame.received_info.missing_packets.begin();
   1945   EXPECT_EQ(GG_UINT64_C(0x0123456789ABE), *missing_iter);
   1946 
   1947   const size_t kReceivedEntropyOffset = kQuicFrameTypeSize;
   1948   const size_t kLargestObservedOffset = kReceivedEntropyOffset +
   1949       kQuicEntropyHashSize;
   1950   const size_t kMissingDeltaTimeOffset = kLargestObservedOffset +
   1951       PACKET_6BYTE_SEQUENCE_NUMBER;
   1952   const size_t kNumMissingPacketOffset = kMissingDeltaTimeOffset +
   1953       kQuicDeltaTimeLargestObservedSize;
   1954   const size_t kMissingPacketsOffset = kNumMissingPacketOffset +
   1955       kNumberOfNackRangesSize;
   1956   const size_t kMissingPacketsRange = kMissingPacketsOffset +
   1957       PACKET_1BYTE_SEQUENCE_NUMBER;
   1958   const size_t kRevivedPacketsLength = kMissingPacketsRange +
   1959       PACKET_1BYTE_SEQUENCE_NUMBER;
   1960   const size_t kRevivedPacketSequenceNumberLength = kRevivedPacketsLength +
   1961       PACKET_1BYTE_SEQUENCE_NUMBER;
   1962   // Now test framing boundaries
   1963   const size_t ack_frame_size = kRevivedPacketSequenceNumberLength +
   1964       PACKET_6BYTE_SEQUENCE_NUMBER;
   1965   for (size_t i = kQuicFrameTypeSize; i < ack_frame_size; ++i) {
   1966     string expected_error;
   1967     if (i < kReceivedEntropyOffset) {
   1968       expected_error = "Unable to read least unacked delta.";
   1969     } else if (i < kLargestObservedOffset) {
   1970       expected_error = "Unable to read entropy hash for received packets.";
   1971     } else if (i < kMissingDeltaTimeOffset) {
   1972       expected_error = "Unable to read largest observed.";
   1973     } else if (i < kNumMissingPacketOffset) {
   1974       expected_error = "Unable to read delta time largest observed.";
   1975     } else if (i < kMissingPacketsOffset) {
   1976       expected_error = "Unable to read num missing packet ranges.";
   1977     } else if (i < kMissingPacketsRange) {
   1978       expected_error = "Unable to read missing sequence number delta.";
   1979     } else if (i < kRevivedPacketsLength) {
   1980       expected_error = "Unable to read missing sequence number range.";
   1981     } else if (i < kRevivedPacketSequenceNumberLength) {
   1982       expected_error = "Unable to read num revived packets.";
   1983     } else {
   1984       expected_error = "Unable to read revived packet.";
   1985     }
   1986     CheckProcessingFails(
   1987         packet,
   1988         i + GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID, !kIncludeVersion,
   1989                                 PACKET_6BYTE_SEQUENCE_NUMBER, NOT_IN_FEC_GROUP),
   1990         expected_error, QUIC_INVALID_ACK_DATA);
   1991   }
   1992 }
   1993 
   1994 TEST_P(QuicFramerTest, AckFrameRevivedPackets15) {
   1995   if (framer_.version() != QUIC_VERSION_15) {
   1996     return;
   1997   }
   1998 
   1999   unsigned char packet[] = {
   2000     // public flags (8 byte connection_id)
   2001     0x3C,
   2002     // connection_id
   2003     0x10, 0x32, 0x54, 0x76,
   2004     0x98, 0xBA, 0xDC, 0xFE,
   2005     // packet sequence number
   2006     0xA8, 0x9A, 0x78, 0x56,
   2007     0x34, 0x12,
   2008     // private flags (entropy)
   2009     0x01,
   2010 
   2011     // frame type (ack frame)
   2012     // (has nacks, not truncated, 6 byte largest observed, 1 byte delta)
   2013     0x6C,
   2014     // entropy hash of sent packets till least awaiting - 1.
   2015     0xAB,
   2016     // least packet sequence number awaiting an ack, delta from sequence number.
   2017     0x08, 0x00, 0x00, 0x00,
   2018     0x00, 0x00,
   2019     // entropy hash of all received packets.
   2020     0xBA,
   2021     // largest observed packet sequence number
   2022     0xBF, 0x9A, 0x78, 0x56,
   2023     0x34, 0x12,
   2024     // Zero delta time.
   2025     0x0, 0x0,
   2026     // num missing packets
   2027     0x01,
   2028     // missing packet delta
   2029     0x01,
   2030     // 0 more missing packets in range.
   2031     0x00,
   2032     // Number of revived packets.
   2033     0x01,
   2034     // Revived packet sequence number.
   2035     0xBE, 0x9A, 0x78, 0x56,
   2036     0x34, 0x12,
   2037   };
   2038 
   2039   QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
   2040   EXPECT_TRUE(framer_.ProcessPacket(encrypted));
   2041 
   2042   EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
   2043   ASSERT_TRUE(visitor_.header_.get());
   2044   EXPECT_TRUE(CheckDecryption(encrypted, !kIncludeVersion));
   2045 
   2046   EXPECT_EQ(0u, visitor_.stream_frames_.size());
   2047   ASSERT_EQ(1u, visitor_.ack_frames_.size());
   2048   const QuicAckFrame& frame = *visitor_.ack_frames_[0];
   2049   EXPECT_EQ(0xAB, frame.sent_info.entropy_hash);
   2050   EXPECT_EQ(0xBA, frame.received_info.entropy_hash);
   2051   EXPECT_EQ(GG_UINT64_C(0x0123456789ABF), frame.received_info.largest_observed);
   2052   ASSERT_EQ(1u, frame.received_info.missing_packets.size());
   2053   SequenceNumberSet::const_iterator missing_iter =
   2054       frame.received_info.missing_packets.begin();
   2055   EXPECT_EQ(GG_UINT64_C(0x0123456789ABE), *missing_iter);
   2056   EXPECT_EQ(GG_UINT64_C(0x0123456789AA0), frame.sent_info.least_unacked);
   2057 
   2058   const size_t kSentEntropyOffset = kQuicFrameTypeSize;
   2059   const size_t kLeastUnackedOffset = kSentEntropyOffset + kQuicEntropyHashSize;
   2060   const size_t kReceivedEntropyOffset = kLeastUnackedOffset +
   2061       PACKET_6BYTE_SEQUENCE_NUMBER;
   2062   const size_t kLargestObservedOffset = kReceivedEntropyOffset +
   2063       kQuicEntropyHashSize;
   2064   const size_t kMissingDeltaTimeOffset = kLargestObservedOffset +
   2065       PACKET_6BYTE_SEQUENCE_NUMBER;
   2066   const size_t kNumMissingPacketOffset = kMissingDeltaTimeOffset +
   2067       kQuicDeltaTimeLargestObservedSize;
   2068   const size_t kMissingPacketsOffset = kNumMissingPacketOffset +
   2069       kNumberOfNackRangesSize;
   2070   const size_t kMissingPacketsRange = kMissingPacketsOffset +
   2071       PACKET_1BYTE_SEQUENCE_NUMBER;
   2072   const size_t kRevivedPacketsLength = kMissingPacketsRange +
   2073       PACKET_1BYTE_SEQUENCE_NUMBER;
   2074   const size_t kRevivedPacketSequenceNumberLength = kRevivedPacketsLength +
   2075       PACKET_1BYTE_SEQUENCE_NUMBER;
   2076   // Now test framing boundaries
   2077   const size_t ack_frame_size = kRevivedPacketSequenceNumberLength +
   2078       PACKET_6BYTE_SEQUENCE_NUMBER;
   2079   for (size_t i = kQuicFrameTypeSize; i < ack_frame_size; ++i) {
   2080     string expected_error;
   2081     if (i < kLeastUnackedOffset) {
   2082       expected_error = "Unable to read entropy hash for sent packets.";
   2083     } else if (i < kReceivedEntropyOffset) {
   2084       expected_error = "Unable to read least unacked delta.";
   2085     } else if (i < kLargestObservedOffset) {
   2086       expected_error = "Unable to read entropy hash for received packets.";
   2087     } else if (i < kMissingDeltaTimeOffset) {
   2088       expected_error = "Unable to read largest observed.";
   2089     } else if (i < kNumMissingPacketOffset) {
   2090       expected_error = "Unable to read delta time largest observed.";
   2091     } else if (i < kMissingPacketsOffset) {
   2092       expected_error = "Unable to read num missing packet ranges.";
   2093     } else if (i < kMissingPacketsRange) {
   2094       expected_error = "Unable to read missing sequence number delta.";
   2095     } else if (i < kRevivedPacketsLength) {
   2096       expected_error = "Unable to read missing sequence number range.";
   2097     } else if (i < kRevivedPacketSequenceNumberLength) {
   2098       expected_error = "Unable to read num revived packets.";
   2099     } else {
   2100       expected_error = "Unable to read revived packet.";
   2101     }
   2102     CheckProcessingFails(
   2103         packet,
   2104         i + GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID, !kIncludeVersion,
   2105                                 PACKET_6BYTE_SEQUENCE_NUMBER, NOT_IN_FEC_GROUP),
   2106         expected_error, QUIC_INVALID_ACK_DATA);
   2107   }
   2108 }
   2109 
   2110 TEST_P(QuicFramerTest, AckFrameNoNacks) {
   2111   if (framer_.version() <= QUIC_VERSION_15) {
   2112     return;
   2113   }
   2114   unsigned char packet[] = {
   2115     // public flags (8 byte connection_id)
   2116     0x3C,
   2117     // connection_id
   2118     0x10, 0x32, 0x54, 0x76,
   2119     0x98, 0xBA, 0xDC, 0xFE,
   2120     // packet sequence number
   2121     0xA8, 0x9A, 0x78, 0x56,
   2122     0x34, 0x12,
   2123     // private flags (entropy)
   2124     0x01,
   2125 
   2126     // frame type (ack frame)
   2127     // (no nacks, not truncated, 6 byte largest observed, 1 byte delta)
   2128     0x4C,
   2129     // entropy hash of all received packets.
   2130     0xBA,
   2131     // largest observed packet sequence number
   2132     0xBF, 0x9A, 0x78, 0x56,
   2133     0x34, 0x12,
   2134     // Zero delta time.
   2135     0x0, 0x0,
   2136   };
   2137 
   2138   QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
   2139   EXPECT_TRUE(framer_.ProcessPacket(encrypted));
   2140 
   2141   EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
   2142   ASSERT_TRUE(visitor_.header_.get());
   2143   EXPECT_TRUE(CheckDecryption(encrypted, !kIncludeVersion));
   2144 
   2145   EXPECT_EQ(0u, visitor_.stream_frames_.size());
   2146   ASSERT_EQ(1u, visitor_.ack_frames_.size());
   2147   QuicAckFrame* frame = visitor_.ack_frames_[0];
   2148   EXPECT_EQ(0xBA, frame->received_info.entropy_hash);
   2149   EXPECT_EQ(GG_UINT64_C(0x0123456789ABF),
   2150             frame->received_info.largest_observed);
   2151   ASSERT_EQ(0u, frame->received_info.missing_packets.size());
   2152 
   2153   // Verify that the packet re-serializes identically.
   2154   QuicFrames frames;
   2155   frames.push_back(QuicFrame(frame));
   2156   scoped_ptr<QuicPacket> data(BuildDataPacket(*visitor_.header_, frames));
   2157   ASSERT_TRUE(data != NULL);
   2158 
   2159   test::CompareCharArraysWithHexError("constructed packet",
   2160                                       data->data(), data->length(),
   2161                                       AsChars(packet), arraysize(packet));
   2162 }
   2163 
   2164 TEST_P(QuicFramerTest, AckFrameNoNacks15) {
   2165   if (framer_.version() > QUIC_VERSION_15) {
   2166     return;
   2167   }
   2168   unsigned char packet[] = {
   2169     // public flags (8 byte connection_id)
   2170     0x3C,
   2171     // connection_id
   2172     0x10, 0x32, 0x54, 0x76,
   2173     0x98, 0xBA, 0xDC, 0xFE,
   2174     // packet sequence number
   2175     0xA8, 0x9A, 0x78, 0x56,
   2176     0x34, 0x12,
   2177     // private flags (entropy)
   2178     0x01,
   2179 
   2180     // frame type (ack frame)
   2181     // (no nacks, not truncated, 6 byte largest observed, 1 byte delta)
   2182     0x4C,
   2183     // entropy hash of sent packets till least awaiting - 1.
   2184     0xAB,
   2185     // least packet sequence number awaiting an ack, delta from sequence number.
   2186     0x08, 0x00, 0x00, 0x00,
   2187     0x00, 0x00,
   2188     // entropy hash of all received packets.
   2189     0xBA,
   2190     // largest observed packet sequence number
   2191     0xBF, 0x9A, 0x78, 0x56,
   2192     0x34, 0x12,
   2193     // Zero delta time.
   2194     0x0, 0x0,
   2195   };
   2196 
   2197   QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
   2198   EXPECT_TRUE(framer_.ProcessPacket(encrypted));
   2199 
   2200   EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
   2201   ASSERT_TRUE(visitor_.header_.get());
   2202   EXPECT_TRUE(CheckDecryption(encrypted, !kIncludeVersion));
   2203 
   2204   EXPECT_EQ(0u, visitor_.stream_frames_.size());
   2205   ASSERT_EQ(1u, visitor_.ack_frames_.size());
   2206   QuicAckFrame* frame = visitor_.ack_frames_[0];
   2207   EXPECT_EQ(0xAB, frame->sent_info.entropy_hash);
   2208   EXPECT_EQ(0xBA, frame->received_info.entropy_hash);
   2209   EXPECT_EQ(GG_UINT64_C(0x0123456789ABF),
   2210             frame->received_info.largest_observed);
   2211   ASSERT_EQ(0u, frame->received_info.missing_packets.size());
   2212   EXPECT_EQ(GG_UINT64_C(0x0123456789AA0), frame->sent_info.least_unacked);
   2213 
   2214   // Verify that the packet re-serializes identically.
   2215   QuicFrames frames;
   2216   frames.push_back(QuicFrame(frame));
   2217   scoped_ptr<QuicPacket> data(BuildDataPacket(*visitor_.header_, frames));
   2218   ASSERT_TRUE(data != NULL);
   2219 
   2220   test::CompareCharArraysWithHexError("constructed packet",
   2221                                       data->data(), data->length(),
   2222                                       AsChars(packet), arraysize(packet));
   2223 }
   2224 
   2225 TEST_P(QuicFramerTest, AckFrame500Nacks) {
   2226   if (framer_.version() <= QUIC_VERSION_15) {
   2227     return;
   2228   }
   2229   unsigned char packet[] = {
   2230     // public flags (8 byte connection_id)
   2231     0x3C,
   2232     // connection_id
   2233     0x10, 0x32, 0x54, 0x76,
   2234     0x98, 0xBA, 0xDC, 0xFE,
   2235     // packet sequence number
   2236     0xA8, 0x9A, 0x78, 0x56,
   2237     0x34, 0x12,
   2238     // private flags (entropy)
   2239     0x01,
   2240 
   2241     // frame type (ack frame)
   2242     // (has nacks, not truncated, 6 byte largest observed, 1 byte delta)
   2243     0x6C,
   2244     // entropy hash of all received packets.
   2245     0xBA,
   2246     // largest observed packet sequence number
   2247     0xBF, 0x9A, 0x78, 0x56,
   2248     0x34, 0x12,
   2249     // Zero delta time.
   2250     0x0, 0x0,
   2251     // num missing packet ranges
   2252     0x02,
   2253     // missing packet delta
   2254     0x01,
   2255     // 243 more missing packets in range.
   2256     // The ranges are listed in this order so the re-constructed packet matches.
   2257     0xF3,
   2258     // No gap between ranges
   2259     0x00,
   2260     // 255 more missing packets in range.
   2261     0xFF,
   2262     // No revived packets.
   2263     0x00,
   2264   };
   2265 
   2266   QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
   2267   EXPECT_TRUE(framer_.ProcessPacket(encrypted));
   2268 
   2269   EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
   2270   ASSERT_TRUE(visitor_.header_.get());
   2271   EXPECT_TRUE(CheckDecryption(encrypted, !kIncludeVersion));
   2272 
   2273   EXPECT_EQ(0u, visitor_.stream_frames_.size());
   2274   ASSERT_EQ(1u, visitor_.ack_frames_.size());
   2275   QuicAckFrame* frame = visitor_.ack_frames_[0];
   2276   EXPECT_EQ(0xBA, frame->received_info.entropy_hash);
   2277   EXPECT_EQ(GG_UINT64_C(0x0123456789ABF),
   2278             frame->received_info.largest_observed);
   2279   EXPECT_EQ(0u, frame->received_info.revived_packets.size());
   2280   ASSERT_EQ(500u, frame->received_info.missing_packets.size());
   2281   SequenceNumberSet::const_iterator first_missing_iter =
   2282       frame->received_info.missing_packets.begin();
   2283   EXPECT_EQ(GG_UINT64_C(0x0123456789ABE) - 499, *first_missing_iter);
   2284   SequenceNumberSet::const_reverse_iterator last_missing_iter =
   2285       frame->received_info.missing_packets.rbegin();
   2286   EXPECT_EQ(GG_UINT64_C(0x0123456789ABE), *last_missing_iter);
   2287 
   2288   // Verify that the packet re-serializes identically.
   2289   QuicFrames frames;
   2290   frames.push_back(QuicFrame(frame));
   2291   scoped_ptr<QuicPacket> data(BuildDataPacket(*visitor_.header_, frames));
   2292   ASSERT_TRUE(data != NULL);
   2293 
   2294   test::CompareCharArraysWithHexError("constructed packet",
   2295                                       data->data(), data->length(),
   2296                                       AsChars(packet), arraysize(packet));
   2297 }
   2298 
   2299 TEST_P(QuicFramerTest, AckFrame500Nacks15) {
   2300   if (framer_.version() != QUIC_VERSION_15) {
   2301     return;
   2302   }
   2303   unsigned char packet[] = {
   2304     // public flags (8 byte connection_id)
   2305     0x3C,
   2306     // connection_id
   2307     0x10, 0x32, 0x54, 0x76,
   2308     0x98, 0xBA, 0xDC, 0xFE,
   2309     // packet sequence number
   2310     0xA8, 0x9A, 0x78, 0x56,
   2311     0x34, 0x12,
   2312     // private flags (entropy)
   2313     0x01,
   2314 
   2315     // frame type (ack frame)
   2316     // (has nacks, not truncated, 6 byte largest observed, 1 byte delta)
   2317     0x6C,
   2318     // entropy hash of sent packets till least awaiting - 1.
   2319     0xAB,
   2320     // least packet sequence number awaiting an ack, delta from sequence number.
   2321     0x08, 0x00, 0x00, 0x00,
   2322     0x00, 0x00,
   2323     // entropy hash of all received packets.
   2324     0xBA,
   2325     // largest observed packet sequence number
   2326     0xBF, 0x9A, 0x78, 0x56,
   2327     0x34, 0x12,
   2328     // Zero delta time.
   2329     0x0, 0x0,
   2330     // num missing packet ranges
   2331     0x02,
   2332     // missing packet delta
   2333     0x01,
   2334     // 243 more missing packets in range.
   2335     // The ranges are listed in this order so the re-constructed packet matches.
   2336     0xF3,
   2337     // No gap between ranges
   2338     0x00,
   2339     // 255 more missing packets in range.
   2340     0xFF,
   2341     // No revived packets.
   2342     0x00,
   2343   };
   2344 
   2345   QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
   2346   EXPECT_TRUE(framer_.ProcessPacket(encrypted));
   2347 
   2348   EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
   2349   ASSERT_TRUE(visitor_.header_.get());
   2350   EXPECT_TRUE(CheckDecryption(encrypted, !kIncludeVersion));
   2351 
   2352   EXPECT_EQ(0u, visitor_.stream_frames_.size());
   2353   ASSERT_EQ(1u, visitor_.ack_frames_.size());
   2354   QuicAckFrame* frame = visitor_.ack_frames_[0];
   2355   EXPECT_EQ(0xAB, frame->sent_info.entropy_hash);
   2356   EXPECT_EQ(0xBA, frame->received_info.entropy_hash);
   2357   EXPECT_EQ(GG_UINT64_C(0x0123456789ABF),
   2358             frame->received_info.largest_observed);
   2359   EXPECT_EQ(0u, frame->received_info.revived_packets.size());
   2360   ASSERT_EQ(500u, frame->received_info.missing_packets.size());
   2361   SequenceNumberSet::const_iterator first_missing_iter =
   2362       frame->received_info.missing_packets.begin();
   2363   EXPECT_EQ(GG_UINT64_C(0x0123456789ABE) - 499, *first_missing_iter);
   2364   SequenceNumberSet::const_reverse_iterator last_missing_iter =
   2365       frame->received_info.missing_packets.rbegin();
   2366   EXPECT_EQ(GG_UINT64_C(0x0123456789ABE), *last_missing_iter);
   2367   EXPECT_EQ(GG_UINT64_C(0x0123456789AA0), frame->sent_info.least_unacked);
   2368 
   2369   // Verify that the packet re-serializes identically.
   2370   QuicFrames frames;
   2371   frames.push_back(QuicFrame(frame));
   2372   scoped_ptr<QuicPacket> data(BuildDataPacket(*visitor_.header_, frames));
   2373   ASSERT_TRUE(data != NULL);
   2374 
   2375   test::CompareCharArraysWithHexError("constructed packet",
   2376                                       data->data(), data->length(),
   2377                                       AsChars(packet), arraysize(packet));
   2378 }
   2379 
   2380 TEST_P(QuicFramerTest, CongestionFeedbackFrameTCP) {
   2381   unsigned char packet[] = {
   2382     // public flags (8 byte connection_id)
   2383     0x3C,
   2384     // connection_id
   2385     0x10, 0x32, 0x54, 0x76,
   2386     0x98, 0xBA, 0xDC, 0xFE,
   2387     // packet sequence number
   2388     0xBC, 0x9A, 0x78, 0x56,
   2389     0x34, 0x12,
   2390     // private flags
   2391     0x00,
   2392 
   2393     // frame type (congestion feedback frame)
   2394     0x20,
   2395     // congestion feedback type (tcp)
   2396     0x00,
   2397     // ack_frame.feedback.tcp.receive_window
   2398     0x03, 0x04,
   2399   };
   2400 
   2401   QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
   2402   EXPECT_TRUE(framer_.ProcessPacket(encrypted));
   2403 
   2404   EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
   2405   ASSERT_TRUE(visitor_.header_.get());
   2406   EXPECT_TRUE(CheckDecryption(encrypted, !kIncludeVersion));
   2407 
   2408   EXPECT_EQ(0u, visitor_.stream_frames_.size());
   2409   ASSERT_EQ(1u, visitor_.congestion_feedback_frames_.size());
   2410   const QuicCongestionFeedbackFrame& frame =
   2411       *visitor_.congestion_feedback_frames_[0];
   2412   ASSERT_EQ(kTCP, frame.type);
   2413   EXPECT_EQ(0x4030u, frame.tcp.receive_window);
   2414 
   2415   // Now test framing boundaries
   2416   for (size_t i = kQuicFrameTypeSize; i < 4; ++i) {
   2417     string expected_error;
   2418     if (i < 2) {
   2419       expected_error = "Unable to read congestion feedback type.";
   2420     } else if (i < 4) {
   2421       expected_error = "Unable to read receive window.";
   2422     }
   2423     CheckProcessingFails(
   2424         packet,
   2425         i + GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID, !kIncludeVersion,
   2426                                 PACKET_6BYTE_SEQUENCE_NUMBER, NOT_IN_FEC_GROUP),
   2427         expected_error, QUIC_INVALID_CONGESTION_FEEDBACK_DATA);
   2428   }
   2429 }
   2430 
   2431 TEST_P(QuicFramerTest, CongestionFeedbackFrameInterArrival) {
   2432   unsigned char packet[] = {
   2433     // public flags (8 byte connection_id)
   2434     0x3C,
   2435     // connection_id
   2436     0x10, 0x32, 0x54, 0x76,
   2437     0x98, 0xBA, 0xDC, 0xFE,
   2438     // packet sequence number
   2439     0xBC, 0x9A, 0x78, 0x56,
   2440     0x34, 0x12,
   2441     // private flags
   2442     0x00,
   2443 
   2444     // frame type (congestion feedback frame)
   2445     0x20,
   2446     // congestion feedback type (inter arrival)
   2447     0x01,
   2448     // num received packets
   2449     0x03,
   2450     // lowest sequence number
   2451     0xBA, 0x9A, 0x78, 0x56,
   2452     0x34, 0x12,
   2453     // receive time
   2454     0x87, 0x96, 0xA5, 0xB4,
   2455     0xC3, 0xD2, 0xE1, 0x07,
   2456     // sequence delta
   2457     0x01, 0x00,
   2458     // time delta
   2459     0x01, 0x00, 0x00, 0x00,
   2460     // sequence delta (skip one packet)
   2461     0x03, 0x00,
   2462     // time delta
   2463     0x02, 0x00, 0x00, 0x00,
   2464   };
   2465 
   2466   QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
   2467   EXPECT_TRUE(framer_.ProcessPacket(encrypted));
   2468 
   2469   EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
   2470   ASSERT_TRUE(visitor_.header_.get());
   2471   EXPECT_TRUE(CheckDecryption(encrypted, !kIncludeVersion));
   2472 
   2473   EXPECT_EQ(0u, visitor_.stream_frames_.size());
   2474   ASSERT_EQ(1u, visitor_.congestion_feedback_frames_.size());
   2475   const QuicCongestionFeedbackFrame& frame =
   2476       *visitor_.congestion_feedback_frames_[0];
   2477   ASSERT_EQ(kInterArrival, frame.type);
   2478   ASSERT_EQ(3u, frame.inter_arrival.received_packet_times.size());
   2479   TimeMap::const_iterator iter =
   2480       frame.inter_arrival.received_packet_times.begin();
   2481   EXPECT_EQ(GG_UINT64_C(0x0123456789ABA), iter->first);
   2482   EXPECT_EQ(GG_INT64_C(0x07E1D2C3B4A59687),
   2483             iter->second.Subtract(start_).ToMicroseconds());
   2484   ++iter;
   2485   EXPECT_EQ(GG_UINT64_C(0x0123456789ABB), iter->first);
   2486   EXPECT_EQ(GG_INT64_C(0x07E1D2C3B4A59688),
   2487             iter->second.Subtract(start_).ToMicroseconds());
   2488   ++iter;
   2489   EXPECT_EQ(GG_UINT64_C(0x0123456789ABD), iter->first);
   2490   EXPECT_EQ(GG_INT64_C(0x07E1D2C3B4A59689),
   2491             iter->second.Subtract(start_).ToMicroseconds());
   2492 
   2493   // Now test framing boundaries
   2494   for (size_t i = kQuicFrameTypeSize; i < 29; ++i) {
   2495     string expected_error;
   2496     if (i < 2) {
   2497       expected_error = "Unable to read congestion feedback type.";
   2498     } else if (i < 3) {
   2499       expected_error = "Unable to read num received packets.";
   2500     } else if (i < 9) {
   2501       expected_error = "Unable to read smallest received.";
   2502     } else if (i < 17) {
   2503       expected_error = "Unable to read time received.";
   2504     } else if (i < 19) {
   2505       expected_error = "Unable to read sequence delta in received packets.";
   2506     } else if (i < 23) {
   2507       expected_error = "Unable to read time delta in received packets.";
   2508     } else if (i < 25) {
   2509       expected_error = "Unable to read sequence delta in received packets.";
   2510     } else if (i < 29) {
   2511       expected_error = "Unable to read time delta in received packets.";
   2512     }
   2513     CheckProcessingFails(
   2514         packet,
   2515         i + GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID, !kIncludeVersion,
   2516                                 PACKET_6BYTE_SEQUENCE_NUMBER, NOT_IN_FEC_GROUP),
   2517         expected_error, QUIC_INVALID_CONGESTION_FEEDBACK_DATA);
   2518   }
   2519 }
   2520 
   2521 TEST_P(QuicFramerTest, CongestionFeedbackFrameFixRate) {
   2522   unsigned char packet[] = {
   2523     // public flags (8 byte connection_id)
   2524     0x3C,
   2525     // connection_id
   2526     0x10, 0x32, 0x54, 0x76,
   2527     0x98, 0xBA, 0xDC, 0xFE,
   2528     // packet sequence number
   2529     0xBC, 0x9A, 0x78, 0x56,
   2530     0x34, 0x12,
   2531     // private flags
   2532     0x00,
   2533 
   2534     // frame type (congestion feedback frame)
   2535     0x20,
   2536     // congestion feedback type (fix rate)
   2537     0x02,
   2538     // bitrate_in_bytes_per_second;
   2539     0x01, 0x02, 0x03, 0x04,
   2540   };
   2541 
   2542   QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
   2543   EXPECT_TRUE(framer_.ProcessPacket(encrypted));
   2544 
   2545   EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
   2546   ASSERT_TRUE(visitor_.header_.get());
   2547   EXPECT_TRUE(CheckDecryption(encrypted, !kIncludeVersion));
   2548 
   2549   EXPECT_EQ(0u, visitor_.stream_frames_.size());
   2550   ASSERT_EQ(1u, visitor_.congestion_feedback_frames_.size());
   2551   const QuicCongestionFeedbackFrame& frame =
   2552       *visitor_.congestion_feedback_frames_[0];
   2553   ASSERT_EQ(kFixRate, frame.type);
   2554   EXPECT_EQ(static_cast<uint32>(0x04030201),
   2555             frame.fix_rate.bitrate.ToBytesPerSecond());
   2556 
   2557   // Now test framing boundaries
   2558   for (size_t i = kQuicFrameTypeSize; i < 6; ++i) {
   2559     string expected_error;
   2560     if (i < 2) {
   2561       expected_error = "Unable to read congestion feedback type.";
   2562     } else if (i < 6) {
   2563       expected_error = "Unable to read bitrate.";
   2564     }
   2565     CheckProcessingFails(
   2566         packet,
   2567         i + GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID, !kIncludeVersion,
   2568                                 PACKET_6BYTE_SEQUENCE_NUMBER, NOT_IN_FEC_GROUP),
   2569         expected_error, QUIC_INVALID_CONGESTION_FEEDBACK_DATA);
   2570   }
   2571 }
   2572 
   2573 TEST_P(QuicFramerTest, CongestionFeedbackFrameInvalidFeedback) {
   2574   unsigned char packet[] = {
   2575     // public flags (8 byte connection_id)
   2576     0x3C,
   2577     // connection_id
   2578     0x10, 0x32, 0x54, 0x76,
   2579     0x98, 0xBA, 0xDC, 0xFE,
   2580     // packet sequence number
   2581     0xBC, 0x9A, 0x78, 0x56,
   2582     0x34, 0x12,
   2583     // private flags
   2584     0x00,
   2585 
   2586     // frame type (congestion feedback frame)
   2587     0x20,
   2588     // congestion feedback type (invalid)
   2589     0x03,
   2590   };
   2591 
   2592   QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
   2593   EXPECT_FALSE(framer_.ProcessPacket(encrypted));
   2594   EXPECT_TRUE(CheckDecryption(encrypted, !kIncludeVersion));
   2595   EXPECT_EQ(QUIC_INVALID_CONGESTION_FEEDBACK_DATA, framer_.error());
   2596 }
   2597 
   2598 TEST_P(QuicFramerTest, StopWaitingFrame) {
   2599   if (framer_.version() <= QUIC_VERSION_15) {
   2600     return;
   2601   }
   2602   unsigned char packet[] = {
   2603     // public flags (8 byte connection_id)
   2604     0x3C,
   2605     // connection_id
   2606     0x10, 0x32, 0x54, 0x76,
   2607     0x98, 0xBA, 0xDC, 0xFE,
   2608     // packet sequence number
   2609     0xA8, 0x9A, 0x78, 0x56,
   2610     0x34, 0x12,
   2611     // private flags (entropy)
   2612     0x01,
   2613 
   2614     // frame type (ack frame)
   2615     // (has nacks, not truncated, 6 byte largest observed, 1 byte delta)
   2616     0x06,
   2617     // entropy hash of sent packets till least awaiting - 1.
   2618     0xAB,
   2619     // least packet sequence number awaiting an ack, delta from sequence number.
   2620     0x08, 0x00, 0x00, 0x00,
   2621     0x00, 0x00,
   2622   };
   2623 
   2624   QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
   2625   EXPECT_TRUE(framer_.ProcessPacket(encrypted));
   2626 
   2627   EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
   2628   ASSERT_TRUE(visitor_.header_.get());
   2629   EXPECT_TRUE(CheckDecryption(encrypted, !kIncludeVersion));
   2630 
   2631   EXPECT_EQ(0u, visitor_.stream_frames_.size());
   2632   ASSERT_EQ(1u, visitor_.stop_waiting_frames_.size());
   2633   const QuicStopWaitingFrame& frame = *visitor_.stop_waiting_frames_[0];
   2634   EXPECT_EQ(0xAB, frame.entropy_hash);
   2635   EXPECT_EQ(GG_UINT64_C(0x0123456789AA0), frame.least_unacked);
   2636 
   2637   const size_t kSentEntropyOffset = kQuicFrameTypeSize;
   2638   const size_t kLeastUnackedOffset = kSentEntropyOffset + kQuicEntropyHashSize;
   2639   const size_t frame_size = 7;
   2640   for (size_t i = kQuicFrameTypeSize; i < frame_size; ++i) {
   2641     string expected_error;
   2642     if (i < kLeastUnackedOffset) {
   2643       expected_error = "Unable to read entropy hash for sent packets.";
   2644     } else {
   2645       expected_error = "Unable to read least unacked delta.";
   2646     }
   2647     CheckProcessingFails(
   2648         packet,
   2649         i + GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID, !kIncludeVersion,
   2650                                 PACKET_6BYTE_SEQUENCE_NUMBER, NOT_IN_FEC_GROUP),
   2651         expected_error, QUIC_INVALID_STOP_WAITING_DATA);
   2652   }
   2653 }
   2654 
   2655 TEST_P(QuicFramerTest, RstStreamFrameQuic) {
   2656   unsigned char packet[] = {
   2657     // public flags (8 byte connection_id)
   2658     0x3C,
   2659     // connection_id
   2660     0x10, 0x32, 0x54, 0x76,
   2661     0x98, 0xBA, 0xDC, 0xFE,
   2662     // packet sequence number
   2663     0xBC, 0x9A, 0x78, 0x56,
   2664     0x34, 0x12,
   2665     // private flags
   2666     0x00,
   2667 
   2668     // frame type (rst stream frame)
   2669     0x01,
   2670     // stream id
   2671     0x04, 0x03, 0x02, 0x01,
   2672 
   2673     // sent byte offset
   2674     0x01, 0x02, 0x03, 0x04,
   2675     0x05, 0x06, 0x07, 0x08,
   2676 
   2677     // error code
   2678     0x01, 0x00, 0x00, 0x00,
   2679 
   2680     // error details length
   2681     0x0d, 0x00,
   2682     // error details
   2683     'b',  'e',  'c',  'a',
   2684     'u',  's',  'e',  ' ',
   2685     'I',  ' ',  'c',  'a',
   2686     'n',
   2687   };
   2688 
   2689   QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
   2690   EXPECT_TRUE(framer_.ProcessPacket(encrypted));
   2691 
   2692   EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
   2693   ASSERT_TRUE(visitor_.header_.get());
   2694   EXPECT_TRUE(CheckDecryption(encrypted, !kIncludeVersion));
   2695 
   2696   EXPECT_EQ(GG_UINT64_C(0x01020304), visitor_.rst_stream_frame_.stream_id);
   2697   EXPECT_EQ(0x01, visitor_.rst_stream_frame_.error_code);
   2698   EXPECT_EQ("because I can", visitor_.rst_stream_frame_.error_details);
   2699   EXPECT_EQ(GG_UINT64_C(0x0807060504030201),
   2700             visitor_.rst_stream_frame_.byte_offset);
   2701 
   2702   // Now test framing boundaries
   2703   for (size_t i = kQuicFrameTypeSize;
   2704        i < QuicFramer::GetMinRstStreamFrameSize(version_); ++i) {
   2705     string expected_error;
   2706     if (i < kQuicFrameTypeSize + kQuicMaxStreamIdSize) {
   2707       expected_error = "Unable to read stream_id.";
   2708     } else if (i < kQuicFrameTypeSize + kQuicMaxStreamIdSize +
   2709                        + kQuicMaxStreamOffsetSize) {
   2710       expected_error = "Unable to read rst stream sent byte offset.";
   2711     } else if (i < kQuicFrameTypeSize + kQuicMaxStreamIdSize +
   2712                        + kQuicMaxStreamOffsetSize + kQuicErrorCodeSize) {
   2713       expected_error = "Unable to read rst stream error code.";
   2714     } else {
   2715       expected_error = "Unable to read rst stream error details.";
   2716     }
   2717     CheckProcessingFails(
   2718         packet,
   2719         i + GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID, !kIncludeVersion,
   2720                                 PACKET_6BYTE_SEQUENCE_NUMBER, NOT_IN_FEC_GROUP),
   2721         expected_error, QUIC_INVALID_RST_STREAM_DATA);
   2722   }
   2723 }
   2724 
   2725 TEST_P(QuicFramerTest, ConnectionCloseFrame) {
   2726   unsigned char packet[] = {
   2727     // public flags (8 byte connection_id)
   2728     0x3C,
   2729     // connection_id
   2730     0x10, 0x32, 0x54, 0x76,
   2731     0x98, 0xBA, 0xDC, 0xFE,
   2732     // packet sequence number
   2733     0xBC, 0x9A, 0x78, 0x56,
   2734     0x34, 0x12,
   2735     // private flags
   2736     0x00,
   2737 
   2738     // frame type (connection close frame)
   2739     0x02,
   2740     // error code
   2741     0x11, 0x00, 0x00, 0x00,
   2742 
   2743     // error details length
   2744     0x0d, 0x00,
   2745     // error details
   2746     'b',  'e',  'c',  'a',
   2747     'u',  's',  'e',  ' ',
   2748     'I',  ' ',  'c',  'a',
   2749     'n',
   2750   };
   2751 
   2752   QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
   2753   EXPECT_TRUE(framer_.ProcessPacket(encrypted));
   2754 
   2755   EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
   2756   ASSERT_TRUE(visitor_.header_.get());
   2757   EXPECT_TRUE(CheckDecryption(encrypted, !kIncludeVersion));
   2758 
   2759   EXPECT_EQ(0u, visitor_.stream_frames_.size());
   2760 
   2761   EXPECT_EQ(0x11, visitor_.connection_close_frame_.error_code);
   2762   EXPECT_EQ("because I can", visitor_.connection_close_frame_.error_details);
   2763 
   2764   ASSERT_EQ(0u, visitor_.ack_frames_.size());
   2765 
   2766   // Now test framing boundaries
   2767   for (size_t i = kQuicFrameTypeSize;
   2768        i < QuicFramer::GetMinConnectionCloseFrameSize(); ++i) {
   2769     string expected_error;
   2770     if (i < kQuicFrameTypeSize + kQuicErrorCodeSize) {
   2771       expected_error = "Unable to read connection close error code.";
   2772     } else {
   2773       expected_error = "Unable to read connection close error details.";
   2774     }
   2775     CheckProcessingFails(
   2776         packet,
   2777         i + GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID, !kIncludeVersion,
   2778                                 PACKET_6BYTE_SEQUENCE_NUMBER, NOT_IN_FEC_GROUP),
   2779         expected_error, QUIC_INVALID_CONNECTION_CLOSE_DATA);
   2780   }
   2781 }
   2782 
   2783 TEST_P(QuicFramerTest, GoAwayFrame) {
   2784   unsigned char packet[] = {
   2785     // public flags (8 byte connection_id)
   2786     0x3C,
   2787     // connection_id
   2788     0x10, 0x32, 0x54, 0x76,
   2789     0x98, 0xBA, 0xDC, 0xFE,
   2790     // packet sequence number
   2791     0xBC, 0x9A, 0x78, 0x56,
   2792     0x34, 0x12,
   2793     // private flags
   2794     0x00,
   2795 
   2796     // frame type (go away frame)
   2797     0x03,
   2798     // error code
   2799     0x09, 0x00, 0x00, 0x00,
   2800     // stream id
   2801     0x04, 0x03, 0x02, 0x01,
   2802     // error details length
   2803     0x0d, 0x00,
   2804     // error details
   2805     'b',  'e',  'c',  'a',
   2806     'u',  's',  'e',  ' ',
   2807     'I',  ' ',  'c',  'a',
   2808     'n',
   2809   };
   2810 
   2811   QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
   2812   EXPECT_TRUE(framer_.ProcessPacket(encrypted));
   2813 
   2814   EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
   2815   ASSERT_TRUE(visitor_.header_.get());
   2816   EXPECT_TRUE(CheckDecryption(encrypted, !kIncludeVersion));
   2817 
   2818   EXPECT_EQ(GG_UINT64_C(0x01020304),
   2819             visitor_.goaway_frame_.last_good_stream_id);
   2820   EXPECT_EQ(0x9, visitor_.goaway_frame_.error_code);
   2821   EXPECT_EQ("because I can", visitor_.goaway_frame_.reason_phrase);
   2822 
   2823   const size_t reason_size = arraysize("because I can") - 1;
   2824   // Now test framing boundaries
   2825   for (size_t i = kQuicFrameTypeSize;
   2826        i < QuicFramer::GetMinGoAwayFrameSize() + reason_size; ++i) {
   2827     string expected_error;
   2828     if (i < kQuicFrameTypeSize + kQuicErrorCodeSize) {
   2829       expected_error = "Unable to read go away error code.";
   2830     } else if (i < kQuicFrameTypeSize + kQuicErrorCodeSize +
   2831                kQuicMaxStreamIdSize) {
   2832       expected_error = "Unable to read last good stream id.";
   2833     } else {
   2834       expected_error = "Unable to read goaway reason.";
   2835     }
   2836     CheckProcessingFails(
   2837         packet,
   2838         i + GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID, !kIncludeVersion,
   2839                                 PACKET_6BYTE_SEQUENCE_NUMBER, NOT_IN_FEC_GROUP),
   2840         expected_error, QUIC_INVALID_GOAWAY_DATA);
   2841   }
   2842 }
   2843 
   2844 TEST_P(QuicFramerTest, WindowUpdateFrame) {
   2845   unsigned char packet[] = {
   2846     // public flags (8 byte connection_id)
   2847     0x3C,
   2848     // connection_id
   2849     0x10, 0x32, 0x54, 0x76,
   2850     0x98, 0xBA, 0xDC, 0xFE,
   2851     // packet sequence number
   2852     0xBC, 0x9A, 0x78, 0x56,
   2853     0x34, 0x12,
   2854     // private flags
   2855     0x00,
   2856 
   2857     // frame type (window update frame)
   2858     0x04,
   2859     // stream id
   2860     0x04, 0x03, 0x02, 0x01,
   2861     // byte offset
   2862     0x05, 0x06, 0x07, 0x08,
   2863     0x09, 0x0a, 0x0b, 0x0c,
   2864   };
   2865 
   2866   QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
   2867 
   2868   EXPECT_TRUE(framer_.ProcessPacket(encrypted));
   2869 
   2870   EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
   2871   ASSERT_TRUE(visitor_.header_.get());
   2872   EXPECT_TRUE(CheckDecryption(encrypted, !kIncludeVersion));
   2873 
   2874   EXPECT_EQ(GG_UINT64_C(0x01020304),
   2875             visitor_.window_update_frame_.stream_id);
   2876   EXPECT_EQ(GG_UINT64_C(0x0c0b0a0908070605),
   2877             visitor_.window_update_frame_.byte_offset);
   2878 
   2879   // Now test framing boundaries
   2880   for (size_t i = kQuicFrameTypeSize;
   2881        i < QuicFramer::GetWindowUpdateFrameSize(); ++i) {
   2882     string expected_error;
   2883     if (i < kQuicFrameTypeSize + kQuicMaxStreamIdSize) {
   2884       expected_error = "Unable to read stream_id.";
   2885     } else {
   2886       expected_error = "Unable to read window byte_offset.";
   2887     }
   2888     CheckProcessingFails(
   2889         packet,
   2890         i + GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID, !kIncludeVersion,
   2891                                 PACKET_6BYTE_SEQUENCE_NUMBER, NOT_IN_FEC_GROUP),
   2892         expected_error, QUIC_INVALID_WINDOW_UPDATE_DATA);
   2893   }
   2894 }
   2895 
   2896 TEST_P(QuicFramerTest, BlockedFrame) {
   2897   unsigned char packet[] = {
   2898     // public flags (8 byte connection_id)
   2899     0x3C,
   2900     // connection_id
   2901     0x10, 0x32, 0x54, 0x76,
   2902     0x98, 0xBA, 0xDC, 0xFE,
   2903     // packet sequence number
   2904     0xBC, 0x9A, 0x78, 0x56,
   2905     0x34, 0x12,
   2906     // private flags
   2907     0x00,
   2908 
   2909     // frame type (blocked frame)
   2910     0x05,
   2911     // stream id
   2912     0x04, 0x03, 0x02, 0x01,
   2913   };
   2914 
   2915   QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
   2916 
   2917   EXPECT_TRUE(framer_.ProcessPacket(encrypted));
   2918 
   2919   EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
   2920   ASSERT_TRUE(visitor_.header_.get());
   2921   EXPECT_TRUE(CheckDecryption(encrypted, !kIncludeVersion));
   2922 
   2923   EXPECT_EQ(GG_UINT64_C(0x01020304),
   2924             visitor_.blocked_frame_.stream_id);
   2925 
   2926   // Now test framing boundaries
   2927   for (size_t i = kQuicFrameTypeSize; i < QuicFramer::GetBlockedFrameSize();
   2928        ++i) {
   2929     string expected_error = "Unable to read stream_id.";
   2930     CheckProcessingFails(
   2931         packet,
   2932         i + GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID, !kIncludeVersion,
   2933                                 PACKET_6BYTE_SEQUENCE_NUMBER, NOT_IN_FEC_GROUP),
   2934         expected_error, QUIC_INVALID_BLOCKED_DATA);
   2935   }
   2936 }
   2937 
   2938 TEST_P(QuicFramerTest, PingFrame) {
   2939   if (version_ <= QUIC_VERSION_17) {
   2940     return;
   2941   }
   2942 
   2943   unsigned char packet[] = {
   2944     // public flags (8 byte connection_id)
   2945     0x3C,
   2946     // connection_id
   2947     0x10, 0x32, 0x54, 0x76,
   2948     0x98, 0xBA, 0xDC, 0xFE,
   2949     // packet sequence number
   2950     0xBC, 0x9A, 0x78, 0x56,
   2951     0x34, 0x12,
   2952     // private flags
   2953     0x00,
   2954 
   2955     // frame type (ping frame)
   2956     0x07,
   2957   };
   2958 
   2959   QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
   2960   EXPECT_TRUE(framer_.ProcessPacket(encrypted));
   2961 
   2962   EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
   2963   ASSERT_TRUE(visitor_.header_.get());
   2964   EXPECT_TRUE(CheckDecryption(encrypted, !kIncludeVersion));
   2965 
   2966   EXPECT_EQ(1u, visitor_.ping_frames_.size());
   2967 
   2968   // No need to check the PING frame boundaries because it has no payload.
   2969 }
   2970 
   2971 TEST_P(QuicFramerTest, PublicResetPacket) {
   2972   unsigned char packet[] = {
   2973     // public flags (public reset, 8 byte connection_id)
   2974     0x0E,
   2975     // connection_id
   2976     0x10, 0x32, 0x54, 0x76,
   2977     0x98, 0xBA, 0xDC, 0xFE,
   2978     // message tag (kPRST)
   2979     'P', 'R', 'S', 'T',
   2980     // num_entries (2) + padding
   2981     0x02, 0x00, 0x00, 0x00,
   2982     // tag kRNON
   2983     'R', 'N', 'O', 'N',
   2984     // end offset 8
   2985     0x08, 0x00, 0x00, 0x00,
   2986     // tag kRSEQ
   2987     'R', 'S', 'E', 'Q',
   2988     // end offset 16
   2989     0x10, 0x00, 0x00, 0x00,
   2990     // nonce proof
   2991     0x89, 0x67, 0x45, 0x23,
   2992     0x01, 0xEF, 0xCD, 0xAB,
   2993     // rejected sequence number
   2994     0xBC, 0x9A, 0x78, 0x56,
   2995     0x34, 0x12, 0x00, 0x00,
   2996   };
   2997 
   2998   QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
   2999   EXPECT_TRUE(framer_.ProcessPacket(encrypted));
   3000   ASSERT_EQ(QUIC_NO_ERROR, framer_.error());
   3001   ASSERT_TRUE(visitor_.public_reset_packet_.get());
   3002   EXPECT_EQ(GG_UINT64_C(0xFEDCBA9876543210),
   3003             visitor_.public_reset_packet_->public_header.connection_id);
   3004   EXPECT_TRUE(visitor_.public_reset_packet_->public_header.reset_flag);
   3005   EXPECT_FALSE(visitor_.public_reset_packet_->public_header.version_flag);
   3006   EXPECT_EQ(GG_UINT64_C(0xABCDEF0123456789),
   3007             visitor_.public_reset_packet_->nonce_proof);
   3008   EXPECT_EQ(GG_UINT64_C(0x123456789ABC),
   3009             visitor_.public_reset_packet_->rejected_sequence_number);
   3010   EXPECT_TRUE(
   3011       visitor_.public_reset_packet_->client_address.address().empty());
   3012 
   3013   // Now test framing boundaries
   3014   for (size_t i = 0; i < arraysize(packet); ++i) {
   3015     string expected_error;
   3016     DVLOG(1) << "iteration: " << i;
   3017     if (i < kConnectionIdOffset) {
   3018       expected_error = "Unable to read public flags.";
   3019       CheckProcessingFails(packet, i, expected_error,
   3020                            QUIC_INVALID_PACKET_HEADER);
   3021     } else if (i < kPublicResetPacketMessageTagOffset) {
   3022       expected_error = "Unable to read ConnectionId.";
   3023       CheckProcessingFails(packet, i, expected_error,
   3024                            QUIC_INVALID_PACKET_HEADER);
   3025     } else {
   3026       expected_error = "Unable to read reset message.";
   3027       CheckProcessingFails(packet, i, expected_error,
   3028                            QUIC_INVALID_PUBLIC_RST_PACKET);
   3029     }
   3030   }
   3031 }
   3032 
   3033 TEST_P(QuicFramerTest, PublicResetPacketWithTrailingJunk) {
   3034   unsigned char packet[] = {
   3035     // public flags (public reset, 8 byte connection_id)
   3036     0x0E,
   3037     // connection_id
   3038     0x10, 0x32, 0x54, 0x76,
   3039     0x98, 0xBA, 0xDC, 0xFE,
   3040     // message tag (kPRST)
   3041     'P', 'R', 'S', 'T',
   3042     // num_entries (2) + padding
   3043     0x02, 0x00, 0x00, 0x00,
   3044     // tag kRNON
   3045     'R', 'N', 'O', 'N',
   3046     // end offset 8
   3047     0x08, 0x00, 0x00, 0x00,
   3048     // tag kRSEQ
   3049     'R', 'S', 'E', 'Q',
   3050     // end offset 16
   3051     0x10, 0x00, 0x00, 0x00,
   3052     // nonce proof
   3053     0x89, 0x67, 0x45, 0x23,
   3054     0x01, 0xEF, 0xCD, 0xAB,
   3055     // rejected sequence number
   3056     0xBC, 0x9A, 0x78, 0x56,
   3057     0x34, 0x12, 0x00, 0x00,
   3058     // trailing junk
   3059     'j', 'u', 'n', 'k',
   3060   };
   3061 
   3062   string expected_error = "Unable to read reset message.";
   3063   CheckProcessingFails(packet, arraysize(packet), expected_error,
   3064                        QUIC_INVALID_PUBLIC_RST_PACKET);
   3065 }
   3066 
   3067 TEST_P(QuicFramerTest, PublicResetPacketWithClientAddress) {
   3068   unsigned char packet[] = {
   3069     // public flags (public reset, 8 byte connection_id)
   3070     0x0E,
   3071     // connection_id
   3072     0x10, 0x32, 0x54, 0x76,
   3073     0x98, 0xBA, 0xDC, 0xFE,
   3074     // message tag (kPRST)
   3075     'P', 'R', 'S', 'T',
   3076     // num_entries (3) + padding
   3077     0x03, 0x00, 0x00, 0x00,
   3078     // tag kRNON
   3079     'R', 'N', 'O', 'N',
   3080     // end offset 8
   3081     0x08, 0x00, 0x00, 0x00,
   3082     // tag kRSEQ
   3083     'R', 'S', 'E', 'Q',
   3084     // end offset 16
   3085     0x10, 0x00, 0x00, 0x00,
   3086     // tag kCADR
   3087     'C', 'A', 'D', 'R',
   3088     // end offset 24
   3089     0x18, 0x00, 0x00, 0x00,
   3090     // nonce proof
   3091     0x89, 0x67, 0x45, 0x23,
   3092     0x01, 0xEF, 0xCD, 0xAB,
   3093     // rejected sequence number
   3094     0xBC, 0x9A, 0x78, 0x56,
   3095     0x34, 0x12, 0x00, 0x00,
   3096     // client address: 4.31.198.44:443
   3097     0x02, 0x00,
   3098     0x04, 0x1F, 0xC6, 0x2C,
   3099     0xBB, 0x01,
   3100   };
   3101 
   3102   QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
   3103   EXPECT_TRUE(framer_.ProcessPacket(encrypted));
   3104   ASSERT_EQ(QUIC_NO_ERROR, framer_.error());
   3105   ASSERT_TRUE(visitor_.public_reset_packet_.get());
   3106   EXPECT_EQ(GG_UINT64_C(0xFEDCBA9876543210),
   3107             visitor_.public_reset_packet_->public_header.connection_id);
   3108   EXPECT_TRUE(visitor_.public_reset_packet_->public_header.reset_flag);
   3109   EXPECT_FALSE(visitor_.public_reset_packet_->public_header.version_flag);
   3110   EXPECT_EQ(GG_UINT64_C(0xABCDEF0123456789),
   3111             visitor_.public_reset_packet_->nonce_proof);
   3112   EXPECT_EQ(GG_UINT64_C(0x123456789ABC),
   3113             visitor_.public_reset_packet_->rejected_sequence_number);
   3114   EXPECT_EQ("4.31.198.44",
   3115             IPAddressToString(visitor_.public_reset_packet_->
   3116                 client_address.address()));
   3117   EXPECT_EQ(443, visitor_.public_reset_packet_->client_address.port());
   3118 
   3119   // Now test framing boundaries
   3120   for (size_t i = 0; i < arraysize(packet); ++i) {
   3121     string expected_error;
   3122     DVLOG(1) << "iteration: " << i;
   3123     if (i < kConnectionIdOffset) {
   3124       expected_error = "Unable to read public flags.";
   3125       CheckProcessingFails(packet, i, expected_error,
   3126                            QUIC_INVALID_PACKET_HEADER);
   3127     } else if (i < kPublicResetPacketMessageTagOffset) {
   3128       expected_error = "Unable to read ConnectionId.";
   3129       CheckProcessingFails(packet, i, expected_error,
   3130                            QUIC_INVALID_PACKET_HEADER);
   3131     } else {
   3132       expected_error = "Unable to read reset message.";
   3133       CheckProcessingFails(packet, i, expected_error,
   3134                            QUIC_INVALID_PUBLIC_RST_PACKET);
   3135     }
   3136   }
   3137 }
   3138 
   3139 TEST_P(QuicFramerTest, VersionNegotiationPacket) {
   3140   unsigned char packet[] = {
   3141     // public flags (version, 8 byte connection_id)
   3142     0x3D,
   3143     // connection_id
   3144     0x10, 0x32, 0x54, 0x76,
   3145     0x98, 0xBA, 0xDC, 0xFE,
   3146     // version tag
   3147     'Q', '0', GetQuicVersionDigitTens(), GetQuicVersionDigitOnes(),
   3148     'Q', '2', '.', '0',
   3149   };
   3150 
   3151   QuicFramerPeer::SetIsServer(&framer_, false);
   3152 
   3153   QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
   3154   EXPECT_TRUE(framer_.ProcessPacket(encrypted));
   3155   ASSERT_EQ(QUIC_NO_ERROR, framer_.error());
   3156   ASSERT_TRUE(visitor_.version_negotiation_packet_.get());
   3157   EXPECT_EQ(2u, visitor_.version_negotiation_packet_->versions.size());
   3158   EXPECT_EQ(GetParam(), visitor_.version_negotiation_packet_->versions[0]);
   3159 
   3160   for (size_t i = 0; i <= kPublicFlagsSize + PACKET_8BYTE_CONNECTION_ID; ++i) {
   3161     string expected_error;
   3162     QuicErrorCode error_code = QUIC_INVALID_PACKET_HEADER;
   3163     if (i < kConnectionIdOffset) {
   3164       expected_error = "Unable to read public flags.";
   3165     } else if (i < kVersionOffset) {
   3166       expected_error = "Unable to read ConnectionId.";
   3167     } else {
   3168       expected_error = "Unable to read supported version in negotiation.";
   3169       error_code = QUIC_INVALID_VERSION_NEGOTIATION_PACKET;
   3170     }
   3171     CheckProcessingFails(packet, i, expected_error, error_code);
   3172   }
   3173 }
   3174 
   3175 TEST_P(QuicFramerTest, FecPacket) {
   3176   unsigned char packet[] = {
   3177     // public flags (8 byte connection_id)
   3178     0x3C,
   3179     // connection_id
   3180     0x10, 0x32, 0x54, 0x76,
   3181     0x98, 0xBA, 0xDC, 0xFE,
   3182     // packet sequence number
   3183     0xBC, 0x9A, 0x78, 0x56,
   3184     0x34, 0x12,
   3185     // private flags (fec group & FEC)
   3186     0x06,
   3187     // first fec protected packet offset
   3188     0x01,
   3189 
   3190     // redundancy
   3191     'a',  'b',  'c',  'd',
   3192     'e',  'f',  'g',  'h',
   3193     'i',  'j',  'k',  'l',
   3194     'm',  'n',  'o',  'p',
   3195   };
   3196 
   3197   QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
   3198   EXPECT_TRUE(framer_.ProcessPacket(encrypted));
   3199 
   3200   EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
   3201   ASSERT_TRUE(visitor_.header_.get());
   3202   EXPECT_TRUE(CheckDecryption(encrypted, !kIncludeVersion));
   3203 
   3204   EXPECT_EQ(0u, visitor_.stream_frames_.size());
   3205   EXPECT_EQ(0u, visitor_.ack_frames_.size());
   3206   ASSERT_EQ(1, visitor_.fec_count_);
   3207   const QuicFecData& fec_data = *visitor_.fec_data_[0];
   3208   EXPECT_EQ(GG_UINT64_C(0x0123456789ABB), fec_data.fec_group);
   3209   EXPECT_EQ("abcdefghijklmnop", fec_data.redundancy);
   3210 }
   3211 
   3212 TEST_P(QuicFramerTest, BuildPaddingFramePacket) {
   3213   QuicPacketHeader header;
   3214   header.public_header.connection_id = GG_UINT64_C(0xFEDCBA9876543210);
   3215   header.public_header.reset_flag = false;
   3216   header.public_header.version_flag = false;
   3217   header.fec_flag = false;
   3218   header.entropy_flag = false;
   3219   header.packet_sequence_number = GG_UINT64_C(0x123456789ABC);
   3220   header.fec_group = 0;
   3221 
   3222   QuicPaddingFrame padding_frame;
   3223 
   3224   QuicFrames frames;
   3225   frames.push_back(QuicFrame(&padding_frame));
   3226 
   3227   unsigned char packet[kMaxPacketSize] = {
   3228     // public flags (8 byte connection_id)
   3229     0x3C,
   3230     // connection_id
   3231     0x10, 0x32, 0x54, 0x76,
   3232     0x98, 0xBA, 0xDC, 0xFE,
   3233     // packet sequence number
   3234     0xBC, 0x9A, 0x78, 0x56,
   3235     0x34, 0x12,
   3236     // private flags
   3237     0x00,
   3238 
   3239     // frame type (padding frame)
   3240     0x00,
   3241     0x00, 0x00, 0x00, 0x00
   3242   };
   3243 
   3244   uint64 header_size =
   3245       GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID, !kIncludeVersion,
   3246                           PACKET_6BYTE_SEQUENCE_NUMBER, NOT_IN_FEC_GROUP);
   3247   memset(packet + header_size + 1, 0x00, kMaxPacketSize - header_size - 1);
   3248 
   3249   scoped_ptr<QuicPacket> data(BuildDataPacket(header, frames));
   3250   ASSERT_TRUE(data != NULL);
   3251 
   3252   test::CompareCharArraysWithHexError("constructed packet",
   3253                                       data->data(), data->length(),
   3254                                       AsChars(packet),
   3255                                       arraysize(packet));
   3256 }
   3257 
   3258 TEST_P(QuicFramerTest, Build4ByteSequenceNumberPaddingFramePacket) {
   3259   QuicPacketHeader header;
   3260   header.public_header.connection_id = GG_UINT64_C(0xFEDCBA9876543210);
   3261   header.public_header.reset_flag = false;
   3262   header.public_header.version_flag = false;
   3263   header.fec_flag = false;
   3264   header.entropy_flag = false;
   3265   header.public_header.sequence_number_length = PACKET_4BYTE_SEQUENCE_NUMBER;
   3266   header.packet_sequence_number = GG_UINT64_C(0x123456789ABC);
   3267   header.fec_group = 0;
   3268 
   3269   QuicPaddingFrame padding_frame;
   3270 
   3271   QuicFrames frames;
   3272   frames.push_back(QuicFrame(&padding_frame));
   3273 
   3274   unsigned char packet[kMaxPacketSize] = {
   3275     // public flags (8 byte connection_id and 4 byte sequence number)
   3276     0x2C,
   3277     // connection_id
   3278     0x10, 0x32, 0x54, 0x76,
   3279     0x98, 0xBA, 0xDC, 0xFE,
   3280     // packet sequence number
   3281     0xBC, 0x9A, 0x78, 0x56,
   3282     // private flags
   3283     0x00,
   3284 
   3285     // frame type (padding frame)
   3286     0x00,
   3287     0x00, 0x00, 0x00, 0x00
   3288   };
   3289 
   3290   uint64 header_size =
   3291       GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID, !kIncludeVersion,
   3292                           PACKET_4BYTE_SEQUENCE_NUMBER, NOT_IN_FEC_GROUP);
   3293   memset(packet + header_size + 1, 0x00, kMaxPacketSize - header_size - 1);
   3294 
   3295   scoped_ptr<QuicPacket> data(BuildDataPacket(header, frames));
   3296   ASSERT_TRUE(data != NULL);
   3297 
   3298   test::CompareCharArraysWithHexError("constructed packet",
   3299                                       data->data(), data->length(),
   3300                                       AsChars(packet),
   3301                                       arraysize(packet));
   3302 }
   3303 
   3304 TEST_P(QuicFramerTest, Build2ByteSequenceNumberPaddingFramePacket) {
   3305   QuicPacketHeader header;
   3306   header.public_header.connection_id = GG_UINT64_C(0xFEDCBA9876543210);
   3307   header.public_header.reset_flag = false;
   3308   header.public_header.version_flag = false;
   3309   header.fec_flag = false;
   3310   header.entropy_flag = false;
   3311   header.public_header.sequence_number_length = PACKET_2BYTE_SEQUENCE_NUMBER;
   3312   header.packet_sequence_number = GG_UINT64_C(0x123456789ABC);
   3313   header.fec_group = 0;
   3314 
   3315   QuicPaddingFrame padding_frame;
   3316 
   3317   QuicFrames frames;
   3318   frames.push_back(QuicFrame(&padding_frame));
   3319 
   3320   unsigned char packet[kMaxPacketSize] = {
   3321     // public flags (8 byte connection_id and 2 byte sequence number)
   3322     0x1C,
   3323     // connection_id
   3324     0x10, 0x32, 0x54, 0x76,
   3325     0x98, 0xBA, 0xDC, 0xFE,
   3326     // packet sequence number
   3327     0xBC, 0x9A,
   3328     // private flags
   3329     0x00,
   3330 
   3331     // frame type (padding frame)
   3332     0x00,
   3333     0x00, 0x00, 0x00, 0x00
   3334   };
   3335 
   3336   uint64 header_size =
   3337       GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID, !kIncludeVersion,
   3338                           PACKET_2BYTE_SEQUENCE_NUMBER, NOT_IN_FEC_GROUP);
   3339   memset(packet + header_size + 1, 0x00, kMaxPacketSize - header_size - 1);
   3340 
   3341   scoped_ptr<QuicPacket> data(BuildDataPacket(header, frames));
   3342   ASSERT_TRUE(data != NULL);
   3343 
   3344   test::CompareCharArraysWithHexError("constructed packet",
   3345                                       data->data(), data->length(),
   3346                                       AsChars(packet),
   3347                                       arraysize(packet));
   3348 }
   3349 
   3350 TEST_P(QuicFramerTest, Build1ByteSequenceNumberPaddingFramePacket) {
   3351   QuicPacketHeader header;
   3352   header.public_header.connection_id = GG_UINT64_C(0xFEDCBA9876543210);
   3353   header.public_header.reset_flag = false;
   3354   header.public_header.version_flag = false;
   3355   header.fec_flag = false;
   3356   header.entropy_flag = false;
   3357   header.public_header.sequence_number_length = PACKET_1BYTE_SEQUENCE_NUMBER;
   3358   header.packet_sequence_number = GG_UINT64_C(0x123456789ABC);
   3359   header.fec_group = 0;
   3360 
   3361   QuicPaddingFrame padding_frame;
   3362 
   3363   QuicFrames frames;
   3364   frames.push_back(QuicFrame(&padding_frame));
   3365 
   3366   unsigned char packet[kMaxPacketSize] = {
   3367     // public flags (8 byte connection_id and 1 byte sequence number)
   3368     0x0C,
   3369     // connection_id
   3370     0x10, 0x32, 0x54, 0x76,
   3371     0x98, 0xBA, 0xDC, 0xFE,
   3372     // packet sequence number
   3373     0xBC,
   3374     // private flags
   3375     0x00,
   3376 
   3377     // frame type (padding frame)
   3378     0x00,
   3379     0x00, 0x00, 0x00, 0x00
   3380   };
   3381 
   3382   uint64 header_size =
   3383       GetPacketHeaderSize(PACKET_8BYTE_CONNECTION_ID, !kIncludeVersion,
   3384                           PACKET_1BYTE_SEQUENCE_NUMBER, NOT_IN_FEC_GROUP);
   3385   memset(packet + header_size + 1, 0x00, kMaxPacketSize - header_size - 1);
   3386 
   3387   scoped_ptr<QuicPacket> data(BuildDataPacket(header, frames));
   3388   ASSERT_TRUE(data != NULL);
   3389 
   3390   test::CompareCharArraysWithHexError("constructed packet",
   3391                                       data->data(), data->length(),
   3392                                       AsChars(packet),
   3393                                       arraysize(packet));
   3394 }
   3395 
   3396 TEST_P(QuicFramerTest, BuildStreamFramePacket) {
   3397   QuicPacketHeader header;
   3398   header.public_header.connection_id = GG_UINT64_C(0xFEDCBA9876543210);
   3399   header.public_header.reset_flag = false;
   3400   header.public_header.version_flag = false;
   3401   header.fec_flag = false;
   3402   header.entropy_flag = true;
   3403   header.packet_sequence_number = GG_UINT64_C(0x77123456789ABC);
   3404   header.fec_group = 0;
   3405 
   3406   QuicStreamFrame stream_frame;
   3407   stream_frame.stream_id = 0x01020304;
   3408   stream_frame.fin = true;
   3409   stream_frame.offset = GG_UINT64_C(0xBA98FEDC32107654);
   3410   stream_frame.data = MakeIOVector("hello world!");
   3411 
   3412   QuicFrames frames;
   3413   frames.push_back(QuicFrame(&stream_frame));
   3414 
   3415   unsigned char packet[] = {
   3416     // public flags (8 byte connection_id)
   3417     0x3C,
   3418     // connection_id
   3419     0x10, 0x32, 0x54, 0x76,
   3420     0x98, 0xBA, 0xDC, 0xFE,
   3421     // packet sequence number
   3422     0xBC, 0x9A, 0x78, 0x56,
   3423     0x34, 0x12,
   3424     // private flags (entropy)
   3425     0x01,
   3426 
   3427     // frame type (stream frame with fin and no length)
   3428     0xDF,
   3429     // stream id
   3430     0x04, 0x03, 0x02, 0x01,
   3431     // offset
   3432     0x54, 0x76, 0x10, 0x32,
   3433     0xDC, 0xFE, 0x98, 0xBA,
   3434     // data
   3435     'h',  'e',  'l',  'l',
   3436     'o',  ' ',  'w',  'o',
   3437     'r',  'l',  'd',  '!',
   3438   };
   3439 
   3440   scoped_ptr<QuicPacket> data(BuildDataPacket(header, frames));
   3441   ASSERT_TRUE(data != NULL);
   3442 
   3443   test::CompareCharArraysWithHexError("constructed packet",
   3444                                       data->data(), data->length(),
   3445                                       AsChars(packet), arraysize(packet));
   3446 }
   3447 
   3448 TEST_P(QuicFramerTest, BuildStreamFramePacketInFecGroup) {
   3449   QuicPacketHeader header;
   3450   header.public_header.connection_id = GG_UINT64_C(0xFEDCBA9876543210);
   3451   header.public_header.reset_flag = false;
   3452   header.public_header.version_flag = false;
   3453   header.fec_flag = false;
   3454   header.entropy_flag = true;
   3455   header.packet_sequence_number = GG_UINT64_C(0x77123456789ABC);
   3456   header.is_in_fec_group = IN_FEC_GROUP;
   3457   header.fec_group = GG_UINT64_C(0x77123456789ABC);
   3458 
   3459   QuicStreamFrame stream_frame;
   3460   stream_frame.stream_id = 0x01020304;
   3461   stream_frame.fin = true;
   3462   stream_frame.offset = GG_UINT64_C(0xBA98FEDC32107654);
   3463   stream_frame.data = MakeIOVector("hello world!");
   3464 
   3465   QuicFrames frames;
   3466   frames.push_back(QuicFrame(&stream_frame));
   3467   unsigned char packet[] = {
   3468     // public flags (8 byte connection_id)
   3469     0x3C,
   3470     // connection_id
   3471     0x10, 0x32, 0x54, 0x76,
   3472     0x98, 0xBA, 0xDC, 0xFE,
   3473     // packet sequence number
   3474     0xBC, 0x9A, 0x78, 0x56,
   3475     0x34, 0x12,
   3476     // private flags (entropy, is_in_fec_group)
   3477     0x03,
   3478     // FEC group
   3479     0x00,
   3480     // frame type (stream frame with fin and data length field)
   3481     0xFF,
   3482     // stream id
   3483     0x04, 0x03, 0x02, 0x01,
   3484     // offset
   3485     0x54, 0x76, 0x10, 0x32,
   3486     0xDC, 0xFE, 0x98, 0xBA,
   3487     // data length (since packet is in an FEC group)
   3488     0x0C, 0x00,
   3489     // data
   3490     'h',  'e',  'l',  'l',
   3491     'o',  ' ',  'w',  'o',
   3492     'r',  'l',  'd',  '!',
   3493   };
   3494 
   3495   scoped_ptr<QuicPacket> data(BuildDataPacket(header, frames));
   3496   ASSERT_TRUE(data != NULL);
   3497 
   3498   test::CompareCharArraysWithHexError("constructed packet",
   3499                                       data->data(), data->length(),
   3500                                       AsChars(packet), arraysize(packet));
   3501 }
   3502 
   3503 TEST_P(QuicFramerTest, BuildStreamFramePacketWithVersionFlag) {
   3504   QuicPacketHeader header;
   3505   header.public_header.connection_id = GG_UINT64_C(0xFEDCBA9876543210);
   3506   header.public_header.reset_flag = false;
   3507   header.public_header.version_flag = true;
   3508   header.fec_flag = false;
   3509   header.entropy_flag = true;
   3510   header.packet_sequence_number = GG_UINT64_C(0x77123456789ABC);
   3511   header.fec_group = 0;
   3512 
   3513   QuicStreamFrame stream_frame;
   3514   stream_frame.stream_id = 0x01020304;
   3515   stream_frame.fin = true;
   3516   stream_frame.offset = GG_UINT64_C(0xBA98FEDC32107654);
   3517   stream_frame.data = MakeIOVector("hello world!");
   3518 
   3519   QuicFrames frames;
   3520   frames.push_back(QuicFrame(&stream_frame));
   3521 
   3522   unsigned char packet[] = {
   3523     // public flags (version, 8 byte connection_id)
   3524     0x3D,
   3525     // connection_id
   3526     0x10, 0x32, 0x54, 0x76,
   3527     0x98, 0xBA, 0xDC, 0xFE,
   3528     // version tag
   3529     'Q', '0', GetQuicVersionDigitTens(), GetQuicVersionDigitOnes(),
   3530     // packet sequence number
   3531     0xBC, 0x9A, 0x78, 0x56,
   3532     0x34, 0x12,
   3533     // private flags (entropy)
   3534     0x01,
   3535 
   3536     // frame type (stream frame with fin and no length)
   3537     0xDF,
   3538     // stream id
   3539     0x04, 0x03, 0x02, 0x01,
   3540     // offset
   3541     0x54, 0x76, 0x10, 0x32,
   3542     0xDC, 0xFE, 0x98, 0xBA,
   3543     // data
   3544     'h',  'e',  'l',  'l',
   3545     'o',  ' ',  'w',  'o',
   3546     'r',  'l',  'd',  '!',
   3547   };
   3548 
   3549   QuicFramerPeer::SetIsServer(&framer_, false);
   3550   scoped_ptr<QuicPacket> data(BuildDataPacket(header, frames));
   3551   ASSERT_TRUE(data != NULL);
   3552 
   3553   test::CompareCharArraysWithHexError("constructed packet",
   3554                                       data->data(), data->length(),
   3555                                       AsChars(packet), arraysize(packet));
   3556 }
   3557 
   3558 TEST_P(QuicFramerTest, BuildVersionNegotiationPacket) {
   3559   QuicPacketPublicHeader header;
   3560   header.connection_id = GG_UINT64_C(0xFEDCBA9876543210);
   3561   header.reset_flag = false;
   3562   header.version_flag = true;
   3563 
   3564   unsigned char packet[] = {
   3565     // public flags (version, 8 byte connection_id)
   3566     0x0D,
   3567     // connection_id
   3568     0x10, 0x32, 0x54, 0x76,
   3569     0x98, 0xBA, 0xDC, 0xFE,
   3570     // version tag
   3571     'Q', '0', GetQuicVersionDigitTens(), GetQuicVersionDigitOnes(),
   3572   };
   3573 
   3574   QuicVersionVector versions;
   3575   versions.push_back(GetParam());
   3576   scoped_ptr<QuicEncryptedPacket> data(
   3577       framer_.BuildVersionNegotiationPacket(header, versions));
   3578 
   3579   test::CompareCharArraysWithHexError("constructed packet",
   3580                                       data->data(), data->length(),
   3581                                       AsChars(packet), arraysize(packet));
   3582 }
   3583 
   3584 TEST_P(QuicFramerTest, BuildAckFramePacket) {
   3585   if (version_ <= QUIC_VERSION_15) {
   3586     return;
   3587   }
   3588   QuicPacketHeader header;
   3589   header.public_header.connection_id = GG_UINT64_C(0xFEDCBA9876543210);
   3590   header.public_header.reset_flag = false;
   3591   header.public_header.version_flag = false;
   3592   header.fec_flag = false;
   3593   header.entropy_flag = true;
   3594   header.packet_sequence_number = GG_UINT64_C(0x770123456789AA8);
   3595   header.fec_group = 0;
   3596 
   3597   QuicAckFrame ack_frame;
   3598   ack_frame.received_info.entropy_hash = 0x43;
   3599   ack_frame.received_info.largest_observed = GG_UINT64_C(0x770123456789ABF);
   3600   ack_frame.received_info.delta_time_largest_observed = QuicTime::Delta::Zero();
   3601   ack_frame.received_info.missing_packets.insert(
   3602       GG_UINT64_C(0x770123456789ABE));
   3603 
   3604   QuicFrames frames;
   3605   frames.push_back(QuicFrame(&ack_frame));
   3606 
   3607   unsigned char packet[] = {
   3608     // public flags (8 byte connection_id)
   3609     0x3C,
   3610     // connection_id
   3611     0x10, 0x32, 0x54, 0x76,
   3612     0x98, 0xBA, 0xDC, 0xFE,
   3613     // packet sequence number
   3614     0xA8, 0x9A, 0x78, 0x56,
   3615     0x34, 0x12,
   3616     // private flags (entropy)
   3617     0x01,
   3618 
   3619     // frame type (ack frame)
   3620     // (has nacks, not truncated, 6 byte largest observed, 1 byte delta)
   3621     0x6C,
   3622     // entropy hash of all received packets.
   3623     0x43,
   3624     // largest observed packet sequence number
   3625     0xBF, 0x9A, 0x78, 0x56,
   3626     0x34, 0x12,
   3627     // Zero delta time.
   3628     0x0, 0x0,
   3629     // num missing packet ranges
   3630     0x01,
   3631     // missing packet delta
   3632     0x01,
   3633     // 0 more missing packets in range.
   3634     0x00,
   3635     // 0 revived packets.
   3636     0x00,
   3637   };
   3638 
   3639   scoped_ptr<QuicPacket> data(BuildDataPacket(header, frames));
   3640   ASSERT_TRUE(data != NULL);
   3641 
   3642   test::CompareCharArraysWithHexError("constructed packet",
   3643                                       data->data(), data->length(),
   3644                                       AsChars(packet), arraysize(packet));
   3645 }
   3646 
   3647 TEST_P(QuicFramerTest, BuildAckFramePacket15) {
   3648   if (version_ != QUIC_VERSION_15) {
   3649     return;
   3650   }
   3651   QuicPacketHeader header;
   3652   header.public_header.connection_id = GG_UINT64_C(0xFEDCBA9876543210);
   3653   header.public_header.reset_flag = false;
   3654   header.public_header.version_flag = false;
   3655   header.fec_flag = false;
   3656   header.entropy_flag = true;
   3657   header.packet_sequence_number = GG_UINT64_C(0x770123456789AA8);
   3658   header.fec_group = 0;
   3659 
   3660   QuicAckFrame ack_frame;
   3661   ack_frame.received_info.entropy_hash = 0x43;
   3662   ack_frame.received_info.largest_observed = GG_UINT64_C(0x770123456789ABF);
   3663   ack_frame.received_info.delta_time_largest_observed = QuicTime::Delta::Zero();
   3664   ack_frame.received_info.missing_packets.insert(
   3665       GG_UINT64_C(0x770123456789ABE));
   3666   ack_frame.sent_info.entropy_hash = 0x14;
   3667   ack_frame.sent_info.least_unacked = GG_UINT64_C(0x770123456789AA0);
   3668 
   3669   QuicFrames frames;
   3670   frames.push_back(QuicFrame(&ack_frame));
   3671 
   3672   unsigned char packet[] = {
   3673     // public flags (8 byte connection_id)
   3674     0x3C,
   3675     // connection_id
   3676     0x10, 0x32, 0x54, 0x76,
   3677     0x98, 0xBA, 0xDC, 0xFE,
   3678     // packet sequence number
   3679     0xA8, 0x9A, 0x78, 0x56,
   3680     0x34, 0x12,
   3681     // private flags (entropy)
   3682     0x01,
   3683 
   3684     // frame type (ack frame)
   3685     // (has nacks, not truncated, 6 byte largest observed, 1 byte delta)
   3686     0x6C,
   3687     // entropy hash of sent packets till least awaiting - 1.
   3688     0x14,
   3689     // least packet sequence number awaiting an ack, delta from sequence number.
   3690     0x08, 0x00, 0x00, 0x00,
   3691     0x00, 0x00,
   3692     // entropy hash of all received packets.
   3693     0x43,
   3694     // largest observed packet sequence number
   3695     0xBF, 0x9A, 0x78, 0x56,
   3696     0x34, 0x12,
   3697     // Zero delta time.
   3698     0x0, 0x0,
   3699     // num missing packet ranges
   3700     0x01,
   3701     // missing packet delta
   3702     0x01,
   3703     // 0 more missing packets in range.
   3704     0x00,
   3705     // 0 revived packets.
   3706     0x00,
   3707   };
   3708 
   3709   scoped_ptr<QuicPacket> data(BuildDataPacket(header, frames));
   3710   ASSERT_TRUE(data != NULL);
   3711 
   3712   test::CompareCharArraysWithHexError("constructed packet",
   3713                                       data->data(), data->length(),
   3714                                       AsChars(packet), arraysize(packet));
   3715 }
   3716 
   3717 // TODO(jri): Add test for tuncated packets in which the original ack frame had
   3718 // revived packets. (In both the large and small packet cases below).
   3719 TEST_P(QuicFramerTest, BuildTruncatedAckFrameLargePacket) {
   3720   if (version_ <= QUIC_VERSION_15) {
   3721     return;
   3722   }
   3723   QuicPacketHeader header;
   3724   header.public_header.connection_id = GG_UINT64_C(0xFEDCBA9876543210);
   3725   header.public_header.reset_flag = false;
   3726   header.public_header.version_flag = false;
   3727   header.fec_flag = false;
   3728   header.entropy_flag = true;
   3729   header.packet_sequence_number = GG_UINT64_C(0x770123456789AA8);
   3730   header.fec_group = 0;
   3731 
   3732   QuicAckFrame ack_frame;
   3733   // This entropy hash is different from what shows up in the packet below,
   3734   // since entropy is recomputed by the framer on ack truncation (by
   3735   // TestEntropyCalculator for this test.)
   3736   ack_frame.received_info.entropy_hash = 0x43;
   3737   ack_frame.received_info.largest_observed = 2 * 300;
   3738   ack_frame.received_info.delta_time_largest_observed = QuicTime::Delta::Zero();
   3739   for (size_t i = 1; i < 2 * 300; i += 2) {
   3740     ack_frame.received_info.missing_packets.insert(i);
   3741   }
   3742 
   3743   QuicFrames frames;
   3744   frames.push_back(QuicFrame(&ack_frame));
   3745 
   3746   unsigned char packet[] = {
   3747     // public flags (8 byte connection_id)
   3748     0x3C,
   3749     // connection_id
   3750     0x10, 0x32, 0x54, 0x76,
   3751     0x98, 0xBA, 0xDC, 0xFE,
   3752     // packet sequence number
   3753     0xA8, 0x9A, 0x78, 0x56,
   3754     0x34, 0x12,
   3755     // private flags (entropy)
   3756     0x01,
   3757 
   3758     // frame type (ack frame)
   3759     // (has nacks, is truncated, 2 byte largest observed, 1 byte delta)
   3760     0x74,
   3761     // entropy hash of all received packets, set to 1 by TestEntropyCalculator
   3762     // since ack is truncated.
   3763     0x01,
   3764     // 2-byte largest observed packet sequence number.
   3765     // Expected to be 510 (0x1FE), since only 255 nack ranges can fit.
   3766     0xFE, 0x01,
   3767     // Zero delta time.
   3768     0x0, 0x0,
   3769     // num missing packet ranges (limited to 255 by size of this field).
   3770     0xFF,
   3771     // {missing packet delta, further missing packets in range}
   3772     // 6 nack ranges x 42 + 3 nack ranges
   3773     0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
   3774     0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
   3775     0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
   3776     0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
   3777     0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
   3778     0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
   3779     0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
   3780     0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
   3781     0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
   3782     0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
   3783 
   3784     0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
   3785     0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
   3786     0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
   3787     0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
   3788     0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
   3789     0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
   3790     0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
   3791     0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
   3792     0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
   3793     0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
   3794 
   3795     0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
   3796     0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
   3797     0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
   3798     0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
   3799     0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
   3800     0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
   3801     0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
   3802     0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
   3803     0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
   3804     0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
   3805 
   3806     0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
   3807     0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
   3808     0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
   3809     0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
   3810     0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
   3811     0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
   3812     0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
   3813     0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
   3814     0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
   3815     0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
   3816 
   3817     0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
   3818     0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
   3819     0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
   3820 
   3821     // 0 revived packets.
   3822     0x00,
   3823   };
   3824 
   3825   scoped_ptr<QuicPacket> data(
   3826       framer_.BuildDataPacket(header, frames, kMaxPacketSize).packet);
   3827   ASSERT_TRUE(data != NULL);
   3828 
   3829   test::CompareCharArraysWithHexError("constructed packet",
   3830                                       data->data(), data->length(),
   3831                                       AsChars(packet), arraysize(packet));
   3832 }
   3833 
   3834 
   3835 TEST_P(QuicFramerTest, BuildTruncatedAckFrameSmallPacket) {
   3836   if (version_ <= QUIC_VERSION_15) {
   3837     return;
   3838   }
   3839   QuicPacketHeader header;
   3840   header.public_header.connection_id = GG_UINT64_C(0xFEDCBA9876543210);
   3841   header.public_header.reset_flag = false;
   3842   header.public_header.version_flag = false;
   3843   header.fec_flag = false;
   3844   header.entropy_flag = true;
   3845   header.packet_sequence_number = GG_UINT64_C(0x770123456789AA8);
   3846   header.fec_group = 0;
   3847 
   3848   QuicAckFrame ack_frame;
   3849   // This entropy hash is different from what shows up in the packet below,
   3850   // since entropy is recomputed by the framer on ack truncation (by
   3851   // TestEntropyCalculator for this test.)
   3852   ack_frame.received_info.entropy_hash = 0x43;
   3853   ack_frame.received_info.largest_observed = 2 * 300;
   3854   ack_frame.received_info.delta_time_largest_observed = QuicTime::Delta::Zero();
   3855   for (size_t i = 1; i < 2 * 300; i += 2) {
   3856     ack_frame.received_info.missing_packets.insert(i);
   3857   }
   3858 
   3859   QuicFrames frames;
   3860   frames.push_back(QuicFrame(&ack_frame));
   3861 
   3862   unsigned char packet[] = {
   3863     // public flags (8 byte connection_id)
   3864     0x3C,
   3865     // connection_id
   3866     0x10, 0x32, 0x54, 0x76,
   3867     0x98, 0xBA, 0xDC, 0xFE,
   3868     // packet sequence number
   3869     0xA8, 0x9A, 0x78, 0x56,
   3870     0x34, 0x12,
   3871     // private flags (entropy)
   3872     0x01,
   3873 
   3874     // frame type (ack frame)
   3875     // (has nacks, is truncated, 2 byte largest observed, 1 byte delta)
   3876     0x74,
   3877     // entropy hash of all received packets, set to 1 by TestEntropyCalculator
   3878     // since ack is truncated.
   3879     0x01,
   3880     // 2-byte largest observed packet sequence number.
   3881     // Expected to be 12 (0x0C), since only 6 nack ranges can fit.
   3882     0x0C, 0x00,
   3883     // Zero delta time.
   3884     0x0, 0x0,
   3885     // num missing packet ranges (limited to 6 by packet size of 37).
   3886     0x06,
   3887     // {missing packet delta, further missing packets in range}
   3888     // 6 nack ranges
   3889     0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
   3890     // 0 revived packets.
   3891     0x00,
   3892   };
   3893 
   3894   scoped_ptr<QuicPacket> data(
   3895       framer_.BuildDataPacket(header, frames, 37u).packet);
   3896   ASSERT_TRUE(data != NULL);
   3897   // Expect 1 byte unused since at least 2 bytes are needed to fit more nacks.
   3898   EXPECT_EQ(36u, data->length());
   3899   test::CompareCharArraysWithHexError("constructed packet",
   3900                                       data->data(), data->length(),
   3901                                       AsChars(packet), arraysize(packet));
   3902 }
   3903 
   3904 TEST_P(QuicFramerTest, BuildCongestionFeedbackFramePacketTCP) {
   3905   QuicPacketHeader header;
   3906   header.public_header.connection_id = GG_UINT64_C(0xFEDCBA9876543210);
   3907   header.public_header.reset_flag = false;
   3908   header.public_header.version_flag = false;
   3909   header.fec_flag = false;
   3910   header.entropy_flag = false;
   3911   header.packet_sequence_number = GG_UINT64_C(0x123456789ABC);
   3912   header.fec_group = 0;
   3913 
   3914   QuicCongestionFeedbackFrame congestion_feedback_frame;
   3915   congestion_feedback_frame.type = kTCP;
   3916   congestion_feedback_frame.tcp.receive_window = 0x4030;
   3917 
   3918   QuicFrames frames;
   3919   frames.push_back(QuicFrame(&congestion_feedback_frame));
   3920 
   3921   unsigned char packet[] = {
   3922     // public flags (8 byte connection_id)
   3923     0x3C,
   3924     // connection_id
   3925     0x10, 0x32, 0x54, 0x76,
   3926     0x98, 0xBA, 0xDC, 0xFE,
   3927     // packet sequence number
   3928     0xBC, 0x9A, 0x78, 0x56,
   3929     0x34, 0x12,
   3930     // private flags
   3931     0x00,
   3932 
   3933     // frame type (congestion feedback frame)
   3934     0x20,
   3935     // congestion feedback type (TCP)
   3936     0x00,
   3937     // TCP receive window
   3938     0x03, 0x04,
   3939   };
   3940 
   3941   scoped_ptr<QuicPacket> data(BuildDataPacket(header, frames));
   3942   ASSERT_TRUE(data != NULL);
   3943 
   3944   test::CompareCharArraysWithHexError("constructed packet",
   3945                                       data->data(), data->length(),
   3946                                       AsChars(packet), arraysize(packet));
   3947 }
   3948 
   3949 TEST_P(QuicFramerTest, BuildCongestionFeedbackFramePacketInterArrival) {
   3950   QuicPacketHeader header;
   3951   header.public_header.connection_id = GG_UINT64_C(0xFEDCBA9876543210);
   3952   header.public_header.reset_flag = false;
   3953   header.public_header.version_flag = false;
   3954   header.fec_flag = false;
   3955   header.entropy_flag = false;
   3956   header.packet_sequence_number = GG_UINT64_C(0x123456789ABC);
   3957   header.fec_group = 0;
   3958 
   3959   QuicCongestionFeedbackFrame frame;
   3960   frame.type = kInterArrival;
   3961   frame.inter_arrival.received_packet_times.insert(
   3962       make_pair(GG_UINT64_C(0x0123456789ABA),
   3963                 start_.Add(QuicTime::Delta::FromMicroseconds(
   3964                     GG_UINT64_C(0x07E1D2C3B4A59687)))));
   3965   frame.inter_arrival.received_packet_times.insert(
   3966       make_pair(GG_UINT64_C(0x0123456789ABB),
   3967                 start_.Add(QuicTime::Delta::FromMicroseconds(
   3968                     GG_UINT64_C(0x07E1D2C3B4A59688)))));
   3969   frame.inter_arrival.received_packet_times.insert(
   3970       make_pair(GG_UINT64_C(0x0123456789ABD),
   3971                 start_.Add(QuicTime::Delta::FromMicroseconds(
   3972                     GG_UINT64_C(0x07E1D2C3B4A59689)))));
   3973   QuicFrames frames;
   3974   frames.push_back(QuicFrame(&frame));
   3975 
   3976   unsigned char packet[] = {
   3977     // public flags (8 byte connection_id)
   3978     0x3C,
   3979     // connection_id
   3980     0x10, 0x32, 0x54, 0x76,
   3981     0x98, 0xBA, 0xDC, 0xFE,
   3982     // packet sequence number
   3983     0xBC, 0x9A, 0x78, 0x56,
   3984     0x34, 0x12,
   3985     // private flags
   3986     0x00,
   3987 
   3988     // frame type (congestion feedback frame)
   3989     0x20,
   3990     // congestion feedback type (inter arrival)
   3991     0x01,
   3992     // num received packets
   3993     0x03,
   3994     // lowest sequence number
   3995     0xBA, 0x9A, 0x78, 0x56,
   3996     0x34, 0x12,
   3997     // receive time
   3998     0x87, 0x96, 0xA5, 0xB4,
   3999     0xC3, 0xD2, 0xE1, 0x07,
   4000     // sequence delta
   4001     0x01, 0x00,
   4002     // time delta
   4003     0x01, 0x00, 0x00, 0x00,
   4004     // sequence delta (skip one packet)
   4005     0x03, 0x00,
   4006     // time delta
   4007     0x02, 0x00, 0x00, 0x00,
   4008   };
   4009 
   4010   scoped_ptr<QuicPacket> data(BuildDataPacket(header, frames));
   4011   ASSERT_TRUE(data != NULL);
   4012 
   4013   test::CompareCharArraysWithHexError("constructed packet",
   4014                                       data->data(), data->length(),
   4015                                       AsChars(packet), arraysize(packet));
   4016 }
   4017 
   4018 TEST_P(QuicFramerTest, BuildStopWaitingPacket) {
   4019   if (version_ <= QUIC_VERSION_15) {
   4020     return;
   4021   }
   4022   QuicPacketHeader header;
   4023   header.public_header.connection_id = GG_UINT64_C(0xFEDCBA9876543210);
   4024   header.public_header.reset_flag = false;
   4025   header.public_header.version_flag = false;
   4026   header.fec_flag = false;
   4027   header.entropy_flag = true;
   4028   header.packet_sequence_number = GG_UINT64_C(0x770123456789AA8);
   4029   header.fec_group = 0;
   4030 
   4031   QuicStopWaitingFrame stop_waiting_frame;
   4032   stop_waiting_frame.entropy_hash = 0x14;
   4033   stop_waiting_frame.least_unacked = GG_UINT64_C(0x770123456789AA0);
   4034 
   4035   QuicFrames frames;
   4036   frames.push_back(QuicFrame(&stop_waiting_frame));
   4037 
   4038   unsigned char packet[] = {
   4039     // public flags (8 byte connection_id)
   4040     0x3C,
   4041     // connection_id
   4042     0x10, 0x32, 0x54, 0x76,
   4043     0x98, 0xBA, 0xDC, 0xFE,
   4044     // packet sequence number
   4045     0xA8, 0x9A, 0x78, 0x56,
   4046     0x34, 0x12,
   4047     // private flags (entropy)
   4048     0x01,
   4049 
   4050     // frame type (stop waiting frame)
   4051     0x06,
   4052     // entropy hash of sent packets till least awaiting - 1.
   4053     0x14,
   4054     // least packet sequence number awaiting an ack, delta from sequence number.
   4055     0x08, 0x00, 0x00, 0x00,
   4056     0x00, 0x00,
   4057   };
   4058 
   4059   scoped_ptr<QuicPacket> data(BuildDataPacket(header, frames));
   4060   ASSERT_TRUE(data != NULL);
   4061 
   4062   test::CompareCharArraysWithHexError("constructed packet",
   4063                                       data->data(), data->length(),
   4064                                       AsChars(packet), arraysize(packet));
   4065 }
   4066 
   4067 TEST_P(QuicFramerTest, BuildCongestionFeedbackFramePacketFixRate) {
   4068   QuicPacketHeader header;
   4069   header.public_header.connection_id = GG_UINT64_C(0xFEDCBA9876543210);
   4070   header.public_header.reset_flag = false;
   4071   header.public_header.version_flag = false;
   4072   header.fec_flag = false;
   4073   header.entropy_flag = false;
   4074   header.packet_sequence_number = GG_UINT64_C(0x123456789ABC);
   4075   header.fec_group = 0;
   4076 
   4077   QuicCongestionFeedbackFrame congestion_feedback_frame;
   4078   congestion_feedback_frame.type = kFixRate;
   4079   congestion_feedback_frame.fix_rate.bitrate
   4080       = QuicBandwidth::FromBytesPerSecond(0x04030201);
   4081 
   4082   QuicFrames frames;
   4083   frames.push_back(QuicFrame(&congestion_feedback_frame));
   4084 
   4085   unsigned char packet[] = {
   4086     // public flags (8 byte connection_id)
   4087     0x3C,
   4088     // connection_id
   4089     0x10, 0x32, 0x54, 0x76,
   4090     0x98, 0xBA, 0xDC, 0xFE,
   4091     // packet sequence number
   4092     0xBC, 0x9A, 0x78, 0x56,
   4093     0x34, 0x12,
   4094     // private flags
   4095     0x00,
   4096 
   4097     // frame type (congestion feedback frame)
   4098     0x20,
   4099     // congestion feedback type (fix rate)
   4100     0x02,
   4101     // bitrate_in_bytes_per_second;
   4102     0x01, 0x02, 0x03, 0x04,
   4103   };
   4104 
   4105   scoped_ptr<QuicPacket> data(BuildDataPacket(header, frames));
   4106   ASSERT_TRUE(data != NULL);
   4107 
   4108   test::CompareCharArraysWithHexError("constructed packet",
   4109                                       data->data(), data->length(),
   4110                                       AsChars(packet), arraysize(packet));
   4111 }
   4112 
   4113 TEST_P(QuicFramerTest, BuildCongestionFeedbackFramePacketInvalidFeedback) {
   4114   QuicPacketHeader header;
   4115   header.public_header.connection_id = GG_UINT64_C(0xFEDCBA9876543210);
   4116   header.public_header.reset_flag = false;
   4117   header.public_header.version_flag = false;
   4118   header.fec_flag = false;
   4119   header.entropy_flag = false;
   4120   header.packet_sequence_number = GG_UINT64_C(0x123456789ABC);
   4121   header.fec_group = 0;
   4122 
   4123   QuicCongestionFeedbackFrame congestion_feedback_frame;
   4124   congestion_feedback_frame.type =
   4125       static_cast<CongestionFeedbackType>(kFixRate + 1);
   4126 
   4127   QuicFrames frames;
   4128   frames.push_back(QuicFrame(&congestion_feedback_frame));
   4129 
   4130   scoped_ptr<QuicPacket> data;
   4131   EXPECT_DFATAL(
   4132       data.reset(BuildDataPacket(header, frames)),
   4133       "AppendCongestionFeedbackFrame failed");
   4134   ASSERT_TRUE(data == NULL);
   4135 }
   4136 
   4137 TEST_P(QuicFramerTest, BuildRstFramePacketQuic) {
   4138   QuicPacketHeader header;
   4139   header.public_header.connection_id = GG_UINT64_C(0xFEDCBA9876543210);
   4140   header.public_header.reset_flag = false;
   4141   header.public_header.version_flag = false;
   4142   header.fec_flag = false;
   4143   header.entropy_flag = false;
   4144   header.packet_sequence_number = GG_UINT64_C(0x123456789ABC);
   4145   header.fec_group = 0;
   4146 
   4147   QuicRstStreamFrame rst_frame;
   4148   rst_frame.stream_id = 0x01020304;
   4149   rst_frame.error_code = static_cast<QuicRstStreamErrorCode>(0x05060708);
   4150   rst_frame.error_details = "because I can";
   4151   rst_frame.byte_offset = 0x0807060504030201;
   4152 
   4153   unsigned char packet[] = {
   4154     // public flags (8 byte connection_id)
   4155     0x3C,
   4156     // connection_id
   4157     0x10, 0x32, 0x54, 0x76,
   4158     0x98, 0xBA, 0xDC, 0xFE,
   4159     // packet sequence number
   4160     0xBC, 0x9A, 0x78, 0x56,
   4161     0x34, 0x12,
   4162     // private flags
   4163     0x00,
   4164 
   4165     // frame type (rst stream frame)
   4166     0x01,
   4167     // stream id
   4168     0x04, 0x03, 0x02, 0x01,
   4169     // sent byte offset
   4170     0x01, 0x02, 0x03, 0x04,
   4171     0x05, 0x06, 0x07, 0x08,
   4172     // error code
   4173     0x08, 0x07, 0x06, 0x05,
   4174     // error details length
   4175     0x0d, 0x00,
   4176     // error details
   4177     'b',  'e',  'c',  'a',
   4178     'u',  's',  'e',  ' ',
   4179     'I',  ' ',  'c',  'a',
   4180     'n',
   4181   };
   4182 
   4183   QuicFrames frames;
   4184   frames.push_back(QuicFrame(&rst_frame));
   4185 
   4186   scoped_ptr<QuicPacket> data(BuildDataPacket(header, frames));
   4187   ASSERT_TRUE(data != NULL);
   4188 
   4189   test::CompareCharArraysWithHexError("constructed packet",
   4190                                       data->data(), data->length(),
   4191                                       AsChars(packet), arraysize(packet));
   4192 }
   4193 
   4194 TEST_P(QuicFramerTest, BuildCloseFramePacket) {
   4195   QuicPacketHeader header;
   4196   header.public_header.connection_id = GG_UINT64_C(0xFEDCBA9876543210);
   4197   header.public_header.reset_flag = false;
   4198   header.public_header.version_flag = false;
   4199   header.fec_flag = false;
   4200   header.entropy_flag = true;
   4201   header.packet_sequence_number = GG_UINT64_C(0x123456789ABC);
   4202   header.fec_group = 0;
   4203 
   4204   QuicConnectionCloseFrame close_frame;
   4205   close_frame.error_code = static_cast<QuicErrorCode>(0x05060708);
   4206   close_frame.error_details = "because I can";
   4207 
   4208   QuicFrames frames;
   4209   frames.push_back(QuicFrame(&close_frame));
   4210 
   4211   unsigned char packet[] = {
   4212     // public flags (8 byte connection_id)
   4213     0x3C,
   4214     // connection_id
   4215     0x10, 0x32, 0x54, 0x76,
   4216     0x98, 0xBA, 0xDC, 0xFE,
   4217     // packet sequence number
   4218     0xBC, 0x9A, 0x78, 0x56,
   4219     0x34, 0x12,
   4220     // private flags (entropy)
   4221     0x01,
   4222 
   4223     // frame type (connection close frame)
   4224     0x02,
   4225     // error code
   4226     0x08, 0x07, 0x06, 0x05,
   4227     // error details length
   4228     0x0d, 0x00,
   4229     // error details
   4230     'b',  'e',  'c',  'a',
   4231     'u',  's',  'e',  ' ',
   4232     'I',  ' ',  'c',  'a',
   4233     'n',
   4234   };
   4235 
   4236   scoped_ptr<QuicPacket> data(BuildDataPacket(header, frames));
   4237   ASSERT_TRUE(data != NULL);
   4238 
   4239   test::CompareCharArraysWithHexError("constructed packet",
   4240                                       data->data(), data->length(),
   4241                                       AsChars(packet), arraysize(packet));
   4242 }
   4243 
   4244 TEST_P(QuicFramerTest, BuildGoAwayPacket) {
   4245   QuicPacketHeader header;
   4246   header.public_header.connection_id = GG_UINT64_C(0xFEDCBA9876543210);
   4247   header.public_header.reset_flag = false;
   4248   header.public_header.version_flag = false;
   4249   header.fec_flag = false;
   4250   header.entropy_flag = true;
   4251   header.packet_sequence_number = GG_UINT64_C(0x123456789ABC);
   4252   header.fec_group = 0;
   4253 
   4254   QuicGoAwayFrame goaway_frame;
   4255   goaway_frame.error_code = static_cast<QuicErrorCode>(0x05060708);
   4256   goaway_frame.last_good_stream_id = 0x01020304;
   4257   goaway_frame.reason_phrase = "because I can";
   4258 
   4259   QuicFrames frames;
   4260   frames.push_back(QuicFrame(&goaway_frame));
   4261 
   4262   unsigned char packet[] = {
   4263     // public flags (8 byte connection_id)
   4264     0x3C,
   4265     // connection_id
   4266     0x10, 0x32, 0x54, 0x76,
   4267     0x98, 0xBA, 0xDC, 0xFE,
   4268     // packet sequence number
   4269     0xBC, 0x9A, 0x78, 0x56,
   4270     0x34, 0x12,
   4271     // private flags(entropy)
   4272     0x01,
   4273 
   4274     // frame type (go away frame)
   4275     0x03,
   4276     // error code
   4277     0x08, 0x07, 0x06, 0x05,
   4278     // stream id
   4279     0x04, 0x03, 0x02, 0x01,
   4280     // error details length
   4281     0x0d, 0x00,
   4282     // error details
   4283     'b',  'e',  'c',  'a',
   4284     'u',  's',  'e',  ' ',
   4285     'I',  ' ',  'c',  'a',
   4286     'n',
   4287   };
   4288 
   4289   scoped_ptr<QuicPacket> data(BuildDataPacket(header, frames));
   4290   ASSERT_TRUE(data != NULL);
   4291 
   4292   test::CompareCharArraysWithHexError("constructed packet",
   4293                                       data->data(), data->length(),
   4294                                       AsChars(packet), arraysize(packet));
   4295 }
   4296 
   4297 TEST_P(QuicFramerTest, BuildWindowUpdatePacket) {
   4298   QuicPacketHeader header;
   4299   header.public_header.connection_id = GG_UINT64_C(0xFEDCBA9876543210);
   4300   header.public_header.reset_flag = false;
   4301   header.public_header.version_flag = false;
   4302   header.fec_flag = false;
   4303   header.entropy_flag = true;
   4304   header.packet_sequence_number = GG_UINT64_C(0x123456789ABC);
   4305   header.fec_group = 0;
   4306 
   4307   QuicWindowUpdateFrame window_update_frame;
   4308   window_update_frame.stream_id = 0x01020304;
   4309   window_update_frame.byte_offset = 0x1122334455667788;
   4310 
   4311   QuicFrames frames;
   4312   frames.push_back(QuicFrame(&window_update_frame));
   4313 
   4314   unsigned char packet[] = {
   4315     // public flags (8 byte connection_id)
   4316     0x3C,
   4317     // connection_id
   4318     0x10, 0x32, 0x54, 0x76,
   4319     0x98, 0xBA, 0xDC, 0xFE,
   4320     // packet sequence number
   4321     0xBC, 0x9A, 0x78, 0x56,
   4322     0x34, 0x12,
   4323     // private flags(entropy)
   4324     0x01,
   4325 
   4326     // frame type (window update frame)
   4327     0x04,
   4328     // stream id
   4329     0x04, 0x03, 0x02, 0x01,
   4330     // byte offset
   4331     0x88, 0x77, 0x66, 0x55,
   4332     0x44, 0x33, 0x22, 0x11,
   4333   };
   4334 
   4335   scoped_ptr<QuicPacket> data(BuildDataPacket(header, frames));
   4336   ASSERT_TRUE(data != NULL);
   4337 
   4338   test::CompareCharArraysWithHexError("constructed packet", data->data(),
   4339                                       data->length(), AsChars(packet),
   4340                                       arraysize(packet));
   4341 }
   4342 
   4343 TEST_P(QuicFramerTest, BuildBlockedPacket) {
   4344   QuicPacketHeader header;
   4345   header.public_header.connection_id = GG_UINT64_C(0xFEDCBA9876543210);
   4346   header.public_header.reset_flag = false;
   4347   header.public_header.version_flag = false;
   4348   header.fec_flag = false;
   4349   header.entropy_flag = true;
   4350   header.packet_sequence_number = GG_UINT64_C(0x123456789ABC);
   4351   header.fec_group = 0;
   4352 
   4353   QuicBlockedFrame blocked_frame;
   4354   blocked_frame.stream_id = 0x01020304;
   4355 
   4356   QuicFrames frames;
   4357   frames.push_back(QuicFrame(&blocked_frame));
   4358 
   4359   unsigned char packet[] = {
   4360     // public flags (8 byte connection_id)
   4361     0x3C,
   4362     // connection_id
   4363     0x10, 0x32, 0x54, 0x76,
   4364     0x98, 0xBA, 0xDC, 0xFE,
   4365     // packet sequence number
   4366     0xBC, 0x9A, 0x78, 0x56,
   4367     0x34, 0x12,
   4368     // private flags(entropy)
   4369     0x01,
   4370 
   4371     // frame type (blocked frame)
   4372     0x05,
   4373     // stream id
   4374     0x04, 0x03, 0x02, 0x01,
   4375   };
   4376 
   4377   scoped_ptr<QuicPacket> data(BuildDataPacket(header, frames));
   4378   ASSERT_TRUE(data != NULL);
   4379 
   4380   test::CompareCharArraysWithHexError("constructed packet", data->data(),
   4381                                       data->length(), AsChars(packet),
   4382                                       arraysize(packet));
   4383 }
   4384 
   4385 TEST_P(QuicFramerTest, BuildPingPacket) {
   4386   QuicPacketHeader header;
   4387   header.public_header.connection_id = GG_UINT64_C(0xFEDCBA9876543210);
   4388   header.public_header.reset_flag = false;
   4389   header.public_header.version_flag = false;
   4390   header.fec_flag = false;
   4391   header.entropy_flag = true;
   4392   header.packet_sequence_number = GG_UINT64_C(0x123456789ABC);
   4393   header.fec_group = 0;
   4394 
   4395   QuicPingFrame ping_frame;
   4396 
   4397   QuicFrames frames;
   4398   frames.push_back(QuicFrame(&ping_frame));
   4399 
   4400   unsigned char packet[] = {
   4401     // public flags (8 byte connection_id)
   4402     0x3C,
   4403     // connection_id
   4404     0x10, 0x32, 0x54, 0x76,
   4405     0x98, 0xBA, 0xDC, 0xFE,
   4406     // packet sequence number
   4407     0xBC, 0x9A, 0x78, 0x56,
   4408     0x34, 0x12,
   4409     // private flags(entropy)
   4410     0x01,
   4411 
   4412     // frame type (ping frame)
   4413     0x07,
   4414   };
   4415 
   4416   if (version_ > QUIC_VERSION_17) {
   4417     scoped_ptr<QuicPacket> data(BuildDataPacket(header, frames));
   4418     ASSERT_TRUE(data != NULL);
   4419 
   4420     test::CompareCharArraysWithHexError("constructed packet", data->data(),
   4421                                         data->length(), AsChars(packet),
   4422                                         arraysize(packet));
   4423   } else {
   4424     string expected_error =
   4425         "Attempt to add a PingFrame in " + QuicVersionToString(version_);
   4426     EXPECT_DFATAL(BuildDataPacket(header, frames),
   4427                   expected_error);
   4428     return;
   4429   }
   4430 }
   4431 
   4432 TEST_P(QuicFramerTest, BuildPublicResetPacket) {
   4433   QuicPublicResetPacket reset_packet;
   4434   reset_packet.public_header.connection_id = GG_UINT64_C(0xFEDCBA9876543210);
   4435   reset_packet.public_header.reset_flag = true;
   4436   reset_packet.public_header.version_flag = false;
   4437   reset_packet.rejected_sequence_number = GG_UINT64_C(0x123456789ABC);
   4438   reset_packet.nonce_proof = GG_UINT64_C(0xABCDEF0123456789);
   4439 
   4440   unsigned char packet[] = {
   4441     // public flags (public reset, 8 byte ConnectionId)
   4442     0x0E,
   4443     // connection_id
   4444     0x10, 0x32, 0x54, 0x76,
   4445     0x98, 0xBA, 0xDC, 0xFE,
   4446     // message tag (kPRST)
   4447     'P', 'R', 'S', 'T',
   4448     // num_entries (2) + padding
   4449     0x02, 0x00, 0x00, 0x00,
   4450     // tag kRNON
   4451     'R', 'N', 'O', 'N',
   4452     // end offset 8
   4453     0x08, 0x00, 0x00, 0x00,
   4454     // tag kRSEQ
   4455     'R', 'S', 'E', 'Q',
   4456     // end offset 16
   4457     0x10, 0x00, 0x00, 0x00,
   4458     // nonce proof
   4459     0x89, 0x67, 0x45, 0x23,
   4460     0x01, 0xEF, 0xCD, 0xAB,
   4461     // rejected sequence number
   4462     0xBC, 0x9A, 0x78, 0x56,
   4463     0x34, 0x12, 0x00, 0x00,
   4464   };
   4465 
   4466   scoped_ptr<QuicEncryptedPacket> data(
   4467       framer_.BuildPublicResetPacket(reset_packet));
   4468   ASSERT_TRUE(data != NULL);
   4469 
   4470   test::CompareCharArraysWithHexError("constructed packet",
   4471                                       data->data(), data->length(),
   4472                                       AsChars(packet), arraysize(packet));
   4473 }
   4474 
   4475 TEST_P(QuicFramerTest, BuildPublicResetPacketWithClientAddress) {
   4476   QuicPublicResetPacket reset_packet;
   4477   reset_packet.public_header.connection_id = GG_UINT64_C(0xFEDCBA9876543210);
   4478   reset_packet.public_header.reset_flag = true;
   4479   reset_packet.public_header.version_flag = false;
   4480   reset_packet.rejected_sequence_number = GG_UINT64_C(0x123456789ABC);
   4481   reset_packet.nonce_proof = GG_UINT64_C(0xABCDEF0123456789);
   4482   reset_packet.client_address = IPEndPoint(Loopback4(), 0x1234);
   4483 
   4484   unsigned char packet[] = {
   4485     // public flags (public reset, 8 byte ConnectionId)
   4486     0x0E,
   4487     // connection_id
   4488     0x10, 0x32, 0x54, 0x76,
   4489     0x98, 0xBA, 0xDC, 0xFE,
   4490     // message tag (kPRST)
   4491     'P', 'R', 'S', 'T',
   4492     // num_entries (3) + padding
   4493     0x03, 0x00, 0x00, 0x00,
   4494     // tag kRNON
   4495     'R', 'N', 'O', 'N',
   4496     // end offset 8
   4497     0x08, 0x00, 0x00, 0x00,
   4498     // tag kRSEQ
   4499     'R', 'S', 'E', 'Q',
   4500     // end offset 16
   4501     0x10, 0x00, 0x00, 0x00,
   4502     // tag kCADR
   4503     'C', 'A', 'D', 'R',
   4504     // end offset 24
   4505     0x18, 0x00, 0x00, 0x00,
   4506     // nonce proof
   4507     0x89, 0x67, 0x45, 0x23,
   4508     0x01, 0xEF, 0xCD, 0xAB,
   4509     // rejected sequence number
   4510     0xBC, 0x9A, 0x78, 0x56,
   4511     0x34, 0x12, 0x00, 0x00,
   4512     // client address
   4513     0x02, 0x00,
   4514     0x7F, 0x00, 0x00, 0x01,
   4515     0x34, 0x12,
   4516   };
   4517 
   4518   scoped_ptr<QuicEncryptedPacket> data(
   4519       framer_.BuildPublicResetPacket(reset_packet));
   4520   ASSERT_TRUE(data != NULL);
   4521 
   4522   test::CompareCharArraysWithHexError("constructed packet",
   4523                                       data->data(), data->length(),
   4524                                       AsChars(packet), arraysize(packet));
   4525 }
   4526 
   4527 TEST_P(QuicFramerTest, BuildFecPacket) {
   4528   QuicPacketHeader header;
   4529   header.public_header.connection_id = GG_UINT64_C(0xFEDCBA9876543210);
   4530   header.public_header.reset_flag = false;
   4531   header.public_header.version_flag = false;
   4532   header.fec_flag = true;
   4533   header.entropy_flag = true;
   4534   header.packet_sequence_number = (GG_UINT64_C(0x123456789ABC));
   4535   header.is_in_fec_group = IN_FEC_GROUP;
   4536   header.fec_group = GG_UINT64_C(0x123456789ABB);;
   4537 
   4538   QuicFecData fec_data;
   4539   fec_data.fec_group = 1;
   4540   fec_data.redundancy = "abcdefghijklmnop";
   4541 
   4542   unsigned char packet[] = {
   4543     // public flags (8 byte connection_id)
   4544     0x3C,
   4545     // connection_id
   4546     0x10, 0x32, 0x54, 0x76,
   4547     0x98, 0xBA, 0xDC, 0xFE,
   4548     // packet sequence number
   4549     0xBC, 0x9A, 0x78, 0x56,
   4550     0x34, 0x12,
   4551     // private flags (entropy & fec group & fec packet)
   4552     0x07,
   4553     // first fec protected packet offset
   4554     0x01,
   4555 
   4556     // redundancy
   4557     'a',  'b',  'c',  'd',
   4558     'e',  'f',  'g',  'h',
   4559     'i',  'j',  'k',  'l',
   4560     'm',  'n',  'o',  'p',
   4561   };
   4562 
   4563   scoped_ptr<QuicPacket> data(
   4564       framer_.BuildFecPacket(header, fec_data).packet);
   4565   ASSERT_TRUE(data != NULL);
   4566 
   4567   test::CompareCharArraysWithHexError("constructed packet",
   4568                                       data->data(), data->length(),
   4569                                       AsChars(packet), arraysize(packet));
   4570 }
   4571 
   4572 TEST_P(QuicFramerTest, EncryptPacket) {
   4573   QuicPacketSequenceNumber sequence_number = GG_UINT64_C(0x123456789ABC);
   4574   unsigned char packet[] = {
   4575     // public flags (8 byte connection_id)
   4576     0x3C,
   4577     // connection_id
   4578     0x10, 0x32, 0x54, 0x76,
   4579     0x98, 0xBA, 0xDC, 0xFE,
   4580     // packet sequence number
   4581     0xBC, 0x9A, 0x78, 0x56,
   4582     0x34, 0x12,
   4583     // private flags (fec group & fec packet)
   4584     0x06,
   4585     // first fec protected packet offset
   4586     0x01,
   4587 
   4588     // redundancy
   4589     'a',  'b',  'c',  'd',
   4590     'e',  'f',  'g',  'h',
   4591     'i',  'j',  'k',  'l',
   4592     'm',  'n',  'o',  'p',
   4593   };
   4594 
   4595   scoped_ptr<QuicPacket> raw(
   4596       QuicPacket::NewDataPacket(AsChars(packet), arraysize(packet), false,
   4597                                 PACKET_8BYTE_CONNECTION_ID, !kIncludeVersion,
   4598                                 PACKET_6BYTE_SEQUENCE_NUMBER));
   4599   scoped_ptr<QuicEncryptedPacket> encrypted(
   4600       framer_.EncryptPacket(ENCRYPTION_NONE, sequence_number, *raw));
   4601 
   4602   ASSERT_TRUE(encrypted.get() != NULL);
   4603   EXPECT_TRUE(CheckEncryption(sequence_number, raw.get()));
   4604 }
   4605 
   4606 TEST_P(QuicFramerTest, EncryptPacketWithVersionFlag) {
   4607   QuicPacketSequenceNumber sequence_number = GG_UINT64_C(0x123456789ABC);
   4608   unsigned char packet[] = {
   4609     // public flags (version, 8 byte connection_id)
   4610     0x3D,
   4611     // connection_id
   4612     0x10, 0x32, 0x54, 0x76,
   4613     0x98, 0xBA, 0xDC, 0xFE,
   4614     // version tag
   4615     'Q', '.', '1', '0',
   4616     // packet sequence number
   4617     0xBC, 0x9A, 0x78, 0x56,
   4618     0x34, 0x12,
   4619     // private flags (fec group & fec flags)
   4620     0x06,
   4621     // first fec protected packet offset
   4622     0x01,
   4623 
   4624     // redundancy
   4625     'a',  'b',  'c',  'd',
   4626     'e',  'f',  'g',  'h',
   4627     'i',  'j',  'k',  'l',
   4628     'm',  'n',  'o',  'p',
   4629   };
   4630 
   4631   scoped_ptr<QuicPacket> raw(
   4632       QuicPacket::NewDataPacket(AsChars(packet), arraysize(packet), false,
   4633                                 PACKET_8BYTE_CONNECTION_ID, kIncludeVersion,
   4634                                 PACKET_6BYTE_SEQUENCE_NUMBER));
   4635   scoped_ptr<QuicEncryptedPacket> encrypted(
   4636       framer_.EncryptPacket(ENCRYPTION_NONE, sequence_number, *raw));
   4637 
   4638   ASSERT_TRUE(encrypted.get() != NULL);
   4639   EXPECT_TRUE(CheckEncryption(sequence_number, raw.get()));
   4640 }
   4641 
   4642 TEST_P(QuicFramerTest, AckTruncationLargePacket) {
   4643   if (framer_.version() <= QUIC_VERSION_15) {
   4644     return;
   4645   }
   4646   QuicPacketHeader header;
   4647   header.public_header.connection_id = GG_UINT64_C(0xFEDCBA9876543210);
   4648   header.public_header.reset_flag = false;
   4649   header.public_header.version_flag = false;
   4650   header.fec_flag = false;
   4651   header.entropy_flag = false;
   4652   header.packet_sequence_number = GG_UINT64_C(0x123456789ABC);
   4653   header.fec_group = 0;
   4654 
   4655   // Create a packet with just the ack.
   4656   QuicAckFrame ack_frame = MakeAckFrameWithNackRanges(300, 0u);
   4657   QuicFrame frame;
   4658   frame.type = ACK_FRAME;
   4659   frame.ack_frame = &ack_frame;
   4660   QuicFrames frames;
   4661   frames.push_back(frame);
   4662 
   4663   // Build an ack packet with truncation due to limit in number of nack ranges.
   4664   scoped_ptr<QuicPacket> raw_ack_packet(
   4665       framer_.BuildDataPacket(header, frames, kMaxPacketSize).packet);
   4666   ASSERT_TRUE(raw_ack_packet != NULL);
   4667   scoped_ptr<QuicEncryptedPacket> ack_packet(
   4668       framer_.EncryptPacket(ENCRYPTION_NONE, header.packet_sequence_number,
   4669                             *raw_ack_packet));
   4670   // Now make sure we can turn our ack packet back into an ack frame.
   4671   ASSERT_TRUE(framer_.ProcessPacket(*ack_packet));
   4672   ASSERT_EQ(1u, visitor_.ack_frames_.size());
   4673   QuicAckFrame& processed_ack_frame = *visitor_.ack_frames_[0];
   4674   EXPECT_TRUE(processed_ack_frame.received_info.is_truncated);
   4675   EXPECT_EQ(510u, processed_ack_frame.received_info.largest_observed);
   4676   ASSERT_EQ(255u, processed_ack_frame.received_info.missing_packets.size());
   4677   SequenceNumberSet::const_iterator missing_iter =
   4678       processed_ack_frame.received_info.missing_packets.begin();
   4679   EXPECT_EQ(1u, *missing_iter);
   4680   SequenceNumberSet::const_reverse_iterator last_missing_iter =
   4681       processed_ack_frame.received_info.missing_packets.rbegin();
   4682   EXPECT_EQ(509u, *last_missing_iter);
   4683 }
   4684 
   4685 TEST_P(QuicFramerTest, AckTruncationSmallPacket) {
   4686   if (framer_.version() <= QUIC_VERSION_15) {
   4687     return;
   4688   }
   4689   QuicPacketHeader header;
   4690   header.public_header.connection_id = GG_UINT64_C(0xFEDCBA9876543210);
   4691   header.public_header.reset_flag = false;
   4692   header.public_header.version_flag = false;
   4693   header.fec_flag = false;
   4694   header.entropy_flag = false;
   4695   header.packet_sequence_number = GG_UINT64_C(0x123456789ABC);
   4696   header.fec_group = 0;
   4697 
   4698   // Create a packet with just the ack.
   4699   QuicAckFrame ack_frame = MakeAckFrameWithNackRanges(300, 0u);
   4700   QuicFrame frame;
   4701   frame.type = ACK_FRAME;
   4702   frame.ack_frame = &ack_frame;
   4703   QuicFrames frames;
   4704   frames.push_back(frame);
   4705 
   4706   // Build an ack packet with truncation due to limit in number of nack ranges.
   4707   scoped_ptr<QuicPacket> raw_ack_packet(
   4708       framer_.BuildDataPacket(header, frames, 500).packet);
   4709   ASSERT_TRUE(raw_ack_packet != NULL);
   4710   scoped_ptr<QuicEncryptedPacket> ack_packet(
   4711       framer_.EncryptPacket(ENCRYPTION_NONE, header.packet_sequence_number,
   4712                             *raw_ack_packet));
   4713   // Now make sure we can turn our ack packet back into an ack frame.
   4714   ASSERT_TRUE(framer_.ProcessPacket(*ack_packet));
   4715   ASSERT_EQ(1u, visitor_.ack_frames_.size());
   4716   QuicAckFrame& processed_ack_frame = *visitor_.ack_frames_[0];
   4717   EXPECT_TRUE(processed_ack_frame.received_info.is_truncated);
   4718   EXPECT_EQ(476u, processed_ack_frame.received_info.largest_observed);
   4719   ASSERT_EQ(238u, processed_ack_frame.received_info.missing_packets.size());
   4720   SequenceNumberSet::const_iterator missing_iter =
   4721       processed_ack_frame.received_info.missing_packets.begin();
   4722   EXPECT_EQ(1u, *missing_iter);
   4723   SequenceNumberSet::const_reverse_iterator last_missing_iter =
   4724       processed_ack_frame.received_info.missing_packets.rbegin();
   4725   EXPECT_EQ(475u, *last_missing_iter);
   4726 }
   4727 
   4728 TEST_P(QuicFramerTest, Truncation15) {
   4729   if (framer_.version() > QUIC_VERSION_15) {
   4730     return;
   4731   }
   4732   QuicPacketHeader header;
   4733   header.public_header.connection_id = GG_UINT64_C(0xFEDCBA9876543210);
   4734   header.public_header.reset_flag = false;
   4735   header.public_header.version_flag = false;
   4736   header.fec_flag = false;
   4737   header.entropy_flag = false;
   4738   header.packet_sequence_number = GG_UINT64_C(0x123456789ABC);
   4739   header.fec_group = 0;
   4740 
   4741   QuicAckFrame ack_frame;
   4742   ack_frame.received_info.largest_observed = 601;
   4743   ack_frame.sent_info.least_unacked = header.packet_sequence_number - 1;
   4744   for (uint64 i = 1; i < ack_frame.received_info.largest_observed; i += 2) {
   4745     ack_frame.received_info.missing_packets.insert(i);
   4746   }
   4747 
   4748   // Create a packet with just the ack.
   4749   QuicFrame frame;
   4750   frame.type = ACK_FRAME;
   4751   frame.ack_frame = &ack_frame;
   4752   QuicFrames frames;
   4753   frames.push_back(frame);
   4754 
   4755   scoped_ptr<QuicPacket> raw_ack_packet(BuildDataPacket(header, frames));
   4756   ASSERT_TRUE(raw_ack_packet != NULL);
   4757 
   4758   scoped_ptr<QuicEncryptedPacket> ack_packet(
   4759       framer_.EncryptPacket(ENCRYPTION_NONE, header.packet_sequence_number,
   4760                             *raw_ack_packet));
   4761 
   4762   // Now make sure we can turn our ack packet back into an ack frame.
   4763   ASSERT_TRUE(framer_.ProcessPacket(*ack_packet));
   4764   ASSERT_EQ(1u, visitor_.ack_frames_.size());
   4765   const QuicAckFrame& processed_ack_frame = *visitor_.ack_frames_[0];
   4766   EXPECT_EQ(header.packet_sequence_number - 1,
   4767             processed_ack_frame.sent_info.least_unacked);
   4768   EXPECT_TRUE(processed_ack_frame.received_info.is_truncated);
   4769   EXPECT_EQ(510u, processed_ack_frame.received_info.largest_observed);
   4770   ASSERT_EQ(255u, processed_ack_frame.received_info.missing_packets.size());
   4771   SequenceNumberSet::const_iterator missing_iter =
   4772       processed_ack_frame.received_info.missing_packets.begin();
   4773   EXPECT_EQ(1u, *missing_iter);
   4774   SequenceNumberSet::const_reverse_iterator last_missing_iter =
   4775       processed_ack_frame.received_info.missing_packets.rbegin();
   4776   EXPECT_EQ(509u, *last_missing_iter);
   4777 }
   4778 
   4779 TEST_P(QuicFramerTest, CleanTruncation) {
   4780   QuicPacketHeader header;
   4781   header.public_header.connection_id = GG_UINT64_C(0xFEDCBA9876543210);
   4782   header.public_header.reset_flag = false;
   4783   header.public_header.version_flag = false;
   4784   header.fec_flag = false;
   4785   header.entropy_flag = true;
   4786   header.packet_sequence_number = GG_UINT64_C(0x123456789ABC);
   4787   header.fec_group = 0;
   4788 
   4789   QuicAckFrame ack_frame;
   4790   ack_frame.received_info.largest_observed = 201;
   4791   ack_frame.sent_info.least_unacked = header.packet_sequence_number - 2;
   4792   for (uint64 i = 1; i < ack_frame.received_info.largest_observed; ++i) {
   4793     ack_frame.received_info.missing_packets.insert(i);
   4794   }
   4795 
   4796   // Create a packet with just the ack.
   4797   QuicFrame frame;
   4798   frame.type = ACK_FRAME;
   4799   frame.ack_frame = &ack_frame;
   4800   QuicFrames frames;
   4801   frames.push_back(frame);
   4802 
   4803   scoped_ptr<QuicPacket> raw_ack_packet(BuildDataPacket(header, frames));
   4804   ASSERT_TRUE(raw_ack_packet != NULL);
   4805 
   4806   scoped_ptr<QuicEncryptedPacket> ack_packet(
   4807       framer_.EncryptPacket(ENCRYPTION_NONE, header.packet_sequence_number,
   4808                             *raw_ack_packet));
   4809 
   4810   // Now make sure we can turn our ack packet back into an ack frame.
   4811   ASSERT_TRUE(framer_.ProcessPacket(*ack_packet));
   4812 
   4813   // Test for clean truncation of the ack by comparing the length of the
   4814   // original packets to the re-serialized packets.
   4815   frames.clear();
   4816   frame.type = ACK_FRAME;
   4817   frame.ack_frame = visitor_.ack_frames_[0];
   4818   frames.push_back(frame);
   4819 
   4820   size_t original_raw_length = raw_ack_packet->length();
   4821   raw_ack_packet.reset(BuildDataPacket(header, frames));
   4822   ASSERT_TRUE(raw_ack_packet != NULL);
   4823   EXPECT_EQ(original_raw_length, raw_ack_packet->length());
   4824   ASSERT_TRUE(raw_ack_packet != NULL);
   4825 }
   4826 
   4827 TEST_P(QuicFramerTest, EntropyFlagTest) {
   4828   unsigned char packet[] = {
   4829     // public flags (8 byte connection_id)
   4830     0x3C,
   4831     // connection_id
   4832     0x10, 0x32, 0x54, 0x76,
   4833     0x98, 0xBA, 0xDC, 0xFE,
   4834     // packet sequence number
   4835     0xBC, 0x9A, 0x78, 0x56,
   4836     0x34, 0x12,
   4837     // private flags (Entropy)
   4838     0x01,
   4839 
   4840     // frame type (stream frame with fin and no length)
   4841     0xDF,
   4842     // stream id
   4843     0x04, 0x03, 0x02, 0x01,
   4844     // offset
   4845     0x54, 0x76, 0x10, 0x32,
   4846     0xDC, 0xFE, 0x98, 0xBA,
   4847     // data
   4848     'h',  'e',  'l',  'l',
   4849     'o',  ' ',  'w',  'o',
   4850     'r',  'l',  'd',  '!',
   4851   };
   4852 
   4853   QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
   4854   EXPECT_TRUE(framer_.ProcessPacket(encrypted));
   4855   EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
   4856   ASSERT_TRUE(visitor_.header_.get());
   4857   EXPECT_TRUE(visitor_.header_->entropy_flag);
   4858   EXPECT_EQ(1 << 4, visitor_.header_->entropy_hash);
   4859   EXPECT_FALSE(visitor_.header_->fec_flag);
   4860 };
   4861 
   4862 TEST_P(QuicFramerTest, FecEntropyTest) {
   4863   unsigned char packet[] = {
   4864     // public flags (8 byte connection_id)
   4865     0x3C,
   4866     // connection_id
   4867     0x10, 0x32, 0x54, 0x76,
   4868     0x98, 0xBA, 0xDC, 0xFE,
   4869     // packet sequence number
   4870     0xBC, 0x9A, 0x78, 0x56,
   4871     0x34, 0x12,
   4872     // private flags (Entropy & fec group & FEC)
   4873     0x07,
   4874     // first fec protected packet offset
   4875     0xFF,
   4876 
   4877     // frame type (stream frame with fin and no length)
   4878     0xDF,
   4879     // stream id
   4880     0x04, 0x03, 0x02, 0x01,
   4881     // offset
   4882     0x54, 0x76, 0x10, 0x32,
   4883     0xDC, 0xFE, 0x98, 0xBA,
   4884     // data
   4885     'h',  'e',  'l',  'l',
   4886     'o',  ' ',  'w',  'o',
   4887     'r',  'l',  'd',  '!',
   4888   };
   4889 
   4890   QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
   4891   EXPECT_TRUE(framer_.ProcessPacket(encrypted));
   4892   EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
   4893   ASSERT_TRUE(visitor_.header_.get());
   4894   EXPECT_TRUE(visitor_.header_->fec_flag);
   4895   EXPECT_TRUE(visitor_.header_->entropy_flag);
   4896   EXPECT_EQ(1 << 4, visitor_.header_->entropy_hash);
   4897 };
   4898 
   4899 TEST_P(QuicFramerTest, StopPacketProcessing) {
   4900   unsigned char packet[] = {
   4901     // public flags (8 byte connection_id)
   4902     0x3C,
   4903     // connection_id
   4904     0x10, 0x32, 0x54, 0x76,
   4905     0x98, 0xBA, 0xDC, 0xFE,
   4906     // packet sequence number
   4907     0xBC, 0x9A, 0x78, 0x56,
   4908     0x34, 0x12,
   4909     // Entropy
   4910     0x01,
   4911 
   4912     // frame type (stream frame with fin)
   4913     0xFF,
   4914     // stream id
   4915     0x04, 0x03, 0x02, 0x01,
   4916     // offset
   4917     0x54, 0x76, 0x10, 0x32,
   4918     0xDC, 0xFE, 0x98, 0xBA,
   4919     // data length
   4920     0x0c, 0x00,
   4921     // data
   4922     'h',  'e',  'l',  'l',
   4923     'o',  ' ',  'w',  'o',
   4924     'r',  'l',  'd',  '!',
   4925 
   4926     // frame type (ack frame)
   4927     0x40,
   4928     // entropy hash of sent packets till least awaiting - 1.
   4929     0x14,
   4930     // least packet sequence number awaiting an ack
   4931     0xA0, 0x9A, 0x78, 0x56,
   4932     0x34, 0x12,
   4933     // entropy hash of all received packets.
   4934     0x43,
   4935     // largest observed packet sequence number
   4936     0xBF, 0x9A, 0x78, 0x56,
   4937     0x34, 0x12,
   4938     // num missing packets
   4939     0x01,
   4940     // missing packet
   4941     0xBE, 0x9A, 0x78, 0x56,
   4942     0x34, 0x12,
   4943   };
   4944 
   4945   MockFramerVisitor visitor;
   4946   framer_.set_visitor(&visitor);
   4947   EXPECT_CALL(visitor, OnPacket());
   4948   EXPECT_CALL(visitor, OnPacketHeader(_));
   4949   EXPECT_CALL(visitor, OnStreamFrame(_)).WillOnce(Return(false));
   4950   EXPECT_CALL(visitor, OnAckFrame(_)).Times(0);
   4951   EXPECT_CALL(visitor, OnPacketComplete());
   4952   EXPECT_CALL(visitor, OnUnauthenticatedPublicHeader(_)).WillOnce(Return(true));
   4953 
   4954   QuicEncryptedPacket encrypted(AsChars(packet), arraysize(packet), false);
   4955   EXPECT_TRUE(framer_.ProcessPacket(encrypted));
   4956   EXPECT_EQ(QUIC_NO_ERROR, framer_.error());
   4957 }
   4958 
   4959 }  // namespace test
   4960 }  // namespace net
   4961