blob: 9aff0e14bf35ee18114e9982036302c1c6f36a1b [file] [log] [blame]
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "net/quic/quic_reliable_client_stream.h"
#include "net/base/net_errors.h"
#include "net/base/test_completion_callback.h"
#include "net/quic/quic_chromium_client_session.h"
#include "net/quic/quic_utils.h"
#include "net/quic/spdy_utils.h"
#include "net/quic/test_tools/quic_test_utils.h"
#include "testing/gmock/include/gmock/gmock.h"
using testing::AnyNumber;
using testing::Return;
using testing::StrEq;
using testing::_;
namespace net {
namespace test {
namespace {
const QuicStreamId kTestStreamId = 5u;
class MockDelegate : public QuicReliableClientStream::Delegate {
public:
MockDelegate() {}
MOCK_METHOD0(OnSendData, int());
MOCK_METHOD2(OnSendDataComplete, int(int, bool*));
MOCK_METHOD1(OnHeadersAvailable, void(StringPiece));
MOCK_METHOD2(OnDataReceived, int(const char*, int));
MOCK_METHOD1(OnClose, void(QuicErrorCode));
MOCK_METHOD1(OnError, void(int));
MOCK_METHOD0(HasSendHeadersComplete, bool());
private:
DISALLOW_COPY_AND_ASSIGN(MockDelegate);
};
class QuicReliableClientStreamTest
: public ::testing::TestWithParam<QuicVersion> {
public:
QuicReliableClientStreamTest()
: session_(new MockConnection(Perspective::IS_CLIENT,
SupportedVersions(GetParam()))) {
stream_ =
new QuicReliableClientStream(kTestStreamId, &session_, BoundNetLog());
session_.ActivateStream(stream_);
stream_->SetDelegate(&delegate_);
}
void InitializeHeaders() {
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"
"RW3Np4bq0F0SDGDNs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L8ySDycdLEFvBPdtctPmWCfTxwmoSMLHU2SCVDhbqMWU5b0yr"
"JBCScs_ejbKaqBDoB7ZGxTvqlrB__2ZmnHHjCr8RgMRtKNtIeuZAo ";
}
testing::StrictMock<MockDelegate> delegate_;
MockQuicSpdySession session_;
QuicReliableClientStream* stream_;
QuicCryptoClientConfig crypto_config_;
SpdyHeaderBlock headers_;
};
INSTANTIATE_TEST_CASE_P(Version, QuicReliableClientStreamTest,
::testing::ValuesIn(QuicSupportedVersions()));
TEST_P(QuicReliableClientStreamTest, OnFinRead) {
InitializeHeaders();
std::string uncompressed_headers =
SpdyUtils::SerializeUncompressedHeaders(headers_, GetParam());
EXPECT_CALL(delegate_, OnHeadersAvailable(StringPiece(uncompressed_headers)));
QuicStreamOffset offset = 0;
stream_->OnStreamHeaders(uncompressed_headers);
stream_->OnStreamHeadersComplete(false, uncompressed_headers.length());
EXPECT_TRUE(stream_->decompressed_headers().empty());
QuicStreamFrame frame2(kTestStreamId, true, offset, StringPiece());
EXPECT_CALL(delegate_, OnClose(QUIC_NO_ERROR));
stream_->OnStreamFrame(frame2);
}
TEST_P(QuicReliableClientStreamTest, ProcessDataBeforeHeaders) {
const char data[] = "hello world!";
EXPECT_CALL(delegate_, OnClose(QUIC_NO_ERROR));
EXPECT_EQ(0u, stream_->ProcessData(data, arraysize(data)));
}
TEST_P(QuicReliableClientStreamTest, ProcessData) {
InitializeHeaders();
std::string uncompressed_headers =
SpdyUtils::SerializeUncompressedHeaders(headers_, GetParam());
EXPECT_CALL(delegate_, OnHeadersAvailable(StringPiece(uncompressed_headers)));
stream_->OnStreamHeaders(uncompressed_headers);
stream_->OnStreamHeadersComplete(false, uncompressed_headers.length());
const char data[] = "hello world!";
EXPECT_CALL(delegate_, OnDataReceived(StrEq(data), arraysize(data)));
EXPECT_CALL(delegate_, OnClose(QUIC_NO_ERROR));
EXPECT_EQ(arraysize(data), stream_->ProcessData(data, arraysize(data)));
}
TEST_P(QuicReliableClientStreamTest, ProcessDataWithError) {
EXPECT_CALL(delegate_, OnHeadersAvailable(StringPiece("")));
stream_->OnStreamHeadersComplete(false, 0); // Send empty headers.
const char data[] = "hello world!";
EXPECT_CALL(delegate_,
OnDataReceived(StrEq(data),
arraysize(data))).WillOnce(Return(ERR_UNEXPECTED));
EXPECT_CALL(delegate_, OnClose(QUIC_NO_ERROR));
EXPECT_EQ(0u, stream_->ProcessData(data, arraysize(data)));
}
TEST_P(QuicReliableClientStreamTest, OnError) {
EXPECT_CALL(delegate_, OnError(ERR_INTERNET_DISCONNECTED));
stream_->OnError(ERR_INTERNET_DISCONNECTED);
EXPECT_FALSE(stream_->GetDelegate());
}
TEST_P(QuicReliableClientStreamTest, WriteStreamData) {
EXPECT_CALL(delegate_, OnClose(QUIC_NO_ERROR));
const char kData1[] = "hello world";
const size_t kDataLen = arraysize(kData1);
// All data written.
EXPECT_CALL(session_, WritevData(stream_->id(), _, _, _, _, _)).WillOnce(
Return(QuicConsumedData(kDataLen, true)));
TestCompletionCallback callback;
EXPECT_EQ(OK, stream_->WriteStreamData(base::StringPiece(kData1, kDataLen),
true, callback.callback()));
}
TEST_P(QuicReliableClientStreamTest, WriteStreamDataAsync) {
EXPECT_CALL(delegate_, HasSendHeadersComplete()).Times(AnyNumber());
EXPECT_CALL(delegate_, OnClose(QUIC_NO_ERROR));
const char kData1[] = "hello world";
const size_t kDataLen = arraysize(kData1);
// No data written.
EXPECT_CALL(session_, WritevData(stream_->id(), _, _, _, _, _)).WillOnce(
Return(QuicConsumedData(0, false)));
TestCompletionCallback callback;
EXPECT_EQ(ERR_IO_PENDING,
stream_->WriteStreamData(base::StringPiece(kData1, kDataLen),
true, callback.callback()));
ASSERT_FALSE(callback.have_result());
// All data written.
EXPECT_CALL(session_, WritevData(stream_->id(), _, _, _, _, _)).WillOnce(
Return(QuicConsumedData(kDataLen, true)));
stream_->OnCanWrite();
ASSERT_TRUE(callback.have_result());
EXPECT_EQ(OK, callback.WaitForResult());
}
} // namespace
} // namespace test
} // namespace net