OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "chrome/browser/prefs/command_line_pref_store.h" | |
6 | |
7 #include <gtest/gtest.h> | |
8 #include <stddef.h> | |
9 | |
10 #include "base/command_line.h" | |
11 #include "base/macros.h" | |
12 #include "base/memory/ref_counted.h" | |
13 #include "chrome/browser/prefs/command_line_pref_store.h" | |
14 #include "chrome/common/chrome_switches.h" | |
15 #include "components/prefs/pref_registry_simple.h" | |
16 #include "components/prefs/pref_service.h" | |
17 #include "components/proxy_config/pref_proxy_config_tracker_impl.h" | |
18 #include "components/syncable_prefs/pref_service_mock_factory.h" | |
19 #include "net/proxy/proxy_config_service_common_unittest.h" | |
20 #include "url/gurl.h" | |
21 | |
22 namespace { | |
23 | |
24 // Test parameter object for testing command line proxy configuration. | |
25 struct CommandLineTestParams { | |
26 // Short description to identify the test. | |
27 const char* description; | |
28 | |
29 // The command line to build a ProxyConfig from. | |
30 struct SwitchValue { | |
31 const char* name; | |
32 const char* value; | |
33 } switches[2]; | |
34 | |
35 // Expected outputs (fields of the ProxyConfig). | |
36 bool is_null; | |
37 bool auto_detect; | |
38 GURL pac_url; | |
39 net::ProxyRulesExpectation proxy_rules; | |
40 }; | |
41 | |
42 void PrintTo(const CommandLineTestParams& params, std::ostream* os) { | |
43 *os << params.description; | |
44 } | |
45 | |
46 static const CommandLineTestParams kCommandLineTestParams[] = { | |
47 { | |
48 "Empty command line", | |
49 // Input | |
50 {}, | |
51 // Expected result | |
52 true, // is_null | |
53 false, // auto_detect | |
54 GURL(), // pac_url | |
55 net::ProxyRulesExpectation::Empty(), | |
56 }, | |
57 { | |
58 "No proxy", | |
59 // Input | |
60 { | |
61 {switches::kNoProxyServer, NULL}, | |
62 }, | |
63 // Expected result | |
64 false, // is_null | |
65 false, // auto_detect | |
66 GURL(), // pac_url | |
67 net::ProxyRulesExpectation::Empty(), | |
68 }, | |
69 { | |
70 "No proxy with extra parameters.", | |
71 // Input | |
72 { | |
73 {switches::kNoProxyServer, NULL}, | |
74 {switches::kProxyServer, "http://proxy:8888"}, | |
75 }, | |
76 // Expected result | |
77 false, // is_null | |
78 false, // auto_detect | |
79 GURL(), // pac_url | |
80 net::ProxyRulesExpectation::Empty(), | |
81 }, | |
82 { | |
83 "Single proxy.", | |
84 // Input | |
85 { | |
86 {switches::kProxyServer, "http://proxy:8888"}, | |
87 }, | |
88 // Expected result | |
89 false, // is_null | |
90 false, // auto_detect | |
91 GURL(), // pac_url | |
92 net::ProxyRulesExpectation::Single("proxy:8888", // single proxy | |
93 ""), // bypass rules | |
94 }, | |
95 { | |
96 "Per scheme proxy.", | |
97 // Input | |
98 { | |
99 {switches::kProxyServer, "http=httpproxy:8888;ftp=ftpproxy:8889"}, | |
100 }, | |
101 // Expected result | |
102 false, // is_null | |
103 false, // auto_detect | |
104 GURL(), // pac_url | |
105 net::ProxyRulesExpectation::PerScheme("httpproxy:8888", // http | |
106 "", // https | |
107 "ftpproxy:8889", // ftp | |
108 ""), // bypass rules | |
109 }, | |
110 { | |
111 "Per scheme proxy with bypass URLs.", | |
112 // Input | |
113 { | |
114 {switches::kProxyServer, "http=httpproxy:8888;ftp=ftpproxy:8889"}, | |
115 {switches::kProxyBypassList, | |
116 ".google.com, foo.com:99, 1.2.3.4:22, 127.0.0.1/8"}, | |
117 }, | |
118 // Expected result | |
119 false, // is_null | |
120 false, // auto_detect | |
121 GURL(), // pac_url | |
122 net::ProxyRulesExpectation::PerScheme( | |
123 "httpproxy:8888", // http | |
124 "", // https | |
125 "ftpproxy:8889", // ftp | |
126 "*.google.com,foo.com:99,1.2.3.4:22,127.0.0.1/8"), | |
127 }, | |
128 { | |
129 "Pac URL", | |
130 // Input | |
131 { | |
132 {switches::kProxyPacUrl, "http://wpad/wpad.dat"}, | |
133 }, | |
134 // Expected result | |
135 false, // is_null | |
136 false, // auto_detect | |
137 GURL("http://wpad/wpad.dat"), // pac_url | |
138 net::ProxyRulesExpectation::Empty(), | |
139 }, | |
140 { | |
141 "Autodetect", | |
142 // Input | |
143 { | |
144 {switches::kProxyAutoDetect, NULL}, | |
145 }, | |
146 // Expected result | |
147 false, // is_null | |
148 true, // auto_detect | |
149 GURL(), // pac_url | |
150 net::ProxyRulesExpectation::Empty(), | |
151 }, | |
152 }; | |
153 | |
154 } // namespace | |
155 | |
156 class CommandLinePrefStoreProxyTest | |
157 : public testing::TestWithParam<CommandLineTestParams> { | |
158 protected: | |
159 CommandLinePrefStoreProxyTest() | |
160 : command_line_(base::CommandLine::NO_PROGRAM) {} | |
161 | |
162 net::ProxyConfig* proxy_config() { return &proxy_config_; } | |
163 | |
164 void SetUp() override { | |
165 for (size_t i = 0; i < arraysize(GetParam().switches); i++) { | |
166 const char* name = GetParam().switches[i].name; | |
167 const char* value = GetParam().switches[i].value; | |
168 if (name && value) | |
169 command_line_.AppendSwitchASCII(name, value); | |
170 else if (name) | |
171 command_line_.AppendSwitch(name); | |
172 } | |
173 scoped_refptr<PrefRegistrySimple> registry = new PrefRegistrySimple; | |
174 PrefProxyConfigTrackerImpl::RegisterPrefs(registry.get()); | |
175 syncable_prefs::PrefServiceMockFactory factory; | |
176 factory.set_command_line_prefs(new CommandLinePrefStore(&command_line_)); | |
177 pref_service_ = factory.Create(registry.get()); | |
178 PrefProxyConfigTrackerImpl::ReadPrefConfig(pref_service_.get(), | |
179 &proxy_config_); | |
180 } | |
181 | |
182 private: | |
183 base::CommandLine command_line_; | |
184 std::unique_ptr<PrefService> pref_service_; | |
185 net::ProxyConfig proxy_config_; | |
186 }; | |
187 | |
188 TEST_P(CommandLinePrefStoreProxyTest, CommandLine) { | |
189 EXPECT_EQ(GetParam().auto_detect, proxy_config()->auto_detect()); | |
190 EXPECT_EQ(GetParam().pac_url, proxy_config()->pac_url()); | |
191 EXPECT_TRUE(GetParam().proxy_rules.Matches(proxy_config()->proxy_rules())); | |
192 } | |
193 | |
194 INSTANTIATE_TEST_CASE_P(CommandLinePrefStoreProxyTestInstance, | |
195 CommandLinePrefStoreProxyTest, | |
196 testing::ValuesIn(kCommandLineTestParams)); | |
OLD | NEW |