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_packet_creator_peer.h"
14 #include "net/quic/test_tools/quic_test_utils.h"
15 #include "testing/gmock/include/gmock/gmock.h"
17 using base::StringPiece
;
21 using testing::InSequence
;
22 using testing::Return
;
23 using testing::SaveArg
;
30 class QuicPacketCreatorTest
: public ::testing::TestWithParam
<bool> {
32 QuicPacketCreatorTest()
33 : server_framer_(QuicSupportedVersions(), QuicTime::Zero(), true),
34 client_framer_(QuicSupportedVersions(), QuicTime::Zero(), false),
38 creator_(guid_
, &client_framer_
, &mock_random_
, false) {
39 client_framer_
.set_visitor(&framer_visitor_
);
40 server_framer_
.set_visitor(&framer_visitor_
);
42 ~QuicPacketCreatorTest() {
45 void ProcessPacket(QuicPacket
* packet
) {
46 scoped_ptr
<QuicEncryptedPacket
> encrypted(
47 server_framer_
.EncryptPacket(ENCRYPTION_NONE
, sequence_number_
,
49 server_framer_
.ProcessPacket(*encrypted
);
52 void CheckStreamFrame(const QuicFrame
& frame
,
53 QuicStreamId stream_id
,
55 QuicStreamOffset offset
,
57 EXPECT_EQ(STREAM_FRAME
, frame
.type
);
58 ASSERT_TRUE(frame
.stream_frame
);
59 EXPECT_EQ(stream_id
, frame
.stream_frame
->stream_id
);
60 scoped_ptr
<string
> frame_data(frame
.stream_frame
->GetDataAsString());
61 EXPECT_EQ(data
, *frame_data
);
62 EXPECT_EQ(offset
, frame
.stream_frame
->offset
);
63 EXPECT_EQ(fin
, frame
.stream_frame
->fin
);
66 // Returns the number of bytes consumed by the header of packet, including
67 // the version, that is not in an FEC group.
68 size_t GetPacketHeaderOverhead() {
69 return GetPacketHeaderSize(creator_
.options()->send_guid_length
,
71 creator_
.options()->send_sequence_number_length
,
75 // Returns the number of bytes of overhead that will be added to a packet
77 size_t GetEncryptionOverhead() {
78 return creator_
.options()->max_packet_length
-
79 client_framer_
.GetMaxPlaintextSize(
80 creator_
.options()->max_packet_length
);
83 // Returns the number of bytes consumed by the non-data fields of a stream
84 // frame, assuming it is the last frame in the packet
85 size_t GetStreamFrameOverhead() {
86 return QuicFramer::GetMinStreamFrameSize(
87 client_framer_
.version(), kStreamId
, kOffset
, true);
90 static const QuicStreamId kStreamId
= 1u;
91 static const QuicStreamOffset kOffset
= 1u;
94 QuicFramer server_framer_
;
95 QuicFramer client_framer_
;
96 testing::StrictMock
<MockFramerVisitor
> framer_visitor_
;
97 QuicPacketSequenceNumber sequence_number_
;
100 MockRandom mock_random_
;
101 QuicPacketCreator creator_
;
104 TEST_F(QuicPacketCreatorTest
, SerializeFrames
) {
105 frames_
.push_back(QuicFrame(new QuicAckFrame(0u, QuicTime::Zero(), 0u)));
106 frames_
.push_back(QuicFrame(new QuicStreamFrame(0u, false, 0u, IOVector())));
107 frames_
.push_back(QuicFrame(new QuicStreamFrame(0u, true, 0u, IOVector())));
108 SerializedPacket serialized
= creator_
.SerializeAllFrames(frames_
);
109 delete frames_
[0].ack_frame
;
110 delete frames_
[1].stream_frame
;
111 delete frames_
[2].stream_frame
;
115 EXPECT_CALL(framer_visitor_
, OnPacket());
116 EXPECT_CALL(framer_visitor_
, OnUnauthenticatedPublicHeader(_
));
117 EXPECT_CALL(framer_visitor_
, OnUnauthenticatedHeader(_
));
118 EXPECT_CALL(framer_visitor_
, OnPacketHeader(_
));
119 EXPECT_CALL(framer_visitor_
, OnAckFrame(_
));
120 EXPECT_CALL(framer_visitor_
, OnStreamFrame(_
));
121 EXPECT_CALL(framer_visitor_
, OnStreamFrame(_
));
122 EXPECT_CALL(framer_visitor_
, OnPacketComplete());
124 ProcessPacket(serialized
.packet
);
125 delete serialized
.packet
;
128 TEST_F(QuicPacketCreatorTest
, SerializeWithFEC
) {
129 creator_
.options()->max_packets_per_fec_group
= 6;
130 ASSERT_FALSE(creator_
.ShouldSendFec(false));
132 frames_
.push_back(QuicFrame(new QuicStreamFrame(0u, false, 0u, IOVector())));
133 SerializedPacket serialized
= creator_
.SerializeAllFrames(frames_
);
134 delete frames_
[0].stream_frame
;
138 EXPECT_CALL(framer_visitor_
, OnPacket());
139 EXPECT_CALL(framer_visitor_
, OnUnauthenticatedPublicHeader(_
));
140 EXPECT_CALL(framer_visitor_
, OnUnauthenticatedHeader(_
));
141 EXPECT_CALL(framer_visitor_
, OnPacketHeader(_
));
142 EXPECT_CALL(framer_visitor_
, OnFecProtectedPayload(_
));
143 EXPECT_CALL(framer_visitor_
, OnStreamFrame(_
));
144 EXPECT_CALL(framer_visitor_
, OnPacketComplete());
146 ProcessPacket(serialized
.packet
);
147 delete serialized
.packet
;
149 ASSERT_FALSE(creator_
.ShouldSendFec(false));
150 ASSERT_TRUE(creator_
.ShouldSendFec(true));
152 serialized
= creator_
.SerializeFec();
153 ASSERT_EQ(2u, serialized
.sequence_number
);
157 EXPECT_CALL(framer_visitor_
, OnPacket());
158 EXPECT_CALL(framer_visitor_
, OnUnauthenticatedPublicHeader(_
));
159 EXPECT_CALL(framer_visitor_
, OnUnauthenticatedHeader(_
));
160 EXPECT_CALL(framer_visitor_
, OnPacketHeader(_
));
161 EXPECT_CALL(framer_visitor_
, OnFecData(_
));
162 EXPECT_CALL(framer_visitor_
, OnPacketComplete());
164 ProcessPacket(serialized
.packet
);
165 delete serialized
.packet
;
168 TEST_F(QuicPacketCreatorTest
, SerializeChangingSequenceNumberLength
) {
169 frames_
.push_back(QuicFrame(new QuicAckFrame(0u, QuicTime::Zero(), 0u)));
170 creator_
.AddSavedFrame(frames_
[0]);
171 creator_
.options()->send_sequence_number_length
=
172 PACKET_4BYTE_SEQUENCE_NUMBER
;
173 SerializedPacket serialized
= creator_
.SerializePacket();
174 // The sequence number length will not change mid-packet.
175 EXPECT_EQ(PACKET_1BYTE_SEQUENCE_NUMBER
, serialized
.sequence_number_length
);
179 EXPECT_CALL(framer_visitor_
, OnPacket());
180 EXPECT_CALL(framer_visitor_
, OnUnauthenticatedPublicHeader(_
));
181 EXPECT_CALL(framer_visitor_
, OnUnauthenticatedHeader(_
));
182 EXPECT_CALL(framer_visitor_
, OnPacketHeader(_
));
183 EXPECT_CALL(framer_visitor_
, OnAckFrame(_
));
184 EXPECT_CALL(framer_visitor_
, OnPacketComplete());
186 ProcessPacket(serialized
.packet
);
187 delete serialized
.packet
;
189 creator_
.AddSavedFrame(frames_
[0]);
190 serialized
= creator_
.SerializePacket();
191 // Now the actual sequence number length should have changed.
192 EXPECT_EQ(PACKET_4BYTE_SEQUENCE_NUMBER
, serialized
.sequence_number_length
);
193 delete frames_
[0].ack_frame
;
197 EXPECT_CALL(framer_visitor_
, OnPacket());
198 EXPECT_CALL(framer_visitor_
, OnUnauthenticatedPublicHeader(_
));
199 EXPECT_CALL(framer_visitor_
, OnUnauthenticatedHeader(_
));
200 EXPECT_CALL(framer_visitor_
, OnPacketHeader(_
));
201 EXPECT_CALL(framer_visitor_
, OnAckFrame(_
));
202 EXPECT_CALL(framer_visitor_
, OnPacketComplete());
204 ProcessPacket(serialized
.packet
);
205 delete serialized
.packet
;
208 TEST_F(QuicPacketCreatorTest
, SerializeWithFECChangingSequenceNumberLength
) {
209 creator_
.options()->max_packets_per_fec_group
= 6;
210 ASSERT_FALSE(creator_
.ShouldSendFec(false));
212 frames_
.push_back(QuicFrame(new QuicAckFrame(0u, QuicTime::Zero(), 0u)));
213 creator_
.AddSavedFrame(frames_
[0]);
214 // Change the sequence number length mid-FEC group and it should not change.
215 creator_
.options()->send_sequence_number_length
=
216 PACKET_4BYTE_SEQUENCE_NUMBER
;
217 SerializedPacket serialized
= creator_
.SerializePacket();
218 EXPECT_EQ(PACKET_1BYTE_SEQUENCE_NUMBER
, serialized
.sequence_number_length
);
222 EXPECT_CALL(framer_visitor_
, OnPacket());
223 EXPECT_CALL(framer_visitor_
, OnUnauthenticatedPublicHeader(_
));
224 EXPECT_CALL(framer_visitor_
, OnUnauthenticatedHeader(_
));
225 EXPECT_CALL(framer_visitor_
, OnPacketHeader(_
));
226 EXPECT_CALL(framer_visitor_
, OnFecProtectedPayload(_
));
227 EXPECT_CALL(framer_visitor_
, OnAckFrame(_
));
228 EXPECT_CALL(framer_visitor_
, OnPacketComplete());
230 ProcessPacket(serialized
.packet
);
231 delete serialized
.packet
;
233 ASSERT_FALSE(creator_
.ShouldSendFec(false));
234 ASSERT_TRUE(creator_
.ShouldSendFec(true));
236 serialized
= creator_
.SerializeFec();
237 EXPECT_EQ(PACKET_1BYTE_SEQUENCE_NUMBER
, serialized
.sequence_number_length
);
238 ASSERT_EQ(2u, serialized
.sequence_number
);
242 EXPECT_CALL(framer_visitor_
, OnPacket());
243 EXPECT_CALL(framer_visitor_
, OnUnauthenticatedPublicHeader(_
));
244 EXPECT_CALL(framer_visitor_
, OnUnauthenticatedHeader(_
));
245 EXPECT_CALL(framer_visitor_
, OnPacketHeader(_
));
246 EXPECT_CALL(framer_visitor_
, OnFecData(_
));
247 EXPECT_CALL(framer_visitor_
, OnPacketComplete());
249 ProcessPacket(serialized
.packet
);
250 delete serialized
.packet
;
252 // Ensure the next FEC group starts using the new sequence number length.
253 serialized
= creator_
.SerializeAllFrames(frames_
);
254 EXPECT_EQ(PACKET_4BYTE_SEQUENCE_NUMBER
, serialized
.sequence_number_length
);
255 delete frames_
[0].ack_frame
;
256 delete serialized
.packet
;
259 TEST_F(QuicPacketCreatorTest
, ReserializeFramesWithSequenceNumberLength
) {
260 // If the original packet sequence number length, the current sequence number
261 // length, and the configured send sequence number length are different, the
262 // retransmit must sent with the original length and the others do not change.
263 creator_
.options()->send_sequence_number_length
=
264 PACKET_4BYTE_SEQUENCE_NUMBER
;
265 QuicPacketCreatorPeer::SetSequenceNumberLength(&creator_
,
266 PACKET_2BYTE_SEQUENCE_NUMBER
);
267 frames_
.push_back(QuicFrame(new QuicStreamFrame(0u, false, 0u, IOVector())));
268 SerializedPacket serialized
=
269 creator_
.ReserializeAllFrames(frames_
, PACKET_1BYTE_SEQUENCE_NUMBER
);
270 EXPECT_EQ(PACKET_4BYTE_SEQUENCE_NUMBER
,
271 creator_
.options()->send_sequence_number_length
);
272 EXPECT_EQ(PACKET_2BYTE_SEQUENCE_NUMBER
,
273 QuicPacketCreatorPeer::GetSequenceNumberLength(&creator_
));
274 EXPECT_EQ(PACKET_1BYTE_SEQUENCE_NUMBER
, serialized
.sequence_number_length
);
275 delete frames_
[0].stream_frame
;
279 EXPECT_CALL(framer_visitor_
, OnPacket());
280 EXPECT_CALL(framer_visitor_
, OnUnauthenticatedPublicHeader(_
));
281 EXPECT_CALL(framer_visitor_
, OnUnauthenticatedHeader(_
));
282 EXPECT_CALL(framer_visitor_
, OnPacketHeader(_
));
283 EXPECT_CALL(framer_visitor_
, OnStreamFrame(_
));
284 EXPECT_CALL(framer_visitor_
, OnPacketComplete());
286 ProcessPacket(serialized
.packet
);
287 delete serialized
.packet
;
290 TEST_F(QuicPacketCreatorTest
, SerializeConnectionClose
) {
291 QuicConnectionCloseFrame frame
;
292 frame
.error_code
= QUIC_NO_ERROR
;
293 frame
.error_details
= "error";
295 SerializedPacket serialized
= creator_
.SerializeConnectionClose(&frame
);
296 ASSERT_EQ(1u, serialized
.sequence_number
);
297 ASSERT_EQ(1u, creator_
.sequence_number());
300 EXPECT_CALL(framer_visitor_
, OnPacket());
301 EXPECT_CALL(framer_visitor_
, OnUnauthenticatedPublicHeader(_
));
302 EXPECT_CALL(framer_visitor_
, OnUnauthenticatedHeader(_
));
303 EXPECT_CALL(framer_visitor_
, OnPacketHeader(_
));
304 EXPECT_CALL(framer_visitor_
, OnConnectionCloseFrame(_
));
305 EXPECT_CALL(framer_visitor_
, OnPacketComplete());
307 ProcessPacket(serialized
.packet
);
308 delete serialized
.packet
;
311 TEST_F(QuicPacketCreatorTest
, CreateStreamFrame
) {
313 size_t consumed
= creator_
.CreateStreamFrame(1u, MakeIOVector("test"), 0u,
315 EXPECT_EQ(4u, consumed
);
316 CheckStreamFrame(frame
, 1u, "test", 0u, false);
317 delete frame
.stream_frame
;
320 TEST_F(QuicPacketCreatorTest
, CreateStreamFrameFin
) {
322 size_t consumed
= creator_
.CreateStreamFrame(1u, MakeIOVector("test"), 10u,
324 EXPECT_EQ(4u, consumed
);
325 CheckStreamFrame(frame
, 1u, "test", 10u, true);
326 delete frame
.stream_frame
;
329 TEST_F(QuicPacketCreatorTest
, CreateStreamFrameFinOnly
) {
331 size_t consumed
= creator_
.CreateStreamFrame(1u, IOVector(), 0u, true,
333 EXPECT_EQ(0u, consumed
);
334 CheckStreamFrame(frame
, 1u, string(), 0u, true);
335 delete frame
.stream_frame
;
338 TEST_F(QuicPacketCreatorTest
, CreateAllFreeBytesForStreamFrames
) {
339 const size_t overhead
= GetPacketHeaderOverhead() + GetEncryptionOverhead();
340 for (size_t i
= overhead
; i
< overhead
+ 100; ++i
) {
341 creator_
.options()->max_packet_length
= i
;
342 const bool should_have_room
= i
> overhead
+ GetStreamFrameOverhead();
343 ASSERT_EQ(should_have_room
,
344 creator_
.HasRoomForStreamFrame(kStreamId
, kOffset
));
345 if (should_have_room
) {
347 size_t bytes_consumed
= creator_
.CreateStreamFrame(
348 kStreamId
, MakeIOVector("testdata"), kOffset
, false, &frame
);
349 EXPECT_LT(0u, bytes_consumed
);
350 ASSERT_TRUE(creator_
.AddSavedFrame(frame
));
351 SerializedPacket serialized_packet
= creator_
.SerializePacket();
352 ASSERT_TRUE(serialized_packet
.packet
);
353 delete serialized_packet
.packet
;
354 delete serialized_packet
.retransmittable_frames
;
359 TEST_F(QuicPacketCreatorTest
, StreamFrameConsumption
) {
360 // Compute the total overhead for a single frame in packet.
361 const size_t overhead
= GetPacketHeaderOverhead() + GetEncryptionOverhead()
362 + GetStreamFrameOverhead();
363 size_t capacity
= kDefaultMaxPacketSize
- overhead
;
364 // Now, test various sizes around this size.
365 for (int delta
= -5; delta
<= 5; ++delta
) {
366 string
data(capacity
+ delta
, 'A');
367 size_t bytes_free
= delta
> 0 ? 0 : 0 - delta
;
369 size_t bytes_consumed
= creator_
.CreateStreamFrame(
370 kStreamId
, MakeIOVector(data
), kOffset
, false, &frame
);
371 EXPECT_EQ(capacity
- bytes_free
, bytes_consumed
);
373 ASSERT_TRUE(creator_
.AddSavedFrame(frame
));
374 // BytesFree() returns bytes available for the next frame, which will
375 // be two bytes smaller since the stream frame would need to be grown.
376 size_t expected_bytes_free
= bytes_free
< 3 ? 0 : bytes_free
- 2;
377 EXPECT_EQ(expected_bytes_free
, creator_
.BytesFree()) << "delta: " << delta
;
378 SerializedPacket serialized_packet
= creator_
.SerializePacket();
379 ASSERT_TRUE(serialized_packet
.packet
);
380 delete serialized_packet
.packet
;
381 delete serialized_packet
.retransmittable_frames
;
385 TEST_F(QuicPacketCreatorTest
, CryptoStreamFramePacketPadding
) {
386 // Compute the total overhead for a single frame in packet.
387 const size_t overhead
= GetPacketHeaderOverhead() + GetEncryptionOverhead()
388 + GetStreamFrameOverhead();
389 ASSERT_GT(kMaxPacketSize
, overhead
);
390 size_t capacity
= kDefaultMaxPacketSize
- overhead
;
391 // Now, test various sizes around this size.
392 for (int delta
= -5; delta
<= 5; ++delta
) {
393 string
data(capacity
+ delta
, 'A');
394 size_t bytes_free
= delta
> 0 ? 0 : 0 - delta
;
397 size_t bytes_consumed
= creator_
.CreateStreamFrame(
398 kStreamId
, MakeIOVector(data
), kOffset
, false, &frame
);
399 EXPECT_LT(0u, bytes_consumed
);
400 ASSERT_TRUE(creator_
.AddSavedFrame(frame
));
401 SerializedPacket serialized_packet
= creator_
.SerializePacket();
402 ASSERT_TRUE(serialized_packet
.packet
);
403 // If there is not enough space in the packet to fit a padding frame
404 // (1 byte) and to expand the stream frame (another 2 bytes) the packet
405 // will not be padded.
406 if (bytes_free
< 3) {
407 EXPECT_EQ(client_framer_
.GetMaxPlaintextSize(kDefaultMaxPacketSize
)
408 - bytes_free
, serialized_packet
.packet
->length());
410 EXPECT_EQ(client_framer_
.GetMaxPlaintextSize(kDefaultMaxPacketSize
),
411 serialized_packet
.packet
->length());
413 delete serialized_packet
.packet
;
414 delete serialized_packet
.retransmittable_frames
;
418 TEST_F(QuicPacketCreatorTest
, NonCryptoStreamFramePacketNonPadding
) {
419 // Compute the total overhead for a single frame in packet.
420 const size_t overhead
= GetPacketHeaderOverhead() + GetEncryptionOverhead()
421 + GetStreamFrameOverhead();
422 ASSERT_GT(kDefaultMaxPacketSize
, overhead
);
423 size_t capacity
= kDefaultMaxPacketSize
- overhead
;
424 // Now, test various sizes around this size.
425 for (int delta
= -5; delta
<= 5; ++delta
) {
426 string
data(capacity
+ delta
, 'A');
427 size_t bytes_free
= delta
> 0 ? 0 : 0 - delta
;
430 size_t bytes_consumed
= creator_
.CreateStreamFrame(
431 kStreamId
+ 2, MakeIOVector(data
), kOffset
, false, &frame
);
432 EXPECT_LT(0u, bytes_consumed
);
433 ASSERT_TRUE(creator_
.AddSavedFrame(frame
));
434 SerializedPacket serialized_packet
= creator_
.SerializePacket();
435 ASSERT_TRUE(serialized_packet
.packet
);
436 if (bytes_free
> 0) {
437 EXPECT_EQ(client_framer_
.GetMaxPlaintextSize(kDefaultMaxPacketSize
)
438 - bytes_free
, serialized_packet
.packet
->length());
440 EXPECT_EQ(client_framer_
.GetMaxPlaintextSize(kDefaultMaxPacketSize
),
441 serialized_packet
.packet
->length());
443 delete serialized_packet
.packet
;
444 delete serialized_packet
.retransmittable_frames
;
448 TEST_F(QuicPacketCreatorTest
, SerializeVersionNegotiationPacket
) {
449 QuicPacketCreatorPeer::SetIsServer(&creator_
, true);
450 QuicVersionVector versions
;
451 versions
.push_back(test::QuicVersionMax());
452 scoped_ptr
<QuicEncryptedPacket
> encrypted(
453 creator_
.SerializeVersionNegotiationPacket(versions
));
457 EXPECT_CALL(framer_visitor_
, OnPacket());
458 EXPECT_CALL(framer_visitor_
, OnUnauthenticatedPublicHeader(_
));
459 EXPECT_CALL(framer_visitor_
, OnVersionNegotiationPacket(_
));
461 client_framer_
.ProcessPacket(*encrypted
.get());
464 TEST_F(QuicPacketCreatorTest
, UpdatePacketSequenceNumberLengthLeastAwaiting
) {
465 EXPECT_EQ(PACKET_1BYTE_SEQUENCE_NUMBER
,
466 creator_
.options()->send_sequence_number_length
);
468 creator_
.set_sequence_number(64);
469 creator_
.UpdateSequenceNumberLength(2, 10000);
470 EXPECT_EQ(PACKET_1BYTE_SEQUENCE_NUMBER
,
471 creator_
.options()->send_sequence_number_length
);
473 creator_
.set_sequence_number(64 * 256);
474 creator_
.UpdateSequenceNumberLength(2, 10000);
475 EXPECT_EQ(PACKET_2BYTE_SEQUENCE_NUMBER
,
476 creator_
.options()->send_sequence_number_length
);
478 creator_
.set_sequence_number(64 * 256 * 256);
479 creator_
.UpdateSequenceNumberLength(2, 10000);
480 EXPECT_EQ(PACKET_4BYTE_SEQUENCE_NUMBER
,
481 creator_
.options()->send_sequence_number_length
);
483 creator_
.set_sequence_number(GG_UINT64_C(64) * 256 * 256 * 256 * 256);
484 creator_
.UpdateSequenceNumberLength(2, 10000);
485 EXPECT_EQ(PACKET_6BYTE_SEQUENCE_NUMBER
,
486 creator_
.options()->send_sequence_number_length
);
489 TEST_F(QuicPacketCreatorTest
, UpdatePacketSequenceNumberLengthBandwidth
) {
490 EXPECT_EQ(PACKET_1BYTE_SEQUENCE_NUMBER
,
491 creator_
.options()->send_sequence_number_length
);
493 creator_
.UpdateSequenceNumberLength(1, 10000);
494 EXPECT_EQ(PACKET_1BYTE_SEQUENCE_NUMBER
,
495 creator_
.options()->send_sequence_number_length
);
497 creator_
.UpdateSequenceNumberLength(1, 10000 * 256);
498 EXPECT_EQ(PACKET_2BYTE_SEQUENCE_NUMBER
,
499 creator_
.options()->send_sequence_number_length
);
501 creator_
.UpdateSequenceNumberLength(1, 10000 * 256 * 256);
502 EXPECT_EQ(PACKET_4BYTE_SEQUENCE_NUMBER
,
503 creator_
.options()->send_sequence_number_length
);
505 creator_
.UpdateSequenceNumberLength(
506 1, GG_UINT64_C(1000) * 256 * 256 * 256 * 256);
507 EXPECT_EQ(PACKET_6BYTE_SEQUENCE_NUMBER
,
508 creator_
.options()->send_sequence_number_length
);
511 TEST_F(QuicPacketCreatorTest
, CreateStreamFrameWithNotifier
) {
512 // Ensure that if CreateStreamFrame does not consume any data (e.g. a FIN only
513 // frame) then any QuicAckNotifier that is passed in still gets attached to
515 scoped_refptr
<MockAckNotifierDelegate
> delegate(new MockAckNotifierDelegate
);
516 QuicAckNotifier
notifier(delegate
.get());
518 IOVector empty_iovector
;
520 size_t consumed_bytes
= creator_
.CreateStreamFrameWithNotifier(
521 1u, empty_iovector
, 0u, fin
, ¬ifier
, &frame
);
522 EXPECT_EQ(0u, consumed_bytes
);
523 EXPECT_EQ(¬ifier
, frame
.stream_frame
->notifier
);
524 delete frame
.stream_frame
;
527 INSTANTIATE_TEST_CASE_P(ToggleVersionSerialization
,
528 QuicPacketCreatorTest
,
529 ::testing::Values(false, true));
531 TEST_P(QuicPacketCreatorTest
, SerializeFrame
) {
533 creator_
.StopSendingVersion();
535 frames_
.push_back(QuicFrame(new QuicStreamFrame(0u, false, 0u, IOVector())));
536 SerializedPacket serialized
= creator_
.SerializeAllFrames(frames_
);
537 delete frames_
[0].stream_frame
;
539 QuicPacketHeader header
;
542 EXPECT_CALL(framer_visitor_
, OnPacket());
543 EXPECT_CALL(framer_visitor_
, OnUnauthenticatedPublicHeader(_
));
544 EXPECT_CALL(framer_visitor_
, OnUnauthenticatedHeader(_
));
545 EXPECT_CALL(framer_visitor_
, OnPacketHeader(_
)).WillOnce(
546 DoAll(SaveArg
<0>(&header
), Return(true)));
547 EXPECT_CALL(framer_visitor_
, OnStreamFrame(_
));
548 EXPECT_CALL(framer_visitor_
, OnPacketComplete());
550 ProcessPacket(serialized
.packet
);
551 EXPECT_EQ(GetParam(), header
.public_header
.version_flag
);
552 delete serialized
.packet
;
555 TEST_P(QuicPacketCreatorTest
, CreateStreamFrameTooLarge
) {
557 creator_
.StopSendingVersion();
559 // A string larger than fits into a frame.
560 size_t payload_length
;
561 creator_
.options()->max_packet_length
= GetPacketLengthForOneStream(
562 client_framer_
.version(),
563 QuicPacketCreatorPeer::SendVersionInPacket(&creator_
),
564 PACKET_1BYTE_SEQUENCE_NUMBER
, NOT_IN_FEC_GROUP
, &payload_length
);
566 const string
too_long_payload(payload_length
* 2, 'a');
567 size_t consumed
= creator_
.CreateStreamFrame(
568 1u, MakeIOVector(too_long_payload
), 0u, true, &frame
);
569 EXPECT_EQ(payload_length
, consumed
);
570 const string
payload(payload_length
, 'a');
571 CheckStreamFrame(frame
, 1u, payload
, 0u, false);
572 delete frame
.stream_frame
;
575 TEST_P(QuicPacketCreatorTest
, AddFrameAndSerialize
) {
577 creator_
.StopSendingVersion();
579 const size_t max_plaintext_size
=
580 client_framer_
.GetMaxPlaintextSize(creator_
.options()->max_packet_length
);
581 EXPECT_FALSE(creator_
.HasPendingFrames());
582 EXPECT_EQ(max_plaintext_size
-
584 creator_
.options()->send_guid_length
,
585 QuicPacketCreatorPeer::SendVersionInPacket(&creator_
),
586 PACKET_1BYTE_SEQUENCE_NUMBER
, NOT_IN_FEC_GROUP
),
587 creator_
.BytesFree());
589 // Add a variety of frame types and then a padding frame.
590 QuicAckFrame
ack_frame(0u, QuicTime::Zero(), 0u);
591 EXPECT_TRUE(creator_
.AddSavedFrame(QuicFrame(&ack_frame
)));
592 EXPECT_TRUE(creator_
.HasPendingFrames());
594 QuicCongestionFeedbackFrame congestion_feedback
;
595 congestion_feedback
.type
= kFixRate
;
596 EXPECT_TRUE(creator_
.AddSavedFrame(QuicFrame(&congestion_feedback
)));
597 EXPECT_TRUE(creator_
.HasPendingFrames());
600 size_t consumed
= creator_
.CreateStreamFrame(
601 1u, MakeIOVector("test"), 0u, false, &frame
);
602 EXPECT_EQ(4u, consumed
);
603 ASSERT_TRUE(frame
.stream_frame
);
604 EXPECT_TRUE(creator_
.AddSavedFrame(frame
));
605 EXPECT_TRUE(creator_
.HasPendingFrames());
607 QuicPaddingFrame padding_frame
;
608 EXPECT_TRUE(creator_
.AddSavedFrame(QuicFrame(&padding_frame
)));
609 EXPECT_TRUE(creator_
.HasPendingFrames());
610 EXPECT_EQ(0u, creator_
.BytesFree());
612 EXPECT_FALSE(creator_
.AddSavedFrame(QuicFrame(&ack_frame
)));
614 // Ensure the packet is successfully created.
615 SerializedPacket serialized
= creator_
.SerializePacket();
616 ASSERT_TRUE(serialized
.packet
);
617 delete serialized
.packet
;
618 ASSERT_TRUE(serialized
.retransmittable_frames
);
619 RetransmittableFrames
* retransmittable
= serialized
.retransmittable_frames
;
620 ASSERT_EQ(1u, retransmittable
->frames().size());
621 EXPECT_EQ(STREAM_FRAME
, retransmittable
->frames()[0].type
);
622 ASSERT_TRUE(retransmittable
->frames()[0].stream_frame
);
623 delete serialized
.retransmittable_frames
;
625 EXPECT_FALSE(creator_
.HasPendingFrames());
626 EXPECT_EQ(max_plaintext_size
-
628 creator_
.options()->send_guid_length
,
629 QuicPacketCreatorPeer::SendVersionInPacket(&creator_
),
630 PACKET_1BYTE_SEQUENCE_NUMBER
,
632 creator_
.BytesFree());
635 TEST_F(QuicPacketCreatorTest
, EntropyFlag
) {
636 frames_
.push_back(QuicFrame(new QuicStreamFrame(0u, false, 0u, IOVector())));
638 for (int i
= 0; i
< 2; ++i
) {
639 for (int j
= 0; j
< 64; ++j
) {
640 SerializedPacket serialized
= creator_
.SerializeAllFrames(frames_
);
641 // Verify both BoolSource and hash algorithm.
642 bool expected_rand_bool
=
643 (mock_random_
.RandUint64() & (GG_UINT64_C(1) << j
)) != 0;
644 bool observed_rand_bool
=
645 (serialized
.entropy_hash
& (1 << ((j
+1) % 8))) != 0;
646 uint8 rest_of_hash
= serialized
.entropy_hash
& ~(1 << ((j
+1) % 8));
647 EXPECT_EQ(expected_rand_bool
, observed_rand_bool
);
648 EXPECT_EQ(0, rest_of_hash
);
649 delete serialized
.packet
;
651 // After 64 calls, BoolSource will refresh the bucket - make sure it does.
652 mock_random_
.ChangeValue();
655 delete frames_
[0].stream_frame
;