blob: a5c817a2862aedea53b9363e0f702e95cb219a8e [file] [log] [blame]
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "net/quic/quic_connection.h"
#include <ostream>
#include "base/basictypes.h"
#include "base/bind.h"
#include "base/memory/scoped_ptr.h"
#include "base/stl_util.h"
#include "net/base/net_errors.h"
#include "net/quic/congestion_control/loss_detection_interface.h"
#include "net/quic/congestion_control/send_algorithm_interface.h"
#include "net/quic/crypto/null_encrypter.h"
#include "net/quic/crypto/quic_decrypter.h"
#include "net/quic/crypto/quic_encrypter.h"
#include "net/quic/quic_ack_notifier.h"
#include "net/quic/quic_flags.h"
#include "net/quic/quic_protocol.h"
#include "net/quic/quic_utils.h"
#include "net/quic/test_tools/mock_clock.h"
#include "net/quic/test_tools/mock_random.h"
#include "net/quic/test_tools/quic_config_peer.h"
#include "net/quic/test_tools/quic_connection_peer.h"
#include "net/quic/test_tools/quic_framer_peer.h"
#include "net/quic/test_tools/quic_packet_creator_peer.h"
#include "net/quic/test_tools/quic_packet_generator_peer.h"
#include "net/quic/test_tools/quic_sent_packet_manager_peer.h"
#include "net/quic/test_tools/quic_test_utils.h"
#include "net/quic/test_tools/simple_quic_framer.h"
#include "net/test/gtest_util.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
using base::StringPiece;
using std::map;
using std::ostream;
using std::string;
using std::vector;
using testing::AnyNumber;
using testing::AtLeast;
using testing::Contains;
using testing::DoAll;
using testing::InSequence;
using testing::InvokeWithoutArgs;
using testing::NiceMock;
using testing::Ref;
using testing::Return;
using testing::SaveArg;
using testing::StrictMock;
using testing::_;
namespace net {
namespace test {
namespace {
const char data1[] = "foo";
const char data2[] = "bar";
const bool kFin = true;
const bool kEntropyFlag = true;
const QuicPacketEntropyHash kTestEntropyHash = 76;
const int kDefaultRetransmissionTimeMs = 500;
// TaggingEncrypter appends kTagSize bytes of |tag| to the end of each message.
class TaggingEncrypter : public QuicEncrypter {
public:
explicit TaggingEncrypter(uint8 tag)
: tag_(tag) {
}
~TaggingEncrypter() override {}
// QuicEncrypter interface.
bool SetKey(StringPiece key) override { return true; }
bool SetNoncePrefix(StringPiece nonce_prefix) override { return true; }
bool EncryptPacket(QuicPacketSequenceNumber sequence_number,
StringPiece associated_data,
StringPiece plaintext,
char* output,
size_t* output_length,
size_t max_output_length) override {
const size_t len = plaintext.size() + kTagSize;
if (max_output_length < len) {
return false;
}
memcpy(output, plaintext.data(), plaintext.size());
output += plaintext.size();
memset(output, tag_, kTagSize);
*output_length = len;
return true;
}
size_t GetKeySize() const override { return 0; }
size_t GetNoncePrefixSize() const override { return 0; }
size_t GetMaxPlaintextSize(size_t ciphertext_size) const override {
return ciphertext_size - kTagSize;
}
size_t GetCiphertextSize(size_t plaintext_size) const override {
return plaintext_size + kTagSize;
}
StringPiece GetKey() const override { return StringPiece(); }
StringPiece GetNoncePrefix() const override { return StringPiece(); }
private:
enum {
kTagSize = 12,
};
const uint8 tag_;
DISALLOW_COPY_AND_ASSIGN(TaggingEncrypter);
};
// TaggingDecrypter ensures that the final kTagSize bytes of the message all
// have the same value and then removes them.
class TaggingDecrypter : public QuicDecrypter {
public:
~TaggingDecrypter() override {}
// QuicDecrypter interface
bool SetKey(StringPiece key) override { return true; }
bool SetNoncePrefix(StringPiece nonce_prefix) override { return true; }
bool DecryptPacket(QuicPacketSequenceNumber sequence_number,
const StringPiece& associated_data,
const StringPiece& ciphertext,
char* output,
size_t* output_length,
size_t max_output_length) override {
if (ciphertext.size() < kTagSize) {
return false;
}
if (!CheckTag(ciphertext, GetTag(ciphertext))) {
return false;
}
*output_length = ciphertext.size() - kTagSize;
memcpy(output, ciphertext.data(), *output_length);
return true;
}
StringPiece GetKey() const override { return StringPiece(); }
StringPiece GetNoncePrefix() const override { return StringPiece(); }
const char* cipher_name() const override { return "Tagging"; }
// Use a distinct value starting with 0xFFFFFF, which is never used by TLS.
uint32 cipher_id() const override { return 0xFFFFFFF0; }
protected:
virtual uint8 GetTag(StringPiece ciphertext) {
return ciphertext.data()[ciphertext.size()-1];
}
private:
enum {
kTagSize = 12,
};
bool CheckTag(StringPiece ciphertext, uint8 tag) {
for (size_t i = ciphertext.size() - kTagSize; i < ciphertext.size(); i++) {
if (ciphertext.data()[i] != tag) {
return false;
}
}
return true;
}
};
// StringTaggingDecrypter ensures that the final kTagSize bytes of the message
// match the expected value.
class StrictTaggingDecrypter : public TaggingDecrypter {
public:
explicit StrictTaggingDecrypter(uint8 tag) : tag_(tag) {}
~StrictTaggingDecrypter() override {}
// TaggingQuicDecrypter
uint8 GetTag(StringPiece ciphertext) override { return tag_; }
const char* cipher_name() const override { return "StrictTagging"; }
// Use a distinct value starting with 0xFFFFFF, which is never used by TLS.
uint32 cipher_id() const override { return 0xFFFFFFF1; }
private:
const uint8 tag_;
};
class TestConnectionHelper : public QuicConnectionHelperInterface {
public:
class TestAlarm : public QuicAlarm {
public:
explicit TestAlarm(QuicAlarm::Delegate* delegate)
: QuicAlarm(delegate) {
}
void SetImpl() override {}
void CancelImpl() override {}
using QuicAlarm::Fire;
};
TestConnectionHelper(MockClock* clock, MockRandom* random_generator)
: clock_(clock),
random_generator_(random_generator) {
clock_->AdvanceTime(QuicTime::Delta::FromSeconds(1));
}
// QuicConnectionHelperInterface
const QuicClock* GetClock() const override { return clock_; }
QuicRandom* GetRandomGenerator() override { return random_generator_; }
QuicAlarm* CreateAlarm(QuicAlarm::Delegate* delegate) override {
return new TestAlarm(delegate);
}
private:
MockClock* clock_;
MockRandom* random_generator_;
DISALLOW_COPY_AND_ASSIGN(TestConnectionHelper);
};
class TestPacketWriter : public QuicPacketWriter {
public:
TestPacketWriter(QuicVersion version, MockClock *clock)
: version_(version),
framer_(SupportedVersions(version_)),
last_packet_size_(0),
write_blocked_(false),
block_on_next_write_(false),
is_write_blocked_data_buffered_(false),
final_bytes_of_last_packet_(0),
final_bytes_of_previous_packet_(0),
use_tagging_decrypter_(false),
packets_write_attempts_(0),
clock_(clock),
write_pause_time_delta_(QuicTime::Delta::Zero()) {
}
// QuicPacketWriter interface
WriteResult WritePacket(const char* buffer,
size_t buf_len,
const IPAddressNumber& self_address,
const IPEndPoint& peer_address) override {
QuicEncryptedPacket packet(buffer, buf_len);
++packets_write_attempts_;
if (packet.length() >= sizeof(final_bytes_of_last_packet_)) {
final_bytes_of_previous_packet_ = final_bytes_of_last_packet_;
memcpy(&final_bytes_of_last_packet_, packet.data() + packet.length() - 4,
sizeof(final_bytes_of_last_packet_));
}
if (use_tagging_decrypter_) {
framer_.framer()->SetDecrypter(ENCRYPTION_NONE, new TaggingDecrypter);
}
EXPECT_TRUE(framer_.ProcessPacket(packet));
if (block_on_next_write_) {
write_blocked_ = true;
block_on_next_write_ = false;
}
if (IsWriteBlocked()) {
return WriteResult(WRITE_STATUS_BLOCKED, -1);
}
last_packet_size_ = packet.length();
if (!write_pause_time_delta_.IsZero()) {
clock_->AdvanceTime(write_pause_time_delta_);
}
return WriteResult(WRITE_STATUS_OK, last_packet_size_);
}
bool IsWriteBlockedDataBuffered() const override {
return is_write_blocked_data_buffered_;
}
bool IsWriteBlocked() const override { return write_blocked_; }
void SetWritable() override { write_blocked_ = false; }
void BlockOnNextWrite() { block_on_next_write_ = true; }
// Sets the amount of time that the writer should before the actual write.
void SetWritePauseTimeDelta(QuicTime::Delta delta) {
write_pause_time_delta_ = delta;
}
const QuicPacketHeader& header() { return framer_.header(); }
size_t frame_count() const { return framer_.num_frames(); }
const vector<QuicAckFrame>& ack_frames() const {
return framer_.ack_frames();
}
const vector<QuicStopWaitingFrame>& stop_waiting_frames() const {
return framer_.stop_waiting_frames();
}
const vector<QuicConnectionCloseFrame>& connection_close_frames() const {
return framer_.connection_close_frames();
}
const vector<QuicRstStreamFrame>& rst_stream_frames() const {
return framer_.rst_stream_frames();
}
const vector<QuicStreamFrame>& stream_frames() const {
return framer_.stream_frames();
}
const vector<QuicPingFrame>& ping_frames() const {
return framer_.ping_frames();
}
size_t last_packet_size() {
return last_packet_size_;
}
const QuicVersionNegotiationPacket* version_negotiation_packet() {
return framer_.version_negotiation_packet();
}
void set_is_write_blocked_data_buffered(bool buffered) {
is_write_blocked_data_buffered_ = buffered;
}
void set_perspective(Perspective perspective) {
// We invert perspective here, because the framer needs to parse packets
// we send.
perspective = perspective == Perspective::IS_CLIENT
? Perspective::IS_SERVER
: Perspective::IS_CLIENT;
QuicFramerPeer::SetPerspective(framer_.framer(), perspective);
}
// final_bytes_of_last_packet_ returns the last four bytes of the previous
// packet as a little-endian, uint32. This is intended to be used with a
// TaggingEncrypter so that tests can determine which encrypter was used for
// a given packet.
uint32 final_bytes_of_last_packet() { return final_bytes_of_last_packet_; }
// Returns the final bytes of the second to last packet.
uint32 final_bytes_of_previous_packet() {
return final_bytes_of_previous_packet_;
}
void use_tagging_decrypter() {
use_tagging_decrypter_ = true;
}
uint32 packets_write_attempts() { return packets_write_attempts_; }
void Reset() { framer_.Reset(); }
void SetSupportedVersions(const QuicVersionVector& versions) {
framer_.SetSupportedVersions(versions);
}
private:
QuicVersion version_;
SimpleQuicFramer framer_;
size_t last_packet_size_;
bool write_blocked_;
bool block_on_next_write_;
bool is_write_blocked_data_buffered_;
uint32 final_bytes_of_last_packet_;
uint32 final_bytes_of_previous_packet_;
bool use_tagging_decrypter_;
uint32 packets_write_attempts_;
MockClock *clock_;
// If non-zero, the clock will pause during WritePacket for this amount of
// time.
QuicTime::Delta write_pause_time_delta_;
DISALLOW_COPY_AND_ASSIGN(TestPacketWriter);
};
class TestConnection : public QuicConnection {
public:
TestConnection(QuicConnectionId connection_id,
IPEndPoint address,
TestConnectionHelper* helper,
const PacketWriterFactory& factory,
Perspective perspective,
QuicVersion version)
: QuicConnection(connection_id,
address,
helper,
factory,
/* owns_writer= */ false,
perspective,
/* is_secure= */ false,
SupportedVersions(version)) {
// Disable tail loss probes for most tests.
QuicSentPacketManagerPeer::SetMaxTailLossProbes(
QuicConnectionPeer::GetSentPacketManager(this), 0);
writer()->set_perspective(perspective);
}
void SendAck() {
QuicConnectionPeer::SendAck(this);
}
void SetSendAlgorithm(SendAlgorithmInterface* send_algorithm) {
QuicConnectionPeer::SetSendAlgorithm(this, send_algorithm);
}
void SetLossAlgorithm(LossDetectionInterface* loss_algorithm) {
QuicSentPacketManagerPeer::SetLossAlgorithm(
QuicConnectionPeer::GetSentPacketManager(this), loss_algorithm);
}
void SendPacket(EncryptionLevel level,
QuicPacketSequenceNumber sequence_number,
QuicPacket* packet,
QuicPacketEntropyHash entropy_hash,
HasRetransmittableData retransmittable) {
RetransmittableFrames* retransmittable_frames =
retransmittable == HAS_RETRANSMITTABLE_DATA
? new RetransmittableFrames(ENCRYPTION_NONE)
: nullptr;
char buffer[kMaxPacketSize];
QuicEncryptedPacket* encrypted =
QuicConnectionPeer::GetFramer(this)->EncryptPayload(
ENCRYPTION_NONE, sequence_number, *packet, buffer, kMaxPacketSize);
delete packet;
OnSerializedPacket(SerializedPacket(sequence_number,
PACKET_6BYTE_SEQUENCE_NUMBER, encrypted,
entropy_hash, retransmittable_frames));
}
QuicConsumedData SendStreamDataWithString(
QuicStreamId id,
StringPiece data,
QuicStreamOffset offset,
bool fin,
QuicAckNotifier::DelegateInterface* delegate) {
return SendStreamDataWithStringHelper(id, data, offset, fin,
MAY_FEC_PROTECT, delegate);
}
QuicConsumedData SendStreamDataWithStringWithFec(
QuicStreamId id,
StringPiece data,
QuicStreamOffset offset,
bool fin,
QuicAckNotifier::DelegateInterface* delegate) {
return SendStreamDataWithStringHelper(id, data, offset, fin,
MUST_FEC_PROTECT, delegate);
}
QuicConsumedData SendStreamDataWithStringHelper(
QuicStreamId id,
StringPiece data,
QuicStreamOffset offset,
bool fin,
FecProtection fec_protection,
QuicAckNotifier::DelegateInterface* delegate) {
struct iovec iov;
QuicIOVector data_iov(MakeIOVector(data, &iov));
return QuicConnection::SendStreamData(id, data_iov, offset, fin,
fec_protection, delegate);
}
QuicConsumedData SendStreamData3() {
return SendStreamDataWithString(kClientDataStreamId1, "food", 0, !kFin,
nullptr);
}
QuicConsumedData SendStreamData3WithFec() {
return SendStreamDataWithStringWithFec(kClientDataStreamId1, "food", 0,
!kFin, nullptr);
}
QuicConsumedData SendStreamData5() {
return SendStreamDataWithString(kClientDataStreamId2, "food2", 0, !kFin,
nullptr);
}
QuicConsumedData SendStreamData5WithFec() {
return SendStreamDataWithStringWithFec(kClientDataStreamId2, "food2", 0,
!kFin, nullptr);
}
// Ensures the connection can write stream data before writing.
QuicConsumedData EnsureWritableAndSendStreamData5() {
EXPECT_TRUE(CanWriteStreamData());
return SendStreamData5();
}
// The crypto stream has special semantics so that it is not blocked by a
// congestion window limitation, and also so that it gets put into a separate
// packet (so that it is easier to reason about a crypto frame not being
// split needlessly across packet boundaries). As a result, we have separate
// tests for some cases for this stream.
QuicConsumedData SendCryptoStreamData() {
return SendStreamDataWithString(kCryptoStreamId, "chlo", 0, !kFin, nullptr);
}
void set_version(QuicVersion version) {
QuicConnectionPeer::GetFramer(this)->set_version(version);
}
void SetSupportedVersions(const QuicVersionVector& versions) {
QuicConnectionPeer::GetFramer(this)->SetSupportedVersions(versions);
writer()->SetSupportedVersions(versions);
}
void set_perspective(Perspective perspective) {
writer()->set_perspective(perspective);
QuicConnectionPeer::SetPerspective(this, perspective);
}
// Enable path MTU discovery. Assumes that the test is performed from the
// client perspective and the higher value of MTU target is used.
void EnablePathMtuDiscovery(MockSendAlgorithm* send_algorithm) {
ASSERT_EQ(Perspective::IS_CLIENT, perspective());
FLAGS_quic_do_path_mtu_discovery = true;
QuicConfig config;
QuicTagVector connection_options;
connection_options.push_back(kMTUH);
config.SetConnectionOptionsToSend(connection_options);
EXPECT_CALL(*send_algorithm, SetFromConfig(_, _));
SetFromConfig(config);
// Normally, the pacing would be disabled in the test, but calling
// SetFromConfig enables it. Set nearly-infinite bandwidth to make the
// pacing algorithm work.
EXPECT_CALL(*send_algorithm, PacingRate())
.WillRepeatedly(Return(QuicBandwidth::FromKBytesPerSecond(10000)));
}
TestConnectionHelper::TestAlarm* GetAckAlarm() {
return reinterpret_cast<TestConnectionHelper::TestAlarm*>(
QuicConnectionPeer::GetAckAlarm(this));
}
TestConnectionHelper::TestAlarm* GetPingAlarm() {
return reinterpret_cast<TestConnectionHelper::TestAlarm*>(
QuicConnectionPeer::GetPingAlarm(this));
}
TestConnectionHelper::TestAlarm* GetFecAlarm() {
return reinterpret_cast<TestConnectionHelper::TestAlarm*>(
QuicConnectionPeer::GetFecAlarm(this));
}
TestConnectionHelper::TestAlarm* GetResumeWritesAlarm() {
return reinterpret_cast<TestConnectionHelper::TestAlarm*>(
QuicConnectionPeer::GetResumeWritesAlarm(this));
}
TestConnectionHelper::TestAlarm* GetRetransmissionAlarm() {
return reinterpret_cast<TestConnectionHelper::TestAlarm*>(
QuicConnectionPeer::GetRetransmissionAlarm(this));
}
TestConnectionHelper::TestAlarm* GetSendAlarm() {
return reinterpret_cast<TestConnectionHelper::TestAlarm*>(
QuicConnectionPeer::GetSendAlarm(this));
}
TestConnectionHelper::TestAlarm* GetTimeoutAlarm() {
return reinterpret_cast<TestConnectionHelper::TestAlarm*>(
QuicConnectionPeer::GetTimeoutAlarm(this));
}
TestConnectionHelper::TestAlarm* GetMtuDiscoveryAlarm() {
return reinterpret_cast<TestConnectionHelper::TestAlarm*>(
QuicConnectionPeer::GetMtuDiscoveryAlarm(this));
}
using QuicConnection::SelectMutualVersion;
private:
TestPacketWriter* writer() {
return static_cast<TestPacketWriter*>(QuicConnection::writer());
}
DISALLOW_COPY_AND_ASSIGN(TestConnection);
};
// Used for testing packets revived from FEC packets.
class FecQuicConnectionDebugVisitor
: public QuicConnectionDebugVisitor {
public:
void OnRevivedPacket(const QuicPacketHeader& header,
StringPiece data) override {
revived_header_ = header;
}
// Public accessor method.
QuicPacketHeader revived_header() const {
return revived_header_;
}
private:
QuicPacketHeader revived_header_;
};
class MockPacketWriterFactory : public QuicConnection::PacketWriterFactory {
public:
explicit MockPacketWriterFactory(QuicPacketWriter* writer) {
ON_CALL(*this, Create(_)).WillByDefault(Return(writer));
}
~MockPacketWriterFactory() override {}
MOCK_CONST_METHOD1(Create, QuicPacketWriter*(QuicConnection* connection));
};
// Run tests with combinations of {QuicVersion, fec_send_policy}.
struct TestParams {
TestParams(QuicVersion version, FecSendPolicy fec_send_policy)
: version(version), fec_send_policy(fec_send_policy) {}
friend ostream& operator<<(ostream& os, const TestParams& p) {
os << "{ client_version: " << QuicVersionToString(p.version)
<< " fec_send_policy: " << p.fec_send_policy << " }";
return os;
}
QuicVersion version;
FecSendPolicy fec_send_policy;
};
// Constructs various test permutations.
vector<TestParams> GetTestParams() {
vector<TestParams> params;
QuicVersionVector all_supported_versions = QuicSupportedVersions();
for (size_t i = 0; i < all_supported_versions.size(); ++i) {
params.push_back(TestParams(all_supported_versions[i], FEC_ANY_TRIGGER));
params.push_back(TestParams(all_supported_versions[i], FEC_ALARM_TRIGGER));
}
return params;
}
class QuicConnectionTest : public ::testing::TestWithParam<TestParams> {
protected:
QuicConnectionTest()
: connection_id_(42),
framer_(SupportedVersions(version()),
QuicTime::Zero(),
Perspective::IS_CLIENT),
peer_creator_(connection_id_, &framer_, &random_generator_),
send_algorithm_(new StrictMock<MockSendAlgorithm>),
loss_algorithm_(new MockLossAlgorithm()),
helper_(new TestConnectionHelper(&clock_, &random_generator_)),
writer_(new TestPacketWriter(version(), &clock_)),
factory_(writer_.get()),
connection_(connection_id_,
IPEndPoint(),
helper_.get(),
factory_,
Perspective::IS_CLIENT,
version()),
creator_(QuicConnectionPeer::GetPacketCreator(&connection_)),
generator_(QuicConnectionPeer::GetPacketGenerator(&connection_)),
manager_(QuicConnectionPeer::GetSentPacketManager(&connection_)),
frame1_(1, false, 0, StringPiece(data1)),
frame2_(1, false, 3, StringPiece(data2)),
sequence_number_length_(PACKET_6BYTE_SEQUENCE_NUMBER),
connection_id_length_(PACKET_8BYTE_CONNECTION_ID) {
connection_.set_visitor(&visitor_);
connection_.SetSendAlgorithm(send_algorithm_);
connection_.SetLossAlgorithm(loss_algorithm_);
framer_.set_received_entropy_calculator(&entropy_calculator_);
generator_->set_fec_send_policy(GetParam().fec_send_policy);
EXPECT_CALL(
*send_algorithm_, TimeUntilSend(_, _, _)).WillRepeatedly(Return(
QuicTime::Delta::Zero()));
EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _))
.Times(AnyNumber());
EXPECT_CALL(*send_algorithm_, RetransmissionDelay()).WillRepeatedly(
Return(QuicTime::Delta::Zero()));
EXPECT_CALL(*send_algorithm_, GetCongestionWindow()).WillRepeatedly(
Return(kMaxPacketSize));
ON_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _))
.WillByDefault(Return(true));
EXPECT_CALL(*send_algorithm_, HasReliableBandwidthEstimate())
.Times(AnyNumber());
EXPECT_CALL(*send_algorithm_, BandwidthEstimate())
.Times(AnyNumber())
.WillRepeatedly(Return(QuicBandwidth::Zero()));
EXPECT_CALL(*send_algorithm_, InSlowStart()).Times(AnyNumber());
EXPECT_CALL(*send_algorithm_, InRecovery()).Times(AnyNumber());
EXPECT_CALL(visitor_, WillingAndAbleToWrite()).Times(AnyNumber());
EXPECT_CALL(visitor_, HasPendingHandshake()).Times(AnyNumber());
EXPECT_CALL(visitor_, OnCanWrite()).Times(AnyNumber());
EXPECT_CALL(visitor_, HasOpenDynamicStreams())
.WillRepeatedly(Return(false));
EXPECT_CALL(visitor_, OnCongestionWindowChange(_)).Times(AnyNumber());
EXPECT_CALL(*loss_algorithm_, GetLossTimeout())
.WillRepeatedly(Return(QuicTime::Zero()));
EXPECT_CALL(*loss_algorithm_, DetectLostPackets(_, _, _, _))
.WillRepeatedly(Return(SequenceNumberSet()));
}
QuicVersion version() { return GetParam().version; }
QuicAckFrame* outgoing_ack() {
QuicConnectionPeer::PopulateAckFrame(&connection_, &ack_);
return &ack_;
}
QuicStopWaitingFrame* stop_waiting() {
QuicConnectionPeer::PopulateStopWaitingFrame(&connection_, &stop_waiting_);
return &stop_waiting_;
}
QuicPacketSequenceNumber least_unacked() {
if (writer_->stop_waiting_frames().empty()) {
return 0;
}
return writer_->stop_waiting_frames()[0].least_unacked;
}
void use_tagging_decrypter() {
writer_->use_tagging_decrypter();
}
void ProcessPacket(QuicPacketSequenceNumber number) {
EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(1);
ProcessDataPacket(number, 0, !kEntropyFlag);
}
QuicPacketEntropyHash ProcessFramePacket(QuicFrame frame) {
QuicFrames frames;
frames.push_back(QuicFrame(frame));
QuicPacketCreatorPeer::SetSendVersionInPacket(
&peer_creator_, connection_.perspective() == Perspective::IS_SERVER);
char buffer[kMaxPacketSize];
SerializedPacket serialized_packet =
peer_creator_.SerializeAllFrames(frames, buffer, kMaxPacketSize);
scoped_ptr<QuicEncryptedPacket> encrypted(serialized_packet.packet);
connection_.ProcessUdpPacket(IPEndPoint(), IPEndPoint(), *encrypted);
return serialized_packet.entropy_hash;
}
size_t ProcessDataPacket(QuicPacketSequenceNumber number,
QuicFecGroupNumber fec_group,
bool entropy_flag) {
return ProcessDataPacketAtLevel(number, fec_group, entropy_flag,
ENCRYPTION_NONE);
}
size_t ProcessDataPacketAtLevel(QuicPacketSequenceNumber number,
QuicFecGroupNumber fec_group,
bool entropy_flag,
EncryptionLevel level) {
scoped_ptr<QuicPacket> packet(ConstructDataPacket(number, fec_group,
entropy_flag));
char buffer[kMaxPacketSize];
scoped_ptr<QuicEncryptedPacket> encrypted(
framer_.EncryptPayload(level, number, *packet, buffer, kMaxPacketSize));
connection_.ProcessUdpPacket(IPEndPoint(), IPEndPoint(), *encrypted);
return encrypted->length();
}
void ProcessClosePacket(QuicPacketSequenceNumber number,
QuicFecGroupNumber fec_group) {
scoped_ptr<QuicPacket> packet(ConstructClosePacket(number, fec_group));
char buffer[kMaxPacketSize];
scoped_ptr<QuicEncryptedPacket> encrypted(framer_.EncryptPayload(
ENCRYPTION_NONE, number, *packet, buffer, kMaxPacketSize));
connection_.ProcessUdpPacket(IPEndPoint(), IPEndPoint(), *encrypted);
}
size_t ProcessFecProtectedPacket(QuicPacketSequenceNumber number,
bool expect_revival, bool entropy_flag) {
if (expect_revival) {
EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(1);
}
EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(1).RetiresOnSaturation();
return ProcessDataPacket(number, 1, entropy_flag);
}
// Processes an FEC packet that covers the packets that would have been
// received.
size_t ProcessFecPacket(QuicPacketSequenceNumber number,
QuicPacketSequenceNumber min_protected_packet,
bool expect_revival,
bool entropy_flag,
QuicPacket* packet) {
if (expect_revival) {
EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(1);
}
// Construct the decrypted data packet so we can compute the correct
// redundancy. If |packet| has been provided then use that, otherwise
// construct a default data packet.
scoped_ptr<QuicPacket> data_packet;
if (packet) {
data_packet.reset(packet);
} else {
data_packet.reset(ConstructDataPacket(number, 1, !kEntropyFlag));
}
QuicPacketHeader header;
header.public_header.connection_id = connection_id_;
header.public_header.sequence_number_length = sequence_number_length_;
header.public_header.connection_id_length = connection_id_length_;
header.packet_sequence_number = number;
header.entropy_flag = entropy_flag;
header.fec_flag = true;
header.is_in_fec_group = IN_FEC_GROUP;
header.fec_group = min_protected_packet;
QuicFecData fec_data;
fec_data.fec_group = header.fec_group;
// Since all data packets in this test have the same payload, the
// redundancy is either equal to that payload or the xor of that payload
// with itself, depending on the number of packets.
if (((number - min_protected_packet) % 2) == 0) {
for (size_t i = GetStartOfFecProtectedData(
header.public_header.connection_id_length,
header.public_header.version_flag,
header.public_header.sequence_number_length);
i < data_packet->length(); ++i) {
data_packet->mutable_data()[i] ^= data_packet->data()[i];
}
}
fec_data.redundancy = data_packet->FecProtectedData();
scoped_ptr<QuicPacket> fec_packet(framer_.BuildFecPacket(header, fec_data));
char buffer[kMaxPacketSize];
scoped_ptr<QuicEncryptedPacket> encrypted(framer_.EncryptPayload(
ENCRYPTION_NONE, number, *fec_packet, buffer, kMaxPacketSize));
connection_.ProcessUdpPacket(IPEndPoint(), IPEndPoint(), *encrypted);
return encrypted->length();
}
QuicByteCount SendStreamDataToPeer(QuicStreamId id,
StringPiece data,
QuicStreamOffset offset,
bool fin,
QuicPacketSequenceNumber* last_packet) {
QuicByteCount packet_size;
EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _))
.WillOnce(DoAll(SaveArg<3>(&packet_size), Return(true)));
connection_.SendStreamDataWithString(id, data, offset, fin, nullptr);
if (last_packet != nullptr) {
*last_packet = creator_->sequence_number();
}
EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _))
.Times(AnyNumber());
return packet_size;
}
void SendAckPacketToPeer() {
EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1);
connection_.SendAck();
EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _))
.Times(AnyNumber());
}
void ProcessAckPacket(QuicPacketSequenceNumber packet_number,
QuicAckFrame* frame) {
QuicPacketCreatorPeer::SetSequenceNumber(&peer_creator_, packet_number - 1);
ProcessFramePacket(QuicFrame(frame));
}
QuicPacketEntropyHash ProcessAckPacket(QuicAckFrame* frame) {
return ProcessFramePacket(QuicFrame(frame));
}
QuicPacketEntropyHash ProcessStopWaitingPacket(QuicStopWaitingFrame* frame) {
return ProcessFramePacket(QuicFrame(frame));
}
QuicPacketEntropyHash ProcessGoAwayPacket(QuicGoAwayFrame* frame) {
return ProcessFramePacket(QuicFrame(frame));
}
bool IsMissing(QuicPacketSequenceNumber number) {
return IsAwaitingPacket(*outgoing_ack(), number);
}
QuicPacket* ConstructPacket(QuicPacketHeader header, QuicFrames frames) {
QuicPacket* packet = BuildUnsizedDataPacket(&framer_, header, frames);
EXPECT_NE(nullptr, packet);
return packet;
}
QuicPacket* ConstructDataPacket(QuicPacketSequenceNumber number,
QuicFecGroupNumber fec_group,
bool entropy_flag) {
QuicPacketHeader header;
header.public_header.connection_id = connection_id_;
header.public_header.sequence_number_length = sequence_number_length_;
header.public_header.connection_id_length = connection_id_length_;
header.entropy_flag = entropy_flag;
header.packet_sequence_number = number;
header.is_in_fec_group = fec_group == 0u ? NOT_IN_FEC_GROUP : IN_FEC_GROUP;
header.fec_group = fec_group;
QuicFrames frames;
frames.push_back(QuicFrame(&frame1_));
return ConstructPacket(header, frames);
}
QuicPacket* ConstructClosePacket(QuicPacketSequenceNumber number,
QuicFecGroupNumber fec_group) {
QuicPacketHeader header;
header.public_header.connection_id = connection_id_;
header.packet_sequence_number = number;
header.is_in_fec_group = fec_group == 0u ? NOT_IN_FEC_GROUP : IN_FEC_GROUP;
header.fec_group = fec_group;
QuicConnectionCloseFrame qccf;
qccf.error_code = QUIC_PEER_GOING_AWAY;
QuicFrames frames;
frames.push_back(QuicFrame(&qccf));
return ConstructPacket(header, frames);
}
QuicTime::Delta DefaultRetransmissionTime() {
return QuicTime::Delta::FromMilliseconds(kDefaultRetransmissionTimeMs);
}
QuicTime::Delta DefaultDelayedAckTime() {
return QuicTime::Delta::FromMilliseconds(kMaxDelayedAckTimeMs);
}
// Initialize a frame acknowledging all packets up to largest_observed.
const QuicAckFrame InitAckFrame(QuicPacketSequenceNumber largest_observed) {
QuicAckFrame frame(MakeAckFrame(largest_observed));
if (largest_observed > 0) {
frame.entropy_hash =
QuicConnectionPeer::GetSentEntropyHash(&connection_,
largest_observed);
}
return frame;
}
const QuicStopWaitingFrame InitStopWaitingFrame(
QuicPacketSequenceNumber least_unacked) {
QuicStopWaitingFrame frame;
frame.least_unacked = least_unacked;
return frame;
}
// Explicitly nack a packet.
void NackPacket(QuicPacketSequenceNumber missing, QuicAckFrame* frame) {
frame->missing_packets.insert(missing);
frame->entropy_hash ^=
QuicConnectionPeer::PacketEntropy(&connection_, missing);
}
// Undo nacking a packet within the frame.
void AckPacket(QuicPacketSequenceNumber arrived, QuicAckFrame* frame) {
EXPECT_THAT(frame->missing_packets, Contains(arrived));
frame->missing_packets.erase(arrived);
frame->entropy_hash ^=
QuicConnectionPeer::PacketEntropy(&connection_, arrived);
}
void TriggerConnectionClose() {
// Send an erroneous packet to close the connection.
EXPECT_CALL(visitor_,
OnConnectionClosed(QUIC_INVALID_PACKET_HEADER, false));
// Call ProcessDataPacket rather than ProcessPacket, as we should not get a
// packet call to the visitor.
ProcessDataPacket(6000, 0, !kEntropyFlag);
EXPECT_FALSE(QuicConnectionPeer::GetConnectionClosePacket(&connection_) ==
nullptr);
}
void BlockOnNextWrite() {
writer_->BlockOnNextWrite();
EXPECT_CALL(visitor_, OnWriteBlocked()).Times(AtLeast(1));
}
void SetWritePauseTimeDelta(QuicTime::Delta delta) {
writer_->SetWritePauseTimeDelta(delta);
}
void CongestionBlockWrites() {
EXPECT_CALL(*send_algorithm_,
TimeUntilSend(_, _, _)).WillRepeatedly(
testing::Return(QuicTime::Delta::FromSeconds(1)));
}
void CongestionUnblockWrites() {
EXPECT_CALL(*send_algorithm_,
TimeUntilSend(_, _, _)).WillRepeatedly(
testing::Return(QuicTime::Delta::Zero()));
}
QuicConnectionId connection_id_;
QuicFramer framer_;
QuicPacketCreator peer_creator_;
MockEntropyCalculator entropy_calculator_;
MockSendAlgorithm* send_algorithm_;
MockLossAlgorithm* loss_algorithm_;
MockClock clock_;
MockRandom random_generator_;
scoped_ptr<TestConnectionHelper> helper_;
scoped_ptr<TestPacketWriter> writer_;
NiceMock<MockPacketWriterFactory> factory_;
TestConnection connection_;
QuicPacketCreator* creator_;
QuicPacketGenerator* generator_;
QuicSentPacketManager* manager_;
StrictMock<MockConnectionVisitor> visitor_;
QuicStreamFrame frame1_;
QuicStreamFrame frame2_;
QuicAckFrame ack_;
QuicStopWaitingFrame stop_waiting_;
QuicSequenceNumberLength sequence_number_length_;
QuicConnectionIdLength connection_id_length_;
private:
DISALLOW_COPY_AND_ASSIGN(QuicConnectionTest);
};
// Run all end to end tests with all supported versions.
INSTANTIATE_TEST_CASE_P(SupportedVersion,
QuicConnectionTest,
::testing::ValuesIn(GetTestParams()));
TEST_P(QuicConnectionTest, MaxPacketSize) {
EXPECT_EQ(Perspective::IS_CLIENT, connection_.perspective());
EXPECT_EQ(1350u, connection_.max_packet_length());
}
TEST_P(QuicConnectionTest, SmallerServerMaxPacketSize) {
QuicConnectionId connection_id = 42;
TestConnection connection(connection_id, IPEndPoint(), helper_.get(),
factory_, Perspective::IS_SERVER, version());
EXPECT_EQ(Perspective::IS_SERVER, connection.perspective());
EXPECT_EQ(1000u, connection.max_packet_length());
}
TEST_P(QuicConnectionTest, IncreaseServerMaxPacketSize) {
EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_));
connection_.set_perspective(Perspective::IS_SERVER);
connection_.set_max_packet_length(1000);
QuicPacketHeader header;
header.public_header.connection_id = connection_id_;
header.public_header.version_flag = true;
header.packet_sequence_number = 1;
QuicFrames frames;
QuicPaddingFrame padding;
frames.push_back(QuicFrame(&frame1_));
frames.push_back(QuicFrame(&padding));
scoped_ptr<QuicPacket> packet(ConstructPacket(header, frames));
char buffer[kMaxPacketSize];
scoped_ptr<QuicEncryptedPacket> encrypted(framer_.EncryptPayload(
ENCRYPTION_NONE, 12, *packet, buffer, kMaxPacketSize));
EXPECT_EQ(kMaxPacketSize, encrypted->length());
framer_.set_version(version());
EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(1);
connection_.ProcessUdpPacket(IPEndPoint(), IPEndPoint(), *encrypted);
EXPECT_EQ(kMaxPacketSize, connection_.max_packet_length());
}
TEST_P(QuicConnectionTest, PacketsInOrder) {
EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_));
ProcessPacket(1);
EXPECT_EQ(1u, outgoing_ack()->largest_observed);
EXPECT_EQ(0u, outgoing_ack()->missing_packets.size());
ProcessPacket(2);
EXPECT_EQ(2u, outgoing_ack()->largest_observed);
EXPECT_EQ(0u, outgoing_ack()->missing_packets.size());
ProcessPacket(3);
EXPECT_EQ(3u, outgoing_ack()->largest_observed);
EXPECT_EQ(0u, outgoing_ack()->missing_packets.size());
}
TEST_P(QuicConnectionTest, PacketsOutOfOrder) {
EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_));
ProcessPacket(3);
EXPECT_EQ(3u, outgoing_ack()->largest_observed);
EXPECT_TRUE(IsMissing(2));
EXPECT_TRUE(IsMissing(1));
ProcessPacket(2);
EXPECT_EQ(3u, outgoing_ack()->largest_observed);
EXPECT_FALSE(IsMissing(2));
EXPECT_TRUE(IsMissing(1));
ProcessPacket(1);
EXPECT_EQ(3u, outgoing_ack()->largest_observed);
EXPECT_FALSE(IsMissing(2));
EXPECT_FALSE(IsMissing(1));
}
TEST_P(QuicConnectionTest, DuplicatePacket) {
EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_));
ProcessPacket(3);
EXPECT_EQ(3u, outgoing_ack()->largest_observed);
EXPECT_TRUE(IsMissing(2));
EXPECT_TRUE(IsMissing(1));
// Send packet 3 again, but do not set the expectation that
// the visitor OnStreamFrame() will be called.
ProcessDataPacket(3, 0, !kEntropyFlag);
EXPECT_EQ(3u, outgoing_ack()->largest_observed);
EXPECT_TRUE(IsMissing(2));
EXPECT_TRUE(IsMissing(1));
}
TEST_P(QuicConnectionTest, PacketsOutOfOrderWithAdditionsAndLeastAwaiting) {
EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_));
ProcessPacket(3);
EXPECT_EQ(3u, outgoing_ack()->largest_observed);
EXPECT_TRUE(IsMissing(2));
EXPECT_TRUE(IsMissing(1));
ProcessPacket(2);
EXPECT_EQ(3u, outgoing_ack()->largest_observed);
EXPECT_TRUE(IsMissing(1));
ProcessPacket(5);
EXPECT_EQ(5u, outgoing_ack()->largest_observed);
EXPECT_TRUE(IsMissing(1));
EXPECT_TRUE(IsMissing(4));
// Pretend at this point the client has gotten acks for 2 and 3 and 1 is a
// packet the peer will not retransmit. It indicates this by sending 'least
// awaiting' is 4. The connection should then realize 1 will not be
// retransmitted, and will remove it from the missing list.
QuicAckFrame frame = InitAckFrame(1);
EXPECT_CALL(*send_algorithm_, OnCongestionEvent(_, _, _, _));
ProcessAckPacket(6, &frame);
// Force an ack to be sent.
SendAckPacketToPeer();
EXPECT_TRUE(IsMissing(4));
}
TEST_P(QuicConnectionTest, RejectPacketTooFarOut) {
EXPECT_CALL(visitor_,
OnConnectionClosed(QUIC_INVALID_PACKET_HEADER, false));
// Call ProcessDataPacket rather than ProcessPacket, as we should not get a
// packet call to the visitor.
ProcessDataPacket(6000, 0, !kEntropyFlag);
EXPECT_FALSE(QuicConnectionPeer::GetConnectionClosePacket(&connection_) ==
nullptr);
}
TEST_P(QuicConnectionTest, RejectUnencryptedStreamData) {
// Process an unencrypted packet from the non-crypto stream.
frame1_.stream_id = 3;
EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_));
EXPECT_CALL(visitor_, OnConnectionClosed(QUIC_UNENCRYPTED_STREAM_DATA,
false));
ProcessDataPacket(1, 0, !kEntropyFlag);
EXPECT_FALSE(QuicConnectionPeer::GetConnectionClosePacket(&connection_) ==
nullptr);
const vector<QuicConnectionCloseFrame>& connection_close_frames =
writer_->connection_close_frames();
EXPECT_EQ(1u, connection_close_frames.size());
EXPECT_EQ(QUIC_UNENCRYPTED_STREAM_DATA,
connection_close_frames[0].error_code);
}
TEST_P(QuicConnectionTest, TruncatedAck) {
EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_));
QuicPacketSequenceNumber num_packets = 256 * 2 + 1;
for (QuicPacketSequenceNumber i = 0; i < num_packets; ++i) {
SendStreamDataToPeer(3, "foo", i * 3, !kFin, nullptr);
}
QuicAckFrame frame = InitAckFrame(num_packets);
SequenceNumberSet lost_packets;
// Create an ack with 256 nacks, none adjacent to one another.
for (QuicPacketSequenceNumber i = 1; i <= 256; ++i) {
NackPacket(i * 2, &frame);
if (i < 256) { // Last packet is nacked, but not lost.
lost_packets.insert(i * 2);
}
}
EXPECT_CALL(*loss_algorithm_, DetectLostPackets(_, _, _, _))
.WillOnce(Return(lost_packets));
EXPECT_CALL(entropy_calculator_, EntropyHash(511))
.WillOnce(Return(static_cast<QuicPacketEntropyHash>(0)));
EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _));
ProcessAckPacket(&frame);
// A truncated ack will not have the true largest observed.
EXPECT_GT(num_packets, manager_->largest_observed());
AckPacket(192, &frame);
// Removing one missing packet allows us to ack 192 and one more range, but
// 192 has already been declared lost, so it doesn't register as an ack.
EXPECT_CALL(*loss_algorithm_, DetectLostPackets(_, _, _, _))
.WillOnce(Return(SequenceNumberSet()));
EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _));
ProcessAckPacket(&frame);
EXPECT_EQ(num_packets, manager_->largest_observed());
}
TEST_P(QuicConnectionTest, AckReceiptCausesAckSendBadEntropy) {
EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_));
ProcessPacket(1);
// Delay sending, then queue up an ack.
EXPECT_CALL(*send_algorithm_,
TimeUntilSend(_, _, _)).WillOnce(
testing::Return(QuicTime::Delta::FromMicroseconds(1)));
QuicConnectionPeer::SendAck(&connection_);
// Process an ack with a least unacked of the received ack.
// This causes an ack to be sent when TimeUntilSend returns 0.
EXPECT_CALL(*send_algorithm_,
TimeUntilSend(_, _, _)).WillRepeatedly(
testing::Return(QuicTime::Delta::Zero()));
// Skip a packet and then record an ack.
QuicAckFrame frame = InitAckFrame(0);
ProcessAckPacket(3, &frame);
}
TEST_P(QuicConnectionTest, OutOfOrderReceiptCausesAckSend) {
EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_));
ProcessPacket(3);
// Should ack immediately since we have missing packets.
EXPECT_EQ(1u, writer_->packets_write_attempts());
ProcessPacket(2);
// Should ack immediately since we have missing packets.
EXPECT_EQ(2u, writer_->packets_write_attempts());
ProcessPacket(1);
// Should ack immediately, since this fills the last hole.
EXPECT_EQ(3u, writer_->packets_write_attempts());
ProcessPacket(4);
// Should not cause an ack.
EXPECT_EQ(3u, writer_->packets_write_attempts());
}
TEST_P(QuicConnectionTest, OutOfOrderAckReceiptCausesNoAck) {
EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_));
SendStreamDataToPeer(1, "foo", 0, !kFin, nullptr);
SendStreamDataToPeer(1, "bar", 3, !kFin, nullptr);
EXPECT_EQ(2u, writer_->packets_write_attempts());
QuicAckFrame ack1 = InitAckFrame(1);
QuicAckFrame ack2 = InitAckFrame(2);
EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _));
ProcessAckPacket(2, &ack2);
// Should ack immediately since we have missing packets.
EXPECT_EQ(2u, writer_->packets_write_attempts());
ProcessAckPacket(1, &ack1);
// Should not ack an ack filling a missing packet.
EXPECT_EQ(2u, writer_->packets_write_attempts());
}
TEST_P(QuicConnectionTest, AckReceiptCausesAckSend) {
EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_));
QuicPacketSequenceNumber original;
QuicByteCount packet_size;
EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _))
.WillOnce(DoAll(SaveArg<2>(&original), SaveArg<3>(&packet_size),
Return(true)));
connection_.SendStreamDataWithString(3, "foo", 0, !kFin, nullptr);
QuicAckFrame frame = InitAckFrame(original);
NackPacket(original, &frame);
// First nack triggers early retransmit.
SequenceNumberSet lost_packets;
lost_packets.insert(1);
EXPECT_CALL(*loss_algorithm_, DetectLostPackets(_, _, _, _))
.WillOnce(Return(lost_packets));
EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _));
QuicPacketSequenceNumber retransmission;
EXPECT_CALL(*send_algorithm_,
OnPacketSent(_, _, _, packet_size - kQuicVersionSize, _))
.WillOnce(DoAll(SaveArg<2>(&retransmission), Return(true)));
ProcessAckPacket(&frame);
QuicAckFrame frame2 = InitAckFrame(retransmission);
NackPacket(original, &frame2);
EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _));
EXPECT_CALL(*loss_algorithm_, DetectLostPackets(_, _, _, _))
.WillOnce(Return(SequenceNumberSet()));
ProcessAckPacket(&frame2);
// Now if the peer sends an ack which still reports the retransmitted packet
// as missing, that will bundle an ack with data after two acks in a row
// indicate the high water mark needs to be raised.
EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _,
HAS_RETRANSMITTABLE_DATA));
connection_.SendStreamDataWithString(3, "foo", 3, !kFin, nullptr);
// No ack sent.
EXPECT_EQ(1u, writer_->frame_count());
EXPECT_EQ(1u, writer_->stream_frames().size());
// No more packet loss for the rest of the test.
EXPECT_CALL(*loss_algorithm_, DetectLostPackets(_, _, _, _))
.WillRepeatedly(Return(SequenceNumberSet()));
ProcessAckPacket(&frame2);
EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _,
HAS_RETRANSMITTABLE_DATA));
connection_.SendStreamDataWithString(3, "foo", 3, !kFin, nullptr);
// Ack bundled.
EXPECT_EQ(3u, writer_->frame_count());
EXPECT_EQ(1u, writer_->stream_frames().size());
EXPECT_FALSE(writer_->ack_frames().empty());
// But an ack with no missing packets will not send an ack.
AckPacket(original, &frame2);
ProcessAckPacket(&frame2);
ProcessAckPacket(&frame2);
}
TEST_P(QuicConnectionTest, 20AcksCausesAckSend) {
EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_));
SendStreamDataToPeer(1, "foo", 0, !kFin, nullptr);
QuicAlarm* ack_alarm = QuicConnectionPeer::GetAckAlarm(&connection_);
// But an ack with no missing packets will not send an ack.
QuicAckFrame frame = InitAckFrame(1);
EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _));
EXPECT_CALL(*loss_algorithm_, DetectLostPackets(_, _, _, _))
.WillRepeatedly(Return(SequenceNumberSet()));
for (int i = 0; i < 20; ++i) {
EXPECT_FALSE(ack_alarm->IsSet());
ProcessAckPacket(&frame);
}
EXPECT_TRUE(ack_alarm->IsSet());
}
TEST_P(QuicConnectionTest, LeastUnackedLower) {
EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_));
SendStreamDataToPeer(1, "foo", 0, !kFin, nullptr);
SendStreamDataToPeer(1, "bar", 3, !kFin, nullptr);
SendStreamDataToPeer(1, "eep", 6, !kFin, nullptr);
// Start out saying the least unacked is 2.
QuicPacketCreatorPeer::SetSequenceNumber(&peer_creator_, 5);
QuicStopWaitingFrame frame = InitStopWaitingFrame(2);
ProcessStopWaitingPacket(&frame);
// Change it to 1, but lower the sequence number to fake out-of-order packets.
// This should be fine.
QuicPacketCreatorPeer::SetSequenceNumber(&peer_creator_, 1);
// The scheduler will not process out of order acks, but all packet processing
// causes the connection to try to write.
EXPECT_CALL(visitor_, OnCanWrite());
QuicStopWaitingFrame frame2 = InitStopWaitingFrame(1);
ProcessStopWaitingPacket(&frame2);
// Now claim it's one, but set the ordering so it was sent "after" the first
// one. This should cause a connection error.
EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _));
QuicPacketCreatorPeer::SetSequenceNumber(&peer_creator_, 7);
EXPECT_CALL(visitor_,
OnConnectionClosed(QUIC_INVALID_STOP_WAITING_DATA, false));
QuicStopWaitingFrame frame3 = InitStopWaitingFrame(1);
ProcessStopWaitingPacket(&frame3);
}
TEST_P(QuicConnectionTest, TooManySentPackets) {
EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_));
const int num_packets = kMaxTrackedPackets + 100;
for (int i = 0; i < num_packets; ++i) {
SendStreamDataToPeer(1, "foo", 3 * i, !kFin, nullptr);
}
// Ack packet 1, which leaves more than the limit outstanding.
EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _));
EXPECT_CALL(visitor_, OnConnectionClosed(
QUIC_TOO_MANY_OUTSTANDING_SENT_PACKETS, false));
// We're receive buffer limited, so the connection won't try to write more.
EXPECT_CALL(visitor_, OnCanWrite()).Times(0);
// Nack the first packet and ack the rest, leaving a huge gap.
QuicAckFrame frame1 = InitAckFrame(num_packets);
NackPacket(1, &frame1);
ProcessAckPacket(&frame1);
}
TEST_P(QuicConnectionTest, TooManyReceivedPackets) {
EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_));
EXPECT_CALL(visitor_, OnConnectionClosed(
QUIC_TOO_MANY_OUTSTANDING_RECEIVED_PACKETS, false));
// Miss 99 of every 100 packets for 5500 packets.
for (QuicPacketSequenceNumber i = 1; i < kMaxTrackedPackets + 500; i += 100) {
ProcessPacket(i);
if (!connection_.connected()) {
break;
}
}
}
TEST_P(QuicConnectionTest, LargestObservedLower) {
EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_));
SendStreamDataToPeer(1, "foo", 0, !kFin, nullptr);
SendStreamDataToPeer(1, "bar", 3, !kFin, nullptr);
SendStreamDataToPeer(1, "eep", 6, !kFin, nullptr);
EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _));
// Start out saying the largest observed is 2.
QuicAckFrame frame1 = InitAckFrame(1);
QuicAckFrame frame2 = InitAckFrame(2);
ProcessAckPacket(&frame2);
// Now change it to 1, and it should cause a connection error.
EXPECT_CALL(visitor_, OnConnectionClosed(QUIC_INVALID_ACK_DATA, false));
EXPECT_CALL(visitor_, OnCanWrite()).Times(0);
ProcessAckPacket(&frame1);
}
TEST_P(QuicConnectionTest, AckUnsentData) {
// Ack a packet which has not been sent.
EXPECT_CALL(visitor_, OnConnectionClosed(QUIC_INVALID_ACK_DATA, false));
EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_));
EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _));
QuicAckFrame frame(MakeAckFrame(1));
EXPECT_CALL(visitor_, OnCanWrite()).Times(0);
ProcessAckPacket(&frame);
}
TEST_P(QuicConnectionTest, AckAll) {
EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_));
ProcessPacket(1);
QuicPacketCreatorPeer::SetSequenceNumber(&peer_creator_, 1);
QuicAckFrame frame1 = InitAckFrame(0);
ProcessAckPacket(&frame1);
}
TEST_P(QuicConnectionTest, SendingDifferentSequenceNumberLengthsBandwidth) {
QuicPacketSequenceNumber last_packet;
SendStreamDataToPeer(1, "foo", 0, !kFin, &last_packet);
EXPECT_EQ(1u, last_packet);
EXPECT_EQ(PACKET_1BYTE_SEQUENCE_NUMBER,
QuicPacketCreatorPeer::NextSequenceNumberLength(creator_));
EXPECT_EQ(PACKET_1BYTE_SEQUENCE_NUMBER,
writer_->header().public_header.sequence_number_length);
EXPECT_CALL(*send_algorithm_, GetCongestionWindow()).WillRepeatedly(
Return(kMaxPacketSize * 256));
SendStreamDataToPeer(1, "bar", 3, !kFin, &last_packet);
EXPECT_EQ(2u, last_packet);
EXPECT_EQ(PACKET_2BYTE_SEQUENCE_NUMBER,
QuicPacketCreatorPeer::NextSequenceNumberLength(creator_));
// The 1 packet lag is due to the sequence number length being recalculated in
// QuicConnection after a packet is sent.
EXPECT_EQ(PACKET_1BYTE_SEQUENCE_NUMBER,
writer_->header().public_header.sequence_number_length);
EXPECT_CALL(*send_algorithm_, GetCongestionWindow()).WillRepeatedly(
Return(kMaxPacketSize * 256 * 256));
SendStreamDataToPeer(1, "foo", 6, !kFin, &last_packet);
EXPECT_EQ(3u, last_packet);
EXPECT_EQ(PACKET_4BYTE_SEQUENCE_NUMBER,
QuicPacketCreatorPeer::NextSequenceNumberLength(creator_));
EXPECT_EQ(PACKET_2BYTE_SEQUENCE_NUMBER,
writer_->header().public_header.sequence_number_length);
EXPECT_CALL(*send_algorithm_, GetCongestionWindow()).WillRepeatedly(
Return(kMaxPacketSize * 256 * 256 * 256));
SendStreamDataToPeer(1, "bar", 9, !kFin, &last_packet);
EXPECT_EQ(4u, last_packet);
EXPECT_EQ(PACKET_4BYTE_SEQUENCE_NUMBER,
QuicPacketCreatorPeer::NextSequenceNumberLength(creator_));
EXPECT_EQ(PACKET_4BYTE_SEQUENCE_NUMBER,
writer_->header().public_header.sequence_number_length);
EXPECT_CALL(*send_algorithm_, GetCongestionWindow()).WillRepeatedly(
Return(kMaxPacketSize * 256 * 256 * 256 * 256));
SendStreamDataToPeer(1, "foo", 12, !kFin, &last_packet);
EXPECT_EQ(5u, last_packet);
EXPECT_EQ(PACKET_6BYTE_SEQUENCE_NUMBER,
QuicPacketCreatorPeer::NextSequenceNumberLength(creator_));
EXPECT_EQ(PACKET_4BYTE_SEQUENCE_NUMBER,
writer_->header().public_header.sequence_number_length);
}
// TODO(ianswett): Re-enable this test by finding a good way to test different
// sequence number lengths without sending packets with giant gaps.
TEST_P(QuicConnectionTest,
DISABLED_SendingDifferentSequenceNumberLengthsUnackedDelta) {
QuicPacketSequenceNumber last_packet;
SendStreamDataToPeer(1, "foo", 0, !kFin, &last_packet);
EXPECT_EQ(1u, last_packet);
EXPECT_EQ(PACKET_1BYTE_SEQUENCE_NUMBER,
QuicPacketCreatorPeer::NextSequenceNumberLength(creator_));
EXPECT_EQ(PACKET_1BYTE_SEQUENCE_NUMBER,
writer_->header().public_header.sequence_number_length);
QuicPacketCreatorPeer::SetSequenceNumber(&peer_creator_, 100);
SendStreamDataToPeer(1, "bar", 3, !kFin, &last_packet);
EXPECT_EQ(PACKET_2BYTE_SEQUENCE_NUMBER,
QuicPacketCreatorPeer::NextSequenceNumberLength(creator_));
EXPECT_EQ(PACKET_1BYTE_SEQUENCE_NUMBER,
writer_->header().public_header.sequence_number_length);
QuicPacketCreatorPeer::SetSequenceNumber(&peer_creator_, 100 * 256);
SendStreamDataToPeer(1, "foo", 6, !kFin, &last_packet);
EXPECT_EQ(PACKET_4BYTE_SEQUENCE_NUMBER,
QuicPacketCreatorPeer::NextSequenceNumberLength(creator_));
EXPECT_EQ(PACKET_2BYTE_SEQUENCE_NUMBER,
writer_->header().public_header.sequence_number_length);
QuicPacketCreatorPeer::SetSequenceNumber(&peer_creator_, 100 * 256 * 256);
SendStreamDataToPeer(1, "bar", 9, !kFin, &last_packet);
EXPECT_EQ(PACKET_4BYTE_SEQUENCE_NUMBER,
QuicPacketCreatorPeer::NextSequenceNumberLength(creator_));
EXPECT_EQ(PACKET_4BYTE_SEQUENCE_NUMBER,
writer_->header().public_header.sequence_number_length);
QuicPacketCreatorPeer::SetSequenceNumber(&peer_creator_,
100 * 256 * 256 * 256);
SendStreamDataToPeer(1, "foo", 12, !kFin, &last_packet);
EXPECT_EQ(PACKET_6BYTE_SEQUENCE_NUMBER,
QuicPacketCreatorPeer::NextSequenceNumberLength(creator_));
EXPECT_EQ(PACKET_4BYTE_SEQUENCE_NUMBER,
writer_->header().public_header.sequence_number_length);
}
TEST_P(QuicConnectionTest, BasicSending) {
EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_));
QuicPacketSequenceNumber last_packet;
SendStreamDataToPeer(1, "foo", 0, !kFin, &last_packet); // Packet 1
EXPECT_EQ(1u, last_packet);
SendAckPacketToPeer(); // Packet 2
EXPECT_EQ(1u, least_unacked());
SendAckPacketToPeer(); // Packet 3
EXPECT_EQ(1u, least_unacked());
SendStreamDataToPeer(1, "bar", 3, !kFin, &last_packet); // Packet 4
EXPECT_EQ(4u, last_packet);
SendAckPacketToPeer(); // Packet 5
EXPECT_EQ(1u, least_unacked());
EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _));
// Peer acks up to packet 3.
QuicAckFrame frame = InitAckFrame(3);
ProcessAckPacket(&frame);
SendAckPacketToPeer(); // Packet 6
// As soon as we've acked one, we skip ack packets 2 and 3 and note lack of
// ack for 4.
EXPECT_EQ(4u, least_unacked());
EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _));
// Peer acks up to packet 4, the last packet.
QuicAckFrame frame2 = InitAckFrame(6);
ProcessAckPacket(&frame2); // Acks don't instigate acks.
// Verify that we did not send an ack.
EXPECT_EQ(6u, writer_->header().packet_sequence_number);
// So the last ack has not changed.
EXPECT_EQ(4u, least_unacked());
// If we force an ack, we shouldn't change our retransmit state.
SendAckPacketToPeer(); // Packet 7
EXPECT_EQ(7u, least_unacked());
// But if we send more data it should.
SendStreamDataToPeer(1, "eep", 6, !kFin, &last_packet); // Packet 8
EXPECT_EQ(8u, last_packet);
SendAckPacketToPeer(); // Packet 9
EXPECT_EQ(7u, least_unacked());
}
// QuicConnection should record the the packet sent-time prior to sending the
// packet.
TEST_P(QuicConnectionTest, RecordSentTimeBeforePacketSent) {
// We're using a MockClock for the tests, so we have complete control over the
// time.
// Our recorded timestamp for the last packet sent time will be passed in to
// the send_algorithm. Make sure that it is set to the correct value.
QuicTime actual_recorded_send_time = QuicTime::Zero();
EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _))
.WillOnce(DoAll(SaveArg<0>(&actual_recorded_send_time), Return(true)));
// First send without any pause and check the result.
QuicTime expected_recorded_send_time = clock_.Now();
connection_.SendStreamDataWithString(1, "foo", 0, !kFin, nullptr);
EXPECT_EQ(expected_recorded_send_time, actual_recorded_send_time)
<< "Expected time = " << expected_recorded_send_time.ToDebuggingValue()
<< ". Actual time = " << actual_recorded_send_time.ToDebuggingValue();
// Now pause during the write, and check the results.
actual_recorded_send_time = QuicTime::Zero();
const QuicTime::Delta write_pause_time_delta =
QuicTime::Delta::FromMilliseconds(5000);
SetWritePauseTimeDelta(write_pause_time_delta);
expected_recorded_send_time = clock_.Now();
EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _))
.WillOnce(DoAll(SaveArg<0>(&actual_recorded_send_time), Return(true)));
connection_.SendStreamDataWithString(2, "baz", 0, !kFin, nullptr);
EXPECT_EQ(expected_recorded_send_time, actual_recorded_send_time)
<< "Expected time = " << expected_recorded_send_time.ToDebuggingValue()
<< ". Actual time = " << actual_recorded_send_time.ToDebuggingValue();
}
TEST_P(QuicConnectionTest, FECSending) {
// All packets carry version info till version is negotiated.
size_t payload_length;
// GetPacketLengthForOneStream() assumes a stream offset of 0 in determining
// packet length. The size of the offset field in a stream frame is 0 for
// offset 0, and 2 for non-zero offsets up through 64K. Increase
// max_packet_length by 2 so that subsequent packets containing subsequent
// stream frames with non-zero offets will fit within the packet length.
size_t length = 2 + GetPacketLengthForOneStream(
connection_.version(), kIncludeVersion,
PACKET_8BYTE_CONNECTION_ID, PACKET_1BYTE_SEQUENCE_NUMBER,
IN_FEC_GROUP, &payload_length);
connection_.set_max_packet_length(length);
if (generator_->fec_send_policy() == FEC_ALARM_TRIGGER) {
// Send 4 protected data packets. FEC packet is not sent.
EXPECT_CALL(*send_algorithm_,
OnPacketSent(_, _, _, _, HAS_RETRANSMITTABLE_DATA)).Times(4);
} else {
// Send 4 protected data packets, which should also trigger 1 FEC packet.
EXPECT_CALL(*send_algorithm_,
OnPacketSent(_, _, _, _, HAS_RETRANSMITTABLE_DATA)).Times(5);
}
// The first stream frame will have 2 fewer overhead bytes than the other 3.
const string payload(payload_length * 4 + 2, 'a');
connection_.SendStreamDataWithStringWithFec(1, payload, 0, !kFin, nullptr);
// Expect the FEC group to be closed after SendStreamDataWithString.
EXPECT_FALSE(creator_->IsFecGroupOpen());
EXPECT_FALSE(creator_->IsFecProtected());
}
TEST_P(QuicConnectionTest, FECQueueing) {
// All packets carry version info till version is negotiated.
size_t payload_length;
size_t length = GetPacketLengthForOneStream(
connection_.version(), kIncludeVersion,
PACKET_8BYTE_CONNECTION_ID, PACKET_1BYTE_SEQUENCE_NUMBER,
IN_FEC_GROUP, &payload_length);
connection_.set_max_packet_length(length);
EXPECT_TRUE(creator_->IsFecEnabled());
EXPECT_EQ(0u, connection_.NumQueuedPackets());
BlockOnNextWrite();
const string payload(payload_length, 'a');
connection_.SendStreamDataWithStringWithFec(1, payload, 0, !kFin, nullptr);
EXPECT_FALSE(creator_->IsFecGroupOpen());
EXPECT_FALSE(creator_->IsFecProtected());
if (generator_->fec_send_policy() == FEC_ALARM_TRIGGER) {
// Expect the first data packet to be queued and not the FEC packet.
EXPECT_EQ(1u, connection_.NumQueuedPackets());
} else {
// Expect the first data packet and the fec packet to be queued.
EXPECT_EQ(2u, connection_.NumQueuedPackets());
}
}
TEST_P(QuicConnectionTest, FECAlarmStoppedWhenFECPacketSent) {
EXPECT_TRUE(creator_->IsFecEnabled());
EXPECT_EQ(0u, QuicSentPacketManagerPeer::GetBytesInFlight(manager_));
EXPECT_FALSE(connection_.GetFecAlarm()->IsSet());
creator_->set_max_packets_per_fec_group(2);
// 1 Data packet. FEC alarm should be set.
EXPECT_CALL(*send_algorithm_,
OnPacketSent(_, _, 1u, _, HAS_RETRANSMITTABLE_DATA)).Times(1);
connection_.SendStreamDataWithStringWithFec(3, "foo", 0, true, nullptr);
EXPECT_TRUE(connection_.GetFecAlarm()->IsSet());
if (generator_->fec_send_policy() == FEC_ALARM_TRIGGER) {
// If FEC send policy is FEC_ALARM_TRIGGER, FEC packet is not sent.
// FEC alarm should not be set.
EXPECT_CALL(*send_algorithm_,
OnPacketSent(_, _, _, _, HAS_RETRANSMITTABLE_DATA)).Times(1);
} else {
// Second data packet triggers FEC packet out. FEC alarm should not be set.
EXPECT_CALL(*send_algorithm_,
OnPacketSent(_, _, _, _, HAS_RETRANSMITTABLE_DATA)).Times(2);
}
connection_.SendStreamDataWithStringWithFec(5, "foo", 0, true, nullptr);
if (generator_->fec_send_policy() == FEC_ANY_TRIGGER) {
EXPECT_TRUE(writer_->header().fec_flag);
}
EXPECT_FALSE(creator_->IsFecGroupOpen());
EXPECT_FALSE(connection_.GetFecAlarm()->IsSet());
}
TEST_P(QuicConnectionTest, FECAlarmStoppedOnConnectionClose) {
EXPECT_TRUE(creator_->IsFecEnabled());
EXPECT_FALSE(connection_.GetFecAlarm()->IsSet());
creator_->set_max_packets_per_fec_group(100);
// 1 Data packet. FEC alarm should be set.
EXPECT_CALL(*send_algorithm_,
OnPacketSent(_, _, 1u, _, HAS_RETRANSMITTABLE_DATA)).Times(1);
connection_.SendStreamDataWithStringWithFec(3, "foo", 0, kFin, nullptr);
EXPECT_TRUE(connection_.GetFecAlarm()->IsSet());
EXPECT_CALL(visitor_, OnConnectionClosed(QUIC_NO_ERROR, false));
// Closing connection should stop the FEC alarm.
connection_.CloseConnection(QUIC_NO_ERROR, /*from_peer=*/false);
EXPECT_FALSE(connection_.GetFecAlarm()->IsSet());
}
TEST_P(QuicConnectionTest, RemoveFECFromInflightOnRetransmissionTimeout) {
EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_));
EXPECT_TRUE(creator_->IsFecEnabled());
EXPECT_EQ(0u, QuicSentPacketManagerPeer::GetBytesInFlight(manager_));
EXPECT_FALSE(connection_.GetFecAlarm()->IsSet());
// 1 Data packet. FEC alarm should be set.
EXPECT_CALL(*send_algorithm_,
OnPacketSent(_, _, 1u, _, HAS_RETRANSMITTABLE_DATA)).Times(1);
connection_.SendStreamDataWithStringWithFec(3, "foo", 0, !kFin, nullptr);
EXPECT_TRUE(connection_.GetFecAlarm()->IsSet());
size_t protected_packet =
QuicSentPacketManagerPeer::GetBytesInFlight(manager_);
// Force FEC timeout to send FEC packet out.
EXPECT_CALL(*send_algorithm_,
OnPacketSent(_, _, 2u, _, HAS_RETRANSMITTABLE_DATA)).Times(1);
connection_.GetFecAlarm()->Fire();
EXPECT_TRUE(writer_->header().fec_flag);
size_t fec_packet = protected_packet;
EXPECT_EQ(protected_packet + fec_packet,
QuicSentPacketManagerPeer::GetBytesInFlight(manager_));
clock_.AdvanceTime(DefaultRetransmissionTime());
// On RTO, both data and FEC packets are removed from inflight, only the data
// packet is retransmitted, and this retransmission (but not FEC) gets added
// back into the inflight.
EXPECT_CALL(*send_algorithm_, OnRetransmissionTimeout(true));
EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1);
connection_.GetRetransmissionAlarm()->Fire();
// The retransmission of packet 1 will be 3 bytes smaller than packet 1, since
// the first transmission will have 1 byte for FEC group number and 2 bytes of
// stream frame size, which are absent in the retransmission.
size_t retransmitted_packet = protected_packet - 3;
EXPECT_EQ(protected_packet + retransmitted_packet,
QuicSentPacketManagerPeer::GetBytesInFlight(manager_));
EXPECT_FALSE(connection_.GetFecAlarm()->IsSet());
// Receive ack for the retransmission. No data should be outstanding.
QuicAckFrame ack = InitAckFrame(3);
NackPacket(1, &ack);
NackPacket(2, &ack);
SequenceNumberSet lost_packets;
lost_packets.insert(1);
EXPECT_CALL(*loss_algorithm_, DetectLostPackets(_, _, _, _))
.WillOnce(Return(lost_packets));
EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _));
ProcessAckPacket(&ack);
// Ensure the alarm is not set since all packets have been acked or abandoned.
EXPECT_FALSE(connection_.GetRetransmissionAlarm()->IsSet());
EXPECT_EQ(0u, QuicSentPacketManagerPeer::GetBytesInFlight(manager_));
}
TEST_P(QuicConnectionTest, RemoveFECFromInflightOnLossRetransmission) {
EXPECT_TRUE(creator_->IsFecEnabled());
EXPECT_FALSE(connection_.GetFecAlarm()->IsSet());
// 1 FEC-protected data packet. FEC alarm should be set.
EXPECT_CALL(*send_algorithm_,
OnPacketSent(_, _, _, _, HAS_RETRANSMITTABLE_DATA)).Times(1);
connection_.SendStreamDataWithStringWithFec(3, "foo", 0, kFin, nullptr);
EXPECT_TRUE(connection_.GetFecAlarm()->IsSet());
size_t protected_packet =
QuicSentPacketManagerPeer::GetBytesInFlight(manager_);
// Force FEC timeout to send FEC packet out.
EXPECT_CALL(*send_algorithm_,
OnPacketSent(_, _, _, _, HAS_RETRANSMITTABLE_DATA)).Times(1);
connection_.GetFecAlarm()->Fire();
EXPECT_TRUE(writer_->header().fec_flag);
size_t fec_packet = protected_packet;
EXPECT_EQ(protected_packet + fec_packet,
QuicSentPacketManagerPeer::GetBytesInFlight(manager_));
// Send more data to trigger NACKs. Note that all data starts at stream offset
// 0 to ensure the same packet size, for ease of testing.
EXPECT_CALL(*send_algorithm_,
OnPacketSent(_, _, _, _, HAS_RETRANSMITTABLE_DATA)).Times(4);
connection_.SendStreamDataWithString(5, "foo", 0, kFin, nullptr);
connection_.SendStreamDataWithString(7, "foo", 0, kFin, nullptr);
connection_.SendStreamDataWithString(9, "foo", 0, kFin, nullptr);
connection_.SendStreamDataWithString(11, "foo", 0, kFin, nullptr);
// An unprotected packet will be 3 bytes smaller than an FEC-protected packet,
// since the protected packet will have 1 byte for FEC group number and
// 2 bytes of stream frame size, which are absent in the unprotected packet.
size_t unprotected_packet = protected_packet - 3;
EXPECT_EQ(protected_packet + fec_packet + 4 * unprotected_packet,
QuicSentPacketManagerPeer::GetBytesInFlight(manager_));
EXPECT_FALSE(connection_.GetFecAlarm()->IsSet());
// Ack data packets, and NACK FEC packet and one data packet. Triggers
// NACK-based loss detection of both packets, but only data packet is
// retransmitted and considered oustanding.
QuicAckFrame ack = InitAckFrame(6);
NackPacket(2, &ack);
NackPacket(3, &ack);
SequenceNumberSet lost_packets;
lost_packets.insert(2);
lost_packets.insert(3);
EXPECT_CALL(*loss_algorithm_, DetectLostPackets(_, _, _, _))
.WillOnce(Return(lost_packets));
EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _));
EXPECT_CALL(*send_algorithm_,
OnPacketSent(_, _, _, _, HAS_RETRANSMITTABLE_DATA)).Times(1);
EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_));
ProcessAckPacket(&ack);
// On receiving this ack from the server, the client will no longer send
// version number in subsequent packets, including in this retransmission.
size_t unprotected_packet_no_version = unprotected_packet - 4;
EXPECT_EQ(unprotected_packet_no_version,
QuicSentPacketManagerPeer::GetBytesInFlight(manager_));
// Receive ack for the retransmission. No data should be outstanding.
QuicAckFrame ack2 = InitAckFrame(7);
NackPacket(2, &ack2);
NackPacket(3, &ack2);
SequenceNumberSet lost_packets2;
EXPECT_CALL(*loss_algorithm_, DetectLostPackets(_, _, _, _))
.WillOnce(Return(lost_packets2));
EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _));
ProcessAckPacket(&ack2);
EXPECT_EQ(0u, QuicSentPacketManagerPeer::GetBytesInFlight(manager_));
}
TEST_P(QuicConnectionTest, FECRemainsInflightOnTLPOfEarlierData) {
// This test checks if TLP is sent correctly when a data and an FEC packet
// are outstanding. TLP should be sent for the data packet when the
// retransmission alarm fires.
// Turn on TLP for this test.
QuicSentPacketManagerPeer::SetMaxTailLossProbes(manager_, 1);
EXPECT_TRUE(creator_->IsFecEnabled());
EXPECT_EQ(0u, QuicSentPacketManagerPeer::GetBytesInFlight(manager_));
EXPECT_FALSE(connection_.GetFecAlarm()->IsSet());
// 1 Data packet. FEC alarm should be set.
EXPECT_CALL(*send_algorithm_,
OnPacketSent(_, _, 1u, _, HAS_RETRANSMITTABLE_DATA)).Times(1);
connection_.SendStreamDataWithStringWithFec(3, "foo", 0, kFin, nullptr);
EXPECT_TRUE(connection_.GetFecAlarm()->IsSet());
size_t protected_packet =
QuicSentPacketManagerPeer::GetBytesInFlight(manager_);
EXPECT_LT(0u, protected_packet);
// Force FEC timeout to send FEC packet out.
EXPECT_CALL(*send_algorithm_,
OnPacketSent(_, _, 2u, _, HAS_RETRANSMITTABLE_DATA)).Times(1);
connection_.GetFecAlarm()->Fire();
EXPECT_TRUE(writer_->header().fec_flag);
size_t fec_packet = protected_packet;
EXPECT_EQ(protected_packet + fec_packet,
QuicSentPacketManagerPeer::GetBytesInFlight(manager_));
// TLP alarm should be set.
QuicTime retransmission_time =
connection_.GetRetransmissionAlarm()->deadline();
EXPECT_NE(QuicTime::Zero(), retransmission_time);
// Simulate the retransmission alarm firing and sending a TLP, so send
// algorithm's OnRetransmissionTimeout is not called.
clock_.AdvanceTime(retransmission_time.Subtract(clock_.Now()));
EXPECT_CALL(*send_algorithm_,
OnPacketSent(_, _, 3u, _, HAS_RETRANSMITTABLE_DATA)).Times(1);
connection_.GetRetransmissionAlarm()->Fire();
// The TLP retransmission of packet 1 will be 3 bytes smaller than packet 1,
// since packet 1 will have 1 byte for FEC group number and 2 bytes of stream
// frame size, which are absent in the the TLP retransmission.
size_t tlp_packet = protected_packet - 3;
EXPECT_EQ(protected_packet + fec_packet + tlp_packet,
QuicSentPacketManagerPeer::GetBytesInFlight(manager_));
}
TEST_P(QuicConnectionTest, FECRemainsInflightOnTLPOfLaterData) {
// Tests if TLP is sent correctly when data packet 1 and an FEC packet are
// sent followed by data packet 2, and data packet 1 is acked. TLP should be
// sent for data packet 2 when the retransmission alarm fires. Turn on TLP for
// this test.
QuicSentPacketManagerPeer::SetMaxTailLossProbes(manager_, 1);
EXPECT_TRUE(creator_->IsFecEnabled());
EXPECT_EQ(0u, QuicSentPacketManagerPeer::GetBytesInFlight(manager_));
EXPECT_FALSE(connection_.GetFecAlarm()->IsSet());
// 1 Data packet. FEC alarm should be set.
EXPECT_CALL(*send_algorithm_,
OnPacketSent(_, _, 1u, _, HAS_RETRANSMITTABLE_DATA)).Times(1);
connection_.SendStreamDataWithStringWithFec(3, "foo", 0, kFin, nullptr);
EXPECT_TRUE(connection_.GetFecAlarm()->IsSet());
size_t protected_packet =
QuicSentPacketManagerPeer::GetBytesInFlight(manager_);
EXPECT_LT(0u, protected_packet);
// Force FEC timeout to send FEC packet out.
EXPECT_CALL(*send_algorithm_,
OnPacketSent(_, _, 2u, _, HAS_RETRANSMITTABLE_DATA)).Times(1);
connection_.GetFecAlarm()->Fire();
EXPECT_TRUE(writer_->header().fec_flag);
// Protected data packet and FEC packet oustanding.
size_t fec_packet = protected_packet;
EXPECT_EQ(protected_packet + fec_packet,
QuicSentPacketManagerPeer::GetBytesInFlight(manager_));
// Send 1 unprotected data packet. No FEC alarm should be set.
EXPECT_CALL(*send_algorithm_,
OnPacketSent(_, _, 3u, _, HAS_RETRANSMITTABLE_DATA)).Times(1);
connection_.SendStreamDataWithString(5, "foo", 0, kFin, nullptr);
EXPECT_FALSE(connection_.GetFecAlarm()->IsSet());
// Protected data packet, FEC packet, and unprotected data packet oustanding.
// An unprotected packet will be 3 bytes smaller than an FEC-protected packet,
// since the protected packet will have 1 byte for FEC group number and
// 2 bytes of stream frame size, which are absent in the unprotected packet.
size_t unprotected_packet = protected_packet - 3;
EXPECT_EQ(protected_packet + fec_packet + unprotected_packet,
QuicSentPacketManagerPeer::GetBytesInFlight(manager_));
// Receive ack for first data packet. FEC and second data packet are still
// outstanding.
QuicAckFrame ack = InitAckFrame(1);
EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _));
EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_));
ProcessAckPacket(&ack);
// FEC packet and unprotected data packet oustanding.
EXPECT_EQ(fec_packet + unprotected_packet,
QuicSentPacketManagerPeer::GetBytesInFlight(manager_));
// TLP alarm should be set.
QuicTime retransmission_time =
connection_.GetRetransmissionAlarm()->deadline();
EXPECT_NE(QuicTime::Zero(), retransmission_time);
// Simulate the retransmission alarm firing and sending a TLP, so send
// algorithm's OnRetransmissionTimeout is not called.
clock_.AdvanceTime(retransmission_time.Subtract(clock_.Now()));
EXPECT_CALL(*send_algorithm_,
OnPacketSent(_, _, 4u, _, HAS_RETRANSMITTABLE_DATA)).Times(1);
connection_.GetRetransmissionAlarm()->Fire();
// Having received an ack from the server, the client will no longer send
// version number in subsequent packets, including in this retransmission.
size_t tlp_packet_no_version = unprotected_packet - 4;
EXPECT_EQ(fec_packet + unprotected_packet + tlp_packet_no_version,
QuicSentPacketManagerPeer::GetBytesInFlight(manager_));
}
TEST_P(QuicConnectionTest, NoTLPForFECPacket) {
// Turn on TLP for this test.
QuicSentPacketManagerPeer::SetMaxTailLossProbes(manager_, 1);
EXPECT_TRUE(creator_->IsFecEnabled());
EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_));
// Send 1 FEC-protected data packet. FEC alarm should be set.
EXPECT_CALL(*send_algorithm_,
OnPacketSent(_, _, _, _, HAS_RETRANSMITTABLE_DATA)).Times(1);
connection_.SendStreamDataWithStringWithFec(3, "foo", 0, !kFin, nullptr);
EXPECT_TRUE(connection_.GetFecAlarm()->IsSet());
// Force FEC timeout to send FEC packet out.
EXPECT_CALL(*send_algorithm_,
OnPacketSent(_, _, _, _, HAS_RETRANSMITTABLE_DATA)).Times(1);
connection_.GetFecAlarm()->Fire();
EXPECT_TRUE(writer_->header().fec_flag);
// Ack data packet, but not FEC packet.
QuicAckFrame ack = InitAckFrame(1);
EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _));
ProcessAckPacket(&ack);
// No TLP alarm for FEC, but retransmission alarm should be set for an RTO.
EXPECT_LT(0u, QuicSentPacketManagerPeer::GetBytesInFlight(manager_));
EXPECT_TRUE(connection_.GetRetransmissionAlarm()->IsSet());
QuicTime rto_time = connection_.GetRetransmissionAlarm()->deadline();
EXPECT_NE(QuicTime::Zero(), rto_time);
// Simulate the retransmission alarm firing. FEC packet is no longer
// outstanding.
clock_.AdvanceTime(rto_time.Subtract(clock_.Now()));
connection_.GetRetransmissionAlarm()->Fire();
EXPECT_FALSE(connection_.GetRetransmissionAlarm()->IsSet());
EXPECT_EQ(0u, QuicSentPacketManagerPeer::GetBytesInFlight(manager_));
}
TEST_P(QuicConnectionTest, FramePacking) {
CongestionBlockWrites();
// Send an ack and two stream frames in 1 packet by queueing them.
connection_.SendAck();
EXPECT_CALL(visitor_, OnCanWrite()).WillOnce(DoAll(
IgnoreResult(InvokeWithoutArgs(&connection_,
&TestConnection::SendStreamData3)),
IgnoreResult(InvokeWithoutArgs(&connection_,
&TestConnection::SendStreamData5))));
EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1);
CongestionUnblockWrites();
connection_.GetSendAlarm()->Fire();
EXPECT_EQ(0u, connection_.NumQueuedPackets());
EXPECT_FALSE(connection_.HasQueuedData());
// Parse the last packet and ensure it's an ack and two stream frames from
// two different streams.
EXPECT_EQ(4u, writer_->frame_count());
EXPECT_FALSE(writer_->stop_waiting_frames().empty());
EXPECT_FALSE(writer_->ack_frames().empty());
ASSERT_EQ(2u, writer_->stream_frames().size());
EXPECT_EQ(kClientDataStreamId1, writer_->stream_frames()[0].stream_id);
EXPECT_EQ(kClientDataStreamId2, writer_->stream_frames()[1].stream_id);
}
TEST_P(QuicConnectionTest, FramePackingNonCryptoThenCrypto) {
CongestionBlockWrites();
// Send an ack and two stream frames (one non-crypto, then one crypto) in 2
// packets by queueing them.
connection_.SendAck();
EXPECT_CALL(visitor_, OnCanWrite()).WillOnce(DoAll(
IgnoreResult(InvokeWithoutArgs(&connection_,
&TestConnection::SendStreamData3)),
IgnoreResult(InvokeWithoutArgs(&connection_,
&TestConnection::SendCryptoStreamData))));
EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(2);
CongestionUnblockWrites();
connection_.GetSendAlarm()->Fire();
EXPECT_EQ(0u, connection_.NumQueuedPackets());
EXPECT_FALSE(connection_.HasQueuedData());
// Parse the last packet and ensure it's the crypto stream frame.
EXPECT_EQ(1u, writer_->frame_count());
ASSERT_EQ(1u, writer_->stream_frames().size());
EXPECT_EQ(kCryptoStreamId, writer_->stream_frames()[0].stream_id);
}
TEST_P(QuicConnectionTest, FramePackingCryptoThenNonCrypto) {
CongestionBlockWrites();
// Send an ack and two stream frames (one crypto, then one non-crypto) in 2
// packets by queueing them.
connection_.SendAck();
EXPECT_CALL(visitor_, OnCanWrite()).WillOnce(DoAll(
IgnoreResult(InvokeWithoutArgs(&connection_,
&TestConnection::SendCryptoStreamData)),
IgnoreResult(InvokeWithoutArgs(&connection_,
&TestConnection::SendStreamData3))));
EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(2);
CongestionUnblockWrites();
connection_.GetSendAlarm()->Fire();
EXPECT_EQ(0u, connection_.NumQueuedPackets());
EXPECT_FALSE(connection_.HasQueuedData());
// Parse the last packet and ensure it's the stream frame from stream 3.
EXPECT_EQ(1u, writer_->frame_count());
ASSERT_EQ(1u, writer_->stream_frames().size());
EXPECT_EQ(kClientDataStreamId1, writer_->stream_frames()[0].stream_id);
}
TEST_P(QuicConnectionTest, FramePackingFEC) {
EXPECT_TRUE(creator_->IsFecEnabled());
CongestionBlockWrites();
// Queue an ack and two stream frames. Ack gets flushed when FEC is turned on
// for sending protected data; two stream frames are packed in 1 packet.
EXPECT_CALL(visitor_, OnCanWrite()).WillOnce(DoAll(
IgnoreResult(InvokeWithoutArgs(
&connection_, &TestConnection::SendStreamData3WithFec)),
IgnoreResult(InvokeWithoutArgs(
&connection_, &TestConnection::SendStreamData5WithFec))));
connection_.SendAck();
EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(2);
CongestionUnblockWrites();
connection_.GetSendAlarm()->Fire();
EXPECT_EQ(0u, connection_.NumQueuedPackets());
EXPECT_FALSE(connection_.HasQueuedData());
// Parse the last packet and ensure it's in an fec group.
EXPECT_EQ(2u, writer_->header().fec_group);
EXPECT_EQ(2u, writer_->frame_count());
// FEC alarm should be set.
EXPECT_TRUE(connection_.GetFecAlarm()->IsSet());
}
TEST_P(QuicConnectionTest, FramePackingAckResponse) {
EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_));
// Process a data packet to queue up a pending ack.
EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(1);
ProcessDataPacket(1, 1, kEntropyFlag);
EXPECT_CALL(visitor_, OnCanWrite()).WillOnce(DoAll(
IgnoreResult(InvokeWithoutArgs(&connection_,
&TestConnection::SendStreamData3)),
IgnoreResult(InvokeWithoutArgs(&connection_,
&TestConnection::SendStreamData5))));
EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1);
// Process an ack to cause the visitor's OnCanWrite to be invoked.
QuicAckFrame ack_one = InitAckFrame(0);
ProcessAckPacket(3, &ack_one);
EXPECT_EQ(0u, connection_.NumQueuedPackets());
EXPECT_FALSE(connection_.HasQueuedData());
// Parse the last packet and ensure it's an ack and two stream frames from
// two different streams.
EXPECT_EQ(4u, writer_->frame_count());
EXPECT_FALSE(writer_->stop_waiting_frames().empty());
EXPECT_FALSE(writer_->ack_frames().empty());
ASSERT_EQ(2u, writer_->stream_frames().size());
EXPECT_EQ(kClientDataStreamId1, writer_->stream_frames()[0].stream_id);
EXPECT_EQ(kClientDataStreamId2, writer_->stream_frames()[1].stream_id);
}
TEST_P(QuicConnectionTest, FramePackingSendv) {
// Send data in 1 packet by writing multiple blocks in a single iovector
// using writev.
EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _));
char data[] = "ABCD";
struct iovec iov[2];
iov[0].iov_base = data;
iov[0].iov_len = 2;
iov[1].iov_base = data + 2;
iov[1].iov_len = 2;
connection_.SendStreamData(1, QuicIOVector(iov, 2, 4), 0, !kFin,
MAY_FEC_PROTECT, nullptr);
EXPECT_EQ(0u, connection_.NumQueuedPackets());
EXPECT_FALSE(connection_.HasQueuedData());
// Parse the last packet and ensure multiple iovector blocks have
// been packed into a single stream frame from one stream.
EXPECT_EQ(1u, writer_->frame_count());
EXPECT_EQ(1u, writer_->stream_frames().size());
QuicStreamFrame frame = writer_->stream_frames()[0];
EXPECT_EQ(1u, frame.stream_id);
EXPECT_EQ("ABCD", frame.data);
}
TEST_P(QuicConnectionTest, FramePackingSendvQueued) {
// Try to send two stream frames in 1 packet by using writev.
EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _));
BlockOnNextWrite();
char data[] = "ABCD";
struct iovec iov[2];
iov[0].iov_base = data;
iov[0].iov_len = 2;
iov[1].iov_base = data + 2;
iov[1].iov_len = 2;
connection_.SendStreamData(1, QuicIOVector(iov, 2, 4), 0, !kFin,
MAY_FEC_PROTECT, nullptr);
EXPECT_EQ(1u, connection_.NumQueuedPackets());
EXPECT_TRUE(connection_.HasQueuedData());
// Unblock the writes and actually send.
writer_->SetWritable();
connection_.OnCanWrite();
EXPECT_EQ(0u, connection_.NumQueuedPackets());
// Parse the last packet and ensure it's one stream frame from one stream.
EXPECT_EQ(1u, writer_->frame_count());
EXPECT_EQ(1u, writer_->stream_frames().size());
EXPECT_EQ(1u, writer_->stream_frames()[0].stream_id);
}
TEST_P(QuicConnectionTest, SendingZeroBytes) {
// Send a zero byte write with a fin using writev.
EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _));
QuicIOVector empty_iov(nullptr, 0, 0);
connection_.SendStreamData(1, empty_iov, 0, kFin, MAY_FEC_PROTECT, nullptr);
EXPECT_EQ(0u, connection_.NumQueuedPackets());
EXPECT_FALSE(connection_.HasQueuedData());
// Parse the last packet and ensure it's one stream frame from one stream.
EXPECT_EQ(1u, writer_->frame_count());
EXPECT_EQ(1u, writer_->stream_frames().size());
EXPECT_EQ(1u, writer_->stream_frames()[0].stream_id);
EXPECT_TRUE(writer_->stream_frames()[0].fin);
}
TEST_P(QuicConnectionTest, OnCanWrite) {
// Visitor's OnCanWrite will send data, but will have more pending writes.
EXPECT_CALL(visitor_, OnCanWrite()).WillOnce(DoAll(
IgnoreResult(InvokeWithoutArgs(&connection_,
&TestConnection::SendStreamData3)),
IgnoreResult(InvokeWithoutArgs(&connection_,
&TestConnection::SendStreamData5))));
EXPECT_CALL(visitor_, WillingAndAbleToWrite()).WillOnce(Return(true));
EXPECT_CALL(*send_algorithm_,
TimeUntilSend(_, _, _)).WillRepeatedly(
testing::Return(QuicTime::Delta::Zero()));
connection_.OnCanWrite();
// Parse the last packet and ensure it's the two stream frames from
// two different streams.
EXPECT_EQ(2u, writer_->frame_count());
EXPECT_EQ(2u, writer_->stream_frames().size());
EXPECT_EQ(kClientDataStreamId1, writer_->stream_frames()[0].stream_id);
EXPECT_EQ(kClientDataStreamId2, writer_->stream_frames()[1].stream_id);
}
TEST_P(QuicConnectionTest, RetransmitOnNack) {
QuicPacketSequenceNumber last_packet;
QuicByteCount second_packet_size;
SendStreamDataToPeer(3, "foo", 0, !kFin, &last_packet); // Packet 1
second_packet_size =
SendStreamDataToPeer(3, "foos", 3, !kFin, &last_packet); // Packet 2
SendStreamDataToPeer(3, "fooos", 7, !kFin, &last_packet); // Packet 3
EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_));
// Don't lose a packet on an ack, and nothing is retransmitted.
EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _));
QuicAckFrame ack_one = InitAckFrame(1);
ProcessAckPacket(&ack_one);
// Lose a packet and ensure it triggers retransmission.
QuicAckFrame nack_two = InitAckFrame(3);
NackPacket(2, &nack_two);
SequenceNumberSet lost_packets;
lost_packets.insert(2);
EXPECT_CALL(*loss_algorithm_, DetectLostPackets(_, _, _, _))
.WillOnce(Return(lost_packets));
EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _));
EXPECT_CALL(*send_algorithm_,
OnPacketSent(_, _, _, second_packet_size - kQuicVersionSize, _)).
Times(1);
ProcessAckPacket(&nack_two);
}
TEST_P(QuicConnectionTest, DoNotSendQueuedPacketForResetStream) {
// Block the connection to queue the packet.
BlockOnNextWrite();
QuicStreamId stream_id = 2;
connection_.SendStreamDataWithString(stream_id, "foo", 0, !kFin, nullptr);
// Now that there is a queued packet, reset the stream.
connection_.SendRstStream(stream_id, QUIC_STREAM_NO_ERROR, 14);
// Unblock the connection and verify that only the RST_STREAM is sent.
EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1);
writer_->SetWritable();
connection_.OnCanWrite();
EXPECT_EQ(1u, writer_->frame_count());
EXPECT_EQ(1u, writer_->rst_stream_frames().size());
}
TEST_P(QuicConnectionTest, DoNotRetransmitForResetStreamOnNack) {
QuicStreamId stream_id = 2;
QuicPacketSequenceNumber last_packet;
SendStreamDataToPeer(stream_id, "foo", 0, !kFin, &last_packet);
SendStreamDataToPeer(stream_id, "foos", 3, !kFin, &last_packet);
SendStreamDataToPeer(stream_id, "fooos", 7, !kFin, &last_packet);
EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1);
connection_.SendRstStream(stream_id, QUIC_STREAM_NO_ERROR, 14);
// Lose a packet and ensure it does not trigger retransmission.
QuicAckFrame nack_two = InitAckFrame(last_packet);
NackPacket(last_packet - 1, &nack_two);
SequenceNumberSet lost_packets;
lost_packets.insert(last_packet - 1);
EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_));
EXPECT_CALL(*loss_algorithm_, DetectLostPackets(_, _, _, _))
.WillOnce(Return(lost_packets));
EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _));
EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(0);
ProcessAckPacket(&nack_two);
}
TEST_P(QuicConnectionTest, DoNotRetransmitForResetStreamOnRTO) {
QuicStreamId stream_id = 2;
QuicPacketSequenceNumber last_packet;
SendStreamDataToPeer(stream_id, "foo", 0, !kFin, &last_packet);
EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1);
connection_.SendRstStream(stream_id, QUIC_STREAM_NO_ERROR, 14);
// Fire the RTO and verify that the RST_STREAM is resent, not stream data.
EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1);
clock_.AdvanceTime(DefaultRetransmissionTime());
connection_.GetRetransmissionAlarm()->Fire();
EXPECT_EQ(1u, writer_->frame_count());
EXPECT_EQ(1u, writer_->rst_stream_frames().size());
EXPECT_EQ(stream_id, writer_->rst_stream_frames().front().stream_id);
}
TEST_P(QuicConnectionTest, DoNotSendPendingRetransmissionForResetStream) {
QuicStreamId stream_id = 2;
QuicPacketSequenceNumber last_packet;
SendStreamDataToPeer(stream_id, "foo", 0, !kFin, &last_packet);
SendStreamDataToPeer(stream_id, "foos", 3, !kFin, &last_packet);
BlockOnNextWrite();
connection_.SendStreamDataWithString(stream_id, "fooos", 7, !kFin, nullptr);
// Lose a packet which will trigger a pending retransmission.
QuicAckFrame ack = InitAckFrame(last_packet);
NackPacket(last_packet - 1, &ack);
SequenceNumberSet lost_packets;
lost_packets.insert(last_packet - 1);
EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_));
EXPECT_CALL(*loss_algorithm_, DetectLostPackets(_, _, _, _))
.WillOnce(Return(lost_packets));
EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _));
EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(0);
ProcessAckPacket(&ack);
connection_.SendRstStream(stream_id, QUIC_STREAM_NO_ERROR, 14);
// Unblock the connection and verify that the RST_STREAM is sent but not the
// second data packet nor a retransmit.
EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1);
writer_->SetWritable();
connection_.OnCanWrite();
EXPECT_EQ(1u, writer_->frame_count());
EXPECT_EQ(1u, writer_->rst_stream_frames().size());
EXPECT_EQ(stream_id, writer_->rst_stream_frames().front().stream_id);
}
TEST_P(QuicConnectionTest, DiscardRetransmit) {
QuicPacketSequenceNumber last_packet;
SendStreamDataToPeer(1, "foo", 0, !kFin, &last_packet); // Packet 1
SendStreamDataToPeer(1, "foos", 3, !kFin, &last_packet); // Packet 2
SendStreamDataToPeer(1, "fooos", 7, !kFin, &last_packet); // Packet 3
EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_));
// Instigate a loss with an ack.
QuicAckFrame nack_two = InitAckFrame(3);
NackPacket(2, &nack_two);
// The first nack should trigger a fast retransmission, but we'll be
// write blocked, so the packet will be queued.
BlockOnNextWrite();
SequenceNumberSet lost_packets;
lost_packets.insert(2);
EXPECT_CALL(*loss_algorithm_, DetectLostPackets(_, _, _, _))
.WillOnce(Return(lost_packets));
EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _));
ProcessAckPacket(&nack_two);
EXPECT_EQ(1u, connection_.NumQueuedPackets());
// Now, ack the previous transmission.
EXPECT_CALL(*loss_algorithm_, DetectLostPackets(_, _, _, _))
.WillOnce(Return(SequenceNumberSet()));
QuicAckFrame ack_all = InitAckFrame(3);
ProcessAckPacket(&ack_all);
// Unblock the socket and attempt to send the queued packets. However,
// since the previous transmission has been acked, we will not
// send the retransmission.
EXPECT_CALL(*send_algorithm_,
OnPacketSent(_, _, _, _, _)).Times(0);
writer_->SetWritable();
connection_.OnCanWrite();
EXPECT_EQ(0u, connection_.NumQueuedPackets());
}
TEST_P(QuicConnectionTest, RetransmitNackedLargestObserved) {
EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_));
QuicPacketSequenceNumber largest_observed;
QuicByteCount packet_size;
EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _))
.WillOnce(DoAll(SaveArg<2>(&largest_observed), SaveArg<3>(&packet_size),
Return(true)));
connection_.SendStreamDataWithString(3, "foo", 0, !kFin, nullptr);
QuicAckFrame frame = InitAckFrame(1);
NackPacket(largest_observed, &frame);
// The first nack should retransmit the largest observed packet.
SequenceNumberSet lost_packets;
lost_packets.insert(1);
EXPECT_CALL(*loss_algorithm_, DetectLostPackets(_, _, _, _))
.WillOnce(Return(lost_packets));
EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _));
EXPECT_CALL(*send_algorithm_,
OnPacketSent(_, _, _, packet_size - kQuicVersionSize, _));
ProcessAckPacket(&frame);
}
TEST_P(QuicConnectionTest, QueueAfterTwoRTOs) {
for (int i = 0; i < 10; ++i) {
EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1);
connection_.SendStreamDataWithString(3, "foo", i * 3, !kFin, nullptr);
}
// Block the writer and ensure they're queued.
BlockOnNextWrite();
clock_.AdvanceTime(DefaultRetransmissionTime());
// Only one packet should be retransmitted.
connection_.GetRetransmissionAlarm()->Fire();
EXPECT_TRUE(connection_.HasQueuedData());
// Unblock the writer.
writer_->SetWritable();
clock_.AdvanceTime(QuicTime::Delta::FromMicroseconds(
2 * DefaultRetransmissionTime().ToMicroseconds()));
// Retransmit already retransmitted packets event though the sequence number
// greater than the largest observed.
EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(2);
connection_.GetRetransmissionAlarm()->Fire();
connection_.OnCanWrite();
}
TEST_P(QuicConnectionTest, WriteBlockedThenSent) {
BlockOnNextWrite();
writer_->set_is_write_blocked_data_buffered(true);
EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1);
connection_.SendStreamDataWithString(1, "foo", 0, !kFin, nullptr);
EXPECT_TRUE(connection_.GetRetransmissionAlarm()->IsSet());
writer_->SetWritable();
connection_.OnCanWrite();
EXPECT_TRUE(connection_.GetRetransmissionAlarm()->IsSet());
}
TEST_P(QuicConnectionTest, RetransmitWriteBlockedAckedOriginalThenSent) {
EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_));
connection_.SendStreamDataWithString(3, "foo", 0, !kFin, nullptr);
EXPECT_TRUE(connection_.GetRetransmissionAlarm()->IsSet());
BlockOnNextWrite();
writer_->set_is_write_blocked_data_buffered(true);
// Simulate the retransmission alarm firing.
clock_.AdvanceTime(DefaultRetransmissionTime());
connection_.GetRetransmissionAlarm()->Fire();
// Ack the sent packet before the callback returns, which happens in
// rare circumstances with write blocked sockets.
QuicAckFrame ack = InitAckFrame(1);
EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _));
ProcessAckPacket(&ack);
writer_->SetWritable();
connection_.OnCanWrite();
// There is now a pending packet, but with no retransmittable frames.
EXPECT_TRUE(connection_.GetRetransmissionAlarm()->IsSet());
EXPECT_FALSE(connection_.sent_packet_manager().HasRetransmittableFrames(2));
}
TEST_P(QuicConnectionTest, AlarmsWhenWriteBlocked) {
// Block the connection.
BlockOnNextWrite();
connection_.SendStreamDataWithString(3, "foo", 0, !kFin, nullptr);
EXPECT_EQ(1u, writer_->packets_write_attempts());
EXPECT_TRUE(writer_->IsWriteBlocked());
// Set the send and resumption alarms. Fire the alarms and ensure they don't
// attempt to write.
connection_.GetResumeWritesAlarm()->Set(clock_.ApproximateNow());
connection_.GetSendAlarm()->Set(clock_.ApproximateNow());
connection_.GetResumeWritesAlarm()->Fire();
connection_.GetSendAlarm()->Fire();
EXPECT_TRUE(writer_->IsWriteBlocked());
EXPECT_EQ(1u, writer_->packets_write_attempts());
}
TEST_P(QuicConnectionTest, NoLimitPacketsPerNack) {