OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "content/browser/loader/url_loader_factory_impl.h" | 5 #include "content/browser/loader/url_loader_factory_impl.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 | 8 |
9 #include <memory> | 9 #include <memory> |
10 #include <string> | 10 #include <string> |
(...skipping 15 matching lines...) Expand all Loading... |
26 #include "content/browser/loader/resource_message_filter.h" | 26 #include "content/browser/loader/resource_message_filter.h" |
27 #include "content/browser/loader/resource_request_info_impl.h" | 27 #include "content/browser/loader/resource_request_info_impl.h" |
28 #include "content/browser/loader/test_url_loader_client.h" | 28 #include "content/browser/loader/test_url_loader_client.h" |
29 #include "content/browser/loader_delegate_impl.h" | 29 #include "content/browser/loader_delegate_impl.h" |
30 #include "content/common/resource_request.h" | 30 #include "content/common/resource_request.h" |
31 #include "content/common/resource_request_completion_status.h" | 31 #include "content/common/resource_request_completion_status.h" |
32 #include "content/common/url_loader.mojom.h" | 32 #include "content/common/url_loader.mojom.h" |
33 #include "content/public/browser/resource_context.h" | 33 #include "content/public/browser/resource_context.h" |
34 #include "content/public/browser/resource_dispatcher_host_delegate.h" | 34 #include "content/public/browser/resource_dispatcher_host_delegate.h" |
35 #include "content/public/common/content_paths.h" | 35 #include "content/public/common/content_paths.h" |
36 #include "content/public/common/process_type.h" | |
37 #include "content/public/test/test_browser_context.h" | 36 #include "content/public/test/test_browser_context.h" |
38 #include "content/public/test/test_browser_thread_bundle.h" | 37 #include "content/public/test/test_browser_thread_bundle.h" |
39 #include "mojo/public/c/system/data_pipe.h" | 38 #include "mojo/public/c/system/data_pipe.h" |
40 #include "mojo/public/c/system/types.h" | 39 #include "mojo/public/c/system/types.h" |
41 #include "mojo/public/cpp/bindings/binding.h" | 40 #include "mojo/public/cpp/bindings/binding.h" |
42 #include "mojo/public/cpp/system/data_pipe.h" | 41 #include "mojo/public/cpp/system/data_pipe.h" |
43 #include "net/base/io_buffer.h" | 42 #include "net/base/io_buffer.h" |
44 #include "net/base/net_errors.h" | 43 #include "net/base/net_errors.h" |
45 #include "net/http/http_response_headers.h" | 44 #include "net/http/http_response_headers.h" |
46 #include "net/http/http_response_info.h" | 45 #include "net/http/http_response_info.h" |
47 #include "net/http/http_status_code.h" | 46 #include "net/http/http_status_code.h" |
48 #include "net/http/http_util.h" | 47 #include "net/http/http_util.h" |
49 #include "net/test/url_request/url_request_failed_job.h" | 48 #include "net/test/url_request/url_request_failed_job.h" |
50 #include "net/test/url_request/url_request_mock_http_job.h" | 49 #include "net/test/url_request/url_request_mock_http_job.h" |
51 #include "net/test/url_request/url_request_slow_download_job.h" | 50 #include "net/test/url_request/url_request_slow_download_job.h" |
52 #include "net/url_request/url_request_filter.h" | 51 #include "net/url_request/url_request_filter.h" |
53 #include "testing/gtest/include/gtest/gtest.h" | 52 #include "testing/gtest/include/gtest/gtest.h" |
54 #include "url/gurl.h" | 53 #include "url/gurl.h" |
| 54 #include "url/origin.h" |
55 | 55 |
56 namespace content { | 56 namespace content { |
57 | 57 |
58 namespace { | 58 namespace { |
59 | 59 |
60 constexpr int kChildId = 99; | 60 constexpr int kChildId = 99; |
61 | 61 |
62 class RejectingResourceDispatcherHostDelegate final | 62 class RejectingResourceDispatcherHostDelegate final |
63 : public ResourceDispatcherHostDelegate { | 63 : public ResourceDispatcherHostDelegate { |
64 public: | 64 public: |
(...skipping 11 matching lines...) Expand all Loading... |
76 // The test parameter is the number of bytes allocated for the buffer in the | 76 // The test parameter is the number of bytes allocated for the buffer in the |
77 // data pipe, for testing the case where the allocated size is smaller than the | 77 // data pipe, for testing the case where the allocated size is smaller than the |
78 // size the mime sniffer *implicitly* requires. | 78 // size the mime sniffer *implicitly* requires. |
79 class URLLoaderFactoryImplTest : public ::testing::TestWithParam<size_t> { | 79 class URLLoaderFactoryImplTest : public ::testing::TestWithParam<size_t> { |
80 public: | 80 public: |
81 URLLoaderFactoryImplTest() | 81 URLLoaderFactoryImplTest() |
82 : thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP), | 82 : thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP), |
83 browser_context_(new TestBrowserContext()), | 83 browser_context_(new TestBrowserContext()), |
84 resource_message_filter_(new ResourceMessageFilter( | 84 resource_message_filter_(new ResourceMessageFilter( |
85 kChildId, | 85 kChildId, |
86 // If browser side navigation is enabled then | |
87 // ResourceDispatcherHostImpl prevents main frame URL requests from | |
88 // the renderer. Ensure that these checks don't trip us up by | |
89 // setting the process type in ResourceMessageFilter as | |
90 // PROCESS_TYPE_UNKNOWN. | |
91 PROCESS_TYPE_UNKNOWN, | |
92 nullptr, | 86 nullptr, |
93 nullptr, | 87 nullptr, |
94 nullptr, | 88 nullptr, |
95 nullptr, | 89 nullptr, |
96 base::Bind(&URLLoaderFactoryImplTest::GetContexts, | 90 base::Bind(&URLLoaderFactoryImplTest::GetContexts, |
97 base::Unretained(this)))) { | 91 base::Unretained(this)))) { |
| 92 resource_message_filter_->InitializeForTest(); |
98 MojoAsyncResourceHandler::SetAllocationSizeForTesting(GetParam()); | 93 MojoAsyncResourceHandler::SetAllocationSizeForTesting(GetParam()); |
99 rdh_.SetLoaderDelegate(&loader_deleate_); | 94 rdh_.SetLoaderDelegate(&loader_deleate_); |
100 | 95 |
101 URLLoaderFactoryImpl::Create(resource_message_filter_, | 96 URLLoaderFactoryImpl::Create( |
102 mojo::GetProxy(&factory_)); | 97 resource_message_filter_->requester_info_for_test(), |
| 98 mojo::GetProxy(&factory_)); |
103 | 99 |
104 // Calling this function creates a request context. | 100 // Calling this function creates a request context. |
105 browser_context_->GetResourceContext()->GetRequestContext(); | 101 browser_context_->GetResourceContext()->GetRequestContext(); |
106 base::RunLoop().RunUntilIdle(); | 102 base::RunLoop().RunUntilIdle(); |
107 } | 103 } |
108 | 104 |
109 ~URLLoaderFactoryImplTest() override { | 105 ~URLLoaderFactoryImplTest() override { |
110 rdh_.SetDelegate(nullptr); | 106 rdh_.SetDelegate(nullptr); |
111 net::URLRequestFilter::GetInstance()->ClearHandlers(); | 107 net::URLRequestFilter::GetInstance()->ClearHandlers(); |
112 | 108 |
(...skipping 29 matching lines...) Expand all Loading... |
142 mojom::URLLoaderAssociatedPtr loader; | 138 mojom::URLLoaderAssociatedPtr loader; |
143 base::FilePath root; | 139 base::FilePath root; |
144 PathService::Get(DIR_TEST_DATA, &root); | 140 PathService::Get(DIR_TEST_DATA, &root); |
145 net::URLRequestMockHTTPJob::AddUrlHandlers(root, | 141 net::URLRequestMockHTTPJob::AddUrlHandlers(root, |
146 BrowserThread::GetBlockingPool()); | 142 BrowserThread::GetBlockingPool()); |
147 ResourceRequest request; | 143 ResourceRequest request; |
148 TestURLLoaderClient client; | 144 TestURLLoaderClient client; |
149 // Assume the file contents is small enough to be stored in the data pipe. | 145 // Assume the file contents is small enough to be stored in the data pipe. |
150 request.url = net::URLRequestMockHTTPJob::GetMockUrl("hello.html"); | 146 request.url = net::URLRequestMockHTTPJob::GetMockUrl("hello.html"); |
151 request.method = "GET"; | 147 request.method = "GET"; |
152 request.is_main_frame = true; | 148 // |resource_type| can't be a frame type. It is because when PlzNavigate is |
| 149 // enabled, the url scheme of frame type requests from the renderer process |
| 150 // must be blob scheme. |
| 151 request.resource_type = RESOURCE_TYPE_XHR; |
| 152 // Need to set |request_initiator| for non main frame type request. |
| 153 request.request_initiator = url::Origin(); |
153 factory_->CreateLoaderAndStart( | 154 factory_->CreateLoaderAndStart( |
154 mojo::GetProxy(&loader, factory_.associated_group()), kRoutingId, | 155 mojo::GetProxy(&loader, factory_.associated_group()), kRoutingId, |
155 kRequestId, request, | 156 kRequestId, request, |
156 client.CreateRemoteAssociatedPtrInfo(factory_.associated_group())); | 157 client.CreateRemoteAssociatedPtrInfo(factory_.associated_group())); |
157 | 158 |
158 ASSERT_FALSE(client.has_received_response()); | 159 ASSERT_FALSE(client.has_received_response()); |
159 ASSERT_FALSE(client.response_body().is_valid()); | 160 ASSERT_FALSE(client.response_body().is_valid()); |
160 ASSERT_FALSE(client.has_received_completion()); | 161 ASSERT_FALSE(client.has_received_completion()); |
161 | 162 |
162 client.RunUntilResponseReceived(); | 163 client.RunUntilResponseReceived(); |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
211 | 212 |
212 TEST_P(URLLoaderFactoryImplTest, GetFailedResponse) { | 213 TEST_P(URLLoaderFactoryImplTest, GetFailedResponse) { |
213 NavigationResourceThrottle::set_ui_checks_always_succeed_for_testing(true); | 214 NavigationResourceThrottle::set_ui_checks_always_succeed_for_testing(true); |
214 mojom::URLLoaderAssociatedPtr loader; | 215 mojom::URLLoaderAssociatedPtr loader; |
215 ResourceRequest request; | 216 ResourceRequest request; |
216 TestURLLoaderClient client; | 217 TestURLLoaderClient client; |
217 net::URLRequestFailedJob::AddUrlHandler(); | 218 net::URLRequestFailedJob::AddUrlHandler(); |
218 request.url = net::URLRequestFailedJob::GetMockHttpUrlWithFailurePhase( | 219 request.url = net::URLRequestFailedJob::GetMockHttpUrlWithFailurePhase( |
219 net::URLRequestFailedJob::START, net::ERR_TIMED_OUT); | 220 net::URLRequestFailedJob::START, net::ERR_TIMED_OUT); |
220 request.method = "GET"; | 221 request.method = "GET"; |
| 222 // |resource_type| can't be a frame type. It is because when PlzNavigate is |
| 223 // enabled, the url scheme of frame type requests from the renderer process |
| 224 // must be blob scheme. |
| 225 request.resource_type = RESOURCE_TYPE_XHR; |
| 226 // Need to set |request_initiator| for non main frame type request. |
| 227 request.request_initiator = url::Origin(); |
221 factory_->CreateLoaderAndStart( | 228 factory_->CreateLoaderAndStart( |
222 mojo::GetProxy(&loader, factory_.associated_group()), 2, 1, request, | 229 mojo::GetProxy(&loader, factory_.associated_group()), 2, 1, request, |
223 client.CreateRemoteAssociatedPtrInfo(factory_.associated_group())); | 230 client.CreateRemoteAssociatedPtrInfo(factory_.associated_group())); |
224 | 231 |
225 client.RunUntilComplete(); | 232 client.RunUntilComplete(); |
226 ASSERT_FALSE(client.has_received_response()); | 233 ASSERT_FALSE(client.has_received_response()); |
227 ASSERT_FALSE(client.response_body().is_valid()); | 234 ASSERT_FALSE(client.response_body().is_valid()); |
228 | 235 |
229 EXPECT_EQ(net::ERR_TIMED_OUT, client.completion_status().error_code); | 236 EXPECT_EQ(net::ERR_TIMED_OUT, client.completion_status().error_code); |
230 EXPECT_EQ(0, client.completion_status().encoded_data_length); | 237 EXPECT_EQ(0, client.completion_status().encoded_data_length); |
231 EXPECT_EQ(0, client.completion_status().encoded_body_length); | 238 EXPECT_EQ(0, client.completion_status().encoded_body_length); |
232 } | 239 } |
233 | 240 |
234 // In this case, the loading fails after receiving a response. | 241 // In this case, the loading fails after receiving a response. |
235 TEST_P(URLLoaderFactoryImplTest, GetFailedResponse2) { | 242 TEST_P(URLLoaderFactoryImplTest, GetFailedResponse2) { |
236 NavigationResourceThrottle::set_ui_checks_always_succeed_for_testing(true); | 243 NavigationResourceThrottle::set_ui_checks_always_succeed_for_testing(true); |
237 mojom::URLLoaderAssociatedPtr loader; | 244 mojom::URLLoaderAssociatedPtr loader; |
238 ResourceRequest request; | 245 ResourceRequest request; |
239 TestURLLoaderClient client; | 246 TestURLLoaderClient client; |
240 net::URLRequestFailedJob::AddUrlHandler(); | 247 net::URLRequestFailedJob::AddUrlHandler(); |
241 request.url = net::URLRequestFailedJob::GetMockHttpUrlWithFailurePhase( | 248 request.url = net::URLRequestFailedJob::GetMockHttpUrlWithFailurePhase( |
242 net::URLRequestFailedJob::READ_ASYNC, net::ERR_TIMED_OUT); | 249 net::URLRequestFailedJob::READ_ASYNC, net::ERR_TIMED_OUT); |
243 request.method = "GET"; | 250 request.method = "GET"; |
244 request.is_main_frame = true; | 251 // |resource_type| can't be a frame type. It is because when PlzNavigate is |
| 252 // enabled, the url scheme of frame type requests from the renderer process |
| 253 // must be blob scheme. |
| 254 request.resource_type = RESOURCE_TYPE_XHR; |
| 255 // Need to set |request_initiator| for non main frame type request. |
| 256 request.request_initiator = url::Origin(); |
245 factory_->CreateLoaderAndStart( | 257 factory_->CreateLoaderAndStart( |
246 mojo::GetProxy(&loader, factory_.associated_group()), 2, 1, request, | 258 mojo::GetProxy(&loader, factory_.associated_group()), 2, 1, request, |
247 client.CreateRemoteAssociatedPtrInfo(factory_.associated_group())); | 259 client.CreateRemoteAssociatedPtrInfo(factory_.associated_group())); |
248 | 260 |
249 client.RunUntilComplete(); | 261 client.RunUntilComplete(); |
250 ASSERT_FALSE(client.has_received_response()); | 262 ASSERT_FALSE(client.has_received_response()); |
251 ASSERT_TRUE(client.response_body().is_valid()); | 263 ASSERT_TRUE(client.response_body().is_valid()); |
252 | 264 |
253 EXPECT_EQ(net::ERR_TIMED_OUT, client.completion_status().error_code); | 265 EXPECT_EQ(net::ERR_TIMED_OUT, client.completion_status().error_code); |
254 EXPECT_GT(client.completion_status().encoded_data_length, 0); | 266 EXPECT_GT(client.completion_status().encoded_data_length, 0); |
255 EXPECT_EQ(0, client.completion_status().encoded_body_length); | 267 EXPECT_EQ(0, client.completion_status().encoded_body_length); |
256 } | 268 } |
257 | 269 |
258 // This test tests a case where resource loading is cancelled before started. | 270 // This test tests a case where resource loading is cancelled before started. |
259 TEST_P(URLLoaderFactoryImplTest, InvalidURL) { | 271 TEST_P(URLLoaderFactoryImplTest, InvalidURL) { |
260 mojom::URLLoaderAssociatedPtr loader; | 272 mojom::URLLoaderAssociatedPtr loader; |
261 ResourceRequest request; | 273 ResourceRequest request; |
262 TestURLLoaderClient client; | 274 TestURLLoaderClient client; |
263 request.url = GURL(); | 275 request.url = GURL(); |
264 request.method = "GET"; | 276 request.method = "GET"; |
| 277 // |resource_type| can't be a frame type. It is because when PlzNavigate is |
| 278 // enabled, the url scheme of frame type requests from the renderer process |
| 279 // must be blob scheme. |
| 280 request.resource_type = RESOURCE_TYPE_XHR; |
| 281 // Need to set |request_initiator| for non main frame type request. |
| 282 request.request_initiator = url::Origin(); |
265 ASSERT_FALSE(request.url.is_valid()); | 283 ASSERT_FALSE(request.url.is_valid()); |
266 factory_->CreateLoaderAndStart( | 284 factory_->CreateLoaderAndStart( |
267 mojo::GetProxy(&loader, factory_.associated_group()), 2, 1, request, | 285 mojo::GetProxy(&loader, factory_.associated_group()), 2, 1, request, |
268 client.CreateRemoteAssociatedPtrInfo(factory_.associated_group())); | 286 client.CreateRemoteAssociatedPtrInfo(factory_.associated_group())); |
269 | 287 |
270 client.RunUntilComplete(); | 288 client.RunUntilComplete(); |
271 ASSERT_FALSE(client.has_received_response()); | 289 ASSERT_FALSE(client.has_received_response()); |
272 ASSERT_FALSE(client.response_body().is_valid()); | 290 ASSERT_FALSE(client.response_body().is_valid()); |
273 | 291 |
274 EXPECT_EQ(net::ERR_ABORTED, client.completion_status().error_code); | 292 EXPECT_EQ(net::ERR_ABORTED, client.completion_status().error_code); |
275 } | 293 } |
276 | 294 |
277 // This test tests a case where resource loading is cancelled before started. | 295 // This test tests a case where resource loading is cancelled before started. |
278 TEST_P(URLLoaderFactoryImplTest, ShouldNotRequestURL) { | 296 TEST_P(URLLoaderFactoryImplTest, ShouldNotRequestURL) { |
279 mojom::URLLoaderAssociatedPtr loader; | 297 mojom::URLLoaderAssociatedPtr loader; |
280 RejectingResourceDispatcherHostDelegate rdh_delegate; | 298 RejectingResourceDispatcherHostDelegate rdh_delegate; |
281 rdh_.SetDelegate(&rdh_delegate); | 299 rdh_.SetDelegate(&rdh_delegate); |
282 ResourceRequest request; | 300 ResourceRequest request; |
283 TestURLLoaderClient client; | 301 TestURLLoaderClient client; |
284 request.url = GURL("http://localhost/"); | 302 request.url = GURL("http://localhost/"); |
285 request.method = "GET"; | 303 request.method = "GET"; |
| 304 // |resource_type| can't be a frame type. It is because when PlzNavigate is |
| 305 // enabled, the url scheme of frame type requests from the renderer process |
| 306 // must be blob scheme. |
| 307 request.resource_type = RESOURCE_TYPE_XHR; |
| 308 // Need to set |request_initiator| for non main frame type request. |
| 309 request.request_initiator = url::Origin(); |
286 factory_->CreateLoaderAndStart( | 310 factory_->CreateLoaderAndStart( |
287 mojo::GetProxy(&loader, factory_.associated_group()), 2, 1, request, | 311 mojo::GetProxy(&loader, factory_.associated_group()), 2, 1, request, |
288 client.CreateRemoteAssociatedPtrInfo(factory_.associated_group())); | 312 client.CreateRemoteAssociatedPtrInfo(factory_.associated_group())); |
289 | 313 |
290 client.RunUntilComplete(); | 314 client.RunUntilComplete(); |
291 rdh_.SetDelegate(nullptr); | 315 rdh_.SetDelegate(nullptr); |
292 | 316 |
293 ASSERT_FALSE(client.has_received_response()); | 317 ASSERT_FALSE(client.has_received_response()); |
294 ASSERT_FALSE(client.response_body().is_valid()); | 318 ASSERT_FALSE(client.response_body().is_valid()); |
295 | 319 |
(...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
431 mojom::URLLoaderAssociatedPtr loader; | 455 mojom::URLLoaderAssociatedPtr loader; |
432 base::FilePath root; | 456 base::FilePath root; |
433 PathService::Get(DIR_TEST_DATA, &root); | 457 PathService::Get(DIR_TEST_DATA, &root); |
434 net::URLRequestFailedJob::AddUrlHandler(); | 458 net::URLRequestFailedJob::AddUrlHandler(); |
435 ResourceRequest request; | 459 ResourceRequest request; |
436 TestURLLoaderClient client; | 460 TestURLLoaderClient client; |
437 // Assume the file contents is small enough to be stored in the data pipe. | 461 // Assume the file contents is small enough to be stored in the data pipe. |
438 request.url = net::URLRequestFailedJob::GetMockHttpUrl(net::ERR_IO_PENDING); | 462 request.url = net::URLRequestFailedJob::GetMockHttpUrl(net::ERR_IO_PENDING); |
439 request.method = "GET"; | 463 request.method = "GET"; |
440 request.is_main_frame = true; | 464 request.is_main_frame = true; |
| 465 // |resource_type| can't be a frame type. It is because when PlzNavigate is |
| 466 // enabled, the url scheme of frame type requests from the renderer process |
| 467 // must be blob scheme. |
| 468 request.resource_type = RESOURCE_TYPE_XHR; |
| 469 // Need to set |request_initiator| for non main frame type request. |
| 470 request.request_initiator = url::Origin(); |
441 factory_->CreateLoaderAndStart( | 471 factory_->CreateLoaderAndStart( |
442 mojo::GetProxy(&loader, factory_.associated_group()), kRoutingId, | 472 mojo::GetProxy(&loader, factory_.associated_group()), kRoutingId, |
443 kRequestId, request, | 473 kRequestId, request, |
444 client.CreateRemoteAssociatedPtrInfo(factory_.associated_group())); | 474 client.CreateRemoteAssociatedPtrInfo(factory_.associated_group())); |
445 | 475 |
446 base::RunLoop().RunUntilIdle(); | 476 base::RunLoop().RunUntilIdle(); |
447 ASSERT_TRUE(rdh_.GetURLRequest(GlobalRequestID(kChildId, kRequestId))); | 477 ASSERT_TRUE(rdh_.GetURLRequest(GlobalRequestID(kChildId, kRequestId))); |
448 ASSERT_FALSE(client.has_received_response()); | 478 ASSERT_FALSE(client.has_received_response()); |
449 ASSERT_FALSE(client.response_body().is_valid()); | 479 ASSERT_FALSE(client.response_body().is_valid()); |
450 ASSERT_FALSE(client.has_received_completion()); | 480 ASSERT_FALSE(client.has_received_completion()); |
451 | 481 |
452 loader = nullptr; | 482 loader = nullptr; |
453 base::RunLoop().RunUntilIdle(); | 483 base::RunLoop().RunUntilIdle(); |
454 ASSERT_FALSE(rdh_.GetURLRequest(GlobalRequestID(kChildId, kRequestId))); | 484 ASSERT_FALSE(rdh_.GetURLRequest(GlobalRequestID(kChildId, kRequestId))); |
455 } | 485 } |
456 | 486 |
457 INSTANTIATE_TEST_CASE_P(URLLoaderFactoryImplTest, | 487 INSTANTIATE_TEST_CASE_P(URLLoaderFactoryImplTest, |
458 URLLoaderFactoryImplTest, | 488 URLLoaderFactoryImplTest, |
459 ::testing::Values(128, 32 * 1024)); | 489 ::testing::Values(128, 32 * 1024)); |
460 | 490 |
461 } // namespace | 491 } // namespace |
462 | 492 |
463 } // namespace content | 493 } // namespace content |
OLD | NEW |