OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/spdy/spdy_http_stream.h" | 5 #include "net/spdy/spdy_http_stream.h" |
6 | 6 |
7 #include "crypto/ec_private_key.h" | 7 #include "crypto/ec_private_key.h" |
8 #include "crypto/ec_signature_creator.h" | 8 #include "crypto/ec_signature_creator.h" |
9 #include "crypto/signature_creator.h" | 9 #include "crypto/signature_creator.h" |
10 #include "net/base/asn1_util.h" | 10 #include "net/base/asn1_util.h" |
11 #include "net/base/default_server_bound_cert_store.h" | 11 #include "net/base/default_server_bound_cert_store.h" |
12 #include "net/http/http_response_headers.h" | 12 #include "net/http/http_response_headers.h" |
13 #include "net/http/http_response_info.h" | 13 #include "net/http/http_response_info.h" |
14 #include "net/spdy/spdy_session.h" | 14 #include "net/spdy/spdy_session.h" |
15 #include "net/spdy/spdy_test_util_spdy2.h" | 15 #include "net/spdy/spdy_test_util_spdy2.h" |
16 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
17 | 17 |
18 using namespace net::test_spdy2; | 18 using namespace net::test_spdy2; |
19 | 19 |
20 namespace net { | 20 namespace net { |
21 | 21 |
22 class SpdyHttpStreamSpdy2Test : public testing::Test { | 22 class SpdyHttpStreamSpdy2Test : public testing::Test { |
23 public: | 23 public: |
24 OrderedSocketData* data() { return data_.get(); } | 24 OrderedSocketData* data() { return data_.get(); } |
25 protected: | 25 protected: |
26 SpdyHttpStreamSpdy2Test() {} | 26 SpdyHttpStreamSpdy2Test() {} |
27 | 27 |
28 virtual void SetUp() { | 28 virtual void SetUp() { |
29 SpdySession::set_default_protocol(kProtoSPDY2); | 29 SpdySession::set_default_protocol(kProtoSPDY2); |
| 30 merge_chunks_ = UploadDataStream::merge_chunks(); |
30 } | 31 } |
31 | 32 |
32 virtual void TearDown() { | 33 virtual void TearDown() { |
33 crypto::ECSignatureCreator::SetFactoryForTesting(NULL); | 34 crypto::ECSignatureCreator::SetFactoryForTesting(NULL); |
| 35 UploadDataStream::set_merge_chunks(merge_chunks_); |
34 MessageLoop::current()->RunAllPending(); | 36 MessageLoop::current()->RunAllPending(); |
35 } | 37 } |
36 | 38 |
37 int InitSession(MockRead* reads, size_t reads_count, | 39 int InitSession(MockRead* reads, size_t reads_count, |
38 MockWrite* writes, size_t writes_count, | 40 MockWrite* writes, size_t writes_count, |
39 HostPortPair& host_port_pair) { | 41 HostPortPair& host_port_pair) { |
40 HostPortProxyPair pair(host_port_pair, ProxyServer::Direct()); | 42 HostPortProxyPair pair(host_port_pair, ProxyServer::Direct()); |
41 data_.reset(new OrderedSocketData(reads, reads_count, | 43 data_.reset(new OrderedSocketData(reads, reads_count, |
42 writes, writes_count)); | 44 writes, writes_count)); |
43 session_deps_.socket_factory->AddSocketDataProvider(data_.get()); | 45 session_deps_.socket_factory->AddSocketDataProvider(data_.get()); |
(...skipping 17 matching lines...) Expand all Loading... |
61 } | 63 } |
62 | 64 |
63 SpdySessionDependencies session_deps_; | 65 SpdySessionDependencies session_deps_; |
64 scoped_ptr<OrderedSocketData> data_; | 66 scoped_ptr<OrderedSocketData> data_; |
65 scoped_refptr<HttpNetworkSession> http_session_; | 67 scoped_refptr<HttpNetworkSession> http_session_; |
66 scoped_refptr<SpdySession> session_; | 68 scoped_refptr<SpdySession> session_; |
67 scoped_refptr<TransportSocketParams> transport_params_; | 69 scoped_refptr<TransportSocketParams> transport_params_; |
68 | 70 |
69 private: | 71 private: |
70 SpdyTestStateHelper spdy_state_; | 72 SpdyTestStateHelper spdy_state_; |
| 73 bool merge_chunks_; |
71 }; | 74 }; |
72 | 75 |
73 TEST_F(SpdyHttpStreamSpdy2Test, SendRequest) { | 76 TEST_F(SpdyHttpStreamSpdy2Test, SendRequest) { |
74 scoped_ptr<SpdyFrame> req(ConstructSpdyGet(NULL, 0, false, 1, LOWEST)); | 77 scoped_ptr<SpdyFrame> req(ConstructSpdyGet(NULL, 0, false, 1, LOWEST)); |
75 MockWrite writes[] = { | 78 MockWrite writes[] = { |
76 CreateMockWrite(*req.get(), 1), | 79 CreateMockWrite(*req.get(), 1), |
77 }; | 80 }; |
78 scoped_ptr<SpdyFrame> resp(ConstructSpdyGetSynReply(NULL, 0, 1)); | 81 scoped_ptr<SpdyFrame> resp(ConstructSpdyGetSynReply(NULL, 0, 1)); |
79 MockRead reads[] = { | 82 MockRead reads[] = { |
80 CreateMockRead(*resp, 2), | 83 CreateMockRead(*resp, 2), |
(...skipping 30 matching lines...) Expand all Loading... |
111 data()->CompleteRead(); | 114 data()->CompleteRead(); |
112 | 115 |
113 // Because we abandoned the stream, we don't expect to find a session in the | 116 // Because we abandoned the stream, we don't expect to find a session in the |
114 // pool anymore. | 117 // pool anymore. |
115 EXPECT_FALSE(http_session_->spdy_session_pool()->HasSession(pair)); | 118 EXPECT_FALSE(http_session_->spdy_session_pool()->HasSession(pair)); |
116 EXPECT_TRUE(data()->at_read_eof()); | 119 EXPECT_TRUE(data()->at_read_eof()); |
117 EXPECT_TRUE(data()->at_write_eof()); | 120 EXPECT_TRUE(data()->at_write_eof()); |
118 } | 121 } |
119 | 122 |
120 TEST_F(SpdyHttpStreamSpdy2Test, SendChunkedPost) { | 123 TEST_F(SpdyHttpStreamSpdy2Test, SendChunkedPost) { |
| 124 // TODO(satish): Remove this once we have a better way to unit test POST |
| 125 // requests with chunked uploads. |
121 UploadDataStream::set_merge_chunks(false); | 126 UploadDataStream::set_merge_chunks(false); |
122 | 127 |
123 scoped_ptr<SpdyFrame> req(ConstructChunkedSpdyPost(NULL, 0)); | 128 scoped_ptr<SpdyFrame> req(ConstructChunkedSpdyPost(NULL, 0)); |
124 scoped_ptr<SpdyFrame> chunk1(ConstructSpdyBodyFrame(1, false)); | 129 scoped_ptr<SpdyFrame> chunk1(ConstructSpdyBodyFrame(1, false)); |
125 scoped_ptr<SpdyFrame> chunk2(ConstructSpdyBodyFrame(1, true)); | 130 scoped_ptr<SpdyFrame> chunk2(ConstructSpdyBodyFrame(1, true)); |
126 MockWrite writes[] = { | 131 MockWrite writes[] = { |
127 CreateMockWrite(*req.get(), 1), | 132 CreateMockWrite(*req.get(), 0), |
128 CreateMockWrite(*chunk1, 2), // POST upload frames | 133 CreateMockWrite(*chunk1, 1), // POST upload frames |
129 CreateMockWrite(*chunk2, 3), | 134 CreateMockWrite(*chunk2, 2), |
130 }; | 135 }; |
131 scoped_ptr<SpdyFrame> resp(ConstructSpdyPostSynReply(NULL, 0)); | 136 scoped_ptr<SpdyFrame> resp(ConstructSpdyPostSynReply(NULL, 0)); |
132 MockRead reads[] = { | 137 MockRead reads[] = { |
133 CreateMockRead(*resp, 4), | 138 CreateMockRead(*resp, 3), |
134 CreateMockRead(*chunk1, 5), | 139 CreateMockRead(*chunk1, 4), |
135 CreateMockRead(*chunk2, 5), | 140 CreateMockRead(*chunk2, 5), |
136 MockRead(SYNCHRONOUS, 0, 6) // EOF | 141 MockRead(SYNCHRONOUS, 0, 6) // EOF |
137 }; | 142 }; |
138 | 143 |
139 HostPortPair host_port_pair("www.google.com", 80); | 144 HostPortPair host_port_pair("www.google.com", 80); |
140 HostPortProxyPair pair(host_port_pair, ProxyServer::Direct()); | 145 HostPortProxyPair pair(host_port_pair, ProxyServer::Direct()); |
141 EXPECT_EQ(OK, InitSession(reads, arraysize(reads), writes, arraysize(writes), | 146 EXPECT_EQ(OK, InitSession(reads, arraysize(reads), writes, arraysize(writes), |
142 host_port_pair)); | 147 host_port_pair)); |
143 | 148 |
144 HttpRequestInfo request; | 149 HttpRequestInfo request; |
(...skipping 26 matching lines...) Expand all Loading... |
171 data()->CompleteRead(); | 176 data()->CompleteRead(); |
172 MessageLoop::current()->RunAllPending(); | 177 MessageLoop::current()->RunAllPending(); |
173 | 178 |
174 // Because we abandoned the stream, we don't expect to find a session in the | 179 // Because we abandoned the stream, we don't expect to find a session in the |
175 // pool anymore. | 180 // pool anymore. |
176 EXPECT_FALSE(http_session_->spdy_session_pool()->HasSession(pair)); | 181 EXPECT_FALSE(http_session_->spdy_session_pool()->HasSession(pair)); |
177 EXPECT_TRUE(data()->at_read_eof()); | 182 EXPECT_TRUE(data()->at_read_eof()); |
178 EXPECT_TRUE(data()->at_write_eof()); | 183 EXPECT_TRUE(data()->at_write_eof()); |
179 } | 184 } |
180 | 185 |
| 186 TEST_F(SpdyHttpStreamSpdy2Test, DelayedSendChunkedPost) { |
| 187 // TODO(satish): Remove this once we have a better way to unit test POST |
| 188 // requests with chunked uploads. |
| 189 UploadDataStream::set_merge_chunks(false); |
| 190 |
| 191 const char kUploadData1[] = "12345678"; |
| 192 const int kUploadData1Size = arraysize(kUploadData1)-1; |
| 193 scoped_ptr<SpdyFrame> req(ConstructChunkedSpdyPost(NULL, 0)); |
| 194 scoped_ptr<SpdyFrame> chunk1(ConstructSpdyBodyFrame(1, false)); |
| 195 scoped_ptr<SpdyFrame> chunk2( |
| 196 ConstructSpdyBodyFrame(1, kUploadData1, kUploadData1Size, false)); |
| 197 scoped_ptr<SpdyFrame> chunk3(ConstructSpdyBodyFrame(1, true)); |
| 198 MockWrite writes[] = { |
| 199 CreateMockWrite(*req.get(), 0), |
| 200 CreateMockWrite(*chunk1, 1), // POST upload frames |
| 201 CreateMockWrite(*chunk2, 2), |
| 202 CreateMockWrite(*chunk3, 3), |
| 203 }; |
| 204 scoped_ptr<SpdyFrame> resp(ConstructSpdyPostSynReply(NULL, 0)); |
| 205 MockRead reads[] = { |
| 206 CreateMockRead(*resp, 4), |
| 207 CreateMockRead(*chunk1, 5), |
| 208 CreateMockRead(*chunk2, 6), |
| 209 CreateMockRead(*chunk3, 7), |
| 210 MockRead(ASYNC, 0, 8) // EOF |
| 211 }; |
| 212 |
| 213 HostPortPair host_port_pair("www.google.com", 80); |
| 214 HostPortProxyPair pair(host_port_pair, ProxyServer::Direct()); |
| 215 |
| 216 scoped_refptr<DeterministicSocketData> data( |
| 217 new DeterministicSocketData(reads, arraysize(reads), |
| 218 writes, arraysize(writes))); |
| 219 |
| 220 DeterministicMockClientSocketFactory* socket_factory = |
| 221 session_deps_.deterministic_socket_factory.get(); |
| 222 socket_factory->AddSocketDataProvider(data.get()); |
| 223 |
| 224 http_session_ = SpdySessionDependencies::SpdyCreateSessionDeterministic( |
| 225 &session_deps_); |
| 226 session_ = http_session_->spdy_session_pool()->Get(pair, BoundNetLog()); |
| 227 transport_params_ = new TransportSocketParams(host_port_pair, |
| 228 MEDIUM, false, false, |
| 229 OnHostResolutionCallback()); |
| 230 |
| 231 TestCompletionCallback callback; |
| 232 scoped_ptr<ClientSocketHandle> connection(new ClientSocketHandle); |
| 233 |
| 234 EXPECT_EQ(ERR_IO_PENDING, |
| 235 connection->Init(host_port_pair.ToString(), |
| 236 transport_params_, |
| 237 MEDIUM, |
| 238 callback.callback(), |
| 239 http_session_->GetTransportSocketPool( |
| 240 HttpNetworkSession::NORMAL_SOCKET_POOL), |
| 241 BoundNetLog())); |
| 242 |
| 243 callback.WaitForResult(); |
| 244 EXPECT_EQ(OK, |
| 245 session_->InitializeWithSocket(connection.release(), false, OK)); |
| 246 |
| 247 HttpRequestInfo request; |
| 248 request.method = "POST"; |
| 249 request.url = GURL("http://www.google.com/"); |
| 250 request.upload_data = new UploadData(); |
| 251 request.upload_data->set_is_chunked(true); |
| 252 |
| 253 BoundNetLog net_log; |
| 254 scoped_ptr<SpdyHttpStream> http_stream( |
| 255 new SpdyHttpStream(session_.get(), true)); |
| 256 ASSERT_EQ(OK, |
| 257 http_stream->InitializeStream(&request, |
| 258 net_log, |
| 259 CompletionCallback())); |
| 260 |
| 261 scoped_ptr<UploadDataStream> upload_stream( |
| 262 new UploadDataStream(request.upload_data)); |
| 263 ASSERT_EQ(OK, upload_stream->Init()); |
| 264 |
| 265 request.upload_data->AppendChunk(kUploadData, kUploadDataSize, false); |
| 266 |
| 267 HttpRequestHeaders headers; |
| 268 HttpResponseInfo response; |
| 269 // This will attempt to Write() the initial request and headers, which will |
| 270 // complete asynchronously. |
| 271 EXPECT_EQ(ERR_IO_PENDING, |
| 272 http_stream->SendRequest(headers, |
| 273 upload_stream.Pass(), |
| 274 &response, |
| 275 callback.callback())); |
| 276 EXPECT_TRUE(http_session_->spdy_session_pool()->HasSession(pair)); |
| 277 |
| 278 // Complete the initial request write and the first chunk. |
| 279 data->RunFor(1); |
| 280 ASSERT_TRUE(callback.have_result()); |
| 281 |
| 282 // Now append the second chunk. This will enqueue another write. |
| 283 request.upload_data->AppendChunk(kUploadData1, kUploadData1Size, false); |
| 284 ASSERT_TRUE(callback.have_result()); |
| 285 |
| 286 // Now append final chunk. This will enqueue another write. |
| 287 request.upload_data->AppendChunk(kUploadData, kUploadDataSize, true); |
| 288 ASSERT_TRUE(callback.have_result()); |
| 289 |
| 290 data->RunFor(1); |
| 291 ASSERT_TRUE(callback.have_result()); |
| 292 |
| 293 // Finish writing all the chunks. |
| 294 data->RunFor(3); |
| 295 int rv = callback.WaitForResult(); |
| 296 EXPECT_GT(rv, 0); |
| 297 EXPECT_EQ(OK, http_stream->ReadResponseHeaders(callback.callback())); |
| 298 |
| 299 // Read and check |chunk1| response. |
| 300 data->RunFor(1); |
| 301 scoped_refptr<IOBuffer> buf1(new IOBuffer(kUploadDataSize)); |
| 302 rv = http_stream->ReadResponseBody(buf1, |
| 303 kUploadDataSize, |
| 304 callback.callback()); |
| 305 ASSERT_EQ(kUploadDataSize, rv); |
| 306 EXPECT_EQ(kUploadData, std::string(buf1->data(), kUploadDataSize)); |
| 307 |
| 308 // Read and check |chunk2| response. |
| 309 data->RunFor(1); |
| 310 scoped_refptr<IOBuffer> buf2(new IOBuffer(kUploadData1Size)); |
| 311 rv = http_stream->ReadResponseBody(buf2, |
| 312 kUploadData1Size, |
| 313 callback.callback()); |
| 314 ASSERT_EQ(kUploadData1Size, rv); |
| 315 EXPECT_EQ(kUploadData1, std::string(buf2->data(), kUploadData1Size)); |
| 316 |
| 317 // Read and check |chunk3| response. |
| 318 data->RunFor(1); |
| 319 scoped_refptr<IOBuffer> buf3(new IOBuffer(kUploadDataSize)); |
| 320 rv = http_stream->ReadResponseBody(buf3, |
| 321 kUploadDataSize, |
| 322 callback.callback()); |
| 323 ASSERT_EQ(kUploadDataSize, rv); |
| 324 EXPECT_EQ(kUploadData, std::string(buf3->data(), kUploadDataSize)); |
| 325 |
| 326 // Finish reading the |EOF|. |
| 327 data->RunFor(1); |
| 328 ASSERT_TRUE(response.headers.get()); |
| 329 ASSERT_EQ(200, response.headers->response_code()); |
| 330 EXPECT_TRUE(data->at_read_eof()); |
| 331 EXPECT_TRUE(data->at_write_eof()); |
| 332 } |
| 333 |
181 // Test case for bug: http://code.google.com/p/chromium/issues/detail?id=50058 | 334 // Test case for bug: http://code.google.com/p/chromium/issues/detail?id=50058 |
182 TEST_F(SpdyHttpStreamSpdy2Test, SpdyURLTest) { | 335 TEST_F(SpdyHttpStreamSpdy2Test, SpdyURLTest) { |
183 const char * const full_url = "http://www.google.com/foo?query=what#anchor"; | 336 const char * const full_url = "http://www.google.com/foo?query=what#anchor"; |
184 const char * const base_url = "http://www.google.com/foo?query=what"; | 337 const char * const base_url = "http://www.google.com/foo?query=what"; |
185 scoped_ptr<SpdyFrame> req(ConstructSpdyGet(base_url, false, 1, LOWEST)); | 338 scoped_ptr<SpdyFrame> req(ConstructSpdyGet(base_url, false, 1, LOWEST)); |
186 MockWrite writes[] = { | 339 MockWrite writes[] = { |
187 CreateMockWrite(*req.get(), 1), | 340 CreateMockWrite(*req.get(), 1), |
188 }; | 341 }; |
189 scoped_ptr<SpdyFrame> resp(ConstructSpdyGetSynReply(NULL, 0, 1)); | 342 scoped_ptr<SpdyFrame> resp(ConstructSpdyGetSynReply(NULL, 0, 1)); |
190 MockRead reads[] = { | 343 MockRead reads[] = { |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
231 // pool anymore. | 384 // pool anymore. |
232 EXPECT_FALSE(http_session_->spdy_session_pool()->HasSession(pair)); | 385 EXPECT_FALSE(http_session_->spdy_session_pool()->HasSession(pair)); |
233 EXPECT_TRUE(data()->at_read_eof()); | 386 EXPECT_TRUE(data()->at_read_eof()); |
234 EXPECT_TRUE(data()->at_write_eof()); | 387 EXPECT_TRUE(data()->at_write_eof()); |
235 } | 388 } |
236 | 389 |
237 // TODO(willchan): Write a longer test for SpdyStream that exercises all | 390 // TODO(willchan): Write a longer test for SpdyStream that exercises all |
238 // methods. | 391 // methods. |
239 | 392 |
240 } // namespace net | 393 } // namespace net |
OLD | NEW |