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