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_generator.h"
      6 
      7 #include <string>
      8 
      9 #include "net/quic/crypto/crypto_protocol.h"
     10 #include "net/quic/crypto/null_encrypter.h"
     11 #include "net/quic/crypto/quic_decrypter.h"
     12 #include "net/quic/crypto/quic_encrypter.h"
     13 #include "net/quic/quic_utils.h"
     14 #include "net/quic/test_tools/quic_test_utils.h"
     15 #include "net/quic/test_tools/simple_quic_framer.h"
     16 #include "testing/gmock/include/gmock/gmock.h"
     17 #include "testing/gtest/include/gtest/gtest.h"
     18 
     19 using base::StringPiece;
     20 using std::string;
     21 using testing::InSequence;
     22 using testing::Return;
     23 using testing::SaveArg;
     24 using testing::StrictMock;
     25 using testing::_;
     26 
     27 namespace net {
     28 namespace test {
     29 namespace {
     30 
     31 class MockDelegate : public QuicPacketGenerator::DelegateInterface {
     32  public:
     33   MockDelegate() {}
     34   virtual ~MockDelegate() {}
     35 
     36   MOCK_METHOD3(ShouldGeneratePacket,
     37                bool(TransmissionType transmission_type,
     38                     HasRetransmittableData retransmittable,
     39                     IsHandshake handshake));
     40   MOCK_METHOD0(CreateAckFrame, QuicAckFrame*());
     41   MOCK_METHOD0(CreateFeedbackFrame, QuicCongestionFeedbackFrame*());
     42   MOCK_METHOD1(OnSerializedPacket, bool(const SerializedPacket& packet));
     43   MOCK_METHOD2(CloseConnection, void(QuicErrorCode, bool));
     44 
     45   void SetCanWriteAnything() {
     46     EXPECT_CALL(*this, ShouldGeneratePacket(NOT_RETRANSMISSION, _, _))
     47         .WillRepeatedly(Return(true));
     48     EXPECT_CALL(*this, ShouldGeneratePacket(NOT_RETRANSMISSION,
     49                                             NO_RETRANSMITTABLE_DATA, _))
     50         .WillRepeatedly(Return(true));
     51   }
     52 
     53   void SetCanNotWrite() {
     54     EXPECT_CALL(*this, ShouldGeneratePacket(NOT_RETRANSMISSION, _, _))
     55         .WillRepeatedly(Return(false));
     56     EXPECT_CALL(*this, ShouldGeneratePacket(NOT_RETRANSMISSION,
     57                                             NO_RETRANSMITTABLE_DATA, _))
     58         .WillRepeatedly(Return(false));
     59   }
     60 
     61   // Use this when only ack and feedback frames should be allowed to be written.
     62   void SetCanWriteOnlyNonRetransmittable() {
     63     EXPECT_CALL(*this, ShouldGeneratePacket(NOT_RETRANSMISSION, _, _))
     64         .WillRepeatedly(Return(false));
     65     EXPECT_CALL(*this, ShouldGeneratePacket(NOT_RETRANSMISSION,
     66                                             NO_RETRANSMITTABLE_DATA, _))
     67         .WillRepeatedly(Return(true));
     68   }
     69 
     70  private:
     71   DISALLOW_COPY_AND_ASSIGN(MockDelegate);
     72 };
     73 
     74 // Simple struct for describing the contents of a packet.
     75 // Useful in conjunction with a SimpleQuicFrame for validating
     76 // that a packet contains the expected frames.
     77 struct PacketContents {
     78   PacketContents()
     79       : num_ack_frames(0),
     80         num_connection_close_frames(0),
     81         num_feedback_frames(0),
     82         num_goaway_frames(0),
     83         num_rst_stream_frames(0),
     84         num_stream_frames(0),
     85         fec_group(0) {
     86   }
     87 
     88   size_t num_ack_frames;
     89   size_t num_connection_close_frames;
     90   size_t num_feedback_frames;
     91   size_t num_goaway_frames;
     92   size_t num_rst_stream_frames;
     93   size_t num_stream_frames;
     94 
     95   QuicFecGroupNumber fec_group;
     96 };
     97 
     98 }  // namespace
     99 
    100 class QuicPacketGeneratorTest : public ::testing::Test {
    101  protected:
    102   QuicPacketGeneratorTest()
    103       : framer_(QuicSupportedVersions(), QuicTime::Zero(), false),
    104         creator_(42, &framer_, &random_, false),
    105         generator_(&delegate_, NULL, &creator_),
    106         packet_(0, PACKET_1BYTE_SEQUENCE_NUMBER, NULL, 0, NULL),
    107         packet2_(0, PACKET_1BYTE_SEQUENCE_NUMBER, NULL, 0, NULL),
    108         packet3_(0, PACKET_1BYTE_SEQUENCE_NUMBER, NULL, 0, NULL),
    109         packet4_(0, PACKET_1BYTE_SEQUENCE_NUMBER, NULL, 0, NULL),
    110         packet5_(0, PACKET_1BYTE_SEQUENCE_NUMBER, NULL, 0, NULL) {
    111   }
    112 
    113   ~QuicPacketGeneratorTest() {
    114     delete packet_.packet;
    115     delete packet_.retransmittable_frames;
    116     delete packet2_.packet;
    117     delete packet2_.retransmittable_frames;
    118     delete packet3_.packet;
    119     delete packet3_.retransmittable_frames;
    120     delete packet4_.packet;
    121     delete packet4_.retransmittable_frames;
    122     delete packet5_.packet;
    123     delete packet5_.retransmittable_frames;
    124   }
    125 
    126   QuicAckFrame* CreateAckFrame() {
    127     // TODO(rch): Initialize this so it can be verified later.
    128     return new QuicAckFrame(0, QuicTime::Zero(), 0);
    129   }
    130 
    131   QuicCongestionFeedbackFrame* CreateFeedbackFrame() {
    132     QuicCongestionFeedbackFrame* frame = new QuicCongestionFeedbackFrame;
    133     frame->type = kFixRate;
    134     frame->fix_rate.bitrate = QuicBandwidth::FromBytesPerSecond(42);
    135     return frame;
    136   }
    137 
    138   QuicRstStreamFrame* CreateRstStreamFrame() {
    139     return new QuicRstStreamFrame(1, QUIC_STREAM_NO_ERROR);
    140   }
    141 
    142   QuicGoAwayFrame* CreateGoAwayFrame() {
    143     return new QuicGoAwayFrame(QUIC_NO_ERROR, 1, string());
    144   }
    145 
    146   void CheckPacketContains(const PacketContents& contents,
    147                            const SerializedPacket& packet) {
    148     size_t num_retransmittable_frames = contents.num_connection_close_frames +
    149         contents.num_goaway_frames + contents.num_rst_stream_frames +
    150         contents.num_stream_frames;
    151     size_t num_frames = contents.num_feedback_frames + contents.num_ack_frames +
    152         num_retransmittable_frames;
    153 
    154     if (num_retransmittable_frames == 0) {
    155       ASSERT_TRUE(packet.retransmittable_frames == NULL);
    156     } else {
    157       ASSERT_TRUE(packet.retransmittable_frames != NULL);
    158       EXPECT_EQ(num_retransmittable_frames,
    159                 packet.retransmittable_frames->frames().size());
    160     }
    161 
    162     ASSERT_TRUE(packet.packet != NULL);
    163     ASSERT_TRUE(simple_framer_.ProcessPacket(*packet.packet));
    164     EXPECT_EQ(num_frames, simple_framer_.num_frames());
    165     EXPECT_EQ(contents.num_ack_frames, simple_framer_.ack_frames().size());
    166     EXPECT_EQ(contents.num_connection_close_frames,
    167               simple_framer_.connection_close_frames().size());
    168     EXPECT_EQ(contents.num_feedback_frames,
    169               simple_framer_.feedback_frames().size());
    170     EXPECT_EQ(contents.num_goaway_frames,
    171               simple_framer_.goaway_frames().size());
    172     EXPECT_EQ(contents.num_rst_stream_frames,
    173               simple_framer_.rst_stream_frames().size());
    174     EXPECT_EQ(contents.num_stream_frames,
    175               simple_framer_.stream_frames().size());
    176     EXPECT_EQ(contents.fec_group, simple_framer_.header().fec_group);
    177   }
    178 
    179   void CheckPacketHasSingleStreamFrame(const SerializedPacket& packet) {
    180     ASSERT_TRUE(packet.retransmittable_frames != NULL);
    181     EXPECT_EQ(1u, packet.retransmittable_frames->frames().size());
    182     ASSERT_TRUE(packet.packet != NULL);
    183     ASSERT_TRUE(simple_framer_.ProcessPacket(*packet.packet));
    184     EXPECT_EQ(1u, simple_framer_.num_frames());
    185     EXPECT_EQ(1u, simple_framer_.stream_frames().size());
    186   }
    187 
    188   void CheckPacketIsFec(const SerializedPacket& packet,
    189                         QuicPacketSequenceNumber fec_group) {
    190     ASSERT_TRUE(packet.retransmittable_frames == NULL);
    191     ASSERT_TRUE(packet.packet != NULL);
    192     ASSERT_TRUE(simple_framer_.ProcessPacket(*packet.packet));
    193     EXPECT_TRUE(simple_framer_.header().fec_flag);
    194     EXPECT_EQ(fec_group, simple_framer_.fec_data().fec_group);
    195   }
    196 
    197   IOVector CreateData(size_t len) {
    198     data_array_.reset(new char[len]);
    199     memset(data_array_.get(), '?', len);
    200     IOVector data;
    201     data.Append(data_array_.get(), len);
    202     return data;
    203   }
    204 
    205   QuicFramer framer_;
    206   MockRandom random_;
    207   QuicPacketCreator creator_;
    208   StrictMock<MockDelegate> delegate_;
    209   QuicPacketGenerator generator_;
    210   SimpleQuicFramer simple_framer_;
    211   SerializedPacket packet_;
    212   SerializedPacket packet2_;
    213   SerializedPacket packet3_;
    214   SerializedPacket packet4_;
    215   SerializedPacket packet5_;
    216 
    217  private:
    218   scoped_ptr<char[]> data_array_;
    219 };
    220 
    221 class MockDebugDelegate : public QuicPacketGenerator::DebugDelegateInterface {
    222  public:
    223   MOCK_METHOD1(OnFrameAddedToPacket,
    224                void(const QuicFrame&));
    225 };
    226 
    227 TEST_F(QuicPacketGeneratorTest, ShouldSendAck_NotWritable) {
    228   delegate_.SetCanNotWrite();
    229 
    230   generator_.SetShouldSendAck(false);
    231   EXPECT_TRUE(generator_.HasQueuedFrames());
    232 }
    233 
    234 TEST_F(QuicPacketGeneratorTest, ShouldSendAck_WritableAndShouldNotFlush) {
    235   StrictMock<MockDebugDelegate> debug_delegate;
    236 
    237   generator_.set_debug_delegate(&debug_delegate);
    238   delegate_.SetCanWriteOnlyNonRetransmittable();
    239   generator_.StartBatchOperations();
    240 
    241   EXPECT_CALL(delegate_, CreateAckFrame()).WillOnce(Return(CreateAckFrame()));
    242   EXPECT_CALL(debug_delegate, OnFrameAddedToPacket(_)).Times(1);
    243 
    244   generator_.SetShouldSendAck(false);
    245   EXPECT_TRUE(generator_.HasQueuedFrames());
    246 }
    247 
    248 TEST_F(QuicPacketGeneratorTest, ShouldSendAck_WritableAndShouldFlush) {
    249   delegate_.SetCanWriteOnlyNonRetransmittable();
    250 
    251   EXPECT_CALL(delegate_, CreateAckFrame()).WillOnce(Return(CreateAckFrame()));
    252   EXPECT_CALL(delegate_, OnSerializedPacket(_)).WillOnce(
    253       DoAll(SaveArg<0>(&packet_), Return(true)));
    254 
    255   generator_.SetShouldSendAck(false);
    256   EXPECT_FALSE(generator_.HasQueuedFrames());
    257 
    258   PacketContents contents;
    259   contents.num_ack_frames = 1;
    260   CheckPacketContains(contents, packet_);
    261 }
    262 
    263 TEST_F(QuicPacketGeneratorTest,
    264        ShouldSendAckWithFeedback_WritableAndShouldNotFlush) {
    265   delegate_.SetCanWriteOnlyNonRetransmittable();
    266   generator_.StartBatchOperations();
    267 
    268   EXPECT_CALL(delegate_, CreateAckFrame()).WillOnce(Return(CreateAckFrame()));
    269   EXPECT_CALL(delegate_, CreateFeedbackFrame()).WillOnce(
    270       Return(CreateFeedbackFrame()));
    271 
    272   generator_.SetShouldSendAck(true);
    273   EXPECT_TRUE(generator_.HasQueuedFrames());
    274 }
    275 
    276 TEST_F(QuicPacketGeneratorTest,
    277        ShouldSendAckWithFeedback_WritableAndShouldFlush) {
    278   delegate_.SetCanWriteOnlyNonRetransmittable();
    279 
    280   EXPECT_CALL(delegate_, CreateAckFrame()).WillOnce(Return(CreateAckFrame()));
    281   EXPECT_CALL(delegate_, CreateFeedbackFrame()).WillOnce(
    282       Return(CreateFeedbackFrame()));
    283 
    284   EXPECT_CALL(delegate_, OnSerializedPacket(_)).WillOnce(
    285       DoAll(SaveArg<0>(&packet_), Return(true)));
    286 
    287   generator_.SetShouldSendAck(true);
    288   EXPECT_FALSE(generator_.HasQueuedFrames());
    289 
    290   PacketContents contents;
    291   contents.num_ack_frames = 1;
    292   contents.num_feedback_frames = 1;
    293   CheckPacketContains(contents, packet_);
    294 }
    295 
    296 TEST_F(QuicPacketGeneratorTest, AddControlFrame_NotWritable) {
    297   delegate_.SetCanNotWrite();
    298 
    299   generator_.AddControlFrame(QuicFrame(CreateRstStreamFrame()));
    300   EXPECT_TRUE(generator_.HasQueuedFrames());
    301 }
    302 
    303 TEST_F(QuicPacketGeneratorTest, AddControlFrame_OnlyAckWritable) {
    304   delegate_.SetCanWriteOnlyNonRetransmittable();
    305 
    306   generator_.AddControlFrame(QuicFrame(CreateRstStreamFrame()));
    307   EXPECT_TRUE(generator_.HasQueuedFrames());
    308 }
    309 
    310 TEST_F(QuicPacketGeneratorTest, AddControlFrame_WritableAndShouldNotFlush) {
    311   delegate_.SetCanWriteAnything();
    312   generator_.StartBatchOperations();
    313 
    314   generator_.AddControlFrame(QuicFrame(CreateRstStreamFrame()));
    315   EXPECT_TRUE(generator_.HasQueuedFrames());
    316 }
    317 
    318 TEST_F(QuicPacketGeneratorTest, AddControlFrame_NotWritableBatchThenFlush) {
    319   delegate_.SetCanNotWrite();
    320   generator_.StartBatchOperations();
    321 
    322   generator_.AddControlFrame(QuicFrame(CreateRstStreamFrame()));
    323   EXPECT_TRUE(generator_.HasQueuedFrames());
    324   generator_.FinishBatchOperations();
    325   EXPECT_TRUE(generator_.HasQueuedFrames());
    326 
    327   EXPECT_CALL(delegate_, OnSerializedPacket(_)).WillOnce(
    328       DoAll(SaveArg<0>(&packet_), Return(true)));
    329   generator_.FlushAllQueuedFrames();
    330   EXPECT_FALSE(generator_.HasQueuedFrames());
    331 
    332   PacketContents contents;
    333   contents.num_rst_stream_frames = 1;
    334   CheckPacketContains(contents, packet_);
    335 }
    336 
    337 TEST_F(QuicPacketGeneratorTest, AddControlFrame_WritableAndShouldFlush) {
    338   delegate_.SetCanWriteAnything();
    339 
    340   EXPECT_CALL(delegate_, OnSerializedPacket(_)).WillOnce(
    341       DoAll(SaveArg<0>(&packet_), Return(true)));
    342 
    343   generator_.AddControlFrame(QuicFrame(CreateRstStreamFrame()));
    344   EXPECT_FALSE(generator_.HasQueuedFrames());
    345 
    346   PacketContents contents;
    347   contents.num_rst_stream_frames = 1;
    348   CheckPacketContains(contents, packet_);
    349 }
    350 
    351 TEST_F(QuicPacketGeneratorTest, ConsumeData_NotWritable) {
    352   delegate_.SetCanNotWrite();
    353 
    354   QuicConsumedData consumed = generator_.ConsumeData(1, MakeIOVector("foo"), 2,
    355                                                      true, NULL);
    356   EXPECT_EQ(0u, consumed.bytes_consumed);
    357   EXPECT_FALSE(consumed.fin_consumed);
    358   EXPECT_FALSE(generator_.HasQueuedFrames());
    359 }
    360 
    361 TEST_F(QuicPacketGeneratorTest, ConsumeData_WritableAndShouldNotFlush) {
    362   delegate_.SetCanWriteAnything();
    363   generator_.StartBatchOperations();
    364 
    365   QuicConsumedData consumed = generator_.ConsumeData(1, MakeIOVector("foo"), 2,
    366                                                      true, NULL);
    367   EXPECT_EQ(3u, consumed.bytes_consumed);
    368   EXPECT_TRUE(consumed.fin_consumed);
    369   EXPECT_TRUE(generator_.HasQueuedFrames());
    370 }
    371 
    372 TEST_F(QuicPacketGeneratorTest, ConsumeData_WritableAndShouldFlush) {
    373   delegate_.SetCanWriteAnything();
    374 
    375   EXPECT_CALL(delegate_, OnSerializedPacket(_)).WillOnce(
    376       DoAll(SaveArg<0>(&packet_), Return(true)));
    377   QuicConsumedData consumed = generator_.ConsumeData(1, MakeIOVector("foo"), 2,
    378                                                      true, NULL);
    379   EXPECT_EQ(3u, consumed.bytes_consumed);
    380   EXPECT_TRUE(consumed.fin_consumed);
    381   EXPECT_FALSE(generator_.HasQueuedFrames());
    382 
    383   PacketContents contents;
    384   contents.num_stream_frames = 1;
    385   CheckPacketContains(contents, packet_);
    386 }
    387 
    388 TEST_F(QuicPacketGeneratorTest,
    389        ConsumeDataMultipleTimes_WritableAndShouldNotFlush) {
    390   delegate_.SetCanWriteAnything();
    391   generator_.StartBatchOperations();
    392 
    393   generator_.ConsumeData(1, MakeIOVector("foo"), 2, true, NULL);
    394   QuicConsumedData consumed = generator_.ConsumeData(3, MakeIOVector("quux"), 7,
    395                                                      false, NULL);
    396   EXPECT_EQ(4u, consumed.bytes_consumed);
    397   EXPECT_FALSE(consumed.fin_consumed);
    398   EXPECT_TRUE(generator_.HasQueuedFrames());
    399 }
    400 
    401 TEST_F(QuicPacketGeneratorTest, ConsumeData_BatchOperations) {
    402   delegate_.SetCanWriteAnything();
    403   generator_.StartBatchOperations();
    404 
    405   generator_.ConsumeData(1, MakeIOVector("foo"), 2, true, NULL);
    406   QuicConsumedData consumed = generator_.ConsumeData(3, MakeIOVector("quux"), 7,
    407                                                      false, NULL);
    408   EXPECT_EQ(4u, consumed.bytes_consumed);
    409   EXPECT_FALSE(consumed.fin_consumed);
    410   EXPECT_TRUE(generator_.HasQueuedFrames());
    411 
    412   // Now both frames will be flushed out.
    413   EXPECT_CALL(delegate_, OnSerializedPacket(_)).WillOnce(
    414       DoAll(SaveArg<0>(&packet_), Return(true)));
    415   generator_.FinishBatchOperations();
    416   EXPECT_FALSE(generator_.HasQueuedFrames());
    417 
    418   PacketContents contents;
    419   contents.num_stream_frames = 2;
    420   CheckPacketContains(contents, packet_);
    421 }
    422 
    423 TEST_F(QuicPacketGeneratorTest, ConsumeDataFEC) {
    424   delegate_.SetCanWriteAnything();
    425 
    426   // Send FEC every two packets.
    427   creator_.options()->max_packets_per_fec_group = 2;
    428 
    429   {
    430     InSequence dummy;
    431     EXPECT_CALL(delegate_, OnSerializedPacket(_)).WillOnce(
    432         DoAll(SaveArg<0>(&packet_), Return(true)));
    433     EXPECT_CALL(delegate_, OnSerializedPacket(_)).WillOnce(
    434         DoAll(SaveArg<0>(&packet2_), Return(true)));
    435     EXPECT_CALL(delegate_, OnSerializedPacket(_)).WillOnce(
    436         DoAll(SaveArg<0>(&packet3_), Return(true)));
    437     EXPECT_CALL(delegate_, OnSerializedPacket(_)).WillOnce(
    438         DoAll(SaveArg<0>(&packet4_), Return(true)));
    439     EXPECT_CALL(delegate_, OnSerializedPacket(_)).WillOnce(
    440         DoAll(SaveArg<0>(&packet5_), Return(true)));
    441   }
    442 
    443   // Send enough data to create 3 packets: two full and one partial.
    444   size_t data_len = 2 * kDefaultMaxPacketSize + 100;
    445   QuicConsumedData consumed =
    446       generator_.ConsumeData(3, CreateData(data_len), 0, true, NULL);
    447   EXPECT_EQ(data_len, consumed.bytes_consumed);
    448   EXPECT_TRUE(consumed.fin_consumed);
    449   EXPECT_FALSE(generator_.HasQueuedFrames());
    450 
    451   CheckPacketHasSingleStreamFrame(packet_);
    452   CheckPacketHasSingleStreamFrame(packet2_);
    453   CheckPacketIsFec(packet3_, 1);
    454 
    455   CheckPacketHasSingleStreamFrame(packet4_);
    456   CheckPacketIsFec(packet5_, 4);
    457 }
    458 
    459 TEST_F(QuicPacketGeneratorTest, ConsumeDataSendsFecAtEnd) {
    460   delegate_.SetCanWriteAnything();
    461 
    462   // Send FEC every six packets.
    463   creator_.options()->max_packets_per_fec_group = 6;
    464 
    465   {
    466     InSequence dummy;
    467     EXPECT_CALL(delegate_, OnSerializedPacket(_)).WillOnce(
    468         DoAll(SaveArg<0>(&packet_), Return(true)));
    469     EXPECT_CALL(delegate_, OnSerializedPacket(_)).WillOnce(
    470         DoAll(SaveArg<0>(&packet2_), Return(true)));
    471     EXPECT_CALL(delegate_, OnSerializedPacket(_)).WillOnce(
    472         DoAll(SaveArg<0>(&packet3_), Return(true)));
    473   }
    474 
    475   // Send enough data to create 2 packets: one full and one partial.
    476   size_t data_len = 1 * kDefaultMaxPacketSize + 100;
    477   QuicConsumedData consumed =
    478       generator_.ConsumeData(3, CreateData(data_len), 0, true, NULL);
    479   EXPECT_EQ(data_len, consumed.bytes_consumed);
    480   EXPECT_TRUE(consumed.fin_consumed);
    481   EXPECT_FALSE(generator_.HasQueuedFrames());
    482 
    483   CheckPacketHasSingleStreamFrame(packet_);
    484   CheckPacketHasSingleStreamFrame(packet2_);
    485   CheckPacketIsFec(packet3_, 1);
    486 }
    487 
    488 TEST_F(QuicPacketGeneratorTest, ConsumeData_FramesPreviouslyQueued) {
    489   // Set the packet size be enough for two stream frames with 0 stream offset,
    490   // but not enough for a stream frame of 0 offset and one with non-zero offset.
    491   creator_.options()->max_packet_length =
    492       NullEncrypter().GetCiphertextSize(0) +
    493       GetPacketHeaderSize(creator_.options()->send_guid_length,
    494                           true,
    495                           creator_.options()->send_sequence_number_length,
    496                           NOT_IN_FEC_GROUP) +
    497       // Add an extra 3 bytes for the payload and 1 byte so BytesFree is larger
    498       // than the GetMinStreamFrameSize.
    499       QuicFramer::GetMinStreamFrameSize(framer_.version(), 1, 0, false) + 3 +
    500       QuicFramer::GetMinStreamFrameSize(framer_.version(), 1, 0, true) + 1;
    501   delegate_.SetCanWriteAnything();
    502   {
    503      InSequence dummy;
    504      EXPECT_CALL(delegate_, OnSerializedPacket(_)).WillOnce(
    505          DoAll(SaveArg<0>(&packet_), Return(true)));
    506      EXPECT_CALL(delegate_, OnSerializedPacket(_)).WillOnce(
    507          DoAll(SaveArg<0>(&packet2_), Return(true)));
    508   }
    509   generator_.StartBatchOperations();
    510   // Queue enough data to prevent a stream frame with a non-zero offset from
    511   // fitting.
    512   QuicConsumedData consumed = generator_.ConsumeData(1, MakeIOVector("foo"), 0,
    513                                                      false, NULL);
    514   EXPECT_EQ(3u, consumed.bytes_consumed);
    515   EXPECT_FALSE(consumed.fin_consumed);
    516   EXPECT_TRUE(generator_.HasQueuedFrames());
    517 
    518   // This frame will not fit with the existing frame, causing the queued frame
    519   // to be serialized, and it will not fit with another frame like it, so it is
    520   // serialized by itself.
    521   consumed = generator_.ConsumeData(1, MakeIOVector("bar"), 3, true, NULL);
    522   EXPECT_EQ(3u, consumed.bytes_consumed);
    523   EXPECT_TRUE(consumed.fin_consumed);
    524   EXPECT_FALSE(generator_.HasQueuedFrames());
    525 
    526   PacketContents contents;
    527   contents.num_stream_frames = 1;
    528   CheckPacketContains(contents, packet_);
    529   CheckPacketContains(contents, packet2_);
    530 }
    531 
    532 TEST_F(QuicPacketGeneratorTest, NotWritableThenBatchOperations) {
    533   delegate_.SetCanNotWrite();
    534 
    535   generator_.SetShouldSendAck(true);
    536   generator_.AddControlFrame(QuicFrame(CreateRstStreamFrame()));
    537   EXPECT_TRUE(generator_.HasQueuedFrames());
    538 
    539   delegate_.SetCanWriteAnything();
    540 
    541   generator_.StartBatchOperations();
    542 
    543   // When the first write operation is invoked, the ack and feedback
    544   // frames will be returned.
    545   EXPECT_CALL(delegate_, CreateAckFrame()).WillOnce(Return(CreateAckFrame()));
    546   EXPECT_CALL(delegate_, CreateFeedbackFrame()).WillOnce(
    547       Return(CreateFeedbackFrame()));
    548 
    549   // Send some data and a control frame
    550   generator_.ConsumeData(3, MakeIOVector("quux"), 7, false, NULL);
    551   generator_.AddControlFrame(QuicFrame(CreateGoAwayFrame()));
    552 
    553   // All five frames will be flushed out in a single packet.
    554   EXPECT_CALL(delegate_, OnSerializedPacket(_)).WillOnce(
    555       DoAll(SaveArg<0>(&packet_), Return(true)));
    556   generator_.FinishBatchOperations();
    557   EXPECT_FALSE(generator_.HasQueuedFrames());
    558 
    559   PacketContents contents;
    560   contents.num_ack_frames = 1;
    561   contents.num_goaway_frames = 1;
    562   contents.num_feedback_frames = 1;
    563   contents.num_rst_stream_frames = 1;
    564   contents.num_stream_frames = 1;
    565   CheckPacketContains(contents, packet_);
    566 }
    567 
    568 TEST_F(QuicPacketGeneratorTest, NotWritableThenBatchOperations2) {
    569   delegate_.SetCanNotWrite();
    570 
    571   generator_.SetShouldSendAck(true);
    572   generator_.AddControlFrame(QuicFrame(CreateRstStreamFrame()));
    573   EXPECT_TRUE(generator_.HasQueuedFrames());
    574 
    575   delegate_.SetCanWriteAnything();
    576 
    577   generator_.StartBatchOperations();
    578 
    579   // When the first write operation is invoked, the ack and feedback
    580   // frames will be returned.
    581   EXPECT_CALL(delegate_, CreateAckFrame()).WillOnce(Return(CreateAckFrame()));
    582   EXPECT_CALL(delegate_, CreateFeedbackFrame()).WillOnce(
    583       Return(CreateFeedbackFrame()));
    584 
    585   {
    586     InSequence dummy;
    587   // All five frames will be flushed out in a single packet
    588   EXPECT_CALL(delegate_, OnSerializedPacket(_)).WillOnce(
    589       DoAll(SaveArg<0>(&packet_), Return(true)));
    590   EXPECT_CALL(delegate_, OnSerializedPacket(_)).WillOnce(
    591       DoAll(SaveArg<0>(&packet2_), Return(true)));
    592   }
    593 
    594   // Send enough data to exceed one packet
    595   size_t data_len = kDefaultMaxPacketSize + 100;
    596   QuicConsumedData consumed =
    597       generator_.ConsumeData(3, CreateData(data_len), 0, true, NULL);
    598   EXPECT_EQ(data_len, consumed.bytes_consumed);
    599   EXPECT_TRUE(consumed.fin_consumed);
    600   generator_.AddControlFrame(QuicFrame(CreateGoAwayFrame()));
    601 
    602   generator_.FinishBatchOperations();
    603   EXPECT_FALSE(generator_.HasQueuedFrames());
    604 
    605   // The first packet should have the queued data and part of the stream data.
    606   PacketContents contents;
    607   contents.num_ack_frames = 1;
    608   contents.num_feedback_frames = 1;
    609   contents.num_rst_stream_frames = 1;
    610   contents.num_stream_frames = 1;
    611   CheckPacketContains(contents, packet_);
    612 
    613   // The second should have the remainder of the stream data.
    614   PacketContents contents2;
    615   contents2.num_goaway_frames = 1;
    616   contents2.num_stream_frames = 1;
    617   CheckPacketContains(contents2, packet2_);
    618 }
    619 
    620 }  // namespace test
    621 }  // namespace net
    622