1 // Copyright 2015 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 "chrome/browser/prefs/command_line_pref_store.h"
7 #include <gtest/gtest.h>
9 #include "base/command_line.h"
10 #include "base/memory/ref_counted.h"
11 #include "base/prefs/pref_registry_simple.h"
12 #include "base/prefs/pref_service.h"
13 #include "chrome/browser/prefs/command_line_pref_store.h"
14 #include "chrome/common/chrome_switches.h"
15 #include "components/proxy_config/pref_proxy_config_tracker_impl.h"
16 #include "components/syncable_prefs/pref_service_mock_factory.h"
17 #include "net/proxy/proxy_config_service_common_unittest.h"
22 // Test parameter object for testing command line proxy configuration.
23 struct CommandLineTestParams
{
24 // Short description to identify the test.
25 const char* description
;
27 // The command line to build a ProxyConfig from.
33 // Expected outputs (fields of the ProxyConfig).
37 net::ProxyRulesExpectation proxy_rules
;
40 void PrintTo(const CommandLineTestParams
& params
, std::ostream
* os
) {
41 *os
<< params
.description
;
44 static const CommandLineTestParams kCommandLineTestParams
[] = {
53 net::ProxyRulesExpectation::Empty(),
59 {switches::kNoProxyServer
, NULL
},
65 net::ProxyRulesExpectation::Empty(),
68 "No proxy with extra parameters.",
71 {switches::kNoProxyServer
, NULL
},
72 {switches::kProxyServer
, "http://proxy:8888"},
78 net::ProxyRulesExpectation::Empty(),
84 {switches::kProxyServer
, "http://proxy:8888"},
90 net::ProxyRulesExpectation::Single("proxy:8888", // single proxy
97 {switches::kProxyServer
, "http=httpproxy:8888;ftp=ftpproxy:8889"},
101 false, // auto_detect
103 net::ProxyRulesExpectation::PerScheme("httpproxy:8888", // http
105 "ftpproxy:8889", // ftp
109 "Per scheme proxy with bypass URLs.",
112 {switches::kProxyServer
, "http=httpproxy:8888;ftp=ftpproxy:8889"},
113 {switches::kProxyBypassList
,
114 ".google.com, foo.com:99, 1.2.3.4:22, 127.0.0.1/8"},
118 false, // auto_detect
120 net::ProxyRulesExpectation::PerScheme(
121 "httpproxy:8888", // http
123 "ftpproxy:8889", // ftp
124 "*.google.com,foo.com:99,1.2.3.4:22,127.0.0.1/8"),
130 {switches::kProxyPacUrl
, "http://wpad/wpad.dat"},
134 false, // auto_detect
135 GURL("http://wpad/wpad.dat"), // pac_url
136 net::ProxyRulesExpectation::Empty(),
142 {switches::kProxyAutoDetect
, NULL
},
148 net::ProxyRulesExpectation::Empty(),
154 class CommandLinePrefStoreProxyTest
155 : public testing::TestWithParam
<CommandLineTestParams
> {
157 CommandLinePrefStoreProxyTest()
158 : command_line_(base::CommandLine::NO_PROGRAM
) {}
160 net::ProxyConfig
* proxy_config() { return &proxy_config_
; }
162 void SetUp() override
{
163 for (size_t i
= 0; i
< arraysize(GetParam().switches
); i
++) {
164 const char* name
= GetParam().switches
[i
].name
;
165 const char* value
= GetParam().switches
[i
].value
;
167 command_line_
.AppendSwitchASCII(name
, value
);
169 command_line_
.AppendSwitch(name
);
171 scoped_refptr
<PrefRegistrySimple
> registry
= new PrefRegistrySimple
;
172 PrefProxyConfigTrackerImpl::RegisterPrefs(registry
.get());
173 syncable_prefs::PrefServiceMockFactory factory
;
174 factory
.set_command_line_prefs(new CommandLinePrefStore(&command_line_
));
175 pref_service_
= factory
.Create(registry
.get()).Pass();
176 PrefProxyConfigTrackerImpl::ReadPrefConfig(pref_service_
.get(),
181 base::CommandLine command_line_
;
182 scoped_ptr
<PrefService
> pref_service_
;
183 net::ProxyConfig proxy_config_
;
186 TEST_P(CommandLinePrefStoreProxyTest
, CommandLine
) {
187 EXPECT_EQ(GetParam().auto_detect
, proxy_config()->auto_detect());
188 EXPECT_EQ(GetParam().pac_url
, proxy_config()->pac_url());
189 EXPECT_TRUE(GetParam().proxy_rules
.Matches(proxy_config()->proxy_rules()));
192 INSTANTIATE_TEST_CASE_P(CommandLinePrefStoreProxyTestInstance
,
193 CommandLinePrefStoreProxyTest
,
194 testing::ValuesIn(kCommandLineTestParams
));