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_service.h" | 5 #include "net/proxy/proxy_service.h" |
6 | 6 |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/format_macros.h" | 9 #include "base/format_macros.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
11 #include "base/strings/string_util.h" | 11 #include "base/strings/string_util.h" |
12 #include "base/strings/utf_string_conversions.h" | 12 #include "base/strings/utf_string_conversions.h" |
13 #include "net/base/load_flags.h" | 13 #include "net/base/load_flags.h" |
14 #include "net/base/net_errors.h" | 14 #include "net/base/net_errors.h" |
15 #include "net/base/network_delegate_impl.h" | 15 #include "net/base/network_delegate_impl.h" |
16 #include "net/base/test_completion_callback.h" | 16 #include "net/base/test_completion_callback.h" |
| 17 #include "net/log/captured_net_log_entry.h" |
17 #include "net/log/net_log.h" | 18 #include "net/log/net_log.h" |
18 #include "net/log/net_log_unittest.h" | 19 #include "net/log/net_log_unittest.h" |
| 20 #include "net/log/test_net_log.h" |
19 #include "net/proxy/dhcp_proxy_script_fetcher.h" | 21 #include "net/proxy/dhcp_proxy_script_fetcher.h" |
20 #include "net/proxy/mock_proxy_resolver.h" | 22 #include "net/proxy/mock_proxy_resolver.h" |
21 #include "net/proxy/mock_proxy_script_fetcher.h" | 23 #include "net/proxy/mock_proxy_script_fetcher.h" |
22 #include "net/proxy/proxy_config_service.h" | 24 #include "net/proxy/proxy_config_service.h" |
23 #include "net/proxy/proxy_resolver.h" | 25 #include "net/proxy/proxy_resolver.h" |
24 #include "net/proxy/proxy_script_fetcher.h" | 26 #include "net/proxy/proxy_script_fetcher.h" |
25 #include "testing/gtest/include/gtest/gtest.h" | 27 #include "testing/gtest/include/gtest/gtest.h" |
26 #include "url/gurl.h" | 28 #include "url/gurl.h" |
27 | 29 |
28 using base::ASCIIToUTF16; | 30 using base::ASCIIToUTF16; |
(...skipping 224 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
253 int rv = service.ResolveProxy(url, LOAD_NORMAL, &info, callback.callback(), | 255 int rv = service.ResolveProxy(url, LOAD_NORMAL, &info, callback.callback(), |
254 NULL, NULL, log.bound()); | 256 NULL, NULL, log.bound()); |
255 EXPECT_EQ(OK, rv); | 257 EXPECT_EQ(OK, rv); |
256 EXPECT_TRUE(resolver.pending_requests().empty()); | 258 EXPECT_TRUE(resolver.pending_requests().empty()); |
257 | 259 |
258 EXPECT_TRUE(info.is_direct()); | 260 EXPECT_TRUE(info.is_direct()); |
259 EXPECT_TRUE(info.proxy_resolve_start_time().is_null()); | 261 EXPECT_TRUE(info.proxy_resolve_start_time().is_null()); |
260 EXPECT_TRUE(info.proxy_resolve_end_time().is_null()); | 262 EXPECT_TRUE(info.proxy_resolve_end_time().is_null()); |
261 | 263 |
262 // Check the NetLog was filled correctly. | 264 // Check the NetLog was filled correctly. |
263 TestNetLog::CapturedEntryList entries; | 265 CapturedNetLogEntry::List entries; |
264 log.GetEntries(&entries); | 266 log.GetEntries(&entries); |
265 | 267 |
266 EXPECT_EQ(3u, entries.size()); | 268 EXPECT_EQ(3u, entries.size()); |
267 EXPECT_TRUE(LogContainsBeginEvent( | 269 EXPECT_TRUE(LogContainsBeginEvent( |
268 entries, 0, NetLog::TYPE_PROXY_SERVICE)); | 270 entries, 0, NetLog::TYPE_PROXY_SERVICE)); |
269 EXPECT_TRUE(LogContainsEvent( | 271 EXPECT_TRUE(LogContainsEvent( |
270 entries, 1, NetLog::TYPE_PROXY_SERVICE_RESOLVED_PROXY_LIST, | 272 entries, 1, NetLog::TYPE_PROXY_SERVICE_RESOLVED_PROXY_LIST, |
271 NetLog::PHASE_NONE)); | 273 NetLog::PHASE_NONE)); |
272 EXPECT_TRUE(LogContainsEndEvent( | 274 EXPECT_TRUE(LogContainsEndEvent( |
273 entries, 2, NetLog::TYPE_PROXY_SERVICE)); | 275 entries, 2, NetLog::TYPE_PROXY_SERVICE)); |
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
405 EXPECT_EQ(OK, callback.WaitForResult()); | 407 EXPECT_EQ(OK, callback.WaitForResult()); |
406 EXPECT_FALSE(info.is_direct()); | 408 EXPECT_FALSE(info.is_direct()); |
407 EXPECT_EQ("foopy:80", info.proxy_server().ToURI()); | 409 EXPECT_EQ("foopy:80", info.proxy_server().ToURI()); |
408 EXPECT_TRUE(info.did_use_pac_script()); | 410 EXPECT_TRUE(info.did_use_pac_script()); |
409 | 411 |
410 EXPECT_FALSE(info.proxy_resolve_start_time().is_null()); | 412 EXPECT_FALSE(info.proxy_resolve_start_time().is_null()); |
411 EXPECT_FALSE(info.proxy_resolve_end_time().is_null()); | 413 EXPECT_FALSE(info.proxy_resolve_end_time().is_null()); |
412 EXPECT_LE(info.proxy_resolve_start_time(), info.proxy_resolve_end_time()); | 414 EXPECT_LE(info.proxy_resolve_start_time(), info.proxy_resolve_end_time()); |
413 | 415 |
414 // Check the NetLog was filled correctly. | 416 // Check the NetLog was filled correctly. |
415 TestNetLog::CapturedEntryList entries; | 417 CapturedNetLogEntry::List entries; |
416 log.GetEntries(&entries); | 418 log.GetEntries(&entries); |
417 | 419 |
418 EXPECT_EQ(5u, entries.size()); | 420 EXPECT_EQ(5u, entries.size()); |
419 EXPECT_TRUE(LogContainsBeginEvent( | 421 EXPECT_TRUE(LogContainsBeginEvent( |
420 entries, 0, NetLog::TYPE_PROXY_SERVICE)); | 422 entries, 0, NetLog::TYPE_PROXY_SERVICE)); |
421 EXPECT_TRUE(LogContainsBeginEvent( | 423 EXPECT_TRUE(LogContainsBeginEvent( |
422 entries, 1, NetLog::TYPE_PROXY_SERVICE_WAITING_FOR_INIT_PAC)); | 424 entries, 1, NetLog::TYPE_PROXY_SERVICE_WAITING_FOR_INIT_PAC)); |
423 EXPECT_TRUE(LogContainsEndEvent( | 425 EXPECT_TRUE(LogContainsEndEvent( |
424 entries, 2, NetLog::TYPE_PROXY_SERVICE_WAITING_FOR_INIT_PAC)); | 426 entries, 2, NetLog::TYPE_PROXY_SERVICE_WAITING_FOR_INIT_PAC)); |
425 EXPECT_TRUE(LogContainsEndEvent( | 427 EXPECT_TRUE(LogContainsEndEvent( |
(...skipping 305 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
731 // This time we will have the resolver succeed (perhaps the PAC script has | 733 // This time we will have the resolver succeed (perhaps the PAC script has |
732 // a dependency on the current time). | 734 // a dependency on the current time). |
733 resolver.pending_requests()[0]->results()->UseNamedProxy("foopy_valid:8080"); | 735 resolver.pending_requests()[0]->results()->UseNamedProxy("foopy_valid:8080"); |
734 resolver.pending_requests()[0]->CompleteNow(OK); | 736 resolver.pending_requests()[0]->CompleteNow(OK); |
735 | 737 |
736 EXPECT_EQ(OK, callback2.WaitForResult()); | 738 EXPECT_EQ(OK, callback2.WaitForResult()); |
737 EXPECT_FALSE(info.is_direct()); | 739 EXPECT_FALSE(info.is_direct()); |
738 EXPECT_EQ("foopy_valid:8080", info.proxy_server().ToURI()); | 740 EXPECT_EQ("foopy_valid:8080", info.proxy_server().ToURI()); |
739 } | 741 } |
740 | 742 |
| 743 TEST_F(ProxyServiceTest, ProxyResolverTerminatedDuringRequest) { |
| 744 // Test what happens when the ProxyResolver fails with a fatal error while |
| 745 // a GetProxyForURL() call is in progress. |
| 746 |
| 747 MockProxyConfigService* config_service = |
| 748 new MockProxyConfigService("http://foopy/proxy.pac"); |
| 749 |
| 750 MockAsyncProxyResolver resolver; |
| 751 |
| 752 ProxyService service( |
| 753 config_service, |
| 754 make_scoped_ptr(new ForwardingProxyResolverFactory(&resolver)), nullptr); |
| 755 |
| 756 // Start first resolve request. |
| 757 GURL url("http://www.google.com/"); |
| 758 ProxyInfo info; |
| 759 TestCompletionCallback callback1; |
| 760 int rv = |
| 761 service.ResolveProxy(url, net::LOAD_NORMAL, &info, callback1.callback(), |
| 762 nullptr, nullptr, BoundNetLog()); |
| 763 EXPECT_EQ(ERR_IO_PENDING, rv); |
| 764 |
| 765 ASSERT_TRUE(resolver.pending_set_pac_script_request()); |
| 766 EXPECT_EQ(GURL("http://foopy/proxy.pac"), |
| 767 resolver.pending_set_pac_script_request()->script_data()->url()); |
| 768 resolver.pending_set_pac_script_request()->CompleteNow(OK); |
| 769 |
| 770 ASSERT_EQ(1u, resolver.pending_requests().size()); |
| 771 EXPECT_EQ(url, resolver.pending_requests()[0]->url()); |
| 772 |
| 773 // Fail the first resolve request in MockAsyncProxyResolver. |
| 774 resolver.pending_requests()[0]->CompleteNow(ERR_PAC_SCRIPT_TERMINATED); |
| 775 |
| 776 // Although the proxy resolver failed the request, ProxyService implicitly |
| 777 // falls-back to DIRECT. |
| 778 EXPECT_EQ(OK, callback1.WaitForResult()); |
| 779 EXPECT_TRUE(info.is_direct()); |
| 780 |
| 781 // Failed PAC executions still have proxy resolution times. |
| 782 EXPECT_FALSE(info.proxy_resolve_start_time().is_null()); |
| 783 EXPECT_FALSE(info.proxy_resolve_end_time().is_null()); |
| 784 EXPECT_LE(info.proxy_resolve_start_time(), info.proxy_resolve_end_time()); |
| 785 |
| 786 // With no other requests, the ProxyService waits for a new request before |
| 787 // initializing a new ProxyResolver. |
| 788 EXPECT_FALSE(resolver.pending_set_pac_script_request()); |
| 789 |
| 790 TestCompletionCallback callback2; |
| 791 rv = service.ResolveProxy(url, net::LOAD_NORMAL, &info, callback2.callback(), |
| 792 nullptr, nullptr, BoundNetLog()); |
| 793 EXPECT_EQ(ERR_IO_PENDING, rv); |
| 794 |
| 795 ASSERT_TRUE(resolver.pending_set_pac_script_request()); |
| 796 EXPECT_EQ(GURL("http://foopy/proxy.pac"), |
| 797 resolver.pending_set_pac_script_request()->script_data()->url()); |
| 798 resolver.pending_set_pac_script_request()->CompleteNow(OK); |
| 799 |
| 800 ASSERT_EQ(1u, resolver.pending_requests().size()); |
| 801 EXPECT_EQ(url, resolver.pending_requests()[0]->url()); |
| 802 |
| 803 // This time we will have the resolver succeed. |
| 804 resolver.pending_requests()[0]->results()->UseNamedProxy("foopy_valid:8080"); |
| 805 resolver.pending_requests()[0]->CompleteNow(OK); |
| 806 |
| 807 EXPECT_EQ(OK, callback2.WaitForResult()); |
| 808 EXPECT_FALSE(info.is_direct()); |
| 809 EXPECT_EQ("foopy_valid:8080", info.proxy_server().ToURI()); |
| 810 } |
| 811 |
| 812 TEST_F(ProxyServiceTest, |
| 813 ProxyResolverTerminatedDuringRequestWithConcurrentRequest) { |
| 814 // Test what happens when the ProxyResolver fails with a fatal error while |
| 815 // a GetProxyForURL() call is in progress. |
| 816 |
| 817 MockProxyConfigService* config_service = |
| 818 new MockProxyConfigService("http://foopy/proxy.pac"); |
| 819 |
| 820 MockAsyncProxyResolver resolver; |
| 821 |
| 822 ProxyService service( |
| 823 config_service, |
| 824 make_scoped_ptr(new ForwardingProxyResolverFactory(&resolver)), nullptr); |
| 825 |
| 826 // Start two resolve requests. |
| 827 GURL url1("http://www.google.com/"); |
| 828 GURL url2("https://www.google.com/"); |
| 829 ProxyInfo info; |
| 830 TestCompletionCallback callback1; |
| 831 int rv = |
| 832 service.ResolveProxy(url1, net::LOAD_NORMAL, &info, callback1.callback(), |
| 833 nullptr, nullptr, BoundNetLog()); |
| 834 EXPECT_EQ(ERR_IO_PENDING, rv); |
| 835 TestCompletionCallback callback2; |
| 836 rv = service.ResolveProxy(url2, net::LOAD_NORMAL, &info, callback2.callback(), |
| 837 nullptr, nullptr, BoundNetLog()); |
| 838 EXPECT_EQ(ERR_IO_PENDING, rv); |
| 839 |
| 840 ASSERT_TRUE(resolver.pending_set_pac_script_request()); |
| 841 EXPECT_EQ(GURL("http://foopy/proxy.pac"), |
| 842 resolver.pending_set_pac_script_request()->script_data()->url()); |
| 843 resolver.pending_set_pac_script_request()->CompleteNow(OK); |
| 844 |
| 845 ASSERT_EQ(2u, resolver.pending_requests().size()); |
| 846 EXPECT_EQ(url1, resolver.pending_requests()[0]->url()); |
| 847 EXPECT_EQ(url2, resolver.pending_requests()[1]->url()); |
| 848 |
| 849 // Fail the first resolve request in MockAsyncProxyResolver. |
| 850 resolver.pending_requests()[0]->CompleteNow(ERR_PAC_SCRIPT_TERMINATED); |
| 851 |
| 852 // Although the proxy resolver failed the request, ProxyService implicitly |
| 853 // falls-back to DIRECT. |
| 854 EXPECT_EQ(OK, callback1.WaitForResult()); |
| 855 EXPECT_TRUE(info.is_direct()); |
| 856 |
| 857 // Failed PAC executions still have proxy resolution times. |
| 858 EXPECT_FALSE(info.proxy_resolve_start_time().is_null()); |
| 859 EXPECT_FALSE(info.proxy_resolve_end_time().is_null()); |
| 860 EXPECT_LE(info.proxy_resolve_start_time(), info.proxy_resolve_end_time()); |
| 861 |
| 862 // The second request is cancelled when the proxy resolver terminates. |
| 863 ASSERT_EQ(1u, resolver.cancelled_requests().size()); |
| 864 EXPECT_EQ(url2, resolver.cancelled_requests()[0]->url()); |
| 865 |
| 866 // Since a second request was in progress, the ProxyService starts |
| 867 // initializating a new ProxyResolver. |
| 868 ASSERT_TRUE(resolver.pending_set_pac_script_request()); |
| 869 EXPECT_EQ(GURL("http://foopy/proxy.pac"), |
| 870 resolver.pending_set_pac_script_request()->script_data()->url()); |
| 871 resolver.pending_set_pac_script_request()->CompleteNow(OK); |
| 872 |
| 873 ASSERT_EQ(1u, resolver.pending_requests().size()); |
| 874 EXPECT_EQ(url2, resolver.pending_requests()[0]->url()); |
| 875 |
| 876 // This request succeeds. |
| 877 resolver.pending_requests()[0]->results()->UseNamedProxy("foopy_valid:8080"); |
| 878 resolver.pending_requests()[0]->CompleteNow(OK); |
| 879 |
| 880 EXPECT_EQ(OK, callback2.WaitForResult()); |
| 881 EXPECT_FALSE(info.is_direct()); |
| 882 EXPECT_EQ("foopy_valid:8080", info.proxy_server().ToURI()); |
| 883 } |
| 884 |
741 TEST_F(ProxyServiceTest, ProxyScriptFetcherFailsDownloadingMandatoryPac) { | 885 TEST_F(ProxyServiceTest, ProxyScriptFetcherFailsDownloadingMandatoryPac) { |
742 // Test what happens when the ProxyScriptResolver fails to download a | 886 // Test what happens when the ProxyScriptResolver fails to download a |
743 // mandatory PAC script. | 887 // mandatory PAC script. |
744 | 888 |
745 ProxyConfig config( | 889 ProxyConfig config( |
746 ProxyConfig::CreateFromCustomPacURL(GURL("http://foopy/proxy.pac"))); | 890 ProxyConfig::CreateFromCustomPacURL(GURL("http://foopy/proxy.pac"))); |
747 config.set_pac_mandatory(true); | 891 config.set_pac_mandatory(true); |
748 | 892 |
749 MockProxyConfigService* config_service = new MockProxyConfigService(config); | 893 MockProxyConfigService* config_service = new MockProxyConfigService(config); |
750 | 894 |
(...skipping 1161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1912 resolver.pending_requests()[0]->CompleteNow(OK); | 2056 resolver.pending_requests()[0]->CompleteNow(OK); |
1913 | 2057 |
1914 EXPECT_EQ(OK, callback3.WaitForResult()); | 2058 EXPECT_EQ(OK, callback3.WaitForResult()); |
1915 EXPECT_EQ("request3:80", info3.proxy_server().ToURI()); | 2059 EXPECT_EQ("request3:80", info3.proxy_server().ToURI()); |
1916 | 2060 |
1917 EXPECT_TRUE(resolver.cancelled_requests().empty()); | 2061 EXPECT_TRUE(resolver.cancelled_requests().empty()); |
1918 | 2062 |
1919 EXPECT_FALSE(callback1.have_result()); // Cancelled. | 2063 EXPECT_FALSE(callback1.have_result()); // Cancelled. |
1920 EXPECT_FALSE(callback2.have_result()); // Cancelled. | 2064 EXPECT_FALSE(callback2.have_result()); // Cancelled. |
1921 | 2065 |
1922 TestNetLog::CapturedEntryList entries1; | 2066 CapturedNetLogEntry::List entries1; |
1923 log1.GetEntries(&entries1); | 2067 log1.GetEntries(&entries1); |
1924 | 2068 |
1925 // Check the NetLog for request 1 (which was cancelled) got filled properly. | 2069 // Check the NetLog for request 1 (which was cancelled) got filled properly. |
1926 EXPECT_EQ(4u, entries1.size()); | 2070 EXPECT_EQ(4u, entries1.size()); |
1927 EXPECT_TRUE(LogContainsBeginEvent( | 2071 EXPECT_TRUE(LogContainsBeginEvent( |
1928 entries1, 0, NetLog::TYPE_PROXY_SERVICE)); | 2072 entries1, 0, NetLog::TYPE_PROXY_SERVICE)); |
1929 EXPECT_TRUE(LogContainsBeginEvent( | 2073 EXPECT_TRUE(LogContainsBeginEvent( |
1930 entries1, 1, NetLog::TYPE_PROXY_SERVICE_WAITING_FOR_INIT_PAC)); | 2074 entries1, 1, NetLog::TYPE_PROXY_SERVICE_WAITING_FOR_INIT_PAC)); |
1931 // Note that TYPE_PROXY_SERVICE_WAITING_FOR_INIT_PAC is never completed before | 2075 // Note that TYPE_PROXY_SERVICE_WAITING_FOR_INIT_PAC is never completed before |
1932 // the cancellation occured. | 2076 // the cancellation occured. |
(...skipping 526 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2459 resolver.pending_requests()[0]->results()->UseNamedProxy("request2:80"); | 2603 resolver.pending_requests()[0]->results()->UseNamedProxy("request2:80"); |
2460 resolver.pending_requests()[0]->CompleteNow(OK); | 2604 resolver.pending_requests()[0]->CompleteNow(OK); |
2461 | 2605 |
2462 // Wait for completion callback, and verify that the request ran as expected. | 2606 // Wait for completion callback, and verify that the request ran as expected. |
2463 EXPECT_EQ(OK, callback2.WaitForResult()); | 2607 EXPECT_EQ(OK, callback2.WaitForResult()); |
2464 EXPECT_EQ("request2:80", info2.proxy_server().ToURI()); | 2608 EXPECT_EQ("request2:80", info2.proxy_server().ToURI()); |
2465 | 2609 |
2466 // Check that the expected events were output to the log stream. In particular | 2610 // Check that the expected events were output to the log stream. In particular |
2467 // PROXY_CONFIG_CHANGED should have only been emitted once (for the initial | 2611 // PROXY_CONFIG_CHANGED should have only been emitted once (for the initial |
2468 // setup), and NOT a second time when the IP address changed. | 2612 // setup), and NOT a second time when the IP address changed. |
2469 TestNetLog::CapturedEntryList entries; | 2613 CapturedNetLogEntry::List entries; |
2470 log.GetEntries(&entries); | 2614 log.GetEntries(&entries); |
2471 | 2615 |
2472 EXPECT_TRUE(LogContainsEntryWithType(entries, 0, | 2616 EXPECT_TRUE(LogContainsEntryWithType(entries, 0, |
2473 NetLog::TYPE_PROXY_CONFIG_CHANGED)); | 2617 NetLog::TYPE_PROXY_CONFIG_CHANGED)); |
2474 ASSERT_EQ(9u, entries.size()); | 2618 ASSERT_EQ(9u, entries.size()); |
2475 for (size_t i = 1; i < entries.size(); ++i) | 2619 for (size_t i = 1; i < entries.size(); ++i) |
2476 EXPECT_NE(NetLog::TYPE_PROXY_CONFIG_CHANGED, entries[i].type); | 2620 EXPECT_NE(NetLog::TYPE_PROXY_CONFIG_CHANGED, entries[i].type); |
2477 } | 2621 } |
2478 | 2622 |
2479 // This test verifies that the PAC script specified by the settings is | 2623 // This test verifies that the PAC script specified by the settings is |
(...skipping 626 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3106 url, LOAD_NORMAL, &info, NULL, log.bound()); | 3250 url, LOAD_NORMAL, &info, NULL, log.bound()); |
3107 EXPECT_TRUE(synchronous_success); | 3251 EXPECT_TRUE(synchronous_success); |
3108 EXPECT_FALSE(info.is_direct()); | 3252 EXPECT_FALSE(info.is_direct()); |
3109 EXPECT_EQ("foopy1", info.proxy_server().host_port_pair().host()); | 3253 EXPECT_EQ("foopy1", info.proxy_server().host_port_pair().host()); |
3110 | 3254 |
3111 // No request should have been queued. | 3255 // No request should have been queued. |
3112 EXPECT_EQ(0u, resolver.pending_requests().size()); | 3256 EXPECT_EQ(0u, resolver.pending_requests().size()); |
3113 } | 3257 } |
3114 | 3258 |
3115 } // namespace net | 3259 } // namespace net |
OLD | NEW |