Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / net / proxy_policy_handler_unittest.cc
blob6112f126a38a5cb45edf072979f2c31e8482cb69
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.
5 #include <string>
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"
19 namespace policy {
21 // Test cases for the proxy policy settings.
22 class ProxyPolicyHandlerTest
23 : public ConfigurationPolicyPrefStoreTest {
24 public:
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.
32 store_ = NULL;
33 policy_service_.reset(new PolicyServiceImpl(providers_));
34 store_ = new ConfigurationPolicyPrefStore(
35 policy_service_.get(), &handler_list_, POLICY_LEVEL_MANDATORY);
38 protected:
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));
50 std::string s;
51 if (expected_proxy_server.empty()) {
52 EXPECT_FALSE(dict.GetProxyServer(&s));
53 } else {
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));
59 } else {
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));
65 } else {
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) {
76 PolicyMap policy;
77 policy.Set(key::kProxyBypassList, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
78 base::Value::CreateStringValue("http://chromium.org/override"),
79 NULL);
80 policy.Set(key::kProxyServer, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
81 base::Value::CreateStringValue("chromium.org"), NULL);
82 policy.Set(
83 key::kProxyServerMode, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
84 base::Value::CreateIntegerValue(
85 ProxyPolicyHandler::PROXY_MANUALLY_CONFIGURED_PROXY_SERVER_MODE),
86 NULL);
87 UpdateProviderPolicy(policy);
89 VerifyProxyPrefs("chromium.org",
90 std::string(),
91 "http://chromium.org/override",
92 ProxyPrefs::MODE_FIXED_SERVERS);
95 TEST_F(ProxyPolicyHandlerTest, ManualOptionsReversedApplyOrder) {
96 PolicyMap policy;
97 policy.Set(
98 key::kProxyServerMode, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
99 base::Value::CreateIntegerValue(
100 ProxyPolicyHandler::PROXY_MANUALLY_CONFIGURED_PROXY_SERVER_MODE),
101 NULL);
102 policy.Set(key::kProxyBypassList, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
103 base::Value::CreateStringValue("http://chromium.org/override"),
104 NULL);
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",
110 std::string(),
111 "http://chromium.org/override",
112 ProxyPrefs::MODE_FIXED_SERVERS);
115 TEST_F(ProxyPolicyHandlerTest, ManualOptionsInvalid) {
116 PolicyMap policy;
117 policy.Set(
118 key::kProxyServerMode, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
119 base::Value::CreateIntegerValue(
120 ProxyPolicyHandler::PROXY_MANUALLY_CONFIGURED_PROXY_SERVER_MODE),
121 NULL);
122 UpdateProviderPolicy(policy);
124 const base::Value* value = NULL;
125 EXPECT_FALSE(store_->GetValue(prefs::kProxy, &value));
128 TEST_F(ProxyPolicyHandlerTest, NoProxyServerMode) {
129 PolicyMap policy;
130 policy.Set(key::kProxyServerMode, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
131 base::Value::CreateIntegerValue(
132 ProxyPolicyHandler::PROXY_SERVER_MODE),
133 NULL);
134 UpdateProviderPolicy(policy);
135 VerifyProxyPrefs(
136 std::string(), std::string(), std::string(), ProxyPrefs::MODE_DIRECT);
139 TEST_F(ProxyPolicyHandlerTest, NoProxyModeName) {
140 PolicyMap policy;
141 policy.Set(key::kProxyMode, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
142 base::Value::CreateStringValue(ProxyPrefs::kDirectProxyModeName),
143 NULL);
144 UpdateProviderPolicy(policy);
145 VerifyProxyPrefs(
146 std::string(), std::string(), std::string(), ProxyPrefs::MODE_DIRECT);
149 TEST_F(ProxyPolicyHandlerTest, AutoDetectProxyServerMode) {
150 PolicyMap policy;
151 policy.Set(
152 key::kProxyServerMode, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
153 base::Value::CreateIntegerValue(
154 ProxyPolicyHandler::PROXY_AUTO_DETECT_PROXY_SERVER_MODE),
155 NULL);
156 UpdateProviderPolicy(policy);
157 VerifyProxyPrefs(std::string(),
158 std::string(),
159 std::string(),
160 ProxyPrefs::MODE_AUTO_DETECT);
163 TEST_F(ProxyPolicyHandlerTest, AutoDetectProxyModeName) {
164 PolicyMap policy;
165 policy.Set(key::kProxyMode, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
166 base::Value::CreateStringValue(
167 ProxyPrefs::kAutoDetectProxyModeName),
168 NULL);
169 UpdateProviderPolicy(policy);
170 VerifyProxyPrefs(std::string(),
171 std::string(),
172 std::string(),
173 ProxyPrefs::MODE_AUTO_DETECT);
176 TEST_F(ProxyPolicyHandlerTest, PacScriptProxyMode) {
177 PolicyMap policy;
178 policy.Set(key::kProxyPacUrl, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
179 base::Value::CreateStringValue("http://short.org/proxy.pac"),
180 NULL);
181 policy.Set(key::kProxyMode, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
182 base::Value::CreateStringValue(
183 ProxyPrefs::kPacScriptProxyModeName),
184 NULL);
185 UpdateProviderPolicy(policy);
186 VerifyProxyPrefs(std::string(),
187 "http://short.org/proxy.pac",
188 std::string(),
189 ProxyPrefs::MODE_PAC_SCRIPT);
192 TEST_F(ProxyPolicyHandlerTest, PacScriptProxyModeInvalid) {
193 PolicyMap policy;
194 policy.Set(key::kProxyMode, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
195 base::Value::CreateStringValue(
196 ProxyPrefs::kPacScriptProxyModeName),
197 NULL);
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) {
206 PolicyMap policy;
207 policy.Set(key::kProxyServer,
208 POLICY_LEVEL_MANDATORY,
209 POLICY_SCOPE_USER,
210 base::Value::CreateStringValue(std::string()),
211 NULL);
212 policy.Set(key::kProxyPacUrl,
213 POLICY_LEVEL_MANDATORY,
214 POLICY_SCOPE_USER,
215 base::Value::CreateStringValue("http://short.org/proxy.pac"),
216 NULL);
217 policy.Set(key::kProxyMode, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
218 base::Value::CreateStringValue(
219 ProxyPrefs::kPacScriptProxyModeName),
220 NULL);
221 UpdateProviderPolicy(policy);
222 VerifyProxyPrefs(std::string(),
223 "http://short.org/proxy.pac",
224 std::string(),
225 ProxyPrefs::MODE_PAC_SCRIPT);
228 TEST_F(ProxyPolicyHandlerTest, UseSystemProxyServerMode) {
229 PolicyMap policy;
230 policy.Set(key::kProxyServerMode, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
231 base::Value::CreateIntegerValue(
232 ProxyPolicyHandler::PROXY_USE_SYSTEM_PROXY_SERVER_MODE),
233 NULL);
234 UpdateProviderPolicy(policy);
235 VerifyProxyPrefs(
236 std::string(), std::string(), std::string(), ProxyPrefs::MODE_SYSTEM);
239 TEST_F(ProxyPolicyHandlerTest, UseSystemProxyMode) {
240 PolicyMap policy;
241 policy.Set(key::kProxyMode, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
242 base::Value::CreateStringValue(ProxyPrefs::kSystemProxyModeName),
243 NULL);
244 UpdateProviderPolicy(policy);
245 VerifyProxyPrefs(
246 std::string(), std::string(), std::string(), ProxyPrefs::MODE_SYSTEM);
249 TEST_F(ProxyPolicyHandlerTest,
250 ProxyModeOverridesProxyServerMode) {
251 PolicyMap policy;
252 policy.Set(key::kProxyServerMode, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
253 base::Value::CreateIntegerValue(
254 ProxyPolicyHandler::PROXY_SERVER_MODE),
255 NULL);
256 policy.Set(key::kProxyMode, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
257 base::Value::CreateStringValue(
258 ProxyPrefs::kAutoDetectProxyModeName),
259 NULL);
260 UpdateProviderPolicy(policy);
261 VerifyProxyPrefs(std::string(),
262 std::string(),
263 std::string(),
264 ProxyPrefs::MODE_AUTO_DETECT);
267 TEST_F(ProxyPolicyHandlerTest, ProxyInvalid) {
268 // No mode expects all three parameters being set.
269 PolicyMap policy;
270 policy.Set(key::kProxyPacUrl, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
271 base::Value::CreateStringValue("http://short.org/proxy.pac"),
272 NULL);
273 policy.Set(key::kProxyBypassList, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
274 base::Value::CreateStringValue("http://chromium.org/override"),
275 NULL);
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