Add more checks to investigate SupervisedUserPrefStore crash at startup.
[chromium-blink-merge.git] / net / tools / quic / quic_spdy_server_stream_test.cc
blob8681735890fb663aa7407687d6fb9cabab892cb4
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;
25 using std::string;
26 using testing::_;
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;
35 namespace net {
36 namespace tools {
37 namespace test {
39 class QuicSpdyServerStreamPeer : public QuicSpdyServerStream {
40 public:
41 QuicSpdyServerStreamPeer(QuicStreamId stream_id, QuicSession* session)
42 : QuicSpdyServerStream(stream_id, session) {
45 using QuicSpdyServerStream::SendResponse;
46 using QuicSpdyServerStream::SendErrorResponse;
48 BalsaHeaders* mutable_headers() {
49 return &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) {
61 return stream->body_;
64 static const BalsaHeaders& headers(QuicSpdyServerStream* stream) {
65 return stream->headers_;
69 namespace {
71 class QuicSpdyServerStreamTest : public ::testing::TestWithParam<QuicVersion> {
72 public:
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 session_.config()->SetInitialStreamFlowControlWindowToSend(
88 kInitialStreamFlowControlWindowForTest);
89 session_.config()->SetInitialSessionFlowControlWindowToSend(
90 kInitialSessionFlowControlWindowForTest);
91 stream_.reset(new QuicSpdyServerStreamPeer(3, &session_));
94 static void SetUpTestCase() {
95 QuicInMemoryCachePeer::ResetForTests();
98 void SetUp() override {
99 QuicInMemoryCache* cache = QuicInMemoryCache::GetInstance();
101 BalsaHeaders request_headers, response_headers;
102 StringPiece body("Yum");
103 request_headers.SetRequestFirstlineFromStringPieces(
104 "GET",
105 "https://www.google.com/foo",
106 "HTTP/1.1");
107 response_headers.SetRequestFirstlineFromStringPieces("HTTP/1.1",
108 "200",
109 "OK");
110 response_headers.AppendHeader("content-length",
111 base::IntToString(body.length()));
113 // Check if response already exists and matches.
114 const QuicInMemoryCache::Response* cached_response =
115 cache->GetResponse(request_headers);
116 if (cached_response != nullptr) {
117 string cached_response_headers_str, response_headers_str;
118 cached_response->headers().DumpToString(&cached_response_headers_str);
119 response_headers.DumpToString(&response_headers_str);
120 CHECK_EQ(cached_response_headers_str, response_headers_str);
121 CHECK_EQ(cached_response->body(), body);
122 return;
125 cache->AddResponse(request_headers, response_headers, body);
128 const string& StreamBody() {
129 return QuicSpdyServerStreamPeer::body(stream_.get());
132 const BalsaHeaders& StreamHeaders() {
133 return QuicSpdyServerStreamPeer::headers(stream_.get());
136 BalsaHeaders response_headers_;
137 EpollServer eps_;
138 StrictMock<MockConnection>* connection_;
139 StrictMock<MockSession> session_;
140 scoped_ptr<QuicSpdyServerStreamPeer> stream_;
141 string headers_string_;
142 string body_;
145 QuicConsumedData ConsumeAllData(
146 QuicStreamId id,
147 const IOVector& data,
148 QuicStreamOffset offset,
149 bool fin,
150 FecProtection /*fec_protection_*/,
151 QuicAckNotifier::DelegateInterface* /*ack_notifier_delegate*/) {
152 return QuicConsumedData(data.TotalBufferSize(), fin);
155 INSTANTIATE_TEST_CASE_P(Tests, QuicSpdyServerStreamTest,
156 ::testing::ValuesIn(QuicSupportedVersions()));
158 TEST_P(QuicSpdyServerStreamTest, TestFraming) {
159 EXPECT_CALL(session_, WritevData(_, _, _, _, _, _)).Times(AnyNumber()).
160 WillRepeatedly(Invoke(ConsumeAllData));
162 EXPECT_EQ(headers_string_.size(), stream_->ProcessData(
163 headers_string_.c_str(), headers_string_.size()));
164 EXPECT_EQ(body_.size(), stream_->ProcessData(body_.c_str(), body_.size()));
165 EXPECT_EQ(11u, StreamHeaders().content_length());
166 EXPECT_EQ("https://www.google.com/", StreamHeaders().request_uri());
167 EXPECT_EQ("POST", StreamHeaders().request_method());
168 EXPECT_EQ(body_, StreamBody());
171 TEST_P(QuicSpdyServerStreamTest, TestFramingOnePacket) {
172 EXPECT_CALL(session_, WritevData(_, _, _, _, _, _)).Times(AnyNumber()).
173 WillRepeatedly(Invoke(ConsumeAllData));
175 string message = headers_string_ + body_;
177 EXPECT_EQ(message.size(), stream_->ProcessData(
178 message.c_str(), message.size()));
179 EXPECT_EQ(11u, StreamHeaders().content_length());
180 EXPECT_EQ("https://www.google.com/", StreamHeaders().request_uri());
181 EXPECT_EQ("POST", StreamHeaders().request_method());
182 EXPECT_EQ(body_, StreamBody());
185 TEST_P(QuicSpdyServerStreamTest, TestFramingExtraData) {
186 string large_body = "hello world!!!!!!";
188 // We'll automatically write out an error (headers + body)
189 EXPECT_CALL(session_, WritevData(_, _, _, _, _, _)).Times(AnyNumber()).
190 WillRepeatedly(Invoke(ConsumeAllData));
192 EXPECT_EQ(headers_string_.size(), stream_->ProcessData(
193 headers_string_.c_str(), headers_string_.size()));
194 // Content length is still 11. This will register as an error and we won't
195 // accept the bytes.
196 stream_->ProcessData(large_body.c_str(), large_body.size());
197 EXPECT_EQ(11u, StreamHeaders().content_length());
198 EXPECT_EQ("https://www.google.com/", StreamHeaders().request_uri());
199 EXPECT_EQ("POST", StreamHeaders().request_method());
202 TEST_P(QuicSpdyServerStreamTest, TestSendResponse) {
203 BalsaHeaders* request_headers = stream_->mutable_headers();
204 request_headers->SetRequestFirstlineFromStringPieces(
205 "GET",
206 "https://www.google.com/foo",
207 "HTTP/1.1");
209 response_headers_.SetResponseFirstlineFromStringPieces(
210 "HTTP/1.1", "200", "OK");
211 response_headers_.ReplaceOrAppendHeader("content-length", "3");
213 InSequence s;
214 EXPECT_CALL(session_, WritevData(kHeadersStreamId, _, 0, false, _, nullptr));
215 EXPECT_CALL(session_, WritevData(_, _, _, _, _, _)).Times(1).
216 WillOnce(Return(QuicConsumedData(3, true)));
218 QuicSpdyServerStreamPeer::SendResponse(stream_.get());
219 EXPECT_TRUE(stream_->read_side_closed());
220 EXPECT_TRUE(stream_->write_side_closed());
223 TEST_P(QuicSpdyServerStreamTest, TestSendErrorResponse) {
224 response_headers_.SetResponseFirstlineFromStringPieces(
225 "HTTP/1.1", "500", "Server Error");
226 response_headers_.ReplaceOrAppendHeader("content-length", "3");
228 InSequence s;
229 EXPECT_CALL(session_, WritevData(kHeadersStreamId, _, 0, false, _, nullptr));
230 EXPECT_CALL(session_, WritevData(_, _, _, _, _, _)).Times(1).
231 WillOnce(Return(QuicConsumedData(3, true)));
233 QuicSpdyServerStreamPeer::SendErrorResponse(stream_.get());
234 EXPECT_TRUE(stream_->read_side_closed());
235 EXPECT_TRUE(stream_->write_side_closed());
238 TEST_P(QuicSpdyServerStreamTest, InvalidHeadersWithFin) {
239 char arr[] = {
240 0x3a, 0x68, 0x6f, 0x73, // :hos
241 0x74, 0x00, 0x00, 0x00, // t...
242 0x00, 0x00, 0x00, 0x00, // ....
243 0x07, 0x3a, 0x6d, 0x65, // .:me
244 0x74, 0x68, 0x6f, 0x64, // thod
245 0x00, 0x00, 0x00, 0x03, // ....
246 0x47, 0x45, 0x54, 0x00, // GET.
247 0x00, 0x00, 0x05, 0x3a, // ...:
248 0x70, 0x61, 0x74, 0x68, // path
249 0x00, 0x00, 0x00, 0x04, // ....
250 0x2f, 0x66, 0x6f, 0x6f, // /foo
251 0x00, 0x00, 0x00, 0x07, // ....
252 0x3a, 0x73, 0x63, 0x68, // :sch
253 0x65, 0x6d, 0x65, 0x00, // eme.
254 0x00, 0x00, 0x00, 0x00, // ....
255 0x00, 0x00, 0x08, 0x3a, // ...:
256 0x76, 0x65, 0x72, 0x73, // vers
257 '\x96', 0x6f, 0x6e, 0x00, // <i(69)>on.
258 0x00, 0x00, 0x08, 0x48, // ...H
259 0x54, 0x54, 0x50, 0x2f, // TTP/
260 0x31, 0x2e, 0x31, // 1.1
262 StringPiece data(arr, arraysize(arr));
263 QuicStreamFrame frame(stream_->id(), true, 0, MakeIOVector(data));
264 // Verify that we don't crash when we get a invalid headers in stream frame.
265 stream_->OnStreamFrame(frame);
268 } // namespace
269 } // namespace test
270 } // namespace tools
271 } // namespace net