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 "base/threading/sequenced_worker_pool.h" | 7 #include "base/threading/sequenced_worker_pool.h" |
8 #include "crypto/ec_private_key.h" | 8 #include "crypto/ec_private_key.h" |
9 #include "crypto/ec_signature_creator.h" | 9 #include "crypto/ec_signature_creator.h" |
10 #include "crypto/signature_creator.h" | 10 #include "crypto/signature_creator.h" |
(...skipping 11 matching lines...) Expand all Loading... |
22 namespace net { | 22 namespace net { |
23 | 23 |
24 class SpdyHttpStreamSpdy3Test : public testing::Test { | 24 class SpdyHttpStreamSpdy3Test : public testing::Test { |
25 public: | 25 public: |
26 OrderedSocketData* data() { return data_.get(); } | 26 OrderedSocketData* data() { return data_.get(); } |
27 protected: | 27 protected: |
28 SpdyHttpStreamSpdy3Test() {} | 28 SpdyHttpStreamSpdy3Test() {} |
29 | 29 |
30 virtual void SetUp() { | 30 virtual void SetUp() { |
31 SpdySession::set_default_protocol(kProtoSPDY3); | 31 SpdySession::set_default_protocol(kProtoSPDY3); |
| 32 merge_chunks_ = UploadDataStream::merge_chunks(); |
32 } | 33 } |
33 | 34 |
34 virtual void TearDown() { | 35 virtual void TearDown() { |
35 crypto::ECSignatureCreator::SetFactoryForTesting(NULL); | 36 crypto::ECSignatureCreator::SetFactoryForTesting(NULL); |
| 37 UploadDataStream::set_merge_chunks(merge_chunks_); |
36 MessageLoop::current()->RunAllPending(); | 38 MessageLoop::current()->RunAllPending(); |
37 } | 39 } |
38 | 40 |
| 41 void set_merge_chunks(bool merge) { |
| 42 UploadDataStream::set_merge_chunks(merge); |
| 43 } |
| 44 |
39 int InitSession(MockRead* reads, size_t reads_count, | 45 int InitSession(MockRead* reads, size_t reads_count, |
40 MockWrite* writes, size_t writes_count, | 46 MockWrite* writes, size_t writes_count, |
41 HostPortPair& host_port_pair) { | 47 HostPortPair& host_port_pair) { |
42 HostPortProxyPair pair(host_port_pair, ProxyServer::Direct()); | 48 HostPortProxyPair pair(host_port_pair, ProxyServer::Direct()); |
43 data_.reset(new OrderedSocketData(reads, reads_count, | 49 data_.reset(new OrderedSocketData(reads, reads_count, |
44 writes, writes_count)); | 50 writes, writes_count)); |
45 session_deps_.socket_factory->AddSocketDataProvider(data_.get()); | 51 session_deps_.socket_factory->AddSocketDataProvider(data_.get()); |
46 http_session_ = SpdySessionDependencies::SpdyCreateSession(&session_deps_); | 52 http_session_ = SpdySessionDependencies::SpdyCreateSession(&session_deps_); |
47 session_ = http_session_->spdy_session_pool()->Get(pair, BoundNetLog()); | 53 session_ = http_session_->spdy_session_pool()->Get(pair, BoundNetLog()); |
48 transport_params_ = new TransportSocketParams(host_port_pair, | 54 transport_params_ = new TransportSocketParams(host_port_pair, |
(...skipping 19 matching lines...) Expand all Loading... |
68 const std::string& proof); | 74 const std::string& proof); |
69 | 75 |
70 SpdySessionDependencies session_deps_; | 76 SpdySessionDependencies session_deps_; |
71 scoped_ptr<OrderedSocketData> data_; | 77 scoped_ptr<OrderedSocketData> data_; |
72 scoped_refptr<HttpNetworkSession> http_session_; | 78 scoped_refptr<HttpNetworkSession> http_session_; |
73 scoped_refptr<SpdySession> session_; | 79 scoped_refptr<SpdySession> session_; |
74 scoped_refptr<TransportSocketParams> transport_params_; | 80 scoped_refptr<TransportSocketParams> transport_params_; |
75 | 81 |
76 private: | 82 private: |
77 SpdyTestStateHelper spdy_state_; | 83 SpdyTestStateHelper spdy_state_; |
| 84 bool merge_chunks_; |
78 }; | 85 }; |
79 | 86 |
80 TEST_F(SpdyHttpStreamSpdy3Test, SendRequest) { | 87 TEST_F(SpdyHttpStreamSpdy3Test, SendRequest) { |
81 scoped_ptr<SpdyFrame> req(ConstructSpdyGet(NULL, 0, false, 1, LOWEST)); | 88 scoped_ptr<SpdyFrame> req(ConstructSpdyGet(NULL, 0, false, 1, LOWEST)); |
82 MockWrite writes[] = { | 89 MockWrite writes[] = { |
83 CreateMockWrite(*req.get(), 1), | 90 CreateMockWrite(*req.get(), 1), |
84 }; | 91 }; |
85 scoped_ptr<SpdyFrame> resp(ConstructSpdyGetSynReply(NULL, 0, 1)); | 92 scoped_ptr<SpdyFrame> resp(ConstructSpdyGetSynReply(NULL, 0, 1)); |
86 MockRead reads[] = { | 93 MockRead reads[] = { |
87 CreateMockRead(*resp, 2), | 94 CreateMockRead(*resp, 2), |
(...skipping 30 matching lines...) Expand all Loading... |
118 data()->CompleteRead(); | 125 data()->CompleteRead(); |
119 | 126 |
120 // Because we abandoned the stream, we don't expect to find a session in the | 127 // Because we abandoned the stream, we don't expect to find a session in the |
121 // pool anymore. | 128 // pool anymore. |
122 EXPECT_FALSE(http_session_->spdy_session_pool()->HasSession(pair)); | 129 EXPECT_FALSE(http_session_->spdy_session_pool()->HasSession(pair)); |
123 EXPECT_TRUE(data()->at_read_eof()); | 130 EXPECT_TRUE(data()->at_read_eof()); |
124 EXPECT_TRUE(data()->at_write_eof()); | 131 EXPECT_TRUE(data()->at_write_eof()); |
125 } | 132 } |
126 | 133 |
127 TEST_F(SpdyHttpStreamSpdy3Test, SendChunkedPost) { | 134 TEST_F(SpdyHttpStreamSpdy3Test, SendChunkedPost) { |
128 UploadDataStream::set_merge_chunks(false); | 135 // TODO(satish): Remove this once we have a better way to unit test POST |
| 136 // requests with chunked uploads. |
| 137 set_merge_chunks(false); |
129 | 138 |
130 scoped_ptr<SpdyFrame> req(ConstructChunkedSpdyPost(NULL, 0)); | 139 scoped_ptr<SpdyFrame> req(ConstructChunkedSpdyPost(NULL, 0)); |
131 scoped_ptr<SpdyFrame> chunk1(ConstructSpdyBodyFrame(1, false)); | 140 scoped_ptr<SpdyFrame> chunk1(ConstructSpdyBodyFrame(1, false)); |
132 scoped_ptr<SpdyFrame> chunk2(ConstructSpdyBodyFrame(1, true)); | 141 scoped_ptr<SpdyFrame> chunk2(ConstructSpdyBodyFrame(1, true)); |
133 MockWrite writes[] = { | 142 MockWrite writes[] = { |
134 CreateMockWrite(*req.get(), 1), | 143 CreateMockWrite(*req.get(), 0), |
135 CreateMockWrite(*chunk1, 2), // POST upload frames | 144 CreateMockWrite(*chunk1, 1), // POST upload frames |
136 CreateMockWrite(*chunk2, 3), | 145 CreateMockWrite(*chunk2, 2), |
137 }; | 146 }; |
138 scoped_ptr<SpdyFrame> resp(ConstructSpdyPostSynReply(NULL, 0)); | 147 scoped_ptr<SpdyFrame> resp(ConstructSpdyPostSynReply(NULL, 0)); |
139 MockRead reads[] = { | 148 MockRead reads[] = { |
140 CreateMockRead(*resp, 4), | 149 CreateMockRead(*resp, 3), |
141 CreateMockRead(*chunk1, 5), | 150 CreateMockRead(*chunk1, 4), |
142 CreateMockRead(*chunk2, 5), | 151 CreateMockRead(*chunk2, 5), |
143 MockRead(SYNCHRONOUS, 0, 6) // EOF | 152 MockRead(SYNCHRONOUS, 0, 6) // EOF |
144 }; | 153 }; |
145 | 154 |
146 HostPortPair host_port_pair("www.google.com", 80); | 155 HostPortPair host_port_pair("www.google.com", 80); |
147 HostPortProxyPair pair(host_port_pair, ProxyServer::Direct()); | 156 HostPortProxyPair pair(host_port_pair, ProxyServer::Direct()); |
148 EXPECT_EQ(OK, InitSession(reads, arraysize(reads), writes, arraysize(writes), | 157 EXPECT_EQ(OK, InitSession(reads, arraysize(reads), writes, arraysize(writes), |
149 host_port_pair)); | 158 host_port_pair)); |
150 | 159 |
151 HttpRequestInfo request; | 160 HttpRequestInfo request; |
(...skipping 26 matching lines...) Expand all Loading... |
178 data()->CompleteRead(); | 187 data()->CompleteRead(); |
179 MessageLoop::current()->RunAllPending(); | 188 MessageLoop::current()->RunAllPending(); |
180 | 189 |
181 // Because we abandoned the stream, we don't expect to find a session in the | 190 // Because we abandoned the stream, we don't expect to find a session in the |
182 // pool anymore. | 191 // pool anymore. |
183 EXPECT_FALSE(http_session_->spdy_session_pool()->HasSession(pair)); | 192 EXPECT_FALSE(http_session_->spdy_session_pool()->HasSession(pair)); |
184 EXPECT_TRUE(data()->at_read_eof()); | 193 EXPECT_TRUE(data()->at_read_eof()); |
185 EXPECT_TRUE(data()->at_write_eof()); | 194 EXPECT_TRUE(data()->at_write_eof()); |
186 } | 195 } |
187 | 196 |
| 197 TEST_F(SpdyHttpStreamSpdy3Test, DelayedSendChunkedPost) { |
| 198 // TODO(satish): Remove this once we have a better way to unit test POST |
| 199 // requests with chunked uploads. |
| 200 set_merge_chunks(false); |
| 201 |
| 202 const char kUploadData1[] = "12345678"; |
| 203 const int kUploadData1Size = arraysize(kUploadData1)-1; |
| 204 scoped_ptr<SpdyFrame> req(ConstructChunkedSpdyPost(NULL, 0)); |
| 205 scoped_ptr<SpdyFrame> chunk1(ConstructSpdyBodyFrame(1, false)); |
| 206 scoped_ptr<SpdyFrame> chunk2( |
| 207 ConstructSpdyBodyFrame(1, kUploadData1, kUploadData1Size, false)); |
| 208 scoped_ptr<SpdyFrame> chunk3(ConstructSpdyBodyFrame(1, true)); |
| 209 MockWrite writes[] = { |
| 210 CreateMockWrite(*req.get(), 0), |
| 211 CreateMockWrite(*chunk1, 1), // POST upload frames |
| 212 CreateMockWrite(*chunk2, 2), |
| 213 CreateMockWrite(*chunk3, 3), |
| 214 }; |
| 215 scoped_ptr<SpdyFrame> resp(ConstructSpdyPostSynReply(NULL, 0)); |
| 216 MockRead reads[] = { |
| 217 CreateMockRead(*resp, 4), |
| 218 CreateMockRead(*chunk1, 5), |
| 219 CreateMockRead(*chunk2, 6), |
| 220 CreateMockRead(*chunk3, 7), |
| 221 MockRead(ASYNC, 0, 8) // EOF |
| 222 }; |
| 223 |
| 224 HostPortPair host_port_pair("www.google.com", 80); |
| 225 HostPortProxyPair pair(host_port_pair, ProxyServer::Direct()); |
| 226 |
| 227 scoped_refptr<DeterministicSocketData> data( |
| 228 new DeterministicSocketData(reads, arraysize(reads), |
| 229 writes, arraysize(writes))); |
| 230 |
| 231 DeterministicMockClientSocketFactory* socket_factory = |
| 232 session_deps_.deterministic_socket_factory.get(); |
| 233 socket_factory->AddSocketDataProvider(data.get()); |
| 234 |
| 235 http_session_ = SpdySessionDependencies::SpdyCreateSessionDeterministic( |
| 236 &session_deps_); |
| 237 session_ = http_session_->spdy_session_pool()->Get(pair, BoundNetLog()); |
| 238 transport_params_ = new TransportSocketParams(host_port_pair, |
| 239 MEDIUM, false, false, |
| 240 OnHostResolutionCallback()); |
| 241 |
| 242 TestCompletionCallback callback; |
| 243 scoped_ptr<ClientSocketHandle> connection(new ClientSocketHandle); |
| 244 |
| 245 EXPECT_EQ(ERR_IO_PENDING, |
| 246 connection->Init(host_port_pair.ToString(), |
| 247 transport_params_, |
| 248 MEDIUM, |
| 249 callback.callback(), |
| 250 http_session_->GetTransportSocketPool( |
| 251 HttpNetworkSession::NORMAL_SOCKET_POOL), |
| 252 BoundNetLog())); |
| 253 |
| 254 callback.WaitForResult(); |
| 255 EXPECT_EQ(OK, |
| 256 session_->InitializeWithSocket(connection.release(), false, OK)); |
| 257 |
| 258 HttpRequestInfo request; |
| 259 request.method = "POST"; |
| 260 request.url = GURL("http://www.google.com/"); |
| 261 request.upload_data = new UploadData(); |
| 262 request.upload_data->set_is_chunked(true); |
| 263 |
| 264 BoundNetLog net_log; |
| 265 scoped_ptr<SpdyHttpStream> http_stream( |
| 266 new SpdyHttpStream(session_.get(), true)); |
| 267 ASSERT_EQ(OK, |
| 268 http_stream->InitializeStream(&request, |
| 269 net_log, |
| 270 CompletionCallback())); |
| 271 |
| 272 scoped_ptr<UploadDataStream> upload_stream( |
| 273 new UploadDataStream(request.upload_data)); |
| 274 ASSERT_EQ(OK, upload_stream->Init()); |
| 275 |
| 276 request.upload_data->AppendChunk(kUploadData, kUploadDataSize, false); |
| 277 |
| 278 HttpRequestHeaders headers; |
| 279 HttpResponseInfo response; |
| 280 // This will attempt to Write() the initial request and headers, which will |
| 281 // complete asynchronously. |
| 282 EXPECT_EQ(ERR_IO_PENDING, |
| 283 http_stream->SendRequest(headers, |
| 284 upload_stream.Pass(), |
| 285 &response, |
| 286 callback.callback())); |
| 287 EXPECT_TRUE(http_session_->spdy_session_pool()->HasSession(pair)); |
| 288 |
| 289 // Complete the initial request write and the first chunk. |
| 290 data->RunFor(1); |
| 291 ASSERT_TRUE(callback.have_result()); |
| 292 |
| 293 // Now append the second chunk. This will enqueue another write. |
| 294 request.upload_data->AppendChunk(kUploadData1, kUploadData1Size, false); |
| 295 ASSERT_TRUE(callback.have_result()); |
| 296 |
| 297 // Now append final chunk. This will enqueue another write. |
| 298 request.upload_data->AppendChunk(kUploadData, kUploadDataSize, true); |
| 299 ASSERT_TRUE(callback.have_result()); |
| 300 |
| 301 data->RunFor(1); |
| 302 ASSERT_TRUE(callback.have_result()); |
| 303 |
| 304 // Finish writing all the chunks. |
| 305 data->RunFor(3); |
| 306 int rv = callback.WaitForResult(); |
| 307 EXPECT_GT(rv, 0); |
| 308 EXPECT_EQ(OK, http_stream->ReadResponseHeaders(callback.callback())); |
| 309 |
| 310 // Read and check |chunk1| response. |
| 311 data->RunFor(1); |
| 312 scoped_refptr<IOBuffer> buf1(new IOBuffer(kUploadDataSize)); |
| 313 rv = http_stream->ReadResponseBody(buf1, |
| 314 kUploadDataSize, |
| 315 callback.callback()); |
| 316 ASSERT_EQ(kUploadDataSize, rv); |
| 317 EXPECT_EQ(kUploadData, std::string(buf1->data(), kUploadDataSize)); |
| 318 |
| 319 // Read and check |chunk2| response. |
| 320 data->RunFor(1); |
| 321 scoped_refptr<IOBuffer> buf2(new IOBuffer(kUploadData1Size)); |
| 322 rv = http_stream->ReadResponseBody(buf2, |
| 323 kUploadData1Size, |
| 324 callback.callback()); |
| 325 ASSERT_EQ(kUploadData1Size, rv); |
| 326 EXPECT_EQ(kUploadData1, std::string(buf2->data(), kUploadData1Size)); |
| 327 |
| 328 // Read and check |chunk3| response. |
| 329 data->RunFor(1); |
| 330 scoped_refptr<IOBuffer> buf3(new IOBuffer(kUploadDataSize)); |
| 331 rv = http_stream->ReadResponseBody(buf3, |
| 332 kUploadDataSize, |
| 333 callback.callback()); |
| 334 ASSERT_EQ(kUploadDataSize, rv); |
| 335 EXPECT_EQ(kUploadData, std::string(buf3->data(), kUploadDataSize)); |
| 336 |
| 337 // Finish reading the |EOF|. |
| 338 data->RunFor(1); |
| 339 ASSERT_TRUE(response.headers.get()); |
| 340 ASSERT_EQ(200, response.headers->response_code()); |
| 341 EXPECT_TRUE(data->at_read_eof()); |
| 342 EXPECT_TRUE(data->at_write_eof()); |
| 343 } |
| 344 |
188 // Test case for bug: http://code.google.com/p/chromium/issues/detail?id=50058 | 345 // Test case for bug: http://code.google.com/p/chromium/issues/detail?id=50058 |
189 TEST_F(SpdyHttpStreamSpdy3Test, SpdyURLTest) { | 346 TEST_F(SpdyHttpStreamSpdy3Test, SpdyURLTest) { |
190 const char * const full_url = "http://www.google.com/foo?query=what#anchor"; | 347 const char * const full_url = "http://www.google.com/foo?query=what#anchor"; |
191 const char * const base_url = "http://www.google.com/foo?query=what"; | 348 const char * const base_url = "http://www.google.com/foo?query=what"; |
192 scoped_ptr<SpdyFrame> req(ConstructSpdyGet(base_url, false, 1, LOWEST)); | 349 scoped_ptr<SpdyFrame> req(ConstructSpdyGet(base_url, false, 1, LOWEST)); |
193 MockWrite writes[] = { | 350 MockWrite writes[] = { |
194 CreateMockWrite(*req.get(), 1), | 351 CreateMockWrite(*req.get(), 1), |
195 }; | 352 }; |
196 scoped_ptr<SpdyFrame> resp(ConstructSpdyGetSynReply(NULL, 0, 1)); | 353 scoped_ptr<SpdyFrame> resp(ConstructSpdyGetSynReply(NULL, 0, 1)); |
197 MockRead reads[] = { | 354 MockRead reads[] = { |
(...skipping 328 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
526 | 683 |
527 sequenced_worker_pool->Shutdown(); | 684 sequenced_worker_pool->Shutdown(); |
528 } | 685 } |
529 | 686 |
530 #endif // !defined(USE_OPENSSL) | 687 #endif // !defined(USE_OPENSSL) |
531 | 688 |
532 // TODO(willchan): Write a longer test for SpdyStream that exercises all | 689 // TODO(willchan): Write a longer test for SpdyStream that exercises all |
533 // methods. | 690 // methods. |
534 | 691 |
535 } // namespace net | 692 } // namespace net |
OLD | NEW |