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"
7 #include "base/basictypes.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/stl_util.h"
11 #include "net/base/net_errors.h"
12 #include "net/quic/congestion_control/loss_detection_interface.h"
13 #include "net/quic/congestion_control/send_algorithm_interface.h"
14 #include "net/quic/crypto/null_encrypter.h"
15 #include "net/quic/crypto/quic_decrypter.h"
16 #include "net/quic/crypto/quic_encrypter.h"
17 #include "net/quic/quic_ack_notifier.h"
18 #include "net/quic/quic_flags.h"
19 #include "net/quic/quic_protocol.h"
20 #include "net/quic/quic_utils.h"
21 #include "net/quic/test_tools/mock_clock.h"
22 #include "net/quic/test_tools/mock_random.h"
23 #include "net/quic/test_tools/quic_config_peer.h"
24 #include "net/quic/test_tools/quic_connection_peer.h"
25 #include "net/quic/test_tools/quic_framer_peer.h"
26 #include "net/quic/test_tools/quic_packet_creator_peer.h"
27 #include "net/quic/test_tools/quic_packet_generator_peer.h"
28 #include "net/quic/test_tools/quic_sent_packet_manager_peer.h"
29 #include "net/quic/test_tools/quic_test_utils.h"
30 #include "net/quic/test_tools/simple_quic_framer.h"
31 #include "net/test/gtest_util.h"
32 #include "testing/gmock/include/gmock/gmock.h"
33 #include "testing/gtest/include/gtest/gtest.h"
35 using base::StringPiece
;
39 using testing::AnyNumber
;
40 using testing::AtLeast
;
41 using testing::ContainerEq
;
42 using testing::Contains
;
44 using testing::InSequence
;
45 using testing::InvokeWithoutArgs
;
46 using testing::NiceMock
;
48 using testing::Return
;
49 using testing::SaveArg
;
50 using testing::StrictMock
;
57 const char data1
[] = "foo";
58 const char data2
[] = "bar";
60 const bool kFin
= true;
61 const bool kEntropyFlag
= true;
63 const QuicPacketEntropyHash kTestEntropyHash
= 76;
65 const int kDefaultRetransmissionTimeMs
= 500;
67 // TaggingEncrypter appends kTagSize bytes of |tag| to the end of each message.
68 class TaggingEncrypter
: public QuicEncrypter
{
70 explicit TaggingEncrypter(uint8 tag
)
74 ~TaggingEncrypter() override
{}
76 // QuicEncrypter interface.
77 bool SetKey(StringPiece key
) override
{ return true; }
79 bool SetNoncePrefix(StringPiece nonce_prefix
) override
{ return true; }
81 bool Encrypt(StringPiece nonce
,
82 StringPiece associated_data
,
83 StringPiece plaintext
,
84 unsigned char* output
) override
{
85 memcpy(output
, plaintext
.data(), plaintext
.size());
86 output
+= plaintext
.size();
87 memset(output
, tag_
, kTagSize
);
91 bool EncryptPacket(QuicPacketSequenceNumber sequence_number
,
92 StringPiece associated_data
,
93 StringPiece plaintext
,
95 size_t* output_length
,
96 size_t max_output_length
) override
{
97 const size_t len
= plaintext
.size() + kTagSize
;
98 if (max_output_length
< len
) {
101 Encrypt(StringPiece(), associated_data
, plaintext
,
102 reinterpret_cast<unsigned char*>(output
));
103 *output_length
= len
;
107 size_t GetKeySize() const override
{ return 0; }
108 size_t GetNoncePrefixSize() const override
{ return 0; }
110 size_t GetMaxPlaintextSize(size_t ciphertext_size
) const override
{
111 return ciphertext_size
- kTagSize
;
114 size_t GetCiphertextSize(size_t plaintext_size
) const override
{
115 return plaintext_size
+ kTagSize
;
118 StringPiece
GetKey() const override
{ return StringPiece(); }
120 StringPiece
GetNoncePrefix() const override
{ return StringPiece(); }
129 DISALLOW_COPY_AND_ASSIGN(TaggingEncrypter
);
132 // TaggingDecrypter ensures that the final kTagSize bytes of the message all
133 // have the same value and then removes them.
134 class TaggingDecrypter
: public QuicDecrypter
{
136 ~TaggingDecrypter() override
{}
138 // QuicDecrypter interface
139 bool SetKey(StringPiece key
) override
{ return true; }
141 bool SetNoncePrefix(StringPiece nonce_prefix
) override
{ return true; }
143 bool DecryptPacket(QuicPacketSequenceNumber sequence_number
,
144 const StringPiece
& associated_data
,
145 const StringPiece
& ciphertext
,
147 size_t* output_length
,
148 size_t max_output_length
) override
{
149 if (ciphertext
.size() < kTagSize
) {
152 if (!CheckTag(ciphertext
, GetTag(ciphertext
))) {
155 *output_length
= ciphertext
.size() - kTagSize
;
156 memcpy(output
, ciphertext
.data(), *output_length
);
160 StringPiece
GetKey() const override
{ return StringPiece(); }
161 StringPiece
GetNoncePrefix() const override
{ return StringPiece(); }
164 virtual uint8
GetTag(StringPiece ciphertext
) {
165 return ciphertext
.data()[ciphertext
.size()-1];
173 bool CheckTag(StringPiece ciphertext
, uint8 tag
) {
174 for (size_t i
= ciphertext
.size() - kTagSize
; i
< ciphertext
.size(); i
++) {
175 if (ciphertext
.data()[i
] != tag
) {
184 // StringTaggingDecrypter ensures that the final kTagSize bytes of the message
185 // match the expected value.
186 class StrictTaggingDecrypter
: public TaggingDecrypter
{
188 explicit StrictTaggingDecrypter(uint8 tag
) : tag_(tag
) {}
189 ~StrictTaggingDecrypter() override
{}
191 // TaggingQuicDecrypter
192 uint8
GetTag(StringPiece ciphertext
) override
{ return tag_
; }
198 class TestConnectionHelper
: public QuicConnectionHelperInterface
{
200 class TestAlarm
: public QuicAlarm
{
202 explicit TestAlarm(QuicAlarm::Delegate
* delegate
)
203 : QuicAlarm(delegate
) {
206 void SetImpl() override
{}
207 void CancelImpl() override
{}
208 using QuicAlarm::Fire
;
211 TestConnectionHelper(MockClock
* clock
, MockRandom
* random_generator
)
213 random_generator_(random_generator
) {
214 clock_
->AdvanceTime(QuicTime::Delta::FromSeconds(1));
217 // QuicConnectionHelperInterface
218 const QuicClock
* GetClock() const override
{ return clock_
; }
220 QuicRandom
* GetRandomGenerator() override
{ return random_generator_
; }
222 QuicAlarm
* CreateAlarm(QuicAlarm::Delegate
* delegate
) override
{
223 return new TestAlarm(delegate
);
228 MockRandom
* random_generator_
;
230 DISALLOW_COPY_AND_ASSIGN(TestConnectionHelper
);
233 class TestPacketWriter
: public QuicPacketWriter
{
235 TestPacketWriter(QuicVersion version
, MockClock
*clock
)
237 framer_(SupportedVersions(version_
)),
238 last_packet_size_(0),
239 write_blocked_(false),
240 block_on_next_write_(false),
241 is_write_blocked_data_buffered_(false),
242 final_bytes_of_last_packet_(0),
243 final_bytes_of_previous_packet_(0),
244 use_tagging_decrypter_(false),
245 packets_write_attempts_(0),
247 write_pause_time_delta_(QuicTime::Delta::Zero()) {
250 // QuicPacketWriter interface
251 WriteResult
WritePacket(const char* buffer
,
253 const IPAddressNumber
& self_address
,
254 const IPEndPoint
& peer_address
) override
{
255 QuicEncryptedPacket
packet(buffer
, buf_len
);
256 ++packets_write_attempts_
;
258 if (packet
.length() >= sizeof(final_bytes_of_last_packet_
)) {
259 final_bytes_of_previous_packet_
= final_bytes_of_last_packet_
;
260 memcpy(&final_bytes_of_last_packet_
, packet
.data() + packet
.length() - 4,
261 sizeof(final_bytes_of_last_packet_
));
264 if (use_tagging_decrypter_
) {
265 framer_
.framer()->SetDecrypter(new TaggingDecrypter
, ENCRYPTION_NONE
);
267 EXPECT_TRUE(framer_
.ProcessPacket(packet
));
268 if (block_on_next_write_
) {
269 write_blocked_
= true;
270 block_on_next_write_
= false;
272 if (IsWriteBlocked()) {
273 return WriteResult(WRITE_STATUS_BLOCKED
, -1);
275 last_packet_size_
= packet
.length();
277 if (!write_pause_time_delta_
.IsZero()) {
278 clock_
->AdvanceTime(write_pause_time_delta_
);
280 return WriteResult(WRITE_STATUS_OK
, last_packet_size_
);
283 bool IsWriteBlockedDataBuffered() const override
{
284 return is_write_blocked_data_buffered_
;
287 bool IsWriteBlocked() const override
{ return write_blocked_
; }
289 void SetWritable() override
{ write_blocked_
= false; }
291 void BlockOnNextWrite() { block_on_next_write_
= true; }
293 // Sets the amount of time that the writer should before the actual write.
294 void SetWritePauseTimeDelta(QuicTime::Delta delta
) {
295 write_pause_time_delta_
= delta
;
298 const QuicPacketHeader
& header() { return framer_
.header(); }
300 size_t frame_count() const { return framer_
.num_frames(); }
302 const vector
<QuicAckFrame
>& ack_frames() const {
303 return framer_
.ack_frames();
306 const vector
<QuicStopWaitingFrame
>& stop_waiting_frames() const {
307 return framer_
.stop_waiting_frames();
310 const vector
<QuicConnectionCloseFrame
>& connection_close_frames() const {
311 return framer_
.connection_close_frames();
314 const vector
<QuicRstStreamFrame
>& rst_stream_frames() const {
315 return framer_
.rst_stream_frames();
318 const vector
<QuicStreamFrame
>& stream_frames() const {
319 return framer_
.stream_frames();
322 const vector
<QuicPingFrame
>& ping_frames() const {
323 return framer_
.ping_frames();
326 size_t last_packet_size() {
327 return last_packet_size_
;
330 const QuicVersionNegotiationPacket
* version_negotiation_packet() {
331 return framer_
.version_negotiation_packet();
334 void set_is_write_blocked_data_buffered(bool buffered
) {
335 is_write_blocked_data_buffered_
= buffered
;
338 void set_perspective(Perspective perspective
) {
339 // We invert perspective here, because the framer needs to parse packets
341 perspective
= perspective
== Perspective::IS_CLIENT
342 ? Perspective::IS_SERVER
343 : Perspective::IS_CLIENT
;
344 QuicFramerPeer::SetPerspective(framer_
.framer(), perspective
);
347 // final_bytes_of_last_packet_ returns the last four bytes of the previous
348 // packet as a little-endian, uint32. This is intended to be used with a
349 // TaggingEncrypter so that tests can determine which encrypter was used for
351 uint32
final_bytes_of_last_packet() { return final_bytes_of_last_packet_
; }
353 // Returns the final bytes of the second to last packet.
354 uint32
final_bytes_of_previous_packet() {
355 return final_bytes_of_previous_packet_
;
358 void use_tagging_decrypter() {
359 use_tagging_decrypter_
= true;
362 uint32
packets_write_attempts() { return packets_write_attempts_
; }
364 void Reset() { framer_
.Reset(); }
366 void SetSupportedVersions(const QuicVersionVector
& versions
) {
367 framer_
.SetSupportedVersions(versions
);
371 QuicVersion version_
;
372 SimpleQuicFramer framer_
;
373 size_t last_packet_size_
;
375 bool block_on_next_write_
;
376 bool is_write_blocked_data_buffered_
;
377 uint32 final_bytes_of_last_packet_
;
378 uint32 final_bytes_of_previous_packet_
;
379 bool use_tagging_decrypter_
;
380 uint32 packets_write_attempts_
;
382 // If non-zero, the clock will pause during WritePacket for this amount of
384 QuicTime::Delta write_pause_time_delta_
;
386 DISALLOW_COPY_AND_ASSIGN(TestPacketWriter
);
389 class TestConnection
: public QuicConnection
{
391 TestConnection(QuicConnectionId connection_id
,
393 TestConnectionHelper
* helper
,
394 const PacketWriterFactory
& factory
,
395 Perspective perspective
,
397 : QuicConnection(connection_id
,
401 /* owns_writer= */ false,
403 /* is_secure= */ false,
404 SupportedVersions(version
)) {
405 // Disable tail loss probes for most tests.
406 QuicSentPacketManagerPeer::SetMaxTailLossProbes(
407 QuicConnectionPeer::GetSentPacketManager(this), 0);
408 writer()->set_perspective(perspective
);
412 QuicConnectionPeer::SendAck(this);
415 void SetSendAlgorithm(SendAlgorithmInterface
* send_algorithm
) {
416 QuicConnectionPeer::SetSendAlgorithm(this, send_algorithm
);
419 void SetLossAlgorithm(LossDetectionInterface
* loss_algorithm
) {
420 QuicSentPacketManagerPeer::SetLossAlgorithm(
421 QuicConnectionPeer::GetSentPacketManager(this), loss_algorithm
);
424 void SendPacket(EncryptionLevel level
,
425 QuicPacketSequenceNumber sequence_number
,
427 QuicPacketEntropyHash entropy_hash
,
428 HasRetransmittableData retransmittable
) {
429 RetransmittableFrames
* retransmittable_frames
=
430 retransmittable
== HAS_RETRANSMITTABLE_DATA
431 ? new RetransmittableFrames(ENCRYPTION_NONE
)
433 char buffer
[kMaxPacketSize
];
434 QuicEncryptedPacket
* encrypted
=
435 QuicConnectionPeer::GetFramer(this)->EncryptPacket(
436 ENCRYPTION_NONE
, sequence_number
, *packet
, buffer
, kMaxPacketSize
);
438 OnSerializedPacket(SerializedPacket(sequence_number
,
439 PACKET_6BYTE_SEQUENCE_NUMBER
, encrypted
,
440 entropy_hash
, retransmittable_frames
));
443 QuicConsumedData
SendStreamDataWithString(
446 QuicStreamOffset offset
,
448 QuicAckNotifier::DelegateInterface
* delegate
) {
449 return SendStreamDataWithStringHelper(id
, data
, offset
, fin
,
450 MAY_FEC_PROTECT
, delegate
);
453 QuicConsumedData
SendStreamDataWithStringWithFec(
456 QuicStreamOffset offset
,
458 QuicAckNotifier::DelegateInterface
* delegate
) {
459 return SendStreamDataWithStringHelper(id
, data
, offset
, fin
,
460 MUST_FEC_PROTECT
, delegate
);
463 QuicConsumedData
SendStreamDataWithStringHelper(
466 QuicStreamOffset offset
,
468 FecProtection fec_protection
,
469 QuicAckNotifier::DelegateInterface
* delegate
) {
472 data_iov
.Append(const_cast<char*>(data
.data()), data
.size());
474 return QuicConnection::SendStreamData(id
, data_iov
, offset
, fin
,
475 fec_protection
, delegate
);
478 QuicConsumedData
SendStreamData3() {
479 return SendStreamDataWithString(kClientDataStreamId1
, "food", 0, !kFin
,
483 QuicConsumedData
SendStreamData3WithFec() {
484 return SendStreamDataWithStringWithFec(kClientDataStreamId1
, "food", 0,
488 QuicConsumedData
SendStreamData5() {
489 return SendStreamDataWithString(kClientDataStreamId2
, "food2", 0, !kFin
,
493 QuicConsumedData
SendStreamData5WithFec() {
494 return SendStreamDataWithStringWithFec(kClientDataStreamId2
, "food2", 0,
497 // Ensures the connection can write stream data before writing.
498 QuicConsumedData
EnsureWritableAndSendStreamData5() {
499 EXPECT_TRUE(CanWriteStreamData());
500 return SendStreamData5();
503 // The crypto stream has special semantics so that it is not blocked by a
504 // congestion window limitation, and also so that it gets put into a separate
505 // packet (so that it is easier to reason about a crypto frame not being
506 // split needlessly across packet boundaries). As a result, we have separate
507 // tests for some cases for this stream.
508 QuicConsumedData
SendCryptoStreamData() {
509 return SendStreamDataWithString(kCryptoStreamId
, "chlo", 0, !kFin
, nullptr);
512 void set_version(QuicVersion version
) {
513 QuicConnectionPeer::GetFramer(this)->set_version(version
);
516 void SetSupportedVersions(const QuicVersionVector
& versions
) {
517 QuicConnectionPeer::GetFramer(this)->SetSupportedVersions(versions
);
518 writer()->SetSupportedVersions(versions
);
521 void set_perspective(Perspective perspective
) {
522 writer()->set_perspective(perspective
);
523 QuicConnectionPeer::SetPerspective(this, perspective
);
526 TestConnectionHelper::TestAlarm
* GetAckAlarm() {
527 return reinterpret_cast<TestConnectionHelper::TestAlarm
*>(
528 QuicConnectionPeer::GetAckAlarm(this));
531 TestConnectionHelper::TestAlarm
* GetPingAlarm() {
532 return reinterpret_cast<TestConnectionHelper::TestAlarm
*>(
533 QuicConnectionPeer::GetPingAlarm(this));
536 TestConnectionHelper::TestAlarm
* GetFecAlarm() {
537 return reinterpret_cast<TestConnectionHelper::TestAlarm
*>(
538 QuicConnectionPeer::GetFecAlarm(this));
541 TestConnectionHelper::TestAlarm
* GetResumeWritesAlarm() {
542 return reinterpret_cast<TestConnectionHelper::TestAlarm
*>(
543 QuicConnectionPeer::GetResumeWritesAlarm(this));
546 TestConnectionHelper::TestAlarm
* GetRetransmissionAlarm() {
547 return reinterpret_cast<TestConnectionHelper::TestAlarm
*>(
548 QuicConnectionPeer::GetRetransmissionAlarm(this));
551 TestConnectionHelper::TestAlarm
* GetSendAlarm() {
552 return reinterpret_cast<TestConnectionHelper::TestAlarm
*>(
553 QuicConnectionPeer::GetSendAlarm(this));
556 TestConnectionHelper::TestAlarm
* GetTimeoutAlarm() {
557 return reinterpret_cast<TestConnectionHelper::TestAlarm
*>(
558 QuicConnectionPeer::GetTimeoutAlarm(this));
561 using QuicConnection::SelectMutualVersion
;
564 TestPacketWriter
* writer() {
565 return static_cast<TestPacketWriter
*>(QuicConnection::writer());
568 DISALLOW_COPY_AND_ASSIGN(TestConnection
);
571 // Used for testing packets revived from FEC packets.
572 class FecQuicConnectionDebugVisitor
573 : public QuicConnectionDebugVisitor
{
575 void OnRevivedPacket(const QuicPacketHeader
& header
,
576 StringPiece data
) override
{
577 revived_header_
= header
;
580 // Public accessor method.
581 QuicPacketHeader
revived_header() const {
582 return revived_header_
;
586 QuicPacketHeader revived_header_
;
589 class MockPacketWriterFactory
: public QuicConnection::PacketWriterFactory
{
591 explicit MockPacketWriterFactory(QuicPacketWriter
* writer
) {
592 ON_CALL(*this, Create(_
)).WillByDefault(Return(writer
));
594 ~MockPacketWriterFactory() override
{}
596 MOCK_CONST_METHOD1(Create
, QuicPacketWriter
*(QuicConnection
* connection
));
599 class QuicConnectionTest
: public ::testing::TestWithParam
<QuicVersion
> {
602 : connection_id_(42),
603 framer_(SupportedVersions(version()),
605 Perspective::IS_CLIENT
),
606 peer_creator_(connection_id_
, &framer_
, &random_generator_
),
607 send_algorithm_(new StrictMock
<MockSendAlgorithm
>),
608 loss_algorithm_(new MockLossAlgorithm()),
609 helper_(new TestConnectionHelper(&clock_
, &random_generator_
)),
610 writer_(new TestPacketWriter(version(), &clock_
)),
611 factory_(writer_
.get()),
612 connection_(connection_id_
,
616 Perspective::IS_CLIENT
,
618 creator_(QuicConnectionPeer::GetPacketCreator(&connection_
)),
619 generator_(QuicConnectionPeer::GetPacketGenerator(&connection_
)),
620 manager_(QuicConnectionPeer::GetSentPacketManager(&connection_
)),
621 frame1_(1, false, 0, MakeIOVector(data1
)),
622 frame2_(1, false, 3, MakeIOVector(data2
)),
623 sequence_number_length_(PACKET_6BYTE_SEQUENCE_NUMBER
),
624 connection_id_length_(PACKET_8BYTE_CONNECTION_ID
) {
625 connection_
.set_visitor(&visitor_
);
626 connection_
.SetSendAlgorithm(send_algorithm_
);
627 connection_
.SetLossAlgorithm(loss_algorithm_
);
628 framer_
.set_received_entropy_calculator(&entropy_calculator_
);
630 *send_algorithm_
, TimeUntilSend(_
, _
, _
)).WillRepeatedly(Return(
631 QuicTime::Delta::Zero()));
632 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
))
634 EXPECT_CALL(*send_algorithm_
, RetransmissionDelay()).WillRepeatedly(
635 Return(QuicTime::Delta::Zero()));
636 EXPECT_CALL(*send_algorithm_
, GetCongestionWindow()).WillRepeatedly(
637 Return(kMaxPacketSize
));
638 ON_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
))
639 .WillByDefault(Return(true));
640 EXPECT_CALL(*send_algorithm_
, HasReliableBandwidthEstimate())
642 EXPECT_CALL(*send_algorithm_
, BandwidthEstimate())
644 .WillRepeatedly(Return(QuicBandwidth::Zero()));
645 EXPECT_CALL(*send_algorithm_
, InSlowStart()).Times(AnyNumber());
646 EXPECT_CALL(*send_algorithm_
, InRecovery()).Times(AnyNumber());
647 EXPECT_CALL(visitor_
, WillingAndAbleToWrite()).Times(AnyNumber());
648 EXPECT_CALL(visitor_
, HasPendingHandshake()).Times(AnyNumber());
649 EXPECT_CALL(visitor_
, OnCanWrite()).Times(AnyNumber());
650 EXPECT_CALL(visitor_
, HasOpenDataStreams()).WillRepeatedly(Return(false));
651 EXPECT_CALL(visitor_
, OnCongestionWindowChange(_
)).Times(AnyNumber());
653 EXPECT_CALL(*loss_algorithm_
, GetLossTimeout())
654 .WillRepeatedly(Return(QuicTime::Zero()));
655 EXPECT_CALL(*loss_algorithm_
, DetectLostPackets(_
, _
, _
, _
))
656 .WillRepeatedly(Return(SequenceNumberSet()));
659 QuicVersion
version() {
663 QuicAckFrame
* outgoing_ack() {
664 QuicConnectionPeer::PopulateAckFrame(&connection_
, &ack_
);
668 QuicStopWaitingFrame
* stop_waiting() {
669 QuicConnectionPeer::PopulateStopWaitingFrame(&connection_
, &stop_waiting_
);
670 return &stop_waiting_
;
673 QuicPacketSequenceNumber
least_unacked() {
674 if (writer_
->stop_waiting_frames().empty()) {
677 return writer_
->stop_waiting_frames()[0].least_unacked
;
680 void use_tagging_decrypter() {
681 writer_
->use_tagging_decrypter();
684 void ProcessPacket(QuicPacketSequenceNumber number
) {
685 EXPECT_CALL(visitor_
, OnStreamFrames(_
)).Times(1);
686 ProcessDataPacket(number
, 0, !kEntropyFlag
);
689 QuicPacketEntropyHash
ProcessFramePacket(QuicFrame frame
) {
691 frames
.push_back(QuicFrame(frame
));
692 QuicPacketCreatorPeer::SetSendVersionInPacket(
693 &peer_creator_
, connection_
.perspective() == Perspective::IS_SERVER
);
695 char buffer
[kMaxPacketSize
];
696 SerializedPacket serialized_packet
=
697 peer_creator_
.SerializeAllFrames(frames
, buffer
, kMaxPacketSize
);
698 scoped_ptr
<QuicEncryptedPacket
> encrypted(serialized_packet
.packet
);
699 connection_
.ProcessUdpPacket(IPEndPoint(), IPEndPoint(), *encrypted
);
700 return serialized_packet
.entropy_hash
;
703 size_t ProcessDataPacket(QuicPacketSequenceNumber number
,
704 QuicFecGroupNumber fec_group
,
706 return ProcessDataPacketAtLevel(number
, fec_group
, entropy_flag
,
710 size_t ProcessDataPacketAtLevel(QuicPacketSequenceNumber number
,
711 QuicFecGroupNumber fec_group
,
713 EncryptionLevel level
) {
714 scoped_ptr
<QuicPacket
> packet(ConstructDataPacket(number
, fec_group
,
716 char buffer
[kMaxPacketSize
];
717 scoped_ptr
<QuicEncryptedPacket
> encrypted(
718 framer_
.EncryptPacket(level
, number
, *packet
, buffer
, kMaxPacketSize
));
719 connection_
.ProcessUdpPacket(IPEndPoint(), IPEndPoint(), *encrypted
);
720 return encrypted
->length();
723 void ProcessClosePacket(QuicPacketSequenceNumber number
,
724 QuicFecGroupNumber fec_group
) {
725 scoped_ptr
<QuicPacket
> packet(ConstructClosePacket(number
, fec_group
));
726 char buffer
[kMaxPacketSize
];
727 scoped_ptr
<QuicEncryptedPacket
> encrypted(framer_
.EncryptPacket(
728 ENCRYPTION_NONE
, number
, *packet
, buffer
, kMaxPacketSize
));
729 connection_
.ProcessUdpPacket(IPEndPoint(), IPEndPoint(), *encrypted
);
732 size_t ProcessFecProtectedPacket(QuicPacketSequenceNumber number
,
733 bool expect_revival
, bool entropy_flag
) {
734 if (expect_revival
) {
735 EXPECT_CALL(visitor_
, OnStreamFrames(_
)).Times(1);
737 EXPECT_CALL(visitor_
, OnStreamFrames(_
)).Times(1).
738 RetiresOnSaturation();
739 return ProcessDataPacket(number
, 1, entropy_flag
);
742 // Processes an FEC packet that covers the packets that would have been
744 size_t ProcessFecPacket(QuicPacketSequenceNumber number
,
745 QuicPacketSequenceNumber min_protected_packet
,
748 QuicPacket
* packet
) {
749 if (expect_revival
) {
750 EXPECT_CALL(visitor_
, OnStreamFrames(_
)).Times(1);
753 // Construct the decrypted data packet so we can compute the correct
754 // redundancy. If |packet| has been provided then use that, otherwise
755 // construct a default data packet.
756 scoped_ptr
<QuicPacket
> data_packet
;
758 data_packet
.reset(packet
);
760 data_packet
.reset(ConstructDataPacket(number
, 1, !kEntropyFlag
));
763 QuicPacketHeader header
;
764 header
.public_header
.connection_id
= connection_id_
;
765 header
.public_header
.sequence_number_length
= sequence_number_length_
;
766 header
.public_header
.connection_id_length
= connection_id_length_
;
767 header
.packet_sequence_number
= number
;
768 header
.entropy_flag
= entropy_flag
;
769 header
.fec_flag
= true;
770 header
.is_in_fec_group
= IN_FEC_GROUP
;
771 header
.fec_group
= min_protected_packet
;
772 QuicFecData fec_data
;
773 fec_data
.fec_group
= header
.fec_group
;
775 // Since all data packets in this test have the same payload, the
776 // redundancy is either equal to that payload or the xor of that payload
777 // with itself, depending on the number of packets.
778 if (((number
- min_protected_packet
) % 2) == 0) {
779 for (size_t i
= GetStartOfFecProtectedData(
780 header
.public_header
.connection_id_length
,
781 header
.public_header
.version_flag
,
782 header
.public_header
.sequence_number_length
);
783 i
< data_packet
->length(); ++i
) {
784 data_packet
->mutable_data()[i
] ^= data_packet
->data()[i
];
787 fec_data
.redundancy
= data_packet
->FecProtectedData();
789 scoped_ptr
<QuicPacket
> fec_packet(framer_
.BuildFecPacket(header
, fec_data
));
790 char buffer
[kMaxPacketSize
];
791 scoped_ptr
<QuicEncryptedPacket
> encrypted(framer_
.EncryptPacket(
792 ENCRYPTION_NONE
, number
, *fec_packet
, buffer
, kMaxPacketSize
));
794 connection_
.ProcessUdpPacket(IPEndPoint(), IPEndPoint(), *encrypted
);
795 return encrypted
->length();
798 QuicByteCount
SendStreamDataToPeer(QuicStreamId id
,
800 QuicStreamOffset offset
,
802 QuicPacketSequenceNumber
* last_packet
) {
803 QuicByteCount packet_size
;
804 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
))
805 .WillOnce(DoAll(SaveArg
<3>(&packet_size
), Return(true)));
806 connection_
.SendStreamDataWithString(id
, data
, offset
, fin
, nullptr);
807 if (last_packet
!= nullptr) {
808 *last_packet
= creator_
->sequence_number();
810 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
))
815 void SendAckPacketToPeer() {
816 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
)).Times(1);
817 connection_
.SendAck();
818 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
))
822 QuicPacketEntropyHash
ProcessAckPacket(QuicAckFrame
* frame
) {
823 return ProcessFramePacket(QuicFrame(frame
));
826 QuicPacketEntropyHash
ProcessStopWaitingPacket(QuicStopWaitingFrame
* frame
) {
827 return ProcessFramePacket(QuicFrame(frame
));
830 QuicPacketEntropyHash
ProcessGoAwayPacket(QuicGoAwayFrame
* frame
) {
831 return ProcessFramePacket(QuicFrame(frame
));
834 bool IsMissing(QuicPacketSequenceNumber number
) {
835 return IsAwaitingPacket(*outgoing_ack(), number
);
838 QuicPacket
* ConstructPacket(QuicPacketHeader header
, QuicFrames frames
) {
839 QuicPacket
* packet
= BuildUnsizedDataPacket(&framer_
, header
, frames
);
840 EXPECT_NE(nullptr, packet
);
844 QuicPacket
* ConstructDataPacket(QuicPacketSequenceNumber number
,
845 QuicFecGroupNumber fec_group
,
847 QuicPacketHeader header
;
848 header
.public_header
.connection_id
= connection_id_
;
849 header
.public_header
.sequence_number_length
= sequence_number_length_
;
850 header
.public_header
.connection_id_length
= connection_id_length_
;
851 header
.entropy_flag
= entropy_flag
;
852 header
.packet_sequence_number
= number
;
853 header
.is_in_fec_group
= fec_group
== 0u ? NOT_IN_FEC_GROUP
: IN_FEC_GROUP
;
854 header
.fec_group
= fec_group
;
857 frames
.push_back(QuicFrame(&frame1_
));
858 return ConstructPacket(header
, frames
);
861 QuicPacket
* ConstructClosePacket(QuicPacketSequenceNumber number
,
862 QuicFecGroupNumber fec_group
) {
863 QuicPacketHeader header
;
864 header
.public_header
.connection_id
= connection_id_
;
865 header
.packet_sequence_number
= number
;
866 header
.is_in_fec_group
= fec_group
== 0u ? NOT_IN_FEC_GROUP
: IN_FEC_GROUP
;
867 header
.fec_group
= fec_group
;
869 QuicConnectionCloseFrame qccf
;
870 qccf
.error_code
= QUIC_PEER_GOING_AWAY
;
873 frames
.push_back(QuicFrame(&qccf
));
874 return ConstructPacket(header
, frames
);
877 QuicTime::Delta
DefaultRetransmissionTime() {
878 return QuicTime::Delta::FromMilliseconds(kDefaultRetransmissionTimeMs
);
881 QuicTime::Delta
DefaultDelayedAckTime() {
882 return QuicTime::Delta::FromMilliseconds(kMaxDelayedAckTimeMs
);
885 // Initialize a frame acknowledging all packets up to largest_observed.
886 const QuicAckFrame
InitAckFrame(QuicPacketSequenceNumber largest_observed
) {
887 QuicAckFrame
frame(MakeAckFrame(largest_observed
));
888 if (largest_observed
> 0) {
890 QuicConnectionPeer::GetSentEntropyHash(&connection_
,
896 const QuicStopWaitingFrame
InitStopWaitingFrame(
897 QuicPacketSequenceNumber least_unacked
) {
898 QuicStopWaitingFrame frame
;
899 frame
.least_unacked
= least_unacked
;
903 // Explicitly nack a packet.
904 void NackPacket(QuicPacketSequenceNumber missing
, QuicAckFrame
* frame
) {
905 frame
->missing_packets
.insert(missing
);
906 frame
->entropy_hash
^=
907 QuicConnectionPeer::PacketEntropy(&connection_
, missing
);
910 // Undo nacking a packet within the frame.
911 void AckPacket(QuicPacketSequenceNumber arrived
, QuicAckFrame
* frame
) {
912 EXPECT_THAT(frame
->missing_packets
, Contains(arrived
));
913 frame
->missing_packets
.erase(arrived
);
914 frame
->entropy_hash
^=
915 QuicConnectionPeer::PacketEntropy(&connection_
, arrived
);
918 void TriggerConnectionClose() {
919 // Send an erroneous packet to close the connection.
920 EXPECT_CALL(visitor_
,
921 OnConnectionClosed(QUIC_INVALID_PACKET_HEADER
, false));
922 // Call ProcessDataPacket rather than ProcessPacket, as we should not get a
923 // packet call to the visitor.
924 ProcessDataPacket(6000, 0, !kEntropyFlag
);
925 EXPECT_FALSE(QuicConnectionPeer::GetConnectionClosePacket(&connection_
) ==
929 void BlockOnNextWrite() {
930 writer_
->BlockOnNextWrite();
931 EXPECT_CALL(visitor_
, OnWriteBlocked()).Times(AtLeast(1));
934 void SetWritePauseTimeDelta(QuicTime::Delta delta
) {
935 writer_
->SetWritePauseTimeDelta(delta
);
938 void CongestionBlockWrites() {
939 EXPECT_CALL(*send_algorithm_
,
940 TimeUntilSend(_
, _
, _
)).WillRepeatedly(
941 testing::Return(QuicTime::Delta::FromSeconds(1)));
944 void CongestionUnblockWrites() {
945 EXPECT_CALL(*send_algorithm_
,
946 TimeUntilSend(_
, _
, _
)).WillRepeatedly(
947 testing::Return(QuicTime::Delta::Zero()));
950 QuicConnectionId connection_id_
;
952 QuicPacketCreator peer_creator_
;
953 MockEntropyCalculator entropy_calculator_
;
955 MockSendAlgorithm
* send_algorithm_
;
956 MockLossAlgorithm
* loss_algorithm_
;
958 MockRandom random_generator_
;
959 scoped_ptr
<TestConnectionHelper
> helper_
;
960 scoped_ptr
<TestPacketWriter
> writer_
;
961 NiceMock
<MockPacketWriterFactory
> factory_
;
962 TestConnection connection_
;
963 QuicPacketCreator
* creator_
;
964 QuicPacketGenerator
* generator_
;
965 QuicSentPacketManager
* manager_
;
966 StrictMock
<MockConnectionVisitor
> visitor_
;
968 QuicStreamFrame frame1_
;
969 QuicStreamFrame frame2_
;
971 QuicStopWaitingFrame stop_waiting_
;
972 QuicSequenceNumberLength sequence_number_length_
;
973 QuicConnectionIdLength connection_id_length_
;
976 DISALLOW_COPY_AND_ASSIGN(QuicConnectionTest
);
979 // Run all end to end tests with all supported versions.
980 INSTANTIATE_TEST_CASE_P(SupportedVersion
,
982 ::testing::ValuesIn(QuicSupportedVersions()));
984 TEST_P(QuicConnectionTest
, MaxPacketSize
) {
985 EXPECT_EQ(Perspective::IS_CLIENT
, connection_
.perspective());
986 EXPECT_EQ(1350u, connection_
.max_packet_length());
989 TEST_P(QuicConnectionTest
, SmallerServerMaxPacketSize
) {
990 QuicConnectionId connection_id
= 42;
991 TestConnection
connection(connection_id
, IPEndPoint(), helper_
.get(),
992 factory_
, Perspective::IS_SERVER
, version());
993 EXPECT_EQ(Perspective::IS_SERVER
, connection
.perspective());
994 EXPECT_EQ(1000u, connection
.max_packet_length());
997 TEST_P(QuicConnectionTest
, IncreaseServerMaxPacketSize
) {
998 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
1000 connection_
.set_perspective(Perspective::IS_SERVER
);
1001 connection_
.set_max_packet_length(1000);
1003 QuicPacketHeader header
;
1004 header
.public_header
.connection_id
= connection_id_
;
1005 header
.public_header
.version_flag
= true;
1006 header
.packet_sequence_number
= 1;
1009 QuicPaddingFrame padding
;
1010 frames
.push_back(QuicFrame(&frame1_
));
1011 frames
.push_back(QuicFrame(&padding
));
1012 scoped_ptr
<QuicPacket
> packet(ConstructPacket(header
, frames
));
1013 char buffer
[kMaxPacketSize
];
1014 scoped_ptr
<QuicEncryptedPacket
> encrypted(framer_
.EncryptPacket(
1015 ENCRYPTION_NONE
, 12, *packet
, buffer
, kMaxPacketSize
));
1016 EXPECT_EQ(kMaxPacketSize
, encrypted
->length());
1018 framer_
.set_version(version());
1019 EXPECT_CALL(visitor_
, OnStreamFrames(_
)).Times(1);
1020 connection_
.ProcessUdpPacket(IPEndPoint(), IPEndPoint(), *encrypted
);
1022 EXPECT_EQ(kMaxPacketSize
, connection_
.max_packet_length());
1025 TEST_P(QuicConnectionTest
, PacketsInOrder
) {
1026 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
1029 EXPECT_EQ(1u, outgoing_ack()->largest_observed
);
1030 EXPECT_EQ(0u, outgoing_ack()->missing_packets
.size());
1033 EXPECT_EQ(2u, outgoing_ack()->largest_observed
);
1034 EXPECT_EQ(0u, outgoing_ack()->missing_packets
.size());
1037 EXPECT_EQ(3u, outgoing_ack()->largest_observed
);
1038 EXPECT_EQ(0u, outgoing_ack()->missing_packets
.size());
1041 TEST_P(QuicConnectionTest
, PacketsOutOfOrder
) {
1042 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
1045 EXPECT_EQ(3u, outgoing_ack()->largest_observed
);
1046 EXPECT_TRUE(IsMissing(2));
1047 EXPECT_TRUE(IsMissing(1));
1050 EXPECT_EQ(3u, outgoing_ack()->largest_observed
);
1051 EXPECT_FALSE(IsMissing(2));
1052 EXPECT_TRUE(IsMissing(1));
1055 EXPECT_EQ(3u, outgoing_ack()->largest_observed
);
1056 EXPECT_FALSE(IsMissing(2));
1057 EXPECT_FALSE(IsMissing(1));
1060 TEST_P(QuicConnectionTest
, DuplicatePacket
) {
1061 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
1064 EXPECT_EQ(3u, outgoing_ack()->largest_observed
);
1065 EXPECT_TRUE(IsMissing(2));
1066 EXPECT_TRUE(IsMissing(1));
1068 // Send packet 3 again, but do not set the expectation that
1069 // the visitor OnStreamFrames() will be called.
1070 ProcessDataPacket(3, 0, !kEntropyFlag
);
1071 EXPECT_EQ(3u, outgoing_ack()->largest_observed
);
1072 EXPECT_TRUE(IsMissing(2));
1073 EXPECT_TRUE(IsMissing(1));
1076 TEST_P(QuicConnectionTest
, PacketsOutOfOrderWithAdditionsAndLeastAwaiting
) {
1077 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
1080 EXPECT_EQ(3u, outgoing_ack()->largest_observed
);
1081 EXPECT_TRUE(IsMissing(2));
1082 EXPECT_TRUE(IsMissing(1));
1085 EXPECT_EQ(3u, outgoing_ack()->largest_observed
);
1086 EXPECT_TRUE(IsMissing(1));
1089 EXPECT_EQ(5u, outgoing_ack()->largest_observed
);
1090 EXPECT_TRUE(IsMissing(1));
1091 EXPECT_TRUE(IsMissing(4));
1093 // Pretend at this point the client has gotten acks for 2 and 3 and 1 is a
1094 // packet the peer will not retransmit. It indicates this by sending 'least
1095 // awaiting' is 4. The connection should then realize 1 will not be
1096 // retransmitted, and will remove it from the missing list.
1097 QuicPacketCreatorPeer::SetSequenceNumber(&peer_creator_
, 5);
1098 QuicAckFrame frame
= InitAckFrame(1);
1099 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(_
, _
, _
, _
));
1100 ProcessAckPacket(&frame
);
1102 // Force an ack to be sent.
1103 SendAckPacketToPeer();
1104 EXPECT_TRUE(IsMissing(4));
1107 TEST_P(QuicConnectionTest
, RejectPacketTooFarOut
) {
1108 EXPECT_CALL(visitor_
,
1109 OnConnectionClosed(QUIC_INVALID_PACKET_HEADER
, false));
1110 // Call ProcessDataPacket rather than ProcessPacket, as we should not get a
1111 // packet call to the visitor.
1112 ProcessDataPacket(6000, 0, !kEntropyFlag
);
1113 EXPECT_FALSE(QuicConnectionPeer::GetConnectionClosePacket(&connection_
) ==
1117 TEST_P(QuicConnectionTest
, RejectUnencryptedStreamData
) {
1118 // Process an unencrypted packet from the non-crypto stream.
1119 frame1_
.stream_id
= 3;
1120 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
1121 EXPECT_CALL(visitor_
, OnConnectionClosed(QUIC_UNENCRYPTED_STREAM_DATA
,
1123 ProcessDataPacket(1, 0, !kEntropyFlag
);
1124 EXPECT_FALSE(QuicConnectionPeer::GetConnectionClosePacket(&connection_
) ==
1126 const vector
<QuicConnectionCloseFrame
>& connection_close_frames
=
1127 writer_
->connection_close_frames();
1128 EXPECT_EQ(1u, connection_close_frames
.size());
1129 EXPECT_EQ(QUIC_UNENCRYPTED_STREAM_DATA
,
1130 connection_close_frames
[0].error_code
);
1133 TEST_P(QuicConnectionTest
, TruncatedAck
) {
1134 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
1135 QuicPacketSequenceNumber num_packets
= 256 * 2 + 1;
1136 for (QuicPacketSequenceNumber i
= 0; i
< num_packets
; ++i
) {
1137 SendStreamDataToPeer(3, "foo", i
* 3, !kFin
, nullptr);
1140 QuicAckFrame frame
= InitAckFrame(num_packets
);
1141 SequenceNumberSet lost_packets
;
1142 // Create an ack with 256 nacks, none adjacent to one another.
1143 for (QuicPacketSequenceNumber i
= 1; i
<= 256; ++i
) {
1144 NackPacket(i
* 2, &frame
);
1145 if (i
< 256) { // Last packet is nacked, but not lost.
1146 lost_packets
.insert(i
* 2);
1149 EXPECT_CALL(*loss_algorithm_
, DetectLostPackets(_
, _
, _
, _
))
1150 .WillOnce(Return(lost_packets
));
1151 EXPECT_CALL(entropy_calculator_
, EntropyHash(511))
1152 .WillOnce(Return(static_cast<QuicPacketEntropyHash
>(0)));
1153 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
1154 ProcessAckPacket(&frame
);
1156 // A truncated ack will not have the true largest observed.
1157 EXPECT_GT(num_packets
, manager_
->largest_observed());
1159 AckPacket(192, &frame
);
1161 // Removing one missing packet allows us to ack 192 and one more range, but
1162 // 192 has already been declared lost, so it doesn't register as an ack.
1163 EXPECT_CALL(*loss_algorithm_
, DetectLostPackets(_
, _
, _
, _
))
1164 .WillOnce(Return(SequenceNumberSet()));
1165 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
1166 ProcessAckPacket(&frame
);
1167 EXPECT_EQ(num_packets
, manager_
->largest_observed());
1170 TEST_P(QuicConnectionTest
, AckReceiptCausesAckSendBadEntropy
) {
1171 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
1174 // Delay sending, then queue up an ack.
1175 EXPECT_CALL(*send_algorithm_
,
1176 TimeUntilSend(_
, _
, _
)).WillOnce(
1177 testing::Return(QuicTime::Delta::FromMicroseconds(1)));
1178 QuicConnectionPeer::SendAck(&connection_
);
1180 // Process an ack with a least unacked of the received ack.
1181 // This causes an ack to be sent when TimeUntilSend returns 0.
1182 EXPECT_CALL(*send_algorithm_
,
1183 TimeUntilSend(_
, _
, _
)).WillRepeatedly(
1184 testing::Return(QuicTime::Delta::Zero()));
1185 // Skip a packet and then record an ack.
1186 QuicPacketCreatorPeer::SetSequenceNumber(&peer_creator_
, 2);
1187 QuicAckFrame frame
= InitAckFrame(0);
1188 ProcessAckPacket(&frame
);
1191 TEST_P(QuicConnectionTest
, OutOfOrderReceiptCausesAckSend
) {
1192 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
1195 // Should ack immediately since we have missing packets.
1196 EXPECT_EQ(1u, writer_
->packets_write_attempts());
1199 // Should ack immediately since we have missing packets.
1200 EXPECT_EQ(2u, writer_
->packets_write_attempts());
1203 // Should ack immediately, since this fills the last hole.
1204 EXPECT_EQ(3u, writer_
->packets_write_attempts());
1207 // Should not cause an ack.
1208 EXPECT_EQ(3u, writer_
->packets_write_attempts());
1211 TEST_P(QuicConnectionTest
, AckReceiptCausesAckSend
) {
1212 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
1214 QuicPacketSequenceNumber original
;
1215 QuicByteCount packet_size
;
1216 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
))
1217 .WillOnce(DoAll(SaveArg
<2>(&original
), SaveArg
<3>(&packet_size
),
1219 connection_
.SendStreamDataWithString(3, "foo", 0, !kFin
, nullptr);
1220 QuicAckFrame frame
= InitAckFrame(original
);
1221 NackPacket(original
, &frame
);
1222 // First nack triggers early retransmit.
1223 SequenceNumberSet lost_packets
;
1224 lost_packets
.insert(1);
1225 EXPECT_CALL(*loss_algorithm_
, DetectLostPackets(_
, _
, _
, _
))
1226 .WillOnce(Return(lost_packets
));
1227 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
1228 QuicPacketSequenceNumber retransmission
;
1229 EXPECT_CALL(*send_algorithm_
,
1230 OnPacketSent(_
, _
, _
, packet_size
- kQuicVersionSize
, _
))
1231 .WillOnce(DoAll(SaveArg
<2>(&retransmission
), Return(true)));
1233 ProcessAckPacket(&frame
);
1235 QuicAckFrame frame2
= InitAckFrame(retransmission
);
1236 NackPacket(original
, &frame2
);
1237 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
1238 EXPECT_CALL(*loss_algorithm_
, DetectLostPackets(_
, _
, _
, _
))
1239 .WillOnce(Return(SequenceNumberSet()));
1240 ProcessAckPacket(&frame2
);
1242 // Now if the peer sends an ack which still reports the retransmitted packet
1243 // as missing, that will bundle an ack with data after two acks in a row
1244 // indicate the high water mark needs to be raised.
1245 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
,
1246 HAS_RETRANSMITTABLE_DATA
));
1247 connection_
.SendStreamDataWithString(3, "foo", 3, !kFin
, nullptr);
1249 EXPECT_EQ(1u, writer_
->frame_count());
1250 EXPECT_EQ(1u, writer_
->stream_frames().size());
1252 // No more packet loss for the rest of the test.
1253 EXPECT_CALL(*loss_algorithm_
, DetectLostPackets(_
, _
, _
, _
))
1254 .WillRepeatedly(Return(SequenceNumberSet()));
1255 ProcessAckPacket(&frame2
);
1256 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
,
1257 HAS_RETRANSMITTABLE_DATA
));
1258 connection_
.SendStreamDataWithString(3, "foo", 3, !kFin
, nullptr);
1260 EXPECT_EQ(3u, writer_
->frame_count());
1261 EXPECT_EQ(1u, writer_
->stream_frames().size());
1262 EXPECT_FALSE(writer_
->ack_frames().empty());
1264 // But an ack with no missing packets will not send an ack.
1265 AckPacket(original
, &frame2
);
1266 ProcessAckPacket(&frame2
);
1267 ProcessAckPacket(&frame2
);
1270 TEST_P(QuicConnectionTest
, 20AcksCausesAckSend
) {
1271 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
1273 SendStreamDataToPeer(1, "foo", 0, !kFin
, nullptr);
1275 QuicAlarm
* ack_alarm
= QuicConnectionPeer::GetAckAlarm(&connection_
);
1276 // But an ack with no missing packets will not send an ack.
1277 QuicAckFrame frame
= InitAckFrame(1);
1278 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
1279 EXPECT_CALL(*loss_algorithm_
, DetectLostPackets(_
, _
, _
, _
))
1280 .WillRepeatedly(Return(SequenceNumberSet()));
1281 for (int i
= 0; i
< 20; ++i
) {
1282 EXPECT_FALSE(ack_alarm
->IsSet());
1283 ProcessAckPacket(&frame
);
1285 EXPECT_TRUE(ack_alarm
->IsSet());
1288 TEST_P(QuicConnectionTest
, LeastUnackedLower
) {
1289 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
1291 SendStreamDataToPeer(1, "foo", 0, !kFin
, nullptr);
1292 SendStreamDataToPeer(1, "bar", 3, !kFin
, nullptr);
1293 SendStreamDataToPeer(1, "eep", 6, !kFin
, nullptr);
1295 // Start out saying the least unacked is 2.
1296 QuicPacketCreatorPeer::SetSequenceNumber(&peer_creator_
, 5);
1297 QuicStopWaitingFrame frame
= InitStopWaitingFrame(2);
1298 ProcessStopWaitingPacket(&frame
);
1300 // Change it to 1, but lower the sequence number to fake out-of-order packets.
1301 // This should be fine.
1302 QuicPacketCreatorPeer::SetSequenceNumber(&peer_creator_
, 1);
1303 // The scheduler will not process out of order acks, but all packet processing
1304 // causes the connection to try to write.
1305 EXPECT_CALL(visitor_
, OnCanWrite());
1306 QuicStopWaitingFrame frame2
= InitStopWaitingFrame(1);
1307 ProcessStopWaitingPacket(&frame2
);
1309 // Now claim it's one, but set the ordering so it was sent "after" the first
1310 // one. This should cause a connection error.
1311 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
));
1312 QuicPacketCreatorPeer::SetSequenceNumber(&peer_creator_
, 7);
1313 EXPECT_CALL(visitor_
,
1314 OnConnectionClosed(QUIC_INVALID_STOP_WAITING_DATA
, false));
1315 QuicStopWaitingFrame frame3
= InitStopWaitingFrame(1);
1316 ProcessStopWaitingPacket(&frame3
);
1319 TEST_P(QuicConnectionTest
, TooManySentPackets
) {
1320 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
1322 for (int i
= 0; i
< 1100; ++i
) {
1323 SendStreamDataToPeer(1, "foo", 3 * i
, !kFin
, nullptr);
1326 // Ack packet 1, which leaves more than the limit outstanding.
1327 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
1328 EXPECT_CALL(visitor_
, OnConnectionClosed(
1329 QUIC_TOO_MANY_OUTSTANDING_SENT_PACKETS
, false));
1330 // We're receive buffer limited, so the connection won't try to write more.
1331 EXPECT_CALL(visitor_
, OnCanWrite()).Times(0);
1333 // Nack every packet except the last one, leaving a huge gap.
1334 QuicAckFrame frame1
= InitAckFrame(1100);
1335 for (QuicPacketSequenceNumber i
= 1; i
< 1100; ++i
) {
1336 NackPacket(i
, &frame1
);
1338 ProcessAckPacket(&frame1
);
1341 TEST_P(QuicConnectionTest
, TooManyReceivedPackets
) {
1342 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
1343 EXPECT_CALL(visitor_
, OnConnectionClosed(
1344 QUIC_TOO_MANY_OUTSTANDING_RECEIVED_PACKETS
, false));
1346 // Miss every other packet for 1000 packets.
1347 for (QuicPacketSequenceNumber i
= 1; i
< 1000; ++i
) {
1348 ProcessPacket(i
* 2);
1349 if (!connection_
.connected()) {
1355 TEST_P(QuicConnectionTest
, LargestObservedLower
) {
1356 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
1358 SendStreamDataToPeer(1, "foo", 0, !kFin
, nullptr);
1359 SendStreamDataToPeer(1, "bar", 3, !kFin
, nullptr);
1360 SendStreamDataToPeer(1, "eep", 6, !kFin
, nullptr);
1361 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
1363 // Start out saying the largest observed is 2.
1364 QuicAckFrame frame1
= InitAckFrame(1);
1365 QuicAckFrame frame2
= InitAckFrame(2);
1366 ProcessAckPacket(&frame2
);
1368 // Now change it to 1, and it should cause a connection error.
1369 EXPECT_CALL(visitor_
, OnConnectionClosed(QUIC_INVALID_ACK_DATA
, false));
1370 EXPECT_CALL(visitor_
, OnCanWrite()).Times(0);
1371 ProcessAckPacket(&frame1
);
1374 TEST_P(QuicConnectionTest
, AckUnsentData
) {
1375 // Ack a packet which has not been sent.
1376 EXPECT_CALL(visitor_
, OnConnectionClosed(QUIC_INVALID_ACK_DATA
, false));
1377 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
1378 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
));
1379 QuicAckFrame
frame(MakeAckFrame(1));
1380 EXPECT_CALL(visitor_
, OnCanWrite()).Times(0);
1381 ProcessAckPacket(&frame
);
1384 TEST_P(QuicConnectionTest
, AckAll
) {
1385 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
1388 QuicPacketCreatorPeer::SetSequenceNumber(&peer_creator_
, 1);
1389 QuicAckFrame frame1
= InitAckFrame(0);
1390 ProcessAckPacket(&frame1
);
1393 TEST_P(QuicConnectionTest
, SendingDifferentSequenceNumberLengthsBandwidth
) {
1394 QuicPacketSequenceNumber last_packet
;
1395 SendStreamDataToPeer(1, "foo", 0, !kFin
, &last_packet
);
1396 EXPECT_EQ(1u, last_packet
);
1397 EXPECT_EQ(PACKET_1BYTE_SEQUENCE_NUMBER
,
1398 QuicPacketCreatorPeer::NextSequenceNumberLength(creator_
));
1399 EXPECT_EQ(PACKET_1BYTE_SEQUENCE_NUMBER
,
1400 writer_
->header().public_header
.sequence_number_length
);
1402 EXPECT_CALL(*send_algorithm_
, GetCongestionWindow()).WillRepeatedly(
1403 Return(kMaxPacketSize
* 256));
1405 SendStreamDataToPeer(1, "bar", 3, !kFin
, &last_packet
);
1406 EXPECT_EQ(2u, last_packet
);
1407 EXPECT_EQ(PACKET_2BYTE_SEQUENCE_NUMBER
,
1408 QuicPacketCreatorPeer::NextSequenceNumberLength(creator_
));
1409 // The 1 packet lag is due to the sequence number length being recalculated in
1410 // QuicConnection after a packet is sent.
1411 EXPECT_EQ(PACKET_1BYTE_SEQUENCE_NUMBER
,
1412 writer_
->header().public_header
.sequence_number_length
);
1414 EXPECT_CALL(*send_algorithm_
, GetCongestionWindow()).WillRepeatedly(
1415 Return(kMaxPacketSize
* 256 * 256));
1417 SendStreamDataToPeer(1, "foo", 6, !kFin
, &last_packet
);
1418 EXPECT_EQ(3u, last_packet
);
1419 EXPECT_EQ(PACKET_4BYTE_SEQUENCE_NUMBER
,
1420 QuicPacketCreatorPeer::NextSequenceNumberLength(creator_
));
1421 EXPECT_EQ(PACKET_2BYTE_SEQUENCE_NUMBER
,
1422 writer_
->header().public_header
.sequence_number_length
);
1424 EXPECT_CALL(*send_algorithm_
, GetCongestionWindow()).WillRepeatedly(
1425 Return(kMaxPacketSize
* 256 * 256 * 256));
1427 SendStreamDataToPeer(1, "bar", 9, !kFin
, &last_packet
);
1428 EXPECT_EQ(4u, last_packet
);
1429 EXPECT_EQ(PACKET_4BYTE_SEQUENCE_NUMBER
,
1430 QuicPacketCreatorPeer::NextSequenceNumberLength(creator_
));
1431 EXPECT_EQ(PACKET_4BYTE_SEQUENCE_NUMBER
,
1432 writer_
->header().public_header
.sequence_number_length
);
1434 EXPECT_CALL(*send_algorithm_
, GetCongestionWindow()).WillRepeatedly(
1435 Return(kMaxPacketSize
* 256 * 256 * 256 * 256));
1437 SendStreamDataToPeer(1, "foo", 12, !kFin
, &last_packet
);
1438 EXPECT_EQ(5u, last_packet
);
1439 EXPECT_EQ(PACKET_6BYTE_SEQUENCE_NUMBER
,
1440 QuicPacketCreatorPeer::NextSequenceNumberLength(creator_
));
1441 EXPECT_EQ(PACKET_4BYTE_SEQUENCE_NUMBER
,
1442 writer_
->header().public_header
.sequence_number_length
);
1445 // TODO(ianswett): Re-enable this test by finding a good way to test different
1446 // sequence number lengths without sending packets with giant gaps.
1447 TEST_P(QuicConnectionTest
,
1448 DISABLED_SendingDifferentSequenceNumberLengthsUnackedDelta
) {
1449 QuicPacketSequenceNumber last_packet
;
1450 SendStreamDataToPeer(1, "foo", 0, !kFin
, &last_packet
);
1451 EXPECT_EQ(1u, last_packet
);
1452 EXPECT_EQ(PACKET_1BYTE_SEQUENCE_NUMBER
,
1453 QuicPacketCreatorPeer::NextSequenceNumberLength(creator_
));
1454 EXPECT_EQ(PACKET_1BYTE_SEQUENCE_NUMBER
,
1455 writer_
->header().public_header
.sequence_number_length
);
1457 QuicPacketCreatorPeer::SetSequenceNumber(&peer_creator_
, 100);
1459 SendStreamDataToPeer(1, "bar", 3, !kFin
, &last_packet
);
1460 EXPECT_EQ(PACKET_2BYTE_SEQUENCE_NUMBER
,
1461 QuicPacketCreatorPeer::NextSequenceNumberLength(creator_
));
1462 EXPECT_EQ(PACKET_1BYTE_SEQUENCE_NUMBER
,
1463 writer_
->header().public_header
.sequence_number_length
);
1465 QuicPacketCreatorPeer::SetSequenceNumber(&peer_creator_
, 100 * 256);
1467 SendStreamDataToPeer(1, "foo", 6, !kFin
, &last_packet
);
1468 EXPECT_EQ(PACKET_4BYTE_SEQUENCE_NUMBER
,
1469 QuicPacketCreatorPeer::NextSequenceNumberLength(creator_
));
1470 EXPECT_EQ(PACKET_2BYTE_SEQUENCE_NUMBER
,
1471 writer_
->header().public_header
.sequence_number_length
);
1473 QuicPacketCreatorPeer::SetSequenceNumber(&peer_creator_
, 100 * 256 * 256);
1475 SendStreamDataToPeer(1, "bar", 9, !kFin
, &last_packet
);
1476 EXPECT_EQ(PACKET_4BYTE_SEQUENCE_NUMBER
,
1477 QuicPacketCreatorPeer::NextSequenceNumberLength(creator_
));
1478 EXPECT_EQ(PACKET_4BYTE_SEQUENCE_NUMBER
,
1479 writer_
->header().public_header
.sequence_number_length
);
1481 QuicPacketCreatorPeer::SetSequenceNumber(&peer_creator_
,
1482 100 * 256 * 256 * 256);
1484 SendStreamDataToPeer(1, "foo", 12, !kFin
, &last_packet
);
1485 EXPECT_EQ(PACKET_6BYTE_SEQUENCE_NUMBER
,
1486 QuicPacketCreatorPeer::NextSequenceNumberLength(creator_
));
1487 EXPECT_EQ(PACKET_4BYTE_SEQUENCE_NUMBER
,
1488 writer_
->header().public_header
.sequence_number_length
);
1491 TEST_P(QuicConnectionTest
, BasicSending
) {
1492 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
1493 QuicPacketSequenceNumber last_packet
;
1494 SendStreamDataToPeer(1, "foo", 0, !kFin
, &last_packet
); // Packet 1
1495 EXPECT_EQ(1u, last_packet
);
1496 SendAckPacketToPeer(); // Packet 2
1498 EXPECT_EQ(1u, least_unacked());
1500 SendAckPacketToPeer(); // Packet 3
1501 EXPECT_EQ(1u, least_unacked());
1503 SendStreamDataToPeer(1, "bar", 3, !kFin
, &last_packet
); // Packet 4
1504 EXPECT_EQ(4u, last_packet
);
1505 SendAckPacketToPeer(); // Packet 5
1506 EXPECT_EQ(1u, least_unacked());
1508 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
1510 // Peer acks up to packet 3.
1511 QuicAckFrame frame
= InitAckFrame(3);
1512 ProcessAckPacket(&frame
);
1513 SendAckPacketToPeer(); // Packet 6
1515 // As soon as we've acked one, we skip ack packets 2 and 3 and note lack of
1517 EXPECT_EQ(4u, least_unacked());
1519 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
1521 // Peer acks up to packet 4, the last packet.
1522 QuicAckFrame frame2
= InitAckFrame(6);
1523 ProcessAckPacket(&frame2
); // Acks don't instigate acks.
1525 // Verify that we did not send an ack.
1526 EXPECT_EQ(6u, writer_
->header().packet_sequence_number
);
1528 // So the last ack has not changed.
1529 EXPECT_EQ(4u, least_unacked());
1531 // If we force an ack, we shouldn't change our retransmit state.
1532 SendAckPacketToPeer(); // Packet 7
1533 EXPECT_EQ(7u, least_unacked());
1535 // But if we send more data it should.
1536 SendStreamDataToPeer(1, "eep", 6, !kFin
, &last_packet
); // Packet 8
1537 EXPECT_EQ(8u, last_packet
);
1538 SendAckPacketToPeer(); // Packet 9
1539 EXPECT_EQ(7u, least_unacked());
1542 // QuicConnection should record the the packet sent-time prior to sending the
1544 TEST_P(QuicConnectionTest
, RecordSentTimeBeforePacketSent
) {
1545 // We're using a MockClock for the tests, so we have complete control over the
1547 // Our recorded timestamp for the last packet sent time will be passed in to
1548 // the send_algorithm. Make sure that it is set to the correct value.
1549 QuicTime actual_recorded_send_time
= QuicTime::Zero();
1550 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
))
1551 .WillOnce(DoAll(SaveArg
<0>(&actual_recorded_send_time
), Return(true)));
1553 // First send without any pause and check the result.
1554 QuicTime expected_recorded_send_time
= clock_
.Now();
1555 connection_
.SendStreamDataWithString(1, "foo", 0, !kFin
, nullptr);
1556 EXPECT_EQ(expected_recorded_send_time
, actual_recorded_send_time
)
1557 << "Expected time = " << expected_recorded_send_time
.ToDebuggingValue()
1558 << ". Actual time = " << actual_recorded_send_time
.ToDebuggingValue();
1560 // Now pause during the write, and check the results.
1561 actual_recorded_send_time
= QuicTime::Zero();
1562 const QuicTime::Delta write_pause_time_delta
=
1563 QuicTime::Delta::FromMilliseconds(5000);
1564 SetWritePauseTimeDelta(write_pause_time_delta
);
1565 expected_recorded_send_time
= clock_
.Now();
1567 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
))
1568 .WillOnce(DoAll(SaveArg
<0>(&actual_recorded_send_time
), Return(true)));
1569 connection_
.SendStreamDataWithString(2, "baz", 0, !kFin
, nullptr);
1570 EXPECT_EQ(expected_recorded_send_time
, actual_recorded_send_time
)
1571 << "Expected time = " << expected_recorded_send_time
.ToDebuggingValue()
1572 << ". Actual time = " << actual_recorded_send_time
.ToDebuggingValue();
1575 TEST_P(QuicConnectionTest
, FECSending
) {
1576 // All packets carry version info till version is negotiated.
1577 size_t payload_length
;
1578 // GetPacketLengthForOneStream() assumes a stream offset of 0 in determining
1579 // packet length. The size of the offset field in a stream frame is 0 for
1580 // offset 0, and 2 for non-zero offsets up through 64K. Increase
1581 // max_packet_length by 2 so that subsequent packets containing subsequent
1582 // stream frames with non-zero offets will fit within the packet length.
1583 size_t length
= 2 + GetPacketLengthForOneStream(
1584 connection_
.version(), kIncludeVersion
,
1585 PACKET_8BYTE_CONNECTION_ID
, PACKET_1BYTE_SEQUENCE_NUMBER
,
1586 IN_FEC_GROUP
, &payload_length
);
1587 creator_
->SetMaxPacketLength(length
);
1589 // Send 4 protected data packets, which should also trigger 1 FEC packet.
1590 EXPECT_CALL(*send_algorithm_
,
1591 OnPacketSent(_
, _
, _
, _
, HAS_RETRANSMITTABLE_DATA
)).Times(5);
1592 // The first stream frame will have 2 fewer overhead bytes than the other 3.
1593 const string
payload(payload_length
* 4 + 2, 'a');
1594 connection_
.SendStreamDataWithStringWithFec(1, payload
, 0, !kFin
, nullptr);
1595 // Expect the FEC group to be closed after SendStreamDataWithString.
1596 EXPECT_FALSE(creator_
->IsFecGroupOpen());
1597 EXPECT_FALSE(creator_
->IsFecProtected());
1600 TEST_P(QuicConnectionTest
, FECQueueing
) {
1601 // All packets carry version info till version is negotiated.
1602 size_t payload_length
;
1603 size_t length
= GetPacketLengthForOneStream(
1604 connection_
.version(), kIncludeVersion
,
1605 PACKET_8BYTE_CONNECTION_ID
, PACKET_1BYTE_SEQUENCE_NUMBER
,
1606 IN_FEC_GROUP
, &payload_length
);
1607 creator_
->SetMaxPacketLength(length
);
1608 EXPECT_TRUE(creator_
->IsFecEnabled());
1610 EXPECT_EQ(0u, connection_
.NumQueuedPackets());
1612 const string
payload(payload_length
, 'a');
1613 connection_
.SendStreamDataWithStringWithFec(1, payload
, 0, !kFin
, nullptr);
1614 EXPECT_FALSE(creator_
->IsFecGroupOpen());
1615 EXPECT_FALSE(creator_
->IsFecProtected());
1616 // Expect the first data packet and the fec packet to be queued.
1617 EXPECT_EQ(2u, connection_
.NumQueuedPackets());
1620 TEST_P(QuicConnectionTest
, FECAlarmStoppedWhenFECPacketSent
) {
1621 EXPECT_TRUE(creator_
->IsFecEnabled());
1622 EXPECT_EQ(0u, QuicSentPacketManagerPeer::GetBytesInFlight(manager_
));
1623 EXPECT_FALSE(connection_
.GetFecAlarm()->IsSet());
1625 creator_
->set_max_packets_per_fec_group(2);
1627 // 1 Data packet. FEC alarm should be set.
1628 EXPECT_CALL(*send_algorithm_
,
1629 OnPacketSent(_
, _
, 1u, _
, HAS_RETRANSMITTABLE_DATA
)).Times(1);
1630 connection_
.SendStreamDataWithStringWithFec(3, "foo", 0, true, nullptr);
1631 EXPECT_TRUE(connection_
.GetFecAlarm()->IsSet());
1633 // Second data packet triggers FEC packet out. FEC alarm should not be set.
1634 EXPECT_CALL(*send_algorithm_
,
1635 OnPacketSent(_
, _
, _
, _
, HAS_RETRANSMITTABLE_DATA
)).Times(2);
1636 connection_
.SendStreamDataWithStringWithFec(5, "foo", 0, true, nullptr);
1637 EXPECT_TRUE(writer_
->header().fec_flag
);
1638 EXPECT_FALSE(connection_
.GetFecAlarm()->IsSet());
1641 TEST_P(QuicConnectionTest
, FECAlarmStoppedOnConnectionClose
) {
1642 EXPECT_TRUE(creator_
->IsFecEnabled());
1643 EXPECT_FALSE(connection_
.GetFecAlarm()->IsSet());
1644 creator_
->set_max_packets_per_fec_group(100);
1646 // 1 Data packet. FEC alarm should be set.
1647 EXPECT_CALL(*send_algorithm_
,
1648 OnPacketSent(_
, _
, 1u, _
, HAS_RETRANSMITTABLE_DATA
)).Times(1);
1649 connection_
.SendStreamDataWithStringWithFec(3, "foo", 0, kFin
, nullptr);
1650 EXPECT_TRUE(connection_
.GetFecAlarm()->IsSet());
1652 EXPECT_CALL(visitor_
, OnConnectionClosed(QUIC_NO_ERROR
, false));
1653 // Closing connection should stop the FEC alarm.
1654 connection_
.CloseConnection(QUIC_NO_ERROR
, /*from_peer=*/false);
1655 EXPECT_FALSE(connection_
.GetFecAlarm()->IsSet());
1658 TEST_P(QuicConnectionTest
, RemoveFECFromInflightOnRetransmissionTimeout
) {
1659 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
1660 EXPECT_TRUE(creator_
->IsFecEnabled());
1661 EXPECT_EQ(0u, QuicSentPacketManagerPeer::GetBytesInFlight(manager_
));
1662 EXPECT_FALSE(connection_
.GetFecAlarm()->IsSet());
1664 // 1 Data packet. FEC alarm should be set.
1665 EXPECT_CALL(*send_algorithm_
,
1666 OnPacketSent(_
, _
, 1u, _
, HAS_RETRANSMITTABLE_DATA
)).Times(1);
1667 connection_
.SendStreamDataWithStringWithFec(3, "foo", 0, !kFin
, nullptr);
1668 EXPECT_TRUE(connection_
.GetFecAlarm()->IsSet());
1669 size_t protected_packet
=
1670 QuicSentPacketManagerPeer::GetBytesInFlight(manager_
);
1672 // Force FEC timeout to send FEC packet out.
1673 EXPECT_CALL(*send_algorithm_
,
1674 OnPacketSent(_
, _
, 2u, _
, HAS_RETRANSMITTABLE_DATA
)).Times(1);
1675 connection_
.GetFecAlarm()->Fire();
1676 EXPECT_TRUE(writer_
->header().fec_flag
);
1678 size_t fec_packet
= protected_packet
;
1679 EXPECT_EQ(protected_packet
+ fec_packet
,
1680 QuicSentPacketManagerPeer::GetBytesInFlight(manager_
));
1681 clock_
.AdvanceTime(DefaultRetransmissionTime());
1683 // On RTO, both data and FEC packets are removed from inflight, only the data
1684 // packet is retransmitted, and this retransmission (but not FEC) gets added
1685 // back into the inflight.
1686 EXPECT_CALL(*send_algorithm_
, OnRetransmissionTimeout(true));
1687 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
)).Times(1);
1688 connection_
.GetRetransmissionAlarm()->Fire();
1690 // The retransmission of packet 1 will be 3 bytes smaller than packet 1, since
1691 // the first transmission will have 1 byte for FEC group number and 2 bytes of
1692 // stream frame size, which are absent in the retransmission.
1693 size_t retransmitted_packet
= protected_packet
- 3;
1694 EXPECT_EQ(protected_packet
+ retransmitted_packet
,
1695 QuicSentPacketManagerPeer::GetBytesInFlight(manager_
));
1696 EXPECT_FALSE(connection_
.GetFecAlarm()->IsSet());
1698 // Receive ack for the retransmission. No data should be outstanding.
1699 QuicAckFrame ack
= InitAckFrame(3);
1700 NackPacket(1, &ack
);
1701 NackPacket(2, &ack
);
1702 SequenceNumberSet lost_packets
;
1703 lost_packets
.insert(1);
1704 EXPECT_CALL(*loss_algorithm_
, DetectLostPackets(_
, _
, _
, _
))
1705 .WillOnce(Return(lost_packets
));
1706 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
1707 ProcessAckPacket(&ack
);
1709 // Ensure the alarm is not set since all packets have been acked or abandoned.
1710 EXPECT_FALSE(connection_
.GetRetransmissionAlarm()->IsSet());
1711 EXPECT_EQ(0u, QuicSentPacketManagerPeer::GetBytesInFlight(manager_
));
1714 TEST_P(QuicConnectionTest
, RemoveFECFromInflightOnLossRetransmission
) {
1715 EXPECT_TRUE(creator_
->IsFecEnabled());
1716 EXPECT_FALSE(connection_
.GetFecAlarm()->IsSet());
1718 // 1 FEC-protected data packet. FEC alarm should be set.
1719 EXPECT_CALL(*send_algorithm_
,
1720 OnPacketSent(_
, _
, _
, _
, HAS_RETRANSMITTABLE_DATA
)).Times(1);
1721 connection_
.SendStreamDataWithStringWithFec(3, "foo", 0, kFin
, nullptr);
1722 EXPECT_TRUE(connection_
.GetFecAlarm()->IsSet());
1723 size_t protected_packet
=
1724 QuicSentPacketManagerPeer::GetBytesInFlight(manager_
);
1726 // Force FEC timeout to send FEC packet out.
1727 EXPECT_CALL(*send_algorithm_
,
1728 OnPacketSent(_
, _
, _
, _
, HAS_RETRANSMITTABLE_DATA
)).Times(1);
1729 connection_
.GetFecAlarm()->Fire();
1730 EXPECT_TRUE(writer_
->header().fec_flag
);
1731 size_t fec_packet
= protected_packet
;
1732 EXPECT_EQ(protected_packet
+ fec_packet
,
1733 QuicSentPacketManagerPeer::GetBytesInFlight(manager_
));
1735 // Send more data to trigger NACKs. Note that all data starts at stream offset
1736 // 0 to ensure the same packet size, for ease of testing.
1737 EXPECT_CALL(*send_algorithm_
,
1738 OnPacketSent(_
, _
, _
, _
, HAS_RETRANSMITTABLE_DATA
)).Times(4);
1739 connection_
.SendStreamDataWithString(5, "foo", 0, kFin
, nullptr);
1740 connection_
.SendStreamDataWithString(7, "foo", 0, kFin
, nullptr);
1741 connection_
.SendStreamDataWithString(9, "foo", 0, kFin
, nullptr);
1742 connection_
.SendStreamDataWithString(11, "foo", 0, kFin
, nullptr);
1744 // An unprotected packet will be 3 bytes smaller than an FEC-protected packet,
1745 // since the protected packet will have 1 byte for FEC group number and
1746 // 2 bytes of stream frame size, which are absent in the unprotected packet.
1747 size_t unprotected_packet
= protected_packet
- 3;
1748 EXPECT_EQ(protected_packet
+ fec_packet
+ 4 * unprotected_packet
,
1749 QuicSentPacketManagerPeer::GetBytesInFlight(manager_
));
1750 EXPECT_FALSE(connection_
.GetFecAlarm()->IsSet());
1752 // Ack data packets, and NACK FEC packet and one data packet. Triggers
1753 // NACK-based loss detection of both packets, but only data packet is
1754 // retransmitted and considered oustanding.
1755 QuicAckFrame ack
= InitAckFrame(6);
1756 NackPacket(2, &ack
);
1757 NackPacket(3, &ack
);
1758 SequenceNumberSet lost_packets
;
1759 lost_packets
.insert(2);
1760 lost_packets
.insert(3);
1761 EXPECT_CALL(*loss_algorithm_
, DetectLostPackets(_
, _
, _
, _
))
1762 .WillOnce(Return(lost_packets
));
1763 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
1764 EXPECT_CALL(*send_algorithm_
,
1765 OnPacketSent(_
, _
, _
, _
, HAS_RETRANSMITTABLE_DATA
)).Times(1);
1766 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
1767 ProcessAckPacket(&ack
);
1768 // On receiving this ack from the server, the client will no longer send
1769 // version number in subsequent packets, including in this retransmission.
1770 size_t unprotected_packet_no_version
= unprotected_packet
- 4;
1771 EXPECT_EQ(unprotected_packet_no_version
,
1772 QuicSentPacketManagerPeer::GetBytesInFlight(manager_
));
1774 // Receive ack for the retransmission. No data should be outstanding.
1775 QuicAckFrame ack2
= InitAckFrame(7);
1776 NackPacket(2, &ack2
);
1777 NackPacket(3, &ack2
);
1778 SequenceNumberSet lost_packets2
;
1779 EXPECT_CALL(*loss_algorithm_
, DetectLostPackets(_
, _
, _
, _
))
1780 .WillOnce(Return(lost_packets2
));
1781 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
1782 ProcessAckPacket(&ack2
);
1783 EXPECT_EQ(0u, QuicSentPacketManagerPeer::GetBytesInFlight(manager_
));
1786 TEST_P(QuicConnectionTest
, FECRemainsInflightOnTLPOfEarlierData
) {
1787 // This test checks if TLP is sent correctly when a data and an FEC packet
1788 // are outstanding. TLP should be sent for the data packet when the
1789 // retransmission alarm fires.
1790 // Turn on TLP for this test.
1791 QuicSentPacketManagerPeer::SetMaxTailLossProbes(manager_
, 1);
1792 EXPECT_TRUE(creator_
->IsFecEnabled());
1793 EXPECT_EQ(0u, QuicSentPacketManagerPeer::GetBytesInFlight(manager_
));
1794 EXPECT_FALSE(connection_
.GetFecAlarm()->IsSet());
1796 // 1 Data packet. FEC alarm should be set.
1797 EXPECT_CALL(*send_algorithm_
,
1798 OnPacketSent(_
, _
, 1u, _
, HAS_RETRANSMITTABLE_DATA
)).Times(1);
1799 connection_
.SendStreamDataWithStringWithFec(3, "foo", 0, kFin
, nullptr);
1800 EXPECT_TRUE(connection_
.GetFecAlarm()->IsSet());
1801 size_t protected_packet
=
1802 QuicSentPacketManagerPeer::GetBytesInFlight(manager_
);
1803 EXPECT_LT(0u, protected_packet
);
1805 // Force FEC timeout to send FEC packet out.
1806 EXPECT_CALL(*send_algorithm_
,
1807 OnPacketSent(_
, _
, 2u, _
, HAS_RETRANSMITTABLE_DATA
)).Times(1);
1808 connection_
.GetFecAlarm()->Fire();
1809 EXPECT_TRUE(writer_
->header().fec_flag
);
1810 size_t fec_packet
= protected_packet
;
1811 EXPECT_EQ(protected_packet
+ fec_packet
,
1812 QuicSentPacketManagerPeer::GetBytesInFlight(manager_
));
1814 // TLP alarm should be set.
1815 QuicTime retransmission_time
=
1816 connection_
.GetRetransmissionAlarm()->deadline();
1817 EXPECT_NE(QuicTime::Zero(), retransmission_time
);
1818 // Simulate the retransmission alarm firing and sending a TLP, so send
1819 // algorithm's OnRetransmissionTimeout is not called.
1820 clock_
.AdvanceTime(retransmission_time
.Subtract(clock_
.Now()));
1821 EXPECT_CALL(*send_algorithm_
,
1822 OnPacketSent(_
, _
, 3u, _
, HAS_RETRANSMITTABLE_DATA
)).Times(1);
1823 connection_
.GetRetransmissionAlarm()->Fire();
1824 // The TLP retransmission of packet 1 will be 3 bytes smaller than packet 1,
1825 // since packet 1 will have 1 byte for FEC group number and 2 bytes of stream
1826 // frame size, which are absent in the the TLP retransmission.
1827 size_t tlp_packet
= protected_packet
- 3;
1828 EXPECT_EQ(protected_packet
+ fec_packet
+ tlp_packet
,
1829 QuicSentPacketManagerPeer::GetBytesInFlight(manager_
));
1832 TEST_P(QuicConnectionTest
, FECRemainsInflightOnTLPOfLaterData
) {
1833 // Tests if TLP is sent correctly when data packet 1 and an FEC packet are
1834 // sent followed by data packet 2, and data packet 1 is acked. TLP should be
1835 // sent for data packet 2 when the retransmission alarm fires. Turn on TLP for
1837 QuicSentPacketManagerPeer::SetMaxTailLossProbes(manager_
, 1);
1838 EXPECT_TRUE(creator_
->IsFecEnabled());
1839 EXPECT_EQ(0u, QuicSentPacketManagerPeer::GetBytesInFlight(manager_
));
1840 EXPECT_FALSE(connection_
.GetFecAlarm()->IsSet());
1842 // 1 Data packet. FEC alarm should be set.
1843 EXPECT_CALL(*send_algorithm_
,
1844 OnPacketSent(_
, _
, 1u, _
, HAS_RETRANSMITTABLE_DATA
)).Times(1);
1845 connection_
.SendStreamDataWithStringWithFec(3, "foo", 0, kFin
, nullptr);
1846 EXPECT_TRUE(connection_
.GetFecAlarm()->IsSet());
1847 size_t protected_packet
=
1848 QuicSentPacketManagerPeer::GetBytesInFlight(manager_
);
1849 EXPECT_LT(0u, protected_packet
);
1851 // Force FEC timeout to send FEC packet out.
1852 EXPECT_CALL(*send_algorithm_
,
1853 OnPacketSent(_
, _
, 2u, _
, HAS_RETRANSMITTABLE_DATA
)).Times(1);
1854 connection_
.GetFecAlarm()->Fire();
1855 EXPECT_TRUE(writer_
->header().fec_flag
);
1856 // Protected data packet and FEC packet oustanding.
1857 size_t fec_packet
= protected_packet
;
1858 EXPECT_EQ(protected_packet
+ fec_packet
,
1859 QuicSentPacketManagerPeer::GetBytesInFlight(manager_
));
1861 // Send 1 unprotected data packet. No FEC alarm should be set.
1862 EXPECT_CALL(*send_algorithm_
,
1863 OnPacketSent(_
, _
, 3u, _
, HAS_RETRANSMITTABLE_DATA
)).Times(1);
1864 connection_
.SendStreamDataWithString(5, "foo", 0, kFin
, nullptr);
1865 EXPECT_FALSE(connection_
.GetFecAlarm()->IsSet());
1866 // Protected data packet, FEC packet, and unprotected data packet oustanding.
1867 // An unprotected packet will be 3 bytes smaller than an FEC-protected packet,
1868 // since the protected packet will have 1 byte for FEC group number and
1869 // 2 bytes of stream frame size, which are absent in the unprotected packet.
1870 size_t unprotected_packet
= protected_packet
- 3;
1871 EXPECT_EQ(protected_packet
+ fec_packet
+ unprotected_packet
,
1872 QuicSentPacketManagerPeer::GetBytesInFlight(manager_
));
1874 // Receive ack for first data packet. FEC and second data packet are still
1876 QuicAckFrame ack
= InitAckFrame(1);
1877 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
1878 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
1879 ProcessAckPacket(&ack
);
1880 // FEC packet and unprotected data packet oustanding.
1881 EXPECT_EQ(fec_packet
+ unprotected_packet
,
1882 QuicSentPacketManagerPeer::GetBytesInFlight(manager_
));
1884 // TLP alarm should be set.
1885 QuicTime retransmission_time
=
1886 connection_
.GetRetransmissionAlarm()->deadline();
1887 EXPECT_NE(QuicTime::Zero(), retransmission_time
);
1888 // Simulate the retransmission alarm firing and sending a TLP, so send
1889 // algorithm's OnRetransmissionTimeout is not called.
1890 clock_
.AdvanceTime(retransmission_time
.Subtract(clock_
.Now()));
1891 EXPECT_CALL(*send_algorithm_
,
1892 OnPacketSent(_
, _
, 4u, _
, HAS_RETRANSMITTABLE_DATA
)).Times(1);
1893 connection_
.GetRetransmissionAlarm()->Fire();
1895 // Having received an ack from the server, the client will no longer send
1896 // version number in subsequent packets, including in this retransmission.
1897 size_t tlp_packet_no_version
= unprotected_packet
- 4;
1898 EXPECT_EQ(fec_packet
+ unprotected_packet
+ tlp_packet_no_version
,
1899 QuicSentPacketManagerPeer::GetBytesInFlight(manager_
));
1902 TEST_P(QuicConnectionTest
, NoTLPForFECPacket
) {
1903 // Turn on TLP for this test.
1904 QuicSentPacketManagerPeer::SetMaxTailLossProbes(manager_
, 1);
1905 EXPECT_TRUE(creator_
->IsFecEnabled());
1906 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
1908 // Send 1 FEC-protected data packet. FEC alarm should be set.
1909 EXPECT_CALL(*send_algorithm_
,
1910 OnPacketSent(_
, _
, _
, _
, HAS_RETRANSMITTABLE_DATA
)).Times(1);
1911 connection_
.SendStreamDataWithStringWithFec(3, "foo", 0, !kFin
, nullptr);
1912 EXPECT_TRUE(connection_
.GetFecAlarm()->IsSet());
1913 // Force FEC timeout to send FEC packet out.
1914 EXPECT_CALL(*send_algorithm_
,
1915 OnPacketSent(_
, _
, _
, _
, HAS_RETRANSMITTABLE_DATA
)).Times(1);
1916 connection_
.GetFecAlarm()->Fire();
1917 EXPECT_TRUE(writer_
->header().fec_flag
);
1919 // Ack data packet, but not FEC packet.
1920 QuicAckFrame ack
= InitAckFrame(1);
1921 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
1922 ProcessAckPacket(&ack
);
1924 // No TLP alarm for FEC, but retransmission alarm should be set for an RTO.
1925 EXPECT_LT(0u, QuicSentPacketManagerPeer::GetBytesInFlight(manager_
));
1926 EXPECT_TRUE(connection_
.GetRetransmissionAlarm()->IsSet());
1927 QuicTime rto_time
= connection_
.GetRetransmissionAlarm()->deadline();
1928 EXPECT_NE(QuicTime::Zero(), rto_time
);
1930 // Simulate the retransmission alarm firing. FEC packet is no longer
1932 clock_
.AdvanceTime(rto_time
.Subtract(clock_
.Now()));
1933 connection_
.GetRetransmissionAlarm()->Fire();
1935 EXPECT_FALSE(connection_
.GetRetransmissionAlarm()->IsSet());
1936 EXPECT_EQ(0u, QuicSentPacketManagerPeer::GetBytesInFlight(manager_
));
1939 TEST_P(QuicConnectionTest
, FramePacking
) {
1940 CongestionBlockWrites();
1942 // Send an ack and two stream frames in 1 packet by queueing them.
1943 connection_
.SendAck();
1944 EXPECT_CALL(visitor_
, OnCanWrite()).WillOnce(DoAll(
1945 IgnoreResult(InvokeWithoutArgs(&connection_
,
1946 &TestConnection::SendStreamData3
)),
1947 IgnoreResult(InvokeWithoutArgs(&connection_
,
1948 &TestConnection::SendStreamData5
))));
1950 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
)).Times(1);
1951 CongestionUnblockWrites();
1952 connection_
.GetSendAlarm()->Fire();
1953 EXPECT_EQ(0u, connection_
.NumQueuedPackets());
1954 EXPECT_FALSE(connection_
.HasQueuedData());
1956 // Parse the last packet and ensure it's an ack and two stream frames from
1957 // two different streams.
1958 EXPECT_EQ(4u, writer_
->frame_count());
1959 EXPECT_FALSE(writer_
->stop_waiting_frames().empty());
1960 EXPECT_FALSE(writer_
->ack_frames().empty());
1961 ASSERT_EQ(2u, writer_
->stream_frames().size());
1962 EXPECT_EQ(kClientDataStreamId1
, writer_
->stream_frames()[0].stream_id
);
1963 EXPECT_EQ(kClientDataStreamId2
, writer_
->stream_frames()[1].stream_id
);
1966 TEST_P(QuicConnectionTest
, FramePackingNonCryptoThenCrypto
) {
1967 CongestionBlockWrites();
1969 // Send an ack and two stream frames (one non-crypto, then one crypto) in 2
1970 // packets by queueing them.
1971 connection_
.SendAck();
1972 EXPECT_CALL(visitor_
, OnCanWrite()).WillOnce(DoAll(
1973 IgnoreResult(InvokeWithoutArgs(&connection_
,
1974 &TestConnection::SendStreamData3
)),
1975 IgnoreResult(InvokeWithoutArgs(&connection_
,
1976 &TestConnection::SendCryptoStreamData
))));
1978 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
)).Times(2);
1979 CongestionUnblockWrites();
1980 connection_
.GetSendAlarm()->Fire();
1981 EXPECT_EQ(0u, connection_
.NumQueuedPackets());
1982 EXPECT_FALSE(connection_
.HasQueuedData());
1984 // Parse the last packet and ensure it's the crypto stream frame.
1985 EXPECT_EQ(1u, writer_
->frame_count());
1986 ASSERT_EQ(1u, writer_
->stream_frames().size());
1987 EXPECT_EQ(kCryptoStreamId
, writer_
->stream_frames()[0].stream_id
);
1990 TEST_P(QuicConnectionTest
, FramePackingCryptoThenNonCrypto
) {
1991 CongestionBlockWrites();
1993 // Send an ack and two stream frames (one crypto, then one non-crypto) in 2
1994 // packets by queueing them.
1995 connection_
.SendAck();
1996 EXPECT_CALL(visitor_
, OnCanWrite()).WillOnce(DoAll(
1997 IgnoreResult(InvokeWithoutArgs(&connection_
,
1998 &TestConnection::SendCryptoStreamData
)),
1999 IgnoreResult(InvokeWithoutArgs(&connection_
,
2000 &TestConnection::SendStreamData3
))));
2002 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
)).Times(2);
2003 CongestionUnblockWrites();
2004 connection_
.GetSendAlarm()->Fire();
2005 EXPECT_EQ(0u, connection_
.NumQueuedPackets());
2006 EXPECT_FALSE(connection_
.HasQueuedData());
2008 // Parse the last packet and ensure it's the stream frame from stream 3.
2009 EXPECT_EQ(1u, writer_
->frame_count());
2010 ASSERT_EQ(1u, writer_
->stream_frames().size());
2011 EXPECT_EQ(kClientDataStreamId1
, writer_
->stream_frames()[0].stream_id
);
2014 TEST_P(QuicConnectionTest
, FramePackingFEC
) {
2015 EXPECT_TRUE(creator_
->IsFecEnabled());
2017 CongestionBlockWrites();
2019 // Queue an ack and two stream frames. Ack gets flushed when FEC is turned on
2020 // for sending protected data; two stream frames are packed in 1 packet.
2021 EXPECT_CALL(visitor_
, OnCanWrite()).WillOnce(DoAll(
2022 IgnoreResult(InvokeWithoutArgs(
2023 &connection_
, &TestConnection::SendStreamData3WithFec
)),
2024 IgnoreResult(InvokeWithoutArgs(
2025 &connection_
, &TestConnection::SendStreamData5WithFec
))));
2026 connection_
.SendAck();
2028 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
)).Times(2);
2029 CongestionUnblockWrites();
2030 connection_
.GetSendAlarm()->Fire();
2031 EXPECT_EQ(0u, connection_
.NumQueuedPackets());
2032 EXPECT_FALSE(connection_
.HasQueuedData());
2034 // Parse the last packet and ensure it's in an fec group.
2035 EXPECT_EQ(2u, writer_
->header().fec_group
);
2036 EXPECT_EQ(2u, writer_
->frame_count());
2038 // FEC alarm should be set.
2039 EXPECT_TRUE(connection_
.GetFecAlarm()->IsSet());
2042 TEST_P(QuicConnectionTest
, FramePackingAckResponse
) {
2043 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
2044 // Process a data packet to queue up a pending ack.
2045 EXPECT_CALL(visitor_
, OnStreamFrames(_
)).Times(1);
2046 ProcessDataPacket(1, 1, kEntropyFlag
);
2048 EXPECT_CALL(visitor_
, OnCanWrite()).WillOnce(DoAll(
2049 IgnoreResult(InvokeWithoutArgs(&connection_
,
2050 &TestConnection::SendStreamData3
)),
2051 IgnoreResult(InvokeWithoutArgs(&connection_
,
2052 &TestConnection::SendStreamData5
))));
2054 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
)).Times(1);
2056 // Process an ack to cause the visitor's OnCanWrite to be invoked.
2057 QuicPacketCreatorPeer::SetSequenceNumber(&peer_creator_
, 2);
2058 QuicAckFrame ack_one
= InitAckFrame(0);
2059 ProcessAckPacket(&ack_one
);
2061 EXPECT_EQ(0u, connection_
.NumQueuedPackets());
2062 EXPECT_FALSE(connection_
.HasQueuedData());
2064 // Parse the last packet and ensure it's an ack and two stream frames from
2065 // two different streams.
2066 EXPECT_EQ(4u, writer_
->frame_count());
2067 EXPECT_FALSE(writer_
->stop_waiting_frames().empty());
2068 EXPECT_FALSE(writer_
->ack_frames().empty());
2069 ASSERT_EQ(2u, writer_
->stream_frames().size());
2070 EXPECT_EQ(kClientDataStreamId1
, writer_
->stream_frames()[0].stream_id
);
2071 EXPECT_EQ(kClientDataStreamId2
, writer_
->stream_frames()[1].stream_id
);
2074 TEST_P(QuicConnectionTest
, FramePackingSendv
) {
2075 // Send data in 1 packet by writing multiple blocks in a single iovector
2077 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
));
2079 char data
[] = "ABCD";
2081 data_iov
.AppendNoCoalesce(data
, 2);
2082 data_iov
.AppendNoCoalesce(data
+ 2, 2);
2083 connection_
.SendStreamData(1, data_iov
, 0, !kFin
, MAY_FEC_PROTECT
, nullptr);
2085 EXPECT_EQ(0u, connection_
.NumQueuedPackets());
2086 EXPECT_FALSE(connection_
.HasQueuedData());
2088 // Parse the last packet and ensure multiple iovector blocks have
2089 // been packed into a single stream frame from one stream.
2090 EXPECT_EQ(1u, writer_
->frame_count());
2091 EXPECT_EQ(1u, writer_
->stream_frames().size());
2092 QuicStreamFrame frame
= writer_
->stream_frames()[0];
2093 EXPECT_EQ(1u, frame
.stream_id
);
2094 EXPECT_EQ("ABCD", string(static_cast<char*>
2095 (frame
.data
.iovec()[0].iov_base
),
2096 (frame
.data
.iovec()[0].iov_len
)));
2099 TEST_P(QuicConnectionTest
, FramePackingSendvQueued
) {
2100 // Try to send two stream frames in 1 packet by using writev.
2101 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
));
2104 char data
[] = "ABCD";
2106 data_iov
.AppendNoCoalesce(data
, 2);
2107 data_iov
.AppendNoCoalesce(data
+ 2, 2);
2108 connection_
.SendStreamData(1, data_iov
, 0, !kFin
, MAY_FEC_PROTECT
, nullptr);
2110 EXPECT_EQ(1u, connection_
.NumQueuedPackets());
2111 EXPECT_TRUE(connection_
.HasQueuedData());
2113 // Unblock the writes and actually send.
2114 writer_
->SetWritable();
2115 connection_
.OnCanWrite();
2116 EXPECT_EQ(0u, connection_
.NumQueuedPackets());
2118 // Parse the last packet and ensure it's one stream frame from one stream.
2119 EXPECT_EQ(1u, writer_
->frame_count());
2120 EXPECT_EQ(1u, writer_
->stream_frames().size());
2121 EXPECT_EQ(1u, writer_
->stream_frames()[0].stream_id
);
2124 TEST_P(QuicConnectionTest
, SendingZeroBytes
) {
2125 // Send a zero byte write with a fin using writev.
2126 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
));
2128 connection_
.SendStreamData(1, empty_iov
, 0, kFin
, MAY_FEC_PROTECT
, nullptr);
2130 EXPECT_EQ(0u, connection_
.NumQueuedPackets());
2131 EXPECT_FALSE(connection_
.HasQueuedData());
2133 // Parse the last packet and ensure it's one stream frame from one stream.
2134 EXPECT_EQ(1u, writer_
->frame_count());
2135 EXPECT_EQ(1u, writer_
->stream_frames().size());
2136 EXPECT_EQ(1u, writer_
->stream_frames()[0].stream_id
);
2137 EXPECT_TRUE(writer_
->stream_frames()[0].fin
);
2140 TEST_P(QuicConnectionTest
, OnCanWrite
) {
2141 // Visitor's OnCanWrite will send data, but will have more pending writes.
2142 EXPECT_CALL(visitor_
, OnCanWrite()).WillOnce(DoAll(
2143 IgnoreResult(InvokeWithoutArgs(&connection_
,
2144 &TestConnection::SendStreamData3
)),
2145 IgnoreResult(InvokeWithoutArgs(&connection_
,
2146 &TestConnection::SendStreamData5
))));
2147 EXPECT_CALL(visitor_
, WillingAndAbleToWrite()).WillOnce(Return(true));
2148 EXPECT_CALL(*send_algorithm_
,
2149 TimeUntilSend(_
, _
, _
)).WillRepeatedly(
2150 testing::Return(QuicTime::Delta::Zero()));
2152 connection_
.OnCanWrite();
2154 // Parse the last packet and ensure it's the two stream frames from
2155 // two different streams.
2156 EXPECT_EQ(2u, writer_
->frame_count());
2157 EXPECT_EQ(2u, writer_
->stream_frames().size());
2158 EXPECT_EQ(kClientDataStreamId1
, writer_
->stream_frames()[0].stream_id
);
2159 EXPECT_EQ(kClientDataStreamId2
, writer_
->stream_frames()[1].stream_id
);
2162 TEST_P(QuicConnectionTest
, RetransmitOnNack
) {
2163 QuicPacketSequenceNumber last_packet
;
2164 QuicByteCount second_packet_size
;
2165 SendStreamDataToPeer(3, "foo", 0, !kFin
, &last_packet
); // Packet 1
2166 second_packet_size
=
2167 SendStreamDataToPeer(3, "foos", 3, !kFin
, &last_packet
); // Packet 2
2168 SendStreamDataToPeer(3, "fooos", 7, !kFin
, &last_packet
); // Packet 3
2170 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
2172 // Don't lose a packet on an ack, and nothing is retransmitted.
2173 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
2174 QuicAckFrame ack_one
= InitAckFrame(1);
2175 ProcessAckPacket(&ack_one
);
2177 // Lose a packet and ensure it triggers retransmission.
2178 QuicAckFrame nack_two
= InitAckFrame(3);
2179 NackPacket(2, &nack_two
);
2180 SequenceNumberSet lost_packets
;
2181 lost_packets
.insert(2);
2182 EXPECT_CALL(*loss_algorithm_
, DetectLostPackets(_
, _
, _
, _
))
2183 .WillOnce(Return(lost_packets
));
2184 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
2185 EXPECT_CALL(*send_algorithm_
,
2186 OnPacketSent(_
, _
, _
, second_packet_size
- kQuicVersionSize
, _
)).
2188 ProcessAckPacket(&nack_two
);
2191 TEST_P(QuicConnectionTest
, DoNotSendQueuedPacketForResetStream
) {
2192 // Block the connection to queue the packet.
2195 QuicStreamId stream_id
= 2;
2196 connection_
.SendStreamDataWithString(stream_id
, "foo", 0, !kFin
, nullptr);
2198 // Now that there is a queued packet, reset the stream.
2199 connection_
.SendRstStream(stream_id
, QUIC_STREAM_NO_ERROR
, 14);
2201 // Unblock the connection and verify that only the RST_STREAM is sent.
2202 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
)).Times(1);
2203 writer_
->SetWritable();
2204 connection_
.OnCanWrite();
2205 EXPECT_EQ(1u, writer_
->frame_count());
2206 EXPECT_EQ(1u, writer_
->rst_stream_frames().size());
2209 TEST_P(QuicConnectionTest
, DoNotRetransmitForResetStreamOnNack
) {
2210 QuicStreamId stream_id
= 2;
2211 QuicPacketSequenceNumber last_packet
;
2212 SendStreamDataToPeer(stream_id
, "foo", 0, !kFin
, &last_packet
);
2213 SendStreamDataToPeer(stream_id
, "foos", 3, !kFin
, &last_packet
);
2214 SendStreamDataToPeer(stream_id
, "fooos", 7, !kFin
, &last_packet
);
2216 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
)).Times(1);
2217 connection_
.SendRstStream(stream_id
, QUIC_STREAM_NO_ERROR
, 14);
2219 // Lose a packet and ensure it does not trigger retransmission.
2220 QuicAckFrame nack_two
= InitAckFrame(last_packet
);
2221 NackPacket(last_packet
- 1, &nack_two
);
2222 SequenceNumberSet lost_packets
;
2223 lost_packets
.insert(last_packet
- 1);
2224 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
2225 EXPECT_CALL(*loss_algorithm_
, DetectLostPackets(_
, _
, _
, _
))
2226 .WillOnce(Return(lost_packets
));
2227 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
2228 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
)).Times(0);
2229 ProcessAckPacket(&nack_two
);
2232 TEST_P(QuicConnectionTest
, DoNotRetransmitForResetStreamOnRTO
) {
2233 QuicStreamId stream_id
= 2;
2234 QuicPacketSequenceNumber last_packet
;
2235 SendStreamDataToPeer(stream_id
, "foo", 0, !kFin
, &last_packet
);
2237 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
)).Times(1);
2238 connection_
.SendRstStream(stream_id
, QUIC_STREAM_NO_ERROR
, 14);
2240 // Fire the RTO and verify that the RST_STREAM is resent, not stream data.
2241 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
)).Times(1);
2242 clock_
.AdvanceTime(DefaultRetransmissionTime());
2243 connection_
.GetRetransmissionAlarm()->Fire();
2244 EXPECT_EQ(1u, writer_
->frame_count());
2245 EXPECT_EQ(1u, writer_
->rst_stream_frames().size());
2246 EXPECT_EQ(stream_id
, writer_
->rst_stream_frames().front().stream_id
);
2249 TEST_P(QuicConnectionTest
, DoNotSendPendingRetransmissionForResetStream
) {
2250 QuicStreamId stream_id
= 2;
2251 QuicPacketSequenceNumber last_packet
;
2252 SendStreamDataToPeer(stream_id
, "foo", 0, !kFin
, &last_packet
);
2253 SendStreamDataToPeer(stream_id
, "foos", 3, !kFin
, &last_packet
);
2255 connection_
.SendStreamDataWithString(stream_id
, "fooos", 7, !kFin
, nullptr);
2257 // Lose a packet which will trigger a pending retransmission.
2258 QuicAckFrame ack
= InitAckFrame(last_packet
);
2259 NackPacket(last_packet
- 1, &ack
);
2260 SequenceNumberSet lost_packets
;
2261 lost_packets
.insert(last_packet
- 1);
2262 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
2263 EXPECT_CALL(*loss_algorithm_
, DetectLostPackets(_
, _
, _
, _
))
2264 .WillOnce(Return(lost_packets
));
2265 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
2266 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
)).Times(0);
2267 ProcessAckPacket(&ack
);
2269 connection_
.SendRstStream(stream_id
, QUIC_STREAM_NO_ERROR
, 14);
2271 // Unblock the connection and verify that the RST_STREAM is sent but not the
2272 // second data packet nor a retransmit.
2273 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
)).Times(1);
2274 writer_
->SetWritable();
2275 connection_
.OnCanWrite();
2276 EXPECT_EQ(1u, writer_
->frame_count());
2277 EXPECT_EQ(1u, writer_
->rst_stream_frames().size());
2278 EXPECT_EQ(stream_id
, writer_
->rst_stream_frames().front().stream_id
);
2281 TEST_P(QuicConnectionTest
, DiscardRetransmit
) {
2282 QuicPacketSequenceNumber last_packet
;
2283 SendStreamDataToPeer(1, "foo", 0, !kFin
, &last_packet
); // Packet 1
2284 SendStreamDataToPeer(1, "foos", 3, !kFin
, &last_packet
); // Packet 2
2285 SendStreamDataToPeer(1, "fooos", 7, !kFin
, &last_packet
); // Packet 3
2287 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
2289 // Instigate a loss with an ack.
2290 QuicAckFrame nack_two
= InitAckFrame(3);
2291 NackPacket(2, &nack_two
);
2292 // The first nack should trigger a fast retransmission, but we'll be
2293 // write blocked, so the packet will be queued.
2295 SequenceNumberSet lost_packets
;
2296 lost_packets
.insert(2);
2297 EXPECT_CALL(*loss_algorithm_
, DetectLostPackets(_
, _
, _
, _
))
2298 .WillOnce(Return(lost_packets
));
2299 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
2300 ProcessAckPacket(&nack_two
);
2301 EXPECT_EQ(1u, connection_
.NumQueuedPackets());
2303 // Now, ack the previous transmission.
2304 EXPECT_CALL(*loss_algorithm_
, DetectLostPackets(_
, _
, _
, _
))
2305 .WillOnce(Return(SequenceNumberSet()));
2306 QuicAckFrame ack_all
= InitAckFrame(3);
2307 ProcessAckPacket(&ack_all
);
2309 // Unblock the socket and attempt to send the queued packets. However,
2310 // since the previous transmission has been acked, we will not
2311 // send the retransmission.
2312 EXPECT_CALL(*send_algorithm_
,
2313 OnPacketSent(_
, _
, _
, _
, _
)).Times(0);
2315 writer_
->SetWritable();
2316 connection_
.OnCanWrite();
2318 EXPECT_EQ(0u, connection_
.NumQueuedPackets());
2321 TEST_P(QuicConnectionTest
, RetransmitNackedLargestObserved
) {
2322 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
2323 QuicPacketSequenceNumber largest_observed
;
2324 QuicByteCount packet_size
;
2325 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
))
2326 .WillOnce(DoAll(SaveArg
<2>(&largest_observed
), SaveArg
<3>(&packet_size
),
2328 connection_
.SendStreamDataWithString(3, "foo", 0, !kFin
, nullptr);
2330 QuicAckFrame frame
= InitAckFrame(1);
2331 NackPacket(largest_observed
, &frame
);
2332 // The first nack should retransmit the largest observed packet.
2333 SequenceNumberSet lost_packets
;
2334 lost_packets
.insert(1);
2335 EXPECT_CALL(*loss_algorithm_
, DetectLostPackets(_
, _
, _
, _
))
2336 .WillOnce(Return(lost_packets
));
2337 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
2338 EXPECT_CALL(*send_algorithm_
,
2339 OnPacketSent(_
, _
, _
, packet_size
- kQuicVersionSize
, _
));
2340 ProcessAckPacket(&frame
);
2343 TEST_P(QuicConnectionTest
, QueueAfterTwoRTOs
) {
2344 for (int i
= 0; i
< 10; ++i
) {
2345 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
)).Times(1);
2346 connection_
.SendStreamDataWithString(3, "foo", i
* 3, !kFin
, nullptr);
2349 // Block the writer and ensure they're queued.
2351 clock_
.AdvanceTime(DefaultRetransmissionTime());
2352 // Only one packet should be retransmitted.
2353 connection_
.GetRetransmissionAlarm()->Fire();
2354 EXPECT_TRUE(connection_
.HasQueuedData());
2356 // Unblock the writer.
2357 writer_
->SetWritable();
2358 clock_
.AdvanceTime(QuicTime::Delta::FromMicroseconds(
2359 2 * DefaultRetransmissionTime().ToMicroseconds()));
2360 // Retransmit already retransmitted packets event though the sequence number
2361 // greater than the largest observed.
2362 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
)).Times(2);
2363 connection_
.GetRetransmissionAlarm()->Fire();
2364 connection_
.OnCanWrite();
2367 TEST_P(QuicConnectionTest
, WriteBlockedThenSent
) {
2369 writer_
->set_is_write_blocked_data_buffered(true);
2370 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
)).Times(1);
2371 connection_
.SendStreamDataWithString(1, "foo", 0, !kFin
, nullptr);
2372 EXPECT_TRUE(connection_
.GetRetransmissionAlarm()->IsSet());
2374 writer_
->SetWritable();
2375 connection_
.OnCanWrite();
2376 EXPECT_TRUE(connection_
.GetRetransmissionAlarm()->IsSet());
2379 TEST_P(QuicConnectionTest
, RetransmitWriteBlockedAckedOriginalThenSent
) {
2380 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
2381 connection_
.SendStreamDataWithString(3, "foo", 0, !kFin
, nullptr);
2382 EXPECT_TRUE(connection_
.GetRetransmissionAlarm()->IsSet());
2385 writer_
->set_is_write_blocked_data_buffered(true);
2386 // Simulate the retransmission alarm firing.
2387 clock_
.AdvanceTime(DefaultRetransmissionTime());
2388 connection_
.GetRetransmissionAlarm()->Fire();
2390 // Ack the sent packet before the callback returns, which happens in
2391 // rare circumstances with write blocked sockets.
2392 QuicAckFrame ack
= InitAckFrame(1);
2393 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
2394 ProcessAckPacket(&ack
);
2396 writer_
->SetWritable();
2397 connection_
.OnCanWrite();
2398 // There is now a pending packet, but with no retransmittable frames.
2399 EXPECT_TRUE(connection_
.GetRetransmissionAlarm()->IsSet());
2400 EXPECT_FALSE(connection_
.sent_packet_manager().HasRetransmittableFrames(2));
2403 TEST_P(QuicConnectionTest
, AlarmsWhenWriteBlocked
) {
2404 // Block the connection.
2406 connection_
.SendStreamDataWithString(3, "foo", 0, !kFin
, nullptr);
2407 EXPECT_EQ(1u, writer_
->packets_write_attempts());
2408 EXPECT_TRUE(writer_
->IsWriteBlocked());
2410 // Set the send and resumption alarms. Fire the alarms and ensure they don't
2411 // attempt to write.
2412 connection_
.GetResumeWritesAlarm()->Set(clock_
.ApproximateNow());
2413 connection_
.GetSendAlarm()->Set(clock_
.ApproximateNow());
2414 connection_
.GetResumeWritesAlarm()->Fire();
2415 connection_
.GetSendAlarm()->Fire();
2416 EXPECT_TRUE(writer_
->IsWriteBlocked());
2417 EXPECT_EQ(1u, writer_
->packets_write_attempts());
2420 TEST_P(QuicConnectionTest
, NoLimitPacketsPerNack
) {
2421 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
2423 // Send packets 1 to 15.
2424 for (int i
= 0; i
< 15; ++i
) {
2425 SendStreamDataToPeer(1, "foo", offset
, !kFin
, nullptr);
2429 // Ack 15, nack 1-14.
2430 SequenceNumberSet lost_packets
;
2431 QuicAckFrame nack
= InitAckFrame(15);
2432 for (int i
= 1; i
< 15; ++i
) {
2433 NackPacket(i
, &nack
);
2434 lost_packets
.insert(i
);
2437 // 14 packets have been NACK'd and lost. In TCP cubic, PRR limits
2438 // the retransmission rate in the case of burst losses.
2439 EXPECT_CALL(*loss_algorithm_
, DetectLostPackets(_
, _
, _
, _
))
2440 .WillOnce(Return(lost_packets
));
2441 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
2442 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
)).Times(14);
2443 ProcessAckPacket(&nack
);
2446 // Test sending multiple acks from the connection to the session.
2447 TEST_P(QuicConnectionTest
, MultipleAcks
) {
2448 QuicPacketSequenceNumber last_packet
;
2449 SendStreamDataToPeer(1, "foo", 0, !kFin
, &last_packet
); // Packet 1
2450 EXPECT_EQ(1u, last_packet
);
2451 SendStreamDataToPeer(3, "foo", 0, !kFin
, &last_packet
); // Packet 2
2452 EXPECT_EQ(2u, last_packet
);
2453 SendAckPacketToPeer(); // Packet 3
2454 SendStreamDataToPeer(5, "foo", 0, !kFin
, &last_packet
); // Packet 4
2455 EXPECT_EQ(4u, last_packet
);
2456 SendStreamDataToPeer(1, "foo", 3, !kFin
, &last_packet
); // Packet 5
2457 EXPECT_EQ(5u, last_packet
);
2458 SendStreamDataToPeer(3, "foo", 3, !kFin
, &last_packet
); // Packet 6
2459 EXPECT_EQ(6u, last_packet
);
2461 // Client will ack packets 1, 2, [!3], 4, 5.
2462 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
2463 QuicAckFrame frame1
= InitAckFrame(5);
2464 NackPacket(3, &frame1
);
2465 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
2466 ProcessAckPacket(&frame1
);
2468 // Now the client implicitly acks 3, and explicitly acks 6.
2469 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
2470 QuicAckFrame frame2
= InitAckFrame(6);
2471 ProcessAckPacket(&frame2
);
2474 TEST_P(QuicConnectionTest
, DontLatchUnackedPacket
) {
2475 SendStreamDataToPeer(1, "foo", 0, !kFin
, nullptr); // Packet 1;
2476 // From now on, we send acks, so the send algorithm won't mark them pending.
2477 ON_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
))
2478 .WillByDefault(Return(false));
2479 SendAckPacketToPeer(); // Packet 2
2481 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
2482 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
2483 QuicAckFrame frame
= InitAckFrame(1);
2484 ProcessAckPacket(&frame
);
2486 // Verify that our internal state has least-unacked as 2, because we're still
2487 // waiting for a potential ack for 2.
2489 EXPECT_EQ(2u, stop_waiting()->least_unacked
);
2491 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
2492 frame
= InitAckFrame(2);
2493 ProcessAckPacket(&frame
);
2494 EXPECT_EQ(3u, stop_waiting()->least_unacked
);
2496 // When we send an ack, we make sure our least-unacked makes sense. In this
2497 // case since we're not waiting on an ack for 2 and all packets are acked, we
2499 SendAckPacketToPeer(); // Packet 3
2500 // Least_unacked remains at 3 until another ack is received.
2501 EXPECT_EQ(3u, stop_waiting()->least_unacked
);
2502 // Check that the outgoing ack had its sequence number as least_unacked.
2503 EXPECT_EQ(3u, least_unacked());
2505 // Ack the ack, which updates the rtt and raises the least unacked.
2506 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
2507 frame
= InitAckFrame(3);
2508 ProcessAckPacket(&frame
);
2510 ON_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
))
2511 .WillByDefault(Return(true));
2512 SendStreamDataToPeer(1, "bar", 3, false, nullptr); // Packet 4
2513 EXPECT_EQ(4u, stop_waiting()->least_unacked
);
2514 ON_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
))
2515 .WillByDefault(Return(false));
2516 SendAckPacketToPeer(); // Packet 5
2517 EXPECT_EQ(4u, least_unacked());
2519 // Send two data packets at the end, and ensure if the last one is acked,
2520 // the least unacked is raised above the ack packets.
2521 ON_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
))
2522 .WillByDefault(Return(true));
2523 SendStreamDataToPeer(1, "bar", 6, false, nullptr); // Packet 6
2524 SendStreamDataToPeer(1, "bar", 9, false, nullptr); // Packet 7
2526 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
2527 frame
= InitAckFrame(7);
2528 NackPacket(5, &frame
);
2529 NackPacket(6, &frame
);
2530 ProcessAckPacket(&frame
);
2532 EXPECT_EQ(6u, stop_waiting()->least_unacked
);
2535 TEST_P(QuicConnectionTest
, ReviveMissingPacketAfterFecPacket
) {
2536 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
2538 // Don't send missing packet 1.
2539 ProcessFecPacket(2, 1, true, !kEntropyFlag
, nullptr);
2540 // Entropy flag should be false, so entropy should be 0.
2541 EXPECT_EQ(0u, QuicConnectionPeer::ReceivedEntropyHash(&connection_
, 2));
2544 TEST_P(QuicConnectionTest
, ReviveMissingPacketWithVaryingSeqNumLengths
) {
2545 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
2547 // Set up a debug visitor to the connection.
2548 scoped_ptr
<FecQuicConnectionDebugVisitor
> fec_visitor(
2549 new FecQuicConnectionDebugVisitor());
2550 connection_
.set_debug_visitor(fec_visitor
.get());
2552 QuicPacketSequenceNumber fec_packet
= 0;
2553 QuicSequenceNumberLength lengths
[] = {PACKET_6BYTE_SEQUENCE_NUMBER
,
2554 PACKET_4BYTE_SEQUENCE_NUMBER
,
2555 PACKET_2BYTE_SEQUENCE_NUMBER
,
2556 PACKET_1BYTE_SEQUENCE_NUMBER
};
2557 // For each sequence number length size, revive a packet and check sequence
2558 // number length in the revived packet.
2559 for (size_t i
= 0; i
< arraysize(lengths
); ++i
) {
2560 // Set sequence_number_length_ (for data and FEC packets).
2561 sequence_number_length_
= lengths
[i
];
2563 // Don't send missing packet, but send fec packet right after it.
2564 ProcessFecPacket(fec_packet
, fec_packet
- 1, true, !kEntropyFlag
, nullptr);
2565 // Sequence number length in the revived header should be the same as
2566 // in the original data/fec packet headers.
2567 EXPECT_EQ(sequence_number_length_
, fec_visitor
->revived_header().
2568 public_header
.sequence_number_length
);
2572 TEST_P(QuicConnectionTest
, ReviveMissingPacketWithVaryingConnectionIdLengths
) {
2573 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
2575 // Set up a debug visitor to the connection.
2576 scoped_ptr
<FecQuicConnectionDebugVisitor
> fec_visitor(
2577 new FecQuicConnectionDebugVisitor());
2578 connection_
.set_debug_visitor(fec_visitor
.get());
2580 QuicPacketSequenceNumber fec_packet
= 0;
2581 QuicConnectionIdLength lengths
[] = {PACKET_8BYTE_CONNECTION_ID
,
2582 PACKET_4BYTE_CONNECTION_ID
,
2583 PACKET_1BYTE_CONNECTION_ID
,
2584 PACKET_0BYTE_CONNECTION_ID
};
2585 // For each connection id length size, revive a packet and check connection
2586 // id length in the revived packet.
2587 for (size_t i
= 0; i
< arraysize(lengths
); ++i
) {
2588 // Set connection id length (for data and FEC packets).
2589 connection_id_length_
= lengths
[i
];
2591 // Don't send missing packet, but send fec packet right after it.
2592 ProcessFecPacket(fec_packet
, fec_packet
- 1, true, !kEntropyFlag
, nullptr);
2593 // Connection id length in the revived header should be the same as
2594 // in the original data/fec packet headers.
2595 EXPECT_EQ(connection_id_length_
,
2596 fec_visitor
->revived_header().public_header
.connection_id_length
);
2600 TEST_P(QuicConnectionTest
, ReviveMissingPacketAfterDataPacketThenFecPacket
) {
2601 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
2603 ProcessFecProtectedPacket(1, false, kEntropyFlag
);
2604 // Don't send missing packet 2.
2605 ProcessFecPacket(3, 1, true, !kEntropyFlag
, nullptr);
2606 // Entropy flag should be true, so entropy should not be 0.
2607 EXPECT_NE(0u, QuicConnectionPeer::ReceivedEntropyHash(&connection_
, 2));
2610 TEST_P(QuicConnectionTest
, ReviveMissingPacketAfterDataPacketsThenFecPacket
) {
2611 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
2613 ProcessFecProtectedPacket(1, false, !kEntropyFlag
);
2614 // Don't send missing packet 2.
2615 ProcessFecProtectedPacket(3, false, !kEntropyFlag
);
2616 ProcessFecPacket(4, 1, true, kEntropyFlag
, nullptr);
2617 // Ensure QUIC no longer revives entropy for lost packets.
2618 EXPECT_EQ(0u, QuicConnectionPeer::ReceivedEntropyHash(&connection_
, 2));
2619 EXPECT_NE(0u, QuicConnectionPeer::ReceivedEntropyHash(&connection_
, 4));
2622 TEST_P(QuicConnectionTest
, ReviveMissingPacketAfterDataPacket
) {
2623 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
2625 // Don't send missing packet 1.
2626 ProcessFecPacket(3, 1, false, !kEntropyFlag
, nullptr);
2628 ProcessFecProtectedPacket(2, true, !kEntropyFlag
);
2629 // Entropy flag should be false, so entropy should be 0.
2630 EXPECT_EQ(0u, QuicConnectionPeer::ReceivedEntropyHash(&connection_
, 2));
2633 TEST_P(QuicConnectionTest
, ReviveMissingPacketAfterDataPackets
) {
2634 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
2636 ProcessFecProtectedPacket(1, false, !kEntropyFlag
);
2637 // Don't send missing packet 2.
2638 ProcessFecPacket(6, 1, false, kEntropyFlag
, nullptr);
2639 ProcessFecProtectedPacket(3, false, kEntropyFlag
);
2640 ProcessFecProtectedPacket(4, false, kEntropyFlag
);
2641 ProcessFecProtectedPacket(5, true, !kEntropyFlag
);
2642 // Ensure entropy is not revived for the missing packet.
2643 EXPECT_EQ(0u, QuicConnectionPeer::ReceivedEntropyHash(&connection_
, 2));
2644 EXPECT_NE(0u, QuicConnectionPeer::ReceivedEntropyHash(&connection_
, 3));
2647 TEST_P(QuicConnectionTest
, TLP
) {
2648 QuicSentPacketManagerPeer::SetMaxTailLossProbes(manager_
, 1);
2650 SendStreamDataToPeer(3, "foo", 0, !kFin
, nullptr);
2651 EXPECT_EQ(1u, stop_waiting()->least_unacked
);
2652 QuicTime retransmission_time
=
2653 connection_
.GetRetransmissionAlarm()->deadline();
2654 EXPECT_NE(QuicTime::Zero(), retransmission_time
);
2656 EXPECT_EQ(1u, writer_
->header().packet_sequence_number
);
2657 // Simulate the retransmission alarm firing and sending a tlp,
2658 // so send algorithm's OnRetransmissionTimeout is not called.
2659 clock_
.AdvanceTime(retransmission_time
.Subtract(clock_
.Now()));
2660 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, 2u, _
, _
));
2661 connection_
.GetRetransmissionAlarm()->Fire();
2662 EXPECT_EQ(2u, writer_
->header().packet_sequence_number
);
2663 // We do not raise the high water mark yet.
2664 EXPECT_EQ(1u, stop_waiting()->least_unacked
);
2667 TEST_P(QuicConnectionTest
, RTO
) {
2668 QuicTime default_retransmission_time
= clock_
.ApproximateNow().Add(
2669 DefaultRetransmissionTime());
2670 SendStreamDataToPeer(3, "foo", 0, !kFin
, nullptr);
2671 EXPECT_EQ(1u, stop_waiting()->least_unacked
);
2673 EXPECT_EQ(1u, writer_
->header().packet_sequence_number
);
2674 EXPECT_EQ(default_retransmission_time
,
2675 connection_
.GetRetransmissionAlarm()->deadline());
2676 // Simulate the retransmission alarm firing.
2677 clock_
.AdvanceTime(DefaultRetransmissionTime());
2678 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, 2u, _
, _
));
2679 connection_
.GetRetransmissionAlarm()->Fire();
2680 EXPECT_EQ(2u, writer_
->header().packet_sequence_number
);
2681 // We do not raise the high water mark yet.
2682 EXPECT_EQ(1u, stop_waiting()->least_unacked
);
2685 TEST_P(QuicConnectionTest
, RTOWithSameEncryptionLevel
) {
2686 QuicTime default_retransmission_time
= clock_
.ApproximateNow().Add(
2687 DefaultRetransmissionTime());
2688 use_tagging_decrypter();
2690 // A TaggingEncrypter puts kTagSize copies of the given byte (0x01 here) at
2691 // the end of the packet. We can test this to check which encrypter was used.
2692 connection_
.SetEncrypter(ENCRYPTION_NONE
, new TaggingEncrypter(0x01));
2693 SendStreamDataToPeer(3, "foo", 0, !kFin
, nullptr);
2694 EXPECT_EQ(0x01010101u
, writer_
->final_bytes_of_last_packet());
2696 connection_
.SetEncrypter(ENCRYPTION_INITIAL
, new TaggingEncrypter(0x02));
2697 connection_
.SetDefaultEncryptionLevel(ENCRYPTION_INITIAL
);
2698 SendStreamDataToPeer(3, "foo", 0, !kFin
, nullptr);
2699 EXPECT_EQ(0x02020202u
, writer_
->final_bytes_of_last_packet());
2701 EXPECT_EQ(default_retransmission_time
,
2702 connection_
.GetRetransmissionAlarm()->deadline());
2705 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, 3, _
, _
));
2706 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, 4, _
, _
));
2709 // Simulate the retransmission alarm firing.
2710 clock_
.AdvanceTime(DefaultRetransmissionTime());
2711 connection_
.GetRetransmissionAlarm()->Fire();
2713 // Packet should have been sent with ENCRYPTION_NONE.
2714 EXPECT_EQ(0x01010101u
, writer_
->final_bytes_of_previous_packet());
2716 // Packet should have been sent with ENCRYPTION_INITIAL.
2717 EXPECT_EQ(0x02020202u
, writer_
->final_bytes_of_last_packet());
2720 TEST_P(QuicConnectionTest
, SendHandshakeMessages
) {
2721 use_tagging_decrypter();
2722 // A TaggingEncrypter puts kTagSize copies of the given byte (0x01 here) at
2723 // the end of the packet. We can test this to check which encrypter was used.
2724 connection_
.SetEncrypter(ENCRYPTION_NONE
, new TaggingEncrypter(0x01));
2726 // Attempt to send a handshake message and have the socket block.
2727 EXPECT_CALL(*send_algorithm_
,
2728 TimeUntilSend(_
, _
, _
)).WillRepeatedly(
2729 testing::Return(QuicTime::Delta::Zero()));
2731 connection_
.SendStreamDataWithString(1, "foo", 0, !kFin
, nullptr);
2732 // The packet should be serialized, but not queued.
2733 EXPECT_EQ(1u, connection_
.NumQueuedPackets());
2735 // Switch to the new encrypter.
2736 connection_
.SetEncrypter(ENCRYPTION_INITIAL
, new TaggingEncrypter(0x02));
2737 connection_
.SetDefaultEncryptionLevel(ENCRYPTION_INITIAL
);
2739 // Now become writeable and flush the packets.
2740 writer_
->SetWritable();
2741 EXPECT_CALL(visitor_
, OnCanWrite());
2742 connection_
.OnCanWrite();
2743 EXPECT_EQ(0u, connection_
.NumQueuedPackets());
2745 // Verify that the handshake packet went out at the null encryption.
2746 EXPECT_EQ(0x01010101u
, writer_
->final_bytes_of_last_packet());
2749 TEST_P(QuicConnectionTest
,
2750 DropRetransmitsForNullEncryptedPacketAfterForwardSecure
) {
2751 use_tagging_decrypter();
2752 connection_
.SetEncrypter(ENCRYPTION_NONE
, new TaggingEncrypter(0x01));
2753 QuicPacketSequenceNumber sequence_number
;
2754 SendStreamDataToPeer(3, "foo", 0, !kFin
, &sequence_number
);
2756 // Simulate the retransmission alarm firing and the socket blocking.
2758 clock_
.AdvanceTime(DefaultRetransmissionTime());
2759 connection_
.GetRetransmissionAlarm()->Fire();
2761 // Go forward secure.
2762 connection_
.SetEncrypter(ENCRYPTION_FORWARD_SECURE
,
2763 new TaggingEncrypter(0x02));
2764 connection_
.SetDefaultEncryptionLevel(ENCRYPTION_FORWARD_SECURE
);
2765 connection_
.NeuterUnencryptedPackets();
2767 EXPECT_EQ(QuicTime::Zero(),
2768 connection_
.GetRetransmissionAlarm()->deadline());
2769 // Unblock the socket and ensure that no packets are sent.
2770 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
)).Times(0);
2771 writer_
->SetWritable();
2772 connection_
.OnCanWrite();
2775 TEST_P(QuicConnectionTest
, RetransmitPacketsWithInitialEncryption
) {
2776 use_tagging_decrypter();
2777 connection_
.SetEncrypter(ENCRYPTION_NONE
, new TaggingEncrypter(0x01));
2778 connection_
.SetDefaultEncryptionLevel(ENCRYPTION_NONE
);
2780 SendStreamDataToPeer(1, "foo", 0, !kFin
, nullptr);
2782 connection_
.SetEncrypter(ENCRYPTION_INITIAL
, new TaggingEncrypter(0x02));
2783 connection_
.SetDefaultEncryptionLevel(ENCRYPTION_INITIAL
);
2785 SendStreamDataToPeer(2, "bar", 0, !kFin
, nullptr);
2786 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
)).Times(1);
2788 connection_
.RetransmitUnackedPackets(ALL_INITIAL_RETRANSMISSION
);
2791 TEST_P(QuicConnectionTest
, DelayForwardSecureEncryptionUntilClientIsReady
) {
2792 // A TaggingEncrypter puts kTagSize copies of the given byte (0x02 here) at
2793 // the end of the packet. We can test this to check which encrypter was used.
2794 use_tagging_decrypter();
2795 connection_
.SetEncrypter(ENCRYPTION_INITIAL
, new TaggingEncrypter(0x02));
2796 connection_
.SetDefaultEncryptionLevel(ENCRYPTION_INITIAL
);
2797 SendAckPacketToPeer();
2798 EXPECT_EQ(0x02020202u
, writer_
->final_bytes_of_last_packet());
2800 // Set a forward-secure encrypter but do not make it the default, and verify
2801 // that it is not yet used.
2802 connection_
.SetEncrypter(ENCRYPTION_FORWARD_SECURE
,
2803 new TaggingEncrypter(0x03));
2804 SendAckPacketToPeer();
2805 EXPECT_EQ(0x02020202u
, writer_
->final_bytes_of_last_packet());
2807 // Now simulate receipt of a forward-secure packet and verify that the
2808 // forward-secure encrypter is now used.
2809 connection_
.OnDecryptedPacket(ENCRYPTION_FORWARD_SECURE
);
2810 SendAckPacketToPeer();
2811 EXPECT_EQ(0x03030303u
, writer_
->final_bytes_of_last_packet());
2814 TEST_P(QuicConnectionTest
, DelayForwardSecureEncryptionUntilManyPacketSent
) {
2815 // Set a congestion window of 10 packets.
2816 QuicPacketCount congestion_window
= 10;
2817 EXPECT_CALL(*send_algorithm_
, GetCongestionWindow()).WillRepeatedly(
2818 Return(congestion_window
* kDefaultMaxPacketSize
));
2820 // A TaggingEncrypter puts kTagSize copies of the given byte (0x02 here) at
2821 // the end of the packet. We can test this to check which encrypter was used.
2822 use_tagging_decrypter();
2823 connection_
.SetEncrypter(ENCRYPTION_INITIAL
, new TaggingEncrypter(0x02));
2824 connection_
.SetDefaultEncryptionLevel(ENCRYPTION_INITIAL
);
2825 SendAckPacketToPeer();
2826 EXPECT_EQ(0x02020202u
, writer_
->final_bytes_of_last_packet());
2828 // Set a forward-secure encrypter but do not make it the default, and
2829 // verify that it is not yet used.
2830 connection_
.SetEncrypter(ENCRYPTION_FORWARD_SECURE
,
2831 new TaggingEncrypter(0x03));
2832 SendAckPacketToPeer();
2833 EXPECT_EQ(0x02020202u
, writer_
->final_bytes_of_last_packet());
2835 // Now send a packet "Far enough" after the encrypter was set and verify that
2836 // the forward-secure encrypter is now used.
2837 for (uint64 i
= 0; i
< 3 * congestion_window
- 1; ++i
) {
2838 EXPECT_EQ(0x02020202u
, writer_
->final_bytes_of_last_packet());
2839 SendAckPacketToPeer();
2841 EXPECT_EQ(0x03030303u
, writer_
->final_bytes_of_last_packet());
2844 TEST_P(QuicConnectionTest
, BufferNonDecryptablePackets
) {
2845 // SetFromConfig is always called after construction from InitializeSession.
2846 EXPECT_CALL(*send_algorithm_
, SetFromConfig(_
, _
));
2848 connection_
.SetFromConfig(config
);
2849 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
2850 use_tagging_decrypter();
2852 const uint8 tag
= 0x07;
2853 framer_
.SetEncrypter(ENCRYPTION_INITIAL
, new TaggingEncrypter(tag
));
2855 // Process an encrypted packet which can not yet be decrypted which should
2856 // result in the packet being buffered.
2857 ProcessDataPacketAtLevel(1, 0, kEntropyFlag
, ENCRYPTION_INITIAL
);
2859 // Transition to the new encryption state and process another encrypted packet
2860 // which should result in the original packet being processed.
2861 connection_
.SetDecrypter(new StrictTaggingDecrypter(tag
),
2862 ENCRYPTION_INITIAL
);
2863 connection_
.SetDefaultEncryptionLevel(ENCRYPTION_INITIAL
);
2864 connection_
.SetEncrypter(ENCRYPTION_INITIAL
, new TaggingEncrypter(tag
));
2865 EXPECT_CALL(visitor_
, OnStreamFrames(_
)).Times(2);
2866 ProcessDataPacketAtLevel(2, 0, kEntropyFlag
, ENCRYPTION_INITIAL
);
2868 // Finally, process a third packet and note that we do not reprocess the
2870 EXPECT_CALL(visitor_
, OnStreamFrames(_
)).Times(1);
2871 ProcessDataPacketAtLevel(3, 0, kEntropyFlag
, ENCRYPTION_INITIAL
);
2874 TEST_P(QuicConnectionTest
, Buffer100NonDecryptablePackets
) {
2875 // SetFromConfig is always called after construction from InitializeSession.
2876 EXPECT_CALL(*send_algorithm_
, SetFromConfig(_
, _
));
2878 config
.set_max_undecryptable_packets(100);
2879 connection_
.SetFromConfig(config
);
2880 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
2881 use_tagging_decrypter();
2883 const uint8 tag
= 0x07;
2884 framer_
.SetEncrypter(ENCRYPTION_INITIAL
, new TaggingEncrypter(tag
));
2886 // Process an encrypted packet which can not yet be decrypted which should
2887 // result in the packet being buffered.
2888 for (QuicPacketSequenceNumber i
= 1; i
<= 100; ++i
) {
2889 ProcessDataPacketAtLevel(i
, 0, kEntropyFlag
, ENCRYPTION_INITIAL
);
2892 // Transition to the new encryption state and process another encrypted packet
2893 // which should result in the original packets being processed.
2894 connection_
.SetDecrypter(new StrictTaggingDecrypter(tag
), ENCRYPTION_INITIAL
);
2895 connection_
.SetDefaultEncryptionLevel(ENCRYPTION_INITIAL
);
2896 connection_
.SetEncrypter(ENCRYPTION_INITIAL
, new TaggingEncrypter(tag
));
2897 EXPECT_CALL(visitor_
, OnStreamFrames(_
)).Times(101);
2898 ProcessDataPacketAtLevel(101, 0, kEntropyFlag
, ENCRYPTION_INITIAL
);
2900 // Finally, process a third packet and note that we do not reprocess the
2902 EXPECT_CALL(visitor_
, OnStreamFrames(_
)).Times(1);
2903 ProcessDataPacketAtLevel(102, 0, kEntropyFlag
, ENCRYPTION_INITIAL
);
2906 TEST_P(QuicConnectionTest
, TestRetransmitOrder
) {
2907 QuicByteCount first_packet_size
;
2908 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
)).WillOnce(
2909 DoAll(SaveArg
<3>(&first_packet_size
), Return(true)));
2911 connection_
.SendStreamDataWithString(3, "first_packet", 0, !kFin
, nullptr);
2912 QuicByteCount second_packet_size
;
2913 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
)).WillOnce(
2914 DoAll(SaveArg
<3>(&second_packet_size
), Return(true)));
2915 connection_
.SendStreamDataWithString(3, "second_packet", 12, !kFin
, nullptr);
2916 EXPECT_NE(first_packet_size
, second_packet_size
);
2917 // Advance the clock by huge time to make sure packets will be retransmitted.
2918 clock_
.AdvanceTime(QuicTime::Delta::FromSeconds(10));
2921 EXPECT_CALL(*send_algorithm_
,
2922 OnPacketSent(_
, _
, _
, first_packet_size
, _
));
2923 EXPECT_CALL(*send_algorithm_
,
2924 OnPacketSent(_
, _
, _
, second_packet_size
, _
));
2926 connection_
.GetRetransmissionAlarm()->Fire();
2928 // Advance again and expect the packets to be sent again in the same order.
2929 clock_
.AdvanceTime(QuicTime::Delta::FromSeconds(20));
2932 EXPECT_CALL(*send_algorithm_
,
2933 OnPacketSent(_
, _
, _
, first_packet_size
, _
));
2934 EXPECT_CALL(*send_algorithm_
,
2935 OnPacketSent(_
, _
, _
, second_packet_size
, _
));
2937 connection_
.GetRetransmissionAlarm()->Fire();
2940 TEST_P(QuicConnectionTest
, SetRTOAfterWritingToSocket
) {
2942 connection_
.SendStreamDataWithString(1, "foo", 0, !kFin
, nullptr);
2943 // Make sure that RTO is not started when the packet is queued.
2944 EXPECT_FALSE(connection_
.GetRetransmissionAlarm()->IsSet());
2946 // Test that RTO is started once we write to the socket.
2947 writer_
->SetWritable();
2948 connection_
.OnCanWrite();
2949 EXPECT_TRUE(connection_
.GetRetransmissionAlarm()->IsSet());
2952 TEST_P(QuicConnectionTest
, DelayRTOWithAckReceipt
) {
2953 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
2954 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
))
2956 connection_
.SendStreamDataWithString(2, "foo", 0, !kFin
, nullptr);
2957 connection_
.SendStreamDataWithString(3, "bar", 0, !kFin
, nullptr);
2958 QuicAlarm
* retransmission_alarm
= connection_
.GetRetransmissionAlarm();
2959 EXPECT_TRUE(retransmission_alarm
->IsSet());
2960 EXPECT_EQ(clock_
.Now().Add(DefaultRetransmissionTime()),
2961 retransmission_alarm
->deadline());
2963 // Advance the time right before the RTO, then receive an ack for the first
2964 // packet to delay the RTO.
2965 clock_
.AdvanceTime(DefaultRetransmissionTime());
2966 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
2967 QuicAckFrame ack
= InitAckFrame(1);
2968 ProcessAckPacket(&ack
);
2969 EXPECT_TRUE(retransmission_alarm
->IsSet());
2970 EXPECT_GT(retransmission_alarm
->deadline(), clock_
.Now());
2972 // Move forward past the original RTO and ensure the RTO is still pending.
2973 clock_
.AdvanceTime(DefaultRetransmissionTime().Multiply(2));
2975 // Ensure the second packet gets retransmitted when it finally fires.
2976 EXPECT_TRUE(retransmission_alarm
->IsSet());
2977 EXPECT_LT(retransmission_alarm
->deadline(), clock_
.ApproximateNow());
2978 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
));
2979 // Manually cancel the alarm to simulate a real test.
2980 connection_
.GetRetransmissionAlarm()->Fire();
2982 // The new retransmitted sequence number should set the RTO to a larger value
2984 EXPECT_TRUE(retransmission_alarm
->IsSet());
2985 QuicTime next_rto_time
= retransmission_alarm
->deadline();
2986 QuicTime expected_rto_time
=
2987 connection_
.sent_packet_manager().GetRetransmissionTime();
2988 EXPECT_EQ(next_rto_time
, expected_rto_time
);
2991 TEST_P(QuicConnectionTest
, TestQueued
) {
2992 EXPECT_EQ(0u, connection_
.NumQueuedPackets());
2994 connection_
.SendStreamDataWithString(1, "foo", 0, !kFin
, nullptr);
2995 EXPECT_EQ(1u, connection_
.NumQueuedPackets());
2997 // Unblock the writes and actually send.
2998 writer_
->SetWritable();
2999 connection_
.OnCanWrite();
3000 EXPECT_EQ(0u, connection_
.NumQueuedPackets());
3003 TEST_P(QuicConnectionTest
, CloseFecGroup
) {
3004 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
3005 // Don't send missing packet 1.
3006 // Don't send missing packet 2.
3007 ProcessFecProtectedPacket(3, false, !kEntropyFlag
);
3008 // Don't send missing FEC packet 3.
3009 ASSERT_EQ(1u, connection_
.NumFecGroups());
3011 // Now send non-fec protected ack packet and close the group.
3012 QuicPacketCreatorPeer::SetSequenceNumber(&peer_creator_
, 4);
3013 QuicStopWaitingFrame frame
= InitStopWaitingFrame(5);
3014 ProcessStopWaitingPacket(&frame
);
3015 ASSERT_EQ(0u, connection_
.NumFecGroups());
3018 TEST_P(QuicConnectionTest
, InitialTimeout
) {
3019 EXPECT_TRUE(connection_
.connected());
3020 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
)).Times(AnyNumber());
3021 EXPECT_FALSE(connection_
.GetTimeoutAlarm()->IsSet());
3023 // SetFromConfig sets the initial timeouts before negotiation.
3024 EXPECT_CALL(*send_algorithm_
, SetFromConfig(_
, _
));
3026 connection_
.SetFromConfig(config
);
3027 // Subtract a second from the idle timeout on the client side.
3028 QuicTime default_timeout
= clock_
.ApproximateNow().Add(
3029 QuicTime::Delta::FromSeconds(kInitialIdleTimeoutSecs
- 1));
3030 EXPECT_EQ(default_timeout
, connection_
.GetTimeoutAlarm()->deadline());
3032 EXPECT_CALL(visitor_
, OnConnectionClosed(QUIC_CONNECTION_TIMED_OUT
, false));
3033 // Simulate the timeout alarm firing.
3035 QuicTime::Delta::FromSeconds(kInitialIdleTimeoutSecs
- 1));
3036 connection_
.GetTimeoutAlarm()->Fire();
3038 EXPECT_FALSE(connection_
.GetTimeoutAlarm()->IsSet());
3039 EXPECT_FALSE(connection_
.connected());
3041 EXPECT_FALSE(connection_
.GetAckAlarm()->IsSet());
3042 EXPECT_FALSE(connection_
.GetPingAlarm()->IsSet());
3043 EXPECT_FALSE(connection_
.GetFecAlarm()->IsSet());
3044 EXPECT_FALSE(connection_
.GetResumeWritesAlarm()->IsSet());
3045 EXPECT_FALSE(connection_
.GetRetransmissionAlarm()->IsSet());
3046 EXPECT_FALSE(connection_
.GetSendAlarm()->IsSet());
3049 TEST_P(QuicConnectionTest
, OverallTimeout
) {
3050 // Use a shorter overall connection timeout than idle timeout for this test.
3051 const QuicTime::Delta timeout
= QuicTime::Delta::FromSeconds(5);
3052 connection_
.SetNetworkTimeouts(timeout
, timeout
);
3053 EXPECT_TRUE(connection_
.connected());
3054 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
)).Times(AnyNumber());
3056 QuicTime overall_timeout
= clock_
.ApproximateNow().Add(timeout
).Subtract(
3057 QuicTime::Delta::FromSeconds(1));
3058 EXPECT_EQ(overall_timeout
, connection_
.GetTimeoutAlarm()->deadline());
3059 EXPECT_TRUE(connection_
.connected());
3061 // Send and ack new data 3 seconds later to lengthen the idle timeout.
3062 SendStreamDataToPeer(1, "GET /", 0, kFin
, nullptr);
3063 clock_
.AdvanceTime(QuicTime::Delta::FromSeconds(3));
3064 QuicAckFrame frame
= InitAckFrame(1);
3065 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
3066 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
3067 ProcessAckPacket(&frame
);
3069 // Fire early to verify it wouldn't timeout yet.
3070 connection_
.GetTimeoutAlarm()->Fire();
3071 EXPECT_TRUE(connection_
.GetTimeoutAlarm()->IsSet());
3072 EXPECT_TRUE(connection_
.connected());
3074 clock_
.AdvanceTime(timeout
.Subtract(QuicTime::Delta::FromSeconds(2)));
3076 EXPECT_CALL(visitor_
,
3077 OnConnectionClosed(QUIC_CONNECTION_OVERALL_TIMED_OUT
, false));
3078 // Simulate the timeout alarm firing.
3079 connection_
.GetTimeoutAlarm()->Fire();
3081 EXPECT_FALSE(connection_
.GetTimeoutAlarm()->IsSet());
3082 EXPECT_FALSE(connection_
.connected());
3084 EXPECT_FALSE(connection_
.GetAckAlarm()->IsSet());
3085 EXPECT_FALSE(connection_
.GetPingAlarm()->IsSet());
3086 EXPECT_FALSE(connection_
.GetFecAlarm()->IsSet());
3087 EXPECT_FALSE(connection_
.GetResumeWritesAlarm()->IsSet());
3088 EXPECT_FALSE(connection_
.GetRetransmissionAlarm()->IsSet());
3089 EXPECT_FALSE(connection_
.GetSendAlarm()->IsSet());
3092 TEST_P(QuicConnectionTest
, PingAfterSend
) {
3093 EXPECT_TRUE(connection_
.connected());
3094 EXPECT_CALL(visitor_
, HasOpenDataStreams()).WillRepeatedly(Return(true));
3095 EXPECT_FALSE(connection_
.GetPingAlarm()->IsSet());
3097 // Advance to 5ms, and send a packet to the peer, which will set
3099 clock_
.AdvanceTime(QuicTime::Delta::FromMilliseconds(5));
3100 EXPECT_FALSE(connection_
.GetRetransmissionAlarm()->IsSet());
3101 SendStreamDataToPeer(1, "GET /", 0, kFin
, nullptr);
3102 EXPECT_TRUE(connection_
.GetPingAlarm()->IsSet());
3103 EXPECT_EQ(clock_
.ApproximateNow().Add(QuicTime::Delta::FromSeconds(15)),
3104 connection_
.GetPingAlarm()->deadline());
3106 // Now recevie and ACK of the previous packet, which will move the
3107 // ping alarm forward.
3108 clock_
.AdvanceTime(QuicTime::Delta::FromMilliseconds(5));
3109 QuicAckFrame frame
= InitAckFrame(1);
3110 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
3111 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
3112 ProcessAckPacket(&frame
);
3113 EXPECT_TRUE(connection_
.GetPingAlarm()->IsSet());
3114 // The ping timer is set slightly less than 15 seconds in the future, because
3115 // of the 1s ping timer alarm granularity.
3116 EXPECT_EQ(clock_
.ApproximateNow().Add(QuicTime::Delta::FromSeconds(15))
3117 .Subtract(QuicTime::Delta::FromMilliseconds(5)),
3118 connection_
.GetPingAlarm()->deadline());
3121 clock_
.AdvanceTime(QuicTime::Delta::FromSeconds(15));
3122 connection_
.GetPingAlarm()->Fire();
3123 EXPECT_EQ(1u, writer_
->frame_count());
3124 ASSERT_EQ(1u, writer_
->ping_frames().size());
3127 EXPECT_CALL(visitor_
, HasOpenDataStreams()).WillRepeatedly(Return(false));
3128 clock_
.AdvanceTime(QuicTime::Delta::FromMilliseconds(5));
3129 SendAckPacketToPeer();
3131 EXPECT_FALSE(connection_
.GetPingAlarm()->IsSet());
3134 TEST_P(QuicConnectionTest
, TimeoutAfterSend
) {
3135 EXPECT_TRUE(connection_
.connected());
3136 EXPECT_CALL(*send_algorithm_
, SetFromConfig(_
, _
));
3138 connection_
.SetFromConfig(config
);
3139 EXPECT_FALSE(QuicConnectionPeer::IsSilentCloseEnabled(&connection_
));
3141 const QuicTime::Delta initial_idle_timeout
=
3142 QuicTime::Delta::FromSeconds(kInitialIdleTimeoutSecs
- 1);
3143 const QuicTime::Delta five_ms
= QuicTime::Delta::FromMilliseconds(5);
3144 QuicTime default_timeout
= clock_
.ApproximateNow().Add(initial_idle_timeout
);
3146 // When we send a packet, the timeout will change to 5ms +
3147 // kInitialIdleTimeoutSecs.
3148 clock_
.AdvanceTime(five_ms
);
3150 // Send an ack so we don't set the retransmission alarm.
3151 SendAckPacketToPeer();
3152 EXPECT_EQ(default_timeout
, connection_
.GetTimeoutAlarm()->deadline());
3154 // The original alarm will fire. We should not time out because we had a
3155 // network event at t=5ms. The alarm will reregister.
3156 clock_
.AdvanceTime(initial_idle_timeout
.Subtract(five_ms
));
3157 EXPECT_EQ(default_timeout
, clock_
.ApproximateNow());
3158 connection_
.GetTimeoutAlarm()->Fire();
3159 EXPECT_TRUE(connection_
.GetTimeoutAlarm()->IsSet());
3160 EXPECT_TRUE(connection_
.connected());
3161 EXPECT_EQ(default_timeout
.Add(five_ms
),
3162 connection_
.GetTimeoutAlarm()->deadline());
3164 // This time, we should time out.
3165 EXPECT_CALL(visitor_
, OnConnectionClosed(QUIC_CONNECTION_TIMED_OUT
, false));
3166 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
));
3167 clock_
.AdvanceTime(five_ms
);
3168 EXPECT_EQ(default_timeout
.Add(five_ms
), clock_
.ApproximateNow());
3169 connection_
.GetTimeoutAlarm()->Fire();
3170 EXPECT_FALSE(connection_
.GetTimeoutAlarm()->IsSet());
3171 EXPECT_FALSE(connection_
.connected());
3174 TEST_P(QuicConnectionTest
, TimeoutAfterSendSilentClose
) {
3175 // Same test as above, but complete a handshake which enables silent close,
3176 // causing no connection close packet to be sent.
3177 EXPECT_TRUE(connection_
.connected());
3178 EXPECT_CALL(*send_algorithm_
, SetFromConfig(_
, _
));
3181 // Create a handshake message that also enables silent close.
3182 CryptoHandshakeMessage msg
;
3183 string error_details
;
3184 QuicConfig client_config
;
3185 client_config
.SetInitialStreamFlowControlWindowToSend(
3186 kInitialStreamFlowControlWindowForTest
);
3187 client_config
.SetInitialSessionFlowControlWindowToSend(
3188 kInitialSessionFlowControlWindowForTest
);
3189 client_config
.SetIdleConnectionStateLifetime(
3190 QuicTime::Delta::FromSeconds(kDefaultIdleTimeoutSecs
),
3191 QuicTime::Delta::FromSeconds(kDefaultIdleTimeoutSecs
));
3192 client_config
.ToHandshakeMessage(&msg
);
3193 const QuicErrorCode error
=
3194 config
.ProcessPeerHello(msg
, CLIENT
, &error_details
);
3195 EXPECT_EQ(QUIC_NO_ERROR
, error
);
3197 connection_
.SetFromConfig(config
);
3198 EXPECT_TRUE(QuicConnectionPeer::IsSilentCloseEnabled(&connection_
));
3200 const QuicTime::Delta default_idle_timeout
=
3201 QuicTime::Delta::FromSeconds(kDefaultIdleTimeoutSecs
- 1);
3202 const QuicTime::Delta five_ms
= QuicTime::Delta::FromMilliseconds(5);
3203 QuicTime default_timeout
= clock_
.ApproximateNow().Add(default_idle_timeout
);
3205 // When we send a packet, the timeout will change to 5ms +
3206 // kInitialIdleTimeoutSecs.
3207 clock_
.AdvanceTime(five_ms
);
3209 // Send an ack so we don't set the retransmission alarm.
3210 SendAckPacketToPeer();
3211 EXPECT_EQ(default_timeout
, connection_
.GetTimeoutAlarm()->deadline());
3213 // The original alarm will fire. We should not time out because we had a
3214 // network event at t=5ms. The alarm will reregister.
3215 clock_
.AdvanceTime(default_idle_timeout
.Subtract(five_ms
));
3216 EXPECT_EQ(default_timeout
, clock_
.ApproximateNow());
3217 connection_
.GetTimeoutAlarm()->Fire();
3218 EXPECT_TRUE(connection_
.GetTimeoutAlarm()->IsSet());
3219 EXPECT_TRUE(connection_
.connected());
3220 EXPECT_EQ(default_timeout
.Add(five_ms
),
3221 connection_
.GetTimeoutAlarm()->deadline());
3223 // This time, we should time out.
3224 EXPECT_CALL(visitor_
, OnConnectionClosed(QUIC_CONNECTION_TIMED_OUT
, false));
3225 clock_
.AdvanceTime(five_ms
);
3226 EXPECT_EQ(default_timeout
.Add(five_ms
), clock_
.ApproximateNow());
3227 connection_
.GetTimeoutAlarm()->Fire();
3228 EXPECT_FALSE(connection_
.GetTimeoutAlarm()->IsSet());
3229 EXPECT_FALSE(connection_
.connected());
3232 TEST_P(QuicConnectionTest
, SendScheduler
) {
3233 // Test that if we send a packet without delay, it is not queued.
3234 QuicPacket
* packet
= ConstructDataPacket(1, 0, !kEntropyFlag
);
3235 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
));
3236 connection_
.SendPacket(
3237 ENCRYPTION_NONE
, 1, packet
, kTestEntropyHash
, HAS_RETRANSMITTABLE_DATA
);
3238 EXPECT_EQ(0u, connection_
.NumQueuedPackets());
3241 TEST_P(QuicConnectionTest
, SendSchedulerEAGAIN
) {
3242 QuicPacket
* packet
= ConstructDataPacket(1, 0, !kEntropyFlag
);
3244 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, 1, _
, _
)).Times(0);
3245 connection_
.SendPacket(
3246 ENCRYPTION_NONE
, 1, packet
, kTestEntropyHash
, HAS_RETRANSMITTABLE_DATA
);
3247 EXPECT_EQ(1u, connection_
.NumQueuedPackets());
3250 TEST_P(QuicConnectionTest
, TestQueueLimitsOnSendStreamData
) {
3251 // All packets carry version info till version is negotiated.
3252 size_t payload_length
;
3253 size_t length
= GetPacketLengthForOneStream(
3254 connection_
.version(), kIncludeVersion
,
3255 PACKET_8BYTE_CONNECTION_ID
, PACKET_1BYTE_SEQUENCE_NUMBER
,
3256 NOT_IN_FEC_GROUP
, &payload_length
);
3257 creator_
->SetMaxPacketLength(length
);
3259 // Queue the first packet.
3260 EXPECT_CALL(*send_algorithm_
,
3261 TimeUntilSend(_
, _
, _
)).WillOnce(
3262 testing::Return(QuicTime::Delta::FromMicroseconds(10)));
3263 const string
payload(payload_length
, 'a');
3264 EXPECT_EQ(0u, connection_
.SendStreamDataWithString(3, payload
, 0, !kFin
,
3265 nullptr).bytes_consumed
);
3266 EXPECT_EQ(0u, connection_
.NumQueuedPackets());
3269 TEST_P(QuicConnectionTest
, LoopThroughSendingPackets
) {
3270 // All packets carry version info till version is negotiated.
3271 size_t payload_length
;
3272 // GetPacketLengthForOneStream() assumes a stream offset of 0 in determining
3273 // packet length. The size of the offset field in a stream frame is 0 for
3274 // offset 0, and 2 for non-zero offsets up through 16K. Increase
3275 // max_packet_length by 2 so that subsequent packets containing subsequent
3276 // stream frames with non-zero offets will fit within the packet length.
3277 size_t length
= 2 + GetPacketLengthForOneStream(
3278 connection_
.version(), kIncludeVersion
,
3279 PACKET_8BYTE_CONNECTION_ID
, PACKET_1BYTE_SEQUENCE_NUMBER
,
3280 NOT_IN_FEC_GROUP
, &payload_length
);
3281 creator_
->SetMaxPacketLength(length
);
3283 // Queue the first packet.
3284 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
)).Times(7);
3285 // The first stream frame will have 2 fewer overhead bytes than the other six.
3286 const string
payload(payload_length
* 7 + 2, 'a');
3287 EXPECT_EQ(payload
.size(),
3288 connection_
.SendStreamDataWithString(1, payload
, 0, !kFin
, nullptr)
3292 TEST_P(QuicConnectionTest
, LoopThroughSendingPacketsWithTruncation
) {
3293 // Set up a larger payload than will fit in one packet.
3294 const string
payload(connection_
.max_packet_length(), 'a');
3295 EXPECT_CALL(*send_algorithm_
, SetFromConfig(_
, _
)).Times(AnyNumber());
3297 // Now send some packets with no truncation.
3298 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
)).Times(2);
3299 EXPECT_EQ(payload
.size(),
3300 connection_
.SendStreamDataWithString(
3301 3, payload
, 0, !kFin
, nullptr).bytes_consumed
);
3302 // Track the size of the second packet here. The overhead will be the largest
3303 // we see in this test, due to the non-truncated connection id.
3304 size_t non_truncated_packet_size
= writer_
->last_packet_size();
3306 // Change to a 4 byte connection id.
3308 QuicConfigPeer::SetReceivedBytesForConnectionId(&config
, 4);
3309 connection_
.SetFromConfig(config
);
3310 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
)).Times(2);
3311 EXPECT_EQ(payload
.size(),
3312 connection_
.SendStreamDataWithString(
3313 3, payload
, 0, !kFin
, nullptr).bytes_consumed
);
3314 // Verify that we have 8 fewer bytes than in the non-truncated case. The
3315 // first packet got 4 bytes of extra payload due to the truncation, and the
3316 // headers here are also 4 byte smaller.
3317 EXPECT_EQ(non_truncated_packet_size
, writer_
->last_packet_size() + 8);
3319 // Change to a 1 byte connection id.
3320 QuicConfigPeer::SetReceivedBytesForConnectionId(&config
, 1);
3321 connection_
.SetFromConfig(config
);
3322 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
)).Times(2);
3323 EXPECT_EQ(payload
.size(),
3324 connection_
.SendStreamDataWithString(
3325 3, payload
, 0, !kFin
, nullptr).bytes_consumed
);
3326 // Just like above, we save 7 bytes on payload, and 7 on truncation.
3327 EXPECT_EQ(non_truncated_packet_size
, writer_
->last_packet_size() + 7 * 2);
3329 // Change to a 0 byte connection id.
3330 QuicConfigPeer::SetReceivedBytesForConnectionId(&config
, 0);
3331 connection_
.SetFromConfig(config
);
3332 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
)).Times(2);
3333 EXPECT_EQ(payload
.size(),
3334 connection_
.SendStreamDataWithString(
3335 3, payload
, 0, !kFin
, nullptr).bytes_consumed
);
3336 // Just like above, we save 8 bytes on payload, and 8 on truncation.
3337 EXPECT_EQ(non_truncated_packet_size
, writer_
->last_packet_size() + 8 * 2);
3340 TEST_P(QuicConnectionTest
, SendDelayedAck
) {
3341 QuicTime ack_time
= clock_
.ApproximateNow().Add(DefaultDelayedAckTime());
3342 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
3343 EXPECT_FALSE(connection_
.GetAckAlarm()->IsSet());
3344 const uint8 tag
= 0x07;
3345 connection_
.SetDecrypter(new StrictTaggingDecrypter(tag
),
3346 ENCRYPTION_INITIAL
);
3347 framer_
.SetEncrypter(ENCRYPTION_INITIAL
, new TaggingEncrypter(tag
));
3348 // Process a packet from the non-crypto stream.
3349 frame1_
.stream_id
= 3;
3351 // The same as ProcessPacket(1) except that ENCRYPTION_INITIAL is used
3352 // instead of ENCRYPTION_NONE.
3353 EXPECT_CALL(visitor_
, OnStreamFrames(_
)).Times(1);
3354 ProcessDataPacketAtLevel(1, 0, !kEntropyFlag
, ENCRYPTION_INITIAL
);
3356 // Check if delayed ack timer is running for the expected interval.
3357 EXPECT_TRUE(connection_
.GetAckAlarm()->IsSet());
3358 EXPECT_EQ(ack_time
, connection_
.GetAckAlarm()->deadline());
3359 // Simulate delayed ack alarm firing.
3360 connection_
.GetAckAlarm()->Fire();
3361 // Check that ack is sent and that delayed ack alarm is reset.
3362 EXPECT_EQ(2u, writer_
->frame_count());
3363 EXPECT_FALSE(writer_
->stop_waiting_frames().empty());
3364 EXPECT_FALSE(writer_
->ack_frames().empty());
3365 EXPECT_FALSE(connection_
.GetAckAlarm()->IsSet());
3368 TEST_P(QuicConnectionTest
, SendDelayedAckOnHandshakeConfirmed
) {
3369 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
3371 // Check that ack is sent and that delayed ack alarm is set.
3372 EXPECT_TRUE(connection_
.GetAckAlarm()->IsSet());
3373 QuicTime ack_time
= clock_
.ApproximateNow().Add(DefaultDelayedAckTime());
3374 EXPECT_EQ(ack_time
, connection_
.GetAckAlarm()->deadline());
3376 // Completing the handshake as the server does nothing.
3377 QuicConnectionPeer::SetPerspective(&connection_
, Perspective::IS_SERVER
);
3378 connection_
.OnHandshakeComplete();
3379 EXPECT_TRUE(connection_
.GetAckAlarm()->IsSet());
3380 EXPECT_EQ(ack_time
, connection_
.GetAckAlarm()->deadline());
3382 // Complete the handshake as the client decreases the delayed ack time to 0ms.
3383 QuicConnectionPeer::SetPerspective(&connection_
, Perspective::IS_CLIENT
);
3384 connection_
.OnHandshakeComplete();
3385 EXPECT_TRUE(connection_
.GetAckAlarm()->IsSet());
3386 EXPECT_EQ(clock_
.ApproximateNow(), connection_
.GetAckAlarm()->deadline());
3389 TEST_P(QuicConnectionTest
, SendDelayedAckOnSecondPacket
) {
3390 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
3393 // Check that ack is sent and that delayed ack alarm is reset.
3394 EXPECT_EQ(2u, writer_
->frame_count());
3395 EXPECT_FALSE(writer_
->stop_waiting_frames().empty());
3396 EXPECT_FALSE(writer_
->ack_frames().empty());
3397 EXPECT_FALSE(connection_
.GetAckAlarm()->IsSet());
3400 TEST_P(QuicConnectionTest
, NoAckOnOldNacks
) {
3401 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
3402 // Drop one packet, triggering a sequence of acks.
3404 size_t frames_per_ack
= 2;
3405 EXPECT_EQ(frames_per_ack
, writer_
->frame_count());
3406 EXPECT_FALSE(writer_
->ack_frames().empty());
3409 EXPECT_EQ(frames_per_ack
, writer_
->frame_count());
3410 EXPECT_FALSE(writer_
->ack_frames().empty());
3413 EXPECT_EQ(frames_per_ack
, writer_
->frame_count());
3414 EXPECT_FALSE(writer_
->ack_frames().empty());
3417 EXPECT_EQ(frames_per_ack
, writer_
->frame_count());
3418 EXPECT_FALSE(writer_
->ack_frames().empty());
3420 // Now only set the timer on the 6th packet, instead of sending another ack.
3422 EXPECT_EQ(0u, writer_
->frame_count());
3423 EXPECT_TRUE(connection_
.GetAckAlarm()->IsSet());
3426 TEST_P(QuicConnectionTest
, SendDelayedAckOnOutgoingPacket
) {
3427 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
3429 connection_
.SendStreamDataWithString(kClientDataStreamId1
, "foo", 0, !kFin
,
3431 // Check that ack is bundled with outgoing data and that delayed ack
3433 EXPECT_EQ(3u, writer_
->frame_count());
3434 EXPECT_FALSE(writer_
->stop_waiting_frames().empty());
3435 EXPECT_FALSE(writer_
->ack_frames().empty());
3436 EXPECT_FALSE(connection_
.GetAckAlarm()->IsSet());
3439 TEST_P(QuicConnectionTest
, SendDelayedAckOnOutgoingCryptoPacket
) {
3440 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
3442 connection_
.SendStreamDataWithString(kCryptoStreamId
, "foo", 0, !kFin
,
3444 // Check that ack is bundled with outgoing crypto data.
3445 EXPECT_EQ(3u, writer_
->frame_count());
3446 EXPECT_FALSE(writer_
->ack_frames().empty());
3447 EXPECT_FALSE(connection_
.GetAckAlarm()->IsSet());
3450 TEST_P(QuicConnectionTest
, BlockAndBufferOnFirstCHLOPacketOfTwo
) {
3451 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
3454 writer_
->set_is_write_blocked_data_buffered(true);
3455 connection_
.SendStreamDataWithString(kCryptoStreamId
, "foo", 0, !kFin
,
3457 EXPECT_TRUE(writer_
->IsWriteBlocked());
3458 EXPECT_FALSE(connection_
.HasQueuedData());
3459 connection_
.SendStreamDataWithString(kCryptoStreamId
, "bar", 3, !kFin
,
3461 EXPECT_TRUE(writer_
->IsWriteBlocked());
3462 EXPECT_TRUE(connection_
.HasQueuedData());
3465 TEST_P(QuicConnectionTest
, BundleAckForSecondCHLO
) {
3466 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
3467 EXPECT_FALSE(connection_
.GetAckAlarm()->IsSet());
3468 EXPECT_CALL(visitor_
, OnCanWrite()).WillOnce(
3469 IgnoreResult(InvokeWithoutArgs(&connection_
,
3470 &TestConnection::SendCryptoStreamData
)));
3471 // Process a packet from the crypto stream, which is frame1_'s default.
3472 // Receiving the CHLO as packet 2 first will cause the connection to
3473 // immediately send an ack, due to the packet gap.
3475 // Check that ack is sent and that delayed ack alarm is reset.
3476 EXPECT_EQ(3u, writer_
->frame_count());
3477 EXPECT_FALSE(writer_
->stop_waiting_frames().empty());
3478 EXPECT_EQ(1u, writer_
->stream_frames().size());
3479 EXPECT_FALSE(writer_
->ack_frames().empty());
3480 EXPECT_FALSE(connection_
.GetAckAlarm()->IsSet());
3483 TEST_P(QuicConnectionTest
, BundleAckWithDataOnIncomingAck
) {
3484 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
3485 connection_
.SendStreamDataWithString(kClientDataStreamId1
, "foo", 0, !kFin
,
3487 connection_
.SendStreamDataWithString(kClientDataStreamId1
, "foo", 3, !kFin
,
3489 // Ack the second packet, which will retransmit the first packet.
3490 QuicAckFrame ack
= InitAckFrame(2);
3491 NackPacket(1, &ack
);
3492 SequenceNumberSet lost_packets
;
3493 lost_packets
.insert(1);
3494 EXPECT_CALL(*loss_algorithm_
, DetectLostPackets(_
, _
, _
, _
))
3495 .WillOnce(Return(lost_packets
));
3496 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
3497 ProcessAckPacket(&ack
);
3498 EXPECT_EQ(1u, writer_
->frame_count());
3499 EXPECT_EQ(1u, writer_
->stream_frames().size());
3502 // Now ack the retransmission, which will both raise the high water mark
3503 // and see if there is more data to send.
3504 ack
= InitAckFrame(3);
3505 NackPacket(1, &ack
);
3506 EXPECT_CALL(*loss_algorithm_
, DetectLostPackets(_
, _
, _
, _
))
3507 .WillOnce(Return(SequenceNumberSet()));
3508 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
3509 ProcessAckPacket(&ack
);
3511 // Check that no packet is sent and the ack alarm isn't set.
3512 EXPECT_EQ(0u, writer_
->frame_count());
3513 EXPECT_FALSE(connection_
.GetAckAlarm()->IsSet());
3516 // Send the same ack, but send both data and an ack together.
3517 ack
= InitAckFrame(3);
3518 NackPacket(1, &ack
);
3519 EXPECT_CALL(*loss_algorithm_
, DetectLostPackets(_
, _
, _
, _
))
3520 .WillOnce(Return(SequenceNumberSet()));
3521 EXPECT_CALL(visitor_
, OnCanWrite()).WillOnce(
3522 IgnoreResult(InvokeWithoutArgs(
3524 &TestConnection::EnsureWritableAndSendStreamData5
)));
3525 ProcessAckPacket(&ack
);
3527 // Check that ack is bundled with outgoing data and the delayed ack
3529 EXPECT_EQ(3u, writer_
->frame_count());
3530 EXPECT_FALSE(writer_
->stop_waiting_frames().empty());
3531 EXPECT_FALSE(writer_
->ack_frames().empty());
3532 EXPECT_EQ(1u, writer_
->stream_frames().size());
3533 EXPECT_FALSE(connection_
.GetAckAlarm()->IsSet());
3536 TEST_P(QuicConnectionTest
, NoAckSentForClose
) {
3537 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
3539 EXPECT_CALL(visitor_
, OnConnectionClosed(QUIC_PEER_GOING_AWAY
, true));
3540 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
)).Times(0);
3541 ProcessClosePacket(2, 0);
3544 TEST_P(QuicConnectionTest
, SendWhenDisconnected
) {
3545 EXPECT_TRUE(connection_
.connected());
3546 EXPECT_CALL(visitor_
, OnConnectionClosed(QUIC_PEER_GOING_AWAY
, false));
3547 connection_
.CloseConnection(QUIC_PEER_GOING_AWAY
, false);
3548 EXPECT_FALSE(connection_
.connected());
3549 EXPECT_FALSE(connection_
.CanWriteStreamData());
3550 QuicPacket
* packet
= ConstructDataPacket(1, 0, !kEntropyFlag
);
3551 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, 1, _
, _
)).Times(0);
3552 connection_
.SendPacket(
3553 ENCRYPTION_NONE
, 1, packet
, kTestEntropyHash
, HAS_RETRANSMITTABLE_DATA
);
3556 TEST_P(QuicConnectionTest
, PublicReset
) {
3557 QuicPublicResetPacket header
;
3558 header
.public_header
.connection_id
= connection_id_
;
3559 header
.public_header
.reset_flag
= true;
3560 header
.public_header
.version_flag
= false;
3561 header
.rejected_sequence_number
= 10101;
3562 scoped_ptr
<QuicEncryptedPacket
> packet(
3563 framer_
.BuildPublicResetPacket(header
));
3564 EXPECT_CALL(visitor_
, OnConnectionClosed(QUIC_PUBLIC_RESET
, true));
3565 connection_
.ProcessUdpPacket(IPEndPoint(), IPEndPoint(), *packet
);
3568 TEST_P(QuicConnectionTest
, GoAway
) {
3569 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
3571 QuicGoAwayFrame goaway
;
3572 goaway
.last_good_stream_id
= 1;
3573 goaway
.error_code
= QUIC_PEER_GOING_AWAY
;
3574 goaway
.reason_phrase
= "Going away.";
3575 EXPECT_CALL(visitor_
, OnGoAway(_
));
3576 ProcessGoAwayPacket(&goaway
);
3579 TEST_P(QuicConnectionTest
, WindowUpdate
) {
3580 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
3582 QuicWindowUpdateFrame window_update
;
3583 window_update
.stream_id
= 3;
3584 window_update
.byte_offset
= 1234;
3585 EXPECT_CALL(visitor_
, OnWindowUpdateFrames(_
));
3586 ProcessFramePacket(QuicFrame(&window_update
));
3589 TEST_P(QuicConnectionTest
, Blocked
) {
3590 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
3592 QuicBlockedFrame blocked
;
3593 blocked
.stream_id
= 3;
3594 EXPECT_CALL(visitor_
, OnBlockedFrames(_
));
3595 ProcessFramePacket(QuicFrame(&blocked
));
3598 TEST_P(QuicConnectionTest
, ZeroBytePacket
) {
3599 // Don't close the connection for zero byte packets.
3600 EXPECT_CALL(visitor_
, OnConnectionClosed(_
, _
)).Times(0);
3601 QuicEncryptedPacket
encrypted(nullptr, 0);
3602 connection_
.ProcessUdpPacket(IPEndPoint(), IPEndPoint(), encrypted
);
3605 TEST_P(QuicConnectionTest
, MissingPacketsBeforeLeastUnacked
) {
3606 // Set the sequence number of the ack packet to be least unacked (4).
3607 QuicPacketCreatorPeer::SetSequenceNumber(&peer_creator_
, 3);
3608 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
3609 QuicStopWaitingFrame frame
= InitStopWaitingFrame(4);
3610 ProcessStopWaitingPacket(&frame
);
3611 EXPECT_TRUE(outgoing_ack()->missing_packets
.empty());
3614 TEST_P(QuicConnectionTest
, ReceivedEntropyHashCalculation
) {
3615 EXPECT_CALL(visitor_
, OnStreamFrames(_
)).Times(AtLeast(1));
3616 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
3617 ProcessDataPacket(1, 1, kEntropyFlag
);
3618 ProcessDataPacket(4, 1, kEntropyFlag
);
3619 ProcessDataPacket(3, 1, !kEntropyFlag
);
3620 ProcessDataPacket(7, 1, kEntropyFlag
);
3621 EXPECT_EQ(146u, outgoing_ack()->entropy_hash
);
3624 TEST_P(QuicConnectionTest
, ReceivedEntropyHashCalculationHalfFEC
) {
3625 // FEC packets should not change the entropy hash calculation.
3626 EXPECT_CALL(visitor_
, OnStreamFrames(_
)).Times(AtLeast(1));
3627 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
3628 ProcessDataPacket(1, 1, kEntropyFlag
);
3629 ProcessFecPacket(4, 1, false, kEntropyFlag
, nullptr);
3630 ProcessDataPacket(3, 3, !kEntropyFlag
);
3631 ProcessFecPacket(7, 3, false, kEntropyFlag
, nullptr);
3632 EXPECT_EQ(146u, outgoing_ack()->entropy_hash
);
3635 TEST_P(QuicConnectionTest
, UpdateEntropyForReceivedPackets
) {
3636 EXPECT_CALL(visitor_
, OnStreamFrames(_
)).Times(AtLeast(1));
3637 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
3638 ProcessDataPacket(1, 1, kEntropyFlag
);
3639 ProcessDataPacket(5, 1, kEntropyFlag
);
3640 ProcessDataPacket(4, 1, !kEntropyFlag
);
3641 EXPECT_EQ(34u, outgoing_ack()->entropy_hash
);
3642 // Make 4th packet my least unacked, and update entropy for 2, 3 packets.
3643 QuicPacketCreatorPeer::SetSequenceNumber(&peer_creator_
, 5);
3644 QuicPacketEntropyHash six_packet_entropy_hash
= 0;
3645 QuicPacketEntropyHash random_entropy_hash
= 129u;
3646 QuicStopWaitingFrame frame
= InitStopWaitingFrame(4);
3647 frame
.entropy_hash
= random_entropy_hash
;
3648 if (ProcessStopWaitingPacket(&frame
)) {
3649 six_packet_entropy_hash
= 1 << 6;
3652 EXPECT_EQ((random_entropy_hash
+ (1 << 5) + six_packet_entropy_hash
),
3653 outgoing_ack()->entropy_hash
);
3656 TEST_P(QuicConnectionTest
, UpdateEntropyHashUptoCurrentPacket
) {
3657 EXPECT_CALL(visitor_
, OnStreamFrames(_
)).Times(AtLeast(1));
3658 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
3659 ProcessDataPacket(1, 1, kEntropyFlag
);
3660 ProcessDataPacket(5, 1, !kEntropyFlag
);
3661 ProcessDataPacket(22, 1, kEntropyFlag
);
3662 EXPECT_EQ(66u, outgoing_ack()->entropy_hash
);
3663 QuicPacketCreatorPeer::SetSequenceNumber(&peer_creator_
, 22);
3664 QuicPacketEntropyHash random_entropy_hash
= 85u;
3665 // Current packet is the least unacked packet.
3666 QuicPacketEntropyHash ack_entropy_hash
;
3667 QuicStopWaitingFrame frame
= InitStopWaitingFrame(23);
3668 frame
.entropy_hash
= random_entropy_hash
;
3669 ack_entropy_hash
= ProcessStopWaitingPacket(&frame
);
3670 EXPECT_EQ((random_entropy_hash
+ ack_entropy_hash
),
3671 outgoing_ack()->entropy_hash
);
3672 ProcessDataPacket(25, 1, kEntropyFlag
);
3673 EXPECT_EQ((random_entropy_hash
+ ack_entropy_hash
+ (1 << (25 % 8))),
3674 outgoing_ack()->entropy_hash
);
3677 TEST_P(QuicConnectionTest
, EntropyCalculationForTruncatedAck
) {
3678 EXPECT_CALL(visitor_
, OnStreamFrames(_
)).Times(AtLeast(1));
3679 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
3680 QuicPacketEntropyHash entropy
[51];
3682 for (int i
= 1; i
< 51; ++i
) {
3683 bool should_send
= i
% 10 != 1;
3684 bool entropy_flag
= (i
& (i
- 1)) != 0;
3686 entropy
[i
] = entropy
[i
- 1];
3690 entropy
[i
] = entropy
[i
- 1] ^ (1 << (i
% 8));
3692 entropy
[i
] = entropy
[i
- 1];
3694 ProcessDataPacket(i
, 1, entropy_flag
);
3696 for (int i
= 1; i
< 50; ++i
) {
3697 EXPECT_EQ(entropy
[i
], QuicConnectionPeer::ReceivedEntropyHash(
3702 TEST_P(QuicConnectionTest
, ServerSendsVersionNegotiationPacket
) {
3703 connection_
.SetSupportedVersions(QuicSupportedVersions());
3704 framer_
.set_version_for_tests(QUIC_VERSION_UNSUPPORTED
);
3706 QuicPacketHeader header
;
3707 header
.public_header
.connection_id
= connection_id_
;
3708 header
.public_header
.version_flag
= true;
3709 header
.packet_sequence_number
= 12;
3712 frames
.push_back(QuicFrame(&frame1_
));
3713 scoped_ptr
<QuicPacket
> packet(ConstructPacket(header
, frames
));
3714 char buffer
[kMaxPacketSize
];
3715 scoped_ptr
<QuicEncryptedPacket
> encrypted(framer_
.EncryptPacket(
3716 ENCRYPTION_NONE
, 12, *packet
, buffer
, kMaxPacketSize
));
3718 framer_
.set_version(version());
3719 connection_
.set_perspective(Perspective::IS_SERVER
);
3720 connection_
.ProcessUdpPacket(IPEndPoint(), IPEndPoint(), *encrypted
);
3721 EXPECT_TRUE(writer_
->version_negotiation_packet() != nullptr);
3723 size_t num_versions
= arraysize(kSupportedQuicVersions
);
3724 ASSERT_EQ(num_versions
,
3725 writer_
->version_negotiation_packet()->versions
.size());
3727 // We expect all versions in kSupportedQuicVersions to be
3728 // included in the packet.
3729 for (size_t i
= 0; i
< num_versions
; ++i
) {
3730 EXPECT_EQ(kSupportedQuicVersions
[i
],
3731 writer_
->version_negotiation_packet()->versions
[i
]);
3735 TEST_P(QuicConnectionTest
, ServerSendsVersionNegotiationPacketSocketBlocked
) {
3736 connection_
.SetSupportedVersions(QuicSupportedVersions());
3737 framer_
.set_version_for_tests(QUIC_VERSION_UNSUPPORTED
);
3739 QuicPacketHeader header
;
3740 header
.public_header
.connection_id
= connection_id_
;
3741 header
.public_header
.version_flag
= true;
3742 header
.packet_sequence_number
= 12;
3745 frames
.push_back(QuicFrame(&frame1_
));
3746 scoped_ptr
<QuicPacket
> packet(ConstructPacket(header
, frames
));
3747 char buffer
[kMaxPacketSize
];
3748 scoped_ptr
<QuicEncryptedPacket
> encrypted(framer_
.EncryptPacket(
3749 ENCRYPTION_NONE
, 12, *packet
, buffer
, kMaxPacketSize
));
3751 framer_
.set_version(version());
3752 connection_
.set_perspective(Perspective::IS_SERVER
);
3754 connection_
.ProcessUdpPacket(IPEndPoint(), IPEndPoint(), *encrypted
);
3755 EXPECT_EQ(0u, writer_
->last_packet_size());
3756 EXPECT_TRUE(connection_
.HasQueuedData());
3758 writer_
->SetWritable();
3759 connection_
.OnCanWrite();
3760 EXPECT_TRUE(writer_
->version_negotiation_packet() != nullptr);
3762 size_t num_versions
= arraysize(kSupportedQuicVersions
);
3763 ASSERT_EQ(num_versions
,
3764 writer_
->version_negotiation_packet()->versions
.size());
3766 // We expect all versions in kSupportedQuicVersions to be
3767 // included in the packet.
3768 for (size_t i
= 0; i
< num_versions
; ++i
) {
3769 EXPECT_EQ(kSupportedQuicVersions
[i
],
3770 writer_
->version_negotiation_packet()->versions
[i
]);
3774 TEST_P(QuicConnectionTest
,
3775 ServerSendsVersionNegotiationPacketSocketBlockedDataBuffered
) {
3776 connection_
.SetSupportedVersions(QuicSupportedVersions());
3777 framer_
.set_version_for_tests(QUIC_VERSION_UNSUPPORTED
);
3779 QuicPacketHeader header
;
3780 header
.public_header
.connection_id
= connection_id_
;
3781 header
.public_header
.version_flag
= true;
3782 header
.packet_sequence_number
= 12;
3785 frames
.push_back(QuicFrame(&frame1_
));
3786 scoped_ptr
<QuicPacket
> packet(ConstructPacket(header
, frames
));
3787 char buffer
[kMaxPacketSize
];
3788 scoped_ptr
<QuicEncryptedPacket
> encrypted(framer_
.EncryptPacket(
3789 ENCRYPTION_NONE
, 12, *packet
, buffer
, kMaxPacketSize
));
3791 framer_
.set_version(version());
3792 connection_
.set_perspective(Perspective::IS_SERVER
);
3794 writer_
->set_is_write_blocked_data_buffered(true);
3795 connection_
.ProcessUdpPacket(IPEndPoint(), IPEndPoint(), *encrypted
);
3796 EXPECT_EQ(0u, writer_
->last_packet_size());
3797 EXPECT_FALSE(connection_
.HasQueuedData());
3800 TEST_P(QuicConnectionTest
, ClientHandlesVersionNegotiation
) {
3801 // Start out with some unsupported version.
3802 QuicConnectionPeer::GetFramer(&connection_
)->set_version_for_tests(
3803 QUIC_VERSION_UNSUPPORTED
);
3805 QuicPacketHeader header
;
3806 header
.public_header
.connection_id
= connection_id_
;
3807 header
.public_header
.version_flag
= true;
3808 header
.packet_sequence_number
= 12;
3810 QuicVersionVector supported_versions
;
3811 for (size_t i
= 0; i
< arraysize(kSupportedQuicVersions
); ++i
) {
3812 supported_versions
.push_back(kSupportedQuicVersions
[i
]);
3815 // Send a version negotiation packet.
3816 scoped_ptr
<QuicEncryptedPacket
> encrypted(
3817 framer_
.BuildVersionNegotiationPacket(
3818 header
.public_header
, supported_versions
));
3819 connection_
.ProcessUdpPacket(IPEndPoint(), IPEndPoint(), *encrypted
);
3821 // Now force another packet. The connection should transition into
3822 // NEGOTIATED_VERSION state and tell the packet creator to StopSendingVersion.
3823 header
.public_header
.version_flag
= false;
3825 frames
.push_back(QuicFrame(&frame1_
));
3826 scoped_ptr
<QuicPacket
> packet(ConstructPacket(header
, frames
));
3827 char buffer
[kMaxPacketSize
];
3828 encrypted
.reset(framer_
.EncryptPacket(ENCRYPTION_NONE
, 12, *packet
, buffer
,
3830 EXPECT_CALL(visitor_
, OnStreamFrames(_
)).Times(1);
3831 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
3832 connection_
.ProcessUdpPacket(IPEndPoint(), IPEndPoint(), *encrypted
);
3834 ASSERT_FALSE(QuicPacketCreatorPeer::SendVersionInPacket(creator_
));
3837 TEST_P(QuicConnectionTest
, BadVersionNegotiation
) {
3838 QuicPacketHeader header
;
3839 header
.public_header
.connection_id
= connection_id_
;
3840 header
.public_header
.version_flag
= true;
3841 header
.packet_sequence_number
= 12;
3843 QuicVersionVector supported_versions
;
3844 for (size_t i
= 0; i
< arraysize(kSupportedQuicVersions
); ++i
) {
3845 supported_versions
.push_back(kSupportedQuicVersions
[i
]);
3848 // Send a version negotiation packet with the version the client started with.
3849 // It should be rejected.
3850 EXPECT_CALL(visitor_
,
3851 OnConnectionClosed(QUIC_INVALID_VERSION_NEGOTIATION_PACKET
,
3853 scoped_ptr
<QuicEncryptedPacket
> encrypted(
3854 framer_
.BuildVersionNegotiationPacket(
3855 header
.public_header
, supported_versions
));
3856 connection_
.ProcessUdpPacket(IPEndPoint(), IPEndPoint(), *encrypted
);
3859 TEST_P(QuicConnectionTest
, CheckSendStats
) {
3860 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
));
3861 connection_
.SendStreamDataWithString(3, "first", 0, !kFin
, nullptr);
3862 size_t first_packet_size
= writer_
->last_packet_size();
3864 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
));
3865 connection_
.SendStreamDataWithString(5, "second", 0, !kFin
, nullptr);
3866 size_t second_packet_size
= writer_
->last_packet_size();
3868 // 2 retransmissions due to rto, 1 due to explicit nack.
3869 EXPECT_CALL(*send_algorithm_
, OnRetransmissionTimeout(true));
3870 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
)).Times(3);
3872 // Retransmit due to RTO.
3873 clock_
.AdvanceTime(QuicTime::Delta::FromSeconds(10));
3874 connection_
.GetRetransmissionAlarm()->Fire();
3876 // Retransmit due to explicit nacks.
3877 QuicAckFrame nack_three
= InitAckFrame(4);
3878 NackPacket(3, &nack_three
);
3879 NackPacket(1, &nack_three
);
3880 SequenceNumberSet lost_packets
;
3881 lost_packets
.insert(1);
3882 lost_packets
.insert(3);
3883 EXPECT_CALL(*loss_algorithm_
, DetectLostPackets(_
, _
, _
, _
))
3884 .WillOnce(Return(lost_packets
));
3885 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
3886 EXPECT_CALL(visitor_
, OnCanWrite()).Times(2);
3887 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
3888 ProcessAckPacket(&nack_three
);
3890 EXPECT_CALL(*send_algorithm_
, BandwidthEstimate()).WillOnce(
3891 Return(QuicBandwidth::Zero()));
3893 const QuicConnectionStats
& stats
= connection_
.GetStats();
3894 EXPECT_EQ(3 * first_packet_size
+ 2 * second_packet_size
- kQuicVersionSize
,
3896 EXPECT_EQ(5u, stats
.packets_sent
);
3897 EXPECT_EQ(2 * first_packet_size
+ second_packet_size
- kQuicVersionSize
,
3898 stats
.bytes_retransmitted
);
3899 EXPECT_EQ(3u, stats
.packets_retransmitted
);
3900 EXPECT_EQ(1u, stats
.rto_count
);
3901 EXPECT_EQ(kDefaultMaxPacketSize
, stats
.max_packet_size
);
3904 TEST_P(QuicConnectionTest
, CheckReceiveStats
) {
3905 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
3907 size_t received_bytes
= 0;
3908 received_bytes
+= ProcessFecProtectedPacket(1, false, !kEntropyFlag
);
3909 received_bytes
+= ProcessFecProtectedPacket(3, false, !kEntropyFlag
);
3910 // Should be counted against dropped packets.
3911 received_bytes
+= ProcessDataPacket(3, 1, !kEntropyFlag
);
3912 received_bytes
+= ProcessFecPacket(4, 1, true, !kEntropyFlag
, nullptr);
3914 EXPECT_CALL(*send_algorithm_
, BandwidthEstimate()).WillOnce(
3915 Return(QuicBandwidth::Zero()));
3917 const QuicConnectionStats
& stats
= connection_
.GetStats();
3918 EXPECT_EQ(received_bytes
, stats
.bytes_received
);
3919 EXPECT_EQ(4u, stats
.packets_received
);
3921 EXPECT_EQ(1u, stats
.packets_revived
);
3922 EXPECT_EQ(1u, stats
.packets_dropped
);
3925 TEST_P(QuicConnectionTest
, TestFecGroupLimits
) {
3926 // Create and return a group for 1.
3927 ASSERT_TRUE(QuicConnectionPeer::GetFecGroup(&connection_
, 1) != nullptr);
3929 // Create and return a group for 2.
3930 ASSERT_TRUE(QuicConnectionPeer::GetFecGroup(&connection_
, 2) != nullptr);
3932 // Create and return a group for 4. This should remove 1 but not 2.
3933 ASSERT_TRUE(QuicConnectionPeer::GetFecGroup(&connection_
, 4) != nullptr);
3934 ASSERT_TRUE(QuicConnectionPeer::GetFecGroup(&connection_
, 1) == nullptr);
3935 ASSERT_TRUE(QuicConnectionPeer::GetFecGroup(&connection_
, 2) != nullptr);
3937 // Create and return a group for 3. This will kill off 2.
3938 ASSERT_TRUE(QuicConnectionPeer::GetFecGroup(&connection_
, 3) != nullptr);
3939 ASSERT_TRUE(QuicConnectionPeer::GetFecGroup(&connection_
, 2) == nullptr);
3941 // Verify that adding 5 kills off 3, despite 4 being created before 3.
3942 ASSERT_TRUE(QuicConnectionPeer::GetFecGroup(&connection_
, 5) != nullptr);
3943 ASSERT_TRUE(QuicConnectionPeer::GetFecGroup(&connection_
, 4) != nullptr);
3944 ASSERT_TRUE(QuicConnectionPeer::GetFecGroup(&connection_
, 3) == nullptr);
3947 TEST_P(QuicConnectionTest
, ProcessFramesIfPacketClosedConnection
) {
3948 // Construct a packet with stream frame and connection close frame.
3949 QuicPacketHeader header
;
3950 header
.public_header
.connection_id
= connection_id_
;
3951 header
.packet_sequence_number
= 1;
3952 header
.public_header
.version_flag
= false;
3954 QuicConnectionCloseFrame qccf
;
3955 qccf
.error_code
= QUIC_PEER_GOING_AWAY
;
3958 frames
.push_back(QuicFrame(&frame1_
));
3959 frames
.push_back(QuicFrame(&qccf
));
3960 scoped_ptr
<QuicPacket
> packet(ConstructPacket(header
, frames
));
3961 EXPECT_TRUE(nullptr != packet
.get());
3962 char buffer
[kMaxPacketSize
];
3963 scoped_ptr
<QuicEncryptedPacket
> encrypted(framer_
.EncryptPacket(
3964 ENCRYPTION_NONE
, 1, *packet
, buffer
, kMaxPacketSize
));
3966 EXPECT_CALL(visitor_
, OnConnectionClosed(QUIC_PEER_GOING_AWAY
, true));
3967 EXPECT_CALL(visitor_
, OnStreamFrames(_
)).Times(1);
3968 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
3970 connection_
.ProcessUdpPacket(IPEndPoint(), IPEndPoint(), *encrypted
);
3973 TEST_P(QuicConnectionTest
, SelectMutualVersion
) {
3974 connection_
.SetSupportedVersions(QuicSupportedVersions());
3975 // Set the connection to speak the lowest quic version.
3976 connection_
.set_version(QuicVersionMin());
3977 EXPECT_EQ(QuicVersionMin(), connection_
.version());
3979 // Pass in available versions which includes a higher mutually supported
3980 // version. The higher mutually supported version should be selected.
3981 QuicVersionVector supported_versions
;
3982 for (size_t i
= 0; i
< arraysize(kSupportedQuicVersions
); ++i
) {
3983 supported_versions
.push_back(kSupportedQuicVersions
[i
]);
3985 EXPECT_TRUE(connection_
.SelectMutualVersion(supported_versions
));
3986 EXPECT_EQ(QuicVersionMax(), connection_
.version());
3988 // Expect that the lowest version is selected.
3989 // Ensure the lowest supported version is less than the max, unless they're
3991 EXPECT_LE(QuicVersionMin(), QuicVersionMax());
3992 QuicVersionVector lowest_version_vector
;
3993 lowest_version_vector
.push_back(QuicVersionMin());
3994 EXPECT_TRUE(connection_
.SelectMutualVersion(lowest_version_vector
));
3995 EXPECT_EQ(QuicVersionMin(), connection_
.version());
3997 // Shouldn't be able to find a mutually supported version.
3998 QuicVersionVector unsupported_version
;
3999 unsupported_version
.push_back(QUIC_VERSION_UNSUPPORTED
);
4000 EXPECT_FALSE(connection_
.SelectMutualVersion(unsupported_version
));
4003 TEST_P(QuicConnectionTest
, ConnectionCloseWhenWritable
) {
4004 EXPECT_FALSE(writer_
->IsWriteBlocked());
4007 connection_
.SendStreamDataWithString(1, "foo", 0, !kFin
, nullptr);
4008 EXPECT_EQ(0u, connection_
.NumQueuedPackets());
4009 EXPECT_EQ(1u, writer_
->packets_write_attempts());
4011 TriggerConnectionClose();
4012 EXPECT_EQ(2u, writer_
->packets_write_attempts());
4015 TEST_P(QuicConnectionTest
, ConnectionCloseGettingWriteBlocked
) {
4017 TriggerConnectionClose();
4018 EXPECT_EQ(1u, writer_
->packets_write_attempts());
4019 EXPECT_TRUE(writer_
->IsWriteBlocked());
4022 TEST_P(QuicConnectionTest
, ConnectionCloseWhenWriteBlocked
) {
4024 connection_
.SendStreamDataWithString(1, "foo", 0, !kFin
, nullptr);
4025 EXPECT_EQ(1u, connection_
.NumQueuedPackets());
4026 EXPECT_EQ(1u, writer_
->packets_write_attempts());
4027 EXPECT_TRUE(writer_
->IsWriteBlocked());
4028 TriggerConnectionClose();
4029 EXPECT_EQ(1u, writer_
->packets_write_attempts());
4032 TEST_P(QuicConnectionTest
, AckNotifierTriggerCallback
) {
4033 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
4035 // Create a delegate which we expect to be called.
4036 scoped_refptr
<MockAckNotifierDelegate
> delegate(new MockAckNotifierDelegate
);
4037 EXPECT_CALL(*delegate
.get(), OnAckNotification(_
, _
, _
)).Times(1);
4039 // Send some data, which will register the delegate to be notified.
4040 connection_
.SendStreamDataWithString(1, "foo", 0, !kFin
, delegate
.get());
4042 // Process an ACK from the server which should trigger the callback.
4043 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
4044 QuicAckFrame frame
= InitAckFrame(1);
4045 ProcessAckPacket(&frame
);
4048 TEST_P(QuicConnectionTest
, AckNotifierFailToTriggerCallback
) {
4049 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
4051 // Create a delegate which we don't expect to be called.
4052 scoped_refptr
<MockAckNotifierDelegate
> delegate(new MockAckNotifierDelegate
);
4053 EXPECT_CALL(*delegate
.get(), OnAckNotification(_
, _
, _
)).Times(0);
4055 // Send some data, which will register the delegate to be notified. This will
4056 // not be ACKed and so the delegate should never be called.
4057 connection_
.SendStreamDataWithString(1, "foo", 0, !kFin
, delegate
.get());
4059 // Send some other data which we will ACK.
4060 connection_
.SendStreamDataWithString(1, "foo", 0, !kFin
, nullptr);
4061 connection_
.SendStreamDataWithString(1, "bar", 0, !kFin
, nullptr);
4063 // Now we receive ACK for packets 2 and 3, but importantly missing packet 1
4064 // which we registered to be notified about.
4065 QuicAckFrame frame
= InitAckFrame(3);
4066 NackPacket(1, &frame
);
4067 SequenceNumberSet lost_packets
;
4068 lost_packets
.insert(1);
4069 EXPECT_CALL(*loss_algorithm_
, DetectLostPackets(_
, _
, _
, _
))
4070 .WillOnce(Return(lost_packets
));
4071 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
4072 ProcessAckPacket(&frame
);
4075 TEST_P(QuicConnectionTest
, AckNotifierCallbackAfterRetransmission
) {
4076 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
4078 // Create a delegate which we expect to be called.
4079 scoped_refptr
<MockAckNotifierDelegate
> delegate(new MockAckNotifierDelegate
);
4080 EXPECT_CALL(*delegate
.get(), OnAckNotification(_
, _
, _
)).Times(1);
4082 // Send four packets, and register to be notified on ACK of packet 2.
4083 connection_
.SendStreamDataWithString(3, "foo", 0, !kFin
, nullptr);
4084 connection_
.SendStreamDataWithString(3, "bar", 0, !kFin
, delegate
.get());
4085 connection_
.SendStreamDataWithString(3, "baz", 0, !kFin
, nullptr);
4086 connection_
.SendStreamDataWithString(3, "qux", 0, !kFin
, nullptr);
4088 // Now we receive ACK for packets 1, 3, and 4 and lose 2.
4089 QuicAckFrame frame
= InitAckFrame(4);
4090 NackPacket(2, &frame
);
4091 SequenceNumberSet lost_packets
;
4092 lost_packets
.insert(2);
4093 EXPECT_CALL(*loss_algorithm_
, DetectLostPackets(_
, _
, _
, _
))
4094 .WillOnce(Return(lost_packets
));
4095 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
4096 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
));
4097 ProcessAckPacket(&frame
);
4099 // Now we get an ACK for packet 5 (retransmitted packet 2), which should
4100 // trigger the callback.
4101 EXPECT_CALL(*loss_algorithm_
, DetectLostPackets(_
, _
, _
, _
))
4102 .WillRepeatedly(Return(SequenceNumberSet()));
4103 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
4104 QuicAckFrame second_ack_frame
= InitAckFrame(5);
4105 ProcessAckPacket(&second_ack_frame
);
4108 // AckNotifierCallback is triggered by the ack of a packet that timed
4109 // out and was retransmitted, even though the retransmission has a
4110 // different sequence number.
4111 TEST_P(QuicConnectionTest
, AckNotifierCallbackForAckAfterRTO
) {
4114 // Create a delegate which we expect to be called.
4115 scoped_refptr
<MockAckNotifierDelegate
> delegate(
4116 new StrictMock
<MockAckNotifierDelegate
>);
4118 QuicTime default_retransmission_time
= clock_
.ApproximateNow().Add(
4119 DefaultRetransmissionTime());
4120 connection_
.SendStreamDataWithString(3, "foo", 0, !kFin
, delegate
.get());
4121 EXPECT_EQ(1u, stop_waiting()->least_unacked
);
4123 EXPECT_EQ(1u, writer_
->header().packet_sequence_number
);
4124 EXPECT_EQ(default_retransmission_time
,
4125 connection_
.GetRetransmissionAlarm()->deadline());
4126 // Simulate the retransmission alarm firing.
4127 clock_
.AdvanceTime(DefaultRetransmissionTime());
4128 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, 2u, _
, _
));
4129 connection_
.GetRetransmissionAlarm()->Fire();
4130 EXPECT_EQ(2u, writer_
->header().packet_sequence_number
);
4131 // We do not raise the high water mark yet.
4132 EXPECT_EQ(1u, stop_waiting()->least_unacked
);
4134 // Ack the original packet, which will revert the RTO.
4135 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
4136 EXPECT_CALL(*delegate
, OnAckNotification(1, _
, _
));
4137 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
4138 QuicAckFrame ack_frame
= InitAckFrame(1);
4139 ProcessAckPacket(&ack_frame
);
4141 // Delegate is not notified again when the retransmit is acked.
4142 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
4143 QuicAckFrame second_ack_frame
= InitAckFrame(2);
4144 ProcessAckPacket(&second_ack_frame
);
4147 // AckNotifierCallback is triggered by the ack of a packet that was
4148 // previously nacked, even though the retransmission has a different
4150 TEST_P(QuicConnectionTest
, AckNotifierCallbackForAckOfNackedPacket
) {
4153 // Create a delegate which we expect to be called.
4154 scoped_refptr
<MockAckNotifierDelegate
> delegate(
4155 new StrictMock
<MockAckNotifierDelegate
>);
4157 // Send four packets, and register to be notified on ACK of packet 2.
4158 connection_
.SendStreamDataWithString(3, "foo", 0, !kFin
, nullptr);
4159 connection_
.SendStreamDataWithString(3, "bar", 0, !kFin
, delegate
.get());
4160 connection_
.SendStreamDataWithString(3, "baz", 0, !kFin
, nullptr);
4161 connection_
.SendStreamDataWithString(3, "qux", 0, !kFin
, nullptr);
4163 // Now we receive ACK for packets 1, 3, and 4 and lose 2.
4164 QuicAckFrame frame
= InitAckFrame(4);
4165 NackPacket(2, &frame
);
4166 SequenceNumberSet lost_packets
;
4167 lost_packets
.insert(2);
4168 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
4169 EXPECT_CALL(*loss_algorithm_
, DetectLostPackets(_
, _
, _
, _
))
4170 .WillOnce(Return(lost_packets
));
4171 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
4172 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
));
4173 ProcessAckPacket(&frame
);
4175 // Now we get an ACK for packet 2, which was previously nacked.
4176 SequenceNumberSet no_lost_packets
;
4177 EXPECT_CALL(*delegate
.get(), OnAckNotification(1, _
, _
));
4178 EXPECT_CALL(*loss_algorithm_
, DetectLostPackets(_
, _
, _
, _
))
4179 .WillOnce(Return(no_lost_packets
));
4180 QuicAckFrame second_ack_frame
= InitAckFrame(4);
4181 ProcessAckPacket(&second_ack_frame
);
4183 // Verify that the delegate is not notified again when the
4184 // retransmit is acked.
4185 EXPECT_CALL(*loss_algorithm_
, DetectLostPackets(_
, _
, _
, _
))
4186 .WillOnce(Return(no_lost_packets
));
4187 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
4188 QuicAckFrame third_ack_frame
= InitAckFrame(5);
4189 ProcessAckPacket(&third_ack_frame
);
4192 TEST_P(QuicConnectionTest
, AckNotifierFECTriggerCallback
) {
4193 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
4195 // Create a delegate which we expect to be called.
4196 scoped_refptr
<MockAckNotifierDelegate
> delegate(
4197 new MockAckNotifierDelegate
);
4198 EXPECT_CALL(*delegate
.get(), OnAckNotification(_
, _
, _
)).Times(1);
4200 // Send some data, which will register the delegate to be notified.
4201 connection_
.SendStreamDataWithString(1, "foo", 0, !kFin
, delegate
.get());
4202 connection_
.SendStreamDataWithString(2, "bar", 0, !kFin
, nullptr);
4204 // Process an ACK from the server with a revived packet, which should trigger
4206 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
4207 QuicAckFrame frame
= InitAckFrame(2);
4208 NackPacket(1, &frame
);
4209 frame
.revived_packets
.insert(1);
4210 ProcessAckPacket(&frame
);
4211 // If the ack is processed again, the notifier should not be called again.
4212 ProcessAckPacket(&frame
);
4215 TEST_P(QuicConnectionTest
, AckNotifierCallbackAfterFECRecovery
) {
4216 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
4217 EXPECT_CALL(visitor_
, OnCanWrite());
4219 // Create a delegate which we expect to be called.
4220 scoped_refptr
<MockAckNotifierDelegate
> delegate(new MockAckNotifierDelegate
);
4221 EXPECT_CALL(*delegate
.get(), OnAckNotification(_
, _
, _
)).Times(1);
4223 // Expect ACKs for 1 packet.
4224 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
4226 // Send one packet, and register to be notified on ACK.
4227 connection_
.SendStreamDataWithString(1, "foo", 0, !kFin
, delegate
.get());
4229 // Ack packet gets dropped, but we receive an FEC packet that covers it.
4230 // Should recover the Ack packet and trigger the notification callback.
4233 QuicAckFrame ack_frame
= InitAckFrame(1);
4234 frames
.push_back(QuicFrame(&ack_frame
));
4236 // Dummy stream frame to satisfy expectations set elsewhere.
4237 frames
.push_back(QuicFrame(&frame1_
));
4239 QuicPacketHeader ack_header
;
4240 ack_header
.public_header
.connection_id
= connection_id_
;
4241 ack_header
.public_header
.reset_flag
= false;
4242 ack_header
.public_header
.version_flag
= false;
4243 ack_header
.entropy_flag
= !kEntropyFlag
;
4244 ack_header
.fec_flag
= true;
4245 ack_header
.packet_sequence_number
= 1;
4246 ack_header
.is_in_fec_group
= IN_FEC_GROUP
;
4247 ack_header
.fec_group
= 1;
4249 QuicPacket
* packet
= BuildUnsizedDataPacket(&framer_
, ack_header
, frames
);
4251 // Take the packet which contains the ACK frame, and construct and deliver an
4252 // FEC packet which allows the ACK packet to be recovered.
4253 ProcessFecPacket(2, 1, true, !kEntropyFlag
, packet
);
4256 TEST_P(QuicConnectionTest
, NetworkChangeVisitorCwndCallbackChangesFecState
) {
4257 size_t max_packets_per_fec_group
= creator_
->max_packets_per_fec_group();
4259 QuicSentPacketManager::NetworkChangeVisitor
* visitor
=
4260 QuicSentPacketManagerPeer::GetNetworkChangeVisitor(manager_
);
4261 EXPECT_TRUE(visitor
);
4263 // Increase FEC group size by increasing congestion window to a large number.
4264 EXPECT_CALL(*send_algorithm_
, GetCongestionWindow()).WillRepeatedly(
4265 Return(1000 * kDefaultTCPMSS
));
4266 visitor
->OnCongestionWindowChange();
4267 EXPECT_LT(max_packets_per_fec_group
, creator_
->max_packets_per_fec_group());
4270 TEST_P(QuicConnectionTest
, NetworkChangeVisitorConfigCallbackChangesFecState
) {
4271 QuicSentPacketManager::NetworkChangeVisitor
* visitor
=
4272 QuicSentPacketManagerPeer::GetNetworkChangeVisitor(manager_
);
4273 EXPECT_TRUE(visitor
);
4274 EXPECT_EQ(QuicTime::Delta::Zero(),
4275 QuicPacketGeneratorPeer::GetFecTimeout(generator_
));
4277 // Verify that sending a config with a new initial rtt changes fec timeout.
4278 // Create and process a config with a non-zero initial RTT.
4279 EXPECT_CALL(*send_algorithm_
, SetFromConfig(_
, _
));
4281 config
.SetInitialRoundTripTimeUsToSend(300000);
4282 connection_
.SetFromConfig(config
);
4283 EXPECT_LT(QuicTime::Delta::Zero(),
4284 QuicPacketGeneratorPeer::GetFecTimeout(generator_
));
4287 TEST_P(QuicConnectionTest
, NetworkChangeVisitorRttCallbackChangesFecState
) {
4288 // Verify that sending a config with a new initial rtt changes fec timeout.
4289 QuicSentPacketManager::NetworkChangeVisitor
* visitor
=
4290 QuicSentPacketManagerPeer::GetNetworkChangeVisitor(manager_
);
4291 EXPECT_TRUE(visitor
);
4292 EXPECT_EQ(QuicTime::Delta::Zero(),
4293 QuicPacketGeneratorPeer::GetFecTimeout(generator_
));
4295 // Increase FEC timeout by increasing RTT.
4296 RttStats
* rtt_stats
= QuicSentPacketManagerPeer::GetRttStats(manager_
);
4297 rtt_stats
->UpdateRtt(QuicTime::Delta::FromMilliseconds(300),
4298 QuicTime::Delta::Zero(), QuicTime::Zero());
4299 visitor
->OnRttChange();
4300 EXPECT_LT(QuicTime::Delta::Zero(),
4301 QuicPacketGeneratorPeer::GetFecTimeout(generator_
));
4304 TEST_P(QuicConnectionTest
, OnPacketHeaderDebugVisitor
) {
4305 QuicPacketHeader header
;
4307 scoped_ptr
<MockQuicConnectionDebugVisitor
> debug_visitor(
4308 new MockQuicConnectionDebugVisitor());
4309 connection_
.set_debug_visitor(debug_visitor
.get());
4310 EXPECT_CALL(*debug_visitor
, OnPacketHeader(Ref(header
))).Times(1);
4311 connection_
.OnPacketHeader(header
);
4314 TEST_P(QuicConnectionTest
, Pacing
) {
4315 TestConnection
server(connection_id_
, IPEndPoint(), helper_
.get(), factory_
,
4316 Perspective::IS_SERVER
, version());
4317 TestConnection
client(connection_id_
, IPEndPoint(), helper_
.get(), factory_
,
4318 Perspective::IS_CLIENT
, version());
4319 EXPECT_FALSE(client
.sent_packet_manager().using_pacing());
4320 EXPECT_FALSE(server
.sent_packet_manager().using_pacing());
4323 TEST_P(QuicConnectionTest
, ControlFramesInstigateAcks
) {
4324 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
4326 // Send a WINDOW_UPDATE frame.
4327 QuicWindowUpdateFrame window_update
;
4328 window_update
.stream_id
= 3;
4329 window_update
.byte_offset
= 1234;
4330 EXPECT_CALL(visitor_
, OnWindowUpdateFrames(_
));
4331 ProcessFramePacket(QuicFrame(&window_update
));
4333 // Ensure that this has caused the ACK alarm to be set.
4334 QuicAlarm
* ack_alarm
= QuicConnectionPeer::GetAckAlarm(&connection_
);
4335 EXPECT_TRUE(ack_alarm
->IsSet());
4337 // Cancel alarm, and try again with BLOCKED frame.
4338 ack_alarm
->Cancel();
4339 QuicBlockedFrame blocked
;
4340 blocked
.stream_id
= 3;
4341 EXPECT_CALL(visitor_
, OnBlockedFrames(_
));
4342 ProcessFramePacket(QuicFrame(&blocked
));
4343 EXPECT_TRUE(ack_alarm
->IsSet());
4346 TEST_P(QuicConnectionTest
, NoDataNoFin
) {
4347 // Make sure that a call to SendStreamWithData, with no data and no FIN, does
4348 // not result in a QuicAckNotifier being used-after-free (fail under ASAN).
4349 // Regression test for b/18594622
4350 scoped_refptr
<MockAckNotifierDelegate
> delegate(new MockAckNotifierDelegate
);
4352 connection_
.SendStreamDataWithString(3, "", 0, !kFin
, delegate
.get()),
4353 "Attempt to send empty stream frame");