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.
5 #include "net/quic/quic_reliable_client_stream.h"
7 #include "net/base/net_errors.h"
8 #include "net/base/test_completion_callback.h"
9 #include "net/quic/quic_chromium_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"
15 using testing::AnyNumber
;
16 using testing::Return
;
24 const QuicStreamId kTestStreamId
= 5u;
26 class MockDelegate
: public QuicReliableClientStream::Delegate
{
30 MOCK_METHOD0(OnSendData
, int());
31 MOCK_METHOD2(OnSendDataComplete
, int(int, bool*));
32 MOCK_METHOD1(OnHeadersAvailable
, void(StringPiece
));
33 MOCK_METHOD2(OnDataReceived
, int(const char*, int));
34 MOCK_METHOD1(OnClose
, void(QuicErrorCode
));
35 MOCK_METHOD1(OnError
, void(int));
36 MOCK_METHOD0(HasSendHeadersComplete
, bool());
39 DISALLOW_COPY_AND_ASSIGN(MockDelegate
);
42 class QuicReliableClientStreamTest
43 : public ::testing::TestWithParam
<QuicVersion
> {
45 QuicReliableClientStreamTest()
46 : session_(new MockConnection(Perspective::IS_CLIENT
,
47 SupportedVersions(GetParam()))) {
49 new QuicReliableClientStream(kTestStreamId
, &session_
, BoundNetLog());
50 session_
.ActivateStream(stream_
);
51 stream_
->SetDelegate(&delegate_
);
54 void InitializeHeaders() {
55 headers_
[":host"] = "www.google.com";
56 headers_
[":path"] = "/index.hml";
57 headers_
[":scheme"] = "https";
59 "__utma=208381060.1228362404.1372200928.1372200928.1372200928.1; "
61 "GX=DQAAAOEAAACWJYdewdE9rIrW6qw3PtVi2-d729qaa-74KqOsM1NVQblK4VhX"
62 "hoALMsy6HOdDad2Sz0flUByv7etmo3mLMidGrBoljqO9hSVA40SLqpG_iuKKSHX"
63 "RW3Np4bq0F0SDGDNsW0DSmTS9ufMRrlpARJDS7qAI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EYHsBQ98rXImL8ySDycdLEFvBPdtctPmWCfTxwmoSMLHU2SCVDhbqMWU5b0yr"
81 "JBCScs_ejbKaqBDoB7ZGxTvqlrB__2ZmnHHjCr8RgMRtKNtIeuZAo ";
84 testing::StrictMock
<MockDelegate
> delegate_
;
85 MockQuicSpdySession session_
;
86 QuicReliableClientStream
* stream_
;
87 QuicCryptoClientConfig crypto_config_
;
88 SpdyHeaderBlock headers_
;
91 INSTANTIATE_TEST_CASE_P(Version
, QuicReliableClientStreamTest
,
92 ::testing::ValuesIn(QuicSupportedVersions()));
94 TEST_P(QuicReliableClientStreamTest
, OnFinRead
) {
96 std::string uncompressed_headers
=
97 SpdyUtils::SerializeUncompressedHeaders(headers_
, GetParam());
98 EXPECT_CALL(delegate_
, OnHeadersAvailable(StringPiece(uncompressed_headers
)));
99 QuicStreamOffset offset
= 0;
100 stream_
->OnStreamHeaders(uncompressed_headers
);
101 stream_
->OnStreamHeadersComplete(false, uncompressed_headers
.length());
102 EXPECT_TRUE(stream_
->decompressed_headers().empty());
104 QuicStreamFrame
frame2(kTestStreamId
, true, offset
, StringPiece());
105 EXPECT_CALL(delegate_
, OnClose(QUIC_NO_ERROR
));
106 stream_
->OnStreamFrame(frame2
);
109 TEST_P(QuicReliableClientStreamTest
, ProcessDataBeforeHeaders
) {
110 const char data
[] = "hello world!";
111 EXPECT_CALL(delegate_
, OnClose(QUIC_NO_ERROR
));
113 EXPECT_EQ(0u, stream_
->ProcessData(data
, arraysize(data
)));
116 TEST_P(QuicReliableClientStreamTest
, ProcessData
) {
118 std::string uncompressed_headers
=
119 SpdyUtils::SerializeUncompressedHeaders(headers_
, GetParam());
120 EXPECT_CALL(delegate_
, OnHeadersAvailable(StringPiece(uncompressed_headers
)));
121 stream_
->OnStreamHeaders(uncompressed_headers
);
122 stream_
->OnStreamHeadersComplete(false, uncompressed_headers
.length());
124 const char data
[] = "hello world!";
125 EXPECT_CALL(delegate_
, OnDataReceived(StrEq(data
), arraysize(data
)));
126 EXPECT_CALL(delegate_
, OnClose(QUIC_NO_ERROR
));
128 EXPECT_EQ(arraysize(data
), stream_
->ProcessData(data
, arraysize(data
)));
131 TEST_P(QuicReliableClientStreamTest
, ProcessDataWithError
) {
132 EXPECT_CALL(delegate_
, OnHeadersAvailable(StringPiece("")));
133 stream_
->OnStreamHeadersComplete(false, 0); // Send empty headers.
135 const char data
[] = "hello world!";
136 EXPECT_CALL(delegate_
,
137 OnDataReceived(StrEq(data
),
138 arraysize(data
))).WillOnce(Return(ERR_UNEXPECTED
));
139 EXPECT_CALL(delegate_
, OnClose(QUIC_NO_ERROR
));
142 EXPECT_EQ(0u, stream_
->ProcessData(data
, arraysize(data
)));
145 TEST_P(QuicReliableClientStreamTest
, OnError
) {
146 EXPECT_CALL(delegate_
, OnError(ERR_INTERNET_DISCONNECTED
));
148 stream_
->OnError(ERR_INTERNET_DISCONNECTED
);
149 EXPECT_FALSE(stream_
->GetDelegate());
152 TEST_P(QuicReliableClientStreamTest
, WriteStreamData
) {
153 EXPECT_CALL(delegate_
, OnClose(QUIC_NO_ERROR
));
155 const char kData1
[] = "hello world";
156 const size_t kDataLen
= arraysize(kData1
);
159 EXPECT_CALL(session_
, WritevData(stream_
->id(), _
, _
, _
, _
, _
)).WillOnce(
160 Return(QuicConsumedData(kDataLen
, true)));
161 TestCompletionCallback callback
;
162 EXPECT_EQ(OK
, stream_
->WriteStreamData(base::StringPiece(kData1
, kDataLen
),
163 true, callback
.callback()));
166 TEST_P(QuicReliableClientStreamTest
, WriteStreamDataAsync
) {
167 EXPECT_CALL(delegate_
, HasSendHeadersComplete()).Times(AnyNumber());
168 EXPECT_CALL(delegate_
, OnClose(QUIC_NO_ERROR
));
170 const char kData1
[] = "hello world";
171 const size_t kDataLen
= arraysize(kData1
);
174 EXPECT_CALL(session_
, WritevData(stream_
->id(), _
, _
, _
, _
, _
)).WillOnce(
175 Return(QuicConsumedData(0, false)));
176 TestCompletionCallback callback
;
177 EXPECT_EQ(ERR_IO_PENDING
,
178 stream_
->WriteStreamData(base::StringPiece(kData1
, kDataLen
),
179 true, callback
.callback()));
180 ASSERT_FALSE(callback
.have_result());
183 EXPECT_CALL(session_
, WritevData(stream_
->id(), _
, _
, _
, _
, _
)).WillOnce(
184 Return(QuicConsumedData(kDataLen
, true)));
185 stream_
->OnCanWrite();
186 ASSERT_TRUE(callback
.have_result());
187 EXPECT_EQ(OK
, callback
.WaitForResult());