pref_proxy_config_tracker_impl_unittest.cc revision 90dce4d38c5ff5333bea97d859d4e484e27edf0c
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 3// found in the LICENSE file. 4 5#include "chrome/browser/net/pref_proxy_config_tracker_impl.h" 6 7#include "base/command_line.h" 8#include "base/files/file_path.h" 9#include "base/message_loop.h" 10#include "base/prefs/pref_registry_simple.h" 11#include "base/prefs/testing_pref_service.h" 12#include "chrome/browser/net/chrome_url_request_context.h" 13#include "chrome/browser/prefs/pref_service_mock_builder.h" 14#include "chrome/browser/prefs/proxy_config_dictionary.h" 15#include "chrome/common/chrome_switches.h" 16#include "chrome/common/pref_names.h" 17#include "content/public/test/test_browser_thread.h" 18#include "net/proxy/proxy_config_service_common_unittest.h" 19#include "testing/gmock/include/gmock/gmock.h" 20#include "testing/gtest/include/gtest/gtest.h" 21 22using content::BrowserThread; 23using testing::_; 24using testing::Mock; 25 26namespace { 27 28const char kFixedPacUrl[] = "http://chromium.org/fixed_pac_url"; 29 30// Testing proxy config service that allows us to fire notifications at will. 31class TestProxyConfigService : public net::ProxyConfigService { 32 public: 33 TestProxyConfigService(const net::ProxyConfig& config, 34 ConfigAvailability availability) 35 : config_(config), 36 availability_(availability) {} 37 38 void SetProxyConfig(const net::ProxyConfig config, 39 ConfigAvailability availability) { 40 config_ = config; 41 availability_ = availability; 42 FOR_EACH_OBSERVER(net::ProxyConfigService::Observer, observers_, 43 OnProxyConfigChanged(config, availability)); 44 } 45 46 private: 47 virtual void AddObserver( 48 net::ProxyConfigService::Observer* observer) OVERRIDE { 49 observers_.AddObserver(observer); 50 } 51 52 virtual void RemoveObserver( 53 net::ProxyConfigService::Observer* observer) OVERRIDE { 54 observers_.RemoveObserver(observer); 55 } 56 57 virtual net::ProxyConfigService::ConfigAvailability GetLatestProxyConfig( 58 net::ProxyConfig* config) OVERRIDE { 59 *config = config_; 60 return availability_; 61 } 62 63 net::ProxyConfig config_; 64 ConfigAvailability availability_; 65 ObserverList<net::ProxyConfigService::Observer, true> observers_; 66}; 67 68// A mock observer for capturing callbacks. 69class MockObserver : public net::ProxyConfigService::Observer { 70 public: 71 MOCK_METHOD2(OnProxyConfigChanged, 72 void(const net::ProxyConfig&, 73 net::ProxyConfigService::ConfigAvailability)); 74}; 75 76template<typename TESTBASE> 77class PrefProxyConfigTrackerImplTestBase : public TESTBASE { 78 protected: 79 PrefProxyConfigTrackerImplTestBase() 80 : ui_thread_(BrowserThread::UI, &loop_), 81 io_thread_(BrowserThread::IO, &loop_) {} 82 83 virtual void Init(PrefService* pref_service, PrefRegistrySimple* registry) { 84 ASSERT_TRUE(pref_service); 85 PrefProxyConfigTrackerImpl::RegisterPrefs(registry); 86 fixed_config_.set_pac_url(GURL(kFixedPacUrl)); 87 delegate_service_ = 88 new TestProxyConfigService(fixed_config_, 89 net::ProxyConfigService::CONFIG_VALID); 90 proxy_config_service_.reset( 91 new ChromeProxyConfigService(delegate_service_)); 92 proxy_config_tracker_.reset(new PrefProxyConfigTrackerImpl(pref_service)); 93 proxy_config_tracker_->SetChromeProxyConfigService( 94 proxy_config_service_.get()); 95 // SetChromeProxyConfigService triggers update of initial prefs proxy 96 // config by tracker to chrome proxy config service, so flush all pending 97 // tasks so that tests start fresh. 98 loop_.RunUntilIdle(); 99 } 100 101 virtual void TearDown() { 102 proxy_config_tracker_->DetachFromPrefService(); 103 loop_.RunUntilIdle(); 104 proxy_config_tracker_.reset(); 105 proxy_config_service_.reset(); 106 } 107 108 base::MessageLoop loop_; 109 TestProxyConfigService* delegate_service_; // weak 110 scoped_ptr<ChromeProxyConfigService> proxy_config_service_; 111 net::ProxyConfig fixed_config_; 112 113 private: 114 scoped_ptr<PrefProxyConfigTrackerImpl> proxy_config_tracker_; 115 content::TestBrowserThread ui_thread_; 116 content::TestBrowserThread io_thread_; 117}; 118 119class PrefProxyConfigTrackerImplTest 120 : public PrefProxyConfigTrackerImplTestBase<testing::Test> { 121 protected: 122 virtual void SetUp() { 123 pref_service_.reset(new TestingPrefServiceSimple()); 124 Init(pref_service_.get(), pref_service_->registry()); 125 } 126 127 scoped_ptr<TestingPrefServiceSimple> pref_service_; 128}; 129 130TEST_F(PrefProxyConfigTrackerImplTest, BaseConfiguration) { 131 net::ProxyConfig actual_config; 132 EXPECT_EQ(net::ProxyConfigService::CONFIG_VALID, 133 proxy_config_service_->GetLatestProxyConfig(&actual_config)); 134 EXPECT_EQ(GURL(kFixedPacUrl), actual_config.pac_url()); 135} 136 137TEST_F(PrefProxyConfigTrackerImplTest, DynamicPrefOverrides) { 138 pref_service_->SetManagedPref(prefs::kProxy, 139 ProxyConfigDictionary::CreateFixedServers( 140 "http://example.com:3128", std::string())); 141 loop_.RunUntilIdle(); 142 143 net::ProxyConfig actual_config; 144 EXPECT_EQ(net::ProxyConfigService::CONFIG_VALID, 145 proxy_config_service_->GetLatestProxyConfig(&actual_config)); 146 EXPECT_FALSE(actual_config.auto_detect()); 147 EXPECT_EQ(net::ProxyConfig::ProxyRules::TYPE_SINGLE_PROXY, 148 actual_config.proxy_rules().type); 149 EXPECT_EQ(actual_config.proxy_rules().single_proxies.Get(), 150 net::ProxyServer::FromURI("http://example.com:3128", 151 net::ProxyServer::SCHEME_HTTP)); 152 153 pref_service_->SetManagedPref(prefs::kProxy, 154 ProxyConfigDictionary::CreateAutoDetect()); 155 loop_.RunUntilIdle(); 156 157 EXPECT_EQ(net::ProxyConfigService::CONFIG_VALID, 158 proxy_config_service_->GetLatestProxyConfig(&actual_config)); 159 EXPECT_TRUE(actual_config.auto_detect()); 160} 161 162// Compares proxy configurations, but allows different identifiers. 163MATCHER_P(ProxyConfigMatches, config, "") { 164 net::ProxyConfig reference(config); 165 reference.set_id(arg.id()); 166 return reference.Equals(arg); 167} 168 169TEST_F(PrefProxyConfigTrackerImplTest, Observers) { 170 const net::ProxyConfigService::ConfigAvailability CONFIG_VALID = 171 net::ProxyConfigService::CONFIG_VALID; 172 MockObserver observer; 173 proxy_config_service_->AddObserver(&observer); 174 175 // Firing the observers in the delegate should trigger a notification. 176 net::ProxyConfig config2; 177 config2.set_auto_detect(true); 178 EXPECT_CALL(observer, OnProxyConfigChanged(ProxyConfigMatches(config2), 179 CONFIG_VALID)).Times(1); 180 delegate_service_->SetProxyConfig(config2, CONFIG_VALID); 181 loop_.RunUntilIdle(); 182 Mock::VerifyAndClearExpectations(&observer); 183 184 // Override configuration, this should trigger a notification. 185 net::ProxyConfig pref_config; 186 pref_config.set_pac_url(GURL(kFixedPacUrl)); 187 188 EXPECT_CALL(observer, OnProxyConfigChanged(ProxyConfigMatches(pref_config), 189 CONFIG_VALID)).Times(1); 190 pref_service_->SetManagedPref( 191 prefs::kProxy, 192 ProxyConfigDictionary::CreatePacScript(kFixedPacUrl, false)); 193 loop_.RunUntilIdle(); 194 Mock::VerifyAndClearExpectations(&observer); 195 196 // Since there are pref overrides, delegate changes should be ignored. 197 net::ProxyConfig config3; 198 config3.proxy_rules().ParseFromString("http=config3:80"); 199 EXPECT_CALL(observer, OnProxyConfigChanged(_, _)).Times(0); 200 fixed_config_.set_auto_detect(true); 201 delegate_service_->SetProxyConfig(config3, CONFIG_VALID); 202 loop_.RunUntilIdle(); 203 Mock::VerifyAndClearExpectations(&observer); 204 205 // Clear the override should switch back to the fixed configuration. 206 EXPECT_CALL(observer, OnProxyConfigChanged(ProxyConfigMatches(config3), 207 CONFIG_VALID)).Times(1); 208 pref_service_->RemoveManagedPref(prefs::kProxy); 209 loop_.RunUntilIdle(); 210 Mock::VerifyAndClearExpectations(&observer); 211 212 // Delegate service notifications should show up again. 213 net::ProxyConfig config4; 214 config4.proxy_rules().ParseFromString("socks:config4"); 215 EXPECT_CALL(observer, OnProxyConfigChanged(ProxyConfigMatches(config4), 216 CONFIG_VALID)).Times(1); 217 delegate_service_->SetProxyConfig(config4, CONFIG_VALID); 218 loop_.RunUntilIdle(); 219 Mock::VerifyAndClearExpectations(&observer); 220 221 proxy_config_service_->RemoveObserver(&observer); 222} 223 224TEST_F(PrefProxyConfigTrackerImplTest, Fallback) { 225 const net::ProxyConfigService::ConfigAvailability CONFIG_VALID = 226 net::ProxyConfigService::CONFIG_VALID; 227 MockObserver observer; 228 net::ProxyConfig actual_config; 229 delegate_service_->SetProxyConfig(net::ProxyConfig::CreateDirect(), 230 net::ProxyConfigService::CONFIG_UNSET); 231 proxy_config_service_->AddObserver(&observer); 232 233 // Prepare test data. 234 net::ProxyConfig recommended_config = net::ProxyConfig::CreateAutoDetect(); 235 net::ProxyConfig user_config = 236 net::ProxyConfig::CreateFromCustomPacURL(GURL(kFixedPacUrl)); 237 238 // Set a recommended pref. 239 EXPECT_CALL(observer, 240 OnProxyConfigChanged(ProxyConfigMatches(recommended_config), 241 CONFIG_VALID)).Times(1); 242 pref_service_->SetRecommendedPref( 243 prefs::kProxy, 244 ProxyConfigDictionary::CreateAutoDetect()); 245 loop_.RunUntilIdle(); 246 Mock::VerifyAndClearExpectations(&observer); 247 EXPECT_EQ(CONFIG_VALID, 248 proxy_config_service_->GetLatestProxyConfig(&actual_config)); 249 EXPECT_TRUE(actual_config.Equals(recommended_config)); 250 251 // Override in user prefs. 252 EXPECT_CALL(observer, 253 OnProxyConfigChanged(ProxyConfigMatches(user_config), 254 CONFIG_VALID)).Times(1); 255 pref_service_->SetManagedPref( 256 prefs::kProxy, 257 ProxyConfigDictionary::CreatePacScript(kFixedPacUrl, false)); 258 loop_.RunUntilIdle(); 259 Mock::VerifyAndClearExpectations(&observer); 260 EXPECT_EQ(CONFIG_VALID, 261 proxy_config_service_->GetLatestProxyConfig(&actual_config)); 262 EXPECT_TRUE(actual_config.Equals(user_config)); 263 264 // Go back to recommended pref. 265 EXPECT_CALL(observer, 266 OnProxyConfigChanged(ProxyConfigMatches(recommended_config), 267 CONFIG_VALID)).Times(1); 268 pref_service_->RemoveManagedPref(prefs::kProxy); 269 loop_.RunUntilIdle(); 270 Mock::VerifyAndClearExpectations(&observer); 271 EXPECT_EQ(CONFIG_VALID, 272 proxy_config_service_->GetLatestProxyConfig(&actual_config)); 273 EXPECT_TRUE(actual_config.Equals(recommended_config)); 274 275 proxy_config_service_->RemoveObserver(&observer); 276} 277 278TEST_F(PrefProxyConfigTrackerImplTest, ExplicitSystemSettings) { 279 pref_service_->SetRecommendedPref( 280 prefs::kProxy, 281 ProxyConfigDictionary::CreateAutoDetect()); 282 pref_service_->SetUserPref( 283 prefs::kProxy, 284 ProxyConfigDictionary::CreateSystem()); 285 loop_.RunUntilIdle(); 286 287 // Test if we actually use the system setting, which is |kFixedPacUrl|. 288 net::ProxyConfig actual_config; 289 EXPECT_EQ(net::ProxyConfigService::CONFIG_VALID, 290 proxy_config_service_->GetLatestProxyConfig(&actual_config)); 291 EXPECT_EQ(GURL(kFixedPacUrl), actual_config.pac_url()); 292} 293 294// Test parameter object for testing command line proxy configuration. 295struct CommandLineTestParams { 296 // Explicit assignment operator, so testing::TestWithParam works with MSVC. 297 CommandLineTestParams& operator=(const CommandLineTestParams& other) { 298 description = other.description; 299 for (unsigned int i = 0; i < arraysize(switches); i++) 300 switches[i] = other.switches[i]; 301 is_null = other.is_null; 302 auto_detect = other.auto_detect; 303 pac_url = other.pac_url; 304 proxy_rules = other.proxy_rules; 305 return *this; 306 } 307 308 // Short description to identify the test. 309 const char* description; 310 311 // The command line to build a ProxyConfig from. 312 struct SwitchValue { 313 const char* name; 314 const char* value; 315 } switches[2]; 316 317 // Expected outputs (fields of the ProxyConfig). 318 bool is_null; 319 bool auto_detect; 320 GURL pac_url; 321 net::ProxyRulesExpectation proxy_rules; 322}; 323 324void PrintTo(const CommandLineTestParams& params, std::ostream* os) { 325 *os << params.description; 326} 327 328class PrefProxyConfigTrackerImplCommandLineTest 329 : public PrefProxyConfigTrackerImplTestBase< 330 testing::TestWithParam<CommandLineTestParams> > { 331 protected: 332 PrefProxyConfigTrackerImplCommandLineTest() 333 : command_line_(CommandLine::NO_PROGRAM) {} 334 335 virtual void SetUp() { 336 for (size_t i = 0; i < arraysize(GetParam().switches); i++) { 337 const char* name = GetParam().switches[i].name; 338 const char* value = GetParam().switches[i].value; 339 if (name && value) 340 command_line_.AppendSwitchASCII(name, value); 341 else if (name) 342 command_line_.AppendSwitch(name); 343 } 344 scoped_refptr<PrefRegistrySimple> registry = new PrefRegistrySimple; 345 pref_service_.reset( 346 PrefServiceMockBuilder().WithCommandLine( 347 &command_line_).Create(registry)); 348 Init(pref_service_.get(), registry); 349 } 350 351 private: 352 CommandLine command_line_; 353 scoped_ptr<PrefService> pref_service_; 354}; 355 356TEST_P(PrefProxyConfigTrackerImplCommandLineTest, CommandLine) { 357 net::ProxyConfig config; 358 EXPECT_EQ(net::ProxyConfigService::CONFIG_VALID, 359 proxy_config_service_->GetLatestProxyConfig(&config)); 360 361 if (GetParam().is_null) { 362 EXPECT_EQ(GURL(kFixedPacUrl), config.pac_url()); 363 } else { 364 EXPECT_NE(GURL(kFixedPacUrl), config.pac_url()); 365 EXPECT_EQ(GetParam().auto_detect, config.auto_detect()); 366 EXPECT_EQ(GetParam().pac_url, config.pac_url()); 367 EXPECT_TRUE(GetParam().proxy_rules.Matches(config.proxy_rules())); 368 } 369} 370 371static const CommandLineTestParams kCommandLineTestParams[] = { 372 { 373 "Empty command line", 374 // Input 375 { }, 376 // Expected result 377 true, // is_null 378 false, // auto_detect 379 GURL(), // pac_url 380 net::ProxyRulesExpectation::Empty(), 381 }, 382 { 383 "No proxy", 384 // Input 385 { 386 { switches::kNoProxyServer, NULL }, 387 }, 388 // Expected result 389 false, // is_null 390 false, // auto_detect 391 GURL(), // pac_url 392 net::ProxyRulesExpectation::Empty(), 393 }, 394 { 395 "No proxy with extra parameters.", 396 // Input 397 { 398 { switches::kNoProxyServer, NULL }, 399 { switches::kProxyServer, "http://proxy:8888" }, 400 }, 401 // Expected result 402 false, // is_null 403 false, // auto_detect 404 GURL(), // pac_url 405 net::ProxyRulesExpectation::Empty(), 406 }, 407 { 408 "Single proxy.", 409 // Input 410 { 411 { switches::kProxyServer, "http://proxy:8888" }, 412 }, 413 // Expected result 414 false, // is_null 415 false, // auto_detect 416 GURL(), // pac_url 417 net::ProxyRulesExpectation::Single( 418 "proxy:8888", // single proxy 419 ""), // bypass rules 420 }, 421 { 422 "Per scheme proxy.", 423 // Input 424 { 425 { switches::kProxyServer, "http=httpproxy:8888;ftp=ftpproxy:8889" }, 426 }, 427 // Expected result 428 false, // is_null 429 false, // auto_detect 430 GURL(), // pac_url 431 net::ProxyRulesExpectation::PerScheme( 432 "httpproxy:8888", // http 433 "", // https 434 "ftpproxy:8889", // ftp 435 ""), // bypass rules 436 }, 437 { 438 "Per scheme proxy with bypass URLs.", 439 // Input 440 { 441 { switches::kProxyServer, "http=httpproxy:8888;ftp=ftpproxy:8889" }, 442 { switches::kProxyBypassList, 443 ".google.com, foo.com:99, 1.2.3.4:22, 127.0.0.1/8" }, 444 }, 445 // Expected result 446 false, // is_null 447 false, // auto_detect 448 GURL(), // pac_url 449 net::ProxyRulesExpectation::PerScheme( 450 "httpproxy:8888", // http 451 "", // https 452 "ftpproxy:8889", // ftp 453 "*.google.com,foo.com:99,1.2.3.4:22,127.0.0.1/8"), 454 }, 455 { 456 "Pac URL", 457 // Input 458 { 459 { switches::kProxyPacUrl, "http://wpad/wpad.dat" }, 460 }, 461 // Expected result 462 false, // is_null 463 false, // auto_detect 464 GURL("http://wpad/wpad.dat"), // pac_url 465 net::ProxyRulesExpectation::Empty(), 466 }, 467 { 468 "Autodetect", 469 // Input 470 { 471 { switches::kProxyAutoDetect, NULL }, 472 }, 473 // Expected result 474 false, // is_null 475 true, // auto_detect 476 GURL(), // pac_url 477 net::ProxyRulesExpectation::Empty(), 478 }, 479}; 480 481INSTANTIATE_TEST_CASE_P( 482 PrefProxyConfigTrackerImplCommandLineTestInstance, 483 PrefProxyConfigTrackerImplCommandLineTest, 484 testing::ValuesIn(kCommandLineTestParams)); 485 486} // namespace 487