This source file includes following definitions.
- should_process_data_
- ProcessRawData
- EffectivePriority
- IsFlowControlEnabled
- Initialize
- fin_sent
- rst_sent
- set_initial_flow_control_window_bytes
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- SaveProxyAckNotifierDelegate
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
#include "net/quic/reliable_quic_stream.h"
#include "net/quic/quic_ack_notifier.h"
#include "net/quic/quic_connection.h"
#include "net/quic/quic_flags.h"
#include "net/quic/quic_utils.h"
#include "net/quic/quic_write_blocked_list.h"
#include "net/quic/spdy_utils.h"
#include "net/quic/test_tools/quic_session_peer.h"
#include "net/quic/test_tools/quic_test_utils.h"
#include "net/quic/test_tools/reliable_quic_stream_peer.h"
#include "net/test/gtest_util.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gmock_mutant.h"
using base::StringPiece;
using std::min;
using testing::_;
using testing::CreateFunctor;
using testing::InSequence;
using testing::Invoke;
using testing::Return;
using testing::SaveArg;
using testing::StrictMock;
using testing::WithArgs;
namespace net {
namespace test {
namespace {
const char kData1[] = "FooAndBar";
const char kData2[] = "EepAndBaz";
const size_t kDataLen = 9;
const QuicConnectionId kStreamId = 3;
const bool kIsServer = true;
const bool kShouldProcessData = true;
class TestStream : public ReliableQuicStream {
public:
TestStream(QuicStreamId id,
QuicSession* session,
bool should_process_data)
: ReliableQuicStream(id, session),
should_process_data_(should_process_data) {}
virtual uint32 ProcessRawData(const char* data, uint32 data_len) OVERRIDE {
EXPECT_NE(0u, data_len);
DVLOG(1) << "ProcessData data_len: " << data_len;
data_ += string(data, data_len);
return should_process_data_ ? data_len : 0;
}
virtual QuicPriority EffectivePriority() const OVERRIDE {
return QuicUtils::HighestPriority();
}
virtual bool IsFlowControlEnabled() const OVERRIDE {
return true;
}
using ReliableQuicStream::WriteOrBufferData;
using ReliableQuicStream::CloseReadSide;
using ReliableQuicStream::CloseWriteSide;
using ReliableQuicStream::OnClose;
private:
bool should_process_data_;
string data_;
};
class ReliableQuicStreamTest : public ::testing::TestWithParam<bool> {
public:
ReliableQuicStreamTest()
: initial_flow_control_window_bytes_(kMaxPacketSize) {
headers_[":host"] = "www.google.com";
headers_[":path"] = "/index.hml";
headers_[":scheme"] = "https";
headers_["cookie"] =
"__utma=208381060.1228362404.1372200928.1372200928.1372200928.1; "
"__utmc=160408618; "
"GX=DQAAAOEAAACWJYdewdE9rIrW6qw3PtVi2-d729qaa-74KqOsM1NVQblK4VhX"
"hoALMsy6HOdDad2Sz0flUByv7etmo3mLMidGrBoljqO9hSVA40SLqpG_iuKKSHX"
"RW3Np4bq0F0SDGDNsW0DSmTS9ufMRrlpARJDS7qAI6M3bghqJp4eABKZiRqebHT"
"pMU-RXvTI5D5oCF1vYxYofH_l1Kviuiy3oQ1kS1enqWgbhJ2t61_SNdv-1XJIS0"
"O3YeHLmVCs62O6zp89QwakfAWK9d3IDQvVSJzCQsvxvNIvaZFa567MawWlXg0Rh"
"1zFMi5vzcns38-8_Sns; "
"GA=v*2%2Fmem*57968640*47239936%2Fmem*57968640*47114716%2Fno-nm-"
"yj*15%2Fno-cc-yj*5%2Fpc-ch*133685%2Fpc-s-cr*133947%2Fpc-s-t*1339"
"47%2Fno-nm-yj*4%2Fno-cc-yj*1%2Fceft-as*1%2Fceft-nqas*0%2Fad-ra-c"
"v_p%2Fad-nr-cv_p-f*1%2Fad-v-cv_p*859%2Fad-ns-cv_p-f*1%2Ffn-v-ad%"
"2Fpc-t*250%2Fpc-cm*461%2Fpc-s-cr*722%2Fpc-s-t*722%2Fau_p*4"
"SICAID=AJKiYcHdKgxum7KMXG0ei2t1-W4OD1uW-ecNsCqC0wDuAXiDGIcT_HA2o1"
"3Rs1UKCuBAF9g8rWNOFbxt8PSNSHFuIhOo2t6bJAVpCsMU5Laa6lewuTMYI8MzdQP"
"ARHKyW-koxuhMZHUnGBJAM1gJODe0cATO_KGoX4pbbFxxJ5IicRxOrWK_5rU3cdy6"
"edlR9FsEdH6iujMcHkbE5l18ehJDwTWmBKBzVD87naobhMMrF6VvnDGxQVGp9Ir_b"
"Rgj3RWUoPumQVCxtSOBdX0GlJOEcDTNCzQIm9BSfetog_eP_TfYubKudt5eMsXmN6"
"QnyXHeGeK2UINUzJ-D30AFcpqYgH9_1BvYSpi7fc7_ydBU8TaD8ZRxvtnzXqj0RfG"
"tuHghmv3aD-uzSYJ75XDdzKdizZ86IG6Fbn1XFhYZM-fbHhm3mVEXnyRW4ZuNOLFk"
"Fas6LMcVC6Q8QLlHYbXBpdNFuGbuZGUnav5C-2I_-46lL0NGg3GewxGKGHvHEfoyn"
"EFFlEYHsBQ98rXImL8ySDycdLEFvBPdtctPmWCfTxwmoSMLHU2SCVDhbqMWU5b0yr"
"JBCScs_ejbKaqBDoB7ZGxTvqlrB__2ZmnHHjCr8RgMRtKNtIeuZAo ";
}
void Initialize(bool stream_should_process_data) {
connection_ = new StrictMock<MockConnection>(kIsServer);
session_.reset(new StrictMock<MockSession>(connection_));
session_->config()->set_peer_initial_flow_control_window_bytes(
initial_flow_control_window_bytes_);
stream_.reset(new TestStream(kStreamId, session_.get(),
stream_should_process_data));
stream2_.reset(new TestStream(kStreamId + 2, session_.get(),
stream_should_process_data));
write_blocked_list_ =
QuicSessionPeer::GetWriteblockedStreams(session_.get());
}
bool fin_sent() { return ReliableQuicStreamPeer::FinSent(stream_.get()); }
bool rst_sent() { return ReliableQuicStreamPeer::RstSent(stream_.get()); }
void set_initial_flow_control_window_bytes(uint32 val) {
initial_flow_control_window_bytes_ = val;
}
protected:
MockConnection* connection_;
scoped_ptr<MockSession> session_;
scoped_ptr<TestStream> stream_;
scoped_ptr<TestStream> stream2_;
SpdyHeaderBlock headers_;
QuicWriteBlockedList* write_blocked_list_;
uint32 initial_flow_control_window_bytes_;
};
TEST_F(ReliableQuicStreamTest, WriteAllData) {
Initialize(kShouldProcessData);
connection_->options()->max_packet_length =
1 + QuicPacketCreator::StreamFramePacketOverhead(
connection_->version(), PACKET_8BYTE_CONNECTION_ID, !kIncludeVersion,
PACKET_6BYTE_SEQUENCE_NUMBER, NOT_IN_FEC_GROUP);
EXPECT_CALL(*session_, WritevData(kStreamId, _, _, _, _)).WillOnce(
Return(QuicConsumedData(kDataLen, true)));
stream_->WriteOrBufferData(kData1, false, NULL);
EXPECT_FALSE(write_blocked_list_->HasWriteBlockedStreams());
}
TEST_F(ReliableQuicStreamTest, NoBlockingIfNoDataOrFin) {
Initialize(kShouldProcessData);
EXPECT_DFATAL(stream_->WriteOrBufferData(StringPiece(), false, NULL), "");
EXPECT_FALSE(write_blocked_list_->HasWriteBlockedStreams());
}
TEST_F(ReliableQuicStreamTest, BlockIfOnlySomeDataConsumed) {
Initialize(kShouldProcessData);
EXPECT_CALL(*session_, WritevData(kStreamId, _, _, _, _)).WillOnce(
Return(QuicConsumedData(1, false)));
stream_->WriteOrBufferData(StringPiece(kData1, 2), false, NULL);
ASSERT_EQ(1u, write_blocked_list_->NumBlockedStreams());
}
TEST_F(ReliableQuicStreamTest, BlockIfFinNotConsumedWithData) {
Initialize(kShouldProcessData);
EXPECT_CALL(*session_, WritevData(kStreamId, _, _, _, _)).WillOnce(
Return(QuicConsumedData(2, false)));
stream_->WriteOrBufferData(StringPiece(kData1, 2), true, NULL);
ASSERT_EQ(1u, write_blocked_list_->NumBlockedStreams());
}
TEST_F(ReliableQuicStreamTest, BlockIfSoloFinNotConsumed) {
Initialize(kShouldProcessData);
EXPECT_CALL(*session_, WritevData(kStreamId, _, _, _, _)).WillOnce(
Return(QuicConsumedData(0, false)));
stream_->WriteOrBufferData(StringPiece(), true, NULL);
ASSERT_EQ(1u, write_blocked_list_->NumBlockedStreams());
}
TEST_F(ReliableQuicStreamTest, WriteOrBufferData) {
Initialize(kShouldProcessData);
EXPECT_FALSE(write_blocked_list_->HasWriteBlockedStreams());
connection_->options()->max_packet_length =
1 + QuicPacketCreator::StreamFramePacketOverhead(
connection_->version(), PACKET_8BYTE_CONNECTION_ID, !kIncludeVersion,
PACKET_6BYTE_SEQUENCE_NUMBER, NOT_IN_FEC_GROUP);
EXPECT_CALL(*session_, WritevData(_, _, _, _, _)).WillOnce(
Return(QuicConsumedData(kDataLen - 1, false)));
stream_->WriteOrBufferData(kData1, false, NULL);
EXPECT_TRUE(write_blocked_list_->HasWriteBlockedStreams());
stream_->WriteOrBufferData(kData2, false, NULL);
InSequence s;
EXPECT_CALL(*session_, WritevData(_, _, _, _, _)).
WillOnce(Return(QuicConsumedData(1, false)));
EXPECT_CALL(*session_, WritevData(_, _, _, _, _)).
WillOnce(Return(QuicConsumedData(kDataLen - 2, false)));
stream_->OnCanWrite();
EXPECT_CALL(*session_, WritevData(_, _, _, _, _)).
WillOnce(Return(QuicConsumedData(2, true)));
stream_->OnCanWrite();
}
TEST_F(ReliableQuicStreamTest, ConnectionCloseAfterStreamClose) {
Initialize(kShouldProcessData);
stream_->CloseReadSide();
stream_->CloseWriteSide();
EXPECT_EQ(QUIC_STREAM_NO_ERROR, stream_->stream_error());
EXPECT_EQ(QUIC_NO_ERROR, stream_->connection_error());
stream_->OnConnectionClosed(QUIC_INTERNAL_ERROR, false);
EXPECT_EQ(QUIC_STREAM_NO_ERROR, stream_->stream_error());
EXPECT_EQ(QUIC_NO_ERROR, stream_->connection_error());
}
TEST_F(ReliableQuicStreamTest, RstAlwaysSentIfNoFinSent) {
Initialize(kShouldProcessData);
EXPECT_FALSE(fin_sent());
EXPECT_FALSE(rst_sent());
EXPECT_CALL(*session_, WritevData(kStreamId, _, _, _, _)).WillOnce(
Return(QuicConsumedData(1, false)));
stream_->WriteOrBufferData(StringPiece(kData1, 1), false, NULL);
EXPECT_FALSE(fin_sent());
EXPECT_FALSE(rst_sent());
EXPECT_CALL(*session_, SendRstStream(_, _, _));
stream_->OnClose();
EXPECT_FALSE(fin_sent());
EXPECT_TRUE(rst_sent());
}
TEST_F(ReliableQuicStreamTest, RstNotSentIfFinSent) {
Initialize(kShouldProcessData);
EXPECT_FALSE(fin_sent());
EXPECT_FALSE(rst_sent());
EXPECT_CALL(*session_, WritevData(kStreamId, _, _, _, _)).WillOnce(
Return(QuicConsumedData(1, true)));
stream_->WriteOrBufferData(StringPiece(kData1, 1), true, NULL);
EXPECT_TRUE(fin_sent());
EXPECT_FALSE(rst_sent());
stream_->OnClose();
EXPECT_TRUE(fin_sent());
EXPECT_FALSE(rst_sent());
}
TEST_F(ReliableQuicStreamTest, OnlySendOneRst) {
Initialize(kShouldProcessData);
EXPECT_FALSE(fin_sent());
EXPECT_FALSE(rst_sent());
const int expected_resets = 1;
EXPECT_CALL(*session_, SendRstStream(_, _, _)).Times(expected_resets);
stream_->Reset(QUIC_STREAM_CANCELLED);
EXPECT_FALSE(fin_sent());
EXPECT_TRUE(rst_sent());
stream_->OnClose();
EXPECT_FALSE(fin_sent());
EXPECT_TRUE(rst_sent());
}
TEST_F(ReliableQuicStreamTest, StreamFlowControlMultipleWindowUpdates) {
ValueRestore<bool> old_flag(&FLAGS_enable_quic_stream_flow_control, true);
set_initial_flow_control_window_bytes(1000);
Initialize(kShouldProcessData);
EXPECT_EQ(initial_flow_control_window_bytes_,
ReliableQuicStreamPeer::SendWindowOffset(stream_.get()));
QuicWindowUpdateFrame window_update_1(stream_->id(), 1234);
stream_->OnWindowUpdateFrame(window_update_1);
EXPECT_EQ(window_update_1.byte_offset,
ReliableQuicStreamPeer::SendWindowOffset(stream_.get()));
QuicWindowUpdateFrame window_update_2(stream_->id(), 1);
QuicWindowUpdateFrame window_update_3(stream_->id(), 9999);
QuicWindowUpdateFrame window_update_4(stream_->id(), 5678);
stream_->OnWindowUpdateFrame(window_update_2);
stream_->OnWindowUpdateFrame(window_update_3);
stream_->OnWindowUpdateFrame(window_update_4);
EXPECT_EQ(window_update_3.byte_offset,
ReliableQuicStreamPeer::SendWindowOffset(stream_.get()));
}
void SaveProxyAckNotifierDelegate(
scoped_refptr<QuicAckNotifier::DelegateInterface>* delegate_out,
QuicAckNotifier::DelegateInterface* delegate) {
*delegate_out = delegate;
}
TEST_F(ReliableQuicStreamTest, WriteOrBufferDataWithQuicAckNotifier) {
Initialize(kShouldProcessData);
scoped_refptr<MockAckNotifierDelegate> delegate(
new StrictMock<MockAckNotifierDelegate>);
const int kDataSize = 16 * 1024;
const string kData(kDataSize, 'a');
const int kFirstWriteSize = 100;
const int kSecondWriteSize = 50;
const int kLastWriteSize = kDataSize - kFirstWriteSize - kSecondWriteSize;
scoped_refptr<QuicAckNotifier::DelegateInterface> proxy_delegate;
EXPECT_CALL(*session_, WritevData(kStreamId, _, _, _, _)).WillOnce(DoAll(
WithArgs<4>(Invoke(CreateFunctor(
&SaveProxyAckNotifierDelegate, &proxy_delegate))),
Return(QuicConsumedData(kFirstWriteSize, false))));
stream_->WriteOrBufferData(kData, false, delegate.get());
EXPECT_TRUE(write_blocked_list_->HasWriteBlockedStreams());
EXPECT_CALL(*session_, WritevData(kStreamId, _, _, _, proxy_delegate.get())).
WillOnce(
Return(QuicConsumedData(kSecondWriteSize, false)));
stream_->OnCanWrite();
EXPECT_CALL(*session_, WritevData(kStreamId, _, _, _, proxy_delegate.get())).
WillOnce(
Return(QuicConsumedData(0, false)));
stream_->OnCanWrite();
EXPECT_CALL(*session_, WritevData(kStreamId, _, _, _, proxy_delegate.get())).
WillOnce(
Return(QuicConsumedData(kLastWriteSize, false)));
stream_->OnCanWrite();
proxy_delegate->OnAckNotification(1, 2, 3, 4);
proxy_delegate->OnAckNotification(10, 20, 30, 40);
EXPECT_CALL(*delegate, OnAckNotification(111, 222, 333, 444));
proxy_delegate->OnAckNotification(100, 200, 300, 400);
}
TEST_F(ReliableQuicStreamTest, WriteOrBufferDataAckNotificationBeforeFlush) {
Initialize(kShouldProcessData);
scoped_refptr<MockAckNotifierDelegate> delegate(
new StrictMock<MockAckNotifierDelegate>);
const int kDataSize = 16 * 1024;
const string kData(kDataSize, 'a');
const int kInitialWriteSize = 100;
scoped_refptr<QuicAckNotifier::DelegateInterface> proxy_delegate;
EXPECT_CALL(*session_, WritevData(kStreamId, _, _, _, _)).WillOnce(DoAll(
WithArgs<4>(Invoke(CreateFunctor(
&SaveProxyAckNotifierDelegate, &proxy_delegate))),
Return(QuicConsumedData(kInitialWriteSize, false))));
stream_->WriteOrBufferData(kData, false, delegate.get());
EXPECT_TRUE(write_blocked_list_->HasWriteBlockedStreams());
proxy_delegate->OnAckNotification(1, 2, 3, 4);
proxy_delegate = NULL;
EXPECT_CALL(*session_, WritevData(kStreamId, _, _, _, _)).WillOnce(DoAll(
WithArgs<4>(Invoke(CreateFunctor(
&SaveProxyAckNotifierDelegate, &proxy_delegate))),
Return(QuicConsumedData(kDataSize - kInitialWriteSize, false))));
stream_->OnCanWrite();
EXPECT_CALL(*delegate, OnAckNotification(101, 202, 303, 404));
proxy_delegate->OnAckNotification(100, 200, 300, 400);
}
TEST_F(ReliableQuicStreamTest, WriteAndBufferDataWithAckNotiferNoBuffer) {
Initialize(kShouldProcessData);
scoped_refptr<MockAckNotifierDelegate> delegate(
new StrictMock<MockAckNotifierDelegate>);
scoped_refptr<QuicAckNotifier::DelegateInterface> proxy_delegate;
EXPECT_CALL(*session_, WritevData(kStreamId, _, _, _, _)).WillOnce(DoAll(
WithArgs<4>(Invoke(CreateFunctor(
&SaveProxyAckNotifierDelegate, &proxy_delegate))),
Return(QuicConsumedData(kDataLen, true))));
stream_->WriteOrBufferData(kData1, true, delegate.get());
EXPECT_FALSE(write_blocked_list_->HasWriteBlockedStreams());
EXPECT_CALL(*delegate, OnAckNotification(1, 2, 3, 4));
proxy_delegate->OnAckNotification(1, 2, 3, 4);
}
TEST_F(ReliableQuicStreamTest, BufferOnWriteAndBufferDataWithAckNotifer) {
Initialize(kShouldProcessData);
scoped_refptr<MockAckNotifierDelegate> delegate(
new StrictMock<MockAckNotifierDelegate>);
scoped_refptr<QuicAckNotifier::DelegateInterface> proxy_delegate;
EXPECT_CALL(*session_, WritevData(kStreamId, _, _, _, _)).WillOnce(
Return(QuicConsumedData(0, false)));
stream_->WriteOrBufferData(kData1, true, delegate.get());
EXPECT_TRUE(write_blocked_list_->HasWriteBlockedStreams());
EXPECT_CALL(*session_, WritevData(kStreamId, _, _, _, _)).WillOnce(DoAll(
WithArgs<4>(Invoke(CreateFunctor(
&SaveProxyAckNotifierDelegate, &proxy_delegate))),
Return(QuicConsumedData(kDataLen, true))));
stream_->OnCanWrite();
EXPECT_CALL(*delegate, OnAckNotification(1, 2, 3, 4));
proxy_delegate->OnAckNotification(1, 2, 3, 4);
}
TEST_F(ReliableQuicStreamTest, WriteAndBufferDataWithAckNotiferOnlyFinRemains) {
Initialize(kShouldProcessData);
scoped_refptr<MockAckNotifierDelegate> delegate(
new StrictMock<MockAckNotifierDelegate>);
scoped_refptr<QuicAckNotifier::DelegateInterface> proxy_delegate;
EXPECT_CALL(*session_, WritevData(kStreamId, _, _, _, _)).WillOnce(DoAll(
WithArgs<4>(Invoke(CreateFunctor(
&SaveProxyAckNotifierDelegate, &proxy_delegate))),
Return(QuicConsumedData(kDataLen, false))));
stream_->WriteOrBufferData(kData1, true, delegate.get());
EXPECT_TRUE(write_blocked_list_->HasWriteBlockedStreams());
EXPECT_CALL(*session_, WritevData(kStreamId, _, _, _, _)).WillOnce(DoAll(
WithArgs<4>(Invoke(CreateFunctor(
&SaveProxyAckNotifierDelegate, &proxy_delegate))),
Return(QuicConsumedData(0, true))));
stream_->OnCanWrite();
proxy_delegate->OnAckNotification(1, 2, 3, 4);
EXPECT_CALL(*delegate, OnAckNotification(11, 22, 33, 44));
proxy_delegate->OnAckNotification(10, 20, 30, 40);
}
}
}
}