Only grant permissions to new extensions from sync if they have the expected version
[chromium-blink-merge.git] / chrome / browser / net / proxy_policy_handler_unittest.cc
blob570bf01dc45a97169b7ada77ea17aa8f20d4b135
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 "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"
18 namespace policy {
20 // Test cases for the proxy policy settings.
21 class ProxyPolicyHandlerTest
22 : public ConfigurationPolicyPrefStoreTest {
23 public:
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.
31 store_ = NULL;
32 policy_service_.reset(new PolicyServiceImpl(providers_));
33 store_ = new ConfigurationPolicyPrefStore(
34 policy_service_.get(), &handler_list_, POLICY_LEVEL_MANDATORY);
37 protected:
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));
49 std::string s;
50 if (expected_proxy_server.empty()) {
51 EXPECT_FALSE(dict.GetProxyServer(&s));
52 } else {
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));
58 } else {
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));
64 } else {
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) {
75 PolicyMap policy;
76 policy.Set(key::kProxyBypassList,
77 POLICY_LEVEL_MANDATORY,
78 POLICY_SCOPE_USER,
79 new base::StringValue("http://chromium.org/override"),
80 NULL);
81 policy.Set(key::kProxyServer,
82 POLICY_LEVEL_MANDATORY,
83 POLICY_SCOPE_USER,
84 new base::StringValue("chromium.org"),
85 NULL);
86 policy.Set(
87 key::kProxyServerMode,
88 POLICY_LEVEL_MANDATORY,
89 POLICY_SCOPE_USER,
90 new base::FundamentalValue(
91 ProxyPolicyHandler::PROXY_MANUALLY_CONFIGURED_PROXY_SERVER_MODE),
92 NULL);
93 UpdateProviderPolicy(policy);
95 VerifyProxyPrefs("chromium.org",
96 std::string(),
97 "http://chromium.org/override",
98 ProxyPrefs::MODE_FIXED_SERVERS);
101 TEST_F(ProxyPolicyHandlerTest, ManualOptionsReversedApplyOrder) {
102 PolicyMap policy;
103 policy.Set(
104 key::kProxyServerMode,
105 POLICY_LEVEL_MANDATORY,
106 POLICY_SCOPE_USER,
107 new base::FundamentalValue(
108 ProxyPolicyHandler::PROXY_MANUALLY_CONFIGURED_PROXY_SERVER_MODE),
109 NULL);
110 policy.Set(key::kProxyBypassList,
111 POLICY_LEVEL_MANDATORY,
112 POLICY_SCOPE_USER,
113 new base::StringValue("http://chromium.org/override"),
114 NULL);
115 policy.Set(key::kProxyServer,
116 POLICY_LEVEL_MANDATORY,
117 POLICY_SCOPE_USER,
118 new base::StringValue("chromium.org"),
119 NULL);
120 UpdateProviderPolicy(policy);
122 VerifyProxyPrefs("chromium.org",
123 std::string(),
124 "http://chromium.org/override",
125 ProxyPrefs::MODE_FIXED_SERVERS);
128 TEST_F(ProxyPolicyHandlerTest, ManualOptionsInvalid) {
129 PolicyMap policy;
130 policy.Set(
131 key::kProxyServerMode,
132 POLICY_LEVEL_MANDATORY,
133 POLICY_SCOPE_USER,
134 new base::FundamentalValue(
135 ProxyPolicyHandler::PROXY_MANUALLY_CONFIGURED_PROXY_SERVER_MODE),
136 NULL);
137 UpdateProviderPolicy(policy);
139 const base::Value* value = NULL;
140 EXPECT_FALSE(store_->GetValue(proxy_config::prefs::kProxy, &value));
143 TEST_F(ProxyPolicyHandlerTest, NoProxyServerMode) {
144 PolicyMap policy;
145 policy.Set(key::kProxyServerMode,
146 POLICY_LEVEL_MANDATORY,
147 POLICY_SCOPE_USER,
148 new base::FundamentalValue(ProxyPolicyHandler::PROXY_SERVER_MODE),
149 NULL);
150 UpdateProviderPolicy(policy);
151 VerifyProxyPrefs(
152 std::string(), std::string(), std::string(), ProxyPrefs::MODE_DIRECT);
155 TEST_F(ProxyPolicyHandlerTest, NoProxyModeName) {
156 PolicyMap policy;
157 policy.Set(key::kProxyMode,
158 POLICY_LEVEL_MANDATORY,
159 POLICY_SCOPE_USER,
160 new base::StringValue(ProxyPrefs::kDirectProxyModeName),
161 NULL);
162 UpdateProviderPolicy(policy);
163 VerifyProxyPrefs(
164 std::string(), std::string(), std::string(), ProxyPrefs::MODE_DIRECT);
167 TEST_F(ProxyPolicyHandlerTest, AutoDetectProxyServerMode) {
168 PolicyMap policy;
169 policy.Set(key::kProxyServerMode,
170 POLICY_LEVEL_MANDATORY,
171 POLICY_SCOPE_USER,
172 new base::FundamentalValue(
173 ProxyPolicyHandler::PROXY_AUTO_DETECT_PROXY_SERVER_MODE),
174 NULL);
175 UpdateProviderPolicy(policy);
176 VerifyProxyPrefs(std::string(),
177 std::string(),
178 std::string(),
179 ProxyPrefs::MODE_AUTO_DETECT);
182 TEST_F(ProxyPolicyHandlerTest, AutoDetectProxyModeName) {
183 PolicyMap policy;
184 policy.Set(key::kProxyMode,
185 POLICY_LEVEL_MANDATORY,
186 POLICY_SCOPE_USER,
187 new base::StringValue(ProxyPrefs::kAutoDetectProxyModeName),
188 NULL);
189 UpdateProviderPolicy(policy);
190 VerifyProxyPrefs(std::string(),
191 std::string(),
192 std::string(),
193 ProxyPrefs::MODE_AUTO_DETECT);
196 TEST_F(ProxyPolicyHandlerTest, PacScriptProxyMode) {
197 PolicyMap policy;
198 policy.Set(key::kProxyPacUrl,
199 POLICY_LEVEL_MANDATORY,
200 POLICY_SCOPE_USER,
201 new base::StringValue("http://short.org/proxy.pac"),
202 NULL);
203 policy.Set(key::kProxyMode,
204 POLICY_LEVEL_MANDATORY,
205 POLICY_SCOPE_USER,
206 new base::StringValue(ProxyPrefs::kPacScriptProxyModeName),
207 NULL);
208 UpdateProviderPolicy(policy);
209 VerifyProxyPrefs(std::string(),
210 "http://short.org/proxy.pac",
211 std::string(),
212 ProxyPrefs::MODE_PAC_SCRIPT);
215 TEST_F(ProxyPolicyHandlerTest, PacScriptProxyModeInvalid) {
216 PolicyMap policy;
217 policy.Set(key::kProxyMode,
218 POLICY_LEVEL_MANDATORY,
219 POLICY_SCOPE_USER,
220 new base::StringValue(ProxyPrefs::kPacScriptProxyModeName),
221 NULL);
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) {
230 PolicyMap policy;
231 policy.Set(key::kProxyServer,
232 POLICY_LEVEL_MANDATORY,
233 POLICY_SCOPE_USER,
234 new base::StringValue(std::string()),
235 NULL);
236 policy.Set(key::kProxyPacUrl,
237 POLICY_LEVEL_MANDATORY,
238 POLICY_SCOPE_USER,
239 new base::StringValue("http://short.org/proxy.pac"),
240 NULL);
241 policy.Set(key::kProxyMode,
242 POLICY_LEVEL_MANDATORY,
243 POLICY_SCOPE_USER,
244 new base::StringValue(ProxyPrefs::kPacScriptProxyModeName),
245 NULL);
246 UpdateProviderPolicy(policy);
247 VerifyProxyPrefs(std::string(),
248 "http://short.org/proxy.pac",
249 std::string(),
250 ProxyPrefs::MODE_PAC_SCRIPT);
253 TEST_F(ProxyPolicyHandlerTest, UseSystemProxyServerMode) {
254 PolicyMap policy;
255 policy.Set(key::kProxyServerMode,
256 POLICY_LEVEL_MANDATORY,
257 POLICY_SCOPE_USER,
258 new base::FundamentalValue(
259 ProxyPolicyHandler::PROXY_USE_SYSTEM_PROXY_SERVER_MODE),
260 NULL);
261 UpdateProviderPolicy(policy);
262 VerifyProxyPrefs(
263 std::string(), std::string(), std::string(), ProxyPrefs::MODE_SYSTEM);
266 TEST_F(ProxyPolicyHandlerTest, UseSystemProxyMode) {
267 PolicyMap policy;
268 policy.Set(key::kProxyMode,
269 POLICY_LEVEL_MANDATORY,
270 POLICY_SCOPE_USER,
271 new base::StringValue(ProxyPrefs::kSystemProxyModeName),
272 NULL);
273 UpdateProviderPolicy(policy);
274 VerifyProxyPrefs(
275 std::string(), std::string(), std::string(), ProxyPrefs::MODE_SYSTEM);
278 TEST_F(ProxyPolicyHandlerTest,
279 ProxyModeOverridesProxyServerMode) {
280 PolicyMap policy;
281 policy.Set(key::kProxyServerMode,
282 POLICY_LEVEL_MANDATORY,
283 POLICY_SCOPE_USER,
284 new base::FundamentalValue(ProxyPolicyHandler::PROXY_SERVER_MODE),
285 NULL);
286 policy.Set(key::kProxyMode,
287 POLICY_LEVEL_MANDATORY,
288 POLICY_SCOPE_USER,
289 new base::StringValue(ProxyPrefs::kAutoDetectProxyModeName),
290 NULL);
291 UpdateProviderPolicy(policy);
292 VerifyProxyPrefs(std::string(),
293 std::string(),
294 std::string(),
295 ProxyPrefs::MODE_AUTO_DETECT);
298 TEST_F(ProxyPolicyHandlerTest, ProxyInvalid) {
299 // No mode expects all three parameters being set.
300 PolicyMap policy;
301 policy.Set(key::kProxyPacUrl,
302 POLICY_LEVEL_MANDATORY,
303 POLICY_SCOPE_USER,
304 new base::StringValue("http://short.org/proxy.pac"),
305 NULL);
306 policy.Set(key::kProxyBypassList,
307 POLICY_LEVEL_MANDATORY,
308 POLICY_SCOPE_USER,
309 new base::StringValue("http://chromium.org/override"),
310 NULL);
311 policy.Set(key::kProxyServer,
312 POLICY_LEVEL_MANDATORY,
313 POLICY_SCOPE_USER,
314 new base::StringValue("chromium.org"),
315 NULL);
316 for (int i = 0; i < ProxyPolicyHandler::MODE_COUNT; ++i) {
317 policy.Set(key::kProxyServerMode,
318 POLICY_LEVEL_MANDATORY,
319 POLICY_SCOPE_USER,
320 new base::FundamentalValue(i),
321 NULL);
322 UpdateProviderPolicy(policy);
323 const base::Value* value = NULL;
324 EXPECT_FALSE(store_->GetValue(proxy_config::prefs::kProxy, &value));
328 } // namespace policy