Switch global error menu icon to vectorized MD asset
[chromium-blink-merge.git] / net / quic / quic_packet_creator_test.cc
blob962c1b5b4d879bff7d1b8927d5bedd49e81b3ae6
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::NextPacketNumberLength(&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.packet_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::SetNextPacketNumberLength(&creator_,
250 PACKET_4BYTE_PACKET_NUMBER);
251 char buffer[kMaxPacketSize];
252 SerializedPacket serialized =
253 creator_.SerializePacket(buffer, kMaxPacketSize);
254 // The packet number length will not change mid-packet.
255 EXPECT_EQ(PACKET_1BYTE_PACKET_NUMBER, serialized.packet_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 packet number length should have changed.
273 EXPECT_EQ(PACKET_4BYTE_PACKET_NUMBER, serialized.packet_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 packet 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 packet number length.
302 QuicPacketCreatorPeer::SetNextPacketNumberLength(&creator_,
303 PACKET_4BYTE_PACKET_NUMBER);
305 // Add a STOP_WAITING frame since it contains a packet 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_PACKET_NUMBER, serialized.packet_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_PACKET_NUMBER,
334 header.public_header.packet_number_length);
335 delete serialized.packet;
337 // Packet 2.
338 EXPECT_FALSE(creator_.HasPendingFrames());
339 // Generate Packet 2 with one frame -- packet 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_PACKET_NUMBER, serialized.packet_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_PACKET_NUMBER,
363 header.public_header.packet_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 packet 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 packet number length mid-FEC group and it should not change.
385 QuicPacketCreatorPeer::SetNextPacketNumberLength(&creator_,
386 PACKET_4BYTE_PACKET_NUMBER);
387 char buffer[kMaxPacketSize];
388 SerializedPacket serialized =
389 creator_.SerializePacket(buffer, kMaxPacketSize);
390 EXPECT_EQ(PACKET_1BYTE_PACKET_NUMBER, serialized.packet_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_PACKET_NUMBER, serialized.packet_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_PACKET_NUMBER, serialized.packet_number_length);
434 ASSERT_EQ(3u, serialized.packet_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 packet number length.
450 serialized = creator_.SerializeAllFrames(frames_, buffer, kMaxPacketSize);
451 EXPECT_EQ(PACKET_4BYTE_PACKET_NUMBER, serialized.packet_number_length);
452 delete frames_[0].ack_frame;
453 delete serialized.packet;
456 TEST_P(QuicPacketCreatorTest, ReserializeFramesWithSequenceNumberLength) {
457 // If the original packet number length, the current packet number
458 // length, and the configured send packet number length are different, the
459 // retransmit must sent with the original length and the others do not change.
460 QuicPacketCreatorPeer::SetNextPacketNumberLength(&creator_,
461 PACKET_4BYTE_PACKET_NUMBER);
462 QuicPacketCreatorPeer::SetPacketNumberLength(&creator_,
463 PACKET_2BYTE_PACKET_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_PACKET_NUMBER, buffer, kMaxPacketSize);
471 EXPECT_EQ(PACKET_4BYTE_PACKET_NUMBER,
472 QuicPacketCreatorPeer::NextPacketNumberLength(&creator_));
473 EXPECT_EQ(PACKET_2BYTE_PACKET_NUMBER,
474 QuicPacketCreatorPeer::GetPacketNumberLength(&creator_));
475 EXPECT_EQ(PACKET_1BYTE_PACKET_NUMBER, serialized.packet_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::NextPacketNumberLength(&creator_), buffer,
503 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::NextPacketNumberLength(&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.packet_number);
555 ASSERT_EQ(1u, creator_.packet_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_PACKET_NUMBER,
873 QuicPacketCreatorPeer::NextPacketNumberLength(&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::SetPacketNumber(&creator_,
878 64 - max_packets_per_fec_group);
879 creator_.UpdatePacketNumberLength(2, 10000 / kDefaultMaxPacketSize);
880 EXPECT_EQ(PACKET_1BYTE_PACKET_NUMBER,
881 QuicPacketCreatorPeer::NextPacketNumberLength(&creator_));
883 QuicPacketCreatorPeer::SetPacketNumber(&creator_,
884 64 * 256 - max_packets_per_fec_group);
885 creator_.UpdatePacketNumberLength(2, 10000 / kDefaultMaxPacketSize);
886 EXPECT_EQ(PACKET_2BYTE_PACKET_NUMBER,
887 QuicPacketCreatorPeer::NextPacketNumberLength(&creator_));
889 QuicPacketCreatorPeer::SetPacketNumber(
890 &creator_, 64 * 256 * 256 - max_packets_per_fec_group);
891 creator_.UpdatePacketNumberLength(2, 10000 / kDefaultMaxPacketSize);
892 EXPECT_EQ(PACKET_4BYTE_PACKET_NUMBER,
893 QuicPacketCreatorPeer::NextPacketNumberLength(&creator_));
895 QuicPacketCreatorPeer::SetPacketNumber(
896 &creator_,
897 UINT64_C(64) * 256 * 256 * 256 * 256 - max_packets_per_fec_group);
898 creator_.UpdatePacketNumberLength(2, 10000 / kDefaultMaxPacketSize);
899 EXPECT_EQ(PACKET_6BYTE_PACKET_NUMBER,
900 QuicPacketCreatorPeer::NextPacketNumberLength(&creator_));
903 TEST_P(QuicPacketCreatorTest, UpdatePacketSequenceNumberLengthBandwidth) {
904 EXPECT_EQ(PACKET_1BYTE_PACKET_NUMBER,
905 QuicPacketCreatorPeer::NextPacketNumberLength(&creator_));
907 creator_.UpdatePacketNumberLength(1, 10000 / kDefaultMaxPacketSize);
908 EXPECT_EQ(PACKET_1BYTE_PACKET_NUMBER,
909 QuicPacketCreatorPeer::NextPacketNumberLength(&creator_));
911 creator_.UpdatePacketNumberLength(1, 10000 * 256 / kDefaultMaxPacketSize);
912 EXPECT_EQ(PACKET_2BYTE_PACKET_NUMBER,
913 QuicPacketCreatorPeer::NextPacketNumberLength(&creator_));
915 creator_.UpdatePacketNumberLength(1,
916 10000 * 256 * 256 / kDefaultMaxPacketSize);
917 EXPECT_EQ(PACKET_4BYTE_PACKET_NUMBER,
918 QuicPacketCreatorPeer::NextPacketNumberLength(&creator_));
920 creator_.UpdatePacketNumberLength(
921 1, UINT64_C(1000) * 256 * 256 * 256 * 256 / kDefaultMaxPacketSize);
922 EXPECT_EQ(PACKET_6BYTE_PACKET_NUMBER,
923 QuicPacketCreatorPeer::NextPacketNumberLength(&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(), PACKET_1BYTE_PACKET_NUMBER,
965 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_PACKET_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_PACKET_NUMBER, NOT_IN_FEC_GROUP),
1034 creator_.BytesFree());
1037 TEST_P(QuicPacketCreatorTest, SerializeTruncatedAckFrameWithLargePacketSize) {
1038 if (!GetParam().version_serialization) {
1039 creator_.StopSendingVersion();
1041 creator_.SetMaxPacketLength(kMaxPacketSize);
1043 // Serialized length of ack frame with 2000 nack ranges should be limited by
1044 // the number of nack ranges that can be fit in an ack frame.
1045 QuicAckFrame ack_frame = MakeAckFrameWithNackRanges(2000u, 0u);
1046 size_t frame_len = client_framer_.GetSerializedFrameLength(
1047 QuicFrame(&ack_frame), creator_.BytesFree(), true, true, NOT_IN_FEC_GROUP,
1048 PACKET_1BYTE_PACKET_NUMBER);
1049 EXPECT_GT(creator_.BytesFree(), frame_len);
1050 EXPECT_GT(creator_.max_packet_length(), creator_.PacketSize());
1052 // Add ack frame to creator.
1053 EXPECT_TRUE(creator_.AddSavedFrame(QuicFrame(&ack_frame)));
1054 EXPECT_TRUE(creator_.HasPendingFrames());
1055 EXPECT_GT(creator_.max_packet_length(), creator_.PacketSize());
1056 EXPECT_LT(0u, creator_.BytesFree());
1058 // Make sure that an additional stream frame can be added to the packet.
1059 QuicFrame stream_frame;
1060 QuicIOVector io_vector(MakeIOVector("test"));
1061 scoped_ptr<char[]> stream_buffer;
1062 size_t consumed = creator_.CreateStreamFrame(2u, io_vector, 0u, 0u, false,
1063 &stream_frame, &stream_buffer);
1064 EXPECT_EQ(4u, consumed);
1065 ASSERT_TRUE(stream_frame.stream_frame);
1066 EXPECT_TRUE(creator_.AddSavedFrame(stream_frame));
1067 EXPECT_TRUE(creator_.HasPendingFrames());
1069 // Ensure the packet is successfully created, and the packet size estimate
1070 // matches the serialized packet length.
1071 EXPECT_CALL(entropy_calculator_,
1072 EntropyHash(_)).WillOnce(testing::Return(0));
1073 size_t est_packet_size = creator_.PacketSize();
1074 char buffer[kMaxPacketSize];
1075 SerializedPacket serialized =
1076 creator_.SerializePacket(buffer, kMaxPacketSize);
1077 ASSERT_TRUE(serialized.packet);
1078 EXPECT_EQ(est_packet_size,
1079 client_framer_.GetMaxPlaintextSize(serialized.packet->length()));
1080 delete serialized.retransmittable_frames;
1081 delete serialized.packet;
1084 TEST_P(QuicPacketCreatorTest, SerializeTruncatedAckFrameWithSmallPacketSize) {
1085 if (!GetParam().version_serialization) {
1086 creator_.StopSendingVersion();
1088 creator_.SetMaxPacketLength(500u);
1090 const size_t max_plaintext_size =
1091 client_framer_.GetMaxPlaintextSize(creator_.max_packet_length());
1092 EXPECT_EQ(max_plaintext_size - creator_.PacketSize(), creator_.BytesFree());
1094 // Serialized length of ack frame with 2000 nack ranges should be limited by
1095 // the packet size.
1096 QuicAckFrame ack_frame = MakeAckFrameWithNackRanges(2000u, 0u);
1097 size_t frame_len = client_framer_.GetSerializedFrameLength(
1098 QuicFrame(&ack_frame), creator_.BytesFree(), true, true, NOT_IN_FEC_GROUP,
1099 PACKET_1BYTE_PACKET_NUMBER);
1100 EXPECT_EQ(creator_.BytesFree(), frame_len);
1102 // Add ack frame to creator.
1103 EXPECT_TRUE(creator_.AddSavedFrame(QuicFrame(&ack_frame)));
1104 EXPECT_TRUE(creator_.HasPendingFrames());
1105 EXPECT_EQ(client_framer_.GetMaxPlaintextSize(creator_.max_packet_length()),
1106 creator_.PacketSize());
1107 EXPECT_EQ(0u, creator_.BytesFree());
1109 // Ensure the packet is successfully created, and the packet size estimate
1110 // may not match the serialized packet length.
1111 EXPECT_CALL(entropy_calculator_,
1112 EntropyHash(_)).WillOnce(Return(0));
1113 size_t est_packet_size = creator_.PacketSize();
1114 char buffer[kMaxPacketSize];
1115 SerializedPacket serialized =
1116 creator_.SerializePacket(buffer, kMaxPacketSize);
1117 ASSERT_TRUE(serialized.packet);
1118 EXPECT_GE(est_packet_size,
1119 client_framer_.GetMaxPlaintextSize(serialized.packet->length()));
1120 delete serialized.packet;
1124 TEST_P(QuicPacketCreatorTest, EntropyFlag) {
1125 frames_.push_back(
1126 QuicFrame(new QuicStreamFrame(0u, false, 0u, StringPiece())));
1128 char buffer[kMaxPacketSize];
1129 for (int i = 0; i < 2; ++i) {
1130 for (int j = 0; j < 64; ++j) {
1131 SerializedPacket serialized =
1132 creator_.SerializeAllFrames(frames_, buffer, kMaxPacketSize);
1133 // Verify both BoolSource and hash algorithm.
1134 bool expected_rand_bool =
1135 (mock_random_.RandUint64() & (UINT64_C(1) << j)) != 0;
1136 bool observed_rand_bool =
1137 (serialized.entropy_hash & (1 << ((j+1) % 8))) != 0;
1138 uint8 rest_of_hash = serialized.entropy_hash & ~(1 << ((j+1) % 8));
1139 EXPECT_EQ(expected_rand_bool, observed_rand_bool);
1140 EXPECT_EQ(0, rest_of_hash);
1141 delete serialized.packet;
1143 // After 64 calls, BoolSource will refresh the bucket - make sure it does.
1144 mock_random_.ChangeValue();
1147 delete frames_[0].stream_frame;
1150 TEST_P(QuicPacketCreatorTest, ResetFecGroup) {
1151 // Enable FEC protection, and send FEC packet every 6 packets.
1152 EXPECT_TRUE(SwitchFecProtectionOn(6));
1153 frames_.push_back(
1154 QuicFrame(new QuicStreamFrame(0u, false, 0u, StringPiece())));
1155 char buffer[kMaxPacketSize];
1156 SerializedPacket serialized =
1157 creator_.SerializeAllFrames(frames_, buffer, kMaxPacketSize);
1158 delete serialized.packet;
1160 EXPECT_TRUE(creator_.IsFecProtected());
1161 EXPECT_TRUE(creator_.IsFecGroupOpen());
1162 // We do not have enough packets in the FEC group to trigger an FEC packet.
1163 EXPECT_FALSE(creator_.ShouldSendFec(/*force_close=*/false));
1164 // Should return true since there are packets in the FEC group.
1165 EXPECT_TRUE(creator_.ShouldSendFec(/*force_close=*/true));
1167 // Close the FEC Group.
1168 creator_.ResetFecGroup();
1169 EXPECT_TRUE(creator_.IsFecProtected());
1170 EXPECT_FALSE(creator_.IsFecGroupOpen());
1171 // We do not have enough packets in the FEC group to trigger an FEC packet.
1172 EXPECT_FALSE(creator_.ShouldSendFec(/*force_close=*/false));
1173 // Confirm that there is no FEC packet under construction.
1174 EXPECT_FALSE(creator_.ShouldSendFec(/*force_close=*/true));
1176 EXPECT_DFATAL(serialized = creator_.SerializeFec(buffer, kMaxPacketSize),
1177 "SerializeFEC called but no group or zero packets in group.");
1178 delete serialized.packet;
1180 // Start a new FEC packet.
1181 serialized = creator_.SerializeAllFrames(frames_, buffer, kMaxPacketSize);
1182 delete frames_[0].stream_frame;
1183 delete serialized.packet;
1185 EXPECT_TRUE(creator_.IsFecProtected());
1186 EXPECT_TRUE(creator_.IsFecGroupOpen());
1187 // We do not have enough packets in the FEC group to trigger an FEC packet.
1188 EXPECT_FALSE(creator_.ShouldSendFec(/*force_close=*/false));
1189 // Should return true since there are packets in the FEC group.
1190 EXPECT_TRUE(creator_.ShouldSendFec(/*force_close=*/true));
1192 // Should return false since we do not have enough packets in the FEC group to
1193 // trigger an FEC packet.
1194 ASSERT_FALSE(creator_.ShouldSendFec(/*force_close=*/false));
1195 // Should return true since there are packets in the FEC group.
1196 ASSERT_TRUE(creator_.ShouldSendFec(/*force_close=*/true));
1198 serialized = creator_.SerializeFec(buffer, kMaxPacketSize);
1199 ASSERT_EQ(3u, serialized.packet_number);
1200 delete serialized.packet;
1203 TEST_P(QuicPacketCreatorTest, ResetFecGroupWithQueuedFrames) {
1204 // Add a stream frame to the creator.
1205 QuicFrame frame;
1206 QuicIOVector io_vector(MakeIOVector("test"));
1207 scoped_ptr<char[]> stream_buffer;
1208 size_t consumed = creator_.CreateStreamFrame(1u, io_vector, 0u, 0u, false,
1209 &frame, &stream_buffer);
1210 EXPECT_EQ(4u, consumed);
1211 ASSERT_TRUE(frame.stream_frame);
1212 EXPECT_TRUE(creator_.AddSavedFrame(frame));
1213 EXPECT_TRUE(creator_.HasPendingFrames());
1214 EXPECT_DFATAL(creator_.ResetFecGroup(),
1215 "Cannot reset FEC group with pending frames.");
1217 // Serialize packet for transmission.
1218 char buffer[kMaxPacketSize];
1219 SerializedPacket serialized =
1220 creator_.SerializePacket(buffer, kMaxPacketSize);
1221 delete serialized.packet;
1222 delete serialized.retransmittable_frames;
1223 EXPECT_FALSE(creator_.HasPendingFrames());
1225 // Close the FEC Group.
1226 creator_.ResetFecGroup();
1227 EXPECT_FALSE(creator_.IsFecGroupOpen());
1230 } // namespace
1231 } // namespace test
1232 } // namespace net