Update V8 to version 4.6.61.
[chromium-blink-merge.git] / net / quic / quic_packet_creator_test.cc
blob46cf52818d83070c44057f4537e10a7e0d956c3b
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_creator.h"
7 #include <stdint.h>
9 #include "base/stl_util.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/mock_random.h"
15 #include "net/quic/test_tools/quic_framer_peer.h"
16 #include "net/quic/test_tools/quic_packet_creator_peer.h"
17 #include "net/quic/test_tools/quic_test_utils.h"
18 #include "net/test/gtest_util.h"
19 #include "testing/gmock/include/gmock/gmock.h"
21 using base::StringPiece;
22 using std::ostream;
23 using std::string;
24 using std::vector;
25 using testing::DoAll;
26 using testing::InSequence;
27 using testing::Return;
28 using testing::SaveArg;
29 using testing::_;
31 namespace net {
32 namespace test {
33 namespace {
35 // Run tests with combinations of {QuicVersion, ToggleVersionSerialization}.
36 struct TestParams {
37 TestParams(QuicVersion version,
38 bool version_serialization,
39 QuicConnectionIdLength length)
40 : version(version),
41 connection_id_length(length),
42 version_serialization(version_serialization) {
45 friend ostream& operator<<(ostream& os, const TestParams& p) {
46 os << "{ client_version: " << QuicVersionToString(p.version)
47 << " connection id length: " << p.connection_id_length
48 << " include version: " << p.version_serialization << " }";
49 return os;
52 QuicVersion version;
53 QuicConnectionIdLength connection_id_length;
54 bool version_serialization;
57 // Constructs various test permutations.
58 vector<TestParams> GetTestParams() {
59 vector<TestParams> params;
60 QuicConnectionIdLength max = PACKET_8BYTE_CONNECTION_ID;
61 QuicVersionVector all_supported_versions = QuicSupportedVersions();
62 for (size_t i = 0; i < all_supported_versions.size(); ++i) {
63 params.push_back(TestParams(all_supported_versions[i], true, max));
64 params.push_back(TestParams(all_supported_versions[i], false, max));
66 params.push_back(TestParams(
67 all_supported_versions[0], true, PACKET_0BYTE_CONNECTION_ID));
68 params.push_back(TestParams(
69 all_supported_versions[0], true, PACKET_1BYTE_CONNECTION_ID));
70 params.push_back(TestParams(
71 all_supported_versions[0], true, PACKET_4BYTE_CONNECTION_ID));
72 return params;
75 class QuicPacketCreatorTest : public ::testing::TestWithParam<TestParams> {
76 protected:
77 QuicPacketCreatorTest()
78 : server_framer_(SupportedVersions(GetParam().version),
79 QuicTime::Zero(),
80 Perspective::IS_SERVER),
81 client_framer_(SupportedVersions(GetParam().version),
82 QuicTime::Zero(),
83 Perspective::IS_CLIENT),
84 connection_id_(2),
85 data_("foo"),
86 creator_(connection_id_, &client_framer_, &mock_random_) {
87 creator_.set_connection_id_length(GetParam().connection_id_length);
88 client_framer_.set_visitor(&framer_visitor_);
89 client_framer_.set_received_entropy_calculator(&entropy_calculator_);
90 server_framer_.set_visitor(&framer_visitor_);
93 ~QuicPacketCreatorTest() override {}
95 void ProcessPacket(QuicEncryptedPacket* encrypted) {
96 server_framer_.ProcessPacket(*encrypted);
99 void CheckStreamFrame(const QuicFrame& frame,
100 QuicStreamId stream_id,
101 const string& data,
102 QuicStreamOffset offset,
103 bool fin) {
104 EXPECT_EQ(STREAM_FRAME, frame.type);
105 ASSERT_TRUE(frame.stream_frame);
106 EXPECT_EQ(stream_id, frame.stream_frame->stream_id);
107 EXPECT_EQ(data, frame.stream_frame->data);
108 EXPECT_EQ(offset, frame.stream_frame->offset);
109 EXPECT_EQ(fin, frame.stream_frame->fin);
112 // Returns the number of bytes consumed by the header of packet, including
113 // the version.
114 size_t GetPacketHeaderOverhead(InFecGroup is_in_fec_group) {
115 return GetPacketHeaderSize(
116 creator_.connection_id_length(), kIncludeVersion,
117 QuicPacketCreatorPeer::NextSequenceNumberLength(&creator_),
118 is_in_fec_group);
121 // Returns the number of bytes of overhead that will be added to a packet
122 // of maximum length.
123 size_t GetEncryptionOverhead() {
124 return creator_.max_packet_length() - client_framer_.GetMaxPlaintextSize(
125 creator_.max_packet_length());
128 // Returns the number of bytes consumed by the non-data fields of a stream
129 // frame, assuming it is the last frame in the packet
130 size_t GetStreamFrameOverhead(InFecGroup is_in_fec_group) {
131 return QuicFramer::GetMinStreamFrameSize(kClientDataStreamId1, kOffset,
132 true, is_in_fec_group);
135 // Enables and turns on FEC protection. Returns true if FEC protection is on.
136 bool SwitchFecProtectionOn(size_t max_packets_per_fec_group) {
137 creator_.set_max_packets_per_fec_group(max_packets_per_fec_group);
138 creator_.StartFecProtectingPackets();
139 return creator_.IsFecProtected();
142 QuicIOVector MakeIOVector(StringPiece s) {
143 return ::net::MakeIOVector(s, &iov_);
146 static const QuicStreamOffset kOffset = 1u;
148 QuicFrames frames_;
149 QuicFramer server_framer_;
150 QuicFramer client_framer_;
151 testing::StrictMock<MockFramerVisitor> framer_visitor_;
152 QuicConnectionId connection_id_;
153 string data_;
154 struct iovec iov_;
155 MockRandom mock_random_;
156 QuicPacketCreator creator_;
157 MockEntropyCalculator entropy_calculator_;
160 // Run all packet creator tests with all supported versions of QUIC, and with
161 // and without version in the packet header, as well as doing a run for each
162 // length of truncated connection id.
163 INSTANTIATE_TEST_CASE_P(QuicPacketCreatorTests,
164 QuicPacketCreatorTest,
165 ::testing::ValuesIn(GetTestParams()));
167 TEST_P(QuicPacketCreatorTest, SerializeFrames) {
168 frames_.push_back(QuicFrame(new QuicAckFrame(MakeAckFrame(0u))));
169 frames_.push_back(
170 QuicFrame(new QuicStreamFrame(0u, false, 0u, StringPiece())));
171 frames_.push_back(
172 QuicFrame(new QuicStreamFrame(0u, true, 0u, StringPiece())));
173 char buffer[kMaxPacketSize];
174 SerializedPacket serialized =
175 creator_.SerializeAllFrames(frames_, buffer, kMaxPacketSize);
176 delete frames_[0].ack_frame;
177 delete frames_[1].stream_frame;
178 delete frames_[2].stream_frame;
181 InSequence s;
182 EXPECT_CALL(framer_visitor_, OnPacket());
183 EXPECT_CALL(framer_visitor_, OnUnauthenticatedPublicHeader(_));
184 EXPECT_CALL(framer_visitor_, OnUnauthenticatedHeader(_));
185 EXPECT_CALL(framer_visitor_, OnDecryptedPacket(_));
186 EXPECT_CALL(framer_visitor_, OnPacketHeader(_));
187 EXPECT_CALL(framer_visitor_, OnAckFrame(_));
188 EXPECT_CALL(framer_visitor_, OnStreamFrame(_));
189 EXPECT_CALL(framer_visitor_, OnStreamFrame(_));
190 EXPECT_CALL(framer_visitor_, OnPacketComplete());
192 ProcessPacket(serialized.packet);
193 delete serialized.packet;
196 TEST_P(QuicPacketCreatorTest, SerializeWithFEC) {
197 // Enable FEC protection, and send FEC packet every 6 packets.
198 EXPECT_TRUE(SwitchFecProtectionOn(6));
199 // Should return false since we do not have enough packets in the FEC group to
200 // trigger an FEC packet.
201 ASSERT_FALSE(creator_.ShouldSendFec(/*force_close=*/false));
203 frames_.push_back(
204 QuicFrame(new QuicStreamFrame(0u, false, 0u, StringPiece())));
205 char buffer[kMaxPacketSize];
206 SerializedPacket serialized =
207 creator_.SerializeAllFrames(frames_, buffer, kMaxPacketSize);
208 delete frames_[0].stream_frame;
211 InSequence s;
212 EXPECT_CALL(framer_visitor_, OnPacket());
213 EXPECT_CALL(framer_visitor_, OnUnauthenticatedPublicHeader(_));
214 EXPECT_CALL(framer_visitor_, OnUnauthenticatedHeader(_));
215 EXPECT_CALL(framer_visitor_, OnDecryptedPacket(_));
216 EXPECT_CALL(framer_visitor_, OnPacketHeader(_));
217 EXPECT_CALL(framer_visitor_, OnFecProtectedPayload(_));
218 EXPECT_CALL(framer_visitor_, OnStreamFrame(_));
219 EXPECT_CALL(framer_visitor_, OnPacketComplete());
221 ProcessPacket(serialized.packet);
222 delete serialized.packet;
224 // Should return false since we do not have enough packets in the FEC group to
225 // trigger an FEC packet.
226 ASSERT_FALSE(creator_.ShouldSendFec(/*force_close=*/false));
227 // Should return true since there are packets in the FEC group.
228 ASSERT_TRUE(creator_.ShouldSendFec(/*force_close=*/true));
230 serialized = creator_.SerializeFec(buffer, kMaxPacketSize);
231 ASSERT_EQ(2u, serialized.sequence_number);
233 InSequence s;
234 EXPECT_CALL(framer_visitor_, OnPacket());
235 EXPECT_CALL(framer_visitor_, OnUnauthenticatedPublicHeader(_));
236 EXPECT_CALL(framer_visitor_, OnUnauthenticatedHeader(_));
237 EXPECT_CALL(framer_visitor_, OnDecryptedPacket(_));
238 EXPECT_CALL(framer_visitor_, OnPacketHeader(_));
239 EXPECT_CALL(framer_visitor_, OnFecData(_));
240 EXPECT_CALL(framer_visitor_, OnPacketComplete());
242 ProcessPacket(serialized.packet);
243 delete serialized.packet;
246 TEST_P(QuicPacketCreatorTest, SerializeChangingSequenceNumberLength) {
247 frames_.push_back(QuicFrame(new QuicAckFrame(MakeAckFrame(0u))));
248 creator_.AddSavedFrame(frames_[0]);
249 QuicPacketCreatorPeer::SetNextSequenceNumberLength(
250 &creator_, PACKET_4BYTE_SEQUENCE_NUMBER);
251 char buffer[kMaxPacketSize];
252 SerializedPacket serialized =
253 creator_.SerializePacket(buffer, kMaxPacketSize);
254 // The sequence number length will not change mid-packet.
255 EXPECT_EQ(PACKET_1BYTE_SEQUENCE_NUMBER, serialized.sequence_number_length);
258 InSequence s;
259 EXPECT_CALL(framer_visitor_, OnPacket());
260 EXPECT_CALL(framer_visitor_, OnUnauthenticatedPublicHeader(_));
261 EXPECT_CALL(framer_visitor_, OnUnauthenticatedHeader(_));
262 EXPECT_CALL(framer_visitor_, OnDecryptedPacket(_));
263 EXPECT_CALL(framer_visitor_, OnPacketHeader(_));
264 EXPECT_CALL(framer_visitor_, OnAckFrame(_));
265 EXPECT_CALL(framer_visitor_, OnPacketComplete());
267 ProcessPacket(serialized.packet);
268 delete serialized.packet;
270 creator_.AddSavedFrame(frames_[0]);
271 serialized = creator_.SerializePacket(buffer, kMaxPacketSize);
272 // Now the actual sequence number length should have changed.
273 EXPECT_EQ(PACKET_4BYTE_SEQUENCE_NUMBER, serialized.sequence_number_length);
274 delete frames_[0].ack_frame;
277 InSequence s;
278 EXPECT_CALL(framer_visitor_, OnPacket());
279 EXPECT_CALL(framer_visitor_, OnUnauthenticatedPublicHeader(_));
280 EXPECT_CALL(framer_visitor_, OnUnauthenticatedHeader(_));
281 EXPECT_CALL(framer_visitor_, OnDecryptedPacket(_));
282 EXPECT_CALL(framer_visitor_, OnPacketHeader(_));
283 EXPECT_CALL(framer_visitor_, OnAckFrame(_));
284 EXPECT_CALL(framer_visitor_, OnPacketComplete());
286 ProcessPacket(serialized.packet);
287 delete serialized.packet;
290 TEST_P(QuicPacketCreatorTest, ChangeSequenceNumberLengthMidPacket) {
291 // Changing the sequence number length with queued frames in the creator
292 // should hold the change until after any currently queued frames are
293 // serialized.
295 // Packet 1.
296 // Queue a frame in the creator.
297 EXPECT_FALSE(creator_.HasPendingFrames());
298 QuicFrame ack_frame = QuicFrame(new QuicAckFrame(MakeAckFrame(0u)));
299 creator_.AddSavedFrame(ack_frame);
301 // Now change sequence number length.
302 QuicPacketCreatorPeer::SetNextSequenceNumberLength(
303 &creator_, PACKET_4BYTE_SEQUENCE_NUMBER);
305 // Add a STOP_WAITING frame since it contains a packet sequence number,
306 // whose length should be 1.
307 QuicStopWaitingFrame stop_waiting_frame;
308 EXPECT_TRUE(creator_.AddSavedFrame(QuicFrame(&stop_waiting_frame)));
309 EXPECT_TRUE(creator_.HasPendingFrames());
311 // Ensure the packet is successfully created.
312 char buffer[kMaxPacketSize];
313 SerializedPacket serialized =
314 creator_.SerializePacket(buffer, kMaxPacketSize);
315 ASSERT_TRUE(serialized.packet);
316 EXPECT_EQ(PACKET_1BYTE_SEQUENCE_NUMBER, serialized.sequence_number_length);
318 // Verify that header in transmitted packet has 1 byte sequence length.
319 QuicPacketHeader header;
321 InSequence s;
322 EXPECT_CALL(framer_visitor_, OnPacket());
323 EXPECT_CALL(framer_visitor_, OnUnauthenticatedPublicHeader(_));
324 EXPECT_CALL(framer_visitor_, OnUnauthenticatedHeader(_));
325 EXPECT_CALL(framer_visitor_, OnDecryptedPacket(_));
326 EXPECT_CALL(framer_visitor_, OnPacketHeader(_)).WillOnce(
327 DoAll(SaveArg<0>(&header), Return(true)));
328 EXPECT_CALL(framer_visitor_, OnAckFrame(_));
329 EXPECT_CALL(framer_visitor_, OnStopWaitingFrame(_));
330 EXPECT_CALL(framer_visitor_, OnPacketComplete());
332 ProcessPacket(serialized.packet);
333 EXPECT_EQ(PACKET_1BYTE_SEQUENCE_NUMBER,
334 header.public_header.sequence_number_length);
335 delete serialized.packet;
337 // Packet 2.
338 EXPECT_FALSE(creator_.HasPendingFrames());
339 // Generate Packet 2 with one frame -- sequence number length should now
340 // change to 4 bytes.
341 EXPECT_TRUE(creator_.AddSavedFrame(QuicFrame(&stop_waiting_frame)));
342 EXPECT_TRUE(creator_.HasPendingFrames());
344 // Ensure the packet is successfully created.
345 serialized = creator_.SerializePacket(buffer, kMaxPacketSize);
346 ASSERT_TRUE(serialized.packet);
347 EXPECT_EQ(PACKET_4BYTE_SEQUENCE_NUMBER, serialized.sequence_number_length);
349 // Verify that header in transmitted packet has 4 byte sequence length.
351 InSequence s;
352 EXPECT_CALL(framer_visitor_, OnPacket());
353 EXPECT_CALL(framer_visitor_, OnUnauthenticatedPublicHeader(_));
354 EXPECT_CALL(framer_visitor_, OnUnauthenticatedHeader(_));
355 EXPECT_CALL(framer_visitor_, OnDecryptedPacket(_));
356 EXPECT_CALL(framer_visitor_, OnPacketHeader(_)).WillOnce(
357 DoAll(SaveArg<0>(&header), Return(true)));
358 EXPECT_CALL(framer_visitor_, OnStopWaitingFrame(_));
359 EXPECT_CALL(framer_visitor_, OnPacketComplete());
361 ProcessPacket(serialized.packet);
362 EXPECT_EQ(PACKET_4BYTE_SEQUENCE_NUMBER,
363 header.public_header.sequence_number_length);
365 delete serialized.packet;
366 delete ack_frame.ack_frame;
369 TEST_P(QuicPacketCreatorTest, SerializeWithFECChangingSequenceNumberLength) {
370 // Test goal is to test the following sequence (P1 => generate Packet 1):
371 // P1 <change seq num length> P2 FEC,
372 // and we expect that sequence number length should not change until the end
373 // of the open FEC group.
375 // Enable FEC protection, and send FEC packet every 6 packets.
376 EXPECT_TRUE(SwitchFecProtectionOn(6));
377 // Should return false since we do not have enough packets in the FEC group to
378 // trigger an FEC packet.
379 ASSERT_FALSE(creator_.ShouldSendFec(/*force_close=*/false));
380 frames_.push_back(QuicFrame(new QuicAckFrame(MakeAckFrame(0u))));
382 // Generate Packet 1.
383 creator_.AddSavedFrame(frames_[0]);
384 // Change the sequence number length mid-FEC group and it should not change.
385 QuicPacketCreatorPeer::SetNextSequenceNumberLength(
386 &creator_, PACKET_4BYTE_SEQUENCE_NUMBER);
387 char buffer[kMaxPacketSize];
388 SerializedPacket serialized =
389 creator_.SerializePacket(buffer, kMaxPacketSize);
390 EXPECT_EQ(PACKET_1BYTE_SEQUENCE_NUMBER, serialized.sequence_number_length);
393 InSequence s;
394 EXPECT_CALL(framer_visitor_, OnPacket());
395 EXPECT_CALL(framer_visitor_, OnUnauthenticatedPublicHeader(_));
396 EXPECT_CALL(framer_visitor_, OnUnauthenticatedHeader(_));
397 EXPECT_CALL(framer_visitor_, OnDecryptedPacket(_));
398 EXPECT_CALL(framer_visitor_, OnPacketHeader(_));
399 EXPECT_CALL(framer_visitor_, OnFecProtectedPayload(_));
400 EXPECT_CALL(framer_visitor_, OnAckFrame(_));
401 EXPECT_CALL(framer_visitor_, OnPacketComplete());
403 ProcessPacket(serialized.packet);
404 delete serialized.packet;
406 // Generate Packet 2.
407 creator_.AddSavedFrame(frames_[0]);
408 serialized = creator_.SerializePacket(buffer, kMaxPacketSize);
409 EXPECT_EQ(PACKET_1BYTE_SEQUENCE_NUMBER, serialized.sequence_number_length);
412 InSequence s;
413 EXPECT_CALL(framer_visitor_, OnPacket());
414 EXPECT_CALL(framer_visitor_, OnUnauthenticatedPublicHeader(_));
415 EXPECT_CALL(framer_visitor_, OnUnauthenticatedHeader(_));
416 EXPECT_CALL(framer_visitor_, OnDecryptedPacket(_));
417 EXPECT_CALL(framer_visitor_, OnPacketHeader(_));
418 EXPECT_CALL(framer_visitor_, OnFecProtectedPayload(_));
419 EXPECT_CALL(framer_visitor_, OnAckFrame(_));
420 EXPECT_CALL(framer_visitor_, OnPacketComplete());
422 ProcessPacket(serialized.packet);
423 delete serialized.packet;
425 // Should return false since we do not have enough packets in the FEC group to
426 // trigger an FEC packet.
427 ASSERT_FALSE(creator_.ShouldSendFec(/*force_close=*/false));
428 // Should return true since there are packets in the FEC group.
429 ASSERT_TRUE(creator_.ShouldSendFec(/*force_close=*/true));
431 // Force generation of FEC packet.
432 serialized = creator_.SerializeFec(buffer, kMaxPacketSize);
433 EXPECT_EQ(PACKET_1BYTE_SEQUENCE_NUMBER, serialized.sequence_number_length);
434 ASSERT_EQ(3u, serialized.sequence_number);
437 InSequence s;
438 EXPECT_CALL(framer_visitor_, OnPacket());
439 EXPECT_CALL(framer_visitor_, OnUnauthenticatedPublicHeader(_));
440 EXPECT_CALL(framer_visitor_, OnUnauthenticatedHeader(_));
441 EXPECT_CALL(framer_visitor_, OnDecryptedPacket(_));
442 EXPECT_CALL(framer_visitor_, OnPacketHeader(_));
443 EXPECT_CALL(framer_visitor_, OnFecData(_));
444 EXPECT_CALL(framer_visitor_, OnPacketComplete());
446 ProcessPacket(serialized.packet);
447 delete serialized.packet;
449 // Ensure the next FEC group starts using the new sequence number length.
450 serialized = creator_.SerializeAllFrames(frames_, buffer, kMaxPacketSize);
451 EXPECT_EQ(PACKET_4BYTE_SEQUENCE_NUMBER, serialized.sequence_number_length);
452 delete frames_[0].ack_frame;
453 delete serialized.packet;
456 TEST_P(QuicPacketCreatorTest, ReserializeFramesWithSequenceNumberLength) {
457 // If the original packet sequence number length, the current sequence number
458 // length, and the configured send sequence number length are different, the
459 // retransmit must sent with the original length and the others do not change.
460 QuicPacketCreatorPeer::SetNextSequenceNumberLength(
461 &creator_, PACKET_4BYTE_SEQUENCE_NUMBER);
462 QuicPacketCreatorPeer::SetSequenceNumberLength(&creator_,
463 PACKET_2BYTE_SEQUENCE_NUMBER);
464 QuicStreamFrame* stream_frame =
465 new QuicStreamFrame(kCryptoStreamId, /*fin=*/false, 0u, StringPiece());
466 RetransmittableFrames frames(ENCRYPTION_NONE);
467 frames.AddFrame(QuicFrame(stream_frame));
468 char buffer[kMaxPacketSize];
469 SerializedPacket serialized = creator_.ReserializeAllFrames(
470 frames, PACKET_1BYTE_SEQUENCE_NUMBER, buffer, kMaxPacketSize);
471 EXPECT_EQ(PACKET_4BYTE_SEQUENCE_NUMBER,
472 QuicPacketCreatorPeer::NextSequenceNumberLength(&creator_));
473 EXPECT_EQ(PACKET_2BYTE_SEQUENCE_NUMBER,
474 QuicPacketCreatorPeer::GetSequenceNumberLength(&creator_));
475 EXPECT_EQ(PACKET_1BYTE_SEQUENCE_NUMBER, serialized.sequence_number_length);
478 InSequence s;
479 EXPECT_CALL(framer_visitor_, OnPacket());
480 EXPECT_CALL(framer_visitor_, OnUnauthenticatedPublicHeader(_));
481 EXPECT_CALL(framer_visitor_, OnUnauthenticatedHeader(_));
482 EXPECT_CALL(framer_visitor_, OnDecryptedPacket(_));
483 EXPECT_CALL(framer_visitor_, OnPacketHeader(_));
484 EXPECT_CALL(framer_visitor_, OnStreamFrame(_));
485 EXPECT_CALL(framer_visitor_, OnPacketComplete());
487 ProcessPacket(serialized.packet);
488 delete serialized.packet;
491 TEST_P(QuicPacketCreatorTest, ReserializeFramesWithPadding) {
492 QuicFrame frame;
493 QuicIOVector io_vector(MakeIOVector("fake handshake message data"));
494 scoped_ptr<char[]> stream_buffer;
495 creator_.CreateStreamFrame(kCryptoStreamId, io_vector, 0u, 0u, false, &frame,
496 &stream_buffer);
497 RetransmittableFrames frames(ENCRYPTION_NONE);
498 frames.AddFrame(frame);
499 frames.set_needs_padding(true);
500 char buffer[kMaxPacketSize];
501 SerializedPacket serialized = creator_.ReserializeAllFrames(
502 frames, QuicPacketCreatorPeer::NextSequenceNumberLength(&creator_),
503 buffer, kMaxPacketSize);
504 EXPECT_EQ(kDefaultMaxPacketSize, serialized.packet->length());
505 delete serialized.packet;
508 TEST_P(QuicPacketCreatorTest, ReserializeFramesWithFullPacketAndPadding) {
509 const size_t overhead = GetPacketHeaderOverhead(NOT_IN_FEC_GROUP)
510 + GetEncryptionOverhead() + GetStreamFrameOverhead(NOT_IN_FEC_GROUP);
511 size_t capacity = kDefaultMaxPacketSize - overhead;
512 for (int delta = -5; delta <= 0; ++delta) {
513 string data(capacity + delta, 'A');
514 size_t bytes_free = 0 - delta;
516 QuicFrame frame;
517 QuicIOVector io_vector(MakeIOVector(data));
518 scoped_ptr<char[]> stream_buffer;
519 creator_.CreateStreamFrame(kCryptoStreamId, io_vector, 0, kOffset, false,
520 &frame, &stream_buffer);
521 RetransmittableFrames frames(ENCRYPTION_NONE);
522 frames.AddFrame(frame);
523 frames.set_needs_padding(true);
524 char buffer[kMaxPacketSize];
525 SerializedPacket serialized = creator_.ReserializeAllFrames(
526 frames, QuicPacketCreatorPeer::NextSequenceNumberLength(&creator_),
527 buffer, kMaxPacketSize);
529 // If there is not enough space in the packet to fit a padding frame
530 // (1 byte) and to expand the stream frame (another 2 bytes) the packet
531 // will not be padded.
532 if (bytes_free < 3) {
533 EXPECT_EQ(kDefaultMaxPacketSize - bytes_free,
534 serialized.packet->length());
535 } else {
536 EXPECT_EQ(kDefaultMaxPacketSize, serialized.packet->length());
539 delete serialized.packet;
540 frames_.clear();
544 TEST_P(QuicPacketCreatorTest, SerializeConnectionClose) {
545 QuicConnectionCloseFrame frame;
546 frame.error_code = QUIC_NO_ERROR;
547 frame.error_details = "error";
549 QuicFrames frames;
550 frames.push_back(QuicFrame(&frame));
551 char buffer[kMaxPacketSize];
552 SerializedPacket serialized =
553 creator_.SerializeAllFrames(frames, buffer, kMaxPacketSize);
554 ASSERT_EQ(1u, serialized.sequence_number);
555 ASSERT_EQ(1u, creator_.sequence_number());
557 InSequence s;
558 EXPECT_CALL(framer_visitor_, OnPacket());
559 EXPECT_CALL(framer_visitor_, OnUnauthenticatedPublicHeader(_));
560 EXPECT_CALL(framer_visitor_, OnUnauthenticatedHeader(_));
561 EXPECT_CALL(framer_visitor_, OnDecryptedPacket(_));
562 EXPECT_CALL(framer_visitor_, OnPacketHeader(_));
563 EXPECT_CALL(framer_visitor_, OnConnectionCloseFrame(_));
564 EXPECT_CALL(framer_visitor_, OnPacketComplete());
566 ProcessPacket(serialized.packet);
567 delete serialized.packet;
570 TEST_P(QuicPacketCreatorTest, SwitchFecOnOffWithNoGroup) {
571 // Enable FEC protection.
572 creator_.set_max_packets_per_fec_group(6);
573 EXPECT_TRUE(creator_.IsFecEnabled());
574 EXPECT_FALSE(creator_.IsFecProtected());
576 // Turn on FEC protection.
577 creator_.StartFecProtectingPackets();
578 EXPECT_TRUE(creator_.IsFecProtected());
579 // We have no packets in the FEC group, so no FEC packet can be created.
580 EXPECT_FALSE(creator_.ShouldSendFec(/*force_close=*/true));
581 // Since no packets are in FEC group yet, we should be able to turn FEC
582 // off with no trouble.
583 creator_.StopFecProtectingPackets();
584 EXPECT_FALSE(creator_.IsFecProtected());
587 TEST_P(QuicPacketCreatorTest, SwitchFecOnOffWithGroupInProgress) {
588 // Enable FEC protection, and send FEC packet every 6 packets.
589 EXPECT_TRUE(SwitchFecProtectionOn(6));
590 frames_.push_back(
591 QuicFrame(new QuicStreamFrame(0u, false, 0u, StringPiece())));
592 char buffer[kMaxPacketSize];
593 SerializedPacket serialized =
594 creator_.SerializeAllFrames(frames_, buffer, kMaxPacketSize);
595 delete frames_[0].stream_frame;
596 delete serialized.packet;
598 EXPECT_TRUE(creator_.IsFecProtected());
599 // We do not have enough packets in the FEC group to trigger an FEC packet.
600 EXPECT_FALSE(creator_.ShouldSendFec(/*force_close=*/false));
601 // Should return true since there are packets in the FEC group.
602 EXPECT_TRUE(creator_.ShouldSendFec(/*force_close=*/true));
604 // Switching FEC off should not change creator state, since there is an
605 // FEC packet under construction.
606 EXPECT_DFATAL(creator_.StopFecProtectingPackets(),
607 "Cannot stop FEC protection with open FEC group.");
608 EXPECT_TRUE(creator_.IsFecProtected());
609 // Confirm that FEC packet is still under construction.
610 EXPECT_TRUE(creator_.ShouldSendFec(/*force_close=*/true));
612 serialized = creator_.SerializeFec(buffer, kMaxPacketSize);
613 delete serialized.packet;
615 // Switching FEC on/off should work now.
616 creator_.StopFecProtectingPackets();
617 EXPECT_FALSE(creator_.IsFecProtected());
618 creator_.StartFecProtectingPackets();
619 EXPECT_TRUE(creator_.IsFecProtected());
622 TEST_P(QuicPacketCreatorTest, SwitchFecOnWithStreamFrameQueued) {
623 // Add a stream frame to the creator.
624 QuicFrame frame;
625 QuicIOVector io_vector(MakeIOVector("test"));
626 scoped_ptr<char[]> stream_buffer;
627 size_t consumed = creator_.CreateStreamFrame(1u, io_vector, 0u, 0u, false,
628 &frame, &stream_buffer);
629 EXPECT_EQ(4u, consumed);
630 ASSERT_TRUE(frame.stream_frame);
631 EXPECT_TRUE(creator_.AddSavedFrame(frame));
632 EXPECT_TRUE(creator_.HasPendingFrames());
634 // Enable FEC protection, and send FEC packet every 6 packets.
635 creator_.set_max_packets_per_fec_group(6);
636 EXPECT_TRUE(creator_.IsFecEnabled());
637 EXPECT_DFATAL(creator_.StartFecProtectingPackets(),
638 "Cannot start FEC protection with pending frames.");
639 EXPECT_FALSE(creator_.IsFecProtected());
641 // Serialize packet for transmission.
642 char buffer[kMaxPacketSize];
643 SerializedPacket serialized =
644 creator_.SerializePacket(buffer, kMaxPacketSize);
645 delete serialized.packet;
646 delete serialized.retransmittable_frames;
647 EXPECT_FALSE(creator_.HasPendingFrames());
649 // Since all pending frames have been serialized, turning FEC on should work.
650 creator_.StartFecProtectingPackets();
651 EXPECT_TRUE(creator_.IsFecProtected());
654 TEST_P(QuicPacketCreatorTest, CreateStreamFrame) {
655 QuicFrame frame;
656 QuicIOVector io_vector(MakeIOVector("test"));
657 scoped_ptr<char[]> stream_buffer;
658 size_t consumed = creator_.CreateStreamFrame(1u, io_vector, 0u, 0u, false,
659 &frame, &stream_buffer);
660 EXPECT_EQ(4u, consumed);
661 CheckStreamFrame(frame, 1u, "test", 0u, false);
662 RetransmittableFrames cleanup_frames(ENCRYPTION_NONE);
663 cleanup_frames.AddFrame(frame);
666 TEST_P(QuicPacketCreatorTest, CreateStreamFrameFin) {
667 QuicFrame frame;
668 QuicIOVector io_vector(MakeIOVector("test"));
669 scoped_ptr<char[]> stream_buffer;
670 size_t consumed = creator_.CreateStreamFrame(1u, io_vector, 0u, 10u, true,
671 &frame, &stream_buffer);
672 EXPECT_EQ(4u, consumed);
673 CheckStreamFrame(frame, 1u, "test", 10u, true);
674 RetransmittableFrames cleanup_frames(ENCRYPTION_NONE);
675 cleanup_frames.AddFrame(frame);
678 TEST_P(QuicPacketCreatorTest, CreateStreamFrameFinOnly) {
679 QuicFrame frame;
680 QuicIOVector io_vector(nullptr, 0, 0);
681 scoped_ptr<char[]> stream_buffer;
682 size_t consumed = creator_.CreateStreamFrame(1u, io_vector, 0u, 0u, true,
683 &frame, &stream_buffer);
684 EXPECT_EQ(0u, consumed);
685 CheckStreamFrame(frame, 1u, string(), 0u, true);
686 delete frame.stream_frame;
689 TEST_P(QuicPacketCreatorTest, CreateAllFreeBytesForStreamFrames) {
690 const size_t overhead = GetPacketHeaderOverhead(NOT_IN_FEC_GROUP)
691 + GetEncryptionOverhead();
692 for (size_t i = overhead; i < overhead + 100; ++i) {
693 creator_.SetMaxPacketLength(i);
694 const bool should_have_room = i > overhead + GetStreamFrameOverhead(
695 NOT_IN_FEC_GROUP);
696 ASSERT_EQ(should_have_room, creator_.HasRoomForStreamFrame(
697 kClientDataStreamId1, kOffset));
698 if (should_have_room) {
699 QuicFrame frame;
700 QuicIOVector io_vector(MakeIOVector("testdata"));
701 scoped_ptr<char[]> stream_buffer;
702 size_t bytes_consumed =
703 creator_.CreateStreamFrame(kClientDataStreamId1, io_vector, 0u,
704 kOffset, false, &frame, &stream_buffer);
705 EXPECT_LT(0u, bytes_consumed);
706 ASSERT_TRUE(creator_.AddSavedFrame(frame));
707 char buffer[kMaxPacketSize];
708 SerializedPacket serialized_packet =
709 creator_.SerializePacket(buffer, kMaxPacketSize);
710 ASSERT_TRUE(serialized_packet.packet);
711 delete serialized_packet.packet;
712 delete serialized_packet.retransmittable_frames;
717 TEST_P(QuicPacketCreatorTest, StreamFrameConsumption) {
718 // Compute the total overhead for a single frame in packet.
719 const size_t overhead = GetPacketHeaderOverhead(NOT_IN_FEC_GROUP)
720 + GetEncryptionOverhead() + GetStreamFrameOverhead(NOT_IN_FEC_GROUP);
721 size_t capacity = kDefaultMaxPacketSize - overhead;
722 // Now, test various sizes around this size.
723 for (int delta = -5; delta <= 5; ++delta) {
724 string data(capacity + delta, 'A');
725 size_t bytes_free = delta > 0 ? 0 : 0 - delta;
726 QuicFrame frame;
727 QuicIOVector io_vector(MakeIOVector(data));
728 scoped_ptr<char[]> stream_buffer;
729 size_t bytes_consumed =
730 creator_.CreateStreamFrame(kClientDataStreamId1, io_vector, 0u, kOffset,
731 false, &frame, &stream_buffer);
732 EXPECT_EQ(capacity - bytes_free, bytes_consumed);
734 ASSERT_TRUE(creator_.AddSavedFrame(frame));
735 // BytesFree() returns bytes available for the next frame, which will
736 // be two bytes smaller since the stream frame would need to be grown.
737 EXPECT_EQ(2u, creator_.ExpansionOnNewFrame());
738 size_t expected_bytes_free = bytes_free < 3 ? 0 : bytes_free - 2;
739 EXPECT_EQ(expected_bytes_free, creator_.BytesFree()) << "delta: " << delta;
740 char buffer[kMaxPacketSize];
741 SerializedPacket serialized_packet =
742 creator_.SerializePacket(buffer, kMaxPacketSize);
743 ASSERT_TRUE(serialized_packet.packet);
744 delete serialized_packet.packet;
745 delete serialized_packet.retransmittable_frames;
749 TEST_P(QuicPacketCreatorTest, StreamFrameConsumptionWithFec) {
750 // Enable FEC protection, and send FEC packet every 6 packets.
751 EXPECT_TRUE(SwitchFecProtectionOn(6));
752 // Compute the total overhead for a single frame in packet.
753 const size_t overhead = GetPacketHeaderOverhead(IN_FEC_GROUP)
754 + GetEncryptionOverhead() + GetStreamFrameOverhead(IN_FEC_GROUP);
755 size_t capacity = kDefaultMaxPacketSize - overhead;
756 // Now, test various sizes around this size.
757 for (int delta = -5; delta <= 5; ++delta) {
758 string data(capacity + delta, 'A');
759 size_t bytes_free = delta > 0 ? 0 : 0 - delta;
760 QuicFrame frame;
761 QuicIOVector io_vector(MakeIOVector(data));
762 scoped_ptr<char[]> stream_buffer;
763 size_t bytes_consumed =
764 creator_.CreateStreamFrame(kClientDataStreamId1, io_vector, 0u, kOffset,
765 false, &frame, &stream_buffer);
766 EXPECT_EQ(capacity - bytes_free, bytes_consumed);
768 ASSERT_TRUE(creator_.AddSavedFrame(frame));
769 // BytesFree() returns bytes available for the next frame. Since stream
770 // frame does not grow for FEC protected packets, this should be the same
771 // as bytes_free (bound by 0).
772 EXPECT_EQ(0u, creator_.ExpansionOnNewFrame());
773 size_t expected_bytes_free = bytes_free > 0 ? bytes_free : 0;
774 EXPECT_EQ(expected_bytes_free, creator_.BytesFree()) << "delta: " << delta;
775 char buffer[kMaxPacketSize];
776 SerializedPacket serialized_packet =
777 creator_.SerializePacket(buffer, kMaxPacketSize);
778 ASSERT_TRUE(serialized_packet.packet);
779 delete serialized_packet.packet;
780 delete serialized_packet.retransmittable_frames;
784 TEST_P(QuicPacketCreatorTest, CryptoStreamFramePacketPadding) {
785 // Compute the total overhead for a single frame in packet.
786 const size_t overhead = GetPacketHeaderOverhead(NOT_IN_FEC_GROUP)
787 + GetEncryptionOverhead() + GetStreamFrameOverhead(NOT_IN_FEC_GROUP);
788 ASSERT_GT(kMaxPacketSize, overhead);
789 size_t capacity = kDefaultMaxPacketSize - overhead;
790 // Now, test various sizes around this size.
791 for (int delta = -5; delta <= 5; ++delta) {
792 string data(capacity + delta, 'A');
793 size_t bytes_free = delta > 0 ? 0 : 0 - delta;
795 QuicFrame frame;
796 QuicIOVector io_vector(MakeIOVector(data));
797 scoped_ptr<char[]> stream_buffer;
798 size_t bytes_consumed = creator_.CreateStreamFrame(
799 kCryptoStreamId, io_vector, 0u, kOffset, false, &frame, &stream_buffer);
800 EXPECT_LT(0u, bytes_consumed);
801 ASSERT_TRUE(creator_.AddPaddedSavedFrame(frame, nullptr));
802 char buffer[kMaxPacketSize];
803 SerializedPacket serialized_packet =
804 creator_.SerializePacket(buffer, kMaxPacketSize);
805 ASSERT_TRUE(serialized_packet.packet);
806 // If there is not enough space in the packet to fit a padding frame
807 // (1 byte) and to expand the stream frame (another 2 bytes) the packet
808 // will not be padded.
809 if (bytes_free < 3) {
810 EXPECT_EQ(kDefaultMaxPacketSize - bytes_free,
811 serialized_packet.packet->length());
812 } else {
813 EXPECT_EQ(kDefaultMaxPacketSize, serialized_packet.packet->length());
815 delete serialized_packet.packet;
816 delete serialized_packet.retransmittable_frames;
820 TEST_P(QuicPacketCreatorTest, NonCryptoStreamFramePacketNonPadding) {
821 // Compute the total overhead for a single frame in packet.
822 const size_t overhead = GetPacketHeaderOverhead(NOT_IN_FEC_GROUP)
823 + GetEncryptionOverhead() + GetStreamFrameOverhead(NOT_IN_FEC_GROUP);
824 ASSERT_GT(kDefaultMaxPacketSize, overhead);
825 size_t capacity = kDefaultMaxPacketSize - overhead;
826 // Now, test various sizes around this size.
827 for (int delta = -5; delta <= 5; ++delta) {
828 string data(capacity + delta, 'A');
829 size_t bytes_free = delta > 0 ? 0 : 0 - delta;
831 QuicFrame frame;
832 QuicIOVector io_vector(MakeIOVector(data));
833 scoped_ptr<char[]> stream_buffer;
834 size_t bytes_consumed =
835 creator_.CreateStreamFrame(kClientDataStreamId1, io_vector, 0u, kOffset,
836 false, &frame, &stream_buffer);
837 EXPECT_LT(0u, bytes_consumed);
838 ASSERT_TRUE(creator_.AddSavedFrame(frame));
839 char buffer[kMaxPacketSize];
840 SerializedPacket serialized_packet =
841 creator_.SerializePacket(buffer, kMaxPacketSize);
842 ASSERT_TRUE(serialized_packet.packet);
843 if (bytes_free > 0) {
844 EXPECT_EQ(kDefaultMaxPacketSize - bytes_free,
845 serialized_packet.packet->length());
846 } else {
847 EXPECT_EQ(kDefaultMaxPacketSize, serialized_packet.packet->length());
849 delete serialized_packet.packet;
850 delete serialized_packet.retransmittable_frames;
854 TEST_P(QuicPacketCreatorTest, SerializeVersionNegotiationPacket) {
855 QuicFramerPeer::SetPerspective(&client_framer_, Perspective::IS_SERVER);
856 QuicVersionVector versions;
857 versions.push_back(test::QuicVersionMax());
858 scoped_ptr<QuicEncryptedPacket> encrypted(
859 creator_.SerializeVersionNegotiationPacket(versions));
862 InSequence s;
863 EXPECT_CALL(framer_visitor_, OnPacket());
864 EXPECT_CALL(framer_visitor_, OnUnauthenticatedPublicHeader(_));
865 EXPECT_CALL(framer_visitor_, OnVersionNegotiationPacket(_));
867 QuicFramerPeer::SetPerspective(&client_framer_, Perspective::IS_CLIENT);
868 client_framer_.ProcessPacket(*encrypted);
871 TEST_P(QuicPacketCreatorTest, UpdatePacketSequenceNumberLengthLeastAwaiting) {
872 EXPECT_EQ(PACKET_1BYTE_SEQUENCE_NUMBER,
873 QuicPacketCreatorPeer::NextSequenceNumberLength(&creator_));
875 size_t max_packets_per_fec_group = 10;
876 creator_.set_max_packets_per_fec_group(max_packets_per_fec_group);
877 QuicPacketCreatorPeer::SetSequenceNumber(&creator_,
878 64 - max_packets_per_fec_group);
879 creator_.UpdateSequenceNumberLength(2, 10000 / kDefaultMaxPacketSize);
880 EXPECT_EQ(PACKET_1BYTE_SEQUENCE_NUMBER,
881 QuicPacketCreatorPeer::NextSequenceNumberLength(&creator_));
883 QuicPacketCreatorPeer::SetSequenceNumber(
884 &creator_, 64 * 256 - max_packets_per_fec_group);
885 creator_.UpdateSequenceNumberLength(2, 10000 / kDefaultMaxPacketSize);
886 EXPECT_EQ(PACKET_2BYTE_SEQUENCE_NUMBER,
887 QuicPacketCreatorPeer::NextSequenceNumberLength(&creator_));
889 QuicPacketCreatorPeer::SetSequenceNumber(
890 &creator_, 64 * 256 * 256 - max_packets_per_fec_group);
891 creator_.UpdateSequenceNumberLength(2, 10000 / kDefaultMaxPacketSize);
892 EXPECT_EQ(PACKET_4BYTE_SEQUENCE_NUMBER,
893 QuicPacketCreatorPeer::NextSequenceNumberLength(&creator_));
895 QuicPacketCreatorPeer::SetSequenceNumber(
896 &creator_,
897 UINT64_C(64) * 256 * 256 * 256 * 256 - max_packets_per_fec_group);
898 creator_.UpdateSequenceNumberLength(2, 10000 / kDefaultMaxPacketSize);
899 EXPECT_EQ(PACKET_6BYTE_SEQUENCE_NUMBER,
900 QuicPacketCreatorPeer::NextSequenceNumberLength(&creator_));
903 TEST_P(QuicPacketCreatorTest, UpdatePacketSequenceNumberLengthBandwidth) {
904 EXPECT_EQ(PACKET_1BYTE_SEQUENCE_NUMBER,
905 QuicPacketCreatorPeer::NextSequenceNumberLength(&creator_));
907 creator_.UpdateSequenceNumberLength(1, 10000 / kDefaultMaxPacketSize);
908 EXPECT_EQ(PACKET_1BYTE_SEQUENCE_NUMBER,
909 QuicPacketCreatorPeer::NextSequenceNumberLength(&creator_));
911 creator_.UpdateSequenceNumberLength(1, 10000 * 256 / kDefaultMaxPacketSize);
912 EXPECT_EQ(PACKET_2BYTE_SEQUENCE_NUMBER,
913 QuicPacketCreatorPeer::NextSequenceNumberLength(&creator_));
915 creator_.UpdateSequenceNumberLength(
916 1, 10000 * 256 * 256 / kDefaultMaxPacketSize);
917 EXPECT_EQ(PACKET_4BYTE_SEQUENCE_NUMBER,
918 QuicPacketCreatorPeer::NextSequenceNumberLength(&creator_));
920 creator_.UpdateSequenceNumberLength(
921 1, UINT64_C(1000) * 256 * 256 * 256 * 256 / kDefaultMaxPacketSize);
922 EXPECT_EQ(PACKET_6BYTE_SEQUENCE_NUMBER,
923 QuicPacketCreatorPeer::NextSequenceNumberLength(&creator_));
926 TEST_P(QuicPacketCreatorTest, SerializeFrame) {
927 if (!GetParam().version_serialization) {
928 creator_.StopSendingVersion();
930 frames_.push_back(
931 QuicFrame(new QuicStreamFrame(0u, false, 0u, StringPiece())));
932 char buffer[kMaxPacketSize];
933 SerializedPacket serialized =
934 creator_.SerializeAllFrames(frames_, buffer, kMaxPacketSize);
935 delete frames_[0].stream_frame;
937 QuicPacketHeader header;
939 InSequence s;
940 EXPECT_CALL(framer_visitor_, OnPacket());
941 EXPECT_CALL(framer_visitor_, OnUnauthenticatedPublicHeader(_));
942 EXPECT_CALL(framer_visitor_, OnUnauthenticatedHeader(_));
943 EXPECT_CALL(framer_visitor_, OnDecryptedPacket(_));
944 EXPECT_CALL(framer_visitor_, OnPacketHeader(_)).WillOnce(
945 DoAll(SaveArg<0>(&header), Return(true)));
946 EXPECT_CALL(framer_visitor_, OnStreamFrame(_));
947 EXPECT_CALL(framer_visitor_, OnPacketComplete());
949 ProcessPacket(serialized.packet);
950 EXPECT_EQ(GetParam().version_serialization,
951 header.public_header.version_flag);
952 delete serialized.packet;
955 TEST_P(QuicPacketCreatorTest, CreateStreamFrameTooLarge) {
956 if (!GetParam().version_serialization) {
957 creator_.StopSendingVersion();
959 // A string larger than fits into a frame.
960 size_t payload_length;
961 creator_.SetMaxPacketLength(GetPacketLengthForOneStream(
962 client_framer_.version(),
963 QuicPacketCreatorPeer::SendVersionInPacket(&creator_),
964 creator_.connection_id_length(),
965 PACKET_1BYTE_SEQUENCE_NUMBER, NOT_IN_FEC_GROUP, &payload_length));
966 QuicFrame frame;
967 const string too_long_payload(payload_length * 2, 'a');
968 QuicIOVector io_vector(MakeIOVector(too_long_payload));
969 scoped_ptr<char[]> stream_buffer;
970 size_t consumed = creator_.CreateStreamFrame(1u, io_vector, 0u, 0u, true,
971 &frame, &stream_buffer);
972 EXPECT_EQ(payload_length, consumed);
973 const string payload(payload_length, 'a');
974 CheckStreamFrame(frame, 1u, payload, 0u, false);
975 RetransmittableFrames cleanup_frames(ENCRYPTION_NONE);
976 cleanup_frames.AddFrame(frame);
979 TEST_P(QuicPacketCreatorTest, AddFrameAndSerialize) {
980 if (!GetParam().version_serialization) {
981 creator_.StopSendingVersion();
983 const size_t max_plaintext_size =
984 client_framer_.GetMaxPlaintextSize(creator_.max_packet_length());
985 EXPECT_FALSE(creator_.HasPendingFrames());
986 EXPECT_EQ(max_plaintext_size -
987 GetPacketHeaderSize(
988 creator_.connection_id_length(),
989 QuicPacketCreatorPeer::SendVersionInPacket(&creator_),
990 PACKET_1BYTE_SEQUENCE_NUMBER, NOT_IN_FEC_GROUP),
991 creator_.BytesFree());
993 // Add a variety of frame types and then a padding frame.
994 QuicAckFrame ack_frame(MakeAckFrame(0u));
995 EXPECT_TRUE(creator_.AddSavedFrame(QuicFrame(&ack_frame)));
996 EXPECT_TRUE(creator_.HasPendingFrames());
998 QuicFrame frame;
999 QuicIOVector io_vector(MakeIOVector("test"));
1000 scoped_ptr<char[]> stream_buffer;
1001 size_t consumed = creator_.CreateStreamFrame(1u, io_vector, 0u, 0u, false,
1002 &frame, &stream_buffer);
1003 EXPECT_EQ(4u, consumed);
1004 ASSERT_TRUE(frame.stream_frame);
1005 EXPECT_TRUE(creator_.AddSavedFrame(frame));
1006 EXPECT_TRUE(creator_.HasPendingFrames());
1008 QuicPaddingFrame padding_frame;
1009 EXPECT_TRUE(creator_.AddSavedFrame(QuicFrame(&padding_frame)));
1010 EXPECT_TRUE(creator_.HasPendingFrames());
1011 EXPECT_EQ(0u, creator_.BytesFree());
1013 EXPECT_FALSE(creator_.AddSavedFrame(QuicFrame(&ack_frame)));
1015 // Ensure the packet is successfully created.
1016 char buffer[kMaxPacketSize];
1017 SerializedPacket serialized =
1018 creator_.SerializePacket(buffer, kMaxPacketSize);
1019 ASSERT_TRUE(serialized.packet);
1020 delete serialized.packet;
1021 ASSERT_TRUE(serialized.retransmittable_frames);
1022 RetransmittableFrames* retransmittable = serialized.retransmittable_frames;
1023 ASSERT_EQ(1u, retransmittable->frames().size());
1024 EXPECT_EQ(STREAM_FRAME, retransmittable->frames()[0].type);
1025 ASSERT_TRUE(retransmittable->frames()[0].stream_frame);
1026 delete serialized.retransmittable_frames;
1028 EXPECT_FALSE(creator_.HasPendingFrames());
1029 EXPECT_EQ(max_plaintext_size -
1030 GetPacketHeaderSize(
1031 creator_.connection_id_length(),
1032 QuicPacketCreatorPeer::SendVersionInPacket(&creator_),
1033 PACKET_1BYTE_SEQUENCE_NUMBER,
1034 NOT_IN_FEC_GROUP),
1035 creator_.BytesFree());
1038 TEST_P(QuicPacketCreatorTest, SerializeTruncatedAckFrameWithLargePacketSize) {
1039 if (!GetParam().version_serialization) {
1040 creator_.StopSendingVersion();
1042 creator_.SetMaxPacketLength(kMaxPacketSize);
1044 // Serialized length of ack frame with 2000 nack ranges should be limited by
1045 // the number of nack ranges that can be fit in an ack frame.
1046 QuicAckFrame ack_frame = MakeAckFrameWithNackRanges(2000u, 0u);
1047 size_t frame_len = client_framer_.GetSerializedFrameLength(
1048 QuicFrame(&ack_frame), creator_.BytesFree(), true, true,
1049 NOT_IN_FEC_GROUP, PACKET_1BYTE_SEQUENCE_NUMBER);
1050 EXPECT_GT(creator_.BytesFree(), frame_len);
1051 EXPECT_GT(creator_.max_packet_length(), creator_.PacketSize());
1053 // Add ack frame to creator.
1054 EXPECT_TRUE(creator_.AddSavedFrame(QuicFrame(&ack_frame)));
1055 EXPECT_TRUE(creator_.HasPendingFrames());
1056 EXPECT_GT(creator_.max_packet_length(), creator_.PacketSize());
1057 EXPECT_LT(0u, creator_.BytesFree());
1059 // Make sure that an additional stream frame can be added to the packet.
1060 QuicFrame stream_frame;
1061 QuicIOVector io_vector(MakeIOVector("test"));
1062 scoped_ptr<char[]> stream_buffer;
1063 size_t consumed = creator_.CreateStreamFrame(2u, io_vector, 0u, 0u, false,
1064 &stream_frame, &stream_buffer);
1065 EXPECT_EQ(4u, consumed);
1066 ASSERT_TRUE(stream_frame.stream_frame);
1067 EXPECT_TRUE(creator_.AddSavedFrame(stream_frame));
1068 EXPECT_TRUE(creator_.HasPendingFrames());
1070 // Ensure the packet is successfully created, and the packet size estimate
1071 // matches the serialized packet length.
1072 EXPECT_CALL(entropy_calculator_,
1073 EntropyHash(_)).WillOnce(testing::Return(0));
1074 size_t est_packet_size = creator_.PacketSize();
1075 char buffer[kMaxPacketSize];
1076 SerializedPacket serialized =
1077 creator_.SerializePacket(buffer, kMaxPacketSize);
1078 ASSERT_TRUE(serialized.packet);
1079 EXPECT_EQ(est_packet_size,
1080 client_framer_.GetMaxPlaintextSize(serialized.packet->length()));
1081 delete serialized.retransmittable_frames;
1082 delete serialized.packet;
1085 TEST_P(QuicPacketCreatorTest, SerializeTruncatedAckFrameWithSmallPacketSize) {
1086 if (!GetParam().version_serialization) {
1087 creator_.StopSendingVersion();
1089 creator_.SetMaxPacketLength(500u);
1091 const size_t max_plaintext_size =
1092 client_framer_.GetMaxPlaintextSize(creator_.max_packet_length());
1093 EXPECT_EQ(max_plaintext_size - creator_.PacketSize(), creator_.BytesFree());
1095 // Serialized length of ack frame with 2000 nack ranges should be limited by
1096 // the packet size.
1097 QuicAckFrame ack_frame = MakeAckFrameWithNackRanges(2000u, 0u);
1098 size_t frame_len = client_framer_.GetSerializedFrameLength(
1099 QuicFrame(&ack_frame), creator_.BytesFree(), true, true,
1100 NOT_IN_FEC_GROUP, PACKET_1BYTE_SEQUENCE_NUMBER);
1101 EXPECT_EQ(creator_.BytesFree(), frame_len);
1103 // Add ack frame to creator.
1104 EXPECT_TRUE(creator_.AddSavedFrame(QuicFrame(&ack_frame)));
1105 EXPECT_TRUE(creator_.HasPendingFrames());
1106 EXPECT_EQ(client_framer_.GetMaxPlaintextSize(creator_.max_packet_length()),
1107 creator_.PacketSize());
1108 EXPECT_EQ(0u, creator_.BytesFree());
1110 // Ensure the packet is successfully created, and the packet size estimate
1111 // may not match the serialized packet length.
1112 EXPECT_CALL(entropy_calculator_,
1113 EntropyHash(_)).WillOnce(Return(0));
1114 size_t est_packet_size = creator_.PacketSize();
1115 char buffer[kMaxPacketSize];
1116 SerializedPacket serialized =
1117 creator_.SerializePacket(buffer, kMaxPacketSize);
1118 ASSERT_TRUE(serialized.packet);
1119 EXPECT_GE(est_packet_size,
1120 client_framer_.GetMaxPlaintextSize(serialized.packet->length()));
1121 delete serialized.packet;
1125 TEST_P(QuicPacketCreatorTest, EntropyFlag) {
1126 frames_.push_back(
1127 QuicFrame(new QuicStreamFrame(0u, false, 0u, StringPiece())));
1129 char buffer[kMaxPacketSize];
1130 for (int i = 0; i < 2; ++i) {
1131 for (int j = 0; j < 64; ++j) {
1132 SerializedPacket serialized =
1133 creator_.SerializeAllFrames(frames_, buffer, kMaxPacketSize);
1134 // Verify both BoolSource and hash algorithm.
1135 bool expected_rand_bool =
1136 (mock_random_.RandUint64() & (UINT64_C(1) << j)) != 0;
1137 bool observed_rand_bool =
1138 (serialized.entropy_hash & (1 << ((j+1) % 8))) != 0;
1139 uint8 rest_of_hash = serialized.entropy_hash & ~(1 << ((j+1) % 8));
1140 EXPECT_EQ(expected_rand_bool, observed_rand_bool);
1141 EXPECT_EQ(0, rest_of_hash);
1142 delete serialized.packet;
1144 // After 64 calls, BoolSource will refresh the bucket - make sure it does.
1145 mock_random_.ChangeValue();
1148 delete frames_[0].stream_frame;
1151 TEST_P(QuicPacketCreatorTest, ResetFecGroup) {
1152 // Enable FEC protection, and send FEC packet every 6 packets.
1153 EXPECT_TRUE(SwitchFecProtectionOn(6));
1154 frames_.push_back(
1155 QuicFrame(new QuicStreamFrame(0u, false, 0u, StringPiece())));
1156 char buffer[kMaxPacketSize];
1157 SerializedPacket serialized =
1158 creator_.SerializeAllFrames(frames_, buffer, kMaxPacketSize);
1159 delete serialized.packet;
1161 EXPECT_TRUE(creator_.IsFecProtected());
1162 EXPECT_TRUE(creator_.IsFecGroupOpen());
1163 // We do not have enough packets in the FEC group to trigger an FEC packet.
1164 EXPECT_FALSE(creator_.ShouldSendFec(/*force_close=*/false));
1165 // Should return true since there are packets in the FEC group.
1166 EXPECT_TRUE(creator_.ShouldSendFec(/*force_close=*/true));
1168 // Close the FEC Group.
1169 creator_.ResetFecGroup();
1170 EXPECT_TRUE(creator_.IsFecProtected());
1171 EXPECT_FALSE(creator_.IsFecGroupOpen());
1172 // We do not have enough packets in the FEC group to trigger an FEC packet.
1173 EXPECT_FALSE(creator_.ShouldSendFec(/*force_close=*/false));
1174 // Confirm that there is no FEC packet under construction.
1175 EXPECT_FALSE(creator_.ShouldSendFec(/*force_close=*/true));
1177 EXPECT_DFATAL(serialized = creator_.SerializeFec(buffer, kMaxPacketSize),
1178 "SerializeFEC called but no group or zero packets in group.");
1179 delete serialized.packet;
1181 // Start a new FEC packet.
1182 serialized = creator_.SerializeAllFrames(frames_, buffer, kMaxPacketSize);
1183 delete frames_[0].stream_frame;
1184 delete serialized.packet;
1186 EXPECT_TRUE(creator_.IsFecProtected());
1187 EXPECT_TRUE(creator_.IsFecGroupOpen());
1188 // We do not have enough packets in the FEC group to trigger an FEC packet.
1189 EXPECT_FALSE(creator_.ShouldSendFec(/*force_close=*/false));
1190 // Should return true since there are packets in the FEC group.
1191 EXPECT_TRUE(creator_.ShouldSendFec(/*force_close=*/true));
1193 // Should return false since we do not have enough packets in the FEC group to
1194 // trigger an FEC packet.
1195 ASSERT_FALSE(creator_.ShouldSendFec(/*force_close=*/false));
1196 // Should return true since there are packets in the FEC group.
1197 ASSERT_TRUE(creator_.ShouldSendFec(/*force_close=*/true));
1199 serialized = creator_.SerializeFec(buffer, kMaxPacketSize);
1200 ASSERT_EQ(3u, serialized.sequence_number);
1201 delete serialized.packet;
1204 TEST_P(QuicPacketCreatorTest, ResetFecGroupWithQueuedFrames) {
1205 // Add a stream frame to the creator.
1206 QuicFrame frame;
1207 QuicIOVector io_vector(MakeIOVector("test"));
1208 scoped_ptr<char[]> stream_buffer;
1209 size_t consumed = creator_.CreateStreamFrame(1u, io_vector, 0u, 0u, false,
1210 &frame, &stream_buffer);
1211 EXPECT_EQ(4u, consumed);
1212 ASSERT_TRUE(frame.stream_frame);
1213 EXPECT_TRUE(creator_.AddSavedFrame(frame));
1214 EXPECT_TRUE(creator_.HasPendingFrames());
1215 EXPECT_DFATAL(creator_.ResetFecGroup(),
1216 "Cannot reset FEC group with pending frames.");
1218 // Serialize packet for transmission.
1219 char buffer[kMaxPacketSize];
1220 SerializedPacket serialized =
1221 creator_.SerializePacket(buffer, kMaxPacketSize);
1222 delete serialized.packet;
1223 delete serialized.retransmittable_frames;
1224 EXPECT_FALSE(creator_.HasPendingFrames());
1226 // Close the FEC Group.
1227 creator_.ResetFecGroup();
1228 EXPECT_FALSE(creator_.IsFecGroupOpen());
1231 } // namespace
1232 } // namespace test
1233 } // namespace net