1 // Copyright (c) 2013 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/tools/quic/quic_spdy_server_stream.h"
7 #include "base/strings/string_number_conversions.h"
8 #include "base/strings/string_piece.h"
9 #include "net/quic/quic_connection.h"
10 #include "net/quic/quic_protocol.h"
11 #include "net/quic/quic_utils.h"
12 #include "net/quic/spdy_utils.h"
13 #include "net/quic/test_tools/quic_test_utils.h"
14 #include "net/tools/epoll_server/epoll_server.h"
15 #include "net/tools/quic/quic_in_memory_cache.h"
16 #include "net/tools/quic/spdy_balsa_utils.h"
17 #include "net/tools/quic/test_tools/quic_in_memory_cache_peer.h"
18 #include "testing/gmock/include/gmock/gmock.h"
19 #include "testing/gtest/include/gtest/gtest.h"
21 using base::StringPiece
;
22 using net::test::MockConnection
;
23 using net::test::MockQuicSpdySession
;
24 using net::test::SupportedVersions
;
25 using net::test::kInitialSessionFlowControlWindowForTest
;
26 using net::test::kInitialStreamFlowControlWindowForTest
;
29 using testing::AnyNumber
;
30 using testing::Invoke
;
31 using testing::InvokeArgument
;
32 using testing::InSequence
;
33 using testing::Return
;
34 using testing::StrictMock
;
35 using testing::WithArgs
;
41 class QuicSpdyServerStreamPeer
: public QuicSpdyServerStream
{
43 QuicSpdyServerStreamPeer(QuicStreamId stream_id
, QuicSpdySession
* session
)
44 : QuicSpdyServerStream(stream_id
, session
) {}
46 using QuicSpdyServerStream::SendResponse
;
47 using QuicSpdyServerStream::SendErrorResponse
;
49 SpdyHeaderBlock
* mutable_headers() {
50 return &request_headers_
;
53 static void SendResponse(QuicSpdyServerStream
* stream
) {
54 stream
->SendResponse();
57 static void SendErrorResponse(QuicSpdyServerStream
* stream
) {
58 stream
->SendResponse();
61 static const string
& body(QuicSpdyServerStream
* stream
) {
65 static const SpdyHeaderBlock
& headers(QuicSpdyServerStream
* stream
) {
66 return stream
->request_headers_
;
72 class QuicSpdyServerStreamTest
: public ::testing::TestWithParam
<QuicVersion
> {
74 QuicSpdyServerStreamTest()
76 new StrictMock
<MockConnection
>(Perspective::IS_SERVER
,
77 SupportedVersions(GetParam()))),
78 session_(connection_
),
79 body_("hello world") {
80 SpdyHeaderBlock request_headers
;
81 request_headers
[":host"] = "";
82 request_headers
[":path"] = "/";
83 request_headers
[":method"] = "POST";
84 request_headers
[":version"] = "HTTP/1.1";
85 request_headers
["content-length"] = "11";
87 headers_string_
= net::SpdyUtils::SerializeUncompressedHeaders(
88 request_headers
, GetParam());
90 // New streams rely on having the peer's flow control receive window
91 // negotiated in the config.
92 session_
.config()->SetInitialStreamFlowControlWindowToSend(
93 kInitialStreamFlowControlWindowForTest
);
94 session_
.config()->SetInitialSessionFlowControlWindowToSend(
95 kInitialSessionFlowControlWindowForTest
);
96 stream_
.reset(new QuicSpdyServerStreamPeer(3, &session_
));
98 QuicInMemoryCachePeer::ResetForTests();
101 string path
= "/foo";
102 SpdyHeaderBlock response_headers
;
103 StringPiece
body("Yum");
104 QuicInMemoryCache::GetInstance()->AddResponse(host
, path
, response_headers
,
108 ~QuicSpdyServerStreamTest() override
{
109 QuicInMemoryCachePeer::ResetForTests();
112 const string
& StreamBody() {
113 return QuicSpdyServerStreamPeer::body(stream_
.get());
116 const string
& StreamHeadersValue(const string
& key
) {
117 return (*stream_
->mutable_headers())[key
];
120 SpdyHeaderBlock response_headers_
;
121 StrictMock
<MockConnection
>* connection_
;
122 StrictMock
<MockQuicSpdySession
> session_
;
123 scoped_ptr
<QuicSpdyServerStreamPeer
> stream_
;
124 string headers_string_
;
128 QuicConsumedData
ConsumeAllData(
130 const QuicIOVector
& data
,
131 QuicStreamOffset offset
,
133 FecProtection
/*fec_protection_*/,
134 QuicAckNotifier::DelegateInterface
* /*ack_notifier_delegate*/) {
135 return QuicConsumedData(data
.total_length
, fin
);
138 INSTANTIATE_TEST_CASE_P(Tests
, QuicSpdyServerStreamTest
,
139 ::testing::ValuesIn(QuicSupportedVersions()));
141 TEST_P(QuicSpdyServerStreamTest
, TestFraming
) {
142 EXPECT_CALL(session_
, WritevData(_
, _
, _
, _
, _
, _
)).Times(AnyNumber()).
143 WillRepeatedly(Invoke(ConsumeAllData
));
144 stream_
->OnStreamHeaders(headers_string_
);
145 stream_
->OnStreamHeadersComplete(false, headers_string_
.size());
146 EXPECT_EQ(body_
.size(), stream_
->ProcessData(body_
.c_str(), body_
.size()));
147 EXPECT_EQ("11", StreamHeadersValue("content-length"));
148 EXPECT_EQ("/", StreamHeadersValue(":path"));
149 EXPECT_EQ("POST", StreamHeadersValue(":method"));
150 EXPECT_EQ(body_
, StreamBody());
153 TEST_P(QuicSpdyServerStreamTest
, TestFramingOnePacket
) {
154 EXPECT_CALL(session_
, WritevData(_
, _
, _
, _
, _
, _
)).Times(AnyNumber()).
155 WillRepeatedly(Invoke(ConsumeAllData
));
157 stream_
->OnStreamHeaders(headers_string_
);
158 stream_
->OnStreamHeadersComplete(false, headers_string_
.size());
159 EXPECT_EQ(body_
.size(), stream_
->ProcessData(body_
.c_str(), body_
.size()));
160 EXPECT_EQ("11", StreamHeadersValue("content-length"));
161 EXPECT_EQ("/", StreamHeadersValue(":path"));
162 EXPECT_EQ("POST", StreamHeadersValue(":method"));
163 EXPECT_EQ(body_
, StreamBody());
166 TEST_P(QuicSpdyServerStreamTest
, TestFramingExtraData
) {
167 string large_body
= "hello world!!!!!!";
169 // We'll automatically write out an error (headers + body)
170 EXPECT_CALL(session_
, WritevData(_
, _
, _
, _
, _
, _
)).Times(AnyNumber()).
171 WillRepeatedly(Invoke(ConsumeAllData
));
173 stream_
->OnStreamHeaders(headers_string_
);
174 stream_
->OnStreamHeadersComplete(false, headers_string_
.size());
175 EXPECT_EQ(body_
.size(), stream_
->ProcessData(body_
.c_str(), body_
.size()));
176 // Content length is still 11. This will register as an error and we won't
178 stream_
->ProcessData(large_body
.c_str(), large_body
.size());
179 EXPECT_EQ("11", StreamHeadersValue("content-length"));
180 EXPECT_EQ("/", StreamHeadersValue(":path"));
181 EXPECT_EQ("POST", StreamHeadersValue(":method"));
184 TEST_P(QuicSpdyServerStreamTest
, TestSendResponse
) {
185 SpdyHeaderBlock
* request_headers
= stream_
->mutable_headers();
186 (*request_headers
)[":path"] = "/foo";
187 (*request_headers
)[":host"] = "";
188 (*request_headers
)[":version"] = "HTTP/1.1";
189 (*request_headers
)[":method"] = "GET";
191 response_headers_
[":version"] = "HTTP/1.1";
192 response_headers_
[":status"] = "200 OK";
193 response_headers_
["content-length"] = "3";
196 EXPECT_CALL(session_
, WritevData(kHeadersStreamId
, _
, 0, false, _
, nullptr));
197 EXPECT_CALL(session_
, WritevData(_
, _
, _
, _
, _
, _
)).Times(1).
198 WillOnce(Return(QuicConsumedData(3, true)));
200 QuicSpdyServerStreamPeer::SendResponse(stream_
.get());
201 EXPECT_TRUE(stream_
->read_side_closed());
202 EXPECT_TRUE(stream_
->write_side_closed());
205 TEST_P(QuicSpdyServerStreamTest
, TestSendErrorResponse
) {
206 response_headers_
[":version"] = "HTTP/1.1";
207 response_headers_
[":status"] = "500 Server Error";
208 response_headers_
["content-length"] = "3";
211 EXPECT_CALL(session_
, WritevData(kHeadersStreamId
, _
, 0, false, _
, nullptr));
212 EXPECT_CALL(session_
, WritevData(_
, _
, _
, _
, _
, _
)).Times(1).
213 WillOnce(Return(QuicConsumedData(3, true)));
215 QuicSpdyServerStreamPeer::SendErrorResponse(stream_
.get());
216 EXPECT_TRUE(stream_
->read_side_closed());
217 EXPECT_TRUE(stream_
->write_side_closed());
220 TEST_P(QuicSpdyServerStreamTest
, InvalidHeadersWithFin
) {
222 0x3a, 0x68, 0x6f, 0x73, // :hos
223 0x74, 0x00, 0x00, 0x00, // t...
224 0x00, 0x00, 0x00, 0x00, // ....
225 0x07, 0x3a, 0x6d, 0x65, // .:me
226 0x74, 0x68, 0x6f, 0x64, // thod
227 0x00, 0x00, 0x00, 0x03, // ....
228 0x47, 0x45, 0x54, 0x00, // GET.
229 0x00, 0x00, 0x05, 0x3a, // ...:
230 0x70, 0x61, 0x74, 0x68, // path
231 0x00, 0x00, 0x00, 0x04, // ....
232 0x2f, 0x66, 0x6f, 0x6f, // /foo
233 0x00, 0x00, 0x00, 0x07, // ....
234 0x3a, 0x73, 0x63, 0x68, // :sch
235 0x65, 0x6d, 0x65, 0x00, // eme.
236 0x00, 0x00, 0x00, 0x00, // ....
237 0x00, 0x00, 0x08, 0x3a, // ...:
238 0x76, 0x65, 0x72, 0x73, // vers
239 '\x96', 0x6f, 0x6e, 0x00, // <i(69)>on.
240 0x00, 0x00, 0x08, 0x48, // ...H
241 0x54, 0x54, 0x50, 0x2f, // TTP/
242 0x31, 0x2e, 0x31, // 1.1
244 StringPiece
data(arr
, arraysize(arr
));
245 QuicStreamFrame
frame(stream_
->id(), true, 0, data
);
246 // Verify that we don't crash when we get a invalid headers in stream frame.
247 stream_
->OnStreamFrame(frame
);