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 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
,
89 POLICY_LEVEL_MANDATORY
,
91 new base::FundamentalValue(
92 ProxyPolicyHandler::PROXY_MANUALLY_CONFIGURED_PROXY_SERVER_MODE
),
94 UpdateProviderPolicy(policy
);
96 VerifyProxyPrefs("chromium.org",
98 "http://chromium.org/override",
99 ProxyPrefs::MODE_FIXED_SERVERS
);
102 TEST_F(ProxyPolicyHandlerTest
, ManualOptionsReversedApplyOrder
) {
105 key::kProxyServerMode
,
106 POLICY_LEVEL_MANDATORY
,
108 new base::FundamentalValue(
109 ProxyPolicyHandler::PROXY_MANUALLY_CONFIGURED_PROXY_SERVER_MODE
),
111 policy
.Set(key::kProxyBypassList
,
112 POLICY_LEVEL_MANDATORY
,
114 new base::StringValue("http://chromium.org/override"),
116 policy
.Set(key::kProxyServer
,
117 POLICY_LEVEL_MANDATORY
,
119 new base::StringValue("chromium.org"),
121 UpdateProviderPolicy(policy
);
123 VerifyProxyPrefs("chromium.org",
125 "http://chromium.org/override",
126 ProxyPrefs::MODE_FIXED_SERVERS
);
129 TEST_F(ProxyPolicyHandlerTest
, ManualOptionsInvalid
) {
132 key::kProxyServerMode
,
133 POLICY_LEVEL_MANDATORY
,
135 new base::FundamentalValue(
136 ProxyPolicyHandler::PROXY_MANUALLY_CONFIGURED_PROXY_SERVER_MODE
),
138 UpdateProviderPolicy(policy
);
140 const base::Value
* value
= NULL
;
141 EXPECT_FALSE(store_
->GetValue(prefs::kProxy
, &value
));
144 TEST_F(ProxyPolicyHandlerTest
, NoProxyServerMode
) {
146 policy
.Set(key::kProxyServerMode
,
147 POLICY_LEVEL_MANDATORY
,
149 new base::FundamentalValue(ProxyPolicyHandler::PROXY_SERVER_MODE
),
151 UpdateProviderPolicy(policy
);
153 std::string(), std::string(), std::string(), ProxyPrefs::MODE_DIRECT
);
156 TEST_F(ProxyPolicyHandlerTest
, NoProxyModeName
) {
158 policy
.Set(key::kProxyMode
,
159 POLICY_LEVEL_MANDATORY
,
161 new base::StringValue(ProxyPrefs::kDirectProxyModeName
),
163 UpdateProviderPolicy(policy
);
165 std::string(), std::string(), std::string(), ProxyPrefs::MODE_DIRECT
);
168 TEST_F(ProxyPolicyHandlerTest
, AutoDetectProxyServerMode
) {
170 policy
.Set(key::kProxyServerMode
,
171 POLICY_LEVEL_MANDATORY
,
173 new base::FundamentalValue(
174 ProxyPolicyHandler::PROXY_AUTO_DETECT_PROXY_SERVER_MODE
),
176 UpdateProviderPolicy(policy
);
177 VerifyProxyPrefs(std::string(),
180 ProxyPrefs::MODE_AUTO_DETECT
);
183 TEST_F(ProxyPolicyHandlerTest
, AutoDetectProxyModeName
) {
185 policy
.Set(key::kProxyMode
,
186 POLICY_LEVEL_MANDATORY
,
188 new base::StringValue(ProxyPrefs::kAutoDetectProxyModeName
),
190 UpdateProviderPolicy(policy
);
191 VerifyProxyPrefs(std::string(),
194 ProxyPrefs::MODE_AUTO_DETECT
);
197 TEST_F(ProxyPolicyHandlerTest
, PacScriptProxyMode
) {
199 policy
.Set(key::kProxyPacUrl
,
200 POLICY_LEVEL_MANDATORY
,
202 new base::StringValue("http://short.org/proxy.pac"),
204 policy
.Set(key::kProxyMode
,
205 POLICY_LEVEL_MANDATORY
,
207 new base::StringValue(ProxyPrefs::kPacScriptProxyModeName
),
209 UpdateProviderPolicy(policy
);
210 VerifyProxyPrefs(std::string(),
211 "http://short.org/proxy.pac",
213 ProxyPrefs::MODE_PAC_SCRIPT
);
216 TEST_F(ProxyPolicyHandlerTest
, PacScriptProxyModeInvalid
) {
218 policy
.Set(key::kProxyMode
,
219 POLICY_LEVEL_MANDATORY
,
221 new base::StringValue(ProxyPrefs::kPacScriptProxyModeName
),
223 UpdateProviderPolicy(policy
);
224 const base::Value
* value
= NULL
;
225 EXPECT_FALSE(store_
->GetValue(prefs::kProxy
, &value
));
228 // Regression test for http://crbug.com/78016, CPanel returns empty strings
229 // for unset properties.
230 TEST_F(ProxyPolicyHandlerTest
, PacScriptProxyModeBug78016
) {
232 policy
.Set(key::kProxyServer
,
233 POLICY_LEVEL_MANDATORY
,
235 new base::StringValue(std::string()),
237 policy
.Set(key::kProxyPacUrl
,
238 POLICY_LEVEL_MANDATORY
,
240 new base::StringValue("http://short.org/proxy.pac"),
242 policy
.Set(key::kProxyMode
,
243 POLICY_LEVEL_MANDATORY
,
245 new base::StringValue(ProxyPrefs::kPacScriptProxyModeName
),
247 UpdateProviderPolicy(policy
);
248 VerifyProxyPrefs(std::string(),
249 "http://short.org/proxy.pac",
251 ProxyPrefs::MODE_PAC_SCRIPT
);
254 TEST_F(ProxyPolicyHandlerTest
, UseSystemProxyServerMode
) {
256 policy
.Set(key::kProxyServerMode
,
257 POLICY_LEVEL_MANDATORY
,
259 new base::FundamentalValue(
260 ProxyPolicyHandler::PROXY_USE_SYSTEM_PROXY_SERVER_MODE
),
262 UpdateProviderPolicy(policy
);
264 std::string(), std::string(), std::string(), ProxyPrefs::MODE_SYSTEM
);
267 TEST_F(ProxyPolicyHandlerTest
, UseSystemProxyMode
) {
269 policy
.Set(key::kProxyMode
,
270 POLICY_LEVEL_MANDATORY
,
272 new base::StringValue(ProxyPrefs::kSystemProxyModeName
),
274 UpdateProviderPolicy(policy
);
276 std::string(), std::string(), std::string(), ProxyPrefs::MODE_SYSTEM
);
279 TEST_F(ProxyPolicyHandlerTest
,
280 ProxyModeOverridesProxyServerMode
) {
282 policy
.Set(key::kProxyServerMode
,
283 POLICY_LEVEL_MANDATORY
,
285 new base::FundamentalValue(ProxyPolicyHandler::PROXY_SERVER_MODE
),
287 policy
.Set(key::kProxyMode
,
288 POLICY_LEVEL_MANDATORY
,
290 new base::StringValue(ProxyPrefs::kAutoDetectProxyModeName
),
292 UpdateProviderPolicy(policy
);
293 VerifyProxyPrefs(std::string(),
296 ProxyPrefs::MODE_AUTO_DETECT
);
299 TEST_F(ProxyPolicyHandlerTest
, ProxyInvalid
) {
300 // No mode expects all three parameters being set.
302 policy
.Set(key::kProxyPacUrl
,
303 POLICY_LEVEL_MANDATORY
,
305 new base::StringValue("http://short.org/proxy.pac"),
307 policy
.Set(key::kProxyBypassList
,
308 POLICY_LEVEL_MANDATORY
,
310 new base::StringValue("http://chromium.org/override"),
312 policy
.Set(key::kProxyServer
,
313 POLICY_LEVEL_MANDATORY
,
315 new base::StringValue("chromium.org"),
317 for (int i
= 0; i
< ProxyPolicyHandler::MODE_COUNT
; ++i
) {
318 policy
.Set(key::kProxyServerMode
,
319 POLICY_LEVEL_MANDATORY
,
321 new base::FundamentalValue(i
),
323 UpdateProviderPolicy(policy
);
324 const base::Value
* value
= NULL
;
325 EXPECT_FALSE(store_
->GetValue(prefs::kProxy
, &value
));
329 } // namespace policy