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 <memory> |
8 #include <string> | 8 #include <string> |
9 #include <utility> | 9 #include <utility> |
10 #include <vector> | 10 #include <vector> |
(...skipping 21 matching lines...) Expand all Loading... |
32 #include "net/http/http_cache.h" | 32 #include "net/http/http_cache.h" |
33 #include "net/http/http_network_session.h" | 33 #include "net/http/http_network_session.h" |
34 #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" | 35 #include "net/http/http_transaction_factory.h" |
36 #include "net/http/transport_security_state.h" | 36 #include "net/http/transport_security_state.h" |
37 #include "net/net_features.h" | 37 #include "net/net_features.h" |
38 #include "net/socket/client_socket_pool_manager.h" | 38 #include "net/socket/client_socket_pool_manager.h" |
39 #include "net/socket/transport_client_socket_pool.h" | 39 #include "net/socket/transport_client_socket_pool.h" |
40 #include "net/ssl/ssl_config_service_defaults.h" | 40 #include "net/ssl/ssl_config_service_defaults.h" |
41 #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/simple_connection_listener.h" | 42 #include "net/test/embedded_test_server/embedded_test_server_connection_listener
.h" |
43 #include "net/test/gtest_util.h" | 43 #include "net/test/gtest_util.h" |
44 #include "net/url_request/url_request_context_storage.h" | 44 #include "net/url_request/url_request_context_storage.h" |
45 #include "net/url_request/url_request_file_job.h" | 45 #include "net/url_request/url_request_file_job.h" |
46 #include "net/url_request/url_request_job_factory_impl.h" | 46 #include "net/url_request/url_request_job_factory_impl.h" |
47 #include "net/url_request/url_request_test_util.h" | 47 #include "net/url_request/url_request_test_util.h" |
48 #include "testing/gmock/include/gmock/gmock.h" | 48 #include "testing/gmock/include/gmock/gmock.h" |
49 #include "testing/gtest/include/gtest/gtest.h" | 49 #include "testing/gtest/include/gtest/gtest.h" |
50 #include "testing/platform_test.h" | 50 #include "testing/platform_test.h" |
51 | 51 |
52 #if !BUILDFLAG(DISABLE_FILE_SUPPORT) | 52 #if !BUILDFLAG(DISABLE_FILE_SUPPORT) |
(...skipping 14 matching lines...) Expand all Loading... |
67 namespace { | 67 namespace { |
68 | 68 |
69 const base::FilePath::CharType kDocRoot[] = | 69 const base::FilePath::CharType kDocRoot[] = |
70 FILE_PATH_LITERAL("net/data/proxy_script_fetcher_unittest"); | 70 FILE_PATH_LITERAL("net/data/proxy_script_fetcher_unittest"); |
71 | 71 |
72 struct FetchResult { | 72 struct FetchResult { |
73 int code; | 73 int code; |
74 base::string16 text; | 74 base::string16 text; |
75 }; | 75 }; |
76 | 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 |
77 // 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 |
78 // the tests were built with file support. | 108 // the tests were built with file support. |
79 class RequestContext : public URLRequestContext { | 109 class RequestContext : public URLRequestContext { |
80 public: | 110 public: |
81 RequestContext() : storage_(this) { | 111 RequestContext() : storage_(this) { |
82 ProxyConfig no_proxy; | 112 ProxyConfig no_proxy; |
83 storage_.set_host_resolver( | 113 storage_.set_host_resolver( |
84 std::unique_ptr<HostResolver>(new MockHostResolver)); | 114 std::unique_ptr<HostResolver>(new MockHostResolver)); |
85 storage_.set_cert_verifier(base::WrapUnique(new MockCertVerifier)); | 115 storage_.set_cert_verifier(base::WrapUnique(new MockCertVerifier)); |
86 storage_.set_transport_security_state( | 116 storage_.set_transport_security_state( |
(...skipping 419 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
506 } | 536 } |
507 | 537 |
508 // Makes sure that a request gets through when the socket pool is full, so | 538 // Makes sure that a request gets through when the socket pool is full, so |
509 // ProxyScriptFetcherImpl can use the same URLRequestContext as everything else. | 539 // ProxyScriptFetcherImpl can use the same URLRequestContext as everything else. |
510 TEST_F(ProxyScriptFetcherImplTest, Priority) { | 540 TEST_F(ProxyScriptFetcherImplTest, Priority) { |
511 // Enough requests to exceed the per-pool limit, which is also enough to | 541 // Enough requests to exceed the per-pool limit, which is also enough to |
512 // exceed the per-group limit. | 542 // exceed the per-group limit. |
513 int num_requests = 10 + ClientSocketPoolManager::max_sockets_per_pool( | 543 int num_requests = 10 + ClientSocketPoolManager::max_sockets_per_pool( |
514 HttpNetworkSession::NORMAL_SOCKET_POOL); | 544 HttpNetworkSession::NORMAL_SOCKET_POOL); |
515 | 545 |
516 net::test_server::SimpleConnectionListener connection_listener( | 546 WaitForConnectionsListener connection_listener(num_requests); |
517 num_requests, net::test_server::SimpleConnectionListener:: | |
518 FAIL_ON_ADDITIONAL_CONNECTIONS); | |
519 test_server_.SetConnectionListener(&connection_listener); | 547 test_server_.SetConnectionListener(&connection_listener); |
520 ASSERT_TRUE(test_server_.Start()); | 548 ASSERT_TRUE(test_server_.Start()); |
521 | 549 |
522 std::vector<std::unique_ptr<ProxyScriptFetcherImpl>> pac_fetchers; | 550 std::vector<std::unique_ptr<ProxyScriptFetcherImpl>> pac_fetchers; |
523 | 551 |
524 TestCompletionCallback callback; | 552 TestCompletionCallback callback; |
525 base::string16 text; | 553 base::string16 text; |
526 for (int i = 0; i < num_requests; i++) { | 554 for (int i = 0; i < num_requests; i++) { |
527 std::unique_ptr<ProxyScriptFetcherImpl> pac_fetcher = | 555 std::unique_ptr<ProxyScriptFetcherImpl> pac_fetcher = |
528 base::MakeUnique<ProxyScriptFetcherImpl>(&context_); | 556 base::MakeUnique<ProxyScriptFetcherImpl>(&context_); |
529 GURL url(test_server_.GetURL("/hung")); | 557 GURL url(test_server_.GetURL("/hung")); |
530 // Fine to use the same string and callback for all of these, as they should | 558 // Fine to use the same string and callback for all of these, as they should |
531 // all hang. | 559 // all hang. |
532 int result = pac_fetcher->Fetch(url, &text, callback.callback()); | 560 int result = pac_fetcher->Fetch(url, &text, callback.callback()); |
533 EXPECT_THAT(result, IsError(ERR_IO_PENDING)); | 561 EXPECT_THAT(result, IsError(ERR_IO_PENDING)); |
534 pac_fetchers.push_back(std::move(pac_fetcher)); | 562 pac_fetchers.push_back(std::move(pac_fetcher)); |
535 } | 563 } |
536 | 564 |
537 connection_listener.WaitForConnections(); | 565 connection_listener.Wait(); |
538 // None of the callbacks should have been invoked - all jobs should still be | 566 // None of the callbacks should have been invoked - all jobs should still be |
539 // hung. | 567 // hung. |
540 EXPECT_FALSE(callback.have_result()); | 568 EXPECT_FALSE(callback.have_result()); |
541 | 569 |
542 // Need to shut down the server before |connection_listener| is destroyed. | 570 // Need to shut down the server before |connection_listener| is destroyed. |
543 EXPECT_TRUE(test_server_.ShutdownAndWaitUntilComplete()); | 571 EXPECT_TRUE(test_server_.ShutdownAndWaitUntilComplete()); |
544 } | 572 } |
545 | 573 |
546 TEST_F(ProxyScriptFetcherImplTest, OnShutdown) { | 574 TEST_F(ProxyScriptFetcherImplTest, OnShutdown) { |
547 ASSERT_TRUE(test_server_.Start()); | 575 ASSERT_TRUE(test_server_.Start()); |
(...skipping 30 matching lines...) Expand all Loading... |
578 TestCompletionCallback callback; | 606 TestCompletionCallback callback; |
579 int result = pac_fetcher.Fetch(test_server_.GetURL("/hung"), &text, | 607 int result = pac_fetcher.Fetch(test_server_.GetURL("/hung"), &text, |
580 callback.callback()); | 608 callback.callback()); |
581 EXPECT_THAT(result, IsError(ERR_CONTEXT_SHUT_DOWN)); | 609 EXPECT_THAT(result, IsError(ERR_CONTEXT_SHUT_DOWN)); |
582 EXPECT_EQ(0u, context_.url_requests().size()); | 610 EXPECT_EQ(0u, context_.url_requests().size()); |
583 } | 611 } |
584 | 612 |
585 } // namespace | 613 } // namespace |
586 | 614 |
587 } // namespace net | 615 } // namespace net |
OLD | NEW |