We started redesigning GpuMemoryBuffer interface to handle multiple buffers [0].
[chromium-blink-merge.git] / net / quic / quic_packet_creator_test.cc
blobd9d13fd44e8e45315db13a39372115c12d1a1a2d
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 SerializedPacket serialized = creator_.SerializeAllFrames(frames_);
166 delete frames_[0].ack_frame;
167 delete frames_[1].stream_frame;
168 delete frames_[2].stream_frame;
171 InSequence s;
172 EXPECT_CALL(framer_visitor_, OnPacket());
173 EXPECT_CALL(framer_visitor_, OnUnauthenticatedPublicHeader(_));
174 EXPECT_CALL(framer_visitor_, OnUnauthenticatedHeader(_));
175 EXPECT_CALL(framer_visitor_, OnDecryptedPacket(_));
176 EXPECT_CALL(framer_visitor_, OnPacketHeader(_));
177 EXPECT_CALL(framer_visitor_, OnAckFrame(_));
178 EXPECT_CALL(framer_visitor_, OnStreamFrame(_));
179 EXPECT_CALL(framer_visitor_, OnStreamFrame(_));
180 EXPECT_CALL(framer_visitor_, OnPacketComplete());
182 ProcessPacket(serialized.packet);
183 delete serialized.packet;
186 TEST_P(QuicPacketCreatorTest, SerializeWithFEC) {
187 // Enable FEC protection, and send FEC packet every 6 packets.
188 EXPECT_TRUE(SwitchFecProtectionOn(6));
189 // Should return false since we do not have enough packets in the FEC group to
190 // trigger an FEC packet.
191 ASSERT_FALSE(creator_.ShouldSendFec(/*force_close=*/false));
193 frames_.push_back(QuicFrame(new QuicStreamFrame(0u, false, 0u, IOVector())));
194 SerializedPacket serialized = creator_.SerializeAllFrames(frames_);
195 delete frames_[0].stream_frame;
198 InSequence s;
199 EXPECT_CALL(framer_visitor_, OnPacket());
200 EXPECT_CALL(framer_visitor_, OnUnauthenticatedPublicHeader(_));
201 EXPECT_CALL(framer_visitor_, OnUnauthenticatedHeader(_));
202 EXPECT_CALL(framer_visitor_, OnDecryptedPacket(_));
203 EXPECT_CALL(framer_visitor_, OnPacketHeader(_));
204 EXPECT_CALL(framer_visitor_, OnFecProtectedPayload(_));
205 EXPECT_CALL(framer_visitor_, OnStreamFrame(_));
206 EXPECT_CALL(framer_visitor_, OnPacketComplete());
208 ProcessPacket(serialized.packet);
209 delete serialized.packet;
211 // Should return false since we do not have enough packets in the FEC group to
212 // trigger an FEC packet.
213 ASSERT_FALSE(creator_.ShouldSendFec(/*force_close=*/false));
214 // Should return true since there are packets in the FEC group.
215 ASSERT_TRUE(creator_.ShouldSendFec(/*force_close=*/true));
217 serialized = creator_.SerializeFec();
218 ASSERT_EQ(2u, serialized.sequence_number);
220 InSequence s;
221 EXPECT_CALL(framer_visitor_, OnPacket());
222 EXPECT_CALL(framer_visitor_, OnUnauthenticatedPublicHeader(_));
223 EXPECT_CALL(framer_visitor_, OnUnauthenticatedHeader(_));
224 EXPECT_CALL(framer_visitor_, OnDecryptedPacket(_));
225 EXPECT_CALL(framer_visitor_, OnPacketHeader(_));
226 EXPECT_CALL(framer_visitor_, OnFecData(_));
227 EXPECT_CALL(framer_visitor_, OnPacketComplete());
229 ProcessPacket(serialized.packet);
230 delete serialized.packet;
233 TEST_P(QuicPacketCreatorTest, SerializeChangingSequenceNumberLength) {
234 frames_.push_back(QuicFrame(new QuicAckFrame(MakeAckFrame(0u))));
235 creator_.AddSavedFrame(frames_[0]);
236 QuicPacketCreatorPeer::SetNextSequenceNumberLength(
237 &creator_, PACKET_4BYTE_SEQUENCE_NUMBER);
238 SerializedPacket serialized = creator_.SerializePacket();
239 // The sequence number length will not change mid-packet.
240 EXPECT_EQ(PACKET_1BYTE_SEQUENCE_NUMBER, serialized.sequence_number_length);
243 InSequence s;
244 EXPECT_CALL(framer_visitor_, OnPacket());
245 EXPECT_CALL(framer_visitor_, OnUnauthenticatedPublicHeader(_));
246 EXPECT_CALL(framer_visitor_, OnUnauthenticatedHeader(_));
247 EXPECT_CALL(framer_visitor_, OnDecryptedPacket(_));
248 EXPECT_CALL(framer_visitor_, OnPacketHeader(_));
249 EXPECT_CALL(framer_visitor_, OnAckFrame(_));
250 EXPECT_CALL(framer_visitor_, OnPacketComplete());
252 ProcessPacket(serialized.packet);
253 delete serialized.packet;
255 creator_.AddSavedFrame(frames_[0]);
256 serialized = creator_.SerializePacket();
257 // Now the actual sequence number length should have changed.
258 EXPECT_EQ(PACKET_4BYTE_SEQUENCE_NUMBER, serialized.sequence_number_length);
259 delete frames_[0].ack_frame;
262 InSequence s;
263 EXPECT_CALL(framer_visitor_, OnPacket());
264 EXPECT_CALL(framer_visitor_, OnUnauthenticatedPublicHeader(_));
265 EXPECT_CALL(framer_visitor_, OnUnauthenticatedHeader(_));
266 EXPECT_CALL(framer_visitor_, OnDecryptedPacket(_));
267 EXPECT_CALL(framer_visitor_, OnPacketHeader(_));
268 EXPECT_CALL(framer_visitor_, OnAckFrame(_));
269 EXPECT_CALL(framer_visitor_, OnPacketComplete());
271 ProcessPacket(serialized.packet);
272 delete serialized.packet;
275 TEST_P(QuicPacketCreatorTest, ChangeSequenceNumberLengthMidPacket) {
276 // Changing the sequence number length with queued frames in the creator
277 // should hold the change until after any currently queued frames are
278 // serialized.
280 // Packet 1.
281 // Queue a frame in the creator.
282 EXPECT_FALSE(creator_.HasPendingFrames());
283 QuicFrame ack_frame = QuicFrame(new QuicAckFrame(MakeAckFrame(0u)));
284 creator_.AddSavedFrame(ack_frame);
286 // Now change sequence number length.
287 QuicPacketCreatorPeer::SetNextSequenceNumberLength(
288 &creator_, PACKET_4BYTE_SEQUENCE_NUMBER);
290 // Add a STOP_WAITING frame since it contains a packet sequence number,
291 // whose length should be 1.
292 QuicStopWaitingFrame stop_waiting_frame;
293 EXPECT_TRUE(creator_.AddSavedFrame(QuicFrame(&stop_waiting_frame)));
294 EXPECT_TRUE(creator_.HasPendingFrames());
296 // Ensure the packet is successfully created.
297 SerializedPacket serialized = creator_.SerializePacket();
298 ASSERT_TRUE(serialized.packet);
299 EXPECT_EQ(PACKET_1BYTE_SEQUENCE_NUMBER, serialized.sequence_number_length);
301 // Verify that header in transmitted packet has 1 byte sequence length.
302 QuicPacketHeader header;
304 InSequence s;
305 EXPECT_CALL(framer_visitor_, OnPacket());
306 EXPECT_CALL(framer_visitor_, OnUnauthenticatedPublicHeader(_));
307 EXPECT_CALL(framer_visitor_, OnUnauthenticatedHeader(_));
308 EXPECT_CALL(framer_visitor_, OnDecryptedPacket(_));
309 EXPECT_CALL(framer_visitor_, OnPacketHeader(_)).WillOnce(
310 DoAll(SaveArg<0>(&header), Return(true)));
311 EXPECT_CALL(framer_visitor_, OnAckFrame(_));
312 EXPECT_CALL(framer_visitor_, OnStopWaitingFrame(_));
313 EXPECT_CALL(framer_visitor_, OnPacketComplete());
315 ProcessPacket(serialized.packet);
316 EXPECT_EQ(PACKET_1BYTE_SEQUENCE_NUMBER,
317 header.public_header.sequence_number_length);
318 delete serialized.packet;
320 // Packet 2.
321 EXPECT_FALSE(creator_.HasPendingFrames());
322 // Generate Packet 2 with one frame -- sequence number length should now
323 // change to 4 bytes.
324 EXPECT_TRUE(creator_.AddSavedFrame(QuicFrame(&stop_waiting_frame)));
325 EXPECT_TRUE(creator_.HasPendingFrames());
327 // Ensure the packet is successfully created.
328 serialized = creator_.SerializePacket();
329 ASSERT_TRUE(serialized.packet);
330 EXPECT_EQ(PACKET_4BYTE_SEQUENCE_NUMBER, serialized.sequence_number_length);
332 // Verify that header in transmitted packet has 4 byte sequence length.
334 InSequence s;
335 EXPECT_CALL(framer_visitor_, OnPacket());
336 EXPECT_CALL(framer_visitor_, OnUnauthenticatedPublicHeader(_));
337 EXPECT_CALL(framer_visitor_, OnUnauthenticatedHeader(_));
338 EXPECT_CALL(framer_visitor_, OnDecryptedPacket(_));
339 EXPECT_CALL(framer_visitor_, OnPacketHeader(_)).WillOnce(
340 DoAll(SaveArg<0>(&header), Return(true)));
341 EXPECT_CALL(framer_visitor_, OnStopWaitingFrame(_));
342 EXPECT_CALL(framer_visitor_, OnPacketComplete());
344 ProcessPacket(serialized.packet);
345 EXPECT_EQ(PACKET_4BYTE_SEQUENCE_NUMBER,
346 header.public_header.sequence_number_length);
348 delete serialized.packet;
349 delete ack_frame.ack_frame;
352 TEST_P(QuicPacketCreatorTest, SerializeWithFECChangingSequenceNumberLength) {
353 // Test goal is to test the following sequence (P1 => generate Packet 1):
354 // P1 <change seq num length> P2 FEC,
355 // and we expect that sequence number length should not change until the end
356 // of the open FEC group.
358 // Enable FEC protection, and send FEC packet every 6 packets.
359 EXPECT_TRUE(SwitchFecProtectionOn(6));
360 // Should return false since we do not have enough packets in the FEC group to
361 // trigger an FEC packet.
362 ASSERT_FALSE(creator_.ShouldSendFec(/*force_close=*/false));
363 frames_.push_back(QuicFrame(new QuicAckFrame(MakeAckFrame(0u))));
365 // Generate Packet 1.
366 creator_.AddSavedFrame(frames_[0]);
367 // Change the sequence number length mid-FEC group and it should not change.
368 QuicPacketCreatorPeer::SetNextSequenceNumberLength(
369 &creator_, PACKET_4BYTE_SEQUENCE_NUMBER);
370 SerializedPacket serialized = creator_.SerializePacket();
371 EXPECT_EQ(PACKET_1BYTE_SEQUENCE_NUMBER, serialized.sequence_number_length);
374 InSequence s;
375 EXPECT_CALL(framer_visitor_, OnPacket());
376 EXPECT_CALL(framer_visitor_, OnUnauthenticatedPublicHeader(_));
377 EXPECT_CALL(framer_visitor_, OnUnauthenticatedHeader(_));
378 EXPECT_CALL(framer_visitor_, OnDecryptedPacket(_));
379 EXPECT_CALL(framer_visitor_, OnPacketHeader(_));
380 EXPECT_CALL(framer_visitor_, OnFecProtectedPayload(_));
381 EXPECT_CALL(framer_visitor_, OnAckFrame(_));
382 EXPECT_CALL(framer_visitor_, OnPacketComplete());
384 ProcessPacket(serialized.packet);
385 delete serialized.packet;
387 // Generate Packet 2.
388 creator_.AddSavedFrame(frames_[0]);
389 serialized = creator_.SerializePacket();
390 EXPECT_EQ(PACKET_1BYTE_SEQUENCE_NUMBER, serialized.sequence_number_length);
393 InSequence s;
394 EXPECT_CALL(framer_visitor_, OnPacket());
395 EXPECT_CALL(framer_visitor_, OnUnauthenticatedPublicHeader(_));
396 EXPECT_CALL(framer_visitor_, OnUnauthenticatedHeader(_));
397 EXPECT_CALL(framer_visitor_, OnDecryptedPacket(_));
398 EXPECT_CALL(framer_visitor_, OnPacketHeader(_));
399 EXPECT_CALL(framer_visitor_, OnFecProtectedPayload(_));
400 EXPECT_CALL(framer_visitor_, OnAckFrame(_));
401 EXPECT_CALL(framer_visitor_, OnPacketComplete());
403 ProcessPacket(serialized.packet);
404 delete serialized.packet;
406 // Should return false since we do not have enough packets in the FEC group to
407 // trigger an FEC packet.
408 ASSERT_FALSE(creator_.ShouldSendFec(/*force_close=*/false));
409 // Should return true since there are packets in the FEC group.
410 ASSERT_TRUE(creator_.ShouldSendFec(/*force_close=*/true));
412 // Force generation of FEC packet.
413 serialized = creator_.SerializeFec();
414 EXPECT_EQ(PACKET_1BYTE_SEQUENCE_NUMBER, serialized.sequence_number_length);
415 ASSERT_EQ(3u, serialized.sequence_number);
418 InSequence s;
419 EXPECT_CALL(framer_visitor_, OnPacket());
420 EXPECT_CALL(framer_visitor_, OnUnauthenticatedPublicHeader(_));
421 EXPECT_CALL(framer_visitor_, OnUnauthenticatedHeader(_));
422 EXPECT_CALL(framer_visitor_, OnDecryptedPacket(_));
423 EXPECT_CALL(framer_visitor_, OnPacketHeader(_));
424 EXPECT_CALL(framer_visitor_, OnFecData(_));
425 EXPECT_CALL(framer_visitor_, OnPacketComplete());
427 ProcessPacket(serialized.packet);
428 delete serialized.packet;
430 // Ensure the next FEC group starts using the new sequence number length.
431 serialized = creator_.SerializeAllFrames(frames_);
432 EXPECT_EQ(PACKET_4BYTE_SEQUENCE_NUMBER, serialized.sequence_number_length);
433 delete frames_[0].ack_frame;
434 delete serialized.packet;
437 TEST_P(QuicPacketCreatorTest, ReserializeFramesWithSequenceNumberLength) {
438 // If the original packet sequence number length, the current sequence number
439 // length, and the configured send sequence number length are different, the
440 // retransmit must sent with the original length and the others do not change.
441 QuicPacketCreatorPeer::SetNextSequenceNumberLength(
442 &creator_, PACKET_4BYTE_SEQUENCE_NUMBER);
443 QuicPacketCreatorPeer::SetSequenceNumberLength(&creator_,
444 PACKET_2BYTE_SEQUENCE_NUMBER);
445 QuicStreamFrame* stream_frame =
446 new QuicStreamFrame(kCryptoStreamId, /*fin=*/false, 0u, IOVector());
447 RetransmittableFrames frames(ENCRYPTION_NONE);
448 frames.AddStreamFrame(stream_frame);
449 SerializedPacket serialized =
450 creator_.ReserializeAllFrames(frames, PACKET_1BYTE_SEQUENCE_NUMBER);
451 EXPECT_EQ(PACKET_4BYTE_SEQUENCE_NUMBER,
452 QuicPacketCreatorPeer::NextSequenceNumberLength(&creator_));
453 EXPECT_EQ(PACKET_2BYTE_SEQUENCE_NUMBER,
454 QuicPacketCreatorPeer::GetSequenceNumberLength(&creator_));
455 EXPECT_EQ(PACKET_1BYTE_SEQUENCE_NUMBER, serialized.sequence_number_length);
458 InSequence s;
459 EXPECT_CALL(framer_visitor_, OnPacket());
460 EXPECT_CALL(framer_visitor_, OnUnauthenticatedPublicHeader(_));
461 EXPECT_CALL(framer_visitor_, OnUnauthenticatedHeader(_));
462 EXPECT_CALL(framer_visitor_, OnDecryptedPacket(_));
463 EXPECT_CALL(framer_visitor_, OnPacketHeader(_));
464 EXPECT_CALL(framer_visitor_, OnStreamFrame(_));
465 EXPECT_CALL(framer_visitor_, OnPacketComplete());
467 ProcessPacket(serialized.packet);
468 delete serialized.packet;
471 TEST_P(QuicPacketCreatorTest, ReserializeFramesWithPadding) {
472 QuicStreamFrame* stream_frame =
473 new QuicStreamFrame(kCryptoStreamId, /*fin=*/false, /*offset=*/0,
474 MakeIOVector("fake handshake message data"));
475 RetransmittableFrames frames(ENCRYPTION_NONE);
476 frames.AddStreamFrame(stream_frame);
477 SerializedPacket serialized = creator_.ReserializeAllFrames(
478 frames, QuicPacketCreatorPeer::NextSequenceNumberLength(&creator_));
479 EXPECT_EQ(kDefaultMaxPacketSize, serialized.packet->length());
480 delete serialized.packet;
483 TEST_P(QuicPacketCreatorTest, ReserializeFramesWithFullPacketAndPadding) {
484 const size_t overhead = GetPacketHeaderOverhead(NOT_IN_FEC_GROUP)
485 + GetEncryptionOverhead() + GetStreamFrameOverhead(NOT_IN_FEC_GROUP);
486 size_t capacity = kDefaultMaxPacketSize - overhead;
487 for (int delta = -5; delta <= 0; ++delta) {
488 string data(capacity + delta, 'A');
489 size_t bytes_free = 0 - delta;
491 QuicStreamFrame* stream_frame = new QuicStreamFrame(
492 kCryptoStreamId, /*fin=*/false, kOffset, MakeIOVector(data));
493 RetransmittableFrames frames(ENCRYPTION_NONE);
494 frames.AddStreamFrame(stream_frame);
495 SerializedPacket serialized = creator_.ReserializeAllFrames(
496 frames, QuicPacketCreatorPeer::NextSequenceNumberLength(&creator_));
498 // If there is not enough space in the packet to fit a padding frame
499 // (1 byte) and to expand the stream frame (another 2 bytes) the packet
500 // will not be padded.
501 if (bytes_free < 3) {
502 EXPECT_EQ(kDefaultMaxPacketSize - bytes_free,
503 serialized.packet->length());
504 } else {
505 EXPECT_EQ(kDefaultMaxPacketSize, serialized.packet->length());
508 delete serialized.packet;
509 frames_.clear();
513 TEST_P(QuicPacketCreatorTest, SerializeConnectionClose) {
514 QuicConnectionCloseFrame frame;
515 frame.error_code = QUIC_NO_ERROR;
516 frame.error_details = "error";
518 QuicFrames frames;
519 frames.push_back(QuicFrame(&frame));
520 SerializedPacket serialized = creator_.SerializeAllFrames(frames);
521 ASSERT_EQ(1u, serialized.sequence_number);
522 ASSERT_EQ(1u, creator_.sequence_number());
524 InSequence s;
525 EXPECT_CALL(framer_visitor_, OnPacket());
526 EXPECT_CALL(framer_visitor_, OnUnauthenticatedPublicHeader(_));
527 EXPECT_CALL(framer_visitor_, OnUnauthenticatedHeader(_));
528 EXPECT_CALL(framer_visitor_, OnDecryptedPacket(_));
529 EXPECT_CALL(framer_visitor_, OnPacketHeader(_));
530 EXPECT_CALL(framer_visitor_, OnConnectionCloseFrame(_));
531 EXPECT_CALL(framer_visitor_, OnPacketComplete());
533 ProcessPacket(serialized.packet);
534 delete serialized.packet;
537 TEST_P(QuicPacketCreatorTest, SwitchFecOnOffWithNoGroup) {
538 // Enable FEC protection.
539 creator_.set_max_packets_per_fec_group(6);
540 EXPECT_TRUE(creator_.IsFecEnabled());
541 EXPECT_FALSE(creator_.IsFecProtected());
543 // Turn on FEC protection.
544 creator_.StartFecProtectingPackets();
545 EXPECT_TRUE(creator_.IsFecProtected());
546 // We have no packets in the FEC group, so no FEC packet can be created.
547 EXPECT_FALSE(creator_.ShouldSendFec(/*force_close=*/true));
548 // Since no packets are in FEC group yet, we should be able to turn FEC
549 // off with no trouble.
550 creator_.StopFecProtectingPackets();
551 EXPECT_FALSE(creator_.IsFecProtected());
554 TEST_P(QuicPacketCreatorTest, SwitchFecOnOffWithGroupInProgress) {
555 // Enable FEC protection, and send FEC packet every 6 packets.
556 EXPECT_TRUE(SwitchFecProtectionOn(6));
557 frames_.push_back(QuicFrame(new QuicStreamFrame(0u, false, 0u, IOVector())));
558 SerializedPacket serialized = creator_.SerializeAllFrames(frames_);
559 delete frames_[0].stream_frame;
560 delete serialized.packet;
562 EXPECT_TRUE(creator_.IsFecProtected());
563 // We do not have enough packets in the FEC group to trigger an FEC packet.
564 EXPECT_FALSE(creator_.ShouldSendFec(/*force_close=*/false));
565 // Should return true since there are packets in the FEC group.
566 EXPECT_TRUE(creator_.ShouldSendFec(/*force_close=*/true));
568 // Switching FEC off should not change creator state, since there is an
569 // FEC packet under construction.
570 EXPECT_DFATAL(creator_.StopFecProtectingPackets(),
571 "Cannot stop FEC protection with open FEC group.");
572 EXPECT_TRUE(creator_.IsFecProtected());
573 // Confirm that FEC packet is still under construction.
574 EXPECT_TRUE(creator_.ShouldSendFec(/*force_close=*/true));
576 serialized = creator_.SerializeFec();
577 delete serialized.packet;
579 // Switching FEC on/off should work now.
580 creator_.StopFecProtectingPackets();
581 EXPECT_FALSE(creator_.IsFecProtected());
582 creator_.StartFecProtectingPackets();
583 EXPECT_TRUE(creator_.IsFecProtected());
586 TEST_P(QuicPacketCreatorTest, SwitchFecOnWithStreamFrameQueued) {
587 // Add a stream frame to the creator.
588 QuicFrame frame;
589 size_t consumed = creator_.CreateStreamFrame(
590 1u, MakeIOVector("test"), 0u, false, &frame);
591 EXPECT_EQ(4u, consumed);
592 ASSERT_TRUE(frame.stream_frame);
593 EXPECT_TRUE(creator_.AddSavedFrame(frame));
594 EXPECT_TRUE(creator_.HasPendingFrames());
596 // Enable FEC protection, and send FEC packet every 6 packets.
597 creator_.set_max_packets_per_fec_group(6);
598 EXPECT_TRUE(creator_.IsFecEnabled());
599 EXPECT_DFATAL(creator_.StartFecProtectingPackets(),
600 "Cannot start FEC protection with pending frames.");
601 EXPECT_FALSE(creator_.IsFecProtected());
603 // Serialize packet for transmission.
604 SerializedPacket serialized = creator_.SerializePacket();
605 delete serialized.packet;
606 delete serialized.retransmittable_frames;
607 EXPECT_FALSE(creator_.HasPendingFrames());
609 // Since all pending frames have been serialized, turning FEC on should work.
610 creator_.StartFecProtectingPackets();
611 EXPECT_TRUE(creator_.IsFecProtected());
614 TEST_P(QuicPacketCreatorTest, CreateStreamFrame) {
615 QuicFrame frame;
616 size_t consumed = creator_.CreateStreamFrame(1u, MakeIOVector("test"), 0u,
617 false, &frame);
618 EXPECT_EQ(4u, consumed);
619 CheckStreamFrame(frame, 1u, "test", 0u, false);
620 delete frame.stream_frame;
623 TEST_P(QuicPacketCreatorTest, CreateStreamFrameFin) {
624 QuicFrame frame;
625 size_t consumed = creator_.CreateStreamFrame(1u, MakeIOVector("test"), 10u,
626 true, &frame);
627 EXPECT_EQ(4u, consumed);
628 CheckStreamFrame(frame, 1u, "test", 10u, true);
629 delete frame.stream_frame;
632 TEST_P(QuicPacketCreatorTest, CreateStreamFrameFinOnly) {
633 QuicFrame frame;
634 size_t consumed = creator_.CreateStreamFrame(1u, IOVector(), 0u, true,
635 &frame);
636 EXPECT_EQ(0u, consumed);
637 CheckStreamFrame(frame, 1u, string(), 0u, true);
638 delete frame.stream_frame;
641 TEST_P(QuicPacketCreatorTest, CreateAllFreeBytesForStreamFrames) {
642 const size_t overhead = GetPacketHeaderOverhead(NOT_IN_FEC_GROUP)
643 + GetEncryptionOverhead();
644 for (size_t i = overhead; i < overhead + 100; ++i) {
645 creator_.SetMaxPacketLength(i);
646 const bool should_have_room = i > overhead + GetStreamFrameOverhead(
647 NOT_IN_FEC_GROUP);
648 ASSERT_EQ(should_have_room, creator_.HasRoomForStreamFrame(
649 kClientDataStreamId1, kOffset));
650 if (should_have_room) {
651 QuicFrame frame;
652 size_t bytes_consumed = creator_.CreateStreamFrame(
653 kClientDataStreamId1, MakeIOVector("testdata"), kOffset, false,
654 &frame);
655 EXPECT_LT(0u, bytes_consumed);
656 ASSERT_TRUE(creator_.AddSavedFrame(frame));
657 SerializedPacket serialized_packet = creator_.SerializePacket();
658 ASSERT_TRUE(serialized_packet.packet);
659 delete serialized_packet.packet;
660 delete serialized_packet.retransmittable_frames;
665 TEST_P(QuicPacketCreatorTest, StreamFrameConsumption) {
666 // Compute the total overhead for a single frame in packet.
667 const size_t overhead = GetPacketHeaderOverhead(NOT_IN_FEC_GROUP)
668 + GetEncryptionOverhead() + GetStreamFrameOverhead(NOT_IN_FEC_GROUP);
669 size_t capacity = kDefaultMaxPacketSize - overhead;
670 // Now, test various sizes around this size.
671 for (int delta = -5; delta <= 5; ++delta) {
672 string data(capacity + delta, 'A');
673 size_t bytes_free = delta > 0 ? 0 : 0 - delta;
674 QuicFrame frame;
675 size_t bytes_consumed = creator_.CreateStreamFrame(
676 kClientDataStreamId1, MakeIOVector(data), kOffset, false, &frame);
677 EXPECT_EQ(capacity - bytes_free, bytes_consumed);
679 ASSERT_TRUE(creator_.AddSavedFrame(frame));
680 // BytesFree() returns bytes available for the next frame, which will
681 // be two bytes smaller since the stream frame would need to be grown.
682 EXPECT_EQ(2u, creator_.ExpansionOnNewFrame());
683 size_t expected_bytes_free = bytes_free < 3 ? 0 : bytes_free - 2;
684 EXPECT_EQ(expected_bytes_free, creator_.BytesFree()) << "delta: " << delta;
685 SerializedPacket serialized_packet = creator_.SerializePacket();
686 ASSERT_TRUE(serialized_packet.packet);
687 delete serialized_packet.packet;
688 delete serialized_packet.retransmittable_frames;
692 TEST_P(QuicPacketCreatorTest, StreamFrameConsumptionWithFec) {
693 // Enable FEC protection, and send FEC packet every 6 packets.
694 EXPECT_TRUE(SwitchFecProtectionOn(6));
695 // Compute the total overhead for a single frame in packet.
696 const size_t overhead = GetPacketHeaderOverhead(IN_FEC_GROUP)
697 + GetEncryptionOverhead() + GetStreamFrameOverhead(IN_FEC_GROUP);
698 size_t capacity = kDefaultMaxPacketSize - overhead;
699 // Now, test various sizes around this size.
700 for (int delta = -5; delta <= 5; ++delta) {
701 string data(capacity + delta, 'A');
702 size_t bytes_free = delta > 0 ? 0 : 0 - delta;
703 QuicFrame frame;
704 size_t bytes_consumed = creator_.CreateStreamFrame(
705 kClientDataStreamId1, MakeIOVector(data), kOffset, false, &frame);
706 EXPECT_EQ(capacity - bytes_free, bytes_consumed);
708 ASSERT_TRUE(creator_.AddSavedFrame(frame));
709 // BytesFree() returns bytes available for the next frame. Since stream
710 // frame does not grow for FEC protected packets, this should be the same
711 // as bytes_free (bound by 0).
712 EXPECT_EQ(0u, creator_.ExpansionOnNewFrame());
713 size_t expected_bytes_free = bytes_free > 0 ? bytes_free : 0;
714 EXPECT_EQ(expected_bytes_free, creator_.BytesFree()) << "delta: " << delta;
715 SerializedPacket serialized_packet = creator_.SerializePacket();
716 ASSERT_TRUE(serialized_packet.packet);
717 delete serialized_packet.packet;
718 delete serialized_packet.retransmittable_frames;
722 TEST_P(QuicPacketCreatorTest, CryptoStreamFramePacketPadding) {
723 // Compute the total overhead for a single frame in packet.
724 const size_t overhead = GetPacketHeaderOverhead(NOT_IN_FEC_GROUP)
725 + GetEncryptionOverhead() + GetStreamFrameOverhead(NOT_IN_FEC_GROUP);
726 ASSERT_GT(kMaxPacketSize, overhead);
727 size_t capacity = kDefaultMaxPacketSize - overhead;
728 // Now, test various sizes around this size.
729 for (int delta = -5; delta <= 5; ++delta) {
730 string data(capacity + delta, 'A');
731 size_t bytes_free = delta > 0 ? 0 : 0 - delta;
733 QuicFrame frame;
734 size_t bytes_consumed = creator_.CreateStreamFrame(
735 kCryptoStreamId, MakeIOVector(data), kOffset, false, &frame);
736 EXPECT_LT(0u, bytes_consumed);
737 ASSERT_TRUE(creator_.AddSavedFrame(frame));
738 SerializedPacket serialized_packet = creator_.SerializePacket();
739 ASSERT_TRUE(serialized_packet.packet);
740 // If there is not enough space in the packet to fit a padding frame
741 // (1 byte) and to expand the stream frame (another 2 bytes) the packet
742 // will not be padded.
743 if (bytes_free < 3) {
744 EXPECT_EQ(kDefaultMaxPacketSize - bytes_free,
745 serialized_packet.packet->length());
746 } else {
747 EXPECT_EQ(kDefaultMaxPacketSize, serialized_packet.packet->length());
749 delete serialized_packet.packet;
750 delete serialized_packet.retransmittable_frames;
754 TEST_P(QuicPacketCreatorTest, NonCryptoStreamFramePacketNonPadding) {
755 // Compute the total overhead for a single frame in packet.
756 const size_t overhead = GetPacketHeaderOverhead(NOT_IN_FEC_GROUP)
757 + GetEncryptionOverhead() + GetStreamFrameOverhead(NOT_IN_FEC_GROUP);
758 ASSERT_GT(kDefaultMaxPacketSize, overhead);
759 size_t capacity = kDefaultMaxPacketSize - overhead;
760 // Now, test various sizes around this size.
761 for (int delta = -5; delta <= 5; ++delta) {
762 string data(capacity + delta, 'A');
763 size_t bytes_free = delta > 0 ? 0 : 0 - delta;
765 QuicFrame frame;
766 size_t bytes_consumed = creator_.CreateStreamFrame(
767 kClientDataStreamId1, MakeIOVector(data), kOffset, false, &frame);
768 EXPECT_LT(0u, bytes_consumed);
769 ASSERT_TRUE(creator_.AddSavedFrame(frame));
770 SerializedPacket serialized_packet = creator_.SerializePacket();
771 ASSERT_TRUE(serialized_packet.packet);
772 if (bytes_free > 0) {
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, SerializeVersionNegotiationPacket) {
784 QuicFramerPeer::SetPerspective(&client_framer_, Perspective::IS_SERVER);
785 QuicVersionVector versions;
786 versions.push_back(test::QuicVersionMax());
787 scoped_ptr<QuicEncryptedPacket> encrypted(
788 creator_.SerializeVersionNegotiationPacket(versions));
791 InSequence s;
792 EXPECT_CALL(framer_visitor_, OnPacket());
793 EXPECT_CALL(framer_visitor_, OnUnauthenticatedPublicHeader(_));
794 EXPECT_CALL(framer_visitor_, OnVersionNegotiationPacket(_));
796 QuicFramerPeer::SetPerspective(&client_framer_, Perspective::IS_CLIENT);
797 client_framer_.ProcessPacket(*encrypted);
800 TEST_P(QuicPacketCreatorTest, UpdatePacketSequenceNumberLengthLeastAwaiting) {
801 EXPECT_EQ(PACKET_1BYTE_SEQUENCE_NUMBER,
802 QuicPacketCreatorPeer::NextSequenceNumberLength(&creator_));
804 size_t max_packets_per_fec_group = 10;
805 creator_.set_max_packets_per_fec_group(max_packets_per_fec_group);
806 QuicPacketCreatorPeer::SetSequenceNumber(&creator_,
807 64 - max_packets_per_fec_group);
808 creator_.UpdateSequenceNumberLength(2, 10000 / kDefaultMaxPacketSize);
809 EXPECT_EQ(PACKET_1BYTE_SEQUENCE_NUMBER,
810 QuicPacketCreatorPeer::NextSequenceNumberLength(&creator_));
812 QuicPacketCreatorPeer::SetSequenceNumber(
813 &creator_, 64 * 256 - max_packets_per_fec_group);
814 creator_.UpdateSequenceNumberLength(2, 10000 / kDefaultMaxPacketSize);
815 EXPECT_EQ(PACKET_2BYTE_SEQUENCE_NUMBER,
816 QuicPacketCreatorPeer::NextSequenceNumberLength(&creator_));
818 QuicPacketCreatorPeer::SetSequenceNumber(
819 &creator_, 64 * 256 * 256 - max_packets_per_fec_group);
820 creator_.UpdateSequenceNumberLength(2, 10000 / kDefaultMaxPacketSize);
821 EXPECT_EQ(PACKET_4BYTE_SEQUENCE_NUMBER,
822 QuicPacketCreatorPeer::NextSequenceNumberLength(&creator_));
824 QuicPacketCreatorPeer::SetSequenceNumber(
825 &creator_,
826 GG_UINT64_C(64) * 256 * 256 * 256 * 256 - max_packets_per_fec_group);
827 creator_.UpdateSequenceNumberLength(2, 10000 / kDefaultMaxPacketSize);
828 EXPECT_EQ(PACKET_6BYTE_SEQUENCE_NUMBER,
829 QuicPacketCreatorPeer::NextSequenceNumberLength(&creator_));
832 TEST_P(QuicPacketCreatorTest, UpdatePacketSequenceNumberLengthBandwidth) {
833 EXPECT_EQ(PACKET_1BYTE_SEQUENCE_NUMBER,
834 QuicPacketCreatorPeer::NextSequenceNumberLength(&creator_));
836 creator_.UpdateSequenceNumberLength(1, 10000 / kDefaultMaxPacketSize);
837 EXPECT_EQ(PACKET_1BYTE_SEQUENCE_NUMBER,
838 QuicPacketCreatorPeer::NextSequenceNumberLength(&creator_));
840 creator_.UpdateSequenceNumberLength(1, 10000 * 256 / kDefaultMaxPacketSize);
841 EXPECT_EQ(PACKET_2BYTE_SEQUENCE_NUMBER,
842 QuicPacketCreatorPeer::NextSequenceNumberLength(&creator_));
844 creator_.UpdateSequenceNumberLength(
845 1, 10000 * 256 * 256 / kDefaultMaxPacketSize);
846 EXPECT_EQ(PACKET_4BYTE_SEQUENCE_NUMBER,
847 QuicPacketCreatorPeer::NextSequenceNumberLength(&creator_));
849 creator_.UpdateSequenceNumberLength(
850 1, GG_UINT64_C(1000) * 256 * 256 * 256 * 256 / kDefaultMaxPacketSize);
851 EXPECT_EQ(PACKET_6BYTE_SEQUENCE_NUMBER,
852 QuicPacketCreatorPeer::NextSequenceNumberLength(&creator_));
855 TEST_P(QuicPacketCreatorTest, SerializeFrame) {
856 if (!GetParam().version_serialization) {
857 creator_.StopSendingVersion();
859 frames_.push_back(QuicFrame(new QuicStreamFrame(0u, false, 0u, IOVector())));
860 SerializedPacket serialized = creator_.SerializeAllFrames(frames_);
861 delete frames_[0].stream_frame;
863 QuicPacketHeader header;
865 InSequence s;
866 EXPECT_CALL(framer_visitor_, OnPacket());
867 EXPECT_CALL(framer_visitor_, OnUnauthenticatedPublicHeader(_));
868 EXPECT_CALL(framer_visitor_, OnUnauthenticatedHeader(_));
869 EXPECT_CALL(framer_visitor_, OnDecryptedPacket(_));
870 EXPECT_CALL(framer_visitor_, OnPacketHeader(_)).WillOnce(
871 DoAll(SaveArg<0>(&header), Return(true)));
872 EXPECT_CALL(framer_visitor_, OnStreamFrame(_));
873 EXPECT_CALL(framer_visitor_, OnPacketComplete());
875 ProcessPacket(serialized.packet);
876 EXPECT_EQ(GetParam().version_serialization,
877 header.public_header.version_flag);
878 delete serialized.packet;
881 TEST_P(QuicPacketCreatorTest, CreateStreamFrameTooLarge) {
882 if (!GetParam().version_serialization) {
883 creator_.StopSendingVersion();
885 // A string larger than fits into a frame.
886 size_t payload_length;
887 creator_.SetMaxPacketLength(GetPacketLengthForOneStream(
888 client_framer_.version(),
889 QuicPacketCreatorPeer::SendVersionInPacket(&creator_),
890 creator_.connection_id_length(),
891 PACKET_1BYTE_SEQUENCE_NUMBER, NOT_IN_FEC_GROUP, &payload_length));
892 QuicFrame frame;
893 const string too_long_payload(payload_length * 2, 'a');
894 size_t consumed = creator_.CreateStreamFrame(
895 1u, MakeIOVector(too_long_payload), 0u, true, &frame);
896 EXPECT_EQ(payload_length, consumed);
897 const string payload(payload_length, 'a');
898 CheckStreamFrame(frame, 1u, payload, 0u, false);
899 delete frame.stream_frame;
902 TEST_P(QuicPacketCreatorTest, AddFrameAndSerialize) {
903 if (!GetParam().version_serialization) {
904 creator_.StopSendingVersion();
906 const size_t max_plaintext_size =
907 client_framer_.GetMaxPlaintextSize(creator_.max_packet_length());
908 EXPECT_FALSE(creator_.HasPendingFrames());
909 EXPECT_EQ(max_plaintext_size -
910 GetPacketHeaderSize(
911 creator_.connection_id_length(),
912 QuicPacketCreatorPeer::SendVersionInPacket(&creator_),
913 PACKET_1BYTE_SEQUENCE_NUMBER, NOT_IN_FEC_GROUP),
914 creator_.BytesFree());
916 // Add a variety of frame types and then a padding frame.
917 QuicAckFrame ack_frame(MakeAckFrame(0u));
918 EXPECT_TRUE(creator_.AddSavedFrame(QuicFrame(&ack_frame)));
919 EXPECT_TRUE(creator_.HasPendingFrames());
921 QuicFrame frame;
922 size_t consumed = creator_.CreateStreamFrame(
923 1u, MakeIOVector("test"), 0u, false, &frame);
924 EXPECT_EQ(4u, consumed);
925 ASSERT_TRUE(frame.stream_frame);
926 EXPECT_TRUE(creator_.AddSavedFrame(frame));
927 EXPECT_TRUE(creator_.HasPendingFrames());
929 QuicPaddingFrame padding_frame;
930 EXPECT_TRUE(creator_.AddSavedFrame(QuicFrame(&padding_frame)));
931 EXPECT_TRUE(creator_.HasPendingFrames());
932 EXPECT_EQ(0u, creator_.BytesFree());
934 EXPECT_FALSE(creator_.AddSavedFrame(QuicFrame(&ack_frame)));
936 // Ensure the packet is successfully created.
937 SerializedPacket serialized = creator_.SerializePacket();
938 ASSERT_TRUE(serialized.packet);
939 delete serialized.packet;
940 ASSERT_TRUE(serialized.retransmittable_frames);
941 RetransmittableFrames* retransmittable = serialized.retransmittable_frames;
942 ASSERT_EQ(1u, retransmittable->frames().size());
943 EXPECT_EQ(STREAM_FRAME, retransmittable->frames()[0].type);
944 ASSERT_TRUE(retransmittable->frames()[0].stream_frame);
945 delete serialized.retransmittable_frames;
947 EXPECT_FALSE(creator_.HasPendingFrames());
948 EXPECT_EQ(max_plaintext_size -
949 GetPacketHeaderSize(
950 creator_.connection_id_length(),
951 QuicPacketCreatorPeer::SendVersionInPacket(&creator_),
952 PACKET_1BYTE_SEQUENCE_NUMBER,
953 NOT_IN_FEC_GROUP),
954 creator_.BytesFree());
957 TEST_P(QuicPacketCreatorTest, SerializeTruncatedAckFrameWithLargePacketSize) {
958 if (!GetParam().version_serialization) {
959 creator_.StopSendingVersion();
961 creator_.SetMaxPacketLength(kMaxPacketSize);
963 // Serialized length of ack frame with 2000 nack ranges should be limited by
964 // the number of nack ranges that can be fit in an ack frame.
965 QuicAckFrame ack_frame = MakeAckFrameWithNackRanges(2000u, 0u);
966 size_t frame_len = client_framer_.GetSerializedFrameLength(
967 QuicFrame(&ack_frame), creator_.BytesFree(), true, true,
968 NOT_IN_FEC_GROUP, PACKET_1BYTE_SEQUENCE_NUMBER);
969 EXPECT_GT(creator_.BytesFree(), frame_len);
970 EXPECT_GT(creator_.max_packet_length(), creator_.PacketSize());
972 // Add ack frame to creator.
973 EXPECT_TRUE(creator_.AddSavedFrame(QuicFrame(&ack_frame)));
974 EXPECT_TRUE(creator_.HasPendingFrames());
975 EXPECT_GT(creator_.max_packet_length(), creator_.PacketSize());
976 EXPECT_LT(0u, creator_.BytesFree());
978 // Make sure that an additional stream frame can be added to the packet.
979 QuicFrame stream_frame;
980 size_t consumed = creator_.CreateStreamFrame(
981 2u, MakeIOVector("test"), 0u, false, &stream_frame);
982 EXPECT_EQ(4u, consumed);
983 ASSERT_TRUE(stream_frame.stream_frame);
984 EXPECT_TRUE(creator_.AddSavedFrame(stream_frame));
985 EXPECT_TRUE(creator_.HasPendingFrames());
987 // Ensure the packet is successfully created, and the packet size estimate
988 // matches the serialized packet length.
989 EXPECT_CALL(entropy_calculator_,
990 EntropyHash(_)).WillOnce(testing::Return(0));
991 size_t est_packet_size = creator_.PacketSize();
992 SerializedPacket serialized = creator_.SerializePacket();
993 ASSERT_TRUE(serialized.packet);
994 EXPECT_EQ(est_packet_size,
995 client_framer_.GetMaxPlaintextSize(serialized.packet->length()));
996 delete serialized.retransmittable_frames;
997 delete serialized.packet;
1000 TEST_P(QuicPacketCreatorTest, SerializeTruncatedAckFrameWithSmallPacketSize) {
1001 if (!GetParam().version_serialization) {
1002 creator_.StopSendingVersion();
1004 creator_.SetMaxPacketLength(500u);
1006 const size_t max_plaintext_size =
1007 client_framer_.GetMaxPlaintextSize(creator_.max_packet_length());
1008 EXPECT_EQ(max_plaintext_size - creator_.PacketSize(), creator_.BytesFree());
1010 // Serialized length of ack frame with 2000 nack ranges should be limited by
1011 // the packet size.
1012 QuicAckFrame ack_frame = MakeAckFrameWithNackRanges(2000u, 0u);
1013 size_t frame_len = client_framer_.GetSerializedFrameLength(
1014 QuicFrame(&ack_frame), creator_.BytesFree(), true, true,
1015 NOT_IN_FEC_GROUP, PACKET_1BYTE_SEQUENCE_NUMBER);
1016 EXPECT_EQ(creator_.BytesFree(), frame_len);
1018 // Add ack frame to creator.
1019 EXPECT_TRUE(creator_.AddSavedFrame(QuicFrame(&ack_frame)));
1020 EXPECT_TRUE(creator_.HasPendingFrames());
1021 EXPECT_EQ(client_framer_.GetMaxPlaintextSize(creator_.max_packet_length()),
1022 creator_.PacketSize());
1023 EXPECT_EQ(0u, creator_.BytesFree());
1025 // Ensure the packet is successfully created, and the packet size estimate
1026 // may not match the serialized packet length.
1027 EXPECT_CALL(entropy_calculator_,
1028 EntropyHash(_)).WillOnce(Return(0));
1029 size_t est_packet_size = creator_.PacketSize();
1030 SerializedPacket serialized = creator_.SerializePacket();
1031 ASSERT_TRUE(serialized.packet);
1032 EXPECT_GE(est_packet_size,
1033 client_framer_.GetMaxPlaintextSize(serialized.packet->length()));
1034 delete serialized.packet;
1038 TEST_P(QuicPacketCreatorTest, EntropyFlag) {
1039 frames_.push_back(QuicFrame(new QuicStreamFrame(0u, false, 0u, IOVector())));
1041 for (int i = 0; i < 2; ++i) {
1042 for (int j = 0; j < 64; ++j) {
1043 SerializedPacket serialized = creator_.SerializeAllFrames(frames_);
1044 // Verify both BoolSource and hash algorithm.
1045 bool expected_rand_bool =
1046 (mock_random_.RandUint64() & (GG_UINT64_C(1) << j)) != 0;
1047 bool observed_rand_bool =
1048 (serialized.entropy_hash & (1 << ((j+1) % 8))) != 0;
1049 uint8 rest_of_hash = serialized.entropy_hash & ~(1 << ((j+1) % 8));
1050 EXPECT_EQ(expected_rand_bool, observed_rand_bool);
1051 EXPECT_EQ(0, rest_of_hash);
1052 delete serialized.packet;
1054 // After 64 calls, BoolSource will refresh the bucket - make sure it does.
1055 mock_random_.ChangeValue();
1058 delete frames_[0].stream_frame;
1061 } // namespace
1062 } // namespace test
1063 } // namespace net