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_flags.h"
14 #include "net/quic/quic_utils.h"
15 #include "net/quic/test_tools/quic_packet_creator_peer.h"
16 #include "net/quic/test_tools/quic_packet_generator_peer.h"
17 #include "net/quic/test_tools/quic_test_utils.h"
18 #include "net/quic/test_tools/simple_quic_framer.h"
19 #include "net/test/gtest_util.h"
20 #include "testing/gmock/include/gmock/gmock.h"
21 #include "testing/gtest/include/gtest/gtest.h"
23 using base::StringPiece
;
25 using testing::InSequence
;
26 using testing::Return
;
27 using testing::SaveArg
;
28 using testing::StrictMock
;
35 const int64 kMinFecTimeoutMs
= 5u;
37 class MockDelegate
: public QuicPacketGenerator::DelegateInterface
{
40 ~MockDelegate() override
{}
42 MOCK_METHOD3(ShouldGeneratePacket
,
43 bool(TransmissionType transmission_type
,
44 HasRetransmittableData retransmittable
,
45 IsHandshake handshake
));
46 MOCK_METHOD1(PopulateAckFrame
, void(QuicAckFrame
*));
47 MOCK_METHOD1(PopulateStopWaitingFrame
, void(QuicStopWaitingFrame
*));
48 MOCK_METHOD1(OnSerializedPacket
, void(const SerializedPacket
& packet
));
49 MOCK_METHOD2(CloseConnection
, void(QuicErrorCode
, bool));
51 void SetCanWriteAnything() {
52 EXPECT_CALL(*this, ShouldGeneratePacket(NOT_RETRANSMISSION
, _
, _
))
53 .WillRepeatedly(Return(true));
54 EXPECT_CALL(*this, ShouldGeneratePacket(NOT_RETRANSMISSION
,
55 NO_RETRANSMITTABLE_DATA
, _
))
56 .WillRepeatedly(Return(true));
59 void SetCanNotWrite() {
60 EXPECT_CALL(*this, ShouldGeneratePacket(NOT_RETRANSMISSION
, _
, _
))
61 .WillRepeatedly(Return(false));
62 EXPECT_CALL(*this, ShouldGeneratePacket(NOT_RETRANSMISSION
,
63 NO_RETRANSMITTABLE_DATA
, _
))
64 .WillRepeatedly(Return(false));
67 // Use this when only ack frames should be allowed to be written.
68 void SetCanWriteOnlyNonRetransmittable() {
69 EXPECT_CALL(*this, ShouldGeneratePacket(NOT_RETRANSMISSION
, _
, _
))
70 .WillRepeatedly(Return(false));
71 EXPECT_CALL(*this, ShouldGeneratePacket(NOT_RETRANSMISSION
,
72 NO_RETRANSMITTABLE_DATA
, _
))
73 .WillRepeatedly(Return(true));
77 DISALLOW_COPY_AND_ASSIGN(MockDelegate
);
80 // Simple struct for describing the contents of a packet.
81 // Useful in conjunction with a SimpleQuicFrame for validating that a packet
82 // contains the expected frames.
83 struct PacketContents
{
86 num_connection_close_frames(0),
88 num_rst_stream_frames(0),
89 num_stop_waiting_frames(0),
94 size_t num_ack_frames
;
95 size_t num_connection_close_frames
;
96 size_t num_goaway_frames
;
97 size_t num_rst_stream_frames
;
98 size_t num_stop_waiting_frames
;
99 size_t num_stream_frames
;
101 QuicFecGroupNumber fec_group
;
106 class QuicPacketGeneratorTest
: public ::testing::Test
{
108 QuicPacketGeneratorTest()
109 : framer_(QuicSupportedVersions(), QuicTime::Zero(), false),
110 generator_(42, &framer_
, &random_
, &delegate_
),
111 creator_(QuicPacketGeneratorPeer::GetPacketCreator(&generator_
)),
112 packet_(0, PACKET_1BYTE_SEQUENCE_NUMBER
, nullptr, 0, nullptr),
113 packet2_(0, PACKET_1BYTE_SEQUENCE_NUMBER
, nullptr, 0, nullptr),
114 packet3_(0, PACKET_1BYTE_SEQUENCE_NUMBER
, nullptr, 0, nullptr),
115 packet4_(0, PACKET_1BYTE_SEQUENCE_NUMBER
, nullptr, 0, nullptr),
116 packet5_(0, PACKET_1BYTE_SEQUENCE_NUMBER
, nullptr, 0, nullptr),
117 packet6_(0, PACKET_1BYTE_SEQUENCE_NUMBER
, nullptr, 0, nullptr),
118 packet7_(0, PACKET_1BYTE_SEQUENCE_NUMBER
, nullptr, 0, nullptr),
119 packet8_(0, PACKET_1BYTE_SEQUENCE_NUMBER
, nullptr, 0, nullptr) {}
121 ~QuicPacketGeneratorTest() override
{
122 delete packet_
.packet
;
123 delete packet_
.retransmittable_frames
;
124 delete packet2_
.packet
;
125 delete packet2_
.retransmittable_frames
;
126 delete packet3_
.packet
;
127 delete packet3_
.retransmittable_frames
;
128 delete packet4_
.packet
;
129 delete packet4_
.retransmittable_frames
;
130 delete packet5_
.packet
;
131 delete packet5_
.retransmittable_frames
;
132 delete packet6_
.packet
;
133 delete packet6_
.retransmittable_frames
;
134 delete packet7_
.packet
;
135 delete packet7_
.retransmittable_frames
;
136 delete packet8_
.packet
;
137 delete packet8_
.retransmittable_frames
;
140 QuicRstStreamFrame
* CreateRstStreamFrame() {
141 return new QuicRstStreamFrame(1, QUIC_STREAM_NO_ERROR
, 0);
144 QuicGoAwayFrame
* CreateGoAwayFrame() {
145 return new QuicGoAwayFrame(QUIC_NO_ERROR
, 1, string());
148 void CheckPacketContains(const PacketContents
& contents
,
149 const SerializedPacket
& packet
) {
150 size_t num_retransmittable_frames
= contents
.num_connection_close_frames
+
151 contents
.num_goaway_frames
+ contents
.num_rst_stream_frames
+
152 contents
.num_stream_frames
;
153 size_t num_frames
= contents
.num_ack_frames
+
154 contents
.num_stop_waiting_frames
+
155 num_retransmittable_frames
;
157 if (num_retransmittable_frames
== 0) {
158 ASSERT_TRUE(packet
.retransmittable_frames
== nullptr);
160 ASSERT_TRUE(packet
.retransmittable_frames
!= nullptr);
161 EXPECT_EQ(num_retransmittable_frames
,
162 packet
.retransmittable_frames
->frames().size());
165 ASSERT_TRUE(packet
.packet
!= nullptr);
166 ASSERT_TRUE(simple_framer_
.ProcessPacket(*packet
.packet
));
167 EXPECT_EQ(num_frames
, simple_framer_
.num_frames());
168 EXPECT_EQ(contents
.num_ack_frames
, simple_framer_
.ack_frames().size());
169 EXPECT_EQ(contents
.num_connection_close_frames
,
170 simple_framer_
.connection_close_frames().size());
171 EXPECT_EQ(contents
.num_goaway_frames
,
172 simple_framer_
.goaway_frames().size());
173 EXPECT_EQ(contents
.num_rst_stream_frames
,
174 simple_framer_
.rst_stream_frames().size());
175 EXPECT_EQ(contents
.num_stream_frames
,
176 simple_framer_
.stream_frames().size());
177 EXPECT_EQ(contents
.num_stop_waiting_frames
,
178 simple_framer_
.stop_waiting_frames().size());
179 EXPECT_EQ(contents
.fec_group
, simple_framer_
.header().fec_group
);
182 void CheckPacketHasSingleStreamFrame(const SerializedPacket
& packet
) {
183 ASSERT_TRUE(packet
.retransmittable_frames
!= nullptr);
184 EXPECT_EQ(1u, packet
.retransmittable_frames
->frames().size());
185 ASSERT_TRUE(packet
.packet
!= nullptr);
186 ASSERT_TRUE(simple_framer_
.ProcessPacket(*packet
.packet
));
187 EXPECT_EQ(1u, simple_framer_
.num_frames());
188 EXPECT_EQ(1u, simple_framer_
.stream_frames().size());
191 void CheckPacketIsFec(const SerializedPacket
& packet
,
192 QuicPacketSequenceNumber fec_group
) {
193 ASSERT_TRUE(packet
.retransmittable_frames
== nullptr);
194 ASSERT_TRUE(packet
.packet
!= nullptr);
195 ASSERT_TRUE(simple_framer_
.ProcessPacket(*packet
.packet
));
196 EXPECT_TRUE(simple_framer_
.header().fec_flag
);
197 EXPECT_EQ(fec_group
, simple_framer_
.fec_data().fec_group
);
200 IOVector
CreateData(size_t len
) {
201 data_array_
.reset(new char[len
]);
202 memset(data_array_
.get(), '?', len
);
204 data
.Append(data_array_
.get(), len
);
210 StrictMock
<MockDelegate
> delegate_
;
211 QuicPacketGenerator generator_
;
212 QuicPacketCreator
* creator_
;
213 SimpleQuicFramer simple_framer_
;
214 SerializedPacket packet_
;
215 SerializedPacket packet2_
;
216 SerializedPacket packet3_
;
217 SerializedPacket packet4_
;
218 SerializedPacket packet5_
;
219 SerializedPacket packet6_
;
220 SerializedPacket packet7_
;
221 SerializedPacket packet8_
;
224 scoped_ptr
<char[]> data_array_
;
227 class MockDebugDelegate
: public QuicPacketGenerator::DebugDelegate
{
229 MOCK_METHOD1(OnFrameAddedToPacket
,
230 void(const QuicFrame
&));
233 TEST_F(QuicPacketGeneratorTest
, ShouldSendAck_NotWritable
) {
234 delegate_
.SetCanNotWrite();
236 generator_
.SetShouldSendAck(false);
237 EXPECT_TRUE(generator_
.HasQueuedFrames());
240 TEST_F(QuicPacketGeneratorTest
, ShouldSendAck_WritableAndShouldNotFlush
) {
241 StrictMock
<MockDebugDelegate
> debug_delegate
;
243 generator_
.set_debug_delegate(&debug_delegate
);
244 delegate_
.SetCanWriteOnlyNonRetransmittable();
245 generator_
.StartBatchOperations();
247 EXPECT_CALL(delegate_
, PopulateAckFrame(_
));
248 EXPECT_CALL(debug_delegate
, OnFrameAddedToPacket(_
)).Times(1);
250 generator_
.SetShouldSendAck(false);
251 EXPECT_TRUE(generator_
.HasQueuedFrames());
254 TEST_F(QuicPacketGeneratorTest
, ShouldSendAck_WritableAndShouldFlush
) {
255 delegate_
.SetCanWriteOnlyNonRetransmittable();
257 EXPECT_CALL(delegate_
, PopulateAckFrame(_
));
258 EXPECT_CALL(delegate_
, OnSerializedPacket(_
)).WillOnce(SaveArg
<0>(&packet_
));
260 generator_
.SetShouldSendAck(false);
261 EXPECT_FALSE(generator_
.HasQueuedFrames());
263 PacketContents contents
;
264 contents
.num_ack_frames
= 1;
265 CheckPacketContains(contents
, packet_
);
268 TEST_F(QuicPacketGeneratorTest
, ShouldSendAck_MultipleCalls
) {
269 // Make sure that calling SetShouldSendAck multiple times does not result in a
270 // crash. Previously this would result in multiple QuicFrames queued in the
271 // packet generator, with all but the last with internal pointers to freed
273 delegate_
.SetCanWriteAnything();
275 // Only one AckFrame should be created.
276 EXPECT_CALL(delegate_
, PopulateAckFrame(_
)).Times(1);
277 EXPECT_CALL(delegate_
, OnSerializedPacket(_
))
279 .WillOnce(SaveArg
<0>(&packet_
));
281 generator_
.StartBatchOperations();
282 generator_
.SetShouldSendAck(false);
283 generator_
.SetShouldSendAck(false);
284 generator_
.FinishBatchOperations();
287 TEST_F(QuicPacketGeneratorTest
, AddControlFrame_NotWritable
) {
288 delegate_
.SetCanNotWrite();
290 generator_
.AddControlFrame(QuicFrame(CreateRstStreamFrame()));
291 EXPECT_TRUE(generator_
.HasQueuedFrames());
294 TEST_F(QuicPacketGeneratorTest
, AddControlFrame_OnlyAckWritable
) {
295 delegate_
.SetCanWriteOnlyNonRetransmittable();
297 generator_
.AddControlFrame(QuicFrame(CreateRstStreamFrame()));
298 EXPECT_TRUE(generator_
.HasQueuedFrames());
301 TEST_F(QuicPacketGeneratorTest
, AddControlFrame_WritableAndShouldNotFlush
) {
302 delegate_
.SetCanWriteAnything();
303 generator_
.StartBatchOperations();
305 generator_
.AddControlFrame(QuicFrame(CreateRstStreamFrame()));
306 EXPECT_TRUE(generator_
.HasQueuedFrames());
309 TEST_F(QuicPacketGeneratorTest
, AddControlFrame_NotWritableBatchThenFlush
) {
310 delegate_
.SetCanNotWrite();
311 generator_
.StartBatchOperations();
313 generator_
.AddControlFrame(QuicFrame(CreateRstStreamFrame()));
314 EXPECT_TRUE(generator_
.HasQueuedFrames());
315 generator_
.FinishBatchOperations();
316 EXPECT_TRUE(generator_
.HasQueuedFrames());
318 EXPECT_CALL(delegate_
, OnSerializedPacket(_
)).WillOnce(SaveArg
<0>(&packet_
));
319 generator_
.FlushAllQueuedFrames();
320 EXPECT_FALSE(generator_
.HasQueuedFrames());
322 PacketContents contents
;
323 contents
.num_rst_stream_frames
= 1;
324 CheckPacketContains(contents
, packet_
);
327 TEST_F(QuicPacketGeneratorTest
, AddControlFrame_WritableAndShouldFlush
) {
328 delegate_
.SetCanWriteAnything();
330 EXPECT_CALL(delegate_
, OnSerializedPacket(_
)).WillOnce(SaveArg
<0>(&packet_
));
332 generator_
.AddControlFrame(QuicFrame(CreateRstStreamFrame()));
333 EXPECT_FALSE(generator_
.HasQueuedFrames());
335 PacketContents contents
;
336 contents
.num_rst_stream_frames
= 1;
337 CheckPacketContains(contents
, packet_
);
340 TEST_F(QuicPacketGeneratorTest
, ConsumeData_NotWritable
) {
341 delegate_
.SetCanNotWrite();
343 QuicConsumedData consumed
= generator_
.ConsumeData(
344 kHeadersStreamId
, MakeIOVector("foo"), 2, true, MAY_FEC_PROTECT
, nullptr);
345 EXPECT_EQ(0u, consumed
.bytes_consumed
);
346 EXPECT_FALSE(consumed
.fin_consumed
);
347 EXPECT_FALSE(generator_
.HasQueuedFrames());
350 TEST_F(QuicPacketGeneratorTest
, ConsumeData_WritableAndShouldNotFlush
) {
351 delegate_
.SetCanWriteAnything();
352 generator_
.StartBatchOperations();
354 QuicConsumedData consumed
= generator_
.ConsumeData(
355 kHeadersStreamId
, MakeIOVector("foo"), 2, true, MAY_FEC_PROTECT
, nullptr);
356 EXPECT_EQ(3u, consumed
.bytes_consumed
);
357 EXPECT_TRUE(consumed
.fin_consumed
);
358 EXPECT_TRUE(generator_
.HasQueuedFrames());
361 TEST_F(QuicPacketGeneratorTest
, ConsumeData_WritableAndShouldFlush
) {
362 delegate_
.SetCanWriteAnything();
364 EXPECT_CALL(delegate_
, OnSerializedPacket(_
)).WillOnce(SaveArg
<0>(&packet_
));
365 QuicConsumedData consumed
= generator_
.ConsumeData(
366 kHeadersStreamId
, MakeIOVector("foo"), 2, true, MAY_FEC_PROTECT
, nullptr);
367 EXPECT_EQ(3u, consumed
.bytes_consumed
);
368 EXPECT_TRUE(consumed
.fin_consumed
);
369 EXPECT_FALSE(generator_
.HasQueuedFrames());
371 PacketContents contents
;
372 contents
.num_stream_frames
= 1;
373 CheckPacketContains(contents
, packet_
);
376 TEST_F(QuicPacketGeneratorTest
, ConsumeData_EmptyData
) {
377 EXPECT_DFATAL(generator_
.ConsumeData(kHeadersStreamId
, MakeIOVector(""), 0,
378 false, MAY_FEC_PROTECT
, nullptr),
379 "Attempt to consume empty data without FIN.");
382 TEST_F(QuicPacketGeneratorTest
,
383 ConsumeDataMultipleTimes_WritableAndShouldNotFlush
) {
384 delegate_
.SetCanWriteAnything();
385 generator_
.StartBatchOperations();
387 generator_
.ConsumeData(kHeadersStreamId
, MakeIOVector("foo"), 2, true,
388 MAY_FEC_PROTECT
, nullptr);
389 QuicConsumedData consumed
= generator_
.ConsumeData(
390 3, MakeIOVector("quux"), 7, false, MAY_FEC_PROTECT
, nullptr);
391 EXPECT_EQ(4u, consumed
.bytes_consumed
);
392 EXPECT_FALSE(consumed
.fin_consumed
);
393 EXPECT_TRUE(generator_
.HasQueuedFrames());
396 TEST_F(QuicPacketGeneratorTest
, ConsumeData_BatchOperations
) {
397 delegate_
.SetCanWriteAnything();
398 generator_
.StartBatchOperations();
400 generator_
.ConsumeData(kHeadersStreamId
, MakeIOVector("foo"), 2, true,
401 MAY_FEC_PROTECT
, nullptr);
402 QuicConsumedData consumed
= generator_
.ConsumeData(
403 3, MakeIOVector("quux"), 7, false, MAY_FEC_PROTECT
, nullptr);
404 EXPECT_EQ(4u, consumed
.bytes_consumed
);
405 EXPECT_FALSE(consumed
.fin_consumed
);
406 EXPECT_TRUE(generator_
.HasQueuedFrames());
408 // Now both frames will be flushed out.
409 EXPECT_CALL(delegate_
, OnSerializedPacket(_
)).WillOnce(SaveArg
<0>(&packet_
));
410 generator_
.FinishBatchOperations();
411 EXPECT_FALSE(generator_
.HasQueuedFrames());
413 PacketContents contents
;
414 contents
.num_stream_frames
= 2;
415 CheckPacketContains(contents
, packet_
);
418 TEST_F(QuicPacketGeneratorTest
, ConsumeDataSendsFecOnMaxGroupSize
) {
419 delegate_
.SetCanWriteAnything();
421 // Send FEC every two packets.
422 creator_
->set_max_packets_per_fec_group(2);
426 EXPECT_CALL(delegate_
, OnSerializedPacket(_
)).WillOnce(
427 SaveArg
<0>(&packet_
));
428 EXPECT_CALL(delegate_
, OnSerializedPacket(_
)).WillOnce(
429 SaveArg
<0>(&packet2_
));
430 EXPECT_CALL(delegate_
, OnSerializedPacket(_
)).WillOnce(
431 SaveArg
<0>(&packet3_
));
432 EXPECT_CALL(delegate_
, OnSerializedPacket(_
)).WillOnce(
433 SaveArg
<0>(&packet4_
));
436 // Send enough data to create 3 packets: two full and one partial. Send with
437 // MUST_FEC_PROTECT flag.
438 size_t data_len
= 2 * kDefaultMaxPacketSize
+ 100;
439 QuicConsumedData consumed
= generator_
.ConsumeData(
440 3, CreateData(data_len
), 0, true, MUST_FEC_PROTECT
, nullptr);
441 EXPECT_EQ(data_len
, consumed
.bytes_consumed
);
442 EXPECT_TRUE(consumed
.fin_consumed
);
443 EXPECT_FALSE(generator_
.HasQueuedFrames());
445 CheckPacketHasSingleStreamFrame(packet_
);
446 CheckPacketHasSingleStreamFrame(packet2_
);
447 CheckPacketIsFec(packet3_
, 1);
448 CheckPacketHasSingleStreamFrame(packet4_
);
449 EXPECT_TRUE(creator_
->IsFecProtected());
451 // The FEC packet under construction will be sent when one more packet is sent
452 // (since FEC group size is 2), or when OnFecTimeout is called. Send more data
453 // with MAY_FEC_PROTECT. This packet should also be protected, and FEC packet
454 // is sent since FEC group size is reached.
457 EXPECT_CALL(delegate_
, OnSerializedPacket(_
))
458 .WillOnce(SaveArg
<0>(&packet5_
));
459 EXPECT_CALL(delegate_
, OnSerializedPacket(_
))
460 .WillOnce(SaveArg
<0>(&packet6_
));
462 consumed
= generator_
.ConsumeData(5, CreateData(1u), 0, true, MAY_FEC_PROTECT
,
464 EXPECT_EQ(1u, consumed
.bytes_consumed
);
465 CheckPacketHasSingleStreamFrame(packet5_
);
466 CheckPacketIsFec(packet6_
, 4);
467 EXPECT_FALSE(creator_
->IsFecProtected());
470 TEST_F(QuicPacketGeneratorTest
, ConsumeDataSendsFecOnTimeout
) {
471 delegate_
.SetCanWriteAnything();
472 creator_
->set_max_packets_per_fec_group(1000);
474 // Send data with MUST_FEC_PROTECT flag. No FEC packet is emitted, but the
475 // creator FEC protects all data.
476 EXPECT_CALL(delegate_
, OnSerializedPacket(_
)).WillOnce(SaveArg
<0>(&packet_
));
477 QuicConsumedData consumed
= generator_
.ConsumeData(3, CreateData(1u), 0, true,
478 MUST_FEC_PROTECT
, nullptr);
479 EXPECT_EQ(1u, consumed
.bytes_consumed
);
480 EXPECT_TRUE(consumed
.fin_consumed
);
481 CheckPacketHasSingleStreamFrame(packet_
);
482 EXPECT_TRUE(creator_
->IsFecProtected());
484 // Send more data with MAY_FEC_PROTECT. This packet should also be protected,
485 // and FEC packet is not yet sent.
486 EXPECT_CALL(delegate_
, OnSerializedPacket(_
)).WillOnce(SaveArg
<0>(&packet2_
));
487 consumed
= generator_
.ConsumeData(5, CreateData(1u), 0, true, MAY_FEC_PROTECT
,
489 EXPECT_EQ(1u, consumed
.bytes_consumed
);
490 CheckPacketHasSingleStreamFrame(packet2_
);
491 EXPECT_TRUE(creator_
->IsFecProtected());
493 // Calling OnFecTimeout should cause the FEC packet to be emitted.
494 EXPECT_CALL(delegate_
, OnSerializedPacket(_
)).WillOnce(SaveArg
<0>(&packet3_
));
495 generator_
.OnFecTimeout();
496 CheckPacketIsFec(packet3_
, 1);
497 EXPECT_FALSE(creator_
->IsFecProtected());
499 // Subsequent data is protected under the next FEC group. Send enough data to
500 // create 2 more packets: one full and one partial.
503 EXPECT_CALL(delegate_
, OnSerializedPacket(_
))
504 .WillOnce(SaveArg
<0>(&packet4_
));
505 EXPECT_CALL(delegate_
, OnSerializedPacket(_
))
506 .WillOnce(SaveArg
<0>(&packet5_
));
508 size_t data_len
= kDefaultMaxPacketSize
+ 1;
509 consumed
= generator_
.ConsumeData(7, CreateData(data_len
), 0, true,
510 MUST_FEC_PROTECT
, nullptr);
511 EXPECT_EQ(data_len
, consumed
.bytes_consumed
);
512 EXPECT_TRUE(consumed
.fin_consumed
);
513 CheckPacketHasSingleStreamFrame(packet4_
);
514 CheckPacketHasSingleStreamFrame(packet5_
);
515 EXPECT_TRUE(creator_
->IsFecProtected());
517 // Calling OnFecTimeout should cause the FEC packet to be emitted.
518 EXPECT_CALL(delegate_
, OnSerializedPacket(_
)).WillOnce(SaveArg
<0>(&packet6_
));
519 generator_
.OnFecTimeout();
520 CheckPacketIsFec(packet6_
, 4);
521 EXPECT_FALSE(creator_
->IsFecProtected());
524 TEST_F(QuicPacketGeneratorTest
, GetFecTimeoutFiniteOnlyOnFirstPacketInGroup
) {
525 delegate_
.SetCanWriteAnything();
526 creator_
->set_max_packets_per_fec_group(6);
528 // Send enough data to create 2 packets: one full and one partial. Send with
529 // MUST_FEC_PROTECT flag. No FEC packet is emitted yet, but the creator FEC
530 // protects all data.
533 EXPECT_CALL(delegate_
, OnSerializedPacket(_
)).WillOnce(
534 SaveArg
<0>(&packet_
));
535 EXPECT_CALL(delegate_
, OnSerializedPacket(_
)).WillOnce(
536 SaveArg
<0>(&packet2_
));
538 size_t data_len
= 1 * kDefaultMaxPacketSize
+ 100;
539 QuicConsumedData consumed
= generator_
.ConsumeData(
540 3, CreateData(data_len
), 0, true, MUST_FEC_PROTECT
, nullptr);
541 EXPECT_EQ(data_len
, consumed
.bytes_consumed
);
542 EXPECT_TRUE(consumed
.fin_consumed
);
543 EXPECT_FALSE(generator_
.HasQueuedFrames());
544 CheckPacketHasSingleStreamFrame(packet_
);
545 CheckPacketHasSingleStreamFrame(packet2_
);
546 EXPECT_TRUE(creator_
->IsFecProtected());
548 // GetFecTimeout returns finite timeout only for first packet in group.
549 EXPECT_EQ(QuicTime::Delta::FromMilliseconds(kMinFecTimeoutMs
),
550 generator_
.GetFecTimeout(/*sequence_number=*/1u));
551 EXPECT_EQ(QuicTime::Delta::Infinite(),
552 generator_
.GetFecTimeout(/*sequence_number=*/2u));
554 // Send more data with MAY_FEC_PROTECT. This packet should also be protected,
555 // and FEC packet is not yet sent.
556 EXPECT_CALL(delegate_
, OnSerializedPacket(_
)).WillOnce(SaveArg
<0>(&packet3_
));
557 consumed
= generator_
.ConsumeData(5, CreateData(1u), 0, true, MAY_FEC_PROTECT
,
559 CheckPacketHasSingleStreamFrame(packet3_
);
560 EXPECT_TRUE(creator_
->IsFecProtected());
562 // GetFecTimeout returns finite timeout only for first packet in group.
563 EXPECT_EQ(QuicTime::Delta::Infinite(),
564 generator_
.GetFecTimeout(/*sequence_number=*/3u));
566 // Calling OnFecTimeout should cause the FEC packet to be emitted.
567 EXPECT_CALL(delegate_
, OnSerializedPacket(_
)).WillOnce(SaveArg
<0>(&packet4_
));
568 generator_
.OnFecTimeout();
569 CheckPacketIsFec(packet4_
, /*fec_group=*/1u);
570 EXPECT_FALSE(creator_
->IsFecProtected());
572 // Subsequent data is protected under the next FEC group. Send enough data to
573 // create 2 more packets: one full and one partial.
576 EXPECT_CALL(delegate_
, OnSerializedPacket(_
))
577 .WillOnce(SaveArg
<0>(&packet5_
));
578 EXPECT_CALL(delegate_
, OnSerializedPacket(_
))
579 .WillOnce(SaveArg
<0>(&packet6_
));
581 data_len
= kDefaultMaxPacketSize
+ 1u;
582 consumed
= generator_
.ConsumeData(7, CreateData(data_len
), 0, true,
583 MUST_FEC_PROTECT
, nullptr);
584 EXPECT_EQ(data_len
, consumed
.bytes_consumed
);
585 EXPECT_TRUE(consumed
.fin_consumed
);
586 CheckPacketHasSingleStreamFrame(packet5_
);
587 CheckPacketHasSingleStreamFrame(packet6_
);
588 EXPECT_TRUE(creator_
->IsFecProtected());
590 // GetFecTimeout returns finite timeout for first packet in the new group.
591 EXPECT_EQ(QuicTime::Delta::FromMilliseconds(kMinFecTimeoutMs
),
592 generator_
.GetFecTimeout(/*sequence_number=*/5u));
593 EXPECT_EQ(QuicTime::Delta::Infinite(),
594 generator_
.GetFecTimeout(/*sequence_number=*/6u));
596 // Calling OnFecTimeout should cause the FEC packet to be emitted.
597 EXPECT_CALL(delegate_
, OnSerializedPacket(_
)).WillOnce(SaveArg
<0>(&packet7_
));
598 generator_
.OnFecTimeout();
599 CheckPacketIsFec(packet7_
, /*fec_group=*/5u);
600 EXPECT_FALSE(creator_
->IsFecProtected());
602 // Send more data with MAY_FEC_PROTECT. No FEC protection, so GetFecTimeout
604 EXPECT_CALL(delegate_
, OnSerializedPacket(_
)).WillOnce(SaveArg
<0>(&packet8_
));
605 consumed
= generator_
.ConsumeData(9, CreateData(1u), 0, true, MAY_FEC_PROTECT
,
607 CheckPacketHasSingleStreamFrame(packet8_
);
608 EXPECT_FALSE(creator_
->IsFecProtected());
609 EXPECT_EQ(QuicTime::Delta::Infinite(),
610 generator_
.GetFecTimeout(/*sequence_number=*/8u));
613 TEST_F(QuicPacketGeneratorTest
, ConsumeData_FramesPreviouslyQueued
) {
614 // Set the packet size be enough for two stream frames with 0 stream offset,
615 // but not enough for a stream frame of 0 offset and one with non-zero offset.
617 NullEncrypter().GetCiphertextSize(0) +
619 creator_
->connection_id_length(), true,
620 QuicPacketCreatorPeer::NextSequenceNumberLength(creator_
),
622 // Add an extra 3 bytes for the payload and 1 byte so BytesFree is larger
623 // than the GetMinStreamFrameSize.
624 QuicFramer::GetMinStreamFrameSize(1, 0, false, NOT_IN_FEC_GROUP
) + 3 +
625 QuicFramer::GetMinStreamFrameSize(1, 0, true, NOT_IN_FEC_GROUP
) + 1;
626 creator_
->set_max_packet_length(length
);
627 delegate_
.SetCanWriteAnything();
630 EXPECT_CALL(delegate_
, OnSerializedPacket(_
)).WillOnce(
631 SaveArg
<0>(&packet_
));
632 EXPECT_CALL(delegate_
, OnSerializedPacket(_
)).WillOnce(
633 SaveArg
<0>(&packet2_
));
635 generator_
.StartBatchOperations();
636 // Queue enough data to prevent a stream frame with a non-zero offset from
638 QuicConsumedData consumed
=
639 generator_
.ConsumeData(kHeadersStreamId
, MakeIOVector("foo"), 0, false,
640 MAY_FEC_PROTECT
, nullptr);
641 EXPECT_EQ(3u, consumed
.bytes_consumed
);
642 EXPECT_FALSE(consumed
.fin_consumed
);
643 EXPECT_TRUE(generator_
.HasQueuedFrames());
645 // This frame will not fit with the existing frame, causing the queued frame
646 // to be serialized, and it will not fit with another frame like it, so it is
647 // serialized by itself.
648 consumed
= generator_
.ConsumeData(kHeadersStreamId
, MakeIOVector("bar"), 3,
649 true, MAY_FEC_PROTECT
, nullptr);
650 EXPECT_EQ(3u, consumed
.bytes_consumed
);
651 EXPECT_TRUE(consumed
.fin_consumed
);
652 EXPECT_FALSE(generator_
.HasQueuedFrames());
654 PacketContents contents
;
655 contents
.num_stream_frames
= 1;
656 CheckPacketContains(contents
, packet_
);
657 CheckPacketContains(contents
, packet2_
);
660 TEST_F(QuicPacketGeneratorTest
, NoFecPacketSentWhenBatchEnds
) {
661 delegate_
.SetCanWriteAnything();
662 creator_
->set_max_packets_per_fec_group(6);
664 generator_
.StartBatchOperations();
666 generator_
.ConsumeData(3, MakeIOVector("foo"), 2, true, MUST_FEC_PROTECT
,
668 QuicConsumedData consumed
= generator_
.ConsumeData(
669 5, MakeIOVector("quux"), 7, false, MUST_FEC_PROTECT
, nullptr);
670 EXPECT_EQ(4u, consumed
.bytes_consumed
);
671 EXPECT_FALSE(consumed
.fin_consumed
);
672 EXPECT_TRUE(generator_
.HasQueuedFrames());
674 // Now both frames will be flushed out, but FEC packet is not yet sent.
675 EXPECT_CALL(delegate_
, OnSerializedPacket(_
)).WillOnce(SaveArg
<0>(&packet_
));
676 generator_
.FinishBatchOperations();
677 EXPECT_FALSE(generator_
.HasQueuedFrames());
679 PacketContents contents
;
680 contents
.num_stream_frames
= 2u;
681 contents
.fec_group
= 1u;
682 CheckPacketContains(contents
, packet_
);
684 // Forcing FEC timeout causes FEC packet to be emitted.
685 EXPECT_CALL(delegate_
, OnSerializedPacket(_
)).WillOnce(SaveArg
<0>(&packet2_
));
686 generator_
.OnFecTimeout();
687 CheckPacketIsFec(packet2_
, /*fec_group=*/1u);
690 TEST_F(QuicPacketGeneratorTest
, FecTimeoutOnRttChange
) {
691 EXPECT_EQ(QuicTime::Delta::Zero(),
692 QuicPacketGeneratorPeer::GetFecTimeout(&generator_
));
693 generator_
.OnRttChange(QuicTime::Delta::FromMilliseconds(300));
694 EXPECT_EQ(QuicTime::Delta::FromMilliseconds(150),
695 QuicPacketGeneratorPeer::GetFecTimeout(&generator_
));
698 TEST_F(QuicPacketGeneratorTest
, FecGroupSizeOnCongestionWindowChange
) {
699 delegate_
.SetCanWriteAnything();
700 creator_
->set_max_packets_per_fec_group(50);
701 EXPECT_EQ(50u, creator_
->max_packets_per_fec_group());
702 EXPECT_FALSE(creator_
->IsFecGroupOpen());
705 generator_
.OnCongestionWindowChange(7);
706 EXPECT_EQ(3u, creator_
->max_packets_per_fec_group());
708 // On increased cwnd.
709 generator_
.OnCongestionWindowChange(100);
710 EXPECT_EQ(50u, creator_
->max_packets_per_fec_group());
712 // On collapsed cwnd.
713 generator_
.OnCongestionWindowChange(1);
714 EXPECT_EQ(2u, creator_
->max_packets_per_fec_group());
717 TEST_F(QuicPacketGeneratorTest
, FecGroupSizeChangeWithOpenGroup
) {
718 delegate_
.SetCanWriteAnything();
719 generator_
.StartBatchOperations();
720 creator_
->set_max_packets_per_fec_group(50);
721 EXPECT_EQ(50u, creator_
->max_packets_per_fec_group());
722 EXPECT_FALSE(creator_
->IsFecGroupOpen());
724 // Send enough data to create 4 packets with MUST_FEC_PROTECT flag. 3 packets
725 // are sent, one is queued in the creator.
728 EXPECT_CALL(delegate_
, OnSerializedPacket(_
)).WillOnce(
729 SaveArg
<0>(&packet_
));
730 EXPECT_CALL(delegate_
, OnSerializedPacket(_
)).WillOnce(
731 SaveArg
<0>(&packet2_
));
732 EXPECT_CALL(delegate_
, OnSerializedPacket(_
)).WillOnce(
733 SaveArg
<0>(&packet3_
));
735 size_t data_len
= 3 * kDefaultMaxPacketSize
+ 1;
736 QuicConsumedData consumed
= generator_
.ConsumeData(
737 7, CreateData(data_len
), 0, true, MUST_FEC_PROTECT
, nullptr);
738 EXPECT_EQ(data_len
, consumed
.bytes_consumed
);
739 EXPECT_TRUE(creator_
->IsFecGroupOpen());
741 // Change FEC groupsize.
742 generator_
.OnCongestionWindowChange(2);
743 EXPECT_EQ(2u, creator_
->max_packets_per_fec_group());
745 // Send enough data to trigger one unprotected data packet, causing the FEC
746 // packet to also be sent.
749 EXPECT_CALL(delegate_
, OnSerializedPacket(_
)).WillOnce(
750 SaveArg
<0>(&packet4_
));
751 EXPECT_CALL(delegate_
, OnSerializedPacket(_
)).WillOnce(
752 SaveArg
<0>(&packet5_
));
754 consumed
= generator_
.ConsumeData(7, CreateData(kDefaultMaxPacketSize
), 0,
755 true, MAY_FEC_PROTECT
, nullptr);
756 EXPECT_EQ(kDefaultMaxPacketSize
, consumed
.bytes_consumed
);
757 // Verify that one FEC packet was sent.
758 CheckPacketIsFec(packet5_
, /*fec_group=*/1u);
759 EXPECT_FALSE(creator_
->IsFecGroupOpen());
760 EXPECT_FALSE(creator_
->IsFecProtected());
763 TEST_F(QuicPacketGeneratorTest
, SwitchFecOnOff
) {
764 delegate_
.SetCanWriteAnything();
765 creator_
->set_max_packets_per_fec_group(2);
766 EXPECT_FALSE(creator_
->IsFecProtected());
768 // Send one unprotected data packet.
769 EXPECT_CALL(delegate_
, OnSerializedPacket(_
)).WillOnce(
770 SaveArg
<0>(&packet_
));
771 QuicConsumedData consumed
= generator_
.ConsumeData(5, CreateData(1u), 0, true,
772 MAY_FEC_PROTECT
, nullptr);
773 EXPECT_EQ(1u, consumed
.bytes_consumed
);
774 EXPECT_FALSE(generator_
.HasQueuedFrames());
775 EXPECT_FALSE(creator_
->IsFecProtected());
776 // Verify that one data packet was sent.
777 PacketContents contents
;
778 contents
.num_stream_frames
= 1;
779 CheckPacketContains(contents
, packet_
);
783 EXPECT_CALL(delegate_
, OnSerializedPacket(_
)).WillOnce(
784 SaveArg
<0>(&packet2_
));
785 EXPECT_CALL(delegate_
, OnSerializedPacket(_
)).WillOnce(
786 SaveArg
<0>(&packet3_
));
787 EXPECT_CALL(delegate_
, OnSerializedPacket(_
)).WillOnce(
788 SaveArg
<0>(&packet4_
));
789 EXPECT_CALL(delegate_
, OnSerializedPacket(_
)).WillOnce(
790 SaveArg
<0>(&packet5_
));
792 // Send enough data to create 3 packets with MUST_FEC_PROTECT flag.
793 size_t data_len
= 2 * kDefaultMaxPacketSize
+ 100;
794 consumed
= generator_
.ConsumeData(7, CreateData(data_len
), 0, true,
795 MUST_FEC_PROTECT
, nullptr);
796 EXPECT_EQ(data_len
, consumed
.bytes_consumed
);
797 EXPECT_FALSE(generator_
.HasQueuedFrames());
799 // Verify that packets sent were 3 data and 1 FEC.
800 CheckPacketHasSingleStreamFrame(packet2_
);
801 CheckPacketHasSingleStreamFrame(packet3_
);
802 CheckPacketIsFec(packet4_
, /*fec_group=*/2u);
803 CheckPacketHasSingleStreamFrame(packet5_
);
805 // Calling OnFecTimeout should emit the pending FEC packet.
806 EXPECT_CALL(delegate_
, OnSerializedPacket(_
)).WillOnce(SaveArg
<0>(&packet6_
));
807 generator_
.OnFecTimeout();
808 CheckPacketIsFec(packet6_
, /*fec_group=*/5u);
810 // Send one unprotected data packet.
811 EXPECT_CALL(delegate_
, OnSerializedPacket(_
)).WillOnce(
812 SaveArg
<0>(&packet7_
));
813 consumed
= generator_
.ConsumeData(7, CreateData(1u), 0, true, MAY_FEC_PROTECT
,
815 EXPECT_EQ(1u, consumed
.bytes_consumed
);
816 EXPECT_FALSE(generator_
.HasQueuedFrames());
817 EXPECT_FALSE(creator_
->IsFecProtected());
818 // Verify that one unprotected data packet was sent.
819 CheckPacketContains(contents
, packet7_
);
822 TEST_F(QuicPacketGeneratorTest
, SwitchFecOnWithPendingFrameInCreator
) {
823 delegate_
.SetCanWriteAnything();
825 creator_
->set_max_packets_per_fec_group(2);
827 generator_
.StartBatchOperations();
828 // Queue enough data to prevent a stream frame with a non-zero offset from
830 QuicConsumedData consumed
= generator_
.ConsumeData(7, CreateData(1u), 0, true,
831 MAY_FEC_PROTECT
, nullptr);
832 EXPECT_EQ(1u, consumed
.bytes_consumed
);
833 EXPECT_TRUE(creator_
->HasPendingFrames());
835 // Queue protected data for sending. Should cause queued frames to be flushed.
836 EXPECT_CALL(delegate_
, OnSerializedPacket(_
)).WillOnce(
837 SaveArg
<0>(&packet_
));
838 EXPECT_FALSE(creator_
->IsFecProtected());
839 consumed
= generator_
.ConsumeData(7, CreateData(1u), 0, true,
840 MUST_FEC_PROTECT
, nullptr);
841 EXPECT_EQ(1u, consumed
.bytes_consumed
);
842 PacketContents contents
;
843 contents
.num_stream_frames
= 1;
844 // Transmitted packet was not FEC protected.
845 CheckPacketContains(contents
, packet_
);
846 EXPECT_TRUE(creator_
->IsFecProtected());
847 EXPECT_TRUE(creator_
->HasPendingFrames());
850 TEST_F(QuicPacketGeneratorTest
, SwitchFecOnWithPendingFramesInGenerator
) {
852 creator_
->set_max_packets_per_fec_group(2);
854 // Queue control frames in generator.
855 delegate_
.SetCanNotWrite();
856 generator_
.SetShouldSendAck(true);
857 delegate_
.SetCanWriteAnything();
858 generator_
.StartBatchOperations();
860 // Set up frames to write into the creator when control frames are written.
861 EXPECT_CALL(delegate_
, PopulateAckFrame(_
));
862 EXPECT_CALL(delegate_
, PopulateStopWaitingFrame(_
));
864 // Generator should have queued control frames, and creator should be empty.
865 EXPECT_TRUE(generator_
.HasQueuedFrames());
866 EXPECT_FALSE(creator_
->HasPendingFrames());
867 EXPECT_FALSE(creator_
->IsFecProtected());
869 // Queue protected data for sending. Should cause queued frames to be flushed.
870 EXPECT_CALL(delegate_
, OnSerializedPacket(_
)).WillOnce(
871 SaveArg
<0>(&packet_
));
872 QuicConsumedData consumed
= generator_
.ConsumeData(7, CreateData(1u), 0, true,
873 MUST_FEC_PROTECT
, nullptr);
874 EXPECT_EQ(1u, consumed
.bytes_consumed
);
875 PacketContents contents
;
876 contents
.num_ack_frames
= 1;
877 contents
.num_stop_waiting_frames
= 1;
878 CheckPacketContains(contents
, packet_
);
880 // FEC protection should be on in creator.
881 EXPECT_TRUE(creator_
->IsFecProtected());
884 TEST_F(QuicPacketGeneratorTest
, SwitchFecOnOffWithSubsequentFramesProtected
) {
885 delegate_
.SetCanWriteAnything();
888 creator_
->set_max_packets_per_fec_group(2);
889 EXPECT_FALSE(creator_
->IsFecProtected());
891 // Queue stream frame to be protected in creator.
892 generator_
.StartBatchOperations();
893 QuicConsumedData consumed
= generator_
.ConsumeData(5, CreateData(1u), 0, true,
894 MUST_FEC_PROTECT
, nullptr);
895 EXPECT_EQ(1u, consumed
.bytes_consumed
);
896 // Creator has a pending protected frame.
897 EXPECT_TRUE(creator_
->HasPendingFrames());
898 EXPECT_TRUE(creator_
->IsFecProtected());
900 // Add enough unprotected data to exceed size of current packet, so that
901 // current packet is sent. Both frames will be sent out in a single packet.
902 EXPECT_CALL(delegate_
, OnSerializedPacket(_
)).WillOnce(SaveArg
<0>(&packet_
));
903 size_t data_len
= kDefaultMaxPacketSize
;
904 consumed
= generator_
.ConsumeData(5, CreateData(data_len
), 0, true,
905 MAY_FEC_PROTECT
, nullptr);
906 EXPECT_EQ(data_len
, consumed
.bytes_consumed
);
907 PacketContents contents
;
908 contents
.num_stream_frames
= 2u;
909 contents
.fec_group
= 1u;
910 CheckPacketContains(contents
, packet_
);
911 // FEC protection should still be on in creator.
912 EXPECT_TRUE(creator_
->IsFecProtected());
915 TEST_F(QuicPacketGeneratorTest
, SwitchFecOnOffWithSubsequentPacketsProtected
) {
916 delegate_
.SetCanWriteAnything();
919 creator_
->set_max_packets_per_fec_group(2);
920 EXPECT_FALSE(creator_
->IsFecProtected());
922 // Send first packet, FEC protected.
923 EXPECT_CALL(delegate_
, OnSerializedPacket(_
)).WillOnce(SaveArg
<0>(&packet_
));
924 QuicConsumedData consumed
= generator_
.ConsumeData(5, CreateData(1u), 0, true,
925 MUST_FEC_PROTECT
, nullptr);
926 EXPECT_EQ(1u, consumed
.bytes_consumed
);
927 PacketContents contents
;
928 contents
.num_stream_frames
= 1u;
929 contents
.fec_group
= 1u;
930 CheckPacketContains(contents
, packet_
);
932 // FEC should still be on in creator.
933 EXPECT_TRUE(creator_
->IsFecProtected());
935 // Send unprotected data to cause second packet to be sent, which gets
936 // protected because it happens to fall within an open FEC group. Data packet
937 // will be followed by FEC packet.
940 EXPECT_CALL(delegate_
, OnSerializedPacket(_
)).WillOnce(
941 SaveArg
<0>(&packet2_
));
942 EXPECT_CALL(delegate_
, OnSerializedPacket(_
)).WillOnce(
943 SaveArg
<0>(&packet3_
));
945 consumed
= generator_
.ConsumeData(5, CreateData(1u), 0, true, MAY_FEC_PROTECT
,
947 EXPECT_EQ(1u, consumed
.bytes_consumed
);
948 contents
.num_stream_frames
= 1u;
949 CheckPacketContains(contents
, packet2_
);
950 CheckPacketIsFec(packet3_
, /*fec_group=*/1u);
952 // FEC protection should be off in creator.
953 EXPECT_FALSE(creator_
->IsFecProtected());
956 TEST_F(QuicPacketGeneratorTest
, SwitchFecOnOffThenOnWithCreatorProtectionOn
) {
957 delegate_
.SetCanWriteAnything();
958 generator_
.StartBatchOperations();
961 creator_
->set_max_packets_per_fec_group(2);
962 EXPECT_FALSE(creator_
->IsFecProtected());
964 // Queue one byte of FEC protected data.
965 QuicConsumedData consumed
= generator_
.ConsumeData(5, CreateData(1u), 0, true,
966 MUST_FEC_PROTECT
, nullptr);
967 EXPECT_TRUE(creator_
->HasPendingFrames());
969 // Add more unprotected data causing first packet to be sent, FEC protected.
970 EXPECT_CALL(delegate_
, OnSerializedPacket(_
)).WillOnce(
971 SaveArg
<0>(&packet_
));
972 size_t data_len
= kDefaultMaxPacketSize
;
973 consumed
= generator_
.ConsumeData(5, CreateData(data_len
), 0, true,
974 MAY_FEC_PROTECT
, nullptr);
975 EXPECT_EQ(data_len
, consumed
.bytes_consumed
);
976 PacketContents contents
;
977 contents
.num_stream_frames
= 2u;
978 contents
.fec_group
= 1u;
979 CheckPacketContains(contents
, packet_
);
981 // FEC group is still open in creator.
982 EXPECT_TRUE(creator_
->IsFecProtected());
984 // Add data that should be protected, large enough to cause second packet to
985 // be sent. Data packet should be followed by FEC packet.
988 EXPECT_CALL(delegate_
, OnSerializedPacket(_
)).WillOnce(
989 SaveArg
<0>(&packet2_
));
990 EXPECT_CALL(delegate_
, OnSerializedPacket(_
)).WillOnce(
991 SaveArg
<0>(&packet3_
));
993 consumed
= generator_
.ConsumeData(5, CreateData(data_len
), 0, true,
994 MUST_FEC_PROTECT
, nullptr);
995 EXPECT_EQ(data_len
, consumed
.bytes_consumed
);
996 CheckPacketContains(contents
, packet2_
);
997 CheckPacketIsFec(packet3_
, /*fec_group=*/1u);
999 // FEC protection should remain on in creator.
1000 EXPECT_TRUE(creator_
->IsFecProtected());
1003 TEST_F(QuicPacketGeneratorTest
, NotWritableThenBatchOperations
) {
1004 delegate_
.SetCanNotWrite();
1006 generator_
.SetShouldSendAck(false);
1007 generator_
.AddControlFrame(QuicFrame(CreateRstStreamFrame()));
1008 EXPECT_TRUE(generator_
.HasQueuedFrames());
1010 delegate_
.SetCanWriteAnything();
1012 generator_
.StartBatchOperations();
1014 // When the first write operation is invoked, the ack frame will be returned.
1015 EXPECT_CALL(delegate_
, PopulateAckFrame(_
));
1017 // Send some data and a control frame
1018 generator_
.ConsumeData(3, MakeIOVector("quux"), 7, false, MAY_FEC_PROTECT
,
1020 generator_
.AddControlFrame(QuicFrame(CreateGoAwayFrame()));
1022 // All five frames will be flushed out in a single packet.
1023 EXPECT_CALL(delegate_
, OnSerializedPacket(_
)).WillOnce(SaveArg
<0>(&packet_
));
1024 generator_
.FinishBatchOperations();
1025 EXPECT_FALSE(generator_
.HasQueuedFrames());
1027 PacketContents contents
;
1028 contents
.num_ack_frames
= 1;
1029 contents
.num_goaway_frames
= 1;
1030 contents
.num_rst_stream_frames
= 1;
1031 contents
.num_stream_frames
= 1;
1032 CheckPacketContains(contents
, packet_
);
1035 TEST_F(QuicPacketGeneratorTest
, NotWritableThenBatchOperations2
) {
1036 delegate_
.SetCanNotWrite();
1038 generator_
.SetShouldSendAck(false);
1039 generator_
.AddControlFrame(QuicFrame(CreateRstStreamFrame()));
1040 EXPECT_TRUE(generator_
.HasQueuedFrames());
1042 delegate_
.SetCanWriteAnything();
1044 generator_
.StartBatchOperations();
1046 // When the first write operation is invoked, the ack frame will be returned.
1047 EXPECT_CALL(delegate_
, PopulateAckFrame(_
));
1051 // All five frames will be flushed out in a single packet
1052 EXPECT_CALL(delegate_
, OnSerializedPacket(_
)).WillOnce(
1053 SaveArg
<0>(&packet_
));
1054 EXPECT_CALL(delegate_
, OnSerializedPacket(_
)).WillOnce(
1055 SaveArg
<0>(&packet2_
));
1058 // Send enough data to exceed one packet
1059 size_t data_len
= kDefaultMaxPacketSize
+ 100;
1060 QuicConsumedData consumed
= generator_
.ConsumeData(
1061 3, CreateData(data_len
), 0, true, MAY_FEC_PROTECT
, nullptr);
1062 EXPECT_EQ(data_len
, consumed
.bytes_consumed
);
1063 EXPECT_TRUE(consumed
.fin_consumed
);
1064 generator_
.AddControlFrame(QuicFrame(CreateGoAwayFrame()));
1066 generator_
.FinishBatchOperations();
1067 EXPECT_FALSE(generator_
.HasQueuedFrames());
1069 // The first packet should have the queued data and part of the stream data.
1070 PacketContents contents
;
1071 contents
.num_ack_frames
= 1;
1072 contents
.num_rst_stream_frames
= 1;
1073 contents
.num_stream_frames
= 1;
1074 CheckPacketContains(contents
, packet_
);
1076 // The second should have the remainder of the stream data.
1077 PacketContents contents2
;
1078 contents2
.num_goaway_frames
= 1;
1079 contents2
.num_stream_frames
= 1;
1080 CheckPacketContains(contents2
, packet2_
);
1083 TEST_F(QuicPacketGeneratorTest
, TestConnectionIdLength
) {
1084 generator_
.SetConnectionIdLength(0);
1085 EXPECT_EQ(PACKET_0BYTE_CONNECTION_ID
, creator_
->connection_id_length());
1086 generator_
.SetConnectionIdLength(1);
1087 EXPECT_EQ(PACKET_1BYTE_CONNECTION_ID
, creator_
->connection_id_length());
1088 generator_
.SetConnectionIdLength(2);
1089 EXPECT_EQ(PACKET_4BYTE_CONNECTION_ID
, creator_
->connection_id_length());
1090 generator_
.SetConnectionIdLength(3);
1091 EXPECT_EQ(PACKET_4BYTE_CONNECTION_ID
, creator_
->connection_id_length());
1092 generator_
.SetConnectionIdLength(4);
1093 EXPECT_EQ(PACKET_4BYTE_CONNECTION_ID
, creator_
->connection_id_length());
1094 generator_
.SetConnectionIdLength(5);
1095 EXPECT_EQ(PACKET_8BYTE_CONNECTION_ID
, creator_
->connection_id_length());
1096 generator_
.SetConnectionIdLength(6);
1097 EXPECT_EQ(PACKET_8BYTE_CONNECTION_ID
, creator_
->connection_id_length());
1098 generator_
.SetConnectionIdLength(7);
1099 EXPECT_EQ(PACKET_8BYTE_CONNECTION_ID
, creator_
->connection_id_length());
1100 generator_
.SetConnectionIdLength(8);
1101 EXPECT_EQ(PACKET_8BYTE_CONNECTION_ID
, creator_
->connection_id_length());
1102 generator_
.SetConnectionIdLength(9);
1103 EXPECT_EQ(PACKET_8BYTE_CONNECTION_ID
, creator_
->connection_id_length());