Don't show supervised user as "already on this device" while they're being imported.
[chromium-blink-merge.git] / net / quic / quic_packet_generator_test.cc
blob21e186def677c168c3864517a254eba5e7388b9f
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::SaveArg;
29 using testing::StrictMock;
30 using testing::_;
32 namespace net {
33 namespace test {
34 namespace {
36 const int64 kMinFecTimeoutMs = 5u;
38 class MockDelegate : public QuicPacketGenerator::DelegateInterface {
39 public:
40 MockDelegate() {}
41 ~MockDelegate() override {}
43 MOCK_METHOD2(ShouldGeneratePacket,
44 bool(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(_, _)).WillRepeatedly(Return(true));
53 EXPECT_CALL(*this, ShouldGeneratePacket(NO_RETRANSMITTABLE_DATA, _))
54 .WillRepeatedly(Return(true));
57 void SetCanNotWrite() {
58 EXPECT_CALL(*this, ShouldGeneratePacket(_, _))
59 .WillRepeatedly(Return(false));
60 EXPECT_CALL(*this, ShouldGeneratePacket(NO_RETRANSMITTABLE_DATA, _))
61 .WillRepeatedly(Return(false));
64 // Use this when only ack frames should be allowed to be written.
65 void SetCanWriteOnlyNonRetransmittable() {
66 EXPECT_CALL(*this, ShouldGeneratePacket(_, _))
67 .WillRepeatedly(Return(false));
68 EXPECT_CALL(*this, ShouldGeneratePacket(NO_RETRANSMITTABLE_DATA, _))
69 .WillRepeatedly(Return(true));
72 private:
73 DISALLOW_COPY_AND_ASSIGN(MockDelegate);
76 // Simple struct for describing the contents of a packet.
77 // Useful in conjunction with a SimpleQuicFrame for validating that a packet
78 // contains the expected frames.
79 struct PacketContents {
80 PacketContents()
81 : num_ack_frames(0),
82 num_connection_close_frames(0),
83 num_goaway_frames(0),
84 num_rst_stream_frames(0),
85 num_stop_waiting_frames(0),
86 num_stream_frames(0),
87 fec_group(0) {
90 size_t num_ack_frames;
91 size_t num_connection_close_frames;
92 size_t num_goaway_frames;
93 size_t num_rst_stream_frames;
94 size_t num_stop_waiting_frames;
95 size_t num_stream_frames;
97 QuicFecGroupNumber fec_group;
100 } // namespace
102 class QuicPacketGeneratorTest : public ::testing::Test {
103 public:
104 QuicPacketGeneratorTest()
105 : framer_(QuicSupportedVersions(),
106 QuicTime::Zero(),
107 Perspective::IS_CLIENT),
108 generator_(42, &framer_, &random_, &delegate_),
109 creator_(QuicPacketGeneratorPeer::GetPacketCreator(&generator_)) {}
111 ~QuicPacketGeneratorTest() override {
112 for (SerializedPacket& packet : packets_) {
113 delete packet.packet;
114 delete packet.retransmittable_frames;
118 void SavePacket(const SerializedPacket& packet) {
119 packets_.push_back(packet);
120 ASSERT_FALSE(packet.packet->owns_buffer());
121 scoped_ptr<QuicEncryptedPacket> encrypted_deleter(packets_.back().packet);
122 packets_.back().packet = packets_.back().packet->Clone();
125 protected:
126 QuicRstStreamFrame* CreateRstStreamFrame() {
127 return new QuicRstStreamFrame(1, QUIC_STREAM_NO_ERROR, 0);
130 QuicGoAwayFrame* CreateGoAwayFrame() {
131 return new QuicGoAwayFrame(QUIC_NO_ERROR, 1, string());
134 void CheckPacketContains(const PacketContents& contents,
135 size_t packet_index) {
136 ASSERT_GT(packets_.size(), packet_index);
137 const SerializedPacket& packet = packets_[packet_index];
138 size_t num_retransmittable_frames = contents.num_connection_close_frames +
139 contents.num_goaway_frames + contents.num_rst_stream_frames +
140 contents.num_stream_frames;
141 size_t num_frames = contents.num_ack_frames +
142 contents.num_stop_waiting_frames +
143 num_retransmittable_frames;
145 if (num_retransmittable_frames == 0) {
146 ASSERT_TRUE(packet.retransmittable_frames == nullptr);
147 } else {
148 ASSERT_TRUE(packet.retransmittable_frames != nullptr);
149 EXPECT_EQ(num_retransmittable_frames,
150 packet.retransmittable_frames->frames().size());
153 ASSERT_TRUE(packet.packet != nullptr);
154 ASSERT_TRUE(simple_framer_.ProcessPacket(*packet.packet));
155 EXPECT_EQ(num_frames, simple_framer_.num_frames());
156 EXPECT_EQ(contents.num_ack_frames, simple_framer_.ack_frames().size());
157 EXPECT_EQ(contents.num_connection_close_frames,
158 simple_framer_.connection_close_frames().size());
159 EXPECT_EQ(contents.num_goaway_frames,
160 simple_framer_.goaway_frames().size());
161 EXPECT_EQ(contents.num_rst_stream_frames,
162 simple_framer_.rst_stream_frames().size());
163 EXPECT_EQ(contents.num_stream_frames,
164 simple_framer_.stream_frames().size());
165 EXPECT_EQ(contents.num_stop_waiting_frames,
166 simple_framer_.stop_waiting_frames().size());
167 EXPECT_EQ(contents.fec_group, simple_framer_.header().fec_group);
170 void CheckPacketHasSingleStreamFrame(size_t packet_index) {
171 ASSERT_GT(packets_.size(), packet_index);
172 const SerializedPacket& packet = packets_[packet_index];
173 ASSERT_TRUE(packet.retransmittable_frames != nullptr);
174 EXPECT_EQ(1u, packet.retransmittable_frames->frames().size());
175 ASSERT_TRUE(packet.packet != nullptr);
176 ASSERT_TRUE(simple_framer_.ProcessPacket(*packet.packet));
177 EXPECT_EQ(1u, simple_framer_.num_frames());
178 EXPECT_EQ(1u, simple_framer_.stream_frames().size());
181 void CheckPacketIsFec(size_t packet_index,
182 QuicPacketSequenceNumber fec_group) {
183 ASSERT_GT(packets_.size(), packet_index);
184 const SerializedPacket& packet = packets_[packet_index];
185 ASSERT_TRUE(packet.retransmittable_frames == nullptr);
186 ASSERT_TRUE(packet.packet != nullptr);
187 ASSERT_TRUE(simple_framer_.ProcessPacket(*packet.packet));
188 EXPECT_TRUE(simple_framer_.header().fec_flag);
189 EXPECT_EQ(fec_group, simple_framer_.fec_data().fec_group);
192 IOVector CreateData(size_t len) {
193 data_array_.reset(new char[len]);
194 memset(data_array_.get(), '?', len);
195 IOVector data;
196 data.Append(data_array_.get(), len);
197 return data;
200 QuicFramer framer_;
201 MockRandom random_;
202 StrictMock<MockDelegate> delegate_;
203 QuicPacketGenerator generator_;
204 QuicPacketCreator* creator_;
205 SimpleQuicFramer simple_framer_;
206 vector<SerializedPacket> packets_;
208 private:
209 scoped_ptr<char[]> data_array_;
212 class MockDebugDelegate : public QuicPacketGenerator::DebugDelegate {
213 public:
214 MOCK_METHOD1(OnFrameAddedToPacket,
215 void(const QuicFrame&));
218 TEST_F(QuicPacketGeneratorTest, ShouldSendAck_NotWritable) {
219 delegate_.SetCanNotWrite();
221 generator_.SetShouldSendAck(false);
222 EXPECT_TRUE(generator_.HasQueuedFrames());
225 TEST_F(QuicPacketGeneratorTest, ShouldSendAck_WritableAndShouldNotFlush) {
226 StrictMock<MockDebugDelegate> debug_delegate;
228 generator_.set_debug_delegate(&debug_delegate);
229 delegate_.SetCanWriteOnlyNonRetransmittable();
230 generator_.StartBatchOperations();
232 EXPECT_CALL(delegate_, PopulateAckFrame(_));
233 EXPECT_CALL(debug_delegate, OnFrameAddedToPacket(_)).Times(1);
235 generator_.SetShouldSendAck(false);
236 EXPECT_TRUE(generator_.HasQueuedFrames());
239 TEST_F(QuicPacketGeneratorTest, ShouldSendAck_WritableAndShouldFlush) {
240 delegate_.SetCanWriteOnlyNonRetransmittable();
242 EXPECT_CALL(delegate_, PopulateAckFrame(_));
243 EXPECT_CALL(delegate_, OnSerializedPacket(_))
244 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
246 generator_.SetShouldSendAck(false);
247 EXPECT_FALSE(generator_.HasQueuedFrames());
249 PacketContents contents;
250 contents.num_ack_frames = 1;
251 CheckPacketContains(contents, 0);
254 TEST_F(QuicPacketGeneratorTest, ShouldSendAck_MultipleCalls) {
255 // Make sure that calling SetShouldSendAck multiple times does not result in a
256 // crash. Previously this would result in multiple QuicFrames queued in the
257 // packet generator, with all but the last with internal pointers to freed
258 // memory.
259 delegate_.SetCanWriteAnything();
261 // Only one AckFrame should be created.
262 EXPECT_CALL(delegate_, PopulateAckFrame(_)).Times(1);
263 EXPECT_CALL(delegate_, OnSerializedPacket(_))
264 .Times(1)
265 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
267 generator_.StartBatchOperations();
268 generator_.SetShouldSendAck(false);
269 generator_.SetShouldSendAck(false);
270 generator_.FinishBatchOperations();
273 TEST_F(QuicPacketGeneratorTest, AddControlFrame_NotWritable) {
274 delegate_.SetCanNotWrite();
276 generator_.AddControlFrame(QuicFrame(CreateRstStreamFrame()));
277 EXPECT_TRUE(generator_.HasQueuedFrames());
280 TEST_F(QuicPacketGeneratorTest, AddControlFrame_OnlyAckWritable) {
281 delegate_.SetCanWriteOnlyNonRetransmittable();
283 generator_.AddControlFrame(QuicFrame(CreateRstStreamFrame()));
284 EXPECT_TRUE(generator_.HasQueuedFrames());
287 TEST_F(QuicPacketGeneratorTest, AddControlFrame_WritableAndShouldNotFlush) {
288 delegate_.SetCanWriteAnything();
289 generator_.StartBatchOperations();
291 generator_.AddControlFrame(QuicFrame(CreateRstStreamFrame()));
292 EXPECT_TRUE(generator_.HasQueuedFrames());
295 TEST_F(QuicPacketGeneratorTest, AddControlFrame_NotWritableBatchThenFlush) {
296 delegate_.SetCanNotWrite();
297 generator_.StartBatchOperations();
299 generator_.AddControlFrame(QuicFrame(CreateRstStreamFrame()));
300 EXPECT_TRUE(generator_.HasQueuedFrames());
301 generator_.FinishBatchOperations();
302 EXPECT_TRUE(generator_.HasQueuedFrames());
304 EXPECT_CALL(delegate_, OnSerializedPacket(_))
305 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
306 generator_.FlushAllQueuedFrames();
307 EXPECT_FALSE(generator_.HasQueuedFrames());
309 PacketContents contents;
310 contents.num_rst_stream_frames = 1;
311 CheckPacketContains(contents, 0);
314 TEST_F(QuicPacketGeneratorTest, AddControlFrame_WritableAndShouldFlush) {
315 delegate_.SetCanWriteAnything();
317 EXPECT_CALL(delegate_, OnSerializedPacket(_))
318 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
320 generator_.AddControlFrame(QuicFrame(CreateRstStreamFrame()));
321 EXPECT_FALSE(generator_.HasQueuedFrames());
323 PacketContents contents;
324 contents.num_rst_stream_frames = 1;
325 CheckPacketContains(contents, 0);
328 TEST_F(QuicPacketGeneratorTest, ConsumeData_NotWritable) {
329 delegate_.SetCanNotWrite();
331 QuicConsumedData consumed = generator_.ConsumeData(
332 kHeadersStreamId, MakeIOVector("foo"), 2, true, MAY_FEC_PROTECT, nullptr);
333 EXPECT_EQ(0u, consumed.bytes_consumed);
334 EXPECT_FALSE(consumed.fin_consumed);
335 EXPECT_FALSE(generator_.HasQueuedFrames());
338 TEST_F(QuicPacketGeneratorTest, ConsumeData_WritableAndShouldNotFlush) {
339 delegate_.SetCanWriteAnything();
340 generator_.StartBatchOperations();
342 QuicConsumedData consumed = generator_.ConsumeData(
343 kHeadersStreamId, MakeIOVector("foo"), 2, true, MAY_FEC_PROTECT, nullptr);
344 EXPECT_EQ(3u, consumed.bytes_consumed);
345 EXPECT_TRUE(consumed.fin_consumed);
346 EXPECT_TRUE(generator_.HasQueuedFrames());
349 TEST_F(QuicPacketGeneratorTest, ConsumeData_WritableAndShouldFlush) {
350 delegate_.SetCanWriteAnything();
352 EXPECT_CALL(delegate_, OnSerializedPacket(_))
353 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
354 QuicConsumedData consumed = generator_.ConsumeData(
355 kHeadersStreamId, MakeIOVector("foo"), 2, true, MAY_FEC_PROTECT, nullptr);
356 EXPECT_EQ(3u, consumed.bytes_consumed);
357 EXPECT_TRUE(consumed.fin_consumed);
358 EXPECT_FALSE(generator_.HasQueuedFrames());
360 PacketContents contents;
361 contents.num_stream_frames = 1;
362 CheckPacketContains(contents, 0);
365 TEST_F(QuicPacketGeneratorTest, ConsumeData_EmptyData) {
366 EXPECT_DFATAL(generator_.ConsumeData(kHeadersStreamId, MakeIOVector(""), 0,
367 false, MAY_FEC_PROTECT, nullptr),
368 "Attempt to consume empty data without FIN.");
371 TEST_F(QuicPacketGeneratorTest,
372 ConsumeDataMultipleTimes_WritableAndShouldNotFlush) {
373 delegate_.SetCanWriteAnything();
374 generator_.StartBatchOperations();
376 generator_.ConsumeData(kHeadersStreamId, MakeIOVector("foo"), 2, true,
377 MAY_FEC_PROTECT, nullptr);
378 QuicConsumedData consumed = generator_.ConsumeData(
379 3, MakeIOVector("quux"), 7, false, MAY_FEC_PROTECT, nullptr);
380 EXPECT_EQ(4u, consumed.bytes_consumed);
381 EXPECT_FALSE(consumed.fin_consumed);
382 EXPECT_TRUE(generator_.HasQueuedFrames());
385 TEST_F(QuicPacketGeneratorTest, ConsumeData_BatchOperations) {
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());
397 // Now both frames will be flushed out.
398 EXPECT_CALL(delegate_, OnSerializedPacket(_))
399 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
400 generator_.FinishBatchOperations();
401 EXPECT_FALSE(generator_.HasQueuedFrames());
403 PacketContents contents;
404 contents.num_stream_frames = 2;
405 CheckPacketContains(contents, 0);
408 TEST_F(QuicPacketGeneratorTest, ConsumeDataSendsFecOnMaxGroupSize) {
409 delegate_.SetCanWriteAnything();
411 // Send FEC every two packets.
412 creator_->set_max_packets_per_fec_group(2);
415 InSequence dummy;
416 EXPECT_CALL(delegate_, OnSerializedPacket(_))
417 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
418 EXPECT_CALL(delegate_, OnSerializedPacket(_))
419 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
420 EXPECT_CALL(delegate_, OnSerializedPacket(_))
421 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
422 EXPECT_CALL(delegate_, OnSerializedPacket(_))
423 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
426 // Send enough data to create 3 packets: two full and one partial. Send with
427 // MUST_FEC_PROTECT flag.
428 size_t data_len = 2 * kDefaultMaxPacketSize + 100;
429 QuicConsumedData consumed = generator_.ConsumeData(
430 3, CreateData(data_len), 0, true, MUST_FEC_PROTECT, nullptr);
431 EXPECT_EQ(data_len, consumed.bytes_consumed);
432 EXPECT_TRUE(consumed.fin_consumed);
433 EXPECT_FALSE(generator_.HasQueuedFrames());
435 CheckPacketHasSingleStreamFrame(0);
436 CheckPacketHasSingleStreamFrame(1);
437 CheckPacketIsFec(2, 1);
438 CheckPacketHasSingleStreamFrame(3);
439 EXPECT_TRUE(creator_->IsFecProtected());
441 // The FEC packet under construction will be sent when one more packet is sent
442 // (since FEC group size is 2), or when OnFecTimeout is called. Send more data
443 // with MAY_FEC_PROTECT. This packet should also be protected, and FEC packet
444 // is sent since FEC group size is reached.
446 InSequence dummy;
447 EXPECT_CALL(delegate_, OnSerializedPacket(_))
448 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
449 EXPECT_CALL(delegate_, OnSerializedPacket(_))
450 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
452 consumed = generator_.ConsumeData(5, CreateData(1u), 0, true, MAY_FEC_PROTECT,
453 nullptr);
454 EXPECT_EQ(1u, consumed.bytes_consumed);
455 CheckPacketHasSingleStreamFrame(4);
456 CheckPacketIsFec(5, 4);
457 EXPECT_FALSE(creator_->IsFecProtected());
460 TEST_F(QuicPacketGeneratorTest, ConsumeDataSendsFecOnTimeout) {
461 delegate_.SetCanWriteAnything();
462 creator_->set_max_packets_per_fec_group(1000);
464 // Send data with MUST_FEC_PROTECT flag. No FEC packet is emitted, but the
465 // creator FEC protects all data.
466 EXPECT_CALL(delegate_, OnSerializedPacket(_))
467 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
468 QuicConsumedData consumed = generator_.ConsumeData(3, CreateData(1u), 0, true,
469 MUST_FEC_PROTECT, nullptr);
470 EXPECT_EQ(1u, consumed.bytes_consumed);
471 EXPECT_TRUE(consumed.fin_consumed);
472 CheckPacketHasSingleStreamFrame(0);
473 EXPECT_TRUE(creator_->IsFecProtected());
475 // Send more data with MAY_FEC_PROTECT. This packet should also be protected,
476 // and FEC packet is not yet sent.
477 EXPECT_CALL(delegate_, OnSerializedPacket(_))
478 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
479 consumed = generator_.ConsumeData(5, CreateData(1u), 0, true, MAY_FEC_PROTECT,
480 nullptr);
481 EXPECT_EQ(1u, consumed.bytes_consumed);
482 CheckPacketHasSingleStreamFrame(1);
483 EXPECT_TRUE(creator_->IsFecProtected());
485 // Calling OnFecTimeout should cause the FEC packet to be emitted.
486 EXPECT_CALL(delegate_, OnSerializedPacket(_))
487 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
488 generator_.OnFecTimeout();
489 CheckPacketIsFec(2, 1);
490 EXPECT_FALSE(creator_->IsFecProtected());
492 // Subsequent data is protected under the next FEC group. Send enough data to
493 // create 2 more packets: one full and one partial.
495 InSequence dummy;
496 EXPECT_CALL(delegate_, OnSerializedPacket(_))
497 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
498 EXPECT_CALL(delegate_, OnSerializedPacket(_))
499 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
501 size_t data_len = kDefaultMaxPacketSize + 1;
502 consumed = generator_.ConsumeData(7, CreateData(data_len), 0, true,
503 MUST_FEC_PROTECT, nullptr);
504 EXPECT_EQ(data_len, consumed.bytes_consumed);
505 EXPECT_TRUE(consumed.fin_consumed);
506 CheckPacketHasSingleStreamFrame(3);
507 CheckPacketHasSingleStreamFrame(4);
508 EXPECT_TRUE(creator_->IsFecProtected());
510 // Calling OnFecTimeout should cause the FEC packet to be emitted.
511 EXPECT_CALL(delegate_, OnSerializedPacket(_))
512 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
513 generator_.OnFecTimeout();
514 CheckPacketIsFec(5, 4);
515 EXPECT_FALSE(creator_->IsFecProtected());
518 TEST_F(QuicPacketGeneratorTest, GetFecTimeoutFiniteOnlyOnFirstPacketInGroup) {
519 delegate_.SetCanWriteAnything();
520 creator_->set_max_packets_per_fec_group(6);
522 // Send enough data to create 2 packets: one full and one partial. Send with
523 // MUST_FEC_PROTECT flag. No FEC packet is emitted yet, but the creator FEC
524 // protects all data.
526 InSequence dummy;
527 EXPECT_CALL(delegate_, OnSerializedPacket(_))
528 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
529 EXPECT_CALL(delegate_, OnSerializedPacket(_))
530 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
532 size_t data_len = 1 * kDefaultMaxPacketSize + 100;
533 QuicConsumedData consumed = generator_.ConsumeData(
534 3, CreateData(data_len), 0, true, MUST_FEC_PROTECT, nullptr);
535 EXPECT_EQ(data_len, consumed.bytes_consumed);
536 EXPECT_TRUE(consumed.fin_consumed);
537 EXPECT_FALSE(generator_.HasQueuedFrames());
538 CheckPacketHasSingleStreamFrame(0);
539 CheckPacketHasSingleStreamFrame(1);
540 EXPECT_TRUE(creator_->IsFecProtected());
542 // GetFecTimeout returns finite timeout only for first packet in group.
543 EXPECT_EQ(QuicTime::Delta::FromMilliseconds(kMinFecTimeoutMs),
544 generator_.GetFecTimeout(/*sequence_number=*/1u));
545 EXPECT_EQ(QuicTime::Delta::Infinite(),
546 generator_.GetFecTimeout(/*sequence_number=*/2u));
548 // Send more data with MAY_FEC_PROTECT. This packet should also be protected,
549 // and FEC packet is not yet sent.
550 EXPECT_CALL(delegate_, OnSerializedPacket(_))
551 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
552 consumed = generator_.ConsumeData(5, CreateData(1u), 0, true, MAY_FEC_PROTECT,
553 nullptr);
554 CheckPacketHasSingleStreamFrame(2);
555 EXPECT_TRUE(creator_->IsFecProtected());
557 // GetFecTimeout returns finite timeout only for first packet in group.
558 EXPECT_EQ(QuicTime::Delta::Infinite(),
559 generator_.GetFecTimeout(/*sequence_number=*/3u));
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(3, /*fec_group=*/1u);
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 data_len = kDefaultMaxPacketSize + 1u;
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(4);
583 CheckPacketHasSingleStreamFrame(5);
584 EXPECT_TRUE(creator_->IsFecProtected());
586 // GetFecTimeout returns finite timeout for first packet in the new group.
587 EXPECT_EQ(QuicTime::Delta::FromMilliseconds(kMinFecTimeoutMs),
588 generator_.GetFecTimeout(/*sequence_number=*/5u));
589 EXPECT_EQ(QuicTime::Delta::Infinite(),
590 generator_.GetFecTimeout(/*sequence_number=*/6u));
592 // Calling OnFecTimeout should cause the FEC packet to be emitted.
593 EXPECT_CALL(delegate_, OnSerializedPacket(_))
594 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
595 generator_.OnFecTimeout();
596 CheckPacketIsFec(6, /*fec_group=*/5u);
597 EXPECT_FALSE(creator_->IsFecProtected());
599 // Send more data with MAY_FEC_PROTECT. No FEC protection, so GetFecTimeout
600 // returns infinite.
601 EXPECT_CALL(delegate_, OnSerializedPacket(_))
602 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
603 consumed = generator_.ConsumeData(9, CreateData(1u), 0, true, MAY_FEC_PROTECT,
604 nullptr);
605 CheckPacketHasSingleStreamFrame(7);
606 EXPECT_FALSE(creator_->IsFecProtected());
607 EXPECT_EQ(QuicTime::Delta::Infinite(),
608 generator_.GetFecTimeout(/*sequence_number=*/8u));
611 TEST_F(QuicPacketGeneratorTest, ConsumeData_FramesPreviouslyQueued) {
612 // Set the packet size be enough for two stream frames with 0 stream offset,
613 // but not enough for a stream frame of 0 offset and one with non-zero offset.
614 size_t length =
615 NullEncrypter().GetCiphertextSize(0) +
616 GetPacketHeaderSize(
617 creator_->connection_id_length(), true,
618 QuicPacketCreatorPeer::NextSequenceNumberLength(creator_),
619 NOT_IN_FEC_GROUP) +
620 // Add an extra 3 bytes for the payload and 1 byte so BytesFree is larger
621 // than the GetMinStreamFrameSize.
622 QuicFramer::GetMinStreamFrameSize(1, 0, false, NOT_IN_FEC_GROUP) + 3 +
623 QuicFramer::GetMinStreamFrameSize(1, 0, true, NOT_IN_FEC_GROUP) + 1;
624 creator_->SetMaxPacketLength(length);
625 delegate_.SetCanWriteAnything();
627 InSequence dummy;
628 EXPECT_CALL(delegate_, OnSerializedPacket(_))
629 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
630 EXPECT_CALL(delegate_, OnSerializedPacket(_))
631 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
633 generator_.StartBatchOperations();
634 // Queue enough data to prevent a stream frame with a non-zero offset from
635 // fitting.
636 QuicConsumedData consumed =
637 generator_.ConsumeData(kHeadersStreamId, MakeIOVector("foo"), 0, false,
638 MAY_FEC_PROTECT, nullptr);
639 EXPECT_EQ(3u, consumed.bytes_consumed);
640 EXPECT_FALSE(consumed.fin_consumed);
641 EXPECT_TRUE(generator_.HasQueuedFrames());
643 // This frame will not fit with the existing frame, causing the queued frame
644 // to be serialized, and it will not fit with another frame like it, so it is
645 // serialized by itself.
646 consumed = generator_.ConsumeData(kHeadersStreamId, MakeIOVector("bar"), 3,
647 true, MAY_FEC_PROTECT, nullptr);
648 EXPECT_EQ(3u, consumed.bytes_consumed);
649 EXPECT_TRUE(consumed.fin_consumed);
650 EXPECT_FALSE(generator_.HasQueuedFrames());
652 PacketContents contents;
653 contents.num_stream_frames = 1;
654 CheckPacketContains(contents, 0);
655 CheckPacketContains(contents, 1);
658 TEST_F(QuicPacketGeneratorTest, NoFecPacketSentWhenBatchEnds) {
659 delegate_.SetCanWriteAnything();
660 creator_->set_max_packets_per_fec_group(6);
662 generator_.StartBatchOperations();
664 generator_.ConsumeData(3, MakeIOVector("foo"), 2, true, MUST_FEC_PROTECT,
665 nullptr);
666 QuicConsumedData consumed = generator_.ConsumeData(
667 5, MakeIOVector("quux"), 7, false, MUST_FEC_PROTECT, nullptr);
668 EXPECT_EQ(4u, consumed.bytes_consumed);
669 EXPECT_FALSE(consumed.fin_consumed);
670 EXPECT_TRUE(generator_.HasQueuedFrames());
672 // Now both frames will be flushed out, but FEC packet is not yet sent.
673 EXPECT_CALL(delegate_, OnSerializedPacket(_))
674 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
675 generator_.FinishBatchOperations();
676 EXPECT_FALSE(generator_.HasQueuedFrames());
678 PacketContents contents;
679 contents.num_stream_frames = 2u;
680 contents.fec_group = 1u;
681 CheckPacketContains(contents, 0);
683 // Forcing FEC timeout causes FEC packet to be emitted.
684 EXPECT_CALL(delegate_, OnSerializedPacket(_))
685 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
686 generator_.OnFecTimeout();
687 CheckPacketIsFec(1, /*fec_group=*/1u);
690 TEST_F(QuicPacketGeneratorTest, FecTimeoutOnRttChange) {
691 EXPECT_EQ(QuicTime::Delta::Zero(),
692 QuicPacketGeneratorPeer::GetFecTimeout(&generator_));
693 generator_.OnRttChange(QuicTime::Delta::FromMilliseconds(300));
694 EXPECT_EQ(QuicTime::Delta::FromMilliseconds(150),
695 QuicPacketGeneratorPeer::GetFecTimeout(&generator_));
698 TEST_F(QuicPacketGeneratorTest, FecGroupSizeOnCongestionWindowChange) {
699 delegate_.SetCanWriteAnything();
700 creator_->set_max_packets_per_fec_group(50);
701 EXPECT_EQ(50u, creator_->max_packets_per_fec_group());
702 EXPECT_FALSE(creator_->IsFecGroupOpen());
704 // On reduced cwnd.
705 generator_.OnCongestionWindowChange(7);
706 EXPECT_EQ(3u, creator_->max_packets_per_fec_group());
708 // On increased cwnd.
709 generator_.OnCongestionWindowChange(100);
710 EXPECT_EQ(50u, creator_->max_packets_per_fec_group());
712 // On collapsed cwnd.
713 generator_.OnCongestionWindowChange(1);
714 EXPECT_EQ(2u, creator_->max_packets_per_fec_group());
717 TEST_F(QuicPacketGeneratorTest, FecGroupSizeChangeWithOpenGroup) {
718 delegate_.SetCanWriteAnything();
719 generator_.StartBatchOperations();
720 creator_->set_max_packets_per_fec_group(50);
721 EXPECT_EQ(50u, creator_->max_packets_per_fec_group());
722 EXPECT_FALSE(creator_->IsFecGroupOpen());
724 // Send enough data to create 4 packets with MUST_FEC_PROTECT flag. 3 packets
725 // are sent, one is queued in the creator.
727 InSequence dummy;
728 EXPECT_CALL(delegate_, OnSerializedPacket(_))
729 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
730 EXPECT_CALL(delegate_, OnSerializedPacket(_))
731 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
732 EXPECT_CALL(delegate_, OnSerializedPacket(_))
733 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
735 size_t data_len = 3 * kDefaultMaxPacketSize + 1;
736 QuicConsumedData consumed = generator_.ConsumeData(
737 7, CreateData(data_len), 0, true, MUST_FEC_PROTECT, nullptr);
738 EXPECT_EQ(data_len, consumed.bytes_consumed);
739 EXPECT_TRUE(creator_->IsFecGroupOpen());
741 // Change FEC groupsize.
742 generator_.OnCongestionWindowChange(2);
743 EXPECT_EQ(2u, creator_->max_packets_per_fec_group());
745 // Send enough data to trigger one unprotected data packet, causing the FEC
746 // packet to also be sent.
748 InSequence dummy;
749 EXPECT_CALL(delegate_, OnSerializedPacket(_))
750 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
751 EXPECT_CALL(delegate_, OnSerializedPacket(_))
752 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
754 consumed = generator_.ConsumeData(7, CreateData(kDefaultMaxPacketSize), 0,
755 true, MAY_FEC_PROTECT, nullptr);
756 EXPECT_EQ(kDefaultMaxPacketSize, consumed.bytes_consumed);
757 // Verify that one FEC packet was sent.
758 CheckPacketIsFec(4, /*fec_group=*/1u);
759 EXPECT_FALSE(creator_->IsFecGroupOpen());
760 EXPECT_FALSE(creator_->IsFecProtected());
763 TEST_F(QuicPacketGeneratorTest, SwitchFecOnOff) {
764 delegate_.SetCanWriteAnything();
765 creator_->set_max_packets_per_fec_group(2);
766 EXPECT_FALSE(creator_->IsFecProtected());
768 // Send one unprotected data packet.
769 EXPECT_CALL(delegate_, OnSerializedPacket(_))
770 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
771 QuicConsumedData consumed = generator_.ConsumeData(5, CreateData(1u), 0, true,
772 MAY_FEC_PROTECT, nullptr);
773 EXPECT_EQ(1u, consumed.bytes_consumed);
774 EXPECT_FALSE(generator_.HasQueuedFrames());
775 EXPECT_FALSE(creator_->IsFecProtected());
776 // Verify that one data packet was sent.
777 PacketContents contents;
778 contents.num_stream_frames = 1;
779 CheckPacketContains(contents, 0);
782 InSequence dummy;
783 EXPECT_CALL(delegate_, OnSerializedPacket(_))
784 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
785 EXPECT_CALL(delegate_, OnSerializedPacket(_))
786 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
787 EXPECT_CALL(delegate_, OnSerializedPacket(_))
788 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
789 EXPECT_CALL(delegate_, OnSerializedPacket(_))
790 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
792 // Send enough data to create 3 packets with MUST_FEC_PROTECT flag.
793 size_t data_len = 2 * kDefaultMaxPacketSize + 100;
794 consumed = generator_.ConsumeData(7, CreateData(data_len), 0, true,
795 MUST_FEC_PROTECT, nullptr);
796 EXPECT_EQ(data_len, consumed.bytes_consumed);
797 EXPECT_FALSE(generator_.HasQueuedFrames());
799 // Verify that packets sent were 3 data and 1 FEC.
800 CheckPacketHasSingleStreamFrame(1);
801 CheckPacketHasSingleStreamFrame(2);
802 CheckPacketIsFec(3, /*fec_group=*/2u);
803 CheckPacketHasSingleStreamFrame(4);
805 // Calling OnFecTimeout should emit the pending FEC packet.
806 EXPECT_CALL(delegate_, OnSerializedPacket(_))
807 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
808 generator_.OnFecTimeout();
809 CheckPacketIsFec(5, /*fec_group=*/5u);
811 // Send one unprotected data packet.
812 EXPECT_CALL(delegate_, OnSerializedPacket(_))
813 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
814 consumed = generator_.ConsumeData(7, CreateData(1u), 0, true, MAY_FEC_PROTECT,
815 nullptr);
816 EXPECT_EQ(1u, consumed.bytes_consumed);
817 EXPECT_FALSE(generator_.HasQueuedFrames());
818 EXPECT_FALSE(creator_->IsFecProtected());
819 // Verify that one unprotected data packet was sent.
820 CheckPacketContains(contents, 6);
823 TEST_F(QuicPacketGeneratorTest, SwitchFecOnWithPendingFrameInCreator) {
824 delegate_.SetCanWriteAnything();
825 // Enable FEC.
826 creator_->set_max_packets_per_fec_group(2);
828 generator_.StartBatchOperations();
829 // Queue enough data to prevent a stream frame with a non-zero offset from
830 // fitting.
831 QuicConsumedData consumed = generator_.ConsumeData(7, CreateData(1u), 0, true,
832 MAY_FEC_PROTECT, nullptr);
833 EXPECT_EQ(1u, consumed.bytes_consumed);
834 EXPECT_TRUE(creator_->HasPendingFrames());
836 // Queue protected data for sending. Should cause queued frames to be flushed.
837 EXPECT_CALL(delegate_, OnSerializedPacket(_))
838 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
839 EXPECT_FALSE(creator_->IsFecProtected());
840 consumed = generator_.ConsumeData(7, CreateData(1u), 0, true,
841 MUST_FEC_PROTECT, nullptr);
842 EXPECT_EQ(1u, consumed.bytes_consumed);
843 PacketContents contents;
844 contents.num_stream_frames = 1;
845 // Transmitted packet was not FEC protected.
846 CheckPacketContains(contents, 0);
847 EXPECT_TRUE(creator_->IsFecProtected());
848 EXPECT_TRUE(creator_->HasPendingFrames());
851 TEST_F(QuicPacketGeneratorTest, SwitchFecOnWithPendingFramesInGenerator) {
852 // Enable FEC.
853 creator_->set_max_packets_per_fec_group(2);
855 // Queue control frames in generator.
856 delegate_.SetCanNotWrite();
857 generator_.SetShouldSendAck(true);
858 delegate_.SetCanWriteAnything();
859 generator_.StartBatchOperations();
861 // Set up frames to write into the creator when control frames are written.
862 EXPECT_CALL(delegate_, PopulateAckFrame(_));
863 EXPECT_CALL(delegate_, PopulateStopWaitingFrame(_));
865 // Generator should have queued control frames, and creator should be empty.
866 EXPECT_TRUE(generator_.HasQueuedFrames());
867 EXPECT_FALSE(creator_->HasPendingFrames());
868 EXPECT_FALSE(creator_->IsFecProtected());
870 // Queue protected data for sending. Should cause queued frames to be flushed.
871 EXPECT_CALL(delegate_, OnSerializedPacket(_))
872 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
873 QuicConsumedData consumed = generator_.ConsumeData(7, CreateData(1u), 0, true,
874 MUST_FEC_PROTECT, nullptr);
875 EXPECT_EQ(1u, consumed.bytes_consumed);
876 PacketContents contents;
877 contents.num_ack_frames = 1;
878 contents.num_stop_waiting_frames = 1;
879 CheckPacketContains(contents, 0);
881 // FEC protection should be on in creator.
882 EXPECT_TRUE(creator_->IsFecProtected());
885 TEST_F(QuicPacketGeneratorTest, SwitchFecOnOffWithSubsequentFramesProtected) {
886 delegate_.SetCanWriteAnything();
888 // Enable FEC.
889 creator_->set_max_packets_per_fec_group(2);
890 EXPECT_FALSE(creator_->IsFecProtected());
892 // Queue stream frame to be protected in creator.
893 generator_.StartBatchOperations();
894 QuicConsumedData consumed = generator_.ConsumeData(5, CreateData(1u), 0, true,
895 MUST_FEC_PROTECT, nullptr);
896 EXPECT_EQ(1u, consumed.bytes_consumed);
897 // Creator has a pending protected frame.
898 EXPECT_TRUE(creator_->HasPendingFrames());
899 EXPECT_TRUE(creator_->IsFecProtected());
901 // Add enough unprotected data to exceed size of current packet, so that
902 // current packet is sent. Both frames will be sent out in a single packet.
903 EXPECT_CALL(delegate_, OnSerializedPacket(_))
904 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
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, 0);
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(_))
926 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
927 QuicConsumedData consumed = generator_.ConsumeData(5, CreateData(1u), 0, true,
928 MUST_FEC_PROTECT, nullptr);
929 EXPECT_EQ(1u, consumed.bytes_consumed);
930 PacketContents contents;
931 contents.num_stream_frames = 1u;
932 contents.fec_group = 1u;
933 CheckPacketContains(contents, 0);
935 // FEC should still be on in creator.
936 EXPECT_TRUE(creator_->IsFecProtected());
938 // Send unprotected data to cause second packet to be sent, which gets
939 // protected because it happens to fall within an open FEC group. Data packet
940 // will be followed by FEC packet.
942 InSequence dummy;
943 EXPECT_CALL(delegate_, OnSerializedPacket(_))
944 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
945 EXPECT_CALL(delegate_, OnSerializedPacket(_))
946 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
948 consumed = generator_.ConsumeData(5, CreateData(1u), 0, true, MAY_FEC_PROTECT,
949 nullptr);
950 EXPECT_EQ(1u, consumed.bytes_consumed);
951 contents.num_stream_frames = 1u;
952 CheckPacketContains(contents, 1);
953 CheckPacketIsFec(2, /*fec_group=*/1u);
955 // FEC protection should be off in creator.
956 EXPECT_FALSE(creator_->IsFecProtected());
959 TEST_F(QuicPacketGeneratorTest, SwitchFecOnOffThenOnWithCreatorProtectionOn) {
960 delegate_.SetCanWriteAnything();
961 generator_.StartBatchOperations();
963 // Enable FEC.
964 creator_->set_max_packets_per_fec_group(2);
965 EXPECT_FALSE(creator_->IsFecProtected());
967 // Queue one byte of FEC protected data.
968 QuicConsumedData consumed = generator_.ConsumeData(5, CreateData(1u), 0, true,
969 MUST_FEC_PROTECT, nullptr);
970 EXPECT_TRUE(creator_->HasPendingFrames());
972 // Add more unprotected data causing first packet to be sent, FEC protected.
973 EXPECT_CALL(delegate_, OnSerializedPacket(_))
974 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
975 size_t data_len = kDefaultMaxPacketSize;
976 consumed = generator_.ConsumeData(5, CreateData(data_len), 0, true,
977 MAY_FEC_PROTECT, nullptr);
978 EXPECT_EQ(data_len, consumed.bytes_consumed);
979 PacketContents contents;
980 contents.num_stream_frames = 2u;
981 contents.fec_group = 1u;
982 CheckPacketContains(contents, 0);
984 // FEC group is still open in creator.
985 EXPECT_TRUE(creator_->IsFecProtected());
987 // Add data that should be protected, large enough to cause second packet to
988 // be sent. Data packet should be followed by FEC packet.
990 InSequence dummy;
991 EXPECT_CALL(delegate_, OnSerializedPacket(_))
992 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
993 EXPECT_CALL(delegate_, OnSerializedPacket(_))
994 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
996 consumed = generator_.ConsumeData(5, CreateData(data_len), 0, true,
997 MUST_FEC_PROTECT, nullptr);
998 EXPECT_EQ(data_len, consumed.bytes_consumed);
999 CheckPacketContains(contents, 1);
1000 CheckPacketIsFec(2, /*fec_group=*/1u);
1002 // FEC protection should remain on in creator.
1003 EXPECT_TRUE(creator_->IsFecProtected());
1006 TEST_F(QuicPacketGeneratorTest, NotWritableThenBatchOperations) {
1007 delegate_.SetCanNotWrite();
1009 generator_.SetShouldSendAck(false);
1010 generator_.AddControlFrame(QuicFrame(CreateRstStreamFrame()));
1011 EXPECT_TRUE(generator_.HasQueuedFrames());
1013 delegate_.SetCanWriteAnything();
1015 generator_.StartBatchOperations();
1017 // When the first write operation is invoked, the ack frame will be returned.
1018 EXPECT_CALL(delegate_, PopulateAckFrame(_));
1020 // Send some data and a control frame
1021 generator_.ConsumeData(3, MakeIOVector("quux"), 7, false, MAY_FEC_PROTECT,
1022 nullptr);
1023 generator_.AddControlFrame(QuicFrame(CreateGoAwayFrame()));
1025 // All five frames will be flushed out in a single packet.
1026 EXPECT_CALL(delegate_, OnSerializedPacket(_))
1027 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
1028 generator_.FinishBatchOperations();
1029 EXPECT_FALSE(generator_.HasQueuedFrames());
1031 PacketContents contents;
1032 contents.num_ack_frames = 1;
1033 contents.num_goaway_frames = 1;
1034 contents.num_rst_stream_frames = 1;
1035 contents.num_stream_frames = 1;
1036 CheckPacketContains(contents, 0);
1039 TEST_F(QuicPacketGeneratorTest, NotWritableThenBatchOperations2) {
1040 delegate_.SetCanNotWrite();
1042 generator_.SetShouldSendAck(false);
1043 generator_.AddControlFrame(QuicFrame(CreateRstStreamFrame()));
1044 EXPECT_TRUE(generator_.HasQueuedFrames());
1046 delegate_.SetCanWriteAnything();
1048 generator_.StartBatchOperations();
1050 // When the first write operation is invoked, the ack frame will be returned.
1051 EXPECT_CALL(delegate_, PopulateAckFrame(_));
1054 InSequence dummy;
1055 // All five frames will be flushed out in a single packet
1056 EXPECT_CALL(delegate_, OnSerializedPacket(_))
1057 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
1058 EXPECT_CALL(delegate_, OnSerializedPacket(_))
1059 .WillOnce(Invoke(this, &QuicPacketGeneratorTest::SavePacket));
1062 // Send enough data to exceed one packet
1063 size_t data_len = kDefaultMaxPacketSize + 100;
1064 QuicConsumedData consumed = generator_.ConsumeData(
1065 3, CreateData(data_len), 0, true, MAY_FEC_PROTECT, nullptr);
1066 EXPECT_EQ(data_len, consumed.bytes_consumed);
1067 EXPECT_TRUE(consumed.fin_consumed);
1068 generator_.AddControlFrame(QuicFrame(CreateGoAwayFrame()));
1070 generator_.FinishBatchOperations();
1071 EXPECT_FALSE(generator_.HasQueuedFrames());
1073 // The first packet should have the queued data and part of the stream data.
1074 PacketContents contents;
1075 contents.num_ack_frames = 1;
1076 contents.num_rst_stream_frames = 1;
1077 contents.num_stream_frames = 1;
1078 CheckPacketContains(contents, 0);
1080 // The second should have the remainder of the stream data.
1081 PacketContents contents2;
1082 contents2.num_goaway_frames = 1;
1083 contents2.num_stream_frames = 1;
1084 CheckPacketContains(contents2, 1);
1087 TEST_F(QuicPacketGeneratorTest, TestConnectionIdLength) {
1088 generator_.SetConnectionIdLength(0);
1089 EXPECT_EQ(PACKET_0BYTE_CONNECTION_ID, creator_->connection_id_length());
1090 generator_.SetConnectionIdLength(1);
1091 EXPECT_EQ(PACKET_1BYTE_CONNECTION_ID, creator_->connection_id_length());
1092 generator_.SetConnectionIdLength(2);
1093 EXPECT_EQ(PACKET_4BYTE_CONNECTION_ID, creator_->connection_id_length());
1094 generator_.SetConnectionIdLength(3);
1095 EXPECT_EQ(PACKET_4BYTE_CONNECTION_ID, creator_->connection_id_length());
1096 generator_.SetConnectionIdLength(4);
1097 EXPECT_EQ(PACKET_4BYTE_CONNECTION_ID, creator_->connection_id_length());
1098 generator_.SetConnectionIdLength(5);
1099 EXPECT_EQ(PACKET_8BYTE_CONNECTION_ID, creator_->connection_id_length());
1100 generator_.SetConnectionIdLength(6);
1101 EXPECT_EQ(PACKET_8BYTE_CONNECTION_ID, creator_->connection_id_length());
1102 generator_.SetConnectionIdLength(7);
1103 EXPECT_EQ(PACKET_8BYTE_CONNECTION_ID, creator_->connection_id_length());
1104 generator_.SetConnectionIdLength(8);
1105 EXPECT_EQ(PACKET_8BYTE_CONNECTION_ID, creator_->connection_id_length());
1106 generator_.SetConnectionIdLength(9);
1107 EXPECT_EQ(PACKET_8BYTE_CONNECTION_ID, creator_->connection_id_length());
1110 } // namespace test
1111 } // namespace net