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> |
11 #include <utility> | 11 #include <utility> |
12 | 12 |
13 #include "base/bind.h" | 13 #include "base/bind.h" |
14 #include "base/callback.h" | 14 #include "base/callback.h" |
15 #include "base/files/file_path.h" | 15 #include "base/files/file_path.h" |
16 #include "base/files/file_util.h" | 16 #include "base/files/file_util.h" |
17 #include "base/location.h" | 17 #include "base/location.h" |
18 #include "base/memory/ptr_util.h" | 18 #include "base/memory/ptr_util.h" |
19 #include "base/memory/weak_ptr.h" | 19 #include "base/memory/weak_ptr.h" |
20 #include "base/message_loop/message_loop.h" | 20 #include "base/message_loop/message_loop.h" |
21 #include "base/path_service.h" | 21 #include "base/path_service.h" |
22 #include "base/run_loop.h" | 22 #include "base/run_loop.h" |
23 #include "content/browser/child_process_security_policy_impl.h" | |
23 #include "content/browser/loader/mojo_async_resource_handler.h" | 24 #include "content/browser/loader/mojo_async_resource_handler.h" |
24 #include "content/browser/loader/navigation_resource_throttle.h" | 25 #include "content/browser/loader/navigation_resource_throttle.h" |
25 #include "content/browser/loader/resource_dispatcher_host_impl.h" | 26 #include "content/browser/loader/resource_dispatcher_host_impl.h" |
26 #include "content/browser/loader/resource_message_filter.h" | 27 #include "content/browser/loader/resource_message_filter.h" |
27 #include "content/browser/loader/resource_request_info_impl.h" | 28 #include "content/browser/loader/resource_request_info_impl.h" |
28 #include "content/browser/loader/test_url_loader_client.h" | 29 #include "content/browser/loader/test_url_loader_client.h" |
29 #include "content/browser/loader_delegate_impl.h" | 30 #include "content/browser/loader_delegate_impl.h" |
30 #include "content/common/resource_request.h" | 31 #include "content/common/resource_request.h" |
31 #include "content/common/resource_request_completion_status.h" | 32 #include "content/common/resource_request_completion_status.h" |
32 #include "content/common/url_loader.mojom.h" | 33 #include "content/common/url_loader.mojom.h" |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
82 : thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP), | 83 : thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP), |
83 browser_context_(new TestBrowserContext()), | 84 browser_context_(new TestBrowserContext()), |
84 resource_message_filter_(new ResourceMessageFilter( | 85 resource_message_filter_(new ResourceMessageFilter( |
85 kChildId, | 86 kChildId, |
86 nullptr, | 87 nullptr, |
87 nullptr, | 88 nullptr, |
88 nullptr, | 89 nullptr, |
89 nullptr, | 90 nullptr, |
90 base::Bind(&URLLoaderFactoryImplTest::GetContexts, | 91 base::Bind(&URLLoaderFactoryImplTest::GetContexts, |
91 base::Unretained(this)))) { | 92 base::Unretained(this)))) { |
93 // Some tests specify request.report_raw_headers, but the RDH checks the | |
94 // CanReadRawCookies permission before enabling it. | |
95 ChildProcessSecurityPolicyImpl::GetInstance()->Add(kChildId); | |
96 ChildProcessSecurityPolicyImpl::GetInstance()->GrantReadRawCookies( | |
97 kChildId); | |
98 | |
92 resource_message_filter_->InitializeForTest(); | 99 resource_message_filter_->InitializeForTest(); |
93 MojoAsyncResourceHandler::SetAllocationSizeForTesting(GetParam()); | 100 MojoAsyncResourceHandler::SetAllocationSizeForTesting(GetParam()); |
94 rdh_.SetLoaderDelegate(&loader_deleate_); | 101 rdh_.SetLoaderDelegate(&loader_deleate_); |
95 | 102 |
96 URLLoaderFactoryImpl::Create( | 103 URLLoaderFactoryImpl::Create( |
97 resource_message_filter_->requester_info_for_test(), | 104 resource_message_filter_->requester_info_for_test(), |
98 mojo::GetProxy(&factory_)); | 105 mojo::GetProxy(&factory_)); |
99 | 106 |
100 // Calling this function creates a request context. | 107 // Calling this function creates a request context. |
101 browser_context_->GetResourceContext()->GetRequestContext(); | 108 browser_context_->GetResourceContext()->GetRequestContext(); |
102 base::RunLoop().RunUntilIdle(); | 109 base::RunLoop().RunUntilIdle(); |
103 } | 110 } |
104 | 111 |
105 ~URLLoaderFactoryImplTest() override { | 112 ~URLLoaderFactoryImplTest() override { |
113 ChildProcessSecurityPolicyImpl::GetInstance()->Remove(kChildId); | |
106 rdh_.SetDelegate(nullptr); | 114 rdh_.SetDelegate(nullptr); |
107 net::URLRequestFilter::GetInstance()->ClearHandlers(); | 115 net::URLRequestFilter::GetInstance()->ClearHandlers(); |
108 | 116 |
109 resource_message_filter_->OnChannelClosing(); | 117 resource_message_filter_->OnChannelClosing(); |
110 rdh_.CancelRequestsForProcess(resource_message_filter_->child_id()); | 118 rdh_.CancelRequestsForProcess(resource_message_filter_->child_id()); |
111 base::RunLoop().RunUntilIdle(); | 119 base::RunLoop().RunUntilIdle(); |
112 MojoAsyncResourceHandler::SetAllocationSizeForTesting( | 120 MojoAsyncResourceHandler::SetAllocationSizeForTesting( |
113 MojoAsyncResourceHandler::kDefaultAllocationSize); | 121 MojoAsyncResourceHandler::kDefaultAllocationSize); |
114 } | 122 } |
115 | 123 |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
197 break; | 205 break; |
198 if (r == MOJO_RESULT_SHOULD_WAIT) | 206 if (r == MOJO_RESULT_SHOULD_WAIT) |
199 continue; | 207 continue; |
200 ASSERT_EQ(MOJO_RESULT_OK, r); | 208 ASSERT_EQ(MOJO_RESULT_OK, r); |
201 contents += std::string(buffer, read_size); | 209 contents += std::string(buffer, read_size); |
202 } | 210 } |
203 std::string expected; | 211 std::string expected; |
204 base::ReadFileToString( | 212 base::ReadFileToString( |
205 root.Append(base::FilePath(FILE_PATH_LITERAL("hello.html"))), &expected); | 213 root.Append(base::FilePath(FILE_PATH_LITERAL("hello.html"))), &expected); |
206 EXPECT_EQ(expected, contents); | 214 EXPECT_EQ(expected, contents); |
207 EXPECT_EQ(static_cast<int64_t>(expected.size()), | 215 EXPECT_EQ(static_cast<int64_t>(expected.size()) + |
216 client.response_head().encoded_data_length, | |
208 client.completion_status().encoded_data_length); | 217 client.completion_status().encoded_data_length); |
209 EXPECT_EQ(static_cast<int64_t>(expected.size()), | 218 EXPECT_EQ(static_cast<int64_t>(expected.size()), |
210 client.completion_status().encoded_body_length); | 219 client.completion_status().encoded_body_length); |
220 // OnTransferSizeUpdated is not dispatched as report_raw_headers is not set. | |
221 EXPECT_EQ(0, client.body_transfer_size()); | |
222 EXPECT_GT(client.response_head().encoded_data_length, 0); | |
223 EXPECT_GT(client.completion_status().encoded_data_length, 0); | |
211 } | 224 } |
212 | 225 |
213 TEST_P(URLLoaderFactoryImplTest, GetFailedResponse) { | 226 TEST_P(URLLoaderFactoryImplTest, GetFailedResponse) { |
214 NavigationResourceThrottle::set_ui_checks_always_succeed_for_testing(true); | 227 NavigationResourceThrottle::set_ui_checks_always_succeed_for_testing(true); |
215 mojom::URLLoaderAssociatedPtr loader; | 228 mojom::URLLoaderAssociatedPtr loader; |
216 ResourceRequest request; | 229 ResourceRequest request; |
217 TestURLLoaderClient client; | 230 TestURLLoaderClient client; |
218 net::URLRequestFailedJob::AddUrlHandler(); | 231 net::URLRequestFailedJob::AddUrlHandler(); |
219 request.url = net::URLRequestFailedJob::GetMockHttpUrlWithFailurePhase( | 232 request.url = net::URLRequestFailedJob::GetMockHttpUrlWithFailurePhase( |
220 net::URLRequestFailedJob::START, net::ERR_TIMED_OUT); | 233 net::URLRequestFailedJob::START, net::ERR_TIMED_OUT); |
(...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
375 | 388 |
376 EXPECT_EQ(static_cast<int64_t>(contents.size()), | 389 EXPECT_EQ(static_cast<int64_t>(contents.size()), |
377 client.download_data_length()); | 390 client.download_data_length()); |
378 EXPECT_EQ(static_cast<int64_t>(contents.size()), | 391 EXPECT_EQ(static_cast<int64_t>(contents.size()), |
379 client.encoded_download_data_length()); | 392 client.encoded_download_data_length()); |
380 | 393 |
381 std::string expected; | 394 std::string expected; |
382 base::ReadFileToString( | 395 base::ReadFileToString( |
383 root.Append(base::FilePath(FILE_PATH_LITERAL("hello.html"))), &expected); | 396 root.Append(base::FilePath(FILE_PATH_LITERAL("hello.html"))), &expected); |
384 EXPECT_EQ(expected, contents); | 397 EXPECT_EQ(expected, contents); |
385 EXPECT_EQ(static_cast<int64_t>(expected.size()), | 398 EXPECT_EQ(static_cast<int64_t>(expected.size()) + |
399 client.response_head().encoded_data_length, | |
386 client.completion_status().encoded_data_length); | 400 client.completion_status().encoded_data_length); |
387 EXPECT_EQ(static_cast<int64_t>(expected.size()), | 401 EXPECT_EQ(static_cast<int64_t>(expected.size()), |
388 client.completion_status().encoded_body_length); | 402 client.completion_status().encoded_body_length); |
389 } | 403 } |
390 | 404 |
391 TEST_P(URLLoaderFactoryImplTest, DownloadToFileFailure) { | 405 TEST_P(URLLoaderFactoryImplTest, DownloadToFileFailure) { |
392 constexpr int32_t kRoutingId = 1; | 406 constexpr int32_t kRoutingId = 1; |
393 constexpr int32_t kRequestId = 2; | 407 constexpr int32_t kRequestId = 2; |
394 | 408 |
395 mojom::URLLoaderAssociatedPtr loader; | 409 mojom::URLLoaderAssociatedPtr loader; |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
440 | 454 |
441 url_request->Cancel(); | 455 url_request->Cancel(); |
442 client.RunUntilComplete(); | 456 client.RunUntilComplete(); |
443 | 457 |
444 ASSERT_TRUE(client.has_received_completion()); | 458 ASSERT_TRUE(client.has_received_completion()); |
445 | 459 |
446 EXPECT_EQ(200, client.response_head().headers->response_code()); | 460 EXPECT_EQ(200, client.response_head().headers->response_code()); |
447 EXPECT_EQ(net::ERR_ABORTED, client.completion_status().error_code); | 461 EXPECT_EQ(net::ERR_ABORTED, client.completion_status().error_code); |
448 } | 462 } |
449 | 463 |
464 TEST_P(URLLoaderFactoryImplTest, OnTransferSizeUpdated) { | |
465 constexpr int32_t kRoutingId = 81; | |
466 constexpr int32_t kRequestId = 28; | |
467 NavigationResourceThrottle::set_ui_checks_always_succeed_for_testing(true); | |
468 mojom::URLLoaderAssociatedPtr loader; | |
469 base::FilePath root; | |
470 PathService::Get(DIR_TEST_DATA, &root); | |
471 net::URLRequestMockHTTPJob::AddUrlHandlers(root, | |
472 BrowserThread::GetBlockingPool()); | |
473 ResourceRequest request; | |
474 TestURLLoaderClient client; | |
475 // Assume the file contents is small enough to be stored in the data pipe. | |
476 request.url = net::URLRequestMockHTTPJob::GetMockUrl("gzip-content.svgz"); | |
477 request.method = "GET"; | |
478 // |resource_type| can't be a frame type. It is because when PlzNavigate is | |
479 // enabled, the url scheme of frame type requests from the renderer process | |
480 // must be blob scheme. | |
481 request.resource_type = RESOURCE_TYPE_XHR; | |
482 // Need to set |request_initiator| for non main frame type request. | |
483 request.request_initiator = url::Origin(); | |
484 request.report_raw_headers = true; | |
485 factory_->CreateLoaderAndStart( | |
486 mojo::GetProxy(&loader, factory_.associated_group()), kRoutingId, | |
487 kRequestId, request, | |
488 client.CreateRemoteAssociatedPtrInfo(factory_.associated_group())); | |
489 | |
490 client.RunUntilComplete(); | |
491 | |
492 std::string contents; | |
493 while (true) { | |
494 char buffer[16]; | |
495 uint32_t read_size = sizeof(buffer); | |
496 MojoResult r = mojo::ReadDataRaw(client.response_body(), buffer, &read_size, | |
497 MOJO_READ_DATA_FLAG_NONE); | |
498 if (r == MOJO_RESULT_FAILED_PRECONDITION) | |
499 break; | |
500 if (r == MOJO_RESULT_SHOULD_WAIT) | |
501 continue; | |
502 ASSERT_EQ(MOJO_RESULT_OK, r); | |
503 contents.append(buffer, read_size); | |
504 } | |
505 | |
506 EXPECT_GT(client.response_head().encoded_data_length, 0); | |
507 EXPECT_GT(client.completion_status().encoded_data_length, 0); | |
mmenke
2016/12/14 18:18:55
Optional: Could use EXPECT_EQ and GetFileSize on
yhirano
2016/12/15 11:27:59
Replaced EXPECT_GT with EXPECT_EQ (for the encoded
| |
508 EXPECT_GT(client.body_transfer_size(), 0); | |
509 EXPECT_EQ(200, client.response_head().headers->response_code()); | |
510 EXPECT_EQ( | |
511 client.response_head().encoded_data_length + client.body_transfer_size(), | |
512 client.completion_status().encoded_data_length); | |
513 EXPECT_NE(client.body_transfer_size(), static_cast<int64_t>(contents.size())); | |
514 EXPECT_EQ(client.body_transfer_size(), | |
515 client.completion_status().encoded_body_length); | |
516 EXPECT_EQ(contents, "Hello World!\n"); | |
517 } | |
518 | |
450 // Removing the loader in the remote side will cancel the request. | 519 // Removing the loader in the remote side will cancel the request. |
451 TEST_P(URLLoaderFactoryImplTest, CancelFromRenderer) { | 520 TEST_P(URLLoaderFactoryImplTest, CancelFromRenderer) { |
452 constexpr int32_t kRoutingId = 81; | 521 constexpr int32_t kRoutingId = 81; |
453 constexpr int32_t kRequestId = 28; | 522 constexpr int32_t kRequestId = 28; |
454 NavigationResourceThrottle::set_ui_checks_always_succeed_for_testing(true); | 523 NavigationResourceThrottle::set_ui_checks_always_succeed_for_testing(true); |
455 mojom::URLLoaderAssociatedPtr loader; | 524 mojom::URLLoaderAssociatedPtr loader; |
456 base::FilePath root; | 525 base::FilePath root; |
457 PathService::Get(DIR_TEST_DATA, &root); | 526 PathService::Get(DIR_TEST_DATA, &root); |
458 net::URLRequestFailedJob::AddUrlHandler(); | 527 net::URLRequestFailedJob::AddUrlHandler(); |
459 ResourceRequest request; | 528 ResourceRequest request; |
(...skipping 24 matching lines...) Expand all Loading... | |
484 ASSERT_FALSE(rdh_.GetURLRequest(GlobalRequestID(kChildId, kRequestId))); | 553 ASSERT_FALSE(rdh_.GetURLRequest(GlobalRequestID(kChildId, kRequestId))); |
485 } | 554 } |
486 | 555 |
487 INSTANTIATE_TEST_CASE_P(URLLoaderFactoryImplTest, | 556 INSTANTIATE_TEST_CASE_P(URLLoaderFactoryImplTest, |
488 URLLoaderFactoryImplTest, | 557 URLLoaderFactoryImplTest, |
489 ::testing::Values(128, 32 * 1024)); | 558 ::testing::Values(128, 32 * 1024)); |
490 | 559 |
491 } // namespace | 560 } // namespace |
492 | 561 |
493 } // namespace content | 562 } // namespace content |
OLD | NEW |