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/policy/configuration_policy_pref_store_test.h"
11 #include "chrome/browser/prefs/proxy_config_dictionary.h"
12 #include "chrome/browser/prefs/proxy_prefs.h"
13 #include "chrome/common/pref_names.h"
14 #include "components/policy/core/browser/configuration_policy_pref_store.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
, POLICY_LEVEL_MANDATORY
, POLICY_SCOPE_USER
,
78 base::Value::CreateStringValue("http://chromium.org/override"),
80 policy
.Set(key::kProxyServer
, POLICY_LEVEL_MANDATORY
, POLICY_SCOPE_USER
,
81 base::Value::CreateStringValue("chromium.org"), NULL
);
83 key::kProxyServerMode
, POLICY_LEVEL_MANDATORY
, POLICY_SCOPE_USER
,
84 base::Value::CreateIntegerValue(
85 ProxyPolicyHandler::PROXY_MANUALLY_CONFIGURED_PROXY_SERVER_MODE
),
87 UpdateProviderPolicy(policy
);
89 VerifyProxyPrefs("chromium.org",
91 "http://chromium.org/override",
92 ProxyPrefs::MODE_FIXED_SERVERS
);
95 TEST_F(ProxyPolicyHandlerTest
, ManualOptionsReversedApplyOrder
) {
98 key::kProxyServerMode
, POLICY_LEVEL_MANDATORY
, POLICY_SCOPE_USER
,
99 base::Value::CreateIntegerValue(
100 ProxyPolicyHandler::PROXY_MANUALLY_CONFIGURED_PROXY_SERVER_MODE
),
102 policy
.Set(key::kProxyBypassList
, POLICY_LEVEL_MANDATORY
, POLICY_SCOPE_USER
,
103 base::Value::CreateStringValue("http://chromium.org/override"),
105 policy
.Set(key::kProxyServer
, POLICY_LEVEL_MANDATORY
, POLICY_SCOPE_USER
,
106 base::Value::CreateStringValue("chromium.org"), NULL
);
107 UpdateProviderPolicy(policy
);
109 VerifyProxyPrefs("chromium.org",
111 "http://chromium.org/override",
112 ProxyPrefs::MODE_FIXED_SERVERS
);
115 TEST_F(ProxyPolicyHandlerTest
, ManualOptionsInvalid
) {
118 key::kProxyServerMode
, POLICY_LEVEL_MANDATORY
, POLICY_SCOPE_USER
,
119 base::Value::CreateIntegerValue(
120 ProxyPolicyHandler::PROXY_MANUALLY_CONFIGURED_PROXY_SERVER_MODE
),
122 UpdateProviderPolicy(policy
);
124 const base::Value
* value
= NULL
;
125 EXPECT_FALSE(store_
->GetValue(prefs::kProxy
, &value
));
128 TEST_F(ProxyPolicyHandlerTest
, NoProxyServerMode
) {
130 policy
.Set(key::kProxyServerMode
, POLICY_LEVEL_MANDATORY
, POLICY_SCOPE_USER
,
131 base::Value::CreateIntegerValue(
132 ProxyPolicyHandler::PROXY_SERVER_MODE
),
134 UpdateProviderPolicy(policy
);
136 std::string(), std::string(), std::string(), ProxyPrefs::MODE_DIRECT
);
139 TEST_F(ProxyPolicyHandlerTest
, NoProxyModeName
) {
141 policy
.Set(key::kProxyMode
, POLICY_LEVEL_MANDATORY
, POLICY_SCOPE_USER
,
142 base::Value::CreateStringValue(ProxyPrefs::kDirectProxyModeName
),
144 UpdateProviderPolicy(policy
);
146 std::string(), std::string(), std::string(), ProxyPrefs::MODE_DIRECT
);
149 TEST_F(ProxyPolicyHandlerTest
, AutoDetectProxyServerMode
) {
152 key::kProxyServerMode
, POLICY_LEVEL_MANDATORY
, POLICY_SCOPE_USER
,
153 base::Value::CreateIntegerValue(
154 ProxyPolicyHandler::PROXY_AUTO_DETECT_PROXY_SERVER_MODE
),
156 UpdateProviderPolicy(policy
);
157 VerifyProxyPrefs(std::string(),
160 ProxyPrefs::MODE_AUTO_DETECT
);
163 TEST_F(ProxyPolicyHandlerTest
, AutoDetectProxyModeName
) {
165 policy
.Set(key::kProxyMode
, POLICY_LEVEL_MANDATORY
, POLICY_SCOPE_USER
,
166 base::Value::CreateStringValue(
167 ProxyPrefs::kAutoDetectProxyModeName
),
169 UpdateProviderPolicy(policy
);
170 VerifyProxyPrefs(std::string(),
173 ProxyPrefs::MODE_AUTO_DETECT
);
176 TEST_F(ProxyPolicyHandlerTest
, PacScriptProxyMode
) {
178 policy
.Set(key::kProxyPacUrl
, POLICY_LEVEL_MANDATORY
, POLICY_SCOPE_USER
,
179 base::Value::CreateStringValue("http://short.org/proxy.pac"),
181 policy
.Set(key::kProxyMode
, POLICY_LEVEL_MANDATORY
, POLICY_SCOPE_USER
,
182 base::Value::CreateStringValue(
183 ProxyPrefs::kPacScriptProxyModeName
),
185 UpdateProviderPolicy(policy
);
186 VerifyProxyPrefs(std::string(),
187 "http://short.org/proxy.pac",
189 ProxyPrefs::MODE_PAC_SCRIPT
);
192 TEST_F(ProxyPolicyHandlerTest
, PacScriptProxyModeInvalid
) {
194 policy
.Set(key::kProxyMode
, POLICY_LEVEL_MANDATORY
, POLICY_SCOPE_USER
,
195 base::Value::CreateStringValue(
196 ProxyPrefs::kPacScriptProxyModeName
),
198 UpdateProviderPolicy(policy
);
199 const base::Value
* value
= NULL
;
200 EXPECT_FALSE(store_
->GetValue(prefs::kProxy
, &value
));
203 // Regression test for http://crbug.com/78016, CPanel returns empty strings
204 // for unset properties.
205 TEST_F(ProxyPolicyHandlerTest
, PacScriptProxyModeBug78016
) {
207 policy
.Set(key::kProxyServer
,
208 POLICY_LEVEL_MANDATORY
,
210 base::Value::CreateStringValue(std::string()),
212 policy
.Set(key::kProxyPacUrl
,
213 POLICY_LEVEL_MANDATORY
,
215 base::Value::CreateStringValue("http://short.org/proxy.pac"),
217 policy
.Set(key::kProxyMode
, POLICY_LEVEL_MANDATORY
, POLICY_SCOPE_USER
,
218 base::Value::CreateStringValue(
219 ProxyPrefs::kPacScriptProxyModeName
),
221 UpdateProviderPolicy(policy
);
222 VerifyProxyPrefs(std::string(),
223 "http://short.org/proxy.pac",
225 ProxyPrefs::MODE_PAC_SCRIPT
);
228 TEST_F(ProxyPolicyHandlerTest
, UseSystemProxyServerMode
) {
230 policy
.Set(key::kProxyServerMode
, POLICY_LEVEL_MANDATORY
, POLICY_SCOPE_USER
,
231 base::Value::CreateIntegerValue(
232 ProxyPolicyHandler::PROXY_USE_SYSTEM_PROXY_SERVER_MODE
),
234 UpdateProviderPolicy(policy
);
236 std::string(), std::string(), std::string(), ProxyPrefs::MODE_SYSTEM
);
239 TEST_F(ProxyPolicyHandlerTest
, UseSystemProxyMode
) {
241 policy
.Set(key::kProxyMode
, POLICY_LEVEL_MANDATORY
, POLICY_SCOPE_USER
,
242 base::Value::CreateStringValue(ProxyPrefs::kSystemProxyModeName
),
244 UpdateProviderPolicy(policy
);
246 std::string(), std::string(), std::string(), ProxyPrefs::MODE_SYSTEM
);
249 TEST_F(ProxyPolicyHandlerTest
,
250 ProxyModeOverridesProxyServerMode
) {
252 policy
.Set(key::kProxyServerMode
, POLICY_LEVEL_MANDATORY
, POLICY_SCOPE_USER
,
253 base::Value::CreateIntegerValue(
254 ProxyPolicyHandler::PROXY_SERVER_MODE
),
256 policy
.Set(key::kProxyMode
, POLICY_LEVEL_MANDATORY
, POLICY_SCOPE_USER
,
257 base::Value::CreateStringValue(
258 ProxyPrefs::kAutoDetectProxyModeName
),
260 UpdateProviderPolicy(policy
);
261 VerifyProxyPrefs(std::string(),
264 ProxyPrefs::MODE_AUTO_DETECT
);
267 TEST_F(ProxyPolicyHandlerTest
, ProxyInvalid
) {
268 // No mode expects all three parameters being set.
270 policy
.Set(key::kProxyPacUrl
, POLICY_LEVEL_MANDATORY
, POLICY_SCOPE_USER
,
271 base::Value::CreateStringValue("http://short.org/proxy.pac"),
273 policy
.Set(key::kProxyBypassList
, POLICY_LEVEL_MANDATORY
, POLICY_SCOPE_USER
,
274 base::Value::CreateStringValue("http://chromium.org/override"),
276 policy
.Set(key::kProxyServer
, POLICY_LEVEL_MANDATORY
, POLICY_SCOPE_USER
,
277 base::Value::CreateStringValue("chromium.org"), NULL
);
278 for (int i
= 0; i
< ProxyPolicyHandler::MODE_COUNT
; ++i
) {
279 policy
.Set(key::kProxyServerMode
, POLICY_LEVEL_MANDATORY
,
280 POLICY_SCOPE_USER
, base::Value::CreateIntegerValue(i
), NULL
);
281 UpdateProviderPolicy(policy
);
282 const base::Value
* value
= NULL
;
283 EXPECT_FALSE(store_
->GetValue(prefs::kProxy
, &value
));
287 } // namespace policy