Lots of random cleanups, mostly for native_theme_win.cc:
[chromium-blink-merge.git] / net / quic / quic_packet_generator_test.cc
blob331b183466704bf71357ab24defc1aaa7d50c7fe
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, 0));
142 QuicCongestionFeedbackFrame* CreateFeedbackFrame() {
143 QuicCongestionFeedbackFrame* frame = new QuicCongestionFeedbackFrame;
144 frame->type = kFixRate;
145 frame->fix_rate.bitrate = QuicBandwidth::FromBytesPerSecond(42);
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(framer_.version(), 1, 0, false,
530 NOT_IN_FEC_GROUP) + 3 +
531 QuicFramer::GetMinStreamFrameSize(framer_.version(), 1, 0, true,
532 NOT_IN_FEC_GROUP) + 1;
533 creator_->set_max_packet_length(length);
534 delegate_.SetCanWriteAnything();
536 InSequence dummy;
537 EXPECT_CALL(delegate_, OnSerializedPacket(_)).WillOnce(
538 DoAll(SaveArg<0>(&packet_), Return(true)));
539 EXPECT_CALL(delegate_, OnSerializedPacket(_)).WillOnce(
540 DoAll(SaveArg<0>(&packet2_), Return(true)));
542 generator_.StartBatchOperations();
543 // Queue enough data to prevent a stream frame with a non-zero offset from
544 // fitting.
545 QuicConsumedData consumed = generator_.ConsumeData(
546 kHeadersStreamId, MakeIOVector("foo"), 0, false, MAY_FEC_PROTECT, NULL);
547 EXPECT_EQ(3u, consumed.bytes_consumed);
548 EXPECT_FALSE(consumed.fin_consumed);
549 EXPECT_TRUE(generator_.HasQueuedFrames());
551 // This frame will not fit with the existing frame, causing the queued frame
552 // to be serialized, and it will not fit with another frame like it, so it is
553 // serialized by itself.
554 consumed = generator_.ConsumeData(kHeadersStreamId, MakeIOVector("bar"), 3,
555 true, MAY_FEC_PROTECT, NULL);
556 EXPECT_EQ(3u, consumed.bytes_consumed);
557 EXPECT_TRUE(consumed.fin_consumed);
558 EXPECT_FALSE(generator_.HasQueuedFrames());
560 PacketContents contents;
561 contents.num_stream_frames = 1;
562 CheckPacketContains(contents, packet_);
563 CheckPacketContains(contents, packet2_);
566 TEST_F(QuicPacketGeneratorTest, SwitchFecOnOff) {
567 delegate_.SetCanWriteAnything();
568 // Enable FEC.
569 creator_->set_max_packets_per_fec_group(2);
570 EXPECT_FALSE(creator_->IsFecProtected());
572 // Send one unprotected data packet.
573 EXPECT_CALL(delegate_, OnSerializedPacket(_)).WillOnce(
574 DoAll(SaveArg<0>(&packet_), Return(true)));
575 QuicConsumedData consumed =
576 generator_.ConsumeData(5, CreateData(1u), 0, true, MAY_FEC_PROTECT,
577 NULL);
578 EXPECT_EQ(1u, consumed.bytes_consumed);
579 EXPECT_FALSE(generator_.HasQueuedFrames());
580 EXPECT_FALSE(creator_->IsFecProtected());
581 // Verify that one data packet was sent.
582 PacketContents contents;
583 contents.num_stream_frames = 1;
584 CheckPacketContains(contents, packet_);
587 InSequence dummy;
588 EXPECT_CALL(delegate_, OnSerializedPacket(_)).WillOnce(
589 DoAll(SaveArg<0>(&packet2_), Return(true)));
590 EXPECT_CALL(delegate_, OnSerializedPacket(_)).WillOnce(
591 DoAll(SaveArg<0>(&packet3_), Return(true)));
592 EXPECT_CALL(delegate_, OnSerializedPacket(_)).WillOnce(
593 DoAll(SaveArg<0>(&packet4_), Return(true)));
594 EXPECT_CALL(delegate_, OnSerializedPacket(_)).WillOnce(
595 DoAll(SaveArg<0>(&packet5_), Return(true)));
596 EXPECT_CALL(delegate_, OnSerializedPacket(_)).WillOnce(
597 DoAll(SaveArg<0>(&packet6_), Return(true)));
599 // Send enough data to create 3 packets with MUST_FEC_PROTECT flag.
600 size_t data_len = 2 * kDefaultMaxPacketSize + 100;
601 consumed = generator_.ConsumeData(7, CreateData(data_len), 0, true,
602 MUST_FEC_PROTECT, NULL);
603 EXPECT_EQ(data_len, consumed.bytes_consumed);
604 EXPECT_FALSE(generator_.HasQueuedFrames());
606 // Verify that two FEC packets were sent.
607 CheckPacketHasSingleStreamFrame(packet2_);
608 CheckPacketHasSingleStreamFrame(packet3_);
609 CheckPacketIsFec(packet4_, /*fec_group=*/2u);
610 CheckPacketHasSingleStreamFrame(packet5_);
611 CheckPacketIsFec(packet6_, /*fec_group=*/5u); // Sent at the end of stream.
613 // Send one unprotected data packet.
614 EXPECT_CALL(delegate_, OnSerializedPacket(_)).WillOnce(
615 DoAll(SaveArg<0>(&packet7_), Return(true)));
616 consumed = generator_.ConsumeData(7, CreateData(1u), 0, true,
617 MAY_FEC_PROTECT, NULL);
618 EXPECT_EQ(1u, consumed.bytes_consumed);
619 EXPECT_FALSE(generator_.HasQueuedFrames());
620 EXPECT_FALSE(creator_->IsFecProtected());
621 // Verify that one unprotected data packet was sent.
622 CheckPacketContains(contents, packet7_);
625 TEST_F(QuicPacketGeneratorTest, SwitchFecOnWithPendingFrameInCreator) {
626 delegate_.SetCanWriteAnything();
627 // Enable FEC.
628 creator_->set_max_packets_per_fec_group(2);
630 generator_.StartBatchOperations();
631 // Queue enough data to prevent a stream frame with a non-zero offset from
632 // fitting.
633 QuicConsumedData consumed = generator_.ConsumeData(
634 7, CreateData(1u), 0, true, MAY_FEC_PROTECT, NULL);
635 EXPECT_EQ(1u, consumed.bytes_consumed);
636 EXPECT_TRUE(creator_->HasPendingFrames());
638 // Queue protected data for sending. Should cause queued frames to be flushed.
639 EXPECT_CALL(delegate_, OnSerializedPacket(_)).WillOnce(
640 DoAll(SaveArg<0>(&packet_), Return(true)));
641 EXPECT_FALSE(creator_->IsFecProtected());
642 consumed = generator_.ConsumeData(7, CreateData(1u), 0, true,
643 MUST_FEC_PROTECT, NULL);
644 EXPECT_EQ(1u, consumed.bytes_consumed);
645 PacketContents contents;
646 contents.num_stream_frames = 1;
647 // Transmitted packet was not FEC protected.
648 CheckPacketContains(contents, packet_);
649 EXPECT_TRUE(creator_->IsFecProtected());
650 EXPECT_TRUE(creator_->HasPendingFrames());
653 TEST_F(QuicPacketGeneratorTest, SwitchFecOnWithPendingFramesInGenerator) {
654 // Enable FEC.
655 creator_->set_max_packets_per_fec_group(2);
657 // Queue control frames in generator.
658 delegate_.SetCanNotWrite();
659 generator_.SetShouldSendAck(true, true);
660 delegate_.SetCanWriteAnything();
661 generator_.StartBatchOperations();
663 // Set up frames to write into the creator when control frames are written.
664 EXPECT_CALL(delegate_, CreateAckFrame()).WillOnce(Return(CreateAckFrame()));
665 EXPECT_CALL(delegate_, CreateFeedbackFrame()).WillOnce(
666 Return(CreateFeedbackFrame()));
667 EXPECT_CALL(delegate_, CreateStopWaitingFrame()).WillOnce(
668 Return(CreateStopWaitingFrame()));
670 // Generator should have queued control frames, and creator should be empty.
671 EXPECT_TRUE(generator_.HasQueuedFrames());
672 EXPECT_FALSE(creator_->HasPendingFrames());
673 EXPECT_FALSE(creator_->IsFecProtected());
675 // Queue protected data for sending. Should cause queued frames to be flushed.
676 EXPECT_CALL(delegate_, OnSerializedPacket(_)).WillOnce(
677 DoAll(SaveArg<0>(&packet_), Return(true)));
678 QuicConsumedData consumed = generator_.ConsumeData(7, CreateData(1u), 0, true,
679 MUST_FEC_PROTECT, NULL);
680 EXPECT_EQ(1u, consumed.bytes_consumed);
681 PacketContents contents;
682 contents.num_ack_frames = 1;
683 contents.num_feedback_frames = 1;
684 contents.num_stop_waiting_frames = 1;
685 CheckPacketContains(contents, packet_);
687 // FEC protection should be on in creator.
688 EXPECT_TRUE(creator_->IsFecProtected());
691 TEST_F(QuicPacketGeneratorTest, SwitchFecOnOffWithSubsequentFramesProtected) {
692 delegate_.SetCanWriteAnything();
694 // Enable FEC.
695 creator_->set_max_packets_per_fec_group(2);
696 EXPECT_FALSE(creator_->IsFecProtected());
698 // Queue stream frame to be protected in creator.
699 generator_.StartBatchOperations();
700 QuicConsumedData consumed = generator_.ConsumeData(5, CreateData(1u), 0, true,
701 MUST_FEC_PROTECT, NULL);
702 EXPECT_EQ(1u, consumed.bytes_consumed);
703 // Creator has a pending protected frame.
704 EXPECT_TRUE(creator_->HasPendingFrames());
705 EXPECT_TRUE(creator_->IsFecProtected());
707 // Add enough unprotected data to exceed size of current packet, so that
708 // current packet is sent. Both frames will be sent out in a single packet.
709 EXPECT_CALL(delegate_, OnSerializedPacket(_)).WillOnce(
710 DoAll(SaveArg<0>(&packet_), Return(true)));
711 size_t data_len = kDefaultMaxPacketSize;
712 consumed = generator_.ConsumeData(5, CreateData(data_len), 0, true,
713 MAY_FEC_PROTECT, NULL);
714 EXPECT_EQ(data_len, consumed.bytes_consumed);
715 PacketContents contents;
716 contents.num_stream_frames = 2u;
717 contents.fec_group = 1u;
718 CheckPacketContains(contents, packet_);
719 // FEC protection should still be on in creator.
720 EXPECT_TRUE(creator_->IsFecProtected());
723 TEST_F(QuicPacketGeneratorTest, SwitchFecOnOffWithSubsequentPacketsProtected) {
724 delegate_.SetCanWriteAnything();
726 // Enable FEC.
727 creator_->set_max_packets_per_fec_group(2);
728 EXPECT_FALSE(creator_->IsFecProtected());
730 generator_.StartBatchOperations();
731 // Send first packet, FEC protected.
732 EXPECT_CALL(delegate_, OnSerializedPacket(_)).WillOnce(
733 DoAll(SaveArg<0>(&packet_), Return(true)));
734 // Write enough data to cause a packet to be emitted.
735 size_t data_len = kDefaultMaxPacketSize;
736 QuicConsumedData consumed = generator_.ConsumeData(
737 5, CreateData(data_len), 0, true, MUST_FEC_PROTECT, NULL);
738 EXPECT_EQ(data_len, consumed.bytes_consumed);
739 PacketContents contents;
740 contents.num_stream_frames = 1u;
741 contents.fec_group = 1u;
742 CheckPacketContains(contents, packet_);
744 // FEC should still be on in creator.
745 EXPECT_TRUE(creator_->IsFecProtected());
747 // Send enough unprotected data to cause second packet to be sent, which gets
748 // protected because it happens to fall within an open FEC group. Data packet
749 // will be followed by FEC packet.
751 InSequence dummy;
752 EXPECT_CALL(delegate_, OnSerializedPacket(_)).WillOnce(
753 DoAll(SaveArg<0>(&packet2_), Return(true)));
754 EXPECT_CALL(delegate_, OnSerializedPacket(_)).WillOnce(
755 DoAll(SaveArg<0>(&packet3_), Return(true)));
757 consumed = generator_.ConsumeData(5, CreateData(data_len), 0, true,
758 MAY_FEC_PROTECT, NULL);
759 EXPECT_EQ(data_len, consumed.bytes_consumed);
760 contents.num_stream_frames = 2u;
761 CheckPacketContains(contents, packet2_);
762 CheckPacketIsFec(packet3_, /*fec_group=*/1u);
764 // FEC protection should be off in creator.
765 EXPECT_FALSE(creator_->IsFecProtected());
768 TEST_F(QuicPacketGeneratorTest, SwitchFecOnOffThenOnWithCreatorProtectionOn) {
769 delegate_.SetCanWriteAnything();
770 generator_.StartBatchOperations();
772 // Enable FEC.
773 creator_->set_max_packets_per_fec_group(2);
774 EXPECT_FALSE(creator_->IsFecProtected());
776 // Queue one byte of FEC protected data.
777 QuicConsumedData consumed = generator_.ConsumeData(5, CreateData(1u), 0, true,
778 MUST_FEC_PROTECT, NULL);
779 EXPECT_TRUE(creator_->HasPendingFrames());
781 // Add more unprotected data causing first packet to be sent, FEC protected.
782 EXPECT_CALL(delegate_, OnSerializedPacket(_)).WillOnce(
783 DoAll(SaveArg<0>(&packet_), Return(true)));
784 size_t data_len = kDefaultMaxPacketSize;
785 consumed = generator_.ConsumeData(5, CreateData(data_len), 0, true,
786 MAY_FEC_PROTECT, NULL);
787 EXPECT_EQ(data_len, consumed.bytes_consumed);
788 PacketContents contents;
789 contents.num_stream_frames = 2u;
790 contents.fec_group = 1u;
791 CheckPacketContains(contents, packet_);
793 // FEC group is still open in creator.
794 EXPECT_TRUE(creator_->IsFecProtected());
796 // Add data that should be protected, large enough to cause second packet to
797 // be sent. Data packet should be followed by FEC packet.
799 InSequence dummy;
800 EXPECT_CALL(delegate_, OnSerializedPacket(_)).WillOnce(
801 DoAll(SaveArg<0>(&packet2_), Return(true)));
802 EXPECT_CALL(delegate_, OnSerializedPacket(_)).WillOnce(
803 DoAll(SaveArg<0>(&packet3_), Return(true)));
805 consumed = generator_.ConsumeData(5, CreateData(data_len), 0, true,
806 MUST_FEC_PROTECT, NULL);
807 EXPECT_EQ(data_len, consumed.bytes_consumed);
808 CheckPacketContains(contents, packet2_);
809 CheckPacketIsFec(packet3_, /*fec_group=*/1u);
811 // FEC protection should remain on in creator.
812 EXPECT_TRUE(creator_->IsFecProtected());
815 TEST_F(QuicPacketGeneratorTest, NotWritableThenBatchOperations) {
816 delegate_.SetCanNotWrite();
818 generator_.SetShouldSendAck(true, false);
819 generator_.AddControlFrame(QuicFrame(CreateRstStreamFrame()));
820 EXPECT_TRUE(generator_.HasQueuedFrames());
822 delegate_.SetCanWriteAnything();
824 generator_.StartBatchOperations();
826 // When the first write operation is invoked, the ack and feedback
827 // frames will be returned.
828 EXPECT_CALL(delegate_, CreateAckFrame()).WillOnce(Return(CreateAckFrame()));
829 EXPECT_CALL(delegate_, CreateFeedbackFrame()).WillOnce(
830 Return(CreateFeedbackFrame()));
832 // Send some data and a control frame
833 generator_.ConsumeData(3, MakeIOVector("quux"), 7, false,
834 MAY_FEC_PROTECT, NULL);
835 generator_.AddControlFrame(QuicFrame(CreateGoAwayFrame()));
837 // All five frames will be flushed out in a single packet.
838 EXPECT_CALL(delegate_, OnSerializedPacket(_)).WillOnce(
839 DoAll(SaveArg<0>(&packet_), Return(true)));
840 generator_.FinishBatchOperations();
841 EXPECT_FALSE(generator_.HasQueuedFrames());
843 PacketContents contents;
844 contents.num_ack_frames = 1;
845 contents.num_goaway_frames = 1;
846 contents.num_feedback_frames = 1;
847 contents.num_rst_stream_frames = 1;
848 contents.num_stream_frames = 1;
849 CheckPacketContains(contents, packet_);
852 TEST_F(QuicPacketGeneratorTest, NotWritableThenBatchOperations2) {
853 delegate_.SetCanNotWrite();
855 generator_.SetShouldSendAck(true, false);
856 generator_.AddControlFrame(QuicFrame(CreateRstStreamFrame()));
857 EXPECT_TRUE(generator_.HasQueuedFrames());
859 delegate_.SetCanWriteAnything();
861 generator_.StartBatchOperations();
863 // When the first write operation is invoked, the ack and feedback
864 // frames will be returned.
865 EXPECT_CALL(delegate_, CreateAckFrame()).WillOnce(Return(CreateAckFrame()));
866 EXPECT_CALL(delegate_, CreateFeedbackFrame()).WillOnce(
867 Return(CreateFeedbackFrame()));
870 InSequence dummy;
871 // All five frames will be flushed out in a single packet
872 EXPECT_CALL(delegate_, OnSerializedPacket(_)).WillOnce(
873 DoAll(SaveArg<0>(&packet_), Return(true)));
874 EXPECT_CALL(delegate_, OnSerializedPacket(_)).WillOnce(
875 DoAll(SaveArg<0>(&packet2_), Return(true)));
878 // Send enough data to exceed one packet
879 size_t data_len = kDefaultMaxPacketSize + 100;
880 QuicConsumedData consumed =
881 generator_.ConsumeData(3, CreateData(data_len), 0, true,
882 MAY_FEC_PROTECT, NULL);
883 EXPECT_EQ(data_len, consumed.bytes_consumed);
884 EXPECT_TRUE(consumed.fin_consumed);
885 generator_.AddControlFrame(QuicFrame(CreateGoAwayFrame()));
887 generator_.FinishBatchOperations();
888 EXPECT_FALSE(generator_.HasQueuedFrames());
890 // The first packet should have the queued data and part of the stream data.
891 PacketContents contents;
892 contents.num_ack_frames = 1;
893 contents.num_feedback_frames = 1;
894 contents.num_rst_stream_frames = 1;
895 contents.num_stream_frames = 1;
896 CheckPacketContains(contents, packet_);
898 // The second should have the remainder of the stream data.
899 PacketContents contents2;
900 contents2.num_goaway_frames = 1;
901 contents2.num_stream_frames = 1;
902 CheckPacketContains(contents2, packet2_);
905 } // namespace test
906 } // namespace net