Refactor WebsiteSettings to operate on a SecurityInfo
[chromium-blink-merge.git] / chrome / browser / prefs / command_line_pref_store_proxy_unittest.cc
blob01aa882b61672caa536cd92a872760ad17684883
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"
18 #include "url/gurl.h"
20 namespace {
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.
28 struct SwitchValue {
29 const char* name;
30 const char* value;
31 } switches[2];
33 // Expected outputs (fields of the ProxyConfig).
34 bool is_null;
35 bool auto_detect;
36 GURL pac_url;
37 net::ProxyRulesExpectation proxy_rules;
40 void PrintTo(const CommandLineTestParams& params, std::ostream* os) {
41 *os << params.description;
44 static const CommandLineTestParams kCommandLineTestParams[] = {
46 "Empty command line",
47 // Input
48 {},
49 // Expected result
50 true, // is_null
51 false, // auto_detect
52 GURL(), // pac_url
53 net::ProxyRulesExpectation::Empty(),
56 "No proxy",
57 // Input
59 {switches::kNoProxyServer, NULL},
61 // Expected result
62 false, // is_null
63 false, // auto_detect
64 GURL(), // pac_url
65 net::ProxyRulesExpectation::Empty(),
68 "No proxy with extra parameters.",
69 // Input
71 {switches::kNoProxyServer, NULL},
72 {switches::kProxyServer, "http://proxy:8888"},
74 // Expected result
75 false, // is_null
76 false, // auto_detect
77 GURL(), // pac_url
78 net::ProxyRulesExpectation::Empty(),
81 "Single proxy.",
82 // Input
84 {switches::kProxyServer, "http://proxy:8888"},
86 // Expected result
87 false, // is_null
88 false, // auto_detect
89 GURL(), // pac_url
90 net::ProxyRulesExpectation::Single("proxy:8888", // single proxy
91 ""), // bypass rules
94 "Per scheme proxy.",
95 // Input
97 {switches::kProxyServer, "http=httpproxy:8888;ftp=ftpproxy:8889"},
99 // Expected result
100 false, // is_null
101 false, // auto_detect
102 GURL(), // pac_url
103 net::ProxyRulesExpectation::PerScheme("httpproxy:8888", // http
104 "", // https
105 "ftpproxy:8889", // ftp
106 ""), // bypass rules
109 "Per scheme proxy with bypass URLs.",
110 // Input
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"},
116 // Expected result
117 false, // is_null
118 false, // auto_detect
119 GURL(), // pac_url
120 net::ProxyRulesExpectation::PerScheme(
121 "httpproxy:8888", // http
122 "", // https
123 "ftpproxy:8889", // ftp
124 "*.google.com,foo.com:99,1.2.3.4:22,127.0.0.1/8"),
127 "Pac URL",
128 // Input
130 {switches::kProxyPacUrl, "http://wpad/wpad.dat"},
132 // Expected result
133 false, // is_null
134 false, // auto_detect
135 GURL("http://wpad/wpad.dat"), // pac_url
136 net::ProxyRulesExpectation::Empty(),
139 "Autodetect",
140 // Input
142 {switches::kProxyAutoDetect, NULL},
144 // Expected result
145 false, // is_null
146 true, // auto_detect
147 GURL(), // pac_url
148 net::ProxyRulesExpectation::Empty(),
152 } // namespace
154 class CommandLinePrefStoreProxyTest
155 : public testing::TestWithParam<CommandLineTestParams> {
156 protected:
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;
166 if (name && value)
167 command_line_.AppendSwitchASCII(name, value);
168 else if (name)
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(),
177 &proxy_config_);
180 private:
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));