Prevent app list doodle from being pinch-to-zoomed.
[chromium-blink-merge.git] / net / quic / quic_packet_creator_test.cc
blob8aecc8b24a3b3931146c8af09b3292d2e2846f2a
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 "base/stl_util.h"
8 #include "net/quic/crypto/null_encrypter.h"
9 #include "net/quic/crypto/quic_decrypter.h"
10 #include "net/quic/crypto/quic_encrypter.h"
11 #include "net/quic/quic_utils.h"
12 #include "net/quic/test_tools/mock_random.h"
13 #include "net/quic/test_tools/quic_framer_peer.h"
14 #include "net/quic/test_tools/quic_packet_creator_peer.h"
15 #include "net/quic/test_tools/quic_test_utils.h"
16 #include "net/test/gtest_util.h"
17 #include "testing/gmock/include/gmock/gmock.h"
19 using base::StringPiece;
20 using std::ostream;
21 using std::string;
22 using std::vector;
23 using testing::DoAll;
24 using testing::InSequence;
25 using testing::Return;
26 using testing::SaveArg;
27 using testing::_;
29 namespace net {
30 namespace test {
31 namespace {
33 // Run tests with combinations of {QuicVersion, ToggleVersionSerialization}.
34 struct TestParams {
35 TestParams(QuicVersion version,
36 bool version_serialization,
37 QuicConnectionIdLength length)
38 : version(version),
39 connection_id_length(length),
40 version_serialization(version_serialization) {
43 friend ostream& operator<<(ostream& os, const TestParams& p) {
44 os << "{ client_version: " << QuicVersionToString(p.version)
45 << " connection id length: " << p.connection_id_length
46 << " include version: " << p.version_serialization << " }";
47 return os;
50 QuicVersion version;
51 QuicConnectionIdLength connection_id_length;
52 bool version_serialization;
55 // Constructs various test permutations.
56 vector<TestParams> GetTestParams() {
57 vector<TestParams> params;
58 QuicConnectionIdLength max = PACKET_8BYTE_CONNECTION_ID;
59 QuicVersionVector all_supported_versions = QuicSupportedVersions();
60 for (size_t i = 0; i < all_supported_versions.size(); ++i) {
61 params.push_back(TestParams(all_supported_versions[i], true, max));
62 params.push_back(TestParams(all_supported_versions[i], false, max));
64 params.push_back(TestParams(
65 all_supported_versions[0], true, PACKET_0BYTE_CONNECTION_ID));
66 params.push_back(TestParams(
67 all_supported_versions[0], true, PACKET_1BYTE_CONNECTION_ID));
68 params.push_back(TestParams(
69 all_supported_versions[0], true, PACKET_4BYTE_CONNECTION_ID));
70 return params;
73 class QuicPacketCreatorTest : public ::testing::TestWithParam<TestParams> {
74 protected:
75 QuicPacketCreatorTest()
76 : server_framer_(SupportedVersions(GetParam().version),
77 QuicTime::Zero(),
78 Perspective::IS_SERVER),
79 client_framer_(SupportedVersions(GetParam().version),
80 QuicTime::Zero(),
81 Perspective::IS_CLIENT),
82 connection_id_(2),
83 data_("foo"),
84 creator_(connection_id_, &client_framer_, &mock_random_) {
85 creator_.set_connection_id_length(GetParam().connection_id_length);
86 client_framer_.set_visitor(&framer_visitor_);
87 client_framer_.set_received_entropy_calculator(&entropy_calculator_);
88 server_framer_.set_visitor(&framer_visitor_);
91 ~QuicPacketCreatorTest() override {}
93 void ProcessPacket(QuicEncryptedPacket* encrypted) {
94 server_framer_.ProcessPacket(*encrypted);
97 void CheckStreamFrame(const QuicFrame& frame,
98 QuicStreamId stream_id,
99 const string& data,
100 QuicStreamOffset offset,
101 bool fin) {
102 EXPECT_EQ(STREAM_FRAME, frame.type);
103 ASSERT_TRUE(frame.stream_frame);
104 EXPECT_EQ(stream_id, frame.stream_frame->stream_id);
105 scoped_ptr<string> frame_data(frame.stream_frame->GetDataAsString());
106 EXPECT_EQ(data, *frame_data);
107 EXPECT_EQ(offset, frame.stream_frame->offset);
108 EXPECT_EQ(fin, frame.stream_frame->fin);
111 // Returns the number of bytes consumed by the header of packet, including
112 // the version.
113 size_t GetPacketHeaderOverhead(InFecGroup is_in_fec_group) {
114 return GetPacketHeaderSize(
115 creator_.connection_id_length(), kIncludeVersion,
116 QuicPacketCreatorPeer::NextSequenceNumberLength(&creator_),
117 is_in_fec_group);
120 // Returns the number of bytes of overhead that will be added to a packet
121 // of maximum length.
122 size_t GetEncryptionOverhead() {
123 return creator_.max_packet_length() - client_framer_.GetMaxPlaintextSize(
124 creator_.max_packet_length());
127 // Returns the number of bytes consumed by the non-data fields of a stream
128 // frame, assuming it is the last frame in the packet
129 size_t GetStreamFrameOverhead(InFecGroup is_in_fec_group) {
130 return QuicFramer::GetMinStreamFrameSize(kClientDataStreamId1, kOffset,
131 true, is_in_fec_group);
134 // Enables and turns on FEC protection. Returns true if FEC protection is on.
135 bool SwitchFecProtectionOn(size_t max_packets_per_fec_group) {
136 creator_.set_max_packets_per_fec_group(max_packets_per_fec_group);
137 creator_.StartFecProtectingPackets();
138 return creator_.IsFecProtected();
141 static const QuicStreamOffset kOffset = 1u;
143 QuicFrames frames_;
144 QuicFramer server_framer_;
145 QuicFramer client_framer_;
146 testing::StrictMock<MockFramerVisitor> framer_visitor_;
147 QuicConnectionId connection_id_;
148 string data_;
149 MockRandom mock_random_;
150 QuicPacketCreator creator_;
151 MockEntropyCalculator entropy_calculator_;
154 // Run all packet creator tests with all supported versions of QUIC, and with
155 // and without version in the packet header, as well as doing a run for each
156 // length of truncated connection id.
157 INSTANTIATE_TEST_CASE_P(QuicPacketCreatorTests,
158 QuicPacketCreatorTest,
159 ::testing::ValuesIn(GetTestParams()));
161 TEST_P(QuicPacketCreatorTest, SerializeFrames) {
162 frames_.push_back(QuicFrame(new QuicAckFrame(MakeAckFrame(0u))));
163 frames_.push_back(QuicFrame(new QuicStreamFrame(0u, false, 0u, IOVector())));
164 frames_.push_back(QuicFrame(new QuicStreamFrame(0u, true, 0u, IOVector())));
165 char buffer[kMaxPacketSize];
166 SerializedPacket serialized =
167 creator_.SerializeAllFrames(frames_, buffer, kMaxPacketSize);
168 delete frames_[0].ack_frame;
169 delete frames_[1].stream_frame;
170 delete frames_[2].stream_frame;
173 InSequence s;
174 EXPECT_CALL(framer_visitor_, OnPacket());
175 EXPECT_CALL(framer_visitor_, OnUnauthenticatedPublicHeader(_));
176 EXPECT_CALL(framer_visitor_, OnUnauthenticatedHeader(_));
177 EXPECT_CALL(framer_visitor_, OnDecryptedPacket(_));
178 EXPECT_CALL(framer_visitor_, OnPacketHeader(_));
179 EXPECT_CALL(framer_visitor_, OnAckFrame(_));
180 EXPECT_CALL(framer_visitor_, OnStreamFrame(_));
181 EXPECT_CALL(framer_visitor_, OnStreamFrame(_));
182 EXPECT_CALL(framer_visitor_, OnPacketComplete());
184 ProcessPacket(serialized.packet);
185 delete serialized.packet;
188 TEST_P(QuicPacketCreatorTest, SerializeWithFEC) {
189 // Enable FEC protection, and send FEC packet every 6 packets.
190 EXPECT_TRUE(SwitchFecProtectionOn(6));
191 // Should return false since we do not have enough packets in the FEC group to
192 // trigger an FEC packet.
193 ASSERT_FALSE(creator_.ShouldSendFec(/*force_close=*/false));
195 frames_.push_back(QuicFrame(new QuicStreamFrame(0u, false, 0u, IOVector())));
196 char buffer[kMaxPacketSize];
197 SerializedPacket serialized =
198 creator_.SerializeAllFrames(frames_, buffer, kMaxPacketSize);
199 delete frames_[0].stream_frame;
202 InSequence s;
203 EXPECT_CALL(framer_visitor_, OnPacket());
204 EXPECT_CALL(framer_visitor_, OnUnauthenticatedPublicHeader(_));
205 EXPECT_CALL(framer_visitor_, OnUnauthenticatedHeader(_));
206 EXPECT_CALL(framer_visitor_, OnDecryptedPacket(_));
207 EXPECT_CALL(framer_visitor_, OnPacketHeader(_));
208 EXPECT_CALL(framer_visitor_, OnFecProtectedPayload(_));
209 EXPECT_CALL(framer_visitor_, OnStreamFrame(_));
210 EXPECT_CALL(framer_visitor_, OnPacketComplete());
212 ProcessPacket(serialized.packet);
213 delete serialized.packet;
215 // Should return false since we do not have enough packets in the FEC group to
216 // trigger an FEC packet.
217 ASSERT_FALSE(creator_.ShouldSendFec(/*force_close=*/false));
218 // Should return true since there are packets in the FEC group.
219 ASSERT_TRUE(creator_.ShouldSendFec(/*force_close=*/true));
221 serialized = creator_.SerializeFec(buffer, kMaxPacketSize);
222 ASSERT_EQ(2u, serialized.sequence_number);
224 InSequence s;
225 EXPECT_CALL(framer_visitor_, OnPacket());
226 EXPECT_CALL(framer_visitor_, OnUnauthenticatedPublicHeader(_));
227 EXPECT_CALL(framer_visitor_, OnUnauthenticatedHeader(_));
228 EXPECT_CALL(framer_visitor_, OnDecryptedPacket(_));
229 EXPECT_CALL(framer_visitor_, OnPacketHeader(_));
230 EXPECT_CALL(framer_visitor_, OnFecData(_));
231 EXPECT_CALL(framer_visitor_, OnPacketComplete());
233 ProcessPacket(serialized.packet);
234 delete serialized.packet;
237 TEST_P(QuicPacketCreatorTest, SerializeChangingSequenceNumberLength) {
238 frames_.push_back(QuicFrame(new QuicAckFrame(MakeAckFrame(0u))));
239 creator_.AddSavedFrame(frames_[0]);
240 QuicPacketCreatorPeer::SetNextSequenceNumberLength(
241 &creator_, PACKET_4BYTE_SEQUENCE_NUMBER);
242 char buffer[kMaxPacketSize];
243 SerializedPacket serialized =
244 creator_.SerializePacket(buffer, kMaxPacketSize);
245 // The sequence number length will not change mid-packet.
246 EXPECT_EQ(PACKET_1BYTE_SEQUENCE_NUMBER, serialized.sequence_number_length);
249 InSequence s;
250 EXPECT_CALL(framer_visitor_, OnPacket());
251 EXPECT_CALL(framer_visitor_, OnUnauthenticatedPublicHeader(_));
252 EXPECT_CALL(framer_visitor_, OnUnauthenticatedHeader(_));
253 EXPECT_CALL(framer_visitor_, OnDecryptedPacket(_));
254 EXPECT_CALL(framer_visitor_, OnPacketHeader(_));
255 EXPECT_CALL(framer_visitor_, OnAckFrame(_));
256 EXPECT_CALL(framer_visitor_, OnPacketComplete());
258 ProcessPacket(serialized.packet);
259 delete serialized.packet;
261 creator_.AddSavedFrame(frames_[0]);
262 serialized = creator_.SerializePacket(buffer, kMaxPacketSize);
263 // Now the actual sequence number length should have changed.
264 EXPECT_EQ(PACKET_4BYTE_SEQUENCE_NUMBER, serialized.sequence_number_length);
265 delete frames_[0].ack_frame;
268 InSequence s;
269 EXPECT_CALL(framer_visitor_, OnPacket());
270 EXPECT_CALL(framer_visitor_, OnUnauthenticatedPublicHeader(_));
271 EXPECT_CALL(framer_visitor_, OnUnauthenticatedHeader(_));
272 EXPECT_CALL(framer_visitor_, OnDecryptedPacket(_));
273 EXPECT_CALL(framer_visitor_, OnPacketHeader(_));
274 EXPECT_CALL(framer_visitor_, OnAckFrame(_));
275 EXPECT_CALL(framer_visitor_, OnPacketComplete());
277 ProcessPacket(serialized.packet);
278 delete serialized.packet;
281 TEST_P(QuicPacketCreatorTest, ChangeSequenceNumberLengthMidPacket) {
282 // Changing the sequence number length with queued frames in the creator
283 // should hold the change until after any currently queued frames are
284 // serialized.
286 // Packet 1.
287 // Queue a frame in the creator.
288 EXPECT_FALSE(creator_.HasPendingFrames());
289 QuicFrame ack_frame = QuicFrame(new QuicAckFrame(MakeAckFrame(0u)));
290 creator_.AddSavedFrame(ack_frame);
292 // Now change sequence number length.
293 QuicPacketCreatorPeer::SetNextSequenceNumberLength(
294 &creator_, PACKET_4BYTE_SEQUENCE_NUMBER);
296 // Add a STOP_WAITING frame since it contains a packet sequence number,
297 // whose length should be 1.
298 QuicStopWaitingFrame stop_waiting_frame;
299 EXPECT_TRUE(creator_.AddSavedFrame(QuicFrame(&stop_waiting_frame)));
300 EXPECT_TRUE(creator_.HasPendingFrames());
302 // Ensure the packet is successfully created.
303 char buffer[kMaxPacketSize];
304 SerializedPacket serialized =
305 creator_.SerializePacket(buffer, kMaxPacketSize);
306 ASSERT_TRUE(serialized.packet);
307 EXPECT_EQ(PACKET_1BYTE_SEQUENCE_NUMBER, serialized.sequence_number_length);
309 // Verify that header in transmitted packet has 1 byte sequence length.
310 QuicPacketHeader header;
312 InSequence s;
313 EXPECT_CALL(framer_visitor_, OnPacket());
314 EXPECT_CALL(framer_visitor_, OnUnauthenticatedPublicHeader(_));
315 EXPECT_CALL(framer_visitor_, OnUnauthenticatedHeader(_));
316 EXPECT_CALL(framer_visitor_, OnDecryptedPacket(_));
317 EXPECT_CALL(framer_visitor_, OnPacketHeader(_)).WillOnce(
318 DoAll(SaveArg<0>(&header), Return(true)));
319 EXPECT_CALL(framer_visitor_, OnAckFrame(_));
320 EXPECT_CALL(framer_visitor_, OnStopWaitingFrame(_));
321 EXPECT_CALL(framer_visitor_, OnPacketComplete());
323 ProcessPacket(serialized.packet);
324 EXPECT_EQ(PACKET_1BYTE_SEQUENCE_NUMBER,
325 header.public_header.sequence_number_length);
326 delete serialized.packet;
328 // Packet 2.
329 EXPECT_FALSE(creator_.HasPendingFrames());
330 // Generate Packet 2 with one frame -- sequence number length should now
331 // change to 4 bytes.
332 EXPECT_TRUE(creator_.AddSavedFrame(QuicFrame(&stop_waiting_frame)));
333 EXPECT_TRUE(creator_.HasPendingFrames());
335 // Ensure the packet is successfully created.
336 serialized = creator_.SerializePacket(buffer, kMaxPacketSize);
337 ASSERT_TRUE(serialized.packet);
338 EXPECT_EQ(PACKET_4BYTE_SEQUENCE_NUMBER, serialized.sequence_number_length);
340 // Verify that header in transmitted packet has 4 byte sequence length.
342 InSequence s;
343 EXPECT_CALL(framer_visitor_, OnPacket());
344 EXPECT_CALL(framer_visitor_, OnUnauthenticatedPublicHeader(_));
345 EXPECT_CALL(framer_visitor_, OnUnauthenticatedHeader(_));
346 EXPECT_CALL(framer_visitor_, OnDecryptedPacket(_));
347 EXPECT_CALL(framer_visitor_, OnPacketHeader(_)).WillOnce(
348 DoAll(SaveArg<0>(&header), Return(true)));
349 EXPECT_CALL(framer_visitor_, OnStopWaitingFrame(_));
350 EXPECT_CALL(framer_visitor_, OnPacketComplete());
352 ProcessPacket(serialized.packet);
353 EXPECT_EQ(PACKET_4BYTE_SEQUENCE_NUMBER,
354 header.public_header.sequence_number_length);
356 delete serialized.packet;
357 delete ack_frame.ack_frame;
360 TEST_P(QuicPacketCreatorTest, SerializeWithFECChangingSequenceNumberLength) {
361 // Test goal is to test the following sequence (P1 => generate Packet 1):
362 // P1 <change seq num length> P2 FEC,
363 // and we expect that sequence number length should not change until the end
364 // of the open FEC group.
366 // Enable FEC protection, and send FEC packet every 6 packets.
367 EXPECT_TRUE(SwitchFecProtectionOn(6));
368 // Should return false since we do not have enough packets in the FEC group to
369 // trigger an FEC packet.
370 ASSERT_FALSE(creator_.ShouldSendFec(/*force_close=*/false));
371 frames_.push_back(QuicFrame(new QuicAckFrame(MakeAckFrame(0u))));
373 // Generate Packet 1.
374 creator_.AddSavedFrame(frames_[0]);
375 // Change the sequence number length mid-FEC group and it should not change.
376 QuicPacketCreatorPeer::SetNextSequenceNumberLength(
377 &creator_, PACKET_4BYTE_SEQUENCE_NUMBER);
378 char buffer[kMaxPacketSize];
379 SerializedPacket serialized =
380 creator_.SerializePacket(buffer, kMaxPacketSize);
381 EXPECT_EQ(PACKET_1BYTE_SEQUENCE_NUMBER, serialized.sequence_number_length);
384 InSequence s;
385 EXPECT_CALL(framer_visitor_, OnPacket());
386 EXPECT_CALL(framer_visitor_, OnUnauthenticatedPublicHeader(_));
387 EXPECT_CALL(framer_visitor_, OnUnauthenticatedHeader(_));
388 EXPECT_CALL(framer_visitor_, OnDecryptedPacket(_));
389 EXPECT_CALL(framer_visitor_, OnPacketHeader(_));
390 EXPECT_CALL(framer_visitor_, OnFecProtectedPayload(_));
391 EXPECT_CALL(framer_visitor_, OnAckFrame(_));
392 EXPECT_CALL(framer_visitor_, OnPacketComplete());
394 ProcessPacket(serialized.packet);
395 delete serialized.packet;
397 // Generate Packet 2.
398 creator_.AddSavedFrame(frames_[0]);
399 serialized = creator_.SerializePacket(buffer, kMaxPacketSize);
400 EXPECT_EQ(PACKET_1BYTE_SEQUENCE_NUMBER, serialized.sequence_number_length);
403 InSequence s;
404 EXPECT_CALL(framer_visitor_, OnPacket());
405 EXPECT_CALL(framer_visitor_, OnUnauthenticatedPublicHeader(_));
406 EXPECT_CALL(framer_visitor_, OnUnauthenticatedHeader(_));
407 EXPECT_CALL(framer_visitor_, OnDecryptedPacket(_));
408 EXPECT_CALL(framer_visitor_, OnPacketHeader(_));
409 EXPECT_CALL(framer_visitor_, OnFecProtectedPayload(_));
410 EXPECT_CALL(framer_visitor_, OnAckFrame(_));
411 EXPECT_CALL(framer_visitor_, OnPacketComplete());
413 ProcessPacket(serialized.packet);
414 delete serialized.packet;
416 // Should return false since we do not have enough packets in the FEC group to
417 // trigger an FEC packet.
418 ASSERT_FALSE(creator_.ShouldSendFec(/*force_close=*/false));
419 // Should return true since there are packets in the FEC group.
420 ASSERT_TRUE(creator_.ShouldSendFec(/*force_close=*/true));
422 // Force generation of FEC packet.
423 serialized = creator_.SerializeFec(buffer, kMaxPacketSize);
424 EXPECT_EQ(PACKET_1BYTE_SEQUENCE_NUMBER, serialized.sequence_number_length);
425 ASSERT_EQ(3u, serialized.sequence_number);
428 InSequence s;
429 EXPECT_CALL(framer_visitor_, OnPacket());
430 EXPECT_CALL(framer_visitor_, OnUnauthenticatedPublicHeader(_));
431 EXPECT_CALL(framer_visitor_, OnUnauthenticatedHeader(_));
432 EXPECT_CALL(framer_visitor_, OnDecryptedPacket(_));
433 EXPECT_CALL(framer_visitor_, OnPacketHeader(_));
434 EXPECT_CALL(framer_visitor_, OnFecData(_));
435 EXPECT_CALL(framer_visitor_, OnPacketComplete());
437 ProcessPacket(serialized.packet);
438 delete serialized.packet;
440 // Ensure the next FEC group starts using the new sequence number length.
441 serialized = creator_.SerializeAllFrames(frames_, buffer, kMaxPacketSize);
442 EXPECT_EQ(PACKET_4BYTE_SEQUENCE_NUMBER, serialized.sequence_number_length);
443 delete frames_[0].ack_frame;
444 delete serialized.packet;
447 TEST_P(QuicPacketCreatorTest, ReserializeFramesWithSequenceNumberLength) {
448 // If the original packet sequence number length, the current sequence number
449 // length, and the configured send sequence number length are different, the
450 // retransmit must sent with the original length and the others do not change.
451 QuicPacketCreatorPeer::SetNextSequenceNumberLength(
452 &creator_, PACKET_4BYTE_SEQUENCE_NUMBER);
453 QuicPacketCreatorPeer::SetSequenceNumberLength(&creator_,
454 PACKET_2BYTE_SEQUENCE_NUMBER);
455 QuicStreamFrame* stream_frame =
456 new QuicStreamFrame(kCryptoStreamId, /*fin=*/false, 0u, IOVector());
457 RetransmittableFrames frames(ENCRYPTION_NONE);
458 frames.AddStreamFrame(stream_frame);
459 char buffer[kMaxPacketSize];
460 SerializedPacket serialized = creator_.ReserializeAllFrames(
461 frames, PACKET_1BYTE_SEQUENCE_NUMBER, buffer, kMaxPacketSize);
462 EXPECT_EQ(PACKET_4BYTE_SEQUENCE_NUMBER,
463 QuicPacketCreatorPeer::NextSequenceNumberLength(&creator_));
464 EXPECT_EQ(PACKET_2BYTE_SEQUENCE_NUMBER,
465 QuicPacketCreatorPeer::GetSequenceNumberLength(&creator_));
466 EXPECT_EQ(PACKET_1BYTE_SEQUENCE_NUMBER, serialized.sequence_number_length);
469 InSequence s;
470 EXPECT_CALL(framer_visitor_, OnPacket());
471 EXPECT_CALL(framer_visitor_, OnUnauthenticatedPublicHeader(_));
472 EXPECT_CALL(framer_visitor_, OnUnauthenticatedHeader(_));
473 EXPECT_CALL(framer_visitor_, OnDecryptedPacket(_));
474 EXPECT_CALL(framer_visitor_, OnPacketHeader(_));
475 EXPECT_CALL(framer_visitor_, OnStreamFrame(_));
476 EXPECT_CALL(framer_visitor_, OnPacketComplete());
478 ProcessPacket(serialized.packet);
479 delete serialized.packet;
482 TEST_P(QuicPacketCreatorTest, ReserializeFramesWithPadding) {
483 QuicStreamFrame* stream_frame =
484 new QuicStreamFrame(kCryptoStreamId, /*fin=*/false, /*offset=*/0,
485 MakeIOVector("fake handshake message data"));
486 RetransmittableFrames frames(ENCRYPTION_NONE);
487 frames.AddStreamFrame(stream_frame);
488 char buffer[kMaxPacketSize];
489 SerializedPacket serialized = creator_.ReserializeAllFrames(
490 frames, QuicPacketCreatorPeer::NextSequenceNumberLength(&creator_),
491 buffer, kMaxPacketSize);
492 EXPECT_EQ(kDefaultMaxPacketSize, serialized.packet->length());
493 delete serialized.packet;
496 TEST_P(QuicPacketCreatorTest, ReserializeFramesWithFullPacketAndPadding) {
497 const size_t overhead = GetPacketHeaderOverhead(NOT_IN_FEC_GROUP)
498 + GetEncryptionOverhead() + GetStreamFrameOverhead(NOT_IN_FEC_GROUP);
499 size_t capacity = kDefaultMaxPacketSize - overhead;
500 for (int delta = -5; delta <= 0; ++delta) {
501 string data(capacity + delta, 'A');
502 size_t bytes_free = 0 - delta;
504 QuicStreamFrame* stream_frame = new QuicStreamFrame(
505 kCryptoStreamId, /*fin=*/false, kOffset, MakeIOVector(data));
506 RetransmittableFrames frames(ENCRYPTION_NONE);
507 frames.AddStreamFrame(stream_frame);
508 char buffer[kMaxPacketSize];
509 SerializedPacket serialized = creator_.ReserializeAllFrames(
510 frames, QuicPacketCreatorPeer::NextSequenceNumberLength(&creator_),
511 buffer, kMaxPacketSize);
513 // If there is not enough space in the packet to fit a padding frame
514 // (1 byte) and to expand the stream frame (another 2 bytes) the packet
515 // will not be padded.
516 if (bytes_free < 3) {
517 EXPECT_EQ(kDefaultMaxPacketSize - bytes_free,
518 serialized.packet->length());
519 } else {
520 EXPECT_EQ(kDefaultMaxPacketSize, serialized.packet->length());
523 delete serialized.packet;
524 frames_.clear();
528 TEST_P(QuicPacketCreatorTest, SerializeConnectionClose) {
529 QuicConnectionCloseFrame frame;
530 frame.error_code = QUIC_NO_ERROR;
531 frame.error_details = "error";
533 QuicFrames frames;
534 frames.push_back(QuicFrame(&frame));
535 char buffer[kMaxPacketSize];
536 SerializedPacket serialized =
537 creator_.SerializeAllFrames(frames, buffer, kMaxPacketSize);
538 ASSERT_EQ(1u, serialized.sequence_number);
539 ASSERT_EQ(1u, creator_.sequence_number());
541 InSequence s;
542 EXPECT_CALL(framer_visitor_, OnPacket());
543 EXPECT_CALL(framer_visitor_, OnUnauthenticatedPublicHeader(_));
544 EXPECT_CALL(framer_visitor_, OnUnauthenticatedHeader(_));
545 EXPECT_CALL(framer_visitor_, OnDecryptedPacket(_));
546 EXPECT_CALL(framer_visitor_, OnPacketHeader(_));
547 EXPECT_CALL(framer_visitor_, OnConnectionCloseFrame(_));
548 EXPECT_CALL(framer_visitor_, OnPacketComplete());
550 ProcessPacket(serialized.packet);
551 delete serialized.packet;
554 TEST_P(QuicPacketCreatorTest, SwitchFecOnOffWithNoGroup) {
555 // Enable FEC protection.
556 creator_.set_max_packets_per_fec_group(6);
557 EXPECT_TRUE(creator_.IsFecEnabled());
558 EXPECT_FALSE(creator_.IsFecProtected());
560 // Turn on FEC protection.
561 creator_.StartFecProtectingPackets();
562 EXPECT_TRUE(creator_.IsFecProtected());
563 // We have no packets in the FEC group, so no FEC packet can be created.
564 EXPECT_FALSE(creator_.ShouldSendFec(/*force_close=*/true));
565 // Since no packets are in FEC group yet, we should be able to turn FEC
566 // off with no trouble.
567 creator_.StopFecProtectingPackets();
568 EXPECT_FALSE(creator_.IsFecProtected());
571 TEST_P(QuicPacketCreatorTest, SwitchFecOnOffWithGroupInProgress) {
572 // Enable FEC protection, and send FEC packet every 6 packets.
573 EXPECT_TRUE(SwitchFecProtectionOn(6));
574 frames_.push_back(QuicFrame(new QuicStreamFrame(0u, false, 0u, IOVector())));
575 char buffer[kMaxPacketSize];
576 SerializedPacket serialized =
577 creator_.SerializeAllFrames(frames_, buffer, kMaxPacketSize);
578 delete frames_[0].stream_frame;
579 delete serialized.packet;
581 EXPECT_TRUE(creator_.IsFecProtected());
582 // We do not have enough packets in the FEC group to trigger an FEC packet.
583 EXPECT_FALSE(creator_.ShouldSendFec(/*force_close=*/false));
584 // Should return true since there are packets in the FEC group.
585 EXPECT_TRUE(creator_.ShouldSendFec(/*force_close=*/true));
587 // Switching FEC off should not change creator state, since there is an
588 // FEC packet under construction.
589 EXPECT_DFATAL(creator_.StopFecProtectingPackets(),
590 "Cannot stop FEC protection with open FEC group.");
591 EXPECT_TRUE(creator_.IsFecProtected());
592 // Confirm that FEC packet is still under construction.
593 EXPECT_TRUE(creator_.ShouldSendFec(/*force_close=*/true));
595 serialized = creator_.SerializeFec(buffer, kMaxPacketSize);
596 delete serialized.packet;
598 // Switching FEC on/off should work now.
599 creator_.StopFecProtectingPackets();
600 EXPECT_FALSE(creator_.IsFecProtected());
601 creator_.StartFecProtectingPackets();
602 EXPECT_TRUE(creator_.IsFecProtected());
605 TEST_P(QuicPacketCreatorTest, SwitchFecOnWithStreamFrameQueued) {
606 // Add a stream frame to the creator.
607 QuicFrame frame;
608 size_t consumed = creator_.CreateStreamFrame(
609 1u, MakeIOVector("test"), 0u, false, &frame);
610 EXPECT_EQ(4u, consumed);
611 ASSERT_TRUE(frame.stream_frame);
612 EXPECT_TRUE(creator_.AddSavedFrame(frame));
613 EXPECT_TRUE(creator_.HasPendingFrames());
615 // Enable FEC protection, and send FEC packet every 6 packets.
616 creator_.set_max_packets_per_fec_group(6);
617 EXPECT_TRUE(creator_.IsFecEnabled());
618 EXPECT_DFATAL(creator_.StartFecProtectingPackets(),
619 "Cannot start FEC protection with pending frames.");
620 EXPECT_FALSE(creator_.IsFecProtected());
622 // Serialize packet for transmission.
623 char buffer[kMaxPacketSize];
624 SerializedPacket serialized =
625 creator_.SerializePacket(buffer, kMaxPacketSize);
626 delete serialized.packet;
627 delete serialized.retransmittable_frames;
628 EXPECT_FALSE(creator_.HasPendingFrames());
630 // Since all pending frames have been serialized, turning FEC on should work.
631 creator_.StartFecProtectingPackets();
632 EXPECT_TRUE(creator_.IsFecProtected());
635 TEST_P(QuicPacketCreatorTest, CreateStreamFrame) {
636 QuicFrame frame;
637 size_t consumed = creator_.CreateStreamFrame(1u, MakeIOVector("test"), 0u,
638 false, &frame);
639 EXPECT_EQ(4u, consumed);
640 CheckStreamFrame(frame, 1u, "test", 0u, false);
641 delete frame.stream_frame;
644 TEST_P(QuicPacketCreatorTest, CreateStreamFrameFin) {
645 QuicFrame frame;
646 size_t consumed = creator_.CreateStreamFrame(1u, MakeIOVector("test"), 10u,
647 true, &frame);
648 EXPECT_EQ(4u, consumed);
649 CheckStreamFrame(frame, 1u, "test", 10u, true);
650 delete frame.stream_frame;
653 TEST_P(QuicPacketCreatorTest, CreateStreamFrameFinOnly) {
654 QuicFrame frame;
655 size_t consumed = creator_.CreateStreamFrame(1u, IOVector(), 0u, true,
656 &frame);
657 EXPECT_EQ(0u, consumed);
658 CheckStreamFrame(frame, 1u, string(), 0u, true);
659 delete frame.stream_frame;
662 TEST_P(QuicPacketCreatorTest, CreateAllFreeBytesForStreamFrames) {
663 const size_t overhead = GetPacketHeaderOverhead(NOT_IN_FEC_GROUP)
664 + GetEncryptionOverhead();
665 for (size_t i = overhead; i < overhead + 100; ++i) {
666 creator_.SetMaxPacketLength(i);
667 const bool should_have_room = i > overhead + GetStreamFrameOverhead(
668 NOT_IN_FEC_GROUP);
669 ASSERT_EQ(should_have_room, creator_.HasRoomForStreamFrame(
670 kClientDataStreamId1, kOffset));
671 if (should_have_room) {
672 QuicFrame frame;
673 size_t bytes_consumed = creator_.CreateStreamFrame(
674 kClientDataStreamId1, MakeIOVector("testdata"), kOffset, false,
675 &frame);
676 EXPECT_LT(0u, bytes_consumed);
677 ASSERT_TRUE(creator_.AddSavedFrame(frame));
678 char buffer[kMaxPacketSize];
679 SerializedPacket serialized_packet =
680 creator_.SerializePacket(buffer, kMaxPacketSize);
681 ASSERT_TRUE(serialized_packet.packet);
682 delete serialized_packet.packet;
683 delete serialized_packet.retransmittable_frames;
688 TEST_P(QuicPacketCreatorTest, StreamFrameConsumption) {
689 // Compute the total overhead for a single frame in packet.
690 const size_t overhead = GetPacketHeaderOverhead(NOT_IN_FEC_GROUP)
691 + GetEncryptionOverhead() + GetStreamFrameOverhead(NOT_IN_FEC_GROUP);
692 size_t capacity = kDefaultMaxPacketSize - overhead;
693 // Now, test various sizes around this size.
694 for (int delta = -5; delta <= 5; ++delta) {
695 string data(capacity + delta, 'A');
696 size_t bytes_free = delta > 0 ? 0 : 0 - delta;
697 QuicFrame frame;
698 size_t bytes_consumed = creator_.CreateStreamFrame(
699 kClientDataStreamId1, MakeIOVector(data), kOffset, false, &frame);
700 EXPECT_EQ(capacity - bytes_free, bytes_consumed);
702 ASSERT_TRUE(creator_.AddSavedFrame(frame));
703 // BytesFree() returns bytes available for the next frame, which will
704 // be two bytes smaller since the stream frame would need to be grown.
705 EXPECT_EQ(2u, creator_.ExpansionOnNewFrame());
706 size_t expected_bytes_free = bytes_free < 3 ? 0 : bytes_free - 2;
707 EXPECT_EQ(expected_bytes_free, creator_.BytesFree()) << "delta: " << delta;
708 char buffer[kMaxPacketSize];
709 SerializedPacket serialized_packet =
710 creator_.SerializePacket(buffer, kMaxPacketSize);
711 ASSERT_TRUE(serialized_packet.packet);
712 delete serialized_packet.packet;
713 delete serialized_packet.retransmittable_frames;
717 TEST_P(QuicPacketCreatorTest, StreamFrameConsumptionWithFec) {
718 // Enable FEC protection, and send FEC packet every 6 packets.
719 EXPECT_TRUE(SwitchFecProtectionOn(6));
720 // Compute the total overhead for a single frame in packet.
721 const size_t overhead = GetPacketHeaderOverhead(IN_FEC_GROUP)
722 + GetEncryptionOverhead() + GetStreamFrameOverhead(IN_FEC_GROUP);
723 size_t capacity = kDefaultMaxPacketSize - overhead;
724 // Now, test various sizes around this size.
725 for (int delta = -5; delta <= 5; ++delta) {
726 string data(capacity + delta, 'A');
727 size_t bytes_free = delta > 0 ? 0 : 0 - delta;
728 QuicFrame frame;
729 size_t bytes_consumed = creator_.CreateStreamFrame(
730 kClientDataStreamId1, MakeIOVector(data), kOffset, false, &frame);
731 EXPECT_EQ(capacity - bytes_free, bytes_consumed);
733 ASSERT_TRUE(creator_.AddSavedFrame(frame));
734 // BytesFree() returns bytes available for the next frame. Since stream
735 // frame does not grow for FEC protected packets, this should be the same
736 // as bytes_free (bound by 0).
737 EXPECT_EQ(0u, creator_.ExpansionOnNewFrame());
738 size_t expected_bytes_free = bytes_free > 0 ? bytes_free : 0;
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, CryptoStreamFramePacketPadding) {
750 // Compute the total overhead for a single frame in packet.
751 const size_t overhead = GetPacketHeaderOverhead(NOT_IN_FEC_GROUP)
752 + GetEncryptionOverhead() + GetStreamFrameOverhead(NOT_IN_FEC_GROUP);
753 ASSERT_GT(kMaxPacketSize, overhead);
754 size_t capacity = kDefaultMaxPacketSize - overhead;
755 // Now, test various sizes around this size.
756 for (int delta = -5; delta <= 5; ++delta) {
757 string data(capacity + delta, 'A');
758 size_t bytes_free = delta > 0 ? 0 : 0 - delta;
760 QuicFrame frame;
761 size_t bytes_consumed = creator_.CreateStreamFrame(
762 kCryptoStreamId, MakeIOVector(data), kOffset, false, &frame);
763 EXPECT_LT(0u, bytes_consumed);
764 ASSERT_TRUE(creator_.AddSavedFrame(frame));
765 char buffer[kMaxPacketSize];
766 SerializedPacket serialized_packet =
767 creator_.SerializePacket(buffer, kMaxPacketSize);
768 ASSERT_TRUE(serialized_packet.packet);
769 // If there is not enough space in the packet to fit a padding frame
770 // (1 byte) and to expand the stream frame (another 2 bytes) the packet
771 // will not be padded.
772 if (bytes_free < 3) {
773 EXPECT_EQ(kDefaultMaxPacketSize - bytes_free,
774 serialized_packet.packet->length());
775 } else {
776 EXPECT_EQ(kDefaultMaxPacketSize, serialized_packet.packet->length());
778 delete serialized_packet.packet;
779 delete serialized_packet.retransmittable_frames;
783 TEST_P(QuicPacketCreatorTest, NonCryptoStreamFramePacketNonPadding) {
784 // Compute the total overhead for a single frame in packet.
785 const size_t overhead = GetPacketHeaderOverhead(NOT_IN_FEC_GROUP)
786 + GetEncryptionOverhead() + GetStreamFrameOverhead(NOT_IN_FEC_GROUP);
787 ASSERT_GT(kDefaultMaxPacketSize, overhead);
788 size_t capacity = kDefaultMaxPacketSize - overhead;
789 // Now, test various sizes around this size.
790 for (int delta = -5; delta <= 5; ++delta) {
791 string data(capacity + delta, 'A');
792 size_t bytes_free = delta > 0 ? 0 : 0 - delta;
794 QuicFrame frame;
795 size_t bytes_consumed = creator_.CreateStreamFrame(
796 kClientDataStreamId1, MakeIOVector(data), kOffset, false, &frame);
797 EXPECT_LT(0u, bytes_consumed);
798 ASSERT_TRUE(creator_.AddSavedFrame(frame));
799 char buffer[kMaxPacketSize];
800 SerializedPacket serialized_packet =
801 creator_.SerializePacket(buffer, kMaxPacketSize);
802 ASSERT_TRUE(serialized_packet.packet);
803 if (bytes_free > 0) {
804 EXPECT_EQ(kDefaultMaxPacketSize - bytes_free,
805 serialized_packet.packet->length());
806 } else {
807 EXPECT_EQ(kDefaultMaxPacketSize, serialized_packet.packet->length());
809 delete serialized_packet.packet;
810 delete serialized_packet.retransmittable_frames;
814 TEST_P(QuicPacketCreatorTest, SerializeVersionNegotiationPacket) {
815 QuicFramerPeer::SetPerspective(&client_framer_, Perspective::IS_SERVER);
816 QuicVersionVector versions;
817 versions.push_back(test::QuicVersionMax());
818 scoped_ptr<QuicEncryptedPacket> encrypted(
819 creator_.SerializeVersionNegotiationPacket(versions));
822 InSequence s;
823 EXPECT_CALL(framer_visitor_, OnPacket());
824 EXPECT_CALL(framer_visitor_, OnUnauthenticatedPublicHeader(_));
825 EXPECT_CALL(framer_visitor_, OnVersionNegotiationPacket(_));
827 QuicFramerPeer::SetPerspective(&client_framer_, Perspective::IS_CLIENT);
828 client_framer_.ProcessPacket(*encrypted);
831 TEST_P(QuicPacketCreatorTest, UpdatePacketSequenceNumberLengthLeastAwaiting) {
832 EXPECT_EQ(PACKET_1BYTE_SEQUENCE_NUMBER,
833 QuicPacketCreatorPeer::NextSequenceNumberLength(&creator_));
835 size_t max_packets_per_fec_group = 10;
836 creator_.set_max_packets_per_fec_group(max_packets_per_fec_group);
837 QuicPacketCreatorPeer::SetSequenceNumber(&creator_,
838 64 - max_packets_per_fec_group);
839 creator_.UpdateSequenceNumberLength(2, 10000 / kDefaultMaxPacketSize);
840 EXPECT_EQ(PACKET_1BYTE_SEQUENCE_NUMBER,
841 QuicPacketCreatorPeer::NextSequenceNumberLength(&creator_));
843 QuicPacketCreatorPeer::SetSequenceNumber(
844 &creator_, 64 * 256 - max_packets_per_fec_group);
845 creator_.UpdateSequenceNumberLength(2, 10000 / kDefaultMaxPacketSize);
846 EXPECT_EQ(PACKET_2BYTE_SEQUENCE_NUMBER,
847 QuicPacketCreatorPeer::NextSequenceNumberLength(&creator_));
849 QuicPacketCreatorPeer::SetSequenceNumber(
850 &creator_, 64 * 256 * 256 - max_packets_per_fec_group);
851 creator_.UpdateSequenceNumberLength(2, 10000 / kDefaultMaxPacketSize);
852 EXPECT_EQ(PACKET_4BYTE_SEQUENCE_NUMBER,
853 QuicPacketCreatorPeer::NextSequenceNumberLength(&creator_));
855 QuicPacketCreatorPeer::SetSequenceNumber(
856 &creator_,
857 GG_UINT64_C(64) * 256 * 256 * 256 * 256 - max_packets_per_fec_group);
858 creator_.UpdateSequenceNumberLength(2, 10000 / kDefaultMaxPacketSize);
859 EXPECT_EQ(PACKET_6BYTE_SEQUENCE_NUMBER,
860 QuicPacketCreatorPeer::NextSequenceNumberLength(&creator_));
863 TEST_P(QuicPacketCreatorTest, UpdatePacketSequenceNumberLengthBandwidth) {
864 EXPECT_EQ(PACKET_1BYTE_SEQUENCE_NUMBER,
865 QuicPacketCreatorPeer::NextSequenceNumberLength(&creator_));
867 creator_.UpdateSequenceNumberLength(1, 10000 / kDefaultMaxPacketSize);
868 EXPECT_EQ(PACKET_1BYTE_SEQUENCE_NUMBER,
869 QuicPacketCreatorPeer::NextSequenceNumberLength(&creator_));
871 creator_.UpdateSequenceNumberLength(1, 10000 * 256 / kDefaultMaxPacketSize);
872 EXPECT_EQ(PACKET_2BYTE_SEQUENCE_NUMBER,
873 QuicPacketCreatorPeer::NextSequenceNumberLength(&creator_));
875 creator_.UpdateSequenceNumberLength(
876 1, 10000 * 256 * 256 / kDefaultMaxPacketSize);
877 EXPECT_EQ(PACKET_4BYTE_SEQUENCE_NUMBER,
878 QuicPacketCreatorPeer::NextSequenceNumberLength(&creator_));
880 creator_.UpdateSequenceNumberLength(
881 1, GG_UINT64_C(1000) * 256 * 256 * 256 * 256 / kDefaultMaxPacketSize);
882 EXPECT_EQ(PACKET_6BYTE_SEQUENCE_NUMBER,
883 QuicPacketCreatorPeer::NextSequenceNumberLength(&creator_));
886 TEST_P(QuicPacketCreatorTest, SerializeFrame) {
887 if (!GetParam().version_serialization) {
888 creator_.StopSendingVersion();
890 frames_.push_back(QuicFrame(new QuicStreamFrame(0u, false, 0u, IOVector())));
891 char buffer[kMaxPacketSize];
892 SerializedPacket serialized =
893 creator_.SerializeAllFrames(frames_, buffer, kMaxPacketSize);
894 delete frames_[0].stream_frame;
896 QuicPacketHeader header;
898 InSequence s;
899 EXPECT_CALL(framer_visitor_, OnPacket());
900 EXPECT_CALL(framer_visitor_, OnUnauthenticatedPublicHeader(_));
901 EXPECT_CALL(framer_visitor_, OnUnauthenticatedHeader(_));
902 EXPECT_CALL(framer_visitor_, OnDecryptedPacket(_));
903 EXPECT_CALL(framer_visitor_, OnPacketHeader(_)).WillOnce(
904 DoAll(SaveArg<0>(&header), Return(true)));
905 EXPECT_CALL(framer_visitor_, OnStreamFrame(_));
906 EXPECT_CALL(framer_visitor_, OnPacketComplete());
908 ProcessPacket(serialized.packet);
909 EXPECT_EQ(GetParam().version_serialization,
910 header.public_header.version_flag);
911 delete serialized.packet;
914 TEST_P(QuicPacketCreatorTest, CreateStreamFrameTooLarge) {
915 if (!GetParam().version_serialization) {
916 creator_.StopSendingVersion();
918 // A string larger than fits into a frame.
919 size_t payload_length;
920 creator_.SetMaxPacketLength(GetPacketLengthForOneStream(
921 client_framer_.version(),
922 QuicPacketCreatorPeer::SendVersionInPacket(&creator_),
923 creator_.connection_id_length(),
924 PACKET_1BYTE_SEQUENCE_NUMBER, NOT_IN_FEC_GROUP, &payload_length));
925 QuicFrame frame;
926 const string too_long_payload(payload_length * 2, 'a');
927 size_t consumed = creator_.CreateStreamFrame(
928 1u, MakeIOVector(too_long_payload), 0u, true, &frame);
929 EXPECT_EQ(payload_length, consumed);
930 const string payload(payload_length, 'a');
931 CheckStreamFrame(frame, 1u, payload, 0u, false);
932 delete frame.stream_frame;
935 TEST_P(QuicPacketCreatorTest, AddFrameAndSerialize) {
936 if (!GetParam().version_serialization) {
937 creator_.StopSendingVersion();
939 const size_t max_plaintext_size =
940 client_framer_.GetMaxPlaintextSize(creator_.max_packet_length());
941 EXPECT_FALSE(creator_.HasPendingFrames());
942 EXPECT_EQ(max_plaintext_size -
943 GetPacketHeaderSize(
944 creator_.connection_id_length(),
945 QuicPacketCreatorPeer::SendVersionInPacket(&creator_),
946 PACKET_1BYTE_SEQUENCE_NUMBER, NOT_IN_FEC_GROUP),
947 creator_.BytesFree());
949 // Add a variety of frame types and then a padding frame.
950 QuicAckFrame ack_frame(MakeAckFrame(0u));
951 EXPECT_TRUE(creator_.AddSavedFrame(QuicFrame(&ack_frame)));
952 EXPECT_TRUE(creator_.HasPendingFrames());
954 QuicFrame frame;
955 size_t consumed = creator_.CreateStreamFrame(
956 1u, MakeIOVector("test"), 0u, false, &frame);
957 EXPECT_EQ(4u, consumed);
958 ASSERT_TRUE(frame.stream_frame);
959 EXPECT_TRUE(creator_.AddSavedFrame(frame));
960 EXPECT_TRUE(creator_.HasPendingFrames());
962 QuicPaddingFrame padding_frame;
963 EXPECT_TRUE(creator_.AddSavedFrame(QuicFrame(&padding_frame)));
964 EXPECT_TRUE(creator_.HasPendingFrames());
965 EXPECT_EQ(0u, creator_.BytesFree());
967 EXPECT_FALSE(creator_.AddSavedFrame(QuicFrame(&ack_frame)));
969 // Ensure the packet is successfully created.
970 char buffer[kMaxPacketSize];
971 SerializedPacket serialized =
972 creator_.SerializePacket(buffer, kMaxPacketSize);
973 ASSERT_TRUE(serialized.packet);
974 delete serialized.packet;
975 ASSERT_TRUE(serialized.retransmittable_frames);
976 RetransmittableFrames* retransmittable = serialized.retransmittable_frames;
977 ASSERT_EQ(1u, retransmittable->frames().size());
978 EXPECT_EQ(STREAM_FRAME, retransmittable->frames()[0].type);
979 ASSERT_TRUE(retransmittable->frames()[0].stream_frame);
980 delete serialized.retransmittable_frames;
982 EXPECT_FALSE(creator_.HasPendingFrames());
983 EXPECT_EQ(max_plaintext_size -
984 GetPacketHeaderSize(
985 creator_.connection_id_length(),
986 QuicPacketCreatorPeer::SendVersionInPacket(&creator_),
987 PACKET_1BYTE_SEQUENCE_NUMBER,
988 NOT_IN_FEC_GROUP),
989 creator_.BytesFree());
992 TEST_P(QuicPacketCreatorTest, SerializeTruncatedAckFrameWithLargePacketSize) {
993 if (!GetParam().version_serialization) {
994 creator_.StopSendingVersion();
996 creator_.SetMaxPacketLength(kMaxPacketSize);
998 // Serialized length of ack frame with 2000 nack ranges should be limited by
999 // the number of nack ranges that can be fit in an ack frame.
1000 QuicAckFrame ack_frame = MakeAckFrameWithNackRanges(2000u, 0u);
1001 size_t frame_len = client_framer_.GetSerializedFrameLength(
1002 QuicFrame(&ack_frame), creator_.BytesFree(), true, true,
1003 NOT_IN_FEC_GROUP, PACKET_1BYTE_SEQUENCE_NUMBER);
1004 EXPECT_GT(creator_.BytesFree(), frame_len);
1005 EXPECT_GT(creator_.max_packet_length(), creator_.PacketSize());
1007 // Add ack frame to creator.
1008 EXPECT_TRUE(creator_.AddSavedFrame(QuicFrame(&ack_frame)));
1009 EXPECT_TRUE(creator_.HasPendingFrames());
1010 EXPECT_GT(creator_.max_packet_length(), creator_.PacketSize());
1011 EXPECT_LT(0u, creator_.BytesFree());
1013 // Make sure that an additional stream frame can be added to the packet.
1014 QuicFrame stream_frame;
1015 size_t consumed = creator_.CreateStreamFrame(
1016 2u, MakeIOVector("test"), 0u, false, &stream_frame);
1017 EXPECT_EQ(4u, consumed);
1018 ASSERT_TRUE(stream_frame.stream_frame);
1019 EXPECT_TRUE(creator_.AddSavedFrame(stream_frame));
1020 EXPECT_TRUE(creator_.HasPendingFrames());
1022 // Ensure the packet is successfully created, and the packet size estimate
1023 // matches the serialized packet length.
1024 EXPECT_CALL(entropy_calculator_,
1025 EntropyHash(_)).WillOnce(testing::Return(0));
1026 size_t est_packet_size = creator_.PacketSize();
1027 char buffer[kMaxPacketSize];
1028 SerializedPacket serialized =
1029 creator_.SerializePacket(buffer, kMaxPacketSize);
1030 ASSERT_TRUE(serialized.packet);
1031 EXPECT_EQ(est_packet_size,
1032 client_framer_.GetMaxPlaintextSize(serialized.packet->length()));
1033 delete serialized.retransmittable_frames;
1034 delete serialized.packet;
1037 TEST_P(QuicPacketCreatorTest, SerializeTruncatedAckFrameWithSmallPacketSize) {
1038 if (!GetParam().version_serialization) {
1039 creator_.StopSendingVersion();
1041 creator_.SetMaxPacketLength(500u);
1043 const size_t max_plaintext_size =
1044 client_framer_.GetMaxPlaintextSize(creator_.max_packet_length());
1045 EXPECT_EQ(max_plaintext_size - creator_.PacketSize(), creator_.BytesFree());
1047 // Serialized length of ack frame with 2000 nack ranges should be limited by
1048 // the packet size.
1049 QuicAckFrame ack_frame = MakeAckFrameWithNackRanges(2000u, 0u);
1050 size_t frame_len = client_framer_.GetSerializedFrameLength(
1051 QuicFrame(&ack_frame), creator_.BytesFree(), true, true,
1052 NOT_IN_FEC_GROUP, PACKET_1BYTE_SEQUENCE_NUMBER);
1053 EXPECT_EQ(creator_.BytesFree(), frame_len);
1055 // Add ack frame to creator.
1056 EXPECT_TRUE(creator_.AddSavedFrame(QuicFrame(&ack_frame)));
1057 EXPECT_TRUE(creator_.HasPendingFrames());
1058 EXPECT_EQ(client_framer_.GetMaxPlaintextSize(creator_.max_packet_length()),
1059 creator_.PacketSize());
1060 EXPECT_EQ(0u, creator_.BytesFree());
1062 // Ensure the packet is successfully created, and the packet size estimate
1063 // may not match the serialized packet length.
1064 EXPECT_CALL(entropy_calculator_,
1065 EntropyHash(_)).WillOnce(Return(0));
1066 size_t est_packet_size = creator_.PacketSize();
1067 char buffer[kMaxPacketSize];
1068 SerializedPacket serialized =
1069 creator_.SerializePacket(buffer, kMaxPacketSize);
1070 ASSERT_TRUE(serialized.packet);
1071 EXPECT_GE(est_packet_size,
1072 client_framer_.GetMaxPlaintextSize(serialized.packet->length()));
1073 delete serialized.packet;
1077 TEST_P(QuicPacketCreatorTest, EntropyFlag) {
1078 frames_.push_back(QuicFrame(new QuicStreamFrame(0u, false, 0u, IOVector())));
1080 char buffer[kMaxPacketSize];
1081 for (int i = 0; i < 2; ++i) {
1082 for (int j = 0; j < 64; ++j) {
1083 SerializedPacket serialized =
1084 creator_.SerializeAllFrames(frames_, buffer, kMaxPacketSize);
1085 // Verify both BoolSource and hash algorithm.
1086 bool expected_rand_bool =
1087 (mock_random_.RandUint64() & (GG_UINT64_C(1) << j)) != 0;
1088 bool observed_rand_bool =
1089 (serialized.entropy_hash & (1 << ((j+1) % 8))) != 0;
1090 uint8 rest_of_hash = serialized.entropy_hash & ~(1 << ((j+1) % 8));
1091 EXPECT_EQ(expected_rand_bool, observed_rand_bool);
1092 EXPECT_EQ(0, rest_of_hash);
1093 delete serialized.packet;
1095 // After 64 calls, BoolSource will refresh the bucket - make sure it does.
1096 mock_random_.ChangeValue();
1099 delete frames_[0].stream_frame;
1102 } // namespace
1103 } // namespace test
1104 } // namespace net