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
;
42 using testing::Return
;
43 using testing::SaveArg
;
44 using testing::StrictMock
;
51 const char data1
[] = "foo";
52 const char data2
[] = "bar";
54 const bool kFin
= true;
55 const bool kEntropyFlag
= true;
57 const QuicPacketEntropyHash kTestEntropyHash
= 76;
59 const int kDefaultRetransmissionTimeMs
= 500;
60 const int kMinRetransmissionTimeMs
= 200;
62 class TestReceiveAlgorithm
: public ReceiveAlgorithmInterface
{
64 explicit TestReceiveAlgorithm(QuicCongestionFeedbackFrame
* feedback
)
65 : feedback_(feedback
) {
68 bool GenerateCongestionFeedback(
69 QuicCongestionFeedbackFrame
* congestion_feedback
) {
70 if (feedback_
== NULL
) {
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 virtual ~TaggingEncrypter() {}
95 // QuicEncrypter interface.
96 virtual bool SetKey(StringPiece key
) OVERRIDE
{ return true; }
97 virtual bool SetNoncePrefix(StringPiece nonce_prefix
) OVERRIDE
{
101 virtual bool Encrypt(StringPiece nonce
,
102 StringPiece associated_data
,
103 StringPiece plaintext
,
104 unsigned char* output
) OVERRIDE
{
105 memcpy(output
, plaintext
.data(), plaintext
.size());
106 output
+= plaintext
.size();
107 memset(output
, tag_
, kTagSize
);
111 virtual QuicData
* EncryptPacket(QuicPacketSequenceNumber sequence_number
,
112 StringPiece associated_data
,
113 StringPiece plaintext
) OVERRIDE
{
114 const size_t len
= plaintext
.size() + kTagSize
;
115 uint8
* buffer
= new uint8
[len
];
116 Encrypt(StringPiece(), associated_data
, plaintext
, buffer
);
117 return new QuicData(reinterpret_cast<char*>(buffer
), len
, true);
120 virtual size_t GetKeySize() const OVERRIDE
{ return 0; }
121 virtual size_t GetNoncePrefixSize() const OVERRIDE
{ return 0; }
123 virtual size_t GetMaxPlaintextSize(size_t ciphertext_size
) const OVERRIDE
{
124 return ciphertext_size
- kTagSize
;
127 virtual size_t GetCiphertextSize(size_t plaintext_size
) const OVERRIDE
{
128 return plaintext_size
+ kTagSize
;
131 virtual StringPiece
GetKey() const OVERRIDE
{
132 return StringPiece();
135 virtual StringPiece
GetNoncePrefix() const OVERRIDE
{
136 return StringPiece();
146 DISALLOW_COPY_AND_ASSIGN(TaggingEncrypter
);
149 // TaggingDecrypter ensures that the final kTagSize bytes of the message all
150 // have the same value and then removes them.
151 class TaggingDecrypter
: public QuicDecrypter
{
153 virtual ~TaggingDecrypter() {}
155 // QuicDecrypter interface
156 virtual bool SetKey(StringPiece key
) OVERRIDE
{ return true; }
157 virtual bool SetNoncePrefix(StringPiece nonce_prefix
) OVERRIDE
{
161 virtual bool Decrypt(StringPiece nonce
,
162 StringPiece associated_data
,
163 StringPiece ciphertext
,
164 unsigned char* output
,
165 size_t* output_length
) OVERRIDE
{
166 if (ciphertext
.size() < kTagSize
) {
169 if (!CheckTag(ciphertext
, GetTag(ciphertext
))) {
172 *output_length
= ciphertext
.size() - kTagSize
;
173 memcpy(output
, ciphertext
.data(), *output_length
);
177 virtual QuicData
* DecryptPacket(QuicPacketSequenceNumber sequence_number
,
178 StringPiece associated_data
,
179 StringPiece ciphertext
) OVERRIDE
{
180 if (ciphertext
.size() < kTagSize
) {
183 if (!CheckTag(ciphertext
, GetTag(ciphertext
))) {
186 const size_t len
= ciphertext
.size() - kTagSize
;
187 uint8
* buf
= new uint8
[len
];
188 memcpy(buf
, ciphertext
.data(), len
);
189 return new QuicData(reinterpret_cast<char*>(buf
), len
,
190 true /* owns buffer */);
193 virtual StringPiece
GetKey() const OVERRIDE
{ return StringPiece(); }
194 virtual StringPiece
GetNoncePrefix() const OVERRIDE
{ return StringPiece(); }
197 virtual uint8
GetTag(StringPiece ciphertext
) {
198 return ciphertext
.data()[ciphertext
.size()-1];
206 bool CheckTag(StringPiece ciphertext
, uint8 tag
) {
207 for (size_t i
= ciphertext
.size() - kTagSize
; i
< ciphertext
.size(); i
++) {
208 if (ciphertext
.data()[i
] != tag
) {
217 // StringTaggingDecrypter ensures that the final kTagSize bytes of the message
218 // match the expected value.
219 class StrictTaggingDecrypter
: public TaggingDecrypter
{
221 explicit StrictTaggingDecrypter(uint8 tag
) : tag_(tag
) {}
222 virtual ~StrictTaggingDecrypter() {}
224 // TaggingQuicDecrypter
225 virtual uint8
GetTag(StringPiece ciphertext
) OVERRIDE
{
233 class TestConnectionHelper
: public QuicConnectionHelperInterface
{
235 class TestAlarm
: public QuicAlarm
{
237 explicit TestAlarm(QuicAlarm::Delegate
* delegate
)
238 : QuicAlarm(delegate
) {
241 virtual void SetImpl() OVERRIDE
{}
242 virtual void CancelImpl() OVERRIDE
{}
243 using QuicAlarm::Fire
;
246 TestConnectionHelper(MockClock
* clock
, MockRandom
* random_generator
)
248 random_generator_(random_generator
) {
249 clock_
->AdvanceTime(QuicTime::Delta::FromSeconds(1));
252 // QuicConnectionHelperInterface
253 virtual const QuicClock
* GetClock() const OVERRIDE
{
257 virtual QuicRandom
* GetRandomGenerator() OVERRIDE
{
258 return random_generator_
;
261 virtual QuicAlarm
* CreateAlarm(QuicAlarm::Delegate
* delegate
) OVERRIDE
{
262 return new TestAlarm(delegate
);
267 MockRandom
* random_generator_
;
269 DISALLOW_COPY_AND_ASSIGN(TestConnectionHelper
);
272 class TestPacketWriter
: public QuicPacketWriter
{
274 explicit TestPacketWriter(QuicVersion version
)
276 framer_(SupportedVersions(version_
)),
277 last_packet_size_(0),
278 write_blocked_(false),
279 block_on_next_write_(false),
280 is_write_blocked_data_buffered_(false),
281 final_bytes_of_last_packet_(0),
282 final_bytes_of_previous_packet_(0),
283 use_tagging_decrypter_(false),
284 packets_write_attempts_(0) {
287 // QuicPacketWriter interface
288 virtual WriteResult
WritePacket(
289 const char* buffer
, size_t buf_len
,
290 const IPAddressNumber
& self_address
,
291 const IPEndPoint
& peer_address
) OVERRIDE
{
292 QuicEncryptedPacket
packet(buffer
, buf_len
);
293 ++packets_write_attempts_
;
295 if (packet
.length() >= sizeof(final_bytes_of_last_packet_
)) {
296 final_bytes_of_previous_packet_
= final_bytes_of_last_packet_
;
297 memcpy(&final_bytes_of_last_packet_
, packet
.data() + packet
.length() - 4,
298 sizeof(final_bytes_of_last_packet_
));
301 if (use_tagging_decrypter_
) {
302 framer_
.framer()->SetDecrypter(new TaggingDecrypter
, ENCRYPTION_NONE
);
304 EXPECT_TRUE(framer_
.ProcessPacket(packet
));
305 if (block_on_next_write_
) {
306 write_blocked_
= true;
307 block_on_next_write_
= false;
309 if (IsWriteBlocked()) {
310 return WriteResult(WRITE_STATUS_BLOCKED
, -1);
312 last_packet_size_
= packet
.length();
313 return WriteResult(WRITE_STATUS_OK
, last_packet_size_
);
316 virtual bool IsWriteBlockedDataBuffered() const OVERRIDE
{
317 return is_write_blocked_data_buffered_
;
320 virtual bool IsWriteBlocked() const OVERRIDE
{ return write_blocked_
; }
322 virtual void SetWritable() OVERRIDE
{ write_blocked_
= false; }
324 void BlockOnNextWrite() { block_on_next_write_
= true; }
326 const QuicPacketHeader
& header() { return framer_
.header(); }
328 size_t frame_count() const { return framer_
.num_frames(); }
330 const vector
<QuicAckFrame
>& ack_frames() const {
331 return framer_
.ack_frames();
334 const vector
<QuicCongestionFeedbackFrame
>& feedback_frames() const {
335 return framer_
.feedback_frames();
338 const vector
<QuicStopWaitingFrame
>& stop_waiting_frames() const {
339 return framer_
.stop_waiting_frames();
342 const vector
<QuicConnectionCloseFrame
>& connection_close_frames() const {
343 return framer_
.connection_close_frames();
346 const vector
<QuicStreamFrame
>& stream_frames() const {
347 return framer_
.stream_frames();
350 const vector
<QuicPingFrame
>& ping_frames() const {
351 return framer_
.ping_frames();
354 size_t last_packet_size() {
355 return last_packet_size_
;
358 const QuicVersionNegotiationPacket
* version_negotiation_packet() {
359 return framer_
.version_negotiation_packet();
362 void set_is_write_blocked_data_buffered(bool buffered
) {
363 is_write_blocked_data_buffered_
= buffered
;
366 void set_is_server(bool is_server
) {
367 // We invert is_server here, because the framer needs to parse packets
369 QuicFramerPeer::SetIsServer(framer_
.framer(), !is_server
);
372 // final_bytes_of_last_packet_ returns the last four bytes of the previous
373 // packet as a little-endian, uint32. This is intended to be used with a
374 // TaggingEncrypter so that tests can determine which encrypter was used for
376 uint32
final_bytes_of_last_packet() { return final_bytes_of_last_packet_
; }
378 // Returns the final bytes of the second to last packet.
379 uint32
final_bytes_of_previous_packet() {
380 return final_bytes_of_previous_packet_
;
383 void use_tagging_decrypter() {
384 use_tagging_decrypter_
= true;
387 uint32
packets_write_attempts() { return packets_write_attempts_
; }
389 void Reset() { framer_
.Reset(); }
391 void SetSupportedVersions(const QuicVersionVector
& versions
) {
392 framer_
.SetSupportedVersions(versions
);
396 QuicVersion version_
;
397 SimpleQuicFramer framer_
;
398 size_t last_packet_size_
;
400 bool block_on_next_write_
;
401 bool is_write_blocked_data_buffered_
;
402 uint32 final_bytes_of_last_packet_
;
403 uint32 final_bytes_of_previous_packet_
;
404 bool use_tagging_decrypter_
;
405 uint32 packets_write_attempts_
;
407 DISALLOW_COPY_AND_ASSIGN(TestPacketWriter
);
410 class TestConnection
: public QuicConnection
{
412 TestConnection(QuicConnectionId connection_id
,
414 TestConnectionHelper
* helper
,
415 TestPacketWriter
* writer
,
418 : QuicConnection(connection_id
, address
, helper
, writer
, is_server
,
419 SupportedVersions(version
)),
421 // Disable tail loss probes for most tests.
422 QuicSentPacketManagerPeer::SetMaxTailLossProbes(
423 QuicConnectionPeer::GetSentPacketManager(this), 0);
424 writer_
->set_is_server(is_server
);
428 QuicConnectionPeer::SendAck(this);
431 void SetReceiveAlgorithm(TestReceiveAlgorithm
* receive_algorithm
) {
432 QuicConnectionPeer::SetReceiveAlgorithm(this, receive_algorithm
);
435 void SetSendAlgorithm(SendAlgorithmInterface
* send_algorithm
) {
436 QuicConnectionPeer::SetSendAlgorithm(this, send_algorithm
);
439 void SetLossAlgorithm(LossDetectionInterface
* loss_algorithm
) {
440 QuicSentPacketManagerPeer::SetLossAlgorithm(
441 QuicConnectionPeer::GetSentPacketManager(this), loss_algorithm
);
444 void SendPacket(EncryptionLevel level
,
445 QuicPacketSequenceNumber sequence_number
,
447 QuicPacketEntropyHash entropy_hash
,
448 HasRetransmittableData retransmittable
) {
449 RetransmittableFrames
* retransmittable_frames
=
450 retransmittable
== HAS_RETRANSMITTABLE_DATA
?
451 new RetransmittableFrames() : NULL
;
453 SerializedPacket(sequence_number
, PACKET_6BYTE_SEQUENCE_NUMBER
,
454 packet
, entropy_hash
, retransmittable_frames
));
457 QuicConsumedData
SendStreamDataWithString(
460 QuicStreamOffset offset
,
462 QuicAckNotifier::DelegateInterface
* delegate
) {
463 return SendStreamDataWithStringHelper(id
, data
, offset
, fin
,
464 MAY_FEC_PROTECT
, delegate
);
467 QuicConsumedData
SendStreamDataWithStringWithFec(
470 QuicStreamOffset offset
,
472 QuicAckNotifier::DelegateInterface
* delegate
) {
473 return SendStreamDataWithStringHelper(id
, data
, offset
, fin
,
474 MUST_FEC_PROTECT
, delegate
);
477 QuicConsumedData
SendStreamDataWithStringHelper(
480 QuicStreamOffset offset
,
482 FecProtection fec_protection
,
483 QuicAckNotifier::DelegateInterface
* delegate
) {
486 data_iov
.Append(const_cast<char*>(data
.data()), data
.size());
488 return QuicConnection::SendStreamData(id
, data_iov
, offset
, fin
,
489 fec_protection
, delegate
);
492 QuicConsumedData
SendStreamData3() {
493 return SendStreamDataWithString(kClientDataStreamId1
, "food", 0, !kFin
,
497 QuicConsumedData
SendStreamData3WithFec() {
498 return SendStreamDataWithStringWithFec(kClientDataStreamId1
, "food", 0,
502 QuicConsumedData
SendStreamData5() {
503 return SendStreamDataWithString(kClientDataStreamId2
, "food2", 0,
507 QuicConsumedData
SendStreamData5WithFec() {
508 return SendStreamDataWithStringWithFec(kClientDataStreamId2
, "food2", 0,
511 // Ensures the connection can write stream data before writing.
512 QuicConsumedData
EnsureWritableAndSendStreamData5() {
513 EXPECT_TRUE(CanWriteStreamData());
514 return SendStreamData5();
517 // The crypto stream has special semantics so that it is not blocked by a
518 // congestion window limitation, and also so that it gets put into a separate
519 // packet (so that it is easier to reason about a crypto frame not being
520 // split needlessly across packet boundaries). As a result, we have separate
521 // tests for some cases for this stream.
522 QuicConsumedData
SendCryptoStreamData() {
523 return SendStreamDataWithString(kCryptoStreamId
, "chlo", 0, !kFin
, NULL
);
527 return QuicConnectionPeer::IsServer(this);
530 void set_version(QuicVersion version
) {
531 QuicConnectionPeer::GetFramer(this)->set_version(version
);
534 void SetSupportedVersions(const QuicVersionVector
& versions
) {
535 QuicConnectionPeer::GetFramer(this)->SetSupportedVersions(versions
);
536 writer_
->SetSupportedVersions(versions
);
539 void set_is_server(bool is_server
) {
540 writer_
->set_is_server(is_server
);
541 QuicConnectionPeer::SetIsServer(this, is_server
);
544 TestConnectionHelper::TestAlarm
* GetAckAlarm() {
545 return reinterpret_cast<TestConnectionHelper::TestAlarm
*>(
546 QuicConnectionPeer::GetAckAlarm(this));
549 TestConnectionHelper::TestAlarm
* GetPingAlarm() {
550 return reinterpret_cast<TestConnectionHelper::TestAlarm
*>(
551 QuicConnectionPeer::GetPingAlarm(this));
554 TestConnectionHelper::TestAlarm
* GetResumeWritesAlarm() {
555 return reinterpret_cast<TestConnectionHelper::TestAlarm
*>(
556 QuicConnectionPeer::GetResumeWritesAlarm(this));
559 TestConnectionHelper::TestAlarm
* GetRetransmissionAlarm() {
560 return reinterpret_cast<TestConnectionHelper::TestAlarm
*>(
561 QuicConnectionPeer::GetRetransmissionAlarm(this));
564 TestConnectionHelper::TestAlarm
* GetSendAlarm() {
565 return reinterpret_cast<TestConnectionHelper::TestAlarm
*>(
566 QuicConnectionPeer::GetSendAlarm(this));
569 TestConnectionHelper::TestAlarm
* GetTimeoutAlarm() {
570 return reinterpret_cast<TestConnectionHelper::TestAlarm
*>(
571 QuicConnectionPeer::GetTimeoutAlarm(this));
574 using QuicConnection::SelectMutualVersion
;
577 TestPacketWriter
* writer_
;
579 DISALLOW_COPY_AND_ASSIGN(TestConnection
);
582 // Used for testing packets revived from FEC packets.
583 class FecQuicConnectionDebugVisitor
584 : public QuicConnectionDebugVisitor
{
586 virtual void OnRevivedPacket(const QuicPacketHeader
& header
,
587 StringPiece data
) OVERRIDE
{
588 revived_header_
= header
;
591 // Public accessor method.
592 QuicPacketHeader
revived_header() const {
593 return revived_header_
;
597 QuicPacketHeader revived_header_
;
600 class QuicConnectionTest
: public ::testing::TestWithParam
<QuicVersion
> {
603 : connection_id_(42),
604 framer_(SupportedVersions(version()), QuicTime::Zero(), false),
605 peer_creator_(connection_id_
, &framer_
, &random_generator_
),
606 send_algorithm_(new StrictMock
<MockSendAlgorithm
>),
607 loss_algorithm_(new MockLossAlgorithm()),
608 helper_(new TestConnectionHelper(&clock_
, &random_generator_
)),
609 writer_(new TestPacketWriter(version())),
610 connection_(connection_id_
, IPEndPoint(), helper_
.get(),
611 writer_
.get(), false, version()),
612 frame1_(1, false, 0, MakeIOVector(data1
)),
613 frame2_(1, false, 3, MakeIOVector(data2
)),
614 sequence_number_length_(PACKET_6BYTE_SEQUENCE_NUMBER
),
615 connection_id_length_(PACKET_8BYTE_CONNECTION_ID
) {
616 connection_
.set_visitor(&visitor_
);
617 connection_
.SetSendAlgorithm(send_algorithm_
);
618 connection_
.SetLossAlgorithm(loss_algorithm_
);
619 framer_
.set_received_entropy_calculator(&entropy_calculator_
);
620 // Simplify tests by not sending feedback unless specifically configured.
623 *send_algorithm_
, TimeUntilSend(_
, _
, _
)).WillRepeatedly(Return(
624 QuicTime::Delta::Zero()));
625 EXPECT_CALL(*receive_algorithm_
,
626 RecordIncomingPacket(_
, _
, _
)).Times(AnyNumber());
627 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
))
629 EXPECT_CALL(*send_algorithm_
, RetransmissionDelay()).WillRepeatedly(
630 Return(QuicTime::Delta::Zero()));
631 EXPECT_CALL(*send_algorithm_
, GetCongestionWindow()).WillRepeatedly(
632 Return(kMaxPacketSize
));
633 ON_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
))
634 .WillByDefault(Return(true));
635 EXPECT_CALL(visitor_
, WillingAndAbleToWrite()).Times(AnyNumber());
636 EXPECT_CALL(visitor_
, HasPendingHandshake()).Times(AnyNumber());
637 EXPECT_CALL(visitor_
, OnCanWrite()).Times(AnyNumber());
638 EXPECT_CALL(visitor_
, HasOpenDataStreams()).WillRepeatedly(Return(false));
640 EXPECT_CALL(*loss_algorithm_
, GetLossTimeout())
641 .WillRepeatedly(Return(QuicTime::Zero()));
642 EXPECT_CALL(*loss_algorithm_
, DetectLostPackets(_
, _
, _
, _
))
643 .WillRepeatedly(Return(SequenceNumberSet()));
646 QuicVersion
version() {
650 QuicAckFrame
* outgoing_ack() {
651 outgoing_ack_
.reset(QuicConnectionPeer::CreateAckFrame(&connection_
));
652 return outgoing_ack_
.get();
655 QuicPacketSequenceNumber
least_unacked() {
656 if (version() <= QUIC_VERSION_15
) {
657 if (writer_
->ack_frames().empty()) {
660 return writer_
->ack_frames()[0].sent_info
.least_unacked
;
662 if (writer_
->stop_waiting_frames().empty()) {
665 return writer_
->stop_waiting_frames()[0].least_unacked
;
668 void use_tagging_decrypter() {
669 writer_
->use_tagging_decrypter();
672 void ProcessPacket(QuicPacketSequenceNumber number
) {
673 EXPECT_CALL(visitor_
, OnStreamFrames(_
)).Times(1);
674 ProcessDataPacket(number
, 0, !kEntropyFlag
);
677 QuicPacketEntropyHash
ProcessFramePacket(QuicFrame frame
) {
679 frames
.push_back(QuicFrame(frame
));
680 QuicPacketCreatorPeer::SetSendVersionInPacket(&peer_creator_
,
681 connection_
.is_server());
682 SerializedPacket serialized_packet
=
683 peer_creator_
.SerializeAllFrames(frames
);
684 scoped_ptr
<QuicPacket
> packet(serialized_packet
.packet
);
685 scoped_ptr
<QuicEncryptedPacket
> encrypted(
686 framer_
.EncryptPacket(ENCRYPTION_NONE
,
687 serialized_packet
.sequence_number
, *packet
));
688 connection_
.ProcessUdpPacket(IPEndPoint(), IPEndPoint(), *encrypted
);
689 return serialized_packet
.entropy_hash
;
692 size_t ProcessDataPacket(QuicPacketSequenceNumber number
,
693 QuicFecGroupNumber fec_group
,
695 return ProcessDataPacketAtLevel(number
, fec_group
, entropy_flag
,
699 size_t ProcessDataPacketAtLevel(QuicPacketSequenceNumber number
,
700 QuicFecGroupNumber fec_group
,
702 EncryptionLevel level
) {
703 scoped_ptr
<QuicPacket
> packet(ConstructDataPacket(number
, fec_group
,
705 scoped_ptr
<QuicEncryptedPacket
> encrypted(framer_
.EncryptPacket(
706 level
, number
, *packet
));
707 connection_
.ProcessUdpPacket(IPEndPoint(), IPEndPoint(), *encrypted
);
708 return encrypted
->length();
711 void ProcessClosePacket(QuicPacketSequenceNumber number
,
712 QuicFecGroupNumber fec_group
) {
713 scoped_ptr
<QuicPacket
> packet(ConstructClosePacket(number
, fec_group
));
714 scoped_ptr
<QuicEncryptedPacket
> encrypted(framer_
.EncryptPacket(
715 ENCRYPTION_NONE
, number
, *packet
));
716 connection_
.ProcessUdpPacket(IPEndPoint(), IPEndPoint(), *encrypted
);
719 size_t ProcessFecProtectedPacket(QuicPacketSequenceNumber number
,
720 bool expect_revival
, bool entropy_flag
) {
721 if (expect_revival
) {
722 EXPECT_CALL(visitor_
, OnStreamFrames(_
)).Times(1);
724 EXPECT_CALL(visitor_
, OnStreamFrames(_
)).Times(1).
725 RetiresOnSaturation();
726 return ProcessDataPacket(number
, 1, entropy_flag
);
729 // Processes an FEC packet that covers the packets that would have been
731 size_t ProcessFecPacket(QuicPacketSequenceNumber number
,
732 QuicPacketSequenceNumber min_protected_packet
,
735 QuicPacket
* packet
) {
736 if (expect_revival
) {
737 EXPECT_CALL(visitor_
, OnStreamFrames(_
)).Times(1);
740 // Construct the decrypted data packet so we can compute the correct
741 // redundancy. If |packet| has been provided then use that, otherwise
742 // construct a default data packet.
743 scoped_ptr
<QuicPacket
> data_packet
;
745 data_packet
.reset(packet
);
747 data_packet
.reset(ConstructDataPacket(number
, 1, !kEntropyFlag
));
750 header_
.public_header
.connection_id
= connection_id_
;
751 header_
.public_header
.reset_flag
= false;
752 header_
.public_header
.version_flag
= false;
753 header_
.public_header
.sequence_number_length
= sequence_number_length_
;
754 header_
.public_header
.connection_id_length
= connection_id_length_
;
755 header_
.packet_sequence_number
= number
;
756 header_
.entropy_flag
= entropy_flag
;
757 header_
.fec_flag
= true;
758 header_
.is_in_fec_group
= IN_FEC_GROUP
;
759 header_
.fec_group
= min_protected_packet
;
760 QuicFecData fec_data
;
761 fec_data
.fec_group
= header_
.fec_group
;
763 // Since all data packets in this test have the same payload, the
764 // redundancy is either equal to that payload or the xor of that payload
765 // with itself, depending on the number of packets.
766 if (((number
- min_protected_packet
) % 2) == 0) {
767 for (size_t i
= GetStartOfFecProtectedData(
768 header_
.public_header
.connection_id_length
,
769 header_
.public_header
.version_flag
,
770 header_
.public_header
.sequence_number_length
);
771 i
< data_packet
->length(); ++i
) {
772 data_packet
->mutable_data()[i
] ^= data_packet
->data()[i
];
775 fec_data
.redundancy
= data_packet
->FecProtectedData();
777 scoped_ptr
<QuicPacket
> fec_packet(
778 framer_
.BuildFecPacket(header_
, fec_data
).packet
);
779 scoped_ptr
<QuicEncryptedPacket
> encrypted(
780 framer_
.EncryptPacket(ENCRYPTION_NONE
, number
, *fec_packet
));
782 connection_
.ProcessUdpPacket(IPEndPoint(), IPEndPoint(), *encrypted
);
783 return encrypted
->length();
786 QuicByteCount
SendStreamDataToPeer(QuicStreamId id
,
788 QuicStreamOffset offset
,
790 QuicPacketSequenceNumber
* last_packet
) {
791 QuicByteCount packet_size
;
792 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
))
793 .WillOnce(DoAll(SaveArg
<3>(&packet_size
), Return(true)));
794 connection_
.SendStreamDataWithString(id
, data
, offset
, fin
, NULL
);
795 if (last_packet
!= NULL
) {
797 QuicConnectionPeer::GetPacketCreator(&connection_
)->sequence_number();
799 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
))
804 void SendAckPacketToPeer() {
805 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
)).Times(1);
806 connection_
.SendAck();
807 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
))
811 QuicPacketEntropyHash
ProcessAckPacket(QuicAckFrame
* frame
) {
812 return ProcessFramePacket(QuicFrame(frame
));
815 QuicPacketEntropyHash
ProcessStopWaitingPacket(QuicStopWaitingFrame
* frame
) {
816 return ProcessFramePacket(QuicFrame(frame
));
819 QuicPacketEntropyHash
ProcessGoAwayPacket(QuicGoAwayFrame
* frame
) {
820 return ProcessFramePacket(QuicFrame(frame
));
823 bool IsMissing(QuicPacketSequenceNumber number
) {
824 return IsAwaitingPacket(outgoing_ack()->received_info
, number
);
827 QuicPacket
* ConstructDataPacket(QuicPacketSequenceNumber number
,
828 QuicFecGroupNumber fec_group
,
830 header_
.public_header
.connection_id
= connection_id_
;
831 header_
.public_header
.reset_flag
= false;
832 header_
.public_header
.version_flag
= false;
833 header_
.public_header
.sequence_number_length
= sequence_number_length_
;
834 header_
.public_header
.connection_id_length
= connection_id_length_
;
835 header_
.entropy_flag
= entropy_flag
;
836 header_
.fec_flag
= false;
837 header_
.packet_sequence_number
= number
;
838 header_
.is_in_fec_group
= fec_group
== 0u ? NOT_IN_FEC_GROUP
: IN_FEC_GROUP
;
839 header_
.fec_group
= fec_group
;
842 QuicFrame
frame(&frame1_
);
843 frames
.push_back(frame
);
845 BuildUnsizedDataPacket(&framer_
, header_
, frames
).packet
;
846 EXPECT_TRUE(packet
!= NULL
);
850 QuicPacket
* ConstructClosePacket(QuicPacketSequenceNumber number
,
851 QuicFecGroupNumber fec_group
) {
852 header_
.public_header
.connection_id
= connection_id_
;
853 header_
.packet_sequence_number
= number
;
854 header_
.public_header
.reset_flag
= false;
855 header_
.public_header
.version_flag
= false;
856 header_
.entropy_flag
= false;
857 header_
.fec_flag
= false;
858 header_
.is_in_fec_group
= fec_group
== 0u ? NOT_IN_FEC_GROUP
: IN_FEC_GROUP
;
859 header_
.fec_group
= fec_group
;
861 QuicConnectionCloseFrame qccf
;
862 qccf
.error_code
= QUIC_PEER_GOING_AWAY
;
865 QuicFrame
frame(&qccf
);
866 frames
.push_back(frame
);
868 BuildUnsizedDataPacket(&framer_
, header_
, frames
).packet
;
869 EXPECT_TRUE(packet
!= NULL
);
873 void SetFeedback(QuicCongestionFeedbackFrame
* feedback
) {
874 receive_algorithm_
= new TestReceiveAlgorithm(feedback
);
875 connection_
.SetReceiveAlgorithm(receive_algorithm_
);
878 QuicTime::Delta
DefaultRetransmissionTime() {
879 return QuicTime::Delta::FromMilliseconds(kDefaultRetransmissionTimeMs
);
882 QuicTime::Delta
DefaultDelayedAckTime() {
883 return QuicTime::Delta::FromMilliseconds(kMinRetransmissionTimeMs
/2);
886 // Initialize a frame acknowledging all packets up to largest_observed.
887 const QuicAckFrame
InitAckFrame(QuicPacketSequenceNumber largest_observed
,
888 QuicPacketSequenceNumber least_unacked
) {
889 QuicAckFrame
frame(MakeAckFrame(largest_observed
, least_unacked
));
890 if (largest_observed
> 0) {
891 frame
.received_info
.entropy_hash
=
892 QuicConnectionPeer::GetSentEntropyHash(&connection_
, largest_observed
);
897 const QuicStopWaitingFrame
InitStopWaitingFrame(
898 QuicPacketSequenceNumber least_unacked
) {
899 QuicStopWaitingFrame frame
;
900 frame
.least_unacked
= least_unacked
;
903 // Explicitly nack a packet.
904 void NackPacket(QuicPacketSequenceNumber missing
, QuicAckFrame
* frame
) {
905 frame
->received_info
.missing_packets
.insert(missing
);
906 frame
->received_info
.entropy_hash
^=
907 QuicConnectionPeer::GetSentEntropyHash(&connection_
, missing
);
909 frame
->received_info
.entropy_hash
^=
910 QuicConnectionPeer::GetSentEntropyHash(&connection_
, missing
- 1);
914 // Undo nacking a packet within the frame.
915 void AckPacket(QuicPacketSequenceNumber arrived
, QuicAckFrame
* frame
) {
916 EXPECT_THAT(frame
->received_info
.missing_packets
, Contains(arrived
));
917 frame
->received_info
.missing_packets
.erase(arrived
);
918 frame
->received_info
.entropy_hash
^=
919 QuicConnectionPeer::GetSentEntropyHash(&connection_
, arrived
);
921 frame
->received_info
.entropy_hash
^=
922 QuicConnectionPeer::GetSentEntropyHash(&connection_
, arrived
- 1);
926 void TriggerConnectionClose() {
927 // Send an erroneous packet to close the connection.
928 EXPECT_CALL(visitor_
,
929 OnConnectionClosed(QUIC_INVALID_PACKET_HEADER
, false));
930 // Call ProcessDataPacket rather than ProcessPacket, as we should not get a
931 // packet call to the visitor.
932 ProcessDataPacket(6000, 0, !kEntropyFlag
);
934 QuicConnectionPeer::GetConnectionClosePacket(&connection_
) == NULL
);
937 void BlockOnNextWrite() {
938 writer_
->BlockOnNextWrite();
939 EXPECT_CALL(visitor_
, OnWriteBlocked()).Times(AtLeast(1));
942 void CongestionBlockWrites() {
943 EXPECT_CALL(*send_algorithm_
,
944 TimeUntilSend(_
, _
, _
)).WillRepeatedly(
945 testing::Return(QuicTime::Delta::FromSeconds(1)));
948 void CongestionUnblockWrites() {
949 EXPECT_CALL(*send_algorithm_
,
950 TimeUntilSend(_
, _
, _
)).WillRepeatedly(
951 testing::Return(QuicTime::Delta::Zero()));
954 QuicConnectionId connection_id_
;
956 QuicPacketCreator peer_creator_
;
957 MockEntropyCalculator entropy_calculator_
;
959 MockSendAlgorithm
* send_algorithm_
;
960 MockLossAlgorithm
* loss_algorithm_
;
961 TestReceiveAlgorithm
* receive_algorithm_
;
963 MockRandom random_generator_
;
964 scoped_ptr
<TestConnectionHelper
> helper_
;
965 scoped_ptr
<TestPacketWriter
> writer_
;
966 TestConnection connection_
;
967 StrictMock
<MockConnectionVisitor
> visitor_
;
969 QuicPacketHeader header_
;
970 QuicStreamFrame frame1_
;
971 QuicStreamFrame frame2_
;
972 scoped_ptr
<QuicAckFrame
> outgoing_ack_
;
973 QuicSequenceNumberLength sequence_number_length_
;
974 QuicConnectionIdLength connection_id_length_
;
977 DISALLOW_COPY_AND_ASSIGN(QuicConnectionTest
);
980 // Run all end to end tests with all supported versions.
981 INSTANTIATE_TEST_CASE_P(SupportedVersion
,
983 ::testing::ValuesIn(QuicSupportedVersions()));
985 TEST_P(QuicConnectionTest
, PacketsInOrder
) {
986 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
989 EXPECT_EQ(1u, outgoing_ack()->received_info
.largest_observed
);
990 EXPECT_EQ(0u, outgoing_ack()->received_info
.missing_packets
.size());
993 EXPECT_EQ(2u, outgoing_ack()->received_info
.largest_observed
);
994 EXPECT_EQ(0u, outgoing_ack()->received_info
.missing_packets
.size());
997 EXPECT_EQ(3u, outgoing_ack()->received_info
.largest_observed
);
998 EXPECT_EQ(0u, outgoing_ack()->received_info
.missing_packets
.size());
1001 TEST_P(QuicConnectionTest
, PacketsOutOfOrder
) {
1002 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
1005 EXPECT_EQ(3u, outgoing_ack()->received_info
.largest_observed
);
1006 EXPECT_TRUE(IsMissing(2));
1007 EXPECT_TRUE(IsMissing(1));
1010 EXPECT_EQ(3u, outgoing_ack()->received_info
.largest_observed
);
1011 EXPECT_FALSE(IsMissing(2));
1012 EXPECT_TRUE(IsMissing(1));
1015 EXPECT_EQ(3u, outgoing_ack()->received_info
.largest_observed
);
1016 EXPECT_FALSE(IsMissing(2));
1017 EXPECT_FALSE(IsMissing(1));
1020 TEST_P(QuicConnectionTest
, DuplicatePacket
) {
1021 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
1024 EXPECT_EQ(3u, outgoing_ack()->received_info
.largest_observed
);
1025 EXPECT_TRUE(IsMissing(2));
1026 EXPECT_TRUE(IsMissing(1));
1028 // Send packet 3 again, but do not set the expectation that
1029 // the visitor OnStreamFrames() will be called.
1030 ProcessDataPacket(3, 0, !kEntropyFlag
);
1031 EXPECT_EQ(3u, outgoing_ack()->received_info
.largest_observed
);
1032 EXPECT_TRUE(IsMissing(2));
1033 EXPECT_TRUE(IsMissing(1));
1036 TEST_P(QuicConnectionTest
, PacketsOutOfOrderWithAdditionsAndLeastAwaiting
) {
1037 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
1040 EXPECT_EQ(3u, outgoing_ack()->received_info
.largest_observed
);
1041 EXPECT_TRUE(IsMissing(2));
1042 EXPECT_TRUE(IsMissing(1));
1045 EXPECT_EQ(3u, outgoing_ack()->received_info
.largest_observed
);
1046 EXPECT_TRUE(IsMissing(1));
1049 EXPECT_EQ(5u, outgoing_ack()->received_info
.largest_observed
);
1050 EXPECT_TRUE(IsMissing(1));
1051 EXPECT_TRUE(IsMissing(4));
1053 // Pretend at this point the client has gotten acks for 2 and 3 and 1 is a
1054 // packet the peer will not retransmit. It indicates this by sending 'least
1055 // awaiting' is 4. The connection should then realize 1 will not be
1056 // retransmitted, and will remove it from the missing list.
1057 peer_creator_
.set_sequence_number(5);
1058 QuicAckFrame frame
= InitAckFrame(1, 4);
1059 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(_
, _
, _
, _
));
1060 ProcessAckPacket(&frame
);
1062 // Force an ack to be sent.
1063 SendAckPacketToPeer();
1064 EXPECT_TRUE(IsMissing(4));
1067 TEST_P(QuicConnectionTest
, RejectPacketTooFarOut
) {
1068 EXPECT_CALL(visitor_
,
1069 OnConnectionClosed(QUIC_INVALID_PACKET_HEADER
, false));
1070 // Call ProcessDataPacket rather than ProcessPacket, as we should not get a
1071 // packet call to the visitor.
1072 ProcessDataPacket(6000, 0, !kEntropyFlag
);
1074 QuicConnectionPeer::GetConnectionClosePacket(&connection_
) == NULL
);
1077 TEST_P(QuicConnectionTest
, RejectUnencryptedStreamData
) {
1078 // Process an unencrypted packet from the non-crypto stream.
1079 frame1_
.stream_id
= 3;
1080 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
1081 EXPECT_CALL(visitor_
, OnConnectionClosed(QUIC_UNENCRYPTED_STREAM_DATA
,
1083 ProcessDataPacket(1, 0, !kEntropyFlag
);
1085 QuicConnectionPeer::GetConnectionClosePacket(&connection_
) == NULL
);
1086 const vector
<QuicConnectionCloseFrame
>& connection_close_frames
=
1087 writer_
->connection_close_frames();
1088 EXPECT_EQ(1u, connection_close_frames
.size());
1089 EXPECT_EQ(QUIC_UNENCRYPTED_STREAM_DATA
,
1090 connection_close_frames
[0].error_code
);
1093 TEST_P(QuicConnectionTest
, TruncatedAck
) {
1094 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
1095 QuicPacketSequenceNumber num_packets
= 256 * 2 + 1;
1096 for (QuicPacketSequenceNumber i
= 0; i
< num_packets
; ++i
) {
1097 SendStreamDataToPeer(3, "foo", i
* 3, !kFin
, NULL
);
1100 QuicAckFrame frame
= InitAckFrame(num_packets
, 1);
1101 SequenceNumberSet lost_packets
;
1102 // Create an ack with 256 nacks, none adjacent to one another.
1103 for (QuicPacketSequenceNumber i
= 1; i
<= 256; ++i
) {
1104 NackPacket(i
* 2, &frame
);
1105 if (i
< 256) { // Last packet is nacked, but not lost.
1106 lost_packets
.insert(i
* 2);
1109 EXPECT_CALL(*loss_algorithm_
, DetectLostPackets(_
, _
, _
, _
))
1110 .WillOnce(Return(lost_packets
));
1111 EXPECT_CALL(entropy_calculator_
,
1112 EntropyHash(511)).WillOnce(testing::Return(0));
1113 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
1114 ProcessAckPacket(&frame
);
1116 QuicReceivedPacketManager
* received_packet_manager
=
1117 QuicConnectionPeer::GetReceivedPacketManager(&connection_
);
1118 // A truncated ack will not have the true largest observed.
1119 EXPECT_GT(num_packets
,
1120 received_packet_manager
->peer_largest_observed_packet());
1122 AckPacket(192, &frame
);
1124 // Removing one missing packet allows us to ack 192 and one more range, but
1125 // 192 has already been declared lost, so it doesn't register as an ack.
1126 EXPECT_CALL(*loss_algorithm_
, DetectLostPackets(_
, _
, _
, _
))
1127 .WillOnce(Return(SequenceNumberSet()));
1128 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
1129 ProcessAckPacket(&frame
);
1130 EXPECT_EQ(num_packets
,
1131 received_packet_manager
->peer_largest_observed_packet());
1134 TEST_P(QuicConnectionTest
, AckReceiptCausesAckSendBadEntropy
) {
1135 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
1138 // Delay sending, then queue up an ack.
1139 EXPECT_CALL(*send_algorithm_
,
1140 TimeUntilSend(_
, _
, _
)).WillOnce(
1141 testing::Return(QuicTime::Delta::FromMicroseconds(1)));
1142 QuicConnectionPeer::SendAck(&connection_
);
1144 // Process an ack with a least unacked of the received ack.
1145 // This causes an ack to be sent when TimeUntilSend returns 0.
1146 EXPECT_CALL(*send_algorithm_
,
1147 TimeUntilSend(_
, _
, _
)).WillRepeatedly(
1148 testing::Return(QuicTime::Delta::Zero()));
1149 // Skip a packet and then record an ack.
1150 peer_creator_
.set_sequence_number(2);
1151 QuicAckFrame frame
= InitAckFrame(0, 3);
1152 ProcessAckPacket(&frame
);
1155 TEST_P(QuicConnectionTest
, OutOfOrderReceiptCausesAckSend
) {
1156 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
1159 // Should ack immediately since we have missing packets.
1160 EXPECT_EQ(1u, writer_
->packets_write_attempts());
1163 // Should ack immediately since we have missing packets.
1164 EXPECT_EQ(2u, writer_
->packets_write_attempts());
1167 // Should ack immediately, since this fills the last hole.
1168 EXPECT_EQ(3u, writer_
->packets_write_attempts());
1171 // Should not cause an ack.
1172 EXPECT_EQ(3u, writer_
->packets_write_attempts());
1175 TEST_P(QuicConnectionTest
, AckReceiptCausesAckSend
) {
1176 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
1178 QuicPacketSequenceNumber original
;
1179 QuicByteCount packet_size
;
1180 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
))
1181 .WillOnce(DoAll(SaveArg
<2>(&original
), SaveArg
<3>(&packet_size
),
1183 connection_
.SendStreamDataWithString(3, "foo", 0, !kFin
, NULL
);
1184 QuicAckFrame frame
= InitAckFrame(original
, 1);
1185 NackPacket(original
, &frame
);
1186 // First nack triggers early retransmit.
1187 SequenceNumberSet lost_packets
;
1188 lost_packets
.insert(1);
1189 EXPECT_CALL(*loss_algorithm_
, DetectLostPackets(_
, _
, _
, _
))
1190 .WillOnce(Return(lost_packets
));
1191 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
1192 QuicPacketSequenceNumber retransmission
;
1193 EXPECT_CALL(*send_algorithm_
,
1194 OnPacketSent(_
, _
, _
, packet_size
- kQuicVersionSize
, _
))
1195 .WillOnce(DoAll(SaveArg
<2>(&retransmission
), Return(true)));
1197 ProcessAckPacket(&frame
);
1199 QuicAckFrame frame2
= InitAckFrame(retransmission
, 1);
1200 NackPacket(original
, &frame2
);
1201 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
1202 EXPECT_CALL(*loss_algorithm_
, DetectLostPackets(_
, _
, _
, _
))
1203 .WillOnce(Return(SequenceNumberSet()));
1204 ProcessAckPacket(&frame2
);
1206 // Now if the peer sends an ack which still reports the retransmitted packet
1207 // as missing, that will bundle an ack with data after two acks in a row
1208 // indicate the high water mark needs to be raised.
1209 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
,
1210 HAS_RETRANSMITTABLE_DATA
));
1211 connection_
.SendStreamDataWithString(3, "foo", 3, !kFin
, NULL
);
1213 EXPECT_EQ(1u, writer_
->frame_count());
1214 EXPECT_EQ(1u, writer_
->stream_frames().size());
1216 // No more packet loss for the rest of the test.
1217 EXPECT_CALL(*loss_algorithm_
, DetectLostPackets(_
, _
, _
, _
))
1218 .WillRepeatedly(Return(SequenceNumberSet()));
1219 ProcessAckPacket(&frame2
);
1220 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
,
1221 HAS_RETRANSMITTABLE_DATA
));
1222 connection_
.SendStreamDataWithString(3, "foo", 3, !kFin
, NULL
);
1224 if (version() > QUIC_VERSION_15
) {
1225 EXPECT_EQ(3u, writer_
->frame_count());
1227 EXPECT_EQ(2u, writer_
->frame_count());
1229 EXPECT_EQ(1u, writer_
->stream_frames().size());
1230 EXPECT_FALSE(writer_
->ack_frames().empty());
1232 // But an ack with no missing packets will not send an ack.
1233 AckPacket(original
, &frame2
);
1234 ProcessAckPacket(&frame2
);
1235 ProcessAckPacket(&frame2
);
1238 TEST_P(QuicConnectionTest
, LeastUnackedLower
) {
1239 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
1241 SendStreamDataToPeer(1, "foo", 0, !kFin
, NULL
);
1242 SendStreamDataToPeer(1, "bar", 3, !kFin
, NULL
);
1243 SendStreamDataToPeer(1, "eep", 6, !kFin
, NULL
);
1245 // Start out saying the least unacked is 2.
1246 peer_creator_
.set_sequence_number(5);
1247 if (version() > QUIC_VERSION_15
) {
1248 QuicStopWaitingFrame frame
= InitStopWaitingFrame(2);
1249 ProcessStopWaitingPacket(&frame
);
1251 QuicAckFrame frame
= InitAckFrame(0, 2);
1252 ProcessAckPacket(&frame
);
1255 // Change it to 1, but lower the sequence number to fake out-of-order packets.
1256 // This should be fine.
1257 peer_creator_
.set_sequence_number(1);
1258 // The scheduler will not process out of order acks, but all packet processing
1259 // causes the connection to try to write.
1260 EXPECT_CALL(visitor_
, OnCanWrite());
1261 if (version() > QUIC_VERSION_15
) {
1262 QuicStopWaitingFrame frame2
= InitStopWaitingFrame(1);
1263 ProcessStopWaitingPacket(&frame2
);
1265 QuicAckFrame frame2
= InitAckFrame(0, 1);
1266 ProcessAckPacket(&frame2
);
1269 // Now claim it's one, but set the ordering so it was sent "after" the first
1270 // one. This should cause a connection error.
1271 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
));
1272 peer_creator_
.set_sequence_number(7);
1273 if (version() > QUIC_VERSION_15
) {
1274 EXPECT_CALL(visitor_
,
1275 OnConnectionClosed(QUIC_INVALID_STOP_WAITING_DATA
, false));
1276 QuicStopWaitingFrame frame2
= InitStopWaitingFrame(1);
1277 ProcessStopWaitingPacket(&frame2
);
1279 EXPECT_CALL(visitor_
, OnConnectionClosed(QUIC_INVALID_ACK_DATA
, false));
1280 QuicAckFrame frame2
= InitAckFrame(0, 1);
1281 ProcessAckPacket(&frame2
);
1285 TEST_P(QuicConnectionTest
, LargestObservedLower
) {
1286 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
1288 SendStreamDataToPeer(1, "foo", 0, !kFin
, NULL
);
1289 SendStreamDataToPeer(1, "bar", 3, !kFin
, NULL
);
1290 SendStreamDataToPeer(1, "eep", 6, !kFin
, NULL
);
1291 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
1293 // Start out saying the largest observed is 2.
1294 QuicAckFrame frame1
= InitAckFrame(1, 0);
1295 QuicAckFrame frame2
= InitAckFrame(2, 0);
1296 ProcessAckPacket(&frame2
);
1298 // Now change it to 1, and it should cause a connection error.
1299 EXPECT_CALL(visitor_
, OnConnectionClosed(QUIC_INVALID_ACK_DATA
, false));
1300 EXPECT_CALL(visitor_
, OnCanWrite()).Times(0);
1301 ProcessAckPacket(&frame1
);
1304 TEST_P(QuicConnectionTest
, AckUnsentData
) {
1305 // Ack a packet which has not been sent.
1306 EXPECT_CALL(visitor_
, OnConnectionClosed(QUIC_INVALID_ACK_DATA
, false));
1307 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
1308 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
));
1309 QuicAckFrame
frame(MakeAckFrame(1, 0));
1310 EXPECT_CALL(visitor_
, OnCanWrite()).Times(0);
1311 ProcessAckPacket(&frame
);
1314 TEST_P(QuicConnectionTest
, AckAll
) {
1315 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
1318 peer_creator_
.set_sequence_number(1);
1319 QuicAckFrame frame1
= InitAckFrame(0, 1);
1320 ProcessAckPacket(&frame1
);
1323 TEST_P(QuicConnectionTest
, SendingDifferentSequenceNumberLengthsBandwidth
) {
1324 QuicPacketSequenceNumber last_packet
;
1325 QuicPacketCreator
* creator
=
1326 QuicConnectionPeer::GetPacketCreator(&connection_
);
1327 SendStreamDataToPeer(1, "foo", 0, !kFin
, &last_packet
);
1328 EXPECT_EQ(1u, last_packet
);
1329 EXPECT_EQ(PACKET_1BYTE_SEQUENCE_NUMBER
,
1330 creator
->next_sequence_number_length());
1331 EXPECT_EQ(PACKET_1BYTE_SEQUENCE_NUMBER
,
1332 writer_
->header().public_header
.sequence_number_length
);
1334 EXPECT_CALL(*send_algorithm_
, GetCongestionWindow()).WillRepeatedly(
1335 Return(kMaxPacketSize
* 256));
1337 SendStreamDataToPeer(1, "bar", 3, !kFin
, &last_packet
);
1338 EXPECT_EQ(2u, last_packet
);
1339 EXPECT_EQ(PACKET_2BYTE_SEQUENCE_NUMBER
,
1340 creator
->next_sequence_number_length());
1341 // The 1 packet lag is due to the sequence number length being recalculated in
1342 // QuicConnection after a packet is sent.
1343 EXPECT_EQ(PACKET_1BYTE_SEQUENCE_NUMBER
,
1344 writer_
->header().public_header
.sequence_number_length
);
1346 EXPECT_CALL(*send_algorithm_
, GetCongestionWindow()).WillRepeatedly(
1347 Return(kMaxPacketSize
* 256 * 256));
1349 SendStreamDataToPeer(1, "foo", 6, !kFin
, &last_packet
);
1350 EXPECT_EQ(3u, last_packet
);
1351 EXPECT_EQ(PACKET_4BYTE_SEQUENCE_NUMBER
,
1352 creator
->next_sequence_number_length());
1353 EXPECT_EQ(PACKET_2BYTE_SEQUENCE_NUMBER
,
1354 writer_
->header().public_header
.sequence_number_length
);
1356 EXPECT_CALL(*send_algorithm_
, GetCongestionWindow()).WillRepeatedly(
1357 Return(kMaxPacketSize
* 256 * 256 * 256));
1359 SendStreamDataToPeer(1, "bar", 9, !kFin
, &last_packet
);
1360 EXPECT_EQ(4u, last_packet
);
1361 EXPECT_EQ(PACKET_4BYTE_SEQUENCE_NUMBER
,
1362 creator
->next_sequence_number_length());
1363 EXPECT_EQ(PACKET_4BYTE_SEQUENCE_NUMBER
,
1364 writer_
->header().public_header
.sequence_number_length
);
1366 EXPECT_CALL(*send_algorithm_
, GetCongestionWindow()).WillRepeatedly(
1367 Return(kMaxPacketSize
* 256 * 256 * 256 * 256));
1369 SendStreamDataToPeer(1, "foo", 12, !kFin
, &last_packet
);
1370 EXPECT_EQ(5u, last_packet
);
1371 EXPECT_EQ(PACKET_6BYTE_SEQUENCE_NUMBER
,
1372 creator
->next_sequence_number_length());
1373 EXPECT_EQ(PACKET_4BYTE_SEQUENCE_NUMBER
,
1374 writer_
->header().public_header
.sequence_number_length
);
1377 TEST_P(QuicConnectionTest
, SendingDifferentSequenceNumberLengthsUnackedDelta
) {
1378 QuicPacketSequenceNumber last_packet
;
1379 QuicPacketCreator
* creator
=
1380 QuicConnectionPeer::GetPacketCreator(&connection_
);
1381 SendStreamDataToPeer(1, "foo", 0, !kFin
, &last_packet
);
1382 EXPECT_EQ(1u, last_packet
);
1383 EXPECT_EQ(PACKET_1BYTE_SEQUENCE_NUMBER
,
1384 creator
->next_sequence_number_length());
1385 EXPECT_EQ(PACKET_1BYTE_SEQUENCE_NUMBER
,
1386 writer_
->header().public_header
.sequence_number_length
);
1388 creator
->set_sequence_number(100);
1390 SendStreamDataToPeer(1, "bar", 3, !kFin
, &last_packet
);
1391 EXPECT_EQ(PACKET_2BYTE_SEQUENCE_NUMBER
,
1392 creator
->next_sequence_number_length());
1393 EXPECT_EQ(PACKET_1BYTE_SEQUENCE_NUMBER
,
1394 writer_
->header().public_header
.sequence_number_length
);
1396 creator
->set_sequence_number(100 * 256);
1398 SendStreamDataToPeer(1, "foo", 6, !kFin
, &last_packet
);
1399 EXPECT_EQ(PACKET_4BYTE_SEQUENCE_NUMBER
,
1400 creator
->next_sequence_number_length());
1401 EXPECT_EQ(PACKET_2BYTE_SEQUENCE_NUMBER
,
1402 writer_
->header().public_header
.sequence_number_length
);
1404 creator
->set_sequence_number(100 * 256 * 256);
1406 SendStreamDataToPeer(1, "bar", 9, !kFin
, &last_packet
);
1407 EXPECT_EQ(PACKET_4BYTE_SEQUENCE_NUMBER
,
1408 creator
->next_sequence_number_length());
1409 EXPECT_EQ(PACKET_4BYTE_SEQUENCE_NUMBER
,
1410 writer_
->header().public_header
.sequence_number_length
);
1412 creator
->set_sequence_number(100 * 256 * 256 * 256);
1414 SendStreamDataToPeer(1, "foo", 12, !kFin
, &last_packet
);
1415 EXPECT_EQ(PACKET_6BYTE_SEQUENCE_NUMBER
,
1416 creator
->next_sequence_number_length());
1417 EXPECT_EQ(PACKET_4BYTE_SEQUENCE_NUMBER
,
1418 writer_
->header().public_header
.sequence_number_length
);
1421 TEST_P(QuicConnectionTest
, BasicSending
) {
1422 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
1423 QuicPacketSequenceNumber last_packet
;
1424 SendStreamDataToPeer(1, "foo", 0, !kFin
, &last_packet
); // Packet 1
1425 EXPECT_EQ(1u, last_packet
);
1426 SendAckPacketToPeer(); // Packet 2
1428 EXPECT_EQ(1u, least_unacked());
1430 SendAckPacketToPeer(); // Packet 3
1431 EXPECT_EQ(1u, least_unacked());
1433 SendStreamDataToPeer(1, "bar", 3, !kFin
, &last_packet
); // Packet 4
1434 EXPECT_EQ(4u, last_packet
);
1435 SendAckPacketToPeer(); // Packet 5
1436 EXPECT_EQ(1u, least_unacked());
1438 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
1440 // Peer acks up to packet 3.
1441 QuicAckFrame frame
= InitAckFrame(3, 0);
1442 ProcessAckPacket(&frame
);
1443 SendAckPacketToPeer(); // Packet 6
1445 // As soon as we've acked one, we skip ack packets 2 and 3 and note lack of
1447 EXPECT_EQ(4u, least_unacked());
1449 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
1451 // Peer acks up to packet 4, the last packet.
1452 QuicAckFrame frame2
= InitAckFrame(6, 0);
1453 ProcessAckPacket(&frame2
); // Acks don't instigate acks.
1455 // Verify that we did not send an ack.
1456 EXPECT_EQ(6u, writer_
->header().packet_sequence_number
);
1458 // So the last ack has not changed.
1459 EXPECT_EQ(4u, least_unacked());
1461 // If we force an ack, we shouldn't change our retransmit state.
1462 SendAckPacketToPeer(); // Packet 7
1463 EXPECT_EQ(7u, least_unacked());
1465 // But if we send more data it should.
1466 SendStreamDataToPeer(1, "eep", 6, !kFin
, &last_packet
); // Packet 8
1467 EXPECT_EQ(8u, last_packet
);
1468 SendAckPacketToPeer(); // Packet 9
1469 EXPECT_EQ(7u, least_unacked());
1472 TEST_P(QuicConnectionTest
, FECSending
) {
1473 // All packets carry version info till version is negotiated.
1474 QuicPacketCreator
* creator
=
1475 QuicConnectionPeer::GetPacketCreator(&connection_
);
1476 size_t payload_length
;
1477 // GetPacketLengthForOneStream() assumes a stream offset of 0 in determining
1478 // packet length. The size of the offset field in a stream frame is 0 for
1479 // offset 0, and 2 for non-zero offsets up through 64K. Increase
1480 // max_packet_length by 2 so that subsequent packets containing subsequent
1481 // stream frames with non-zero offets will fit within the packet length.
1482 size_t length
= 2 + GetPacketLengthForOneStream(
1483 connection_
.version(), kIncludeVersion
, PACKET_1BYTE_SEQUENCE_NUMBER
,
1484 IN_FEC_GROUP
, &payload_length
);
1485 creator
->set_max_packet_length(length
);
1487 // Send 4 protected data packets, which should also trigger 1 FEC packet.
1488 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
)).Times(5);
1489 // The first stream frame will have 2 fewer overhead bytes than the other 3.
1490 const string
payload(payload_length
* 4 + 2, 'a');
1491 connection_
.SendStreamDataWithStringWithFec(1, payload
, 0, !kFin
, NULL
);
1492 // Expect the FEC group to be closed after SendStreamDataWithString.
1493 EXPECT_FALSE(creator
->IsFecGroupOpen());
1494 EXPECT_FALSE(creator
->IsFecProtected());
1497 TEST_P(QuicConnectionTest
, FECQueueing
) {
1498 // All packets carry version info till version is negotiated.
1499 size_t payload_length
;
1500 QuicPacketCreator
* creator
=
1501 QuicConnectionPeer::GetPacketCreator(&connection_
);
1502 size_t length
= GetPacketLengthForOneStream(
1503 connection_
.version(), kIncludeVersion
, PACKET_1BYTE_SEQUENCE_NUMBER
,
1504 IN_FEC_GROUP
, &payload_length
);
1505 creator
->set_max_packet_length(length
);
1506 EXPECT_TRUE(creator
->IsFecEnabled());
1508 EXPECT_EQ(0u, connection_
.NumQueuedPackets());
1510 const string
payload(payload_length
, 'a');
1511 connection_
.SendStreamDataWithStringWithFec(1, payload
, 0, !kFin
, NULL
);
1512 EXPECT_FALSE(creator
->IsFecGroupOpen());
1513 EXPECT_FALSE(creator
->IsFecProtected());
1514 // Expect the first data packet and the fec packet to be queued.
1515 EXPECT_EQ(2u, connection_
.NumQueuedPackets());
1518 TEST_P(QuicConnectionTest
, AbandonFECFromCongestionWindow
) {
1519 EXPECT_TRUE(QuicConnectionPeer::GetPacketCreator(
1520 &connection_
)->IsFecEnabled());
1522 // 1 Data and 1 FEC packet.
1523 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
)).Times(2);
1524 connection_
.SendStreamDataWithStringWithFec(3, "foo", 0, !kFin
, NULL
);
1526 const QuicTime::Delta retransmission_time
=
1527 QuicTime::Delta::FromMilliseconds(5000);
1528 clock_
.AdvanceTime(retransmission_time
);
1530 // Abandon FEC packet and data packet.
1531 EXPECT_CALL(*send_algorithm_
, OnRetransmissionTimeout(true));
1532 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
)).Times(1);
1533 EXPECT_CALL(visitor_
, OnCanWrite());
1534 connection_
.OnRetransmissionTimeout();
1537 TEST_P(QuicConnectionTest
, DontAbandonAckedFEC
) {
1538 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
1539 EXPECT_TRUE(QuicConnectionPeer::GetPacketCreator(
1540 &connection_
)->IsFecEnabled());
1542 // 1 Data and 1 FEC packet.
1543 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
)).Times(6);
1544 connection_
.SendStreamDataWithStringWithFec(3, "foo", 0, !kFin
, NULL
);
1545 // Send some more data afterwards to ensure early retransmit doesn't trigger.
1546 connection_
.SendStreamDataWithStringWithFec(3, "foo", 3, !kFin
, NULL
);
1547 connection_
.SendStreamDataWithStringWithFec(3, "foo", 6, !kFin
, NULL
);
1549 QuicAckFrame ack_fec
= InitAckFrame(2, 1);
1550 // Data packet missing.
1551 // TODO(ianswett): Note that this is not a sensible ack, since if the FEC was
1552 // received, it would cause the covered packet to be acked as well.
1553 NackPacket(1, &ack_fec
);
1554 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
1555 ProcessAckPacket(&ack_fec
);
1556 clock_
.AdvanceTime(DefaultRetransmissionTime());
1558 // Don't abandon the acked FEC packet, but it will abandon 2 the subsequent
1560 EXPECT_CALL(*send_algorithm_
, OnRetransmissionTimeout(true));
1561 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
)).Times(3);
1562 connection_
.GetRetransmissionAlarm()->Fire();
1565 TEST_P(QuicConnectionTest
, AbandonAllFEC
) {
1566 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
1567 EXPECT_TRUE(QuicConnectionPeer::GetPacketCreator(
1568 &connection_
)->IsFecEnabled());
1570 // 1 Data and 1 FEC packet.
1571 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
)).Times(6);
1572 connection_
.SendStreamDataWithStringWithFec(3, "foo", 0, !kFin
, NULL
);
1573 // Send some more data afterwards to ensure early retransmit doesn't trigger.
1574 connection_
.SendStreamDataWithStringWithFec(3, "foo", 3, !kFin
, NULL
);
1575 // Advance the time so not all the FEC packets are abandoned.
1576 clock_
.AdvanceTime(QuicTime::Delta::FromMilliseconds(1));
1577 connection_
.SendStreamDataWithStringWithFec(3, "foo", 6, !kFin
, NULL
);
1579 QuicAckFrame ack_fec
= InitAckFrame(5, 1);
1580 // Ack all data packets, but no fec packets.
1581 NackPacket(2, &ack_fec
);
1582 NackPacket(4, &ack_fec
);
1584 // Lose the first FEC packet and ack the three data packets.
1585 SequenceNumberSet lost_packets
;
1586 lost_packets
.insert(2);
1587 EXPECT_CALL(*loss_algorithm_
, DetectLostPackets(_
, _
, _
, _
))
1588 .WillOnce(Return(lost_packets
));
1589 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
1590 ProcessAckPacket(&ack_fec
);
1592 clock_
.AdvanceTime(DefaultRetransmissionTime().Subtract(
1593 QuicTime::Delta::FromMilliseconds(1)));
1595 // Abandon all packets
1596 EXPECT_CALL(*send_algorithm_
, OnRetransmissionTimeout(false));
1597 connection_
.GetRetransmissionAlarm()->Fire();
1599 // Ensure the alarm is not set since all packets have been abandoned.
1600 EXPECT_FALSE(connection_
.GetRetransmissionAlarm()->IsSet());
1603 TEST_P(QuicConnectionTest
, FramePacking
) {
1604 CongestionBlockWrites();
1606 // Send an ack and two stream frames in 1 packet by queueing them.
1607 connection_
.SendAck();
1608 EXPECT_CALL(visitor_
, OnCanWrite()).WillOnce(DoAll(
1609 IgnoreResult(InvokeWithoutArgs(&connection_
,
1610 &TestConnection::SendStreamData3
)),
1611 IgnoreResult(InvokeWithoutArgs(&connection_
,
1612 &TestConnection::SendStreamData5
))));
1614 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
)).Times(1);
1615 CongestionUnblockWrites();
1616 connection_
.GetSendAlarm()->Fire();
1617 EXPECT_EQ(0u, connection_
.NumQueuedPackets());
1618 EXPECT_FALSE(connection_
.HasQueuedData());
1620 // Parse the last packet and ensure it's an ack and two stream frames from
1621 // two different streams.
1622 if (version() > QUIC_VERSION_15
) {
1623 EXPECT_EQ(4u, writer_
->frame_count());
1624 EXPECT_FALSE(writer_
->stop_waiting_frames().empty());
1626 EXPECT_EQ(3u, writer_
->frame_count());
1628 EXPECT_FALSE(writer_
->ack_frames().empty());
1629 ASSERT_EQ(2u, writer_
->stream_frames().size());
1630 EXPECT_EQ(kClientDataStreamId1
, writer_
->stream_frames()[0].stream_id
);
1631 EXPECT_EQ(kClientDataStreamId2
, writer_
->stream_frames()[1].stream_id
);
1634 TEST_P(QuicConnectionTest
, FramePackingNonCryptoThenCrypto
) {
1635 CongestionBlockWrites();
1637 // Send an ack and two stream frames (one non-crypto, then one crypto) in 2
1638 // packets by queueing them.
1639 connection_
.SendAck();
1640 EXPECT_CALL(visitor_
, OnCanWrite()).WillOnce(DoAll(
1641 IgnoreResult(InvokeWithoutArgs(&connection_
,
1642 &TestConnection::SendStreamData3
)),
1643 IgnoreResult(InvokeWithoutArgs(&connection_
,
1644 &TestConnection::SendCryptoStreamData
))));
1646 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
)).Times(2);
1647 CongestionUnblockWrites();
1648 connection_
.GetSendAlarm()->Fire();
1649 EXPECT_EQ(0u, connection_
.NumQueuedPackets());
1650 EXPECT_FALSE(connection_
.HasQueuedData());
1652 // Parse the last packet and ensure it's the crypto stream frame.
1653 EXPECT_EQ(1u, writer_
->frame_count());
1654 ASSERT_EQ(1u, writer_
->stream_frames().size());
1655 EXPECT_EQ(kCryptoStreamId
, writer_
->stream_frames()[0].stream_id
);
1658 TEST_P(QuicConnectionTest
, FramePackingCryptoThenNonCrypto
) {
1659 CongestionBlockWrites();
1661 // Send an ack and two stream frames (one crypto, then one non-crypto) in 2
1662 // packets by queueing them.
1663 connection_
.SendAck();
1664 EXPECT_CALL(visitor_
, OnCanWrite()).WillOnce(DoAll(
1665 IgnoreResult(InvokeWithoutArgs(&connection_
,
1666 &TestConnection::SendCryptoStreamData
)),
1667 IgnoreResult(InvokeWithoutArgs(&connection_
,
1668 &TestConnection::SendStreamData3
))));
1670 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
)).Times(2);
1671 CongestionUnblockWrites();
1672 connection_
.GetSendAlarm()->Fire();
1673 EXPECT_EQ(0u, connection_
.NumQueuedPackets());
1674 EXPECT_FALSE(connection_
.HasQueuedData());
1676 // Parse the last packet and ensure it's the stream frame from stream 3.
1677 EXPECT_EQ(1u, writer_
->frame_count());
1678 ASSERT_EQ(1u, writer_
->stream_frames().size());
1679 EXPECT_EQ(kClientDataStreamId1
, writer_
->stream_frames()[0].stream_id
);
1682 TEST_P(QuicConnectionTest
, FramePackingFEC
) {
1683 EXPECT_TRUE(QuicConnectionPeer::GetPacketCreator(
1684 &connection_
)->IsFecEnabled());
1686 CongestionBlockWrites();
1688 // Queue an ack and two stream frames. Ack gets flushed when FEC is turned on
1689 // for sending protected data; two stream frames are packing in 1 packet.
1690 EXPECT_CALL(visitor_
, OnCanWrite()).WillOnce(DoAll(
1691 IgnoreResult(InvokeWithoutArgs(
1692 &connection_
, &TestConnection::SendStreamData3WithFec
)),
1693 IgnoreResult(InvokeWithoutArgs(
1694 &connection_
, &TestConnection::SendStreamData5WithFec
))));
1695 connection_
.SendAck();
1697 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
)).Times(3);
1698 CongestionUnblockWrites();
1699 connection_
.GetSendAlarm()->Fire();
1700 EXPECT_EQ(0u, connection_
.NumQueuedPackets());
1701 EXPECT_FALSE(connection_
.HasQueuedData());
1703 // Parse the last packet and ensure it's in an fec group.
1704 EXPECT_EQ(2u, writer_
->header().fec_group
);
1705 EXPECT_EQ(0u, writer_
->frame_count());
1708 TEST_P(QuicConnectionTest
, FramePackingAckResponse
) {
1709 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
1710 // Process a data packet to queue up a pending ack.
1711 EXPECT_CALL(visitor_
, OnStreamFrames(_
)).Times(1);
1712 ProcessDataPacket(1, 1, kEntropyFlag
);
1714 EXPECT_CALL(visitor_
, OnCanWrite()).WillOnce(DoAll(
1715 IgnoreResult(InvokeWithoutArgs(&connection_
,
1716 &TestConnection::SendStreamData3
)),
1717 IgnoreResult(InvokeWithoutArgs(&connection_
,
1718 &TestConnection::SendStreamData5
))));
1720 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
)).Times(1);
1722 // Process an ack to cause the visitor's OnCanWrite to be invoked.
1723 peer_creator_
.set_sequence_number(2);
1724 QuicAckFrame ack_one
= InitAckFrame(0, 0);
1725 ProcessAckPacket(&ack_one
);
1727 EXPECT_EQ(0u, connection_
.NumQueuedPackets());
1728 EXPECT_FALSE(connection_
.HasQueuedData());
1730 // Parse the last packet and ensure it's an ack and two stream frames from
1731 // two different streams.
1732 if (version() > QUIC_VERSION_15
) {
1733 EXPECT_EQ(4u, writer_
->frame_count());
1734 EXPECT_FALSE(writer_
->stop_waiting_frames().empty());
1736 EXPECT_EQ(3u, writer_
->frame_count());
1738 EXPECT_FALSE(writer_
->ack_frames().empty());
1739 ASSERT_EQ(2u, writer_
->stream_frames().size());
1740 EXPECT_EQ(kClientDataStreamId1
, writer_
->stream_frames()[0].stream_id
);
1741 EXPECT_EQ(kClientDataStreamId2
, writer_
->stream_frames()[1].stream_id
);
1744 TEST_P(QuicConnectionTest
, FramePackingSendv
) {
1745 // Send data in 1 packet by writing multiple blocks in a single iovector
1747 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
));
1749 char data
[] = "ABCD";
1751 data_iov
.AppendNoCoalesce(data
, 2);
1752 data_iov
.AppendNoCoalesce(data
+ 2, 2);
1753 connection_
.SendStreamData(1, data_iov
, 0, !kFin
, MAY_FEC_PROTECT
, NULL
);
1755 EXPECT_EQ(0u, connection_
.NumQueuedPackets());
1756 EXPECT_FALSE(connection_
.HasQueuedData());
1758 // Parse the last packet and ensure multiple iovector blocks have
1759 // been packed into a single stream frame from one stream.
1760 EXPECT_EQ(1u, writer_
->frame_count());
1761 EXPECT_EQ(1u, writer_
->stream_frames().size());
1762 QuicStreamFrame frame
= writer_
->stream_frames()[0];
1763 EXPECT_EQ(1u, frame
.stream_id
);
1764 EXPECT_EQ("ABCD", string(static_cast<char*>
1765 (frame
.data
.iovec()[0].iov_base
),
1766 (frame
.data
.iovec()[0].iov_len
)));
1769 TEST_P(QuicConnectionTest
, FramePackingSendvQueued
) {
1770 // Try to send two stream frames in 1 packet by using writev.
1771 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
));
1774 char data
[] = "ABCD";
1776 data_iov
.AppendNoCoalesce(data
, 2);
1777 data_iov
.AppendNoCoalesce(data
+ 2, 2);
1778 connection_
.SendStreamData(1, data_iov
, 0, !kFin
, MAY_FEC_PROTECT
, NULL
);
1780 EXPECT_EQ(1u, connection_
.NumQueuedPackets());
1781 EXPECT_TRUE(connection_
.HasQueuedData());
1783 // Unblock the writes and actually send.
1784 writer_
->SetWritable();
1785 connection_
.OnCanWrite();
1786 EXPECT_EQ(0u, connection_
.NumQueuedPackets());
1788 // Parse the last packet and ensure it's one stream frame from one stream.
1789 EXPECT_EQ(1u, writer_
->frame_count());
1790 EXPECT_EQ(1u, writer_
->stream_frames().size());
1791 EXPECT_EQ(1u, writer_
->stream_frames()[0].stream_id
);
1794 TEST_P(QuicConnectionTest
, SendingZeroBytes
) {
1795 // Send a zero byte write with a fin using writev.
1796 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
));
1798 connection_
.SendStreamData(1, empty_iov
, 0, kFin
, MAY_FEC_PROTECT
, NULL
);
1800 EXPECT_EQ(0u, connection_
.NumQueuedPackets());
1801 EXPECT_FALSE(connection_
.HasQueuedData());
1803 // Parse the last packet and ensure it's one stream frame from one stream.
1804 EXPECT_EQ(1u, writer_
->frame_count());
1805 EXPECT_EQ(1u, writer_
->stream_frames().size());
1806 EXPECT_EQ(1u, writer_
->stream_frames()[0].stream_id
);
1807 EXPECT_TRUE(writer_
->stream_frames()[0].fin
);
1810 TEST_P(QuicConnectionTest
, OnCanWrite
) {
1811 // Visitor's OnCanWrite will send data, but will have more pending writes.
1812 EXPECT_CALL(visitor_
, OnCanWrite()).WillOnce(DoAll(
1813 IgnoreResult(InvokeWithoutArgs(&connection_
,
1814 &TestConnection::SendStreamData3
)),
1815 IgnoreResult(InvokeWithoutArgs(&connection_
,
1816 &TestConnection::SendStreamData5
))));
1817 EXPECT_CALL(visitor_
, WillingAndAbleToWrite()).WillOnce(Return(true));
1818 EXPECT_CALL(*send_algorithm_
,
1819 TimeUntilSend(_
, _
, _
)).WillRepeatedly(
1820 testing::Return(QuicTime::Delta::Zero()));
1822 connection_
.OnCanWrite();
1824 // Parse the last packet and ensure it's the two stream frames from
1825 // two different streams.
1826 EXPECT_EQ(2u, writer_
->frame_count());
1827 EXPECT_EQ(2u, writer_
->stream_frames().size());
1828 EXPECT_EQ(kClientDataStreamId1
, writer_
->stream_frames()[0].stream_id
);
1829 EXPECT_EQ(kClientDataStreamId2
, writer_
->stream_frames()[1].stream_id
);
1832 TEST_P(QuicConnectionTest
, RetransmitOnNack
) {
1833 QuicPacketSequenceNumber last_packet
;
1834 QuicByteCount second_packet_size
;
1835 SendStreamDataToPeer(3, "foo", 0, !kFin
, &last_packet
); // Packet 1
1836 second_packet_size
=
1837 SendStreamDataToPeer(3, "foos", 3, !kFin
, &last_packet
); // Packet 2
1838 SendStreamDataToPeer(3, "fooos", 7, !kFin
, &last_packet
); // Packet 3
1840 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
1842 // Don't lose a packet on an ack, and nothing is retransmitted.
1843 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
1844 QuicAckFrame ack_one
= InitAckFrame(1, 0);
1845 ProcessAckPacket(&ack_one
);
1847 // Lose a packet and ensure it triggers retransmission.
1848 QuicAckFrame nack_two
= InitAckFrame(3, 0);
1849 NackPacket(2, &nack_two
);
1850 SequenceNumberSet lost_packets
;
1851 lost_packets
.insert(2);
1852 EXPECT_CALL(*loss_algorithm_
, DetectLostPackets(_
, _
, _
, _
))
1853 .WillOnce(Return(lost_packets
));
1854 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
1855 EXPECT_CALL(*send_algorithm_
,
1856 OnPacketSent(_
, _
, _
, second_packet_size
- kQuicVersionSize
, _
)).
1858 ProcessAckPacket(&nack_two
);
1861 TEST_P(QuicConnectionTest
, DiscardRetransmit
) {
1862 QuicPacketSequenceNumber last_packet
;
1863 SendStreamDataToPeer(1, "foo", 0, !kFin
, &last_packet
); // Packet 1
1864 SendStreamDataToPeer(1, "foos", 3, !kFin
, &last_packet
); // Packet 2
1865 SendStreamDataToPeer(1, "fooos", 7, !kFin
, &last_packet
); // Packet 3
1867 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
1869 // Instigate a loss with an ack.
1870 QuicAckFrame nack_two
= InitAckFrame(3, 0);
1871 NackPacket(2, &nack_two
);
1872 // The first nack should trigger a fast retransmission, but we'll be
1873 // write blocked, so the packet will be queued.
1875 SequenceNumberSet lost_packets
;
1876 lost_packets
.insert(2);
1877 EXPECT_CALL(*loss_algorithm_
, DetectLostPackets(_
, _
, _
, _
))
1878 .WillOnce(Return(lost_packets
));
1879 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
1880 ProcessAckPacket(&nack_two
);
1881 EXPECT_EQ(1u, connection_
.NumQueuedPackets());
1883 // Now, ack the previous transmission.
1884 EXPECT_CALL(*loss_algorithm_
, DetectLostPackets(_
, _
, _
, _
))
1885 .WillOnce(Return(SequenceNumberSet()));
1886 QuicAckFrame ack_all
= InitAckFrame(3, 0);
1887 ProcessAckPacket(&ack_all
);
1889 // Unblock the socket and attempt to send the queued packets. However,
1890 // since the previous transmission has been acked, we will not
1891 // send the retransmission.
1892 EXPECT_CALL(*send_algorithm_
,
1893 OnPacketSent(_
, _
, _
, _
, _
)).Times(0);
1895 writer_
->SetWritable();
1896 connection_
.OnCanWrite();
1898 EXPECT_EQ(0u, connection_
.NumQueuedPackets());
1901 TEST_P(QuicConnectionTest
, RetransmitNackedLargestObserved
) {
1902 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
1903 QuicPacketSequenceNumber largest_observed
;
1904 QuicByteCount packet_size
;
1905 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
))
1906 .WillOnce(DoAll(SaveArg
<2>(&largest_observed
), SaveArg
<3>(&packet_size
),
1908 connection_
.SendStreamDataWithString(3, "foo", 0, !kFin
, NULL
);
1910 QuicAckFrame frame
= InitAckFrame(1, largest_observed
);
1911 NackPacket(largest_observed
, &frame
);
1912 // The first nack should retransmit the largest observed packet.
1913 SequenceNumberSet lost_packets
;
1914 lost_packets
.insert(1);
1915 EXPECT_CALL(*loss_algorithm_
, DetectLostPackets(_
, _
, _
, _
))
1916 .WillOnce(Return(lost_packets
));
1917 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
1918 EXPECT_CALL(*send_algorithm_
,
1919 OnPacketSent(_
, _
, _
, packet_size
- kQuicVersionSize
, _
));
1920 ProcessAckPacket(&frame
);
1923 TEST_P(QuicConnectionTest
, QueueAfterTwoRTOs
) {
1924 for (int i
= 0; i
< 10; ++i
) {
1925 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
)).Times(1);
1926 connection_
.SendStreamDataWithString(3, "foo", i
* 3, !kFin
, NULL
);
1929 // Block the congestion window and ensure they're queued.
1931 clock_
.AdvanceTime(DefaultRetransmissionTime());
1932 // Only one packet should be retransmitted.
1933 EXPECT_CALL(*send_algorithm_
, OnRetransmissionTimeout(true));
1934 connection_
.GetRetransmissionAlarm()->Fire();
1935 EXPECT_TRUE(connection_
.HasQueuedData());
1937 // Unblock the congestion window.
1938 writer_
->SetWritable();
1939 clock_
.AdvanceTime(QuicTime::Delta::FromMicroseconds(
1940 2 * DefaultRetransmissionTime().ToMicroseconds()));
1941 // Retransmit already retransmitted packets event though the sequence number
1942 // greater than the largest observed.
1943 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
)).Times(10);
1944 connection_
.GetRetransmissionAlarm()->Fire();
1945 connection_
.OnCanWrite();
1948 TEST_P(QuicConnectionTest
, WriteBlockedThenSent
) {
1950 writer_
->set_is_write_blocked_data_buffered(true);
1951 connection_
.SendStreamDataWithString(1, "foo", 0, !kFin
, NULL
);
1952 EXPECT_FALSE(connection_
.GetRetransmissionAlarm()->IsSet());
1954 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
)).Times(1);
1955 connection_
.OnPacketSent(WriteResult(WRITE_STATUS_OK
, 0));
1956 EXPECT_TRUE(connection_
.GetRetransmissionAlarm()->IsSet());
1959 TEST_P(QuicConnectionTest
, WriteBlockedAckedThenSent
) {
1960 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
1962 writer_
->set_is_write_blocked_data_buffered(true);
1963 connection_
.SendStreamDataWithString(1, "foo", 0, !kFin
, NULL
);
1964 EXPECT_FALSE(connection_
.GetRetransmissionAlarm()->IsSet());
1966 // Ack the sent packet before the callback returns, which happens in
1967 // rare circumstances with write blocked sockets.
1968 QuicAckFrame ack
= InitAckFrame(1, 0);
1969 ProcessAckPacket(&ack
);
1971 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
)).Times(0);
1972 connection_
.OnPacketSent(WriteResult(WRITE_STATUS_OK
, 0));
1973 EXPECT_FALSE(connection_
.GetRetransmissionAlarm()->IsSet());
1976 TEST_P(QuicConnectionTest
, RetransmitWriteBlockedAckedOriginalThenSent
) {
1977 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
1978 connection_
.SendStreamDataWithString(3, "foo", 0, !kFin
, NULL
);
1979 EXPECT_TRUE(connection_
.GetRetransmissionAlarm()->IsSet());
1982 writer_
->set_is_write_blocked_data_buffered(true);
1983 // Simulate the retransmission alarm firing.
1984 EXPECT_CALL(*send_algorithm_
, OnRetransmissionTimeout(_
));
1985 clock_
.AdvanceTime(DefaultRetransmissionTime());
1986 connection_
.GetRetransmissionAlarm()->Fire();
1988 // Ack the sent packet before the callback returns, which happens in
1989 // rare circumstances with write blocked sockets.
1990 QuicAckFrame ack
= InitAckFrame(1, 0);
1991 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
1992 EXPECT_CALL(*send_algorithm_
, RevertRetransmissionTimeout());
1993 ProcessAckPacket(&ack
);
1995 connection_
.OnPacketSent(WriteResult(WRITE_STATUS_OK
, 0));
1996 // There is now a pending packet, but with no retransmittable frames.
1997 EXPECT_TRUE(connection_
.GetRetransmissionAlarm()->IsSet());
1998 EXPECT_FALSE(connection_
.sent_packet_manager().HasRetransmittableFrames(2));
2001 TEST_P(QuicConnectionTest
, AlarmsWhenWriteBlocked
) {
2002 // Block the connection.
2004 connection_
.SendStreamDataWithString(3, "foo", 0, !kFin
, NULL
);
2005 EXPECT_EQ(1u, writer_
->packets_write_attempts());
2006 EXPECT_TRUE(writer_
->IsWriteBlocked());
2008 // Set the send and resumption alarms. Fire the alarms and ensure they don't
2009 // attempt to write.
2010 connection_
.GetResumeWritesAlarm()->Set(clock_
.ApproximateNow());
2011 connection_
.GetSendAlarm()->Set(clock_
.ApproximateNow());
2012 connection_
.GetResumeWritesAlarm()->Fire();
2013 connection_
.GetSendAlarm()->Fire();
2014 EXPECT_TRUE(writer_
->IsWriteBlocked());
2015 EXPECT_EQ(1u, writer_
->packets_write_attempts());
2018 TEST_P(QuicConnectionTest
, NoLimitPacketsPerNack
) {
2019 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
2021 // Send packets 1 to 15.
2022 for (int i
= 0; i
< 15; ++i
) {
2023 SendStreamDataToPeer(1, "foo", offset
, !kFin
, NULL
);
2027 // Ack 15, nack 1-14.
2028 SequenceNumberSet lost_packets
;
2029 QuicAckFrame nack
= InitAckFrame(15, 0);
2030 for (int i
= 1; i
< 15; ++i
) {
2031 NackPacket(i
, &nack
);
2032 lost_packets
.insert(i
);
2035 // 14 packets have been NACK'd and lost. In TCP cubic, PRR limits
2036 // the retransmission rate in the case of burst losses.
2037 EXPECT_CALL(*loss_algorithm_
, DetectLostPackets(_
, _
, _
, _
))
2038 .WillOnce(Return(lost_packets
));
2039 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
2040 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
)).Times(14);
2041 ProcessAckPacket(&nack
);
2044 // Test sending multiple acks from the connection to the session.
2045 TEST_P(QuicConnectionTest
, MultipleAcks
) {
2046 QuicPacketSequenceNumber last_packet
;
2047 SendStreamDataToPeer(1, "foo", 0, !kFin
, &last_packet
); // Packet 1
2048 EXPECT_EQ(1u, last_packet
);
2049 SendStreamDataToPeer(3, "foo", 0, !kFin
, &last_packet
); // Packet 2
2050 EXPECT_EQ(2u, last_packet
);
2051 SendAckPacketToPeer(); // Packet 3
2052 SendStreamDataToPeer(5, "foo", 0, !kFin
, &last_packet
); // Packet 4
2053 EXPECT_EQ(4u, last_packet
);
2054 SendStreamDataToPeer(1, "foo", 3, !kFin
, &last_packet
); // Packet 5
2055 EXPECT_EQ(5u, last_packet
);
2056 SendStreamDataToPeer(3, "foo", 3, !kFin
, &last_packet
); // Packet 6
2057 EXPECT_EQ(6u, last_packet
);
2059 // Client will ack packets 1, 2, [!3], 4, 5.
2060 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
2061 QuicAckFrame frame1
= InitAckFrame(5, 0);
2062 NackPacket(3, &frame1
);
2063 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
2064 ProcessAckPacket(&frame1
);
2066 // Now the client implicitly acks 3, and explicitly acks 6.
2067 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
2068 QuicAckFrame frame2
= InitAckFrame(6, 0);
2069 ProcessAckPacket(&frame2
);
2072 TEST_P(QuicConnectionTest
, DontLatchUnackedPacket
) {
2073 SendStreamDataToPeer(1, "foo", 0, !kFin
, NULL
); // Packet 1;
2074 // From now on, we send acks, so the send algorithm won't mark them pending.
2075 ON_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
))
2076 .WillByDefault(Return(false));
2077 SendAckPacketToPeer(); // Packet 2
2079 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
2080 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
2081 QuicAckFrame frame
= InitAckFrame(1, 0);
2082 ProcessAckPacket(&frame
);
2084 // Verify that our internal state has least-unacked as 2, because we're still
2085 // waiting for a potential ack for 2.
2086 EXPECT_EQ(2u, outgoing_ack()->sent_info
.least_unacked
);
2088 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
2089 frame
= InitAckFrame(2, 0);
2090 ProcessAckPacket(&frame
);
2091 EXPECT_EQ(3u, outgoing_ack()->sent_info
.least_unacked
);
2093 // When we send an ack, we make sure our least-unacked makes sense. In this
2094 // case since we're not waiting on an ack for 2 and all packets are acked, we
2096 SendAckPacketToPeer(); // Packet 3
2097 // Least_unacked remains at 3 until another ack is received.
2098 EXPECT_EQ(3u, outgoing_ack()->sent_info
.least_unacked
);
2099 // Check that the outgoing ack had its sequence number as least_unacked.
2100 EXPECT_EQ(3u, least_unacked());
2102 // Ack the ack, which updates the rtt and raises the least unacked.
2103 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
2104 frame
= InitAckFrame(3, 0);
2105 ProcessAckPacket(&frame
);
2107 ON_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
))
2108 .WillByDefault(Return(true));
2109 SendStreamDataToPeer(1, "bar", 3, false, NULL
); // Packet 4
2110 EXPECT_EQ(4u, outgoing_ack()->sent_info
.least_unacked
);
2111 ON_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
))
2112 .WillByDefault(Return(false));
2113 SendAckPacketToPeer(); // Packet 5
2114 EXPECT_EQ(4u, least_unacked());
2116 // Send two data packets at the end, and ensure if the last one is acked,
2117 // the least unacked is raised above the ack packets.
2118 ON_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
))
2119 .WillByDefault(Return(true));
2120 SendStreamDataToPeer(1, "bar", 6, false, NULL
); // Packet 6
2121 SendStreamDataToPeer(1, "bar", 9, false, NULL
); // Packet 7
2123 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
2124 frame
= InitAckFrame(7, 0);
2125 NackPacket(5, &frame
);
2126 NackPacket(6, &frame
);
2127 ProcessAckPacket(&frame
);
2129 EXPECT_EQ(6u, outgoing_ack()->sent_info
.least_unacked
);
2132 TEST_P(QuicConnectionTest
, ReviveMissingPacketAfterFecPacket
) {
2133 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
2135 // Don't send missing packet 1.
2136 ProcessFecPacket(2, 1, true, !kEntropyFlag
, NULL
);
2137 // Entropy flag should be false, so entropy should be 0.
2138 EXPECT_EQ(0u, QuicConnectionPeer::ReceivedEntropyHash(&connection_
, 2));
2141 TEST_P(QuicConnectionTest
, ReviveMissingPacketWithVaryingSeqNumLengths
) {
2142 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
2144 // Set up a debug visitor to the connection.
2145 scoped_ptr
<FecQuicConnectionDebugVisitor
>
2146 fec_visitor(new FecQuicConnectionDebugVisitor
);
2147 connection_
.set_debug_visitor(fec_visitor
.get());
2149 QuicPacketSequenceNumber fec_packet
= 0;
2150 QuicSequenceNumberLength lengths
[] = {PACKET_6BYTE_SEQUENCE_NUMBER
,
2151 PACKET_4BYTE_SEQUENCE_NUMBER
,
2152 PACKET_2BYTE_SEQUENCE_NUMBER
,
2153 PACKET_1BYTE_SEQUENCE_NUMBER
};
2154 // For each sequence number length size, revive a packet and check sequence
2155 // number length in the revived packet.
2156 for (size_t i
= 0; i
< arraysize(lengths
); ++i
) {
2157 // Set sequence_number_length_ (for data and FEC packets).
2158 sequence_number_length_
= lengths
[i
];
2160 // Don't send missing packet, but send fec packet right after it.
2161 ProcessFecPacket(fec_packet
, fec_packet
- 1, true, !kEntropyFlag
, NULL
);
2162 // Sequence number length in the revived header should be the same as
2163 // in the original data/fec packet headers.
2164 EXPECT_EQ(sequence_number_length_
, fec_visitor
->revived_header().
2165 public_header
.sequence_number_length
);
2169 TEST_P(QuicConnectionTest
, ReviveMissingPacketWithVaryingConnectionIdLengths
) {
2170 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
2172 // Set up a debug visitor to the connection.
2173 scoped_ptr
<FecQuicConnectionDebugVisitor
>
2174 fec_visitor(new FecQuicConnectionDebugVisitor
);
2175 connection_
.set_debug_visitor(fec_visitor
.get());
2177 QuicPacketSequenceNumber fec_packet
= 0;
2178 QuicConnectionIdLength lengths
[] = {PACKET_8BYTE_CONNECTION_ID
,
2179 PACKET_4BYTE_CONNECTION_ID
,
2180 PACKET_1BYTE_CONNECTION_ID
,
2181 PACKET_0BYTE_CONNECTION_ID
};
2182 // For each connection id length size, revive a packet and check connection
2183 // id length in the revived packet.
2184 for (size_t i
= 0; i
< arraysize(lengths
); ++i
) {
2185 // Set connection id length (for data and FEC packets).
2186 connection_id_length_
= lengths
[i
];
2188 // Don't send missing packet, but send fec packet right after it.
2189 ProcessFecPacket(fec_packet
, fec_packet
- 1, true, !kEntropyFlag
, NULL
);
2190 // Connection id length in the revived header should be the same as
2191 // in the original data/fec packet headers.
2192 EXPECT_EQ(connection_id_length_
,
2193 fec_visitor
->revived_header().public_header
.connection_id_length
);
2197 TEST_P(QuicConnectionTest
, ReviveMissingPacketAfterDataPacketThenFecPacket
) {
2198 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
2200 ProcessFecProtectedPacket(1, false, kEntropyFlag
);
2201 // Don't send missing packet 2.
2202 ProcessFecPacket(3, 1, true, !kEntropyFlag
, NULL
);
2203 // Entropy flag should be true, so entropy should not be 0.
2204 EXPECT_NE(0u, QuicConnectionPeer::ReceivedEntropyHash(&connection_
, 2));
2207 TEST_P(QuicConnectionTest
, ReviveMissingPacketAfterDataPacketsThenFecPacket
) {
2208 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
2210 ProcessFecProtectedPacket(1, false, !kEntropyFlag
);
2211 // Don't send missing packet 2.
2212 ProcessFecProtectedPacket(3, false, !kEntropyFlag
);
2213 ProcessFecPacket(4, 1, true, kEntropyFlag
, NULL
);
2214 // Ensure QUIC no longer revives entropy for lost packets.
2215 EXPECT_EQ(0u, QuicConnectionPeer::ReceivedEntropyHash(&connection_
, 2));
2216 EXPECT_NE(0u, QuicConnectionPeer::ReceivedEntropyHash(&connection_
, 4));
2219 TEST_P(QuicConnectionTest
, ReviveMissingPacketAfterDataPacket
) {
2220 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
2222 // Don't send missing packet 1.
2223 ProcessFecPacket(3, 1, false, !kEntropyFlag
, NULL
);
2225 ProcessFecProtectedPacket(2, true, !kEntropyFlag
);
2226 // Entropy flag should be false, so entropy should be 0.
2227 EXPECT_EQ(0u, QuicConnectionPeer::ReceivedEntropyHash(&connection_
, 2));
2230 TEST_P(QuicConnectionTest
, ReviveMissingPacketAfterDataPackets
) {
2231 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
2233 ProcessFecProtectedPacket(1, false, !kEntropyFlag
);
2234 // Don't send missing packet 2.
2235 ProcessFecPacket(6, 1, false, kEntropyFlag
, NULL
);
2236 ProcessFecProtectedPacket(3, false, kEntropyFlag
);
2237 ProcessFecProtectedPacket(4, false, kEntropyFlag
);
2238 ProcessFecProtectedPacket(5, true, !kEntropyFlag
);
2239 // Ensure entropy is not revived for the missing packet.
2240 EXPECT_EQ(0u, QuicConnectionPeer::ReceivedEntropyHash(&connection_
, 2));
2241 EXPECT_NE(0u, QuicConnectionPeer::ReceivedEntropyHash(&connection_
, 3));
2244 TEST_P(QuicConnectionTest
, TLP
) {
2245 QuicSentPacketManagerPeer::SetMaxTailLossProbes(
2246 QuicConnectionPeer::GetSentPacketManager(&connection_
), 1);
2248 SendStreamDataToPeer(3, "foo", 0, !kFin
, NULL
);
2249 EXPECT_EQ(1u, outgoing_ack()->sent_info
.least_unacked
);
2250 QuicTime retransmission_time
=
2251 connection_
.GetRetransmissionAlarm()->deadline();
2252 EXPECT_NE(QuicTime::Zero(), retransmission_time
);
2254 EXPECT_EQ(1u, writer_
->header().packet_sequence_number
);
2255 // Simulate the retransmission alarm firing and sending a tlp,
2256 // so send algorithm's OnRetransmissionTimeout is not called.
2257 clock_
.AdvanceTime(retransmission_time
.Subtract(clock_
.Now()));
2258 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, 2u, _
, _
));
2259 connection_
.GetRetransmissionAlarm()->Fire();
2260 EXPECT_EQ(2u, writer_
->header().packet_sequence_number
);
2261 // We do not raise the high water mark yet.
2262 EXPECT_EQ(1u, outgoing_ack()->sent_info
.least_unacked
);
2265 TEST_P(QuicConnectionTest
, RTO
) {
2266 QuicTime default_retransmission_time
= clock_
.ApproximateNow().Add(
2267 DefaultRetransmissionTime());
2268 SendStreamDataToPeer(3, "foo", 0, !kFin
, NULL
);
2269 EXPECT_EQ(1u, outgoing_ack()->sent_info
.least_unacked
);
2271 EXPECT_EQ(1u, writer_
->header().packet_sequence_number
);
2272 EXPECT_EQ(default_retransmission_time
,
2273 connection_
.GetRetransmissionAlarm()->deadline());
2274 // Simulate the retransmission alarm firing.
2275 clock_
.AdvanceTime(DefaultRetransmissionTime());
2276 EXPECT_CALL(*send_algorithm_
, OnRetransmissionTimeout(true));
2277 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, 2u, _
, _
));
2278 connection_
.GetRetransmissionAlarm()->Fire();
2279 EXPECT_EQ(2u, writer_
->header().packet_sequence_number
);
2280 // We do not raise the high water mark yet.
2281 EXPECT_EQ(1u, outgoing_ack()->sent_info
.least_unacked
);
2284 TEST_P(QuicConnectionTest
, RTOWithSameEncryptionLevel
) {
2285 QuicTime default_retransmission_time
= clock_
.ApproximateNow().Add(
2286 DefaultRetransmissionTime());
2287 use_tagging_decrypter();
2289 // A TaggingEncrypter puts kTagSize copies of the given byte (0x01 here) at
2290 // the end of the packet. We can test this to check which encrypter was used.
2291 connection_
.SetEncrypter(ENCRYPTION_NONE
, new TaggingEncrypter(0x01));
2292 SendStreamDataToPeer(3, "foo", 0, !kFin
, NULL
);
2293 EXPECT_EQ(0x01010101u
, writer_
->final_bytes_of_last_packet());
2295 connection_
.SetEncrypter(ENCRYPTION_INITIAL
, new TaggingEncrypter(0x02));
2296 connection_
.SetDefaultEncryptionLevel(ENCRYPTION_INITIAL
);
2297 SendStreamDataToPeer(3, "foo", 0, !kFin
, NULL
);
2298 EXPECT_EQ(0x02020202u
, writer_
->final_bytes_of_last_packet());
2300 EXPECT_EQ(default_retransmission_time
,
2301 connection_
.GetRetransmissionAlarm()->deadline());
2304 EXPECT_CALL(*send_algorithm_
, OnRetransmissionTimeout(true));
2305 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, 3, _
, _
));
2306 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, 4, _
, _
));
2309 // Simulate the retransmission alarm firing.
2310 clock_
.AdvanceTime(DefaultRetransmissionTime());
2311 connection_
.GetRetransmissionAlarm()->Fire();
2313 // Packet should have been sent with ENCRYPTION_NONE.
2314 EXPECT_EQ(0x01010101u
, writer_
->final_bytes_of_previous_packet());
2316 // Packet should have been sent with ENCRYPTION_INITIAL.
2317 EXPECT_EQ(0x02020202u
, writer_
->final_bytes_of_last_packet());
2320 TEST_P(QuicConnectionTest
, SendHandshakeMessages
) {
2321 use_tagging_decrypter();
2322 // A TaggingEncrypter puts kTagSize copies of the given byte (0x01 here) at
2323 // the end of the packet. We can test this to check which encrypter was used.
2324 connection_
.SetEncrypter(ENCRYPTION_NONE
, new TaggingEncrypter(0x01));
2326 // Attempt to send a handshake message and have the socket block.
2327 EXPECT_CALL(*send_algorithm_
,
2328 TimeUntilSend(_
, _
, _
)).WillRepeatedly(
2329 testing::Return(QuicTime::Delta::Zero()));
2331 connection_
.SendStreamDataWithString(1, "foo", 0, !kFin
, NULL
);
2332 // The packet should be serialized, but not queued.
2333 EXPECT_EQ(1u, connection_
.NumQueuedPackets());
2335 // Switch to the new encrypter.
2336 connection_
.SetEncrypter(ENCRYPTION_INITIAL
, new TaggingEncrypter(0x02));
2337 connection_
.SetDefaultEncryptionLevel(ENCRYPTION_INITIAL
);
2339 // Now become writeable and flush the packets.
2340 writer_
->SetWritable();
2341 EXPECT_CALL(visitor_
, OnCanWrite());
2342 connection_
.OnCanWrite();
2343 EXPECT_EQ(0u, connection_
.NumQueuedPackets());
2345 // Verify that the handshake packet went out at the null encryption.
2346 EXPECT_EQ(0x01010101u
, writer_
->final_bytes_of_last_packet());
2349 TEST_P(QuicConnectionTest
,
2350 DropRetransmitsForNullEncryptedPacketAfterForwardSecure
) {
2351 use_tagging_decrypter();
2352 connection_
.SetEncrypter(ENCRYPTION_NONE
, new TaggingEncrypter(0x01));
2353 QuicPacketSequenceNumber sequence_number
;
2354 SendStreamDataToPeer(3, "foo", 0, !kFin
, &sequence_number
);
2356 // Simulate the retransmission alarm firing and the socket blocking.
2358 EXPECT_CALL(*send_algorithm_
, OnRetransmissionTimeout(true));
2359 clock_
.AdvanceTime(DefaultRetransmissionTime());
2360 connection_
.GetRetransmissionAlarm()->Fire();
2362 // Go forward secure.
2363 connection_
.SetEncrypter(ENCRYPTION_FORWARD_SECURE
,
2364 new TaggingEncrypter(0x02));
2365 connection_
.SetDefaultEncryptionLevel(ENCRYPTION_FORWARD_SECURE
);
2366 connection_
.NeuterUnencryptedPackets();
2368 EXPECT_EQ(QuicTime::Zero(),
2369 connection_
.GetRetransmissionAlarm()->deadline());
2370 // Unblock the socket and ensure that no packets are sent.
2371 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
)).Times(0);
2372 writer_
->SetWritable();
2373 connection_
.OnCanWrite();
2376 TEST_P(QuicConnectionTest
, RetransmitPacketsWithInitialEncryption
) {
2377 use_tagging_decrypter();
2378 connection_
.SetEncrypter(ENCRYPTION_NONE
, new TaggingEncrypter(0x01));
2379 connection_
.SetDefaultEncryptionLevel(ENCRYPTION_NONE
);
2381 SendStreamDataToPeer(1, "foo", 0, !kFin
, NULL
);
2383 connection_
.SetEncrypter(ENCRYPTION_INITIAL
, new TaggingEncrypter(0x02));
2384 connection_
.SetDefaultEncryptionLevel(ENCRYPTION_INITIAL
);
2386 SendStreamDataToPeer(2, "bar", 0, !kFin
, NULL
);
2387 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
)).Times(1);
2389 connection_
.RetransmitUnackedPackets(INITIAL_ENCRYPTION_ONLY
);
2392 TEST_P(QuicConnectionTest
, BufferNonDecryptablePackets
) {
2393 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
2394 use_tagging_decrypter();
2396 const uint8 tag
= 0x07;
2397 framer_
.SetEncrypter(ENCRYPTION_INITIAL
, new TaggingEncrypter(tag
));
2399 // Process an encrypted packet which can not yet be decrypted
2400 // which should result in the packet being buffered.
2401 ProcessDataPacketAtLevel(1, 0, kEntropyFlag
, ENCRYPTION_INITIAL
);
2403 // Transition to the new encryption state and process another
2404 // encrypted packet which should result in the original packet being
2406 connection_
.SetDecrypter(new StrictTaggingDecrypter(tag
),
2407 ENCRYPTION_INITIAL
);
2408 connection_
.SetDefaultEncryptionLevel(ENCRYPTION_INITIAL
);
2409 connection_
.SetEncrypter(ENCRYPTION_INITIAL
, new TaggingEncrypter(tag
));
2410 EXPECT_CALL(visitor_
, OnStreamFrames(_
)).Times(2);
2411 ProcessDataPacketAtLevel(2, 0, kEntropyFlag
, ENCRYPTION_INITIAL
);
2413 // Finally, process a third packet and note that we do not
2414 // reprocess the buffered packet.
2415 EXPECT_CALL(visitor_
, OnStreamFrames(_
)).Times(1);
2416 ProcessDataPacketAtLevel(3, 0, kEntropyFlag
, ENCRYPTION_INITIAL
);
2419 TEST_P(QuicConnectionTest
, TestRetransmitOrder
) {
2420 QuicByteCount first_packet_size
;
2421 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
)).WillOnce(
2422 DoAll(SaveArg
<3>(&first_packet_size
), Return(true)));
2424 connection_
.SendStreamDataWithString(3, "first_packet", 0, !kFin
, NULL
);
2425 QuicByteCount second_packet_size
;
2426 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
)).WillOnce(
2427 DoAll(SaveArg
<3>(&second_packet_size
), Return(true)));
2428 connection_
.SendStreamDataWithString(3, "second_packet", 12, !kFin
, NULL
);
2429 EXPECT_NE(first_packet_size
, second_packet_size
);
2430 // Advance the clock by huge time to make sure packets will be retransmitted.
2431 clock_
.AdvanceTime(QuicTime::Delta::FromSeconds(10));
2432 EXPECT_CALL(*send_algorithm_
, OnRetransmissionTimeout(true));
2435 EXPECT_CALL(*send_algorithm_
,
2436 OnPacketSent(_
, _
, _
, first_packet_size
, _
));
2437 EXPECT_CALL(*send_algorithm_
,
2438 OnPacketSent(_
, _
, _
, second_packet_size
, _
));
2440 connection_
.GetRetransmissionAlarm()->Fire();
2442 // Advance again and expect the packets to be sent again in the same order.
2443 clock_
.AdvanceTime(QuicTime::Delta::FromSeconds(20));
2444 EXPECT_CALL(*send_algorithm_
, OnRetransmissionTimeout(true));
2447 EXPECT_CALL(*send_algorithm_
,
2448 OnPacketSent(_
, _
, _
, first_packet_size
, _
));
2449 EXPECT_CALL(*send_algorithm_
,
2450 OnPacketSent(_
, _
, _
, second_packet_size
, _
));
2452 connection_
.GetRetransmissionAlarm()->Fire();
2455 TEST_P(QuicConnectionTest
, RetransmissionCountCalculation
) {
2456 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
2457 QuicPacketSequenceNumber original_sequence_number
;
2458 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
))
2459 .WillOnce(DoAll(SaveArg
<2>(&original_sequence_number
), Return(true)));
2460 connection_
.SendStreamDataWithString(3, "foo", 0, !kFin
, NULL
);
2462 EXPECT_TRUE(QuicConnectionPeer::IsSavedForRetransmission(
2463 &connection_
, original_sequence_number
));
2464 EXPECT_FALSE(QuicConnectionPeer::IsRetransmission(
2465 &connection_
, original_sequence_number
));
2466 // Force retransmission due to RTO.
2467 clock_
.AdvanceTime(QuicTime::Delta::FromSeconds(10));
2468 EXPECT_CALL(*send_algorithm_
, OnRetransmissionTimeout(true));
2469 QuicPacketSequenceNumber rto_sequence_number
;
2470 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
))
2471 .WillOnce(DoAll(SaveArg
<2>(&rto_sequence_number
), Return(true)));
2472 connection_
.GetRetransmissionAlarm()->Fire();
2473 EXPECT_FALSE(QuicConnectionPeer::IsSavedForRetransmission(
2474 &connection_
, original_sequence_number
));
2475 ASSERT_TRUE(QuicConnectionPeer::IsSavedForRetransmission(
2476 &connection_
, rto_sequence_number
));
2477 EXPECT_TRUE(QuicConnectionPeer::IsRetransmission(
2478 &connection_
, rto_sequence_number
));
2479 // Once by explicit nack.
2480 SequenceNumberSet lost_packets
;
2481 lost_packets
.insert(rto_sequence_number
);
2482 EXPECT_CALL(*loss_algorithm_
, DetectLostPackets(_
, _
, _
, _
))
2483 .WillOnce(Return(lost_packets
));
2484 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
2485 QuicPacketSequenceNumber nack_sequence_number
= 0;
2486 // Ack packets might generate some other packets, which are not
2487 // retransmissions. (More ack packets).
2488 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
))
2489 .Times(AnyNumber());
2490 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
))
2491 .WillOnce(DoAll(SaveArg
<2>(&nack_sequence_number
), Return(true)));
2492 QuicAckFrame ack
= InitAckFrame(rto_sequence_number
, 0);
2493 // Nack the retransmitted packet.
2494 NackPacket(original_sequence_number
, &ack
);
2495 NackPacket(rto_sequence_number
, &ack
);
2496 ProcessAckPacket(&ack
);
2498 ASSERT_NE(0u, nack_sequence_number
);
2499 EXPECT_FALSE(QuicConnectionPeer::IsSavedForRetransmission(
2500 &connection_
, rto_sequence_number
));
2501 ASSERT_TRUE(QuicConnectionPeer::IsSavedForRetransmission(
2502 &connection_
, nack_sequence_number
));
2503 EXPECT_TRUE(QuicConnectionPeer::IsRetransmission(
2504 &connection_
, nack_sequence_number
));
2507 TEST_P(QuicConnectionTest
, SetRTOAfterWritingToSocket
) {
2509 connection_
.SendStreamDataWithString(1, "foo", 0, !kFin
, NULL
);
2510 // Make sure that RTO is not started when the packet is queued.
2511 EXPECT_FALSE(connection_
.GetRetransmissionAlarm()->IsSet());
2513 // Test that RTO is started once we write to the socket.
2514 writer_
->SetWritable();
2515 connection_
.OnCanWrite();
2516 EXPECT_TRUE(connection_
.GetRetransmissionAlarm()->IsSet());
2519 TEST_P(QuicConnectionTest
, DelayRTOWithAckReceipt
) {
2520 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
2521 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
))
2523 connection_
.SendStreamDataWithString(2, "foo", 0, !kFin
, NULL
);
2524 connection_
.SendStreamDataWithString(3, "bar", 0, !kFin
, NULL
);
2525 QuicAlarm
* retransmission_alarm
= connection_
.GetRetransmissionAlarm();
2526 EXPECT_TRUE(retransmission_alarm
->IsSet());
2527 EXPECT_EQ(clock_
.Now().Add(DefaultRetransmissionTime()),
2528 retransmission_alarm
->deadline());
2530 // Advance the time right before the RTO, then receive an ack for the first
2531 // packet to delay the RTO.
2532 clock_
.AdvanceTime(DefaultRetransmissionTime());
2533 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
2534 QuicAckFrame ack
= InitAckFrame(1, 0);
2535 ProcessAckPacket(&ack
);
2536 EXPECT_TRUE(retransmission_alarm
->IsSet());
2537 EXPECT_GT(retransmission_alarm
->deadline(), clock_
.Now());
2539 // Move forward past the original RTO and ensure the RTO is still pending.
2540 clock_
.AdvanceTime(DefaultRetransmissionTime().Multiply(2));
2542 // Ensure the second packet gets retransmitted when it finally fires.
2543 EXPECT_TRUE(retransmission_alarm
->IsSet());
2544 EXPECT_LT(retransmission_alarm
->deadline(), clock_
.ApproximateNow());
2545 EXPECT_CALL(*send_algorithm_
, OnRetransmissionTimeout(true));
2546 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
));
2547 // Manually cancel the alarm to simulate a real test.
2548 connection_
.GetRetransmissionAlarm()->Fire();
2550 // The new retransmitted sequence number should set the RTO to a larger value
2552 EXPECT_TRUE(retransmission_alarm
->IsSet());
2553 QuicTime next_rto_time
= retransmission_alarm
->deadline();
2554 QuicTime expected_rto_time
=
2555 connection_
.sent_packet_manager().GetRetransmissionTime();
2556 EXPECT_EQ(next_rto_time
, expected_rto_time
);
2559 TEST_P(QuicConnectionTest
, TestQueued
) {
2560 EXPECT_EQ(0u, connection_
.NumQueuedPackets());
2562 connection_
.SendStreamDataWithString(1, "foo", 0, !kFin
, NULL
);
2563 EXPECT_EQ(1u, connection_
.NumQueuedPackets());
2565 // Unblock the writes and actually send.
2566 writer_
->SetWritable();
2567 connection_
.OnCanWrite();
2568 EXPECT_EQ(0u, connection_
.NumQueuedPackets());
2571 TEST_P(QuicConnectionTest
, CloseFecGroup
) {
2572 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
2573 // Don't send missing packet 1.
2574 // Don't send missing packet 2.
2575 ProcessFecProtectedPacket(3, false, !kEntropyFlag
);
2576 // Don't send missing FEC packet 3.
2577 ASSERT_EQ(1u, connection_
.NumFecGroups());
2579 // Now send non-fec protected ack packet and close the group.
2580 peer_creator_
.set_sequence_number(4);
2581 if (version() > QUIC_VERSION_15
) {
2582 QuicStopWaitingFrame frame
= InitStopWaitingFrame(5);
2583 ProcessStopWaitingPacket(&frame
);
2585 QuicAckFrame frame
= InitAckFrame(0, 5);
2586 ProcessAckPacket(&frame
);
2588 ASSERT_EQ(0u, connection_
.NumFecGroups());
2591 TEST_P(QuicConnectionTest
, NoQuicCongestionFeedbackFrame
) {
2592 SendAckPacketToPeer();
2593 EXPECT_TRUE(writer_
->feedback_frames().empty());
2596 TEST_P(QuicConnectionTest
, WithQuicCongestionFeedbackFrame
) {
2597 QuicCongestionFeedbackFrame info
;
2598 info
.type
= kFixRate
;
2599 info
.fix_rate
.bitrate
= QuicBandwidth::FromBytesPerSecond(123);
2602 SendAckPacketToPeer();
2603 ASSERT_FALSE(writer_
->feedback_frames().empty());
2604 ASSERT_EQ(kFixRate
, writer_
->feedback_frames()[0].type
);
2605 ASSERT_EQ(info
.fix_rate
.bitrate
,
2606 writer_
->feedback_frames()[0].fix_rate
.bitrate
);
2609 TEST_P(QuicConnectionTest
, UpdateQuicCongestionFeedbackFrame
) {
2610 SendAckPacketToPeer();
2611 EXPECT_CALL(*receive_algorithm_
, RecordIncomingPacket(_
, _
, _
));
2612 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
2616 TEST_P(QuicConnectionTest
, DontUpdateQuicCongestionFeedbackFrameForRevived
) {
2617 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
2618 SendAckPacketToPeer();
2619 // Process an FEC packet, and revive the missing data packet
2620 // but only contact the receive_algorithm once.
2621 EXPECT_CALL(*receive_algorithm_
, RecordIncomingPacket(_
, _
, _
));
2622 ProcessFecPacket(2, 1, true, !kEntropyFlag
, NULL
);
2625 TEST_P(QuicConnectionTest
, InitialTimeout
) {
2626 EXPECT_TRUE(connection_
.connected());
2627 EXPECT_CALL(visitor_
, OnConnectionClosed(QUIC_CONNECTION_TIMED_OUT
, false));
2628 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
));
2630 QuicTime default_timeout
= clock_
.ApproximateNow().Add(
2631 QuicTime::Delta::FromSeconds(kDefaultInitialTimeoutSecs
));
2632 EXPECT_EQ(default_timeout
, connection_
.GetTimeoutAlarm()->deadline());
2634 // Simulate the timeout alarm firing.
2636 QuicTime::Delta::FromSeconds(kDefaultInitialTimeoutSecs
));
2637 connection_
.GetTimeoutAlarm()->Fire();
2638 EXPECT_FALSE(connection_
.GetTimeoutAlarm()->IsSet());
2639 EXPECT_FALSE(connection_
.connected());
2641 EXPECT_FALSE(connection_
.GetAckAlarm()->IsSet());
2642 EXPECT_FALSE(connection_
.GetPingAlarm()->IsSet());
2643 EXPECT_FALSE(connection_
.GetResumeWritesAlarm()->IsSet());
2644 EXPECT_FALSE(connection_
.GetRetransmissionAlarm()->IsSet());
2645 EXPECT_FALSE(connection_
.GetSendAlarm()->IsSet());
2646 EXPECT_FALSE(connection_
.GetTimeoutAlarm()->IsSet());
2649 TEST_P(QuicConnectionTest
, PingAfterSend
) {
2650 EXPECT_TRUE(connection_
.connected());
2651 EXPECT_CALL(visitor_
, HasOpenDataStreams()).WillRepeatedly(Return(true));
2652 EXPECT_FALSE(connection_
.GetPingAlarm()->IsSet());
2654 // Advance to 5ms, and send a packet to the peer, which will set
2656 clock_
.AdvanceTime(QuicTime::Delta::FromMilliseconds(5));
2657 EXPECT_FALSE(connection_
.GetRetransmissionAlarm()->IsSet());
2658 SendStreamDataToPeer(1, "GET /", 0, kFin
, NULL
);
2659 EXPECT_TRUE(connection_
.GetPingAlarm()->IsSet());
2660 EXPECT_EQ(clock_
.ApproximateNow().Add(QuicTime::Delta::FromSeconds(15)),
2661 connection_
.GetPingAlarm()->deadline());
2663 // Now recevie and ACK of the previous packet, which will move the
2664 // ping alarm forward.
2665 clock_
.AdvanceTime(QuicTime::Delta::FromMilliseconds(5));
2666 QuicAckFrame frame
= InitAckFrame(1, 0);
2667 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
2668 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
2669 ProcessAckPacket(&frame
);
2670 EXPECT_TRUE(connection_
.GetPingAlarm()->IsSet());
2671 EXPECT_EQ(clock_
.ApproximateNow().Add(QuicTime::Delta::FromSeconds(15)),
2672 connection_
.GetPingAlarm()->deadline());
2675 clock_
.AdvanceTime(QuicTime::Delta::FromSeconds(15));
2676 connection_
.GetPingAlarm()->Fire();
2677 EXPECT_EQ(1u, writer_
->frame_count());
2678 if (version() >= QUIC_VERSION_18
) {
2679 ASSERT_EQ(1u, writer_
->ping_frames().size());
2681 ASSERT_EQ(1u, writer_
->stream_frames().size());
2682 EXPECT_EQ(kCryptoStreamId
, writer_
->stream_frames()[0].stream_id
);
2683 EXPECT_EQ(0u, writer_
->stream_frames()[0].offset
);
2687 EXPECT_CALL(visitor_
, HasOpenDataStreams()).WillRepeatedly(Return(false));
2688 clock_
.AdvanceTime(QuicTime::Delta::FromMilliseconds(5));
2689 SendAckPacketToPeer();
2691 EXPECT_FALSE(connection_
.GetPingAlarm()->IsSet());
2694 TEST_P(QuicConnectionTest
, TimeoutAfterSend
) {
2695 EXPECT_TRUE(connection_
.connected());
2697 QuicTime default_timeout
= clock_
.ApproximateNow().Add(
2698 QuicTime::Delta::FromSeconds(kDefaultInitialTimeoutSecs
));
2700 // When we send a packet, the timeout will change to 5000 +
2701 // kDefaultInitialTimeoutSecs.
2702 clock_
.AdvanceTime(QuicTime::Delta::FromMilliseconds(5));
2704 // Send an ack so we don't set the retransmission alarm.
2705 SendAckPacketToPeer();
2706 EXPECT_EQ(default_timeout
, connection_
.GetTimeoutAlarm()->deadline());
2708 // The original alarm will fire. We should not time out because we had a
2709 // network event at t=5000. The alarm will reregister.
2710 clock_
.AdvanceTime(QuicTime::Delta::FromMicroseconds(
2711 kDefaultInitialTimeoutSecs
* 1000000 - 5000));
2712 EXPECT_EQ(default_timeout
, clock_
.ApproximateNow());
2713 connection_
.GetTimeoutAlarm()->Fire();
2714 EXPECT_TRUE(connection_
.GetTimeoutAlarm()->IsSet());
2715 EXPECT_TRUE(connection_
.connected());
2716 EXPECT_EQ(default_timeout
.Add(QuicTime::Delta::FromMilliseconds(5)),
2717 connection_
.GetTimeoutAlarm()->deadline());
2719 // This time, we should time out.
2720 EXPECT_CALL(visitor_
, OnConnectionClosed(QUIC_CONNECTION_TIMED_OUT
, false));
2721 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
));
2722 clock_
.AdvanceTime(QuicTime::Delta::FromMilliseconds(5));
2723 EXPECT_EQ(default_timeout
.Add(QuicTime::Delta::FromMilliseconds(5)),
2724 clock_
.ApproximateNow());
2725 connection_
.GetTimeoutAlarm()->Fire();
2726 EXPECT_FALSE(connection_
.GetTimeoutAlarm()->IsSet());
2727 EXPECT_FALSE(connection_
.connected());
2730 TEST_P(QuicConnectionTest
, SendScheduler
) {
2731 // Test that if we send a packet without delay, it is not queued.
2732 QuicPacket
* packet
= ConstructDataPacket(1, 0, !kEntropyFlag
);
2733 EXPECT_CALL(*send_algorithm_
,
2734 TimeUntilSend(_
, _
, _
)).WillOnce(
2735 testing::Return(QuicTime::Delta::Zero()));
2736 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
));
2737 connection_
.SendPacket(
2738 ENCRYPTION_NONE
, 1, packet
, kTestEntropyHash
, HAS_RETRANSMITTABLE_DATA
);
2739 EXPECT_EQ(0u, connection_
.NumQueuedPackets());
2742 TEST_P(QuicConnectionTest
, SendSchedulerDelay
) {
2743 // Test that if we send a packet with a delay, it ends up queued.
2744 QuicPacket
* packet
= ConstructDataPacket(1, 0, !kEntropyFlag
);
2745 EXPECT_CALL(*send_algorithm_
,
2746 TimeUntilSend(_
, _
, _
)).WillOnce(
2747 testing::Return(QuicTime::Delta::FromMicroseconds(1)));
2748 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, 1, _
, _
)).Times(0);
2749 connection_
.SendPacket(
2750 ENCRYPTION_NONE
, 1, packet
, kTestEntropyHash
, HAS_RETRANSMITTABLE_DATA
);
2751 EXPECT_EQ(1u, connection_
.NumQueuedPackets());
2754 TEST_P(QuicConnectionTest
, SendSchedulerEAGAIN
) {
2755 QuicPacket
* packet
= ConstructDataPacket(1, 0, !kEntropyFlag
);
2757 EXPECT_CALL(*send_algorithm_
,
2758 TimeUntilSend(_
, _
, _
)).WillOnce(
2759 testing::Return(QuicTime::Delta::Zero()));
2760 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, 1, _
, _
)).Times(0);
2761 connection_
.SendPacket(
2762 ENCRYPTION_NONE
, 1, packet
, kTestEntropyHash
, HAS_RETRANSMITTABLE_DATA
);
2763 EXPECT_EQ(1u, connection_
.NumQueuedPackets());
2766 TEST_P(QuicConnectionTest
, SendSchedulerDelayThenSend
) {
2767 // Test that if we send a packet with a delay, it ends up queued.
2768 QuicPacket
* packet
= ConstructDataPacket(1, 0, !kEntropyFlag
);
2769 EXPECT_CALL(*send_algorithm_
,
2770 TimeUntilSend(_
, _
, _
)).WillOnce(
2771 testing::Return(QuicTime::Delta::FromMicroseconds(1)));
2772 connection_
.SendPacket(
2773 ENCRYPTION_NONE
, 1, packet
, kTestEntropyHash
, HAS_RETRANSMITTABLE_DATA
);
2774 EXPECT_EQ(1u, connection_
.NumQueuedPackets());
2776 // Advance the clock to fire the alarm, and configure the scheduler
2777 // to permit the packet to be sent.
2778 EXPECT_CALL(*send_algorithm_
,
2779 TimeUntilSend(_
, _
, _
)).WillRepeatedly(
2780 testing::Return(QuicTime::Delta::Zero()));
2781 clock_
.AdvanceTime(QuicTime::Delta::FromMicroseconds(1));
2782 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
));
2783 connection_
.GetSendAlarm()->Fire();
2784 EXPECT_EQ(0u, connection_
.NumQueuedPackets());
2787 TEST_P(QuicConnectionTest
, SendSchedulerDelayThenRetransmit
) {
2788 CongestionUnblockWrites();
2789 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, 1, _
, _
));
2790 connection_
.SendStreamDataWithString(3, "foo", 0, !kFin
, NULL
);
2791 EXPECT_EQ(0u, connection_
.NumQueuedPackets());
2792 // Advance the time for retransmission of lost packet.
2793 clock_
.AdvanceTime(QuicTime::Delta::FromMilliseconds(501));
2794 // Test that if we send a retransmit with a delay, it ends up queued in the
2795 // sent packet manager, but not yet serialized.
2796 EXPECT_CALL(*send_algorithm_
, OnRetransmissionTimeout(true));
2797 CongestionBlockWrites();
2798 connection_
.GetRetransmissionAlarm()->Fire();
2799 EXPECT_EQ(0u, connection_
.NumQueuedPackets());
2801 // Advance the clock to fire the alarm, and configure the scheduler
2802 // to permit the packet to be sent.
2803 CongestionUnblockWrites();
2805 // Ensure the scheduler is notified this is a retransmit.
2806 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
));
2807 clock_
.AdvanceTime(QuicTime::Delta::FromMicroseconds(1));
2808 connection_
.GetSendAlarm()->Fire();
2809 EXPECT_EQ(0u, connection_
.NumQueuedPackets());
2812 TEST_P(QuicConnectionTest
, SendSchedulerDelayAndQueue
) {
2813 QuicPacket
* packet
= ConstructDataPacket(1, 0, !kEntropyFlag
);
2814 EXPECT_CALL(*send_algorithm_
,
2815 TimeUntilSend(_
, _
, _
)).WillOnce(
2816 testing::Return(QuicTime::Delta::FromMicroseconds(1)));
2817 connection_
.SendPacket(
2818 ENCRYPTION_NONE
, 1, packet
, kTestEntropyHash
, HAS_RETRANSMITTABLE_DATA
);
2819 EXPECT_EQ(1u, connection_
.NumQueuedPackets());
2821 // Attempt to send another packet and make sure that it gets queued.
2822 packet
= ConstructDataPacket(2, 0, !kEntropyFlag
);
2823 connection_
.SendPacket(
2824 ENCRYPTION_NONE
, 2, packet
, kTestEntropyHash
, HAS_RETRANSMITTABLE_DATA
);
2825 EXPECT_EQ(2u, connection_
.NumQueuedPackets());
2828 TEST_P(QuicConnectionTest
, SendSchedulerDelayThenAckAndSend
) {
2829 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
2830 QuicPacket
* packet
= ConstructDataPacket(1, 0, !kEntropyFlag
);
2831 EXPECT_CALL(*send_algorithm_
,
2832 TimeUntilSend(_
, _
, _
)).WillOnce(
2833 testing::Return(QuicTime::Delta::FromMicroseconds(10)));
2834 connection_
.SendPacket(
2835 ENCRYPTION_NONE
, 1, packet
, kTestEntropyHash
, HAS_RETRANSMITTABLE_DATA
);
2836 EXPECT_EQ(1u, connection_
.NumQueuedPackets());
2838 // Now send non-retransmitting information, that we're not going to
2839 // retransmit 3. The far end should stop waiting for it.
2840 QuicAckFrame frame
= InitAckFrame(0, 1);
2841 EXPECT_CALL(*send_algorithm_
,
2842 TimeUntilSend(_
, _
, _
)).WillRepeatedly(
2843 testing::Return(QuicTime::Delta::Zero()));
2844 EXPECT_CALL(*send_algorithm_
,
2845 OnPacketSent(_
, _
, _
, _
, _
));
2846 ProcessAckPacket(&frame
);
2848 EXPECT_EQ(0u, connection_
.NumQueuedPackets());
2849 // Ensure alarm is not set
2850 EXPECT_FALSE(connection_
.GetSendAlarm()->IsSet());
2853 TEST_P(QuicConnectionTest
, SendSchedulerDelayThenAckAndHold
) {
2854 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
2855 QuicPacket
* packet
= ConstructDataPacket(1, 0, !kEntropyFlag
);
2856 EXPECT_CALL(*send_algorithm_
,
2857 TimeUntilSend(_
, _
, _
)).WillOnce(
2858 testing::Return(QuicTime::Delta::FromMicroseconds(10)));
2859 connection_
.SendPacket(
2860 ENCRYPTION_NONE
, 1, packet
, kTestEntropyHash
, HAS_RETRANSMITTABLE_DATA
);
2861 EXPECT_EQ(1u, connection_
.NumQueuedPackets());
2863 // Now send non-retransmitting information, that we're not going to
2864 // retransmit 3. The far end should stop waiting for it.
2865 QuicAckFrame frame
= InitAckFrame(0, 1);
2866 EXPECT_CALL(*send_algorithm_
,
2867 TimeUntilSend(_
, _
, _
)).WillOnce(
2868 testing::Return(QuicTime::Delta::FromMicroseconds(1)));
2869 ProcessAckPacket(&frame
);
2871 EXPECT_EQ(1u, connection_
.NumQueuedPackets());
2874 TEST_P(QuicConnectionTest
, SendSchedulerDelayThenOnCanWrite
) {
2875 // TODO(ianswett): This test is unrealistic, because we would not serialize
2876 // new data if the send algorithm said not to.
2877 QuicPacket
* packet
= ConstructDataPacket(1, 0, !kEntropyFlag
);
2878 CongestionBlockWrites();
2879 connection_
.SendPacket(
2880 ENCRYPTION_NONE
, 1, packet
, kTestEntropyHash
, HAS_RETRANSMITTABLE_DATA
);
2881 EXPECT_EQ(1u, connection_
.NumQueuedPackets());
2883 // OnCanWrite should send the packet, because it won't consult the send
2884 // algorithm for queued packets.
2885 connection_
.OnCanWrite();
2886 EXPECT_EQ(0u, connection_
.NumQueuedPackets());
2889 TEST_P(QuicConnectionTest
, TestQueueLimitsOnSendStreamData
) {
2890 // All packets carry version info till version is negotiated.
2891 size_t payload_length
;
2892 size_t length
= GetPacketLengthForOneStream(
2893 connection_
.version(), kIncludeVersion
, PACKET_1BYTE_SEQUENCE_NUMBER
,
2894 NOT_IN_FEC_GROUP
, &payload_length
);
2895 QuicConnectionPeer::GetPacketCreator(&connection_
)->set_max_packet_length(
2898 // Queue the first packet.
2899 EXPECT_CALL(*send_algorithm_
,
2900 TimeUntilSend(_
, _
, _
)).WillOnce(
2901 testing::Return(QuicTime::Delta::FromMicroseconds(10)));
2902 const string
payload(payload_length
, 'a');
2904 connection_
.SendStreamDataWithString(3, payload
, 0,
2905 !kFin
, NULL
).bytes_consumed
);
2906 EXPECT_EQ(0u, connection_
.NumQueuedPackets());
2909 TEST_P(QuicConnectionTest
, LoopThroughSendingPackets
) {
2910 // All packets carry version info till version is negotiated.
2911 size_t payload_length
;
2912 // GetPacketLengthForOneStream() assumes a stream offset of 0 in determining
2913 // packet length. The size of the offset field in a stream frame is 0 for
2914 // offset 0, and 2 for non-zero offsets up through 16K. Increase
2915 // max_packet_length by 2 so that subsequent packets containing subsequent
2916 // stream frames with non-zero offets will fit within the packet length.
2917 size_t length
= 2 + GetPacketLengthForOneStream(
2918 connection_
.version(), kIncludeVersion
, PACKET_1BYTE_SEQUENCE_NUMBER
,
2919 NOT_IN_FEC_GROUP
, &payload_length
);
2920 QuicConnectionPeer::GetPacketCreator(&connection_
)->set_max_packet_length(
2923 // Queue the first packet.
2924 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
)).Times(7);
2925 // The first stream frame will have 2 fewer overhead bytes than the other six.
2926 const string
payload(payload_length
* 7 + 2, 'a');
2927 EXPECT_EQ(payload
.size(),
2928 connection_
.SendStreamDataWithString(1, payload
, 0,
2929 !kFin
, NULL
).bytes_consumed
);
2932 TEST_P(QuicConnectionTest
, SendDelayedAck
) {
2933 QuicTime ack_time
= clock_
.ApproximateNow().Add(DefaultDelayedAckTime());
2934 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
2935 EXPECT_FALSE(connection_
.GetAckAlarm()->IsSet());
2936 const uint8 tag
= 0x07;
2937 connection_
.SetDecrypter(new StrictTaggingDecrypter(tag
),
2938 ENCRYPTION_INITIAL
);
2939 framer_
.SetEncrypter(ENCRYPTION_INITIAL
, new TaggingEncrypter(tag
));
2940 // Process a packet from the non-crypto stream.
2941 frame1_
.stream_id
= 3;
2943 // The same as ProcessPacket(1) except that ENCRYPTION_INITIAL is used
2944 // instead of ENCRYPTION_NONE.
2945 EXPECT_CALL(visitor_
, OnStreamFrames(_
)).Times(1);
2946 ProcessDataPacketAtLevel(1, 0, !kEntropyFlag
, ENCRYPTION_INITIAL
);
2948 // Check if delayed ack timer is running for the expected interval.
2949 EXPECT_TRUE(connection_
.GetAckAlarm()->IsSet());
2950 EXPECT_EQ(ack_time
, connection_
.GetAckAlarm()->deadline());
2951 // Simulate delayed ack alarm firing.
2952 connection_
.GetAckAlarm()->Fire();
2953 // Check that ack is sent and that delayed ack alarm is reset.
2954 if (version() > QUIC_VERSION_15
) {
2955 EXPECT_EQ(2u, writer_
->frame_count());
2956 EXPECT_FALSE(writer_
->stop_waiting_frames().empty());
2958 EXPECT_EQ(1u, writer_
->frame_count());
2960 EXPECT_FALSE(writer_
->ack_frames().empty());
2961 EXPECT_FALSE(connection_
.GetAckAlarm()->IsSet());
2964 TEST_P(QuicConnectionTest
, SendEarlyDelayedAckForCrypto
) {
2965 QuicTime ack_time
= clock_
.ApproximateNow();
2966 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
2967 EXPECT_FALSE(connection_
.GetAckAlarm()->IsSet());
2968 // Process a packet from the crypto stream, which is frame1_'s default.
2970 // Check if delayed ack timer is running for the expected interval.
2971 EXPECT_TRUE(connection_
.GetAckAlarm()->IsSet());
2972 EXPECT_EQ(ack_time
, connection_
.GetAckAlarm()->deadline());
2973 // Simulate delayed ack alarm firing.
2974 connection_
.GetAckAlarm()->Fire();
2975 // Check that ack is sent and that delayed ack alarm is reset.
2976 if (version() > QUIC_VERSION_15
) {
2977 EXPECT_EQ(2u, writer_
->frame_count());
2978 EXPECT_FALSE(writer_
->stop_waiting_frames().empty());
2980 EXPECT_EQ(1u, writer_
->frame_count());
2982 EXPECT_FALSE(writer_
->ack_frames().empty());
2983 EXPECT_FALSE(connection_
.GetAckAlarm()->IsSet());
2986 TEST_P(QuicConnectionTest
, SendDelayedAckOnSecondPacket
) {
2987 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
2990 // Check that ack is sent and that delayed ack alarm is reset.
2991 if (version() > QUIC_VERSION_15
) {
2992 EXPECT_EQ(2u, writer_
->frame_count());
2993 EXPECT_FALSE(writer_
->stop_waiting_frames().empty());
2995 EXPECT_EQ(1u, writer_
->frame_count());
2997 EXPECT_FALSE(writer_
->ack_frames().empty());
2998 EXPECT_FALSE(connection_
.GetAckAlarm()->IsSet());
3001 TEST_P(QuicConnectionTest
, NoAckOnOldNacks
) {
3002 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
3003 // Drop one packet, triggering a sequence of acks.
3005 size_t frames_per_ack
= version() > QUIC_VERSION_15
? 2 : 1;
3006 EXPECT_EQ(frames_per_ack
, writer_
->frame_count());
3007 EXPECT_FALSE(writer_
->ack_frames().empty());
3010 EXPECT_EQ(frames_per_ack
, writer_
->frame_count());
3011 EXPECT_FALSE(writer_
->ack_frames().empty());
3014 EXPECT_EQ(frames_per_ack
, writer_
->frame_count());
3015 EXPECT_FALSE(writer_
->ack_frames().empty());
3018 EXPECT_EQ(frames_per_ack
, writer_
->frame_count());
3019 EXPECT_FALSE(writer_
->ack_frames().empty());
3021 // Now only set the timer on the 6th packet, instead of sending another ack.
3023 EXPECT_EQ(0u, writer_
->frame_count());
3024 EXPECT_TRUE(connection_
.GetAckAlarm()->IsSet());
3027 TEST_P(QuicConnectionTest
, SendDelayedAckOnOutgoingPacket
) {
3028 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
3030 connection_
.SendStreamDataWithString(kClientDataStreamId1
, "foo", 0,
3032 // Check that ack is bundled with outgoing data and that delayed ack
3034 if (version() > QUIC_VERSION_15
) {
3035 EXPECT_EQ(3u, writer_
->frame_count());
3036 EXPECT_FALSE(writer_
->stop_waiting_frames().empty());
3038 EXPECT_EQ(2u, writer_
->frame_count());
3040 EXPECT_FALSE(writer_
->ack_frames().empty());
3041 EXPECT_FALSE(connection_
.GetAckAlarm()->IsSet());
3044 TEST_P(QuicConnectionTest
, SendDelayedAckOnOutgoingCryptoPacket
) {
3045 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
3047 connection_
.SendStreamDataWithString(kCryptoStreamId
, "foo", 0, !kFin
, NULL
);
3048 // Check that ack is bundled with outgoing crypto data.
3049 EXPECT_EQ(version() <= QUIC_VERSION_15
? 2u : 3u, writer_
->frame_count());
3050 EXPECT_FALSE(writer_
->ack_frames().empty());
3051 EXPECT_FALSE(connection_
.GetAckAlarm()->IsSet());
3054 TEST_P(QuicConnectionTest
, BundleAckForSecondCHLO
) {
3055 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
3056 EXPECT_FALSE(connection_
.GetAckAlarm()->IsSet());
3057 EXPECT_CALL(visitor_
, OnCanWrite()).WillOnce(
3058 IgnoreResult(InvokeWithoutArgs(&connection_
,
3059 &TestConnection::SendCryptoStreamData
)));
3060 // Process a packet from the crypto stream, which is frame1_'s default.
3061 // Receiving the CHLO as packet 2 first will cause the connection to
3062 // immediately send an ack, due to the packet gap.
3064 // Check that ack is sent and that delayed ack alarm is reset.
3065 if (version() > QUIC_VERSION_15
) {
3066 EXPECT_EQ(3u, writer_
->frame_count());
3067 EXPECT_FALSE(writer_
->stop_waiting_frames().empty());
3069 EXPECT_EQ(2u, writer_
->frame_count());
3071 EXPECT_EQ(1u, writer_
->stream_frames().size());
3072 EXPECT_FALSE(writer_
->ack_frames().empty());
3073 EXPECT_FALSE(connection_
.GetAckAlarm()->IsSet());
3076 TEST_P(QuicConnectionTest
, BundleAckWithDataOnIncomingAck
) {
3077 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
3078 connection_
.SendStreamDataWithString(kClientDataStreamId1
, "foo", 0,
3080 connection_
.SendStreamDataWithString(kClientDataStreamId1
, "foo", 3,
3082 // Ack the second packet, which will retransmit the first packet.
3083 QuicAckFrame ack
= InitAckFrame(2, 0);
3084 NackPacket(1, &ack
);
3085 SequenceNumberSet lost_packets
;
3086 lost_packets
.insert(1);
3087 EXPECT_CALL(*loss_algorithm_
, DetectLostPackets(_
, _
, _
, _
))
3088 .WillOnce(Return(lost_packets
));
3089 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
3090 ProcessAckPacket(&ack
);
3091 EXPECT_EQ(1u, writer_
->frame_count());
3092 EXPECT_EQ(1u, writer_
->stream_frames().size());
3095 // Now ack the retransmission, which will both raise the high water mark
3096 // and see if there is more data to send.
3097 ack
= InitAckFrame(3, 0);
3098 NackPacket(1, &ack
);
3099 EXPECT_CALL(*loss_algorithm_
, DetectLostPackets(_
, _
, _
, _
))
3100 .WillOnce(Return(SequenceNumberSet()));
3101 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
3102 ProcessAckPacket(&ack
);
3104 // Check that no packet is sent and the ack alarm isn't set.
3105 EXPECT_EQ(0u, writer_
->frame_count());
3106 EXPECT_FALSE(connection_
.GetAckAlarm()->IsSet());
3109 // Send the same ack, but send both data and an ack together.
3110 ack
= InitAckFrame(3, 0);
3111 NackPacket(1, &ack
);
3112 EXPECT_CALL(*loss_algorithm_
, DetectLostPackets(_
, _
, _
, _
))
3113 .WillOnce(Return(SequenceNumberSet()));
3114 EXPECT_CALL(visitor_
, OnCanWrite()).WillOnce(
3115 IgnoreResult(InvokeWithoutArgs(
3117 &TestConnection::EnsureWritableAndSendStreamData5
)));
3118 ProcessAckPacket(&ack
);
3120 // Check that ack is bundled with outgoing data and the delayed ack
3122 if (version() > QUIC_VERSION_15
) {
3123 EXPECT_EQ(3u, writer_
->frame_count());
3124 EXPECT_FALSE(writer_
->stop_waiting_frames().empty());
3126 EXPECT_EQ(2u, writer_
->frame_count());
3128 EXPECT_FALSE(writer_
->ack_frames().empty());
3129 EXPECT_EQ(1u, writer_
->stream_frames().size());
3130 EXPECT_FALSE(connection_
.GetAckAlarm()->IsSet());
3133 TEST_P(QuicConnectionTest
, NoAckSentForClose
) {
3134 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
3136 EXPECT_CALL(visitor_
, OnConnectionClosed(QUIC_PEER_GOING_AWAY
, true));
3137 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
)).Times(0);
3138 ProcessClosePacket(2, 0);
3141 TEST_P(QuicConnectionTest
, SendWhenDisconnected
) {
3142 EXPECT_TRUE(connection_
.connected());
3143 EXPECT_CALL(visitor_
, OnConnectionClosed(QUIC_PEER_GOING_AWAY
, false));
3144 connection_
.CloseConnection(QUIC_PEER_GOING_AWAY
, false);
3145 EXPECT_FALSE(connection_
.connected());
3146 QuicPacket
* packet
= ConstructDataPacket(1, 0, !kEntropyFlag
);
3147 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, 1, _
, _
)).Times(0);
3148 connection_
.SendPacket(
3149 ENCRYPTION_NONE
, 1, packet
, kTestEntropyHash
, HAS_RETRANSMITTABLE_DATA
);
3152 TEST_P(QuicConnectionTest
, PublicReset
) {
3153 QuicPublicResetPacket header
;
3154 header
.public_header
.connection_id
= connection_id_
;
3155 header
.public_header
.reset_flag
= true;
3156 header
.public_header
.version_flag
= false;
3157 header
.rejected_sequence_number
= 10101;
3158 scoped_ptr
<QuicEncryptedPacket
> packet(
3159 framer_
.BuildPublicResetPacket(header
));
3160 EXPECT_CALL(visitor_
, OnConnectionClosed(QUIC_PUBLIC_RESET
, true));
3161 connection_
.ProcessUdpPacket(IPEndPoint(), IPEndPoint(), *packet
);
3164 TEST_P(QuicConnectionTest
, GoAway
) {
3165 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
3167 QuicGoAwayFrame goaway
;
3168 goaway
.last_good_stream_id
= 1;
3169 goaway
.error_code
= QUIC_PEER_GOING_AWAY
;
3170 goaway
.reason_phrase
= "Going away.";
3171 EXPECT_CALL(visitor_
, OnGoAway(_
));
3172 ProcessGoAwayPacket(&goaway
);
3175 TEST_P(QuicConnectionTest
, WindowUpdate
) {
3176 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
3178 QuicWindowUpdateFrame window_update
;
3179 window_update
.stream_id
= 3;
3180 window_update
.byte_offset
= 1234;
3181 EXPECT_CALL(visitor_
, OnWindowUpdateFrames(_
));
3182 ProcessFramePacket(QuicFrame(&window_update
));
3185 TEST_P(QuicConnectionTest
, Blocked
) {
3186 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
3188 QuicBlockedFrame blocked
;
3189 blocked
.stream_id
= 3;
3190 EXPECT_CALL(visitor_
, OnBlockedFrames(_
));
3191 ProcessFramePacket(QuicFrame(&blocked
));
3194 TEST_P(QuicConnectionTest
, InvalidPacket
) {
3195 EXPECT_CALL(visitor_
,
3196 OnConnectionClosed(QUIC_INVALID_PACKET_HEADER
, false));
3197 QuicEncryptedPacket
encrypted(NULL
, 0);
3198 connection_
.ProcessUdpPacket(IPEndPoint(), IPEndPoint(), encrypted
);
3199 // The connection close packet should have error details.
3200 ASSERT_FALSE(writer_
->connection_close_frames().empty());
3201 EXPECT_EQ("Unable to read public flags.",
3202 writer_
->connection_close_frames()[0].error_details
);
3205 TEST_P(QuicConnectionTest
, MissingPacketsBeforeLeastUnacked
) {
3206 // Set the sequence number of the ack packet to be least unacked (4).
3207 peer_creator_
.set_sequence_number(3);
3208 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
3209 if (version() > QUIC_VERSION_15
) {
3210 QuicStopWaitingFrame frame
= InitStopWaitingFrame(4);
3211 ProcessStopWaitingPacket(&frame
);
3213 QuicAckFrame ack
= InitAckFrame(0, 4);
3214 ProcessAckPacket(&ack
);
3216 EXPECT_TRUE(outgoing_ack()->received_info
.missing_packets
.empty());
3219 TEST_P(QuicConnectionTest
, ReceivedEntropyHashCalculation
) {
3220 EXPECT_CALL(visitor_
, OnStreamFrames(_
)).Times(AtLeast(1));
3221 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
3222 ProcessDataPacket(1, 1, kEntropyFlag
);
3223 ProcessDataPacket(4, 1, kEntropyFlag
);
3224 ProcessDataPacket(3, 1, !kEntropyFlag
);
3225 ProcessDataPacket(7, 1, kEntropyFlag
);
3226 EXPECT_EQ(146u, outgoing_ack()->received_info
.entropy_hash
);
3229 TEST_P(QuicConnectionTest
, ReceivedEntropyHashCalculationHalfFEC
) {
3230 // FEC packets should not change the entropy hash calculation.
3231 EXPECT_CALL(visitor_
, OnStreamFrames(_
)).Times(AtLeast(1));
3232 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
3233 ProcessDataPacket(1, 1, kEntropyFlag
);
3234 ProcessFecPacket(4, 1, false, kEntropyFlag
, NULL
);
3235 ProcessDataPacket(3, 3, !kEntropyFlag
);
3236 ProcessFecPacket(7, 3, false, kEntropyFlag
, NULL
);
3237 EXPECT_EQ(146u, outgoing_ack()->received_info
.entropy_hash
);
3240 TEST_P(QuicConnectionTest
, UpdateEntropyForReceivedPackets
) {
3241 EXPECT_CALL(visitor_
, OnStreamFrames(_
)).Times(AtLeast(1));
3242 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
3243 ProcessDataPacket(1, 1, kEntropyFlag
);
3244 ProcessDataPacket(5, 1, kEntropyFlag
);
3245 ProcessDataPacket(4, 1, !kEntropyFlag
);
3246 EXPECT_EQ(34u, outgoing_ack()->received_info
.entropy_hash
);
3247 // Make 4th packet my least unacked, and update entropy for 2, 3 packets.
3248 peer_creator_
.set_sequence_number(5);
3249 QuicPacketEntropyHash six_packet_entropy_hash
= 0;
3250 QuicPacketEntropyHash kRandomEntropyHash
= 129u;
3251 if (version() > QUIC_VERSION_15
) {
3252 QuicStopWaitingFrame frame
= InitStopWaitingFrame(4);
3253 frame
.entropy_hash
= kRandomEntropyHash
;
3254 if (ProcessStopWaitingPacket(&frame
)) {
3255 six_packet_entropy_hash
= 1 << 6;
3258 QuicAckFrame ack
= InitAckFrame(0, 4);
3259 ack
.sent_info
.entropy_hash
= kRandomEntropyHash
;
3260 if (ProcessAckPacket(&ack
)) {
3261 six_packet_entropy_hash
= 1 << 6;
3265 EXPECT_EQ((kRandomEntropyHash
+ (1 << 5) + six_packet_entropy_hash
),
3266 outgoing_ack()->received_info
.entropy_hash
);
3269 TEST_P(QuicConnectionTest
, UpdateEntropyHashUptoCurrentPacket
) {
3270 EXPECT_CALL(visitor_
, OnStreamFrames(_
)).Times(AtLeast(1));
3271 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
3272 ProcessDataPacket(1, 1, kEntropyFlag
);
3273 ProcessDataPacket(5, 1, !kEntropyFlag
);
3274 ProcessDataPacket(22, 1, kEntropyFlag
);
3275 EXPECT_EQ(66u, outgoing_ack()->received_info
.entropy_hash
);
3276 peer_creator_
.set_sequence_number(22);
3277 QuicPacketEntropyHash kRandomEntropyHash
= 85u;
3278 // Current packet is the least unacked packet.
3279 QuicPacketEntropyHash ack_entropy_hash
;
3280 if (version() > QUIC_VERSION_15
) {
3281 QuicStopWaitingFrame frame
= InitStopWaitingFrame(23);
3282 frame
.entropy_hash
= kRandomEntropyHash
;
3283 ack_entropy_hash
= ProcessStopWaitingPacket(&frame
);
3285 QuicAckFrame ack
= InitAckFrame(0, 23);
3286 ack
.sent_info
.entropy_hash
= kRandomEntropyHash
;
3287 ack_entropy_hash
= ProcessAckPacket(&ack
);
3289 EXPECT_EQ((kRandomEntropyHash
+ ack_entropy_hash
),
3290 outgoing_ack()->received_info
.entropy_hash
);
3291 ProcessDataPacket(25, 1, kEntropyFlag
);
3292 EXPECT_EQ((kRandomEntropyHash
+ ack_entropy_hash
+ (1 << (25 % 8))),
3293 outgoing_ack()->received_info
.entropy_hash
);
3296 TEST_P(QuicConnectionTest
, EntropyCalculationForTruncatedAck
) {
3297 EXPECT_CALL(visitor_
, OnStreamFrames(_
)).Times(AtLeast(1));
3298 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
3299 QuicPacketEntropyHash entropy
[51];
3301 for (int i
= 1; i
< 51; ++i
) {
3302 bool should_send
= i
% 10 != 1;
3303 bool entropy_flag
= (i
& (i
- 1)) != 0;
3305 entropy
[i
] = entropy
[i
- 1];
3309 entropy
[i
] = entropy
[i
- 1] ^ (1 << (i
% 8));
3311 entropy
[i
] = entropy
[i
- 1];
3313 ProcessDataPacket(i
, 1, entropy_flag
);
3315 for (int i
= 1; i
< 50; ++i
) {
3316 EXPECT_EQ(entropy
[i
], QuicConnectionPeer::ReceivedEntropyHash(
3321 TEST_P(QuicConnectionTest
, CheckSentEntropyHash
) {
3322 peer_creator_
.set_sequence_number(1);
3323 SequenceNumberSet missing_packets
;
3324 QuicPacketEntropyHash entropy_hash
= 0;
3325 QuicPacketSequenceNumber max_sequence_number
= 51;
3326 for (QuicPacketSequenceNumber i
= 1; i
<= max_sequence_number
; ++i
) {
3327 bool is_missing
= i
% 10 != 0;
3328 bool entropy_flag
= (i
& (i
- 1)) != 0;
3329 QuicPacketEntropyHash packet_entropy_hash
= 0;
3331 packet_entropy_hash
= 1 << (i
% 8);
3333 QuicPacket
* packet
= ConstructDataPacket(i
, 0, entropy_flag
);
3334 connection_
.SendPacket(
3335 ENCRYPTION_NONE
, i
, packet
, packet_entropy_hash
,
3336 HAS_RETRANSMITTABLE_DATA
);
3339 missing_packets
.insert(i
);
3343 entropy_hash
^= packet_entropy_hash
;
3345 EXPECT_TRUE(QuicConnectionPeer::IsValidEntropy(
3346 &connection_
, max_sequence_number
, missing_packets
, entropy_hash
))
3350 TEST_P(QuicConnectionTest
, ServerSendsVersionNegotiationPacket
) {
3351 connection_
.SetSupportedVersions(QuicSupportedVersions());
3352 framer_
.set_version_for_tests(QUIC_VERSION_UNSUPPORTED
);
3354 QuicPacketHeader header
;
3355 header
.public_header
.connection_id
= connection_id_
;
3356 header
.public_header
.reset_flag
= false;
3357 header
.public_header
.version_flag
= true;
3358 header
.entropy_flag
= false;
3359 header
.fec_flag
= false;
3360 header
.packet_sequence_number
= 12;
3361 header
.fec_group
= 0;
3364 QuicFrame
frame(&frame1_
);
3365 frames
.push_back(frame
);
3366 scoped_ptr
<QuicPacket
> packet(
3367 BuildUnsizedDataPacket(&framer_
, header
, frames
).packet
);
3368 scoped_ptr
<QuicEncryptedPacket
> encrypted(
3369 framer_
.EncryptPacket(ENCRYPTION_NONE
, 12, *packet
));
3371 framer_
.set_version(version());
3372 connection_
.set_is_server(true);
3373 connection_
.ProcessUdpPacket(IPEndPoint(), IPEndPoint(), *encrypted
);
3374 EXPECT_TRUE(writer_
->version_negotiation_packet() != NULL
);
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
, ServerSendsVersionNegotiationPacketSocketBlocked
) {
3389 connection_
.SetSupportedVersions(QuicSupportedVersions());
3390 framer_
.set_version_for_tests(QUIC_VERSION_UNSUPPORTED
);
3392 QuicPacketHeader header
;
3393 header
.public_header
.connection_id
= connection_id_
;
3394 header
.public_header
.reset_flag
= false;
3395 header
.public_header
.version_flag
= true;
3396 header
.entropy_flag
= false;
3397 header
.fec_flag
= false;
3398 header
.packet_sequence_number
= 12;
3399 header
.fec_group
= 0;
3402 QuicFrame
frame(&frame1_
);
3403 frames
.push_back(frame
);
3404 scoped_ptr
<QuicPacket
> packet(
3405 BuildUnsizedDataPacket(&framer_
, header
, frames
).packet
);
3406 scoped_ptr
<QuicEncryptedPacket
> encrypted(
3407 framer_
.EncryptPacket(ENCRYPTION_NONE
, 12, *packet
));
3409 framer_
.set_version(version());
3410 connection_
.set_is_server(true);
3412 connection_
.ProcessUdpPacket(IPEndPoint(), IPEndPoint(), *encrypted
);
3413 EXPECT_EQ(0u, writer_
->last_packet_size());
3414 EXPECT_TRUE(connection_
.HasQueuedData());
3416 writer_
->SetWritable();
3417 connection_
.OnCanWrite();
3418 EXPECT_TRUE(writer_
->version_negotiation_packet() != NULL
);
3420 size_t num_versions
= arraysize(kSupportedQuicVersions
);
3421 ASSERT_EQ(num_versions
,
3422 writer_
->version_negotiation_packet()->versions
.size());
3424 // We expect all versions in kSupportedQuicVersions to be
3425 // included in the packet.
3426 for (size_t i
= 0; i
< num_versions
; ++i
) {
3427 EXPECT_EQ(kSupportedQuicVersions
[i
],
3428 writer_
->version_negotiation_packet()->versions
[i
]);
3432 TEST_P(QuicConnectionTest
,
3433 ServerSendsVersionNegotiationPacketSocketBlockedDataBuffered
) {
3434 connection_
.SetSupportedVersions(QuicSupportedVersions());
3435 framer_
.set_version_for_tests(QUIC_VERSION_UNSUPPORTED
);
3437 QuicPacketHeader header
;
3438 header
.public_header
.connection_id
= connection_id_
;
3439 header
.public_header
.reset_flag
= false;
3440 header
.public_header
.version_flag
= true;
3441 header
.entropy_flag
= false;
3442 header
.fec_flag
= false;
3443 header
.packet_sequence_number
= 12;
3444 header
.fec_group
= 0;
3447 QuicFrame
frame(&frame1_
);
3448 frames
.push_back(frame
);
3449 scoped_ptr
<QuicPacket
> packet(
3450 BuildUnsizedDataPacket(&framer_
, header
, frames
).packet
);
3451 scoped_ptr
<QuicEncryptedPacket
> encrypted(
3452 framer_
.EncryptPacket(ENCRYPTION_NONE
, 12, *packet
));
3454 framer_
.set_version(version());
3455 connection_
.set_is_server(true);
3457 writer_
->set_is_write_blocked_data_buffered(true);
3458 connection_
.ProcessUdpPacket(IPEndPoint(), IPEndPoint(), *encrypted
);
3459 EXPECT_EQ(0u, writer_
->last_packet_size());
3460 EXPECT_FALSE(connection_
.HasQueuedData());
3463 TEST_P(QuicConnectionTest
, ClientHandlesVersionNegotiation
) {
3464 // Start out with some unsupported version.
3465 QuicConnectionPeer::GetFramer(&connection_
)->set_version_for_tests(
3466 QUIC_VERSION_UNSUPPORTED
);
3468 QuicPacketHeader header
;
3469 header
.public_header
.connection_id
= connection_id_
;
3470 header
.public_header
.reset_flag
= false;
3471 header
.public_header
.version_flag
= true;
3472 header
.entropy_flag
= false;
3473 header
.fec_flag
= false;
3474 header
.packet_sequence_number
= 12;
3475 header
.fec_group
= 0;
3477 QuicVersionVector supported_versions
;
3478 for (size_t i
= 0; i
< arraysize(kSupportedQuicVersions
); ++i
) {
3479 supported_versions
.push_back(kSupportedQuicVersions
[i
]);
3482 // Send a version negotiation packet.
3483 scoped_ptr
<QuicEncryptedPacket
> encrypted(
3484 framer_
.BuildVersionNegotiationPacket(
3485 header
.public_header
, supported_versions
));
3486 connection_
.ProcessUdpPacket(IPEndPoint(), IPEndPoint(), *encrypted
);
3488 // Now force another packet. The connection should transition into
3489 // NEGOTIATED_VERSION state and tell the packet creator to StopSendingVersion.
3490 header
.public_header
.version_flag
= false;
3492 QuicFrame
frame(&frame1_
);
3493 frames
.push_back(frame
);
3494 scoped_ptr
<QuicPacket
> packet(
3495 BuildUnsizedDataPacket(&framer_
, header
, frames
).packet
);
3496 encrypted
.reset(framer_
.EncryptPacket(ENCRYPTION_NONE
, 12, *packet
));
3497 EXPECT_CALL(visitor_
, OnStreamFrames(_
)).Times(1);
3498 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
3499 connection_
.ProcessUdpPacket(IPEndPoint(), IPEndPoint(), *encrypted
);
3501 ASSERT_FALSE(QuicPacketCreatorPeer::SendVersionInPacket(
3502 QuicConnectionPeer::GetPacketCreator(&connection_
)));
3505 TEST_P(QuicConnectionTest
, BadVersionNegotiation
) {
3506 QuicPacketHeader header
;
3507 header
.public_header
.connection_id
= connection_id_
;
3508 header
.public_header
.reset_flag
= false;
3509 header
.public_header
.version_flag
= true;
3510 header
.entropy_flag
= false;
3511 header
.fec_flag
= false;
3512 header
.packet_sequence_number
= 12;
3513 header
.fec_group
= 0;
3515 QuicVersionVector supported_versions
;
3516 for (size_t i
= 0; i
< arraysize(kSupportedQuicVersions
); ++i
) {
3517 supported_versions
.push_back(kSupportedQuicVersions
[i
]);
3520 // Send a version negotiation packet with the version the client started with.
3521 // It should be rejected.
3522 EXPECT_CALL(visitor_
,
3523 OnConnectionClosed(QUIC_INVALID_VERSION_NEGOTIATION_PACKET
,
3525 scoped_ptr
<QuicEncryptedPacket
> encrypted(
3526 framer_
.BuildVersionNegotiationPacket(
3527 header
.public_header
, supported_versions
));
3528 connection_
.ProcessUdpPacket(IPEndPoint(), IPEndPoint(), *encrypted
);
3531 TEST_P(QuicConnectionTest
, CheckSendStats
) {
3532 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
));
3533 connection_
.SendStreamDataWithString(3, "first", 0, !kFin
, NULL
);
3534 size_t first_packet_size
= writer_
->last_packet_size();
3536 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
));
3537 connection_
.SendStreamDataWithString(5, "second", 0, !kFin
, NULL
);
3538 size_t second_packet_size
= writer_
->last_packet_size();
3540 // 2 retransmissions due to rto, 1 due to explicit nack.
3541 EXPECT_CALL(*send_algorithm_
, OnRetransmissionTimeout(true));
3542 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
)).Times(3);
3544 // Retransmit due to RTO.
3545 clock_
.AdvanceTime(QuicTime::Delta::FromSeconds(10));
3546 connection_
.GetRetransmissionAlarm()->Fire();
3548 // Retransmit due to explicit nacks.
3549 QuicAckFrame nack_three
= InitAckFrame(4, 0);
3550 NackPacket(3, &nack_three
);
3551 NackPacket(1, &nack_three
);
3552 SequenceNumberSet lost_packets
;
3553 lost_packets
.insert(1);
3554 lost_packets
.insert(3);
3555 EXPECT_CALL(*loss_algorithm_
, DetectLostPackets(_
, _
, _
, _
))
3556 .WillOnce(Return(lost_packets
));
3557 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
3558 EXPECT_CALL(visitor_
, OnCanWrite()).Times(2);
3559 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
3560 EXPECT_CALL(*send_algorithm_
, RevertRetransmissionTimeout());
3561 ProcessAckPacket(&nack_three
);
3563 EXPECT_CALL(*send_algorithm_
, BandwidthEstimate()).WillOnce(
3564 Return(QuicBandwidth::Zero()));
3566 const QuicConnectionStats
& stats
= connection_
.GetStats();
3567 EXPECT_EQ(3 * first_packet_size
+ 2 * second_packet_size
- kQuicVersionSize
,
3569 EXPECT_EQ(5u, stats
.packets_sent
);
3570 EXPECT_EQ(2 * first_packet_size
+ second_packet_size
- kQuicVersionSize
,
3571 stats
.bytes_retransmitted
);
3572 EXPECT_EQ(3u, stats
.packets_retransmitted
);
3573 EXPECT_EQ(1u, stats
.rto_count
);
3576 TEST_P(QuicConnectionTest
, CheckReceiveStats
) {
3577 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
3579 size_t received_bytes
= 0;
3580 received_bytes
+= ProcessFecProtectedPacket(1, false, !kEntropyFlag
);
3581 received_bytes
+= ProcessFecProtectedPacket(3, false, !kEntropyFlag
);
3582 // Should be counted against dropped packets.
3583 received_bytes
+= ProcessDataPacket(3, 1, !kEntropyFlag
);
3584 received_bytes
+= ProcessFecPacket(4, 1, true, !kEntropyFlag
, NULL
);
3586 EXPECT_CALL(*send_algorithm_
, BandwidthEstimate()).WillOnce(
3587 Return(QuicBandwidth::Zero()));
3589 const QuicConnectionStats
& stats
= connection_
.GetStats();
3590 EXPECT_EQ(received_bytes
, stats
.bytes_received
);
3591 EXPECT_EQ(4u, stats
.packets_received
);
3593 EXPECT_EQ(1u, stats
.packets_revived
);
3594 EXPECT_EQ(1u, stats
.packets_dropped
);
3597 TEST_P(QuicConnectionTest
, TestFecGroupLimits
) {
3598 // Create and return a group for 1.
3599 ASSERT_TRUE(QuicConnectionPeer::GetFecGroup(&connection_
, 1) != NULL
);
3601 // Create and return a group for 2.
3602 ASSERT_TRUE(QuicConnectionPeer::GetFecGroup(&connection_
, 2) != NULL
);
3604 // Create and return a group for 4. This should remove 1 but not 2.
3605 ASSERT_TRUE(QuicConnectionPeer::GetFecGroup(&connection_
, 4) != NULL
);
3606 ASSERT_TRUE(QuicConnectionPeer::GetFecGroup(&connection_
, 1) == NULL
);
3607 ASSERT_TRUE(QuicConnectionPeer::GetFecGroup(&connection_
, 2) != NULL
);
3609 // Create and return a group for 3. This will kill off 2.
3610 ASSERT_TRUE(QuicConnectionPeer::GetFecGroup(&connection_
, 3) != NULL
);
3611 ASSERT_TRUE(QuicConnectionPeer::GetFecGroup(&connection_
, 2) == NULL
);
3613 // Verify that adding 5 kills off 3, despite 4 being created before 3.
3614 ASSERT_TRUE(QuicConnectionPeer::GetFecGroup(&connection_
, 5) != NULL
);
3615 ASSERT_TRUE(QuicConnectionPeer::GetFecGroup(&connection_
, 4) != NULL
);
3616 ASSERT_TRUE(QuicConnectionPeer::GetFecGroup(&connection_
, 3) == NULL
);
3619 TEST_P(QuicConnectionTest
, ProcessFramesIfPacketClosedConnection
) {
3620 // Construct a packet with stream frame and connection close frame.
3621 header_
.public_header
.connection_id
= connection_id_
;
3622 header_
.packet_sequence_number
= 1;
3623 header_
.public_header
.reset_flag
= false;
3624 header_
.public_header
.version_flag
= false;
3625 header_
.entropy_flag
= false;
3626 header_
.fec_flag
= false;
3627 header_
.fec_group
= 0;
3629 QuicConnectionCloseFrame qccf
;
3630 qccf
.error_code
= QUIC_PEER_GOING_AWAY
;
3631 QuicFrame
close_frame(&qccf
);
3632 QuicFrame
stream_frame(&frame1_
);
3635 frames
.push_back(stream_frame
);
3636 frames
.push_back(close_frame
);
3637 scoped_ptr
<QuicPacket
> packet(
3638 BuildUnsizedDataPacket(&framer_
, header_
, frames
).packet
);
3639 EXPECT_TRUE(NULL
!= packet
.get());
3640 scoped_ptr
<QuicEncryptedPacket
> encrypted(framer_
.EncryptPacket(
3641 ENCRYPTION_NONE
, 1, *packet
));
3643 EXPECT_CALL(visitor_
, OnConnectionClosed(QUIC_PEER_GOING_AWAY
, true));
3644 EXPECT_CALL(visitor_
, OnStreamFrames(_
)).Times(1);
3645 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
3647 connection_
.ProcessUdpPacket(IPEndPoint(), IPEndPoint(), *encrypted
);
3650 TEST_P(QuicConnectionTest
, SelectMutualVersion
) {
3651 connection_
.SetSupportedVersions(QuicSupportedVersions());
3652 // Set the connection to speak the lowest quic version.
3653 connection_
.set_version(QuicVersionMin());
3654 EXPECT_EQ(QuicVersionMin(), connection_
.version());
3656 // Pass in available versions which includes a higher mutually supported
3657 // version. The higher mutually supported version should be selected.
3658 QuicVersionVector supported_versions
;
3659 for (size_t i
= 0; i
< arraysize(kSupportedQuicVersions
); ++i
) {
3660 supported_versions
.push_back(kSupportedQuicVersions
[i
]);
3662 EXPECT_TRUE(connection_
.SelectMutualVersion(supported_versions
));
3663 EXPECT_EQ(QuicVersionMax(), connection_
.version());
3665 // Expect that the lowest version is selected.
3666 // Ensure the lowest supported version is less than the max, unless they're
3668 EXPECT_LE(QuicVersionMin(), QuicVersionMax());
3669 QuicVersionVector lowest_version_vector
;
3670 lowest_version_vector
.push_back(QuicVersionMin());
3671 EXPECT_TRUE(connection_
.SelectMutualVersion(lowest_version_vector
));
3672 EXPECT_EQ(QuicVersionMin(), connection_
.version());
3674 // Shouldn't be able to find a mutually supported version.
3675 QuicVersionVector unsupported_version
;
3676 unsupported_version
.push_back(QUIC_VERSION_UNSUPPORTED
);
3677 EXPECT_FALSE(connection_
.SelectMutualVersion(unsupported_version
));
3680 TEST_P(QuicConnectionTest
, ConnectionCloseWhenWritable
) {
3681 EXPECT_FALSE(writer_
->IsWriteBlocked());
3684 connection_
.SendStreamDataWithString(1, "foo", 0, !kFin
, NULL
);
3685 EXPECT_EQ(0u, connection_
.NumQueuedPackets());
3686 EXPECT_EQ(1u, writer_
->packets_write_attempts());
3688 TriggerConnectionClose();
3689 EXPECT_EQ(2u, writer_
->packets_write_attempts());
3692 TEST_P(QuicConnectionTest
, ConnectionCloseGettingWriteBlocked
) {
3694 TriggerConnectionClose();
3695 EXPECT_EQ(1u, writer_
->packets_write_attempts());
3696 EXPECT_TRUE(writer_
->IsWriteBlocked());
3699 TEST_P(QuicConnectionTest
, ConnectionCloseWhenWriteBlocked
) {
3701 connection_
.SendStreamDataWithString(1, "foo", 0, !kFin
, NULL
);
3702 EXPECT_EQ(1u, connection_
.NumQueuedPackets());
3703 EXPECT_EQ(1u, writer_
->packets_write_attempts());
3704 EXPECT_TRUE(writer_
->IsWriteBlocked());
3705 TriggerConnectionClose();
3706 EXPECT_EQ(1u, writer_
->packets_write_attempts());
3709 TEST_P(QuicConnectionTest
, AckNotifierTriggerCallback
) {
3710 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
3712 // Create a delegate which we expect to be called.
3713 scoped_refptr
<MockAckNotifierDelegate
> delegate(new MockAckNotifierDelegate
);
3714 EXPECT_CALL(*delegate
, OnAckNotification(_
, _
, _
, _
, _
)).Times(1);
3716 // Send some data, which will register the delegate to be notified.
3717 connection_
.SendStreamDataWithString(1, "foo", 0, !kFin
, delegate
.get());
3719 // Process an ACK from the server which should trigger the callback.
3720 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
3721 QuicAckFrame frame
= InitAckFrame(1, 0);
3722 ProcessAckPacket(&frame
);
3725 TEST_P(QuicConnectionTest
, AckNotifierFailToTriggerCallback
) {
3726 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
3728 // Create a delegate which we don't expect to be called.
3729 scoped_refptr
<MockAckNotifierDelegate
> delegate(new MockAckNotifierDelegate
);
3730 EXPECT_CALL(*delegate
, OnAckNotification(_
, _
, _
, _
, _
)).Times(0);
3732 // Send some data, which will register the delegate to be notified. This will
3733 // not be ACKed and so the delegate should never be called.
3734 connection_
.SendStreamDataWithString(1, "foo", 0, !kFin
, delegate
.get());
3736 // Send some other data which we will ACK.
3737 connection_
.SendStreamDataWithString(1, "foo", 0, !kFin
, NULL
);
3738 connection_
.SendStreamDataWithString(1, "bar", 0, !kFin
, NULL
);
3740 // Now we receive ACK for packets 2 and 3, but importantly missing packet 1
3741 // which we registered to be notified about.
3742 QuicAckFrame frame
= InitAckFrame(3, 0);
3743 NackPacket(1, &frame
);
3744 SequenceNumberSet lost_packets
;
3745 lost_packets
.insert(1);
3746 EXPECT_CALL(*loss_algorithm_
, DetectLostPackets(_
, _
, _
, _
))
3747 .WillOnce(Return(lost_packets
));
3748 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
3749 ProcessAckPacket(&frame
);
3752 TEST_P(QuicConnectionTest
, AckNotifierCallbackAfterRetransmission
) {
3753 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
3755 // Create a delegate which we expect to be called.
3756 scoped_refptr
<MockAckNotifierDelegate
> delegate(new MockAckNotifierDelegate
);
3757 EXPECT_CALL(*delegate
, OnAckNotification(_
, _
, _
, _
, _
)).Times(1);
3759 // Send four packets, and register to be notified on ACK of packet 2.
3760 connection_
.SendStreamDataWithString(3, "foo", 0, !kFin
, NULL
);
3761 connection_
.SendStreamDataWithString(3, "bar", 0, !kFin
, delegate
.get());
3762 connection_
.SendStreamDataWithString(3, "baz", 0, !kFin
, NULL
);
3763 connection_
.SendStreamDataWithString(3, "qux", 0, !kFin
, NULL
);
3765 // Now we receive ACK for packets 1, 3, and 4 and lose 2.
3766 QuicAckFrame frame
= InitAckFrame(4, 0);
3767 NackPacket(2, &frame
);
3768 SequenceNumberSet lost_packets
;
3769 lost_packets
.insert(2);
3770 EXPECT_CALL(*loss_algorithm_
, DetectLostPackets(_
, _
, _
, _
))
3771 .WillOnce(Return(lost_packets
));
3772 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
3773 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
));
3774 ProcessAckPacket(&frame
);
3776 // Now we get an ACK for packet 5 (retransmitted packet 2), which should
3777 // trigger the callback.
3778 EXPECT_CALL(*loss_algorithm_
, DetectLostPackets(_
, _
, _
, _
))
3779 .WillRepeatedly(Return(SequenceNumberSet()));
3780 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
3781 QuicAckFrame second_ack_frame
= InitAckFrame(5, 0);
3782 ProcessAckPacket(&second_ack_frame
);
3785 // AckNotifierCallback is triggered by the ack of a packet that timed
3786 // out and was retransmitted, even though the retransmission has a
3787 // different sequence number.
3788 TEST_P(QuicConnectionTest
, AckNotifierCallbackForAckAfterRTO
) {
3791 // Create a delegate which we expect to be called.
3792 scoped_refptr
<MockAckNotifierDelegate
> delegate(
3793 new StrictMock
<MockAckNotifierDelegate
>);
3795 QuicTime default_retransmission_time
= clock_
.ApproximateNow().Add(
3796 DefaultRetransmissionTime());
3797 connection_
.SendStreamDataWithString(3, "foo", 0, !kFin
, delegate
.get());
3798 EXPECT_EQ(1u, outgoing_ack()->sent_info
.least_unacked
);
3800 EXPECT_EQ(1u, writer_
->header().packet_sequence_number
);
3801 EXPECT_EQ(default_retransmission_time
,
3802 connection_
.GetRetransmissionAlarm()->deadline());
3803 // Simulate the retransmission alarm firing.
3804 clock_
.AdvanceTime(DefaultRetransmissionTime());
3805 EXPECT_CALL(*send_algorithm_
, OnRetransmissionTimeout(true));
3806 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, 2u, _
, _
));
3807 connection_
.GetRetransmissionAlarm()->Fire();
3808 EXPECT_EQ(2u, writer_
->header().packet_sequence_number
);
3809 // We do not raise the high water mark yet.
3810 EXPECT_EQ(1u, outgoing_ack()->sent_info
.least_unacked
);
3812 // Ack the original packet, which will revert the RTO.
3813 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
3814 EXPECT_CALL(*delegate
, OnAckNotification(1, _
, 1, _
, _
));
3815 EXPECT_CALL(*send_algorithm_
, RevertRetransmissionTimeout());
3816 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
3817 QuicAckFrame ack_frame
= InitAckFrame(1, 0);
3818 ProcessAckPacket(&ack_frame
);
3820 // Delegate is not notified again when the retransmit is acked.
3821 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
3822 QuicAckFrame second_ack_frame
= InitAckFrame(2, 0);
3823 ProcessAckPacket(&second_ack_frame
);
3826 // AckNotifierCallback is triggered by the ack of a packet that was
3827 // previously nacked, even though the retransmission has a different
3829 TEST_P(QuicConnectionTest
, AckNotifierCallbackForAckOfNackedPacket
) {
3832 // Create a delegate which we expect to be called.
3833 scoped_refptr
<MockAckNotifierDelegate
> delegate(
3834 new StrictMock
<MockAckNotifierDelegate
>);
3836 // Send four packets, and register to be notified on ACK of packet 2.
3837 connection_
.SendStreamDataWithString(3, "foo", 0, !kFin
, NULL
);
3838 connection_
.SendStreamDataWithString(3, "bar", 0, !kFin
, delegate
.get());
3839 connection_
.SendStreamDataWithString(3, "baz", 0, !kFin
, NULL
);
3840 connection_
.SendStreamDataWithString(3, "qux", 0, !kFin
, NULL
);
3842 // Now we receive ACK for packets 1, 3, and 4 and lose 2.
3843 QuicAckFrame frame
= InitAckFrame(4, 0);
3844 NackPacket(2, &frame
);
3845 SequenceNumberSet lost_packets
;
3846 lost_packets
.insert(2);
3847 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
3848 EXPECT_CALL(*loss_algorithm_
, DetectLostPackets(_
, _
, _
, _
))
3849 .WillOnce(Return(lost_packets
));
3850 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
3851 EXPECT_CALL(*send_algorithm_
, OnPacketSent(_
, _
, _
, _
, _
));
3852 ProcessAckPacket(&frame
);
3854 // Now we get an ACK for packet 2, which was previously nacked.
3855 SequenceNumberSet no_lost_packets
;
3856 EXPECT_CALL(*delegate
, OnAckNotification(1, _
, 1, _
, _
));
3857 EXPECT_CALL(*loss_algorithm_
, DetectLostPackets(_
, _
, _
, _
))
3858 .WillOnce(Return(no_lost_packets
));
3859 QuicAckFrame second_ack_frame
= InitAckFrame(4, 0);
3860 ProcessAckPacket(&second_ack_frame
);
3862 // Verify that the delegate is not notified again when the
3863 // retransmit is acked.
3864 EXPECT_CALL(*loss_algorithm_
, DetectLostPackets(_
, _
, _
, _
))
3865 .WillOnce(Return(no_lost_packets
));
3866 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
3867 QuicAckFrame third_ack_frame
= InitAckFrame(5, 0);
3868 ProcessAckPacket(&third_ack_frame
);
3871 TEST_P(QuicConnectionTest
, AckNotifierFECTriggerCallback
) {
3872 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
3874 // Create a delegate which we expect to be called.
3875 scoped_refptr
<MockAckNotifierDelegate
> delegate(
3876 new MockAckNotifierDelegate
);
3877 EXPECT_CALL(*delegate
, OnAckNotification(_
, _
, _
, _
, _
)).Times(1);
3879 // Send some data, which will register the delegate to be notified.
3880 connection_
.SendStreamDataWithString(1, "foo", 0, !kFin
, delegate
.get());
3881 connection_
.SendStreamDataWithString(2, "bar", 0, !kFin
, NULL
);
3883 // Process an ACK from the server with a revived packet, which should trigger
3885 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
3886 QuicAckFrame frame
= InitAckFrame(2, 0);
3887 NackPacket(1, &frame
);
3888 frame
.received_info
.revived_packets
.insert(1);
3889 ProcessAckPacket(&frame
);
3890 // If the ack is processed again, the notifier should not be called again.
3891 ProcessAckPacket(&frame
);
3894 TEST_P(QuicConnectionTest
, AckNotifierCallbackAfterFECRecovery
) {
3895 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
3896 EXPECT_CALL(visitor_
, OnCanWrite());
3898 // Create a delegate which we expect to be called.
3899 scoped_refptr
<MockAckNotifierDelegate
> delegate(new MockAckNotifierDelegate
);
3900 EXPECT_CALL(*delegate
, OnAckNotification(_
, _
, _
, _
, _
)).Times(1);
3902 // Expect ACKs for 1 packet.
3903 EXPECT_CALL(*send_algorithm_
, OnCongestionEvent(true, _
, _
, _
));
3905 // Send one packet, and register to be notified on ACK.
3906 connection_
.SendStreamDataWithString(1, "foo", 0, !kFin
, delegate
.get());
3908 // Ack packet gets dropped, but we receive an FEC packet that covers it.
3909 // Should recover the Ack packet and trigger the notification callback.
3912 QuicAckFrame ack_frame
= InitAckFrame(1, 0);
3913 frames
.push_back(QuicFrame(&ack_frame
));
3915 // Dummy stream frame to satisfy expectations set elsewhere.
3916 frames
.push_back(QuicFrame(&frame1_
));
3918 QuicPacketHeader ack_header
;
3919 ack_header
.public_header
.connection_id
= connection_id_
;
3920 ack_header
.public_header
.reset_flag
= false;
3921 ack_header
.public_header
.version_flag
= false;
3922 ack_header
.entropy_flag
= !kEntropyFlag
;
3923 ack_header
.fec_flag
= true;
3924 ack_header
.packet_sequence_number
= 1;
3925 ack_header
.is_in_fec_group
= IN_FEC_GROUP
;
3926 ack_header
.fec_group
= 1;
3928 QuicPacket
* packet
=
3929 BuildUnsizedDataPacket(&framer_
, ack_header
, frames
).packet
;
3931 // Take the packet which contains the ACK frame, and construct and deliver an
3932 // FEC packet which allows the ACK packet to be recovered.
3933 ProcessFecPacket(2, 1, true, !kEntropyFlag
, packet
);
3936 class MockQuicConnectionDebugVisitor
3937 : public QuicConnectionDebugVisitor
{
3939 MOCK_METHOD1(OnFrameAddedToPacket
,
3940 void(const QuicFrame
&));
3942 MOCK_METHOD5(OnPacketSent
,
3943 void(QuicPacketSequenceNumber
,
3946 const QuicEncryptedPacket
&,
3949 MOCK_METHOD2(OnPacketRetransmitted
,
3950 void(QuicPacketSequenceNumber
,
3951 QuicPacketSequenceNumber
));
3953 MOCK_METHOD3(OnPacketReceived
,
3954 void(const IPEndPoint
&,
3956 const QuicEncryptedPacket
&));
3958 MOCK_METHOD1(OnProtocolVersionMismatch
,
3961 MOCK_METHOD1(OnPacketHeader
,
3962 void(const QuicPacketHeader
& header
));
3964 MOCK_METHOD1(OnStreamFrame
,
3965 void(const QuicStreamFrame
&));
3967 MOCK_METHOD1(OnAckFrame
,
3968 void(const QuicAckFrame
& frame
));
3970 MOCK_METHOD1(OnCongestionFeedbackFrame
,
3971 void(const QuicCongestionFeedbackFrame
&));
3973 MOCK_METHOD1(OnStopWaitingFrame
,
3974 void(const QuicStopWaitingFrame
&));
3976 MOCK_METHOD1(OnRstStreamFrame
,
3977 void(const QuicRstStreamFrame
&));
3979 MOCK_METHOD1(OnConnectionCloseFrame
,
3980 void(const QuicConnectionCloseFrame
&));
3982 MOCK_METHOD1(OnPublicResetPacket
,
3983 void(const QuicPublicResetPacket
&));
3985 MOCK_METHOD1(OnVersionNegotiationPacket
,
3986 void(const QuicVersionNegotiationPacket
&));
3988 MOCK_METHOD2(OnRevivedPacket
,
3989 void(const QuicPacketHeader
&, StringPiece payload
));
3992 TEST_P(QuicConnectionTest
, OnPacketHeaderDebugVisitor
) {
3993 QuicPacketHeader header
;
3995 scoped_ptr
<MockQuicConnectionDebugVisitor
>
3996 debug_visitor(new StrictMock
<MockQuicConnectionDebugVisitor
>);
3997 connection_
.set_debug_visitor(debug_visitor
.get());
3998 EXPECT_CALL(*debug_visitor
, OnPacketHeader(Ref(header
))).Times(1);
3999 connection_
.OnPacketHeader(header
);
4002 TEST_P(QuicConnectionTest
, Pacing
) {
4003 ValueRestore
<bool> old_flag(&FLAGS_enable_quic_pacing
, true);
4005 TestConnection
server(connection_id_
, IPEndPoint(), helper_
.get(),
4006 writer_
.get(), true, version());
4007 TestConnection
client(connection_id_
, IPEndPoint(), helper_
.get(),
4008 writer_
.get(), false, version());
4009 EXPECT_TRUE(client
.sent_packet_manager().using_pacing());
4010 EXPECT_FALSE(server
.sent_packet_manager().using_pacing());
4013 TEST_P(QuicConnectionTest
, ControlFramesInstigateAcks
) {
4014 EXPECT_CALL(visitor_
, OnSuccessfulVersionNegotiation(_
));
4016 // Send a WINDOW_UPDATE frame.
4017 QuicWindowUpdateFrame window_update
;
4018 window_update
.stream_id
= 3;
4019 window_update
.byte_offset
= 1234;
4020 EXPECT_CALL(visitor_
, OnWindowUpdateFrames(_
));
4021 ProcessFramePacket(QuicFrame(&window_update
));
4023 // Ensure that this has caused the ACK alarm to be set.
4024 QuicAlarm
* ack_alarm
= QuicConnectionPeer::GetAckAlarm(&connection_
);
4025 EXPECT_TRUE(ack_alarm
->IsSet());
4027 // Cancel alarm, and try again with BLOCKED frame.
4028 ack_alarm
->Cancel();
4029 QuicBlockedFrame blocked
;
4030 blocked
.stream_id
= 3;
4031 EXPECT_CALL(visitor_
, OnBlockedFrames(_
));
4032 ProcessFramePacket(QuicFrame(&blocked
));
4033 EXPECT_TRUE(ack_alarm
->IsSet());