Adding yfriedman@ as owner for the enhanced_bookmarks component.
[chromium-blink-merge.git] / net / quic / quic_packet_generator_test.cc
blob081161e1c3adbe203577ff59ee7032dd7e7edd67
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_utils.h"
14 #include "net/quic/test_tools/quic_packet_creator_peer.h"
15 #include "net/quic/test_tools/quic_packet_generator_peer.h"
16 #include "net/quic/test_tools/quic_test_utils.h"
17 #include "net/quic/test_tools/simple_quic_framer.h"
18 #include "testing/gmock/include/gmock/gmock.h"
19 #include "testing/gtest/include/gtest/gtest.h"
21 using base::StringPiece;
22 using std::string;
23 using testing::InSequence;
24 using testing::Return;
25 using testing::SaveArg;
26 using testing::StrictMock;
27 using testing::_;
29 namespace net {
30 namespace test {
31 namespace {
33 class MockDelegate : public QuicPacketGenerator::DelegateInterface {
34 public:
35 MockDelegate() {}
36 virtual ~MockDelegate() OVERRIDE {}
38 MOCK_METHOD3(ShouldGeneratePacket,
39 bool(TransmissionType transmission_type,
40 HasRetransmittableData retransmittable,
41 IsHandshake handshake));
42 MOCK_METHOD0(CreateAckFrame, QuicAckFrame*());
43 MOCK_METHOD0(CreateFeedbackFrame, QuicCongestionFeedbackFrame*());
44 MOCK_METHOD0(CreateStopWaitingFrame, QuicStopWaitingFrame*());
45 MOCK_METHOD1(OnSerializedPacket, bool(const SerializedPacket& packet));
46 MOCK_METHOD2(CloseConnection, void(QuicErrorCode, bool));
48 void SetCanWriteAnything() {
49 EXPECT_CALL(*this, ShouldGeneratePacket(NOT_RETRANSMISSION, _, _))
50 .WillRepeatedly(Return(true));
51 EXPECT_CALL(*this, ShouldGeneratePacket(NOT_RETRANSMISSION,
52 NO_RETRANSMITTABLE_DATA, _))
53 .WillRepeatedly(Return(true));
56 void SetCanNotWrite() {
57 EXPECT_CALL(*this, ShouldGeneratePacket(NOT_RETRANSMISSION, _, _))
58 .WillRepeatedly(Return(false));
59 EXPECT_CALL(*this, ShouldGeneratePacket(NOT_RETRANSMISSION,
60 NO_RETRANSMITTABLE_DATA, _))
61 .WillRepeatedly(Return(false));
64 // Use this when only ack and feedback frames should be allowed to be written.
65 void SetCanWriteOnlyNonRetransmittable() {
66 EXPECT_CALL(*this, ShouldGeneratePacket(NOT_RETRANSMISSION, _, _))
67 .WillRepeatedly(Return(false));
68 EXPECT_CALL(*this, ShouldGeneratePacket(NOT_RETRANSMISSION,
69 NO_RETRANSMITTABLE_DATA, _))
70 .WillRepeatedly(Return(true));
73 private:
74 DISALLOW_COPY_AND_ASSIGN(MockDelegate);
77 // Simple struct for describing the contents of a packet.
78 // Useful in conjunction with a SimpleQuicFrame for validating
79 // that a packet contains the expected frames.
80 struct PacketContents {
81 PacketContents()
82 : num_ack_frames(0),
83 num_connection_close_frames(0),
84 num_feedback_frames(0),
85 num_goaway_frames(0),
86 num_rst_stream_frames(0),
87 num_stop_waiting_frames(0),
88 num_stream_frames(0),
89 fec_group(0) {
92 size_t num_ack_frames;
93 size_t num_connection_close_frames;
94 size_t num_feedback_frames;
95 size_t num_goaway_frames;
96 size_t num_rst_stream_frames;
97 size_t num_stop_waiting_frames;
98 size_t num_stream_frames;
100 QuicFecGroupNumber fec_group;
103 } // namespace
105 class QuicPacketGeneratorTest : public ::testing::Test {
106 protected:
107 QuicPacketGeneratorTest()
108 : framer_(QuicSupportedVersions(), QuicTime::Zero(), false),
109 generator_(42, &framer_, &random_, &delegate_),
110 creator_(QuicPacketGeneratorPeer::GetPacketCreator(&generator_)),
111 packet_(0, PACKET_1BYTE_SEQUENCE_NUMBER, NULL, 0, NULL),
112 packet2_(0, PACKET_1BYTE_SEQUENCE_NUMBER, NULL, 0, NULL),
113 packet3_(0, PACKET_1BYTE_SEQUENCE_NUMBER, NULL, 0, NULL),
114 packet4_(0, PACKET_1BYTE_SEQUENCE_NUMBER, NULL, 0, NULL),
115 packet5_(0, PACKET_1BYTE_SEQUENCE_NUMBER, NULL, 0, NULL),
116 packet6_(0, PACKET_1BYTE_SEQUENCE_NUMBER, NULL, 0, NULL),
117 packet7_(0, PACKET_1BYTE_SEQUENCE_NUMBER, NULL, 0, NULL) {
120 virtual ~QuicPacketGeneratorTest() OVERRIDE {
121 delete packet_.packet;
122 delete packet_.retransmittable_frames;
123 delete packet2_.packet;
124 delete packet2_.retransmittable_frames;
125 delete packet3_.packet;
126 delete packet3_.retransmittable_frames;
127 delete packet4_.packet;
128 delete packet4_.retransmittable_frames;
129 delete packet5_.packet;
130 delete packet5_.retransmittable_frames;
131 delete packet6_.packet;
132 delete packet6_.retransmittable_frames;
133 delete packet7_.packet;
134 delete packet7_.retransmittable_frames;
137 QuicAckFrame* CreateAckFrame() {
138 // TODO(rch): Initialize this so it can be verified later.
139 return new QuicAckFrame(MakeAckFrame(0));
142 QuicCongestionFeedbackFrame* CreateFeedbackFrame() {
143 QuicCongestionFeedbackFrame* frame = new QuicCongestionFeedbackFrame;
144 frame->type = kTCP;
145 frame->tcp.receive_window = 0x4030;
146 return frame;
149 QuicStopWaitingFrame* CreateStopWaitingFrame() {
150 QuicStopWaitingFrame* frame = new QuicStopWaitingFrame();
151 frame->entropy_hash = 0;
152 frame->least_unacked = 0;
153 return frame;
156 QuicRstStreamFrame* CreateRstStreamFrame() {
157 return new QuicRstStreamFrame(1, QUIC_STREAM_NO_ERROR, 0);
160 QuicGoAwayFrame* CreateGoAwayFrame() {
161 return new QuicGoAwayFrame(QUIC_NO_ERROR, 1, string());
164 void CheckPacketContains(const PacketContents& contents,
165 const SerializedPacket& packet) {
166 size_t num_retransmittable_frames = contents.num_connection_close_frames +
167 contents.num_goaway_frames + contents.num_rst_stream_frames +
168 contents.num_stream_frames;
169 size_t num_frames = contents.num_feedback_frames + contents.num_ack_frames +
170 contents.num_stop_waiting_frames + num_retransmittable_frames;
172 if (num_retransmittable_frames == 0) {
173 ASSERT_TRUE(packet.retransmittable_frames == NULL);
174 } else {
175 ASSERT_TRUE(packet.retransmittable_frames != NULL);
176 EXPECT_EQ(num_retransmittable_frames,
177 packet.retransmittable_frames->frames().size());
180 ASSERT_TRUE(packet.packet != NULL);
181 ASSERT_TRUE(simple_framer_.ProcessPacket(*packet.packet));
182 EXPECT_EQ(num_frames, simple_framer_.num_frames());
183 EXPECT_EQ(contents.num_ack_frames, simple_framer_.ack_frames().size());
184 EXPECT_EQ(contents.num_connection_close_frames,
185 simple_framer_.connection_close_frames().size());
186 EXPECT_EQ(contents.num_feedback_frames,
187 simple_framer_.feedback_frames().size());
188 EXPECT_EQ(contents.num_goaway_frames,
189 simple_framer_.goaway_frames().size());
190 EXPECT_EQ(contents.num_rst_stream_frames,
191 simple_framer_.rst_stream_frames().size());
192 EXPECT_EQ(contents.num_stream_frames,
193 simple_framer_.stream_frames().size());
194 EXPECT_EQ(contents.num_stop_waiting_frames,
195 simple_framer_.stop_waiting_frames().size());
196 EXPECT_EQ(contents.fec_group, simple_framer_.header().fec_group);
199 void CheckPacketHasSingleStreamFrame(const SerializedPacket& packet) {
200 ASSERT_TRUE(packet.retransmittable_frames != NULL);
201 EXPECT_EQ(1u, packet.retransmittable_frames->frames().size());
202 ASSERT_TRUE(packet.packet != NULL);
203 ASSERT_TRUE(simple_framer_.ProcessPacket(*packet.packet));
204 EXPECT_EQ(1u, simple_framer_.num_frames());
205 EXPECT_EQ(1u, simple_framer_.stream_frames().size());
208 void CheckPacketIsFec(const SerializedPacket& packet,
209 QuicPacketSequenceNumber fec_group) {
210 ASSERT_TRUE(packet.retransmittable_frames == NULL);
211 ASSERT_TRUE(packet.packet != NULL);
212 ASSERT_TRUE(simple_framer_.ProcessPacket(*packet.packet));
213 EXPECT_TRUE(simple_framer_.header().fec_flag);
214 EXPECT_EQ(fec_group, simple_framer_.fec_data().fec_group);
217 IOVector CreateData(size_t len) {
218 data_array_.reset(new char[len]);
219 memset(data_array_.get(), '?', len);
220 IOVector data;
221 data.Append(data_array_.get(), len);
222 return data;
225 QuicFramer framer_;
226 MockRandom random_;
227 StrictMock<MockDelegate> delegate_;
228 QuicPacketGenerator generator_;
229 QuicPacketCreator* creator_;
230 SimpleQuicFramer simple_framer_;
231 SerializedPacket packet_;
232 SerializedPacket packet2_;
233 SerializedPacket packet3_;
234 SerializedPacket packet4_;
235 SerializedPacket packet5_;
236 SerializedPacket packet6_;
237 SerializedPacket packet7_;
239 private:
240 scoped_ptr<char[]> data_array_;
243 class MockDebugDelegate : public QuicPacketGenerator::DebugDelegate {
244 public:
245 MOCK_METHOD1(OnFrameAddedToPacket,
246 void(const QuicFrame&));
249 TEST_F(QuicPacketGeneratorTest, ShouldSendAck_NotWritable) {
250 delegate_.SetCanNotWrite();
252 generator_.SetShouldSendAck(false, false);
253 EXPECT_TRUE(generator_.HasQueuedFrames());
256 TEST_F(QuicPacketGeneratorTest, ShouldSendAck_WritableAndShouldNotFlush) {
257 StrictMock<MockDebugDelegate> debug_delegate;
259 generator_.set_debug_delegate(&debug_delegate);
260 delegate_.SetCanWriteOnlyNonRetransmittable();
261 generator_.StartBatchOperations();
263 EXPECT_CALL(delegate_, CreateAckFrame()).WillOnce(Return(CreateAckFrame()));
264 EXPECT_CALL(debug_delegate, OnFrameAddedToPacket(_)).Times(1);
266 generator_.SetShouldSendAck(false, false);
267 EXPECT_TRUE(generator_.HasQueuedFrames());
270 TEST_F(QuicPacketGeneratorTest, ShouldSendAck_WritableAndShouldFlush) {
271 delegate_.SetCanWriteOnlyNonRetransmittable();
273 EXPECT_CALL(delegate_, CreateAckFrame()).WillOnce(Return(CreateAckFrame()));
274 EXPECT_CALL(delegate_, OnSerializedPacket(_)).WillOnce(
275 DoAll(SaveArg<0>(&packet_), Return(true)));
277 generator_.SetShouldSendAck(false, false);
278 EXPECT_FALSE(generator_.HasQueuedFrames());
280 PacketContents contents;
281 contents.num_ack_frames = 1;
282 CheckPacketContains(contents, packet_);
285 TEST_F(QuicPacketGeneratorTest,
286 ShouldSendAckWithFeedback_WritableAndShouldNotFlush) {
287 delegate_.SetCanWriteOnlyNonRetransmittable();
288 generator_.StartBatchOperations();
290 EXPECT_CALL(delegate_, CreateAckFrame()).WillOnce(Return(CreateAckFrame()));
291 EXPECT_CALL(delegate_, CreateFeedbackFrame()).WillOnce(
292 Return(CreateFeedbackFrame()));
294 generator_.SetShouldSendAck(true, false);
295 EXPECT_TRUE(generator_.HasQueuedFrames());
298 TEST_F(QuicPacketGeneratorTest,
299 ShouldSendAckWithFeedback_WritableAndShouldFlush) {
300 delegate_.SetCanWriteOnlyNonRetransmittable();
302 EXPECT_CALL(delegate_, CreateAckFrame()).WillOnce(Return(CreateAckFrame()));
303 EXPECT_CALL(delegate_, CreateFeedbackFrame()).WillOnce(
304 Return(CreateFeedbackFrame()));
305 EXPECT_CALL(delegate_, CreateStopWaitingFrame()).WillOnce(
306 Return(CreateStopWaitingFrame()));
308 EXPECT_CALL(delegate_, OnSerializedPacket(_)).WillOnce(
309 DoAll(SaveArg<0>(&packet_), Return(true)));
311 generator_.SetShouldSendAck(true, true);
312 EXPECT_FALSE(generator_.HasQueuedFrames());
314 PacketContents contents;
315 contents.num_ack_frames = 1;
316 contents.num_feedback_frames = 1;
317 contents.num_stop_waiting_frames = 1;
318 CheckPacketContains(contents, packet_);
321 TEST_F(QuicPacketGeneratorTest, AddControlFrame_NotWritable) {
322 delegate_.SetCanNotWrite();
324 generator_.AddControlFrame(QuicFrame(CreateRstStreamFrame()));
325 EXPECT_TRUE(generator_.HasQueuedFrames());
328 TEST_F(QuicPacketGeneratorTest, AddControlFrame_OnlyAckWritable) {
329 delegate_.SetCanWriteOnlyNonRetransmittable();
331 generator_.AddControlFrame(QuicFrame(CreateRstStreamFrame()));
332 EXPECT_TRUE(generator_.HasQueuedFrames());
335 TEST_F(QuicPacketGeneratorTest, AddControlFrame_WritableAndShouldNotFlush) {
336 delegate_.SetCanWriteAnything();
337 generator_.StartBatchOperations();
339 generator_.AddControlFrame(QuicFrame(CreateRstStreamFrame()));
340 EXPECT_TRUE(generator_.HasQueuedFrames());
343 TEST_F(QuicPacketGeneratorTest, AddControlFrame_NotWritableBatchThenFlush) {
344 delegate_.SetCanNotWrite();
345 generator_.StartBatchOperations();
347 generator_.AddControlFrame(QuicFrame(CreateRstStreamFrame()));
348 EXPECT_TRUE(generator_.HasQueuedFrames());
349 generator_.FinishBatchOperations();
350 EXPECT_TRUE(generator_.HasQueuedFrames());
352 EXPECT_CALL(delegate_, OnSerializedPacket(_)).WillOnce(
353 DoAll(SaveArg<0>(&packet_), Return(true)));
354 generator_.FlushAllQueuedFrames();
355 EXPECT_FALSE(generator_.HasQueuedFrames());
357 PacketContents contents;
358 contents.num_rst_stream_frames = 1;
359 CheckPacketContains(contents, packet_);
362 TEST_F(QuicPacketGeneratorTest, AddControlFrame_WritableAndShouldFlush) {
363 delegate_.SetCanWriteAnything();
365 EXPECT_CALL(delegate_, OnSerializedPacket(_)).WillOnce(
366 DoAll(SaveArg<0>(&packet_), Return(true)));
368 generator_.AddControlFrame(QuicFrame(CreateRstStreamFrame()));
369 EXPECT_FALSE(generator_.HasQueuedFrames());
371 PacketContents contents;
372 contents.num_rst_stream_frames = 1;
373 CheckPacketContains(contents, packet_);
376 TEST_F(QuicPacketGeneratorTest, ConsumeData_NotWritable) {
377 delegate_.SetCanNotWrite();
379 QuicConsumedData consumed = generator_.ConsumeData(
380 kHeadersStreamId, MakeIOVector("foo"), 2, true, MAY_FEC_PROTECT, NULL);
381 EXPECT_EQ(0u, consumed.bytes_consumed);
382 EXPECT_FALSE(consumed.fin_consumed);
383 EXPECT_FALSE(generator_.HasQueuedFrames());
386 TEST_F(QuicPacketGeneratorTest, ConsumeData_WritableAndShouldNotFlush) {
387 delegate_.SetCanWriteAnything();
388 generator_.StartBatchOperations();
390 QuicConsumedData consumed = generator_.ConsumeData(
391 kHeadersStreamId, MakeIOVector("foo"), 2, true, MAY_FEC_PROTECT, NULL);
392 EXPECT_EQ(3u, consumed.bytes_consumed);
393 EXPECT_TRUE(consumed.fin_consumed);
394 EXPECT_TRUE(generator_.HasQueuedFrames());
397 TEST_F(QuicPacketGeneratorTest, ConsumeData_WritableAndShouldFlush) {
398 delegate_.SetCanWriteAnything();
400 EXPECT_CALL(delegate_, OnSerializedPacket(_)).WillOnce(
401 DoAll(SaveArg<0>(&packet_), Return(true)));
402 QuicConsumedData consumed = generator_.ConsumeData(
403 kHeadersStreamId, MakeIOVector("foo"), 2, true, MAY_FEC_PROTECT, NULL);
404 EXPECT_EQ(3u, consumed.bytes_consumed);
405 EXPECT_TRUE(consumed.fin_consumed);
406 EXPECT_FALSE(generator_.HasQueuedFrames());
408 PacketContents contents;
409 contents.num_stream_frames = 1;
410 CheckPacketContains(contents, packet_);
413 TEST_F(QuicPacketGeneratorTest,
414 ConsumeDataMultipleTimes_WritableAndShouldNotFlush) {
415 delegate_.SetCanWriteAnything();
416 generator_.StartBatchOperations();
418 generator_.ConsumeData(kHeadersStreamId, MakeIOVector("foo"), 2, true,
419 MAY_FEC_PROTECT, NULL);
420 QuicConsumedData consumed = generator_.ConsumeData(
421 3, MakeIOVector("quux"), 7, false, MAY_FEC_PROTECT, NULL);
422 EXPECT_EQ(4u, consumed.bytes_consumed);
423 EXPECT_FALSE(consumed.fin_consumed);
424 EXPECT_TRUE(generator_.HasQueuedFrames());
427 TEST_F(QuicPacketGeneratorTest, ConsumeData_BatchOperations) {
428 delegate_.SetCanWriteAnything();
429 generator_.StartBatchOperations();
431 generator_.ConsumeData(kHeadersStreamId, MakeIOVector("foo"), 2, true,
432 MAY_FEC_PROTECT, NULL);
433 QuicConsumedData consumed = generator_.ConsumeData(
434 3, MakeIOVector("quux"), 7, false, MAY_FEC_PROTECT, NULL);
435 EXPECT_EQ(4u, consumed.bytes_consumed);
436 EXPECT_FALSE(consumed.fin_consumed);
437 EXPECT_TRUE(generator_.HasQueuedFrames());
439 // Now both frames will be flushed out.
440 EXPECT_CALL(delegate_, OnSerializedPacket(_)).WillOnce(
441 DoAll(SaveArg<0>(&packet_), Return(true)));
442 generator_.FinishBatchOperations();
443 EXPECT_FALSE(generator_.HasQueuedFrames());
445 PacketContents contents;
446 contents.num_stream_frames = 2;
447 CheckPacketContains(contents, packet_);
450 TEST_F(QuicPacketGeneratorTest, ConsumeDataFEC) {
451 delegate_.SetCanWriteAnything();
453 // Send FEC every two packets.
454 creator_->set_max_packets_per_fec_group(2);
457 InSequence dummy;
458 EXPECT_CALL(delegate_, OnSerializedPacket(_)).WillOnce(
459 DoAll(SaveArg<0>(&packet_), Return(true)));
460 EXPECT_CALL(delegate_, OnSerializedPacket(_)).WillOnce(
461 DoAll(SaveArg<0>(&packet2_), Return(true)));
462 EXPECT_CALL(delegate_, OnSerializedPacket(_)).WillOnce(
463 DoAll(SaveArg<0>(&packet3_), Return(true)));
464 EXPECT_CALL(delegate_, OnSerializedPacket(_)).WillOnce(
465 DoAll(SaveArg<0>(&packet4_), Return(true)));
466 EXPECT_CALL(delegate_, OnSerializedPacket(_)).WillOnce(
467 DoAll(SaveArg<0>(&packet5_), Return(true)));
470 // Send enough data to create 3 packets: two full and one partial. Send
471 // with MUST_FEC_PROTECT flag.
472 size_t data_len = 2 * kDefaultMaxPacketSize + 100;
473 QuicConsumedData consumed =
474 generator_.ConsumeData(3, CreateData(data_len), 0, true,
475 MUST_FEC_PROTECT, NULL);
476 EXPECT_EQ(data_len, consumed.bytes_consumed);
477 EXPECT_TRUE(consumed.fin_consumed);
478 EXPECT_FALSE(generator_.HasQueuedFrames());
480 CheckPacketHasSingleStreamFrame(packet_);
481 CheckPacketHasSingleStreamFrame(packet2_);
482 CheckPacketIsFec(packet3_, 1);
484 CheckPacketHasSingleStreamFrame(packet4_);
485 CheckPacketIsFec(packet5_, 4);
488 TEST_F(QuicPacketGeneratorTest, ConsumeDataSendsFecAtEnd) {
489 delegate_.SetCanWriteAnything();
491 // Enable FEC.
492 creator_->set_max_packets_per_fec_group(6);
494 InSequence dummy;
495 EXPECT_CALL(delegate_, OnSerializedPacket(_)).WillOnce(
496 DoAll(SaveArg<0>(&packet_), Return(true)));
497 EXPECT_CALL(delegate_, OnSerializedPacket(_)).WillOnce(
498 DoAll(SaveArg<0>(&packet2_), Return(true)));
499 EXPECT_CALL(delegate_, OnSerializedPacket(_)).WillOnce(
500 DoAll(SaveArg<0>(&packet3_), Return(true)));
503 // Send enough data to create 2 packets: one full and one partial. Send
504 // with MUST_FEC_PROTECT flag.
505 size_t data_len = 1 * kDefaultMaxPacketSize + 100;
506 QuicConsumedData consumed =
507 generator_.ConsumeData(3, CreateData(data_len), 0, true,
508 MUST_FEC_PROTECT, NULL);
509 EXPECT_EQ(data_len, consumed.bytes_consumed);
510 EXPECT_TRUE(consumed.fin_consumed);
511 EXPECT_FALSE(generator_.HasQueuedFrames());
513 CheckPacketHasSingleStreamFrame(packet_);
514 CheckPacketHasSingleStreamFrame(packet2_);
515 CheckPacketIsFec(packet3_, 1);
518 TEST_F(QuicPacketGeneratorTest, ConsumeData_FramesPreviouslyQueued) {
519 // Set the packet size be enough for two stream frames with 0 stream offset,
520 // but not enough for a stream frame of 0 offset and one with non-zero offset.
521 size_t length =
522 NullEncrypter().GetCiphertextSize(0) +
523 GetPacketHeaderSize(creator_->connection_id_length(),
524 true,
525 creator_->next_sequence_number_length(),
526 NOT_IN_FEC_GROUP) +
527 // Add an extra 3 bytes for the payload and 1 byte so BytesFree is larger
528 // than the GetMinStreamFrameSize.
529 QuicFramer::GetMinStreamFrameSize(1, 0, false, NOT_IN_FEC_GROUP) + 3 +
530 QuicFramer::GetMinStreamFrameSize(1, 0, true, NOT_IN_FEC_GROUP) + 1;
531 creator_->set_max_packet_length(length);
532 delegate_.SetCanWriteAnything();
534 InSequence dummy;
535 EXPECT_CALL(delegate_, OnSerializedPacket(_)).WillOnce(
536 DoAll(SaveArg<0>(&packet_), Return(true)));
537 EXPECT_CALL(delegate_, OnSerializedPacket(_)).WillOnce(
538 DoAll(SaveArg<0>(&packet2_), Return(true)));
540 generator_.StartBatchOperations();
541 // Queue enough data to prevent a stream frame with a non-zero offset from
542 // fitting.
543 QuicConsumedData consumed = generator_.ConsumeData(
544 kHeadersStreamId, MakeIOVector("foo"), 0, false, MAY_FEC_PROTECT, NULL);
545 EXPECT_EQ(3u, consumed.bytes_consumed);
546 EXPECT_FALSE(consumed.fin_consumed);
547 EXPECT_TRUE(generator_.HasQueuedFrames());
549 // This frame will not fit with the existing frame, causing the queued frame
550 // to be serialized, and it will not fit with another frame like it, so it is
551 // serialized by itself.
552 consumed = generator_.ConsumeData(kHeadersStreamId, MakeIOVector("bar"), 3,
553 true, MAY_FEC_PROTECT, NULL);
554 EXPECT_EQ(3u, consumed.bytes_consumed);
555 EXPECT_TRUE(consumed.fin_consumed);
556 EXPECT_FALSE(generator_.HasQueuedFrames());
558 PacketContents contents;
559 contents.num_stream_frames = 1;
560 CheckPacketContains(contents, packet_);
561 CheckPacketContains(contents, packet2_);
564 TEST_F(QuicPacketGeneratorTest, FecGroupSizeOnCongestionWindowChange) {
565 delegate_.SetCanWriteAnything();
566 creator_->set_max_packets_per_fec_group(50);
567 EXPECT_EQ(50u, creator_->max_packets_per_fec_group());
568 EXPECT_FALSE(creator_->IsFecGroupOpen());
570 // On reduced cwnd.
571 generator_.OnCongestionWindowChange(7 * kDefaultTCPMSS);
572 EXPECT_EQ(3u, creator_->max_packets_per_fec_group());
574 // On increased cwnd.
575 generator_.OnCongestionWindowChange(100 * kDefaultTCPMSS);
576 EXPECT_EQ(50u, creator_->max_packets_per_fec_group());
578 // On collapsed cwnd.
579 generator_.OnCongestionWindowChange(1 * kDefaultTCPMSS);
580 EXPECT_EQ(2u, creator_->max_packets_per_fec_group());
583 TEST_F(QuicPacketGeneratorTest, FecGroupSizeChangeWithOpenGroup) {
584 delegate_.SetCanWriteAnything();
585 // TODO(jri): This starting of batch mode should not be required when
586 // FEC sending is separated from batching operations.
587 generator_.StartBatchOperations();
588 creator_->set_max_packets_per_fec_group(50);
589 EXPECT_EQ(50u, creator_->max_packets_per_fec_group());
590 EXPECT_FALSE(creator_->IsFecGroupOpen());
592 // Send enough data to create 4 packets with MUST_FEC_PROTECT flag.
593 // 3 packets are sent, one is queued in the creator.
595 InSequence dummy;
596 EXPECT_CALL(delegate_, OnSerializedPacket(_)).WillOnce(
597 DoAll(SaveArg<0>(&packet_), Return(true)));
598 EXPECT_CALL(delegate_, OnSerializedPacket(_)).WillOnce(
599 DoAll(SaveArg<0>(&packet2_), Return(true)));
600 EXPECT_CALL(delegate_, OnSerializedPacket(_)).WillOnce(
601 DoAll(SaveArg<0>(&packet3_), Return(true)));
603 size_t data_len = 3 * kDefaultMaxPacketSize + 1;
604 QuicConsumedData consumed = generator_.ConsumeData(
605 7, CreateData(data_len), 0, true, MUST_FEC_PROTECT, NULL);
606 EXPECT_EQ(data_len, consumed.bytes_consumed);
607 EXPECT_TRUE(creator_->IsFecGroupOpen());
609 // Change FEC groupsize.
610 generator_.OnCongestionWindowChange(2 * kDefaultTCPMSS);
611 EXPECT_EQ(2u, creator_->max_packets_per_fec_group());
613 // Send enough data to trigger one unprotected data packet,
614 // causing the FEC packet to also be sent.
616 InSequence dummy;
617 EXPECT_CALL(delegate_, OnSerializedPacket(_)).WillOnce(
618 DoAll(SaveArg<0>(&packet4_), Return(true)));
619 EXPECT_CALL(delegate_, OnSerializedPacket(_)).WillOnce(
620 DoAll(SaveArg<0>(&packet5_), Return(true)));
622 consumed = generator_.ConsumeData(7, CreateData(kDefaultMaxPacketSize), 0,
623 true, MAY_FEC_PROTECT, NULL);
624 EXPECT_EQ(kDefaultMaxPacketSize, consumed.bytes_consumed);
625 // Verify that one FEC packet was sent.
626 CheckPacketIsFec(packet5_, /*fec_group=*/1u);
627 EXPECT_FALSE(creator_->IsFecGroupOpen());
628 EXPECT_FALSE(creator_->IsFecProtected());
631 TEST_F(QuicPacketGeneratorTest, SwitchFecOnOff) {
632 delegate_.SetCanWriteAnything();
633 creator_->set_max_packets_per_fec_group(2);
634 EXPECT_FALSE(creator_->IsFecProtected());
636 // Send one unprotected data packet.
637 EXPECT_CALL(delegate_, OnSerializedPacket(_)).WillOnce(
638 DoAll(SaveArg<0>(&packet_), Return(true)));
639 QuicConsumedData consumed =
640 generator_.ConsumeData(5, CreateData(1u), 0, true, MAY_FEC_PROTECT,
641 NULL);
642 EXPECT_EQ(1u, consumed.bytes_consumed);
643 EXPECT_FALSE(generator_.HasQueuedFrames());
644 EXPECT_FALSE(creator_->IsFecProtected());
645 // Verify that one data packet was sent.
646 PacketContents contents;
647 contents.num_stream_frames = 1;
648 CheckPacketContains(contents, packet_);
651 InSequence dummy;
652 EXPECT_CALL(delegate_, OnSerializedPacket(_)).WillOnce(
653 DoAll(SaveArg<0>(&packet2_), Return(true)));
654 EXPECT_CALL(delegate_, OnSerializedPacket(_)).WillOnce(
655 DoAll(SaveArg<0>(&packet3_), Return(true)));
656 EXPECT_CALL(delegate_, OnSerializedPacket(_)).WillOnce(
657 DoAll(SaveArg<0>(&packet4_), Return(true)));
658 EXPECT_CALL(delegate_, OnSerializedPacket(_)).WillOnce(
659 DoAll(SaveArg<0>(&packet5_), Return(true)));
660 EXPECT_CALL(delegate_, OnSerializedPacket(_)).WillOnce(
661 DoAll(SaveArg<0>(&packet6_), Return(true)));
663 // Send enough data to create 3 packets with MUST_FEC_PROTECT flag.
664 size_t data_len = 2 * kDefaultMaxPacketSize + 100;
665 consumed = generator_.ConsumeData(7, CreateData(data_len), 0, true,
666 MUST_FEC_PROTECT, NULL);
667 EXPECT_EQ(data_len, consumed.bytes_consumed);
668 EXPECT_FALSE(generator_.HasQueuedFrames());
670 // Verify that two FEC packets were sent.
671 CheckPacketHasSingleStreamFrame(packet2_);
672 CheckPacketHasSingleStreamFrame(packet3_);
673 CheckPacketIsFec(packet4_, /*fec_group=*/2u);
674 CheckPacketHasSingleStreamFrame(packet5_);
675 CheckPacketIsFec(packet6_, /*fec_group=*/5u); // Sent at the end of stream.
677 // Send one unprotected data packet.
678 EXPECT_CALL(delegate_, OnSerializedPacket(_)).WillOnce(
679 DoAll(SaveArg<0>(&packet7_), Return(true)));
680 consumed = generator_.ConsumeData(7, CreateData(1u), 0, true,
681 MAY_FEC_PROTECT, NULL);
682 EXPECT_EQ(1u, consumed.bytes_consumed);
683 EXPECT_FALSE(generator_.HasQueuedFrames());
684 EXPECT_FALSE(creator_->IsFecProtected());
685 // Verify that one unprotected data packet was sent.
686 CheckPacketContains(contents, packet7_);
689 TEST_F(QuicPacketGeneratorTest, SwitchFecOnWithPendingFrameInCreator) {
690 delegate_.SetCanWriteAnything();
691 // Enable FEC.
692 creator_->set_max_packets_per_fec_group(2);
694 generator_.StartBatchOperations();
695 // Queue enough data to prevent a stream frame with a non-zero offset from
696 // fitting.
697 QuicConsumedData consumed = generator_.ConsumeData(
698 7, CreateData(1u), 0, true, MAY_FEC_PROTECT, NULL);
699 EXPECT_EQ(1u, consumed.bytes_consumed);
700 EXPECT_TRUE(creator_->HasPendingFrames());
702 // Queue protected data for sending. Should cause queued frames to be flushed.
703 EXPECT_CALL(delegate_, OnSerializedPacket(_)).WillOnce(
704 DoAll(SaveArg<0>(&packet_), Return(true)));
705 EXPECT_FALSE(creator_->IsFecProtected());
706 consumed = generator_.ConsumeData(7, CreateData(1u), 0, true,
707 MUST_FEC_PROTECT, NULL);
708 EXPECT_EQ(1u, consumed.bytes_consumed);
709 PacketContents contents;
710 contents.num_stream_frames = 1;
711 // Transmitted packet was not FEC protected.
712 CheckPacketContains(contents, packet_);
713 EXPECT_TRUE(creator_->IsFecProtected());
714 EXPECT_TRUE(creator_->HasPendingFrames());
717 TEST_F(QuicPacketGeneratorTest, SwitchFecOnWithPendingFramesInGenerator) {
718 // Enable FEC.
719 creator_->set_max_packets_per_fec_group(2);
721 // Queue control frames in generator.
722 delegate_.SetCanNotWrite();
723 generator_.SetShouldSendAck(true, true);
724 delegate_.SetCanWriteAnything();
725 generator_.StartBatchOperations();
727 // Set up frames to write into the creator when control frames are written.
728 EXPECT_CALL(delegate_, CreateAckFrame()).WillOnce(Return(CreateAckFrame()));
729 EXPECT_CALL(delegate_, CreateFeedbackFrame()).WillOnce(
730 Return(CreateFeedbackFrame()));
731 EXPECT_CALL(delegate_, CreateStopWaitingFrame()).WillOnce(
732 Return(CreateStopWaitingFrame()));
734 // Generator should have queued control frames, and creator should be empty.
735 EXPECT_TRUE(generator_.HasQueuedFrames());
736 EXPECT_FALSE(creator_->HasPendingFrames());
737 EXPECT_FALSE(creator_->IsFecProtected());
739 // Queue protected data for sending. Should cause queued frames to be flushed.
740 EXPECT_CALL(delegate_, OnSerializedPacket(_)).WillOnce(
741 DoAll(SaveArg<0>(&packet_), Return(true)));
742 QuicConsumedData consumed = generator_.ConsumeData(7, CreateData(1u), 0, true,
743 MUST_FEC_PROTECT, NULL);
744 EXPECT_EQ(1u, consumed.bytes_consumed);
745 PacketContents contents;
746 contents.num_ack_frames = 1;
747 contents.num_feedback_frames = 1;
748 contents.num_stop_waiting_frames = 1;
749 CheckPacketContains(contents, packet_);
751 // FEC protection should be on in creator.
752 EXPECT_TRUE(creator_->IsFecProtected());
755 TEST_F(QuicPacketGeneratorTest, SwitchFecOnOffWithSubsequentFramesProtected) {
756 delegate_.SetCanWriteAnything();
758 // Enable FEC.
759 creator_->set_max_packets_per_fec_group(2);
760 EXPECT_FALSE(creator_->IsFecProtected());
762 // Queue stream frame to be protected in creator.
763 generator_.StartBatchOperations();
764 QuicConsumedData consumed = generator_.ConsumeData(5, CreateData(1u), 0, true,
765 MUST_FEC_PROTECT, NULL);
766 EXPECT_EQ(1u, consumed.bytes_consumed);
767 // Creator has a pending protected frame.
768 EXPECT_TRUE(creator_->HasPendingFrames());
769 EXPECT_TRUE(creator_->IsFecProtected());
771 // Add enough unprotected data to exceed size of current packet, so that
772 // current packet is sent. Both frames will be sent out in a single packet.
773 EXPECT_CALL(delegate_, OnSerializedPacket(_)).WillOnce(
774 DoAll(SaveArg<0>(&packet_), Return(true)));
775 size_t data_len = kDefaultMaxPacketSize;
776 consumed = generator_.ConsumeData(5, CreateData(data_len), 0, true,
777 MAY_FEC_PROTECT, NULL);
778 EXPECT_EQ(data_len, consumed.bytes_consumed);
779 PacketContents contents;
780 contents.num_stream_frames = 2u;
781 contents.fec_group = 1u;
782 CheckPacketContains(contents, packet_);
783 // FEC protection should still be on in creator.
784 EXPECT_TRUE(creator_->IsFecProtected());
787 TEST_F(QuicPacketGeneratorTest, SwitchFecOnOffWithSubsequentPacketsProtected) {
788 delegate_.SetCanWriteAnything();
790 // Enable FEC.
791 creator_->set_max_packets_per_fec_group(2);
792 EXPECT_FALSE(creator_->IsFecProtected());
794 generator_.StartBatchOperations();
795 // Send first packet, FEC protected.
796 EXPECT_CALL(delegate_, OnSerializedPacket(_)).WillOnce(
797 DoAll(SaveArg<0>(&packet_), Return(true)));
798 // Write enough data to cause a packet to be emitted.
799 size_t data_len = kDefaultMaxPacketSize;
800 QuicConsumedData consumed = generator_.ConsumeData(
801 5, CreateData(data_len), 0, true, MUST_FEC_PROTECT, NULL);
802 EXPECT_EQ(data_len, consumed.bytes_consumed);
803 PacketContents contents;
804 contents.num_stream_frames = 1u;
805 contents.fec_group = 1u;
806 CheckPacketContains(contents, packet_);
808 // FEC should still be on in creator.
809 EXPECT_TRUE(creator_->IsFecProtected());
811 // Send enough unprotected data to cause second packet to be sent, which gets
812 // protected because it happens to fall within an open FEC group. Data packet
813 // will be followed by FEC packet.
815 InSequence dummy;
816 EXPECT_CALL(delegate_, OnSerializedPacket(_)).WillOnce(
817 DoAll(SaveArg<0>(&packet2_), Return(true)));
818 EXPECT_CALL(delegate_, OnSerializedPacket(_)).WillOnce(
819 DoAll(SaveArg<0>(&packet3_), Return(true)));
821 consumed = generator_.ConsumeData(5, CreateData(data_len), 0, true,
822 MAY_FEC_PROTECT, NULL);
823 EXPECT_EQ(data_len, consumed.bytes_consumed);
824 contents.num_stream_frames = 2u;
825 CheckPacketContains(contents, packet2_);
826 CheckPacketIsFec(packet3_, /*fec_group=*/1u);
828 // FEC protection should be off in creator.
829 EXPECT_FALSE(creator_->IsFecProtected());
832 TEST_F(QuicPacketGeneratorTest, SwitchFecOnOffThenOnWithCreatorProtectionOn) {
833 delegate_.SetCanWriteAnything();
834 generator_.StartBatchOperations();
836 // Enable FEC.
837 creator_->set_max_packets_per_fec_group(2);
838 EXPECT_FALSE(creator_->IsFecProtected());
840 // Queue one byte of FEC protected data.
841 QuicConsumedData consumed = generator_.ConsumeData(5, CreateData(1u), 0, true,
842 MUST_FEC_PROTECT, NULL);
843 EXPECT_TRUE(creator_->HasPendingFrames());
845 // Add more unprotected data causing first packet to be sent, FEC protected.
846 EXPECT_CALL(delegate_, OnSerializedPacket(_)).WillOnce(
847 DoAll(SaveArg<0>(&packet_), Return(true)));
848 size_t data_len = kDefaultMaxPacketSize;
849 consumed = generator_.ConsumeData(5, CreateData(data_len), 0, true,
850 MAY_FEC_PROTECT, NULL);
851 EXPECT_EQ(data_len, consumed.bytes_consumed);
852 PacketContents contents;
853 contents.num_stream_frames = 2u;
854 contents.fec_group = 1u;
855 CheckPacketContains(contents, packet_);
857 // FEC group is still open in creator.
858 EXPECT_TRUE(creator_->IsFecProtected());
860 // Add data that should be protected, large enough to cause second packet to
861 // be sent. Data packet should be followed by FEC packet.
863 InSequence dummy;
864 EXPECT_CALL(delegate_, OnSerializedPacket(_)).WillOnce(
865 DoAll(SaveArg<0>(&packet2_), Return(true)));
866 EXPECT_CALL(delegate_, OnSerializedPacket(_)).WillOnce(
867 DoAll(SaveArg<0>(&packet3_), Return(true)));
869 consumed = generator_.ConsumeData(5, CreateData(data_len), 0, true,
870 MUST_FEC_PROTECT, NULL);
871 EXPECT_EQ(data_len, consumed.bytes_consumed);
872 CheckPacketContains(contents, packet2_);
873 CheckPacketIsFec(packet3_, /*fec_group=*/1u);
875 // FEC protection should remain on in creator.
876 EXPECT_TRUE(creator_->IsFecProtected());
879 TEST_F(QuicPacketGeneratorTest, NotWritableThenBatchOperations) {
880 delegate_.SetCanNotWrite();
882 generator_.SetShouldSendAck(true, false);
883 generator_.AddControlFrame(QuicFrame(CreateRstStreamFrame()));
884 EXPECT_TRUE(generator_.HasQueuedFrames());
886 delegate_.SetCanWriteAnything();
888 generator_.StartBatchOperations();
890 // When the first write operation is invoked, the ack and feedback
891 // frames will be returned.
892 EXPECT_CALL(delegate_, CreateAckFrame()).WillOnce(Return(CreateAckFrame()));
893 EXPECT_CALL(delegate_, CreateFeedbackFrame()).WillOnce(
894 Return(CreateFeedbackFrame()));
896 // Send some data and a control frame
897 generator_.ConsumeData(3, MakeIOVector("quux"), 7, false,
898 MAY_FEC_PROTECT, NULL);
899 generator_.AddControlFrame(QuicFrame(CreateGoAwayFrame()));
901 // All five frames will be flushed out in a single packet.
902 EXPECT_CALL(delegate_, OnSerializedPacket(_)).WillOnce(
903 DoAll(SaveArg<0>(&packet_), Return(true)));
904 generator_.FinishBatchOperations();
905 EXPECT_FALSE(generator_.HasQueuedFrames());
907 PacketContents contents;
908 contents.num_ack_frames = 1;
909 contents.num_goaway_frames = 1;
910 contents.num_feedback_frames = 1;
911 contents.num_rst_stream_frames = 1;
912 contents.num_stream_frames = 1;
913 CheckPacketContains(contents, packet_);
916 TEST_F(QuicPacketGeneratorTest, NotWritableThenBatchOperations2) {
917 delegate_.SetCanNotWrite();
919 generator_.SetShouldSendAck(true, false);
920 generator_.AddControlFrame(QuicFrame(CreateRstStreamFrame()));
921 EXPECT_TRUE(generator_.HasQueuedFrames());
923 delegate_.SetCanWriteAnything();
925 generator_.StartBatchOperations();
927 // When the first write operation is invoked, the ack and feedback
928 // frames will be returned.
929 EXPECT_CALL(delegate_, CreateAckFrame()).WillOnce(Return(CreateAckFrame()));
930 EXPECT_CALL(delegate_, CreateFeedbackFrame()).WillOnce(
931 Return(CreateFeedbackFrame()));
934 InSequence dummy;
935 // All five frames will be flushed out in a single packet
936 EXPECT_CALL(delegate_, OnSerializedPacket(_)).WillOnce(
937 DoAll(SaveArg<0>(&packet_), Return(true)));
938 EXPECT_CALL(delegate_, OnSerializedPacket(_)).WillOnce(
939 DoAll(SaveArg<0>(&packet2_), Return(true)));
942 // Send enough data to exceed one packet
943 size_t data_len = kDefaultMaxPacketSize + 100;
944 QuicConsumedData consumed =
945 generator_.ConsumeData(3, CreateData(data_len), 0, true,
946 MAY_FEC_PROTECT, NULL);
947 EXPECT_EQ(data_len, consumed.bytes_consumed);
948 EXPECT_TRUE(consumed.fin_consumed);
949 generator_.AddControlFrame(QuicFrame(CreateGoAwayFrame()));
951 generator_.FinishBatchOperations();
952 EXPECT_FALSE(generator_.HasQueuedFrames());
954 // The first packet should have the queued data and part of the stream data.
955 PacketContents contents;
956 contents.num_ack_frames = 1;
957 contents.num_feedback_frames = 1;
958 contents.num_rst_stream_frames = 1;
959 contents.num_stream_frames = 1;
960 CheckPacketContains(contents, packet_);
962 // The second should have the remainder of the stream data.
963 PacketContents contents2;
964 contents2.num_goaway_frames = 1;
965 contents2.num_stream_frames = 1;
966 CheckPacketContains(contents2, packet2_);
969 } // namespace test
970 } // namespace net