NaCl: Update revision in DEPS, r12770 -> r12773
[chromium-blink-merge.git] / chrome / browser / extensions / policy_handlers_unittest.cc
blobe163ab8e43c626df4ea0a913f8f2f3e3c7a95697
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 "base/prefs/pref_value_map.h"
6 #include "chrome/browser/extensions/external_policy_loader.h"
7 #include "chrome/browser/extensions/policy_handlers.h"
8 #include "chrome/common/pref_names.h"
9 #include "components/policy/core/browser/policy_error_map.h"
10 #include "components/policy/core/common/policy_map.h"
11 #include "extensions/browser/pref_names.h"
12 #include "policy/policy_constants.h"
13 #include "testing/gtest/include/gtest/gtest.h"
15 namespace extensions {
17 const char kTestPref[] = "unit_test.test_pref";
19 TEST(ExtensionListPolicyHandlerTest, CheckPolicySettings) {
20 base::ListValue list;
21 policy::PolicyMap policy_map;
22 policy::PolicyErrorMap errors;
23 ExtensionListPolicyHandler handler(
24 policy::key::kExtensionInstallBlacklist, kTestPref, true);
26 policy_map.Set(policy::key::kExtensionInstallBlacklist,
27 policy::POLICY_LEVEL_MANDATORY,
28 policy::POLICY_SCOPE_USER,
29 list.DeepCopy(),
30 NULL);
31 errors.Clear();
32 EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors));
33 EXPECT_TRUE(errors.empty());
35 list.Append(
36 base::Value::CreateStringValue("abcdefghijklmnopabcdefghijklmnop"));
37 policy_map.Set(policy::key::kExtensionInstallBlacklist,
38 policy::POLICY_LEVEL_MANDATORY,
39 policy::POLICY_SCOPE_USER,
40 list.DeepCopy(),
41 NULL);
42 errors.Clear();
43 EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors));
44 EXPECT_TRUE(errors.empty());
46 list.Append(base::Value::CreateStringValue("*"));
47 policy_map.Set(policy::key::kExtensionInstallBlacklist,
48 policy::POLICY_LEVEL_MANDATORY,
49 policy::POLICY_SCOPE_USER,
50 list.DeepCopy(),
51 NULL);
52 errors.Clear();
53 EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors));
54 EXPECT_TRUE(errors.empty());
56 list.Append(base::Value::CreateStringValue("invalid"));
57 policy_map.Set(policy::key::kExtensionInstallBlacklist,
58 policy::POLICY_LEVEL_MANDATORY,
59 policy::POLICY_SCOPE_USER,
60 list.DeepCopy(),
61 NULL);
62 errors.Clear();
63 EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors));
64 EXPECT_FALSE(errors.empty());
65 EXPECT_FALSE(
66 errors.GetErrors(policy::key::kExtensionInstallBlacklist).empty());
69 TEST(ExtensionListPolicyHandlerTest, ApplyPolicySettings) {
70 base::ListValue policy;
71 base::ListValue expected;
72 policy::PolicyMap policy_map;
73 PrefValueMap prefs;
74 base::Value* value = NULL;
75 ExtensionListPolicyHandler handler(
76 policy::key::kExtensionInstallBlacklist, kTestPref, false);
78 policy.Append(
79 base::Value::CreateStringValue("abcdefghijklmnopabcdefghijklmnop"));
80 expected.Append(
81 base::Value::CreateStringValue("abcdefghijklmnopabcdefghijklmnop"));
83 policy_map.Set(policy::key::kExtensionInstallBlacklist,
84 policy::POLICY_LEVEL_MANDATORY,
85 policy::POLICY_SCOPE_USER,
86 policy.DeepCopy(),
87 NULL);
88 handler.ApplyPolicySettings(policy_map, &prefs);
89 EXPECT_TRUE(prefs.GetValue(kTestPref, &value));
90 EXPECT_TRUE(base::Value::Equals(&expected, value));
92 policy.Append(base::Value::CreateStringValue("invalid"));
93 policy_map.Set(policy::key::kExtensionInstallBlacklist,
94 policy::POLICY_LEVEL_MANDATORY,
95 policy::POLICY_SCOPE_USER,
96 policy.DeepCopy(),
97 NULL);
98 handler.ApplyPolicySettings(policy_map, &prefs);
99 EXPECT_TRUE(prefs.GetValue(kTestPref, &value));
100 EXPECT_TRUE(base::Value::Equals(&expected, value));
103 TEST(ExtensionInstallForcelistPolicyHandlerTest, CheckPolicySettings) {
104 base::ListValue list;
105 policy::PolicyMap policy_map;
106 policy::PolicyErrorMap errors;
107 ExtensionInstallForcelistPolicyHandler handler;
109 policy_map.Set(policy::key::kExtensionInstallForcelist,
110 policy::POLICY_LEVEL_MANDATORY,
111 policy::POLICY_SCOPE_USER,
112 list.DeepCopy(),
113 NULL);
114 errors.Clear();
115 EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors));
116 EXPECT_TRUE(errors.empty());
118 list.AppendString("abcdefghijklmnopabcdefghijklmnop;http://example.com");
119 policy_map.Set(policy::key::kExtensionInstallForcelist,
120 policy::POLICY_LEVEL_MANDATORY,
121 policy::POLICY_SCOPE_USER,
122 list.DeepCopy(),
123 NULL);
124 errors.Clear();
125 EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors));
126 EXPECT_TRUE(errors.empty());
128 // Add an erroneous entry. This should generate an error, but the good
129 // entry should still be translated successfully.
130 list.AppendString("adfasdf;http://example.com");
131 policy_map.Set(policy::key::kExtensionInstallForcelist,
132 policy::POLICY_LEVEL_MANDATORY,
133 policy::POLICY_SCOPE_USER,
134 list.DeepCopy(),
135 NULL);
136 errors.Clear();
137 EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors));
138 EXPECT_EQ(1U, errors.size());
140 // Add an entry with bad URL, which should generate another error.
141 list.AppendString("abcdefghijklmnopabcdefghijklmnop;nourl");
142 policy_map.Set(policy::key::kExtensionInstallForcelist,
143 policy::POLICY_LEVEL_MANDATORY,
144 policy::POLICY_SCOPE_USER,
145 list.DeepCopy(),
146 NULL);
147 errors.Clear();
148 EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors));
149 EXPECT_EQ(2U, errors.size());
151 // Just an extension ID should also generate an error.
152 list.AppendString("abcdefghijklmnopabcdefghijklmnop");
153 policy_map.Set(policy::key::kExtensionInstallForcelist,
154 policy::POLICY_LEVEL_MANDATORY,
155 policy::POLICY_SCOPE_USER,
156 list.DeepCopy(),
157 NULL);
158 errors.Clear();
159 EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors));
160 EXPECT_EQ(3U, errors.size());
163 TEST(ExtensionInstallForcelistPolicyHandlerTest, ApplyPolicySettings) {
164 base::ListValue policy;
165 base::DictionaryValue expected;
166 policy::PolicyMap policy_map;
167 PrefValueMap prefs;
168 base::Value* value = NULL;
169 ExtensionInstallForcelistPolicyHandler handler;
171 handler.ApplyPolicySettings(policy_map, &prefs);
172 EXPECT_FALSE(prefs.GetValue(pref_names::kInstallForceList, &value));
173 EXPECT_FALSE(value);
175 policy_map.Set(policy::key::kExtensionInstallForcelist,
176 policy::POLICY_LEVEL_MANDATORY,
177 policy::POLICY_SCOPE_USER,
178 policy.DeepCopy(),
179 NULL);
180 handler.ApplyPolicySettings(policy_map, &prefs);
181 EXPECT_TRUE(prefs.GetValue(pref_names::kInstallForceList, &value));
182 EXPECT_TRUE(base::Value::Equals(&expected, value));
184 policy.AppendString("abcdefghijklmnopabcdefghijklmnop;http://example.com");
185 extensions::ExternalPolicyLoader::AddExtension(
186 &expected, "abcdefghijklmnopabcdefghijklmnop", "http://example.com");
187 policy_map.Set(policy::key::kExtensionInstallForcelist,
188 policy::POLICY_LEVEL_MANDATORY,
189 policy::POLICY_SCOPE_USER,
190 policy.DeepCopy(),
191 NULL);
192 handler.ApplyPolicySettings(policy_map, &prefs);
193 EXPECT_TRUE(prefs.GetValue(pref_names::kInstallForceList, &value));
194 EXPECT_TRUE(base::Value::Equals(&expected, value));
196 policy.AppendString("invalid");
197 policy_map.Set(policy::key::kExtensionInstallForcelist,
198 policy::POLICY_LEVEL_MANDATORY,
199 policy::POLICY_SCOPE_USER,
200 policy.DeepCopy(),
201 NULL);
202 handler.ApplyPolicySettings(policy_map, &prefs);
203 EXPECT_TRUE(prefs.GetValue(pref_names::kInstallForceList, &value));
204 EXPECT_TRUE(base::Value::Equals(&expected, value));
207 TEST(ExtensionURLPatternListPolicyHandlerTest, CheckPolicySettings) {
208 base::ListValue list;
209 policy::PolicyMap policy_map;
210 policy::PolicyErrorMap errors;
211 ExtensionURLPatternListPolicyHandler handler(
212 policy::key::kExtensionInstallSources, kTestPref);
214 policy_map.Set(policy::key::kExtensionInstallSources,
215 policy::POLICY_LEVEL_MANDATORY,
216 policy::POLICY_SCOPE_USER,
217 list.DeepCopy(),
218 NULL);
219 errors.Clear();
220 EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors));
221 EXPECT_TRUE(errors.empty());
223 list.Append(base::Value::CreateStringValue("http://*.google.com/*"));
224 policy_map.Set(policy::key::kExtensionInstallSources,
225 policy::POLICY_LEVEL_MANDATORY,
226 policy::POLICY_SCOPE_USER,
227 list.DeepCopy(),
228 NULL);
229 errors.Clear();
230 EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors));
231 EXPECT_TRUE(errors.empty());
233 list.Append(base::Value::CreateStringValue("<all_urls>"));
234 policy_map.Set(policy::key::kExtensionInstallSources,
235 policy::POLICY_LEVEL_MANDATORY,
236 policy::POLICY_SCOPE_USER,
237 list.DeepCopy(),
238 NULL);
239 errors.Clear();
240 EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors));
241 EXPECT_TRUE(errors.empty());
243 list.Append(base::Value::CreateStringValue("invalid"));
244 policy_map.Set(policy::key::kExtensionInstallSources,
245 policy::POLICY_LEVEL_MANDATORY,
246 policy::POLICY_SCOPE_USER,
247 list.DeepCopy(),
248 NULL);
249 errors.Clear();
250 EXPECT_FALSE(handler.CheckPolicySettings(policy_map, &errors));
251 EXPECT_FALSE(errors.empty());
252 EXPECT_FALSE(errors.GetErrors(policy::key::kExtensionInstallSources).empty());
254 // URLPattern syntax has a different way to express 'all urls'. Though '*'
255 // would be compatible today, it would be brittle, so we disallow.
256 list.Append(base::Value::CreateStringValue("*"));
257 policy_map.Set(policy::key::kExtensionInstallSources,
258 policy::POLICY_LEVEL_MANDATORY,
259 policy::POLICY_SCOPE_USER,
260 list.DeepCopy(),
261 NULL);
262 errors.Clear();
263 EXPECT_FALSE(handler.CheckPolicySettings(policy_map, &errors));
264 EXPECT_FALSE(errors.empty());
265 EXPECT_FALSE(errors.GetErrors(policy::key::kExtensionInstallSources).empty());
268 TEST(ExtensionURLPatternListPolicyHandlerTest, ApplyPolicySettings) {
269 base::ListValue list;
270 policy::PolicyMap policy_map;
271 PrefValueMap prefs;
272 base::Value* value = NULL;
273 ExtensionURLPatternListPolicyHandler handler(
274 policy::key::kExtensionInstallSources, kTestPref);
276 list.Append(base::Value::CreateStringValue("https://corp.monkey.net/*"));
277 policy_map.Set(policy::key::kExtensionInstallSources,
278 policy::POLICY_LEVEL_MANDATORY,
279 policy::POLICY_SCOPE_USER,
280 list.DeepCopy(),
281 NULL);
282 handler.ApplyPolicySettings(policy_map, &prefs);
283 ASSERT_TRUE(prefs.GetValue(kTestPref, &value));
284 EXPECT_TRUE(base::Value::Equals(&list, value));
287 } // namespace extensions