1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "net/quic/quic_packet_generator.h"
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"
19 using base::StringPiece
;
21 using testing::InSequence
;
22 using testing::Return
;
23 using testing::SaveArg
;
24 using testing::StrictMock
;
31 class MockDelegate
: public QuicPacketGenerator::DelegateInterface
{
34 virtual ~MockDelegate() {}
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_METHOD0(CreateStopWaitingFrame
, QuicStopWaitingFrame
*());
43 MOCK_METHOD1(OnSerializedPacket
, bool(const SerializedPacket
& packet
));
44 MOCK_METHOD2(CloseConnection
, void(QuicErrorCode
, bool));
46 void SetCanWriteAnything() {
47 EXPECT_CALL(*this, ShouldGeneratePacket(NOT_RETRANSMISSION
, _
, _
))
48 .WillRepeatedly(Return(true));
49 EXPECT_CALL(*this, ShouldGeneratePacket(NOT_RETRANSMISSION
,
50 NO_RETRANSMITTABLE_DATA
, _
))
51 .WillRepeatedly(Return(true));
54 void SetCanNotWrite() {
55 EXPECT_CALL(*this, ShouldGeneratePacket(NOT_RETRANSMISSION
, _
, _
))
56 .WillRepeatedly(Return(false));
57 EXPECT_CALL(*this, ShouldGeneratePacket(NOT_RETRANSMISSION
,
58 NO_RETRANSMITTABLE_DATA
, _
))
59 .WillRepeatedly(Return(false));
62 // Use this when only ack and feedback frames should be allowed to be written.
63 void SetCanWriteOnlyNonRetransmittable() {
64 EXPECT_CALL(*this, ShouldGeneratePacket(NOT_RETRANSMISSION
, _
, _
))
65 .WillRepeatedly(Return(false));
66 EXPECT_CALL(*this, ShouldGeneratePacket(NOT_RETRANSMISSION
,
67 NO_RETRANSMITTABLE_DATA
, _
))
68 .WillRepeatedly(Return(true));
72 DISALLOW_COPY_AND_ASSIGN(MockDelegate
);
75 // Simple struct for describing the contents of a packet.
76 // Useful in conjunction with a SimpleQuicFrame for validating
77 // that a packet contains the expected frames.
78 struct PacketContents
{
81 num_connection_close_frames(0),
82 num_feedback_frames(0),
84 num_rst_stream_frames(0),
85 num_stop_waiting_frames(0),
90 size_t num_ack_frames
;
91 size_t num_connection_close_frames
;
92 size_t num_feedback_frames
;
93 size_t num_goaway_frames
;
94 size_t num_rst_stream_frames
;
95 size_t num_stop_waiting_frames
;
96 size_t num_stream_frames
;
98 QuicFecGroupNumber fec_group
;
103 class QuicPacketGeneratorTest
: public ::testing::Test
{
105 QuicPacketGeneratorTest()
106 : framer_(QuicSupportedVersions(), QuicTime::Zero(), false),
107 creator_(42, &framer_
, &random_
, false),
108 generator_(&delegate_
, NULL
, &creator_
),
109 packet_(0, PACKET_1BYTE_SEQUENCE_NUMBER
, NULL
, 0, NULL
),
110 packet2_(0, PACKET_1BYTE_SEQUENCE_NUMBER
, NULL
, 0, NULL
),
111 packet3_(0, PACKET_1BYTE_SEQUENCE_NUMBER
, NULL
, 0, NULL
),
112 packet4_(0, PACKET_1BYTE_SEQUENCE_NUMBER
, NULL
, 0, NULL
),
113 packet5_(0, PACKET_1BYTE_SEQUENCE_NUMBER
, NULL
, 0, NULL
) {
116 ~QuicPacketGeneratorTest() {
117 delete packet_
.packet
;
118 delete packet_
.retransmittable_frames
;
119 delete packet2_
.packet
;
120 delete packet2_
.retransmittable_frames
;
121 delete packet3_
.packet
;
122 delete packet3_
.retransmittable_frames
;
123 delete packet4_
.packet
;
124 delete packet4_
.retransmittable_frames
;
125 delete packet5_
.packet
;
126 delete packet5_
.retransmittable_frames
;
129 QuicAckFrame
* CreateAckFrame() {
130 // TODO(rch): Initialize this so it can be verified later.
131 return new QuicAckFrame(0, QuicTime::Zero(), 0);
134 QuicCongestionFeedbackFrame
* CreateFeedbackFrame() {
135 QuicCongestionFeedbackFrame
* frame
= new QuicCongestionFeedbackFrame
;
136 frame
->type
= kFixRate
;
137 frame
->fix_rate
.bitrate
= QuicBandwidth::FromBytesPerSecond(42);
141 QuicStopWaitingFrame
* CreateStopWaitingFrame() {
142 QuicStopWaitingFrame
* frame
= new QuicStopWaitingFrame();
143 frame
->entropy_hash
= 0;
144 frame
->least_unacked
= 0;
148 QuicRstStreamFrame
* CreateRstStreamFrame() {
149 return new QuicRstStreamFrame(1, QUIC_STREAM_NO_ERROR
, 0);
152 QuicGoAwayFrame
* CreateGoAwayFrame() {
153 return new QuicGoAwayFrame(QUIC_NO_ERROR
, 1, string());
156 void CheckPacketContains(const PacketContents
& contents
,
157 const SerializedPacket
& packet
) {
158 size_t num_retransmittable_frames
= contents
.num_connection_close_frames
+
159 contents
.num_goaway_frames
+ contents
.num_rst_stream_frames
+
160 contents
.num_stream_frames
;
161 size_t num_frames
= contents
.num_feedback_frames
+ contents
.num_ack_frames
+
162 contents
.num_stop_waiting_frames
+ num_retransmittable_frames
;
164 if (num_retransmittable_frames
== 0) {
165 ASSERT_TRUE(packet
.retransmittable_frames
== NULL
);
167 ASSERT_TRUE(packet
.retransmittable_frames
!= NULL
);
168 EXPECT_EQ(num_retransmittable_frames
,
169 packet
.retransmittable_frames
->frames().size());
172 ASSERT_TRUE(packet
.packet
!= NULL
);
173 ASSERT_TRUE(simple_framer_
.ProcessPacket(*packet
.packet
));
174 EXPECT_EQ(num_frames
, simple_framer_
.num_frames());
175 EXPECT_EQ(contents
.num_ack_frames
, simple_framer_
.ack_frames().size());
176 EXPECT_EQ(contents
.num_connection_close_frames
,
177 simple_framer_
.connection_close_frames().size());
178 EXPECT_EQ(contents
.num_feedback_frames
,
179 simple_framer_
.feedback_frames().size());
180 EXPECT_EQ(contents
.num_goaway_frames
,
181 simple_framer_
.goaway_frames().size());
182 EXPECT_EQ(contents
.num_rst_stream_frames
,
183 simple_framer_
.rst_stream_frames().size());
184 EXPECT_EQ(contents
.num_stream_frames
,
185 simple_framer_
.stream_frames().size());
186 EXPECT_EQ(contents
.num_stop_waiting_frames
,
187 simple_framer_
.stop_waiting_frames().size());
188 EXPECT_EQ(contents
.fec_group
, simple_framer_
.header().fec_group
);
191 void CheckPacketHasSingleStreamFrame(const SerializedPacket
& packet
) {
192 ASSERT_TRUE(packet
.retransmittable_frames
!= NULL
);
193 EXPECT_EQ(1u, packet
.retransmittable_frames
->frames().size());
194 ASSERT_TRUE(packet
.packet
!= NULL
);
195 ASSERT_TRUE(simple_framer_
.ProcessPacket(*packet
.packet
));
196 EXPECT_EQ(1u, simple_framer_
.num_frames());
197 EXPECT_EQ(1u, simple_framer_
.stream_frames().size());
200 void CheckPacketIsFec(const SerializedPacket
& packet
,
201 QuicPacketSequenceNumber fec_group
) {
202 ASSERT_TRUE(packet
.retransmittable_frames
== NULL
);
203 ASSERT_TRUE(packet
.packet
!= NULL
);
204 ASSERT_TRUE(simple_framer_
.ProcessPacket(*packet
.packet
));
205 EXPECT_TRUE(simple_framer_
.header().fec_flag
);
206 EXPECT_EQ(fec_group
, simple_framer_
.fec_data().fec_group
);
209 IOVector
CreateData(size_t len
) {
210 data_array_
.reset(new char[len
]);
211 memset(data_array_
.get(), '?', len
);
213 data
.Append(data_array_
.get(), len
);
219 QuicPacketCreator creator_
;
220 StrictMock
<MockDelegate
> delegate_
;
221 QuicPacketGenerator generator_
;
222 SimpleQuicFramer simple_framer_
;
223 SerializedPacket packet_
;
224 SerializedPacket packet2_
;
225 SerializedPacket packet3_
;
226 SerializedPacket packet4_
;
227 SerializedPacket packet5_
;
230 scoped_ptr
<char[]> data_array_
;
233 class MockDebugDelegate
: public QuicPacketGenerator::DebugDelegateInterface
{
235 MOCK_METHOD1(OnFrameAddedToPacket
,
236 void(const QuicFrame
&));
239 TEST_F(QuicPacketGeneratorTest
, ShouldSendAck_NotWritable
) {
240 delegate_
.SetCanNotWrite();
242 generator_
.SetShouldSendAck(false, false);
243 EXPECT_TRUE(generator_
.HasQueuedFrames());
246 TEST_F(QuicPacketGeneratorTest
, ShouldSendAck_WritableAndShouldNotFlush
) {
247 StrictMock
<MockDebugDelegate
> debug_delegate
;
249 generator_
.set_debug_delegate(&debug_delegate
);
250 delegate_
.SetCanWriteOnlyNonRetransmittable();
251 generator_
.StartBatchOperations();
253 EXPECT_CALL(delegate_
, CreateAckFrame()).WillOnce(Return(CreateAckFrame()));
254 EXPECT_CALL(debug_delegate
, OnFrameAddedToPacket(_
)).Times(1);
256 generator_
.SetShouldSendAck(false, false);
257 EXPECT_TRUE(generator_
.HasQueuedFrames());
260 TEST_F(QuicPacketGeneratorTest
, ShouldSendAck_WritableAndShouldFlush
) {
261 delegate_
.SetCanWriteOnlyNonRetransmittable();
263 EXPECT_CALL(delegate_
, CreateAckFrame()).WillOnce(Return(CreateAckFrame()));
264 EXPECT_CALL(delegate_
, OnSerializedPacket(_
)).WillOnce(
265 DoAll(SaveArg
<0>(&packet_
), Return(true)));
267 generator_
.SetShouldSendAck(false, false);
268 EXPECT_FALSE(generator_
.HasQueuedFrames());
270 PacketContents contents
;
271 contents
.num_ack_frames
= 1;
272 CheckPacketContains(contents
, packet_
);
275 TEST_F(QuicPacketGeneratorTest
,
276 ShouldSendAckWithFeedback_WritableAndShouldNotFlush
) {
277 delegate_
.SetCanWriteOnlyNonRetransmittable();
278 generator_
.StartBatchOperations();
280 EXPECT_CALL(delegate_
, CreateAckFrame()).WillOnce(Return(CreateAckFrame()));
281 EXPECT_CALL(delegate_
, CreateFeedbackFrame()).WillOnce(
282 Return(CreateFeedbackFrame()));
284 generator_
.SetShouldSendAck(true, false);
285 EXPECT_TRUE(generator_
.HasQueuedFrames());
288 TEST_F(QuicPacketGeneratorTest
,
289 ShouldSendAckWithFeedback_WritableAndShouldFlush
) {
290 delegate_
.SetCanWriteOnlyNonRetransmittable();
292 EXPECT_CALL(delegate_
, CreateAckFrame()).WillOnce(Return(CreateAckFrame()));
293 EXPECT_CALL(delegate_
, CreateFeedbackFrame()).WillOnce(
294 Return(CreateFeedbackFrame()));
295 EXPECT_CALL(delegate_
, CreateStopWaitingFrame()).WillOnce(
296 Return(CreateStopWaitingFrame()));
298 EXPECT_CALL(delegate_
, OnSerializedPacket(_
)).WillOnce(
299 DoAll(SaveArg
<0>(&packet_
), Return(true)));
301 generator_
.SetShouldSendAck(true, true);
302 EXPECT_FALSE(generator_
.HasQueuedFrames());
304 PacketContents contents
;
305 contents
.num_ack_frames
= 1;
306 contents
.num_feedback_frames
= 1;
307 contents
.num_stop_waiting_frames
= 1;
308 CheckPacketContains(contents
, packet_
);
311 TEST_F(QuicPacketGeneratorTest
, AddControlFrame_NotWritable
) {
312 delegate_
.SetCanNotWrite();
314 generator_
.AddControlFrame(QuicFrame(CreateRstStreamFrame()));
315 EXPECT_TRUE(generator_
.HasQueuedFrames());
318 TEST_F(QuicPacketGeneratorTest
, AddControlFrame_OnlyAckWritable
) {
319 delegate_
.SetCanWriteOnlyNonRetransmittable();
321 generator_
.AddControlFrame(QuicFrame(CreateRstStreamFrame()));
322 EXPECT_TRUE(generator_
.HasQueuedFrames());
325 TEST_F(QuicPacketGeneratorTest
, AddControlFrame_WritableAndShouldNotFlush
) {
326 delegate_
.SetCanWriteAnything();
327 generator_
.StartBatchOperations();
329 generator_
.AddControlFrame(QuicFrame(CreateRstStreamFrame()));
330 EXPECT_TRUE(generator_
.HasQueuedFrames());
333 TEST_F(QuicPacketGeneratorTest
, AddControlFrame_NotWritableBatchThenFlush
) {
334 delegate_
.SetCanNotWrite();
335 generator_
.StartBatchOperations();
337 generator_
.AddControlFrame(QuicFrame(CreateRstStreamFrame()));
338 EXPECT_TRUE(generator_
.HasQueuedFrames());
339 generator_
.FinishBatchOperations();
340 EXPECT_TRUE(generator_
.HasQueuedFrames());
342 EXPECT_CALL(delegate_
, OnSerializedPacket(_
)).WillOnce(
343 DoAll(SaveArg
<0>(&packet_
), Return(true)));
344 generator_
.FlushAllQueuedFrames();
345 EXPECT_FALSE(generator_
.HasQueuedFrames());
347 PacketContents contents
;
348 contents
.num_rst_stream_frames
= 1;
349 CheckPacketContains(contents
, packet_
);
352 TEST_F(QuicPacketGeneratorTest
, AddControlFrame_WritableAndShouldFlush
) {
353 delegate_
.SetCanWriteAnything();
355 EXPECT_CALL(delegate_
, OnSerializedPacket(_
)).WillOnce(
356 DoAll(SaveArg
<0>(&packet_
), Return(true)));
358 generator_
.AddControlFrame(QuicFrame(CreateRstStreamFrame()));
359 EXPECT_FALSE(generator_
.HasQueuedFrames());
361 PacketContents contents
;
362 contents
.num_rst_stream_frames
= 1;
363 CheckPacketContains(contents
, packet_
);
366 TEST_F(QuicPacketGeneratorTest
, ConsumeData_NotWritable
) {
367 delegate_
.SetCanNotWrite();
369 QuicConsumedData consumed
= generator_
.ConsumeData(1, MakeIOVector("foo"), 2,
371 EXPECT_EQ(0u, consumed
.bytes_consumed
);
372 EXPECT_FALSE(consumed
.fin_consumed
);
373 EXPECT_FALSE(generator_
.HasQueuedFrames());
376 TEST_F(QuicPacketGeneratorTest
, ConsumeData_WritableAndShouldNotFlush
) {
377 delegate_
.SetCanWriteAnything();
378 generator_
.StartBatchOperations();
380 QuicConsumedData consumed
= generator_
.ConsumeData(1, MakeIOVector("foo"), 2,
382 EXPECT_EQ(3u, consumed
.bytes_consumed
);
383 EXPECT_TRUE(consumed
.fin_consumed
);
384 EXPECT_TRUE(generator_
.HasQueuedFrames());
387 TEST_F(QuicPacketGeneratorTest
, ConsumeData_WritableAndShouldFlush
) {
388 delegate_
.SetCanWriteAnything();
390 EXPECT_CALL(delegate_
, OnSerializedPacket(_
)).WillOnce(
391 DoAll(SaveArg
<0>(&packet_
), Return(true)));
392 QuicConsumedData consumed
= generator_
.ConsumeData(1, MakeIOVector("foo"), 2,
394 EXPECT_EQ(3u, consumed
.bytes_consumed
);
395 EXPECT_TRUE(consumed
.fin_consumed
);
396 EXPECT_FALSE(generator_
.HasQueuedFrames());
398 PacketContents contents
;
399 contents
.num_stream_frames
= 1;
400 CheckPacketContains(contents
, packet_
);
403 TEST_F(QuicPacketGeneratorTest
,
404 ConsumeDataMultipleTimes_WritableAndShouldNotFlush
) {
405 delegate_
.SetCanWriteAnything();
406 generator_
.StartBatchOperations();
408 generator_
.ConsumeData(1, MakeIOVector("foo"), 2, true, NULL
);
409 QuicConsumedData consumed
= generator_
.ConsumeData(3, MakeIOVector("quux"), 7,
411 EXPECT_EQ(4u, consumed
.bytes_consumed
);
412 EXPECT_FALSE(consumed
.fin_consumed
);
413 EXPECT_TRUE(generator_
.HasQueuedFrames());
416 TEST_F(QuicPacketGeneratorTest
, ConsumeData_BatchOperations
) {
417 delegate_
.SetCanWriteAnything();
418 generator_
.StartBatchOperations();
420 generator_
.ConsumeData(1, MakeIOVector("foo"), 2, true, NULL
);
421 QuicConsumedData consumed
= generator_
.ConsumeData(3, MakeIOVector("quux"), 7,
423 EXPECT_EQ(4u, consumed
.bytes_consumed
);
424 EXPECT_FALSE(consumed
.fin_consumed
);
425 EXPECT_TRUE(generator_
.HasQueuedFrames());
427 // Now both frames will be flushed out.
428 EXPECT_CALL(delegate_
, OnSerializedPacket(_
)).WillOnce(
429 DoAll(SaveArg
<0>(&packet_
), Return(true)));
430 generator_
.FinishBatchOperations();
431 EXPECT_FALSE(generator_
.HasQueuedFrames());
433 PacketContents contents
;
434 contents
.num_stream_frames
= 2;
435 CheckPacketContains(contents
, packet_
);
438 TEST_F(QuicPacketGeneratorTest
, ConsumeDataFEC
) {
439 delegate_
.SetCanWriteAnything();
441 // Send FEC every two packets.
442 creator_
.options()->max_packets_per_fec_group
= 2;
446 EXPECT_CALL(delegate_
, OnSerializedPacket(_
)).WillOnce(
447 DoAll(SaveArg
<0>(&packet_
), Return(true)));
448 EXPECT_CALL(delegate_
, OnSerializedPacket(_
)).WillOnce(
449 DoAll(SaveArg
<0>(&packet2_
), Return(true)));
450 EXPECT_CALL(delegate_
, OnSerializedPacket(_
)).WillOnce(
451 DoAll(SaveArg
<0>(&packet3_
), Return(true)));
452 EXPECT_CALL(delegate_
, OnSerializedPacket(_
)).WillOnce(
453 DoAll(SaveArg
<0>(&packet4_
), Return(true)));
454 EXPECT_CALL(delegate_
, OnSerializedPacket(_
)).WillOnce(
455 DoAll(SaveArg
<0>(&packet5_
), Return(true)));
458 // Send enough data to create 3 packets: two full and one partial.
459 size_t data_len
= 2 * kDefaultMaxPacketSize
+ 100;
460 QuicConsumedData consumed
=
461 generator_
.ConsumeData(3, CreateData(data_len
), 0, true, NULL
);
462 EXPECT_EQ(data_len
, consumed
.bytes_consumed
);
463 EXPECT_TRUE(consumed
.fin_consumed
);
464 EXPECT_FALSE(generator_
.HasQueuedFrames());
466 CheckPacketHasSingleStreamFrame(packet_
);
467 CheckPacketHasSingleStreamFrame(packet2_
);
468 CheckPacketIsFec(packet3_
, 1);
470 CheckPacketHasSingleStreamFrame(packet4_
);
471 CheckPacketIsFec(packet5_
, 4);
474 TEST_F(QuicPacketGeneratorTest
, ConsumeDataSendsFecAtEnd
) {
475 delegate_
.SetCanWriteAnything();
477 // Send FEC every six packets.
478 creator_
.options()->max_packets_per_fec_group
= 6;
482 EXPECT_CALL(delegate_
, OnSerializedPacket(_
)).WillOnce(
483 DoAll(SaveArg
<0>(&packet_
), Return(true)));
484 EXPECT_CALL(delegate_
, OnSerializedPacket(_
)).WillOnce(
485 DoAll(SaveArg
<0>(&packet2_
), Return(true)));
486 EXPECT_CALL(delegate_
, OnSerializedPacket(_
)).WillOnce(
487 DoAll(SaveArg
<0>(&packet3_
), Return(true)));
490 // Send enough data to create 2 packets: one full and one partial.
491 size_t data_len
= 1 * kDefaultMaxPacketSize
+ 100;
492 QuicConsumedData consumed
=
493 generator_
.ConsumeData(3, CreateData(data_len
), 0, true, NULL
);
494 EXPECT_EQ(data_len
, consumed
.bytes_consumed
);
495 EXPECT_TRUE(consumed
.fin_consumed
);
496 EXPECT_FALSE(generator_
.HasQueuedFrames());
498 CheckPacketHasSingleStreamFrame(packet_
);
499 CheckPacketHasSingleStreamFrame(packet2_
);
500 CheckPacketIsFec(packet3_
, 1);
503 TEST_F(QuicPacketGeneratorTest
, ConsumeData_FramesPreviouslyQueued
) {
504 // Set the packet size be enough for two stream frames with 0 stream offset,
505 // but not enough for a stream frame of 0 offset and one with non-zero offset.
506 creator_
.options()->max_packet_length
=
507 NullEncrypter().GetCiphertextSize(0) +
508 GetPacketHeaderSize(creator_
.options()->send_connection_id_length
,
510 creator_
.options()->send_sequence_number_length
,
512 // Add an extra 3 bytes for the payload and 1 byte so BytesFree is larger
513 // than the GetMinStreamFrameSize.
514 QuicFramer::GetMinStreamFrameSize(framer_
.version(), 1, 0, false) + 3 +
515 QuicFramer::GetMinStreamFrameSize(framer_
.version(), 1, 0, true) + 1;
516 delegate_
.SetCanWriteAnything();
519 EXPECT_CALL(delegate_
, OnSerializedPacket(_
)).WillOnce(
520 DoAll(SaveArg
<0>(&packet_
), Return(true)));
521 EXPECT_CALL(delegate_
, OnSerializedPacket(_
)).WillOnce(
522 DoAll(SaveArg
<0>(&packet2_
), Return(true)));
524 generator_
.StartBatchOperations();
525 // Queue enough data to prevent a stream frame with a non-zero offset from
527 QuicConsumedData consumed
= generator_
.ConsumeData(1, MakeIOVector("foo"), 0,
529 EXPECT_EQ(3u, consumed
.bytes_consumed
);
530 EXPECT_FALSE(consumed
.fin_consumed
);
531 EXPECT_TRUE(generator_
.HasQueuedFrames());
533 // This frame will not fit with the existing frame, causing the queued frame
534 // to be serialized, and it will not fit with another frame like it, so it is
535 // serialized by itself.
536 consumed
= generator_
.ConsumeData(1, MakeIOVector("bar"), 3, true, NULL
);
537 EXPECT_EQ(3u, consumed
.bytes_consumed
);
538 EXPECT_TRUE(consumed
.fin_consumed
);
539 EXPECT_FALSE(generator_
.HasQueuedFrames());
541 PacketContents contents
;
542 contents
.num_stream_frames
= 1;
543 CheckPacketContains(contents
, packet_
);
544 CheckPacketContains(contents
, packet2_
);
547 TEST_F(QuicPacketGeneratorTest
, NotWritableThenBatchOperations
) {
548 delegate_
.SetCanNotWrite();
550 generator_
.SetShouldSendAck(true, false);
551 generator_
.AddControlFrame(QuicFrame(CreateRstStreamFrame()));
552 EXPECT_TRUE(generator_
.HasQueuedFrames());
554 delegate_
.SetCanWriteAnything();
556 generator_
.StartBatchOperations();
558 // When the first write operation is invoked, the ack and feedback
559 // frames will be returned.
560 EXPECT_CALL(delegate_
, CreateAckFrame()).WillOnce(Return(CreateAckFrame()));
561 EXPECT_CALL(delegate_
, CreateFeedbackFrame()).WillOnce(
562 Return(CreateFeedbackFrame()));
564 // Send some data and a control frame
565 generator_
.ConsumeData(3, MakeIOVector("quux"), 7, false, NULL
);
566 generator_
.AddControlFrame(QuicFrame(CreateGoAwayFrame()));
568 // All five frames will be flushed out in a single packet.
569 EXPECT_CALL(delegate_
, OnSerializedPacket(_
)).WillOnce(
570 DoAll(SaveArg
<0>(&packet_
), Return(true)));
571 generator_
.FinishBatchOperations();
572 EXPECT_FALSE(generator_
.HasQueuedFrames());
574 PacketContents contents
;
575 contents
.num_ack_frames
= 1;
576 contents
.num_goaway_frames
= 1;
577 contents
.num_feedback_frames
= 1;
578 contents
.num_rst_stream_frames
= 1;
579 contents
.num_stream_frames
= 1;
580 CheckPacketContains(contents
, packet_
);
583 TEST_F(QuicPacketGeneratorTest
, NotWritableThenBatchOperations2
) {
584 delegate_
.SetCanNotWrite();
586 generator_
.SetShouldSendAck(true, false);
587 generator_
.AddControlFrame(QuicFrame(CreateRstStreamFrame()));
588 EXPECT_TRUE(generator_
.HasQueuedFrames());
590 delegate_
.SetCanWriteAnything();
592 generator_
.StartBatchOperations();
594 // When the first write operation is invoked, the ack and feedback
595 // frames will be returned.
596 EXPECT_CALL(delegate_
, CreateAckFrame()).WillOnce(Return(CreateAckFrame()));
597 EXPECT_CALL(delegate_
, CreateFeedbackFrame()).WillOnce(
598 Return(CreateFeedbackFrame()));
602 // All five frames will be flushed out in a single packet
603 EXPECT_CALL(delegate_
, OnSerializedPacket(_
)).WillOnce(
604 DoAll(SaveArg
<0>(&packet_
), Return(true)));
605 EXPECT_CALL(delegate_
, OnSerializedPacket(_
)).WillOnce(
606 DoAll(SaveArg
<0>(&packet2_
), Return(true)));
609 // Send enough data to exceed one packet
610 size_t data_len
= kDefaultMaxPacketSize
+ 100;
611 QuicConsumedData consumed
=
612 generator_
.ConsumeData(3, CreateData(data_len
), 0, true, NULL
);
613 EXPECT_EQ(data_len
, consumed
.bytes_consumed
);
614 EXPECT_TRUE(consumed
.fin_consumed
);
615 generator_
.AddControlFrame(QuicFrame(CreateGoAwayFrame()));
617 generator_
.FinishBatchOperations();
618 EXPECT_FALSE(generator_
.HasQueuedFrames());
620 // The first packet should have the queued data and part of the stream data.
621 PacketContents contents
;
622 contents
.num_ack_frames
= 1;
623 contents
.num_feedback_frames
= 1;
624 contents
.num_rst_stream_frames
= 1;
625 contents
.num_stream_frames
= 1;
626 CheckPacketContains(contents
, packet_
);
628 // The second should have the remainder of the stream data.
629 PacketContents contents2
;
630 contents2
.num_goaway_frames
= 1;
631 contents2
.num_stream_frames
= 1;
632 CheckPacketContains(contents2
, packet2_
);