Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / net / tools / quic / quic_spdy_server_stream_test.cc
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.
4
5 #include "net/tools/quic/quic_spdy_server_stream.h"
6
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"
20
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;
34
35 namespace net {
36 namespace tools {
37 namespace test {
38
39 class QuicSpdyServerStreamPeer : public QuicSpdyServerStream {
40  public:
41   QuicSpdyServerStreamPeer(QuicStreamId stream_id, QuicSession* session)
42       : QuicSpdyServerStream(stream_id, session) {
43   }
44
45   using QuicSpdyServerStream::SendResponse;
46   using QuicSpdyServerStream::SendErrorResponse;
47
48   BalsaHeaders* mutable_headers() {
49     return &headers_;
50   }
51
52   static void SendResponse(QuicSpdyServerStream* stream) {
53     stream->SendResponse();
54   }
55
56   static void SendErrorResponse(QuicSpdyServerStream* stream) {
57     stream->SendResponse();
58   }
59
60   static const string& body(QuicSpdyServerStream* stream) {
61     return stream->body_;
62   }
63
64   static const BalsaHeaders& headers(QuicSpdyServerStream* stream) {
65     return stream->headers_;
66   }
67 };
68
69 namespace {
70
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");
82
83     headers_string_ = SpdyUtils::SerializeRequestHeaders(request_headers);
84
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(
89         kInitialWindow);
90     stream_.reset(new QuicSpdyServerStreamPeer(3, &session_));
91   }
92
93   static void SetUpTestCase() {
94     QuicInMemoryCachePeer::ResetForTests();
95   }
96
97   virtual void SetUp() {
98     QuicInMemoryCache* cache = QuicInMemoryCache::GetInstance();
99
100     BalsaHeaders request_headers, response_headers;
101     StringPiece body("Yum");
102     request_headers.SetRequestFirstlineFromStringPieces(
103         "GET",
104         "https://www.google.com/foo",
105         "HTTP/1.1");
106     response_headers.SetRequestFirstlineFromStringPieces("HTTP/1.1",
107                                                          "200",
108                                                          "OK");
109     response_headers.AppendHeader("content-length",
110                                   base::IntToString(body.length()));
111
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);
121       return;
122     }
123
124     cache->AddResponse(request_headers, response_headers, body);
125   }
126
127   const string& StreamBody() {
128     return QuicSpdyServerStreamPeer::body(stream_.get());
129   }
130
131   const BalsaHeaders& StreamHeaders() {
132     return QuicSpdyServerStreamPeer::headers(stream_.get());
133   }
134
135   BalsaHeaders response_headers_;
136   EpollServer eps_;
137   StrictMock<MockConnection>* connection_;
138   StrictMock<MockSession> session_;
139   scoped_ptr<QuicSpdyServerStreamPeer> stream_;
140   string headers_string_;
141   string body_;
142 };
143
144 QuicConsumedData ConsumeAllData(
145     QuicStreamId id,
146     const IOVector& data,
147     QuicStreamOffset offset,
148     bool fin,
149     QuicAckNotifier::DelegateInterface* /*ack_notifier_delegate*/) {
150   return QuicConsumedData(data.TotalBufferSize(), fin);
151 }
152
153 INSTANTIATE_TEST_CASE_P(Tests, QuicSpdyServerStreamTest,
154                         ::testing::ValuesIn(QuicSupportedVersions()));
155
156 TEST_P(QuicSpdyServerStreamTest, TestFraming) {
157   EXPECT_CALL(session_, WritevData(_, _, _, _, _)).Times(AnyNumber()).
158       WillRepeatedly(Invoke(ConsumeAllData));
159
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());
167 }
168
169 TEST_P(QuicSpdyServerStreamTest, TestFramingOnePacket) {
170   EXPECT_CALL(session_, WritevData(_, _, _, _, _)).Times(AnyNumber()).
171       WillRepeatedly(Invoke(ConsumeAllData));
172
173   string message = headers_string_ + body_;
174
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());
181 }
182
183 TEST_P(QuicSpdyServerStreamTest, TestFramingExtraData) {
184   string large_body = "hello world!!!!!!";
185
186   // We'll automatically write out an error (headers + body)
187   EXPECT_CALL(session_, WritevData(_, _, _, _, _)).Times(AnyNumber()).
188       WillRepeatedly(Invoke(ConsumeAllData));
189
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
193   // accept the bytes.
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());
198 }
199
200 TEST_P(QuicSpdyServerStreamTest, TestSendResponse) {
201   BalsaHeaders* request_headers = stream_->mutable_headers();
202   request_headers->SetRequestFirstlineFromStringPieces(
203       "GET",
204       "https://www.google.com/foo",
205       "HTTP/1.1");
206
207   response_headers_.SetResponseFirstlineFromStringPieces(
208       "HTTP/1.1", "200", "OK");
209   response_headers_.ReplaceOrAppendHeader("content-length", "3");
210
211   InSequence s;
212   EXPECT_CALL(session_,
213               WritevData(kHeadersStreamId, _, 0, false, NULL));
214
215
216   EXPECT_CALL(session_, WritevData(_, _, _, _, _)).Times(1).
217       WillOnce(Return(QuicConsumedData(3, true)));
218
219   QuicSpdyServerStreamPeer::SendResponse(stream_.get());
220   EXPECT_TRUE(stream_->read_side_closed());
221   EXPECT_TRUE(stream_->write_side_closed());
222 }
223
224 TEST_P(QuicSpdyServerStreamTest, TestSendErrorResponse) {
225   response_headers_.SetResponseFirstlineFromStringPieces(
226       "HTTP/1.1", "500", "Server Error");
227   response_headers_.ReplaceOrAppendHeader("content-length", "3");
228
229   InSequence s;
230   EXPECT_CALL(session_,
231               WritevData(kHeadersStreamId, _, 0, false, NULL));
232
233   EXPECT_CALL(session_, WritevData(_, _, _, _, _)).Times(1).
234       WillOnce(Return(QuicConsumedData(3, true)));
235
236   QuicSpdyServerStreamPeer::SendErrorResponse(stream_.get());
237   EXPECT_TRUE(stream_->read_side_closed());
238   EXPECT_TRUE(stream_->write_side_closed());
239 }
240
241 TEST_P(QuicSpdyServerStreamTest, InvalidHeadersWithFin) {
242   char arr[] = {
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
264   };
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);
269 }
270
271 }  // namespace
272 }  // namespace test
273 }  // namespace tools
274 }  // namespace net