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/memory/scoped_ptr.h" | 7 #include "base/memory/scoped_ptr.h" |
8 #include "base/threading/sequenced_worker_pool.h" | 8 #include "base/threading/sequenced_worker_pool.h" |
9 #include "crypto/ec_private_key.h" | 9 #include "crypto/ec_private_key.h" |
10 #include "crypto/ec_signature_creator.h" | 10 #include "crypto/ec_signature_creator.h" |
(...skipping 16 matching lines...) Expand all Loading... |
27 OrderedSocketData* data() { return data_.get(); } | 27 OrderedSocketData* data() { return data_.get(); } |
28 protected: | 28 protected: |
29 SpdyHttpStreamSpdy3Test() {} | 29 SpdyHttpStreamSpdy3Test() {} |
30 | 30 |
31 virtual void SetUp() { | 31 virtual void SetUp() { |
32 SpdySession::set_default_protocol(kProtoSPDY3); | 32 SpdySession::set_default_protocol(kProtoSPDY3); |
33 } | 33 } |
34 | 34 |
35 virtual void TearDown() { | 35 virtual void TearDown() { |
36 crypto::ECSignatureCreator::SetFactoryForTesting(NULL); | 36 crypto::ECSignatureCreator::SetFactoryForTesting(NULL); |
| 37 UploadDataStream::ResetMergeChunks(); |
37 MessageLoop::current()->RunAllPending(); | 38 MessageLoop::current()->RunAllPending(); |
38 } | 39 } |
39 | 40 |
| 41 void set_merge_chunks(bool merge) { |
| 42 UploadDataStream::set_merge_chunks(merge); |
| 43 } |
| 44 |
40 int InitSession(MockRead* reads, size_t reads_count, | 45 int InitSession(MockRead* reads, size_t reads_count, |
41 MockWrite* writes, size_t writes_count, | 46 MockWrite* writes, size_t writes_count, |
42 HostPortPair& host_port_pair) { | 47 HostPortPair& host_port_pair) { |
43 HostPortProxyPair pair(host_port_pair, ProxyServer::Direct()); | 48 HostPortProxyPair pair(host_port_pair, ProxyServer::Direct()); |
44 data_.reset(new OrderedSocketData(reads, reads_count, | 49 data_.reset(new OrderedSocketData(reads, reads_count, |
45 writes, writes_count)); | 50 writes, writes_count)); |
46 session_deps_.socket_factory->AddSocketDataProvider(data_.get()); | 51 session_deps_.socket_factory->AddSocketDataProvider(data_.get()); |
47 http_session_ = SpdySessionDependencies::SpdyCreateSession(&session_deps_); | 52 http_session_ = SpdySessionDependencies::SpdyCreateSession(&session_deps_); |
48 session_ = http_session_->spdy_session_pool()->Get(pair, BoundNetLog()); | 53 session_ = http_session_->spdy_session_pool()->Get(pair, BoundNetLog()); |
49 transport_params_ = new TransportSocketParams(host_port_pair, | 54 transport_params_ = new TransportSocketParams(host_port_pair, |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
119 data()->CompleteRead(); | 124 data()->CompleteRead(); |
120 | 125 |
121 // Because we abandoned the stream, we don't expect to find a session in the | 126 // Because we abandoned the stream, we don't expect to find a session in the |
122 // pool anymore. | 127 // pool anymore. |
123 EXPECT_FALSE(http_session_->spdy_session_pool()->HasSession(pair)); | 128 EXPECT_FALSE(http_session_->spdy_session_pool()->HasSession(pair)); |
124 EXPECT_TRUE(data()->at_read_eof()); | 129 EXPECT_TRUE(data()->at_read_eof()); |
125 EXPECT_TRUE(data()->at_write_eof()); | 130 EXPECT_TRUE(data()->at_write_eof()); |
126 } | 131 } |
127 | 132 |
128 TEST_F(SpdyHttpStreamSpdy3Test, SendChunkedPost) { | 133 TEST_F(SpdyHttpStreamSpdy3Test, SendChunkedPost) { |
129 UploadDataStream::set_merge_chunks(false); | 134 set_merge_chunks(false); |
130 | 135 |
131 scoped_ptr<SpdyFrame> req(ConstructChunkedSpdyPost(NULL, 0)); | 136 scoped_ptr<SpdyFrame> req(ConstructChunkedSpdyPost(NULL, 0)); |
132 scoped_ptr<SpdyFrame> chunk1(ConstructSpdyBodyFrame(1, false)); | 137 scoped_ptr<SpdyFrame> chunk1(ConstructSpdyBodyFrame(1, false)); |
133 scoped_ptr<SpdyFrame> chunk2(ConstructSpdyBodyFrame(1, true)); | 138 scoped_ptr<SpdyFrame> chunk2(ConstructSpdyBodyFrame(1, true)); |
134 MockWrite writes[] = { | 139 MockWrite writes[] = { |
135 CreateMockWrite(*req.get(), 1), | 140 CreateMockWrite(*req.get(), 0), |
136 CreateMockWrite(*chunk1, 2), // POST upload frames | 141 CreateMockWrite(*chunk1, 1), // POST upload frames |
137 CreateMockWrite(*chunk2, 3), | 142 CreateMockWrite(*chunk2, 2), |
138 }; | 143 }; |
139 scoped_ptr<SpdyFrame> resp(ConstructSpdyPostSynReply(NULL, 0)); | 144 scoped_ptr<SpdyFrame> resp(ConstructSpdyPostSynReply(NULL, 0)); |
140 MockRead reads[] = { | 145 MockRead reads[] = { |
141 CreateMockRead(*resp, 4), | 146 CreateMockRead(*resp, 3), |
142 CreateMockRead(*chunk1, 5), | 147 CreateMockRead(*chunk1, 4), |
143 CreateMockRead(*chunk2, 5), | 148 CreateMockRead(*chunk2, 5), |
144 MockRead(SYNCHRONOUS, 0, 6) // EOF | 149 MockRead(SYNCHRONOUS, 0, 6) // EOF |
145 }; | 150 }; |
146 | 151 |
147 HostPortPair host_port_pair("www.google.com", 80); | 152 HostPortPair host_port_pair("www.google.com", 80); |
148 HostPortProxyPair pair(host_port_pair, ProxyServer::Direct()); | 153 HostPortProxyPair pair(host_port_pair, ProxyServer::Direct()); |
149 EXPECT_EQ(OK, InitSession(reads, arraysize(reads), writes, arraysize(writes), | 154 EXPECT_EQ(OK, InitSession(reads, arraysize(reads), writes, arraysize(writes), |
150 host_port_pair)); | 155 host_port_pair)); |
151 | 156 |
152 HttpRequestInfo request; | 157 HttpRequestInfo request; |
(...skipping 26 matching lines...) Expand all Loading... |
179 data()->CompleteRead(); | 184 data()->CompleteRead(); |
180 MessageLoop::current()->RunAllPending(); | 185 MessageLoop::current()->RunAllPending(); |
181 | 186 |
182 // Because we abandoned the stream, we don't expect to find a session in the | 187 // Because we abandoned the stream, we don't expect to find a session in the |
183 // pool anymore. | 188 // pool anymore. |
184 EXPECT_FALSE(http_session_->spdy_session_pool()->HasSession(pair)); | 189 EXPECT_FALSE(http_session_->spdy_session_pool()->HasSession(pair)); |
185 EXPECT_TRUE(data()->at_read_eof()); | 190 EXPECT_TRUE(data()->at_read_eof()); |
186 EXPECT_TRUE(data()->at_write_eof()); | 191 EXPECT_TRUE(data()->at_write_eof()); |
187 } | 192 } |
188 | 193 |
| 194 // Test to ensure the SpdyStream state machine does not get confused when a |
| 195 // chunk becomes available while a write is pending. |
| 196 TEST_F(SpdyHttpStreamSpdy3Test, DelayedSendChunkedPost) { |
| 197 set_merge_chunks(false); |
| 198 |
| 199 const char kUploadData1[] = "12345678"; |
| 200 const int kUploadData1Size = arraysize(kUploadData1)-1; |
| 201 scoped_ptr<SpdyFrame> req(ConstructChunkedSpdyPost(NULL, 0)); |
| 202 scoped_ptr<SpdyFrame> chunk1(ConstructSpdyBodyFrame(1, false)); |
| 203 scoped_ptr<SpdyFrame> chunk2( |
| 204 ConstructSpdyBodyFrame(1, kUploadData1, kUploadData1Size, false)); |
| 205 scoped_ptr<SpdyFrame> chunk3(ConstructSpdyBodyFrame(1, true)); |
| 206 MockWrite writes[] = { |
| 207 CreateMockWrite(*req.get(), 0), |
| 208 CreateMockWrite(*chunk1, 1), // POST upload frames |
| 209 CreateMockWrite(*chunk2, 2), |
| 210 CreateMockWrite(*chunk3, 3), |
| 211 }; |
| 212 scoped_ptr<SpdyFrame> resp(ConstructSpdyPostSynReply(NULL, 0)); |
| 213 MockRead reads[] = { |
| 214 CreateMockRead(*resp, 4), |
| 215 CreateMockRead(*chunk1, 5), |
| 216 CreateMockRead(*chunk2, 6), |
| 217 CreateMockRead(*chunk3, 7), |
| 218 MockRead(ASYNC, 0, 8) // EOF |
| 219 }; |
| 220 |
| 221 HostPortPair host_port_pair("www.google.com", 80); |
| 222 HostPortProxyPair pair(host_port_pair, ProxyServer::Direct()); |
| 223 |
| 224 scoped_ptr<DeterministicSocketData> data( |
| 225 new DeterministicSocketData(reads, arraysize(reads), |
| 226 writes, arraysize(writes))); |
| 227 |
| 228 DeterministicMockClientSocketFactory* socket_factory = |
| 229 session_deps_.deterministic_socket_factory.get(); |
| 230 socket_factory->AddSocketDataProvider(data.get()); |
| 231 |
| 232 http_session_ = SpdySessionDependencies::SpdyCreateSessionDeterministic( |
| 233 &session_deps_); |
| 234 session_ = http_session_->spdy_session_pool()->Get(pair, BoundNetLog()); |
| 235 transport_params_ = new TransportSocketParams(host_port_pair, |
| 236 MEDIUM, false, false, |
| 237 OnHostResolutionCallback()); |
| 238 |
| 239 TestCompletionCallback callback; |
| 240 scoped_ptr<ClientSocketHandle> connection(new ClientSocketHandle); |
| 241 |
| 242 EXPECT_EQ(ERR_IO_PENDING, |
| 243 connection->Init(host_port_pair.ToString(), |
| 244 transport_params_, |
| 245 MEDIUM, |
| 246 callback.callback(), |
| 247 http_session_->GetTransportSocketPool( |
| 248 HttpNetworkSession::NORMAL_SOCKET_POOL), |
| 249 BoundNetLog())); |
| 250 |
| 251 callback.WaitForResult(); |
| 252 EXPECT_EQ(OK, |
| 253 session_->InitializeWithSocket(connection.release(), false, OK)); |
| 254 |
| 255 HttpRequestInfo request; |
| 256 request.method = "POST"; |
| 257 request.url = GURL("http://www.google.com/"); |
| 258 request.upload_data = new UploadData(); |
| 259 request.upload_data->set_is_chunked(true); |
| 260 |
| 261 BoundNetLog net_log; |
| 262 scoped_ptr<SpdyHttpStream> http_stream( |
| 263 new SpdyHttpStream(session_.get(), true)); |
| 264 ASSERT_EQ(OK, |
| 265 http_stream->InitializeStream(&request, |
| 266 net_log, |
| 267 CompletionCallback())); |
| 268 |
| 269 scoped_ptr<UploadDataStream> upload_stream( |
| 270 new UploadDataStream(request.upload_data)); |
| 271 ASSERT_EQ(OK, upload_stream->Init()); |
| 272 |
| 273 request.upload_data->AppendChunk(kUploadData, kUploadDataSize, false); |
| 274 |
| 275 HttpRequestHeaders headers; |
| 276 HttpResponseInfo response; |
| 277 // This will attempt to Write() the initial request and headers, which will |
| 278 // complete asynchronously. |
| 279 EXPECT_EQ(ERR_IO_PENDING, |
| 280 http_stream->SendRequest(headers, |
| 281 upload_stream.Pass(), |
| 282 &response, |
| 283 callback.callback())); |
| 284 EXPECT_TRUE(http_session_->spdy_session_pool()->HasSession(pair)); |
| 285 |
| 286 // Complete the initial request write and the first chunk. |
| 287 data->RunFor(2); |
| 288 ASSERT_TRUE(callback.have_result()); |
| 289 EXPECT_GT(callback.WaitForResult(), 0); |
| 290 |
| 291 // Now append the final two chunks which will enqueue two more writes. |
| 292 request.upload_data->AppendChunk(kUploadData1, kUploadData1Size, false); |
| 293 request.upload_data->AppendChunk(kUploadData, kUploadDataSize, true); |
| 294 |
| 295 // Finish writing all the chunks. |
| 296 data->RunFor(2); |
| 297 |
| 298 // Read response headers. |
| 299 data->RunFor(1); |
| 300 ASSERT_EQ(OK, http_stream->ReadResponseHeaders(callback.callback())); |
| 301 |
| 302 // Read and check |chunk1| response. |
| 303 data->RunFor(1); |
| 304 scoped_refptr<IOBuffer> buf1(new IOBuffer(kUploadDataSize)); |
| 305 ASSERT_EQ(kUploadDataSize, |
| 306 http_stream->ReadResponseBody(buf1, |
| 307 kUploadDataSize, |
| 308 callback.callback())); |
| 309 EXPECT_EQ(kUploadData, std::string(buf1->data(), kUploadDataSize)); |
| 310 |
| 311 // Read and check |chunk2| response. |
| 312 data->RunFor(1); |
| 313 scoped_refptr<IOBuffer> buf2(new IOBuffer(kUploadData1Size)); |
| 314 ASSERT_EQ(kUploadData1Size, |
| 315 http_stream->ReadResponseBody(buf2, |
| 316 kUploadData1Size, |
| 317 callback.callback())); |
| 318 EXPECT_EQ(kUploadData1, std::string(buf2->data(), kUploadData1Size)); |
| 319 |
| 320 // Read and check |chunk3| response. |
| 321 data->RunFor(1); |
| 322 scoped_refptr<IOBuffer> buf3(new IOBuffer(kUploadDataSize)); |
| 323 ASSERT_EQ(kUploadDataSize, |
| 324 http_stream->ReadResponseBody(buf3, |
| 325 kUploadDataSize, |
| 326 callback.callback())); |
| 327 EXPECT_EQ(kUploadData, std::string(buf3->data(), kUploadDataSize)); |
| 328 |
| 329 // Finish reading the |EOF|. |
| 330 data->RunFor(1); |
| 331 ASSERT_TRUE(response.headers.get()); |
| 332 ASSERT_EQ(200, response.headers->response_code()); |
| 333 EXPECT_TRUE(data->at_read_eof()); |
| 334 EXPECT_TRUE(data->at_write_eof()); |
| 335 } |
| 336 |
| 337 // Test the receipt of a WINDOW_UPDATE frame while waiting for a chunk to be |
| 338 // made available is handled correctly. |
| 339 TEST_F(SpdyHttpStreamSpdy3Test, DelayedSendChunkedPostWithWindowUpdate) { |
| 340 set_merge_chunks(false); |
| 341 |
| 342 scoped_ptr<SpdyFrame> req(ConstructChunkedSpdyPost(NULL, 0)); |
| 343 scoped_ptr<SpdyFrame> chunk1(ConstructSpdyBodyFrame(1, true)); |
| 344 MockWrite writes[] = { |
| 345 CreateMockWrite(*req.get(), 0), |
| 346 CreateMockWrite(*chunk1, 1), |
| 347 }; |
| 348 scoped_ptr<SpdyFrame> resp(ConstructSpdyPostSynReply(NULL, 0)); |
| 349 scoped_ptr<SpdyFrame> window_update( |
| 350 ConstructSpdyWindowUpdate(1, kUploadDataSize)); |
| 351 MockRead reads[] = { |
| 352 CreateMockRead(*window_update, 2), |
| 353 CreateMockRead(*resp, 3), |
| 354 CreateMockRead(*chunk1, 4), |
| 355 MockRead(ASYNC, 0, 5) // EOF |
| 356 }; |
| 357 |
| 358 HostPortPair host_port_pair("www.google.com", 80); |
| 359 HostPortProxyPair pair(host_port_pair, ProxyServer::Direct()); |
| 360 |
| 361 scoped_ptr<DeterministicSocketData> data( |
| 362 new DeterministicSocketData(reads, arraysize(reads), |
| 363 writes, arraysize(writes))); |
| 364 |
| 365 DeterministicMockClientSocketFactory* socket_factory = |
| 366 session_deps_.deterministic_socket_factory.get(); |
| 367 socket_factory->AddSocketDataProvider(data.get()); |
| 368 |
| 369 http_session_ = SpdySessionDependencies::SpdyCreateSessionDeterministic( |
| 370 &session_deps_); |
| 371 session_ = http_session_->spdy_session_pool()->Get(pair, BoundNetLog()); |
| 372 transport_params_ = new TransportSocketParams(host_port_pair, |
| 373 MEDIUM, false, false, |
| 374 OnHostResolutionCallback()); |
| 375 |
| 376 TestCompletionCallback callback; |
| 377 scoped_ptr<ClientSocketHandle> connection(new ClientSocketHandle); |
| 378 |
| 379 EXPECT_EQ(ERR_IO_PENDING, |
| 380 connection->Init(host_port_pair.ToString(), |
| 381 transport_params_, |
| 382 MEDIUM, |
| 383 callback.callback(), |
| 384 http_session_->GetTransportSocketPool( |
| 385 HttpNetworkSession::NORMAL_SOCKET_POOL), |
| 386 BoundNetLog())); |
| 387 |
| 388 callback.WaitForResult(); |
| 389 EXPECT_EQ(OK, |
| 390 session_->InitializeWithSocket(connection.release(), false, OK)); |
| 391 |
| 392 HttpRequestInfo request; |
| 393 request.method = "POST"; |
| 394 request.url = GURL("http://www.google.com/"); |
| 395 request.upload_data = new UploadData(); |
| 396 request.upload_data->set_is_chunked(true); |
| 397 |
| 398 BoundNetLog net_log; |
| 399 scoped_ptr<SpdyHttpStream> http_stream( |
| 400 new SpdyHttpStream(session_.get(), true)); |
| 401 ASSERT_EQ(OK, |
| 402 http_stream->InitializeStream(&request, |
| 403 net_log, |
| 404 CompletionCallback())); |
| 405 |
| 406 scoped_ptr<UploadDataStream> upload_stream( |
| 407 new UploadDataStream(request.upload_data)); |
| 408 ASSERT_EQ(OK, upload_stream->Init()); |
| 409 |
| 410 request.upload_data->AppendChunk(kUploadData, kUploadDataSize, true); |
| 411 |
| 412 HttpRequestHeaders headers; |
| 413 HttpResponseInfo response; |
| 414 // This will attempt to Write() the initial request and headers, which will |
| 415 // complete asynchronously. |
| 416 EXPECT_EQ(ERR_IO_PENDING, |
| 417 http_stream->SendRequest(headers, |
| 418 upload_stream.Pass(), |
| 419 &response, |
| 420 callback.callback())); |
| 421 EXPECT_TRUE(http_session_->spdy_session_pool()->HasSession(pair)); |
| 422 |
| 423 // Complete the initial request write and first chunk. |
| 424 data->RunFor(2); |
| 425 ASSERT_TRUE(callback.have_result()); |
| 426 EXPECT_GT(callback.WaitForResult(), 0); |
| 427 |
| 428 // Verify that the window size has decreased. |
| 429 ASSERT_TRUE(http_stream->stream() != NULL); |
| 430 EXPECT_NE(static_cast<int>(kSpdyStreamInitialWindowSize), |
| 431 http_stream->stream()->send_window_size()); |
| 432 |
| 433 // Read window update. |
| 434 data->RunFor(1); |
| 435 |
| 436 // Verify the window update. |
| 437 ASSERT_TRUE(http_stream->stream() != NULL); |
| 438 EXPECT_EQ(static_cast<int>(kSpdyStreamInitialWindowSize), |
| 439 http_stream->stream()->send_window_size()); |
| 440 |
| 441 // Read response headers. |
| 442 data->RunFor(1); |
| 443 ASSERT_EQ(OK, http_stream->ReadResponseHeaders(callback.callback())); |
| 444 |
| 445 // Read and check |chunk1| response. |
| 446 data->RunFor(1); |
| 447 scoped_refptr<IOBuffer> buf1(new IOBuffer(kUploadDataSize)); |
| 448 ASSERT_EQ(kUploadDataSize, |
| 449 http_stream->ReadResponseBody(buf1, |
| 450 kUploadDataSize, |
| 451 callback.callback())); |
| 452 EXPECT_EQ(kUploadData, std::string(buf1->data(), kUploadDataSize)); |
| 453 |
| 454 // Finish reading the |EOF|. |
| 455 data->RunFor(1); |
| 456 ASSERT_TRUE(response.headers.get()); |
| 457 ASSERT_EQ(200, response.headers->response_code()); |
| 458 EXPECT_TRUE(data->at_read_eof()); |
| 459 EXPECT_TRUE(data->at_write_eof()); |
| 460 } |
| 461 |
189 // Test case for bug: http://code.google.com/p/chromium/issues/detail?id=50058 | 462 // Test case for bug: http://code.google.com/p/chromium/issues/detail?id=50058 |
190 TEST_F(SpdyHttpStreamSpdy3Test, SpdyURLTest) { | 463 TEST_F(SpdyHttpStreamSpdy3Test, SpdyURLTest) { |
191 const char * const full_url = "http://www.google.com/foo?query=what#anchor"; | 464 const char * const full_url = "http://www.google.com/foo?query=what#anchor"; |
192 const char * const base_url = "http://www.google.com/foo?query=what"; | 465 const char * const base_url = "http://www.google.com/foo?query=what"; |
193 scoped_ptr<SpdyFrame> req(ConstructSpdyGet(base_url, false, 1, LOWEST)); | 466 scoped_ptr<SpdyFrame> req(ConstructSpdyGet(base_url, false, 1, LOWEST)); |
194 MockWrite writes[] = { | 467 MockWrite writes[] = { |
195 CreateMockWrite(*req.get(), 1), | 468 CreateMockWrite(*req.get(), 1), |
196 }; | 469 }; |
197 scoped_ptr<SpdyFrame> resp(ConstructSpdyGetSynReply(NULL, 0, 1)); | 470 scoped_ptr<SpdyFrame> resp(ConstructSpdyGetSynReply(NULL, 0, 1)); |
198 MockRead reads[] = { | 471 MockRead reads[] = { |
(...skipping 328 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
527 | 800 |
528 sequenced_worker_pool->Shutdown(); | 801 sequenced_worker_pool->Shutdown(); |
529 } | 802 } |
530 | 803 |
531 #endif // !defined(USE_OPENSSL) | 804 #endif // !defined(USE_OPENSSL) |
532 | 805 |
533 // TODO(willchan): Write a longer test for SpdyStream that exercises all | 806 // TODO(willchan): Write a longer test for SpdyStream that exercises all |
534 // methods. | 807 // methods. |
535 | 808 |
536 } // namespace net | 809 } // namespace net |
OLD | NEW |