Chromium Code Reviews| 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" |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 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 } | 30 } |
| 31 | 31 |
| 32 virtual void TearDown() { | 32 virtual void TearDown() { |
| 33 crypto::ECSignatureCreator::SetFactoryForTesting(NULL); | 33 crypto::ECSignatureCreator::SetFactoryForTesting(NULL); |
| 34 UploadDataStream::ResetMergeChunks(); | |
| 34 MessageLoop::current()->RunAllPending(); | 35 MessageLoop::current()->RunAllPending(); |
| 35 } | 36 } |
| 36 | 37 |
| 38 void set_merge_chunks(bool merge) { | |
| 39 UploadDataStream::set_merge_chunks(merge); | |
| 40 } | |
| 41 | |
| 37 int InitSession(MockRead* reads, size_t reads_count, | 42 int InitSession(MockRead* reads, size_t reads_count, |
| 38 MockWrite* writes, size_t writes_count, | 43 MockWrite* writes, size_t writes_count, |
| 39 HostPortPair& host_port_pair) { | 44 HostPortPair& host_port_pair) { |
| 40 HostPortProxyPair pair(host_port_pair, ProxyServer::Direct()); | 45 HostPortProxyPair pair(host_port_pair, ProxyServer::Direct()); |
| 41 data_.reset(new OrderedSocketData(reads, reads_count, | 46 data_.reset(new OrderedSocketData(reads, reads_count, |
| 42 writes, writes_count)); | 47 writes, writes_count)); |
| 43 session_deps_.socket_factory->AddSocketDataProvider(data_.get()); | 48 session_deps_.socket_factory->AddSocketDataProvider(data_.get()); |
| 44 http_session_ = SpdySessionDependencies::SpdyCreateSession(&session_deps_); | 49 http_session_ = SpdySessionDependencies::SpdyCreateSession(&session_deps_); |
| 45 session_ = http_session_->spdy_session_pool()->Get(pair, BoundNetLog()); | 50 session_ = http_session_->spdy_session_pool()->Get(pair, BoundNetLog()); |
| 46 transport_params_ = new TransportSocketParams(host_port_pair, | 51 transport_params_ = new TransportSocketParams(host_port_pair, |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 111 data()->CompleteRead(); | 116 data()->CompleteRead(); |
| 112 | 117 |
| 113 // Because we abandoned the stream, we don't expect to find a session in the | 118 // Because we abandoned the stream, we don't expect to find a session in the |
| 114 // pool anymore. | 119 // pool anymore. |
| 115 EXPECT_FALSE(http_session_->spdy_session_pool()->HasSession(pair)); | 120 EXPECT_FALSE(http_session_->spdy_session_pool()->HasSession(pair)); |
| 116 EXPECT_TRUE(data()->at_read_eof()); | 121 EXPECT_TRUE(data()->at_read_eof()); |
| 117 EXPECT_TRUE(data()->at_write_eof()); | 122 EXPECT_TRUE(data()->at_write_eof()); |
| 118 } | 123 } |
| 119 | 124 |
| 120 TEST_F(SpdyHttpStreamSpdy2Test, SendChunkedPost) { | 125 TEST_F(SpdyHttpStreamSpdy2Test, SendChunkedPost) { |
| 121 UploadDataStream::set_merge_chunks(false); | 126 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 to ensure the SpdyStream state machine does not get confused when | |
| 187 // sending a request with a chunked body, where chunks become available | |
| 188 // asynchronously, over a socket where writes may also complete asynchronously. | |
|
Ryan Hamilton
2012/07/11 21:08:21
nit: ... does not get confused when a chunk becom
ramant (doing other things)
2012/07/11 22:02:21
Done.
| |
| 189 TEST_F(SpdyHttpStreamSpdy2Test, DelayedSendChunkedPost) { | |
| 190 set_merge_chunks(false); | |
| 191 | |
| 192 const char kUploadData1[] = "12345678"; | |
| 193 const int kUploadData1Size = arraysize(kUploadData1)-1; | |
| 194 scoped_ptr<SpdyFrame> req(ConstructChunkedSpdyPost(NULL, 0)); | |
| 195 scoped_ptr<SpdyFrame> chunk1(ConstructSpdyBodyFrame(1, false)); | |
| 196 scoped_ptr<SpdyFrame> chunk2( | |
| 197 ConstructSpdyBodyFrame(1, kUploadData1, kUploadData1Size, false)); | |
| 198 scoped_ptr<SpdyFrame> chunk3(ConstructSpdyBodyFrame(1, true)); | |
| 199 MockWrite writes[] = { | |
| 200 CreateMockWrite(*req.get(), 0), | |
| 201 CreateMockWrite(*chunk1, 1), // POST upload frames | |
| 202 CreateMockWrite(*chunk2, 2), | |
| 203 CreateMockWrite(*chunk3, 3), | |
| 204 }; | |
| 205 scoped_ptr<SpdyFrame> resp(ConstructSpdyPostSynReply(NULL, 0)); | |
| 206 MockRead reads[] = { | |
| 207 CreateMockRead(*resp, 4), | |
| 208 CreateMockRead(*chunk1, 5), | |
| 209 CreateMockRead(*chunk2, 6), | |
| 210 CreateMockRead(*chunk3, 7), | |
| 211 MockRead(ASYNC, 0, 8) // EOF | |
| 212 }; | |
| 213 | |
| 214 HostPortPair host_port_pair("www.google.com", 80); | |
| 215 HostPortProxyPair pair(host_port_pair, ProxyServer::Direct()); | |
| 216 | |
| 217 scoped_ptr<DeterministicSocketData> data( | |
| 218 new DeterministicSocketData(reads, arraysize(reads), | |
| 219 writes, arraysize(writes))); | |
| 220 | |
| 221 DeterministicMockClientSocketFactory* socket_factory = | |
| 222 session_deps_.deterministic_socket_factory.get(); | |
| 223 socket_factory->AddSocketDataProvider(data.get()); | |
| 224 | |
| 225 http_session_ = SpdySessionDependencies::SpdyCreateSessionDeterministic( | |
| 226 &session_deps_); | |
| 227 session_ = http_session_->spdy_session_pool()->Get(pair, BoundNetLog()); | |
| 228 transport_params_ = new TransportSocketParams(host_port_pair, | |
| 229 MEDIUM, false, false, | |
| 230 OnHostResolutionCallback()); | |
| 231 | |
| 232 TestCompletionCallback callback; | |
| 233 scoped_ptr<ClientSocketHandle> connection(new ClientSocketHandle); | |
| 234 | |
| 235 EXPECT_EQ(ERR_IO_PENDING, | |
| 236 connection->Init(host_port_pair.ToString(), | |
| 237 transport_params_, | |
| 238 MEDIUM, | |
| 239 callback.callback(), | |
| 240 http_session_->GetTransportSocketPool( | |
| 241 HttpNetworkSession::NORMAL_SOCKET_POOL), | |
| 242 BoundNetLog())); | |
| 243 | |
| 244 callback.WaitForResult(); | |
| 245 EXPECT_EQ(OK, | |
| 246 session_->InitializeWithSocket(connection.release(), false, OK)); | |
| 247 | |
| 248 HttpRequestInfo request; | |
| 249 request.method = "POST"; | |
| 250 request.url = GURL("http://www.google.com/"); | |
| 251 request.upload_data = new UploadData(); | |
| 252 request.upload_data->set_is_chunked(true); | |
| 253 | |
| 254 BoundNetLog net_log; | |
| 255 scoped_ptr<SpdyHttpStream> http_stream( | |
| 256 new SpdyHttpStream(session_.get(), true)); | |
| 257 ASSERT_EQ(OK, | |
| 258 http_stream->InitializeStream(&request, | |
| 259 net_log, | |
| 260 CompletionCallback())); | |
| 261 | |
| 262 scoped_ptr<UploadDataStream> upload_stream( | |
| 263 new UploadDataStream(request.upload_data)); | |
| 264 ASSERT_EQ(OK, upload_stream->Init()); | |
| 265 | |
| 266 request.upload_data->AppendChunk(kUploadData, kUploadDataSize, false); | |
| 267 | |
| 268 HttpRequestHeaders headers; | |
| 269 HttpResponseInfo response; | |
| 270 // This will attempt to Write() the initial request and headers, which will | |
| 271 // complete asynchronously. | |
| 272 EXPECT_EQ(ERR_IO_PENDING, | |
| 273 http_stream->SendRequest(headers, | |
| 274 upload_stream.Pass(), | |
| 275 &response, | |
| 276 callback.callback())); | |
| 277 EXPECT_TRUE(http_session_->spdy_session_pool()->HasSession(pair)); | |
| 278 | |
| 279 // Complete the initial request write and the first chunk. | |
| 280 data->RunFor(2); | |
| 281 ASSERT_TRUE(callback.have_result()); | |
| 282 EXPECT_GT(callback.WaitForResult(), 0); | |
| 283 | |
| 284 // Now append the second chunk. This will enqueue another write. | |
|
Ryan Hamilton
2012/07/11 21:08:21
nit: // Now append the final two chunks which will
ramant (doing other things)
2012/07/11 22:02:21
Done.
| |
| 285 request.upload_data->AppendChunk(kUploadData1, kUploadData1Size, false); | |
| 286 | |
| 287 // Now append final chunk. This will enqueue another write. | |
| 288 request.upload_data->AppendChunk(kUploadData, kUploadDataSize, true); | |
| 289 | |
| 290 // Finish writing all the chunks. | |
| 291 data->RunFor(2); | |
| 292 | |
| 293 // Read response headers. | |
| 294 data->RunFor(1); | |
| 295 ASSERT_EQ(OK, http_stream->ReadResponseHeaders(callback.callback())); | |
| 296 | |
| 297 // Read and check |chunk1| response. | |
| 298 data->RunFor(1); | |
| 299 scoped_refptr<IOBuffer> buf1(new IOBuffer(kUploadDataSize)); | |
| 300 int rv = http_stream->ReadResponseBody(buf1, | |
| 301 kUploadDataSize, | |
| 302 callback.callback()); | |
| 303 ASSERT_EQ(kUploadDataSize, rv); | |
|
Ryan Hamilton
2012/07/11 21:08:21
nit: I would rewrite this as:
ASSERT_EQ(kUploadDa
ramant (doing other things)
2012/07/11 22:02:21
Done.
| |
| 304 EXPECT_EQ(kUploadData, std::string(buf1->data(), kUploadDataSize)); | |
| 305 | |
| 306 // Read and check |chunk2| response. | |
| 307 data->RunFor(1); | |
| 308 scoped_refptr<IOBuffer> buf2(new IOBuffer(kUploadData1Size)); | |
| 309 rv = http_stream->ReadResponseBody(buf2, | |
| 310 kUploadData1Size, | |
| 311 callback.callback()); | |
| 312 ASSERT_EQ(kUploadData1Size, rv); | |
| 313 EXPECT_EQ(kUploadData1, std::string(buf2->data(), kUploadData1Size)); | |
| 314 | |
| 315 // Read and check |chunk3| response. | |
| 316 data->RunFor(1); | |
| 317 scoped_refptr<IOBuffer> buf3(new IOBuffer(kUploadDataSize)); | |
| 318 rv = http_stream->ReadResponseBody(buf3, | |
| 319 kUploadDataSize, | |
| 320 callback.callback()); | |
| 321 ASSERT_EQ(kUploadDataSize, rv); | |
| 322 EXPECT_EQ(kUploadData, std::string(buf3->data(), kUploadDataSize)); | |
| 323 | |
| 324 // Finish reading the |EOF|. | |
| 325 data->RunFor(1); | |
| 326 ASSERT_TRUE(response.headers.get()); | |
| 327 ASSERT_EQ(200, response.headers->response_code()); | |
| 328 EXPECT_TRUE(data->at_read_eof()); | |
| 329 EXPECT_TRUE(data->at_write_eof()); | |
| 330 } | |
| 331 | |
| 181 // Test case for bug: http://code.google.com/p/chromium/issues/detail?id=50058 | 332 // Test case for bug: http://code.google.com/p/chromium/issues/detail?id=50058 |
| 182 TEST_F(SpdyHttpStreamSpdy2Test, SpdyURLTest) { | 333 TEST_F(SpdyHttpStreamSpdy2Test, SpdyURLTest) { |
| 183 const char * const full_url = "http://www.google.com/foo?query=what#anchor"; | 334 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"; | 335 const char * const base_url = "http://www.google.com/foo?query=what"; |
| 185 scoped_ptr<SpdyFrame> req(ConstructSpdyGet(base_url, false, 1, LOWEST)); | 336 scoped_ptr<SpdyFrame> req(ConstructSpdyGet(base_url, false, 1, LOWEST)); |
| 186 MockWrite writes[] = { | 337 MockWrite writes[] = { |
| 187 CreateMockWrite(*req.get(), 1), | 338 CreateMockWrite(*req.get(), 1), |
| 188 }; | 339 }; |
| 189 scoped_ptr<SpdyFrame> resp(ConstructSpdyGetSynReply(NULL, 0, 1)); | 340 scoped_ptr<SpdyFrame> resp(ConstructSpdyGetSynReply(NULL, 0, 1)); |
| 190 MockRead reads[] = { | 341 MockRead reads[] = { |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 231 // pool anymore. | 382 // pool anymore. |
| 232 EXPECT_FALSE(http_session_->spdy_session_pool()->HasSession(pair)); | 383 EXPECT_FALSE(http_session_->spdy_session_pool()->HasSession(pair)); |
| 233 EXPECT_TRUE(data()->at_read_eof()); | 384 EXPECT_TRUE(data()->at_read_eof()); |
| 234 EXPECT_TRUE(data()->at_write_eof()); | 385 EXPECT_TRUE(data()->at_write_eof()); |
| 235 } | 386 } |
| 236 | 387 |
| 237 // TODO(willchan): Write a longer test for SpdyStream that exercises all | 388 // TODO(willchan): Write a longer test for SpdyStream that exercises all |
| 238 // methods. | 389 // methods. |
| 239 | 390 |
| 240 } // namespace net | 391 } // namespace net |
| OLD | NEW |