[ExtensionToolbarMac] Restrict action button drags to the container's bounds
[chromium-blink-merge.git] / chrome / browser / net / proxy_policy_handler_unittest.cc
bloba1818a14592adf23cd70d10478340dd96c122177
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/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"
19 namespace policy {
21 // Test cases for the proxy policy settings.
22 class ProxyPolicyHandlerTest
23 : public ConfigurationPolicyPrefStoreTest {
24 public:
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.
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,
78 POLICY_LEVEL_MANDATORY,
79 POLICY_SCOPE_USER,
80 new base::StringValue("http://chromium.org/override"),
81 NULL);
82 policy.Set(key::kProxyServer,
83 POLICY_LEVEL_MANDATORY,
84 POLICY_SCOPE_USER,
85 new base::StringValue("chromium.org"),
86 NULL);
87 policy.Set(
88 key::kProxyServerMode,
89 POLICY_LEVEL_MANDATORY,
90 POLICY_SCOPE_USER,
91 new base::FundamentalValue(
92 ProxyPolicyHandler::PROXY_MANUALLY_CONFIGURED_PROXY_SERVER_MODE),
93 NULL);
94 UpdateProviderPolicy(policy);
96 VerifyProxyPrefs("chromium.org",
97 std::string(),
98 "http://chromium.org/override",
99 ProxyPrefs::MODE_FIXED_SERVERS);
102 TEST_F(ProxyPolicyHandlerTest, ManualOptionsReversedApplyOrder) {
103 PolicyMap policy;
104 policy.Set(
105 key::kProxyServerMode,
106 POLICY_LEVEL_MANDATORY,
107 POLICY_SCOPE_USER,
108 new base::FundamentalValue(
109 ProxyPolicyHandler::PROXY_MANUALLY_CONFIGURED_PROXY_SERVER_MODE),
110 NULL);
111 policy.Set(key::kProxyBypassList,
112 POLICY_LEVEL_MANDATORY,
113 POLICY_SCOPE_USER,
114 new base::StringValue("http://chromium.org/override"),
115 NULL);
116 policy.Set(key::kProxyServer,
117 POLICY_LEVEL_MANDATORY,
118 POLICY_SCOPE_USER,
119 new base::StringValue("chromium.org"),
120 NULL);
121 UpdateProviderPolicy(policy);
123 VerifyProxyPrefs("chromium.org",
124 std::string(),
125 "http://chromium.org/override",
126 ProxyPrefs::MODE_FIXED_SERVERS);
129 TEST_F(ProxyPolicyHandlerTest, ManualOptionsInvalid) {
130 PolicyMap policy;
131 policy.Set(
132 key::kProxyServerMode,
133 POLICY_LEVEL_MANDATORY,
134 POLICY_SCOPE_USER,
135 new base::FundamentalValue(
136 ProxyPolicyHandler::PROXY_MANUALLY_CONFIGURED_PROXY_SERVER_MODE),
137 NULL);
138 UpdateProviderPolicy(policy);
140 const base::Value* value = NULL;
141 EXPECT_FALSE(store_->GetValue(prefs::kProxy, &value));
144 TEST_F(ProxyPolicyHandlerTest, NoProxyServerMode) {
145 PolicyMap policy;
146 policy.Set(key::kProxyServerMode,
147 POLICY_LEVEL_MANDATORY,
148 POLICY_SCOPE_USER,
149 new base::FundamentalValue(ProxyPolicyHandler::PROXY_SERVER_MODE),
150 NULL);
151 UpdateProviderPolicy(policy);
152 VerifyProxyPrefs(
153 std::string(), std::string(), std::string(), ProxyPrefs::MODE_DIRECT);
156 TEST_F(ProxyPolicyHandlerTest, NoProxyModeName) {
157 PolicyMap policy;
158 policy.Set(key::kProxyMode,
159 POLICY_LEVEL_MANDATORY,
160 POLICY_SCOPE_USER,
161 new base::StringValue(ProxyPrefs::kDirectProxyModeName),
162 NULL);
163 UpdateProviderPolicy(policy);
164 VerifyProxyPrefs(
165 std::string(), std::string(), std::string(), ProxyPrefs::MODE_DIRECT);
168 TEST_F(ProxyPolicyHandlerTest, AutoDetectProxyServerMode) {
169 PolicyMap policy;
170 policy.Set(key::kProxyServerMode,
171 POLICY_LEVEL_MANDATORY,
172 POLICY_SCOPE_USER,
173 new base::FundamentalValue(
174 ProxyPolicyHandler::PROXY_AUTO_DETECT_PROXY_SERVER_MODE),
175 NULL);
176 UpdateProviderPolicy(policy);
177 VerifyProxyPrefs(std::string(),
178 std::string(),
179 std::string(),
180 ProxyPrefs::MODE_AUTO_DETECT);
183 TEST_F(ProxyPolicyHandlerTest, AutoDetectProxyModeName) {
184 PolicyMap policy;
185 policy.Set(key::kProxyMode,
186 POLICY_LEVEL_MANDATORY,
187 POLICY_SCOPE_USER,
188 new base::StringValue(ProxyPrefs::kAutoDetectProxyModeName),
189 NULL);
190 UpdateProviderPolicy(policy);
191 VerifyProxyPrefs(std::string(),
192 std::string(),
193 std::string(),
194 ProxyPrefs::MODE_AUTO_DETECT);
197 TEST_F(ProxyPolicyHandlerTest, PacScriptProxyMode) {
198 PolicyMap policy;
199 policy.Set(key::kProxyPacUrl,
200 POLICY_LEVEL_MANDATORY,
201 POLICY_SCOPE_USER,
202 new base::StringValue("http://short.org/proxy.pac"),
203 NULL);
204 policy.Set(key::kProxyMode,
205 POLICY_LEVEL_MANDATORY,
206 POLICY_SCOPE_USER,
207 new base::StringValue(ProxyPrefs::kPacScriptProxyModeName),
208 NULL);
209 UpdateProviderPolicy(policy);
210 VerifyProxyPrefs(std::string(),
211 "http://short.org/proxy.pac",
212 std::string(),
213 ProxyPrefs::MODE_PAC_SCRIPT);
216 TEST_F(ProxyPolicyHandlerTest, PacScriptProxyModeInvalid) {
217 PolicyMap policy;
218 policy.Set(key::kProxyMode,
219 POLICY_LEVEL_MANDATORY,
220 POLICY_SCOPE_USER,
221 new base::StringValue(ProxyPrefs::kPacScriptProxyModeName),
222 NULL);
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) {
231 PolicyMap policy;
232 policy.Set(key::kProxyServer,
233 POLICY_LEVEL_MANDATORY,
234 POLICY_SCOPE_USER,
235 new base::StringValue(std::string()),
236 NULL);
237 policy.Set(key::kProxyPacUrl,
238 POLICY_LEVEL_MANDATORY,
239 POLICY_SCOPE_USER,
240 new base::StringValue("http://short.org/proxy.pac"),
241 NULL);
242 policy.Set(key::kProxyMode,
243 POLICY_LEVEL_MANDATORY,
244 POLICY_SCOPE_USER,
245 new base::StringValue(ProxyPrefs::kPacScriptProxyModeName),
246 NULL);
247 UpdateProviderPolicy(policy);
248 VerifyProxyPrefs(std::string(),
249 "http://short.org/proxy.pac",
250 std::string(),
251 ProxyPrefs::MODE_PAC_SCRIPT);
254 TEST_F(ProxyPolicyHandlerTest, UseSystemProxyServerMode) {
255 PolicyMap policy;
256 policy.Set(key::kProxyServerMode,
257 POLICY_LEVEL_MANDATORY,
258 POLICY_SCOPE_USER,
259 new base::FundamentalValue(
260 ProxyPolicyHandler::PROXY_USE_SYSTEM_PROXY_SERVER_MODE),
261 NULL);
262 UpdateProviderPolicy(policy);
263 VerifyProxyPrefs(
264 std::string(), std::string(), std::string(), ProxyPrefs::MODE_SYSTEM);
267 TEST_F(ProxyPolicyHandlerTest, UseSystemProxyMode) {
268 PolicyMap policy;
269 policy.Set(key::kProxyMode,
270 POLICY_LEVEL_MANDATORY,
271 POLICY_SCOPE_USER,
272 new base::StringValue(ProxyPrefs::kSystemProxyModeName),
273 NULL);
274 UpdateProviderPolicy(policy);
275 VerifyProxyPrefs(
276 std::string(), std::string(), std::string(), ProxyPrefs::MODE_SYSTEM);
279 TEST_F(ProxyPolicyHandlerTest,
280 ProxyModeOverridesProxyServerMode) {
281 PolicyMap policy;
282 policy.Set(key::kProxyServerMode,
283 POLICY_LEVEL_MANDATORY,
284 POLICY_SCOPE_USER,
285 new base::FundamentalValue(ProxyPolicyHandler::PROXY_SERVER_MODE),
286 NULL);
287 policy.Set(key::kProxyMode,
288 POLICY_LEVEL_MANDATORY,
289 POLICY_SCOPE_USER,
290 new base::StringValue(ProxyPrefs::kAutoDetectProxyModeName),
291 NULL);
292 UpdateProviderPolicy(policy);
293 VerifyProxyPrefs(std::string(),
294 std::string(),
295 std::string(),
296 ProxyPrefs::MODE_AUTO_DETECT);
299 TEST_F(ProxyPolicyHandlerTest, ProxyInvalid) {
300 // No mode expects all three parameters being set.
301 PolicyMap policy;
302 policy.Set(key::kProxyPacUrl,
303 POLICY_LEVEL_MANDATORY,
304 POLICY_SCOPE_USER,
305 new base::StringValue("http://short.org/proxy.pac"),
306 NULL);
307 policy.Set(key::kProxyBypassList,
308 POLICY_LEVEL_MANDATORY,
309 POLICY_SCOPE_USER,
310 new base::StringValue("http://chromium.org/override"),
311 NULL);
312 policy.Set(key::kProxyServer,
313 POLICY_LEVEL_MANDATORY,
314 POLICY_SCOPE_USER,
315 new base::StringValue("chromium.org"),
316 NULL);
317 for (int i = 0; i < ProxyPolicyHandler::MODE_COUNT; ++i) {
318 policy.Set(key::kProxyServerMode,
319 POLICY_LEVEL_MANDATORY,
320 POLICY_SCOPE_USER,
321 new base::FundamentalValue(i),
322 NULL);
323 UpdateProviderPolicy(policy);
324 const base::Value* value = NULL;
325 EXPECT_FALSE(store_->GetValue(prefs::kProxy, &value));
329 } // namespace policy