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_packet_creator.h"
      6 
      7 #include "base/stl_util.h"
      8 #include "net/quic/crypto/null_encrypter.h"
      9 #include "net/quic/crypto/quic_decrypter.h"
     10 #include "net/quic/crypto/quic_encrypter.h"
     11 #include "net/quic/quic_utils.h"
     12 #include "net/quic/test_tools/mock_random.h"
     13 #include "net/quic/test_tools/quic_packet_creator_peer.h"
     14 #include "net/quic/test_tools/quic_test_utils.h"
     15 #include "testing/gmock/include/gmock/gmock.h"
     16 
     17 using base::StringPiece;
     18 using std::string;
     19 using std::vector;
     20 using testing::DoAll;
     21 using testing::InSequence;
     22 using testing::Return;
     23 using testing::SaveArg;
     24 using testing::_;
     25 
     26 namespace net {
     27 namespace test {
     28 namespace {
     29 
     30 class QuicPacketCreatorTest : public ::testing::TestWithParam<bool> {
     31  protected:
     32   QuicPacketCreatorTest()
     33       : server_framer_(QuicSupportedVersions(), QuicTime::Zero(), true),
     34         client_framer_(QuicSupportedVersions(), QuicTime::Zero(), false),
     35         sequence_number_(0),
     36         guid_(2),
     37         data_("foo"),
     38         creator_(guid_, &client_framer_, &mock_random_, false) {
     39     client_framer_.set_visitor(&framer_visitor_);
     40     server_framer_.set_visitor(&framer_visitor_);
     41   }
     42   ~QuicPacketCreatorTest() {
     43   }
     44 
     45   void ProcessPacket(QuicPacket* packet) {
     46     scoped_ptr<QuicEncryptedPacket> encrypted(
     47         server_framer_.EncryptPacket(ENCRYPTION_NONE, sequence_number_,
     48                                      *packet));
     49     server_framer_.ProcessPacket(*encrypted);
     50   }
     51 
     52   void CheckStreamFrame(const QuicFrame& frame,
     53                         QuicStreamId stream_id,
     54                         const string& data,
     55                         QuicStreamOffset offset,
     56                         bool fin) {
     57     EXPECT_EQ(STREAM_FRAME, frame.type);
     58     ASSERT_TRUE(frame.stream_frame);
     59     EXPECT_EQ(stream_id, frame.stream_frame->stream_id);
     60     scoped_ptr<string> frame_data(frame.stream_frame->GetDataAsString());
     61     EXPECT_EQ(data, *frame_data);
     62     EXPECT_EQ(offset, frame.stream_frame->offset);
     63     EXPECT_EQ(fin, frame.stream_frame->fin);
     64   }
     65 
     66   // Returns the number of bytes consumed by the header of packet, including
     67   // the version, that is not in an FEC group.
     68   size_t GetPacketHeaderOverhead() {
     69     return GetPacketHeaderSize(creator_.options()->send_guid_length,
     70                                kIncludeVersion,
     71                                creator_.options()->send_sequence_number_length,
     72                                NOT_IN_FEC_GROUP);
     73   }
     74 
     75   // Returns the number of bytes of overhead that will be added to a packet
     76   // of maximum length.
     77   size_t GetEncryptionOverhead() {
     78     return creator_.options()->max_packet_length -
     79         client_framer_.GetMaxPlaintextSize(
     80             creator_.options()->max_packet_length);
     81   }
     82 
     83   // Returns the number of bytes consumed by the non-data fields of a stream
     84   // frame, assuming it is the last frame in the packet
     85   size_t GetStreamFrameOverhead() {
     86     return QuicFramer::GetMinStreamFrameSize(
     87         client_framer_.version(), kStreamId, kOffset, true);
     88   }
     89 
     90   static const QuicStreamId kStreamId = 1u;
     91   static const QuicStreamOffset kOffset = 1u;
     92 
     93   QuicFrames frames_;
     94   QuicFramer server_framer_;
     95   QuicFramer client_framer_;
     96   testing::StrictMock<MockFramerVisitor> framer_visitor_;
     97   QuicPacketSequenceNumber sequence_number_;
     98   QuicGuid guid_;
     99   string data_;
    100   MockRandom mock_random_;
    101   QuicPacketCreator creator_;
    102 };
    103 
    104 TEST_F(QuicPacketCreatorTest, SerializeFrames) {
    105   frames_.push_back(QuicFrame(new QuicAckFrame(0u, QuicTime::Zero(), 0u)));
    106   frames_.push_back(QuicFrame(new QuicStreamFrame(0u, false, 0u, IOVector())));
    107   frames_.push_back(QuicFrame(new QuicStreamFrame(0u, true, 0u, IOVector())));
    108   SerializedPacket serialized = creator_.SerializeAllFrames(frames_);
    109   delete frames_[0].ack_frame;
    110   delete frames_[1].stream_frame;
    111   delete frames_[2].stream_frame;
    112 
    113   {
    114     InSequence s;
    115     EXPECT_CALL(framer_visitor_, OnPacket());
    116     EXPECT_CALL(framer_visitor_, OnUnauthenticatedHeader(_));
    117     EXPECT_CALL(framer_visitor_, OnPacketHeader(_));
    118     EXPECT_CALL(framer_visitor_, OnAckFrame(_));
    119     EXPECT_CALL(framer_visitor_, OnStreamFrame(_));
    120     EXPECT_CALL(framer_visitor_, OnStreamFrame(_));
    121     EXPECT_CALL(framer_visitor_, OnPacketComplete());
    122   }
    123   ProcessPacket(serialized.packet);
    124   delete serialized.packet;
    125 }
    126 
    127 TEST_F(QuicPacketCreatorTest, SerializeWithFEC) {
    128   creator_.options()->max_packets_per_fec_group = 6;
    129   ASSERT_FALSE(creator_.ShouldSendFec(false));
    130 
    131   frames_.push_back(QuicFrame(new QuicStreamFrame(0u, false, 0u, IOVector())));
    132   SerializedPacket serialized = creator_.SerializeAllFrames(frames_);
    133   delete frames_[0].stream_frame;
    134 
    135   {
    136     InSequence s;
    137     EXPECT_CALL(framer_visitor_, OnPacket());
    138     EXPECT_CALL(framer_visitor_, OnUnauthenticatedHeader(_));
    139     EXPECT_CALL(framer_visitor_, OnPacketHeader(_));
    140     EXPECT_CALL(framer_visitor_, OnFecProtectedPayload(_));
    141     EXPECT_CALL(framer_visitor_, OnStreamFrame(_));
    142     EXPECT_CALL(framer_visitor_, OnPacketComplete());
    143   }
    144   ProcessPacket(serialized.packet);
    145   delete serialized.packet;
    146 
    147   ASSERT_FALSE(creator_.ShouldSendFec(false));
    148   ASSERT_TRUE(creator_.ShouldSendFec(true));
    149 
    150   serialized = creator_.SerializeFec();
    151   ASSERT_EQ(2u, serialized.sequence_number);
    152 
    153   {
    154     InSequence s;
    155     EXPECT_CALL(framer_visitor_, OnPacket());
    156     EXPECT_CALL(framer_visitor_, OnUnauthenticatedHeader(_));
    157     EXPECT_CALL(framer_visitor_, OnPacketHeader(_));
    158     EXPECT_CALL(framer_visitor_, OnFecData(_));
    159     EXPECT_CALL(framer_visitor_, OnPacketComplete());
    160   }
    161   ProcessPacket(serialized.packet);
    162   delete serialized.packet;
    163 }
    164 
    165 TEST_F(QuicPacketCreatorTest, SerializeChangingSequenceNumberLength) {
    166   frames_.push_back(QuicFrame(new QuicAckFrame(0u, QuicTime::Zero(), 0u)));
    167   creator_.AddSavedFrame(frames_[0]);
    168   creator_.options()->send_sequence_number_length =
    169       PACKET_4BYTE_SEQUENCE_NUMBER;
    170   SerializedPacket serialized = creator_.SerializePacket();
    171   // The sequence number length will not change mid-packet.
    172   EXPECT_EQ(PACKET_1BYTE_SEQUENCE_NUMBER, serialized.sequence_number_length);
    173 
    174   {
    175     InSequence s;
    176     EXPECT_CALL(framer_visitor_, OnPacket());
    177     EXPECT_CALL(framer_visitor_, OnUnauthenticatedHeader(_));
    178     EXPECT_CALL(framer_visitor_, OnPacketHeader(_));
    179     EXPECT_CALL(framer_visitor_, OnAckFrame(_));
    180     EXPECT_CALL(framer_visitor_, OnPacketComplete());
    181   }
    182   ProcessPacket(serialized.packet);
    183   delete serialized.packet;
    184 
    185   creator_.AddSavedFrame(frames_[0]);
    186   serialized = creator_.SerializePacket();
    187   // Now the actual sequence number length should have changed.
    188   EXPECT_EQ(PACKET_4BYTE_SEQUENCE_NUMBER, serialized.sequence_number_length);
    189   delete frames_[0].ack_frame;
    190 
    191   {
    192     InSequence s;
    193     EXPECT_CALL(framer_visitor_, OnPacket());
    194     EXPECT_CALL(framer_visitor_, OnUnauthenticatedHeader(_));
    195     EXPECT_CALL(framer_visitor_, OnPacketHeader(_));
    196     EXPECT_CALL(framer_visitor_, OnAckFrame(_));
    197     EXPECT_CALL(framer_visitor_, OnPacketComplete());
    198   }
    199   ProcessPacket(serialized.packet);
    200   delete serialized.packet;
    201 }
    202 
    203 TEST_F(QuicPacketCreatorTest, SerializeWithFECChangingSequenceNumberLength) {
    204   creator_.options()->max_packets_per_fec_group = 6;
    205   ASSERT_FALSE(creator_.ShouldSendFec(false));
    206 
    207   frames_.push_back(QuicFrame(new QuicAckFrame(0u, QuicTime::Zero(), 0u)));
    208   creator_.AddSavedFrame(frames_[0]);
    209   // Change the sequence number length mid-FEC group and it should not change.
    210   creator_.options()->send_sequence_number_length =
    211       PACKET_4BYTE_SEQUENCE_NUMBER;
    212   SerializedPacket serialized = creator_.SerializePacket();
    213   EXPECT_EQ(PACKET_1BYTE_SEQUENCE_NUMBER, serialized.sequence_number_length);
    214 
    215   {
    216     InSequence s;
    217     EXPECT_CALL(framer_visitor_, OnPacket());
    218     EXPECT_CALL(framer_visitor_, OnUnauthenticatedHeader(_));
    219     EXPECT_CALL(framer_visitor_, OnPacketHeader(_));
    220     EXPECT_CALL(framer_visitor_, OnFecProtectedPayload(_));
    221     EXPECT_CALL(framer_visitor_, OnAckFrame(_));
    222     EXPECT_CALL(framer_visitor_, OnPacketComplete());
    223   }
    224   ProcessPacket(serialized.packet);
    225   delete serialized.packet;
    226 
    227   ASSERT_FALSE(creator_.ShouldSendFec(false));
    228   ASSERT_TRUE(creator_.ShouldSendFec(true));
    229 
    230   serialized = creator_.SerializeFec();
    231   EXPECT_EQ(PACKET_1BYTE_SEQUENCE_NUMBER, serialized.sequence_number_length);
    232   ASSERT_EQ(2u, serialized.sequence_number);
    233 
    234   {
    235     InSequence s;
    236     EXPECT_CALL(framer_visitor_, OnPacket());
    237     EXPECT_CALL(framer_visitor_, OnUnauthenticatedHeader(_));
    238     EXPECT_CALL(framer_visitor_, OnPacketHeader(_));
    239     EXPECT_CALL(framer_visitor_, OnFecData(_));
    240     EXPECT_CALL(framer_visitor_, OnPacketComplete());
    241   }
    242   ProcessPacket(serialized.packet);
    243   delete serialized.packet;
    244 
    245   // Ensure the next FEC group starts using the new sequence number length.
    246   serialized = creator_.SerializeAllFrames(frames_);
    247   EXPECT_EQ(PACKET_4BYTE_SEQUENCE_NUMBER, serialized.sequence_number_length);
    248   delete frames_[0].ack_frame;
    249   delete serialized.packet;
    250 }
    251 
    252 TEST_F(QuicPacketCreatorTest, ReserializeFramesWithSequenceNumberLength) {
    253   // If the original packet sequence number length, the current sequence number
    254   // length, and the configured send sequence number length are different, the
    255   // retransmit must sent with the original length and the others do not change.
    256   creator_.options()->send_sequence_number_length =
    257       PACKET_4BYTE_SEQUENCE_NUMBER;
    258   QuicPacketCreatorPeer::SetSequenceNumberLength(&creator_,
    259                                                  PACKET_2BYTE_SEQUENCE_NUMBER);
    260   frames_.push_back(QuicFrame(new QuicStreamFrame(0u, false, 0u, IOVector())));
    261   SerializedPacket serialized =
    262       creator_.ReserializeAllFrames(frames_, PACKET_1BYTE_SEQUENCE_NUMBER);
    263   EXPECT_EQ(PACKET_4BYTE_SEQUENCE_NUMBER,
    264             creator_.options()->send_sequence_number_length);
    265   EXPECT_EQ(PACKET_2BYTE_SEQUENCE_NUMBER,
    266             QuicPacketCreatorPeer::GetSequenceNumberLength(&creator_));
    267   EXPECT_EQ(PACKET_1BYTE_SEQUENCE_NUMBER, serialized.sequence_number_length);
    268   delete frames_[0].stream_frame;
    269 
    270   {
    271     InSequence s;
    272     EXPECT_CALL(framer_visitor_, OnPacket());
    273     EXPECT_CALL(framer_visitor_, OnUnauthenticatedHeader(_));
    274     EXPECT_CALL(framer_visitor_, OnPacketHeader(_));
    275     EXPECT_CALL(framer_visitor_, OnStreamFrame(_));
    276     EXPECT_CALL(framer_visitor_, OnPacketComplete());
    277   }
    278   ProcessPacket(serialized.packet);
    279   delete serialized.packet;
    280 }
    281 
    282 TEST_F(QuicPacketCreatorTest, SerializeConnectionClose) {
    283   QuicConnectionCloseFrame frame;
    284   frame.error_code = QUIC_NO_ERROR;
    285   frame.error_details = "error";
    286 
    287   SerializedPacket serialized = creator_.SerializeConnectionClose(&frame);
    288   ASSERT_EQ(1u, serialized.sequence_number);
    289   ASSERT_EQ(1u, creator_.sequence_number());
    290 
    291   InSequence s;
    292   EXPECT_CALL(framer_visitor_, OnPacket());
    293   EXPECT_CALL(framer_visitor_, OnUnauthenticatedHeader(_));
    294   EXPECT_CALL(framer_visitor_, OnPacketHeader(_));
    295   EXPECT_CALL(framer_visitor_, OnConnectionCloseFrame(_));
    296   EXPECT_CALL(framer_visitor_, OnPacketComplete());
    297 
    298   ProcessPacket(serialized.packet);
    299   delete serialized.packet;
    300 }
    301 
    302 TEST_F(QuicPacketCreatorTest, CreateStreamFrame) {
    303   QuicFrame frame;
    304   size_t consumed = creator_.CreateStreamFrame(1u, MakeIOVector("test"), 0u,
    305                                                false, &frame);
    306   EXPECT_EQ(4u, consumed);
    307   CheckStreamFrame(frame, 1u, "test", 0u, false);
    308   delete frame.stream_frame;
    309 }
    310 
    311 TEST_F(QuicPacketCreatorTest, CreateStreamFrameFin) {
    312   QuicFrame frame;
    313   size_t consumed = creator_.CreateStreamFrame(1u, MakeIOVector("test"), 10u,
    314                                                true, &frame);
    315   EXPECT_EQ(4u, consumed);
    316   CheckStreamFrame(frame, 1u, "test", 10u, true);
    317   delete frame.stream_frame;
    318 }
    319 
    320 TEST_F(QuicPacketCreatorTest, CreateStreamFrameFinOnly) {
    321   QuicFrame frame;
    322   size_t consumed = creator_.CreateStreamFrame(1u, IOVector(), 0u, true,
    323                                                &frame);
    324   EXPECT_EQ(0u, consumed);
    325   CheckStreamFrame(frame, 1u, string(), 0u, true);
    326   delete frame.stream_frame;
    327 }
    328 
    329 TEST_F(QuicPacketCreatorTest, CreateAllFreeBytesForStreamFrames) {
    330   const size_t overhead = GetPacketHeaderOverhead() + GetEncryptionOverhead();
    331   for (size_t i = overhead; i < overhead + 100; ++i) {
    332     creator_.options()->max_packet_length = i;
    333     const bool should_have_room = i > overhead + GetStreamFrameOverhead();
    334     ASSERT_EQ(should_have_room,
    335               creator_.HasRoomForStreamFrame(kStreamId, kOffset));
    336     if (should_have_room) {
    337       QuicFrame frame;
    338       size_t bytes_consumed = creator_.CreateStreamFrame(
    339           kStreamId, MakeIOVector("testdata"), kOffset, false, &frame);
    340       EXPECT_LT(0u, bytes_consumed);
    341       ASSERT_TRUE(creator_.AddSavedFrame(frame));
    342       SerializedPacket serialized_packet = creator_.SerializePacket();
    343       ASSERT_TRUE(serialized_packet.packet);
    344       delete serialized_packet.packet;
    345       delete serialized_packet.retransmittable_frames;
    346     }
    347   }
    348 }
    349 
    350 TEST_F(QuicPacketCreatorTest, StreamFrameConsumption) {
    351   // Compute the total overhead for a single frame in packet.
    352   const size_t overhead = GetPacketHeaderOverhead() + GetEncryptionOverhead()
    353       + GetStreamFrameOverhead();
    354   size_t capacity = kDefaultMaxPacketSize - overhead;
    355   // Now, test various sizes around this size.
    356   for (int delta = -5; delta <= 5; ++delta) {
    357     string data(capacity + delta, 'A');
    358     size_t bytes_free = delta > 0 ? 0 : 0 - delta;
    359     QuicFrame frame;
    360     size_t bytes_consumed = creator_.CreateStreamFrame(
    361         kStreamId, MakeIOVector(data), kOffset, false, &frame);
    362     EXPECT_EQ(capacity - bytes_free, bytes_consumed);
    363 
    364     ASSERT_TRUE(creator_.AddSavedFrame(frame));
    365     // BytesFree() returns bytes available for the next frame, which will
    366     // be two bytes smaller since the stream frame would need to be grown.
    367     size_t expected_bytes_free = bytes_free < 3 ? 0 : bytes_free - 2;
    368     EXPECT_EQ(expected_bytes_free, creator_.BytesFree()) << "delta: " << delta;
    369     SerializedPacket serialized_packet = creator_.SerializePacket();
    370     ASSERT_TRUE(serialized_packet.packet);
    371     delete serialized_packet.packet;
    372     delete serialized_packet.retransmittable_frames;
    373   }
    374 }
    375 
    376 TEST_F(QuicPacketCreatorTest, CryptoStreamFramePacketPadding) {
    377   // Compute the total overhead for a single frame in packet.
    378   const size_t overhead = GetPacketHeaderOverhead() + GetEncryptionOverhead()
    379       + GetStreamFrameOverhead();
    380   ASSERT_GT(kMaxPacketSize, overhead);
    381   size_t capacity = kDefaultMaxPacketSize - overhead;
    382   // Now, test various sizes around this size.
    383   for (int delta = -5; delta <= 5; ++delta) {
    384     string data(capacity + delta, 'A');
    385     size_t bytes_free = delta > 0 ? 0 : 0 - delta;
    386 
    387     QuicFrame frame;
    388     size_t bytes_consumed = creator_.CreateStreamFrame(
    389         kStreamId, MakeIOVector(data), kOffset, false, &frame);
    390     EXPECT_LT(0u, bytes_consumed);
    391     ASSERT_TRUE(creator_.AddSavedFrame(frame));
    392     SerializedPacket serialized_packet = creator_.SerializePacket();
    393     ASSERT_TRUE(serialized_packet.packet);
    394     // If there is not enough space in the packet to fit a padding frame
    395     // (1 byte) and to expand the stream frame (another 2 bytes) the packet
    396     // will not be padded.
    397     if (bytes_free < 3) {
    398       EXPECT_EQ(client_framer_.GetMaxPlaintextSize(kDefaultMaxPacketSize)
    399                 - bytes_free, serialized_packet.packet->length());
    400     } else {
    401       EXPECT_EQ(client_framer_.GetMaxPlaintextSize(kDefaultMaxPacketSize),
    402                 serialized_packet.packet->length());
    403     }
    404     delete serialized_packet.packet;
    405     delete serialized_packet.retransmittable_frames;
    406   }
    407 }
    408 
    409 TEST_F(QuicPacketCreatorTest, NonCryptoStreamFramePacketNonPadding) {
    410   // Compute the total overhead for a single frame in packet.
    411   const size_t overhead = GetPacketHeaderOverhead() + GetEncryptionOverhead()
    412       + GetStreamFrameOverhead();
    413   ASSERT_GT(kDefaultMaxPacketSize, overhead);
    414   size_t capacity = kDefaultMaxPacketSize - overhead;
    415   // Now, test various sizes around this size.
    416   for (int delta = -5; delta <= 5; ++delta) {
    417     string data(capacity + delta, 'A');
    418     size_t bytes_free = delta > 0 ? 0 : 0 - delta;
    419 
    420     QuicFrame frame;
    421     size_t bytes_consumed = creator_.CreateStreamFrame(
    422         kStreamId + 2, MakeIOVector(data), kOffset, false, &frame);
    423     EXPECT_LT(0u, bytes_consumed);
    424     ASSERT_TRUE(creator_.AddSavedFrame(frame));
    425     SerializedPacket serialized_packet = creator_.SerializePacket();
    426     ASSERT_TRUE(serialized_packet.packet);
    427     if (bytes_free > 0) {
    428       EXPECT_EQ(client_framer_.GetMaxPlaintextSize(kDefaultMaxPacketSize)
    429                 - bytes_free, serialized_packet.packet->length());
    430     } else {
    431       EXPECT_EQ(client_framer_.GetMaxPlaintextSize(kDefaultMaxPacketSize),
    432                 serialized_packet.packet->length());
    433     }
    434     delete serialized_packet.packet;
    435     delete serialized_packet.retransmittable_frames;
    436   }
    437 }
    438 
    439 TEST_F(QuicPacketCreatorTest, SerializeVersionNegotiationPacket) {
    440   QuicPacketCreatorPeer::SetIsServer(&creator_, true);
    441   QuicVersionVector versions;
    442   versions.push_back(test::QuicVersionMax());
    443   scoped_ptr<QuicEncryptedPacket> encrypted(
    444       creator_.SerializeVersionNegotiationPacket(versions));
    445 
    446   {
    447     InSequence s;
    448     EXPECT_CALL(framer_visitor_, OnPacket());
    449     EXPECT_CALL(framer_visitor_, OnVersionNegotiationPacket(_));
    450   }
    451   client_framer_.ProcessPacket(*encrypted.get());
    452 }
    453 
    454 TEST_F(QuicPacketCreatorTest, UpdatePacketSequenceNumberLengthLeastAwaiting) {
    455   EXPECT_EQ(PACKET_1BYTE_SEQUENCE_NUMBER,
    456             creator_.options()->send_sequence_number_length);
    457 
    458   creator_.set_sequence_number(64);
    459   creator_.UpdateSequenceNumberLength(2, 10000);
    460   EXPECT_EQ(PACKET_1BYTE_SEQUENCE_NUMBER,
    461             creator_.options()->send_sequence_number_length);
    462 
    463   creator_.set_sequence_number(64 * 256);
    464   creator_.UpdateSequenceNumberLength(2, 10000);
    465   EXPECT_EQ(PACKET_2BYTE_SEQUENCE_NUMBER,
    466             creator_.options()->send_sequence_number_length);
    467 
    468   creator_.set_sequence_number(64 * 256 * 256);
    469   creator_.UpdateSequenceNumberLength(2, 10000);
    470   EXPECT_EQ(PACKET_4BYTE_SEQUENCE_NUMBER,
    471             creator_.options()->send_sequence_number_length);
    472 
    473   creator_.set_sequence_number(GG_UINT64_C(64) * 256 * 256 * 256 * 256);
    474   creator_.UpdateSequenceNumberLength(2, 10000);
    475   EXPECT_EQ(PACKET_6BYTE_SEQUENCE_NUMBER,
    476             creator_.options()->send_sequence_number_length);
    477 }
    478 
    479 TEST_F(QuicPacketCreatorTest, UpdatePacketSequenceNumberLengthBandwidth) {
    480   EXPECT_EQ(PACKET_1BYTE_SEQUENCE_NUMBER,
    481             creator_.options()->send_sequence_number_length);
    482 
    483   creator_.UpdateSequenceNumberLength(1, 10000);
    484   EXPECT_EQ(PACKET_1BYTE_SEQUENCE_NUMBER,
    485             creator_.options()->send_sequence_number_length);
    486 
    487   creator_.UpdateSequenceNumberLength(1, 10000 * 256);
    488   EXPECT_EQ(PACKET_2BYTE_SEQUENCE_NUMBER,
    489             creator_.options()->send_sequence_number_length);
    490 
    491   creator_.UpdateSequenceNumberLength(1, 10000 * 256 * 256);
    492   EXPECT_EQ(PACKET_4BYTE_SEQUENCE_NUMBER,
    493             creator_.options()->send_sequence_number_length);
    494 
    495   creator_.UpdateSequenceNumberLength(
    496       1, GG_UINT64_C(1000) * 256 * 256 * 256 * 256);
    497   EXPECT_EQ(PACKET_6BYTE_SEQUENCE_NUMBER,
    498             creator_.options()->send_sequence_number_length);
    499 }
    500 
    501 TEST_F(QuicPacketCreatorTest, CreateStreamFrameWithNotifier) {
    502   // Ensure that if CreateStreamFrame does not consume any data (e.g. a FIN only
    503   // frame) then any QuicAckNotifier that is passed in still gets attached to
    504   // the frame.
    505   MockAckNotifierDelegate delegate;
    506   QuicAckNotifier notifier(&delegate);
    507   QuicFrame frame;
    508   IOVector empty_iovector;
    509   bool fin = true;
    510   size_t consumed_bytes = creator_.CreateStreamFrameWithNotifier(
    511       1u, empty_iovector, 0u, fin, &notifier, &frame);
    512   EXPECT_EQ(0u, consumed_bytes);
    513   EXPECT_EQ(&notifier, frame.stream_frame->notifier);
    514   delete frame.stream_frame;
    515 }
    516 
    517 INSTANTIATE_TEST_CASE_P(ToggleVersionSerialization,
    518                         QuicPacketCreatorTest,
    519                         ::testing::Values(false, true));
    520 
    521 TEST_P(QuicPacketCreatorTest, SerializeFrame) {
    522   if (!GetParam()) {
    523     creator_.StopSendingVersion();
    524   }
    525   frames_.push_back(QuicFrame(new QuicStreamFrame(0u, false, 0u, IOVector())));
    526   SerializedPacket serialized = creator_.SerializeAllFrames(frames_);
    527   delete frames_[0].stream_frame;
    528 
    529   QuicPacketHeader header;
    530   {
    531     InSequence s;
    532     EXPECT_CALL(framer_visitor_, OnPacket());
    533     EXPECT_CALL(framer_visitor_, OnUnauthenticatedHeader(_));
    534     EXPECT_CALL(framer_visitor_, OnPacketHeader(_)).WillOnce(
    535         DoAll(SaveArg<0>(&header), Return(true)));
    536     EXPECT_CALL(framer_visitor_, OnStreamFrame(_));
    537     EXPECT_CALL(framer_visitor_, OnPacketComplete());
    538   }
    539   ProcessPacket(serialized.packet);
    540   EXPECT_EQ(GetParam(), header.public_header.version_flag);
    541   delete serialized.packet;
    542 }
    543 
    544 TEST_P(QuicPacketCreatorTest, CreateStreamFrameTooLarge) {
    545   if (!GetParam()) {
    546     creator_.StopSendingVersion();
    547   }
    548   // A string larger than fits into a frame.
    549   size_t payload_length;
    550   creator_.options()->max_packet_length = GetPacketLengthForOneStream(
    551       client_framer_.version(),
    552       QuicPacketCreatorPeer::SendVersionInPacket(&creator_),
    553       PACKET_1BYTE_SEQUENCE_NUMBER, NOT_IN_FEC_GROUP, &payload_length);
    554   QuicFrame frame;
    555   const string too_long_payload(payload_length * 2, 'a');
    556   size_t consumed = creator_.CreateStreamFrame(
    557       1u, MakeIOVector(too_long_payload), 0u, true, &frame);
    558   EXPECT_EQ(payload_length, consumed);
    559   const string payload(payload_length, 'a');
    560   CheckStreamFrame(frame, 1u, payload, 0u, false);
    561   delete frame.stream_frame;
    562 }
    563 
    564 TEST_P(QuicPacketCreatorTest, AddFrameAndSerialize) {
    565   if (!GetParam()) {
    566     creator_.StopSendingVersion();
    567   }
    568   const size_t max_plaintext_size =
    569       client_framer_.GetMaxPlaintextSize(creator_.options()->max_packet_length);
    570   EXPECT_FALSE(creator_.HasPendingFrames());
    571   EXPECT_EQ(max_plaintext_size -
    572             GetPacketHeaderSize(
    573                 creator_.options()->send_guid_length,
    574                 QuicPacketCreatorPeer::SendVersionInPacket(&creator_),
    575                 PACKET_1BYTE_SEQUENCE_NUMBER, NOT_IN_FEC_GROUP),
    576             creator_.BytesFree());
    577 
    578   // Add a variety of frame types and then a padding frame.
    579   QuicAckFrame ack_frame(0u, QuicTime::Zero(), 0u);
    580   EXPECT_TRUE(creator_.AddSavedFrame(QuicFrame(&ack_frame)));
    581   EXPECT_TRUE(creator_.HasPendingFrames());
    582 
    583   QuicCongestionFeedbackFrame congestion_feedback;
    584   congestion_feedback.type = kFixRate;
    585   EXPECT_TRUE(creator_.AddSavedFrame(QuicFrame(&congestion_feedback)));
    586   EXPECT_TRUE(creator_.HasPendingFrames());
    587 
    588   QuicFrame frame;
    589   size_t consumed = creator_.CreateStreamFrame(
    590       1u, MakeIOVector("test"), 0u, false, &frame);
    591   EXPECT_EQ(4u, consumed);
    592   ASSERT_TRUE(frame.stream_frame);
    593   EXPECT_TRUE(creator_.AddSavedFrame(frame));
    594   EXPECT_TRUE(creator_.HasPendingFrames());
    595 
    596   QuicPaddingFrame padding_frame;
    597   EXPECT_TRUE(creator_.AddSavedFrame(QuicFrame(&padding_frame)));
    598   EXPECT_TRUE(creator_.HasPendingFrames());
    599   EXPECT_EQ(0u, creator_.BytesFree());
    600 
    601   EXPECT_FALSE(creator_.AddSavedFrame(QuicFrame(&ack_frame)));
    602 
    603   // Ensure the packet is successfully created.
    604   SerializedPacket serialized = creator_.SerializePacket();
    605   ASSERT_TRUE(serialized.packet);
    606   delete serialized.packet;
    607   ASSERT_TRUE(serialized.retransmittable_frames);
    608   RetransmittableFrames* retransmittable = serialized.retransmittable_frames;
    609   ASSERT_EQ(1u, retransmittable->frames().size());
    610   EXPECT_EQ(STREAM_FRAME, retransmittable->frames()[0].type);
    611   ASSERT_TRUE(retransmittable->frames()[0].stream_frame);
    612   delete serialized.retransmittable_frames;
    613 
    614   EXPECT_FALSE(creator_.HasPendingFrames());
    615   EXPECT_EQ(max_plaintext_size -
    616             GetPacketHeaderSize(
    617                 creator_.options()->send_guid_length,
    618                 QuicPacketCreatorPeer::SendVersionInPacket(&creator_),
    619                 PACKET_1BYTE_SEQUENCE_NUMBER,
    620                 NOT_IN_FEC_GROUP),
    621             creator_.BytesFree());
    622 }
    623 
    624 TEST_F(QuicPacketCreatorTest, EntropyFlag) {
    625   frames_.push_back(QuicFrame(new QuicStreamFrame(0u, false, 0u, IOVector())));
    626 
    627   for (int i = 0; i < 2; ++i) {
    628     for (int j = 0; j < 64; ++j) {
    629       SerializedPacket serialized = creator_.SerializeAllFrames(frames_);
    630       // Verify both BoolSource and hash algorithm.
    631       bool expected_rand_bool =
    632           (mock_random_.RandUint64() & (GG_UINT64_C(1) << j)) != 0;
    633       bool observed_rand_bool =
    634           (serialized.entropy_hash & (1 << ((j+1) % 8))) != 0;
    635       uint8 rest_of_hash = serialized.entropy_hash & ~(1 << ((j+1) % 8));
    636       EXPECT_EQ(expected_rand_bool, observed_rand_bool);
    637       EXPECT_EQ(0, rest_of_hash);
    638       delete serialized.packet;
    639     }
    640     // After 64 calls, BoolSource will refresh the bucket - make sure it does.
    641     mock_random_.ChangeValue();
    642   }
    643 
    644   delete frames_[0].stream_frame;
    645 }
    646 
    647 }  // namespace
    648 }  // namespace test
    649 }  // namespace net
    650