We started redesigning GpuMemoryBuffer interface to handle multiple buffers [0].
[chromium-blink-merge.git] / net / tools / quic / quic_spdy_server_stream_test.cc
blob79ed30369218238e091c672554e8f0f61836e624
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 "testing/gmock/include/gmock/gmock.h"
18 #include "testing/gtest/include/gtest/gtest.h"
20 using base::StringPiece;
21 using net::test::MockConnection;
22 using net::test::MockSession;
23 using net::test::SupportedVersions;
24 using net::test::kInitialSessionFlowControlWindowForTest;
25 using net::test::kInitialStreamFlowControlWindowForTest;
26 using std::string;
27 using testing::_;
28 using testing::AnyNumber;
29 using testing::Invoke;
30 using testing::InvokeArgument;
31 using testing::InSequence;
32 using testing::Return;
33 using testing::StrictMock;
34 using testing::WithArgs;
36 namespace net {
37 namespace tools {
38 namespace test {
40 class QuicSpdyServerStreamPeer : public QuicSpdyServerStream {
41 public:
42 QuicSpdyServerStreamPeer(QuicStreamId stream_id, QuicSession* session)
43 : 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) {
62 return stream->body_;
65 static const SpdyHeaderBlock& headers(QuicSpdyServerStream* stream) {
66 return stream->request_headers_;
70 namespace {
72 class QuicSpdyServerStreamTest : public ::testing::TestWithParam<QuicVersion> {
73 public:
74 QuicSpdyServerStreamTest()
75 : connection_(
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_ = SpdyUtils::SerializeUncompressedHeaders(request_headers);
89 // New streams rely on having the peer's flow control receive window
90 // negotiated in the config.
91 session_.config()->SetInitialStreamFlowControlWindowToSend(
92 kInitialStreamFlowControlWindowForTest);
93 session_.config()->SetInitialSessionFlowControlWindowToSend(
94 kInitialSessionFlowControlWindowForTest);
95 stream_.reset(new QuicSpdyServerStreamPeer(3, &session_));
97 QuicInMemoryCachePeer::ResetForTests();
99 string host = "";
100 string path = "/foo";
101 SpdyHeaderBlock response_headers;
102 StringPiece body("Yum");
103 QuicInMemoryCache::GetInstance()->AddResponse(host, path, response_headers,
104 body);
107 ~QuicSpdyServerStreamTest() override {
108 QuicInMemoryCachePeer::ResetForTests();
111 const string& StreamBody() {
112 return QuicSpdyServerStreamPeer::body(stream_.get());
115 const string& StreamHeadersValue(const string& key) {
116 return (*stream_->mutable_headers())[key];
119 SpdyHeaderBlock response_headers_;
120 StrictMock<MockConnection>* connection_;
121 StrictMock<MockSession> session_;
122 scoped_ptr<QuicSpdyServerStreamPeer> stream_;
123 string headers_string_;
124 string body_;
127 QuicConsumedData ConsumeAllData(
128 QuicStreamId id,
129 const IOVector& data,
130 QuicStreamOffset offset,
131 bool fin,
132 FecProtection /*fec_protection_*/,
133 QuicAckNotifier::DelegateInterface* /*ack_notifier_delegate*/) {
134 return QuicConsumedData(data.TotalBufferSize(), fin);
137 INSTANTIATE_TEST_CASE_P(Tests, QuicSpdyServerStreamTest,
138 ::testing::ValuesIn(QuicSupportedVersions()));
140 TEST_P(QuicSpdyServerStreamTest, TestFraming) {
141 EXPECT_CALL(session_, WritevData(_, _, _, _, _, _)).Times(AnyNumber()).
142 WillRepeatedly(Invoke(ConsumeAllData));
143 stream_->OnStreamHeaders(headers_string_);
144 stream_->OnStreamHeadersComplete(false, headers_string_.size());
145 EXPECT_EQ(body_.size(), stream_->ProcessData(body_.c_str(), body_.size()));
146 EXPECT_EQ("11", StreamHeadersValue("content-length"));
147 EXPECT_EQ("/", StreamHeadersValue(":path"));
148 EXPECT_EQ("POST", StreamHeadersValue(":method"));
149 EXPECT_EQ(body_, StreamBody());
152 TEST_P(QuicSpdyServerStreamTest, TestFramingOnePacket) {
153 EXPECT_CALL(session_, WritevData(_, _, _, _, _, _)).Times(AnyNumber()).
154 WillRepeatedly(Invoke(ConsumeAllData));
156 stream_->OnStreamHeaders(headers_string_);
157 stream_->OnStreamHeadersComplete(false, headers_string_.size());
158 EXPECT_EQ(body_.size(), stream_->ProcessData(body_.c_str(), body_.size()));
159 EXPECT_EQ("11", StreamHeadersValue("content-length"));
160 EXPECT_EQ("/", StreamHeadersValue(":path"));
161 EXPECT_EQ("POST", StreamHeadersValue(":method"));
162 EXPECT_EQ(body_, StreamBody());
165 TEST_P(QuicSpdyServerStreamTest, TestFramingExtraData) {
166 string large_body = "hello world!!!!!!";
168 // We'll automatically write out an error (headers + body)
169 EXPECT_CALL(session_, WritevData(_, _, _, _, _, _)).Times(AnyNumber()).
170 WillRepeatedly(Invoke(ConsumeAllData));
172 stream_->OnStreamHeaders(headers_string_);
173 stream_->OnStreamHeadersComplete(false, headers_string_.size());
174 EXPECT_EQ(body_.size(), stream_->ProcessData(body_.c_str(), body_.size()));
175 // Content length is still 11. This will register as an error and we won't
176 // accept the bytes.
177 stream_->ProcessData(large_body.c_str(), large_body.size());
178 EXPECT_EQ("11", StreamHeadersValue("content-length"));
179 EXPECT_EQ("/", StreamHeadersValue(":path"));
180 EXPECT_EQ("POST", StreamHeadersValue(":method"));
183 TEST_P(QuicSpdyServerStreamTest, TestSendResponse) {
184 SpdyHeaderBlock* request_headers = stream_->mutable_headers();
185 (*request_headers)[":path"] = "/foo";
186 (*request_headers)[":host"] = "";
187 (*request_headers)[":version"] = "HTTP/1.1";
188 (*request_headers)[":method"] = "GET";
190 response_headers_[":version"] = "HTTP/1.1";
191 response_headers_[":status"] = "200 OK";
192 response_headers_["content-length"] = "3";
194 InSequence s;
195 EXPECT_CALL(session_, WritevData(kHeadersStreamId, _, 0, false, _, nullptr));
196 EXPECT_CALL(session_, WritevData(_, _, _, _, _, _)).Times(1).
197 WillOnce(Return(QuicConsumedData(3, true)));
199 QuicSpdyServerStreamPeer::SendResponse(stream_.get());
200 EXPECT_TRUE(stream_->read_side_closed());
201 EXPECT_TRUE(stream_->write_side_closed());
204 TEST_P(QuicSpdyServerStreamTest, TestSendErrorResponse) {
205 response_headers_[":version"] = "HTTP/1.1";
206 response_headers_[":status"] = "500 Server Error";
207 response_headers_["content-length"] = "3";
209 InSequence s;
210 EXPECT_CALL(session_, WritevData(kHeadersStreamId, _, 0, false, _, nullptr));
211 EXPECT_CALL(session_, WritevData(_, _, _, _, _, _)).Times(1).
212 WillOnce(Return(QuicConsumedData(3, true)));
214 QuicSpdyServerStreamPeer::SendErrorResponse(stream_.get());
215 EXPECT_TRUE(stream_->read_side_closed());
216 EXPECT_TRUE(stream_->write_side_closed());
219 TEST_P(QuicSpdyServerStreamTest, InvalidHeadersWithFin) {
220 char arr[] = {
221 0x3a, 0x68, 0x6f, 0x73, // :hos
222 0x74, 0x00, 0x00, 0x00, // t...
223 0x00, 0x00, 0x00, 0x00, // ....
224 0x07, 0x3a, 0x6d, 0x65, // .:me
225 0x74, 0x68, 0x6f, 0x64, // thod
226 0x00, 0x00, 0x00, 0x03, // ....
227 0x47, 0x45, 0x54, 0x00, // GET.
228 0x00, 0x00, 0x05, 0x3a, // ...:
229 0x70, 0x61, 0x74, 0x68, // path
230 0x00, 0x00, 0x00, 0x04, // ....
231 0x2f, 0x66, 0x6f, 0x6f, // /foo
232 0x00, 0x00, 0x00, 0x07, // ....
233 0x3a, 0x73, 0x63, 0x68, // :sch
234 0x65, 0x6d, 0x65, 0x00, // eme.
235 0x00, 0x00, 0x00, 0x00, // ....
236 0x00, 0x00, 0x08, 0x3a, // ...:
237 0x76, 0x65, 0x72, 0x73, // vers
238 '\x96', 0x6f, 0x6e, 0x00, // <i(69)>on.
239 0x00, 0x00, 0x08, 0x48, // ...H
240 0x54, 0x54, 0x50, 0x2f, // TTP/
241 0x31, 0x2e, 0x31, // 1.1
243 StringPiece data(arr, arraysize(arr));
244 QuicStreamFrame frame(stream_->id(), true, 0, MakeIOVector(data));
245 // Verify that we don't crash when we get a invalid headers in stream frame.
246 stream_->OnStreamFrame(frame);
249 } // namespace
250 } // namespace test
251 } // namespace tools
252 } // namespace net