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
;
26 using testing::InSequence
;
27 using testing::Return
;
28 using testing::StrictMock
;
35 const int64 kMinFecTimeoutMs
= 5u;
37 static const FecSendPolicy kFecSendPolicyList
[] = {
42 class MockDelegate
: public QuicPacketGenerator::DelegateInterface
{
45 ~MockDelegate() override
{}
47 MOCK_METHOD2(ShouldGeneratePacket
,
48 bool(HasRetransmittableData retransmittable
,
49 IsHandshake handshake
));
50 MOCK_METHOD1(PopulateAckFrame
, void(QuicAckFrame
*));
51 MOCK_METHOD1(PopulateStopWaitingFrame
, void(QuicStopWaitingFrame
*));
52 MOCK_METHOD1(OnSerializedPacket
, void(const SerializedPacket
& packet
));
53 MOCK_METHOD2(CloseConnection
, void(QuicErrorCode
, bool));
54 MOCK_METHOD0(OnResetFecGroup
, void());
56 void SetCanWriteAnything() {
57 EXPECT_CALL(*this, ShouldGeneratePacket(_
, _
)).WillRepeatedly(Return(true));
58 EXPECT_CALL(*this, ShouldGeneratePacket(NO_RETRANSMITTABLE_DATA
, _
))
59 .WillRepeatedly(Return(true));
62 void SetCanNotWrite() {
63 EXPECT_CALL(*this, ShouldGeneratePacket(_
, _
))
64 .WillRepeatedly(Return(false));
65 EXPECT_CALL(*this, ShouldGeneratePacket(NO_RETRANSMITTABLE_DATA
, _
))
66 .WillRepeatedly(Return(false));
69 // Use this when only ack frames should be allowed to be written.
70 void SetCanWriteOnlyNonRetransmittable() {
71 EXPECT_CALL(*this, ShouldGeneratePacket(_
, _
))
72 .WillRepeatedly(Return(false));
73 EXPECT_CALL(*this, ShouldGeneratePacket(NO_RETRANSMITTABLE_DATA
, _
))
74 .WillRepeatedly(Return(true));
78 DISALLOW_COPY_AND_ASSIGN(MockDelegate
);
81 // Simple struct for describing the contents of a packet.
82 // Useful in conjunction with a SimpleQuicFrame for validating that a packet
83 // contains the expected frames.
84 struct PacketContents
{
87 num_connection_close_frames(0),
89 num_rst_stream_frames(0),
90 num_stop_waiting_frames(0),
93 num_mtu_discovery_frames(0),
96 size_t num_ack_frames
;
97 size_t num_connection_close_frames
;
98 size_t num_goaway_frames
;
99 size_t num_rst_stream_frames
;
100 size_t num_stop_waiting_frames
;
101 size_t num_stream_frames
;
102 size_t num_ping_frames
;
103 size_t num_mtu_discovery_frames
;
105 QuicFecGroupNumber fec_group
;
110 class QuicPacketGeneratorTest
: public ::testing::TestWithParam
<FecSendPolicy
> {
112 QuicPacketGeneratorTest()
113 : framer_(QuicSupportedVersions(),
115 Perspective::IS_CLIENT
),
116 generator_(42, &framer_
, &random_
, &delegate_
),
117 creator_(QuicPacketGeneratorPeer::GetPacketCreator(&generator_
)) {
118 generator_
.set_fec_send_policy(GetParam());
121 ~QuicPacketGeneratorTest() override
{
122 for (SerializedPacket
& packet
: packets_
) {
123 delete packet
.packet
;
124 delete packet
.retransmittable_frames
;
128 void SavePacket(const SerializedPacket
& packet
) {
129 packets_
.push_back(packet
);
130 ASSERT_FALSE(packet
.packet
->owns_buffer());
131 scoped_ptr
<QuicEncryptedPacket
> encrypted_deleter(packets_
.back().packet
);
132 packets_
.back().packet
= packets_
.back().packet
->Clone();
136 QuicRstStreamFrame
* CreateRstStreamFrame() {
137 return new QuicRstStreamFrame(1, QUIC_STREAM_NO_ERROR
, 0);
140 QuicGoAwayFrame
* CreateGoAwayFrame() {
141 return new QuicGoAwayFrame(QUIC_NO_ERROR
, 1, string());
144 void CheckPacketContains(const PacketContents
& contents
,
145 size_t packet_index
) {
146 ASSERT_GT(packets_
.size(), packet_index
);
147 const SerializedPacket
& packet
= packets_
[packet_index
];
148 size_t num_retransmittable_frames
=
149 contents
.num_connection_close_frames
+ contents
.num_goaway_frames
+
150 contents
.num_rst_stream_frames
+ contents
.num_stream_frames
+
151 contents
.num_ping_frames
;
153 contents
.num_ack_frames
+ contents
.num_stop_waiting_frames
+
154 contents
.num_mtu_discovery_frames
+ num_retransmittable_frames
;
156 if (num_retransmittable_frames
== 0) {
157 ASSERT_TRUE(packet
.retransmittable_frames
== nullptr);
159 ASSERT_TRUE(packet
.retransmittable_frames
!= nullptr);
160 EXPECT_EQ(num_retransmittable_frames
,
161 packet
.retransmittable_frames
->frames().size());
164 ASSERT_TRUE(packet
.packet
!= nullptr);
165 ASSERT_TRUE(simple_framer_
.ProcessPacket(*packet
.packet
));
166 EXPECT_EQ(num_frames
, simple_framer_
.num_frames());
167 EXPECT_EQ(contents
.num_ack_frames
, simple_framer_
.ack_frames().size());
168 EXPECT_EQ(contents
.num_connection_close_frames
,
169 simple_framer_
.connection_close_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
.num_stop_waiting_frames
,
177 simple_framer_
.stop_waiting_frames().size());
178 EXPECT_EQ(contents
.fec_group
, simple_framer_
.header().fec_group
);
180 // From the receiver's perspective, MTU discovery frames are ping frames.
181 EXPECT_EQ(contents
.num_ping_frames
+ contents
.num_mtu_discovery_frames
,
182 simple_framer_
.ping_frames().size());
185 void CheckPacketHasSingleStreamFrame(size_t packet_index
) {
186 ASSERT_GT(packets_
.size(), packet_index
);
187 const SerializedPacket
& packet
= packets_
[packet_index
];
188 ASSERT_TRUE(packet
.retransmittable_frames
!= nullptr);
189 EXPECT_EQ(1u, packet
.retransmittable_frames
->frames().size());
190 ASSERT_TRUE(packet
.packet
!= nullptr);
191 ASSERT_TRUE(simple_framer_
.ProcessPacket(*packet
.packet
));
192 EXPECT_EQ(1u, simple_framer_
.num_frames());
193 EXPECT_EQ(1u, simple_framer_
.stream_frames().size());
196 void CheckAllPacketsHaveSingleStreamFrame() {
197 for (size_t i
= 0; i
< packets_
.size(); i
++) {
198 CheckPacketHasSingleStreamFrame(i
);
202 void CheckPacketIsFec(size_t packet_index
,
203 QuicPacketSequenceNumber fec_group
) {
204 ASSERT_GT(packets_
.size(), packet_index
);
205 const SerializedPacket
& packet
= packets_
[packet_index
];
206 ASSERT_TRUE(packet
.retransmittable_frames
== nullptr);
207 ASSERT_TRUE(packet
.packet
!= nullptr);
208 ASSERT_TRUE(simple_framer_
.ProcessPacket(*packet
.packet
));
209 EXPECT_TRUE(simple_framer_
.header().fec_flag
);
210 EXPECT_EQ(fec_group
, simple_framer_
.fec_data().fec_group
);
213 QuicIOVector
CreateData(size_t len
) {
214 data_array_
.reset(new char[len
]);
215 memset(data_array_
.get(), '?', len
);
216 iov_
.iov_base
= data_array_
.get();
218 return QuicIOVector(&iov_
, 1, len
);
221 QuicIOVector
MakeIOVector(StringPiece s
) {
222 return ::net::MakeIOVector(s
, &iov_
);
227 StrictMock
<MockDelegate
> delegate_
;
228 QuicPacketGenerator generator_
;
229 QuicPacketCreator
* creator_
;
230 SimpleQuicFramer simple_framer_
;
231 vector
<SerializedPacket
> packets_
;
234 scoped_ptr
<char[]> data_array_
;
238 class MockDebugDelegate
: public QuicPacketGenerator::DebugDelegate
{
240 MOCK_METHOD1(OnFrameAddedToPacket
,
241 void(const QuicFrame
&));
244 // Run all end to end tests with all supported FEC send polocies.
245 INSTANTIATE_TEST_CASE_P(FecSendPolicy
,
246 QuicPacketGeneratorTest
,
247 ::testing::ValuesIn(kFecSendPolicyList
));
249 TEST_P(QuicPacketGeneratorTest
, ShouldSendAck_NotWritable
) {
250 delegate_
.SetCanNotWrite();
252 generator_
.SetShouldSendAck(false);
253 EXPECT_TRUE(generator_
.HasQueuedFrames());
256 TEST_P(QuicPacketGeneratorTest
, ShouldSendAck_WritableAndShouldNotFlush
) {
257 StrictMock
<MockDebugDelegate
> debug_delegate
;
259 generator_
.set_debug_delegate(&debug_delegate
);
260 delegate_
.SetCanWriteOnlyNonRetransmittable();
261 generator_
.StartBatchOperations();
263 EXPECT_CALL(delegate_
, PopulateAckFrame(_
));
264 EXPECT_CALL(debug_delegate
, OnFrameAddedToPacket(_
)).Times(1);
266 generator_
.SetShouldSendAck(false);
267 EXPECT_TRUE(generator_
.HasQueuedFrames());
270 TEST_P(QuicPacketGeneratorTest
, ShouldSendAck_WritableAndShouldFlush
) {
271 delegate_
.SetCanWriteOnlyNonRetransmittable();
273 EXPECT_CALL(delegate_
, PopulateAckFrame(_
));
274 EXPECT_CALL(delegate_
, OnSerializedPacket(_
))
275 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket
));
277 generator_
.SetShouldSendAck(false);
278 EXPECT_FALSE(generator_
.HasQueuedFrames());
280 PacketContents contents
;
281 contents
.num_ack_frames
= 1;
282 CheckPacketContains(contents
, 0);
285 TEST_P(QuicPacketGeneratorTest
, ShouldSendAck_MultipleCalls
) {
286 // Make sure that calling SetShouldSendAck multiple times does not result in a
287 // crash. Previously this would result in multiple QuicFrames queued in the
288 // packet generator, with all but the last with internal pointers to freed
290 delegate_
.SetCanWriteAnything();
292 // Only one AckFrame should be created.
293 EXPECT_CALL(delegate_
, PopulateAckFrame(_
)).Times(1);
294 EXPECT_CALL(delegate_
, OnSerializedPacket(_
))
296 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket
));
298 generator_
.StartBatchOperations();
299 generator_
.SetShouldSendAck(false);
300 generator_
.SetShouldSendAck(false);
301 generator_
.FinishBatchOperations();
304 TEST_P(QuicPacketGeneratorTest
, AddControlFrame_NotWritable
) {
305 delegate_
.SetCanNotWrite();
307 generator_
.AddControlFrame(QuicFrame(CreateRstStreamFrame()));
308 EXPECT_TRUE(generator_
.HasQueuedFrames());
311 TEST_P(QuicPacketGeneratorTest
, AddControlFrame_OnlyAckWritable
) {
312 delegate_
.SetCanWriteOnlyNonRetransmittable();
314 generator_
.AddControlFrame(QuicFrame(CreateRstStreamFrame()));
315 EXPECT_TRUE(generator_
.HasQueuedFrames());
318 TEST_P(QuicPacketGeneratorTest
, AddControlFrame_WritableAndShouldNotFlush
) {
319 delegate_
.SetCanWriteAnything();
320 generator_
.StartBatchOperations();
322 generator_
.AddControlFrame(QuicFrame(CreateRstStreamFrame()));
323 EXPECT_TRUE(generator_
.HasQueuedFrames());
326 TEST_P(QuicPacketGeneratorTest
, AddControlFrame_NotWritableBatchThenFlush
) {
327 delegate_
.SetCanNotWrite();
328 generator_
.StartBatchOperations();
330 generator_
.AddControlFrame(QuicFrame(CreateRstStreamFrame()));
331 EXPECT_TRUE(generator_
.HasQueuedFrames());
332 generator_
.FinishBatchOperations();
333 EXPECT_TRUE(generator_
.HasQueuedFrames());
335 EXPECT_CALL(delegate_
, OnSerializedPacket(_
))
336 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket
));
337 generator_
.FlushAllQueuedFrames();
338 EXPECT_FALSE(generator_
.HasQueuedFrames());
340 PacketContents contents
;
341 contents
.num_rst_stream_frames
= 1;
342 CheckPacketContains(contents
, 0);
345 TEST_P(QuicPacketGeneratorTest
, AddControlFrame_WritableAndShouldFlush
) {
346 delegate_
.SetCanWriteAnything();
348 EXPECT_CALL(delegate_
, OnSerializedPacket(_
))
349 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket
));
351 generator_
.AddControlFrame(QuicFrame(CreateRstStreamFrame()));
352 EXPECT_FALSE(generator_
.HasQueuedFrames());
354 PacketContents contents
;
355 contents
.num_rst_stream_frames
= 1;
356 CheckPacketContains(contents
, 0);
359 TEST_P(QuicPacketGeneratorTest
, ConsumeData_NotWritable
) {
360 delegate_
.SetCanNotWrite();
362 QuicConsumedData consumed
= generator_
.ConsumeData(
363 kHeadersStreamId
, MakeIOVector("foo"), 2, true, MAY_FEC_PROTECT
, nullptr);
364 EXPECT_EQ(0u, consumed
.bytes_consumed
);
365 EXPECT_FALSE(consumed
.fin_consumed
);
366 EXPECT_FALSE(generator_
.HasQueuedFrames());
369 TEST_P(QuicPacketGeneratorTest
, ConsumeData_WritableAndShouldNotFlush
) {
370 delegate_
.SetCanWriteAnything();
371 generator_
.StartBatchOperations();
373 QuicConsumedData consumed
= generator_
.ConsumeData(
374 kHeadersStreamId
, MakeIOVector("foo"), 2, true, MAY_FEC_PROTECT
, nullptr);
375 EXPECT_EQ(3u, consumed
.bytes_consumed
);
376 EXPECT_TRUE(consumed
.fin_consumed
);
377 EXPECT_TRUE(generator_
.HasQueuedFrames());
380 TEST_P(QuicPacketGeneratorTest
, ConsumeData_WritableAndShouldFlush
) {
381 delegate_
.SetCanWriteAnything();
383 EXPECT_CALL(delegate_
, OnSerializedPacket(_
))
384 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket
));
385 QuicConsumedData consumed
= generator_
.ConsumeData(
386 kHeadersStreamId
, MakeIOVector("foo"), 2, true, MAY_FEC_PROTECT
, nullptr);
387 EXPECT_EQ(3u, consumed
.bytes_consumed
);
388 EXPECT_TRUE(consumed
.fin_consumed
);
389 EXPECT_FALSE(generator_
.HasQueuedFrames());
391 PacketContents contents
;
392 contents
.num_stream_frames
= 1;
393 CheckPacketContains(contents
, 0);
396 // Test the behavior of ConsumeData when the data consumed is for the crypto
397 // handshake stream. Ensure that the packet is always sent and padded even if
398 // the generator operates in batch mode.
399 TEST_P(QuicPacketGeneratorTest
, ConsumeData_Handshake
) {
400 delegate_
.SetCanWriteAnything();
401 generator_
.StartBatchOperations();
403 EXPECT_CALL(delegate_
, OnSerializedPacket(_
))
404 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket
));
405 QuicConsumedData consumed
= generator_
.ConsumeData(
406 kCryptoStreamId
, MakeIOVector("foo"), 0, false, MAY_FEC_PROTECT
, nullptr);
407 EXPECT_EQ(3u, consumed
.bytes_consumed
);
408 EXPECT_FALSE(generator_
.HasQueuedFrames());
410 PacketContents contents
;
411 contents
.num_stream_frames
= 1;
412 CheckPacketContains(contents
, 0);
414 ASSERT_EQ(1u, packets_
.size());
415 ASSERT_EQ(kDefaultMaxPacketSize
, generator_
.GetMaxPacketLength());
416 EXPECT_EQ(kDefaultMaxPacketSize
, packets_
[0].packet
->length());
419 TEST_P(QuicPacketGeneratorTest
, ConsumeData_EmptyData
) {
420 EXPECT_DFATAL(generator_
.ConsumeData(kHeadersStreamId
, MakeIOVector(""), 0,
421 false, MAY_FEC_PROTECT
, nullptr),
422 "Attempt to consume empty data without FIN.");
425 TEST_P(QuicPacketGeneratorTest
,
426 ConsumeDataMultipleTimes_WritableAndShouldNotFlush
) {
427 delegate_
.SetCanWriteAnything();
428 generator_
.StartBatchOperations();
430 generator_
.ConsumeData(kHeadersStreamId
, MakeIOVector("foo"), 2, true,
431 MAY_FEC_PROTECT
, nullptr);
432 QuicConsumedData consumed
= generator_
.ConsumeData(
433 3, MakeIOVector("quux"), 7, false, MAY_FEC_PROTECT
, nullptr);
434 EXPECT_EQ(4u, consumed
.bytes_consumed
);
435 EXPECT_FALSE(consumed
.fin_consumed
);
436 EXPECT_TRUE(generator_
.HasQueuedFrames());
439 TEST_P(QuicPacketGeneratorTest
, ConsumeData_BatchOperations
) {
440 delegate_
.SetCanWriteAnything();
441 generator_
.StartBatchOperations();
443 generator_
.ConsumeData(kHeadersStreamId
, MakeIOVector("foo"), 2, true,
444 MAY_FEC_PROTECT
, nullptr);
445 QuicConsumedData consumed
= generator_
.ConsumeData(
446 3, MakeIOVector("quux"), 7, false, MAY_FEC_PROTECT
, nullptr);
447 EXPECT_EQ(4u, consumed
.bytes_consumed
);
448 EXPECT_FALSE(consumed
.fin_consumed
);
449 EXPECT_TRUE(generator_
.HasQueuedFrames());
451 // Now both frames will be flushed out.
452 EXPECT_CALL(delegate_
, OnSerializedPacket(_
))
453 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket
));
454 generator_
.FinishBatchOperations();
455 EXPECT_FALSE(generator_
.HasQueuedFrames());
457 PacketContents contents
;
458 contents
.num_stream_frames
= 2;
459 CheckPacketContains(contents
, 0);
462 TEST_P(QuicPacketGeneratorTest
, ConsumeDataFecOnMaxGroupSize
) {
463 delegate_
.SetCanWriteAnything();
465 // Send FEC every two packets.
466 creator_
->set_max_packets_per_fec_group(2);
470 EXPECT_CALL(delegate_
, OnSerializedPacket(_
))
471 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket
));
472 EXPECT_CALL(delegate_
, OnSerializedPacket(_
))
473 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket
));
474 if (generator_
.fec_send_policy() == FEC_ALARM_TRIGGER
) {
475 // FEC packet is not sent when send policy is FEC_ALARM_TRIGGER, but FEC
477 EXPECT_CALL(delegate_
, OnResetFecGroup()).Times(1);
479 EXPECT_CALL(delegate_
, OnSerializedPacket(_
))
480 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket
));
482 EXPECT_CALL(delegate_
, OnSerializedPacket(_
))
483 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket
));
486 // Send enough data to create 3 packets: two full and one partial. Send with
487 // MUST_FEC_PROTECT flag.
488 size_t data_len
= 2 * kDefaultMaxPacketSize
+ 100;
489 QuicConsumedData consumed
= generator_
.ConsumeData(
490 3, CreateData(data_len
), 0, true, MUST_FEC_PROTECT
, nullptr);
491 EXPECT_EQ(data_len
, consumed
.bytes_consumed
);
492 EXPECT_TRUE(consumed
.fin_consumed
);
493 EXPECT_FALSE(generator_
.HasQueuedFrames());
495 CheckPacketHasSingleStreamFrame(0);
496 CheckPacketHasSingleStreamFrame(1);
497 if (generator_
.fec_send_policy() == FEC_ALARM_TRIGGER
) {
498 // FEC packet is not sent when send policy is FEC_ALARM_TRIGGER.
499 CheckPacketHasSingleStreamFrame(2);
501 CheckPacketIsFec(2, 1);
502 CheckPacketHasSingleStreamFrame(3);
504 EXPECT_TRUE(creator_
->IsFecProtected());
506 // If FEC send policy is FEC_ANY_TRIGGER, then the FEC packet under
507 // construction will be sent when one more packet is sent (since FEC group
508 // size is 2), or when OnFecTimeout is called. Send more data with
509 // MAY_FEC_PROTECT. This packet should also be protected, and FEC packet is
510 // sent since FEC group size is reached.
512 // If FEC send policy is FEC_ALARM_TRIGGER, FEC group is closed when the group
513 // size is reached. FEC packet is not sent.
516 EXPECT_CALL(delegate_
, OnSerializedPacket(_
))
517 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket
));
518 if (generator_
.fec_send_policy() == FEC_ALARM_TRIGGER
) {
519 EXPECT_CALL(delegate_
, OnResetFecGroup()).Times(1);
521 EXPECT_CALL(delegate_
, OnSerializedPacket(_
))
522 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket
));
525 consumed
= generator_
.ConsumeData(5, CreateData(1u), 0, true, MAY_FEC_PROTECT
,
527 EXPECT_EQ(1u, consumed
.bytes_consumed
);
528 if (generator_
.fec_send_policy() == FEC_ALARM_TRIGGER
) {
529 CheckPacketHasSingleStreamFrame(3);
531 CheckPacketHasSingleStreamFrame(4);
532 CheckPacketIsFec(5, 4);
534 EXPECT_FALSE(creator_
->IsFecProtected());
537 TEST_P(QuicPacketGeneratorTest
, ConsumeDataSendsFecOnTimeout
) {
538 delegate_
.SetCanWriteAnything();
539 creator_
->set_max_packets_per_fec_group(1000);
541 // Send data with MUST_FEC_PROTECT flag. No FEC packet is emitted, but the
542 // creator FEC protects all data.
543 EXPECT_CALL(delegate_
, OnSerializedPacket(_
))
544 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket
));
545 QuicConsumedData consumed
= generator_
.ConsumeData(3, CreateData(1u), 0, true,
546 MUST_FEC_PROTECT
, nullptr);
547 EXPECT_EQ(1u, consumed
.bytes_consumed
);
548 EXPECT_TRUE(consumed
.fin_consumed
);
549 CheckPacketHasSingleStreamFrame(0);
550 EXPECT_TRUE(creator_
->IsFecProtected());
552 // Send more data with MAY_FEC_PROTECT. This packet should also be protected,
553 // and FEC packet is not yet sent.
554 EXPECT_CALL(delegate_
, OnSerializedPacket(_
))
555 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket
));
556 consumed
= generator_
.ConsumeData(5, CreateData(1u), 0, true, MAY_FEC_PROTECT
,
558 EXPECT_EQ(1u, consumed
.bytes_consumed
);
559 CheckPacketHasSingleStreamFrame(1);
560 EXPECT_TRUE(creator_
->IsFecProtected());
562 // Calling OnFecTimeout should cause the FEC packet to be emitted.
563 EXPECT_CALL(delegate_
, OnSerializedPacket(_
))
564 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket
));
565 generator_
.OnFecTimeout();
566 CheckPacketIsFec(2, 1);
567 EXPECT_FALSE(creator_
->IsFecProtected());
569 // Subsequent data is protected under the next FEC group. Send enough data to
570 // create 2 more packets: one full and one partial.
573 EXPECT_CALL(delegate_
, OnSerializedPacket(_
))
574 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket
));
575 EXPECT_CALL(delegate_
, OnSerializedPacket(_
))
576 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket
));
578 size_t data_len
= kDefaultMaxPacketSize
+ 1;
579 consumed
= generator_
.ConsumeData(7, CreateData(data_len
), 0, true,
580 MUST_FEC_PROTECT
, nullptr);
581 EXPECT_EQ(data_len
, consumed
.bytes_consumed
);
582 EXPECT_TRUE(consumed
.fin_consumed
);
583 CheckPacketHasSingleStreamFrame(3);
584 CheckPacketHasSingleStreamFrame(4);
585 EXPECT_TRUE(creator_
->IsFecProtected());
587 // Calling OnFecTimeout should cause the FEC packet to be emitted.
588 EXPECT_CALL(delegate_
, OnSerializedPacket(_
))
589 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket
));
590 generator_
.OnFecTimeout();
591 CheckPacketIsFec(5, 4);
592 EXPECT_FALSE(creator_
->IsFecProtected());
595 TEST_P(QuicPacketGeneratorTest
, GetFecTimeoutFiniteOnlyOnFirstPacketInGroup
) {
596 delegate_
.SetCanWriteAnything();
597 creator_
->set_max_packets_per_fec_group(6);
599 // Send enough data to create 2 packets: one full and one partial. Send with
600 // MUST_FEC_PROTECT flag. No FEC packet is emitted yet, but the creator FEC
601 // protects all data.
604 EXPECT_CALL(delegate_
, OnSerializedPacket(_
))
605 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket
));
606 EXPECT_CALL(delegate_
, OnSerializedPacket(_
))
607 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket
));
609 size_t data_len
= 1 * kDefaultMaxPacketSize
+ 100;
610 QuicConsumedData consumed
= generator_
.ConsumeData(
611 3, CreateData(data_len
), 0, true, MUST_FEC_PROTECT
, nullptr);
612 EXPECT_EQ(data_len
, consumed
.bytes_consumed
);
613 EXPECT_TRUE(consumed
.fin_consumed
);
614 EXPECT_FALSE(generator_
.HasQueuedFrames());
615 CheckPacketHasSingleStreamFrame(0);
616 CheckPacketHasSingleStreamFrame(1);
617 EXPECT_TRUE(creator_
->IsFecProtected());
619 // GetFecTimeout returns finite timeout only for first packet in group.
620 EXPECT_EQ(QuicTime::Delta::FromMilliseconds(kMinFecTimeoutMs
),
621 generator_
.GetFecTimeout(/*sequence_number=*/1u));
622 EXPECT_EQ(QuicTime::Delta::Infinite(),
623 generator_
.GetFecTimeout(/*sequence_number=*/2u));
625 // Send more data with MAY_FEC_PROTECT. This packet should also be protected,
626 // and FEC packet is not yet sent.
627 EXPECT_CALL(delegate_
, OnSerializedPacket(_
))
628 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket
));
629 consumed
= generator_
.ConsumeData(5, CreateData(1u), 0, true, MAY_FEC_PROTECT
,
631 CheckPacketHasSingleStreamFrame(2);
632 EXPECT_TRUE(creator_
->IsFecProtected());
634 // GetFecTimeout returns finite timeout only for first packet in group.
635 EXPECT_EQ(QuicTime::Delta::Infinite(),
636 generator_
.GetFecTimeout(/*sequence_number=*/3u));
638 // Calling OnFecTimeout should cause the FEC packet to be emitted.
639 EXPECT_CALL(delegate_
, OnSerializedPacket(_
))
640 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket
));
641 generator_
.OnFecTimeout();
642 CheckPacketIsFec(3, /*fec_group=*/1u);
643 EXPECT_FALSE(creator_
->IsFecProtected());
645 // Subsequent data is protected under the next FEC group. Send enough data to
646 // create 2 more packets: one full and one partial.
649 EXPECT_CALL(delegate_
, OnSerializedPacket(_
))
650 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket
));
651 EXPECT_CALL(delegate_
, OnSerializedPacket(_
))
652 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket
));
654 data_len
= kDefaultMaxPacketSize
+ 1u;
655 consumed
= generator_
.ConsumeData(7, CreateData(data_len
), 0, true,
656 MUST_FEC_PROTECT
, nullptr);
657 EXPECT_EQ(data_len
, consumed
.bytes_consumed
);
658 EXPECT_TRUE(consumed
.fin_consumed
);
659 CheckPacketHasSingleStreamFrame(4);
660 CheckPacketHasSingleStreamFrame(5);
661 EXPECT_TRUE(creator_
->IsFecProtected());
663 // GetFecTimeout returns finite timeout for first packet in the new group.
664 EXPECT_EQ(QuicTime::Delta::FromMilliseconds(kMinFecTimeoutMs
),
665 generator_
.GetFecTimeout(/*sequence_number=*/5u));
666 EXPECT_EQ(QuicTime::Delta::Infinite(),
667 generator_
.GetFecTimeout(/*sequence_number=*/6u));
669 // Calling OnFecTimeout should cause the FEC packet to be emitted.
670 EXPECT_CALL(delegate_
, OnSerializedPacket(_
))
671 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket
));
672 generator_
.OnFecTimeout();
673 CheckPacketIsFec(6, /*fec_group=*/5u);
674 EXPECT_FALSE(creator_
->IsFecProtected());
676 // Send more data with MAY_FEC_PROTECT. No FEC protection, so GetFecTimeout
678 EXPECT_CALL(delegate_
, OnSerializedPacket(_
))
679 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket
));
680 consumed
= generator_
.ConsumeData(9, CreateData(1u), 0, true, MAY_FEC_PROTECT
,
682 CheckPacketHasSingleStreamFrame(7);
683 EXPECT_FALSE(creator_
->IsFecProtected());
684 EXPECT_EQ(QuicTime::Delta::Infinite(),
685 generator_
.GetFecTimeout(/*sequence_number=*/8u));
688 TEST_P(QuicPacketGeneratorTest
, ConsumeData_FramesPreviouslyQueued
) {
689 // Set the packet size be enough for two stream frames with 0 stream offset,
690 // but not enough for a stream frame of 0 offset and one with non-zero offset.
692 NullEncrypter().GetCiphertextSize(0) +
694 creator_
->connection_id_length(), true,
695 QuicPacketCreatorPeer::NextSequenceNumberLength(creator_
),
697 // Add an extra 3 bytes for the payload and 1 byte so BytesFree is larger
698 // than the GetMinStreamFrameSize.
699 QuicFramer::GetMinStreamFrameSize(1, 0, false, NOT_IN_FEC_GROUP
) + 3 +
700 QuicFramer::GetMinStreamFrameSize(1, 0, true, NOT_IN_FEC_GROUP
) + 1;
701 generator_
.SetMaxPacketLength(length
, /*force=*/false);
702 delegate_
.SetCanWriteAnything();
705 EXPECT_CALL(delegate_
, OnSerializedPacket(_
))
706 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket
));
707 EXPECT_CALL(delegate_
, OnSerializedPacket(_
))
708 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket
));
710 generator_
.StartBatchOperations();
711 // Queue enough data to prevent a stream frame with a non-zero offset from
713 QuicConsumedData consumed
=
714 generator_
.ConsumeData(kHeadersStreamId
, MakeIOVector("foo"), 0, false,
715 MAY_FEC_PROTECT
, nullptr);
716 EXPECT_EQ(3u, consumed
.bytes_consumed
);
717 EXPECT_FALSE(consumed
.fin_consumed
);
718 EXPECT_TRUE(generator_
.HasQueuedFrames());
720 // This frame will not fit with the existing frame, causing the queued frame
721 // to be serialized, and it will not fit with another frame like it, so it is
722 // serialized by itself.
723 consumed
= generator_
.ConsumeData(kHeadersStreamId
, MakeIOVector("bar"), 3,
724 true, MAY_FEC_PROTECT
, nullptr);
725 EXPECT_EQ(3u, consumed
.bytes_consumed
);
726 EXPECT_TRUE(consumed
.fin_consumed
);
727 EXPECT_FALSE(generator_
.HasQueuedFrames());
729 PacketContents contents
;
730 contents
.num_stream_frames
= 1;
731 CheckPacketContains(contents
, 0);
732 CheckPacketContains(contents
, 1);
735 TEST_P(QuicPacketGeneratorTest
, NoFecPacketSentWhenBatchEnds
) {
736 delegate_
.SetCanWriteAnything();
737 creator_
->set_max_packets_per_fec_group(6);
739 generator_
.StartBatchOperations();
741 generator_
.ConsumeData(3, MakeIOVector("foo"), 2, true, MUST_FEC_PROTECT
,
743 QuicConsumedData consumed
= generator_
.ConsumeData(
744 5, MakeIOVector("quux"), 7, false, MUST_FEC_PROTECT
, nullptr);
745 EXPECT_EQ(4u, consumed
.bytes_consumed
);
746 EXPECT_FALSE(consumed
.fin_consumed
);
747 EXPECT_TRUE(generator_
.HasQueuedFrames());
749 // Now both frames will be flushed out, but FEC packet is not yet sent.
750 EXPECT_CALL(delegate_
, OnSerializedPacket(_
))
751 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket
));
752 generator_
.FinishBatchOperations();
753 EXPECT_FALSE(generator_
.HasQueuedFrames());
755 PacketContents contents
;
756 contents
.num_stream_frames
= 2u;
757 contents
.fec_group
= 1u;
758 CheckPacketContains(contents
, 0);
760 // Forcing FEC timeout causes FEC packet to be emitted.
761 EXPECT_CALL(delegate_
, OnSerializedPacket(_
))
762 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket
));
763 generator_
.OnFecTimeout();
764 CheckPacketIsFec(1, /*fec_group=*/1u);
767 TEST_P(QuicPacketGeneratorTest
, FecTimeoutOnRttChange
) {
768 EXPECT_EQ(QuicTime::Delta::Zero(),
769 QuicPacketGeneratorPeer::GetFecTimeout(&generator_
));
770 generator_
.OnRttChange(QuicTime::Delta::FromMilliseconds(300));
771 EXPECT_EQ(QuicTime::Delta::FromMilliseconds(150),
772 QuicPacketGeneratorPeer::GetFecTimeout(&generator_
));
775 TEST_P(QuicPacketGeneratorTest
, FecGroupSizeOnCongestionWindowChange
) {
776 delegate_
.SetCanWriteAnything();
777 creator_
->set_max_packets_per_fec_group(50);
778 EXPECT_EQ(50u, creator_
->max_packets_per_fec_group());
779 EXPECT_FALSE(creator_
->IsFecGroupOpen());
782 generator_
.OnCongestionWindowChange(7);
783 EXPECT_EQ(3u, creator_
->max_packets_per_fec_group());
785 // On increased cwnd.
786 generator_
.OnCongestionWindowChange(100);
787 EXPECT_EQ(50u, creator_
->max_packets_per_fec_group());
789 // On collapsed cwnd.
790 generator_
.OnCongestionWindowChange(1);
791 EXPECT_EQ(2u, creator_
->max_packets_per_fec_group());
794 TEST_P(QuicPacketGeneratorTest
, FecGroupSizeChangeWithOpenGroup
) {
795 delegate_
.SetCanWriteAnything();
796 generator_
.StartBatchOperations();
797 creator_
->set_max_packets_per_fec_group(50);
798 EXPECT_EQ(50u, creator_
->max_packets_per_fec_group());
799 EXPECT_FALSE(creator_
->IsFecGroupOpen());
801 // Send enough data to create 4 packets with MUST_FEC_PROTECT flag. 3 packets
802 // are sent, one is queued in the creator.
805 EXPECT_CALL(delegate_
, OnSerializedPacket(_
))
806 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket
));
807 EXPECT_CALL(delegate_
, OnSerializedPacket(_
))
808 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket
));
809 EXPECT_CALL(delegate_
, OnSerializedPacket(_
))
810 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket
));
812 size_t data_len
= 3 * kDefaultMaxPacketSize
+ 1;
813 QuicConsumedData consumed
= generator_
.ConsumeData(
814 7, CreateData(data_len
), 0, true, MUST_FEC_PROTECT
, nullptr);
815 EXPECT_EQ(data_len
, consumed
.bytes_consumed
);
816 EXPECT_TRUE(creator_
->IsFecGroupOpen());
818 // Change FEC groupsize.
819 generator_
.OnCongestionWindowChange(2);
820 EXPECT_EQ(2u, creator_
->max_packets_per_fec_group());
822 // If FEC send policy is FEC_ANY_TRIGGER, then send enough data to trigger one
823 // unprotected data packet, causing the FEC packet to also be sent.
825 // If FEC send policy is FEC_ALARM_TRIGGER, FEC group is closed and FEC packet
829 EXPECT_CALL(delegate_
, OnSerializedPacket(_
))
830 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket
));
831 if (generator_
.fec_send_policy() == FEC_ALARM_TRIGGER
) {
832 EXPECT_CALL(delegate_
, OnResetFecGroup()).Times(1);
834 EXPECT_CALL(delegate_
, OnSerializedPacket(_
))
835 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket
));
838 consumed
= generator_
.ConsumeData(7, CreateData(kDefaultMaxPacketSize
), 0,
839 true, MAY_FEC_PROTECT
, nullptr);
840 EXPECT_EQ(kDefaultMaxPacketSize
, consumed
.bytes_consumed
);
841 if (generator_
.fec_send_policy() == FEC_ANY_TRIGGER
) {
842 // Verify that one FEC packet was sent.
843 CheckPacketIsFec(4, /*fec_group=*/1u);
845 EXPECT_FALSE(creator_
->IsFecGroupOpen());
846 EXPECT_FALSE(creator_
->IsFecProtected());
849 TEST_P(QuicPacketGeneratorTest
, SwitchFecOnOff
) {
850 delegate_
.SetCanWriteAnything();
851 creator_
->set_max_packets_per_fec_group(2);
852 EXPECT_FALSE(creator_
->IsFecProtected());
854 // Send one unprotected data packet.
855 EXPECT_CALL(delegate_
, OnSerializedPacket(_
))
856 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket
));
857 QuicConsumedData consumed
= generator_
.ConsumeData(5, CreateData(1u), 0, true,
858 MAY_FEC_PROTECT
, nullptr);
859 EXPECT_EQ(1u, consumed
.bytes_consumed
);
860 EXPECT_FALSE(generator_
.HasQueuedFrames());
861 EXPECT_FALSE(creator_
->IsFecProtected());
862 // Verify that one data packet was sent.
863 PacketContents contents
;
864 contents
.num_stream_frames
= 1;
865 CheckPacketContains(contents
, 0);
869 EXPECT_CALL(delegate_
, OnSerializedPacket(_
))
870 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket
));
871 EXPECT_CALL(delegate_
, OnSerializedPacket(_
))
872 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket
));
873 if (generator_
.fec_send_policy() == FEC_ALARM_TRIGGER
) {
874 // If FEC send policy is FEC_ALARM_TRIGGER, FEC group is closed.
875 EXPECT_CALL(delegate_
, OnResetFecGroup()).Times(1);
877 EXPECT_CALL(delegate_
, OnSerializedPacket(_
))
878 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket
));
880 EXPECT_CALL(delegate_
, OnSerializedPacket(_
))
881 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket
));
883 // Send enough data to create 3 packets with MUST_FEC_PROTECT flag.
884 size_t data_len
= 2 * kDefaultMaxPacketSize
+ 100;
885 consumed
= generator_
.ConsumeData(7, CreateData(data_len
), 0, true,
886 MUST_FEC_PROTECT
, nullptr);
887 EXPECT_EQ(data_len
, consumed
.bytes_consumed
);
888 EXPECT_FALSE(generator_
.HasQueuedFrames());
890 // If FEC send policy is FEC_ANY_TRIGGER, verify that packets sent were 3 data
893 // If FEC send policy is FEC_ALARM_TRIGGER, verify that packets sent were 3
894 // data and FEC group is closed.
895 CheckPacketHasSingleStreamFrame(1);
896 CheckPacketHasSingleStreamFrame(2);
897 if (generator_
.fec_send_policy() == FEC_ALARM_TRIGGER
) {
898 CheckPacketHasSingleStreamFrame(3);
900 CheckPacketIsFec(3, /*fec_group=*/2u);
901 CheckPacketHasSingleStreamFrame(4);
904 // Calling OnFecTimeout should emit the pending FEC packet.
905 EXPECT_CALL(delegate_
, OnSerializedPacket(_
))
906 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket
));
907 generator_
.OnFecTimeout();
908 if (generator_
.fec_send_policy() == FEC_ALARM_TRIGGER
) {
909 CheckPacketIsFec(4, /*fec_group=*/4u);
911 CheckPacketIsFec(5, /*fec_group=*/5u);
914 // Send one unprotected data packet.
915 EXPECT_CALL(delegate_
, OnSerializedPacket(_
))
916 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket
));
917 consumed
= generator_
.ConsumeData(7, CreateData(1u), 0, true, MAY_FEC_PROTECT
,
919 EXPECT_EQ(1u, consumed
.bytes_consumed
);
920 EXPECT_FALSE(generator_
.HasQueuedFrames());
921 EXPECT_FALSE(creator_
->IsFecProtected());
922 // Verify that one unprotected data packet was sent.
923 if (generator_
.fec_send_policy() == FEC_ALARM_TRIGGER
) {
924 CheckPacketContains(contents
, 5);
926 CheckPacketContains(contents
, 6);
930 TEST_P(QuicPacketGeneratorTest
, SwitchFecOnWithPendingFrameInCreator
) {
931 delegate_
.SetCanWriteAnything();
933 creator_
->set_max_packets_per_fec_group(2);
935 generator_
.StartBatchOperations();
936 // Queue enough data to prevent a stream frame with a non-zero offset from
938 QuicConsumedData consumed
= generator_
.ConsumeData(7, CreateData(1u), 0, true,
939 MAY_FEC_PROTECT
, nullptr);
940 EXPECT_EQ(1u, consumed
.bytes_consumed
);
941 EXPECT_TRUE(creator_
->HasPendingFrames());
943 // Queue protected data for sending. Should cause queued frames to be flushed.
944 EXPECT_CALL(delegate_
, OnSerializedPacket(_
))
945 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket
));
946 EXPECT_FALSE(creator_
->IsFecProtected());
947 consumed
= generator_
.ConsumeData(7, CreateData(1u), 0, true,
948 MUST_FEC_PROTECT
, nullptr);
949 EXPECT_EQ(1u, consumed
.bytes_consumed
);
950 PacketContents contents
;
951 contents
.num_stream_frames
= 1;
952 // Transmitted packet was not FEC protected.
953 CheckPacketContains(contents
, 0);
954 EXPECT_TRUE(creator_
->IsFecProtected());
955 EXPECT_TRUE(creator_
->HasPendingFrames());
958 TEST_P(QuicPacketGeneratorTest
, SwitchFecOnWithPendingFramesInGenerator
) {
960 creator_
->set_max_packets_per_fec_group(2);
962 // Queue control frames in generator.
963 delegate_
.SetCanNotWrite();
964 generator_
.SetShouldSendAck(true);
965 delegate_
.SetCanWriteAnything();
966 generator_
.StartBatchOperations();
968 // Set up frames to write into the creator when control frames are written.
969 EXPECT_CALL(delegate_
, PopulateAckFrame(_
));
970 EXPECT_CALL(delegate_
, PopulateStopWaitingFrame(_
));
972 // Generator should have queued control frames, and creator should be empty.
973 EXPECT_TRUE(generator_
.HasQueuedFrames());
974 EXPECT_FALSE(creator_
->HasPendingFrames());
975 EXPECT_FALSE(creator_
->IsFecProtected());
977 // Queue protected data for sending. Should cause queued frames to be flushed.
978 EXPECT_CALL(delegate_
, OnSerializedPacket(_
))
979 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket
));
980 QuicConsumedData consumed
= generator_
.ConsumeData(7, CreateData(1u), 0, true,
981 MUST_FEC_PROTECT
, nullptr);
982 EXPECT_EQ(1u, consumed
.bytes_consumed
);
983 PacketContents contents
;
984 contents
.num_ack_frames
= 1;
985 contents
.num_stop_waiting_frames
= 1;
986 CheckPacketContains(contents
, 0);
988 // FEC protection should be on in creator.
989 EXPECT_TRUE(creator_
->IsFecProtected());
992 TEST_P(QuicPacketGeneratorTest
, SwitchFecOnOffWithSubsequentFramesProtected
) {
993 delegate_
.SetCanWriteAnything();
996 creator_
->set_max_packets_per_fec_group(2);
997 EXPECT_FALSE(creator_
->IsFecProtected());
999 // Queue stream frame to be protected in creator.
1000 generator_
.StartBatchOperations();
1001 QuicConsumedData consumed
= generator_
.ConsumeData(5, CreateData(1u), 0, true,
1002 MUST_FEC_PROTECT
, nullptr);
1003 EXPECT_EQ(1u, consumed
.bytes_consumed
);
1004 // Creator has a pending protected frame.
1005 EXPECT_TRUE(creator_
->HasPendingFrames());
1006 EXPECT_TRUE(creator_
->IsFecProtected());
1008 // Add enough unprotected data to exceed size of current packet, so that
1009 // current packet is sent. Both frames will be sent out in a single packet.
1010 EXPECT_CALL(delegate_
, OnSerializedPacket(_
))
1011 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket
));
1012 size_t data_len
= kDefaultMaxPacketSize
;
1013 consumed
= generator_
.ConsumeData(5, CreateData(data_len
), 0, true,
1014 MAY_FEC_PROTECT
, nullptr);
1015 EXPECT_EQ(data_len
, consumed
.bytes_consumed
);
1016 PacketContents contents
;
1017 contents
.num_stream_frames
= 2u;
1018 contents
.fec_group
= 1u;
1019 CheckPacketContains(contents
, 0);
1020 // FEC protection should still be on in creator.
1021 EXPECT_TRUE(creator_
->IsFecProtected());
1024 TEST_P(QuicPacketGeneratorTest
, SwitchFecOnOffWithSubsequentPacketsProtected
) {
1025 delegate_
.SetCanWriteAnything();
1028 creator_
->set_max_packets_per_fec_group(2);
1029 EXPECT_FALSE(creator_
->IsFecProtected());
1031 // Send first packet, FEC protected.
1032 EXPECT_CALL(delegate_
, OnSerializedPacket(_
))
1033 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket
));
1034 QuicConsumedData consumed
= generator_
.ConsumeData(5, CreateData(1u), 0, true,
1035 MUST_FEC_PROTECT
, nullptr);
1036 EXPECT_EQ(1u, consumed
.bytes_consumed
);
1037 PacketContents contents
;
1038 contents
.num_stream_frames
= 1u;
1039 contents
.fec_group
= 1u;
1040 CheckPacketContains(contents
, 0);
1042 // FEC should still be on in creator.
1043 EXPECT_TRUE(creator_
->IsFecProtected());
1045 // Send unprotected data to cause second packet to be sent, which gets
1046 // protected because it happens to fall within an open FEC group. Data packet
1047 // will be followed by FEC packet.
1050 EXPECT_CALL(delegate_
, OnSerializedPacket(_
))
1051 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket
));
1052 if (generator_
.fec_send_policy() == FEC_ALARM_TRIGGER
) {
1053 EXPECT_CALL(delegate_
, OnResetFecGroup()).Times(1);
1055 EXPECT_CALL(delegate_
, OnSerializedPacket(_
))
1056 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket
));
1059 consumed
= generator_
.ConsumeData(5, CreateData(1u), 0, true, MAY_FEC_PROTECT
,
1061 EXPECT_EQ(1u, consumed
.bytes_consumed
);
1062 contents
.num_stream_frames
= 1u;
1063 CheckPacketContains(contents
, 1);
1064 if (generator_
.fec_send_policy() == FEC_ANY_TRIGGER
) {
1065 // FEC packet is sent when send policy is FEC_ANY_TRIGGER.
1066 CheckPacketIsFec(2, /*fec_group=*/1u);
1069 // FEC protection should be off in creator.
1070 EXPECT_FALSE(creator_
->IsFecProtected());
1073 TEST_P(QuicPacketGeneratorTest
, SwitchFecOnOffThenOnWithCreatorProtectionOn
) {
1074 delegate_
.SetCanWriteAnything();
1075 generator_
.StartBatchOperations();
1078 creator_
->set_max_packets_per_fec_group(2);
1079 EXPECT_FALSE(creator_
->IsFecProtected());
1081 // Queue one byte of FEC protected data.
1082 QuicConsumedData consumed
= generator_
.ConsumeData(5, CreateData(1u), 0, true,
1083 MUST_FEC_PROTECT
, nullptr);
1084 EXPECT_TRUE(creator_
->HasPendingFrames());
1086 // Add more unprotected data causing first packet to be sent, FEC protected.
1087 EXPECT_CALL(delegate_
, OnSerializedPacket(_
))
1088 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket
));
1089 size_t data_len
= kDefaultMaxPacketSize
;
1090 consumed
= generator_
.ConsumeData(5, CreateData(data_len
), 0, true,
1091 MAY_FEC_PROTECT
, nullptr);
1092 EXPECT_EQ(data_len
, consumed
.bytes_consumed
);
1093 PacketContents contents
;
1094 contents
.num_stream_frames
= 2u;
1095 contents
.fec_group
= 1u;
1096 CheckPacketContains(contents
, 0);
1098 // FEC group is still open in creator.
1099 EXPECT_TRUE(creator_
->IsFecProtected());
1101 // Add data that should be protected, large enough to cause second packet to
1102 // be sent. Data packet should be followed by FEC packet.
1105 EXPECT_CALL(delegate_
, OnSerializedPacket(_
))
1106 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket
));
1107 if (generator_
.fec_send_policy() == FEC_ALARM_TRIGGER
) {
1108 EXPECT_CALL(delegate_
, OnResetFecGroup()).Times(1);
1110 EXPECT_CALL(delegate_
, OnSerializedPacket(_
))
1111 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket
));
1114 consumed
= generator_
.ConsumeData(5, CreateData(data_len
), 0, true,
1115 MUST_FEC_PROTECT
, nullptr);
1116 EXPECT_EQ(data_len
, consumed
.bytes_consumed
);
1117 CheckPacketContains(contents
, 1);
1118 if (generator_
.fec_send_policy() == FEC_ANY_TRIGGER
) {
1119 // FEC packet is sent when send policy is FEC_ANY_TRIGGER.
1120 CheckPacketIsFec(2, /*fec_group=*/1u);
1123 // FEC protection should remain on in creator.
1124 EXPECT_TRUE(creator_
->IsFecProtected());
1127 TEST_P(QuicPacketGeneratorTest
, ResetFecGroupNoTimeout
) {
1128 delegate_
.SetCanWriteAnything();
1129 // Send FEC packet after 2 packets.
1130 creator_
->set_max_packets_per_fec_group(2);
1131 EXPECT_FALSE(creator_
->IsFecProtected());
1133 // Send two packets so that when this data is consumed, two packets are sent
1134 // out. In FEC_TRIGGER_ANY, this will cause an FEC packet to be sent out and
1135 // with FEC_TRIGGER_ALARM, this will cause a Reset to be called. In both
1136 // cases, the creator's fec protection will be turned off afterwards.
1139 EXPECT_CALL(delegate_
, OnSerializedPacket(_
))
1140 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket
));
1141 EXPECT_CALL(delegate_
, OnSerializedPacket(_
))
1142 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket
));
1143 if (generator_
.fec_send_policy() == FEC_ALARM_TRIGGER
) {
1144 // FEC packet is not sent when send policy is FEC_ALARM_TRIGGER, but FEC
1146 EXPECT_CALL(delegate_
, OnResetFecGroup()).Times(1);
1148 EXPECT_CALL(delegate_
, OnSerializedPacket(_
))
1149 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket
));
1152 EXPECT_CALL(delegate_
, OnSerializedPacket(_
))
1153 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket
));
1155 size_t data_len
= 2 * kDefaultMaxPacketSize
;
1156 QuicConsumedData consumed
= generator_
.ConsumeData(
1157 5, CreateData(data_len
), 0, true, MUST_FEC_PROTECT
, nullptr);
1158 EXPECT_EQ(data_len
, consumed
.bytes_consumed
);
1159 EXPECT_TRUE(consumed
.fin_consumed
);
1160 EXPECT_FALSE(generator_
.HasQueuedFrames());
1161 CheckPacketHasSingleStreamFrame(0);
1162 CheckPacketHasSingleStreamFrame(1);
1163 if (generator_
.fec_send_policy() == FEC_ALARM_TRIGGER
) {
1164 // FEC packet is not sent when send policy is FEC_ALARM_TRIGGER.
1165 CheckPacketHasSingleStreamFrame(2);
1167 // FEC packet is sent after 2 packets and when send policy is
1169 CheckPacketIsFec(2, 1);
1170 CheckPacketHasSingleStreamFrame(3);
1172 EXPECT_TRUE(creator_
->IsFecProtected());
1174 // Do the same send (with MUST_FEC_PROTECT) on a different stream id.
1177 EXPECT_CALL(delegate_
, OnSerializedPacket(_
))
1178 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket
));
1179 // FEC packet is sent after 2 packets and when send policy is
1180 // FEC_ANY_TRIGGER. When policy is FEC_ALARM_TRIGGER, FEC group is closed
1181 // and FEC packet is not sent.
1182 if (generator_
.fec_send_policy() == FEC_ALARM_TRIGGER
) {
1183 EXPECT_CALL(delegate_
, OnResetFecGroup()).Times(1);
1185 EXPECT_CALL(delegate_
, OnSerializedPacket(_
))
1186 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket
));
1188 EXPECT_CALL(delegate_
, OnSerializedPacket(_
))
1189 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket
));
1190 EXPECT_CALL(delegate_
, OnSerializedPacket(_
))
1191 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket
));
1192 // FEC packet is sent after 2 packets and when send policy is
1193 // FEC_ANY_TRIGGER. When policy is FEC_ALARM_TRIGGER, FEC group is closed
1194 // and FEC packet is not sent.
1195 if (generator_
.fec_send_policy() == FEC_ALARM_TRIGGER
) {
1196 EXPECT_CALL(delegate_
, OnResetFecGroup()).Times(1);
1198 EXPECT_CALL(delegate_
, OnSerializedPacket(_
))
1199 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket
));
1202 consumed
= generator_
.ConsumeData(7, CreateData(data_len
), 0, true,
1203 MUST_FEC_PROTECT
, nullptr);
1204 EXPECT_EQ(data_len
, consumed
.bytes_consumed
);
1205 EXPECT_TRUE(consumed
.fin_consumed
);
1206 EXPECT_FALSE(generator_
.HasQueuedFrames());
1207 if (generator_
.fec_send_policy() == FEC_ALARM_TRIGGER
) {
1208 CheckPacketHasSingleStreamFrame(3);
1209 CheckPacketHasSingleStreamFrame(4);
1210 CheckPacketHasSingleStreamFrame(5);
1212 CheckPacketHasSingleStreamFrame(4);
1213 // FEC packet is sent after 2 packets and when send policy is
1215 CheckPacketIsFec(5, 4);
1216 CheckPacketHasSingleStreamFrame(6);
1217 CheckPacketHasSingleStreamFrame(7);
1218 // FEC packet is sent after 2 packets and when send policy is
1220 CheckPacketIsFec(8, 7);
1222 EXPECT_TRUE(creator_
->IsFecProtected());
1225 // 1. Create and send one packet with MUST_FEC_PROTECT.
1226 // 2. Call FecTimeout, expect FEC packet is sent.
1227 // 3. Do the same thing over again, with a different stream id.
1228 TEST_P(QuicPacketGeneratorTest
, FecPacketSentOnFecTimeout
) {
1229 delegate_
.SetCanWriteAnything();
1230 creator_
->set_max_packets_per_fec_group(1000);
1231 EXPECT_FALSE(creator_
->IsFecProtected());
1233 for (int i
= 1; i
< 4; i
= i
+ 2) {
1234 // Send data with MUST_FEC_PROTECT flag. No FEC packet is emitted, but the
1235 // creator FEC protects all data.
1236 EXPECT_CALL(delegate_
, OnSerializedPacket(_
))
1237 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket
));
1238 QuicConsumedData consumed
= generator_
.ConsumeData(
1239 i
+ 2, CreateData(1u), 0, true, MUST_FEC_PROTECT
, nullptr);
1240 EXPECT_EQ(1u, consumed
.bytes_consumed
);
1241 EXPECT_TRUE(consumed
.fin_consumed
);
1242 CheckPacketHasSingleStreamFrame(0);
1243 EXPECT_TRUE(creator_
->IsFecProtected());
1245 // Calling OnFecTimeout should cause the FEC packet to be emitted.
1246 EXPECT_CALL(delegate_
, OnSerializedPacket(_
))
1247 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket
));
1248 generator_
.OnFecTimeout();
1249 CheckPacketIsFec(i
, i
);
1250 EXPECT_FALSE(creator_
->IsFecProtected());
1254 TEST_P(QuicPacketGeneratorTest
, NotWritableThenBatchOperations
) {
1255 delegate_
.SetCanNotWrite();
1257 generator_
.SetShouldSendAck(false);
1258 generator_
.AddControlFrame(QuicFrame(CreateRstStreamFrame()));
1259 EXPECT_TRUE(generator_
.HasQueuedFrames());
1261 delegate_
.SetCanWriteAnything();
1263 generator_
.StartBatchOperations();
1265 // When the first write operation is invoked, the ack frame will be returned.
1266 EXPECT_CALL(delegate_
, PopulateAckFrame(_
));
1268 // Send some data and a control frame
1269 generator_
.ConsumeData(3, MakeIOVector("quux"), 7, false, MAY_FEC_PROTECT
,
1271 generator_
.AddControlFrame(QuicFrame(CreateGoAwayFrame()));
1273 // All five frames will be flushed out in a single packet.
1274 EXPECT_CALL(delegate_
, OnSerializedPacket(_
))
1275 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket
));
1276 generator_
.FinishBatchOperations();
1277 EXPECT_FALSE(generator_
.HasQueuedFrames());
1279 PacketContents contents
;
1280 contents
.num_ack_frames
= 1;
1281 contents
.num_goaway_frames
= 1;
1282 contents
.num_rst_stream_frames
= 1;
1283 contents
.num_stream_frames
= 1;
1284 CheckPacketContains(contents
, 0);
1287 TEST_P(QuicPacketGeneratorTest
, NotWritableThenBatchOperations2
) {
1288 delegate_
.SetCanNotWrite();
1290 generator_
.SetShouldSendAck(false);
1291 generator_
.AddControlFrame(QuicFrame(CreateRstStreamFrame()));
1292 EXPECT_TRUE(generator_
.HasQueuedFrames());
1294 delegate_
.SetCanWriteAnything();
1296 generator_
.StartBatchOperations();
1298 // When the first write operation is invoked, the ack frame will be returned.
1299 EXPECT_CALL(delegate_
, PopulateAckFrame(_
));
1303 // All five frames will be flushed out in a single packet
1304 EXPECT_CALL(delegate_
, OnSerializedPacket(_
))
1305 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket
));
1306 EXPECT_CALL(delegate_
, OnSerializedPacket(_
))
1307 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket
));
1310 // Send enough data to exceed one packet
1311 size_t data_len
= kDefaultMaxPacketSize
+ 100;
1312 QuicConsumedData consumed
= generator_
.ConsumeData(
1313 3, CreateData(data_len
), 0, true, MAY_FEC_PROTECT
, nullptr);
1314 EXPECT_EQ(data_len
, consumed
.bytes_consumed
);
1315 EXPECT_TRUE(consumed
.fin_consumed
);
1316 generator_
.AddControlFrame(QuicFrame(CreateGoAwayFrame()));
1318 generator_
.FinishBatchOperations();
1319 EXPECT_FALSE(generator_
.HasQueuedFrames());
1321 // The first packet should have the queued data and part of the stream data.
1322 PacketContents contents
;
1323 contents
.num_ack_frames
= 1;
1324 contents
.num_rst_stream_frames
= 1;
1325 contents
.num_stream_frames
= 1;
1326 CheckPacketContains(contents
, 0);
1328 // The second should have the remainder of the stream data.
1329 PacketContents contents2
;
1330 contents2
.num_goaway_frames
= 1;
1331 contents2
.num_stream_frames
= 1;
1332 CheckPacketContains(contents2
, 1);
1335 TEST_P(QuicPacketGeneratorTest
, TestConnectionIdLength
) {
1336 generator_
.SetConnectionIdLength(0);
1337 EXPECT_EQ(PACKET_0BYTE_CONNECTION_ID
, creator_
->connection_id_length());
1338 generator_
.SetConnectionIdLength(1);
1339 EXPECT_EQ(PACKET_1BYTE_CONNECTION_ID
, creator_
->connection_id_length());
1340 generator_
.SetConnectionIdLength(2);
1341 EXPECT_EQ(PACKET_4BYTE_CONNECTION_ID
, creator_
->connection_id_length());
1342 generator_
.SetConnectionIdLength(3);
1343 EXPECT_EQ(PACKET_4BYTE_CONNECTION_ID
, creator_
->connection_id_length());
1344 generator_
.SetConnectionIdLength(4);
1345 EXPECT_EQ(PACKET_4BYTE_CONNECTION_ID
, creator_
->connection_id_length());
1346 generator_
.SetConnectionIdLength(5);
1347 EXPECT_EQ(PACKET_8BYTE_CONNECTION_ID
, creator_
->connection_id_length());
1348 generator_
.SetConnectionIdLength(6);
1349 EXPECT_EQ(PACKET_8BYTE_CONNECTION_ID
, creator_
->connection_id_length());
1350 generator_
.SetConnectionIdLength(7);
1351 EXPECT_EQ(PACKET_8BYTE_CONNECTION_ID
, creator_
->connection_id_length());
1352 generator_
.SetConnectionIdLength(8);
1353 EXPECT_EQ(PACKET_8BYTE_CONNECTION_ID
, creator_
->connection_id_length());
1354 generator_
.SetConnectionIdLength(9);
1355 EXPECT_EQ(PACKET_8BYTE_CONNECTION_ID
, creator_
->connection_id_length());
1358 // Test whether SetMaxPacketLength() works in the situation when the queue is
1359 // empty, and we send three packets worth of data.
1360 TEST_P(QuicPacketGeneratorTest
, SetMaxPacketLength_Initial
) {
1361 delegate_
.SetCanWriteAnything();
1363 // Send enough data for three packets.
1364 size_t data_len
= 3 * kDefaultMaxPacketSize
+ 1;
1365 size_t packet_len
= kDefaultMaxPacketSize
+ 100;
1366 ASSERT_LE(packet_len
, kMaxPacketSize
);
1367 generator_
.SetMaxPacketLength(packet_len
, /*force=*/false);
1368 EXPECT_EQ(packet_len
, generator_
.GetCurrentMaxPacketLength());
1370 EXPECT_CALL(delegate_
, OnSerializedPacket(_
))
1372 .WillRepeatedly(Invoke(this, &QuicPacketGeneratorTest::SavePacket
));
1373 QuicConsumedData consumed
=
1374 generator_
.ConsumeData(kHeadersStreamId
, CreateData(data_len
),
1376 /*fin=*/true, MAY_FEC_PROTECT
, nullptr);
1377 EXPECT_EQ(data_len
, consumed
.bytes_consumed
);
1378 EXPECT_TRUE(consumed
.fin_consumed
);
1379 EXPECT_FALSE(generator_
.HasQueuedFrames());
1381 // We expect three packets, and first two of them have to be of packet_len
1382 // size. We check multiple packets (instead of just one) because we want to
1383 // ensure that |max_packet_length_| does not get changed incorrectly by the
1384 // generator after first packet is serialized.
1385 ASSERT_EQ(3u, packets_
.size());
1386 EXPECT_EQ(packet_len
, packets_
[0].packet
->length());
1387 EXPECT_EQ(packet_len
, packets_
[1].packet
->length());
1388 CheckAllPacketsHaveSingleStreamFrame();
1391 // Test whether SetMaxPacketLength() works in the situation when we first write
1392 // data, then change packet size, then write data again.
1393 TEST_P(QuicPacketGeneratorTest
, SetMaxPacketLength_Middle
) {
1394 delegate_
.SetCanWriteAnything();
1396 // We send enough data to overflow default packet length, but not the altered
1398 size_t data_len
= kDefaultMaxPacketSize
;
1399 size_t packet_len
= kDefaultMaxPacketSize
+ 100;
1400 ASSERT_LE(packet_len
, kMaxPacketSize
);
1402 // We expect to see three packets in total.
1403 EXPECT_CALL(delegate_
, OnSerializedPacket(_
))
1405 .WillRepeatedly(Invoke(this, &QuicPacketGeneratorTest::SavePacket
));
1407 // Send two packets before packet size change.
1408 QuicConsumedData consumed
=
1409 generator_
.ConsumeData(kHeadersStreamId
, CreateData(data_len
),
1411 /*fin=*/false, MAY_FEC_PROTECT
, nullptr);
1412 EXPECT_EQ(data_len
, consumed
.bytes_consumed
);
1413 EXPECT_FALSE(consumed
.fin_consumed
);
1414 EXPECT_FALSE(generator_
.HasQueuedFrames());
1416 // Make sure we already have two packets.
1417 ASSERT_EQ(2u, packets_
.size());
1419 // Increase packet size.
1420 generator_
.SetMaxPacketLength(packet_len
, /*force=*/false);
1421 EXPECT_EQ(packet_len
, generator_
.GetCurrentMaxPacketLength());
1423 // Send a packet after packet size change.
1424 consumed
= generator_
.ConsumeData(kHeadersStreamId
, CreateData(data_len
),
1426 /*fin=*/true, MAY_FEC_PROTECT
, nullptr);
1427 EXPECT_EQ(data_len
, consumed
.bytes_consumed
);
1428 EXPECT_TRUE(consumed
.fin_consumed
);
1429 EXPECT_FALSE(generator_
.HasQueuedFrames());
1431 // We expect first data chunk to get fragmented, but the second one to fit
1432 // into a single packet.
1433 ASSERT_EQ(3u, packets_
.size());
1434 EXPECT_EQ(kDefaultMaxPacketSize
, packets_
[0].packet
->length());
1435 EXPECT_LE(kDefaultMaxPacketSize
, packets_
[2].packet
->length());
1436 CheckAllPacketsHaveSingleStreamFrame();
1439 // Test whether SetMaxPacketLength() works correctly when we change the packet
1440 // size in the middle of the batched packet.
1441 TEST_P(QuicPacketGeneratorTest
, SetMaxPacketLength_Midpacket
) {
1442 delegate_
.SetCanWriteAnything();
1443 generator_
.StartBatchOperations();
1445 size_t first_write_len
= kDefaultMaxPacketSize
/ 2;
1446 size_t second_write_len
= kDefaultMaxPacketSize
;
1447 size_t packet_len
= kDefaultMaxPacketSize
+ 100;
1448 ASSERT_LE(packet_len
, kMaxPacketSize
);
1450 // First send half of the packet worth of data. We are in the batch mode, so
1451 // should not cause packet serialization.
1452 QuicConsumedData consumed
=
1453 generator_
.ConsumeData(kHeadersStreamId
, CreateData(first_write_len
),
1455 /*fin=*/false, MAY_FEC_PROTECT
, nullptr);
1456 EXPECT_EQ(first_write_len
, consumed
.bytes_consumed
);
1457 EXPECT_FALSE(consumed
.fin_consumed
);
1458 EXPECT_TRUE(generator_
.HasQueuedFrames());
1460 // Make sure we have no packets so far.
1461 ASSERT_EQ(0u, packets_
.size());
1463 // Increase packet size. Ensure it's not immediately enacted.
1464 generator_
.SetMaxPacketLength(packet_len
, /*force=*/false);
1465 EXPECT_EQ(packet_len
, generator_
.GetMaxPacketLength());
1466 EXPECT_EQ(kDefaultMaxPacketSize
, generator_
.GetCurrentMaxPacketLength());
1468 // We expect to see exactly one packet serialized after that, since we are in
1469 // batch mode and we have sent approximately 3/2 of our MTU.
1470 EXPECT_CALL(delegate_
, OnSerializedPacket(_
))
1471 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket
));
1473 // Send a packet worth of data to the same stream. This should trigger
1474 // serialization of other packet.
1476 generator_
.ConsumeData(kHeadersStreamId
, CreateData(second_write_len
),
1477 /*offset=*/2 + first_write_len
,
1478 /*fin=*/true, MAY_FEC_PROTECT
, nullptr);
1479 EXPECT_EQ(second_write_len
, consumed
.bytes_consumed
);
1480 EXPECT_TRUE(consumed
.fin_consumed
);
1481 EXPECT_TRUE(generator_
.HasQueuedFrames());
1483 // We expect the first packet to contain two frames, and to not reflect the
1484 // packet size change.
1485 ASSERT_EQ(1u, packets_
.size());
1486 EXPECT_EQ(kDefaultMaxPacketSize
, packets_
[0].packet
->length());
1488 PacketContents contents
;
1489 contents
.num_stream_frames
= 2;
1490 CheckPacketContains(contents
, 0);
1493 // Test whether SetMaxPacketLength() works correctly when we force the change of
1494 // the packet size in the middle of the batched packet.
1495 TEST_P(QuicPacketGeneratorTest
, SetMaxPacketLength_MidpacketFlush
) {
1496 delegate_
.SetCanWriteAnything();
1497 generator_
.StartBatchOperations();
1499 size_t first_write_len
= kDefaultMaxPacketSize
/ 2;
1500 size_t packet_len
= kDefaultMaxPacketSize
+ 100;
1501 size_t second_write_len
= packet_len
+ 1;
1502 ASSERT_LE(packet_len
, kMaxPacketSize
);
1504 // First send half of the packet worth of data. We are in the batch mode, so
1505 // should not cause packet serialization.
1506 QuicConsumedData consumed
=
1507 generator_
.ConsumeData(kHeadersStreamId
, CreateData(first_write_len
),
1509 /*fin=*/false, MAY_FEC_PROTECT
, nullptr);
1510 EXPECT_EQ(first_write_len
, consumed
.bytes_consumed
);
1511 EXPECT_FALSE(consumed
.fin_consumed
);
1512 EXPECT_TRUE(generator_
.HasQueuedFrames());
1514 // Make sure we have no packets so far.
1515 ASSERT_EQ(0u, packets_
.size());
1517 // Expect a packet to be flushed.
1518 EXPECT_CALL(delegate_
, OnSerializedPacket(_
))
1519 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket
));
1521 // Increase packet size. Ensure it's immediately enacted.
1522 generator_
.SetMaxPacketLength(packet_len
, /*force=*/true);
1523 EXPECT_EQ(packet_len
, generator_
.GetMaxPacketLength());
1524 EXPECT_EQ(packet_len
, generator_
.GetCurrentMaxPacketLength());
1525 EXPECT_FALSE(generator_
.HasQueuedFrames());
1527 // We expect to see exactly one packet serialized after that, because we send
1528 // a value somewhat exceeding new max packet size, and the tail data does not
1529 // get serialized because we are still in the batch mode.
1530 EXPECT_CALL(delegate_
, OnSerializedPacket(_
))
1531 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket
));
1533 // Send a more than a packet worth of data to the same stream. This should
1534 // trigger serialization of one packet, and queue another one.
1536 generator_
.ConsumeData(kHeadersStreamId
, CreateData(second_write_len
),
1537 /*offset=*/2 + first_write_len
,
1538 /*fin=*/true, MAY_FEC_PROTECT
, nullptr);
1539 EXPECT_EQ(second_write_len
, consumed
.bytes_consumed
);
1540 EXPECT_TRUE(consumed
.fin_consumed
);
1541 EXPECT_TRUE(generator_
.HasQueuedFrames());
1543 // We expect the first packet to be underfilled, and the second packet be up
1544 // to the new max packet size.
1545 ASSERT_EQ(2u, packets_
.size());
1546 EXPECT_GT(kDefaultMaxPacketSize
, packets_
[0].packet
->length());
1547 EXPECT_EQ(packet_len
, packets_
[1].packet
->length());
1549 CheckAllPacketsHaveSingleStreamFrame();
1552 // Test sending an MTU probe, without any surrounding data.
1553 TEST_P(QuicPacketGeneratorTest
, GenerateMtuDiscoveryPacket_Simple
) {
1554 delegate_
.SetCanWriteAnything();
1556 const size_t target_mtu
= kDefaultMaxPacketSize
+ 100;
1557 static_assert(target_mtu
< kMaxPacketSize
,
1558 "The MTU probe used by the test exceeds maximum packet size");
1560 EXPECT_CALL(delegate_
, OnSerializedPacket(_
))
1561 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket
));
1563 generator_
.GenerateMtuDiscoveryPacket(target_mtu
, nullptr);
1565 EXPECT_FALSE(generator_
.HasQueuedFrames());
1566 ASSERT_EQ(1u, packets_
.size());
1567 EXPECT_EQ(target_mtu
, packets_
[0].packet
->length());
1569 PacketContents contents
;
1570 contents
.num_mtu_discovery_frames
= 1;
1571 CheckPacketContains(contents
, 0);
1574 // Test sending an MTU probe. Surround it with data, to ensure that it resets
1575 // the MTU to the value before the probe was sent.
1576 TEST_P(QuicPacketGeneratorTest
, GenerateMtuDiscoveryPacket_SurroundedByData
) {
1577 delegate_
.SetCanWriteAnything();
1579 const size_t target_mtu
= kDefaultMaxPacketSize
+ 100;
1580 static_assert(target_mtu
< kMaxPacketSize
,
1581 "The MTU probe used by the test exceeds maximum packet size");
1583 // Send enough data so it would always cause two packets to be sent.
1584 const size_t data_len
= target_mtu
+ 1;
1586 // Send a total of five packets: two packets before the probe, the probe
1587 // itself, and two packets after the probe.
1588 EXPECT_CALL(delegate_
, OnSerializedPacket(_
))
1590 .WillRepeatedly(Invoke(this, &QuicPacketGeneratorTest::SavePacket
));
1592 // Send data before the MTU probe.
1593 QuicConsumedData consumed
=
1594 generator_
.ConsumeData(kHeadersStreamId
, CreateData(data_len
),
1596 /*fin=*/false, MAY_FEC_PROTECT
, nullptr);
1597 EXPECT_EQ(data_len
, consumed
.bytes_consumed
);
1598 EXPECT_FALSE(consumed
.fin_consumed
);
1599 EXPECT_FALSE(generator_
.HasQueuedFrames());
1601 // Send the MTU probe.
1602 generator_
.GenerateMtuDiscoveryPacket(target_mtu
, nullptr);
1603 EXPECT_FALSE(generator_
.HasQueuedFrames());
1605 // Send data after the MTU probe.
1606 consumed
= generator_
.ConsumeData(kHeadersStreamId
, CreateData(data_len
),
1607 /*offset=*/2 + data_len
,
1608 /*fin=*/true, MAY_FEC_PROTECT
, nullptr);
1609 EXPECT_EQ(data_len
, consumed
.bytes_consumed
);
1610 EXPECT_TRUE(consumed
.fin_consumed
);
1611 EXPECT_FALSE(generator_
.HasQueuedFrames());
1613 ASSERT_EQ(5u, packets_
.size());
1614 EXPECT_EQ(kDefaultMaxPacketSize
, packets_
[0].packet
->length());
1615 EXPECT_EQ(target_mtu
, packets_
[2].packet
->length());
1616 EXPECT_EQ(kDefaultMaxPacketSize
, packets_
[3].packet
->length());
1618 PacketContents probe_contents
;
1619 probe_contents
.num_mtu_discovery_frames
= 1;
1621 CheckPacketHasSingleStreamFrame(0);
1622 CheckPacketHasSingleStreamFrame(1);
1623 CheckPacketContains(probe_contents
, 2);
1624 CheckPacketHasSingleStreamFrame(3);
1625 CheckPacketHasSingleStreamFrame(4);
1628 TEST_P(QuicPacketGeneratorTest
, DontCrashOnInvalidStopWaiting
) {
1629 // Test added to ensure the generator does not crash when an invalid frame is
1630 // added. Because this is an indication of internal programming errors,
1631 // DFATALs are expected.
1632 // A 1 byte sequence number length can't encode a gap of 1000.
1633 QuicPacketCreatorPeer::SetSequenceNumber(creator_
, 1000);
1635 delegate_
.SetCanNotWrite();
1636 generator_
.SetShouldSendAck(true);
1637 delegate_
.SetCanWriteAnything();
1638 generator_
.StartBatchOperations();
1640 // Set up frames to write into the creator when control frames are written.
1641 EXPECT_CALL(delegate_
, PopulateAckFrame(_
));
1642 EXPECT_CALL(delegate_
, PopulateStopWaitingFrame(_
));
1643 // Generator should have queued control frames, and creator should be empty.
1644 EXPECT_TRUE(generator_
.HasQueuedFrames());
1645 EXPECT_FALSE(creator_
->HasPendingFrames());
1647 // This will not serialize any packets, because of the invalid frame.
1648 EXPECT_CALL(delegate_
,
1649 CloseConnection(QUIC_FAILED_TO_SERIALIZE_PACKET
, false));
1650 EXPECT_DFATAL(generator_
.FinishBatchOperations(),
1651 "sequence_number_length 1 is too small "
1652 "for least_unacked_delta: 1001");