Explicitly add python-numpy dependency to install-build-deps.
[chromium-blink-merge.git] / net / quic / quic_packet_creator_test.cc
blob578acea825d95c1deec757d78b640df6f09799b0
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), QuicTime::Zero(),
77 true),
78 client_framer_(SupportedVersions(GetParam().version), QuicTime::Zero(),
79 false),
80 sequence_number_(0),
81 connection_id_(2),
82 data_("foo"),
83 creator_(connection_id_, &client_framer_, &mock_random_) {
84 creator_.set_connection_id_length(GetParam().connection_id_length);
85 client_framer_.set_visitor(&framer_visitor_);
86 client_framer_.set_received_entropy_calculator(&entropy_calculator_);
87 server_framer_.set_visitor(&framer_visitor_);
90 ~QuicPacketCreatorTest() override {}
92 void ProcessPacket(QuicPacket* packet) {
93 scoped_ptr<QuicEncryptedPacket> encrypted(
94 server_framer_.EncryptPacket(ENCRYPTION_NONE, sequence_number_,
95 *packet));
96 server_framer_.ProcessPacket(*encrypted);
99 void CheckStreamFrame(const QuicFrame& frame,
100 QuicStreamId stream_id,
101 const string& data,
102 QuicStreamOffset offset,
103 bool fin) {
104 EXPECT_EQ(STREAM_FRAME, frame.type);
105 ASSERT_TRUE(frame.stream_frame);
106 EXPECT_EQ(stream_id, frame.stream_frame->stream_id);
107 scoped_ptr<string> frame_data(frame.stream_frame->GetDataAsString());
108 EXPECT_EQ(data, *frame_data);
109 EXPECT_EQ(offset, frame.stream_frame->offset);
110 EXPECT_EQ(fin, frame.stream_frame->fin);
113 // Returns the number of bytes consumed by the header of packet, including
114 // the version.
115 size_t GetPacketHeaderOverhead(InFecGroup is_in_fec_group) {
116 return GetPacketHeaderSize(creator_.connection_id_length(),
117 kIncludeVersion,
118 creator_.next_sequence_number_length(),
119 is_in_fec_group);
122 // Returns the number of bytes of overhead that will be added to a packet
123 // of maximum length.
124 size_t GetEncryptionOverhead() {
125 return creator_.max_packet_length() - client_framer_.GetMaxPlaintextSize(
126 creator_.max_packet_length());
129 // Returns the number of bytes consumed by the non-data fields of a stream
130 // frame, assuming it is the last frame in the packet
131 size_t GetStreamFrameOverhead(InFecGroup is_in_fec_group) {
132 return QuicFramer::GetMinStreamFrameSize(kClientDataStreamId1, kOffset,
133 true, is_in_fec_group);
136 // Enables and turns on FEC protection. Returns true if FEC protection is on.
137 bool SwitchFecProtectionOn(size_t max_packets_per_fec_group) {
138 creator_.set_max_packets_per_fec_group(max_packets_per_fec_group);
139 creator_.StartFecProtectingPackets();
140 return creator_.IsFecProtected();
143 static const QuicStreamOffset kOffset = 1u;
145 QuicFrames frames_;
146 QuicFramer server_framer_;
147 QuicFramer client_framer_;
148 testing::StrictMock<MockFramerVisitor> framer_visitor_;
149 QuicPacketSequenceNumber sequence_number_;
150 QuicConnectionId connection_id_;
151 string data_;
152 MockRandom mock_random_;
153 QuicPacketCreator creator_;
154 MockEntropyCalculator entropy_calculator_;
157 // Run all packet creator tests with all supported versions of QUIC, and with
158 // and without version in the packet header, as well as doing a run for each
159 // length of truncated connection id.
160 INSTANTIATE_TEST_CASE_P(QuicPacketCreatorTests,
161 QuicPacketCreatorTest,
162 ::testing::ValuesIn(GetTestParams()));
164 TEST_P(QuicPacketCreatorTest, SerializeFrames) {
165 frames_.push_back(QuicFrame(new QuicAckFrame(MakeAckFrame(0u))));
166 frames_.push_back(QuicFrame(new QuicStreamFrame(0u, false, 0u, IOVector())));
167 frames_.push_back(QuicFrame(new QuicStreamFrame(0u, true, 0u, IOVector())));
168 SerializedPacket serialized = creator_.SerializeAllFrames(frames_);
169 delete frames_[0].ack_frame;
170 delete frames_[1].stream_frame;
171 delete frames_[2].stream_frame;
174 InSequence s;
175 EXPECT_CALL(framer_visitor_, OnPacket());
176 EXPECT_CALL(framer_visitor_, OnUnauthenticatedPublicHeader(_));
177 EXPECT_CALL(framer_visitor_, OnUnauthenticatedHeader(_));
178 EXPECT_CALL(framer_visitor_, OnDecryptedPacket(_));
179 EXPECT_CALL(framer_visitor_, OnPacketHeader(_));
180 EXPECT_CALL(framer_visitor_, OnAckFrame(_));
181 EXPECT_CALL(framer_visitor_, OnStreamFrame(_));
182 EXPECT_CALL(framer_visitor_, OnStreamFrame(_));
183 EXPECT_CALL(framer_visitor_, OnPacketComplete());
185 ProcessPacket(serialized.packet);
186 delete serialized.packet;
189 TEST_P(QuicPacketCreatorTest, SerializeWithFEC) {
190 // Enable FEC protection, and send FEC packet every 6 packets.
191 EXPECT_TRUE(SwitchFecProtectionOn(6));
192 // Should return false since we do not have enough packets in the FEC group to
193 // trigger an FEC packet.
194 ASSERT_FALSE(creator_.ShouldSendFec(/*force_close=*/false));
196 frames_.push_back(QuicFrame(new QuicStreamFrame(0u, false, 0u, IOVector())));
197 SerializedPacket serialized = creator_.SerializeAllFrames(frames_);
198 delete frames_[0].stream_frame;
201 InSequence s;
202 EXPECT_CALL(framer_visitor_, OnPacket());
203 EXPECT_CALL(framer_visitor_, OnUnauthenticatedPublicHeader(_));
204 EXPECT_CALL(framer_visitor_, OnUnauthenticatedHeader(_));
205 EXPECT_CALL(framer_visitor_, OnDecryptedPacket(_));
206 EXPECT_CALL(framer_visitor_, OnPacketHeader(_));
207 EXPECT_CALL(framer_visitor_, OnFecProtectedPayload(_));
208 EXPECT_CALL(framer_visitor_, OnStreamFrame(_));
209 EXPECT_CALL(framer_visitor_, OnPacketComplete());
211 ProcessPacket(serialized.packet);
212 delete serialized.packet;
214 // Should return false since we do not have enough packets in the FEC group to
215 // trigger an FEC packet.
216 ASSERT_FALSE(creator_.ShouldSendFec(/*force_close=*/false));
217 // Should return true since there are packets in the FEC group.
218 ASSERT_TRUE(creator_.ShouldSendFec(/*force_close=*/true));
220 serialized = creator_.SerializeFec();
221 ASSERT_EQ(2u, serialized.sequence_number);
223 InSequence s;
224 EXPECT_CALL(framer_visitor_, OnPacket());
225 EXPECT_CALL(framer_visitor_, OnUnauthenticatedPublicHeader(_));
226 EXPECT_CALL(framer_visitor_, OnUnauthenticatedHeader(_));
227 EXPECT_CALL(framer_visitor_, OnDecryptedPacket(_));
228 EXPECT_CALL(framer_visitor_, OnPacketHeader(_));
229 EXPECT_CALL(framer_visitor_, OnFecData(_));
230 EXPECT_CALL(framer_visitor_, OnPacketComplete());
232 ProcessPacket(serialized.packet);
233 delete serialized.packet;
236 TEST_P(QuicPacketCreatorTest, SerializeChangingSequenceNumberLength) {
237 frames_.push_back(QuicFrame(new QuicAckFrame(MakeAckFrame(0u))));
238 creator_.AddSavedFrame(frames_[0]);
239 creator_.set_next_sequence_number_length(PACKET_4BYTE_SEQUENCE_NUMBER);
240 SerializedPacket serialized = creator_.SerializePacket();
241 // The sequence number length will not change mid-packet.
242 EXPECT_EQ(PACKET_1BYTE_SEQUENCE_NUMBER, serialized.sequence_number_length);
245 InSequence s;
246 EXPECT_CALL(framer_visitor_, OnPacket());
247 EXPECT_CALL(framer_visitor_, OnUnauthenticatedPublicHeader(_));
248 EXPECT_CALL(framer_visitor_, OnUnauthenticatedHeader(_));
249 EXPECT_CALL(framer_visitor_, OnDecryptedPacket(_));
250 EXPECT_CALL(framer_visitor_, OnPacketHeader(_));
251 EXPECT_CALL(framer_visitor_, OnAckFrame(_));
252 EXPECT_CALL(framer_visitor_, OnPacketComplete());
254 ProcessPacket(serialized.packet);
255 delete serialized.packet;
257 creator_.AddSavedFrame(frames_[0]);
258 serialized = creator_.SerializePacket();
259 // Now the actual sequence number length should have changed.
260 EXPECT_EQ(PACKET_4BYTE_SEQUENCE_NUMBER, serialized.sequence_number_length);
261 delete frames_[0].ack_frame;
264 InSequence s;
265 EXPECT_CALL(framer_visitor_, OnPacket());
266 EXPECT_CALL(framer_visitor_, OnUnauthenticatedPublicHeader(_));
267 EXPECT_CALL(framer_visitor_, OnUnauthenticatedHeader(_));
268 EXPECT_CALL(framer_visitor_, OnDecryptedPacket(_));
269 EXPECT_CALL(framer_visitor_, OnPacketHeader(_));
270 EXPECT_CALL(framer_visitor_, OnAckFrame(_));
271 EXPECT_CALL(framer_visitor_, OnPacketComplete());
273 ProcessPacket(serialized.packet);
274 delete serialized.packet;
277 TEST_P(QuicPacketCreatorTest, ChangeSequenceNumberLengthMidPacket) {
278 // Changing the sequence number length with queued frames in the creator
279 // should hold the change until after any currently queued frames are
280 // serialized.
282 // Packet 1.
283 // Queue a frame in the creator.
284 EXPECT_FALSE(creator_.HasPendingFrames());
285 QuicFrame ack_frame = QuicFrame(new QuicAckFrame(MakeAckFrame(0u)));
286 creator_.AddSavedFrame(ack_frame);
288 // Now change sequence number length.
289 creator_.set_next_sequence_number_length(PACKET_4BYTE_SEQUENCE_NUMBER);
291 // Add a STOP_WAITING frame since it contains a packet sequence number,
292 // whose length should be 1.
293 QuicStopWaitingFrame stop_waiting_frame;
294 EXPECT_TRUE(creator_.AddSavedFrame(QuicFrame(&stop_waiting_frame)));
295 EXPECT_TRUE(creator_.HasPendingFrames());
297 // Ensure the packet is successfully created.
298 SerializedPacket serialized = creator_.SerializePacket();
299 ASSERT_TRUE(serialized.packet);
300 EXPECT_EQ(PACKET_1BYTE_SEQUENCE_NUMBER, serialized.sequence_number_length);
302 // Verify that header in transmitted packet has 1 byte sequence length.
303 QuicPacketHeader header;
305 InSequence s;
306 EXPECT_CALL(framer_visitor_, OnPacket());
307 EXPECT_CALL(framer_visitor_, OnUnauthenticatedPublicHeader(_));
308 EXPECT_CALL(framer_visitor_, OnUnauthenticatedHeader(_));
309 EXPECT_CALL(framer_visitor_, OnDecryptedPacket(_));
310 EXPECT_CALL(framer_visitor_, OnPacketHeader(_)).WillOnce(
311 DoAll(SaveArg<0>(&header), Return(true)));
312 EXPECT_CALL(framer_visitor_, OnAckFrame(_));
313 EXPECT_CALL(framer_visitor_, OnStopWaitingFrame(_));
314 EXPECT_CALL(framer_visitor_, OnPacketComplete());
316 ProcessPacket(serialized.packet);
317 EXPECT_EQ(PACKET_1BYTE_SEQUENCE_NUMBER,
318 header.public_header.sequence_number_length);
319 delete serialized.packet;
321 // Packet 2.
322 EXPECT_FALSE(creator_.HasPendingFrames());
323 // Generate Packet 2 with one frame -- sequence number length should now
324 // change to 4 bytes.
325 EXPECT_TRUE(creator_.AddSavedFrame(QuicFrame(&stop_waiting_frame)));
326 EXPECT_TRUE(creator_.HasPendingFrames());
328 // Ensure the packet is successfully created.
329 serialized = creator_.SerializePacket();
330 ASSERT_TRUE(serialized.packet);
331 EXPECT_EQ(PACKET_4BYTE_SEQUENCE_NUMBER, serialized.sequence_number_length);
333 // Verify that header in transmitted packet has 4 byte sequence length.
335 InSequence s;
336 EXPECT_CALL(framer_visitor_, OnPacket());
337 EXPECT_CALL(framer_visitor_, OnUnauthenticatedPublicHeader(_));
338 EXPECT_CALL(framer_visitor_, OnUnauthenticatedHeader(_));
339 EXPECT_CALL(framer_visitor_, OnDecryptedPacket(_));
340 EXPECT_CALL(framer_visitor_, OnPacketHeader(_)).WillOnce(
341 DoAll(SaveArg<0>(&header), Return(true)));
342 EXPECT_CALL(framer_visitor_, OnStopWaitingFrame(_));
343 EXPECT_CALL(framer_visitor_, OnPacketComplete());
345 ProcessPacket(serialized.packet);
346 EXPECT_EQ(PACKET_4BYTE_SEQUENCE_NUMBER,
347 header.public_header.sequence_number_length);
349 delete serialized.packet;
350 delete ack_frame.ack_frame;
353 TEST_P(QuicPacketCreatorTest, SerializeWithFECChangingSequenceNumberLength) {
354 // Test goal is to test the following sequence (P1 => generate Packet 1):
355 // P1 <change seq num length> P2 FEC,
356 // and we expect that sequence number length should not change until the end
357 // of the open FEC group.
359 // Enable FEC protection, and send FEC packet every 6 packets.
360 EXPECT_TRUE(SwitchFecProtectionOn(6));
361 // Should return false since we do not have enough packets in the FEC group to
362 // trigger an FEC packet.
363 ASSERT_FALSE(creator_.ShouldSendFec(/*force_close=*/false));
364 frames_.push_back(QuicFrame(new QuicAckFrame(MakeAckFrame(0u))));
366 // Generate Packet 1.
367 creator_.AddSavedFrame(frames_[0]);
368 // Change the sequence number length mid-FEC group and it should not change.
369 creator_.set_next_sequence_number_length(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 creator_.set_next_sequence_number_length(PACKET_4BYTE_SEQUENCE_NUMBER);
442 QuicPacketCreatorPeer::SetSequenceNumberLength(&creator_,
443 PACKET_2BYTE_SEQUENCE_NUMBER);
444 frames_.push_back(QuicFrame(new QuicStreamFrame(0u, false, 0u, IOVector())));
445 SerializedPacket serialized =
446 creator_.ReserializeAllFrames(frames_, PACKET_1BYTE_SEQUENCE_NUMBER);
447 EXPECT_EQ(PACKET_4BYTE_SEQUENCE_NUMBER,
448 creator_.next_sequence_number_length());
449 EXPECT_EQ(PACKET_2BYTE_SEQUENCE_NUMBER,
450 QuicPacketCreatorPeer::GetSequenceNumberLength(&creator_));
451 EXPECT_EQ(PACKET_1BYTE_SEQUENCE_NUMBER, serialized.sequence_number_length);
452 delete frames_[0].stream_frame;
455 InSequence s;
456 EXPECT_CALL(framer_visitor_, OnPacket());
457 EXPECT_CALL(framer_visitor_, OnUnauthenticatedPublicHeader(_));
458 EXPECT_CALL(framer_visitor_, OnUnauthenticatedHeader(_));
459 EXPECT_CALL(framer_visitor_, OnDecryptedPacket(_));
460 EXPECT_CALL(framer_visitor_, OnPacketHeader(_));
461 EXPECT_CALL(framer_visitor_, OnStreamFrame(_));
462 EXPECT_CALL(framer_visitor_, OnPacketComplete());
464 ProcessPacket(serialized.packet);
465 delete serialized.packet;
468 TEST_P(QuicPacketCreatorTest, ReserializeFramesWithPadding) {
469 scoped_ptr<QuicStreamFrame> stream_frame(
470 new QuicStreamFrame(kCryptoStreamId, /*fin=*/ false, /*offset=*/ 0,
471 MakeIOVector("fake handshake message data")));
472 frames_.push_back(QuicFrame(stream_frame.get()));
473 SerializedPacket serialized =
474 creator_.ReserializeAllFrames(frames_,
475 creator_.next_sequence_number_length());
477 EXPECT_EQ(client_framer_.GetMaxPlaintextSize(kDefaultMaxPacketSize),
478 serialized.packet->length());
479 delete serialized.packet;
482 TEST_P(QuicPacketCreatorTest, ReserializeFramesWithFullPacketAndPadding) {
483 const size_t overhead = GetPacketHeaderOverhead(NOT_IN_FEC_GROUP)
484 + GetEncryptionOverhead() + GetStreamFrameOverhead(NOT_IN_FEC_GROUP);
485 size_t capacity = kDefaultMaxPacketSize - overhead;
486 for (int delta = -5; delta <= 0; ++delta) {
487 string data(capacity + delta, 'A');
488 size_t bytes_free = 0 - delta;
490 scoped_ptr<QuicStreamFrame> stream_frame(
491 new QuicStreamFrame(kCryptoStreamId, /*fin=*/ false, kOffset,
492 MakeIOVector(data)));
493 frames_.push_back(QuicFrame(stream_frame.get()));
494 SerializedPacket serialized =
495 creator_.ReserializeAllFrames(frames_,
496 creator_.next_sequence_number_length());
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(client_framer_.GetMaxPlaintextSize(kDefaultMaxPacketSize)
503 - bytes_free, serialized.packet->length());
504 } else {
505 EXPECT_EQ(client_framer_.GetMaxPlaintextSize(kDefaultMaxPacketSize),
506 serialized.packet->length());
509 delete serialized.packet;
510 frames_.clear();
514 TEST_P(QuicPacketCreatorTest, SerializeConnectionClose) {
515 QuicConnectionCloseFrame frame;
516 frame.error_code = QUIC_NO_ERROR;
517 frame.error_details = "error";
519 SerializedPacket serialized = creator_.SerializeConnectionClose(&frame);
520 ASSERT_EQ(1u, serialized.sequence_number);
521 ASSERT_EQ(1u, creator_.sequence_number());
523 InSequence s;
524 EXPECT_CALL(framer_visitor_, OnPacket());
525 EXPECT_CALL(framer_visitor_, OnUnauthenticatedPublicHeader(_));
526 EXPECT_CALL(framer_visitor_, OnUnauthenticatedHeader(_));
527 EXPECT_CALL(framer_visitor_, OnDecryptedPacket(_));
528 EXPECT_CALL(framer_visitor_, OnPacketHeader(_));
529 EXPECT_CALL(framer_visitor_, OnConnectionCloseFrame(_));
530 EXPECT_CALL(framer_visitor_, OnPacketComplete());
532 ProcessPacket(serialized.packet);
533 delete serialized.packet;
536 TEST_P(QuicPacketCreatorTest, SwitchFecOnOffWithNoGroup) {
537 // Enable FEC protection.
538 creator_.set_max_packets_per_fec_group(6);
539 EXPECT_TRUE(creator_.IsFecEnabled());
540 EXPECT_FALSE(creator_.IsFecProtected());
542 // Turn on FEC protection.
543 creator_.StartFecProtectingPackets();
544 EXPECT_TRUE(creator_.IsFecProtected());
545 // We have no packets in the FEC group, so no FEC packet can be created.
546 EXPECT_FALSE(creator_.ShouldSendFec(/*force_close=*/true));
547 // Since no packets are in FEC group yet, we should be able to turn FEC
548 // off with no trouble.
549 creator_.StopFecProtectingPackets();
550 EXPECT_FALSE(creator_.IsFecProtected());
553 TEST_P(QuicPacketCreatorTest, SwitchFecOnOffWithGroupInProgress) {
554 // Enable FEC protection, and send FEC packet every 6 packets.
555 EXPECT_TRUE(SwitchFecProtectionOn(6));
556 frames_.push_back(QuicFrame(new QuicStreamFrame(0u, false, 0u, IOVector())));
557 SerializedPacket serialized = creator_.SerializeAllFrames(frames_);
558 delete frames_[0].stream_frame;
559 delete serialized.packet;
561 EXPECT_TRUE(creator_.IsFecProtected());
562 // We do not have enough packets in the FEC group to trigger an FEC packet.
563 EXPECT_FALSE(creator_.ShouldSendFec(/*force_close=*/false));
564 // Should return true since there are packets in the FEC group.
565 EXPECT_TRUE(creator_.ShouldSendFec(/*force_close=*/true));
567 // Switching FEC off should not change creator state, since there is an
568 // FEC packet under construction.
569 EXPECT_DFATAL(creator_.StopFecProtectingPackets(),
570 "Cannot stop FEC protection with open FEC group.");
571 EXPECT_TRUE(creator_.IsFecProtected());
572 // Confirm that FEC packet is still under construction.
573 EXPECT_TRUE(creator_.ShouldSendFec(/*force_close=*/true));
575 serialized = creator_.SerializeFec();
576 delete serialized.packet;
578 // Switching FEC on/off should work now.
579 creator_.StopFecProtectingPackets();
580 EXPECT_FALSE(creator_.IsFecProtected());
581 creator_.StartFecProtectingPackets();
582 EXPECT_TRUE(creator_.IsFecProtected());
585 TEST_P(QuicPacketCreatorTest, SwitchFecOnWithStreamFrameQueued) {
586 // Add a stream frame to the creator.
587 QuicFrame frame;
588 size_t consumed = creator_.CreateStreamFrame(
589 1u, MakeIOVector("test"), 0u, false, &frame);
590 EXPECT_EQ(4u, consumed);
591 ASSERT_TRUE(frame.stream_frame);
592 EXPECT_TRUE(creator_.AddSavedFrame(frame));
593 EXPECT_TRUE(creator_.HasPendingFrames());
595 // Enable FEC protection, and send FEC packet every 6 packets.
596 creator_.set_max_packets_per_fec_group(6);
597 EXPECT_TRUE(creator_.IsFecEnabled());
598 EXPECT_DFATAL(creator_.StartFecProtectingPackets(),
599 "Cannot start FEC protection with pending frames.");
600 EXPECT_FALSE(creator_.IsFecProtected());
602 // Serialize packet for transmission.
603 SerializedPacket serialized = creator_.SerializePacket();
604 delete serialized.packet;
605 delete serialized.retransmittable_frames;
606 EXPECT_FALSE(creator_.HasPendingFrames());
608 // Since all pending frames have been serialized, turning FEC on should work.
609 creator_.StartFecProtectingPackets();
610 EXPECT_TRUE(creator_.IsFecProtected());
613 TEST_P(QuicPacketCreatorTest, CreateStreamFrame) {
614 QuicFrame frame;
615 size_t consumed = creator_.CreateStreamFrame(1u, MakeIOVector("test"), 0u,
616 false, &frame);
617 EXPECT_EQ(4u, consumed);
618 CheckStreamFrame(frame, 1u, "test", 0u, false);
619 delete frame.stream_frame;
622 TEST_P(QuicPacketCreatorTest, CreateStreamFrameFin) {
623 QuicFrame frame;
624 size_t consumed = creator_.CreateStreamFrame(1u, MakeIOVector("test"), 10u,
625 true, &frame);
626 EXPECT_EQ(4u, consumed);
627 CheckStreamFrame(frame, 1u, "test", 10u, true);
628 delete frame.stream_frame;
631 TEST_P(QuicPacketCreatorTest, CreateStreamFrameFinOnly) {
632 QuicFrame frame;
633 size_t consumed = creator_.CreateStreamFrame(1u, IOVector(), 0u, true,
634 &frame);
635 EXPECT_EQ(0u, consumed);
636 CheckStreamFrame(frame, 1u, string(), 0u, true);
637 delete frame.stream_frame;
640 TEST_P(QuicPacketCreatorTest, CreateAllFreeBytesForStreamFrames) {
641 const size_t overhead = GetPacketHeaderOverhead(NOT_IN_FEC_GROUP)
642 + GetEncryptionOverhead();
643 for (size_t i = overhead; i < overhead + 100; ++i) {
644 creator_.set_max_packet_length(i);
645 const bool should_have_room = i > overhead + GetStreamFrameOverhead(
646 NOT_IN_FEC_GROUP);
647 ASSERT_EQ(should_have_room, creator_.HasRoomForStreamFrame(
648 kClientDataStreamId1, kOffset));
649 if (should_have_room) {
650 QuicFrame frame;
651 size_t bytes_consumed = creator_.CreateStreamFrame(
652 kClientDataStreamId1, MakeIOVector("testdata"), kOffset, false,
653 &frame);
654 EXPECT_LT(0u, bytes_consumed);
655 ASSERT_TRUE(creator_.AddSavedFrame(frame));
656 SerializedPacket serialized_packet = creator_.SerializePacket();
657 ASSERT_TRUE(serialized_packet.packet);
658 delete serialized_packet.packet;
659 delete serialized_packet.retransmittable_frames;
664 TEST_P(QuicPacketCreatorTest, StreamFrameConsumption) {
665 // Compute the total overhead for a single frame in packet.
666 const size_t overhead = GetPacketHeaderOverhead(NOT_IN_FEC_GROUP)
667 + GetEncryptionOverhead() + GetStreamFrameOverhead(NOT_IN_FEC_GROUP);
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;
673 QuicFrame frame;
674 size_t bytes_consumed = creator_.CreateStreamFrame(
675 kClientDataStreamId1, MakeIOVector(data), kOffset, false, &frame);
676 EXPECT_EQ(capacity - bytes_free, bytes_consumed);
678 ASSERT_TRUE(creator_.AddSavedFrame(frame));
679 // BytesFree() returns bytes available for the next frame, which will
680 // be two bytes smaller since the stream frame would need to be grown.
681 EXPECT_EQ(2u, creator_.ExpansionOnNewFrame());
682 size_t expected_bytes_free = bytes_free < 3 ? 0 : bytes_free - 2;
683 EXPECT_EQ(expected_bytes_free, creator_.BytesFree()) << "delta: " << delta;
684 SerializedPacket serialized_packet = creator_.SerializePacket();
685 ASSERT_TRUE(serialized_packet.packet);
686 delete serialized_packet.packet;
687 delete serialized_packet.retransmittable_frames;
691 TEST_P(QuicPacketCreatorTest, StreamFrameConsumptionWithFec) {
692 // Enable FEC protection, and send FEC packet every 6 packets.
693 EXPECT_TRUE(SwitchFecProtectionOn(6));
694 // Compute the total overhead for a single frame in packet.
695 const size_t overhead = GetPacketHeaderOverhead(IN_FEC_GROUP)
696 + GetEncryptionOverhead() + GetStreamFrameOverhead(IN_FEC_GROUP);
697 size_t capacity = kDefaultMaxPacketSize - overhead;
698 // Now, test various sizes around this size.
699 for (int delta = -5; delta <= 5; ++delta) {
700 string data(capacity + delta, 'A');
701 size_t bytes_free = delta > 0 ? 0 : 0 - delta;
702 QuicFrame frame;
703 size_t bytes_consumed = creator_.CreateStreamFrame(
704 kClientDataStreamId1, MakeIOVector(data), kOffset, false, &frame);
705 EXPECT_EQ(capacity - bytes_free, bytes_consumed);
707 ASSERT_TRUE(creator_.AddSavedFrame(frame));
708 // BytesFree() returns bytes available for the next frame. Since stream
709 // frame does not grow for FEC protected packets, this should be the same
710 // as bytes_free (bound by 0).
711 EXPECT_EQ(0u, creator_.ExpansionOnNewFrame());
712 size_t expected_bytes_free = bytes_free > 0 ? bytes_free : 0;
713 EXPECT_EQ(expected_bytes_free, creator_.BytesFree()) << "delta: " << delta;
714 SerializedPacket serialized_packet = creator_.SerializePacket();
715 ASSERT_TRUE(serialized_packet.packet);
716 delete serialized_packet.packet;
717 delete serialized_packet.retransmittable_frames;
721 TEST_P(QuicPacketCreatorTest, CryptoStreamFramePacketPadding) {
722 // Compute the total overhead for a single frame in packet.
723 const size_t overhead = GetPacketHeaderOverhead(NOT_IN_FEC_GROUP)
724 + GetEncryptionOverhead() + GetStreamFrameOverhead(NOT_IN_FEC_GROUP);
725 ASSERT_GT(kMaxPacketSize, overhead);
726 size_t capacity = kDefaultMaxPacketSize - overhead;
727 // Now, test various sizes around this size.
728 for (int delta = -5; delta <= 5; ++delta) {
729 string data(capacity + delta, 'A');
730 size_t bytes_free = delta > 0 ? 0 : 0 - delta;
732 QuicFrame frame;
733 size_t bytes_consumed = creator_.CreateStreamFrame(
734 kCryptoStreamId, MakeIOVector(data), kOffset, false, &frame);
735 EXPECT_LT(0u, bytes_consumed);
736 ASSERT_TRUE(creator_.AddSavedFrame(frame));
737 SerializedPacket serialized_packet = creator_.SerializePacket();
738 ASSERT_TRUE(serialized_packet.packet);
739 // If there is not enough space in the packet to fit a padding frame
740 // (1 byte) and to expand the stream frame (another 2 bytes) the packet
741 // will not be padded.
742 if (bytes_free < 3) {
743 EXPECT_EQ(client_framer_.GetMaxPlaintextSize(kDefaultMaxPacketSize)
744 - bytes_free, serialized_packet.packet->length());
745 } else {
746 EXPECT_EQ(client_framer_.GetMaxPlaintextSize(kDefaultMaxPacketSize),
747 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(client_framer_.GetMaxPlaintextSize(kDefaultMaxPacketSize)
774 - bytes_free, serialized_packet.packet->length());
775 } else {
776 EXPECT_EQ(client_framer_.GetMaxPlaintextSize(kDefaultMaxPacketSize),
777 serialized_packet.packet->length());
779 delete serialized_packet.packet;
780 delete serialized_packet.retransmittable_frames;
784 TEST_P(QuicPacketCreatorTest, SerializeVersionNegotiationPacket) {
785 QuicFramerPeer::SetIsServer(&client_framer_, true);
786 QuicVersionVector versions;
787 versions.push_back(test::QuicVersionMax());
788 scoped_ptr<QuicEncryptedPacket> encrypted(
789 creator_.SerializeVersionNegotiationPacket(versions));
792 InSequence s;
793 EXPECT_CALL(framer_visitor_, OnPacket());
794 EXPECT_CALL(framer_visitor_, OnUnauthenticatedPublicHeader(_));
795 EXPECT_CALL(framer_visitor_, OnVersionNegotiationPacket(_));
797 QuicFramerPeer::SetIsServer(&client_framer_, false);
798 client_framer_.ProcessPacket(*encrypted);
801 TEST_P(QuicPacketCreatorTest, UpdatePacketSequenceNumberLengthLeastAwaiting) {
802 EXPECT_EQ(PACKET_1BYTE_SEQUENCE_NUMBER,
803 creator_.next_sequence_number_length());
805 size_t max_packets_per_fec_group = 10;
806 creator_.set_max_packets_per_fec_group(max_packets_per_fec_group);
807 creator_.set_sequence_number(64 - max_packets_per_fec_group);
808 creator_.UpdateSequenceNumberLength(2, 10000 / kDefaultMaxPacketSize);
809 EXPECT_EQ(PACKET_1BYTE_SEQUENCE_NUMBER,
810 creator_.next_sequence_number_length());
812 creator_.set_sequence_number(64 * 256 - max_packets_per_fec_group);
813 creator_.UpdateSequenceNumberLength(2, 10000 / kDefaultMaxPacketSize);
814 EXPECT_EQ(PACKET_2BYTE_SEQUENCE_NUMBER,
815 creator_.next_sequence_number_length());
817 creator_.set_sequence_number(64 * 256 * 256 - max_packets_per_fec_group);
818 creator_.UpdateSequenceNumberLength(2, 10000 / kDefaultMaxPacketSize);
819 EXPECT_EQ(PACKET_4BYTE_SEQUENCE_NUMBER,
820 creator_.next_sequence_number_length());
822 creator_.set_sequence_number(
823 GG_UINT64_C(64) * 256 * 256 * 256 * 256 - max_packets_per_fec_group);
824 creator_.UpdateSequenceNumberLength(2, 10000 / kDefaultMaxPacketSize);
825 EXPECT_EQ(PACKET_6BYTE_SEQUENCE_NUMBER,
826 creator_.next_sequence_number_length());
829 TEST_P(QuicPacketCreatorTest, UpdatePacketSequenceNumberLengthBandwidth) {
830 EXPECT_EQ(PACKET_1BYTE_SEQUENCE_NUMBER,
831 creator_.next_sequence_number_length());
833 creator_.UpdateSequenceNumberLength(1, 10000 / kDefaultMaxPacketSize);
834 EXPECT_EQ(PACKET_1BYTE_SEQUENCE_NUMBER,
835 creator_.next_sequence_number_length());
837 creator_.UpdateSequenceNumberLength(1, 10000 * 256 / kDefaultMaxPacketSize);
838 EXPECT_EQ(PACKET_2BYTE_SEQUENCE_NUMBER,
839 creator_.next_sequence_number_length());
841 creator_.UpdateSequenceNumberLength(
842 1, 10000 * 256 * 256 / kDefaultMaxPacketSize);
843 EXPECT_EQ(PACKET_4BYTE_SEQUENCE_NUMBER,
844 creator_.next_sequence_number_length());
846 creator_.UpdateSequenceNumberLength(
847 1, GG_UINT64_C(1000) * 256 * 256 * 256 * 256 / kDefaultMaxPacketSize);
848 EXPECT_EQ(PACKET_6BYTE_SEQUENCE_NUMBER,
849 creator_.next_sequence_number_length());
852 TEST_P(QuicPacketCreatorTest, CreateStreamFrameWithNotifier) {
853 // Ensure that if CreateStreamFrame does not consume any data (e.g. a FIN only
854 // frame) then any QuicAckNotifier that is passed in still gets attached to
855 // the frame.
856 scoped_refptr<MockAckNotifierDelegate> delegate(new MockAckNotifierDelegate);
857 QuicAckNotifier notifier(delegate.get());
858 QuicFrame frame;
859 IOVector empty_iovector;
860 bool fin = true;
861 size_t consumed_bytes = creator_.CreateStreamFrameWithNotifier(
862 1u, empty_iovector, 0u, fin, &notifier, &frame);
863 EXPECT_EQ(0u, consumed_bytes);
864 EXPECT_EQ(&notifier, frame.stream_frame->notifier);
865 delete frame.stream_frame;
868 TEST_P(QuicPacketCreatorTest, SerializeFrame) {
869 if (!GetParam().version_serialization) {
870 creator_.StopSendingVersion();
872 frames_.push_back(QuicFrame(new QuicStreamFrame(0u, false, 0u, IOVector())));
873 SerializedPacket serialized = creator_.SerializeAllFrames(frames_);
874 delete frames_[0].stream_frame;
876 QuicPacketHeader header;
878 InSequence s;
879 EXPECT_CALL(framer_visitor_, OnPacket());
880 EXPECT_CALL(framer_visitor_, OnUnauthenticatedPublicHeader(_));
881 EXPECT_CALL(framer_visitor_, OnUnauthenticatedHeader(_));
882 EXPECT_CALL(framer_visitor_, OnDecryptedPacket(_));
883 EXPECT_CALL(framer_visitor_, OnPacketHeader(_)).WillOnce(
884 DoAll(SaveArg<0>(&header), Return(true)));
885 EXPECT_CALL(framer_visitor_, OnStreamFrame(_));
886 EXPECT_CALL(framer_visitor_, OnPacketComplete());
888 ProcessPacket(serialized.packet);
889 EXPECT_EQ(GetParam().version_serialization,
890 header.public_header.version_flag);
891 delete serialized.packet;
894 TEST_P(QuicPacketCreatorTest, CreateStreamFrameTooLarge) {
895 if (!GetParam().version_serialization) {
896 creator_.StopSendingVersion();
898 // A string larger than fits into a frame.
899 size_t payload_length;
900 creator_.set_max_packet_length(GetPacketLengthForOneStream(
901 client_framer_.version(),
902 QuicPacketCreatorPeer::SendVersionInPacket(&creator_),
903 creator_.connection_id_length(),
904 PACKET_1BYTE_SEQUENCE_NUMBER, NOT_IN_FEC_GROUP, &payload_length));
905 QuicFrame frame;
906 const string too_long_payload(payload_length * 2, 'a');
907 size_t consumed = creator_.CreateStreamFrame(
908 1u, MakeIOVector(too_long_payload), 0u, true, &frame);
909 EXPECT_EQ(payload_length, consumed);
910 const string payload(payload_length, 'a');
911 CheckStreamFrame(frame, 1u, payload, 0u, false);
912 delete frame.stream_frame;
915 TEST_P(QuicPacketCreatorTest, AddFrameAndSerialize) {
916 if (!GetParam().version_serialization) {
917 creator_.StopSendingVersion();
919 const size_t max_plaintext_size =
920 client_framer_.GetMaxPlaintextSize(creator_.max_packet_length());
921 EXPECT_FALSE(creator_.HasPendingFrames());
922 EXPECT_EQ(max_plaintext_size -
923 GetPacketHeaderSize(
924 creator_.connection_id_length(),
925 QuicPacketCreatorPeer::SendVersionInPacket(&creator_),
926 PACKET_1BYTE_SEQUENCE_NUMBER, NOT_IN_FEC_GROUP),
927 creator_.BytesFree());
929 // Add a variety of frame types and then a padding frame.
930 QuicAckFrame ack_frame(MakeAckFrame(0u));
931 EXPECT_TRUE(creator_.AddSavedFrame(QuicFrame(&ack_frame)));
932 EXPECT_TRUE(creator_.HasPendingFrames());
934 QuicCongestionFeedbackFrame congestion_feedback;
935 congestion_feedback.type = kTCP;
936 congestion_feedback.tcp.receive_window = 0x4030;
937 EXPECT_TRUE(creator_.AddSavedFrame(QuicFrame(&congestion_feedback)));
938 EXPECT_TRUE(creator_.HasPendingFrames());
940 QuicFrame frame;
941 size_t consumed = creator_.CreateStreamFrame(
942 1u, MakeIOVector("test"), 0u, false, &frame);
943 EXPECT_EQ(4u, consumed);
944 ASSERT_TRUE(frame.stream_frame);
945 EXPECT_TRUE(creator_.AddSavedFrame(frame));
946 EXPECT_TRUE(creator_.HasPendingFrames());
948 QuicPaddingFrame padding_frame;
949 EXPECT_TRUE(creator_.AddSavedFrame(QuicFrame(&padding_frame)));
950 EXPECT_TRUE(creator_.HasPendingFrames());
951 EXPECT_EQ(0u, creator_.BytesFree());
953 EXPECT_FALSE(creator_.AddSavedFrame(QuicFrame(&ack_frame)));
955 // Ensure the packet is successfully created.
956 SerializedPacket serialized = creator_.SerializePacket();
957 ASSERT_TRUE(serialized.packet);
958 delete serialized.packet;
959 ASSERT_TRUE(serialized.retransmittable_frames);
960 RetransmittableFrames* retransmittable = serialized.retransmittable_frames;
961 ASSERT_EQ(1u, retransmittable->frames().size());
962 EXPECT_EQ(STREAM_FRAME, retransmittable->frames()[0].type);
963 ASSERT_TRUE(retransmittable->frames()[0].stream_frame);
964 delete serialized.retransmittable_frames;
966 EXPECT_FALSE(creator_.HasPendingFrames());
967 EXPECT_EQ(max_plaintext_size -
968 GetPacketHeaderSize(
969 creator_.connection_id_length(),
970 QuicPacketCreatorPeer::SendVersionInPacket(&creator_),
971 PACKET_1BYTE_SEQUENCE_NUMBER,
972 NOT_IN_FEC_GROUP),
973 creator_.BytesFree());
976 TEST_P(QuicPacketCreatorTest, SerializeTruncatedAckFrameWithLargePacketSize) {
977 if (!GetParam().version_serialization) {
978 creator_.StopSendingVersion();
980 creator_.set_max_packet_length(kMaxPacketSize);
981 const size_t max_plaintext_size =
982 client_framer_.GetMaxPlaintextSize(creator_.max_packet_length());
984 // Serialized length of ack frame with 2000 nack ranges should be limited by
985 // the number of nack ranges that can be fit in an ack frame.
986 QuicAckFrame ack_frame = MakeAckFrameWithNackRanges(2000u, 0u);
987 size_t frame_len = client_framer_.GetSerializedFrameLength(
988 QuicFrame(&ack_frame), creator_.BytesFree(), true, true,
989 NOT_IN_FEC_GROUP, PACKET_1BYTE_SEQUENCE_NUMBER);
990 EXPECT_GT(creator_.BytesFree(), frame_len);
991 EXPECT_GT(max_plaintext_size, creator_.PacketSize());
993 // Add ack frame to creator.
994 EXPECT_TRUE(creator_.AddSavedFrame(QuicFrame(&ack_frame)));
995 EXPECT_TRUE(creator_.HasPendingFrames());
996 EXPECT_GT(max_plaintext_size, creator_.PacketSize());
997 EXPECT_LT(0u, creator_.BytesFree());
999 // Make sure that an additional stream frame can be added to the packet.
1000 QuicFrame stream_frame;
1001 size_t consumed = creator_.CreateStreamFrame(
1002 2u, MakeIOVector("test"), 0u, false, &stream_frame);
1003 EXPECT_EQ(4u, consumed);
1004 ASSERT_TRUE(stream_frame.stream_frame);
1005 EXPECT_TRUE(creator_.AddSavedFrame(stream_frame));
1006 EXPECT_TRUE(creator_.HasPendingFrames());
1008 // Ensure the packet is successfully created, and the packet size estimate
1009 // matches the serialized packet length.
1010 EXPECT_CALL(entropy_calculator_,
1011 EntropyHash(_)).WillOnce(testing::Return(0));
1012 size_t est_packet_size = creator_.PacketSize();
1013 SerializedPacket serialized = creator_.SerializePacket();
1014 ASSERT_TRUE(serialized.packet);
1015 EXPECT_EQ(est_packet_size, serialized.packet->length());
1016 delete serialized.retransmittable_frames;
1017 delete serialized.packet;
1020 TEST_P(QuicPacketCreatorTest, SerializeTruncatedAckFrameWithSmallPacketSize) {
1021 if (!GetParam().version_serialization) {
1022 creator_.StopSendingVersion();
1024 creator_.set_max_packet_length(500u);
1026 const size_t max_plaintext_size =
1027 client_framer_.GetMaxPlaintextSize(creator_.max_packet_length());
1028 EXPECT_EQ(max_plaintext_size - creator_.PacketSize(), creator_.BytesFree());
1030 // Serialized length of ack frame with 2000 nack ranges should be limited by
1031 // the packet size.
1032 QuicAckFrame ack_frame = MakeAckFrameWithNackRanges(2000u, 0u);
1033 size_t frame_len = client_framer_.GetSerializedFrameLength(
1034 QuicFrame(&ack_frame), creator_.BytesFree(), true, true,
1035 NOT_IN_FEC_GROUP, PACKET_1BYTE_SEQUENCE_NUMBER);
1036 EXPECT_EQ(creator_.BytesFree(), frame_len);
1038 // Add ack frame to creator.
1039 EXPECT_TRUE(creator_.AddSavedFrame(QuicFrame(&ack_frame)));
1040 EXPECT_TRUE(creator_.HasPendingFrames());
1041 EXPECT_EQ(max_plaintext_size, creator_.PacketSize());
1042 EXPECT_EQ(0u, creator_.BytesFree());
1044 // Ensure the packet is successfully created, and the packet size estimate
1045 // may not match the serialized packet length.
1046 EXPECT_CALL(entropy_calculator_,
1047 EntropyHash(_)).WillOnce(Return(0));
1048 size_t est_packet_size = creator_.PacketSize();
1049 SerializedPacket serialized = creator_.SerializePacket();
1050 ASSERT_TRUE(serialized.packet);
1051 EXPECT_GE(est_packet_size, serialized.packet->length());
1052 delete serialized.packet;
1056 TEST_P(QuicPacketCreatorTest, EntropyFlag) {
1057 frames_.push_back(QuicFrame(new QuicStreamFrame(0u, false, 0u, IOVector())));
1059 for (int i = 0; i < 2; ++i) {
1060 for (int j = 0; j < 64; ++j) {
1061 SerializedPacket serialized = creator_.SerializeAllFrames(frames_);
1062 // Verify both BoolSource and hash algorithm.
1063 bool expected_rand_bool =
1064 (mock_random_.RandUint64() & (GG_UINT64_C(1) << j)) != 0;
1065 bool observed_rand_bool =
1066 (serialized.entropy_hash & (1 << ((j+1) % 8))) != 0;
1067 uint8 rest_of_hash = serialized.entropy_hash & ~(1 << ((j+1) % 8));
1068 EXPECT_EQ(expected_rand_bool, observed_rand_bool);
1069 EXPECT_EQ(0, rest_of_hash);
1070 delete serialized.packet;
1072 // After 64 calls, BoolSource will refresh the bucket - make sure it does.
1073 mock_random_.ChangeValue();
1076 delete frames_[0].stream_frame;
1079 } // namespace
1080 } // namespace test
1081 } // namespace net