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.
7 #include "base/memory/scoped_ptr.h"
8 #include "base/values.h"
9 #include "chrome/browser/net/proxy_policy_handler.h"
10 #include "chrome/browser/prefs/proxy_config_dictionary.h"
11 #include "chrome/browser/prefs/proxy_prefs.h"
12 #include "chrome/common/pref_names.h"
13 #include "components/policy/core/browser/configuration_policy_pref_store.h"
14 #include "components/policy/core/browser/configuration_policy_pref_store_test.h"
15 #include "components/policy/core/common/policy_service_impl.h"
16 #include "policy/policy_constants.h"
17 #include "testing/gtest/include/gtest/gtest.h"
21 // Test cases for the proxy policy settings.
22 class ProxyPolicyHandlerTest
23 : public ConfigurationPolicyPrefStoreTest
{
25 virtual 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.
33 policy_service_
.reset(new PolicyServiceImpl(providers_
));
34 store_
= new ConfigurationPolicyPrefStore(
35 policy_service_
.get(), &handler_list_
, POLICY_LEVEL_MANDATORY
);
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(prefs::kProxy
, &value
));
47 ASSERT_EQ(base::Value::TYPE_DICTIONARY
, value
->GetType());
48 ProxyConfigDictionary
dict(
49 static_cast<const base::DictionaryValue
*>(value
));
51 if (expected_proxy_server
.empty()) {
52 EXPECT_FALSE(dict
.GetProxyServer(&s
));
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
));
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
));
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
) {
77 policy
.Set(key::kProxyBypassList
,
78 POLICY_LEVEL_MANDATORY
,
80 new base::StringValue("http://chromium.org/override"),
82 policy
.Set(key::kProxyServer
,
83 POLICY_LEVEL_MANDATORY
,
85 new base::StringValue("chromium.org"),
88 key::kProxyServerMode
, POLICY_LEVEL_MANDATORY
, POLICY_SCOPE_USER
,
89 base::Value::CreateIntegerValue(
90 ProxyPolicyHandler::PROXY_MANUALLY_CONFIGURED_PROXY_SERVER_MODE
),
92 UpdateProviderPolicy(policy
);
94 VerifyProxyPrefs("chromium.org",
96 "http://chromium.org/override",
97 ProxyPrefs::MODE_FIXED_SERVERS
);
100 TEST_F(ProxyPolicyHandlerTest
, ManualOptionsReversedApplyOrder
) {
103 key::kProxyServerMode
, POLICY_LEVEL_MANDATORY
, POLICY_SCOPE_USER
,
104 base::Value::CreateIntegerValue(
105 ProxyPolicyHandler::PROXY_MANUALLY_CONFIGURED_PROXY_SERVER_MODE
),
107 policy
.Set(key::kProxyBypassList
,
108 POLICY_LEVEL_MANDATORY
,
110 new base::StringValue("http://chromium.org/override"),
112 policy
.Set(key::kProxyServer
,
113 POLICY_LEVEL_MANDATORY
,
115 new base::StringValue("chromium.org"),
117 UpdateProviderPolicy(policy
);
119 VerifyProxyPrefs("chromium.org",
121 "http://chromium.org/override",
122 ProxyPrefs::MODE_FIXED_SERVERS
);
125 TEST_F(ProxyPolicyHandlerTest
, ManualOptionsInvalid
) {
128 key::kProxyServerMode
, POLICY_LEVEL_MANDATORY
, POLICY_SCOPE_USER
,
129 base::Value::CreateIntegerValue(
130 ProxyPolicyHandler::PROXY_MANUALLY_CONFIGURED_PROXY_SERVER_MODE
),
132 UpdateProviderPolicy(policy
);
134 const base::Value
* value
= NULL
;
135 EXPECT_FALSE(store_
->GetValue(prefs::kProxy
, &value
));
138 TEST_F(ProxyPolicyHandlerTest
, NoProxyServerMode
) {
140 policy
.Set(key::kProxyServerMode
, POLICY_LEVEL_MANDATORY
, POLICY_SCOPE_USER
,
141 base::Value::CreateIntegerValue(
142 ProxyPolicyHandler::PROXY_SERVER_MODE
),
144 UpdateProviderPolicy(policy
);
146 std::string(), std::string(), std::string(), ProxyPrefs::MODE_DIRECT
);
149 TEST_F(ProxyPolicyHandlerTest
, NoProxyModeName
) {
151 policy
.Set(key::kProxyMode
,
152 POLICY_LEVEL_MANDATORY
,
154 new base::StringValue(ProxyPrefs::kDirectProxyModeName
),
156 UpdateProviderPolicy(policy
);
158 std::string(), std::string(), std::string(), ProxyPrefs::MODE_DIRECT
);
161 TEST_F(ProxyPolicyHandlerTest
, AutoDetectProxyServerMode
) {
164 key::kProxyServerMode
, POLICY_LEVEL_MANDATORY
, POLICY_SCOPE_USER
,
165 base::Value::CreateIntegerValue(
166 ProxyPolicyHandler::PROXY_AUTO_DETECT_PROXY_SERVER_MODE
),
168 UpdateProviderPolicy(policy
);
169 VerifyProxyPrefs(std::string(),
172 ProxyPrefs::MODE_AUTO_DETECT
);
175 TEST_F(ProxyPolicyHandlerTest
, AutoDetectProxyModeName
) {
177 policy
.Set(key::kProxyMode
,
178 POLICY_LEVEL_MANDATORY
,
180 new base::StringValue(ProxyPrefs::kAutoDetectProxyModeName
),
182 UpdateProviderPolicy(policy
);
183 VerifyProxyPrefs(std::string(),
186 ProxyPrefs::MODE_AUTO_DETECT
);
189 TEST_F(ProxyPolicyHandlerTest
, PacScriptProxyMode
) {
191 policy
.Set(key::kProxyPacUrl
,
192 POLICY_LEVEL_MANDATORY
,
194 new base::StringValue("http://short.org/proxy.pac"),
196 policy
.Set(key::kProxyMode
,
197 POLICY_LEVEL_MANDATORY
,
199 new base::StringValue(ProxyPrefs::kPacScriptProxyModeName
),
201 UpdateProviderPolicy(policy
);
202 VerifyProxyPrefs(std::string(),
203 "http://short.org/proxy.pac",
205 ProxyPrefs::MODE_PAC_SCRIPT
);
208 TEST_F(ProxyPolicyHandlerTest
, PacScriptProxyModeInvalid
) {
210 policy
.Set(key::kProxyMode
,
211 POLICY_LEVEL_MANDATORY
,
213 new base::StringValue(ProxyPrefs::kPacScriptProxyModeName
),
215 UpdateProviderPolicy(policy
);
216 const base::Value
* value
= NULL
;
217 EXPECT_FALSE(store_
->GetValue(prefs::kProxy
, &value
));
220 // Regression test for http://crbug.com/78016, CPanel returns empty strings
221 // for unset properties.
222 TEST_F(ProxyPolicyHandlerTest
, PacScriptProxyModeBug78016
) {
224 policy
.Set(key::kProxyServer
,
225 POLICY_LEVEL_MANDATORY
,
227 new base::StringValue(std::string()),
229 policy
.Set(key::kProxyPacUrl
,
230 POLICY_LEVEL_MANDATORY
,
232 new base::StringValue("http://short.org/proxy.pac"),
234 policy
.Set(key::kProxyMode
,
235 POLICY_LEVEL_MANDATORY
,
237 new base::StringValue(ProxyPrefs::kPacScriptProxyModeName
),
239 UpdateProviderPolicy(policy
);
240 VerifyProxyPrefs(std::string(),
241 "http://short.org/proxy.pac",
243 ProxyPrefs::MODE_PAC_SCRIPT
);
246 TEST_F(ProxyPolicyHandlerTest
, UseSystemProxyServerMode
) {
248 policy
.Set(key::kProxyServerMode
, POLICY_LEVEL_MANDATORY
, POLICY_SCOPE_USER
,
249 base::Value::CreateIntegerValue(
250 ProxyPolicyHandler::PROXY_USE_SYSTEM_PROXY_SERVER_MODE
),
252 UpdateProviderPolicy(policy
);
254 std::string(), std::string(), std::string(), ProxyPrefs::MODE_SYSTEM
);
257 TEST_F(ProxyPolicyHandlerTest
, UseSystemProxyMode
) {
259 policy
.Set(key::kProxyMode
,
260 POLICY_LEVEL_MANDATORY
,
262 new base::StringValue(ProxyPrefs::kSystemProxyModeName
),
264 UpdateProviderPolicy(policy
);
266 std::string(), std::string(), std::string(), ProxyPrefs::MODE_SYSTEM
);
269 TEST_F(ProxyPolicyHandlerTest
,
270 ProxyModeOverridesProxyServerMode
) {
272 policy
.Set(key::kProxyServerMode
, POLICY_LEVEL_MANDATORY
, POLICY_SCOPE_USER
,
273 base::Value::CreateIntegerValue(
274 ProxyPolicyHandler::PROXY_SERVER_MODE
),
276 policy
.Set(key::kProxyMode
,
277 POLICY_LEVEL_MANDATORY
,
279 new base::StringValue(ProxyPrefs::kAutoDetectProxyModeName
),
281 UpdateProviderPolicy(policy
);
282 VerifyProxyPrefs(std::string(),
285 ProxyPrefs::MODE_AUTO_DETECT
);
288 TEST_F(ProxyPolicyHandlerTest
, ProxyInvalid
) {
289 // No mode expects all three parameters being set.
291 policy
.Set(key::kProxyPacUrl
,
292 POLICY_LEVEL_MANDATORY
,
294 new base::StringValue("http://short.org/proxy.pac"),
296 policy
.Set(key::kProxyBypassList
,
297 POLICY_LEVEL_MANDATORY
,
299 new base::StringValue("http://chromium.org/override"),
301 policy
.Set(key::kProxyServer
,
302 POLICY_LEVEL_MANDATORY
,
304 new base::StringValue("chromium.org"),
306 for (int i
= 0; i
< ProxyPolicyHandler::MODE_COUNT
; ++i
) {
307 policy
.Set(key::kProxyServerMode
, POLICY_LEVEL_MANDATORY
,
308 POLICY_SCOPE_USER
, base::Value::CreateIntegerValue(i
), NULL
);
309 UpdateProviderPolicy(policy
);
310 const base::Value
* value
= NULL
;
311 EXPECT_FALSE(store_
->GetValue(prefs::kProxy
, &value
));
315 } // namespace policy