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
) {
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
,
32 EXPECT_TRUE(handler
.CheckPolicySettings(policy_map
, &errors
));
33 EXPECT_TRUE(errors
.empty());
36 base::Value::CreateStringValue("abcdefghijklmnopabcdefghijklmnop"));
37 policy_map
.Set(policy::key::kExtensionInstallBlacklist
,
38 policy::POLICY_LEVEL_MANDATORY
,
39 policy::POLICY_SCOPE_USER
,
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
,
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
,
63 EXPECT_TRUE(handler
.CheckPolicySettings(policy_map
, &errors
));
64 EXPECT_FALSE(errors
.empty());
66 errors
.GetErrors(policy::key::kExtensionInstallBlacklist
).empty());
69 TEST(ExtensionListPolicyHandlerTest
, ApplyPolicySettings
) {
70 base::ListValue policy
;
71 base::ListValue expected
;
72 policy::PolicyMap policy_map
;
74 base::Value
* value
= NULL
;
75 ExtensionListPolicyHandler
handler(
76 policy::key::kExtensionInstallBlacklist
, kTestPref
, false);
79 base::Value::CreateStringValue("abcdefghijklmnopabcdefghijklmnop"));
81 base::Value::CreateStringValue("abcdefghijklmnopabcdefghijklmnop"));
83 policy_map
.Set(policy::key::kExtensionInstallBlacklist
,
84 policy::POLICY_LEVEL_MANDATORY
,
85 policy::POLICY_SCOPE_USER
,
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
,
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
,
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
,
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
,
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
,
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
,
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
;
168 base::Value
* value
= NULL
;
169 ExtensionInstallForcelistPolicyHandler handler
;
171 handler
.ApplyPolicySettings(policy_map
, &prefs
);
172 EXPECT_FALSE(prefs
.GetValue(pref_names::kInstallForceList
, &value
));
175 policy_map
.Set(policy::key::kExtensionInstallForcelist
,
176 policy::POLICY_LEVEL_MANDATORY
,
177 policy::POLICY_SCOPE_USER
,
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
,
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
,
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
,
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
,
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
,
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
,
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
,
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
;
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
,
282 handler
.ApplyPolicySettings(policy_map
, &prefs
);
283 ASSERT_TRUE(prefs
.GetValue(kTestPref
, &value
));
284 EXPECT_TRUE(base::Value::Equals(&list
, value
));
287 } // namespace extensions