This source file includes following definitions.
- GenerateCongestionFeedback
- SetKey
- SetNoncePrefix
- Encrypt
- EncryptPacket
- GetKeySize
- GetNoncePrefixSize
- GetMaxPlaintextSize
- GetCiphertextSize
- GetKey
- GetNoncePrefix
- SetKey
- SetNoncePrefix
- Decrypt
- DecryptPacket
- GetKey
- GetNoncePrefix
- GetTag
- CheckTag
- GetTag
- SetImpl
- CancelImpl
- random_generator_
- GetClock
- GetRandomGenerator
- CreateAlarm
- packets_write_attempts_
- WritePacket
- IsWriteBlockedDataBuffered
- IsWriteBlocked
- SetWritable
- BlockOnNextWrite
- Reset
- header
- frame_count
- ack
- feedback
- stop_waiting
- close
- stream_frames
- last_packet_size
- version_negotiation_packet
- set_is_write_blocked_data_buffered
- set_is_server
- final_bytes_of_last_packet
- final_bytes_of_previous_packet
- use_tagging_decrypter
- packets_write_attempts
- writer_
- SendAck
- SetReceiveAlgorithm
- SetSendAlgorithm
- SetLossAlgorithm
- SendPacket
- SendStreamDataWithString
- SendStreamData3
- SendStreamData5
- EnsureWritableAndSendStreamData5
- SendCryptoStreamData
- is_server
- set_version
- SetSupportedVersions
- set_is_server
- GetAckAlarm
- GetRetransmissionAlarm
- GetSendAlarm
- GetResumeWritesAlarm
- GetTimeoutAlarm
- accept_packet_
- version
- outgoing_ack
- least_unacked
- last_ack
- last_feedback
- last_stop_waiting
- last_close
- last_header
- last_sent_packet_size
- final_bytes_of_last_packet
- final_bytes_of_previous_packet
- use_tagging_decrypter
- ProcessPacket
- ProcessFramePacket
- ProcessDataPacket
- ProcessDataPacketAtLevel
- ProcessClosePacket
- ProcessFecProtectedPacket
- ProcessFecPacket
- SendStreamDataToPeer
- SendAckPacketToPeer
- ProcessAckPacket
- ProcessStopWaitingPacket
- ProcessGoAwayPacket
- IsMissing
- ConstructDataPacket
- ConstructClosePacket
- SetFeedback
- DefaultRetransmissionTime
- DefaultDelayedAckTime
- InitAckFrame
- InitStopWaitingFrame
- NackPacket
- AckPacket
- TriggerConnectionClose
- BlockOnNextWrite
- TEST_P
- TEST_P
- TEST_P
- TEST_P
- TEST_P
- TEST_P
- TEST_P
- TEST_P
- TEST_P
- TEST_P
- TEST_P
- TEST_P
- TEST_P
- TEST_P
- TEST_P
- TEST_P
- TEST_P
- TEST_P
- TEST_P
- TEST_P
- TEST_P
- TEST_P
- TEST_P
- TEST_P
- TEST_P
- TEST_P
- TEST_P
- TEST_P
- TEST_P
- TEST_P
- TEST_P
- TEST_P
- TEST_P
- TEST_P
- TEST_P
- TEST_P
- TEST_P
- TEST_P
- TEST_P
- TEST_P
- TEST_P
- TEST_P
- TEST_P
- TEST_P
- TEST_P
- TEST_P
- TEST_P
- TEST_P
- TEST_P
- TEST_P
- TEST_P
- TEST_P
- TEST_P
- TEST_P
- TEST_P
- TEST_P
- TEST_P
- TEST_P
- TEST_P
- TEST_P
- TEST_P
- TEST_P
- TEST_P
- TEST_P
- TEST_P
- TEST_P
- TEST_P
- TEST_P
- TEST_P
- TEST_P
- TEST_P
- TEST_P
- TEST_P
- TEST_P
- TEST_P
- TEST_P
- TEST_P
- TEST_P
- TEST_P
- TEST_P
- TEST_P
- TEST_P
- TEST_P
- TEST_P
- TEST_P
- TEST_P
- TEST_P
- TEST_P
- TEST_P
- TEST_P
- TEST_P
- TEST_P
- TEST_P
- TEST_P
- TEST_P
- TEST_P
- TEST_P
- TEST_P
- TEST_P
- TEST_P
- TEST_P
- TEST_P
- TEST_P
- TEST_P
- TEST_P
- TEST_P
- TEST_P
- TEST_P
- TEST_P
- TEST_P
- TEST_P
- TEST_P
- TEST_P
- TEST_P
- TEST_P
- TEST_P
- TEST_P
- TEST_P
- TEST_P
- TEST_P
- TEST_P
#include "net/quic/quic_connection.h"
#include "base/basictypes.h"
#include "base/bind.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/receive_algorithm_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_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_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_sent_packet_manager_peer.h"
#include "net/quic/test_tools/quic_test_utils.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
using base::StringPiece;
using std::map;
using std::vector;
using testing::_;
using testing::AnyNumber;
using testing::AtLeast;
using testing::ContainerEq;
using testing::Contains;
using testing::DoAll;
using testing::InSequence;
using testing::InvokeWithoutArgs;
using testing::Ref;
using testing::Return;
using testing::SaveArg;
using testing::StrictMock;
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;
const int kMinRetransmissionTimeMs = 200;
const QuicStreamId kStreamId3 = 3;
const QuicStreamId kStreamId5 = 5;
class TestReceiveAlgorithm : public ReceiveAlgorithmInterface {
public:
explicit TestReceiveAlgorithm(QuicCongestionFeedbackFrame* feedback)
: feedback_(feedback) {
}
bool GenerateCongestionFeedback(
QuicCongestionFeedbackFrame* congestion_feedback) {
if (feedback_ == NULL) {
return false;
}
*congestion_feedback = *feedback_;
return true;
}
MOCK_METHOD3(RecordIncomingPacket,
void(QuicByteCount, QuicPacketSequenceNumber, QuicTime));
private:
QuicCongestionFeedbackFrame* feedback_;
DISALLOW_COPY_AND_ASSIGN(TestReceiveAlgorithm);
};
class TaggingEncrypter : public QuicEncrypter {
public:
explicit TaggingEncrypter(uint8 tag)
: tag_(tag) {
}
virtual ~TaggingEncrypter() {}
virtual bool SetKey(StringPiece key) OVERRIDE { return true; }
virtual bool SetNoncePrefix(StringPiece nonce_prefix) OVERRIDE {
return true;
}
virtual bool Encrypt(StringPiece nonce,
StringPiece associated_data,
StringPiece plaintext,
unsigned char* output) OVERRIDE {
memcpy(output, plaintext.data(), plaintext.size());
output += plaintext.size();
memset(output, tag_, kTagSize);
return true;
}
virtual QuicData* EncryptPacket(QuicPacketSequenceNumber sequence_number,
StringPiece associated_data,
StringPiece plaintext) OVERRIDE {
const size_t len = plaintext.size() + kTagSize;
uint8* buffer = new uint8[len];
Encrypt(StringPiece(), associated_data, plaintext, buffer);
return new QuicData(reinterpret_cast<char*>(buffer), len, true);
}
virtual size_t GetKeySize() const OVERRIDE { return 0; }
virtual size_t GetNoncePrefixSize() const OVERRIDE { return 0; }
virtual size_t GetMaxPlaintextSize(size_t ciphertext_size) const OVERRIDE {
return ciphertext_size - kTagSize;
}
virtual size_t GetCiphertextSize(size_t plaintext_size) const OVERRIDE {
return plaintext_size + kTagSize;
}
virtual StringPiece GetKey() const OVERRIDE {
return StringPiece();
}
virtual StringPiece GetNoncePrefix() const OVERRIDE {
return StringPiece();
}
private:
enum {
kTagSize = 12,
};
const uint8 tag_;
};
class TaggingDecrypter : public QuicDecrypter {
public:
virtual ~TaggingDecrypter() {}
virtual bool SetKey(StringPiece key) OVERRIDE { return true; }
virtual bool SetNoncePrefix(StringPiece nonce_prefix) OVERRIDE {
return true;
}
virtual bool Decrypt(StringPiece nonce,
StringPiece associated_data,
StringPiece ciphertext,
unsigned char* output,
size_t* 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;
}
virtual QuicData* DecryptPacket(QuicPacketSequenceNumber sequence_number,
StringPiece associated_data,
StringPiece ciphertext) OVERRIDE {
if (ciphertext.size() < kTagSize) {
return NULL;
}
if (!CheckTag(ciphertext, GetTag(ciphertext))) {
return NULL;
}
const size_t len = ciphertext.size() - kTagSize;
uint8* buf = new uint8[len];
memcpy(buf, ciphertext.data(), len);
return new QuicData(reinterpret_cast<char*>(buf), len,
true );
}
virtual StringPiece GetKey() const OVERRIDE { return StringPiece(); }
virtual StringPiece GetNoncePrefix() const OVERRIDE { return StringPiece(); }
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;
}
};
class StrictTaggingDecrypter : public TaggingDecrypter {
public:
explicit StrictTaggingDecrypter(uint8 tag) : tag_(tag) {}
virtual ~StrictTaggingDecrypter() {}
virtual uint8 GetTag(StringPiece ciphertext) OVERRIDE {
return tag_;
}
private:
const uint8 tag_;
};
class TestConnectionHelper : public QuicConnectionHelperInterface {
public:
class TestAlarm : public QuicAlarm {
public:
explicit TestAlarm(QuicAlarm::Delegate* delegate)
: QuicAlarm(delegate) {
}
virtual void SetImpl() OVERRIDE {}
virtual void CancelImpl() OVERRIDE {}
using QuicAlarm::Fire;
};
TestConnectionHelper(MockClock* clock, MockRandom* random_generator)
: clock_(clock),
random_generator_(random_generator) {
clock_->AdvanceTime(QuicTime::Delta::FromSeconds(1));
}
virtual const QuicClock* GetClock() const OVERRIDE {
return clock_;
}
virtual QuicRandom* GetRandomGenerator() OVERRIDE {
return random_generator_;
}
virtual 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:
explicit TestPacketWriter(QuicVersion version)
: version_(version),
last_packet_size_(0),
write_blocked_(false),
block_on_next_write_(false),
is_write_blocked_data_buffered_(false),
is_server_(true),
final_bytes_of_last_packet_(0),
final_bytes_of_previous_packet_(0),
use_tagging_decrypter_(false),
packets_write_attempts_(0) {
}
virtual 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_));
}
QuicFramer framer(SupportedVersions(version_),
QuicTime::Zero(), !is_server_);
if (use_tagging_decrypter_) {
framer.SetDecrypter(new TaggingDecrypter);
}
visitor_.Reset();
framer.set_visitor(&visitor_);
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();
return WriteResult(WRITE_STATUS_OK, last_packet_size_);
}
virtual bool IsWriteBlockedDataBuffered() const OVERRIDE {
return is_write_blocked_data_buffered_;
}
virtual bool IsWriteBlocked() const OVERRIDE { return write_blocked_; }
virtual void SetWritable() OVERRIDE { write_blocked_ = false; }
void BlockOnNextWrite() { block_on_next_write_ = true; }
void Reset() {
visitor_.Reset();
}
QuicPacketHeader* header() { return visitor_.header(); }
size_t frame_count() const { return visitor_.frame_count(); }
QuicAckFrame* ack() { return visitor_.ack(); }
QuicCongestionFeedbackFrame* feedback() { return visitor_.feedback(); }
QuicStopWaitingFrame* stop_waiting() { return visitor_.stop_waiting(); }
QuicConnectionCloseFrame* close() { return visitor_.close(); }
const vector<QuicStreamFrame>* stream_frames() const {
return visitor_.stream_frames();
}
size_t last_packet_size() {
return last_packet_size_;
}
QuicVersionNegotiationPacket* version_negotiation_packet() {
return visitor_.version_negotiation_packet();
}
void set_is_write_blocked_data_buffered(bool buffered) {
is_write_blocked_data_buffered_ = buffered;
}
void set_is_server(bool is_server) { is_server_ = is_server; }
uint32 final_bytes_of_last_packet() { return final_bytes_of_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_; }
private:
QuicVersion version_;
FramerVisitorCapturingFrames visitor_;
size_t last_packet_size_;
bool write_blocked_;
bool block_on_next_write_;
bool is_write_blocked_data_buffered_;
bool is_server_;
uint32 final_bytes_of_last_packet_;
uint32 final_bytes_of_previous_packet_;
bool use_tagging_decrypter_;
uint32 packets_write_attempts_;
DISALLOW_COPY_AND_ASSIGN(TestPacketWriter);
};
class TestConnection : public QuicConnection {
public:
TestConnection(QuicConnectionId connection_id,
IPEndPoint address,
TestConnectionHelper* helper,
TestPacketWriter* writer,
bool is_server,
QuicVersion version,
uint32 flow_control_send_window)
: QuicConnection(connection_id, address, helper, writer, is_server,
SupportedVersions(version),
flow_control_send_window),
writer_(writer) {
QuicSentPacketManagerPeer::SetMaxTailLossProbes(
QuicConnectionPeer::GetSentPacketManager(this), 0);
writer_->set_is_server(is_server);
}
void SendAck() {
QuicConnectionPeer::SendAck(this);
}
void SetReceiveAlgorithm(TestReceiveAlgorithm* receive_algorithm) {
QuicConnectionPeer::SetReceiveAlgorithm(this, receive_algorithm);
}
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() : NULL;
OnSerializedPacket(
SerializedPacket(sequence_number, PACKET_6BYTE_SEQUENCE_NUMBER,
packet, entropy_hash, retransmittable_frames));
}
QuicConsumedData SendStreamDataWithString(
QuicStreamId id,
StringPiece data,
QuicStreamOffset offset,
bool fin,
QuicAckNotifier::DelegateInterface* delegate) {
IOVector data_iov;
if (!data.empty()) {
data_iov.Append(const_cast<char*>(data.data()), data.size());
}
return QuicConnection::SendStreamData(id, data_iov, offset, fin, delegate);
}
QuicConsumedData SendStreamData3() {
return SendStreamDataWithString(kStreamId3, "food", 0, !kFin, NULL);
}
QuicConsumedData SendStreamData5() {
return SendStreamDataWithString(kStreamId5, "food2", 0, !kFin, NULL);
}
QuicConsumedData EnsureWritableAndSendStreamData5() {
EXPECT_TRUE(CanWriteStreamData());
return SendStreamData5();
}
QuicConsumedData SendCryptoStreamData() {
this->Flush();
QuicConsumedData consumed =
SendStreamDataWithString(kCryptoStreamId, "chlo", 0, !kFin, NULL);
this->Flush();
return consumed;
}
bool is_server() {
return QuicConnectionPeer::IsServer(this);
}
void set_version(QuicVersion version) {
QuicConnectionPeer::GetFramer(this)->set_version(version);
}
void SetSupportedVersions(const QuicVersionVector& versions) {
QuicConnectionPeer::GetFramer(this)->SetSupportedVersions(versions);
}
void set_is_server(bool is_server) {
writer_->set_is_server(is_server);
QuicPacketCreatorPeer::SetIsServer(
QuicConnectionPeer::GetPacketCreator(this), is_server);
QuicConnectionPeer::SetIsServer(this, is_server);
}
TestConnectionHelper::TestAlarm* GetAckAlarm() {
return reinterpret_cast<TestConnectionHelper::TestAlarm*>(
QuicConnectionPeer::GetAckAlarm(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* GetResumeWritesAlarm() {
return reinterpret_cast<TestConnectionHelper::TestAlarm*>(
QuicConnectionPeer::GetResumeWritesAlarm(this));
}
TestConnectionHelper::TestAlarm* GetTimeoutAlarm() {
return reinterpret_cast<TestConnectionHelper::TestAlarm*>(
QuicConnectionPeer::GetTimeoutAlarm(this));
}
using QuicConnection::SelectMutualVersion;
private:
TestPacketWriter* writer_;
DISALLOW_COPY_AND_ASSIGN(TestConnection);
};
class QuicConnectionTest : public ::testing::TestWithParam<QuicVersion> {
protected:
QuicConnectionTest()
: connection_id_(42),
framer_(SupportedVersions(version()), QuicTime::Zero(), false),
creator_(connection_id_, &framer_, &random_generator_, false),
send_algorithm_(new StrictMock<MockSendAlgorithm>),
loss_algorithm_(new MockLossAlgorithm()),
helper_(new TestConnectionHelper(&clock_, &random_generator_)),
writer_(new TestPacketWriter(version())),
connection_(connection_id_, IPEndPoint(), helper_.get(),
writer_.get(), false, version(),
kDefaultFlowControlSendWindow),
frame1_(1, false, 0, MakeIOVector(data1)),
frame2_(1, false, 3, MakeIOVector(data2)),
accept_packet_(true) {
connection_.set_visitor(&visitor_);
connection_.SetSendAlgorithm(send_algorithm_);
connection_.SetLossAlgorithm(loss_algorithm_);
framer_.set_received_entropy_calculator(&entropy_calculator_);
SetFeedback(NULL);
EXPECT_CALL(
*send_algorithm_, TimeUntilSend(_, _)).WillRepeatedly(Return(
QuicTime::Delta::Zero()));
EXPECT_CALL(*receive_algorithm_,
RecordIncomingPacket(_, _, _)).Times(AnyNumber());
EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _))
.Times(AnyNumber());
EXPECT_CALL(*send_algorithm_, RetransmissionDelay()).WillRepeatedly(
Return(QuicTime::Delta::Zero()));
EXPECT_CALL(*send_algorithm_, BandwidthEstimate()).WillRepeatedly(Return(
QuicBandwidth::FromKBitsPerSecond(100)));
ON_CALL(*send_algorithm_, OnPacketSent(_, _, _, _))
.WillByDefault(Return(true));
EXPECT_CALL(visitor_, HasPendingWrites()).Times(AnyNumber());
EXPECT_CALL(visitor_, HasPendingHandshake()).Times(AnyNumber());
EXPECT_CALL(visitor_, OnCanWrite()).Times(AnyNumber());
EXPECT_CALL(*loss_algorithm_, GetLossTimeout())
.WillRepeatedly(Return(QuicTime::Zero()));
EXPECT_CALL(*loss_algorithm_, DetectLostPackets(_, _, _, _))
.WillRepeatedly(Return(SequenceNumberSet()));
}
QuicVersion version() {
return GetParam();
}
QuicAckFrame* outgoing_ack() {
outgoing_ack_.reset(QuicConnectionPeer::CreateAckFrame(&connection_));
return outgoing_ack_.get();
}
QuicPacketSequenceNumber least_unacked() {
if (version() <= QUIC_VERSION_15) {
QuicAckFrame* ack = last_ack();
if (ack == NULL) {
return 0;
}
return ack->sent_info.least_unacked;
}
QuicStopWaitingFrame* stop_waiting = last_stop_waiting();
if (stop_waiting == NULL) {
return 0;
}
return stop_waiting->least_unacked;
}
QuicAckFrame* last_ack() {
return writer_->ack();
}
QuicCongestionFeedbackFrame* last_feedback() {
return writer_->feedback();
}
QuicStopWaitingFrame* last_stop_waiting() {
return writer_->stop_waiting();
}
QuicConnectionCloseFrame* last_close() {
return writer_->close();
}
QuicPacketHeader* last_header() {
return writer_->header();
}
size_t last_sent_packet_size() {
return writer_->last_packet_size();
}
uint32 final_bytes_of_last_packet() {
return writer_->final_bytes_of_last_packet();
}
uint32 final_bytes_of_previous_packet() {
return writer_->final_bytes_of_previous_packet();
}
void use_tagging_decrypter() {
writer_->use_tagging_decrypter();
}
void ProcessPacket(QuicPacketSequenceNumber number) {
EXPECT_CALL(visitor_, OnStreamFrames(_)).WillOnce(Return(accept_packet_));
ProcessDataPacket(number, 0, !kEntropyFlag);
}
QuicPacketEntropyHash ProcessFramePacket(QuicFrame frame) {
QuicFrames frames;
frames.push_back(QuicFrame(frame));
QuicPacketCreatorPeer::SetSendVersionInPacket(&creator_,
connection_.is_server());
SerializedPacket serialized_packet = creator_.SerializeAllFrames(frames);
scoped_ptr<QuicPacket> packet(serialized_packet.packet);
scoped_ptr<QuicEncryptedPacket> encrypted(
framer_.EncryptPacket(ENCRYPTION_NONE,
serialized_packet.sequence_number, *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));
scoped_ptr<QuicEncryptedPacket> encrypted(framer_.EncryptPacket(
level, number, *packet));
connection_.ProcessUdpPacket(IPEndPoint(), IPEndPoint(), *encrypted);
return encrypted->length();
}
void ProcessClosePacket(QuicPacketSequenceNumber number,
QuicFecGroupNumber fec_group) {
scoped_ptr<QuicPacket> packet(ConstructClosePacket(number, fec_group));
scoped_ptr<QuicEncryptedPacket> encrypted(framer_.EncryptPacket(
ENCRYPTION_NONE, number, *packet));
connection_.ProcessUdpPacket(IPEndPoint(), IPEndPoint(), *encrypted);
}
size_t ProcessFecProtectedPacket(QuicPacketSequenceNumber number,
bool expect_revival, bool entropy_flag) {
if (expect_revival) {
EXPECT_CALL(visitor_, OnStreamFrames(_)).WillOnce(Return(accept_packet_));
}
EXPECT_CALL(visitor_, OnStreamFrames(_)).WillOnce(Return(accept_packet_))
.RetiresOnSaturation();
return ProcessDataPacket(number, 1, entropy_flag);
}
size_t ProcessFecPacket(QuicPacketSequenceNumber number,
QuicPacketSequenceNumber min_protected_packet,
bool expect_revival,
bool entropy_flag,
QuicPacket* packet) {
if (expect_revival) {
EXPECT_CALL(visitor_, OnStreamFrames(_)).WillOnce(Return(accept_packet_));
}
scoped_ptr<QuicPacket> data_packet;
if (packet) {
data_packet.reset(packet);
} else {
data_packet.reset(ConstructDataPacket(number, 1, !kEntropyFlag));
}
header_.public_header.connection_id = connection_id_;
header_.public_header.reset_flag = false;
header_.public_header.version_flag = false;
header_.entropy_flag = entropy_flag;
header_.fec_flag = true;
header_.packet_sequence_number = number;
header_.is_in_fec_group = IN_FEC_GROUP;
header_.fec_group = min_protected_packet;
QuicFecData fec_data;
fec_data.fec_group = header_.fec_group;
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).packet);
scoped_ptr<QuicEncryptedPacket> encrypted(
framer_.EncryptPacket(ENCRYPTION_NONE, number, *fec_packet));
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<2>(&packet_size), Return(true)));
connection_.SendStreamDataWithString(id, data, offset, fin, NULL);
if (last_packet != NULL) {
*last_packet =
QuicConnectionPeer::GetPacketCreator(&connection_)->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());
}
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()->received_info, number);
}
QuicPacket* ConstructDataPacket(QuicPacketSequenceNumber number,
QuicFecGroupNumber fec_group,
bool entropy_flag) {
header_.public_header.connection_id = connection_id_;
header_.public_header.reset_flag = false;
header_.public_header.version_flag = false;
header_.entropy_flag = entropy_flag;
header_.fec_flag = false;
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;
QuicFrame frame(&frame1_);
frames.push_back(frame);
QuicPacket* packet =
framer_.BuildUnsizedDataPacket(header_, frames).packet;
EXPECT_TRUE(packet != NULL);
return packet;
}
QuicPacket* ConstructClosePacket(QuicPacketSequenceNumber number,
QuicFecGroupNumber fec_group) {
header_.public_header.connection_id = connection_id_;
header_.packet_sequence_number = number;
header_.public_header.reset_flag = false;
header_.public_header.version_flag = false;
header_.entropy_flag = false;
header_.fec_flag = false;
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;
QuicFrame frame(&qccf);
frames.push_back(frame);
QuicPacket* packet =
framer_.BuildUnsizedDataPacket(header_, frames).packet;
EXPECT_TRUE(packet != NULL);
return packet;
}
void SetFeedback(QuicCongestionFeedbackFrame* feedback) {
receive_algorithm_ = new TestReceiveAlgorithm(feedback);
connection_.SetReceiveAlgorithm(receive_algorithm_);
}
QuicTime::Delta DefaultRetransmissionTime() {
return QuicTime::Delta::FromMilliseconds(kDefaultRetransmissionTimeMs);
}
QuicTime::Delta DefaultDelayedAckTime() {
return QuicTime::Delta::FromMilliseconds(kMinRetransmissionTimeMs/2);
}
const QuicAckFrame InitAckFrame(QuicPacketSequenceNumber largest_observed,
QuicPacketSequenceNumber least_unacked) {
QuicAckFrame frame(largest_observed, QuicTime::Zero(), least_unacked);
if (largest_observed > 0) {
frame.received_info.entropy_hash =
QuicConnectionPeer::GetSentEntropyHash(&connection_, largest_observed);
}
return frame;
}
const QuicStopWaitingFrame InitStopWaitingFrame(
QuicPacketSequenceNumber least_unacked) {
QuicStopWaitingFrame frame;
frame.least_unacked = least_unacked;
return frame;
}
void NackPacket(QuicPacketSequenceNumber missing, QuicAckFrame* frame) {
frame->received_info.missing_packets.insert(missing);
frame->received_info.entropy_hash ^=
QuicConnectionPeer::GetSentEntropyHash(&connection_, missing);
if (missing > 1) {
frame->received_info.entropy_hash ^=
QuicConnectionPeer::GetSentEntropyHash(&connection_, missing - 1);
}
}
void AckPacket(QuicPacketSequenceNumber arrived, QuicAckFrame* frame) {
EXPECT_THAT(frame->received_info.missing_packets, Contains(arrived));
frame->received_info.missing_packets.erase(arrived);
frame->received_info.entropy_hash ^=
QuicConnectionPeer::GetSentEntropyHash(&connection_, arrived);
if (arrived > 1) {
frame->received_info.entropy_hash ^=
QuicConnectionPeer::GetSentEntropyHash(&connection_, arrived - 1);
}
}
void TriggerConnectionClose() {
EXPECT_CALL(visitor_,
OnConnectionClosed(QUIC_INVALID_PACKET_HEADER, false));
ProcessDataPacket(6000, 0, !kEntropyFlag);
EXPECT_FALSE(
QuicConnectionPeer::GetConnectionClosePacket(&connection_) == NULL);
}
void BlockOnNextWrite() {
writer_->BlockOnNextWrite();
EXPECT_CALL(visitor_, OnWriteBlocked()).Times(AtLeast(1));
}
QuicConnectionId connection_id_;
QuicFramer framer_;
QuicPacketCreator creator_;
MockEntropyCalculator entropy_calculator_;
MockSendAlgorithm* send_algorithm_;
MockLossAlgorithm* loss_algorithm_;
TestReceiveAlgorithm* receive_algorithm_;
MockClock clock_;
MockRandom random_generator_;
scoped_ptr<TestConnectionHelper> helper_;
scoped_ptr<TestPacketWriter> writer_;
TestConnection connection_;
StrictMock<MockConnectionVisitor> visitor_;
QuicPacketHeader header_;
QuicStreamFrame frame1_;
QuicStreamFrame frame2_;
scoped_ptr<QuicAckFrame> outgoing_ack_;
bool accept_packet_;
private:
DISALLOW_COPY_AND_ASSIGN(QuicConnectionTest);
};
INSTANTIATE_TEST_CASE_P(SupportedVersion,
QuicConnectionTest,
::testing::ValuesIn(QuicSupportedVersions()));
TEST_P(QuicConnectionTest, PacketsInOrder) {
EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_));
ProcessPacket(1);
EXPECT_EQ(1u, outgoing_ack()->received_info.largest_observed);
EXPECT_EQ(0u, outgoing_ack()->received_info.missing_packets.size());
ProcessPacket(2);
EXPECT_EQ(2u, outgoing_ack()->received_info.largest_observed);
EXPECT_EQ(0u, outgoing_ack()->received_info.missing_packets.size());
ProcessPacket(3);
EXPECT_EQ(3u, outgoing_ack()->received_info.largest_observed);
EXPECT_EQ(0u, outgoing_ack()->received_info.missing_packets.size());
}
TEST_P(QuicConnectionTest, PacketsRejected) {
EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_));
ProcessPacket(1);
EXPECT_EQ(1u, outgoing_ack()->received_info.largest_observed);
EXPECT_EQ(0u, outgoing_ack()->received_info.missing_packets.size());
accept_packet_ = false;
ProcessPacket(2);
EXPECT_EQ(1u, outgoing_ack()->received_info.largest_observed);
EXPECT_EQ(0u, outgoing_ack()->received_info.missing_packets.size());
}
TEST_P(QuicConnectionTest, PacketsOutOfOrder) {
EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_));
ProcessPacket(3);
EXPECT_EQ(3u, outgoing_ack()->received_info.largest_observed);
EXPECT_TRUE(IsMissing(2));
EXPECT_TRUE(IsMissing(1));
ProcessPacket(2);
EXPECT_EQ(3u, outgoing_ack()->received_info.largest_observed);
EXPECT_FALSE(IsMissing(2));
EXPECT_TRUE(IsMissing(1));
ProcessPacket(1);
EXPECT_EQ(3u, outgoing_ack()->received_info.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()->received_info.largest_observed);
EXPECT_TRUE(IsMissing(2));
EXPECT_TRUE(IsMissing(1));
ProcessDataPacket(3, 0, !kEntropyFlag);
EXPECT_EQ(3u, outgoing_ack()->received_info.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()->received_info.largest_observed);
EXPECT_TRUE(IsMissing(2));
EXPECT_TRUE(IsMissing(1));
ProcessPacket(2);
EXPECT_EQ(3u, outgoing_ack()->received_info.largest_observed);
EXPECT_TRUE(IsMissing(1));
ProcessPacket(5);
EXPECT_EQ(5u, outgoing_ack()->received_info.largest_observed);
EXPECT_TRUE(IsMissing(1));
EXPECT_TRUE(IsMissing(4));
creator_.set_sequence_number(5);
QuicAckFrame frame = InitAckFrame(0, 4);
ProcessAckPacket(&frame);
SendAckPacketToPeer();
EXPECT_TRUE(IsMissing(4));
}
TEST_P(QuicConnectionTest, RejectPacketTooFarOut) {
EXPECT_CALL(visitor_,
OnConnectionClosed(QUIC_INVALID_PACKET_HEADER, false));
ProcessDataPacket(6000, 0, !kEntropyFlag);
EXPECT_FALSE(
QuicConnectionPeer::GetConnectionClosePacket(&connection_) == NULL);
}
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, NULL);
}
QuicAckFrame frame = InitAckFrame(num_packets, 1);
SequenceNumberSet lost_packets;
for (QuicPacketSequenceNumber i = 1; i <= 256; ++i) {
NackPacket(i * 2, &frame);
if (i < 256) {
lost_packets.insert(i * 2);
}
}
EXPECT_CALL(*loss_algorithm_, DetectLostPackets(_, _, _, _))
.WillOnce(Return(lost_packets));
EXPECT_CALL(entropy_calculator_,
EntropyHash(511)).WillOnce(testing::Return(0));
EXPECT_CALL(*send_algorithm_, UpdateRtt(_));
EXPECT_CALL(*send_algorithm_, OnPacketAcked(_, _)).Times(256);
EXPECT_CALL(*send_algorithm_, OnPacketLost(_, _)).Times(255);
EXPECT_CALL(*send_algorithm_, OnPacketAbandoned(_, _)).Times(255);
ProcessAckPacket(&frame);
QuicReceivedPacketManager* received_packet_manager =
QuicConnectionPeer::GetReceivedPacketManager(&connection_);
EXPECT_GT(num_packets,
received_packet_manager->peer_largest_observed_packet());
AckPacket(192, &frame);
EXPECT_CALL(*loss_algorithm_, DetectLostPackets(_, _, _, _))
.WillOnce(Return(SequenceNumberSet()));
EXPECT_CALL(*send_algorithm_, UpdateRtt(_));
EXPECT_CALL(*send_algorithm_, OnPacketAcked(_, _)).Times(1);
ProcessAckPacket(&frame);
EXPECT_EQ(num_packets,
received_packet_manager->peer_largest_observed_packet());
}
TEST_P(QuicConnectionTest, AckReceiptCausesAckSendBadEntropy) {
EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_));
ProcessPacket(1);
EXPECT_CALL(*send_algorithm_,
TimeUntilSend(_, _)).WillOnce(
testing::Return(QuicTime::Delta::FromMicroseconds(1)));
QuicConnectionPeer::SendAck(&connection_);
EXPECT_CALL(*send_algorithm_,
TimeUntilSend(_, _)).WillRepeatedly(
testing::Return(QuicTime::Delta::Zero()));
creator_.set_sequence_number(2);
QuicAckFrame frame = InitAckFrame(0, 3);
ProcessAckPacket(&frame);
}
TEST_P(QuicConnectionTest, OutOfOrderReceiptCausesAckSend) {
EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_));
ProcessPacket(3);
EXPECT_EQ(1u, writer_->packets_write_attempts());
ProcessPacket(2);
EXPECT_EQ(2u, writer_->packets_write_attempts());
ProcessPacket(1);
EXPECT_EQ(3u, writer_->packets_write_attempts());
ProcessPacket(4);
EXPECT_EQ(3u, 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<1>(&original), SaveArg<2>(&packet_size),
Return(true)));
connection_.SendStreamDataWithString(3, "foo", 0, !kFin, NULL);
QuicAckFrame frame = InitAckFrame(original, 1);
NackPacket(original, &frame);
SequenceNumberSet lost_packets;
lost_packets.insert(1);
EXPECT_CALL(*loss_algorithm_, DetectLostPackets(_, _, _, _))
.WillOnce(Return(lost_packets));
EXPECT_CALL(*send_algorithm_, UpdateRtt(_));
EXPECT_CALL(*send_algorithm_, OnPacketLost(1, _)).Times(1);
EXPECT_CALL(*send_algorithm_, OnPacketAbandoned(1, _)).Times(1);
QuicPacketSequenceNumber retransmission;
EXPECT_CALL(*send_algorithm_,
OnPacketSent(_, _, packet_size - kQuicVersionSize, _))
.WillOnce(DoAll(SaveArg<1>(&retransmission), Return(true)));
ProcessAckPacket(&frame);
QuicAckFrame frame2 = InitAckFrame(retransmission, 1);
NackPacket(original, &frame2);
EXPECT_CALL(*send_algorithm_, UpdateRtt(_));
EXPECT_CALL(*send_algorithm_, OnPacketAcked(_, _));
EXPECT_CALL(*loss_algorithm_, DetectLostPackets(_, _, _, _))
.WillOnce(Return(SequenceNumberSet()));
ProcessAckPacket(&frame2);
EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _,
HAS_RETRANSMITTABLE_DATA));
connection_.SendStreamDataWithString(3, "foo", 3, !kFin, NULL);
EXPECT_EQ(1u, writer_->frame_count());
EXPECT_EQ(1u, writer_->stream_frames()->size());
writer_->Reset();
EXPECT_CALL(*loss_algorithm_, DetectLostPackets(_, _, _, _))
.WillRepeatedly(Return(SequenceNumberSet()));
ProcessAckPacket(&frame2);
EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _,
HAS_RETRANSMITTABLE_DATA));
connection_.SendStreamDataWithString(3, "foo", 3, !kFin, NULL);
if (version() > QUIC_VERSION_15) {
EXPECT_EQ(3u, writer_->frame_count());
} else {
EXPECT_EQ(2u, writer_->frame_count());
}
EXPECT_EQ(1u, writer_->stream_frames()->size());
EXPECT_TRUE(writer_->ack());
AckPacket(original, &frame2);
ProcessAckPacket(&frame2);
ProcessAckPacket(&frame2);
}
TEST_P(QuicConnectionTest, LeastUnackedLower) {
EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_));
SendStreamDataToPeer(1, "foo", 0, !kFin, NULL);
SendStreamDataToPeer(1, "bar", 3, !kFin, NULL);
SendStreamDataToPeer(1, "eep", 6, !kFin, NULL);
creator_.set_sequence_number(5);
if (version() > QUIC_VERSION_15) {
QuicStopWaitingFrame frame = InitStopWaitingFrame(2);
ProcessStopWaitingPacket(&frame);
} else {
QuicAckFrame frame = InitAckFrame(0, 2);
ProcessAckPacket(&frame);
}
creator_.set_sequence_number(1);
EXPECT_CALL(visitor_, OnCanWrite());
if (version() > QUIC_VERSION_15) {
QuicStopWaitingFrame frame2 = InitStopWaitingFrame(1);
ProcessStopWaitingPacket(&frame2);
} else {
QuicAckFrame frame2 = InitAckFrame(0, 1);
ProcessAckPacket(&frame2);
}
EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _));
creator_.set_sequence_number(7);
if (version() > QUIC_VERSION_15) {
EXPECT_CALL(visitor_,
OnConnectionClosed(QUIC_INVALID_STOP_WAITING_DATA, false));
QuicStopWaitingFrame frame2 = InitStopWaitingFrame(1);
ProcessStopWaitingPacket(&frame2);
} else {
EXPECT_CALL(visitor_, OnConnectionClosed(QUIC_INVALID_ACK_DATA, false));
QuicAckFrame frame2 = InitAckFrame(0, 1);
ProcessAckPacket(&frame2);
}
}
TEST_P(QuicConnectionTest, LargestObservedLower) {
EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_));
SendStreamDataToPeer(1, "foo", 0, !kFin, NULL);
SendStreamDataToPeer(1, "bar", 3, !kFin, NULL);
SendStreamDataToPeer(1, "eep", 6, !kFin, NULL);
EXPECT_CALL(*send_algorithm_, UpdateRtt(_));
EXPECT_CALL(*send_algorithm_, OnPacketAcked(_, _)).Times(2);
QuicAckFrame frame1 = InitAckFrame(1, 0);
QuicAckFrame frame2 = InitAckFrame(2, 0);
ProcessAckPacket(&frame2);
EXPECT_CALL(visitor_, OnConnectionClosed(QUIC_INVALID_ACK_DATA, false));
EXPECT_CALL(visitor_, OnCanWrite()).Times(0);
ProcessAckPacket(&frame1);
}
TEST_P(QuicConnectionTest, AckUnsentData) {
EXPECT_CALL(visitor_, OnConnectionClosed(QUIC_INVALID_ACK_DATA, false));
EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_));
EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _));
QuicAckFrame frame(1, QuicTime::Zero(), 0);
EXPECT_CALL(visitor_, OnCanWrite()).Times(0);
ProcessAckPacket(&frame);
}
TEST_P(QuicConnectionTest, AckAll) {
EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_));
ProcessPacket(1);
creator_.set_sequence_number(1);
QuicAckFrame frame1 = InitAckFrame(0, 1);
ProcessAckPacket(&frame1);
}
TEST_P(QuicConnectionTest, SendingDifferentSequenceNumberLengthsBandwidth) {
EXPECT_CALL(*send_algorithm_, BandwidthEstimate()).WillOnce(Return(
QuicBandwidth::FromKBitsPerSecond(1000)));
QuicPacketSequenceNumber last_packet;
SendStreamDataToPeer(1, "foo", 0, !kFin, &last_packet);
EXPECT_EQ(1u, last_packet);
EXPECT_EQ(PACKET_1BYTE_SEQUENCE_NUMBER,
connection_.options()->send_sequence_number_length);
EXPECT_EQ(PACKET_1BYTE_SEQUENCE_NUMBER,
last_header()->public_header.sequence_number_length);
EXPECT_CALL(*send_algorithm_, BandwidthEstimate()).WillOnce(Return(
QuicBandwidth::FromKBitsPerSecond(1000 * 256)));
SendStreamDataToPeer(1, "bar", 3, !kFin, &last_packet);
EXPECT_EQ(2u, last_packet);
EXPECT_EQ(PACKET_2BYTE_SEQUENCE_NUMBER,
connection_.options()->send_sequence_number_length);
EXPECT_EQ(PACKET_1BYTE_SEQUENCE_NUMBER,
last_header()->public_header.sequence_number_length);
EXPECT_CALL(*send_algorithm_, BandwidthEstimate()).WillOnce(Return(
QuicBandwidth::FromKBitsPerSecond(1000 * 256 * 256)));
SendStreamDataToPeer(1, "foo", 6, !kFin, &last_packet);
EXPECT_EQ(3u, last_packet);
EXPECT_EQ(PACKET_4BYTE_SEQUENCE_NUMBER,
connection_.options()->send_sequence_number_length);
EXPECT_EQ(PACKET_2BYTE_SEQUENCE_NUMBER,
last_header()->public_header.sequence_number_length);
EXPECT_CALL(*send_algorithm_, BandwidthEstimate()).WillOnce(Return(
QuicBandwidth::FromKBitsPerSecond(1000ll * 256 * 256 * 256)));
SendStreamDataToPeer(1, "bar", 9, !kFin, &last_packet);
EXPECT_EQ(4u, last_packet);
EXPECT_EQ(PACKET_4BYTE_SEQUENCE_NUMBER,
connection_.options()->send_sequence_number_length);
EXPECT_EQ(PACKET_4BYTE_SEQUENCE_NUMBER,
last_header()->public_header.sequence_number_length);
EXPECT_CALL(*send_algorithm_, BandwidthEstimate()).WillOnce(Return(
QuicBandwidth::FromKBitsPerSecond(1000ll * 256 * 256 * 256 * 256)));
SendStreamDataToPeer(1, "foo", 12, !kFin, &last_packet);
EXPECT_EQ(5u, last_packet);
EXPECT_EQ(PACKET_6BYTE_SEQUENCE_NUMBER,
connection_.options()->send_sequence_number_length);
EXPECT_EQ(PACKET_4BYTE_SEQUENCE_NUMBER,
last_header()->public_header.sequence_number_length);
}
TEST_P(QuicConnectionTest, SendingDifferentSequenceNumberLengthsUnackedDelta) {
QuicPacketSequenceNumber last_packet;
SendStreamDataToPeer(1, "foo", 0, !kFin, &last_packet);
EXPECT_EQ(1u, last_packet);
EXPECT_EQ(PACKET_1BYTE_SEQUENCE_NUMBER,
connection_.options()->send_sequence_number_length);
EXPECT_EQ(PACKET_1BYTE_SEQUENCE_NUMBER,
last_header()->public_header.sequence_number_length);
QuicConnectionPeer::GetPacketCreator(&connection_)->set_sequence_number(100);
SendStreamDataToPeer(1, "bar", 3, !kFin, &last_packet);
EXPECT_EQ(PACKET_2BYTE_SEQUENCE_NUMBER,
connection_.options()->send_sequence_number_length);
EXPECT_EQ(PACKET_1BYTE_SEQUENCE_NUMBER,
last_header()->public_header.sequence_number_length);
QuicConnectionPeer::GetPacketCreator(&connection_)->set_sequence_number(
100 * 256);
SendStreamDataToPeer(1, "foo", 6, !kFin, &last_packet);
EXPECT_EQ(PACKET_4BYTE_SEQUENCE_NUMBER,
connection_.options()->send_sequence_number_length);
EXPECT_EQ(PACKET_2BYTE_SEQUENCE_NUMBER,
last_header()->public_header.sequence_number_length);
QuicConnectionPeer::GetPacketCreator(&connection_)->set_sequence_number(
100 * 256 * 256);
SendStreamDataToPeer(1, "bar", 9, !kFin, &last_packet);
EXPECT_EQ(PACKET_4BYTE_SEQUENCE_NUMBER,
connection_.options()->send_sequence_number_length);
EXPECT_EQ(PACKET_4BYTE_SEQUENCE_NUMBER,
last_header()->public_header.sequence_number_length);
QuicConnectionPeer::GetPacketCreator(&connection_)->set_sequence_number(
100 * 256 * 256 * 256);
SendStreamDataToPeer(1, "foo", 12, !kFin, &last_packet);
EXPECT_EQ(PACKET_6BYTE_SEQUENCE_NUMBER,
connection_.options()->send_sequence_number_length);
EXPECT_EQ(PACKET_4BYTE_SEQUENCE_NUMBER,
last_header()->public_header.sequence_number_length);
}
TEST_P(QuicConnectionTest, BasicSending) {
EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_));
QuicPacketSequenceNumber last_packet;
SendStreamDataToPeer(1, "foo", 0, !kFin, &last_packet);
EXPECT_EQ(1u, last_packet);
SendAckPacketToPeer();
EXPECT_EQ(1u, least_unacked());
SendAckPacketToPeer();
EXPECT_EQ(1u, least_unacked());
SendStreamDataToPeer(1, "bar", 3, !kFin, &last_packet);
EXPECT_EQ(4u, last_packet);
SendAckPacketToPeer();
EXPECT_EQ(1u, least_unacked());
EXPECT_CALL(*send_algorithm_, UpdateRtt(_));
EXPECT_CALL(*send_algorithm_, OnPacketAcked(_, _)).Times(3);
QuicAckFrame frame = InitAckFrame(3, 0);
ProcessAckPacket(&frame);
SendAckPacketToPeer();
EXPECT_EQ(4u, least_unacked());
EXPECT_CALL(*send_algorithm_, UpdateRtt(_));
EXPECT_CALL(*send_algorithm_, OnPacketAcked(_, _)).Times(3);
QuicAckFrame frame2 = InitAckFrame(6, 0);
ProcessAckPacket(&frame2);
EXPECT_EQ(6u, last_header()->packet_sequence_number);
EXPECT_EQ(4u, least_unacked());
SendAckPacketToPeer();
EXPECT_EQ(7u, least_unacked());
SendStreamDataToPeer(1, "eep", 6, !kFin, &last_packet);
EXPECT_EQ(8u, last_packet);
SendAckPacketToPeer();
EXPECT_EQ(7u, least_unacked());
}
TEST_P(QuicConnectionTest, FECSending) {
if (version() < QUIC_VERSION_15) {
return;
}
size_t payload_length;
connection_.options()->max_packet_length =
GetPacketLengthForOneStream(
connection_.version(), kIncludeVersion, PACKET_1BYTE_SEQUENCE_NUMBER,
IN_FEC_GROUP, &payload_length);
connection_.options()->max_packets_per_fec_group = 2;
EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _)).Times(6);
const string payload(payload_length * 4 - 6, 'a');
connection_.SendStreamDataWithString(1, payload, 0, !kFin, NULL);
EXPECT_FALSE(creator_.ShouldSendFec(true));
}
TEST_P(QuicConnectionTest, FECQueueing) {
if (version() < QUIC_VERSION_15) {
return;
}
size_t payload_length;
connection_.options()->max_packet_length =
GetPacketLengthForOneStream(
connection_.version(), kIncludeVersion, PACKET_1BYTE_SEQUENCE_NUMBER,
IN_FEC_GROUP, &payload_length);
connection_.options()->max_packets_per_fec_group = 2;
EXPECT_EQ(0u, connection_.NumQueuedPackets());
BlockOnNextWrite();
const string payload(payload_length, 'a');
connection_.SendStreamDataWithString(1, payload, 0, !kFin, NULL);
EXPECT_FALSE(creator_.ShouldSendFec(true));
EXPECT_EQ(2u, connection_.NumQueuedPackets());
}
TEST_P(QuicConnectionTest, AbandonFECFromCongestionWindow) {
if (version() < QUIC_VERSION_15) {
return;
}
connection_.options()->max_packets_per_fec_group = 1;
EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _)).Times(2);
connection_.SendStreamDataWithString(3, "foo", 0, !kFin, NULL);
const QuicTime::Delta retransmission_time =
QuicTime::Delta::FromMilliseconds(5000);
clock_.AdvanceTime(retransmission_time);
EXPECT_CALL(*send_algorithm_, OnRetransmissionTimeout(true));
EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _)).Times(1);
EXPECT_CALL(visitor_, OnCanWrite());
connection_.OnRetransmissionTimeout();
}
TEST_P(QuicConnectionTest, DontAbandonAckedFEC) {
if (version() < QUIC_VERSION_15) {
return;
}
EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_));
connection_.options()->max_packets_per_fec_group = 1;
EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _)).Times(6);
connection_.SendStreamDataWithString(3, "foo", 0, !kFin, NULL);
connection_.SendStreamDataWithString(3, "foo", 3, !kFin, NULL);
connection_.SendStreamDataWithString(3, "foo", 6, !kFin, NULL);
QuicAckFrame ack_fec = InitAckFrame(2, 1);
NackPacket(1, &ack_fec);
EXPECT_CALL(*send_algorithm_, UpdateRtt(_));
EXPECT_CALL(*send_algorithm_, OnPacketAcked(_, _)).Times(1);
ProcessAckPacket(&ack_fec);
clock_.AdvanceTime(DefaultRetransmissionTime());
EXPECT_CALL(*send_algorithm_, OnRetransmissionTimeout(true));
EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _)).Times(3);
connection_.GetRetransmissionAlarm()->Fire();
}
TEST_P(QuicConnectionTest, AbandonAllFEC) {
if (version() < QUIC_VERSION_15) {
return;
}
EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_));
connection_.options()->max_packets_per_fec_group = 1;
EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _)).Times(6);
connection_.SendStreamDataWithString(3, "foo", 0, !kFin, NULL);
connection_.SendStreamDataWithString(3, "foo", 3, !kFin, NULL);
clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(1));
connection_.SendStreamDataWithString(3, "foo", 6, !kFin, NULL);
QuicAckFrame ack_fec = InitAckFrame(5, 1);
NackPacket(2, &ack_fec);
NackPacket(4, &ack_fec);
SequenceNumberSet lost_packets;
lost_packets.insert(2);
EXPECT_CALL(*loss_algorithm_, DetectLostPackets(_, _, _, _))
.WillOnce(Return(lost_packets));
EXPECT_CALL(*send_algorithm_, UpdateRtt(_));
EXPECT_CALL(*send_algorithm_, OnPacketAcked(_, _)).Times(3);
EXPECT_CALL(*send_algorithm_, OnPacketAbandoned(2, _));
EXPECT_CALL(*send_algorithm_, OnPacketLost(2, _));
ProcessAckPacket(&ack_fec);
clock_.AdvanceTime(DefaultRetransmissionTime().Subtract(
QuicTime::Delta::FromMilliseconds(1)));
EXPECT_CALL(*send_algorithm_, OnRetransmissionTimeout(false));
connection_.GetRetransmissionAlarm()->Fire();
EXPECT_FALSE(connection_.GetRetransmissionAlarm()->IsSet());
}
TEST_P(QuicConnectionTest, FramePacking) {
connection_.GetSendAlarm()->Set(
clock_.ApproximateNow().Add(QuicTime::Delta::FromSeconds(1)));
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);
connection_.GetSendAlarm()->Fire();
EXPECT_EQ(0u, connection_.NumQueuedPackets());
EXPECT_FALSE(connection_.HasQueuedData());
if (version() > QUIC_VERSION_15) {
EXPECT_EQ(4u, writer_->frame_count());
EXPECT_TRUE(writer_->stop_waiting());
} else {
EXPECT_EQ(3u, writer_->frame_count());
}
EXPECT_TRUE(writer_->ack());
EXPECT_EQ(2u, writer_->stream_frames()->size());
EXPECT_EQ(kStreamId3, (*writer_->stream_frames())[0].stream_id);
EXPECT_EQ(kStreamId5, (*writer_->stream_frames())[1].stream_id);
}
TEST_P(QuicConnectionTest, FramePackingNonCryptoThenCrypto) {
connection_.GetSendAlarm()->Set(
clock_.ApproximateNow().Add(QuicTime::Delta::FromSeconds(1)));
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);
connection_.GetSendAlarm()->Fire();
EXPECT_EQ(0u, connection_.NumQueuedPackets());
EXPECT_FALSE(connection_.HasQueuedData());
EXPECT_EQ(1u, writer_->frame_count());
EXPECT_EQ(1u, writer_->stream_frames()->size());
EXPECT_EQ(kCryptoStreamId, (*writer_->stream_frames())[0].stream_id);
}
TEST_P(QuicConnectionTest, FramePackingCryptoThenNonCrypto) {
connection_.GetSendAlarm()->Set(
clock_.ApproximateNow().Add(QuicTime::Delta::FromSeconds(1)));
connection_.SendAck();
EXPECT_CALL(visitor_, OnCanWrite()).WillOnce(DoAll(
IgnoreResult(InvokeWithoutArgs(&connection_,
&TestConnection::SendCryptoStreamData)),
IgnoreResult(InvokeWithoutArgs(&connection_,
&TestConnection::SendStreamData3))));
EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _)).Times(3);
connection_.GetSendAlarm()->Fire();
EXPECT_EQ(0u, connection_.NumQueuedPackets());
EXPECT_FALSE(connection_.HasQueuedData());
EXPECT_EQ(1u, writer_->frame_count());
EXPECT_EQ(1u, writer_->stream_frames()->size());
EXPECT_EQ(kStreamId3, (*writer_->stream_frames())[0].stream_id);
}
TEST_P(QuicConnectionTest, FramePackingFEC) {
if (version() < QUIC_VERSION_15) {
return;
}
connection_.options()->max_packets_per_fec_group = 6;
connection_.GetSendAlarm()->Set(
clock_.ApproximateNow().Add(QuicTime::Delta::FromSeconds(1)));
connection_.SendAck();
EXPECT_CALL(visitor_, OnCanWrite()).WillOnce(DoAll(
IgnoreResult(InvokeWithoutArgs(&connection_,
&TestConnection::SendStreamData3)),
IgnoreResult(InvokeWithoutArgs(&connection_,
&TestConnection::SendStreamData5))));
EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _)).Times(2);
connection_.GetSendAlarm()->Fire();
EXPECT_EQ(0u, connection_.NumQueuedPackets());
EXPECT_FALSE(connection_.HasQueuedData());
EXPECT_EQ(1u, writer_->header()->fec_group);
EXPECT_EQ(0u, writer_->frame_count());
}
TEST_P(QuicConnectionTest, FramePackingAckResponse) {
EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_));
EXPECT_CALL(visitor_, OnStreamFrames(_)).WillOnce(Return(true));
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);
creator_.set_sequence_number(2);
QuicAckFrame ack_one = InitAckFrame(0, 0);
ProcessAckPacket(&ack_one);
EXPECT_EQ(0u, connection_.NumQueuedPackets());
EXPECT_FALSE(connection_.HasQueuedData());
if (version() > QUIC_VERSION_15) {
EXPECT_EQ(4u, writer_->frame_count());
EXPECT_TRUE(writer_->stop_waiting());
} else {
EXPECT_EQ(3u, writer_->frame_count());
}
EXPECT_TRUE(writer_->ack());
ASSERT_EQ(2u, writer_->stream_frames()->size());
EXPECT_EQ(kStreamId3, (*writer_->stream_frames())[0].stream_id);
EXPECT_EQ(kStreamId5, (*writer_->stream_frames())[1].stream_id);
}
TEST_P(QuicConnectionTest, FramePackingSendv) {
EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _));
char data[] = "ABCD";
IOVector data_iov;
data_iov.AppendNoCoalesce(data, 2);
data_iov.AppendNoCoalesce(data + 2, 2);
connection_.SendStreamData(1, data_iov, 0, !kFin, NULL);
EXPECT_EQ(0u, connection_.NumQueuedPackets());
EXPECT_FALSE(connection_.HasQueuedData());
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", string(static_cast<char*>
(frame.data.iovec()[0].iov_base),
(frame.data.iovec()[0].iov_len)));
}
TEST_P(QuicConnectionTest, FramePackingSendvQueued) {
EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _));
BlockOnNextWrite();
char data[] = "ABCD";
IOVector data_iov;
data_iov.AppendNoCoalesce(data, 2);
data_iov.AppendNoCoalesce(data + 2, 2);
connection_.SendStreamData(1, data_iov, 0, !kFin, NULL);
EXPECT_EQ(1u, connection_.NumQueuedPackets());
EXPECT_TRUE(connection_.HasQueuedData());
writer_->SetWritable();
connection_.OnCanWrite();
EXPECT_EQ(0u, connection_.NumQueuedPackets());
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) {
EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _));
IOVector empty_iov;
connection_.SendStreamData(1, empty_iov, 0, kFin, NULL);
EXPECT_EQ(0u, connection_.NumQueuedPackets());
EXPECT_FALSE(connection_.HasQueuedData());
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) {
EXPECT_CALL(visitor_, OnCanWrite()).WillOnce(DoAll(
IgnoreResult(InvokeWithoutArgs(&connection_,
&TestConnection::SendStreamData3)),
IgnoreResult(InvokeWithoutArgs(&connection_,
&TestConnection::SendStreamData5))));
EXPECT_CALL(visitor_, HasPendingWrites()).WillOnce(Return(true));
EXPECT_CALL(*send_algorithm_,
TimeUntilSend(_, _)).WillRepeatedly(
testing::Return(QuicTime::Delta::Zero()));
connection_.OnCanWrite();
EXPECT_EQ(2u, writer_->frame_count());
EXPECT_EQ(2u, writer_->stream_frames()->size());
EXPECT_EQ(kStreamId3, (*writer_->stream_frames())[0].stream_id);
EXPECT_EQ(kStreamId5, (*writer_->stream_frames())[1].stream_id);
}
TEST_P(QuicConnectionTest, RetransmitOnNack) {
QuicPacketSequenceNumber last_packet;
QuicByteCount second_packet_size;
SendStreamDataToPeer(3, "foo", 0, !kFin, &last_packet);
second_packet_size =
SendStreamDataToPeer(3, "foos", 3, !kFin, &last_packet);
SendStreamDataToPeer(3, "fooos", 7, !kFin, &last_packet);
EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_));
EXPECT_CALL(*send_algorithm_, UpdateRtt(_));
EXPECT_CALL(*send_algorithm_, OnPacketAcked(1, _));
QuicAckFrame ack_one = InitAckFrame(1, 0);
ProcessAckPacket(&ack_one);
QuicAckFrame nack_two = InitAckFrame(3, 0);
NackPacket(2, &nack_two);
SequenceNumberSet lost_packets;
lost_packets.insert(2);
EXPECT_CALL(*loss_algorithm_, DetectLostPackets(_, _, _, _))
.WillOnce(Return(lost_packets));
EXPECT_CALL(*send_algorithm_, UpdateRtt(_));
EXPECT_CALL(*send_algorithm_, OnPacketAcked(3, _));
EXPECT_CALL(*send_algorithm_, OnPacketLost(2, _)).Times(1);
EXPECT_CALL(*send_algorithm_, OnPacketAbandoned(2, _)).Times(1);
EXPECT_CALL(*send_algorithm_,
OnPacketSent(_, _, second_packet_size - kQuicVersionSize, _)).
Times(1);
ProcessAckPacket(&nack_two);
}
TEST_P(QuicConnectionTest, DiscardRetransmit) {
QuicPacketSequenceNumber last_packet;
SendStreamDataToPeer(1, "foo", 0, !kFin, &last_packet);
SendStreamDataToPeer(1, "foos", 3, !kFin, &last_packet);
SendStreamDataToPeer(1, "fooos", 7, !kFin, &last_packet);
EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_));
QuicAckFrame nack_two = InitAckFrame(3, 0);
NackPacket(2, &nack_two);
BlockOnNextWrite();
SequenceNumberSet lost_packets;
lost_packets.insert(2);
EXPECT_CALL(*loss_algorithm_, DetectLostPackets(_, _, _, _))
.WillOnce(Return(lost_packets));
EXPECT_CALL(*send_algorithm_, UpdateRtt(_));
EXPECT_CALL(*send_algorithm_, OnPacketAcked(_, _)).Times(2);
EXPECT_CALL(*send_algorithm_, OnPacketLost(2, _));
EXPECT_CALL(*send_algorithm_, OnPacketAbandoned(2, _));
ProcessAckPacket(&nack_two);
EXPECT_EQ(1u, connection_.NumQueuedPackets());
EXPECT_CALL(*loss_algorithm_, DetectLostPackets(_, _, _, _))
.WillOnce(Return(SequenceNumberSet()));
QuicAckFrame ack_all = InitAckFrame(3, 0);
ProcessAckPacket(&ack_all);
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<1>(&largest_observed), SaveArg<2>(&packet_size),
Return(true)));
connection_.SendStreamDataWithString(3, "foo", 0, !kFin, NULL);
QuicAckFrame frame = InitAckFrame(1, largest_observed);
NackPacket(largest_observed, &frame);
SequenceNumberSet lost_packets;
lost_packets.insert(1);
EXPECT_CALL(*loss_algorithm_, DetectLostPackets(_, _, _, _))
.WillOnce(Return(lost_packets));
EXPECT_CALL(*send_algorithm_, UpdateRtt(_));
EXPECT_CALL(*send_algorithm_, OnPacketLost(1, _)).Times(1);
EXPECT_CALL(*send_algorithm_, OnPacketAbandoned(1, _)).Times(1);
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, NULL);
}
BlockOnNextWrite();
clock_.AdvanceTime(DefaultRetransmissionTime());
EXPECT_CALL(*send_algorithm_, OnRetransmissionTimeout(true));
connection_.GetRetransmissionAlarm()->Fire();
EXPECT_TRUE(connection_.HasQueuedData());
writer_->SetWritable();
clock_.AdvanceTime(QuicTime::Delta::FromMicroseconds(
2 * DefaultRetransmissionTime().ToMicroseconds()));
EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _)).Times(10);
connection_.GetRetransmissionAlarm()->Fire();
connection_.OnCanWrite();
}
TEST_P(QuicConnectionTest, WriteBlockedThenSent) {
BlockOnNextWrite();
writer_->set_is_write_blocked_data_buffered(true);
connection_.SendStreamDataWithString(1, "foo", 0, !kFin, NULL);
EXPECT_FALSE(connection_.GetRetransmissionAlarm()->IsSet());
EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _)).Times(1);
connection_.OnPacketSent(WriteResult(WRITE_STATUS_OK, 0));
EXPECT_TRUE(connection_.GetRetransmissionAlarm()->IsSet());
}
TEST_P(QuicConnectionTest, WriteBlockedAckedThenSent) {
EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_));
BlockOnNextWrite();
writer_->set_is_write_blocked_data_buffered(true);
connection_.SendStreamDataWithString(1, "foo", 0, !kFin, NULL);
EXPECT_FALSE(connection_.GetRetransmissionAlarm()->IsSet());
QuicAckFrame ack = InitAckFrame(1, 0);
ProcessAckPacket(&ack);
EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _)).Times(0);
connection_.OnPacketSent(WriteResult(WRITE_STATUS_OK, 0));
EXPECT_FALSE(connection_.GetRetransmissionAlarm()->IsSet());
}
TEST_P(QuicConnectionTest, RetransmitWriteBlockedAckedOriginalThenSent) {
EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_));
connection_.SendStreamDataWithString(3, "foo", 0, !kFin, NULL);
EXPECT_TRUE(connection_.GetRetransmissionAlarm()->IsSet());
BlockOnNextWrite();
writer_->set_is_write_blocked_data_buffered(true);
EXPECT_CALL(*send_algorithm_, OnRetransmissionTimeout(_));
clock_.AdvanceTime(DefaultRetransmissionTime());
connection_.GetRetransmissionAlarm()->Fire();
EXPECT_CALL(*send_algorithm_, UpdateRtt(_));
QuicAckFrame ack = InitAckFrame(1, 0);
ProcessAckPacket(&ack);
connection_.OnPacketSent(WriteResult(WRITE_STATUS_OK, 0));
EXPECT_FALSE(connection_.GetRetransmissionAlarm()->IsSet());
}
TEST_P(QuicConnectionTest, AlarmsWhenWriteBlocked) {
BlockOnNextWrite();
connection_.SendStreamDataWithString(3, "foo", 0, !kFin, NULL);
EXPECT_EQ(1u, writer_->packets_write_attempts());
EXPECT_TRUE(writer_->IsWriteBlocked());
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) {
EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_));
int offset = 0;
for (int i = 0; i < 15; ++i) {
SendStreamDataToPeer(1, "foo", offset, !kFin, NULL);
offset += 3;
}
SequenceNumberSet lost_packets;
QuicAckFrame nack = InitAckFrame(15, 0);
for (int i = 1; i < 15; ++i) {
NackPacket(i, &nack);
lost_packets.insert(i);
}
EXPECT_CALL(*loss_algorithm_, DetectLostPackets(_, _, _, _))
.WillOnce(Return(lost_packets));
EXPECT_CALL(*send_algorithm_, UpdateRtt(_));
EXPECT_CALL(*send_algorithm_, OnPacketAcked(15, _)).Times(1);
EXPECT_CALL(*send_algorithm_, OnPacketAbandoned(_, _)).Times(14);
EXPECT_CALL(*send_algorithm_, OnPacketLost(_, _)).Times(14);
EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _)).Times(14);
ProcessAckPacket(&nack);
}
TEST_P(QuicConnectionTest, MultipleAcks) {
QuicPacketSequenceNumber last_packet;
SendStreamDataToPeer(1, "foo", 0, !kFin, &last_packet);
EXPECT_EQ(1u, last_packet);
SendStreamDataToPeer(3, "foo", 0, !kFin, &last_packet);
EXPECT_EQ(2u, last_packet);
SendAckPacketToPeer();
SendStreamDataToPeer(5, "foo", 0, !kFin, &last_packet);
EXPECT_EQ(4u, last_packet);
SendStreamDataToPeer(1, "foo", 3, !kFin, &last_packet);
EXPECT_EQ(5u, last_packet);
SendStreamDataToPeer(3, "foo", 3, !kFin, &last_packet);
EXPECT_EQ(6u, last_packet);
EXPECT_CALL(*send_algorithm_, UpdateRtt(_));
EXPECT_CALL(*send_algorithm_, OnPacketAcked(_, _)).Times(4);
QuicAckFrame frame1 = InitAckFrame(5, 0);
NackPacket(3, &frame1);
EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_));
ProcessAckPacket(&frame1);
EXPECT_CALL(*send_algorithm_, UpdateRtt(_));
EXPECT_CALL(*send_algorithm_, OnPacketAcked(_, _)).Times(2);
QuicAckFrame frame2 = InitAckFrame(6, 0);
ProcessAckPacket(&frame2);
}
TEST_P(QuicConnectionTest, DontLatchUnackedPacket) {
EXPECT_CALL(*send_algorithm_, UpdateRtt(_));
EXPECT_CALL(*send_algorithm_, OnPacketAcked(_, _)).Times(1);
SendStreamDataToPeer(1, "foo", 0, !kFin, NULL);
ON_CALL(*send_algorithm_, OnPacketSent(_, _, _, _))
.WillByDefault(Return(false));
SendAckPacketToPeer();
EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_));
QuicAckFrame frame = InitAckFrame(1, 0);
ProcessAckPacket(&frame);
EXPECT_EQ(2u, outgoing_ack()->sent_info.least_unacked);
EXPECT_CALL(*send_algorithm_, UpdateRtt(_));
frame = InitAckFrame(2, 0);
ProcessAckPacket(&frame);
EXPECT_EQ(3u, outgoing_ack()->sent_info.least_unacked);
SendAckPacketToPeer();
EXPECT_EQ(3u, outgoing_ack()->sent_info.least_unacked);
EXPECT_EQ(3u, least_unacked());
EXPECT_CALL(*send_algorithm_, UpdateRtt(_));
frame = InitAckFrame(3, 0);
ProcessAckPacket(&frame);
ON_CALL(*send_algorithm_, OnPacketSent(_, _, _, _))
.WillByDefault(Return(true));
SendStreamDataToPeer(1, "bar", 3, false, NULL);
EXPECT_EQ(4u, outgoing_ack()->sent_info.least_unacked);
ON_CALL(*send_algorithm_, OnPacketSent(_, _, _, _))
.WillByDefault(Return(false));
SendAckPacketToPeer();
EXPECT_EQ(4u, least_unacked());
ON_CALL(*send_algorithm_, OnPacketSent(_, _, _, _))
.WillByDefault(Return(true));
SendStreamDataToPeer(1, "bar", 6, false, NULL);
SendStreamDataToPeer(1, "bar", 9, false, NULL);
EXPECT_CALL(*send_algorithm_, UpdateRtt(_));
EXPECT_CALL(*send_algorithm_, OnPacketAcked(_, _)).Times(2);
frame = InitAckFrame(7, 0);
NackPacket(5, &frame);
NackPacket(6, &frame);
ProcessAckPacket(&frame);
EXPECT_EQ(6u, outgoing_ack()->sent_info.least_unacked);
}
TEST_P(QuicConnectionTest, ReviveMissingPacketAfterFecPacket) {
if (version() < QUIC_VERSION_15) {
return;
}
EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_));
ProcessFecPacket(2, 1, true, !kEntropyFlag, NULL);
EXPECT_EQ(0u, QuicConnectionPeer::ReceivedEntropyHash(&connection_, 2));
}
TEST_P(QuicConnectionTest, ReviveMissingPacketAfterDataPacketThenFecPacket) {
if (version() < QUIC_VERSION_15) {
return;
}
EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_));
ProcessFecProtectedPacket(1, false, kEntropyFlag);
ProcessFecPacket(3, 1, true, !kEntropyFlag, NULL);
EXPECT_NE(0u, QuicConnectionPeer::ReceivedEntropyHash(&connection_, 2));
}
TEST_P(QuicConnectionTest, ReviveMissingPacketAfterDataPacketsThenFecPacket) {
if (version() < QUIC_VERSION_15) {
return;
}
EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_));
ProcessFecProtectedPacket(1, false, !kEntropyFlag);
ProcessFecProtectedPacket(3, false, !kEntropyFlag);
ProcessFecPacket(4, 1, true, kEntropyFlag, NULL);
EXPECT_EQ(0u, QuicConnectionPeer::ReceivedEntropyHash(&connection_, 2));
EXPECT_NE(0u, QuicConnectionPeer::ReceivedEntropyHash(&connection_, 4));
}
TEST_P(QuicConnectionTest, ReviveMissingPacketAfterDataPacket) {
if (version() < QUIC_VERSION_15) {
return;
}
EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_));
ProcessFecPacket(3, 1, false, !kEntropyFlag, NULL);
ProcessFecProtectedPacket(2, true, !kEntropyFlag);
EXPECT_EQ(0u, QuicConnectionPeer::ReceivedEntropyHash(&connection_, 2));
}
TEST_P(QuicConnectionTest, ReviveMissingPacketAfterDataPackets) {
if (version() < QUIC_VERSION_15) {
return;
}
EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_));
ProcessFecProtectedPacket(1, false, !kEntropyFlag);
ProcessFecPacket(6, 1, false, kEntropyFlag, NULL);
ProcessFecProtectedPacket(3, false, kEntropyFlag);
ProcessFecProtectedPacket(4, false, kEntropyFlag);
ProcessFecProtectedPacket(5, true, !kEntropyFlag);
EXPECT_EQ(0u, QuicConnectionPeer::ReceivedEntropyHash(&connection_, 2));
EXPECT_NE(0u, QuicConnectionPeer::ReceivedEntropyHash(&connection_, 3));
}
TEST_P(QuicConnectionTest, RTO) {
QuicTime default_retransmission_time = clock_.ApproximateNow().Add(
DefaultRetransmissionTime());
SendStreamDataToPeer(3, "foo", 0, !kFin, NULL);
EXPECT_EQ(1u, outgoing_ack()->sent_info.least_unacked);
EXPECT_EQ(1u, last_header()->packet_sequence_number);
EXPECT_EQ(default_retransmission_time,
connection_.GetRetransmissionAlarm()->deadline());
clock_.AdvanceTime(DefaultRetransmissionTime());
EXPECT_CALL(*send_algorithm_, OnRetransmissionTimeout(true));
EXPECT_CALL(*send_algorithm_, OnPacketSent(_, 2u, _, _));
connection_.GetRetransmissionAlarm()->Fire();
EXPECT_EQ(2u, last_header()->packet_sequence_number);
EXPECT_EQ(1u, outgoing_ack()->sent_info.least_unacked);
}
TEST_P(QuicConnectionTest, RTOWithSameEncryptionLevel) {
QuicTime default_retransmission_time = clock_.ApproximateNow().Add(
DefaultRetransmissionTime());
use_tagging_decrypter();
connection_.SetEncrypter(ENCRYPTION_NONE, new TaggingEncrypter(0x01));
SendStreamDataToPeer(3, "foo", 0, !kFin, NULL);
EXPECT_EQ(0x01010101u, final_bytes_of_last_packet());
connection_.SetEncrypter(ENCRYPTION_INITIAL, new TaggingEncrypter(0x02));
connection_.SetDefaultEncryptionLevel(ENCRYPTION_INITIAL);
SendStreamDataToPeer(3, "foo", 0, !kFin, NULL);
EXPECT_EQ(0x02020202u, final_bytes_of_last_packet());
EXPECT_EQ(default_retransmission_time,
connection_.GetRetransmissionAlarm()->deadline());
{
InSequence s;
EXPECT_CALL(*send_algorithm_, OnRetransmissionTimeout(true));
EXPECT_CALL(*send_algorithm_, OnPacketSent(_, 3, _, _));
EXPECT_CALL(*send_algorithm_, OnPacketSent(_, 4, _, _));
}
clock_.AdvanceTime(DefaultRetransmissionTime());
connection_.GetRetransmissionAlarm()->Fire();
EXPECT_EQ(0x01010101u, final_bytes_of_previous_packet());
EXPECT_EQ(0x02020202u, final_bytes_of_last_packet());
}
TEST_P(QuicConnectionTest, SendHandshakeMessages) {
use_tagging_decrypter();
connection_.SetEncrypter(ENCRYPTION_NONE, new TaggingEncrypter(0x01));
EXPECT_CALL(*send_algorithm_,
TimeUntilSend(_, _)).WillRepeatedly(
testing::Return(QuicTime::Delta::Zero()));
BlockOnNextWrite();
connection_.SendStreamDataWithString(1, "foo", 0, !kFin, NULL);
EXPECT_EQ(1u, connection_.NumQueuedPackets());
connection_.SetEncrypter(ENCRYPTION_INITIAL, new TaggingEncrypter(0x02));
connection_.SetDefaultEncryptionLevel(ENCRYPTION_INITIAL);
writer_->SetWritable();
EXPECT_CALL(visitor_, OnCanWrite());
connection_.OnCanWrite();
EXPECT_EQ(0u, connection_.NumQueuedPackets());
EXPECT_EQ(0x01010101u, final_bytes_of_last_packet());
}
TEST_P(QuicConnectionTest,
DropRetransmitsForNullEncryptedPacketAfterForwardSecure) {
use_tagging_decrypter();
connection_.SetEncrypter(ENCRYPTION_NONE, new TaggingEncrypter(0x01));
QuicPacketSequenceNumber sequence_number;
SendStreamDataToPeer(3, "foo", 0, !kFin, &sequence_number);
connection_.SetEncrypter(ENCRYPTION_FORWARD_SECURE,
new TaggingEncrypter(0x02));
connection_.SetDefaultEncryptionLevel(ENCRYPTION_FORWARD_SECURE);
EXPECT_CALL(*send_algorithm_, OnRetransmissionTimeout(true));
EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _)).Times(0);
QuicTime default_retransmission_time = clock_.ApproximateNow().Add(
DefaultRetransmissionTime());
EXPECT_EQ(default_retransmission_time,
connection_.GetRetransmissionAlarm()->deadline());
clock_.AdvanceTime(DefaultRetransmissionTime());
connection_.GetRetransmissionAlarm()->Fire();
}
TEST_P(QuicConnectionTest, RetransmitPacketsWithInitialEncryption) {
use_tagging_decrypter();
connection_.SetEncrypter(ENCRYPTION_NONE, new TaggingEncrypter(0x01));
connection_.SetDefaultEncryptionLevel(ENCRYPTION_NONE);
SendStreamDataToPeer(1, "foo", 0, !kFin, NULL);
connection_.SetEncrypter(ENCRYPTION_INITIAL, new TaggingEncrypter(0x02));
connection_.SetDefaultEncryptionLevel(ENCRYPTION_INITIAL);
SendStreamDataToPeer(2, "bar", 0, !kFin, NULL);
EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _)).Times(1);
EXPECT_CALL(*send_algorithm_, OnPacketAbandoned(_, _)).Times(1);
connection_.RetransmitUnackedPackets(INITIAL_ENCRYPTION_ONLY);
}
TEST_P(QuicConnectionTest, BufferNonDecryptablePackets) {
EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_));
use_tagging_decrypter();
const uint8 tag = 0x07;
framer_.SetEncrypter(ENCRYPTION_INITIAL, new TaggingEncrypter(tag));
ProcessDataPacketAtLevel(1, false, kEntropyFlag, ENCRYPTION_INITIAL);
connection_.SetDecrypter(new StrictTaggingDecrypter(tag));
connection_.SetDefaultEncryptionLevel(ENCRYPTION_INITIAL);
connection_.SetEncrypter(ENCRYPTION_INITIAL, new TaggingEncrypter(tag));
EXPECT_CALL(visitor_, OnStreamFrames(_)).Times(2).WillRepeatedly(
Return(true));
ProcessDataPacketAtLevel(2, false, kEntropyFlag, ENCRYPTION_INITIAL);
EXPECT_CALL(visitor_, OnStreamFrames(_)).WillOnce(Return(true));
ProcessDataPacketAtLevel(3, false, kEntropyFlag, ENCRYPTION_INITIAL);
}
TEST_P(QuicConnectionTest, TestRetransmitOrder) {
QuicByteCount first_packet_size;
EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _)).WillOnce(
DoAll(SaveArg<2>(&first_packet_size), Return(true)));
connection_.SendStreamDataWithString(3, "first_packet", 0, !kFin, NULL);
QuicByteCount second_packet_size;
EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _)).WillOnce(
DoAll(SaveArg<2>(&second_packet_size), Return(true)));
connection_.SendStreamDataWithString(3, "second_packet", 12, !kFin, NULL);
EXPECT_NE(first_packet_size, second_packet_size);
clock_.AdvanceTime(QuicTime::Delta::FromSeconds(10));
EXPECT_CALL(*send_algorithm_, OnRetransmissionTimeout(true));
{
InSequence s;
EXPECT_CALL(*send_algorithm_,
OnPacketSent(_, _, first_packet_size, _));
EXPECT_CALL(*send_algorithm_,
OnPacketSent(_, _, second_packet_size, _));
}
connection_.GetRetransmissionAlarm()->Fire();
clock_.AdvanceTime(QuicTime::Delta::FromSeconds(20));
EXPECT_CALL(*send_algorithm_, OnRetransmissionTimeout(true));
{
InSequence s;
EXPECT_CALL(*send_algorithm_,
OnPacketSent(_, _, first_packet_size, _));
EXPECT_CALL(*send_algorithm_,
OnPacketSent(_, _, second_packet_size, _));
}
connection_.GetRetransmissionAlarm()->Fire();
}
TEST_P(QuicConnectionTest, RetransmissionCountCalculation) {
EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_));
QuicPacketSequenceNumber original_sequence_number;
EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _))
.WillOnce(DoAll(SaveArg<1>(&original_sequence_number), Return(true)));
connection_.SendStreamDataWithString(3, "foo", 0, !kFin, NULL);
EXPECT_TRUE(QuicConnectionPeer::IsSavedForRetransmission(
&connection_, original_sequence_number));
EXPECT_FALSE(QuicConnectionPeer::IsRetransmission(
&connection_, original_sequence_number));
clock_.AdvanceTime(QuicTime::Delta::FromSeconds(10));
EXPECT_CALL(*send_algorithm_, OnRetransmissionTimeout(true));
QuicPacketSequenceNumber rto_sequence_number;
EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _))
.WillOnce(DoAll(SaveArg<1>(&rto_sequence_number), Return(true)));
connection_.GetRetransmissionAlarm()->Fire();
EXPECT_FALSE(QuicConnectionPeer::IsSavedForRetransmission(
&connection_, original_sequence_number));
ASSERT_TRUE(QuicConnectionPeer::IsSavedForRetransmission(
&connection_, rto_sequence_number));
EXPECT_TRUE(QuicConnectionPeer::IsRetransmission(
&connection_, rto_sequence_number));
SequenceNumberSet lost_packets;
lost_packets.insert(rto_sequence_number);
EXPECT_CALL(*loss_algorithm_, DetectLostPackets(_, _, _, _))
.WillOnce(Return(lost_packets));
EXPECT_CALL(*send_algorithm_, UpdateRtt(_)).Times(1);
EXPECT_CALL(*send_algorithm_, OnPacketLost(rto_sequence_number, _)).Times(1);
EXPECT_CALL(*send_algorithm_,
OnPacketAbandoned(rto_sequence_number, _)).Times(1);
QuicPacketSequenceNumber nack_sequence_number = 0;
EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _))
.Times(AnyNumber());
EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _))
.WillOnce(DoAll(SaveArg<1>(&nack_sequence_number), Return(true)));
QuicAckFrame ack = InitAckFrame(rto_sequence_number, 0);
NackPacket(original_sequence_number, &ack);
NackPacket(rto_sequence_number, &ack);
ProcessAckPacket(&ack);
ASSERT_NE(0u, nack_sequence_number);
EXPECT_FALSE(QuicConnectionPeer::IsSavedForRetransmission(
&connection_, rto_sequence_number));
ASSERT_TRUE(QuicConnectionPeer::IsSavedForRetransmission(
&connection_, nack_sequence_number));
EXPECT_TRUE(QuicConnectionPeer::IsRetransmission(
&connection_, nack_sequence_number));
}
TEST_P(QuicConnectionTest, SetRTOAfterWritingToSocket) {
BlockOnNextWrite();
connection_.SendStreamDataWithString(1, "foo", 0, !kFin, NULL);
EXPECT_FALSE(connection_.GetRetransmissionAlarm()->IsSet());
writer_->SetWritable();
connection_.OnCanWrite();
EXPECT_TRUE(connection_.GetRetransmissionAlarm()->IsSet());
}
TEST_P(QuicConnectionTest, DelayRTOWithAckReceipt) {
EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_));
EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _))
.Times(2);
connection_.SendStreamDataWithString(2, "foo", 0, !kFin, NULL);
connection_.SendStreamDataWithString(3, "bar", 0, !kFin, NULL);
QuicAlarm* retransmission_alarm = connection_.GetRetransmissionAlarm();
EXPECT_TRUE(retransmission_alarm->IsSet());
EXPECT_EQ(clock_.Now().Add(DefaultRetransmissionTime()),
retransmission_alarm->deadline());
clock_.AdvanceTime(DefaultRetransmissionTime());
EXPECT_CALL(*send_algorithm_, UpdateRtt(_));
EXPECT_CALL(*send_algorithm_, OnPacketAcked(_, _)).Times(1);
QuicAckFrame ack = InitAckFrame(1, 0);
ProcessAckPacket(&ack);
EXPECT_TRUE(retransmission_alarm->IsSet());
EXPECT_GT(retransmission_alarm->deadline(), clock_.Now());
clock_.AdvanceTime(DefaultRetransmissionTime().Multiply(2));
EXPECT_TRUE(retransmission_alarm->IsSet());
EXPECT_LT(retransmission_alarm->deadline(), clock_.ApproximateNow());
EXPECT_CALL(*send_algorithm_, OnRetransmissionTimeout(true));
EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _));
connection_.GetRetransmissionAlarm()->Fire();
EXPECT_TRUE(retransmission_alarm->IsSet());
QuicTime next_rto_time = retransmission_alarm->deadline();
QuicTime expected_rto_time =
connection_.sent_packet_manager().GetRetransmissionTime();
EXPECT_EQ(next_rto_time, expected_rto_time);
}
TEST_P(QuicConnectionTest, TestQueued) {
EXPECT_EQ(0u, connection_.NumQueuedPackets());
BlockOnNextWrite();
connection_.SendStreamDataWithString(1, "foo", 0, !kFin, NULL);
EXPECT_EQ(1u, connection_.NumQueuedPackets());
writer_->SetWritable();
connection_.OnCanWrite();
EXPECT_EQ(0u, connection_.NumQueuedPackets());
}
TEST_P(QuicConnectionTest, CloseFecGroup) {
EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_));
ProcessFecProtectedPacket(3, false, !kEntropyFlag);
ASSERT_EQ(1u, connection_.NumFecGroups());
creator_.set_sequence_number(4);
if (version() > QUIC_VERSION_15) {
QuicStopWaitingFrame frame = InitStopWaitingFrame(5);
ProcessStopWaitingPacket(&frame);
} else {
QuicAckFrame frame = InitAckFrame(0, 5);
ProcessAckPacket(&frame);
}
ASSERT_EQ(0u, connection_.NumFecGroups());
}
TEST_P(QuicConnectionTest, NoQuicCongestionFeedbackFrame) {
SendAckPacketToPeer();
EXPECT_TRUE(last_feedback() == NULL);
}
TEST_P(QuicConnectionTest, WithQuicCongestionFeedbackFrame) {
QuicCongestionFeedbackFrame info;
info.type = kFixRate;
info.fix_rate.bitrate = QuicBandwidth::FromBytesPerSecond(123);
SetFeedback(&info);
SendAckPacketToPeer();
EXPECT_EQ(kFixRate, last_feedback()->type);
EXPECT_EQ(info.fix_rate.bitrate, last_feedback()->fix_rate.bitrate);
}
TEST_P(QuicConnectionTest, UpdateQuicCongestionFeedbackFrame) {
SendAckPacketToPeer();
EXPECT_CALL(*receive_algorithm_, RecordIncomingPacket(_, _, _));
EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_));
ProcessPacket(1);
}
TEST_P(QuicConnectionTest, DontUpdateQuicCongestionFeedbackFrameForRevived) {
if (version() < QUIC_VERSION_15) {
return;
}
EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_));
SendAckPacketToPeer();
EXPECT_CALL(*receive_algorithm_, RecordIncomingPacket(_, _, _));
ProcessFecPacket(2, 1, true, !kEntropyFlag, NULL);
}
TEST_P(QuicConnectionTest, InitialTimeout) {
EXPECT_TRUE(connection_.connected());
EXPECT_CALL(visitor_, OnConnectionClosed(QUIC_CONNECTION_TIMED_OUT, false));
EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _));
QuicTime default_timeout = clock_.ApproximateNow().Add(
QuicTime::Delta::FromSeconds(kDefaultInitialTimeoutSecs));
EXPECT_EQ(default_timeout, connection_.GetTimeoutAlarm()->deadline());
clock_.AdvanceTime(
QuicTime::Delta::FromSeconds(kDefaultInitialTimeoutSecs));
connection_.GetTimeoutAlarm()->Fire();
EXPECT_FALSE(connection_.GetTimeoutAlarm()->IsSet());
EXPECT_FALSE(connection_.connected());
EXPECT_FALSE(connection_.GetAckAlarm()->IsSet());
EXPECT_FALSE(connection_.GetResumeWritesAlarm()->IsSet());
EXPECT_FALSE(connection_.GetRetransmissionAlarm()->IsSet());
EXPECT_FALSE(connection_.GetSendAlarm()->IsSet());
EXPECT_FALSE(connection_.GetTimeoutAlarm()->IsSet());
}
TEST_P(QuicConnectionTest, TimeoutAfterSend) {
EXPECT_TRUE(connection_.connected());
QuicTime default_timeout = clock_.ApproximateNow().Add(
QuicTime::Delta::FromSeconds(kDefaultInitialTimeoutSecs));
clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(5));
SendAckPacketToPeer();
EXPECT_EQ(default_timeout, connection_.GetTimeoutAlarm()->deadline());
clock_.AdvanceTime(QuicTime::Delta::FromMicroseconds(
kDefaultInitialTimeoutSecs * 1000000 - 5000));
EXPECT_EQ(default_timeout, clock_.ApproximateNow());
connection_.GetTimeoutAlarm()->Fire();
EXPECT_TRUE(connection_.GetTimeoutAlarm()->IsSet());
EXPECT_TRUE(connection_.connected());
EXPECT_EQ(default_timeout.Add(QuicTime::Delta::FromMilliseconds(5)),
connection_.GetTimeoutAlarm()->deadline());
EXPECT_CALL(visitor_, OnConnectionClosed(QUIC_CONNECTION_TIMED_OUT, false));
EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _));
clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(5));
EXPECT_EQ(default_timeout.Add(QuicTime::Delta::FromMilliseconds(5)),
clock_.ApproximateNow());
connection_.GetTimeoutAlarm()->Fire();
EXPECT_FALSE(connection_.GetTimeoutAlarm()->IsSet());
EXPECT_FALSE(connection_.connected());
}
TEST_P(QuicConnectionTest, SendScheduler) {
QuicPacket* packet = ConstructDataPacket(1, 0, !kEntropyFlag);
EXPECT_CALL(*send_algorithm_,
TimeUntilSend(_, _)).WillOnce(
testing::Return(QuicTime::Delta::Zero()));
EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _));
connection_.SendPacket(
ENCRYPTION_NONE, 1, packet, kTestEntropyHash, HAS_RETRANSMITTABLE_DATA);
EXPECT_EQ(0u, connection_.NumQueuedPackets());
}
TEST_P(QuicConnectionTest, SendSchedulerDelay) {
QuicPacket* packet = ConstructDataPacket(1, 0, !kEntropyFlag);
EXPECT_CALL(*send_algorithm_,
TimeUntilSend(_, _)).WillOnce(
testing::Return(QuicTime::Delta::FromMicroseconds(1)));
EXPECT_CALL(*send_algorithm_, OnPacketSent(_, 1, _, _)).Times(0);
connection_.SendPacket(
ENCRYPTION_NONE, 1, packet, kTestEntropyHash, HAS_RETRANSMITTABLE_DATA);
EXPECT_EQ(1u, connection_.NumQueuedPackets());
}
TEST_P(QuicConnectionTest, SendSchedulerEAGAIN) {
QuicPacket* packet = ConstructDataPacket(1, 0, !kEntropyFlag);
BlockOnNextWrite();
EXPECT_CALL(*send_algorithm_,
TimeUntilSend(_, _)).WillOnce(
testing::Return(QuicTime::Delta::Zero()));
EXPECT_CALL(*send_algorithm_, OnPacketSent(_, 1, _, _)).Times(0);
connection_.SendPacket(
ENCRYPTION_NONE, 1, packet, kTestEntropyHash, HAS_RETRANSMITTABLE_DATA);
EXPECT_EQ(1u, connection_.NumQueuedPackets());
}
TEST_P(QuicConnectionTest, SendSchedulerDelayThenSend) {
QuicPacket* packet = ConstructDataPacket(1, 0, !kEntropyFlag);
EXPECT_CALL(*send_algorithm_,
TimeUntilSend(_, _)).WillOnce(
testing::Return(QuicTime::Delta::FromMicroseconds(1)));
connection_.SendPacket(
ENCRYPTION_NONE, 1, packet, kTestEntropyHash, HAS_RETRANSMITTABLE_DATA);
EXPECT_EQ(1u, connection_.NumQueuedPackets());
EXPECT_CALL(*send_algorithm_,
TimeUntilSend(_, _)).WillRepeatedly(
testing::Return(QuicTime::Delta::Zero()));
clock_.AdvanceTime(QuicTime::Delta::FromMicroseconds(1));
EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _));
connection_.GetSendAlarm()->Fire();
EXPECT_EQ(0u, connection_.NumQueuedPackets());
}
TEST_P(QuicConnectionTest, SendSchedulerDelayThenRetransmit) {
EXPECT_CALL(*send_algorithm_, TimeUntilSend(_, _))
.WillRepeatedly(testing::Return(QuicTime::Delta::Zero()));
EXPECT_CALL(*send_algorithm_, OnPacketSent(_, 1, _, _));
connection_.SendStreamDataWithString(3, "foo", 0, !kFin, NULL);
EXPECT_EQ(0u, connection_.NumQueuedPackets());
clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(501));
EXPECT_CALL(*send_algorithm_, OnRetransmissionTimeout(true));
EXPECT_CALL(*send_algorithm_,
TimeUntilSend(_, _)).WillOnce(
testing::Return(QuicTime::Delta::FromMicroseconds(1)));
connection_.GetRetransmissionAlarm()->Fire();
EXPECT_EQ(0u, connection_.NumQueuedPackets());
EXPECT_CALL(*send_algorithm_,
TimeUntilSend(_, _)).Times(3).
WillRepeatedly(testing::Return(QuicTime::Delta::Zero()));
EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _));
clock_.AdvanceTime(QuicTime::Delta::FromMicroseconds(1));
connection_.GetSendAlarm()->Fire();
EXPECT_EQ(0u, connection_.NumQueuedPackets());
}
TEST_P(QuicConnectionTest, SendSchedulerDelayAndQueue) {
QuicPacket* packet = ConstructDataPacket(1, 0, !kEntropyFlag);
EXPECT_CALL(*send_algorithm_,
TimeUntilSend(_, _)).WillOnce(
testing::Return(QuicTime::Delta::FromMicroseconds(1)));
connection_.SendPacket(
ENCRYPTION_NONE, 1, packet, kTestEntropyHash, HAS_RETRANSMITTABLE_DATA);
EXPECT_EQ(1u, connection_.NumQueuedPackets());
packet = ConstructDataPacket(2, 0, !kEntropyFlag);
connection_.SendPacket(
ENCRYPTION_NONE, 2, packet, kTestEntropyHash, HAS_RETRANSMITTABLE_DATA);
EXPECT_EQ(2u, connection_.NumQueuedPackets());
}
TEST_P(QuicConnectionTest, SendSchedulerDelayThenAckAndSend) {
EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_));
QuicPacket* packet = ConstructDataPacket(1, 0, !kEntropyFlag);
EXPECT_CALL(*send_algorithm_,
TimeUntilSend(_, _)).WillOnce(
testing::Return(QuicTime::Delta::FromMicroseconds(10)));
connection_.SendPacket(
ENCRYPTION_NONE, 1, packet, kTestEntropyHash, HAS_RETRANSMITTABLE_DATA);
EXPECT_EQ(1u, connection_.NumQueuedPackets());
QuicAckFrame frame = InitAckFrame(0, 1);
EXPECT_CALL(*send_algorithm_,
TimeUntilSend(_, _)).WillRepeatedly(
testing::Return(QuicTime::Delta::Zero()));
EXPECT_CALL(*send_algorithm_,
OnPacketSent(_, _, _, _));
ProcessAckPacket(&frame);
EXPECT_EQ(0u, connection_.NumQueuedPackets());
EXPECT_FALSE(connection_.GetSendAlarm()->IsSet());
}
TEST_P(QuicConnectionTest, SendSchedulerDelayThenAckAndHold) {
EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_));
QuicPacket* packet = ConstructDataPacket(1, 0, !kEntropyFlag);
EXPECT_CALL(*send_algorithm_,
TimeUntilSend(_, _)).WillOnce(
testing::Return(QuicTime::Delta::FromMicroseconds(10)));
connection_.SendPacket(
ENCRYPTION_NONE, 1, packet, kTestEntropyHash, HAS_RETRANSMITTABLE_DATA);
EXPECT_EQ(1u, connection_.NumQueuedPackets());
QuicAckFrame frame = InitAckFrame(0, 1);
EXPECT_CALL(*send_algorithm_,
TimeUntilSend(_, _)).WillOnce(
testing::Return(QuicTime::Delta::FromMicroseconds(1)));
ProcessAckPacket(&frame);
EXPECT_EQ(1u, connection_.NumQueuedPackets());
}
TEST_P(QuicConnectionTest, SendSchedulerDelayThenOnCanWrite) {
QuicPacket* packet = ConstructDataPacket(1, 0, !kEntropyFlag);
EXPECT_CALL(*send_algorithm_,
TimeUntilSend(_, _)).WillOnce(
testing::Return(QuicTime::Delta::FromMicroseconds(10)));
connection_.SendPacket(
ENCRYPTION_NONE, 1, packet, kTestEntropyHash, HAS_RETRANSMITTABLE_DATA);
EXPECT_EQ(1u, connection_.NumQueuedPackets());
connection_.OnCanWrite();
EXPECT_EQ(0u, connection_.NumQueuedPackets());
}
TEST_P(QuicConnectionTest, TestQueueLimitsOnSendStreamData) {
size_t payload_length;
connection_.options()->max_packet_length =
GetPacketLengthForOneStream(
connection_.version(), kIncludeVersion, PACKET_1BYTE_SEQUENCE_NUMBER,
NOT_IN_FEC_GROUP, &payload_length);
EXPECT_CALL(*send_algorithm_,
TimeUntilSend(_, _)).WillOnce(
testing::Return(QuicTime::Delta::FromMicroseconds(10)));
const string payload(payload_length, 'a');
EXPECT_EQ(0u,
connection_.SendStreamDataWithString(3, payload, 0,
!kFin, NULL).bytes_consumed);
EXPECT_EQ(0u, connection_.NumQueuedPackets());
}
TEST_P(QuicConnectionTest, LoopThroughSendingPackets) {
size_t payload_length;
connection_.options()->max_packet_length =
GetPacketLengthForOneStream(
connection_.version(), kIncludeVersion, PACKET_1BYTE_SEQUENCE_NUMBER,
NOT_IN_FEC_GROUP, &payload_length);
EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _)).Times(7);
const string payload(payload_length * 7 - 12, 'a');
EXPECT_EQ(payload.size(),
connection_.SendStreamDataWithString(1, payload, 0,
!kFin, NULL).bytes_consumed);
}
TEST_P(QuicConnectionTest, SendDelayedAck) {
QuicTime ack_time = clock_.ApproximateNow().Add(DefaultDelayedAckTime());
EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_));
EXPECT_FALSE(connection_.GetAckAlarm()->IsSet());
frame1_.stream_id = 3;
ProcessPacket(1);
EXPECT_TRUE(connection_.GetAckAlarm()->IsSet());
EXPECT_EQ(ack_time, connection_.GetAckAlarm()->deadline());
connection_.GetAckAlarm()->Fire();
if (version() > QUIC_VERSION_15) {
EXPECT_EQ(2u, writer_->frame_count());
EXPECT_TRUE(writer_->stop_waiting());
} else {
EXPECT_EQ(1u, writer_->frame_count());
}
EXPECT_TRUE(writer_->ack());
EXPECT_FALSE(connection_.GetAckAlarm()->IsSet());
}
TEST_P(QuicConnectionTest, SendEarlyDelayedAckForCrypto) {
QuicTime ack_time = clock_.ApproximateNow();
EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_));
EXPECT_FALSE(connection_.GetAckAlarm()->IsSet());
ProcessPacket(1);
EXPECT_TRUE(connection_.GetAckAlarm()->IsSet());
EXPECT_EQ(ack_time, connection_.GetAckAlarm()->deadline());
connection_.GetAckAlarm()->Fire();
if (version() > QUIC_VERSION_15) {
EXPECT_EQ(2u, writer_->frame_count());
EXPECT_TRUE(writer_->stop_waiting());
} else {
EXPECT_EQ(1u, writer_->frame_count());
}
EXPECT_TRUE(writer_->ack());
EXPECT_FALSE(connection_.GetAckAlarm()->IsSet());
}
TEST_P(QuicConnectionTest, SendDelayedAckOnSecondPacket) {
EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_));
ProcessPacket(1);
ProcessPacket(2);
if (version() > QUIC_VERSION_15) {
EXPECT_EQ(2u, writer_->frame_count());
EXPECT_TRUE(writer_->stop_waiting());
} else {
EXPECT_EQ(1u, writer_->frame_count());
}
EXPECT_TRUE(writer_->ack());
EXPECT_FALSE(connection_.GetAckAlarm()->IsSet());
}
TEST_P(QuicConnectionTest, NoAckOnOldNacks) {
EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_));
ProcessPacket(2);
size_t frames_per_ack = version() > QUIC_VERSION_15 ? 2 : 1;
EXPECT_EQ(frames_per_ack, writer_->frame_count());
EXPECT_TRUE(writer_->ack());
writer_->Reset();
ProcessPacket(3);
EXPECT_EQ(frames_per_ack, writer_->frame_count());
EXPECT_TRUE(writer_->ack());
writer_->Reset();
ProcessPacket(4);
EXPECT_EQ(frames_per_ack, writer_->frame_count());
EXPECT_TRUE(writer_->ack());
writer_->Reset();
ProcessPacket(5);
EXPECT_EQ(frames_per_ack, writer_->frame_count());
EXPECT_TRUE(writer_->ack());
writer_->Reset();
ProcessPacket(6);
EXPECT_EQ(0u, writer_->frame_count());
EXPECT_TRUE(connection_.GetAckAlarm()->IsSet());
}
TEST_P(QuicConnectionTest, SendDelayedAckOnOutgoingPacket) {
EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_));
ProcessPacket(1);
connection_.SendStreamDataWithString(kStreamId3, "foo", 0, !kFin, NULL);
if (version() > QUIC_VERSION_15) {
EXPECT_EQ(3u, writer_->frame_count());
EXPECT_TRUE(writer_->stop_waiting());
} else {
EXPECT_EQ(2u, writer_->frame_count());
}
EXPECT_TRUE(writer_->ack());
EXPECT_FALSE(connection_.GetAckAlarm()->IsSet());
}
TEST_P(QuicConnectionTest, SendDelayedAckOnOutgoingCryptoPacket) {
EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_));
ProcessPacket(1);
connection_.SendStreamDataWithString(kCryptoStreamId, "foo", 0, !kFin, NULL);
EXPECT_EQ(version() <= QUIC_VERSION_15 ? 2u : 3u, writer_->frame_count());
EXPECT_TRUE(writer_->ack());
EXPECT_FALSE(connection_.GetAckAlarm()->IsSet());
}
TEST_P(QuicConnectionTest, BundleAckWithDataOnIncomingAck) {
EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_));
connection_.SendStreamDataWithString(kStreamId3, "foo", 0, !kFin, NULL);
connection_.SendStreamDataWithString(kStreamId3, "foo", 3, !kFin, NULL);
QuicAckFrame ack = InitAckFrame(2, 0);
NackPacket(1, &ack);
SequenceNumberSet lost_packets;
lost_packets.insert(1);
EXPECT_CALL(*loss_algorithm_, DetectLostPackets(_, _, _, _))
.WillOnce(Return(lost_packets));
EXPECT_CALL(*send_algorithm_, UpdateRtt(_));
EXPECT_CALL(*send_algorithm_, OnPacketAcked(2, _)).Times(1);
EXPECT_CALL(*send_algorithm_, OnPacketLost(1, _)).Times(1);
EXPECT_CALL(*send_algorithm_, OnPacketAbandoned(1, _)).Times(1);
ProcessAckPacket(&ack);
EXPECT_EQ(1u, writer_->frame_count());
EXPECT_EQ(1u, writer_->stream_frames()->size());
writer_->Reset();
ack = InitAckFrame(3, 0);
NackPacket(1, &ack);
EXPECT_CALL(*loss_algorithm_, DetectLostPackets(_, _, _, _))
.WillOnce(Return(SequenceNumberSet()));
EXPECT_CALL(*send_algorithm_, UpdateRtt(_));
EXPECT_CALL(*send_algorithm_, OnPacketAcked(3, _)).Times(1);
ProcessAckPacket(&ack);
EXPECT_EQ(0u, writer_->frame_count());
EXPECT_FALSE(connection_.GetAckAlarm()->IsSet());
writer_->Reset();
ack = InitAckFrame(3, 0);
NackPacket(1, &ack);
EXPECT_CALL(*loss_algorithm_, DetectLostPackets(_, _, _, _))
.WillOnce(Return(SequenceNumberSet()));
EXPECT_CALL(visitor_, OnCanWrite()).WillOnce(
IgnoreResult(InvokeWithoutArgs(
&connection_,
&TestConnection::EnsureWritableAndSendStreamData5)));
ProcessAckPacket(&ack);
if (version() > QUIC_VERSION_15) {
EXPECT_EQ(3u, writer_->frame_count());
EXPECT_TRUE(writer_->stop_waiting());
} else {
EXPECT_EQ(2u, writer_->frame_count());
}
EXPECT_TRUE(writer_->ack());
EXPECT_EQ(1u, writer_->stream_frames()->size());
EXPECT_FALSE(connection_.GetAckAlarm()->IsSet());
}
TEST_P(QuicConnectionTest, NoAckForClose) {
EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_));
ProcessPacket(1);
EXPECT_CALL(*send_algorithm_, OnPacketAcked(_, _)).Times(0);
EXPECT_CALL(visitor_, OnConnectionClosed(QUIC_PEER_GOING_AWAY, true));
EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _)).Times(0);
ProcessClosePacket(2, 0);
}
TEST_P(QuicConnectionTest, SendWhenDisconnected) {
EXPECT_TRUE(connection_.connected());
EXPECT_CALL(visitor_, OnConnectionClosed(QUIC_PEER_GOING_AWAY, false));
connection_.CloseConnection(QUIC_PEER_GOING_AWAY, false);
EXPECT_FALSE(connection_.connected());
QuicPacket* packet = ConstructDataPacket(1, 0, !kEntropyFlag);
EXPECT_CALL(*send_algorithm_, OnPacketSent(_, 1, _, _)).Times(0);
connection_.SendPacket(
ENCRYPTION_NONE, 1, packet, kTestEntropyHash, HAS_RETRANSMITTABLE_DATA);
}
TEST_P(QuicConnectionTest, PublicReset) {
QuicPublicResetPacket header;
header.public_header.connection_id = connection_id_;
header.public_header.reset_flag = true;
header.public_header.version_flag = false;
header.rejected_sequence_number = 10101;
scoped_ptr<QuicEncryptedPacket> packet(
framer_.BuildPublicResetPacket(header));
EXPECT_CALL(visitor_, OnConnectionClosed(QUIC_PUBLIC_RESET, true));
connection_.ProcessUdpPacket(IPEndPoint(), IPEndPoint(), *packet);
}
TEST_P(QuicConnectionTest, GoAway) {
EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_));
QuicGoAwayFrame goaway;
goaway.last_good_stream_id = 1;
goaway.error_code = QUIC_PEER_GOING_AWAY;
goaway.reason_phrase = "Going away.";
EXPECT_CALL(visitor_, OnGoAway(_));
ProcessGoAwayPacket(&goaway);
}
TEST_P(QuicConnectionTest, WindowUpdate) {
if (version() == QUIC_VERSION_13) {
return;
}
EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_));
QuicWindowUpdateFrame window_update;
window_update.stream_id = 3;
window_update.byte_offset = 1234;
EXPECT_CALL(visitor_, OnWindowUpdateFrames(_));
ProcessFramePacket(QuicFrame(&window_update));
}
TEST_P(QuicConnectionTest, Blocked) {
if (version() == QUIC_VERSION_13) {
return;
}
EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_));
QuicBlockedFrame blocked;
blocked.stream_id = 3;
EXPECT_CALL(visitor_, OnBlockedFrames(_));
ProcessFramePacket(QuicFrame(&blocked));
}
TEST_P(QuicConnectionTest, InvalidPacket) {
EXPECT_CALL(visitor_,
OnConnectionClosed(QUIC_INVALID_PACKET_HEADER, false));
QuicEncryptedPacket encrypted(NULL, 0);
connection_.ProcessUdpPacket(IPEndPoint(), IPEndPoint(), encrypted);
ASSERT_TRUE(last_close() != NULL);
EXPECT_EQ("Unable to read public flags.", last_close()->error_details);
}
TEST_P(QuicConnectionTest, MissingPacketsBeforeLeastUnacked) {
creator_.set_sequence_number(3);
EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_));
if (version() > QUIC_VERSION_15) {
QuicStopWaitingFrame frame = InitStopWaitingFrame(4);
ProcessStopWaitingPacket(&frame);
} else {
QuicAckFrame ack = InitAckFrame(0, 4);
ProcessAckPacket(&ack);
}
EXPECT_TRUE(outgoing_ack()->received_info.missing_packets.empty());
}
TEST_P(QuicConnectionTest, ReceivedEntropyHashCalculation) {
EXPECT_CALL(visitor_, OnStreamFrames(_)).WillRepeatedly(Return(true));
EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_));
ProcessDataPacket(1, 1, kEntropyFlag);
ProcessDataPacket(4, 1, kEntropyFlag);
ProcessDataPacket(3, 1, !kEntropyFlag);
ProcessDataPacket(7, 1, kEntropyFlag);
EXPECT_EQ(146u, outgoing_ack()->received_info.entropy_hash);
}
TEST_P(QuicConnectionTest, ReceivedEntropyHashCalculationHalfFEC) {
if (version() < QUIC_VERSION_15) {
return;
}
EXPECT_CALL(visitor_, OnStreamFrames(_)).WillRepeatedly(Return(true));
EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_));
ProcessDataPacket(1, 1, kEntropyFlag);
ProcessFecPacket(4, 1, false, kEntropyFlag, NULL);
ProcessDataPacket(3, 3, !kEntropyFlag);
ProcessFecPacket(7, 3, false, kEntropyFlag, NULL);
EXPECT_EQ(146u, outgoing_ack()->received_info.entropy_hash);
}
TEST_P(QuicConnectionTest, UpdateEntropyForReceivedPackets) {
EXPECT_CALL(visitor_, OnStreamFrames(_)).WillRepeatedly(Return(true));
EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_));
ProcessDataPacket(1, 1, kEntropyFlag);
ProcessDataPacket(5, 1, kEntropyFlag);
ProcessDataPacket(4, 1, !kEntropyFlag);
EXPECT_EQ(34u, outgoing_ack()->received_info.entropy_hash);
creator_.set_sequence_number(5);
QuicPacketEntropyHash six_packet_entropy_hash = 0;
QuicPacketEntropyHash kRandomEntropyHash = 129u;
if (version() > QUIC_VERSION_15) {
QuicStopWaitingFrame frame = InitStopWaitingFrame(4);
frame.entropy_hash = kRandomEntropyHash;
if (ProcessStopWaitingPacket(&frame)) {
six_packet_entropy_hash = 1 << 6;
}
} else {
QuicAckFrame ack = InitAckFrame(0, 4);
ack.sent_info.entropy_hash = kRandomEntropyHash;
if (ProcessAckPacket(&ack)) {
six_packet_entropy_hash = 1 << 6;
}
}
EXPECT_EQ((kRandomEntropyHash + (1 << 5) + six_packet_entropy_hash),
outgoing_ack()->received_info.entropy_hash);
}
TEST_P(QuicConnectionTest, UpdateEntropyHashUptoCurrentPacket) {
EXPECT_CALL(visitor_, OnStreamFrames(_)).WillRepeatedly(Return(true));
EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_));
ProcessDataPacket(1, 1, kEntropyFlag);
ProcessDataPacket(5, 1, !kEntropyFlag);
ProcessDataPacket(22, 1, kEntropyFlag);
EXPECT_EQ(66u, outgoing_ack()->received_info.entropy_hash);
creator_.set_sequence_number(22);
QuicPacketEntropyHash kRandomEntropyHash = 85u;
QuicPacketEntropyHash ack_entropy_hash;
if (version() > QUIC_VERSION_15) {
QuicStopWaitingFrame frame = InitStopWaitingFrame(23);
frame.entropy_hash = kRandomEntropyHash;
ack_entropy_hash = ProcessStopWaitingPacket(&frame);
} else {
QuicAckFrame ack = InitAckFrame(0, 23);
ack.sent_info.entropy_hash = kRandomEntropyHash;
ack_entropy_hash = ProcessAckPacket(&ack);
}
EXPECT_EQ((kRandomEntropyHash + ack_entropy_hash),
outgoing_ack()->received_info.entropy_hash);
ProcessDataPacket(25, 1, kEntropyFlag);
EXPECT_EQ((kRandomEntropyHash + ack_entropy_hash + (1 << (25 % 8))),
outgoing_ack()->received_info.entropy_hash);
}
TEST_P(QuicConnectionTest, EntropyCalculationForTruncatedAck) {
EXPECT_CALL(visitor_, OnStreamFrames(_)).WillRepeatedly(Return(true));
EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_));
QuicPacketEntropyHash entropy[51];
entropy[0] = 0;
for (int i = 1; i < 51; ++i) {
bool should_send = i % 10 != 0;
bool entropy_flag = (i & (i - 1)) != 0;
if (!should_send) {
entropy[i] = entropy[i - 1];
continue;
}
if (entropy_flag) {
entropy[i] = entropy[i - 1] ^ (1 << (i % 8));
} else {
entropy[i] = entropy[i - 1];
}
ProcessDataPacket(i, 1, entropy_flag);
}
for (int i = 1; i < 50; ++i) {
EXPECT_EQ(entropy[i], QuicConnectionPeer::ReceivedEntropyHash(
&connection_, i));
}
}
TEST_P(QuicConnectionTest, CheckSentEntropyHash) {
creator_.set_sequence_number(1);
SequenceNumberSet missing_packets;
QuicPacketEntropyHash entropy_hash = 0;
QuicPacketSequenceNumber max_sequence_number = 51;
for (QuicPacketSequenceNumber i = 1; i <= max_sequence_number; ++i) {
bool is_missing = i % 10 != 0;
bool entropy_flag = (i & (i - 1)) != 0;
QuicPacketEntropyHash packet_entropy_hash = 0;
if (entropy_flag) {
packet_entropy_hash = 1 << (i % 8);
}
QuicPacket* packet = ConstructDataPacket(i, 0, entropy_flag);
connection_.SendPacket(
ENCRYPTION_NONE, i, packet, packet_entropy_hash,
HAS_RETRANSMITTABLE_DATA);
if (is_missing) {
missing_packets.insert(i);
continue;
}
entropy_hash ^= packet_entropy_hash;
}
EXPECT_TRUE(QuicConnectionPeer::IsValidEntropy(
&connection_, max_sequence_number, missing_packets, entropy_hash))
<< "";
}
TEST_P(QuicConnectionTest, ServerSendsVersionNegotiationPacket) {
connection_.SetSupportedVersions(QuicSupportedVersions());
framer_.set_version_for_tests(QUIC_VERSION_UNSUPPORTED);
QuicPacketHeader header;
header.public_header.connection_id = connection_id_;
header.public_header.reset_flag = false;
header.public_header.version_flag = true;
header.entropy_flag = false;
header.fec_flag = false;
header.packet_sequence_number = 12;
header.fec_group = 0;
QuicFrames frames;
QuicFrame frame(&frame1_);
frames.push_back(frame);
scoped_ptr<QuicPacket> packet(
framer_.BuildUnsizedDataPacket(header, frames).packet);
scoped_ptr<QuicEncryptedPacket> encrypted(
framer_.EncryptPacket(ENCRYPTION_NONE, 12, *packet));
framer_.set_version(version());
connection_.set_is_server(true);
connection_.ProcessUdpPacket(IPEndPoint(), IPEndPoint(), *encrypted);
EXPECT_TRUE(writer_->version_negotiation_packet() != NULL);
size_t num_versions = arraysize(kSupportedQuicVersions);
ASSERT_EQ(num_versions,
writer_->version_negotiation_packet()->versions.size());
for (size_t i = 0; i < num_versions; ++i) {
EXPECT_EQ(kSupportedQuicVersions[i],
writer_->version_negotiation_packet()->versions[i]);
}
}
TEST_P(QuicConnectionTest, ServerSendsVersionNegotiationPacketSocketBlocked) {
connection_.SetSupportedVersions(QuicSupportedVersions());
framer_.set_version_for_tests(QUIC_VERSION_UNSUPPORTED);
QuicPacketHeader header;
header.public_header.connection_id = connection_id_;
header.public_header.reset_flag = false;
header.public_header.version_flag = true;
header.entropy_flag = false;
header.fec_flag = false;
header.packet_sequence_number = 12;
header.fec_group = 0;
QuicFrames frames;
QuicFrame frame(&frame1_);
frames.push_back(frame);
scoped_ptr<QuicPacket> packet(
framer_.BuildUnsizedDataPacket(header, frames).packet);
scoped_ptr<QuicEncryptedPacket> encrypted(
framer_.EncryptPacket(ENCRYPTION_NONE, 12, *packet));
framer_.set_version(version());
connection_.set_is_server(true);
BlockOnNextWrite();
connection_.ProcessUdpPacket(IPEndPoint(), IPEndPoint(), *encrypted);
EXPECT_EQ(0u, writer_->last_packet_size());
EXPECT_TRUE(connection_.HasQueuedData());
writer_->SetWritable();
connection_.OnCanWrite();
EXPECT_TRUE(writer_->version_negotiation_packet() != NULL);
size_t num_versions = arraysize(kSupportedQuicVersions);
ASSERT_EQ(num_versions,
writer_->version_negotiation_packet()->versions.size());
for (size_t i = 0; i < num_versions; ++i) {
EXPECT_EQ(kSupportedQuicVersions[i],
writer_->version_negotiation_packet()->versions[i]);
}
}
TEST_P(QuicConnectionTest,
ServerSendsVersionNegotiationPacketSocketBlockedDataBuffered) {
connection_.SetSupportedVersions(QuicSupportedVersions());
framer_.set_version_for_tests(QUIC_VERSION_UNSUPPORTED);
QuicPacketHeader header;
header.public_header.connection_id = connection_id_;
header.public_header.reset_flag = false;
header.public_header.version_flag = true;
header.entropy_flag = false;
header.fec_flag = false;
header.packet_sequence_number = 12;
header.fec_group = 0;
QuicFrames frames;
QuicFrame frame(&frame1_);
frames.push_back(frame);
scoped_ptr<QuicPacket> packet(
framer_.BuildUnsizedDataPacket(header, frames).packet);
scoped_ptr<QuicEncryptedPacket> encrypted(
framer_.EncryptPacket(ENCRYPTION_NONE, 12, *packet));
framer_.set_version(version());
connection_.set_is_server(true);
BlockOnNextWrite();
writer_->set_is_write_blocked_data_buffered(true);
connection_.ProcessUdpPacket(IPEndPoint(), IPEndPoint(), *encrypted);
EXPECT_EQ(0u, writer_->last_packet_size());
EXPECT_FALSE(connection_.HasQueuedData());
}
TEST_P(QuicConnectionTest, ClientHandlesVersionNegotiation) {
QuicConnectionPeer::GetFramer(&connection_)->set_version_for_tests(
QUIC_VERSION_UNSUPPORTED);
QuicPacketHeader header;
header.public_header.connection_id = connection_id_;
header.public_header.reset_flag = false;
header.public_header.version_flag = true;
header.entropy_flag = false;
header.fec_flag = false;
header.packet_sequence_number = 12;
header.fec_group = 0;
QuicVersionVector supported_versions;
for (size_t i = 0; i < arraysize(kSupportedQuicVersions); ++i) {
supported_versions.push_back(kSupportedQuicVersions[i]);
}
scoped_ptr<QuicEncryptedPacket> encrypted(
framer_.BuildVersionNegotiationPacket(
header.public_header, supported_versions));
connection_.ProcessUdpPacket(IPEndPoint(), IPEndPoint(), *encrypted);
header.public_header.version_flag = false;
QuicFrames frames;
QuicFrame frame(&frame1_);
frames.push_back(frame);
scoped_ptr<QuicPacket> packet(
framer_.BuildUnsizedDataPacket(header, frames).packet);
encrypted.reset(framer_.EncryptPacket(ENCRYPTION_NONE, 12, *packet));
EXPECT_CALL(visitor_, OnStreamFrames(_)).Times(1);
EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_));
connection_.ProcessUdpPacket(IPEndPoint(), IPEndPoint(), *encrypted);
ASSERT_FALSE(QuicPacketCreatorPeer::SendVersionInPacket(
QuicConnectionPeer::GetPacketCreator(&connection_)));
}
TEST_P(QuicConnectionTest, BadVersionNegotiation) {
QuicPacketHeader header;
header.public_header.connection_id = connection_id_;
header.public_header.reset_flag = false;
header.public_header.version_flag = true;
header.entropy_flag = false;
header.fec_flag = false;
header.packet_sequence_number = 12;
header.fec_group = 0;
QuicVersionVector supported_versions;
for (size_t i = 0; i < arraysize(kSupportedQuicVersions); ++i) {
supported_versions.push_back(kSupportedQuicVersions[i]);
}
EXPECT_CALL(visitor_,
OnConnectionClosed(QUIC_INVALID_VERSION_NEGOTIATION_PACKET,
false));
scoped_ptr<QuicEncryptedPacket> encrypted(
framer_.BuildVersionNegotiationPacket(
header.public_header, supported_versions));
connection_.ProcessUdpPacket(IPEndPoint(), IPEndPoint(), *encrypted);
}
TEST_P(QuicConnectionTest, CheckSendStats) {
EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _));
connection_.SendStreamDataWithString(3, "first", 0, !kFin, NULL);
size_t first_packet_size = last_sent_packet_size();
EXPECT_CALL(*send_algorithm_,
OnPacketSent(_, _, _, _));
connection_.SendStreamDataWithString(5, "second", 0, !kFin, NULL);
size_t second_packet_size = last_sent_packet_size();
EXPECT_CALL(*send_algorithm_, OnRetransmissionTimeout(true));
EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _)).Times(3);
EXPECT_CALL(*send_algorithm_, OnPacketAbandoned(_, _)).Times(1);
clock_.AdvanceTime(QuicTime::Delta::FromSeconds(10));
connection_.GetRetransmissionAlarm()->Fire();
QuicAckFrame nack_three = InitAckFrame(4, 0);
NackPacket(3, &nack_three);
NackPacket(1, &nack_three);
SequenceNumberSet lost_packets;
lost_packets.insert(1);
lost_packets.insert(3);
EXPECT_CALL(*loss_algorithm_, DetectLostPackets(_, _, _, _))
.WillOnce(Return(lost_packets));
EXPECT_CALL(*send_algorithm_, UpdateRtt(_));
EXPECT_CALL(*send_algorithm_, OnPacketAcked(4, _)).Times(1);
EXPECT_CALL(*send_algorithm_, OnPacketLost(_, _)).Times(2);
EXPECT_CALL(visitor_, OnCanWrite()).Times(2);
EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_));
ProcessAckPacket(&nack_three);
EXPECT_CALL(*send_algorithm_, BandwidthEstimate()).WillOnce(
Return(QuicBandwidth::Zero()));
const QuicConnectionStats& stats = connection_.GetStats();
EXPECT_EQ(3 * first_packet_size + 2 * second_packet_size - kQuicVersionSize,
stats.bytes_sent);
EXPECT_EQ(5u, stats.packets_sent);
EXPECT_EQ(2 * first_packet_size + second_packet_size - kQuicVersionSize,
stats.bytes_retransmitted);
EXPECT_EQ(3u, stats.packets_retransmitted);
EXPECT_EQ(1u, stats.rto_count);
}
TEST_P(QuicConnectionTest, CheckReceiveStats) {
if (version() < QUIC_VERSION_15) {
return;
}
EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_));
size_t received_bytes = 0;
received_bytes += ProcessFecProtectedPacket(1, false, !kEntropyFlag);
received_bytes += ProcessFecProtectedPacket(3, false, !kEntropyFlag);
received_bytes += ProcessDataPacket(3, 1, !kEntropyFlag);
received_bytes += ProcessFecPacket(4, 1, true, !kEntropyFlag, NULL);
EXPECT_CALL(*send_algorithm_, BandwidthEstimate()).WillOnce(
Return(QuicBandwidth::Zero()));
const QuicConnectionStats& stats = connection_.GetStats();
EXPECT_EQ(received_bytes, stats.bytes_received);
EXPECT_EQ(4u, stats.packets_received);
EXPECT_EQ(1u, stats.packets_revived);
EXPECT_EQ(1u, stats.packets_dropped);
}
TEST_P(QuicConnectionTest, TestFecGroupLimits) {
ASSERT_TRUE(QuicConnectionPeer::GetFecGroup(&connection_, 1) != NULL);
ASSERT_TRUE(QuicConnectionPeer::GetFecGroup(&connection_, 2) != NULL);
ASSERT_TRUE(QuicConnectionPeer::GetFecGroup(&connection_, 4) != NULL);
ASSERT_TRUE(QuicConnectionPeer::GetFecGroup(&connection_, 1) == NULL);
ASSERT_TRUE(QuicConnectionPeer::GetFecGroup(&connection_, 2) != NULL);
ASSERT_TRUE(QuicConnectionPeer::GetFecGroup(&connection_, 3) != NULL);
ASSERT_TRUE(QuicConnectionPeer::GetFecGroup(&connection_, 2) == NULL);
ASSERT_TRUE(QuicConnectionPeer::GetFecGroup(&connection_, 5) != NULL);
ASSERT_TRUE(QuicConnectionPeer::GetFecGroup(&connection_, 4) != NULL);
ASSERT_TRUE(QuicConnectionPeer::GetFecGroup(&connection_, 3) == NULL);
}
TEST_P(QuicConnectionTest, ProcessFramesIfPacketClosedConnection) {
header_.public_header.connection_id = connection_id_;
header_.packet_sequence_number = 1;
header_.public_header.reset_flag = false;
header_.public_header.version_flag = false;
header_.entropy_flag = false;
header_.fec_flag = false;
header_.fec_group = 0;
QuicConnectionCloseFrame qccf;
qccf.error_code = QUIC_PEER_GOING_AWAY;
QuicFrame close_frame(&qccf);
QuicFrame stream_frame(&frame1_);
QuicFrames frames;
frames.push_back(stream_frame);
frames.push_back(close_frame);
scoped_ptr<QuicPacket> packet(
framer_.BuildUnsizedDataPacket(header_, frames).packet);
EXPECT_TRUE(NULL != packet.get());
scoped_ptr<QuicEncryptedPacket> encrypted(framer_.EncryptPacket(
ENCRYPTION_NONE, 1, *packet));
EXPECT_CALL(visitor_, OnConnectionClosed(QUIC_PEER_GOING_AWAY, true));
EXPECT_CALL(visitor_, OnStreamFrames(_)).WillOnce(Return(true));
EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_));
connection_.ProcessUdpPacket(IPEndPoint(), IPEndPoint(), *encrypted);
}
TEST_P(QuicConnectionTest, DontProcessStreamFrameAndIgnoreCloseFrame) {
header_.public_header.connection_id = connection_id_;
header_.packet_sequence_number = 1;
header_.public_header.reset_flag = false;
header_.public_header.version_flag = false;
header_.entropy_flag = false;
header_.fec_flag = false;
header_.fec_group = 0;
QuicConnectionCloseFrame qccf;
qccf.error_code = QUIC_PEER_GOING_AWAY;
QuicFrame close_frame(&qccf);
QuicFrame stream_frame(&frame1_);
QuicFrames frames;
frames.push_back(stream_frame);
frames.push_back(close_frame);
scoped_ptr<QuicPacket> packet(
framer_.BuildUnsizedDataPacket(header_, frames).packet);
EXPECT_TRUE(NULL != packet.get());
scoped_ptr<QuicEncryptedPacket> encrypted(framer_.EncryptPacket(
ENCRYPTION_NONE, 1, *packet));
EXPECT_CALL(visitor_, OnStreamFrames(_)).WillOnce(Return(false));
EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_));
connection_.ProcessUdpPacket(IPEndPoint(), IPEndPoint(), *encrypted);
}
TEST_P(QuicConnectionTest, SelectMutualVersion) {
connection_.SetSupportedVersions(QuicSupportedVersions());
connection_.set_version(QuicVersionMin());
EXPECT_EQ(QuicVersionMin(), connection_.version());
QuicVersionVector supported_versions;
for (size_t i = 0; i < arraysize(kSupportedQuicVersions); ++i) {
supported_versions.push_back(kSupportedQuicVersions[i]);
}
EXPECT_TRUE(connection_.SelectMutualVersion(supported_versions));
EXPECT_EQ(QuicVersionMax(), connection_.version());
EXPECT_LE(QuicVersionMin(), QuicVersionMax());
QuicVersionVector lowest_version_vector;
lowest_version_vector.push_back(QuicVersionMin());
EXPECT_TRUE(connection_.SelectMutualVersion(lowest_version_vector));
EXPECT_EQ(QuicVersionMin(), connection_.version());
QuicVersionVector unsupported_version;
unsupported_version.push_back(QUIC_VERSION_UNSUPPORTED);
EXPECT_FALSE(connection_.SelectMutualVersion(unsupported_version));
}
TEST_P(QuicConnectionTest, ConnectionCloseWhenWritable) {
EXPECT_FALSE(writer_->IsWriteBlocked());
connection_.SendStreamDataWithString(1, "foo", 0, !kFin, NULL);
EXPECT_EQ(0u, connection_.NumQueuedPackets());
EXPECT_EQ(1u, writer_->packets_write_attempts());
TriggerConnectionClose();
EXPECT_EQ(2u, writer_->packets_write_attempts());
}
TEST_P(QuicConnectionTest, ConnectionCloseGettingWriteBlocked) {
BlockOnNextWrite();
TriggerConnectionClose();
EXPECT_EQ(1u, writer_->packets_write_attempts());
EXPECT_TRUE(writer_->IsWriteBlocked());
}
TEST_P(QuicConnectionTest, ConnectionCloseWhenWriteBlocked) {
BlockOnNextWrite();
connection_.SendStreamDataWithString(1, "foo", 0, !kFin, NULL);
EXPECT_EQ(1u, connection_.NumQueuedPackets());
EXPECT_EQ(1u, writer_->packets_write_attempts());
EXPECT_TRUE(writer_->IsWriteBlocked());
TriggerConnectionClose();
EXPECT_EQ(1u, writer_->packets_write_attempts());
}
TEST_P(QuicConnectionTest, AckNotifierTriggerCallback) {
EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_));
scoped_refptr<MockAckNotifierDelegate> delegate(new MockAckNotifierDelegate);
EXPECT_CALL(*delegate, OnAckNotification(_, _, _, _)).Times(1);
connection_.SendStreamDataWithString(1, "foo", 0, !kFin, delegate.get());
EXPECT_CALL(*send_algorithm_, UpdateRtt(_));
EXPECT_CALL(*send_algorithm_, OnPacketAcked(_, _)).Times(1);
QuicAckFrame frame = InitAckFrame(1, 0);
ProcessAckPacket(&frame);
}
TEST_P(QuicConnectionTest, AckNotifierFailToTriggerCallback) {
EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_));
scoped_refptr<MockAckNotifierDelegate> delegate(new MockAckNotifierDelegate);
EXPECT_CALL(*delegate, OnAckNotification(_, _, _, _)).Times(0);
EXPECT_CALL(*send_algorithm_, UpdateRtt(_));
EXPECT_CALL(*send_algorithm_, OnPacketAcked(_, _)).Times(2);
connection_.SendStreamDataWithString(1, "foo", 0, !kFin, delegate.get());
connection_.SendStreamDataWithString(1, "foo", 0, !kFin, NULL);
connection_.SendStreamDataWithString(1, "bar", 0, !kFin, NULL);
QuicAckFrame frame = InitAckFrame(3, 0);
NackPacket(1, &frame);
SequenceNumberSet lost_packets;
lost_packets.insert(1);
EXPECT_CALL(*loss_algorithm_, DetectLostPackets(_, _, _, _))
.WillOnce(Return(lost_packets));
EXPECT_CALL(*send_algorithm_, OnPacketLost(1, _));
EXPECT_CALL(*send_algorithm_, OnPacketAbandoned(1, _));
ProcessAckPacket(&frame);
}
TEST_P(QuicConnectionTest, AckNotifierCallbackAfterRetransmission) {
EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_));
scoped_refptr<MockAckNotifierDelegate> delegate(new MockAckNotifierDelegate);
EXPECT_CALL(*delegate, OnAckNotification(_, _, _, _)).Times(1);
connection_.SendStreamDataWithString(3, "foo", 0, !kFin, NULL);
connection_.SendStreamDataWithString(3, "bar", 0, !kFin, delegate.get());
connection_.SendStreamDataWithString(3, "baz", 0, !kFin, NULL);
connection_.SendStreamDataWithString(3, "qux", 0, !kFin, NULL);
QuicAckFrame frame = InitAckFrame(4, 0);
NackPacket(2, &frame);
SequenceNumberSet lost_packets;
lost_packets.insert(2);
EXPECT_CALL(*loss_algorithm_, DetectLostPackets(_, _, _, _))
.WillOnce(Return(lost_packets));
EXPECT_CALL(*send_algorithm_, UpdateRtt(_));
EXPECT_CALL(*send_algorithm_, OnPacketAcked(_, _)).Times(3);
EXPECT_CALL(*send_algorithm_, OnPacketLost(2, _));
EXPECT_CALL(*send_algorithm_, OnPacketAbandoned(2, _));
EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _));
ProcessAckPacket(&frame);
EXPECT_CALL(*loss_algorithm_, DetectLostPackets(_, _, _, _))
.WillRepeatedly(Return(SequenceNumberSet()));
EXPECT_CALL(*send_algorithm_, UpdateRtt(_));
EXPECT_CALL(*send_algorithm_, OnPacketAcked(5, _));
QuicAckFrame second_ack_frame = InitAckFrame(5, 0);
ProcessAckPacket(&second_ack_frame);
}
TEST_P(QuicConnectionTest, AckNotifierCallbackForAckAfterRTO) {
InSequence s;
scoped_refptr<MockAckNotifierDelegate> delegate(
new StrictMock<MockAckNotifierDelegate>);
QuicTime default_retransmission_time = clock_.ApproximateNow().Add(
DefaultRetransmissionTime());
connection_.SendStreamDataWithString(3, "foo", 0, !kFin, delegate.get());
EXPECT_EQ(1u, outgoing_ack()->sent_info.least_unacked);
EXPECT_EQ(1u, last_header()->packet_sequence_number);
EXPECT_EQ(default_retransmission_time,
connection_.GetRetransmissionAlarm()->deadline());
clock_.AdvanceTime(DefaultRetransmissionTime());
EXPECT_CALL(*send_algorithm_, OnRetransmissionTimeout(true));
EXPECT_CALL(*send_algorithm_, OnPacketSent(_, 2u, _, _));
connection_.GetRetransmissionAlarm()->Fire();
EXPECT_EQ(2u, last_header()->packet_sequence_number);
EXPECT_EQ(1u, outgoing_ack()->sent_info.least_unacked);
EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_));
EXPECT_CALL(*send_algorithm_, UpdateRtt(_));
EXPECT_CALL(*delegate, OnAckNotification(1, _, 1, _));
QuicAckFrame ack_frame = InitAckFrame(1, 0);
ProcessAckPacket(&ack_frame);
EXPECT_CALL(*send_algorithm_, UpdateRtt(_));
EXPECT_CALL(*send_algorithm_, OnPacketAcked(2, _));
QuicAckFrame second_ack_frame = InitAckFrame(2, 0);
ProcessAckPacket(&second_ack_frame);
}
TEST_P(QuicConnectionTest, AckNotifierCallbackForAckOfNackedPacket) {
InSequence s;
scoped_refptr<MockAckNotifierDelegate> delegate(
new StrictMock<MockAckNotifierDelegate>);
connection_.SendStreamDataWithString(3, "foo", 0, !kFin, NULL);
connection_.SendStreamDataWithString(3, "bar", 0, !kFin, delegate.get());
connection_.SendStreamDataWithString(3, "baz", 0, !kFin, NULL);
connection_.SendStreamDataWithString(3, "qux", 0, !kFin, NULL);
QuicAckFrame frame = InitAckFrame(4, 0);
NackPacket(2, &frame);
SequenceNumberSet lost_packets;
lost_packets.insert(2);
EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_));
EXPECT_CALL(*send_algorithm_, UpdateRtt(_));
EXPECT_CALL(*send_algorithm_, OnPacketAcked(1, _));
EXPECT_CALL(*send_algorithm_, OnPacketAcked(3, _));
EXPECT_CALL(*send_algorithm_, OnPacketAcked(4, _));
EXPECT_CALL(*loss_algorithm_, DetectLostPackets(_, _, _, _))
.WillOnce(Return(lost_packets));
EXPECT_CALL(*send_algorithm_, OnPacketLost(2, _));
EXPECT_CALL(*send_algorithm_, OnPacketAbandoned(2, _));
EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _));
ProcessAckPacket(&frame);
SequenceNumberSet no_lost_packets;
EXPECT_CALL(*delegate, OnAckNotification(1, _, 1, _));
EXPECT_CALL(*loss_algorithm_, DetectLostPackets(_, _, _, _))
.WillOnce(Return(no_lost_packets));
QuicAckFrame second_ack_frame = InitAckFrame(4, 0);
ProcessAckPacket(&second_ack_frame);
EXPECT_CALL(*send_algorithm_, UpdateRtt(_));
EXPECT_CALL(*send_algorithm_, OnPacketAcked(5, _));
EXPECT_CALL(*loss_algorithm_, DetectLostPackets(_, _, _, _))
.WillOnce(Return(no_lost_packets));
QuicAckFrame third_ack_frame = InitAckFrame(5, 0);
ProcessAckPacket(&third_ack_frame);
}
TEST_P(QuicConnectionTest, AckNotifierCallbackAfterFECRecovery) {
if (version() < QUIC_VERSION_15) {
return;
}
EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_));
EXPECT_CALL(visitor_, OnCanWrite());
scoped_refptr<MockAckNotifierDelegate> delegate(new MockAckNotifierDelegate);
EXPECT_CALL(*delegate, OnAckNotification(_, _, _, _)).Times(1);
EXPECT_CALL(*send_algorithm_, UpdateRtt(_));
EXPECT_CALL(*send_algorithm_, OnPacketAcked(_, _)).Times(1);
connection_.SendStreamDataWithString(1, "foo", 0, !kFin, delegate.get());
QuicFrames frames;
QuicAckFrame ack_frame = InitAckFrame(1, 0);
frames.push_back(QuicFrame(&ack_frame));
frames.push_back(QuicFrame(&frame1_));
QuicPacketHeader ack_header;
ack_header.public_header.connection_id = connection_id_;
ack_header.public_header.reset_flag = false;
ack_header.public_header.version_flag = false;
ack_header.entropy_flag = !kEntropyFlag;
ack_header.fec_flag = true;
ack_header.packet_sequence_number = 1;
ack_header.is_in_fec_group = IN_FEC_GROUP;
ack_header.fec_group = 1;
QuicPacket* packet =
framer_.BuildUnsizedDataPacket(ack_header, frames).packet;
ProcessFecPacket(2, 1, true, !kEntropyFlag, packet);
}
class MockQuicConnectionDebugVisitor
: public QuicConnectionDebugVisitorInterface {
public:
MOCK_METHOD1(OnFrameAddedToPacket,
void(const QuicFrame&));
MOCK_METHOD5(OnPacketSent,
void(QuicPacketSequenceNumber,
EncryptionLevel,
TransmissionType,
const QuicEncryptedPacket&,
WriteResult));
MOCK_METHOD2(OnPacketRetransmitted,
void(QuicPacketSequenceNumber,
QuicPacketSequenceNumber));
MOCK_METHOD3(OnPacketReceived,
void(const IPEndPoint&,
const IPEndPoint&,
const QuicEncryptedPacket&));
MOCK_METHOD1(OnProtocolVersionMismatch,
void(QuicVersion));
MOCK_METHOD1(OnPacketHeader,
void(const QuicPacketHeader& header));
MOCK_METHOD1(OnStreamFrame,
void(const QuicStreamFrame&));
MOCK_METHOD1(OnAckFrame,
void(const QuicAckFrame& frame));
MOCK_METHOD1(OnCongestionFeedbackFrame,
void(const QuicCongestionFeedbackFrame&));
MOCK_METHOD1(OnStopWaitingFrame,
void(const QuicStopWaitingFrame&));
MOCK_METHOD1(OnRstStreamFrame,
void(const QuicRstStreamFrame&));
MOCK_METHOD1(OnConnectionCloseFrame,
void(const QuicConnectionCloseFrame&));
MOCK_METHOD1(OnPublicResetPacket,
void(const QuicPublicResetPacket&));
MOCK_METHOD1(OnVersionNegotiationPacket,
void(const QuicVersionNegotiationPacket&));
MOCK_METHOD2(OnRevivedPacket,
void(const QuicPacketHeader&, StringPiece payload));
};
TEST_P(QuicConnectionTest, OnPacketHeaderDebugVisitor) {
QuicPacketHeader header;
scoped_ptr<MockQuicConnectionDebugVisitor>
debug_visitor(new StrictMock<MockQuicConnectionDebugVisitor>);
connection_.set_debug_visitor(debug_visitor.get());
EXPECT_CALL(*debug_visitor, OnPacketHeader(Ref(header))).Times(1);
connection_.OnPacketHeader(header);
}
TEST_P(QuicConnectionTest, Pacing) {
ValueRestore<bool> old_flag(&FLAGS_enable_quic_pacing, true);
TestConnection server(connection_id_, IPEndPoint(), helper_.get(),
writer_.get(), true, version(),
kDefaultFlowControlSendWindow);
TestConnection client(connection_id_, IPEndPoint(), helper_.get(),
writer_.get(), false, version(),
kDefaultFlowControlSendWindow);
EXPECT_TRUE(client.sent_packet_manager().using_pacing());
EXPECT_FALSE(server.sent_packet_manager().using_pacing());
}
TEST_P(QuicConnectionTest, ControlFramesInstigateAcks) {
if (version() == QUIC_VERSION_13) {
return;
}
EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_));
QuicWindowUpdateFrame window_update;
window_update.stream_id = 3;
window_update.byte_offset = 1234;
EXPECT_CALL(visitor_, OnWindowUpdateFrames(_));
ProcessFramePacket(QuicFrame(&window_update));
QuicAlarm* ack_alarm = QuicConnectionPeer::GetAckAlarm(&connection_);
EXPECT_TRUE(ack_alarm->IsSet());
ack_alarm->Cancel();
QuicBlockedFrame blocked;
blocked.stream_id = 3;
EXPECT_CALL(visitor_, OnBlockedFrames(_));
ProcessFramePacket(QuicFrame(&blocked));
EXPECT_TRUE(ack_alarm->IsSet());
}
TEST_P(QuicConnectionTest, InvalidFlowControlWindow) {
ValueRestore<bool> old_flag(&FLAGS_enable_quic_pacing, true);
const uint32 kSmallerFlowControlWindow = kDefaultFlowControlSendWindow - 1;
TestConnection connection(connection_id_, IPEndPoint(), helper_.get(),
writer_.get(), true, version(),
kSmallerFlowControlWindow);
EXPECT_EQ(kDefaultFlowControlSendWindow,
connection.max_flow_control_receive_window_bytes());
}
}
}
}