1 // Copyright (c) 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.
9 #include "base/at_exit.h"
10 #include "base/basictypes.h"
11 #include "base/bind.h"
12 #include "base/command_line.h"
13 #include "base/files/file_enumerator.h"
14 #include "base/files/file_util.h"
15 #include "base/files/scoped_temp_dir.h"
16 #include "base/json/json_file_value_serializer.h"
17 #include "base/json/json_reader.h"
18 #include "base/json/json_string_value_serializer.h"
19 #include "base/memory/scoped_ptr.h"
20 #include "base/memory/weak_ptr.h"
21 #include "base/message_loop/message_loop.h"
22 #include "base/prefs/scoped_user_pref_update.h"
23 #include "base/stl_util.h"
24 #include "base/strings/string16.h"
25 #include "base/strings/string_number_conversions.h"
26 #include "base/strings/string_util.h"
27 #include "base/strings/utf_string_conversions.h"
28 #include "base/version.h"
29 #include "chrome/browser/browser_process.h"
30 #include "chrome/browser/chrome_notification_types.h"
31 #include "chrome/browser/extensions/app_sync_data.h"
32 #include "chrome/browser/extensions/blacklist.h"
33 #include "chrome/browser/extensions/chrome_app_sorting.h"
34 #include "chrome/browser/extensions/component_loader.h"
35 #include "chrome/browser/extensions/crx_installer.h"
36 #include "chrome/browser/extensions/default_apps.h"
37 #include "chrome/browser/extensions/extension_creator.h"
38 #include "chrome/browser/extensions/extension_error_reporter.h"
39 #include "chrome/browser/extensions/extension_error_ui.h"
40 #include "chrome/browser/extensions/extension_management_test_util.h"
41 #include "chrome/browser/extensions/extension_notification_observer.h"
42 #include "chrome/browser/extensions/extension_service.h"
43 #include "chrome/browser/extensions/extension_service_test_base.h"
44 #include "chrome/browser/extensions/extension_special_storage_policy.h"
45 #include "chrome/browser/extensions/extension_sync_data.h"
46 #include "chrome/browser/extensions/extension_sync_service.h"
47 #include "chrome/browser/extensions/extension_util.h"
48 #include "chrome/browser/extensions/external_install_error.h"
49 #include "chrome/browser/extensions/external_install_manager.h"
50 #include "chrome/browser/extensions/external_policy_loader.h"
51 #include "chrome/browser/extensions/external_pref_loader.h"
52 #include "chrome/browser/extensions/external_provider_impl.h"
53 #include "chrome/browser/extensions/fake_safe_browsing_database_manager.h"
54 #include "chrome/browser/extensions/installed_loader.h"
55 #include "chrome/browser/extensions/pack_extension_job.h"
56 #include "chrome/browser/extensions/pending_extension_info.h"
57 #include "chrome/browser/extensions/pending_extension_manager.h"
58 #include "chrome/browser/extensions/permissions_updater.h"
59 #include "chrome/browser/extensions/test_blacklist.h"
60 #include "chrome/browser/extensions/test_extension_system.h"
61 #include "chrome/browser/extensions/unpacked_installer.h"
62 #include "chrome/browser/extensions/updater/extension_updater.h"
63 #include "chrome/browser/prefs/pref_service_syncable.h"
64 #include "chrome/browser/sync/profile_sync_service.h"
65 #include "chrome/browser/sync/profile_sync_service_factory.h"
66 #include "chrome/common/chrome_constants.h"
67 #include "chrome/common/chrome_switches.h"
68 #include "chrome/common/extensions/api/plugins/plugins_handler.h"
69 #include "chrome/common/extensions/manifest_handlers/app_launch_info.h"
70 #include "chrome/common/extensions/manifest_handlers/content_scripts_handler.h"
71 #include "chrome/common/pref_names.h"
72 #include "chrome/common/url_constants.h"
73 #include "chrome/test/base/scoped_browser_locale.h"
74 #include "chrome/test/base/testing_pref_service_syncable.h"
75 #include "chrome/test/base/testing_profile.h"
76 #include "components/crx_file/id_util.h"
77 #include "components/pref_registry/pref_registry_syncable.h"
78 #include "content/public/browser/dom_storage_context.h"
79 #include "content/public/browser/gpu_data_manager.h"
80 #include "content/public/browser/indexed_db_context.h"
81 #include "content/public/browser/notification_registrar.h"
82 #include "content/public/browser/notification_service.h"
83 #include "content/public/browser/plugin_service.h"
84 #include "content/public/browser/render_process_host.h"
85 #include "content/public/browser/storage_partition.h"
86 #include "content/public/common/content_constants.h"
87 #include "content/public/test/test_browser_thread_bundle.h"
88 #include "content/public/test/test_utils.h"
89 #include "extensions/browser/extension_prefs.h"
90 #include "extensions/browser/extension_registry.h"
91 #include "extensions/browser/extension_system.h"
92 #include "extensions/browser/external_provider_interface.h"
93 #include "extensions/browser/install_flag.h"
94 #include "extensions/browser/management_policy.h"
95 #include "extensions/browser/test_management_policy.h"
96 #include "extensions/browser/uninstall_reason.h"
97 #include "extensions/common/constants.h"
98 #include "extensions/common/extension.h"
99 #include "extensions/common/extension_builder.h"
100 #include "extensions/common/extension_l10n_util.h"
101 #include "extensions/common/extension_resource.h"
102 #include "extensions/common/feature_switch.h"
103 #include "extensions/common/manifest_constants.h"
104 #include "extensions/common/manifest_handlers/background_info.h"
105 #include "extensions/common/manifest_handlers/permissions_parser.h"
106 #include "extensions/common/manifest_url_handlers.h"
107 #include "extensions/common/permissions/permission_set.h"
108 #include "extensions/common/permissions/permissions_data.h"
109 #include "extensions/common/switches.h"
110 #include "extensions/common/url_pattern.h"
111 #include "extensions/common/value_builder.h"
112 #include "gpu/config/gpu_info.h"
113 #include "grit/browser_resources.h"
114 #include "grit/generated_resources.h"
115 #include "net/cookies/canonical_cookie.h"
116 #include "net/cookies/cookie_monster.h"
117 #include "net/cookies/cookie_options.h"
118 #include "net/url_request/url_request_context.h"
119 #include "net/url_request/url_request_context_getter.h"
120 #include "storage/browser/database/database_tracker.h"
121 #include "storage/browser/quota/quota_manager.h"
122 #include "storage/common/database/database_identifier.h"
123 #include "sync/api/fake_sync_change_processor.h"
124 #include "sync/api/string_ordinal.h"
125 #include "sync/api/sync_data.h"
126 #include "sync/api/sync_error_factory.h"
127 #include "sync/api/sync_error_factory_mock.h"
128 #include "sync/api/syncable_service.h"
129 #include "sync/protocol/app_specifics.pb.h"
130 #include "sync/protocol/extension_specifics.pb.h"
131 #include "sync/protocol/sync.pb.h"
132 #include "testing/gtest/include/gtest/gtest.h"
133 #include "testing/platform_test.h"
134 #include "ui/base/l10n/l10n_util.h"
135 #include "url/gurl.h"
137 #if defined(ENABLE_SUPERVISED_USERS)
138 #include "chrome/browser/supervised_user/permission_request_creator.h"
139 #include "chrome/browser/supervised_user/supervised_user_constants.h"
140 #include "chrome/browser/supervised_user/supervised_user_service.h"
141 #include "chrome/browser/supervised_user/supervised_user_service_factory.h"
144 #if defined(OS_CHROMEOS)
145 #include "chrome/browser/chromeos/login/users/scoped_test_user_manager.h"
146 #include "chrome/browser/chromeos/settings/cros_settings.h"
147 #include "chrome/browser/chromeos/settings/device_settings_service.h"
150 // The blacklist tests rely on the safe-browsing database.
151 #if defined(SAFE_BROWSING_DB_LOCAL)
152 #define ENABLE_BLACKLIST_TESTS
155 using base::DictionaryValue
;
156 using base::ListValue
;
158 using content::BrowserContext
;
159 using content::BrowserThread
;
160 using content::DOMStorageContext
;
161 using content::IndexedDBContext
;
162 using content::PluginService
;
163 using extensions::APIPermission
;
164 using extensions::APIPermissionSet
;
165 using extensions::AppSorting
;
166 using extensions::AppSyncData
;
167 using extensions::Blacklist
;
168 using extensions::CrxInstaller
;
169 using extensions::Extension
;
170 using extensions::ExtensionCreator
;
171 using extensions::ExtensionPrefs
;
172 using extensions::ExtensionRegistry
;
173 using extensions::ExtensionResource
;
174 using extensions::ExtensionSyncData
;
175 using extensions::ExtensionSystem
;
176 using extensions::FakeSafeBrowsingDatabaseManager
;
177 using extensions::FeatureSwitch
;
178 using extensions::Manifest
;
179 using extensions::PermissionSet
;
180 using extensions::TestExtensionSystem
;
181 using extensions::UnloadedExtensionInfo
;
182 using extensions::URLPatternSet
;
184 namespace keys
= extensions::manifest_keys
;
188 // Extension ids used during testing.
189 const char good0
[] = "behllobkkfkfnphdnhnkndlbkcpglgmj";
190 const char good1
[] = "hpiknbiabeeppbpihjehijgoemciehgk";
191 const char good2
[] = "bjafgdebaacbbbecmhlhpofkepfkgcpa";
192 const char all_zero
[] = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
193 const char good2048
[] = "nmgjhmhbleinmjpbdhgajfjkbijcmgbh";
194 const char good_crx
[] = "ldnnhddmnhbkjipkidpdiheffobcpfmf";
195 const char hosted_app
[] = "kbmnembihfiondgfjekmnmcbddelicoi";
196 const char page_action
[] = "obcimlgaoabeegjmmpldobjndiealpln";
197 const char theme_crx
[] = "iamefpfkojoapidjnbafmgkgncegbkad";
198 const char theme2_crx
[] = "pjpgmfcmabopnnfonnhmdjglfpjjfkbf";
199 const char permissions_crx
[] = "eagpmdpfmaekmmcejjbmjoecnejeiiin";
200 const char updates_from_webstore
[] = "akjooamlhcgeopfifcmlggaebeocgokj";
201 const char permissions_blocklist
[] = "noffkehfcaggllbcojjbopcmlhcnhcdn";
203 struct ExtensionsOrder
{
204 bool operator()(const scoped_refptr
<const Extension
>& a
,
205 const scoped_refptr
<const Extension
>& b
) {
206 return a
->name() < b
->name();
210 static std::vector
<base::string16
> GetErrors() {
211 const std::vector
<base::string16
>* errors
=
212 ExtensionErrorReporter::GetInstance()->GetErrors();
213 std::vector
<base::string16
> ret_val
;
215 for (std::vector
<base::string16
>::const_iterator iter
= errors
->begin();
216 iter
!= errors
->end(); ++iter
) {
217 std::string utf8_error
= base::UTF16ToUTF8(*iter
);
218 if (utf8_error
.find(".svn") == std::string::npos
) {
219 ret_val
.push_back(*iter
);
223 // The tests rely on the errors being in a certain order, which can vary
224 // depending on how filesystem iteration works.
225 std::stable_sort(ret_val
.begin(), ret_val
.end());
230 static void AddPattern(URLPatternSet
* extent
, const std::string
& pattern
) {
231 int schemes
= URLPattern::SCHEME_ALL
;
232 extent
->AddPattern(URLPattern(schemes
, pattern
));
235 base::FilePath
GetTemporaryFile() {
236 base::FilePath temp_file
;
237 CHECK(base::CreateTemporaryFile(&temp_file
));
241 bool WaitForCountNotificationsCallback(int *count
) {
242 return --(*count
) == 0;
247 class MockExtensionProvider
: public extensions::ExternalProviderInterface
{
249 MockExtensionProvider(
250 VisitorInterface
* visitor
,
251 Manifest::Location location
)
252 : location_(location
), visitor_(visitor
), visit_count_(0) {
255 ~MockExtensionProvider() override
{}
257 void UpdateOrAddExtension(const std::string
& id
,
258 const std::string
& version
,
259 const base::FilePath
& path
) {
260 extension_map_
[id
] = std::make_pair(version
, path
);
263 void RemoveExtension(const std::string
& id
) {
264 extension_map_
.erase(id
);
267 // ExternalProvider implementation:
268 void VisitRegisteredExtension() override
{
270 for (DataMap::const_iterator i
= extension_map_
.begin();
271 i
!= extension_map_
.end(); ++i
) {
272 Version
version(i
->second
.first
);
274 visitor_
->OnExternalExtensionFileFound(
275 i
->first
, &version
, i
->second
.second
, location_
,
276 Extension::NO_FLAGS
, false, false);
278 visitor_
->OnExternalProviderReady(this);
281 bool HasExtension(const std::string
& id
) const override
{
282 return extension_map_
.find(id
) != extension_map_
.end();
285 bool GetExtensionDetails(const std::string
& id
,
286 Manifest::Location
* location
,
287 scoped_ptr
<Version
>* version
) const override
{
288 DataMap::const_iterator it
= extension_map_
.find(id
);
289 if (it
== extension_map_
.end())
293 version
->reset(new Version(it
->second
.first
));
296 *location
= location_
;
301 bool IsReady() const override
{ return true; }
303 void ServiceShutdown() override
{}
305 int visit_count() const { return visit_count_
; }
306 void set_visit_count(int visit_count
) {
307 visit_count_
= visit_count
;
311 typedef std::map
< std::string
, std::pair
<std::string
, base::FilePath
> >
313 DataMap extension_map_
;
314 Manifest::Location location_
;
315 VisitorInterface
* visitor_
;
317 // visit_count_ tracks the number of calls to VisitRegisteredExtension().
318 // Mutable because it must be incremented on each call to
319 // VisitRegisteredExtension(), which must be a const method to inherit
320 // from the class being mocked.
321 mutable int visit_count_
;
323 DISALLOW_COPY_AND_ASSIGN(MockExtensionProvider
);
326 class MockProviderVisitor
327 : public extensions::ExternalProviderInterface::VisitorInterface
{
329 // The provider will return |fake_base_path| from
330 // GetBaseCrxFilePath(). User can test the behavior with
331 // and without an empty path using this parameter.
332 explicit MockProviderVisitor(base::FilePath fake_base_path
)
334 fake_base_path_(fake_base_path
),
335 expected_creation_flags_(Extension::NO_FLAGS
) {
336 profile_
.reset(new TestingProfile
);
339 MockProviderVisitor(base::FilePath fake_base_path
,
340 int expected_creation_flags
)
342 fake_base_path_(fake_base_path
),
343 expected_creation_flags_(expected_creation_flags
) {
346 int Visit(const std::string
& json_data
) {
347 // Give the test json file to the provider for parsing.
348 provider_
.reset(new extensions::ExternalProviderImpl(
350 new extensions::ExternalTestingLoader(json_data
, fake_base_path_
),
352 Manifest::EXTERNAL_PREF
,
353 Manifest::EXTERNAL_PREF_DOWNLOAD
,
354 Extension::NO_FLAGS
));
356 // We also parse the file into a dictionary to compare what we get back
357 // from the provider.
358 JSONStringValueDeserializer
deserializer(json_data
);
359 base::Value
* json_value
= deserializer
.Deserialize(NULL
, NULL
);
361 if (!json_value
|| !json_value
->IsType(base::Value::TYPE_DICTIONARY
)) {
362 NOTREACHED() << "Unable to deserialize json data";
365 base::DictionaryValue
* external_extensions
=
366 static_cast<base::DictionaryValue
*>(json_value
);
367 prefs_
.reset(external_extensions
);
370 // Reset our counter.
372 // Ask the provider to look up all extensions and return them.
373 provider_
->VisitRegisteredExtension();
378 bool OnExternalExtensionFileFound(const std::string
& id
,
379 const Version
* version
,
380 const base::FilePath
& path
,
381 Manifest::Location unused
,
383 bool mark_acknowledged
,
384 bool install_immediately
) override
{
385 EXPECT_EQ(expected_creation_flags_
, creation_flags
);
388 base::DictionaryValue
* pref
;
389 // This tests is to make sure that the provider only notifies us of the
390 // values we gave it. So if the id we doesn't exist in our internal
391 // dictionary then something is wrong.
392 EXPECT_TRUE(prefs_
->GetDictionary(id
, &pref
))
393 << "Got back ID (" << id
.c_str() << ") we weren't expecting";
395 EXPECT_TRUE(path
.IsAbsolute());
396 if (!fake_base_path_
.empty())
397 EXPECT_TRUE(fake_base_path_
.IsParent(path
));
400 EXPECT_TRUE(provider_
->HasExtension(id
));
402 // Ask provider if the extension we got back is registered.
403 Manifest::Location location
= Manifest::INVALID_LOCATION
;
404 scoped_ptr
<Version
> v1
;
405 base::FilePath crx_path
;
407 EXPECT_TRUE(provider_
->GetExtensionDetails(id
, NULL
, &v1
));
408 EXPECT_STREQ(version
->GetString().c_str(), v1
->GetString().c_str());
410 scoped_ptr
<Version
> v2
;
411 EXPECT_TRUE(provider_
->GetExtensionDetails(id
, &location
, &v2
));
412 EXPECT_STREQ(version
->GetString().c_str(), v1
->GetString().c_str());
413 EXPECT_STREQ(version
->GetString().c_str(), v2
->GetString().c_str());
414 EXPECT_EQ(Manifest::EXTERNAL_PREF
, location
);
416 // Remove it so we won't count it ever again.
417 prefs_
->Remove(id
, NULL
);
422 bool OnExternalExtensionUpdateUrlFound(const std::string
& id
,
423 const std::string
& install_parameter
,
424 const GURL
& update_url
,
425 Manifest::Location location
,
427 bool mark_acknowledged
) override
{
429 base::DictionaryValue
* pref
;
430 // This tests is to make sure that the provider only notifies us of the
431 // values we gave it. So if the id we doesn't exist in our internal
432 // dictionary then something is wrong.
433 EXPECT_TRUE(prefs_
->GetDictionary(id
, &pref
))
434 << L
"Got back ID (" << id
.c_str() << ") we weren't expecting";
435 EXPECT_EQ(Manifest::EXTERNAL_PREF_DOWNLOAD
, location
);
438 EXPECT_TRUE(provider_
->HasExtension(id
));
440 // External extensions with update URLs do not have versions.
441 scoped_ptr
<Version
> v1
;
442 Manifest::Location location1
= Manifest::INVALID_LOCATION
;
443 EXPECT_TRUE(provider_
->GetExtensionDetails(id
, &location1
, &v1
));
444 EXPECT_FALSE(v1
.get());
445 EXPECT_EQ(Manifest::EXTERNAL_PREF_DOWNLOAD
, location1
);
447 std::string parsed_install_parameter
;
448 pref
->GetString("install_parameter", &parsed_install_parameter
);
449 EXPECT_EQ(parsed_install_parameter
, install_parameter
);
451 // Remove it so we won't count it again.
452 prefs_
->Remove(id
, NULL
);
457 void OnExternalProviderReady(
458 const extensions::ExternalProviderInterface
* provider
) override
{
459 EXPECT_EQ(provider
, provider_
.get());
460 EXPECT_TRUE(provider
->IsReady());
463 Profile
* profile() { return profile_
.get(); }
467 base::FilePath fake_base_path_
;
468 int expected_creation_flags_
;
469 scoped_ptr
<extensions::ExternalProviderImpl
> provider_
;
470 scoped_ptr
<base::DictionaryValue
> prefs_
;
471 scoped_ptr
<TestingProfile
> profile_
;
473 DISALLOW_COPY_AND_ASSIGN(MockProviderVisitor
);
476 class ExtensionServiceTest
: public extensions::ExtensionServiceTestBase
,
477 public content::NotificationObserver
{
479 ExtensionServiceTest()
480 : unloaded_reason_(UnloadedExtensionInfo::REASON_UNDEFINED
),
483 override_external_install_prompt_(
484 FeatureSwitch::prompt_for_external_extensions(),
486 expected_extensions_count_(0) {
488 extensions::NOTIFICATION_EXTENSION_LOADED_DEPRECATED
,
489 content::NotificationService::AllSources());
491 extensions::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED
,
492 content::NotificationService::AllSources());
495 extensions::NOTIFICATION_EXTENSION_WILL_BE_INSTALLED_DEPRECATED
,
496 content::NotificationService::AllSources());
499 void Observe(int type
,
500 const content::NotificationSource
& source
,
501 const content::NotificationDetails
& details
) override
{
503 case extensions::NOTIFICATION_EXTENSION_LOADED_DEPRECATED
: {
504 const Extension
* extension
=
505 content::Details
<const Extension
>(details
).ptr();
506 loaded_
.push_back(make_scoped_refptr(extension
));
507 // The tests rely on the errors being in a certain order, which can vary
508 // depending on how filesystem iteration works.
509 std::stable_sort(loaded_
.begin(), loaded_
.end(), ExtensionsOrder());
513 case extensions::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED
: {
514 UnloadedExtensionInfo
* unloaded_info
=
515 content::Details
<UnloadedExtensionInfo
>(details
).ptr();
516 const Extension
* e
= unloaded_info
->extension
;
517 unloaded_id_
= e
->id();
518 unloaded_reason_
= unloaded_info
->reason
;
519 extensions::ExtensionList::iterator i
=
520 std::find(loaded_
.begin(), loaded_
.end(), e
);
521 // TODO(erikkay) fix so this can be an assert. Right now the tests
522 // are manually calling clear() on loaded_, so this isn't doable.
523 if (i
== loaded_
.end())
528 case extensions::NOTIFICATION_EXTENSION_WILL_BE_INSTALLED_DEPRECATED
: {
529 const extensions::InstalledExtensionInfo
* installed_info
=
530 content::Details
<const extensions::InstalledExtensionInfo
>(details
)
532 installed_
= installed_info
->extension
;
533 was_update_
= installed_info
->is_update
;
534 old_name_
= installed_info
->old_name
;
543 void AddMockExternalProvider(
544 extensions::ExternalProviderInterface
* provider
) {
545 service()->AddProviderForTesting(provider
);
548 void MockSyncStartFlare(bool* was_called
,
549 syncer::ModelType
* model_type_passed_in
,
550 syncer::ModelType model_type
) {
552 *model_type_passed_in
= model_type
;
556 // Paths to some of the fake extensions.
557 base::FilePath
good0_path() {
560 .AppendASCII("Extensions")
562 .AppendASCII("1.0.0.0");
565 base::FilePath
good1_path() {
568 .AppendASCII("Extensions")
573 base::FilePath
good2_path() {
576 .AppendASCII("Extensions")
581 void TestExternalProvider(MockExtensionProvider
* provider
,
582 Manifest::Location location
);
584 void PackCRX(const base::FilePath
& dir_path
,
585 const base::FilePath
& pem_path
,
586 const base::FilePath
& crx_path
) {
587 // Use the existing pem key, if provided.
588 base::FilePath pem_output_path
;
589 if (pem_path
.value().empty()) {
590 pem_output_path
= crx_path
.DirName().AppendASCII("temp.pem");
592 ASSERT_TRUE(base::PathExists(pem_path
));
595 ASSERT_TRUE(base::DeleteFile(crx_path
, false));
597 scoped_ptr
<ExtensionCreator
> creator(new ExtensionCreator());
598 ASSERT_TRUE(creator
->Run(dir_path
,
602 ExtensionCreator::kOverwriteCRX
));
604 ASSERT_TRUE(base::PathExists(crx_path
));
611 INSTALL_WITHOUT_LOAD
,
614 const Extension
* PackAndInstallCRX(const base::FilePath
& dir_path
,
615 const base::FilePath
& pem_path
,
616 InstallState install_state
,
617 int creation_flags
) {
618 base::FilePath crx_path
;
619 base::ScopedTempDir temp_dir
;
620 EXPECT_TRUE(temp_dir
.CreateUniqueTempDir());
621 crx_path
= temp_dir
.path().AppendASCII("temp.crx");
623 PackCRX(dir_path
, pem_path
, crx_path
);
624 return InstallCRX(crx_path
, install_state
, creation_flags
);
627 const Extension
* PackAndInstallCRX(const base::FilePath
& dir_path
,
628 const base::FilePath
& pem_path
,
629 InstallState install_state
) {
630 return PackAndInstallCRX(dir_path
, pem_path
, install_state
,
631 Extension::NO_FLAGS
);
634 const Extension
* PackAndInstallCRX(const base::FilePath
& dir_path
,
635 InstallState install_state
) {
636 return PackAndInstallCRX(dir_path
, base::FilePath(), install_state
,
637 Extension::NO_FLAGS
);
640 // Attempts to install an extension. Use INSTALL_FAILED if the installation
641 // is expected to fail.
642 // If |install_state| is INSTALL_UPDATED, and |expected_old_name| is
643 // non-empty, expects that the existing extension's title was
644 // |expected_old_name|.
645 const Extension
* InstallCRX(const base::FilePath
& path
,
646 InstallState install_state
,
648 const std::string
& expected_old_name
) {
649 InstallCRXInternal(path
, creation_flags
);
650 return VerifyCrxInstall(path
, install_state
, expected_old_name
);
653 // Attempts to install an extension. Use INSTALL_FAILED if the installation
654 // is expected to fail.
655 const Extension
* InstallCRX(const base::FilePath
& path
,
656 InstallState install_state
,
657 int creation_flags
) {
658 return InstallCRX(path
, install_state
, creation_flags
, std::string());
661 // Attempts to install an extension. Use INSTALL_FAILED if the installation
662 // is expected to fail.
663 const Extension
* InstallCRX(const base::FilePath
& path
,
664 InstallState install_state
) {
665 return InstallCRX(path
, install_state
, Extension::NO_FLAGS
);
668 const Extension
* InstallCRXFromWebStore(const base::FilePath
& path
,
669 InstallState install_state
) {
670 InstallCRXInternal(path
, Extension::FROM_WEBSTORE
);
671 return VerifyCrxInstall(path
, install_state
);
674 const Extension
* InstallCRXWithLocation(const base::FilePath
& crx_path
,
675 Manifest::Location install_location
,
676 InstallState install_state
) {
677 EXPECT_TRUE(base::PathExists(crx_path
))
678 << "Path does not exist: "<< crx_path
.value().c_str();
679 // no client (silent install)
680 scoped_refptr
<CrxInstaller
> installer(
681 CrxInstaller::CreateSilent(service()));
682 installer
->set_install_source(install_location
);
684 content::WindowedNotificationObserver
observer(
685 extensions::NOTIFICATION_CRX_INSTALLER_DONE
,
686 content::NotificationService::AllSources());
687 installer
->InstallCrx(crx_path
);
690 return VerifyCrxInstall(crx_path
, install_state
);
693 // Verifies the result of a CRX installation. Used by InstallCRX. Set the
694 // |install_state| to INSTALL_FAILED if the installation is expected to fail.
695 // Returns an Extension pointer if the install succeeded, NULL otherwise.
696 const Extension
* VerifyCrxInstall(const base::FilePath
& path
,
697 InstallState install_state
) {
698 return VerifyCrxInstall(path
, install_state
, std::string());
701 // Verifies the result of a CRX installation. Used by InstallCRX. Set the
702 // |install_state| to INSTALL_FAILED if the installation is expected to fail.
703 // If |install_state| is INSTALL_UPDATED, and |expected_old_name| is
704 // non-empty, expects that the existing extension's title was
705 // |expected_old_name|.
706 // Returns an Extension pointer if the install succeeded, NULL otherwise.
707 const Extension
* VerifyCrxInstall(const base::FilePath
& path
,
708 InstallState install_state
,
709 const std::string
& expected_old_name
) {
710 std::vector
<base::string16
> errors
= GetErrors();
711 const Extension
* extension
= NULL
;
712 if (install_state
!= INSTALL_FAILED
) {
713 if (install_state
== INSTALL_NEW
)
714 ++expected_extensions_count_
;
716 EXPECT_TRUE(installed_
) << path
.value();
717 // If and only if INSTALL_UPDATED, it should have the is_update flag.
718 EXPECT_EQ(install_state
== INSTALL_UPDATED
, was_update_
)
720 // If INSTALL_UPDATED, old_name_ should match the given string.
721 if (install_state
== INSTALL_UPDATED
&& !expected_old_name
.empty())
722 EXPECT_EQ(expected_old_name
, old_name_
);
723 EXPECT_EQ(0u, errors
.size()) << path
.value();
725 if (install_state
== INSTALL_WITHOUT_LOAD
) {
726 EXPECT_EQ(0u, loaded_
.size()) << path
.value();
728 EXPECT_EQ(1u, loaded_
.size()) << path
.value();
729 size_t actual_extension_count
=
730 registry()->enabled_extensions().size() +
731 registry()->disabled_extensions().size();
732 EXPECT_EQ(expected_extensions_count_
, actual_extension_count
) <<
734 extension
= loaded_
[0].get();
735 EXPECT_TRUE(service()->GetExtensionById(extension
->id(), false))
739 for (std::vector
<base::string16
>::iterator err
= errors
.begin();
740 err
!= errors
.end(); ++err
) {
744 EXPECT_FALSE(installed_
) << path
.value();
745 EXPECT_EQ(0u, loaded_
.size()) << path
.value();
746 EXPECT_EQ(1u, errors
.size()) << path
.value();
753 ExtensionErrorReporter::GetInstance()->ClearErrors();
766 void BlackListWebGL() {
767 static const std::string json_blacklist
=
769 " \"name\": \"gpu blacklist\",\n"
770 " \"version\": \"1.0\",\n"
774 " \"features\": [\"webgl\"]\n"
778 gpu::GPUInfo gpu_info
;
779 content::GpuDataManager::GetInstance()->InitializeForTesting(
780 json_blacklist
, gpu_info
);
783 // Grants all optional permissions stated in manifest to active permission
784 // set for extension |id|.
785 void GrantAllOptionalPermissions(const std::string
& id
) {
786 const Extension
* extension
= service()->GetInstalledExtension(id
);
787 scoped_refptr
<const PermissionSet
> all_optional_permissions
=
788 extensions::PermissionsParser::GetOptionalPermissions(extension
);
789 extensions::PermissionsUpdater
perms_updater(profile());
790 perms_updater
.AddPermissions(extension
, all_optional_permissions
.get());
793 // Helper method to set up a WindowedNotificationObserver to wait for a
794 // specific CrxInstaller to finish if we don't know the value of the
796 static bool IsCrxInstallerDone(extensions::CrxInstaller
** installer
,
797 const content::NotificationSource
& source
,
798 const content::NotificationDetails
& details
) {
799 return content::Source
<extensions::CrxInstaller
>(source
).ptr() ==
803 void PackCRXAndUpdateExtension(const std::string
& id
,
804 const base::FilePath
& dir_path
,
805 const base::FilePath
& pem_path
,
806 UpdateState expected_state
) {
807 base::ScopedTempDir temp_dir
;
808 EXPECT_TRUE(temp_dir
.CreateUniqueTempDir());
809 base::FilePath crx_path
= temp_dir
.path().AppendASCII("temp.crx");
811 PackCRX(dir_path
, pem_path
, crx_path
);
812 UpdateExtension(id
, crx_path
, expected_state
);
815 void UpdateExtension(const std::string
& id
,
816 const base::FilePath
& in_path
,
817 UpdateState expected_state
) {
818 ASSERT_TRUE(base::PathExists(in_path
));
820 // We need to copy this to a temporary location because Update() will delete
822 base::FilePath path
= temp_dir().path();
823 path
= path
.Append(in_path
.BaseName());
824 ASSERT_TRUE(base::CopyFile(in_path
, path
));
826 int previous_enabled_extension_count
=
827 registry()->enabled_extensions().size();
828 int previous_installed_extension_count
=
829 previous_enabled_extension_count
+
830 registry()->disabled_extensions().size();
832 extensions::CrxInstaller
* installer
= NULL
;
833 content::WindowedNotificationObserver
observer(
834 extensions::NOTIFICATION_CRX_INSTALLER_DONE
,
835 base::Bind(&IsCrxInstallerDone
, &installer
));
836 service()->UpdateExtension(extensions::CRXFileInfo(id
, path
), true,
842 base::RunLoop().RunUntilIdle();
844 std::vector
<base::string16
> errors
= GetErrors();
845 int error_count
= errors
.size();
846 int enabled_extension_count
= registry()->enabled_extensions().size();
847 int installed_extension_count
=
848 enabled_extension_count
+ registry()->disabled_extensions().size();
850 int expected_error_count
= (expected_state
== FAILED
) ? 1 : 0;
851 EXPECT_EQ(expected_error_count
, error_count
) << path
.value();
853 if (expected_state
<= FAILED
) {
854 EXPECT_EQ(previous_enabled_extension_count
,
855 enabled_extension_count
);
856 EXPECT_EQ(previous_installed_extension_count
,
857 installed_extension_count
);
859 int expected_installed_extension_count
=
860 (expected_state
>= INSTALLED
) ? 1 : 0;
861 int expected_enabled_extension_count
=
862 (expected_state
>= ENABLED
) ? 1 : 0;
863 EXPECT_EQ(expected_installed_extension_count
,
864 installed_extension_count
);
865 EXPECT_EQ(expected_enabled_extension_count
,
866 enabled_extension_count
);
869 // Update() should the temporary input file.
870 EXPECT_FALSE(base::PathExists(path
));
873 void TerminateExtension(const std::string
& id
) {
874 const Extension
* extension
= service()->GetInstalledExtension(id
);
879 service()->TrackTerminatedExtensionForTest(extension
);
882 testing::AssertionResult
IsBlocked(const std::string
& id
) {
883 scoped_ptr
<extensions::ExtensionSet
> all_unblocked_extensions
=
884 registry()->GenerateInstalledExtensionsSet(
885 ExtensionRegistry::EVERYTHING
& ~ExtensionRegistry::BLOCKED
);
886 if (all_unblocked_extensions
.get()->Contains(id
))
887 return testing::AssertionFailure() << id
<< " is still unblocked!";
888 if (!registry()->blocked_extensions().Contains(id
))
889 return testing::AssertionFailure() << id
<< " is not blocked!";
890 return testing::AssertionSuccess();
893 // Helper method to test that an extension moves through being blocked and
894 // unblocked as appropriate for its type.
895 void AssertExtensionBlocksAndUnblocks(
896 bool should_block
, const std::string extension_id
) {
897 // Assume we start in an unblocked state.
898 EXPECT_FALSE(IsBlocked(extension_id
));
900 // Block the extensions.
901 service()->BlockAllExtensions();
902 base::RunLoop().RunUntilIdle();
905 ASSERT_TRUE(IsBlocked(extension_id
));
907 ASSERT_FALSE(IsBlocked(extension_id
));
909 service()->UnblockAllExtensions();
910 base::RunLoop().RunUntilIdle();
912 ASSERT_FALSE(IsBlocked(extension_id
));
915 size_t GetPrefKeyCount() {
916 const base::DictionaryValue
* dict
=
917 profile()->GetPrefs()->GetDictionary("extensions.settings");
925 void UninstallExtension(const std::string
& id
, bool use_helper
) {
926 UninstallExtension(id
, use_helper
, Extension::ENABLED
);
929 void UninstallExtension(const std::string
& id
, bool use_helper
,
930 Extension::State expected_state
) {
931 // Verify that the extension is installed.
932 base::FilePath extension_path
= extensions_install_dir().AppendASCII(id
);
933 EXPECT_TRUE(base::PathExists(extension_path
));
934 size_t pref_key_count
= GetPrefKeyCount();
935 EXPECT_GT(pref_key_count
, 0u);
936 ValidateIntegerPref(id
, "state", expected_state
);
940 EXPECT_TRUE(ExtensionService::UninstallExtensionHelper(
941 service(), id
, extensions::UNINSTALL_REASON_FOR_TESTING
));
943 EXPECT_TRUE(service()->UninstallExtension(
945 extensions::UNINSTALL_REASON_FOR_TESTING
,
946 base::Bind(&base::DoNothing
),
949 --expected_extensions_count_
;
951 // We should get an unload notification.
952 EXPECT_FALSE(unloaded_id_
.empty());
953 EXPECT_EQ(id
, unloaded_id_
);
955 // Verify uninstalled state.
956 size_t new_pref_key_count
= GetPrefKeyCount();
957 if (new_pref_key_count
== pref_key_count
) {
958 ValidateIntegerPref(id
, "state",
959 Extension::EXTERNAL_EXTENSION_UNINSTALLED
);
961 EXPECT_EQ(new_pref_key_count
, pref_key_count
- 1);
964 // The extension should not be in the service anymore.
965 EXPECT_FALSE(service()->GetInstalledExtension(id
));
966 base::RunLoop().RunUntilIdle();
968 // The directory should be gone.
969 EXPECT_FALSE(base::PathExists(extension_path
));
972 void ValidatePrefKeyCount(size_t count
) {
973 EXPECT_EQ(count
, GetPrefKeyCount());
976 testing::AssertionResult
ValidateBooleanPref(
977 const std::string
& extension_id
,
978 const std::string
& pref_path
,
980 std::string msg
= "while checking: ";
985 msg
+= expected_val
? "true" : "false";
987 PrefService
* prefs
= profile()->GetPrefs();
988 const base::DictionaryValue
* dict
=
989 prefs
->GetDictionary("extensions.settings");
991 return testing::AssertionFailure()
992 << "extension.settings does not exist " << msg
;
995 const base::DictionaryValue
* pref
= NULL
;
996 if (!dict
->GetDictionary(extension_id
, &pref
)) {
997 return testing::AssertionFailure()
998 << "extension pref does not exist " << msg
;
1002 if (!pref
->GetBoolean(pref_path
, &val
)) {
1003 return testing::AssertionFailure()
1004 << pref_path
<< " pref not found " << msg
;
1007 return expected_val
== val
1008 ? testing::AssertionSuccess()
1009 : testing::AssertionFailure() << "base::Value is incorrect " << msg
;
1012 bool IsPrefExist(const std::string
& extension_id
,
1013 const std::string
& pref_path
) {
1014 const base::DictionaryValue
* dict
=
1015 profile()->GetPrefs()->GetDictionary("extensions.settings");
1016 if (dict
== NULL
) return false;
1017 const base::DictionaryValue
* pref
= NULL
;
1018 if (!dict
->GetDictionary(extension_id
, &pref
)) {
1025 if (!pref
->GetBoolean(pref_path
, &val
)) {
1031 void ValidateIntegerPref(const std::string
& extension_id
,
1032 const std::string
& pref_path
,
1034 std::string msg
= " while checking: ";
1035 msg
+= extension_id
;
1039 msg
+= base::IntToString(expected_val
);
1041 PrefService
* prefs
= profile()->GetPrefs();
1042 const base::DictionaryValue
* dict
=
1043 prefs
->GetDictionary("extensions.settings");
1044 ASSERT_TRUE(dict
!= NULL
) << msg
;
1045 const base::DictionaryValue
* pref
= NULL
;
1046 ASSERT_TRUE(dict
->GetDictionary(extension_id
, &pref
)) << msg
;
1047 EXPECT_TRUE(pref
!= NULL
) << msg
;
1049 ASSERT_TRUE(pref
->GetInteger(pref_path
, &val
)) << msg
;
1050 EXPECT_EQ(expected_val
, val
) << msg
;
1053 void ValidateStringPref(const std::string
& extension_id
,
1054 const std::string
& pref_path
,
1055 const std::string
& expected_val
) {
1056 std::string msg
= " while checking: ";
1057 msg
+= extension_id
;
1058 msg
+= ".manifest.";
1061 msg
+= expected_val
;
1063 const base::DictionaryValue
* dict
=
1064 profile()->GetPrefs()->GetDictionary("extensions.settings");
1065 ASSERT_TRUE(dict
!= NULL
) << msg
;
1066 const base::DictionaryValue
* pref
= NULL
;
1067 std::string manifest_path
= extension_id
+ ".manifest";
1068 ASSERT_TRUE(dict
->GetDictionary(manifest_path
, &pref
)) << msg
;
1069 EXPECT_TRUE(pref
!= NULL
) << msg
;
1071 ASSERT_TRUE(pref
->GetString(pref_path
, &val
)) << msg
;
1072 EXPECT_EQ(expected_val
, val
) << msg
;
1075 void SetPref(const std::string
& extension_id
,
1076 const std::string
& pref_path
,
1078 const std::string
& msg
) {
1079 DictionaryPrefUpdate
update(profile()->GetPrefs(), "extensions.settings");
1080 base::DictionaryValue
* dict
= update
.Get();
1081 ASSERT_TRUE(dict
!= NULL
) << msg
;
1082 base::DictionaryValue
* pref
= NULL
;
1083 ASSERT_TRUE(dict
->GetDictionary(extension_id
, &pref
)) << msg
;
1084 EXPECT_TRUE(pref
!= NULL
) << msg
;
1085 pref
->Set(pref_path
, value
);
1088 void SetPrefInteg(const std::string
& extension_id
,
1089 const std::string
& pref_path
,
1091 std::string msg
= " while setting: ";
1092 msg
+= extension_id
;
1096 msg
+= base::IntToString(value
);
1098 SetPref(extension_id
, pref_path
, new base::FundamentalValue(value
), msg
);
1101 void SetPrefBool(const std::string
& extension_id
,
1102 const std::string
& pref_path
,
1104 std::string msg
= " while setting: ";
1105 msg
+= extension_id
+ " " + pref_path
;
1107 msg
+= (value
? "true" : "false");
1109 SetPref(extension_id
, pref_path
, new base::FundamentalValue(value
), msg
);
1112 void ClearPref(const std::string
& extension_id
,
1113 const std::string
& pref_path
) {
1114 std::string msg
= " while clearing: ";
1115 msg
+= extension_id
+ " " + pref_path
;
1117 DictionaryPrefUpdate
update(profile()->GetPrefs(), "extensions.settings");
1118 base::DictionaryValue
* dict
= update
.Get();
1119 ASSERT_TRUE(dict
!= NULL
) << msg
;
1120 base::DictionaryValue
* pref
= NULL
;
1121 ASSERT_TRUE(dict
->GetDictionary(extension_id
, &pref
)) << msg
;
1122 EXPECT_TRUE(pref
!= NULL
) << msg
;
1123 pref
->Remove(pref_path
, NULL
);
1126 void SetPrefStringSet(const std::string
& extension_id
,
1127 const std::string
& pref_path
,
1128 const std::set
<std::string
>& value
) {
1129 std::string msg
= " while setting: ";
1130 msg
+= extension_id
+ " " + pref_path
;
1132 base::ListValue
* list_value
= new base::ListValue();
1133 for (std::set
<std::string
>::const_iterator iter
= value
.begin();
1134 iter
!= value
.end(); ++iter
)
1135 list_value
->Append(new base::StringValue(*iter
));
1137 SetPref(extension_id
, pref_path
, list_value
, msg
);
1140 void InitPluginService() {
1141 #if defined(ENABLE_PLUGINS)
1142 PluginService::GetInstance()->Init();
1146 void InitializeExtensionSyncService() {
1147 extension_sync_service_
.reset(new ExtensionSyncService(
1148 profile(), ExtensionPrefs::Get(browser_context()), service()));
1151 void InitializeEmptyExtensionServiceWithTestingPrefs() {
1152 ExtensionServiceTestBase::ExtensionServiceInitParams params
=
1153 CreateDefaultInitParams();
1154 params
.pref_file
= base::FilePath();
1155 InitializeExtensionService(params
);
1158 extensions::ManagementPolicy
* GetManagementPolicy() {
1159 return ExtensionSystem::Get(browser_context())->management_policy();
1162 ExtensionSyncService
* extension_sync_service() {
1163 return extension_sync_service_
.get();
1167 typedef extensions::ExtensionManagementPrefUpdater
<TestingPrefServiceSyncable
>
1168 ManagementPrefUpdater
;
1169 scoped_ptr
<ExtensionSyncService
> extension_sync_service_
;
1170 extensions::ExtensionList loaded_
;
1171 std::string unloaded_id_
;
1172 UnloadedExtensionInfo::Reason unloaded_reason_
;
1173 const Extension
* installed_
;
1175 std::string old_name_
;
1176 FeatureSwitch::ScopedOverride override_external_install_prompt_
;
1179 // Create a CrxInstaller and install the CRX file.
1180 // Instead of calling this method yourself, use InstallCRX(), which does extra
1182 void InstallCRXInternal(const base::FilePath
& crx_path
) {
1183 InstallCRXInternal(crx_path
, Extension::NO_FLAGS
);
1186 void InstallCRXInternal(const base::FilePath
& crx_path
, int creation_flags
) {
1187 ASSERT_TRUE(base::PathExists(crx_path
))
1188 << "Path does not exist: "<< crx_path
.value().c_str();
1189 scoped_refptr
<CrxInstaller
> installer(
1190 CrxInstaller::CreateSilent(service()));
1191 installer
->set_creation_flags(creation_flags
);
1192 if (!(creation_flags
& Extension::WAS_INSTALLED_BY_DEFAULT
))
1193 installer
->set_allow_silent_install(true);
1195 content::WindowedNotificationObserver
observer(
1196 extensions::NOTIFICATION_CRX_INSTALLER_DONE
,
1197 content::Source
<extensions::CrxInstaller
>(installer
.get()));
1199 installer
->InstallCrx(crx_path
);
1204 size_t expected_extensions_count_
;
1205 content::NotificationRegistrar registrar_
;
1208 // Receives notifications from a PackExtensionJob, indicating either that
1209 // packing succeeded or that there was some error.
1210 class PackExtensionTestClient
: public extensions::PackExtensionJob::Client
{
1212 PackExtensionTestClient(const base::FilePath
& expected_crx_path
,
1213 const base::FilePath
& expected_private_key_path
);
1214 void OnPackSuccess(const base::FilePath
& crx_path
,
1215 const base::FilePath
& private_key_path
) override
;
1216 void OnPackFailure(const std::string
& error_message
,
1217 ExtensionCreator::ErrorType type
) override
;
1220 const base::FilePath expected_crx_path_
;
1221 const base::FilePath expected_private_key_path_
;
1222 DISALLOW_COPY_AND_ASSIGN(PackExtensionTestClient
);
1225 PackExtensionTestClient::PackExtensionTestClient(
1226 const base::FilePath
& expected_crx_path
,
1227 const base::FilePath
& expected_private_key_path
)
1228 : expected_crx_path_(expected_crx_path
),
1229 expected_private_key_path_(expected_private_key_path
) {}
1231 // If packing succeeded, we make sure that the package names match our
1233 void PackExtensionTestClient::OnPackSuccess(
1234 const base::FilePath
& crx_path
,
1235 const base::FilePath
& private_key_path
) {
1236 // We got the notification and processed it; we don't expect any further tasks
1237 // to be posted to the current thread, so we should stop blocking and continue
1238 // on with the rest of the test.
1239 // This call to |Quit()| matches the call to |Run()| in the
1240 // |PackPunctuatedExtension| test.
1241 base::MessageLoop::current()->Quit();
1242 EXPECT_EQ(expected_crx_path_
.value(), crx_path
.value());
1243 EXPECT_EQ(expected_private_key_path_
.value(), private_key_path
.value());
1244 ASSERT_TRUE(base::PathExists(private_key_path
));
1247 // The tests are designed so that we never expect to see a packing error.
1248 void PackExtensionTestClient::OnPackFailure(const std::string
& error_message
,
1249 ExtensionCreator::ErrorType type
) {
1250 if (type
== ExtensionCreator::kCRXExists
)
1251 FAIL() << "Packing should not fail.";
1253 FAIL() << "Existing CRX should have been overwritten.";
1256 // Test loading good extensions from the profile directory.
1257 TEST_F(ExtensionServiceTest
, LoadAllExtensionsFromDirectorySuccess
) {
1258 InitPluginService();
1259 InitializeGoodInstalledExtensionService();
1262 uint32 expected_num_extensions
= 3u;
1263 ASSERT_EQ(expected_num_extensions
, loaded_
.size());
1265 EXPECT_EQ(std::string(good0
), loaded_
[0]->id());
1266 EXPECT_EQ(std::string("My extension 1"),
1267 loaded_
[0]->name());
1268 EXPECT_EQ(std::string("The first extension that I made."),
1269 loaded_
[0]->description());
1270 EXPECT_EQ(Manifest::INTERNAL
, loaded_
[0]->location());
1271 EXPECT_TRUE(service()->GetExtensionById(loaded_
[0]->id(), false));
1272 EXPECT_EQ(expected_num_extensions
, registry()->enabled_extensions().size());
1274 ValidatePrefKeyCount(3);
1275 ValidateIntegerPref(good0
, "state", Extension::ENABLED
);
1276 ValidateIntegerPref(good0
, "location", Manifest::INTERNAL
);
1277 ValidateIntegerPref(good1
, "state", Extension::ENABLED
);
1278 ValidateIntegerPref(good1
, "location", Manifest::INTERNAL
);
1279 ValidateIntegerPref(good2
, "state", Extension::ENABLED
);
1280 ValidateIntegerPref(good2
, "location", Manifest::INTERNAL
);
1282 URLPatternSet expected_patterns
;
1283 AddPattern(&expected_patterns
, "file:///*");
1284 AddPattern(&expected_patterns
, "http://*.google.com/*");
1285 AddPattern(&expected_patterns
, "https://*.google.com/*");
1286 const Extension
* extension
= loaded_
[0].get();
1287 const extensions::UserScriptList
& scripts
=
1288 extensions::ContentScriptsInfo::GetContentScripts(extension
);
1289 ASSERT_EQ(2u, scripts
.size());
1290 EXPECT_EQ(expected_patterns
, scripts
[0].url_patterns());
1291 EXPECT_EQ(2u, scripts
[0].js_scripts().size());
1292 ExtensionResource
resource00(extension
->id(),
1293 scripts
[0].js_scripts()[0].extension_root(),
1294 scripts
[0].js_scripts()[0].relative_path());
1295 base::FilePath expected_path
=
1296 base::MakeAbsoluteFilePath(extension
->path().AppendASCII("script1.js"));
1297 EXPECT_TRUE(resource00
.ComparePathWithDefault(expected_path
));
1298 ExtensionResource
resource01(extension
->id(),
1299 scripts
[0].js_scripts()[1].extension_root(),
1300 scripts
[0].js_scripts()[1].relative_path());
1302 base::MakeAbsoluteFilePath(extension
->path().AppendASCII("script2.js"));
1303 EXPECT_TRUE(resource01
.ComparePathWithDefault(expected_path
));
1304 EXPECT_TRUE(!extensions::PluginInfo::HasPlugins(extension
));
1305 EXPECT_EQ(1u, scripts
[1].url_patterns().patterns().size());
1306 EXPECT_EQ("http://*.news.com/*",
1307 scripts
[1].url_patterns().begin()->GetAsString());
1308 ExtensionResource
resource10(extension
->id(),
1309 scripts
[1].js_scripts()[0].extension_root(),
1310 scripts
[1].js_scripts()[0].relative_path());
1312 extension
->path().AppendASCII("js_files").AppendASCII("script3.js");
1313 expected_path
= base::MakeAbsoluteFilePath(expected_path
);
1314 EXPECT_TRUE(resource10
.ComparePathWithDefault(expected_path
));
1316 expected_patterns
.ClearPatterns();
1317 AddPattern(&expected_patterns
, "http://*.google.com/*");
1318 AddPattern(&expected_patterns
, "https://*.google.com/*");
1321 extension
->permissions_data()->active_permissions()->explicit_hosts());
1323 EXPECT_EQ(std::string(good1
), loaded_
[1]->id());
1324 EXPECT_EQ(std::string("My extension 2"), loaded_
[1]->name());
1325 EXPECT_EQ(std::string(), loaded_
[1]->description());
1326 EXPECT_EQ(loaded_
[1]->GetResourceURL("background.html"),
1327 extensions::BackgroundInfo::GetBackgroundURL(loaded_
[1].get()));
1329 extensions::ContentScriptsInfo::GetContentScripts(loaded_
[1].get())
1332 // We don't parse the plugins section on Chrome OS.
1333 #if defined(OS_CHROMEOS)
1334 EXPECT_TRUE(!extensions::PluginInfo::HasPlugins(loaded_
[1].get()));
1336 ASSERT_TRUE(extensions::PluginInfo::HasPlugins(loaded_
[1].get()));
1337 const std::vector
<extensions::PluginInfo
>* plugins
=
1338 extensions::PluginInfo::GetPlugins(loaded_
[1].get());
1339 ASSERT_TRUE(plugins
);
1340 ASSERT_EQ(2u, plugins
->size());
1341 EXPECT_EQ(loaded_
[1]->path().AppendASCII("content_plugin.dll").value(),
1342 plugins
->at(0).path
.value());
1343 EXPECT_TRUE(plugins
->at(0).is_public
);
1344 EXPECT_EQ(loaded_
[1]->path().AppendASCII("extension_plugin.dll").value(),
1345 plugins
->at(1).path
.value());
1346 EXPECT_FALSE(plugins
->at(1).is_public
);
1349 EXPECT_EQ(Manifest::INTERNAL
, loaded_
[1]->location());
1351 int index
= expected_num_extensions
- 1;
1352 EXPECT_EQ(std::string(good2
), loaded_
[index
]->id());
1353 EXPECT_EQ(std::string("My extension 3"), loaded_
[index
]->name());
1354 EXPECT_EQ(std::string(), loaded_
[index
]->description());
1356 extensions::ContentScriptsInfo::GetContentScripts(
1357 loaded_
[index
].get()).size());
1358 EXPECT_EQ(Manifest::INTERNAL
, loaded_
[index
]->location());
1361 // Test loading bad extensions from the profile directory.
1362 TEST_F(ExtensionServiceTest
, LoadAllExtensionsFromDirectoryFail
) {
1363 // Initialize the test dir with a bad Preferences/extensions.
1364 base::FilePath source_install_dir
=
1365 data_dir().AppendASCII("bad").AppendASCII("Extensions");
1366 base::FilePath pref_path
=
1367 source_install_dir
.DirName().Append(chrome::kPreferencesFilename
);
1369 InitializeInstalledExtensionService(pref_path
, source_install_dir
);
1373 ASSERT_EQ(4u, GetErrors().size());
1374 ASSERT_EQ(0u, loaded_
.size());
1376 EXPECT_TRUE(MatchPattern(base::UTF16ToUTF8(GetErrors()[0]),
1377 l10n_util::GetStringUTF8(IDS_EXTENSIONS_LOAD_ERROR_MESSAGE
) + " *. " +
1378 extensions::manifest_errors::kManifestUnreadable
)) <<
1379 base::UTF16ToUTF8(GetErrors()[0]);
1381 EXPECT_TRUE(MatchPattern(base::UTF16ToUTF8(GetErrors()[1]),
1382 l10n_util::GetStringUTF8(IDS_EXTENSIONS_LOAD_ERROR_MESSAGE
) + " *. " +
1383 extensions::manifest_errors::kManifestUnreadable
)) <<
1384 base::UTF16ToUTF8(GetErrors()[1]);
1386 EXPECT_TRUE(MatchPattern(base::UTF16ToUTF8(GetErrors()[2]),
1387 l10n_util::GetStringUTF8(IDS_EXTENSIONS_LOAD_ERROR_MESSAGE
) + " *. " +
1388 extensions::manifest_errors::kMissingFile
)) <<
1389 base::UTF16ToUTF8(GetErrors()[2]);
1391 EXPECT_TRUE(MatchPattern(base::UTF16ToUTF8(GetErrors()[3]),
1392 l10n_util::GetStringUTF8(IDS_EXTENSIONS_LOAD_ERROR_MESSAGE
) + " *. " +
1393 extensions::manifest_errors::kManifestUnreadable
)) <<
1394 base::UTF16ToUTF8(GetErrors()[3]);
1397 // Test various cases for delayed install because of missing imports.
1398 TEST_F(ExtensionServiceTest
, PendingImports
) {
1399 InitPluginService();
1401 base::FilePath source_install_dir
=
1402 data_dir().AppendASCII("pending_updates_with_imports").AppendASCII(
1404 base::FilePath pref_path
=
1405 source_install_dir
.DirName().Append(chrome::kPreferencesFilename
);
1407 InitializeInstalledExtensionService(pref_path
, source_install_dir
);
1409 // Verify there are no pending extensions initially.
1410 EXPECT_FALSE(service()->pending_extension_manager()->HasPendingExtensions());
1413 // Wait for GarbageCollectExtensions task to complete.
1414 base::RunLoop().RunUntilIdle();
1416 // These extensions are used by the extensions we test below, they must be
1418 EXPECT_TRUE(base::PathExists(extensions_install_dir().AppendASCII(
1419 "bjafgdebaacbbbecmhlhpofkepfkgcpa/1.0")));
1420 EXPECT_TRUE(base::PathExists(extensions_install_dir().AppendASCII(
1421 "hpiknbiabeeppbpihjehijgoemciehgk/2")));
1423 // Each of these extensions should have been rejected because of dependencies
1424 // that cannot be satisfied.
1425 ExtensionPrefs
* prefs
= ExtensionPrefs::Get(profile());
1427 prefs
->GetDelayedInstallInfo("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"));
1429 prefs
->GetInstalledExtensionInfo("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"));
1431 prefs
->GetDelayedInstallInfo("bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"));
1433 prefs
->GetInstalledExtensionInfo("bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"));
1435 prefs
->GetDelayedInstallInfo("cccccccccccccccccccccccccccccccc"));
1437 prefs
->GetInstalledExtensionInfo("cccccccccccccccccccccccccccccccc"));
1439 // Make sure the import started for the extension with a dependency.
1441 prefs
->GetDelayedInstallInfo("behllobkkfkfnphdnhnkndlbkcpglgmj"));
1442 EXPECT_EQ(ExtensionPrefs::DELAY_REASON_WAIT_FOR_IMPORTS
,
1443 prefs
->GetDelayedInstallReason("behllobkkfkfnphdnhnkndlbkcpglgmj"));
1445 EXPECT_FALSE(base::PathExists(extensions_install_dir().AppendASCII(
1446 "behllobkkfkfnphdnhnkndlbkcpglgmj/1.0.0.0")));
1448 EXPECT_TRUE(service()->pending_extension_manager()->HasPendingExtensions());
1449 std::string
pending_id("eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee");
1450 EXPECT_TRUE(service()->pending_extension_manager()->IsIdPending(pending_id
));
1451 // Remove it because we are not testing the pending extension manager's
1452 // ability to download and install extensions.
1453 EXPECT_TRUE(service()->pending_extension_manager()->Remove(pending_id
));
1456 // Test installing extensions. This test tries to install few extensions using
1457 // crx files. If you need to change those crx files, feel free to repackage
1458 // them, throw away the key used and change the id's above.
1459 TEST_F(ExtensionServiceTest
, InstallExtension
) {
1460 InitializeEmptyExtensionService();
1462 // Extensions not enabled.
1463 service()->set_extensions_enabled(false);
1464 base::FilePath path
= data_dir().AppendASCII("good.crx");
1465 InstallCRX(path
, INSTALL_FAILED
);
1466 service()->set_extensions_enabled(true);
1468 ValidatePrefKeyCount(0);
1470 // A simple extension that should install without error.
1471 path
= data_dir().AppendASCII("good.crx");
1472 InstallCRX(path
, INSTALL_NEW
);
1473 // TODO(erikkay): verify the contents of the installed extension.
1476 ValidatePrefKeyCount(++pref_count
);
1477 ValidateIntegerPref(good_crx
, "state", Extension::ENABLED
);
1478 ValidateIntegerPref(good_crx
, "location", Manifest::INTERNAL
);
1480 // An extension with page actions.
1481 path
= data_dir().AppendASCII("page_action.crx");
1482 InstallCRX(path
, INSTALL_NEW
);
1483 ValidatePrefKeyCount(++pref_count
);
1484 ValidateIntegerPref(page_action
, "state", Extension::ENABLED
);
1485 ValidateIntegerPref(page_action
, "location", Manifest::INTERNAL
);
1488 path
= data_dir().AppendASCII("bad_signature.crx");
1489 InstallCRX(path
, INSTALL_FAILED
);
1490 ValidatePrefKeyCount(pref_count
);
1492 // 0-length extension file.
1493 path
= data_dir().AppendASCII("not_an_extension.crx");
1494 InstallCRX(path
, INSTALL_FAILED
);
1495 ValidatePrefKeyCount(pref_count
);
1497 // Bad magic number.
1498 path
= data_dir().AppendASCII("bad_magic.crx");
1499 InstallCRX(path
, INSTALL_FAILED
);
1500 ValidatePrefKeyCount(pref_count
);
1502 // Packed extensions may have folders or files that have underscores.
1503 // This will only cause a warning, rather than a fatal error.
1504 path
= data_dir().AppendASCII("bad_underscore.crx");
1505 InstallCRX(path
, INSTALL_NEW
);
1506 ValidatePrefKeyCount(++pref_count
);
1508 // A test for an extension with a 2048-bit public key.
1509 path
= data_dir().AppendASCII("good2048.crx");
1510 InstallCRX(path
, INSTALL_NEW
);
1511 ValidatePrefKeyCount(++pref_count
);
1512 ValidateIntegerPref(good2048
, "state", Extension::ENABLED
);
1513 ValidateIntegerPref(good2048
, "location", Manifest::INTERNAL
);
1515 // TODO(erikkay): add more tests for many of the failure cases.
1516 // TODO(erikkay): add tests for upgrade cases.
1519 struct MockExtensionRegistryObserver
1520 : public extensions::ExtensionRegistryObserver
{
1521 void OnExtensionWillBeInstalled(content::BrowserContext
* browser_context
,
1522 const Extension
* extension
,
1524 bool from_ephemeral
,
1525 const std::string
& old_name
) override
{
1526 last_extension_installed
= extension
->id();
1529 void OnExtensionUninstalled(content::BrowserContext
* browser_context
,
1530 const Extension
* extension
,
1531 extensions::UninstallReason reason
) override
{
1532 last_extension_uninstalled
= extension
->id();
1535 std::string last_extension_installed
;
1536 std::string last_extension_uninstalled
;
1539 // Test that correct notifications are sent to ExtensionRegistryObserver on
1540 // extension install and uninstall.
1541 TEST_F(ExtensionServiceTest
, InstallObserverNotified
) {
1542 InitializeEmptyExtensionService();
1544 extensions::ExtensionRegistry
* registry(
1545 extensions::ExtensionRegistry::Get(profile()));
1546 MockExtensionRegistryObserver observer
;
1547 registry
->AddObserver(&observer
);
1549 // A simple extension that should install without error.
1550 ASSERT_TRUE(observer
.last_extension_installed
.empty());
1551 base::FilePath path
= data_dir().AppendASCII("good.crx");
1552 InstallCRX(path
, INSTALL_NEW
);
1553 ASSERT_EQ(good_crx
, observer
.last_extension_installed
);
1555 // Uninstall the extension.
1556 ASSERT_TRUE(observer
.last_extension_uninstalled
.empty());
1557 UninstallExtension(good_crx
, false);
1558 ASSERT_EQ(good_crx
, observer
.last_extension_uninstalled
);
1560 registry
->RemoveObserver(&observer
);
1563 // Tests that flags passed to OnExternalExtensionFileFound() make it to the
1564 // extension object.
1565 TEST_F(ExtensionServiceTest
, InstallingExternalExtensionWithFlags
) {
1566 const char kPrefFromBookmark
[] = "from_bookmark";
1568 InitializeEmptyExtensionService();
1570 base::FilePath path
= data_dir().AppendASCII("good.crx");
1571 service()->set_extensions_enabled(true);
1573 // Register and install an external extension.
1574 Version
version("1.0.0.0");
1575 content::WindowedNotificationObserver
observer(
1576 extensions::NOTIFICATION_CRX_INSTALLER_DONE
,
1577 content::NotificationService::AllSources());
1578 if (service()->OnExternalExtensionFileFound(
1582 Manifest::EXTERNAL_PREF
,
1583 Extension::FROM_BOOKMARK
,
1584 false /* mark_acknowledged */,
1585 false /* install_immediately */)) {
1589 const Extension
* extension
= service()->GetExtensionById(good_crx
, false);
1590 ASSERT_TRUE(extension
);
1591 ASSERT_TRUE(extension
->from_bookmark());
1592 ASSERT_TRUE(ValidateBooleanPref(good_crx
, kPrefFromBookmark
, true));
1594 // Upgrade to version 2.0, the flag should be preserved.
1595 path
= data_dir().AppendASCII("good2.crx");
1596 UpdateExtension(good_crx
, path
, ENABLED
);
1597 ASSERT_TRUE(ValidateBooleanPref(good_crx
, kPrefFromBookmark
, true));
1598 extension
= service()->GetExtensionById(good_crx
, false);
1599 ASSERT_TRUE(extension
);
1600 ASSERT_TRUE(extension
->from_bookmark());
1603 // Test the handling of Extension::EXTERNAL_EXTENSION_UNINSTALLED
1604 TEST_F(ExtensionServiceTest
, UninstallingExternalExtensions
) {
1605 InitializeEmptyExtensionService();
1607 base::FilePath path
= data_dir().AppendASCII("good.crx");
1608 service()->set_extensions_enabled(true);
1610 // Install an external extension.
1611 Version
version("1.0.0.0");
1612 content::WindowedNotificationObserver
observer(
1613 extensions::NOTIFICATION_CRX_INSTALLER_DONE
,
1614 content::NotificationService::AllSources());
1615 if (service()->OnExternalExtensionFileFound(good_crx
,
1618 Manifest::EXTERNAL_PREF
,
1619 Extension::NO_FLAGS
,
1625 ASSERT_TRUE(service()->GetExtensionById(good_crx
, false));
1627 // Uninstall it and check that its killbit gets set.
1628 UninstallExtension(good_crx
, false);
1629 ValidateIntegerPref(good_crx
, "state",
1630 Extension::EXTERNAL_EXTENSION_UNINSTALLED
);
1632 // Try to re-install it externally. This should fail because of the killbit.
1633 service()->OnExternalExtensionFileFound(good_crx
,
1636 Manifest::EXTERNAL_PREF
,
1637 Extension::NO_FLAGS
,
1640 base::RunLoop().RunUntilIdle();
1641 ASSERT_TRUE(NULL
== service()->GetExtensionById(good_crx
, false));
1642 ValidateIntegerPref(good_crx
, "state",
1643 Extension::EXTERNAL_EXTENSION_UNINSTALLED
);
1645 version
= Version("1.0.0.1");
1646 // Repeat the same thing with a newer version of the extension.
1647 path
= data_dir().AppendASCII("good2.crx");
1648 service()->OnExternalExtensionFileFound(good_crx
,
1651 Manifest::EXTERNAL_PREF
,
1652 Extension::NO_FLAGS
,
1655 base::RunLoop().RunUntilIdle();
1656 ASSERT_TRUE(NULL
== service()->GetExtensionById(good_crx
, false));
1657 ValidateIntegerPref(good_crx
, "state",
1658 Extension::EXTERNAL_EXTENSION_UNINSTALLED
);
1660 // Try adding the same extension from an external update URL.
1661 ASSERT_FALSE(service()->pending_extension_manager()->AddFromExternalUpdateUrl(
1664 GURL("http:://fake.update/url"),
1665 Manifest::EXTERNAL_PREF_DOWNLOAD
,
1666 Extension::NO_FLAGS
,
1669 ASSERT_FALSE(service()->pending_extension_manager()->IsIdPending(good_crx
));
1672 // Test that uninstalling an external extension does not crash when
1673 // the extension could not be loaded.
1674 // This extension shown in preferences file requires an experimental permission.
1675 // It could not be loaded without such permission.
1676 TEST_F(ExtensionServiceTest
, UninstallingNotLoadedExtension
) {
1677 base::FilePath source_install_dir
=
1678 data_dir().AppendASCII("good").AppendASCII("Extensions");
1679 // The preference contains an external extension
1680 // that requires 'experimental' permission.
1681 base::FilePath pref_path
= source_install_dir
1683 .AppendASCII("PreferencesExperimental");
1685 // Aforementioned extension will not be loaded if
1686 // there is no '--enable-experimental-extension-apis' command line flag.
1687 InitializeInstalledExtensionService(pref_path
, source_install_dir
);
1691 // Check and try to uninstall it.
1692 // If we don't check whether the extension is loaded before we uninstall it
1693 // in CheckExternalUninstall, a crash will happen here because we will get or
1694 // dereference a NULL pointer (extension) inside UninstallExtension.
1695 MockExtensionProvider
provider(NULL
, Manifest::EXTERNAL_REGISTRY
);
1696 service()->OnExternalProviderReady(&provider
);
1699 // Test that external extensions with incorrect IDs are not installed.
1700 TEST_F(ExtensionServiceTest
, FailOnWrongId
) {
1701 InitializeEmptyExtensionService();
1702 base::FilePath path
= data_dir().AppendASCII("good.crx");
1703 service()->set_extensions_enabled(true);
1705 Version
version("1.0.0.0");
1707 const std::string wrong_id
= all_zero
;
1708 const std::string correct_id
= good_crx
;
1709 ASSERT_NE(correct_id
, wrong_id
);
1711 // Install an external extension with an ID from the external
1712 // source that is not equal to the ID in the extension manifest.
1713 content::WindowedNotificationObserver
observer(
1714 extensions::NOTIFICATION_CRX_INSTALLER_DONE
,
1715 content::NotificationService::AllSources());
1716 service()->OnExternalExtensionFileFound(wrong_id
,
1719 Manifest::EXTERNAL_PREF
,
1720 Extension::NO_FLAGS
,
1725 ASSERT_FALSE(service()->GetExtensionById(good_crx
, false));
1727 // Try again with the right ID. Expect success.
1728 content::WindowedNotificationObserver
observer2(
1729 extensions::NOTIFICATION_CRX_INSTALLER_DONE
,
1730 content::NotificationService::AllSources());
1731 if (service()->OnExternalExtensionFileFound(correct_id
,
1734 Manifest::EXTERNAL_PREF
,
1735 Extension::NO_FLAGS
,
1740 ASSERT_TRUE(service()->GetExtensionById(good_crx
, false));
1743 // Test that external extensions with incorrect versions are not installed.
1744 TEST_F(ExtensionServiceTest
, FailOnWrongVersion
) {
1745 InitializeEmptyExtensionService();
1746 base::FilePath path
= data_dir().AppendASCII("good.crx");
1747 service()->set_extensions_enabled(true);
1749 // Install an external extension with a version from the external
1750 // source that is not equal to the version in the extension manifest.
1751 Version
wrong_version("1.2.3.4");
1752 content::WindowedNotificationObserver
observer(
1753 extensions::NOTIFICATION_CRX_INSTALLER_DONE
,
1754 content::NotificationService::AllSources());
1755 service()->OnExternalExtensionFileFound(good_crx
,
1758 Manifest::EXTERNAL_PREF
,
1759 Extension::NO_FLAGS
,
1764 ASSERT_FALSE(service()->GetExtensionById(good_crx
, false));
1766 // Try again with the right version. Expect success.
1767 service()->pending_extension_manager()->Remove(good_crx
);
1768 Version
correct_version("1.0.0.0");
1769 content::WindowedNotificationObserver
observer2(
1770 extensions::NOTIFICATION_CRX_INSTALLER_DONE
,
1771 content::NotificationService::AllSources());
1772 if (service()->OnExternalExtensionFileFound(good_crx
,
1775 Manifest::EXTERNAL_PREF
,
1776 Extension::NO_FLAGS
,
1781 ASSERT_TRUE(service()->GetExtensionById(good_crx
, false));
1784 // Install a user script (they get converted automatically to an extension)
1785 TEST_F(ExtensionServiceTest
, InstallUserScript
) {
1786 // The details of script conversion are tested elsewhere, this just tests
1787 // integration with ExtensionService.
1788 InitializeEmptyExtensionService();
1790 base::FilePath path
= data_dir().AppendASCII("user_script_basic.user.js");
1792 ASSERT_TRUE(base::PathExists(path
));
1793 scoped_refptr
<CrxInstaller
> installer(CrxInstaller::CreateSilent(service()));
1794 installer
->set_allow_silent_install(true);
1795 installer
->InstallUserScript(
1797 GURL("http://www.aaronboodman.com/scripts/user_script_basic.user.js"));
1799 base::RunLoop().RunUntilIdle();
1800 std::vector
<base::string16
> errors
= GetErrors();
1801 EXPECT_TRUE(installed_
) << "Nothing was installed.";
1802 EXPECT_FALSE(was_update_
) << path
.value();
1803 ASSERT_EQ(1u, loaded_
.size()) << "Nothing was loaded.";
1804 EXPECT_EQ(0u, errors
.size()) << "There were errors: "
1805 << JoinString(errors
, ',');
1806 EXPECT_TRUE(service()->GetExtensionById(loaded_
[0]->id(), false))
1810 was_update_
= false;
1812 ExtensionErrorReporter::GetInstance()->ClearErrors();
1815 // Extensions don't install during shutdown.
1816 TEST_F(ExtensionServiceTest
, InstallExtensionDuringShutdown
) {
1817 InitializeEmptyExtensionService();
1819 // Simulate shutdown.
1820 service()->set_browser_terminating_for_test(true);
1822 base::FilePath path
= data_dir().AppendASCII("good.crx");
1823 scoped_refptr
<CrxInstaller
> installer(CrxInstaller::CreateSilent(service()));
1824 installer
->set_allow_silent_install(true);
1825 installer
->InstallCrx(path
);
1826 base::RunLoop().RunUntilIdle();
1828 EXPECT_FALSE(installed_
) << "Extension installed during shutdown.";
1829 ASSERT_EQ(0u, loaded_
.size()) << "Extension loaded during shutdown.";
1832 // This tests that the granted permissions preferences are correctly set when
1833 // installing an extension.
1834 TEST_F(ExtensionServiceTest
, GrantedPermissions
) {
1835 InitializeEmptyExtensionService();
1836 base::FilePath path
= data_dir().AppendASCII("permissions");
1838 base::FilePath pem_path
= path
.AppendASCII("unknown.pem");
1839 path
= path
.AppendASCII("unknown");
1841 ASSERT_TRUE(base::PathExists(pem_path
));
1842 ASSERT_TRUE(base::PathExists(path
));
1844 ExtensionPrefs
* prefs
= ExtensionPrefs::Get(profile());
1846 APIPermissionSet expected_api_perms
;
1847 URLPatternSet expected_host_perms
;
1849 // Make sure there aren't any granted permissions before the
1850 // extension is installed.
1851 scoped_refptr
<PermissionSet
> known_perms(
1852 prefs
->GetGrantedPermissions(permissions_crx
));
1853 EXPECT_FALSE(known_perms
.get());
1855 const Extension
* extension
= PackAndInstallCRX(path
, pem_path
, INSTALL_NEW
);
1857 EXPECT_EQ(0u, GetErrors().size());
1858 ASSERT_EQ(1u, registry()->enabled_extensions().size());
1859 EXPECT_EQ(permissions_crx
, extension
->id());
1861 // Verify that the valid API permissions have been recognized.
1862 expected_api_perms
.insert(APIPermission::kTab
);
1864 AddPattern(&expected_host_perms
, "http://*.google.com/*");
1865 AddPattern(&expected_host_perms
, "https://*.google.com/*");
1866 AddPattern(&expected_host_perms
, "http://*.google.com.hk/*");
1867 AddPattern(&expected_host_perms
, "http://www.example.com/*");
1869 known_perms
= prefs
->GetGrantedPermissions(extension
->id());
1870 EXPECT_TRUE(known_perms
.get());
1871 EXPECT_FALSE(known_perms
->IsEmpty());
1872 EXPECT_EQ(expected_api_perms
, known_perms
->apis());
1873 EXPECT_FALSE(known_perms
->HasEffectiveFullAccess());
1874 EXPECT_EQ(expected_host_perms
, known_perms
->effective_hosts());
1878 #if !defined(OS_CHROMEOS)
1879 // This tests that the granted permissions preferences are correctly set for
1881 TEST_F(ExtensionServiceTest
, DefaultAppsGrantedPermissions
) {
1882 InitializeEmptyExtensionService();
1883 base::FilePath path
= data_dir().AppendASCII("permissions");
1885 base::FilePath pem_path
= path
.AppendASCII("unknown.pem");
1886 path
= path
.AppendASCII("unknown");
1888 ASSERT_TRUE(base::PathExists(pem_path
));
1889 ASSERT_TRUE(base::PathExists(path
));
1891 ExtensionPrefs
* prefs
= ExtensionPrefs::Get(profile());
1893 APIPermissionSet expected_api_perms
;
1894 URLPatternSet expected_host_perms
;
1896 // Make sure there aren't any granted permissions before the
1897 // extension is installed.
1898 scoped_refptr
<PermissionSet
> known_perms(
1899 prefs
->GetGrantedPermissions(permissions_crx
));
1900 EXPECT_FALSE(known_perms
.get());
1902 const Extension
* extension
= PackAndInstallCRX(
1903 path
, pem_path
, INSTALL_NEW
, Extension::WAS_INSTALLED_BY_DEFAULT
);
1905 EXPECT_EQ(0u, GetErrors().size());
1906 ASSERT_EQ(1u, registry()->enabled_extensions().size());
1907 EXPECT_EQ(permissions_crx
, extension
->id());
1909 // Verify that the valid API permissions have been recognized.
1910 expected_api_perms
.insert(APIPermission::kTab
);
1912 known_perms
= prefs
->GetGrantedPermissions(extension
->id());
1913 EXPECT_TRUE(known_perms
.get());
1914 EXPECT_FALSE(known_perms
->IsEmpty());
1915 EXPECT_EQ(expected_api_perms
, known_perms
->apis());
1916 EXPECT_FALSE(known_perms
->HasEffectiveFullAccess());
1920 #if !defined(OS_POSIX) || defined(OS_MACOSX)
1921 // Tests that the granted permissions full_access bit gets set correctly when
1922 // an extension contains an NPAPI plugin.
1923 // Only run this on platforms that support NPAPI plugins.
1924 TEST_F(ExtensionServiceTest
, GrantedFullAccessPermissions
) {
1925 InitPluginService();
1927 InitializeEmptyExtensionService();
1929 ASSERT_TRUE(base::PathExists(good1_path()));
1930 const Extension
* extension
= PackAndInstallCRX(good1_path(), INSTALL_NEW
);
1931 EXPECT_EQ(0u, GetErrors().size());
1932 EXPECT_EQ(1u, registry()->enabled_extensions().size());
1933 ExtensionPrefs
* prefs
= ExtensionPrefs::Get(profile());
1935 scoped_refptr
<PermissionSet
> permissions(
1936 prefs
->GetGrantedPermissions(extension
->id()));
1937 EXPECT_FALSE(permissions
->IsEmpty());
1938 EXPECT_TRUE(permissions
->HasEffectiveFullAccess());
1939 EXPECT_FALSE(permissions
->apis().empty());
1940 EXPECT_TRUE(permissions
->HasAPIPermission(APIPermission::kPlugin
));
1942 // Full access implies full host access too...
1943 EXPECT_TRUE(permissions
->HasEffectiveAccessToAllHosts());
1947 // Tests that the extension is disabled when permissions are missing from
1948 // the extension's granted permissions preferences. (This simulates updating
1949 // the browser to a version which recognizes more permissions).
1950 TEST_F(ExtensionServiceTest
, GrantedAPIAndHostPermissions
) {
1951 InitializeEmptyExtensionService();
1953 base::FilePath path
=
1954 data_dir().AppendASCII("permissions").AppendASCII("unknown");
1956 ASSERT_TRUE(base::PathExists(path
));
1958 const Extension
* extension
= PackAndInstallCRX(path
, INSTALL_NEW
);
1960 EXPECT_EQ(0u, GetErrors().size());
1961 EXPECT_EQ(1u, registry()->enabled_extensions().size());
1962 std::string extension_id
= extension
->id();
1964 ExtensionPrefs
* prefs
= ExtensionPrefs::Get(profile());
1966 APIPermissionSet expected_api_permissions
;
1967 URLPatternSet expected_host_permissions
;
1969 expected_api_permissions
.insert(APIPermission::kTab
);
1970 AddPattern(&expected_host_permissions
, "http://*.google.com/*");
1971 AddPattern(&expected_host_permissions
, "https://*.google.com/*");
1972 AddPattern(&expected_host_permissions
, "http://*.google.com.hk/*");
1973 AddPattern(&expected_host_permissions
, "http://www.example.com/*");
1975 std::set
<std::string
> host_permissions
;
1977 // Test that the extension is disabled when an API permission is missing from
1978 // the extension's granted api permissions preference. (This simulates
1979 // updating the browser to a version which recognizes a new API permission).
1980 SetPref(extension_id
, "granted_permissions.api",
1981 new base::ListValue(), "granted_permissions.api");
1982 service()->ReloadExtensionsForTest();
1984 EXPECT_EQ(1u, registry()->disabled_extensions().size());
1985 extension
= registry()->disabled_extensions().begin()->get();
1987 ASSERT_TRUE(prefs
->IsExtensionDisabled(extension_id
));
1988 ASSERT_FALSE(service()->IsExtensionEnabled(extension_id
));
1989 ASSERT_TRUE(prefs
->DidExtensionEscalatePermissions(extension_id
));
1991 // Now grant and re-enable the extension, making sure the prefs are updated.
1992 service()->GrantPermissionsAndEnableExtension(extension
);
1994 ASSERT_FALSE(prefs
->IsExtensionDisabled(extension_id
));
1995 ASSERT_TRUE(service()->IsExtensionEnabled(extension_id
));
1996 ASSERT_FALSE(prefs
->DidExtensionEscalatePermissions(extension_id
));
1998 scoped_refptr
<PermissionSet
> current_perms(
1999 prefs
->GetGrantedPermissions(extension_id
));
2000 ASSERT_TRUE(current_perms
.get());
2001 ASSERT_FALSE(current_perms
->IsEmpty());
2002 ASSERT_FALSE(current_perms
->HasEffectiveFullAccess());
2003 ASSERT_EQ(expected_api_permissions
, current_perms
->apis());
2004 ASSERT_EQ(expected_host_permissions
, current_perms
->effective_hosts());
2006 // Tests that the extension is disabled when a host permission is missing from
2007 // the extension's granted host permissions preference. (This simulates
2008 // updating the browser to a version which recognizes additional host
2010 host_permissions
.clear();
2011 current_perms
= NULL
;
2013 host_permissions
.insert("http://*.google.com/*");
2014 host_permissions
.insert("https://*.google.com/*");
2015 host_permissions
.insert("http://*.google.com.hk/*");
2017 base::ListValue
* api_permissions
= new base::ListValue();
2018 api_permissions
->Append(
2019 new base::StringValue("tabs"));
2020 SetPref(extension_id
, "granted_permissions.api",
2021 api_permissions
, "granted_permissions.api");
2023 extension_id
, "granted_permissions.scriptable_host", host_permissions
);
2025 service()->ReloadExtensionsForTest();
2027 EXPECT_EQ(1u, registry()->disabled_extensions().size());
2028 extension
= registry()->disabled_extensions().begin()->get();
2030 ASSERT_TRUE(prefs
->IsExtensionDisabled(extension_id
));
2031 ASSERT_FALSE(service()->IsExtensionEnabled(extension_id
));
2032 ASSERT_TRUE(prefs
->DidExtensionEscalatePermissions(extension_id
));
2034 // Now grant and re-enable the extension, making sure the prefs are updated.
2035 service()->GrantPermissionsAndEnableExtension(extension
);
2037 ASSERT_TRUE(service()->IsExtensionEnabled(extension_id
));
2038 ASSERT_FALSE(prefs
->DidExtensionEscalatePermissions(extension_id
));
2040 current_perms
= prefs
->GetGrantedPermissions(extension_id
);
2041 ASSERT_TRUE(current_perms
.get());
2042 ASSERT_FALSE(current_perms
->IsEmpty());
2043 ASSERT_FALSE(current_perms
->HasEffectiveFullAccess());
2044 ASSERT_EQ(expected_api_permissions
, current_perms
->apis());
2045 ASSERT_EQ(expected_host_permissions
, current_perms
->effective_hosts());
2048 // Test Packaging and installing an extension.
2049 TEST_F(ExtensionServiceTest
, PackExtension
) {
2050 InitializeEmptyExtensionService();
2051 base::FilePath input_directory
=
2053 .AppendASCII("good")
2054 .AppendASCII("Extensions")
2055 .AppendASCII("behllobkkfkfnphdnhnkndlbkcpglgmj")
2056 .AppendASCII("1.0.0.0");
2058 base::ScopedTempDir temp_dir
;
2059 ASSERT_TRUE(temp_dir
.CreateUniqueTempDir());
2060 base::FilePath output_directory
= temp_dir
.path();
2062 base::FilePath
crx_path(output_directory
.AppendASCII("ex1.crx"));
2063 base::FilePath
privkey_path(output_directory
.AppendASCII("privkey.pem"));
2065 scoped_ptr
<ExtensionCreator
> creator(new ExtensionCreator());
2066 ASSERT_TRUE(creator
->Run(input_directory
, crx_path
, base::FilePath(),
2067 privkey_path
, ExtensionCreator::kNoRunFlags
));
2068 ASSERT_TRUE(base::PathExists(crx_path
));
2069 ASSERT_TRUE(base::PathExists(privkey_path
));
2071 // Repeat the run with the pem file gone, and no special flags
2072 // Should refuse to overwrite the existing crx.
2073 base::DeleteFile(privkey_path
, false);
2074 ASSERT_FALSE(creator
->Run(input_directory
, crx_path
, base::FilePath(),
2075 privkey_path
, ExtensionCreator::kNoRunFlags
));
2077 // OK, now try it with a flag to overwrite existing crx. Should work.
2078 ASSERT_TRUE(creator
->Run(input_directory
, crx_path
, base::FilePath(),
2079 privkey_path
, ExtensionCreator::kOverwriteCRX
));
2081 // Repeat the run allowing existing crx, but the existing pem is still
2082 // an error. Should fail.
2083 ASSERT_FALSE(creator
->Run(input_directory
, crx_path
, base::FilePath(),
2084 privkey_path
, ExtensionCreator::kOverwriteCRX
));
2086 ASSERT_TRUE(base::PathExists(privkey_path
));
2087 InstallCRX(crx_path
, INSTALL_NEW
);
2089 // Try packing with invalid paths.
2090 creator
.reset(new ExtensionCreator());
2092 creator
->Run(base::FilePath(), base::FilePath(), base::FilePath(),
2093 base::FilePath(), ExtensionCreator::kOverwriteCRX
));
2095 // Try packing an empty directory. Should fail because an empty directory is
2096 // not a valid extension.
2097 base::ScopedTempDir temp_dir2
;
2098 ASSERT_TRUE(temp_dir2
.CreateUniqueTempDir());
2099 creator
.reset(new ExtensionCreator());
2100 ASSERT_FALSE(creator
->Run(temp_dir2
.path(), crx_path
, privkey_path
,
2101 base::FilePath(), ExtensionCreator::kOverwriteCRX
));
2103 // Try packing with an invalid manifest.
2104 std::string invalid_manifest_content
= "I am not a manifest.";
2105 ASSERT_TRUE(base::WriteFile(
2106 temp_dir2
.path().Append(extensions::kManifestFilename
),
2107 invalid_manifest_content
.c_str(), invalid_manifest_content
.size()));
2108 creator
.reset(new ExtensionCreator());
2109 ASSERT_FALSE(creator
->Run(temp_dir2
.path(), crx_path
, privkey_path
,
2110 base::FilePath(), ExtensionCreator::kOverwriteCRX
));
2112 // Try packing with a private key that is a valid key, but invalid for the
2114 base::FilePath bad_private_key_dir
=
2115 data_dir().AppendASCII("bad_private_key");
2116 crx_path
= output_directory
.AppendASCII("bad_private_key.crx");
2117 privkey_path
= data_dir().AppendASCII("bad_private_key.pem");
2118 ASSERT_FALSE(creator
->Run(bad_private_key_dir
, crx_path
, base::FilePath(),
2119 privkey_path
, ExtensionCreator::kOverwriteCRX
));
2122 // Test Packaging and installing an extension whose name contains punctuation.
2123 TEST_F(ExtensionServiceTest
, PackPunctuatedExtension
) {
2124 InitializeEmptyExtensionService();
2125 base::FilePath input_directory
= data_dir()
2126 .AppendASCII("good")
2127 .AppendASCII("Extensions")
2129 .AppendASCII("1.0.0.0");
2131 base::ScopedTempDir temp_dir
;
2132 ASSERT_TRUE(temp_dir
.CreateUniqueTempDir());
2134 // Extension names containing punctuation, and the expected names for the
2135 // packed extensions.
2136 const base::FilePath punctuated_names
[] = {
2137 base::FilePath(FILE_PATH_LITERAL("this.extensions.name.has.periods")),
2138 base::FilePath(FILE_PATH_LITERAL(".thisextensionsnamestartswithaperiod")),
2139 base::FilePath(FILE_PATH_LITERAL("thisextensionhasaslashinitsname/")).
2140 NormalizePathSeparators(),
2142 const base::FilePath expected_crx_names
[] = {
2143 base::FilePath(FILE_PATH_LITERAL("this.extensions.name.has.periods.crx")),
2145 FILE_PATH_LITERAL(".thisextensionsnamestartswithaperiod.crx")),
2146 base::FilePath(FILE_PATH_LITERAL("thisextensionhasaslashinitsname.crx")),
2148 const base::FilePath expected_private_key_names
[] = {
2149 base::FilePath(FILE_PATH_LITERAL("this.extensions.name.has.periods.pem")),
2151 FILE_PATH_LITERAL(".thisextensionsnamestartswithaperiod.pem")),
2152 base::FilePath(FILE_PATH_LITERAL("thisextensionhasaslashinitsname.pem")),
2155 for (size_t i
= 0; i
< arraysize(punctuated_names
); ++i
) {
2156 SCOPED_TRACE(punctuated_names
[i
].value().c_str());
2157 base::FilePath output_dir
= temp_dir
.path().Append(punctuated_names
[i
]);
2159 // Copy the extension into the output directory, as PackExtensionJob doesn't
2160 // let us choose where to output the packed extension.
2161 ASSERT_TRUE(base::CopyDirectory(input_directory
, output_dir
, true));
2163 base::FilePath expected_crx_path
=
2164 temp_dir
.path().Append(expected_crx_names
[i
]);
2165 base::FilePath expected_private_key_path
=
2166 temp_dir
.path().Append(expected_private_key_names
[i
]);
2167 PackExtensionTestClient
pack_client(expected_crx_path
,
2168 expected_private_key_path
);
2169 scoped_refptr
<extensions::PackExtensionJob
> packer(
2170 new extensions::PackExtensionJob(&pack_client
, output_dir
,
2172 ExtensionCreator::kOverwriteCRX
));
2175 // The packer will post a notification task to the current thread's message
2176 // loop when it is finished. We manually run the loop here so that we
2177 // block and catch the notification; otherwise, the process would exit.
2178 // This call to |Run()| is matched by a call to |Quit()| in the
2179 // |PackExtensionTestClient|'s notification handling code.
2180 base::MessageLoop::current()->Run();
2182 if (HasFatalFailure())
2185 InstallCRX(expected_crx_path
, INSTALL_NEW
);
2189 TEST_F(ExtensionServiceTest
, PackExtensionContainingKeyFails
) {
2190 InitializeEmptyExtensionService();
2192 base::ScopedTempDir extension_temp_dir
;
2193 ASSERT_TRUE(extension_temp_dir
.CreateUniqueTempDir());
2194 base::FilePath input_directory
= extension_temp_dir
.path().AppendASCII("ext");
2196 base::CopyDirectory(data_dir()
2197 .AppendASCII("good")
2198 .AppendASCII("Extensions")
2199 .AppendASCII("behllobkkfkfnphdnhnkndlbkcpglgmj")
2200 .AppendASCII("1.0.0.0"),
2202 /*recursive=*/true));
2204 base::ScopedTempDir output_temp_dir
;
2205 ASSERT_TRUE(output_temp_dir
.CreateUniqueTempDir());
2206 base::FilePath output_directory
= output_temp_dir
.path();
2208 base::FilePath
crx_path(output_directory
.AppendASCII("ex1.crx"));
2209 base::FilePath
privkey_path(output_directory
.AppendASCII("privkey.pem"));
2211 // Pack the extension once to get a private key.
2212 scoped_ptr
<ExtensionCreator
> creator(new ExtensionCreator());
2213 ASSERT_TRUE(creator
->Run(input_directory
, crx_path
, base::FilePath(),
2214 privkey_path
, ExtensionCreator::kNoRunFlags
))
2215 << creator
->error_message();
2216 ASSERT_TRUE(base::PathExists(crx_path
));
2217 ASSERT_TRUE(base::PathExists(privkey_path
));
2219 base::DeleteFile(crx_path
, false);
2220 // Move the pem file into the extension.
2221 base::Move(privkey_path
,
2222 input_directory
.AppendASCII("privkey.pem"));
2224 // This pack should fail because of the contained private key.
2225 EXPECT_FALSE(creator
->Run(input_directory
, crx_path
, base::FilePath(),
2226 privkey_path
, ExtensionCreator::kNoRunFlags
));
2227 EXPECT_THAT(creator
->error_message(),
2228 testing::ContainsRegex(
2229 "extension includes the key file.*privkey.pem"));
2232 // Test Packaging and installing an extension using an openssl generated key.
2233 // The openssl is generated with the following:
2234 // > openssl genrsa -out privkey.pem 1024
2235 // > openssl pkcs8 -topk8 -nocrypt -in privkey.pem -out privkey_asn1.pem
2236 // The privkey.pem is a PrivateKey, and the pcks8 -topk8 creates a
2237 // PrivateKeyInfo ASN.1 structure, we our RSAPrivateKey expects.
2238 TEST_F(ExtensionServiceTest
, PackExtensionOpenSSLKey
) {
2239 InitializeEmptyExtensionService();
2240 base::FilePath input_directory
=
2242 .AppendASCII("good")
2243 .AppendASCII("Extensions")
2244 .AppendASCII("behllobkkfkfnphdnhnkndlbkcpglgmj")
2245 .AppendASCII("1.0.0.0");
2246 base::FilePath
privkey_path(
2247 data_dir().AppendASCII("openssl_privkey_asn1.pem"));
2248 ASSERT_TRUE(base::PathExists(privkey_path
));
2250 base::ScopedTempDir temp_dir
;
2251 ASSERT_TRUE(temp_dir
.CreateUniqueTempDir());
2252 base::FilePath output_directory
= temp_dir
.path();
2254 base::FilePath
crx_path(output_directory
.AppendASCII("ex1.crx"));
2256 scoped_ptr
<ExtensionCreator
> creator(new ExtensionCreator());
2257 ASSERT_TRUE(creator
->Run(input_directory
, crx_path
, privkey_path
,
2258 base::FilePath(), ExtensionCreator::kOverwriteCRX
));
2260 InstallCRX(crx_path
, INSTALL_NEW
);
2263 #if defined(THREAD_SANITIZER)
2264 // Flaky under Tsan. http://crbug.com/377702
2265 #define MAYBE_InstallTheme DISABLED_InstallTheme
2267 #define MAYBE_InstallTheme InstallTheme
2270 TEST_F(ExtensionServiceTest
, MAYBE_InstallTheme
) {
2271 InitializeEmptyExtensionService();
2275 base::FilePath path
= data_dir().AppendASCII("theme.crx");
2276 InstallCRX(path
, INSTALL_NEW
);
2278 ValidatePrefKeyCount(++pref_count
);
2279 ValidateIntegerPref(theme_crx
, "state", Extension::ENABLED
);
2280 ValidateIntegerPref(theme_crx
, "location", Manifest::INTERNAL
);
2282 // A theme when extensions are disabled. Themes can be installed, even when
2283 // extensions are disabled.
2284 service()->set_extensions_enabled(false);
2285 path
= data_dir().AppendASCII("theme2.crx");
2286 InstallCRX(path
, INSTALL_NEW
);
2287 ValidatePrefKeyCount(++pref_count
);
2288 ValidateIntegerPref(theme2_crx
, "state", Extension::ENABLED
);
2289 ValidateIntegerPref(theme2_crx
, "location", Manifest::INTERNAL
);
2291 // A theme with extension elements. Themes cannot have extension elements,
2292 // so any such elements (like content scripts) should be ignored.
2293 service()->set_extensions_enabled(true);
2295 path
= data_dir().AppendASCII("theme_with_extension.crx");
2296 const Extension
* extension
= InstallCRX(path
, INSTALL_NEW
);
2297 ValidatePrefKeyCount(++pref_count
);
2298 ASSERT_TRUE(extension
);
2299 EXPECT_TRUE(extension
->is_theme());
2302 extensions::ContentScriptsInfo::GetContentScripts(extension
).size());
2305 // A theme with image resources missing (misspelt path).
2306 path
= data_dir().AppendASCII("theme_missing_image.crx");
2307 InstallCRX(path
, INSTALL_FAILED
);
2308 ValidatePrefKeyCount(pref_count
);
2311 TEST_F(ExtensionServiceTest
, LoadLocalizedTheme
) {
2313 InitializeEmptyExtensionService();
2316 base::FilePath extension_path
= data_dir().AppendASCII("theme_i18n");
2318 extensions::UnpackedInstaller::Create(service())->Load(extension_path
);
2319 base::RunLoop().RunUntilIdle();
2320 EXPECT_EQ(0u, GetErrors().size());
2321 ASSERT_EQ(1u, loaded_
.size());
2322 EXPECT_EQ(1u, registry()->enabled_extensions().size());
2323 const Extension
* theme
= registry()->enabled_extensions().begin()->get();
2324 EXPECT_EQ("name", theme
->name());
2325 EXPECT_EQ("description", theme
->description());
2327 // Cleanup the "Cached Theme.pak" file. Ideally, this would be installed in a
2328 // temporary directory, but it automatically installs to the extension's
2329 // directory, and we don't want to copy the whole extension for a unittest.
2330 base::FilePath theme_file
= extension_path
.Append(chrome::kThemePackFilename
);
2331 ASSERT_TRUE(base::PathExists(theme_file
));
2332 ASSERT_TRUE(base::DeleteFile(theme_file
, false)); // Not recursive.
2335 #if defined(OS_POSIX)
2336 TEST_F(ExtensionServiceTest
, UnpackedExtensionMayContainSymlinkedFiles
) {
2337 base::FilePath source_data_dir
=
2338 data_dir().AppendASCII("unpacked").AppendASCII("symlinks_allowed");
2340 // Paths to test data files.
2341 base::FilePath source_manifest
= source_data_dir
.AppendASCII("manifest.json");
2342 ASSERT_TRUE(base::PathExists(source_manifest
));
2343 base::FilePath source_icon
= source_data_dir
.AppendASCII("icon.png");
2344 ASSERT_TRUE(base::PathExists(source_icon
));
2346 // Set up the temporary extension directory.
2347 base::ScopedTempDir temp
;
2348 ASSERT_TRUE(temp
.CreateUniqueTempDir());
2349 base::FilePath extension_path
= temp
.path();
2350 base::FilePath manifest
= extension_path
.Append(
2351 extensions::kManifestFilename
);
2352 base::FilePath icon_symlink
= extension_path
.AppendASCII("icon.png");
2353 base::CopyFile(source_manifest
, manifest
);
2354 base::CreateSymbolicLink(source_icon
, icon_symlink
);
2357 InitializeEmptyExtensionService();
2358 extensions::UnpackedInstaller::Create(service())->Load(extension_path
);
2359 base::RunLoop().RunUntilIdle();
2361 EXPECT_TRUE(GetErrors().empty());
2362 ASSERT_EQ(1u, loaded_
.size());
2363 EXPECT_EQ(1u, registry()->enabled_extensions().size());
2367 TEST_F(ExtensionServiceTest
, UnpackedExtensionMayNotHaveUnderscore
) {
2368 InitializeEmptyExtensionService();
2369 base::FilePath extension_path
= data_dir().AppendASCII("underscore_name");
2370 extensions::UnpackedInstaller::Create(service())->Load(extension_path
);
2371 base::RunLoop().RunUntilIdle();
2372 EXPECT_EQ(1u, GetErrors().size());
2373 EXPECT_EQ(0u, registry()->enabled_extensions().size());
2376 TEST_F(ExtensionServiceTest
, InstallLocalizedTheme
) {
2377 InitializeEmptyExtensionService();
2380 base::FilePath theme_path
= data_dir().AppendASCII("theme_i18n");
2382 const Extension
* theme
= PackAndInstallCRX(theme_path
, INSTALL_NEW
);
2384 EXPECT_EQ(0u, GetErrors().size());
2385 EXPECT_EQ(1u, registry()->enabled_extensions().size());
2386 EXPECT_EQ("name", theme
->name());
2387 EXPECT_EQ("description", theme
->description());
2390 TEST_F(ExtensionServiceTest
, InstallApps
) {
2391 InitializeEmptyExtensionService();
2394 const Extension
* app
=
2395 PackAndInstallCRX(data_dir().AppendASCII("app1"), INSTALL_NEW
);
2397 ValidatePrefKeyCount(++pref_count
);
2398 ASSERT_EQ(1u, registry()->enabled_extensions().size());
2399 ValidateIntegerPref(app
->id(), "state", Extension::ENABLED
);
2400 ValidateIntegerPref(app
->id(), "location", Manifest::INTERNAL
);
2402 // Another app with non-overlapping extent. Should succeed.
2403 PackAndInstallCRX(data_dir().AppendASCII("app2"), INSTALL_NEW
);
2404 ValidatePrefKeyCount(++pref_count
);
2406 // A third app whose extent overlaps the first. Should fail.
2407 PackAndInstallCRX(data_dir().AppendASCII("app3"), INSTALL_FAILED
);
2408 ValidatePrefKeyCount(pref_count
);
2411 // Tests that file access is OFF by default.
2412 TEST_F(ExtensionServiceTest
, DefaultFileAccess
) {
2413 InitializeEmptyExtensionService();
2414 const Extension
* extension
= PackAndInstallCRX(
2415 data_dir().AppendASCII("permissions").AppendASCII("files"), INSTALL_NEW
);
2416 EXPECT_EQ(0u, GetErrors().size());
2417 EXPECT_EQ(1u, registry()->enabled_extensions().size());
2419 ExtensionPrefs::Get(profile())->AllowFileAccess(extension
->id()));
2422 TEST_F(ExtensionServiceTest
, UpdateApps
) {
2423 InitializeEmptyExtensionService();
2424 base::FilePath extensions_path
= data_dir().AppendASCII("app_update");
2426 // First install v1 of a hosted app.
2427 const Extension
* extension
=
2428 InstallCRX(extensions_path
.AppendASCII("v1.crx"), INSTALL_NEW
);
2429 ASSERT_EQ(1u, registry()->enabled_extensions().size());
2430 std::string id
= extension
->id();
2431 ASSERT_EQ(std::string("1"), extension
->version()->GetString());
2433 // Now try updating to v2.
2435 extensions_path
.AppendASCII("v2.crx"),
2437 ASSERT_EQ(std::string("2"),
2438 service()->GetExtensionById(id
, false)->version()->GetString());
2441 // Verifies that the NTP page and launch ordinals are kept when updating apps.
2442 TEST_F(ExtensionServiceTest
, UpdateAppsRetainOrdinals
) {
2443 InitializeEmptyExtensionService();
2444 AppSorting
* sorting
= ExtensionPrefs::Get(profile())->app_sorting();
2445 base::FilePath extensions_path
= data_dir().AppendASCII("app_update");
2447 // First install v1 of a hosted app.
2448 const Extension
* extension
=
2449 InstallCRX(extensions_path
.AppendASCII("v1.crx"), INSTALL_NEW
);
2450 ASSERT_EQ(1u, registry()->enabled_extensions().size());
2451 std::string id
= extension
->id();
2452 ASSERT_EQ(std::string("1"), extension
->version()->GetString());
2454 // Modify the ordinals so we can distinguish them from the defaults.
2455 syncer::StringOrdinal new_page_ordinal
=
2456 sorting
->GetPageOrdinal(id
).CreateAfter();
2457 syncer::StringOrdinal new_launch_ordinal
=
2458 sorting
->GetAppLaunchOrdinal(id
).CreateBefore();
2460 sorting
->SetPageOrdinal(id
, new_page_ordinal
);
2461 sorting
->SetAppLaunchOrdinal(id
, new_launch_ordinal
);
2463 // Now try updating to v2.
2464 UpdateExtension(id
, extensions_path
.AppendASCII("v2.crx"), ENABLED
);
2465 ASSERT_EQ(std::string("2"),
2466 service()->GetExtensionById(id
, false)->version()->GetString());
2468 // Verify that the ordinals match.
2469 ASSERT_TRUE(new_page_ordinal
.Equals(sorting
->GetPageOrdinal(id
)));
2470 ASSERT_TRUE(new_launch_ordinal
.Equals(sorting
->GetAppLaunchOrdinal(id
)));
2473 // Ensures that the CWS has properly initialized ordinals.
2474 TEST_F(ExtensionServiceTest
, EnsureCWSOrdinalsInitialized
) {
2475 InitializeEmptyExtensionService();
2476 service()->component_loader()->Add(
2477 IDR_WEBSTORE_MANIFEST
, base::FilePath(FILE_PATH_LITERAL("web_store")));
2480 AppSorting
* sorting
= ExtensionPrefs::Get(profile())->app_sorting();
2482 sorting
->GetPageOrdinal(extensions::kWebStoreAppId
).IsValid());
2484 sorting
->GetAppLaunchOrdinal(extensions::kWebStoreAppId
).IsValid());
2487 TEST_F(ExtensionServiceTest
, InstallAppsWithUnlimitedStorage
) {
2488 InitializeEmptyExtensionService();
2489 EXPECT_TRUE(registry()->enabled_extensions().is_empty());
2493 // Install app1 with unlimited storage.
2494 const Extension
* extension
=
2495 PackAndInstallCRX(data_dir().AppendASCII("app1"), INSTALL_NEW
);
2496 ValidatePrefKeyCount(++pref_count
);
2497 ASSERT_EQ(1u, registry()->enabled_extensions().size());
2498 const std::string id1
= extension
->id();
2499 EXPECT_TRUE(extension
->permissions_data()->HasAPIPermission(
2500 APIPermission::kUnlimitedStorage
));
2501 EXPECT_TRUE(extension
->web_extent().MatchesURL(
2502 extensions::AppLaunchInfo::GetFullLaunchURL(extension
)));
2504 extensions::AppLaunchInfo::GetFullLaunchURL(extension
).GetOrigin());
2505 EXPECT_TRUE(profile()->GetExtensionSpecialStoragePolicy()->IsStorageUnlimited(
2508 // Install app2 from the same origin with unlimited storage.
2509 extension
= PackAndInstallCRX(data_dir().AppendASCII("app2"), INSTALL_NEW
);
2510 ValidatePrefKeyCount(++pref_count
);
2511 ASSERT_EQ(2u, registry()->enabled_extensions().size());
2512 const std::string id2
= extension
->id();
2513 EXPECT_TRUE(extension
->permissions_data()->HasAPIPermission(
2514 APIPermission::kUnlimitedStorage
));
2515 EXPECT_TRUE(extension
->web_extent().MatchesURL(
2516 extensions::AppLaunchInfo::GetFullLaunchURL(extension
)));
2518 extensions::AppLaunchInfo::GetFullLaunchURL(extension
).GetOrigin());
2519 EXPECT_EQ(origin1
, origin2
);
2520 EXPECT_TRUE(profile()->GetExtensionSpecialStoragePolicy()->IsStorageUnlimited(
2523 // Uninstall one of them, unlimited storage should still be granted
2525 UninstallExtension(id1
, false);
2526 EXPECT_EQ(1u, registry()->enabled_extensions().size());
2527 EXPECT_TRUE(profile()->GetExtensionSpecialStoragePolicy()->IsStorageUnlimited(
2530 // Uninstall the other, unlimited storage should be revoked.
2531 UninstallExtension(id2
, false);
2532 EXPECT_EQ(0u, registry()->enabled_extensions().size());
2534 profile()->GetExtensionSpecialStoragePolicy()->IsStorageUnlimited(
2538 TEST_F(ExtensionServiceTest
, InstallAppsAndCheckStorageProtection
) {
2539 InitializeEmptyExtensionService();
2540 EXPECT_TRUE(registry()->enabled_extensions().is_empty());
2544 const Extension
* extension
=
2545 PackAndInstallCRX(data_dir().AppendASCII("app1"), INSTALL_NEW
);
2546 ValidatePrefKeyCount(++pref_count
);
2547 ASSERT_EQ(1u, registry()->enabled_extensions().size());
2548 EXPECT_TRUE(extension
->is_app());
2549 const std::string id1
= extension
->id();
2551 extensions::AppLaunchInfo::GetFullLaunchURL(extension
).GetOrigin());
2552 EXPECT_TRUE(profile()->GetExtensionSpecialStoragePolicy()->IsStorageProtected(
2555 // App 4 has a different origin (maps.google.com).
2556 extension
= PackAndInstallCRX(data_dir().AppendASCII("app4"), INSTALL_NEW
);
2557 ValidatePrefKeyCount(++pref_count
);
2558 ASSERT_EQ(2u, registry()->enabled_extensions().size());
2559 const std::string id2
= extension
->id();
2561 extensions::AppLaunchInfo::GetFullLaunchURL(extension
).GetOrigin());
2562 ASSERT_NE(origin1
, origin2
);
2563 EXPECT_TRUE(profile()->GetExtensionSpecialStoragePolicy()->IsStorageProtected(
2566 UninstallExtension(id1
, false);
2567 EXPECT_EQ(1u, registry()->enabled_extensions().size());
2569 UninstallExtension(id2
, false);
2571 EXPECT_TRUE(registry()->enabled_extensions().is_empty());
2573 profile()->GetExtensionSpecialStoragePolicy()->IsStorageProtected(
2576 profile()->GetExtensionSpecialStoragePolicy()->IsStorageProtected(
2580 // Test that when an extension version is reinstalled, nothing happens.
2581 TEST_F(ExtensionServiceTest
, Reinstall
) {
2582 InitializeEmptyExtensionService();
2584 // A simple extension that should install without error.
2585 base::FilePath path
= data_dir().AppendASCII("good.crx");
2586 InstallCRX(path
, INSTALL_NEW
);
2588 ValidatePrefKeyCount(1);
2589 ValidateIntegerPref(good_crx
, "state", Extension::ENABLED
);
2590 ValidateIntegerPref(good_crx
, "location", Manifest::INTERNAL
);
2592 // Reinstall the same version, it should overwrite the previous one.
2593 InstallCRX(path
, INSTALL_UPDATED
);
2595 ValidatePrefKeyCount(1);
2596 ValidateIntegerPref(good_crx
, "state", Extension::ENABLED
);
2597 ValidateIntegerPref(good_crx
, "location", Manifest::INTERNAL
);
2600 // Test that we can determine if extensions came from the
2601 // Chrome web store.
2602 TEST_F(ExtensionServiceTest
, FromWebStore
) {
2603 InitializeEmptyExtensionService();
2605 // A simple extension that should install without error.
2606 base::FilePath path
= data_dir().AppendASCII("good.crx");
2607 // Not from web store.
2608 const Extension
* extension
= InstallCRX(path
, INSTALL_NEW
);
2609 std::string id
= extension
->id();
2611 ValidatePrefKeyCount(1);
2612 ASSERT_TRUE(ValidateBooleanPref(good_crx
, "from_webstore", false));
2613 ASSERT_FALSE(extension
->from_webstore());
2615 // Test install from web store.
2616 InstallCRXFromWebStore(path
, INSTALL_UPDATED
); // From web store.
2618 ValidatePrefKeyCount(1);
2619 ASSERT_TRUE(ValidateBooleanPref(good_crx
, "from_webstore", true));
2621 // Reload so extension gets reinitialized with new value.
2622 service()->ReloadExtensionsForTest();
2623 extension
= service()->GetExtensionById(id
, false);
2624 ASSERT_TRUE(extension
->from_webstore());
2626 // Upgrade to version 2.0
2627 path
= data_dir().AppendASCII("good2.crx");
2628 UpdateExtension(good_crx
, path
, ENABLED
);
2629 ValidatePrefKeyCount(1);
2630 ASSERT_TRUE(ValidateBooleanPref(good_crx
, "from_webstore", true));
2633 // Test upgrading a signed extension.
2634 TEST_F(ExtensionServiceTest
, UpgradeSignedGood
) {
2635 InitializeEmptyExtensionService();
2637 base::FilePath path
= data_dir().AppendASCII("good.crx");
2638 const Extension
* extension
= InstallCRX(path
, INSTALL_NEW
);
2639 std::string id
= extension
->id();
2641 ASSERT_EQ("1.0.0.0", extension
->version()->GetString());
2642 ASSERT_EQ(0u, GetErrors().size());
2644 // Upgrade to version 1.0.0.1.
2645 // Also test that the extension's old and new title are correctly retrieved.
2646 path
= data_dir().AppendASCII("good2.crx");
2647 InstallCRX(path
, INSTALL_UPDATED
, Extension::NO_FLAGS
, "My extension 1");
2648 extension
= service()->GetExtensionById(id
, false);
2650 ASSERT_EQ("1.0.0.1", extension
->version()->GetString());
2651 ASSERT_EQ("My updated extension 1", extension
->name());
2652 ASSERT_EQ(0u, GetErrors().size());
2655 // Test upgrading a signed extension with a bad signature.
2656 TEST_F(ExtensionServiceTest
, UpgradeSignedBad
) {
2657 InitializeEmptyExtensionService();
2659 base::FilePath path
= data_dir().AppendASCII("good.crx");
2660 InstallCRX(path
, INSTALL_NEW
);
2662 // Try upgrading with a bad signature. This should fail during the unpack,
2663 // because the key will not match the signature.
2664 path
= data_dir().AppendASCII("bad_signature.crx");
2665 InstallCRX(path
, INSTALL_FAILED
);
2668 // Test a normal update via the UpdateExtension API
2669 TEST_F(ExtensionServiceTest
, UpdateExtension
) {
2670 InitializeEmptyExtensionService();
2672 base::FilePath path
= data_dir().AppendASCII("good.crx");
2674 const Extension
* good
= InstallCRX(path
, INSTALL_NEW
);
2675 ASSERT_EQ("1.0.0.0", good
->VersionString());
2676 ASSERT_EQ(good_crx
, good
->id());
2678 path
= data_dir().AppendASCII("good2.crx");
2679 UpdateExtension(good_crx
, path
, ENABLED
);
2682 service()->GetExtensionById(good_crx
, false)->version()->GetString());
2685 // Extensions should not be updated during browser shutdown.
2686 TEST_F(ExtensionServiceTest
, UpdateExtensionDuringShutdown
) {
2687 InitializeEmptyExtensionService();
2689 // Install an extension.
2690 base::FilePath path
= data_dir().AppendASCII("good.crx");
2691 const Extension
* good
= InstallCRX(path
, INSTALL_NEW
);
2692 ASSERT_EQ(good_crx
, good
->id());
2694 // Simulate shutdown.
2695 service()->set_browser_terminating_for_test(true);
2697 // Update should fail and extension should not be updated.
2698 path
= data_dir().AppendASCII("good2.crx");
2699 bool updated
= service()->UpdateExtension(
2700 extensions::CRXFileInfo(good_crx
, path
), true, NULL
);
2701 ASSERT_FALSE(updated
);
2704 service()->GetExtensionById(good_crx
, false)->version()->GetString());
2707 // Test updating a not-already-installed extension - this should fail
2708 TEST_F(ExtensionServiceTest
, UpdateNotInstalledExtension
) {
2709 InitializeEmptyExtensionService();
2711 base::FilePath path
= data_dir().AppendASCII("good.crx");
2712 UpdateExtension(good_crx
, path
, UPDATED
);
2713 base::RunLoop().RunUntilIdle();
2715 ASSERT_EQ(0u, registry()->enabled_extensions().size());
2716 ASSERT_FALSE(installed_
);
2717 ASSERT_EQ(0u, loaded_
.size());
2720 // Makes sure you can't downgrade an extension via UpdateExtension
2721 TEST_F(ExtensionServiceTest
, UpdateWillNotDowngrade
) {
2722 InitializeEmptyExtensionService();
2724 base::FilePath path
= data_dir().AppendASCII("good2.crx");
2726 const Extension
* good
= InstallCRX(path
, INSTALL_NEW
);
2727 ASSERT_EQ("1.0.0.1", good
->VersionString());
2728 ASSERT_EQ(good_crx
, good
->id());
2730 // Change path from good2.crx -> good.crx
2731 path
= data_dir().AppendASCII("good.crx");
2732 UpdateExtension(good_crx
, path
, FAILED
);
2735 service()->GetExtensionById(good_crx
, false)->version()->GetString());
2738 // Make sure calling update with an identical version does nothing
2739 TEST_F(ExtensionServiceTest
, UpdateToSameVersionIsNoop
) {
2740 InitializeEmptyExtensionService();
2742 base::FilePath path
= data_dir().AppendASCII("good.crx");
2744 const Extension
* good
= InstallCRX(path
, INSTALL_NEW
);
2745 ASSERT_EQ(good_crx
, good
->id());
2746 UpdateExtension(good_crx
, path
, FAILED_SILENTLY
);
2749 // Tests that updating an extension does not clobber old state.
2750 TEST_F(ExtensionServiceTest
, UpdateExtensionPreservesState
) {
2751 InitializeEmptyExtensionService();
2753 base::FilePath path
= data_dir().AppendASCII("good.crx");
2755 const Extension
* good
= InstallCRX(path
, INSTALL_NEW
);
2756 ASSERT_EQ("1.0.0.0", good
->VersionString());
2757 ASSERT_EQ(good_crx
, good
->id());
2759 // Disable it and allow it to run in incognito. These settings should carry
2760 // over to the updated version.
2761 service()->DisableExtension(good
->id(), Extension::DISABLE_USER_ACTION
);
2762 extensions::util::SetIsIncognitoEnabled(good
->id(), profile(), true);
2763 ExtensionPrefs::Get(profile())
2764 ->SetDidExtensionEscalatePermissions(good
, true);
2766 path
= data_dir().AppendASCII("good2.crx");
2767 UpdateExtension(good_crx
, path
, INSTALLED
);
2768 ASSERT_EQ(1u, registry()->disabled_extensions().size());
2769 const Extension
* good2
= service()->GetExtensionById(good_crx
, true);
2770 ASSERT_EQ("1.0.0.1", good2
->version()->GetString());
2771 EXPECT_TRUE(extensions::util::IsIncognitoEnabled(good2
->id(), profile()));
2772 EXPECT_TRUE(ExtensionPrefs::Get(profile())
2773 ->DidExtensionEscalatePermissions(good2
->id()));
2776 // Tests that updating preserves extension location.
2777 TEST_F(ExtensionServiceTest
, UpdateExtensionPreservesLocation
) {
2778 InitializeEmptyExtensionService();
2780 base::FilePath path
= data_dir().AppendASCII("good.crx");
2782 const Extension
* good
=
2783 InstallCRXWithLocation(path
, Manifest::EXTERNAL_PREF
, INSTALL_NEW
);
2785 ASSERT_EQ("1.0.0.0", good
->VersionString());
2786 ASSERT_EQ(good_crx
, good
->id());
2788 path
= data_dir().AppendASCII("good2.crx");
2789 UpdateExtension(good_crx
, path
, ENABLED
);
2790 const Extension
* good2
= service()->GetExtensionById(good_crx
, false);
2791 ASSERT_EQ("1.0.0.1", good2
->version()->GetString());
2792 EXPECT_EQ(good2
->location(), Manifest::EXTERNAL_PREF
);
2795 // Makes sure that LOAD extension types can downgrade.
2796 TEST_F(ExtensionServiceTest
, LoadExtensionsCanDowngrade
) {
2797 InitializeEmptyExtensionService();
2799 base::ScopedTempDir temp
;
2800 ASSERT_TRUE(temp
.CreateUniqueTempDir());
2802 // We'll write the extension manifest dynamically to a temporary path
2803 // to make it easier to change the version number.
2804 base::FilePath extension_path
= temp
.path();
2805 base::FilePath manifest_path
=
2806 extension_path
.Append(extensions::kManifestFilename
);
2807 ASSERT_FALSE(base::PathExists(manifest_path
));
2809 // Start with version 2.0.
2810 base::DictionaryValue manifest
;
2811 manifest
.SetString("version", "2.0");
2812 manifest
.SetString("name", "LOAD Downgrade Test");
2813 manifest
.SetInteger("manifest_version", 2);
2815 JSONFileValueSerializer
serializer(manifest_path
);
2816 ASSERT_TRUE(serializer
.Serialize(manifest
));
2818 extensions::UnpackedInstaller::Create(service())->Load(extension_path
);
2819 base::RunLoop().RunUntilIdle();
2821 EXPECT_EQ(0u, GetErrors().size());
2822 ASSERT_EQ(1u, loaded_
.size());
2823 EXPECT_EQ(Manifest::UNPACKED
, loaded_
[0]->location());
2824 EXPECT_EQ(1u, registry()->enabled_extensions().size());
2825 EXPECT_EQ("2.0", loaded_
[0]->VersionString());
2827 // Now set the version number to 1.0, reload the extensions and verify that
2828 // the downgrade was accepted.
2829 manifest
.SetString("version", "1.0");
2830 ASSERT_TRUE(serializer
.Serialize(manifest
));
2832 extensions::UnpackedInstaller::Create(service())->Load(extension_path
);
2833 base::RunLoop().RunUntilIdle();
2835 EXPECT_EQ(0u, GetErrors().size());
2836 ASSERT_EQ(1u, loaded_
.size());
2837 EXPECT_EQ(Manifest::UNPACKED
, loaded_
[0]->location());
2838 EXPECT_EQ(1u, registry()->enabled_extensions().size());
2839 EXPECT_EQ("1.0", loaded_
[0]->VersionString());
2842 #if !defined(OS_POSIX) || defined(OS_MACOSX)
2843 // LOAD extensions with plugins require approval.
2844 // Only run this on platforms that support NPAPI plugins.
2845 TEST_F(ExtensionServiceTest
, LoadExtensionsWithPlugins
) {
2846 base::FilePath extension_with_plugin_path
= good1_path();
2847 base::FilePath extension_no_plugin_path
= good2_path();
2849 InitPluginService();
2850 InitializeEmptyExtensionService();
2851 service()->set_show_extensions_prompts(true);
2853 // Start by canceling any install prompts.
2854 ExtensionInstallPrompt::g_auto_confirm_for_tests
=
2855 ExtensionInstallPrompt::CANCEL
;
2857 // The extension that has a plugin should not install.
2858 extensions::UnpackedInstaller::Create(service())
2859 ->Load(extension_with_plugin_path
);
2860 base::RunLoop().RunUntilIdle();
2861 EXPECT_EQ(0u, GetErrors().size());
2862 EXPECT_EQ(0u, loaded_
.size());
2863 EXPECT_EQ(0u, registry()->enabled_extensions().size());
2864 EXPECT_EQ(0u, registry()->disabled_extensions().size());
2866 // But the extension with no plugin should since there's no prompt.
2867 ExtensionErrorReporter::GetInstance()->ClearErrors();
2868 extensions::UnpackedInstaller::Create(service())
2869 ->Load(extension_no_plugin_path
);
2870 base::RunLoop().RunUntilIdle();
2871 EXPECT_EQ(0u, GetErrors().size());
2872 EXPECT_EQ(1u, loaded_
.size());
2873 EXPECT_EQ(1u, registry()->enabled_extensions().size());
2874 EXPECT_EQ(0u, registry()->disabled_extensions().size());
2875 EXPECT_TRUE(registry()->enabled_extensions().Contains(good2
));
2877 // The plugin extension should install if we accept the dialog.
2878 ExtensionInstallPrompt::g_auto_confirm_for_tests
=
2879 ExtensionInstallPrompt::ACCEPT
;
2881 ExtensionErrorReporter::GetInstance()->ClearErrors();
2882 extensions::UnpackedInstaller::Create(service())
2883 ->Load(extension_with_plugin_path
);
2884 base::RunLoop().RunUntilIdle();
2885 EXPECT_EQ(0u, GetErrors().size());
2886 EXPECT_EQ(2u, loaded_
.size());
2887 EXPECT_EQ(2u, registry()->enabled_extensions().size());
2888 EXPECT_EQ(0u, registry()->disabled_extensions().size());
2889 EXPECT_TRUE(registry()->enabled_extensions().Contains(good1
));
2890 EXPECT_TRUE(registry()->enabled_extensions().Contains(good2
));
2892 // Make sure the granted permissions have been setup.
2893 scoped_refptr
<PermissionSet
> permissions(
2894 ExtensionPrefs::Get(profile())->GetGrantedPermissions(good1
));
2895 EXPECT_FALSE(permissions
->IsEmpty());
2896 EXPECT_TRUE(permissions
->HasEffectiveFullAccess());
2897 EXPECT_FALSE(permissions
->apis().empty());
2898 EXPECT_TRUE(permissions
->HasAPIPermission(APIPermission::kPlugin
));
2900 // We should be able to reload the extension without getting another prompt.
2902 ExtensionInstallPrompt::g_auto_confirm_for_tests
=
2903 ExtensionInstallPrompt::CANCEL
;
2905 service()->ReloadExtension(good1
);
2906 base::RunLoop().RunUntilIdle();
2907 EXPECT_EQ(1u, loaded_
.size());
2908 EXPECT_EQ(2u, registry()->enabled_extensions().size());
2909 EXPECT_EQ(0u, registry()->disabled_extensions().size());
2911 #endif // !defined(OS_POSIX) || defined(OS_MACOSX)
2915 bool IsExtension(const Extension
* extension
) {
2916 return extension
->GetType() == Manifest::TYPE_EXTENSION
;
2919 #if defined(ENABLE_BLACKLIST_TESTS)
2920 std::set
<std::string
> StringSet(const std::string
& s
) {
2921 std::set
<std::string
> set
;
2925 std::set
<std::string
> StringSet(const std::string
& s1
, const std::string
& s2
) {
2926 std::set
<std::string
> set
= StringSet(s1
);
2930 #endif // defined(ENABLE_BLACKLIST_TESTS)
2934 // Test adding a pending extension.
2935 TEST_F(ExtensionServiceTest
, AddPendingExtensionFromSync
) {
2936 InitializeEmptyExtensionService();
2938 const std::string
kFakeId(all_zero
);
2939 const GURL
kFakeUpdateURL("http:://fake.update/url");
2940 const bool kFakeRemoteInstall(false);
2941 const bool kFakeInstalledByCustodian(false);
2944 service()->pending_extension_manager()->AddFromSync(
2949 kFakeInstalledByCustodian
));
2951 const extensions::PendingExtensionInfo
* pending_extension_info
;
2952 ASSERT_TRUE((pending_extension_info
=
2953 service()->pending_extension_manager()->GetById(kFakeId
)));
2954 EXPECT_EQ(kFakeUpdateURL
, pending_extension_info
->update_url());
2955 EXPECT_EQ(&IsExtension
, pending_extension_info
->should_allow_install_
);
2957 // EXPECT_TRUE(kFakeRemoteInstall == pending_extension_info->remote_install())
2959 // EXPECT_EQ(kFakeRemoteInstall, pending_extension_info->remote_install())
2960 // as gcc 4.7 issues the following warning on EXPECT_EQ(false, x), which is
2961 // turned into an error with -Werror=conversion-null:
2962 // converting 'false' to pointer type for argument 1 of
2963 // 'char testing::internal::IsNullLiteralHelper(testing::internal::Secret*)'
2964 // https://code.google.com/p/googletest/issues/detail?id=458
2965 EXPECT_TRUE(kFakeRemoteInstall
== pending_extension_info
->remote_install());
2969 const char kGoodId
[] = "ldnnhddmnhbkjipkidpdiheffobcpfmf";
2970 const char kGoodUpdateURL
[] = "http://good.update/url";
2971 const bool kGoodIsFromSync
= true;
2972 const bool kGoodRemoteInstall
= false;
2973 const bool kGoodInstalledByCustodian
= false;
2976 // Test updating a pending extension.
2977 TEST_F(ExtensionServiceTest
, UpdatePendingExtension
) {
2978 InitializeEmptyExtensionService();
2980 service()->pending_extension_manager()->AddFromSync(
2982 GURL(kGoodUpdateURL
),
2985 kGoodInstalledByCustodian
));
2986 EXPECT_TRUE(service()->pending_extension_manager()->IsIdPending(kGoodId
));
2988 base::FilePath path
= data_dir().AppendASCII("good.crx");
2989 UpdateExtension(kGoodId
, path
, ENABLED
);
2991 EXPECT_FALSE(service()->pending_extension_manager()->IsIdPending(kGoodId
));
2993 const Extension
* extension
= service()->GetExtensionById(kGoodId
, true);
2994 ASSERT_TRUE(extension
);
2999 bool IsTheme(const Extension
* extension
) {
3000 return extension
->is_theme();
3005 // Test updating a pending theme.
3006 // Disabled due to ASAN failure. http://crbug.com/108320
3007 TEST_F(ExtensionServiceTest
, DISABLED_UpdatePendingTheme
) {
3008 InitializeEmptyExtensionService();
3009 EXPECT_TRUE(service()->pending_extension_manager()->AddFromSync(
3010 theme_crx
, GURL(), &IsTheme
, false, false));
3011 EXPECT_TRUE(service()->pending_extension_manager()->IsIdPending(theme_crx
));
3013 base::FilePath path
= data_dir().AppendASCII("theme.crx");
3014 UpdateExtension(theme_crx
, path
, ENABLED
);
3016 EXPECT_FALSE(service()->pending_extension_manager()->IsIdPending(theme_crx
));
3018 const Extension
* extension
= service()->GetExtensionById(theme_crx
, true);
3019 ASSERT_TRUE(extension
);
3022 ExtensionPrefs::Get(profile())->IsExtensionDisabled(extension
->id()));
3023 EXPECT_TRUE(service()->IsExtensionEnabled(theme_crx
));
3026 #if defined(OS_CHROMEOS)
3027 // Always fails on ChromeOS: http://crbug.com/79737
3028 #define MAYBE_UpdatePendingExternalCrx DISABLED_UpdatePendingExternalCrx
3030 #define MAYBE_UpdatePendingExternalCrx UpdatePendingExternalCrx
3032 // Test updating a pending CRX as if the source is an external extension
3033 // with an update URL. In this case we don't know if the CRX is a theme
3035 TEST_F(ExtensionServiceTest
, MAYBE_UpdatePendingExternalCrx
) {
3036 InitializeEmptyExtensionService();
3037 EXPECT_TRUE(service()->pending_extension_manager()->AddFromExternalUpdateUrl(
3041 Manifest::EXTERNAL_PREF_DOWNLOAD
,
3042 Extension::NO_FLAGS
,
3045 EXPECT_TRUE(service()->pending_extension_manager()->IsIdPending(theme_crx
));
3047 base::FilePath path
= data_dir().AppendASCII("theme.crx");
3048 UpdateExtension(theme_crx
, path
, ENABLED
);
3050 EXPECT_FALSE(service()->pending_extension_manager()->IsIdPending(theme_crx
));
3052 const Extension
* extension
= service()->GetExtensionById(theme_crx
, true);
3053 ASSERT_TRUE(extension
);
3056 ExtensionPrefs::Get(profile())->IsExtensionDisabled(extension
->id()));
3057 EXPECT_TRUE(service()->IsExtensionEnabled(extension
->id()));
3059 extensions::util::IsIncognitoEnabled(extension
->id(), profile()));
3062 // Test updating a pending CRX as if the source is an external extension
3063 // with an update URL. The external update should overwrite a sync update,
3064 // but a sync update should not overwrite a non-sync update.
3065 TEST_F(ExtensionServiceTest
, UpdatePendingExternalCrxWinsOverSync
) {
3066 InitializeEmptyExtensionService();
3068 // Add a crx to be installed from the update mechanism.
3070 service()->pending_extension_manager()->AddFromSync(
3072 GURL(kGoodUpdateURL
),
3075 kGoodInstalledByCustodian
));
3077 // Check that there is a pending crx, with is_from_sync set to true.
3078 const extensions::PendingExtensionInfo
* pending_extension_info
;
3079 ASSERT_TRUE((pending_extension_info
=
3080 service()->pending_extension_manager()->GetById(kGoodId
)));
3081 EXPECT_TRUE(pending_extension_info
->is_from_sync());
3083 // Add a crx to be updated, with the same ID, from a non-sync source.
3084 EXPECT_TRUE(service()->pending_extension_manager()->AddFromExternalUpdateUrl(
3087 GURL(kGoodUpdateURL
),
3088 Manifest::EXTERNAL_PREF_DOWNLOAD
,
3089 Extension::NO_FLAGS
,
3092 // Check that there is a pending crx, with is_from_sync set to false.
3093 ASSERT_TRUE((pending_extension_info
=
3094 service()->pending_extension_manager()->GetById(kGoodId
)));
3095 EXPECT_FALSE(pending_extension_info
->is_from_sync());
3096 EXPECT_EQ(Manifest::EXTERNAL_PREF_DOWNLOAD
,
3097 pending_extension_info
->install_source());
3099 // Add a crx to be installed from the update mechanism.
3101 service()->pending_extension_manager()->AddFromSync(
3103 GURL(kGoodUpdateURL
),
3106 kGoodInstalledByCustodian
));
3108 // Check that the external, non-sync update was not overridden.
3109 ASSERT_TRUE((pending_extension_info
=
3110 service()->pending_extension_manager()->GetById(kGoodId
)));
3111 EXPECT_FALSE(pending_extension_info
->is_from_sync());
3112 EXPECT_EQ(Manifest::EXTERNAL_PREF_DOWNLOAD
,
3113 pending_extension_info
->install_source());
3116 // Updating a theme should fail if the updater is explicitly told that
3117 // the CRX is not a theme.
3118 TEST_F(ExtensionServiceTest
, UpdatePendingCrxThemeMismatch
) {
3119 InitializeEmptyExtensionService();
3120 EXPECT_TRUE(service()->pending_extension_manager()->AddFromSync(
3121 theme_crx
, GURL(), &IsExtension
, false, false));
3123 EXPECT_TRUE(service()->pending_extension_manager()->IsIdPending(theme_crx
));
3125 base::FilePath path
= data_dir().AppendASCII("theme.crx");
3126 UpdateExtension(theme_crx
, path
, FAILED_SILENTLY
);
3128 EXPECT_FALSE(service()->pending_extension_manager()->IsIdPending(theme_crx
));
3130 const Extension
* extension
= service()->GetExtensionById(theme_crx
, true);
3131 ASSERT_FALSE(extension
);
3134 // TODO(akalin): Test updating a pending extension non-silently once
3135 // we can mock out ExtensionInstallUI and inject our version into
3136 // UpdateExtension().
3138 // Test updating a pending extension which fails the should-install test.
3139 TEST_F(ExtensionServiceTest
, UpdatePendingExtensionFailedShouldInstallTest
) {
3140 InitializeEmptyExtensionService();
3141 // Add pending extension with a flipped is_theme.
3143 service()->pending_extension_manager()->AddFromSync(
3145 GURL(kGoodUpdateURL
),
3148 kGoodInstalledByCustodian
));
3149 EXPECT_TRUE(service()->pending_extension_manager()->IsIdPending(kGoodId
));
3151 base::FilePath path
= data_dir().AppendASCII("good.crx");
3152 UpdateExtension(kGoodId
, path
, UPDATED
);
3154 // TODO(akalin): Figure out how to check that the extensions
3155 // directory is cleaned up properly in OnExtensionInstalled().
3157 EXPECT_FALSE(service()->pending_extension_manager()->IsIdPending(kGoodId
));
3160 // TODO(akalin): Figure out how to test that installs of pending
3161 // unsyncable extensions are blocked.
3163 // Test updating a pending extension for one that is not pending.
3164 TEST_F(ExtensionServiceTest
, UpdatePendingExtensionNotPending
) {
3165 InitializeEmptyExtensionService();
3167 base::FilePath path
= data_dir().AppendASCII("good.crx");
3168 UpdateExtension(kGoodId
, path
, UPDATED
);
3170 EXPECT_FALSE(service()->pending_extension_manager()->IsIdPending(kGoodId
));
3173 // Test updating a pending extension for one that is already
3175 TEST_F(ExtensionServiceTest
, UpdatePendingExtensionAlreadyInstalled
) {
3176 InitializeEmptyExtensionService();
3178 base::FilePath path
= data_dir().AppendASCII("good.crx");
3179 const Extension
* good
= InstallCRX(path
, INSTALL_NEW
);
3180 ASSERT_EQ(1u, registry()->enabled_extensions().size());
3182 EXPECT_FALSE(good
->is_theme());
3184 // Use AddExtensionImpl() as AddFrom*() would balk.
3185 service()->pending_extension_manager()->AddExtensionImpl(
3188 extensions::ManifestURL::GetUpdateURL(good
),
3193 Extension::NO_FLAGS
,
3195 kGoodRemoteInstall
);
3196 UpdateExtension(good
->id(), path
, ENABLED
);
3198 EXPECT_FALSE(service()->pending_extension_manager()->IsIdPending(kGoodId
));
3201 #if defined(ENABLE_BLACKLIST_TESTS)
3202 // Tests blacklisting then unblacklisting extensions after the service has been
3204 TEST_F(ExtensionServiceTest
, SetUnsetBlacklistInPrefs
) {
3205 extensions::TestBlacklist test_blacklist
;
3206 // A profile with 3 extensions installed: good0, good1, and good2.
3207 InitializeGoodInstalledExtensionService();
3208 test_blacklist
.Attach(service()->blacklist_
);
3211 const extensions::ExtensionSet
& enabled_extensions
=
3212 registry()->enabled_extensions();
3213 const extensions::ExtensionSet
& blacklisted_extensions
=
3214 registry()->blacklisted_extensions();
3216 EXPECT_TRUE(enabled_extensions
.Contains(good0
) &&
3217 !blacklisted_extensions
.Contains(good0
));
3218 EXPECT_TRUE(enabled_extensions
.Contains(good1
) &&
3219 !blacklisted_extensions
.Contains(good1
));
3220 EXPECT_TRUE(enabled_extensions
.Contains(good2
) &&
3221 !blacklisted_extensions
.Contains(good2
));
3223 EXPECT_FALSE(IsPrefExist(good0
, "blacklist"));
3224 EXPECT_FALSE(IsPrefExist(good1
, "blacklist"));
3225 EXPECT_FALSE(IsPrefExist(good2
, "blacklist"));
3226 EXPECT_FALSE(IsPrefExist("invalid_id", "blacklist"));
3228 // Blacklist good0 and good1 (and an invalid extension ID).
3229 test_blacklist
.SetBlacklistState(
3230 good0
, extensions::BLACKLISTED_MALWARE
, true);
3231 test_blacklist
.SetBlacklistState(
3232 good1
, extensions::BLACKLISTED_MALWARE
, true);
3233 test_blacklist
.SetBlacklistState(
3234 "invalid_id", extensions::BLACKLISTED_MALWARE
, true);
3235 base::RunLoop().RunUntilIdle();
3237 EXPECT_TRUE(!enabled_extensions
.Contains(good0
) &&
3238 blacklisted_extensions
.Contains(good0
));
3239 EXPECT_TRUE(!enabled_extensions
.Contains(good1
) &&
3240 blacklisted_extensions
.Contains(good1
));
3241 EXPECT_TRUE(enabled_extensions
.Contains(good2
) &&
3242 !blacklisted_extensions
.Contains(good2
));
3244 EXPECT_TRUE(ValidateBooleanPref(good0
, "blacklist", true));
3245 EXPECT_TRUE(ValidateBooleanPref(good1
, "blacklist", true));
3246 EXPECT_FALSE(IsPrefExist(good2
, "blacklist"));
3247 EXPECT_FALSE(IsPrefExist("invalid_id", "blacklist"));
3249 // Un-blacklist good1 and blacklist good2.
3250 test_blacklist
.Clear(false);
3251 test_blacklist
.SetBlacklistState(
3252 good0
, extensions::BLACKLISTED_MALWARE
, true);
3253 test_blacklist
.SetBlacklistState(
3254 good2
, extensions::BLACKLISTED_MALWARE
, true);
3255 test_blacklist
.SetBlacklistState(
3256 "invalid_id", extensions::BLACKLISTED_MALWARE
, true);
3257 base::RunLoop().RunUntilIdle();
3259 EXPECT_TRUE(!enabled_extensions
.Contains(good0
) &&
3260 blacklisted_extensions
.Contains(good0
));
3261 EXPECT_TRUE(enabled_extensions
.Contains(good1
) &&
3262 !blacklisted_extensions
.Contains(good1
));
3263 EXPECT_TRUE(!enabled_extensions
.Contains(good2
) &&
3264 blacklisted_extensions
.Contains(good2
));
3266 EXPECT_TRUE(ValidateBooleanPref(good0
, "blacklist", true));
3267 EXPECT_FALSE(IsPrefExist(good1
, "blacklist"));
3268 EXPECT_TRUE(ValidateBooleanPref(good2
, "blacklist", true));
3269 EXPECT_FALSE(IsPrefExist("invalid_id", "blacklist"));
3271 #endif // defined(ENABLE_BLACKLIST_TESTS)
3273 #if defined(ENABLE_BLACKLIST_TESTS)
3274 // Tests trying to install a blacklisted extension.
3275 TEST_F(ExtensionServiceTest
, BlacklistedExtensionWillNotInstall
) {
3276 scoped_refptr
<FakeSafeBrowsingDatabaseManager
> blacklist_db(
3277 new FakeSafeBrowsingDatabaseManager(true));
3278 Blacklist::ScopedDatabaseManagerForTest
scoped_blacklist_db(blacklist_db
);
3280 InitializeEmptyExtensionService();
3283 // After blacklisting good_crx, we cannot install it.
3284 blacklist_db
->SetUnsafe(good_crx
).NotifyUpdate();
3285 base::RunLoop().RunUntilIdle();
3287 base::FilePath path
= data_dir().AppendASCII("good.crx");
3288 // HACK: specify WAS_INSTALLED_BY_DEFAULT so that test machinery doesn't
3289 // decide to install this silently. Somebody should fix these tests, all
3290 // 6,000 lines of them. Hah!
3291 InstallCRX(path
, INSTALL_FAILED
, Extension::WAS_INSTALLED_BY_DEFAULT
);
3292 EXPECT_EQ(0u, registry()->enabled_extensions().size());
3294 #endif // defined(ENABLE_BLACKLIST_TESTS)
3296 #if defined(ENABLE_BLACKLIST_TESTS)
3297 // Unload blacklisted extension on policy change.
3298 TEST_F(ExtensionServiceTest
, UnloadBlacklistedExtensionPolicy
) {
3299 extensions::TestBlacklist test_blacklist
;
3301 // A profile with no extensions installed.
3302 InitializeEmptyExtensionServiceWithTestingPrefs();
3303 test_blacklist
.Attach(service()->blacklist_
);
3305 base::FilePath path
= data_dir().AppendASCII("good.crx");
3307 const Extension
* good
= InstallCRX(path
, INSTALL_NEW
);
3308 EXPECT_EQ(good_crx
, good
->id());
3309 UpdateExtension(good_crx
, path
, FAILED_SILENTLY
);
3310 EXPECT_EQ(1u, registry()->enabled_extensions().size());
3313 ManagementPrefUpdater
pref(profile_
->GetTestingPrefService());
3314 pref
.SetIndividualExtensionInstallationAllowed(good_crx
, true);
3317 test_blacklist
.SetBlacklistState(
3318 good_crx
, extensions::BLACKLISTED_MALWARE
, true);
3319 base::RunLoop().RunUntilIdle();
3321 // The good_crx is blacklisted and the whitelist doesn't negate it.
3322 ASSERT_TRUE(ValidateBooleanPref(good_crx
, "blacklist", true));
3323 EXPECT_EQ(0u, registry()->enabled_extensions().size());
3325 #endif // defined(ENABLE_BLACKLIST_TESTS)
3327 #if defined(ENABLE_BLACKLIST_TESTS)
3328 // Tests that a blacklisted extension is eventually unloaded on startup, if it
3330 TEST_F(ExtensionServiceTest
, WillNotLoadBlacklistedExtensionsFromDirectory
) {
3331 extensions::TestBlacklist test_blacklist
;
3333 // A profile with 3 extensions installed: good0, good1, and good2.
3334 InitializeGoodInstalledExtensionService();
3335 test_blacklist
.Attach(service()->blacklist_
);
3337 // Blacklist good1 before the service initializes.
3338 test_blacklist
.SetBlacklistState(
3339 good1
, extensions::BLACKLISTED_MALWARE
, false);
3343 ASSERT_EQ(3u, loaded_
.size()); // hasn't had time to blacklist yet
3345 base::RunLoop().RunUntilIdle();
3347 ASSERT_EQ(1u, registry()->blacklisted_extensions().size());
3348 ASSERT_EQ(2u, registry()->enabled_extensions().size());
3350 ASSERT_TRUE(registry()->enabled_extensions().Contains(good0
));
3351 ASSERT_TRUE(registry()->blacklisted_extensions().Contains(good1
));
3352 ASSERT_TRUE(registry()->enabled_extensions().Contains(good2
));
3354 #endif // defined(ENABLE_BLACKLIST_TESTS)
3356 #if defined(ENABLE_BLACKLIST_TESTS)
3357 // Tests extensions blacklisted in prefs on startup; one still blacklisted by
3358 // safe browsing, the other not. The not-blacklisted one should recover.
3359 TEST_F(ExtensionServiceTest
, BlacklistedInPrefsFromStartup
) {
3360 extensions::TestBlacklist test_blacklist
;
3362 InitializeGoodInstalledExtensionService();
3363 test_blacklist
.Attach(service()->blacklist_
);
3364 ExtensionPrefs::Get(profile())->SetExtensionBlacklisted(good0
, true);
3365 ExtensionPrefs::Get(profile())->SetExtensionBlacklisted(good1
, true);
3367 test_blacklist
.SetBlacklistState(
3368 good1
, extensions::BLACKLISTED_MALWARE
, false);
3370 // Extension service hasn't loaded yet, but IsExtensionEnabled reads out of
3371 // prefs. Ensure it takes into account the blacklist state (crbug.com/373842).
3372 EXPECT_FALSE(service()->IsExtensionEnabled(good0
));
3373 EXPECT_FALSE(service()->IsExtensionEnabled(good1
));
3374 EXPECT_TRUE(service()->IsExtensionEnabled(good2
));
3378 EXPECT_EQ(2u, registry()->blacklisted_extensions().size());
3379 EXPECT_EQ(1u, registry()->enabled_extensions().size());
3381 EXPECT_TRUE(registry()->blacklisted_extensions().Contains(good0
));
3382 EXPECT_TRUE(registry()->blacklisted_extensions().Contains(good1
));
3383 EXPECT_TRUE(registry()->enabled_extensions().Contains(good2
));
3385 // Give time for the blacklist to update.
3386 base::RunLoop().RunUntilIdle();
3388 EXPECT_EQ(1u, registry()->blacklisted_extensions().size());
3389 EXPECT_EQ(2u, registry()->enabled_extensions().size());
3391 EXPECT_TRUE(registry()->enabled_extensions().Contains(good0
));
3392 EXPECT_TRUE(registry()->blacklisted_extensions().Contains(good1
));
3393 EXPECT_TRUE(registry()->enabled_extensions().Contains(good2
));
3395 #endif // defined(ENABLE_BLACKLIST_TESTS)
3397 #if defined(ENABLE_BLACKLIST_TESTS)
3398 // Extension is added to blacklist with BLACKLISTED_POTENTIALLY_UNWANTED state
3399 // after it is installed. It is then successfully re-enabled by the user.
3400 TEST_F(ExtensionServiceTest
, GreylistedExtensionDisabled
) {
3401 extensions::TestBlacklist test_blacklist
;
3402 // A profile with 3 extensions installed: good0, good1, and good2.
3403 InitializeGoodInstalledExtensionService();
3404 test_blacklist
.Attach(service()->blacklist_
);
3407 const extensions::ExtensionSet
& enabled_extensions
=
3408 registry()->enabled_extensions();
3409 const extensions::ExtensionSet
& disabled_extensions
=
3410 registry()->disabled_extensions();
3412 EXPECT_TRUE(enabled_extensions
.Contains(good0
));
3413 EXPECT_TRUE(enabled_extensions
.Contains(good1
));
3414 EXPECT_TRUE(enabled_extensions
.Contains(good2
));
3416 // Blacklist good0 and good1 (and an invalid extension ID).
3417 test_blacklist
.SetBlacklistState(
3418 good0
, extensions::BLACKLISTED_CWS_POLICY_VIOLATION
, true);
3419 test_blacklist
.SetBlacklistState(
3420 good1
, extensions::BLACKLISTED_POTENTIALLY_UNWANTED
, true);
3421 test_blacklist
.SetBlacklistState(
3422 "invalid_id", extensions::BLACKLISTED_MALWARE
, true);
3423 base::RunLoop().RunUntilIdle();
3425 EXPECT_FALSE(enabled_extensions
.Contains(good0
));
3426 EXPECT_TRUE(disabled_extensions
.Contains(good0
));
3427 EXPECT_FALSE(enabled_extensions
.Contains(good1
));
3428 EXPECT_TRUE(disabled_extensions
.Contains(good1
));
3429 EXPECT_TRUE(enabled_extensions
.Contains(good2
));
3430 EXPECT_FALSE(disabled_extensions
.Contains(good2
));
3432 ValidateIntegerPref(
3433 good0
, "blacklist_state", extensions::BLACKLISTED_CWS_POLICY_VIOLATION
);
3434 ValidateIntegerPref(
3435 good1
, "blacklist_state", extensions::BLACKLISTED_POTENTIALLY_UNWANTED
);
3437 // Now user enables good0.
3438 service()->EnableExtension(good0
);
3440 EXPECT_TRUE(enabled_extensions
.Contains(good0
));
3441 EXPECT_FALSE(disabled_extensions
.Contains(good0
));
3442 EXPECT_FALSE(enabled_extensions
.Contains(good1
));
3443 EXPECT_TRUE(disabled_extensions
.Contains(good1
));
3445 // Remove extensions from blacklist.
3446 test_blacklist
.SetBlacklistState(
3447 good0
, extensions::NOT_BLACKLISTED
, true);
3448 test_blacklist
.SetBlacklistState(
3449 good1
, extensions::NOT_BLACKLISTED
, true);
3450 base::RunLoop().RunUntilIdle();
3452 // All extensions are enabled.
3453 EXPECT_TRUE(enabled_extensions
.Contains(good0
));
3454 EXPECT_FALSE(disabled_extensions
.Contains(good0
));
3455 EXPECT_TRUE(enabled_extensions
.Contains(good1
));
3456 EXPECT_FALSE(disabled_extensions
.Contains(good1
));
3457 EXPECT_TRUE(enabled_extensions
.Contains(good2
));
3458 EXPECT_FALSE(disabled_extensions
.Contains(good2
));
3460 #endif // defined(ENABLE_BLACKLIST_TESTS)
3462 #if defined(ENABLE_BLACKLIST_TESTS)
3463 // When extension is removed from greylist, do not re-enable it if it is
3464 // disabled by user.
3465 TEST_F(ExtensionServiceTest
, GreylistDontEnableManuallyDisabled
) {
3466 extensions::TestBlacklist test_blacklist
;
3467 // A profile with 3 extensions installed: good0, good1, and good2.
3468 InitializeGoodInstalledExtensionService();
3469 test_blacklist
.Attach(service()->blacklist_
);
3472 const extensions::ExtensionSet
& enabled_extensions
=
3473 registry()->enabled_extensions();
3474 const extensions::ExtensionSet
& disabled_extensions
=
3475 registry()->disabled_extensions();
3477 // Manually disable.
3478 service()->DisableExtension(good0
,
3479 extensions::Extension::DISABLE_USER_ACTION
);
3481 test_blacklist
.SetBlacklistState(
3482 good0
, extensions::BLACKLISTED_CWS_POLICY_VIOLATION
, true);
3483 test_blacklist
.SetBlacklistState(
3484 good1
, extensions::BLACKLISTED_POTENTIALLY_UNWANTED
, true);
3485 test_blacklist
.SetBlacklistState(
3486 good2
, extensions::BLACKLISTED_SECURITY_VULNERABILITY
, true);
3487 base::RunLoop().RunUntilIdle();
3489 // All extensions disabled.
3490 EXPECT_FALSE(enabled_extensions
.Contains(good0
));
3491 EXPECT_TRUE(disabled_extensions
.Contains(good0
));
3492 EXPECT_FALSE(enabled_extensions
.Contains(good1
));
3493 EXPECT_TRUE(disabled_extensions
.Contains(good1
));
3494 EXPECT_FALSE(enabled_extensions
.Contains(good2
));
3495 EXPECT_TRUE(disabled_extensions
.Contains(good2
));
3497 // Greylisted extension can be enabled.
3498 service()->EnableExtension(good1
);
3499 EXPECT_TRUE(enabled_extensions
.Contains(good1
));
3500 EXPECT_FALSE(disabled_extensions
.Contains(good1
));
3502 // good1 is now manually disabled.
3503 service()->DisableExtension(good1
,
3504 extensions::Extension::DISABLE_USER_ACTION
);
3505 EXPECT_FALSE(enabled_extensions
.Contains(good1
));
3506 EXPECT_TRUE(disabled_extensions
.Contains(good1
));
3508 // Remove extensions from blacklist.
3509 test_blacklist
.SetBlacklistState(
3510 good0
, extensions::NOT_BLACKLISTED
, true);
3511 test_blacklist
.SetBlacklistState(
3512 good1
, extensions::NOT_BLACKLISTED
, true);
3513 test_blacklist
.SetBlacklistState(
3514 good2
, extensions::NOT_BLACKLISTED
, true);
3515 base::RunLoop().RunUntilIdle();
3517 // good0 and good1 remain disabled.
3518 EXPECT_FALSE(enabled_extensions
.Contains(good0
));
3519 EXPECT_TRUE(disabled_extensions
.Contains(good0
));
3520 EXPECT_FALSE(enabled_extensions
.Contains(good1
));
3521 EXPECT_TRUE(disabled_extensions
.Contains(good1
));
3522 EXPECT_TRUE(enabled_extensions
.Contains(good2
));
3523 EXPECT_FALSE(disabled_extensions
.Contains(good2
));
3525 #endif // defined(ENABLE_BLACKLIST_TESTS)
3527 #if defined(ENABLE_BLACKLIST_TESTS)
3528 // Blacklisted extension with unknown state are not enabled/disabled.
3529 TEST_F(ExtensionServiceTest
, GreylistUnknownDontChange
) {
3530 extensions::TestBlacklist test_blacklist
;
3531 // A profile with 3 extensions installed: good0, good1, and good2.
3532 InitializeGoodInstalledExtensionService();
3533 test_blacklist
.Attach(service()->blacklist_
);
3536 const extensions::ExtensionSet
& enabled_extensions
=
3537 registry()->enabled_extensions();
3538 const extensions::ExtensionSet
& disabled_extensions
=
3539 registry()->disabled_extensions();
3541 test_blacklist
.SetBlacklistState(
3542 good0
, extensions::BLACKLISTED_CWS_POLICY_VIOLATION
, true);
3543 test_blacklist
.SetBlacklistState(
3544 good1
, extensions::BLACKLISTED_POTENTIALLY_UNWANTED
, true);
3545 base::RunLoop().RunUntilIdle();
3547 EXPECT_FALSE(enabled_extensions
.Contains(good0
));
3548 EXPECT_TRUE(disabled_extensions
.Contains(good0
));
3549 EXPECT_FALSE(enabled_extensions
.Contains(good1
));
3550 EXPECT_TRUE(disabled_extensions
.Contains(good1
));
3551 EXPECT_TRUE(enabled_extensions
.Contains(good2
));
3552 EXPECT_FALSE(disabled_extensions
.Contains(good2
));
3554 test_blacklist
.SetBlacklistState(
3555 good0
, extensions::NOT_BLACKLISTED
, true);
3556 test_blacklist
.SetBlacklistState(
3557 good1
, extensions::BLACKLISTED_UNKNOWN
, true);
3558 test_blacklist
.SetBlacklistState(
3559 good2
, extensions::BLACKLISTED_UNKNOWN
, true);
3560 base::RunLoop().RunUntilIdle();
3562 // good0 re-enabled, other remain as they were.
3563 EXPECT_TRUE(enabled_extensions
.Contains(good0
));
3564 EXPECT_FALSE(disabled_extensions
.Contains(good0
));
3565 EXPECT_FALSE(enabled_extensions
.Contains(good1
));
3566 EXPECT_TRUE(disabled_extensions
.Contains(good1
));
3567 EXPECT_TRUE(enabled_extensions
.Contains(good2
));
3568 EXPECT_FALSE(disabled_extensions
.Contains(good2
));
3571 // Tests that blacklisted extensions cannot be reloaded, both those loaded
3572 // before and after extension service startup.
3573 TEST_F(ExtensionServiceTest
, ReloadBlacklistedExtension
) {
3574 extensions::TestBlacklist test_blacklist
;
3576 InitializeGoodInstalledExtensionService();
3577 test_blacklist
.Attach(service()->blacklist_
);
3579 test_blacklist
.SetBlacklistState(
3580 good1
, extensions::BLACKLISTED_MALWARE
, false);
3582 test_blacklist
.SetBlacklistState(
3583 good2
, extensions::BLACKLISTED_MALWARE
, false);
3584 base::RunLoop().RunUntilIdle();
3586 EXPECT_EQ(StringSet(good0
), registry()->enabled_extensions().GetIDs());
3587 EXPECT_EQ(StringSet(good1
, good2
),
3588 registry()->blacklisted_extensions().GetIDs());
3590 service()->ReloadExtension(good1
);
3591 service()->ReloadExtension(good2
);
3592 base::RunLoop().RunUntilIdle();
3594 EXPECT_EQ(StringSet(good0
), registry()->enabled_extensions().GetIDs());
3595 EXPECT_EQ(StringSet(good1
, good2
),
3596 registry()->blacklisted_extensions().GetIDs());
3598 #endif // defined(ENABLE_BLACKLIST_TESTS)
3600 // Tests blocking then unblocking enabled extensions after the service has been
3602 TEST_F(ExtensionServiceTest
, BlockAndUnblockEnabledExtension
) {
3603 InitializeGoodInstalledExtensionService();
3606 AssertExtensionBlocksAndUnblocks(true, good0
);
3609 // Tests blocking then unblocking disabled extensions after the service has been
3611 TEST_F(ExtensionServiceTest
, BlockAndUnblockDisabledExtension
) {
3612 InitializeGoodInstalledExtensionService();
3615 service()->DisableExtension(good0
, Extension::DISABLE_RELOAD
);
3617 AssertExtensionBlocksAndUnblocks(true, good0
);
3620 // Tests blocking then unblocking terminated extensions after the service has
3621 // been initialized.
3622 TEST_F(ExtensionServiceTest
, BlockAndUnblockTerminatedExtension
) {
3623 InitializeGoodInstalledExtensionService();
3626 TerminateExtension(good0
);
3628 AssertExtensionBlocksAndUnblocks(true, good0
);
3631 // Tests blocking then unblocking policy-forced extensions after the service has
3632 // been initialized.
3633 TEST_F(ExtensionServiceTest
, BlockAndUnblockPolicyExtension
) {
3634 InitializeEmptyExtensionServiceWithTestingPrefs();
3637 ManagementPrefUpdater
pref(profile_
->GetTestingPrefService());
3638 // // Blacklist everything.
3639 // pref.SetBlacklistedByDefault(true);
3640 // Mark good.crx for force-installation.
3641 pref
.SetIndividualExtensionAutoInstalled(
3642 good_crx
, "http://example.com/update_url", true);
3645 // Have policy force-install an extension.
3646 MockExtensionProvider
* provider
=
3647 new MockExtensionProvider(service(), Manifest::EXTERNAL_POLICY_DOWNLOAD
);
3648 AddMockExternalProvider(provider
);
3649 provider
->UpdateOrAddExtension(
3650 good_crx
, "1.0.0.0", data_dir().AppendASCII("good_crx"));
3652 // Reloading extensions should find our externally registered extension
3654 content::WindowedNotificationObserver
observer(
3655 extensions::NOTIFICATION_CRX_INSTALLER_DONE
,
3656 content::NotificationService::AllSources());
3657 service()->CheckForExternalUpdates();
3660 AssertExtensionBlocksAndUnblocks(false, good_crx
);
3664 #if defined(ENABLE_BLACKLIST_TESTS)
3665 // Tests blocking then unblocking extensions that are blacklisted both before
3666 // and after Init().
3667 TEST_F(ExtensionServiceTest
, BlockAndUnblockBlacklistedExtension
) {
3668 extensions::TestBlacklist test_blacklist
;
3670 InitializeGoodInstalledExtensionService();
3671 test_blacklist
.Attach(service()->blacklist_
);
3673 test_blacklist
.SetBlacklistState(
3674 good0
, extensions::BLACKLISTED_MALWARE
, true);
3675 base::RunLoop().RunUntilIdle();
3679 test_blacklist
.SetBlacklistState(
3680 good1
, extensions::BLACKLISTED_MALWARE
, true);
3681 base::RunLoop().RunUntilIdle();
3683 // Blacklisted extensions stay blacklisted.
3684 AssertExtensionBlocksAndUnblocks(false, good0
);
3685 AssertExtensionBlocksAndUnblocks(false, good1
);
3687 service()->BlockAllExtensions();
3689 // Remove an extension from the blacklist while the service is blocked.
3690 test_blacklist
.SetBlacklistState(
3691 good0
, extensions::NOT_BLACKLISTED
, true);
3692 // Add an extension to the blacklist while the service is blocked.
3693 test_blacklist
.SetBlacklistState(
3694 good2
, extensions::BLACKLISTED_MALWARE
, true);
3695 base::RunLoop().RunUntilIdle();
3697 // Go directly to blocked, do not pass go, do not collect $200.
3698 ASSERT_TRUE(IsBlocked(good0
));
3699 // Get on the blacklist - even if you were blocked!
3700 ASSERT_FALSE(IsBlocked(good2
));
3702 #endif // defined(ENABLE_BLACKLIST_TESTS)
3704 // Tests blocking then unblocking enabled component extensions after the service
3705 // has been initialized.
3706 TEST_F(ExtensionServiceTest
, BlockAndUnblockEnabledComponentExtension
) {
3707 InitializeEmptyExtensionServiceWithTestingPrefs();
3709 // Install a component extension.
3710 base::FilePath path
= data_dir()
3711 .AppendASCII("good")
3712 .AppendASCII("Extensions")
3714 .AppendASCII("1.0.0.0");
3715 std::string manifest
;
3716 ASSERT_TRUE(base::ReadFileToString(
3717 path
.Append(extensions::kManifestFilename
), &manifest
));
3718 service()->component_loader()->Add(manifest
, path
);
3721 // Component extension should never block.
3722 AssertExtensionBlocksAndUnblocks(false, good0
);
3725 // Tests blocking then unblocking a theme after the service has been
3727 TEST_F(ExtensionServiceTest
, BlockAndUnblockTheme
) {
3728 InitializeEmptyExtensionService();
3731 base::FilePath path
= data_dir().AppendASCII("theme.crx");
3732 InstallCRX(path
, INSTALL_NEW
);
3734 AssertExtensionBlocksAndUnblocks(true, theme_crx
);
3737 // Tests that blocking extensions before Init() results in loading blocked
3739 TEST_F(ExtensionServiceTest
, WillNotLoadExtensionsWhenBlocked
) {
3740 InitializeGoodInstalledExtensionService();
3742 service()->BlockAllExtensions();
3746 ASSERT_TRUE(IsBlocked(good0
));
3747 ASSERT_TRUE(IsBlocked(good0
));
3748 ASSERT_TRUE(IsBlocked(good0
));
3751 // Tests that IsEnabledExtension won't crash on an uninstalled extension.
3752 TEST_F(ExtensionServiceTest
, IsEnabledExtensionBlockedAndNotInstalled
) {
3753 InitializeEmptyExtensionService();
3755 service()->BlockAllExtensions();
3757 service()->IsExtensionEnabled(theme_crx
);
3760 // Will not install extension blacklisted by policy.
3761 TEST_F(ExtensionServiceTest
, BlacklistedByPolicyWillNotInstall
) {
3762 InitializeEmptyExtensionServiceWithTestingPrefs();
3764 // Blacklist everything.
3766 ManagementPrefUpdater
pref(profile_
->GetTestingPrefService());
3767 pref
.SetBlacklistedByDefault(true);
3770 // Blacklist prevents us from installing good_crx.
3771 base::FilePath path
= data_dir().AppendASCII("good.crx");
3772 InstallCRX(path
, INSTALL_FAILED
);
3773 EXPECT_EQ(0u, registry()->enabled_extensions().size());
3775 // Now whitelist this particular extension.
3777 ManagementPrefUpdater
pref(profile_
->GetTestingPrefService());
3778 pref
.SetIndividualExtensionInstallationAllowed(good_crx
, true);
3781 // Ensure we can now install good_crx.
3782 InstallCRX(path
, INSTALL_NEW
);
3783 EXPECT_EQ(1u, registry()->enabled_extensions().size());
3786 // Extension blacklisted by policy get unloaded after installing.
3787 TEST_F(ExtensionServiceTest
, BlacklistedByPolicyRemovedIfRunning
) {
3788 InitializeEmptyExtensionServiceWithTestingPrefs();
3790 // Install good_crx.
3791 base::FilePath path
= data_dir().AppendASCII("good.crx");
3792 InstallCRX(path
, INSTALL_NEW
);
3793 EXPECT_EQ(1u, registry()->enabled_extensions().size());
3796 ManagementPrefUpdater
pref(profile_
->GetTestingPrefService());
3797 // Blacklist this extension.
3798 pref
.SetIndividualExtensionInstallationAllowed(good_crx
, false);
3801 // Extension should not be running now.
3802 base::RunLoop().RunUntilIdle();
3803 EXPECT_EQ(0u, registry()->enabled_extensions().size());
3806 // Tests that component extensions are not blacklisted by policy.
3807 TEST_F(ExtensionServiceTest
, ComponentExtensionWhitelisted
) {
3808 InitializeEmptyExtensionServiceWithTestingPrefs();
3810 // Blacklist everything.
3812 ManagementPrefUpdater
pref(profile_
->GetTestingPrefService());
3813 pref
.SetBlacklistedByDefault(true);
3816 // Install a component extension.
3817 base::FilePath path
= data_dir()
3818 .AppendASCII("good")
3819 .AppendASCII("Extensions")
3821 .AppendASCII("1.0.0.0");
3822 std::string manifest
;
3823 ASSERT_TRUE(base::ReadFileToString(
3824 path
.Append(extensions::kManifestFilename
), &manifest
));
3825 service()->component_loader()->Add(manifest
, path
);
3828 // Extension should be installed despite blacklist.
3829 ASSERT_EQ(1u, registry()->enabled_extensions().size());
3830 EXPECT_TRUE(service()->GetExtensionById(good0
, false));
3832 // Poke external providers and make sure the extension is still present.
3833 service()->CheckForExternalUpdates();
3834 ASSERT_EQ(1u, registry()->enabled_extensions().size());
3835 EXPECT_TRUE(service()->GetExtensionById(good0
, false));
3837 // Extension should not be uninstalled on blacklist changes.
3839 ManagementPrefUpdater
pref(profile_
->GetTestingPrefService());
3840 pref
.SetIndividualExtensionInstallationAllowed(good0
, false);
3842 base::RunLoop().RunUntilIdle();
3843 ASSERT_EQ(1u, registry()->enabled_extensions().size());
3844 EXPECT_TRUE(service()->GetExtensionById(good0
, false));
3847 // Tests that policy-installed extensions are not blacklisted by policy.
3848 TEST_F(ExtensionServiceTest
, PolicyInstalledExtensionsWhitelisted
) {
3849 InitializeEmptyExtensionServiceWithTestingPrefs();
3852 ManagementPrefUpdater
pref(profile_
->GetTestingPrefService());
3853 // Blacklist everything.
3854 pref
.SetBlacklistedByDefault(true);
3855 // Mark good.crx for force-installation.
3856 pref
.SetIndividualExtensionAutoInstalled(
3857 good_crx
, "http://example.com/update_url", true);
3860 // Have policy force-install an extension.
3861 MockExtensionProvider
* provider
=
3862 new MockExtensionProvider(service(), Manifest::EXTERNAL_POLICY_DOWNLOAD
);
3863 AddMockExternalProvider(provider
);
3864 provider
->UpdateOrAddExtension(
3865 good_crx
, "1.0.0.0", data_dir().AppendASCII("good.crx"));
3867 // Reloading extensions should find our externally registered extension
3869 content::WindowedNotificationObserver
observer(
3870 extensions::NOTIFICATION_CRX_INSTALLER_DONE
,
3871 content::NotificationService::AllSources());
3872 service()->CheckForExternalUpdates();
3875 // Extension should be installed despite blacklist.
3876 ASSERT_EQ(1u, registry()->enabled_extensions().size());
3877 EXPECT_TRUE(service()->GetExtensionById(good_crx
, false));
3879 // Blacklist update should not uninstall the extension.
3881 ManagementPrefUpdater
pref(profile_
->GetTestingPrefService());
3882 pref
.SetIndividualExtensionInstallationAllowed(good0
, false);
3884 base::RunLoop().RunUntilIdle();
3885 ASSERT_EQ(1u, registry()->enabled_extensions().size());
3886 EXPECT_TRUE(service()->GetExtensionById(good_crx
, false));
3889 // Tests that extensions cannot be installed if the policy provider prohibits
3890 // it. This functionality is implemented in CrxInstaller::ConfirmInstall().
3891 TEST_F(ExtensionServiceTest
, ManagementPolicyProhibitsInstall
) {
3892 InitializeEmptyExtensionService();
3894 GetManagementPolicy()->UnregisterAllProviders();
3895 extensions::TestManagementPolicyProvider
provider_(
3896 extensions::TestManagementPolicyProvider::PROHIBIT_LOAD
);
3897 GetManagementPolicy()->RegisterProvider(&provider_
);
3899 InstallCRX(data_dir().AppendASCII("good.crx"), INSTALL_FAILED
);
3900 EXPECT_EQ(0u, registry()->enabled_extensions().size());
3903 // Tests that extensions cannot be loaded from prefs if the policy provider
3904 // prohibits it. This functionality is implemented in InstalledLoader::Load().
3905 TEST_F(ExtensionServiceTest
, ManagementPolicyProhibitsLoadFromPrefs
) {
3906 InitializeEmptyExtensionService();
3908 // Create a fake extension to be loaded as though it were read from prefs.
3909 base::FilePath path
=
3910 data_dir().AppendASCII("management").AppendASCII("simple_extension");
3911 base::DictionaryValue manifest
;
3912 manifest
.SetString(keys::kName
, "simple_extension");
3913 manifest
.SetString(keys::kVersion
, "1");
3914 // UNPACKED is for extensions loaded from a directory. We use it here, even
3915 // though we're testing loading from prefs, so that we don't need to provide
3916 // an extension key.
3917 extensions::ExtensionInfo
extension_info(
3918 &manifest
, std::string(), path
, Manifest::UNPACKED
);
3920 // Ensure we can load it with no management policy in place.
3921 GetManagementPolicy()->UnregisterAllProviders();
3922 EXPECT_EQ(0u, registry()->enabled_extensions().size());
3923 extensions::InstalledLoader(service()).Load(extension_info
, false);
3924 EXPECT_EQ(1u, registry()->enabled_extensions().size());
3926 const Extension
* extension
=
3927 (registry()->enabled_extensions().begin())->get();
3929 service()->UninstallExtension(extension
->id(),
3930 extensions::UNINSTALL_REASON_FOR_TESTING
,
3931 base::Bind(&base::DoNothing
),
3933 EXPECT_EQ(0u, registry()->enabled_extensions().size());
3935 // Ensure we cannot load it if management policy prohibits installation.
3936 extensions::TestManagementPolicyProvider
provider_(
3937 extensions::TestManagementPolicyProvider::PROHIBIT_LOAD
);
3938 GetManagementPolicy()->RegisterProvider(&provider_
);
3940 extensions::InstalledLoader(service()).Load(extension_info
, false);
3941 EXPECT_EQ(0u, registry()->enabled_extensions().size());
3944 // Tests disabling an extension when prohibited by the ManagementPolicy.
3945 TEST_F(ExtensionServiceTest
, ManagementPolicyProhibitsDisable
) {
3946 InitializeEmptyExtensionService();
3948 InstallCRX(data_dir().AppendASCII("good.crx"), INSTALL_NEW
);
3949 EXPECT_EQ(1u, registry()->enabled_extensions().size());
3950 EXPECT_EQ(0u, registry()->disabled_extensions().size());
3952 GetManagementPolicy()->UnregisterAllProviders();
3953 extensions::TestManagementPolicyProvider
provider(
3954 extensions::TestManagementPolicyProvider::PROHIBIT_MODIFY_STATUS
);
3955 GetManagementPolicy()->RegisterProvider(&provider
);
3957 // Attempt to disable it.
3958 service()->DisableExtension(good_crx
, Extension::DISABLE_USER_ACTION
);
3960 EXPECT_EQ(1u, registry()->enabled_extensions().size());
3961 EXPECT_TRUE(service()->GetExtensionById(good_crx
, false));
3962 EXPECT_EQ(0u, registry()->disabled_extensions().size());
3965 // Tests uninstalling an extension when prohibited by the ManagementPolicy.
3966 TEST_F(ExtensionServiceTest
, ManagementPolicyProhibitsUninstall
) {
3967 InitializeEmptyExtensionService();
3969 InstallCRX(data_dir().AppendASCII("good.crx"), INSTALL_NEW
);
3970 EXPECT_EQ(1u, registry()->enabled_extensions().size());
3971 EXPECT_EQ(0u, registry()->disabled_extensions().size());
3973 GetManagementPolicy()->UnregisterAllProviders();
3974 extensions::TestManagementPolicyProvider
provider(
3975 extensions::TestManagementPolicyProvider::PROHIBIT_MODIFY_STATUS
);
3976 GetManagementPolicy()->RegisterProvider(&provider
);
3978 // Attempt to uninstall it.
3980 service()->UninstallExtension(good_crx
,
3981 extensions::UNINSTALL_REASON_FOR_TESTING
,
3982 base::Bind(&base::DoNothing
),
3985 EXPECT_EQ(1u, registry()->enabled_extensions().size());
3986 EXPECT_TRUE(service()->GetExtensionById(good_crx
, false));
3989 // Tests that previously installed extensions that are now prohibited from
3990 // being installed are removed.
3991 TEST_F(ExtensionServiceTest
, ManagementPolicyUnloadsAllProhibited
) {
3992 InitializeEmptyExtensionService();
3994 InstallCRX(data_dir().AppendASCII("good.crx"), INSTALL_NEW
);
3995 InstallCRX(data_dir().AppendASCII("page_action.crx"), INSTALL_NEW
);
3996 EXPECT_EQ(2u, registry()->enabled_extensions().size());
3997 EXPECT_EQ(0u, registry()->disabled_extensions().size());
3999 GetManagementPolicy()->UnregisterAllProviders();
4000 extensions::TestManagementPolicyProvider
provider(
4001 extensions::TestManagementPolicyProvider::PROHIBIT_LOAD
);
4002 GetManagementPolicy()->RegisterProvider(&provider
);
4004 // Run the policy check.
4005 service()->CheckManagementPolicy();
4006 EXPECT_EQ(0u, registry()->enabled_extensions().size());
4007 EXPECT_EQ(0u, registry()->disabled_extensions().size());
4010 // Tests that previously disabled extensions that are now required to be
4011 // enabled are re-enabled on reinstall.
4012 TEST_F(ExtensionServiceTest
, ManagementPolicyRequiresEnable
) {
4013 InitializeEmptyExtensionService();
4015 // Install, then disable, an extension.
4016 InstallCRX(data_dir().AppendASCII("good.crx"), INSTALL_NEW
);
4017 EXPECT_EQ(1u, registry()->enabled_extensions().size());
4018 service()->DisableExtension(good_crx
, Extension::DISABLE_USER_ACTION
);
4019 EXPECT_EQ(1u, registry()->disabled_extensions().size());
4021 // Register an ExtensionManagementPolicy that requires the extension to remain
4023 GetManagementPolicy()->UnregisterAllProviders();
4024 extensions::TestManagementPolicyProvider
provider(
4025 extensions::TestManagementPolicyProvider::MUST_REMAIN_ENABLED
);
4026 GetManagementPolicy()->RegisterProvider(&provider
);
4028 // Reinstall the extension.
4029 InstallCRX(data_dir().AppendASCII("good.crx"), INSTALL_UPDATED
);
4030 EXPECT_EQ(1u, registry()->enabled_extensions().size());
4031 EXPECT_EQ(0u, registry()->disabled_extensions().size());
4034 // Tests that extensions disabled by management policy can be installed but
4035 // will get disabled after installing.
4036 TEST_F(ExtensionServiceTest
, ManagementPolicyProhibitsEnableOnInstalled
) {
4037 InitializeEmptyExtensionService();
4039 // Register an ExtensionManagementPolicy that disables all extensions, with
4040 // a specified Extension::DisableReason.
4041 GetManagementPolicy()->UnregisterAllProviders();
4042 extensions::TestManagementPolicyProvider
provider(
4043 extensions::TestManagementPolicyProvider::MUST_REMAIN_DISABLED
);
4044 provider
.SetDisableReason(Extension::DISABLE_NOT_VERIFIED
);
4045 GetManagementPolicy()->RegisterProvider(&provider
);
4047 // Attempts to install an extensions, it should be installed but disabled.
4048 EXPECT_EQ(0u, registry()->enabled_extensions().size());
4049 EXPECT_EQ(0u, registry()->disabled_extensions().size());
4050 InstallCRX(data_dir().AppendASCII("good.crx"), INSTALL_WITHOUT_LOAD
);
4051 EXPECT_EQ(0u, registry()->enabled_extensions().size());
4052 EXPECT_EQ(1u, registry()->disabled_extensions().size());
4054 // Verifies that the disable reason is set properly.
4055 EXPECT_EQ(Extension::DISABLE_NOT_VERIFIED
,
4056 service()->extension_prefs_
->GetDisableReasons(kGoodId
));
4059 // Tests that extensions with conflicting required permissions by enterprise
4060 // policy cannot be installed.
4061 TEST_F(ExtensionServiceTest
, PolicyBlockedPermissionNewExtensionInstall
) {
4062 InitializeEmptyExtensionServiceWithTestingPrefs();
4063 base::FilePath path
= data_dir().AppendASCII("permissions_blocklist");
4066 // Update policy to block one of the required permissions of target.
4067 ManagementPrefUpdater
pref(profile_
->GetTestingPrefService());
4068 pref
.AddBlockedPermission("*", "tabs");
4071 // The extension should be failed to install.
4072 PackAndInstallCRX(path
, INSTALL_FAILED
);
4075 // Update policy to block one of the optional permissions instead.
4076 ManagementPrefUpdater
pref(profile_
->GetTestingPrefService());
4077 pref
.ClearBlockedPermissions("*");
4078 pref
.AddBlockedPermission("*", "history");
4081 // The extension should succeed to install this time.
4082 std::string id
= PackAndInstallCRX(path
, INSTALL_NEW
)->id();
4084 // Uninstall the extension and update policy to block some arbitrary
4085 // unknown permission.
4086 UninstallExtension(id
, false);
4088 ManagementPrefUpdater
pref(profile_
->GetTestingPrefService());
4089 pref
.ClearBlockedPermissions("*");
4090 pref
.AddBlockedPermission("*", "unknown.permission.for.testing");
4093 // The extension should succeed to install as well.
4094 PackAndInstallCRX(path
, INSTALL_NEW
);
4097 // Tests that extension supposed to be force installed but with conflicting
4098 // required permissions cannot be installed.
4099 TEST_F(ExtensionServiceTest
, PolicyBlockedPermissionConflictsWithForceInstall
) {
4100 InitializeEmptyExtensionServiceWithTestingPrefs();
4102 // Pack the crx file.
4103 base::FilePath path
= data_dir().AppendASCII("permissions_blocklist");
4104 base::FilePath pem_path
= data_dir().AppendASCII("permissions_blocklist.pem");
4105 base::ScopedTempDir temp_dir
;
4106 EXPECT_TRUE(temp_dir
.CreateUniqueTempDir());
4107 base::FilePath crx_path
= temp_dir
.path().AppendASCII("temp.crx");
4109 PackCRX(path
, pem_path
, crx_path
);
4112 // Block one of the required permissions.
4113 ManagementPrefUpdater
pref(profile_
->GetTestingPrefService());
4114 pref
.AddBlockedPermission("*", "tabs");
4117 // Use MockExtensionProvider to simulate force installing extension.
4118 MockExtensionProvider
* provider
=
4119 new MockExtensionProvider(service(), Manifest::EXTERNAL_POLICY_DOWNLOAD
);
4120 AddMockExternalProvider(provider
);
4121 provider
->UpdateOrAddExtension(permissions_blocklist
, "1.0", crx_path
);
4124 // Attempts to force install this extension.
4125 content::WindowedNotificationObserver
observer(
4126 extensions::NOTIFICATION_CRX_INSTALLER_DONE
,
4127 content::NotificationService::AllSources());
4128 service()->CheckForExternalUpdates();
4132 // The extension should not be installed.
4133 ASSERT_FALSE(service()->GetInstalledExtension(permissions_blocklist
));
4135 // Remove this extension from pending extension manager as we would like to
4136 // give another attempt later.
4137 service()->pending_extension_manager()->Remove(permissions_blocklist
);
4140 // Clears the permission block list.
4141 ManagementPrefUpdater
pref(profile_
->GetTestingPrefService());
4142 pref
.ClearBlockedPermissions("*");
4146 // Attempts to force install this extension again.
4147 content::WindowedNotificationObserver
observer(
4148 extensions::NOTIFICATION_CRX_INSTALLER_DONE
,
4149 content::NotificationService::AllSources());
4150 service()->CheckForExternalUpdates();
4154 const Extension
* installed
=
4155 service()->GetInstalledExtension(permissions_blocklist
);
4156 ASSERT_TRUE(installed
);
4157 EXPECT_EQ(installed
->location(), Manifest::EXTERNAL_POLICY_DOWNLOAD
);
4160 // Tests that newer versions of an extension with conflicting required
4161 // permissions by enterprise policy cannot be updated to.
4162 TEST_F(ExtensionServiceTest
, PolicyBlockedPermissionExtensionUpdate
) {
4163 InitializeEmptyExtensionServiceWithTestingPrefs();
4165 base::FilePath path
= data_dir().AppendASCII("permissions_blocklist");
4166 base::FilePath path2
= data_dir().AppendASCII("permissions_blocklist2");
4167 base::FilePath pem_path
= data_dir().AppendASCII("permissions_blocklist.pem");
4169 // Install 'permissions_blocklist'.
4170 const Extension
* installed
= PackAndInstallCRX(path
, pem_path
, INSTALL_NEW
);
4171 EXPECT_EQ(installed
->id(), permissions_blocklist
);
4174 // Block one of the required permissions of 'permissions_blocklist2'.
4175 ManagementPrefUpdater
pref(profile_
->GetTestingPrefService());
4176 pref
.AddBlockedPermission("*", "downloads");
4179 // Install 'permissions_blocklist' again, should be updated.
4180 const Extension
* updated
= PackAndInstallCRX(path
, pem_path
, INSTALL_UPDATED
);
4181 EXPECT_EQ(updated
->id(), permissions_blocklist
);
4183 std::string old_version
= updated
->VersionString();
4185 // Attempts to update to 'permissions_blocklist2' should fail.
4186 PackAndInstallCRX(path2
, pem_path
, INSTALL_FAILED
);
4188 // Verify that the old version is still enabled.
4189 updated
= service()->GetExtensionById(permissions_blocklist
, false);
4190 ASSERT_TRUE(updated
);
4191 EXPECT_EQ(old_version
, updated
->VersionString());
4194 // Tests that policy update with additional permissions blocked revoke
4195 // conflicting granted optional permissions and unload extensions with
4196 // conflicting required permissions, including the force installed ones.
4197 TEST_F(ExtensionServiceTest
, PolicyBlockedPermissionPolicyUpdate
) {
4198 InitializeEmptyExtensionServiceWithTestingPrefs();
4200 base::FilePath path
= data_dir().AppendASCII("permissions_blocklist");
4201 base::FilePath path2
= data_dir().AppendASCII("permissions_blocklist2");
4202 base::FilePath pem_path
= data_dir().AppendASCII("permissions_blocklist.pem");
4204 // Pack the crx file.
4205 base::ScopedTempDir temp_dir
;
4206 EXPECT_TRUE(temp_dir
.CreateUniqueTempDir());
4207 base::FilePath crx_path
= temp_dir
.path().AppendASCII("temp.crx");
4209 PackCRX(path2
, pem_path
, crx_path
);
4211 // Install two arbitary extensions with specified manifest.
4212 std::string ext1
= PackAndInstallCRX(path
, INSTALL_NEW
)->id();
4213 std::string ext2
= PackAndInstallCRX(path2
, INSTALL_NEW
)->id();
4214 ASSERT_NE(ext1
, permissions_blocklist
);
4215 ASSERT_NE(ext2
, permissions_blocklist
);
4216 ASSERT_NE(ext1
, ext2
);
4218 // Force install another extension with known id and same manifest as 'ext2'.
4219 std::string ext2_forced
= permissions_blocklist
;
4220 MockExtensionProvider
* provider
=
4221 new MockExtensionProvider(service(), Manifest::EXTERNAL_POLICY_DOWNLOAD
);
4222 AddMockExternalProvider(provider
);
4223 provider
->UpdateOrAddExtension(ext2_forced
, "2.0", crx_path
);
4225 content::WindowedNotificationObserver
observer(
4226 extensions::NOTIFICATION_CRX_INSTALLER_DONE
,
4227 content::NotificationService::AllSources());
4228 service()->CheckForExternalUpdates();
4231 extensions::ExtensionRegistry
* registry
=
4232 extensions::ExtensionRegistry::Get(profile());
4234 // Verify all three extensions are installed and enabled.
4235 ASSERT_TRUE(registry
->enabled_extensions().GetByID(ext1
));
4236 ASSERT_TRUE(registry
->enabled_extensions().GetByID(ext2
));
4237 ASSERT_TRUE(registry
->enabled_extensions().GetByID(ext2_forced
));
4239 // Grant all optional permissions to each extension.
4240 GrantAllOptionalPermissions(ext1
);
4241 GrantAllOptionalPermissions(ext2
);
4242 GrantAllOptionalPermissions(ext2_forced
);
4244 scoped_refptr
<const PermissionSet
> active_permissions(
4245 ExtensionPrefs::Get(profile())->GetActivePermissions(ext1
));
4246 EXPECT_TRUE(active_permissions
->HasAPIPermission(
4247 extensions::APIPermission::kDownloads
));
4249 // Set policy to block 'downloads' permission.
4251 ManagementPrefUpdater
pref(profile_
->GetTestingPrefService());
4252 pref
.AddBlockedPermission("*", "downloads");
4255 base::RunLoop().RunUntilIdle();
4257 // 'ext1' should still be enabled, but with 'downloads' permission revoked.
4258 EXPECT_TRUE(registry
->enabled_extensions().GetByID(ext1
));
4259 active_permissions
=
4260 ExtensionPrefs::Get(profile())->GetActivePermissions(ext1
);
4261 EXPECT_FALSE(active_permissions
->HasAPIPermission(
4262 extensions::APIPermission::kDownloads
));
4264 // 'ext2' should be disabled because one of its required permissions is
4266 EXPECT_FALSE(registry
->enabled_extensions().GetByID(ext2
));
4268 // 'ext2_forced' should be handled the same as 'ext2'
4269 EXPECT_FALSE(registry
->enabled_extensions().GetByID(ext2_forced
));
4272 // Flaky on windows; http://crbug.com/309833
4274 #define MAYBE_ExternalExtensionAutoAcknowledgement DISABLED_ExternalExtensionAutoAcknowledgement
4276 #define MAYBE_ExternalExtensionAutoAcknowledgement ExternalExtensionAutoAcknowledgement
4278 TEST_F(ExtensionServiceTest
, MAYBE_ExternalExtensionAutoAcknowledgement
) {
4279 InitializeEmptyExtensionService();
4280 service()->set_extensions_enabled(true);
4283 // Register and install an external extension.
4284 MockExtensionProvider
* provider
=
4285 new MockExtensionProvider(service(), Manifest::EXTERNAL_PREF
);
4286 AddMockExternalProvider(provider
);
4287 provider
->UpdateOrAddExtension(
4288 good_crx
, "1.0.0.0", data_dir().AppendASCII("good.crx"));
4291 // Have policy force-install an extension.
4292 MockExtensionProvider
* provider
= new MockExtensionProvider(
4293 service(), Manifest::EXTERNAL_POLICY_DOWNLOAD
);
4294 AddMockExternalProvider(provider
);
4295 provider
->UpdateOrAddExtension(
4296 page_action
, "1.0.0.0", data_dir().AppendASCII("page_action.crx"));
4299 // Providers are set up. Let them run.
4301 content::WindowedNotificationObserver
observer(
4302 extensions::NOTIFICATION_CRX_INSTALLER_DONE
,
4303 base::Bind(&WaitForCountNotificationsCallback
, &count
));
4304 service()->CheckForExternalUpdates();
4308 ASSERT_EQ(2u, registry()->enabled_extensions().size());
4309 EXPECT_TRUE(service()->GetExtensionById(good_crx
, false));
4310 EXPECT_TRUE(service()->GetExtensionById(page_action
, false));
4311 ExtensionPrefs
* prefs
= ExtensionPrefs::Get(profile());
4312 ASSERT_TRUE(!prefs
->IsExternalExtensionAcknowledged(good_crx
));
4313 ASSERT_TRUE(prefs
->IsExternalExtensionAcknowledged(page_action
));
4316 #if !defined(OS_CHROMEOS)
4317 // This tests if default apps are installed correctly.
4318 TEST_F(ExtensionServiceTest
, DefaultAppsInstall
) {
4319 InitializeEmptyExtensionService();
4320 service()->set_extensions_enabled(true);
4323 std::string json_data
=
4325 " \"ldnnhddmnhbkjipkidpdiheffobcpfmf\" : {"
4326 " \"external_crx\": \"good.crx\","
4327 " \"external_version\": \"1.0.0.0\","
4328 " \"is_bookmark_app\": false"
4331 default_apps::Provider
* provider
= new default_apps::Provider(
4334 new extensions::ExternalTestingLoader(json_data
, data_dir()),
4336 Manifest::INVALID_LOCATION
,
4337 Extension::FROM_WEBSTORE
| Extension::WAS_INSTALLED_BY_DEFAULT
);
4339 AddMockExternalProvider(provider
);
4342 ASSERT_EQ(0u, registry()->enabled_extensions().size());
4343 content::WindowedNotificationObserver
observer(
4344 extensions::NOTIFICATION_CRX_INSTALLER_DONE
,
4345 content::NotificationService::AllSources());
4346 service()->CheckForExternalUpdates();
4349 ASSERT_EQ(1u, registry()->enabled_extensions().size());
4350 EXPECT_TRUE(service()->GetExtensionById(good_crx
, false));
4351 const Extension
* extension
= service()->GetExtensionById(good_crx
, false);
4352 EXPECT_TRUE(extension
->from_webstore());
4353 EXPECT_TRUE(extension
->was_installed_by_default());
4357 // Tests disabling extensions
4358 TEST_F(ExtensionServiceTest
, DisableExtension
) {
4359 InitializeEmptyExtensionService();
4361 InstallCRX(data_dir().AppendASCII("good.crx"), INSTALL_NEW
);
4362 EXPECT_TRUE(service()->GetExtensionById(good_crx
, true));
4363 EXPECT_TRUE(service()->GetExtensionById(good_crx
, false));
4365 EXPECT_EQ(1u, registry()->enabled_extensions().size());
4366 EXPECT_EQ(0u, registry()->disabled_extensions().size());
4367 EXPECT_EQ(0u, registry()->terminated_extensions().size());
4368 EXPECT_EQ(0u, registry()->blacklisted_extensions().size());
4371 service()->DisableExtension(good_crx
, Extension::DISABLE_USER_ACTION
);
4373 EXPECT_TRUE(service()->GetExtensionById(good_crx
, true));
4374 EXPECT_FALSE(service()->GetExtensionById(good_crx
, false));
4375 EXPECT_EQ(0u, registry()->enabled_extensions().size());
4376 EXPECT_EQ(1u, registry()->disabled_extensions().size());
4377 EXPECT_EQ(0u, registry()->terminated_extensions().size());
4378 EXPECT_EQ(0u, registry()->blacklisted_extensions().size());
4381 TEST_F(ExtensionServiceTest
, TerminateExtension
) {
4382 InitializeEmptyExtensionService();
4384 InstallCRX(data_dir().AppendASCII("good.crx"), INSTALL_NEW
);
4385 EXPECT_EQ(1u, registry()->enabled_extensions().size());
4386 EXPECT_EQ(0u, registry()->disabled_extensions().size());
4387 EXPECT_EQ(0u, registry()->terminated_extensions().size());
4388 EXPECT_EQ(0u, registry()->blacklisted_extensions().size());
4390 TerminateExtension(good_crx
);
4392 EXPECT_EQ(0u, registry()->enabled_extensions().size());
4393 EXPECT_EQ(0u, registry()->disabled_extensions().size());
4394 EXPECT_EQ(1u, registry()->terminated_extensions().size());
4395 EXPECT_EQ(0u, registry()->blacklisted_extensions().size());
4398 TEST_F(ExtensionServiceTest
, DisableTerminatedExtension
) {
4399 InitializeEmptyExtensionService();
4401 InstallCRX(data_dir().AppendASCII("good.crx"), INSTALL_NEW
);
4402 TerminateExtension(good_crx
);
4403 EXPECT_TRUE(registry()->GetExtensionById(
4404 good_crx
, extensions::ExtensionRegistry::TERMINATED
));
4407 service()->DisableExtension(good_crx
, Extension::DISABLE_USER_ACTION
);
4409 EXPECT_FALSE(registry()->GetExtensionById(
4410 good_crx
, extensions::ExtensionRegistry::TERMINATED
));
4411 EXPECT_TRUE(service()->GetExtensionById(good_crx
, true));
4413 EXPECT_EQ(0u, registry()->enabled_extensions().size());
4414 EXPECT_EQ(1u, registry()->disabled_extensions().size());
4415 EXPECT_EQ(0u, registry()->terminated_extensions().size());
4416 EXPECT_EQ(0u, registry()->blacklisted_extensions().size());
4419 // Tests disabling all extensions (simulating --disable-extensions flag).
4420 TEST_F(ExtensionServiceTest
, DisableAllExtensions
) {
4421 InitializeEmptyExtensionService();
4423 base::FilePath path
= data_dir().AppendASCII("good.crx");
4424 InstallCRX(path
, INSTALL_NEW
);
4426 EXPECT_EQ(1u, registry()->enabled_extensions().size());
4427 EXPECT_EQ(0u, registry()->disabled_extensions().size());
4429 // Disable extensions.
4430 service()->set_extensions_enabled(false);
4431 service()->ReloadExtensionsForTest();
4433 // There shouldn't be extensions in either list.
4434 EXPECT_EQ(0u, registry()->enabled_extensions().size());
4435 EXPECT_EQ(0u, registry()->disabled_extensions().size());
4437 // This shouldn't do anything when all extensions are disabled.
4438 service()->EnableExtension(good_crx
);
4439 service()->ReloadExtensionsForTest();
4441 // There still shouldn't be extensions in either list.
4442 EXPECT_EQ(0u, registry()->enabled_extensions().size());
4443 EXPECT_EQ(0u, registry()->disabled_extensions().size());
4445 // And then re-enable the extensions.
4446 service()->set_extensions_enabled(true);
4447 service()->ReloadExtensionsForTest();
4449 EXPECT_EQ(1u, registry()->enabled_extensions().size());
4450 EXPECT_EQ(0u, registry()->disabled_extensions().size());
4453 // Tests reloading extensions.
4454 TEST_F(ExtensionServiceTest
, ReloadExtensions
) {
4455 InitializeEmptyExtensionService();
4457 // Simple extension that should install without error.
4458 base::FilePath path
= data_dir().AppendASCII("good.crx");
4459 InstallCRX(path
, INSTALL_NEW
,
4460 Extension::FROM_WEBSTORE
| Extension::WAS_INSTALLED_BY_DEFAULT
);
4461 const char* const extension_id
= good_crx
;
4462 service()->DisableExtension(extension_id
, Extension::DISABLE_USER_ACTION
);
4464 EXPECT_EQ(0u, registry()->enabled_extensions().size());
4465 EXPECT_EQ(1u, registry()->disabled_extensions().size());
4467 service()->ReloadExtensionsForTest();
4469 // The creation flags should not change when reloading the extension.
4470 const Extension
* extension
= service()->GetExtensionById(good_crx
, true);
4471 EXPECT_TRUE(extension
->from_webstore());
4472 EXPECT_TRUE(extension
->was_installed_by_default());
4473 EXPECT_FALSE(extension
->from_bookmark());
4475 // Extension counts shouldn't change.
4476 EXPECT_EQ(0u, registry()->enabled_extensions().size());
4477 EXPECT_EQ(1u, registry()->disabled_extensions().size());
4479 service()->EnableExtension(extension_id
);
4481 EXPECT_EQ(1u, registry()->enabled_extensions().size());
4482 EXPECT_EQ(0u, registry()->disabled_extensions().size());
4484 // Need to clear |loaded_| manually before reloading as the
4485 // EnableExtension() call above inserted into it and
4486 // UnloadAllExtensions() doesn't send out notifications.
4488 service()->ReloadExtensionsForTest();
4490 // Extension counts shouldn't change.
4491 EXPECT_EQ(1u, registry()->enabled_extensions().size());
4492 EXPECT_EQ(0u, registry()->disabled_extensions().size());
4495 // Tests reloading an extension.
4496 TEST_F(ExtensionServiceTest
, ReloadExtension
) {
4497 InitializeEmptyExtensionService();
4499 // Simple extension that should install without error.
4500 const char extension_id
[] = "behllobkkfkfnphdnhnkndlbkcpglgmj";
4501 base::FilePath ext
= data_dir()
4502 .AppendASCII("good")
4503 .AppendASCII("Extensions")
4504 .AppendASCII(extension_id
)
4505 .AppendASCII("1.0.0.0");
4506 extensions::UnpackedInstaller::Create(service())->Load(ext
);
4507 base::RunLoop().RunUntilIdle();
4509 EXPECT_EQ(1u, registry()->enabled_extensions().size());
4510 EXPECT_EQ(0u, registry()->disabled_extensions().size());
4512 service()->ReloadExtension(extension_id
);
4514 // Extension should be disabled now, waiting to be reloaded.
4515 EXPECT_EQ(0u, registry()->enabled_extensions().size());
4516 EXPECT_EQ(1u, registry()->disabled_extensions().size());
4517 EXPECT_EQ(Extension::DISABLE_RELOAD
,
4518 ExtensionPrefs::Get(profile())->GetDisableReasons(extension_id
));
4520 // Reloading again should not crash.
4521 service()->ReloadExtension(extension_id
);
4524 base::RunLoop().RunUntilIdle();
4526 // Extension should be enabled again.
4527 EXPECT_EQ(1u, registry()->enabled_extensions().size());
4528 EXPECT_EQ(0u, registry()->disabled_extensions().size());
4531 TEST_F(ExtensionServiceTest
, UninstallExtension
) {
4532 InitializeEmptyExtensionService();
4533 InstallCRX(data_dir().AppendASCII("good.crx"), INSTALL_NEW
);
4534 EXPECT_EQ(1u, registry()->enabled_extensions().size());
4535 UninstallExtension(good_crx
, false);
4536 EXPECT_EQ(0u, registry()->enabled_extensions().size());
4537 EXPECT_EQ(UnloadedExtensionInfo::REASON_UNINSTALL
, unloaded_reason_
);
4540 TEST_F(ExtensionServiceTest
, UninstallTerminatedExtension
) {
4541 InitializeEmptyExtensionService();
4542 InstallCRX(data_dir().AppendASCII("good.crx"), INSTALL_NEW
);
4543 TerminateExtension(good_crx
);
4544 UninstallExtension(good_crx
, false);
4545 EXPECT_EQ(UnloadedExtensionInfo::REASON_TERMINATE
, unloaded_reason_
);
4548 // Tests the uninstaller helper.
4549 TEST_F(ExtensionServiceTest
, UninstallExtensionHelper
) {
4550 InitializeEmptyExtensionService();
4551 InstallCRX(data_dir().AppendASCII("good.crx"), INSTALL_NEW
);
4552 UninstallExtension(good_crx
, true);
4553 EXPECT_EQ(UnloadedExtensionInfo::REASON_UNINSTALL
, unloaded_reason_
);
4556 TEST_F(ExtensionServiceTest
, UninstallExtensionHelperTerminated
) {
4557 InitializeEmptyExtensionService();
4558 InstallCRX(data_dir().AppendASCII("good.crx"), INSTALL_NEW
);
4559 TerminateExtension(good_crx
);
4560 UninstallExtension(good_crx
, true);
4561 EXPECT_EQ(UnloadedExtensionInfo::REASON_TERMINATE
, unloaded_reason_
);
4564 // An extension disabled because of unsupported requirements should re-enabled
4565 // if updated to a version with supported requirements as long as there are no
4566 // other disable reasons.
4567 TEST_F(ExtensionServiceTest
, UpgradingRequirementsEnabled
) {
4568 InitializeEmptyExtensionService();
4571 base::FilePath path
= data_dir().AppendASCII("requirements");
4572 base::FilePath pem_path
=
4573 data_dir().AppendASCII("requirements").AppendASCII("v1_good.pem");
4574 const Extension
* extension_v1
= PackAndInstallCRX(path
.AppendASCII("v1_good"),
4577 std::string id
= extension_v1
->id();
4578 EXPECT_TRUE(service()->IsExtensionEnabled(id
));
4580 base::FilePath v2_bad_requirements_crx
= GetTemporaryFile();
4582 PackCRX(path
.AppendASCII("v2_bad_requirements"),
4584 v2_bad_requirements_crx
);
4585 UpdateExtension(id
, v2_bad_requirements_crx
, INSTALLED
);
4586 EXPECT_FALSE(service()->IsExtensionEnabled(id
));
4588 base::FilePath v3_good_crx
= GetTemporaryFile();
4590 PackCRX(path
.AppendASCII("v3_good"), pem_path
, v3_good_crx
);
4591 UpdateExtension(id
, v3_good_crx
, ENABLED
);
4592 EXPECT_TRUE(service()->IsExtensionEnabled(id
));
4595 // Extensions disabled through user action should stay disabled.
4596 TEST_F(ExtensionServiceTest
, UpgradingRequirementsDisabled
) {
4597 InitializeEmptyExtensionService();
4600 base::FilePath path
= data_dir().AppendASCII("requirements");
4601 base::FilePath pem_path
=
4602 data_dir().AppendASCII("requirements").AppendASCII("v1_good.pem");
4603 const Extension
* extension_v1
= PackAndInstallCRX(path
.AppendASCII("v1_good"),
4606 std::string id
= extension_v1
->id();
4607 service()->DisableExtension(id
, Extension::DISABLE_USER_ACTION
);
4608 EXPECT_FALSE(service()->IsExtensionEnabled(id
));
4610 base::FilePath v2_bad_requirements_crx
= GetTemporaryFile();
4612 PackCRX(path
.AppendASCII("v2_bad_requirements"),
4614 v2_bad_requirements_crx
);
4615 UpdateExtension(id
, v2_bad_requirements_crx
, INSTALLED
);
4616 EXPECT_FALSE(service()->IsExtensionEnabled(id
));
4618 base::FilePath v3_good_crx
= GetTemporaryFile();
4620 PackCRX(path
.AppendASCII("v3_good"), pem_path
, v3_good_crx
);
4621 UpdateExtension(id
, v3_good_crx
, INSTALLED
);
4622 EXPECT_FALSE(service()->IsExtensionEnabled(id
));
4625 // The extension should not re-enabled because it was disabled from a
4626 // permission increase.
4627 TEST_F(ExtensionServiceTest
, UpgradingRequirementsPermissions
) {
4628 InitializeEmptyExtensionService();
4631 base::FilePath path
= data_dir().AppendASCII("requirements");
4632 base::FilePath pem_path
=
4633 data_dir().AppendASCII("requirements").AppendASCII("v1_good.pem");
4634 const Extension
* extension_v1
= PackAndInstallCRX(path
.AppendASCII("v1_good"),
4637 std::string id
= extension_v1
->id();
4638 EXPECT_TRUE(service()->IsExtensionEnabled(id
));
4640 base::FilePath v2_bad_requirements_and_permissions_crx
= GetTemporaryFile();
4642 PackCRX(path
.AppendASCII("v2_bad_requirements_and_permissions"),
4644 v2_bad_requirements_and_permissions_crx
);
4645 UpdateExtension(id
, v2_bad_requirements_and_permissions_crx
, INSTALLED
);
4646 EXPECT_FALSE(service()->IsExtensionEnabled(id
));
4648 base::FilePath v3_bad_permissions_crx
= GetTemporaryFile();
4650 PackCRX(path
.AppendASCII("v3_bad_permissions"),
4652 v3_bad_permissions_crx
);
4653 UpdateExtension(id
, v3_bad_permissions_crx
, INSTALLED
);
4654 EXPECT_FALSE(service()->IsExtensionEnabled(id
));
4657 // Unpacked extensions are not allowed to be installed if they have unsupported
4659 TEST_F(ExtensionServiceTest
, UnpackedRequirements
) {
4660 InitializeEmptyExtensionService();
4663 base::FilePath path
=
4664 data_dir().AppendASCII("requirements").AppendASCII("v2_bad_requirements");
4665 extensions::UnpackedInstaller::Create(service())->Load(path
);
4666 base::RunLoop().RunUntilIdle();
4667 EXPECT_EQ(1u, GetErrors().size());
4668 EXPECT_EQ(0u, registry()->enabled_extensions().size());
4671 class ExtensionCookieCallback
{
4673 ExtensionCookieCallback()
4675 weak_factory_(base::MessageLoop::current()) {}
4677 void SetCookieCallback(bool result
) {
4678 base::MessageLoop::current()->PostTask(FROM_HERE
,
4679 base::Bind(&base::MessageLoop::Quit
, weak_factory_
.GetWeakPtr()));
4683 void GetAllCookiesCallback(const net::CookieList
& list
) {
4684 base::MessageLoop::current()->PostTask(FROM_HERE
,
4685 base::Bind(&base::MessageLoop::Quit
, weak_factory_
.GetWeakPtr()));
4688 net::CookieList list_
;
4690 base::WeakPtrFactory
<base::MessageLoop
> weak_factory_
;
4693 // Verifies extension state is removed upon uninstall.
4694 TEST_F(ExtensionServiceTest
, ClearExtensionData
) {
4695 InitializeEmptyExtensionService();
4696 ExtensionCookieCallback callback
;
4698 // Load a test extension.
4699 base::FilePath path
= data_dir();
4700 path
= path
.AppendASCII("good.crx");
4701 const Extension
* extension
= InstallCRX(path
, INSTALL_NEW
);
4702 ASSERT_TRUE(extension
);
4703 GURL
ext_url(extension
->url());
4704 std::string origin_id
= storage::GetIdentifierFromOrigin(ext_url
);
4706 // Set a cookie for the extension.
4707 net::CookieMonster
* cookie_monster
= profile()
4708 ->GetRequestContextForExtensions()
4709 ->GetURLRequestContext()
4711 ->GetCookieMonster();
4712 ASSERT_TRUE(cookie_monster
);
4713 net::CookieOptions options
;
4714 cookie_monster
->SetCookieWithOptionsAsync(
4715 ext_url
, "dummy=value", options
,
4716 base::Bind(&ExtensionCookieCallback::SetCookieCallback
,
4717 base::Unretained(&callback
)));
4718 base::RunLoop().RunUntilIdle();
4719 EXPECT_TRUE(callback
.result_
);
4721 cookie_monster
->GetAllCookiesForURLAsync(
4723 base::Bind(&ExtensionCookieCallback::GetAllCookiesCallback
,
4724 base::Unretained(&callback
)));
4725 base::RunLoop().RunUntilIdle();
4726 EXPECT_EQ(1U, callback
.list_
.size());
4729 storage::DatabaseTracker
* db_tracker
=
4730 BrowserContext::GetDefaultStoragePartition(profile())
4731 ->GetDatabaseTracker();
4732 base::string16 db_name
= base::UTF8ToUTF16("db");
4733 base::string16 description
= base::UTF8ToUTF16("db_description");
4735 db_tracker
->DatabaseOpened(origin_id
, db_name
, description
, 1, &size
);
4736 db_tracker
->DatabaseClosed(origin_id
, db_name
);
4737 std::vector
<storage::OriginInfo
> origins
;
4738 db_tracker
->GetAllOriginsInfo(&origins
);
4739 EXPECT_EQ(1U, origins
.size());
4740 EXPECT_EQ(origin_id
, origins
[0].GetOriginIdentifier());
4742 // Create local storage. We only simulate this by creating the backing files.
4743 // Note: This test depends on details of how the dom_storage library
4744 // stores data in the host file system.
4745 base::FilePath lso_dir_path
=
4746 profile()->GetPath().AppendASCII("Local Storage");
4747 base::FilePath lso_file_path
= lso_dir_path
.AppendASCII(origin_id
)
4748 .AddExtension(FILE_PATH_LITERAL(".localstorage"));
4749 EXPECT_TRUE(base::CreateDirectory(lso_dir_path
));
4750 EXPECT_EQ(0, base::WriteFile(lso_file_path
, NULL
, 0));
4751 EXPECT_TRUE(base::PathExists(lso_file_path
));
4753 // Create indexed db. Similarly, it is enough to only simulate this by
4754 // creating the directory on the disk.
4755 IndexedDBContext
* idb_context
= BrowserContext::GetDefaultStoragePartition(
4756 profile())->GetIndexedDBContext();
4757 idb_context
->SetTaskRunnerForTesting(
4758 base::MessageLoop::current()->message_loop_proxy().get());
4759 base::FilePath idb_path
= idb_context
->GetFilePathForTesting(origin_id
);
4760 EXPECT_TRUE(base::CreateDirectory(idb_path
));
4761 EXPECT_TRUE(base::DirectoryExists(idb_path
));
4763 // Uninstall the extension.
4764 base::RunLoop run_loop
;
4766 service()->UninstallExtension(good_crx
,
4767 extensions::UNINSTALL_REASON_FOR_TESTING
,
4768 run_loop
.QuitClosure(),
4770 // The data deletion happens on the IO thread.
4773 // Check that the cookie is gone.
4774 cookie_monster
->GetAllCookiesForURLAsync(
4776 base::Bind(&ExtensionCookieCallback::GetAllCookiesCallback
,
4777 base::Unretained(&callback
)));
4778 base::RunLoop().RunUntilIdle();
4779 EXPECT_EQ(0U, callback
.list_
.size());
4781 // The database should have vanished as well.
4783 db_tracker
->GetAllOriginsInfo(&origins
);
4784 EXPECT_EQ(0U, origins
.size());
4786 // Check that the LSO file has been removed.
4787 EXPECT_FALSE(base::PathExists(lso_file_path
));
4789 // Check if the indexed db has disappeared too.
4790 EXPECT_FALSE(base::DirectoryExists(idb_path
));
4793 // Verifies app state is removed upon uninstall.
4794 TEST_F(ExtensionServiceTest
, ClearAppData
) {
4795 InitializeEmptyExtensionService();
4796 ExtensionCookieCallback callback
;
4800 // Install app1 with unlimited storage.
4801 const Extension
* extension
=
4802 PackAndInstallCRX(data_dir().AppendASCII("app1"), INSTALL_NEW
);
4803 ValidatePrefKeyCount(++pref_count
);
4804 ASSERT_EQ(1u, registry()->enabled_extensions().size());
4805 const std::string id1
= extension
->id();
4806 EXPECT_TRUE(extension
->permissions_data()->HasAPIPermission(
4807 APIPermission::kUnlimitedStorage
));
4809 extensions::AppLaunchInfo::GetFullLaunchURL(extension
).GetOrigin());
4810 EXPECT_TRUE(profile()->GetExtensionSpecialStoragePolicy()->IsStorageUnlimited(
4812 std::string origin_id
= storage::GetIdentifierFromOrigin(origin1
);
4814 // Install app2 from the same origin with unlimited storage.
4815 extension
= PackAndInstallCRX(data_dir().AppendASCII("app2"), INSTALL_NEW
);
4816 ValidatePrefKeyCount(++pref_count
);
4817 ASSERT_EQ(2u, registry()->enabled_extensions().size());
4818 const std::string id2
= extension
->id();
4819 EXPECT_TRUE(extension
->permissions_data()->HasAPIPermission(
4820 APIPermission::kUnlimitedStorage
));
4821 EXPECT_TRUE(extension
->web_extent().MatchesURL(
4822 extensions::AppLaunchInfo::GetFullLaunchURL(extension
)));
4824 extensions::AppLaunchInfo::GetFullLaunchURL(extension
).GetOrigin());
4825 EXPECT_EQ(origin1
, origin2
);
4826 EXPECT_TRUE(profile()->GetExtensionSpecialStoragePolicy()->IsStorageUnlimited(
4829 // Set a cookie for the extension.
4830 net::CookieMonster
* cookie_monster
= profile()
4831 ->GetRequestContext()
4832 ->GetURLRequestContext()
4834 ->GetCookieMonster();
4835 ASSERT_TRUE(cookie_monster
);
4836 net::CookieOptions options
;
4837 cookie_monster
->SetCookieWithOptionsAsync(
4838 origin1
, "dummy=value", options
,
4839 base::Bind(&ExtensionCookieCallback::SetCookieCallback
,
4840 base::Unretained(&callback
)));
4841 base::RunLoop().RunUntilIdle();
4842 EXPECT_TRUE(callback
.result_
);
4844 cookie_monster
->GetAllCookiesForURLAsync(
4846 base::Bind(&ExtensionCookieCallback::GetAllCookiesCallback
,
4847 base::Unretained(&callback
)));
4848 base::RunLoop().RunUntilIdle();
4849 EXPECT_EQ(1U, callback
.list_
.size());
4852 storage::DatabaseTracker
* db_tracker
=
4853 BrowserContext::GetDefaultStoragePartition(profile())
4854 ->GetDatabaseTracker();
4855 base::string16 db_name
= base::UTF8ToUTF16("db");
4856 base::string16 description
= base::UTF8ToUTF16("db_description");
4858 db_tracker
->DatabaseOpened(origin_id
, db_name
, description
, 1, &size
);
4859 db_tracker
->DatabaseClosed(origin_id
, db_name
);
4860 std::vector
<storage::OriginInfo
> origins
;
4861 db_tracker
->GetAllOriginsInfo(&origins
);
4862 EXPECT_EQ(1U, origins
.size());
4863 EXPECT_EQ(origin_id
, origins
[0].GetOriginIdentifier());
4865 // Create local storage. We only simulate this by creating the backing files.
4866 // Note: This test depends on details of how the dom_storage library
4867 // stores data in the host file system.
4868 base::FilePath lso_dir_path
=
4869 profile()->GetPath().AppendASCII("Local Storage");
4870 base::FilePath lso_file_path
= lso_dir_path
.AppendASCII(origin_id
)
4871 .AddExtension(FILE_PATH_LITERAL(".localstorage"));
4872 EXPECT_TRUE(base::CreateDirectory(lso_dir_path
));
4873 EXPECT_EQ(0, base::WriteFile(lso_file_path
, NULL
, 0));
4874 EXPECT_TRUE(base::PathExists(lso_file_path
));
4876 // Create indexed db. Similarly, it is enough to only simulate this by
4877 // creating the directory on the disk.
4878 IndexedDBContext
* idb_context
= BrowserContext::GetDefaultStoragePartition(
4879 profile())->GetIndexedDBContext();
4880 idb_context
->SetTaskRunnerForTesting(
4881 base::MessageLoop::current()->message_loop_proxy().get());
4882 base::FilePath idb_path
= idb_context
->GetFilePathForTesting(origin_id
);
4883 EXPECT_TRUE(base::CreateDirectory(idb_path
));
4884 EXPECT_TRUE(base::DirectoryExists(idb_path
));
4886 // Uninstall one of them, unlimited storage should still be granted
4888 UninstallExtension(id1
, false);
4889 EXPECT_EQ(1u, registry()->enabled_extensions().size());
4890 EXPECT_TRUE(profile()->GetExtensionSpecialStoragePolicy()->IsStorageUnlimited(
4893 // Check that the cookie is still there.
4894 cookie_monster
->GetAllCookiesForURLAsync(
4896 base::Bind(&ExtensionCookieCallback::GetAllCookiesCallback
,
4897 base::Unretained(&callback
)));
4898 base::RunLoop().RunUntilIdle();
4899 EXPECT_EQ(1U, callback
.list_
.size());
4901 // Now uninstall the other. Storage should be cleared for the apps.
4902 UninstallExtension(id2
, false);
4903 EXPECT_EQ(0u, registry()->enabled_extensions().size());
4905 profile()->GetExtensionSpecialStoragePolicy()->IsStorageUnlimited(
4908 // Check that the cookie is gone.
4909 cookie_monster
->GetAllCookiesForURLAsync(
4911 base::Bind(&ExtensionCookieCallback::GetAllCookiesCallback
,
4912 base::Unretained(&callback
)));
4913 base::RunLoop().RunUntilIdle();
4914 EXPECT_EQ(0U, callback
.list_
.size());
4916 // The database should have vanished as well.
4918 db_tracker
->GetAllOriginsInfo(&origins
);
4919 EXPECT_EQ(0U, origins
.size());
4921 // Check that the LSO file has been removed.
4922 EXPECT_FALSE(base::PathExists(lso_file_path
));
4924 // Check if the indexed db has disappeared too.
4925 EXPECT_FALSE(base::DirectoryExists(idb_path
));
4928 // Tests loading single extensions (like --load-extension)
4929 // Flaky crashes. http://crbug.com/231806
4930 TEST_F(ExtensionServiceTest
, DISABLED_LoadExtension
) {
4931 InitializeEmptyExtensionService();
4933 base::FilePath ext1
= data_dir()
4934 .AppendASCII("good")
4935 .AppendASCII("Extensions")
4936 .AppendASCII("behllobkkfkfnphdnhnkndlbkcpglgmj")
4937 .AppendASCII("1.0.0.0");
4938 extensions::UnpackedInstaller::Create(service())->Load(ext1
);
4939 base::RunLoop().RunUntilIdle();
4940 EXPECT_EQ(0u, GetErrors().size());
4941 ASSERT_EQ(1u, loaded_
.size());
4942 EXPECT_EQ(Manifest::UNPACKED
, loaded_
[0]->location());
4943 EXPECT_EQ(1u, registry()->enabled_extensions().size());
4945 ValidatePrefKeyCount(1);
4947 base::FilePath no_manifest
=
4950 // .AppendASCII("Extensions")
4951 .AppendASCII("cccccccccccccccccccccccccccccccc")
4953 extensions::UnpackedInstaller::Create(service())->Load(no_manifest
);
4954 base::RunLoop().RunUntilIdle();
4955 EXPECT_EQ(1u, GetErrors().size());
4956 ASSERT_EQ(1u, loaded_
.size());
4957 EXPECT_EQ(1u, registry()->enabled_extensions().size());
4960 std::string id
= loaded_
[0]->id();
4961 EXPECT_FALSE(unloaded_id_
.length());
4962 service()->UninstallExtension(id
,
4963 extensions::UNINSTALL_REASON_FOR_TESTING
,
4964 base::Bind(&base::DoNothing
),
4966 base::RunLoop().RunUntilIdle();
4967 EXPECT_EQ(id
, unloaded_id_
);
4968 ASSERT_EQ(0u, loaded_
.size());
4969 EXPECT_EQ(0u, registry()->enabled_extensions().size());
4972 // Tests that we generate IDs when they are not specified in the manifest for
4973 // --load-extension.
4974 TEST_F(ExtensionServiceTest
, GenerateID
) {
4975 InitializeEmptyExtensionService();
4977 base::FilePath no_id_ext
= data_dir().AppendASCII("no_id");
4978 extensions::UnpackedInstaller::Create(service())->Load(no_id_ext
);
4979 base::RunLoop().RunUntilIdle();
4980 EXPECT_EQ(0u, GetErrors().size());
4981 ASSERT_EQ(1u, loaded_
.size());
4982 ASSERT_TRUE(crx_file::id_util::IdIsValid(loaded_
[0]->id()));
4983 EXPECT_EQ(loaded_
[0]->location(), Manifest::UNPACKED
);
4985 ValidatePrefKeyCount(1);
4987 std::string previous_id
= loaded_
[0]->id();
4989 // If we reload the same path, we should get the same extension ID.
4990 extensions::UnpackedInstaller::Create(service())->Load(no_id_ext
);
4991 base::RunLoop().RunUntilIdle();
4992 ASSERT_EQ(1u, loaded_
.size());
4993 ASSERT_EQ(previous_id
, loaded_
[0]->id());
4996 TEST_F(ExtensionServiceTest
, UnpackedValidatesLocales
) {
4997 InitializeEmptyExtensionService();
4999 base::FilePath bad_locale
=
5000 data_dir().AppendASCII("unpacked").AppendASCII("bad_messages_file");
5001 extensions::UnpackedInstaller::Create(service())->Load(bad_locale
);
5002 base::RunLoop().RunUntilIdle();
5003 EXPECT_EQ(1u, GetErrors().size());
5004 base::FilePath ms_messages_file
= bad_locale
.AppendASCII("_locales")
5006 .AppendASCII("messages.json");
5007 EXPECT_THAT(base::UTF16ToUTF8(GetErrors()[0]), testing::AllOf(
5009 base::UTF16ToUTF8(ms_messages_file
.LossyDisplayName())),
5010 testing::HasSubstr("Dictionary keys must be quoted.")));
5011 ASSERT_EQ(0u, loaded_
.size());
5014 void ExtensionServiceTest::TestExternalProvider(
5015 MockExtensionProvider
* provider
, Manifest::Location location
) {
5016 // Verify that starting with no providers loads no extensions.
5018 ASSERT_EQ(0u, loaded_
.size());
5020 provider
->set_visit_count(0);
5022 // Register a test extension externally using the mock registry provider.
5023 base::FilePath source_path
= data_dir().AppendASCII("good.crx");
5025 // Add the extension.
5026 provider
->UpdateOrAddExtension(good_crx
, "1.0.0.0", source_path
);
5028 // Reloading extensions should find our externally registered extension
5030 content::WindowedNotificationObserver
observer(
5031 extensions::NOTIFICATION_CRX_INSTALLER_DONE
,
5032 content::NotificationService::AllSources());
5033 service()->CheckForExternalUpdates();
5036 ASSERT_EQ(0u, GetErrors().size());
5037 ASSERT_EQ(1u, loaded_
.size());
5038 ASSERT_EQ(location
, loaded_
[0]->location());
5039 ASSERT_EQ("1.0.0.0", loaded_
[0]->version()->GetString());
5040 ValidatePrefKeyCount(1);
5041 ValidateIntegerPref(good_crx
, "state", Extension::ENABLED
);
5042 ValidateIntegerPref(good_crx
, "location", location
);
5044 // Reload extensions without changing anything. The extension should be
5047 service()->ReloadExtensionsForTest();
5048 base::RunLoop().RunUntilIdle();
5049 ASSERT_EQ(0u, GetErrors().size());
5050 ASSERT_EQ(1u, loaded_
.size());
5051 ValidatePrefKeyCount(1);
5052 ValidateIntegerPref(good_crx
, "state", Extension::ENABLED
);
5053 ValidateIntegerPref(good_crx
, "location", location
);
5055 // Now update the extension with a new version. We should get upgraded.
5056 source_path
= source_path
.DirName().AppendASCII("good2.crx");
5057 provider
->UpdateOrAddExtension(good_crx
, "1.0.0.1", source_path
);
5060 content::WindowedNotificationObserver
observer_2(
5061 extensions::NOTIFICATION_CRX_INSTALLER_DONE
,
5062 content::NotificationService::AllSources());
5063 service()->CheckForExternalUpdates();
5065 ASSERT_EQ(0u, GetErrors().size());
5066 ASSERT_EQ(1u, loaded_
.size());
5067 ASSERT_EQ("1.0.0.1", loaded_
[0]->version()->GetString());
5068 ValidatePrefKeyCount(1);
5069 ValidateIntegerPref(good_crx
, "state", Extension::ENABLED
);
5070 ValidateIntegerPref(good_crx
, "location", location
);
5072 // Uninstall the extension and reload. Nothing should happen because the
5073 // preference should prevent us from reinstalling.
5074 std::string id
= loaded_
[0]->id();
5076 GetManagementPolicy()->MustRemainEnabled(loaded_
[0].get(), NULL
);
5077 service()->UninstallExtension(id
,
5078 extensions::UNINSTALL_REASON_FOR_TESTING
,
5079 base::Bind(&base::DoNothing
),
5081 base::RunLoop().RunUntilIdle();
5083 base::FilePath install_path
= extensions_install_dir().AppendASCII(id
);
5085 // Policy controlled extensions should not have been touched by uninstall.
5086 ASSERT_TRUE(base::PathExists(install_path
));
5088 // The extension should also be gone from the install directory.
5089 ASSERT_FALSE(base::PathExists(install_path
));
5091 service()->CheckForExternalUpdates();
5092 base::RunLoop().RunUntilIdle();
5093 ASSERT_EQ(0u, loaded_
.size());
5094 ValidatePrefKeyCount(1);
5095 ValidateIntegerPref(good_crx
, "state",
5096 Extension::EXTERNAL_EXTENSION_UNINSTALLED
);
5097 ValidateIntegerPref(good_crx
, "location", location
);
5099 // Now clear the preference and reinstall.
5100 SetPrefInteg(good_crx
, "state", Extension::ENABLED
);
5103 content::WindowedNotificationObserver
observer(
5104 extensions::NOTIFICATION_CRX_INSTALLER_DONE
,
5105 content::NotificationService::AllSources());
5106 service()->CheckForExternalUpdates();
5108 ASSERT_EQ(1u, loaded_
.size());
5110 ValidatePrefKeyCount(1);
5111 ValidateIntegerPref(good_crx
, "state", Extension::ENABLED
);
5112 ValidateIntegerPref(good_crx
, "location", location
);
5114 if (GetManagementPolicy()->MustRemainEnabled(loaded_
[0].get(), NULL
)) {
5115 EXPECT_EQ(2, provider
->visit_count());
5117 // Now test an externally triggered uninstall (deleting the registry key or
5119 provider
->RemoveExtension(good_crx
);
5122 service()->OnExternalProviderReady(provider
);
5123 base::RunLoop().RunUntilIdle();
5124 ASSERT_EQ(0u, loaded_
.size());
5125 ValidatePrefKeyCount(0);
5127 // The extension should also be gone from the install directory.
5128 ASSERT_FALSE(base::PathExists(install_path
));
5130 // Now test the case where user uninstalls and then the extension is removed
5131 // from the external provider.
5132 content::WindowedNotificationObserver
observer(
5133 extensions::NOTIFICATION_CRX_INSTALLER_DONE
,
5134 content::NotificationService::AllSources());
5135 provider
->UpdateOrAddExtension(good_crx
, "1.0.0.1", source_path
);
5136 service()->CheckForExternalUpdates();
5139 ASSERT_EQ(1u, loaded_
.size());
5140 ASSERT_EQ(0u, GetErrors().size());
5144 service()->UninstallExtension(id
,
5145 extensions::UNINSTALL_REASON_FOR_TESTING
,
5146 base::Bind(&base::DoNothing
),
5148 base::RunLoop().RunUntilIdle();
5149 ASSERT_EQ(0u, loaded_
.size());
5151 // Then remove the extension from the extension provider.
5152 provider
->RemoveExtension(good_crx
);
5154 // Should still be at 0.
5156 extensions::InstalledLoader(service()).LoadAllExtensions();
5157 base::RunLoop().RunUntilIdle();
5158 ASSERT_EQ(0u, loaded_
.size());
5159 ValidatePrefKeyCount(1);
5161 EXPECT_EQ(5, provider
->visit_count());
5165 // Tests the external installation feature
5167 TEST_F(ExtensionServiceTest
, ExternalInstallRegistry
) {
5168 // This should all work, even when normal extension installation is disabled.
5169 InitializeEmptyExtensionService();
5170 service()->set_extensions_enabled(false);
5172 // Now add providers. Extension system takes ownership of the objects.
5173 MockExtensionProvider
* reg_provider
=
5174 new MockExtensionProvider(service(), Manifest::EXTERNAL_REGISTRY
);
5175 AddMockExternalProvider(reg_provider
);
5176 TestExternalProvider(reg_provider
, Manifest::EXTERNAL_REGISTRY
);
5180 TEST_F(ExtensionServiceTest
, ExternalInstallPref
) {
5181 InitializeEmptyExtensionService();
5183 // Now add providers. Extension system takes ownership of the objects.
5184 MockExtensionProvider
* pref_provider
=
5185 new MockExtensionProvider(service(), Manifest::EXTERNAL_PREF
);
5187 AddMockExternalProvider(pref_provider
);
5188 TestExternalProvider(pref_provider
, Manifest::EXTERNAL_PREF
);
5191 TEST_F(ExtensionServiceTest
, ExternalInstallPrefUpdateUrl
) {
5192 // This should all work, even when normal extension installation is disabled.
5193 InitializeEmptyExtensionService();
5194 service()->set_extensions_enabled(false);
5196 // TODO(skerner): The mock provider is not a good model of a provider
5197 // that works with update URLs, because it adds file and version info.
5198 // Extend the mock to work with update URLs. This test checks the
5199 // behavior that is common to all external extension visitors. The
5200 // browser test ExtensionManagementTest.ExternalUrlUpdate tests that
5201 // what the visitor does results in an extension being downloaded and
5203 MockExtensionProvider
* pref_provider
=
5204 new MockExtensionProvider(service(), Manifest::EXTERNAL_PREF_DOWNLOAD
);
5205 AddMockExternalProvider(pref_provider
);
5206 TestExternalProvider(pref_provider
, Manifest::EXTERNAL_PREF_DOWNLOAD
);
5209 TEST_F(ExtensionServiceTest
, ExternalInstallPolicyUpdateUrl
) {
5210 // This should all work, even when normal extension installation is disabled.
5211 InitializeEmptyExtensionService();
5212 service()->set_extensions_enabled(false);
5214 // TODO(skerner): The mock provider is not a good model of a provider
5215 // that works with update URLs, because it adds file and version info.
5216 // Extend the mock to work with update URLs. This test checks the
5217 // behavior that is common to all external extension visitors. The
5218 // browser test ExtensionManagementTest.ExternalUrlUpdate tests that
5219 // what the visitor does results in an extension being downloaded and
5221 MockExtensionProvider
* pref_provider
=
5222 new MockExtensionProvider(service(), Manifest::EXTERNAL_POLICY_DOWNLOAD
);
5223 AddMockExternalProvider(pref_provider
);
5224 TestExternalProvider(pref_provider
, Manifest::EXTERNAL_POLICY_DOWNLOAD
);
5227 // Tests that external extensions get uninstalled when the external extension
5228 // providers can't account for them.
5229 TEST_F(ExtensionServiceTest
, ExternalUninstall
) {
5230 // Start the extensions service with one external extension already installed.
5231 base::FilePath source_install_dir
=
5232 data_dir().AppendASCII("good").AppendASCII("Extensions");
5233 base::FilePath pref_path
= source_install_dir
5235 .AppendASCII("PreferencesExternal");
5237 // This initializes the extensions service with no ExternalProviders.
5238 InitializeInstalledExtensionService(pref_path
, source_install_dir
);
5239 service()->set_extensions_enabled(false);
5243 ASSERT_EQ(0u, GetErrors().size());
5244 ASSERT_EQ(0u, loaded_
.size());
5246 // Verify that it's not the disabled extensions flag causing it not to load.
5247 service()->set_extensions_enabled(true);
5248 service()->ReloadExtensionsForTest();
5249 base::RunLoop().RunUntilIdle();
5251 ASSERT_EQ(0u, GetErrors().size());
5252 ASSERT_EQ(0u, loaded_
.size());
5255 // Test that running multiple update checks simultaneously does not
5256 // keep the update from succeeding.
5257 TEST_F(ExtensionServiceTest
, MultipleExternalUpdateCheck
) {
5258 InitializeEmptyExtensionService();
5260 MockExtensionProvider
* provider
=
5261 new MockExtensionProvider(service(), Manifest::EXTERNAL_PREF
);
5262 AddMockExternalProvider(provider
);
5264 // Verify that starting with no providers loads no extensions.
5266 ASSERT_EQ(0u, loaded_
.size());
5268 // Start two checks for updates.
5269 provider
->set_visit_count(0);
5270 service()->CheckForExternalUpdates();
5271 service()->CheckForExternalUpdates();
5272 base::RunLoop().RunUntilIdle();
5274 // Two calls should cause two checks for external extensions.
5275 EXPECT_EQ(2, provider
->visit_count());
5276 EXPECT_EQ(0u, GetErrors().size());
5277 EXPECT_EQ(0u, loaded_
.size());
5279 // Register a test extension externally using the mock registry provider.
5280 base::FilePath source_path
= data_dir().AppendASCII("good.crx");
5281 provider
->UpdateOrAddExtension(good_crx
, "1.0.0.0", source_path
);
5283 // Two checks for external updates should find the extension, and install it
5285 content::WindowedNotificationObserver
observer(
5286 extensions::NOTIFICATION_CRX_INSTALLER_DONE
,
5287 content::NotificationService::AllSources());
5288 provider
->set_visit_count(0);
5289 service()->CheckForExternalUpdates();
5290 service()->CheckForExternalUpdates();
5292 EXPECT_EQ(2, provider
->visit_count());
5293 ASSERT_EQ(0u, GetErrors().size());
5294 ASSERT_EQ(1u, loaded_
.size());
5295 ASSERT_EQ(Manifest::EXTERNAL_PREF
, loaded_
[0]->location());
5296 ASSERT_EQ("1.0.0.0", loaded_
[0]->version()->GetString());
5297 ValidatePrefKeyCount(1);
5298 ValidateIntegerPref(good_crx
, "state", Extension::ENABLED
);
5299 ValidateIntegerPref(good_crx
, "location", Manifest::EXTERNAL_PREF
);
5301 provider
->RemoveExtension(good_crx
);
5302 provider
->set_visit_count(0);
5303 service()->CheckForExternalUpdates();
5304 service()->CheckForExternalUpdates();
5305 base::RunLoop().RunUntilIdle();
5307 // Two calls should cause two checks for external extensions.
5308 // Because the external source no longer includes good_crx,
5309 // good_crx will be uninstalled. So, expect that no extensions
5311 EXPECT_EQ(2, provider
->visit_count());
5312 EXPECT_EQ(0u, GetErrors().size());
5313 EXPECT_EQ(0u, loaded_
.size());
5316 TEST_F(ExtensionServiceTest
, ExternalPrefProvider
) {
5317 InitializeEmptyExtensionService();
5319 // Test some valid extension records.
5320 // Set a base path to avoid erroring out on relative paths.
5321 // Paths starting with // are absolute on every platform we support.
5322 base::FilePath
base_path(FILE_PATH_LITERAL("//base/path"));
5323 ASSERT_TRUE(base_path
.IsAbsolute());
5324 MockProviderVisitor
visitor(base_path
);
5325 std::string json_data
=
5327 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\": {"
5328 " \"external_crx\": \"RandomExtension.crx\","
5329 " \"external_version\": \"1.0\""
5331 " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\": {"
5332 " \"external_crx\": \"RandomExtension2.crx\","
5333 " \"external_version\": \"2.0\""
5335 " \"cccccccccccccccccccccccccccccccc\": {"
5336 " \"external_update_url\": \"http:\\\\foo.com/update\","
5337 " \"install_parameter\": \"id\""
5340 EXPECT_EQ(3, visitor
.Visit(json_data
));
5342 // Simulate an external_extensions.json file that contains seven invalid
5344 // - One that is missing the 'external_crx' key.
5345 // - One that is missing the 'external_version' key.
5346 // - One that is specifying .. in the path.
5347 // - One that specifies both a file and update URL.
5348 // - One that specifies no file or update URL.
5349 // - One that has an update URL that is not well formed.
5350 // - One that contains a malformed version.
5351 // - One that has an invalid id.
5352 // - One that has a non-dictionary value.
5353 // - One that has an integer 'external_version' instead of a string.
5354 // The final extension is valid, and we check that it is read to make sure
5355 // failures don't stop valid records from being read.
5358 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\": {"
5359 " \"external_version\": \"1.0\""
5361 " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\": {"
5362 " \"external_crx\": \"RandomExtension.crx\""
5364 " \"cccccccccccccccccccccccccccccccc\": {"
5365 " \"external_crx\": \"..\\\\foo\\\\RandomExtension2.crx\","
5366 " \"external_version\": \"2.0\""
5368 " \"dddddddddddddddddddddddddddddddd\": {"
5369 " \"external_crx\": \"RandomExtension2.crx\","
5370 " \"external_version\": \"2.0\","
5371 " \"external_update_url\": \"http:\\\\foo.com/update\""
5373 " \"eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee\": {"
5375 " \"ffffffffffffffffffffffffffffffff\": {"
5376 " \"external_update_url\": \"This string is not a valid URL\""
5378 " \"gggggggggggggggggggggggggggggggg\": {"
5379 " \"external_crx\": \"RandomExtension3.crx\","
5380 " \"external_version\": \"This is not a valid version!\""
5382 " \"This is not a valid id!\": {},"
5383 " \"hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh\": true,"
5384 " \"iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii\": {"
5385 " \"external_crx\": \"RandomExtension4.crx\","
5386 " \"external_version\": 1.0"
5388 " \"pppppppppppppppppppppppppppppppp\": {"
5389 " \"external_crx\": \"RandomValidExtension.crx\","
5390 " \"external_version\": \"1.0\""
5393 EXPECT_EQ(1, visitor
.Visit(json_data
));
5395 // Check that if a base path is not provided, use of a relative
5397 base::FilePath empty
;
5398 MockProviderVisitor
visitor_no_relative_paths(empty
);
5400 // Use absolute paths. Expect success.
5403 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\": {"
5404 " \"external_crx\": \"//RandomExtension1.crx\","
5405 " \"external_version\": \"3.0\""
5407 " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\": {"
5408 " \"external_crx\": \"//path/to/RandomExtension2.crx\","
5409 " \"external_version\": \"3.0\""
5412 EXPECT_EQ(2, visitor_no_relative_paths
.Visit(json_data
));
5414 // Use a relative path. Expect that it will error out.
5417 " \"cccccccccccccccccccccccccccccccc\": {"
5418 " \"external_crx\": \"RandomExtension2.crx\","
5419 " \"external_version\": \"3.0\""
5422 EXPECT_EQ(0, visitor_no_relative_paths
.Visit(json_data
));
5424 // Test supported_locales.
5427 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\": {"
5428 " \"external_crx\": \"RandomExtension.crx\","
5429 " \"external_version\": \"1.0\","
5430 " \"supported_locales\": [ \"en\" ]"
5432 " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\": {"
5433 " \"external_crx\": \"RandomExtension2.crx\","
5434 " \"external_version\": \"2.0\","
5435 " \"supported_locales\": [ \"en-GB\" ]"
5437 " \"cccccccccccccccccccccccccccccccc\": {"
5438 " \"external_crx\": \"RandomExtension2.crx\","
5439 " \"external_version\": \"3.0\","
5440 " \"supported_locales\": [ \"en_US\", \"fr\" ]"
5444 ScopedBrowserLocale
guard("en-US");
5445 EXPECT_EQ(2, visitor
.Visit(json_data
));
5448 // Test keep_if_present.
5451 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\": {"
5452 " \"external_crx\": \"RandomExtension.crx\","
5453 " \"external_version\": \"1.0\","
5454 " \"keep_if_present\": true"
5458 EXPECT_EQ(0, visitor
.Visit(json_data
));
5461 // Test is_bookmark_app.
5462 MockProviderVisitor
from_bookmark_visitor(
5463 base_path
, Extension::FROM_BOOKMARK
);
5466 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\": {"
5467 " \"external_crx\": \"RandomExtension.crx\","
5468 " \"external_version\": \"1.0\","
5469 " \"is_bookmark_app\": true"
5472 EXPECT_EQ(1, from_bookmark_visitor
.Visit(json_data
));
5474 // Test is_from_webstore.
5475 MockProviderVisitor
from_webstore_visitor(
5476 base_path
, Extension::FROM_WEBSTORE
);
5479 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\": {"
5480 " \"external_crx\": \"RandomExtension.crx\","
5481 " \"external_version\": \"1.0\","
5482 " \"is_from_webstore\": true"
5485 EXPECT_EQ(1, from_webstore_visitor
.Visit(json_data
));
5487 // Test was_installed_by_eom.
5488 MockProviderVisitor
was_installed_by_eom_visitor(
5489 base_path
, Extension::WAS_INSTALLED_BY_OEM
);
5492 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\": {"
5493 " \"external_crx\": \"RandomExtension.crx\","
5494 " \"external_version\": \"1.0\","
5495 " \"was_installed_by_oem\": true"
5498 EXPECT_EQ(1, was_installed_by_eom_visitor
.Visit(json_data
));
5500 // Test min_profile_created_by_version.
5501 MockProviderVisitor
min_profile_created_by_version_visitor(base_path
);
5504 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\": {"
5505 " \"external_crx\": \"RandomExtension.crx\","
5506 " \"external_version\": \"1.0\","
5507 " \"min_profile_created_by_version\": \"42.0.0.1\""
5509 " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\": {"
5510 " \"external_crx\": \"RandomExtension2.crx\","
5511 " \"external_version\": \"1.0\","
5512 " \"min_profile_created_by_version\": \"43.0.0.1\""
5514 " \"cccccccccccccccccccccccccccccccc\": {"
5515 " \"external_crx\": \"RandomExtension3.crx\","
5516 " \"external_version\": \"3.0\","
5517 " \"min_profile_created_by_version\": \"44.0.0.1\""
5520 min_profile_created_by_version_visitor
.profile()->GetPrefs()->SetString(
5521 prefs::kProfileCreatedByVersion
, "40.0.0.1");
5522 EXPECT_EQ(0, min_profile_created_by_version_visitor
.Visit(json_data
));
5523 min_profile_created_by_version_visitor
.profile()->GetPrefs()->SetString(
5524 prefs::kProfileCreatedByVersion
, "43.0.0.1");
5525 EXPECT_EQ(2, min_profile_created_by_version_visitor
.Visit(json_data
));
5526 min_profile_created_by_version_visitor
.profile()->GetPrefs()->SetString(
5527 prefs::kProfileCreatedByVersion
, "45.0.0.1");
5528 EXPECT_EQ(3, min_profile_created_by_version_visitor
.Visit(json_data
));
5531 // Test loading good extensions from the profile directory.
5532 TEST_F(ExtensionServiceTest
, LoadAndRelocalizeExtensions
) {
5533 // Ensure we're testing in "en" and leave global state untouched.
5534 extension_l10n_util::ScopedLocaleForTest
testLocale("en");
5536 // Initialize the test dir with a good Preferences/extensions.
5537 base::FilePath source_install_dir
= data_dir().AppendASCII("l10n");
5538 base::FilePath pref_path
=
5539 source_install_dir
.Append(chrome::kPreferencesFilename
);
5540 InitializeInstalledExtensionService(pref_path
, source_install_dir
);
5544 ASSERT_EQ(3u, loaded_
.size());
5546 // This was equal to "sr" on load.
5547 ValidateStringPref(loaded_
[0]->id(), keys::kCurrentLocale
, "en");
5549 // These are untouched by re-localization.
5550 ValidateStringPref(loaded_
[1]->id(), keys::kCurrentLocale
, "en");
5551 EXPECT_FALSE(IsPrefExist(loaded_
[1]->id(), keys::kCurrentLocale
));
5553 // This one starts with Serbian name, and gets re-localized into English.
5554 EXPECT_EQ("My name is simple.", loaded_
[0]->name());
5556 // These are untouched by re-localization.
5557 EXPECT_EQ("My name is simple.", loaded_
[1]->name());
5558 EXPECT_EQ("no l10n", loaded_
[2]->name());
5561 class ExtensionsReadyRecorder
: public content::NotificationObserver
{
5563 ExtensionsReadyRecorder() : ready_(false) {
5564 registrar_
.Add(this,
5565 extensions::NOTIFICATION_EXTENSIONS_READY_DEPRECATED
,
5566 content::NotificationService::AllSources());
5569 void set_ready(bool value
) { ready_
= value
; }
5570 bool ready() { return ready_
; }
5573 void Observe(int type
,
5574 const content::NotificationSource
& source
,
5575 const content::NotificationDetails
& details
) override
{
5577 case extensions::NOTIFICATION_EXTENSIONS_READY_DEPRECATED
:
5585 content::NotificationRegistrar registrar_
;
5589 // Test that we get enabled/disabled correctly for all the pref/command-line
5590 // combinations. We don't want to derive from the ExtensionServiceTest class
5591 // for this test, so we use ExtensionServiceTestSimple.
5593 // Also tests that we always fire EXTENSIONS_READY, no matter whether we are
5595 class ExtensionServiceTestSimple
: public testing::Test
{
5596 content::TestBrowserThreadBundle thread_bundle_
;
5599 TEST_F(ExtensionServiceTestSimple
, Enabledness
) {
5600 // Make sure the PluginService singleton is destroyed at the end of the test.
5601 base::ShadowingAtExitManager at_exit_manager
;
5602 #if defined(ENABLE_PLUGINS)
5603 content::PluginService::GetInstance()->Init();
5604 content::PluginService::GetInstance()->DisablePluginsDiscoveryForTesting();
5607 ExtensionErrorReporter::Init(false); // no noisy errors
5608 ExtensionsReadyRecorder recorder
;
5609 scoped_ptr
<TestingProfile
> profile(new TestingProfile());
5610 #if defined OS_CHROMEOS
5611 chromeos::ScopedTestDeviceSettingsService device_settings_service
;
5612 chromeos::ScopedTestCrosSettings cros_settings
;
5613 scoped_ptr
<chromeos::ScopedTestUserManager
> user_manager(
5614 new chromeos::ScopedTestUserManager
);
5616 scoped_ptr
<base::CommandLine
> command_line
;
5617 base::FilePath install_dir
= profile
->GetPath()
5618 .AppendASCII(extensions::kInstallDirectoryName
);
5620 // By default, we are enabled.
5621 command_line
.reset(new base::CommandLine(base::CommandLine::NO_PROGRAM
));
5622 ExtensionService
* service
= static_cast<extensions::TestExtensionSystem
*>(
5623 ExtensionSystem::Get(profile
.get()))->
5624 CreateExtensionService(
5628 EXPECT_TRUE(service
->extensions_enabled());
5630 base::RunLoop().RunUntilIdle();
5631 EXPECT_TRUE(recorder
.ready());
5632 #if defined OS_CHROMEOS
5633 user_manager
.reset();
5636 // If either the command line or pref is set, we are disabled.
5637 recorder
.set_ready(false);
5638 profile
.reset(new TestingProfile());
5639 command_line
->AppendSwitch(switches::kDisableExtensions
);
5640 service
= static_cast<extensions::TestExtensionSystem
*>(
5641 ExtensionSystem::Get(profile
.get()))->
5642 CreateExtensionService(
5646 EXPECT_FALSE(service
->extensions_enabled());
5648 base::RunLoop().RunUntilIdle();
5649 EXPECT_TRUE(recorder
.ready());
5651 recorder
.set_ready(false);
5652 profile
.reset(new TestingProfile());
5653 profile
->GetPrefs()->SetBoolean(prefs::kDisableExtensions
, true);
5654 service
= static_cast<extensions::TestExtensionSystem
*>(
5655 ExtensionSystem::Get(profile
.get()))->
5656 CreateExtensionService(
5660 EXPECT_FALSE(service
->extensions_enabled());
5662 base::RunLoop().RunUntilIdle();
5663 EXPECT_TRUE(recorder
.ready());
5665 recorder
.set_ready(false);
5666 profile
.reset(new TestingProfile());
5667 profile
->GetPrefs()->SetBoolean(prefs::kDisableExtensions
, true);
5668 command_line
.reset(new base::CommandLine(base::CommandLine::NO_PROGRAM
));
5669 service
= static_cast<extensions::TestExtensionSystem
*>(
5670 ExtensionSystem::Get(profile
.get()))->
5671 CreateExtensionService(
5675 EXPECT_FALSE(service
->extensions_enabled());
5677 base::RunLoop().RunUntilIdle();
5678 EXPECT_TRUE(recorder
.ready());
5680 // Explicitly delete all the resources used in this test.
5683 // Execute any pending deletion tasks.
5684 base::RunLoop().RunUntilIdle();
5687 // Test loading extensions that require limited and unlimited storage quotas.
5688 TEST_F(ExtensionServiceTest
, StorageQuota
) {
5689 InitializeEmptyExtensionService();
5691 base::FilePath extensions_path
= data_dir().AppendASCII("storage_quota");
5693 base::FilePath limited_quota_ext
=
5694 extensions_path
.AppendASCII("limited_quota")
5695 .AppendASCII("1.0");
5697 // The old permission name for unlimited quota was "unlimited_storage", but
5698 // we changed it to "unlimitedStorage". This tests both versions.
5699 base::FilePath unlimited_quota_ext
=
5700 extensions_path
.AppendASCII("unlimited_quota")
5701 .AppendASCII("1.0");
5702 base::FilePath unlimited_quota_ext2
=
5703 extensions_path
.AppendASCII("unlimited_quota")
5704 .AppendASCII("2.0");
5705 extensions::UnpackedInstaller::Create(service())->Load(limited_quota_ext
);
5706 extensions::UnpackedInstaller::Create(service())->Load(unlimited_quota_ext
);
5707 extensions::UnpackedInstaller::Create(service())->Load(unlimited_quota_ext2
);
5708 base::RunLoop().RunUntilIdle();
5710 ASSERT_EQ(3u, loaded_
.size());
5711 EXPECT_TRUE(profile());
5712 EXPECT_FALSE(profile()->IsOffTheRecord());
5714 profile()->GetExtensionSpecialStoragePolicy()->IsStorageUnlimited(
5715 loaded_
[0]->url()));
5716 EXPECT_TRUE(profile()->GetExtensionSpecialStoragePolicy()->IsStorageUnlimited(
5717 loaded_
[1]->url()));
5718 EXPECT_TRUE(profile()->GetExtensionSpecialStoragePolicy()->IsStorageUnlimited(
5719 loaded_
[2]->url()));
5722 // Tests ComponentLoader::Add().
5723 TEST_F(ExtensionServiceTest
, ComponentExtensions
) {
5724 InitializeEmptyExtensionService();
5726 // Component extensions should work even when extensions are disabled.
5727 service()->set_extensions_enabled(false);
5729 base::FilePath path
= data_dir()
5730 .AppendASCII("good")
5731 .AppendASCII("Extensions")
5732 .AppendASCII("behllobkkfkfnphdnhnkndlbkcpglgmj")
5733 .AppendASCII("1.0.0.0");
5735 std::string manifest
;
5736 ASSERT_TRUE(base::ReadFileToString(
5737 path
.Append(extensions::kManifestFilename
), &manifest
));
5739 service()->component_loader()->Add(manifest
, path
);
5742 // Note that we do not pump messages -- the extension should be loaded
5745 EXPECT_EQ(0u, GetErrors().size());
5746 ASSERT_EQ(1u, loaded_
.size());
5747 EXPECT_EQ(Manifest::COMPONENT
, loaded_
[0]->location());
5748 EXPECT_EQ(1u, registry()->enabled_extensions().size());
5750 // Component extensions get a prefs entry on first install.
5751 ValidatePrefKeyCount(1);
5753 // Reload all extensions, and make sure it comes back.
5754 std::string extension_id
= (*registry()->enabled_extensions().begin())->id();
5756 service()->ReloadExtensionsForTest();
5757 ASSERT_EQ(1u, registry()->enabled_extensions().size());
5758 EXPECT_EQ(extension_id
, (*registry()->enabled_extensions().begin())->id());
5761 TEST_F(ExtensionServiceTest
, DeferredSyncStartupPreInstalledComponent
) {
5762 InitializeEmptyExtensionService();
5763 InitializeExtensionSyncService();
5765 bool flare_was_called
= false;
5766 syncer::ModelType
triggered_type(syncer::UNSPECIFIED
);
5767 base::WeakPtrFactory
<ExtensionServiceTest
> factory(this);
5768 extension_sync_service()->SetSyncStartFlare(
5769 base::Bind(&ExtensionServiceTest::MockSyncStartFlare
,
5770 factory
.GetWeakPtr(),
5771 &flare_was_called
, // Safe due to WeakPtrFactory scope.
5772 &triggered_type
)); // Safe due to WeakPtrFactory scope.
5774 // Install a component extension.
5775 std::string manifest
;
5776 ASSERT_TRUE(base::ReadFileToString(
5777 good0_path().Append(extensions::kManifestFilename
), &manifest
));
5778 service()->component_loader()->Add(manifest
, good0_path());
5779 ASSERT_FALSE(service()->is_ready());
5781 ASSERT_TRUE(service()->is_ready());
5783 // Extensions added before service is_ready() don't trigger sync startup.
5784 EXPECT_FALSE(flare_was_called
);
5785 ASSERT_EQ(syncer::UNSPECIFIED
, triggered_type
);
5788 TEST_F(ExtensionServiceTest
, DeferredSyncStartupPreInstalledNormal
) {
5789 InitializeGoodInstalledExtensionService();
5790 InitializeExtensionSyncService();
5792 bool flare_was_called
= false;
5793 syncer::ModelType
triggered_type(syncer::UNSPECIFIED
);
5794 base::WeakPtrFactory
<ExtensionServiceTest
> factory(this);
5795 extension_sync_service()->SetSyncStartFlare(
5796 base::Bind(&ExtensionServiceTest::MockSyncStartFlare
,
5797 factory
.GetWeakPtr(),
5798 &flare_was_called
, // Safe due to WeakPtrFactory scope.
5799 &triggered_type
)); // Safe due to WeakPtrFactory scope.
5801 ASSERT_FALSE(service()->is_ready());
5803 ASSERT_EQ(3u, loaded_
.size());
5804 ASSERT_TRUE(service()->is_ready());
5806 // Extensions added before service is_ready() don't trigger sync startup.
5807 EXPECT_FALSE(flare_was_called
);
5808 ASSERT_EQ(syncer::UNSPECIFIED
, triggered_type
);
5811 TEST_F(ExtensionServiceTest
, DeferredSyncStartupOnInstall
) {
5812 InitializeEmptyExtensionService();
5813 InitializeExtensionSyncService();
5815 ASSERT_TRUE(service()->is_ready());
5817 bool flare_was_called
= false;
5818 syncer::ModelType
triggered_type(syncer::UNSPECIFIED
);
5819 base::WeakPtrFactory
<ExtensionServiceTest
> factory(this);
5820 extension_sync_service()->SetSyncStartFlare(
5821 base::Bind(&ExtensionServiceTest::MockSyncStartFlare
,
5822 factory
.GetWeakPtr(),
5823 &flare_was_called
, // Safe due to WeakPtrFactory scope.
5824 &triggered_type
)); // Safe due to WeakPtrFactory scope.
5826 base::FilePath path
= data_dir().AppendASCII("good.crx");
5827 InstallCRX(path
, INSTALL_NEW
);
5829 EXPECT_TRUE(flare_was_called
);
5830 EXPECT_EQ(syncer::EXTENSIONS
, triggered_type
);
5833 flare_was_called
= false;
5834 triggered_type
= syncer::UNSPECIFIED
;
5836 // Once sync starts, flare should no longer be invoked.
5837 extension_sync_service()->MergeDataAndStartSyncing(
5839 syncer::SyncDataList(),
5840 scoped_ptr
<syncer::SyncChangeProcessor
>(
5841 new syncer::FakeSyncChangeProcessor
),
5842 scoped_ptr
<syncer::SyncErrorFactory
>(new syncer::SyncErrorFactoryMock()));
5843 path
= data_dir().AppendASCII("page_action.crx");
5844 InstallCRX(path
, INSTALL_NEW
);
5845 EXPECT_FALSE(flare_was_called
);
5846 ASSERT_EQ(syncer::UNSPECIFIED
, triggered_type
);
5849 TEST_F(ExtensionServiceTest
, DisableExtensionFromSync
) {
5850 // Start the extensions service with one external extension already installed.
5851 base::FilePath source_install_dir
=
5852 data_dir().AppendASCII("good").AppendASCII("Extensions");
5853 base::FilePath pref_path
=
5854 source_install_dir
.DirName().Append(chrome::kPreferencesFilename
);
5856 InitializeInstalledExtensionService(pref_path
, source_install_dir
);
5857 InitializeExtensionSyncService();
5859 // The user has enabled sync.
5860 ProfileSyncService
* sync_service
=
5861 ProfileSyncServiceFactory::GetForProfile(profile());
5862 sync_service
->SetSyncSetupCompleted();
5865 ASSERT_TRUE(service()->is_ready());
5867 ASSERT_EQ(3u, loaded_
.size());
5869 // We start enabled.
5870 const Extension
* extension
= service()->GetExtensionById(good0
, true);
5871 ASSERT_TRUE(extension
);
5872 ASSERT_TRUE(service()->IsExtensionEnabled(good0
));
5874 // Then sync data arrives telling us to disable |good0|.
5875 ExtensionSyncData
disable_good_crx(*extension
, false,
5876 Extension::DISABLE_USER_ACTION
, false,
5877 false, ExtensionSyncData::BOOLEAN_UNSET
);
5878 syncer::SyncDataList sync_data
;
5879 sync_data
.push_back(disable_good_crx
.GetSyncData());
5880 extension_sync_service()->MergeDataAndStartSyncing(
5883 scoped_ptr
<syncer::SyncChangeProcessor
>(
5884 new syncer::FakeSyncChangeProcessor
),
5885 scoped_ptr
<syncer::SyncErrorFactory
>(new syncer::SyncErrorFactoryMock()));
5886 ASSERT_FALSE(service()->IsExtensionEnabled(good0
));
5889 TEST_F(ExtensionServiceTest
, DontDisableExtensionWithPendingEnableFromSync
) {
5890 // Start the extensions service with one external extension already installed.
5891 base::FilePath source_install_dir
=
5892 data_dir().AppendASCII("good").AppendASCII("Extensions");
5893 base::FilePath pref_path
=
5894 source_install_dir
.DirName().Append(chrome::kPreferencesFilename
);
5896 InitializeInstalledExtensionService(pref_path
, source_install_dir
);
5897 InitializeExtensionSyncService();
5899 // The user has enabled sync.
5900 ProfileSyncService
* sync_service
=
5901 ProfileSyncServiceFactory::GetForProfile(profile());
5902 sync_service
->SetSyncSetupCompleted();
5905 ASSERT_TRUE(service()->is_ready());
5906 ASSERT_EQ(3u, loaded_
.size());
5908 const Extension
* extension
= service()->GetExtensionById(good0
, true);
5909 ASSERT_TRUE(service()->IsExtensionEnabled(good0
));
5911 // Disable extension before first sync data arrives.
5912 service()->DisableExtension(good0
, Extension::DISABLE_USER_ACTION
);
5913 ASSERT_FALSE(service()->IsExtensionEnabled(good0
));
5915 // Enable extension - this is now the most recent state.
5916 service()->EnableExtension(good0
);
5917 ASSERT_TRUE(service()->IsExtensionEnabled(good0
));
5919 // Now sync data comes in that says to disable good0. This should be
5921 ExtensionSyncData
disable_good_crx(*extension
, false,
5922 Extension::DISABLE_USER_ACTION
, false,
5923 false, ExtensionSyncData::BOOLEAN_UNSET
);
5924 syncer::SyncDataList sync_data
;
5925 sync_data
.push_back(disable_good_crx
.GetSyncData());
5926 extension_sync_service()->MergeDataAndStartSyncing(
5929 scoped_ptr
<syncer::SyncChangeProcessor
>(
5930 new syncer::FakeSyncChangeProcessor
),
5931 scoped_ptr
<syncer::SyncErrorFactory
>(new syncer::SyncErrorFactoryMock()));
5933 // The extension was enabled locally before the sync data arrived, so it
5934 // should still be enabled now.
5935 ASSERT_TRUE(service()->IsExtensionEnabled(good0
));
5938 TEST_F(ExtensionServiceTest
, GetSyncData
) {
5939 InitializeEmptyExtensionService();
5940 InitializeExtensionSyncService();
5941 InstallCRX(data_dir().AppendASCII("good.crx"), INSTALL_NEW
);
5942 const Extension
* extension
= service()->GetInstalledExtension(good_crx
);
5943 ASSERT_TRUE(extension
);
5945 extension_sync_service()->MergeDataAndStartSyncing(
5947 syncer::SyncDataList(),
5948 scoped_ptr
<syncer::SyncChangeProcessor
>(
5949 new syncer::FakeSyncChangeProcessor
),
5950 scoped_ptr
<syncer::SyncErrorFactory
>(new syncer::SyncErrorFactoryMock()));
5952 syncer::SyncDataList list
=
5953 extension_sync_service()->GetAllSyncData(syncer::EXTENSIONS
);
5954 ASSERT_EQ(list
.size(), 1U);
5955 scoped_ptr
<ExtensionSyncData
> data
=
5956 ExtensionSyncData::CreateFromSyncData(list
[0]);
5957 ASSERT_TRUE(data
.get());
5958 EXPECT_EQ(extension
->id(), data
->id());
5959 EXPECT_FALSE(data
->uninstalled());
5960 EXPECT_EQ(service()->IsExtensionEnabled(good_crx
), data
->enabled());
5961 EXPECT_EQ(extensions::util::IsIncognitoEnabled(good_crx
, profile()),
5962 data
->incognito_enabled());
5963 EXPECT_EQ(ExtensionSyncData::BOOLEAN_UNSET
, data
->all_urls_enabled());
5964 EXPECT_TRUE(data
->version().Equals(*extension
->version()));
5965 EXPECT_EQ(extensions::ManifestURL::GetUpdateURL(extension
),
5966 data
->update_url());
5967 EXPECT_EQ(extension
->name(), data
->name());
5970 TEST_F(ExtensionServiceTest
, GetSyncDataTerminated
) {
5971 InitializeEmptyExtensionService();
5972 InitializeExtensionSyncService();
5973 InstallCRX(data_dir().AppendASCII("good.crx"), INSTALL_NEW
);
5974 TerminateExtension(good_crx
);
5975 const Extension
* extension
= service()->GetInstalledExtension(good_crx
);
5976 ASSERT_TRUE(extension
);
5978 syncer::FakeSyncChangeProcessor processor
;
5979 extension_sync_service()->MergeDataAndStartSyncing(
5981 syncer::SyncDataList(),
5982 scoped_ptr
<syncer::SyncChangeProcessor
>(
5983 new syncer::FakeSyncChangeProcessor
),
5984 scoped_ptr
<syncer::SyncErrorFactory
>(new syncer::SyncErrorFactoryMock()));
5986 syncer::SyncDataList list
=
5987 extension_sync_service()->GetAllSyncData(syncer::EXTENSIONS
);
5988 ASSERT_EQ(list
.size(), 1U);
5989 scoped_ptr
<ExtensionSyncData
> data
=
5990 ExtensionSyncData::CreateFromSyncData(list
[0]);
5991 ASSERT_TRUE(data
.get());
5992 EXPECT_EQ(extension
->id(), data
->id());
5993 EXPECT_FALSE(data
->uninstalled());
5994 EXPECT_EQ(service()->IsExtensionEnabled(good_crx
), data
->enabled());
5995 EXPECT_EQ(extensions::util::IsIncognitoEnabled(good_crx
, profile()),
5996 data
->incognito_enabled());
5997 EXPECT_EQ(ExtensionSyncData::BOOLEAN_UNSET
, data
->all_urls_enabled());
5998 EXPECT_TRUE(data
->version().Equals(*extension
->version()));
5999 EXPECT_EQ(extensions::ManifestURL::GetUpdateURL(extension
),
6000 data
->update_url());
6001 EXPECT_EQ(extension
->name(), data
->name());
6004 TEST_F(ExtensionServiceTest
, GetSyncDataFilter
) {
6005 InitializeEmptyExtensionService();
6006 InitializeExtensionSyncService();
6007 InstallCRX(data_dir().AppendASCII("good.crx"), INSTALL_NEW
);
6008 const Extension
* extension
= service()->GetInstalledExtension(good_crx
);
6009 ASSERT_TRUE(extension
);
6011 syncer::FakeSyncChangeProcessor processor
;
6012 extension_sync_service()->MergeDataAndStartSyncing(
6014 syncer::SyncDataList(),
6015 scoped_ptr
<syncer::SyncChangeProcessor
>(
6016 new syncer::FakeSyncChangeProcessor
),
6017 scoped_ptr
<syncer::SyncErrorFactory
>(new syncer::SyncErrorFactoryMock()));
6019 syncer::SyncDataList list
=
6020 extension_sync_service()->GetAllSyncData(syncer::EXTENSIONS
);
6021 ASSERT_EQ(list
.size(), 0U);
6024 TEST_F(ExtensionServiceTest
, GetSyncExtensionDataUserSettings
) {
6025 InitializeEmptyExtensionService();
6026 InitializeExtensionSyncService();
6027 InstallCRX(data_dir().AppendASCII("good.crx"), INSTALL_NEW
);
6028 const Extension
* extension
= service()->GetInstalledExtension(good_crx
);
6029 ASSERT_TRUE(extension
);
6031 syncer::FakeSyncChangeProcessor processor
;
6032 extension_sync_service()->MergeDataAndStartSyncing(
6034 syncer::SyncDataList(),
6035 scoped_ptr
<syncer::SyncChangeProcessor
>(
6036 new syncer::FakeSyncChangeProcessor
),
6037 scoped_ptr
<syncer::SyncErrorFactory
>(new syncer::SyncErrorFactoryMock()));
6040 syncer::SyncDataList list
=
6041 extension_sync_service()->GetAllSyncData(syncer::EXTENSIONS
);
6042 ASSERT_EQ(list
.size(), 1U);
6043 scoped_ptr
<ExtensionSyncData
> data
=
6044 ExtensionSyncData::CreateFromSyncData(list
[0]);
6045 ASSERT_TRUE(data
.get());
6046 EXPECT_TRUE(data
->enabled());
6047 EXPECT_FALSE(data
->incognito_enabled());
6048 EXPECT_EQ(ExtensionSyncData::BOOLEAN_UNSET
, data
->all_urls_enabled());
6051 service()->DisableExtension(good_crx
, Extension::DISABLE_USER_ACTION
);
6053 syncer::SyncDataList list
=
6054 extension_sync_service()->GetAllSyncData(syncer::EXTENSIONS
);
6055 ASSERT_EQ(list
.size(), 1U);
6056 scoped_ptr
<ExtensionSyncData
> data
=
6057 ExtensionSyncData::CreateFromSyncData(list
[0]);
6058 ASSERT_TRUE(data
.get());
6059 EXPECT_FALSE(data
->enabled());
6060 EXPECT_FALSE(data
->incognito_enabled());
6061 EXPECT_EQ(ExtensionSyncData::BOOLEAN_UNSET
, data
->all_urls_enabled());
6064 extensions::util::SetIsIncognitoEnabled(good_crx
, profile(), true);
6065 extensions::util::SetAllowedScriptingOnAllUrls(
6066 good_crx
, profile(), false);
6068 syncer::SyncDataList list
=
6069 extension_sync_service()->GetAllSyncData(syncer::EXTENSIONS
);
6070 ASSERT_EQ(list
.size(), 1U);
6071 scoped_ptr
<ExtensionSyncData
> data
=
6072 ExtensionSyncData::CreateFromSyncData(list
[0]);
6073 ASSERT_TRUE(data
.get());
6074 EXPECT_FALSE(data
->enabled());
6075 EXPECT_TRUE(data
->incognito_enabled());
6076 EXPECT_EQ(ExtensionSyncData::BOOLEAN_FALSE
, data
->all_urls_enabled());
6079 service()->EnableExtension(good_crx
);
6080 extensions::util::SetAllowedScriptingOnAllUrls(
6081 good_crx
, profile(), true);
6083 syncer::SyncDataList list
=
6084 extension_sync_service()->GetAllSyncData(syncer::EXTENSIONS
);
6085 ASSERT_EQ(list
.size(), 1U);
6086 scoped_ptr
<ExtensionSyncData
> data
=
6087 ExtensionSyncData::CreateFromSyncData(list
[0]);
6088 ASSERT_TRUE(data
.get());
6089 EXPECT_TRUE(data
->enabled());
6090 EXPECT_TRUE(data
->incognito_enabled());
6091 EXPECT_EQ(ExtensionSyncData::BOOLEAN_TRUE
, data
->all_urls_enabled());
6095 TEST_F(ExtensionServiceTest
, SyncForUninstalledExternalExtension
) {
6096 InitializeEmptyExtensionService();
6097 InitializeExtensionSyncService();
6098 InstallCRXWithLocation(
6099 data_dir().AppendASCII("good.crx"), Manifest::EXTERNAL_PREF
, INSTALL_NEW
);
6100 const Extension
* extension
= service()->GetInstalledExtension(good_crx
);
6101 ASSERT_TRUE(extension
);
6103 syncer::FakeSyncChangeProcessor processor
;
6104 extension_sync_service()->MergeDataAndStartSyncing(
6106 syncer::SyncDataList(),
6107 scoped_ptr
<syncer::SyncChangeProcessor
>(
6108 new syncer::FakeSyncChangeProcessor
),
6109 scoped_ptr
<syncer::SyncErrorFactory
>(new syncer::SyncErrorFactoryMock()));
6111 UninstallExtension(good_crx
, false);
6113 ExtensionPrefs::Get(profile())->IsExternalExtensionUninstalled(good_crx
));
6115 sync_pb::EntitySpecifics specifics
;
6116 sync_pb::AppSpecifics
* app_specifics
= specifics
.mutable_app();
6117 sync_pb::ExtensionSpecifics
* extension_specifics
=
6118 app_specifics
->mutable_extension();
6119 extension_specifics
->set_id(good_crx
);
6120 extension_specifics
->set_version("1.0");
6121 extension_specifics
->set_enabled(true);
6123 syncer::SyncData sync_data
=
6124 syncer::SyncData::CreateLocalData(good_crx
, "Name", specifics
);
6125 syncer::SyncChange
sync_change(FROM_HERE
,
6126 syncer::SyncChange::ACTION_UPDATE
,
6128 syncer::SyncChangeList
list(1);
6129 list
[0] = sync_change
;
6131 extension_sync_service()->ProcessSyncChanges(FROM_HERE
, list
);
6133 ExtensionPrefs::Get(profile())->IsExternalExtensionUninstalled(good_crx
));
6136 TEST_F(ExtensionServiceTest
, GetSyncAppDataUserSettings
) {
6137 InitializeEmptyExtensionService();
6138 InitializeExtensionSyncService();
6139 const Extension
* app
=
6140 PackAndInstallCRX(data_dir().AppendASCII("app"), INSTALL_NEW
);
6142 ASSERT_TRUE(app
->is_app());
6144 syncer::FakeSyncChangeProcessor processor
;
6145 extension_sync_service()->MergeDataAndStartSyncing(
6147 syncer::SyncDataList(),
6148 scoped_ptr
<syncer::SyncChangeProcessor
>(
6149 new syncer::FakeSyncChangeProcessor
),
6150 scoped_ptr
<syncer::SyncErrorFactory
>(new syncer::SyncErrorFactoryMock()));
6152 syncer::StringOrdinal initial_ordinal
=
6153 syncer::StringOrdinal::CreateInitialOrdinal();
6155 syncer::SyncDataList list
=
6156 extension_sync_service()->GetAllSyncData(syncer::APPS
);
6157 ASSERT_EQ(list
.size(), 1U);
6159 scoped_ptr
<AppSyncData
> app_sync_data
=
6160 AppSyncData::CreateFromSyncData(list
[0]);
6161 EXPECT_TRUE(initial_ordinal
.Equals(app_sync_data
->app_launch_ordinal()));
6162 EXPECT_TRUE(initial_ordinal
.Equals(app_sync_data
->page_ordinal()));
6165 AppSorting
* sorting
= ExtensionPrefs::Get(profile())->app_sorting();
6166 sorting
->SetAppLaunchOrdinal(app
->id(), initial_ordinal
.CreateAfter());
6168 syncer::SyncDataList list
=
6169 extension_sync_service()->GetAllSyncData(syncer::APPS
);
6170 ASSERT_EQ(list
.size(), 1U);
6172 scoped_ptr
<AppSyncData
> app_sync_data
=
6173 AppSyncData::CreateFromSyncData(list
[0]);
6174 ASSERT_TRUE(app_sync_data
.get());
6175 EXPECT_TRUE(initial_ordinal
.LessThan(app_sync_data
->app_launch_ordinal()));
6176 EXPECT_TRUE(initial_ordinal
.Equals(app_sync_data
->page_ordinal()));
6179 sorting
->SetPageOrdinal(app
->id(), initial_ordinal
.CreateAfter());
6181 syncer::SyncDataList list
=
6182 extension_sync_service()->GetAllSyncData(syncer::APPS
);
6183 ASSERT_EQ(list
.size(), 1U);
6185 scoped_ptr
<AppSyncData
> app_sync_data
=
6186 AppSyncData::CreateFromSyncData(list
[0]);
6187 ASSERT_TRUE(app_sync_data
.get());
6188 EXPECT_TRUE(initial_ordinal
.LessThan(app_sync_data
->app_launch_ordinal()));
6189 EXPECT_TRUE(initial_ordinal
.LessThan(app_sync_data
->page_ordinal()));
6193 // TODO (rdevlin.cronin): The OnExtensionMoved() method has been removed from
6194 // ExtensionService, so this test probably needs a new home. Unfortunately, it
6195 // relies pretty heavily on things like InitializeExtension[Sync]Service() and
6196 // PackAndInstallCRX(). When we clean up a bit more, this should move out.
6197 TEST_F(ExtensionServiceTest
, GetSyncAppDataUserSettingsOnExtensionMoved
) {
6198 InitializeEmptyExtensionService();
6199 InitializeExtensionSyncService();
6200 const size_t kAppCount
= 3;
6201 const Extension
* apps
[kAppCount
];
6202 apps
[0] = PackAndInstallCRX(data_dir().AppendASCII("app1"), INSTALL_NEW
);
6203 apps
[1] = PackAndInstallCRX(data_dir().AppendASCII("app2"), INSTALL_NEW
);
6204 apps
[2] = PackAndInstallCRX(data_dir().AppendASCII("app4"), INSTALL_NEW
);
6205 for (size_t i
= 0; i
< kAppCount
; ++i
) {
6206 ASSERT_TRUE(apps
[i
]);
6207 ASSERT_TRUE(apps
[i
]->is_app());
6210 syncer::FakeSyncChangeProcessor processor
;
6211 extension_sync_service()->MergeDataAndStartSyncing(
6213 syncer::SyncDataList(),
6214 scoped_ptr
<syncer::SyncChangeProcessor
>(
6215 new syncer::FakeSyncChangeProcessor
),
6216 scoped_ptr
<syncer::SyncErrorFactory
>(new syncer::SyncErrorFactoryMock()));
6218 ExtensionPrefs::Get(service()->GetBrowserContext())
6220 ->OnExtensionMoved(apps
[0]->id(), apps
[1]->id(), apps
[2]->id());
6222 syncer::SyncDataList list
=
6223 extension_sync_service()->GetAllSyncData(syncer::APPS
);
6224 ASSERT_EQ(list
.size(), 3U);
6226 scoped_ptr
<AppSyncData
> data
[kAppCount
];
6227 for (size_t i
= 0; i
< kAppCount
; ++i
) {
6228 data
[i
] = AppSyncData::CreateFromSyncData(list
[i
]);
6229 ASSERT_TRUE(data
[i
].get());
6232 // The sync data is not always in the same order our apps were installed in,
6233 // so we do that sorting here so we can make sure the values are changed as
6235 syncer::StringOrdinal app_launch_ordinals
[kAppCount
];
6236 for (size_t i
= 0; i
< kAppCount
; ++i
) {
6237 for (size_t j
= 0; j
< kAppCount
; ++j
) {
6238 if (apps
[i
]->id() == data
[j
]->id())
6239 app_launch_ordinals
[i
] = data
[j
]->app_launch_ordinal();
6243 EXPECT_TRUE(app_launch_ordinals
[1].LessThan(app_launch_ordinals
[0]));
6244 EXPECT_TRUE(app_launch_ordinals
[0].LessThan(app_launch_ordinals
[2]));
6248 TEST_F(ExtensionServiceTest
, GetSyncDataList
) {
6249 InitializeEmptyExtensionService();
6250 InitializeExtensionSyncService();
6251 InstallCRX(data_dir().AppendASCII("good.crx"), INSTALL_NEW
);
6252 InstallCRX(data_dir().AppendASCII("page_action.crx"), INSTALL_NEW
);
6253 InstallCRX(data_dir().AppendASCII("theme.crx"), INSTALL_NEW
);
6254 InstallCRX(data_dir().AppendASCII("theme2.crx"), INSTALL_NEW
);
6256 syncer::FakeSyncChangeProcessor processor
;
6257 extension_sync_service()->MergeDataAndStartSyncing(
6259 syncer::SyncDataList(),
6260 scoped_ptr
<syncer::SyncChangeProcessor
>(
6261 new syncer::FakeSyncChangeProcessor
),
6262 scoped_ptr
<syncer::SyncErrorFactory
>(new syncer::SyncErrorFactoryMock()));
6263 extension_sync_service()->MergeDataAndStartSyncing(
6265 syncer::SyncDataList(),
6266 scoped_ptr
<syncer::SyncChangeProcessor
>(
6267 new syncer::FakeSyncChangeProcessor
),
6268 scoped_ptr
<syncer::SyncErrorFactory
>(new syncer::SyncErrorFactoryMock()));
6270 service()->DisableExtension(page_action
, Extension::DISABLE_USER_ACTION
);
6271 TerminateExtension(theme2_crx
);
6273 EXPECT_EQ(0u, extension_sync_service()->GetAllSyncData(syncer::APPS
).size());
6275 2u, extension_sync_service()->GetAllSyncData(syncer::EXTENSIONS
).size());
6278 TEST_F(ExtensionServiceTest
, ProcessSyncDataUninstall
) {
6279 InitializeEmptyExtensionService();
6280 InitializeExtensionSyncService();
6281 syncer::FakeSyncChangeProcessor processor
;
6282 extension_sync_service()->MergeDataAndStartSyncing(
6284 syncer::SyncDataList(),
6285 scoped_ptr
<syncer::SyncChangeProcessor
>(
6286 new syncer::FakeSyncChangeProcessor
),
6287 scoped_ptr
<syncer::SyncErrorFactory
>(new syncer::SyncErrorFactoryMock()));
6289 sync_pb::EntitySpecifics specifics
;
6290 sync_pb::ExtensionSpecifics
* ext_specifics
= specifics
.mutable_extension();
6291 ext_specifics
->set_id(good_crx
);
6292 ext_specifics
->set_version("1.0");
6293 syncer::SyncData sync_data
=
6294 syncer::SyncData::CreateLocalData(good_crx
, "Name", specifics
);
6295 syncer::SyncChange
sync_change(FROM_HERE
,
6296 syncer::SyncChange::ACTION_DELETE
,
6298 syncer::SyncChangeList
list(1);
6299 list
[0] = sync_change
;
6301 // Should do nothing.
6302 extension_sync_service()->ProcessSyncChanges(FROM_HERE
, list
);
6303 EXPECT_FALSE(service()->GetExtensionById(good_crx
, true));
6305 // Install the extension.
6306 base::FilePath extension_path
= data_dir().AppendASCII("good.crx");
6307 InstallCRX(extension_path
, INSTALL_NEW
);
6308 EXPECT_TRUE(service()->GetExtensionById(good_crx
, true));
6310 // Should uninstall the extension.
6311 extension_sync_service()->ProcessSyncChanges(FROM_HERE
, list
);
6312 EXPECT_FALSE(service()->GetExtensionById(good_crx
, true));
6314 // Should again do nothing.
6315 extension_sync_service()->ProcessSyncChanges(FROM_HERE
, list
);
6316 EXPECT_FALSE(service()->GetExtensionById(good_crx
, true));
6319 TEST_F(ExtensionServiceTest
, ProcessSyncDataWrongType
) {
6320 InitializeEmptyExtensionService();
6321 InitializeExtensionSyncService();
6323 // Install the extension.
6324 base::FilePath extension_path
= data_dir().AppendASCII("good.crx");
6325 InstallCRX(extension_path
, INSTALL_NEW
);
6326 EXPECT_TRUE(service()->GetExtensionById(good_crx
, true));
6328 sync_pb::EntitySpecifics specifics
;
6329 sync_pb::AppSpecifics
* app_specifics
= specifics
.mutable_app();
6330 sync_pb::ExtensionSpecifics
* extension_specifics
=
6331 app_specifics
->mutable_extension();
6332 extension_specifics
->set_id(good_crx
);
6333 extension_specifics
->set_version(
6334 service()->GetInstalledExtension(good_crx
)->version()->GetString());
6337 extension_specifics
->set_enabled(true);
6338 syncer::SyncData sync_data
=
6339 syncer::SyncData::CreateLocalData(good_crx
, "Name", specifics
);
6340 syncer::SyncChange
sync_change(FROM_HERE
,
6341 syncer::SyncChange::ACTION_DELETE
,
6343 syncer::SyncChangeList
list(1);
6344 list
[0] = sync_change
;
6346 // Should do nothing
6347 extension_sync_service()->ProcessSyncChanges(FROM_HERE
, list
);
6348 EXPECT_TRUE(service()->GetExtensionById(good_crx
, true));
6352 extension_specifics
->set_enabled(false);
6353 syncer::SyncData sync_data
=
6354 syncer::SyncData::CreateLocalData(good_crx
, "Name", specifics
);
6355 syncer::SyncChange
sync_change(FROM_HERE
,
6356 syncer::SyncChange::ACTION_UPDATE
,
6358 syncer::SyncChangeList
list(1);
6359 list
[0] = sync_change
;
6361 // Should again do nothing.
6362 extension_sync_service()->ProcessSyncChanges(FROM_HERE
, list
);
6363 EXPECT_TRUE(service()->GetExtensionById(good_crx
, false));
6367 TEST_F(ExtensionServiceTest
, ProcessSyncDataSettings
) {
6368 InitializeEmptyExtensionService();
6369 InitializeExtensionSyncService();
6370 syncer::FakeSyncChangeProcessor processor
;
6371 extension_sync_service()->MergeDataAndStartSyncing(
6373 syncer::SyncDataList(),
6374 scoped_ptr
<syncer::SyncChangeProcessor
>(
6375 new syncer::FakeSyncChangeProcessor
),
6376 scoped_ptr
<syncer::SyncErrorFactory
>(new syncer::SyncErrorFactoryMock()));
6378 InstallCRX(data_dir().AppendASCII("good.crx"), INSTALL_NEW
);
6379 EXPECT_TRUE(service()->IsExtensionEnabled(good_crx
));
6380 EXPECT_FALSE(extensions::util::IsIncognitoEnabled(good_crx
, profile()));
6381 EXPECT_FALSE(extensions::util::HasSetAllowedScriptingOnAllUrls(
6382 good_crx
, profile()));
6383 const bool kDefaultAllowedScripting
=
6384 extensions::util::DefaultAllowedScriptingOnAllUrls();
6385 EXPECT_EQ(kDefaultAllowedScripting
,
6386 extensions::util::AllowedScriptingOnAllUrls(good_crx
, profile()));
6388 sync_pb::EntitySpecifics specifics
;
6389 sync_pb::ExtensionSpecifics
* ext_specifics
= specifics
.mutable_extension();
6390 ext_specifics
->set_id(good_crx
);
6391 ext_specifics
->set_version(
6392 service()->GetInstalledExtension(good_crx
)->version()->GetString());
6393 ext_specifics
->set_enabled(false);
6396 syncer::SyncData sync_data
=
6397 syncer::SyncData::CreateLocalData(good_crx
, "Name", specifics
);
6398 syncer::SyncChange
sync_change(FROM_HERE
,
6399 syncer::SyncChange::ACTION_UPDATE
,
6401 syncer::SyncChangeList
list(1);
6402 list
[0] = sync_change
;
6403 extension_sync_service()->ProcessSyncChanges(FROM_HERE
, list
);
6404 EXPECT_FALSE(service()->IsExtensionEnabled(good_crx
));
6405 EXPECT_FALSE(extensions::util::IsIncognitoEnabled(good_crx
, profile()));
6406 EXPECT_FALSE(extensions::util::HasSetAllowedScriptingOnAllUrls(
6407 good_crx
, profile()));
6408 EXPECT_EQ(kDefaultAllowedScripting
,
6409 extensions::util::AllowedScriptingOnAllUrls(good_crx
, profile()));
6413 ext_specifics
->set_enabled(true);
6414 ext_specifics
->set_incognito_enabled(true);
6415 syncer::SyncData sync_data
=
6416 syncer::SyncData::CreateLocalData(good_crx
, "Name", specifics
);
6417 syncer::SyncChange
sync_change(FROM_HERE
,
6418 syncer::SyncChange::ACTION_UPDATE
,
6420 syncer::SyncChangeList
list(1);
6421 list
[0] = sync_change
;
6422 extension_sync_service()->ProcessSyncChanges(FROM_HERE
, list
);
6423 EXPECT_TRUE(service()->IsExtensionEnabled(good_crx
));
6424 EXPECT_TRUE(extensions::util::IsIncognitoEnabled(good_crx
, profile()));
6428 ext_specifics
->set_enabled(false);
6429 ext_specifics
->set_incognito_enabled(true);
6430 syncer::SyncData sync_data
=
6431 syncer::SyncData::CreateLocalData(good_crx
, "Name", specifics
);
6432 syncer::SyncChange
sync_change(FROM_HERE
,
6433 syncer::SyncChange::ACTION_UPDATE
,
6435 syncer::SyncChangeList
list(1);
6436 list
[0] = sync_change
;
6437 extension_sync_service()->ProcessSyncChanges(FROM_HERE
, list
);
6438 EXPECT_FALSE(service()->IsExtensionEnabled(good_crx
));
6439 EXPECT_TRUE(extensions::util::IsIncognitoEnabled(good_crx
, profile()));
6443 ext_specifics
->set_enabled(true);
6444 ext_specifics
->set_all_urls_enabled(!kDefaultAllowedScripting
);
6445 syncer::SyncData sync_data
=
6446 syncer::SyncData::CreateLocalData(good_crx
, "Name", specifics
);
6447 syncer::SyncChange
sync_change(FROM_HERE
,
6448 syncer::SyncChange::ACTION_UPDATE
,
6450 syncer::SyncChangeList
list(1);
6451 list
[0] = sync_change
;
6452 extension_sync_service()->ProcessSyncChanges(FROM_HERE
, list
);
6453 EXPECT_TRUE(service()->IsExtensionEnabled(good_crx
));
6454 EXPECT_TRUE(extensions::util::HasSetAllowedScriptingOnAllUrls(
6455 good_crx
, profile()));
6456 EXPECT_EQ(!kDefaultAllowedScripting
,
6457 extensions::util::AllowedScriptingOnAllUrls(good_crx
, profile()));
6461 ext_specifics
->set_all_urls_enabled(kDefaultAllowedScripting
);
6462 syncer::SyncData sync_data
=
6463 syncer::SyncData::CreateLocalData(good_crx
, "Name", specifics
);
6464 syncer::SyncChange
sync_change(FROM_HERE
,
6465 syncer::SyncChange::ACTION_UPDATE
,
6467 syncer::SyncChangeList
list(1);
6468 list
[0] = sync_change
;
6469 extension_sync_service()->ProcessSyncChanges(FROM_HERE
, list
);
6470 EXPECT_TRUE(service()->IsExtensionEnabled(good_crx
));
6471 EXPECT_TRUE(extensions::util::HasSetAllowedScriptingOnAllUrls(
6472 good_crx
, profile()));
6473 EXPECT_EQ(kDefaultAllowedScripting
,
6474 extensions::util::AllowedScriptingOnAllUrls(good_crx
, profile()));
6477 EXPECT_FALSE(service()->pending_extension_manager()->IsIdPending(good_crx
));
6480 TEST_F(ExtensionServiceTest
, ProcessSyncDataNewExtension
) {
6481 InitializeEmptyExtensionService();
6482 InitializeExtensionSyncService();
6483 syncer::FakeSyncChangeProcessor processor
;
6484 extension_sync_service()->MergeDataAndStartSyncing(
6486 syncer::SyncDataList(),
6487 scoped_ptr
<syncer::SyncChangeProcessor
>(
6488 new syncer::FakeSyncChangeProcessor
),
6489 scoped_ptr
<syncer::SyncErrorFactory
>(new syncer::SyncErrorFactoryMock()));
6491 sync_pb::EntitySpecifics specifics
;
6492 sync_pb::ExtensionSpecifics
* ext_specifics
= specifics
.mutable_extension();
6493 ext_specifics
->set_id(good_crx
);
6494 ext_specifics
->set_version(base::Version("1").GetString());
6496 const base::FilePath path
= data_dir().AppendASCII("good.crx");
6497 const ExtensionPrefs
* prefs
= ExtensionPrefs::Get(profile());
6500 const char* name
; // For failure output only.
6501 bool sync_enabled
; // The "enabled" flag coming in from Sync.
6502 int sync_disable_reasons
; // The disable reason(s) coming in from Sync.
6503 // The disable reason(s) that should be set on the installed extension.
6504 // This will usually be the same as |sync_disable_reasons|, but see the
6506 int expect_disable_reasons
;
6507 // Whether the extension's permissions should be auto-granted during
6509 bool expect_permissions_granted
;
6511 // Standard case: Extension comes in enabled; permissions should be granted
6512 // during installation.
6513 { "Standard", true, 0, 0, true },
6514 // If the extension comes in disabled, its permissions should still be
6515 // granted (the user already approved them on another machine).
6516 { "Disabled", false, Extension::DISABLE_USER_ACTION
,
6517 Extension::DISABLE_USER_ACTION
, true },
6518 // Legacy case (<M45): No disable reasons come in from Sync (see
6519 // crbug.com/484214). After installation, the reason should be set to
6520 // DISABLE_UNKNOWN_FROM_SYNC.
6521 { "Legacy", false, 0, Extension::DISABLE_UNKNOWN_FROM_SYNC
, true },
6522 // If the extension came in disabled due to a permissions increase, then the
6523 // user has *not* approved the permissions, and they shouldn't be granted.
6525 { "PermissionsIncrease", false, Extension::DISABLE_PERMISSIONS_INCREASE
,
6526 Extension::DISABLE_PERMISSIONS_INCREASE
, false },
6529 for (const TestCase
& test_case
: test_cases
) {
6530 SCOPED_TRACE(test_case
.name
);
6532 ext_specifics
->set_enabled(test_case
.sync_enabled
);
6533 ext_specifics
->set_disable_reasons(test_case
.sync_disable_reasons
);
6535 syncer::SyncData sync_data
=
6536 syncer::SyncData::CreateLocalData(good_crx
, "Name", specifics
);
6537 syncer::SyncChange
sync_change(FROM_HERE
,
6538 syncer::SyncChange::ACTION_UPDATE
,
6540 syncer::SyncChangeList
list(1);
6541 list
[0] = sync_change
;
6542 extension_sync_service()->ProcessSyncChanges(FROM_HERE
, list
);
6544 ASSERT_TRUE(service()->pending_extension_manager()->IsIdPending(good_crx
));
6545 UpdateExtension(good_crx
, path
, test_case
.sync_enabled
? ENABLED
6547 EXPECT_EQ(test_case
.expect_disable_reasons
,
6548 prefs
->GetDisableReasons(good_crx
));
6549 scoped_refptr
<PermissionSet
> permissions(
6550 prefs
->GetGrantedPermissions(good_crx
));
6551 EXPECT_EQ(test_case
.expect_permissions_granted
, !permissions
->IsEmpty());
6552 ASSERT_FALSE(service()->pending_extension_manager()->IsIdPending(good_crx
));
6554 // Remove the extension again, so we can install it again for the next case.
6555 UninstallExtension(good_crx
, false,
6556 test_case
.sync_enabled
? Extension::ENABLED
6557 : Extension::DISABLED
);
6561 TEST_F(ExtensionServiceTest
, ProcessSyncDataTerminatedExtension
) {
6562 InitializeExtensionServiceWithUpdater();
6563 InitializeExtensionSyncService();
6564 syncer::FakeSyncChangeProcessor processor
;
6565 extension_sync_service()->MergeDataAndStartSyncing(
6567 syncer::SyncDataList(),
6568 scoped_ptr
<syncer::SyncChangeProcessor
>(
6569 new syncer::FakeSyncChangeProcessor
),
6570 scoped_ptr
<syncer::SyncErrorFactory
>(new syncer::SyncErrorFactoryMock()));
6572 InstallCRX(data_dir().AppendASCII("good.crx"), INSTALL_NEW
);
6573 TerminateExtension(good_crx
);
6574 EXPECT_TRUE(service()->IsExtensionEnabled(good_crx
));
6575 EXPECT_FALSE(extensions::util::IsIncognitoEnabled(good_crx
, profile()));
6577 sync_pb::EntitySpecifics specifics
;
6578 sync_pb::ExtensionSpecifics
* ext_specifics
= specifics
.mutable_extension();
6579 ext_specifics
->set_id(good_crx
);
6580 ext_specifics
->set_version(
6581 service()->GetInstalledExtension(good_crx
)->version()->GetString());
6582 ext_specifics
->set_enabled(false);
6583 ext_specifics
->set_incognito_enabled(true);
6584 syncer::SyncData sync_data
=
6585 syncer::SyncData::CreateLocalData(good_crx
, "Name", specifics
);
6586 syncer::SyncChange
sync_change(FROM_HERE
,
6587 syncer::SyncChange::ACTION_UPDATE
,
6589 syncer::SyncChangeList
list(1);
6590 list
[0] = sync_change
;
6592 extension_sync_service()->ProcessSyncChanges(FROM_HERE
, list
);
6593 EXPECT_FALSE(service()->IsExtensionEnabled(good_crx
));
6594 EXPECT_TRUE(extensions::util::IsIncognitoEnabled(good_crx
, profile()));
6596 EXPECT_FALSE(service()->pending_extension_manager()->IsIdPending(good_crx
));
6599 TEST_F(ExtensionServiceTest
, ProcessSyncDataVersionCheck
) {
6600 InitializeExtensionServiceWithUpdater();
6601 InitializeExtensionSyncService();
6602 syncer::FakeSyncChangeProcessor processor
;
6603 extension_sync_service()->MergeDataAndStartSyncing(
6605 syncer::SyncDataList(),
6606 scoped_ptr
<syncer::SyncChangeProcessor
>(
6607 new syncer::FakeSyncChangeProcessor
),
6608 scoped_ptr
<syncer::SyncErrorFactory
>(new syncer::SyncErrorFactoryMock()));
6610 InstallCRX(data_dir().AppendASCII("good.crx"), INSTALL_NEW
);
6611 EXPECT_TRUE(service()->IsExtensionEnabled(good_crx
));
6612 EXPECT_FALSE(extensions::util::IsIncognitoEnabled(good_crx
, profile()));
6614 sync_pb::EntitySpecifics specifics
;
6615 sync_pb::ExtensionSpecifics
* ext_specifics
= specifics
.mutable_extension();
6616 ext_specifics
->set_id(good_crx
);
6617 ext_specifics
->set_enabled(true);
6620 ext_specifics
->set_version(
6621 service()->GetInstalledExtension(good_crx
)->version()->GetString());
6622 syncer::SyncData sync_data
=
6623 syncer::SyncData::CreateLocalData(good_crx
, "Name", specifics
);
6624 syncer::SyncChange
sync_change(FROM_HERE
,
6625 syncer::SyncChange::ACTION_UPDATE
,
6627 syncer::SyncChangeList
list(1);
6628 list
[0] = sync_change
;
6630 // Should do nothing if extension version == sync version.
6631 extension_sync_service()->ProcessSyncChanges(FROM_HERE
, list
);
6632 EXPECT_FALSE(service()->updater()->WillCheckSoon());
6635 // Should do nothing if extension version > sync version (but see
6636 // the TODO in ProcessExtensionSyncData).
6638 ext_specifics
->set_version("0.0.0.0");
6639 syncer::SyncData sync_data
=
6640 syncer::SyncData::CreateLocalData(good_crx
, "Name", specifics
);
6641 syncer::SyncChange
sync_change(FROM_HERE
,
6642 syncer::SyncChange::ACTION_UPDATE
,
6644 syncer::SyncChangeList
list(1);
6645 list
[0] = sync_change
;
6647 extension_sync_service()->ProcessSyncChanges(FROM_HERE
, list
);
6648 EXPECT_FALSE(service()->updater()->WillCheckSoon());
6651 // Should kick off an update if extension version < sync version.
6653 ext_specifics
->set_version("9.9.9.9");
6654 syncer::SyncData sync_data
=
6655 syncer::SyncData::CreateLocalData(good_crx
, "Name", specifics
);
6656 syncer::SyncChange
sync_change(FROM_HERE
,
6657 syncer::SyncChange::ACTION_UPDATE
,
6659 syncer::SyncChangeList
list(1);
6660 list
[0] = sync_change
;
6662 extension_sync_service()->ProcessSyncChanges(FROM_HERE
, list
);
6663 EXPECT_TRUE(service()->updater()->WillCheckSoon());
6666 EXPECT_FALSE(service()->pending_extension_manager()->IsIdPending(good_crx
));
6669 TEST_F(ExtensionServiceTest
, ProcessSyncDataNotInstalled
) {
6670 InitializeExtensionServiceWithUpdater();
6671 InitializeExtensionSyncService();
6672 syncer::FakeSyncChangeProcessor processor
;
6673 extension_sync_service()->MergeDataAndStartSyncing(
6675 syncer::SyncDataList(),
6676 scoped_ptr
<syncer::SyncChangeProcessor
>(
6677 new syncer::FakeSyncChangeProcessor
),
6678 scoped_ptr
<syncer::SyncErrorFactory
>(new syncer::SyncErrorFactoryMock()));
6680 sync_pb::EntitySpecifics specifics
;
6681 sync_pb::ExtensionSpecifics
* ext_specifics
= specifics
.mutable_extension();
6682 ext_specifics
->set_id(good_crx
);
6683 ext_specifics
->set_enabled(false);
6684 ext_specifics
->set_incognito_enabled(true);
6685 ext_specifics
->set_update_url("http://www.google.com/");
6686 ext_specifics
->set_version("1.2.3.4");
6687 syncer::SyncData sync_data
=
6688 syncer::SyncData::CreateLocalData(good_crx
, "Name", specifics
);
6689 syncer::SyncChange
sync_change(FROM_HERE
,
6690 syncer::SyncChange::ACTION_UPDATE
,
6692 syncer::SyncChangeList
list(1);
6693 list
[0] = sync_change
;
6695 EXPECT_TRUE(service()->IsExtensionEnabled(good_crx
));
6696 EXPECT_FALSE(extensions::util::IsIncognitoEnabled(good_crx
, profile()));
6697 extension_sync_service()->ProcessSyncChanges(FROM_HERE
, list
);
6698 EXPECT_TRUE(service()->updater()->WillCheckSoon());
6699 EXPECT_FALSE(service()->IsExtensionEnabled(good_crx
));
6700 EXPECT_TRUE(extensions::util::IsIncognitoEnabled(good_crx
, profile()));
6702 const extensions::PendingExtensionInfo
* info
;
6704 (info
= service()->pending_extension_manager()->GetById(good_crx
)));
6705 EXPECT_EQ(ext_specifics
->update_url(), info
->update_url().spec());
6706 EXPECT_TRUE(info
->is_from_sync());
6707 EXPECT_EQ(Manifest::INTERNAL
, info
->install_source());
6708 // TODO(akalin): Figure out a way to test |info.ShouldAllowInstall()|.
6711 #if defined(ENABLE_SUPERVISED_USERS)
6712 class ScopedSupervisedUserServiceDelegate
6713 : public SupervisedUserService::Delegate
{
6715 explicit ScopedSupervisedUserServiceDelegate(SupervisedUserService
* service
)
6716 : service_(service
) {
6717 service_
->SetDelegate(this);
6719 ~ScopedSupervisedUserServiceDelegate() override
{
6720 service_
->SetDelegate(nullptr);
6723 // This prevents the legacy supervised user init code from running.
6724 bool SetActive(bool active
) override
{ return true; }
6727 SupervisedUserService
* service_
;
6730 class MockPermissionRequestCreator
: public PermissionRequestCreator
{
6732 MockPermissionRequestCreator() {}
6733 ~MockPermissionRequestCreator() override
{}
6735 bool IsEnabled() const override
{ return true; }
6737 void CreateURLAccessRequest(const GURL
& url_requested
,
6738 const SuccessCallback
& callback
) override
{
6742 MOCK_METHOD2(CreateExtensionUpdateRequest
,
6743 void(const std::string
& id
,
6744 const SupervisedUserService::SuccessCallback
& callback
));
6747 DISALLOW_COPY_AND_ASSIGN(MockPermissionRequestCreator
);
6750 TEST_F(ExtensionServiceTest
, SupervisedUser_InstallOnlyAllowedByCustodian
) {
6751 ExtensionServiceInitParams params
= CreateDefaultInitParams();
6752 params
.profile_is_supervised
= true;
6753 InitializeExtensionService(params
);
6755 SupervisedUserService
* supervised_user_service
=
6756 SupervisedUserServiceFactory::GetForProfile(profile());
6757 ScopedSupervisedUserServiceDelegate
delegate(supervised_user_service
);
6758 supervised_user_service
->Init();
6760 base::FilePath path1
= data_dir().AppendASCII("good.crx");
6761 base::FilePath path2
= data_dir().AppendASCII("good2048.crx");
6762 const Extension
* extensions
[] = {
6763 InstallCRX(path1
, INSTALL_FAILED
),
6764 InstallCRX(path2
, INSTALL_NEW
, Extension::WAS_INSTALLED_BY_CUSTODIAN
)
6767 // Only the extension with the "installed by custodian" flag should have been
6768 // installed and enabled.
6769 EXPECT_FALSE(extensions
[0]);
6770 ASSERT_TRUE(extensions
[1]);
6771 EXPECT_TRUE(registry()->enabled_extensions().Contains(extensions
[1]->id()));
6774 TEST_F(ExtensionServiceTest
, SupervisedUser_PreinstalledExtension
) {
6775 ExtensionServiceInitParams params
= CreateDefaultInitParams();
6776 // Do *not* set the profile to supervised here!
6777 InitializeExtensionService(params
);
6779 SupervisedUserService
* supervised_user_service
=
6780 SupervisedUserServiceFactory::GetForProfile(profile());
6781 ScopedSupervisedUserServiceDelegate
delegate(supervised_user_service
);
6782 supervised_user_service
->Init();
6784 // Install an extension.
6785 base::FilePath path
= data_dir().AppendASCII("good.crx");
6786 const Extension
* extension
= InstallCRX(path
, INSTALL_NEW
);
6787 std::string id
= extension
->id();
6789 // Now make the profile supervised.
6790 profile()->AsTestingProfile()->SetSupervisedUserId(
6791 supervised_users::kChildAccountSUID
);
6793 // The extension should not be enabled anymore.
6794 EXPECT_FALSE(registry()->enabled_extensions().Contains(id
));
6797 TEST_F(ExtensionServiceTest
, SupervisedUser_UpdateWithoutPermissionIncrease
) {
6798 ExtensionServiceInitParams params
= CreateDefaultInitParams();
6799 params
.profile_is_supervised
= true;
6800 InitializeExtensionService(params
);
6802 SupervisedUserService
* supervised_user_service
=
6803 SupervisedUserServiceFactory::GetForProfile(profile());
6804 ScopedSupervisedUserServiceDelegate
delegate(supervised_user_service
);
6805 supervised_user_service
->Init();
6807 base::FilePath base_path
= data_dir().AppendASCII("autoupdate");
6808 base::FilePath pem_path
= base_path
.AppendASCII("key.pem");
6810 base::FilePath path
= base_path
.AppendASCII("v1");
6811 const Extension
* extension
=
6812 PackAndInstallCRX(path
, pem_path
, INSTALL_NEW
,
6813 Extension::WAS_INSTALLED_BY_CUSTODIAN
);
6814 // The extension must now be installed and enabled.
6815 ASSERT_TRUE(extension
);
6816 ASSERT_TRUE(registry()->enabled_extensions().Contains(extension
->id()));
6818 // Save the id, as the extension object will be destroyed during updating.
6819 std::string id
= extension
->id();
6821 std::string old_version
= extension
->VersionString();
6823 // Update to a new version.
6824 path
= base_path
.AppendASCII("v2");
6825 PackCRXAndUpdateExtension(id
, path
, pem_path
, ENABLED
);
6827 // The extension should still be there and enabled.
6828 extension
= registry()->enabled_extensions().GetByID(id
);
6829 ASSERT_TRUE(extension
);
6830 // The version should have changed.
6831 EXPECT_NE(extension
->VersionString(), old_version
);
6834 TEST_F(ExtensionServiceTest
, SupervisedUser_UpdateWithPermissionIncrease
) {
6835 ExtensionServiceInitParams params
= CreateDefaultInitParams();
6836 params
.profile_is_supervised
= true;
6837 InitializeExtensionService(params
);
6839 SupervisedUserService
* supervised_user_service
=
6840 SupervisedUserServiceFactory::GetForProfile(profile());
6841 ScopedSupervisedUserServiceDelegate
delegate(supervised_user_service
);
6842 supervised_user_service
->Init();
6843 MockPermissionRequestCreator
* creator
= new MockPermissionRequestCreator
;
6844 supervised_user_service
->AddPermissionRequestCreator(
6845 make_scoped_ptr(creator
));
6847 base::FilePath base_path
= data_dir().AppendASCII("permissions_increase");
6848 base::FilePath pem_path
= base_path
.AppendASCII("permissions.pem");
6850 base::FilePath path
= base_path
.AppendASCII("v1");
6851 const Extension
* extension
=
6852 PackAndInstallCRX(path
, pem_path
, INSTALL_NEW
,
6853 Extension::WAS_INSTALLED_BY_CUSTODIAN
);
6854 // The extension must now be installed and enabled.
6855 ASSERT_TRUE(extension
);
6856 ASSERT_TRUE(registry()->enabled_extensions().Contains(extension
->id()));
6858 // Save the id, as the extension object will be destroyed during updating.
6859 std::string id
= extension
->id();
6861 std::string old_version
= extension
->VersionString();
6863 // Update to a new version with increased permissions.
6864 EXPECT_CALL(*creator
,
6865 CreateExtensionUpdateRequest(id
+ ":2", testing::_
));
6866 path
= base_path
.AppendASCII("v2");
6867 PackCRXAndUpdateExtension(id
, path
, pem_path
, DISABLED
);
6869 // The extension should still be there, but disabled.
6870 EXPECT_FALSE(registry()->enabled_extensions().Contains(id
));
6871 extension
= registry()->disabled_extensions().GetByID(id
);
6872 ASSERT_TRUE(extension
);
6873 // The version should have changed.
6874 EXPECT_NE(extension
->VersionString(), old_version
);
6877 TEST_F(ExtensionServiceTest
,
6878 SupervisedUser_SyncUninstallByCustodianSkipsPolicy
) {
6879 InitializeEmptyExtensionService();
6880 InitializeExtensionSyncService();
6881 extension_sync_service()->MergeDataAndStartSyncing(
6883 syncer::SyncDataList(),
6884 scoped_ptr
<syncer::SyncChangeProcessor
>(
6885 new syncer::FakeSyncChangeProcessor
),
6886 scoped_ptr
<syncer::SyncErrorFactory
>(new syncer::SyncErrorFactoryMock()));
6888 // Install two extensions.
6889 base::FilePath path1
= data_dir().AppendASCII("good.crx");
6890 base::FilePath path2
= data_dir().AppendASCII("good2048.crx");
6891 const Extension
* extensions
[] = {
6892 InstallCRX(path1
, INSTALL_NEW
),
6893 InstallCRX(path2
, INSTALL_NEW
, Extension::WAS_INSTALLED_BY_CUSTODIAN
)
6896 // Add a policy provider that will disallow any changes.
6897 extensions::TestManagementPolicyProvider
provider(
6898 extensions::TestManagementPolicyProvider::PROHIBIT_MODIFY_STATUS
);
6899 GetManagementPolicy()->RegisterProvider(&provider
);
6901 // Create a sync deletion for each extension.
6902 syncer::SyncChangeList change_list
;
6903 for (size_t i
= 0; i
< arraysize(extensions
); i
++) {
6904 const std::string
& id
= extensions
[i
]->id();
6905 sync_pb::EntitySpecifics specifics
;
6906 sync_pb::ExtensionSpecifics
* ext_specifics
= specifics
.mutable_extension();
6907 ext_specifics
->set_id(id
);
6908 ext_specifics
->set_version("1.0");
6909 ext_specifics
->set_installed_by_custodian(
6910 extensions
[i
]->was_installed_by_custodian());
6911 syncer::SyncData sync_data
=
6912 syncer::SyncData::CreateLocalData(id
, "Name", specifics
);
6913 change_list
.push_back(syncer::SyncChange(FROM_HERE
,
6914 syncer::SyncChange::ACTION_DELETE
,
6918 // Save the extension ids, as uninstalling destroys the Extension instance.
6919 std::string extension_ids
[] = {
6920 extensions
[0]->id(),
6924 // Now apply the uninstallations.
6925 extension_sync_service()->ProcessSyncChanges(FROM_HERE
, change_list
);
6927 // Uninstalling the extension without installed_by_custodian should have been
6928 // blocked by policy, so it should still be there.
6929 EXPECT_TRUE(registry()->enabled_extensions().Contains(extension_ids
[0]));
6931 // But installed_by_custodian should result in bypassing the policy check.
6933 registry()->GenerateInstalledExtensionsSet()->Contains(extension_ids
[1]));
6935 #endif // defined(ENABLE_SUPERVISED_USERS)
6937 TEST_F(ExtensionServiceTest
, InstallPriorityExternalUpdateUrl
) {
6938 InitializeEmptyExtensionService();
6940 base::FilePath path
= data_dir().AppendASCII("good.crx");
6941 InstallCRX(path
, INSTALL_NEW
);
6942 ValidatePrefKeyCount(1u);
6943 ValidateIntegerPref(good_crx
, "state", Extension::ENABLED
);
6944 ValidateIntegerPref(good_crx
, "location", Manifest::INTERNAL
);
6946 extensions::PendingExtensionManager
* pending
=
6947 service()->pending_extension_manager();
6948 EXPECT_FALSE(pending
->IsIdPending(kGoodId
));
6950 // Skip install when the location is the same.
6952 service()->OnExternalExtensionUpdateUrlFound(kGoodId
,
6954 GURL(kGoodUpdateURL
),
6956 Extension::NO_FLAGS
,
6958 EXPECT_FALSE(pending
->IsIdPending(kGoodId
));
6960 // Install when the location has higher priority.
6961 EXPECT_TRUE(service()->OnExternalExtensionUpdateUrlFound(
6964 GURL(kGoodUpdateURL
),
6965 Manifest::EXTERNAL_POLICY_DOWNLOAD
,
6966 Extension::NO_FLAGS
,
6968 EXPECT_TRUE(pending
->IsIdPending(kGoodId
));
6970 // Try the low priority again. Should be rejected.
6971 EXPECT_FALSE(service()->OnExternalExtensionUpdateUrlFound(
6974 GURL(kGoodUpdateURL
),
6975 Manifest::EXTERNAL_PREF_DOWNLOAD
,
6976 Extension::NO_FLAGS
,
6978 // The existing record should still be present in the pending extension
6980 EXPECT_TRUE(pending
->IsIdPending(kGoodId
));
6982 pending
->Remove(kGoodId
);
6984 // Skip install when the location has the same priority as the installed
6987 service()->OnExternalExtensionUpdateUrlFound(kGoodId
,
6989 GURL(kGoodUpdateURL
),
6991 Extension::NO_FLAGS
,
6994 EXPECT_FALSE(pending
->IsIdPending(kGoodId
));
6997 TEST_F(ExtensionServiceTest
, InstallPriorityExternalLocalFile
) {
6998 Version
older_version("0.1.0.0");
6999 Version
newer_version("2.0.0.0");
7001 // We don't want the extension to be installed. A path that doesn't
7002 // point to a valid CRX ensures this.
7003 const base::FilePath
kInvalidPathToCrx(FILE_PATH_LITERAL("invalid_path"));
7005 const int kCreationFlags
= 0;
7006 const bool kDontMarkAcknowledged
= false;
7007 const bool kDontInstallImmediately
= false;
7009 InitializeEmptyExtensionService();
7011 // The test below uses install source constants to test that
7012 // priority is enforced. It assumes a specific ranking of install
7013 // sources: Registry (EXTERNAL_REGISTRY) overrides external pref
7014 // (EXTERNAL_PREF), and external pref overrides user install (INTERNAL).
7015 // The following assertions verify these assumptions:
7016 ASSERT_EQ(Manifest::EXTERNAL_REGISTRY
,
7017 Manifest::GetHigherPriorityLocation(Manifest::EXTERNAL_REGISTRY
,
7018 Manifest::EXTERNAL_PREF
));
7019 ASSERT_EQ(Manifest::EXTERNAL_REGISTRY
,
7020 Manifest::GetHigherPriorityLocation(Manifest::EXTERNAL_REGISTRY
,
7021 Manifest::INTERNAL
));
7022 ASSERT_EQ(Manifest::EXTERNAL_PREF
,
7023 Manifest::GetHigherPriorityLocation(Manifest::EXTERNAL_PREF
,
7024 Manifest::INTERNAL
));
7026 extensions::PendingExtensionManager
* pending
=
7027 service()->pending_extension_manager();
7028 EXPECT_FALSE(pending
->IsIdPending(kGoodId
));
7031 // Simulate an external source adding the extension as INTERNAL.
7032 content::WindowedNotificationObserver
observer(
7033 extensions::NOTIFICATION_CRX_INSTALLER_DONE
,
7034 content::NotificationService::AllSources());
7035 EXPECT_TRUE(service()->OnExternalExtensionFileFound(
7041 kDontMarkAcknowledged
,
7042 kDontInstallImmediately
));
7043 EXPECT_TRUE(pending
->IsIdPending(kGoodId
));
7045 VerifyCrxInstall(kInvalidPathToCrx
, INSTALL_FAILED
);
7049 // Simulate an external source adding the extension as EXTERNAL_PREF.
7050 content::WindowedNotificationObserver
observer(
7051 extensions::NOTIFICATION_CRX_INSTALLER_DONE
,
7052 content::NotificationService::AllSources());
7053 EXPECT_TRUE(service()->OnExternalExtensionFileFound(
7057 Manifest::EXTERNAL_PREF
,
7059 kDontMarkAcknowledged
,
7060 kDontInstallImmediately
));
7061 EXPECT_TRUE(pending
->IsIdPending(kGoodId
));
7063 VerifyCrxInstall(kInvalidPathToCrx
, INSTALL_FAILED
);
7066 // Simulate an external source adding as EXTERNAL_PREF again.
7067 // This is rejected because the version and the location are the same as
7068 // the previous installation, which is still pending.
7069 EXPECT_FALSE(service()->OnExternalExtensionFileFound(
7073 Manifest::EXTERNAL_PREF
,
7075 kDontMarkAcknowledged
,
7076 kDontInstallImmediately
));
7077 EXPECT_TRUE(pending
->IsIdPending(kGoodId
));
7079 // Try INTERNAL again. Should fail.
7080 EXPECT_FALSE(service()->OnExternalExtensionFileFound(
7086 kDontMarkAcknowledged
,
7087 kDontInstallImmediately
));
7088 EXPECT_TRUE(pending
->IsIdPending(kGoodId
));
7091 // Now the registry adds the extension.
7092 content::WindowedNotificationObserver
observer(
7093 extensions::NOTIFICATION_CRX_INSTALLER_DONE
,
7094 content::NotificationService::AllSources());
7095 EXPECT_TRUE(service()->OnExternalExtensionFileFound(
7099 Manifest::EXTERNAL_REGISTRY
,
7101 kDontMarkAcknowledged
,
7102 kDontInstallImmediately
));
7103 EXPECT_TRUE(pending
->IsIdPending(kGoodId
));
7105 VerifyCrxInstall(kInvalidPathToCrx
, INSTALL_FAILED
);
7108 // Registry outranks both external pref and internal, so both fail.
7109 EXPECT_FALSE(service()->OnExternalExtensionFileFound(
7113 Manifest::EXTERNAL_PREF
,
7115 kDontMarkAcknowledged
,
7116 kDontInstallImmediately
));
7117 EXPECT_TRUE(pending
->IsIdPending(kGoodId
));
7119 EXPECT_FALSE(service()->OnExternalExtensionFileFound(
7125 kDontMarkAcknowledged
,
7126 kDontInstallImmediately
));
7127 EXPECT_TRUE(pending
->IsIdPending(kGoodId
));
7129 pending
->Remove(kGoodId
);
7131 // Install the extension.
7132 base::FilePath path
= data_dir().AppendASCII("good.crx");
7133 const Extension
* ext
= InstallCRX(path
, INSTALL_NEW
);
7134 ValidatePrefKeyCount(1u);
7135 ValidateIntegerPref(good_crx
, "state", Extension::ENABLED
);
7136 ValidateIntegerPref(good_crx
, "location", Manifest::INTERNAL
);
7138 // Now test the logic of OnExternalExtensionFileFound() when the extension
7139 // being added is already installed.
7141 // Tests assume |older_version| is less than the installed version, and
7142 // |newer_version| is greater. Verify this:
7143 ASSERT_TRUE(older_version
.IsOlderThan(ext
->VersionString()));
7144 ASSERT_TRUE(ext
->version()->IsOlderThan(newer_version
.GetString()));
7146 // An external install for the same location should fail if the version is
7147 // older, or the same, and succeed if the version is newer.
7149 // Older than the installed version...
7150 EXPECT_FALSE(service()->OnExternalExtensionFileFound(
7156 kDontMarkAcknowledged
,
7157 kDontInstallImmediately
));
7158 EXPECT_FALSE(pending
->IsIdPending(kGoodId
));
7160 // Same version as the installed version...
7161 EXPECT_FALSE(service()->OnExternalExtensionFileFound(
7167 kDontMarkAcknowledged
,
7168 kDontInstallImmediately
));
7169 EXPECT_FALSE(pending
->IsIdPending(kGoodId
));
7171 // Newer than the installed version...
7172 EXPECT_TRUE(service()->OnExternalExtensionFileFound(
7178 kDontMarkAcknowledged
,
7179 kDontInstallImmediately
));
7180 EXPECT_TRUE(pending
->IsIdPending(kGoodId
));
7182 // An external install for a higher priority install source should succeed
7183 // if the version is greater. |older_version| is not...
7184 EXPECT_FALSE(service()->OnExternalExtensionFileFound(
7188 Manifest::EXTERNAL_PREF
,
7190 kDontMarkAcknowledged
,
7191 kDontInstallImmediately
));
7192 EXPECT_TRUE(pending
->IsIdPending(kGoodId
));
7194 // |newer_version| is newer.
7195 EXPECT_TRUE(service()->OnExternalExtensionFileFound(
7199 Manifest::EXTERNAL_PREF
,
7201 kDontMarkAcknowledged
,
7202 kDontInstallImmediately
));
7203 EXPECT_TRUE(pending
->IsIdPending(kGoodId
));
7205 // An external install for an even higher priority install source should
7206 // succeed if the version is greater.
7207 EXPECT_TRUE(service()->OnExternalExtensionFileFound(
7211 Manifest::EXTERNAL_REGISTRY
,
7213 kDontMarkAcknowledged
,
7214 kDontInstallImmediately
));
7215 EXPECT_TRUE(pending
->IsIdPending(kGoodId
));
7217 // Because EXTERNAL_PREF is a lower priority source than EXTERNAL_REGISTRY,
7218 // adding from external pref will now fail.
7219 EXPECT_FALSE(service()->OnExternalExtensionFileFound(
7223 Manifest::EXTERNAL_PREF
,
7225 kDontMarkAcknowledged
,
7226 kDontInstallImmediately
));
7227 EXPECT_TRUE(pending
->IsIdPending(kGoodId
));
7230 TEST_F(ExtensionServiceTest
, ConcurrentExternalLocalFile
) {
7231 Version
kVersion123("1.2.3");
7232 Version
kVersion124("1.2.4");
7233 Version
kVersion125("1.2.5");
7234 const base::FilePath
kInvalidPathToCrx(FILE_PATH_LITERAL("invalid_path"));
7235 const int kCreationFlags
= 0;
7236 const bool kDontMarkAcknowledged
= false;
7237 const bool kDontInstallImmediately
= false;
7239 InitializeEmptyExtensionService();
7241 extensions::PendingExtensionManager
* pending
=
7242 service()->pending_extension_manager();
7243 EXPECT_FALSE(pending
->IsIdPending(kGoodId
));
7245 // An external provider starts installing from a local crx.
7246 EXPECT_TRUE(service()->OnExternalExtensionFileFound(
7250 Manifest::EXTERNAL_PREF
,
7252 kDontMarkAcknowledged
,
7253 kDontInstallImmediately
));
7254 const extensions::PendingExtensionInfo
* info
;
7255 EXPECT_TRUE((info
= pending
->GetById(kGoodId
)));
7256 EXPECT_TRUE(info
->version().IsValid());
7257 EXPECT_TRUE(info
->version().Equals(kVersion123
));
7259 // Adding a newer version overrides the currently pending version.
7260 EXPECT_TRUE(service()->OnExternalExtensionFileFound(
7264 Manifest::EXTERNAL_PREF
,
7266 kDontMarkAcknowledged
,
7267 kDontInstallImmediately
));
7268 EXPECT_TRUE((info
= pending
->GetById(kGoodId
)));
7269 EXPECT_TRUE(info
->version().IsValid());
7270 EXPECT_TRUE(info
->version().Equals(kVersion124
));
7272 // Adding an older version fails.
7273 EXPECT_FALSE(service()->OnExternalExtensionFileFound(
7277 Manifest::EXTERNAL_PREF
,
7279 kDontMarkAcknowledged
,
7280 kDontInstallImmediately
));
7281 EXPECT_TRUE((info
= pending
->GetById(kGoodId
)));
7282 EXPECT_TRUE(info
->version().IsValid());
7283 EXPECT_TRUE(info
->version().Equals(kVersion124
));
7285 // Adding an older version fails even when coming from a higher-priority
7287 EXPECT_FALSE(service()->OnExternalExtensionFileFound(
7291 Manifest::EXTERNAL_REGISTRY
,
7293 kDontMarkAcknowledged
,
7294 kDontInstallImmediately
));
7295 EXPECT_TRUE((info
= pending
->GetById(kGoodId
)));
7296 EXPECT_TRUE(info
->version().IsValid());
7297 EXPECT_TRUE(info
->version().Equals(kVersion124
));
7299 // Adding the latest version from the webstore overrides a specific version.
7300 GURL
kUpdateUrl("http://example.com/update");
7301 EXPECT_TRUE(service()->OnExternalExtensionUpdateUrlFound(
7305 Manifest::EXTERNAL_POLICY_DOWNLOAD
,
7306 Extension::NO_FLAGS
,
7308 EXPECT_TRUE((info
= pending
->GetById(kGoodId
)));
7309 EXPECT_FALSE(info
->version().IsValid());
7312 // This makes sure we can package and install CRX files that use whitelisted
7314 TEST_F(ExtensionServiceTest
, InstallWhitelistedExtension
) {
7315 std::string test_id
= "hdkklepkcpckhnpgjnmbdfhehckloojk";
7316 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
7317 extensions::switches::kWhitelistedExtensionID
, test_id
);
7319 InitializeEmptyExtensionService();
7320 base::FilePath path
= data_dir().AppendASCII("permissions");
7321 base::FilePath pem_path
= path
7322 .AppendASCII("whitelist.pem");
7324 .AppendASCII("whitelist");
7326 const Extension
* extension
= PackAndInstallCRX(path
, pem_path
, INSTALL_NEW
);
7327 EXPECT_EQ(0u, GetErrors().size());
7328 ASSERT_EQ(1u, registry()->enabled_extensions().size());
7329 EXPECT_EQ(test_id
, extension
->id());
7332 // Test that when multiple sources try to install an extension,
7333 // we consistently choose the right one. To make tests easy to read,
7334 // methods that fake requests to install crx files in several ways
7336 class ExtensionSourcePriorityTest
: public ExtensionServiceTest
{
7338 void SetUp() override
{
7339 ExtensionServiceTest::SetUp();
7341 // All tests use a single extension. Put the id and path in member vars
7342 // that all methods can read.
7344 crx_path_
= data_dir().AppendASCII("good.crx");
7347 // Fake an external source adding a URL to fetch an extension from.
7348 bool AddPendingExternalPrefUrl() {
7349 return service()->pending_extension_manager()->AddFromExternalUpdateUrl(
7353 Manifest::EXTERNAL_PREF_DOWNLOAD
,
7354 Extension::NO_FLAGS
,
7358 // Fake an external file from external_extensions.json.
7359 bool AddPendingExternalPrefFileInstall() {
7360 Version
version("1.0.0.0");
7362 return service()->OnExternalExtensionFileFound(crx_id_
,
7365 Manifest::EXTERNAL_PREF
,
7366 Extension::NO_FLAGS
,
7371 // Fake a request from sync to install an extension.
7372 bool AddPendingSyncInstall() {
7373 return service()->pending_extension_manager()->AddFromSync(
7375 GURL(kGoodUpdateURL
),
7378 kGoodInstalledByCustodian
);
7381 // Fake a policy install.
7382 bool AddPendingPolicyInstall() {
7383 // Get path to the CRX with id |kGoodId|.
7384 return service()->OnExternalExtensionUpdateUrlFound(
7388 Manifest::EXTERNAL_POLICY_DOWNLOAD
,
7389 Extension::NO_FLAGS
,
7393 // Get the install source of a pending extension.
7394 Manifest::Location
GetPendingLocation() {
7395 const extensions::PendingExtensionInfo
* info
;
7397 (info
= service()->pending_extension_manager()->GetById(crx_id_
)));
7398 return info
->install_source();
7401 // Is an extension pending from a sync request?
7402 bool GetPendingIsFromSync() {
7403 const extensions::PendingExtensionInfo
* info
;
7405 (info
= service()->pending_extension_manager()->GetById(crx_id_
)));
7406 return info
->is_from_sync();
7409 // Is the CRX id these tests use pending?
7410 bool IsCrxPending() {
7411 return service()->pending_extension_manager()->IsIdPending(crx_id_
);
7414 // Is an extension installed?
7415 bool IsCrxInstalled() {
7416 return (service()->GetExtensionById(crx_id_
, true) != NULL
);
7420 // All tests use a single extension. Making the id and path member
7421 // vars avoids pasing the same argument to every method.
7422 std::string crx_id_
;
7423 base::FilePath crx_path_
;
7426 // Test that a pending request for installation of an external CRX from
7427 // an update URL overrides a pending request to install the same extension
7429 TEST_F(ExtensionSourcePriorityTest
, PendingExternalFileOverSync
) {
7430 InitializeEmptyExtensionService();
7432 ASSERT_FALSE(IsCrxInstalled());
7434 // Install pending extension from sync.
7435 content::WindowedNotificationObserver
observer(
7436 extensions::NOTIFICATION_CRX_INSTALLER_DONE
,
7437 content::NotificationService::AllSources());
7438 EXPECT_TRUE(AddPendingSyncInstall());
7439 ASSERT_EQ(Manifest::INTERNAL
, GetPendingLocation());
7440 EXPECT_TRUE(GetPendingIsFromSync());
7441 ASSERT_FALSE(IsCrxInstalled());
7443 // Install pending as external prefs json would.
7444 AddPendingExternalPrefFileInstall();
7445 ASSERT_EQ(Manifest::EXTERNAL_PREF
, GetPendingLocation());
7446 ASSERT_FALSE(IsCrxInstalled());
7448 // Another request from sync should be ignored.
7449 EXPECT_FALSE(AddPendingSyncInstall());
7450 ASSERT_EQ(Manifest::EXTERNAL_PREF
, GetPendingLocation());
7451 ASSERT_FALSE(IsCrxInstalled());
7454 VerifyCrxInstall(crx_path_
, INSTALL_NEW
);
7455 ASSERT_TRUE(IsCrxInstalled());
7458 // Test that an install of an external CRX from an update overrides
7459 // an install of the same extension from sync.
7460 TEST_F(ExtensionSourcePriorityTest
, PendingExternalUrlOverSync
) {
7461 InitializeEmptyExtensionService();
7462 ASSERT_FALSE(IsCrxInstalled());
7464 EXPECT_TRUE(AddPendingSyncInstall());
7465 ASSERT_EQ(Manifest::INTERNAL
, GetPendingLocation());
7466 EXPECT_TRUE(GetPendingIsFromSync());
7467 ASSERT_FALSE(IsCrxInstalled());
7469 ASSERT_TRUE(AddPendingExternalPrefUrl());
7470 ASSERT_EQ(Manifest::EXTERNAL_PREF_DOWNLOAD
, GetPendingLocation());
7471 EXPECT_FALSE(GetPendingIsFromSync());
7472 ASSERT_FALSE(IsCrxInstalled());
7474 EXPECT_FALSE(AddPendingSyncInstall());
7475 ASSERT_EQ(Manifest::EXTERNAL_PREF_DOWNLOAD
, GetPendingLocation());
7476 EXPECT_FALSE(GetPendingIsFromSync());
7477 ASSERT_FALSE(IsCrxInstalled());
7480 // Test that an external install request stops sync from installing
7481 // the same extension.
7482 TEST_F(ExtensionSourcePriorityTest
, InstallExternalBlocksSyncRequest
) {
7483 InitializeEmptyExtensionService();
7484 ASSERT_FALSE(IsCrxInstalled());
7486 // External prefs starts an install.
7487 AddPendingExternalPrefFileInstall();
7489 // Crx installer was made, but has not yet run.
7490 ASSERT_FALSE(IsCrxInstalled());
7492 // Before the CRX installer runs, Sync requests that the same extension
7493 // be installed. Should fail, because an external source is pending.
7494 content::WindowedNotificationObserver
observer(
7495 extensions::NOTIFICATION_CRX_INSTALLER_DONE
,
7496 content::NotificationService::AllSources());
7497 ASSERT_FALSE(AddPendingSyncInstall());
7499 // Wait for the external source to install.
7501 VerifyCrxInstall(crx_path_
, INSTALL_NEW
);
7502 ASSERT_TRUE(IsCrxInstalled());
7504 // Now that the extension is installed, sync request should fail
7505 // because the extension is already installed.
7506 ASSERT_FALSE(AddPendingSyncInstall());
7509 // Test that installing an external extension displays a GlobalError.
7510 TEST_F(ExtensionServiceTest
, ExternalInstallGlobalError
) {
7511 FeatureSwitch::ScopedOverride
prompt(
7512 FeatureSwitch::prompt_for_external_extensions(), true);
7514 InitializeEmptyExtensionService();
7515 MockExtensionProvider
* provider
=
7516 new MockExtensionProvider(service(), Manifest::EXTERNAL_PREF
);
7517 AddMockExternalProvider(provider
);
7519 service()->external_install_manager()->UpdateExternalExtensionAlert();
7520 // Should return false, meaning there aren't any extensions that the user
7521 // needs to know about.
7523 service()->external_install_manager()->HasExternalInstallError());
7525 // This is a normal extension, installed normally.
7526 // This should NOT trigger an alert.
7527 service()->set_extensions_enabled(true);
7528 base::FilePath path
= data_dir().AppendASCII("good.crx");
7529 InstallCRX(path
, INSTALL_NEW
);
7531 service()->CheckForExternalUpdates();
7532 base::RunLoop().RunUntilIdle();
7534 service()->external_install_manager()->HasExternalInstallError());
7536 // A hosted app, installed externally.
7537 // This should NOT trigger an alert.
7538 provider
->UpdateOrAddExtension(
7539 hosted_app
, "1.0.0.0", data_dir().AppendASCII("hosted_app.crx"));
7541 content::WindowedNotificationObserver
observer(
7542 extensions::NOTIFICATION_CRX_INSTALLER_DONE
,
7543 content::NotificationService::AllSources());
7544 service()->CheckForExternalUpdates();
7547 service()->external_install_manager()->HasExternalInstallError());
7549 // Another normal extension, but installed externally.
7550 // This SHOULD trigger an alert.
7551 provider
->UpdateOrAddExtension(
7552 page_action
, "1.0.0.0", data_dir().AppendASCII("page_action.crx"));
7554 content::WindowedNotificationObserver
observer2(
7555 extensions::NOTIFICATION_CRX_INSTALLER_DONE
,
7556 content::NotificationService::AllSources());
7557 service()->CheckForExternalUpdates();
7559 EXPECT_TRUE(service()->external_install_manager()->HasExternalInstallError());
7562 // Test that external extensions are initially disabled, and that enabling
7563 // them clears the prompt.
7564 TEST_F(ExtensionServiceTest
, ExternalInstallInitiallyDisabled
) {
7565 FeatureSwitch::ScopedOverride
prompt(
7566 FeatureSwitch::prompt_for_external_extensions(), true);
7568 InitializeEmptyExtensionService();
7569 MockExtensionProvider
* provider
=
7570 new MockExtensionProvider(service(), Manifest::EXTERNAL_PREF
);
7571 AddMockExternalProvider(provider
);
7573 provider
->UpdateOrAddExtension(
7574 page_action
, "1.0.0.0", data_dir().AppendASCII("page_action.crx"));
7576 content::WindowedNotificationObserver
observer(
7577 extensions::NOTIFICATION_CRX_INSTALLER_DONE
,
7578 content::NotificationService::AllSources());
7579 service()->CheckForExternalUpdates();
7581 EXPECT_TRUE(service()->external_install_manager()->HasExternalInstallError());
7582 EXPECT_FALSE(service()->IsExtensionEnabled(page_action
));
7584 const Extension
* extension
=
7585 registry()->disabled_extensions().GetByID(page_action
);
7586 EXPECT_TRUE(extension
);
7587 EXPECT_EQ(page_action
, extension
->id());
7589 service()->EnableExtension(page_action
);
7591 service()->external_install_manager()->HasExternalInstallError());
7592 EXPECT_TRUE(service()->IsExtensionEnabled(page_action
));
7595 // Test that installing multiple external extensions works.
7596 // Flaky on windows; http://crbug.com/295757 .
7598 #define MAYBE_ExternalInstallMultiple DISABLED_ExternalInstallMultiple
7600 #define MAYBE_ExternalInstallMultiple ExternalInstallMultiple
7602 TEST_F(ExtensionServiceTest
, MAYBE_ExternalInstallMultiple
) {
7603 FeatureSwitch::ScopedOverride
prompt(
7604 FeatureSwitch::prompt_for_external_extensions(), true);
7606 InitializeEmptyExtensionService();
7607 MockExtensionProvider
* provider
=
7608 new MockExtensionProvider(service(), Manifest::EXTERNAL_PREF
);
7609 AddMockExternalProvider(provider
);
7611 provider
->UpdateOrAddExtension(
7612 page_action
, "1.0.0.0", data_dir().AppendASCII("page_action.crx"));
7613 provider
->UpdateOrAddExtension(
7614 good_crx
, "1.0.0.0", data_dir().AppendASCII("good.crx"));
7615 provider
->UpdateOrAddExtension(
7616 theme_crx
, "2.0", data_dir().AppendASCII("theme.crx"));
7619 content::WindowedNotificationObserver
observer(
7620 extensions::NOTIFICATION_CRX_INSTALLER_DONE
,
7621 base::Bind(&WaitForCountNotificationsCallback
, &count
));
7622 service()->CheckForExternalUpdates();
7624 EXPECT_TRUE(service()->external_install_manager()->HasExternalInstallError());
7625 EXPECT_FALSE(service()->IsExtensionEnabled(page_action
));
7626 EXPECT_FALSE(service()->IsExtensionEnabled(good_crx
));
7627 EXPECT_FALSE(service()->IsExtensionEnabled(theme_crx
));
7629 service()->EnableExtension(page_action
);
7630 EXPECT_TRUE(service()->external_install_manager()->HasExternalInstallError());
7631 EXPECT_FALSE(service()
7632 ->external_install_manager()
7633 ->HasExternalInstallBubbleForTesting());
7635 service()->EnableExtension(theme_crx
);
7636 EXPECT_TRUE(service()->external_install_manager()->HasExternalInstallError());
7637 EXPECT_FALSE(service()
7638 ->external_install_manager()
7639 ->HasExternalInstallBubbleForTesting());
7641 service()->EnableExtension(good_crx
);
7643 service()->external_install_manager()->HasExternalInstallError());
7644 EXPECT_FALSE(service()
7645 ->external_install_manager()
7646 ->HasExternalInstallBubbleForTesting());
7649 // Test that there is a bubble for external extensions that update
7650 // from the webstore if the profile is not new.
7651 TEST_F(ExtensionServiceTest
, ExternalInstallUpdatesFromWebstoreOldProfile
) {
7652 FeatureSwitch::ScopedOverride
prompt(
7653 FeatureSwitch::prompt_for_external_extensions(), true);
7655 // This sets up the ExtensionPrefs used by our ExtensionService to be
7657 ExtensionServiceInitParams params
= CreateDefaultInitParams();
7658 params
.is_first_run
= false;
7659 InitializeExtensionService(params
);
7661 base::FilePath crx_path
= temp_dir().path().AppendASCII("webstore.crx");
7662 PackCRX(data_dir().AppendASCII("update_from_webstore"),
7663 data_dir().AppendASCII("update_from_webstore.pem"),
7666 MockExtensionProvider
* provider
=
7667 new MockExtensionProvider(service(), Manifest::EXTERNAL_PREF
);
7668 AddMockExternalProvider(provider
);
7669 provider
->UpdateOrAddExtension(updates_from_webstore
, "1", crx_path
);
7671 content::WindowedNotificationObserver
observer(
7672 extensions::NOTIFICATION_CRX_INSTALLER_DONE
,
7673 content::NotificationService::AllSources());
7674 service()->CheckForExternalUpdates();
7676 EXPECT_TRUE(service()->external_install_manager()->HasExternalInstallError());
7677 EXPECT_TRUE(service()
7678 ->external_install_manager()
7679 ->HasExternalInstallBubbleForTesting());
7680 EXPECT_FALSE(service()->IsExtensionEnabled(updates_from_webstore
));
7683 // Test that there is no bubble for external extensions if the profile is new.
7684 TEST_F(ExtensionServiceTest
, ExternalInstallUpdatesFromWebstoreNewProfile
) {
7685 FeatureSwitch::ScopedOverride
prompt(
7686 FeatureSwitch::prompt_for_external_extensions(), true);
7688 InitializeEmptyExtensionService();
7690 base::FilePath crx_path
= temp_dir().path().AppendASCII("webstore.crx");
7691 PackCRX(data_dir().AppendASCII("update_from_webstore"),
7692 data_dir().AppendASCII("update_from_webstore.pem"),
7695 MockExtensionProvider
* provider
=
7696 new MockExtensionProvider(service(), Manifest::EXTERNAL_PREF
);
7697 AddMockExternalProvider(provider
);
7698 provider
->UpdateOrAddExtension(updates_from_webstore
, "1", crx_path
);
7700 content::WindowedNotificationObserver
observer(
7701 extensions::NOTIFICATION_CRX_INSTALLER_DONE
,
7702 content::NotificationService::AllSources());
7703 service()->CheckForExternalUpdates();
7705 EXPECT_TRUE(service()->external_install_manager()->HasExternalInstallError());
7706 EXPECT_FALSE(service()
7707 ->external_install_manager()
7708 ->HasExternalInstallBubbleForTesting());
7709 EXPECT_FALSE(service()->IsExtensionEnabled(updates_from_webstore
));
7712 // Test that clicking to remove the extension on an external install warning
7713 // uninstalls the extension.
7714 TEST_F(ExtensionServiceTest
, ExternalInstallClickToRemove
) {
7715 FeatureSwitch::ScopedOverride
prompt(
7716 FeatureSwitch::prompt_for_external_extensions(), true);
7718 ExtensionServiceInitParams params
= CreateDefaultInitParams();
7719 params
.is_first_run
= false;
7720 InitializeExtensionService(params
);
7722 base::FilePath crx_path
= temp_dir().path().AppendASCII("webstore.crx");
7723 PackCRX(data_dir().AppendASCII("update_from_webstore"),
7724 data_dir().AppendASCII("update_from_webstore.pem"),
7727 MockExtensionProvider
* provider
=
7728 new MockExtensionProvider(service_
, Manifest::EXTERNAL_PREF
);
7729 AddMockExternalProvider(provider
);
7730 provider
->UpdateOrAddExtension(updates_from_webstore
, "1", crx_path
);
7732 content::WindowedNotificationObserver
observer(
7733 extensions::NOTIFICATION_CRX_INSTALLER_DONE
,
7734 content::NotificationService::AllSources());
7735 service_
->CheckForExternalUpdates();
7737 EXPECT_TRUE(service_
->external_install_manager()->HasExternalInstallError());
7739 // We check both enabled and disabled, since these are "eventually exclusive"
7741 EXPECT_TRUE(registry()->disabled_extensions().GetByID(updates_from_webstore
));
7742 EXPECT_FALSE(registry()->enabled_extensions().GetByID(updates_from_webstore
));
7744 // Click the negative response.
7745 service_
->external_install_manager()->error_for_testing()->InstallUIAbort(
7747 // The Extension should be uninstalled.
7748 EXPECT_FALSE(registry()->GetExtensionById(updates_from_webstore
,
7749 ExtensionRegistry::EVERYTHING
));
7750 // The error should be removed.
7751 EXPECT_FALSE(service_
->external_install_manager()->HasExternalInstallError());
7754 // Test that clicking to keep the extension on an external install warning
7755 // re-enables the extension.
7756 TEST_F(ExtensionServiceTest
, ExternalInstallClickToKeep
) {
7757 FeatureSwitch::ScopedOverride
prompt(
7758 FeatureSwitch::prompt_for_external_extensions(), true);
7760 ExtensionServiceInitParams params
= CreateDefaultInitParams();
7761 params
.is_first_run
= false;
7762 InitializeExtensionService(params
);
7764 base::FilePath crx_path
= temp_dir().path().AppendASCII("webstore.crx");
7765 PackCRX(data_dir().AppendASCII("update_from_webstore"),
7766 data_dir().AppendASCII("update_from_webstore.pem"),
7769 MockExtensionProvider
* provider
=
7770 new MockExtensionProvider(service_
, Manifest::EXTERNAL_PREF
);
7771 AddMockExternalProvider(provider
);
7772 provider
->UpdateOrAddExtension(updates_from_webstore
, "1", crx_path
);
7774 content::WindowedNotificationObserver
observer(
7775 extensions::NOTIFICATION_CRX_INSTALLER_DONE
,
7776 content::NotificationService::AllSources());
7777 service_
->CheckForExternalUpdates();
7779 EXPECT_TRUE(service_
->external_install_manager()->HasExternalInstallError());
7781 // We check both enabled and disabled, since these are "eventually exclusive"
7783 EXPECT_TRUE(registry()->disabled_extensions().GetByID(updates_from_webstore
));
7784 EXPECT_FALSE(registry()->enabled_extensions().GetByID(updates_from_webstore
));
7786 // Accept the extension.
7787 service_
->external_install_manager()->error_for_testing()->InstallUIProceed();
7789 // It should be enabled again.
7790 EXPECT_TRUE(registry()->enabled_extensions().GetByID(updates_from_webstore
));
7792 registry()->disabled_extensions().GetByID(updates_from_webstore
));
7794 // The error should be removed.
7795 EXPECT_FALSE(service_
->external_install_manager()->HasExternalInstallError());
7798 TEST_F(ExtensionServiceTest
, InstallBlacklistedExtension
) {
7799 InitializeEmptyExtensionService();
7801 scoped_refptr
<Extension
> extension
= extensions::ExtensionBuilder()
7802 .SetManifest(extensions::DictionaryBuilder()
7803 .Set("name", "extension")
7804 .Set("version", "1.0")
7805 .Set("manifest_version", 2).Build())
7807 ASSERT_TRUE(extension
.get());
7808 const std::string
& id
= extension
->id();
7810 std::set
<std::string
> id_set
;
7812 extensions::ExtensionNotificationObserver
notifications(
7813 content::NotificationService::AllSources(), id_set
);
7815 // Installation should be allowed but the extension should never have been
7816 // loaded and it should be blacklisted in prefs.
7817 service()->OnExtensionInstalled(
7819 syncer::StringOrdinal(),
7820 (extensions::kInstallFlagIsBlacklistedForMalware
|
7821 extensions::kInstallFlagInstallImmediately
));
7822 base::RunLoop().RunUntilIdle();
7824 // Extension was installed but not loaded.
7825 EXPECT_TRUE(notifications
.CheckNotifications(
7826 extensions::NOTIFICATION_EXTENSION_WILL_BE_INSTALLED_DEPRECATED
));
7827 EXPECT_TRUE(service()->GetInstalledExtension(id
));
7829 EXPECT_FALSE(registry()->enabled_extensions().Contains(id
));
7830 EXPECT_TRUE(registry()->blacklisted_extensions().Contains(id
));
7832 EXPECT_TRUE(ExtensionPrefs::Get(profile())->IsExtensionBlacklisted(id
));
7834 ExtensionPrefs::Get(profile())->IsBlacklistedExtensionAcknowledged(id
));
7837 // Tests a profile being destroyed correctly disables extensions.
7838 TEST_F(ExtensionServiceTest
, DestroyingProfileClearsExtensions
) {
7839 InitializeEmptyExtensionService();
7841 InstallCRX(data_dir().AppendASCII("good.crx"), INSTALL_NEW
);
7842 EXPECT_NE(UnloadedExtensionInfo::REASON_PROFILE_SHUTDOWN
, unloaded_reason_
);
7843 EXPECT_EQ(1u, registry()->enabled_extensions().size());
7844 EXPECT_EQ(0u, registry()->disabled_extensions().size());
7845 EXPECT_EQ(0u, registry()->terminated_extensions().size());
7846 EXPECT_EQ(0u, registry()->blacklisted_extensions().size());
7848 service()->Observe(chrome::NOTIFICATION_PROFILE_DESTRUCTION_STARTED
,
7849 content::Source
<Profile
>(profile()),
7850 content::NotificationService::NoDetails());
7851 EXPECT_EQ(UnloadedExtensionInfo::REASON_PROFILE_SHUTDOWN
, unloaded_reason_
);
7852 EXPECT_EQ(0u, registry()->enabled_extensions().size());
7853 EXPECT_EQ(0u, registry()->disabled_extensions().size());
7854 EXPECT_EQ(0u, registry()->terminated_extensions().size());
7855 EXPECT_EQ(0u, registry()->blacklisted_extensions().size());