// 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" #include "testing/gmock_mutant.h" using testing::AnyNumber; using testing::CreateFunctor; using testing::Invoke; 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(const SpdyHeaderBlock&)); MOCK_METHOD2(OnDataReceived, int(const char*, int)); MOCK_METHOD0(OnDataAvailable, void()); 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 { 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" "RW3Np4bq0F0SDGDNsW0DSmTS9ufMRrlpARJDS7qA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MWU5b0yr" "JBCScs_ejbKaqBDoB7ZGxTvqlrB__2ZmnHHjCr8RgMRtKNtIeuZAo "; } void ReadData(StringPiece expected_data) { scoped_refptr buffer(new IOBuffer(expected_data.length() + 1)); EXPECT_EQ(static_cast(expected_data.length()), stream_->Read(buffer.get(), expected_data.length() + 1)); EXPECT_EQ(expected_data, StringPiece(buffer->data(), expected_data.length())); } testing::StrictMock 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()); QuicStreamOffset offset = 0; stream_->OnStreamHeaders(uncompressed_headers); stream_->OnStreamHeadersComplete(false, uncompressed_headers.length()); EXPECT_CALL(delegate_, OnHeadersAvailable(headers_)); base::MessageLoop::current()->RunUntilIdle(); 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, OnDataAvailableBeforeHeaders) { EXPECT_CALL(delegate_, OnClose(QUIC_NO_ERROR)); EXPECT_CALL(delegate_, OnDataAvailable()).Times(0); stream_->OnDataAvailable(); } TEST_P(QuicReliableClientStreamTest, OnDataAvailable) { InitializeHeaders(); std::string uncompressed_headers = SpdyUtils::SerializeUncompressedHeaders(headers_, GetParam()); stream_->OnStreamHeaders(uncompressed_headers); stream_->OnStreamHeadersComplete(false, uncompressed_headers.length()); EXPECT_CALL(delegate_, OnHeadersAvailable(headers_)); base::MessageLoop::current()->RunUntilIdle(); EXPECT_TRUE(stream_->decompressed_headers().empty()); const char data[] = "hello world!"; stream_->OnStreamFrame(QuicStreamFrame(kTestStreamId, /*fin=*/false, /*offset=*/0, data)); EXPECT_CALL(delegate_, OnDataAvailable()) .WillOnce(testing::Invoke( CreateFunctor(this, &QuicReliableClientStreamTest::ReadData, StringPiece(data, arraysize(data) - 1)))); base::MessageLoop::current()->RunUntilIdle(); EXPECT_CALL(delegate_, OnClose(QUIC_NO_ERROR)); } TEST_P(QuicReliableClientStreamTest, ProcessHeadersWithError) { std::string bad_headers = "..."; stream_->OnStreamHeaders(StringPiece(bad_headers)); stream_->OnStreamHeadersComplete(false, bad_headers.length()); EXPECT_CALL(session_, SendRstStream(kTestStreamId, QUIC_BAD_APPLICATION_PAYLOAD, 0)); base::MessageLoop::current()->RunUntilIdle(); EXPECT_CALL(delegate_, OnClose(QUIC_NO_ERROR)); } TEST_P(QuicReliableClientStreamTest, OnDataAvailableWithError) { InitializeHeaders(); std::string uncompressed_headers = SpdyUtils::SerializeUncompressedHeaders(headers_, GetParam()); stream_->OnStreamHeaders(uncompressed_headers); stream_->OnStreamHeadersComplete(false, uncompressed_headers.length()); EXPECT_CALL(delegate_, OnHeadersAvailable(headers_)); base::MessageLoop::current()->RunUntilIdle(); EXPECT_TRUE(stream_->decompressed_headers().empty()); const char data[] = "hello world!"; stream_->OnStreamFrame(QuicStreamFrame(kTestStreamId, /*fin=*/false, /*offset=*/0, data)); EXPECT_CALL(delegate_, OnDataAvailable()) .WillOnce(testing::Invoke(CreateFunctor( stream_, &QuicReliableClientStream::Reset, QUIC_STREAM_CANCELLED))); base::MessageLoop::current()->RunUntilIdle(); EXPECT_CALL(delegate_, OnClose(QUIC_NO_ERROR)); } 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