| 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 20 matching lines...) Expand all  Loading... | 
| 31 //              ProxyService::SuspendAllPendingRequests(). | 31 //              ProxyService::SuspendAllPendingRequests(). | 
| 32 namespace net { | 32 namespace net { | 
| 33 namespace { | 33 namespace { | 
| 34 | 34 | 
| 35 // This polling policy will decide to poll every 1 ms. | 35 // This polling policy will decide to poll every 1 ms. | 
| 36 class ImmediatePollPolicy : public ProxyService::PacPollPolicy { | 36 class ImmediatePollPolicy : public ProxyService::PacPollPolicy { | 
| 37  public: | 37  public: | 
| 38   ImmediatePollPolicy() {} | 38   ImmediatePollPolicy() {} | 
| 39 | 39 | 
| 40   virtual Mode GetNextDelay(int error, base::TimeDelta current_delay, | 40   virtual Mode GetNextDelay(int error, base::TimeDelta current_delay, | 
| 41                             base::TimeDelta* next_delay) const OVERRIDE { | 41                             base::TimeDelta* next_delay) const override { | 
| 42     *next_delay = base::TimeDelta::FromMilliseconds(1); | 42     *next_delay = base::TimeDelta::FromMilliseconds(1); | 
| 43     return MODE_USE_TIMER; | 43     return MODE_USE_TIMER; | 
| 44   } | 44   } | 
| 45 | 45 | 
| 46  private: | 46  private: | 
| 47   DISALLOW_COPY_AND_ASSIGN(ImmediatePollPolicy); | 47   DISALLOW_COPY_AND_ASSIGN(ImmediatePollPolicy); | 
| 48 }; | 48 }; | 
| 49 | 49 | 
| 50 // This polling policy chooses a fantastically large delay. In other words, it | 50 // This polling policy chooses a fantastically large delay. In other words, it | 
| 51 // will never trigger a poll | 51 // will never trigger a poll | 
| 52 class NeverPollPolicy : public ProxyService::PacPollPolicy { | 52 class NeverPollPolicy : public ProxyService::PacPollPolicy { | 
| 53  public: | 53  public: | 
| 54   NeverPollPolicy() {} | 54   NeverPollPolicy() {} | 
| 55 | 55 | 
| 56   virtual Mode GetNextDelay(int error, base::TimeDelta current_delay, | 56   virtual Mode GetNextDelay(int error, base::TimeDelta current_delay, | 
| 57                             base::TimeDelta* next_delay) const OVERRIDE { | 57                             base::TimeDelta* next_delay) const override { | 
| 58     *next_delay = base::TimeDelta::FromDays(60); | 58     *next_delay = base::TimeDelta::FromDays(60); | 
| 59     return MODE_USE_TIMER; | 59     return MODE_USE_TIMER; | 
| 60   } | 60   } | 
| 61 | 61 | 
| 62  private: | 62  private: | 
| 63   DISALLOW_COPY_AND_ASSIGN(NeverPollPolicy); | 63   DISALLOW_COPY_AND_ASSIGN(NeverPollPolicy); | 
| 64 }; | 64 }; | 
| 65 | 65 | 
| 66 // This polling policy starts a poll immediately after network activity. | 66 // This polling policy starts a poll immediately after network activity. | 
| 67 class ImmediateAfterActivityPollPolicy : public ProxyService::PacPollPolicy { | 67 class ImmediateAfterActivityPollPolicy : public ProxyService::PacPollPolicy { | 
| 68  public: | 68  public: | 
| 69   ImmediateAfterActivityPollPolicy() {} | 69   ImmediateAfterActivityPollPolicy() {} | 
| 70 | 70 | 
| 71   virtual Mode GetNextDelay(int error, base::TimeDelta current_delay, | 71   virtual Mode GetNextDelay(int error, base::TimeDelta current_delay, | 
| 72                             base::TimeDelta* next_delay) const OVERRIDE { | 72                             base::TimeDelta* next_delay) const override { | 
| 73     *next_delay = base::TimeDelta(); | 73     *next_delay = base::TimeDelta(); | 
| 74     return MODE_START_AFTER_ACTIVITY; | 74     return MODE_START_AFTER_ACTIVITY; | 
| 75   } | 75   } | 
| 76 | 76 | 
| 77  private: | 77  private: | 
| 78   DISALLOW_COPY_AND_ASSIGN(ImmediateAfterActivityPollPolicy); | 78   DISALLOW_COPY_AND_ASSIGN(ImmediateAfterActivityPollPolicy); | 
| 79 }; | 79 }; | 
| 80 | 80 | 
| 81 // This test fixture is used to partially disable the background polling done by | 81 // This test fixture is used to partially disable the background polling done by | 
| 82 // the ProxyService (which it uses to detect whenever its PAC script contents or | 82 // the ProxyService (which it uses to detect whenever its PAC script contents or | 
| 83 // WPAD results have changed). | 83 // WPAD results have changed). | 
| 84 // | 84 // | 
| 85 // We disable the feature by setting the poll interval to something really | 85 // We disable the feature by setting the poll interval to something really | 
| 86 // large, so it will never actually be reached even on the slowest bots that run | 86 // large, so it will never actually be reached even on the slowest bots that run | 
| 87 // these tests. | 87 // these tests. | 
| 88 // | 88 // | 
| 89 // We disable the polling in order to avoid any timing dependencies in the | 89 // We disable the polling in order to avoid any timing dependencies in the | 
| 90 // tests. If the bot were to run the tests very slowly and we hadn't disabled | 90 // tests. If the bot were to run the tests very slowly and we hadn't disabled | 
| 91 // polling, then it might start a background re-try in the middle of our test | 91 // polling, then it might start a background re-try in the middle of our test | 
| 92 // and confuse our expectations leading to flaky failures. | 92 // and confuse our expectations leading to flaky failures. | 
| 93 // | 93 // | 
| 94 // The tests which verify the polling code re-enable the polling behavior but | 94 // The tests which verify the polling code re-enable the polling behavior but | 
| 95 // are careful to avoid timing problems. | 95 // are careful to avoid timing problems. | 
| 96 class ProxyServiceTest : public testing::Test { | 96 class ProxyServiceTest : public testing::Test { | 
| 97  protected: | 97  protected: | 
| 98   virtual void SetUp() OVERRIDE { | 98   virtual void SetUp() override { | 
| 99     testing::Test::SetUp(); | 99     testing::Test::SetUp(); | 
| 100     previous_policy_ = | 100     previous_policy_ = | 
| 101         ProxyService::set_pac_script_poll_policy(&never_poll_policy_); | 101         ProxyService::set_pac_script_poll_policy(&never_poll_policy_); | 
| 102   } | 102   } | 
| 103 | 103 | 
| 104   virtual void TearDown() OVERRIDE { | 104   virtual void TearDown() override { | 
| 105     // Restore the original policy. | 105     // Restore the original policy. | 
| 106     ProxyService::set_pac_script_poll_policy(previous_policy_); | 106     ProxyService::set_pac_script_poll_policy(previous_policy_); | 
| 107     testing::Test::TearDown(); | 107     testing::Test::TearDown(); | 
| 108   } | 108   } | 
| 109 | 109 | 
| 110  private: | 110  private: | 
| 111   NeverPollPolicy never_poll_policy_; | 111   NeverPollPolicy never_poll_policy_; | 
| 112   const ProxyService::PacPollPolicy* previous_policy_; | 112   const ProxyService::PacPollPolicy* previous_policy_; | 
| 113 }; | 113 }; | 
| 114 | 114 | 
| 115 const char kValidPacScript1[] = "pac-script-v1-FindProxyForURL"; | 115 const char kValidPacScript1[] = "pac-script-v1-FindProxyForURL"; | 
| 116 const char kValidPacScript2[] = "pac-script-v2-FindProxyForURL"; | 116 const char kValidPacScript2[] = "pac-script-v2-FindProxyForURL"; | 
| 117 | 117 | 
| 118 class MockProxyConfigService: public ProxyConfigService { | 118 class MockProxyConfigService: public ProxyConfigService { | 
| 119  public: | 119  public: | 
| 120   explicit MockProxyConfigService(const ProxyConfig& config) | 120   explicit MockProxyConfigService(const ProxyConfig& config) | 
| 121       : availability_(CONFIG_VALID), | 121       : availability_(CONFIG_VALID), | 
| 122         config_(config) { | 122         config_(config) { | 
| 123   } | 123   } | 
| 124 | 124 | 
| 125   explicit MockProxyConfigService(const std::string& pac_url) | 125   explicit MockProxyConfigService(const std::string& pac_url) | 
| 126       : availability_(CONFIG_VALID), | 126       : availability_(CONFIG_VALID), | 
| 127         config_(ProxyConfig::CreateFromCustomPacURL(GURL(pac_url))) { | 127         config_(ProxyConfig::CreateFromCustomPacURL(GURL(pac_url))) { | 
| 128   } | 128   } | 
| 129 | 129 | 
| 130   virtual void AddObserver(Observer* observer) OVERRIDE { | 130   virtual void AddObserver(Observer* observer) override { | 
| 131     observers_.AddObserver(observer); | 131     observers_.AddObserver(observer); | 
| 132   } | 132   } | 
| 133 | 133 | 
| 134   virtual void RemoveObserver(Observer* observer) OVERRIDE { | 134   virtual void RemoveObserver(Observer* observer) override { | 
| 135     observers_.RemoveObserver(observer); | 135     observers_.RemoveObserver(observer); | 
| 136   } | 136   } | 
| 137 | 137 | 
| 138   virtual ConfigAvailability GetLatestProxyConfig(ProxyConfig* results) | 138   virtual ConfigAvailability GetLatestProxyConfig(ProxyConfig* results) | 
| 139       OVERRIDE { | 139       override { | 
| 140     if (availability_ == CONFIG_VALID) | 140     if (availability_ == CONFIG_VALID) | 
| 141       *results = config_; | 141       *results = config_; | 
| 142     return availability_; | 142     return availability_; | 
| 143   } | 143   } | 
| 144 | 144 | 
| 145   void SetConfig(const ProxyConfig& config) { | 145   void SetConfig(const ProxyConfig& config) { | 
| 146     availability_ = CONFIG_VALID; | 146     availability_ = CONFIG_VALID; | 
| 147     config_ = config; | 147     config_ = config; | 
| 148     FOR_EACH_OBSERVER(Observer, observers_, | 148     FOR_EACH_OBSERVER(Observer, observers_, | 
| 149                       OnProxyConfigChanged(config_, availability_)); | 149                       OnProxyConfigChanged(config_, availability_)); | 
| (...skipping 11 matching lines...) Expand all  Loading... | 
| 161   TestResolveProxyNetworkDelegate() | 161   TestResolveProxyNetworkDelegate() | 
| 162       : on_resolve_proxy_called_(false), | 162       : on_resolve_proxy_called_(false), | 
| 163         add_proxy_(false), | 163         add_proxy_(false), | 
| 164         remove_proxy_(false), | 164         remove_proxy_(false), | 
| 165         proxy_service_(NULL) { | 165         proxy_service_(NULL) { | 
| 166   } | 166   } | 
| 167 | 167 | 
| 168   virtual void OnResolveProxy(const GURL& url, | 168   virtual void OnResolveProxy(const GURL& url, | 
| 169                               int load_flags, | 169                               int load_flags, | 
| 170                               const ProxyService& proxy_service, | 170                               const ProxyService& proxy_service, | 
| 171                               ProxyInfo* result) OVERRIDE { | 171                               ProxyInfo* result) override { | 
| 172     on_resolve_proxy_called_ = true; | 172     on_resolve_proxy_called_ = true; | 
| 173     proxy_service_ = &proxy_service; | 173     proxy_service_ = &proxy_service; | 
| 174     DCHECK(!add_proxy_ || !remove_proxy_); | 174     DCHECK(!add_proxy_ || !remove_proxy_); | 
| 175     if (add_proxy_) { | 175     if (add_proxy_) { | 
| 176       result->UseNamedProxy("delegate_proxy.com"); | 176       result->UseNamedProxy("delegate_proxy.com"); | 
| 177     } else if (remove_proxy_) { | 177     } else if (remove_proxy_) { | 
| 178       result->UseDirect(); | 178       result->UseDirect(); | 
| 179     } | 179     } | 
| 180   } | 180   } | 
| 181 | 181 | 
| (...skipping 22 matching lines...) Expand all  Loading... | 
| 204 | 204 | 
| 205 // A test network delegate that exercises the OnProxyFallback callback. | 205 // A test network delegate that exercises the OnProxyFallback callback. | 
| 206 class TestProxyFallbackNetworkDelegate : public NetworkDelegate { | 206 class TestProxyFallbackNetworkDelegate : public NetworkDelegate { | 
| 207  public: | 207  public: | 
| 208   TestProxyFallbackNetworkDelegate() | 208   TestProxyFallbackNetworkDelegate() | 
| 209       : on_proxy_fallback_called_(false), | 209       : on_proxy_fallback_called_(false), | 
| 210         proxy_fallback_net_error_(OK) { | 210         proxy_fallback_net_error_(OK) { | 
| 211   } | 211   } | 
| 212 | 212 | 
| 213   virtual void OnProxyFallback(const ProxyServer& proxy_server, | 213   virtual void OnProxyFallback(const ProxyServer& proxy_server, | 
| 214                                int net_error) OVERRIDE { | 214                                int net_error) override { | 
| 215     proxy_server_ = proxy_server; | 215     proxy_server_ = proxy_server; | 
| 216     proxy_fallback_net_error_ = net_error; | 216     proxy_fallback_net_error_ = net_error; | 
| 217     on_proxy_fallback_called_ = true; | 217     on_proxy_fallback_called_ = true; | 
| 218   } | 218   } | 
| 219 | 219 | 
| 220   bool on_proxy_fallback_called() const { | 220   bool on_proxy_fallback_called() const { | 
| 221     return on_proxy_fallback_called_; | 221     return on_proxy_fallback_called_; | 
| 222   } | 222   } | 
| 223 | 223 | 
| 224   const ProxyServer& proxy_server() const { | 224   const ProxyServer& proxy_server() const { | 
| (...skipping 2850 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 3075   ProxyInfo info3; | 3075   ProxyInfo info3; | 
| 3076   TestCompletionCallback callback3; | 3076   TestCompletionCallback callback3; | 
| 3077   rv = service.ResolveProxy( | 3077   rv = service.ResolveProxy( | 
| 3078       GURL("http://request3"), net::LOAD_NORMAL, &info3, callback3.callback(), | 3078       GURL("http://request3"), net::LOAD_NORMAL, &info3, callback3.callback(), | 
| 3079       NULL, NULL, BoundNetLog()); | 3079       NULL, NULL, BoundNetLog()); | 
| 3080   EXPECT_EQ(OK, rv); | 3080   EXPECT_EQ(OK, rv); | 
| 3081   EXPECT_TRUE(info3.is_direct()); | 3081   EXPECT_TRUE(info3.is_direct()); | 
| 3082 } | 3082 } | 
| 3083 | 3083 | 
| 3084 }  // namespace net | 3084 }  // namespace net | 
| OLD | NEW | 
|---|