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/proxy/proxy_script_fetcher_impl.h" | 5 #include "net/proxy/proxy_script_fetcher_impl.h" |
6 | 6 |
| 7 #include <memory> |
7 #include <string> | 8 #include <string> |
8 #include <utility> | 9 #include <utility> |
| 10 #include <vector> |
9 | 11 |
10 #include "base/compiler_specific.h" | 12 #include "base/compiler_specific.h" |
11 #include "base/files/file_path.h" | 13 #include "base/files/file_path.h" |
12 #include "base/macros.h" | 14 #include "base/macros.h" |
13 #include "base/memory/ptr_util.h" | 15 #include "base/memory/ptr_util.h" |
| 16 #include "base/memory/ref_counted.h" |
14 #include "base/path_service.h" | 17 #include "base/path_service.h" |
| 18 #include "base/run_loop.h" |
| 19 #include "base/sequenced_task_runner.h" |
15 #include "base/single_thread_task_runner.h" | 20 #include "base/single_thread_task_runner.h" |
16 #include "base/strings/utf_string_conversions.h" | 21 #include "base/strings/utf_string_conversions.h" |
17 #include "base/threading/thread_task_runner_handle.h" | 22 #include "base/threading/thread_task_runner_handle.h" |
18 #include "net/base/filename_util.h" | 23 #include "net/base/filename_util.h" |
19 #include "net/base/load_flags.h" | 24 #include "net/base/load_flags.h" |
20 #include "net/base/network_delegate_impl.h" | 25 #include "net/base/network_delegate_impl.h" |
21 #include "net/base/test_completion_callback.h" | 26 #include "net/base/test_completion_callback.h" |
22 #include "net/cert/ct_policy_enforcer.h" | 27 #include "net/cert/ct_policy_enforcer.h" |
23 #include "net/cert/mock_cert_verifier.h" | 28 #include "net/cert/mock_cert_verifier.h" |
24 #include "net/cert/multi_log_ct_verifier.h" | 29 #include "net/cert/multi_log_ct_verifier.h" |
25 #include "net/disk_cache/disk_cache.h" | 30 #include "net/disk_cache/disk_cache.h" |
26 #include "net/dns/mock_host_resolver.h" | 31 #include "net/dns/mock_host_resolver.h" |
27 #include "net/http/http_cache.h" | 32 #include "net/http/http_cache.h" |
28 #include "net/http/http_network_session.h" | 33 #include "net/http/http_network_session.h" |
29 #include "net/http/http_server_properties_impl.h" | 34 #include "net/http/http_server_properties_impl.h" |
| 35 #include "net/http/http_transaction_factory.h" |
30 #include "net/http/transport_security_state.h" | 36 #include "net/http/transport_security_state.h" |
31 #include "net/net_features.h" | 37 #include "net/net_features.h" |
| 38 #include "net/socket/client_socket_pool_manager.h" |
| 39 #include "net/socket/transport_client_socket_pool.h" |
32 #include "net/ssl/ssl_config_service_defaults.h" | 40 #include "net/ssl/ssl_config_service_defaults.h" |
33 #include "net/test/embedded_test_server/embedded_test_server.h" | 41 #include "net/test/embedded_test_server/embedded_test_server.h" |
| 42 #include "net/test/embedded_test_server/embedded_test_server_connection_listener
.h" |
34 #include "net/test/gtest_util.h" | 43 #include "net/test/gtest_util.h" |
35 #include "net/url_request/url_request_context_storage.h" | 44 #include "net/url_request/url_request_context_storage.h" |
36 #include "net/url_request/url_request_file_job.h" | 45 #include "net/url_request/url_request_file_job.h" |
37 #include "net/url_request/url_request_job_factory_impl.h" | 46 #include "net/url_request/url_request_job_factory_impl.h" |
38 #include "net/url_request/url_request_test_util.h" | 47 #include "net/url_request/url_request_test_util.h" |
39 #include "testing/gmock/include/gmock/gmock.h" | 48 #include "testing/gmock/include/gmock/gmock.h" |
40 #include "testing/gtest/include/gtest/gtest.h" | 49 #include "testing/gtest/include/gtest/gtest.h" |
41 #include "testing/platform_test.h" | 50 #include "testing/platform_test.h" |
42 | 51 |
43 #if !BUILDFLAG(DISABLE_FILE_SUPPORT) | 52 #if !BUILDFLAG(DISABLE_FILE_SUPPORT) |
(...skipping 14 matching lines...) Expand all Loading... |
58 namespace { | 67 namespace { |
59 | 68 |
60 const base::FilePath::CharType kDocRoot[] = | 69 const base::FilePath::CharType kDocRoot[] = |
61 FILE_PATH_LITERAL("net/data/proxy_script_fetcher_unittest"); | 70 FILE_PATH_LITERAL("net/data/proxy_script_fetcher_unittest"); |
62 | 71 |
63 struct FetchResult { | 72 struct FetchResult { |
64 int code; | 73 int code; |
65 base::string16 text; | 74 base::string16 text; |
66 }; | 75 }; |
67 | 76 |
| 77 // Waits for the specified number of connection attempts to be seen. |
| 78 class WaitForConnectionsListener |
| 79 : public test_server::EmbeddedTestServerConnectionListener { |
| 80 public: |
| 81 explicit WaitForConnectionsListener(int expected_num_connections) |
| 82 : expected_num_connections_(expected_num_connections), |
| 83 task_runner_(base::SequencedTaskRunnerHandle::Get()) {} |
| 84 |
| 85 void AcceptedSocket(const StreamSocket& socket) override { |
| 86 ++seen_connections_; |
| 87 EXPECT_LE(seen_connections_, expected_num_connections_); |
| 88 if (expected_num_connections_ == seen_connections_) |
| 89 task_runner_->PostTask(FROM_HERE, run_loop_.QuitClosure()); |
| 90 } |
| 91 |
| 92 void ReadFromSocket(const StreamSocket& socket, int rv) override {} |
| 93 |
| 94 void Wait() { run_loop_.Run(); } |
| 95 |
| 96 private: |
| 97 int seen_connections_ = 0; |
| 98 int expected_num_connections_; |
| 99 |
| 100 scoped_refptr<base::SequencedTaskRunner> task_runner_; |
| 101 |
| 102 base::RunLoop run_loop_; |
| 103 |
| 104 DISALLOW_COPY_AND_ASSIGN(WaitForConnectionsListener); |
| 105 }; |
| 106 |
68 // A non-mock URL request which can access http:// and file:// urls, in the case | 107 // A non-mock URL request which can access http:// and file:// urls, in the case |
69 // the tests were built with file support. | 108 // the tests were built with file support. |
70 class RequestContext : public URLRequestContext { | 109 class RequestContext : public URLRequestContext { |
71 public: | 110 public: |
72 RequestContext() : storage_(this) { | 111 RequestContext() : storage_(this) { |
73 ProxyConfig no_proxy; | 112 ProxyConfig no_proxy; |
74 storage_.set_host_resolver( | 113 storage_.set_host_resolver( |
75 std::unique_ptr<HostResolver>(new MockHostResolver)); | 114 std::unique_ptr<HostResolver>(new MockHostResolver)); |
76 storage_.set_cert_verifier(base::WrapUnique(new MockCertVerifier)); | 115 storage_.set_cert_verifier(base::WrapUnique(new MockCertVerifier)); |
77 storage_.set_transport_security_state( | 116 storage_.set_transport_security_state( |
(...skipping 411 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
489 // Test a broken "data:"-url containing a base64 encoded PAC script. | 528 // Test a broken "data:"-url containing a base64 encoded PAC script. |
490 { | 529 { |
491 GURL url(kEncodedUrlBroken); | 530 GURL url(kEncodedUrlBroken); |
492 base::string16 text; | 531 base::string16 text; |
493 TestCompletionCallback callback; | 532 TestCompletionCallback callback; |
494 int result = pac_fetcher.Fetch(url, &text, callback.callback()); | 533 int result = pac_fetcher.Fetch(url, &text, callback.callback()); |
495 EXPECT_THAT(result, IsError(ERR_FAILED)); | 534 EXPECT_THAT(result, IsError(ERR_FAILED)); |
496 } | 535 } |
497 } | 536 } |
498 | 537 |
| 538 // Makes sure that a request gets through when the socket pool is full, so |
| 539 // ProxyScriptFetcherImpl can use the same URLRequestContext as everything else. |
| 540 TEST_F(ProxyScriptFetcherImplTest, Priority) { |
| 541 // Enough requests to exceed the per-pool limit, which is also enough to |
| 542 // exceed the per-group limit. |
| 543 int num_requests = 10 + ClientSocketPoolManager::max_sockets_per_pool( |
| 544 HttpNetworkSession::NORMAL_SOCKET_POOL); |
| 545 |
| 546 WaitForConnectionsListener connection_listener(num_requests); |
| 547 test_server_.SetConnectionListener(&connection_listener); |
| 548 ASSERT_TRUE(test_server_.Start()); |
| 549 |
| 550 std::vector<std::unique_ptr<ProxyScriptFetcherImpl>> pac_fetchers; |
| 551 |
| 552 TestCompletionCallback callback; |
| 553 base::string16 text; |
| 554 for (int i = 0; i < num_requests; i++) { |
| 555 std::unique_ptr<ProxyScriptFetcherImpl> pac_fetcher = |
| 556 base::MakeUnique<ProxyScriptFetcherImpl>(&context_); |
| 557 GURL url(test_server_.GetURL("/hung")); |
| 558 // Fine to use the same string and callback for all of these, as they should |
| 559 // all hang. |
| 560 int result = pac_fetcher->Fetch(url, &text, callback.callback()); |
| 561 EXPECT_THAT(result, IsError(ERR_IO_PENDING)); |
| 562 pac_fetchers.push_back(std::move(pac_fetcher)); |
| 563 } |
| 564 |
| 565 connection_listener.Wait(); |
| 566 // None of the callbacks should have been invoked - all jobs should still be |
| 567 // hung. |
| 568 EXPECT_FALSE(callback.have_result()); |
| 569 |
| 570 // Need to shut down the server before |connection_listener| is destroyed. |
| 571 EXPECT_TRUE(test_server_.ShutdownAndWaitUntilComplete()); |
| 572 } |
| 573 |
| 574 TEST_F(ProxyScriptFetcherImplTest, OnShutdown) { |
| 575 ASSERT_TRUE(test_server_.Start()); |
| 576 |
| 577 ProxyScriptFetcherImpl pac_fetcher(&context_); |
| 578 base::string16 text; |
| 579 TestCompletionCallback callback; |
| 580 int result = pac_fetcher.Fetch(test_server_.GetURL("/hung"), &text, |
| 581 callback.callback()); |
| 582 EXPECT_THAT(result, IsError(ERR_IO_PENDING)); |
| 583 EXPECT_EQ(1u, context_.url_requests().size()); |
| 584 |
| 585 pac_fetcher.OnShutdown(); |
| 586 EXPECT_EQ(0u, context_.url_requests().size()); |
| 587 EXPECT_THAT(callback.WaitForResult(), IsError(ERR_CONTEXT_SHUT_DOWN)); |
| 588 |
| 589 // Make sure there's no asynchronous completion notification. |
| 590 base::RunLoop().RunUntilIdle(); |
| 591 EXPECT_EQ(0u, context_.url_requests().size()); |
| 592 EXPECT_FALSE(callback.have_result()); |
| 593 |
| 594 result = pac_fetcher.Fetch(test_server_.GetURL("/hung"), &text, |
| 595 callback.callback()); |
| 596 EXPECT_THAT(result, IsError(ERR_CONTEXT_SHUT_DOWN)); |
| 597 } |
| 598 |
| 599 TEST_F(ProxyScriptFetcherImplTest, OnShutdownWithNoLiveRequest) { |
| 600 ASSERT_TRUE(test_server_.Start()); |
| 601 |
| 602 ProxyScriptFetcherImpl pac_fetcher(&context_); |
| 603 pac_fetcher.OnShutdown(); |
| 604 |
| 605 base::string16 text; |
| 606 TestCompletionCallback callback; |
| 607 int result = pac_fetcher.Fetch(test_server_.GetURL("/hung"), &text, |
| 608 callback.callback()); |
| 609 EXPECT_THAT(result, IsError(ERR_CONTEXT_SHUT_DOWN)); |
| 610 EXPECT_EQ(0u, context_.url_requests().size()); |
| 611 } |
| 612 |
499 } // namespace | 613 } // namespace |
500 | 614 |
501 } // namespace net | 615 } // namespace net |
OLD | NEW |