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
;
24 using testing::InSequence
;
25 using testing::Return
;
26 using testing::SaveArg
;
33 // Run tests with combinations of {QuicVersion, ToggleVersionSerialization}.
35 TestParams(QuicVersion version
,
36 bool version_serialization
)
38 version_serialization(version_serialization
) {
41 friend ostream
& operator<<(ostream
& os
, const TestParams
& p
) {
42 os
<< "{ client_version: " << QuicVersionToString(p
.version
)
43 << " include version: " << p
.version_serialization
<< " }";
48 bool version_serialization
;
51 // Constructs various test permutations.
52 vector
<TestParams
> GetTestParams() {
53 vector
<TestParams
> params
;
54 QuicVersionVector all_supported_versions
= QuicSupportedVersions();
55 for (size_t i
= 0; i
< all_supported_versions
.size(); ++i
) {
56 params
.push_back(TestParams(all_supported_versions
[i
], true));
57 params
.push_back(TestParams(all_supported_versions
[i
], false));
62 class QuicPacketCreatorTest
: public ::testing::TestWithParam
<TestParams
> {
64 QuicPacketCreatorTest()
65 : server_framer_(SupportedVersions(GetParam().version
), QuicTime::Zero(),
67 client_framer_(SupportedVersions(GetParam().version
), QuicTime::Zero(),
72 creator_(connection_id_
, &client_framer_
, &mock_random_
) {
73 client_framer_
.set_visitor(&framer_visitor_
);
74 client_framer_
.set_received_entropy_calculator(&entropy_calculator_
);
75 server_framer_
.set_visitor(&framer_visitor_
);
78 virtual ~QuicPacketCreatorTest() OVERRIDE
{
81 void ProcessPacket(QuicPacket
* packet
) {
82 scoped_ptr
<QuicEncryptedPacket
> encrypted(
83 server_framer_
.EncryptPacket(ENCRYPTION_NONE
, sequence_number_
,
85 server_framer_
.ProcessPacket(*encrypted
);
88 void CheckStreamFrame(const QuicFrame
& frame
,
89 QuicStreamId stream_id
,
91 QuicStreamOffset offset
,
93 EXPECT_EQ(STREAM_FRAME
, frame
.type
);
94 ASSERT_TRUE(frame
.stream_frame
);
95 EXPECT_EQ(stream_id
, frame
.stream_frame
->stream_id
);
96 scoped_ptr
<string
> frame_data(frame
.stream_frame
->GetDataAsString());
97 EXPECT_EQ(data
, *frame_data
);
98 EXPECT_EQ(offset
, frame
.stream_frame
->offset
);
99 EXPECT_EQ(fin
, frame
.stream_frame
->fin
);
102 // Returns the number of bytes consumed by the header of packet, including
104 size_t GetPacketHeaderOverhead(InFecGroup is_in_fec_group
) {
105 return GetPacketHeaderSize(creator_
.connection_id_length(),
107 creator_
.next_sequence_number_length(),
111 // Returns the number of bytes of overhead that will be added to a packet
112 // of maximum length.
113 size_t GetEncryptionOverhead() {
114 return creator_
.max_packet_length() - client_framer_
.GetMaxPlaintextSize(
115 creator_
.max_packet_length());
118 // Returns the number of bytes consumed by the non-data fields of a stream
119 // frame, assuming it is the last frame in the packet
120 size_t GetStreamFrameOverhead(InFecGroup is_in_fec_group
) {
121 return QuicFramer::GetMinStreamFrameSize(kClientDataStreamId1
, kOffset
,
122 true, is_in_fec_group
);
125 // Enables and turns on FEC protection. Returns true if FEC protection is on.
126 bool SwitchFecProtectionOn(size_t max_packets_per_fec_group
) {
127 creator_
.set_max_packets_per_fec_group(max_packets_per_fec_group
);
128 creator_
.StartFecProtectingPackets();
129 return creator_
.IsFecProtected();
132 static const QuicStreamOffset kOffset
= 1u;
135 QuicFramer server_framer_
;
136 QuicFramer client_framer_
;
137 testing::StrictMock
<MockFramerVisitor
> framer_visitor_
;
138 QuicPacketSequenceNumber sequence_number_
;
139 QuicConnectionId connection_id_
;
141 MockRandom mock_random_
;
142 QuicPacketCreator creator_
;
143 MockEntropyCalculator entropy_calculator_
;
146 // Run all packet creator tests with all supported versions of QUIC, and with
147 // and without version in the packet header.
148 INSTANTIATE_TEST_CASE_P(QuicPacketCreatorTests
,
149 QuicPacketCreatorTest
,
150 ::testing::ValuesIn(GetTestParams()));
152 TEST_P(QuicPacketCreatorTest
, SerializeFrames
) {
153 frames_
.push_back(QuicFrame(new QuicAckFrame(MakeAckFrame(0u))));
154 frames_
.push_back(QuicFrame(new QuicStreamFrame(0u, false, 0u, IOVector())));
155 frames_
.push_back(QuicFrame(new QuicStreamFrame(0u, true, 0u, IOVector())));
156 SerializedPacket serialized
= creator_
.SerializeAllFrames(frames_
);
157 delete frames_
[0].ack_frame
;
158 delete frames_
[1].stream_frame
;
159 delete frames_
[2].stream_frame
;
163 EXPECT_CALL(framer_visitor_
, OnPacket());
164 EXPECT_CALL(framer_visitor_
, OnUnauthenticatedPublicHeader(_
));
165 EXPECT_CALL(framer_visitor_
, OnUnauthenticatedHeader(_
));
166 EXPECT_CALL(framer_visitor_
, OnDecryptedPacket(_
));
167 EXPECT_CALL(framer_visitor_
, OnPacketHeader(_
));
168 EXPECT_CALL(framer_visitor_
, OnAckFrame(_
));
169 EXPECT_CALL(framer_visitor_
, OnStreamFrame(_
));
170 EXPECT_CALL(framer_visitor_
, OnStreamFrame(_
));
171 EXPECT_CALL(framer_visitor_
, OnPacketComplete());
173 ProcessPacket(serialized
.packet
);
174 delete serialized
.packet
;
177 TEST_P(QuicPacketCreatorTest
, SerializeWithFEC
) {
178 // Enable FEC protection, and send FEC packet every 6 packets.
179 EXPECT_TRUE(SwitchFecProtectionOn(6));
180 // Should return false since we do not have enough packets in the FEC group to
181 // trigger an FEC packet.
182 ASSERT_FALSE(creator_
.ShouldSendFec(/*force_close=*/false));
184 frames_
.push_back(QuicFrame(new QuicStreamFrame(0u, false, 0u, IOVector())));
185 SerializedPacket serialized
= creator_
.SerializeAllFrames(frames_
);
186 delete frames_
[0].stream_frame
;
190 EXPECT_CALL(framer_visitor_
, OnPacket());
191 EXPECT_CALL(framer_visitor_
, OnUnauthenticatedPublicHeader(_
));
192 EXPECT_CALL(framer_visitor_
, OnUnauthenticatedHeader(_
));
193 EXPECT_CALL(framer_visitor_
, OnDecryptedPacket(_
));
194 EXPECT_CALL(framer_visitor_
, OnPacketHeader(_
));
195 EXPECT_CALL(framer_visitor_
, OnFecProtectedPayload(_
));
196 EXPECT_CALL(framer_visitor_
, OnStreamFrame(_
));
197 EXPECT_CALL(framer_visitor_
, OnPacketComplete());
199 ProcessPacket(serialized
.packet
);
200 delete serialized
.packet
;
202 // Should return false since we do not have enough packets in the FEC group to
203 // trigger an FEC packet.
204 ASSERT_FALSE(creator_
.ShouldSendFec(/*force_close=*/false));
205 // Should return true since there are packets in the FEC group.
206 ASSERT_TRUE(creator_
.ShouldSendFec(/*force_close=*/true));
208 serialized
= creator_
.SerializeFec();
209 ASSERT_EQ(2u, serialized
.sequence_number
);
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_
, OnFecData(_
));
218 EXPECT_CALL(framer_visitor_
, OnPacketComplete());
220 ProcessPacket(serialized
.packet
);
221 delete serialized
.packet
;
224 TEST_P(QuicPacketCreatorTest
, SerializeChangingSequenceNumberLength
) {
225 frames_
.push_back(QuicFrame(new QuicAckFrame(MakeAckFrame(0u))));
226 creator_
.AddSavedFrame(frames_
[0]);
227 creator_
.set_next_sequence_number_length(PACKET_4BYTE_SEQUENCE_NUMBER
);
228 SerializedPacket serialized
= creator_
.SerializePacket();
229 // The sequence number length will not change mid-packet.
230 EXPECT_EQ(PACKET_1BYTE_SEQUENCE_NUMBER
, serialized
.sequence_number_length
);
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_
, OnAckFrame(_
));
240 EXPECT_CALL(framer_visitor_
, OnPacketComplete());
242 ProcessPacket(serialized
.packet
);
243 delete serialized
.packet
;
245 creator_
.AddSavedFrame(frames_
[0]);
246 serialized
= creator_
.SerializePacket();
247 // Now the actual sequence number length should have changed.
248 EXPECT_EQ(PACKET_4BYTE_SEQUENCE_NUMBER
, serialized
.sequence_number_length
);
249 delete frames_
[0].ack_frame
;
253 EXPECT_CALL(framer_visitor_
, OnPacket());
254 EXPECT_CALL(framer_visitor_
, OnUnauthenticatedPublicHeader(_
));
255 EXPECT_CALL(framer_visitor_
, OnUnauthenticatedHeader(_
));
256 EXPECT_CALL(framer_visitor_
, OnDecryptedPacket(_
));
257 EXPECT_CALL(framer_visitor_
, OnPacketHeader(_
));
258 EXPECT_CALL(framer_visitor_
, OnAckFrame(_
));
259 EXPECT_CALL(framer_visitor_
, OnPacketComplete());
261 ProcessPacket(serialized
.packet
);
262 delete serialized
.packet
;
265 TEST_P(QuicPacketCreatorTest
, ChangeSequenceNumberLengthMidPacket
) {
266 // Changing the sequence number length with queued frames in the creator
267 // should hold the change until after any currently queued frames are
271 // Queue a frame in the creator.
272 EXPECT_FALSE(creator_
.HasPendingFrames());
273 QuicFrame ack_frame
= QuicFrame(new QuicAckFrame(MakeAckFrame(0u)));
274 creator_
.AddSavedFrame(ack_frame
);
276 // Now change sequence number length.
277 creator_
.set_next_sequence_number_length(PACKET_4BYTE_SEQUENCE_NUMBER
);
279 // Add a STOP_WAITING frame since it contains a packet sequence number,
280 // whose length should be 1.
281 QuicStopWaitingFrame stop_waiting_frame
;
282 EXPECT_TRUE(creator_
.AddSavedFrame(QuicFrame(&stop_waiting_frame
)));
283 EXPECT_TRUE(creator_
.HasPendingFrames());
285 // Ensure the packet is successfully created.
286 SerializedPacket serialized
= creator_
.SerializePacket();
287 ASSERT_TRUE(serialized
.packet
);
288 EXPECT_EQ(PACKET_1BYTE_SEQUENCE_NUMBER
, serialized
.sequence_number_length
);
290 // Verify that header in transmitted packet has 1 byte sequence length.
291 QuicPacketHeader header
;
294 EXPECT_CALL(framer_visitor_
, OnPacket());
295 EXPECT_CALL(framer_visitor_
, OnUnauthenticatedPublicHeader(_
));
296 EXPECT_CALL(framer_visitor_
, OnUnauthenticatedHeader(_
));
297 EXPECT_CALL(framer_visitor_
, OnDecryptedPacket(_
));
298 EXPECT_CALL(framer_visitor_
, OnPacketHeader(_
)).WillOnce(
299 DoAll(SaveArg
<0>(&header
), Return(true)));
300 EXPECT_CALL(framer_visitor_
, OnAckFrame(_
));
301 EXPECT_CALL(framer_visitor_
, OnStopWaitingFrame(_
));
302 EXPECT_CALL(framer_visitor_
, OnPacketComplete());
304 ProcessPacket(serialized
.packet
);
305 EXPECT_EQ(PACKET_1BYTE_SEQUENCE_NUMBER
,
306 header
.public_header
.sequence_number_length
);
307 delete serialized
.packet
;
310 EXPECT_FALSE(creator_
.HasPendingFrames());
311 // Generate Packet 2 with one frame -- sequence number length should now
312 // change to 4 bytes.
313 EXPECT_TRUE(creator_
.AddSavedFrame(QuicFrame(&stop_waiting_frame
)));
314 EXPECT_TRUE(creator_
.HasPendingFrames());
316 // Ensure the packet is successfully created.
317 serialized
= creator_
.SerializePacket();
318 ASSERT_TRUE(serialized
.packet
);
319 EXPECT_EQ(PACKET_4BYTE_SEQUENCE_NUMBER
, serialized
.sequence_number_length
);
321 // Verify that header in transmitted packet has 4 byte sequence length.
324 EXPECT_CALL(framer_visitor_
, OnPacket());
325 EXPECT_CALL(framer_visitor_
, OnUnauthenticatedPublicHeader(_
));
326 EXPECT_CALL(framer_visitor_
, OnUnauthenticatedHeader(_
));
327 EXPECT_CALL(framer_visitor_
, OnDecryptedPacket(_
));
328 EXPECT_CALL(framer_visitor_
, OnPacketHeader(_
)).WillOnce(
329 DoAll(SaveArg
<0>(&header
), Return(true)));
330 EXPECT_CALL(framer_visitor_
, OnStopWaitingFrame(_
));
331 EXPECT_CALL(framer_visitor_
, OnPacketComplete());
333 ProcessPacket(serialized
.packet
);
334 EXPECT_EQ(PACKET_4BYTE_SEQUENCE_NUMBER
,
335 header
.public_header
.sequence_number_length
);
337 delete serialized
.packet
;
338 delete ack_frame
.ack_frame
;
341 TEST_P(QuicPacketCreatorTest
, SerializeWithFECChangingSequenceNumberLength
) {
342 // Test goal is to test the following sequence (P1 => generate Packet 1):
343 // P1 <change seq num length> P2 FEC,
344 // and we expect that sequence number length should not change until the end
345 // of the open FEC group.
347 // Enable FEC protection, and send FEC packet every 6 packets.
348 EXPECT_TRUE(SwitchFecProtectionOn(6));
349 // Should return false since we do not have enough packets in the FEC group to
350 // trigger an FEC packet.
351 ASSERT_FALSE(creator_
.ShouldSendFec(/*force_close=*/false));
352 frames_
.push_back(QuicFrame(new QuicAckFrame(MakeAckFrame(0u))));
354 // Generate Packet 1.
355 creator_
.AddSavedFrame(frames_
[0]);
356 // Change the sequence number length mid-FEC group and it should not change.
357 creator_
.set_next_sequence_number_length(PACKET_4BYTE_SEQUENCE_NUMBER
);
358 SerializedPacket serialized
= creator_
.SerializePacket();
359 EXPECT_EQ(PACKET_1BYTE_SEQUENCE_NUMBER
, serialized
.sequence_number_length
);
363 EXPECT_CALL(framer_visitor_
, OnPacket());
364 EXPECT_CALL(framer_visitor_
, OnUnauthenticatedPublicHeader(_
));
365 EXPECT_CALL(framer_visitor_
, OnUnauthenticatedHeader(_
));
366 EXPECT_CALL(framer_visitor_
, OnDecryptedPacket(_
));
367 EXPECT_CALL(framer_visitor_
, OnPacketHeader(_
));
368 EXPECT_CALL(framer_visitor_
, OnFecProtectedPayload(_
));
369 EXPECT_CALL(framer_visitor_
, OnAckFrame(_
));
370 EXPECT_CALL(framer_visitor_
, OnPacketComplete());
372 ProcessPacket(serialized
.packet
);
373 delete serialized
.packet
;
375 // Generate Packet 2.
376 creator_
.AddSavedFrame(frames_
[0]);
377 serialized
= creator_
.SerializePacket();
378 EXPECT_EQ(PACKET_1BYTE_SEQUENCE_NUMBER
, serialized
.sequence_number_length
);
382 EXPECT_CALL(framer_visitor_
, OnPacket());
383 EXPECT_CALL(framer_visitor_
, OnUnauthenticatedPublicHeader(_
));
384 EXPECT_CALL(framer_visitor_
, OnUnauthenticatedHeader(_
));
385 EXPECT_CALL(framer_visitor_
, OnDecryptedPacket(_
));
386 EXPECT_CALL(framer_visitor_
, OnPacketHeader(_
));
387 EXPECT_CALL(framer_visitor_
, OnFecProtectedPayload(_
));
388 EXPECT_CALL(framer_visitor_
, OnAckFrame(_
));
389 EXPECT_CALL(framer_visitor_
, OnPacketComplete());
391 ProcessPacket(serialized
.packet
);
392 delete serialized
.packet
;
394 // Should return false since we do not have enough packets in the FEC group to
395 // trigger an FEC packet.
396 ASSERT_FALSE(creator_
.ShouldSendFec(/*force_close=*/false));
397 // Should return true since there are packets in the FEC group.
398 ASSERT_TRUE(creator_
.ShouldSendFec(/*force_close=*/true));
400 // Force generation of FEC packet.
401 serialized
= creator_
.SerializeFec();
402 EXPECT_EQ(PACKET_1BYTE_SEQUENCE_NUMBER
, serialized
.sequence_number_length
);
403 ASSERT_EQ(3u, serialized
.sequence_number
);
407 EXPECT_CALL(framer_visitor_
, OnPacket());
408 EXPECT_CALL(framer_visitor_
, OnUnauthenticatedPublicHeader(_
));
409 EXPECT_CALL(framer_visitor_
, OnUnauthenticatedHeader(_
));
410 EXPECT_CALL(framer_visitor_
, OnDecryptedPacket(_
));
411 EXPECT_CALL(framer_visitor_
, OnPacketHeader(_
));
412 EXPECT_CALL(framer_visitor_
, OnFecData(_
));
413 EXPECT_CALL(framer_visitor_
, OnPacketComplete());
415 ProcessPacket(serialized
.packet
);
416 delete serialized
.packet
;
418 // Ensure the next FEC group starts using the new sequence number length.
419 serialized
= creator_
.SerializeAllFrames(frames_
);
420 EXPECT_EQ(PACKET_4BYTE_SEQUENCE_NUMBER
, serialized
.sequence_number_length
);
421 delete frames_
[0].ack_frame
;
422 delete serialized
.packet
;
425 TEST_P(QuicPacketCreatorTest
, ReserializeFramesWithSequenceNumberLength
) {
426 // If the original packet sequence number length, the current sequence number
427 // length, and the configured send sequence number length are different, the
428 // retransmit must sent with the original length and the others do not change.
429 creator_
.set_next_sequence_number_length(PACKET_4BYTE_SEQUENCE_NUMBER
);
430 QuicPacketCreatorPeer::SetSequenceNumberLength(&creator_
,
431 PACKET_2BYTE_SEQUENCE_NUMBER
);
432 frames_
.push_back(QuicFrame(new QuicStreamFrame(0u, false, 0u, IOVector())));
433 SerializedPacket serialized
=
434 creator_
.ReserializeAllFrames(frames_
, PACKET_1BYTE_SEQUENCE_NUMBER
);
435 EXPECT_EQ(PACKET_4BYTE_SEQUENCE_NUMBER
,
436 creator_
.next_sequence_number_length());
437 EXPECT_EQ(PACKET_2BYTE_SEQUENCE_NUMBER
,
438 QuicPacketCreatorPeer::GetSequenceNumberLength(&creator_
));
439 EXPECT_EQ(PACKET_1BYTE_SEQUENCE_NUMBER
, serialized
.sequence_number_length
);
440 delete frames_
[0].stream_frame
;
444 EXPECT_CALL(framer_visitor_
, OnPacket());
445 EXPECT_CALL(framer_visitor_
, OnUnauthenticatedPublicHeader(_
));
446 EXPECT_CALL(framer_visitor_
, OnUnauthenticatedHeader(_
));
447 EXPECT_CALL(framer_visitor_
, OnDecryptedPacket(_
));
448 EXPECT_CALL(framer_visitor_
, OnPacketHeader(_
));
449 EXPECT_CALL(framer_visitor_
, OnStreamFrame(_
));
450 EXPECT_CALL(framer_visitor_
, OnPacketComplete());
452 ProcessPacket(serialized
.packet
);
453 delete serialized
.packet
;
456 TEST_P(QuicPacketCreatorTest
, SerializeConnectionClose
) {
457 QuicConnectionCloseFrame frame
;
458 frame
.error_code
= QUIC_NO_ERROR
;
459 frame
.error_details
= "error";
461 SerializedPacket serialized
= creator_
.SerializeConnectionClose(&frame
);
462 ASSERT_EQ(1u, serialized
.sequence_number
);
463 ASSERT_EQ(1u, creator_
.sequence_number());
466 EXPECT_CALL(framer_visitor_
, OnPacket());
467 EXPECT_CALL(framer_visitor_
, OnUnauthenticatedPublicHeader(_
));
468 EXPECT_CALL(framer_visitor_
, OnUnauthenticatedHeader(_
));
469 EXPECT_CALL(framer_visitor_
, OnDecryptedPacket(_
));
470 EXPECT_CALL(framer_visitor_
, OnPacketHeader(_
));
471 EXPECT_CALL(framer_visitor_
, OnConnectionCloseFrame(_
));
472 EXPECT_CALL(framer_visitor_
, OnPacketComplete());
474 ProcessPacket(serialized
.packet
);
475 delete serialized
.packet
;
478 TEST_P(QuicPacketCreatorTest
, SwitchFecOnOffWithNoGroup
) {
479 // Enable FEC protection.
480 creator_
.set_max_packets_per_fec_group(6);
481 EXPECT_TRUE(creator_
.IsFecEnabled());
482 EXPECT_FALSE(creator_
.IsFecProtected());
484 // Turn on FEC protection.
485 creator_
.StartFecProtectingPackets();
486 EXPECT_TRUE(creator_
.IsFecProtected());
487 // We have no packets in the FEC group, so no FEC packet can be created.
488 EXPECT_FALSE(creator_
.ShouldSendFec(/*force_close=*/true));
489 // Since no packets are in FEC group yet, we should be able to turn FEC
490 // off with no trouble.
491 creator_
.StopFecProtectingPackets();
492 EXPECT_FALSE(creator_
.IsFecProtected());
495 TEST_P(QuicPacketCreatorTest
, SwitchFecOnOffWithGroupInProgress
) {
496 // Enable FEC protection, and send FEC packet every 6 packets.
497 EXPECT_TRUE(SwitchFecProtectionOn(6));
498 frames_
.push_back(QuicFrame(new QuicStreamFrame(0u, false, 0u, IOVector())));
499 SerializedPacket serialized
= creator_
.SerializeAllFrames(frames_
);
500 delete frames_
[0].stream_frame
;
501 delete serialized
.packet
;
503 EXPECT_TRUE(creator_
.IsFecProtected());
504 // We do not have enough packets in the FEC group to trigger an FEC packet.
505 EXPECT_FALSE(creator_
.ShouldSendFec(/*force_close=*/false));
506 // Should return true since there are packets in the FEC group.
507 EXPECT_TRUE(creator_
.ShouldSendFec(/*force_close=*/true));
509 // Switching FEC off should not change creator state, since there is an
510 // FEC packet under construction.
511 EXPECT_DFATAL(creator_
.StopFecProtectingPackets(),
512 "Cannot stop FEC protection with open FEC group.");
513 EXPECT_TRUE(creator_
.IsFecProtected());
514 // Confirm that FEC packet is still under construction.
515 EXPECT_TRUE(creator_
.ShouldSendFec(/*force_close=*/true));
517 serialized
= creator_
.SerializeFec();
518 delete serialized
.packet
;
520 // Switching FEC on/off should work now.
521 creator_
.StopFecProtectingPackets();
522 EXPECT_FALSE(creator_
.IsFecProtected());
523 creator_
.StartFecProtectingPackets();
524 EXPECT_TRUE(creator_
.IsFecProtected());
527 TEST_P(QuicPacketCreatorTest
, SwitchFecOnWithStreamFrameQueued
) {
528 // Add a stream frame to the creator.
530 size_t consumed
= creator_
.CreateStreamFrame(
531 1u, MakeIOVector("test"), 0u, false, &frame
);
532 EXPECT_EQ(4u, consumed
);
533 ASSERT_TRUE(frame
.stream_frame
);
534 EXPECT_TRUE(creator_
.AddSavedFrame(frame
));
535 EXPECT_TRUE(creator_
.HasPendingFrames());
537 // Enable FEC protection, and send FEC packet every 6 packets.
538 creator_
.set_max_packets_per_fec_group(6);
539 EXPECT_TRUE(creator_
.IsFecEnabled());
540 EXPECT_DFATAL(creator_
.StartFecProtectingPackets(),
541 "Cannot start FEC protection with pending frames.");
542 EXPECT_FALSE(creator_
.IsFecProtected());
544 // Serialize packet for transmission.
545 SerializedPacket serialized
= creator_
.SerializePacket();
546 delete serialized
.packet
;
547 delete serialized
.retransmittable_frames
;
548 EXPECT_FALSE(creator_
.HasPendingFrames());
550 // Since all pending frames have been serialized, turning FEC on should work.
551 creator_
.StartFecProtectingPackets();
552 EXPECT_TRUE(creator_
.IsFecProtected());
555 TEST_P(QuicPacketCreatorTest
, CreateStreamFrame
) {
557 size_t consumed
= creator_
.CreateStreamFrame(1u, MakeIOVector("test"), 0u,
559 EXPECT_EQ(4u, consumed
);
560 CheckStreamFrame(frame
, 1u, "test", 0u, false);
561 delete frame
.stream_frame
;
564 TEST_P(QuicPacketCreatorTest
, CreateStreamFrameFin
) {
566 size_t consumed
= creator_
.CreateStreamFrame(1u, MakeIOVector("test"), 10u,
568 EXPECT_EQ(4u, consumed
);
569 CheckStreamFrame(frame
, 1u, "test", 10u, true);
570 delete frame
.stream_frame
;
573 TEST_P(QuicPacketCreatorTest
, CreateStreamFrameFinOnly
) {
575 size_t consumed
= creator_
.CreateStreamFrame(1u, IOVector(), 0u, true,
577 EXPECT_EQ(0u, consumed
);
578 CheckStreamFrame(frame
, 1u, string(), 0u, true);
579 delete frame
.stream_frame
;
582 TEST_P(QuicPacketCreatorTest
, CreateAllFreeBytesForStreamFrames
) {
583 const size_t overhead
= GetPacketHeaderOverhead(NOT_IN_FEC_GROUP
)
584 + GetEncryptionOverhead();
585 for (size_t i
= overhead
; i
< overhead
+ 100; ++i
) {
586 creator_
.set_max_packet_length(i
);
587 const bool should_have_room
= i
> overhead
+ GetStreamFrameOverhead(
589 ASSERT_EQ(should_have_room
, creator_
.HasRoomForStreamFrame(
590 kClientDataStreamId1
, kOffset
));
591 if (should_have_room
) {
593 size_t bytes_consumed
= creator_
.CreateStreamFrame(
594 kClientDataStreamId1
, MakeIOVector("testdata"), kOffset
, false,
596 EXPECT_LT(0u, bytes_consumed
);
597 ASSERT_TRUE(creator_
.AddSavedFrame(frame
));
598 SerializedPacket serialized_packet
= creator_
.SerializePacket();
599 ASSERT_TRUE(serialized_packet
.packet
);
600 delete serialized_packet
.packet
;
601 delete serialized_packet
.retransmittable_frames
;
606 TEST_P(QuicPacketCreatorTest
, StreamFrameConsumption
) {
607 // Compute the total overhead for a single frame in packet.
608 const size_t overhead
= GetPacketHeaderOverhead(NOT_IN_FEC_GROUP
)
609 + GetEncryptionOverhead() + GetStreamFrameOverhead(NOT_IN_FEC_GROUP
);
610 size_t capacity
= kDefaultMaxPacketSize
- overhead
;
611 // Now, test various sizes around this size.
612 for (int delta
= -5; delta
<= 5; ++delta
) {
613 string
data(capacity
+ delta
, 'A');
614 size_t bytes_free
= delta
> 0 ? 0 : 0 - delta
;
616 size_t bytes_consumed
= creator_
.CreateStreamFrame(
617 kClientDataStreamId1
, MakeIOVector(data
), kOffset
, false, &frame
);
618 EXPECT_EQ(capacity
- bytes_free
, bytes_consumed
);
620 ASSERT_TRUE(creator_
.AddSavedFrame(frame
));
621 // BytesFree() returns bytes available for the next frame, which will
622 // be two bytes smaller since the stream frame would need to be grown.
623 EXPECT_EQ(2u, creator_
.ExpansionOnNewFrame());
624 size_t expected_bytes_free
= bytes_free
< 3 ? 0 : bytes_free
- 2;
625 EXPECT_EQ(expected_bytes_free
, creator_
.BytesFree()) << "delta: " << delta
;
626 SerializedPacket serialized_packet
= creator_
.SerializePacket();
627 ASSERT_TRUE(serialized_packet
.packet
);
628 delete serialized_packet
.packet
;
629 delete serialized_packet
.retransmittable_frames
;
633 TEST_P(QuicPacketCreatorTest
, StreamFrameConsumptionWithFec
) {
634 // Enable FEC protection, and send FEC packet every 6 packets.
635 EXPECT_TRUE(SwitchFecProtectionOn(6));
636 // Compute the total overhead for a single frame in packet.
637 const size_t overhead
= GetPacketHeaderOverhead(IN_FEC_GROUP
)
638 + GetEncryptionOverhead() + GetStreamFrameOverhead(IN_FEC_GROUP
);
639 size_t capacity
= kDefaultMaxPacketSize
- overhead
;
640 // Now, test various sizes around this size.
641 for (int delta
= -5; delta
<= 5; ++delta
) {
642 string
data(capacity
+ delta
, 'A');
643 size_t bytes_free
= delta
> 0 ? 0 : 0 - delta
;
645 size_t bytes_consumed
= creator_
.CreateStreamFrame(
646 kClientDataStreamId1
, MakeIOVector(data
), kOffset
, false, &frame
);
647 EXPECT_EQ(capacity
- bytes_free
, bytes_consumed
);
649 ASSERT_TRUE(creator_
.AddSavedFrame(frame
));
650 // BytesFree() returns bytes available for the next frame. Since stream
651 // frame does not grow for FEC protected packets, this should be the same
652 // as bytes_free (bound by 0).
653 EXPECT_EQ(0u, creator_
.ExpansionOnNewFrame());
654 size_t expected_bytes_free
= bytes_free
> 0 ? bytes_free
: 0;
655 EXPECT_EQ(expected_bytes_free
, creator_
.BytesFree()) << "delta: " << delta
;
656 SerializedPacket serialized_packet
= creator_
.SerializePacket();
657 ASSERT_TRUE(serialized_packet
.packet
);
658 delete serialized_packet
.packet
;
659 delete serialized_packet
.retransmittable_frames
;
663 TEST_P(QuicPacketCreatorTest
, CryptoStreamFramePacketPadding
) {
664 // Compute the total overhead for a single frame in packet.
665 const size_t overhead
= GetPacketHeaderOverhead(NOT_IN_FEC_GROUP
)
666 + GetEncryptionOverhead() + GetStreamFrameOverhead(NOT_IN_FEC_GROUP
);
667 ASSERT_GT(kMaxPacketSize
, overhead
);
668 size_t capacity
= kDefaultMaxPacketSize
- overhead
;
669 // Now, test various sizes around this size.
670 for (int delta
= -5; delta
<= 5; ++delta
) {
671 string
data(capacity
+ delta
, 'A');
672 size_t bytes_free
= delta
> 0 ? 0 : 0 - delta
;
675 size_t bytes_consumed
= creator_
.CreateStreamFrame(
676 kCryptoStreamId
, MakeIOVector(data
), kOffset
, false, &frame
);
677 EXPECT_LT(0u, bytes_consumed
);
678 ASSERT_TRUE(creator_
.AddSavedFrame(frame
));
679 SerializedPacket serialized_packet
= creator_
.SerializePacket();
680 ASSERT_TRUE(serialized_packet
.packet
);
681 // If there is not enough space in the packet to fit a padding frame
682 // (1 byte) and to expand the stream frame (another 2 bytes) the packet
683 // will not be padded.
684 if (bytes_free
< 3) {
685 EXPECT_EQ(client_framer_
.GetMaxPlaintextSize(kDefaultMaxPacketSize
)
686 - bytes_free
, serialized_packet
.packet
->length());
688 EXPECT_EQ(client_framer_
.GetMaxPlaintextSize(kDefaultMaxPacketSize
),
689 serialized_packet
.packet
->length());
691 delete serialized_packet
.packet
;
692 delete serialized_packet
.retransmittable_frames
;
696 TEST_P(QuicPacketCreatorTest
, NonCryptoStreamFramePacketNonPadding
) {
697 // Compute the total overhead for a single frame in packet.
698 const size_t overhead
= GetPacketHeaderOverhead(NOT_IN_FEC_GROUP
)
699 + GetEncryptionOverhead() + GetStreamFrameOverhead(NOT_IN_FEC_GROUP
);
700 ASSERT_GT(kDefaultMaxPacketSize
, overhead
);
701 size_t capacity
= kDefaultMaxPacketSize
- overhead
;
702 // Now, test various sizes around this size.
703 for (int delta
= -5; delta
<= 5; ++delta
) {
704 string
data(capacity
+ delta
, 'A');
705 size_t bytes_free
= delta
> 0 ? 0 : 0 - delta
;
708 size_t bytes_consumed
= creator_
.CreateStreamFrame(
709 kClientDataStreamId1
, MakeIOVector(data
), kOffset
, false, &frame
);
710 EXPECT_LT(0u, bytes_consumed
);
711 ASSERT_TRUE(creator_
.AddSavedFrame(frame
));
712 SerializedPacket serialized_packet
= creator_
.SerializePacket();
713 ASSERT_TRUE(serialized_packet
.packet
);
714 if (bytes_free
> 0) {
715 EXPECT_EQ(client_framer_
.GetMaxPlaintextSize(kDefaultMaxPacketSize
)
716 - bytes_free
, serialized_packet
.packet
->length());
718 EXPECT_EQ(client_framer_
.GetMaxPlaintextSize(kDefaultMaxPacketSize
),
719 serialized_packet
.packet
->length());
721 delete serialized_packet
.packet
;
722 delete serialized_packet
.retransmittable_frames
;
726 TEST_P(QuicPacketCreatorTest
, SerializeVersionNegotiationPacket
) {
727 QuicFramerPeer::SetIsServer(&client_framer_
, true);
728 QuicVersionVector versions
;
729 versions
.push_back(test::QuicVersionMax());
730 scoped_ptr
<QuicEncryptedPacket
> encrypted(
731 creator_
.SerializeVersionNegotiationPacket(versions
));
735 EXPECT_CALL(framer_visitor_
, OnPacket());
736 EXPECT_CALL(framer_visitor_
, OnUnauthenticatedPublicHeader(_
));
737 EXPECT_CALL(framer_visitor_
, OnVersionNegotiationPacket(_
));
739 QuicFramerPeer::SetIsServer(&client_framer_
, false);
740 client_framer_
.ProcessPacket(*encrypted
);
743 TEST_P(QuicPacketCreatorTest
, UpdatePacketSequenceNumberLengthLeastAwaiting
) {
744 EXPECT_EQ(PACKET_1BYTE_SEQUENCE_NUMBER
,
745 creator_
.next_sequence_number_length());
747 size_t max_packets_per_fec_group
= 10;
748 creator_
.set_max_packets_per_fec_group(max_packets_per_fec_group
);
749 creator_
.set_sequence_number(64 - max_packets_per_fec_group
);
750 creator_
.UpdateSequenceNumberLength(2, 10000);
751 EXPECT_EQ(PACKET_1BYTE_SEQUENCE_NUMBER
,
752 creator_
.next_sequence_number_length());
754 creator_
.set_sequence_number(64 * 256 - max_packets_per_fec_group
);
755 creator_
.UpdateSequenceNumberLength(2, 10000);
756 EXPECT_EQ(PACKET_2BYTE_SEQUENCE_NUMBER
,
757 creator_
.next_sequence_number_length());
759 creator_
.set_sequence_number(64 * 256 * 256 - max_packets_per_fec_group
);
760 creator_
.UpdateSequenceNumberLength(2, 10000);
761 EXPECT_EQ(PACKET_4BYTE_SEQUENCE_NUMBER
,
762 creator_
.next_sequence_number_length());
764 creator_
.set_sequence_number(
765 GG_UINT64_C(64) * 256 * 256 * 256 * 256 - max_packets_per_fec_group
);
766 creator_
.UpdateSequenceNumberLength(2, 10000);
767 EXPECT_EQ(PACKET_6BYTE_SEQUENCE_NUMBER
,
768 creator_
.next_sequence_number_length());
771 TEST_P(QuicPacketCreatorTest
, UpdatePacketSequenceNumberLengthBandwidth
) {
772 EXPECT_EQ(PACKET_1BYTE_SEQUENCE_NUMBER
,
773 creator_
.next_sequence_number_length());
775 creator_
.UpdateSequenceNumberLength(1, 10000);
776 EXPECT_EQ(PACKET_1BYTE_SEQUENCE_NUMBER
,
777 creator_
.next_sequence_number_length());
779 creator_
.UpdateSequenceNumberLength(1, 10000 * 256);
780 EXPECT_EQ(PACKET_2BYTE_SEQUENCE_NUMBER
,
781 creator_
.next_sequence_number_length());
783 creator_
.UpdateSequenceNumberLength(1, 10000 * 256 * 256);
784 EXPECT_EQ(PACKET_4BYTE_SEQUENCE_NUMBER
,
785 creator_
.next_sequence_number_length());
787 creator_
.UpdateSequenceNumberLength(
788 1, GG_UINT64_C(1000) * 256 * 256 * 256 * 256);
789 EXPECT_EQ(PACKET_6BYTE_SEQUENCE_NUMBER
,
790 creator_
.next_sequence_number_length());
793 TEST_P(QuicPacketCreatorTest
, CreateStreamFrameWithNotifier
) {
794 // Ensure that if CreateStreamFrame does not consume any data (e.g. a FIN only
795 // frame) then any QuicAckNotifier that is passed in still gets attached to
797 scoped_refptr
<MockAckNotifierDelegate
> delegate(new MockAckNotifierDelegate
);
798 QuicAckNotifier
notifier(delegate
.get());
800 IOVector empty_iovector
;
802 size_t consumed_bytes
= creator_
.CreateStreamFrameWithNotifier(
803 1u, empty_iovector
, 0u, fin
, ¬ifier
, &frame
);
804 EXPECT_EQ(0u, consumed_bytes
);
805 EXPECT_EQ(¬ifier
, frame
.stream_frame
->notifier
);
806 delete frame
.stream_frame
;
809 TEST_P(QuicPacketCreatorTest
, SerializeFrame
) {
810 if (!GetParam().version_serialization
) {
811 creator_
.StopSendingVersion();
813 frames_
.push_back(QuicFrame(new QuicStreamFrame(0u, false, 0u, IOVector())));
814 SerializedPacket serialized
= creator_
.SerializeAllFrames(frames_
);
815 delete frames_
[0].stream_frame
;
817 QuicPacketHeader header
;
820 EXPECT_CALL(framer_visitor_
, OnPacket());
821 EXPECT_CALL(framer_visitor_
, OnUnauthenticatedPublicHeader(_
));
822 EXPECT_CALL(framer_visitor_
, OnUnauthenticatedHeader(_
));
823 EXPECT_CALL(framer_visitor_
, OnDecryptedPacket(_
));
824 EXPECT_CALL(framer_visitor_
, OnPacketHeader(_
)).WillOnce(
825 DoAll(SaveArg
<0>(&header
), Return(true)));
826 EXPECT_CALL(framer_visitor_
, OnStreamFrame(_
));
827 EXPECT_CALL(framer_visitor_
, OnPacketComplete());
829 ProcessPacket(serialized
.packet
);
830 EXPECT_EQ(GetParam().version_serialization
,
831 header
.public_header
.version_flag
);
832 delete serialized
.packet
;
835 TEST_P(QuicPacketCreatorTest
, CreateStreamFrameTooLarge
) {
836 if (!GetParam().version_serialization
) {
837 creator_
.StopSendingVersion();
839 // A string larger than fits into a frame.
840 size_t payload_length
;
841 creator_
.set_max_packet_length(GetPacketLengthForOneStream(
842 client_framer_
.version(),
843 QuicPacketCreatorPeer::SendVersionInPacket(&creator_
),
844 PACKET_1BYTE_SEQUENCE_NUMBER
, NOT_IN_FEC_GROUP
, &payload_length
));
846 const string
too_long_payload(payload_length
* 2, 'a');
847 size_t consumed
= creator_
.CreateStreamFrame(
848 1u, MakeIOVector(too_long_payload
), 0u, true, &frame
);
849 EXPECT_EQ(payload_length
, consumed
);
850 const string
payload(payload_length
, 'a');
851 CheckStreamFrame(frame
, 1u, payload
, 0u, false);
852 delete frame
.stream_frame
;
855 TEST_P(QuicPacketCreatorTest
, AddFrameAndSerialize
) {
856 if (!GetParam().version_serialization
) {
857 creator_
.StopSendingVersion();
859 const size_t max_plaintext_size
=
860 client_framer_
.GetMaxPlaintextSize(creator_
.max_packet_length());
861 EXPECT_FALSE(creator_
.HasPendingFrames());
862 EXPECT_EQ(max_plaintext_size
-
864 creator_
.connection_id_length(),
865 QuicPacketCreatorPeer::SendVersionInPacket(&creator_
),
866 PACKET_1BYTE_SEQUENCE_NUMBER
, NOT_IN_FEC_GROUP
),
867 creator_
.BytesFree());
869 // Add a variety of frame types and then a padding frame.
870 QuicAckFrame
ack_frame(MakeAckFrame(0u));
871 EXPECT_TRUE(creator_
.AddSavedFrame(QuicFrame(&ack_frame
)));
872 EXPECT_TRUE(creator_
.HasPendingFrames());
874 QuicCongestionFeedbackFrame congestion_feedback
;
875 congestion_feedback
.type
= kTCP
;
876 congestion_feedback
.tcp
.receive_window
= 0x4030;
877 EXPECT_TRUE(creator_
.AddSavedFrame(QuicFrame(&congestion_feedback
)));
878 EXPECT_TRUE(creator_
.HasPendingFrames());
881 size_t consumed
= creator_
.CreateStreamFrame(
882 1u, MakeIOVector("test"), 0u, false, &frame
);
883 EXPECT_EQ(4u, consumed
);
884 ASSERT_TRUE(frame
.stream_frame
);
885 EXPECT_TRUE(creator_
.AddSavedFrame(frame
));
886 EXPECT_TRUE(creator_
.HasPendingFrames());
888 QuicPaddingFrame padding_frame
;
889 EXPECT_TRUE(creator_
.AddSavedFrame(QuicFrame(&padding_frame
)));
890 EXPECT_TRUE(creator_
.HasPendingFrames());
891 EXPECT_EQ(0u, creator_
.BytesFree());
893 EXPECT_FALSE(creator_
.AddSavedFrame(QuicFrame(&ack_frame
)));
895 // Ensure the packet is successfully created.
896 SerializedPacket serialized
= creator_
.SerializePacket();
897 ASSERT_TRUE(serialized
.packet
);
898 delete serialized
.packet
;
899 ASSERT_TRUE(serialized
.retransmittable_frames
);
900 RetransmittableFrames
* retransmittable
= serialized
.retransmittable_frames
;
901 ASSERT_EQ(1u, retransmittable
->frames().size());
902 EXPECT_EQ(STREAM_FRAME
, retransmittable
->frames()[0].type
);
903 ASSERT_TRUE(retransmittable
->frames()[0].stream_frame
);
904 delete serialized
.retransmittable_frames
;
906 EXPECT_FALSE(creator_
.HasPendingFrames());
907 EXPECT_EQ(max_plaintext_size
-
909 creator_
.connection_id_length(),
910 QuicPacketCreatorPeer::SendVersionInPacket(&creator_
),
911 PACKET_1BYTE_SEQUENCE_NUMBER
,
913 creator_
.BytesFree());
916 TEST_P(QuicPacketCreatorTest
, SerializeTruncatedAckFrameWithLargePacketSize
) {
917 if (!GetParam().version_serialization
) {
918 creator_
.StopSendingVersion();
920 creator_
.set_max_packet_length(kMaxPacketSize
);
921 const size_t max_plaintext_size
=
922 client_framer_
.GetMaxPlaintextSize(creator_
.max_packet_length());
924 // Serialized length of ack frame with 2000 nack ranges should be limited by
925 // the number of nack ranges that can be fit in an ack frame.
926 QuicAckFrame ack_frame
= MakeAckFrameWithNackRanges(2000u, 0u);
927 size_t frame_len
= client_framer_
.GetSerializedFrameLength(
928 QuicFrame(&ack_frame
), creator_
.BytesFree(), true, true,
929 NOT_IN_FEC_GROUP
, PACKET_1BYTE_SEQUENCE_NUMBER
);
930 EXPECT_GT(creator_
.BytesFree(), frame_len
);
931 EXPECT_GT(max_plaintext_size
, creator_
.PacketSize());
933 // Add ack frame to creator.
934 EXPECT_TRUE(creator_
.AddSavedFrame(QuicFrame(&ack_frame
)));
935 EXPECT_TRUE(creator_
.HasPendingFrames());
936 EXPECT_GT(max_plaintext_size
, creator_
.PacketSize());
937 EXPECT_LT(0u, creator_
.BytesFree());
939 // Make sure that an additional stream frame can be added to the packet.
940 QuicFrame stream_frame
;
941 size_t consumed
= creator_
.CreateStreamFrame(
942 2u, MakeIOVector("test"), 0u, false, &stream_frame
);
943 EXPECT_EQ(4u, consumed
);
944 ASSERT_TRUE(stream_frame
.stream_frame
);
945 EXPECT_TRUE(creator_
.AddSavedFrame(stream_frame
));
946 EXPECT_TRUE(creator_
.HasPendingFrames());
948 // Ensure the packet is successfully created, and the packet size estimate
949 // matches the serialized packet length.
950 EXPECT_CALL(entropy_calculator_
,
951 EntropyHash(_
)).WillOnce(testing::Return(0));
952 size_t est_packet_size
= creator_
.PacketSize();
953 SerializedPacket serialized
= creator_
.SerializePacket();
954 ASSERT_TRUE(serialized
.packet
);
955 EXPECT_EQ(est_packet_size
, serialized
.packet
->length());
956 delete serialized
.retransmittable_frames
;
957 delete serialized
.packet
;
960 TEST_P(QuicPacketCreatorTest
, SerializeTruncatedAckFrameWithSmallPacketSize
) {
961 if (!GetParam().version_serialization
) {
962 creator_
.StopSendingVersion();
964 creator_
.set_max_packet_length(500u);
966 const size_t max_plaintext_size
=
967 client_framer_
.GetMaxPlaintextSize(creator_
.max_packet_length());
968 EXPECT_EQ(max_plaintext_size
- creator_
.PacketSize(), creator_
.BytesFree());
970 // Serialized length of ack frame with 2000 nack ranges should be limited by
972 QuicAckFrame ack_frame
= MakeAckFrameWithNackRanges(2000u, 0u);
973 size_t frame_len
= client_framer_
.GetSerializedFrameLength(
974 QuicFrame(&ack_frame
), creator_
.BytesFree(), true, true,
975 NOT_IN_FEC_GROUP
, PACKET_1BYTE_SEQUENCE_NUMBER
);
976 EXPECT_EQ(creator_
.BytesFree(), frame_len
);
978 // Add ack frame to creator.
979 EXPECT_TRUE(creator_
.AddSavedFrame(QuicFrame(&ack_frame
)));
980 EXPECT_TRUE(creator_
.HasPendingFrames());
981 EXPECT_EQ(max_plaintext_size
, creator_
.PacketSize());
982 EXPECT_EQ(0u, creator_
.BytesFree());
984 // Ensure the packet is successfully created, and the packet size estimate
985 // may not match the serialized packet length.
986 EXPECT_CALL(entropy_calculator_
,
987 EntropyHash(_
)).WillOnce(Return(0));
988 size_t est_packet_size
= creator_
.PacketSize();
989 SerializedPacket serialized
= creator_
.SerializePacket();
990 ASSERT_TRUE(serialized
.packet
);
991 EXPECT_GE(est_packet_size
, serialized
.packet
->length());
992 delete serialized
.packet
;
996 TEST_P(QuicPacketCreatorTest
, EntropyFlag
) {
997 frames_
.push_back(QuicFrame(new QuicStreamFrame(0u, false, 0u, IOVector())));
999 for (int i
= 0; i
< 2; ++i
) {
1000 for (int j
= 0; j
< 64; ++j
) {
1001 SerializedPacket serialized
= creator_
.SerializeAllFrames(frames_
);
1002 // Verify both BoolSource and hash algorithm.
1003 bool expected_rand_bool
=
1004 (mock_random_
.RandUint64() & (GG_UINT64_C(1) << j
)) != 0;
1005 bool observed_rand_bool
=
1006 (serialized
.entropy_hash
& (1 << ((j
+1) % 8))) != 0;
1007 uint8 rest_of_hash
= serialized
.entropy_hash
& ~(1 << ((j
+1) % 8));
1008 EXPECT_EQ(expected_rand_bool
, observed_rand_bool
);
1009 EXPECT_EQ(0, rest_of_hash
);
1010 delete serialized
.packet
;
1012 // After 64 calls, BoolSource will refresh the bucket - make sure it does.
1013 mock_random_
.ChangeValue();
1016 delete frames_
[0].stream_frame
;