1 // Copyright 2014 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.
8 #include "base/json/json_parser.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/prefs/pref_registry_simple.h"
11 #include "base/prefs/testing_pref_service.h"
12 #include "base/values.h"
13 #include "chrome/browser/extensions/extension_management.h"
14 #include "chrome/browser/extensions/extension_management_internal.h"
15 #include "chrome/browser/extensions/extension_management_test_util.h"
16 #include "chrome/browser/extensions/external_policy_loader.h"
17 #include "extensions/browser/pref_names.h"
18 #include "extensions/common/manifest.h"
19 #include "extensions/common/manifest_constants.h"
20 #include "extensions/common/url_pattern.h"
21 #include "testing/gtest/include/gtest/gtest.h"
24 namespace extensions
{
28 const char kTargetExtension
[] = "abcdefghijklmnopabcdefghijklmnop";
29 const char kTargetExtension2
[] = "bcdefghijklmnopabcdefghijklmnopa";
30 const char kTargetExtension3
[] = "cdefghijklmnopabcdefghijklmnopab";
31 const char kTargetExtension4
[] = "defghijklmnopabcdefghijklmnopabc";
32 const char kOtherExtension
[] = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
33 const char kExampleUpdateUrl
[] = "http://example.com/update_url";
35 const char kExampleDictPreference
[] =
37 " \"abcdefghijklmnopabcdefghijklmnop\": {" // kTargetExtension
38 " \"installation_mode\": \"allowed\","
40 " \"bcdefghijklmnopabcdefghijklmnopa\": {" // kTargetExtension2
41 " \"installation_mode\": \"force_installed\","
42 " \"update_url\": \"http://example.com/update_url\","
44 " \"cdefghijklmnopabcdefghijklmnopab\": {" // kTargetExtension3
45 " \"installation_mode\": \"normal_installed\","
46 " \"update_url\": \"http://example.com/update_url\","
48 " \"defghijklmnopabcdefghijklmnopabc\": {" // kTargetExtension4
49 " \"installation_mode\": \"blocked\","
52 " \"installation_mode\": \"blocked\","
53 " \"install_sources\": [\"*://foo.com/*\"],"
54 " \"allowed_types\": [\"theme\", \"user_script\"],"
60 class ExtensionManagementServiceTest
: public testing::Test
{
62 typedef ExtensionManagementPrefUpdater
<TestingPrefServiceSimple
> PrefUpdater
;
64 ExtensionManagementServiceTest() {}
65 virtual ~ExtensionManagementServiceTest() {}
68 virtual void SetUp() override
{
72 void InitPrefService() {
73 extension_management_
.reset();
74 pref_service_
.reset(new TestingPrefServiceSimple());
75 pref_service_
->registry()->RegisterListPref(
76 pref_names::kAllowedInstallSites
);
77 pref_service_
->registry()->RegisterListPref(pref_names::kAllowedTypes
);
78 pref_service_
->registry()->RegisterListPref(pref_names::kInstallDenyList
);
79 pref_service_
->registry()->RegisterListPref(pref_names::kInstallAllowList
);
80 pref_service_
->registry()->RegisterDictionaryPref(
81 pref_names::kInstallForceList
);
82 pref_service_
->registry()->RegisterDictionaryPref(
83 pref_names::kExtensionManagement
);
84 extension_management_
.reset(new ExtensionManagement(pref_service_
.get()));
87 void SetPref(bool managed
, const char* path
, base::Value
* value
) {
89 pref_service_
->SetManagedPref(path
, value
);
91 pref_service_
->SetUserPref(path
, value
);
94 void RemovePref(bool managed
, const char* path
) {
96 pref_service_
->RemoveManagedPref(path
);
98 pref_service_
->RemoveUserPref(path
);
101 const internal::IndividualSettings
* ReadById(const ExtensionId
& id
) {
102 return extension_management_
->ReadById(id
);
105 const internal::GlobalSettings
* ReadGlobalSettings() {
106 return extension_management_
->ReadGlobalSettings();
109 void SetExampleDictPref() {
110 std::string error_msg
;
111 scoped_ptr
<base::Value
> parsed(base::JSONReader::ReadAndReturnError(
112 kExampleDictPreference
,
113 base::JSONParserOptions::JSON_ALLOW_TRAILING_COMMAS
,
116 ASSERT_TRUE(parsed
&& parsed
->IsType(base::Value::TYPE_DICTIONARY
))
118 SetPref(true, pref_names::kExtensionManagement
, parsed
.release());
122 scoped_ptr
<TestingPrefServiceSimple
> pref_service_
;
123 scoped_ptr
<ExtensionManagement
> extension_management_
;
126 class ExtensionAdminPolicyTest
: public ExtensionManagementServiceTest
{
128 ExtensionAdminPolicyTest() {}
129 virtual ~ExtensionAdminPolicyTest() {}
131 void CreateExtension(Manifest::Location location
) {
132 base::DictionaryValue values
;
133 CreateExtensionFromValues(location
, &values
);
136 void CreateHostedApp(Manifest::Location location
) {
137 base::DictionaryValue values
;
138 values
.Set(extensions::manifest_keys::kWebURLs
, new base::ListValue());
139 values
.SetString(extensions::manifest_keys::kLaunchWebURL
,
140 "http://www.example.com");
141 CreateExtensionFromValues(location
, &values
);
144 void CreateExtensionFromValues(Manifest::Location location
,
145 base::DictionaryValue
* values
) {
146 values
->SetString(extensions::manifest_keys::kName
, "test");
147 values
->SetString(extensions::manifest_keys::kVersion
, "0.1");
149 extension_
= Extension::Create(base::FilePath(), location
, *values
,
150 Extension::NO_FLAGS
, &error
);
151 ASSERT_TRUE(extension_
.get());
154 // Wrappers for legacy admin policy functions, for testing purpose only.
155 bool BlacklistedByDefault(const base::ListValue
* blacklist
);
156 bool UserMayLoad(const base::ListValue
* blacklist
,
157 const base::ListValue
* whitelist
,
158 const base::DictionaryValue
* forcelist
,
159 const base::ListValue
* allowed_types
,
160 const Extension
* extension
,
161 base::string16
* error
);
162 bool UserMayModifySettings(const Extension
* extension
, base::string16
* error
);
163 bool MustRemainEnabled(const Extension
* extension
, base::string16
* error
);
166 scoped_refptr
<Extension
> extension_
;
169 bool ExtensionAdminPolicyTest::BlacklistedByDefault(
170 const base::ListValue
* blacklist
) {
173 SetPref(true, pref_names::kInstallDenyList
, blacklist
->DeepCopy());
174 return extension_management_
->BlacklistedByDefault();
177 bool ExtensionAdminPolicyTest::UserMayLoad(
178 const base::ListValue
* blacklist
,
179 const base::ListValue
* whitelist
,
180 const base::DictionaryValue
* forcelist
,
181 const base::ListValue
* allowed_types
,
182 const Extension
* extension
,
183 base::string16
* error
) {
186 SetPref(true, pref_names::kInstallDenyList
, blacklist
->DeepCopy());
188 SetPref(true, pref_names::kInstallAllowList
, whitelist
->DeepCopy());
190 SetPref(true, pref_names::kInstallForceList
, forcelist
->DeepCopy());
192 SetPref(true, pref_names::kAllowedTypes
, allowed_types
->DeepCopy());
193 return extension_management_
->GetProvider()->UserMayLoad(extension
, error
);
196 bool ExtensionAdminPolicyTest::UserMayModifySettings(const Extension
* extension
,
197 base::string16
* error
) {
199 return extension_management_
->GetProvider()->UserMayModifySettings(extension
,
203 bool ExtensionAdminPolicyTest::MustRemainEnabled(const Extension
* extension
,
204 base::string16
* error
) {
206 return extension_management_
->GetProvider()->MustRemainEnabled(extension
,
210 // Verify that preference controlled by legacy ExtensionInstallSources policy is
212 TEST_F(ExtensionManagementServiceTest
, LegacyInstallSources
) {
213 base::ListValue allowed_sites_pref
;
214 allowed_sites_pref
.AppendString("https://www.example.com/foo");
215 allowed_sites_pref
.AppendString("https://corp.mycompany.com/*");
217 true, pref_names::kAllowedInstallSites
, allowed_sites_pref
.DeepCopy());
218 const URLPatternSet
& allowed_sites
= ReadGlobalSettings()->install_sources
;
219 ASSERT_TRUE(ReadGlobalSettings()->has_restricted_install_sources
);
220 EXPECT_FALSE(allowed_sites
.is_empty());
221 EXPECT_TRUE(allowed_sites
.MatchesURL(GURL("https://www.example.com/foo")));
222 EXPECT_FALSE(allowed_sites
.MatchesURL(GURL("https://www.example.com/bar")));
224 allowed_sites
.MatchesURL(GURL("https://corp.mycompany.com/entry")));
226 allowed_sites
.MatchesURL(GURL("https://www.mycompany.com/entry")));
229 // Verify that preference controlled by legacy ExtensionAllowedTypes policy is
231 TEST_F(ExtensionManagementServiceTest
, LegacyAllowedTypes
) {
232 base::ListValue allowed_types_pref
;
233 allowed_types_pref
.AppendInteger(Manifest::TYPE_THEME
);
234 allowed_types_pref
.AppendInteger(Manifest::TYPE_USER_SCRIPT
);
236 SetPref(true, pref_names::kAllowedTypes
, allowed_types_pref
.DeepCopy());
237 const std::vector
<Manifest::Type
>& allowed_types
=
238 ReadGlobalSettings()->allowed_types
;
239 ASSERT_TRUE(ReadGlobalSettings()->has_restricted_allowed_types
);
240 EXPECT_TRUE(allowed_types
.size() == 2);
241 EXPECT_FALSE(std::find(allowed_types
.begin(),
243 Manifest::TYPE_EXTENSION
) != allowed_types
.end());
244 EXPECT_TRUE(std::find(allowed_types
.begin(),
246 Manifest::TYPE_THEME
) != allowed_types
.end());
247 EXPECT_TRUE(std::find(allowed_types
.begin(),
249 Manifest::TYPE_USER_SCRIPT
) != allowed_types
.end());
252 // Verify that preference controlled by legacy ExtensionInstallBlacklist policy
254 TEST_F(ExtensionManagementServiceTest
, LegacyInstallBlacklist
) {
255 base::ListValue denied_list_pref
;
256 denied_list_pref
.AppendString(kTargetExtension
);
258 SetPref(true, pref_names::kInstallDenyList
, denied_list_pref
.DeepCopy());
259 EXPECT_EQ(ReadById(kTargetExtension
)->installation_mode
,
260 ExtensionManagement::INSTALLATION_BLOCKED
);
261 EXPECT_EQ(ReadById(kOtherExtension
)->installation_mode
,
262 ExtensionManagement::INSTALLATION_ALLOWED
);
265 // Verify that preference controlled by legacy ExtensionInstallWhitelist policy
267 TEST_F(ExtensionManagementServiceTest
, LegacyInstallWhitelist
) {
268 base::ListValue denied_list_pref
;
269 denied_list_pref
.AppendString("*");
270 base::ListValue allowed_list_pref
;
271 allowed_list_pref
.AppendString(kTargetExtension
);
273 SetPref(true, pref_names::kInstallDenyList
, denied_list_pref
.DeepCopy());
274 SetPref(true, pref_names::kInstallAllowList
, allowed_list_pref
.DeepCopy());
275 EXPECT_EQ(ReadById(kTargetExtension
)->installation_mode
,
276 ExtensionManagement::INSTALLATION_ALLOWED
);
277 EXPECT_EQ(ReadById(kOtherExtension
)->installation_mode
,
278 ExtensionManagement::INSTALLATION_BLOCKED
);
280 // Verify that install whitelist preference set by user is ignored.
281 RemovePref(true, pref_names::kInstallAllowList
);
282 SetPref(false, pref_names::kInstallAllowList
, allowed_list_pref
.DeepCopy());
283 EXPECT_EQ(ReadById(kTargetExtension
)->installation_mode
,
284 ExtensionManagement::INSTALLATION_BLOCKED
);
287 // Verify that preference controlled by legacy ExtensionInstallForcelist policy
289 TEST_F(ExtensionManagementServiceTest
, LegacyInstallForcelist
) {
290 base::DictionaryValue forced_list_pref
;
291 ExternalPolicyLoader::AddExtension(
292 &forced_list_pref
, kTargetExtension
, kExampleUpdateUrl
);
294 SetPref(true, pref_names::kInstallForceList
, forced_list_pref
.DeepCopy());
295 EXPECT_EQ(ReadById(kTargetExtension
)->installation_mode
,
296 ExtensionManagement::INSTALLATION_FORCED
);
297 EXPECT_EQ(ReadById(kTargetExtension
)->update_url
, kExampleUpdateUrl
);
298 EXPECT_EQ(ReadById(kOtherExtension
)->installation_mode
,
299 ExtensionManagement::INSTALLATION_ALLOWED
);
301 // Verify that install forcelist preference set by user is ignored.
302 RemovePref(true, pref_names::kInstallForceList
);
303 SetPref(false, pref_names::kInstallForceList
, forced_list_pref
.DeepCopy());
304 EXPECT_EQ(ReadById(kTargetExtension
)->installation_mode
,
305 ExtensionManagement::INSTALLATION_ALLOWED
);
308 // Tests parsing of new dictionary preference.
309 TEST_F(ExtensionManagementServiceTest
, PreferenceParsing
) {
310 SetExampleDictPref();
312 // Verifies the installation mode settings.
313 EXPECT_TRUE(extension_management_
->BlacklistedByDefault());
314 EXPECT_EQ(ReadById(kTargetExtension
)->installation_mode
,
315 ExtensionManagement::INSTALLATION_ALLOWED
);
316 EXPECT_EQ(ReadById(kTargetExtension2
)->installation_mode
,
317 ExtensionManagement::INSTALLATION_FORCED
);
318 EXPECT_EQ(ReadById(kTargetExtension2
)->update_url
, kExampleUpdateUrl
);
319 EXPECT_EQ(ReadById(kTargetExtension3
)->installation_mode
,
320 ExtensionManagement::INSTALLATION_RECOMMENDED
);
321 EXPECT_EQ(ReadById(kTargetExtension3
)->update_url
, kExampleUpdateUrl
);
322 EXPECT_EQ(ReadById(kOtherExtension
)->installation_mode
,
323 ExtensionManagement::INSTALLATION_BLOCKED
);
325 // Verifies global settings.
326 EXPECT_TRUE(ReadGlobalSettings()->has_restricted_install_sources
);
327 const URLPatternSet
& allowed_sites
= ReadGlobalSettings()->install_sources
;
328 EXPECT_EQ(allowed_sites
.size(), 1u);
329 EXPECT_TRUE(allowed_sites
.MatchesURL(GURL("http://foo.com/entry")));
330 EXPECT_FALSE(allowed_sites
.MatchesURL(GURL("http://bar.com/entry")));
332 EXPECT_TRUE(ReadGlobalSettings()->has_restricted_allowed_types
);
333 const std::vector
<Manifest::Type
>& allowed_types
=
334 ReadGlobalSettings()->allowed_types
;
335 EXPECT_EQ(allowed_types
.size(), 2u);
336 EXPECT_TRUE(std::find(allowed_types
.begin(),
338 Manifest::TYPE_THEME
) != allowed_types
.end());
339 EXPECT_TRUE(std::find(allowed_types
.begin(),
341 Manifest::TYPE_USER_SCRIPT
) != allowed_types
.end());
344 // Tests functionality of new preference as to deprecate legacy
345 // ExtensionInstallSources policy.
346 TEST_F(ExtensionManagementServiceTest
, NewInstallSources
) {
347 // Set the legacy preference, and verifies that it works.
348 base::ListValue allowed_sites_pref
;
349 allowed_sites_pref
.AppendString("https://www.example.com/foo");
351 true, pref_names::kAllowedInstallSites
, allowed_sites_pref
.DeepCopy());
352 EXPECT_TRUE(ReadGlobalSettings()->has_restricted_install_sources
);
353 EXPECT_TRUE(ReadGlobalSettings()->install_sources
.MatchesURL(
354 GURL("https://www.example.com/foo")));
356 // Set the new dictionary preference.
358 PrefUpdater
updater(pref_service_
.get());
359 updater
.ClearInstallSources();
361 // Verifies that the new one overrides the legacy ones.
362 EXPECT_TRUE(ReadGlobalSettings()->has_restricted_install_sources
);
363 EXPECT_FALSE(ReadGlobalSettings()->install_sources
.MatchesURL(
364 GURL("https://www.example.com/foo")));
366 // Updates the new dictionary preference.
368 PrefUpdater
updater(pref_service_
.get());
369 updater
.AddInstallSource("https://corp.mycompany.com/*");
371 EXPECT_TRUE(ReadGlobalSettings()->has_restricted_install_sources
);
372 EXPECT_TRUE(ReadGlobalSettings()->install_sources
.MatchesURL(
373 GURL("https://corp.mycompany.com/entry")));
376 // Tests functionality of new preference as to deprecate legacy
377 // ExtensionAllowedTypes policy.
378 TEST_F(ExtensionManagementServiceTest
, NewAllowedTypes
) {
379 // Set the legacy preference, and verifies that it works.
380 base::ListValue allowed_types_pref
;
381 allowed_types_pref
.AppendInteger(Manifest::TYPE_USER_SCRIPT
);
382 SetPref(true, pref_names::kAllowedTypes
, allowed_types_pref
.DeepCopy());
383 EXPECT_TRUE(ReadGlobalSettings()->has_restricted_allowed_types
);
384 EXPECT_EQ(ReadGlobalSettings()->allowed_types
.size(), 1u);
385 EXPECT_EQ(ReadGlobalSettings()->allowed_types
[0], Manifest::TYPE_USER_SCRIPT
);
387 // Set the new dictionary preference.
389 PrefUpdater
updater(pref_service_
.get());
390 updater
.ClearAllowedTypes();
392 // Verifies that the new one overrides the legacy ones.
393 EXPECT_TRUE(ReadGlobalSettings()->has_restricted_allowed_types
);
394 EXPECT_EQ(ReadGlobalSettings()->allowed_types
.size(), 0u);
396 // Updates the new dictionary preference.
398 PrefUpdater
updater(pref_service_
.get());
399 updater
.AddAllowedType("theme");
401 EXPECT_TRUE(ReadGlobalSettings()->has_restricted_allowed_types
);
402 EXPECT_EQ(ReadGlobalSettings()->allowed_types
.size(), 1u);
403 EXPECT_EQ(ReadGlobalSettings()->allowed_types
[0], Manifest::TYPE_THEME
);
406 // Tests functionality of new preference as to deprecate legacy
407 // ExtensionInstallBlacklist policy.
408 TEST_F(ExtensionManagementServiceTest
, NewInstallBlacklist
) {
409 // Set the new dictionary preference.
411 PrefUpdater
updater(pref_service_
.get());
412 updater
.SetBlacklistedByDefault(false); // Allowed by default.
413 updater
.SetIndividualExtensionInstallationAllowed(kTargetExtension
, false);
414 updater
.ClearPerExtensionSettings(kTargetExtension2
);
415 updater
.ClearPerExtensionSettings(kOtherExtension
);
417 EXPECT_FALSE(extension_management_
->BlacklistedByDefault());
418 EXPECT_EQ(ReadById(kTargetExtension
)->installation_mode
,
419 ExtensionManagement::INSTALLATION_BLOCKED
);
420 EXPECT_EQ(ReadById(kOtherExtension
)->installation_mode
,
421 ExtensionManagement::INSTALLATION_ALLOWED
);
423 // Set legacy preference.
424 base::ListValue denied_list_pref
;
425 denied_list_pref
.AppendString("*");
426 denied_list_pref
.AppendString(kTargetExtension2
);
427 SetPref(true, pref_names::kInstallDenyList
, denied_list_pref
.DeepCopy());
429 base::ListValue allowed_list_pref
;
430 allowed_list_pref
.AppendString(kTargetExtension
);
431 SetPref(true, pref_names::kInstallAllowList
, allowed_list_pref
.DeepCopy());
433 // Verifies that the new one have higher priority over the legacy ones.
434 EXPECT_FALSE(extension_management_
->BlacklistedByDefault());
435 EXPECT_EQ(ReadById(kTargetExtension
)->installation_mode
,
436 ExtensionManagement::INSTALLATION_BLOCKED
);
437 EXPECT_EQ(ReadById(kTargetExtension2
)->installation_mode
,
438 ExtensionManagement::INSTALLATION_BLOCKED
);
439 EXPECT_EQ(ReadById(kOtherExtension
)->installation_mode
,
440 ExtensionManagement::INSTALLATION_ALLOWED
);
443 // Tests functionality of new preference as to deprecate legacy
444 // ExtensionInstallWhitelist policy.
445 TEST_F(ExtensionManagementServiceTest
, NewInstallWhitelist
) {
446 // Set the new dictionary preference.
448 PrefUpdater
updater(pref_service_
.get());
449 updater
.SetBlacklistedByDefault(true); // Disallowed by default.
450 updater
.SetIndividualExtensionInstallationAllowed(kTargetExtension
, true);
451 updater
.ClearPerExtensionSettings(kTargetExtension2
);
452 updater
.ClearPerExtensionSettings(kOtherExtension
);
454 EXPECT_TRUE(extension_management_
->BlacklistedByDefault());
455 EXPECT_EQ(ReadById(kTargetExtension
)->installation_mode
,
456 ExtensionManagement::INSTALLATION_ALLOWED
);
457 EXPECT_EQ(ReadById(kOtherExtension
)->installation_mode
,
458 ExtensionManagement::INSTALLATION_BLOCKED
);
460 // Set legacy preference.
461 base::ListValue denied_list_pref
;
462 denied_list_pref
.AppendString(kTargetExtension
);
463 SetPref(true, pref_names::kInstallDenyList
, denied_list_pref
.DeepCopy());
465 base::ListValue allowed_list_pref
;
466 allowed_list_pref
.AppendString(kTargetExtension2
);
467 SetPref(true, pref_names::kInstallAllowList
, allowed_list_pref
.DeepCopy());
469 // Verifies that the new one have higher priority over the legacy ones.
470 EXPECT_TRUE(extension_management_
->BlacklistedByDefault());
471 EXPECT_EQ(ReadById(kTargetExtension
)->installation_mode
,
472 ExtensionManagement::INSTALLATION_ALLOWED
);
473 EXPECT_EQ(ReadById(kTargetExtension2
)->installation_mode
,
474 ExtensionManagement::INSTALLATION_ALLOWED
);
475 EXPECT_EQ(ReadById(kOtherExtension
)->installation_mode
,
476 ExtensionManagement::INSTALLATION_BLOCKED
);
479 // Tests functionality of new preference as to deprecate legacy
480 // ExtensionInstallForcelist policy.
481 TEST_F(ExtensionManagementServiceTest
, NewInstallForcelist
) {
482 // Set some legacy preferences, to verify that the new one overrides the
484 base::ListValue denied_list_pref
;
485 denied_list_pref
.AppendString(kTargetExtension
);
486 SetPref(true, pref_names::kInstallDenyList
, denied_list_pref
.DeepCopy());
488 // Set the new dictionary preference.
490 PrefUpdater
updater(pref_service_
.get());
491 updater
.SetIndividualExtensionAutoInstalled(
492 kTargetExtension
, kExampleUpdateUrl
, true);
493 updater
.ClearPerExtensionSettings(kOtherExtension
);
495 EXPECT_EQ(ReadById(kTargetExtension
)->installation_mode
,
496 ExtensionManagement::INSTALLATION_FORCED
);
497 EXPECT_EQ(ReadById(kTargetExtension
)->update_url
, kExampleUpdateUrl
);
498 EXPECT_EQ(ReadById(kOtherExtension
)->installation_mode
,
499 ExtensionManagement::INSTALLATION_ALLOWED
);
502 // Tests the behavior of IsInstallationExplicitlyAllowed().
503 TEST_F(ExtensionManagementServiceTest
, IsInstallationExplicitlyAllowed
) {
504 SetExampleDictPref();
506 // Constant name indicates the installation_mode of extensions in example
508 const char* allowed
= kTargetExtension
;
509 const char* forced
= kTargetExtension2
;
510 const char* recommended
= kTargetExtension3
;
511 const char* blocked
= kTargetExtension4
;
512 const char* not_specified
= kOtherExtension
;
514 // BlacklistedByDefault() is true in example preference.
515 EXPECT_TRUE(extension_management_
->IsInstallationExplicitlyAllowed(allowed
));
516 EXPECT_TRUE(extension_management_
->IsInstallationExplicitlyAllowed(forced
));
518 extension_management_
->IsInstallationExplicitlyAllowed(recommended
));
519 EXPECT_FALSE(extension_management_
->IsInstallationExplicitlyAllowed(blocked
));
521 extension_management_
->IsInstallationExplicitlyAllowed(not_specified
));
524 // Set BlacklistedByDefault() to false.
525 PrefUpdater
pref(pref_service_
.get());
526 pref
.SetBlacklistedByDefault(false);
529 // The result should remain the same.
530 EXPECT_TRUE(extension_management_
->IsInstallationExplicitlyAllowed(allowed
));
531 EXPECT_TRUE(extension_management_
->IsInstallationExplicitlyAllowed(forced
));
533 extension_management_
->IsInstallationExplicitlyAllowed(recommended
));
534 EXPECT_FALSE(extension_management_
->IsInstallationExplicitlyAllowed(blocked
));
536 extension_management_
->IsInstallationExplicitlyAllowed(not_specified
));
539 // Tests the flag value indicating that extensions are blacklisted by default.
540 TEST_F(ExtensionAdminPolicyTest
, BlacklistedByDefault
) {
541 EXPECT_FALSE(BlacklistedByDefault(NULL
));
543 base::ListValue blacklist
;
544 blacklist
.Append(new base::StringValue(kOtherExtension
));
545 EXPECT_FALSE(BlacklistedByDefault(&blacklist
));
546 blacklist
.Append(new base::StringValue("*"));
547 EXPECT_TRUE(BlacklistedByDefault(&blacklist
));
550 blacklist
.Append(new base::StringValue("*"));
551 EXPECT_TRUE(BlacklistedByDefault(&blacklist
));
554 // Tests UserMayLoad for required extensions.
555 TEST_F(ExtensionAdminPolicyTest
, UserMayLoadRequired
) {
556 CreateExtension(Manifest::COMPONENT
);
557 EXPECT_TRUE(UserMayLoad(NULL
, NULL
, NULL
, NULL
, extension_
.get(), NULL
));
558 base::string16 error
;
559 EXPECT_TRUE(UserMayLoad(NULL
, NULL
, NULL
, NULL
, extension_
.get(), &error
));
560 EXPECT_TRUE(error
.empty());
562 // Required extensions may load even if they're on the blacklist.
563 base::ListValue blacklist
;
564 blacklist
.Append(new base::StringValue(extension_
->id()));
566 UserMayLoad(&blacklist
, NULL
, NULL
, NULL
, extension_
.get(), NULL
));
568 blacklist
.Append(new base::StringValue("*"));
570 UserMayLoad(&blacklist
, NULL
, NULL
, NULL
, extension_
.get(), NULL
));
573 // Tests UserMayLoad when no blacklist exists, or it's empty.
574 TEST_F(ExtensionAdminPolicyTest
, UserMayLoadNoBlacklist
) {
575 CreateExtension(Manifest::INTERNAL
);
576 EXPECT_TRUE(UserMayLoad(NULL
, NULL
, NULL
, NULL
, extension_
.get(), NULL
));
577 base::ListValue blacklist
;
579 UserMayLoad(&blacklist
, NULL
, NULL
, NULL
, extension_
.get(), NULL
));
580 base::string16 error
;
582 UserMayLoad(&blacklist
, NULL
, NULL
, NULL
, extension_
.get(), &error
));
583 EXPECT_TRUE(error
.empty());
586 // Tests UserMayLoad for an extension on the whitelist.
587 TEST_F(ExtensionAdminPolicyTest
, UserMayLoadWhitelisted
) {
588 CreateExtension(Manifest::INTERNAL
);
590 base::ListValue whitelist
;
591 whitelist
.Append(new base::StringValue(extension_
->id()));
593 UserMayLoad(NULL
, &whitelist
, NULL
, NULL
, extension_
.get(), NULL
));
595 base::ListValue blacklist
;
596 blacklist
.Append(new base::StringValue(extension_
->id()));
598 UserMayLoad(NULL
, &whitelist
, NULL
, NULL
, extension_
.get(), NULL
));
599 base::string16 error
;
601 UserMayLoad(NULL
, &whitelist
, NULL
, NULL
, extension_
.get(), &error
));
602 EXPECT_TRUE(error
.empty());
605 // Tests UserMayLoad for an extension on the blacklist.
606 TEST_F(ExtensionAdminPolicyTest
, UserMayLoadBlacklisted
) {
607 CreateExtension(Manifest::INTERNAL
);
609 // Blacklisted by default.
610 base::ListValue blacklist
;
611 blacklist
.Append(new base::StringValue("*"));
613 UserMayLoad(&blacklist
, NULL
, NULL
, NULL
, extension_
.get(), NULL
));
614 base::string16 error
;
616 UserMayLoad(&blacklist
, NULL
, NULL
, NULL
, extension_
.get(), &error
));
617 EXPECT_FALSE(error
.empty());
619 // Extension on the blacklist, with and without wildcard.
620 blacklist
.Append(new base::StringValue(extension_
->id()));
622 UserMayLoad(&blacklist
, NULL
, NULL
, NULL
, extension_
.get(), NULL
));
624 blacklist
.Append(new base::StringValue(extension_
->id()));
626 UserMayLoad(&blacklist
, NULL
, NULL
, NULL
, extension_
.get(), NULL
));
628 // With a whitelist. There's no such thing as a whitelist wildcard.
629 base::ListValue whitelist
;
630 whitelist
.Append(new base::StringValue("behllobkkfkfnphdnhnkndlbkcpglgmj"));
632 UserMayLoad(&blacklist
, &whitelist
, NULL
, NULL
, extension_
.get(), NULL
));
633 whitelist
.Append(new base::StringValue("*"));
635 UserMayLoad(&blacklist
, &whitelist
, NULL
, NULL
, extension_
.get(), NULL
));
638 TEST_F(ExtensionAdminPolicyTest
, UserMayLoadAllowedTypes
) {
639 CreateExtension(Manifest::INTERNAL
);
640 EXPECT_TRUE(UserMayLoad(NULL
, NULL
, NULL
, NULL
, extension_
.get(), NULL
));
642 base::ListValue allowed_types
;
644 UserMayLoad(NULL
, NULL
, NULL
, &allowed_types
, extension_
.get(), NULL
));
646 allowed_types
.AppendInteger(Manifest::TYPE_EXTENSION
);
648 UserMayLoad(NULL
, NULL
, NULL
, &allowed_types
, extension_
.get(), NULL
));
650 CreateHostedApp(Manifest::INTERNAL
);
652 UserMayLoad(NULL
, NULL
, NULL
, &allowed_types
, extension_
.get(), NULL
));
654 CreateHostedApp(Manifest::EXTERNAL_POLICY_DOWNLOAD
);
656 UserMayLoad(NULL
, NULL
, NULL
, &allowed_types
, extension_
.get(), NULL
));
659 TEST_F(ExtensionAdminPolicyTest
, UserMayModifySettings
) {
660 CreateExtension(Manifest::INTERNAL
);
661 EXPECT_TRUE(UserMayModifySettings(extension_
.get(), NULL
));
662 base::string16 error
;
663 EXPECT_TRUE(UserMayModifySettings(extension_
.get(), &error
));
664 EXPECT_TRUE(error
.empty());
666 CreateExtension(Manifest::EXTERNAL_POLICY_DOWNLOAD
);
668 EXPECT_FALSE(UserMayModifySettings(extension_
.get(), NULL
));
669 EXPECT_FALSE(UserMayModifySettings(extension_
.get(), &error
));
670 EXPECT_FALSE(error
.empty());
673 TEST_F(ExtensionAdminPolicyTest
, MustRemainEnabled
) {
674 CreateExtension(Manifest::EXTERNAL_POLICY_DOWNLOAD
);
675 EXPECT_TRUE(MustRemainEnabled(extension_
.get(), NULL
));
676 base::string16 error
;
677 EXPECT_TRUE(MustRemainEnabled(extension_
.get(), &error
));
678 EXPECT_FALSE(error
.empty());
680 CreateExtension(Manifest::INTERNAL
);
682 EXPECT_FALSE(MustRemainEnabled(extension_
.get(), NULL
));
683 EXPECT_FALSE(MustRemainEnabled(extension_
.get(), &error
));
684 EXPECT_TRUE(error
.empty());
687 } // namespace extensions