| 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" |
| (...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 148 FOR_EACH_OBSERVER(Observer, observers_, | 148 FOR_EACH_OBSERVER(Observer, observers_, |
| 149 OnProxyConfigChanged(config_, availability_)); | 149 OnProxyConfigChanged(config_, availability_)); |
| 150 } | 150 } |
| 151 | 151 |
| 152 private: | 152 private: |
| 153 ConfigAvailability availability_; | 153 ConfigAvailability availability_; |
| 154 ProxyConfig config_; | 154 ProxyConfig config_; |
| 155 ObserverList<Observer, true> observers_; | 155 ObserverList<Observer, true> observers_; |
| 156 }; | 156 }; |
| 157 | 157 |
| 158 // A test network delegate that exercises NotifyProxyFallback. |
| 159 class TestNotifyProxyFallbackNetworkDelegate : public NetworkDelegate { |
| 160 public: |
| 161 TestNotifyProxyFallbackNetworkDelegate() |
| 162 : net_error_(0) { |
| 163 } |
| 164 |
| 165 virtual void NotifyProxyFallback(const ProxyServer& bad_proxy, |
| 166 int net_error) OVERRIDE { |
| 167 bad_proxy_ = bad_proxy; |
| 168 net_error_ = net_error; |
| 169 } |
| 170 |
| 171 ProxyServer& bad_proxy() { |
| 172 return bad_proxy_; |
| 173 } |
| 174 |
| 175 int net_error() { |
| 176 return net_error_; |
| 177 } |
| 178 |
| 179 private: |
| 180 ProxyServer bad_proxy_; |
| 181 int net_error_; |
| 182 }; |
| 183 |
| 158 // A test network delegate that exercises the OnResolveProxy callback. | 184 // A test network delegate that exercises the OnResolveProxy callback. |
| 159 class TestResolveProxyNetworkDelegate : public NetworkDelegate { | 185 class TestResolveProxyNetworkDelegate : public NetworkDelegate { |
| 160 public: | 186 public: |
| 161 TestResolveProxyNetworkDelegate() | 187 TestResolveProxyNetworkDelegate() |
| 162 : on_resolve_proxy_called_(false), | 188 : on_resolve_proxy_called_(false), |
| 163 add_proxy_(false), | 189 add_proxy_(false), |
| 164 remove_proxy_(false) { | 190 remove_proxy_(false) { |
| 165 } | 191 } |
| 166 | 192 |
| 167 virtual void OnResolveProxy( | 193 virtual void OnResolveProxy( |
| (...skipping 18 matching lines...) Expand all Loading... |
| 186 void set_remove_proxy(bool remove_proxy) { | 212 void set_remove_proxy(bool remove_proxy) { |
| 187 remove_proxy_ = remove_proxy; | 213 remove_proxy_ = remove_proxy; |
| 188 } | 214 } |
| 189 | 215 |
| 190 private: | 216 private: |
| 191 bool on_resolve_proxy_called_; | 217 bool on_resolve_proxy_called_; |
| 192 bool add_proxy_; | 218 bool add_proxy_; |
| 193 bool remove_proxy_; | 219 bool remove_proxy_; |
| 194 }; | 220 }; |
| 195 | 221 |
| 196 // A test network delegate that exercises the OnProxyFallback callback. | |
| 197 class TestProxyFallbackNetworkDelegate : public NetworkDelegate { | |
| 198 public: | |
| 199 TestProxyFallbackNetworkDelegate() | |
| 200 : on_proxy_fallback_called_(false), | |
| 201 proxy_fallback_net_error_(0), | |
| 202 proxy_did_fallback_(false) { | |
| 203 } | |
| 204 | |
| 205 virtual void OnProxyFallback( | |
| 206 const ProxyServer& proxy_server, | |
| 207 int net_error, | |
| 208 bool did_fallback) OVERRIDE { | |
| 209 proxy_server_ = proxy_server; | |
| 210 proxy_fallback_net_error_ = net_error; | |
| 211 proxy_did_fallback_ = did_fallback; | |
| 212 on_proxy_fallback_called_ = true; | |
| 213 } | |
| 214 | |
| 215 bool on_proxy_fallback_called() const { | |
| 216 return on_proxy_fallback_called_; | |
| 217 } | |
| 218 | |
| 219 const ProxyServer& proxy_server() const { | |
| 220 return proxy_server_; | |
| 221 } | |
| 222 | |
| 223 int proxy_fallback_net_error() const { | |
| 224 return proxy_fallback_net_error_; | |
| 225 } | |
| 226 | |
| 227 bool proxy_did_fallback() const { | |
| 228 return proxy_did_fallback_; | |
| 229 } | |
| 230 | |
| 231 private: | |
| 232 bool on_proxy_fallback_called_; | |
| 233 ProxyServer proxy_server_; | |
| 234 int proxy_fallback_net_error_; | |
| 235 bool proxy_did_fallback_; | |
| 236 }; | |
| 237 | |
| 238 } // namespace | 222 } // namespace |
| 239 | 223 |
| 240 TEST_F(ProxyServiceTest, Direct) { | 224 TEST_F(ProxyServiceTest, Direct) { |
| 241 MockAsyncProxyResolver* resolver = new MockAsyncProxyResolver; | 225 MockAsyncProxyResolver* resolver = new MockAsyncProxyResolver; |
| 242 ProxyService service(new MockProxyConfigService( | 226 ProxyService service(new MockProxyConfigService( |
| 243 ProxyConfig::CreateDirect()), resolver, NULL); | 227 ProxyConfig::CreateDirect()), resolver, NULL); |
| 244 | 228 |
| 245 GURL url("http://www.google.com/"); | 229 GURL url("http://www.google.com/"); |
| 246 | 230 |
| 247 ProxyInfo info; | 231 ProxyInfo info; |
| (...skipping 249 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 497 EXPECT_EQ("foopy:8080", info.proxy_server().ToURI()); | 481 EXPECT_EQ("foopy:8080", info.proxy_server().ToURI()); |
| 498 EXPECT_TRUE(info.did_use_pac_script()); | 482 EXPECT_TRUE(info.did_use_pac_script()); |
| 499 | 483 |
| 500 EXPECT_FALSE(info.proxy_resolve_start_time().is_null()); | 484 EXPECT_FALSE(info.proxy_resolve_start_time().is_null()); |
| 501 EXPECT_FALSE(info.proxy_resolve_end_time().is_null()); | 485 EXPECT_FALSE(info.proxy_resolve_end_time().is_null()); |
| 502 EXPECT_LE(info.proxy_resolve_start_time(), info.proxy_resolve_end_time()); | 486 EXPECT_LE(info.proxy_resolve_start_time(), info.proxy_resolve_end_time()); |
| 503 | 487 |
| 504 // Now, imagine that connecting to foopy:8080 fails: there is nothing | 488 // Now, imagine that connecting to foopy:8080 fails: there is nothing |
| 505 // left to fallback to, since our proxy list was NOT terminated by | 489 // left to fallback to, since our proxy list was NOT terminated by |
| 506 // DIRECT. | 490 // DIRECT. |
| 507 TestProxyFallbackNetworkDelegate network_delegate; | 491 NetworkDelegate network_delegate; |
| 508 TestCompletionCallback callback2; | 492 TestCompletionCallback callback2; |
| 509 ProxyServer expected_proxy_server = info.proxy_server(); | 493 ProxyServer expected_proxy_server = info.proxy_server(); |
| 510 rv = service.ReconsiderProxyAfterError( | 494 rv = service.ReconsiderProxyAfterError( |
| 511 url, net::LOAD_NORMAL, net::ERR_PROXY_CONNECTION_FAILED, | 495 url, net::LOAD_NORMAL, net::ERR_PROXY_CONNECTION_FAILED, |
| 512 &info, callback2.callback(), NULL, &network_delegate, BoundNetLog()); | 496 &info, callback2.callback(), NULL, &network_delegate, BoundNetLog()); |
| 513 // ReconsiderProxyAfterError returns error indicating nothing left. | 497 // ReconsiderProxyAfterError returns error indicating nothing left. |
| 514 EXPECT_EQ(ERR_FAILED, rv); | 498 EXPECT_EQ(ERR_FAILED, rv); |
| 515 EXPECT_TRUE(info.is_empty()); | 499 EXPECT_TRUE(info.is_empty()); |
| 516 EXPECT_TRUE(network_delegate.on_proxy_fallback_called()); | |
| 517 EXPECT_EQ(expected_proxy_server, network_delegate.proxy_server()); | |
| 518 EXPECT_EQ(net::ERR_PROXY_CONNECTION_FAILED, | |
| 519 network_delegate.proxy_fallback_net_error()); | |
| 520 EXPECT_FALSE(network_delegate.proxy_did_fallback()); | |
| 521 } | 500 } |
| 522 | 501 |
| 523 // Test that if the execution of the PAC script fails (i.e. javascript runtime | 502 // Test that if the execution of the PAC script fails (i.e. javascript runtime |
| 524 // error), and the PAC settings are non-mandatory, that we fall-back to direct. | 503 // error), and the PAC settings are non-mandatory, that we fall-back to direct. |
| 525 TEST_F(ProxyServiceTest, PAC_RuntimeError) { | 504 TEST_F(ProxyServiceTest, PAC_RuntimeError) { |
| 526 MockProxyConfigService* config_service = | 505 MockProxyConfigService* config_service = |
| 527 new MockProxyConfigService("http://foopy/proxy.pac"); | 506 new MockProxyConfigService("http://foopy/proxy.pac"); |
| 528 MockAsyncProxyResolver* resolver = new MockAsyncProxyResolver; | 507 MockAsyncProxyResolver* resolver = new MockAsyncProxyResolver; |
| 529 | 508 |
| 530 ProxyService service(config_service, resolver, NULL); | 509 ProxyService service(config_service, resolver, NULL); |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 603 | 582 |
| 604 // Set the result in proxy resolver. | 583 // Set the result in proxy resolver. |
| 605 resolver->pending_requests()[0]->results()->UsePacString( | 584 resolver->pending_requests()[0]->results()->UsePacString( |
| 606 "DIRECT ; PROXY foobar:10 ; DIRECT ; PROXY foobar:20"); | 585 "DIRECT ; PROXY foobar:10 ; DIRECT ; PROXY foobar:20"); |
| 607 resolver->pending_requests()[0]->CompleteNow(OK); | 586 resolver->pending_requests()[0]->CompleteNow(OK); |
| 608 | 587 |
| 609 EXPECT_EQ(OK, callback1.WaitForResult()); | 588 EXPECT_EQ(OK, callback1.WaitForResult()); |
| 610 EXPECT_TRUE(info.is_direct()); | 589 EXPECT_TRUE(info.is_direct()); |
| 611 | 590 |
| 612 // Fallback 1. | 591 // Fallback 1. |
| 613 TestProxyFallbackNetworkDelegate network_delegate2; | |
| 614 TestCompletionCallback callback2; | 592 TestCompletionCallback callback2; |
| 615 rv = service.ReconsiderProxyAfterError(url, net::LOAD_NORMAL, | 593 rv = service.ReconsiderProxyAfterError(url, net::LOAD_NORMAL, |
| 616 net::ERR_PROXY_CONNECTION_FAILED, | 594 net::ERR_PROXY_CONNECTION_FAILED, |
| 617 &info, callback2.callback(), NULL, | 595 &info, callback2.callback(), NULL, |
| 618 NULL, BoundNetLog()); | 596 NULL, BoundNetLog()); |
| 619 EXPECT_EQ(OK, rv); | 597 EXPECT_EQ(OK, rv); |
| 620 EXPECT_FALSE(info.is_direct()); | 598 EXPECT_FALSE(info.is_direct()); |
| 621 EXPECT_EQ("foobar:10", info.proxy_server().ToURI()); | 599 EXPECT_EQ("foobar:10", info.proxy_server().ToURI()); |
| 622 // No network delegate provided. | |
| 623 EXPECT_FALSE(network_delegate2.on_proxy_fallback_called()); | |
| 624 | 600 |
| 625 // Fallback 2. | 601 // Fallback 2. |
| 626 TestProxyFallbackNetworkDelegate network_delegate3; | 602 NetworkDelegate network_delegate; |
| 627 ProxyServer expected_proxy_server3 = info.proxy_server(); | 603 ProxyServer expected_proxy_server3 = info.proxy_server(); |
| 628 TestCompletionCallback callback3; | 604 TestCompletionCallback callback3; |
| 629 rv = service.ReconsiderProxyAfterError(url, net::LOAD_NORMAL, | 605 rv = service.ReconsiderProxyAfterError(url, net::LOAD_NORMAL, |
| 630 net::ERR_PROXY_CONNECTION_FAILED, | 606 net::ERR_PROXY_CONNECTION_FAILED, |
| 631 &info, callback3.callback(), NULL, | 607 &info, callback3.callback(), NULL, |
| 632 &network_delegate3, BoundNetLog()); | 608 &network_delegate, BoundNetLog()); |
| 633 EXPECT_EQ(OK, rv); | 609 EXPECT_EQ(OK, rv); |
| 634 EXPECT_TRUE(info.is_direct()); | 610 EXPECT_TRUE(info.is_direct()); |
| 635 EXPECT_TRUE(network_delegate3.on_proxy_fallback_called()); | |
| 636 EXPECT_EQ(expected_proxy_server3, network_delegate3.proxy_server()); | |
| 637 EXPECT_EQ(net::ERR_PROXY_CONNECTION_FAILED, | |
| 638 network_delegate3.proxy_fallback_net_error()); | |
| 639 EXPECT_TRUE(network_delegate3.proxy_did_fallback()); | |
| 640 | 611 |
| 641 // Fallback 3. | 612 // Fallback 3. |
| 642 TestProxyFallbackNetworkDelegate network_delegate4; | |
| 643 ProxyServer expected_proxy_server4 = info.proxy_server(); | 613 ProxyServer expected_proxy_server4 = info.proxy_server(); |
| 644 TestCompletionCallback callback4; | 614 TestCompletionCallback callback4; |
| 645 rv = service.ReconsiderProxyAfterError(url, net::LOAD_NORMAL, | 615 rv = service.ReconsiderProxyAfterError(url, net::LOAD_NORMAL, |
| 646 net::ERR_PROXY_CONNECTION_FAILED, | 616 net::ERR_PROXY_CONNECTION_FAILED, |
| 647 &info, callback4.callback(), NULL, | 617 &info, callback4.callback(), NULL, |
| 648 &network_delegate4, BoundNetLog()); | 618 &network_delegate, BoundNetLog()); |
| 649 EXPECT_EQ(OK, rv); | 619 EXPECT_EQ(OK, rv); |
| 650 EXPECT_FALSE(info.is_direct()); | 620 EXPECT_FALSE(info.is_direct()); |
| 651 EXPECT_EQ("foobar:20", info.proxy_server().ToURI()); | 621 EXPECT_EQ("foobar:20", info.proxy_server().ToURI()); |
| 652 EXPECT_TRUE(network_delegate4.on_proxy_fallback_called()); | |
| 653 EXPECT_EQ(expected_proxy_server4, network_delegate4.proxy_server()); | |
| 654 EXPECT_EQ(net::ERR_PROXY_CONNECTION_FAILED, | |
| 655 network_delegate4.proxy_fallback_net_error()); | |
| 656 EXPECT_TRUE(network_delegate4.proxy_did_fallback()); | |
| 657 | 622 |
| 658 // Fallback 4 -- Nothing to fall back to! | 623 // Fallback 4 -- Nothing to fall back to! |
| 659 TestProxyFallbackNetworkDelegate network_delegate5; | |
| 660 ProxyServer expected_proxy_server5 = info.proxy_server(); | 624 ProxyServer expected_proxy_server5 = info.proxy_server(); |
| 661 TestCompletionCallback callback5; | 625 TestCompletionCallback callback5; |
| 662 rv = service.ReconsiderProxyAfterError(url, net::LOAD_NORMAL, | 626 rv = service.ReconsiderProxyAfterError(url, net::LOAD_NORMAL, |
| 663 net::ERR_PROXY_CONNECTION_FAILED, | 627 net::ERR_PROXY_CONNECTION_FAILED, |
| 664 &info, callback5.callback(), NULL, | 628 &info, callback5.callback(), NULL, |
| 665 &network_delegate5, BoundNetLog()); | 629 &network_delegate, BoundNetLog()); |
| 666 EXPECT_EQ(ERR_FAILED, rv); | 630 EXPECT_EQ(ERR_FAILED, rv); |
| 667 EXPECT_TRUE(info.is_empty()); | 631 EXPECT_TRUE(info.is_empty()); |
| 668 EXPECT_TRUE(network_delegate5.on_proxy_fallback_called()); | |
| 669 EXPECT_EQ(expected_proxy_server5, network_delegate5.proxy_server()); | |
| 670 EXPECT_EQ(net::ERR_PROXY_CONNECTION_FAILED, | |
| 671 network_delegate5.proxy_fallback_net_error()); | |
| 672 EXPECT_FALSE(network_delegate5.proxy_did_fallback()); | |
| 673 } | 632 } |
| 674 | 633 |
| 675 TEST_F(ProxyServiceTest, PAC_ConfigSourcePropagates) { | 634 TEST_F(ProxyServiceTest, PAC_ConfigSourcePropagates) { |
| 676 // Test whether the ProxyConfigSource set by the ProxyConfigService is applied | 635 // Test whether the ProxyConfigSource set by the ProxyConfigService is applied |
| 677 // to ProxyInfo after the proxy is resolved via a PAC script. | 636 // to ProxyInfo after the proxy is resolved via a PAC script. |
| 678 ProxyConfig config = | 637 ProxyConfig config = |
| 679 ProxyConfig::CreateFromCustomPacURL(GURL("http://foopy/proxy.pac")); | 638 ProxyConfig::CreateFromCustomPacURL(GURL("http://foopy/proxy.pac")); |
| 680 config.set_source(PROXY_CONFIG_SOURCE_TEST); | 639 config.set_source(PROXY_CONFIG_SOURCE_TEST); |
| 681 | 640 |
| 682 MockProxyConfigService* config_service = new MockProxyConfigService(config); | 641 MockProxyConfigService* config_service = new MockProxyConfigService(config); |
| (...skipping 294 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 977 EXPECT_EQ(OK, rv); | 936 EXPECT_EQ(OK, rv); |
| 978 | 937 |
| 979 // Proxy times should not have been modified by fallback. | 938 // Proxy times should not have been modified by fallback. |
| 980 EXPECT_EQ(proxy_resolve_start_time, info.proxy_resolve_start_time()); | 939 EXPECT_EQ(proxy_resolve_start_time, info.proxy_resolve_start_time()); |
| 981 EXPECT_EQ(proxy_resolve_end_time, info.proxy_resolve_end_time()); | 940 EXPECT_EQ(proxy_resolve_end_time, info.proxy_resolve_end_time()); |
| 982 | 941 |
| 983 // The second proxy should be specified. | 942 // The second proxy should be specified. |
| 984 EXPECT_EQ("foopy2:9090", info.proxy_server().ToURI()); | 943 EXPECT_EQ("foopy2:9090", info.proxy_server().ToURI()); |
| 985 // Report back that the second proxy worked. This will globally mark the | 944 // Report back that the second proxy worked. This will globally mark the |
| 986 // first proxy as bad. | 945 // first proxy as bad. |
| 987 service.ReportSuccess(info); | 946 TestNotifyProxyFallbackNetworkDelegate test_delegate; |
| 947 service.ReportSuccess(info, &test_delegate); |
| 948 EXPECT_EQ(info.proxy_server(), test_delegate.bad_proxy()); |
| 949 EXPECT_EQ(net::ERR_PROXY_CONNECTION_FAILED, test_delegate.net_error()); |
| 988 | 950 |
| 989 TestCompletionCallback callback3; | 951 TestCompletionCallback callback3; |
| 990 rv = service.ResolveProxy( | 952 rv = service.ResolveProxy( |
| 991 url, net::LOAD_NORMAL, &info, callback3.callback(), NULL, NULL, | 953 url, net::LOAD_NORMAL, &info, callback3.callback(), NULL, NULL, |
| 992 BoundNetLog()); | 954 BoundNetLog()); |
| 993 EXPECT_EQ(ERR_IO_PENDING, rv); | 955 EXPECT_EQ(ERR_IO_PENDING, rv); |
| 994 | 956 |
| 995 ASSERT_EQ(1u, resolver->pending_requests().size()); | 957 ASSERT_EQ(1u, resolver->pending_requests().size()); |
| 996 EXPECT_EQ(url, resolver->pending_requests()[0]->url()); | 958 EXPECT_EQ(url, resolver->pending_requests()[0]->url()); |
| 997 | 959 |
| (...skipping 2097 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3095 ProxyInfo info3; | 3057 ProxyInfo info3; |
| 3096 TestCompletionCallback callback3; | 3058 TestCompletionCallback callback3; |
| 3097 rv = service.ResolveProxy( | 3059 rv = service.ResolveProxy( |
| 3098 GURL("http://request3"), net::LOAD_NORMAL, &info3, callback3.callback(), | 3060 GURL("http://request3"), net::LOAD_NORMAL, &info3, callback3.callback(), |
| 3099 NULL, NULL, BoundNetLog()); | 3061 NULL, NULL, BoundNetLog()); |
| 3100 EXPECT_EQ(OK, rv); | 3062 EXPECT_EQ(OK, rv); |
| 3101 EXPECT_TRUE(info3.is_direct()); | 3063 EXPECT_TRUE(info3.is_direct()); |
| 3102 } | 3064 } |
| 3103 | 3065 |
| 3104 } // namespace net | 3066 } // namespace net |
| OLD | NEW |