1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "net/quic/quic_reliable_client_stream.h"
6
7 #include "net/base/net_errors.h"
8 #include "net/base/test_completion_callback.h"
9 #include "net/quic/quic_client_session.h"
10 #include "net/quic/quic_utils.h"
11 #include "net/quic/spdy_utils.h"
12 #include "net/quic/test_tools/quic_test_utils.h"
13 #include "testing/gmock/include/gmock/gmock.h"
14
15 using testing::AnyNumber;
16 using testing::Return;
17 using testing::StrEq;
18 using testing::_;
19
20 namespace net {
21 namespace test {
22 namespace {
23
24 const QuicConnectionId kStreamId = 3;
25
26 class MockDelegate : public QuicReliableClientStream::Delegate {
27 public:
MockDelegate()28 MockDelegate() {}
29
30 MOCK_METHOD0(OnSendData, int());
31 MOCK_METHOD2(OnSendDataComplete, int(int, bool*));
32 MOCK_METHOD2(OnDataReceived, int(const char*, int));
33 MOCK_METHOD1(OnClose, void(QuicErrorCode));
34 MOCK_METHOD1(OnError, void(int));
35 MOCK_METHOD0(HasSendHeadersComplete, bool());
36
37 private:
38 DISALLOW_COPY_AND_ASSIGN(MockDelegate);
39 };
40
41 class QuicReliableClientStreamTest
42 : public ::testing::TestWithParam<QuicVersion> {
43 public:
QuicReliableClientStreamTest()44 QuicReliableClientStreamTest()
45 : session_(new MockConnection(false, SupportedVersions(GetParam()))) {
46 stream_ = new QuicReliableClientStream(kStreamId, &session_, BoundNetLog());
47 session_.ActivateStream(stream_);
48 stream_->SetDelegate(&delegate_);
49 }
50
InitializeHeaders()51 void InitializeHeaders() {
52 headers_[":host"] = "www.google.com";
53 headers_[":path"] = "/index.hml";
54 headers_[":scheme"] = "https";
55 headers_["cookie"] =
56 "__utma=208381060.1228362404.1372200928.1372200928.1372200928.1; "
57 "__utmc=160408618; "
58 "GX=DQAAAOEAAACWJYdewdE9rIrW6qw3PtVi2-d729qaa-74KqOsM1NVQblK4VhX"
59 "hoALMsy6HOdDad2Sz0flUByv7et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k"
76 "Fas6LMcVC6Q8QLlHYbXBpdNFuGbuZGUnav5C-2I_-46lL0NGg3GewxGKGHvHEfoyn"
77 "EFFlEYHsBQ98rXImL8ySDycdLEFvBPdtctPmWCfTxwmoSMLHU2SCVDhbqMWU5b0yr"
78 "JBCScs_ejbKaqBDoB7ZGxTvqlrB__2ZmnHHjCr8RgMRtKNtIeuZAo ";
79 }
80
81 testing::StrictMock<MockDelegate> delegate_;
82 MockSession session_;
83 QuicReliableClientStream* stream_;
84 QuicCryptoClientConfig crypto_config_;
85 SpdyHeaderBlock headers_;
86 };
87
88 INSTANTIATE_TEST_CASE_P(Version, QuicReliableClientStreamTest,
89 ::testing::ValuesIn(QuicSupportedVersions()));
90
TEST_P(QuicReliableClientStreamTest,OnFinRead)91 TEST_P(QuicReliableClientStreamTest, OnFinRead) {
92 InitializeHeaders();
93 string uncompressed_headers =
94 SpdyUtils::SerializeUncompressedHeaders(headers_);
95 EXPECT_CALL(delegate_, OnDataReceived(StrEq(uncompressed_headers.data()),
96 uncompressed_headers.size()));
97 QuicStreamOffset offset = 0;
98 stream_->OnStreamHeaders(uncompressed_headers);
99 stream_->OnStreamHeadersComplete(false, uncompressed_headers.length());
100
101 IOVector iov;
102 QuicStreamFrame frame2(kStreamId, true, offset, iov);
103 EXPECT_CALL(delegate_, OnClose(QUIC_NO_ERROR));
104 stream_->OnStreamFrame(frame2);
105 }
106
TEST_P(QuicReliableClientStreamTest,ProcessData)107 TEST_P(QuicReliableClientStreamTest, ProcessData) {
108 const char data[] = "hello world!";
109 EXPECT_CALL(delegate_, OnDataReceived(StrEq(data), arraysize(data)));
110 EXPECT_CALL(delegate_, OnClose(QUIC_NO_ERROR));
111
112 EXPECT_EQ(arraysize(data), stream_->ProcessData(data, arraysize(data)));
113 }
114
TEST_P(QuicReliableClientStreamTest,ProcessDataWithError)115 TEST_P(QuicReliableClientStreamTest, ProcessDataWithError) {
116 const char data[] = "hello world!";
117 EXPECT_CALL(delegate_,
118 OnDataReceived(StrEq(data),
119 arraysize(data))).WillOnce(Return(ERR_UNEXPECTED));
120 EXPECT_CALL(delegate_, OnClose(QUIC_NO_ERROR));
121
122
123 EXPECT_EQ(0u, stream_->ProcessData(data, arraysize(data)));
124 }
125
TEST_P(QuicReliableClientStreamTest,OnError)126 TEST_P(QuicReliableClientStreamTest, OnError) {
127 EXPECT_CALL(delegate_, OnError(ERR_INTERNET_DISCONNECTED));
128
129 stream_->OnError(ERR_INTERNET_DISCONNECTED);
130 EXPECT_FALSE(stream_->GetDelegate());
131 }
132
TEST_P(QuicReliableClientStreamTest,WriteStreamData)133 TEST_P(QuicReliableClientStreamTest, WriteStreamData) {
134 EXPECT_CALL(delegate_, OnClose(QUIC_NO_ERROR));
135
136 const char kData1[] = "hello world";
137 const size_t kDataLen = arraysize(kData1);
138
139 // All data written.
140 EXPECT_CALL(session_, WritevData(stream_->id(), _, _, _, _, _)).WillOnce(
141 Return(QuicConsumedData(kDataLen, true)));
142 TestCompletionCallback callback;
143 EXPECT_EQ(OK, stream_->WriteStreamData(base::StringPiece(kData1, kDataLen),
144 true, callback.callback()));
145 }
146
TEST_P(QuicReliableClientStreamTest,WriteStreamDataAsync)147 TEST_P(QuicReliableClientStreamTest, WriteStreamDataAsync) {
148 EXPECT_CALL(delegate_, HasSendHeadersComplete()).Times(AnyNumber());
149 EXPECT_CALL(delegate_, OnClose(QUIC_NO_ERROR));
150
151 const char kData1[] = "hello world";
152 const size_t kDataLen = arraysize(kData1);
153
154 // No data written.
155 EXPECT_CALL(session_, WritevData(stream_->id(), _, _, _, _, _)).WillOnce(
156 Return(QuicConsumedData(0, false)));
157 TestCompletionCallback callback;
158 EXPECT_EQ(ERR_IO_PENDING,
159 stream_->WriteStreamData(base::StringPiece(kData1, kDataLen),
160 true, callback.callback()));
161 ASSERT_FALSE(callback.have_result());
162
163 // All data written.
164 EXPECT_CALL(session_, WritevData(stream_->id(), _, _, _, _, _)).WillOnce(
165 Return(QuicConsumedData(kDataLen, true)));
166 stream_->OnCanWrite();
167 ASSERT_TRUE(callback.have_result());
168 EXPECT_EQ(OK, callback.WaitForResult());
169 }
170
171 } // namespace
172 } // namespace test
173 } // namespace net
174