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.
5 #include "components/proxy_config/pref_proxy_config_tracker_impl.h"
9 #include "base/command_line.h"
10 #include "base/files/file_path.h"
11 #include "base/message_loop/message_loop.h"
12 #include "base/prefs/pref_registry_simple.h"
13 #include "base/prefs/testing_pref_service.h"
14 #include "base/test/histogram_tester.h"
15 #include "base/thread_task_runner_handle.h"
16 #include "chrome/browser/prefs/pref_service_mock_factory.h"
17 #include "chrome/common/chrome_switches.h"
18 #include "components/proxy_config/proxy_config_dictionary.h"
19 #include "components/proxy_config/proxy_config_pref_names.h"
20 #include "net/proxy/proxy_config_service_common_unittest.h"
21 #include "net/proxy/proxy_info.h"
22 #include "net/proxy/proxy_list.h"
23 #include "testing/gmock/include/gmock/gmock.h"
24 #include "testing/gtest/include/gtest/gtest.h"
31 const char kFixedPacUrl
[] = "http://chromium.org/fixed_pac_url";
33 // Testing proxy config service that allows us to fire notifications at will.
34 class TestProxyConfigService
: public net::ProxyConfigService
{
36 TestProxyConfigService(const net::ProxyConfig
& config
,
37 ConfigAvailability availability
)
39 availability_(availability
) {}
41 void SetProxyConfig(const net::ProxyConfig config
,
42 ConfigAvailability availability
) {
44 availability_
= availability
;
45 FOR_EACH_OBSERVER(net::ProxyConfigService::Observer
, observers_
,
46 OnProxyConfigChanged(config
, availability
));
50 void AddObserver(net::ProxyConfigService::Observer
* observer
) override
{
51 observers_
.AddObserver(observer
);
54 void RemoveObserver(net::ProxyConfigService::Observer
* observer
) override
{
55 observers_
.RemoveObserver(observer
);
58 net::ProxyConfigService::ConfigAvailability
GetLatestProxyConfig(
59 net::ProxyConfig
* config
) override
{
64 net::ProxyConfig config_
;
65 ConfigAvailability availability_
;
66 base::ObserverList
<net::ProxyConfigService::Observer
, true> observers_
;
69 // A mock observer for capturing callbacks.
70 class MockObserver
: public net::ProxyConfigService::Observer
{
72 MOCK_METHOD2(OnProxyConfigChanged
,
73 void(const net::ProxyConfig
&,
74 net::ProxyConfigService::ConfigAvailability
));
77 template<typename TESTBASE
>
78 class PrefProxyConfigTrackerImplTestBase
: public TESTBASE
{
80 PrefProxyConfigTrackerImplTestBase() {}
82 virtual void Init(PrefService
* pref_service
, PrefRegistrySimple
* registry
) {
83 ASSERT_TRUE(pref_service
);
84 PrefProxyConfigTrackerImpl::RegisterPrefs(registry
);
85 fixed_config_
.set_pac_url(GURL(kFixedPacUrl
));
87 new TestProxyConfigService(fixed_config_
,
88 net::ProxyConfigService::CONFIG_VALID
);
89 proxy_config_tracker_
.reset(new PrefProxyConfigTrackerImpl(
90 pref_service
, base::ThreadTaskRunnerHandle::Get()));
91 proxy_config_service_
=
92 proxy_config_tracker_
->CreateTrackingProxyConfigService(
93 scoped_ptr
<net::ProxyConfigService
>(delegate_service_
));
94 // SetProxyConfigServiceImpl 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.
100 virtual void TearDown() {
101 proxy_config_tracker_
->DetachFromPrefService();
102 loop_
.RunUntilIdle();
103 proxy_config_tracker_
.reset();
104 proxy_config_service_
.reset();
107 base::MessageLoop loop_
;
108 TestProxyConfigService
* delegate_service_
; // weak
109 scoped_ptr
<net::ProxyConfigService
> proxy_config_service_
;
110 net::ProxyConfig fixed_config_
;
113 scoped_ptr
<PrefProxyConfigTrackerImpl
> proxy_config_tracker_
;
116 class PrefProxyConfigTrackerImplTest
117 : public PrefProxyConfigTrackerImplTestBase
<testing::Test
> {
119 void SetUp() override
{
120 pref_service_
.reset(new TestingPrefServiceSimple());
121 Init(pref_service_
.get(), pref_service_
->registry());
124 scoped_ptr
<TestingPrefServiceSimple
> pref_service_
;
127 TEST_F(PrefProxyConfigTrackerImplTest
, BaseConfiguration
) {
128 net::ProxyConfig actual_config
;
129 EXPECT_EQ(net::ProxyConfigService::CONFIG_VALID
,
130 proxy_config_service_
->GetLatestProxyConfig(&actual_config
));
131 EXPECT_EQ(GURL(kFixedPacUrl
), actual_config
.pac_url());
134 TEST_F(PrefProxyConfigTrackerImplTest
, DynamicPrefOverrides
) {
135 pref_service_
->SetManagedPref(proxy_config::prefs::kProxy
,
136 ProxyConfigDictionary::CreateFixedServers(
137 "http://example.com:3128", std::string()));
138 loop_
.RunUntilIdle();
140 net::ProxyConfig actual_config
;
141 EXPECT_EQ(net::ProxyConfigService::CONFIG_VALID
,
142 proxy_config_service_
->GetLatestProxyConfig(&actual_config
));
143 EXPECT_FALSE(actual_config
.auto_detect());
144 EXPECT_EQ(net::ProxyConfig::ProxyRules::TYPE_SINGLE_PROXY
,
145 actual_config
.proxy_rules().type
);
146 EXPECT_EQ(actual_config
.proxy_rules().single_proxies
.Get(),
147 net::ProxyServer::FromURI("http://example.com:3128",
148 net::ProxyServer::SCHEME_HTTP
));
150 pref_service_
->SetManagedPref(proxy_config::prefs::kProxy
,
151 ProxyConfigDictionary::CreateAutoDetect());
152 loop_
.RunUntilIdle();
154 EXPECT_EQ(net::ProxyConfigService::CONFIG_VALID
,
155 proxy_config_service_
->GetLatestProxyConfig(&actual_config
));
156 EXPECT_TRUE(actual_config
.auto_detect());
159 // Compares proxy configurations, but allows different identifiers.
160 MATCHER_P(ProxyConfigMatches
, config
, "") {
161 net::ProxyConfig
reference(config
);
162 reference
.set_id(arg
.id());
163 return reference
.Equals(arg
);
166 TEST_F(PrefProxyConfigTrackerImplTest
, Observers
) {
167 const net::ProxyConfigService::ConfigAvailability CONFIG_VALID
=
168 net::ProxyConfigService::CONFIG_VALID
;
169 MockObserver observer
;
170 proxy_config_service_
->AddObserver(&observer
);
172 // Firing the observers in the delegate should trigger a notification.
173 net::ProxyConfig config2
;
174 config2
.set_auto_detect(true);
175 EXPECT_CALL(observer
, OnProxyConfigChanged(ProxyConfigMatches(config2
),
176 CONFIG_VALID
)).Times(1);
177 delegate_service_
->SetProxyConfig(config2
, CONFIG_VALID
);
178 loop_
.RunUntilIdle();
179 Mock::VerifyAndClearExpectations(&observer
);
181 // Override configuration, this should trigger a notification.
182 net::ProxyConfig pref_config
;
183 pref_config
.set_pac_url(GURL(kFixedPacUrl
));
185 EXPECT_CALL(observer
, OnProxyConfigChanged(ProxyConfigMatches(pref_config
),
186 CONFIG_VALID
)).Times(1);
187 pref_service_
->SetManagedPref(
188 proxy_config::prefs::kProxy
,
189 ProxyConfigDictionary::CreatePacScript(kFixedPacUrl
, false));
190 loop_
.RunUntilIdle();
191 Mock::VerifyAndClearExpectations(&observer
);
193 // Since there are pref overrides, delegate changes should be ignored.
194 net::ProxyConfig config3
;
195 config3
.proxy_rules().ParseFromString("http=config3:80");
196 EXPECT_CALL(observer
, OnProxyConfigChanged(_
, _
)).Times(0);
197 fixed_config_
.set_auto_detect(true);
198 delegate_service_
->SetProxyConfig(config3
, CONFIG_VALID
);
199 loop_
.RunUntilIdle();
200 Mock::VerifyAndClearExpectations(&observer
);
202 // Clear the override should switch back to the fixed configuration.
203 EXPECT_CALL(observer
, OnProxyConfigChanged(ProxyConfigMatches(config3
),
204 CONFIG_VALID
)).Times(1);
205 pref_service_
->RemoveManagedPref(proxy_config::prefs::kProxy
);
206 loop_
.RunUntilIdle();
207 Mock::VerifyAndClearExpectations(&observer
);
209 // Delegate service notifications should show up again.
210 net::ProxyConfig config4
;
211 config4
.proxy_rules().ParseFromString("socks:config4");
212 EXPECT_CALL(observer
, OnProxyConfigChanged(ProxyConfigMatches(config4
),
213 CONFIG_VALID
)).Times(1);
214 delegate_service_
->SetProxyConfig(config4
, CONFIG_VALID
);
215 loop_
.RunUntilIdle();
216 Mock::VerifyAndClearExpectations(&observer
);
218 proxy_config_service_
->RemoveObserver(&observer
);
221 TEST_F(PrefProxyConfigTrackerImplTest
, Fallback
) {
222 const net::ProxyConfigService::ConfigAvailability CONFIG_VALID
=
223 net::ProxyConfigService::CONFIG_VALID
;
224 MockObserver observer
;
225 net::ProxyConfig actual_config
;
226 delegate_service_
->SetProxyConfig(net::ProxyConfig::CreateDirect(),
227 net::ProxyConfigService::CONFIG_UNSET
);
228 proxy_config_service_
->AddObserver(&observer
);
230 // Prepare test data.
231 net::ProxyConfig recommended_config
= net::ProxyConfig::CreateAutoDetect();
232 net::ProxyConfig user_config
=
233 net::ProxyConfig::CreateFromCustomPacURL(GURL(kFixedPacUrl
));
235 // Set a recommended pref.
236 EXPECT_CALL(observer
,
237 OnProxyConfigChanged(ProxyConfigMatches(recommended_config
),
238 CONFIG_VALID
)).Times(1);
239 pref_service_
->SetRecommendedPref(proxy_config::prefs::kProxy
,
240 ProxyConfigDictionary::CreateAutoDetect());
241 loop_
.RunUntilIdle();
242 Mock::VerifyAndClearExpectations(&observer
);
243 EXPECT_EQ(CONFIG_VALID
,
244 proxy_config_service_
->GetLatestProxyConfig(&actual_config
));
245 EXPECT_TRUE(actual_config
.Equals(recommended_config
));
247 // Override in user prefs.
248 EXPECT_CALL(observer
,
249 OnProxyConfigChanged(ProxyConfigMatches(user_config
),
250 CONFIG_VALID
)).Times(1);
251 pref_service_
->SetManagedPref(
252 proxy_config::prefs::kProxy
,
253 ProxyConfigDictionary::CreatePacScript(kFixedPacUrl
, false));
254 loop_
.RunUntilIdle();
255 Mock::VerifyAndClearExpectations(&observer
);
256 EXPECT_EQ(CONFIG_VALID
,
257 proxy_config_service_
->GetLatestProxyConfig(&actual_config
));
258 EXPECT_TRUE(actual_config
.Equals(user_config
));
260 // Go back to recommended pref.
261 EXPECT_CALL(observer
,
262 OnProxyConfigChanged(ProxyConfigMatches(recommended_config
),
263 CONFIG_VALID
)).Times(1);
264 pref_service_
->RemoveManagedPref(proxy_config::prefs::kProxy
);
265 loop_
.RunUntilIdle();
266 Mock::VerifyAndClearExpectations(&observer
);
267 EXPECT_EQ(CONFIG_VALID
,
268 proxy_config_service_
->GetLatestProxyConfig(&actual_config
));
269 EXPECT_TRUE(actual_config
.Equals(recommended_config
));
271 proxy_config_service_
->RemoveObserver(&observer
);
274 TEST_F(PrefProxyConfigTrackerImplTest
, ExplicitSystemSettings
) {
275 pref_service_
->SetRecommendedPref(proxy_config::prefs::kProxy
,
276 ProxyConfigDictionary::CreateAutoDetect());
277 pref_service_
->SetUserPref(proxy_config::prefs::kProxy
,
278 ProxyConfigDictionary::CreateSystem());
279 loop_
.RunUntilIdle();
281 // Test if we actually use the system setting, which is |kFixedPacUrl|.
282 net::ProxyConfig actual_config
;
283 EXPECT_EQ(net::ProxyConfigService::CONFIG_VALID
,
284 proxy_config_service_
->GetLatestProxyConfig(&actual_config
));
285 EXPECT_EQ(GURL(kFixedPacUrl
), actual_config
.pac_url());
288 void CheckResolvedProxyMatches(net::ProxyConfig
* config
,
290 const std::string
& result_string
) {
291 net::ProxyInfo expected_result
;
292 expected_result
.UseNamedProxy(result_string
);
294 net::ProxyInfo result
;
295 config
->proxy_rules().Apply(url
, &result
);
297 EXPECT_TRUE(expected_result
.proxy_list().Equals(result
.proxy_list()))
298 << "expected: " << expected_result
.proxy_list().ToPacString()
299 << "\nactual: " << result
.proxy_list().ToPacString();
302 TEST_F(PrefProxyConfigTrackerImplTest
, ExcludeGooglezipDataReductionProxies
) {
303 const std::string kDataReductionProxies
=
304 "https://proxy.googlezip.net:443,compress.googlezip.net,"
305 "https://proxy-dev.googlezip.net:443,proxy-dev.googlezip.net,"
306 "quic://proxy.googlezip.net";
307 const int kNumDataReductionProxies
= 5;
310 std::string initial_proxy_rules
;
311 const char* http_proxy_info
;
312 const char* https_proxy_info
;
313 const char* ftp_proxy_info
;
314 int expected_num_removed_proxies
;
316 {"http=foopyhttp," + kDataReductionProxies
+
317 ",direct://;https=foopyhttps," + kDataReductionProxies
+
318 ",direct://;ftp=foopyftp," + kDataReductionProxies
+ ",direct://",
319 "foopyhttp;direct://",
320 "foopyhttps;direct://",
321 "foopyftp;direct://",
322 kNumDataReductionProxies
* 3},
324 {"foopy," + kDataReductionProxies
+ ",direct://",
328 kNumDataReductionProxies
},
330 {"http=" + kDataReductionProxies
+ ";https=" + kDataReductionProxies
+
331 ";ftp=" + kDataReductionProxies
,
335 kNumDataReductionProxies
* 3},
337 {"http=" + kDataReductionProxies
+ ",foopy,direct://",
341 kNumDataReductionProxies
},
356 // Test setting the proxy from a user pref.
357 for (const auto& test
: test_cases
) {
358 base::HistogramTester histogram_tester
;
359 pref_service_
->SetUserPref(proxy_config::prefs::kProxy
,
360 ProxyConfigDictionary::CreateFixedServers(
361 test
.initial_proxy_rules
, std::string()));
362 loop_
.RunUntilIdle();
364 net::ProxyConfig config
;
365 EXPECT_EQ(net::ProxyConfigService::CONFIG_VALID
,
366 proxy_config_service_
->GetLatestProxyConfig(&config
));
367 histogram_tester
.ExpectUniqueSample(
368 "Net.PrefProxyConfig.GooglezipProxyRemovalCount",
369 test
.expected_num_removed_proxies
, 1);
371 CheckResolvedProxyMatches(&config
, GURL("http://google.com"),
372 test
.http_proxy_info
);
373 CheckResolvedProxyMatches(&config
, GURL("https://google.com"),
374 test
.https_proxy_info
);
375 CheckResolvedProxyMatches(&config
, GURL("ftp://google.com"),
376 test
.ftp_proxy_info
);
380 // Test parameter object for testing command line proxy configuration.
381 struct CommandLineTestParams
{
382 // Explicit assignment operator, so testing::TestWithParam works with MSVC.
383 CommandLineTestParams
& operator=(const CommandLineTestParams
& other
) {
384 description
= other
.description
;
385 for (unsigned int i
= 0; i
< arraysize(switches
); i
++)
386 switches
[i
] = other
.switches
[i
];
387 is_null
= other
.is_null
;
388 auto_detect
= other
.auto_detect
;
389 pac_url
= other
.pac_url
;
390 proxy_rules
= other
.proxy_rules
;
394 // Short description to identify the test.
395 const char* description
;
397 // The command line to build a ProxyConfig from.
403 // Expected outputs (fields of the ProxyConfig).
407 net::ProxyRulesExpectation proxy_rules
;
410 void PrintTo(const CommandLineTestParams
& params
, std::ostream
* os
) {
411 *os
<< params
.description
;
414 class PrefProxyConfigTrackerImplCommandLineTest
415 : public PrefProxyConfigTrackerImplTestBase
<
416 testing::TestWithParam
<CommandLineTestParams
> > {
418 PrefProxyConfigTrackerImplCommandLineTest()
419 : command_line_(base::CommandLine::NO_PROGRAM
) {}
421 void SetUp() override
{
422 for (size_t i
= 0; i
< arraysize(GetParam().switches
); i
++) {
423 const char* name
= GetParam().switches
[i
].name
;
424 const char* value
= GetParam().switches
[i
].value
;
426 command_line_
.AppendSwitchASCII(name
, value
);
428 command_line_
.AppendSwitch(name
);
430 scoped_refptr
<PrefRegistrySimple
> registry
= new PrefRegistrySimple
;
431 PrefServiceMockFactory factory
;
432 factory
.SetCommandLine(&command_line_
);
433 pref_service_
= factory
.Create(registry
.get()).Pass();
434 Init(pref_service_
.get(), registry
.get());
438 base::CommandLine command_line_
;
439 scoped_ptr
<PrefService
> pref_service_
;
442 TEST_P(PrefProxyConfigTrackerImplCommandLineTest
, CommandLine
) {
443 net::ProxyConfig config
;
444 EXPECT_EQ(net::ProxyConfigService::CONFIG_VALID
,
445 proxy_config_service_
->GetLatestProxyConfig(&config
));
447 if (GetParam().is_null
) {
448 EXPECT_EQ(GURL(kFixedPacUrl
), config
.pac_url());
450 EXPECT_NE(GURL(kFixedPacUrl
), config
.pac_url());
451 EXPECT_EQ(GetParam().auto_detect
, config
.auto_detect());
452 EXPECT_EQ(GetParam().pac_url
, config
.pac_url());
453 EXPECT_TRUE(GetParam().proxy_rules
.Matches(config
.proxy_rules()));
457 static const CommandLineTestParams kCommandLineTestParams
[] = {
459 "Empty command line",
464 false, // auto_detect
466 net::ProxyRulesExpectation::Empty(),
472 { switches::kNoProxyServer
, NULL
},
476 false, // auto_detect
478 net::ProxyRulesExpectation::Empty(),
481 "No proxy with extra parameters.",
484 { switches::kNoProxyServer
, NULL
},
485 { switches::kProxyServer
, "http://proxy:8888" },
489 false, // auto_detect
491 net::ProxyRulesExpectation::Empty(),
497 { switches::kProxyServer
, "http://proxy:8888" },
501 false, // auto_detect
503 net::ProxyRulesExpectation::Single(
504 "proxy:8888", // single proxy
511 { switches::kProxyServer
, "http=httpproxy:8888;ftp=ftpproxy:8889" },
515 false, // auto_detect
517 net::ProxyRulesExpectation::PerScheme(
518 "httpproxy:8888", // http
520 "ftpproxy:8889", // ftp
524 "Per scheme proxy with bypass URLs.",
527 { switches::kProxyServer
, "http=httpproxy:8888;ftp=ftpproxy:8889" },
528 { switches::kProxyBypassList
,
529 ".google.com, foo.com:99, 1.2.3.4:22, 127.0.0.1/8" },
533 false, // auto_detect
535 net::ProxyRulesExpectation::PerScheme(
536 "httpproxy:8888", // http
538 "ftpproxy:8889", // ftp
539 "*.google.com,foo.com:99,1.2.3.4:22,127.0.0.1/8"),
545 { switches::kProxyPacUrl
, "http://wpad/wpad.dat" },
549 false, // auto_detect
550 GURL("http://wpad/wpad.dat"), // pac_url
551 net::ProxyRulesExpectation::Empty(),
557 { switches::kProxyAutoDetect
, NULL
},
563 net::ProxyRulesExpectation::Empty(),
567 INSTANTIATE_TEST_CASE_P(
568 PrefProxyConfigTrackerImplCommandLineTestInstance
,
569 PrefProxyConfigTrackerImplCommandLineTest
,
570 testing::ValuesIn(kCommandLineTestParams
));