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_connection.h"
9 #include "base/basictypes.h"
10 #include "base/bind.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/stl_util.h"
13 #include "net/base/net_errors.h"
14 #include "net/quic/congestion_control/loss_detection_interface.h"
15 #include "net/quic/congestion_control/send_algorithm_interface.h"
16 #include "net/quic/crypto/null_encrypter.h"
17 #include "net/quic/crypto/quic_decrypter.h"
18 #include "net/quic/crypto/quic_encrypter.h"
19 #include "net/quic/quic_ack_notifier.h"
20 #include "net/quic/quic_flags.h"
21 #include "net/quic/quic_protocol.h"
22 #include "net/quic/quic_utils.h"
23 #include "net/quic/test_tools/mock_clock.h"
24 #include "net/quic/test_tools/mock_random.h"
25 #include "net/quic/test_tools/quic_config_peer.h"
26 #include "net/quic/test_tools/quic_connection_peer.h"
27 #include "net/quic/test_tools/quic_framer_peer.h"
28 #include "net/quic/test_tools/quic_packet_creator_peer.h"
29 #include "net/quic/test_tools/quic_packet_generator_peer.h"
30 #include "net/quic/test_tools/quic_sent_packet_manager_peer.h"
31 #include "net/quic/test_tools/quic_test_utils.h"
32 #include "net/quic/test_tools/simple_quic_framer.h"
33 #include "net/test/gtest_util.h"
34 #include "testing/gmock/include/gmock/gmock.h"
35 #include "testing/gtest/include/gtest/gtest.h"
37 using base::StringPiece
;
42 using testing::AnyNumber
;
43 using testing::AtLeast
;
44 using testing::ContainerEq
;
45 using testing::Contains
;
47 using testing::InSequence
;
48 using testing::InvokeWithoutArgs
;
49 using testing::NiceMock
;
51 using testing::Return
;
52 using testing::SaveArg
;
53 using testing::StrictMock
;
60 const char data1
[] = "foo";
61 const char data2
[] = "bar";
63 const bool kFin
= true;
64 const bool kEntropyFlag
= true;
66 const QuicPacketEntropyHash kTestEntropyHash
= 76;
68 const int kDefaultRetransmissionTimeMs
= 500;
70 // TaggingEncrypter appends kTagSize bytes of |tag| to the end of each message.
71 class TaggingEncrypter
: public QuicEncrypter
{
73 explicit TaggingEncrypter(uint8 tag
)
77 ~TaggingEncrypter() override
{}
79 // QuicEncrypter interface.
80 bool SetKey(StringPiece key
) override
{ return true; }
82 bool SetNoncePrefix(StringPiece nonce_prefix
) override
{ return true; }
84 bool EncryptPacket(QuicPacketSequenceNumber sequence_number
,
85 StringPiece associated_data
,
86 StringPiece plaintext
,
88 size_t* output_length
,
89 size_t max_output_length
) override
{
90 const size_t len
= plaintext
.size() + kTagSize
;
91 if (max_output_length
< len
) {
94 memcpy(output
, plaintext
.data(), plaintext
.size());
95 output
+= plaintext
.size();
96 memset(output
, tag_
, kTagSize
);
101 size_t GetKeySize() const override
{ return 0; }
102 size_t GetNoncePrefixSize() const override
{ return 0; }
104 size_t GetMaxPlaintextSize(size_t ciphertext_size
) const override
{
105 return ciphertext_size
- kTagSize
;
108 size_t GetCiphertextSize(size_t plaintext_size
) const override
{
109 return plaintext_size
+ kTagSize
;
112 StringPiece
GetKey() const override
{ return StringPiece(); }
114 StringPiece
GetNoncePrefix() const override
{ return StringPiece(); }
123 DISALLOW_COPY_AND_ASSIGN(TaggingEncrypter
);
126 // TaggingDecrypter ensures that the final kTagSize bytes of the message all
127 // have the same value and then removes them.
128 class TaggingDecrypter
: public QuicDecrypter
{
130 ~TaggingDecrypter() override
{}
132 // QuicDecrypter interface
133 bool SetKey(StringPiece key
) override
{ return true; }
135 bool SetNoncePrefix(StringPiece nonce_prefix
) override
{ return true; }
137 bool DecryptPacket(QuicPacketSequenceNumber sequence_number
,
138 const StringPiece
& associated_data
,
139 const StringPiece
& ciphertext
,
141 size_t* output_length
,
142 size_t max_output_length
) override
{
143 if (ciphertext
.size() < kTagSize
) {
146 if (!CheckTag(ciphertext
, GetTag(ciphertext
))) {
149 *output_length
= ciphertext
.size() - kTagSize
;
150 memcpy(output
, ciphertext
.data(), *output_length
);
154 StringPiece
GetKey() const override
{ return StringPiece(); }
155 StringPiece
GetNoncePrefix() const override
{ return StringPiece(); }
158 virtual uint8
GetTag(StringPiece ciphertext
) {
159 return ciphertext
.data()[ciphertext
.size()-1];
167 bool CheckTag(StringPiece ciphertext
, uint8 tag
) {
168 for (size_t i
= ciphertext
.size() - kTagSize
; i
< ciphertext
.size(); i
++) {
169 if (ciphertext
.data()[i
] != tag
) {
178 // StringTaggingDecrypter ensures that the final kTagSize bytes of the message
179 // match the expected value.
180 class StrictTaggingDecrypter
: public TaggingDecrypter
{
182 explicit StrictTaggingDecrypter(uint8 tag
) : tag_(tag
) {}
183 ~StrictTaggingDecrypter() override
{}
185 // TaggingQuicDecrypter
186 uint8
GetTag(StringPiece ciphertext
) override
{ return tag_
; }
192 class TestConnectionHelper
: public QuicConnectionHelperInterface
{
194 class TestAlarm
: public QuicAlarm
{
196 explicit TestAlarm(QuicAlarm::Delegate
* delegate
)
197 : QuicAlarm(delegate
) {
200 void SetImpl() override
{}
201 void CancelImpl() override
{}
202 using QuicAlarm::Fire
;
205 TestConnectionHelper(MockClock
* clock
, MockRandom
* random_generator
)
207 random_generator_(random_generator
) {
208 clock_
->AdvanceTime(QuicTime::Delta::FromSeconds(1));
211 // QuicConnectionHelperInterface
212 const QuicClock
* GetClock() const override
{ return clock_
; }
214 QuicRandom
* GetRandomGenerator() override
{ return random_generator_
; }
216 QuicAlarm
* CreateAlarm(QuicAlarm::Delegate
* delegate
) override
{
217 return new TestAlarm(delegate
);
222 MockRandom
* random_generator_
;
224 DISALLOW_COPY_AND_ASSIGN(TestConnectionHelper
);
227 class TestPacketWriter
: public QuicPacketWriter
{
229 TestPacketWriter(QuicVersion version
, MockClock
*clock
)
231 framer_(SupportedVersions(version_
)),
232 last_packet_size_(0),
233 write_blocked_(false),
234 block_on_next_write_(false),
235 is_write_blocked_data_buffered_(false),
236 final_bytes_of_last_packet_(0),
237 final_bytes_of_previous_packet_(0),
238 use_tagging_decrypter_(false),
239 packets_write_attempts_(0),
241 write_pause_time_delta_(QuicTime::Delta::Zero()) {
244 // QuicPacketWriter interface
245 WriteResult
WritePacket(const char* buffer
,
247 const IPAddressNumber
& self_address
,
248 const IPEndPoint
& peer_address
) override
{
249 QuicEncryptedPacket
packet(buffer
, buf_len
);
250 ++packets_write_attempts_
;
252 if (packet
.length() >= sizeof(final_bytes_of_last_packet_
)) {
253 final_bytes_of_previous_packet_
= final_bytes_of_last_packet_
;
254 memcpy(&final_bytes_of_last_packet_
, packet
.data() + packet
.length() - 4,
255 sizeof(final_bytes_of_last_packet_
));
258 if (use_tagging_decrypter_
) {
259 framer_
.framer()->SetDecrypter(ENCRYPTION_NONE
, new TaggingDecrypter
);
261 EXPECT_TRUE(framer_
.ProcessPacket(packet
));
262 if (block_on_next_write_
) {
263 write_blocked_
= true;
264 block_on_next_write_
= false;
266 if (IsWriteBlocked()) {
267 return WriteResult(WRITE_STATUS_BLOCKED
, -1);
269 last_packet_size_
= packet
.length();
271 if (!write_pause_time_delta_
.IsZero()) {
272 clock_
->AdvanceTime(write_pause_time_delta_
);
274 return WriteResult(WRITE_STATUS_OK
, last_packet_size_
);
277 bool IsWriteBlockedDataBuffered() const override
{
278 return is_write_blocked_data_buffered_
;
281 bool IsWriteBlocked() const override
{ return write_blocked_
; }
283 void SetWritable() override
{ write_blocked_
= false; }
285 void BlockOnNextWrite() { block_on_next_write_
= true; }
287 // Sets the amount of time that the writer should before the actual write.
288 void SetWritePauseTimeDelta(QuicTime::Delta delta
) {
289 write_pause_time_delta_
= delta
;
292 const QuicPacketHeader
& header() { return framer_
.header(); }
294 size_t frame_count() const { return framer_
.num_frames(); }
296 const vector
<QuicAckFrame
>& ack_frames() const {
297 return framer_
.ack_frames();
300 const vector
<QuicStopWaitingFrame
>& stop_waiting_frames() const {
301 return framer_
.stop_waiting_frames();
304 const vector
<QuicConnectionCloseFrame
>& connection_close_frames() const {
305 return framer_
.connection_close_frames();
308 const vector
<QuicRstStreamFrame
>& rst_stream_frames() const {
309 return framer_
.rst_stream_frames();
312 const vector
<QuicStreamFrame
>& stream_frames() const {
313 return framer_
.stream_frames();
316 const vector
<QuicPingFrame
>& ping_frames() const {
317 return framer_
.ping_frames();
320 size_t last_packet_size() {
321 return last_packet_size_
;
324 const QuicVersionNegotiationPacket
* version_negotiation_packet() {
325 return framer_
.version_negotiation_packet();
328 void set_is_write_blocked_data_buffered(bool buffered
) {
329 is_write_blocked_data_buffered_
= buffered
;
332 void set_perspective(Perspective perspective
) {
333 // We invert perspective here, because the framer needs to parse packets
335 perspective
= perspective
== Perspective::IS_CLIENT
336 ? Perspective::IS_SERVER
337 : Perspective::IS_CLIENT
;
338 QuicFramerPeer::SetPerspective(framer_
.framer(), perspective
);
341 // final_bytes_of_last_packet_ returns the last four bytes of the previous
342 // packet as a little-endian, uint32. This is intended to be used with a
343 // TaggingEncrypter so that tests can determine which encrypter was used for
345 uint32
final_bytes_of_last_packet() { return final_bytes_of_last_packet_
; }
347 // Returns the final bytes of the second to last packet.
348 uint32
final_bytes_of_previous_packet() {
349 return final_bytes_of_previous_packet_
;
352 void use_tagging_decrypter() {
353 use_tagging_decrypter_
= true;
356 uint32
packets_write_attempts() { return packets_write_attempts_
; }
358 void Reset() { framer_
.Reset(); }
360 void SetSupportedVersions(const QuicVersionVector
& versions
) {
361 framer_
.SetSupportedVersions(versions
);
365 QuicVersion version_
;
366 SimpleQuicFramer framer_
;
367 size_t last_packet_size_
;
369 bool block_on_next_write_
;
370 bool is_write_blocked_data_buffered_
;
371 uint32 final_bytes_of_last_packet_
;
372 uint32 final_bytes_of_previous_packet_
;
373 bool use_tagging_decrypter_
;
374 uint32 packets_write_attempts_
;
376 // If non-zero, the clock will pause during WritePacket for this amount of
378 QuicTime::Delta write_pause_time_delta_
;
380 DISALLOW_COPY_AND_ASSIGN(TestPacketWriter
);
383 class TestConnection
: public QuicConnection
{
385 TestConnection(QuicConnectionId connection_id
,
387 TestConnectionHelper
* helper
,
388 const PacketWriterFactory
& factory
,
389 Perspective perspective
,
391 : QuicConnection(connection_id
,
395 /* owns_writer= */ false,
397 /* is_secure= */ false,
398 SupportedVersions(version
)) {
399 // Disable tail loss probes for most tests.
400 QuicSentPacketManagerPeer::SetMaxTailLossProbes(
401 QuicConnectionPeer::GetSentPacketManager(this), 0);
402 writer()->set_perspective(perspective
);
406 QuicConnectionPeer::SendAck(this);
409 void SetSendAlgorithm(SendAlgorithmInterface
* send_algorithm
) {
410 QuicConnectionPeer::SetSendAlgorithm(this, send_algorithm
);
413 void SetLossAlgorithm(LossDetectionInterface
* loss_algorithm
) {
414 QuicSentPacketManagerPeer::SetLossAlgorithm(
415 QuicConnectionPeer::GetSentPacketManager(this), loss_algorithm
);
418 void SendPacket(EncryptionLevel level
,
419 QuicPacketSequenceNumber sequence_number
,
421 QuicPacketEntropyHash entropy_hash
,
422 HasRetransmittableData retransmittable
) {
423 RetransmittableFrames
* retransmittable_frames
=
424 retransmittable
== HAS_RETRANSMITTABLE_DATA
425 ? new RetransmittableFrames(ENCRYPTION_NONE
)
427 char buffer
[kMaxPacketSize
];
428 QuicEncryptedPacket
* encrypted
=
429 QuicConnectionPeer::GetFramer(this)->EncryptPayload(
430 ENCRYPTION_NONE
, sequence_number
, *packet
, buffer
, kMaxPacketSize
);
432 OnSerializedPacket(SerializedPacket(sequence_number
,
433 PACKET_6BYTE_SEQUENCE_NUMBER
, encrypted
,
434 entropy_hash
, retransmittable_frames
));
437 QuicConsumedData
SendStreamDataWithString(
440 QuicStreamOffset offset
,
442 QuicAckNotifier::DelegateInterface
* delegate
) {
443 return SendStreamDataWithStringHelper(id
, data
, offset
, fin
,
444 MAY_FEC_PROTECT
, delegate
);
447 QuicConsumedData
SendStreamDataWithStringWithFec(
450 QuicStreamOffset offset
,
452 QuicAckNotifier::DelegateInterface
* delegate
) {
453 return SendStreamDataWithStringHelper(id
, data
, offset
, fin
,
454 MUST_FEC_PROTECT
, delegate
);
457 QuicConsumedData
SendStreamDataWithStringHelper(
460 QuicStreamOffset offset
,
462 FecProtection fec_protection
,
463 QuicAckNotifier::DelegateInterface
* delegate
) {
465 QuicIOVector
data_iov(MakeIOVector(data
, &iov
));
466 return QuicConnection::SendStreamData(id
, data_iov
, offset
, fin
,
467 fec_protection
, delegate
);
470 QuicConsumedData
SendStreamData3() {
471 return SendStreamDataWithString(kClientDataStreamId1
, "food", 0, !kFin
,
475 QuicConsumedData
SendStreamData3WithFec() {
476 return SendStreamDataWithStringWithFec(kClientDataStreamId1
, "food", 0,
480 QuicConsumedData
SendStreamData5() {
481 return SendStreamDataWithString(kClientDataStreamId2
, "food2", 0, !kFin
,
485 QuicConsumedData
SendStreamData5WithFec() {
486 return SendStreamDataWithStringWithFec(kClientDataStreamId2
, "food2", 0,
489 // Ensures the connection can write stream data before writing.
490 QuicConsumedData
EnsureWritableAndSendStreamData5() {
491 EXPECT_TRUE(CanWriteStreamData());
492 return SendStreamData5();
495 // The crypto stream has special semantics so that it is not blocked by a
496 // congestion window limitation, and also so that it gets put into a separate
497 // packet (so that it is easier to reason about a crypto frame not being
498 // split needlessly across packet boundaries). As a result, we have separate
499 // tests for some cases for this stream.
500 QuicConsumedData
SendCryptoStreamData() {
501 return SendStreamDataWithString(kCryptoStreamId
, "chlo", 0, !kFin
, nullptr);
504 void set_version(QuicVersion version
) {
505 QuicConnectionPeer::GetFramer(this)->set_version(version
);
508 void SetSupportedVersions(const QuicVersionVector
& versions
) {
509 QuicConnectionPeer::GetFramer(this)->SetSupportedVersions(versions
);
510 writer()->SetSupportedVersions(versions
);
513 void set_perspective(Perspective perspective
) {
514 writer()->set_perspective(perspective
);
515 QuicConnectionPeer::SetPerspective(this, perspective
);
518 TestConnectionHelper::TestAlarm
* GetAckAlarm() {
519 return reinterpret_cast<TestConnectionHelper::TestAlarm
*>(
520 QuicConnectionPeer::GetAckAlarm(this));
523 TestConnectionHelper::TestAlarm
* GetPingAlarm() {
524 return reinterpret_cast<TestConnectionHelper::TestAlarm
*>(
525 QuicConnectionPeer::GetPingAlarm(this));
528 TestConnectionHelper::TestAlarm
* GetFecAlarm() {
529 return reinterpret_cast<TestConnectionHelper::TestAlarm
*>(
530 QuicConnectionPeer::GetFecAlarm(this));
533 TestConnectionHelper::TestAlarm
* GetResumeWritesAlarm() {
534 return reinterpret_cast<TestConnectionHelper::TestAlarm
*>(
535 QuicConnectionPeer::GetResumeWritesAlarm(this));
538 TestConnectionHelper::TestAlarm
* GetRetransmissionAlarm() {
539 return reinterpret_cast<TestConnectionHelper::TestAlarm
*>(
540 QuicConnectionPeer::GetRetransmissionAlarm(this));
543 TestConnectionHelper::TestAlarm
* GetSendAlarm() {
544 return reinterpret_cast<TestConnectionHelper::TestAlarm
*>(
545 QuicConnectionPeer::GetSendAlarm(this));
548 TestConnectionHelper::TestAlarm
* GetTimeoutAlarm() {
549 return reinterpret_cast<TestConnectionHelper::TestAlarm
*>(
550 QuicConnectionPeer::GetTimeoutAlarm(this));
553 using QuicConnection::SelectMutualVersion
;
556 TestPacketWriter
* writer() {
557 return static_cast<TestPacketWriter
*>(QuicConnection::writer());
560 DISALLOW_COPY_AND_ASSIGN(TestConnection
);
563 // Used for testing packets revived from FEC packets.
564 class FecQuicConnectionDebugVisitor
565 : public QuicConnectionDebugVisitor
{
567 void OnRevivedPacket(const QuicPacketHeader
& header
,
568 StringPiece data
) override
{
569 revived_header_
= header
;
572 // Public accessor method.
573 QuicPacketHeader
revived_header() const {
574 return revived_header_
;
578 QuicPacketHeader revived_header_
;
581 class MockPacketWriterFactory
: public QuicConnection::PacketWriterFactory
{
583 explicit MockPacketWriterFactory(QuicPacketWriter
* writer
) {
584 ON_CALL(*this, Create(_
)).WillByDefault(Return(writer
));
586 ~MockPacketWriterFactory() override
{}
588 MOCK_CONST_METHOD1(Create
, QuicPacketWriter
*(QuicConnection
* connection
));
591 // Run tests with combinations of {QuicVersion, fec_send_policy}.
593 TestParams(QuicVersion version
, FecSendPolicy fec_send_policy
)
594 : version(version
), fec_send_policy(fec_send_policy
) {}
596 friend ostream
& operator<<(ostream
& os
, const TestParams
& p
) {
597 os
<< "{ client_version: " << QuicVersionToString(p
.version
)
598 << " fec_send_policy: " << p
.fec_send_policy
<< " }";
603 FecSendPolicy fec_send_policy
;
606 // Constructs various test permutations.
607 vector
<TestParams
> GetTestParams() {
608 vector
<TestParams
> params
;
609 QuicVersionVector all_supported_versions
= QuicSupportedVersions();
610 for (size_t i
= 0; i
< all_supported_versions
.size(); ++i
) {
611 params
.push_back(TestParams(all_supported_versions
[i
], FEC_ANY_TRIGGER
));
612 params
.push_back(TestParams(all_supported_versions
[i
], FEC_ALARM_TRIGGER
));
617 class QuicConnectionTest
: public ::testing::TestWithParam
<TestParams
> {
620 : connection_id_(42),
621 framer_(SupportedVersions(version()),
623 Perspective::IS_CLIENT
),
624 peer_creator_(connection_id_
, &framer_
, &random_generator_
),
625 send_algorithm_(new StrictMock
<MockSendAlgorithm
>),
626 loss_algorithm_(new MockLossAlgorithm()),
627 helper_(new TestConnectionHelper(&clock_
, &random_generator_
)),
628 writer_(new TestPacketWriter(version(), &clock_
)),
629 factory_(writer_
.get()),
630 connection_(connection_id_
,
634 Perspective::IS_CLIENT
,
636 creator_(QuicConnectionPeer::GetPacketCreator(&connection_
)),
637 generator_(QuicConnectionPeer::GetPacketGenerator(&connection_
)),
638 manager_(QuicConnectionPeer::GetSentPacketManager(&connection_
)),
639 frame1_(1, false, 0, StringPiece(data1
)),
640 frame2_(1, false, 3, StringPiece(data2
)),
641 sequence_number_length_(PACKET_6BYTE_SEQUENCE_NUMBER
),
642 connection_id_length_(PACKET_8BYTE_CONNECTION_ID
) {
643 connection_
.set_visitor(&visitor_
);
644 connection_
.SetSendAlgorithm(send_algorithm_
);
645 connection_
.SetLossAlgorithm(loss_algorithm_
);
646 framer_
.set_received_entropy_calculator(&entropy_calculator_
);
647 generator_
->set_fec_send_policy(GetParam().fec_send_policy
);
649 *send_algorithm_
, TimeUntilSend(_
, _
, _
)).WillRepeatedly(Return(
650 QuicTime::Delta::Zero()));
651 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
))
653 EXPECT_CALL(*send_algorithm_
, RetransmissionDelay()).WillRepeatedly(
654 Return(QuicTime::Delta::Zero()));
655 EXPECT_CALL(*send_algorithm_
, GetCongestionWindow()).WillRepeatedly(
656 Return(kMaxPacketSize
));
657 ON_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
))
658 .WillByDefault(Return(true));
659 EXPECT_CALL(*send_algorithm_
, HasReliableBandwidthEstimate())
661 EXPECT_CALL(*send_algorithm_
, BandwidthEstimate())
663 .WillRepeatedly(Return(QuicBandwidth::Zero()));
664 EXPECT_CALL(*send_algorithm_
, InSlowStart()).Times(AnyNumber());
665 EXPECT_CALL(*send_algorithm_
, InRecovery()).Times(AnyNumber());
666 EXPECT_CALL(visitor_
, WillingAndAbleToWrite()).Times(AnyNumber());
667 EXPECT_CALL(visitor_
, HasPendingHandshake()).Times(AnyNumber());
668 EXPECT_CALL(visitor_
, OnCanWrite()).Times(AnyNumber());
669 EXPECT_CALL(visitor_
, HasOpenDynamicStreams())
670 .WillRepeatedly(Return(false));
671 EXPECT_CALL(visitor_
, OnCongestionWindowChange(_
)).Times(AnyNumber());
673 EXPECT_CALL(*loss_algorithm_
, GetLossTimeout())
674 .WillRepeatedly(Return(QuicTime::Zero()));
675 EXPECT_CALL(*loss_algorithm_
, DetectLostPackets(_
, _
, _
, _
))
676 .WillRepeatedly(Return(SequenceNumberSet()));
679 QuicVersion
version() { return GetParam().version
; }
681 QuicAckFrame
* outgoing_ack() {
682 QuicConnectionPeer::PopulateAckFrame(&connection_
, &ack_
);
686 QuicStopWaitingFrame
* stop_waiting() {
687 QuicConnectionPeer::PopulateStopWaitingFrame(&connection_
, &stop_waiting_
);
688 return &stop_waiting_
;
691 QuicPacketSequenceNumber
least_unacked() {
692 if (writer_
->stop_waiting_frames().empty()) {
695 return writer_
->stop_waiting_frames()[0].least_unacked
;
698 void use_tagging_decrypter() {
699 writer_
->use_tagging_decrypter();
702 void ProcessPacket(QuicPacketSequenceNumber number
) {
703 EXPECT_CALL(visitor_
, OnStreamFrames(_
)).Times(1);
704 ProcessDataPacket(number
, 0, !kEntropyFlag
);
707 QuicPacketEntropyHash
ProcessFramePacket(QuicFrame frame
) {
709 frames
.push_back(QuicFrame(frame
));
710 QuicPacketCreatorPeer::SetSendVersionInPacket(
711 &peer_creator_
, connection_
.perspective() == Perspective::IS_SERVER
);
713 char buffer
[kMaxPacketSize
];
714 SerializedPacket serialized_packet
=
715 peer_creator_
.SerializeAllFrames(frames
, buffer
, kMaxPacketSize
);
716 scoped_ptr
<QuicEncryptedPacket
> encrypted(serialized_packet
.packet
);
717 connection_
.ProcessUdpPacket(IPEndPoint(), IPEndPoint(), *encrypted
);
718 return serialized_packet
.entropy_hash
;
721 size_t ProcessDataPacket(QuicPacketSequenceNumber number
,
722 QuicFecGroupNumber fec_group
,
724 return ProcessDataPacketAtLevel(number
, fec_group
, entropy_flag
,
728 size_t ProcessDataPacketAtLevel(QuicPacketSequenceNumber number
,
729 QuicFecGroupNumber fec_group
,
731 EncryptionLevel level
) {
732 scoped_ptr
<QuicPacket
> packet(ConstructDataPacket(number
, fec_group
,
734 char buffer
[kMaxPacketSize
];
735 scoped_ptr
<QuicEncryptedPacket
> encrypted(
736 framer_
.EncryptPayload(level
, number
, *packet
, buffer
, kMaxPacketSize
));
737 connection_
.ProcessUdpPacket(IPEndPoint(), IPEndPoint(), *encrypted
);
738 return encrypted
->length();
741 void ProcessClosePacket(QuicPacketSequenceNumber number
,
742 QuicFecGroupNumber fec_group
) {
743 scoped_ptr
<QuicPacket
> packet(ConstructClosePacket(number
, fec_group
));
744 char buffer
[kMaxPacketSize
];
745 scoped_ptr
<QuicEncryptedPacket
> encrypted(framer_
.EncryptPayload(
746 ENCRYPTION_NONE
, number
, *packet
, buffer
, kMaxPacketSize
));
747 connection_
.ProcessUdpPacket(IPEndPoint(), IPEndPoint(), *encrypted
);
750 size_t ProcessFecProtectedPacket(QuicPacketSequenceNumber number
,
751 bool expect_revival
, bool entropy_flag
) {
752 if (expect_revival
) {
753 EXPECT_CALL(visitor_
, OnStreamFrames(_
)).Times(1);
755 EXPECT_CALL(visitor_
, OnStreamFrames(_
)).Times(1).
756 RetiresOnSaturation();
757 return ProcessDataPacket(number
, 1, entropy_flag
);
760 // Processes an FEC packet that covers the packets that would have been
762 size_t ProcessFecPacket(QuicPacketSequenceNumber number
,
763 QuicPacketSequenceNumber min_protected_packet
,
766 QuicPacket
* packet
) {
767 if (expect_revival
) {
768 EXPECT_CALL(visitor_
, OnStreamFrames(_
)).Times(1);
771 // Construct the decrypted data packet so we can compute the correct
772 // redundancy. If |packet| has been provided then use that, otherwise
773 // construct a default data packet.
774 scoped_ptr
<QuicPacket
> data_packet
;
776 data_packet
.reset(packet
);
778 data_packet
.reset(ConstructDataPacket(number
, 1, !kEntropyFlag
));
781 QuicPacketHeader header
;
782 header
.public_header
.connection_id
= connection_id_
;
783 header
.public_header
.sequence_number_length
= sequence_number_length_
;
784 header
.public_header
.connection_id_length
= connection_id_length_
;
785 header
.packet_sequence_number
= number
;
786 header
.entropy_flag
= entropy_flag
;
787 header
.fec_flag
= true;
788 header
.is_in_fec_group
= IN_FEC_GROUP
;
789 header
.fec_group
= min_protected_packet
;
790 QuicFecData fec_data
;
791 fec_data
.fec_group
= header
.fec_group
;
793 // Since all data packets in this test have the same payload, the
794 // redundancy is either equal to that payload or the xor of that payload
795 // with itself, depending on the number of packets.
796 if (((number
- min_protected_packet
) % 2) == 0) {
797 for (size_t i
= GetStartOfFecProtectedData(
798 header
.public_header
.connection_id_length
,
799 header
.public_header
.version_flag
,
800 header
.public_header
.sequence_number_length
);
801 i
< data_packet
->length(); ++i
) {
802 data_packet
->mutable_data()[i
] ^= data_packet
->data()[i
];
805 fec_data
.redundancy
= data_packet
->FecProtectedData();
807 scoped_ptr
<QuicPacket
> fec_packet(framer_
.BuildFecPacket(header
, fec_data
));
808 char buffer
[kMaxPacketSize
];
809 scoped_ptr
<QuicEncryptedPacket
> encrypted(framer_
.EncryptPayload(
810 ENCRYPTION_NONE
, number
, *fec_packet
, buffer
, kMaxPacketSize
));
812 connection_
.ProcessUdpPacket(IPEndPoint(), IPEndPoint(), *encrypted
);
813 return encrypted
->length();
816 QuicByteCount
SendStreamDataToPeer(QuicStreamId id
,
818 QuicStreamOffset offset
,
820 QuicPacketSequenceNumber
* last_packet
) {
821 QuicByteCount packet_size
;
822 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
))
823 .WillOnce(DoAll(SaveArg
<3>(&packet_size
), Return(true)));
824 connection_
.SendStreamDataWithString(id
, data
, offset
, fin
, nullptr);
825 if (last_packet
!= nullptr) {
826 *last_packet
= creator_
->sequence_number();
828 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
))
833 void SendAckPacketToPeer() {
834 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
)).Times(1);
835 connection_
.SendAck();
836 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
))
840 void ProcessAckPacket(QuicPacketSequenceNumber packet_number
,
841 QuicAckFrame
* frame
) {
842 QuicPacketCreatorPeer::SetSequenceNumber(&peer_creator_
, packet_number
- 1);
843 ProcessFramePacket(QuicFrame(frame
));
846 QuicPacketEntropyHash
ProcessAckPacket(QuicAckFrame
* frame
) {
847 return ProcessFramePacket(QuicFrame(frame
));
850 QuicPacketEntropyHash
ProcessStopWaitingPacket(QuicStopWaitingFrame
* frame
) {
851 return ProcessFramePacket(QuicFrame(frame
));
854 QuicPacketEntropyHash
ProcessGoAwayPacket(QuicGoAwayFrame
* frame
) {
855 return ProcessFramePacket(QuicFrame(frame
));
858 bool IsMissing(QuicPacketSequenceNumber number
) {
859 return IsAwaitingPacket(*outgoing_ack(), number
);
862 QuicPacket
* ConstructPacket(QuicPacketHeader header
, QuicFrames frames
) {
863 QuicPacket
* packet
= BuildUnsizedDataPacket(&framer_
, header
, frames
);
864 EXPECT_NE(nullptr, packet
);
868 QuicPacket
* ConstructDataPacket(QuicPacketSequenceNumber number
,
869 QuicFecGroupNumber fec_group
,
871 QuicPacketHeader header
;
872 header
.public_header
.connection_id
= connection_id_
;
873 header
.public_header
.sequence_number_length
= sequence_number_length_
;
874 header
.public_header
.connection_id_length
= connection_id_length_
;
875 header
.entropy_flag
= entropy_flag
;
876 header
.packet_sequence_number
= number
;
877 header
.is_in_fec_group
= fec_group
== 0u ? NOT_IN_FEC_GROUP
: IN_FEC_GROUP
;
878 header
.fec_group
= fec_group
;
881 frames
.push_back(QuicFrame(&frame1_
));
882 return ConstructPacket(header
, frames
);
885 QuicPacket
* ConstructClosePacket(QuicPacketSequenceNumber number
,
886 QuicFecGroupNumber fec_group
) {
887 QuicPacketHeader header
;
888 header
.public_header
.connection_id
= connection_id_
;
889 header
.packet_sequence_number
= number
;
890 header
.is_in_fec_group
= fec_group
== 0u ? NOT_IN_FEC_GROUP
: IN_FEC_GROUP
;
891 header
.fec_group
= fec_group
;
893 QuicConnectionCloseFrame qccf
;
894 qccf
.error_code
= QUIC_PEER_GOING_AWAY
;
897 frames
.push_back(QuicFrame(&qccf
));
898 return ConstructPacket(header
, frames
);
901 QuicTime::Delta
DefaultRetransmissionTime() {
902 return QuicTime::Delta::FromMilliseconds(kDefaultRetransmissionTimeMs
);
905 QuicTime::Delta
DefaultDelayedAckTime() {
906 return QuicTime::Delta::FromMilliseconds(kMaxDelayedAckTimeMs
);
909 // Initialize a frame acknowledging all packets up to largest_observed.
910 const QuicAckFrame
InitAckFrame(QuicPacketSequenceNumber largest_observed
) {
911 QuicAckFrame
frame(MakeAckFrame(largest_observed
));
912 if (largest_observed
> 0) {
914 QuicConnectionPeer::GetSentEntropyHash(&connection_
,
920 const QuicStopWaitingFrame
InitStopWaitingFrame(
921 QuicPacketSequenceNumber least_unacked
) {
922 QuicStopWaitingFrame frame
;
923 frame
.least_unacked
= least_unacked
;
927 // Explicitly nack a packet.
928 void NackPacket(QuicPacketSequenceNumber missing
, QuicAckFrame
* frame
) {
929 frame
->missing_packets
.insert(missing
);
930 frame
->entropy_hash
^=
931 QuicConnectionPeer::PacketEntropy(&connection_
, missing
);
934 // Undo nacking a packet within the frame.
935 void AckPacket(QuicPacketSequenceNumber arrived
, QuicAckFrame
* frame
) {
936 EXPECT_THAT(frame
->missing_packets
, Contains(arrived
));
937 frame
->missing_packets
.erase(arrived
);
938 frame
->entropy_hash
^=
939 QuicConnectionPeer::PacketEntropy(&connection_
, arrived
);
942 void TriggerConnectionClose() {
943 // Send an erroneous packet to close the connection.
944 EXPECT_CALL(visitor_
,
945 OnConnectionClosed(QUIC_INVALID_PACKET_HEADER
, false));
946 // Call ProcessDataPacket rather than ProcessPacket, as we should not get a
947 // packet call to the visitor.
948 ProcessDataPacket(6000, 0, !kEntropyFlag
);
949 EXPECT_FALSE(QuicConnectionPeer::GetConnectionClosePacket(&connection_
) ==
953 void BlockOnNextWrite() {
954 writer_
->BlockOnNextWrite();
955 EXPECT_CALL(visitor_
, OnWriteBlocked()).Times(AtLeast(1));
958 void SetWritePauseTimeDelta(QuicTime::Delta delta
) {
959 writer_
->SetWritePauseTimeDelta(delta
);
962 void CongestionBlockWrites() {
963 EXPECT_CALL(*send_algorithm_
,
964 TimeUntilSend(_
, _
, _
)).WillRepeatedly(
965 testing::Return(QuicTime::Delta::FromSeconds(1)));
968 void CongestionUnblockWrites() {
969 EXPECT_CALL(*send_algorithm_
,
970 TimeUntilSend(_
, _
, _
)).WillRepeatedly(
971 testing::Return(QuicTime::Delta::Zero()));
974 QuicConnectionId connection_id_
;
976 QuicPacketCreator peer_creator_
;
977 MockEntropyCalculator entropy_calculator_
;
979 MockSendAlgorithm
* send_algorithm_
;
980 MockLossAlgorithm
* loss_algorithm_
;
982 MockRandom random_generator_
;
983 scoped_ptr
<TestConnectionHelper
> helper_
;
984 scoped_ptr
<TestPacketWriter
> writer_
;
985 NiceMock
<MockPacketWriterFactory
> factory_
;
986 TestConnection connection_
;
987 QuicPacketCreator
* creator_
;
988 QuicPacketGenerator
* generator_
;
989 QuicSentPacketManager
* manager_
;
990 StrictMock
<MockConnectionVisitor
> visitor_
;
992 QuicStreamFrame frame1_
;
993 QuicStreamFrame frame2_
;
995 QuicStopWaitingFrame stop_waiting_
;
996 QuicSequenceNumberLength sequence_number_length_
;
997 QuicConnectionIdLength connection_id_length_
;
1000 DISALLOW_COPY_AND_ASSIGN(QuicConnectionTest
);
1003 // Run all end to end tests with all supported versions.
1004 INSTANTIATE_TEST_CASE_P(SupportedVersion
,
1006 ::testing::ValuesIn(GetTestParams()));
1008 TEST_P(QuicConnectionTest
, MaxPacketSize
) {
1009 EXPECT_EQ(Perspective::IS_CLIENT
, connection_
.perspective());
1010 EXPECT_EQ(1350u, connection_
.max_packet_length());
1013 TEST_P(QuicConnectionTest
, SmallerServerMaxPacketSize
) {
1014 QuicConnectionId connection_id
= 42;
1015 TestConnection
connection(connection_id
, IPEndPoint(), helper_
.get(),
1016 factory_
, Perspective::IS_SERVER
, version());
1017 EXPECT_EQ(Perspective::IS_SERVER
, connection
.perspective());
1018 EXPECT_EQ(1000u, connection
.max_packet_length());
1021 TEST_P(QuicConnectionTest
, IncreaseServerMaxPacketSize
) {
1022 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
1024 connection_
.set_perspective(Perspective::IS_SERVER
);
1025 connection_
.set_max_packet_length(1000);
1027 QuicPacketHeader header
;
1028 header
.public_header
.connection_id
= connection_id_
;
1029 header
.public_header
.version_flag
= true;
1030 header
.packet_sequence_number
= 1;
1033 QuicPaddingFrame padding
;
1034 frames
.push_back(QuicFrame(&frame1_
));
1035 frames
.push_back(QuicFrame(&padding
));
1036 scoped_ptr
<QuicPacket
> packet(ConstructPacket(header
, frames
));
1037 char buffer
[kMaxPacketSize
];
1038 scoped_ptr
<QuicEncryptedPacket
> encrypted(framer_
.EncryptPayload(
1039 ENCRYPTION_NONE
, 12, *packet
, buffer
, kMaxPacketSize
));
1040 EXPECT_EQ(kMaxPacketSize
, encrypted
->length());
1042 framer_
.set_version(version());
1043 EXPECT_CALL(visitor_
, OnStreamFrames(_
)).Times(1);
1044 connection_
.ProcessUdpPacket(IPEndPoint(), IPEndPoint(), *encrypted
);
1046 EXPECT_EQ(kMaxPacketSize
, connection_
.max_packet_length());
1049 TEST_P(QuicConnectionTest
, PacketsInOrder
) {
1050 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
1053 EXPECT_EQ(1u, outgoing_ack()->largest_observed
);
1054 EXPECT_EQ(0u, outgoing_ack()->missing_packets
.size());
1057 EXPECT_EQ(2u, outgoing_ack()->largest_observed
);
1058 EXPECT_EQ(0u, outgoing_ack()->missing_packets
.size());
1061 EXPECT_EQ(3u, outgoing_ack()->largest_observed
);
1062 EXPECT_EQ(0u, outgoing_ack()->missing_packets
.size());
1065 TEST_P(QuicConnectionTest
, PacketsOutOfOrder
) {
1066 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
1069 EXPECT_EQ(3u, outgoing_ack()->largest_observed
);
1070 EXPECT_TRUE(IsMissing(2));
1071 EXPECT_TRUE(IsMissing(1));
1074 EXPECT_EQ(3u, outgoing_ack()->largest_observed
);
1075 EXPECT_FALSE(IsMissing(2));
1076 EXPECT_TRUE(IsMissing(1));
1079 EXPECT_EQ(3u, outgoing_ack()->largest_observed
);
1080 EXPECT_FALSE(IsMissing(2));
1081 EXPECT_FALSE(IsMissing(1));
1084 TEST_P(QuicConnectionTest
, DuplicatePacket
) {
1085 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
1088 EXPECT_EQ(3u, outgoing_ack()->largest_observed
);
1089 EXPECT_TRUE(IsMissing(2));
1090 EXPECT_TRUE(IsMissing(1));
1092 // Send packet 3 again, but do not set the expectation that
1093 // the visitor OnStreamFrames() will be called.
1094 ProcessDataPacket(3, 0, !kEntropyFlag
);
1095 EXPECT_EQ(3u, outgoing_ack()->largest_observed
);
1096 EXPECT_TRUE(IsMissing(2));
1097 EXPECT_TRUE(IsMissing(1));
1100 TEST_P(QuicConnectionTest
, PacketsOutOfOrderWithAdditionsAndLeastAwaiting
) {
1101 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
1104 EXPECT_EQ(3u, outgoing_ack()->largest_observed
);
1105 EXPECT_TRUE(IsMissing(2));
1106 EXPECT_TRUE(IsMissing(1));
1109 EXPECT_EQ(3u, outgoing_ack()->largest_observed
);
1110 EXPECT_TRUE(IsMissing(1));
1113 EXPECT_EQ(5u, outgoing_ack()->largest_observed
);
1114 EXPECT_TRUE(IsMissing(1));
1115 EXPECT_TRUE(IsMissing(4));
1117 // Pretend at this point the client has gotten acks for 2 and 3 and 1 is a
1118 // packet the peer will not retransmit. It indicates this by sending 'least
1119 // awaiting' is 4. The connection should then realize 1 will not be
1120 // retransmitted, and will remove it from the missing list.
1121 QuicAckFrame frame
= InitAckFrame(1);
1122 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(_
, _
, _
, _
));
1123 ProcessAckPacket(6, &frame
);
1125 // Force an ack to be sent.
1126 SendAckPacketToPeer();
1127 EXPECT_TRUE(IsMissing(4));
1130 TEST_P(QuicConnectionTest
, RejectPacketTooFarOut
) {
1131 EXPECT_CALL(visitor_
,
1132 OnConnectionClosed(QUIC_INVALID_PACKET_HEADER
, false));
1133 // Call ProcessDataPacket rather than ProcessPacket, as we should not get a
1134 // packet call to the visitor.
1135 ProcessDataPacket(6000, 0, !kEntropyFlag
);
1136 EXPECT_FALSE(QuicConnectionPeer::GetConnectionClosePacket(&connection_
) ==
1140 TEST_P(QuicConnectionTest
, RejectUnencryptedStreamData
) {
1141 // Process an unencrypted packet from the non-crypto stream.
1142 frame1_
.stream_id
= 3;
1143 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
1144 EXPECT_CALL(visitor_
, OnConnectionClosed(QUIC_UNENCRYPTED_STREAM_DATA
,
1146 ProcessDataPacket(1, 0, !kEntropyFlag
);
1147 EXPECT_FALSE(QuicConnectionPeer::GetConnectionClosePacket(&connection_
) ==
1149 const vector
<QuicConnectionCloseFrame
>& connection_close_frames
=
1150 writer_
->connection_close_frames();
1151 EXPECT_EQ(1u, connection_close_frames
.size());
1152 EXPECT_EQ(QUIC_UNENCRYPTED_STREAM_DATA
,
1153 connection_close_frames
[0].error_code
);
1156 TEST_P(QuicConnectionTest
, TruncatedAck
) {
1157 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
1158 QuicPacketSequenceNumber num_packets
= 256 * 2 + 1;
1159 for (QuicPacketSequenceNumber i
= 0; i
< num_packets
; ++i
) {
1160 SendStreamDataToPeer(3, "foo", i
* 3, !kFin
, nullptr);
1163 QuicAckFrame frame
= InitAckFrame(num_packets
);
1164 SequenceNumberSet lost_packets
;
1165 // Create an ack with 256 nacks, none adjacent to one another.
1166 for (QuicPacketSequenceNumber i
= 1; i
<= 256; ++i
) {
1167 NackPacket(i
* 2, &frame
);
1168 if (i
< 256) { // Last packet is nacked, but not lost.
1169 lost_packets
.insert(i
* 2);
1172 EXPECT_CALL(*loss_algorithm_
, DetectLostPackets(_
, _
, _
, _
))
1173 .WillOnce(Return(lost_packets
));
1174 EXPECT_CALL(entropy_calculator_
, EntropyHash(511))
1175 .WillOnce(Return(static_cast<QuicPacketEntropyHash
>(0)));
1176 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
1177 ProcessAckPacket(&frame
);
1179 // A truncated ack will not have the true largest observed.
1180 EXPECT_GT(num_packets
, manager_
->largest_observed());
1182 AckPacket(192, &frame
);
1184 // Removing one missing packet allows us to ack 192 and one more range, but
1185 // 192 has already been declared lost, so it doesn't register as an ack.
1186 EXPECT_CALL(*loss_algorithm_
, DetectLostPackets(_
, _
, _
, _
))
1187 .WillOnce(Return(SequenceNumberSet()));
1188 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
1189 ProcessAckPacket(&frame
);
1190 EXPECT_EQ(num_packets
, manager_
->largest_observed());
1193 TEST_P(QuicConnectionTest
, AckReceiptCausesAckSendBadEntropy
) {
1194 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
1197 // Delay sending, then queue up an ack.
1198 EXPECT_CALL(*send_algorithm_
,
1199 TimeUntilSend(_
, _
, _
)).WillOnce(
1200 testing::Return(QuicTime::Delta::FromMicroseconds(1)));
1201 QuicConnectionPeer::SendAck(&connection_
);
1203 // Process an ack with a least unacked of the received ack.
1204 // This causes an ack to be sent when TimeUntilSend returns 0.
1205 EXPECT_CALL(*send_algorithm_
,
1206 TimeUntilSend(_
, _
, _
)).WillRepeatedly(
1207 testing::Return(QuicTime::Delta::Zero()));
1208 // Skip a packet and then record an ack.
1209 QuicAckFrame frame
= InitAckFrame(0);
1210 ProcessAckPacket(3, &frame
);
1213 TEST_P(QuicConnectionTest
, OutOfOrderReceiptCausesAckSend
) {
1214 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
1217 // Should ack immediately since we have missing packets.
1218 EXPECT_EQ(1u, writer_
->packets_write_attempts());
1221 // Should ack immediately since we have missing packets.
1222 EXPECT_EQ(2u, writer_
->packets_write_attempts());
1225 // Should ack immediately, since this fills the last hole.
1226 EXPECT_EQ(3u, writer_
->packets_write_attempts());
1229 // Should not cause an ack.
1230 EXPECT_EQ(3u, writer_
->packets_write_attempts());
1233 TEST_P(QuicConnectionTest
, OutOfOrderAckReceiptCausesNoAck
) {
1234 ValueRestore
<bool> old_flag(&FLAGS_quic_dont_ack_acks
, true);
1235 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
1237 SendStreamDataToPeer(1, "foo", 0, !kFin
, nullptr);
1238 SendStreamDataToPeer(1, "bar", 3, !kFin
, nullptr);
1239 EXPECT_EQ(2u, writer_
->packets_write_attempts());
1241 QuicAckFrame ack1
= InitAckFrame(1);
1242 QuicAckFrame ack2
= InitAckFrame(2);
1243 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
1244 ProcessAckPacket(2, &ack2
);
1245 // Should ack immediately since we have missing packets.
1246 EXPECT_EQ(2u, writer_
->packets_write_attempts());
1248 ProcessAckPacket(1, &ack1
);
1249 // Should not ack an ack filling a missing packet.
1250 EXPECT_EQ(2u, writer_
->packets_write_attempts());
1253 TEST_P(QuicConnectionTest
, OutOfOrderAckReceiptCausesOneAck
) {
1254 ValueRestore
<bool> old_flag(&FLAGS_quic_dont_ack_acks
, false);
1255 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
1257 SendStreamDataToPeer(1, "foo", 0, !kFin
, nullptr);
1258 SendStreamDataToPeer(1, "bar", 3, !kFin
, nullptr);
1259 EXPECT_EQ(2u, writer_
->packets_write_attempts());
1261 QuicAckFrame ack1
= InitAckFrame(1);
1262 QuicAckFrame ack2
= InitAckFrame(2);
1263 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
1264 ProcessAckPacket(2, &ack2
);
1265 // Should ack immediately since we have missing packets.
1266 EXPECT_EQ(3u, writer_
->packets_write_attempts());
1268 ProcessAckPacket(1, &ack1
);
1269 // Should not ack an ack filling a missing packet.
1270 EXPECT_EQ(3u, writer_
->packets_write_attempts());
1273 TEST_P(QuicConnectionTest
, AckReceiptCausesAckSend
) {
1274 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
1276 QuicPacketSequenceNumber original
;
1277 QuicByteCount packet_size
;
1278 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
))
1279 .WillOnce(DoAll(SaveArg
<2>(&original
), SaveArg
<3>(&packet_size
),
1281 connection_
.SendStreamDataWithString(3, "foo", 0, !kFin
, nullptr);
1282 QuicAckFrame frame
= InitAckFrame(original
);
1283 NackPacket(original
, &frame
);
1284 // First nack triggers early retransmit.
1285 SequenceNumberSet lost_packets
;
1286 lost_packets
.insert(1);
1287 EXPECT_CALL(*loss_algorithm_
, DetectLostPackets(_
, _
, _
, _
))
1288 .WillOnce(Return(lost_packets
));
1289 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
1290 QuicPacketSequenceNumber retransmission
;
1291 EXPECT_CALL(*send_algorithm_
,
1292 OnPacketSent(_
, _
, _
, packet_size
- kQuicVersionSize
, _
))
1293 .WillOnce(DoAll(SaveArg
<2>(&retransmission
), Return(true)));
1295 ProcessAckPacket(&frame
);
1297 QuicAckFrame frame2
= InitAckFrame(retransmission
);
1298 NackPacket(original
, &frame2
);
1299 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
1300 EXPECT_CALL(*loss_algorithm_
, DetectLostPackets(_
, _
, _
, _
))
1301 .WillOnce(Return(SequenceNumberSet()));
1302 ProcessAckPacket(&frame2
);
1304 // Now if the peer sends an ack which still reports the retransmitted packet
1305 // as missing, that will bundle an ack with data after two acks in a row
1306 // indicate the high water mark needs to be raised.
1307 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
,
1308 HAS_RETRANSMITTABLE_DATA
));
1309 connection_
.SendStreamDataWithString(3, "foo", 3, !kFin
, nullptr);
1311 EXPECT_EQ(1u, writer_
->frame_count());
1312 EXPECT_EQ(1u, writer_
->stream_frames().size());
1314 // No more packet loss for the rest of the test.
1315 EXPECT_CALL(*loss_algorithm_
, DetectLostPackets(_
, _
, _
, _
))
1316 .WillRepeatedly(Return(SequenceNumberSet()));
1317 ProcessAckPacket(&frame2
);
1318 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
,
1319 HAS_RETRANSMITTABLE_DATA
));
1320 connection_
.SendStreamDataWithString(3, "foo", 3, !kFin
, nullptr);
1322 EXPECT_EQ(3u, writer_
->frame_count());
1323 EXPECT_EQ(1u, writer_
->stream_frames().size());
1324 EXPECT_FALSE(writer_
->ack_frames().empty());
1326 // But an ack with no missing packets will not send an ack.
1327 AckPacket(original
, &frame2
);
1328 ProcessAckPacket(&frame2
);
1329 ProcessAckPacket(&frame2
);
1332 TEST_P(QuicConnectionTest
, 20AcksCausesAckSend
) {
1333 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
1335 SendStreamDataToPeer(1, "foo", 0, !kFin
, nullptr);
1337 QuicAlarm
* ack_alarm
= QuicConnectionPeer::GetAckAlarm(&connection_
);
1338 // But an ack with no missing packets will not send an ack.
1339 QuicAckFrame frame
= InitAckFrame(1);
1340 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
1341 EXPECT_CALL(*loss_algorithm_
, DetectLostPackets(_
, _
, _
, _
))
1342 .WillRepeatedly(Return(SequenceNumberSet()));
1343 for (int i
= 0; i
< 20; ++i
) {
1344 EXPECT_FALSE(ack_alarm
->IsSet());
1345 ProcessAckPacket(&frame
);
1347 EXPECT_TRUE(ack_alarm
->IsSet());
1350 TEST_P(QuicConnectionTest
, LeastUnackedLower
) {
1351 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
1353 SendStreamDataToPeer(1, "foo", 0, !kFin
, nullptr);
1354 SendStreamDataToPeer(1, "bar", 3, !kFin
, nullptr);
1355 SendStreamDataToPeer(1, "eep", 6, !kFin
, nullptr);
1357 // Start out saying the least unacked is 2.
1358 QuicPacketCreatorPeer::SetSequenceNumber(&peer_creator_
, 5);
1359 QuicStopWaitingFrame frame
= InitStopWaitingFrame(2);
1360 ProcessStopWaitingPacket(&frame
);
1362 // Change it to 1, but lower the sequence number to fake out-of-order packets.
1363 // This should be fine.
1364 QuicPacketCreatorPeer::SetSequenceNumber(&peer_creator_
, 1);
1365 // The scheduler will not process out of order acks, but all packet processing
1366 // causes the connection to try to write.
1367 EXPECT_CALL(visitor_
, OnCanWrite());
1368 QuicStopWaitingFrame frame2
= InitStopWaitingFrame(1);
1369 ProcessStopWaitingPacket(&frame2
);
1371 // Now claim it's one, but set the ordering so it was sent "after" the first
1372 // one. This should cause a connection error.
1373 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
));
1374 QuicPacketCreatorPeer::SetSequenceNumber(&peer_creator_
, 7);
1375 EXPECT_CALL(visitor_
,
1376 OnConnectionClosed(QUIC_INVALID_STOP_WAITING_DATA
, false));
1377 QuicStopWaitingFrame frame3
= InitStopWaitingFrame(1);
1378 ProcessStopWaitingPacket(&frame3
);
1381 TEST_P(QuicConnectionTest
, TooManySentPackets
) {
1382 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
1384 const int num_packets
= 5100;
1385 for (int i
= 0; i
< num_packets
; ++i
) {
1386 SendStreamDataToPeer(1, "foo", 3 * i
, !kFin
, nullptr);
1389 // Ack packet 1, which leaves more than the limit outstanding.
1390 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
1391 EXPECT_CALL(visitor_
, OnConnectionClosed(
1392 QUIC_TOO_MANY_OUTSTANDING_SENT_PACKETS
, false));
1393 // We're receive buffer limited, so the connection won't try to write more.
1394 EXPECT_CALL(visitor_
, OnCanWrite()).Times(0);
1396 // Nack every packet except the last one, leaving a huge gap.
1397 QuicAckFrame frame1
= InitAckFrame(num_packets
);
1398 for (QuicPacketSequenceNumber i
= 1; i
< num_packets
; ++i
) {
1399 NackPacket(i
, &frame1
);
1401 ProcessAckPacket(&frame1
);
1404 // Flaky time out on Windows 7. http://crbug.com/501812
1405 #if !defined(OS_WIN)
1406 TEST_P(QuicConnectionTest
, TooManyReceivedPackets
) {
1407 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
1408 EXPECT_CALL(visitor_
, OnConnectionClosed(
1409 QUIC_TOO_MANY_OUTSTANDING_RECEIVED_PACKETS
, false));
1411 // Miss every other packet for 5000 packets.
1412 for (QuicPacketSequenceNumber i
= 1; i
< 5000; ++i
) {
1413 ProcessPacket(i
* 2);
1414 if (!connection_
.connected()) {
1421 TEST_P(QuicConnectionTest
, LargestObservedLower
) {
1422 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
1424 SendStreamDataToPeer(1, "foo", 0, !kFin
, nullptr);
1425 SendStreamDataToPeer(1, "bar", 3, !kFin
, nullptr);
1426 SendStreamDataToPeer(1, "eep", 6, !kFin
, nullptr);
1427 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
1429 // Start out saying the largest observed is 2.
1430 QuicAckFrame frame1
= InitAckFrame(1);
1431 QuicAckFrame frame2
= InitAckFrame(2);
1432 ProcessAckPacket(&frame2
);
1434 // Now change it to 1, and it should cause a connection error.
1435 EXPECT_CALL(visitor_
, OnConnectionClosed(QUIC_INVALID_ACK_DATA
, false));
1436 EXPECT_CALL(visitor_
, OnCanWrite()).Times(0);
1437 ProcessAckPacket(&frame1
);
1440 TEST_P(QuicConnectionTest
, AckUnsentData
) {
1441 // Ack a packet which has not been sent.
1442 EXPECT_CALL(visitor_
, OnConnectionClosed(QUIC_INVALID_ACK_DATA
, false));
1443 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
1444 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
));
1445 QuicAckFrame
frame(MakeAckFrame(1));
1446 EXPECT_CALL(visitor_
, OnCanWrite()).Times(0);
1447 ProcessAckPacket(&frame
);
1450 TEST_P(QuicConnectionTest
, AckAll
) {
1451 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
1454 QuicPacketCreatorPeer::SetSequenceNumber(&peer_creator_
, 1);
1455 QuicAckFrame frame1
= InitAckFrame(0);
1456 ProcessAckPacket(&frame1
);
1459 TEST_P(QuicConnectionTest
, SendingDifferentSequenceNumberLengthsBandwidth
) {
1460 QuicPacketSequenceNumber last_packet
;
1461 SendStreamDataToPeer(1, "foo", 0, !kFin
, &last_packet
);
1462 EXPECT_EQ(1u, last_packet
);
1463 EXPECT_EQ(PACKET_1BYTE_SEQUENCE_NUMBER
,
1464 QuicPacketCreatorPeer::NextSequenceNumberLength(creator_
));
1465 EXPECT_EQ(PACKET_1BYTE_SEQUENCE_NUMBER
,
1466 writer_
->header().public_header
.sequence_number_length
);
1468 EXPECT_CALL(*send_algorithm_
, GetCongestionWindow()).WillRepeatedly(
1469 Return(kMaxPacketSize
* 256));
1471 SendStreamDataToPeer(1, "bar", 3, !kFin
, &last_packet
);
1472 EXPECT_EQ(2u, last_packet
);
1473 EXPECT_EQ(PACKET_2BYTE_SEQUENCE_NUMBER
,
1474 QuicPacketCreatorPeer::NextSequenceNumberLength(creator_
));
1475 // The 1 packet lag is due to the sequence number length being recalculated in
1476 // QuicConnection after a packet is sent.
1477 EXPECT_EQ(PACKET_1BYTE_SEQUENCE_NUMBER
,
1478 writer_
->header().public_header
.sequence_number_length
);
1480 EXPECT_CALL(*send_algorithm_
, GetCongestionWindow()).WillRepeatedly(
1481 Return(kMaxPacketSize
* 256 * 256));
1483 SendStreamDataToPeer(1, "foo", 6, !kFin
, &last_packet
);
1484 EXPECT_EQ(3u, last_packet
);
1485 EXPECT_EQ(PACKET_4BYTE_SEQUENCE_NUMBER
,
1486 QuicPacketCreatorPeer::NextSequenceNumberLength(creator_
));
1487 EXPECT_EQ(PACKET_2BYTE_SEQUENCE_NUMBER
,
1488 writer_
->header().public_header
.sequence_number_length
);
1490 EXPECT_CALL(*send_algorithm_
, GetCongestionWindow()).WillRepeatedly(
1491 Return(kMaxPacketSize
* 256 * 256 * 256));
1493 SendStreamDataToPeer(1, "bar", 9, !kFin
, &last_packet
);
1494 EXPECT_EQ(4u, last_packet
);
1495 EXPECT_EQ(PACKET_4BYTE_SEQUENCE_NUMBER
,
1496 QuicPacketCreatorPeer::NextSequenceNumberLength(creator_
));
1497 EXPECT_EQ(PACKET_4BYTE_SEQUENCE_NUMBER
,
1498 writer_
->header().public_header
.sequence_number_length
);
1500 EXPECT_CALL(*send_algorithm_
, GetCongestionWindow()).WillRepeatedly(
1501 Return(kMaxPacketSize
* 256 * 256 * 256 * 256));
1503 SendStreamDataToPeer(1, "foo", 12, !kFin
, &last_packet
);
1504 EXPECT_EQ(5u, last_packet
);
1505 EXPECT_EQ(PACKET_6BYTE_SEQUENCE_NUMBER
,
1506 QuicPacketCreatorPeer::NextSequenceNumberLength(creator_
));
1507 EXPECT_EQ(PACKET_4BYTE_SEQUENCE_NUMBER
,
1508 writer_
->header().public_header
.sequence_number_length
);
1511 // TODO(ianswett): Re-enable this test by finding a good way to test different
1512 // sequence number lengths without sending packets with giant gaps.
1513 TEST_P(QuicConnectionTest
,
1514 DISABLED_SendingDifferentSequenceNumberLengthsUnackedDelta
) {
1515 QuicPacketSequenceNumber last_packet
;
1516 SendStreamDataToPeer(1, "foo", 0, !kFin
, &last_packet
);
1517 EXPECT_EQ(1u, last_packet
);
1518 EXPECT_EQ(PACKET_1BYTE_SEQUENCE_NUMBER
,
1519 QuicPacketCreatorPeer::NextSequenceNumberLength(creator_
));
1520 EXPECT_EQ(PACKET_1BYTE_SEQUENCE_NUMBER
,
1521 writer_
->header().public_header
.sequence_number_length
);
1523 QuicPacketCreatorPeer::SetSequenceNumber(&peer_creator_
, 100);
1525 SendStreamDataToPeer(1, "bar", 3, !kFin
, &last_packet
);
1526 EXPECT_EQ(PACKET_2BYTE_SEQUENCE_NUMBER
,
1527 QuicPacketCreatorPeer::NextSequenceNumberLength(creator_
));
1528 EXPECT_EQ(PACKET_1BYTE_SEQUENCE_NUMBER
,
1529 writer_
->header().public_header
.sequence_number_length
);
1531 QuicPacketCreatorPeer::SetSequenceNumber(&peer_creator_
, 100 * 256);
1533 SendStreamDataToPeer(1, "foo", 6, !kFin
, &last_packet
);
1534 EXPECT_EQ(PACKET_4BYTE_SEQUENCE_NUMBER
,
1535 QuicPacketCreatorPeer::NextSequenceNumberLength(creator_
));
1536 EXPECT_EQ(PACKET_2BYTE_SEQUENCE_NUMBER
,
1537 writer_
->header().public_header
.sequence_number_length
);
1539 QuicPacketCreatorPeer::SetSequenceNumber(&peer_creator_
, 100 * 256 * 256);
1541 SendStreamDataToPeer(1, "bar", 9, !kFin
, &last_packet
);
1542 EXPECT_EQ(PACKET_4BYTE_SEQUENCE_NUMBER
,
1543 QuicPacketCreatorPeer::NextSequenceNumberLength(creator_
));
1544 EXPECT_EQ(PACKET_4BYTE_SEQUENCE_NUMBER
,
1545 writer_
->header().public_header
.sequence_number_length
);
1547 QuicPacketCreatorPeer::SetSequenceNumber(&peer_creator_
,
1548 100 * 256 * 256 * 256);
1550 SendStreamDataToPeer(1, "foo", 12, !kFin
, &last_packet
);
1551 EXPECT_EQ(PACKET_6BYTE_SEQUENCE_NUMBER
,
1552 QuicPacketCreatorPeer::NextSequenceNumberLength(creator_
));
1553 EXPECT_EQ(PACKET_4BYTE_SEQUENCE_NUMBER
,
1554 writer_
->header().public_header
.sequence_number_length
);
1557 TEST_P(QuicConnectionTest
, BasicSending
) {
1558 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
1559 QuicPacketSequenceNumber last_packet
;
1560 SendStreamDataToPeer(1, "foo", 0, !kFin
, &last_packet
); // Packet 1
1561 EXPECT_EQ(1u, last_packet
);
1562 SendAckPacketToPeer(); // Packet 2
1564 EXPECT_EQ(1u, least_unacked());
1566 SendAckPacketToPeer(); // Packet 3
1567 EXPECT_EQ(1u, least_unacked());
1569 SendStreamDataToPeer(1, "bar", 3, !kFin
, &last_packet
); // Packet 4
1570 EXPECT_EQ(4u, last_packet
);
1571 SendAckPacketToPeer(); // Packet 5
1572 EXPECT_EQ(1u, least_unacked());
1574 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
1576 // Peer acks up to packet 3.
1577 QuicAckFrame frame
= InitAckFrame(3);
1578 ProcessAckPacket(&frame
);
1579 SendAckPacketToPeer(); // Packet 6
1581 // As soon as we've acked one, we skip ack packets 2 and 3 and note lack of
1583 EXPECT_EQ(4u, least_unacked());
1585 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
1587 // Peer acks up to packet 4, the last packet.
1588 QuicAckFrame frame2
= InitAckFrame(6);
1589 ProcessAckPacket(&frame2
); // Acks don't instigate acks.
1591 // Verify that we did not send an ack.
1592 EXPECT_EQ(6u, writer_
->header().packet_sequence_number
);
1594 // So the last ack has not changed.
1595 EXPECT_EQ(4u, least_unacked());
1597 // If we force an ack, we shouldn't change our retransmit state.
1598 SendAckPacketToPeer(); // Packet 7
1599 EXPECT_EQ(7u, least_unacked());
1601 // But if we send more data it should.
1602 SendStreamDataToPeer(1, "eep", 6, !kFin
, &last_packet
); // Packet 8
1603 EXPECT_EQ(8u, last_packet
);
1604 SendAckPacketToPeer(); // Packet 9
1605 EXPECT_EQ(7u, least_unacked());
1608 // QuicConnection should record the the packet sent-time prior to sending the
1610 TEST_P(QuicConnectionTest
, RecordSentTimeBeforePacketSent
) {
1611 // We're using a MockClock for the tests, so we have complete control over the
1613 // Our recorded timestamp for the last packet sent time will be passed in to
1614 // the send_algorithm. Make sure that it is set to the correct value.
1615 QuicTime actual_recorded_send_time
= QuicTime::Zero();
1616 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
))
1617 .WillOnce(DoAll(SaveArg
<0>(&actual_recorded_send_time
), Return(true)));
1619 // First send without any pause and check the result.
1620 QuicTime expected_recorded_send_time
= clock_
.Now();
1621 connection_
.SendStreamDataWithString(1, "foo", 0, !kFin
, nullptr);
1622 EXPECT_EQ(expected_recorded_send_time
, actual_recorded_send_time
)
1623 << "Expected time = " << expected_recorded_send_time
.ToDebuggingValue()
1624 << ". Actual time = " << actual_recorded_send_time
.ToDebuggingValue();
1626 // Now pause during the write, and check the results.
1627 actual_recorded_send_time
= QuicTime::Zero();
1628 const QuicTime::Delta write_pause_time_delta
=
1629 QuicTime::Delta::FromMilliseconds(5000);
1630 SetWritePauseTimeDelta(write_pause_time_delta
);
1631 expected_recorded_send_time
= clock_
.Now();
1633 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
))
1634 .WillOnce(DoAll(SaveArg
<0>(&actual_recorded_send_time
), Return(true)));
1635 connection_
.SendStreamDataWithString(2, "baz", 0, !kFin
, nullptr);
1636 EXPECT_EQ(expected_recorded_send_time
, actual_recorded_send_time
)
1637 << "Expected time = " << expected_recorded_send_time
.ToDebuggingValue()
1638 << ". Actual time = " << actual_recorded_send_time
.ToDebuggingValue();
1641 TEST_P(QuicConnectionTest
, FECSending
) {
1642 // All packets carry version info till version is negotiated.
1643 size_t payload_length
;
1644 // GetPacketLengthForOneStream() assumes a stream offset of 0 in determining
1645 // packet length. The size of the offset field in a stream frame is 0 for
1646 // offset 0, and 2 for non-zero offsets up through 64K. Increase
1647 // max_packet_length by 2 so that subsequent packets containing subsequent
1648 // stream frames with non-zero offets will fit within the packet length.
1649 size_t length
= 2 + GetPacketLengthForOneStream(
1650 connection_
.version(), kIncludeVersion
,
1651 PACKET_8BYTE_CONNECTION_ID
, PACKET_1BYTE_SEQUENCE_NUMBER
,
1652 IN_FEC_GROUP
, &payload_length
);
1653 connection_
.set_max_packet_length(length
);
1655 if (generator_
->fec_send_policy() == FEC_ALARM_TRIGGER
) {
1656 // Send 4 protected data packets. FEC packet is not sent.
1657 EXPECT_CALL(*send_algorithm_
,
1658 OnPacketSent(_
, _
, _
, _
, HAS_RETRANSMITTABLE_DATA
)).Times(4);
1660 // Send 4 protected data packets, which should also trigger 1 FEC packet.
1661 EXPECT_CALL(*send_algorithm_
,
1662 OnPacketSent(_
, _
, _
, _
, HAS_RETRANSMITTABLE_DATA
)).Times(5);
1664 // The first stream frame will have 2 fewer overhead bytes than the other 3.
1665 const string
payload(payload_length
* 4 + 2, 'a');
1666 connection_
.SendStreamDataWithStringWithFec(1, payload
, 0, !kFin
, nullptr);
1667 // Expect the FEC group to be closed after SendStreamDataWithString.
1668 EXPECT_FALSE(creator_
->IsFecGroupOpen());
1669 EXPECT_FALSE(creator_
->IsFecProtected());
1672 TEST_P(QuicConnectionTest
, FECQueueing
) {
1673 // All packets carry version info till version is negotiated.
1674 size_t payload_length
;
1675 size_t length
= GetPacketLengthForOneStream(
1676 connection_
.version(), kIncludeVersion
,
1677 PACKET_8BYTE_CONNECTION_ID
, PACKET_1BYTE_SEQUENCE_NUMBER
,
1678 IN_FEC_GROUP
, &payload_length
);
1679 connection_
.set_max_packet_length(length
);
1680 EXPECT_TRUE(creator_
->IsFecEnabled());
1682 EXPECT_EQ(0u, connection_
.NumQueuedPackets());
1684 const string
payload(payload_length
, 'a');
1685 connection_
.SendStreamDataWithStringWithFec(1, payload
, 0, !kFin
, nullptr);
1686 EXPECT_FALSE(creator_
->IsFecGroupOpen());
1687 EXPECT_FALSE(creator_
->IsFecProtected());
1688 if (generator_
->fec_send_policy() == FEC_ALARM_TRIGGER
) {
1689 // Expect the first data packet to be queued and not the FEC packet.
1690 EXPECT_EQ(1u, connection_
.NumQueuedPackets());
1692 // Expect the first data packet and the fec packet to be queued.
1693 EXPECT_EQ(2u, connection_
.NumQueuedPackets());
1697 TEST_P(QuicConnectionTest
, FECAlarmStoppedWhenFECPacketSent
) {
1698 EXPECT_TRUE(creator_
->IsFecEnabled());
1699 EXPECT_EQ(0u, QuicSentPacketManagerPeer::GetBytesInFlight(manager_
));
1700 EXPECT_FALSE(connection_
.GetFecAlarm()->IsSet());
1702 creator_
->set_max_packets_per_fec_group(2);
1704 // 1 Data packet. FEC alarm should be set.
1705 EXPECT_CALL(*send_algorithm_
,
1706 OnPacketSent(_
, _
, 1u, _
, HAS_RETRANSMITTABLE_DATA
)).Times(1);
1707 connection_
.SendStreamDataWithStringWithFec(3, "foo", 0, true, nullptr);
1708 EXPECT_TRUE(connection_
.GetFecAlarm()->IsSet());
1710 if (generator_
->fec_send_policy() == FEC_ALARM_TRIGGER
) {
1711 // If FEC send policy is FEC_ALARM_TRIGGER, FEC packet is not sent.
1712 // FEC alarm should not be set.
1713 EXPECT_CALL(*send_algorithm_
,
1714 OnPacketSent(_
, _
, _
, _
, HAS_RETRANSMITTABLE_DATA
)).Times(1);
1716 // Second data packet triggers FEC packet out. FEC alarm should not be set.
1717 EXPECT_CALL(*send_algorithm_
,
1718 OnPacketSent(_
, _
, _
, _
, HAS_RETRANSMITTABLE_DATA
)).Times(2);
1720 connection_
.SendStreamDataWithStringWithFec(5, "foo", 0, true, nullptr);
1721 if (generator_
->fec_send_policy() == FEC_ANY_TRIGGER
) {
1722 EXPECT_TRUE(writer_
->header().fec_flag
);
1724 EXPECT_FALSE(creator_
->IsFecGroupOpen());
1725 EXPECT_FALSE(connection_
.GetFecAlarm()->IsSet());
1728 TEST_P(QuicConnectionTest
, FECAlarmStoppedOnConnectionClose
) {
1729 EXPECT_TRUE(creator_
->IsFecEnabled());
1730 EXPECT_FALSE(connection_
.GetFecAlarm()->IsSet());
1731 creator_
->set_max_packets_per_fec_group(100);
1733 // 1 Data packet. FEC alarm should be set.
1734 EXPECT_CALL(*send_algorithm_
,
1735 OnPacketSent(_
, _
, 1u, _
, HAS_RETRANSMITTABLE_DATA
)).Times(1);
1736 connection_
.SendStreamDataWithStringWithFec(3, "foo", 0, kFin
, nullptr);
1737 EXPECT_TRUE(connection_
.GetFecAlarm()->IsSet());
1739 EXPECT_CALL(visitor_
, OnConnectionClosed(QUIC_NO_ERROR
, false));
1740 // Closing connection should stop the FEC alarm.
1741 connection_
.CloseConnection(QUIC_NO_ERROR
, /*from_peer=*/false);
1742 EXPECT_FALSE(connection_
.GetFecAlarm()->IsSet());
1745 TEST_P(QuicConnectionTest
, RemoveFECFromInflightOnRetransmissionTimeout
) {
1746 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
1747 EXPECT_TRUE(creator_
->IsFecEnabled());
1748 EXPECT_EQ(0u, QuicSentPacketManagerPeer::GetBytesInFlight(manager_
));
1749 EXPECT_FALSE(connection_
.GetFecAlarm()->IsSet());
1751 // 1 Data packet. FEC alarm should be set.
1752 EXPECT_CALL(*send_algorithm_
,
1753 OnPacketSent(_
, _
, 1u, _
, HAS_RETRANSMITTABLE_DATA
)).Times(1);
1754 connection_
.SendStreamDataWithStringWithFec(3, "foo", 0, !kFin
, nullptr);
1755 EXPECT_TRUE(connection_
.GetFecAlarm()->IsSet());
1756 size_t protected_packet
=
1757 QuicSentPacketManagerPeer::GetBytesInFlight(manager_
);
1759 // Force FEC timeout to send FEC packet out.
1760 EXPECT_CALL(*send_algorithm_
,
1761 OnPacketSent(_
, _
, 2u, _
, HAS_RETRANSMITTABLE_DATA
)).Times(1);
1762 connection_
.GetFecAlarm()->Fire();
1763 EXPECT_TRUE(writer_
->header().fec_flag
);
1765 size_t fec_packet
= protected_packet
;
1766 EXPECT_EQ(protected_packet
+ fec_packet
,
1767 QuicSentPacketManagerPeer::GetBytesInFlight(manager_
));
1768 clock_
.AdvanceTime(DefaultRetransmissionTime());
1770 // On RTO, both data and FEC packets are removed from inflight, only the data
1771 // packet is retransmitted, and this retransmission (but not FEC) gets added
1772 // back into the inflight.
1773 EXPECT_CALL(*send_algorithm_
, OnRetransmissionTimeout(true));
1774 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
)).Times(1);
1775 connection_
.GetRetransmissionAlarm()->Fire();
1777 // The retransmission of packet 1 will be 3 bytes smaller than packet 1, since
1778 // the first transmission will have 1 byte for FEC group number and 2 bytes of
1779 // stream frame size, which are absent in the retransmission.
1780 size_t retransmitted_packet
= protected_packet
- 3;
1781 EXPECT_EQ(protected_packet
+ retransmitted_packet
,
1782 QuicSentPacketManagerPeer::GetBytesInFlight(manager_
));
1783 EXPECT_FALSE(connection_
.GetFecAlarm()->IsSet());
1785 // Receive ack for the retransmission. No data should be outstanding.
1786 QuicAckFrame ack
= InitAckFrame(3);
1787 NackPacket(1, &ack
);
1788 NackPacket(2, &ack
);
1789 SequenceNumberSet lost_packets
;
1790 lost_packets
.insert(1);
1791 EXPECT_CALL(*loss_algorithm_
, DetectLostPackets(_
, _
, _
, _
))
1792 .WillOnce(Return(lost_packets
));
1793 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
1794 ProcessAckPacket(&ack
);
1796 // Ensure the alarm is not set since all packets have been acked or abandoned.
1797 EXPECT_FALSE(connection_
.GetRetransmissionAlarm()->IsSet());
1798 EXPECT_EQ(0u, QuicSentPacketManagerPeer::GetBytesInFlight(manager_
));
1801 TEST_P(QuicConnectionTest
, RemoveFECFromInflightOnLossRetransmission
) {
1802 EXPECT_TRUE(creator_
->IsFecEnabled());
1803 EXPECT_FALSE(connection_
.GetFecAlarm()->IsSet());
1805 // 1 FEC-protected data packet. FEC alarm should be set.
1806 EXPECT_CALL(*send_algorithm_
,
1807 OnPacketSent(_
, _
, _
, _
, HAS_RETRANSMITTABLE_DATA
)).Times(1);
1808 connection_
.SendStreamDataWithStringWithFec(3, "foo", 0, kFin
, nullptr);
1809 EXPECT_TRUE(connection_
.GetFecAlarm()->IsSet());
1810 size_t protected_packet
=
1811 QuicSentPacketManagerPeer::GetBytesInFlight(manager_
);
1813 // Force FEC timeout to send FEC packet out.
1814 EXPECT_CALL(*send_algorithm_
,
1815 OnPacketSent(_
, _
, _
, _
, HAS_RETRANSMITTABLE_DATA
)).Times(1);
1816 connection_
.GetFecAlarm()->Fire();
1817 EXPECT_TRUE(writer_
->header().fec_flag
);
1818 size_t fec_packet
= protected_packet
;
1819 EXPECT_EQ(protected_packet
+ fec_packet
,
1820 QuicSentPacketManagerPeer::GetBytesInFlight(manager_
));
1822 // Send more data to trigger NACKs. Note that all data starts at stream offset
1823 // 0 to ensure the same packet size, for ease of testing.
1824 EXPECT_CALL(*send_algorithm_
,
1825 OnPacketSent(_
, _
, _
, _
, HAS_RETRANSMITTABLE_DATA
)).Times(4);
1826 connection_
.SendStreamDataWithString(5, "foo", 0, kFin
, nullptr);
1827 connection_
.SendStreamDataWithString(7, "foo", 0, kFin
, nullptr);
1828 connection_
.SendStreamDataWithString(9, "foo", 0, kFin
, nullptr);
1829 connection_
.SendStreamDataWithString(11, "foo", 0, kFin
, nullptr);
1831 // An unprotected packet will be 3 bytes smaller than an FEC-protected packet,
1832 // since the protected packet will have 1 byte for FEC group number and
1833 // 2 bytes of stream frame size, which are absent in the unprotected packet.
1834 size_t unprotected_packet
= protected_packet
- 3;
1835 EXPECT_EQ(protected_packet
+ fec_packet
+ 4 * unprotected_packet
,
1836 QuicSentPacketManagerPeer::GetBytesInFlight(manager_
));
1837 EXPECT_FALSE(connection_
.GetFecAlarm()->IsSet());
1839 // Ack data packets, and NACK FEC packet and one data packet. Triggers
1840 // NACK-based loss detection of both packets, but only data packet is
1841 // retransmitted and considered oustanding.
1842 QuicAckFrame ack
= InitAckFrame(6);
1843 NackPacket(2, &ack
);
1844 NackPacket(3, &ack
);
1845 SequenceNumberSet lost_packets
;
1846 lost_packets
.insert(2);
1847 lost_packets
.insert(3);
1848 EXPECT_CALL(*loss_algorithm_
, DetectLostPackets(_
, _
, _
, _
))
1849 .WillOnce(Return(lost_packets
));
1850 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
1851 EXPECT_CALL(*send_algorithm_
,
1852 OnPacketSent(_
, _
, _
, _
, HAS_RETRANSMITTABLE_DATA
)).Times(1);
1853 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
1854 ProcessAckPacket(&ack
);
1855 // On receiving this ack from the server, the client will no longer send
1856 // version number in subsequent packets, including in this retransmission.
1857 size_t unprotected_packet_no_version
= unprotected_packet
- 4;
1858 EXPECT_EQ(unprotected_packet_no_version
,
1859 QuicSentPacketManagerPeer::GetBytesInFlight(manager_
));
1861 // Receive ack for the retransmission. No data should be outstanding.
1862 QuicAckFrame ack2
= InitAckFrame(7);
1863 NackPacket(2, &ack2
);
1864 NackPacket(3, &ack2
);
1865 SequenceNumberSet lost_packets2
;
1866 EXPECT_CALL(*loss_algorithm_
, DetectLostPackets(_
, _
, _
, _
))
1867 .WillOnce(Return(lost_packets2
));
1868 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
1869 ProcessAckPacket(&ack2
);
1870 EXPECT_EQ(0u, QuicSentPacketManagerPeer::GetBytesInFlight(manager_
));
1873 TEST_P(QuicConnectionTest
, FECRemainsInflightOnTLPOfEarlierData
) {
1874 // This test checks if TLP is sent correctly when a data and an FEC packet
1875 // are outstanding. TLP should be sent for the data packet when the
1876 // retransmission alarm fires.
1877 // Turn on TLP for this test.
1878 QuicSentPacketManagerPeer::SetMaxTailLossProbes(manager_
, 1);
1879 EXPECT_TRUE(creator_
->IsFecEnabled());
1880 EXPECT_EQ(0u, QuicSentPacketManagerPeer::GetBytesInFlight(manager_
));
1881 EXPECT_FALSE(connection_
.GetFecAlarm()->IsSet());
1883 // 1 Data packet. FEC alarm should be set.
1884 EXPECT_CALL(*send_algorithm_
,
1885 OnPacketSent(_
, _
, 1u, _
, HAS_RETRANSMITTABLE_DATA
)).Times(1);
1886 connection_
.SendStreamDataWithStringWithFec(3, "foo", 0, kFin
, nullptr);
1887 EXPECT_TRUE(connection_
.GetFecAlarm()->IsSet());
1888 size_t protected_packet
=
1889 QuicSentPacketManagerPeer::GetBytesInFlight(manager_
);
1890 EXPECT_LT(0u, protected_packet
);
1892 // Force FEC timeout to send FEC packet out.
1893 EXPECT_CALL(*send_algorithm_
,
1894 OnPacketSent(_
, _
, 2u, _
, HAS_RETRANSMITTABLE_DATA
)).Times(1);
1895 connection_
.GetFecAlarm()->Fire();
1896 EXPECT_TRUE(writer_
->header().fec_flag
);
1897 size_t fec_packet
= protected_packet
;
1898 EXPECT_EQ(protected_packet
+ fec_packet
,
1899 QuicSentPacketManagerPeer::GetBytesInFlight(manager_
));
1901 // TLP alarm should be set.
1902 QuicTime retransmission_time
=
1903 connection_
.GetRetransmissionAlarm()->deadline();
1904 EXPECT_NE(QuicTime::Zero(), retransmission_time
);
1905 // Simulate the retransmission alarm firing and sending a TLP, so send
1906 // algorithm's OnRetransmissionTimeout is not called.
1907 clock_
.AdvanceTime(retransmission_time
.Subtract(clock_
.Now()));
1908 EXPECT_CALL(*send_algorithm_
,
1909 OnPacketSent(_
, _
, 3u, _
, HAS_RETRANSMITTABLE_DATA
)).Times(1);
1910 connection_
.GetRetransmissionAlarm()->Fire();
1911 // The TLP retransmission of packet 1 will be 3 bytes smaller than packet 1,
1912 // since packet 1 will have 1 byte for FEC group number and 2 bytes of stream
1913 // frame size, which are absent in the the TLP retransmission.
1914 size_t tlp_packet
= protected_packet
- 3;
1915 EXPECT_EQ(protected_packet
+ fec_packet
+ tlp_packet
,
1916 QuicSentPacketManagerPeer::GetBytesInFlight(manager_
));
1919 TEST_P(QuicConnectionTest
, FECRemainsInflightOnTLPOfLaterData
) {
1920 // Tests if TLP is sent correctly when data packet 1 and an FEC packet are
1921 // sent followed by data packet 2, and data packet 1 is acked. TLP should be
1922 // sent for data packet 2 when the retransmission alarm fires. Turn on TLP for
1924 QuicSentPacketManagerPeer::SetMaxTailLossProbes(manager_
, 1);
1925 EXPECT_TRUE(creator_
->IsFecEnabled());
1926 EXPECT_EQ(0u, QuicSentPacketManagerPeer::GetBytesInFlight(manager_
));
1927 EXPECT_FALSE(connection_
.GetFecAlarm()->IsSet());
1929 // 1 Data packet. FEC alarm should be set.
1930 EXPECT_CALL(*send_algorithm_
,
1931 OnPacketSent(_
, _
, 1u, _
, HAS_RETRANSMITTABLE_DATA
)).Times(1);
1932 connection_
.SendStreamDataWithStringWithFec(3, "foo", 0, kFin
, nullptr);
1933 EXPECT_TRUE(connection_
.GetFecAlarm()->IsSet());
1934 size_t protected_packet
=
1935 QuicSentPacketManagerPeer::GetBytesInFlight(manager_
);
1936 EXPECT_LT(0u, protected_packet
);
1938 // Force FEC timeout to send FEC packet out.
1939 EXPECT_CALL(*send_algorithm_
,
1940 OnPacketSent(_
, _
, 2u, _
, HAS_RETRANSMITTABLE_DATA
)).Times(1);
1941 connection_
.GetFecAlarm()->Fire();
1942 EXPECT_TRUE(writer_
->header().fec_flag
);
1943 // Protected data packet and FEC packet oustanding.
1944 size_t fec_packet
= protected_packet
;
1945 EXPECT_EQ(protected_packet
+ fec_packet
,
1946 QuicSentPacketManagerPeer::GetBytesInFlight(manager_
));
1948 // Send 1 unprotected data packet. No FEC alarm should be set.
1949 EXPECT_CALL(*send_algorithm_
,
1950 OnPacketSent(_
, _
, 3u, _
, HAS_RETRANSMITTABLE_DATA
)).Times(1);
1951 connection_
.SendStreamDataWithString(5, "foo", 0, kFin
, nullptr);
1952 EXPECT_FALSE(connection_
.GetFecAlarm()->IsSet());
1953 // Protected data packet, FEC packet, and unprotected data packet oustanding.
1954 // An unprotected packet will be 3 bytes smaller than an FEC-protected packet,
1955 // since the protected packet will have 1 byte for FEC group number and
1956 // 2 bytes of stream frame size, which are absent in the unprotected packet.
1957 size_t unprotected_packet
= protected_packet
- 3;
1958 EXPECT_EQ(protected_packet
+ fec_packet
+ unprotected_packet
,
1959 QuicSentPacketManagerPeer::GetBytesInFlight(manager_
));
1961 // Receive ack for first data packet. FEC and second data packet are still
1963 QuicAckFrame ack
= InitAckFrame(1);
1964 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
1965 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
1966 ProcessAckPacket(&ack
);
1967 // FEC packet and unprotected data packet oustanding.
1968 EXPECT_EQ(fec_packet
+ unprotected_packet
,
1969 QuicSentPacketManagerPeer::GetBytesInFlight(manager_
));
1971 // TLP alarm should be set.
1972 QuicTime retransmission_time
=
1973 connection_
.GetRetransmissionAlarm()->deadline();
1974 EXPECT_NE(QuicTime::Zero(), retransmission_time
);
1975 // Simulate the retransmission alarm firing and sending a TLP, so send
1976 // algorithm's OnRetransmissionTimeout is not called.
1977 clock_
.AdvanceTime(retransmission_time
.Subtract(clock_
.Now()));
1978 EXPECT_CALL(*send_algorithm_
,
1979 OnPacketSent(_
, _
, 4u, _
, HAS_RETRANSMITTABLE_DATA
)).Times(1);
1980 connection_
.GetRetransmissionAlarm()->Fire();
1982 // Having received an ack from the server, the client will no longer send
1983 // version number in subsequent packets, including in this retransmission.
1984 size_t tlp_packet_no_version
= unprotected_packet
- 4;
1985 EXPECT_EQ(fec_packet
+ unprotected_packet
+ tlp_packet_no_version
,
1986 QuicSentPacketManagerPeer::GetBytesInFlight(manager_
));
1989 TEST_P(QuicConnectionTest
, NoTLPForFECPacket
) {
1990 // Turn on TLP for this test.
1991 QuicSentPacketManagerPeer::SetMaxTailLossProbes(manager_
, 1);
1992 EXPECT_TRUE(creator_
->IsFecEnabled());
1993 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
1995 // Send 1 FEC-protected data packet. FEC alarm should be set.
1996 EXPECT_CALL(*send_algorithm_
,
1997 OnPacketSent(_
, _
, _
, _
, HAS_RETRANSMITTABLE_DATA
)).Times(1);
1998 connection_
.SendStreamDataWithStringWithFec(3, "foo", 0, !kFin
, nullptr);
1999 EXPECT_TRUE(connection_
.GetFecAlarm()->IsSet());
2000 // Force FEC timeout to send FEC packet out.
2001 EXPECT_CALL(*send_algorithm_
,
2002 OnPacketSent(_
, _
, _
, _
, HAS_RETRANSMITTABLE_DATA
)).Times(1);
2003 connection_
.GetFecAlarm()->Fire();
2004 EXPECT_TRUE(writer_
->header().fec_flag
);
2006 // Ack data packet, but not FEC packet.
2007 QuicAckFrame ack
= InitAckFrame(1);
2008 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
2009 ProcessAckPacket(&ack
);
2011 // No TLP alarm for FEC, but retransmission alarm should be set for an RTO.
2012 EXPECT_LT(0u, QuicSentPacketManagerPeer::GetBytesInFlight(manager_
));
2013 EXPECT_TRUE(connection_
.GetRetransmissionAlarm()->IsSet());
2014 QuicTime rto_time
= connection_
.GetRetransmissionAlarm()->deadline();
2015 EXPECT_NE(QuicTime::Zero(), rto_time
);
2017 // Simulate the retransmission alarm firing. FEC packet is no longer
2019 clock_
.AdvanceTime(rto_time
.Subtract(clock_
.Now()));
2020 connection_
.GetRetransmissionAlarm()->Fire();
2022 EXPECT_FALSE(connection_
.GetRetransmissionAlarm()->IsSet());
2023 EXPECT_EQ(0u, QuicSentPacketManagerPeer::GetBytesInFlight(manager_
));
2026 TEST_P(QuicConnectionTest
, FramePacking
) {
2027 CongestionBlockWrites();
2029 // Send an ack and two stream frames in 1 packet by queueing them.
2030 connection_
.SendAck();
2031 EXPECT_CALL(visitor_
, OnCanWrite()).WillOnce(DoAll(
2032 IgnoreResult(InvokeWithoutArgs(&connection_
,
2033 &TestConnection::SendStreamData3
)),
2034 IgnoreResult(InvokeWithoutArgs(&connection_
,
2035 &TestConnection::SendStreamData5
))));
2037 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
)).Times(1);
2038 CongestionUnblockWrites();
2039 connection_
.GetSendAlarm()->Fire();
2040 EXPECT_EQ(0u, connection_
.NumQueuedPackets());
2041 EXPECT_FALSE(connection_
.HasQueuedData());
2043 // Parse the last packet and ensure it's an ack and two stream frames from
2044 // two different streams.
2045 EXPECT_EQ(4u, writer_
->frame_count());
2046 EXPECT_FALSE(writer_
->stop_waiting_frames().empty());
2047 EXPECT_FALSE(writer_
->ack_frames().empty());
2048 ASSERT_EQ(2u, writer_
->stream_frames().size());
2049 EXPECT_EQ(kClientDataStreamId1
, writer_
->stream_frames()[0].stream_id
);
2050 EXPECT_EQ(kClientDataStreamId2
, writer_
->stream_frames()[1].stream_id
);
2053 TEST_P(QuicConnectionTest
, FramePackingNonCryptoThenCrypto
) {
2054 CongestionBlockWrites();
2056 // Send an ack and two stream frames (one non-crypto, then one crypto) in 2
2057 // packets by queueing them.
2058 connection_
.SendAck();
2059 EXPECT_CALL(visitor_
, OnCanWrite()).WillOnce(DoAll(
2060 IgnoreResult(InvokeWithoutArgs(&connection_
,
2061 &TestConnection::SendStreamData3
)),
2062 IgnoreResult(InvokeWithoutArgs(&connection_
,
2063 &TestConnection::SendCryptoStreamData
))));
2065 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
)).Times(2);
2066 CongestionUnblockWrites();
2067 connection_
.GetSendAlarm()->Fire();
2068 EXPECT_EQ(0u, connection_
.NumQueuedPackets());
2069 EXPECT_FALSE(connection_
.HasQueuedData());
2071 // Parse the last packet and ensure it's the crypto stream frame.
2072 EXPECT_EQ(1u, writer_
->frame_count());
2073 ASSERT_EQ(1u, writer_
->stream_frames().size());
2074 EXPECT_EQ(kCryptoStreamId
, writer_
->stream_frames()[0].stream_id
);
2077 TEST_P(QuicConnectionTest
, FramePackingCryptoThenNonCrypto
) {
2078 CongestionBlockWrites();
2080 // Send an ack and two stream frames (one crypto, then one non-crypto) in 2
2081 // packets by queueing them.
2082 connection_
.SendAck();
2083 EXPECT_CALL(visitor_
, OnCanWrite()).WillOnce(DoAll(
2084 IgnoreResult(InvokeWithoutArgs(&connection_
,
2085 &TestConnection::SendCryptoStreamData
)),
2086 IgnoreResult(InvokeWithoutArgs(&connection_
,
2087 &TestConnection::SendStreamData3
))));
2089 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
)).Times(2);
2090 CongestionUnblockWrites();
2091 connection_
.GetSendAlarm()->Fire();
2092 EXPECT_EQ(0u, connection_
.NumQueuedPackets());
2093 EXPECT_FALSE(connection_
.HasQueuedData());
2095 // Parse the last packet and ensure it's the stream frame from stream 3.
2096 EXPECT_EQ(1u, writer_
->frame_count());
2097 ASSERT_EQ(1u, writer_
->stream_frames().size());
2098 EXPECT_EQ(kClientDataStreamId1
, writer_
->stream_frames()[0].stream_id
);
2101 TEST_P(QuicConnectionTest
, FramePackingFEC
) {
2102 EXPECT_TRUE(creator_
->IsFecEnabled());
2104 CongestionBlockWrites();
2106 // Queue an ack and two stream frames. Ack gets flushed when FEC is turned on
2107 // for sending protected data; two stream frames are packed in 1 packet.
2108 EXPECT_CALL(visitor_
, OnCanWrite()).WillOnce(DoAll(
2109 IgnoreResult(InvokeWithoutArgs(
2110 &connection_
, &TestConnection::SendStreamData3WithFec
)),
2111 IgnoreResult(InvokeWithoutArgs(
2112 &connection_
, &TestConnection::SendStreamData5WithFec
))));
2113 connection_
.SendAck();
2115 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
)).Times(2);
2116 CongestionUnblockWrites();
2117 connection_
.GetSendAlarm()->Fire();
2118 EXPECT_EQ(0u, connection_
.NumQueuedPackets());
2119 EXPECT_FALSE(connection_
.HasQueuedData());
2121 // Parse the last packet and ensure it's in an fec group.
2122 EXPECT_EQ(2u, writer_
->header().fec_group
);
2123 EXPECT_EQ(2u, writer_
->frame_count());
2125 // FEC alarm should be set.
2126 EXPECT_TRUE(connection_
.GetFecAlarm()->IsSet());
2129 TEST_P(QuicConnectionTest
, FramePackingAckResponse
) {
2130 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
2131 // Process a data packet to queue up a pending ack.
2132 EXPECT_CALL(visitor_
, OnStreamFrames(_
)).Times(1);
2133 ProcessDataPacket(1, 1, kEntropyFlag
);
2135 EXPECT_CALL(visitor_
, OnCanWrite()).WillOnce(DoAll(
2136 IgnoreResult(InvokeWithoutArgs(&connection_
,
2137 &TestConnection::SendStreamData3
)),
2138 IgnoreResult(InvokeWithoutArgs(&connection_
,
2139 &TestConnection::SendStreamData5
))));
2141 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
)).Times(1);
2143 // Process an ack to cause the visitor's OnCanWrite to be invoked.
2144 QuicAckFrame ack_one
= InitAckFrame(0);
2145 ProcessAckPacket(3, &ack_one
);
2147 EXPECT_EQ(0u, connection_
.NumQueuedPackets());
2148 EXPECT_FALSE(connection_
.HasQueuedData());
2150 // Parse the last packet and ensure it's an ack and two stream frames from
2151 // two different streams.
2152 EXPECT_EQ(4u, writer_
->frame_count());
2153 EXPECT_FALSE(writer_
->stop_waiting_frames().empty());
2154 EXPECT_FALSE(writer_
->ack_frames().empty());
2155 ASSERT_EQ(2u, writer_
->stream_frames().size());
2156 EXPECT_EQ(kClientDataStreamId1
, writer_
->stream_frames()[0].stream_id
);
2157 EXPECT_EQ(kClientDataStreamId2
, writer_
->stream_frames()[1].stream_id
);
2160 TEST_P(QuicConnectionTest
, FramePackingSendv
) {
2161 // Send data in 1 packet by writing multiple blocks in a single iovector
2163 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
));
2165 char data
[] = "ABCD";
2166 struct iovec iov
[2];
2167 iov
[0].iov_base
= data
;
2169 iov
[1].iov_base
= data
+ 2;
2171 connection_
.SendStreamData(1, QuicIOVector(iov
, 2, 4), 0, !kFin
,
2172 MAY_FEC_PROTECT
, nullptr);
2174 EXPECT_EQ(0u, connection_
.NumQueuedPackets());
2175 EXPECT_FALSE(connection_
.HasQueuedData());
2177 // Parse the last packet and ensure multiple iovector blocks have
2178 // been packed into a single stream frame from one stream.
2179 EXPECT_EQ(1u, writer_
->frame_count());
2180 EXPECT_EQ(1u, writer_
->stream_frames().size());
2181 QuicStreamFrame frame
= writer_
->stream_frames()[0];
2182 EXPECT_EQ(1u, frame
.stream_id
);
2183 EXPECT_EQ("ABCD", frame
.data
);
2186 TEST_P(QuicConnectionTest
, FramePackingSendvQueued
) {
2187 // Try to send two stream frames in 1 packet by using writev.
2188 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
));
2191 char data
[] = "ABCD";
2192 struct iovec iov
[2];
2193 iov
[0].iov_base
= data
;
2195 iov
[1].iov_base
= data
+ 2;
2197 connection_
.SendStreamData(1, QuicIOVector(iov
, 2, 4), 0, !kFin
,
2198 MAY_FEC_PROTECT
, nullptr);
2200 EXPECT_EQ(1u, connection_
.NumQueuedPackets());
2201 EXPECT_TRUE(connection_
.HasQueuedData());
2203 // Unblock the writes and actually send.
2204 writer_
->SetWritable();
2205 connection_
.OnCanWrite();
2206 EXPECT_EQ(0u, connection_
.NumQueuedPackets());
2208 // Parse the last packet and ensure it's one stream frame from one stream.
2209 EXPECT_EQ(1u, writer_
->frame_count());
2210 EXPECT_EQ(1u, writer_
->stream_frames().size());
2211 EXPECT_EQ(1u, writer_
->stream_frames()[0].stream_id
);
2214 TEST_P(QuicConnectionTest
, SendingZeroBytes
) {
2215 // Send a zero byte write with a fin using writev.
2216 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
));
2217 QuicIOVector
empty_iov(nullptr, 0, 0);
2218 connection_
.SendStreamData(1, empty_iov
, 0, kFin
, MAY_FEC_PROTECT
, nullptr);
2220 EXPECT_EQ(0u, connection_
.NumQueuedPackets());
2221 EXPECT_FALSE(connection_
.HasQueuedData());
2223 // Parse the last packet and ensure it's one stream frame from one stream.
2224 EXPECT_EQ(1u, writer_
->frame_count());
2225 EXPECT_EQ(1u, writer_
->stream_frames().size());
2226 EXPECT_EQ(1u, writer_
->stream_frames()[0].stream_id
);
2227 EXPECT_TRUE(writer_
->stream_frames()[0].fin
);
2230 TEST_P(QuicConnectionTest
, OnCanWrite
) {
2231 // Visitor's OnCanWrite will send data, but will have more pending writes.
2232 EXPECT_CALL(visitor_
, OnCanWrite()).WillOnce(DoAll(
2233 IgnoreResult(InvokeWithoutArgs(&connection_
,
2234 &TestConnection::SendStreamData3
)),
2235 IgnoreResult(InvokeWithoutArgs(&connection_
,
2236 &TestConnection::SendStreamData5
))));
2237 EXPECT_CALL(visitor_
, WillingAndAbleToWrite()).WillOnce(Return(true));
2238 EXPECT_CALL(*send_algorithm_
,
2239 TimeUntilSend(_
, _
, _
)).WillRepeatedly(
2240 testing::Return(QuicTime::Delta::Zero()));
2242 connection_
.OnCanWrite();
2244 // Parse the last packet and ensure it's the two stream frames from
2245 // two different streams.
2246 EXPECT_EQ(2u, writer_
->frame_count());
2247 EXPECT_EQ(2u, writer_
->stream_frames().size());
2248 EXPECT_EQ(kClientDataStreamId1
, writer_
->stream_frames()[0].stream_id
);
2249 EXPECT_EQ(kClientDataStreamId2
, writer_
->stream_frames()[1].stream_id
);
2252 TEST_P(QuicConnectionTest
, RetransmitOnNack
) {
2253 QuicPacketSequenceNumber last_packet
;
2254 QuicByteCount second_packet_size
;
2255 SendStreamDataToPeer(3, "foo", 0, !kFin
, &last_packet
); // Packet 1
2256 second_packet_size
=
2257 SendStreamDataToPeer(3, "foos", 3, !kFin
, &last_packet
); // Packet 2
2258 SendStreamDataToPeer(3, "fooos", 7, !kFin
, &last_packet
); // Packet 3
2260 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
2262 // Don't lose a packet on an ack, and nothing is retransmitted.
2263 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
2264 QuicAckFrame ack_one
= InitAckFrame(1);
2265 ProcessAckPacket(&ack_one
);
2267 // Lose a packet and ensure it triggers retransmission.
2268 QuicAckFrame nack_two
= InitAckFrame(3);
2269 NackPacket(2, &nack_two
);
2270 SequenceNumberSet lost_packets
;
2271 lost_packets
.insert(2);
2272 EXPECT_CALL(*loss_algorithm_
, DetectLostPackets(_
, _
, _
, _
))
2273 .WillOnce(Return(lost_packets
));
2274 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
2275 EXPECT_CALL(*send_algorithm_
,
2276 OnPacketSent(_
, _
, _
, second_packet_size
- kQuicVersionSize
, _
)).
2278 ProcessAckPacket(&nack_two
);
2281 TEST_P(QuicConnectionTest
, DoNotSendQueuedPacketForResetStream
) {
2282 // Block the connection to queue the packet.
2285 QuicStreamId stream_id
= 2;
2286 connection_
.SendStreamDataWithString(stream_id
, "foo", 0, !kFin
, nullptr);
2288 // Now that there is a queued packet, reset the stream.
2289 connection_
.SendRstStream(stream_id
, QUIC_STREAM_NO_ERROR
, 14);
2291 // Unblock the connection and verify that only the RST_STREAM is sent.
2292 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
)).Times(1);
2293 writer_
->SetWritable();
2294 connection_
.OnCanWrite();
2295 EXPECT_EQ(1u, writer_
->frame_count());
2296 EXPECT_EQ(1u, writer_
->rst_stream_frames().size());
2299 TEST_P(QuicConnectionTest
, DoNotRetransmitForResetStreamOnNack
) {
2300 QuicStreamId stream_id
= 2;
2301 QuicPacketSequenceNumber last_packet
;
2302 SendStreamDataToPeer(stream_id
, "foo", 0, !kFin
, &last_packet
);
2303 SendStreamDataToPeer(stream_id
, "foos", 3, !kFin
, &last_packet
);
2304 SendStreamDataToPeer(stream_id
, "fooos", 7, !kFin
, &last_packet
);
2306 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
)).Times(1);
2307 connection_
.SendRstStream(stream_id
, QUIC_STREAM_NO_ERROR
, 14);
2309 // Lose a packet and ensure it does not trigger retransmission.
2310 QuicAckFrame nack_two
= InitAckFrame(last_packet
);
2311 NackPacket(last_packet
- 1, &nack_two
);
2312 SequenceNumberSet lost_packets
;
2313 lost_packets
.insert(last_packet
- 1);
2314 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
2315 EXPECT_CALL(*loss_algorithm_
, DetectLostPackets(_
, _
, _
, _
))
2316 .WillOnce(Return(lost_packets
));
2317 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
2318 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
)).Times(0);
2319 ProcessAckPacket(&nack_two
);
2322 TEST_P(QuicConnectionTest
, DoNotRetransmitForResetStreamOnRTO
) {
2323 QuicStreamId stream_id
= 2;
2324 QuicPacketSequenceNumber last_packet
;
2325 SendStreamDataToPeer(stream_id
, "foo", 0, !kFin
, &last_packet
);
2327 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
)).Times(1);
2328 connection_
.SendRstStream(stream_id
, QUIC_STREAM_NO_ERROR
, 14);
2330 // Fire the RTO and verify that the RST_STREAM is resent, not stream data.
2331 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
)).Times(1);
2332 clock_
.AdvanceTime(DefaultRetransmissionTime());
2333 connection_
.GetRetransmissionAlarm()->Fire();
2334 EXPECT_EQ(1u, writer_
->frame_count());
2335 EXPECT_EQ(1u, writer_
->rst_stream_frames().size());
2336 EXPECT_EQ(stream_id
, writer_
->rst_stream_frames().front().stream_id
);
2339 TEST_P(QuicConnectionTest
, DoNotSendPendingRetransmissionForResetStream
) {
2340 QuicStreamId stream_id
= 2;
2341 QuicPacketSequenceNumber last_packet
;
2342 SendStreamDataToPeer(stream_id
, "foo", 0, !kFin
, &last_packet
);
2343 SendStreamDataToPeer(stream_id
, "foos", 3, !kFin
, &last_packet
);
2345 connection_
.SendStreamDataWithString(stream_id
, "fooos", 7, !kFin
, nullptr);
2347 // Lose a packet which will trigger a pending retransmission.
2348 QuicAckFrame ack
= InitAckFrame(last_packet
);
2349 NackPacket(last_packet
- 1, &ack
);
2350 SequenceNumberSet lost_packets
;
2351 lost_packets
.insert(last_packet
- 1);
2352 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
2353 EXPECT_CALL(*loss_algorithm_
, DetectLostPackets(_
, _
, _
, _
))
2354 .WillOnce(Return(lost_packets
));
2355 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
2356 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
)).Times(0);
2357 ProcessAckPacket(&ack
);
2359 connection_
.SendRstStream(stream_id
, QUIC_STREAM_NO_ERROR
, 14);
2361 // Unblock the connection and verify that the RST_STREAM is sent but not the
2362 // second data packet nor a retransmit.
2363 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
)).Times(1);
2364 writer_
->SetWritable();
2365 connection_
.OnCanWrite();
2366 EXPECT_EQ(1u, writer_
->frame_count());
2367 EXPECT_EQ(1u, writer_
->rst_stream_frames().size());
2368 EXPECT_EQ(stream_id
, writer_
->rst_stream_frames().front().stream_id
);
2371 TEST_P(QuicConnectionTest
, DiscardRetransmit
) {
2372 QuicPacketSequenceNumber last_packet
;
2373 SendStreamDataToPeer(1, "foo", 0, !kFin
, &last_packet
); // Packet 1
2374 SendStreamDataToPeer(1, "foos", 3, !kFin
, &last_packet
); // Packet 2
2375 SendStreamDataToPeer(1, "fooos", 7, !kFin
, &last_packet
); // Packet 3
2377 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
2379 // Instigate a loss with an ack.
2380 QuicAckFrame nack_two
= InitAckFrame(3);
2381 NackPacket(2, &nack_two
);
2382 // The first nack should trigger a fast retransmission, but we'll be
2383 // write blocked, so the packet will be queued.
2385 SequenceNumberSet lost_packets
;
2386 lost_packets
.insert(2);
2387 EXPECT_CALL(*loss_algorithm_
, DetectLostPackets(_
, _
, _
, _
))
2388 .WillOnce(Return(lost_packets
));
2389 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
2390 ProcessAckPacket(&nack_two
);
2391 EXPECT_EQ(1u, connection_
.NumQueuedPackets());
2393 // Now, ack the previous transmission.
2394 EXPECT_CALL(*loss_algorithm_
, DetectLostPackets(_
, _
, _
, _
))
2395 .WillOnce(Return(SequenceNumberSet()));
2396 QuicAckFrame ack_all
= InitAckFrame(3);
2397 ProcessAckPacket(&ack_all
);
2399 // Unblock the socket and attempt to send the queued packets. However,
2400 // since the previous transmission has been acked, we will not
2401 // send the retransmission.
2402 EXPECT_CALL(*send_algorithm_
,
2403 OnPacketSent(_
, _
, _
, _
, _
)).Times(0);
2405 writer_
->SetWritable();
2406 connection_
.OnCanWrite();
2408 EXPECT_EQ(0u, connection_
.NumQueuedPackets());
2411 TEST_P(QuicConnectionTest
, RetransmitNackedLargestObserved
) {
2412 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
2413 QuicPacketSequenceNumber largest_observed
;
2414 QuicByteCount packet_size
;
2415 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
))
2416 .WillOnce(DoAll(SaveArg
<2>(&largest_observed
), SaveArg
<3>(&packet_size
),
2418 connection_
.SendStreamDataWithString(3, "foo", 0, !kFin
, nullptr);
2420 QuicAckFrame frame
= InitAckFrame(1);
2421 NackPacket(largest_observed
, &frame
);
2422 // The first nack should retransmit the largest observed packet.
2423 SequenceNumberSet lost_packets
;
2424 lost_packets
.insert(1);
2425 EXPECT_CALL(*loss_algorithm_
, DetectLostPackets(_
, _
, _
, _
))
2426 .WillOnce(Return(lost_packets
));
2427 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
2428 EXPECT_CALL(*send_algorithm_
,
2429 OnPacketSent(_
, _
, _
, packet_size
- kQuicVersionSize
, _
));
2430 ProcessAckPacket(&frame
);
2433 TEST_P(QuicConnectionTest
, QueueAfterTwoRTOs
) {
2434 for (int i
= 0; i
< 10; ++i
) {
2435 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
)).Times(1);
2436 connection_
.SendStreamDataWithString(3, "foo", i
* 3, !kFin
, nullptr);
2439 // Block the writer and ensure they're queued.
2441 clock_
.AdvanceTime(DefaultRetransmissionTime());
2442 // Only one packet should be retransmitted.
2443 connection_
.GetRetransmissionAlarm()->Fire();
2444 EXPECT_TRUE(connection_
.HasQueuedData());
2446 // Unblock the writer.
2447 writer_
->SetWritable();
2448 clock_
.AdvanceTime(QuicTime::Delta::FromMicroseconds(
2449 2 * DefaultRetransmissionTime().ToMicroseconds()));
2450 // Retransmit already retransmitted packets event though the sequence number
2451 // greater than the largest observed.
2452 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
)).Times(2);
2453 connection_
.GetRetransmissionAlarm()->Fire();
2454 connection_
.OnCanWrite();
2457 TEST_P(QuicConnectionTest
, WriteBlockedThenSent
) {
2459 writer_
->set_is_write_blocked_data_buffered(true);
2460 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
)).Times(1);
2461 connection_
.SendStreamDataWithString(1, "foo", 0, !kFin
, nullptr);
2462 EXPECT_TRUE(connection_
.GetRetransmissionAlarm()->IsSet());
2464 writer_
->SetWritable();
2465 connection_
.OnCanWrite();
2466 EXPECT_TRUE(connection_
.GetRetransmissionAlarm()->IsSet());
2469 TEST_P(QuicConnectionTest
, RetransmitWriteBlockedAckedOriginalThenSent
) {
2470 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
2471 connection_
.SendStreamDataWithString(3, "foo", 0, !kFin
, nullptr);
2472 EXPECT_TRUE(connection_
.GetRetransmissionAlarm()->IsSet());
2475 writer_
->set_is_write_blocked_data_buffered(true);
2476 // Simulate the retransmission alarm firing.
2477 clock_
.AdvanceTime(DefaultRetransmissionTime());
2478 connection_
.GetRetransmissionAlarm()->Fire();
2480 // Ack the sent packet before the callback returns, which happens in
2481 // rare circumstances with write blocked sockets.
2482 QuicAckFrame ack
= InitAckFrame(1);
2483 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
2484 ProcessAckPacket(&ack
);
2486 writer_
->SetWritable();
2487 connection_
.OnCanWrite();
2488 // There is now a pending packet, but with no retransmittable frames.
2489 EXPECT_TRUE(connection_
.GetRetransmissionAlarm()->IsSet());
2490 EXPECT_FALSE(connection_
.sent_packet_manager().HasRetransmittableFrames(2));
2493 TEST_P(QuicConnectionTest
, AlarmsWhenWriteBlocked
) {
2494 // Block the connection.
2496 connection_
.SendStreamDataWithString(3, "foo", 0, !kFin
, nullptr);
2497 EXPECT_EQ(1u, writer_
->packets_write_attempts());
2498 EXPECT_TRUE(writer_
->IsWriteBlocked());
2500 // Set the send and resumption alarms. Fire the alarms and ensure they don't
2501 // attempt to write.
2502 connection_
.GetResumeWritesAlarm()->Set(clock_
.ApproximateNow());
2503 connection_
.GetSendAlarm()->Set(clock_
.ApproximateNow());
2504 connection_
.GetResumeWritesAlarm()->Fire();
2505 connection_
.GetSendAlarm()->Fire();
2506 EXPECT_TRUE(writer_
->IsWriteBlocked());
2507 EXPECT_EQ(1u, writer_
->packets_write_attempts());
2510 TEST_P(QuicConnectionTest
, NoLimitPacketsPerNack
) {
2511 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
2513 // Send packets 1 to 15.
2514 for (int i
= 0; i
< 15; ++i
) {
2515 SendStreamDataToPeer(1, "foo", offset
, !kFin
, nullptr);
2519 // Ack 15, nack 1-14.
2520 SequenceNumberSet lost_packets
;
2521 QuicAckFrame nack
= InitAckFrame(15);
2522 for (int i
= 1; i
< 15; ++i
) {
2523 NackPacket(i
, &nack
);
2524 lost_packets
.insert(i
);
2527 // 14 packets have been NACK'd and lost. In TCP cubic, PRR limits
2528 // the retransmission rate in the case of burst losses.
2529 EXPECT_CALL(*loss_algorithm_
, DetectLostPackets(_
, _
, _
, _
))
2530 .WillOnce(Return(lost_packets
));
2531 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
2532 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
)).Times(14);
2533 ProcessAckPacket(&nack
);
2536 // Test sending multiple acks from the connection to the session.
2537 TEST_P(QuicConnectionTest
, MultipleAcks
) {
2538 QuicPacketSequenceNumber last_packet
;
2539 SendStreamDataToPeer(1, "foo", 0, !kFin
, &last_packet
); // Packet 1
2540 EXPECT_EQ(1u, last_packet
);
2541 SendStreamDataToPeer(3, "foo", 0, !kFin
, &last_packet
); // Packet 2
2542 EXPECT_EQ(2u, last_packet
);
2543 SendAckPacketToPeer(); // Packet 3
2544 SendStreamDataToPeer(5, "foo", 0, !kFin
, &last_packet
); // Packet 4
2545 EXPECT_EQ(4u, last_packet
);
2546 SendStreamDataToPeer(1, "foo", 3, !kFin
, &last_packet
); // Packet 5
2547 EXPECT_EQ(5u, last_packet
);
2548 SendStreamDataToPeer(3, "foo", 3, !kFin
, &last_packet
); // Packet 6
2549 EXPECT_EQ(6u, last_packet
);
2551 // Client will ack packets 1, 2, [!3], 4, 5.
2552 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
2553 QuicAckFrame frame1
= InitAckFrame(5);
2554 NackPacket(3, &frame1
);
2555 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
2556 ProcessAckPacket(&frame1
);
2558 // Now the client implicitly acks 3, and explicitly acks 6.
2559 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
2560 QuicAckFrame frame2
= InitAckFrame(6);
2561 ProcessAckPacket(&frame2
);
2564 TEST_P(QuicConnectionTest
, DontLatchUnackedPacket
) {
2565 SendStreamDataToPeer(1, "foo", 0, !kFin
, nullptr); // Packet 1;
2566 // From now on, we send acks, so the send algorithm won't mark them pending.
2567 ON_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
))
2568 .WillByDefault(Return(false));
2569 SendAckPacketToPeer(); // Packet 2
2571 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
2572 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
2573 QuicAckFrame frame
= InitAckFrame(1);
2574 ProcessAckPacket(&frame
);
2576 // Verify that our internal state has least-unacked as 2, because we're still
2577 // waiting for a potential ack for 2.
2579 EXPECT_EQ(2u, stop_waiting()->least_unacked
);
2581 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
2582 frame
= InitAckFrame(2);
2583 ProcessAckPacket(&frame
);
2584 EXPECT_EQ(3u, stop_waiting()->least_unacked
);
2586 // When we send an ack, we make sure our least-unacked makes sense. In this
2587 // case since we're not waiting on an ack for 2 and all packets are acked, we
2589 SendAckPacketToPeer(); // Packet 3
2590 // Least_unacked remains at 3 until another ack is received.
2591 EXPECT_EQ(3u, stop_waiting()->least_unacked
);
2592 // Check that the outgoing ack had its sequence number as least_unacked.
2593 EXPECT_EQ(3u, least_unacked());
2595 // Ack the ack, which updates the rtt and raises the least unacked.
2596 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
2597 frame
= InitAckFrame(3);
2598 ProcessAckPacket(&frame
);
2600 ON_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
))
2601 .WillByDefault(Return(true));
2602 SendStreamDataToPeer(1, "bar", 3, false, nullptr); // Packet 4
2603 EXPECT_EQ(4u, stop_waiting()->least_unacked
);
2604 ON_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
))
2605 .WillByDefault(Return(false));
2606 SendAckPacketToPeer(); // Packet 5
2607 EXPECT_EQ(4u, least_unacked());
2609 // Send two data packets at the end, and ensure if the last one is acked,
2610 // the least unacked is raised above the ack packets.
2611 ON_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
))
2612 .WillByDefault(Return(true));
2613 SendStreamDataToPeer(1, "bar", 6, false, nullptr); // Packet 6
2614 SendStreamDataToPeer(1, "bar", 9, false, nullptr); // Packet 7
2616 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
2617 frame
= InitAckFrame(7);
2618 NackPacket(5, &frame
);
2619 NackPacket(6, &frame
);
2620 ProcessAckPacket(&frame
);
2622 EXPECT_EQ(6u, stop_waiting()->least_unacked
);
2625 TEST_P(QuicConnectionTest
, ReviveMissingPacketAfterFecPacket
) {
2626 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
2628 // Don't send missing packet 1.
2629 ProcessFecPacket(2, 1, true, !kEntropyFlag
, nullptr);
2630 // Entropy flag should be false, so entropy should be 0.
2631 EXPECT_EQ(0u, QuicConnectionPeer::ReceivedEntropyHash(&connection_
, 2));
2634 TEST_P(QuicConnectionTest
, ReviveMissingPacketWithVaryingSeqNumLengths
) {
2635 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
2637 // Set up a debug visitor to the connection.
2638 scoped_ptr
<FecQuicConnectionDebugVisitor
> fec_visitor(
2639 new FecQuicConnectionDebugVisitor());
2640 connection_
.set_debug_visitor(fec_visitor
.get());
2642 QuicPacketSequenceNumber fec_packet
= 0;
2643 QuicSequenceNumberLength lengths
[] = {PACKET_6BYTE_SEQUENCE_NUMBER
,
2644 PACKET_4BYTE_SEQUENCE_NUMBER
,
2645 PACKET_2BYTE_SEQUENCE_NUMBER
,
2646 PACKET_1BYTE_SEQUENCE_NUMBER
};
2647 // For each sequence number length size, revive a packet and check sequence
2648 // number length in the revived packet.
2649 for (size_t i
= 0; i
< arraysize(lengths
); ++i
) {
2650 // Set sequence_number_length_ (for data and FEC packets).
2651 sequence_number_length_
= lengths
[i
];
2653 // Don't send missing packet, but send fec packet right after it.
2654 ProcessFecPacket(fec_packet
, fec_packet
- 1, true, !kEntropyFlag
, nullptr);
2655 // Sequence number length in the revived header should be the same as
2656 // in the original data/fec packet headers.
2657 EXPECT_EQ(sequence_number_length_
, fec_visitor
->revived_header().
2658 public_header
.sequence_number_length
);
2662 TEST_P(QuicConnectionTest
, ReviveMissingPacketWithVaryingConnectionIdLengths
) {
2663 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
2665 // Set up a debug visitor to the connection.
2666 scoped_ptr
<FecQuicConnectionDebugVisitor
> fec_visitor(
2667 new FecQuicConnectionDebugVisitor());
2668 connection_
.set_debug_visitor(fec_visitor
.get());
2670 QuicPacketSequenceNumber fec_packet
= 0;
2671 QuicConnectionIdLength lengths
[] = {PACKET_8BYTE_CONNECTION_ID
,
2672 PACKET_4BYTE_CONNECTION_ID
,
2673 PACKET_1BYTE_CONNECTION_ID
,
2674 PACKET_0BYTE_CONNECTION_ID
};
2675 // For each connection id length size, revive a packet and check connection
2676 // id length in the revived packet.
2677 for (size_t i
= 0; i
< arraysize(lengths
); ++i
) {
2678 // Set connection id length (for data and FEC packets).
2679 connection_id_length_
= lengths
[i
];
2681 // Don't send missing packet, but send fec packet right after it.
2682 ProcessFecPacket(fec_packet
, fec_packet
- 1, true, !kEntropyFlag
, nullptr);
2683 // Connection id length in the revived header should be the same as
2684 // in the original data/fec packet headers.
2685 EXPECT_EQ(connection_id_length_
,
2686 fec_visitor
->revived_header().public_header
.connection_id_length
);
2690 TEST_P(QuicConnectionTest
, ReviveMissingPacketAfterDataPacketThenFecPacket
) {
2691 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
2693 ProcessFecProtectedPacket(1, false, kEntropyFlag
);
2694 // Don't send missing packet 2.
2695 ProcessFecPacket(3, 1, true, !kEntropyFlag
, nullptr);
2696 // Entropy flag should be true, so entropy should not be 0.
2697 EXPECT_NE(0u, QuicConnectionPeer::ReceivedEntropyHash(&connection_
, 2));
2700 TEST_P(QuicConnectionTest
, ReviveMissingPacketAfterDataPacketsThenFecPacket
) {
2701 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
2703 ProcessFecProtectedPacket(1, false, !kEntropyFlag
);
2704 // Don't send missing packet 2.
2705 ProcessFecProtectedPacket(3, false, !kEntropyFlag
);
2706 ProcessFecPacket(4, 1, true, kEntropyFlag
, nullptr);
2707 // Ensure QUIC no longer revives entropy for lost packets.
2708 EXPECT_EQ(0u, QuicConnectionPeer::ReceivedEntropyHash(&connection_
, 2));
2709 EXPECT_NE(0u, QuicConnectionPeer::ReceivedEntropyHash(&connection_
, 4));
2712 TEST_P(QuicConnectionTest
, ReviveMissingPacketAfterDataPacket
) {
2713 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
2715 // Don't send missing packet 1.
2716 ProcessFecPacket(3, 1, false, !kEntropyFlag
, nullptr);
2718 ProcessFecProtectedPacket(2, true, !kEntropyFlag
);
2719 // Entropy flag should be false, so entropy should be 0.
2720 EXPECT_EQ(0u, QuicConnectionPeer::ReceivedEntropyHash(&connection_
, 2));
2723 TEST_P(QuicConnectionTest
, ReviveMissingPacketAfterDataPackets
) {
2724 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
2726 ProcessFecProtectedPacket(1, false, !kEntropyFlag
);
2727 // Don't send missing packet 2.
2728 ProcessFecPacket(6, 1, false, kEntropyFlag
, nullptr);
2729 ProcessFecProtectedPacket(3, false, kEntropyFlag
);
2730 ProcessFecProtectedPacket(4, false, kEntropyFlag
);
2731 ProcessFecProtectedPacket(5, true, !kEntropyFlag
);
2732 // Ensure entropy is not revived for the missing packet.
2733 EXPECT_EQ(0u, QuicConnectionPeer::ReceivedEntropyHash(&connection_
, 2));
2734 EXPECT_NE(0u, QuicConnectionPeer::ReceivedEntropyHash(&connection_
, 3));
2737 TEST_P(QuicConnectionTest
, TLP
) {
2738 QuicSentPacketManagerPeer::SetMaxTailLossProbes(manager_
, 1);
2740 SendStreamDataToPeer(3, "foo", 0, !kFin
, nullptr);
2741 EXPECT_EQ(1u, stop_waiting()->least_unacked
);
2742 QuicTime retransmission_time
=
2743 connection_
.GetRetransmissionAlarm()->deadline();
2744 EXPECT_NE(QuicTime::Zero(), retransmission_time
);
2746 EXPECT_EQ(1u, writer_
->header().packet_sequence_number
);
2747 // Simulate the retransmission alarm firing and sending a tlp,
2748 // so send algorithm's OnRetransmissionTimeout is not called.
2749 clock_
.AdvanceTime(retransmission_time
.Subtract(clock_
.Now()));
2750 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, 2u, _
, _
));
2751 connection_
.GetRetransmissionAlarm()->Fire();
2752 EXPECT_EQ(2u, writer_
->header().packet_sequence_number
);
2753 // We do not raise the high water mark yet.
2754 EXPECT_EQ(1u, stop_waiting()->least_unacked
);
2757 TEST_P(QuicConnectionTest
, RTO
) {
2758 QuicTime default_retransmission_time
= clock_
.ApproximateNow().Add(
2759 DefaultRetransmissionTime());
2760 SendStreamDataToPeer(3, "foo", 0, !kFin
, nullptr);
2761 EXPECT_EQ(1u, stop_waiting()->least_unacked
);
2763 EXPECT_EQ(1u, writer_
->header().packet_sequence_number
);
2764 EXPECT_EQ(default_retransmission_time
,
2765 connection_
.GetRetransmissionAlarm()->deadline());
2766 // Simulate the retransmission alarm firing.
2767 clock_
.AdvanceTime(DefaultRetransmissionTime());
2768 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, 2u, _
, _
));
2769 connection_
.GetRetransmissionAlarm()->Fire();
2770 EXPECT_EQ(2u, writer_
->header().packet_sequence_number
);
2771 // We do not raise the high water mark yet.
2772 EXPECT_EQ(1u, stop_waiting()->least_unacked
);
2775 TEST_P(QuicConnectionTest
, RTOWithSameEncryptionLevel
) {
2776 QuicTime default_retransmission_time
= clock_
.ApproximateNow().Add(
2777 DefaultRetransmissionTime());
2778 use_tagging_decrypter();
2780 // A TaggingEncrypter puts kTagSize copies of the given byte (0x01 here) at
2781 // the end of the packet. We can test this to check which encrypter was used.
2782 connection_
.SetEncrypter(ENCRYPTION_NONE
, new TaggingEncrypter(0x01));
2783 SendStreamDataToPeer(3, "foo", 0, !kFin
, nullptr);
2784 EXPECT_EQ(0x01010101u
, writer_
->final_bytes_of_last_packet());
2786 connection_
.SetEncrypter(ENCRYPTION_INITIAL
, new TaggingEncrypter(0x02));
2787 connection_
.SetDefaultEncryptionLevel(ENCRYPTION_INITIAL
);
2788 SendStreamDataToPeer(3, "foo", 0, !kFin
, nullptr);
2789 EXPECT_EQ(0x02020202u
, writer_
->final_bytes_of_last_packet());
2791 EXPECT_EQ(default_retransmission_time
,
2792 connection_
.GetRetransmissionAlarm()->deadline());
2795 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, 3, _
, _
));
2796 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, 4, _
, _
));
2799 // Simulate the retransmission alarm firing.
2800 clock_
.AdvanceTime(DefaultRetransmissionTime());
2801 connection_
.GetRetransmissionAlarm()->Fire();
2803 // Packet should have been sent with ENCRYPTION_NONE.
2804 EXPECT_EQ(0x01010101u
, writer_
->final_bytes_of_previous_packet());
2806 // Packet should have been sent with ENCRYPTION_INITIAL.
2807 EXPECT_EQ(0x02020202u
, writer_
->final_bytes_of_last_packet());
2810 TEST_P(QuicConnectionTest
, SendHandshakeMessages
) {
2811 use_tagging_decrypter();
2812 // A TaggingEncrypter puts kTagSize copies of the given byte (0x01 here) at
2813 // the end of the packet. We can test this to check which encrypter was used.
2814 connection_
.SetEncrypter(ENCRYPTION_NONE
, new TaggingEncrypter(0x01));
2816 // Attempt to send a handshake message and have the socket block.
2817 EXPECT_CALL(*send_algorithm_
,
2818 TimeUntilSend(_
, _
, _
)).WillRepeatedly(
2819 testing::Return(QuicTime::Delta::Zero()));
2821 connection_
.SendStreamDataWithString(1, "foo", 0, !kFin
, nullptr);
2822 // The packet should be serialized, but not queued.
2823 EXPECT_EQ(1u, connection_
.NumQueuedPackets());
2825 // Switch to the new encrypter.
2826 connection_
.SetEncrypter(ENCRYPTION_INITIAL
, new TaggingEncrypter(0x02));
2827 connection_
.SetDefaultEncryptionLevel(ENCRYPTION_INITIAL
);
2829 // Now become writeable and flush the packets.
2830 writer_
->SetWritable();
2831 EXPECT_CALL(visitor_
, OnCanWrite());
2832 connection_
.OnCanWrite();
2833 EXPECT_EQ(0u, connection_
.NumQueuedPackets());
2835 // Verify that the handshake packet went out at the null encryption.
2836 EXPECT_EQ(0x01010101u
, writer_
->final_bytes_of_last_packet());
2839 TEST_P(QuicConnectionTest
,
2840 DropRetransmitsForNullEncryptedPacketAfterForwardSecure
) {
2841 use_tagging_decrypter();
2842 connection_
.SetEncrypter(ENCRYPTION_NONE
, new TaggingEncrypter(0x01));
2843 QuicPacketSequenceNumber sequence_number
;
2844 SendStreamDataToPeer(3, "foo", 0, !kFin
, &sequence_number
);
2846 // Simulate the retransmission alarm firing and the socket blocking.
2848 clock_
.AdvanceTime(DefaultRetransmissionTime());
2849 connection_
.GetRetransmissionAlarm()->Fire();
2851 // Go forward secure.
2852 connection_
.SetEncrypter(ENCRYPTION_FORWARD_SECURE
,
2853 new TaggingEncrypter(0x02));
2854 connection_
.SetDefaultEncryptionLevel(ENCRYPTION_FORWARD_SECURE
);
2855 connection_
.NeuterUnencryptedPackets();
2857 EXPECT_EQ(QuicTime::Zero(),
2858 connection_
.GetRetransmissionAlarm()->deadline());
2859 // Unblock the socket and ensure that no packets are sent.
2860 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
)).Times(0);
2861 writer_
->SetWritable();
2862 connection_
.OnCanWrite();
2865 TEST_P(QuicConnectionTest
, RetransmitPacketsWithInitialEncryption
) {
2866 use_tagging_decrypter();
2867 connection_
.SetEncrypter(ENCRYPTION_NONE
, new TaggingEncrypter(0x01));
2868 connection_
.SetDefaultEncryptionLevel(ENCRYPTION_NONE
);
2870 SendStreamDataToPeer(1, "foo", 0, !kFin
, nullptr);
2872 connection_
.SetEncrypter(ENCRYPTION_INITIAL
, new TaggingEncrypter(0x02));
2873 connection_
.SetDefaultEncryptionLevel(ENCRYPTION_INITIAL
);
2875 SendStreamDataToPeer(2, "bar", 0, !kFin
, nullptr);
2876 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
)).Times(1);
2878 connection_
.RetransmitUnackedPackets(ALL_INITIAL_RETRANSMISSION
);
2881 TEST_P(QuicConnectionTest
, DelayForwardSecureEncryptionUntilClientIsReady
) {
2882 // A TaggingEncrypter puts kTagSize copies of the given byte (0x02 here) at
2883 // the end of the packet. We can test this to check which encrypter was used.
2884 use_tagging_decrypter();
2885 connection_
.SetEncrypter(ENCRYPTION_INITIAL
, new TaggingEncrypter(0x02));
2886 connection_
.SetDefaultEncryptionLevel(ENCRYPTION_INITIAL
);
2887 SendAckPacketToPeer();
2888 EXPECT_EQ(0x02020202u
, writer_
->final_bytes_of_last_packet());
2890 // Set a forward-secure encrypter but do not make it the default, and verify
2891 // that it is not yet used.
2892 connection_
.SetEncrypter(ENCRYPTION_FORWARD_SECURE
,
2893 new TaggingEncrypter(0x03));
2894 SendAckPacketToPeer();
2895 EXPECT_EQ(0x02020202u
, writer_
->final_bytes_of_last_packet());
2897 // Now simulate receipt of a forward-secure packet and verify that the
2898 // forward-secure encrypter is now used.
2899 connection_
.OnDecryptedPacket(ENCRYPTION_FORWARD_SECURE
);
2900 SendAckPacketToPeer();
2901 EXPECT_EQ(0x03030303u
, writer_
->final_bytes_of_last_packet());
2904 TEST_P(QuicConnectionTest
, DelayForwardSecureEncryptionUntilManyPacketSent
) {
2905 // Set a congestion window of 10 packets.
2906 QuicPacketCount congestion_window
= 10;
2907 EXPECT_CALL(*send_algorithm_
, GetCongestionWindow()).WillRepeatedly(
2908 Return(congestion_window
* kDefaultMaxPacketSize
));
2910 // A TaggingEncrypter puts kTagSize copies of the given byte (0x02 here) at
2911 // the end of the packet. We can test this to check which encrypter was used.
2912 use_tagging_decrypter();
2913 connection_
.SetEncrypter(ENCRYPTION_INITIAL
, new TaggingEncrypter(0x02));
2914 connection_
.SetDefaultEncryptionLevel(ENCRYPTION_INITIAL
);
2915 SendAckPacketToPeer();
2916 EXPECT_EQ(0x02020202u
, writer_
->final_bytes_of_last_packet());
2918 // Set a forward-secure encrypter but do not make it the default, and
2919 // verify that it is not yet used.
2920 connection_
.SetEncrypter(ENCRYPTION_FORWARD_SECURE
,
2921 new TaggingEncrypter(0x03));
2922 SendAckPacketToPeer();
2923 EXPECT_EQ(0x02020202u
, writer_
->final_bytes_of_last_packet());
2925 // Now send a packet "Far enough" after the encrypter was set and verify that
2926 // the forward-secure encrypter is now used.
2927 for (uint64 i
= 0; i
< 3 * congestion_window
- 1; ++i
) {
2928 EXPECT_EQ(0x02020202u
, writer_
->final_bytes_of_last_packet());
2929 SendAckPacketToPeer();
2931 EXPECT_EQ(0x03030303u
, writer_
->final_bytes_of_last_packet());
2934 TEST_P(QuicConnectionTest
, BufferNonDecryptablePackets
) {
2935 // SetFromConfig is always called after construction from InitializeSession.
2936 EXPECT_CALL(*send_algorithm_
, SetFromConfig(_
, _
));
2938 connection_
.SetFromConfig(config
);
2939 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
2940 use_tagging_decrypter();
2942 const uint8 tag
= 0x07;
2943 framer_
.SetEncrypter(ENCRYPTION_INITIAL
, new TaggingEncrypter(tag
));
2945 // Process an encrypted packet which can not yet be decrypted which should
2946 // result in the packet being buffered.
2947 ProcessDataPacketAtLevel(1, 0, kEntropyFlag
, ENCRYPTION_INITIAL
);
2949 // Transition to the new encryption state and process another encrypted packet
2950 // which should result in the original packet being processed.
2951 connection_
.SetDecrypter(ENCRYPTION_INITIAL
, new StrictTaggingDecrypter(tag
));
2952 connection_
.SetDefaultEncryptionLevel(ENCRYPTION_INITIAL
);
2953 connection_
.SetEncrypter(ENCRYPTION_INITIAL
, new TaggingEncrypter(tag
));
2954 EXPECT_CALL(visitor_
, OnStreamFrames(_
)).Times(2);
2955 ProcessDataPacketAtLevel(2, 0, kEntropyFlag
, ENCRYPTION_INITIAL
);
2957 // Finally, process a third packet and note that we do not reprocess the
2959 EXPECT_CALL(visitor_
, OnStreamFrames(_
)).Times(1);
2960 ProcessDataPacketAtLevel(3, 0, kEntropyFlag
, ENCRYPTION_INITIAL
);
2963 TEST_P(QuicConnectionTest
, Buffer100NonDecryptablePackets
) {
2964 // SetFromConfig is always called after construction from InitializeSession.
2965 EXPECT_CALL(*send_algorithm_
, SetFromConfig(_
, _
));
2967 config
.set_max_undecryptable_packets(100);
2968 connection_
.SetFromConfig(config
);
2969 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
2970 use_tagging_decrypter();
2972 const uint8 tag
= 0x07;
2973 framer_
.SetEncrypter(ENCRYPTION_INITIAL
, new TaggingEncrypter(tag
));
2975 // Process an encrypted packet which can not yet be decrypted which should
2976 // result in the packet being buffered.
2977 for (QuicPacketSequenceNumber i
= 1; i
<= 100; ++i
) {
2978 ProcessDataPacketAtLevel(i
, 0, kEntropyFlag
, ENCRYPTION_INITIAL
);
2981 // Transition to the new encryption state and process another encrypted packet
2982 // which should result in the original packets being processed.
2983 connection_
.SetDecrypter(ENCRYPTION_INITIAL
, new StrictTaggingDecrypter(tag
));
2984 connection_
.SetDefaultEncryptionLevel(ENCRYPTION_INITIAL
);
2985 connection_
.SetEncrypter(ENCRYPTION_INITIAL
, new TaggingEncrypter(tag
));
2986 EXPECT_CALL(visitor_
, OnStreamFrames(_
)).Times(101);
2987 ProcessDataPacketAtLevel(101, 0, kEntropyFlag
, ENCRYPTION_INITIAL
);
2989 // Finally, process a third packet and note that we do not reprocess the
2991 EXPECT_CALL(visitor_
, OnStreamFrames(_
)).Times(1);
2992 ProcessDataPacketAtLevel(102, 0, kEntropyFlag
, ENCRYPTION_INITIAL
);
2995 TEST_P(QuicConnectionTest
, TestRetransmitOrder
) {
2996 QuicByteCount first_packet_size
;
2997 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
)).WillOnce(
2998 DoAll(SaveArg
<3>(&first_packet_size
), Return(true)));
3000 connection_
.SendStreamDataWithString(3, "first_packet", 0, !kFin
, nullptr);
3001 QuicByteCount second_packet_size
;
3002 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
)).WillOnce(
3003 DoAll(SaveArg
<3>(&second_packet_size
), Return(true)));
3004 connection_
.SendStreamDataWithString(3, "second_packet", 12, !kFin
, nullptr);
3005 EXPECT_NE(first_packet_size
, second_packet_size
);
3006 // Advance the clock by huge time to make sure packets will be retransmitted.
3007 clock_
.AdvanceTime(QuicTime::Delta::FromSeconds(10));
3010 EXPECT_CALL(*send_algorithm_
,
3011 OnPacketSent(_
, _
, _
, first_packet_size
, _
));
3012 EXPECT_CALL(*send_algorithm_
,
3013 OnPacketSent(_
, _
, _
, second_packet_size
, _
));
3015 connection_
.GetRetransmissionAlarm()->Fire();
3017 // Advance again and expect the packets to be sent again in the same order.
3018 clock_
.AdvanceTime(QuicTime::Delta::FromSeconds(20));
3021 EXPECT_CALL(*send_algorithm_
,
3022 OnPacketSent(_
, _
, _
, first_packet_size
, _
));
3023 EXPECT_CALL(*send_algorithm_
,
3024 OnPacketSent(_
, _
, _
, second_packet_size
, _
));
3026 connection_
.GetRetransmissionAlarm()->Fire();
3029 TEST_P(QuicConnectionTest
, SetRTOAfterWritingToSocket
) {
3031 connection_
.SendStreamDataWithString(1, "foo", 0, !kFin
, nullptr);
3032 // Make sure that RTO is not started when the packet is queued.
3033 EXPECT_FALSE(connection_
.GetRetransmissionAlarm()->IsSet());
3035 // Test that RTO is started once we write to the socket.
3036 writer_
->SetWritable();
3037 connection_
.OnCanWrite();
3038 EXPECT_TRUE(connection_
.GetRetransmissionAlarm()->IsSet());
3041 TEST_P(QuicConnectionTest
, DelayRTOWithAckReceipt
) {
3042 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
3043 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
))
3045 connection_
.SendStreamDataWithString(2, "foo", 0, !kFin
, nullptr);
3046 connection_
.SendStreamDataWithString(3, "bar", 0, !kFin
, nullptr);
3047 QuicAlarm
* retransmission_alarm
= connection_
.GetRetransmissionAlarm();
3048 EXPECT_TRUE(retransmission_alarm
->IsSet());
3049 EXPECT_EQ(clock_
.Now().Add(DefaultRetransmissionTime()),
3050 retransmission_alarm
->deadline());
3052 // Advance the time right before the RTO, then receive an ack for the first
3053 // packet to delay the RTO.
3054 clock_
.AdvanceTime(DefaultRetransmissionTime());
3055 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
3056 QuicAckFrame ack
= InitAckFrame(1);
3057 ProcessAckPacket(&ack
);
3058 EXPECT_TRUE(retransmission_alarm
->IsSet());
3059 EXPECT_GT(retransmission_alarm
->deadline(), clock_
.Now());
3061 // Move forward past the original RTO and ensure the RTO is still pending.
3062 clock_
.AdvanceTime(DefaultRetransmissionTime().Multiply(2));
3064 // Ensure the second packet gets retransmitted when it finally fires.
3065 EXPECT_TRUE(retransmission_alarm
->IsSet());
3066 EXPECT_LT(retransmission_alarm
->deadline(), clock_
.ApproximateNow());
3067 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
));
3068 // Manually cancel the alarm to simulate a real test.
3069 connection_
.GetRetransmissionAlarm()->Fire();
3071 // The new retransmitted sequence number should set the RTO to a larger value
3073 EXPECT_TRUE(retransmission_alarm
->IsSet());
3074 QuicTime next_rto_time
= retransmission_alarm
->deadline();
3075 QuicTime expected_rto_time
=
3076 connection_
.sent_packet_manager().GetRetransmissionTime();
3077 EXPECT_EQ(next_rto_time
, expected_rto_time
);
3080 TEST_P(QuicConnectionTest
, TestQueued
) {
3081 EXPECT_EQ(0u, connection_
.NumQueuedPackets());
3083 connection_
.SendStreamDataWithString(1, "foo", 0, !kFin
, nullptr);
3084 EXPECT_EQ(1u, connection_
.NumQueuedPackets());
3086 // Unblock the writes and actually send.
3087 writer_
->SetWritable();
3088 connection_
.OnCanWrite();
3089 EXPECT_EQ(0u, connection_
.NumQueuedPackets());
3092 TEST_P(QuicConnectionTest
, CloseFecGroup
) {
3093 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
3094 // Don't send missing packet 1.
3095 // Don't send missing packet 2.
3096 ProcessFecProtectedPacket(3, false, !kEntropyFlag
);
3097 // Don't send missing FEC packet 3.
3098 ASSERT_EQ(1u, connection_
.NumFecGroups());
3100 // Now send non-fec protected ack packet and close the group.
3101 QuicPacketCreatorPeer::SetSequenceNumber(&peer_creator_
, 4);
3102 QuicStopWaitingFrame frame
= InitStopWaitingFrame(5);
3103 ProcessStopWaitingPacket(&frame
);
3104 ASSERT_EQ(0u, connection_
.NumFecGroups());
3107 TEST_P(QuicConnectionTest
, InitialTimeout
) {
3108 EXPECT_TRUE(connection_
.connected());
3109 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
)).Times(AnyNumber());
3110 EXPECT_FALSE(connection_
.GetTimeoutAlarm()->IsSet());
3112 // SetFromConfig sets the initial timeouts before negotiation.
3113 EXPECT_CALL(*send_algorithm_
, SetFromConfig(_
, _
));
3115 connection_
.SetFromConfig(config
);
3116 // Subtract a second from the idle timeout on the client side.
3117 QuicTime default_timeout
= clock_
.ApproximateNow().Add(
3118 QuicTime::Delta::FromSeconds(kInitialIdleTimeoutSecs
- 1));
3119 EXPECT_EQ(default_timeout
, connection_
.GetTimeoutAlarm()->deadline());
3121 EXPECT_CALL(visitor_
, OnConnectionClosed(QUIC_CONNECTION_TIMED_OUT
, false));
3122 // Simulate the timeout alarm firing.
3124 QuicTime::Delta::FromSeconds(kInitialIdleTimeoutSecs
- 1));
3125 connection_
.GetTimeoutAlarm()->Fire();
3127 EXPECT_FALSE(connection_
.GetTimeoutAlarm()->IsSet());
3128 EXPECT_FALSE(connection_
.connected());
3130 EXPECT_FALSE(connection_
.GetAckAlarm()->IsSet());
3131 EXPECT_FALSE(connection_
.GetPingAlarm()->IsSet());
3132 EXPECT_FALSE(connection_
.GetFecAlarm()->IsSet());
3133 EXPECT_FALSE(connection_
.GetResumeWritesAlarm()->IsSet());
3134 EXPECT_FALSE(connection_
.GetRetransmissionAlarm()->IsSet());
3135 EXPECT_FALSE(connection_
.GetSendAlarm()->IsSet());
3138 TEST_P(QuicConnectionTest
, OverallTimeout
) {
3139 // Use a shorter overall connection timeout than idle timeout for this test.
3140 const QuicTime::Delta timeout
= QuicTime::Delta::FromSeconds(5);
3141 connection_
.SetNetworkTimeouts(timeout
, timeout
);
3142 EXPECT_TRUE(connection_
.connected());
3143 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
)).Times(AnyNumber());
3145 QuicTime overall_timeout
= clock_
.ApproximateNow().Add(timeout
).Subtract(
3146 QuicTime::Delta::FromSeconds(1));
3147 EXPECT_EQ(overall_timeout
, connection_
.GetTimeoutAlarm()->deadline());
3148 EXPECT_TRUE(connection_
.connected());
3150 // Send and ack new data 3 seconds later to lengthen the idle timeout.
3151 SendStreamDataToPeer(1, "GET /", 0, kFin
, nullptr);
3152 clock_
.AdvanceTime(QuicTime::Delta::FromSeconds(3));
3153 QuicAckFrame frame
= InitAckFrame(1);
3154 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
3155 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
3156 ProcessAckPacket(&frame
);
3158 // Fire early to verify it wouldn't timeout yet.
3159 connection_
.GetTimeoutAlarm()->Fire();
3160 EXPECT_TRUE(connection_
.GetTimeoutAlarm()->IsSet());
3161 EXPECT_TRUE(connection_
.connected());
3163 clock_
.AdvanceTime(timeout
.Subtract(QuicTime::Delta::FromSeconds(2)));
3165 EXPECT_CALL(visitor_
,
3166 OnConnectionClosed(QUIC_CONNECTION_OVERALL_TIMED_OUT
, false));
3167 // Simulate the timeout alarm firing.
3168 connection_
.GetTimeoutAlarm()->Fire();
3170 EXPECT_FALSE(connection_
.GetTimeoutAlarm()->IsSet());
3171 EXPECT_FALSE(connection_
.connected());
3173 EXPECT_FALSE(connection_
.GetAckAlarm()->IsSet());
3174 EXPECT_FALSE(connection_
.GetPingAlarm()->IsSet());
3175 EXPECT_FALSE(connection_
.GetFecAlarm()->IsSet());
3176 EXPECT_FALSE(connection_
.GetResumeWritesAlarm()->IsSet());
3177 EXPECT_FALSE(connection_
.GetRetransmissionAlarm()->IsSet());
3178 EXPECT_FALSE(connection_
.GetSendAlarm()->IsSet());
3181 TEST_P(QuicConnectionTest
, PingAfterSend
) {
3182 EXPECT_TRUE(connection_
.connected());
3183 EXPECT_CALL(visitor_
, HasOpenDynamicStreams()).WillRepeatedly(Return(true));
3184 EXPECT_FALSE(connection_
.GetPingAlarm()->IsSet());
3186 // Advance to 5ms, and send a packet to the peer, which will set
3188 clock_
.AdvanceTime(QuicTime::Delta::FromMilliseconds(5));
3189 EXPECT_FALSE(connection_
.GetRetransmissionAlarm()->IsSet());
3190 SendStreamDataToPeer(1, "GET /", 0, kFin
, nullptr);
3191 EXPECT_TRUE(connection_
.GetPingAlarm()->IsSet());
3192 EXPECT_EQ(clock_
.ApproximateNow().Add(QuicTime::Delta::FromSeconds(15)),
3193 connection_
.GetPingAlarm()->deadline());
3195 // Now recevie and ACK of the previous packet, which will move the
3196 // ping alarm forward.
3197 clock_
.AdvanceTime(QuicTime::Delta::FromMilliseconds(5));
3198 QuicAckFrame frame
= InitAckFrame(1);
3199 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
3200 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
3201 ProcessAckPacket(&frame
);
3202 EXPECT_TRUE(connection_
.GetPingAlarm()->IsSet());
3203 // The ping timer is set slightly less than 15 seconds in the future, because
3204 // of the 1s ping timer alarm granularity.
3205 EXPECT_EQ(clock_
.ApproximateNow().Add(QuicTime::Delta::FromSeconds(15))
3206 .Subtract(QuicTime::Delta::FromMilliseconds(5)),
3207 connection_
.GetPingAlarm()->deadline());
3210 clock_
.AdvanceTime(QuicTime::Delta::FromSeconds(15));
3211 connection_
.GetPingAlarm()->Fire();
3212 EXPECT_EQ(1u, writer_
->frame_count());
3213 ASSERT_EQ(1u, writer_
->ping_frames().size());
3216 EXPECT_CALL(visitor_
, HasOpenDynamicStreams()).WillRepeatedly(Return(false));
3217 clock_
.AdvanceTime(QuicTime::Delta::FromMilliseconds(5));
3218 SendAckPacketToPeer();
3220 EXPECT_FALSE(connection_
.GetPingAlarm()->IsSet());
3223 TEST_P(QuicConnectionTest
, TimeoutAfterSend
) {
3224 EXPECT_TRUE(connection_
.connected());
3225 EXPECT_CALL(*send_algorithm_
, SetFromConfig(_
, _
));
3227 connection_
.SetFromConfig(config
);
3228 EXPECT_FALSE(QuicConnectionPeer::IsSilentCloseEnabled(&connection_
));
3230 const QuicTime::Delta initial_idle_timeout
=
3231 QuicTime::Delta::FromSeconds(kInitialIdleTimeoutSecs
- 1);
3232 const QuicTime::Delta five_ms
= QuicTime::Delta::FromMilliseconds(5);
3233 QuicTime default_timeout
= clock_
.ApproximateNow().Add(initial_idle_timeout
);
3235 // When we send a packet, the timeout will change to 5ms +
3236 // kInitialIdleTimeoutSecs.
3237 clock_
.AdvanceTime(five_ms
);
3239 // Send an ack so we don't set the retransmission alarm.
3240 SendAckPacketToPeer();
3241 EXPECT_EQ(default_timeout
, connection_
.GetTimeoutAlarm()->deadline());
3243 // The original alarm will fire. We should not time out because we had a
3244 // network event at t=5ms. The alarm will reregister.
3245 clock_
.AdvanceTime(initial_idle_timeout
.Subtract(five_ms
));
3246 EXPECT_EQ(default_timeout
, clock_
.ApproximateNow());
3247 connection_
.GetTimeoutAlarm()->Fire();
3248 EXPECT_TRUE(connection_
.GetTimeoutAlarm()->IsSet());
3249 EXPECT_TRUE(connection_
.connected());
3250 EXPECT_EQ(default_timeout
.Add(five_ms
),
3251 connection_
.GetTimeoutAlarm()->deadline());
3253 // This time, we should time out.
3254 EXPECT_CALL(visitor_
, OnConnectionClosed(QUIC_CONNECTION_TIMED_OUT
, false));
3255 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
));
3256 clock_
.AdvanceTime(five_ms
);
3257 EXPECT_EQ(default_timeout
.Add(five_ms
), clock_
.ApproximateNow());
3258 connection_
.GetTimeoutAlarm()->Fire();
3259 EXPECT_FALSE(connection_
.GetTimeoutAlarm()->IsSet());
3260 EXPECT_FALSE(connection_
.connected());
3263 TEST_P(QuicConnectionTest
, TimeoutAfterSendSilentClose
) {
3264 // Same test as above, but complete a handshake which enables silent close,
3265 // causing no connection close packet to be sent.
3266 EXPECT_TRUE(connection_
.connected());
3267 EXPECT_CALL(*send_algorithm_
, SetFromConfig(_
, _
));
3270 // Create a handshake message that also enables silent close.
3271 CryptoHandshakeMessage msg
;
3272 string error_details
;
3273 QuicConfig client_config
;
3274 client_config
.SetInitialStreamFlowControlWindowToSend(
3275 kInitialStreamFlowControlWindowForTest
);
3276 client_config
.SetInitialSessionFlowControlWindowToSend(
3277 kInitialSessionFlowControlWindowForTest
);
3278 client_config
.SetIdleConnectionStateLifetime(
3279 QuicTime::Delta::FromSeconds(kDefaultIdleTimeoutSecs
),
3280 QuicTime::Delta::FromSeconds(kDefaultIdleTimeoutSecs
));
3281 client_config
.ToHandshakeMessage(&msg
);
3282 const QuicErrorCode error
=
3283 config
.ProcessPeerHello(msg
, CLIENT
, &error_details
);
3284 EXPECT_EQ(QUIC_NO_ERROR
, error
);
3286 connection_
.SetFromConfig(config
);
3287 EXPECT_TRUE(QuicConnectionPeer::IsSilentCloseEnabled(&connection_
));
3289 const QuicTime::Delta default_idle_timeout
=
3290 QuicTime::Delta::FromSeconds(kDefaultIdleTimeoutSecs
- 1);
3291 const QuicTime::Delta five_ms
= QuicTime::Delta::FromMilliseconds(5);
3292 QuicTime default_timeout
= clock_
.ApproximateNow().Add(default_idle_timeout
);
3294 // When we send a packet, the timeout will change to 5ms +
3295 // kInitialIdleTimeoutSecs.
3296 clock_
.AdvanceTime(five_ms
);
3298 // Send an ack so we don't set the retransmission alarm.
3299 SendAckPacketToPeer();
3300 EXPECT_EQ(default_timeout
, connection_
.GetTimeoutAlarm()->deadline());
3302 // The original alarm will fire. We should not time out because we had a
3303 // network event at t=5ms. The alarm will reregister.
3304 clock_
.AdvanceTime(default_idle_timeout
.Subtract(five_ms
));
3305 EXPECT_EQ(default_timeout
, clock_
.ApproximateNow());
3306 connection_
.GetTimeoutAlarm()->Fire();
3307 EXPECT_TRUE(connection_
.GetTimeoutAlarm()->IsSet());
3308 EXPECT_TRUE(connection_
.connected());
3309 EXPECT_EQ(default_timeout
.Add(five_ms
),
3310 connection_
.GetTimeoutAlarm()->deadline());
3312 // This time, we should time out.
3313 EXPECT_CALL(visitor_
, OnConnectionClosed(QUIC_CONNECTION_TIMED_OUT
, false));
3314 clock_
.AdvanceTime(five_ms
);
3315 EXPECT_EQ(default_timeout
.Add(five_ms
), clock_
.ApproximateNow());
3316 connection_
.GetTimeoutAlarm()->Fire();
3317 EXPECT_FALSE(connection_
.GetTimeoutAlarm()->IsSet());
3318 EXPECT_FALSE(connection_
.connected());
3321 TEST_P(QuicConnectionTest
, SendScheduler
) {
3322 // Test that if we send a packet without delay, it is not queued.
3323 QuicPacket
* packet
= ConstructDataPacket(1, 0, !kEntropyFlag
);
3324 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
));
3325 connection_
.SendPacket(
3326 ENCRYPTION_NONE
, 1, packet
, kTestEntropyHash
, HAS_RETRANSMITTABLE_DATA
);
3327 EXPECT_EQ(0u, connection_
.NumQueuedPackets());
3330 TEST_P(QuicConnectionTest
, SendSchedulerEAGAIN
) {
3331 QuicPacket
* packet
= ConstructDataPacket(1, 0, !kEntropyFlag
);
3333 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, 1, _
, _
)).Times(0);
3334 connection_
.SendPacket(
3335 ENCRYPTION_NONE
, 1, packet
, kTestEntropyHash
, HAS_RETRANSMITTABLE_DATA
);
3336 EXPECT_EQ(1u, connection_
.NumQueuedPackets());
3339 TEST_P(QuicConnectionTest
, TestQueueLimitsOnSendStreamData
) {
3340 // All packets carry version info till version is negotiated.
3341 size_t payload_length
;
3342 size_t length
= GetPacketLengthForOneStream(
3343 connection_
.version(), kIncludeVersion
,
3344 PACKET_8BYTE_CONNECTION_ID
, PACKET_1BYTE_SEQUENCE_NUMBER
,
3345 NOT_IN_FEC_GROUP
, &payload_length
);
3346 connection_
.set_max_packet_length(length
);
3348 // Queue the first packet.
3349 EXPECT_CALL(*send_algorithm_
,
3350 TimeUntilSend(_
, _
, _
)).WillOnce(
3351 testing::Return(QuicTime::Delta::FromMicroseconds(10)));
3352 const string
payload(payload_length
, 'a');
3353 EXPECT_EQ(0u, connection_
.SendStreamDataWithString(3, payload
, 0, !kFin
,
3354 nullptr).bytes_consumed
);
3355 EXPECT_EQ(0u, connection_
.NumQueuedPackets());
3358 TEST_P(QuicConnectionTest
, LoopThroughSendingPackets
) {
3359 // All packets carry version info till version is negotiated.
3360 size_t payload_length
;
3361 // GetPacketLengthForOneStream() assumes a stream offset of 0 in determining
3362 // packet length. The size of the offset field in a stream frame is 0 for
3363 // offset 0, and 2 for non-zero offsets up through 16K. Increase
3364 // max_packet_length by 2 so that subsequent packets containing subsequent
3365 // stream frames with non-zero offets will fit within the packet length.
3366 size_t length
= 2 + GetPacketLengthForOneStream(
3367 connection_
.version(), kIncludeVersion
,
3368 PACKET_8BYTE_CONNECTION_ID
, PACKET_1BYTE_SEQUENCE_NUMBER
,
3369 NOT_IN_FEC_GROUP
, &payload_length
);
3370 connection_
.set_max_packet_length(length
);
3372 // Queue the first packet.
3373 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
)).Times(7);
3374 // The first stream frame will have 2 fewer overhead bytes than the other six.
3375 const string
payload(payload_length
* 7 + 2, 'a');
3376 EXPECT_EQ(payload
.size(),
3377 connection_
.SendStreamDataWithString(1, payload
, 0, !kFin
, nullptr)
3381 TEST_P(QuicConnectionTest
, LoopThroughSendingPacketsWithTruncation
) {
3382 // Set up a larger payload than will fit in one packet.
3383 const string
payload(connection_
.max_packet_length(), 'a');
3384 EXPECT_CALL(*send_algorithm_
, SetFromConfig(_
, _
)).Times(AnyNumber());
3386 // Now send some packets with no truncation.
3387 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
)).Times(2);
3388 EXPECT_EQ(payload
.size(),
3389 connection_
.SendStreamDataWithString(
3390 3, payload
, 0, !kFin
, nullptr).bytes_consumed
);
3391 // Track the size of the second packet here. The overhead will be the largest
3392 // we see in this test, due to the non-truncated connection id.
3393 size_t non_truncated_packet_size
= writer_
->last_packet_size();
3395 // Change to a 4 byte connection id.
3397 QuicConfigPeer::SetReceivedBytesForConnectionId(&config
, 4);
3398 connection_
.SetFromConfig(config
);
3399 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
)).Times(2);
3400 EXPECT_EQ(payload
.size(),
3401 connection_
.SendStreamDataWithString(
3402 3, payload
, 0, !kFin
, nullptr).bytes_consumed
);
3403 // Verify that we have 8 fewer bytes than in the non-truncated case. The
3404 // first packet got 4 bytes of extra payload due to the truncation, and the
3405 // headers here are also 4 byte smaller.
3406 EXPECT_EQ(non_truncated_packet_size
, writer_
->last_packet_size() + 8);
3408 // Change to a 1 byte connection id.
3409 QuicConfigPeer::SetReceivedBytesForConnectionId(&config
, 1);
3410 connection_
.SetFromConfig(config
);
3411 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
)).Times(2);
3412 EXPECT_EQ(payload
.size(),
3413 connection_
.SendStreamDataWithString(
3414 3, payload
, 0, !kFin
, nullptr).bytes_consumed
);
3415 // Just like above, we save 7 bytes on payload, and 7 on truncation.
3416 EXPECT_EQ(non_truncated_packet_size
, writer_
->last_packet_size() + 7 * 2);
3418 // Change to a 0 byte connection id.
3419 QuicConfigPeer::SetReceivedBytesForConnectionId(&config
, 0);
3420 connection_
.SetFromConfig(config
);
3421 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
)).Times(2);
3422 EXPECT_EQ(payload
.size(),
3423 connection_
.SendStreamDataWithString(
3424 3, payload
, 0, !kFin
, nullptr).bytes_consumed
);
3425 // Just like above, we save 8 bytes on payload, and 8 on truncation.
3426 EXPECT_EQ(non_truncated_packet_size
, writer_
->last_packet_size() + 8 * 2);
3429 TEST_P(QuicConnectionTest
, SendDelayedAck
) {
3430 QuicTime ack_time
= clock_
.ApproximateNow().Add(DefaultDelayedAckTime());
3431 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
3432 EXPECT_FALSE(connection_
.GetAckAlarm()->IsSet());
3433 const uint8 tag
= 0x07;
3434 connection_
.SetDecrypter(ENCRYPTION_INITIAL
, new StrictTaggingDecrypter(tag
));
3435 framer_
.SetEncrypter(ENCRYPTION_INITIAL
, new TaggingEncrypter(tag
));
3436 // Process a packet from the non-crypto stream.
3437 frame1_
.stream_id
= 3;
3439 // The same as ProcessPacket(1) except that ENCRYPTION_INITIAL is used
3440 // instead of ENCRYPTION_NONE.
3441 EXPECT_CALL(visitor_
, OnStreamFrames(_
)).Times(1);
3442 ProcessDataPacketAtLevel(1, 0, !kEntropyFlag
, ENCRYPTION_INITIAL
);
3444 // Check if delayed ack timer is running for the expected interval.
3445 EXPECT_TRUE(connection_
.GetAckAlarm()->IsSet());
3446 EXPECT_EQ(ack_time
, connection_
.GetAckAlarm()->deadline());
3447 // Simulate delayed ack alarm firing.
3448 connection_
.GetAckAlarm()->Fire();
3449 // Check that ack is sent and that delayed ack alarm is reset.
3450 EXPECT_EQ(2u, writer_
->frame_count());
3451 EXPECT_FALSE(writer_
->stop_waiting_frames().empty());
3452 EXPECT_FALSE(writer_
->ack_frames().empty());
3453 EXPECT_FALSE(connection_
.GetAckAlarm()->IsSet());
3456 TEST_P(QuicConnectionTest
, SendDelayedAckOnHandshakeConfirmed
) {
3457 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
3459 // Check that ack is sent and that delayed ack alarm is set.
3460 EXPECT_TRUE(connection_
.GetAckAlarm()->IsSet());
3461 QuicTime ack_time
= clock_
.ApproximateNow().Add(DefaultDelayedAckTime());
3462 EXPECT_EQ(ack_time
, connection_
.GetAckAlarm()->deadline());
3464 // Completing the handshake as the server does nothing.
3465 QuicConnectionPeer::SetPerspective(&connection_
, Perspective::IS_SERVER
);
3466 connection_
.OnHandshakeComplete();
3467 EXPECT_TRUE(connection_
.GetAckAlarm()->IsSet());
3468 EXPECT_EQ(ack_time
, connection_
.GetAckAlarm()->deadline());
3470 // Complete the handshake as the client decreases the delayed ack time to 0ms.
3471 QuicConnectionPeer::SetPerspective(&connection_
, Perspective::IS_CLIENT
);
3472 connection_
.OnHandshakeComplete();
3473 EXPECT_TRUE(connection_
.GetAckAlarm()->IsSet());
3474 EXPECT_EQ(clock_
.ApproximateNow(), connection_
.GetAckAlarm()->deadline());
3477 TEST_P(QuicConnectionTest
, SendDelayedAckOnSecondPacket
) {
3478 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
3481 // Check that ack is sent and that delayed ack alarm is reset.
3482 EXPECT_EQ(2u, writer_
->frame_count());
3483 EXPECT_FALSE(writer_
->stop_waiting_frames().empty());
3484 EXPECT_FALSE(writer_
->ack_frames().empty());
3485 EXPECT_FALSE(connection_
.GetAckAlarm()->IsSet());
3488 TEST_P(QuicConnectionTest
, NoAckOnOldNacks
) {
3489 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
3490 // Drop one packet, triggering a sequence of acks.
3492 size_t frames_per_ack
= 2;
3493 EXPECT_EQ(frames_per_ack
, writer_
->frame_count());
3494 EXPECT_FALSE(writer_
->ack_frames().empty());
3497 EXPECT_EQ(frames_per_ack
, writer_
->frame_count());
3498 EXPECT_FALSE(writer_
->ack_frames().empty());
3501 EXPECT_EQ(frames_per_ack
, writer_
->frame_count());
3502 EXPECT_FALSE(writer_
->ack_frames().empty());
3505 EXPECT_EQ(frames_per_ack
, writer_
->frame_count());
3506 EXPECT_FALSE(writer_
->ack_frames().empty());
3508 // Now only set the timer on the 6th packet, instead of sending another ack.
3510 EXPECT_EQ(0u, writer_
->frame_count());
3511 EXPECT_TRUE(connection_
.GetAckAlarm()->IsSet());
3514 TEST_P(QuicConnectionTest
, SendDelayedAckOnOutgoingPacket
) {
3515 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
3517 connection_
.SendStreamDataWithString(kClientDataStreamId1
, "foo", 0, !kFin
,
3519 // Check that ack is bundled with outgoing data and that delayed ack
3521 EXPECT_EQ(3u, writer_
->frame_count());
3522 EXPECT_FALSE(writer_
->stop_waiting_frames().empty());
3523 EXPECT_FALSE(writer_
->ack_frames().empty());
3524 EXPECT_FALSE(connection_
.GetAckAlarm()->IsSet());
3527 TEST_P(QuicConnectionTest
, SendDelayedAckOnOutgoingCryptoPacket
) {
3528 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
3530 connection_
.SendStreamDataWithString(kCryptoStreamId
, "foo", 0, !kFin
,
3532 // Check that ack is bundled with outgoing crypto data.
3533 EXPECT_EQ(3u, writer_
->frame_count());
3534 EXPECT_FALSE(writer_
->ack_frames().empty());
3535 EXPECT_FALSE(connection_
.GetAckAlarm()->IsSet());
3538 TEST_P(QuicConnectionTest
, BlockAndBufferOnFirstCHLOPacketOfTwo
) {
3539 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
3542 writer_
->set_is_write_blocked_data_buffered(true);
3543 connection_
.SendStreamDataWithString(kCryptoStreamId
, "foo", 0, !kFin
,
3545 EXPECT_TRUE(writer_
->IsWriteBlocked());
3546 EXPECT_FALSE(connection_
.HasQueuedData());
3547 connection_
.SendStreamDataWithString(kCryptoStreamId
, "bar", 3, !kFin
,
3549 EXPECT_TRUE(writer_
->IsWriteBlocked());
3550 EXPECT_TRUE(connection_
.HasQueuedData());
3553 TEST_P(QuicConnectionTest
, BundleAckForSecondCHLO
) {
3554 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
3555 EXPECT_FALSE(connection_
.GetAckAlarm()->IsSet());
3556 EXPECT_CALL(visitor_
, OnCanWrite()).WillOnce(
3557 IgnoreResult(InvokeWithoutArgs(&connection_
,
3558 &TestConnection::SendCryptoStreamData
)));
3559 // Process a packet from the crypto stream, which is frame1_'s default.
3560 // Receiving the CHLO as packet 2 first will cause the connection to
3561 // immediately send an ack, due to the packet gap.
3563 // Check that ack is sent and that delayed ack alarm is reset.
3564 EXPECT_EQ(3u, writer_
->frame_count());
3565 EXPECT_FALSE(writer_
->stop_waiting_frames().empty());
3566 EXPECT_EQ(1u, writer_
->stream_frames().size());
3567 EXPECT_FALSE(writer_
->ack_frames().empty());
3568 EXPECT_FALSE(connection_
.GetAckAlarm()->IsSet());
3571 TEST_P(QuicConnectionTest
, BundleAckWithDataOnIncomingAck
) {
3572 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
3573 connection_
.SendStreamDataWithString(kClientDataStreamId1
, "foo", 0, !kFin
,
3575 connection_
.SendStreamDataWithString(kClientDataStreamId1
, "foo", 3, !kFin
,
3577 // Ack the second packet, which will retransmit the first packet.
3578 QuicAckFrame ack
= InitAckFrame(2);
3579 NackPacket(1, &ack
);
3580 SequenceNumberSet lost_packets
;
3581 lost_packets
.insert(1);
3582 EXPECT_CALL(*loss_algorithm_
, DetectLostPackets(_
, _
, _
, _
))
3583 .WillOnce(Return(lost_packets
));
3584 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
3585 ProcessAckPacket(&ack
);
3586 EXPECT_EQ(1u, writer_
->frame_count());
3587 EXPECT_EQ(1u, writer_
->stream_frames().size());
3590 // Now ack the retransmission, which will both raise the high water mark
3591 // and see if there is more data to send.
3592 ack
= InitAckFrame(3);
3593 NackPacket(1, &ack
);
3594 EXPECT_CALL(*loss_algorithm_
, DetectLostPackets(_
, _
, _
, _
))
3595 .WillOnce(Return(SequenceNumberSet()));
3596 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
3597 ProcessAckPacket(&ack
);
3599 // Check that no packet is sent and the ack alarm isn't set.
3600 EXPECT_EQ(0u, writer_
->frame_count());
3601 EXPECT_FALSE(connection_
.GetAckAlarm()->IsSet());
3604 // Send the same ack, but send both data and an ack together.
3605 ack
= InitAckFrame(3);
3606 NackPacket(1, &ack
);
3607 EXPECT_CALL(*loss_algorithm_
, DetectLostPackets(_
, _
, _
, _
))
3608 .WillOnce(Return(SequenceNumberSet()));
3609 EXPECT_CALL(visitor_
, OnCanWrite()).WillOnce(
3610 IgnoreResult(InvokeWithoutArgs(
3612 &TestConnection::EnsureWritableAndSendStreamData5
)));
3613 ProcessAckPacket(&ack
);
3615 // Check that ack is bundled with outgoing data and the delayed ack
3617 EXPECT_EQ(3u, writer_
->frame_count());
3618 EXPECT_FALSE(writer_
->stop_waiting_frames().empty());
3619 EXPECT_FALSE(writer_
->ack_frames().empty());
3620 EXPECT_EQ(1u, writer_
->stream_frames().size());
3621 EXPECT_FALSE(connection_
.GetAckAlarm()->IsSet());
3624 TEST_P(QuicConnectionTest
, NoAckSentForClose
) {
3625 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
3627 EXPECT_CALL(visitor_
, OnConnectionClosed(QUIC_PEER_GOING_AWAY
, true));
3628 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
)).Times(0);
3629 ProcessClosePacket(2, 0);
3632 TEST_P(QuicConnectionTest
, SendWhenDisconnected
) {
3633 EXPECT_TRUE(connection_
.connected());
3634 EXPECT_CALL(visitor_
, OnConnectionClosed(QUIC_PEER_GOING_AWAY
, false));
3635 connection_
.CloseConnection(QUIC_PEER_GOING_AWAY
, false);
3636 EXPECT_FALSE(connection_
.connected());
3637 EXPECT_FALSE(connection_
.CanWriteStreamData());
3638 QuicPacket
* packet
= ConstructDataPacket(1, 0, !kEntropyFlag
);
3639 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, 1, _
, _
)).Times(0);
3640 connection_
.SendPacket(
3641 ENCRYPTION_NONE
, 1, packet
, kTestEntropyHash
, HAS_RETRANSMITTABLE_DATA
);
3644 TEST_P(QuicConnectionTest
, PublicReset
) {
3645 QuicPublicResetPacket header
;
3646 header
.public_header
.connection_id
= connection_id_
;
3647 header
.public_header
.reset_flag
= true;
3648 header
.public_header
.version_flag
= false;
3649 header
.rejected_sequence_number
= 10101;
3650 scoped_ptr
<QuicEncryptedPacket
> packet(
3651 framer_
.BuildPublicResetPacket(header
));
3652 EXPECT_CALL(visitor_
, OnConnectionClosed(QUIC_PUBLIC_RESET
, true));
3653 connection_
.ProcessUdpPacket(IPEndPoint(), IPEndPoint(), *packet
);
3656 TEST_P(QuicConnectionTest
, GoAway
) {
3657 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
3659 QuicGoAwayFrame goaway
;
3660 goaway
.last_good_stream_id
= 1;
3661 goaway
.error_code
= QUIC_PEER_GOING_AWAY
;
3662 goaway
.reason_phrase
= "Going away.";
3663 EXPECT_CALL(visitor_
, OnGoAway(_
));
3664 ProcessGoAwayPacket(&goaway
);
3667 TEST_P(QuicConnectionTest
, WindowUpdate
) {
3668 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
3670 QuicWindowUpdateFrame window_update
;
3671 window_update
.stream_id
= 3;
3672 window_update
.byte_offset
= 1234;
3673 EXPECT_CALL(visitor_
, OnWindowUpdateFrames(_
));
3674 ProcessFramePacket(QuicFrame(&window_update
));
3677 TEST_P(QuicConnectionTest
, Blocked
) {
3678 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
3680 QuicBlockedFrame blocked
;
3681 blocked
.stream_id
= 3;
3682 EXPECT_CALL(visitor_
, OnBlockedFrames(_
));
3683 ProcessFramePacket(QuicFrame(&blocked
));
3686 TEST_P(QuicConnectionTest
, ZeroBytePacket
) {
3687 // Don't close the connection for zero byte packets.
3688 EXPECT_CALL(visitor_
, OnConnectionClosed(_
, _
)).Times(0);
3689 QuicEncryptedPacket
encrypted(nullptr, 0);
3690 connection_
.ProcessUdpPacket(IPEndPoint(), IPEndPoint(), encrypted
);
3693 TEST_P(QuicConnectionTest
, MissingPacketsBeforeLeastUnacked
) {
3694 // Set the sequence number of the ack packet to be least unacked (4).
3695 QuicPacketCreatorPeer::SetSequenceNumber(&peer_creator_
, 3);
3696 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
3697 QuicStopWaitingFrame frame
= InitStopWaitingFrame(4);
3698 ProcessStopWaitingPacket(&frame
);
3699 EXPECT_TRUE(outgoing_ack()->missing_packets
.empty());
3702 TEST_P(QuicConnectionTest
, ReceivedEntropyHashCalculation
) {
3703 EXPECT_CALL(visitor_
, OnStreamFrames(_
)).Times(AtLeast(1));
3704 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
3705 ProcessDataPacket(1, 1, kEntropyFlag
);
3706 ProcessDataPacket(4, 1, kEntropyFlag
);
3707 ProcessDataPacket(3, 1, !kEntropyFlag
);
3708 ProcessDataPacket(7, 1, kEntropyFlag
);
3709 EXPECT_EQ(146u, outgoing_ack()->entropy_hash
);
3712 TEST_P(QuicConnectionTest
, ReceivedEntropyHashCalculationHalfFEC
) {
3713 // FEC packets should not change the entropy hash calculation.
3714 EXPECT_CALL(visitor_
, OnStreamFrames(_
)).Times(AtLeast(1));
3715 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
3716 ProcessDataPacket(1, 1, kEntropyFlag
);
3717 ProcessFecPacket(4, 1, false, kEntropyFlag
, nullptr);
3718 ProcessDataPacket(3, 3, !kEntropyFlag
);
3719 ProcessFecPacket(7, 3, false, kEntropyFlag
, nullptr);
3720 EXPECT_EQ(146u, outgoing_ack()->entropy_hash
);
3723 TEST_P(QuicConnectionTest
, UpdateEntropyForReceivedPackets
) {
3724 EXPECT_CALL(visitor_
, OnStreamFrames(_
)).Times(AtLeast(1));
3725 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
3726 ProcessDataPacket(1, 1, kEntropyFlag
);
3727 ProcessDataPacket(5, 1, kEntropyFlag
);
3728 ProcessDataPacket(4, 1, !kEntropyFlag
);
3729 EXPECT_EQ(34u, outgoing_ack()->entropy_hash
);
3730 // Make 4th packet my least unacked, and update entropy for 2, 3 packets.
3731 QuicPacketCreatorPeer::SetSequenceNumber(&peer_creator_
, 5);
3732 QuicPacketEntropyHash six_packet_entropy_hash
= 0;
3733 QuicPacketEntropyHash random_entropy_hash
= 129u;
3734 QuicStopWaitingFrame frame
= InitStopWaitingFrame(4);
3735 frame
.entropy_hash
= random_entropy_hash
;
3736 if (ProcessStopWaitingPacket(&frame
)) {
3737 six_packet_entropy_hash
= 1 << 6;
3740 EXPECT_EQ((random_entropy_hash
+ (1 << 5) + six_packet_entropy_hash
),
3741 outgoing_ack()->entropy_hash
);
3744 TEST_P(QuicConnectionTest
, UpdateEntropyHashUptoCurrentPacket
) {
3745 EXPECT_CALL(visitor_
, OnStreamFrames(_
)).Times(AtLeast(1));
3746 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
3747 ProcessDataPacket(1, 1, kEntropyFlag
);
3748 ProcessDataPacket(5, 1, !kEntropyFlag
);
3749 ProcessDataPacket(22, 1, kEntropyFlag
);
3750 EXPECT_EQ(66u, outgoing_ack()->entropy_hash
);
3751 QuicPacketCreatorPeer::SetSequenceNumber(&peer_creator_
, 22);
3752 QuicPacketEntropyHash random_entropy_hash
= 85u;
3753 // Current packet is the least unacked packet.
3754 QuicPacketEntropyHash ack_entropy_hash
;
3755 QuicStopWaitingFrame frame
= InitStopWaitingFrame(23);
3756 frame
.entropy_hash
= random_entropy_hash
;
3757 ack_entropy_hash
= ProcessStopWaitingPacket(&frame
);
3758 EXPECT_EQ((random_entropy_hash
+ ack_entropy_hash
),
3759 outgoing_ack()->entropy_hash
);
3760 ProcessDataPacket(25, 1, kEntropyFlag
);
3761 EXPECT_EQ((random_entropy_hash
+ ack_entropy_hash
+ (1 << (25 % 8))),
3762 outgoing_ack()->entropy_hash
);
3765 TEST_P(QuicConnectionTest
, EntropyCalculationForTruncatedAck
) {
3766 EXPECT_CALL(visitor_
, OnStreamFrames(_
)).Times(AtLeast(1));
3767 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
3768 QuicPacketEntropyHash entropy
[51];
3770 for (int i
= 1; i
< 51; ++i
) {
3771 bool should_send
= i
% 10 != 1;
3772 bool entropy_flag
= (i
& (i
- 1)) != 0;
3774 entropy
[i
] = entropy
[i
- 1];
3778 entropy
[i
] = entropy
[i
- 1] ^ (1 << (i
% 8));
3780 entropy
[i
] = entropy
[i
- 1];
3782 ProcessDataPacket(i
, 1, entropy_flag
);
3784 for (int i
= 1; i
< 50; ++i
) {
3785 EXPECT_EQ(entropy
[i
], QuicConnectionPeer::ReceivedEntropyHash(
3790 TEST_P(QuicConnectionTest
, ServerSendsVersionNegotiationPacket
) {
3791 connection_
.SetSupportedVersions(QuicSupportedVersions());
3792 framer_
.set_version_for_tests(QUIC_VERSION_UNSUPPORTED
);
3794 QuicPacketHeader header
;
3795 header
.public_header
.connection_id
= connection_id_
;
3796 header
.public_header
.version_flag
= true;
3797 header
.packet_sequence_number
= 12;
3800 frames
.push_back(QuicFrame(&frame1_
));
3801 scoped_ptr
<QuicPacket
> packet(ConstructPacket(header
, frames
));
3802 char buffer
[kMaxPacketSize
];
3803 scoped_ptr
<QuicEncryptedPacket
> encrypted(framer_
.EncryptPayload(
3804 ENCRYPTION_NONE
, 12, *packet
, buffer
, kMaxPacketSize
));
3806 framer_
.set_version(version());
3807 connection_
.set_perspective(Perspective::IS_SERVER
);
3808 connection_
.ProcessUdpPacket(IPEndPoint(), IPEndPoint(), *encrypted
);
3809 EXPECT_TRUE(writer_
->version_negotiation_packet() != nullptr);
3811 size_t num_versions
= arraysize(kSupportedQuicVersions
);
3812 ASSERT_EQ(num_versions
,
3813 writer_
->version_negotiation_packet()->versions
.size());
3815 // We expect all versions in kSupportedQuicVersions to be
3816 // included in the packet.
3817 for (size_t i
= 0; i
< num_versions
; ++i
) {
3818 EXPECT_EQ(kSupportedQuicVersions
[i
],
3819 writer_
->version_negotiation_packet()->versions
[i
]);
3823 TEST_P(QuicConnectionTest
, ServerSendsVersionNegotiationPacketSocketBlocked
) {
3824 connection_
.SetSupportedVersions(QuicSupportedVersions());
3825 framer_
.set_version_for_tests(QUIC_VERSION_UNSUPPORTED
);
3827 QuicPacketHeader header
;
3828 header
.public_header
.connection_id
= connection_id_
;
3829 header
.public_header
.version_flag
= true;
3830 header
.packet_sequence_number
= 12;
3833 frames
.push_back(QuicFrame(&frame1_
));
3834 scoped_ptr
<QuicPacket
> packet(ConstructPacket(header
, frames
));
3835 char buffer
[kMaxPacketSize
];
3836 scoped_ptr
<QuicEncryptedPacket
> encrypted(framer_
.EncryptPayload(
3837 ENCRYPTION_NONE
, 12, *packet
, buffer
, kMaxPacketSize
));
3839 framer_
.set_version(version());
3840 connection_
.set_perspective(Perspective::IS_SERVER
);
3842 connection_
.ProcessUdpPacket(IPEndPoint(), IPEndPoint(), *encrypted
);
3843 EXPECT_EQ(0u, writer_
->last_packet_size());
3844 EXPECT_TRUE(connection_
.HasQueuedData());
3846 writer_
->SetWritable();
3847 connection_
.OnCanWrite();
3848 EXPECT_TRUE(writer_
->version_negotiation_packet() != nullptr);
3850 size_t num_versions
= arraysize(kSupportedQuicVersions
);
3851 ASSERT_EQ(num_versions
,
3852 writer_
->version_negotiation_packet()->versions
.size());
3854 // We expect all versions in kSupportedQuicVersions to be
3855 // included in the packet.
3856 for (size_t i
= 0; i
< num_versions
; ++i
) {
3857 EXPECT_EQ(kSupportedQuicVersions
[i
],
3858 writer_
->version_negotiation_packet()->versions
[i
]);
3862 TEST_P(QuicConnectionTest
,
3863 ServerSendsVersionNegotiationPacketSocketBlockedDataBuffered
) {
3864 connection_
.SetSupportedVersions(QuicSupportedVersions());
3865 framer_
.set_version_for_tests(QUIC_VERSION_UNSUPPORTED
);
3867 QuicPacketHeader header
;
3868 header
.public_header
.connection_id
= connection_id_
;
3869 header
.public_header
.version_flag
= true;
3870 header
.packet_sequence_number
= 12;
3873 frames
.push_back(QuicFrame(&frame1_
));
3874 scoped_ptr
<QuicPacket
> packet(ConstructPacket(header
, frames
));
3875 char buffer
[kMaxPacketSize
];
3876 scoped_ptr
<QuicEncryptedPacket
> encrypted(framer_
.EncryptPayload(
3877 ENCRYPTION_NONE
, 12, *packet
, buffer
, kMaxPacketSize
));
3879 framer_
.set_version(version());
3880 connection_
.set_perspective(Perspective::IS_SERVER
);
3882 writer_
->set_is_write_blocked_data_buffered(true);
3883 connection_
.ProcessUdpPacket(IPEndPoint(), IPEndPoint(), *encrypted
);
3884 EXPECT_EQ(0u, writer_
->last_packet_size());
3885 EXPECT_FALSE(connection_
.HasQueuedData());
3888 TEST_P(QuicConnectionTest
, ClientHandlesVersionNegotiation
) {
3889 // Start out with some unsupported version.
3890 QuicConnectionPeer::GetFramer(&connection_
)->set_version_for_tests(
3891 QUIC_VERSION_UNSUPPORTED
);
3893 QuicPacketHeader header
;
3894 header
.public_header
.connection_id
= connection_id_
;
3895 header
.public_header
.version_flag
= true;
3896 header
.packet_sequence_number
= 12;
3898 QuicVersionVector supported_versions
;
3899 for (size_t i
= 0; i
< arraysize(kSupportedQuicVersions
); ++i
) {
3900 supported_versions
.push_back(kSupportedQuicVersions
[i
]);
3903 // Send a version negotiation packet.
3904 scoped_ptr
<QuicEncryptedPacket
> encrypted(
3905 framer_
.BuildVersionNegotiationPacket(
3906 header
.public_header
, supported_versions
));
3907 connection_
.ProcessUdpPacket(IPEndPoint(), IPEndPoint(), *encrypted
);
3909 // Now force another packet. The connection should transition into
3910 // NEGOTIATED_VERSION state and tell the packet creator to StopSendingVersion.
3911 header
.public_header
.version_flag
= false;
3913 frames
.push_back(QuicFrame(&frame1_
));
3914 scoped_ptr
<QuicPacket
> packet(ConstructPacket(header
, frames
));
3915 char buffer
[kMaxPacketSize
];
3916 encrypted
.reset(framer_
.EncryptPayload(ENCRYPTION_NONE
, 12, *packet
, buffer
,
3918 EXPECT_CALL(visitor_
, OnStreamFrames(_
)).Times(1);
3919 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
3920 connection_
.ProcessUdpPacket(IPEndPoint(), IPEndPoint(), *encrypted
);
3922 ASSERT_FALSE(QuicPacketCreatorPeer::SendVersionInPacket(creator_
));
3925 TEST_P(QuicConnectionTest
, BadVersionNegotiation
) {
3926 QuicPacketHeader header
;
3927 header
.public_header
.connection_id
= connection_id_
;
3928 header
.public_header
.version_flag
= true;
3929 header
.packet_sequence_number
= 12;
3931 QuicVersionVector supported_versions
;
3932 for (size_t i
= 0; i
< arraysize(kSupportedQuicVersions
); ++i
) {
3933 supported_versions
.push_back(kSupportedQuicVersions
[i
]);
3936 // Send a version negotiation packet with the version the client started with.
3937 // It should be rejected.
3938 EXPECT_CALL(visitor_
,
3939 OnConnectionClosed(QUIC_INVALID_VERSION_NEGOTIATION_PACKET
,
3941 scoped_ptr
<QuicEncryptedPacket
> encrypted(
3942 framer_
.BuildVersionNegotiationPacket(
3943 header
.public_header
, supported_versions
));
3944 connection_
.ProcessUdpPacket(IPEndPoint(), IPEndPoint(), *encrypted
);
3947 TEST_P(QuicConnectionTest
, CheckSendStats
) {
3948 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
));
3949 connection_
.SendStreamDataWithString(3, "first", 0, !kFin
, nullptr);
3950 size_t first_packet_size
= writer_
->last_packet_size();
3952 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
));
3953 connection_
.SendStreamDataWithString(5, "second", 0, !kFin
, nullptr);
3954 size_t second_packet_size
= writer_
->last_packet_size();
3956 // 2 retransmissions due to rto, 1 due to explicit nack.
3957 EXPECT_CALL(*send_algorithm_
, OnRetransmissionTimeout(true));
3958 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
)).Times(3);
3960 // Retransmit due to RTO.
3961 clock_
.AdvanceTime(QuicTime::Delta::FromSeconds(10));
3962 connection_
.GetRetransmissionAlarm()->Fire();
3964 // Retransmit due to explicit nacks.
3965 QuicAckFrame nack_three
= InitAckFrame(4);
3966 NackPacket(3, &nack_three
);
3967 NackPacket(1, &nack_three
);
3968 SequenceNumberSet lost_packets
;
3969 lost_packets
.insert(1);
3970 lost_packets
.insert(3);
3971 EXPECT_CALL(*loss_algorithm_
, DetectLostPackets(_
, _
, _
, _
))
3972 .WillOnce(Return(lost_packets
));
3973 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
3974 EXPECT_CALL(visitor_
, OnCanWrite()).Times(2);
3975 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
3976 ProcessAckPacket(&nack_three
);
3978 EXPECT_CALL(*send_algorithm_
, BandwidthEstimate()).WillOnce(
3979 Return(QuicBandwidth::Zero()));
3981 const QuicConnectionStats
& stats
= connection_
.GetStats();
3982 EXPECT_EQ(3 * first_packet_size
+ 2 * second_packet_size
- kQuicVersionSize
,
3984 EXPECT_EQ(5u, stats
.packets_sent
);
3985 EXPECT_EQ(2 * first_packet_size
+ second_packet_size
- kQuicVersionSize
,
3986 stats
.bytes_retransmitted
);
3987 EXPECT_EQ(3u, stats
.packets_retransmitted
);
3988 EXPECT_EQ(1u, stats
.rto_count
);
3989 EXPECT_EQ(kDefaultMaxPacketSize
, stats
.max_packet_size
);
3992 TEST_P(QuicConnectionTest
, CheckReceiveStats
) {
3993 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
3995 size_t received_bytes
= 0;
3996 received_bytes
+= ProcessFecProtectedPacket(1, false, !kEntropyFlag
);
3997 received_bytes
+= ProcessFecProtectedPacket(3, false, !kEntropyFlag
);
3998 // Should be counted against dropped packets.
3999 received_bytes
+= ProcessDataPacket(3, 1, !kEntropyFlag
);
4000 received_bytes
+= ProcessFecPacket(4, 1, true, !kEntropyFlag
, nullptr);
4002 EXPECT_CALL(*send_algorithm_
, BandwidthEstimate()).WillOnce(
4003 Return(QuicBandwidth::Zero()));
4005 const QuicConnectionStats
& stats
= connection_
.GetStats();
4006 EXPECT_EQ(received_bytes
, stats
.bytes_received
);
4007 EXPECT_EQ(4u, stats
.packets_received
);
4009 EXPECT_EQ(1u, stats
.packets_revived
);
4010 EXPECT_EQ(1u, stats
.packets_dropped
);
4013 TEST_P(QuicConnectionTest
, TestFecGroupLimits
) {
4014 // Create and return a group for 1.
4015 ASSERT_TRUE(QuicConnectionPeer::GetFecGroup(&connection_
, 1) != nullptr);
4017 // Create and return a group for 2.
4018 ASSERT_TRUE(QuicConnectionPeer::GetFecGroup(&connection_
, 2) != nullptr);
4020 // Create and return a group for 4. This should remove 1 but not 2.
4021 ASSERT_TRUE(QuicConnectionPeer::GetFecGroup(&connection_
, 4) != nullptr);
4022 ASSERT_TRUE(QuicConnectionPeer::GetFecGroup(&connection_
, 1) == nullptr);
4023 ASSERT_TRUE(QuicConnectionPeer::GetFecGroup(&connection_
, 2) != nullptr);
4025 // Create and return a group for 3. This will kill off 2.
4026 ASSERT_TRUE(QuicConnectionPeer::GetFecGroup(&connection_
, 3) != nullptr);
4027 ASSERT_TRUE(QuicConnectionPeer::GetFecGroup(&connection_
, 2) == nullptr);
4029 // Verify that adding 5 kills off 3, despite 4 being created before 3.
4030 ASSERT_TRUE(QuicConnectionPeer::GetFecGroup(&connection_
, 5) != nullptr);
4031 ASSERT_TRUE(QuicConnectionPeer::GetFecGroup(&connection_
, 4) != nullptr);
4032 ASSERT_TRUE(QuicConnectionPeer::GetFecGroup(&connection_
, 3) == nullptr);
4035 TEST_P(QuicConnectionTest
, ProcessFramesIfPacketClosedConnection
) {
4036 // Construct a packet with stream frame and connection close frame.
4037 QuicPacketHeader header
;
4038 header
.public_header
.connection_id
= connection_id_
;
4039 header
.packet_sequence_number
= 1;
4040 header
.public_header
.version_flag
= false;
4042 QuicConnectionCloseFrame qccf
;
4043 qccf
.error_code
= QUIC_PEER_GOING_AWAY
;
4046 frames
.push_back(QuicFrame(&frame1_
));
4047 frames
.push_back(QuicFrame(&qccf
));
4048 scoped_ptr
<QuicPacket
> packet(ConstructPacket(header
, frames
));
4049 EXPECT_TRUE(nullptr != packet
.get());
4050 char buffer
[kMaxPacketSize
];
4051 scoped_ptr
<QuicEncryptedPacket
> encrypted(framer_
.EncryptPayload(
4052 ENCRYPTION_NONE
, 1, *packet
, buffer
, kMaxPacketSize
));
4054 EXPECT_CALL(visitor_
, OnConnectionClosed(QUIC_PEER_GOING_AWAY
, true));
4055 EXPECT_CALL(visitor_
, OnStreamFrames(_
)).Times(1);
4056 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
4058 connection_
.ProcessUdpPacket(IPEndPoint(), IPEndPoint(), *encrypted
);
4061 TEST_P(QuicConnectionTest
, SelectMutualVersion
) {
4062 connection_
.SetSupportedVersions(QuicSupportedVersions());
4063 // Set the connection to speak the lowest quic version.
4064 connection_
.set_version(QuicVersionMin());
4065 EXPECT_EQ(QuicVersionMin(), connection_
.version());
4067 // Pass in available versions which includes a higher mutually supported
4068 // version. The higher mutually supported version should be selected.
4069 QuicVersionVector supported_versions
;
4070 for (size_t i
= 0; i
< arraysize(kSupportedQuicVersions
); ++i
) {
4071 supported_versions
.push_back(kSupportedQuicVersions
[i
]);
4073 EXPECT_TRUE(connection_
.SelectMutualVersion(supported_versions
));
4074 EXPECT_EQ(QuicVersionMax(), connection_
.version());
4076 // Expect that the lowest version is selected.
4077 // Ensure the lowest supported version is less than the max, unless they're
4079 EXPECT_LE(QuicVersionMin(), QuicVersionMax());
4080 QuicVersionVector lowest_version_vector
;
4081 lowest_version_vector
.push_back(QuicVersionMin());
4082 EXPECT_TRUE(connection_
.SelectMutualVersion(lowest_version_vector
));
4083 EXPECT_EQ(QuicVersionMin(), connection_
.version());
4085 // Shouldn't be able to find a mutually supported version.
4086 QuicVersionVector unsupported_version
;
4087 unsupported_version
.push_back(QUIC_VERSION_UNSUPPORTED
);
4088 EXPECT_FALSE(connection_
.SelectMutualVersion(unsupported_version
));
4091 TEST_P(QuicConnectionTest
, ConnectionCloseWhenWritable
) {
4092 EXPECT_FALSE(writer_
->IsWriteBlocked());
4095 connection_
.SendStreamDataWithString(1, "foo", 0, !kFin
, nullptr);
4096 EXPECT_EQ(0u, connection_
.NumQueuedPackets());
4097 EXPECT_EQ(1u, writer_
->packets_write_attempts());
4099 TriggerConnectionClose();
4100 EXPECT_EQ(2u, writer_
->packets_write_attempts());
4103 TEST_P(QuicConnectionTest
, ConnectionCloseGettingWriteBlocked
) {
4105 TriggerConnectionClose();
4106 EXPECT_EQ(1u, writer_
->packets_write_attempts());
4107 EXPECT_TRUE(writer_
->IsWriteBlocked());
4110 TEST_P(QuicConnectionTest
, ConnectionCloseWhenWriteBlocked
) {
4112 connection_
.SendStreamDataWithString(1, "foo", 0, !kFin
, nullptr);
4113 EXPECT_EQ(1u, connection_
.NumQueuedPackets());
4114 EXPECT_EQ(1u, writer_
->packets_write_attempts());
4115 EXPECT_TRUE(writer_
->IsWriteBlocked());
4116 TriggerConnectionClose();
4117 EXPECT_EQ(1u, writer_
->packets_write_attempts());
4120 TEST_P(QuicConnectionTest
, AckNotifierTriggerCallback
) {
4121 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
4123 // Create a delegate which we expect to be called.
4124 scoped_refptr
<MockAckNotifierDelegate
> delegate(new MockAckNotifierDelegate
);
4125 EXPECT_CALL(*delegate
.get(), OnAckNotification(_
, _
, _
)).Times(1);
4127 // Send some data, which will register the delegate to be notified.
4128 connection_
.SendStreamDataWithString(1, "foo", 0, !kFin
, delegate
.get());
4130 // Process an ACK from the server which should trigger the callback.
4131 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
4132 QuicAckFrame frame
= InitAckFrame(1);
4133 ProcessAckPacket(&frame
);
4136 TEST_P(QuicConnectionTest
, AckNotifierFailToTriggerCallback
) {
4137 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
4139 // Create a delegate which we don't expect to be called.
4140 scoped_refptr
<MockAckNotifierDelegate
> delegate(new MockAckNotifierDelegate
);
4141 EXPECT_CALL(*delegate
.get(), OnAckNotification(_
, _
, _
)).Times(0);
4143 // Send some data, which will register the delegate to be notified. This will
4144 // not be ACKed and so the delegate should never be called.
4145 connection_
.SendStreamDataWithString(1, "foo", 0, !kFin
, delegate
.get());
4147 // Send some other data which we will ACK.
4148 connection_
.SendStreamDataWithString(1, "foo", 0, !kFin
, nullptr);
4149 connection_
.SendStreamDataWithString(1, "bar", 0, !kFin
, nullptr);
4151 // Now we receive ACK for packets 2 and 3, but importantly missing packet 1
4152 // which we registered to be notified about.
4153 QuicAckFrame frame
= InitAckFrame(3);
4154 NackPacket(1, &frame
);
4155 SequenceNumberSet lost_packets
;
4156 lost_packets
.insert(1);
4157 EXPECT_CALL(*loss_algorithm_
, DetectLostPackets(_
, _
, _
, _
))
4158 .WillOnce(Return(lost_packets
));
4159 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
4160 ProcessAckPacket(&frame
);
4163 TEST_P(QuicConnectionTest
, AckNotifierCallbackAfterRetransmission
) {
4164 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
4166 // Create a delegate which we expect to be called.
4167 scoped_refptr
<MockAckNotifierDelegate
> delegate(new MockAckNotifierDelegate
);
4168 EXPECT_CALL(*delegate
.get(), OnAckNotification(_
, _
, _
)).Times(1);
4170 // Send four packets, and register to be notified on ACK of packet 2.
4171 connection_
.SendStreamDataWithString(3, "foo", 0, !kFin
, nullptr);
4172 connection_
.SendStreamDataWithString(3, "bar", 0, !kFin
, delegate
.get());
4173 connection_
.SendStreamDataWithString(3, "baz", 0, !kFin
, nullptr);
4174 connection_
.SendStreamDataWithString(3, "qux", 0, !kFin
, nullptr);
4176 // Now we receive ACK for packets 1, 3, and 4 and lose 2.
4177 QuicAckFrame frame
= InitAckFrame(4);
4178 NackPacket(2, &frame
);
4179 SequenceNumberSet lost_packets
;
4180 lost_packets
.insert(2);
4181 EXPECT_CALL(*loss_algorithm_
, DetectLostPackets(_
, _
, _
, _
))
4182 .WillOnce(Return(lost_packets
));
4183 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
4184 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
));
4185 ProcessAckPacket(&frame
);
4187 // Now we get an ACK for packet 5 (retransmitted packet 2), which should
4188 // trigger the callback.
4189 EXPECT_CALL(*loss_algorithm_
, DetectLostPackets(_
, _
, _
, _
))
4190 .WillRepeatedly(Return(SequenceNumberSet()));
4191 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
4192 QuicAckFrame second_ack_frame
= InitAckFrame(5);
4193 ProcessAckPacket(&second_ack_frame
);
4196 // AckNotifierCallback is triggered by the ack of a packet that timed
4197 // out and was retransmitted, even though the retransmission has a
4198 // different sequence number.
4199 TEST_P(QuicConnectionTest
, AckNotifierCallbackForAckAfterRTO
) {
4202 // Create a delegate which we expect to be called.
4203 scoped_refptr
<MockAckNotifierDelegate
> delegate(
4204 new StrictMock
<MockAckNotifierDelegate
>);
4206 QuicTime default_retransmission_time
= clock_
.ApproximateNow().Add(
4207 DefaultRetransmissionTime());
4208 connection_
.SendStreamDataWithString(3, "foo", 0, !kFin
, delegate
.get());
4209 EXPECT_EQ(1u, stop_waiting()->least_unacked
);
4211 EXPECT_EQ(1u, writer_
->header().packet_sequence_number
);
4212 EXPECT_EQ(default_retransmission_time
,
4213 connection_
.GetRetransmissionAlarm()->deadline());
4214 // Simulate the retransmission alarm firing.
4215 clock_
.AdvanceTime(DefaultRetransmissionTime());
4216 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, 2u, _
, _
));
4217 connection_
.GetRetransmissionAlarm()->Fire();
4218 EXPECT_EQ(2u, writer_
->header().packet_sequence_number
);
4219 // We do not raise the high water mark yet.
4220 EXPECT_EQ(1u, stop_waiting()->least_unacked
);
4222 // Ack the original packet, which will revert the RTO.
4223 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
4224 EXPECT_CALL(*delegate
, OnAckNotification(1, _
, _
));
4225 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
4226 QuicAckFrame ack_frame
= InitAckFrame(1);
4227 ProcessAckPacket(&ack_frame
);
4229 // Delegate is not notified again when the retransmit is acked.
4230 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
4231 QuicAckFrame second_ack_frame
= InitAckFrame(2);
4232 ProcessAckPacket(&second_ack_frame
);
4235 // AckNotifierCallback is triggered by the ack of a packet that was
4236 // previously nacked, even though the retransmission has a different
4238 TEST_P(QuicConnectionTest
, AckNotifierCallbackForAckOfNackedPacket
) {
4241 // Create a delegate which we expect to be called.
4242 scoped_refptr
<MockAckNotifierDelegate
> delegate(
4243 new StrictMock
<MockAckNotifierDelegate
>);
4245 // Send four packets, and register to be notified on ACK of packet 2.
4246 connection_
.SendStreamDataWithString(3, "foo", 0, !kFin
, nullptr);
4247 connection_
.SendStreamDataWithString(3, "bar", 0, !kFin
, delegate
.get());
4248 connection_
.SendStreamDataWithString(3, "baz", 0, !kFin
, nullptr);
4249 connection_
.SendStreamDataWithString(3, "qux", 0, !kFin
, nullptr);
4251 // Now we receive ACK for packets 1, 3, and 4 and lose 2.
4252 QuicAckFrame frame
= InitAckFrame(4);
4253 NackPacket(2, &frame
);
4254 SequenceNumberSet lost_packets
;
4255 lost_packets
.insert(2);
4256 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
4257 EXPECT_CALL(*loss_algorithm_
, DetectLostPackets(_
, _
, _
, _
))
4258 .WillOnce(Return(lost_packets
));
4259 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
4260 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
));
4261 ProcessAckPacket(&frame
);
4263 // Now we get an ACK for packet 2, which was previously nacked.
4264 SequenceNumberSet no_lost_packets
;
4265 EXPECT_CALL(*delegate
.get(), OnAckNotification(1, _
, _
));
4266 EXPECT_CALL(*loss_algorithm_
, DetectLostPackets(_
, _
, _
, _
))
4267 .WillOnce(Return(no_lost_packets
));
4268 QuicAckFrame second_ack_frame
= InitAckFrame(4);
4269 ProcessAckPacket(&second_ack_frame
);
4271 // Verify that the delegate is not notified again when the
4272 // retransmit is acked.
4273 EXPECT_CALL(*loss_algorithm_
, DetectLostPackets(_
, _
, _
, _
))
4274 .WillOnce(Return(no_lost_packets
));
4275 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
4276 QuicAckFrame third_ack_frame
= InitAckFrame(5);
4277 ProcessAckPacket(&third_ack_frame
);
4280 TEST_P(QuicConnectionTest
, AckNotifierFECTriggerCallback
) {
4281 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
4283 // Create a delegate which we expect to be called.
4284 scoped_refptr
<MockAckNotifierDelegate
> delegate(
4285 new MockAckNotifierDelegate
);
4286 EXPECT_CALL(*delegate
.get(), OnAckNotification(_
, _
, _
)).Times(1);
4288 // Send some data, which will register the delegate to be notified.
4289 connection_
.SendStreamDataWithString(1, "foo", 0, !kFin
, delegate
.get());
4290 connection_
.SendStreamDataWithString(2, "bar", 0, !kFin
, nullptr);
4292 // Process an ACK from the server with a revived packet, which should trigger
4294 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
4295 QuicAckFrame frame
= InitAckFrame(2);
4296 NackPacket(1, &frame
);
4297 frame
.revived_packets
.insert(1);
4298 ProcessAckPacket(&frame
);
4299 // If the ack is processed again, the notifier should not be called again.
4300 ProcessAckPacket(&frame
);
4303 TEST_P(QuicConnectionTest
, AckNotifierCallbackAfterFECRecovery
) {
4304 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
4305 EXPECT_CALL(visitor_
, OnCanWrite());
4307 // Create a delegate which we expect to be called.
4308 scoped_refptr
<MockAckNotifierDelegate
> delegate(new MockAckNotifierDelegate
);
4309 EXPECT_CALL(*delegate
.get(), OnAckNotification(_
, _
, _
)).Times(1);
4311 // Expect ACKs for 1 packet.
4312 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
4314 // Send one packet, and register to be notified on ACK.
4315 connection_
.SendStreamDataWithString(1, "foo", 0, !kFin
, delegate
.get());
4317 // Ack packet gets dropped, but we receive an FEC packet that covers it.
4318 // Should recover the Ack packet and trigger the notification callback.
4321 QuicAckFrame ack_frame
= InitAckFrame(1);
4322 frames
.push_back(QuicFrame(&ack_frame
));
4324 // Dummy stream frame to satisfy expectations set elsewhere.
4325 frames
.push_back(QuicFrame(&frame1_
));
4327 QuicPacketHeader ack_header
;
4328 ack_header
.public_header
.connection_id
= connection_id_
;
4329 ack_header
.public_header
.reset_flag
= false;
4330 ack_header
.public_header
.version_flag
= false;
4331 ack_header
.entropy_flag
= !kEntropyFlag
;
4332 ack_header
.fec_flag
= true;
4333 ack_header
.packet_sequence_number
= 1;
4334 ack_header
.is_in_fec_group
= IN_FEC_GROUP
;
4335 ack_header
.fec_group
= 1;
4337 QuicPacket
* packet
= BuildUnsizedDataPacket(&framer_
, ack_header
, frames
);
4339 // Take the packet which contains the ACK frame, and construct and deliver an
4340 // FEC packet which allows the ACK packet to be recovered.
4341 ProcessFecPacket(2, 1, true, !kEntropyFlag
, packet
);
4344 TEST_P(QuicConnectionTest
, NetworkChangeVisitorCwndCallbackChangesFecState
) {
4345 size_t max_packets_per_fec_group
= creator_
->max_packets_per_fec_group();
4347 QuicSentPacketManager::NetworkChangeVisitor
* visitor
=
4348 QuicSentPacketManagerPeer::GetNetworkChangeVisitor(manager_
);
4349 EXPECT_TRUE(visitor
);
4351 // Increase FEC group size by increasing congestion window to a large number.
4352 EXPECT_CALL(*send_algorithm_
, GetCongestionWindow()).WillRepeatedly(
4353 Return(1000 * kDefaultTCPMSS
));
4354 visitor
->OnCongestionWindowChange();
4355 EXPECT_LT(max_packets_per_fec_group
, creator_
->max_packets_per_fec_group());
4358 TEST_P(QuicConnectionTest
, NetworkChangeVisitorConfigCallbackChangesFecState
) {
4359 QuicSentPacketManager::NetworkChangeVisitor
* visitor
=
4360 QuicSentPacketManagerPeer::GetNetworkChangeVisitor(manager_
);
4361 EXPECT_TRUE(visitor
);
4362 EXPECT_EQ(QuicTime::Delta::Zero(),
4363 QuicPacketGeneratorPeer::GetFecTimeout(generator_
));
4365 // Verify that sending a config with a new initial rtt changes fec timeout.
4366 // Create and process a config with a non-zero initial RTT.
4367 EXPECT_CALL(*send_algorithm_
, SetFromConfig(_
, _
));
4369 config
.SetInitialRoundTripTimeUsToSend(300000);
4370 connection_
.SetFromConfig(config
);
4371 EXPECT_LT(QuicTime::Delta::Zero(),
4372 QuicPacketGeneratorPeer::GetFecTimeout(generator_
));
4375 TEST_P(QuicConnectionTest
, NetworkChangeVisitorRttCallbackChangesFecState
) {
4376 // Verify that sending a config with a new initial rtt changes fec timeout.
4377 QuicSentPacketManager::NetworkChangeVisitor
* visitor
=
4378 QuicSentPacketManagerPeer::GetNetworkChangeVisitor(manager_
);
4379 EXPECT_TRUE(visitor
);
4380 EXPECT_EQ(QuicTime::Delta::Zero(),
4381 QuicPacketGeneratorPeer::GetFecTimeout(generator_
));
4383 // Increase FEC timeout by increasing RTT.
4384 RttStats
* rtt_stats
= QuicSentPacketManagerPeer::GetRttStats(manager_
);
4385 rtt_stats
->UpdateRtt(QuicTime::Delta::FromMilliseconds(300),
4386 QuicTime::Delta::Zero(), QuicTime::Zero());
4387 visitor
->OnRttChange();
4388 EXPECT_LT(QuicTime::Delta::Zero(),
4389 QuicPacketGeneratorPeer::GetFecTimeout(generator_
));
4392 TEST_P(QuicConnectionTest
, OnPacketHeaderDebugVisitor
) {
4393 QuicPacketHeader header
;
4395 scoped_ptr
<MockQuicConnectionDebugVisitor
> debug_visitor(
4396 new MockQuicConnectionDebugVisitor());
4397 connection_
.set_debug_visitor(debug_visitor
.get());
4398 EXPECT_CALL(*debug_visitor
, OnPacketHeader(Ref(header
))).Times(1);
4399 connection_
.OnPacketHeader(header
);
4402 TEST_P(QuicConnectionTest
, Pacing
) {
4403 TestConnection
server(connection_id_
, IPEndPoint(), helper_
.get(), factory_
,
4404 Perspective::IS_SERVER
, version());
4405 TestConnection
client(connection_id_
, IPEndPoint(), helper_
.get(), factory_
,
4406 Perspective::IS_CLIENT
, version());
4407 EXPECT_FALSE(client
.sent_packet_manager().using_pacing());
4408 EXPECT_FALSE(server
.sent_packet_manager().using_pacing());
4411 TEST_P(QuicConnectionTest
, ControlFramesInstigateAcks
) {
4412 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
4414 // Send a WINDOW_UPDATE frame.
4415 QuicWindowUpdateFrame window_update
;
4416 window_update
.stream_id
= 3;
4417 window_update
.byte_offset
= 1234;
4418 EXPECT_CALL(visitor_
, OnWindowUpdateFrames(_
));
4419 ProcessFramePacket(QuicFrame(&window_update
));
4421 // Ensure that this has caused the ACK alarm to be set.
4422 QuicAlarm
* ack_alarm
= QuicConnectionPeer::GetAckAlarm(&connection_
);
4423 EXPECT_TRUE(ack_alarm
->IsSet());
4425 // Cancel alarm, and try again with BLOCKED frame.
4426 ack_alarm
->Cancel();
4427 QuicBlockedFrame blocked
;
4428 blocked
.stream_id
= 3;
4429 EXPECT_CALL(visitor_
, OnBlockedFrames(_
));
4430 ProcessFramePacket(QuicFrame(&blocked
));
4431 EXPECT_TRUE(ack_alarm
->IsSet());
4434 TEST_P(QuicConnectionTest
, NoDataNoFin
) {
4435 // Make sure that a call to SendStreamWithData, with no data and no FIN, does
4436 // not result in a QuicAckNotifier being used-after-free (fail under ASAN).
4437 // Regression test for b/18594622
4438 scoped_refptr
<MockAckNotifierDelegate
> delegate(new MockAckNotifierDelegate
);
4440 connection_
.SendStreamDataWithString(3, "", 0, !kFin
, delegate
.get()),
4441 "Attempt to send empty stream frame");
4444 TEST_P(QuicConnectionTest
, FecSendPolicyReceivedConnectionOption
) {
4445 // Test sending SetReceivedConnectionOptions when FEC send policy is
4447 if (GetParam().fec_send_policy
== FEC_ALARM_TRIGGER
) {
4450 ValueRestore
<bool> old_flag(&FLAGS_quic_send_fec_packet_only_on_fec_alarm
,
4452 connection_
.set_perspective(Perspective::IS_SERVER
);
4454 // Test ReceivedConnectionOptions.
4455 EXPECT_CALL(*send_algorithm_
, SetFromConfig(_
, _
));
4458 copt
.push_back(kFSPA
);
4459 QuicConfigPeer::SetReceivedConnectionOptions(&config
, copt
);
4460 EXPECT_EQ(FEC_ANY_TRIGGER
, generator_
->fec_send_policy());
4461 connection_
.SetFromConfig(config
);
4462 EXPECT_EQ(FEC_ALARM_TRIGGER
, generator_
->fec_send_policy());