| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "chrome/browser/net/pref_proxy_config_service.h" | 5 #include "chrome/browser/net/pref_proxy_config_service.h" |
| 6 | 6 |
| 7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
| 8 #include "base/file_path.h" | 8 #include "base/file_path.h" |
| 9 #include "chrome/browser/net/chrome_url_request_context.h" | 9 #include "chrome/browser/net/chrome_url_request_context.h" |
| 10 #include "chrome/browser/prefs/pref_service_mock_builder.h" | 10 #include "chrome/browser/prefs/pref_service_mock_builder.h" |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 60 MOCK_METHOD1(OnProxyConfigChanged, void(const net::ProxyConfig&)); | 60 MOCK_METHOD1(OnProxyConfigChanged, void(const net::ProxyConfig&)); |
| 61 }; | 61 }; |
| 62 | 62 |
| 63 template<typename TESTBASE> | 63 template<typename TESTBASE> |
| 64 class PrefProxyConfigServiceTestBase : public TESTBASE { | 64 class PrefProxyConfigServiceTestBase : public TESTBASE { |
| 65 protected: | 65 protected: |
| 66 PrefProxyConfigServiceTestBase() | 66 PrefProxyConfigServiceTestBase() |
| 67 : ui_thread_(BrowserThread::UI, &loop_), | 67 : ui_thread_(BrowserThread::UI, &loop_), |
| 68 io_thread_(BrowserThread::IO, &loop_) {} | 68 io_thread_(BrowserThread::IO, &loop_) {} |
| 69 | 69 |
| 70 virtual void Init(PrefService* pref_service) { | 70 virtual void Init(PrefService* pref_service, PrefService* fallback) { |
| 71 ASSERT_TRUE(pref_service); | 71 ASSERT_TRUE(pref_service); |
| 72 PrefProxyConfigService::RegisterPrefs(pref_service); | 72 PrefProxyConfigService::RegisterPrefs(pref_service); |
| 73 if (fallback) |
| 74 PrefProxyConfigService::RegisterPrefs(fallback); |
| 73 fixed_config_.set_pac_url(GURL(kFixedPacUrl)); | 75 fixed_config_.set_pac_url(GURL(kFixedPacUrl)); |
| 74 delegate_service_ = new TestProxyConfigService(fixed_config_); | 76 delegate_service_ = new TestProxyConfigService(fixed_config_); |
| 75 proxy_config_tracker_ = new PrefProxyConfigTracker(pref_service); | 77 proxy_config_tracker_ = new PrefProxyConfigTracker(pref_service, fallback); |
| 76 proxy_config_service_.reset( | 78 proxy_config_service_.reset( |
| 77 new PrefProxyConfigService(proxy_config_tracker_.get(), | 79 new PrefProxyConfigService(proxy_config_tracker_.get(), |
| 78 delegate_service_)); | 80 delegate_service_)); |
| 79 } | 81 } |
| 80 | 82 |
| 81 virtual void TearDown() { | 83 virtual void TearDown() { |
| 82 proxy_config_tracker_->DetachFromPrefService(); | 84 proxy_config_tracker_->DetachFromPrefService(); |
| 83 loop_.RunAllPending(); | 85 loop_.RunAllPending(); |
| 84 proxy_config_service_.reset(); | 86 proxy_config_service_.reset(); |
| 85 } | 87 } |
| 86 | 88 |
| 87 MessageLoop loop_; | 89 MessageLoop loop_; |
| 88 TestProxyConfigService* delegate_service_; // weak | 90 TestProxyConfigService* delegate_service_; // weak |
| 89 scoped_ptr<PrefProxyConfigService> proxy_config_service_; | 91 scoped_ptr<PrefProxyConfigService> proxy_config_service_; |
| 90 net::ProxyConfig fixed_config_; | 92 net::ProxyConfig fixed_config_; |
| 91 | 93 |
| 92 private: | 94 private: |
| 93 scoped_refptr<PrefProxyConfigTracker> proxy_config_tracker_; | 95 scoped_refptr<PrefProxyConfigTracker> proxy_config_tracker_; |
| 94 BrowserThread ui_thread_; | 96 BrowserThread ui_thread_; |
| 95 BrowserThread io_thread_; | 97 BrowserThread io_thread_; |
| 96 }; | 98 }; |
| 97 | 99 |
| 98 class PrefProxyConfigServiceTest | 100 class PrefProxyConfigServiceTest |
| 99 : public PrefProxyConfigServiceTestBase<testing::Test> { | 101 : public PrefProxyConfigServiceTestBase<testing::Test> { |
| 100 protected: | 102 protected: |
| 101 virtual void SetUp() { | 103 virtual void SetUp() { |
| 102 pref_service_.reset(new TestingPrefService()); | 104 Init(&pref_service_, NULL); |
| 103 Init(pref_service_.get()); | |
| 104 } | 105 } |
| 105 | 106 |
| 106 scoped_ptr<TestingPrefService> pref_service_; | 107 TestingPrefService pref_service_; |
| 107 }; | 108 }; |
| 108 | 109 |
| 109 TEST_F(PrefProxyConfigServiceTest, BaseConfiguration) { | 110 TEST_F(PrefProxyConfigServiceTest, BaseConfiguration) { |
| 110 net::ProxyConfig actual_config; | 111 net::ProxyConfig actual_config; |
| 111 proxy_config_service_->GetLatestProxyConfig(&actual_config); | 112 proxy_config_service_->GetLatestProxyConfig(&actual_config); |
| 112 EXPECT_EQ(GURL(kFixedPacUrl), actual_config.pac_url()); | 113 EXPECT_EQ(GURL(kFixedPacUrl), actual_config.pac_url()); |
| 113 } | 114 } |
| 114 | 115 |
| 115 TEST_F(PrefProxyConfigServiceTest, DynamicPrefOverrides) { | 116 TEST_F(PrefProxyConfigServiceTest, DynamicPrefOverrides) { |
| 116 pref_service_->SetManagedPref( | 117 pref_service_.SetManagedPref( |
| 117 prefs::kProxy, | 118 prefs::kProxy, |
| 118 ProxyConfigDictionary::CreateFixedServers("http://example.com:3128", "")); | 119 ProxyConfigDictionary::CreateFixedServers("http://example.com:3128", "")); |
| 119 loop_.RunAllPending(); | 120 loop_.RunAllPending(); |
| 120 | 121 |
| 121 net::ProxyConfig actual_config; | 122 net::ProxyConfig actual_config; |
| 122 proxy_config_service_->GetLatestProxyConfig(&actual_config); | 123 proxy_config_service_->GetLatestProxyConfig(&actual_config); |
| 123 EXPECT_FALSE(actual_config.auto_detect()); | 124 EXPECT_FALSE(actual_config.auto_detect()); |
| 124 EXPECT_EQ(net::ProxyConfig::ProxyRules::TYPE_SINGLE_PROXY, | 125 EXPECT_EQ(net::ProxyConfig::ProxyRules::TYPE_SINGLE_PROXY, |
| 125 actual_config.proxy_rules().type); | 126 actual_config.proxy_rules().type); |
| 126 EXPECT_EQ(actual_config.proxy_rules().single_proxy, | 127 EXPECT_EQ(actual_config.proxy_rules().single_proxy, |
| 127 net::ProxyServer::FromURI("http://example.com:3128", | 128 net::ProxyServer::FromURI("http://example.com:3128", |
| 128 net::ProxyServer::SCHEME_HTTP)); | 129 net::ProxyServer::SCHEME_HTTP)); |
| 129 | 130 |
| 130 pref_service_->SetManagedPref(prefs::kProxy, | 131 pref_service_.SetManagedPref(prefs::kProxy, |
| 131 ProxyConfigDictionary::CreateAutoDetect()); | 132 ProxyConfigDictionary::CreateAutoDetect()); |
| 132 loop_.RunAllPending(); | 133 loop_.RunAllPending(); |
| 133 | 134 |
| 134 proxy_config_service_->GetLatestProxyConfig(&actual_config); | 135 proxy_config_service_->GetLatestProxyConfig(&actual_config); |
| 135 EXPECT_TRUE(actual_config.auto_detect()); | 136 EXPECT_TRUE(actual_config.auto_detect()); |
| 136 } | 137 } |
| 137 | 138 |
| 138 // Compares proxy configurations, but allows different identifiers. | 139 // Compares proxy configurations, but allows different identifiers. |
| 139 MATCHER_P(ProxyConfigMatches, config, "") { | 140 MATCHER_P(ProxyConfigMatches, config, "") { |
| 140 net::ProxyConfig reference(config); | 141 net::ProxyConfig reference(config); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 154 delegate_service_->SetProxyConfig(config2); | 155 delegate_service_->SetProxyConfig(config2); |
| 155 loop_.RunAllPending(); | 156 loop_.RunAllPending(); |
| 156 Mock::VerifyAndClearExpectations(&observer); | 157 Mock::VerifyAndClearExpectations(&observer); |
| 157 | 158 |
| 158 // Override configuration, this should trigger a notification. | 159 // Override configuration, this should trigger a notification. |
| 159 net::ProxyConfig pref_config; | 160 net::ProxyConfig pref_config; |
| 160 pref_config.set_pac_url(GURL(kFixedPacUrl)); | 161 pref_config.set_pac_url(GURL(kFixedPacUrl)); |
| 161 | 162 |
| 162 EXPECT_CALL(observer, | 163 EXPECT_CALL(observer, |
| 163 OnProxyConfigChanged(ProxyConfigMatches(pref_config))).Times(1); | 164 OnProxyConfigChanged(ProxyConfigMatches(pref_config))).Times(1); |
| 164 pref_service_->SetManagedPref( | 165 pref_service_.SetManagedPref( |
| 165 prefs::kProxy, | 166 prefs::kProxy, |
| 166 ProxyConfigDictionary::CreatePacScript(kFixedPacUrl)); | 167 ProxyConfigDictionary::CreatePacScript(kFixedPacUrl)); |
| 167 loop_.RunAllPending(); | 168 loop_.RunAllPending(); |
| 168 Mock::VerifyAndClearExpectations(&observer); | 169 Mock::VerifyAndClearExpectations(&observer); |
| 169 | 170 |
| 170 // Since there are pref overrides, delegate changes should be ignored. | 171 // Since there are pref overrides, delegate changes should be ignored. |
| 171 net::ProxyConfig config3; | 172 net::ProxyConfig config3; |
| 172 config3.proxy_rules().ParseFromString("http=config3:80"); | 173 config3.proxy_rules().ParseFromString("http=config3:80"); |
| 173 EXPECT_CALL(observer, OnProxyConfigChanged(_)).Times(0); | 174 EXPECT_CALL(observer, OnProxyConfigChanged(_)).Times(0); |
| 174 fixed_config_.set_auto_detect(true); | 175 fixed_config_.set_auto_detect(true); |
| 175 delegate_service_->SetProxyConfig(config3); | 176 delegate_service_->SetProxyConfig(config3); |
| 176 loop_.RunAllPending(); | 177 loop_.RunAllPending(); |
| 177 Mock::VerifyAndClearExpectations(&observer); | 178 Mock::VerifyAndClearExpectations(&observer); |
| 178 | 179 |
| 179 // Clear the override should switch back to the fixed configuration. | 180 // Clear the override should switch back to the fixed configuration. |
| 180 EXPECT_CALL(observer, | 181 EXPECT_CALL(observer, |
| 181 OnProxyConfigChanged(ProxyConfigMatches(config3))).Times(1); | 182 OnProxyConfigChanged(ProxyConfigMatches(config3))).Times(1); |
| 182 pref_service_->RemoveManagedPref(prefs::kProxy); | 183 pref_service_.RemoveManagedPref(prefs::kProxy); |
| 183 loop_.RunAllPending(); | 184 loop_.RunAllPending(); |
| 184 Mock::VerifyAndClearExpectations(&observer); | 185 Mock::VerifyAndClearExpectations(&observer); |
| 185 | 186 |
| 186 // Delegate service notifications should show up again. | 187 // Delegate service notifications should show up again. |
| 187 net::ProxyConfig config4; | 188 net::ProxyConfig config4; |
| 188 config4.proxy_rules().ParseFromString("socks:config4"); | 189 config4.proxy_rules().ParseFromString("socks:config4"); |
| 189 EXPECT_CALL(observer, | 190 EXPECT_CALL(observer, |
| 190 OnProxyConfigChanged(ProxyConfigMatches(config4))).Times(1); | 191 OnProxyConfigChanged(ProxyConfigMatches(config4))).Times(1); |
| 191 delegate_service_->SetProxyConfig(config4); | 192 delegate_service_->SetProxyConfig(config4); |
| 192 loop_.RunAllPending(); | 193 loop_.RunAllPending(); |
| 193 Mock::VerifyAndClearExpectations(&observer); | 194 Mock::VerifyAndClearExpectations(&observer); |
| 194 | 195 |
| 195 proxy_config_service_->RemoveObserver(&observer); | 196 proxy_config_service_->RemoveObserver(&observer); |
| 196 } | 197 } |
| 197 | 198 |
| 199 class PrefProxyConfigServiceFallbackTest |
| 200 : public PrefProxyConfigServiceTestBase<testing::Test> { |
| 201 protected: |
| 202 virtual void SetUp() { |
| 203 Init(&pref_service_, &fallback_pref_service_); |
| 204 } |
| 205 |
| 206 TestingPrefService pref_service_; |
| 207 TestingPrefService fallback_pref_service_; |
| 208 }; |
| 209 |
| 210 TEST_F(PrefProxyConfigServiceFallbackTest, NoFallback) { |
| 211 pref_service_.SetUserPref( |
| 212 prefs::kProxy, |
| 213 ProxyConfigDictionary::CreateFixedServers("http://example.com:3128", "")); |
| 214 loop_.RunAllPending(); |
| 215 |
| 216 net::ProxyConfig actual_config; |
| 217 proxy_config_service_->GetLatestProxyConfig(&actual_config); |
| 218 EXPECT_FALSE(actual_config.auto_detect()); |
| 219 EXPECT_EQ(net::ProxyConfig::ProxyRules::TYPE_SINGLE_PROXY, |
| 220 actual_config.proxy_rules().type); |
| 221 EXPECT_EQ(actual_config.proxy_rules().single_proxy, |
| 222 net::ProxyServer::FromURI("http://example.com:3128", |
| 223 net::ProxyServer::SCHEME_HTTP)); |
| 224 } |
| 225 |
| 226 TEST_F(PrefProxyConfigServiceFallbackTest, Fallback) { |
| 227 fallback_pref_service_.SetUserPref( |
| 228 prefs::kProxy, |
| 229 ProxyConfigDictionary::CreateFixedServers("http://example.com:3128", "")); |
| 230 loop_.RunAllPending(); |
| 231 |
| 232 net::ProxyConfig actual_config; |
| 233 proxy_config_service_->GetLatestProxyConfig(&actual_config); |
| 234 EXPECT_FALSE(actual_config.auto_detect()); |
| 235 EXPECT_EQ(net::ProxyConfig::ProxyRules::TYPE_SINGLE_PROXY, |
| 236 actual_config.proxy_rules().type); |
| 237 EXPECT_EQ(actual_config.proxy_rules().single_proxy, |
| 238 net::ProxyServer::FromURI("http://example.com:3128", |
| 239 net::ProxyServer::SCHEME_HTTP)); |
| 240 } |
| 241 |
| 242 TEST_F(PrefProxyConfigServiceFallbackTest, FallbackWithPolicy) { |
| 243 pref_service_.SetManagedPref( |
| 244 prefs::kProxy, |
| 245 ProxyConfigDictionary::CreateAutoDetect()); |
| 246 fallback_pref_service_.SetUserPref( |
| 247 prefs::kProxy, |
| 248 ProxyConfigDictionary::CreateFixedServers("http://example.com:3128", "")); |
| 249 loop_.RunAllPending(); |
| 250 |
| 251 net::ProxyConfig actual_config; |
| 252 proxy_config_service_->GetLatestProxyConfig(&actual_config); |
| 253 EXPECT_TRUE(actual_config.auto_detect()); |
| 254 } |
| 255 |
| 256 TEST_F(PrefProxyConfigServiceFallbackTest, FallbackPolicy) { |
| 257 fallback_pref_service_.SetManagedPref( |
| 258 prefs::kProxy, |
| 259 ProxyConfigDictionary::CreateFixedServers("http://example.com:3128", "")); |
| 260 pref_service_.SetUserPref( |
| 261 prefs::kProxy, |
| 262 ProxyConfigDictionary::CreateAutoDetect()); |
| 263 loop_.RunAllPending(); |
| 264 |
| 265 net::ProxyConfig actual_config; |
| 266 proxy_config_service_->GetLatestProxyConfig(&actual_config); |
| 267 EXPECT_TRUE(actual_config.auto_detect()); |
| 268 } |
| 269 |
| 198 // Test parameter object for testing command line proxy configuration. | 270 // Test parameter object for testing command line proxy configuration. |
| 199 struct CommandLineTestParams { | 271 struct CommandLineTestParams { |
| 200 // Explicit assignment operator, so testing::TestWithParam works with MSVC. | 272 // Explicit assignment operator, so testing::TestWithParam works with MSVC. |
| 201 CommandLineTestParams& operator=(const CommandLineTestParams& other) { | 273 CommandLineTestParams& operator=(const CommandLineTestParams& other) { |
| 202 description = other.description; | 274 description = other.description; |
| 203 for (unsigned int i = 0; i < arraysize(switches); i++) | 275 for (unsigned int i = 0; i < arraysize(switches); i++) |
| 204 switches[i] = other.switches[i]; | 276 switches[i] = other.switches[i]; |
| 205 is_null = other.is_null; | 277 is_null = other.is_null; |
| 206 auto_detect = other.auto_detect; | 278 auto_detect = other.auto_detect; |
| 207 pac_url = other.pac_url; | 279 pac_url = other.pac_url; |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 240 for (size_t i = 0; i < arraysize(GetParam().switches); i++) { | 312 for (size_t i = 0; i < arraysize(GetParam().switches); i++) { |
| 241 const char* name = GetParam().switches[i].name; | 313 const char* name = GetParam().switches[i].name; |
| 242 const char* value = GetParam().switches[i].value; | 314 const char* value = GetParam().switches[i].value; |
| 243 if (name && value) | 315 if (name && value) |
| 244 command_line_.AppendSwitchASCII(name, value); | 316 command_line_.AppendSwitchASCII(name, value); |
| 245 else if (name) | 317 else if (name) |
| 246 command_line_.AppendSwitch(name); | 318 command_line_.AppendSwitch(name); |
| 247 } | 319 } |
| 248 pref_service_.reset( | 320 pref_service_.reset( |
| 249 PrefServiceMockBuilder().WithCommandLine(&command_line_).Create()); | 321 PrefServiceMockBuilder().WithCommandLine(&command_line_).Create()); |
| 250 Init(pref_service_.get()); | 322 Init(pref_service_.get(), NULL); |
| 251 } | 323 } |
| 252 | 324 |
| 253 private: | 325 private: |
| 254 CommandLine command_line_; | 326 CommandLine command_line_; |
| 255 scoped_ptr<PrefService> pref_service_; | 327 scoped_ptr<PrefService> pref_service_; |
| 256 }; | 328 }; |
| 257 | 329 |
| 258 TEST_P(PrefProxyConfigServiceCommandLineTest, CommandLine) { | 330 TEST_P(PrefProxyConfigServiceCommandLineTest, CommandLine) { |
| 259 net::ProxyConfig config; | 331 net::ProxyConfig config; |
| 260 proxy_config_service_->GetLatestProxyConfig(&config); | 332 proxy_config_service_->GetLatestProxyConfig(&config); |
| (...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 378 net::ProxyRulesExpectation::Empty(), | 450 net::ProxyRulesExpectation::Empty(), |
| 379 }, | 451 }, |
| 380 }; | 452 }; |
| 381 | 453 |
| 382 INSTANTIATE_TEST_CASE_P( | 454 INSTANTIATE_TEST_CASE_P( |
| 383 PrefProxyConfigServiceCommandLineTestInstance, | 455 PrefProxyConfigServiceCommandLineTestInstance, |
| 384 PrefProxyConfigServiceCommandLineTest, | 456 PrefProxyConfigServiceCommandLineTest, |
| 385 testing::ValuesIn(kCommandLineTestParams)); | 457 testing::ValuesIn(kCommandLineTestParams)); |
| 386 | 458 |
| 387 } // namespace | 459 } // namespace |
| OLD | NEW |