Delete unused downloads page asset.
[chromium-blink-merge.git] / net / quic / quic_packet_generator_test.cc
blob7d99e158f08694dc4773e6d398ed29643b801343
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"
7 #include <string>
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;
24 using std::string;
25 using std::vector;
26 using testing::InSequence;
27 using testing::Return;
28 using testing::StrictMock;
29 using testing::_;
31 namespace net {
32 namespace test {
33 namespace {
35 const int64 kMinFecTimeoutMs = 5u;
37 static const FecSendPolicy kFecSendPolicyList[] = {
38 FEC_ANY_TRIGGER,
39 FEC_ALARM_TRIGGER,
42 class MockDelegate : public QuicPacketGenerator::DelegateInterface {
43 public:
44 MockDelegate() {}
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));
77 private:
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 {
85 PacketContents()
86 : num_ack_frames(0),
87 num_connection_close_frames(0),
88 num_goaway_frames(0),
89 num_rst_stream_frames(0),
90 num_stop_waiting_frames(0),
91 num_stream_frames(0),
92 num_ping_frames(0),
93 num_mtu_discovery_frames(0),
94 fec_group(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;
108 } // namespace
110 class QuicPacketGeneratorTest : public ::testing::TestWithParam<FecSendPolicy> {
111 public:
112 QuicPacketGeneratorTest()
113 : framer_(QuicSupportedVersions(),
114 QuicTime::Zero(),
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();
135 protected:
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;
152 size_t num_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);
158 } else {
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, QuicPacketNumber fec_group) {
203 ASSERT_GT(packets_.size(), packet_index);
204 const SerializedPacket& packet = packets_[packet_index];
205 ASSERT_TRUE(packet.retransmittable_frames == nullptr);
206 ASSERT_TRUE(packet.packet != nullptr);
207 ASSERT_TRUE(simple_framer_.ProcessPacket(*packet.packet));
208 EXPECT_TRUE(simple_framer_.header().fec_flag);
209 EXPECT_EQ(fec_group, simple_framer_.fec_data().fec_group);
212 QuicIOVector CreateData(size_t len) {
213 data_array_.reset(new char[len]);
214 memset(data_array_.get(), '?', len);
215 iov_.iov_base = data_array_.get();
216 iov_.iov_len = len;
217 return QuicIOVector(&iov_, 1, len);
220 QuicIOVector MakeIOVector(StringPiece s) {
221 return ::net::MakeIOVector(s, &iov_);
224 QuicFramer framer_;
225 MockRandom random_;
226 StrictMock<MockDelegate> delegate_;
227 QuicPacketGenerator generator_;
228 QuicPacketCreator* creator_;
229 SimpleQuicFramer simple_framer_;
230 vector<SerializedPacket> packets_;
232 private:
233 scoped_ptr<char[]> data_array_;
234 struct iovec iov_;
237 class MockDebugDelegate : public QuicPacketGenerator::DebugDelegate {
238 public:
239 MOCK_METHOD1(OnFrameAddedToPacket,
240 void(const QuicFrame&));
243 // Run all end to end tests with all supported FEC send polocies.
244 INSTANTIATE_TEST_CASE_P(FecSendPolicy,
245 QuicPacketGeneratorTest,
246 ::testing::ValuesIn(kFecSendPolicyList));
248 TEST_P(QuicPacketGeneratorTest, ShouldSendAck_NotWritable) {
249 delegate_.SetCanNotWrite();
251 generator_.SetShouldSendAck(false);
252 EXPECT_TRUE(generator_.HasQueuedFrames());
255 TEST_P(QuicPacketGeneratorTest, ShouldSendAck_WritableAndShouldNotFlush) {
256 StrictMock<MockDebugDelegate> debug_delegate;
258 generator_.set_debug_delegate(&debug_delegate);
259 delegate_.SetCanWriteOnlyNonRetransmittable();
260 generator_.StartBatchOperations();
262 EXPECT_CALL(delegate_, PopulateAckFrame(_));
263 EXPECT_CALL(debug_delegate, OnFrameAddedToPacket(_)).Times(1);
265 generator_.SetShouldSendAck(false);
266 EXPECT_TRUE(generator_.HasQueuedFrames());
269 TEST_P(QuicPacketGeneratorTest, ShouldSendAck_WritableAndShouldFlush) {
270 delegate_.SetCanWriteOnlyNonRetransmittable();
272 EXPECT_CALL(delegate_, PopulateAckFrame(_));
273 EXPECT_CALL(delegate_, OnSerializedPacket(_))
274 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
276 generator_.SetShouldSendAck(false);
277 EXPECT_FALSE(generator_.HasQueuedFrames());
279 PacketContents contents;
280 contents.num_ack_frames = 1;
281 CheckPacketContains(contents, 0);
284 TEST_P(QuicPacketGeneratorTest, ShouldSendAck_MultipleCalls) {
285 // Make sure that calling SetShouldSendAck multiple times does not result in a
286 // crash. Previously this would result in multiple QuicFrames queued in the
287 // packet generator, with all but the last with internal pointers to freed
288 // memory.
289 delegate_.SetCanWriteAnything();
291 // Only one AckFrame should be created.
292 EXPECT_CALL(delegate_, PopulateAckFrame(_)).Times(1);
293 EXPECT_CALL(delegate_, OnSerializedPacket(_))
294 .Times(1)
295 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
297 generator_.StartBatchOperations();
298 generator_.SetShouldSendAck(false);
299 generator_.SetShouldSendAck(false);
300 generator_.FinishBatchOperations();
303 TEST_P(QuicPacketGeneratorTest, AddControlFrame_NotWritable) {
304 delegate_.SetCanNotWrite();
306 generator_.AddControlFrame(QuicFrame(CreateRstStreamFrame()));
307 EXPECT_TRUE(generator_.HasQueuedFrames());
310 TEST_P(QuicPacketGeneratorTest, AddControlFrame_OnlyAckWritable) {
311 delegate_.SetCanWriteOnlyNonRetransmittable();
313 generator_.AddControlFrame(QuicFrame(CreateRstStreamFrame()));
314 EXPECT_TRUE(generator_.HasQueuedFrames());
317 TEST_P(QuicPacketGeneratorTest, AddControlFrame_WritableAndShouldNotFlush) {
318 delegate_.SetCanWriteAnything();
319 generator_.StartBatchOperations();
321 generator_.AddControlFrame(QuicFrame(CreateRstStreamFrame()));
322 EXPECT_TRUE(generator_.HasQueuedFrames());
325 TEST_P(QuicPacketGeneratorTest, AddControlFrame_NotWritableBatchThenFlush) {
326 delegate_.SetCanNotWrite();
327 generator_.StartBatchOperations();
329 generator_.AddControlFrame(QuicFrame(CreateRstStreamFrame()));
330 EXPECT_TRUE(generator_.HasQueuedFrames());
331 generator_.FinishBatchOperations();
332 EXPECT_TRUE(generator_.HasQueuedFrames());
334 EXPECT_CALL(delegate_, OnSerializedPacket(_))
335 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
336 generator_.FlushAllQueuedFrames();
337 EXPECT_FALSE(generator_.HasQueuedFrames());
339 PacketContents contents;
340 contents.num_rst_stream_frames = 1;
341 CheckPacketContains(contents, 0);
344 TEST_P(QuicPacketGeneratorTest, AddControlFrame_WritableAndShouldFlush) {
345 delegate_.SetCanWriteAnything();
347 EXPECT_CALL(delegate_, OnSerializedPacket(_))
348 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
350 generator_.AddControlFrame(QuicFrame(CreateRstStreamFrame()));
351 EXPECT_FALSE(generator_.HasQueuedFrames());
353 PacketContents contents;
354 contents.num_rst_stream_frames = 1;
355 CheckPacketContains(contents, 0);
358 TEST_P(QuicPacketGeneratorTest, ConsumeData_NotWritable) {
359 delegate_.SetCanNotWrite();
361 QuicConsumedData consumed = generator_.ConsumeData(
362 kHeadersStreamId, MakeIOVector("foo"), 2, true, MAY_FEC_PROTECT, nullptr);
363 EXPECT_EQ(0u, consumed.bytes_consumed);
364 EXPECT_FALSE(consumed.fin_consumed);
365 EXPECT_FALSE(generator_.HasQueuedFrames());
368 TEST_P(QuicPacketGeneratorTest, ConsumeData_WritableAndShouldNotFlush) {
369 delegate_.SetCanWriteAnything();
370 generator_.StartBatchOperations();
372 QuicConsumedData consumed = generator_.ConsumeData(
373 kHeadersStreamId, MakeIOVector("foo"), 2, true, MAY_FEC_PROTECT, nullptr);
374 EXPECT_EQ(3u, consumed.bytes_consumed);
375 EXPECT_TRUE(consumed.fin_consumed);
376 EXPECT_TRUE(generator_.HasQueuedFrames());
379 TEST_P(QuicPacketGeneratorTest, ConsumeData_WritableAndShouldFlush) {
380 delegate_.SetCanWriteAnything();
382 EXPECT_CALL(delegate_, OnSerializedPacket(_))
383 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
384 QuicConsumedData consumed = generator_.ConsumeData(
385 kHeadersStreamId, MakeIOVector("foo"), 2, true, MAY_FEC_PROTECT, nullptr);
386 EXPECT_EQ(3u, consumed.bytes_consumed);
387 EXPECT_TRUE(consumed.fin_consumed);
388 EXPECT_FALSE(generator_.HasQueuedFrames());
390 PacketContents contents;
391 contents.num_stream_frames = 1;
392 CheckPacketContains(contents, 0);
395 // Test the behavior of ConsumeData when the data consumed is for the crypto
396 // handshake stream. Ensure that the packet is always sent and padded even if
397 // the generator operates in batch mode.
398 TEST_P(QuicPacketGeneratorTest, ConsumeData_Handshake) {
399 delegate_.SetCanWriteAnything();
400 generator_.StartBatchOperations();
402 EXPECT_CALL(delegate_, OnSerializedPacket(_))
403 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
404 QuicConsumedData consumed = generator_.ConsumeData(
405 kCryptoStreamId, MakeIOVector("foo"), 0, false, MAY_FEC_PROTECT, nullptr);
406 EXPECT_EQ(3u, consumed.bytes_consumed);
407 EXPECT_FALSE(generator_.HasQueuedFrames());
409 PacketContents contents;
410 contents.num_stream_frames = 1;
411 CheckPacketContains(contents, 0);
413 ASSERT_EQ(1u, packets_.size());
414 ASSERT_EQ(kDefaultMaxPacketSize, generator_.GetMaxPacketLength());
415 EXPECT_EQ(kDefaultMaxPacketSize, packets_[0].packet->length());
418 TEST_P(QuicPacketGeneratorTest, ConsumeData_EmptyData) {
419 EXPECT_DFATAL(generator_.ConsumeData(kHeadersStreamId, MakeIOVector(""), 0,
420 false, MAY_FEC_PROTECT, nullptr),
421 "Attempt to consume empty data without FIN.");
424 TEST_P(QuicPacketGeneratorTest,
425 ConsumeDataMultipleTimes_WritableAndShouldNotFlush) {
426 delegate_.SetCanWriteAnything();
427 generator_.StartBatchOperations();
429 generator_.ConsumeData(kHeadersStreamId, MakeIOVector("foo"), 2, true,
430 MAY_FEC_PROTECT, nullptr);
431 QuicConsumedData consumed = generator_.ConsumeData(
432 3, MakeIOVector("quux"), 7, false, MAY_FEC_PROTECT, nullptr);
433 EXPECT_EQ(4u, consumed.bytes_consumed);
434 EXPECT_FALSE(consumed.fin_consumed);
435 EXPECT_TRUE(generator_.HasQueuedFrames());
438 TEST_P(QuicPacketGeneratorTest, ConsumeData_BatchOperations) {
439 delegate_.SetCanWriteAnything();
440 generator_.StartBatchOperations();
442 generator_.ConsumeData(kHeadersStreamId, MakeIOVector("foo"), 2, true,
443 MAY_FEC_PROTECT, nullptr);
444 QuicConsumedData consumed = generator_.ConsumeData(
445 3, MakeIOVector("quux"), 7, false, MAY_FEC_PROTECT, nullptr);
446 EXPECT_EQ(4u, consumed.bytes_consumed);
447 EXPECT_FALSE(consumed.fin_consumed);
448 EXPECT_TRUE(generator_.HasQueuedFrames());
450 // Now both frames will be flushed out.
451 EXPECT_CALL(delegate_, OnSerializedPacket(_))
452 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
453 generator_.FinishBatchOperations();
454 EXPECT_FALSE(generator_.HasQueuedFrames());
456 PacketContents contents;
457 contents.num_stream_frames = 2;
458 CheckPacketContains(contents, 0);
461 TEST_P(QuicPacketGeneratorTest, ConsumeDataFecOnMaxGroupSize) {
462 delegate_.SetCanWriteAnything();
464 // Send FEC every two packets.
465 creator_->set_max_packets_per_fec_group(2);
468 InSequence dummy;
469 EXPECT_CALL(delegate_, OnSerializedPacket(_))
470 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
471 EXPECT_CALL(delegate_, OnSerializedPacket(_))
472 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
473 if (generator_.fec_send_policy() == FEC_ALARM_TRIGGER) {
474 // FEC packet is not sent when send policy is FEC_ALARM_TRIGGER, but FEC
475 // group is closed.
476 EXPECT_CALL(delegate_, OnResetFecGroup()).Times(1);
477 } else {
478 EXPECT_CALL(delegate_, OnSerializedPacket(_))
479 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
481 EXPECT_CALL(delegate_, OnSerializedPacket(_))
482 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
485 // Send enough data to create 3 packets: two full and one partial. Send with
486 // MUST_FEC_PROTECT flag.
487 size_t data_len = 2 * kDefaultMaxPacketSize + 100;
488 QuicConsumedData consumed = generator_.ConsumeData(
489 3, CreateData(data_len), 0, true, MUST_FEC_PROTECT, nullptr);
490 EXPECT_EQ(data_len, consumed.bytes_consumed);
491 EXPECT_TRUE(consumed.fin_consumed);
492 EXPECT_FALSE(generator_.HasQueuedFrames());
494 CheckPacketHasSingleStreamFrame(0);
495 CheckPacketHasSingleStreamFrame(1);
496 if (generator_.fec_send_policy() == FEC_ALARM_TRIGGER) {
497 // FEC packet is not sent when send policy is FEC_ALARM_TRIGGER.
498 CheckPacketHasSingleStreamFrame(2);
499 } else {
500 CheckPacketIsFec(2, 1);
501 CheckPacketHasSingleStreamFrame(3);
503 EXPECT_TRUE(creator_->IsFecProtected());
505 // If FEC send policy is FEC_ANY_TRIGGER, then the FEC packet under
506 // construction will be sent when one more packet is sent (since FEC group
507 // size is 2), or when OnFecTimeout is called. Send more data with
508 // MAY_FEC_PROTECT. This packet should also be protected, and FEC packet is
509 // sent since FEC group size is reached.
511 // If FEC send policy is FEC_ALARM_TRIGGER, FEC group is closed when the group
512 // size is reached. FEC packet is not sent.
514 InSequence dummy;
515 EXPECT_CALL(delegate_, OnSerializedPacket(_))
516 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
517 if (generator_.fec_send_policy() == FEC_ALARM_TRIGGER) {
518 EXPECT_CALL(delegate_, OnResetFecGroup()).Times(1);
519 } else {
520 EXPECT_CALL(delegate_, OnSerializedPacket(_))
521 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
524 consumed = generator_.ConsumeData(5, CreateData(1u), 0, true, MAY_FEC_PROTECT,
525 nullptr);
526 EXPECT_EQ(1u, consumed.bytes_consumed);
527 if (generator_.fec_send_policy() == FEC_ALARM_TRIGGER) {
528 CheckPacketHasSingleStreamFrame(3);
529 } else {
530 CheckPacketHasSingleStreamFrame(4);
531 CheckPacketIsFec(5, 4);
533 EXPECT_FALSE(creator_->IsFecProtected());
536 TEST_P(QuicPacketGeneratorTest, ConsumeDataSendsFecOnTimeout) {
537 delegate_.SetCanWriteAnything();
538 creator_->set_max_packets_per_fec_group(1000);
540 // Send data with MUST_FEC_PROTECT flag. No FEC packet is emitted, but the
541 // creator FEC protects all data.
542 EXPECT_CALL(delegate_, OnSerializedPacket(_))
543 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
544 QuicConsumedData consumed = generator_.ConsumeData(3, CreateData(1u), 0, true,
545 MUST_FEC_PROTECT, nullptr);
546 EXPECT_EQ(1u, consumed.bytes_consumed);
547 EXPECT_TRUE(consumed.fin_consumed);
548 CheckPacketHasSingleStreamFrame(0);
549 EXPECT_TRUE(creator_->IsFecProtected());
551 // Send more data with MAY_FEC_PROTECT. This packet should also be protected,
552 // and FEC packet is not yet sent.
553 EXPECT_CALL(delegate_, OnSerializedPacket(_))
554 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
555 consumed = generator_.ConsumeData(5, CreateData(1u), 0, true, MAY_FEC_PROTECT,
556 nullptr);
557 EXPECT_EQ(1u, consumed.bytes_consumed);
558 CheckPacketHasSingleStreamFrame(1);
559 EXPECT_TRUE(creator_->IsFecProtected());
561 // Calling OnFecTimeout should cause the FEC packet to be emitted.
562 EXPECT_CALL(delegate_, OnSerializedPacket(_))
563 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
564 generator_.OnFecTimeout();
565 CheckPacketIsFec(2, 1);
566 EXPECT_FALSE(creator_->IsFecProtected());
568 // Subsequent data is protected under the next FEC group. Send enough data to
569 // create 2 more packets: one full and one partial.
571 InSequence dummy;
572 EXPECT_CALL(delegate_, OnSerializedPacket(_))
573 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
574 EXPECT_CALL(delegate_, OnSerializedPacket(_))
575 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
577 size_t data_len = kDefaultMaxPacketSize + 1;
578 consumed = generator_.ConsumeData(7, CreateData(data_len), 0, true,
579 MUST_FEC_PROTECT, nullptr);
580 EXPECT_EQ(data_len, consumed.bytes_consumed);
581 EXPECT_TRUE(consumed.fin_consumed);
582 CheckPacketHasSingleStreamFrame(3);
583 CheckPacketHasSingleStreamFrame(4);
584 EXPECT_TRUE(creator_->IsFecProtected());
586 // Calling OnFecTimeout should cause the FEC packet to be emitted.
587 EXPECT_CALL(delegate_, OnSerializedPacket(_))
588 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
589 generator_.OnFecTimeout();
590 CheckPacketIsFec(5, 4);
591 EXPECT_FALSE(creator_->IsFecProtected());
594 TEST_P(QuicPacketGeneratorTest, GetFecTimeoutFiniteOnlyOnFirstPacketInGroup) {
595 delegate_.SetCanWriteAnything();
596 creator_->set_max_packets_per_fec_group(6);
598 // Send enough data to create 2 packets: one full and one partial. Send with
599 // MUST_FEC_PROTECT flag. No FEC packet is emitted yet, but the creator FEC
600 // protects all data.
602 InSequence dummy;
603 EXPECT_CALL(delegate_, OnSerializedPacket(_))
604 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
605 EXPECT_CALL(delegate_, OnSerializedPacket(_))
606 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
608 size_t data_len = 1 * kDefaultMaxPacketSize + 100;
609 QuicConsumedData consumed = generator_.ConsumeData(
610 3, CreateData(data_len), 0, true, MUST_FEC_PROTECT, nullptr);
611 EXPECT_EQ(data_len, consumed.bytes_consumed);
612 EXPECT_TRUE(consumed.fin_consumed);
613 EXPECT_FALSE(generator_.HasQueuedFrames());
614 CheckPacketHasSingleStreamFrame(0);
615 CheckPacketHasSingleStreamFrame(1);
616 EXPECT_TRUE(creator_->IsFecProtected());
618 // GetFecTimeout returns finite timeout only for first packet in group.
619 EXPECT_EQ(QuicTime::Delta::FromMilliseconds(kMinFecTimeoutMs),
620 generator_.GetFecTimeout(/*packet_number=*/1u));
621 EXPECT_EQ(QuicTime::Delta::Infinite(),
622 generator_.GetFecTimeout(/*packet_number=*/2u));
624 // Send more data with MAY_FEC_PROTECT. This packet should also be protected,
625 // and FEC packet is not yet sent.
626 EXPECT_CALL(delegate_, OnSerializedPacket(_))
627 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
628 consumed = generator_.ConsumeData(5, CreateData(1u), 0, true, MAY_FEC_PROTECT,
629 nullptr);
630 CheckPacketHasSingleStreamFrame(2);
631 EXPECT_TRUE(creator_->IsFecProtected());
633 // GetFecTimeout returns finite timeout only for first packet in group.
634 EXPECT_EQ(QuicTime::Delta::Infinite(),
635 generator_.GetFecTimeout(/*packet_number=*/3u));
637 // Calling OnFecTimeout should cause the FEC packet to be emitted.
638 EXPECT_CALL(delegate_, OnSerializedPacket(_))
639 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
640 generator_.OnFecTimeout();
641 CheckPacketIsFec(3, /*fec_group=*/1u);
642 EXPECT_FALSE(creator_->IsFecProtected());
644 // Subsequent data is protected under the next FEC group. Send enough data to
645 // create 2 more packets: one full and one partial.
647 InSequence dummy;
648 EXPECT_CALL(delegate_, OnSerializedPacket(_))
649 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
650 EXPECT_CALL(delegate_, OnSerializedPacket(_))
651 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
653 data_len = kDefaultMaxPacketSize + 1u;
654 consumed = generator_.ConsumeData(7, CreateData(data_len), 0, true,
655 MUST_FEC_PROTECT, nullptr);
656 EXPECT_EQ(data_len, consumed.bytes_consumed);
657 EXPECT_TRUE(consumed.fin_consumed);
658 CheckPacketHasSingleStreamFrame(4);
659 CheckPacketHasSingleStreamFrame(5);
660 EXPECT_TRUE(creator_->IsFecProtected());
662 // GetFecTimeout returns finite timeout for first packet in the new group.
663 EXPECT_EQ(QuicTime::Delta::FromMilliseconds(kMinFecTimeoutMs),
664 generator_.GetFecTimeout(/*packet_number=*/5u));
665 EXPECT_EQ(QuicTime::Delta::Infinite(),
666 generator_.GetFecTimeout(/*packet_number=*/6u));
668 // Calling OnFecTimeout should cause the FEC packet to be emitted.
669 EXPECT_CALL(delegate_, OnSerializedPacket(_))
670 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
671 generator_.OnFecTimeout();
672 CheckPacketIsFec(6, /*fec_group=*/5u);
673 EXPECT_FALSE(creator_->IsFecProtected());
675 // Send more data with MAY_FEC_PROTECT. No FEC protection, so GetFecTimeout
676 // returns infinite.
677 EXPECT_CALL(delegate_, OnSerializedPacket(_))
678 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
679 consumed = generator_.ConsumeData(9, CreateData(1u), 0, true, MAY_FEC_PROTECT,
680 nullptr);
681 CheckPacketHasSingleStreamFrame(7);
682 EXPECT_FALSE(creator_->IsFecProtected());
683 EXPECT_EQ(QuicTime::Delta::Infinite(),
684 generator_.GetFecTimeout(/*packet_number=*/8u));
687 TEST_P(QuicPacketGeneratorTest, ConsumeData_FramesPreviouslyQueued) {
688 // Set the packet size be enough for two stream frames with 0 stream offset,
689 // but not enough for a stream frame of 0 offset and one with non-zero offset.
690 size_t length =
691 NullEncrypter().GetCiphertextSize(0) +
692 GetPacketHeaderSize(
693 creator_->connection_id_length(), true,
694 QuicPacketCreatorPeer::NextPacketNumberLength(creator_),
695 NOT_IN_FEC_GROUP) +
696 // Add an extra 3 bytes for the payload and 1 byte so BytesFree is larger
697 // than the GetMinStreamFrameSize.
698 QuicFramer::GetMinStreamFrameSize(1, 0, false, NOT_IN_FEC_GROUP) + 3 +
699 QuicFramer::GetMinStreamFrameSize(1, 0, true, NOT_IN_FEC_GROUP) + 1;
700 generator_.SetMaxPacketLength(length, /*force=*/false);
701 delegate_.SetCanWriteAnything();
703 InSequence dummy;
704 EXPECT_CALL(delegate_, OnSerializedPacket(_))
705 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
706 EXPECT_CALL(delegate_, OnSerializedPacket(_))
707 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
709 generator_.StartBatchOperations();
710 // Queue enough data to prevent a stream frame with a non-zero offset from
711 // fitting.
712 QuicConsumedData consumed =
713 generator_.ConsumeData(kHeadersStreamId, MakeIOVector("foo"), 0, false,
714 MAY_FEC_PROTECT, nullptr);
715 EXPECT_EQ(3u, consumed.bytes_consumed);
716 EXPECT_FALSE(consumed.fin_consumed);
717 EXPECT_TRUE(generator_.HasQueuedFrames());
719 // This frame will not fit with the existing frame, causing the queued frame
720 // to be serialized, and it will not fit with another frame like it, so it is
721 // serialized by itself.
722 consumed = generator_.ConsumeData(kHeadersStreamId, MakeIOVector("bar"), 3,
723 true, MAY_FEC_PROTECT, nullptr);
724 EXPECT_EQ(3u, consumed.bytes_consumed);
725 EXPECT_TRUE(consumed.fin_consumed);
726 EXPECT_FALSE(generator_.HasQueuedFrames());
728 PacketContents contents;
729 contents.num_stream_frames = 1;
730 CheckPacketContains(contents, 0);
731 CheckPacketContains(contents, 1);
734 TEST_P(QuicPacketGeneratorTest, NoFecPacketSentWhenBatchEnds) {
735 delegate_.SetCanWriteAnything();
736 creator_->set_max_packets_per_fec_group(6);
738 generator_.StartBatchOperations();
740 generator_.ConsumeData(3, MakeIOVector("foo"), 2, true, MUST_FEC_PROTECT,
741 nullptr);
742 QuicConsumedData consumed = generator_.ConsumeData(
743 5, MakeIOVector("quux"), 7, false, MUST_FEC_PROTECT, nullptr);
744 EXPECT_EQ(4u, consumed.bytes_consumed);
745 EXPECT_FALSE(consumed.fin_consumed);
746 EXPECT_TRUE(generator_.HasQueuedFrames());
748 // Now both frames will be flushed out, but FEC packet is not yet sent.
749 EXPECT_CALL(delegate_, OnSerializedPacket(_))
750 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
751 generator_.FinishBatchOperations();
752 EXPECT_FALSE(generator_.HasQueuedFrames());
754 PacketContents contents;
755 contents.num_stream_frames = 2u;
756 contents.fec_group = 1u;
757 CheckPacketContains(contents, 0);
759 // Forcing FEC timeout causes FEC packet to be emitted.
760 EXPECT_CALL(delegate_, OnSerializedPacket(_))
761 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
762 generator_.OnFecTimeout();
763 CheckPacketIsFec(1, /*fec_group=*/1u);
766 TEST_P(QuicPacketGeneratorTest, FecTimeoutOnRttChange) {
767 EXPECT_EQ(QuicTime::Delta::Zero(),
768 QuicPacketGeneratorPeer::GetFecTimeout(&generator_));
769 generator_.OnRttChange(QuicTime::Delta::FromMilliseconds(300));
770 EXPECT_EQ(QuicTime::Delta::FromMilliseconds(150),
771 QuicPacketGeneratorPeer::GetFecTimeout(&generator_));
774 TEST_P(QuicPacketGeneratorTest, FecGroupSizeOnCongestionWindowChange) {
775 delegate_.SetCanWriteAnything();
776 creator_->set_max_packets_per_fec_group(50);
777 EXPECT_EQ(50u, creator_->max_packets_per_fec_group());
778 EXPECT_FALSE(creator_->IsFecGroupOpen());
780 // On reduced cwnd.
781 generator_.OnCongestionWindowChange(7);
782 EXPECT_EQ(3u, creator_->max_packets_per_fec_group());
784 // On increased cwnd.
785 generator_.OnCongestionWindowChange(100);
786 EXPECT_EQ(50u, creator_->max_packets_per_fec_group());
788 // On collapsed cwnd.
789 generator_.OnCongestionWindowChange(1);
790 EXPECT_EQ(2u, creator_->max_packets_per_fec_group());
793 TEST_P(QuicPacketGeneratorTest, FecGroupSizeChangeWithOpenGroup) {
794 delegate_.SetCanWriteAnything();
795 generator_.StartBatchOperations();
796 creator_->set_max_packets_per_fec_group(50);
797 EXPECT_EQ(50u, creator_->max_packets_per_fec_group());
798 EXPECT_FALSE(creator_->IsFecGroupOpen());
800 // Send enough data to create 4 packets with MUST_FEC_PROTECT flag. 3 packets
801 // are sent, one is queued in the creator.
803 InSequence dummy;
804 EXPECT_CALL(delegate_, OnSerializedPacket(_))
805 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
806 EXPECT_CALL(delegate_, OnSerializedPacket(_))
807 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
808 EXPECT_CALL(delegate_, OnSerializedPacket(_))
809 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
811 size_t data_len = 3 * kDefaultMaxPacketSize + 1;
812 QuicConsumedData consumed = generator_.ConsumeData(
813 7, CreateData(data_len), 0, true, MUST_FEC_PROTECT, nullptr);
814 EXPECT_EQ(data_len, consumed.bytes_consumed);
815 EXPECT_TRUE(creator_->IsFecGroupOpen());
817 // Change FEC groupsize.
818 generator_.OnCongestionWindowChange(2);
819 EXPECT_EQ(2u, creator_->max_packets_per_fec_group());
821 // If FEC send policy is FEC_ANY_TRIGGER, then send enough data to trigger one
822 // unprotected data packet, causing the FEC packet to also be sent.
824 // If FEC send policy is FEC_ALARM_TRIGGER, FEC group is closed and FEC packet
825 // is not sent.
827 InSequence dummy;
828 EXPECT_CALL(delegate_, OnSerializedPacket(_))
829 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
830 if (generator_.fec_send_policy() == FEC_ALARM_TRIGGER) {
831 EXPECT_CALL(delegate_, OnResetFecGroup()).Times(1);
832 } else {
833 EXPECT_CALL(delegate_, OnSerializedPacket(_))
834 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
837 consumed = generator_.ConsumeData(7, CreateData(kDefaultMaxPacketSize), 0,
838 true, MAY_FEC_PROTECT, nullptr);
839 EXPECT_EQ(kDefaultMaxPacketSize, consumed.bytes_consumed);
840 if (generator_.fec_send_policy() == FEC_ANY_TRIGGER) {
841 // Verify that one FEC packet was sent.
842 CheckPacketIsFec(4, /*fec_group=*/1u);
844 EXPECT_FALSE(creator_->IsFecGroupOpen());
845 EXPECT_FALSE(creator_->IsFecProtected());
848 TEST_P(QuicPacketGeneratorTest, SwitchFecOnOff) {
849 delegate_.SetCanWriteAnything();
850 creator_->set_max_packets_per_fec_group(2);
851 EXPECT_FALSE(creator_->IsFecProtected());
853 // Send one unprotected data packet.
854 EXPECT_CALL(delegate_, OnSerializedPacket(_))
855 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
856 QuicConsumedData consumed = generator_.ConsumeData(5, CreateData(1u), 0, true,
857 MAY_FEC_PROTECT, nullptr);
858 EXPECT_EQ(1u, consumed.bytes_consumed);
859 EXPECT_FALSE(generator_.HasQueuedFrames());
860 EXPECT_FALSE(creator_->IsFecProtected());
861 // Verify that one data packet was sent.
862 PacketContents contents;
863 contents.num_stream_frames = 1;
864 CheckPacketContains(contents, 0);
867 InSequence dummy;
868 EXPECT_CALL(delegate_, OnSerializedPacket(_))
869 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
870 EXPECT_CALL(delegate_, OnSerializedPacket(_))
871 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
872 if (generator_.fec_send_policy() == FEC_ALARM_TRIGGER) {
873 // If FEC send policy is FEC_ALARM_TRIGGER, FEC group is closed.
874 EXPECT_CALL(delegate_, OnResetFecGroup()).Times(1);
875 } else {
876 EXPECT_CALL(delegate_, OnSerializedPacket(_))
877 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
879 EXPECT_CALL(delegate_, OnSerializedPacket(_))
880 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
882 // Send enough data to create 3 packets with MUST_FEC_PROTECT flag.
883 size_t data_len = 2 * kDefaultMaxPacketSize + 100;
884 consumed = generator_.ConsumeData(7, CreateData(data_len), 0, true,
885 MUST_FEC_PROTECT, nullptr);
886 EXPECT_EQ(data_len, consumed.bytes_consumed);
887 EXPECT_FALSE(generator_.HasQueuedFrames());
889 // If FEC send policy is FEC_ANY_TRIGGER, verify that packets sent were 3 data
890 // and 1 FEC.
892 // If FEC send policy is FEC_ALARM_TRIGGER, verify that packets sent were 3
893 // data and FEC group is closed.
894 CheckPacketHasSingleStreamFrame(1);
895 CheckPacketHasSingleStreamFrame(2);
896 if (generator_.fec_send_policy() == FEC_ALARM_TRIGGER) {
897 CheckPacketHasSingleStreamFrame(3);
898 } else {
899 CheckPacketIsFec(3, /*fec_group=*/2u);
900 CheckPacketHasSingleStreamFrame(4);
903 // Calling OnFecTimeout should emit the pending FEC packet.
904 EXPECT_CALL(delegate_, OnSerializedPacket(_))
905 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
906 generator_.OnFecTimeout();
907 if (generator_.fec_send_policy() == FEC_ALARM_TRIGGER) {
908 CheckPacketIsFec(4, /*fec_group=*/4u);
909 } else {
910 CheckPacketIsFec(5, /*fec_group=*/5u);
913 // Send one unprotected data packet.
914 EXPECT_CALL(delegate_, OnSerializedPacket(_))
915 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
916 consumed = generator_.ConsumeData(7, CreateData(1u), 0, true, MAY_FEC_PROTECT,
917 nullptr);
918 EXPECT_EQ(1u, consumed.bytes_consumed);
919 EXPECT_FALSE(generator_.HasQueuedFrames());
920 EXPECT_FALSE(creator_->IsFecProtected());
921 // Verify that one unprotected data packet was sent.
922 if (generator_.fec_send_policy() == FEC_ALARM_TRIGGER) {
923 CheckPacketContains(contents, 5);
924 } else {
925 CheckPacketContains(contents, 6);
929 TEST_P(QuicPacketGeneratorTest, SwitchFecOnWithPendingFrameInCreator) {
930 delegate_.SetCanWriteAnything();
931 // Enable FEC.
932 creator_->set_max_packets_per_fec_group(2);
934 generator_.StartBatchOperations();
935 // Queue enough data to prevent a stream frame with a non-zero offset from
936 // fitting.
937 QuicConsumedData consumed = generator_.ConsumeData(7, CreateData(1u), 0, true,
938 MAY_FEC_PROTECT, nullptr);
939 EXPECT_EQ(1u, consumed.bytes_consumed);
940 EXPECT_TRUE(creator_->HasPendingFrames());
942 // Queue protected data for sending. Should cause queued frames to be flushed.
943 EXPECT_CALL(delegate_, OnSerializedPacket(_))
944 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
945 EXPECT_FALSE(creator_->IsFecProtected());
946 consumed = generator_.ConsumeData(7, CreateData(1u), 0, true,
947 MUST_FEC_PROTECT, nullptr);
948 EXPECT_EQ(1u, consumed.bytes_consumed);
949 PacketContents contents;
950 contents.num_stream_frames = 1;
951 // Transmitted packet was not FEC protected.
952 CheckPacketContains(contents, 0);
953 EXPECT_TRUE(creator_->IsFecProtected());
954 EXPECT_TRUE(creator_->HasPendingFrames());
957 TEST_P(QuicPacketGeneratorTest, SwitchFecOnWithPendingFramesInGenerator) {
958 // Enable FEC.
959 creator_->set_max_packets_per_fec_group(2);
961 // Queue control frames in generator.
962 delegate_.SetCanNotWrite();
963 generator_.SetShouldSendAck(true);
964 delegate_.SetCanWriteAnything();
965 generator_.StartBatchOperations();
967 // Set up frames to write into the creator when control frames are written.
968 EXPECT_CALL(delegate_, PopulateAckFrame(_));
969 EXPECT_CALL(delegate_, PopulateStopWaitingFrame(_));
971 // Generator should have queued control frames, and creator should be empty.
972 EXPECT_TRUE(generator_.HasQueuedFrames());
973 EXPECT_FALSE(creator_->HasPendingFrames());
974 EXPECT_FALSE(creator_->IsFecProtected());
976 // Queue protected data for sending. Should cause queued frames to be flushed.
977 EXPECT_CALL(delegate_, OnSerializedPacket(_))
978 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
979 QuicConsumedData consumed = generator_.ConsumeData(7, CreateData(1u), 0, true,
980 MUST_FEC_PROTECT, nullptr);
981 EXPECT_EQ(1u, consumed.bytes_consumed);
982 PacketContents contents;
983 contents.num_ack_frames = 1;
984 contents.num_stop_waiting_frames = 1;
985 CheckPacketContains(contents, 0);
987 // FEC protection should be on in creator.
988 EXPECT_TRUE(creator_->IsFecProtected());
991 TEST_P(QuicPacketGeneratorTest, SwitchFecOnOffWithSubsequentFramesProtected) {
992 delegate_.SetCanWriteAnything();
994 // Enable FEC.
995 creator_->set_max_packets_per_fec_group(2);
996 EXPECT_FALSE(creator_->IsFecProtected());
998 // Queue stream frame to be protected in creator.
999 generator_.StartBatchOperations();
1000 QuicConsumedData consumed = generator_.ConsumeData(5, CreateData(1u), 0, true,
1001 MUST_FEC_PROTECT, nullptr);
1002 EXPECT_EQ(1u, consumed.bytes_consumed);
1003 // Creator has a pending protected frame.
1004 EXPECT_TRUE(creator_->HasPendingFrames());
1005 EXPECT_TRUE(creator_->IsFecProtected());
1007 // Add enough unprotected data to exceed size of current packet, so that
1008 // current packet is sent. Both frames will be sent out in a single packet.
1009 EXPECT_CALL(delegate_, OnSerializedPacket(_))
1010 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
1011 size_t data_len = kDefaultMaxPacketSize;
1012 consumed = generator_.ConsumeData(5, CreateData(data_len), 0, true,
1013 MAY_FEC_PROTECT, nullptr);
1014 EXPECT_EQ(data_len, consumed.bytes_consumed);
1015 PacketContents contents;
1016 contents.num_stream_frames = 2u;
1017 contents.fec_group = 1u;
1018 CheckPacketContains(contents, 0);
1019 // FEC protection should still be on in creator.
1020 EXPECT_TRUE(creator_->IsFecProtected());
1023 TEST_P(QuicPacketGeneratorTest, SwitchFecOnOffWithSubsequentPacketsProtected) {
1024 delegate_.SetCanWriteAnything();
1026 // Enable FEC.
1027 creator_->set_max_packets_per_fec_group(2);
1028 EXPECT_FALSE(creator_->IsFecProtected());
1030 // Send first packet, FEC protected.
1031 EXPECT_CALL(delegate_, OnSerializedPacket(_))
1032 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
1033 QuicConsumedData consumed = generator_.ConsumeData(5, CreateData(1u), 0, true,
1034 MUST_FEC_PROTECT, nullptr);
1035 EXPECT_EQ(1u, consumed.bytes_consumed);
1036 PacketContents contents;
1037 contents.num_stream_frames = 1u;
1038 contents.fec_group = 1u;
1039 CheckPacketContains(contents, 0);
1041 // FEC should still be on in creator.
1042 EXPECT_TRUE(creator_->IsFecProtected());
1044 // Send unprotected data to cause second packet to be sent, which gets
1045 // protected because it happens to fall within an open FEC group. Data packet
1046 // will be followed by FEC packet.
1048 InSequence dummy;
1049 EXPECT_CALL(delegate_, OnSerializedPacket(_))
1050 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
1051 if (generator_.fec_send_policy() == FEC_ALARM_TRIGGER) {
1052 EXPECT_CALL(delegate_, OnResetFecGroup()).Times(1);
1053 } else {
1054 EXPECT_CALL(delegate_, OnSerializedPacket(_))
1055 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
1058 consumed = generator_.ConsumeData(5, CreateData(1u), 0, true, MAY_FEC_PROTECT,
1059 nullptr);
1060 EXPECT_EQ(1u, consumed.bytes_consumed);
1061 contents.num_stream_frames = 1u;
1062 CheckPacketContains(contents, 1);
1063 if (generator_.fec_send_policy() == FEC_ANY_TRIGGER) {
1064 // FEC packet is sent when send policy is FEC_ANY_TRIGGER.
1065 CheckPacketIsFec(2, /*fec_group=*/1u);
1068 // FEC protection should be off in creator.
1069 EXPECT_FALSE(creator_->IsFecProtected());
1072 TEST_P(QuicPacketGeneratorTest, SwitchFecOnOffThenOnWithCreatorProtectionOn) {
1073 delegate_.SetCanWriteAnything();
1074 generator_.StartBatchOperations();
1076 // Enable FEC.
1077 creator_->set_max_packets_per_fec_group(2);
1078 EXPECT_FALSE(creator_->IsFecProtected());
1080 // Queue one byte of FEC protected data.
1081 QuicConsumedData consumed = generator_.ConsumeData(5, CreateData(1u), 0, true,
1082 MUST_FEC_PROTECT, nullptr);
1083 EXPECT_TRUE(creator_->HasPendingFrames());
1085 // Add more unprotected data causing first packet to be sent, FEC protected.
1086 EXPECT_CALL(delegate_, OnSerializedPacket(_))
1087 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
1088 size_t data_len = kDefaultMaxPacketSize;
1089 consumed = generator_.ConsumeData(5, CreateData(data_len), 0, true,
1090 MAY_FEC_PROTECT, nullptr);
1091 EXPECT_EQ(data_len, consumed.bytes_consumed);
1092 PacketContents contents;
1093 contents.num_stream_frames = 2u;
1094 contents.fec_group = 1u;
1095 CheckPacketContains(contents, 0);
1097 // FEC group is still open in creator.
1098 EXPECT_TRUE(creator_->IsFecProtected());
1100 // Add data that should be protected, large enough to cause second packet to
1101 // be sent. Data packet should be followed by FEC packet.
1103 InSequence dummy;
1104 EXPECT_CALL(delegate_, OnSerializedPacket(_))
1105 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
1106 if (generator_.fec_send_policy() == FEC_ALARM_TRIGGER) {
1107 EXPECT_CALL(delegate_, OnResetFecGroup()).Times(1);
1108 } else {
1109 EXPECT_CALL(delegate_, OnSerializedPacket(_))
1110 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
1113 consumed = generator_.ConsumeData(5, CreateData(data_len), 0, true,
1114 MUST_FEC_PROTECT, nullptr);
1115 EXPECT_EQ(data_len, consumed.bytes_consumed);
1116 CheckPacketContains(contents, 1);
1117 if (generator_.fec_send_policy() == FEC_ANY_TRIGGER) {
1118 // FEC packet is sent when send policy is FEC_ANY_TRIGGER.
1119 CheckPacketIsFec(2, /*fec_group=*/1u);
1122 // FEC protection should remain on in creator.
1123 EXPECT_TRUE(creator_->IsFecProtected());
1126 TEST_P(QuicPacketGeneratorTest, ResetFecGroupNoTimeout) {
1127 delegate_.SetCanWriteAnything();
1128 // Send FEC packet after 2 packets.
1129 creator_->set_max_packets_per_fec_group(2);
1130 EXPECT_FALSE(creator_->IsFecProtected());
1132 // Send two packets so that when this data is consumed, two packets are sent
1133 // out. In FEC_TRIGGER_ANY, this will cause an FEC packet to be sent out and
1134 // with FEC_TRIGGER_ALARM, this will cause a Reset to be called. In both
1135 // cases, the creator's fec protection will be turned off afterwards.
1137 InSequence dummy;
1138 EXPECT_CALL(delegate_, OnSerializedPacket(_))
1139 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
1140 EXPECT_CALL(delegate_, OnSerializedPacket(_))
1141 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
1142 if (generator_.fec_send_policy() == FEC_ALARM_TRIGGER) {
1143 // FEC packet is not sent when send policy is FEC_ALARM_TRIGGER, but FEC
1144 // group is closed.
1145 EXPECT_CALL(delegate_, OnResetFecGroup()).Times(1);
1146 } else {
1147 EXPECT_CALL(delegate_, OnSerializedPacket(_))
1148 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
1150 // Fin Packet.
1151 EXPECT_CALL(delegate_, OnSerializedPacket(_))
1152 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
1154 size_t data_len = 2 * kDefaultMaxPacketSize;
1155 QuicConsumedData consumed = generator_.ConsumeData(
1156 5, CreateData(data_len), 0, true, MUST_FEC_PROTECT, nullptr);
1157 EXPECT_EQ(data_len, consumed.bytes_consumed);
1158 EXPECT_TRUE(consumed.fin_consumed);
1159 EXPECT_FALSE(generator_.HasQueuedFrames());
1160 CheckPacketHasSingleStreamFrame(0);
1161 CheckPacketHasSingleStreamFrame(1);
1162 if (generator_.fec_send_policy() == FEC_ALARM_TRIGGER) {
1163 // FEC packet is not sent when send policy is FEC_ALARM_TRIGGER.
1164 CheckPacketHasSingleStreamFrame(2);
1165 } else {
1166 // FEC packet is sent after 2 packets and when send policy is
1167 // FEC_ANY_TRIGGER.
1168 CheckPacketIsFec(2, 1);
1169 CheckPacketHasSingleStreamFrame(3);
1171 EXPECT_TRUE(creator_->IsFecProtected());
1173 // Do the same send (with MUST_FEC_PROTECT) on a different stream id.
1175 InSequence dummy;
1176 EXPECT_CALL(delegate_, OnSerializedPacket(_))
1177 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
1178 // FEC packet is sent after 2 packets and when send policy is
1179 // FEC_ANY_TRIGGER. When policy is FEC_ALARM_TRIGGER, FEC group is closed
1180 // and FEC packet is not sent.
1181 if (generator_.fec_send_policy() == FEC_ALARM_TRIGGER) {
1182 EXPECT_CALL(delegate_, OnResetFecGroup()).Times(1);
1183 } else {
1184 EXPECT_CALL(delegate_, OnSerializedPacket(_))
1185 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
1187 EXPECT_CALL(delegate_, OnSerializedPacket(_))
1188 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
1189 EXPECT_CALL(delegate_, OnSerializedPacket(_))
1190 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
1191 // FEC packet is sent after 2 packets and when send policy is
1192 // FEC_ANY_TRIGGER. When policy is FEC_ALARM_TRIGGER, FEC group is closed
1193 // and FEC packet is not sent.
1194 if (generator_.fec_send_policy() == FEC_ALARM_TRIGGER) {
1195 EXPECT_CALL(delegate_, OnResetFecGroup()).Times(1);
1196 } else {
1197 EXPECT_CALL(delegate_, OnSerializedPacket(_))
1198 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
1201 consumed = generator_.ConsumeData(7, CreateData(data_len), 0, true,
1202 MUST_FEC_PROTECT, nullptr);
1203 EXPECT_EQ(data_len, consumed.bytes_consumed);
1204 EXPECT_TRUE(consumed.fin_consumed);
1205 EXPECT_FALSE(generator_.HasQueuedFrames());
1206 if (generator_.fec_send_policy() == FEC_ALARM_TRIGGER) {
1207 CheckPacketHasSingleStreamFrame(3);
1208 CheckPacketHasSingleStreamFrame(4);
1209 CheckPacketHasSingleStreamFrame(5);
1210 } else {
1211 CheckPacketHasSingleStreamFrame(4);
1212 // FEC packet is sent after 2 packets and when send policy is
1213 // FEC_ANY_TRIGGER.
1214 CheckPacketIsFec(5, 4);
1215 CheckPacketHasSingleStreamFrame(6);
1216 CheckPacketHasSingleStreamFrame(7);
1217 // FEC packet is sent after 2 packets and when send policy is
1218 // FEC_ANY_TRIGGER.
1219 CheckPacketIsFec(8, 7);
1221 EXPECT_TRUE(creator_->IsFecProtected());
1224 // 1. Create and send one packet with MUST_FEC_PROTECT.
1225 // 2. Call FecTimeout, expect FEC packet is sent.
1226 // 3. Do the same thing over again, with a different stream id.
1227 TEST_P(QuicPacketGeneratorTest, FecPacketSentOnFecTimeout) {
1228 delegate_.SetCanWriteAnything();
1229 creator_->set_max_packets_per_fec_group(1000);
1230 EXPECT_FALSE(creator_->IsFecProtected());
1232 for (int i = 1; i < 4; i = i + 2) {
1233 // Send data with MUST_FEC_PROTECT flag. No FEC packet is emitted, but the
1234 // creator FEC protects all data.
1235 EXPECT_CALL(delegate_, OnSerializedPacket(_))
1236 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
1237 QuicConsumedData consumed = generator_.ConsumeData(
1238 i + 2, CreateData(1u), 0, true, MUST_FEC_PROTECT, nullptr);
1239 EXPECT_EQ(1u, consumed.bytes_consumed);
1240 EXPECT_TRUE(consumed.fin_consumed);
1241 CheckPacketHasSingleStreamFrame(0);
1242 EXPECT_TRUE(creator_->IsFecProtected());
1244 // Calling OnFecTimeout should cause the FEC packet to be emitted.
1245 EXPECT_CALL(delegate_, OnSerializedPacket(_))
1246 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
1247 generator_.OnFecTimeout();
1248 CheckPacketIsFec(i, i);
1249 EXPECT_FALSE(creator_->IsFecProtected());
1253 TEST_P(QuicPacketGeneratorTest, NotWritableThenBatchOperations) {
1254 delegate_.SetCanNotWrite();
1256 generator_.SetShouldSendAck(false);
1257 generator_.AddControlFrame(QuicFrame(CreateRstStreamFrame()));
1258 EXPECT_TRUE(generator_.HasQueuedFrames());
1260 delegate_.SetCanWriteAnything();
1262 generator_.StartBatchOperations();
1264 // When the first write operation is invoked, the ack frame will be returned.
1265 EXPECT_CALL(delegate_, PopulateAckFrame(_));
1267 // Send some data and a control frame
1268 generator_.ConsumeData(3, MakeIOVector("quux"), 7, false, MAY_FEC_PROTECT,
1269 nullptr);
1270 generator_.AddControlFrame(QuicFrame(CreateGoAwayFrame()));
1272 // All five frames will be flushed out in a single packet.
1273 EXPECT_CALL(delegate_, OnSerializedPacket(_))
1274 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
1275 generator_.FinishBatchOperations();
1276 EXPECT_FALSE(generator_.HasQueuedFrames());
1278 PacketContents contents;
1279 contents.num_ack_frames = 1;
1280 contents.num_goaway_frames = 1;
1281 contents.num_rst_stream_frames = 1;
1282 contents.num_stream_frames = 1;
1283 CheckPacketContains(contents, 0);
1286 TEST_P(QuicPacketGeneratorTest, NotWritableThenBatchOperations2) {
1287 delegate_.SetCanNotWrite();
1289 generator_.SetShouldSendAck(false);
1290 generator_.AddControlFrame(QuicFrame(CreateRstStreamFrame()));
1291 EXPECT_TRUE(generator_.HasQueuedFrames());
1293 delegate_.SetCanWriteAnything();
1295 generator_.StartBatchOperations();
1297 // When the first write operation is invoked, the ack frame will be returned.
1298 EXPECT_CALL(delegate_, PopulateAckFrame(_));
1301 InSequence dummy;
1302 // All five frames will be flushed out in a single packet
1303 EXPECT_CALL(delegate_, OnSerializedPacket(_))
1304 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
1305 EXPECT_CALL(delegate_, OnSerializedPacket(_))
1306 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
1309 // Send enough data to exceed one packet
1310 size_t data_len = kDefaultMaxPacketSize + 100;
1311 QuicConsumedData consumed = generator_.ConsumeData(
1312 3, CreateData(data_len), 0, true, MAY_FEC_PROTECT, nullptr);
1313 EXPECT_EQ(data_len, consumed.bytes_consumed);
1314 EXPECT_TRUE(consumed.fin_consumed);
1315 generator_.AddControlFrame(QuicFrame(CreateGoAwayFrame()));
1317 generator_.FinishBatchOperations();
1318 EXPECT_FALSE(generator_.HasQueuedFrames());
1320 // The first packet should have the queued data and part of the stream data.
1321 PacketContents contents;
1322 contents.num_ack_frames = 1;
1323 contents.num_rst_stream_frames = 1;
1324 contents.num_stream_frames = 1;
1325 CheckPacketContains(contents, 0);
1327 // The second should have the remainder of the stream data.
1328 PacketContents contents2;
1329 contents2.num_goaway_frames = 1;
1330 contents2.num_stream_frames = 1;
1331 CheckPacketContains(contents2, 1);
1334 TEST_P(QuicPacketGeneratorTest, TestConnectionIdLength) {
1335 generator_.SetConnectionIdLength(0);
1336 EXPECT_EQ(PACKET_0BYTE_CONNECTION_ID, creator_->connection_id_length());
1337 generator_.SetConnectionIdLength(1);
1338 EXPECT_EQ(PACKET_1BYTE_CONNECTION_ID, creator_->connection_id_length());
1339 generator_.SetConnectionIdLength(2);
1340 EXPECT_EQ(PACKET_4BYTE_CONNECTION_ID, creator_->connection_id_length());
1341 generator_.SetConnectionIdLength(3);
1342 EXPECT_EQ(PACKET_4BYTE_CONNECTION_ID, creator_->connection_id_length());
1343 generator_.SetConnectionIdLength(4);
1344 EXPECT_EQ(PACKET_4BYTE_CONNECTION_ID, creator_->connection_id_length());
1345 generator_.SetConnectionIdLength(5);
1346 EXPECT_EQ(PACKET_8BYTE_CONNECTION_ID, creator_->connection_id_length());
1347 generator_.SetConnectionIdLength(6);
1348 EXPECT_EQ(PACKET_8BYTE_CONNECTION_ID, creator_->connection_id_length());
1349 generator_.SetConnectionIdLength(7);
1350 EXPECT_EQ(PACKET_8BYTE_CONNECTION_ID, creator_->connection_id_length());
1351 generator_.SetConnectionIdLength(8);
1352 EXPECT_EQ(PACKET_8BYTE_CONNECTION_ID, creator_->connection_id_length());
1353 generator_.SetConnectionIdLength(9);
1354 EXPECT_EQ(PACKET_8BYTE_CONNECTION_ID, creator_->connection_id_length());
1357 // Test whether SetMaxPacketLength() works in the situation when the queue is
1358 // empty, and we send three packets worth of data.
1359 TEST_P(QuicPacketGeneratorTest, SetMaxPacketLength_Initial) {
1360 delegate_.SetCanWriteAnything();
1362 // Send enough data for three packets.
1363 size_t data_len = 3 * kDefaultMaxPacketSize + 1;
1364 size_t packet_len = kDefaultMaxPacketSize + 100;
1365 ASSERT_LE(packet_len, kMaxPacketSize);
1366 generator_.SetMaxPacketLength(packet_len, /*force=*/false);
1367 EXPECT_EQ(packet_len, generator_.GetCurrentMaxPacketLength());
1369 EXPECT_CALL(delegate_, OnSerializedPacket(_))
1370 .Times(3)
1371 .WillRepeatedly(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
1372 QuicConsumedData consumed =
1373 generator_.ConsumeData(kHeadersStreamId, CreateData(data_len),
1374 /*offset=*/2,
1375 /*fin=*/true, MAY_FEC_PROTECT, nullptr);
1376 EXPECT_EQ(data_len, consumed.bytes_consumed);
1377 EXPECT_TRUE(consumed.fin_consumed);
1378 EXPECT_FALSE(generator_.HasQueuedFrames());
1380 // We expect three packets, and first two of them have to be of packet_len
1381 // size. We check multiple packets (instead of just one) because we want to
1382 // ensure that |max_packet_length_| does not get changed incorrectly by the
1383 // generator after first packet is serialized.
1384 ASSERT_EQ(3u, packets_.size());
1385 EXPECT_EQ(packet_len, packets_[0].packet->length());
1386 EXPECT_EQ(packet_len, packets_[1].packet->length());
1387 CheckAllPacketsHaveSingleStreamFrame();
1390 // Test whether SetMaxPacketLength() works in the situation when we first write
1391 // data, then change packet size, then write data again.
1392 TEST_P(QuicPacketGeneratorTest, SetMaxPacketLength_Middle) {
1393 delegate_.SetCanWriteAnything();
1395 // We send enough data to overflow default packet length, but not the altered
1396 // one.
1397 size_t data_len = kDefaultMaxPacketSize;
1398 size_t packet_len = kDefaultMaxPacketSize + 100;
1399 ASSERT_LE(packet_len, kMaxPacketSize);
1401 // We expect to see three packets in total.
1402 EXPECT_CALL(delegate_, OnSerializedPacket(_))
1403 .Times(3)
1404 .WillRepeatedly(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
1406 // Send two packets before packet size change.
1407 QuicConsumedData consumed =
1408 generator_.ConsumeData(kHeadersStreamId, CreateData(data_len),
1409 /*offset=*/2,
1410 /*fin=*/false, MAY_FEC_PROTECT, nullptr);
1411 EXPECT_EQ(data_len, consumed.bytes_consumed);
1412 EXPECT_FALSE(consumed.fin_consumed);
1413 EXPECT_FALSE(generator_.HasQueuedFrames());
1415 // Make sure we already have two packets.
1416 ASSERT_EQ(2u, packets_.size());
1418 // Increase packet size.
1419 generator_.SetMaxPacketLength(packet_len, /*force=*/false);
1420 EXPECT_EQ(packet_len, generator_.GetCurrentMaxPacketLength());
1422 // Send a packet after packet size change.
1423 consumed = generator_.ConsumeData(kHeadersStreamId, CreateData(data_len),
1424 2 + data_len,
1425 /*fin=*/true, MAY_FEC_PROTECT, nullptr);
1426 EXPECT_EQ(data_len, consumed.bytes_consumed);
1427 EXPECT_TRUE(consumed.fin_consumed);
1428 EXPECT_FALSE(generator_.HasQueuedFrames());
1430 // We expect first data chunk to get fragmented, but the second one to fit
1431 // into a single packet.
1432 ASSERT_EQ(3u, packets_.size());
1433 EXPECT_EQ(kDefaultMaxPacketSize, packets_[0].packet->length());
1434 EXPECT_LE(kDefaultMaxPacketSize, packets_[2].packet->length());
1435 CheckAllPacketsHaveSingleStreamFrame();
1438 // Test whether SetMaxPacketLength() works correctly when we change the packet
1439 // size in the middle of the batched packet.
1440 TEST_P(QuicPacketGeneratorTest, SetMaxPacketLength_Midpacket) {
1441 delegate_.SetCanWriteAnything();
1442 generator_.StartBatchOperations();
1444 size_t first_write_len = kDefaultMaxPacketSize / 2;
1445 size_t second_write_len = kDefaultMaxPacketSize;
1446 size_t packet_len = kDefaultMaxPacketSize + 100;
1447 ASSERT_LE(packet_len, kMaxPacketSize);
1449 // First send half of the packet worth of data. We are in the batch mode, so
1450 // should not cause packet serialization.
1451 QuicConsumedData consumed =
1452 generator_.ConsumeData(kHeadersStreamId, CreateData(first_write_len),
1453 /*offset=*/2,
1454 /*fin=*/false, MAY_FEC_PROTECT, nullptr);
1455 EXPECT_EQ(first_write_len, consumed.bytes_consumed);
1456 EXPECT_FALSE(consumed.fin_consumed);
1457 EXPECT_TRUE(generator_.HasQueuedFrames());
1459 // Make sure we have no packets so far.
1460 ASSERT_EQ(0u, packets_.size());
1462 // Increase packet size. Ensure it's not immediately enacted.
1463 generator_.SetMaxPacketLength(packet_len, /*force=*/false);
1464 EXPECT_EQ(packet_len, generator_.GetMaxPacketLength());
1465 EXPECT_EQ(kDefaultMaxPacketSize, generator_.GetCurrentMaxPacketLength());
1467 // We expect to see exactly one packet serialized after that, since we are in
1468 // batch mode and we have sent approximately 3/2 of our MTU.
1469 EXPECT_CALL(delegate_, OnSerializedPacket(_))
1470 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
1472 // Send a packet worth of data to the same stream. This should trigger
1473 // serialization of other packet.
1474 consumed =
1475 generator_.ConsumeData(kHeadersStreamId, CreateData(second_write_len),
1476 /*offset=*/2 + first_write_len,
1477 /*fin=*/true, MAY_FEC_PROTECT, nullptr);
1478 EXPECT_EQ(second_write_len, consumed.bytes_consumed);
1479 EXPECT_TRUE(consumed.fin_consumed);
1480 EXPECT_TRUE(generator_.HasQueuedFrames());
1482 // We expect the first packet to contain two frames, and to not reflect the
1483 // packet size change.
1484 ASSERT_EQ(1u, packets_.size());
1485 EXPECT_EQ(kDefaultMaxPacketSize, packets_[0].packet->length());
1487 PacketContents contents;
1488 contents.num_stream_frames = 2;
1489 CheckPacketContains(contents, 0);
1492 // Test whether SetMaxPacketLength() works correctly when we force the change of
1493 // the packet size in the middle of the batched packet.
1494 TEST_P(QuicPacketGeneratorTest, SetMaxPacketLength_MidpacketFlush) {
1495 delegate_.SetCanWriteAnything();
1496 generator_.StartBatchOperations();
1498 size_t first_write_len = kDefaultMaxPacketSize / 2;
1499 size_t packet_len = kDefaultMaxPacketSize + 100;
1500 size_t second_write_len = packet_len + 1;
1501 ASSERT_LE(packet_len, kMaxPacketSize);
1503 // First send half of the packet worth of data. We are in the batch mode, so
1504 // should not cause packet serialization.
1505 QuicConsumedData consumed =
1506 generator_.ConsumeData(kHeadersStreamId, CreateData(first_write_len),
1507 /*offset=*/2,
1508 /*fin=*/false, MAY_FEC_PROTECT, nullptr);
1509 EXPECT_EQ(first_write_len, consumed.bytes_consumed);
1510 EXPECT_FALSE(consumed.fin_consumed);
1511 EXPECT_TRUE(generator_.HasQueuedFrames());
1513 // Make sure we have no packets so far.
1514 ASSERT_EQ(0u, packets_.size());
1516 // Expect a packet to be flushed.
1517 EXPECT_CALL(delegate_, OnSerializedPacket(_))
1518 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
1520 // Increase packet size. Ensure it's immediately enacted.
1521 generator_.SetMaxPacketLength(packet_len, /*force=*/true);
1522 EXPECT_EQ(packet_len, generator_.GetMaxPacketLength());
1523 EXPECT_EQ(packet_len, generator_.GetCurrentMaxPacketLength());
1524 EXPECT_FALSE(generator_.HasQueuedFrames());
1526 // We expect to see exactly one packet serialized after that, because we send
1527 // a value somewhat exceeding new max packet size, and the tail data does not
1528 // get serialized because we are still in the batch mode.
1529 EXPECT_CALL(delegate_, OnSerializedPacket(_))
1530 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
1532 // Send a more than a packet worth of data to the same stream. This should
1533 // trigger serialization of one packet, and queue another one.
1534 consumed =
1535 generator_.ConsumeData(kHeadersStreamId, CreateData(second_write_len),
1536 /*offset=*/2 + first_write_len,
1537 /*fin=*/true, MAY_FEC_PROTECT, nullptr);
1538 EXPECT_EQ(second_write_len, consumed.bytes_consumed);
1539 EXPECT_TRUE(consumed.fin_consumed);
1540 EXPECT_TRUE(generator_.HasQueuedFrames());
1542 // We expect the first packet to be underfilled, and the second packet be up
1543 // to the new max packet size.
1544 ASSERT_EQ(2u, packets_.size());
1545 EXPECT_GT(kDefaultMaxPacketSize, packets_[0].packet->length());
1546 EXPECT_EQ(packet_len, packets_[1].packet->length());
1548 CheckAllPacketsHaveSingleStreamFrame();
1551 // Test sending an MTU probe, without any surrounding data.
1552 TEST_P(QuicPacketGeneratorTest, GenerateMtuDiscoveryPacket_Simple) {
1553 delegate_.SetCanWriteAnything();
1555 const size_t target_mtu = kDefaultMaxPacketSize + 100;
1556 static_assert(target_mtu < kMaxPacketSize,
1557 "The MTU probe used by the test exceeds maximum packet size");
1559 EXPECT_CALL(delegate_, OnSerializedPacket(_))
1560 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
1562 generator_.GenerateMtuDiscoveryPacket(target_mtu, nullptr);
1564 EXPECT_FALSE(generator_.HasQueuedFrames());
1565 ASSERT_EQ(1u, packets_.size());
1566 EXPECT_EQ(target_mtu, packets_[0].packet->length());
1568 PacketContents contents;
1569 contents.num_mtu_discovery_frames = 1;
1570 CheckPacketContains(contents, 0);
1573 // Test sending an MTU probe. Surround it with data, to ensure that it resets
1574 // the MTU to the value before the probe was sent.
1575 TEST_P(QuicPacketGeneratorTest, GenerateMtuDiscoveryPacket_SurroundedByData) {
1576 delegate_.SetCanWriteAnything();
1578 const size_t target_mtu = kDefaultMaxPacketSize + 100;
1579 static_assert(target_mtu < kMaxPacketSize,
1580 "The MTU probe used by the test exceeds maximum packet size");
1582 // Send enough data so it would always cause two packets to be sent.
1583 const size_t data_len = target_mtu + 1;
1585 // Send a total of five packets: two packets before the probe, the probe
1586 // itself, and two packets after the probe.
1587 EXPECT_CALL(delegate_, OnSerializedPacket(_))
1588 .Times(5)
1589 .WillRepeatedly(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
1591 // Send data before the MTU probe.
1592 QuicConsumedData consumed =
1593 generator_.ConsumeData(kHeadersStreamId, CreateData(data_len),
1594 /*offset=*/2,
1595 /*fin=*/false, MAY_FEC_PROTECT, nullptr);
1596 EXPECT_EQ(data_len, consumed.bytes_consumed);
1597 EXPECT_FALSE(consumed.fin_consumed);
1598 EXPECT_FALSE(generator_.HasQueuedFrames());
1600 // Send the MTU probe.
1601 generator_.GenerateMtuDiscoveryPacket(target_mtu, nullptr);
1602 EXPECT_FALSE(generator_.HasQueuedFrames());
1604 // Send data after the MTU probe.
1605 consumed = generator_.ConsumeData(kHeadersStreamId, CreateData(data_len),
1606 /*offset=*/2 + data_len,
1607 /*fin=*/true, MAY_FEC_PROTECT, nullptr);
1608 EXPECT_EQ(data_len, consumed.bytes_consumed);
1609 EXPECT_TRUE(consumed.fin_consumed);
1610 EXPECT_FALSE(generator_.HasQueuedFrames());
1612 ASSERT_EQ(5u, packets_.size());
1613 EXPECT_EQ(kDefaultMaxPacketSize, packets_[0].packet->length());
1614 EXPECT_EQ(target_mtu, packets_[2].packet->length());
1615 EXPECT_EQ(kDefaultMaxPacketSize, packets_[3].packet->length());
1617 PacketContents probe_contents;
1618 probe_contents.num_mtu_discovery_frames = 1;
1620 CheckPacketHasSingleStreamFrame(0);
1621 CheckPacketHasSingleStreamFrame(1);
1622 CheckPacketContains(probe_contents, 2);
1623 CheckPacketHasSingleStreamFrame(3);
1624 CheckPacketHasSingleStreamFrame(4);
1627 TEST_P(QuicPacketGeneratorTest, DontCrashOnInvalidStopWaiting) {
1628 // Test added to ensure the generator does not crash when an invalid frame is
1629 // added. Because this is an indication of internal programming errors,
1630 // DFATALs are expected.
1631 // A 1 byte packet number length can't encode a gap of 1000.
1632 QuicPacketCreatorPeer::SetPacketNumber(creator_, 1000);
1634 delegate_.SetCanNotWrite();
1635 generator_.SetShouldSendAck(true);
1636 delegate_.SetCanWriteAnything();
1637 generator_.StartBatchOperations();
1639 // Set up frames to write into the creator when control frames are written.
1640 EXPECT_CALL(delegate_, PopulateAckFrame(_));
1641 EXPECT_CALL(delegate_, PopulateStopWaitingFrame(_));
1642 // Generator should have queued control frames, and creator should be empty.
1643 EXPECT_TRUE(generator_.HasQueuedFrames());
1644 EXPECT_FALSE(creator_->HasPendingFrames());
1646 // This will not serialize any packets, because of the invalid frame.
1647 EXPECT_CALL(delegate_,
1648 CloseConnection(QUIC_FAILED_TO_SERIALIZE_PACKET, false));
1649 EXPECT_DFATAL(generator_.FinishBatchOperations(),
1650 "packet_number_length 1 is too small "
1651 "for least_unacked_delta: 1001");
1654 } // namespace test
1655 } // namespace net