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/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"
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(proxy_config::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
,
81 new base::StringValue("http://chromium.org/override"),
83 policy
.Set(key::kProxyServer
,
84 POLICY_LEVEL_MANDATORY
,
87 new base::StringValue("chromium.org"),
90 key::kProxyServerMode
,
91 POLICY_LEVEL_MANDATORY
,
94 new base::FundamentalValue(
95 ProxyPolicyHandler::PROXY_MANUALLY_CONFIGURED_PROXY_SERVER_MODE
),
97 UpdateProviderPolicy(policy
);
99 VerifyProxyPrefs("chromium.org",
101 "http://chromium.org/override",
102 ProxyPrefs::MODE_FIXED_SERVERS
);
105 TEST_F(ProxyPolicyHandlerTest
, ManualOptionsReversedApplyOrder
) {
108 key::kProxyServerMode
,
109 POLICY_LEVEL_MANDATORY
,
112 new base::FundamentalValue(
113 ProxyPolicyHandler::PROXY_MANUALLY_CONFIGURED_PROXY_SERVER_MODE
),
115 policy
.Set(key::kProxyBypassList
,
116 POLICY_LEVEL_MANDATORY
,
119 new base::StringValue("http://chromium.org/override"),
121 policy
.Set(key::kProxyServer
,
122 POLICY_LEVEL_MANDATORY
,
125 new base::StringValue("chromium.org"),
127 UpdateProviderPolicy(policy
);
129 VerifyProxyPrefs("chromium.org",
131 "http://chromium.org/override",
132 ProxyPrefs::MODE_FIXED_SERVERS
);
135 TEST_F(ProxyPolicyHandlerTest
, ManualOptionsInvalid
) {
138 key::kProxyServerMode
,
139 POLICY_LEVEL_MANDATORY
,
142 new base::FundamentalValue(
143 ProxyPolicyHandler::PROXY_MANUALLY_CONFIGURED_PROXY_SERVER_MODE
),
145 UpdateProviderPolicy(policy
);
147 const base::Value
* value
= NULL
;
148 EXPECT_FALSE(store_
->GetValue(proxy_config::prefs::kProxy
, &value
));
151 TEST_F(ProxyPolicyHandlerTest
, NoProxyServerMode
) {
153 policy
.Set(key::kProxyServerMode
,
154 POLICY_LEVEL_MANDATORY
,
157 new base::FundamentalValue(ProxyPolicyHandler::PROXY_SERVER_MODE
),
159 UpdateProviderPolicy(policy
);
161 std::string(), std::string(), std::string(), ProxyPrefs::MODE_DIRECT
);
164 TEST_F(ProxyPolicyHandlerTest
, NoProxyModeName
) {
166 policy
.Set(key::kProxyMode
,
167 POLICY_LEVEL_MANDATORY
,
170 new base::StringValue(ProxyPrefs::kDirectProxyModeName
),
172 UpdateProviderPolicy(policy
);
174 std::string(), std::string(), std::string(), ProxyPrefs::MODE_DIRECT
);
177 TEST_F(ProxyPolicyHandlerTest
, AutoDetectProxyServerMode
) {
179 policy
.Set(key::kProxyServerMode
,
180 POLICY_LEVEL_MANDATORY
,
183 new base::FundamentalValue(
184 ProxyPolicyHandler::PROXY_AUTO_DETECT_PROXY_SERVER_MODE
),
186 UpdateProviderPolicy(policy
);
187 VerifyProxyPrefs(std::string(),
190 ProxyPrefs::MODE_AUTO_DETECT
);
193 TEST_F(ProxyPolicyHandlerTest
, AutoDetectProxyModeName
) {
195 policy
.Set(key::kProxyMode
,
196 POLICY_LEVEL_MANDATORY
,
199 new base::StringValue(ProxyPrefs::kAutoDetectProxyModeName
),
201 UpdateProviderPolicy(policy
);
202 VerifyProxyPrefs(std::string(),
205 ProxyPrefs::MODE_AUTO_DETECT
);
208 TEST_F(ProxyPolicyHandlerTest
, PacScriptProxyMode
) {
210 policy
.Set(key::kProxyPacUrl
,
211 POLICY_LEVEL_MANDATORY
,
214 new base::StringValue("http://short.org/proxy.pac"),
216 policy
.Set(key::kProxyMode
,
217 POLICY_LEVEL_MANDATORY
,
220 new base::StringValue(ProxyPrefs::kPacScriptProxyModeName
),
222 UpdateProviderPolicy(policy
);
223 VerifyProxyPrefs(std::string(),
224 "http://short.org/proxy.pac",
226 ProxyPrefs::MODE_PAC_SCRIPT
);
229 TEST_F(ProxyPolicyHandlerTest
, PacScriptProxyModeInvalid
) {
231 policy
.Set(key::kProxyMode
,
232 POLICY_LEVEL_MANDATORY
,
235 new base::StringValue(ProxyPrefs::kPacScriptProxyModeName
),
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
) {
246 policy
.Set(key::kProxyServer
,
247 POLICY_LEVEL_MANDATORY
,
250 new base::StringValue(std::string()),
252 policy
.Set(key::kProxyPacUrl
,
253 POLICY_LEVEL_MANDATORY
,
256 new base::StringValue("http://short.org/proxy.pac"),
258 policy
.Set(key::kProxyMode
,
259 POLICY_LEVEL_MANDATORY
,
262 new base::StringValue(ProxyPrefs::kPacScriptProxyModeName
),
264 UpdateProviderPolicy(policy
);
265 VerifyProxyPrefs(std::string(),
266 "http://short.org/proxy.pac",
268 ProxyPrefs::MODE_PAC_SCRIPT
);
271 TEST_F(ProxyPolicyHandlerTest
, UseSystemProxyServerMode
) {
273 policy
.Set(key::kProxyServerMode
,
274 POLICY_LEVEL_MANDATORY
,
277 new base::FundamentalValue(
278 ProxyPolicyHandler::PROXY_USE_SYSTEM_PROXY_SERVER_MODE
),
280 UpdateProviderPolicy(policy
);
282 std::string(), std::string(), std::string(), ProxyPrefs::MODE_SYSTEM
);
285 TEST_F(ProxyPolicyHandlerTest
, UseSystemProxyMode
) {
287 policy
.Set(key::kProxyMode
,
288 POLICY_LEVEL_MANDATORY
,
291 new base::StringValue(ProxyPrefs::kSystemProxyModeName
),
293 UpdateProviderPolicy(policy
);
295 std::string(), std::string(), std::string(), ProxyPrefs::MODE_SYSTEM
);
298 TEST_F(ProxyPolicyHandlerTest
,
299 ProxyModeOverridesProxyServerMode
) {
301 policy
.Set(key::kProxyServerMode
,
302 POLICY_LEVEL_MANDATORY
,
305 new base::FundamentalValue(ProxyPolicyHandler::PROXY_SERVER_MODE
),
307 policy
.Set(key::kProxyMode
,
308 POLICY_LEVEL_MANDATORY
,
311 new base::StringValue(ProxyPrefs::kAutoDetectProxyModeName
),
313 UpdateProviderPolicy(policy
);
314 VerifyProxyPrefs(std::string(),
317 ProxyPrefs::MODE_AUTO_DETECT
);
320 TEST_F(ProxyPolicyHandlerTest
, ProxyInvalid
) {
321 // No mode expects all three parameters being set.
323 policy
.Set(key::kProxyPacUrl
,
324 POLICY_LEVEL_MANDATORY
,
327 new base::StringValue("http://short.org/proxy.pac"),
329 policy
.Set(key::kProxyBypassList
,
330 POLICY_LEVEL_MANDATORY
,
333 new base::StringValue("http://chromium.org/override"),
335 policy
.Set(key::kProxyServer
,
336 POLICY_LEVEL_MANDATORY
,
339 new base::StringValue("chromium.org"),
341 for (int i
= 0; i
< ProxyPolicyHandler::MODE_COUNT
; ++i
) {
342 policy
.Set(key::kProxyServerMode
,
343 POLICY_LEVEL_MANDATORY
,
346 new base::FundamentalValue(i
),
348 UpdateProviderPolicy(policy
);
349 const base::Value
* value
= NULL
;
350 EXPECT_FALSE(store_
->GetValue(proxy_config::prefs::kProxy
, &value
));
354 } // namespace policy