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/test_tools/quic_test_utils.h"
13 #include "net/tools/epoll_server/epoll_server.h"
14 #include "net/tools/quic/quic_in_memory_cache.h"
15 #include "net/tools/quic/spdy_utils.h"
16 #include "net/tools/quic/test_tools/quic_in_memory_cache_peer.h"
17 #include "net/tools/quic/test_tools/quic_test_utils.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::MockSession;
23 using net::test::SupportedVersions;
24 using net::tools::test::MockConnection;
27 using testing::AnyNumber;
28 using testing::Invoke;
29 using testing::InvokeArgument;
30 using testing::InSequence;
31 using testing::Return;
32 using testing::StrictMock;
33 using testing::WithArgs;
39 class QuicSpdyServerStreamPeer : public QuicSpdyServerStream {
41 QuicSpdyServerStreamPeer(QuicStreamId stream_id, QuicSession* session)
42 : QuicSpdyServerStream(stream_id, session) {
45 using QuicSpdyServerStream::SendResponse;
46 using QuicSpdyServerStream::SendErrorResponse;
48 BalsaHeaders* mutable_headers() {
52 static void SendResponse(QuicSpdyServerStream* stream) {
53 stream->SendResponse();
56 static void SendErrorResponse(QuicSpdyServerStream* stream) {
57 stream->SendResponse();
60 static const string& body(QuicSpdyServerStream* stream) {
64 static const BalsaHeaders& headers(QuicSpdyServerStream* stream) {
65 return stream->headers_;
71 class QuicSpdyServerStreamTest : public ::testing::TestWithParam<QuicVersion> {
73 QuicSpdyServerStreamTest()
74 : connection_(new StrictMock<MockConnection>(
75 true, SupportedVersions(GetParam()))),
76 session_(connection_),
77 body_("hello world") {
78 BalsaHeaders request_headers;
79 request_headers.SetRequestFirstlineFromStringPieces(
80 "POST", "https://www.google.com/", "HTTP/1.1");
81 request_headers.ReplaceOrAppendHeader("content-length", "11");
83 headers_string_ = SpdyUtils::SerializeRequestHeaders(request_headers);
85 // New streams rely on having the peer's flow control receive window
86 // negotiated in the config.
87 const uint32 kInitialWindow = 10 * kMaxPacketSize;
88 session_.config()->SetInitialFlowControlWindowToSend(
90 stream_.reset(new QuicSpdyServerStreamPeer(3, &session_));
93 static void SetUpTestCase() {
94 QuicInMemoryCachePeer::ResetForTests();
97 virtual void SetUp() {
98 QuicInMemoryCache* cache = QuicInMemoryCache::GetInstance();
100 BalsaHeaders request_headers, response_headers;
101 StringPiece body("Yum");
102 request_headers.SetRequestFirstlineFromStringPieces(
104 "https://www.google.com/foo",
106 response_headers.SetRequestFirstlineFromStringPieces("HTTP/1.1",
109 response_headers.AppendHeader("content-length",
110 base::IntToString(body.length()));
112 // Check if response already exists and matches.
113 const QuicInMemoryCache::Response* cached_response =
114 cache->GetResponse(request_headers);
115 if (cached_response != NULL) {
116 string cached_response_headers_str, response_headers_str;
117 cached_response->headers().DumpToString(&cached_response_headers_str);
118 response_headers.DumpToString(&response_headers_str);
119 CHECK_EQ(cached_response_headers_str, response_headers_str);
120 CHECK_EQ(cached_response->body(), body);
124 cache->AddResponse(request_headers, response_headers, body);
127 const string& StreamBody() {
128 return QuicSpdyServerStreamPeer::body(stream_.get());
131 const BalsaHeaders& StreamHeaders() {
132 return QuicSpdyServerStreamPeer::headers(stream_.get());
135 BalsaHeaders response_headers_;
137 StrictMock<MockConnection>* connection_;
138 StrictMock<MockSession> session_;
139 scoped_ptr<QuicSpdyServerStreamPeer> stream_;
140 string headers_string_;
144 QuicConsumedData ConsumeAllData(
146 const IOVector& data,
147 QuicStreamOffset offset,
149 QuicAckNotifier::DelegateInterface* /*ack_notifier_delegate*/) {
150 return QuicConsumedData(data.TotalBufferSize(), fin);
153 INSTANTIATE_TEST_CASE_P(Tests, QuicSpdyServerStreamTest,
154 ::testing::ValuesIn(QuicSupportedVersions()));
156 TEST_P(QuicSpdyServerStreamTest, TestFraming) {
157 EXPECT_CALL(session_, WritevData(_, _, _, _, _)).Times(AnyNumber()).
158 WillRepeatedly(Invoke(ConsumeAllData));
160 EXPECT_EQ(headers_string_.size(), stream_->ProcessData(
161 headers_string_.c_str(), headers_string_.size()));
162 EXPECT_EQ(body_.size(), stream_->ProcessData(body_.c_str(), body_.size()));
163 EXPECT_EQ(11u, StreamHeaders().content_length());
164 EXPECT_EQ("https://www.google.com/", StreamHeaders().request_uri());
165 EXPECT_EQ("POST", StreamHeaders().request_method());
166 EXPECT_EQ(body_, StreamBody());
169 TEST_P(QuicSpdyServerStreamTest, TestFramingOnePacket) {
170 EXPECT_CALL(session_, WritevData(_, _, _, _, _)).Times(AnyNumber()).
171 WillRepeatedly(Invoke(ConsumeAllData));
173 string message = headers_string_ + body_;
175 EXPECT_EQ(message.size(), stream_->ProcessData(
176 message.c_str(), message.size()));
177 EXPECT_EQ(11u, StreamHeaders().content_length());
178 EXPECT_EQ("https://www.google.com/", StreamHeaders().request_uri());
179 EXPECT_EQ("POST", StreamHeaders().request_method());
180 EXPECT_EQ(body_, StreamBody());
183 TEST_P(QuicSpdyServerStreamTest, TestFramingExtraData) {
184 string large_body = "hello world!!!!!!";
186 // We'll automatically write out an error (headers + body)
187 EXPECT_CALL(session_, WritevData(_, _, _, _, _)).Times(AnyNumber()).
188 WillRepeatedly(Invoke(ConsumeAllData));
190 EXPECT_EQ(headers_string_.size(), stream_->ProcessData(
191 headers_string_.c_str(), headers_string_.size()));
192 // Content length is still 11. This will register as an error and we won't
194 stream_->ProcessData(large_body.c_str(), large_body.size());
195 EXPECT_EQ(11u, StreamHeaders().content_length());
196 EXPECT_EQ("https://www.google.com/", StreamHeaders().request_uri());
197 EXPECT_EQ("POST", StreamHeaders().request_method());
200 TEST_P(QuicSpdyServerStreamTest, TestSendResponse) {
201 BalsaHeaders* request_headers = stream_->mutable_headers();
202 request_headers->SetRequestFirstlineFromStringPieces(
204 "https://www.google.com/foo",
207 response_headers_.SetResponseFirstlineFromStringPieces(
208 "HTTP/1.1", "200", "OK");
209 response_headers_.ReplaceOrAppendHeader("content-length", "3");
212 EXPECT_CALL(session_,
213 WritevData(kHeadersStreamId, _, 0, false, NULL));
216 EXPECT_CALL(session_, WritevData(_, _, _, _, _)).Times(1).
217 WillOnce(Return(QuicConsumedData(3, true)));
219 QuicSpdyServerStreamPeer::SendResponse(stream_.get());
220 EXPECT_TRUE(stream_->read_side_closed());
221 EXPECT_TRUE(stream_->write_side_closed());
224 TEST_P(QuicSpdyServerStreamTest, TestSendErrorResponse) {
225 response_headers_.SetResponseFirstlineFromStringPieces(
226 "HTTP/1.1", "500", "Server Error");
227 response_headers_.ReplaceOrAppendHeader("content-length", "3");
230 EXPECT_CALL(session_,
231 WritevData(kHeadersStreamId, _, 0, false, NULL));
233 EXPECT_CALL(session_, WritevData(_, _, _, _, _)).Times(1).
234 WillOnce(Return(QuicConsumedData(3, true)));
236 QuicSpdyServerStreamPeer::SendErrorResponse(stream_.get());
237 EXPECT_TRUE(stream_->read_side_closed());
238 EXPECT_TRUE(stream_->write_side_closed());
241 TEST_P(QuicSpdyServerStreamTest, InvalidHeadersWithFin) {
243 0x3a, 0x68, 0x6f, 0x73, // :hos
244 0x74, 0x00, 0x00, 0x00, // t...
245 0x00, 0x00, 0x00, 0x00, // ....
246 0x07, 0x3a, 0x6d, 0x65, // .:me
247 0x74, 0x68, 0x6f, 0x64, // thod
248 0x00, 0x00, 0x00, 0x03, // ....
249 0x47, 0x45, 0x54, 0x00, // GET.
250 0x00, 0x00, 0x05, 0x3a, // ...:
251 0x70, 0x61, 0x74, 0x68, // path
252 0x00, 0x00, 0x00, 0x04, // ....
253 0x2f, 0x66, 0x6f, 0x6f, // /foo
254 0x00, 0x00, 0x00, 0x07, // ....
255 0x3a, 0x73, 0x63, 0x68, // :sch
256 0x65, 0x6d, 0x65, 0x00, // eme.
257 0x00, 0x00, 0x00, 0x00, // ....
258 0x00, 0x00, 0x08, 0x3a, // ...:
259 0x76, 0x65, 0x72, 0x73, // vers
260 '\x96', 0x6f, 0x6e, 0x00, // <i(69)>on.
261 0x00, 0x00, 0x08, 0x48, // ...H
262 0x54, 0x54, 0x50, 0x2f, // TTP/
263 0x31, 0x2e, 0x31, // 1.1
265 StringPiece data(arr, arraysize(arr));
266 QuicStreamFrame frame(stream_->id(), true, 0, MakeIOVector(data));
267 // Verify that we don't crash when we get a invalid headers in stream frame.
268 stream_->OnStreamFrame(frame);