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/stl_util.h"
10 #include "net/base/net_errors.h"
11 #include "net/quic/congestion_control/loss_detection_interface.h"
12 #include "net/quic/congestion_control/receive_algorithm_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_flags.h"
18 #include "net/quic/quic_protocol.h"
19 #include "net/quic/quic_utils.h"
20 #include "net/quic/test_tools/mock_clock.h"
21 #include "net/quic/test_tools/mock_random.h"
22 #include "net/quic/test_tools/quic_connection_peer.h"
23 #include "net/quic/test_tools/quic_framer_peer.h"
24 #include "net/quic/test_tools/quic_packet_creator_peer.h"
25 #include "net/quic/test_tools/quic_sent_packet_manager_peer.h"
26 #include "net/quic/test_tools/quic_test_utils.h"
27 #include "net/quic/test_tools/simple_quic_framer.h"
28 #include "testing/gmock/include/gmock/gmock.h"
29 #include "testing/gtest/include/gtest/gtest.h"
31 using base::StringPiece
;
34 using testing::AnyNumber
;
35 using testing::AtLeast
;
36 using testing::ContainerEq
;
37 using testing::Contains
;
39 using testing::InSequence
;
40 using testing::InvokeWithoutArgs
;
41 using testing::NiceMock
;
43 using testing::Return
;
44 using testing::SaveArg
;
45 using testing::StrictMock
;
52 const char data1
[] = "foo";
53 const char data2
[] = "bar";
55 const bool kFin
= true;
56 const bool kEntropyFlag
= true;
58 const QuicPacketEntropyHash kTestEntropyHash
= 76;
60 const int kDefaultRetransmissionTimeMs
= 500;
62 class TestReceiveAlgorithm
: public ReceiveAlgorithmInterface
{
64 explicit TestReceiveAlgorithm(QuicCongestionFeedbackFrame
* feedback
)
65 : feedback_(feedback
) {
68 bool GenerateCongestionFeedback(
69 QuicCongestionFeedbackFrame
* congestion_feedback
) {
70 if (feedback_
== nullptr) {
73 *congestion_feedback
= *feedback_
;
77 MOCK_METHOD3(RecordIncomingPacket
,
78 void(QuicByteCount
, QuicPacketSequenceNumber
, QuicTime
));
81 QuicCongestionFeedbackFrame
* feedback_
;
83 DISALLOW_COPY_AND_ASSIGN(TestReceiveAlgorithm
);
86 // TaggingEncrypter appends kTagSize bytes of |tag| to the end of each message.
87 class TaggingEncrypter
: public QuicEncrypter
{
89 explicit TaggingEncrypter(uint8 tag
)
93 ~TaggingEncrypter() override
{}
95 // QuicEncrypter interface.
96 bool SetKey(StringPiece key
) override
{ return true; }
97 bool SetNoncePrefix(StringPiece nonce_prefix
) override
{ return true; }
99 bool Encrypt(StringPiece nonce
,
100 StringPiece associated_data
,
101 StringPiece plaintext
,
102 unsigned char* output
) override
{
103 memcpy(output
, plaintext
.data(), plaintext
.size());
104 output
+= plaintext
.size();
105 memset(output
, tag_
, kTagSize
);
109 QuicData
* EncryptPacket(QuicPacketSequenceNumber sequence_number
,
110 StringPiece associated_data
,
111 StringPiece plaintext
) override
{
112 const size_t len
= plaintext
.size() + kTagSize
;
113 uint8
* buffer
= new uint8
[len
];
114 Encrypt(StringPiece(), associated_data
, plaintext
, buffer
);
115 return new QuicData(reinterpret_cast<char*>(buffer
), len
, true);
118 size_t GetKeySize() const override
{ return 0; }
119 size_t GetNoncePrefixSize() const override
{ return 0; }
121 size_t GetMaxPlaintextSize(size_t ciphertext_size
) const override
{
122 return ciphertext_size
- kTagSize
;
125 size_t GetCiphertextSize(size_t plaintext_size
) const override
{
126 return plaintext_size
+ kTagSize
;
129 StringPiece
GetKey() const override
{ return StringPiece(); }
131 StringPiece
GetNoncePrefix() const override
{ return StringPiece(); }
140 DISALLOW_COPY_AND_ASSIGN(TaggingEncrypter
);
143 // TaggingDecrypter ensures that the final kTagSize bytes of the message all
144 // have the same value and then removes them.
145 class TaggingDecrypter
: public QuicDecrypter
{
147 ~TaggingDecrypter() override
{}
149 // QuicDecrypter interface
150 bool SetKey(StringPiece key
) override
{ return true; }
151 bool SetNoncePrefix(StringPiece nonce_prefix
) override
{ return true; }
153 bool Decrypt(StringPiece nonce
,
154 StringPiece associated_data
,
155 StringPiece ciphertext
,
156 unsigned char* output
,
157 size_t* output_length
) override
{
158 if (ciphertext
.size() < kTagSize
) {
161 if (!CheckTag(ciphertext
, GetTag(ciphertext
))) {
164 *output_length
= ciphertext
.size() - kTagSize
;
165 memcpy(output
, ciphertext
.data(), *output_length
);
169 QuicData
* DecryptPacket(QuicPacketSequenceNumber sequence_number
,
170 StringPiece associated_data
,
171 StringPiece ciphertext
) override
{
172 if (ciphertext
.size() < kTagSize
) {
175 if (!CheckTag(ciphertext
, GetTag(ciphertext
))) {
178 const size_t len
= ciphertext
.size() - kTagSize
;
179 uint8
* buf
= new uint8
[len
];
180 memcpy(buf
, ciphertext
.data(), len
);
181 return new QuicData(reinterpret_cast<char*>(buf
), len
,
182 true /* owns buffer */);
185 StringPiece
GetKey() const override
{ return StringPiece(); }
186 StringPiece
GetNoncePrefix() const override
{ return StringPiece(); }
189 virtual uint8
GetTag(StringPiece ciphertext
) {
190 return ciphertext
.data()[ciphertext
.size()-1];
198 bool CheckTag(StringPiece ciphertext
, uint8 tag
) {
199 for (size_t i
= ciphertext
.size() - kTagSize
; i
< ciphertext
.size(); i
++) {
200 if (ciphertext
.data()[i
] != tag
) {
209 // StringTaggingDecrypter ensures that the final kTagSize bytes of the message
210 // match the expected value.
211 class StrictTaggingDecrypter
: public TaggingDecrypter
{
213 explicit StrictTaggingDecrypter(uint8 tag
) : tag_(tag
) {}
214 ~StrictTaggingDecrypter() override
{}
216 // TaggingQuicDecrypter
217 uint8
GetTag(StringPiece ciphertext
) override
{ return tag_
; }
223 class TestConnectionHelper
: public QuicConnectionHelperInterface
{
225 class TestAlarm
: public QuicAlarm
{
227 explicit TestAlarm(QuicAlarm::Delegate
* delegate
)
228 : QuicAlarm(delegate
) {
231 void SetImpl() override
{}
232 void CancelImpl() override
{}
233 using QuicAlarm::Fire
;
236 TestConnectionHelper(MockClock
* clock
, MockRandom
* random_generator
)
238 random_generator_(random_generator
) {
239 clock_
->AdvanceTime(QuicTime::Delta::FromSeconds(1));
242 // QuicConnectionHelperInterface
243 const QuicClock
* GetClock() const override
{ return clock_
; }
245 QuicRandom
* GetRandomGenerator() override
{ return random_generator_
; }
247 QuicAlarm
* CreateAlarm(QuicAlarm::Delegate
* delegate
) override
{
248 return new TestAlarm(delegate
);
253 MockRandom
* random_generator_
;
255 DISALLOW_COPY_AND_ASSIGN(TestConnectionHelper
);
258 class TestPacketWriter
: public QuicPacketWriter
{
260 explicit TestPacketWriter(QuicVersion version
)
262 framer_(SupportedVersions(version_
)),
263 last_packet_size_(0),
264 write_blocked_(false),
265 block_on_next_write_(false),
266 is_write_blocked_data_buffered_(false),
267 final_bytes_of_last_packet_(0),
268 final_bytes_of_previous_packet_(0),
269 use_tagging_decrypter_(false),
270 packets_write_attempts_(0) {
273 // QuicPacketWriter interface
274 WriteResult
WritePacket(const char* buffer
,
276 const IPAddressNumber
& self_address
,
277 const IPEndPoint
& peer_address
) override
{
278 QuicEncryptedPacket
packet(buffer
, buf_len
);
279 ++packets_write_attempts_
;
281 if (packet
.length() >= sizeof(final_bytes_of_last_packet_
)) {
282 final_bytes_of_previous_packet_
= final_bytes_of_last_packet_
;
283 memcpy(&final_bytes_of_last_packet_
, packet
.data() + packet
.length() - 4,
284 sizeof(final_bytes_of_last_packet_
));
287 if (use_tagging_decrypter_
) {
288 framer_
.framer()->SetDecrypter(new TaggingDecrypter
, ENCRYPTION_NONE
);
290 EXPECT_TRUE(framer_
.ProcessPacket(packet
));
291 if (block_on_next_write_
) {
292 write_blocked_
= true;
293 block_on_next_write_
= false;
295 if (IsWriteBlocked()) {
296 return WriteResult(WRITE_STATUS_BLOCKED
, -1);
298 last_packet_size_
= packet
.length();
299 return WriteResult(WRITE_STATUS_OK
, last_packet_size_
);
302 bool IsWriteBlockedDataBuffered() const override
{
303 return is_write_blocked_data_buffered_
;
306 bool IsWriteBlocked() const override
{ return write_blocked_
; }
308 void SetWritable() override
{ write_blocked_
= false; }
310 void BlockOnNextWrite() { block_on_next_write_
= true; }
312 const QuicPacketHeader
& header() { return framer_
.header(); }
314 size_t frame_count() const { return framer_
.num_frames(); }
316 const vector
<QuicAckFrame
>& ack_frames() const {
317 return framer_
.ack_frames();
320 const vector
<QuicCongestionFeedbackFrame
>& feedback_frames() const {
321 return framer_
.feedback_frames();
324 const vector
<QuicStopWaitingFrame
>& stop_waiting_frames() const {
325 return framer_
.stop_waiting_frames();
328 const vector
<QuicConnectionCloseFrame
>& connection_close_frames() const {
329 return framer_
.connection_close_frames();
332 const vector
<QuicStreamFrame
>& stream_frames() const {
333 return framer_
.stream_frames();
336 const vector
<QuicPingFrame
>& ping_frames() const {
337 return framer_
.ping_frames();
340 size_t last_packet_size() {
341 return last_packet_size_
;
344 const QuicVersionNegotiationPacket
* version_negotiation_packet() {
345 return framer_
.version_negotiation_packet();
348 void set_is_write_blocked_data_buffered(bool buffered
) {
349 is_write_blocked_data_buffered_
= buffered
;
352 void set_is_server(bool is_server
) {
353 // We invert is_server here, because the framer needs to parse packets
355 QuicFramerPeer::SetIsServer(framer_
.framer(), !is_server
);
358 // final_bytes_of_last_packet_ returns the last four bytes of the previous
359 // packet as a little-endian, uint32. This is intended to be used with a
360 // TaggingEncrypter so that tests can determine which encrypter was used for
362 uint32
final_bytes_of_last_packet() { return final_bytes_of_last_packet_
; }
364 // Returns the final bytes of the second to last packet.
365 uint32
final_bytes_of_previous_packet() {
366 return final_bytes_of_previous_packet_
;
369 void use_tagging_decrypter() {
370 use_tagging_decrypter_
= true;
373 uint32
packets_write_attempts() { return packets_write_attempts_
; }
375 void Reset() { framer_
.Reset(); }
377 void SetSupportedVersions(const QuicVersionVector
& versions
) {
378 framer_
.SetSupportedVersions(versions
);
382 QuicVersion version_
;
383 SimpleQuicFramer framer_
;
384 size_t last_packet_size_
;
386 bool block_on_next_write_
;
387 bool is_write_blocked_data_buffered_
;
388 uint32 final_bytes_of_last_packet_
;
389 uint32 final_bytes_of_previous_packet_
;
390 bool use_tagging_decrypter_
;
391 uint32 packets_write_attempts_
;
393 DISALLOW_COPY_AND_ASSIGN(TestPacketWriter
);
396 class TestConnection
: public QuicConnection
{
398 TestConnection(QuicConnectionId connection_id
,
400 TestConnectionHelper
* helper
,
401 const PacketWriterFactory
& factory
,
404 : QuicConnection(connection_id
,
408 /* owns_writer= */ false,
410 SupportedVersions(version
)) {
411 // Disable tail loss probes for most tests.
412 QuicSentPacketManagerPeer::SetMaxTailLossProbes(
413 QuicConnectionPeer::GetSentPacketManager(this), 0);
414 writer()->set_is_server(is_server
);
418 QuicConnectionPeer::SendAck(this);
421 void SetReceiveAlgorithm(TestReceiveAlgorithm
* receive_algorithm
) {
422 QuicConnectionPeer::SetReceiveAlgorithm(this, receive_algorithm
);
425 void SetSendAlgorithm(SendAlgorithmInterface
* send_algorithm
) {
426 QuicConnectionPeer::SetSendAlgorithm(this, send_algorithm
);
429 void SetLossAlgorithm(LossDetectionInterface
* loss_algorithm
) {
430 QuicSentPacketManagerPeer::SetLossAlgorithm(
431 QuicConnectionPeer::GetSentPacketManager(this), loss_algorithm
);
434 void SendPacket(EncryptionLevel level
,
435 QuicPacketSequenceNumber sequence_number
,
437 QuicPacketEntropyHash entropy_hash
,
438 HasRetransmittableData retransmittable
) {
439 RetransmittableFrames
* retransmittable_frames
=
440 retransmittable
== HAS_RETRANSMITTABLE_DATA
441 ? new RetransmittableFrames()
444 SerializedPacket(sequence_number
, PACKET_6BYTE_SEQUENCE_NUMBER
,
445 packet
, entropy_hash
, retransmittable_frames
));
448 QuicConsumedData
SendStreamDataWithString(
451 QuicStreamOffset offset
,
453 QuicAckNotifier::DelegateInterface
* delegate
) {
454 return SendStreamDataWithStringHelper(id
, data
, offset
, fin
,
455 MAY_FEC_PROTECT
, delegate
);
458 QuicConsumedData
SendStreamDataWithStringWithFec(
461 QuicStreamOffset offset
,
463 QuicAckNotifier::DelegateInterface
* delegate
) {
464 return SendStreamDataWithStringHelper(id
, data
, offset
, fin
,
465 MUST_FEC_PROTECT
, delegate
);
468 QuicConsumedData
SendStreamDataWithStringHelper(
471 QuicStreamOffset offset
,
473 FecProtection fec_protection
,
474 QuicAckNotifier::DelegateInterface
* delegate
) {
477 data_iov
.Append(const_cast<char*>(data
.data()), data
.size());
479 return QuicConnection::SendStreamData(id
, data_iov
, offset
, fin
,
480 fec_protection
, delegate
);
483 QuicConsumedData
SendStreamData3() {
484 return SendStreamDataWithString(kClientDataStreamId1
, "food", 0, !kFin
,
488 QuicConsumedData
SendStreamData3WithFec() {
489 return SendStreamDataWithStringWithFec(kClientDataStreamId1
, "food", 0,
493 QuicConsumedData
SendStreamData5() {
494 return SendStreamDataWithString(kClientDataStreamId2
, "food2", 0, !kFin
,
498 QuicConsumedData
SendStreamData5WithFec() {
499 return SendStreamDataWithStringWithFec(kClientDataStreamId2
, "food2", 0,
502 // Ensures the connection can write stream data before writing.
503 QuicConsumedData
EnsureWritableAndSendStreamData5() {
504 EXPECT_TRUE(CanWriteStreamData());
505 return SendStreamData5();
508 // The crypto stream has special semantics so that it is not blocked by a
509 // congestion window limitation, and also so that it gets put into a separate
510 // packet (so that it is easier to reason about a crypto frame not being
511 // split needlessly across packet boundaries). As a result, we have separate
512 // tests for some cases for this stream.
513 QuicConsumedData
SendCryptoStreamData() {
514 return SendStreamDataWithString(kCryptoStreamId
, "chlo", 0, !kFin
, nullptr);
518 return QuicConnectionPeer::IsServer(this);
521 void set_version(QuicVersion version
) {
522 QuicConnectionPeer::GetFramer(this)->set_version(version
);
525 void SetSupportedVersions(const QuicVersionVector
& versions
) {
526 QuicConnectionPeer::GetFramer(this)->SetSupportedVersions(versions
);
527 writer()->SetSupportedVersions(versions
);
530 void set_is_server(bool is_server
) {
531 writer()->set_is_server(is_server
);
532 QuicConnectionPeer::SetIsServer(this, is_server
);
535 TestConnectionHelper::TestAlarm
* GetAckAlarm() {
536 return reinterpret_cast<TestConnectionHelper::TestAlarm
*>(
537 QuicConnectionPeer::GetAckAlarm(this));
540 TestConnectionHelper::TestAlarm
* GetPingAlarm() {
541 return reinterpret_cast<TestConnectionHelper::TestAlarm
*>(
542 QuicConnectionPeer::GetPingAlarm(this));
545 TestConnectionHelper::TestAlarm
* GetResumeWritesAlarm() {
546 return reinterpret_cast<TestConnectionHelper::TestAlarm
*>(
547 QuicConnectionPeer::GetResumeWritesAlarm(this));
550 TestConnectionHelper::TestAlarm
* GetRetransmissionAlarm() {
551 return reinterpret_cast<TestConnectionHelper::TestAlarm
*>(
552 QuicConnectionPeer::GetRetransmissionAlarm(this));
555 TestConnectionHelper::TestAlarm
* GetSendAlarm() {
556 return reinterpret_cast<TestConnectionHelper::TestAlarm
*>(
557 QuicConnectionPeer::GetSendAlarm(this));
560 TestConnectionHelper::TestAlarm
* GetTimeoutAlarm() {
561 return reinterpret_cast<TestConnectionHelper::TestAlarm
*>(
562 QuicConnectionPeer::GetTimeoutAlarm(this));
565 using QuicConnection::SelectMutualVersion
;
568 TestPacketWriter
* writer() {
569 return static_cast<TestPacketWriter
*>(QuicConnection::writer());
572 DISALLOW_COPY_AND_ASSIGN(TestConnection
);
575 // Used for testing packets revived from FEC packets.
576 class FecQuicConnectionDebugVisitor
577 : public QuicConnectionDebugVisitor
{
579 void OnRevivedPacket(const QuicPacketHeader
& header
,
580 StringPiece data
) override
{
581 revived_header_
= header
;
584 // Public accessor method.
585 QuicPacketHeader
revived_header() const {
586 return revived_header_
;
590 QuicPacketHeader revived_header_
;
593 class MockPacketWriterFactory
: public QuicConnection::PacketWriterFactory
{
595 MockPacketWriterFactory(QuicPacketWriter
* writer
) {
596 ON_CALL(*this, Create(_
)).WillByDefault(Return(writer
));
598 virtual ~MockPacketWriterFactory() {}
600 MOCK_CONST_METHOD1(Create
, QuicPacketWriter
*(QuicConnection
* connection
));
603 class QuicConnectionTest
: public ::testing::TestWithParam
<QuicVersion
> {
606 : connection_id_(42),
607 framer_(SupportedVersions(version()), QuicTime::Zero(), false),
608 peer_creator_(connection_id_
, &framer_
, &random_generator_
),
609 send_algorithm_(new StrictMock
<MockSendAlgorithm
>),
610 loss_algorithm_(new MockLossAlgorithm()),
611 helper_(new TestConnectionHelper(&clock_
, &random_generator_
)),
612 writer_(new TestPacketWriter(version())),
613 factory_(writer_
.get()),
614 connection_(connection_id_
, IPEndPoint(), helper_
.get(),
615 factory_
, false, version()),
616 frame1_(1, false, 0, MakeIOVector(data1
)),
617 frame2_(1, false, 3, MakeIOVector(data2
)),
618 sequence_number_length_(PACKET_6BYTE_SEQUENCE_NUMBER
),
619 connection_id_length_(PACKET_8BYTE_CONNECTION_ID
) {
620 connection_
.set_visitor(&visitor_
);
621 connection_
.SetSendAlgorithm(send_algorithm_
);
622 connection_
.SetLossAlgorithm(loss_algorithm_
);
623 framer_
.set_received_entropy_calculator(&entropy_calculator_
);
624 // Simplify tests by not sending feedback unless specifically configured.
625 SetFeedback(nullptr);
627 *send_algorithm_
, TimeUntilSend(_
, _
, _
)).WillRepeatedly(Return(
628 QuicTime::Delta::Zero()));
629 EXPECT_CALL(*receive_algorithm_
,
630 RecordIncomingPacket(_
, _
, _
)).Times(AnyNumber());
631 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
))
633 EXPECT_CALL(*send_algorithm_
, RetransmissionDelay()).WillRepeatedly(
634 Return(QuicTime::Delta::Zero()));
635 EXPECT_CALL(*send_algorithm_
, GetCongestionWindow()).WillRepeatedly(
636 Return(kMaxPacketSize
));
637 ON_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
))
638 .WillByDefault(Return(true));
639 EXPECT_CALL(*send_algorithm_
, HasReliableBandwidthEstimate())
641 EXPECT_CALL(*send_algorithm_
, BandwidthEstimate())
643 .WillRepeatedly(Return(QuicBandwidth::Zero()));
644 EXPECT_CALL(*send_algorithm_
, InSlowStart()).Times(AnyNumber());
645 EXPECT_CALL(*send_algorithm_
, InRecovery()).Times(AnyNumber());
646 EXPECT_CALL(visitor_
, WillingAndAbleToWrite()).Times(AnyNumber());
647 EXPECT_CALL(visitor_
, HasPendingHandshake()).Times(AnyNumber());
648 EXPECT_CALL(visitor_
, OnCanWrite()).Times(AnyNumber());
649 EXPECT_CALL(visitor_
, HasOpenDataStreams()).WillRepeatedly(Return(false));
650 EXPECT_CALL(visitor_
, OnCongestionWindowChange(_
)).Times(AnyNumber());
652 EXPECT_CALL(*loss_algorithm_
, GetLossTimeout())
653 .WillRepeatedly(Return(QuicTime::Zero()));
654 EXPECT_CALL(*loss_algorithm_
, DetectLostPackets(_
, _
, _
, _
))
655 .WillRepeatedly(Return(SequenceNumberSet()));
658 QuicVersion
version() {
662 QuicAckFrame
* outgoing_ack() {
663 outgoing_ack_
.reset(QuicConnectionPeer::CreateAckFrame(&connection_
));
664 return outgoing_ack_
.get();
667 QuicStopWaitingFrame
* stop_waiting() {
669 QuicConnectionPeer::CreateStopWaitingFrame(&connection_
));
670 return stop_waiting_
.get();
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(&peer_creator_
,
693 connection_
.is_server());
694 SerializedPacket serialized_packet
=
695 peer_creator_
.SerializeAllFrames(frames
);
696 scoped_ptr
<QuicPacket
> packet(serialized_packet
.packet
);
697 scoped_ptr
<QuicEncryptedPacket
> encrypted(
698 framer_
.EncryptPacket(ENCRYPTION_NONE
,
699 serialized_packet
.sequence_number
, *packet
));
700 connection_
.ProcessUdpPacket(IPEndPoint(), IPEndPoint(), *encrypted
);
701 return serialized_packet
.entropy_hash
;
704 size_t ProcessDataPacket(QuicPacketSequenceNumber number
,
705 QuicFecGroupNumber fec_group
,
707 return ProcessDataPacketAtLevel(number
, fec_group
, entropy_flag
,
711 size_t ProcessDataPacketAtLevel(QuicPacketSequenceNumber number
,
712 QuicFecGroupNumber fec_group
,
714 EncryptionLevel level
) {
715 scoped_ptr
<QuicPacket
> packet(ConstructDataPacket(number
, fec_group
,
717 scoped_ptr
<QuicEncryptedPacket
> encrypted(framer_
.EncryptPacket(
718 level
, number
, *packet
));
719 connection_
.ProcessUdpPacket(IPEndPoint(), IPEndPoint(), *encrypted
);
720 return encrypted
->length();
723 void ProcessPingPacket(QuicPacketSequenceNumber number
) {
724 scoped_ptr
<QuicPacket
> packet(ConstructPingPacket(number
));
725 scoped_ptr
<QuicEncryptedPacket
> encrypted(framer_
.EncryptPacket(
726 ENCRYPTION_NONE
, number
, *packet
));
727 connection_
.ProcessUdpPacket(IPEndPoint(), IPEndPoint(), *encrypted
);
730 void ProcessClosePacket(QuicPacketSequenceNumber number
,
731 QuicFecGroupNumber fec_group
) {
732 scoped_ptr
<QuicPacket
> packet(ConstructClosePacket(number
, fec_group
));
733 scoped_ptr
<QuicEncryptedPacket
> encrypted(framer_
.EncryptPacket(
734 ENCRYPTION_NONE
, number
, *packet
));
735 connection_
.ProcessUdpPacket(IPEndPoint(), IPEndPoint(), *encrypted
);
738 size_t ProcessFecProtectedPacket(QuicPacketSequenceNumber number
,
739 bool expect_revival
, bool entropy_flag
) {
740 if (expect_revival
) {
741 EXPECT_CALL(visitor_
, OnStreamFrames(_
)).Times(1);
743 EXPECT_CALL(visitor_
, OnStreamFrames(_
)).Times(1).
744 RetiresOnSaturation();
745 return ProcessDataPacket(number
, 1, entropy_flag
);
748 // Processes an FEC packet that covers the packets that would have been
750 size_t ProcessFecPacket(QuicPacketSequenceNumber number
,
751 QuicPacketSequenceNumber min_protected_packet
,
754 QuicPacket
* packet
) {
755 if (expect_revival
) {
756 EXPECT_CALL(visitor_
, OnStreamFrames(_
)).Times(1);
759 // Construct the decrypted data packet so we can compute the correct
760 // redundancy. If |packet| has been provided then use that, otherwise
761 // construct a default data packet.
762 scoped_ptr
<QuicPacket
> data_packet
;
764 data_packet
.reset(packet
);
766 data_packet
.reset(ConstructDataPacket(number
, 1, !kEntropyFlag
));
769 header_
.public_header
.connection_id
= connection_id_
;
770 header_
.public_header
.reset_flag
= false;
771 header_
.public_header
.version_flag
= false;
772 header_
.public_header
.sequence_number_length
= sequence_number_length_
;
773 header_
.public_header
.connection_id_length
= connection_id_length_
;
774 header_
.packet_sequence_number
= number
;
775 header_
.entropy_flag
= entropy_flag
;
776 header_
.fec_flag
= true;
777 header_
.is_in_fec_group
= IN_FEC_GROUP
;
778 header_
.fec_group
= min_protected_packet
;
779 QuicFecData fec_data
;
780 fec_data
.fec_group
= header_
.fec_group
;
782 // Since all data packets in this test have the same payload, the
783 // redundancy is either equal to that payload or the xor of that payload
784 // with itself, depending on the number of packets.
785 if (((number
- min_protected_packet
) % 2) == 0) {
786 for (size_t i
= GetStartOfFecProtectedData(
787 header_
.public_header
.connection_id_length
,
788 header_
.public_header
.version_flag
,
789 header_
.public_header
.sequence_number_length
);
790 i
< data_packet
->length(); ++i
) {
791 data_packet
->mutable_data()[i
] ^= data_packet
->data()[i
];
794 fec_data
.redundancy
= data_packet
->FecProtectedData();
796 scoped_ptr
<QuicPacket
> fec_packet(
797 framer_
.BuildFecPacket(header_
, fec_data
).packet
);
798 scoped_ptr
<QuicEncryptedPacket
> encrypted(
799 framer_
.EncryptPacket(ENCRYPTION_NONE
, number
, *fec_packet
));
801 connection_
.ProcessUdpPacket(IPEndPoint(), IPEndPoint(), *encrypted
);
802 return encrypted
->length();
805 QuicByteCount
SendStreamDataToPeer(QuicStreamId id
,
807 QuicStreamOffset offset
,
809 QuicPacketSequenceNumber
* last_packet
) {
810 QuicByteCount packet_size
;
811 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
))
812 .WillOnce(DoAll(SaveArg
<3>(&packet_size
), Return(true)));
813 connection_
.SendStreamDataWithString(id
, data
, offset
, fin
, nullptr);
814 if (last_packet
!= nullptr) {
816 QuicConnectionPeer::GetPacketCreator(&connection_
)->sequence_number();
818 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
))
823 void SendAckPacketToPeer() {
824 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
)).Times(1);
825 connection_
.SendAck();
826 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
))
830 QuicPacketEntropyHash
ProcessAckPacket(QuicAckFrame
* frame
) {
831 return ProcessFramePacket(QuicFrame(frame
));
834 QuicPacketEntropyHash
ProcessStopWaitingPacket(QuicStopWaitingFrame
* frame
) {
835 return ProcessFramePacket(QuicFrame(frame
));
838 QuicPacketEntropyHash
ProcessGoAwayPacket(QuicGoAwayFrame
* frame
) {
839 return ProcessFramePacket(QuicFrame(frame
));
842 bool IsMissing(QuicPacketSequenceNumber number
) {
843 return IsAwaitingPacket(*outgoing_ack(), number
);
846 QuicPacket
* ConstructDataPacket(QuicPacketSequenceNumber number
,
847 QuicFecGroupNumber fec_group
,
849 header_
.public_header
.connection_id
= connection_id_
;
850 header_
.public_header
.reset_flag
= false;
851 header_
.public_header
.version_flag
= false;
852 header_
.public_header
.sequence_number_length
= sequence_number_length_
;
853 header_
.public_header
.connection_id_length
= connection_id_length_
;
854 header_
.entropy_flag
= entropy_flag
;
855 header_
.fec_flag
= false;
856 header_
.packet_sequence_number
= number
;
857 header_
.is_in_fec_group
= fec_group
== 0u ? NOT_IN_FEC_GROUP
: IN_FEC_GROUP
;
858 header_
.fec_group
= fec_group
;
861 QuicFrame
frame(&frame1_
);
862 frames
.push_back(frame
);
864 BuildUnsizedDataPacket(&framer_
, header_
, frames
).packet
;
865 EXPECT_TRUE(packet
!= nullptr);
869 QuicPacket
* ConstructPingPacket(QuicPacketSequenceNumber number
) {
870 header_
.public_header
.connection_id
= connection_id_
;
871 header_
.packet_sequence_number
= number
;
872 header_
.public_header
.reset_flag
= false;
873 header_
.public_header
.version_flag
= false;
874 header_
.entropy_flag
= false;
875 header_
.fec_flag
= false;
876 header_
.is_in_fec_group
= NOT_IN_FEC_GROUP
;
877 header_
.fec_group
= 0;
882 QuicFrame
frame(&ping
);
883 frames
.push_back(frame
);
885 BuildUnsizedDataPacket(&framer_
, header_
, frames
).packet
;
886 EXPECT_TRUE(packet
!= nullptr);
890 QuicPacket
* ConstructClosePacket(QuicPacketSequenceNumber number
,
891 QuicFecGroupNumber fec_group
) {
892 header_
.public_header
.connection_id
= connection_id_
;
893 header_
.packet_sequence_number
= number
;
894 header_
.public_header
.reset_flag
= false;
895 header_
.public_header
.version_flag
= false;
896 header_
.entropy_flag
= false;
897 header_
.fec_flag
= false;
898 header_
.is_in_fec_group
= fec_group
== 0u ? NOT_IN_FEC_GROUP
: IN_FEC_GROUP
;
899 header_
.fec_group
= fec_group
;
901 QuicConnectionCloseFrame qccf
;
902 qccf
.error_code
= QUIC_PEER_GOING_AWAY
;
905 QuicFrame
frame(&qccf
);
906 frames
.push_back(frame
);
908 BuildUnsizedDataPacket(&framer_
, header_
, frames
).packet
;
909 EXPECT_TRUE(packet
!= nullptr);
913 void SetFeedback(QuicCongestionFeedbackFrame
* feedback
) {
914 receive_algorithm_
= new TestReceiveAlgorithm(feedback
);
915 connection_
.SetReceiveAlgorithm(receive_algorithm_
);
918 QuicTime::Delta
DefaultRetransmissionTime() {
919 return QuicTime::Delta::FromMilliseconds(kDefaultRetransmissionTimeMs
);
922 QuicTime::Delta
DefaultDelayedAckTime() {
923 return QuicTime::Delta::FromMilliseconds(kMaxDelayedAckTimeMs
);
926 // Initialize a frame acknowledging all packets up to largest_observed.
927 const QuicAckFrame
InitAckFrame(QuicPacketSequenceNumber largest_observed
) {
928 QuicAckFrame
frame(MakeAckFrame(largest_observed
));
929 if (largest_observed
> 0) {
931 QuicConnectionPeer::GetSentEntropyHash(&connection_
,
937 const QuicStopWaitingFrame
InitStopWaitingFrame(
938 QuicPacketSequenceNumber least_unacked
) {
939 QuicStopWaitingFrame frame
;
940 frame
.least_unacked
= least_unacked
;
944 // Explicitly nack a packet.
945 void NackPacket(QuicPacketSequenceNumber missing
, QuicAckFrame
* frame
) {
946 frame
->missing_packets
.insert(missing
);
947 frame
->entropy_hash
^=
948 QuicConnectionPeer::PacketEntropy(&connection_
, missing
);
951 // Undo nacking a packet within the frame.
952 void AckPacket(QuicPacketSequenceNumber arrived
, QuicAckFrame
* frame
) {
953 EXPECT_THAT(frame
->missing_packets
, Contains(arrived
));
954 frame
->missing_packets
.erase(arrived
);
955 frame
->entropy_hash
^=
956 QuicConnectionPeer::PacketEntropy(&connection_
, arrived
);
959 void TriggerConnectionClose() {
960 // Send an erroneous packet to close the connection.
961 EXPECT_CALL(visitor_
,
962 OnConnectionClosed(QUIC_INVALID_PACKET_HEADER
, false));
963 // Call ProcessDataPacket rather than ProcessPacket, as we should not get a
964 // packet call to the visitor.
965 ProcessDataPacket(6000, 0, !kEntropyFlag
);
966 EXPECT_FALSE(QuicConnectionPeer::GetConnectionClosePacket(&connection_
) ==
970 void BlockOnNextWrite() {
971 writer_
->BlockOnNextWrite();
972 EXPECT_CALL(visitor_
, OnWriteBlocked()).Times(AtLeast(1));
975 void CongestionBlockWrites() {
976 EXPECT_CALL(*send_algorithm_
,
977 TimeUntilSend(_
, _
, _
)).WillRepeatedly(
978 testing::Return(QuicTime::Delta::FromSeconds(1)));
981 void CongestionUnblockWrites() {
982 EXPECT_CALL(*send_algorithm_
,
983 TimeUntilSend(_
, _
, _
)).WillRepeatedly(
984 testing::Return(QuicTime::Delta::Zero()));
987 QuicConnectionId connection_id_
;
989 QuicPacketCreator peer_creator_
;
990 MockEntropyCalculator entropy_calculator_
;
992 MockSendAlgorithm
* send_algorithm_
;
993 MockLossAlgorithm
* loss_algorithm_
;
994 TestReceiveAlgorithm
* receive_algorithm_
;
996 MockRandom random_generator_
;
997 scoped_ptr
<TestConnectionHelper
> helper_
;
998 scoped_ptr
<TestPacketWriter
> writer_
;
999 NiceMock
<MockPacketWriterFactory
> factory_
;
1000 TestConnection connection_
;
1001 StrictMock
<MockConnectionVisitor
> visitor_
;
1003 QuicPacketHeader header_
;
1004 QuicStreamFrame frame1_
;
1005 QuicStreamFrame frame2_
;
1006 scoped_ptr
<QuicAckFrame
> outgoing_ack_
;
1007 scoped_ptr
<QuicStopWaitingFrame
> stop_waiting_
;
1008 QuicSequenceNumberLength sequence_number_length_
;
1009 QuicConnectionIdLength connection_id_length_
;
1012 DISALLOW_COPY_AND_ASSIGN(QuicConnectionTest
);
1015 // Run all end to end tests with all supported versions.
1016 INSTANTIATE_TEST_CASE_P(SupportedVersion
,
1018 ::testing::ValuesIn(QuicSupportedVersions()));
1020 TEST_P(QuicConnectionTest
, PacketsInOrder
) {
1021 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
1024 EXPECT_EQ(1u, outgoing_ack()->largest_observed
);
1025 EXPECT_EQ(0u, outgoing_ack()->missing_packets
.size());
1028 EXPECT_EQ(2u, outgoing_ack()->largest_observed
);
1029 EXPECT_EQ(0u, outgoing_ack()->missing_packets
.size());
1032 EXPECT_EQ(3u, outgoing_ack()->largest_observed
);
1033 EXPECT_EQ(0u, outgoing_ack()->missing_packets
.size());
1036 TEST_P(QuicConnectionTest
, PacketsOutOfOrder
) {
1037 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
1040 EXPECT_EQ(3u, outgoing_ack()->largest_observed
);
1041 EXPECT_TRUE(IsMissing(2));
1042 EXPECT_TRUE(IsMissing(1));
1045 EXPECT_EQ(3u, outgoing_ack()->largest_observed
);
1046 EXPECT_FALSE(IsMissing(2));
1047 EXPECT_TRUE(IsMissing(1));
1050 EXPECT_EQ(3u, outgoing_ack()->largest_observed
);
1051 EXPECT_FALSE(IsMissing(2));
1052 EXPECT_FALSE(IsMissing(1));
1055 TEST_P(QuicConnectionTest
, DuplicatePacket
) {
1056 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
1059 EXPECT_EQ(3u, outgoing_ack()->largest_observed
);
1060 EXPECT_TRUE(IsMissing(2));
1061 EXPECT_TRUE(IsMissing(1));
1063 // Send packet 3 again, but do not set the expectation that
1064 // the visitor OnStreamFrames() will be called.
1065 ProcessDataPacket(3, 0, !kEntropyFlag
);
1066 EXPECT_EQ(3u, outgoing_ack()->largest_observed
);
1067 EXPECT_TRUE(IsMissing(2));
1068 EXPECT_TRUE(IsMissing(1));
1071 TEST_P(QuicConnectionTest
, PacketsOutOfOrderWithAdditionsAndLeastAwaiting
) {
1072 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
1075 EXPECT_EQ(3u, outgoing_ack()->largest_observed
);
1076 EXPECT_TRUE(IsMissing(2));
1077 EXPECT_TRUE(IsMissing(1));
1080 EXPECT_EQ(3u, outgoing_ack()->largest_observed
);
1081 EXPECT_TRUE(IsMissing(1));
1084 EXPECT_EQ(5u, outgoing_ack()->largest_observed
);
1085 EXPECT_TRUE(IsMissing(1));
1086 EXPECT_TRUE(IsMissing(4));
1088 // Pretend at this point the client has gotten acks for 2 and 3 and 1 is a
1089 // packet the peer will not retransmit. It indicates this by sending 'least
1090 // awaiting' is 4. The connection should then realize 1 will not be
1091 // retransmitted, and will remove it from the missing list.
1092 peer_creator_
.set_sequence_number(5);
1093 QuicAckFrame frame
= InitAckFrame(1);
1094 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(_
, _
, _
, _
));
1095 ProcessAckPacket(&frame
);
1097 // Force an ack to be sent.
1098 SendAckPacketToPeer();
1099 EXPECT_TRUE(IsMissing(4));
1102 TEST_P(QuicConnectionTest
, RejectPacketTooFarOut
) {
1103 EXPECT_CALL(visitor_
,
1104 OnConnectionClosed(QUIC_INVALID_PACKET_HEADER
, false));
1105 // Call ProcessDataPacket rather than ProcessPacket, as we should not get a
1106 // packet call to the visitor.
1107 ProcessDataPacket(6000, 0, !kEntropyFlag
);
1108 EXPECT_FALSE(QuicConnectionPeer::GetConnectionClosePacket(&connection_
) ==
1112 TEST_P(QuicConnectionTest
, RejectUnencryptedStreamData
) {
1113 // Process an unencrypted packet from the non-crypto stream.
1114 frame1_
.stream_id
= 3;
1115 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
1116 EXPECT_CALL(visitor_
, OnConnectionClosed(QUIC_UNENCRYPTED_STREAM_DATA
,
1118 ProcessDataPacket(1, 0, !kEntropyFlag
);
1119 EXPECT_FALSE(QuicConnectionPeer::GetConnectionClosePacket(&connection_
) ==
1121 const vector
<QuicConnectionCloseFrame
>& connection_close_frames
=
1122 writer_
->connection_close_frames();
1123 EXPECT_EQ(1u, connection_close_frames
.size());
1124 EXPECT_EQ(QUIC_UNENCRYPTED_STREAM_DATA
,
1125 connection_close_frames
[0].error_code
);
1128 TEST_P(QuicConnectionTest
, TruncatedAck
) {
1129 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
1130 QuicPacketSequenceNumber num_packets
= 256 * 2 + 1;
1131 for (QuicPacketSequenceNumber i
= 0; i
< num_packets
; ++i
) {
1132 SendStreamDataToPeer(3, "foo", i
* 3, !kFin
, nullptr);
1135 QuicAckFrame frame
= InitAckFrame(num_packets
);
1136 SequenceNumberSet lost_packets
;
1137 // Create an ack with 256 nacks, none adjacent to one another.
1138 for (QuicPacketSequenceNumber i
= 1; i
<= 256; ++i
) {
1139 NackPacket(i
* 2, &frame
);
1140 if (i
< 256) { // Last packet is nacked, but not lost.
1141 lost_packets
.insert(i
* 2);
1144 EXPECT_CALL(*loss_algorithm_
, DetectLostPackets(_
, _
, _
, _
))
1145 .WillOnce(Return(lost_packets
));
1146 EXPECT_CALL(entropy_calculator_
,
1147 EntropyHash(511)).WillOnce(testing::Return(0));
1148 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
1149 ProcessAckPacket(&frame
);
1151 const QuicSentPacketManager
& sent_packet_manager
=
1152 connection_
.sent_packet_manager();
1153 // A truncated ack will not have the true largest observed.
1154 EXPECT_GT(num_packets
, sent_packet_manager
.largest_observed());
1156 AckPacket(192, &frame
);
1158 // Removing one missing packet allows us to ack 192 and one more range, but
1159 // 192 has already been declared lost, so it doesn't register as an ack.
1160 EXPECT_CALL(*loss_algorithm_
, DetectLostPackets(_
, _
, _
, _
))
1161 .WillOnce(Return(SequenceNumberSet()));
1162 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
1163 ProcessAckPacket(&frame
);
1164 EXPECT_EQ(num_packets
, sent_packet_manager
.largest_observed());
1167 TEST_P(QuicConnectionTest
, AckReceiptCausesAckSendBadEntropy
) {
1168 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
1171 // Delay sending, then queue up an ack.
1172 EXPECT_CALL(*send_algorithm_
,
1173 TimeUntilSend(_
, _
, _
)).WillOnce(
1174 testing::Return(QuicTime::Delta::FromMicroseconds(1)));
1175 QuicConnectionPeer::SendAck(&connection_
);
1177 // Process an ack with a least unacked of the received ack.
1178 // This causes an ack to be sent when TimeUntilSend returns 0.
1179 EXPECT_CALL(*send_algorithm_
,
1180 TimeUntilSend(_
, _
, _
)).WillRepeatedly(
1181 testing::Return(QuicTime::Delta::Zero()));
1182 // Skip a packet and then record an ack.
1183 peer_creator_
.set_sequence_number(2);
1184 QuicAckFrame frame
= InitAckFrame(0);
1185 ProcessAckPacket(&frame
);
1188 TEST_P(QuicConnectionTest
, OutOfOrderReceiptCausesAckSend
) {
1189 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
1192 // Should ack immediately since we have missing packets.
1193 EXPECT_EQ(1u, writer_
->packets_write_attempts());
1196 // Should ack immediately since we have missing packets.
1197 EXPECT_EQ(2u, writer_
->packets_write_attempts());
1200 // Should ack immediately, since this fills the last hole.
1201 EXPECT_EQ(3u, writer_
->packets_write_attempts());
1204 // Should not cause an ack.
1205 EXPECT_EQ(3u, writer_
->packets_write_attempts());
1208 TEST_P(QuicConnectionTest
, AckReceiptCausesAckSend
) {
1209 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
1211 QuicPacketSequenceNumber original
;
1212 QuicByteCount packet_size
;
1213 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
))
1214 .WillOnce(DoAll(SaveArg
<2>(&original
), SaveArg
<3>(&packet_size
),
1216 connection_
.SendStreamDataWithString(3, "foo", 0, !kFin
, nullptr);
1217 QuicAckFrame frame
= InitAckFrame(original
);
1218 NackPacket(original
, &frame
);
1219 // First nack triggers early retransmit.
1220 SequenceNumberSet lost_packets
;
1221 lost_packets
.insert(1);
1222 EXPECT_CALL(*loss_algorithm_
, DetectLostPackets(_
, _
, _
, _
))
1223 .WillOnce(Return(lost_packets
));
1224 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
1225 QuicPacketSequenceNumber retransmission
;
1226 EXPECT_CALL(*send_algorithm_
,
1227 OnPacketSent(_
, _
, _
, packet_size
- kQuicVersionSize
, _
))
1228 .WillOnce(DoAll(SaveArg
<2>(&retransmission
), Return(true)));
1230 ProcessAckPacket(&frame
);
1232 QuicAckFrame frame2
= InitAckFrame(retransmission
);
1233 NackPacket(original
, &frame2
);
1234 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
1235 EXPECT_CALL(*loss_algorithm_
, DetectLostPackets(_
, _
, _
, _
))
1236 .WillOnce(Return(SequenceNumberSet()));
1237 ProcessAckPacket(&frame2
);
1239 // Now if the peer sends an ack which still reports the retransmitted packet
1240 // as missing, that will bundle an ack with data after two acks in a row
1241 // indicate the high water mark needs to be raised.
1242 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
,
1243 HAS_RETRANSMITTABLE_DATA
));
1244 connection_
.SendStreamDataWithString(3, "foo", 3, !kFin
, nullptr);
1246 EXPECT_EQ(1u, writer_
->frame_count());
1247 EXPECT_EQ(1u, writer_
->stream_frames().size());
1249 // No more packet loss for the rest of the test.
1250 EXPECT_CALL(*loss_algorithm_
, DetectLostPackets(_
, _
, _
, _
))
1251 .WillRepeatedly(Return(SequenceNumberSet()));
1252 ProcessAckPacket(&frame2
);
1253 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
,
1254 HAS_RETRANSMITTABLE_DATA
));
1255 connection_
.SendStreamDataWithString(3, "foo", 3, !kFin
, nullptr);
1257 EXPECT_EQ(3u, writer_
->frame_count());
1258 EXPECT_EQ(1u, writer_
->stream_frames().size());
1259 EXPECT_FALSE(writer_
->ack_frames().empty());
1261 // But an ack with no missing packets will not send an ack.
1262 AckPacket(original
, &frame2
);
1263 ProcessAckPacket(&frame2
);
1264 ProcessAckPacket(&frame2
);
1267 TEST_P(QuicConnectionTest
, 20AcksCausesAckSend
) {
1268 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
1270 SendStreamDataToPeer(1, "foo", 0, !kFin
, nullptr);
1272 QuicAlarm
* ack_alarm
= QuicConnectionPeer::GetAckAlarm(&connection_
);
1273 // But an ack with no missing packets will not send an ack.
1274 QuicAckFrame frame
= InitAckFrame(1);
1275 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
1276 EXPECT_CALL(*loss_algorithm_
, DetectLostPackets(_
, _
, _
, _
))
1277 .WillRepeatedly(Return(SequenceNumberSet()));
1278 for (int i
= 0; i
< 20; ++i
) {
1279 EXPECT_FALSE(ack_alarm
->IsSet());
1280 ProcessAckPacket(&frame
);
1282 EXPECT_TRUE(ack_alarm
->IsSet());
1285 TEST_P(QuicConnectionTest
, LeastUnackedLower
) {
1286 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
1288 SendStreamDataToPeer(1, "foo", 0, !kFin
, nullptr);
1289 SendStreamDataToPeer(1, "bar", 3, !kFin
, nullptr);
1290 SendStreamDataToPeer(1, "eep", 6, !kFin
, nullptr);
1292 // Start out saying the least unacked is 2.
1293 peer_creator_
.set_sequence_number(5);
1294 QuicStopWaitingFrame frame
= InitStopWaitingFrame(2);
1295 ProcessStopWaitingPacket(&frame
);
1297 // Change it to 1, but lower the sequence number to fake out-of-order packets.
1298 // This should be fine.
1299 peer_creator_
.set_sequence_number(1);
1300 // The scheduler will not process out of order acks, but all packet processing
1301 // causes the connection to try to write.
1302 EXPECT_CALL(visitor_
, OnCanWrite());
1303 QuicStopWaitingFrame frame2
= InitStopWaitingFrame(1);
1304 ProcessStopWaitingPacket(&frame2
);
1306 // Now claim it's one, but set the ordering so it was sent "after" the first
1307 // one. This should cause a connection error.
1308 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
));
1309 peer_creator_
.set_sequence_number(7);
1310 EXPECT_CALL(visitor_
,
1311 OnConnectionClosed(QUIC_INVALID_STOP_WAITING_DATA
, false));
1312 QuicStopWaitingFrame frame3
= InitStopWaitingFrame(1);
1313 ProcessStopWaitingPacket(&frame3
);
1316 TEST_P(QuicConnectionTest
, LargestObservedLower
) {
1317 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
1319 SendStreamDataToPeer(1, "foo", 0, !kFin
, nullptr);
1320 SendStreamDataToPeer(1, "bar", 3, !kFin
, nullptr);
1321 SendStreamDataToPeer(1, "eep", 6, !kFin
, nullptr);
1322 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
1324 // Start out saying the largest observed is 2.
1325 QuicAckFrame frame1
= InitAckFrame(1);
1326 QuicAckFrame frame2
= InitAckFrame(2);
1327 ProcessAckPacket(&frame2
);
1329 // Now change it to 1, and it should cause a connection error.
1330 EXPECT_CALL(visitor_
, OnConnectionClosed(QUIC_INVALID_ACK_DATA
, false));
1331 EXPECT_CALL(visitor_
, OnCanWrite()).Times(0);
1332 ProcessAckPacket(&frame1
);
1335 TEST_P(QuicConnectionTest
, AckUnsentData
) {
1336 // Ack a packet which has not been sent.
1337 EXPECT_CALL(visitor_
, OnConnectionClosed(QUIC_INVALID_ACK_DATA
, false));
1338 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
1339 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
));
1340 QuicAckFrame
frame(MakeAckFrame(1));
1341 EXPECT_CALL(visitor_
, OnCanWrite()).Times(0);
1342 ProcessAckPacket(&frame
);
1345 TEST_P(QuicConnectionTest
, AckAll
) {
1346 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
1349 peer_creator_
.set_sequence_number(1);
1350 QuicAckFrame frame1
= InitAckFrame(0);
1351 ProcessAckPacket(&frame1
);
1354 TEST_P(QuicConnectionTest
, SendingDifferentSequenceNumberLengthsBandwidth
) {
1355 QuicPacketSequenceNumber last_packet
;
1356 QuicPacketCreator
* creator
=
1357 QuicConnectionPeer::GetPacketCreator(&connection_
);
1358 SendStreamDataToPeer(1, "foo", 0, !kFin
, &last_packet
);
1359 EXPECT_EQ(1u, last_packet
);
1360 EXPECT_EQ(PACKET_1BYTE_SEQUENCE_NUMBER
,
1361 creator
->next_sequence_number_length());
1362 EXPECT_EQ(PACKET_1BYTE_SEQUENCE_NUMBER
,
1363 writer_
->header().public_header
.sequence_number_length
);
1365 EXPECT_CALL(*send_algorithm_
, GetCongestionWindow()).WillRepeatedly(
1366 Return(kMaxPacketSize
* 256));
1368 SendStreamDataToPeer(1, "bar", 3, !kFin
, &last_packet
);
1369 EXPECT_EQ(2u, last_packet
);
1370 EXPECT_EQ(PACKET_2BYTE_SEQUENCE_NUMBER
,
1371 creator
->next_sequence_number_length());
1372 // The 1 packet lag is due to the sequence number length being recalculated in
1373 // QuicConnection after a packet is sent.
1374 EXPECT_EQ(PACKET_1BYTE_SEQUENCE_NUMBER
,
1375 writer_
->header().public_header
.sequence_number_length
);
1377 EXPECT_CALL(*send_algorithm_
, GetCongestionWindow()).WillRepeatedly(
1378 Return(kMaxPacketSize
* 256 * 256));
1380 SendStreamDataToPeer(1, "foo", 6, !kFin
, &last_packet
);
1381 EXPECT_EQ(3u, last_packet
);
1382 EXPECT_EQ(PACKET_4BYTE_SEQUENCE_NUMBER
,
1383 creator
->next_sequence_number_length());
1384 EXPECT_EQ(PACKET_2BYTE_SEQUENCE_NUMBER
,
1385 writer_
->header().public_header
.sequence_number_length
);
1387 EXPECT_CALL(*send_algorithm_
, GetCongestionWindow()).WillRepeatedly(
1388 Return(kMaxPacketSize
* 256 * 256 * 256));
1390 SendStreamDataToPeer(1, "bar", 9, !kFin
, &last_packet
);
1391 EXPECT_EQ(4u, last_packet
);
1392 EXPECT_EQ(PACKET_4BYTE_SEQUENCE_NUMBER
,
1393 creator
->next_sequence_number_length());
1394 EXPECT_EQ(PACKET_4BYTE_SEQUENCE_NUMBER
,
1395 writer_
->header().public_header
.sequence_number_length
);
1397 EXPECT_CALL(*send_algorithm_
, GetCongestionWindow()).WillRepeatedly(
1398 Return(kMaxPacketSize
* 256 * 256 * 256 * 256));
1400 SendStreamDataToPeer(1, "foo", 12, !kFin
, &last_packet
);
1401 EXPECT_EQ(5u, last_packet
);
1402 EXPECT_EQ(PACKET_6BYTE_SEQUENCE_NUMBER
,
1403 creator
->next_sequence_number_length());
1404 EXPECT_EQ(PACKET_4BYTE_SEQUENCE_NUMBER
,
1405 writer_
->header().public_header
.sequence_number_length
);
1408 // TODO(ianswett): Re-enable this test by finding a good way to test different
1409 // sequence number lengths without sending packets with giant gaps.
1410 TEST_P(QuicConnectionTest
,
1411 DISABLED_SendingDifferentSequenceNumberLengthsUnackedDelta
) {
1412 QuicPacketSequenceNumber last_packet
;
1413 QuicPacketCreator
* creator
=
1414 QuicConnectionPeer::GetPacketCreator(&connection_
);
1415 SendStreamDataToPeer(1, "foo", 0, !kFin
, &last_packet
);
1416 EXPECT_EQ(1u, last_packet
);
1417 EXPECT_EQ(PACKET_1BYTE_SEQUENCE_NUMBER
,
1418 creator
->next_sequence_number_length());
1419 EXPECT_EQ(PACKET_1BYTE_SEQUENCE_NUMBER
,
1420 writer_
->header().public_header
.sequence_number_length
);
1422 creator
->set_sequence_number(100);
1424 SendStreamDataToPeer(1, "bar", 3, !kFin
, &last_packet
);
1425 EXPECT_EQ(PACKET_2BYTE_SEQUENCE_NUMBER
,
1426 creator
->next_sequence_number_length());
1427 EXPECT_EQ(PACKET_1BYTE_SEQUENCE_NUMBER
,
1428 writer_
->header().public_header
.sequence_number_length
);
1430 creator
->set_sequence_number(100 * 256);
1432 SendStreamDataToPeer(1, "foo", 6, !kFin
, &last_packet
);
1433 EXPECT_EQ(PACKET_4BYTE_SEQUENCE_NUMBER
,
1434 creator
->next_sequence_number_length());
1435 EXPECT_EQ(PACKET_2BYTE_SEQUENCE_NUMBER
,
1436 writer_
->header().public_header
.sequence_number_length
);
1438 creator
->set_sequence_number(100 * 256 * 256);
1440 SendStreamDataToPeer(1, "bar", 9, !kFin
, &last_packet
);
1441 EXPECT_EQ(PACKET_4BYTE_SEQUENCE_NUMBER
,
1442 creator
->next_sequence_number_length());
1443 EXPECT_EQ(PACKET_4BYTE_SEQUENCE_NUMBER
,
1444 writer_
->header().public_header
.sequence_number_length
);
1446 creator
->set_sequence_number(100 * 256 * 256 * 256);
1448 SendStreamDataToPeer(1, "foo", 12, !kFin
, &last_packet
);
1449 EXPECT_EQ(PACKET_6BYTE_SEQUENCE_NUMBER
,
1450 creator
->next_sequence_number_length());
1451 EXPECT_EQ(PACKET_4BYTE_SEQUENCE_NUMBER
,
1452 writer_
->header().public_header
.sequence_number_length
);
1455 TEST_P(QuicConnectionTest
, BasicSending
) {
1456 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
1457 QuicPacketSequenceNumber last_packet
;
1458 SendStreamDataToPeer(1, "foo", 0, !kFin
, &last_packet
); // Packet 1
1459 EXPECT_EQ(1u, last_packet
);
1460 SendAckPacketToPeer(); // Packet 2
1462 EXPECT_EQ(1u, least_unacked());
1464 SendAckPacketToPeer(); // Packet 3
1465 EXPECT_EQ(1u, least_unacked());
1467 SendStreamDataToPeer(1, "bar", 3, !kFin
, &last_packet
); // Packet 4
1468 EXPECT_EQ(4u, last_packet
);
1469 SendAckPacketToPeer(); // Packet 5
1470 EXPECT_EQ(1u, least_unacked());
1472 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
1474 // Peer acks up to packet 3.
1475 QuicAckFrame frame
= InitAckFrame(3);
1476 ProcessAckPacket(&frame
);
1477 SendAckPacketToPeer(); // Packet 6
1479 // As soon as we've acked one, we skip ack packets 2 and 3 and note lack of
1481 EXPECT_EQ(4u, least_unacked());
1483 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
1485 // Peer acks up to packet 4, the last packet.
1486 QuicAckFrame frame2
= InitAckFrame(6);
1487 ProcessAckPacket(&frame2
); // Acks don't instigate acks.
1489 // Verify that we did not send an ack.
1490 EXPECT_EQ(6u, writer_
->header().packet_sequence_number
);
1492 // So the last ack has not changed.
1493 EXPECT_EQ(4u, least_unacked());
1495 // If we force an ack, we shouldn't change our retransmit state.
1496 SendAckPacketToPeer(); // Packet 7
1497 EXPECT_EQ(7u, least_unacked());
1499 // But if we send more data it should.
1500 SendStreamDataToPeer(1, "eep", 6, !kFin
, &last_packet
); // Packet 8
1501 EXPECT_EQ(8u, last_packet
);
1502 SendAckPacketToPeer(); // Packet 9
1503 EXPECT_EQ(7u, least_unacked());
1506 TEST_P(QuicConnectionTest
, FECSending
) {
1507 // All packets carry version info till version is negotiated.
1508 QuicPacketCreator
* creator
=
1509 QuicConnectionPeer::GetPacketCreator(&connection_
);
1510 size_t payload_length
;
1511 // GetPacketLengthForOneStream() assumes a stream offset of 0 in determining
1512 // packet length. The size of the offset field in a stream frame is 0 for
1513 // offset 0, and 2 for non-zero offsets up through 64K. Increase
1514 // max_packet_length by 2 so that subsequent packets containing subsequent
1515 // stream frames with non-zero offets will fit within the packet length.
1516 size_t length
= 2 + GetPacketLengthForOneStream(
1517 connection_
.version(), kIncludeVersion
, PACKET_1BYTE_SEQUENCE_NUMBER
,
1518 IN_FEC_GROUP
, &payload_length
);
1519 creator
->set_max_packet_length(length
);
1521 // Send 4 protected data packets, which should also trigger 1 FEC packet.
1522 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
)).Times(5);
1523 // The first stream frame will have 2 fewer overhead bytes than the other 3.
1524 const string
payload(payload_length
* 4 + 2, 'a');
1525 connection_
.SendStreamDataWithStringWithFec(1, payload
, 0, !kFin
, nullptr);
1526 // Expect the FEC group to be closed after SendStreamDataWithString.
1527 EXPECT_FALSE(creator
->IsFecGroupOpen());
1528 EXPECT_FALSE(creator
->IsFecProtected());
1531 TEST_P(QuicConnectionTest
, FECQueueing
) {
1532 // All packets carry version info till version is negotiated.
1533 size_t payload_length
;
1534 QuicPacketCreator
* creator
=
1535 QuicConnectionPeer::GetPacketCreator(&connection_
);
1536 size_t length
= GetPacketLengthForOneStream(
1537 connection_
.version(), kIncludeVersion
, PACKET_1BYTE_SEQUENCE_NUMBER
,
1538 IN_FEC_GROUP
, &payload_length
);
1539 creator
->set_max_packet_length(length
);
1540 EXPECT_TRUE(creator
->IsFecEnabled());
1542 EXPECT_EQ(0u, connection_
.NumQueuedPackets());
1544 const string
payload(payload_length
, 'a');
1545 connection_
.SendStreamDataWithStringWithFec(1, payload
, 0, !kFin
, nullptr);
1546 EXPECT_FALSE(creator
->IsFecGroupOpen());
1547 EXPECT_FALSE(creator
->IsFecProtected());
1548 // Expect the first data packet and the fec packet to be queued.
1549 EXPECT_EQ(2u, connection_
.NumQueuedPackets());
1552 TEST_P(QuicConnectionTest
, AbandonFECFromCongestionWindow
) {
1553 EXPECT_TRUE(QuicConnectionPeer::GetPacketCreator(
1554 &connection_
)->IsFecEnabled());
1556 // 1 Data and 1 FEC packet.
1557 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
)).Times(2);
1558 connection_
.SendStreamDataWithStringWithFec(3, "foo", 0, !kFin
, nullptr);
1560 const QuicTime::Delta retransmission_time
=
1561 QuicTime::Delta::FromMilliseconds(5000);
1562 clock_
.AdvanceTime(retransmission_time
);
1564 // Abandon FEC packet and data packet.
1565 EXPECT_CALL(*send_algorithm_
, OnRetransmissionTimeout(true));
1566 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
)).Times(1);
1567 EXPECT_CALL(visitor_
, OnCanWrite());
1568 connection_
.OnRetransmissionTimeout();
1571 TEST_P(QuicConnectionTest
, DontAbandonAckedFEC
) {
1572 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
1573 EXPECT_TRUE(QuicConnectionPeer::GetPacketCreator(
1574 &connection_
)->IsFecEnabled());
1576 // 1 Data and 1 FEC packet.
1577 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
)).Times(6);
1578 connection_
.SendStreamDataWithStringWithFec(3, "foo", 0, !kFin
, nullptr);
1579 // Send some more data afterwards to ensure early retransmit doesn't trigger.
1580 connection_
.SendStreamDataWithStringWithFec(3, "foo", 3, !kFin
, nullptr);
1581 connection_
.SendStreamDataWithStringWithFec(3, "foo", 6, !kFin
, nullptr);
1583 QuicAckFrame ack_fec
= InitAckFrame(2);
1584 // Data packet missing.
1585 // TODO(ianswett): Note that this is not a sensible ack, since if the FEC was
1586 // received, it would cause the covered packet to be acked as well.
1587 NackPacket(1, &ack_fec
);
1588 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
1589 ProcessAckPacket(&ack_fec
);
1590 clock_
.AdvanceTime(DefaultRetransmissionTime());
1592 // Don't abandon the acked FEC packet, but it will abandon 2 the subsequent
1594 EXPECT_CALL(*send_algorithm_
, OnRetransmissionTimeout(true));
1595 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
)).Times(3);
1596 connection_
.GetRetransmissionAlarm()->Fire();
1599 TEST_P(QuicConnectionTest
, AbandonAllFEC
) {
1600 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
1601 EXPECT_TRUE(QuicConnectionPeer::GetPacketCreator(
1602 &connection_
)->IsFecEnabled());
1604 // 1 Data and 1 FEC packet.
1605 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
)).Times(6);
1606 connection_
.SendStreamDataWithStringWithFec(3, "foo", 0, !kFin
, nullptr);
1607 // Send some more data afterwards to ensure early retransmit doesn't trigger.
1608 connection_
.SendStreamDataWithStringWithFec(3, "foo", 3, !kFin
, nullptr);
1609 // Advance the time so not all the FEC packets are abandoned.
1610 clock_
.AdvanceTime(QuicTime::Delta::FromMilliseconds(1));
1611 connection_
.SendStreamDataWithStringWithFec(3, "foo", 6, !kFin
, nullptr);
1613 QuicAckFrame ack_fec
= InitAckFrame(5);
1614 // Ack all data packets, but no fec packets.
1615 NackPacket(2, &ack_fec
);
1616 NackPacket(4, &ack_fec
);
1618 // Lose the first FEC packet and ack the three data packets.
1619 SequenceNumberSet lost_packets
;
1620 lost_packets
.insert(2);
1621 EXPECT_CALL(*loss_algorithm_
, DetectLostPackets(_
, _
, _
, _
))
1622 .WillOnce(Return(lost_packets
));
1623 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
1624 ProcessAckPacket(&ack_fec
);
1626 clock_
.AdvanceTime(DefaultRetransmissionTime().Subtract(
1627 QuicTime::Delta::FromMilliseconds(1)));
1629 // Abandon all packets
1630 EXPECT_CALL(*send_algorithm_
, OnRetransmissionTimeout(false));
1631 connection_
.GetRetransmissionAlarm()->Fire();
1633 // Ensure the alarm is not set since all packets have been abandoned.
1634 EXPECT_FALSE(connection_
.GetRetransmissionAlarm()->IsSet());
1637 TEST_P(QuicConnectionTest
, FramePacking
) {
1638 CongestionBlockWrites();
1640 // Send an ack and two stream frames in 1 packet by queueing them.
1641 connection_
.SendAck();
1642 EXPECT_CALL(visitor_
, OnCanWrite()).WillOnce(DoAll(
1643 IgnoreResult(InvokeWithoutArgs(&connection_
,
1644 &TestConnection::SendStreamData3
)),
1645 IgnoreResult(InvokeWithoutArgs(&connection_
,
1646 &TestConnection::SendStreamData5
))));
1648 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
)).Times(1);
1649 CongestionUnblockWrites();
1650 connection_
.GetSendAlarm()->Fire();
1651 EXPECT_EQ(0u, connection_
.NumQueuedPackets());
1652 EXPECT_FALSE(connection_
.HasQueuedData());
1654 // Parse the last packet and ensure it's an ack and two stream frames from
1655 // two different streams.
1656 EXPECT_EQ(4u, writer_
->frame_count());
1657 EXPECT_FALSE(writer_
->stop_waiting_frames().empty());
1658 EXPECT_FALSE(writer_
->ack_frames().empty());
1659 ASSERT_EQ(2u, writer_
->stream_frames().size());
1660 EXPECT_EQ(kClientDataStreamId1
, writer_
->stream_frames()[0].stream_id
);
1661 EXPECT_EQ(kClientDataStreamId2
, writer_
->stream_frames()[1].stream_id
);
1664 TEST_P(QuicConnectionTest
, FramePackingNonCryptoThenCrypto
) {
1665 CongestionBlockWrites();
1667 // Send an ack and two stream frames (one non-crypto, then one crypto) in 2
1668 // packets by queueing them.
1669 connection_
.SendAck();
1670 EXPECT_CALL(visitor_
, OnCanWrite()).WillOnce(DoAll(
1671 IgnoreResult(InvokeWithoutArgs(&connection_
,
1672 &TestConnection::SendStreamData3
)),
1673 IgnoreResult(InvokeWithoutArgs(&connection_
,
1674 &TestConnection::SendCryptoStreamData
))));
1676 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
)).Times(2);
1677 CongestionUnblockWrites();
1678 connection_
.GetSendAlarm()->Fire();
1679 EXPECT_EQ(0u, connection_
.NumQueuedPackets());
1680 EXPECT_FALSE(connection_
.HasQueuedData());
1682 // Parse the last packet and ensure it's the crypto stream frame.
1683 EXPECT_EQ(1u, writer_
->frame_count());
1684 ASSERT_EQ(1u, writer_
->stream_frames().size());
1685 EXPECT_EQ(kCryptoStreamId
, writer_
->stream_frames()[0].stream_id
);
1688 TEST_P(QuicConnectionTest
, FramePackingCryptoThenNonCrypto
) {
1689 CongestionBlockWrites();
1691 // Send an ack and two stream frames (one crypto, then one non-crypto) in 2
1692 // packets by queueing them.
1693 connection_
.SendAck();
1694 EXPECT_CALL(visitor_
, OnCanWrite()).WillOnce(DoAll(
1695 IgnoreResult(InvokeWithoutArgs(&connection_
,
1696 &TestConnection::SendCryptoStreamData
)),
1697 IgnoreResult(InvokeWithoutArgs(&connection_
,
1698 &TestConnection::SendStreamData3
))));
1700 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
)).Times(2);
1701 CongestionUnblockWrites();
1702 connection_
.GetSendAlarm()->Fire();
1703 EXPECT_EQ(0u, connection_
.NumQueuedPackets());
1704 EXPECT_FALSE(connection_
.HasQueuedData());
1706 // Parse the last packet and ensure it's the stream frame from stream 3.
1707 EXPECT_EQ(1u, writer_
->frame_count());
1708 ASSERT_EQ(1u, writer_
->stream_frames().size());
1709 EXPECT_EQ(kClientDataStreamId1
, writer_
->stream_frames()[0].stream_id
);
1712 TEST_P(QuicConnectionTest
, FramePackingFEC
) {
1713 EXPECT_TRUE(QuicConnectionPeer::GetPacketCreator(
1714 &connection_
)->IsFecEnabled());
1716 CongestionBlockWrites();
1718 // Queue an ack and two stream frames. Ack gets flushed when FEC is turned on
1719 // for sending protected data; two stream frames are packing in 1 packet.
1720 EXPECT_CALL(visitor_
, OnCanWrite()).WillOnce(DoAll(
1721 IgnoreResult(InvokeWithoutArgs(
1722 &connection_
, &TestConnection::SendStreamData3WithFec
)),
1723 IgnoreResult(InvokeWithoutArgs(
1724 &connection_
, &TestConnection::SendStreamData5WithFec
))));
1725 connection_
.SendAck();
1727 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
)).Times(3);
1728 CongestionUnblockWrites();
1729 connection_
.GetSendAlarm()->Fire();
1730 EXPECT_EQ(0u, connection_
.NumQueuedPackets());
1731 EXPECT_FALSE(connection_
.HasQueuedData());
1733 // Parse the last packet and ensure it's in an fec group.
1734 EXPECT_EQ(2u, writer_
->header().fec_group
);
1735 EXPECT_EQ(0u, writer_
->frame_count());
1738 TEST_P(QuicConnectionTest
, FramePackingAckResponse
) {
1739 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
1740 // Process a data packet to queue up a pending ack.
1741 EXPECT_CALL(visitor_
, OnStreamFrames(_
)).Times(1);
1742 ProcessDataPacket(1, 1, kEntropyFlag
);
1744 EXPECT_CALL(visitor_
, OnCanWrite()).WillOnce(DoAll(
1745 IgnoreResult(InvokeWithoutArgs(&connection_
,
1746 &TestConnection::SendStreamData3
)),
1747 IgnoreResult(InvokeWithoutArgs(&connection_
,
1748 &TestConnection::SendStreamData5
))));
1750 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
)).Times(1);
1752 // Process an ack to cause the visitor's OnCanWrite to be invoked.
1753 peer_creator_
.set_sequence_number(2);
1754 QuicAckFrame ack_one
= InitAckFrame(0);
1755 ProcessAckPacket(&ack_one
);
1757 EXPECT_EQ(0u, connection_
.NumQueuedPackets());
1758 EXPECT_FALSE(connection_
.HasQueuedData());
1760 // Parse the last packet and ensure it's an ack and two stream frames from
1761 // two different streams.
1762 EXPECT_EQ(4u, writer_
->frame_count());
1763 EXPECT_FALSE(writer_
->stop_waiting_frames().empty());
1764 EXPECT_FALSE(writer_
->ack_frames().empty());
1765 ASSERT_EQ(2u, writer_
->stream_frames().size());
1766 EXPECT_EQ(kClientDataStreamId1
, writer_
->stream_frames()[0].stream_id
);
1767 EXPECT_EQ(kClientDataStreamId2
, writer_
->stream_frames()[1].stream_id
);
1770 TEST_P(QuicConnectionTest
, FramePackingSendv
) {
1771 // Send data in 1 packet by writing multiple blocks in a single iovector
1773 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
));
1775 char data
[] = "ABCD";
1777 data_iov
.AppendNoCoalesce(data
, 2);
1778 data_iov
.AppendNoCoalesce(data
+ 2, 2);
1779 connection_
.SendStreamData(1, data_iov
, 0, !kFin
, MAY_FEC_PROTECT
, nullptr);
1781 EXPECT_EQ(0u, connection_
.NumQueuedPackets());
1782 EXPECT_FALSE(connection_
.HasQueuedData());
1784 // Parse the last packet and ensure multiple iovector blocks have
1785 // been packed into a single stream frame from one stream.
1786 EXPECT_EQ(1u, writer_
->frame_count());
1787 EXPECT_EQ(1u, writer_
->stream_frames().size());
1788 QuicStreamFrame frame
= writer_
->stream_frames()[0];
1789 EXPECT_EQ(1u, frame
.stream_id
);
1790 EXPECT_EQ("ABCD", string(static_cast<char*>
1791 (frame
.data
.iovec()[0].iov_base
),
1792 (frame
.data
.iovec()[0].iov_len
)));
1795 TEST_P(QuicConnectionTest
, FramePackingSendvQueued
) {
1796 // Try to send two stream frames in 1 packet by using writev.
1797 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
));
1800 char data
[] = "ABCD";
1802 data_iov
.AppendNoCoalesce(data
, 2);
1803 data_iov
.AppendNoCoalesce(data
+ 2, 2);
1804 connection_
.SendStreamData(1, data_iov
, 0, !kFin
, MAY_FEC_PROTECT
, nullptr);
1806 EXPECT_EQ(1u, connection_
.NumQueuedPackets());
1807 EXPECT_TRUE(connection_
.HasQueuedData());
1809 // Unblock the writes and actually send.
1810 writer_
->SetWritable();
1811 connection_
.OnCanWrite();
1812 EXPECT_EQ(0u, connection_
.NumQueuedPackets());
1814 // Parse the last packet and ensure it's one stream frame from one stream.
1815 EXPECT_EQ(1u, writer_
->frame_count());
1816 EXPECT_EQ(1u, writer_
->stream_frames().size());
1817 EXPECT_EQ(1u, writer_
->stream_frames()[0].stream_id
);
1820 TEST_P(QuicConnectionTest
, SendingZeroBytes
) {
1821 // Send a zero byte write with a fin using writev.
1822 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
));
1824 connection_
.SendStreamData(1, empty_iov
, 0, kFin
, MAY_FEC_PROTECT
, nullptr);
1826 EXPECT_EQ(0u, connection_
.NumQueuedPackets());
1827 EXPECT_FALSE(connection_
.HasQueuedData());
1829 // Parse the last packet and ensure it's one stream frame from one stream.
1830 EXPECT_EQ(1u, writer_
->frame_count());
1831 EXPECT_EQ(1u, writer_
->stream_frames().size());
1832 EXPECT_EQ(1u, writer_
->stream_frames()[0].stream_id
);
1833 EXPECT_TRUE(writer_
->stream_frames()[0].fin
);
1836 TEST_P(QuicConnectionTest
, OnCanWrite
) {
1837 // Visitor's OnCanWrite will send data, but will have more pending writes.
1838 EXPECT_CALL(visitor_
, OnCanWrite()).WillOnce(DoAll(
1839 IgnoreResult(InvokeWithoutArgs(&connection_
,
1840 &TestConnection::SendStreamData3
)),
1841 IgnoreResult(InvokeWithoutArgs(&connection_
,
1842 &TestConnection::SendStreamData5
))));
1843 EXPECT_CALL(visitor_
, WillingAndAbleToWrite()).WillOnce(Return(true));
1844 EXPECT_CALL(*send_algorithm_
,
1845 TimeUntilSend(_
, _
, _
)).WillRepeatedly(
1846 testing::Return(QuicTime::Delta::Zero()));
1848 connection_
.OnCanWrite();
1850 // Parse the last packet and ensure it's the two stream frames from
1851 // two different streams.
1852 EXPECT_EQ(2u, writer_
->frame_count());
1853 EXPECT_EQ(2u, writer_
->stream_frames().size());
1854 EXPECT_EQ(kClientDataStreamId1
, writer_
->stream_frames()[0].stream_id
);
1855 EXPECT_EQ(kClientDataStreamId2
, writer_
->stream_frames()[1].stream_id
);
1858 TEST_P(QuicConnectionTest
, RetransmitOnNack
) {
1859 QuicPacketSequenceNumber last_packet
;
1860 QuicByteCount second_packet_size
;
1861 SendStreamDataToPeer(3, "foo", 0, !kFin
, &last_packet
); // Packet 1
1862 second_packet_size
=
1863 SendStreamDataToPeer(3, "foos", 3, !kFin
, &last_packet
); // Packet 2
1864 SendStreamDataToPeer(3, "fooos", 7, !kFin
, &last_packet
); // Packet 3
1866 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
1868 // Don't lose a packet on an ack, and nothing is retransmitted.
1869 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
1870 QuicAckFrame ack_one
= InitAckFrame(1);
1871 ProcessAckPacket(&ack_one
);
1873 // Lose a packet and ensure it triggers retransmission.
1874 QuicAckFrame nack_two
= InitAckFrame(3);
1875 NackPacket(2, &nack_two
);
1876 SequenceNumberSet lost_packets
;
1877 lost_packets
.insert(2);
1878 EXPECT_CALL(*loss_algorithm_
, DetectLostPackets(_
, _
, _
, _
))
1879 .WillOnce(Return(lost_packets
));
1880 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
1881 EXPECT_CALL(*send_algorithm_
,
1882 OnPacketSent(_
, _
, _
, second_packet_size
- kQuicVersionSize
, _
)).
1884 ProcessAckPacket(&nack_two
);
1887 TEST_P(QuicConnectionTest
, DiscardRetransmit
) {
1888 QuicPacketSequenceNumber last_packet
;
1889 SendStreamDataToPeer(1, "foo", 0, !kFin
, &last_packet
); // Packet 1
1890 SendStreamDataToPeer(1, "foos", 3, !kFin
, &last_packet
); // Packet 2
1891 SendStreamDataToPeer(1, "fooos", 7, !kFin
, &last_packet
); // Packet 3
1893 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
1895 // Instigate a loss with an ack.
1896 QuicAckFrame nack_two
= InitAckFrame(3);
1897 NackPacket(2, &nack_two
);
1898 // The first nack should trigger a fast retransmission, but we'll be
1899 // write blocked, so the packet will be queued.
1901 SequenceNumberSet lost_packets
;
1902 lost_packets
.insert(2);
1903 EXPECT_CALL(*loss_algorithm_
, DetectLostPackets(_
, _
, _
, _
))
1904 .WillOnce(Return(lost_packets
));
1905 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
1906 ProcessAckPacket(&nack_two
);
1907 EXPECT_EQ(1u, connection_
.NumQueuedPackets());
1909 // Now, ack the previous transmission.
1910 EXPECT_CALL(*loss_algorithm_
, DetectLostPackets(_
, _
, _
, _
))
1911 .WillOnce(Return(SequenceNumberSet()));
1912 QuicAckFrame ack_all
= InitAckFrame(3);
1913 ProcessAckPacket(&ack_all
);
1915 // Unblock the socket and attempt to send the queued packets. However,
1916 // since the previous transmission has been acked, we will not
1917 // send the retransmission.
1918 EXPECT_CALL(*send_algorithm_
,
1919 OnPacketSent(_
, _
, _
, _
, _
)).Times(0);
1921 writer_
->SetWritable();
1922 connection_
.OnCanWrite();
1924 EXPECT_EQ(0u, connection_
.NumQueuedPackets());
1927 TEST_P(QuicConnectionTest
, RetransmitNackedLargestObserved
) {
1928 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
1929 QuicPacketSequenceNumber largest_observed
;
1930 QuicByteCount packet_size
;
1931 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
))
1932 .WillOnce(DoAll(SaveArg
<2>(&largest_observed
), SaveArg
<3>(&packet_size
),
1934 connection_
.SendStreamDataWithString(3, "foo", 0, !kFin
, nullptr);
1936 QuicAckFrame frame
= InitAckFrame(1);
1937 NackPacket(largest_observed
, &frame
);
1938 // The first nack should retransmit the largest observed packet.
1939 SequenceNumberSet lost_packets
;
1940 lost_packets
.insert(1);
1941 EXPECT_CALL(*loss_algorithm_
, DetectLostPackets(_
, _
, _
, _
))
1942 .WillOnce(Return(lost_packets
));
1943 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
1944 EXPECT_CALL(*send_algorithm_
,
1945 OnPacketSent(_
, _
, _
, packet_size
- kQuicVersionSize
, _
));
1946 ProcessAckPacket(&frame
);
1949 TEST_P(QuicConnectionTest
, QueueAfterTwoRTOs
) {
1950 for (int i
= 0; i
< 10; ++i
) {
1951 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
)).Times(1);
1952 connection_
.SendStreamDataWithString(3, "foo", i
* 3, !kFin
, nullptr);
1955 // Block the congestion window and ensure they're queued.
1957 clock_
.AdvanceTime(DefaultRetransmissionTime());
1958 // Only one packet should be retransmitted.
1959 EXPECT_CALL(*send_algorithm_
, OnRetransmissionTimeout(true));
1960 connection_
.GetRetransmissionAlarm()->Fire();
1961 EXPECT_TRUE(connection_
.HasQueuedData());
1963 // Unblock the congestion window.
1964 writer_
->SetWritable();
1965 clock_
.AdvanceTime(QuicTime::Delta::FromMicroseconds(
1966 2 * DefaultRetransmissionTime().ToMicroseconds()));
1967 // Retransmit already retransmitted packets event though the sequence number
1968 // greater than the largest observed.
1969 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
)).Times(10);
1970 connection_
.GetRetransmissionAlarm()->Fire();
1971 connection_
.OnCanWrite();
1974 TEST_P(QuicConnectionTest
, WriteBlockedThenSent
) {
1976 writer_
->set_is_write_blocked_data_buffered(true);
1977 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
)).Times(1);
1978 connection_
.SendStreamDataWithString(1, "foo", 0, !kFin
, nullptr);
1979 EXPECT_TRUE(connection_
.GetRetransmissionAlarm()->IsSet());
1981 writer_
->SetWritable();
1982 connection_
.OnCanWrite();
1983 EXPECT_TRUE(connection_
.GetRetransmissionAlarm()->IsSet());
1986 TEST_P(QuicConnectionTest
, RetransmitWriteBlockedAckedOriginalThenSent
) {
1987 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
1988 connection_
.SendStreamDataWithString(3, "foo", 0, !kFin
, nullptr);
1989 EXPECT_TRUE(connection_
.GetRetransmissionAlarm()->IsSet());
1992 writer_
->set_is_write_blocked_data_buffered(true);
1993 // Simulate the retransmission alarm firing.
1994 EXPECT_CALL(*send_algorithm_
, OnRetransmissionTimeout(_
));
1995 clock_
.AdvanceTime(DefaultRetransmissionTime());
1996 connection_
.GetRetransmissionAlarm()->Fire();
1998 // Ack the sent packet before the callback returns, which happens in
1999 // rare circumstances with write blocked sockets.
2000 QuicAckFrame ack
= InitAckFrame(1);
2001 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
2002 EXPECT_CALL(*send_algorithm_
, RevertRetransmissionTimeout());
2003 ProcessAckPacket(&ack
);
2005 writer_
->SetWritable();
2006 connection_
.OnCanWrite();
2007 // There is now a pending packet, but with no retransmittable frames.
2008 EXPECT_TRUE(connection_
.GetRetransmissionAlarm()->IsSet());
2009 EXPECT_FALSE(connection_
.sent_packet_manager().HasRetransmittableFrames(2));
2012 TEST_P(QuicConnectionTest
, AlarmsWhenWriteBlocked
) {
2013 // Block the connection.
2015 connection_
.SendStreamDataWithString(3, "foo", 0, !kFin
, nullptr);
2016 EXPECT_EQ(1u, writer_
->packets_write_attempts());
2017 EXPECT_TRUE(writer_
->IsWriteBlocked());
2019 // Set the send and resumption alarms. Fire the alarms and ensure they don't
2020 // attempt to write.
2021 connection_
.GetResumeWritesAlarm()->Set(clock_
.ApproximateNow());
2022 connection_
.GetSendAlarm()->Set(clock_
.ApproximateNow());
2023 connection_
.GetResumeWritesAlarm()->Fire();
2024 connection_
.GetSendAlarm()->Fire();
2025 EXPECT_TRUE(writer_
->IsWriteBlocked());
2026 EXPECT_EQ(1u, writer_
->packets_write_attempts());
2029 TEST_P(QuicConnectionTest
, NoLimitPacketsPerNack
) {
2030 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
2032 // Send packets 1 to 15.
2033 for (int i
= 0; i
< 15; ++i
) {
2034 SendStreamDataToPeer(1, "foo", offset
, !kFin
, nullptr);
2038 // Ack 15, nack 1-14.
2039 SequenceNumberSet lost_packets
;
2040 QuicAckFrame nack
= InitAckFrame(15);
2041 for (int i
= 1; i
< 15; ++i
) {
2042 NackPacket(i
, &nack
);
2043 lost_packets
.insert(i
);
2046 // 14 packets have been NACK'd and lost. In TCP cubic, PRR limits
2047 // the retransmission rate in the case of burst losses.
2048 EXPECT_CALL(*loss_algorithm_
, DetectLostPackets(_
, _
, _
, _
))
2049 .WillOnce(Return(lost_packets
));
2050 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
2051 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
)).Times(14);
2052 ProcessAckPacket(&nack
);
2055 // Test sending multiple acks from the connection to the session.
2056 TEST_P(QuicConnectionTest
, MultipleAcks
) {
2057 QuicPacketSequenceNumber last_packet
;
2058 SendStreamDataToPeer(1, "foo", 0, !kFin
, &last_packet
); // Packet 1
2059 EXPECT_EQ(1u, last_packet
);
2060 SendStreamDataToPeer(3, "foo", 0, !kFin
, &last_packet
); // Packet 2
2061 EXPECT_EQ(2u, last_packet
);
2062 SendAckPacketToPeer(); // Packet 3
2063 SendStreamDataToPeer(5, "foo", 0, !kFin
, &last_packet
); // Packet 4
2064 EXPECT_EQ(4u, last_packet
);
2065 SendStreamDataToPeer(1, "foo", 3, !kFin
, &last_packet
); // Packet 5
2066 EXPECT_EQ(5u, last_packet
);
2067 SendStreamDataToPeer(3, "foo", 3, !kFin
, &last_packet
); // Packet 6
2068 EXPECT_EQ(6u, last_packet
);
2070 // Client will ack packets 1, 2, [!3], 4, 5.
2071 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
2072 QuicAckFrame frame1
= InitAckFrame(5);
2073 NackPacket(3, &frame1
);
2074 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
2075 ProcessAckPacket(&frame1
);
2077 // Now the client implicitly acks 3, and explicitly acks 6.
2078 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
2079 QuicAckFrame frame2
= InitAckFrame(6);
2080 ProcessAckPacket(&frame2
);
2083 TEST_P(QuicConnectionTest
, DontLatchUnackedPacket
) {
2084 SendStreamDataToPeer(1, "foo", 0, !kFin
, nullptr); // Packet 1;
2085 // From now on, we send acks, so the send algorithm won't mark them pending.
2086 ON_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
))
2087 .WillByDefault(Return(false));
2088 SendAckPacketToPeer(); // Packet 2
2090 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
2091 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
2092 QuicAckFrame frame
= InitAckFrame(1);
2093 ProcessAckPacket(&frame
);
2095 // Verify that our internal state has least-unacked as 2, because we're still
2096 // waiting for a potential ack for 2.
2098 EXPECT_EQ(2u, stop_waiting()->least_unacked
);
2100 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
2101 frame
= InitAckFrame(2);
2102 ProcessAckPacket(&frame
);
2103 EXPECT_EQ(3u, stop_waiting()->least_unacked
);
2105 // When we send an ack, we make sure our least-unacked makes sense. In this
2106 // case since we're not waiting on an ack for 2 and all packets are acked, we
2108 SendAckPacketToPeer(); // Packet 3
2109 // Least_unacked remains at 3 until another ack is received.
2110 EXPECT_EQ(3u, stop_waiting()->least_unacked
);
2111 // Check that the outgoing ack had its sequence number as least_unacked.
2112 EXPECT_EQ(3u, least_unacked());
2114 // Ack the ack, which updates the rtt and raises the least unacked.
2115 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
2116 frame
= InitAckFrame(3);
2117 ProcessAckPacket(&frame
);
2119 ON_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
))
2120 .WillByDefault(Return(true));
2121 SendStreamDataToPeer(1, "bar", 3, false, nullptr); // Packet 4
2122 EXPECT_EQ(4u, stop_waiting()->least_unacked
);
2123 ON_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
))
2124 .WillByDefault(Return(false));
2125 SendAckPacketToPeer(); // Packet 5
2126 EXPECT_EQ(4u, least_unacked());
2128 // Send two data packets at the end, and ensure if the last one is acked,
2129 // the least unacked is raised above the ack packets.
2130 ON_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
))
2131 .WillByDefault(Return(true));
2132 SendStreamDataToPeer(1, "bar", 6, false, nullptr); // Packet 6
2133 SendStreamDataToPeer(1, "bar", 9, false, nullptr); // Packet 7
2135 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
2136 frame
= InitAckFrame(7);
2137 NackPacket(5, &frame
);
2138 NackPacket(6, &frame
);
2139 ProcessAckPacket(&frame
);
2141 EXPECT_EQ(6u, stop_waiting()->least_unacked
);
2144 TEST_P(QuicConnectionTest
, ReviveMissingPacketAfterFecPacket
) {
2145 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
2147 // Don't send missing packet 1.
2148 ProcessFecPacket(2, 1, true, !kEntropyFlag
, nullptr);
2149 // Entropy flag should be false, so entropy should be 0.
2150 EXPECT_EQ(0u, QuicConnectionPeer::ReceivedEntropyHash(&connection_
, 2));
2153 TEST_P(QuicConnectionTest
, ReviveMissingPacketWithVaryingSeqNumLengths
) {
2154 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
2156 // Set up a debug visitor to the connection.
2157 FecQuicConnectionDebugVisitor
* fec_visitor
=
2158 new FecQuicConnectionDebugVisitor();
2159 connection_
.set_debug_visitor(fec_visitor
);
2161 QuicPacketSequenceNumber fec_packet
= 0;
2162 QuicSequenceNumberLength lengths
[] = {PACKET_6BYTE_SEQUENCE_NUMBER
,
2163 PACKET_4BYTE_SEQUENCE_NUMBER
,
2164 PACKET_2BYTE_SEQUENCE_NUMBER
,
2165 PACKET_1BYTE_SEQUENCE_NUMBER
};
2166 // For each sequence number length size, revive a packet and check sequence
2167 // number length in the revived packet.
2168 for (size_t i
= 0; i
< arraysize(lengths
); ++i
) {
2169 // Set sequence_number_length_ (for data and FEC packets).
2170 sequence_number_length_
= lengths
[i
];
2172 // Don't send missing packet, but send fec packet right after it.
2173 ProcessFecPacket(fec_packet
, fec_packet
- 1, true, !kEntropyFlag
, nullptr);
2174 // Sequence number length in the revived header should be the same as
2175 // in the original data/fec packet headers.
2176 EXPECT_EQ(sequence_number_length_
, fec_visitor
->revived_header().
2177 public_header
.sequence_number_length
);
2181 TEST_P(QuicConnectionTest
, ReviveMissingPacketWithVaryingConnectionIdLengths
) {
2182 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
2184 // Set up a debug visitor to the connection.
2185 FecQuicConnectionDebugVisitor
* fec_visitor
=
2186 new FecQuicConnectionDebugVisitor();
2187 connection_
.set_debug_visitor(fec_visitor
);
2189 QuicPacketSequenceNumber fec_packet
= 0;
2190 QuicConnectionIdLength lengths
[] = {PACKET_8BYTE_CONNECTION_ID
,
2191 PACKET_4BYTE_CONNECTION_ID
,
2192 PACKET_1BYTE_CONNECTION_ID
,
2193 PACKET_0BYTE_CONNECTION_ID
};
2194 // For each connection id length size, revive a packet and check connection
2195 // id length in the revived packet.
2196 for (size_t i
= 0; i
< arraysize(lengths
); ++i
) {
2197 // Set connection id length (for data and FEC packets).
2198 connection_id_length_
= lengths
[i
];
2200 // Don't send missing packet, but send fec packet right after it.
2201 ProcessFecPacket(fec_packet
, fec_packet
- 1, true, !kEntropyFlag
, nullptr);
2202 // Connection id length in the revived header should be the same as
2203 // in the original data/fec packet headers.
2204 EXPECT_EQ(connection_id_length_
,
2205 fec_visitor
->revived_header().public_header
.connection_id_length
);
2209 TEST_P(QuicConnectionTest
, ReviveMissingPacketAfterDataPacketThenFecPacket
) {
2210 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
2212 ProcessFecProtectedPacket(1, false, kEntropyFlag
);
2213 // Don't send missing packet 2.
2214 ProcessFecPacket(3, 1, true, !kEntropyFlag
, nullptr);
2215 // Entropy flag should be true, so entropy should not be 0.
2216 EXPECT_NE(0u, QuicConnectionPeer::ReceivedEntropyHash(&connection_
, 2));
2219 TEST_P(QuicConnectionTest
, ReviveMissingPacketAfterDataPacketsThenFecPacket
) {
2220 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
2222 ProcessFecProtectedPacket(1, false, !kEntropyFlag
);
2223 // Don't send missing packet 2.
2224 ProcessFecProtectedPacket(3, false, !kEntropyFlag
);
2225 ProcessFecPacket(4, 1, true, kEntropyFlag
, nullptr);
2226 // Ensure QUIC no longer revives entropy for lost packets.
2227 EXPECT_EQ(0u, QuicConnectionPeer::ReceivedEntropyHash(&connection_
, 2));
2228 EXPECT_NE(0u, QuicConnectionPeer::ReceivedEntropyHash(&connection_
, 4));
2231 TEST_P(QuicConnectionTest
, ReviveMissingPacketAfterDataPacket
) {
2232 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
2234 // Don't send missing packet 1.
2235 ProcessFecPacket(3, 1, false, !kEntropyFlag
, nullptr);
2237 ProcessFecProtectedPacket(2, true, !kEntropyFlag
);
2238 // Entropy flag should be false, so entropy should be 0.
2239 EXPECT_EQ(0u, QuicConnectionPeer::ReceivedEntropyHash(&connection_
, 2));
2242 TEST_P(QuicConnectionTest
, ReviveMissingPacketAfterDataPackets
) {
2243 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
2245 ProcessFecProtectedPacket(1, false, !kEntropyFlag
);
2246 // Don't send missing packet 2.
2247 ProcessFecPacket(6, 1, false, kEntropyFlag
, nullptr);
2248 ProcessFecProtectedPacket(3, false, kEntropyFlag
);
2249 ProcessFecProtectedPacket(4, false, kEntropyFlag
);
2250 ProcessFecProtectedPacket(5, true, !kEntropyFlag
);
2251 // Ensure entropy is not revived for the missing packet.
2252 EXPECT_EQ(0u, QuicConnectionPeer::ReceivedEntropyHash(&connection_
, 2));
2253 EXPECT_NE(0u, QuicConnectionPeer::ReceivedEntropyHash(&connection_
, 3));
2256 TEST_P(QuicConnectionTest
, TLP
) {
2257 QuicSentPacketManagerPeer::SetMaxTailLossProbes(
2258 QuicConnectionPeer::GetSentPacketManager(&connection_
), 1);
2260 SendStreamDataToPeer(3, "foo", 0, !kFin
, nullptr);
2261 EXPECT_EQ(1u, stop_waiting()->least_unacked
);
2262 QuicTime retransmission_time
=
2263 connection_
.GetRetransmissionAlarm()->deadline();
2264 EXPECT_NE(QuicTime::Zero(), retransmission_time
);
2266 EXPECT_EQ(1u, writer_
->header().packet_sequence_number
);
2267 // Simulate the retransmission alarm firing and sending a tlp,
2268 // so send algorithm's OnRetransmissionTimeout is not called.
2269 clock_
.AdvanceTime(retransmission_time
.Subtract(clock_
.Now()));
2270 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, 2u, _
, _
));
2271 connection_
.GetRetransmissionAlarm()->Fire();
2272 EXPECT_EQ(2u, writer_
->header().packet_sequence_number
);
2273 // We do not raise the high water mark yet.
2274 EXPECT_EQ(1u, stop_waiting()->least_unacked
);
2277 TEST_P(QuicConnectionTest
, RTO
) {
2278 QuicTime default_retransmission_time
= clock_
.ApproximateNow().Add(
2279 DefaultRetransmissionTime());
2280 SendStreamDataToPeer(3, "foo", 0, !kFin
, nullptr);
2281 EXPECT_EQ(1u, stop_waiting()->least_unacked
);
2283 EXPECT_EQ(1u, writer_
->header().packet_sequence_number
);
2284 EXPECT_EQ(default_retransmission_time
,
2285 connection_
.GetRetransmissionAlarm()->deadline());
2286 // Simulate the retransmission alarm firing.
2287 clock_
.AdvanceTime(DefaultRetransmissionTime());
2288 EXPECT_CALL(*send_algorithm_
, OnRetransmissionTimeout(true));
2289 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, 2u, _
, _
));
2290 connection_
.GetRetransmissionAlarm()->Fire();
2291 EXPECT_EQ(2u, writer_
->header().packet_sequence_number
);
2292 // We do not raise the high water mark yet.
2293 EXPECT_EQ(1u, stop_waiting()->least_unacked
);
2296 TEST_P(QuicConnectionTest
, RTOWithSameEncryptionLevel
) {
2297 QuicTime default_retransmission_time
= clock_
.ApproximateNow().Add(
2298 DefaultRetransmissionTime());
2299 use_tagging_decrypter();
2301 // A TaggingEncrypter puts kTagSize copies of the given byte (0x01 here) at
2302 // the end of the packet. We can test this to check which encrypter was used.
2303 connection_
.SetEncrypter(ENCRYPTION_NONE
, new TaggingEncrypter(0x01));
2304 SendStreamDataToPeer(3, "foo", 0, !kFin
, nullptr);
2305 EXPECT_EQ(0x01010101u
, writer_
->final_bytes_of_last_packet());
2307 connection_
.SetEncrypter(ENCRYPTION_INITIAL
, new TaggingEncrypter(0x02));
2308 connection_
.SetDefaultEncryptionLevel(ENCRYPTION_INITIAL
);
2309 SendStreamDataToPeer(3, "foo", 0, !kFin
, nullptr);
2310 EXPECT_EQ(0x02020202u
, writer_
->final_bytes_of_last_packet());
2312 EXPECT_EQ(default_retransmission_time
,
2313 connection_
.GetRetransmissionAlarm()->deadline());
2316 EXPECT_CALL(*send_algorithm_
, OnRetransmissionTimeout(true));
2317 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, 3, _
, _
));
2318 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, 4, _
, _
));
2321 // Simulate the retransmission alarm firing.
2322 clock_
.AdvanceTime(DefaultRetransmissionTime());
2323 connection_
.GetRetransmissionAlarm()->Fire();
2325 // Packet should have been sent with ENCRYPTION_NONE.
2326 EXPECT_EQ(0x01010101u
, writer_
->final_bytes_of_previous_packet());
2328 // Packet should have been sent with ENCRYPTION_INITIAL.
2329 EXPECT_EQ(0x02020202u
, writer_
->final_bytes_of_last_packet());
2332 TEST_P(QuicConnectionTest
, SendHandshakeMessages
) {
2333 use_tagging_decrypter();
2334 // A TaggingEncrypter puts kTagSize copies of the given byte (0x01 here) at
2335 // the end of the packet. We can test this to check which encrypter was used.
2336 connection_
.SetEncrypter(ENCRYPTION_NONE
, new TaggingEncrypter(0x01));
2338 // Attempt to send a handshake message and have the socket block.
2339 EXPECT_CALL(*send_algorithm_
,
2340 TimeUntilSend(_
, _
, _
)).WillRepeatedly(
2341 testing::Return(QuicTime::Delta::Zero()));
2343 connection_
.SendStreamDataWithString(1, "foo", 0, !kFin
, nullptr);
2344 // The packet should be serialized, but not queued.
2345 EXPECT_EQ(1u, connection_
.NumQueuedPackets());
2347 // Switch to the new encrypter.
2348 connection_
.SetEncrypter(ENCRYPTION_INITIAL
, new TaggingEncrypter(0x02));
2349 connection_
.SetDefaultEncryptionLevel(ENCRYPTION_INITIAL
);
2351 // Now become writeable and flush the packets.
2352 writer_
->SetWritable();
2353 EXPECT_CALL(visitor_
, OnCanWrite());
2354 connection_
.OnCanWrite();
2355 EXPECT_EQ(0u, connection_
.NumQueuedPackets());
2357 // Verify that the handshake packet went out at the null encryption.
2358 EXPECT_EQ(0x01010101u
, writer_
->final_bytes_of_last_packet());
2361 TEST_P(QuicConnectionTest
,
2362 DropRetransmitsForNullEncryptedPacketAfterForwardSecure
) {
2363 use_tagging_decrypter();
2364 connection_
.SetEncrypter(ENCRYPTION_NONE
, new TaggingEncrypter(0x01));
2365 QuicPacketSequenceNumber sequence_number
;
2366 SendStreamDataToPeer(3, "foo", 0, !kFin
, &sequence_number
);
2368 // Simulate the retransmission alarm firing and the socket blocking.
2370 EXPECT_CALL(*send_algorithm_
, OnRetransmissionTimeout(true));
2371 clock_
.AdvanceTime(DefaultRetransmissionTime());
2372 connection_
.GetRetransmissionAlarm()->Fire();
2374 // Go forward secure.
2375 connection_
.SetEncrypter(ENCRYPTION_FORWARD_SECURE
,
2376 new TaggingEncrypter(0x02));
2377 connection_
.SetDefaultEncryptionLevel(ENCRYPTION_FORWARD_SECURE
);
2378 connection_
.NeuterUnencryptedPackets();
2380 EXPECT_EQ(QuicTime::Zero(),
2381 connection_
.GetRetransmissionAlarm()->deadline());
2382 // Unblock the socket and ensure that no packets are sent.
2383 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
)).Times(0);
2384 writer_
->SetWritable();
2385 connection_
.OnCanWrite();
2388 TEST_P(QuicConnectionTest
, RetransmitPacketsWithInitialEncryption
) {
2389 use_tagging_decrypter();
2390 connection_
.SetEncrypter(ENCRYPTION_NONE
, new TaggingEncrypter(0x01));
2391 connection_
.SetDefaultEncryptionLevel(ENCRYPTION_NONE
);
2393 SendStreamDataToPeer(1, "foo", 0, !kFin
, nullptr);
2395 connection_
.SetEncrypter(ENCRYPTION_INITIAL
, new TaggingEncrypter(0x02));
2396 connection_
.SetDefaultEncryptionLevel(ENCRYPTION_INITIAL
);
2398 SendStreamDataToPeer(2, "bar", 0, !kFin
, nullptr);
2399 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
)).Times(1);
2401 connection_
.RetransmitUnackedPackets(ALL_INITIAL_RETRANSMISSION
);
2404 TEST_P(QuicConnectionTest
, BufferNonDecryptablePackets
) {
2405 // SetFromConfig is always called after construction from InitializeSession.
2406 EXPECT_CALL(*send_algorithm_
, SetFromConfig(_
, _
));
2408 connection_
.SetFromConfig(config
);
2409 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
2410 use_tagging_decrypter();
2412 const uint8 tag
= 0x07;
2413 framer_
.SetEncrypter(ENCRYPTION_INITIAL
, new TaggingEncrypter(tag
));
2415 // Process an encrypted packet which can not yet be decrypted which should
2416 // result in the packet being buffered.
2417 ProcessDataPacketAtLevel(1, 0, kEntropyFlag
, ENCRYPTION_INITIAL
);
2419 // Transition to the new encryption state and process another encrypted packet
2420 // which should result in the original packet being processed.
2421 connection_
.SetDecrypter(new StrictTaggingDecrypter(tag
),
2422 ENCRYPTION_INITIAL
);
2423 connection_
.SetDefaultEncryptionLevel(ENCRYPTION_INITIAL
);
2424 connection_
.SetEncrypter(ENCRYPTION_INITIAL
, new TaggingEncrypter(tag
));
2425 EXPECT_CALL(visitor_
, OnStreamFrames(_
)).Times(2);
2426 ProcessDataPacketAtLevel(2, 0, kEntropyFlag
, ENCRYPTION_INITIAL
);
2428 // Finally, process a third packet and note that we do not reprocess the
2430 EXPECT_CALL(visitor_
, OnStreamFrames(_
)).Times(1);
2431 ProcessDataPacketAtLevel(3, 0, kEntropyFlag
, ENCRYPTION_INITIAL
);
2434 TEST_P(QuicConnectionTest
, Buffer100NonDecryptablePackets
) {
2435 // SetFromConfig is always called after construction from InitializeSession.
2436 EXPECT_CALL(*send_algorithm_
, SetFromConfig(_
, _
));
2438 config
.set_max_undecryptable_packets(100);
2439 connection_
.SetFromConfig(config
);
2440 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
2441 use_tagging_decrypter();
2443 const uint8 tag
= 0x07;
2444 framer_
.SetEncrypter(ENCRYPTION_INITIAL
, new TaggingEncrypter(tag
));
2446 // Process an encrypted packet which can not yet be decrypted which should
2447 // result in the packet being buffered.
2448 for (QuicPacketSequenceNumber i
= 1; i
<= 100; ++i
) {
2449 ProcessDataPacketAtLevel(i
, 0, kEntropyFlag
, ENCRYPTION_INITIAL
);
2452 // Transition to the new encryption state and process another encrypted packet
2453 // which should result in the original packets being processed.
2454 connection_
.SetDecrypter(new StrictTaggingDecrypter(tag
), ENCRYPTION_INITIAL
);
2455 connection_
.SetDefaultEncryptionLevel(ENCRYPTION_INITIAL
);
2456 connection_
.SetEncrypter(ENCRYPTION_INITIAL
, new TaggingEncrypter(tag
));
2457 EXPECT_CALL(visitor_
, OnStreamFrames(_
)).Times(101);
2458 ProcessDataPacketAtLevel(101, 0, kEntropyFlag
, ENCRYPTION_INITIAL
);
2460 // Finally, process a third packet and note that we do not reprocess the
2462 EXPECT_CALL(visitor_
, OnStreamFrames(_
)).Times(1);
2463 ProcessDataPacketAtLevel(102, 0, kEntropyFlag
, ENCRYPTION_INITIAL
);
2466 TEST_P(QuicConnectionTest
, TestRetransmitOrder
) {
2467 QuicByteCount first_packet_size
;
2468 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
)).WillOnce(
2469 DoAll(SaveArg
<3>(&first_packet_size
), Return(true)));
2471 connection_
.SendStreamDataWithString(3, "first_packet", 0, !kFin
, nullptr);
2472 QuicByteCount second_packet_size
;
2473 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
)).WillOnce(
2474 DoAll(SaveArg
<3>(&second_packet_size
), Return(true)));
2475 connection_
.SendStreamDataWithString(3, "second_packet", 12, !kFin
, nullptr);
2476 EXPECT_NE(first_packet_size
, second_packet_size
);
2477 // Advance the clock by huge time to make sure packets will be retransmitted.
2478 clock_
.AdvanceTime(QuicTime::Delta::FromSeconds(10));
2479 EXPECT_CALL(*send_algorithm_
, OnRetransmissionTimeout(true));
2482 EXPECT_CALL(*send_algorithm_
,
2483 OnPacketSent(_
, _
, _
, first_packet_size
, _
));
2484 EXPECT_CALL(*send_algorithm_
,
2485 OnPacketSent(_
, _
, _
, second_packet_size
, _
));
2487 connection_
.GetRetransmissionAlarm()->Fire();
2489 // Advance again and expect the packets to be sent again in the same order.
2490 clock_
.AdvanceTime(QuicTime::Delta::FromSeconds(20));
2491 EXPECT_CALL(*send_algorithm_
, OnRetransmissionTimeout(true));
2494 EXPECT_CALL(*send_algorithm_
,
2495 OnPacketSent(_
, _
, _
, first_packet_size
, _
));
2496 EXPECT_CALL(*send_algorithm_
,
2497 OnPacketSent(_
, _
, _
, second_packet_size
, _
));
2499 connection_
.GetRetransmissionAlarm()->Fire();
2502 TEST_P(QuicConnectionTest
, RetransmissionCountCalculation
) {
2503 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
2504 QuicPacketSequenceNumber original_sequence_number
;
2505 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
))
2506 .WillOnce(DoAll(SaveArg
<2>(&original_sequence_number
), Return(true)));
2507 connection_
.SendStreamDataWithString(3, "foo", 0, !kFin
, nullptr);
2509 EXPECT_TRUE(QuicConnectionPeer::IsSavedForRetransmission(
2510 &connection_
, original_sequence_number
));
2511 EXPECT_FALSE(QuicConnectionPeer::IsRetransmission(
2512 &connection_
, original_sequence_number
));
2513 // Force retransmission due to RTO.
2514 clock_
.AdvanceTime(QuicTime::Delta::FromSeconds(10));
2515 EXPECT_CALL(*send_algorithm_
, OnRetransmissionTimeout(true));
2516 QuicPacketSequenceNumber rto_sequence_number
;
2517 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
))
2518 .WillOnce(DoAll(SaveArg
<2>(&rto_sequence_number
), Return(true)));
2519 connection_
.GetRetransmissionAlarm()->Fire();
2520 EXPECT_FALSE(QuicConnectionPeer::IsSavedForRetransmission(
2521 &connection_
, original_sequence_number
));
2522 ASSERT_TRUE(QuicConnectionPeer::IsSavedForRetransmission(
2523 &connection_
, rto_sequence_number
));
2524 EXPECT_TRUE(QuicConnectionPeer::IsRetransmission(
2525 &connection_
, rto_sequence_number
));
2526 // Once by explicit nack.
2527 SequenceNumberSet lost_packets
;
2528 lost_packets
.insert(rto_sequence_number
);
2529 EXPECT_CALL(*loss_algorithm_
, DetectLostPackets(_
, _
, _
, _
))
2530 .WillOnce(Return(lost_packets
));
2531 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
2532 QuicPacketSequenceNumber nack_sequence_number
= 0;
2533 // Ack packets might generate some other packets, which are not
2534 // retransmissions. (More ack packets).
2535 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
))
2536 .Times(AnyNumber());
2537 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
))
2538 .WillOnce(DoAll(SaveArg
<2>(&nack_sequence_number
), Return(true)));
2539 QuicAckFrame ack
= InitAckFrame(rto_sequence_number
);
2540 // Nack the retransmitted packet.
2541 NackPacket(original_sequence_number
, &ack
);
2542 NackPacket(rto_sequence_number
, &ack
);
2543 ProcessAckPacket(&ack
);
2545 ASSERT_NE(0u, nack_sequence_number
);
2546 EXPECT_FALSE(QuicConnectionPeer::IsSavedForRetransmission(
2547 &connection_
, rto_sequence_number
));
2548 ASSERT_TRUE(QuicConnectionPeer::IsSavedForRetransmission(
2549 &connection_
, nack_sequence_number
));
2550 EXPECT_TRUE(QuicConnectionPeer::IsRetransmission(
2551 &connection_
, nack_sequence_number
));
2554 TEST_P(QuicConnectionTest
, SetRTOAfterWritingToSocket
) {
2556 connection_
.SendStreamDataWithString(1, "foo", 0, !kFin
, nullptr);
2557 // Make sure that RTO is not started when the packet is queued.
2558 EXPECT_FALSE(connection_
.GetRetransmissionAlarm()->IsSet());
2560 // Test that RTO is started once we write to the socket.
2561 writer_
->SetWritable();
2562 connection_
.OnCanWrite();
2563 EXPECT_TRUE(connection_
.GetRetransmissionAlarm()->IsSet());
2566 TEST_P(QuicConnectionTest
, DelayRTOWithAckReceipt
) {
2567 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
2568 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
))
2570 connection_
.SendStreamDataWithString(2, "foo", 0, !kFin
, nullptr);
2571 connection_
.SendStreamDataWithString(3, "bar", 0, !kFin
, nullptr);
2572 QuicAlarm
* retransmission_alarm
= connection_
.GetRetransmissionAlarm();
2573 EXPECT_TRUE(retransmission_alarm
->IsSet());
2574 EXPECT_EQ(clock_
.Now().Add(DefaultRetransmissionTime()),
2575 retransmission_alarm
->deadline());
2577 // Advance the time right before the RTO, then receive an ack for the first
2578 // packet to delay the RTO.
2579 clock_
.AdvanceTime(DefaultRetransmissionTime());
2580 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
2581 QuicAckFrame ack
= InitAckFrame(1);
2582 ProcessAckPacket(&ack
);
2583 EXPECT_TRUE(retransmission_alarm
->IsSet());
2584 EXPECT_GT(retransmission_alarm
->deadline(), clock_
.Now());
2586 // Move forward past the original RTO and ensure the RTO is still pending.
2587 clock_
.AdvanceTime(DefaultRetransmissionTime().Multiply(2));
2589 // Ensure the second packet gets retransmitted when it finally fires.
2590 EXPECT_TRUE(retransmission_alarm
->IsSet());
2591 EXPECT_LT(retransmission_alarm
->deadline(), clock_
.ApproximateNow());
2592 EXPECT_CALL(*send_algorithm_
, OnRetransmissionTimeout(true));
2593 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
));
2594 // Manually cancel the alarm to simulate a real test.
2595 connection_
.GetRetransmissionAlarm()->Fire();
2597 // The new retransmitted sequence number should set the RTO to a larger value
2599 EXPECT_TRUE(retransmission_alarm
->IsSet());
2600 QuicTime next_rto_time
= retransmission_alarm
->deadline();
2601 QuicTime expected_rto_time
=
2602 connection_
.sent_packet_manager().GetRetransmissionTime();
2603 EXPECT_EQ(next_rto_time
, expected_rto_time
);
2606 TEST_P(QuicConnectionTest
, TestQueued
) {
2607 EXPECT_EQ(0u, connection_
.NumQueuedPackets());
2609 connection_
.SendStreamDataWithString(1, "foo", 0, !kFin
, nullptr);
2610 EXPECT_EQ(1u, connection_
.NumQueuedPackets());
2612 // Unblock the writes and actually send.
2613 writer_
->SetWritable();
2614 connection_
.OnCanWrite();
2615 EXPECT_EQ(0u, connection_
.NumQueuedPackets());
2618 TEST_P(QuicConnectionTest
, CloseFecGroup
) {
2619 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
2620 // Don't send missing packet 1.
2621 // Don't send missing packet 2.
2622 ProcessFecProtectedPacket(3, false, !kEntropyFlag
);
2623 // Don't send missing FEC packet 3.
2624 ASSERT_EQ(1u, connection_
.NumFecGroups());
2626 // Now send non-fec protected ack packet and close the group.
2627 peer_creator_
.set_sequence_number(4);
2628 QuicStopWaitingFrame frame
= InitStopWaitingFrame(5);
2629 ProcessStopWaitingPacket(&frame
);
2630 ASSERT_EQ(0u, connection_
.NumFecGroups());
2633 TEST_P(QuicConnectionTest
, NoQuicCongestionFeedbackFrame
) {
2634 SendAckPacketToPeer();
2635 EXPECT_TRUE(writer_
->feedback_frames().empty());
2638 TEST_P(QuicConnectionTest
, WithQuicCongestionFeedbackFrame
) {
2639 QuicCongestionFeedbackFrame info
;
2641 info
.tcp
.receive_window
= 0x4030;
2643 // After QUIC_VERSION_22, do not send TCP Congestion Feedback Frames anymore.
2644 if (version() > QUIC_VERSION_22
) {
2645 SendAckPacketToPeer();
2646 ASSERT_TRUE(writer_
->feedback_frames().empty());
2648 // Only SetFeedback in this case because SetFeedback will create a receive
2649 // algorithm which is how the received_packet_manager checks if it should be
2650 // creating TCP Congestion Feedback Frames.
2652 SendAckPacketToPeer();
2653 ASSERT_FALSE(writer_
->feedback_frames().empty());
2654 ASSERT_EQ(kTCP
, writer_
->feedback_frames()[0].type
);
2658 TEST_P(QuicConnectionTest
, UpdateQuicCongestionFeedbackFrame
) {
2659 SendAckPacketToPeer();
2660 EXPECT_CALL(*receive_algorithm_
, RecordIncomingPacket(_
, _
, _
));
2661 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
2665 TEST_P(QuicConnectionTest
, DontUpdateQuicCongestionFeedbackFrameForRevived
) {
2666 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
2667 SendAckPacketToPeer();
2668 // Process an FEC packet, and revive the missing data packet
2669 // but only contact the receive_algorithm once.
2670 EXPECT_CALL(*receive_algorithm_
, RecordIncomingPacket(_
, _
, _
));
2671 ProcessFecPacket(2, 1, true, !kEntropyFlag
, nullptr);
2674 TEST_P(QuicConnectionTest
, InitialTimeout
) {
2675 if (!FLAGS_quic_unified_timeouts
) {
2676 EXPECT_TRUE(connection_
.connected());
2677 EXPECT_CALL(visitor_
, OnConnectionClosed(QUIC_CONNECTION_TIMED_OUT
, false));
2678 EXPECT_CALL(*send_algorithm_
,
2679 OnPacketSent(_
, _
, _
, _
, _
)).Times(AnyNumber());
2681 QuicTime default_timeout
= clock_
.ApproximateNow().Add(
2682 QuicTime::Delta::FromSeconds(kDefaultIdleTimeoutSecs
));
2683 EXPECT_EQ(default_timeout
, connection_
.GetTimeoutAlarm()->deadline());
2685 // Simulate the timeout alarm firing.
2686 clock_
.AdvanceTime(QuicTime::Delta::FromSeconds(kDefaultIdleTimeoutSecs
));
2687 connection_
.GetTimeoutAlarm()->Fire();
2689 EXPECT_FALSE(connection_
.GetTimeoutAlarm()->IsSet());
2690 EXPECT_FALSE(connection_
.connected());
2692 EXPECT_FALSE(connection_
.GetAckAlarm()->IsSet());
2693 EXPECT_FALSE(connection_
.GetPingAlarm()->IsSet());
2694 EXPECT_FALSE(connection_
.GetResumeWritesAlarm()->IsSet());
2695 EXPECT_FALSE(connection_
.GetRetransmissionAlarm()->IsSet());
2696 EXPECT_FALSE(connection_
.GetSendAlarm()->IsSet());
2699 EXPECT_TRUE(connection_
.connected());
2700 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
)).Times(AnyNumber());
2701 EXPECT_FALSE(connection_
.GetTimeoutAlarm()->IsSet());
2703 // SetFromConfig sets the initial timeouts before negotiation.
2704 EXPECT_CALL(*send_algorithm_
, SetFromConfig(_
, _
));
2706 connection_
.SetFromConfig(config
);
2707 // Subtract a second from the idle timeout on the client side.
2708 QuicTime default_timeout
= clock_
.ApproximateNow().Add(
2709 QuicTime::Delta::FromSeconds(kInitialIdleTimeoutSecs
- 1));
2710 EXPECT_EQ(default_timeout
, connection_
.GetTimeoutAlarm()->deadline());
2712 EXPECT_CALL(visitor_
, OnConnectionClosed(QUIC_CONNECTION_TIMED_OUT
, false));
2713 // Simulate the timeout alarm firing.
2715 QuicTime::Delta::FromSeconds(kInitialIdleTimeoutSecs
- 1));
2716 connection_
.GetTimeoutAlarm()->Fire();
2718 EXPECT_FALSE(connection_
.GetTimeoutAlarm()->IsSet());
2719 EXPECT_FALSE(connection_
.connected());
2721 EXPECT_FALSE(connection_
.GetAckAlarm()->IsSet());
2722 EXPECT_FALSE(connection_
.GetPingAlarm()->IsSet());
2723 EXPECT_FALSE(connection_
.GetResumeWritesAlarm()->IsSet());
2724 EXPECT_FALSE(connection_
.GetRetransmissionAlarm()->IsSet());
2725 EXPECT_FALSE(connection_
.GetSendAlarm()->IsSet());
2728 TEST_P(QuicConnectionTest
, OverallTimeout
) {
2729 // Use a shorter overall connection timeout than idle timeout for this test.
2730 const QuicTime::Delta timeout
= QuicTime::Delta::FromSeconds(5);
2731 connection_
.SetNetworkTimeouts(timeout
, timeout
);
2732 EXPECT_TRUE(connection_
.connected());
2733 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
)).Times(AnyNumber());
2735 QuicTime overall_timeout
= clock_
.ApproximateNow().Add(timeout
).Subtract(
2736 QuicTime::Delta::FromSeconds(1));
2737 EXPECT_EQ(overall_timeout
, connection_
.GetTimeoutAlarm()->deadline());
2738 EXPECT_TRUE(connection_
.connected());
2740 // Send and ack new data 3 seconds later to lengthen the idle timeout.
2741 SendStreamDataToPeer(1, "GET /", 0, kFin
, nullptr);
2742 clock_
.AdvanceTime(QuicTime::Delta::FromSeconds(3));
2743 QuicAckFrame frame
= InitAckFrame(1);
2744 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
2745 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
2746 ProcessAckPacket(&frame
);
2748 // Fire early to verify it wouldn't timeout yet.
2749 connection_
.GetTimeoutAlarm()->Fire();
2750 EXPECT_TRUE(connection_
.GetTimeoutAlarm()->IsSet());
2751 EXPECT_TRUE(connection_
.connected());
2753 clock_
.AdvanceTime(timeout
.Subtract(QuicTime::Delta::FromSeconds(2)));
2755 EXPECT_CALL(visitor_
,
2756 OnConnectionClosed(QUIC_CONNECTION_OVERALL_TIMED_OUT
, false));
2757 // Simulate the timeout alarm firing.
2758 connection_
.GetTimeoutAlarm()->Fire();
2760 EXPECT_FALSE(connection_
.GetTimeoutAlarm()->IsSet());
2761 EXPECT_FALSE(connection_
.connected());
2763 EXPECT_FALSE(connection_
.GetAckAlarm()->IsSet());
2764 EXPECT_FALSE(connection_
.GetPingAlarm()->IsSet());
2765 EXPECT_FALSE(connection_
.GetResumeWritesAlarm()->IsSet());
2766 EXPECT_FALSE(connection_
.GetRetransmissionAlarm()->IsSet());
2767 EXPECT_FALSE(connection_
.GetSendAlarm()->IsSet());
2770 TEST_P(QuicConnectionTest
, PingAfterSend
) {
2771 EXPECT_TRUE(connection_
.connected());
2772 EXPECT_CALL(visitor_
, HasOpenDataStreams()).WillRepeatedly(Return(true));
2773 EXPECT_FALSE(connection_
.GetPingAlarm()->IsSet());
2775 // Advance to 5ms, and send a packet to the peer, which will set
2777 clock_
.AdvanceTime(QuicTime::Delta::FromMilliseconds(5));
2778 EXPECT_FALSE(connection_
.GetRetransmissionAlarm()->IsSet());
2779 SendStreamDataToPeer(1, "GET /", 0, kFin
, nullptr);
2780 EXPECT_TRUE(connection_
.GetPingAlarm()->IsSet());
2781 EXPECT_EQ(clock_
.ApproximateNow().Add(QuicTime::Delta::FromSeconds(15)),
2782 connection_
.GetPingAlarm()->deadline());
2784 // Now recevie and ACK of the previous packet, which will move the
2785 // ping alarm forward.
2786 clock_
.AdvanceTime(QuicTime::Delta::FromMilliseconds(5));
2787 QuicAckFrame frame
= InitAckFrame(1);
2788 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
2789 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
2790 ProcessAckPacket(&frame
);
2791 EXPECT_TRUE(connection_
.GetPingAlarm()->IsSet());
2792 // The ping timer is set slightly less than 15 seconds in the future, because
2793 // of the 1s ping timer alarm granularity.
2794 EXPECT_EQ(clock_
.ApproximateNow().Add(QuicTime::Delta::FromSeconds(15))
2795 .Subtract(QuicTime::Delta::FromMilliseconds(5)),
2796 connection_
.GetPingAlarm()->deadline());
2799 clock_
.AdvanceTime(QuicTime::Delta::FromSeconds(15));
2800 connection_
.GetPingAlarm()->Fire();
2801 EXPECT_EQ(1u, writer_
->frame_count());
2802 ASSERT_EQ(1u, writer_
->ping_frames().size());
2805 EXPECT_CALL(visitor_
, HasOpenDataStreams()).WillRepeatedly(Return(false));
2806 clock_
.AdvanceTime(QuicTime::Delta::FromMilliseconds(5));
2807 SendAckPacketToPeer();
2809 EXPECT_FALSE(connection_
.GetPingAlarm()->IsSet());
2812 TEST_P(QuicConnectionTest
, TimeoutAfterSend
) {
2813 if (!FLAGS_quic_unified_timeouts
) {
2814 EXPECT_TRUE(connection_
.connected());
2816 QuicTime default_timeout
= clock_
.ApproximateNow().Add(
2817 QuicTime::Delta::FromSeconds(kDefaultIdleTimeoutSecs
));
2819 // When we send a packet, the timeout will change to 5000 +
2820 // kDefaultInitialTimeoutSecs.
2821 clock_
.AdvanceTime(QuicTime::Delta::FromMilliseconds(5));
2823 // Send an ack so we don't set the retransmission alarm.
2824 SendAckPacketToPeer();
2825 EXPECT_EQ(default_timeout
, connection_
.GetTimeoutAlarm()->deadline());
2827 // The original alarm will fire. We should not time out because we had a
2828 // network event at t=5000. The alarm will reregister.
2829 clock_
.AdvanceTime(QuicTime::Delta::FromMicroseconds(
2830 kDefaultIdleTimeoutSecs
* 1000000 - 5000));
2831 EXPECT_EQ(default_timeout
, clock_
.ApproximateNow());
2832 connection_
.GetTimeoutAlarm()->Fire();
2833 EXPECT_TRUE(connection_
.GetTimeoutAlarm()->IsSet());
2834 EXPECT_TRUE(connection_
.connected());
2835 EXPECT_EQ(default_timeout
.Add(QuicTime::Delta::FromMilliseconds(5)),
2836 connection_
.GetTimeoutAlarm()->deadline());
2838 // This time, we should time out.
2839 EXPECT_CALL(visitor_
, OnConnectionClosed(QUIC_CONNECTION_TIMED_OUT
, false));
2840 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
));
2841 clock_
.AdvanceTime(QuicTime::Delta::FromMilliseconds(5));
2842 EXPECT_EQ(default_timeout
.Add(QuicTime::Delta::FromMilliseconds(5)),
2843 clock_
.ApproximateNow());
2844 connection_
.GetTimeoutAlarm()->Fire();
2845 EXPECT_FALSE(connection_
.GetTimeoutAlarm()->IsSet());
2846 EXPECT_FALSE(connection_
.connected());
2849 EXPECT_TRUE(connection_
.connected());
2850 EXPECT_CALL(*send_algorithm_
, SetFromConfig(_
, _
));
2852 connection_
.SetFromConfig(config
);
2854 const QuicTime::Delta initial_idle_timeout
=
2855 QuicTime::Delta::FromSeconds(kInitialIdleTimeoutSecs
- 1);
2856 const QuicTime::Delta five_ms
= QuicTime::Delta::FromMilliseconds(5);
2857 QuicTime default_timeout
= clock_
.ApproximateNow().Add(initial_idle_timeout
);
2859 // When we send a packet, the timeout will change to 5ms +
2860 // kInitialIdleTimeoutSecs.
2861 clock_
.AdvanceTime(five_ms
);
2863 // Send an ack so we don't set the retransmission alarm.
2864 SendAckPacketToPeer();
2865 EXPECT_EQ(default_timeout
, connection_
.GetTimeoutAlarm()->deadline());
2867 // The original alarm will fire. We should not time out because we had a
2868 // network event at t=5ms. The alarm will reregister.
2869 clock_
.AdvanceTime(initial_idle_timeout
.Subtract(five_ms
));
2870 EXPECT_EQ(default_timeout
, clock_
.ApproximateNow());
2871 connection_
.GetTimeoutAlarm()->Fire();
2872 EXPECT_TRUE(connection_
.GetTimeoutAlarm()->IsSet());
2873 EXPECT_TRUE(connection_
.connected());
2874 EXPECT_EQ(default_timeout
.Add(five_ms
),
2875 connection_
.GetTimeoutAlarm()->deadline());
2877 // This time, we should time out.
2878 EXPECT_CALL(visitor_
, OnConnectionClosed(QUIC_CONNECTION_TIMED_OUT
, false));
2879 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
));
2880 clock_
.AdvanceTime(five_ms
);
2881 EXPECT_EQ(default_timeout
.Add(five_ms
), clock_
.ApproximateNow());
2882 connection_
.GetTimeoutAlarm()->Fire();
2883 EXPECT_FALSE(connection_
.GetTimeoutAlarm()->IsSet());
2884 EXPECT_FALSE(connection_
.connected());
2887 TEST_P(QuicConnectionTest
, SendScheduler
) {
2888 // Test that if we send a packet without delay, it is not queued.
2889 QuicPacket
* packet
= ConstructDataPacket(1, 0, !kEntropyFlag
);
2890 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
));
2891 connection_
.SendPacket(
2892 ENCRYPTION_NONE
, 1, packet
, kTestEntropyHash
, HAS_RETRANSMITTABLE_DATA
);
2893 EXPECT_EQ(0u, connection_
.NumQueuedPackets());
2896 TEST_P(QuicConnectionTest
, SendSchedulerEAGAIN
) {
2897 QuicPacket
* packet
= ConstructDataPacket(1, 0, !kEntropyFlag
);
2899 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, 1, _
, _
)).Times(0);
2900 connection_
.SendPacket(
2901 ENCRYPTION_NONE
, 1, packet
, kTestEntropyHash
, HAS_RETRANSMITTABLE_DATA
);
2902 EXPECT_EQ(1u, connection_
.NumQueuedPackets());
2905 TEST_P(QuicConnectionTest
, TestQueueLimitsOnSendStreamData
) {
2906 // All packets carry version info till version is negotiated.
2907 size_t payload_length
;
2908 size_t length
= GetPacketLengthForOneStream(
2909 connection_
.version(), kIncludeVersion
, PACKET_1BYTE_SEQUENCE_NUMBER
,
2910 NOT_IN_FEC_GROUP
, &payload_length
);
2911 QuicConnectionPeer::GetPacketCreator(&connection_
)->set_max_packet_length(
2914 // Queue the first packet.
2915 EXPECT_CALL(*send_algorithm_
,
2916 TimeUntilSend(_
, _
, _
)).WillOnce(
2917 testing::Return(QuicTime::Delta::FromMicroseconds(10)));
2918 const string
payload(payload_length
, 'a');
2919 EXPECT_EQ(0u, connection_
.SendStreamDataWithString(3, payload
, 0, !kFin
,
2920 nullptr).bytes_consumed
);
2921 EXPECT_EQ(0u, connection_
.NumQueuedPackets());
2924 TEST_P(QuicConnectionTest
, LoopThroughSendingPackets
) {
2925 // All packets carry version info till version is negotiated.
2926 size_t payload_length
;
2927 // GetPacketLengthForOneStream() assumes a stream offset of 0 in determining
2928 // packet length. The size of the offset field in a stream frame is 0 for
2929 // offset 0, and 2 for non-zero offsets up through 16K. Increase
2930 // max_packet_length by 2 so that subsequent packets containing subsequent
2931 // stream frames with non-zero offets will fit within the packet length.
2932 size_t length
= 2 + GetPacketLengthForOneStream(
2933 connection_
.version(), kIncludeVersion
, PACKET_1BYTE_SEQUENCE_NUMBER
,
2934 NOT_IN_FEC_GROUP
, &payload_length
);
2935 QuicConnectionPeer::GetPacketCreator(&connection_
)->set_max_packet_length(
2938 // Queue the first packet.
2939 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
)).Times(7);
2940 // The first stream frame will have 2 fewer overhead bytes than the other six.
2941 const string
payload(payload_length
* 7 + 2, 'a');
2942 EXPECT_EQ(payload
.size(),
2943 connection_
.SendStreamDataWithString(1, payload
, 0, !kFin
, nullptr)
2947 TEST_P(QuicConnectionTest
, SendDelayedAck
) {
2948 QuicTime ack_time
= clock_
.ApproximateNow().Add(DefaultDelayedAckTime());
2949 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
2950 EXPECT_FALSE(connection_
.GetAckAlarm()->IsSet());
2951 const uint8 tag
= 0x07;
2952 connection_
.SetDecrypter(new StrictTaggingDecrypter(tag
),
2953 ENCRYPTION_INITIAL
);
2954 framer_
.SetEncrypter(ENCRYPTION_INITIAL
, new TaggingEncrypter(tag
));
2955 // Process a packet from the non-crypto stream.
2956 frame1_
.stream_id
= 3;
2958 // The same as ProcessPacket(1) except that ENCRYPTION_INITIAL is used
2959 // instead of ENCRYPTION_NONE.
2960 EXPECT_CALL(visitor_
, OnStreamFrames(_
)).Times(1);
2961 ProcessDataPacketAtLevel(1, 0, !kEntropyFlag
, ENCRYPTION_INITIAL
);
2963 // Check if delayed ack timer is running for the expected interval.
2964 EXPECT_TRUE(connection_
.GetAckAlarm()->IsSet());
2965 EXPECT_EQ(ack_time
, connection_
.GetAckAlarm()->deadline());
2966 // Simulate delayed ack alarm firing.
2967 connection_
.GetAckAlarm()->Fire();
2968 // Check that ack is sent and that delayed ack alarm is reset.
2969 EXPECT_EQ(2u, writer_
->frame_count());
2970 EXPECT_FALSE(writer_
->stop_waiting_frames().empty());
2971 EXPECT_FALSE(writer_
->ack_frames().empty());
2972 EXPECT_FALSE(connection_
.GetAckAlarm()->IsSet());
2975 TEST_P(QuicConnectionTest
, SendEarlyDelayedAckForCrypto
) {
2976 QuicTime ack_time
= clock_
.ApproximateNow();
2977 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
2978 EXPECT_FALSE(connection_
.GetAckAlarm()->IsSet());
2979 // Process a packet from the crypto stream, which is frame1_'s default.
2981 // Check if delayed ack timer is running for the expected interval.
2982 EXPECT_TRUE(connection_
.GetAckAlarm()->IsSet());
2983 EXPECT_EQ(ack_time
, connection_
.GetAckAlarm()->deadline());
2984 // Simulate delayed ack alarm firing.
2985 connection_
.GetAckAlarm()->Fire();
2986 // Check that ack is sent and that delayed ack alarm is reset.
2987 EXPECT_EQ(2u, writer_
->frame_count());
2988 EXPECT_FALSE(writer_
->stop_waiting_frames().empty());
2989 EXPECT_FALSE(writer_
->ack_frames().empty());
2990 EXPECT_FALSE(connection_
.GetAckAlarm()->IsSet());
2993 TEST_P(QuicConnectionTest
, SendDelayedAckOnSecondPacket
) {
2994 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
2997 // Check that ack is sent and that delayed ack alarm is reset.
2998 EXPECT_EQ(2u, writer_
->frame_count());
2999 EXPECT_FALSE(writer_
->stop_waiting_frames().empty());
3000 EXPECT_FALSE(writer_
->ack_frames().empty());
3001 EXPECT_FALSE(connection_
.GetAckAlarm()->IsSet());
3004 TEST_P(QuicConnectionTest
, NoAckOnOldNacks
) {
3005 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
3006 // Drop one packet, triggering a sequence of acks.
3008 size_t frames_per_ack
= 2;
3009 EXPECT_EQ(frames_per_ack
, writer_
->frame_count());
3010 EXPECT_FALSE(writer_
->ack_frames().empty());
3013 EXPECT_EQ(frames_per_ack
, writer_
->frame_count());
3014 EXPECT_FALSE(writer_
->ack_frames().empty());
3017 EXPECT_EQ(frames_per_ack
, writer_
->frame_count());
3018 EXPECT_FALSE(writer_
->ack_frames().empty());
3021 EXPECT_EQ(frames_per_ack
, writer_
->frame_count());
3022 EXPECT_FALSE(writer_
->ack_frames().empty());
3024 // Now only set the timer on the 6th packet, instead of sending another ack.
3026 EXPECT_EQ(0u, writer_
->frame_count());
3027 EXPECT_TRUE(connection_
.GetAckAlarm()->IsSet());
3030 TEST_P(QuicConnectionTest
, SendDelayedAckOnOutgoingPacket
) {
3031 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
3033 connection_
.SendStreamDataWithString(kClientDataStreamId1
, "foo", 0, !kFin
,
3035 // Check that ack is bundled with outgoing data and that delayed ack
3037 EXPECT_EQ(3u, writer_
->frame_count());
3038 EXPECT_FALSE(writer_
->stop_waiting_frames().empty());
3039 EXPECT_FALSE(writer_
->ack_frames().empty());
3040 EXPECT_FALSE(connection_
.GetAckAlarm()->IsSet());
3043 TEST_P(QuicConnectionTest
, SendDelayedAckOnOutgoingCryptoPacket
) {
3044 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
3046 connection_
.SendStreamDataWithString(kCryptoStreamId
, "foo", 0, !kFin
,
3048 // Check that ack is bundled with outgoing crypto data.
3049 EXPECT_EQ(3u, writer_
->frame_count());
3050 EXPECT_FALSE(writer_
->ack_frames().empty());
3051 EXPECT_FALSE(connection_
.GetAckAlarm()->IsSet());
3054 TEST_P(QuicConnectionTest
, BlockAndBufferOnFirstCHLOPacketOfTwo
) {
3055 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
3058 writer_
->set_is_write_blocked_data_buffered(true);
3059 connection_
.SendStreamDataWithString(kCryptoStreamId
, "foo", 0, !kFin
,
3061 EXPECT_TRUE(writer_
->IsWriteBlocked());
3062 EXPECT_FALSE(connection_
.HasQueuedData());
3063 connection_
.SendStreamDataWithString(kCryptoStreamId
, "bar", 3, !kFin
,
3065 EXPECT_TRUE(writer_
->IsWriteBlocked());
3066 EXPECT_TRUE(connection_
.HasQueuedData());
3069 TEST_P(QuicConnectionTest
, BundleAckForSecondCHLO
) {
3070 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
3071 EXPECT_FALSE(connection_
.GetAckAlarm()->IsSet());
3072 EXPECT_CALL(visitor_
, OnCanWrite()).WillOnce(
3073 IgnoreResult(InvokeWithoutArgs(&connection_
,
3074 &TestConnection::SendCryptoStreamData
)));
3075 // Process a packet from the crypto stream, which is frame1_'s default.
3076 // Receiving the CHLO as packet 2 first will cause the connection to
3077 // immediately send an ack, due to the packet gap.
3079 // Check that ack is sent and that delayed ack alarm is reset.
3080 EXPECT_EQ(3u, writer_
->frame_count());
3081 EXPECT_FALSE(writer_
->stop_waiting_frames().empty());
3082 EXPECT_EQ(1u, writer_
->stream_frames().size());
3083 EXPECT_FALSE(writer_
->ack_frames().empty());
3084 EXPECT_FALSE(connection_
.GetAckAlarm()->IsSet());
3087 TEST_P(QuicConnectionTest
, BundleAckWithDataOnIncomingAck
) {
3088 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
3089 connection_
.SendStreamDataWithString(kClientDataStreamId1
, "foo", 0, !kFin
,
3091 connection_
.SendStreamDataWithString(kClientDataStreamId1
, "foo", 3, !kFin
,
3093 // Ack the second packet, which will retransmit the first packet.
3094 QuicAckFrame ack
= InitAckFrame(2);
3095 NackPacket(1, &ack
);
3096 SequenceNumberSet lost_packets
;
3097 lost_packets
.insert(1);
3098 EXPECT_CALL(*loss_algorithm_
, DetectLostPackets(_
, _
, _
, _
))
3099 .WillOnce(Return(lost_packets
));
3100 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
3101 ProcessAckPacket(&ack
);
3102 EXPECT_EQ(1u, writer_
->frame_count());
3103 EXPECT_EQ(1u, writer_
->stream_frames().size());
3106 // Now ack the retransmission, which will both raise the high water mark
3107 // and see if there is more data to send.
3108 ack
= InitAckFrame(3);
3109 NackPacket(1, &ack
);
3110 EXPECT_CALL(*loss_algorithm_
, DetectLostPackets(_
, _
, _
, _
))
3111 .WillOnce(Return(SequenceNumberSet()));
3112 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
3113 ProcessAckPacket(&ack
);
3115 // Check that no packet is sent and the ack alarm isn't set.
3116 EXPECT_EQ(0u, writer_
->frame_count());
3117 EXPECT_FALSE(connection_
.GetAckAlarm()->IsSet());
3120 // Send the same ack, but send both data and an ack together.
3121 ack
= InitAckFrame(3);
3122 NackPacket(1, &ack
);
3123 EXPECT_CALL(*loss_algorithm_
, DetectLostPackets(_
, _
, _
, _
))
3124 .WillOnce(Return(SequenceNumberSet()));
3125 EXPECT_CALL(visitor_
, OnCanWrite()).WillOnce(
3126 IgnoreResult(InvokeWithoutArgs(
3128 &TestConnection::EnsureWritableAndSendStreamData5
)));
3129 ProcessAckPacket(&ack
);
3131 // Check that ack is bundled with outgoing data and the delayed ack
3133 EXPECT_EQ(3u, writer_
->frame_count());
3134 EXPECT_FALSE(writer_
->stop_waiting_frames().empty());
3135 EXPECT_FALSE(writer_
->ack_frames().empty());
3136 EXPECT_EQ(1u, writer_
->stream_frames().size());
3137 EXPECT_FALSE(connection_
.GetAckAlarm()->IsSet());
3140 TEST_P(QuicConnectionTest
, NoAckSentForClose
) {
3141 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
3143 EXPECT_CALL(visitor_
, OnConnectionClosed(QUIC_PEER_GOING_AWAY
, true));
3144 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
)).Times(0);
3145 ProcessClosePacket(2, 0);
3148 TEST_P(QuicConnectionTest
, SendWhenDisconnected
) {
3149 EXPECT_TRUE(connection_
.connected());
3150 EXPECT_CALL(visitor_
, OnConnectionClosed(QUIC_PEER_GOING_AWAY
, false));
3151 connection_
.CloseConnection(QUIC_PEER_GOING_AWAY
, false);
3152 EXPECT_FALSE(connection_
.connected());
3153 EXPECT_FALSE(connection_
.CanWriteStreamData());
3154 QuicPacket
* packet
= ConstructDataPacket(1, 0, !kEntropyFlag
);
3155 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, 1, _
, _
)).Times(0);
3156 connection_
.SendPacket(
3157 ENCRYPTION_NONE
, 1, packet
, kTestEntropyHash
, HAS_RETRANSMITTABLE_DATA
);
3160 TEST_P(QuicConnectionTest
, PublicReset
) {
3161 QuicPublicResetPacket header
;
3162 header
.public_header
.connection_id
= connection_id_
;
3163 header
.public_header
.reset_flag
= true;
3164 header
.public_header
.version_flag
= false;
3165 header
.rejected_sequence_number
= 10101;
3166 scoped_ptr
<QuicEncryptedPacket
> packet(
3167 framer_
.BuildPublicResetPacket(header
));
3168 EXPECT_CALL(visitor_
, OnConnectionClosed(QUIC_PUBLIC_RESET
, true));
3169 connection_
.ProcessUdpPacket(IPEndPoint(), IPEndPoint(), *packet
);
3172 TEST_P(QuicConnectionTest
, GoAway
) {
3173 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
3175 QuicGoAwayFrame goaway
;
3176 goaway
.last_good_stream_id
= 1;
3177 goaway
.error_code
= QUIC_PEER_GOING_AWAY
;
3178 goaway
.reason_phrase
= "Going away.";
3179 EXPECT_CALL(visitor_
, OnGoAway(_
));
3180 ProcessGoAwayPacket(&goaway
);
3183 TEST_P(QuicConnectionTest
, WindowUpdate
) {
3184 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
3186 QuicWindowUpdateFrame window_update
;
3187 window_update
.stream_id
= 3;
3188 window_update
.byte_offset
= 1234;
3189 EXPECT_CALL(visitor_
, OnWindowUpdateFrames(_
));
3190 ProcessFramePacket(QuicFrame(&window_update
));
3193 TEST_P(QuicConnectionTest
, Blocked
) {
3194 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
3196 QuicBlockedFrame blocked
;
3197 blocked
.stream_id
= 3;
3198 EXPECT_CALL(visitor_
, OnBlockedFrames(_
));
3199 ProcessFramePacket(QuicFrame(&blocked
));
3202 TEST_P(QuicConnectionTest
, ZeroBytePacket
) {
3203 // Don't close the connection for zero byte packets.
3204 EXPECT_CALL(visitor_
, OnConnectionClosed(_
, _
)).Times(0);
3205 QuicEncryptedPacket
encrypted(nullptr, 0);
3206 connection_
.ProcessUdpPacket(IPEndPoint(), IPEndPoint(), encrypted
);
3209 TEST_P(QuicConnectionTest
, MissingPacketsBeforeLeastUnacked
) {
3210 // Set the sequence number of the ack packet to be least unacked (4).
3211 peer_creator_
.set_sequence_number(3);
3212 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
3213 QuicStopWaitingFrame frame
= InitStopWaitingFrame(4);
3214 ProcessStopWaitingPacket(&frame
);
3215 EXPECT_TRUE(outgoing_ack()->missing_packets
.empty());
3218 TEST_P(QuicConnectionTest
, ReceivedEntropyHashCalculation
) {
3219 EXPECT_CALL(visitor_
, OnStreamFrames(_
)).Times(AtLeast(1));
3220 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
3221 ProcessDataPacket(1, 1, kEntropyFlag
);
3222 ProcessDataPacket(4, 1, kEntropyFlag
);
3223 ProcessDataPacket(3, 1, !kEntropyFlag
);
3224 ProcessDataPacket(7, 1, kEntropyFlag
);
3225 EXPECT_EQ(146u, outgoing_ack()->entropy_hash
);
3228 TEST_P(QuicConnectionTest
, ReceivedEntropyHashCalculationHalfFEC
) {
3229 // FEC packets should not change the entropy hash calculation.
3230 EXPECT_CALL(visitor_
, OnStreamFrames(_
)).Times(AtLeast(1));
3231 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
3232 ProcessDataPacket(1, 1, kEntropyFlag
);
3233 ProcessFecPacket(4, 1, false, kEntropyFlag
, nullptr);
3234 ProcessDataPacket(3, 3, !kEntropyFlag
);
3235 ProcessFecPacket(7, 3, false, kEntropyFlag
, nullptr);
3236 EXPECT_EQ(146u, outgoing_ack()->entropy_hash
);
3239 TEST_P(QuicConnectionTest
, UpdateEntropyForReceivedPackets
) {
3240 EXPECT_CALL(visitor_
, OnStreamFrames(_
)).Times(AtLeast(1));
3241 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
3242 ProcessDataPacket(1, 1, kEntropyFlag
);
3243 ProcessDataPacket(5, 1, kEntropyFlag
);
3244 ProcessDataPacket(4, 1, !kEntropyFlag
);
3245 EXPECT_EQ(34u, outgoing_ack()->entropy_hash
);
3246 // Make 4th packet my least unacked, and update entropy for 2, 3 packets.
3247 peer_creator_
.set_sequence_number(5);
3248 QuicPacketEntropyHash six_packet_entropy_hash
= 0;
3249 QuicPacketEntropyHash kRandomEntropyHash
= 129u;
3250 QuicStopWaitingFrame frame
= InitStopWaitingFrame(4);
3251 frame
.entropy_hash
= kRandomEntropyHash
;
3252 if (ProcessStopWaitingPacket(&frame
)) {
3253 six_packet_entropy_hash
= 1 << 6;
3256 EXPECT_EQ((kRandomEntropyHash
+ (1 << 5) + six_packet_entropy_hash
),
3257 outgoing_ack()->entropy_hash
);
3260 TEST_P(QuicConnectionTest
, UpdateEntropyHashUptoCurrentPacket
) {
3261 EXPECT_CALL(visitor_
, OnStreamFrames(_
)).Times(AtLeast(1));
3262 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
3263 ProcessDataPacket(1, 1, kEntropyFlag
);
3264 ProcessDataPacket(5, 1, !kEntropyFlag
);
3265 ProcessDataPacket(22, 1, kEntropyFlag
);
3266 EXPECT_EQ(66u, outgoing_ack()->entropy_hash
);
3267 peer_creator_
.set_sequence_number(22);
3268 QuicPacketEntropyHash kRandomEntropyHash
= 85u;
3269 // Current packet is the least unacked packet.
3270 QuicPacketEntropyHash ack_entropy_hash
;
3271 QuicStopWaitingFrame frame
= InitStopWaitingFrame(23);
3272 frame
.entropy_hash
= kRandomEntropyHash
;
3273 ack_entropy_hash
= ProcessStopWaitingPacket(&frame
);
3274 EXPECT_EQ((kRandomEntropyHash
+ ack_entropy_hash
),
3275 outgoing_ack()->entropy_hash
);
3276 ProcessDataPacket(25, 1, kEntropyFlag
);
3277 EXPECT_EQ((kRandomEntropyHash
+ ack_entropy_hash
+ (1 << (25 % 8))),
3278 outgoing_ack()->entropy_hash
);
3281 TEST_P(QuicConnectionTest
, EntropyCalculationForTruncatedAck
) {
3282 EXPECT_CALL(visitor_
, OnStreamFrames(_
)).Times(AtLeast(1));
3283 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
3284 QuicPacketEntropyHash entropy
[51];
3286 for (int i
= 1; i
< 51; ++i
) {
3287 bool should_send
= i
% 10 != 1;
3288 bool entropy_flag
= (i
& (i
- 1)) != 0;
3290 entropy
[i
] = entropy
[i
- 1];
3294 entropy
[i
] = entropy
[i
- 1] ^ (1 << (i
% 8));
3296 entropy
[i
] = entropy
[i
- 1];
3298 ProcessDataPacket(i
, 1, entropy_flag
);
3300 for (int i
= 1; i
< 50; ++i
) {
3301 EXPECT_EQ(entropy
[i
], QuicConnectionPeer::ReceivedEntropyHash(
3306 TEST_P(QuicConnectionTest
, ServerSendsVersionNegotiationPacket
) {
3307 connection_
.SetSupportedVersions(QuicSupportedVersions());
3308 framer_
.set_version_for_tests(QUIC_VERSION_UNSUPPORTED
);
3310 QuicPacketHeader header
;
3311 header
.public_header
.connection_id
= connection_id_
;
3312 header
.public_header
.reset_flag
= false;
3313 header
.public_header
.version_flag
= true;
3314 header
.entropy_flag
= false;
3315 header
.fec_flag
= false;
3316 header
.packet_sequence_number
= 12;
3317 header
.fec_group
= 0;
3320 QuicFrame
frame(&frame1_
);
3321 frames
.push_back(frame
);
3322 scoped_ptr
<QuicPacket
> packet(
3323 BuildUnsizedDataPacket(&framer_
, header
, frames
).packet
);
3324 scoped_ptr
<QuicEncryptedPacket
> encrypted(
3325 framer_
.EncryptPacket(ENCRYPTION_NONE
, 12, *packet
));
3327 framer_
.set_version(version());
3328 connection_
.set_is_server(true);
3329 connection_
.ProcessUdpPacket(IPEndPoint(), IPEndPoint(), *encrypted
);
3330 EXPECT_TRUE(writer_
->version_negotiation_packet() != nullptr);
3332 size_t num_versions
= arraysize(kSupportedQuicVersions
);
3333 ASSERT_EQ(num_versions
,
3334 writer_
->version_negotiation_packet()->versions
.size());
3336 // We expect all versions in kSupportedQuicVersions to be
3337 // included in the packet.
3338 for (size_t i
= 0; i
< num_versions
; ++i
) {
3339 EXPECT_EQ(kSupportedQuicVersions
[i
],
3340 writer_
->version_negotiation_packet()->versions
[i
]);
3344 TEST_P(QuicConnectionTest
, ServerSendsVersionNegotiationPacketSocketBlocked
) {
3345 connection_
.SetSupportedVersions(QuicSupportedVersions());
3346 framer_
.set_version_for_tests(QUIC_VERSION_UNSUPPORTED
);
3348 QuicPacketHeader header
;
3349 header
.public_header
.connection_id
= connection_id_
;
3350 header
.public_header
.reset_flag
= false;
3351 header
.public_header
.version_flag
= true;
3352 header
.entropy_flag
= false;
3353 header
.fec_flag
= false;
3354 header
.packet_sequence_number
= 12;
3355 header
.fec_group
= 0;
3358 QuicFrame
frame(&frame1_
);
3359 frames
.push_back(frame
);
3360 scoped_ptr
<QuicPacket
> packet(
3361 BuildUnsizedDataPacket(&framer_
, header
, frames
).packet
);
3362 scoped_ptr
<QuicEncryptedPacket
> encrypted(
3363 framer_
.EncryptPacket(ENCRYPTION_NONE
, 12, *packet
));
3365 framer_
.set_version(version());
3366 connection_
.set_is_server(true);
3368 connection_
.ProcessUdpPacket(IPEndPoint(), IPEndPoint(), *encrypted
);
3369 EXPECT_EQ(0u, writer_
->last_packet_size());
3370 EXPECT_TRUE(connection_
.HasQueuedData());
3372 writer_
->SetWritable();
3373 connection_
.OnCanWrite();
3374 EXPECT_TRUE(writer_
->version_negotiation_packet() != nullptr);
3376 size_t num_versions
= arraysize(kSupportedQuicVersions
);
3377 ASSERT_EQ(num_versions
,
3378 writer_
->version_negotiation_packet()->versions
.size());
3380 // We expect all versions in kSupportedQuicVersions to be
3381 // included in the packet.
3382 for (size_t i
= 0; i
< num_versions
; ++i
) {
3383 EXPECT_EQ(kSupportedQuicVersions
[i
],
3384 writer_
->version_negotiation_packet()->versions
[i
]);
3388 TEST_P(QuicConnectionTest
,
3389 ServerSendsVersionNegotiationPacketSocketBlockedDataBuffered
) {
3390 connection_
.SetSupportedVersions(QuicSupportedVersions());
3391 framer_
.set_version_for_tests(QUIC_VERSION_UNSUPPORTED
);
3393 QuicPacketHeader header
;
3394 header
.public_header
.connection_id
= connection_id_
;
3395 header
.public_header
.reset_flag
= false;
3396 header
.public_header
.version_flag
= true;
3397 header
.entropy_flag
= false;
3398 header
.fec_flag
= false;
3399 header
.packet_sequence_number
= 12;
3400 header
.fec_group
= 0;
3403 QuicFrame
frame(&frame1_
);
3404 frames
.push_back(frame
);
3405 scoped_ptr
<QuicPacket
> packet(
3406 BuildUnsizedDataPacket(&framer_
, header
, frames
).packet
);
3407 scoped_ptr
<QuicEncryptedPacket
> encrypted(
3408 framer_
.EncryptPacket(ENCRYPTION_NONE
, 12, *packet
));
3410 framer_
.set_version(version());
3411 connection_
.set_is_server(true);
3413 writer_
->set_is_write_blocked_data_buffered(true);
3414 connection_
.ProcessUdpPacket(IPEndPoint(), IPEndPoint(), *encrypted
);
3415 EXPECT_EQ(0u, writer_
->last_packet_size());
3416 EXPECT_FALSE(connection_
.HasQueuedData());
3419 TEST_P(QuicConnectionTest
, ClientHandlesVersionNegotiation
) {
3420 // Start out with some unsupported version.
3421 QuicConnectionPeer::GetFramer(&connection_
)->set_version_for_tests(
3422 QUIC_VERSION_UNSUPPORTED
);
3424 QuicPacketHeader header
;
3425 header
.public_header
.connection_id
= connection_id_
;
3426 header
.public_header
.reset_flag
= false;
3427 header
.public_header
.version_flag
= true;
3428 header
.entropy_flag
= false;
3429 header
.fec_flag
= false;
3430 header
.packet_sequence_number
= 12;
3431 header
.fec_group
= 0;
3433 QuicVersionVector supported_versions
;
3434 for (size_t i
= 0; i
< arraysize(kSupportedQuicVersions
); ++i
) {
3435 supported_versions
.push_back(kSupportedQuicVersions
[i
]);
3438 // Send a version negotiation packet.
3439 scoped_ptr
<QuicEncryptedPacket
> encrypted(
3440 framer_
.BuildVersionNegotiationPacket(
3441 header
.public_header
, supported_versions
));
3442 connection_
.ProcessUdpPacket(IPEndPoint(), IPEndPoint(), *encrypted
);
3444 // Now force another packet. The connection should transition into
3445 // NEGOTIATED_VERSION state and tell the packet creator to StopSendingVersion.
3446 header
.public_header
.version_flag
= false;
3448 QuicFrame
frame(&frame1_
);
3449 frames
.push_back(frame
);
3450 scoped_ptr
<QuicPacket
> packet(
3451 BuildUnsizedDataPacket(&framer_
, header
, frames
).packet
);
3452 encrypted
.reset(framer_
.EncryptPacket(ENCRYPTION_NONE
, 12, *packet
));
3453 EXPECT_CALL(visitor_
, OnStreamFrames(_
)).Times(1);
3454 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
3455 connection_
.ProcessUdpPacket(IPEndPoint(), IPEndPoint(), *encrypted
);
3457 ASSERT_FALSE(QuicPacketCreatorPeer::SendVersionInPacket(
3458 QuicConnectionPeer::GetPacketCreator(&connection_
)));
3461 TEST_P(QuicConnectionTest
, BadVersionNegotiation
) {
3462 QuicPacketHeader header
;
3463 header
.public_header
.connection_id
= connection_id_
;
3464 header
.public_header
.reset_flag
= false;
3465 header
.public_header
.version_flag
= true;
3466 header
.entropy_flag
= false;
3467 header
.fec_flag
= false;
3468 header
.packet_sequence_number
= 12;
3469 header
.fec_group
= 0;
3471 QuicVersionVector supported_versions
;
3472 for (size_t i
= 0; i
< arraysize(kSupportedQuicVersions
); ++i
) {
3473 supported_versions
.push_back(kSupportedQuicVersions
[i
]);
3476 // Send a version negotiation packet with the version the client started with.
3477 // It should be rejected.
3478 EXPECT_CALL(visitor_
,
3479 OnConnectionClosed(QUIC_INVALID_VERSION_NEGOTIATION_PACKET
,
3481 scoped_ptr
<QuicEncryptedPacket
> encrypted(
3482 framer_
.BuildVersionNegotiationPacket(
3483 header
.public_header
, supported_versions
));
3484 connection_
.ProcessUdpPacket(IPEndPoint(), IPEndPoint(), *encrypted
);
3487 TEST_P(QuicConnectionTest
, CheckSendStats
) {
3488 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
));
3489 connection_
.SendStreamDataWithString(3, "first", 0, !kFin
, nullptr);
3490 size_t first_packet_size
= writer_
->last_packet_size();
3492 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
));
3493 connection_
.SendStreamDataWithString(5, "second", 0, !kFin
, nullptr);
3494 size_t second_packet_size
= writer_
->last_packet_size();
3496 // 2 retransmissions due to rto, 1 due to explicit nack.
3497 EXPECT_CALL(*send_algorithm_
, OnRetransmissionTimeout(true));
3498 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
)).Times(3);
3500 // Retransmit due to RTO.
3501 clock_
.AdvanceTime(QuicTime::Delta::FromSeconds(10));
3502 connection_
.GetRetransmissionAlarm()->Fire();
3504 // Retransmit due to explicit nacks.
3505 QuicAckFrame nack_three
= InitAckFrame(4);
3506 NackPacket(3, &nack_three
);
3507 NackPacket(1, &nack_three
);
3508 SequenceNumberSet lost_packets
;
3509 lost_packets
.insert(1);
3510 lost_packets
.insert(3);
3511 EXPECT_CALL(*loss_algorithm_
, DetectLostPackets(_
, _
, _
, _
))
3512 .WillOnce(Return(lost_packets
));
3513 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
3514 EXPECT_CALL(visitor_
, OnCanWrite()).Times(2);
3515 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
3516 EXPECT_CALL(*send_algorithm_
, RevertRetransmissionTimeout());
3517 ProcessAckPacket(&nack_three
);
3519 EXPECT_CALL(*send_algorithm_
, BandwidthEstimate()).WillOnce(
3520 Return(QuicBandwidth::Zero()));
3522 const uint32 kSlowStartThreshold
= 23u;
3523 EXPECT_CALL(*send_algorithm_
, GetSlowStartThreshold()).WillOnce(
3524 Return(kSlowStartThreshold
));
3526 const QuicConnectionStats
& stats
= connection_
.GetStats();
3527 EXPECT_EQ(3 * first_packet_size
+ 2 * second_packet_size
- kQuicVersionSize
,
3529 EXPECT_EQ(5u, stats
.packets_sent
);
3530 EXPECT_EQ(2 * first_packet_size
+ second_packet_size
- kQuicVersionSize
,
3531 stats
.bytes_retransmitted
);
3532 EXPECT_EQ(3u, stats
.packets_retransmitted
);
3533 EXPECT_EQ(1u, stats
.rto_count
);
3534 EXPECT_EQ(kMaxPacketSize
, stats
.congestion_window
);
3535 EXPECT_EQ(kSlowStartThreshold
, stats
.slow_start_threshold
);
3536 EXPECT_EQ(kDefaultMaxPacketSize
, stats
.max_packet_size
);
3539 TEST_P(QuicConnectionTest
, CheckReceiveStats
) {
3540 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
3542 size_t received_bytes
= 0;
3543 received_bytes
+= ProcessFecProtectedPacket(1, false, !kEntropyFlag
);
3544 received_bytes
+= ProcessFecProtectedPacket(3, false, !kEntropyFlag
);
3545 // Should be counted against dropped packets.
3546 received_bytes
+= ProcessDataPacket(3, 1, !kEntropyFlag
);
3547 received_bytes
+= ProcessFecPacket(4, 1, true, !kEntropyFlag
, nullptr);
3549 EXPECT_CALL(*send_algorithm_
, BandwidthEstimate()).WillOnce(
3550 Return(QuicBandwidth::Zero()));
3551 const uint32 kSlowStartThreshold
= 23u;
3552 EXPECT_CALL(*send_algorithm_
, GetSlowStartThreshold()).WillOnce(
3553 Return(kSlowStartThreshold
));
3555 const QuicConnectionStats
& stats
= connection_
.GetStats();
3556 EXPECT_EQ(received_bytes
, stats
.bytes_received
);
3557 EXPECT_EQ(4u, stats
.packets_received
);
3559 EXPECT_EQ(1u, stats
.packets_revived
);
3560 EXPECT_EQ(1u, stats
.packets_dropped
);
3562 EXPECT_EQ(kSlowStartThreshold
, stats
.slow_start_threshold
);
3565 TEST_P(QuicConnectionTest
, TestFecGroupLimits
) {
3566 // Create and return a group for 1.
3567 ASSERT_TRUE(QuicConnectionPeer::GetFecGroup(&connection_
, 1) != nullptr);
3569 // Create and return a group for 2.
3570 ASSERT_TRUE(QuicConnectionPeer::GetFecGroup(&connection_
, 2) != nullptr);
3572 // Create and return a group for 4. This should remove 1 but not 2.
3573 ASSERT_TRUE(QuicConnectionPeer::GetFecGroup(&connection_
, 4) != nullptr);
3574 ASSERT_TRUE(QuicConnectionPeer::GetFecGroup(&connection_
, 1) == nullptr);
3575 ASSERT_TRUE(QuicConnectionPeer::GetFecGroup(&connection_
, 2) != nullptr);
3577 // Create and return a group for 3. This will kill off 2.
3578 ASSERT_TRUE(QuicConnectionPeer::GetFecGroup(&connection_
, 3) != nullptr);
3579 ASSERT_TRUE(QuicConnectionPeer::GetFecGroup(&connection_
, 2) == nullptr);
3581 // Verify that adding 5 kills off 3, despite 4 being created before 3.
3582 ASSERT_TRUE(QuicConnectionPeer::GetFecGroup(&connection_
, 5) != nullptr);
3583 ASSERT_TRUE(QuicConnectionPeer::GetFecGroup(&connection_
, 4) != nullptr);
3584 ASSERT_TRUE(QuicConnectionPeer::GetFecGroup(&connection_
, 3) == nullptr);
3587 TEST_P(QuicConnectionTest
, ProcessFramesIfPacketClosedConnection
) {
3588 // Construct a packet with stream frame and connection close frame.
3589 header_
.public_header
.connection_id
= connection_id_
;
3590 header_
.packet_sequence_number
= 1;
3591 header_
.public_header
.reset_flag
= false;
3592 header_
.public_header
.version_flag
= false;
3593 header_
.entropy_flag
= false;
3594 header_
.fec_flag
= false;
3595 header_
.fec_group
= 0;
3597 QuicConnectionCloseFrame qccf
;
3598 qccf
.error_code
= QUIC_PEER_GOING_AWAY
;
3599 QuicFrame
close_frame(&qccf
);
3600 QuicFrame
stream_frame(&frame1_
);
3603 frames
.push_back(stream_frame
);
3604 frames
.push_back(close_frame
);
3605 scoped_ptr
<QuicPacket
> packet(
3606 BuildUnsizedDataPacket(&framer_
, header_
, frames
).packet
);
3607 EXPECT_TRUE(nullptr != packet
.get());
3608 scoped_ptr
<QuicEncryptedPacket
> encrypted(framer_
.EncryptPacket(
3609 ENCRYPTION_NONE
, 1, *packet
));
3611 EXPECT_CALL(visitor_
, OnConnectionClosed(QUIC_PEER_GOING_AWAY
, true));
3612 EXPECT_CALL(visitor_
, OnStreamFrames(_
)).Times(1);
3613 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
3615 connection_
.ProcessUdpPacket(IPEndPoint(), IPEndPoint(), *encrypted
);
3618 TEST_P(QuicConnectionTest
, SelectMutualVersion
) {
3619 connection_
.SetSupportedVersions(QuicSupportedVersions());
3620 // Set the connection to speak the lowest quic version.
3621 connection_
.set_version(QuicVersionMin());
3622 EXPECT_EQ(QuicVersionMin(), connection_
.version());
3624 // Pass in available versions which includes a higher mutually supported
3625 // version. The higher mutually supported version should be selected.
3626 QuicVersionVector supported_versions
;
3627 for (size_t i
= 0; i
< arraysize(kSupportedQuicVersions
); ++i
) {
3628 supported_versions
.push_back(kSupportedQuicVersions
[i
]);
3630 EXPECT_TRUE(connection_
.SelectMutualVersion(supported_versions
));
3631 EXPECT_EQ(QuicVersionMax(), connection_
.version());
3633 // Expect that the lowest version is selected.
3634 // Ensure the lowest supported version is less than the max, unless they're
3636 EXPECT_LE(QuicVersionMin(), QuicVersionMax());
3637 QuicVersionVector lowest_version_vector
;
3638 lowest_version_vector
.push_back(QuicVersionMin());
3639 EXPECT_TRUE(connection_
.SelectMutualVersion(lowest_version_vector
));
3640 EXPECT_EQ(QuicVersionMin(), connection_
.version());
3642 // Shouldn't be able to find a mutually supported version.
3643 QuicVersionVector unsupported_version
;
3644 unsupported_version
.push_back(QUIC_VERSION_UNSUPPORTED
);
3645 EXPECT_FALSE(connection_
.SelectMutualVersion(unsupported_version
));
3648 TEST_P(QuicConnectionTest
, ConnectionCloseWhenWritable
) {
3649 EXPECT_FALSE(writer_
->IsWriteBlocked());
3652 connection_
.SendStreamDataWithString(1, "foo", 0, !kFin
, nullptr);
3653 EXPECT_EQ(0u, connection_
.NumQueuedPackets());
3654 EXPECT_EQ(1u, writer_
->packets_write_attempts());
3656 TriggerConnectionClose();
3657 EXPECT_EQ(2u, writer_
->packets_write_attempts());
3660 TEST_P(QuicConnectionTest
, ConnectionCloseGettingWriteBlocked
) {
3662 TriggerConnectionClose();
3663 EXPECT_EQ(1u, writer_
->packets_write_attempts());
3664 EXPECT_TRUE(writer_
->IsWriteBlocked());
3667 TEST_P(QuicConnectionTest
, ConnectionCloseWhenWriteBlocked
) {
3669 connection_
.SendStreamDataWithString(1, "foo", 0, !kFin
, nullptr);
3670 EXPECT_EQ(1u, connection_
.NumQueuedPackets());
3671 EXPECT_EQ(1u, writer_
->packets_write_attempts());
3672 EXPECT_TRUE(writer_
->IsWriteBlocked());
3673 TriggerConnectionClose();
3674 EXPECT_EQ(1u, writer_
->packets_write_attempts());
3677 TEST_P(QuicConnectionTest
, AckNotifierTriggerCallback
) {
3678 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
3680 // Create a delegate which we expect to be called.
3681 scoped_refptr
<MockAckNotifierDelegate
> delegate(new MockAckNotifierDelegate
);
3682 EXPECT_CALL(*delegate
.get(), OnAckNotification(_
, _
, _
, _
, _
)).Times(1);
3684 // Send some data, which will register the delegate to be notified.
3685 connection_
.SendStreamDataWithString(1, "foo", 0, !kFin
, delegate
.get());
3687 // Process an ACK from the server which should trigger the callback.
3688 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
3689 QuicAckFrame frame
= InitAckFrame(1);
3690 ProcessAckPacket(&frame
);
3693 TEST_P(QuicConnectionTest
, AckNotifierFailToTriggerCallback
) {
3694 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
3696 // Create a delegate which we don't expect to be called.
3697 scoped_refptr
<MockAckNotifierDelegate
> delegate(new MockAckNotifierDelegate
);
3698 EXPECT_CALL(*delegate
.get(), OnAckNotification(_
, _
, _
, _
, _
)).Times(0);
3700 // Send some data, which will register the delegate to be notified. This will
3701 // not be ACKed and so the delegate should never be called.
3702 connection_
.SendStreamDataWithString(1, "foo", 0, !kFin
, delegate
.get());
3704 // Send some other data which we will ACK.
3705 connection_
.SendStreamDataWithString(1, "foo", 0, !kFin
, nullptr);
3706 connection_
.SendStreamDataWithString(1, "bar", 0, !kFin
, nullptr);
3708 // Now we receive ACK for packets 2 and 3, but importantly missing packet 1
3709 // which we registered to be notified about.
3710 QuicAckFrame frame
= InitAckFrame(3);
3711 NackPacket(1, &frame
);
3712 SequenceNumberSet lost_packets
;
3713 lost_packets
.insert(1);
3714 EXPECT_CALL(*loss_algorithm_
, DetectLostPackets(_
, _
, _
, _
))
3715 .WillOnce(Return(lost_packets
));
3716 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
3717 ProcessAckPacket(&frame
);
3720 TEST_P(QuicConnectionTest
, AckNotifierCallbackAfterRetransmission
) {
3721 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
3723 // Create a delegate which we expect to be called.
3724 scoped_refptr
<MockAckNotifierDelegate
> delegate(new MockAckNotifierDelegate
);
3725 EXPECT_CALL(*delegate
.get(), OnAckNotification(_
, _
, _
, _
, _
)).Times(1);
3727 // Send four packets, and register to be notified on ACK of packet 2.
3728 connection_
.SendStreamDataWithString(3, "foo", 0, !kFin
, nullptr);
3729 connection_
.SendStreamDataWithString(3, "bar", 0, !kFin
, delegate
.get());
3730 connection_
.SendStreamDataWithString(3, "baz", 0, !kFin
, nullptr);
3731 connection_
.SendStreamDataWithString(3, "qux", 0, !kFin
, nullptr);
3733 // Now we receive ACK for packets 1, 3, and 4 and lose 2.
3734 QuicAckFrame frame
= InitAckFrame(4);
3735 NackPacket(2, &frame
);
3736 SequenceNumberSet lost_packets
;
3737 lost_packets
.insert(2);
3738 EXPECT_CALL(*loss_algorithm_
, DetectLostPackets(_
, _
, _
, _
))
3739 .WillOnce(Return(lost_packets
));
3740 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
3741 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
));
3742 ProcessAckPacket(&frame
);
3744 // Now we get an ACK for packet 5 (retransmitted packet 2), which should
3745 // trigger the callback.
3746 EXPECT_CALL(*loss_algorithm_
, DetectLostPackets(_
, _
, _
, _
))
3747 .WillRepeatedly(Return(SequenceNumberSet()));
3748 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
3749 QuicAckFrame second_ack_frame
= InitAckFrame(5);
3750 ProcessAckPacket(&second_ack_frame
);
3753 // AckNotifierCallback is triggered by the ack of a packet that timed
3754 // out and was retransmitted, even though the retransmission has a
3755 // different sequence number.
3756 TEST_P(QuicConnectionTest
, AckNotifierCallbackForAckAfterRTO
) {
3759 // Create a delegate which we expect to be called.
3760 scoped_refptr
<MockAckNotifierDelegate
> delegate(
3761 new StrictMock
<MockAckNotifierDelegate
>);
3763 QuicTime default_retransmission_time
= clock_
.ApproximateNow().Add(
3764 DefaultRetransmissionTime());
3765 connection_
.SendStreamDataWithString(3, "foo", 0, !kFin
, delegate
.get());
3766 EXPECT_EQ(1u, stop_waiting()->least_unacked
);
3768 EXPECT_EQ(1u, writer_
->header().packet_sequence_number
);
3769 EXPECT_EQ(default_retransmission_time
,
3770 connection_
.GetRetransmissionAlarm()->deadline());
3771 // Simulate the retransmission alarm firing.
3772 clock_
.AdvanceTime(DefaultRetransmissionTime());
3773 EXPECT_CALL(*send_algorithm_
, OnRetransmissionTimeout(true));
3774 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, 2u, _
, _
));
3775 connection_
.GetRetransmissionAlarm()->Fire();
3776 EXPECT_EQ(2u, writer_
->header().packet_sequence_number
);
3777 // We do not raise the high water mark yet.
3778 EXPECT_EQ(1u, stop_waiting()->least_unacked
);
3780 // Ack the original packet, which will revert the RTO.
3781 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
3782 EXPECT_CALL(*delegate
.get(), OnAckNotification(1, _
, 1, _
, _
));
3783 EXPECT_CALL(*send_algorithm_
, RevertRetransmissionTimeout());
3784 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
3785 QuicAckFrame ack_frame
= InitAckFrame(1);
3786 ProcessAckPacket(&ack_frame
);
3788 // Delegate is not notified again when the retransmit is acked.
3789 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
3790 QuicAckFrame second_ack_frame
= InitAckFrame(2);
3791 ProcessAckPacket(&second_ack_frame
);
3794 // AckNotifierCallback is triggered by the ack of a packet that was
3795 // previously nacked, even though the retransmission has a different
3797 TEST_P(QuicConnectionTest
, AckNotifierCallbackForAckOfNackedPacket
) {
3800 // Create a delegate which we expect to be called.
3801 scoped_refptr
<MockAckNotifierDelegate
> delegate(
3802 new StrictMock
<MockAckNotifierDelegate
>);
3804 // Send four packets, and register to be notified on ACK of packet 2.
3805 connection_
.SendStreamDataWithString(3, "foo", 0, !kFin
, nullptr);
3806 connection_
.SendStreamDataWithString(3, "bar", 0, !kFin
, delegate
.get());
3807 connection_
.SendStreamDataWithString(3, "baz", 0, !kFin
, nullptr);
3808 connection_
.SendStreamDataWithString(3, "qux", 0, !kFin
, nullptr);
3810 // Now we receive ACK for packets 1, 3, and 4 and lose 2.
3811 QuicAckFrame frame
= InitAckFrame(4);
3812 NackPacket(2, &frame
);
3813 SequenceNumberSet lost_packets
;
3814 lost_packets
.insert(2);
3815 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
3816 EXPECT_CALL(*loss_algorithm_
, DetectLostPackets(_
, _
, _
, _
))
3817 .WillOnce(Return(lost_packets
));
3818 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
3819 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
));
3820 ProcessAckPacket(&frame
);
3822 // Now we get an ACK for packet 2, which was previously nacked.
3823 SequenceNumberSet no_lost_packets
;
3824 EXPECT_CALL(*delegate
.get(), OnAckNotification(1, _
, 1, _
, _
));
3825 EXPECT_CALL(*loss_algorithm_
, DetectLostPackets(_
, _
, _
, _
))
3826 .WillOnce(Return(no_lost_packets
));
3827 QuicAckFrame second_ack_frame
= InitAckFrame(4);
3828 ProcessAckPacket(&second_ack_frame
);
3830 // Verify that the delegate is not notified again when the
3831 // retransmit is acked.
3832 EXPECT_CALL(*loss_algorithm_
, DetectLostPackets(_
, _
, _
, _
))
3833 .WillOnce(Return(no_lost_packets
));
3834 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
3835 QuicAckFrame third_ack_frame
= InitAckFrame(5);
3836 ProcessAckPacket(&third_ack_frame
);
3839 TEST_P(QuicConnectionTest
, AckNotifierFECTriggerCallback
) {
3840 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
3842 // Create a delegate which we expect to be called.
3843 scoped_refptr
<MockAckNotifierDelegate
> delegate(
3844 new MockAckNotifierDelegate
);
3845 EXPECT_CALL(*delegate
.get(), OnAckNotification(_
, _
, _
, _
, _
)).Times(1);
3847 // Send some data, which will register the delegate to be notified.
3848 connection_
.SendStreamDataWithString(1, "foo", 0, !kFin
, delegate
.get());
3849 connection_
.SendStreamDataWithString(2, "bar", 0, !kFin
, nullptr);
3851 // Process an ACK from the server with a revived packet, which should trigger
3853 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
3854 QuicAckFrame frame
= InitAckFrame(2);
3855 NackPacket(1, &frame
);
3856 frame
.revived_packets
.insert(1);
3857 ProcessAckPacket(&frame
);
3858 // If the ack is processed again, the notifier should not be called again.
3859 ProcessAckPacket(&frame
);
3862 TEST_P(QuicConnectionTest
, AckNotifierCallbackAfterFECRecovery
) {
3863 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
3864 EXPECT_CALL(visitor_
, OnCanWrite());
3866 // Create a delegate which we expect to be called.
3867 scoped_refptr
<MockAckNotifierDelegate
> delegate(new MockAckNotifierDelegate
);
3868 EXPECT_CALL(*delegate
.get(), OnAckNotification(_
, _
, _
, _
, _
)).Times(1);
3870 // Expect ACKs for 1 packet.
3871 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
3873 // Send one packet, and register to be notified on ACK.
3874 connection_
.SendStreamDataWithString(1, "foo", 0, !kFin
, delegate
.get());
3876 // Ack packet gets dropped, but we receive an FEC packet that covers it.
3877 // Should recover the Ack packet and trigger the notification callback.
3880 QuicAckFrame ack_frame
= InitAckFrame(1);
3881 frames
.push_back(QuicFrame(&ack_frame
));
3883 // Dummy stream frame to satisfy expectations set elsewhere.
3884 frames
.push_back(QuicFrame(&frame1_
));
3886 QuicPacketHeader ack_header
;
3887 ack_header
.public_header
.connection_id
= connection_id_
;
3888 ack_header
.public_header
.reset_flag
= false;
3889 ack_header
.public_header
.version_flag
= false;
3890 ack_header
.entropy_flag
= !kEntropyFlag
;
3891 ack_header
.fec_flag
= true;
3892 ack_header
.packet_sequence_number
= 1;
3893 ack_header
.is_in_fec_group
= IN_FEC_GROUP
;
3894 ack_header
.fec_group
= 1;
3896 QuicPacket
* packet
=
3897 BuildUnsizedDataPacket(&framer_
, ack_header
, frames
).packet
;
3899 // Take the packet which contains the ACK frame, and construct and deliver an
3900 // FEC packet which allows the ACK packet to be recovered.
3901 ProcessFecPacket(2, 1, true, !kEntropyFlag
, packet
);
3904 TEST_P(QuicConnectionTest
, NetworkChangeVisitorCallbacksChangeFecState
) {
3905 QuicPacketCreator
* creator
=
3906 QuicConnectionPeer::GetPacketCreator(&connection_
);
3907 size_t max_packets_per_fec_group
= creator
->max_packets_per_fec_group();
3909 QuicSentPacketManager::NetworkChangeVisitor
* visitor
=
3910 QuicSentPacketManagerPeer::GetNetworkChangeVisitor(
3911 QuicConnectionPeer::GetSentPacketManager(&connection_
));
3912 EXPECT_TRUE(visitor
);
3914 // Increase FEC group size by increasing congestion window to a large number.
3915 visitor
->OnCongestionWindowChange(1000 * kDefaultTCPMSS
);
3916 EXPECT_LT(max_packets_per_fec_group
, creator
->max_packets_per_fec_group());
3919 class MockQuicConnectionDebugVisitor
3920 : public QuicConnectionDebugVisitor
{
3922 MOCK_METHOD1(OnFrameAddedToPacket
,
3923 void(const QuicFrame
&));
3925 MOCK_METHOD6(OnPacketSent
,
3926 void(const SerializedPacket
&,
3927 QuicPacketSequenceNumber
,
3930 const QuicEncryptedPacket
&,
3933 MOCK_METHOD3(OnPacketReceived
,
3934 void(const IPEndPoint
&,
3936 const QuicEncryptedPacket
&));
3938 MOCK_METHOD1(OnProtocolVersionMismatch
,
3941 MOCK_METHOD1(OnPacketHeader
,
3942 void(const QuicPacketHeader
& header
));
3944 MOCK_METHOD1(OnStreamFrame
,
3945 void(const QuicStreamFrame
&));
3947 MOCK_METHOD1(OnAckFrame
,
3948 void(const QuicAckFrame
& frame
));
3950 MOCK_METHOD1(OnCongestionFeedbackFrame
,
3951 void(const QuicCongestionFeedbackFrame
&));
3953 MOCK_METHOD1(OnStopWaitingFrame
,
3954 void(const QuicStopWaitingFrame
&));
3956 MOCK_METHOD1(OnRstStreamFrame
,
3957 void(const QuicRstStreamFrame
&));
3959 MOCK_METHOD1(OnConnectionCloseFrame
,
3960 void(const QuicConnectionCloseFrame
&));
3962 MOCK_METHOD1(OnPublicResetPacket
,
3963 void(const QuicPublicResetPacket
&));
3965 MOCK_METHOD1(OnVersionNegotiationPacket
,
3966 void(const QuicVersionNegotiationPacket
&));
3968 MOCK_METHOD2(OnRevivedPacket
,
3969 void(const QuicPacketHeader
&, StringPiece payload
));
3972 TEST_P(QuicConnectionTest
, OnPacketHeaderDebugVisitor
) {
3973 QuicPacketHeader header
;
3975 MockQuicConnectionDebugVisitor
* debug_visitor
=
3976 new MockQuicConnectionDebugVisitor();
3977 connection_
.set_debug_visitor(debug_visitor
);
3978 EXPECT_CALL(*debug_visitor
, OnPacketHeader(Ref(header
))).Times(1);
3979 connection_
.OnPacketHeader(header
);
3982 TEST_P(QuicConnectionTest
, Pacing
) {
3983 TestConnection
server(connection_id_
, IPEndPoint(), helper_
.get(),
3984 factory_
, /* is_server= */ true, version());
3985 TestConnection
client(connection_id_
, IPEndPoint(), helper_
.get(),
3986 factory_
, /* is_server= */ false, version());
3987 EXPECT_FALSE(client
.sent_packet_manager().using_pacing());
3988 EXPECT_FALSE(server
.sent_packet_manager().using_pacing());
3991 TEST_P(QuicConnectionTest
, ControlFramesInstigateAcks
) {
3992 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
3994 // Send a WINDOW_UPDATE frame.
3995 QuicWindowUpdateFrame window_update
;
3996 window_update
.stream_id
= 3;
3997 window_update
.byte_offset
= 1234;
3998 EXPECT_CALL(visitor_
, OnWindowUpdateFrames(_
));
3999 ProcessFramePacket(QuicFrame(&window_update
));
4001 // Ensure that this has caused the ACK alarm to be set.
4002 QuicAlarm
* ack_alarm
= QuicConnectionPeer::GetAckAlarm(&connection_
);
4003 EXPECT_TRUE(ack_alarm
->IsSet());
4005 // Cancel alarm, and try again with BLOCKED frame.
4006 ack_alarm
->Cancel();
4007 QuicBlockedFrame blocked
;
4008 blocked
.stream_id
= 3;
4009 EXPECT_CALL(visitor_
, OnBlockedFrames(_
));
4010 ProcessFramePacket(QuicFrame(&blocked
));
4011 EXPECT_TRUE(ack_alarm
->IsSet());