Supervised user whitelists: Cleanup
[chromium-blink-merge.git] / net / quic / quic_packet_generator_test.cc
blob13731173a37f92b6084e0372362659cb859fe9c7
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 testing::InSequence;
26 using testing::Return;
27 using testing::SaveArg;
28 using testing::StrictMock;
29 using testing::_;
31 namespace net {
32 namespace test {
33 namespace {
35 const int64 kMinFecTimeoutMs = 5u;
37 class MockDelegate : public QuicPacketGenerator::DelegateInterface {
38 public:
39 MockDelegate() {}
40 ~MockDelegate() override {}
42 MOCK_METHOD3(ShouldGeneratePacket,
43 bool(TransmissionType transmission_type,
44 HasRetransmittableData retransmittable,
45 IsHandshake handshake));
46 MOCK_METHOD1(PopulateAckFrame, void(QuicAckFrame*));
47 MOCK_METHOD1(PopulateStopWaitingFrame, void(QuicStopWaitingFrame*));
48 MOCK_METHOD1(OnSerializedPacket, void(const SerializedPacket& packet));
49 MOCK_METHOD2(CloseConnection, void(QuicErrorCode, bool));
51 void SetCanWriteAnything() {
52 EXPECT_CALL(*this, ShouldGeneratePacket(NOT_RETRANSMISSION, _, _))
53 .WillRepeatedly(Return(true));
54 EXPECT_CALL(*this, ShouldGeneratePacket(NOT_RETRANSMISSION,
55 NO_RETRANSMITTABLE_DATA, _))
56 .WillRepeatedly(Return(true));
59 void SetCanNotWrite() {
60 EXPECT_CALL(*this, ShouldGeneratePacket(NOT_RETRANSMISSION, _, _))
61 .WillRepeatedly(Return(false));
62 EXPECT_CALL(*this, ShouldGeneratePacket(NOT_RETRANSMISSION,
63 NO_RETRANSMITTABLE_DATA, _))
64 .WillRepeatedly(Return(false));
67 // Use this when only ack frames should be allowed to be written.
68 void SetCanWriteOnlyNonRetransmittable() {
69 EXPECT_CALL(*this, ShouldGeneratePacket(NOT_RETRANSMISSION, _, _))
70 .WillRepeatedly(Return(false));
71 EXPECT_CALL(*this, ShouldGeneratePacket(NOT_RETRANSMISSION,
72 NO_RETRANSMITTABLE_DATA, _))
73 .WillRepeatedly(Return(true));
76 private:
77 DISALLOW_COPY_AND_ASSIGN(MockDelegate);
80 // Simple struct for describing the contents of a packet.
81 // Useful in conjunction with a SimpleQuicFrame for validating that a packet
82 // contains the expected frames.
83 struct PacketContents {
84 PacketContents()
85 : num_ack_frames(0),
86 num_connection_close_frames(0),
87 num_goaway_frames(0),
88 num_rst_stream_frames(0),
89 num_stop_waiting_frames(0),
90 num_stream_frames(0),
91 fec_group(0) {
94 size_t num_ack_frames;
95 size_t num_connection_close_frames;
96 size_t num_goaway_frames;
97 size_t num_rst_stream_frames;
98 size_t num_stop_waiting_frames;
99 size_t num_stream_frames;
101 QuicFecGroupNumber fec_group;
104 } // namespace
106 class QuicPacketGeneratorTest : public ::testing::Test {
107 protected:
108 QuicPacketGeneratorTest()
109 : framer_(QuicSupportedVersions(),
110 QuicTime::Zero(),
111 Perspective::IS_CLIENT),
112 generator_(42, &framer_, &random_, &delegate_),
113 creator_(QuicPacketGeneratorPeer::GetPacketCreator(&generator_)),
114 packet_(0, PACKET_1BYTE_SEQUENCE_NUMBER, nullptr, 0, nullptr),
115 packet2_(0, PACKET_1BYTE_SEQUENCE_NUMBER, nullptr, 0, nullptr),
116 packet3_(0, PACKET_1BYTE_SEQUENCE_NUMBER, nullptr, 0, nullptr),
117 packet4_(0, PACKET_1BYTE_SEQUENCE_NUMBER, nullptr, 0, nullptr),
118 packet5_(0, PACKET_1BYTE_SEQUENCE_NUMBER, nullptr, 0, nullptr),
119 packet6_(0, PACKET_1BYTE_SEQUENCE_NUMBER, nullptr, 0, nullptr),
120 packet7_(0, PACKET_1BYTE_SEQUENCE_NUMBER, nullptr, 0, nullptr),
121 packet8_(0, PACKET_1BYTE_SEQUENCE_NUMBER, nullptr, 0, nullptr) {}
123 ~QuicPacketGeneratorTest() override {
124 delete packet_.packet;
125 delete packet_.retransmittable_frames;
126 delete packet2_.packet;
127 delete packet2_.retransmittable_frames;
128 delete packet3_.packet;
129 delete packet3_.retransmittable_frames;
130 delete packet4_.packet;
131 delete packet4_.retransmittable_frames;
132 delete packet5_.packet;
133 delete packet5_.retransmittable_frames;
134 delete packet6_.packet;
135 delete packet6_.retransmittable_frames;
136 delete packet7_.packet;
137 delete packet7_.retransmittable_frames;
138 delete packet8_.packet;
139 delete packet8_.retransmittable_frames;
142 QuicRstStreamFrame* CreateRstStreamFrame() {
143 return new QuicRstStreamFrame(1, QUIC_STREAM_NO_ERROR, 0);
146 QuicGoAwayFrame* CreateGoAwayFrame() {
147 return new QuicGoAwayFrame(QUIC_NO_ERROR, 1, string());
150 void CheckPacketContains(const PacketContents& contents,
151 const SerializedPacket& packet) {
152 size_t num_retransmittable_frames = contents.num_connection_close_frames +
153 contents.num_goaway_frames + contents.num_rst_stream_frames +
154 contents.num_stream_frames;
155 size_t num_frames = contents.num_ack_frames +
156 contents.num_stop_waiting_frames +
157 num_retransmittable_frames;
159 if (num_retransmittable_frames == 0) {
160 ASSERT_TRUE(packet.retransmittable_frames == nullptr);
161 } else {
162 ASSERT_TRUE(packet.retransmittable_frames != nullptr);
163 EXPECT_EQ(num_retransmittable_frames,
164 packet.retransmittable_frames->frames().size());
167 ASSERT_TRUE(packet.packet != nullptr);
168 ASSERT_TRUE(simple_framer_.ProcessPacket(*packet.packet));
169 EXPECT_EQ(num_frames, simple_framer_.num_frames());
170 EXPECT_EQ(contents.num_ack_frames, simple_framer_.ack_frames().size());
171 EXPECT_EQ(contents.num_connection_close_frames,
172 simple_framer_.connection_close_frames().size());
173 EXPECT_EQ(contents.num_goaway_frames,
174 simple_framer_.goaway_frames().size());
175 EXPECT_EQ(contents.num_rst_stream_frames,
176 simple_framer_.rst_stream_frames().size());
177 EXPECT_EQ(contents.num_stream_frames,
178 simple_framer_.stream_frames().size());
179 EXPECT_EQ(contents.num_stop_waiting_frames,
180 simple_framer_.stop_waiting_frames().size());
181 EXPECT_EQ(contents.fec_group, simple_framer_.header().fec_group);
184 void CheckPacketHasSingleStreamFrame(const SerializedPacket& packet) {
185 ASSERT_TRUE(packet.retransmittable_frames != nullptr);
186 EXPECT_EQ(1u, packet.retransmittable_frames->frames().size());
187 ASSERT_TRUE(packet.packet != nullptr);
188 ASSERT_TRUE(simple_framer_.ProcessPacket(*packet.packet));
189 EXPECT_EQ(1u, simple_framer_.num_frames());
190 EXPECT_EQ(1u, simple_framer_.stream_frames().size());
193 void CheckPacketIsFec(const SerializedPacket& packet,
194 QuicPacketSequenceNumber fec_group) {
195 ASSERT_TRUE(packet.retransmittable_frames == nullptr);
196 ASSERT_TRUE(packet.packet != nullptr);
197 ASSERT_TRUE(simple_framer_.ProcessPacket(*packet.packet));
198 EXPECT_TRUE(simple_framer_.header().fec_flag);
199 EXPECT_EQ(fec_group, simple_framer_.fec_data().fec_group);
202 IOVector CreateData(size_t len) {
203 data_array_.reset(new char[len]);
204 memset(data_array_.get(), '?', len);
205 IOVector data;
206 data.Append(data_array_.get(), len);
207 return data;
210 QuicFramer framer_;
211 MockRandom random_;
212 StrictMock<MockDelegate> delegate_;
213 QuicPacketGenerator generator_;
214 QuicPacketCreator* creator_;
215 SimpleQuicFramer simple_framer_;
216 SerializedPacket packet_;
217 SerializedPacket packet2_;
218 SerializedPacket packet3_;
219 SerializedPacket packet4_;
220 SerializedPacket packet5_;
221 SerializedPacket packet6_;
222 SerializedPacket packet7_;
223 SerializedPacket packet8_;
225 private:
226 scoped_ptr<char[]> data_array_;
229 class MockDebugDelegate : public QuicPacketGenerator::DebugDelegate {
230 public:
231 MOCK_METHOD1(OnFrameAddedToPacket,
232 void(const QuicFrame&));
235 TEST_F(QuicPacketGeneratorTest, ShouldSendAck_NotWritable) {
236 delegate_.SetCanNotWrite();
238 generator_.SetShouldSendAck(false);
239 EXPECT_TRUE(generator_.HasQueuedFrames());
242 TEST_F(QuicPacketGeneratorTest, ShouldSendAck_WritableAndShouldNotFlush) {
243 StrictMock<MockDebugDelegate> debug_delegate;
245 generator_.set_debug_delegate(&debug_delegate);
246 delegate_.SetCanWriteOnlyNonRetransmittable();
247 generator_.StartBatchOperations();
249 EXPECT_CALL(delegate_, PopulateAckFrame(_));
250 EXPECT_CALL(debug_delegate, OnFrameAddedToPacket(_)).Times(1);
252 generator_.SetShouldSendAck(false);
253 EXPECT_TRUE(generator_.HasQueuedFrames());
256 TEST_F(QuicPacketGeneratorTest, ShouldSendAck_WritableAndShouldFlush) {
257 delegate_.SetCanWriteOnlyNonRetransmittable();
259 EXPECT_CALL(delegate_, PopulateAckFrame(_));
260 EXPECT_CALL(delegate_, OnSerializedPacket(_)).WillOnce(SaveArg<0>(&packet_));
262 generator_.SetShouldSendAck(false);
263 EXPECT_FALSE(generator_.HasQueuedFrames());
265 PacketContents contents;
266 contents.num_ack_frames = 1;
267 CheckPacketContains(contents, packet_);
270 TEST_F(QuicPacketGeneratorTest, ShouldSendAck_MultipleCalls) {
271 // Make sure that calling SetShouldSendAck multiple times does not result in a
272 // crash. Previously this would result in multiple QuicFrames queued in the
273 // packet generator, with all but the last with internal pointers to freed
274 // memory.
275 delegate_.SetCanWriteAnything();
277 // Only one AckFrame should be created.
278 EXPECT_CALL(delegate_, PopulateAckFrame(_)).Times(1);
279 EXPECT_CALL(delegate_, OnSerializedPacket(_))
280 .Times(1)
281 .WillOnce(SaveArg<0>(&packet_));
283 generator_.StartBatchOperations();
284 generator_.SetShouldSendAck(false);
285 generator_.SetShouldSendAck(false);
286 generator_.FinishBatchOperations();
289 TEST_F(QuicPacketGeneratorTest, AddControlFrame_NotWritable) {
290 delegate_.SetCanNotWrite();
292 generator_.AddControlFrame(QuicFrame(CreateRstStreamFrame()));
293 EXPECT_TRUE(generator_.HasQueuedFrames());
296 TEST_F(QuicPacketGeneratorTest, AddControlFrame_OnlyAckWritable) {
297 delegate_.SetCanWriteOnlyNonRetransmittable();
299 generator_.AddControlFrame(QuicFrame(CreateRstStreamFrame()));
300 EXPECT_TRUE(generator_.HasQueuedFrames());
303 TEST_F(QuicPacketGeneratorTest, AddControlFrame_WritableAndShouldNotFlush) {
304 delegate_.SetCanWriteAnything();
305 generator_.StartBatchOperations();
307 generator_.AddControlFrame(QuicFrame(CreateRstStreamFrame()));
308 EXPECT_TRUE(generator_.HasQueuedFrames());
311 TEST_F(QuicPacketGeneratorTest, AddControlFrame_NotWritableBatchThenFlush) {
312 delegate_.SetCanNotWrite();
313 generator_.StartBatchOperations();
315 generator_.AddControlFrame(QuicFrame(CreateRstStreamFrame()));
316 EXPECT_TRUE(generator_.HasQueuedFrames());
317 generator_.FinishBatchOperations();
318 EXPECT_TRUE(generator_.HasQueuedFrames());
320 EXPECT_CALL(delegate_, OnSerializedPacket(_)).WillOnce(SaveArg<0>(&packet_));
321 generator_.FlushAllQueuedFrames();
322 EXPECT_FALSE(generator_.HasQueuedFrames());
324 PacketContents contents;
325 contents.num_rst_stream_frames = 1;
326 CheckPacketContains(contents, packet_);
329 TEST_F(QuicPacketGeneratorTest, AddControlFrame_WritableAndShouldFlush) {
330 delegate_.SetCanWriteAnything();
332 EXPECT_CALL(delegate_, OnSerializedPacket(_)).WillOnce(SaveArg<0>(&packet_));
334 generator_.AddControlFrame(QuicFrame(CreateRstStreamFrame()));
335 EXPECT_FALSE(generator_.HasQueuedFrames());
337 PacketContents contents;
338 contents.num_rst_stream_frames = 1;
339 CheckPacketContains(contents, packet_);
342 TEST_F(QuicPacketGeneratorTest, ConsumeData_NotWritable) {
343 delegate_.SetCanNotWrite();
345 QuicConsumedData consumed = generator_.ConsumeData(
346 kHeadersStreamId, MakeIOVector("foo"), 2, true, MAY_FEC_PROTECT, nullptr);
347 EXPECT_EQ(0u, consumed.bytes_consumed);
348 EXPECT_FALSE(consumed.fin_consumed);
349 EXPECT_FALSE(generator_.HasQueuedFrames());
352 TEST_F(QuicPacketGeneratorTest, ConsumeData_WritableAndShouldNotFlush) {
353 delegate_.SetCanWriteAnything();
354 generator_.StartBatchOperations();
356 QuicConsumedData consumed = generator_.ConsumeData(
357 kHeadersStreamId, MakeIOVector("foo"), 2, true, MAY_FEC_PROTECT, nullptr);
358 EXPECT_EQ(3u, consumed.bytes_consumed);
359 EXPECT_TRUE(consumed.fin_consumed);
360 EXPECT_TRUE(generator_.HasQueuedFrames());
363 TEST_F(QuicPacketGeneratorTest, ConsumeData_WritableAndShouldFlush) {
364 delegate_.SetCanWriteAnything();
366 EXPECT_CALL(delegate_, OnSerializedPacket(_)).WillOnce(SaveArg<0>(&packet_));
367 QuicConsumedData consumed = generator_.ConsumeData(
368 kHeadersStreamId, MakeIOVector("foo"), 2, true, MAY_FEC_PROTECT, nullptr);
369 EXPECT_EQ(3u, consumed.bytes_consumed);
370 EXPECT_TRUE(consumed.fin_consumed);
371 EXPECT_FALSE(generator_.HasQueuedFrames());
373 PacketContents contents;
374 contents.num_stream_frames = 1;
375 CheckPacketContains(contents, packet_);
378 TEST_F(QuicPacketGeneratorTest, ConsumeData_EmptyData) {
379 EXPECT_DFATAL(generator_.ConsumeData(kHeadersStreamId, MakeIOVector(""), 0,
380 false, MAY_FEC_PROTECT, nullptr),
381 "Attempt to consume empty data without FIN.");
384 TEST_F(QuicPacketGeneratorTest,
385 ConsumeDataMultipleTimes_WritableAndShouldNotFlush) {
386 delegate_.SetCanWriteAnything();
387 generator_.StartBatchOperations();
389 generator_.ConsumeData(kHeadersStreamId, MakeIOVector("foo"), 2, true,
390 MAY_FEC_PROTECT, nullptr);
391 QuicConsumedData consumed = generator_.ConsumeData(
392 3, MakeIOVector("quux"), 7, false, MAY_FEC_PROTECT, nullptr);
393 EXPECT_EQ(4u, consumed.bytes_consumed);
394 EXPECT_FALSE(consumed.fin_consumed);
395 EXPECT_TRUE(generator_.HasQueuedFrames());
398 TEST_F(QuicPacketGeneratorTest, ConsumeData_BatchOperations) {
399 delegate_.SetCanWriteAnything();
400 generator_.StartBatchOperations();
402 generator_.ConsumeData(kHeadersStreamId, MakeIOVector("foo"), 2, true,
403 MAY_FEC_PROTECT, nullptr);
404 QuicConsumedData consumed = generator_.ConsumeData(
405 3, MakeIOVector("quux"), 7, false, MAY_FEC_PROTECT, nullptr);
406 EXPECT_EQ(4u, consumed.bytes_consumed);
407 EXPECT_FALSE(consumed.fin_consumed);
408 EXPECT_TRUE(generator_.HasQueuedFrames());
410 // Now both frames will be flushed out.
411 EXPECT_CALL(delegate_, OnSerializedPacket(_)).WillOnce(SaveArg<0>(&packet_));
412 generator_.FinishBatchOperations();
413 EXPECT_FALSE(generator_.HasQueuedFrames());
415 PacketContents contents;
416 contents.num_stream_frames = 2;
417 CheckPacketContains(contents, packet_);
420 TEST_F(QuicPacketGeneratorTest, ConsumeDataSendsFecOnMaxGroupSize) {
421 delegate_.SetCanWriteAnything();
423 // Send FEC every two packets.
424 creator_->set_max_packets_per_fec_group(2);
427 InSequence dummy;
428 EXPECT_CALL(delegate_, OnSerializedPacket(_)).WillOnce(
429 SaveArg<0>(&packet_));
430 EXPECT_CALL(delegate_, OnSerializedPacket(_)).WillOnce(
431 SaveArg<0>(&packet2_));
432 EXPECT_CALL(delegate_, OnSerializedPacket(_)).WillOnce(
433 SaveArg<0>(&packet3_));
434 EXPECT_CALL(delegate_, OnSerializedPacket(_)).WillOnce(
435 SaveArg<0>(&packet4_));
438 // Send enough data to create 3 packets: two full and one partial. Send with
439 // MUST_FEC_PROTECT flag.
440 size_t data_len = 2 * kDefaultMaxPacketSize + 100;
441 QuicConsumedData consumed = generator_.ConsumeData(
442 3, CreateData(data_len), 0, true, MUST_FEC_PROTECT, nullptr);
443 EXPECT_EQ(data_len, consumed.bytes_consumed);
444 EXPECT_TRUE(consumed.fin_consumed);
445 EXPECT_FALSE(generator_.HasQueuedFrames());
447 CheckPacketHasSingleStreamFrame(packet_);
448 CheckPacketHasSingleStreamFrame(packet2_);
449 CheckPacketIsFec(packet3_, 1);
450 CheckPacketHasSingleStreamFrame(packet4_);
451 EXPECT_TRUE(creator_->IsFecProtected());
453 // The FEC packet under construction will be sent when one more packet is sent
454 // (since FEC group size is 2), or when OnFecTimeout is called. Send more data
455 // with MAY_FEC_PROTECT. This packet should also be protected, and FEC packet
456 // is sent since FEC group size is reached.
458 InSequence dummy;
459 EXPECT_CALL(delegate_, OnSerializedPacket(_))
460 .WillOnce(SaveArg<0>(&packet5_));
461 EXPECT_CALL(delegate_, OnSerializedPacket(_))
462 .WillOnce(SaveArg<0>(&packet6_));
464 consumed = generator_.ConsumeData(5, CreateData(1u), 0, true, MAY_FEC_PROTECT,
465 nullptr);
466 EXPECT_EQ(1u, consumed.bytes_consumed);
467 CheckPacketHasSingleStreamFrame(packet5_);
468 CheckPacketIsFec(packet6_, 4);
469 EXPECT_FALSE(creator_->IsFecProtected());
472 TEST_F(QuicPacketGeneratorTest, ConsumeDataSendsFecOnTimeout) {
473 delegate_.SetCanWriteAnything();
474 creator_->set_max_packets_per_fec_group(1000);
476 // Send data with MUST_FEC_PROTECT flag. No FEC packet is emitted, but the
477 // creator FEC protects all data.
478 EXPECT_CALL(delegate_, OnSerializedPacket(_)).WillOnce(SaveArg<0>(&packet_));
479 QuicConsumedData consumed = generator_.ConsumeData(3, CreateData(1u), 0, true,
480 MUST_FEC_PROTECT, nullptr);
481 EXPECT_EQ(1u, consumed.bytes_consumed);
482 EXPECT_TRUE(consumed.fin_consumed);
483 CheckPacketHasSingleStreamFrame(packet_);
484 EXPECT_TRUE(creator_->IsFecProtected());
486 // Send more data with MAY_FEC_PROTECT. This packet should also be protected,
487 // and FEC packet is not yet sent.
488 EXPECT_CALL(delegate_, OnSerializedPacket(_)).WillOnce(SaveArg<0>(&packet2_));
489 consumed = generator_.ConsumeData(5, CreateData(1u), 0, true, MAY_FEC_PROTECT,
490 nullptr);
491 EXPECT_EQ(1u, consumed.bytes_consumed);
492 CheckPacketHasSingleStreamFrame(packet2_);
493 EXPECT_TRUE(creator_->IsFecProtected());
495 // Calling OnFecTimeout should cause the FEC packet to be emitted.
496 EXPECT_CALL(delegate_, OnSerializedPacket(_)).WillOnce(SaveArg<0>(&packet3_));
497 generator_.OnFecTimeout();
498 CheckPacketIsFec(packet3_, 1);
499 EXPECT_FALSE(creator_->IsFecProtected());
501 // Subsequent data is protected under the next FEC group. Send enough data to
502 // create 2 more packets: one full and one partial.
504 InSequence dummy;
505 EXPECT_CALL(delegate_, OnSerializedPacket(_))
506 .WillOnce(SaveArg<0>(&packet4_));
507 EXPECT_CALL(delegate_, OnSerializedPacket(_))
508 .WillOnce(SaveArg<0>(&packet5_));
510 size_t data_len = kDefaultMaxPacketSize + 1;
511 consumed = generator_.ConsumeData(7, CreateData(data_len), 0, true,
512 MUST_FEC_PROTECT, nullptr);
513 EXPECT_EQ(data_len, consumed.bytes_consumed);
514 EXPECT_TRUE(consumed.fin_consumed);
515 CheckPacketHasSingleStreamFrame(packet4_);
516 CheckPacketHasSingleStreamFrame(packet5_);
517 EXPECT_TRUE(creator_->IsFecProtected());
519 // Calling OnFecTimeout should cause the FEC packet to be emitted.
520 EXPECT_CALL(delegate_, OnSerializedPacket(_)).WillOnce(SaveArg<0>(&packet6_));
521 generator_.OnFecTimeout();
522 CheckPacketIsFec(packet6_, 4);
523 EXPECT_FALSE(creator_->IsFecProtected());
526 TEST_F(QuicPacketGeneratorTest, GetFecTimeoutFiniteOnlyOnFirstPacketInGroup) {
527 delegate_.SetCanWriteAnything();
528 creator_->set_max_packets_per_fec_group(6);
530 // Send enough data to create 2 packets: one full and one partial. Send with
531 // MUST_FEC_PROTECT flag. No FEC packet is emitted yet, but the creator FEC
532 // protects all data.
534 InSequence dummy;
535 EXPECT_CALL(delegate_, OnSerializedPacket(_)).WillOnce(
536 SaveArg<0>(&packet_));
537 EXPECT_CALL(delegate_, OnSerializedPacket(_)).WillOnce(
538 SaveArg<0>(&packet2_));
540 size_t data_len = 1 * kDefaultMaxPacketSize + 100;
541 QuicConsumedData consumed = generator_.ConsumeData(
542 3, CreateData(data_len), 0, true, MUST_FEC_PROTECT, nullptr);
543 EXPECT_EQ(data_len, consumed.bytes_consumed);
544 EXPECT_TRUE(consumed.fin_consumed);
545 EXPECT_FALSE(generator_.HasQueuedFrames());
546 CheckPacketHasSingleStreamFrame(packet_);
547 CheckPacketHasSingleStreamFrame(packet2_);
548 EXPECT_TRUE(creator_->IsFecProtected());
550 // GetFecTimeout returns finite timeout only for first packet in group.
551 EXPECT_EQ(QuicTime::Delta::FromMilliseconds(kMinFecTimeoutMs),
552 generator_.GetFecTimeout(/*sequence_number=*/1u));
553 EXPECT_EQ(QuicTime::Delta::Infinite(),
554 generator_.GetFecTimeout(/*sequence_number=*/2u));
556 // Send more data with MAY_FEC_PROTECT. This packet should also be protected,
557 // and FEC packet is not yet sent.
558 EXPECT_CALL(delegate_, OnSerializedPacket(_)).WillOnce(SaveArg<0>(&packet3_));
559 consumed = generator_.ConsumeData(5, CreateData(1u), 0, true, MAY_FEC_PROTECT,
560 nullptr);
561 CheckPacketHasSingleStreamFrame(packet3_);
562 EXPECT_TRUE(creator_->IsFecProtected());
564 // GetFecTimeout returns finite timeout only for first packet in group.
565 EXPECT_EQ(QuicTime::Delta::Infinite(),
566 generator_.GetFecTimeout(/*sequence_number=*/3u));
568 // Calling OnFecTimeout should cause the FEC packet to be emitted.
569 EXPECT_CALL(delegate_, OnSerializedPacket(_)).WillOnce(SaveArg<0>(&packet4_));
570 generator_.OnFecTimeout();
571 CheckPacketIsFec(packet4_, /*fec_group=*/1u);
572 EXPECT_FALSE(creator_->IsFecProtected());
574 // Subsequent data is protected under the next FEC group. Send enough data to
575 // create 2 more packets: one full and one partial.
577 InSequence dummy;
578 EXPECT_CALL(delegate_, OnSerializedPacket(_))
579 .WillOnce(SaveArg<0>(&packet5_));
580 EXPECT_CALL(delegate_, OnSerializedPacket(_))
581 .WillOnce(SaveArg<0>(&packet6_));
583 data_len = kDefaultMaxPacketSize + 1u;
584 consumed = generator_.ConsumeData(7, CreateData(data_len), 0, true,
585 MUST_FEC_PROTECT, nullptr);
586 EXPECT_EQ(data_len, consumed.bytes_consumed);
587 EXPECT_TRUE(consumed.fin_consumed);
588 CheckPacketHasSingleStreamFrame(packet5_);
589 CheckPacketHasSingleStreamFrame(packet6_);
590 EXPECT_TRUE(creator_->IsFecProtected());
592 // GetFecTimeout returns finite timeout for first packet in the new group.
593 EXPECT_EQ(QuicTime::Delta::FromMilliseconds(kMinFecTimeoutMs),
594 generator_.GetFecTimeout(/*sequence_number=*/5u));
595 EXPECT_EQ(QuicTime::Delta::Infinite(),
596 generator_.GetFecTimeout(/*sequence_number=*/6u));
598 // Calling OnFecTimeout should cause the FEC packet to be emitted.
599 EXPECT_CALL(delegate_, OnSerializedPacket(_)).WillOnce(SaveArg<0>(&packet7_));
600 generator_.OnFecTimeout();
601 CheckPacketIsFec(packet7_, /*fec_group=*/5u);
602 EXPECT_FALSE(creator_->IsFecProtected());
604 // Send more data with MAY_FEC_PROTECT. No FEC protection, so GetFecTimeout
605 // returns infinite.
606 EXPECT_CALL(delegate_, OnSerializedPacket(_)).WillOnce(SaveArg<0>(&packet8_));
607 consumed = generator_.ConsumeData(9, CreateData(1u), 0, true, MAY_FEC_PROTECT,
608 nullptr);
609 CheckPacketHasSingleStreamFrame(packet8_);
610 EXPECT_FALSE(creator_->IsFecProtected());
611 EXPECT_EQ(QuicTime::Delta::Infinite(),
612 generator_.GetFecTimeout(/*sequence_number=*/8u));
615 TEST_F(QuicPacketGeneratorTest, ConsumeData_FramesPreviouslyQueued) {
616 // Set the packet size be enough for two stream frames with 0 stream offset,
617 // but not enough for a stream frame of 0 offset and one with non-zero offset.
618 size_t length =
619 NullEncrypter().GetCiphertextSize(0) +
620 GetPacketHeaderSize(
621 creator_->connection_id_length(), true,
622 QuicPacketCreatorPeer::NextSequenceNumberLength(creator_),
623 NOT_IN_FEC_GROUP) +
624 // Add an extra 3 bytes for the payload and 1 byte so BytesFree is larger
625 // than the GetMinStreamFrameSize.
626 QuicFramer::GetMinStreamFrameSize(1, 0, false, NOT_IN_FEC_GROUP) + 3 +
627 QuicFramer::GetMinStreamFrameSize(1, 0, true, NOT_IN_FEC_GROUP) + 1;
628 creator_->SetMaxPacketLength(length);
629 delegate_.SetCanWriteAnything();
631 InSequence dummy;
632 EXPECT_CALL(delegate_, OnSerializedPacket(_)).WillOnce(
633 SaveArg<0>(&packet_));
634 EXPECT_CALL(delegate_, OnSerializedPacket(_)).WillOnce(
635 SaveArg<0>(&packet2_));
637 generator_.StartBatchOperations();
638 // Queue enough data to prevent a stream frame with a non-zero offset from
639 // fitting.
640 QuicConsumedData consumed =
641 generator_.ConsumeData(kHeadersStreamId, MakeIOVector("foo"), 0, false,
642 MAY_FEC_PROTECT, nullptr);
643 EXPECT_EQ(3u, consumed.bytes_consumed);
644 EXPECT_FALSE(consumed.fin_consumed);
645 EXPECT_TRUE(generator_.HasQueuedFrames());
647 // This frame will not fit with the existing frame, causing the queued frame
648 // to be serialized, and it will not fit with another frame like it, so it is
649 // serialized by itself.
650 consumed = generator_.ConsumeData(kHeadersStreamId, MakeIOVector("bar"), 3,
651 true, MAY_FEC_PROTECT, nullptr);
652 EXPECT_EQ(3u, consumed.bytes_consumed);
653 EXPECT_TRUE(consumed.fin_consumed);
654 EXPECT_FALSE(generator_.HasQueuedFrames());
656 PacketContents contents;
657 contents.num_stream_frames = 1;
658 CheckPacketContains(contents, packet_);
659 CheckPacketContains(contents, packet2_);
662 TEST_F(QuicPacketGeneratorTest, NoFecPacketSentWhenBatchEnds) {
663 delegate_.SetCanWriteAnything();
664 creator_->set_max_packets_per_fec_group(6);
666 generator_.StartBatchOperations();
668 generator_.ConsumeData(3, MakeIOVector("foo"), 2, true, MUST_FEC_PROTECT,
669 nullptr);
670 QuicConsumedData consumed = generator_.ConsumeData(
671 5, MakeIOVector("quux"), 7, false, MUST_FEC_PROTECT, nullptr);
672 EXPECT_EQ(4u, consumed.bytes_consumed);
673 EXPECT_FALSE(consumed.fin_consumed);
674 EXPECT_TRUE(generator_.HasQueuedFrames());
676 // Now both frames will be flushed out, but FEC packet is not yet sent.
677 EXPECT_CALL(delegate_, OnSerializedPacket(_)).WillOnce(SaveArg<0>(&packet_));
678 generator_.FinishBatchOperations();
679 EXPECT_FALSE(generator_.HasQueuedFrames());
681 PacketContents contents;
682 contents.num_stream_frames = 2u;
683 contents.fec_group = 1u;
684 CheckPacketContains(contents, packet_);
686 // Forcing FEC timeout causes FEC packet to be emitted.
687 EXPECT_CALL(delegate_, OnSerializedPacket(_)).WillOnce(SaveArg<0>(&packet2_));
688 generator_.OnFecTimeout();
689 CheckPacketIsFec(packet2_, /*fec_group=*/1u);
692 TEST_F(QuicPacketGeneratorTest, FecTimeoutOnRttChange) {
693 EXPECT_EQ(QuicTime::Delta::Zero(),
694 QuicPacketGeneratorPeer::GetFecTimeout(&generator_));
695 generator_.OnRttChange(QuicTime::Delta::FromMilliseconds(300));
696 EXPECT_EQ(QuicTime::Delta::FromMilliseconds(150),
697 QuicPacketGeneratorPeer::GetFecTimeout(&generator_));
700 TEST_F(QuicPacketGeneratorTest, FecGroupSizeOnCongestionWindowChange) {
701 delegate_.SetCanWriteAnything();
702 creator_->set_max_packets_per_fec_group(50);
703 EXPECT_EQ(50u, creator_->max_packets_per_fec_group());
704 EXPECT_FALSE(creator_->IsFecGroupOpen());
706 // On reduced cwnd.
707 generator_.OnCongestionWindowChange(7);
708 EXPECT_EQ(3u, creator_->max_packets_per_fec_group());
710 // On increased cwnd.
711 generator_.OnCongestionWindowChange(100);
712 EXPECT_EQ(50u, creator_->max_packets_per_fec_group());
714 // On collapsed cwnd.
715 generator_.OnCongestionWindowChange(1);
716 EXPECT_EQ(2u, creator_->max_packets_per_fec_group());
719 TEST_F(QuicPacketGeneratorTest, FecGroupSizeChangeWithOpenGroup) {
720 delegate_.SetCanWriteAnything();
721 generator_.StartBatchOperations();
722 creator_->set_max_packets_per_fec_group(50);
723 EXPECT_EQ(50u, creator_->max_packets_per_fec_group());
724 EXPECT_FALSE(creator_->IsFecGroupOpen());
726 // Send enough data to create 4 packets with MUST_FEC_PROTECT flag. 3 packets
727 // are sent, one is queued in the creator.
729 InSequence dummy;
730 EXPECT_CALL(delegate_, OnSerializedPacket(_)).WillOnce(
731 SaveArg<0>(&packet_));
732 EXPECT_CALL(delegate_, OnSerializedPacket(_)).WillOnce(
733 SaveArg<0>(&packet2_));
734 EXPECT_CALL(delegate_, OnSerializedPacket(_)).WillOnce(
735 SaveArg<0>(&packet3_));
737 size_t data_len = 3 * kDefaultMaxPacketSize + 1;
738 QuicConsumedData consumed = generator_.ConsumeData(
739 7, CreateData(data_len), 0, true, MUST_FEC_PROTECT, nullptr);
740 EXPECT_EQ(data_len, consumed.bytes_consumed);
741 EXPECT_TRUE(creator_->IsFecGroupOpen());
743 // Change FEC groupsize.
744 generator_.OnCongestionWindowChange(2);
745 EXPECT_EQ(2u, creator_->max_packets_per_fec_group());
747 // Send enough data to trigger one unprotected data packet, causing the FEC
748 // packet to also be sent.
750 InSequence dummy;
751 EXPECT_CALL(delegate_, OnSerializedPacket(_)).WillOnce(
752 SaveArg<0>(&packet4_));
753 EXPECT_CALL(delegate_, OnSerializedPacket(_)).WillOnce(
754 SaveArg<0>(&packet5_));
756 consumed = generator_.ConsumeData(7, CreateData(kDefaultMaxPacketSize), 0,
757 true, MAY_FEC_PROTECT, nullptr);
758 EXPECT_EQ(kDefaultMaxPacketSize, consumed.bytes_consumed);
759 // Verify that one FEC packet was sent.
760 CheckPacketIsFec(packet5_, /*fec_group=*/1u);
761 EXPECT_FALSE(creator_->IsFecGroupOpen());
762 EXPECT_FALSE(creator_->IsFecProtected());
765 TEST_F(QuicPacketGeneratorTest, SwitchFecOnOff) {
766 delegate_.SetCanWriteAnything();
767 creator_->set_max_packets_per_fec_group(2);
768 EXPECT_FALSE(creator_->IsFecProtected());
770 // Send one unprotected data packet.
771 EXPECT_CALL(delegate_, OnSerializedPacket(_)).WillOnce(
772 SaveArg<0>(&packet_));
773 QuicConsumedData consumed = generator_.ConsumeData(5, CreateData(1u), 0, true,
774 MAY_FEC_PROTECT, nullptr);
775 EXPECT_EQ(1u, consumed.bytes_consumed);
776 EXPECT_FALSE(generator_.HasQueuedFrames());
777 EXPECT_FALSE(creator_->IsFecProtected());
778 // Verify that one data packet was sent.
779 PacketContents contents;
780 contents.num_stream_frames = 1;
781 CheckPacketContains(contents, packet_);
784 InSequence dummy;
785 EXPECT_CALL(delegate_, OnSerializedPacket(_)).WillOnce(
786 SaveArg<0>(&packet2_));
787 EXPECT_CALL(delegate_, OnSerializedPacket(_)).WillOnce(
788 SaveArg<0>(&packet3_));
789 EXPECT_CALL(delegate_, OnSerializedPacket(_)).WillOnce(
790 SaveArg<0>(&packet4_));
791 EXPECT_CALL(delegate_, OnSerializedPacket(_)).WillOnce(
792 SaveArg<0>(&packet5_));
794 // Send enough data to create 3 packets with MUST_FEC_PROTECT flag.
795 size_t data_len = 2 * kDefaultMaxPacketSize + 100;
796 consumed = generator_.ConsumeData(7, CreateData(data_len), 0, true,
797 MUST_FEC_PROTECT, nullptr);
798 EXPECT_EQ(data_len, consumed.bytes_consumed);
799 EXPECT_FALSE(generator_.HasQueuedFrames());
801 // Verify that packets sent were 3 data and 1 FEC.
802 CheckPacketHasSingleStreamFrame(packet2_);
803 CheckPacketHasSingleStreamFrame(packet3_);
804 CheckPacketIsFec(packet4_, /*fec_group=*/2u);
805 CheckPacketHasSingleStreamFrame(packet5_);
807 // Calling OnFecTimeout should emit the pending FEC packet.
808 EXPECT_CALL(delegate_, OnSerializedPacket(_)).WillOnce(SaveArg<0>(&packet6_));
809 generator_.OnFecTimeout();
810 CheckPacketIsFec(packet6_, /*fec_group=*/5u);
812 // Send one unprotected data packet.
813 EXPECT_CALL(delegate_, OnSerializedPacket(_)).WillOnce(
814 SaveArg<0>(&packet7_));
815 consumed = generator_.ConsumeData(7, CreateData(1u), 0, true, MAY_FEC_PROTECT,
816 nullptr);
817 EXPECT_EQ(1u, consumed.bytes_consumed);
818 EXPECT_FALSE(generator_.HasQueuedFrames());
819 EXPECT_FALSE(creator_->IsFecProtected());
820 // Verify that one unprotected data packet was sent.
821 CheckPacketContains(contents, packet7_);
824 TEST_F(QuicPacketGeneratorTest, SwitchFecOnWithPendingFrameInCreator) {
825 delegate_.SetCanWriteAnything();
826 // Enable FEC.
827 creator_->set_max_packets_per_fec_group(2);
829 generator_.StartBatchOperations();
830 // Queue enough data to prevent a stream frame with a non-zero offset from
831 // fitting.
832 QuicConsumedData consumed = generator_.ConsumeData(7, CreateData(1u), 0, true,
833 MAY_FEC_PROTECT, nullptr);
834 EXPECT_EQ(1u, consumed.bytes_consumed);
835 EXPECT_TRUE(creator_->HasPendingFrames());
837 // Queue protected data for sending. Should cause queued frames to be flushed.
838 EXPECT_CALL(delegate_, OnSerializedPacket(_)).WillOnce(
839 SaveArg<0>(&packet_));
840 EXPECT_FALSE(creator_->IsFecProtected());
841 consumed = generator_.ConsumeData(7, CreateData(1u), 0, true,
842 MUST_FEC_PROTECT, nullptr);
843 EXPECT_EQ(1u, consumed.bytes_consumed);
844 PacketContents contents;
845 contents.num_stream_frames = 1;
846 // Transmitted packet was not FEC protected.
847 CheckPacketContains(contents, packet_);
848 EXPECT_TRUE(creator_->IsFecProtected());
849 EXPECT_TRUE(creator_->HasPendingFrames());
852 TEST_F(QuicPacketGeneratorTest, SwitchFecOnWithPendingFramesInGenerator) {
853 // Enable FEC.
854 creator_->set_max_packets_per_fec_group(2);
856 // Queue control frames in generator.
857 delegate_.SetCanNotWrite();
858 generator_.SetShouldSendAck(true);
859 delegate_.SetCanWriteAnything();
860 generator_.StartBatchOperations();
862 // Set up frames to write into the creator when control frames are written.
863 EXPECT_CALL(delegate_, PopulateAckFrame(_));
864 EXPECT_CALL(delegate_, PopulateStopWaitingFrame(_));
866 // Generator should have queued control frames, and creator should be empty.
867 EXPECT_TRUE(generator_.HasQueuedFrames());
868 EXPECT_FALSE(creator_->HasPendingFrames());
869 EXPECT_FALSE(creator_->IsFecProtected());
871 // Queue protected data for sending. Should cause queued frames to be flushed.
872 EXPECT_CALL(delegate_, OnSerializedPacket(_)).WillOnce(
873 SaveArg<0>(&packet_));
874 QuicConsumedData consumed = generator_.ConsumeData(7, CreateData(1u), 0, true,
875 MUST_FEC_PROTECT, nullptr);
876 EXPECT_EQ(1u, consumed.bytes_consumed);
877 PacketContents contents;
878 contents.num_ack_frames = 1;
879 contents.num_stop_waiting_frames = 1;
880 CheckPacketContains(contents, packet_);
882 // FEC protection should be on in creator.
883 EXPECT_TRUE(creator_->IsFecProtected());
886 TEST_F(QuicPacketGeneratorTest, SwitchFecOnOffWithSubsequentFramesProtected) {
887 delegate_.SetCanWriteAnything();
889 // Enable FEC.
890 creator_->set_max_packets_per_fec_group(2);
891 EXPECT_FALSE(creator_->IsFecProtected());
893 // Queue stream frame to be protected in creator.
894 generator_.StartBatchOperations();
895 QuicConsumedData consumed = generator_.ConsumeData(5, CreateData(1u), 0, true,
896 MUST_FEC_PROTECT, nullptr);
897 EXPECT_EQ(1u, consumed.bytes_consumed);
898 // Creator has a pending protected frame.
899 EXPECT_TRUE(creator_->HasPendingFrames());
900 EXPECT_TRUE(creator_->IsFecProtected());
902 // Add enough unprotected data to exceed size of current packet, so that
903 // current packet is sent. Both frames will be sent out in a single packet.
904 EXPECT_CALL(delegate_, OnSerializedPacket(_)).WillOnce(SaveArg<0>(&packet_));
905 size_t data_len = kDefaultMaxPacketSize;
906 consumed = generator_.ConsumeData(5, CreateData(data_len), 0, true,
907 MAY_FEC_PROTECT, nullptr);
908 EXPECT_EQ(data_len, consumed.bytes_consumed);
909 PacketContents contents;
910 contents.num_stream_frames = 2u;
911 contents.fec_group = 1u;
912 CheckPacketContains(contents, packet_);
913 // FEC protection should still be on in creator.
914 EXPECT_TRUE(creator_->IsFecProtected());
917 TEST_F(QuicPacketGeneratorTest, SwitchFecOnOffWithSubsequentPacketsProtected) {
918 delegate_.SetCanWriteAnything();
920 // Enable FEC.
921 creator_->set_max_packets_per_fec_group(2);
922 EXPECT_FALSE(creator_->IsFecProtected());
924 // Send first packet, FEC protected.
925 EXPECT_CALL(delegate_, OnSerializedPacket(_)).WillOnce(SaveArg<0>(&packet_));
926 QuicConsumedData consumed = generator_.ConsumeData(5, CreateData(1u), 0, true,
927 MUST_FEC_PROTECT, nullptr);
928 EXPECT_EQ(1u, consumed.bytes_consumed);
929 PacketContents contents;
930 contents.num_stream_frames = 1u;
931 contents.fec_group = 1u;
932 CheckPacketContains(contents, packet_);
934 // FEC should still be on in creator.
935 EXPECT_TRUE(creator_->IsFecProtected());
937 // Send unprotected data to cause second packet to be sent, which gets
938 // protected because it happens to fall within an open FEC group. Data packet
939 // will be followed by FEC packet.
941 InSequence dummy;
942 EXPECT_CALL(delegate_, OnSerializedPacket(_)).WillOnce(
943 SaveArg<0>(&packet2_));
944 EXPECT_CALL(delegate_, OnSerializedPacket(_)).WillOnce(
945 SaveArg<0>(&packet3_));
947 consumed = generator_.ConsumeData(5, CreateData(1u), 0, true, MAY_FEC_PROTECT,
948 nullptr);
949 EXPECT_EQ(1u, consumed.bytes_consumed);
950 contents.num_stream_frames = 1u;
951 CheckPacketContains(contents, packet2_);
952 CheckPacketIsFec(packet3_, /*fec_group=*/1u);
954 // FEC protection should be off in creator.
955 EXPECT_FALSE(creator_->IsFecProtected());
958 TEST_F(QuicPacketGeneratorTest, SwitchFecOnOffThenOnWithCreatorProtectionOn) {
959 delegate_.SetCanWriteAnything();
960 generator_.StartBatchOperations();
962 // Enable FEC.
963 creator_->set_max_packets_per_fec_group(2);
964 EXPECT_FALSE(creator_->IsFecProtected());
966 // Queue one byte of FEC protected data.
967 QuicConsumedData consumed = generator_.ConsumeData(5, CreateData(1u), 0, true,
968 MUST_FEC_PROTECT, nullptr);
969 EXPECT_TRUE(creator_->HasPendingFrames());
971 // Add more unprotected data causing first packet to be sent, FEC protected.
972 EXPECT_CALL(delegate_, OnSerializedPacket(_)).WillOnce(
973 SaveArg<0>(&packet_));
974 size_t data_len = kDefaultMaxPacketSize;
975 consumed = generator_.ConsumeData(5, CreateData(data_len), 0, true,
976 MAY_FEC_PROTECT, nullptr);
977 EXPECT_EQ(data_len, consumed.bytes_consumed);
978 PacketContents contents;
979 contents.num_stream_frames = 2u;
980 contents.fec_group = 1u;
981 CheckPacketContains(contents, packet_);
983 // FEC group is still open in creator.
984 EXPECT_TRUE(creator_->IsFecProtected());
986 // Add data that should be protected, large enough to cause second packet to
987 // be sent. Data packet should be followed by FEC packet.
989 InSequence dummy;
990 EXPECT_CALL(delegate_, OnSerializedPacket(_)).WillOnce(
991 SaveArg<0>(&packet2_));
992 EXPECT_CALL(delegate_, OnSerializedPacket(_)).WillOnce(
993 SaveArg<0>(&packet3_));
995 consumed = generator_.ConsumeData(5, CreateData(data_len), 0, true,
996 MUST_FEC_PROTECT, nullptr);
997 EXPECT_EQ(data_len, consumed.bytes_consumed);
998 CheckPacketContains(contents, packet2_);
999 CheckPacketIsFec(packet3_, /*fec_group=*/1u);
1001 // FEC protection should remain on in creator.
1002 EXPECT_TRUE(creator_->IsFecProtected());
1005 TEST_F(QuicPacketGeneratorTest, NotWritableThenBatchOperations) {
1006 delegate_.SetCanNotWrite();
1008 generator_.SetShouldSendAck(false);
1009 generator_.AddControlFrame(QuicFrame(CreateRstStreamFrame()));
1010 EXPECT_TRUE(generator_.HasQueuedFrames());
1012 delegate_.SetCanWriteAnything();
1014 generator_.StartBatchOperations();
1016 // When the first write operation is invoked, the ack frame will be returned.
1017 EXPECT_CALL(delegate_, PopulateAckFrame(_));
1019 // Send some data and a control frame
1020 generator_.ConsumeData(3, MakeIOVector("quux"), 7, false, MAY_FEC_PROTECT,
1021 nullptr);
1022 generator_.AddControlFrame(QuicFrame(CreateGoAwayFrame()));
1024 // All five frames will be flushed out in a single packet.
1025 EXPECT_CALL(delegate_, OnSerializedPacket(_)).WillOnce(SaveArg<0>(&packet_));
1026 generator_.FinishBatchOperations();
1027 EXPECT_FALSE(generator_.HasQueuedFrames());
1029 PacketContents contents;
1030 contents.num_ack_frames = 1;
1031 contents.num_goaway_frames = 1;
1032 contents.num_rst_stream_frames = 1;
1033 contents.num_stream_frames = 1;
1034 CheckPacketContains(contents, packet_);
1037 TEST_F(QuicPacketGeneratorTest, NotWritableThenBatchOperations2) {
1038 delegate_.SetCanNotWrite();
1040 generator_.SetShouldSendAck(false);
1041 generator_.AddControlFrame(QuicFrame(CreateRstStreamFrame()));
1042 EXPECT_TRUE(generator_.HasQueuedFrames());
1044 delegate_.SetCanWriteAnything();
1046 generator_.StartBatchOperations();
1048 // When the first write operation is invoked, the ack frame will be returned.
1049 EXPECT_CALL(delegate_, PopulateAckFrame(_));
1052 InSequence dummy;
1053 // All five frames will be flushed out in a single packet
1054 EXPECT_CALL(delegate_, OnSerializedPacket(_)).WillOnce(
1055 SaveArg<0>(&packet_));
1056 EXPECT_CALL(delegate_, OnSerializedPacket(_)).WillOnce(
1057 SaveArg<0>(&packet2_));
1060 // Send enough data to exceed one packet
1061 size_t data_len = kDefaultMaxPacketSize + 100;
1062 QuicConsumedData consumed = generator_.ConsumeData(
1063 3, CreateData(data_len), 0, true, MAY_FEC_PROTECT, nullptr);
1064 EXPECT_EQ(data_len, consumed.bytes_consumed);
1065 EXPECT_TRUE(consumed.fin_consumed);
1066 generator_.AddControlFrame(QuicFrame(CreateGoAwayFrame()));
1068 generator_.FinishBatchOperations();
1069 EXPECT_FALSE(generator_.HasQueuedFrames());
1071 // The first packet should have the queued data and part of the stream data.
1072 PacketContents contents;
1073 contents.num_ack_frames = 1;
1074 contents.num_rst_stream_frames = 1;
1075 contents.num_stream_frames = 1;
1076 CheckPacketContains(contents, packet_);
1078 // The second should have the remainder of the stream data.
1079 PacketContents contents2;
1080 contents2.num_goaway_frames = 1;
1081 contents2.num_stream_frames = 1;
1082 CheckPacketContains(contents2, packet2_);
1085 TEST_F(QuicPacketGeneratorTest, TestConnectionIdLength) {
1086 generator_.SetConnectionIdLength(0);
1087 EXPECT_EQ(PACKET_0BYTE_CONNECTION_ID, creator_->connection_id_length());
1088 generator_.SetConnectionIdLength(1);
1089 EXPECT_EQ(PACKET_1BYTE_CONNECTION_ID, creator_->connection_id_length());
1090 generator_.SetConnectionIdLength(2);
1091 EXPECT_EQ(PACKET_4BYTE_CONNECTION_ID, creator_->connection_id_length());
1092 generator_.SetConnectionIdLength(3);
1093 EXPECT_EQ(PACKET_4BYTE_CONNECTION_ID, creator_->connection_id_length());
1094 generator_.SetConnectionIdLength(4);
1095 EXPECT_EQ(PACKET_4BYTE_CONNECTION_ID, creator_->connection_id_length());
1096 generator_.SetConnectionIdLength(5);
1097 EXPECT_EQ(PACKET_8BYTE_CONNECTION_ID, creator_->connection_id_length());
1098 generator_.SetConnectionIdLength(6);
1099 EXPECT_EQ(PACKET_8BYTE_CONNECTION_ID, creator_->connection_id_length());
1100 generator_.SetConnectionIdLength(7);
1101 EXPECT_EQ(PACKET_8BYTE_CONNECTION_ID, creator_->connection_id_length());
1102 generator_.SetConnectionIdLength(8);
1103 EXPECT_EQ(PACKET_8BYTE_CONNECTION_ID, creator_->connection_id_length());
1104 generator_.SetConnectionIdLength(9);
1105 EXPECT_EQ(PACKET_8BYTE_CONNECTION_ID, creator_->connection_id_length());
1108 } // namespace test
1109 } // namespace net