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 "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/proxy_config/proxy_config_dictionary.h"
14 #include "components/proxy_config/proxy_config_pref_names.h"
15 #include "policy/policy_constants.h"
16 #include "testing/gtest/include/gtest/gtest.h"
20 // Test cases for the proxy policy settings.
21 class ProxyPolicyHandlerTest
22 : public ConfigurationPolicyPrefStoreTest
{
24 void SetUp() override
{
25 ConfigurationPolicyPrefStoreTest::SetUp();
26 handler_list_
.AddHandler(
27 make_scoped_ptr
<ConfigurationPolicyHandler
>(new ProxyPolicyHandler
));
28 // Reset the PolicyServiceImpl to one that has the policy fixup
29 // preprocessor. The previous store must be nulled out first so that it
30 // removes itself from the service's observer list.
32 policy_service_
.reset(new PolicyServiceImpl(providers_
));
33 store_
= new ConfigurationPolicyPrefStore(
34 policy_service_
.get(), &handler_list_
, POLICY_LEVEL_MANDATORY
);
38 // Verify that all the proxy prefs are set to the specified expected values.
39 void VerifyProxyPrefs(
40 const std::string
& expected_proxy_server
,
41 const std::string
& expected_proxy_pac_url
,
42 const std::string
& expected_proxy_bypass_list
,
43 const ProxyPrefs::ProxyMode
& expected_proxy_mode
) {
44 const base::Value
* value
= NULL
;
45 ASSERT_TRUE(store_
->GetValue(proxy_config::prefs::kProxy
, &value
));
46 ASSERT_EQ(base::Value::TYPE_DICTIONARY
, value
->GetType());
47 ProxyConfigDictionary
dict(
48 static_cast<const base::DictionaryValue
*>(value
));
50 if (expected_proxy_server
.empty()) {
51 EXPECT_FALSE(dict
.GetProxyServer(&s
));
53 ASSERT_TRUE(dict
.GetProxyServer(&s
));
54 EXPECT_EQ(expected_proxy_server
, s
);
56 if (expected_proxy_pac_url
.empty()) {
57 EXPECT_FALSE(dict
.GetPacUrl(&s
));
59 ASSERT_TRUE(dict
.GetPacUrl(&s
));
60 EXPECT_EQ(expected_proxy_pac_url
, s
);
62 if (expected_proxy_bypass_list
.empty()) {
63 EXPECT_FALSE(dict
.GetBypassList(&s
));
65 ASSERT_TRUE(dict
.GetBypassList(&s
));
66 EXPECT_EQ(expected_proxy_bypass_list
, s
);
68 ProxyPrefs::ProxyMode mode
;
69 ASSERT_TRUE(dict
.GetMode(&mode
));
70 EXPECT_EQ(expected_proxy_mode
, mode
);
74 TEST_F(ProxyPolicyHandlerTest
, ManualOptions
) {
76 policy
.Set(key::kProxyBypassList
,
77 POLICY_LEVEL_MANDATORY
,
79 new base::StringValue("http://chromium.org/override"),
81 policy
.Set(key::kProxyServer
,
82 POLICY_LEVEL_MANDATORY
,
84 new base::StringValue("chromium.org"),
87 key::kProxyServerMode
,
88 POLICY_LEVEL_MANDATORY
,
90 new base::FundamentalValue(
91 ProxyPolicyHandler::PROXY_MANUALLY_CONFIGURED_PROXY_SERVER_MODE
),
93 UpdateProviderPolicy(policy
);
95 VerifyProxyPrefs("chromium.org",
97 "http://chromium.org/override",
98 ProxyPrefs::MODE_FIXED_SERVERS
);
101 TEST_F(ProxyPolicyHandlerTest
, ManualOptionsReversedApplyOrder
) {
104 key::kProxyServerMode
,
105 POLICY_LEVEL_MANDATORY
,
107 new base::FundamentalValue(
108 ProxyPolicyHandler::PROXY_MANUALLY_CONFIGURED_PROXY_SERVER_MODE
),
110 policy
.Set(key::kProxyBypassList
,
111 POLICY_LEVEL_MANDATORY
,
113 new base::StringValue("http://chromium.org/override"),
115 policy
.Set(key::kProxyServer
,
116 POLICY_LEVEL_MANDATORY
,
118 new base::StringValue("chromium.org"),
120 UpdateProviderPolicy(policy
);
122 VerifyProxyPrefs("chromium.org",
124 "http://chromium.org/override",
125 ProxyPrefs::MODE_FIXED_SERVERS
);
128 TEST_F(ProxyPolicyHandlerTest
, ManualOptionsInvalid
) {
131 key::kProxyServerMode
,
132 POLICY_LEVEL_MANDATORY
,
134 new base::FundamentalValue(
135 ProxyPolicyHandler::PROXY_MANUALLY_CONFIGURED_PROXY_SERVER_MODE
),
137 UpdateProviderPolicy(policy
);
139 const base::Value
* value
= NULL
;
140 EXPECT_FALSE(store_
->GetValue(proxy_config::prefs::kProxy
, &value
));
143 TEST_F(ProxyPolicyHandlerTest
, NoProxyServerMode
) {
145 policy
.Set(key::kProxyServerMode
,
146 POLICY_LEVEL_MANDATORY
,
148 new base::FundamentalValue(ProxyPolicyHandler::PROXY_SERVER_MODE
),
150 UpdateProviderPolicy(policy
);
152 std::string(), std::string(), std::string(), ProxyPrefs::MODE_DIRECT
);
155 TEST_F(ProxyPolicyHandlerTest
, NoProxyModeName
) {
157 policy
.Set(key::kProxyMode
,
158 POLICY_LEVEL_MANDATORY
,
160 new base::StringValue(ProxyPrefs::kDirectProxyModeName
),
162 UpdateProviderPolicy(policy
);
164 std::string(), std::string(), std::string(), ProxyPrefs::MODE_DIRECT
);
167 TEST_F(ProxyPolicyHandlerTest
, AutoDetectProxyServerMode
) {
169 policy
.Set(key::kProxyServerMode
,
170 POLICY_LEVEL_MANDATORY
,
172 new base::FundamentalValue(
173 ProxyPolicyHandler::PROXY_AUTO_DETECT_PROXY_SERVER_MODE
),
175 UpdateProviderPolicy(policy
);
176 VerifyProxyPrefs(std::string(),
179 ProxyPrefs::MODE_AUTO_DETECT
);
182 TEST_F(ProxyPolicyHandlerTest
, AutoDetectProxyModeName
) {
184 policy
.Set(key::kProxyMode
,
185 POLICY_LEVEL_MANDATORY
,
187 new base::StringValue(ProxyPrefs::kAutoDetectProxyModeName
),
189 UpdateProviderPolicy(policy
);
190 VerifyProxyPrefs(std::string(),
193 ProxyPrefs::MODE_AUTO_DETECT
);
196 TEST_F(ProxyPolicyHandlerTest
, PacScriptProxyMode
) {
198 policy
.Set(key::kProxyPacUrl
,
199 POLICY_LEVEL_MANDATORY
,
201 new base::StringValue("http://short.org/proxy.pac"),
203 policy
.Set(key::kProxyMode
,
204 POLICY_LEVEL_MANDATORY
,
206 new base::StringValue(ProxyPrefs::kPacScriptProxyModeName
),
208 UpdateProviderPolicy(policy
);
209 VerifyProxyPrefs(std::string(),
210 "http://short.org/proxy.pac",
212 ProxyPrefs::MODE_PAC_SCRIPT
);
215 TEST_F(ProxyPolicyHandlerTest
, PacScriptProxyModeInvalid
) {
217 policy
.Set(key::kProxyMode
,
218 POLICY_LEVEL_MANDATORY
,
220 new base::StringValue(ProxyPrefs::kPacScriptProxyModeName
),
222 UpdateProviderPolicy(policy
);
223 const base::Value
* value
= NULL
;
224 EXPECT_FALSE(store_
->GetValue(proxy_config::prefs::kProxy
, &value
));
227 // Regression test for http://crbug.com/78016, CPanel returns empty strings
228 // for unset properties.
229 TEST_F(ProxyPolicyHandlerTest
, PacScriptProxyModeBug78016
) {
231 policy
.Set(key::kProxyServer
,
232 POLICY_LEVEL_MANDATORY
,
234 new base::StringValue(std::string()),
236 policy
.Set(key::kProxyPacUrl
,
237 POLICY_LEVEL_MANDATORY
,
239 new base::StringValue("http://short.org/proxy.pac"),
241 policy
.Set(key::kProxyMode
,
242 POLICY_LEVEL_MANDATORY
,
244 new base::StringValue(ProxyPrefs::kPacScriptProxyModeName
),
246 UpdateProviderPolicy(policy
);
247 VerifyProxyPrefs(std::string(),
248 "http://short.org/proxy.pac",
250 ProxyPrefs::MODE_PAC_SCRIPT
);
253 TEST_F(ProxyPolicyHandlerTest
, UseSystemProxyServerMode
) {
255 policy
.Set(key::kProxyServerMode
,
256 POLICY_LEVEL_MANDATORY
,
258 new base::FundamentalValue(
259 ProxyPolicyHandler::PROXY_USE_SYSTEM_PROXY_SERVER_MODE
),
261 UpdateProviderPolicy(policy
);
263 std::string(), std::string(), std::string(), ProxyPrefs::MODE_SYSTEM
);
266 TEST_F(ProxyPolicyHandlerTest
, UseSystemProxyMode
) {
268 policy
.Set(key::kProxyMode
,
269 POLICY_LEVEL_MANDATORY
,
271 new base::StringValue(ProxyPrefs::kSystemProxyModeName
),
273 UpdateProviderPolicy(policy
);
275 std::string(), std::string(), std::string(), ProxyPrefs::MODE_SYSTEM
);
278 TEST_F(ProxyPolicyHandlerTest
,
279 ProxyModeOverridesProxyServerMode
) {
281 policy
.Set(key::kProxyServerMode
,
282 POLICY_LEVEL_MANDATORY
,
284 new base::FundamentalValue(ProxyPolicyHandler::PROXY_SERVER_MODE
),
286 policy
.Set(key::kProxyMode
,
287 POLICY_LEVEL_MANDATORY
,
289 new base::StringValue(ProxyPrefs::kAutoDetectProxyModeName
),
291 UpdateProviderPolicy(policy
);
292 VerifyProxyPrefs(std::string(),
295 ProxyPrefs::MODE_AUTO_DETECT
);
298 TEST_F(ProxyPolicyHandlerTest
, ProxyInvalid
) {
299 // No mode expects all three parameters being set.
301 policy
.Set(key::kProxyPacUrl
,
302 POLICY_LEVEL_MANDATORY
,
304 new base::StringValue("http://short.org/proxy.pac"),
306 policy
.Set(key::kProxyBypassList
,
307 POLICY_LEVEL_MANDATORY
,
309 new base::StringValue("http://chromium.org/override"),
311 policy
.Set(key::kProxyServer
,
312 POLICY_LEVEL_MANDATORY
,
314 new base::StringValue("chromium.org"),
316 for (int i
= 0; i
< ProxyPolicyHandler::MODE_COUNT
; ++i
) {
317 policy
.Set(key::kProxyServerMode
,
318 POLICY_LEVEL_MANDATORY
,
320 new base::FundamentalValue(i
),
322 UpdateProviderPolicy(policy
);
323 const base::Value
* value
= NULL
;
324 EXPECT_FALSE(store_
->GetValue(proxy_config::prefs::kProxy
, &value
));
328 } // namespace policy