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 unpacked
[] = "cbcdidchbppangcjoddlpdjlenngjldk";
201 const char updates_from_webstore
[] = "akjooamlhcgeopfifcmlggaebeocgokj";
202 const char permissions_blocklist
[] = "noffkehfcaggllbcojjbopcmlhcnhcdn";
204 struct ExtensionsOrder
{
205 bool operator()(const scoped_refptr
<const Extension
>& a
,
206 const scoped_refptr
<const Extension
>& b
) {
207 return a
->name() < b
->name();
211 static std::vector
<base::string16
> GetErrors() {
212 const std::vector
<base::string16
>* errors
=
213 ExtensionErrorReporter::GetInstance()->GetErrors();
214 std::vector
<base::string16
> ret_val
;
216 for (std::vector
<base::string16
>::const_iterator iter
= errors
->begin();
217 iter
!= errors
->end(); ++iter
) {
218 std::string utf8_error
= base::UTF16ToUTF8(*iter
);
219 if (utf8_error
.find(".svn") == std::string::npos
) {
220 ret_val
.push_back(*iter
);
224 // The tests rely on the errors being in a certain order, which can vary
225 // depending on how filesystem iteration works.
226 std::stable_sort(ret_val
.begin(), ret_val
.end());
231 static void AddPattern(URLPatternSet
* extent
, const std::string
& pattern
) {
232 int schemes
= URLPattern::SCHEME_ALL
;
233 extent
->AddPattern(URLPattern(schemes
, pattern
));
236 base::FilePath
GetTemporaryFile() {
237 base::FilePath temp_file
;
238 CHECK(base::CreateTemporaryFile(&temp_file
));
242 bool WaitForCountNotificationsCallback(int *count
) {
243 return --(*count
) == 0;
248 class MockExtensionProvider
: public extensions::ExternalProviderInterface
{
250 MockExtensionProvider(
251 VisitorInterface
* visitor
,
252 Manifest::Location location
)
253 : location_(location
), visitor_(visitor
), visit_count_(0) {
256 ~MockExtensionProvider() override
{}
258 void UpdateOrAddExtension(const std::string
& id
,
259 const std::string
& version
,
260 const base::FilePath
& path
) {
261 extension_map_
[id
] = std::make_pair(version
, path
);
264 void RemoveExtension(const std::string
& id
) {
265 extension_map_
.erase(id
);
268 // ExternalProvider implementation:
269 void VisitRegisteredExtension() override
{
271 for (DataMap::const_iterator i
= extension_map_
.begin();
272 i
!= extension_map_
.end(); ++i
) {
273 Version
version(i
->second
.first
);
275 visitor_
->OnExternalExtensionFileFound(
276 i
->first
, &version
, i
->second
.second
, location_
,
277 Extension::NO_FLAGS
, false, false);
279 visitor_
->OnExternalProviderReady(this);
282 bool HasExtension(const std::string
& id
) const override
{
283 return extension_map_
.find(id
) != extension_map_
.end();
286 bool GetExtensionDetails(const std::string
& id
,
287 Manifest::Location
* location
,
288 scoped_ptr
<Version
>* version
) const override
{
289 DataMap::const_iterator it
= extension_map_
.find(id
);
290 if (it
== extension_map_
.end())
294 version
->reset(new Version(it
->second
.first
));
297 *location
= location_
;
302 bool IsReady() const override
{ return true; }
304 void ServiceShutdown() override
{}
306 int visit_count() const { return visit_count_
; }
307 void set_visit_count(int visit_count
) {
308 visit_count_
= visit_count
;
312 typedef std::map
< std::string
, std::pair
<std::string
, base::FilePath
> >
314 DataMap extension_map_
;
315 Manifest::Location location_
;
316 VisitorInterface
* visitor_
;
318 // visit_count_ tracks the number of calls to VisitRegisteredExtension().
319 // Mutable because it must be incremented on each call to
320 // VisitRegisteredExtension(), which must be a const method to inherit
321 // from the class being mocked.
322 mutable int visit_count_
;
324 DISALLOW_COPY_AND_ASSIGN(MockExtensionProvider
);
327 class MockProviderVisitor
328 : public extensions::ExternalProviderInterface::VisitorInterface
{
330 // The provider will return |fake_base_path| from
331 // GetBaseCrxFilePath(). User can test the behavior with
332 // and without an empty path using this parameter.
333 explicit MockProviderVisitor(base::FilePath fake_base_path
)
335 fake_base_path_(fake_base_path
),
336 expected_creation_flags_(Extension::NO_FLAGS
) {
337 profile_
.reset(new TestingProfile
);
340 MockProviderVisitor(base::FilePath fake_base_path
,
341 int expected_creation_flags
)
343 fake_base_path_(fake_base_path
),
344 expected_creation_flags_(expected_creation_flags
) {
347 int Visit(const std::string
& json_data
) {
348 // Give the test json file to the provider for parsing.
349 provider_
.reset(new extensions::ExternalProviderImpl(
351 new extensions::ExternalTestingLoader(json_data
, fake_base_path_
),
353 Manifest::EXTERNAL_PREF
,
354 Manifest::EXTERNAL_PREF_DOWNLOAD
,
355 Extension::NO_FLAGS
));
357 // We also parse the file into a dictionary to compare what we get back
358 // from the provider.
359 JSONStringValueDeserializer
deserializer(json_data
);
360 base::Value
* json_value
= deserializer
.Deserialize(NULL
, NULL
);
362 if (!json_value
|| !json_value
->IsType(base::Value::TYPE_DICTIONARY
)) {
363 NOTREACHED() << "Unable to deserialize json data";
366 base::DictionaryValue
* external_extensions
=
367 static_cast<base::DictionaryValue
*>(json_value
);
368 prefs_
.reset(external_extensions
);
371 // Reset our counter.
373 // Ask the provider to look up all extensions and return them.
374 provider_
->VisitRegisteredExtension();
379 bool OnExternalExtensionFileFound(const std::string
& id
,
380 const Version
* version
,
381 const base::FilePath
& path
,
382 Manifest::Location unused
,
384 bool mark_acknowledged
,
385 bool install_immediately
) override
{
386 EXPECT_EQ(expected_creation_flags_
, creation_flags
);
389 base::DictionaryValue
* pref
;
390 // This tests is to make sure that the provider only notifies us of the
391 // values we gave it. So if the id we doesn't exist in our internal
392 // dictionary then something is wrong.
393 EXPECT_TRUE(prefs_
->GetDictionary(id
, &pref
))
394 << "Got back ID (" << id
.c_str() << ") we weren't expecting";
396 EXPECT_TRUE(path
.IsAbsolute());
397 if (!fake_base_path_
.empty())
398 EXPECT_TRUE(fake_base_path_
.IsParent(path
));
401 EXPECT_TRUE(provider_
->HasExtension(id
));
403 // Ask provider if the extension we got back is registered.
404 Manifest::Location location
= Manifest::INVALID_LOCATION
;
405 scoped_ptr
<Version
> v1
;
406 base::FilePath crx_path
;
408 EXPECT_TRUE(provider_
->GetExtensionDetails(id
, NULL
, &v1
));
409 EXPECT_STREQ(version
->GetString().c_str(), v1
->GetString().c_str());
411 scoped_ptr
<Version
> v2
;
412 EXPECT_TRUE(provider_
->GetExtensionDetails(id
, &location
, &v2
));
413 EXPECT_STREQ(version
->GetString().c_str(), v1
->GetString().c_str());
414 EXPECT_STREQ(version
->GetString().c_str(), v2
->GetString().c_str());
415 EXPECT_EQ(Manifest::EXTERNAL_PREF
, location
);
417 // Remove it so we won't count it ever again.
418 prefs_
->Remove(id
, NULL
);
423 bool OnExternalExtensionUpdateUrlFound(const std::string
& id
,
424 const std::string
& install_parameter
,
425 const GURL
& update_url
,
426 Manifest::Location location
,
428 bool mark_acknowledged
) override
{
430 base::DictionaryValue
* pref
;
431 // This tests is to make sure that the provider only notifies us of the
432 // values we gave it. So if the id we doesn't exist in our internal
433 // dictionary then something is wrong.
434 EXPECT_TRUE(prefs_
->GetDictionary(id
, &pref
))
435 << L
"Got back ID (" << id
.c_str() << ") we weren't expecting";
436 EXPECT_EQ(Manifest::EXTERNAL_PREF_DOWNLOAD
, location
);
439 EXPECT_TRUE(provider_
->HasExtension(id
));
441 // External extensions with update URLs do not have versions.
442 scoped_ptr
<Version
> v1
;
443 Manifest::Location location1
= Manifest::INVALID_LOCATION
;
444 EXPECT_TRUE(provider_
->GetExtensionDetails(id
, &location1
, &v1
));
445 EXPECT_FALSE(v1
.get());
446 EXPECT_EQ(Manifest::EXTERNAL_PREF_DOWNLOAD
, location1
);
448 std::string parsed_install_parameter
;
449 pref
->GetString("install_parameter", &parsed_install_parameter
);
450 EXPECT_EQ(parsed_install_parameter
, install_parameter
);
452 // Remove it so we won't count it again.
453 prefs_
->Remove(id
, NULL
);
458 void OnExternalProviderReady(
459 const extensions::ExternalProviderInterface
* provider
) override
{
460 EXPECT_EQ(provider
, provider_
.get());
461 EXPECT_TRUE(provider
->IsReady());
464 Profile
* profile() { return profile_
.get(); }
468 base::FilePath fake_base_path_
;
469 int expected_creation_flags_
;
470 scoped_ptr
<extensions::ExternalProviderImpl
> provider_
;
471 scoped_ptr
<base::DictionaryValue
> prefs_
;
472 scoped_ptr
<TestingProfile
> profile_
;
474 DISALLOW_COPY_AND_ASSIGN(MockProviderVisitor
);
477 class ExtensionServiceTest
: public extensions::ExtensionServiceTestBase
,
478 public content::NotificationObserver
{
480 ExtensionServiceTest()
481 : unloaded_reason_(UnloadedExtensionInfo::REASON_UNDEFINED
),
484 override_external_install_prompt_(
485 FeatureSwitch::prompt_for_external_extensions(),
487 expected_extensions_count_(0) {
489 extensions::NOTIFICATION_EXTENSION_LOADED_DEPRECATED
,
490 content::NotificationService::AllSources());
492 extensions::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED
,
493 content::NotificationService::AllSources());
496 extensions::NOTIFICATION_EXTENSION_WILL_BE_INSTALLED_DEPRECATED
,
497 content::NotificationService::AllSources());
500 void Observe(int type
,
501 const content::NotificationSource
& source
,
502 const content::NotificationDetails
& details
) override
{
504 case extensions::NOTIFICATION_EXTENSION_LOADED_DEPRECATED
: {
505 const Extension
* extension
=
506 content::Details
<const Extension
>(details
).ptr();
507 loaded_
.push_back(make_scoped_refptr(extension
));
508 // The tests rely on the errors being in a certain order, which can vary
509 // depending on how filesystem iteration works.
510 std::stable_sort(loaded_
.begin(), loaded_
.end(), ExtensionsOrder());
514 case extensions::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED
: {
515 UnloadedExtensionInfo
* unloaded_info
=
516 content::Details
<UnloadedExtensionInfo
>(details
).ptr();
517 const Extension
* e
= unloaded_info
->extension
;
518 unloaded_id_
= e
->id();
519 unloaded_reason_
= unloaded_info
->reason
;
520 extensions::ExtensionList::iterator i
=
521 std::find(loaded_
.begin(), loaded_
.end(), e
);
522 // TODO(erikkay) fix so this can be an assert. Right now the tests
523 // are manually calling clear() on loaded_, so this isn't doable.
524 if (i
== loaded_
.end())
529 case extensions::NOTIFICATION_EXTENSION_WILL_BE_INSTALLED_DEPRECATED
: {
530 const extensions::InstalledExtensionInfo
* installed_info
=
531 content::Details
<const extensions::InstalledExtensionInfo
>(details
)
533 installed_
= installed_info
->extension
;
534 was_update_
= installed_info
->is_update
;
535 old_name_
= installed_info
->old_name
;
544 void AddMockExternalProvider(
545 extensions::ExternalProviderInterface
* provider
) {
546 service()->AddProviderForTesting(provider
);
549 void MockSyncStartFlare(bool* was_called
,
550 syncer::ModelType
* model_type_passed_in
,
551 syncer::ModelType model_type
) {
553 *model_type_passed_in
= model_type
;
557 // Paths to some of the fake extensions.
558 base::FilePath
good0_path() {
561 .AppendASCII("Extensions")
563 .AppendASCII("1.0.0.0");
566 base::FilePath
good1_path() {
569 .AppendASCII("Extensions")
574 base::FilePath
good2_path() {
577 .AppendASCII("Extensions")
582 void TestExternalProvider(MockExtensionProvider
* provider
,
583 Manifest::Location location
);
585 void PackCRX(const base::FilePath
& dir_path
,
586 const base::FilePath
& pem_path
,
587 const base::FilePath
& crx_path
) {
588 // Use the existing pem key, if provided.
589 base::FilePath pem_output_path
;
590 if (pem_path
.value().empty()) {
591 pem_output_path
= crx_path
.DirName().AppendASCII("temp.pem");
593 ASSERT_TRUE(base::PathExists(pem_path
));
596 ASSERT_TRUE(base::DeleteFile(crx_path
, false));
598 scoped_ptr
<ExtensionCreator
> creator(new ExtensionCreator());
599 ASSERT_TRUE(creator
->Run(dir_path
,
603 ExtensionCreator::kOverwriteCRX
));
605 ASSERT_TRUE(base::PathExists(crx_path
));
612 INSTALL_WITHOUT_LOAD
,
615 const Extension
* PackAndInstallCRX(const base::FilePath
& dir_path
,
616 const base::FilePath
& pem_path
,
617 InstallState install_state
,
618 int creation_flags
) {
619 base::FilePath crx_path
;
620 base::ScopedTempDir temp_dir
;
621 EXPECT_TRUE(temp_dir
.CreateUniqueTempDir());
622 crx_path
= temp_dir
.path().AppendASCII("temp.crx");
624 PackCRX(dir_path
, pem_path
, crx_path
);
625 return InstallCRX(crx_path
, install_state
, creation_flags
);
628 const Extension
* PackAndInstallCRX(const base::FilePath
& dir_path
,
629 const base::FilePath
& pem_path
,
630 InstallState install_state
) {
631 return PackAndInstallCRX(dir_path
, pem_path
, install_state
,
632 Extension::NO_FLAGS
);
635 const Extension
* PackAndInstallCRX(const base::FilePath
& dir_path
,
636 InstallState install_state
) {
637 return PackAndInstallCRX(dir_path
, base::FilePath(), install_state
,
638 Extension::NO_FLAGS
);
641 // Attempts to install an extension. Use INSTALL_FAILED if the installation
642 // is expected to fail.
643 // If |install_state| is INSTALL_UPDATED, and |expected_old_name| is
644 // non-empty, expects that the existing extension's title was
645 // |expected_old_name|.
646 const Extension
* InstallCRX(const base::FilePath
& path
,
647 InstallState install_state
,
649 const std::string
& expected_old_name
) {
650 InstallCRXInternal(path
, creation_flags
);
651 return VerifyCrxInstall(path
, install_state
, expected_old_name
);
654 // Attempts to install an extension. Use INSTALL_FAILED if the installation
655 // is expected to fail.
656 const Extension
* InstallCRX(const base::FilePath
& path
,
657 InstallState install_state
,
658 int creation_flags
) {
659 return InstallCRX(path
, install_state
, creation_flags
, std::string());
662 // Attempts to install an extension. Use INSTALL_FAILED if the installation
663 // is expected to fail.
664 const Extension
* InstallCRX(const base::FilePath
& path
,
665 InstallState install_state
) {
666 return InstallCRX(path
, install_state
, Extension::NO_FLAGS
);
669 const Extension
* InstallCRXFromWebStore(const base::FilePath
& path
,
670 InstallState install_state
) {
671 InstallCRXInternal(path
, Extension::FROM_WEBSTORE
);
672 return VerifyCrxInstall(path
, install_state
);
675 const Extension
* InstallCRXWithLocation(const base::FilePath
& crx_path
,
676 Manifest::Location install_location
,
677 InstallState install_state
) {
678 EXPECT_TRUE(base::PathExists(crx_path
))
679 << "Path does not exist: "<< crx_path
.value().c_str();
680 // no client (silent install)
681 scoped_refptr
<CrxInstaller
> installer(
682 CrxInstaller::CreateSilent(service()));
683 installer
->set_install_source(install_location
);
685 content::WindowedNotificationObserver
observer(
686 extensions::NOTIFICATION_CRX_INSTALLER_DONE
,
687 content::NotificationService::AllSources());
688 installer
->InstallCrx(crx_path
);
691 return VerifyCrxInstall(crx_path
, install_state
);
694 // Verifies the result of a CRX installation. Used by InstallCRX. Set the
695 // |install_state| to INSTALL_FAILED if the installation is expected to fail.
696 // Returns an Extension pointer if the install succeeded, NULL otherwise.
697 const Extension
* VerifyCrxInstall(const base::FilePath
& path
,
698 InstallState install_state
) {
699 return VerifyCrxInstall(path
, install_state
, std::string());
702 // Verifies the result of a CRX installation. Used by InstallCRX. Set the
703 // |install_state| to INSTALL_FAILED if the installation is expected to fail.
704 // If |install_state| is INSTALL_UPDATED, and |expected_old_name| is
705 // non-empty, expects that the existing extension's title was
706 // |expected_old_name|.
707 // Returns an Extension pointer if the install succeeded, NULL otherwise.
708 const Extension
* VerifyCrxInstall(const base::FilePath
& path
,
709 InstallState install_state
,
710 const std::string
& expected_old_name
) {
711 std::vector
<base::string16
> errors
= GetErrors();
712 const Extension
* extension
= NULL
;
713 if (install_state
!= INSTALL_FAILED
) {
714 if (install_state
== INSTALL_NEW
)
715 ++expected_extensions_count_
;
717 EXPECT_TRUE(installed_
) << path
.value();
718 // If and only if INSTALL_UPDATED, it should have the is_update flag.
719 EXPECT_EQ(install_state
== INSTALL_UPDATED
, was_update_
)
721 // If INSTALL_UPDATED, old_name_ should match the given string.
722 if (install_state
== INSTALL_UPDATED
&& !expected_old_name
.empty())
723 EXPECT_EQ(expected_old_name
, old_name_
);
724 EXPECT_EQ(0u, errors
.size()) << path
.value();
726 if (install_state
== INSTALL_WITHOUT_LOAD
) {
727 EXPECT_EQ(0u, loaded_
.size()) << path
.value();
729 EXPECT_EQ(1u, loaded_
.size()) << path
.value();
730 size_t actual_extension_count
=
731 registry()->enabled_extensions().size() +
732 registry()->disabled_extensions().size();
733 EXPECT_EQ(expected_extensions_count_
, actual_extension_count
) <<
735 extension
= loaded_
[0].get();
736 EXPECT_TRUE(service()->GetExtensionById(extension
->id(), false))
740 for (std::vector
<base::string16
>::iterator err
= errors
.begin();
741 err
!= errors
.end(); ++err
) {
745 EXPECT_FALSE(installed_
) << path
.value();
746 EXPECT_EQ(0u, loaded_
.size()) << path
.value();
747 EXPECT_EQ(1u, errors
.size()) << path
.value();
754 ExtensionErrorReporter::GetInstance()->ClearErrors();
767 void BlackListWebGL() {
768 static const std::string json_blacklist
=
770 " \"name\": \"gpu blacklist\",\n"
771 " \"version\": \"1.0\",\n"
775 " \"features\": [\"webgl\"]\n"
779 gpu::GPUInfo gpu_info
;
780 content::GpuDataManager::GetInstance()->InitializeForTesting(
781 json_blacklist
, gpu_info
);
784 // Grants all optional permissions stated in manifest to active permission
785 // set for extension |id|.
786 void GrantAllOptionalPermissions(const std::string
& id
) {
787 const Extension
* extension
= service()->GetInstalledExtension(id
);
788 scoped_refptr
<const PermissionSet
> all_optional_permissions
=
789 extensions::PermissionsParser::GetOptionalPermissions(extension
);
790 extensions::PermissionsUpdater
perms_updater(profile());
791 perms_updater
.AddPermissions(extension
, all_optional_permissions
.get());
794 // Helper method to set up a WindowedNotificationObserver to wait for a
795 // specific CrxInstaller to finish if we don't know the value of the
797 static bool IsCrxInstallerDone(extensions::CrxInstaller
** installer
,
798 const content::NotificationSource
& source
,
799 const content::NotificationDetails
& details
) {
800 return content::Source
<extensions::CrxInstaller
>(source
).ptr() ==
804 void PackCRXAndUpdateExtension(const std::string
& id
,
805 const base::FilePath
& dir_path
,
806 const base::FilePath
& pem_path
,
807 UpdateState expected_state
) {
808 base::ScopedTempDir temp_dir
;
809 EXPECT_TRUE(temp_dir
.CreateUniqueTempDir());
810 base::FilePath crx_path
= temp_dir
.path().AppendASCII("temp.crx");
812 PackCRX(dir_path
, pem_path
, crx_path
);
813 UpdateExtension(id
, crx_path
, expected_state
);
816 void UpdateExtension(const std::string
& id
,
817 const base::FilePath
& in_path
,
818 UpdateState expected_state
) {
819 ASSERT_TRUE(base::PathExists(in_path
));
821 // We need to copy this to a temporary location because Update() will delete
823 base::FilePath path
= temp_dir().path();
824 path
= path
.Append(in_path
.BaseName());
825 ASSERT_TRUE(base::CopyFile(in_path
, path
));
827 int previous_enabled_extension_count
=
828 registry()->enabled_extensions().size();
829 int previous_installed_extension_count
=
830 previous_enabled_extension_count
+
831 registry()->disabled_extensions().size();
833 extensions::CrxInstaller
* installer
= NULL
;
834 content::WindowedNotificationObserver
observer(
835 extensions::NOTIFICATION_CRX_INSTALLER_DONE
,
836 base::Bind(&IsCrxInstallerDone
, &installer
));
837 service()->UpdateExtension(extensions::CRXFileInfo(id
, path
), true,
843 base::RunLoop().RunUntilIdle();
845 std::vector
<base::string16
> errors
= GetErrors();
846 int error_count
= errors
.size();
847 int enabled_extension_count
= registry()->enabled_extensions().size();
848 int installed_extension_count
=
849 enabled_extension_count
+ registry()->disabled_extensions().size();
851 int expected_error_count
= (expected_state
== FAILED
) ? 1 : 0;
852 EXPECT_EQ(expected_error_count
, error_count
) << path
.value();
854 if (expected_state
<= FAILED
) {
855 EXPECT_EQ(previous_enabled_extension_count
,
856 enabled_extension_count
);
857 EXPECT_EQ(previous_installed_extension_count
,
858 installed_extension_count
);
860 int expected_installed_extension_count
=
861 (expected_state
>= INSTALLED
) ? 1 : 0;
862 int expected_enabled_extension_count
=
863 (expected_state
>= ENABLED
) ? 1 : 0;
864 EXPECT_EQ(expected_installed_extension_count
,
865 installed_extension_count
);
866 EXPECT_EQ(expected_enabled_extension_count
,
867 enabled_extension_count
);
870 // Update() should the temporary input file.
871 EXPECT_FALSE(base::PathExists(path
));
874 void TerminateExtension(const std::string
& id
) {
875 const Extension
* extension
= service()->GetInstalledExtension(id
);
880 service()->TrackTerminatedExtensionForTest(extension
);
883 testing::AssertionResult
IsBlocked(const std::string
& id
) {
884 scoped_ptr
<extensions::ExtensionSet
> all_unblocked_extensions
=
885 registry()->GenerateInstalledExtensionsSet(
886 ExtensionRegistry::EVERYTHING
& ~ExtensionRegistry::BLOCKED
);
887 if (all_unblocked_extensions
.get()->Contains(id
))
888 return testing::AssertionFailure() << id
<< " is still unblocked!";
889 if (!registry()->blocked_extensions().Contains(id
))
890 return testing::AssertionFailure() << id
<< " is not blocked!";
891 return testing::AssertionSuccess();
894 // Helper method to test that an extension moves through being blocked and
895 // unblocked as appropriate for its type.
896 void AssertExtensionBlocksAndUnblocks(
897 bool should_block
, const std::string extension_id
) {
898 // Assume we start in an unblocked state.
899 EXPECT_FALSE(IsBlocked(extension_id
));
901 // Block the extensions.
902 service()->BlockAllExtensions();
903 base::RunLoop().RunUntilIdle();
906 ASSERT_TRUE(IsBlocked(extension_id
));
908 ASSERT_FALSE(IsBlocked(extension_id
));
910 service()->UnblockAllExtensions();
911 base::RunLoop().RunUntilIdle();
913 ASSERT_FALSE(IsBlocked(extension_id
));
916 size_t GetPrefKeyCount() {
917 const base::DictionaryValue
* dict
=
918 profile()->GetPrefs()->GetDictionary("extensions.settings");
926 void UninstallExtension(const std::string
& id
, bool use_helper
) {
927 // Verify that the extension is installed.
928 base::FilePath extension_path
= extensions_install_dir().AppendASCII(id
);
929 EXPECT_TRUE(base::PathExists(extension_path
));
930 size_t pref_key_count
= GetPrefKeyCount();
931 EXPECT_GT(pref_key_count
, 0u);
932 ValidateIntegerPref(id
, "state", Extension::ENABLED
);
936 EXPECT_TRUE(ExtensionService::UninstallExtensionHelper(
937 service(), id
, extensions::UNINSTALL_REASON_FOR_TESTING
));
939 EXPECT_TRUE(service()->UninstallExtension(
941 extensions::UNINSTALL_REASON_FOR_TESTING
,
942 base::Bind(&base::DoNothing
),
945 --expected_extensions_count_
;
947 // We should get an unload notification.
948 EXPECT_FALSE(unloaded_id_
.empty());
949 EXPECT_EQ(id
, unloaded_id_
);
951 // Verify uninstalled state.
952 size_t new_pref_key_count
= GetPrefKeyCount();
953 if (new_pref_key_count
== pref_key_count
) {
954 ValidateIntegerPref(id
, "state",
955 Extension::EXTERNAL_EXTENSION_UNINSTALLED
);
957 EXPECT_EQ(new_pref_key_count
, pref_key_count
- 1);
960 // The extension should not be in the service anymore.
961 EXPECT_FALSE(service()->GetInstalledExtension(id
));
962 base::RunLoop().RunUntilIdle();
964 // The directory should be gone.
965 EXPECT_FALSE(base::PathExists(extension_path
));
968 void ValidatePrefKeyCount(size_t count
) {
969 EXPECT_EQ(count
, GetPrefKeyCount());
972 testing::AssertionResult
ValidateBooleanPref(
973 const std::string
& extension_id
,
974 const std::string
& pref_path
,
976 std::string msg
= "while checking: ";
981 msg
+= expected_val
? "true" : "false";
983 PrefService
* prefs
= profile()->GetPrefs();
984 const base::DictionaryValue
* dict
=
985 prefs
->GetDictionary("extensions.settings");
987 return testing::AssertionFailure()
988 << "extension.settings does not exist " << msg
;
991 const base::DictionaryValue
* pref
= NULL
;
992 if (!dict
->GetDictionary(extension_id
, &pref
)) {
993 return testing::AssertionFailure()
994 << "extension pref does not exist " << msg
;
998 if (!pref
->GetBoolean(pref_path
, &val
)) {
999 return testing::AssertionFailure()
1000 << pref_path
<< " pref not found " << msg
;
1003 return expected_val
== val
1004 ? testing::AssertionSuccess()
1005 : testing::AssertionFailure() << "base::Value is incorrect " << msg
;
1008 bool IsPrefExist(const std::string
& extension_id
,
1009 const std::string
& pref_path
) {
1010 const base::DictionaryValue
* dict
=
1011 profile()->GetPrefs()->GetDictionary("extensions.settings");
1012 if (dict
== NULL
) return false;
1013 const base::DictionaryValue
* pref
= NULL
;
1014 if (!dict
->GetDictionary(extension_id
, &pref
)) {
1021 if (!pref
->GetBoolean(pref_path
, &val
)) {
1027 void ValidateIntegerPref(const std::string
& extension_id
,
1028 const std::string
& pref_path
,
1030 std::string msg
= " while checking: ";
1031 msg
+= extension_id
;
1035 msg
+= base::IntToString(expected_val
);
1037 PrefService
* prefs
= profile()->GetPrefs();
1038 const base::DictionaryValue
* dict
=
1039 prefs
->GetDictionary("extensions.settings");
1040 ASSERT_TRUE(dict
!= NULL
) << msg
;
1041 const base::DictionaryValue
* pref
= NULL
;
1042 ASSERT_TRUE(dict
->GetDictionary(extension_id
, &pref
)) << msg
;
1043 EXPECT_TRUE(pref
!= NULL
) << msg
;
1045 ASSERT_TRUE(pref
->GetInteger(pref_path
, &val
)) << msg
;
1046 EXPECT_EQ(expected_val
, val
) << msg
;
1049 void ValidateStringPref(const std::string
& extension_id
,
1050 const std::string
& pref_path
,
1051 const std::string
& expected_val
) {
1052 std::string msg
= " while checking: ";
1053 msg
+= extension_id
;
1054 msg
+= ".manifest.";
1057 msg
+= expected_val
;
1059 const base::DictionaryValue
* dict
=
1060 profile()->GetPrefs()->GetDictionary("extensions.settings");
1061 ASSERT_TRUE(dict
!= NULL
) << msg
;
1062 const base::DictionaryValue
* pref
= NULL
;
1063 std::string manifest_path
= extension_id
+ ".manifest";
1064 ASSERT_TRUE(dict
->GetDictionary(manifest_path
, &pref
)) << msg
;
1065 EXPECT_TRUE(pref
!= NULL
) << msg
;
1067 ASSERT_TRUE(pref
->GetString(pref_path
, &val
)) << msg
;
1068 EXPECT_EQ(expected_val
, val
) << msg
;
1071 void SetPref(const std::string
& extension_id
,
1072 const std::string
& pref_path
,
1074 const std::string
& msg
) {
1075 DictionaryPrefUpdate
update(profile()->GetPrefs(), "extensions.settings");
1076 base::DictionaryValue
* dict
= update
.Get();
1077 ASSERT_TRUE(dict
!= NULL
) << msg
;
1078 base::DictionaryValue
* pref
= NULL
;
1079 ASSERT_TRUE(dict
->GetDictionary(extension_id
, &pref
)) << msg
;
1080 EXPECT_TRUE(pref
!= NULL
) << msg
;
1081 pref
->Set(pref_path
, value
);
1084 void SetPrefInteg(const std::string
& extension_id
,
1085 const std::string
& pref_path
,
1087 std::string msg
= " while setting: ";
1088 msg
+= extension_id
;
1092 msg
+= base::IntToString(value
);
1094 SetPref(extension_id
, pref_path
, new base::FundamentalValue(value
), msg
);
1097 void SetPrefBool(const std::string
& extension_id
,
1098 const std::string
& pref_path
,
1100 std::string msg
= " while setting: ";
1101 msg
+= extension_id
+ " " + pref_path
;
1103 msg
+= (value
? "true" : "false");
1105 SetPref(extension_id
, pref_path
, new base::FundamentalValue(value
), msg
);
1108 void ClearPref(const std::string
& extension_id
,
1109 const std::string
& pref_path
) {
1110 std::string msg
= " while clearing: ";
1111 msg
+= extension_id
+ " " + pref_path
;
1113 DictionaryPrefUpdate
update(profile()->GetPrefs(), "extensions.settings");
1114 base::DictionaryValue
* dict
= update
.Get();
1115 ASSERT_TRUE(dict
!= NULL
) << msg
;
1116 base::DictionaryValue
* pref
= NULL
;
1117 ASSERT_TRUE(dict
->GetDictionary(extension_id
, &pref
)) << msg
;
1118 EXPECT_TRUE(pref
!= NULL
) << msg
;
1119 pref
->Remove(pref_path
, NULL
);
1122 void SetPrefStringSet(const std::string
& extension_id
,
1123 const std::string
& pref_path
,
1124 const std::set
<std::string
>& value
) {
1125 std::string msg
= " while setting: ";
1126 msg
+= extension_id
+ " " + pref_path
;
1128 base::ListValue
* list_value
= new base::ListValue();
1129 for (std::set
<std::string
>::const_iterator iter
= value
.begin();
1130 iter
!= value
.end(); ++iter
)
1131 list_value
->Append(new base::StringValue(*iter
));
1133 SetPref(extension_id
, pref_path
, list_value
, msg
);
1136 void InitPluginService() {
1137 #if defined(ENABLE_PLUGINS)
1138 PluginService::GetInstance()->Init();
1142 void InitializeExtensionSyncService() {
1143 extension_sync_service_
.reset(new ExtensionSyncService(
1144 profile(), ExtensionPrefs::Get(browser_context()), service()));
1147 void InitializeEmptyExtensionServiceWithTestingPrefs() {
1148 ExtensionServiceTestBase::ExtensionServiceInitParams params
=
1149 CreateDefaultInitParams();
1150 params
.pref_file
= base::FilePath();
1151 InitializeExtensionService(params
);
1154 extensions::ManagementPolicy
* GetManagementPolicy() {
1155 return ExtensionSystem::Get(browser_context())->management_policy();
1158 ExtensionSyncService
* extension_sync_service() {
1159 return extension_sync_service_
.get();
1163 typedef extensions::ExtensionManagementPrefUpdater
<TestingPrefServiceSyncable
>
1164 ManagementPrefUpdater
;
1165 scoped_ptr
<ExtensionSyncService
> extension_sync_service_
;
1166 extensions::ExtensionList loaded_
;
1167 std::string unloaded_id_
;
1168 UnloadedExtensionInfo::Reason unloaded_reason_
;
1169 const Extension
* installed_
;
1171 std::string old_name_
;
1172 FeatureSwitch::ScopedOverride override_external_install_prompt_
;
1175 // Create a CrxInstaller and install the CRX file.
1176 // Instead of calling this method yourself, use InstallCRX(), which does extra
1178 void InstallCRXInternal(const base::FilePath
& crx_path
) {
1179 InstallCRXInternal(crx_path
, Extension::NO_FLAGS
);
1182 void InstallCRXInternal(const base::FilePath
& crx_path
, int creation_flags
) {
1183 ASSERT_TRUE(base::PathExists(crx_path
))
1184 << "Path does not exist: "<< crx_path
.value().c_str();
1185 scoped_refptr
<CrxInstaller
> installer(
1186 CrxInstaller::CreateSilent(service()));
1187 installer
->set_creation_flags(creation_flags
);
1188 if (!(creation_flags
& Extension::WAS_INSTALLED_BY_DEFAULT
))
1189 installer
->set_allow_silent_install(true);
1191 content::WindowedNotificationObserver
observer(
1192 extensions::NOTIFICATION_CRX_INSTALLER_DONE
,
1193 content::Source
<extensions::CrxInstaller
>(installer
.get()));
1195 installer
->InstallCrx(crx_path
);
1200 size_t expected_extensions_count_
;
1201 content::NotificationRegistrar registrar_
;
1204 // Receives notifications from a PackExtensionJob, indicating either that
1205 // packing succeeded or that there was some error.
1206 class PackExtensionTestClient
: public extensions::PackExtensionJob::Client
{
1208 PackExtensionTestClient(const base::FilePath
& expected_crx_path
,
1209 const base::FilePath
& expected_private_key_path
);
1210 void OnPackSuccess(const base::FilePath
& crx_path
,
1211 const base::FilePath
& private_key_path
) override
;
1212 void OnPackFailure(const std::string
& error_message
,
1213 ExtensionCreator::ErrorType type
) override
;
1216 const base::FilePath expected_crx_path_
;
1217 const base::FilePath expected_private_key_path_
;
1218 DISALLOW_COPY_AND_ASSIGN(PackExtensionTestClient
);
1221 PackExtensionTestClient::PackExtensionTestClient(
1222 const base::FilePath
& expected_crx_path
,
1223 const base::FilePath
& expected_private_key_path
)
1224 : expected_crx_path_(expected_crx_path
),
1225 expected_private_key_path_(expected_private_key_path
) {}
1227 // If packing succeeded, we make sure that the package names match our
1229 void PackExtensionTestClient::OnPackSuccess(
1230 const base::FilePath
& crx_path
,
1231 const base::FilePath
& private_key_path
) {
1232 // We got the notification and processed it; we don't expect any further tasks
1233 // to be posted to the current thread, so we should stop blocking and continue
1234 // on with the rest of the test.
1235 // This call to |Quit()| matches the call to |Run()| in the
1236 // |PackPunctuatedExtension| test.
1237 base::MessageLoop::current()->Quit();
1238 EXPECT_EQ(expected_crx_path_
.value(), crx_path
.value());
1239 EXPECT_EQ(expected_private_key_path_
.value(), private_key_path
.value());
1240 ASSERT_TRUE(base::PathExists(private_key_path
));
1243 // The tests are designed so that we never expect to see a packing error.
1244 void PackExtensionTestClient::OnPackFailure(const std::string
& error_message
,
1245 ExtensionCreator::ErrorType type
) {
1246 if (type
== ExtensionCreator::kCRXExists
)
1247 FAIL() << "Packing should not fail.";
1249 FAIL() << "Existing CRX should have been overwritten.";
1252 // Test loading good extensions from the profile directory.
1253 TEST_F(ExtensionServiceTest
, LoadAllExtensionsFromDirectorySuccess
) {
1254 InitPluginService();
1255 InitializeGoodInstalledExtensionService();
1258 uint32 expected_num_extensions
= 3u;
1259 ASSERT_EQ(expected_num_extensions
, loaded_
.size());
1261 EXPECT_EQ(std::string(good0
), loaded_
[0]->id());
1262 EXPECT_EQ(std::string("My extension 1"),
1263 loaded_
[0]->name());
1264 EXPECT_EQ(std::string("The first extension that I made."),
1265 loaded_
[0]->description());
1266 EXPECT_EQ(Manifest::INTERNAL
, loaded_
[0]->location());
1267 EXPECT_TRUE(service()->GetExtensionById(loaded_
[0]->id(), false));
1268 EXPECT_EQ(expected_num_extensions
, registry()->enabled_extensions().size());
1270 ValidatePrefKeyCount(3);
1271 ValidateIntegerPref(good0
, "state", Extension::ENABLED
);
1272 ValidateIntegerPref(good0
, "location", Manifest::INTERNAL
);
1273 ValidateIntegerPref(good1
, "state", Extension::ENABLED
);
1274 ValidateIntegerPref(good1
, "location", Manifest::INTERNAL
);
1275 ValidateIntegerPref(good2
, "state", Extension::ENABLED
);
1276 ValidateIntegerPref(good2
, "location", Manifest::INTERNAL
);
1278 URLPatternSet expected_patterns
;
1279 AddPattern(&expected_patterns
, "file:///*");
1280 AddPattern(&expected_patterns
, "http://*.google.com/*");
1281 AddPattern(&expected_patterns
, "https://*.google.com/*");
1282 const Extension
* extension
= loaded_
[0].get();
1283 const extensions::UserScriptList
& scripts
=
1284 extensions::ContentScriptsInfo::GetContentScripts(extension
);
1285 ASSERT_EQ(2u, scripts
.size());
1286 EXPECT_EQ(expected_patterns
, scripts
[0].url_patterns());
1287 EXPECT_EQ(2u, scripts
[0].js_scripts().size());
1288 ExtensionResource
resource00(extension
->id(),
1289 scripts
[0].js_scripts()[0].extension_root(),
1290 scripts
[0].js_scripts()[0].relative_path());
1291 base::FilePath expected_path
=
1292 base::MakeAbsoluteFilePath(extension
->path().AppendASCII("script1.js"));
1293 EXPECT_TRUE(resource00
.ComparePathWithDefault(expected_path
));
1294 ExtensionResource
resource01(extension
->id(),
1295 scripts
[0].js_scripts()[1].extension_root(),
1296 scripts
[0].js_scripts()[1].relative_path());
1298 base::MakeAbsoluteFilePath(extension
->path().AppendASCII("script2.js"));
1299 EXPECT_TRUE(resource01
.ComparePathWithDefault(expected_path
));
1300 EXPECT_TRUE(!extensions::PluginInfo::HasPlugins(extension
));
1301 EXPECT_EQ(1u, scripts
[1].url_patterns().patterns().size());
1302 EXPECT_EQ("http://*.news.com/*",
1303 scripts
[1].url_patterns().begin()->GetAsString());
1304 ExtensionResource
resource10(extension
->id(),
1305 scripts
[1].js_scripts()[0].extension_root(),
1306 scripts
[1].js_scripts()[0].relative_path());
1308 extension
->path().AppendASCII("js_files").AppendASCII("script3.js");
1309 expected_path
= base::MakeAbsoluteFilePath(expected_path
);
1310 EXPECT_TRUE(resource10
.ComparePathWithDefault(expected_path
));
1312 expected_patterns
.ClearPatterns();
1313 AddPattern(&expected_patterns
, "http://*.google.com/*");
1314 AddPattern(&expected_patterns
, "https://*.google.com/*");
1317 extension
->permissions_data()->active_permissions()->explicit_hosts());
1319 EXPECT_EQ(std::string(good1
), loaded_
[1]->id());
1320 EXPECT_EQ(std::string("My extension 2"), loaded_
[1]->name());
1321 EXPECT_EQ(std::string(), loaded_
[1]->description());
1322 EXPECT_EQ(loaded_
[1]->GetResourceURL("background.html"),
1323 extensions::BackgroundInfo::GetBackgroundURL(loaded_
[1].get()));
1325 extensions::ContentScriptsInfo::GetContentScripts(loaded_
[1].get())
1328 // We don't parse the plugins section on Chrome OS.
1329 #if defined(OS_CHROMEOS)
1330 EXPECT_TRUE(!extensions::PluginInfo::HasPlugins(loaded_
[1].get()));
1332 ASSERT_TRUE(extensions::PluginInfo::HasPlugins(loaded_
[1].get()));
1333 const std::vector
<extensions::PluginInfo
>* plugins
=
1334 extensions::PluginInfo::GetPlugins(loaded_
[1].get());
1335 ASSERT_TRUE(plugins
);
1336 ASSERT_EQ(2u, plugins
->size());
1337 EXPECT_EQ(loaded_
[1]->path().AppendASCII("content_plugin.dll").value(),
1338 plugins
->at(0).path
.value());
1339 EXPECT_TRUE(plugins
->at(0).is_public
);
1340 EXPECT_EQ(loaded_
[1]->path().AppendASCII("extension_plugin.dll").value(),
1341 plugins
->at(1).path
.value());
1342 EXPECT_FALSE(plugins
->at(1).is_public
);
1345 EXPECT_EQ(Manifest::INTERNAL
, loaded_
[1]->location());
1347 int index
= expected_num_extensions
- 1;
1348 EXPECT_EQ(std::string(good2
), loaded_
[index
]->id());
1349 EXPECT_EQ(std::string("My extension 3"), loaded_
[index
]->name());
1350 EXPECT_EQ(std::string(), loaded_
[index
]->description());
1352 extensions::ContentScriptsInfo::GetContentScripts(
1353 loaded_
[index
].get()).size());
1354 EXPECT_EQ(Manifest::INTERNAL
, loaded_
[index
]->location());
1357 // Test loading bad extensions from the profile directory.
1358 TEST_F(ExtensionServiceTest
, LoadAllExtensionsFromDirectoryFail
) {
1359 // Initialize the test dir with a bad Preferences/extensions.
1360 base::FilePath source_install_dir
=
1361 data_dir().AppendASCII("bad").AppendASCII("Extensions");
1362 base::FilePath pref_path
=
1363 source_install_dir
.DirName().Append(chrome::kPreferencesFilename
);
1365 InitializeInstalledExtensionService(pref_path
, source_install_dir
);
1369 ASSERT_EQ(4u, GetErrors().size());
1370 ASSERT_EQ(0u, loaded_
.size());
1372 EXPECT_TRUE(MatchPattern(base::UTF16ToUTF8(GetErrors()[0]),
1373 l10n_util::GetStringUTF8(IDS_EXTENSIONS_LOAD_ERROR_MESSAGE
) + " *. " +
1374 extensions::manifest_errors::kManifestUnreadable
)) <<
1375 base::UTF16ToUTF8(GetErrors()[0]);
1377 EXPECT_TRUE(MatchPattern(base::UTF16ToUTF8(GetErrors()[1]),
1378 l10n_util::GetStringUTF8(IDS_EXTENSIONS_LOAD_ERROR_MESSAGE
) + " *. " +
1379 extensions::manifest_errors::kManifestUnreadable
)) <<
1380 base::UTF16ToUTF8(GetErrors()[1]);
1382 EXPECT_TRUE(MatchPattern(base::UTF16ToUTF8(GetErrors()[2]),
1383 l10n_util::GetStringUTF8(IDS_EXTENSIONS_LOAD_ERROR_MESSAGE
) + " *. " +
1384 extensions::manifest_errors::kMissingFile
)) <<
1385 base::UTF16ToUTF8(GetErrors()[2]);
1387 EXPECT_TRUE(MatchPattern(base::UTF16ToUTF8(GetErrors()[3]),
1388 l10n_util::GetStringUTF8(IDS_EXTENSIONS_LOAD_ERROR_MESSAGE
) + " *. " +
1389 extensions::manifest_errors::kManifestUnreadable
)) <<
1390 base::UTF16ToUTF8(GetErrors()[3]);
1393 // Test various cases for delayed install because of missing imports.
1394 TEST_F(ExtensionServiceTest
, PendingImports
) {
1395 InitPluginService();
1397 base::FilePath source_install_dir
=
1398 data_dir().AppendASCII("pending_updates_with_imports").AppendASCII(
1400 base::FilePath pref_path
=
1401 source_install_dir
.DirName().Append(chrome::kPreferencesFilename
);
1403 InitializeInstalledExtensionService(pref_path
, source_install_dir
);
1405 // Verify there are no pending extensions initially.
1406 EXPECT_FALSE(service()->pending_extension_manager()->HasPendingExtensions());
1409 // Wait for GarbageCollectExtensions task to complete.
1410 base::RunLoop().RunUntilIdle();
1412 // These extensions are used by the extensions we test below, they must be
1414 EXPECT_TRUE(base::PathExists(extensions_install_dir().AppendASCII(
1415 "bjafgdebaacbbbecmhlhpofkepfkgcpa/1.0")));
1416 EXPECT_TRUE(base::PathExists(extensions_install_dir().AppendASCII(
1417 "hpiknbiabeeppbpihjehijgoemciehgk/2")));
1419 // Each of these extensions should have been rejected because of dependencies
1420 // that cannot be satisfied.
1421 ExtensionPrefs
* prefs
= ExtensionPrefs::Get(profile());
1423 prefs
->GetDelayedInstallInfo("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"));
1425 prefs
->GetInstalledExtensionInfo("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"));
1427 prefs
->GetDelayedInstallInfo("bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"));
1429 prefs
->GetInstalledExtensionInfo("bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"));
1431 prefs
->GetDelayedInstallInfo("cccccccccccccccccccccccccccccccc"));
1433 prefs
->GetInstalledExtensionInfo("cccccccccccccccccccccccccccccccc"));
1435 // Make sure the import started for the extension with a dependency.
1437 prefs
->GetDelayedInstallInfo("behllobkkfkfnphdnhnkndlbkcpglgmj"));
1438 EXPECT_EQ(ExtensionPrefs::DELAY_REASON_WAIT_FOR_IMPORTS
,
1439 prefs
->GetDelayedInstallReason("behllobkkfkfnphdnhnkndlbkcpglgmj"));
1441 EXPECT_FALSE(base::PathExists(extensions_install_dir().AppendASCII(
1442 "behllobkkfkfnphdnhnkndlbkcpglgmj/1.0.0.0")));
1444 EXPECT_TRUE(service()->pending_extension_manager()->HasPendingExtensions());
1445 std::string
pending_id("eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee");
1446 EXPECT_TRUE(service()->pending_extension_manager()->IsIdPending(pending_id
));
1447 // Remove it because we are not testing the pending extension manager's
1448 // ability to download and install extensions.
1449 EXPECT_TRUE(service()->pending_extension_manager()->Remove(pending_id
));
1452 // Test installing extensions. This test tries to install few extensions using
1453 // crx files. If you need to change those crx files, feel free to repackage
1454 // them, throw away the key used and change the id's above.
1455 TEST_F(ExtensionServiceTest
, InstallExtension
) {
1456 InitializeEmptyExtensionService();
1458 // Extensions not enabled.
1459 service()->set_extensions_enabled(false);
1460 base::FilePath path
= data_dir().AppendASCII("good.crx");
1461 InstallCRX(path
, INSTALL_FAILED
);
1462 service()->set_extensions_enabled(true);
1464 ValidatePrefKeyCount(0);
1466 // A simple extension that should install without error.
1467 path
= data_dir().AppendASCII("good.crx");
1468 InstallCRX(path
, INSTALL_NEW
);
1469 // TODO(erikkay): verify the contents of the installed extension.
1472 ValidatePrefKeyCount(++pref_count
);
1473 ValidateIntegerPref(good_crx
, "state", Extension::ENABLED
);
1474 ValidateIntegerPref(good_crx
, "location", Manifest::INTERNAL
);
1476 // An extension with page actions.
1477 path
= data_dir().AppendASCII("page_action.crx");
1478 InstallCRX(path
, INSTALL_NEW
);
1479 ValidatePrefKeyCount(++pref_count
);
1480 ValidateIntegerPref(page_action
, "state", Extension::ENABLED
);
1481 ValidateIntegerPref(page_action
, "location", Manifest::INTERNAL
);
1484 path
= data_dir().AppendASCII("bad_signature.crx");
1485 InstallCRX(path
, INSTALL_FAILED
);
1486 ValidatePrefKeyCount(pref_count
);
1488 // 0-length extension file.
1489 path
= data_dir().AppendASCII("not_an_extension.crx");
1490 InstallCRX(path
, INSTALL_FAILED
);
1491 ValidatePrefKeyCount(pref_count
);
1493 // Bad magic number.
1494 path
= data_dir().AppendASCII("bad_magic.crx");
1495 InstallCRX(path
, INSTALL_FAILED
);
1496 ValidatePrefKeyCount(pref_count
);
1498 // Packed extensions may have folders or files that have underscores.
1499 // This will only cause a warning, rather than a fatal error.
1500 path
= data_dir().AppendASCII("bad_underscore.crx");
1501 InstallCRX(path
, INSTALL_NEW
);
1502 ValidatePrefKeyCount(++pref_count
);
1504 // A test for an extension with a 2048-bit public key.
1505 path
= data_dir().AppendASCII("good2048.crx");
1506 InstallCRX(path
, INSTALL_NEW
);
1507 ValidatePrefKeyCount(++pref_count
);
1508 ValidateIntegerPref(good2048
, "state", Extension::ENABLED
);
1509 ValidateIntegerPref(good2048
, "location", Manifest::INTERNAL
);
1511 // TODO(erikkay): add more tests for many of the failure cases.
1512 // TODO(erikkay): add tests for upgrade cases.
1515 struct MockExtensionRegistryObserver
1516 : public extensions::ExtensionRegistryObserver
{
1517 void OnExtensionWillBeInstalled(content::BrowserContext
* browser_context
,
1518 const Extension
* extension
,
1520 bool from_ephemeral
,
1521 const std::string
& old_name
) override
{
1522 last_extension_installed
= extension
->id();
1525 void OnExtensionUninstalled(content::BrowserContext
* browser_context
,
1526 const Extension
* extension
,
1527 extensions::UninstallReason reason
) override
{
1528 last_extension_uninstalled
= extension
->id();
1531 std::string last_extension_installed
;
1532 std::string last_extension_uninstalled
;
1535 // Test that correct notifications are sent to ExtensionRegistryObserver on
1536 // extension install and uninstall.
1537 TEST_F(ExtensionServiceTest
, InstallObserverNotified
) {
1538 InitializeEmptyExtensionService();
1540 extensions::ExtensionRegistry
* registry(
1541 extensions::ExtensionRegistry::Get(profile()));
1542 MockExtensionRegistryObserver observer
;
1543 registry
->AddObserver(&observer
);
1545 // A simple extension that should install without error.
1546 ASSERT_TRUE(observer
.last_extension_installed
.empty());
1547 base::FilePath path
= data_dir().AppendASCII("good.crx");
1548 InstallCRX(path
, INSTALL_NEW
);
1549 ASSERT_EQ(good_crx
, observer
.last_extension_installed
);
1551 // Uninstall the extension.
1552 ASSERT_TRUE(observer
.last_extension_uninstalled
.empty());
1553 UninstallExtension(good_crx
, false);
1554 ASSERT_EQ(good_crx
, observer
.last_extension_uninstalled
);
1556 registry
->RemoveObserver(&observer
);
1559 // Tests that flags passed to OnExternalExtensionFileFound() make it to the
1560 // extension object.
1561 TEST_F(ExtensionServiceTest
, InstallingExternalExtensionWithFlags
) {
1562 const char kPrefFromBookmark
[] = "from_bookmark";
1564 InitializeEmptyExtensionService();
1566 base::FilePath path
= data_dir().AppendASCII("good.crx");
1567 service()->set_extensions_enabled(true);
1569 // Register and install an external extension.
1570 Version
version("1.0.0.0");
1571 content::WindowedNotificationObserver
observer(
1572 extensions::NOTIFICATION_CRX_INSTALLER_DONE
,
1573 content::NotificationService::AllSources());
1574 if (service()->OnExternalExtensionFileFound(
1578 Manifest::EXTERNAL_PREF
,
1579 Extension::FROM_BOOKMARK
,
1580 false /* mark_acknowledged */,
1581 false /* install_immediately */)) {
1585 const Extension
* extension
= service()->GetExtensionById(good_crx
, false);
1586 ASSERT_TRUE(extension
);
1587 ASSERT_TRUE(extension
->from_bookmark());
1588 ASSERT_TRUE(ValidateBooleanPref(good_crx
, kPrefFromBookmark
, true));
1590 // Upgrade to version 2.0, the flag should be preserved.
1591 path
= data_dir().AppendASCII("good2.crx");
1592 UpdateExtension(good_crx
, path
, ENABLED
);
1593 ASSERT_TRUE(ValidateBooleanPref(good_crx
, kPrefFromBookmark
, true));
1594 extension
= service()->GetExtensionById(good_crx
, false);
1595 ASSERT_TRUE(extension
);
1596 ASSERT_TRUE(extension
->from_bookmark());
1599 // Test the handling of Extension::EXTERNAL_EXTENSION_UNINSTALLED
1600 TEST_F(ExtensionServiceTest
, UninstallingExternalExtensions
) {
1601 InitializeEmptyExtensionService();
1603 base::FilePath path
= data_dir().AppendASCII("good.crx");
1604 service()->set_extensions_enabled(true);
1606 // Install an external extension.
1607 Version
version("1.0.0.0");
1608 content::WindowedNotificationObserver
observer(
1609 extensions::NOTIFICATION_CRX_INSTALLER_DONE
,
1610 content::NotificationService::AllSources());
1611 if (service()->OnExternalExtensionFileFound(good_crx
,
1614 Manifest::EXTERNAL_PREF
,
1615 Extension::NO_FLAGS
,
1621 ASSERT_TRUE(service()->GetExtensionById(good_crx
, false));
1623 // Uninstall it and check that its killbit gets set.
1624 UninstallExtension(good_crx
, false);
1625 ValidateIntegerPref(good_crx
, "state",
1626 Extension::EXTERNAL_EXTENSION_UNINSTALLED
);
1628 // Try to re-install it externally. This should fail because of the killbit.
1629 service()->OnExternalExtensionFileFound(good_crx
,
1632 Manifest::EXTERNAL_PREF
,
1633 Extension::NO_FLAGS
,
1636 base::RunLoop().RunUntilIdle();
1637 ASSERT_TRUE(NULL
== service()->GetExtensionById(good_crx
, false));
1638 ValidateIntegerPref(good_crx
, "state",
1639 Extension::EXTERNAL_EXTENSION_UNINSTALLED
);
1641 version
= Version("1.0.0.1");
1642 // Repeat the same thing with a newer version of the extension.
1643 path
= data_dir().AppendASCII("good2.crx");
1644 service()->OnExternalExtensionFileFound(good_crx
,
1647 Manifest::EXTERNAL_PREF
,
1648 Extension::NO_FLAGS
,
1651 base::RunLoop().RunUntilIdle();
1652 ASSERT_TRUE(NULL
== service()->GetExtensionById(good_crx
, false));
1653 ValidateIntegerPref(good_crx
, "state",
1654 Extension::EXTERNAL_EXTENSION_UNINSTALLED
);
1656 // Try adding the same extension from an external update URL.
1657 ASSERT_FALSE(service()->pending_extension_manager()->AddFromExternalUpdateUrl(
1660 GURL("http:://fake.update/url"),
1661 Manifest::EXTERNAL_PREF_DOWNLOAD
,
1662 Extension::NO_FLAGS
,
1665 ASSERT_FALSE(service()->pending_extension_manager()->IsIdPending(good_crx
));
1668 // Test that uninstalling an external extension does not crash when
1669 // the extension could not be loaded.
1670 // This extension shown in preferences file requires an experimental permission.
1671 // It could not be loaded without such permission.
1672 TEST_F(ExtensionServiceTest
, UninstallingNotLoadedExtension
) {
1673 base::FilePath source_install_dir
=
1674 data_dir().AppendASCII("good").AppendASCII("Extensions");
1675 // The preference contains an external extension
1676 // that requires 'experimental' permission.
1677 base::FilePath pref_path
= source_install_dir
1679 .AppendASCII("PreferencesExperimental");
1681 // Aforementioned extension will not be loaded if
1682 // there is no '--enable-experimental-extension-apis' command line flag.
1683 InitializeInstalledExtensionService(pref_path
, source_install_dir
);
1687 // Check and try to uninstall it.
1688 // If we don't check whether the extension is loaded before we uninstall it
1689 // in CheckExternalUninstall, a crash will happen here because we will get or
1690 // dereference a NULL pointer (extension) inside UninstallExtension.
1691 MockExtensionProvider
provider(NULL
, Manifest::EXTERNAL_REGISTRY
);
1692 service()->OnExternalProviderReady(&provider
);
1695 // Test that external extensions with incorrect IDs are not installed.
1696 TEST_F(ExtensionServiceTest
, FailOnWrongId
) {
1697 InitializeEmptyExtensionService();
1698 base::FilePath path
= data_dir().AppendASCII("good.crx");
1699 service()->set_extensions_enabled(true);
1701 Version
version("1.0.0.0");
1703 const std::string wrong_id
= all_zero
;
1704 const std::string correct_id
= good_crx
;
1705 ASSERT_NE(correct_id
, wrong_id
);
1707 // Install an external extension with an ID from the external
1708 // source that is not equal to the ID in the extension manifest.
1709 content::WindowedNotificationObserver
observer(
1710 extensions::NOTIFICATION_CRX_INSTALLER_DONE
,
1711 content::NotificationService::AllSources());
1712 service()->OnExternalExtensionFileFound(wrong_id
,
1715 Manifest::EXTERNAL_PREF
,
1716 Extension::NO_FLAGS
,
1721 ASSERT_FALSE(service()->GetExtensionById(good_crx
, false));
1723 // Try again with the right ID. Expect success.
1724 content::WindowedNotificationObserver
observer2(
1725 extensions::NOTIFICATION_CRX_INSTALLER_DONE
,
1726 content::NotificationService::AllSources());
1727 if (service()->OnExternalExtensionFileFound(correct_id
,
1730 Manifest::EXTERNAL_PREF
,
1731 Extension::NO_FLAGS
,
1736 ASSERT_TRUE(service()->GetExtensionById(good_crx
, false));
1739 // Test that external extensions with incorrect versions are not installed.
1740 TEST_F(ExtensionServiceTest
, FailOnWrongVersion
) {
1741 InitializeEmptyExtensionService();
1742 base::FilePath path
= data_dir().AppendASCII("good.crx");
1743 service()->set_extensions_enabled(true);
1745 // Install an external extension with a version from the external
1746 // source that is not equal to the version in the extension manifest.
1747 Version
wrong_version("1.2.3.4");
1748 content::WindowedNotificationObserver
observer(
1749 extensions::NOTIFICATION_CRX_INSTALLER_DONE
,
1750 content::NotificationService::AllSources());
1751 service()->OnExternalExtensionFileFound(good_crx
,
1754 Manifest::EXTERNAL_PREF
,
1755 Extension::NO_FLAGS
,
1760 ASSERT_FALSE(service()->GetExtensionById(good_crx
, false));
1762 // Try again with the right version. Expect success.
1763 service()->pending_extension_manager()->Remove(good_crx
);
1764 Version
correct_version("1.0.0.0");
1765 content::WindowedNotificationObserver
observer2(
1766 extensions::NOTIFICATION_CRX_INSTALLER_DONE
,
1767 content::NotificationService::AllSources());
1768 if (service()->OnExternalExtensionFileFound(good_crx
,
1771 Manifest::EXTERNAL_PREF
,
1772 Extension::NO_FLAGS
,
1777 ASSERT_TRUE(service()->GetExtensionById(good_crx
, false));
1780 // Install a user script (they get converted automatically to an extension)
1781 TEST_F(ExtensionServiceTest
, InstallUserScript
) {
1782 // The details of script conversion are tested elsewhere, this just tests
1783 // integration with ExtensionService.
1784 InitializeEmptyExtensionService();
1786 base::FilePath path
= data_dir().AppendASCII("user_script_basic.user.js");
1788 ASSERT_TRUE(base::PathExists(path
));
1789 scoped_refptr
<CrxInstaller
> installer(CrxInstaller::CreateSilent(service()));
1790 installer
->set_allow_silent_install(true);
1791 installer
->InstallUserScript(
1793 GURL("http://www.aaronboodman.com/scripts/user_script_basic.user.js"));
1795 base::RunLoop().RunUntilIdle();
1796 std::vector
<base::string16
> errors
= GetErrors();
1797 EXPECT_TRUE(installed_
) << "Nothing was installed.";
1798 EXPECT_FALSE(was_update_
) << path
.value();
1799 ASSERT_EQ(1u, loaded_
.size()) << "Nothing was loaded.";
1800 EXPECT_EQ(0u, errors
.size()) << "There were errors: "
1801 << JoinString(errors
, ',');
1802 EXPECT_TRUE(service()->GetExtensionById(loaded_
[0]->id(), false))
1806 was_update_
= false;
1808 ExtensionErrorReporter::GetInstance()->ClearErrors();
1811 // Extensions don't install during shutdown.
1812 TEST_F(ExtensionServiceTest
, InstallExtensionDuringShutdown
) {
1813 InitializeEmptyExtensionService();
1815 // Simulate shutdown.
1816 service()->set_browser_terminating_for_test(true);
1818 base::FilePath path
= data_dir().AppendASCII("good.crx");
1819 scoped_refptr
<CrxInstaller
> installer(CrxInstaller::CreateSilent(service()));
1820 installer
->set_allow_silent_install(true);
1821 installer
->InstallCrx(path
);
1822 base::RunLoop().RunUntilIdle();
1824 EXPECT_FALSE(installed_
) << "Extension installed during shutdown.";
1825 ASSERT_EQ(0u, loaded_
.size()) << "Extension loaded during shutdown.";
1828 // This tests that the granted permissions preferences are correctly set when
1829 // installing an extension.
1830 TEST_F(ExtensionServiceTest
, GrantedPermissions
) {
1831 InitializeEmptyExtensionService();
1832 base::FilePath path
= data_dir().AppendASCII("permissions");
1834 base::FilePath pem_path
= path
.AppendASCII("unknown.pem");
1835 path
= path
.AppendASCII("unknown");
1837 ASSERT_TRUE(base::PathExists(pem_path
));
1838 ASSERT_TRUE(base::PathExists(path
));
1840 ExtensionPrefs
* prefs
= ExtensionPrefs::Get(profile());
1842 APIPermissionSet expected_api_perms
;
1843 URLPatternSet expected_host_perms
;
1845 // Make sure there aren't any granted permissions before the
1846 // extension is installed.
1847 scoped_refptr
<PermissionSet
> known_perms(
1848 prefs
->GetGrantedPermissions(permissions_crx
));
1849 EXPECT_FALSE(known_perms
.get());
1851 const Extension
* extension
= PackAndInstallCRX(path
, pem_path
, INSTALL_NEW
);
1853 EXPECT_EQ(0u, GetErrors().size());
1854 ASSERT_EQ(1u, registry()->enabled_extensions().size());
1855 EXPECT_EQ(permissions_crx
, extension
->id());
1857 // Verify that the valid API permissions have been recognized.
1858 expected_api_perms
.insert(APIPermission::kTab
);
1860 AddPattern(&expected_host_perms
, "http://*.google.com/*");
1861 AddPattern(&expected_host_perms
, "https://*.google.com/*");
1862 AddPattern(&expected_host_perms
, "http://*.google.com.hk/*");
1863 AddPattern(&expected_host_perms
, "http://www.example.com/*");
1865 known_perms
= prefs
->GetGrantedPermissions(extension
->id());
1866 EXPECT_TRUE(known_perms
.get());
1867 EXPECT_FALSE(known_perms
->IsEmpty());
1868 EXPECT_EQ(expected_api_perms
, known_perms
->apis());
1869 EXPECT_FALSE(known_perms
->HasEffectiveFullAccess());
1870 EXPECT_EQ(expected_host_perms
, known_perms
->effective_hosts());
1874 #if !defined(OS_CHROMEOS)
1875 // This tests that the granted permissions preferences are correctly set for
1877 TEST_F(ExtensionServiceTest
, DefaultAppsGrantedPermissions
) {
1878 InitializeEmptyExtensionService();
1879 base::FilePath path
= data_dir().AppendASCII("permissions");
1881 base::FilePath pem_path
= path
.AppendASCII("unknown.pem");
1882 path
= path
.AppendASCII("unknown");
1884 ASSERT_TRUE(base::PathExists(pem_path
));
1885 ASSERT_TRUE(base::PathExists(path
));
1887 ExtensionPrefs
* prefs
= ExtensionPrefs::Get(profile());
1889 APIPermissionSet expected_api_perms
;
1890 URLPatternSet expected_host_perms
;
1892 // Make sure there aren't any granted permissions before the
1893 // extension is installed.
1894 scoped_refptr
<PermissionSet
> known_perms(
1895 prefs
->GetGrantedPermissions(permissions_crx
));
1896 EXPECT_FALSE(known_perms
.get());
1898 const Extension
* extension
= PackAndInstallCRX(
1899 path
, pem_path
, INSTALL_NEW
, Extension::WAS_INSTALLED_BY_DEFAULT
);
1901 EXPECT_EQ(0u, GetErrors().size());
1902 ASSERT_EQ(1u, registry()->enabled_extensions().size());
1903 EXPECT_EQ(permissions_crx
, extension
->id());
1905 // Verify that the valid API permissions have been recognized.
1906 expected_api_perms
.insert(APIPermission::kTab
);
1908 known_perms
= prefs
->GetGrantedPermissions(extension
->id());
1909 EXPECT_TRUE(known_perms
.get());
1910 EXPECT_FALSE(known_perms
->IsEmpty());
1911 EXPECT_EQ(expected_api_perms
, known_perms
->apis());
1912 EXPECT_FALSE(known_perms
->HasEffectiveFullAccess());
1916 #if !defined(OS_POSIX) || defined(OS_MACOSX)
1917 // Tests that the granted permissions full_access bit gets set correctly when
1918 // an extension contains an NPAPI plugin.
1919 // Only run this on platforms that support NPAPI plugins.
1920 TEST_F(ExtensionServiceTest
, GrantedFullAccessPermissions
) {
1921 InitPluginService();
1923 InitializeEmptyExtensionService();
1925 ASSERT_TRUE(base::PathExists(good1_path()));
1926 const Extension
* extension
= PackAndInstallCRX(good1_path(), INSTALL_NEW
);
1927 EXPECT_EQ(0u, GetErrors().size());
1928 EXPECT_EQ(1u, registry()->enabled_extensions().size());
1929 ExtensionPrefs
* prefs
= ExtensionPrefs::Get(profile());
1931 scoped_refptr
<PermissionSet
> permissions(
1932 prefs
->GetGrantedPermissions(extension
->id()));
1933 EXPECT_FALSE(permissions
->IsEmpty());
1934 EXPECT_TRUE(permissions
->HasEffectiveFullAccess());
1935 EXPECT_FALSE(permissions
->apis().empty());
1936 EXPECT_TRUE(permissions
->HasAPIPermission(APIPermission::kPlugin
));
1938 // Full access implies full host access too...
1939 EXPECT_TRUE(permissions
->HasEffectiveAccessToAllHosts());
1943 // Tests that the extension is disabled when permissions are missing from
1944 // the extension's granted permissions preferences. (This simulates updating
1945 // the browser to a version which recognizes more permissions).
1946 TEST_F(ExtensionServiceTest
, GrantedAPIAndHostPermissions
) {
1947 InitializeEmptyExtensionService();
1949 base::FilePath path
=
1950 data_dir().AppendASCII("permissions").AppendASCII("unknown");
1952 ASSERT_TRUE(base::PathExists(path
));
1954 const Extension
* extension
= PackAndInstallCRX(path
, INSTALL_NEW
);
1956 EXPECT_EQ(0u, GetErrors().size());
1957 EXPECT_EQ(1u, registry()->enabled_extensions().size());
1958 std::string extension_id
= extension
->id();
1960 ExtensionPrefs
* prefs
= ExtensionPrefs::Get(profile());
1962 APIPermissionSet expected_api_permissions
;
1963 URLPatternSet expected_host_permissions
;
1965 expected_api_permissions
.insert(APIPermission::kTab
);
1966 AddPattern(&expected_host_permissions
, "http://*.google.com/*");
1967 AddPattern(&expected_host_permissions
, "https://*.google.com/*");
1968 AddPattern(&expected_host_permissions
, "http://*.google.com.hk/*");
1969 AddPattern(&expected_host_permissions
, "http://www.example.com/*");
1971 std::set
<std::string
> host_permissions
;
1973 // Test that the extension is disabled when an API permission is missing from
1974 // the extension's granted api permissions preference. (This simulates
1975 // updating the browser to a version which recognizes a new API permission).
1976 SetPref(extension_id
, "granted_permissions.api",
1977 new base::ListValue(), "granted_permissions.api");
1978 service()->ReloadExtensionsForTest();
1980 EXPECT_EQ(1u, registry()->disabled_extensions().size());
1981 extension
= registry()->disabled_extensions().begin()->get();
1983 ASSERT_TRUE(prefs
->IsExtensionDisabled(extension_id
));
1984 ASSERT_FALSE(service()->IsExtensionEnabled(extension_id
));
1985 ASSERT_TRUE(prefs
->DidExtensionEscalatePermissions(extension_id
));
1987 // Now grant and re-enable the extension, making sure the prefs are updated.
1988 service()->GrantPermissionsAndEnableExtension(extension
);
1990 ASSERT_FALSE(prefs
->IsExtensionDisabled(extension_id
));
1991 ASSERT_TRUE(service()->IsExtensionEnabled(extension_id
));
1992 ASSERT_FALSE(prefs
->DidExtensionEscalatePermissions(extension_id
));
1994 scoped_refptr
<PermissionSet
> current_perms(
1995 prefs
->GetGrantedPermissions(extension_id
));
1996 ASSERT_TRUE(current_perms
.get());
1997 ASSERT_FALSE(current_perms
->IsEmpty());
1998 ASSERT_FALSE(current_perms
->HasEffectiveFullAccess());
1999 ASSERT_EQ(expected_api_permissions
, current_perms
->apis());
2000 ASSERT_EQ(expected_host_permissions
, current_perms
->effective_hosts());
2002 // Tests that the extension is disabled when a host permission is missing from
2003 // the extension's granted host permissions preference. (This simulates
2004 // updating the browser to a version which recognizes additional host
2006 host_permissions
.clear();
2007 current_perms
= NULL
;
2009 host_permissions
.insert("http://*.google.com/*");
2010 host_permissions
.insert("https://*.google.com/*");
2011 host_permissions
.insert("http://*.google.com.hk/*");
2013 base::ListValue
* api_permissions
= new base::ListValue();
2014 api_permissions
->Append(
2015 new base::StringValue("tabs"));
2016 SetPref(extension_id
, "granted_permissions.api",
2017 api_permissions
, "granted_permissions.api");
2019 extension_id
, "granted_permissions.scriptable_host", host_permissions
);
2021 service()->ReloadExtensionsForTest();
2023 EXPECT_EQ(1u, registry()->disabled_extensions().size());
2024 extension
= registry()->disabled_extensions().begin()->get();
2026 ASSERT_TRUE(prefs
->IsExtensionDisabled(extension_id
));
2027 ASSERT_FALSE(service()->IsExtensionEnabled(extension_id
));
2028 ASSERT_TRUE(prefs
->DidExtensionEscalatePermissions(extension_id
));
2030 // Now grant and re-enable the extension, making sure the prefs are updated.
2031 service()->GrantPermissionsAndEnableExtension(extension
);
2033 ASSERT_TRUE(service()->IsExtensionEnabled(extension_id
));
2034 ASSERT_FALSE(prefs
->DidExtensionEscalatePermissions(extension_id
));
2036 current_perms
= prefs
->GetGrantedPermissions(extension_id
);
2037 ASSERT_TRUE(current_perms
.get());
2038 ASSERT_FALSE(current_perms
->IsEmpty());
2039 ASSERT_FALSE(current_perms
->HasEffectiveFullAccess());
2040 ASSERT_EQ(expected_api_permissions
, current_perms
->apis());
2041 ASSERT_EQ(expected_host_permissions
, current_perms
->effective_hosts());
2044 // Test Packaging and installing an extension.
2045 TEST_F(ExtensionServiceTest
, PackExtension
) {
2046 InitializeEmptyExtensionService();
2047 base::FilePath input_directory
=
2049 .AppendASCII("good")
2050 .AppendASCII("Extensions")
2051 .AppendASCII("behllobkkfkfnphdnhnkndlbkcpglgmj")
2052 .AppendASCII("1.0.0.0");
2054 base::ScopedTempDir temp_dir
;
2055 ASSERT_TRUE(temp_dir
.CreateUniqueTempDir());
2056 base::FilePath output_directory
= temp_dir
.path();
2058 base::FilePath
crx_path(output_directory
.AppendASCII("ex1.crx"));
2059 base::FilePath
privkey_path(output_directory
.AppendASCII("privkey.pem"));
2061 scoped_ptr
<ExtensionCreator
> creator(new ExtensionCreator());
2062 ASSERT_TRUE(creator
->Run(input_directory
, crx_path
, base::FilePath(),
2063 privkey_path
, ExtensionCreator::kNoRunFlags
));
2064 ASSERT_TRUE(base::PathExists(crx_path
));
2065 ASSERT_TRUE(base::PathExists(privkey_path
));
2067 // Repeat the run with the pem file gone, and no special flags
2068 // Should refuse to overwrite the existing crx.
2069 base::DeleteFile(privkey_path
, false);
2070 ASSERT_FALSE(creator
->Run(input_directory
, crx_path
, base::FilePath(),
2071 privkey_path
, ExtensionCreator::kNoRunFlags
));
2073 // OK, now try it with a flag to overwrite existing crx. Should work.
2074 ASSERT_TRUE(creator
->Run(input_directory
, crx_path
, base::FilePath(),
2075 privkey_path
, ExtensionCreator::kOverwriteCRX
));
2077 // Repeat the run allowing existing crx, but the existing pem is still
2078 // an error. Should fail.
2079 ASSERT_FALSE(creator
->Run(input_directory
, crx_path
, base::FilePath(),
2080 privkey_path
, ExtensionCreator::kOverwriteCRX
));
2082 ASSERT_TRUE(base::PathExists(privkey_path
));
2083 InstallCRX(crx_path
, INSTALL_NEW
);
2085 // Try packing with invalid paths.
2086 creator
.reset(new ExtensionCreator());
2088 creator
->Run(base::FilePath(), base::FilePath(), base::FilePath(),
2089 base::FilePath(), ExtensionCreator::kOverwriteCRX
));
2091 // Try packing an empty directory. Should fail because an empty directory is
2092 // not a valid extension.
2093 base::ScopedTempDir temp_dir2
;
2094 ASSERT_TRUE(temp_dir2
.CreateUniqueTempDir());
2095 creator
.reset(new ExtensionCreator());
2096 ASSERT_FALSE(creator
->Run(temp_dir2
.path(), crx_path
, privkey_path
,
2097 base::FilePath(), ExtensionCreator::kOverwriteCRX
));
2099 // Try packing with an invalid manifest.
2100 std::string invalid_manifest_content
= "I am not a manifest.";
2101 ASSERT_TRUE(base::WriteFile(
2102 temp_dir2
.path().Append(extensions::kManifestFilename
),
2103 invalid_manifest_content
.c_str(), invalid_manifest_content
.size()));
2104 creator
.reset(new ExtensionCreator());
2105 ASSERT_FALSE(creator
->Run(temp_dir2
.path(), crx_path
, privkey_path
,
2106 base::FilePath(), ExtensionCreator::kOverwriteCRX
));
2108 // Try packing with a private key that is a valid key, but invalid for the
2110 base::FilePath bad_private_key_dir
=
2111 data_dir().AppendASCII("bad_private_key");
2112 crx_path
= output_directory
.AppendASCII("bad_private_key.crx");
2113 privkey_path
= data_dir().AppendASCII("bad_private_key.pem");
2114 ASSERT_FALSE(creator
->Run(bad_private_key_dir
, crx_path
, base::FilePath(),
2115 privkey_path
, ExtensionCreator::kOverwriteCRX
));
2118 // Test Packaging and installing an extension whose name contains punctuation.
2119 TEST_F(ExtensionServiceTest
, PackPunctuatedExtension
) {
2120 InitializeEmptyExtensionService();
2121 base::FilePath input_directory
= data_dir()
2122 .AppendASCII("good")
2123 .AppendASCII("Extensions")
2125 .AppendASCII("1.0.0.0");
2127 base::ScopedTempDir temp_dir
;
2128 ASSERT_TRUE(temp_dir
.CreateUniqueTempDir());
2130 // Extension names containing punctuation, and the expected names for the
2131 // packed extensions.
2132 const base::FilePath punctuated_names
[] = {
2133 base::FilePath(FILE_PATH_LITERAL("this.extensions.name.has.periods")),
2134 base::FilePath(FILE_PATH_LITERAL(".thisextensionsnamestartswithaperiod")),
2135 base::FilePath(FILE_PATH_LITERAL("thisextensionhasaslashinitsname/")).
2136 NormalizePathSeparators(),
2138 const base::FilePath expected_crx_names
[] = {
2139 base::FilePath(FILE_PATH_LITERAL("this.extensions.name.has.periods.crx")),
2141 FILE_PATH_LITERAL(".thisextensionsnamestartswithaperiod.crx")),
2142 base::FilePath(FILE_PATH_LITERAL("thisextensionhasaslashinitsname.crx")),
2144 const base::FilePath expected_private_key_names
[] = {
2145 base::FilePath(FILE_PATH_LITERAL("this.extensions.name.has.periods.pem")),
2147 FILE_PATH_LITERAL(".thisextensionsnamestartswithaperiod.pem")),
2148 base::FilePath(FILE_PATH_LITERAL("thisextensionhasaslashinitsname.pem")),
2151 for (size_t i
= 0; i
< arraysize(punctuated_names
); ++i
) {
2152 SCOPED_TRACE(punctuated_names
[i
].value().c_str());
2153 base::FilePath output_dir
= temp_dir
.path().Append(punctuated_names
[i
]);
2155 // Copy the extension into the output directory, as PackExtensionJob doesn't
2156 // let us choose where to output the packed extension.
2157 ASSERT_TRUE(base::CopyDirectory(input_directory
, output_dir
, true));
2159 base::FilePath expected_crx_path
=
2160 temp_dir
.path().Append(expected_crx_names
[i
]);
2161 base::FilePath expected_private_key_path
=
2162 temp_dir
.path().Append(expected_private_key_names
[i
]);
2163 PackExtensionTestClient
pack_client(expected_crx_path
,
2164 expected_private_key_path
);
2165 scoped_refptr
<extensions::PackExtensionJob
> packer(
2166 new extensions::PackExtensionJob(&pack_client
, output_dir
,
2168 ExtensionCreator::kOverwriteCRX
));
2171 // The packer will post a notification task to the current thread's message
2172 // loop when it is finished. We manually run the loop here so that we
2173 // block and catch the notification; otherwise, the process would exit.
2174 // This call to |Run()| is matched by a call to |Quit()| in the
2175 // |PackExtensionTestClient|'s notification handling code.
2176 base::MessageLoop::current()->Run();
2178 if (HasFatalFailure())
2181 InstallCRX(expected_crx_path
, INSTALL_NEW
);
2185 TEST_F(ExtensionServiceTest
, PackExtensionContainingKeyFails
) {
2186 InitializeEmptyExtensionService();
2188 base::ScopedTempDir extension_temp_dir
;
2189 ASSERT_TRUE(extension_temp_dir
.CreateUniqueTempDir());
2190 base::FilePath input_directory
= extension_temp_dir
.path().AppendASCII("ext");
2192 base::CopyDirectory(data_dir()
2193 .AppendASCII("good")
2194 .AppendASCII("Extensions")
2195 .AppendASCII("behllobkkfkfnphdnhnkndlbkcpglgmj")
2196 .AppendASCII("1.0.0.0"),
2198 /*recursive=*/true));
2200 base::ScopedTempDir output_temp_dir
;
2201 ASSERT_TRUE(output_temp_dir
.CreateUniqueTempDir());
2202 base::FilePath output_directory
= output_temp_dir
.path();
2204 base::FilePath
crx_path(output_directory
.AppendASCII("ex1.crx"));
2205 base::FilePath
privkey_path(output_directory
.AppendASCII("privkey.pem"));
2207 // Pack the extension once to get a private key.
2208 scoped_ptr
<ExtensionCreator
> creator(new ExtensionCreator());
2209 ASSERT_TRUE(creator
->Run(input_directory
, crx_path
, base::FilePath(),
2210 privkey_path
, ExtensionCreator::kNoRunFlags
))
2211 << creator
->error_message();
2212 ASSERT_TRUE(base::PathExists(crx_path
));
2213 ASSERT_TRUE(base::PathExists(privkey_path
));
2215 base::DeleteFile(crx_path
, false);
2216 // Move the pem file into the extension.
2217 base::Move(privkey_path
,
2218 input_directory
.AppendASCII("privkey.pem"));
2220 // This pack should fail because of the contained private key.
2221 EXPECT_FALSE(creator
->Run(input_directory
, crx_path
, base::FilePath(),
2222 privkey_path
, ExtensionCreator::kNoRunFlags
));
2223 EXPECT_THAT(creator
->error_message(),
2224 testing::ContainsRegex(
2225 "extension includes the key file.*privkey.pem"));
2228 // Test Packaging and installing an extension using an openssl generated key.
2229 // The openssl is generated with the following:
2230 // > openssl genrsa -out privkey.pem 1024
2231 // > openssl pkcs8 -topk8 -nocrypt -in privkey.pem -out privkey_asn1.pem
2232 // The privkey.pem is a PrivateKey, and the pcks8 -topk8 creates a
2233 // PrivateKeyInfo ASN.1 structure, we our RSAPrivateKey expects.
2234 TEST_F(ExtensionServiceTest
, PackExtensionOpenSSLKey
) {
2235 InitializeEmptyExtensionService();
2236 base::FilePath input_directory
=
2238 .AppendASCII("good")
2239 .AppendASCII("Extensions")
2240 .AppendASCII("behllobkkfkfnphdnhnkndlbkcpglgmj")
2241 .AppendASCII("1.0.0.0");
2242 base::FilePath
privkey_path(
2243 data_dir().AppendASCII("openssl_privkey_asn1.pem"));
2244 ASSERT_TRUE(base::PathExists(privkey_path
));
2246 base::ScopedTempDir temp_dir
;
2247 ASSERT_TRUE(temp_dir
.CreateUniqueTempDir());
2248 base::FilePath output_directory
= temp_dir
.path();
2250 base::FilePath
crx_path(output_directory
.AppendASCII("ex1.crx"));
2252 scoped_ptr
<ExtensionCreator
> creator(new ExtensionCreator());
2253 ASSERT_TRUE(creator
->Run(input_directory
, crx_path
, privkey_path
,
2254 base::FilePath(), ExtensionCreator::kOverwriteCRX
));
2256 InstallCRX(crx_path
, INSTALL_NEW
);
2259 #if defined(THREAD_SANITIZER)
2260 // Flaky under Tsan. http://crbug.com/377702
2261 #define MAYBE_InstallTheme DISABLED_InstallTheme
2263 #define MAYBE_InstallTheme InstallTheme
2266 TEST_F(ExtensionServiceTest
, MAYBE_InstallTheme
) {
2267 InitializeEmptyExtensionService();
2271 base::FilePath path
= data_dir().AppendASCII("theme.crx");
2272 InstallCRX(path
, INSTALL_NEW
);
2274 ValidatePrefKeyCount(++pref_count
);
2275 ValidateIntegerPref(theme_crx
, "state", Extension::ENABLED
);
2276 ValidateIntegerPref(theme_crx
, "location", Manifest::INTERNAL
);
2278 // A theme when extensions are disabled. Themes can be installed, even when
2279 // extensions are disabled.
2280 service()->set_extensions_enabled(false);
2281 path
= data_dir().AppendASCII("theme2.crx");
2282 InstallCRX(path
, INSTALL_NEW
);
2283 ValidatePrefKeyCount(++pref_count
);
2284 ValidateIntegerPref(theme2_crx
, "state", Extension::ENABLED
);
2285 ValidateIntegerPref(theme2_crx
, "location", Manifest::INTERNAL
);
2287 // A theme with extension elements. Themes cannot have extension elements,
2288 // so any such elements (like content scripts) should be ignored.
2289 service()->set_extensions_enabled(true);
2291 path
= data_dir().AppendASCII("theme_with_extension.crx");
2292 const Extension
* extension
= InstallCRX(path
, INSTALL_NEW
);
2293 ValidatePrefKeyCount(++pref_count
);
2294 ASSERT_TRUE(extension
);
2295 EXPECT_TRUE(extension
->is_theme());
2298 extensions::ContentScriptsInfo::GetContentScripts(extension
).size());
2301 // A theme with image resources missing (misspelt path).
2302 path
= data_dir().AppendASCII("theme_missing_image.crx");
2303 InstallCRX(path
, INSTALL_FAILED
);
2304 ValidatePrefKeyCount(pref_count
);
2307 TEST_F(ExtensionServiceTest
, LoadLocalizedTheme
) {
2309 InitializeEmptyExtensionService();
2312 base::FilePath extension_path
= data_dir().AppendASCII("theme_i18n");
2314 extensions::UnpackedInstaller::Create(service())->Load(extension_path
);
2315 base::RunLoop().RunUntilIdle();
2316 EXPECT_EQ(0u, GetErrors().size());
2317 ASSERT_EQ(1u, loaded_
.size());
2318 EXPECT_EQ(1u, registry()->enabled_extensions().size());
2319 const Extension
* theme
= registry()->enabled_extensions().begin()->get();
2320 EXPECT_EQ("name", theme
->name());
2321 EXPECT_EQ("description", theme
->description());
2323 // Cleanup the "Cached Theme.pak" file. Ideally, this would be installed in a
2324 // temporary directory, but it automatically installs to the extension's
2325 // directory, and we don't want to copy the whole extension for a unittest.
2326 base::FilePath theme_file
= extension_path
.Append(chrome::kThemePackFilename
);
2327 ASSERT_TRUE(base::PathExists(theme_file
));
2328 ASSERT_TRUE(base::DeleteFile(theme_file
, false)); // Not recursive.
2331 // Tests that we can change the ID of an unpacked extension by adding a key
2333 TEST_F(ExtensionServiceTest
, UnpackedExtensionCanChangeID
) {
2334 InitializeEmptyExtensionService();
2336 base::ScopedTempDir temp
;
2337 ASSERT_TRUE(temp
.CreateUniqueTempDir());
2339 base::FilePath extension_path
= temp
.path();
2340 base::FilePath manifest_path
=
2341 extension_path
.Append(extensions::kManifestFilename
);
2342 base::FilePath manifest_no_key
=
2343 data_dir().AppendASCII("unpacked").AppendASCII("manifest_no_key.json");
2345 base::FilePath manifest_with_key
=
2346 data_dir().AppendASCII("unpacked").AppendASCII("manifest_with_key.json");
2348 ASSERT_TRUE(base::PathExists(manifest_no_key
));
2349 ASSERT_TRUE(base::PathExists(manifest_with_key
));
2351 // Load the unpacked extension with no key.
2352 base::CopyFile(manifest_no_key
, manifest_path
);
2353 extensions::UnpackedInstaller::Create(service())->Load(extension_path
);
2355 base::RunLoop().RunUntilIdle();
2356 EXPECT_EQ(0u, GetErrors().size());
2357 ASSERT_EQ(1u, loaded_
.size());
2358 EXPECT_EQ(1u, registry()->enabled_extensions().size());
2360 // Add the key to the manifest.
2361 base::CopyFile(manifest_with_key
, manifest_path
);
2364 // Reload the extensions.
2365 service()->ReloadExtensionsForTest();
2366 const Extension
* extension
= service()->GetExtensionById(unpacked
, false);
2367 EXPECT_EQ(unpacked
, extension
->id());
2368 ASSERT_EQ(1u, loaded_
.size());
2370 // TODO(jstritar): Right now this just makes sure we don't crash and burn, but
2371 // we should also test that preferences are preserved.
2374 #if defined(OS_POSIX)
2375 TEST_F(ExtensionServiceTest
, UnpackedExtensionMayContainSymlinkedFiles
) {
2376 base::FilePath source_data_dir
=
2377 data_dir().AppendASCII("unpacked").AppendASCII("symlinks_allowed");
2379 // Paths to test data files.
2380 base::FilePath source_manifest
= source_data_dir
.AppendASCII("manifest.json");
2381 ASSERT_TRUE(base::PathExists(source_manifest
));
2382 base::FilePath source_icon
= source_data_dir
.AppendASCII("icon.png");
2383 ASSERT_TRUE(base::PathExists(source_icon
));
2385 // Set up the temporary extension directory.
2386 base::ScopedTempDir temp
;
2387 ASSERT_TRUE(temp
.CreateUniqueTempDir());
2388 base::FilePath extension_path
= temp
.path();
2389 base::FilePath manifest
= extension_path
.Append(
2390 extensions::kManifestFilename
);
2391 base::FilePath icon_symlink
= extension_path
.AppendASCII("icon.png");
2392 base::CopyFile(source_manifest
, manifest
);
2393 base::CreateSymbolicLink(source_icon
, icon_symlink
);
2396 InitializeEmptyExtensionService();
2397 extensions::UnpackedInstaller::Create(service())->Load(extension_path
);
2398 base::RunLoop().RunUntilIdle();
2400 EXPECT_TRUE(GetErrors().empty());
2401 ASSERT_EQ(1u, loaded_
.size());
2402 EXPECT_EQ(1u, registry()->enabled_extensions().size());
2406 TEST_F(ExtensionServiceTest
, UnpackedExtensionMayNotHaveUnderscore
) {
2407 InitializeEmptyExtensionService();
2408 base::FilePath extension_path
= data_dir().AppendASCII("underscore_name");
2409 extensions::UnpackedInstaller::Create(service())->Load(extension_path
);
2410 base::RunLoop().RunUntilIdle();
2411 EXPECT_EQ(1u, GetErrors().size());
2412 EXPECT_EQ(0u, registry()->enabled_extensions().size());
2415 TEST_F(ExtensionServiceTest
, InstallLocalizedTheme
) {
2416 InitializeEmptyExtensionService();
2419 base::FilePath theme_path
= data_dir().AppendASCII("theme_i18n");
2421 const Extension
* theme
= PackAndInstallCRX(theme_path
, INSTALL_NEW
);
2423 EXPECT_EQ(0u, GetErrors().size());
2424 EXPECT_EQ(1u, registry()->enabled_extensions().size());
2425 EXPECT_EQ("name", theme
->name());
2426 EXPECT_EQ("description", theme
->description());
2429 TEST_F(ExtensionServiceTest
, InstallApps
) {
2430 InitializeEmptyExtensionService();
2433 const Extension
* app
=
2434 PackAndInstallCRX(data_dir().AppendASCII("app1"), INSTALL_NEW
);
2436 ValidatePrefKeyCount(++pref_count
);
2437 ASSERT_EQ(1u, registry()->enabled_extensions().size());
2438 ValidateIntegerPref(app
->id(), "state", Extension::ENABLED
);
2439 ValidateIntegerPref(app
->id(), "location", Manifest::INTERNAL
);
2441 // Another app with non-overlapping extent. Should succeed.
2442 PackAndInstallCRX(data_dir().AppendASCII("app2"), INSTALL_NEW
);
2443 ValidatePrefKeyCount(++pref_count
);
2445 // A third app whose extent overlaps the first. Should fail.
2446 PackAndInstallCRX(data_dir().AppendASCII("app3"), INSTALL_FAILED
);
2447 ValidatePrefKeyCount(pref_count
);
2450 // Tests that file access is OFF by default.
2451 TEST_F(ExtensionServiceTest
, DefaultFileAccess
) {
2452 InitializeEmptyExtensionService();
2453 const Extension
* extension
= PackAndInstallCRX(
2454 data_dir().AppendASCII("permissions").AppendASCII("files"), INSTALL_NEW
);
2455 EXPECT_EQ(0u, GetErrors().size());
2456 EXPECT_EQ(1u, registry()->enabled_extensions().size());
2458 ExtensionPrefs::Get(profile())->AllowFileAccess(extension
->id()));
2461 TEST_F(ExtensionServiceTest
, UpdateApps
) {
2462 InitializeEmptyExtensionService();
2463 base::FilePath extensions_path
= data_dir().AppendASCII("app_update");
2465 // First install v1 of a hosted app.
2466 const Extension
* extension
=
2467 InstallCRX(extensions_path
.AppendASCII("v1.crx"), INSTALL_NEW
);
2468 ASSERT_EQ(1u, registry()->enabled_extensions().size());
2469 std::string id
= extension
->id();
2470 ASSERT_EQ(std::string("1"), extension
->version()->GetString());
2472 // Now try updating to v2.
2474 extensions_path
.AppendASCII("v2.crx"),
2476 ASSERT_EQ(std::string("2"),
2477 service()->GetExtensionById(id
, false)->version()->GetString());
2480 // Verifies that the NTP page and launch ordinals are kept when updating apps.
2481 TEST_F(ExtensionServiceTest
, UpdateAppsRetainOrdinals
) {
2482 InitializeEmptyExtensionService();
2483 AppSorting
* sorting
= ExtensionPrefs::Get(profile())->app_sorting();
2484 base::FilePath extensions_path
= data_dir().AppendASCII("app_update");
2486 // First install v1 of a hosted app.
2487 const Extension
* extension
=
2488 InstallCRX(extensions_path
.AppendASCII("v1.crx"), INSTALL_NEW
);
2489 ASSERT_EQ(1u, registry()->enabled_extensions().size());
2490 std::string id
= extension
->id();
2491 ASSERT_EQ(std::string("1"), extension
->version()->GetString());
2493 // Modify the ordinals so we can distinguish them from the defaults.
2494 syncer::StringOrdinal new_page_ordinal
=
2495 sorting
->GetPageOrdinal(id
).CreateAfter();
2496 syncer::StringOrdinal new_launch_ordinal
=
2497 sorting
->GetAppLaunchOrdinal(id
).CreateBefore();
2499 sorting
->SetPageOrdinal(id
, new_page_ordinal
);
2500 sorting
->SetAppLaunchOrdinal(id
, new_launch_ordinal
);
2502 // Now try updating to v2.
2503 UpdateExtension(id
, extensions_path
.AppendASCII("v2.crx"), ENABLED
);
2504 ASSERT_EQ(std::string("2"),
2505 service()->GetExtensionById(id
, false)->version()->GetString());
2507 // Verify that the ordinals match.
2508 ASSERT_TRUE(new_page_ordinal
.Equals(sorting
->GetPageOrdinal(id
)));
2509 ASSERT_TRUE(new_launch_ordinal
.Equals(sorting
->GetAppLaunchOrdinal(id
)));
2512 // Ensures that the CWS has properly initialized ordinals.
2513 TEST_F(ExtensionServiceTest
, EnsureCWSOrdinalsInitialized
) {
2514 InitializeEmptyExtensionService();
2515 service()->component_loader()->Add(
2516 IDR_WEBSTORE_MANIFEST
, base::FilePath(FILE_PATH_LITERAL("web_store")));
2519 AppSorting
* sorting
= ExtensionPrefs::Get(profile())->app_sorting();
2521 sorting
->GetPageOrdinal(extensions::kWebStoreAppId
).IsValid());
2523 sorting
->GetAppLaunchOrdinal(extensions::kWebStoreAppId
).IsValid());
2526 TEST_F(ExtensionServiceTest
, InstallAppsWithUnlimitedStorage
) {
2527 InitializeEmptyExtensionService();
2528 EXPECT_TRUE(registry()->enabled_extensions().is_empty());
2532 // Install app1 with unlimited storage.
2533 const Extension
* extension
=
2534 PackAndInstallCRX(data_dir().AppendASCII("app1"), INSTALL_NEW
);
2535 ValidatePrefKeyCount(++pref_count
);
2536 ASSERT_EQ(1u, registry()->enabled_extensions().size());
2537 const std::string id1
= extension
->id();
2538 EXPECT_TRUE(extension
->permissions_data()->HasAPIPermission(
2539 APIPermission::kUnlimitedStorage
));
2540 EXPECT_TRUE(extension
->web_extent().MatchesURL(
2541 extensions::AppLaunchInfo::GetFullLaunchURL(extension
)));
2543 extensions::AppLaunchInfo::GetFullLaunchURL(extension
).GetOrigin());
2544 EXPECT_TRUE(profile()->GetExtensionSpecialStoragePolicy()->IsStorageUnlimited(
2547 // Install app2 from the same origin with unlimited storage.
2548 extension
= PackAndInstallCRX(data_dir().AppendASCII("app2"), INSTALL_NEW
);
2549 ValidatePrefKeyCount(++pref_count
);
2550 ASSERT_EQ(2u, registry()->enabled_extensions().size());
2551 const std::string id2
= extension
->id();
2552 EXPECT_TRUE(extension
->permissions_data()->HasAPIPermission(
2553 APIPermission::kUnlimitedStorage
));
2554 EXPECT_TRUE(extension
->web_extent().MatchesURL(
2555 extensions::AppLaunchInfo::GetFullLaunchURL(extension
)));
2557 extensions::AppLaunchInfo::GetFullLaunchURL(extension
).GetOrigin());
2558 EXPECT_EQ(origin1
, origin2
);
2559 EXPECT_TRUE(profile()->GetExtensionSpecialStoragePolicy()->IsStorageUnlimited(
2562 // Uninstall one of them, unlimited storage should still be granted
2564 UninstallExtension(id1
, false);
2565 EXPECT_EQ(1u, registry()->enabled_extensions().size());
2566 EXPECT_TRUE(profile()->GetExtensionSpecialStoragePolicy()->IsStorageUnlimited(
2569 // Uninstall the other, unlimited storage should be revoked.
2570 UninstallExtension(id2
, false);
2571 EXPECT_EQ(0u, registry()->enabled_extensions().size());
2573 profile()->GetExtensionSpecialStoragePolicy()->IsStorageUnlimited(
2577 TEST_F(ExtensionServiceTest
, InstallAppsAndCheckStorageProtection
) {
2578 InitializeEmptyExtensionService();
2579 EXPECT_TRUE(registry()->enabled_extensions().is_empty());
2583 const Extension
* extension
=
2584 PackAndInstallCRX(data_dir().AppendASCII("app1"), INSTALL_NEW
);
2585 ValidatePrefKeyCount(++pref_count
);
2586 ASSERT_EQ(1u, registry()->enabled_extensions().size());
2587 EXPECT_TRUE(extension
->is_app());
2588 const std::string id1
= extension
->id();
2590 extensions::AppLaunchInfo::GetFullLaunchURL(extension
).GetOrigin());
2591 EXPECT_TRUE(profile()->GetExtensionSpecialStoragePolicy()->IsStorageProtected(
2594 // App 4 has a different origin (maps.google.com).
2595 extension
= PackAndInstallCRX(data_dir().AppendASCII("app4"), INSTALL_NEW
);
2596 ValidatePrefKeyCount(++pref_count
);
2597 ASSERT_EQ(2u, registry()->enabled_extensions().size());
2598 const std::string id2
= extension
->id();
2600 extensions::AppLaunchInfo::GetFullLaunchURL(extension
).GetOrigin());
2601 ASSERT_NE(origin1
, origin2
);
2602 EXPECT_TRUE(profile()->GetExtensionSpecialStoragePolicy()->IsStorageProtected(
2605 UninstallExtension(id1
, false);
2606 EXPECT_EQ(1u, registry()->enabled_extensions().size());
2608 UninstallExtension(id2
, false);
2610 EXPECT_TRUE(registry()->enabled_extensions().is_empty());
2612 profile()->GetExtensionSpecialStoragePolicy()->IsStorageProtected(
2615 profile()->GetExtensionSpecialStoragePolicy()->IsStorageProtected(
2619 // Test that when an extension version is reinstalled, nothing happens.
2620 TEST_F(ExtensionServiceTest
, Reinstall
) {
2621 InitializeEmptyExtensionService();
2623 // A simple extension that should install without error.
2624 base::FilePath path
= data_dir().AppendASCII("good.crx");
2625 InstallCRX(path
, INSTALL_NEW
);
2627 ValidatePrefKeyCount(1);
2628 ValidateIntegerPref(good_crx
, "state", Extension::ENABLED
);
2629 ValidateIntegerPref(good_crx
, "location", Manifest::INTERNAL
);
2631 // Reinstall the same version, it should overwrite the previous one.
2632 InstallCRX(path
, INSTALL_UPDATED
);
2634 ValidatePrefKeyCount(1);
2635 ValidateIntegerPref(good_crx
, "state", Extension::ENABLED
);
2636 ValidateIntegerPref(good_crx
, "location", Manifest::INTERNAL
);
2639 // Test that we can determine if extensions came from the
2640 // Chrome web store.
2641 TEST_F(ExtensionServiceTest
, FromWebStore
) {
2642 InitializeEmptyExtensionService();
2644 // A simple extension that should install without error.
2645 base::FilePath path
= data_dir().AppendASCII("good.crx");
2646 // Not from web store.
2647 const Extension
* extension
= InstallCRX(path
, INSTALL_NEW
);
2648 std::string id
= extension
->id();
2650 ValidatePrefKeyCount(1);
2651 ASSERT_TRUE(ValidateBooleanPref(good_crx
, "from_webstore", false));
2652 ASSERT_FALSE(extension
->from_webstore());
2654 // Test install from web store.
2655 InstallCRXFromWebStore(path
, INSTALL_UPDATED
); // From web store.
2657 ValidatePrefKeyCount(1);
2658 ASSERT_TRUE(ValidateBooleanPref(good_crx
, "from_webstore", true));
2660 // Reload so extension gets reinitialized with new value.
2661 service()->ReloadExtensionsForTest();
2662 extension
= service()->GetExtensionById(id
, false);
2663 ASSERT_TRUE(extension
->from_webstore());
2665 // Upgrade to version 2.0
2666 path
= data_dir().AppendASCII("good2.crx");
2667 UpdateExtension(good_crx
, path
, ENABLED
);
2668 ValidatePrefKeyCount(1);
2669 ASSERT_TRUE(ValidateBooleanPref(good_crx
, "from_webstore", true));
2672 // Test upgrading a signed extension.
2673 TEST_F(ExtensionServiceTest
, UpgradeSignedGood
) {
2674 InitializeEmptyExtensionService();
2676 base::FilePath path
= data_dir().AppendASCII("good.crx");
2677 const Extension
* extension
= InstallCRX(path
, INSTALL_NEW
);
2678 std::string id
= extension
->id();
2680 ASSERT_EQ("1.0.0.0", extension
->version()->GetString());
2681 ASSERT_EQ(0u, GetErrors().size());
2683 // Upgrade to version 1.0.0.1.
2684 // Also test that the extension's old and new title are correctly retrieved.
2685 path
= data_dir().AppendASCII("good2.crx");
2686 InstallCRX(path
, INSTALL_UPDATED
, Extension::NO_FLAGS
, "My extension 1");
2687 extension
= service()->GetExtensionById(id
, false);
2689 ASSERT_EQ("1.0.0.1", extension
->version()->GetString());
2690 ASSERT_EQ("My updated extension 1", extension
->name());
2691 ASSERT_EQ(0u, GetErrors().size());
2694 // Test upgrading a signed extension with a bad signature.
2695 TEST_F(ExtensionServiceTest
, UpgradeSignedBad
) {
2696 InitializeEmptyExtensionService();
2698 base::FilePath path
= data_dir().AppendASCII("good.crx");
2699 InstallCRX(path
, INSTALL_NEW
);
2701 // Try upgrading with a bad signature. This should fail during the unpack,
2702 // because the key will not match the signature.
2703 path
= data_dir().AppendASCII("bad_signature.crx");
2704 InstallCRX(path
, INSTALL_FAILED
);
2707 // Test a normal update via the UpdateExtension API
2708 TEST_F(ExtensionServiceTest
, UpdateExtension
) {
2709 InitializeEmptyExtensionService();
2711 base::FilePath path
= data_dir().AppendASCII("good.crx");
2713 const Extension
* good
= InstallCRX(path
, INSTALL_NEW
);
2714 ASSERT_EQ("1.0.0.0", good
->VersionString());
2715 ASSERT_EQ(good_crx
, good
->id());
2717 path
= data_dir().AppendASCII("good2.crx");
2718 UpdateExtension(good_crx
, path
, ENABLED
);
2721 service()->GetExtensionById(good_crx
, false)->version()->GetString());
2724 // Extensions should not be updated during browser shutdown.
2725 TEST_F(ExtensionServiceTest
, UpdateExtensionDuringShutdown
) {
2726 InitializeEmptyExtensionService();
2728 // Install an extension.
2729 base::FilePath path
= data_dir().AppendASCII("good.crx");
2730 const Extension
* good
= InstallCRX(path
, INSTALL_NEW
);
2731 ASSERT_EQ(good_crx
, good
->id());
2733 // Simulate shutdown.
2734 service()->set_browser_terminating_for_test(true);
2736 // Update should fail and extension should not be updated.
2737 path
= data_dir().AppendASCII("good2.crx");
2738 bool updated
= service()->UpdateExtension(
2739 extensions::CRXFileInfo(good_crx
, path
), true, NULL
);
2740 ASSERT_FALSE(updated
);
2743 service()->GetExtensionById(good_crx
, false)->version()->GetString());
2746 // Test updating a not-already-installed extension - this should fail
2747 TEST_F(ExtensionServiceTest
, UpdateNotInstalledExtension
) {
2748 InitializeEmptyExtensionService();
2750 base::FilePath path
= data_dir().AppendASCII("good.crx");
2751 UpdateExtension(good_crx
, path
, UPDATED
);
2752 base::RunLoop().RunUntilIdle();
2754 ASSERT_EQ(0u, registry()->enabled_extensions().size());
2755 ASSERT_FALSE(installed_
);
2756 ASSERT_EQ(0u, loaded_
.size());
2759 // Makes sure you can't downgrade an extension via UpdateExtension
2760 TEST_F(ExtensionServiceTest
, UpdateWillNotDowngrade
) {
2761 InitializeEmptyExtensionService();
2763 base::FilePath path
= data_dir().AppendASCII("good2.crx");
2765 const Extension
* good
= InstallCRX(path
, INSTALL_NEW
);
2766 ASSERT_EQ("1.0.0.1", good
->VersionString());
2767 ASSERT_EQ(good_crx
, good
->id());
2769 // Change path from good2.crx -> good.crx
2770 path
= data_dir().AppendASCII("good.crx");
2771 UpdateExtension(good_crx
, path
, FAILED
);
2774 service()->GetExtensionById(good_crx
, false)->version()->GetString());
2777 // Make sure calling update with an identical version does nothing
2778 TEST_F(ExtensionServiceTest
, UpdateToSameVersionIsNoop
) {
2779 InitializeEmptyExtensionService();
2781 base::FilePath path
= data_dir().AppendASCII("good.crx");
2783 const Extension
* good
= InstallCRX(path
, INSTALL_NEW
);
2784 ASSERT_EQ(good_crx
, good
->id());
2785 UpdateExtension(good_crx
, path
, FAILED_SILENTLY
);
2788 // Tests that updating an extension does not clobber old state.
2789 TEST_F(ExtensionServiceTest
, UpdateExtensionPreservesState
) {
2790 InitializeEmptyExtensionService();
2792 base::FilePath path
= data_dir().AppendASCII("good.crx");
2794 const Extension
* good
= InstallCRX(path
, INSTALL_NEW
);
2795 ASSERT_EQ("1.0.0.0", good
->VersionString());
2796 ASSERT_EQ(good_crx
, good
->id());
2798 // Disable it and allow it to run in incognito. These settings should carry
2799 // over to the updated version.
2800 service()->DisableExtension(good
->id(), Extension::DISABLE_USER_ACTION
);
2801 extensions::util::SetIsIncognitoEnabled(good
->id(), profile(), true);
2802 ExtensionPrefs::Get(profile())
2803 ->SetDidExtensionEscalatePermissions(good
, true);
2805 path
= data_dir().AppendASCII("good2.crx");
2806 UpdateExtension(good_crx
, path
, INSTALLED
);
2807 ASSERT_EQ(1u, registry()->disabled_extensions().size());
2808 const Extension
* good2
= service()->GetExtensionById(good_crx
, true);
2809 ASSERT_EQ("1.0.0.1", good2
->version()->GetString());
2810 EXPECT_TRUE(extensions::util::IsIncognitoEnabled(good2
->id(), profile()));
2811 EXPECT_TRUE(ExtensionPrefs::Get(profile())
2812 ->DidExtensionEscalatePermissions(good2
->id()));
2815 // Tests that updating preserves extension location.
2816 TEST_F(ExtensionServiceTest
, UpdateExtensionPreservesLocation
) {
2817 InitializeEmptyExtensionService();
2819 base::FilePath path
= data_dir().AppendASCII("good.crx");
2821 const Extension
* good
=
2822 InstallCRXWithLocation(path
, Manifest::EXTERNAL_PREF
, INSTALL_NEW
);
2824 ASSERT_EQ("1.0.0.0", good
->VersionString());
2825 ASSERT_EQ(good_crx
, good
->id());
2827 path
= data_dir().AppendASCII("good2.crx");
2828 UpdateExtension(good_crx
, path
, ENABLED
);
2829 const Extension
* good2
= service()->GetExtensionById(good_crx
, false);
2830 ASSERT_EQ("1.0.0.1", good2
->version()->GetString());
2831 EXPECT_EQ(good2
->location(), Manifest::EXTERNAL_PREF
);
2834 // Makes sure that LOAD extension types can downgrade.
2835 TEST_F(ExtensionServiceTest
, LoadExtensionsCanDowngrade
) {
2836 InitializeEmptyExtensionService();
2838 base::ScopedTempDir temp
;
2839 ASSERT_TRUE(temp
.CreateUniqueTempDir());
2841 // We'll write the extension manifest dynamically to a temporary path
2842 // to make it easier to change the version number.
2843 base::FilePath extension_path
= temp
.path();
2844 base::FilePath manifest_path
=
2845 extension_path
.Append(extensions::kManifestFilename
);
2846 ASSERT_FALSE(base::PathExists(manifest_path
));
2848 // Start with version 2.0.
2849 base::DictionaryValue manifest
;
2850 manifest
.SetString("version", "2.0");
2851 manifest
.SetString("name", "LOAD Downgrade Test");
2852 manifest
.SetInteger("manifest_version", 2);
2854 JSONFileValueSerializer
serializer(manifest_path
);
2855 ASSERT_TRUE(serializer
.Serialize(manifest
));
2857 extensions::UnpackedInstaller::Create(service())->Load(extension_path
);
2858 base::RunLoop().RunUntilIdle();
2860 EXPECT_EQ(0u, GetErrors().size());
2861 ASSERT_EQ(1u, loaded_
.size());
2862 EXPECT_EQ(Manifest::UNPACKED
, loaded_
[0]->location());
2863 EXPECT_EQ(1u, registry()->enabled_extensions().size());
2864 EXPECT_EQ("2.0", loaded_
[0]->VersionString());
2866 // Now set the version number to 1.0, reload the extensions and verify that
2867 // the downgrade was accepted.
2868 manifest
.SetString("version", "1.0");
2869 ASSERT_TRUE(serializer
.Serialize(manifest
));
2871 extensions::UnpackedInstaller::Create(service())->Load(extension_path
);
2872 base::RunLoop().RunUntilIdle();
2874 EXPECT_EQ(0u, GetErrors().size());
2875 ASSERT_EQ(1u, loaded_
.size());
2876 EXPECT_EQ(Manifest::UNPACKED
, loaded_
[0]->location());
2877 EXPECT_EQ(1u, registry()->enabled_extensions().size());
2878 EXPECT_EQ("1.0", loaded_
[0]->VersionString());
2881 #if !defined(OS_POSIX) || defined(OS_MACOSX)
2882 // LOAD extensions with plugins require approval.
2883 // Only run this on platforms that support NPAPI plugins.
2884 TEST_F(ExtensionServiceTest
, LoadExtensionsWithPlugins
) {
2885 base::FilePath extension_with_plugin_path
= good1_path();
2886 base::FilePath extension_no_plugin_path
= good2_path();
2888 InitPluginService();
2889 InitializeEmptyExtensionService();
2890 service()->set_show_extensions_prompts(true);
2892 // Start by canceling any install prompts.
2893 ExtensionInstallPrompt::g_auto_confirm_for_tests
=
2894 ExtensionInstallPrompt::CANCEL
;
2896 // The extension that has a plugin should not install.
2897 extensions::UnpackedInstaller::Create(service())
2898 ->Load(extension_with_plugin_path
);
2899 base::RunLoop().RunUntilIdle();
2900 EXPECT_EQ(0u, GetErrors().size());
2901 EXPECT_EQ(0u, loaded_
.size());
2902 EXPECT_EQ(0u, registry()->enabled_extensions().size());
2903 EXPECT_EQ(0u, registry()->disabled_extensions().size());
2905 // But the extension with no plugin should since there's no prompt.
2906 ExtensionErrorReporter::GetInstance()->ClearErrors();
2907 extensions::UnpackedInstaller::Create(service())
2908 ->Load(extension_no_plugin_path
);
2909 base::RunLoop().RunUntilIdle();
2910 EXPECT_EQ(0u, GetErrors().size());
2911 EXPECT_EQ(1u, loaded_
.size());
2912 EXPECT_EQ(1u, registry()->enabled_extensions().size());
2913 EXPECT_EQ(0u, registry()->disabled_extensions().size());
2914 EXPECT_TRUE(registry()->enabled_extensions().Contains(good2
));
2916 // The plugin extension should install if we accept the dialog.
2917 ExtensionInstallPrompt::g_auto_confirm_for_tests
=
2918 ExtensionInstallPrompt::ACCEPT
;
2920 ExtensionErrorReporter::GetInstance()->ClearErrors();
2921 extensions::UnpackedInstaller::Create(service())
2922 ->Load(extension_with_plugin_path
);
2923 base::RunLoop().RunUntilIdle();
2924 EXPECT_EQ(0u, GetErrors().size());
2925 EXPECT_EQ(2u, loaded_
.size());
2926 EXPECT_EQ(2u, registry()->enabled_extensions().size());
2927 EXPECT_EQ(0u, registry()->disabled_extensions().size());
2928 EXPECT_TRUE(registry()->enabled_extensions().Contains(good1
));
2929 EXPECT_TRUE(registry()->enabled_extensions().Contains(good2
));
2931 // Make sure the granted permissions have been setup.
2932 scoped_refptr
<PermissionSet
> permissions(
2933 ExtensionPrefs::Get(profile())->GetGrantedPermissions(good1
));
2934 EXPECT_FALSE(permissions
->IsEmpty());
2935 EXPECT_TRUE(permissions
->HasEffectiveFullAccess());
2936 EXPECT_FALSE(permissions
->apis().empty());
2937 EXPECT_TRUE(permissions
->HasAPIPermission(APIPermission::kPlugin
));
2939 // We should be able to reload the extension without getting another prompt.
2941 ExtensionInstallPrompt::g_auto_confirm_for_tests
=
2942 ExtensionInstallPrompt::CANCEL
;
2944 service()->ReloadExtension(good1
);
2945 base::RunLoop().RunUntilIdle();
2946 EXPECT_EQ(1u, loaded_
.size());
2947 EXPECT_EQ(2u, registry()->enabled_extensions().size());
2948 EXPECT_EQ(0u, registry()->disabled_extensions().size());
2950 #endif // !defined(OS_POSIX) || defined(OS_MACOSX)
2954 bool IsExtension(const Extension
* extension
) {
2955 return extension
->GetType() == Manifest::TYPE_EXTENSION
;
2958 #if defined(ENABLE_BLACKLIST_TESTS)
2959 std::set
<std::string
> StringSet(const std::string
& s
) {
2960 std::set
<std::string
> set
;
2964 std::set
<std::string
> StringSet(const std::string
& s1
, const std::string
& s2
) {
2965 std::set
<std::string
> set
= StringSet(s1
);
2969 #endif // defined(ENABLE_BLACKLIST_TESTS)
2973 // Test adding a pending extension.
2974 TEST_F(ExtensionServiceTest
, AddPendingExtensionFromSync
) {
2975 InitializeEmptyExtensionService();
2977 const std::string
kFakeId(all_zero
);
2978 const GURL
kFakeUpdateURL("http:://fake.update/url");
2979 const bool kFakeRemoteInstall(false);
2980 const bool kFakeInstalledByCustodian(false);
2983 service()->pending_extension_manager()->AddFromSync(
2988 kFakeInstalledByCustodian
));
2990 const extensions::PendingExtensionInfo
* pending_extension_info
;
2991 ASSERT_TRUE((pending_extension_info
=
2992 service()->pending_extension_manager()->GetById(kFakeId
)));
2993 EXPECT_EQ(kFakeUpdateURL
, pending_extension_info
->update_url());
2994 EXPECT_EQ(&IsExtension
, pending_extension_info
->should_allow_install_
);
2996 // EXPECT_TRUE(kFakeRemoteInstall == pending_extension_info->remote_install())
2998 // EXPECT_EQ(kFakeRemoteInstall, pending_extension_info->remote_install())
2999 // as gcc 4.7 issues the following warning on EXPECT_EQ(false, x), which is
3000 // turned into an error with -Werror=conversion-null:
3001 // converting 'false' to pointer type for argument 1 of
3002 // 'char testing::internal::IsNullLiteralHelper(testing::internal::Secret*)'
3003 // https://code.google.com/p/googletest/issues/detail?id=458
3004 EXPECT_TRUE(kFakeRemoteInstall
== pending_extension_info
->remote_install());
3008 const char kGoodId
[] = "ldnnhddmnhbkjipkidpdiheffobcpfmf";
3009 const char kGoodUpdateURL
[] = "http://good.update/url";
3010 const bool kGoodIsFromSync
= true;
3011 const bool kGoodRemoteInstall
= false;
3012 const bool kGoodInstalledByCustodian
= false;
3015 // Test updating a pending extension.
3016 TEST_F(ExtensionServiceTest
, UpdatePendingExtension
) {
3017 InitializeEmptyExtensionService();
3019 service()->pending_extension_manager()->AddFromSync(
3021 GURL(kGoodUpdateURL
),
3024 kGoodInstalledByCustodian
));
3025 EXPECT_TRUE(service()->pending_extension_manager()->IsIdPending(kGoodId
));
3027 base::FilePath path
= data_dir().AppendASCII("good.crx");
3028 UpdateExtension(kGoodId
, path
, ENABLED
);
3030 EXPECT_FALSE(service()->pending_extension_manager()->IsIdPending(kGoodId
));
3032 const Extension
* extension
= service()->GetExtensionById(kGoodId
, true);
3033 ASSERT_TRUE(extension
);
3038 bool IsTheme(const Extension
* extension
) {
3039 return extension
->is_theme();
3044 // Test updating a pending theme.
3045 // Disabled due to ASAN failure. http://crbug.com/108320
3046 TEST_F(ExtensionServiceTest
, DISABLED_UpdatePendingTheme
) {
3047 InitializeEmptyExtensionService();
3048 EXPECT_TRUE(service()->pending_extension_manager()->AddFromSync(
3049 theme_crx
, GURL(), &IsTheme
, false, false));
3050 EXPECT_TRUE(service()->pending_extension_manager()->IsIdPending(theme_crx
));
3052 base::FilePath path
= data_dir().AppendASCII("theme.crx");
3053 UpdateExtension(theme_crx
, path
, ENABLED
);
3055 EXPECT_FALSE(service()->pending_extension_manager()->IsIdPending(theme_crx
));
3057 const Extension
* extension
= service()->GetExtensionById(theme_crx
, true);
3058 ASSERT_TRUE(extension
);
3061 ExtensionPrefs::Get(profile())->IsExtensionDisabled(extension
->id()));
3062 EXPECT_TRUE(service()->IsExtensionEnabled(theme_crx
));
3065 #if defined(OS_CHROMEOS)
3066 // Always fails on ChromeOS: http://crbug.com/79737
3067 #define MAYBE_UpdatePendingExternalCrx DISABLED_UpdatePendingExternalCrx
3069 #define MAYBE_UpdatePendingExternalCrx UpdatePendingExternalCrx
3071 // Test updating a pending CRX as if the source is an external extension
3072 // with an update URL. In this case we don't know if the CRX is a theme
3074 TEST_F(ExtensionServiceTest
, MAYBE_UpdatePendingExternalCrx
) {
3075 InitializeEmptyExtensionService();
3076 EXPECT_TRUE(service()->pending_extension_manager()->AddFromExternalUpdateUrl(
3080 Manifest::EXTERNAL_PREF_DOWNLOAD
,
3081 Extension::NO_FLAGS
,
3084 EXPECT_TRUE(service()->pending_extension_manager()->IsIdPending(theme_crx
));
3086 base::FilePath path
= data_dir().AppendASCII("theme.crx");
3087 UpdateExtension(theme_crx
, path
, ENABLED
);
3089 EXPECT_FALSE(service()->pending_extension_manager()->IsIdPending(theme_crx
));
3091 const Extension
* extension
= service()->GetExtensionById(theme_crx
, true);
3092 ASSERT_TRUE(extension
);
3095 ExtensionPrefs::Get(profile())->IsExtensionDisabled(extension
->id()));
3096 EXPECT_TRUE(service()->IsExtensionEnabled(extension
->id()));
3098 extensions::util::IsIncognitoEnabled(extension
->id(), profile()));
3101 // Test updating a pending CRX as if the source is an external extension
3102 // with an update URL. The external update should overwrite a sync update,
3103 // but a sync update should not overwrite a non-sync update.
3104 TEST_F(ExtensionServiceTest
, UpdatePendingExternalCrxWinsOverSync
) {
3105 InitializeEmptyExtensionService();
3107 // Add a crx to be installed from the update mechanism.
3109 service()->pending_extension_manager()->AddFromSync(
3111 GURL(kGoodUpdateURL
),
3114 kGoodInstalledByCustodian
));
3116 // Check that there is a pending crx, with is_from_sync set to true.
3117 const extensions::PendingExtensionInfo
* pending_extension_info
;
3118 ASSERT_TRUE((pending_extension_info
=
3119 service()->pending_extension_manager()->GetById(kGoodId
)));
3120 EXPECT_TRUE(pending_extension_info
->is_from_sync());
3122 // Add a crx to be updated, with the same ID, from a non-sync source.
3123 EXPECT_TRUE(service()->pending_extension_manager()->AddFromExternalUpdateUrl(
3126 GURL(kGoodUpdateURL
),
3127 Manifest::EXTERNAL_PREF_DOWNLOAD
,
3128 Extension::NO_FLAGS
,
3131 // Check that there is a pending crx, with is_from_sync set to false.
3132 ASSERT_TRUE((pending_extension_info
=
3133 service()->pending_extension_manager()->GetById(kGoodId
)));
3134 EXPECT_FALSE(pending_extension_info
->is_from_sync());
3135 EXPECT_EQ(Manifest::EXTERNAL_PREF_DOWNLOAD
,
3136 pending_extension_info
->install_source());
3138 // Add a crx to be installed from the update mechanism.
3140 service()->pending_extension_manager()->AddFromSync(
3142 GURL(kGoodUpdateURL
),
3145 kGoodInstalledByCustodian
));
3147 // Check that the external, non-sync update was not overridden.
3148 ASSERT_TRUE((pending_extension_info
=
3149 service()->pending_extension_manager()->GetById(kGoodId
)));
3150 EXPECT_FALSE(pending_extension_info
->is_from_sync());
3151 EXPECT_EQ(Manifest::EXTERNAL_PREF_DOWNLOAD
,
3152 pending_extension_info
->install_source());
3155 // Updating a theme should fail if the updater is explicitly told that
3156 // the CRX is not a theme.
3157 TEST_F(ExtensionServiceTest
, UpdatePendingCrxThemeMismatch
) {
3158 InitializeEmptyExtensionService();
3159 EXPECT_TRUE(service()->pending_extension_manager()->AddFromSync(
3160 theme_crx
, GURL(), &IsExtension
, false, false));
3162 EXPECT_TRUE(service()->pending_extension_manager()->IsIdPending(theme_crx
));
3164 base::FilePath path
= data_dir().AppendASCII("theme.crx");
3165 UpdateExtension(theme_crx
, path
, FAILED_SILENTLY
);
3167 EXPECT_FALSE(service()->pending_extension_manager()->IsIdPending(theme_crx
));
3169 const Extension
* extension
= service()->GetExtensionById(theme_crx
, true);
3170 ASSERT_FALSE(extension
);
3173 // TODO(akalin): Test updating a pending extension non-silently once
3174 // we can mock out ExtensionInstallUI and inject our version into
3175 // UpdateExtension().
3177 // Test updating a pending extension which fails the should-install test.
3178 TEST_F(ExtensionServiceTest
, UpdatePendingExtensionFailedShouldInstallTest
) {
3179 InitializeEmptyExtensionService();
3180 // Add pending extension with a flipped is_theme.
3182 service()->pending_extension_manager()->AddFromSync(
3184 GURL(kGoodUpdateURL
),
3187 kGoodInstalledByCustodian
));
3188 EXPECT_TRUE(service()->pending_extension_manager()->IsIdPending(kGoodId
));
3190 base::FilePath path
= data_dir().AppendASCII("good.crx");
3191 UpdateExtension(kGoodId
, path
, UPDATED
);
3193 // TODO(akalin): Figure out how to check that the extensions
3194 // directory is cleaned up properly in OnExtensionInstalled().
3196 EXPECT_FALSE(service()->pending_extension_manager()->IsIdPending(kGoodId
));
3199 // TODO(akalin): Figure out how to test that installs of pending
3200 // unsyncable extensions are blocked.
3202 // Test updating a pending extension for one that is not pending.
3203 TEST_F(ExtensionServiceTest
, UpdatePendingExtensionNotPending
) {
3204 InitializeEmptyExtensionService();
3206 base::FilePath path
= data_dir().AppendASCII("good.crx");
3207 UpdateExtension(kGoodId
, path
, UPDATED
);
3209 EXPECT_FALSE(service()->pending_extension_manager()->IsIdPending(kGoodId
));
3212 // Test updating a pending extension for one that is already
3214 TEST_F(ExtensionServiceTest
, UpdatePendingExtensionAlreadyInstalled
) {
3215 InitializeEmptyExtensionService();
3217 base::FilePath path
= data_dir().AppendASCII("good.crx");
3218 const Extension
* good
= InstallCRX(path
, INSTALL_NEW
);
3219 ASSERT_EQ(1u, registry()->enabled_extensions().size());
3221 EXPECT_FALSE(good
->is_theme());
3223 // Use AddExtensionImpl() as AddFrom*() would balk.
3224 service()->pending_extension_manager()->AddExtensionImpl(
3227 extensions::ManifestURL::GetUpdateURL(good
),
3232 Extension::NO_FLAGS
,
3234 kGoodRemoteInstall
);
3235 UpdateExtension(good
->id(), path
, ENABLED
);
3237 EXPECT_FALSE(service()->pending_extension_manager()->IsIdPending(kGoodId
));
3240 #if defined(ENABLE_BLACKLIST_TESTS)
3241 // Tests blacklisting then unblacklisting extensions after the service has been
3243 TEST_F(ExtensionServiceTest
, SetUnsetBlacklistInPrefs
) {
3244 extensions::TestBlacklist test_blacklist
;
3245 // A profile with 3 extensions installed: good0, good1, and good2.
3246 InitializeGoodInstalledExtensionService();
3247 test_blacklist
.Attach(service()->blacklist_
);
3250 const extensions::ExtensionSet
& enabled_extensions
=
3251 registry()->enabled_extensions();
3252 const extensions::ExtensionSet
& blacklisted_extensions
=
3253 registry()->blacklisted_extensions();
3255 EXPECT_TRUE(enabled_extensions
.Contains(good0
) &&
3256 !blacklisted_extensions
.Contains(good0
));
3257 EXPECT_TRUE(enabled_extensions
.Contains(good1
) &&
3258 !blacklisted_extensions
.Contains(good1
));
3259 EXPECT_TRUE(enabled_extensions
.Contains(good2
) &&
3260 !blacklisted_extensions
.Contains(good2
));
3262 EXPECT_FALSE(IsPrefExist(good0
, "blacklist"));
3263 EXPECT_FALSE(IsPrefExist(good1
, "blacklist"));
3264 EXPECT_FALSE(IsPrefExist(good2
, "blacklist"));
3265 EXPECT_FALSE(IsPrefExist("invalid_id", "blacklist"));
3267 // Blacklist good0 and good1 (and an invalid extension ID).
3268 test_blacklist
.SetBlacklistState(
3269 good0
, extensions::BLACKLISTED_MALWARE
, true);
3270 test_blacklist
.SetBlacklistState(
3271 good1
, extensions::BLACKLISTED_MALWARE
, true);
3272 test_blacklist
.SetBlacklistState(
3273 "invalid_id", extensions::BLACKLISTED_MALWARE
, true);
3274 base::RunLoop().RunUntilIdle();
3276 EXPECT_TRUE(!enabled_extensions
.Contains(good0
) &&
3277 blacklisted_extensions
.Contains(good0
));
3278 EXPECT_TRUE(!enabled_extensions
.Contains(good1
) &&
3279 blacklisted_extensions
.Contains(good1
));
3280 EXPECT_TRUE(enabled_extensions
.Contains(good2
) &&
3281 !blacklisted_extensions
.Contains(good2
));
3283 EXPECT_TRUE(ValidateBooleanPref(good0
, "blacklist", true));
3284 EXPECT_TRUE(ValidateBooleanPref(good1
, "blacklist", true));
3285 EXPECT_FALSE(IsPrefExist(good2
, "blacklist"));
3286 EXPECT_FALSE(IsPrefExist("invalid_id", "blacklist"));
3288 // Un-blacklist good1 and blacklist good2.
3289 test_blacklist
.Clear(false);
3290 test_blacklist
.SetBlacklistState(
3291 good0
, extensions::BLACKLISTED_MALWARE
, true);
3292 test_blacklist
.SetBlacklistState(
3293 good2
, extensions::BLACKLISTED_MALWARE
, true);
3294 test_blacklist
.SetBlacklistState(
3295 "invalid_id", extensions::BLACKLISTED_MALWARE
, true);
3296 base::RunLoop().RunUntilIdle();
3298 EXPECT_TRUE(!enabled_extensions
.Contains(good0
) &&
3299 blacklisted_extensions
.Contains(good0
));
3300 EXPECT_TRUE(enabled_extensions
.Contains(good1
) &&
3301 !blacklisted_extensions
.Contains(good1
));
3302 EXPECT_TRUE(!enabled_extensions
.Contains(good2
) &&
3303 blacklisted_extensions
.Contains(good2
));
3305 EXPECT_TRUE(ValidateBooleanPref(good0
, "blacklist", true));
3306 EXPECT_FALSE(IsPrefExist(good1
, "blacklist"));
3307 EXPECT_TRUE(ValidateBooleanPref(good2
, "blacklist", true));
3308 EXPECT_FALSE(IsPrefExist("invalid_id", "blacklist"));
3310 #endif // defined(ENABLE_BLACKLIST_TESTS)
3312 #if defined(ENABLE_BLACKLIST_TESTS)
3313 // Tests trying to install a blacklisted extension.
3314 TEST_F(ExtensionServiceTest
, BlacklistedExtensionWillNotInstall
) {
3315 scoped_refptr
<FakeSafeBrowsingDatabaseManager
> blacklist_db(
3316 new FakeSafeBrowsingDatabaseManager(true));
3317 Blacklist::ScopedDatabaseManagerForTest
scoped_blacklist_db(blacklist_db
);
3319 InitializeEmptyExtensionService();
3322 // After blacklisting good_crx, we cannot install it.
3323 blacklist_db
->SetUnsafe(good_crx
).NotifyUpdate();
3324 base::RunLoop().RunUntilIdle();
3326 base::FilePath path
= data_dir().AppendASCII("good.crx");
3327 // HACK: specify WAS_INSTALLED_BY_DEFAULT so that test machinery doesn't
3328 // decide to install this silently. Somebody should fix these tests, all
3329 // 6,000 lines of them. Hah!
3330 InstallCRX(path
, INSTALL_FAILED
, Extension::WAS_INSTALLED_BY_DEFAULT
);
3331 EXPECT_EQ(0u, registry()->enabled_extensions().size());
3333 #endif // defined(ENABLE_BLACKLIST_TESTS)
3335 #if defined(ENABLE_BLACKLIST_TESTS)
3336 // Unload blacklisted extension on policy change.
3337 TEST_F(ExtensionServiceTest
, UnloadBlacklistedExtensionPolicy
) {
3338 extensions::TestBlacklist test_blacklist
;
3340 // A profile with no extensions installed.
3341 InitializeEmptyExtensionServiceWithTestingPrefs();
3342 test_blacklist
.Attach(service()->blacklist_
);
3344 base::FilePath path
= data_dir().AppendASCII("good.crx");
3346 const Extension
* good
= InstallCRX(path
, INSTALL_NEW
);
3347 EXPECT_EQ(good_crx
, good
->id());
3348 UpdateExtension(good_crx
, path
, FAILED_SILENTLY
);
3349 EXPECT_EQ(1u, registry()->enabled_extensions().size());
3352 ManagementPrefUpdater
pref(profile_
->GetTestingPrefService());
3353 pref
.SetIndividualExtensionInstallationAllowed(good_crx
, true);
3356 test_blacklist
.SetBlacklistState(
3357 good_crx
, extensions::BLACKLISTED_MALWARE
, true);
3358 base::RunLoop().RunUntilIdle();
3360 // The good_crx is blacklisted and the whitelist doesn't negate it.
3361 ASSERT_TRUE(ValidateBooleanPref(good_crx
, "blacklist", true));
3362 EXPECT_EQ(0u, registry()->enabled_extensions().size());
3364 #endif // defined(ENABLE_BLACKLIST_TESTS)
3366 #if defined(ENABLE_BLACKLIST_TESTS)
3367 // Tests that a blacklisted extension is eventually unloaded on startup, if it
3369 TEST_F(ExtensionServiceTest
, WillNotLoadBlacklistedExtensionsFromDirectory
) {
3370 extensions::TestBlacklist test_blacklist
;
3372 // A profile with 3 extensions installed: good0, good1, and good2.
3373 InitializeGoodInstalledExtensionService();
3374 test_blacklist
.Attach(service()->blacklist_
);
3376 // Blacklist good1 before the service initializes.
3377 test_blacklist
.SetBlacklistState(
3378 good1
, extensions::BLACKLISTED_MALWARE
, false);
3382 ASSERT_EQ(3u, loaded_
.size()); // hasn't had time to blacklist yet
3384 base::RunLoop().RunUntilIdle();
3386 ASSERT_EQ(1u, registry()->blacklisted_extensions().size());
3387 ASSERT_EQ(2u, registry()->enabled_extensions().size());
3389 ASSERT_TRUE(registry()->enabled_extensions().Contains(good0
));
3390 ASSERT_TRUE(registry()->blacklisted_extensions().Contains(good1
));
3391 ASSERT_TRUE(registry()->enabled_extensions().Contains(good2
));
3393 #endif // defined(ENABLE_BLACKLIST_TESTS)
3395 #if defined(ENABLE_BLACKLIST_TESTS)
3396 // Tests extensions blacklisted in prefs on startup; one still blacklisted by
3397 // safe browsing, the other not. The not-blacklisted one should recover.
3398 TEST_F(ExtensionServiceTest
, BlacklistedInPrefsFromStartup
) {
3399 extensions::TestBlacklist test_blacklist
;
3401 InitializeGoodInstalledExtensionService();
3402 test_blacklist
.Attach(service()->blacklist_
);
3403 ExtensionPrefs::Get(profile())->SetExtensionBlacklisted(good0
, true);
3404 ExtensionPrefs::Get(profile())->SetExtensionBlacklisted(good1
, true);
3406 test_blacklist
.SetBlacklistState(
3407 good1
, extensions::BLACKLISTED_MALWARE
, false);
3409 // Extension service hasn't loaded yet, but IsExtensionEnabled reads out of
3410 // prefs. Ensure it takes into account the blacklist state (crbug.com/373842).
3411 EXPECT_FALSE(service()->IsExtensionEnabled(good0
));
3412 EXPECT_FALSE(service()->IsExtensionEnabled(good1
));
3413 EXPECT_TRUE(service()->IsExtensionEnabled(good2
));
3417 EXPECT_EQ(2u, registry()->blacklisted_extensions().size());
3418 EXPECT_EQ(1u, registry()->enabled_extensions().size());
3420 EXPECT_TRUE(registry()->blacklisted_extensions().Contains(good0
));
3421 EXPECT_TRUE(registry()->blacklisted_extensions().Contains(good1
));
3422 EXPECT_TRUE(registry()->enabled_extensions().Contains(good2
));
3424 // Give time for the blacklist to update.
3425 base::RunLoop().RunUntilIdle();
3427 EXPECT_EQ(1u, registry()->blacklisted_extensions().size());
3428 EXPECT_EQ(2u, registry()->enabled_extensions().size());
3430 EXPECT_TRUE(registry()->enabled_extensions().Contains(good0
));
3431 EXPECT_TRUE(registry()->blacklisted_extensions().Contains(good1
));
3432 EXPECT_TRUE(registry()->enabled_extensions().Contains(good2
));
3434 #endif // defined(ENABLE_BLACKLIST_TESTS)
3436 #if defined(ENABLE_BLACKLIST_TESTS)
3437 // Extension is added to blacklist with BLACKLISTED_POTENTIALLY_UNWANTED state
3438 // after it is installed. It is then successfully re-enabled by the user.
3439 TEST_F(ExtensionServiceTest
, GreylistedExtensionDisabled
) {
3440 extensions::TestBlacklist test_blacklist
;
3441 // A profile with 3 extensions installed: good0, good1, and good2.
3442 InitializeGoodInstalledExtensionService();
3443 test_blacklist
.Attach(service()->blacklist_
);
3446 const extensions::ExtensionSet
& enabled_extensions
=
3447 registry()->enabled_extensions();
3448 const extensions::ExtensionSet
& disabled_extensions
=
3449 registry()->disabled_extensions();
3451 EXPECT_TRUE(enabled_extensions
.Contains(good0
));
3452 EXPECT_TRUE(enabled_extensions
.Contains(good1
));
3453 EXPECT_TRUE(enabled_extensions
.Contains(good2
));
3455 // Blacklist good0 and good1 (and an invalid extension ID).
3456 test_blacklist
.SetBlacklistState(
3457 good0
, extensions::BLACKLISTED_CWS_POLICY_VIOLATION
, true);
3458 test_blacklist
.SetBlacklistState(
3459 good1
, extensions::BLACKLISTED_POTENTIALLY_UNWANTED
, true);
3460 test_blacklist
.SetBlacklistState(
3461 "invalid_id", extensions::BLACKLISTED_MALWARE
, true);
3462 base::RunLoop().RunUntilIdle();
3464 EXPECT_FALSE(enabled_extensions
.Contains(good0
));
3465 EXPECT_TRUE(disabled_extensions
.Contains(good0
));
3466 EXPECT_FALSE(enabled_extensions
.Contains(good1
));
3467 EXPECT_TRUE(disabled_extensions
.Contains(good1
));
3468 EXPECT_TRUE(enabled_extensions
.Contains(good2
));
3469 EXPECT_FALSE(disabled_extensions
.Contains(good2
));
3471 ValidateIntegerPref(
3472 good0
, "blacklist_state", extensions::BLACKLISTED_CWS_POLICY_VIOLATION
);
3473 ValidateIntegerPref(
3474 good1
, "blacklist_state", extensions::BLACKLISTED_POTENTIALLY_UNWANTED
);
3476 // Now user enables good0.
3477 service()->EnableExtension(good0
);
3479 EXPECT_TRUE(enabled_extensions
.Contains(good0
));
3480 EXPECT_FALSE(disabled_extensions
.Contains(good0
));
3481 EXPECT_FALSE(enabled_extensions
.Contains(good1
));
3482 EXPECT_TRUE(disabled_extensions
.Contains(good1
));
3484 // Remove extensions from blacklist.
3485 test_blacklist
.SetBlacklistState(
3486 good0
, extensions::NOT_BLACKLISTED
, true);
3487 test_blacklist
.SetBlacklistState(
3488 good1
, extensions::NOT_BLACKLISTED
, true);
3489 base::RunLoop().RunUntilIdle();
3491 // All extensions are enabled.
3492 EXPECT_TRUE(enabled_extensions
.Contains(good0
));
3493 EXPECT_FALSE(disabled_extensions
.Contains(good0
));
3494 EXPECT_TRUE(enabled_extensions
.Contains(good1
));
3495 EXPECT_FALSE(disabled_extensions
.Contains(good1
));
3496 EXPECT_TRUE(enabled_extensions
.Contains(good2
));
3497 EXPECT_FALSE(disabled_extensions
.Contains(good2
));
3499 #endif // defined(ENABLE_BLACKLIST_TESTS)
3501 #if defined(ENABLE_BLACKLIST_TESTS)
3502 // When extension is removed from greylist, do not re-enable it if it is
3503 // disabled by user.
3504 TEST_F(ExtensionServiceTest
, GreylistDontEnableManuallyDisabled
) {
3505 extensions::TestBlacklist test_blacklist
;
3506 // A profile with 3 extensions installed: good0, good1, and good2.
3507 InitializeGoodInstalledExtensionService();
3508 test_blacklist
.Attach(service()->blacklist_
);
3511 const extensions::ExtensionSet
& enabled_extensions
=
3512 registry()->enabled_extensions();
3513 const extensions::ExtensionSet
& disabled_extensions
=
3514 registry()->disabled_extensions();
3516 // Manually disable.
3517 service()->DisableExtension(good0
,
3518 extensions::Extension::DISABLE_USER_ACTION
);
3520 test_blacklist
.SetBlacklistState(
3521 good0
, extensions::BLACKLISTED_CWS_POLICY_VIOLATION
, true);
3522 test_blacklist
.SetBlacklistState(
3523 good1
, extensions::BLACKLISTED_POTENTIALLY_UNWANTED
, true);
3524 test_blacklist
.SetBlacklistState(
3525 good2
, extensions::BLACKLISTED_SECURITY_VULNERABILITY
, true);
3526 base::RunLoop().RunUntilIdle();
3528 // All extensions disabled.
3529 EXPECT_FALSE(enabled_extensions
.Contains(good0
));
3530 EXPECT_TRUE(disabled_extensions
.Contains(good0
));
3531 EXPECT_FALSE(enabled_extensions
.Contains(good1
));
3532 EXPECT_TRUE(disabled_extensions
.Contains(good1
));
3533 EXPECT_FALSE(enabled_extensions
.Contains(good2
));
3534 EXPECT_TRUE(disabled_extensions
.Contains(good2
));
3536 // Greylisted extension can be enabled.
3537 service()->EnableExtension(good1
);
3538 EXPECT_TRUE(enabled_extensions
.Contains(good1
));
3539 EXPECT_FALSE(disabled_extensions
.Contains(good1
));
3541 // good1 is now manually disabled.
3542 service()->DisableExtension(good1
,
3543 extensions::Extension::DISABLE_USER_ACTION
);
3544 EXPECT_FALSE(enabled_extensions
.Contains(good1
));
3545 EXPECT_TRUE(disabled_extensions
.Contains(good1
));
3547 // Remove extensions from blacklist.
3548 test_blacklist
.SetBlacklistState(
3549 good0
, extensions::NOT_BLACKLISTED
, true);
3550 test_blacklist
.SetBlacklistState(
3551 good1
, extensions::NOT_BLACKLISTED
, true);
3552 test_blacklist
.SetBlacklistState(
3553 good2
, extensions::NOT_BLACKLISTED
, true);
3554 base::RunLoop().RunUntilIdle();
3556 // good0 and good1 remain disabled.
3557 EXPECT_FALSE(enabled_extensions
.Contains(good0
));
3558 EXPECT_TRUE(disabled_extensions
.Contains(good0
));
3559 EXPECT_FALSE(enabled_extensions
.Contains(good1
));
3560 EXPECT_TRUE(disabled_extensions
.Contains(good1
));
3561 EXPECT_TRUE(enabled_extensions
.Contains(good2
));
3562 EXPECT_FALSE(disabled_extensions
.Contains(good2
));
3564 #endif // defined(ENABLE_BLACKLIST_TESTS)
3566 #if defined(ENABLE_BLACKLIST_TESTS)
3567 // Blacklisted extension with unknown state are not enabled/disabled.
3568 TEST_F(ExtensionServiceTest
, GreylistUnknownDontChange
) {
3569 extensions::TestBlacklist test_blacklist
;
3570 // A profile with 3 extensions installed: good0, good1, and good2.
3571 InitializeGoodInstalledExtensionService();
3572 test_blacklist
.Attach(service()->blacklist_
);
3575 const extensions::ExtensionSet
& enabled_extensions
=
3576 registry()->enabled_extensions();
3577 const extensions::ExtensionSet
& disabled_extensions
=
3578 registry()->disabled_extensions();
3580 test_blacklist
.SetBlacklistState(
3581 good0
, extensions::BLACKLISTED_CWS_POLICY_VIOLATION
, true);
3582 test_blacklist
.SetBlacklistState(
3583 good1
, extensions::BLACKLISTED_POTENTIALLY_UNWANTED
, true);
3584 base::RunLoop().RunUntilIdle();
3586 EXPECT_FALSE(enabled_extensions
.Contains(good0
));
3587 EXPECT_TRUE(disabled_extensions
.Contains(good0
));
3588 EXPECT_FALSE(enabled_extensions
.Contains(good1
));
3589 EXPECT_TRUE(disabled_extensions
.Contains(good1
));
3590 EXPECT_TRUE(enabled_extensions
.Contains(good2
));
3591 EXPECT_FALSE(disabled_extensions
.Contains(good2
));
3593 test_blacklist
.SetBlacklistState(
3594 good0
, extensions::NOT_BLACKLISTED
, true);
3595 test_blacklist
.SetBlacklistState(
3596 good1
, extensions::BLACKLISTED_UNKNOWN
, true);
3597 test_blacklist
.SetBlacklistState(
3598 good2
, extensions::BLACKLISTED_UNKNOWN
, true);
3599 base::RunLoop().RunUntilIdle();
3601 // good0 re-enabled, other remain as they were.
3602 EXPECT_TRUE(enabled_extensions
.Contains(good0
));
3603 EXPECT_FALSE(disabled_extensions
.Contains(good0
));
3604 EXPECT_FALSE(enabled_extensions
.Contains(good1
));
3605 EXPECT_TRUE(disabled_extensions
.Contains(good1
));
3606 EXPECT_TRUE(enabled_extensions
.Contains(good2
));
3607 EXPECT_FALSE(disabled_extensions
.Contains(good2
));
3610 // Tests that blacklisted extensions cannot be reloaded, both those loaded
3611 // before and after extension service startup.
3612 TEST_F(ExtensionServiceTest
, ReloadBlacklistedExtension
) {
3613 extensions::TestBlacklist test_blacklist
;
3615 InitializeGoodInstalledExtensionService();
3616 test_blacklist
.Attach(service()->blacklist_
);
3618 test_blacklist
.SetBlacklistState(
3619 good1
, extensions::BLACKLISTED_MALWARE
, false);
3621 test_blacklist
.SetBlacklistState(
3622 good2
, extensions::BLACKLISTED_MALWARE
, false);
3623 base::RunLoop().RunUntilIdle();
3625 EXPECT_EQ(StringSet(good0
), registry()->enabled_extensions().GetIDs());
3626 EXPECT_EQ(StringSet(good1
, good2
),
3627 registry()->blacklisted_extensions().GetIDs());
3629 service()->ReloadExtension(good1
);
3630 service()->ReloadExtension(good2
);
3631 base::RunLoop().RunUntilIdle();
3633 EXPECT_EQ(StringSet(good0
), registry()->enabled_extensions().GetIDs());
3634 EXPECT_EQ(StringSet(good1
, good2
),
3635 registry()->blacklisted_extensions().GetIDs());
3637 #endif // defined(ENABLE_BLACKLIST_TESTS)
3639 // Tests blocking then unblocking enabled extensions after the service has been
3641 TEST_F(ExtensionServiceTest
, BlockAndUnblockEnabledExtension
) {
3642 InitializeGoodInstalledExtensionService();
3645 AssertExtensionBlocksAndUnblocks(true, good0
);
3648 // Tests blocking then unblocking disabled extensions after the service has been
3650 TEST_F(ExtensionServiceTest
, BlockAndUnblockDisabledExtension
) {
3651 InitializeGoodInstalledExtensionService();
3654 service()->DisableExtension(good0
, Extension::DISABLE_RELOAD
);
3656 AssertExtensionBlocksAndUnblocks(true, good0
);
3659 // Tests blocking then unblocking terminated extensions after the service has
3660 // been initialized.
3661 TEST_F(ExtensionServiceTest
, BlockAndUnblockTerminatedExtension
) {
3662 InitializeGoodInstalledExtensionService();
3665 TerminateExtension(good0
);
3667 AssertExtensionBlocksAndUnblocks(true, good0
);
3670 // Tests blocking then unblocking policy-forced extensions after the service has
3671 // been initialized.
3672 TEST_F(ExtensionServiceTest
, BlockAndUnblockPolicyExtension
) {
3673 InitializeEmptyExtensionServiceWithTestingPrefs();
3676 ManagementPrefUpdater
pref(profile_
->GetTestingPrefService());
3677 // // Blacklist everything.
3678 // pref.SetBlacklistedByDefault(true);
3679 // Mark good.crx for force-installation.
3680 pref
.SetIndividualExtensionAutoInstalled(
3681 good_crx
, "http://example.com/update_url", true);
3684 // Have policy force-install an extension.
3685 MockExtensionProvider
* provider
=
3686 new MockExtensionProvider(service(), Manifest::EXTERNAL_POLICY_DOWNLOAD
);
3687 AddMockExternalProvider(provider
);
3688 provider
->UpdateOrAddExtension(
3689 good_crx
, "1.0.0.0", data_dir().AppendASCII("good_crx"));
3691 // Reloading extensions should find our externally registered extension
3693 content::WindowedNotificationObserver
observer(
3694 extensions::NOTIFICATION_CRX_INSTALLER_DONE
,
3695 content::NotificationService::AllSources());
3696 service()->CheckForExternalUpdates();
3699 AssertExtensionBlocksAndUnblocks(false, good_crx
);
3703 #if defined(ENABLE_BLACKLIST_TESTS)
3704 // Tests blocking then unblocking extensions that are blacklisted both before
3705 // and after Init().
3706 TEST_F(ExtensionServiceTest
, BlockAndUnblockBlacklistedExtension
) {
3707 extensions::TestBlacklist test_blacklist
;
3709 InitializeGoodInstalledExtensionService();
3710 test_blacklist
.Attach(service()->blacklist_
);
3712 test_blacklist
.SetBlacklistState(
3713 good0
, extensions::BLACKLISTED_MALWARE
, true);
3714 base::RunLoop().RunUntilIdle();
3718 test_blacklist
.SetBlacklistState(
3719 good1
, extensions::BLACKLISTED_MALWARE
, true);
3720 base::RunLoop().RunUntilIdle();
3722 // Blacklisted extensions stay blacklisted.
3723 AssertExtensionBlocksAndUnblocks(false, good0
);
3724 AssertExtensionBlocksAndUnblocks(false, good1
);
3726 service()->BlockAllExtensions();
3728 // Remove an extension from the blacklist while the service is blocked.
3729 test_blacklist
.SetBlacklistState(
3730 good0
, extensions::NOT_BLACKLISTED
, true);
3731 // Add an extension to the blacklist while the service is blocked.
3732 test_blacklist
.SetBlacklistState(
3733 good2
, extensions::BLACKLISTED_MALWARE
, true);
3734 base::RunLoop().RunUntilIdle();
3736 // Go directly to blocked, do not pass go, do not collect $200.
3737 ASSERT_TRUE(IsBlocked(good0
));
3738 // Get on the blacklist - even if you were blocked!
3739 ASSERT_FALSE(IsBlocked(good2
));
3741 #endif // defined(ENABLE_BLACKLIST_TESTS)
3743 // Tests blocking then unblocking enabled component extensions after the service
3744 // has been initialized.
3745 TEST_F(ExtensionServiceTest
, BlockAndUnblockEnabledComponentExtension
) {
3746 InitializeEmptyExtensionServiceWithTestingPrefs();
3748 // Install a component extension.
3749 base::FilePath path
= data_dir()
3750 .AppendASCII("good")
3751 .AppendASCII("Extensions")
3753 .AppendASCII("1.0.0.0");
3754 std::string manifest
;
3755 ASSERT_TRUE(base::ReadFileToString(
3756 path
.Append(extensions::kManifestFilename
), &manifest
));
3757 service()->component_loader()->Add(manifest
, path
);
3760 // Component extension should never block.
3761 AssertExtensionBlocksAndUnblocks(false, good0
);
3764 // Tests blocking then unblocking a theme after the service has been
3766 TEST_F(ExtensionServiceTest
, BlockAndUnblockTheme
) {
3767 InitializeEmptyExtensionService();
3770 base::FilePath path
= data_dir().AppendASCII("theme.crx");
3771 InstallCRX(path
, INSTALL_NEW
);
3773 AssertExtensionBlocksAndUnblocks(true, theme_crx
);
3776 // Tests that blocking extensions before Init() results in loading blocked
3778 TEST_F(ExtensionServiceTest
, WillNotLoadExtensionsWhenBlocked
) {
3779 InitializeGoodInstalledExtensionService();
3781 service()->BlockAllExtensions();
3785 ASSERT_TRUE(IsBlocked(good0
));
3786 ASSERT_TRUE(IsBlocked(good0
));
3787 ASSERT_TRUE(IsBlocked(good0
));
3790 // Tests that IsEnabledExtension won't crash on an uninstalled extension.
3791 TEST_F(ExtensionServiceTest
, IsEnabledExtensionBlockedAndNotInstalled
) {
3792 InitializeEmptyExtensionService();
3794 service()->BlockAllExtensions();
3796 service()->IsExtensionEnabled(theme_crx
);
3799 // Will not install extension blacklisted by policy.
3800 TEST_F(ExtensionServiceTest
, BlacklistedByPolicyWillNotInstall
) {
3801 InitializeEmptyExtensionServiceWithTestingPrefs();
3803 // Blacklist everything.
3805 ManagementPrefUpdater
pref(profile_
->GetTestingPrefService());
3806 pref
.SetBlacklistedByDefault(true);
3809 // Blacklist prevents us from installing good_crx.
3810 base::FilePath path
= data_dir().AppendASCII("good.crx");
3811 InstallCRX(path
, INSTALL_FAILED
);
3812 EXPECT_EQ(0u, registry()->enabled_extensions().size());
3814 // Now whitelist this particular extension.
3816 ManagementPrefUpdater
pref(profile_
->GetTestingPrefService());
3817 pref
.SetIndividualExtensionInstallationAllowed(good_crx
, true);
3820 // Ensure we can now install good_crx.
3821 InstallCRX(path
, INSTALL_NEW
);
3822 EXPECT_EQ(1u, registry()->enabled_extensions().size());
3825 // Extension blacklisted by policy get unloaded after installing.
3826 TEST_F(ExtensionServiceTest
, BlacklistedByPolicyRemovedIfRunning
) {
3827 InitializeEmptyExtensionServiceWithTestingPrefs();
3829 // Install good_crx.
3830 base::FilePath path
= data_dir().AppendASCII("good.crx");
3831 InstallCRX(path
, INSTALL_NEW
);
3832 EXPECT_EQ(1u, registry()->enabled_extensions().size());
3835 ManagementPrefUpdater
pref(profile_
->GetTestingPrefService());
3836 // Blacklist this extension.
3837 pref
.SetIndividualExtensionInstallationAllowed(good_crx
, false);
3840 // Extension should not be running now.
3841 base::RunLoop().RunUntilIdle();
3842 EXPECT_EQ(0u, registry()->enabled_extensions().size());
3845 // Tests that component extensions are not blacklisted by policy.
3846 TEST_F(ExtensionServiceTest
, ComponentExtensionWhitelisted
) {
3847 InitializeEmptyExtensionServiceWithTestingPrefs();
3849 // Blacklist everything.
3851 ManagementPrefUpdater
pref(profile_
->GetTestingPrefService());
3852 pref
.SetBlacklistedByDefault(true);
3855 // Install a component extension.
3856 base::FilePath path
= data_dir()
3857 .AppendASCII("good")
3858 .AppendASCII("Extensions")
3860 .AppendASCII("1.0.0.0");
3861 std::string manifest
;
3862 ASSERT_TRUE(base::ReadFileToString(
3863 path
.Append(extensions::kManifestFilename
), &manifest
));
3864 service()->component_loader()->Add(manifest
, path
);
3867 // Extension should be installed despite blacklist.
3868 ASSERT_EQ(1u, registry()->enabled_extensions().size());
3869 EXPECT_TRUE(service()->GetExtensionById(good0
, false));
3871 // Poke external providers and make sure the extension is still present.
3872 service()->CheckForExternalUpdates();
3873 ASSERT_EQ(1u, registry()->enabled_extensions().size());
3874 EXPECT_TRUE(service()->GetExtensionById(good0
, false));
3876 // Extension should not be uninstalled on blacklist changes.
3878 ManagementPrefUpdater
pref(profile_
->GetTestingPrefService());
3879 pref
.SetIndividualExtensionInstallationAllowed(good0
, false);
3881 base::RunLoop().RunUntilIdle();
3882 ASSERT_EQ(1u, registry()->enabled_extensions().size());
3883 EXPECT_TRUE(service()->GetExtensionById(good0
, false));
3886 // Tests that policy-installed extensions are not blacklisted by policy.
3887 TEST_F(ExtensionServiceTest
, PolicyInstalledExtensionsWhitelisted
) {
3888 InitializeEmptyExtensionServiceWithTestingPrefs();
3891 ManagementPrefUpdater
pref(profile_
->GetTestingPrefService());
3892 // Blacklist everything.
3893 pref
.SetBlacklistedByDefault(true);
3894 // Mark good.crx for force-installation.
3895 pref
.SetIndividualExtensionAutoInstalled(
3896 good_crx
, "http://example.com/update_url", true);
3899 // Have policy force-install an extension.
3900 MockExtensionProvider
* provider
=
3901 new MockExtensionProvider(service(), Manifest::EXTERNAL_POLICY_DOWNLOAD
);
3902 AddMockExternalProvider(provider
);
3903 provider
->UpdateOrAddExtension(
3904 good_crx
, "1.0.0.0", data_dir().AppendASCII("good.crx"));
3906 // Reloading extensions should find our externally registered extension
3908 content::WindowedNotificationObserver
observer(
3909 extensions::NOTIFICATION_CRX_INSTALLER_DONE
,
3910 content::NotificationService::AllSources());
3911 service()->CheckForExternalUpdates();
3914 // Extension should be installed despite blacklist.
3915 ASSERT_EQ(1u, registry()->enabled_extensions().size());
3916 EXPECT_TRUE(service()->GetExtensionById(good_crx
, false));
3918 // Blacklist update should not uninstall the extension.
3920 ManagementPrefUpdater
pref(profile_
->GetTestingPrefService());
3921 pref
.SetIndividualExtensionInstallationAllowed(good0
, false);
3923 base::RunLoop().RunUntilIdle();
3924 ASSERT_EQ(1u, registry()->enabled_extensions().size());
3925 EXPECT_TRUE(service()->GetExtensionById(good_crx
, false));
3928 // Tests that extensions cannot be installed if the policy provider prohibits
3929 // it. This functionality is implemented in CrxInstaller::ConfirmInstall().
3930 TEST_F(ExtensionServiceTest
, ManagementPolicyProhibitsInstall
) {
3931 InitializeEmptyExtensionService();
3933 GetManagementPolicy()->UnregisterAllProviders();
3934 extensions::TestManagementPolicyProvider
provider_(
3935 extensions::TestManagementPolicyProvider::PROHIBIT_LOAD
);
3936 GetManagementPolicy()->RegisterProvider(&provider_
);
3938 InstallCRX(data_dir().AppendASCII("good.crx"), INSTALL_FAILED
);
3939 EXPECT_EQ(0u, registry()->enabled_extensions().size());
3942 // Tests that extensions cannot be loaded from prefs if the policy provider
3943 // prohibits it. This functionality is implemented in InstalledLoader::Load().
3944 TEST_F(ExtensionServiceTest
, ManagementPolicyProhibitsLoadFromPrefs
) {
3945 InitializeEmptyExtensionService();
3947 // Create a fake extension to be loaded as though it were read from prefs.
3948 base::FilePath path
=
3949 data_dir().AppendASCII("management").AppendASCII("simple_extension");
3950 base::DictionaryValue manifest
;
3951 manifest
.SetString(keys::kName
, "simple_extension");
3952 manifest
.SetString(keys::kVersion
, "1");
3953 // UNPACKED is for extensions loaded from a directory. We use it here, even
3954 // though we're testing loading from prefs, so that we don't need to provide
3955 // an extension key.
3956 extensions::ExtensionInfo
extension_info(
3957 &manifest
, std::string(), path
, Manifest::UNPACKED
);
3959 // Ensure we can load it with no management policy in place.
3960 GetManagementPolicy()->UnregisterAllProviders();
3961 EXPECT_EQ(0u, registry()->enabled_extensions().size());
3962 extensions::InstalledLoader(service()).Load(extension_info
, false);
3963 EXPECT_EQ(1u, registry()->enabled_extensions().size());
3965 const Extension
* extension
=
3966 (registry()->enabled_extensions().begin())->get();
3968 service()->UninstallExtension(extension
->id(),
3969 extensions::UNINSTALL_REASON_FOR_TESTING
,
3970 base::Bind(&base::DoNothing
),
3972 EXPECT_EQ(0u, registry()->enabled_extensions().size());
3974 // Ensure we cannot load it if management policy prohibits installation.
3975 extensions::TestManagementPolicyProvider
provider_(
3976 extensions::TestManagementPolicyProvider::PROHIBIT_LOAD
);
3977 GetManagementPolicy()->RegisterProvider(&provider_
);
3979 extensions::InstalledLoader(service()).Load(extension_info
, false);
3980 EXPECT_EQ(0u, registry()->enabled_extensions().size());
3983 // Tests disabling an extension when prohibited by the ManagementPolicy.
3984 TEST_F(ExtensionServiceTest
, ManagementPolicyProhibitsDisable
) {
3985 InitializeEmptyExtensionService();
3987 InstallCRX(data_dir().AppendASCII("good.crx"), INSTALL_NEW
);
3988 EXPECT_EQ(1u, registry()->enabled_extensions().size());
3989 EXPECT_EQ(0u, registry()->disabled_extensions().size());
3991 GetManagementPolicy()->UnregisterAllProviders();
3992 extensions::TestManagementPolicyProvider
provider(
3993 extensions::TestManagementPolicyProvider::PROHIBIT_MODIFY_STATUS
);
3994 GetManagementPolicy()->RegisterProvider(&provider
);
3996 // Attempt to disable it.
3997 service()->DisableExtension(good_crx
, Extension::DISABLE_USER_ACTION
);
3999 EXPECT_EQ(1u, registry()->enabled_extensions().size());
4000 EXPECT_TRUE(service()->GetExtensionById(good_crx
, false));
4001 EXPECT_EQ(0u, registry()->disabled_extensions().size());
4004 // Tests uninstalling an extension when prohibited by the ManagementPolicy.
4005 TEST_F(ExtensionServiceTest
, ManagementPolicyProhibitsUninstall
) {
4006 InitializeEmptyExtensionService();
4008 InstallCRX(data_dir().AppendASCII("good.crx"), INSTALL_NEW
);
4009 EXPECT_EQ(1u, registry()->enabled_extensions().size());
4010 EXPECT_EQ(0u, registry()->disabled_extensions().size());
4012 GetManagementPolicy()->UnregisterAllProviders();
4013 extensions::TestManagementPolicyProvider
provider(
4014 extensions::TestManagementPolicyProvider::PROHIBIT_MODIFY_STATUS
);
4015 GetManagementPolicy()->RegisterProvider(&provider
);
4017 // Attempt to uninstall it.
4019 service()->UninstallExtension(good_crx
,
4020 extensions::UNINSTALL_REASON_FOR_TESTING
,
4021 base::Bind(&base::DoNothing
),
4024 EXPECT_EQ(1u, registry()->enabled_extensions().size());
4025 EXPECT_TRUE(service()->GetExtensionById(good_crx
, false));
4028 // Tests that previously installed extensions that are now prohibited from
4029 // being installed are removed.
4030 TEST_F(ExtensionServiceTest
, ManagementPolicyUnloadsAllProhibited
) {
4031 InitializeEmptyExtensionService();
4033 InstallCRX(data_dir().AppendASCII("good.crx"), INSTALL_NEW
);
4034 InstallCRX(data_dir().AppendASCII("page_action.crx"), INSTALL_NEW
);
4035 EXPECT_EQ(2u, registry()->enabled_extensions().size());
4036 EXPECT_EQ(0u, registry()->disabled_extensions().size());
4038 GetManagementPolicy()->UnregisterAllProviders();
4039 extensions::TestManagementPolicyProvider
provider(
4040 extensions::TestManagementPolicyProvider::PROHIBIT_LOAD
);
4041 GetManagementPolicy()->RegisterProvider(&provider
);
4043 // Run the policy check.
4044 service()->CheckManagementPolicy();
4045 EXPECT_EQ(0u, registry()->enabled_extensions().size());
4046 EXPECT_EQ(0u, registry()->disabled_extensions().size());
4049 // Tests that previously disabled extensions that are now required to be
4050 // enabled are re-enabled on reinstall.
4051 TEST_F(ExtensionServiceTest
, ManagementPolicyRequiresEnable
) {
4052 InitializeEmptyExtensionService();
4054 // Install, then disable, an extension.
4055 InstallCRX(data_dir().AppendASCII("good.crx"), INSTALL_NEW
);
4056 EXPECT_EQ(1u, registry()->enabled_extensions().size());
4057 service()->DisableExtension(good_crx
, Extension::DISABLE_USER_ACTION
);
4058 EXPECT_EQ(1u, registry()->disabled_extensions().size());
4060 // Register an ExtensionManagementPolicy that requires the extension to remain
4062 GetManagementPolicy()->UnregisterAllProviders();
4063 extensions::TestManagementPolicyProvider
provider(
4064 extensions::TestManagementPolicyProvider::MUST_REMAIN_ENABLED
);
4065 GetManagementPolicy()->RegisterProvider(&provider
);
4067 // Reinstall the extension.
4068 InstallCRX(data_dir().AppendASCII("good.crx"), INSTALL_UPDATED
);
4069 EXPECT_EQ(1u, registry()->enabled_extensions().size());
4070 EXPECT_EQ(0u, registry()->disabled_extensions().size());
4073 // Tests that extensions disabled by management policy can be installed but
4074 // will get disabled after installing.
4075 TEST_F(ExtensionServiceTest
, ManagementPolicyProhibitsEnableOnInstalled
) {
4076 InitializeEmptyExtensionService();
4078 // Register an ExtensionManagementPolicy that disables all extensions, with
4079 // a specified Extension::DisableReason.
4080 GetManagementPolicy()->UnregisterAllProviders();
4081 extensions::TestManagementPolicyProvider
provider(
4082 extensions::TestManagementPolicyProvider::MUST_REMAIN_DISABLED
);
4083 provider
.SetDisableReason(Extension::DISABLE_NOT_VERIFIED
);
4084 GetManagementPolicy()->RegisterProvider(&provider
);
4086 // Attempts to install an extensions, it should be installed but disabled.
4087 EXPECT_EQ(0u, registry()->enabled_extensions().size());
4088 EXPECT_EQ(0u, registry()->disabled_extensions().size());
4089 InstallCRX(data_dir().AppendASCII("good.crx"), INSTALL_WITHOUT_LOAD
);
4090 EXPECT_EQ(0u, registry()->enabled_extensions().size());
4091 EXPECT_EQ(1u, registry()->disabled_extensions().size());
4093 // Verifies that the disable reason is set properly.
4094 EXPECT_EQ(Extension::DISABLE_NOT_VERIFIED
,
4095 service()->extension_prefs_
->GetDisableReasons(kGoodId
));
4098 // Tests that extensions with conflicting required permissions by enterprise
4099 // policy cannot be installed.
4100 TEST_F(ExtensionServiceTest
, PolicyBlockedPermissionNewExtensionInstall
) {
4101 InitializeEmptyExtensionServiceWithTestingPrefs();
4102 base::FilePath path
= data_dir().AppendASCII("permissions_blocklist");
4105 // Update policy to block one of the required permissions of target.
4106 ManagementPrefUpdater
pref(profile_
->GetTestingPrefService());
4107 pref
.AddBlockedPermission("*", "tabs");
4110 // The extension should be failed to install.
4111 PackAndInstallCRX(path
, INSTALL_FAILED
);
4114 // Update policy to block one of the optional permissions instead.
4115 ManagementPrefUpdater
pref(profile_
->GetTestingPrefService());
4116 pref
.ClearBlockedPermissions("*");
4117 pref
.AddBlockedPermission("*", "history");
4120 // The extension should succeed to install this time.
4121 std::string id
= PackAndInstallCRX(path
, INSTALL_NEW
)->id();
4123 // Uninstall the extension and update policy to block some arbitrary
4124 // unknown permission.
4125 UninstallExtension(id
, false);
4127 ManagementPrefUpdater
pref(profile_
->GetTestingPrefService());
4128 pref
.ClearBlockedPermissions("*");
4129 pref
.AddBlockedPermission("*", "unknown.permission.for.testing");
4132 // The extension should succeed to install as well.
4133 PackAndInstallCRX(path
, INSTALL_NEW
);
4136 // Tests that extension supposed to be force installed but with conflicting
4137 // required permissions cannot be installed.
4138 TEST_F(ExtensionServiceTest
, PolicyBlockedPermissionConflictsWithForceInstall
) {
4139 InitializeEmptyExtensionServiceWithTestingPrefs();
4141 // Pack the crx file.
4142 base::FilePath path
= data_dir().AppendASCII("permissions_blocklist");
4143 base::FilePath pem_path
= data_dir().AppendASCII("permissions_blocklist.pem");
4144 base::ScopedTempDir temp_dir
;
4145 EXPECT_TRUE(temp_dir
.CreateUniqueTempDir());
4146 base::FilePath crx_path
= temp_dir
.path().AppendASCII("temp.crx");
4148 PackCRX(path
, pem_path
, crx_path
);
4151 // Block one of the required permissions.
4152 ManagementPrefUpdater
pref(profile_
->GetTestingPrefService());
4153 pref
.AddBlockedPermission("*", "tabs");
4156 // Use MockExtensionProvider to simulate force installing extension.
4157 MockExtensionProvider
* provider
=
4158 new MockExtensionProvider(service(), Manifest::EXTERNAL_POLICY_DOWNLOAD
);
4159 AddMockExternalProvider(provider
);
4160 provider
->UpdateOrAddExtension(permissions_blocklist
, "1.0", crx_path
);
4163 // Attempts to force install this extension.
4164 content::WindowedNotificationObserver
observer(
4165 extensions::NOTIFICATION_CRX_INSTALLER_DONE
,
4166 content::NotificationService::AllSources());
4167 service()->CheckForExternalUpdates();
4171 // The extension should not be installed.
4172 ASSERT_FALSE(service()->GetInstalledExtension(permissions_blocklist
));
4174 // Remove this extension from pending extension manager as we would like to
4175 // give another attempt later.
4176 service()->pending_extension_manager()->Remove(permissions_blocklist
);
4179 // Clears the permission block list.
4180 ManagementPrefUpdater
pref(profile_
->GetTestingPrefService());
4181 pref
.ClearBlockedPermissions("*");
4185 // Attempts to force install this extension again.
4186 content::WindowedNotificationObserver
observer(
4187 extensions::NOTIFICATION_CRX_INSTALLER_DONE
,
4188 content::NotificationService::AllSources());
4189 service()->CheckForExternalUpdates();
4193 const Extension
* installed
=
4194 service()->GetInstalledExtension(permissions_blocklist
);
4195 ASSERT_TRUE(installed
);
4196 EXPECT_EQ(installed
->location(), Manifest::EXTERNAL_POLICY_DOWNLOAD
);
4199 // Tests that newer versions of an extension with conflicting required
4200 // permissions by enterprise policy cannot be updated to.
4201 TEST_F(ExtensionServiceTest
, PolicyBlockedPermissionExtensionUpdate
) {
4202 InitializeEmptyExtensionServiceWithTestingPrefs();
4204 base::FilePath path
= data_dir().AppendASCII("permissions_blocklist");
4205 base::FilePath path2
= data_dir().AppendASCII("permissions_blocklist2");
4206 base::FilePath pem_path
= data_dir().AppendASCII("permissions_blocklist.pem");
4208 // Install 'permissions_blocklist'.
4209 const Extension
* installed
= PackAndInstallCRX(path
, pem_path
, INSTALL_NEW
);
4210 EXPECT_EQ(installed
->id(), permissions_blocklist
);
4213 // Block one of the required permissions of 'permissions_blocklist2'.
4214 ManagementPrefUpdater
pref(profile_
->GetTestingPrefService());
4215 pref
.AddBlockedPermission("*", "downloads");
4218 // Install 'permissions_blocklist' again, should be updated.
4219 const Extension
* updated
= PackAndInstallCRX(path
, pem_path
, INSTALL_UPDATED
);
4220 EXPECT_EQ(updated
->id(), permissions_blocklist
);
4222 std::string old_version
= updated
->VersionString();
4224 // Attempts to update to 'permissions_blocklist2' should fail.
4225 PackAndInstallCRX(path2
, pem_path
, INSTALL_FAILED
);
4227 // Verify that the old version is still enabled.
4228 updated
= service()->GetExtensionById(permissions_blocklist
, false);
4229 ASSERT_TRUE(updated
);
4230 EXPECT_EQ(old_version
, updated
->VersionString());
4233 // Tests that policy update with additional permissions blocked revoke
4234 // conflicting granted optional permissions and unload extensions with
4235 // conflicting required permissions, including the force installed ones.
4236 TEST_F(ExtensionServiceTest
, PolicyBlockedPermissionPolicyUpdate
) {
4237 InitializeEmptyExtensionServiceWithTestingPrefs();
4239 base::FilePath path
= data_dir().AppendASCII("permissions_blocklist");
4240 base::FilePath path2
= data_dir().AppendASCII("permissions_blocklist2");
4241 base::FilePath pem_path
= data_dir().AppendASCII("permissions_blocklist.pem");
4243 // Pack the crx file.
4244 base::ScopedTempDir temp_dir
;
4245 EXPECT_TRUE(temp_dir
.CreateUniqueTempDir());
4246 base::FilePath crx_path
= temp_dir
.path().AppendASCII("temp.crx");
4248 PackCRX(path2
, pem_path
, crx_path
);
4250 // Install two arbitary extensions with specified manifest.
4251 std::string ext1
= PackAndInstallCRX(path
, INSTALL_NEW
)->id();
4252 std::string ext2
= PackAndInstallCRX(path2
, INSTALL_NEW
)->id();
4253 ASSERT_NE(ext1
, permissions_blocklist
);
4254 ASSERT_NE(ext2
, permissions_blocklist
);
4255 ASSERT_NE(ext1
, ext2
);
4257 // Force install another extension with known id and same manifest as 'ext2'.
4258 std::string ext2_forced
= permissions_blocklist
;
4259 MockExtensionProvider
* provider
=
4260 new MockExtensionProvider(service(), Manifest::EXTERNAL_POLICY_DOWNLOAD
);
4261 AddMockExternalProvider(provider
);
4262 provider
->UpdateOrAddExtension(ext2_forced
, "2.0", crx_path
);
4264 content::WindowedNotificationObserver
observer(
4265 extensions::NOTIFICATION_CRX_INSTALLER_DONE
,
4266 content::NotificationService::AllSources());
4267 service()->CheckForExternalUpdates();
4270 extensions::ExtensionRegistry
* registry
=
4271 extensions::ExtensionRegistry::Get(profile());
4273 // Verify all three extensions are installed and enabled.
4274 ASSERT_TRUE(registry
->enabled_extensions().GetByID(ext1
));
4275 ASSERT_TRUE(registry
->enabled_extensions().GetByID(ext2
));
4276 ASSERT_TRUE(registry
->enabled_extensions().GetByID(ext2_forced
));
4278 // Grant all optional permissions to each extension.
4279 GrantAllOptionalPermissions(ext1
);
4280 GrantAllOptionalPermissions(ext2
);
4281 GrantAllOptionalPermissions(ext2_forced
);
4283 scoped_refptr
<const PermissionSet
> active_permissions(
4284 ExtensionPrefs::Get(profile())->GetActivePermissions(ext1
));
4285 EXPECT_TRUE(active_permissions
->HasAPIPermission(
4286 extensions::APIPermission::kDownloads
));
4288 // Set policy to block 'downloads' permission.
4290 ManagementPrefUpdater
pref(profile_
->GetTestingPrefService());
4291 pref
.AddBlockedPermission("*", "downloads");
4294 base::RunLoop().RunUntilIdle();
4296 // 'ext1' should still be enabled, but with 'downloads' permission revoked.
4297 EXPECT_TRUE(registry
->enabled_extensions().GetByID(ext1
));
4298 active_permissions
=
4299 ExtensionPrefs::Get(profile())->GetActivePermissions(ext1
);
4300 EXPECT_FALSE(active_permissions
->HasAPIPermission(
4301 extensions::APIPermission::kDownloads
));
4303 // 'ext2' should be disabled because one of its required permissions is
4305 EXPECT_FALSE(registry
->enabled_extensions().GetByID(ext2
));
4307 // 'ext2_forced' should be handled the same as 'ext2'
4308 EXPECT_FALSE(registry
->enabled_extensions().GetByID(ext2_forced
));
4311 // Flaky on windows; http://crbug.com/309833
4313 #define MAYBE_ExternalExtensionAutoAcknowledgement DISABLED_ExternalExtensionAutoAcknowledgement
4315 #define MAYBE_ExternalExtensionAutoAcknowledgement ExternalExtensionAutoAcknowledgement
4317 TEST_F(ExtensionServiceTest
, MAYBE_ExternalExtensionAutoAcknowledgement
) {
4318 InitializeEmptyExtensionService();
4319 service()->set_extensions_enabled(true);
4322 // Register and install an external extension.
4323 MockExtensionProvider
* provider
=
4324 new MockExtensionProvider(service(), Manifest::EXTERNAL_PREF
);
4325 AddMockExternalProvider(provider
);
4326 provider
->UpdateOrAddExtension(
4327 good_crx
, "1.0.0.0", data_dir().AppendASCII("good.crx"));
4330 // Have policy force-install an extension.
4331 MockExtensionProvider
* provider
= new MockExtensionProvider(
4332 service(), Manifest::EXTERNAL_POLICY_DOWNLOAD
);
4333 AddMockExternalProvider(provider
);
4334 provider
->UpdateOrAddExtension(
4335 page_action
, "1.0.0.0", data_dir().AppendASCII("page_action.crx"));
4338 // Providers are set up. Let them run.
4340 content::WindowedNotificationObserver
observer(
4341 extensions::NOTIFICATION_CRX_INSTALLER_DONE
,
4342 base::Bind(&WaitForCountNotificationsCallback
, &count
));
4343 service()->CheckForExternalUpdates();
4347 ASSERT_EQ(2u, registry()->enabled_extensions().size());
4348 EXPECT_TRUE(service()->GetExtensionById(good_crx
, false));
4349 EXPECT_TRUE(service()->GetExtensionById(page_action
, false));
4350 ExtensionPrefs
* prefs
= ExtensionPrefs::Get(profile());
4351 ASSERT_TRUE(!prefs
->IsExternalExtensionAcknowledged(good_crx
));
4352 ASSERT_TRUE(prefs
->IsExternalExtensionAcknowledged(page_action
));
4355 #if !defined(OS_CHROMEOS)
4356 // This tests if default apps are installed correctly.
4357 TEST_F(ExtensionServiceTest
, DefaultAppsInstall
) {
4358 InitializeEmptyExtensionService();
4359 service()->set_extensions_enabled(true);
4362 std::string json_data
=
4364 " \"ldnnhddmnhbkjipkidpdiheffobcpfmf\" : {"
4365 " \"external_crx\": \"good.crx\","
4366 " \"external_version\": \"1.0.0.0\","
4367 " \"is_bookmark_app\": false"
4370 default_apps::Provider
* provider
= new default_apps::Provider(
4373 new extensions::ExternalTestingLoader(json_data
, data_dir()),
4375 Manifest::INVALID_LOCATION
,
4376 Extension::FROM_WEBSTORE
| Extension::WAS_INSTALLED_BY_DEFAULT
);
4378 AddMockExternalProvider(provider
);
4381 ASSERT_EQ(0u, registry()->enabled_extensions().size());
4382 content::WindowedNotificationObserver
observer(
4383 extensions::NOTIFICATION_CRX_INSTALLER_DONE
,
4384 content::NotificationService::AllSources());
4385 service()->CheckForExternalUpdates();
4388 ASSERT_EQ(1u, registry()->enabled_extensions().size());
4389 EXPECT_TRUE(service()->GetExtensionById(good_crx
, false));
4390 const Extension
* extension
= service()->GetExtensionById(good_crx
, false);
4391 EXPECT_TRUE(extension
->from_webstore());
4392 EXPECT_TRUE(extension
->was_installed_by_default());
4396 // Tests disabling extensions
4397 TEST_F(ExtensionServiceTest
, DisableExtension
) {
4398 InitializeEmptyExtensionService();
4400 InstallCRX(data_dir().AppendASCII("good.crx"), INSTALL_NEW
);
4401 EXPECT_TRUE(service()->GetExtensionById(good_crx
, true));
4402 EXPECT_TRUE(service()->GetExtensionById(good_crx
, false));
4404 EXPECT_EQ(1u, registry()->enabled_extensions().size());
4405 EXPECT_EQ(0u, registry()->disabled_extensions().size());
4406 EXPECT_EQ(0u, registry()->terminated_extensions().size());
4407 EXPECT_EQ(0u, registry()->blacklisted_extensions().size());
4410 service()->DisableExtension(good_crx
, Extension::DISABLE_USER_ACTION
);
4412 EXPECT_TRUE(service()->GetExtensionById(good_crx
, true));
4413 EXPECT_FALSE(service()->GetExtensionById(good_crx
, false));
4414 EXPECT_EQ(0u, registry()->enabled_extensions().size());
4415 EXPECT_EQ(1u, registry()->disabled_extensions().size());
4416 EXPECT_EQ(0u, registry()->terminated_extensions().size());
4417 EXPECT_EQ(0u, registry()->blacklisted_extensions().size());
4420 TEST_F(ExtensionServiceTest
, TerminateExtension
) {
4421 InitializeEmptyExtensionService();
4423 InstallCRX(data_dir().AppendASCII("good.crx"), INSTALL_NEW
);
4424 EXPECT_EQ(1u, registry()->enabled_extensions().size());
4425 EXPECT_EQ(0u, registry()->disabled_extensions().size());
4426 EXPECT_EQ(0u, registry()->terminated_extensions().size());
4427 EXPECT_EQ(0u, registry()->blacklisted_extensions().size());
4429 TerminateExtension(good_crx
);
4431 EXPECT_EQ(0u, registry()->enabled_extensions().size());
4432 EXPECT_EQ(0u, registry()->disabled_extensions().size());
4433 EXPECT_EQ(1u, registry()->terminated_extensions().size());
4434 EXPECT_EQ(0u, registry()->blacklisted_extensions().size());
4437 TEST_F(ExtensionServiceTest
, DisableTerminatedExtension
) {
4438 InitializeEmptyExtensionService();
4440 InstallCRX(data_dir().AppendASCII("good.crx"), INSTALL_NEW
);
4441 TerminateExtension(good_crx
);
4442 EXPECT_TRUE(registry()->GetExtensionById(
4443 good_crx
, extensions::ExtensionRegistry::TERMINATED
));
4446 service()->DisableExtension(good_crx
, Extension::DISABLE_USER_ACTION
);
4448 EXPECT_FALSE(registry()->GetExtensionById(
4449 good_crx
, extensions::ExtensionRegistry::TERMINATED
));
4450 EXPECT_TRUE(service()->GetExtensionById(good_crx
, true));
4452 EXPECT_EQ(0u, registry()->enabled_extensions().size());
4453 EXPECT_EQ(1u, registry()->disabled_extensions().size());
4454 EXPECT_EQ(0u, registry()->terminated_extensions().size());
4455 EXPECT_EQ(0u, registry()->blacklisted_extensions().size());
4458 // Tests disabling all extensions (simulating --disable-extensions flag).
4459 TEST_F(ExtensionServiceTest
, DisableAllExtensions
) {
4460 InitializeEmptyExtensionService();
4462 base::FilePath path
= data_dir().AppendASCII("good.crx");
4463 InstallCRX(path
, INSTALL_NEW
);
4465 EXPECT_EQ(1u, registry()->enabled_extensions().size());
4466 EXPECT_EQ(0u, registry()->disabled_extensions().size());
4468 // Disable extensions.
4469 service()->set_extensions_enabled(false);
4470 service()->ReloadExtensionsForTest();
4472 // There shouldn't be extensions in either list.
4473 EXPECT_EQ(0u, registry()->enabled_extensions().size());
4474 EXPECT_EQ(0u, registry()->disabled_extensions().size());
4476 // This shouldn't do anything when all extensions are disabled.
4477 service()->EnableExtension(good_crx
);
4478 service()->ReloadExtensionsForTest();
4480 // There still shouldn't be extensions in either list.
4481 EXPECT_EQ(0u, registry()->enabled_extensions().size());
4482 EXPECT_EQ(0u, registry()->disabled_extensions().size());
4484 // And then re-enable the extensions.
4485 service()->set_extensions_enabled(true);
4486 service()->ReloadExtensionsForTest();
4488 EXPECT_EQ(1u, registry()->enabled_extensions().size());
4489 EXPECT_EQ(0u, registry()->disabled_extensions().size());
4492 // Tests reloading extensions.
4493 TEST_F(ExtensionServiceTest
, ReloadExtensions
) {
4494 InitializeEmptyExtensionService();
4496 // Simple extension that should install without error.
4497 base::FilePath path
= data_dir().AppendASCII("good.crx");
4498 InstallCRX(path
, INSTALL_NEW
,
4499 Extension::FROM_WEBSTORE
| Extension::WAS_INSTALLED_BY_DEFAULT
);
4500 const char* const extension_id
= good_crx
;
4501 service()->DisableExtension(extension_id
, Extension::DISABLE_USER_ACTION
);
4503 EXPECT_EQ(0u, registry()->enabled_extensions().size());
4504 EXPECT_EQ(1u, registry()->disabled_extensions().size());
4506 service()->ReloadExtensionsForTest();
4508 // The creation flags should not change when reloading the extension.
4509 const Extension
* extension
= service()->GetExtensionById(good_crx
, true);
4510 EXPECT_TRUE(extension
->from_webstore());
4511 EXPECT_TRUE(extension
->was_installed_by_default());
4512 EXPECT_FALSE(extension
->from_bookmark());
4514 // Extension counts shouldn't change.
4515 EXPECT_EQ(0u, registry()->enabled_extensions().size());
4516 EXPECT_EQ(1u, registry()->disabled_extensions().size());
4518 service()->EnableExtension(extension_id
);
4520 EXPECT_EQ(1u, registry()->enabled_extensions().size());
4521 EXPECT_EQ(0u, registry()->disabled_extensions().size());
4523 // Need to clear |loaded_| manually before reloading as the
4524 // EnableExtension() call above inserted into it and
4525 // UnloadAllExtensions() doesn't send out notifications.
4527 service()->ReloadExtensionsForTest();
4529 // Extension counts shouldn't change.
4530 EXPECT_EQ(1u, registry()->enabled_extensions().size());
4531 EXPECT_EQ(0u, registry()->disabled_extensions().size());
4534 // Tests reloading an extension.
4535 TEST_F(ExtensionServiceTest
, ReloadExtension
) {
4536 InitializeEmptyExtensionService();
4538 // Simple extension that should install without error.
4539 const char extension_id
[] = "behllobkkfkfnphdnhnkndlbkcpglgmj";
4540 base::FilePath ext
= data_dir()
4541 .AppendASCII("good")
4542 .AppendASCII("Extensions")
4543 .AppendASCII(extension_id
)
4544 .AppendASCII("1.0.0.0");
4545 extensions::UnpackedInstaller::Create(service())->Load(ext
);
4546 base::RunLoop().RunUntilIdle();
4548 EXPECT_EQ(1u, registry()->enabled_extensions().size());
4549 EXPECT_EQ(0u, registry()->disabled_extensions().size());
4551 service()->ReloadExtension(extension_id
);
4553 // Extension should be disabled now, waiting to be reloaded.
4554 EXPECT_EQ(0u, registry()->enabled_extensions().size());
4555 EXPECT_EQ(1u, registry()->disabled_extensions().size());
4556 EXPECT_EQ(Extension::DISABLE_RELOAD
,
4557 ExtensionPrefs::Get(profile())->GetDisableReasons(extension_id
));
4559 // Reloading again should not crash.
4560 service()->ReloadExtension(extension_id
);
4563 base::RunLoop().RunUntilIdle();
4565 // Extension should be enabled again.
4566 EXPECT_EQ(1u, registry()->enabled_extensions().size());
4567 EXPECT_EQ(0u, registry()->disabled_extensions().size());
4570 TEST_F(ExtensionServiceTest
, UninstallExtension
) {
4571 InitializeEmptyExtensionService();
4572 InstallCRX(data_dir().AppendASCII("good.crx"), INSTALL_NEW
);
4573 EXPECT_EQ(1u, registry()->enabled_extensions().size());
4574 UninstallExtension(good_crx
, false);
4575 EXPECT_EQ(0u, registry()->enabled_extensions().size());
4576 EXPECT_EQ(UnloadedExtensionInfo::REASON_UNINSTALL
, unloaded_reason_
);
4579 TEST_F(ExtensionServiceTest
, UninstallTerminatedExtension
) {
4580 InitializeEmptyExtensionService();
4581 InstallCRX(data_dir().AppendASCII("good.crx"), INSTALL_NEW
);
4582 TerminateExtension(good_crx
);
4583 UninstallExtension(good_crx
, false);
4584 EXPECT_EQ(UnloadedExtensionInfo::REASON_TERMINATE
, unloaded_reason_
);
4587 // Tests the uninstaller helper.
4588 TEST_F(ExtensionServiceTest
, UninstallExtensionHelper
) {
4589 InitializeEmptyExtensionService();
4590 InstallCRX(data_dir().AppendASCII("good.crx"), INSTALL_NEW
);
4591 UninstallExtension(good_crx
, true);
4592 EXPECT_EQ(UnloadedExtensionInfo::REASON_UNINSTALL
, unloaded_reason_
);
4595 TEST_F(ExtensionServiceTest
, UninstallExtensionHelperTerminated
) {
4596 InitializeEmptyExtensionService();
4597 InstallCRX(data_dir().AppendASCII("good.crx"), INSTALL_NEW
);
4598 TerminateExtension(good_crx
);
4599 UninstallExtension(good_crx
, true);
4600 EXPECT_EQ(UnloadedExtensionInfo::REASON_TERMINATE
, unloaded_reason_
);
4603 // An extension disabled because of unsupported requirements should re-enabled
4604 // if updated to a version with supported requirements as long as there are no
4605 // other disable reasons.
4606 TEST_F(ExtensionServiceTest
, UpgradingRequirementsEnabled
) {
4607 InitializeEmptyExtensionService();
4610 base::FilePath path
= data_dir().AppendASCII("requirements");
4611 base::FilePath pem_path
=
4612 data_dir().AppendASCII("requirements").AppendASCII("v1_good.pem");
4613 const Extension
* extension_v1
= PackAndInstallCRX(path
.AppendASCII("v1_good"),
4616 std::string id
= extension_v1
->id();
4617 EXPECT_TRUE(service()->IsExtensionEnabled(id
));
4619 base::FilePath v2_bad_requirements_crx
= GetTemporaryFile();
4621 PackCRX(path
.AppendASCII("v2_bad_requirements"),
4623 v2_bad_requirements_crx
);
4624 UpdateExtension(id
, v2_bad_requirements_crx
, INSTALLED
);
4625 EXPECT_FALSE(service()->IsExtensionEnabled(id
));
4627 base::FilePath v3_good_crx
= GetTemporaryFile();
4629 PackCRX(path
.AppendASCII("v3_good"), pem_path
, v3_good_crx
);
4630 UpdateExtension(id
, v3_good_crx
, ENABLED
);
4631 EXPECT_TRUE(service()->IsExtensionEnabled(id
));
4634 // Extensions disabled through user action should stay disabled.
4635 TEST_F(ExtensionServiceTest
, UpgradingRequirementsDisabled
) {
4636 InitializeEmptyExtensionService();
4639 base::FilePath path
= data_dir().AppendASCII("requirements");
4640 base::FilePath pem_path
=
4641 data_dir().AppendASCII("requirements").AppendASCII("v1_good.pem");
4642 const Extension
* extension_v1
= PackAndInstallCRX(path
.AppendASCII("v1_good"),
4645 std::string id
= extension_v1
->id();
4646 service()->DisableExtension(id
, Extension::DISABLE_USER_ACTION
);
4647 EXPECT_FALSE(service()->IsExtensionEnabled(id
));
4649 base::FilePath v2_bad_requirements_crx
= GetTemporaryFile();
4651 PackCRX(path
.AppendASCII("v2_bad_requirements"),
4653 v2_bad_requirements_crx
);
4654 UpdateExtension(id
, v2_bad_requirements_crx
, INSTALLED
);
4655 EXPECT_FALSE(service()->IsExtensionEnabled(id
));
4657 base::FilePath v3_good_crx
= GetTemporaryFile();
4659 PackCRX(path
.AppendASCII("v3_good"), pem_path
, v3_good_crx
);
4660 UpdateExtension(id
, v3_good_crx
, INSTALLED
);
4661 EXPECT_FALSE(service()->IsExtensionEnabled(id
));
4664 // The extension should not re-enabled because it was disabled from a
4665 // permission increase.
4666 TEST_F(ExtensionServiceTest
, UpgradingRequirementsPermissions
) {
4667 InitializeEmptyExtensionService();
4670 base::FilePath path
= data_dir().AppendASCII("requirements");
4671 base::FilePath pem_path
=
4672 data_dir().AppendASCII("requirements").AppendASCII("v1_good.pem");
4673 const Extension
* extension_v1
= PackAndInstallCRX(path
.AppendASCII("v1_good"),
4676 std::string id
= extension_v1
->id();
4677 EXPECT_TRUE(service()->IsExtensionEnabled(id
));
4679 base::FilePath v2_bad_requirements_and_permissions_crx
= GetTemporaryFile();
4681 PackCRX(path
.AppendASCII("v2_bad_requirements_and_permissions"),
4683 v2_bad_requirements_and_permissions_crx
);
4684 UpdateExtension(id
, v2_bad_requirements_and_permissions_crx
, INSTALLED
);
4685 EXPECT_FALSE(service()->IsExtensionEnabled(id
));
4687 base::FilePath v3_bad_permissions_crx
= GetTemporaryFile();
4689 PackCRX(path
.AppendASCII("v3_bad_permissions"),
4691 v3_bad_permissions_crx
);
4692 UpdateExtension(id
, v3_bad_permissions_crx
, INSTALLED
);
4693 EXPECT_FALSE(service()->IsExtensionEnabled(id
));
4696 // Unpacked extensions are not allowed to be installed if they have unsupported
4698 TEST_F(ExtensionServiceTest
, UnpackedRequirements
) {
4699 InitializeEmptyExtensionService();
4702 base::FilePath path
=
4703 data_dir().AppendASCII("requirements").AppendASCII("v2_bad_requirements");
4704 extensions::UnpackedInstaller::Create(service())->Load(path
);
4705 base::RunLoop().RunUntilIdle();
4706 EXPECT_EQ(1u, GetErrors().size());
4707 EXPECT_EQ(0u, registry()->enabled_extensions().size());
4710 class ExtensionCookieCallback
{
4712 ExtensionCookieCallback()
4714 weak_factory_(base::MessageLoop::current()) {}
4716 void SetCookieCallback(bool result
) {
4717 base::MessageLoop::current()->PostTask(FROM_HERE
,
4718 base::Bind(&base::MessageLoop::Quit
, weak_factory_
.GetWeakPtr()));
4722 void GetAllCookiesCallback(const net::CookieList
& list
) {
4723 base::MessageLoop::current()->PostTask(FROM_HERE
,
4724 base::Bind(&base::MessageLoop::Quit
, weak_factory_
.GetWeakPtr()));
4727 net::CookieList list_
;
4729 base::WeakPtrFactory
<base::MessageLoop
> weak_factory_
;
4732 // Verifies extension state is removed upon uninstall.
4733 TEST_F(ExtensionServiceTest
, ClearExtensionData
) {
4734 InitializeEmptyExtensionService();
4735 ExtensionCookieCallback callback
;
4737 // Load a test extension.
4738 base::FilePath path
= data_dir();
4739 path
= path
.AppendASCII("good.crx");
4740 const Extension
* extension
= InstallCRX(path
, INSTALL_NEW
);
4741 ASSERT_TRUE(extension
);
4742 GURL
ext_url(extension
->url());
4743 std::string origin_id
= storage::GetIdentifierFromOrigin(ext_url
);
4745 // Set a cookie for the extension.
4746 net::CookieMonster
* cookie_monster
= profile()
4747 ->GetRequestContextForExtensions()
4748 ->GetURLRequestContext()
4750 ->GetCookieMonster();
4751 ASSERT_TRUE(cookie_monster
);
4752 net::CookieOptions options
;
4753 cookie_monster
->SetCookieWithOptionsAsync(
4754 ext_url
, "dummy=value", options
,
4755 base::Bind(&ExtensionCookieCallback::SetCookieCallback
,
4756 base::Unretained(&callback
)));
4757 base::RunLoop().RunUntilIdle();
4758 EXPECT_TRUE(callback
.result_
);
4760 cookie_monster
->GetAllCookiesForURLAsync(
4762 base::Bind(&ExtensionCookieCallback::GetAllCookiesCallback
,
4763 base::Unretained(&callback
)));
4764 base::RunLoop().RunUntilIdle();
4765 EXPECT_EQ(1U, callback
.list_
.size());
4768 storage::DatabaseTracker
* db_tracker
=
4769 BrowserContext::GetDefaultStoragePartition(profile())
4770 ->GetDatabaseTracker();
4771 base::string16 db_name
= base::UTF8ToUTF16("db");
4772 base::string16 description
= base::UTF8ToUTF16("db_description");
4774 db_tracker
->DatabaseOpened(origin_id
, db_name
, description
, 1, &size
);
4775 db_tracker
->DatabaseClosed(origin_id
, db_name
);
4776 std::vector
<storage::OriginInfo
> origins
;
4777 db_tracker
->GetAllOriginsInfo(&origins
);
4778 EXPECT_EQ(1U, origins
.size());
4779 EXPECT_EQ(origin_id
, origins
[0].GetOriginIdentifier());
4781 // Create local storage. We only simulate this by creating the backing files.
4782 // Note: This test depends on details of how the dom_storage library
4783 // stores data in the host file system.
4784 base::FilePath lso_dir_path
=
4785 profile()->GetPath().AppendASCII("Local Storage");
4786 base::FilePath lso_file_path
= lso_dir_path
.AppendASCII(origin_id
)
4787 .AddExtension(FILE_PATH_LITERAL(".localstorage"));
4788 EXPECT_TRUE(base::CreateDirectory(lso_dir_path
));
4789 EXPECT_EQ(0, base::WriteFile(lso_file_path
, NULL
, 0));
4790 EXPECT_TRUE(base::PathExists(lso_file_path
));
4792 // Create indexed db. Similarly, it is enough to only simulate this by
4793 // creating the directory on the disk.
4794 IndexedDBContext
* idb_context
= BrowserContext::GetDefaultStoragePartition(
4795 profile())->GetIndexedDBContext();
4796 idb_context
->SetTaskRunnerForTesting(
4797 base::MessageLoop::current()->message_loop_proxy().get());
4798 base::FilePath idb_path
= idb_context
->GetFilePathForTesting(origin_id
);
4799 EXPECT_TRUE(base::CreateDirectory(idb_path
));
4800 EXPECT_TRUE(base::DirectoryExists(idb_path
));
4802 // Uninstall the extension.
4803 base::RunLoop run_loop
;
4805 service()->UninstallExtension(good_crx
,
4806 extensions::UNINSTALL_REASON_FOR_TESTING
,
4807 run_loop
.QuitClosure(),
4809 // The data deletion happens on the IO thread.
4812 // Check that the cookie is gone.
4813 cookie_monster
->GetAllCookiesForURLAsync(
4815 base::Bind(&ExtensionCookieCallback::GetAllCookiesCallback
,
4816 base::Unretained(&callback
)));
4817 base::RunLoop().RunUntilIdle();
4818 EXPECT_EQ(0U, callback
.list_
.size());
4820 // The database should have vanished as well.
4822 db_tracker
->GetAllOriginsInfo(&origins
);
4823 EXPECT_EQ(0U, origins
.size());
4825 // Check that the LSO file has been removed.
4826 EXPECT_FALSE(base::PathExists(lso_file_path
));
4828 // Check if the indexed db has disappeared too.
4829 EXPECT_FALSE(base::DirectoryExists(idb_path
));
4832 // Verifies app state is removed upon uninstall.
4833 TEST_F(ExtensionServiceTest
, ClearAppData
) {
4834 InitializeEmptyExtensionService();
4835 ExtensionCookieCallback callback
;
4839 // Install app1 with unlimited storage.
4840 const Extension
* extension
=
4841 PackAndInstallCRX(data_dir().AppendASCII("app1"), INSTALL_NEW
);
4842 ValidatePrefKeyCount(++pref_count
);
4843 ASSERT_EQ(1u, registry()->enabled_extensions().size());
4844 const std::string id1
= extension
->id();
4845 EXPECT_TRUE(extension
->permissions_data()->HasAPIPermission(
4846 APIPermission::kUnlimitedStorage
));
4848 extensions::AppLaunchInfo::GetFullLaunchURL(extension
).GetOrigin());
4849 EXPECT_TRUE(profile()->GetExtensionSpecialStoragePolicy()->IsStorageUnlimited(
4851 std::string origin_id
= storage::GetIdentifierFromOrigin(origin1
);
4853 // Install app2 from the same origin with unlimited storage.
4854 extension
= PackAndInstallCRX(data_dir().AppendASCII("app2"), INSTALL_NEW
);
4855 ValidatePrefKeyCount(++pref_count
);
4856 ASSERT_EQ(2u, registry()->enabled_extensions().size());
4857 const std::string id2
= extension
->id();
4858 EXPECT_TRUE(extension
->permissions_data()->HasAPIPermission(
4859 APIPermission::kUnlimitedStorage
));
4860 EXPECT_TRUE(extension
->web_extent().MatchesURL(
4861 extensions::AppLaunchInfo::GetFullLaunchURL(extension
)));
4863 extensions::AppLaunchInfo::GetFullLaunchURL(extension
).GetOrigin());
4864 EXPECT_EQ(origin1
, origin2
);
4865 EXPECT_TRUE(profile()->GetExtensionSpecialStoragePolicy()->IsStorageUnlimited(
4868 // Set a cookie for the extension.
4869 net::CookieMonster
* cookie_monster
= profile()
4870 ->GetRequestContext()
4871 ->GetURLRequestContext()
4873 ->GetCookieMonster();
4874 ASSERT_TRUE(cookie_monster
);
4875 net::CookieOptions options
;
4876 cookie_monster
->SetCookieWithOptionsAsync(
4877 origin1
, "dummy=value", options
,
4878 base::Bind(&ExtensionCookieCallback::SetCookieCallback
,
4879 base::Unretained(&callback
)));
4880 base::RunLoop().RunUntilIdle();
4881 EXPECT_TRUE(callback
.result_
);
4883 cookie_monster
->GetAllCookiesForURLAsync(
4885 base::Bind(&ExtensionCookieCallback::GetAllCookiesCallback
,
4886 base::Unretained(&callback
)));
4887 base::RunLoop().RunUntilIdle();
4888 EXPECT_EQ(1U, callback
.list_
.size());
4891 storage::DatabaseTracker
* db_tracker
=
4892 BrowserContext::GetDefaultStoragePartition(profile())
4893 ->GetDatabaseTracker();
4894 base::string16 db_name
= base::UTF8ToUTF16("db");
4895 base::string16 description
= base::UTF8ToUTF16("db_description");
4897 db_tracker
->DatabaseOpened(origin_id
, db_name
, description
, 1, &size
);
4898 db_tracker
->DatabaseClosed(origin_id
, db_name
);
4899 std::vector
<storage::OriginInfo
> origins
;
4900 db_tracker
->GetAllOriginsInfo(&origins
);
4901 EXPECT_EQ(1U, origins
.size());
4902 EXPECT_EQ(origin_id
, origins
[0].GetOriginIdentifier());
4904 // Create local storage. We only simulate this by creating the backing files.
4905 // Note: This test depends on details of how the dom_storage library
4906 // stores data in the host file system.
4907 base::FilePath lso_dir_path
=
4908 profile()->GetPath().AppendASCII("Local Storage");
4909 base::FilePath lso_file_path
= lso_dir_path
.AppendASCII(origin_id
)
4910 .AddExtension(FILE_PATH_LITERAL(".localstorage"));
4911 EXPECT_TRUE(base::CreateDirectory(lso_dir_path
));
4912 EXPECT_EQ(0, base::WriteFile(lso_file_path
, NULL
, 0));
4913 EXPECT_TRUE(base::PathExists(lso_file_path
));
4915 // Create indexed db. Similarly, it is enough to only simulate this by
4916 // creating the directory on the disk.
4917 IndexedDBContext
* idb_context
= BrowserContext::GetDefaultStoragePartition(
4918 profile())->GetIndexedDBContext();
4919 idb_context
->SetTaskRunnerForTesting(
4920 base::MessageLoop::current()->message_loop_proxy().get());
4921 base::FilePath idb_path
= idb_context
->GetFilePathForTesting(origin_id
);
4922 EXPECT_TRUE(base::CreateDirectory(idb_path
));
4923 EXPECT_TRUE(base::DirectoryExists(idb_path
));
4925 // Uninstall one of them, unlimited storage should still be granted
4927 UninstallExtension(id1
, false);
4928 EXPECT_EQ(1u, registry()->enabled_extensions().size());
4929 EXPECT_TRUE(profile()->GetExtensionSpecialStoragePolicy()->IsStorageUnlimited(
4932 // Check that the cookie is still there.
4933 cookie_monster
->GetAllCookiesForURLAsync(
4935 base::Bind(&ExtensionCookieCallback::GetAllCookiesCallback
,
4936 base::Unretained(&callback
)));
4937 base::RunLoop().RunUntilIdle();
4938 EXPECT_EQ(1U, callback
.list_
.size());
4940 // Now uninstall the other. Storage should be cleared for the apps.
4941 UninstallExtension(id2
, false);
4942 EXPECT_EQ(0u, registry()->enabled_extensions().size());
4944 profile()->GetExtensionSpecialStoragePolicy()->IsStorageUnlimited(
4947 // Check that the cookie is gone.
4948 cookie_monster
->GetAllCookiesForURLAsync(
4950 base::Bind(&ExtensionCookieCallback::GetAllCookiesCallback
,
4951 base::Unretained(&callback
)));
4952 base::RunLoop().RunUntilIdle();
4953 EXPECT_EQ(0U, callback
.list_
.size());
4955 // The database should have vanished as well.
4957 db_tracker
->GetAllOriginsInfo(&origins
);
4958 EXPECT_EQ(0U, origins
.size());
4960 // Check that the LSO file has been removed.
4961 EXPECT_FALSE(base::PathExists(lso_file_path
));
4963 // Check if the indexed db has disappeared too.
4964 EXPECT_FALSE(base::DirectoryExists(idb_path
));
4967 // Tests loading single extensions (like --load-extension)
4968 // Flaky crashes. http://crbug.com/231806
4969 TEST_F(ExtensionServiceTest
, DISABLED_LoadExtension
) {
4970 InitializeEmptyExtensionService();
4972 base::FilePath ext1
= data_dir()
4973 .AppendASCII("good")
4974 .AppendASCII("Extensions")
4975 .AppendASCII("behllobkkfkfnphdnhnkndlbkcpglgmj")
4976 .AppendASCII("1.0.0.0");
4977 extensions::UnpackedInstaller::Create(service())->Load(ext1
);
4978 base::RunLoop().RunUntilIdle();
4979 EXPECT_EQ(0u, GetErrors().size());
4980 ASSERT_EQ(1u, loaded_
.size());
4981 EXPECT_EQ(Manifest::UNPACKED
, loaded_
[0]->location());
4982 EXPECT_EQ(1u, registry()->enabled_extensions().size());
4984 ValidatePrefKeyCount(1);
4986 base::FilePath no_manifest
=
4989 // .AppendASCII("Extensions")
4990 .AppendASCII("cccccccccccccccccccccccccccccccc")
4992 extensions::UnpackedInstaller::Create(service())->Load(no_manifest
);
4993 base::RunLoop().RunUntilIdle();
4994 EXPECT_EQ(1u, GetErrors().size());
4995 ASSERT_EQ(1u, loaded_
.size());
4996 EXPECT_EQ(1u, registry()->enabled_extensions().size());
4999 std::string id
= loaded_
[0]->id();
5000 EXPECT_FALSE(unloaded_id_
.length());
5001 service()->UninstallExtension(id
,
5002 extensions::UNINSTALL_REASON_FOR_TESTING
,
5003 base::Bind(&base::DoNothing
),
5005 base::RunLoop().RunUntilIdle();
5006 EXPECT_EQ(id
, unloaded_id_
);
5007 ASSERT_EQ(0u, loaded_
.size());
5008 EXPECT_EQ(0u, registry()->enabled_extensions().size());
5011 // Tests that we generate IDs when they are not specified in the manifest for
5012 // --load-extension.
5013 TEST_F(ExtensionServiceTest
, GenerateID
) {
5014 InitializeEmptyExtensionService();
5016 base::FilePath no_id_ext
= data_dir().AppendASCII("no_id");
5017 extensions::UnpackedInstaller::Create(service())->Load(no_id_ext
);
5018 base::RunLoop().RunUntilIdle();
5019 EXPECT_EQ(0u, GetErrors().size());
5020 ASSERT_EQ(1u, loaded_
.size());
5021 ASSERT_TRUE(crx_file::id_util::IdIsValid(loaded_
[0]->id()));
5022 EXPECT_EQ(loaded_
[0]->location(), Manifest::UNPACKED
);
5024 ValidatePrefKeyCount(1);
5026 std::string previous_id
= loaded_
[0]->id();
5028 // If we reload the same path, we should get the same extension ID.
5029 extensions::UnpackedInstaller::Create(service())->Load(no_id_ext
);
5030 base::RunLoop().RunUntilIdle();
5031 ASSERT_EQ(1u, loaded_
.size());
5032 ASSERT_EQ(previous_id
, loaded_
[0]->id());
5035 TEST_F(ExtensionServiceTest
, UnpackedValidatesLocales
) {
5036 InitializeEmptyExtensionService();
5038 base::FilePath bad_locale
=
5039 data_dir().AppendASCII("unpacked").AppendASCII("bad_messages_file");
5040 extensions::UnpackedInstaller::Create(service())->Load(bad_locale
);
5041 base::RunLoop().RunUntilIdle();
5042 EXPECT_EQ(1u, GetErrors().size());
5043 base::FilePath ms_messages_file
= bad_locale
.AppendASCII("_locales")
5045 .AppendASCII("messages.json");
5046 EXPECT_THAT(base::UTF16ToUTF8(GetErrors()[0]), testing::AllOf(
5048 base::UTF16ToUTF8(ms_messages_file
.LossyDisplayName())),
5049 testing::HasSubstr("Dictionary keys must be quoted.")));
5050 ASSERT_EQ(0u, loaded_
.size());
5053 void ExtensionServiceTest::TestExternalProvider(
5054 MockExtensionProvider
* provider
, Manifest::Location location
) {
5055 // Verify that starting with no providers loads no extensions.
5057 ASSERT_EQ(0u, loaded_
.size());
5059 provider
->set_visit_count(0);
5061 // Register a test extension externally using the mock registry provider.
5062 base::FilePath source_path
= data_dir().AppendASCII("good.crx");
5064 // Add the extension.
5065 provider
->UpdateOrAddExtension(good_crx
, "1.0.0.0", source_path
);
5067 // Reloading extensions should find our externally registered extension
5069 content::WindowedNotificationObserver
observer(
5070 extensions::NOTIFICATION_CRX_INSTALLER_DONE
,
5071 content::NotificationService::AllSources());
5072 service()->CheckForExternalUpdates();
5075 ASSERT_EQ(0u, GetErrors().size());
5076 ASSERT_EQ(1u, loaded_
.size());
5077 ASSERT_EQ(location
, loaded_
[0]->location());
5078 ASSERT_EQ("1.0.0.0", loaded_
[0]->version()->GetString());
5079 ValidatePrefKeyCount(1);
5080 ValidateIntegerPref(good_crx
, "state", Extension::ENABLED
);
5081 ValidateIntegerPref(good_crx
, "location", location
);
5083 // Reload extensions without changing anything. The extension should be
5086 service()->ReloadExtensionsForTest();
5087 base::RunLoop().RunUntilIdle();
5088 ASSERT_EQ(0u, GetErrors().size());
5089 ASSERT_EQ(1u, loaded_
.size());
5090 ValidatePrefKeyCount(1);
5091 ValidateIntegerPref(good_crx
, "state", Extension::ENABLED
);
5092 ValidateIntegerPref(good_crx
, "location", location
);
5094 // Now update the extension with a new version. We should get upgraded.
5095 source_path
= source_path
.DirName().AppendASCII("good2.crx");
5096 provider
->UpdateOrAddExtension(good_crx
, "1.0.0.1", source_path
);
5099 content::WindowedNotificationObserver
observer_2(
5100 extensions::NOTIFICATION_CRX_INSTALLER_DONE
,
5101 content::NotificationService::AllSources());
5102 service()->CheckForExternalUpdates();
5104 ASSERT_EQ(0u, GetErrors().size());
5105 ASSERT_EQ(1u, loaded_
.size());
5106 ASSERT_EQ("1.0.0.1", loaded_
[0]->version()->GetString());
5107 ValidatePrefKeyCount(1);
5108 ValidateIntegerPref(good_crx
, "state", Extension::ENABLED
);
5109 ValidateIntegerPref(good_crx
, "location", location
);
5111 // Uninstall the extension and reload. Nothing should happen because the
5112 // preference should prevent us from reinstalling.
5113 std::string id
= loaded_
[0]->id();
5115 GetManagementPolicy()->MustRemainEnabled(loaded_
[0].get(), NULL
);
5116 service()->UninstallExtension(id
,
5117 extensions::UNINSTALL_REASON_FOR_TESTING
,
5118 base::Bind(&base::DoNothing
),
5120 base::RunLoop().RunUntilIdle();
5122 base::FilePath install_path
= extensions_install_dir().AppendASCII(id
);
5124 // Policy controlled extensions should not have been touched by uninstall.
5125 ASSERT_TRUE(base::PathExists(install_path
));
5127 // The extension should also be gone from the install directory.
5128 ASSERT_FALSE(base::PathExists(install_path
));
5130 service()->CheckForExternalUpdates();
5131 base::RunLoop().RunUntilIdle();
5132 ASSERT_EQ(0u, loaded_
.size());
5133 ValidatePrefKeyCount(1);
5134 ValidateIntegerPref(good_crx
, "state",
5135 Extension::EXTERNAL_EXTENSION_UNINSTALLED
);
5136 ValidateIntegerPref(good_crx
, "location", location
);
5138 // Now clear the preference and reinstall.
5139 SetPrefInteg(good_crx
, "state", Extension::ENABLED
);
5142 content::WindowedNotificationObserver
observer(
5143 extensions::NOTIFICATION_CRX_INSTALLER_DONE
,
5144 content::NotificationService::AllSources());
5145 service()->CheckForExternalUpdates();
5147 ASSERT_EQ(1u, loaded_
.size());
5149 ValidatePrefKeyCount(1);
5150 ValidateIntegerPref(good_crx
, "state", Extension::ENABLED
);
5151 ValidateIntegerPref(good_crx
, "location", location
);
5153 if (GetManagementPolicy()->MustRemainEnabled(loaded_
[0].get(), NULL
)) {
5154 EXPECT_EQ(2, provider
->visit_count());
5156 // Now test an externally triggered uninstall (deleting the registry key or
5158 provider
->RemoveExtension(good_crx
);
5161 service()->OnExternalProviderReady(provider
);
5162 base::RunLoop().RunUntilIdle();
5163 ASSERT_EQ(0u, loaded_
.size());
5164 ValidatePrefKeyCount(0);
5166 // The extension should also be gone from the install directory.
5167 ASSERT_FALSE(base::PathExists(install_path
));
5169 // Now test the case where user uninstalls and then the extension is removed
5170 // from the external provider.
5171 content::WindowedNotificationObserver
observer(
5172 extensions::NOTIFICATION_CRX_INSTALLER_DONE
,
5173 content::NotificationService::AllSources());
5174 provider
->UpdateOrAddExtension(good_crx
, "1.0.0.1", source_path
);
5175 service()->CheckForExternalUpdates();
5178 ASSERT_EQ(1u, loaded_
.size());
5179 ASSERT_EQ(0u, GetErrors().size());
5183 service()->UninstallExtension(id
,
5184 extensions::UNINSTALL_REASON_FOR_TESTING
,
5185 base::Bind(&base::DoNothing
),
5187 base::RunLoop().RunUntilIdle();
5188 ASSERT_EQ(0u, loaded_
.size());
5190 // Then remove the extension from the extension provider.
5191 provider
->RemoveExtension(good_crx
);
5193 // Should still be at 0.
5195 extensions::InstalledLoader(service()).LoadAllExtensions();
5196 base::RunLoop().RunUntilIdle();
5197 ASSERT_EQ(0u, loaded_
.size());
5198 ValidatePrefKeyCount(1);
5200 EXPECT_EQ(5, provider
->visit_count());
5204 // Tests the external installation feature
5206 TEST_F(ExtensionServiceTest
, ExternalInstallRegistry
) {
5207 // This should all work, even when normal extension installation is disabled.
5208 InitializeEmptyExtensionService();
5209 service()->set_extensions_enabled(false);
5211 // Now add providers. Extension system takes ownership of the objects.
5212 MockExtensionProvider
* reg_provider
=
5213 new MockExtensionProvider(service(), Manifest::EXTERNAL_REGISTRY
);
5214 AddMockExternalProvider(reg_provider
);
5215 TestExternalProvider(reg_provider
, Manifest::EXTERNAL_REGISTRY
);
5219 TEST_F(ExtensionServiceTest
, ExternalInstallPref
) {
5220 InitializeEmptyExtensionService();
5222 // Now add providers. Extension system takes ownership of the objects.
5223 MockExtensionProvider
* pref_provider
=
5224 new MockExtensionProvider(service(), Manifest::EXTERNAL_PREF
);
5226 AddMockExternalProvider(pref_provider
);
5227 TestExternalProvider(pref_provider
, Manifest::EXTERNAL_PREF
);
5230 TEST_F(ExtensionServiceTest
, ExternalInstallPrefUpdateUrl
) {
5231 // This should all work, even when normal extension installation is disabled.
5232 InitializeEmptyExtensionService();
5233 service()->set_extensions_enabled(false);
5235 // TODO(skerner): The mock provider is not a good model of a provider
5236 // that works with update URLs, because it adds file and version info.
5237 // Extend the mock to work with update URLs. This test checks the
5238 // behavior that is common to all external extension visitors. The
5239 // browser test ExtensionManagementTest.ExternalUrlUpdate tests that
5240 // what the visitor does results in an extension being downloaded and
5242 MockExtensionProvider
* pref_provider
=
5243 new MockExtensionProvider(service(), Manifest::EXTERNAL_PREF_DOWNLOAD
);
5244 AddMockExternalProvider(pref_provider
);
5245 TestExternalProvider(pref_provider
, Manifest::EXTERNAL_PREF_DOWNLOAD
);
5248 TEST_F(ExtensionServiceTest
, ExternalInstallPolicyUpdateUrl
) {
5249 // This should all work, even when normal extension installation is disabled.
5250 InitializeEmptyExtensionService();
5251 service()->set_extensions_enabled(false);
5253 // TODO(skerner): The mock provider is not a good model of a provider
5254 // that works with update URLs, because it adds file and version info.
5255 // Extend the mock to work with update URLs. This test checks the
5256 // behavior that is common to all external extension visitors. The
5257 // browser test ExtensionManagementTest.ExternalUrlUpdate tests that
5258 // what the visitor does results in an extension being downloaded and
5260 MockExtensionProvider
* pref_provider
=
5261 new MockExtensionProvider(service(), Manifest::EXTERNAL_POLICY_DOWNLOAD
);
5262 AddMockExternalProvider(pref_provider
);
5263 TestExternalProvider(pref_provider
, Manifest::EXTERNAL_POLICY_DOWNLOAD
);
5266 // Tests that external extensions get uninstalled when the external extension
5267 // providers can't account for them.
5268 TEST_F(ExtensionServiceTest
, ExternalUninstall
) {
5269 // Start the extensions service with one external extension already installed.
5270 base::FilePath source_install_dir
=
5271 data_dir().AppendASCII("good").AppendASCII("Extensions");
5272 base::FilePath pref_path
= source_install_dir
5274 .AppendASCII("PreferencesExternal");
5276 // This initializes the extensions service with no ExternalProviders.
5277 InitializeInstalledExtensionService(pref_path
, source_install_dir
);
5278 service()->set_extensions_enabled(false);
5282 ASSERT_EQ(0u, GetErrors().size());
5283 ASSERT_EQ(0u, loaded_
.size());
5285 // Verify that it's not the disabled extensions flag causing it not to load.
5286 service()->set_extensions_enabled(true);
5287 service()->ReloadExtensionsForTest();
5288 base::RunLoop().RunUntilIdle();
5290 ASSERT_EQ(0u, GetErrors().size());
5291 ASSERT_EQ(0u, loaded_
.size());
5294 // Test that running multiple update checks simultaneously does not
5295 // keep the update from succeeding.
5296 TEST_F(ExtensionServiceTest
, MultipleExternalUpdateCheck
) {
5297 InitializeEmptyExtensionService();
5299 MockExtensionProvider
* provider
=
5300 new MockExtensionProvider(service(), Manifest::EXTERNAL_PREF
);
5301 AddMockExternalProvider(provider
);
5303 // Verify that starting with no providers loads no extensions.
5305 ASSERT_EQ(0u, loaded_
.size());
5307 // Start two checks for updates.
5308 provider
->set_visit_count(0);
5309 service()->CheckForExternalUpdates();
5310 service()->CheckForExternalUpdates();
5311 base::RunLoop().RunUntilIdle();
5313 // Two calls should cause two checks for external extensions.
5314 EXPECT_EQ(2, provider
->visit_count());
5315 EXPECT_EQ(0u, GetErrors().size());
5316 EXPECT_EQ(0u, loaded_
.size());
5318 // Register a test extension externally using the mock registry provider.
5319 base::FilePath source_path
= data_dir().AppendASCII("good.crx");
5320 provider
->UpdateOrAddExtension(good_crx
, "1.0.0.0", source_path
);
5322 // Two checks for external updates should find the extension, and install it
5324 content::WindowedNotificationObserver
observer(
5325 extensions::NOTIFICATION_CRX_INSTALLER_DONE
,
5326 content::NotificationService::AllSources());
5327 provider
->set_visit_count(0);
5328 service()->CheckForExternalUpdates();
5329 service()->CheckForExternalUpdates();
5331 EXPECT_EQ(2, provider
->visit_count());
5332 ASSERT_EQ(0u, GetErrors().size());
5333 ASSERT_EQ(1u, loaded_
.size());
5334 ASSERT_EQ(Manifest::EXTERNAL_PREF
, loaded_
[0]->location());
5335 ASSERT_EQ("1.0.0.0", loaded_
[0]->version()->GetString());
5336 ValidatePrefKeyCount(1);
5337 ValidateIntegerPref(good_crx
, "state", Extension::ENABLED
);
5338 ValidateIntegerPref(good_crx
, "location", Manifest::EXTERNAL_PREF
);
5340 provider
->RemoveExtension(good_crx
);
5341 provider
->set_visit_count(0);
5342 service()->CheckForExternalUpdates();
5343 service()->CheckForExternalUpdates();
5344 base::RunLoop().RunUntilIdle();
5346 // Two calls should cause two checks for external extensions.
5347 // Because the external source no longer includes good_crx,
5348 // good_crx will be uninstalled. So, expect that no extensions
5350 EXPECT_EQ(2, provider
->visit_count());
5351 EXPECT_EQ(0u, GetErrors().size());
5352 EXPECT_EQ(0u, loaded_
.size());
5355 TEST_F(ExtensionServiceTest
, ExternalPrefProvider
) {
5356 InitializeEmptyExtensionService();
5358 // Test some valid extension records.
5359 // Set a base path to avoid erroring out on relative paths.
5360 // Paths starting with // are absolute on every platform we support.
5361 base::FilePath
base_path(FILE_PATH_LITERAL("//base/path"));
5362 ASSERT_TRUE(base_path
.IsAbsolute());
5363 MockProviderVisitor
visitor(base_path
);
5364 std::string json_data
=
5366 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\": {"
5367 " \"external_crx\": \"RandomExtension.crx\","
5368 " \"external_version\": \"1.0\""
5370 " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\": {"
5371 " \"external_crx\": \"RandomExtension2.crx\","
5372 " \"external_version\": \"2.0\""
5374 " \"cccccccccccccccccccccccccccccccc\": {"
5375 " \"external_update_url\": \"http:\\\\foo.com/update\","
5376 " \"install_parameter\": \"id\""
5379 EXPECT_EQ(3, visitor
.Visit(json_data
));
5381 // Simulate an external_extensions.json file that contains seven invalid
5383 // - One that is missing the 'external_crx' key.
5384 // - One that is missing the 'external_version' key.
5385 // - One that is specifying .. in the path.
5386 // - One that specifies both a file and update URL.
5387 // - One that specifies no file or update URL.
5388 // - One that has an update URL that is not well formed.
5389 // - One that contains a malformed version.
5390 // - One that has an invalid id.
5391 // - One that has a non-dictionary value.
5392 // - One that has an integer 'external_version' instead of a string.
5393 // The final extension is valid, and we check that it is read to make sure
5394 // failures don't stop valid records from being read.
5397 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\": {"
5398 " \"external_version\": \"1.0\""
5400 " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\": {"
5401 " \"external_crx\": \"RandomExtension.crx\""
5403 " \"cccccccccccccccccccccccccccccccc\": {"
5404 " \"external_crx\": \"..\\\\foo\\\\RandomExtension2.crx\","
5405 " \"external_version\": \"2.0\""
5407 " \"dddddddddddddddddddddddddddddddd\": {"
5408 " \"external_crx\": \"RandomExtension2.crx\","
5409 " \"external_version\": \"2.0\","
5410 " \"external_update_url\": \"http:\\\\foo.com/update\""
5412 " \"eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee\": {"
5414 " \"ffffffffffffffffffffffffffffffff\": {"
5415 " \"external_update_url\": \"This string is not a valid URL\""
5417 " \"gggggggggggggggggggggggggggggggg\": {"
5418 " \"external_crx\": \"RandomExtension3.crx\","
5419 " \"external_version\": \"This is not a valid version!\""
5421 " \"This is not a valid id!\": {},"
5422 " \"hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh\": true,"
5423 " \"iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii\": {"
5424 " \"external_crx\": \"RandomExtension4.crx\","
5425 " \"external_version\": 1.0"
5427 " \"pppppppppppppppppppppppppppppppp\": {"
5428 " \"external_crx\": \"RandomValidExtension.crx\","
5429 " \"external_version\": \"1.0\""
5432 EXPECT_EQ(1, visitor
.Visit(json_data
));
5434 // Check that if a base path is not provided, use of a relative
5436 base::FilePath empty
;
5437 MockProviderVisitor
visitor_no_relative_paths(empty
);
5439 // Use absolute paths. Expect success.
5442 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\": {"
5443 " \"external_crx\": \"//RandomExtension1.crx\","
5444 " \"external_version\": \"3.0\""
5446 " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\": {"
5447 " \"external_crx\": \"//path/to/RandomExtension2.crx\","
5448 " \"external_version\": \"3.0\""
5451 EXPECT_EQ(2, visitor_no_relative_paths
.Visit(json_data
));
5453 // Use a relative path. Expect that it will error out.
5456 " \"cccccccccccccccccccccccccccccccc\": {"
5457 " \"external_crx\": \"RandomExtension2.crx\","
5458 " \"external_version\": \"3.0\""
5461 EXPECT_EQ(0, visitor_no_relative_paths
.Visit(json_data
));
5463 // Test supported_locales.
5466 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\": {"
5467 " \"external_crx\": \"RandomExtension.crx\","
5468 " \"external_version\": \"1.0\","
5469 " \"supported_locales\": [ \"en\" ]"
5471 " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\": {"
5472 " \"external_crx\": \"RandomExtension2.crx\","
5473 " \"external_version\": \"2.0\","
5474 " \"supported_locales\": [ \"en-GB\" ]"
5476 " \"cccccccccccccccccccccccccccccccc\": {"
5477 " \"external_crx\": \"RandomExtension2.crx\","
5478 " \"external_version\": \"3.0\","
5479 " \"supported_locales\": [ \"en_US\", \"fr\" ]"
5483 ScopedBrowserLocale
guard("en-US");
5484 EXPECT_EQ(2, visitor
.Visit(json_data
));
5487 // Test keep_if_present.
5490 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\": {"
5491 " \"external_crx\": \"RandomExtension.crx\","
5492 " \"external_version\": \"1.0\","
5493 " \"keep_if_present\": true"
5497 EXPECT_EQ(0, visitor
.Visit(json_data
));
5500 // Test is_bookmark_app.
5501 MockProviderVisitor
from_bookmark_visitor(
5502 base_path
, Extension::FROM_BOOKMARK
);
5505 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\": {"
5506 " \"external_crx\": \"RandomExtension.crx\","
5507 " \"external_version\": \"1.0\","
5508 " \"is_bookmark_app\": true"
5511 EXPECT_EQ(1, from_bookmark_visitor
.Visit(json_data
));
5513 // Test is_from_webstore.
5514 MockProviderVisitor
from_webstore_visitor(
5515 base_path
, Extension::FROM_WEBSTORE
);
5518 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\": {"
5519 " \"external_crx\": \"RandomExtension.crx\","
5520 " \"external_version\": \"1.0\","
5521 " \"is_from_webstore\": true"
5524 EXPECT_EQ(1, from_webstore_visitor
.Visit(json_data
));
5526 // Test was_installed_by_eom.
5527 MockProviderVisitor
was_installed_by_eom_visitor(
5528 base_path
, Extension::WAS_INSTALLED_BY_OEM
);
5531 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\": {"
5532 " \"external_crx\": \"RandomExtension.crx\","
5533 " \"external_version\": \"1.0\","
5534 " \"was_installed_by_oem\": true"
5537 EXPECT_EQ(1, was_installed_by_eom_visitor
.Visit(json_data
));
5539 // Test min_profile_created_by_version.
5540 MockProviderVisitor
min_profile_created_by_version_visitor(base_path
);
5543 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\": {"
5544 " \"external_crx\": \"RandomExtension.crx\","
5545 " \"external_version\": \"1.0\","
5546 " \"min_profile_created_by_version\": \"42.0.0.1\""
5548 " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\": {"
5549 " \"external_crx\": \"RandomExtension2.crx\","
5550 " \"external_version\": \"1.0\","
5551 " \"min_profile_created_by_version\": \"43.0.0.1\""
5553 " \"cccccccccccccccccccccccccccccccc\": {"
5554 " \"external_crx\": \"RandomExtension3.crx\","
5555 " \"external_version\": \"3.0\","
5556 " \"min_profile_created_by_version\": \"44.0.0.1\""
5559 min_profile_created_by_version_visitor
.profile()->GetPrefs()->SetString(
5560 prefs::kProfileCreatedByVersion
, "40.0.0.1");
5561 EXPECT_EQ(0, min_profile_created_by_version_visitor
.Visit(json_data
));
5562 min_profile_created_by_version_visitor
.profile()->GetPrefs()->SetString(
5563 prefs::kProfileCreatedByVersion
, "43.0.0.1");
5564 EXPECT_EQ(2, min_profile_created_by_version_visitor
.Visit(json_data
));
5565 min_profile_created_by_version_visitor
.profile()->GetPrefs()->SetString(
5566 prefs::kProfileCreatedByVersion
, "45.0.0.1");
5567 EXPECT_EQ(3, min_profile_created_by_version_visitor
.Visit(json_data
));
5570 // Test loading good extensions from the profile directory.
5571 TEST_F(ExtensionServiceTest
, LoadAndRelocalizeExtensions
) {
5572 // Ensure we're testing in "en" and leave global state untouched.
5573 extension_l10n_util::ScopedLocaleForTest
testLocale("en");
5575 // Initialize the test dir with a good Preferences/extensions.
5576 base::FilePath source_install_dir
= data_dir().AppendASCII("l10n");
5577 base::FilePath pref_path
=
5578 source_install_dir
.Append(chrome::kPreferencesFilename
);
5579 InitializeInstalledExtensionService(pref_path
, source_install_dir
);
5583 ASSERT_EQ(3u, loaded_
.size());
5585 // This was equal to "sr" on load.
5586 ValidateStringPref(loaded_
[0]->id(), keys::kCurrentLocale
, "en");
5588 // These are untouched by re-localization.
5589 ValidateStringPref(loaded_
[1]->id(), keys::kCurrentLocale
, "en");
5590 EXPECT_FALSE(IsPrefExist(loaded_
[1]->id(), keys::kCurrentLocale
));
5592 // This one starts with Serbian name, and gets re-localized into English.
5593 EXPECT_EQ("My name is simple.", loaded_
[0]->name());
5595 // These are untouched by re-localization.
5596 EXPECT_EQ("My name is simple.", loaded_
[1]->name());
5597 EXPECT_EQ("no l10n", loaded_
[2]->name());
5600 class ExtensionsReadyRecorder
: public content::NotificationObserver
{
5602 ExtensionsReadyRecorder() : ready_(false) {
5603 registrar_
.Add(this,
5604 extensions::NOTIFICATION_EXTENSIONS_READY_DEPRECATED
,
5605 content::NotificationService::AllSources());
5608 void set_ready(bool value
) { ready_
= value
; }
5609 bool ready() { return ready_
; }
5612 void Observe(int type
,
5613 const content::NotificationSource
& source
,
5614 const content::NotificationDetails
& details
) override
{
5616 case extensions::NOTIFICATION_EXTENSIONS_READY_DEPRECATED
:
5624 content::NotificationRegistrar registrar_
;
5628 // Test that we get enabled/disabled correctly for all the pref/command-line
5629 // combinations. We don't want to derive from the ExtensionServiceTest class
5630 // for this test, so we use ExtensionServiceTestSimple.
5632 // Also tests that we always fire EXTENSIONS_READY, no matter whether we are
5634 class ExtensionServiceTestSimple
: public testing::Test
{
5635 content::TestBrowserThreadBundle thread_bundle_
;
5638 TEST_F(ExtensionServiceTestSimple
, Enabledness
) {
5639 // Make sure the PluginService singleton is destroyed at the end of the test.
5640 base::ShadowingAtExitManager at_exit_manager
;
5641 #if defined(ENABLE_PLUGINS)
5642 content::PluginService::GetInstance()->Init();
5643 content::PluginService::GetInstance()->DisablePluginsDiscoveryForTesting();
5646 ExtensionErrorReporter::Init(false); // no noisy errors
5647 ExtensionsReadyRecorder recorder
;
5648 scoped_ptr
<TestingProfile
> profile(new TestingProfile());
5649 #if defined OS_CHROMEOS
5650 chromeos::ScopedTestDeviceSettingsService device_settings_service
;
5651 chromeos::ScopedTestCrosSettings cros_settings
;
5652 scoped_ptr
<chromeos::ScopedTestUserManager
> user_manager(
5653 new chromeos::ScopedTestUserManager
);
5655 scoped_ptr
<base::CommandLine
> command_line
;
5656 base::FilePath install_dir
= profile
->GetPath()
5657 .AppendASCII(extensions::kInstallDirectoryName
);
5659 // By default, we are enabled.
5660 command_line
.reset(new base::CommandLine(base::CommandLine::NO_PROGRAM
));
5661 ExtensionService
* service
= static_cast<extensions::TestExtensionSystem
*>(
5662 ExtensionSystem::Get(profile
.get()))->
5663 CreateExtensionService(
5667 EXPECT_TRUE(service
->extensions_enabled());
5669 base::RunLoop().RunUntilIdle();
5670 EXPECT_TRUE(recorder
.ready());
5671 #if defined OS_CHROMEOS
5672 user_manager
.reset();
5675 // If either the command line or pref is set, we are disabled.
5676 recorder
.set_ready(false);
5677 profile
.reset(new TestingProfile());
5678 command_line
->AppendSwitch(switches::kDisableExtensions
);
5679 service
= static_cast<extensions::TestExtensionSystem
*>(
5680 ExtensionSystem::Get(profile
.get()))->
5681 CreateExtensionService(
5685 EXPECT_FALSE(service
->extensions_enabled());
5687 base::RunLoop().RunUntilIdle();
5688 EXPECT_TRUE(recorder
.ready());
5690 recorder
.set_ready(false);
5691 profile
.reset(new TestingProfile());
5692 profile
->GetPrefs()->SetBoolean(prefs::kDisableExtensions
, true);
5693 service
= static_cast<extensions::TestExtensionSystem
*>(
5694 ExtensionSystem::Get(profile
.get()))->
5695 CreateExtensionService(
5699 EXPECT_FALSE(service
->extensions_enabled());
5701 base::RunLoop().RunUntilIdle();
5702 EXPECT_TRUE(recorder
.ready());
5704 recorder
.set_ready(false);
5705 profile
.reset(new TestingProfile());
5706 profile
->GetPrefs()->SetBoolean(prefs::kDisableExtensions
, true);
5707 command_line
.reset(new base::CommandLine(base::CommandLine::NO_PROGRAM
));
5708 service
= static_cast<extensions::TestExtensionSystem
*>(
5709 ExtensionSystem::Get(profile
.get()))->
5710 CreateExtensionService(
5714 EXPECT_FALSE(service
->extensions_enabled());
5716 base::RunLoop().RunUntilIdle();
5717 EXPECT_TRUE(recorder
.ready());
5719 // Explicitly delete all the resources used in this test.
5722 // Execute any pending deletion tasks.
5723 base::RunLoop().RunUntilIdle();
5726 // Test loading extensions that require limited and unlimited storage quotas.
5727 TEST_F(ExtensionServiceTest
, StorageQuota
) {
5728 InitializeEmptyExtensionService();
5730 base::FilePath extensions_path
= data_dir().AppendASCII("storage_quota");
5732 base::FilePath limited_quota_ext
=
5733 extensions_path
.AppendASCII("limited_quota")
5734 .AppendASCII("1.0");
5736 // The old permission name for unlimited quota was "unlimited_storage", but
5737 // we changed it to "unlimitedStorage". This tests both versions.
5738 base::FilePath unlimited_quota_ext
=
5739 extensions_path
.AppendASCII("unlimited_quota")
5740 .AppendASCII("1.0");
5741 base::FilePath unlimited_quota_ext2
=
5742 extensions_path
.AppendASCII("unlimited_quota")
5743 .AppendASCII("2.0");
5744 extensions::UnpackedInstaller::Create(service())->Load(limited_quota_ext
);
5745 extensions::UnpackedInstaller::Create(service())->Load(unlimited_quota_ext
);
5746 extensions::UnpackedInstaller::Create(service())->Load(unlimited_quota_ext2
);
5747 base::RunLoop().RunUntilIdle();
5749 ASSERT_EQ(3u, loaded_
.size());
5750 EXPECT_TRUE(profile());
5751 EXPECT_FALSE(profile()->IsOffTheRecord());
5753 profile()->GetExtensionSpecialStoragePolicy()->IsStorageUnlimited(
5754 loaded_
[0]->url()));
5755 EXPECT_TRUE(profile()->GetExtensionSpecialStoragePolicy()->IsStorageUnlimited(
5756 loaded_
[1]->url()));
5757 EXPECT_TRUE(profile()->GetExtensionSpecialStoragePolicy()->IsStorageUnlimited(
5758 loaded_
[2]->url()));
5761 // Tests ComponentLoader::Add().
5762 TEST_F(ExtensionServiceTest
, ComponentExtensions
) {
5763 InitializeEmptyExtensionService();
5765 // Component extensions should work even when extensions are disabled.
5766 service()->set_extensions_enabled(false);
5768 base::FilePath path
= data_dir()
5769 .AppendASCII("good")
5770 .AppendASCII("Extensions")
5771 .AppendASCII("behllobkkfkfnphdnhnkndlbkcpglgmj")
5772 .AppendASCII("1.0.0.0");
5774 std::string manifest
;
5775 ASSERT_TRUE(base::ReadFileToString(
5776 path
.Append(extensions::kManifestFilename
), &manifest
));
5778 service()->component_loader()->Add(manifest
, path
);
5781 // Note that we do not pump messages -- the extension should be loaded
5784 EXPECT_EQ(0u, GetErrors().size());
5785 ASSERT_EQ(1u, loaded_
.size());
5786 EXPECT_EQ(Manifest::COMPONENT
, loaded_
[0]->location());
5787 EXPECT_EQ(1u, registry()->enabled_extensions().size());
5789 // Component extensions get a prefs entry on first install.
5790 ValidatePrefKeyCount(1);
5792 // Reload all extensions, and make sure it comes back.
5793 std::string extension_id
= (*registry()->enabled_extensions().begin())->id();
5795 service()->ReloadExtensionsForTest();
5796 ASSERT_EQ(1u, registry()->enabled_extensions().size());
5797 EXPECT_EQ(extension_id
, (*registry()->enabled_extensions().begin())->id());
5800 TEST_F(ExtensionServiceTest
, DeferredSyncStartupPreInstalledComponent
) {
5801 InitializeEmptyExtensionService();
5802 InitializeExtensionSyncService();
5804 bool flare_was_called
= false;
5805 syncer::ModelType
triggered_type(syncer::UNSPECIFIED
);
5806 base::WeakPtrFactory
<ExtensionServiceTest
> factory(this);
5807 extension_sync_service()->SetSyncStartFlare(
5808 base::Bind(&ExtensionServiceTest::MockSyncStartFlare
,
5809 factory
.GetWeakPtr(),
5810 &flare_was_called
, // Safe due to WeakPtrFactory scope.
5811 &triggered_type
)); // Safe due to WeakPtrFactory scope.
5813 // Install a component extension.
5814 std::string manifest
;
5815 ASSERT_TRUE(base::ReadFileToString(
5816 good0_path().Append(extensions::kManifestFilename
), &manifest
));
5817 service()->component_loader()->Add(manifest
, good0_path());
5818 ASSERT_FALSE(service()->is_ready());
5820 ASSERT_TRUE(service()->is_ready());
5822 // Extensions added before service is_ready() don't trigger sync startup.
5823 EXPECT_FALSE(flare_was_called
);
5824 ASSERT_EQ(syncer::UNSPECIFIED
, triggered_type
);
5827 TEST_F(ExtensionServiceTest
, DeferredSyncStartupPreInstalledNormal
) {
5828 InitializeGoodInstalledExtensionService();
5829 InitializeExtensionSyncService();
5831 bool flare_was_called
= false;
5832 syncer::ModelType
triggered_type(syncer::UNSPECIFIED
);
5833 base::WeakPtrFactory
<ExtensionServiceTest
> factory(this);
5834 extension_sync_service()->SetSyncStartFlare(
5835 base::Bind(&ExtensionServiceTest::MockSyncStartFlare
,
5836 factory
.GetWeakPtr(),
5837 &flare_was_called
, // Safe due to WeakPtrFactory scope.
5838 &triggered_type
)); // Safe due to WeakPtrFactory scope.
5840 ASSERT_FALSE(service()->is_ready());
5842 ASSERT_EQ(3u, loaded_
.size());
5843 ASSERT_TRUE(service()->is_ready());
5845 // Extensions added before service is_ready() don't trigger sync startup.
5846 EXPECT_FALSE(flare_was_called
);
5847 ASSERT_EQ(syncer::UNSPECIFIED
, triggered_type
);
5850 TEST_F(ExtensionServiceTest
, DeferredSyncStartupOnInstall
) {
5851 InitializeEmptyExtensionService();
5852 InitializeExtensionSyncService();
5854 ASSERT_TRUE(service()->is_ready());
5856 bool flare_was_called
= false;
5857 syncer::ModelType
triggered_type(syncer::UNSPECIFIED
);
5858 base::WeakPtrFactory
<ExtensionServiceTest
> factory(this);
5859 extension_sync_service()->SetSyncStartFlare(
5860 base::Bind(&ExtensionServiceTest::MockSyncStartFlare
,
5861 factory
.GetWeakPtr(),
5862 &flare_was_called
, // Safe due to WeakPtrFactory scope.
5863 &triggered_type
)); // Safe due to WeakPtrFactory scope.
5865 base::FilePath path
= data_dir().AppendASCII("good.crx");
5866 InstallCRX(path
, INSTALL_NEW
);
5868 EXPECT_TRUE(flare_was_called
);
5869 EXPECT_EQ(syncer::EXTENSIONS
, triggered_type
);
5872 flare_was_called
= false;
5873 triggered_type
= syncer::UNSPECIFIED
;
5875 // Once sync starts, flare should no longer be invoked.
5876 extension_sync_service()->MergeDataAndStartSyncing(
5878 syncer::SyncDataList(),
5879 scoped_ptr
<syncer::SyncChangeProcessor
>(
5880 new syncer::FakeSyncChangeProcessor
),
5881 scoped_ptr
<syncer::SyncErrorFactory
>(new syncer::SyncErrorFactoryMock()));
5882 path
= data_dir().AppendASCII("page_action.crx");
5883 InstallCRX(path
, INSTALL_NEW
);
5884 EXPECT_FALSE(flare_was_called
);
5885 ASSERT_EQ(syncer::UNSPECIFIED
, triggered_type
);
5888 TEST_F(ExtensionServiceTest
, DisableExtensionFromSync
) {
5889 // Start the extensions service with one external extension already installed.
5890 base::FilePath source_install_dir
=
5891 data_dir().AppendASCII("good").AppendASCII("Extensions");
5892 base::FilePath pref_path
=
5893 source_install_dir
.DirName().Append(chrome::kPreferencesFilename
);
5895 InitializeInstalledExtensionService(pref_path
, source_install_dir
);
5896 InitializeExtensionSyncService();
5898 // The user has enabled sync.
5899 ProfileSyncService
* sync_service
=
5900 ProfileSyncServiceFactory::GetForProfile(profile());
5901 sync_service
->SetSyncSetupCompleted();
5904 ASSERT_TRUE(service()->is_ready());
5906 ASSERT_EQ(3u, loaded_
.size());
5908 // We start enabled.
5909 const Extension
* extension
= service()->GetExtensionById(good0
, true);
5910 ASSERT_TRUE(extension
);
5911 ASSERT_TRUE(service()->IsExtensionEnabled(good0
));
5912 ExtensionSyncData
disable_good_crx(*extension
, false, false, false,
5913 ExtensionSyncData::BOOLEAN_UNSET
);
5915 // Then sync data arrives telling us to disable |good0|.
5916 syncer::SyncDataList sync_data
;
5917 sync_data
.push_back(disable_good_crx
.GetSyncData());
5918 extension_sync_service()->MergeDataAndStartSyncing(
5921 scoped_ptr
<syncer::SyncChangeProcessor
>(
5922 new syncer::FakeSyncChangeProcessor
),
5923 scoped_ptr
<syncer::SyncErrorFactory
>(new syncer::SyncErrorFactoryMock()));
5924 ASSERT_FALSE(service()->IsExtensionEnabled(good0
));
5927 TEST_F(ExtensionServiceTest
, DontDisableExtensionWithPendingEnableFromSync
) {
5928 // Start the extensions service with one external extension already installed.
5929 base::FilePath source_install_dir
=
5930 data_dir().AppendASCII("good").AppendASCII("Extensions");
5931 base::FilePath pref_path
=
5932 source_install_dir
.DirName().Append(chrome::kPreferencesFilename
);
5934 InitializeInstalledExtensionService(pref_path
, source_install_dir
);
5935 InitializeExtensionSyncService();
5937 // The user has enabled sync.
5938 ProfileSyncService
* sync_service
=
5939 ProfileSyncServiceFactory::GetForProfile(profile());
5940 sync_service
->SetSyncSetupCompleted();
5943 ASSERT_TRUE(service()->is_ready());
5944 ASSERT_EQ(3u, loaded_
.size());
5946 const Extension
* extension
= service()->GetExtensionById(good0
, true);
5947 ASSERT_TRUE(service()->IsExtensionEnabled(good0
));
5949 // Disable extension before first sync data arrives.
5950 service()->DisableExtension(good0
, Extension::DISABLE_USER_ACTION
);
5951 ASSERT_FALSE(service()->IsExtensionEnabled(good0
));
5953 // Enable extension - this is now the most recent state.
5954 service()->EnableExtension(good0
);
5955 ASSERT_TRUE(service()->IsExtensionEnabled(good0
));
5957 // Now sync data comes in that says to disable good0. This should be
5959 ExtensionSyncData
disable_good_crx(*extension
, false, false, false,
5960 ExtensionSyncData::BOOLEAN_FALSE
);
5961 syncer::SyncDataList sync_data
;
5962 sync_data
.push_back(disable_good_crx
.GetSyncData());
5963 extension_sync_service()->MergeDataAndStartSyncing(
5966 scoped_ptr
<syncer::SyncChangeProcessor
>(
5967 new syncer::FakeSyncChangeProcessor
),
5968 scoped_ptr
<syncer::SyncErrorFactory
>(new syncer::SyncErrorFactoryMock()));
5970 // The extension was enabled locally before the sync data arrived, so it
5971 // should still be enabled now.
5972 ASSERT_TRUE(service()->IsExtensionEnabled(good0
));
5975 TEST_F(ExtensionServiceTest
, GetSyncData
) {
5976 InitializeEmptyExtensionService();
5977 InitializeExtensionSyncService();
5978 InstallCRX(data_dir().AppendASCII("good.crx"), INSTALL_NEW
);
5979 const Extension
* extension
= service()->GetInstalledExtension(good_crx
);
5980 ASSERT_TRUE(extension
);
5982 extension_sync_service()->MergeDataAndStartSyncing(
5984 syncer::SyncDataList(),
5985 scoped_ptr
<syncer::SyncChangeProcessor
>(
5986 new syncer::FakeSyncChangeProcessor
),
5987 scoped_ptr
<syncer::SyncErrorFactory
>(new syncer::SyncErrorFactoryMock()));
5989 syncer::SyncDataList list
=
5990 extension_sync_service()->GetAllSyncData(syncer::EXTENSIONS
);
5991 ASSERT_EQ(list
.size(), 1U);
5992 scoped_ptr
<ExtensionSyncData
> data
=
5993 ExtensionSyncData::CreateFromSyncData(list
[0]);
5994 ASSERT_TRUE(data
.get());
5995 EXPECT_EQ(extension
->id(), data
->id());
5996 EXPECT_FALSE(data
->uninstalled());
5997 EXPECT_EQ(service()->IsExtensionEnabled(good_crx
), data
->enabled());
5998 EXPECT_EQ(extensions::util::IsIncognitoEnabled(good_crx
, profile()),
5999 data
->incognito_enabled());
6000 EXPECT_EQ(ExtensionSyncData::BOOLEAN_UNSET
, data
->all_urls_enabled());
6001 EXPECT_TRUE(data
->version().Equals(*extension
->version()));
6002 EXPECT_EQ(extensions::ManifestURL::GetUpdateURL(extension
),
6003 data
->update_url());
6004 EXPECT_EQ(extension
->name(), data
->name());
6007 TEST_F(ExtensionServiceTest
, GetSyncDataTerminated
) {
6008 InitializeEmptyExtensionService();
6009 InitializeExtensionSyncService();
6010 InstallCRX(data_dir().AppendASCII("good.crx"), INSTALL_NEW
);
6011 TerminateExtension(good_crx
);
6012 const Extension
* extension
= service()->GetInstalledExtension(good_crx
);
6013 ASSERT_TRUE(extension
);
6015 syncer::FakeSyncChangeProcessor processor
;
6016 extension_sync_service()->MergeDataAndStartSyncing(
6018 syncer::SyncDataList(),
6019 scoped_ptr
<syncer::SyncChangeProcessor
>(
6020 new syncer::FakeSyncChangeProcessor
),
6021 scoped_ptr
<syncer::SyncErrorFactory
>(new syncer::SyncErrorFactoryMock()));
6023 syncer::SyncDataList list
=
6024 extension_sync_service()->GetAllSyncData(syncer::EXTENSIONS
);
6025 ASSERT_EQ(list
.size(), 1U);
6026 scoped_ptr
<ExtensionSyncData
> data
=
6027 ExtensionSyncData::CreateFromSyncData(list
[0]);
6028 ASSERT_TRUE(data
.get());
6029 EXPECT_EQ(extension
->id(), data
->id());
6030 EXPECT_FALSE(data
->uninstalled());
6031 EXPECT_EQ(service()->IsExtensionEnabled(good_crx
), data
->enabled());
6032 EXPECT_EQ(extensions::util::IsIncognitoEnabled(good_crx
, profile()),
6033 data
->incognito_enabled());
6034 EXPECT_EQ(ExtensionSyncData::BOOLEAN_UNSET
, data
->all_urls_enabled());
6035 EXPECT_TRUE(data
->version().Equals(*extension
->version()));
6036 EXPECT_EQ(extensions::ManifestURL::GetUpdateURL(extension
),
6037 data
->update_url());
6038 EXPECT_EQ(extension
->name(), data
->name());
6041 TEST_F(ExtensionServiceTest
, GetSyncDataFilter
) {
6042 InitializeEmptyExtensionService();
6043 InitializeExtensionSyncService();
6044 InstallCRX(data_dir().AppendASCII("good.crx"), INSTALL_NEW
);
6045 const Extension
* extension
= service()->GetInstalledExtension(good_crx
);
6046 ASSERT_TRUE(extension
);
6048 syncer::FakeSyncChangeProcessor processor
;
6049 extension_sync_service()->MergeDataAndStartSyncing(
6051 syncer::SyncDataList(),
6052 scoped_ptr
<syncer::SyncChangeProcessor
>(
6053 new syncer::FakeSyncChangeProcessor
),
6054 scoped_ptr
<syncer::SyncErrorFactory
>(new syncer::SyncErrorFactoryMock()));
6056 syncer::SyncDataList list
=
6057 extension_sync_service()->GetAllSyncData(syncer::EXTENSIONS
);
6058 ASSERT_EQ(list
.size(), 0U);
6061 TEST_F(ExtensionServiceTest
, GetSyncExtensionDataUserSettings
) {
6062 InitializeEmptyExtensionService();
6063 InitializeExtensionSyncService();
6064 InstallCRX(data_dir().AppendASCII("good.crx"), INSTALL_NEW
);
6065 const Extension
* extension
= service()->GetInstalledExtension(good_crx
);
6066 ASSERT_TRUE(extension
);
6068 syncer::FakeSyncChangeProcessor processor
;
6069 extension_sync_service()->MergeDataAndStartSyncing(
6071 syncer::SyncDataList(),
6072 scoped_ptr
<syncer::SyncChangeProcessor
>(
6073 new syncer::FakeSyncChangeProcessor
),
6074 scoped_ptr
<syncer::SyncErrorFactory
>(new syncer::SyncErrorFactoryMock()));
6077 syncer::SyncDataList list
=
6078 extension_sync_service()->GetAllSyncData(syncer::EXTENSIONS
);
6079 ASSERT_EQ(list
.size(), 1U);
6080 scoped_ptr
<ExtensionSyncData
> data
=
6081 ExtensionSyncData::CreateFromSyncData(list
[0]);
6082 ASSERT_TRUE(data
.get());
6083 EXPECT_TRUE(data
->enabled());
6084 EXPECT_FALSE(data
->incognito_enabled());
6085 EXPECT_EQ(ExtensionSyncData::BOOLEAN_UNSET
, data
->all_urls_enabled());
6088 service()->DisableExtension(good_crx
, Extension::DISABLE_USER_ACTION
);
6090 syncer::SyncDataList list
=
6091 extension_sync_service()->GetAllSyncData(syncer::EXTENSIONS
);
6092 ASSERT_EQ(list
.size(), 1U);
6093 scoped_ptr
<ExtensionSyncData
> data
=
6094 ExtensionSyncData::CreateFromSyncData(list
[0]);
6095 ASSERT_TRUE(data
.get());
6096 EXPECT_FALSE(data
->enabled());
6097 EXPECT_FALSE(data
->incognito_enabled());
6098 EXPECT_EQ(ExtensionSyncData::BOOLEAN_UNSET
, data
->all_urls_enabled());
6101 extensions::util::SetIsIncognitoEnabled(good_crx
, profile(), true);
6102 extensions::util::SetAllowedScriptingOnAllUrls(
6103 good_crx
, profile(), false);
6105 syncer::SyncDataList list
=
6106 extension_sync_service()->GetAllSyncData(syncer::EXTENSIONS
);
6107 ASSERT_EQ(list
.size(), 1U);
6108 scoped_ptr
<ExtensionSyncData
> data
=
6109 ExtensionSyncData::CreateFromSyncData(list
[0]);
6110 ASSERT_TRUE(data
.get());
6111 EXPECT_FALSE(data
->enabled());
6112 EXPECT_TRUE(data
->incognito_enabled());
6113 EXPECT_EQ(ExtensionSyncData::BOOLEAN_FALSE
, data
->all_urls_enabled());
6116 service()->EnableExtension(good_crx
);
6117 extensions::util::SetAllowedScriptingOnAllUrls(
6118 good_crx
, profile(), true);
6120 syncer::SyncDataList list
=
6121 extension_sync_service()->GetAllSyncData(syncer::EXTENSIONS
);
6122 ASSERT_EQ(list
.size(), 1U);
6123 scoped_ptr
<ExtensionSyncData
> data
=
6124 ExtensionSyncData::CreateFromSyncData(list
[0]);
6125 ASSERT_TRUE(data
.get());
6126 EXPECT_TRUE(data
->enabled());
6127 EXPECT_TRUE(data
->incognito_enabled());
6128 EXPECT_EQ(ExtensionSyncData::BOOLEAN_TRUE
, data
->all_urls_enabled());
6132 TEST_F(ExtensionServiceTest
, SyncForUninstalledExternalExtension
) {
6133 InitializeEmptyExtensionService();
6134 InitializeExtensionSyncService();
6135 InstallCRXWithLocation(
6136 data_dir().AppendASCII("good.crx"), Manifest::EXTERNAL_PREF
, INSTALL_NEW
);
6137 const Extension
* extension
= service()->GetInstalledExtension(good_crx
);
6138 ASSERT_TRUE(extension
);
6140 syncer::FakeSyncChangeProcessor processor
;
6141 extension_sync_service()->MergeDataAndStartSyncing(
6143 syncer::SyncDataList(),
6144 scoped_ptr
<syncer::SyncChangeProcessor
>(
6145 new syncer::FakeSyncChangeProcessor
),
6146 scoped_ptr
<syncer::SyncErrorFactory
>(new syncer::SyncErrorFactoryMock()));
6148 UninstallExtension(good_crx
, false);
6150 ExtensionPrefs::Get(profile())->IsExternalExtensionUninstalled(good_crx
));
6152 sync_pb::EntitySpecifics specifics
;
6153 sync_pb::AppSpecifics
* app_specifics
= specifics
.mutable_app();
6154 sync_pb::ExtensionSpecifics
* extension_specifics
=
6155 app_specifics
->mutable_extension();
6156 extension_specifics
->set_id(good_crx
);
6157 extension_specifics
->set_version("1.0");
6158 extension_specifics
->set_enabled(true);
6160 syncer::SyncData sync_data
=
6161 syncer::SyncData::CreateLocalData(good_crx
, "Name", specifics
);
6162 syncer::SyncChange
sync_change(FROM_HERE
,
6163 syncer::SyncChange::ACTION_UPDATE
,
6165 syncer::SyncChangeList
list(1);
6166 list
[0] = sync_change
;
6168 extension_sync_service()->ProcessSyncChanges(FROM_HERE
, list
);
6170 ExtensionPrefs::Get(profile())->IsExternalExtensionUninstalled(good_crx
));
6173 TEST_F(ExtensionServiceTest
, GetSyncAppDataUserSettings
) {
6174 InitializeEmptyExtensionService();
6175 InitializeExtensionSyncService();
6176 const Extension
* app
=
6177 PackAndInstallCRX(data_dir().AppendASCII("app"), INSTALL_NEW
);
6179 ASSERT_TRUE(app
->is_app());
6181 syncer::FakeSyncChangeProcessor processor
;
6182 extension_sync_service()->MergeDataAndStartSyncing(
6184 syncer::SyncDataList(),
6185 scoped_ptr
<syncer::SyncChangeProcessor
>(
6186 new syncer::FakeSyncChangeProcessor
),
6187 scoped_ptr
<syncer::SyncErrorFactory
>(new syncer::SyncErrorFactoryMock()));
6189 syncer::StringOrdinal initial_ordinal
=
6190 syncer::StringOrdinal::CreateInitialOrdinal();
6192 syncer::SyncDataList list
=
6193 extension_sync_service()->GetAllSyncData(syncer::APPS
);
6194 ASSERT_EQ(list
.size(), 1U);
6196 scoped_ptr
<AppSyncData
> app_sync_data
=
6197 AppSyncData::CreateFromSyncData(list
[0]);
6198 EXPECT_TRUE(initial_ordinal
.Equals(app_sync_data
->app_launch_ordinal()));
6199 EXPECT_TRUE(initial_ordinal
.Equals(app_sync_data
->page_ordinal()));
6202 AppSorting
* sorting
= ExtensionPrefs::Get(profile())->app_sorting();
6203 sorting
->SetAppLaunchOrdinal(app
->id(), initial_ordinal
.CreateAfter());
6205 syncer::SyncDataList list
=
6206 extension_sync_service()->GetAllSyncData(syncer::APPS
);
6207 ASSERT_EQ(list
.size(), 1U);
6209 scoped_ptr
<AppSyncData
> app_sync_data
=
6210 AppSyncData::CreateFromSyncData(list
[0]);
6211 ASSERT_TRUE(app_sync_data
.get());
6212 EXPECT_TRUE(initial_ordinal
.LessThan(app_sync_data
->app_launch_ordinal()));
6213 EXPECT_TRUE(initial_ordinal
.Equals(app_sync_data
->page_ordinal()));
6216 sorting
->SetPageOrdinal(app
->id(), initial_ordinal
.CreateAfter());
6218 syncer::SyncDataList list
=
6219 extension_sync_service()->GetAllSyncData(syncer::APPS
);
6220 ASSERT_EQ(list
.size(), 1U);
6222 scoped_ptr
<AppSyncData
> app_sync_data
=
6223 AppSyncData::CreateFromSyncData(list
[0]);
6224 ASSERT_TRUE(app_sync_data
.get());
6225 EXPECT_TRUE(initial_ordinal
.LessThan(app_sync_data
->app_launch_ordinal()));
6226 EXPECT_TRUE(initial_ordinal
.LessThan(app_sync_data
->page_ordinal()));
6230 // TODO (rdevlin.cronin): The OnExtensionMoved() method has been removed from
6231 // ExtensionService, so this test probably needs a new home. Unfortunately, it
6232 // relies pretty heavily on things like InitializeExtension[Sync]Service() and
6233 // PackAndInstallCRX(). When we clean up a bit more, this should move out.
6234 TEST_F(ExtensionServiceTest
, GetSyncAppDataUserSettingsOnExtensionMoved
) {
6235 InitializeEmptyExtensionService();
6236 InitializeExtensionSyncService();
6237 const size_t kAppCount
= 3;
6238 const Extension
* apps
[kAppCount
];
6239 apps
[0] = PackAndInstallCRX(data_dir().AppendASCII("app1"), INSTALL_NEW
);
6240 apps
[1] = PackAndInstallCRX(data_dir().AppendASCII("app2"), INSTALL_NEW
);
6241 apps
[2] = PackAndInstallCRX(data_dir().AppendASCII("app4"), INSTALL_NEW
);
6242 for (size_t i
= 0; i
< kAppCount
; ++i
) {
6243 ASSERT_TRUE(apps
[i
]);
6244 ASSERT_TRUE(apps
[i
]->is_app());
6247 syncer::FakeSyncChangeProcessor processor
;
6248 extension_sync_service()->MergeDataAndStartSyncing(
6250 syncer::SyncDataList(),
6251 scoped_ptr
<syncer::SyncChangeProcessor
>(
6252 new syncer::FakeSyncChangeProcessor
),
6253 scoped_ptr
<syncer::SyncErrorFactory
>(new syncer::SyncErrorFactoryMock()));
6255 ExtensionPrefs::Get(service()->GetBrowserContext())
6257 ->OnExtensionMoved(apps
[0]->id(), apps
[1]->id(), apps
[2]->id());
6259 syncer::SyncDataList list
=
6260 extension_sync_service()->GetAllSyncData(syncer::APPS
);
6261 ASSERT_EQ(list
.size(), 3U);
6263 scoped_ptr
<AppSyncData
> data
[kAppCount
];
6264 for (size_t i
= 0; i
< kAppCount
; ++i
) {
6265 data
[i
] = AppSyncData::CreateFromSyncData(list
[i
]);
6266 ASSERT_TRUE(data
[i
].get());
6269 // The sync data is not always in the same order our apps were installed in,
6270 // so we do that sorting here so we can make sure the values are changed as
6272 syncer::StringOrdinal app_launch_ordinals
[kAppCount
];
6273 for (size_t i
= 0; i
< kAppCount
; ++i
) {
6274 for (size_t j
= 0; j
< kAppCount
; ++j
) {
6275 if (apps
[i
]->id() == data
[j
]->id())
6276 app_launch_ordinals
[i
] = data
[j
]->app_launch_ordinal();
6280 EXPECT_TRUE(app_launch_ordinals
[1].LessThan(app_launch_ordinals
[0]));
6281 EXPECT_TRUE(app_launch_ordinals
[0].LessThan(app_launch_ordinals
[2]));
6285 TEST_F(ExtensionServiceTest
, GetSyncDataList
) {
6286 InitializeEmptyExtensionService();
6287 InitializeExtensionSyncService();
6288 InstallCRX(data_dir().AppendASCII("good.crx"), INSTALL_NEW
);
6289 InstallCRX(data_dir().AppendASCII("page_action.crx"), INSTALL_NEW
);
6290 InstallCRX(data_dir().AppendASCII("theme.crx"), INSTALL_NEW
);
6291 InstallCRX(data_dir().AppendASCII("theme2.crx"), INSTALL_NEW
);
6293 syncer::FakeSyncChangeProcessor processor
;
6294 extension_sync_service()->MergeDataAndStartSyncing(
6296 syncer::SyncDataList(),
6297 scoped_ptr
<syncer::SyncChangeProcessor
>(
6298 new syncer::FakeSyncChangeProcessor
),
6299 scoped_ptr
<syncer::SyncErrorFactory
>(new syncer::SyncErrorFactoryMock()));
6300 extension_sync_service()->MergeDataAndStartSyncing(
6302 syncer::SyncDataList(),
6303 scoped_ptr
<syncer::SyncChangeProcessor
>(
6304 new syncer::FakeSyncChangeProcessor
),
6305 scoped_ptr
<syncer::SyncErrorFactory
>(new syncer::SyncErrorFactoryMock()));
6307 service()->DisableExtension(page_action
, Extension::DISABLE_USER_ACTION
);
6308 TerminateExtension(theme2_crx
);
6310 EXPECT_EQ(0u, extension_sync_service()->GetAllSyncData(syncer::APPS
).size());
6312 2u, extension_sync_service()->GetAllSyncData(syncer::EXTENSIONS
).size());
6315 TEST_F(ExtensionServiceTest
, ProcessSyncDataUninstall
) {
6316 InitializeEmptyExtensionService();
6317 InitializeExtensionSyncService();
6318 syncer::FakeSyncChangeProcessor processor
;
6319 extension_sync_service()->MergeDataAndStartSyncing(
6321 syncer::SyncDataList(),
6322 scoped_ptr
<syncer::SyncChangeProcessor
>(
6323 new syncer::FakeSyncChangeProcessor
),
6324 scoped_ptr
<syncer::SyncErrorFactory
>(new syncer::SyncErrorFactoryMock()));
6326 sync_pb::EntitySpecifics specifics
;
6327 sync_pb::ExtensionSpecifics
* ext_specifics
= specifics
.mutable_extension();
6328 ext_specifics
->set_id(good_crx
);
6329 ext_specifics
->set_version("1.0");
6330 syncer::SyncData sync_data
=
6331 syncer::SyncData::CreateLocalData(good_crx
, "Name", specifics
);
6332 syncer::SyncChange
sync_change(FROM_HERE
,
6333 syncer::SyncChange::ACTION_DELETE
,
6335 syncer::SyncChangeList
list(1);
6336 list
[0] = sync_change
;
6338 // Should do nothing.
6339 extension_sync_service()->ProcessSyncChanges(FROM_HERE
, list
);
6340 EXPECT_FALSE(service()->GetExtensionById(good_crx
, true));
6342 // Install the extension.
6343 base::FilePath extension_path
= data_dir().AppendASCII("good.crx");
6344 InstallCRX(extension_path
, INSTALL_NEW
);
6345 EXPECT_TRUE(service()->GetExtensionById(good_crx
, true));
6347 // Should uninstall the extension.
6348 extension_sync_service()->ProcessSyncChanges(FROM_HERE
, list
);
6349 EXPECT_FALSE(service()->GetExtensionById(good_crx
, true));
6351 // Should again do nothing.
6352 extension_sync_service()->ProcessSyncChanges(FROM_HERE
, list
);
6353 EXPECT_FALSE(service()->GetExtensionById(good_crx
, true));
6356 TEST_F(ExtensionServiceTest
, ProcessSyncDataWrongType
) {
6357 InitializeEmptyExtensionService();
6358 InitializeExtensionSyncService();
6360 // Install the extension.
6361 base::FilePath extension_path
= data_dir().AppendASCII("good.crx");
6362 InstallCRX(extension_path
, INSTALL_NEW
);
6363 EXPECT_TRUE(service()->GetExtensionById(good_crx
, true));
6365 sync_pb::EntitySpecifics specifics
;
6366 sync_pb::AppSpecifics
* app_specifics
= specifics
.mutable_app();
6367 sync_pb::ExtensionSpecifics
* extension_specifics
=
6368 app_specifics
->mutable_extension();
6369 extension_specifics
->set_id(good_crx
);
6370 extension_specifics
->set_version(
6371 service()->GetInstalledExtension(good_crx
)->version()->GetString());
6374 extension_specifics
->set_enabled(true);
6375 syncer::SyncData sync_data
=
6376 syncer::SyncData::CreateLocalData(good_crx
, "Name", specifics
);
6377 syncer::SyncChange
sync_change(FROM_HERE
,
6378 syncer::SyncChange::ACTION_DELETE
,
6380 syncer::SyncChangeList
list(1);
6381 list
[0] = sync_change
;
6383 // Should do nothing
6384 extension_sync_service()->ProcessSyncChanges(FROM_HERE
, list
);
6385 EXPECT_TRUE(service()->GetExtensionById(good_crx
, true));
6389 extension_specifics
->set_enabled(false);
6390 syncer::SyncData sync_data
=
6391 syncer::SyncData::CreateLocalData(good_crx
, "Name", specifics
);
6392 syncer::SyncChange
sync_change(FROM_HERE
,
6393 syncer::SyncChange::ACTION_UPDATE
,
6395 syncer::SyncChangeList
list(1);
6396 list
[0] = sync_change
;
6398 // Should again do nothing.
6399 extension_sync_service()->ProcessSyncChanges(FROM_HERE
, list
);
6400 EXPECT_TRUE(service()->GetExtensionById(good_crx
, false));
6404 TEST_F(ExtensionServiceTest
, ProcessSyncDataSettings
) {
6405 InitializeEmptyExtensionService();
6406 InitializeExtensionSyncService();
6407 syncer::FakeSyncChangeProcessor processor
;
6408 extension_sync_service()->MergeDataAndStartSyncing(
6410 syncer::SyncDataList(),
6411 scoped_ptr
<syncer::SyncChangeProcessor
>(
6412 new syncer::FakeSyncChangeProcessor
),
6413 scoped_ptr
<syncer::SyncErrorFactory
>(new syncer::SyncErrorFactoryMock()));
6415 InstallCRX(data_dir().AppendASCII("good.crx"), INSTALL_NEW
);
6416 EXPECT_TRUE(service()->IsExtensionEnabled(good_crx
));
6417 EXPECT_FALSE(extensions::util::IsIncognitoEnabled(good_crx
, profile()));
6418 EXPECT_FALSE(extensions::util::HasSetAllowedScriptingOnAllUrls(
6419 good_crx
, profile()));
6420 const bool kDefaultAllowedScripting
=
6421 extensions::util::DefaultAllowedScriptingOnAllUrls();
6422 EXPECT_EQ(kDefaultAllowedScripting
,
6423 extensions::util::AllowedScriptingOnAllUrls(good_crx
, profile()));
6425 sync_pb::EntitySpecifics specifics
;
6426 sync_pb::ExtensionSpecifics
* ext_specifics
= specifics
.mutable_extension();
6427 ext_specifics
->set_id(good_crx
);
6428 ext_specifics
->set_version(
6429 service()->GetInstalledExtension(good_crx
)->version()->GetString());
6430 ext_specifics
->set_enabled(false);
6433 syncer::SyncData sync_data
=
6434 syncer::SyncData::CreateLocalData(good_crx
, "Name", specifics
);
6435 syncer::SyncChange
sync_change(FROM_HERE
,
6436 syncer::SyncChange::ACTION_UPDATE
,
6438 syncer::SyncChangeList
list(1);
6439 list
[0] = sync_change
;
6440 extension_sync_service()->ProcessSyncChanges(FROM_HERE
, list
);
6441 EXPECT_FALSE(service()->IsExtensionEnabled(good_crx
));
6442 EXPECT_FALSE(extensions::util::IsIncognitoEnabled(good_crx
, profile()));
6443 EXPECT_FALSE(extensions::util::HasSetAllowedScriptingOnAllUrls(
6444 good_crx
, profile()));
6445 EXPECT_EQ(kDefaultAllowedScripting
,
6446 extensions::util::AllowedScriptingOnAllUrls(good_crx
, profile()));
6450 ext_specifics
->set_enabled(true);
6451 ext_specifics
->set_incognito_enabled(true);
6452 syncer::SyncData sync_data
=
6453 syncer::SyncData::CreateLocalData(good_crx
, "Name", specifics
);
6454 syncer::SyncChange
sync_change(FROM_HERE
,
6455 syncer::SyncChange::ACTION_UPDATE
,
6457 syncer::SyncChangeList
list(1);
6458 list
[0] = sync_change
;
6459 extension_sync_service()->ProcessSyncChanges(FROM_HERE
, list
);
6460 EXPECT_TRUE(service()->IsExtensionEnabled(good_crx
));
6461 EXPECT_TRUE(extensions::util::IsIncognitoEnabled(good_crx
, profile()));
6465 ext_specifics
->set_enabled(false);
6466 ext_specifics
->set_incognito_enabled(true);
6467 syncer::SyncData sync_data
=
6468 syncer::SyncData::CreateLocalData(good_crx
, "Name", specifics
);
6469 syncer::SyncChange
sync_change(FROM_HERE
,
6470 syncer::SyncChange::ACTION_UPDATE
,
6472 syncer::SyncChangeList
list(1);
6473 list
[0] = sync_change
;
6474 extension_sync_service()->ProcessSyncChanges(FROM_HERE
, list
);
6475 EXPECT_FALSE(service()->IsExtensionEnabled(good_crx
));
6476 EXPECT_TRUE(extensions::util::IsIncognitoEnabled(good_crx
, profile()));
6480 ext_specifics
->set_enabled(true);
6481 ext_specifics
->set_all_urls_enabled(!kDefaultAllowedScripting
);
6482 syncer::SyncData sync_data
=
6483 syncer::SyncData::CreateLocalData(good_crx
, "Name", specifics
);
6484 syncer::SyncChange
sync_change(FROM_HERE
,
6485 syncer::SyncChange::ACTION_UPDATE
,
6487 syncer::SyncChangeList
list(1);
6488 list
[0] = sync_change
;
6489 extension_sync_service()->ProcessSyncChanges(FROM_HERE
, list
);
6490 EXPECT_TRUE(service()->IsExtensionEnabled(good_crx
));
6491 EXPECT_TRUE(extensions::util::HasSetAllowedScriptingOnAllUrls(
6492 good_crx
, profile()));
6493 EXPECT_EQ(!kDefaultAllowedScripting
,
6494 extensions::util::AllowedScriptingOnAllUrls(good_crx
, profile()));
6498 ext_specifics
->set_all_urls_enabled(kDefaultAllowedScripting
);
6499 syncer::SyncData sync_data
=
6500 syncer::SyncData::CreateLocalData(good_crx
, "Name", specifics
);
6501 syncer::SyncChange
sync_change(FROM_HERE
,
6502 syncer::SyncChange::ACTION_UPDATE
,
6504 syncer::SyncChangeList
list(1);
6505 list
[0] = sync_change
;
6506 extension_sync_service()->ProcessSyncChanges(FROM_HERE
, list
);
6507 EXPECT_TRUE(service()->IsExtensionEnabled(good_crx
));
6508 EXPECT_TRUE(extensions::util::HasSetAllowedScriptingOnAllUrls(
6509 good_crx
, profile()));
6510 EXPECT_EQ(kDefaultAllowedScripting
,
6511 extensions::util::AllowedScriptingOnAllUrls(good_crx
, profile()));
6514 EXPECT_FALSE(service()->pending_extension_manager()->IsIdPending(good_crx
));
6517 TEST_F(ExtensionServiceTest
, ProcessSyncDataTerminatedExtension
) {
6518 InitializeExtensionServiceWithUpdater();
6519 InitializeExtensionSyncService();
6520 syncer::FakeSyncChangeProcessor processor
;
6521 extension_sync_service()->MergeDataAndStartSyncing(
6523 syncer::SyncDataList(),
6524 scoped_ptr
<syncer::SyncChangeProcessor
>(
6525 new syncer::FakeSyncChangeProcessor
),
6526 scoped_ptr
<syncer::SyncErrorFactory
>(new syncer::SyncErrorFactoryMock()));
6528 InstallCRX(data_dir().AppendASCII("good.crx"), INSTALL_NEW
);
6529 TerminateExtension(good_crx
);
6530 EXPECT_TRUE(service()->IsExtensionEnabled(good_crx
));
6531 EXPECT_FALSE(extensions::util::IsIncognitoEnabled(good_crx
, profile()));
6533 sync_pb::EntitySpecifics specifics
;
6534 sync_pb::ExtensionSpecifics
* ext_specifics
= specifics
.mutable_extension();
6535 ext_specifics
->set_id(good_crx
);
6536 ext_specifics
->set_version(
6537 service()->GetInstalledExtension(good_crx
)->version()->GetString());
6538 ext_specifics
->set_enabled(false);
6539 ext_specifics
->set_incognito_enabled(true);
6540 syncer::SyncData sync_data
=
6541 syncer::SyncData::CreateLocalData(good_crx
, "Name", specifics
);
6542 syncer::SyncChange
sync_change(FROM_HERE
,
6543 syncer::SyncChange::ACTION_UPDATE
,
6545 syncer::SyncChangeList
list(1);
6546 list
[0] = sync_change
;
6548 extension_sync_service()->ProcessSyncChanges(FROM_HERE
, list
);
6549 EXPECT_FALSE(service()->IsExtensionEnabled(good_crx
));
6550 EXPECT_TRUE(extensions::util::IsIncognitoEnabled(good_crx
, profile()));
6552 EXPECT_FALSE(service()->pending_extension_manager()->IsIdPending(good_crx
));
6555 TEST_F(ExtensionServiceTest
, ProcessSyncDataVersionCheck
) {
6556 InitializeExtensionServiceWithUpdater();
6557 InitializeExtensionSyncService();
6558 syncer::FakeSyncChangeProcessor processor
;
6559 extension_sync_service()->MergeDataAndStartSyncing(
6561 syncer::SyncDataList(),
6562 scoped_ptr
<syncer::SyncChangeProcessor
>(
6563 new syncer::FakeSyncChangeProcessor
),
6564 scoped_ptr
<syncer::SyncErrorFactory
>(new syncer::SyncErrorFactoryMock()));
6566 InstallCRX(data_dir().AppendASCII("good.crx"), INSTALL_NEW
);
6567 EXPECT_TRUE(service()->IsExtensionEnabled(good_crx
));
6568 EXPECT_FALSE(extensions::util::IsIncognitoEnabled(good_crx
, profile()));
6570 sync_pb::EntitySpecifics specifics
;
6571 sync_pb::ExtensionSpecifics
* ext_specifics
= specifics
.mutable_extension();
6572 ext_specifics
->set_id(good_crx
);
6573 ext_specifics
->set_enabled(true);
6576 ext_specifics
->set_version(
6577 service()->GetInstalledExtension(good_crx
)->version()->GetString());
6578 syncer::SyncData sync_data
=
6579 syncer::SyncData::CreateLocalData(good_crx
, "Name", specifics
);
6580 syncer::SyncChange
sync_change(FROM_HERE
,
6581 syncer::SyncChange::ACTION_UPDATE
,
6583 syncer::SyncChangeList
list(1);
6584 list
[0] = sync_change
;
6586 // Should do nothing if extension version == sync version.
6587 extension_sync_service()->ProcessSyncChanges(FROM_HERE
, list
);
6588 EXPECT_FALSE(service()->updater()->WillCheckSoon());
6591 // Should do nothing if extension version > sync version (but see
6592 // the TODO in ProcessExtensionSyncData).
6594 ext_specifics
->set_version("0.0.0.0");
6595 syncer::SyncData sync_data
=
6596 syncer::SyncData::CreateLocalData(good_crx
, "Name", specifics
);
6597 syncer::SyncChange
sync_change(FROM_HERE
,
6598 syncer::SyncChange::ACTION_UPDATE
,
6600 syncer::SyncChangeList
list(1);
6601 list
[0] = sync_change
;
6603 extension_sync_service()->ProcessSyncChanges(FROM_HERE
, list
);
6604 EXPECT_FALSE(service()->updater()->WillCheckSoon());
6607 // Should kick off an update if extension version < sync version.
6609 ext_specifics
->set_version("9.9.9.9");
6610 syncer::SyncData sync_data
=
6611 syncer::SyncData::CreateLocalData(good_crx
, "Name", specifics
);
6612 syncer::SyncChange
sync_change(FROM_HERE
,
6613 syncer::SyncChange::ACTION_UPDATE
,
6615 syncer::SyncChangeList
list(1);
6616 list
[0] = sync_change
;
6618 extension_sync_service()->ProcessSyncChanges(FROM_HERE
, list
);
6619 EXPECT_TRUE(service()->updater()->WillCheckSoon());
6622 EXPECT_FALSE(service()->pending_extension_manager()->IsIdPending(good_crx
));
6625 TEST_F(ExtensionServiceTest
, ProcessSyncDataNotInstalled
) {
6626 InitializeExtensionServiceWithUpdater();
6627 InitializeExtensionSyncService();
6628 syncer::FakeSyncChangeProcessor processor
;
6629 extension_sync_service()->MergeDataAndStartSyncing(
6631 syncer::SyncDataList(),
6632 scoped_ptr
<syncer::SyncChangeProcessor
>(
6633 new syncer::FakeSyncChangeProcessor
),
6634 scoped_ptr
<syncer::SyncErrorFactory
>(new syncer::SyncErrorFactoryMock()));
6636 sync_pb::EntitySpecifics specifics
;
6637 sync_pb::ExtensionSpecifics
* ext_specifics
= specifics
.mutable_extension();
6638 ext_specifics
->set_id(good_crx
);
6639 ext_specifics
->set_enabled(false);
6640 ext_specifics
->set_incognito_enabled(true);
6641 ext_specifics
->set_update_url("http://www.google.com/");
6642 ext_specifics
->set_version("1.2.3.4");
6643 syncer::SyncData sync_data
=
6644 syncer::SyncData::CreateLocalData(good_crx
, "Name", specifics
);
6645 syncer::SyncChange
sync_change(FROM_HERE
,
6646 syncer::SyncChange::ACTION_UPDATE
,
6648 syncer::SyncChangeList
list(1);
6649 list
[0] = sync_change
;
6651 EXPECT_TRUE(service()->IsExtensionEnabled(good_crx
));
6652 EXPECT_FALSE(extensions::util::IsIncognitoEnabled(good_crx
, profile()));
6653 extension_sync_service()->ProcessSyncChanges(FROM_HERE
, list
);
6654 EXPECT_TRUE(service()->updater()->WillCheckSoon());
6655 EXPECT_FALSE(service()->IsExtensionEnabled(good_crx
));
6656 EXPECT_TRUE(extensions::util::IsIncognitoEnabled(good_crx
, profile()));
6658 const extensions::PendingExtensionInfo
* info
;
6660 (info
= service()->pending_extension_manager()->GetById(good_crx
)));
6661 EXPECT_EQ(ext_specifics
->update_url(), info
->update_url().spec());
6662 EXPECT_TRUE(info
->is_from_sync());
6663 EXPECT_EQ(Manifest::INTERNAL
, info
->install_source());
6664 // TODO(akalin): Figure out a way to test |info.ShouldAllowInstall()|.
6667 #if defined(ENABLE_SUPERVISED_USERS)
6668 class ScopedSupervisedUserServiceDelegate
6669 : public SupervisedUserService::Delegate
{
6671 explicit ScopedSupervisedUserServiceDelegate(SupervisedUserService
* service
)
6672 : service_(service
) {
6673 service_
->SetDelegate(this);
6675 ~ScopedSupervisedUserServiceDelegate() override
{
6676 service_
->SetDelegate(nullptr);
6679 // This prevents the legacy supervised user init code from running.
6680 bool SetActive(bool active
) override
{ return true; }
6683 SupervisedUserService
* service_
;
6686 class MockPermissionRequestCreator
: public PermissionRequestCreator
{
6688 MockPermissionRequestCreator() {}
6689 ~MockPermissionRequestCreator() override
{}
6691 bool IsEnabled() const override
{ return true; }
6693 void CreateURLAccessRequest(const GURL
& url_requested
,
6694 const SuccessCallback
& callback
) override
{
6698 MOCK_METHOD2(CreateExtensionUpdateRequest
,
6699 void(const std::string
& id
,
6700 const SupervisedUserService::SuccessCallback
& callback
));
6703 DISALLOW_COPY_AND_ASSIGN(MockPermissionRequestCreator
);
6706 TEST_F(ExtensionServiceTest
, SupervisedUser_InstallOnlyAllowedByCustodian
) {
6707 ExtensionServiceInitParams params
= CreateDefaultInitParams();
6708 params
.profile_is_supervised
= true;
6709 InitializeExtensionService(params
);
6711 SupervisedUserService
* supervised_user_service
=
6712 SupervisedUserServiceFactory::GetForProfile(profile());
6713 ScopedSupervisedUserServiceDelegate
delegate(supervised_user_service
);
6714 supervised_user_service
->Init();
6716 base::FilePath path1
= data_dir().AppendASCII("good.crx");
6717 base::FilePath path2
= data_dir().AppendASCII("good2048.crx");
6718 const Extension
* extensions
[] = {
6719 InstallCRX(path1
, INSTALL_FAILED
),
6720 InstallCRX(path2
, INSTALL_NEW
, Extension::WAS_INSTALLED_BY_CUSTODIAN
)
6723 // Only the extension with the "installed by custodian" flag should have been
6724 // installed and enabled.
6725 EXPECT_FALSE(extensions
[0]);
6726 ASSERT_TRUE(extensions
[1]);
6727 EXPECT_TRUE(registry()->enabled_extensions().Contains(extensions
[1]->id()));
6730 TEST_F(ExtensionServiceTest
, SupervisedUser_PreinstalledExtension
) {
6731 ExtensionServiceInitParams params
= CreateDefaultInitParams();
6732 // Do *not* set the profile to supervised here!
6733 InitializeExtensionService(params
);
6735 SupervisedUserService
* supervised_user_service
=
6736 SupervisedUserServiceFactory::GetForProfile(profile());
6737 ScopedSupervisedUserServiceDelegate
delegate(supervised_user_service
);
6738 supervised_user_service
->Init();
6740 // Install an extension.
6741 base::FilePath path
= data_dir().AppendASCII("good.crx");
6742 const Extension
* extension
= InstallCRX(path
, INSTALL_NEW
);
6743 std::string id
= extension
->id();
6745 // Now make the profile supervised.
6746 profile()->AsTestingProfile()->SetSupervisedUserId(
6747 supervised_users::kChildAccountSUID
);
6749 // The extension should not be enabled anymore.
6750 EXPECT_FALSE(registry()->enabled_extensions().Contains(id
));
6753 TEST_F(ExtensionServiceTest
, SupervisedUser_UpdateWithoutPermissionIncrease
) {
6754 ExtensionServiceInitParams params
= CreateDefaultInitParams();
6755 params
.profile_is_supervised
= true;
6756 InitializeExtensionService(params
);
6758 SupervisedUserService
* supervised_user_service
=
6759 SupervisedUserServiceFactory::GetForProfile(profile());
6760 ScopedSupervisedUserServiceDelegate
delegate(supervised_user_service
);
6761 supervised_user_service
->Init();
6763 base::FilePath base_path
= data_dir().AppendASCII("autoupdate");
6764 base::FilePath pem_path
= base_path
.AppendASCII("key.pem");
6766 base::FilePath path
= base_path
.AppendASCII("v1");
6767 const Extension
* extension
=
6768 PackAndInstallCRX(path
, pem_path
, INSTALL_NEW
,
6769 Extension::WAS_INSTALLED_BY_CUSTODIAN
);
6770 // The extension must now be installed and enabled.
6771 ASSERT_TRUE(extension
);
6772 ASSERT_TRUE(registry()->enabled_extensions().Contains(extension
->id()));
6774 // Save the id, as the extension object will be destroyed during updating.
6775 std::string id
= extension
->id();
6777 std::string old_version
= extension
->VersionString();
6779 // Update to a new version.
6780 path
= base_path
.AppendASCII("v2");
6781 PackCRXAndUpdateExtension(id
, path
, pem_path
, ENABLED
);
6783 // The extension should still be there and enabled.
6784 extension
= registry()->enabled_extensions().GetByID(id
);
6785 ASSERT_TRUE(extension
);
6786 // The version should have changed.
6787 EXPECT_NE(extension
->VersionString(), old_version
);
6790 TEST_F(ExtensionServiceTest
, SupervisedUser_UpdateWithPermissionIncrease
) {
6791 ExtensionServiceInitParams params
= CreateDefaultInitParams();
6792 params
.profile_is_supervised
= true;
6793 InitializeExtensionService(params
);
6795 SupervisedUserService
* supervised_user_service
=
6796 SupervisedUserServiceFactory::GetForProfile(profile());
6797 ScopedSupervisedUserServiceDelegate
delegate(supervised_user_service
);
6798 supervised_user_service
->Init();
6799 MockPermissionRequestCreator
* creator
= new MockPermissionRequestCreator
;
6800 supervised_user_service
->AddPermissionRequestCreator(
6801 make_scoped_ptr(creator
));
6803 base::FilePath base_path
= data_dir().AppendASCII("permissions_increase");
6804 base::FilePath pem_path
= base_path
.AppendASCII("permissions.pem");
6806 base::FilePath path
= base_path
.AppendASCII("v1");
6807 const Extension
* extension
=
6808 PackAndInstallCRX(path
, pem_path
, INSTALL_NEW
,
6809 Extension::WAS_INSTALLED_BY_CUSTODIAN
);
6810 // The extension must now be installed and enabled.
6811 ASSERT_TRUE(extension
);
6812 ASSERT_TRUE(registry()->enabled_extensions().Contains(extension
->id()));
6814 // Save the id, as the extension object will be destroyed during updating.
6815 std::string id
= extension
->id();
6817 std::string old_version
= extension
->VersionString();
6819 // Update to a new version with increased permissions.
6820 EXPECT_CALL(*creator
,
6821 CreateExtensionUpdateRequest(id
+ ":2", testing::_
));
6822 path
= base_path
.AppendASCII("v2");
6823 PackCRXAndUpdateExtension(id
, path
, pem_path
, DISABLED
);
6825 // The extension should still be there, but disabled.
6826 EXPECT_FALSE(registry()->enabled_extensions().Contains(id
));
6827 extension
= registry()->disabled_extensions().GetByID(id
);
6828 ASSERT_TRUE(extension
);
6829 // The version should have changed.
6830 EXPECT_NE(extension
->VersionString(), old_version
);
6833 TEST_F(ExtensionServiceTest
,
6834 SupervisedUser_SyncUninstallByCustodianSkipsPolicy
) {
6835 InitializeEmptyExtensionService();
6836 InitializeExtensionSyncService();
6837 extension_sync_service()->MergeDataAndStartSyncing(
6839 syncer::SyncDataList(),
6840 scoped_ptr
<syncer::SyncChangeProcessor
>(
6841 new syncer::FakeSyncChangeProcessor
),
6842 scoped_ptr
<syncer::SyncErrorFactory
>(new syncer::SyncErrorFactoryMock()));
6844 // Install two extensions.
6845 base::FilePath path1
= data_dir().AppendASCII("good.crx");
6846 base::FilePath path2
= data_dir().AppendASCII("good2048.crx");
6847 const Extension
* extensions
[] = {
6848 InstallCRX(path1
, INSTALL_NEW
),
6849 InstallCRX(path2
, INSTALL_NEW
, Extension::WAS_INSTALLED_BY_CUSTODIAN
)
6852 // Add a policy provider that will disallow any changes.
6853 extensions::TestManagementPolicyProvider
provider(
6854 extensions::TestManagementPolicyProvider::PROHIBIT_MODIFY_STATUS
);
6855 GetManagementPolicy()->RegisterProvider(&provider
);
6857 // Create a sync deletion for each extension.
6858 syncer::SyncChangeList change_list
;
6859 for (size_t i
= 0; i
< arraysize(extensions
); i
++) {
6860 const std::string
& id
= extensions
[i
]->id();
6861 sync_pb::EntitySpecifics specifics
;
6862 sync_pb::ExtensionSpecifics
* ext_specifics
= specifics
.mutable_extension();
6863 ext_specifics
->set_id(id
);
6864 ext_specifics
->set_version("1.0");
6865 ext_specifics
->set_installed_by_custodian(
6866 extensions
[i
]->was_installed_by_custodian());
6867 syncer::SyncData sync_data
=
6868 syncer::SyncData::CreateLocalData(id
, "Name", specifics
);
6869 change_list
.push_back(syncer::SyncChange(FROM_HERE
,
6870 syncer::SyncChange::ACTION_DELETE
,
6874 // Save the extension ids, as uninstalling destroys the Extension instance.
6875 std::string extension_ids
[] = {
6876 extensions
[0]->id(),
6880 // Now apply the uninstallations.
6881 extension_sync_service()->ProcessSyncChanges(FROM_HERE
, change_list
);
6883 // Uninstalling the extension without installed_by_custodian should have been
6884 // blocked by policy, so it should still be there.
6885 EXPECT_TRUE(registry()->enabled_extensions().Contains(extension_ids
[0]));
6887 // But installed_by_custodian should result in bypassing the policy check.
6889 registry()->GenerateInstalledExtensionsSet()->Contains(extension_ids
[1]));
6891 #endif // defined(ENABLE_SUPERVISED_USERS)
6893 TEST_F(ExtensionServiceTest
, InstallPriorityExternalUpdateUrl
) {
6894 InitializeEmptyExtensionService();
6896 base::FilePath path
= data_dir().AppendASCII("good.crx");
6897 InstallCRX(path
, INSTALL_NEW
);
6898 ValidatePrefKeyCount(1u);
6899 ValidateIntegerPref(good_crx
, "state", Extension::ENABLED
);
6900 ValidateIntegerPref(good_crx
, "location", Manifest::INTERNAL
);
6902 extensions::PendingExtensionManager
* pending
=
6903 service()->pending_extension_manager();
6904 EXPECT_FALSE(pending
->IsIdPending(kGoodId
));
6906 // Skip install when the location is the same.
6908 service()->OnExternalExtensionUpdateUrlFound(kGoodId
,
6910 GURL(kGoodUpdateURL
),
6912 Extension::NO_FLAGS
,
6914 EXPECT_FALSE(pending
->IsIdPending(kGoodId
));
6916 // Install when the location has higher priority.
6917 EXPECT_TRUE(service()->OnExternalExtensionUpdateUrlFound(
6920 GURL(kGoodUpdateURL
),
6921 Manifest::EXTERNAL_POLICY_DOWNLOAD
,
6922 Extension::NO_FLAGS
,
6924 EXPECT_TRUE(pending
->IsIdPending(kGoodId
));
6926 // Try the low priority again. Should be rejected.
6927 EXPECT_FALSE(service()->OnExternalExtensionUpdateUrlFound(
6930 GURL(kGoodUpdateURL
),
6931 Manifest::EXTERNAL_PREF_DOWNLOAD
,
6932 Extension::NO_FLAGS
,
6934 // The existing record should still be present in the pending extension
6936 EXPECT_TRUE(pending
->IsIdPending(kGoodId
));
6938 pending
->Remove(kGoodId
);
6940 // Skip install when the location has the same priority as the installed
6943 service()->OnExternalExtensionUpdateUrlFound(kGoodId
,
6945 GURL(kGoodUpdateURL
),
6947 Extension::NO_FLAGS
,
6950 EXPECT_FALSE(pending
->IsIdPending(kGoodId
));
6953 TEST_F(ExtensionServiceTest
, InstallPriorityExternalLocalFile
) {
6954 Version
older_version("0.1.0.0");
6955 Version
newer_version("2.0.0.0");
6957 // We don't want the extension to be installed. A path that doesn't
6958 // point to a valid CRX ensures this.
6959 const base::FilePath
kInvalidPathToCrx(FILE_PATH_LITERAL("invalid_path"));
6961 const int kCreationFlags
= 0;
6962 const bool kDontMarkAcknowledged
= false;
6963 const bool kDontInstallImmediately
= false;
6965 InitializeEmptyExtensionService();
6967 // The test below uses install source constants to test that
6968 // priority is enforced. It assumes a specific ranking of install
6969 // sources: Registry (EXTERNAL_REGISTRY) overrides external pref
6970 // (EXTERNAL_PREF), and external pref overrides user install (INTERNAL).
6971 // The following assertions verify these assumptions:
6972 ASSERT_EQ(Manifest::EXTERNAL_REGISTRY
,
6973 Manifest::GetHigherPriorityLocation(Manifest::EXTERNAL_REGISTRY
,
6974 Manifest::EXTERNAL_PREF
));
6975 ASSERT_EQ(Manifest::EXTERNAL_REGISTRY
,
6976 Manifest::GetHigherPriorityLocation(Manifest::EXTERNAL_REGISTRY
,
6977 Manifest::INTERNAL
));
6978 ASSERT_EQ(Manifest::EXTERNAL_PREF
,
6979 Manifest::GetHigherPriorityLocation(Manifest::EXTERNAL_PREF
,
6980 Manifest::INTERNAL
));
6982 extensions::PendingExtensionManager
* pending
=
6983 service()->pending_extension_manager();
6984 EXPECT_FALSE(pending
->IsIdPending(kGoodId
));
6987 // Simulate an external source adding the extension as INTERNAL.
6988 content::WindowedNotificationObserver
observer(
6989 extensions::NOTIFICATION_CRX_INSTALLER_DONE
,
6990 content::NotificationService::AllSources());
6991 EXPECT_TRUE(service()->OnExternalExtensionFileFound(
6997 kDontMarkAcknowledged
,
6998 kDontInstallImmediately
));
6999 EXPECT_TRUE(pending
->IsIdPending(kGoodId
));
7001 VerifyCrxInstall(kInvalidPathToCrx
, INSTALL_FAILED
);
7005 // Simulate an external source adding the extension as EXTERNAL_PREF.
7006 content::WindowedNotificationObserver
observer(
7007 extensions::NOTIFICATION_CRX_INSTALLER_DONE
,
7008 content::NotificationService::AllSources());
7009 EXPECT_TRUE(service()->OnExternalExtensionFileFound(
7013 Manifest::EXTERNAL_PREF
,
7015 kDontMarkAcknowledged
,
7016 kDontInstallImmediately
));
7017 EXPECT_TRUE(pending
->IsIdPending(kGoodId
));
7019 VerifyCrxInstall(kInvalidPathToCrx
, INSTALL_FAILED
);
7022 // Simulate an external source adding as EXTERNAL_PREF again.
7023 // This is rejected because the version and the location are the same as
7024 // the previous installation, which is still pending.
7025 EXPECT_FALSE(service()->OnExternalExtensionFileFound(
7029 Manifest::EXTERNAL_PREF
,
7031 kDontMarkAcknowledged
,
7032 kDontInstallImmediately
));
7033 EXPECT_TRUE(pending
->IsIdPending(kGoodId
));
7035 // Try INTERNAL again. Should fail.
7036 EXPECT_FALSE(service()->OnExternalExtensionFileFound(
7042 kDontMarkAcknowledged
,
7043 kDontInstallImmediately
));
7044 EXPECT_TRUE(pending
->IsIdPending(kGoodId
));
7047 // Now the registry adds the extension.
7048 content::WindowedNotificationObserver
observer(
7049 extensions::NOTIFICATION_CRX_INSTALLER_DONE
,
7050 content::NotificationService::AllSources());
7051 EXPECT_TRUE(service()->OnExternalExtensionFileFound(
7055 Manifest::EXTERNAL_REGISTRY
,
7057 kDontMarkAcknowledged
,
7058 kDontInstallImmediately
));
7059 EXPECT_TRUE(pending
->IsIdPending(kGoodId
));
7061 VerifyCrxInstall(kInvalidPathToCrx
, INSTALL_FAILED
);
7064 // Registry outranks both external pref and internal, so both fail.
7065 EXPECT_FALSE(service()->OnExternalExtensionFileFound(
7069 Manifest::EXTERNAL_PREF
,
7071 kDontMarkAcknowledged
,
7072 kDontInstallImmediately
));
7073 EXPECT_TRUE(pending
->IsIdPending(kGoodId
));
7075 EXPECT_FALSE(service()->OnExternalExtensionFileFound(
7081 kDontMarkAcknowledged
,
7082 kDontInstallImmediately
));
7083 EXPECT_TRUE(pending
->IsIdPending(kGoodId
));
7085 pending
->Remove(kGoodId
);
7087 // Install the extension.
7088 base::FilePath path
= data_dir().AppendASCII("good.crx");
7089 const Extension
* ext
= InstallCRX(path
, INSTALL_NEW
);
7090 ValidatePrefKeyCount(1u);
7091 ValidateIntegerPref(good_crx
, "state", Extension::ENABLED
);
7092 ValidateIntegerPref(good_crx
, "location", Manifest::INTERNAL
);
7094 // Now test the logic of OnExternalExtensionFileFound() when the extension
7095 // being added is already installed.
7097 // Tests assume |older_version| is less than the installed version, and
7098 // |newer_version| is greater. Verify this:
7099 ASSERT_TRUE(older_version
.IsOlderThan(ext
->VersionString()));
7100 ASSERT_TRUE(ext
->version()->IsOlderThan(newer_version
.GetString()));
7102 // An external install for the same location should fail if the version is
7103 // older, or the same, and succeed if the version is newer.
7105 // Older than the installed version...
7106 EXPECT_FALSE(service()->OnExternalExtensionFileFound(
7112 kDontMarkAcknowledged
,
7113 kDontInstallImmediately
));
7114 EXPECT_FALSE(pending
->IsIdPending(kGoodId
));
7116 // Same version as the installed version...
7117 EXPECT_FALSE(service()->OnExternalExtensionFileFound(
7123 kDontMarkAcknowledged
,
7124 kDontInstallImmediately
));
7125 EXPECT_FALSE(pending
->IsIdPending(kGoodId
));
7127 // Newer than the installed version...
7128 EXPECT_TRUE(service()->OnExternalExtensionFileFound(
7134 kDontMarkAcknowledged
,
7135 kDontInstallImmediately
));
7136 EXPECT_TRUE(pending
->IsIdPending(kGoodId
));
7138 // An external install for a higher priority install source should succeed
7139 // if the version is greater. |older_version| is not...
7140 EXPECT_FALSE(service()->OnExternalExtensionFileFound(
7144 Manifest::EXTERNAL_PREF
,
7146 kDontMarkAcknowledged
,
7147 kDontInstallImmediately
));
7148 EXPECT_TRUE(pending
->IsIdPending(kGoodId
));
7150 // |newer_version| is newer.
7151 EXPECT_TRUE(service()->OnExternalExtensionFileFound(
7155 Manifest::EXTERNAL_PREF
,
7157 kDontMarkAcknowledged
,
7158 kDontInstallImmediately
));
7159 EXPECT_TRUE(pending
->IsIdPending(kGoodId
));
7161 // An external install for an even higher priority install source should
7162 // succeed if the version is greater.
7163 EXPECT_TRUE(service()->OnExternalExtensionFileFound(
7167 Manifest::EXTERNAL_REGISTRY
,
7169 kDontMarkAcknowledged
,
7170 kDontInstallImmediately
));
7171 EXPECT_TRUE(pending
->IsIdPending(kGoodId
));
7173 // Because EXTERNAL_PREF is a lower priority source than EXTERNAL_REGISTRY,
7174 // adding from external pref will now fail.
7175 EXPECT_FALSE(service()->OnExternalExtensionFileFound(
7179 Manifest::EXTERNAL_PREF
,
7181 kDontMarkAcknowledged
,
7182 kDontInstallImmediately
));
7183 EXPECT_TRUE(pending
->IsIdPending(kGoodId
));
7186 TEST_F(ExtensionServiceTest
, ConcurrentExternalLocalFile
) {
7187 Version
kVersion123("1.2.3");
7188 Version
kVersion124("1.2.4");
7189 Version
kVersion125("1.2.5");
7190 const base::FilePath
kInvalidPathToCrx(FILE_PATH_LITERAL("invalid_path"));
7191 const int kCreationFlags
= 0;
7192 const bool kDontMarkAcknowledged
= false;
7193 const bool kDontInstallImmediately
= false;
7195 InitializeEmptyExtensionService();
7197 extensions::PendingExtensionManager
* pending
=
7198 service()->pending_extension_manager();
7199 EXPECT_FALSE(pending
->IsIdPending(kGoodId
));
7201 // An external provider starts installing from a local crx.
7202 EXPECT_TRUE(service()->OnExternalExtensionFileFound(
7206 Manifest::EXTERNAL_PREF
,
7208 kDontMarkAcknowledged
,
7209 kDontInstallImmediately
));
7210 const extensions::PendingExtensionInfo
* info
;
7211 EXPECT_TRUE((info
= pending
->GetById(kGoodId
)));
7212 EXPECT_TRUE(info
->version().IsValid());
7213 EXPECT_TRUE(info
->version().Equals(kVersion123
));
7215 // Adding a newer version overrides the currently pending version.
7216 EXPECT_TRUE(service()->OnExternalExtensionFileFound(
7220 Manifest::EXTERNAL_PREF
,
7222 kDontMarkAcknowledged
,
7223 kDontInstallImmediately
));
7224 EXPECT_TRUE((info
= pending
->GetById(kGoodId
)));
7225 EXPECT_TRUE(info
->version().IsValid());
7226 EXPECT_TRUE(info
->version().Equals(kVersion124
));
7228 // Adding an older version fails.
7229 EXPECT_FALSE(service()->OnExternalExtensionFileFound(
7233 Manifest::EXTERNAL_PREF
,
7235 kDontMarkAcknowledged
,
7236 kDontInstallImmediately
));
7237 EXPECT_TRUE((info
= pending
->GetById(kGoodId
)));
7238 EXPECT_TRUE(info
->version().IsValid());
7239 EXPECT_TRUE(info
->version().Equals(kVersion124
));
7241 // Adding an older version fails even when coming from a higher-priority
7243 EXPECT_FALSE(service()->OnExternalExtensionFileFound(
7247 Manifest::EXTERNAL_REGISTRY
,
7249 kDontMarkAcknowledged
,
7250 kDontInstallImmediately
));
7251 EXPECT_TRUE((info
= pending
->GetById(kGoodId
)));
7252 EXPECT_TRUE(info
->version().IsValid());
7253 EXPECT_TRUE(info
->version().Equals(kVersion124
));
7255 // Adding the latest version from the webstore overrides a specific version.
7256 GURL
kUpdateUrl("http://example.com/update");
7257 EXPECT_TRUE(service()->OnExternalExtensionUpdateUrlFound(
7261 Manifest::EXTERNAL_POLICY_DOWNLOAD
,
7262 Extension::NO_FLAGS
,
7264 EXPECT_TRUE((info
= pending
->GetById(kGoodId
)));
7265 EXPECT_FALSE(info
->version().IsValid());
7268 // This makes sure we can package and install CRX files that use whitelisted
7270 TEST_F(ExtensionServiceTest
, InstallWhitelistedExtension
) {
7271 std::string test_id
= "hdkklepkcpckhnpgjnmbdfhehckloojk";
7272 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
7273 extensions::switches::kWhitelistedExtensionID
, test_id
);
7275 InitializeEmptyExtensionService();
7276 base::FilePath path
= data_dir().AppendASCII("permissions");
7277 base::FilePath pem_path
= path
7278 .AppendASCII("whitelist.pem");
7280 .AppendASCII("whitelist");
7282 const Extension
* extension
= PackAndInstallCRX(path
, pem_path
, INSTALL_NEW
);
7283 EXPECT_EQ(0u, GetErrors().size());
7284 ASSERT_EQ(1u, registry()->enabled_extensions().size());
7285 EXPECT_EQ(test_id
, extension
->id());
7288 // Test that when multiple sources try to install an extension,
7289 // we consistently choose the right one. To make tests easy to read,
7290 // methods that fake requests to install crx files in several ways
7292 class ExtensionSourcePriorityTest
: public ExtensionServiceTest
{
7294 void SetUp() override
{
7295 ExtensionServiceTest::SetUp();
7297 // All tests use a single extension. Put the id and path in member vars
7298 // that all methods can read.
7300 crx_path_
= data_dir().AppendASCII("good.crx");
7303 // Fake an external source adding a URL to fetch an extension from.
7304 bool AddPendingExternalPrefUrl() {
7305 return service()->pending_extension_manager()->AddFromExternalUpdateUrl(
7309 Manifest::EXTERNAL_PREF_DOWNLOAD
,
7310 Extension::NO_FLAGS
,
7314 // Fake an external file from external_extensions.json.
7315 bool AddPendingExternalPrefFileInstall() {
7316 Version
version("1.0.0.0");
7318 return service()->OnExternalExtensionFileFound(crx_id_
,
7321 Manifest::EXTERNAL_PREF
,
7322 Extension::NO_FLAGS
,
7327 // Fake a request from sync to install an extension.
7328 bool AddPendingSyncInstall() {
7329 return service()->pending_extension_manager()->AddFromSync(
7331 GURL(kGoodUpdateURL
),
7334 kGoodInstalledByCustodian
);
7337 // Fake a policy install.
7338 bool AddPendingPolicyInstall() {
7339 // Get path to the CRX with id |kGoodId|.
7340 return service()->OnExternalExtensionUpdateUrlFound(
7344 Manifest::EXTERNAL_POLICY_DOWNLOAD
,
7345 Extension::NO_FLAGS
,
7349 // Get the install source of a pending extension.
7350 Manifest::Location
GetPendingLocation() {
7351 const extensions::PendingExtensionInfo
* info
;
7353 (info
= service()->pending_extension_manager()->GetById(crx_id_
)));
7354 return info
->install_source();
7357 // Is an extension pending from a sync request?
7358 bool GetPendingIsFromSync() {
7359 const extensions::PendingExtensionInfo
* info
;
7361 (info
= service()->pending_extension_manager()->GetById(crx_id_
)));
7362 return info
->is_from_sync();
7365 // Is the CRX id these tests use pending?
7366 bool IsCrxPending() {
7367 return service()->pending_extension_manager()->IsIdPending(crx_id_
);
7370 // Is an extension installed?
7371 bool IsCrxInstalled() {
7372 return (service()->GetExtensionById(crx_id_
, true) != NULL
);
7376 // All tests use a single extension. Making the id and path member
7377 // vars avoids pasing the same argument to every method.
7378 std::string crx_id_
;
7379 base::FilePath crx_path_
;
7382 // Test that a pending request for installation of an external CRX from
7383 // an update URL overrides a pending request to install the same extension
7385 TEST_F(ExtensionSourcePriorityTest
, PendingExternalFileOverSync
) {
7386 InitializeEmptyExtensionService();
7388 ASSERT_FALSE(IsCrxInstalled());
7390 // Install pending extension from sync.
7391 content::WindowedNotificationObserver
observer(
7392 extensions::NOTIFICATION_CRX_INSTALLER_DONE
,
7393 content::NotificationService::AllSources());
7394 EXPECT_TRUE(AddPendingSyncInstall());
7395 ASSERT_EQ(Manifest::INTERNAL
, GetPendingLocation());
7396 EXPECT_TRUE(GetPendingIsFromSync());
7397 ASSERT_FALSE(IsCrxInstalled());
7399 // Install pending as external prefs json would.
7400 AddPendingExternalPrefFileInstall();
7401 ASSERT_EQ(Manifest::EXTERNAL_PREF
, GetPendingLocation());
7402 ASSERT_FALSE(IsCrxInstalled());
7404 // Another request from sync should be ignored.
7405 EXPECT_FALSE(AddPendingSyncInstall());
7406 ASSERT_EQ(Manifest::EXTERNAL_PREF
, GetPendingLocation());
7407 ASSERT_FALSE(IsCrxInstalled());
7410 VerifyCrxInstall(crx_path_
, INSTALL_NEW
);
7411 ASSERT_TRUE(IsCrxInstalled());
7414 // Test that an install of an external CRX from an update overrides
7415 // an install of the same extension from sync.
7416 TEST_F(ExtensionSourcePriorityTest
, PendingExternalUrlOverSync
) {
7417 InitializeEmptyExtensionService();
7418 ASSERT_FALSE(IsCrxInstalled());
7420 EXPECT_TRUE(AddPendingSyncInstall());
7421 ASSERT_EQ(Manifest::INTERNAL
, GetPendingLocation());
7422 EXPECT_TRUE(GetPendingIsFromSync());
7423 ASSERT_FALSE(IsCrxInstalled());
7425 ASSERT_TRUE(AddPendingExternalPrefUrl());
7426 ASSERT_EQ(Manifest::EXTERNAL_PREF_DOWNLOAD
, GetPendingLocation());
7427 EXPECT_FALSE(GetPendingIsFromSync());
7428 ASSERT_FALSE(IsCrxInstalled());
7430 EXPECT_FALSE(AddPendingSyncInstall());
7431 ASSERT_EQ(Manifest::EXTERNAL_PREF_DOWNLOAD
, GetPendingLocation());
7432 EXPECT_FALSE(GetPendingIsFromSync());
7433 ASSERT_FALSE(IsCrxInstalled());
7436 // Test that an external install request stops sync from installing
7437 // the same extension.
7438 TEST_F(ExtensionSourcePriorityTest
, InstallExternalBlocksSyncRequest
) {
7439 InitializeEmptyExtensionService();
7440 ASSERT_FALSE(IsCrxInstalled());
7442 // External prefs starts an install.
7443 AddPendingExternalPrefFileInstall();
7445 // Crx installer was made, but has not yet run.
7446 ASSERT_FALSE(IsCrxInstalled());
7448 // Before the CRX installer runs, Sync requests that the same extension
7449 // be installed. Should fail, because an external source is pending.
7450 content::WindowedNotificationObserver
observer(
7451 extensions::NOTIFICATION_CRX_INSTALLER_DONE
,
7452 content::NotificationService::AllSources());
7453 ASSERT_FALSE(AddPendingSyncInstall());
7455 // Wait for the external source to install.
7457 VerifyCrxInstall(crx_path_
, INSTALL_NEW
);
7458 ASSERT_TRUE(IsCrxInstalled());
7460 // Now that the extension is installed, sync request should fail
7461 // because the extension is already installed.
7462 ASSERT_FALSE(AddPendingSyncInstall());
7465 // Test that installing an external extension displays a GlobalError.
7466 TEST_F(ExtensionServiceTest
, ExternalInstallGlobalError
) {
7467 FeatureSwitch::ScopedOverride
prompt(
7468 FeatureSwitch::prompt_for_external_extensions(), true);
7470 InitializeEmptyExtensionService();
7471 MockExtensionProvider
* provider
=
7472 new MockExtensionProvider(service(), Manifest::EXTERNAL_PREF
);
7473 AddMockExternalProvider(provider
);
7475 service()->external_install_manager()->UpdateExternalExtensionAlert();
7476 // Should return false, meaning there aren't any extensions that the user
7477 // needs to know about.
7479 service()->external_install_manager()->HasExternalInstallError());
7481 // This is a normal extension, installed normally.
7482 // This should NOT trigger an alert.
7483 service()->set_extensions_enabled(true);
7484 base::FilePath path
= data_dir().AppendASCII("good.crx");
7485 InstallCRX(path
, INSTALL_NEW
);
7487 service()->CheckForExternalUpdates();
7488 base::RunLoop().RunUntilIdle();
7490 service()->external_install_manager()->HasExternalInstallError());
7492 // A hosted app, installed externally.
7493 // This should NOT trigger an alert.
7494 provider
->UpdateOrAddExtension(
7495 hosted_app
, "1.0.0.0", data_dir().AppendASCII("hosted_app.crx"));
7497 content::WindowedNotificationObserver
observer(
7498 extensions::NOTIFICATION_CRX_INSTALLER_DONE
,
7499 content::NotificationService::AllSources());
7500 service()->CheckForExternalUpdates();
7503 service()->external_install_manager()->HasExternalInstallError());
7505 // Another normal extension, but installed externally.
7506 // This SHOULD trigger an alert.
7507 provider
->UpdateOrAddExtension(
7508 page_action
, "1.0.0.0", data_dir().AppendASCII("page_action.crx"));
7510 content::WindowedNotificationObserver
observer2(
7511 extensions::NOTIFICATION_CRX_INSTALLER_DONE
,
7512 content::NotificationService::AllSources());
7513 service()->CheckForExternalUpdates();
7515 EXPECT_TRUE(service()->external_install_manager()->HasExternalInstallError());
7518 // Test that external extensions are initially disabled, and that enabling
7519 // them clears the prompt.
7520 TEST_F(ExtensionServiceTest
, ExternalInstallInitiallyDisabled
) {
7521 FeatureSwitch::ScopedOverride
prompt(
7522 FeatureSwitch::prompt_for_external_extensions(), true);
7524 InitializeEmptyExtensionService();
7525 MockExtensionProvider
* provider
=
7526 new MockExtensionProvider(service(), Manifest::EXTERNAL_PREF
);
7527 AddMockExternalProvider(provider
);
7529 provider
->UpdateOrAddExtension(
7530 page_action
, "1.0.0.0", data_dir().AppendASCII("page_action.crx"));
7532 content::WindowedNotificationObserver
observer(
7533 extensions::NOTIFICATION_CRX_INSTALLER_DONE
,
7534 content::NotificationService::AllSources());
7535 service()->CheckForExternalUpdates();
7537 EXPECT_TRUE(service()->external_install_manager()->HasExternalInstallError());
7538 EXPECT_FALSE(service()->IsExtensionEnabled(page_action
));
7540 const Extension
* extension
=
7541 registry()->disabled_extensions().GetByID(page_action
);
7542 EXPECT_TRUE(extension
);
7543 EXPECT_EQ(page_action
, extension
->id());
7545 service()->EnableExtension(page_action
);
7547 service()->external_install_manager()->HasExternalInstallError());
7548 EXPECT_TRUE(service()->IsExtensionEnabled(page_action
));
7551 // Test that installing multiple external extensions works.
7552 // Flaky on windows; http://crbug.com/295757 .
7554 #define MAYBE_ExternalInstallMultiple DISABLED_ExternalInstallMultiple
7556 #define MAYBE_ExternalInstallMultiple ExternalInstallMultiple
7558 TEST_F(ExtensionServiceTest
, MAYBE_ExternalInstallMultiple
) {
7559 FeatureSwitch::ScopedOverride
prompt(
7560 FeatureSwitch::prompt_for_external_extensions(), true);
7562 InitializeEmptyExtensionService();
7563 MockExtensionProvider
* provider
=
7564 new MockExtensionProvider(service(), Manifest::EXTERNAL_PREF
);
7565 AddMockExternalProvider(provider
);
7567 provider
->UpdateOrAddExtension(
7568 page_action
, "1.0.0.0", data_dir().AppendASCII("page_action.crx"));
7569 provider
->UpdateOrAddExtension(
7570 good_crx
, "1.0.0.0", data_dir().AppendASCII("good.crx"));
7571 provider
->UpdateOrAddExtension(
7572 theme_crx
, "2.0", data_dir().AppendASCII("theme.crx"));
7575 content::WindowedNotificationObserver
observer(
7576 extensions::NOTIFICATION_CRX_INSTALLER_DONE
,
7577 base::Bind(&WaitForCountNotificationsCallback
, &count
));
7578 service()->CheckForExternalUpdates();
7580 EXPECT_TRUE(service()->external_install_manager()->HasExternalInstallError());
7581 EXPECT_FALSE(service()->IsExtensionEnabled(page_action
));
7582 EXPECT_FALSE(service()->IsExtensionEnabled(good_crx
));
7583 EXPECT_FALSE(service()->IsExtensionEnabled(theme_crx
));
7585 service()->EnableExtension(page_action
);
7586 EXPECT_TRUE(service()->external_install_manager()->HasExternalInstallError());
7587 EXPECT_FALSE(service()
7588 ->external_install_manager()
7589 ->HasExternalInstallBubbleForTesting());
7591 service()->EnableExtension(theme_crx
);
7592 EXPECT_TRUE(service()->external_install_manager()->HasExternalInstallError());
7593 EXPECT_FALSE(service()
7594 ->external_install_manager()
7595 ->HasExternalInstallBubbleForTesting());
7597 service()->EnableExtension(good_crx
);
7599 service()->external_install_manager()->HasExternalInstallError());
7600 EXPECT_FALSE(service()
7601 ->external_install_manager()
7602 ->HasExternalInstallBubbleForTesting());
7605 // Test that there is a bubble for external extensions that update
7606 // from the webstore if the profile is not new.
7607 TEST_F(ExtensionServiceTest
, ExternalInstallUpdatesFromWebstoreOldProfile
) {
7608 FeatureSwitch::ScopedOverride
prompt(
7609 FeatureSwitch::prompt_for_external_extensions(), true);
7611 // This sets up the ExtensionPrefs used by our ExtensionService to be
7613 ExtensionServiceInitParams params
= CreateDefaultInitParams();
7614 params
.is_first_run
= false;
7615 InitializeExtensionService(params
);
7617 base::FilePath crx_path
= temp_dir().path().AppendASCII("webstore.crx");
7618 PackCRX(data_dir().AppendASCII("update_from_webstore"),
7619 data_dir().AppendASCII("update_from_webstore.pem"),
7622 MockExtensionProvider
* provider
=
7623 new MockExtensionProvider(service(), Manifest::EXTERNAL_PREF
);
7624 AddMockExternalProvider(provider
);
7625 provider
->UpdateOrAddExtension(updates_from_webstore
, "1", crx_path
);
7627 content::WindowedNotificationObserver
observer(
7628 extensions::NOTIFICATION_CRX_INSTALLER_DONE
,
7629 content::NotificationService::AllSources());
7630 service()->CheckForExternalUpdates();
7632 EXPECT_TRUE(service()->external_install_manager()->HasExternalInstallError());
7633 EXPECT_TRUE(service()
7634 ->external_install_manager()
7635 ->HasExternalInstallBubbleForTesting());
7636 EXPECT_FALSE(service()->IsExtensionEnabled(updates_from_webstore
));
7639 // Test that there is no bubble for external extensions if the profile is new.
7640 TEST_F(ExtensionServiceTest
, ExternalInstallUpdatesFromWebstoreNewProfile
) {
7641 FeatureSwitch::ScopedOverride
prompt(
7642 FeatureSwitch::prompt_for_external_extensions(), true);
7644 InitializeEmptyExtensionService();
7646 base::FilePath crx_path
= temp_dir().path().AppendASCII("webstore.crx");
7647 PackCRX(data_dir().AppendASCII("update_from_webstore"),
7648 data_dir().AppendASCII("update_from_webstore.pem"),
7651 MockExtensionProvider
* provider
=
7652 new MockExtensionProvider(service(), Manifest::EXTERNAL_PREF
);
7653 AddMockExternalProvider(provider
);
7654 provider
->UpdateOrAddExtension(updates_from_webstore
, "1", crx_path
);
7656 content::WindowedNotificationObserver
observer(
7657 extensions::NOTIFICATION_CRX_INSTALLER_DONE
,
7658 content::NotificationService::AllSources());
7659 service()->CheckForExternalUpdates();
7661 EXPECT_TRUE(service()->external_install_manager()->HasExternalInstallError());
7662 EXPECT_FALSE(service()
7663 ->external_install_manager()
7664 ->HasExternalInstallBubbleForTesting());
7665 EXPECT_FALSE(service()->IsExtensionEnabled(updates_from_webstore
));
7668 // Test that clicking to remove the extension on an external install warning
7669 // uninstalls the extension.
7670 TEST_F(ExtensionServiceTest
, ExternalInstallClickToRemove
) {
7671 FeatureSwitch::ScopedOverride
prompt(
7672 FeatureSwitch::prompt_for_external_extensions(), true);
7674 ExtensionServiceInitParams params
= CreateDefaultInitParams();
7675 params
.is_first_run
= false;
7676 InitializeExtensionService(params
);
7678 base::FilePath crx_path
= temp_dir().path().AppendASCII("webstore.crx");
7679 PackCRX(data_dir().AppendASCII("update_from_webstore"),
7680 data_dir().AppendASCII("update_from_webstore.pem"),
7683 MockExtensionProvider
* provider
=
7684 new MockExtensionProvider(service_
, Manifest::EXTERNAL_PREF
);
7685 AddMockExternalProvider(provider
);
7686 provider
->UpdateOrAddExtension(updates_from_webstore
, "1", crx_path
);
7688 content::WindowedNotificationObserver
observer(
7689 extensions::NOTIFICATION_CRX_INSTALLER_DONE
,
7690 content::NotificationService::AllSources());
7691 service_
->CheckForExternalUpdates();
7693 EXPECT_TRUE(service_
->external_install_manager()->HasExternalInstallError());
7695 // We check both enabled and disabled, since these are "eventually exclusive"
7697 EXPECT_TRUE(registry()->disabled_extensions().GetByID(updates_from_webstore
));
7698 EXPECT_FALSE(registry()->enabled_extensions().GetByID(updates_from_webstore
));
7700 // Click the negative response.
7701 service_
->external_install_manager()->error_for_testing()->InstallUIAbort(
7703 // The Extension should be uninstalled.
7704 EXPECT_FALSE(registry()->GetExtensionById(updates_from_webstore
,
7705 ExtensionRegistry::EVERYTHING
));
7706 // The error should be removed.
7707 EXPECT_FALSE(service_
->external_install_manager()->HasExternalInstallError());
7710 // Test that clicking to keep the extension on an external install warning
7711 // re-enables the extension.
7712 TEST_F(ExtensionServiceTest
, ExternalInstallClickToKeep
) {
7713 FeatureSwitch::ScopedOverride
prompt(
7714 FeatureSwitch::prompt_for_external_extensions(), true);
7716 ExtensionServiceInitParams params
= CreateDefaultInitParams();
7717 params
.is_first_run
= false;
7718 InitializeExtensionService(params
);
7720 base::FilePath crx_path
= temp_dir().path().AppendASCII("webstore.crx");
7721 PackCRX(data_dir().AppendASCII("update_from_webstore"),
7722 data_dir().AppendASCII("update_from_webstore.pem"),
7725 MockExtensionProvider
* provider
=
7726 new MockExtensionProvider(service_
, Manifest::EXTERNAL_PREF
);
7727 AddMockExternalProvider(provider
);
7728 provider
->UpdateOrAddExtension(updates_from_webstore
, "1", crx_path
);
7730 content::WindowedNotificationObserver
observer(
7731 extensions::NOTIFICATION_CRX_INSTALLER_DONE
,
7732 content::NotificationService::AllSources());
7733 service_
->CheckForExternalUpdates();
7735 EXPECT_TRUE(service_
->external_install_manager()->HasExternalInstallError());
7737 // We check both enabled and disabled, since these are "eventually exclusive"
7739 EXPECT_TRUE(registry()->disabled_extensions().GetByID(updates_from_webstore
));
7740 EXPECT_FALSE(registry()->enabled_extensions().GetByID(updates_from_webstore
));
7742 // Accept the extension.
7743 service_
->external_install_manager()->error_for_testing()->InstallUIProceed();
7745 // It should be enabled again.
7746 EXPECT_TRUE(registry()->enabled_extensions().GetByID(updates_from_webstore
));
7748 registry()->disabled_extensions().GetByID(updates_from_webstore
));
7750 // The error should be removed.
7751 EXPECT_FALSE(service_
->external_install_manager()->HasExternalInstallError());
7754 TEST_F(ExtensionServiceTest
, InstallBlacklistedExtension
) {
7755 InitializeEmptyExtensionService();
7757 scoped_refptr
<Extension
> extension
= extensions::ExtensionBuilder()
7758 .SetManifest(extensions::DictionaryBuilder()
7759 .Set("name", "extension")
7760 .Set("version", "1.0")
7761 .Set("manifest_version", 2).Build())
7763 ASSERT_TRUE(extension
.get());
7764 const std::string
& id
= extension
->id();
7766 std::set
<std::string
> id_set
;
7768 extensions::ExtensionNotificationObserver
notifications(
7769 content::NotificationService::AllSources(), id_set
);
7771 // Installation should be allowed but the extension should never have been
7772 // loaded and it should be blacklisted in prefs.
7773 service()->OnExtensionInstalled(
7775 syncer::StringOrdinal(),
7776 (extensions::kInstallFlagIsBlacklistedForMalware
|
7777 extensions::kInstallFlagInstallImmediately
));
7778 base::RunLoop().RunUntilIdle();
7780 // Extension was installed but not loaded.
7781 EXPECT_TRUE(notifications
.CheckNotifications(
7782 extensions::NOTIFICATION_EXTENSION_WILL_BE_INSTALLED_DEPRECATED
));
7783 EXPECT_TRUE(service()->GetInstalledExtension(id
));
7785 EXPECT_FALSE(registry()->enabled_extensions().Contains(id
));
7786 EXPECT_TRUE(registry()->blacklisted_extensions().Contains(id
));
7788 EXPECT_TRUE(ExtensionPrefs::Get(profile())->IsExtensionBlacklisted(id
));
7790 ExtensionPrefs::Get(profile())->IsBlacklistedExtensionAcknowledged(id
));
7793 // Tests a profile being destroyed correctly disables extensions.
7794 TEST_F(ExtensionServiceTest
, DestroyingProfileClearsExtensions
) {
7795 InitializeEmptyExtensionService();
7797 InstallCRX(data_dir().AppendASCII("good.crx"), INSTALL_NEW
);
7798 EXPECT_NE(UnloadedExtensionInfo::REASON_PROFILE_SHUTDOWN
, unloaded_reason_
);
7799 EXPECT_EQ(1u, registry()->enabled_extensions().size());
7800 EXPECT_EQ(0u, registry()->disabled_extensions().size());
7801 EXPECT_EQ(0u, registry()->terminated_extensions().size());
7802 EXPECT_EQ(0u, registry()->blacklisted_extensions().size());
7804 service()->Observe(chrome::NOTIFICATION_PROFILE_DESTRUCTION_STARTED
,
7805 content::Source
<Profile
>(profile()),
7806 content::NotificationService::NoDetails());
7807 EXPECT_EQ(UnloadedExtensionInfo::REASON_PROFILE_SHUTDOWN
, unloaded_reason_
);
7808 EXPECT_EQ(0u, registry()->enabled_extensions().size());
7809 EXPECT_EQ(0u, registry()->disabled_extensions().size());
7810 EXPECT_EQ(0u, registry()->terminated_extensions().size());
7811 EXPECT_EQ(0u, registry()->blacklisted_extensions().size());