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