Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / net / proxy_policy_handler_unittest.cc
blob823101b348b1fa2ccb0d6a74227a4d5004d43538
1 // Copyright 2013 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 <string>
7 #include "base/memory/scoped_ptr.h"
8 #include "base/values.h"
9 #include "chrome/browser/net/proxy_policy_handler.h"
10 #include "components/policy/core/browser/configuration_policy_pref_store.h"
11 #include "components/policy/core/browser/configuration_policy_pref_store_test.h"
12 #include "components/policy/core/common/policy_service_impl.h"
13 #include "components/policy/core/common/policy_types.h"
14 #include "components/proxy_config/proxy_config_dictionary.h"
15 #include "components/proxy_config/proxy_config_pref_names.h"
16 #include "policy/policy_constants.h"
17 #include "testing/gtest/include/gtest/gtest.h"
19 namespace policy {
21 // Test cases for the proxy policy settings.
22 class ProxyPolicyHandlerTest
23 : public ConfigurationPolicyPrefStoreTest {
24 public:
25 void SetUp() override {
26 ConfigurationPolicyPrefStoreTest::SetUp();
27 handler_list_.AddHandler(
28 make_scoped_ptr<ConfigurationPolicyHandler>(new ProxyPolicyHandler));
29 // Reset the PolicyServiceImpl to one that has the policy fixup
30 // preprocessor. The previous store must be nulled out first so that it
31 // removes itself from the service's observer list.
32 store_ = NULL;
33 policy_service_.reset(new PolicyServiceImpl(providers_));
34 store_ = new ConfigurationPolicyPrefStore(
35 policy_service_.get(), &handler_list_, POLICY_LEVEL_MANDATORY);
38 protected:
39 // Verify that all the proxy prefs are set to the specified expected values.
40 void VerifyProxyPrefs(
41 const std::string& expected_proxy_server,
42 const std::string& expected_proxy_pac_url,
43 const std::string& expected_proxy_bypass_list,
44 const ProxyPrefs::ProxyMode& expected_proxy_mode) {
45 const base::Value* value = NULL;
46 ASSERT_TRUE(store_->GetValue(proxy_config::prefs::kProxy, &value));
47 ASSERT_EQ(base::Value::TYPE_DICTIONARY, value->GetType());
48 ProxyConfigDictionary dict(
49 static_cast<const base::DictionaryValue*>(value));
50 std::string s;
51 if (expected_proxy_server.empty()) {
52 EXPECT_FALSE(dict.GetProxyServer(&s));
53 } else {
54 ASSERT_TRUE(dict.GetProxyServer(&s));
55 EXPECT_EQ(expected_proxy_server, s);
57 if (expected_proxy_pac_url.empty()) {
58 EXPECT_FALSE(dict.GetPacUrl(&s));
59 } else {
60 ASSERT_TRUE(dict.GetPacUrl(&s));
61 EXPECT_EQ(expected_proxy_pac_url, s);
63 if (expected_proxy_bypass_list.empty()) {
64 EXPECT_FALSE(dict.GetBypassList(&s));
65 } else {
66 ASSERT_TRUE(dict.GetBypassList(&s));
67 EXPECT_EQ(expected_proxy_bypass_list, s);
69 ProxyPrefs::ProxyMode mode;
70 ASSERT_TRUE(dict.GetMode(&mode));
71 EXPECT_EQ(expected_proxy_mode, mode);
75 TEST_F(ProxyPolicyHandlerTest, ManualOptions) {
76 PolicyMap policy;
77 policy.Set(key::kProxyBypassList,
78 POLICY_LEVEL_MANDATORY,
79 POLICY_SCOPE_USER,
80 POLICY_SOURCE_CLOUD,
81 new base::StringValue("http://chromium.org/override"),
82 NULL);
83 policy.Set(key::kProxyServer,
84 POLICY_LEVEL_MANDATORY,
85 POLICY_SCOPE_USER,
86 POLICY_SOURCE_CLOUD,
87 new base::StringValue("chromium.org"),
88 NULL);
89 policy.Set(
90 key::kProxyServerMode,
91 POLICY_LEVEL_MANDATORY,
92 POLICY_SCOPE_USER,
93 POLICY_SOURCE_CLOUD,
94 new base::FundamentalValue(
95 ProxyPolicyHandler::PROXY_MANUALLY_CONFIGURED_PROXY_SERVER_MODE),
96 NULL);
97 UpdateProviderPolicy(policy);
99 VerifyProxyPrefs("chromium.org",
100 std::string(),
101 "http://chromium.org/override",
102 ProxyPrefs::MODE_FIXED_SERVERS);
105 TEST_F(ProxyPolicyHandlerTest, ManualOptionsReversedApplyOrder) {
106 PolicyMap policy;
107 policy.Set(
108 key::kProxyServerMode,
109 POLICY_LEVEL_MANDATORY,
110 POLICY_SCOPE_USER,
111 POLICY_SOURCE_CLOUD,
112 new base::FundamentalValue(
113 ProxyPolicyHandler::PROXY_MANUALLY_CONFIGURED_PROXY_SERVER_MODE),
114 NULL);
115 policy.Set(key::kProxyBypassList,
116 POLICY_LEVEL_MANDATORY,
117 POLICY_SCOPE_USER,
118 POLICY_SOURCE_CLOUD,
119 new base::StringValue("http://chromium.org/override"),
120 NULL);
121 policy.Set(key::kProxyServer,
122 POLICY_LEVEL_MANDATORY,
123 POLICY_SCOPE_USER,
124 POLICY_SOURCE_CLOUD,
125 new base::StringValue("chromium.org"),
126 NULL);
127 UpdateProviderPolicy(policy);
129 VerifyProxyPrefs("chromium.org",
130 std::string(),
131 "http://chromium.org/override",
132 ProxyPrefs::MODE_FIXED_SERVERS);
135 TEST_F(ProxyPolicyHandlerTest, ManualOptionsInvalid) {
136 PolicyMap policy;
137 policy.Set(
138 key::kProxyServerMode,
139 POLICY_LEVEL_MANDATORY,
140 POLICY_SCOPE_USER,
141 POLICY_SOURCE_CLOUD,
142 new base::FundamentalValue(
143 ProxyPolicyHandler::PROXY_MANUALLY_CONFIGURED_PROXY_SERVER_MODE),
144 NULL);
145 UpdateProviderPolicy(policy);
147 const base::Value* value = NULL;
148 EXPECT_FALSE(store_->GetValue(proxy_config::prefs::kProxy, &value));
151 TEST_F(ProxyPolicyHandlerTest, NoProxyServerMode) {
152 PolicyMap policy;
153 policy.Set(key::kProxyServerMode,
154 POLICY_LEVEL_MANDATORY,
155 POLICY_SCOPE_USER,
156 POLICY_SOURCE_CLOUD,
157 new base::FundamentalValue(ProxyPolicyHandler::PROXY_SERVER_MODE),
158 NULL);
159 UpdateProviderPolicy(policy);
160 VerifyProxyPrefs(
161 std::string(), std::string(), std::string(), ProxyPrefs::MODE_DIRECT);
164 TEST_F(ProxyPolicyHandlerTest, NoProxyModeName) {
165 PolicyMap policy;
166 policy.Set(key::kProxyMode,
167 POLICY_LEVEL_MANDATORY,
168 POLICY_SCOPE_USER,
169 POLICY_SOURCE_CLOUD,
170 new base::StringValue(ProxyPrefs::kDirectProxyModeName),
171 NULL);
172 UpdateProviderPolicy(policy);
173 VerifyProxyPrefs(
174 std::string(), std::string(), std::string(), ProxyPrefs::MODE_DIRECT);
177 TEST_F(ProxyPolicyHandlerTest, AutoDetectProxyServerMode) {
178 PolicyMap policy;
179 policy.Set(key::kProxyServerMode,
180 POLICY_LEVEL_MANDATORY,
181 POLICY_SCOPE_USER,
182 POLICY_SOURCE_CLOUD,
183 new base::FundamentalValue(
184 ProxyPolicyHandler::PROXY_AUTO_DETECT_PROXY_SERVER_MODE),
185 NULL);
186 UpdateProviderPolicy(policy);
187 VerifyProxyPrefs(std::string(),
188 std::string(),
189 std::string(),
190 ProxyPrefs::MODE_AUTO_DETECT);
193 TEST_F(ProxyPolicyHandlerTest, AutoDetectProxyModeName) {
194 PolicyMap policy;
195 policy.Set(key::kProxyMode,
196 POLICY_LEVEL_MANDATORY,
197 POLICY_SCOPE_USER,
198 POLICY_SOURCE_CLOUD,
199 new base::StringValue(ProxyPrefs::kAutoDetectProxyModeName),
200 NULL);
201 UpdateProviderPolicy(policy);
202 VerifyProxyPrefs(std::string(),
203 std::string(),
204 std::string(),
205 ProxyPrefs::MODE_AUTO_DETECT);
208 TEST_F(ProxyPolicyHandlerTest, PacScriptProxyMode) {
209 PolicyMap policy;
210 policy.Set(key::kProxyPacUrl,
211 POLICY_LEVEL_MANDATORY,
212 POLICY_SCOPE_USER,
213 POLICY_SOURCE_CLOUD,
214 new base::StringValue("http://short.org/proxy.pac"),
215 NULL);
216 policy.Set(key::kProxyMode,
217 POLICY_LEVEL_MANDATORY,
218 POLICY_SCOPE_USER,
219 POLICY_SOURCE_CLOUD,
220 new base::StringValue(ProxyPrefs::kPacScriptProxyModeName),
221 NULL);
222 UpdateProviderPolicy(policy);
223 VerifyProxyPrefs(std::string(),
224 "http://short.org/proxy.pac",
225 std::string(),
226 ProxyPrefs::MODE_PAC_SCRIPT);
229 TEST_F(ProxyPolicyHandlerTest, PacScriptProxyModeInvalid) {
230 PolicyMap policy;
231 policy.Set(key::kProxyMode,
232 POLICY_LEVEL_MANDATORY,
233 POLICY_SCOPE_USER,
234 POLICY_SOURCE_CLOUD,
235 new base::StringValue(ProxyPrefs::kPacScriptProxyModeName),
236 NULL);
237 UpdateProviderPolicy(policy);
238 const base::Value* value = NULL;
239 EXPECT_FALSE(store_->GetValue(proxy_config::prefs::kProxy, &value));
242 // Regression test for http://crbug.com/78016, CPanel returns empty strings
243 // for unset properties.
244 TEST_F(ProxyPolicyHandlerTest, PacScriptProxyModeBug78016) {
245 PolicyMap policy;
246 policy.Set(key::kProxyServer,
247 POLICY_LEVEL_MANDATORY,
248 POLICY_SCOPE_USER,
249 POLICY_SOURCE_CLOUD,
250 new base::StringValue(std::string()),
251 NULL);
252 policy.Set(key::kProxyPacUrl,
253 POLICY_LEVEL_MANDATORY,
254 POLICY_SCOPE_USER,
255 POLICY_SOURCE_CLOUD,
256 new base::StringValue("http://short.org/proxy.pac"),
257 NULL);
258 policy.Set(key::kProxyMode,
259 POLICY_LEVEL_MANDATORY,
260 POLICY_SCOPE_USER,
261 POLICY_SOURCE_CLOUD,
262 new base::StringValue(ProxyPrefs::kPacScriptProxyModeName),
263 NULL);
264 UpdateProviderPolicy(policy);
265 VerifyProxyPrefs(std::string(),
266 "http://short.org/proxy.pac",
267 std::string(),
268 ProxyPrefs::MODE_PAC_SCRIPT);
271 TEST_F(ProxyPolicyHandlerTest, UseSystemProxyServerMode) {
272 PolicyMap policy;
273 policy.Set(key::kProxyServerMode,
274 POLICY_LEVEL_MANDATORY,
275 POLICY_SCOPE_USER,
276 POLICY_SOURCE_CLOUD,
277 new base::FundamentalValue(
278 ProxyPolicyHandler::PROXY_USE_SYSTEM_PROXY_SERVER_MODE),
279 NULL);
280 UpdateProviderPolicy(policy);
281 VerifyProxyPrefs(
282 std::string(), std::string(), std::string(), ProxyPrefs::MODE_SYSTEM);
285 TEST_F(ProxyPolicyHandlerTest, UseSystemProxyMode) {
286 PolicyMap policy;
287 policy.Set(key::kProxyMode,
288 POLICY_LEVEL_MANDATORY,
289 POLICY_SCOPE_USER,
290 POLICY_SOURCE_CLOUD,
291 new base::StringValue(ProxyPrefs::kSystemProxyModeName),
292 NULL);
293 UpdateProviderPolicy(policy);
294 VerifyProxyPrefs(
295 std::string(), std::string(), std::string(), ProxyPrefs::MODE_SYSTEM);
298 TEST_F(ProxyPolicyHandlerTest,
299 ProxyModeOverridesProxyServerMode) {
300 PolicyMap policy;
301 policy.Set(key::kProxyServerMode,
302 POLICY_LEVEL_MANDATORY,
303 POLICY_SCOPE_USER,
304 POLICY_SOURCE_CLOUD,
305 new base::FundamentalValue(ProxyPolicyHandler::PROXY_SERVER_MODE),
306 NULL);
307 policy.Set(key::kProxyMode,
308 POLICY_LEVEL_MANDATORY,
309 POLICY_SCOPE_USER,
310 POLICY_SOURCE_CLOUD,
311 new base::StringValue(ProxyPrefs::kAutoDetectProxyModeName),
312 NULL);
313 UpdateProviderPolicy(policy);
314 VerifyProxyPrefs(std::string(),
315 std::string(),
316 std::string(),
317 ProxyPrefs::MODE_AUTO_DETECT);
320 TEST_F(ProxyPolicyHandlerTest, ProxyInvalid) {
321 // No mode expects all three parameters being set.
322 PolicyMap policy;
323 policy.Set(key::kProxyPacUrl,
324 POLICY_LEVEL_MANDATORY,
325 POLICY_SCOPE_USER,
326 POLICY_SOURCE_CLOUD,
327 new base::StringValue("http://short.org/proxy.pac"),
328 NULL);
329 policy.Set(key::kProxyBypassList,
330 POLICY_LEVEL_MANDATORY,
331 POLICY_SCOPE_USER,
332 POLICY_SOURCE_CLOUD,
333 new base::StringValue("http://chromium.org/override"),
334 NULL);
335 policy.Set(key::kProxyServer,
336 POLICY_LEVEL_MANDATORY,
337 POLICY_SCOPE_USER,
338 POLICY_SOURCE_CLOUD,
339 new base::StringValue("chromium.org"),
340 NULL);
341 for (int i = 0; i < ProxyPolicyHandler::MODE_COUNT; ++i) {
342 policy.Set(key::kProxyServerMode,
343 POLICY_LEVEL_MANDATORY,
344 POLICY_SCOPE_USER,
345 POLICY_SOURCE_CLOUD,
346 new base::FundamentalValue(i),
347 NULL);
348 UpdateProviderPolicy(policy);
349 const base::Value* value = NULL;
350 EXPECT_FALSE(store_->GetValue(proxy_config::prefs::kProxy, &value));
354 } // namespace policy