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/browser/policy/policy_error_map.h"
9 #include "chrome/browser/policy/policy_map.h"
10 #include "chrome/common/pref_names.h"
11 #include "policy/policy_constants.h"
12 #include "testing/gtest/include/gtest/gtest.h"
14 namespace extensions
{
16 const char kTestPref
[] = "unit_test.test_pref";
18 TEST(ExtensionListPolicyHandlerTest
, CheckPolicySettings
) {
20 policy::PolicyMap policy_map
;
21 policy::PolicyErrorMap errors
;
22 ExtensionListPolicyHandler
handler(
23 policy::key::kExtensionInstallBlacklist
, kTestPref
, true);
25 policy_map
.Set(policy::key::kExtensionInstallBlacklist
,
26 policy::POLICY_LEVEL_MANDATORY
,
27 policy::POLICY_SCOPE_USER
,
31 EXPECT_TRUE(handler
.CheckPolicySettings(policy_map
, &errors
));
32 EXPECT_TRUE(errors
.empty());
34 list
.Append(Value::CreateStringValue("abcdefghijklmnopabcdefghijklmnop"));
35 policy_map
.Set(policy::key::kExtensionInstallBlacklist
,
36 policy::POLICY_LEVEL_MANDATORY
,
37 policy::POLICY_SCOPE_USER
,
41 EXPECT_TRUE(handler
.CheckPolicySettings(policy_map
, &errors
));
42 EXPECT_TRUE(errors
.empty());
44 list
.Append(Value::CreateStringValue("*"));
45 policy_map
.Set(policy::key::kExtensionInstallBlacklist
,
46 policy::POLICY_LEVEL_MANDATORY
,
47 policy::POLICY_SCOPE_USER
,
51 EXPECT_TRUE(handler
.CheckPolicySettings(policy_map
, &errors
));
52 EXPECT_TRUE(errors
.empty());
54 list
.Append(Value::CreateStringValue("invalid"));
55 policy_map
.Set(policy::key::kExtensionInstallBlacklist
,
56 policy::POLICY_LEVEL_MANDATORY
,
57 policy::POLICY_SCOPE_USER
,
61 EXPECT_TRUE(handler
.CheckPolicySettings(policy_map
, &errors
));
62 EXPECT_FALSE(errors
.empty());
64 errors
.GetErrors(policy::key::kExtensionInstallBlacklist
).empty());
67 TEST(ExtensionListPolicyHandlerTest
, ApplyPolicySettings
) {
68 base::ListValue policy
;
69 base::ListValue expected
;
70 policy::PolicyMap policy_map
;
72 base::Value
* value
= NULL
;
73 ExtensionListPolicyHandler
handler(
74 policy::key::kExtensionInstallBlacklist
, kTestPref
, false);
76 policy
.Append(Value::CreateStringValue("abcdefghijklmnopabcdefghijklmnop"));
77 expected
.Append(Value::CreateStringValue("abcdefghijklmnopabcdefghijklmnop"));
79 policy_map
.Set(policy::key::kExtensionInstallBlacklist
,
80 policy::POLICY_LEVEL_MANDATORY
,
81 policy::POLICY_SCOPE_USER
,
84 handler
.ApplyPolicySettings(policy_map
, &prefs
);
85 EXPECT_TRUE(prefs
.GetValue(kTestPref
, &value
));
86 EXPECT_TRUE(base::Value::Equals(&expected
, value
));
88 policy
.Append(Value::CreateStringValue("invalid"));
89 policy_map
.Set(policy::key::kExtensionInstallBlacklist
,
90 policy::POLICY_LEVEL_MANDATORY
,
91 policy::POLICY_SCOPE_USER
,
94 handler
.ApplyPolicySettings(policy_map
, &prefs
);
95 EXPECT_TRUE(prefs
.GetValue(kTestPref
, &value
));
96 EXPECT_TRUE(base::Value::Equals(&expected
, value
));
99 TEST(ExtensionInstallForcelistPolicyHandlerTest
, CheckPolicySettings
) {
100 base::ListValue list
;
101 policy::PolicyMap policy_map
;
102 policy::PolicyErrorMap errors
;
103 ExtensionInstallForcelistPolicyHandler handler
;
105 policy_map
.Set(policy::key::kExtensionInstallForcelist
,
106 policy::POLICY_LEVEL_MANDATORY
,
107 policy::POLICY_SCOPE_USER
,
111 EXPECT_TRUE(handler
.CheckPolicySettings(policy_map
, &errors
));
112 EXPECT_TRUE(errors
.empty());
114 list
.AppendString("abcdefghijklmnopabcdefghijklmnop;http://example.com");
115 policy_map
.Set(policy::key::kExtensionInstallForcelist
,
116 policy::POLICY_LEVEL_MANDATORY
,
117 policy::POLICY_SCOPE_USER
,
121 EXPECT_TRUE(handler
.CheckPolicySettings(policy_map
, &errors
));
122 EXPECT_TRUE(errors
.empty());
124 // Add an erroneous entry. This should generate an error, but the good
125 // entry should still be translated successfully.
126 list
.AppendString("adfasdf;http://example.com");
127 policy_map
.Set(policy::key::kExtensionInstallForcelist
,
128 policy::POLICY_LEVEL_MANDATORY
,
129 policy::POLICY_SCOPE_USER
,
133 EXPECT_TRUE(handler
.CheckPolicySettings(policy_map
, &errors
));
134 EXPECT_EQ(1U, errors
.size());
136 // Add an entry with bad URL, which should generate another error.
137 list
.AppendString("abcdefghijklmnopabcdefghijklmnop;nourl");
138 policy_map
.Set(policy::key::kExtensionInstallForcelist
,
139 policy::POLICY_LEVEL_MANDATORY
,
140 policy::POLICY_SCOPE_USER
,
144 EXPECT_TRUE(handler
.CheckPolicySettings(policy_map
, &errors
));
145 EXPECT_EQ(2U, errors
.size());
147 // Just an extension ID should also generate an error.
148 list
.AppendString("abcdefghijklmnopabcdefghijklmnop");
149 policy_map
.Set(policy::key::kExtensionInstallForcelist
,
150 policy::POLICY_LEVEL_MANDATORY
,
151 policy::POLICY_SCOPE_USER
,
155 EXPECT_TRUE(handler
.CheckPolicySettings(policy_map
, &errors
));
156 EXPECT_EQ(3U, errors
.size());
159 TEST(ExtensionInstallForcelistPolicyHandlerTest
, ApplyPolicySettings
) {
160 base::ListValue policy
;
161 base::DictionaryValue expected
;
162 policy::PolicyMap policy_map
;
164 base::Value
* value
= NULL
;
165 ExtensionInstallForcelistPolicyHandler handler
;
167 handler
.ApplyPolicySettings(policy_map
, &prefs
);
168 EXPECT_FALSE(prefs
.GetValue(prefs::kExtensionInstallForceList
, &value
));
171 policy_map
.Set(policy::key::kExtensionInstallForcelist
,
172 policy::POLICY_LEVEL_MANDATORY
,
173 policy::POLICY_SCOPE_USER
,
176 handler
.ApplyPolicySettings(policy_map
, &prefs
);
177 EXPECT_TRUE(prefs
.GetValue(prefs::kExtensionInstallForceList
, &value
));
178 EXPECT_TRUE(base::Value::Equals(&expected
, value
));
180 policy
.AppendString("abcdefghijklmnopabcdefghijklmnop;http://example.com");
181 extensions::ExternalPolicyLoader::AddExtension(
182 &expected
, "abcdefghijklmnopabcdefghijklmnop", "http://example.com");
183 policy_map
.Set(policy::key::kExtensionInstallForcelist
,
184 policy::POLICY_LEVEL_MANDATORY
,
185 policy::POLICY_SCOPE_USER
,
188 handler
.ApplyPolicySettings(policy_map
, &prefs
);
189 EXPECT_TRUE(prefs
.GetValue(prefs::kExtensionInstallForceList
, &value
));
190 EXPECT_TRUE(base::Value::Equals(&expected
, value
));
192 policy
.AppendString("invalid");
193 policy_map
.Set(policy::key::kExtensionInstallForcelist
,
194 policy::POLICY_LEVEL_MANDATORY
,
195 policy::POLICY_SCOPE_USER
,
198 handler
.ApplyPolicySettings(policy_map
, &prefs
);
199 EXPECT_TRUE(prefs
.GetValue(prefs::kExtensionInstallForceList
, &value
));
200 EXPECT_TRUE(base::Value::Equals(&expected
, value
));
203 TEST(ExtensionURLPatternListPolicyHandlerTest
, CheckPolicySettings
) {
204 base::ListValue list
;
205 policy::PolicyMap policy_map
;
206 policy::PolicyErrorMap errors
;
207 ExtensionURLPatternListPolicyHandler
handler(
208 policy::key::kExtensionInstallSources
, kTestPref
);
210 policy_map
.Set(policy::key::kExtensionInstallSources
,
211 policy::POLICY_LEVEL_MANDATORY
,
212 policy::POLICY_SCOPE_USER
,
216 EXPECT_TRUE(handler
.CheckPolicySettings(policy_map
, &errors
));
217 EXPECT_TRUE(errors
.empty());
219 list
.Append(Value::CreateStringValue("http://*.google.com/*"));
220 policy_map
.Set(policy::key::kExtensionInstallSources
,
221 policy::POLICY_LEVEL_MANDATORY
,
222 policy::POLICY_SCOPE_USER
,
226 EXPECT_TRUE(handler
.CheckPolicySettings(policy_map
, &errors
));
227 EXPECT_TRUE(errors
.empty());
229 list
.Append(Value::CreateStringValue("<all_urls>"));
230 policy_map
.Set(policy::key::kExtensionInstallSources
,
231 policy::POLICY_LEVEL_MANDATORY
,
232 policy::POLICY_SCOPE_USER
,
236 EXPECT_TRUE(handler
.CheckPolicySettings(policy_map
, &errors
));
237 EXPECT_TRUE(errors
.empty());
239 list
.Append(Value::CreateStringValue("invalid"));
240 policy_map
.Set(policy::key::kExtensionInstallSources
,
241 policy::POLICY_LEVEL_MANDATORY
,
242 policy::POLICY_SCOPE_USER
,
246 EXPECT_FALSE(handler
.CheckPolicySettings(policy_map
, &errors
));
247 EXPECT_FALSE(errors
.empty());
248 EXPECT_FALSE(errors
.GetErrors(policy::key::kExtensionInstallSources
).empty());
250 // URLPattern syntax has a different way to express 'all urls'. Though '*'
251 // would be compatible today, it would be brittle, so we disallow.
252 list
.Append(Value::CreateStringValue("*"));
253 policy_map
.Set(policy::key::kExtensionInstallSources
,
254 policy::POLICY_LEVEL_MANDATORY
,
255 policy::POLICY_SCOPE_USER
,
259 EXPECT_FALSE(handler
.CheckPolicySettings(policy_map
, &errors
));
260 EXPECT_FALSE(errors
.empty());
261 EXPECT_FALSE(errors
.GetErrors(policy::key::kExtensionInstallSources
).empty());
264 TEST(ExtensionURLPatternListPolicyHandlerTest
, ApplyPolicySettings
) {
265 base::ListValue list
;
266 policy::PolicyMap policy_map
;
268 base::Value
* value
= NULL
;
269 ExtensionURLPatternListPolicyHandler
handler(
270 policy::key::kExtensionInstallSources
, kTestPref
);
272 list
.Append(Value::CreateStringValue("https://corp.monkey.net/*"));
273 policy_map
.Set(policy::key::kExtensionInstallSources
,
274 policy::POLICY_LEVEL_MANDATORY
,
275 policy::POLICY_SCOPE_USER
,
278 handler
.ApplyPolicySettings(policy_map
, &prefs
);
279 ASSERT_TRUE(prefs
.GetValue(kTestPref
, &value
));
280 EXPECT_TRUE(base::Value::Equals(&list
, value
));
283 } // namespace extensions