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 stream_
->OnStreamFrame(
147 QuicStreamFrame(stream_
->id(), /*fin=*/false, /*offset=*/0, body_
));
148 EXPECT_EQ("11", StreamHeadersValue("content-length"));
149 EXPECT_EQ("/", StreamHeadersValue(":path"));
150 EXPECT_EQ("POST", StreamHeadersValue(":method"));
151 EXPECT_EQ(body_
, StreamBody());
154 TEST_P(QuicSpdyServerStreamTest
, TestFramingOnePacket
) {
155 EXPECT_CALL(session_
, WritevData(_
, _
, _
, _
, _
, _
)).Times(AnyNumber()).
156 WillRepeatedly(Invoke(ConsumeAllData
));
158 stream_
->OnStreamHeaders(headers_string_
);
159 stream_
->OnStreamHeadersComplete(false, headers_string_
.size());
160 stream_
->OnStreamFrame(
161 QuicStreamFrame(stream_
->id(), /*fin=*/false, /*offset=*/0, body_
));
162 EXPECT_EQ("11", StreamHeadersValue("content-length"));
163 EXPECT_EQ("/", StreamHeadersValue(":path"));
164 EXPECT_EQ("POST", StreamHeadersValue(":method"));
165 EXPECT_EQ(body_
, StreamBody());
168 TEST_P(QuicSpdyServerStreamTest
, TestFramingExtraData
) {
169 string large_body
= "hello world!!!!!!";
171 // We'll automatically write out an error (headers + body)
172 EXPECT_CALL(session_
, WritevData(_
, _
, _
, _
, _
, _
)).Times(AnyNumber()).
173 WillRepeatedly(Invoke(ConsumeAllData
));
175 stream_
->OnStreamHeaders(headers_string_
);
176 stream_
->OnStreamHeadersComplete(false, headers_string_
.size());
177 stream_
->OnStreamFrame(
178 QuicStreamFrame(stream_
->id(), /*fin=*/false, /*offset=*/0, body_
));
179 // Content length is still 11. This will register as an error and we won't
181 stream_
->OnStreamFrame(
182 QuicStreamFrame(stream_
->id(), /*fin=*/false, body_
.size(), large_body
));
183 EXPECT_EQ("11", StreamHeadersValue("content-length"));
184 EXPECT_EQ("/", StreamHeadersValue(":path"));
185 EXPECT_EQ("POST", StreamHeadersValue(":method"));
188 TEST_P(QuicSpdyServerStreamTest
, TestSendResponse
) {
189 SpdyHeaderBlock
* request_headers
= stream_
->mutable_headers();
190 (*request_headers
)[":path"] = "/foo";
191 (*request_headers
)[":host"] = "";
192 (*request_headers
)[":version"] = "HTTP/1.1";
193 (*request_headers
)[":method"] = "GET";
195 response_headers_
[":version"] = "HTTP/1.1";
196 response_headers_
[":status"] = "200 OK";
197 response_headers_
["content-length"] = "3";
200 EXPECT_CALL(session_
, WritevData(kHeadersStreamId
, _
, 0, false, _
, nullptr));
201 EXPECT_CALL(session_
, WritevData(_
, _
, _
, _
, _
, _
)).Times(1).
202 WillOnce(Return(QuicConsumedData(3, true)));
204 QuicSpdyServerStreamPeer::SendResponse(stream_
.get());
205 EXPECT_TRUE(stream_
->read_side_closed());
206 EXPECT_TRUE(stream_
->write_side_closed());
209 TEST_P(QuicSpdyServerStreamTest
, TestSendErrorResponse
) {
210 response_headers_
[":version"] = "HTTP/1.1";
211 response_headers_
[":status"] = "500 Server Error";
212 response_headers_
["content-length"] = "3";
215 EXPECT_CALL(session_
, WritevData(kHeadersStreamId
, _
, 0, false, _
, nullptr));
216 EXPECT_CALL(session_
, WritevData(_
, _
, _
, _
, _
, _
)).Times(1).
217 WillOnce(Return(QuicConsumedData(3, true)));
219 QuicSpdyServerStreamPeer::SendErrorResponse(stream_
.get());
220 EXPECT_TRUE(stream_
->read_side_closed());
221 EXPECT_TRUE(stream_
->write_side_closed());
224 TEST_P(QuicSpdyServerStreamTest
, InvalidHeadersWithFin
) {
226 0x3a, 0x68, 0x6f, 0x73, // :hos
227 0x74, 0x00, 0x00, 0x00, // t...
228 0x00, 0x00, 0x00, 0x00, // ....
229 0x07, 0x3a, 0x6d, 0x65, // .:me
230 0x74, 0x68, 0x6f, 0x64, // thod
231 0x00, 0x00, 0x00, 0x03, // ....
232 0x47, 0x45, 0x54, 0x00, // GET.
233 0x00, 0x00, 0x05, 0x3a, // ...:
234 0x70, 0x61, 0x74, 0x68, // path
235 0x00, 0x00, 0x00, 0x04, // ....
236 0x2f, 0x66, 0x6f, 0x6f, // /foo
237 0x00, 0x00, 0x00, 0x07, // ....
238 0x3a, 0x73, 0x63, 0x68, // :sch
239 0x65, 0x6d, 0x65, 0x00, // eme.
240 0x00, 0x00, 0x00, 0x00, // ....
241 0x00, 0x00, 0x08, 0x3a, // ...:
242 0x76, 0x65, 0x72, 0x73, // vers
243 '\x96', 0x6f, 0x6e, 0x00, // <i(69)>on.
244 0x00, 0x00, 0x08, 0x48, // ...H
245 0x54, 0x54, 0x50, 0x2f, // TTP/
246 0x31, 0x2e, 0x31, // 1.1
248 StringPiece
data(arr
, arraysize(arr
));
249 QuicStreamFrame
frame(stream_
->id(), true, 0, data
);
250 // Verify that we don't crash when we get a invalid headers in stream frame.
251 stream_
->OnStreamFrame(frame
);