blob: 943aae5787f717a8168024007b1f24487e4d440b [file] [log] [blame]
[email protected]098fa7a2013-03-08 22:11:171// Copyright (c) 2013 The Chromium Authors. All rights reserved.
[email protected]6014d672008-12-05 00:38:252// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
dcheng1fc00f12015-12-26 22:18:035#include "chrome/browser/extensions/extension_service.h"
6
avia2f4804a2015-12-24 23:11:137#include <stddef.h>
8#include <stdint.h>
dchengc963c7142016-04-08 03:55:229
[email protected]f0397fa2008-12-11 17:59:5810#include <algorithm>
treib9afc6212015-10-30 18:49:5811#include <map>
dchengc963c7142016-04-08 03:55:2212#include <memory>
[email protected]7fa19f82010-12-21 19:40:0813#include <set>
treib9afc6212015-10-30 18:49:5814#include <string>
dcheng1fc00f12015-12-26 22:18:0315#include <utility>
[email protected]6014d672008-12-05 00:38:2516#include <vector>
17
[email protected]be5a6db2012-11-13 14:39:1118#include "base/at_exit.h"
[email protected]c4148a72011-08-09 23:04:2019#include "base/bind.h"
[email protected]36a784c2009-06-23 06:21:0820#include "base/command_line.h"
[email protected]25a4c1c2013-06-08 04:53:3621#include "base/files/file_enumerator.h"
thestig18dfb7a52014-08-26 10:44:0422#include "base/files/file_util.h"
[email protected]ea1a3f62012-11-16 20:34:2323#include "base/files/scoped_temp_dir.h"
[email protected]ffbec692012-02-26 20:26:4224#include "base/json/json_file_value_serializer.h"
[email protected]93d49d72009-10-23 20:00:2025#include "base/json/json_reader.h"
[email protected]ffbec692012-02-26 20:26:4226#include "base/json/json_string_value_serializer.h"
skyostilf221b7de2015-06-11 20:36:3227#include "base/location.h"
avia2f4804a2015-12-24 23:11:1328#include "base/macros.h"
dchengc963c7142016-04-08 03:55:2229#include "base/memory/ptr_util.h"
fdoraycb32419d2016-06-23 15:52:5530#include "base/run_loop.h"
skyostilf221b7de2015-06-11 20:36:3231#include "base/single_thread_task_runner.h"
[email protected]7286e3fc2011-07-19 22:13:2432#include "base/stl_util.h"
brettwd97eede2015-07-06 22:09:0033#include "base/strings/pattern.h"
[email protected]00e7bef2013-06-10 20:35:1734#include "base/strings/string16.h"
[email protected]3ea1b182013-02-08 22:38:4135#include "base/strings/string_number_conversions.h"
[email protected]00e7bef2013-06-10 20:35:1736#include "base/strings/string_util.h"
[email protected]112158af2013-06-07 23:46:1837#include "base/strings/utf_string_conversions.h"
vabr9984ea62017-04-10 11:33:4938#include "base/values.h"
[email protected]aa142702010-03-26 01:26:3339#include "base/version.h"
avia2f4804a2015-12-24 23:11:1340#include "build/build_config.h"
[email protected]9d32ded072011-10-11 16:31:0541#include "chrome/browser/browser_process.h"
[email protected]9ea0cd32013-07-12 01:50:3642#include "chrome/browser/chrome_notification_types.h"
[email protected]3f2a2fa2013-09-24 02:55:2543#include "chrome/browser/extensions/blacklist.h"
[email protected]74474042013-11-21 12:03:5444#include "chrome/browser/extensions/chrome_app_sorting.h"
rdevlin.croninf87d15f2017-01-26 22:57:1945#include "chrome/browser/extensions/chrome_test_extension_loader.h"
[email protected]d8c8f25f2011-11-02 18:18:0146#include "chrome/browser/extensions/component_loader.h"
[email protected]eb6c7ef2011-12-12 23:12:2047#include "chrome/browser/extensions/crx_installer.h"
[email protected]a7cd28e2012-10-05 21:03:3648#include "chrome/browser/extensions/default_apps.h"
[email protected]a17f9462009-06-09 02:56:4149#include "chrome/browser/extensions/extension_creator.h"
[email protected]89226982012-07-16 20:09:1850#include "chrome/browser/extensions/extension_error_ui.h"
binjinb2454382014-09-22 15:17:4351#include "chrome/browser/extensions/extension_management_test_util.h"
[email protected]f484f8d52014-06-12 08:38:1852#include "chrome/browser/extensions/extension_service_test_base.h"
treib9afc6212015-10-30 18:49:5853#include "chrome/browser/extensions/extension_service_test_with_install.h"
[email protected]19eb80152011-02-26 00:28:4354#include "chrome/browser/extensions/extension_special_storage_policy.h"
[email protected]a7ff4b72013-10-17 20:56:0255#include "chrome/browser/extensions/extension_util.h"
[email protected]2894a512014-06-26 19:03:5656#include "chrome/browser/extensions/external_install_error.h"
57#include "chrome/browser/extensions/external_install_manager.h"
[email protected]e410b5f2012-12-14 14:02:2458#include "chrome/browser/extensions/external_policy_loader.h"
[email protected]5df038b2012-07-16 19:03:2759#include "chrome/browser/extensions/external_pref_loader.h"
60#include "chrome/browser/extensions/external_provider_impl.h"
[email protected]3f2a2fa2013-09-24 02:55:2561#include "chrome/browser/extensions/fake_safe_browsing_database_manager.h"
[email protected]d8c8f25f2011-11-02 18:18:0162#include "chrome/browser/extensions/installed_loader.h"
Devlin Cronin9722a722017-12-16 03:35:1063#include "chrome/browser/extensions/load_error_reporter.h"
[email protected]f0bfe622012-06-22 01:01:4464#include "chrome/browser/extensions/pack_extension_job.h"
[email protected]f3d3b382014-03-14 21:19:2865#include "chrome/browser/extensions/pending_extension_info.h"
66#include "chrome/browser/extensions/pending_extension_manager.h"
binjine6b58b52014-10-31 01:55:5767#include "chrome/browser/extensions/permissions_updater.h"
[email protected]f71b582c2014-01-10 17:03:1568#include "chrome/browser/extensions/test_blacklist.h"
Devlin Cronina7baea22017-12-12 14:43:0569#include "chrome/browser/extensions/test_extension_dir.h"
[email protected]31d8f5f22012-04-02 15:22:0870#include "chrome/browser/extensions/test_extension_system.h"
[email protected]d8c8f25f2011-11-02 18:18:0171#include "chrome/browser/extensions/unpacked_installer.h"
[email protected]42a08162012-03-16 18:09:1172#include "chrome/browser/extensions/updater/extension_updater.h"
dpolukhin1687ef32015-06-22 11:12:3773#include "chrome/browser/policy/profile_policy_connector.h"
74#include "chrome/browser/policy/profile_policy_connector_factory.h"
lazyboy1899eec42016-03-08 19:00:5075#include "chrome/browser/ui/global_error/global_error.h"
76#include "chrome/browser/ui/global_error/global_error_service.h"
77#include "chrome/browser/ui/global_error/global_error_service_factory.h"
[email protected]31d8f5f22012-04-02 15:22:0878#include "chrome/common/chrome_constants.h"
[email protected]37858e52010-08-26 00:22:0279#include "chrome/common/chrome_switches.h"
[email protected]6b414c232013-06-05 07:53:3480#include "chrome/common/extensions/manifest_handlers/app_launch_info.h"
[email protected]334ec0a2013-03-24 01:36:1681#include "chrome/common/extensions/manifest_handlers/content_scripts_handler.h"
[email protected]36a784c2009-06-23 06:21:0882#include "chrome/common/pref_names.h"
[email protected]24b538a2010-02-27 01:22:4483#include "chrome/common/url_constants.h"
thestig4a9b0ef2016-08-29 08:22:1284#include "chrome/grit/browser_resources.h"
85#include "chrome/grit/generated_resources.h"
[email protected]078c3192013-08-22 08:18:0586#include "chrome/test/base/scoped_browser_locale.h"
[email protected]a4ff9eae2011-08-01 19:58:1687#include "chrome/test/base/testing_profile.h"
[email protected]fdd28372014-08-21 02:27:2688#include "components/crx_file/id_util.h"
[email protected]f0c8c4992014-05-15 17:37:2689#include "components/pref_registry/pref_registry_syncable.h"
brettwb1fc1b82016-02-02 00:19:0890#include "components/prefs/scoped_user_pref_update.h"
skym71603842016-10-10 18:17:3191#include "components/sync/model/string_ordinal.h"
maxbogueea16ff412016-10-28 16:35:2992#include "components/sync_preferences/pref_service_syncable.h"
93#include "components/sync_preferences/testing_pref_service_syncable.h"
[email protected]35cc399e2012-02-23 18:19:2894#include "content/public/browser/dom_storage_context.h"
[email protected]98270432012-09-11 20:51:2495#include "content/public/browser/gpu_data_manager.h"
[email protected]35cc399e2012-02-23 18:19:2896#include "content/public/browser/indexed_db_context.h"
[email protected]ad50def52011-10-19 23:17:0797#include "content/public/browser/notification_service.h"
[email protected]e67385f2011-12-21 06:00:5698#include "content/public/browser/plugin_service.h"
[email protected]6d057a0c2013-07-09 21:12:0799#include "content/public/browser/render_process_host.h"
[email protected]5c8e67c2012-08-29 00:48:52100#include "content/public/browser/storage_partition.h"
[email protected]55eb70e762012-02-20 17:38:39101#include "content/public/common/content_constants.h"
skyostil0becb332015-04-27 17:59:37102#include "content/public/test/test_browser_thread_bundle.h"
[email protected]6d057a0c2013-07-09 21:12:07103#include "content/public/test/test_utils.h"
binjin47947f842014-11-18 12:10:24104#include "extensions/browser/extension_prefs.h"
[email protected]5fdfa562013-12-27 17:43:59105#include "extensions/browser/extension_registry.h"
[email protected]59b0e602014-01-30 00:41:24106#include "extensions/browser/extension_system.h"
karandeepb810e33402017-04-05 23:41:22107#include "extensions/browser/extension_util.h"
lazyboye8634172016-01-28 00:10:48108#include "extensions/browser/external_install_info.h"
[email protected]301116c62013-11-26 10:37:45109#include "extensions/browser/external_provider_interface.h"
[email protected]4a1d9c0d2014-06-13 12:50:11110#include "extensions/browser/install_flag.h"
[email protected]301116c62013-11-26 10:37:45111#include "extensions/browser/management_policy.h"
lazyboya00eafc2017-04-08 00:57:19112#include "extensions/browser/mock_external_provider.h"
limasdfe1d046f2015-10-29 00:48:00113#include "extensions/browser/test_extension_registry_observer.h"
[email protected]301116c62013-11-26 10:37:45114#include "extensions/browser/test_management_policy.h"
[email protected]e43c61f2014-07-20 21:46:34115#include "extensions/browser/uninstall_reason.h"
Minh X. Nguyen45479012017-08-18 21:35:36116#include "extensions/common/disable_reason.h"
[email protected]e4452d32013-11-15 23:07:41117#include "extensions/common/extension.h"
[email protected]22b7b2c2013-11-05 22:52:42118#include "extensions/common/extension_builder.h"
[email protected]6668e5d2014-04-08 23:32:52119#include "extensions/common/extension_l10n_util.h"
[email protected]993da5e2013-03-23 21:25:16120#include "extensions/common/extension_resource.h"
Karan Bhatiafe12281a2017-09-16 02:38:18121#include "extensions/common/file_util.h"
[email protected]0c3c9732013-09-16 08:53:41122#include "extensions/common/manifest_constants.h"
[email protected]558878cc82013-11-09 01:25:51123#include "extensions/common/manifest_handlers/background_info.h"
binjine6b58b52014-10-31 01:55:57124#include "extensions/common/manifest_handlers/permissions_parser.h"
rockotd5546142014-10-15 00:29:08125#include "extensions/common/manifest_url_handlers.h"
[email protected]5a55f3f2013-10-29 01:08:29126#include "extensions/common/permissions/permission_set.h"
[email protected]076ebeda2014-06-06 21:47:26127#include "extensions/common/permissions/permissions_data.h"
[email protected]a612eb32014-03-31 22:09:17128#include "extensions/common/switches.h"
[email protected]885c0e92012-11-13 20:27:42129#include "extensions/common/url_pattern.h"
[email protected]22b7b2c2013-11-05 22:52:42130#include "extensions/common/value_builder.h"
[email protected]aa84a7e2012-03-15 21:29:06131#include "net/cookies/cookie_options.h"
mmenkee28c9aa2016-01-20 23:54:04132#include "net/cookies/cookie_store.h"
[email protected]dbbad7a2010-08-13 18:18:36133#include "net/url_request/url_request_context.h"
[email protected]abe2c032011-03-31 18:49:34134#include "net/url_request/url_request_context_getter.h"
brettw4b461082016-11-19 18:55:16135#include "ppapi/features/features.h"
pilgrime92c5fcd2014-09-10 23:31:23136#include "storage/browser/database/database_tracker.h"
137#include "storage/browser/quota/quota_manager.h"
pilgrim16330552014-09-10 01:32:22138#include "storage/common/database/database_identifier.h"
treib9afc6212015-10-30 18:49:58139#include "testing/gmock/include/gmock/gmock.h"
[email protected]6014d672008-12-05 00:38:25140#include "testing/gtest/include/gtest/gtest.h"
[email protected]f66c110c2008-12-05 20:26:29141#include "testing/platform_test.h"
fatalerr2443c2df2014-10-31 19:14:35142#include "ui/base/l10n/l10n_util.h"
[email protected]a6483d22013-07-03 22:11:00143#include "url/gurl.h"
[email protected]6014d672008-12-05 00:38:25144
nparker333f169b2015-04-18 13:33:07145// The blacklist tests rely on the safe-browsing database.
146#if defined(SAFE_BROWSING_DB_LOCAL)
[email protected]3f2a2fa2013-09-24 02:55:25147#define ENABLE_BLACKLIST_TESTS
148#endif
149
[email protected]55eb70e762012-02-20 17:38:39150using content::BrowserContext;
[email protected]631bb742011-11-02 11:29:39151using content::BrowserThread;
[email protected]35cc399e2012-02-23 18:19:28152using content::DOMStorageContext;
153using content::IndexedDBContext;
[email protected]e67385f2011-12-21 06:00:56154using content::PluginService;
[email protected]c2e66e12012-06-27 06:27:06155using extensions::APIPermission;
156using extensions::APIPermissionSet;
[email protected]74474042013-11-21 12:03:54157using extensions::AppSorting;
[email protected]3f2a2fa2013-09-24 02:55:25158using extensions::Blacklist;
[email protected]bf3d9df2012-07-24 23:20:27159using extensions::CrxInstaller;
[email protected]1c321ee52012-05-21 03:02:34160using extensions::Extension;
Devlin Cronin05b047d2017-08-22 21:53:55161using extensions::ExtensionBuilder;
[email protected]6d777492012-07-11 17:33:43162using extensions::ExtensionCreator;
[email protected]45759612012-07-10 17:21:23163using extensions::ExtensionPrefs;
[email protected]5fdfa562013-12-27 17:43:59164using extensions::ExtensionRegistry;
[email protected]993da5e2013-03-23 21:25:16165using extensions::ExtensionResource;
[email protected]bd306722012-07-11 20:43:59166using extensions::ExtensionSystem;
lazyboy0b9b30f2016-01-05 03:15:37167using extensions::ExternalInstallError;
lazyboye8634172016-01-28 00:10:48168using extensions::ExternalInstallInfoFile;
169using extensions::ExternalInstallInfoUpdateUrl;
170using extensions::ExternalProviderInterface;
[email protected]3f2a2fa2013-09-24 02:55:25171using extensions::FakeSafeBrowsingDatabaseManager;
[email protected]00b5d0a52012-10-30 13:13:53172using extensions::FeatureSwitch;
[email protected]1d5e58b2013-01-31 08:41:40173using extensions::Manifest;
lazyboya00eafc2017-04-08 00:57:19174using extensions::MockExternalProvider;
[email protected]c2e66e12012-06-27 06:27:06175using extensions::PermissionSet;
[email protected]b3aa7182013-04-25 04:45:23176using extensions::TestExtensionSystem;
limasdf0deef2042017-05-03 19:17:17177using extensions::UnloadedExtensionReason;
[email protected]e9f541a2012-11-19 21:52:31178using extensions::URLPatternSet;
[email protected]631bb742011-11-02 11:29:39179
[email protected]6bf90612013-08-15 00:36:27180namespace keys = extensions::manifest_keys;
[email protected]c6d474f82009-12-16 21:11:06181
[email protected]f0397fa2008-12-11 17:59:58182namespace {
183
[email protected]df4956e2009-06-10 16:53:42184// Extension ids used during testing.
[email protected]095ccbe42013-09-26 00:06:42185const char good0[] = "behllobkkfkfnphdnhnkndlbkcpglgmj";
186const char good1[] = "hpiknbiabeeppbpihjehijgoemciehgk";
187const char good2[] = "bjafgdebaacbbbecmhlhpofkepfkgcpa";
[email protected]aa5219d2013-11-09 19:25:41188const char all_zero[] = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
[email protected]095ccbe42013-09-26 00:06:42189const char good2048[] = "nmgjhmhbleinmjpbdhgajfjkbijcmgbh";
190const char good_crx[] = "ldnnhddmnhbkjipkidpdiheffobcpfmf";
lazyboy0b9b30f2016-01-05 03:15:37191const char minimal_platform_app_crx[] = "jjeoclcdfjddkdjokiejckgcildcflpp";
[email protected]095ccbe42013-09-26 00:06:42192const char hosted_app[] = "kbmnembihfiondgfjekmnmcbddelicoi";
193const char page_action[] = "obcimlgaoabeegjmmpldobjndiealpln";
194const char theme_crx[] = "iamefpfkojoapidjnbafmgkgncegbkad";
195const char theme2_crx[] = "pjpgmfcmabopnnfonnhmdjglfpjjfkbf";
196const char permissions_crx[] = "eagpmdpfmaekmmcejjbmjoecnejeiiin";
[email protected]095ccbe42013-09-26 00:06:42197const char updates_from_webstore[] = "akjooamlhcgeopfifcmlggaebeocgokj";
lazyboy1899eec42016-03-08 19:00:50198const char updates_from_webstore2[] = "oolblhbomdbcpmafphaodhjfcgbihcdg";
199const char updates_from_webstore3[] = "bmfoocgfinpmkmlbjhcbofejhkhlbchk";
binjine6b58b52014-10-31 01:55:57200const char permissions_blocklist[] = "noffkehfcaggllbcojjbopcmlhcnhcdn";
Takumi Fujimoto43c8c00f2017-07-26 22:48:56201const char cast_stable[] = "boadgeojelhgndaghljhdicfkmllpafd";
202const char cast_beta[] = "dliochdbjfkdbacpmhlcpmleaejidimm";
Tatsuhisa Yamaguchi5ed1aece32017-10-26 10:18:56203const char zip_unpacker[] = "oedeeodfidgoollimchfdnbmhcpnklnd";
[email protected]df4956e2009-06-10 16:53:42204
lazyboy1899eec42016-03-08 19:00:50205struct BubbleErrorsTestData {
206 BubbleErrorsTestData(const std::string& id,
207 const std::string& version,
208 const base::FilePath& crx_path,
209 size_t expected_bubble_error_count)
210 : id(id),
211 version(version),
212 crx_path(crx_path),
213 expected_bubble_error_count(expected_bubble_error_count) {}
214 std::string id;
215 std::string version;
216 base::FilePath crx_path;
217 size_t expected_bubble_error_count;
218 bool expect_has_shown_bubble_view;
219};
220
[email protected]cced75a2011-05-20 08:31:12221static void AddPattern(URLPatternSet* extent, const std::string& pattern) {
[email protected]8d888c12010-11-30 00:00:25222 int schemes = URLPattern::SCHEME_ALL;
223 extent->AddPattern(URLPattern(schemes, pattern));
224}
225
[email protected]650b2d52013-02-10 03:41:45226base::FilePath GetTemporaryFile() {
227 base::FilePath temp_file;
[email protected]03d9afc02013-12-03 17:55:52228 CHECK(base::CreateTemporaryFile(&temp_file));
[email protected]a17dfcf2012-12-30 02:07:09229 return temp_file;
230}
231
[email protected]6d057a0c2013-07-09 21:12:07232bool WaitForCountNotificationsCallback(int *count) {
233 return --(*count) == 0;
234}
235
lazyboy0b9b30f2016-01-05 03:15:37236bool HasExternalInstallErrors(ExtensionService* service) {
237 return !service->external_install_manager()->GetErrorsForTesting().empty();
238}
239
240bool HasExternalInstallBubble(ExtensionService* service) {
241 std::vector<ExternalInstallError*> errors =
242 service->external_install_manager()->GetErrorsForTesting();
243 auto found = std::find_if(
244 errors.begin(), errors.end(),
245 [](const ExternalInstallError* error) {
246 return error->alert_type() == ExternalInstallError::BUBBLE_ALERT;
247 });
248 return found != errors.end();
249}
250
lazyboy1899eec42016-03-08 19:00:50251size_t GetExternalInstallBubbleCount(ExtensionService* service) {
252 size_t bubble_count = 0u;
253 std::vector<ExternalInstallError*> errors =
254 service->external_install_manager()->GetErrorsForTesting();
vmpstrae72b082016-07-25 21:55:47255 for (auto* error : errors)
lazyboy1899eec42016-03-08 19:00:50256 bubble_count += error->alert_type() == ExternalInstallError::BUBBLE_ALERT;
257 return bubble_count;
258}
259
mtomasz622f70d22017-01-27 03:36:26260scoped_refptr<Extension> CreateExtension(const base::string16& name,
261 const base::FilePath& path,
262 Manifest::Location location) {
263 base::DictionaryValue manifest;
264 manifest.SetString(extensions::manifest_keys::kVersion, "1.0.0.0");
265 manifest.SetString(extensions::manifest_keys::kName, name);
266 std::string error;
267 scoped_refptr<Extension> extension =
268 Extension::Create(path, location, manifest, Extension::NO_FLAGS, &error);
269 EXPECT_TRUE(extension.get() != nullptr) << error;
270 return extension;
271}
272
lazyboy8a08c9d2017-04-11 19:53:22273std::unique_ptr<ExternalInstallInfoFile> CreateExternalExtension(
274 const extensions::ExtensionId& extension_id,
275 const std::string& version_str,
276 const base::FilePath& path,
277 Manifest::Location location,
278 Extension::InitFromValueFlags flags) {
279 return base::MakeUnique<ExternalInstallInfoFile>(
Devlin Cronind4c2a8f32017-09-29 17:08:30280 extension_id, base::Version(version_str), path, location, flags, false,
281 false);
lazyboy8a08c9d2017-04-11 19:53:22282}
283
Karan Bhatiafe12281a2017-09-16 02:38:18284// Helper function to persist the passed directories and file paths in
285// |extension_dir|. Also, writes a generic manifest file.
286void PersistExtensionWithPaths(
287 const base::FilePath& extension_dir,
288 const std::vector<base::FilePath>& directory_paths,
289 const std::vector<base::FilePath>& file_paths) {
290 for (const auto& directory : directory_paths)
291 EXPECT_TRUE(base::CreateDirectory(directory));
292
293 std::string data = "file_data";
294 for (const auto& file : file_paths) {
295 EXPECT_EQ(static_cast<int>(data.size()),
296 base::WriteFile(file, data.c_str(), data.size()));
297 }
298
299 std::unique_ptr<base::DictionaryValue> manifest =
300 extensions::DictionaryBuilder()
301 .Set(keys::kName, "Test extension")
302 .Set(keys::kVersion, "1.0")
303 .Set(keys::kManifestVersion, 2)
304 .Build();
305
306 // Persist manifest file.
307 base::FilePath manifest_path =
308 extension_dir.Append(extensions::kManifestFilename);
309 JSONFileValueSerializer(manifest_path).Serialize(*manifest);
310 EXPECT_TRUE(base::PathExists(manifest_path));
311}
312
[email protected]f0397fa2008-12-11 17:59:58313} // namespace
[email protected]6014d672008-12-05 00:38:25314
Istiaque Ahmed2a59ae02017-08-24 04:43:26315namespace extensions {
316
317// A simplified version of ExternalPrefLoader that loads the dictionary
318// from json data specified in a string.
319class ExternalTestingLoader : public ExternalLoader {
320 public:
321 ExternalTestingLoader(const std::string& json_data,
322 const base::FilePath& fake_base_path)
323 : fake_base_path_(fake_base_path) {
324 DCHECK_CURRENTLY_ON(BrowserThread::UI);
325 JSONStringValueDeserializer deserializer(json_data);
326 base::FilePath fake_json_path = fake_base_path.AppendASCII("fake.json");
327 testing_prefs_ = ExternalPrefLoader::ExtractExtensionPrefs(&deserializer,
328 fake_json_path);
329 }
330
331 // ExternalLoader:
332 const base::FilePath GetBaseCrxFilePath() override { return fake_base_path_; }
333
334 void StartLoading() override {
335 DCHECK_CURRENTLY_ON(BrowserThread::UI);
Istiaque Ahmedef99c8ea2017-09-06 21:19:57336 LoadFinished(testing_prefs_->CreateDeepCopy());
Istiaque Ahmed2a59ae02017-08-24 04:43:26337 }
338
339 private:
340 friend class base::RefCountedThreadSafe<ExternalLoader>;
341
342 ~ExternalTestingLoader() override {}
343
344 base::FilePath fake_base_path_;
345 std::unique_ptr<base::DictionaryValue> testing_prefs_;
346
347 DISALLOW_COPY_AND_ASSIGN(ExternalTestingLoader);
348};
349
350} // namespace extensions
351
[email protected]8e4560b62011-01-14 10:09:14352class MockProviderVisitor
[email protected]5df038b2012-07-16 19:03:27353 : public extensions::ExternalProviderInterface::VisitorInterface {
[email protected]27b985d2009-06-25 17:53:15354 public:
[email protected]f0841cd2011-01-19 15:07:24355 // The provider will return |fake_base_path| from
356 // GetBaseCrxFilePath(). User can test the behavior with
357 // and without an empty path using this parameter.
[email protected]650b2d52013-02-10 03:41:45358 explicit MockProviderVisitor(base::FilePath fake_base_path)
[email protected]f0841cd2011-01-19 15:07:24359 : ids_found_(0),
[email protected]f121003b2012-05-04 21:57:47360 fake_base_path_(fake_base_path),
361 expected_creation_flags_(Extension::NO_FLAGS) {
[email protected]19eac6d2013-05-30 06:51:03362 profile_.reset(new TestingProfile);
[email protected]f121003b2012-05-04 21:57:47363 }
364
[email protected]650b2d52013-02-10 03:41:45365 MockProviderVisitor(base::FilePath fake_base_path,
366 int expected_creation_flags)
[email protected]f121003b2012-05-04 21:57:47367 : ids_found_(0),
368 fake_base_path_(fake_base_path),
369 expected_creation_flags_(expected_creation_flags) {
dpolukhin1687ef32015-06-22 11:12:37370 profile_.reset(new TestingProfile);
[email protected]27b985d2009-06-25 17:53:15371 }
372
[email protected]683d0702010-12-06 16:25:57373 int Visit(const std::string& json_data) {
lazyboye8634172016-01-28 00:10:48374 return Visit(json_data, Manifest::EXTERNAL_PREF,
375 Manifest::EXTERNAL_PREF_DOWNLOAD);
376 }
377
378 int Visit(const std::string& json_data,
379 Manifest::Location crx_location,
380 Manifest::Location download_location) {
381 crx_location_ = crx_location;
[email protected]27b985d2009-06-25 17:53:15382 // Give the test json file to the provider for parsing.
[email protected]5df038b2012-07-16 19:03:27383 provider_.reset(new extensions::ExternalProviderImpl(
lazyboye8634172016-01-28 00:10:48384 this, new extensions::ExternalTestingLoader(json_data, fake_base_path_),
385 profile_.get(), crx_location, download_location, Extension::NO_FLAGS));
[email protected]27b985d2009-06-25 17:53:15386
387 // We also parse the file into a dictionary to compare what we get back
388 // from the provider.
lazyboye8634172016-01-28 00:10:48389 prefs_ = GetDictionaryFromJSON(json_data);
[email protected]27b985d2009-06-25 17:53:15390
391 // Reset our counter.
392 ids_found_ = 0;
[email protected]683d0702010-12-06 16:25:57393 // Ask the provider to look up all extensions and return them.
[email protected]8e4560b62011-01-14 10:09:14394 provider_->VisitRegisteredExtension();
[email protected]27b985d2009-06-25 17:53:15395
396 return ids_found_;
397 }
398
lazyboye8634172016-01-28 00:10:48399 bool OnExternalExtensionFileFound(
400 const ExternalInstallInfoFile& info) override {
401 EXPECT_EQ(expected_creation_flags_, info.creation_flags);
[email protected]1bf73cc32011-10-26 22:38:31402
[email protected]27b985d2009-06-25 17:53:15403 ++ids_found_;
[email protected]023b3d12013-12-23 18:46:49404 base::DictionaryValue* pref;
[email protected]27b985d2009-06-25 17:53:15405 // This tests is to make sure that the provider only notifies us of the
406 // values we gave it. So if the id we doesn't exist in our internal
407 // dictionary then something is wrong.
lazyboye8634172016-01-28 00:10:48408 EXPECT_TRUE(prefs_->GetDictionary(info.extension_id, &pref))
409 << "Got back ID (" << info.extension_id.c_str()
410 << ") we weren't expecting";
[email protected]27b985d2009-06-25 17:53:15411
lazyboye8634172016-01-28 00:10:48412 EXPECT_TRUE(info.path.IsAbsolute());
[email protected]f0841cd2011-01-19 15:07:24413 if (!fake_base_path_.empty())
lazyboye8634172016-01-28 00:10:48414 EXPECT_TRUE(fake_base_path_.IsParent(info.path));
[email protected]f0841cd2011-01-19 15:07:24415
[email protected]27b985d2009-06-25 17:53:15416 if (pref) {
lazyboye8634172016-01-28 00:10:48417 EXPECT_TRUE(provider_->HasExtension(info.extension_id));
[email protected]0a60a2e2010-10-25 16:15:21418
[email protected]27b985d2009-06-25 17:53:15419 // Ask provider if the extension we got back is registered.
[email protected]1d5e58b2013-01-31 08:41:40420 Manifest::Location location = Manifest::INVALID_LOCATION;
pwnallcbd73192016-08-22 18:59:17421 std::unique_ptr<base::Version> v1;
[email protected]650b2d52013-02-10 03:41:45422 base::FilePath crx_path;
[email protected]0a60a2e2010-10-25 16:15:21423
lazyboye8634172016-01-28 00:10:48424 EXPECT_TRUE(provider_->GetExtensionDetails(info.extension_id, NULL, &v1));
Devlin Cronind4c2a8f32017-09-29 17:08:30425 EXPECT_EQ(info.version.GetString(), v1->GetString());
[email protected]0a60a2e2010-10-25 16:15:21426
pwnallcbd73192016-08-22 18:59:17427 std::unique_ptr<base::Version> v2;
lazyboye8634172016-01-28 00:10:48428 EXPECT_TRUE(
429 provider_->GetExtensionDetails(info.extension_id, &location, &v2));
Devlin Cronind4c2a8f32017-09-29 17:08:30430 EXPECT_EQ(info.version.GetString(), v1->GetString());
431 EXPECT_EQ(info.version.GetString(), v2->GetString());
lazyboye8634172016-01-28 00:10:48432 EXPECT_EQ(crx_location_, location);
[email protected]27b985d2009-06-25 17:53:15433
434 // Remove it so we won't count it ever again.
lazyboye8634172016-01-28 00:10:48435 prefs_->Remove(info.extension_id, NULL);
[email protected]27b985d2009-06-25 17:53:15436 }
[email protected]9060d8b02012-01-13 02:14:30437 return true;
[email protected]27b985d2009-06-25 17:53:15438 }
439
lazyboye8634172016-01-28 00:10:48440 bool OnExternalExtensionUpdateUrlFound(
441 const ExternalInstallInfoUpdateUrl& info,
442 bool is_initial_load) override {
[email protected]8ef78fd2010-08-19 17:14:32443 ++ids_found_;
[email protected]023b3d12013-12-23 18:46:49444 base::DictionaryValue* pref;
[email protected]8ef78fd2010-08-19 17:14:32445 // This tests is to make sure that the provider only notifies us of the
446 // values we gave it. So if the id we doesn't exist in our internal
447 // dictionary then something is wrong.
lazyboye8634172016-01-28 00:10:48448 EXPECT_TRUE(prefs_->GetDictionary(info.extension_id, &pref))
449 << L"Got back ID (" << info.extension_id.c_str()
450 << ") we weren't expecting";
451 EXPECT_EQ(Manifest::EXTERNAL_PREF_DOWNLOAD, info.download_location);
[email protected]8ef78fd2010-08-19 17:14:32452
453 if (pref) {
lazyboye8634172016-01-28 00:10:48454 EXPECT_TRUE(provider_->HasExtension(info.extension_id));
[email protected]0a60a2e2010-10-25 16:15:21455
456 // External extensions with update URLs do not have versions.
pwnallcbd73192016-08-22 18:59:17457 std::unique_ptr<base::Version> v1;
[email protected]1d5e58b2013-01-31 08:41:40458 Manifest::Location location1 = Manifest::INVALID_LOCATION;
lazyboye8634172016-01-28 00:10:48459 EXPECT_TRUE(
460 provider_->GetExtensionDetails(info.extension_id, &location1, &v1));
[email protected]0a60a2e2010-10-25 16:15:21461 EXPECT_FALSE(v1.get());
[email protected]1d5e58b2013-01-31 08:41:40462 EXPECT_EQ(Manifest::EXTERNAL_PREF_DOWNLOAD, location1);
[email protected]0a60a2e2010-10-25 16:15:21463
[email protected]d8fd0fd2014-03-24 13:16:06464 std::string parsed_install_parameter;
465 pref->GetString("install_parameter", &parsed_install_parameter);
lazyboye8634172016-01-28 00:10:48466 EXPECT_EQ(parsed_install_parameter, info.install_parameter);
[email protected]d8fd0fd2014-03-24 13:16:06467
[email protected]8ef78fd2010-08-19 17:14:32468 // Remove it so we won't count it again.
lazyboye8634172016-01-28 00:10:48469 prefs_->Remove(info.extension_id, NULL);
[email protected]8ef78fd2010-08-19 17:14:32470 }
[email protected]9060d8b02012-01-13 02:14:30471 return true;
[email protected]8ef78fd2010-08-19 17:14:32472 }
473
lazyboye8634172016-01-28 00:10:48474 void OnExternalProviderUpdateComplete(
475 const ExternalProviderInterface* provider,
Devlin Cronin19f70b6a2017-10-01 04:14:05476 const std::vector<ExternalInstallInfoUpdateUrl>& update_url_extensions,
477 const std::vector<ExternalInstallInfoFile>& file_extensions,
lazyboye8634172016-01-28 00:10:48478 const std::set<std::string>& removed_extensions) override {
479 ADD_FAILURE() << "MockProviderVisitor does not provide incremental updates,"
480 " use MockUpdateProviderVisitor instead.";
481 }
482
dchengae36a4a2014-10-21 12:36:36483 void OnExternalProviderReady(
mostynba15bee12014-10-04 00:40:32484 const extensions::ExternalProviderInterface* provider) override {
[email protected]50067e52011-10-20 23:17:07485 EXPECT_EQ(provider, provider_.get());
486 EXPECT_TRUE(provider->IsReady());
[email protected]8e4560b62011-01-14 10:09:14487 }
488
dpolukhin2c6ef2932015-05-12 16:06:13489 Profile* profile() { return profile_.get(); }
490
lazyboye8634172016-01-28 00:10:48491 protected:
dchengc963c7142016-04-08 03:55:22492 std::unique_ptr<extensions::ExternalProviderImpl> provider_;
lazyboye8634172016-01-28 00:10:48493
dchengc963c7142016-04-08 03:55:22494 std::unique_ptr<base::DictionaryValue> GetDictionaryFromJSON(
lazyboye8634172016-01-28 00:10:48495 const std::string& json_data) {
496 // We also parse the file into a dictionary to compare what we get back
497 // from the provider.
498 JSONStringValueDeserializer deserializer(json_data);
dchengc963c7142016-04-08 03:55:22499 std::unique_ptr<base::Value> json_value =
500 deserializer.Deserialize(NULL, NULL);
lazyboye8634172016-01-28 00:10:48501
jdoerrie1f536b22017-10-23 17:15:11502 if (!json_value || !json_value->is_dict()) {
lazyboye8634172016-01-28 00:10:48503 ADD_FAILURE() << "Unable to deserialize json data";
dchengc963c7142016-04-08 03:55:22504 return std::unique_ptr<base::DictionaryValue>();
lazyboye8634172016-01-28 00:10:48505 } else {
506 return base::DictionaryValue::From(std::move(json_value));
507 }
508 }
509
[email protected]27b985d2009-06-25 17:53:15510 private:
511 int ids_found_;
[email protected]650b2d52013-02-10 03:41:45512 base::FilePath fake_base_path_;
[email protected]f121003b2012-05-04 21:57:47513 int expected_creation_flags_;
lazyboye8634172016-01-28 00:10:48514 Manifest::Location crx_location_;
dchengc963c7142016-04-08 03:55:22515 std::unique_ptr<base::DictionaryValue> prefs_;
516 std::unique_ptr<TestingProfile> profile_;
[email protected]27b985d2009-06-25 17:53:15517
518 DISALLOW_COPY_AND_ASSIGN(MockProviderVisitor);
[email protected]a1257b12009-06-12 02:51:34519};
520
lazyboye8634172016-01-28 00:10:48521// Mock provider that can simulate incremental update like
522// ExternalRegistryLoader.
523class MockUpdateProviderVisitor : public MockProviderVisitor {
524 public:
525 // The provider will return |fake_base_path| from
526 // GetBaseCrxFilePath(). User can test the behavior with
527 // and without an empty path using this parameter.
528 explicit MockUpdateProviderVisitor(base::FilePath fake_base_path)
529 : MockProviderVisitor(fake_base_path) {}
530
531 void VisitDueToUpdate(const std::string& json_data) {
532 update_url_extension_ids_.clear();
533 file_extension_ids_.clear();
534 removed_extension_ids_.clear();
535
dchengc963c7142016-04-08 03:55:22536 std::unique_ptr<base::DictionaryValue> new_prefs =
lazyboye8634172016-01-28 00:10:48537 GetDictionaryFromJSON(json_data);
538 if (!new_prefs)
539 return;
Istiaque Ahmeda7431b32017-08-20 18:33:37540 provider_->UpdatePrefs(std::move(new_prefs));
lazyboye8634172016-01-28 00:10:48541 }
542
543 void OnExternalProviderUpdateComplete(
544 const ExternalProviderInterface* provider,
Devlin Cronin19f70b6a2017-10-01 04:14:05545 const std::vector<ExternalInstallInfoUpdateUrl>& update_url_extensions,
546 const std::vector<ExternalInstallInfoFile>& file_extensions,
lazyboye8634172016-01-28 00:10:48547 const std::set<std::string>& removed_extensions) override {
lazyboy4aeef202016-09-07 21:28:59548 for (const auto& extension_info : update_url_extensions)
Devlin Cronin19f70b6a2017-10-01 04:14:05549 update_url_extension_ids_.insert(extension_info.extension_id);
lazyboye8634172016-01-28 00:10:48550 EXPECT_EQ(update_url_extension_ids_.size(), update_url_extensions.size());
551
lazyboy4aeef202016-09-07 21:28:59552 for (const auto& extension_info : file_extensions)
Devlin Cronin19f70b6a2017-10-01 04:14:05553 file_extension_ids_.insert(extension_info.extension_id);
lazyboye8634172016-01-28 00:10:48554 EXPECT_EQ(file_extension_ids_.size(), file_extensions.size());
555
556 for (const auto& extension_id : removed_extensions)
557 removed_extension_ids_.insert(extension_id);
558 }
559
560 size_t GetUpdateURLExtensionCount() {
561 return update_url_extension_ids_.size();
562 }
563 size_t GetFileExtensionCount() { return file_extension_ids_.size(); }
564 size_t GetRemovedExtensionCount() { return removed_extension_ids_.size(); }
565
566 bool HasSeenUpdateWithUpdateUrl(const std::string& extension_id) {
567 return update_url_extension_ids_.count(extension_id) > 0u;
568 }
569 bool HasSeenUpdateWithFile(const std::string& extension_id) {
570 return file_extension_ids_.count(extension_id) > 0u;
571 }
572 bool HasSeenRemoval(const std::string& extension_id) {
573 return removed_extension_ids_.count(extension_id) > 0u;
574 }
575
576 private:
577 std::set<std::string> update_url_extension_ids_;
578 std::set<std::string> file_extension_ids_;
579 std::set<std::string> removed_extension_ids_;
580
581 DISALLOW_COPY_AND_ASSIGN(MockUpdateProviderVisitor);
582};
583
treib9afc6212015-10-30 18:49:58584class ExtensionServiceTest
585 : public extensions::ExtensionServiceTestWithInstall {
[email protected]bf73f0b2010-02-10 19:26:59586 public:
Gabriel Charette4a7f5ac2017-06-14 21:54:07587 ExtensionServiceTest() = default;
wkormana08d7c02015-11-17 21:43:38588
lazyboy8a08c9d2017-04-11 19:53:22589 MockExternalProvider* AddMockExternalProvider(Manifest::Location location) {
590 auto provider = base::MakeUnique<MockExternalProvider>(service(), location);
591 MockExternalProvider* provider_ptr = provider.get();
592 service()->AddProviderForTesting(std::move(provider));
593 return provider_ptr;
[email protected]a1257b12009-06-12 02:51:34594 }
595
rdevlin.cronine1456712016-12-29 22:47:28596 // Checks for external extensions and waits for one to complete installing.
597 void WaitForExternalExtensionInstalled() {
598 content::WindowedNotificationObserver observer(
599 extensions::NOTIFICATION_CRX_INSTALLER_DONE,
600 content::NotificationService::AllSources());
601 service()->CheckForExternalUpdates();
602 observer.Wait();
603 }
604
[email protected]9197f3b2009-06-02 00:49:27605 protected:
lazyboya00eafc2017-04-08 00:57:19606 void TestExternalProvider(MockExternalProvider* provider,
[email protected]1d5e58b2013-01-31 08:41:40607 Manifest::Location location);
[email protected]d55e7602009-12-16 04:20:42608
binjine6b58b52014-10-31 01:55:57609 // Grants all optional permissions stated in manifest to active permission
610 // set for extension |id|.
611 void GrantAllOptionalPermissions(const std::string& id) {
612 const Extension* extension = service()->GetInstalledExtension(id);
rdevlin.cronind630c302015-09-30 20:19:33613 const PermissionSet& all_optional_permissions =
binjine6b58b52014-10-31 01:55:57614 extensions::PermissionsParser::GetOptionalPermissions(extension);
615 extensions::PermissionsUpdater perms_updater(profile());
rdevlin.cronine2d0fd02015-09-24 22:35:49616 perms_updater.AddPermissions(extension, all_optional_permissions);
binjine6b58b52014-10-31 01:55:57617 }
618
mlerman6a37b6a42014-11-26 22:10:53619 testing::AssertionResult IsBlocked(const std::string& id) {
dchengc963c7142016-04-08 03:55:22620 std::unique_ptr<extensions::ExtensionSet> all_unblocked_extensions =
mlerman6a37b6a42014-11-26 22:10:53621 registry()->GenerateInstalledExtensionsSet(
622 ExtensionRegistry::EVERYTHING & ~ExtensionRegistry::BLOCKED);
rdevlin.cronin165732a42016-07-18 22:25:08623 if (all_unblocked_extensions->Contains(id))
mlerman6a37b6a42014-11-26 22:10:53624 return testing::AssertionFailure() << id << " is still unblocked!";
625 if (!registry()->blocked_extensions().Contains(id))
626 return testing::AssertionFailure() << id << " is not blocked!";
627 return testing::AssertionSuccess();
628 }
629
630 // Helper method to test that an extension moves through being blocked and
631 // unblocked as appropriate for its type.
632 void AssertExtensionBlocksAndUnblocks(
633 bool should_block, const std::string extension_id) {
634 // Assume we start in an unblocked state.
635 EXPECT_FALSE(IsBlocked(extension_id));
636
637 // Block the extensions.
638 service()->BlockAllExtensions();
Gabriel Charette01507a22017-09-27 21:30:08639 content::RunAllTasksUntilIdle();
mlerman6a37b6a42014-11-26 22:10:53640
641 if (should_block)
642 ASSERT_TRUE(IsBlocked(extension_id));
643 else
644 ASSERT_FALSE(IsBlocked(extension_id));
645
646 service()->UnblockAllExtensions();
Gabriel Charette01507a22017-09-27 21:30:08647 content::RunAllTasksUntilIdle();
mlerman6a37b6a42014-11-26 22:10:53648
649 ASSERT_FALSE(IsBlocked(extension_id));
650 }
651
[email protected]6b75ec32009-08-14 06:37:18652 bool IsPrefExist(const std::string& extension_id,
[email protected]e2194742010-08-12 05:54:34653 const std::string& pref_path) {
[email protected]023b3d12013-12-23 18:46:49654 const base::DictionaryValue* dict =
[email protected]f484f8d52014-06-12 08:38:18655 profile()->GetPrefs()->GetDictionary("extensions.settings");
[email protected]6b75ec32009-08-14 06:37:18656 if (dict == NULL) return false;
[email protected]023b3d12013-12-23 18:46:49657 const base::DictionaryValue* pref = NULL;
[email protected]e2194742010-08-12 05:54:34658 if (!dict->GetDictionary(extension_id, &pref)) {
[email protected]6b75ec32009-08-14 06:37:18659 return false;
660 }
661 if (pref == NULL) {
662 return false;
663 }
664 bool val;
665 if (!pref->GetBoolean(pref_path, &val)) {
666 return false;
667 }
668 return true;
669 }
670
[email protected]8d888c12010-11-30 00:00:25671 void SetPref(const std::string& extension_id,
672 const std::string& pref_path,
vabr9984ea62017-04-10 11:33:49673 std::unique_ptr<base::Value> value,
[email protected]8d888c12010-11-30 00:00:25674 const std::string& msg) {
[email protected]f484f8d52014-06-12 08:38:18675 DictionaryPrefUpdate update(profile()->GetPrefs(), "extensions.settings");
[email protected]023b3d12013-12-23 18:46:49676 base::DictionaryValue* dict = update.Get();
[email protected]8d888c12010-11-30 00:00:25677 ASSERT_TRUE(dict != NULL) << msg;
[email protected]023b3d12013-12-23 18:46:49678 base::DictionaryValue* pref = NULL;
[email protected]8d888c12010-11-30 00:00:25679 ASSERT_TRUE(dict->GetDictionary(extension_id, &pref)) << msg;
680 EXPECT_TRUE(pref != NULL) << msg;
vabr9984ea62017-04-10 11:33:49681 pref->Set(pref_path, std::move(value));
[email protected]8d888c12010-11-30 00:00:25682 }
683
[email protected]6b75ec32009-08-14 06:37:18684 void SetPrefInteg(const std::string& extension_id,
[email protected]e2194742010-08-12 05:54:34685 const std::string& pref_path,
[email protected]6b75ec32009-08-14 06:37:18686 int value) {
[email protected]e2194742010-08-12 05:54:34687 std::string msg = " while setting: ";
688 msg += extension_id;
689 msg += " ";
[email protected]a1257b12009-06-12 02:51:34690 msg += pref_path;
[email protected]e2194742010-08-12 05:54:34691 msg += " = ";
692 msg += base::IntToString(value);
[email protected]a1257b12009-06-12 02:51:34693
vabr9984ea62017-04-10 11:33:49694 SetPref(extension_id, pref_path, base::MakeUnique<base::Value>(value), msg);
[email protected]8d888c12010-11-30 00:00:25695 }
696
697 void SetPrefBool(const std::string& extension_id,
698 const std::string& pref_path,
699 bool value) {
700 std::string msg = " while setting: ";
701 msg += extension_id + " " + pref_path;
702 msg += " = ";
703 msg += (value ? "true" : "false");
704
vabr9984ea62017-04-10 11:33:49705 SetPref(extension_id, pref_path, base::MakeUnique<base::Value>(value), msg);
[email protected]8d888c12010-11-30 00:00:25706 }
707
708 void ClearPref(const std::string& extension_id,
709 const std::string& pref_path) {
710 std::string msg = " while clearing: ";
711 msg += extension_id + " " + pref_path;
712
[email protected]f484f8d52014-06-12 08:38:18713 DictionaryPrefUpdate update(profile()->GetPrefs(), "extensions.settings");
[email protected]023b3d12013-12-23 18:46:49714 base::DictionaryValue* dict = update.Get();
[email protected]a1257b12009-06-12 02:51:34715 ASSERT_TRUE(dict != NULL) << msg;
[email protected]023b3d12013-12-23 18:46:49716 base::DictionaryValue* pref = NULL;
[email protected]e2194742010-08-12 05:54:34717 ASSERT_TRUE(dict->GetDictionary(extension_id, &pref)) << msg;
[email protected]a1257b12009-06-12 02:51:34718 EXPECT_TRUE(pref != NULL) << msg;
[email protected]8d888c12010-11-30 00:00:25719 pref->Remove(pref_path, NULL);
720 }
721
722 void SetPrefStringSet(const std::string& extension_id,
723 const std::string& pref_path,
724 const std::set<std::string>& value) {
725 std::string msg = " while setting: ";
726 msg += extension_id + " " + pref_path;
727
vabr9984ea62017-04-10 11:33:49728 auto list_value = base::MakeUnique<base::ListValue>();
[email protected]8d888c12010-11-30 00:00:25729 for (std::set<std::string>::const_iterator iter = value.begin();
730 iter != value.end(); ++iter)
dchengd9ea63862016-06-03 02:27:18731 list_value->AppendString(*iter);
[email protected]8d888c12010-11-30 00:00:25732
vabr9984ea62017-04-10 11:33:49733 SetPref(extension_id, pref_path, std::move(list_value), msg);
[email protected]a1257b12009-06-12 02:51:34734 }
735
[email protected]ebd71962012-12-20 02:56:55736 void InitPluginService() {
brettw4b461082016-11-19 18:55:16737#if BUILDFLAG(ENABLE_PLUGINS)
[email protected]ebd71962012-12-20 02:56:55738 PluginService::GetInstance()->Init();
739#endif
740 }
741
binjin1569c9b2014-09-05 13:33:18742 void InitializeEmptyExtensionServiceWithTestingPrefs() {
743 ExtensionServiceTestBase::ExtensionServiceInitParams params =
744 CreateDefaultInitParams();
745 params.pref_file = base::FilePath();
746 InitializeExtensionService(params);
747 }
748
[email protected]f484f8d52014-06-12 08:38:18749 extensions::ManagementPolicy* GetManagementPolicy() {
750 return ExtensionSystem::Get(browser_context())->management_policy();
751 }
752
lazyboy0b9b30f2016-01-05 03:15:37753 ExternalInstallError* GetError(const std::string& extension_id) {
754 std::vector<ExternalInstallError*> errors =
755 service_->external_install_manager()->GetErrorsForTesting();
756 auto found = std::find_if(
757 errors.begin(), errors.end(),
758 [&extension_id](const ExternalInstallError* error) {
759 return error->extension_id() == extension_id;
760 });
761 return found == errors.end() ? nullptr : *found;
762 }
763
sdefresne50c1e522015-09-18 09:47:51764 typedef extensions::ExtensionManagementPrefUpdater<
maxbogueea16ff412016-10-28 16:35:29765 sync_preferences::TestingPrefServiceSyncable>
766 ManagementPrefUpdater;
[email protected]bb28e062009-02-27 17:19:18767};
[email protected]6014d672008-12-05 00:38:25768
[email protected]0349ab5d2010-08-11 21:41:57769// Receives notifications from a PackExtensionJob, indicating either that
770// packing succeeded or that there was some error.
[email protected]d9ede582012-08-14 19:21:38771class PackExtensionTestClient : public extensions::PackExtensionJob::Client {
[email protected]0349ab5d2010-08-11 21:41:57772 public:
[email protected]650b2d52013-02-10 03:41:45773 PackExtensionTestClient(const base::FilePath& expected_crx_path,
774 const base::FilePath& expected_private_key_path);
dchengae36a4a2014-10-21 12:36:36775 void OnPackSuccess(const base::FilePath& crx_path,
776 const base::FilePath& private_key_path) override;
777 void OnPackFailure(const std::string& error_message,
778 ExtensionCreator::ErrorType type) override;
[email protected]0349ab5d2010-08-11 21:41:57779
780 private:
[email protected]650b2d52013-02-10 03:41:45781 const base::FilePath expected_crx_path_;
782 const base::FilePath expected_private_key_path_;
[email protected]0349ab5d2010-08-11 21:41:57783 DISALLOW_COPY_AND_ASSIGN(PackExtensionTestClient);
784};
785
786PackExtensionTestClient::PackExtensionTestClient(
[email protected]650b2d52013-02-10 03:41:45787 const base::FilePath& expected_crx_path,
788 const base::FilePath& expected_private_key_path)
[email protected]0349ab5d2010-08-11 21:41:57789 : expected_crx_path_(expected_crx_path),
790 expected_private_key_path_(expected_private_key_path) {}
791
792// If packing succeeded, we make sure that the package names match our
793// expectations.
[email protected]650b2d52013-02-10 03:41:45794void PackExtensionTestClient::OnPackSuccess(
795 const base::FilePath& crx_path,
796 const base::FilePath& private_key_path) {
[email protected]0349ab5d2010-08-11 21:41:57797 // We got the notification and processed it; we don't expect any further tasks
798 // to be posted to the current thread, so we should stop blocking and continue
799 // on with the rest of the test.
800 // This call to |Quit()| matches the call to |Run()| in the
801 // |PackPunctuatedExtension| test.
Gabriel Charette53a9ef812017-07-26 12:36:23802 base::RunLoop::QuitCurrentWhenIdleDeprecated();
[email protected]0349ab5d2010-08-11 21:41:57803 EXPECT_EQ(expected_crx_path_.value(), crx_path.value());
804 EXPECT_EQ(expected_private_key_path_.value(), private_key_path.value());
[email protected]7567484142013-07-11 17:36:07805 ASSERT_TRUE(base::PathExists(private_key_path));
[email protected]0349ab5d2010-08-11 21:41:57806}
807
808// The tests are designed so that we never expect to see a packing error.
[email protected]93d973a2012-01-08 07:38:26809void PackExtensionTestClient::OnPackFailure(const std::string& error_message,
810 ExtensionCreator::ErrorType type) {
811 if (type == ExtensionCreator::kCRXExists)
812 FAIL() << "Packing should not fail.";
813 else
814 FAIL() << "Existing CRX should have been overwritten.";
[email protected]0349ab5d2010-08-11 21:41:57815}
816
[email protected]54cb3c92009-02-17 22:30:21817// Test loading good extensions from the profile directory.
[email protected]d9a61e12012-11-14 02:43:47818TEST_F(ExtensionServiceTest, LoadAllExtensionsFromDirectorySuccess) {
[email protected]ebd71962012-12-20 02:56:55819 InitPluginService();
[email protected]3f2a2fa2013-09-24 02:55:25820 InitializeGoodInstalledExtensionService();
[email protected]f484f8d52014-06-12 08:38:18821 service()->Init();
[email protected]6014d672008-12-05 00:38:25822
avia2f4804a2015-12-24 23:11:13823 uint32_t expected_num_extensions = 3u;
[email protected]e50013c32010-08-18 21:05:24824 ASSERT_EQ(expected_num_extensions, loaded_.size());
[email protected]6014d672008-12-05 00:38:25825
[email protected]fbcc40302009-06-12 20:45:45826 EXPECT_EQ(std::string(good0), loaded_[0]->id());
[email protected]e1cec06c2008-12-18 01:22:23827 EXPECT_EQ(std::string("My extension 1"),
[email protected]894bb502009-05-21 22:39:57828 loaded_[0]->name());
[email protected]e1cec06c2008-12-18 01:22:23829 EXPECT_EQ(std::string("The first extension that I made."),
[email protected]894bb502009-05-21 22:39:57830 loaded_[0]->description());
[email protected]1d5e58b2013-01-31 08:41:40831 EXPECT_EQ(Manifest::INTERNAL, loaded_[0]->location());
[email protected]f484f8d52014-06-12 08:38:18832 EXPECT_TRUE(service()->GetExtensionById(loaded_[0]->id(), false));
833 EXPECT_EQ(expected_num_extensions, registry()->enabled_extensions().size());
[email protected]eab9b452009-01-23 20:48:59834
[email protected]25b34332009-06-05 21:53:19835 ValidatePrefKeyCount(3);
[email protected]e2194742010-08-12 05:54:34836 ValidateIntegerPref(good0, "state", Extension::ENABLED);
[email protected]1d5e58b2013-01-31 08:41:40837 ValidateIntegerPref(good0, "location", Manifest::INTERNAL);
[email protected]e2194742010-08-12 05:54:34838 ValidateIntegerPref(good1, "state", Extension::ENABLED);
[email protected]1d5e58b2013-01-31 08:41:40839 ValidateIntegerPref(good1, "location", Manifest::INTERNAL);
[email protected]e2194742010-08-12 05:54:34840 ValidateIntegerPref(good2, "state", Extension::ENABLED);
[email protected]1d5e58b2013-01-31 08:41:40841 ValidateIntegerPref(good2, "location", Manifest::INTERNAL);
[email protected]25b34332009-06-05 21:53:19842
[email protected]06e8b8ff2011-07-13 15:03:47843 URLPatternSet expected_patterns;
844 AddPattern(&expected_patterns, "file:///*");
845 AddPattern(&expected_patterns, "http://*.google.com/*");
846 AddPattern(&expected_patterns, "https://*.google.com/*");
[email protected]dc24976f2013-06-02 21:15:09847 const Extension* extension = loaded_[0].get();
[email protected]334ec0a2013-03-24 01:36:16848 const extensions::UserScriptList& scripts =
849 extensions::ContentScriptsInfo::GetContentScripts(extension);
[email protected]e66de892009-03-20 20:38:43850 ASSERT_EQ(2u, scripts.size());
lazyboy12c77d72016-08-19 20:06:09851 EXPECT_EQ(expected_patterns, scripts[0]->url_patterns());
852 EXPECT_EQ(2u, scripts[0]->js_scripts().size());
[email protected]052c92702010-06-25 07:25:52853 ExtensionResource resource00(extension->id(),
lazyboy12c77d72016-08-19 20:06:09854 scripts[0]->js_scripts()[0]->extension_root(),
855 scripts[0]->js_scripts()[0]->relative_path());
[email protected]15476932013-04-12 05:17:15856 base::FilePath expected_path =
857 base::MakeAbsoluteFilePath(extension->path().AppendASCII("script1.js"));
[email protected]a14b16b2009-10-28 12:41:29858 EXPECT_TRUE(resource00.ComparePathWithDefault(expected_path));
[email protected]052c92702010-06-25 07:25:52859 ExtensionResource resource01(extension->id(),
lazyboy12c77d72016-08-19 20:06:09860 scripts[0]->js_scripts()[1]->extension_root(),
861 scripts[0]->js_scripts()[1]->relative_path());
[email protected]15476932013-04-12 05:17:15862 expected_path =
863 base::MakeAbsoluteFilePath(extension->path().AppendASCII("script2.js"));
[email protected]a14b16b2009-10-28 12:41:29864 EXPECT_TRUE(resource01.ComparePathWithDefault(expected_path));
lazyboy12c77d72016-08-19 20:06:09865 EXPECT_EQ(1u, scripts[1]->url_patterns().patterns().size());
[email protected]06e8b8ff2011-07-13 15:03:47866 EXPECT_EQ("http://*.news.com/*",
lazyboy12c77d72016-08-19 20:06:09867 scripts[1]->url_patterns().begin()->GetAsString());
[email protected]052c92702010-06-25 07:25:52868 ExtensionResource resource10(extension->id(),
lazyboy12c77d72016-08-19 20:06:09869 scripts[1]->js_scripts()[0]->extension_root(),
870 scripts[1]->js_scripts()[0]->relative_path());
[email protected]a14b16b2009-10-28 12:41:29871 expected_path =
872 extension->path().AppendASCII("js_files").AppendASCII("script3.js");
[email protected]15476932013-04-12 05:17:15873 expected_path = base::MakeAbsoluteFilePath(expected_path);
[email protected]a14b16b2009-10-28 12:41:29874 EXPECT_TRUE(resource10.ComparePathWithDefault(expected_path));
[email protected]06e8b8ff2011-07-13 15:03:47875
876 expected_patterns.ClearPatterns();
877 AddPattern(&expected_patterns, "http://*.google.com/*");
878 AddPattern(&expected_patterns, "https://*.google.com/*");
[email protected]076ebeda2014-06-06 21:47:26879 EXPECT_EQ(
880 expected_patterns,
rdevlin.cronind630c302015-09-30 20:19:33881 extension->permissions_data()->active_permissions().explicit_hosts());
[email protected]6014d672008-12-05 00:38:25882
[email protected]25b34332009-06-05 21:53:19883 EXPECT_EQ(std::string(good1), loaded_[1]->id());
[email protected]894bb502009-05-21 22:39:57884 EXPECT_EQ(std::string("My extension 2"), loaded_[1]->name());
[email protected]007b3f82013-04-09 08:46:45885 EXPECT_EQ(std::string(), loaded_[1]->description());
[email protected]81067e02009-07-27 15:12:09886 EXPECT_EQ(loaded_[1]->GetResourceURL("background.html"),
[email protected]dc24976f2013-06-02 21:15:09887 extensions::BackgroundInfo::GetBackgroundURL(loaded_[1].get()));
lazyboy12c77d72016-08-19 20:06:09888 EXPECT_TRUE(
889 extensions::ContentScriptsInfo::GetContentScripts(loaded_[1].get())
890 .empty());
[email protected]1d5e58b2013-01-31 08:41:40891 EXPECT_EQ(Manifest::INTERNAL, loaded_[1]->location());
[email protected]b8fb3032011-04-29 18:45:56892
[email protected]e50013c32010-08-18 21:05:24893 int index = expected_num_extensions - 1;
894 EXPECT_EQ(std::string(good2), loaded_[index]->id());
895 EXPECT_EQ(std::string("My extension 3"), loaded_[index]->name());
[email protected]007b3f82013-04-09 08:46:45896 EXPECT_EQ(std::string(), loaded_[index]->description());
lazyboy12c77d72016-08-19 20:06:09897 EXPECT_TRUE(
898 extensions::ContentScriptsInfo::GetContentScripts(loaded_[index].get())
899 .empty());
[email protected]1d5e58b2013-01-31 08:41:40900 EXPECT_EQ(Manifest::INTERNAL, loaded_[index]->location());
thestig4b36dd32014-10-31 20:30:19901}
[email protected]cc655912009-01-29 23:19:19902
[email protected]54cb3c92009-02-17 22:30:21903// Test loading bad extensions from the profile directory.
[email protected]d9a61e12012-11-14 02:43:47904TEST_F(ExtensionServiceTest, LoadAllExtensionsFromDirectoryFail) {
[email protected]c6d474f82009-12-16 21:11:06905 // Initialize the test dir with a bad Preferences/extensions.
[email protected]f484f8d52014-06-12 08:38:18906 base::FilePath source_install_dir =
907 data_dir().AppendASCII("bad").AppendASCII("Extensions");
[email protected]e96a0602014-02-15 08:27:42908 base::FilePath pref_path =
909 source_install_dir.DirName().Append(chrome::kPreferencesFilename);
[email protected]54cb3c92009-02-17 22:30:21910
[email protected]eaa7dd182010-12-14 11:09:00911 InitializeInstalledExtensionService(pref_path, source_install_dir);
[email protected]54cb3c92009-02-17 22:30:21912
[email protected]f484f8d52014-06-12 08:38:18913 service()->Init();
[email protected]54cb3c92009-02-17 22:30:21914
[email protected]a9b00ac2009-06-25 21:03:23915 ASSERT_EQ(4u, GetErrors().size());
916 ASSERT_EQ(0u, loaded_.size());
[email protected]25b34332009-06-05 21:53:19917
brettwd97eede2015-07-06 22:09:00918 EXPECT_TRUE(base::MatchPattern(base::UTF16ToUTF8(GetErrors()[0]),
fatalerr2443c2df2014-10-31 19:14:35919 l10n_util::GetStringUTF8(IDS_EXTENSIONS_LOAD_ERROR_MESSAGE) + " *. " +
[email protected]0c3c9732013-09-16 08:53:41920 extensions::manifest_errors::kManifestUnreadable)) <<
[email protected]04338722013-12-24 23:18:05921 base::UTF16ToUTF8(GetErrors()[0]);
[email protected]8d6d9ff2009-02-20 08:14:39922
brettwd97eede2015-07-06 22:09:00923 EXPECT_TRUE(base::MatchPattern(base::UTF16ToUTF8(GetErrors()[1]),
fatalerr2443c2df2014-10-31 19:14:35924 l10n_util::GetStringUTF8(IDS_EXTENSIONS_LOAD_ERROR_MESSAGE) + " *. " +
[email protected]0c3c9732013-09-16 08:53:41925 extensions::manifest_errors::kManifestUnreadable)) <<
[email protected]04338722013-12-24 23:18:05926 base::UTF16ToUTF8(GetErrors()[1]);
[email protected]8d6d9ff2009-02-20 08:14:39927
brettwd97eede2015-07-06 22:09:00928 EXPECT_TRUE(base::MatchPattern(base::UTF16ToUTF8(GetErrors()[2]),
fatalerr2443c2df2014-10-31 19:14:35929 l10n_util::GetStringUTF8(IDS_EXTENSIONS_LOAD_ERROR_MESSAGE) + " *. " +
[email protected]0c3c9732013-09-16 08:53:41930 extensions::manifest_errors::kMissingFile)) <<
[email protected]04338722013-12-24 23:18:05931 base::UTF16ToUTF8(GetErrors()[2]);
[email protected]a9b00ac2009-06-25 21:03:23932
brettwd97eede2015-07-06 22:09:00933 EXPECT_TRUE(base::MatchPattern(base::UTF16ToUTF8(GetErrors()[3]),
fatalerr2443c2df2014-10-31 19:14:35934 l10n_util::GetStringUTF8(IDS_EXTENSIONS_LOAD_ERROR_MESSAGE) + " *. " +
[email protected]0c3c9732013-09-16 08:53:41935 extensions::manifest_errors::kManifestUnreadable)) <<
[email protected]04338722013-12-24 23:18:05936 base::UTF16ToUTF8(GetErrors()[3]);
thestig4b36dd32014-10-31 20:30:19937}
[email protected]54cb3c92009-02-17 22:30:21938
[email protected]9f4e4f082013-06-21 07:11:19939// Test various cases for delayed install because of missing imports.
940TEST_F(ExtensionServiceTest, PendingImports) {
941 InitPluginService();
942
[email protected]f484f8d52014-06-12 08:38:18943 base::FilePath source_install_dir =
944 data_dir().AppendASCII("pending_updates_with_imports").AppendASCII(
945 "Extensions");
[email protected]e96a0602014-02-15 08:27:42946 base::FilePath pref_path =
947 source_install_dir.DirName().Append(chrome::kPreferencesFilename);
[email protected]9f4e4f082013-06-21 07:11:19948
949 InitializeInstalledExtensionService(pref_path, source_install_dir);
950
951 // Verify there are no pending extensions initially.
[email protected]f484f8d52014-06-12 08:38:18952 EXPECT_FALSE(service()->pending_extension_manager()->HasPendingExtensions());
[email protected]9f4e4f082013-06-21 07:11:19953
[email protected]f484f8d52014-06-12 08:38:18954 service()->Init();
[email protected]9f4e4f082013-06-21 07:11:19955 // Wait for GarbageCollectExtensions task to complete.
Gabriel Charette01507a22017-09-27 21:30:08956 content::RunAllTasksUntilIdle();
[email protected]9f4e4f082013-06-21 07:11:19957
958 // These extensions are used by the extensions we test below, they must be
959 // installed.
[email protected]f484f8d52014-06-12 08:38:18960 EXPECT_TRUE(base::PathExists(extensions_install_dir().AppendASCII(
[email protected]9f4e4f082013-06-21 07:11:19961 "bjafgdebaacbbbecmhlhpofkepfkgcpa/1.0")));
[email protected]f484f8d52014-06-12 08:38:18962 EXPECT_TRUE(base::PathExists(extensions_install_dir().AppendASCII(
[email protected]9f4e4f082013-06-21 07:11:19963 "hpiknbiabeeppbpihjehijgoemciehgk/2")));
964
965 // Each of these extensions should have been rejected because of dependencies
966 // that cannot be satisfied.
[email protected]f484f8d52014-06-12 08:38:18967 ExtensionPrefs* prefs = ExtensionPrefs::Get(profile());
[email protected]9f4e4f082013-06-21 07:11:19968 EXPECT_FALSE(
969 prefs->GetDelayedInstallInfo("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"));
970 EXPECT_FALSE(
971 prefs->GetInstalledExtensionInfo("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"));
972 EXPECT_FALSE(
973 prefs->GetDelayedInstallInfo("bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"));
974 EXPECT_FALSE(
975 prefs->GetInstalledExtensionInfo("bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"));
976 EXPECT_FALSE(
977 prefs->GetDelayedInstallInfo("cccccccccccccccccccccccccccccccc"));
978 EXPECT_FALSE(
979 prefs->GetInstalledExtensionInfo("cccccccccccccccccccccccccccccccc"));
980
981 // Make sure the import started for the extension with a dependency.
982 EXPECT_TRUE(
983 prefs->GetDelayedInstallInfo("behllobkkfkfnphdnhnkndlbkcpglgmj"));
984 EXPECT_EQ(ExtensionPrefs::DELAY_REASON_WAIT_FOR_IMPORTS,
985 prefs->GetDelayedInstallReason("behllobkkfkfnphdnhnkndlbkcpglgmj"));
986
[email protected]f484f8d52014-06-12 08:38:18987 EXPECT_FALSE(base::PathExists(extensions_install_dir().AppendASCII(
[email protected]9f4e4f082013-06-21 07:11:19988 "behllobkkfkfnphdnhnkndlbkcpglgmj/1.0.0.0")));
989
[email protected]f484f8d52014-06-12 08:38:18990 EXPECT_TRUE(service()->pending_extension_manager()->HasPendingExtensions());
[email protected]9f4e4f082013-06-21 07:11:19991 std::string pending_id("eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee");
[email protected]f484f8d52014-06-12 08:38:18992 EXPECT_TRUE(service()->pending_extension_manager()->IsIdPending(pending_id));
[email protected]9f4e4f082013-06-21 07:11:19993 // Remove it because we are not testing the pending extension manager's
994 // ability to download and install extensions.
[email protected]f484f8d52014-06-12 08:38:18995 EXPECT_TRUE(service()->pending_extension_manager()->Remove(pending_id));
[email protected]9f4e4f082013-06-21 07:11:19996}
997
rdevlin.cronin7217be52017-03-24 20:47:05998// Tests that installation fails with extensions disabled.
999TEST_F(ExtensionServiceTest, InstallExtensionsWithExtensionsDisabled) {
1000 InitializeExtensionServiceWithExtensionsDisabled();
1001 base::FilePath path = data_dir().AppendASCII("good.crx");
1002 InstallCRX(path, INSTALL_FAILED);
1003}
1004
[email protected]d7eaf572009-07-01 21:57:001005// Test installing extensions. This test tries to install few extensions using
1006// crx files. If you need to change those crx files, feel free to repackage
1007// them, throw away the key used and change the id's above.
[email protected]d9a61e12012-11-14 02:43:471008TEST_F(ExtensionServiceTest, InstallExtension) {
[email protected]eaa7dd182010-12-14 11:09:001009 InitializeEmptyExtensionService();
[email protected]25b34332009-06-05 21:53:191010 ValidatePrefKeyCount(0);
1011
[email protected]e2eb43112009-05-29 21:19:541012 // A simple extension that should install without error.
rdevlin.cronin7217be52017-03-24 20:47:051013 base::FilePath path = data_dir().AppendASCII("good.crx");
[email protected]8f512c72011-11-22 21:02:501014 InstallCRX(path, INSTALL_NEW);
[email protected]cc655912009-01-29 23:19:191015 // TODO(erikkay): verify the contents of the installed extension.
1016
[email protected]25b34332009-06-05 21:53:191017 int pref_count = 0;
1018 ValidatePrefKeyCount(++pref_count);
[email protected]e2194742010-08-12 05:54:341019 ValidateIntegerPref(good_crx, "state", Extension::ENABLED);
[email protected]1d5e58b2013-01-31 08:41:401020 ValidateIntegerPref(good_crx, "location", Manifest::INTERNAL);
[email protected]25b34332009-06-05 21:53:191021
[email protected]902f7cd2009-05-22 19:02:191022 // An extension with page actions.
[email protected]f484f8d52014-06-12 08:38:181023 path = data_dir().AppendASCII("page_action.crx");
[email protected]8f512c72011-11-22 21:02:501024 InstallCRX(path, INSTALL_NEW);
[email protected]25b34332009-06-05 21:53:191025 ValidatePrefKeyCount(++pref_count);
[email protected]e2194742010-08-12 05:54:341026 ValidateIntegerPref(page_action, "state", Extension::ENABLED);
[email protected]1d5e58b2013-01-31 08:41:401027 ValidateIntegerPref(page_action, "location", Manifest::INTERNAL);
[email protected]902f7cd2009-05-22 19:02:191028
[email protected]9f1087e2009-06-15 17:29:321029 // Bad signature.
[email protected]f484f8d52014-06-12 08:38:181030 path = data_dir().AppendASCII("bad_signature.crx");
[email protected]8f512c72011-11-22 21:02:501031 InstallCRX(path, INSTALL_FAILED);
[email protected]d7eaf572009-07-01 21:57:001032 ValidatePrefKeyCount(pref_count);
[email protected]fbcc40302009-06-12 20:45:451033
[email protected]cc655912009-01-29 23:19:191034 // 0-length extension file.
[email protected]f484f8d52014-06-12 08:38:181035 path = data_dir().AppendASCII("not_an_extension.crx");
[email protected]8f512c72011-11-22 21:02:501036 InstallCRX(path, INSTALL_FAILED);
[email protected]25b34332009-06-05 21:53:191037 ValidatePrefKeyCount(pref_count);
[email protected]cc655912009-01-29 23:19:191038
1039 // Bad magic number.
[email protected]f484f8d52014-06-12 08:38:181040 path = data_dir().AppendASCII("bad_magic.crx");
[email protected]8f512c72011-11-22 21:02:501041 InstallCRX(path, INSTALL_FAILED);
[email protected]25b34332009-06-05 21:53:191042 ValidatePrefKeyCount(pref_count);
[email protected]cc655912009-01-29 23:19:191043
[email protected]622813ee02013-08-21 21:00:291044 // Packed extensions may have folders or files that have underscores.
1045 // This will only cause a warning, rather than a fatal error.
[email protected]f484f8d52014-06-12 08:38:181046 path = data_dir().AppendASCII("bad_underscore.crx");
[email protected]622813ee02013-08-21 21:00:291047 InstallCRX(path, INSTALL_NEW);
1048 ValidatePrefKeyCount(++pref_count);
[email protected]99872e32009-09-25 22:02:491049
[email protected]5fda91942013-09-24 06:47:531050 // A test for an extension with a 2048-bit public key.
[email protected]f484f8d52014-06-12 08:38:181051 path = data_dir().AppendASCII("good2048.crx");
[email protected]5fda91942013-09-24 06:47:531052 InstallCRX(path, INSTALL_NEW);
1053 ValidatePrefKeyCount(++pref_count);
1054 ValidateIntegerPref(good2048, "state", Extension::ENABLED);
1055 ValidateIntegerPref(good2048, "location", Manifest::INTERNAL);
1056
[email protected]cc655912009-01-29 23:19:191057 // TODO(erikkay): add more tests for many of the failure cases.
1058 // TODO(erikkay): add tests for upgrade cases.
1059}
1060
[email protected]b8e45df2014-06-11 19:32:361061struct MockExtensionRegistryObserver
1062 : public extensions::ExtensionRegistryObserver {
dchengae36a4a2014-10-21 12:36:361063 void OnExtensionWillBeInstalled(content::BrowserContext* browser_context,
1064 const Extension* extension,
1065 bool is_update,
dchengae36a4a2014-10-21 12:36:361066 const std::string& old_name) override {
[email protected]0d55b9a2013-07-19 10:40:261067 last_extension_installed = extension->id();
1068 }
1069
dchengae36a4a2014-10-21 12:36:361070 void OnExtensionUninstalled(content::BrowserContext* browser_context,
1071 const Extension* extension,
1072 extensions::UninstallReason reason) override {
[email protected]0d55b9a2013-07-19 10:40:261073 last_extension_uninstalled = extension->id();
1074 }
1075
[email protected]0d55b9a2013-07-19 10:40:261076 std::string last_extension_installed;
1077 std::string last_extension_uninstalled;
1078};
1079
[email protected]b8e45df2014-06-11 19:32:361080// Test that correct notifications are sent to ExtensionRegistryObserver on
[email protected]0d55b9a2013-07-19 10:40:261081// extension install and uninstall.
1082TEST_F(ExtensionServiceTest, InstallObserverNotified) {
1083 InitializeEmptyExtensionService();
1084
[email protected]b8e45df2014-06-11 19:32:361085 extensions::ExtensionRegistry* registry(
[email protected]f484f8d52014-06-12 08:38:181086 extensions::ExtensionRegistry::Get(profile()));
[email protected]b8e45df2014-06-11 19:32:361087 MockExtensionRegistryObserver observer;
1088 registry->AddObserver(&observer);
[email protected]0d55b9a2013-07-19 10:40:261089
1090 // A simple extension that should install without error.
1091 ASSERT_TRUE(observer.last_extension_installed.empty());
[email protected]f484f8d52014-06-12 08:38:181092 base::FilePath path = data_dir().AppendASCII("good.crx");
[email protected]0d55b9a2013-07-19 10:40:261093 InstallCRX(path, INSTALL_NEW);
1094 ASSERT_EQ(good_crx, observer.last_extension_installed);
1095
1096 // Uninstall the extension.
1097 ASSERT_TRUE(observer.last_extension_uninstalled.empty());
Devlin Cronin6fd1cd62017-12-05 19:13:571098 UninstallExtension(good_crx);
[email protected]0d55b9a2013-07-19 10:40:261099 ASSERT_EQ(good_crx, observer.last_extension_uninstalled);
1100
[email protected]b8e45df2014-06-11 19:32:361101 registry->RemoveObserver(&observer);
[email protected]0d55b9a2013-07-19 10:40:261102}
1103
[email protected]1bf73cc32011-10-26 22:38:311104// Tests that flags passed to OnExternalExtensionFileFound() make it to the
1105// extension object.
[email protected]d9a61e12012-11-14 02:43:471106TEST_F(ExtensionServiceTest, InstallingExternalExtensionWithFlags) {
[email protected]a12ce8b22012-01-17 18:40:531107 const char kPrefFromBookmark[] = "from_bookmark";
1108
[email protected]1bf73cc32011-10-26 22:38:311109 InitializeEmptyExtensionService();
1110
[email protected]f484f8d52014-06-12 08:38:181111 base::FilePath path = data_dir().AppendASCII("good.crx");
[email protected]1bf73cc32011-10-26 22:38:311112
[email protected]a12ce8b22012-01-17 18:40:531113 // Register and install an external extension.
lazyboy8a08c9d2017-04-11 19:53:221114 std::string version_str = "1.0.0.0";
1115 std::unique_ptr<ExternalInstallInfoFile> info = CreateExternalExtension(
1116 good_crx, version_str, path, Manifest::EXTERNAL_PREF,
1117 Extension::FROM_BOOKMARK);
1118 MockExternalProvider* provider =
1119 AddMockExternalProvider(Manifest::EXTERNAL_POLICY_DOWNLOAD);
1120 provider->UpdateOrAddExtension(std::move(info));
1121 WaitForExternalExtensionInstalled();
[email protected]1bf73cc32011-10-26 22:38:311122
[email protected]f484f8d52014-06-12 08:38:181123 const Extension* extension = service()->GetExtensionById(good_crx, false);
[email protected]1bf73cc32011-10-26 22:38:311124 ASSERT_TRUE(extension);
[email protected]a12ce8b22012-01-17 18:40:531125 ASSERT_TRUE(extension->from_bookmark());
[email protected]3f2a2fa2013-09-24 02:55:251126 ASSERT_TRUE(ValidateBooleanPref(good_crx, kPrefFromBookmark, true));
[email protected]a12ce8b22012-01-17 18:40:531127
1128 // Upgrade to version 2.0, the flag should be preserved.
[email protected]f484f8d52014-06-12 08:38:181129 path = data_dir().AppendASCII("good2.crx");
[email protected]a12ce8b22012-01-17 18:40:531130 UpdateExtension(good_crx, path, ENABLED);
[email protected]3f2a2fa2013-09-24 02:55:251131 ASSERT_TRUE(ValidateBooleanPref(good_crx, kPrefFromBookmark, true));
[email protected]f484f8d52014-06-12 08:38:181132 extension = service()->GetExtensionById(good_crx, false);
[email protected]a12ce8b22012-01-17 18:40:531133 ASSERT_TRUE(extension);
1134 ASSERT_TRUE(extension->from_bookmark());
[email protected]1bf73cc32011-10-26 22:38:311135}
1136
[email protected]79c833b52011-04-05 18:31:011137// Test the handling of Extension::EXTERNAL_EXTENSION_UNINSTALLED
[email protected]d9a61e12012-11-14 02:43:471138TEST_F(ExtensionServiceTest, UninstallingExternalExtensions) {
[email protected]eaa7dd182010-12-14 11:09:001139 InitializeEmptyExtensionService();
[email protected]683d0702010-12-06 16:25:571140
[email protected]f484f8d52014-06-12 08:38:181141 base::FilePath path = data_dir().AppendASCII("good.crx");
[email protected]683d0702010-12-06 16:25:571142
lazyboy8a08c9d2017-04-11 19:53:221143 std::string version_str = "1.0.0.0";
[email protected]683d0702010-12-06 16:25:571144 // Install an external extension.
lazyboy8a08c9d2017-04-11 19:53:221145 std::unique_ptr<ExternalInstallInfoFile> info =
1146 CreateExternalExtension(good_crx, version_str, path,
1147 Manifest::EXTERNAL_PREF, Extension::NO_FLAGS);
1148 MockExternalProvider* provider =
1149 AddMockExternalProvider(Manifest::EXTERNAL_PREF);
1150 provider->UpdateOrAddExtension(std::move(info));
1151 WaitForExternalExtensionInstalled();
[email protected]6d057a0c2013-07-09 21:12:071152
[email protected]f484f8d52014-06-12 08:38:181153 ASSERT_TRUE(service()->GetExtensionById(good_crx, false));
[email protected]683d0702010-12-06 16:25:571154
1155 // Uninstall it and check that its killbit gets set.
Devlin Cronin6fd1cd62017-12-05 19:13:571156 UninstallExtension(good_crx);
Devlin Cronin55fab54d2017-12-16 02:27:191157 ExtensionPrefs* prefs = ExtensionPrefs::Get(profile());
1158 EXPECT_TRUE(prefs->IsExternalExtensionUninstalled(good_crx));
[email protected]683d0702010-12-06 16:25:571159
1160 // Try to re-install it externally. This should fail because of the killbit.
lazyboy8a08c9d2017-04-11 19:53:221161 info = CreateExternalExtension(good_crx, version_str, path,
1162 Manifest::EXTERNAL_PREF, Extension::NO_FLAGS);
1163 provider->UpdateOrAddExtension(std::move(info));
Gabriel Charette01507a22017-09-27 21:30:081164 content::RunAllTasksUntilIdle();
Devlin Cronin55fab54d2017-12-16 02:27:191165 ASSERT_FALSE(service()->GetExtensionById(good_crx, false));
1166 EXPECT_TRUE(prefs->IsExternalExtensionUninstalled(good_crx));
[email protected]683d0702010-12-06 16:25:571167
lazyboy8a08c9d2017-04-11 19:53:221168 std::string newer_version = "1.0.0.1";
[email protected]683d0702010-12-06 16:25:571169 // Repeat the same thing with a newer version of the extension.
[email protected]f484f8d52014-06-12 08:38:181170 path = data_dir().AppendASCII("good2.crx");
lazyboy8a08c9d2017-04-11 19:53:221171 info = CreateExternalExtension(good_crx, newer_version, path,
1172 Manifest::EXTERNAL_PREF, Extension::NO_FLAGS);
1173 provider->UpdateOrAddExtension(std::move(info));
Gabriel Charette01507a22017-09-27 21:30:081174 content::RunAllTasksUntilIdle();
Devlin Cronin55fab54d2017-12-16 02:27:191175 ASSERT_FALSE(service()->GetExtensionById(good_crx, false));
1176 EXPECT_TRUE(prefs->IsExternalExtensionUninstalled(good_crx));
[email protected]683d0702010-12-06 16:25:571177
1178 // Try adding the same extension from an external update URL.
[email protected]f484f8d52014-06-12 08:38:181179 ASSERT_FALSE(service()->pending_extension_manager()->AddFromExternalUpdateUrl(
[email protected]683d0702010-12-06 16:25:571180 good_crx,
[email protected]d8fd0fd2014-03-24 13:16:061181 std::string(),
[email protected]683d0702010-12-06 16:25:571182 GURL("http:://fake.update/url"),
[email protected]464213a2013-10-15 01:06:481183 Manifest::EXTERNAL_PREF_DOWNLOAD,
1184 Extension::NO_FLAGS,
1185 false));
[email protected]b2907fd2011-03-25 16:43:371186
emaxx6699afb682016-10-10 21:22:131187 // Installation of the same extension through the policy should be successful.
1188 ASSERT_TRUE(service()->pending_extension_manager()->AddFromExternalUpdateUrl(
1189 good_crx,
1190 std::string(),
1191 GURL("http:://fake.update/url"),
1192 Manifest::EXTERNAL_POLICY_DOWNLOAD,
1193 Extension::NO_FLAGS,
1194 false));
1195 EXPECT_TRUE(service()->pending_extension_manager()->IsIdPending(good_crx));
1196 EXPECT_TRUE(service()->pending_extension_manager()->Remove(good_crx));
1197
[email protected]f484f8d52014-06-12 08:38:181198 ASSERT_FALSE(service()->pending_extension_manager()->IsIdPending(good_crx));
[email protected]683d0702010-12-06 16:25:571199}
1200
[email protected]0f48fca2011-05-19 18:46:351201// Test that uninstalling an external extension does not crash when
1202// the extension could not be loaded.
1203// This extension shown in preferences file requires an experimental permission.
1204// It could not be loaded without such permission.
[email protected]d9a61e12012-11-14 02:43:471205TEST_F(ExtensionServiceTest, UninstallingNotLoadedExtension) {
[email protected]f484f8d52014-06-12 08:38:181206 base::FilePath source_install_dir =
1207 data_dir().AppendASCII("good").AppendASCII("Extensions");
[email protected]0f48fca2011-05-19 18:46:351208 // The preference contains an external extension
1209 // that requires 'experimental' permission.
[email protected]650b2d52013-02-10 03:41:451210 base::FilePath pref_path = source_install_dir
[email protected]0f48fca2011-05-19 18:46:351211 .DirName()
1212 .AppendASCII("PreferencesExperimental");
1213
1214 // Aforementioned extension will not be loaded if
1215 // there is no '--enable-experimental-extension-apis' command line flag.
1216 InitializeInstalledExtensionService(pref_path, source_install_dir);
1217
[email protected]f484f8d52014-06-12 08:38:181218 service()->Init();
[email protected]0f48fca2011-05-19 18:46:351219
1220 // Check and try to uninstall it.
1221 // If we don't check whether the extension is loaded before we uninstall it
1222 // in CheckExternalUninstall, a crash will happen here because we will get or
1223 // dereference a NULL pointer (extension) inside UninstallExtension.
lazyboya00eafc2017-04-08 00:57:191224 MockExternalProvider provider(NULL, Manifest::EXTERNAL_REGISTRY);
[email protected]f484f8d52014-06-12 08:38:181225 service()->OnExternalProviderReady(&provider);
[email protected]0f48fca2011-05-19 18:46:351226}
1227
[email protected]604322d2011-03-22 16:51:561228// Test that external extensions with incorrect IDs are not installed.
[email protected]d9a61e12012-11-14 02:43:471229TEST_F(ExtensionServiceTest, FailOnWrongId) {
[email protected]604322d2011-03-22 16:51:561230 InitializeEmptyExtensionService();
[email protected]f484f8d52014-06-12 08:38:181231 base::FilePath path = data_dir().AppendASCII("good.crx");
[email protected]604322d2011-03-22 16:51:561232
lazyboy8a08c9d2017-04-11 19:53:221233 std::string version_str = "1.0.0.0";
[email protected]604322d2011-03-22 16:51:561234
1235 const std::string wrong_id = all_zero;
1236 const std::string correct_id = good_crx;
1237 ASSERT_NE(correct_id, wrong_id);
1238
lazyboy8a08c9d2017-04-11 19:53:221239 MockExternalProvider* provider =
1240 AddMockExternalProvider(Manifest::EXTERNAL_PREF);
1241
[email protected]604322d2011-03-22 16:51:561242 // Install an external extension with an ID from the external
1243 // source that is not equal to the ID in the extension manifest.
lazyboy8a08c9d2017-04-11 19:53:221244 std::unique_ptr<ExternalInstallInfoFile> info =
1245 CreateExternalExtension(wrong_id, version_str, path,
1246 Manifest::EXTERNAL_PREF, Extension::NO_FLAGS);
1247 provider->UpdateOrAddExtension(std::move(info));
1248 WaitForExternalExtensionInstalled();
[email protected]f484f8d52014-06-12 08:38:181249 ASSERT_FALSE(service()->GetExtensionById(good_crx, false));
[email protected]604322d2011-03-22 16:51:561250
1251 // Try again with the right ID. Expect success.
lazyboy8a08c9d2017-04-11 19:53:221252 info = CreateExternalExtension(correct_id, version_str, path,
1253 Manifest::EXTERNAL_PREF, Extension::NO_FLAGS);
1254 provider->UpdateOrAddExtension(std::move(info));
1255 WaitForExternalExtensionInstalled();
[email protected]f484f8d52014-06-12 08:38:181256 ASSERT_TRUE(service()->GetExtensionById(good_crx, false));
[email protected]604322d2011-03-22 16:51:561257}
1258
1259// Test that external extensions with incorrect versions are not installed.
[email protected]d9a61e12012-11-14 02:43:471260TEST_F(ExtensionServiceTest, FailOnWrongVersion) {
[email protected]604322d2011-03-22 16:51:561261 InitializeEmptyExtensionService();
[email protected]f484f8d52014-06-12 08:38:181262 base::FilePath path = data_dir().AppendASCII("good.crx");
lazyboy8a08c9d2017-04-11 19:53:221263 MockExternalProvider* provider =
1264 AddMockExternalProvider(Manifest::EXTERNAL_PREF);
[email protected]604322d2011-03-22 16:51:561265
1266 // Install an external extension with a version from the external
1267 // source that is not equal to the version in the extension manifest.
lazyboy8a08c9d2017-04-11 19:53:221268 std::string wrong_version_str = "1.2.3.4";
1269 std::unique_ptr<ExternalInstallInfoFile> wrong_info =
1270 CreateExternalExtension(good_crx, wrong_version_str, path,
1271 Manifest::EXTERNAL_PREF, Extension::NO_FLAGS);
1272 provider->UpdateOrAddExtension(std::move(wrong_info));
1273 WaitForExternalExtensionInstalled();
[email protected]f484f8d52014-06-12 08:38:181274 ASSERT_FALSE(service()->GetExtensionById(good_crx, false));
[email protected]604322d2011-03-22 16:51:561275
1276 // Try again with the right version. Expect success.
[email protected]f484f8d52014-06-12 08:38:181277 service()->pending_extension_manager()->Remove(good_crx);
lazyboy8a08c9d2017-04-11 19:53:221278 std::unique_ptr<ExternalInstallInfoFile> correct_info =
1279 CreateExternalExtension(good_crx, "1.0.0.0", path,
1280 Manifest::EXTERNAL_PREF, Extension::NO_FLAGS);
1281 provider->UpdateOrAddExtension(std::move(correct_info));
1282 WaitForExternalExtensionInstalled();
[email protected]f484f8d52014-06-12 08:38:181283 ASSERT_TRUE(service()->GetExtensionById(good_crx, false));
[email protected]604322d2011-03-22 16:51:561284}
1285
[email protected]da0aa3b2009-12-06 21:41:031286// Install a user script (they get converted automatically to an extension)
[email protected]d9a61e12012-11-14 02:43:471287TEST_F(ExtensionServiceTest, InstallUserScript) {
[email protected]da0aa3b2009-12-06 21:41:031288 // The details of script conversion are tested elsewhere, this just tests
[email protected]eaa7dd182010-12-14 11:09:001289 // integration with ExtensionService.
1290 InitializeEmptyExtensionService();
[email protected]da0aa3b2009-12-06 21:41:031291
[email protected]f484f8d52014-06-12 08:38:181292 base::FilePath path = data_dir().AppendASCII("user_script_basic.user.js");
[email protected]da0aa3b2009-12-06 21:41:031293
[email protected]7567484142013-07-11 17:36:071294 ASSERT_TRUE(base::PathExists(path));
[email protected]f484f8d52014-06-12 08:38:181295 scoped_refptr<CrxInstaller> installer(CrxInstaller::CreateSilent(service()));
[email protected]0d3e4a22011-06-23 19:02:521296 installer->set_allow_silent_install(true);
[email protected]6dfbbf82010-03-12 23:09:161297 installer->InstallUserScript(
[email protected]da0aa3b2009-12-06 21:41:031298 path,
[email protected]6dfbbf82010-03-12 23:09:161299 GURL("http://www.aaronboodman.com/scripts/user_script_basic.user.js"));
[email protected]da0aa3b2009-12-06 21:41:031300
Gabriel Charette01507a22017-09-27 21:30:081301 content::RunAllTasksUntilIdle();
[email protected]d2065e062013-12-12 23:49:521302 std::vector<base::string16> errors = GetErrors();
[email protected]da0aa3b2009-12-06 21:41:031303 EXPECT_TRUE(installed_) << "Nothing was installed.";
[email protected]41bb80bd2013-05-03 10:56:021304 EXPECT_FALSE(was_update_) << path.value();
[email protected]da0aa3b2009-12-06 21:41:031305 ASSERT_EQ(1u, loaded_.size()) << "Nothing was loaded.";
brettwd94a22142015-07-15 05:19:261306 EXPECT_EQ(0u, errors.size())
1307 << "There were errors: "
1308 << base::JoinString(errors, base::ASCIIToUTF16(","));
[email protected]f484f8d52014-06-12 08:38:181309 EXPECT_TRUE(service()->GetExtensionById(loaded_[0]->id(), false))
1310 << path.value();
[email protected]da0aa3b2009-12-06 21:41:031311
1312 installed_ = NULL;
[email protected]41bb80bd2013-05-03 10:56:021313 was_update_ = false;
[email protected]da0aa3b2009-12-06 21:41:031314 loaded_.clear();
Devlin Cronin9722a722017-12-16 03:35:101315 extensions::LoadErrorReporter::GetInstance()->ClearErrors();
[email protected]da0aa3b2009-12-06 21:41:031316}
1317
[email protected]3c4abc82012-10-22 22:25:541318// Extensions don't install during shutdown.
[email protected]d9a61e12012-11-14 02:43:471319TEST_F(ExtensionServiceTest, InstallExtensionDuringShutdown) {
[email protected]3c4abc82012-10-22 22:25:541320 InitializeEmptyExtensionService();
1321
1322 // Simulate shutdown.
[email protected]f484f8d52014-06-12 08:38:181323 service()->set_browser_terminating_for_test(true);
[email protected]3c4abc82012-10-22 22:25:541324
[email protected]f484f8d52014-06-12 08:38:181325 base::FilePath path = data_dir().AppendASCII("good.crx");
1326 scoped_refptr<CrxInstaller> installer(CrxInstaller::CreateSilent(service()));
[email protected]3c4abc82012-10-22 22:25:541327 installer->set_allow_silent_install(true);
1328 installer->InstallCrx(path);
Gabriel Charette01507a22017-09-27 21:30:081329 content::RunAllTasksUntilIdle();
[email protected]3c4abc82012-10-22 22:25:541330
1331 EXPECT_FALSE(installed_) << "Extension installed during shutdown.";
1332 ASSERT_EQ(0u, loaded_.size()) << "Extension loaded during shutdown.";
1333}
1334
[email protected]8d888c12010-11-30 00:00:251335// This tests that the granted permissions preferences are correctly set when
1336// installing an extension.
[email protected]d9a61e12012-11-14 02:43:471337TEST_F(ExtensionServiceTest, GrantedPermissions) {
[email protected]eaa7dd182010-12-14 11:09:001338 InitializeEmptyExtensionService();
[email protected]f484f8d52014-06-12 08:38:181339 base::FilePath path = data_dir().AppendASCII("permissions");
[email protected]8d888c12010-11-30 00:00:251340
[email protected]650b2d52013-02-10 03:41:451341 base::FilePath pem_path = path.AppendASCII("unknown.pem");
[email protected]8d888c12010-11-30 00:00:251342 path = path.AppendASCII("unknown");
1343
[email protected]7567484142013-07-11 17:36:071344 ASSERT_TRUE(base::PathExists(pem_path));
1345 ASSERT_TRUE(base::PathExists(path));
[email protected]8d888c12010-11-30 00:00:251346
[email protected]f484f8d52014-06-12 08:38:181347 ExtensionPrefs* prefs = ExtensionPrefs::Get(profile());
[email protected]8d888c12010-11-30 00:00:251348
[email protected]c2e66e12012-06-27 06:27:061349 APIPermissionSet expected_api_perms;
[email protected]cced75a2011-05-20 08:31:121350 URLPatternSet expected_host_perms;
[email protected]8d888c12010-11-30 00:00:251351
1352 // Make sure there aren't any granted permissions before the
1353 // extension is installed.
rdevlin.cronine2d0fd02015-09-24 22:35:491354 EXPECT_FALSE(prefs->GetGrantedPermissions(permissions_crx).get());
[email protected]8d888c12010-11-30 00:00:251355
[email protected]8f512c72011-11-22 21:02:501356 const Extension* extension = PackAndInstallCRX(path, pem_path, INSTALL_NEW);
[email protected]8d888c12010-11-30 00:00:251357
1358 EXPECT_EQ(0u, GetErrors().size());
[email protected]f484f8d52014-06-12 08:38:181359 ASSERT_EQ(1u, registry()->enabled_extensions().size());
[email protected]8f512c72011-11-22 21:02:501360 EXPECT_EQ(permissions_crx, extension->id());
[email protected]8d888c12010-11-30 00:00:251361
[email protected]8d888c12010-11-30 00:00:251362 // Verify that the valid API permissions have been recognized.
[email protected]c2e66e12012-06-27 06:27:061363 expected_api_perms.insert(APIPermission::kTab);
[email protected]8d888c12010-11-30 00:00:251364
1365 AddPattern(&expected_host_perms, "http://*.google.com/*");
1366 AddPattern(&expected_host_perms, "https://*.google.com/*");
[email protected]d6a5c78c2010-12-07 05:18:151367 AddPattern(&expected_host_perms, "http://*.google.com.hk/*");
[email protected]8d888c12010-11-30 00:00:251368 AddPattern(&expected_host_perms, "http://www.example.com/*");
1369
dchengc963c7142016-04-08 03:55:221370 std::unique_ptr<const PermissionSet> known_perms =
rdevlin.cronine2d0fd02015-09-24 22:35:491371 prefs->GetGrantedPermissions(extension->id());
treibfef128d2016-06-02 09:14:511372 ASSERT_TRUE(known_perms.get());
[email protected]0d3e4a22011-06-23 19:02:521373 EXPECT_FALSE(known_perms->IsEmpty());
1374 EXPECT_EQ(expected_api_perms, known_perms->apis());
[email protected]06e8b8ff2011-07-13 15:03:471375 EXPECT_EQ(expected_host_perms, known_perms->effective_hosts());
[email protected]8d888c12010-11-30 00:00:251376}
1377
treibfef128d2016-06-02 09:14:511378// This tests that the granted permissions preferences are correctly set when
1379// updating an extension, and the extension is disabled in case of a permission
1380// escalation.
1381TEST_F(ExtensionServiceTest, GrantedPermissionsOnUpdate) {
1382 InitializeEmptyExtensionService();
1383 const base::FilePath base_path = data_dir().AppendASCII("permissions");
1384
1385 const base::FilePath pem_path = base_path.AppendASCII("update.pem");
1386 const base::FilePath path1 = base_path.AppendASCII("update_1");
1387 const base::FilePath path2 = base_path.AppendASCII("update_2");
1388 const base::FilePath path3 = base_path.AppendASCII("update_3");
1389 const base::FilePath path4 = base_path.AppendASCII("update_4");
treib6e51bca2016-06-15 10:26:031390 const base::FilePath path5 = base_path.AppendASCII("update_5");
treibfef128d2016-06-02 09:14:511391
1392 ASSERT_TRUE(base::PathExists(pem_path));
1393 ASSERT_TRUE(base::PathExists(path1));
1394 ASSERT_TRUE(base::PathExists(path2));
1395 ASSERT_TRUE(base::PathExists(path3));
1396 ASSERT_TRUE(base::PathExists(path4));
treib6e51bca2016-06-15 10:26:031397 ASSERT_TRUE(base::PathExists(path5));
treibfef128d2016-06-02 09:14:511398
1399 ExtensionPrefs* prefs = ExtensionPrefs::Get(profile());
1400
1401 // Install version 1, which has the kHistory permission.
1402 const Extension* extension = PackAndInstallCRX(path1, pem_path, INSTALL_NEW);
1403 const std::string id = extension->id();
1404
1405 EXPECT_EQ(0u, GetErrors().size());
1406 ASSERT_TRUE(registry()->enabled_extensions().Contains(id));
1407
1408 // Verify that the history permission has been recognized.
1409 APIPermissionSet expected_api_perms;
1410 expected_api_perms.insert(APIPermission::kHistory);
1411 {
1412 std::unique_ptr<const PermissionSet> known_perms =
1413 prefs->GetGrantedPermissions(id);
1414 ASSERT_TRUE(known_perms.get());
1415 EXPECT_EQ(expected_api_perms, known_perms->apis());
1416 }
1417
1418 // Update to version 2 that adds the kTopSites permission, which has a
1419 // separate message, but is implied by kHistory. The extension should remain
1420 // enabled.
1421 PackCRXAndUpdateExtension(id, path2, pem_path, ENABLED);
1422 extension = service()->GetInstalledExtension(id);
1423 ASSERT_TRUE(extension);
1424 EXPECT_TRUE(registry()->enabled_extensions().Contains(id));
1425
1426 // The extra permission should have been granted automatically.
1427 expected_api_perms.insert(APIPermission::kTopSites);
1428 {
1429 std::unique_ptr<const PermissionSet> known_perms =
1430 prefs->GetGrantedPermissions(id);
1431 ASSERT_TRUE(known_perms.get());
1432 EXPECT_EQ(expected_api_perms, known_perms->apis());
1433 }
1434
1435 // Update to version 3 that adds the kStorage permission, which does not have
1436 // a message. The extension should remain enabled.
1437 PackCRXAndUpdateExtension(id, path3, pem_path, ENABLED);
1438 extension = service()->GetInstalledExtension(id);
1439 ASSERT_TRUE(extension);
1440 EXPECT_TRUE(registry()->enabled_extensions().Contains(id));
1441
1442 // The extra permission should have been granted automatically.
1443 expected_api_perms.insert(APIPermission::kStorage);
1444 {
1445 std::unique_ptr<const PermissionSet> known_perms =
1446 prefs->GetGrantedPermissions(id);
1447 ASSERT_TRUE(known_perms.get());
1448 EXPECT_EQ(expected_api_perms, known_perms->apis());
1449 }
1450
1451 // Update to version 4 that adds the kNotifications permission, which has a
1452 // message and hence is considered a permission increase. Now the extension
1453 // should get disabled.
1454 PackCRXAndUpdateExtension(id, path4, pem_path, DISABLED);
1455 extension = service()->GetInstalledExtension(id);
1456 ASSERT_TRUE(extension);
1457 EXPECT_TRUE(registry()->disabled_extensions().Contains(id));
1458
1459 // No new permissions should have been granted.
1460 {
1461 std::unique_ptr<const PermissionSet> known_perms =
1462 prefs->GetGrantedPermissions(id);
1463 ASSERT_TRUE(known_perms.get());
1464 EXPECT_EQ(expected_api_perms, known_perms->apis());
1465 }
1466}
[email protected]be083862012-09-01 03:53:451467
treib6e51bca2016-06-15 10:26:031468TEST_F(ExtensionServiceTest, ReenableWithAllPermissionsGranted) {
1469 InitializeEmptyExtensionService();
1470 const base::FilePath base_path = data_dir().AppendASCII("permissions");
1471
1472 const base::FilePath pem_path = base_path.AppendASCII("update.pem");
1473 const base::FilePath path1 = base_path.AppendASCII("update_1");
1474 const base::FilePath path4 = base_path.AppendASCII("update_4");
1475 const base::FilePath path5 = base_path.AppendASCII("update_5");
1476
1477 ASSERT_TRUE(base::PathExists(pem_path));
1478 ASSERT_TRUE(base::PathExists(path1));
1479 ASSERT_TRUE(base::PathExists(path4));
1480 ASSERT_TRUE(base::PathExists(path5));
1481
1482 ExtensionPrefs* prefs = ExtensionPrefs::Get(profile());
1483
1484 // Install version 1, which has the kHistory permission.
1485 const Extension* extension = PackAndInstallCRX(path1, pem_path, INSTALL_NEW);
1486 const std::string id = extension->id();
1487
1488 EXPECT_EQ(0u, GetErrors().size());
1489 ASSERT_TRUE(registry()->enabled_extensions().Contains(id));
1490
1491 // Update to version 4 that adds the kNotifications permission, which has a
1492 // message and hence is considered a permission increase. The extension
1493 // should get disabled due to a permissions increase.
1494 PackCRXAndUpdateExtension(id, path4, pem_path, DISABLED);
1495 extension = service()->GetInstalledExtension(id);
1496 ASSERT_TRUE(extension);
1497 EXPECT_TRUE(registry()->disabled_extensions().Contains(id));
Minh X. Nguyen45479012017-08-18 21:35:361498 EXPECT_TRUE(prefs->HasDisableReason(
1499 id, extensions::disable_reason::DISABLE_PERMISSIONS_INCREASE));
treib6e51bca2016-06-15 10:26:031500
1501 // Update to version 5 that removes the kNotifications permission again.
1502 // The extension should get re-enabled.
1503 PackCRXAndUpdateExtension(id, path5, pem_path, ENABLED);
1504 extension = service()->GetInstalledExtension(id);
1505 ASSERT_TRUE(extension);
1506 EXPECT_TRUE(registry()->enabled_extensions().Contains(id));
1507}
1508
1509TEST_F(ExtensionServiceTest, ReenableWithAllPermissionsGrantedOnStartup) {
1510 InitializeEmptyExtensionService();
1511 const base::FilePath base_path = data_dir().AppendASCII("permissions");
1512
1513 const base::FilePath pem_path = base_path.AppendASCII("update.pem");
1514 const base::FilePath path1 = base_path.AppendASCII("update_1");
1515
1516 ASSERT_TRUE(base::PathExists(pem_path));
1517 ASSERT_TRUE(base::PathExists(path1));
1518
1519 // Install an extension which has the kHistory permission.
1520 const Extension* extension = PackAndInstallCRX(path1, pem_path, INSTALL_NEW);
1521 const std::string id = extension->id();
1522
1523 EXPECT_EQ(0u, GetErrors().size());
1524 ASSERT_TRUE(registry()->enabled_extensions().Contains(id));
1525
1526 ExtensionPrefs* prefs = ExtensionPrefs::Get(profile());
1527
1528 // Disable the extension due to a supposed permission increase, but retain its
1529 // granted permissions.
Minh X. Nguyen45479012017-08-18 21:35:361530 service()->DisableExtension(
1531 id, extensions::disable_reason::DISABLE_PERMISSIONS_INCREASE);
treib6e51bca2016-06-15 10:26:031532 EXPECT_TRUE(registry()->disabled_extensions().Contains(id));
Minh X. Nguyen45479012017-08-18 21:35:361533 EXPECT_TRUE(prefs->HasDisableReason(
1534 id, extensions::disable_reason::DISABLE_PERMISSIONS_INCREASE));
treib6e51bca2016-06-15 10:26:031535
1536 // Simulate a Chrome restart. Since the extension has all required
1537 // permissions, it should get re-enabled.
1538 service()->ReloadExtensionsForTest();
1539 EXPECT_TRUE(registry()->enabled_extensions().Contains(id));
Minh X. Nguyen45479012017-08-18 21:35:361540 EXPECT_FALSE(prefs->HasDisableReason(
1541 id, extensions::disable_reason::DISABLE_PERMISSIONS_INCREASE));
treib6e51bca2016-06-15 10:26:031542}
1543
1544TEST_F(ExtensionServiceTest,
1545 DontReenableWithAllPermissionsGrantedButOtherReason) {
1546 InitializeEmptyExtensionService();
1547 const base::FilePath base_path = data_dir().AppendASCII("permissions");
1548
1549 const base::FilePath pem_path = base_path.AppendASCII("update.pem");
1550 const base::FilePath path1 = base_path.AppendASCII("update_1");
1551 const base::FilePath path4 = base_path.AppendASCII("update_4");
1552 const base::FilePath path5 = base_path.AppendASCII("update_5");
1553
1554 ASSERT_TRUE(base::PathExists(pem_path));
1555 ASSERT_TRUE(base::PathExists(path1));
1556 ASSERT_TRUE(base::PathExists(path4));
1557 ASSERT_TRUE(base::PathExists(path5));
1558
1559 ExtensionPrefs* prefs = ExtensionPrefs::Get(profile());
1560
1561 // Install version 1, which has the kHistory permission.
1562 const Extension* extension = PackAndInstallCRX(path1, pem_path, INSTALL_NEW);
1563 const std::string id = extension->id();
1564
1565 EXPECT_EQ(0u, GetErrors().size());
1566 ASSERT_TRUE(registry()->enabled_extensions().Contains(id));
1567
1568 // Disable the extension.
Minh X. Nguyen45479012017-08-18 21:35:361569 service()->DisableExtension(id,
1570 extensions::disable_reason::DISABLE_USER_ACTION);
treib6e51bca2016-06-15 10:26:031571 EXPECT_TRUE(registry()->disabled_extensions().Contains(id));
Minh X. Nguyen45479012017-08-18 21:35:361572 EXPECT_TRUE(prefs->HasDisableReason(
1573 id, extensions::disable_reason::DISABLE_USER_ACTION));
treib6e51bca2016-06-15 10:26:031574
1575 // Update to version 4 that adds the kNotifications permission, which has a
1576 // message and hence is considered a permission increase. The extension
1577 // should get disabled due to a permissions increase.
1578 PackCRXAndUpdateExtension(id, path4, pem_path, DISABLED);
1579 extension = service()->GetInstalledExtension(id);
1580 ASSERT_TRUE(extension);
1581 EXPECT_TRUE(registry()->disabled_extensions().Contains(id));
Minh X. Nguyen45479012017-08-18 21:35:361582 EXPECT_TRUE(prefs->HasDisableReason(
1583 id, extensions::disable_reason::DISABLE_PERMISSIONS_INCREASE));
treib6e51bca2016-06-15 10:26:031584 // The USER_ACTION reason should also still be there.
Minh X. Nguyen45479012017-08-18 21:35:361585 EXPECT_TRUE(prefs->HasDisableReason(
1586 id, extensions::disable_reason::DISABLE_USER_ACTION));
treib6e51bca2016-06-15 10:26:031587
1588 // Update to version 5 that removes the kNotifications permission again.
1589 // The PERMISSIONS_INCREASE should be removed, but the extension should stay
1590 // disabled since USER_ACTION is still there.
1591 PackCRXAndUpdateExtension(id, path5, pem_path, DISABLED);
1592 extension = service()->GetInstalledExtension(id);
1593 ASSERT_TRUE(extension);
1594 EXPECT_TRUE(registry()->disabled_extensions().Contains(id));
Minh X. Nguyen45479012017-08-18 21:35:361595 EXPECT_EQ(extensions::disable_reason::DISABLE_USER_ACTION,
1596 prefs->GetDisableReasons(id));
treib6e51bca2016-06-15 10:26:031597}
1598
1599TEST_F(ExtensionServiceTest,
1600 DontReenableWithAllPermissionsGrantedOnStartupButOtherReason) {
1601 InitializeEmptyExtensionService();
1602 const base::FilePath base_path = data_dir().AppendASCII("permissions");
1603
1604 const base::FilePath pem_path = base_path.AppendASCII("update.pem");
1605 const base::FilePath path1 = base_path.AppendASCII("update_1");
1606
1607 ASSERT_TRUE(base::PathExists(pem_path));
1608 ASSERT_TRUE(base::PathExists(path1));
1609
1610 // Install an extension which has the kHistory permission.
1611 const Extension* extension = PackAndInstallCRX(path1, pem_path, INSTALL_NEW);
1612 const std::string id = extension->id();
1613
1614 EXPECT_EQ(0u, GetErrors().size());
1615 ASSERT_TRUE(registry()->enabled_extensions().Contains(id));
1616
1617 ExtensionPrefs* prefs = ExtensionPrefs::Get(profile());
1618
1619 // Disable the extension due to a supposed permission increase, but retain its
1620 // granted permissions.
1621 service()->DisableExtension(
Minh X. Nguyen45479012017-08-18 21:35:361622 id, extensions::disable_reason::DISABLE_PERMISSIONS_INCREASE |
1623 extensions::disable_reason::DISABLE_USER_ACTION);
treib6e51bca2016-06-15 10:26:031624 EXPECT_TRUE(registry()->disabled_extensions().Contains(id));
Minh X. Nguyen45479012017-08-18 21:35:361625 EXPECT_TRUE(prefs->HasDisableReason(
1626 id, extensions::disable_reason::DISABLE_PERMISSIONS_INCREASE));
treib6e51bca2016-06-15 10:26:031627
1628 // Simulate a Chrome restart. Since the extension has all required
1629 // permissions, the DISABLE_PERMISSIONS_INCREASE should get removed, but it
1630 // should stay disabled due to the remaining DISABLE_USER_ACTION reason.
1631 service()->ReloadExtensionsForTest();
1632 EXPECT_TRUE(registry()->disabled_extensions().Contains(id));
Minh X. Nguyen45479012017-08-18 21:35:361633 EXPECT_EQ(extensions::disable_reason::DISABLE_USER_ACTION,
1634 prefs->GetDisableReasons(id));
treib6e51bca2016-06-15 10:26:031635}
1636
[email protected]be083862012-09-01 03:53:451637#if !defined(OS_CHROMEOS)
1638// This tests that the granted permissions preferences are correctly set for
1639// default apps.
[email protected]d9a61e12012-11-14 02:43:471640TEST_F(ExtensionServiceTest, DefaultAppsGrantedPermissions) {
[email protected]be083862012-09-01 03:53:451641 InitializeEmptyExtensionService();
[email protected]f484f8d52014-06-12 08:38:181642 base::FilePath path = data_dir().AppendASCII("permissions");
[email protected]be083862012-09-01 03:53:451643
[email protected]650b2d52013-02-10 03:41:451644 base::FilePath pem_path = path.AppendASCII("unknown.pem");
[email protected]be083862012-09-01 03:53:451645 path = path.AppendASCII("unknown");
1646
[email protected]7567484142013-07-11 17:36:071647 ASSERT_TRUE(base::PathExists(pem_path));
1648 ASSERT_TRUE(base::PathExists(path));
[email protected]be083862012-09-01 03:53:451649
[email protected]f484f8d52014-06-12 08:38:181650 ExtensionPrefs* prefs = ExtensionPrefs::Get(profile());
[email protected]be083862012-09-01 03:53:451651
1652 APIPermissionSet expected_api_perms;
1653 URLPatternSet expected_host_perms;
1654
1655 // Make sure there aren't any granted permissions before the
1656 // extension is installed.
rdevlin.cronine2d0fd02015-09-24 22:35:491657 EXPECT_FALSE(prefs->GetGrantedPermissions(permissions_crx).get());
[email protected]be083862012-09-01 03:53:451658
1659 const Extension* extension = PackAndInstallCRX(
catmullings22bc2372016-11-02 19:59:351660 path, pem_path, INSTALL_NEW, Extension::WAS_INSTALLED_BY_DEFAULT,
1661 Manifest::Location::INTERNAL);
[email protected]be083862012-09-01 03:53:451662
1663 EXPECT_EQ(0u, GetErrors().size());
[email protected]f484f8d52014-06-12 08:38:181664 ASSERT_EQ(1u, registry()->enabled_extensions().size());
[email protected]be083862012-09-01 03:53:451665 EXPECT_EQ(permissions_crx, extension->id());
1666
1667 // Verify that the valid API permissions have been recognized.
1668 expected_api_perms.insert(APIPermission::kTab);
1669
dchengc963c7142016-04-08 03:55:221670 std::unique_ptr<const PermissionSet> known_perms =
rdevlin.cronine2d0fd02015-09-24 22:35:491671 prefs->GetGrantedPermissions(extension->id());
[email protected]be083862012-09-01 03:53:451672 EXPECT_TRUE(known_perms.get());
1673 EXPECT_FALSE(known_perms->IsEmpty());
1674 EXPECT_EQ(expected_api_perms, known_perms->apis());
[email protected]8d888c12010-11-30 00:00:251675}
1676#endif
1677
1678// Tests that the extension is disabled when permissions are missing from
1679// the extension's granted permissions preferences. (This simulates updating
1680// the browser to a version which recognizes more permissions).
[email protected]d9a61e12012-11-14 02:43:471681TEST_F(ExtensionServiceTest, GrantedAPIAndHostPermissions) {
[email protected]eaa7dd182010-12-14 11:09:001682 InitializeEmptyExtensionService();
[email protected]8d888c12010-11-30 00:00:251683
[email protected]f484f8d52014-06-12 08:38:181684 base::FilePath path =
1685 data_dir().AppendASCII("permissions").AppendASCII("unknown");
[email protected]8d888c12010-11-30 00:00:251686
[email protected]7567484142013-07-11 17:36:071687 ASSERT_TRUE(base::PathExists(path));
[email protected]8d888c12010-11-30 00:00:251688
[email protected]8f512c72011-11-22 21:02:501689 const Extension* extension = PackAndInstallCRX(path, INSTALL_NEW);
[email protected]8d888c12010-11-30 00:00:251690
1691 EXPECT_EQ(0u, GetErrors().size());
[email protected]f484f8d52014-06-12 08:38:181692 EXPECT_EQ(1u, registry()->enabled_extensions().size());
[email protected]8d888c12010-11-30 00:00:251693 std::string extension_id = extension->id();
1694
[email protected]f484f8d52014-06-12 08:38:181695 ExtensionPrefs* prefs = ExtensionPrefs::Get(profile());
[email protected]8d888c12010-11-30 00:00:251696
[email protected]c2e66e12012-06-27 06:27:061697 APIPermissionSet expected_api_permissions;
[email protected]cced75a2011-05-20 08:31:121698 URLPatternSet expected_host_permissions;
[email protected]8d888c12010-11-30 00:00:251699
[email protected]c2e66e12012-06-27 06:27:061700 expected_api_permissions.insert(APIPermission::kTab);
[email protected]8d888c12010-11-30 00:00:251701 AddPattern(&expected_host_permissions, "http://*.google.com/*");
1702 AddPattern(&expected_host_permissions, "https://*.google.com/*");
[email protected]d6a5c78c2010-12-07 05:18:151703 AddPattern(&expected_host_permissions, "http://*.google.com.hk/*");
[email protected]8d888c12010-11-30 00:00:251704 AddPattern(&expected_host_permissions, "http://www.example.com/*");
1705
[email protected]8d888c12010-11-30 00:00:251706 std::set<std::string> host_permissions;
1707
1708 // Test that the extension is disabled when an API permission is missing from
1709 // the extension's granted api permissions preference. (This simulates
1710 // updating the browser to a version which recognizes a new API permission).
[email protected]0d3e4a22011-06-23 19:02:521711 SetPref(extension_id, "granted_permissions.api",
vabr9984ea62017-04-10 11:33:491712 base::MakeUnique<base::ListValue>(), "granted_permissions.api");
[email protected]f484f8d52014-06-12 08:38:181713 service()->ReloadExtensionsForTest();
[email protected]8d888c12010-11-30 00:00:251714
[email protected]f484f8d52014-06-12 08:38:181715 EXPECT_EQ(1u, registry()->disabled_extensions().size());
1716 extension = registry()->disabled_extensions().begin()->get();
[email protected]8d888c12010-11-30 00:00:251717
[email protected]ad83ca242011-07-29 01:32:251718 ASSERT_TRUE(prefs->IsExtensionDisabled(extension_id));
[email protected]f484f8d52014-06-12 08:38:181719 ASSERT_FALSE(service()->IsExtensionEnabled(extension_id));
[email protected]8d888c12010-11-30 00:00:251720 ASSERT_TRUE(prefs->DidExtensionEscalatePermissions(extension_id));
1721
1722 // Now grant and re-enable the extension, making sure the prefs are updated.
[email protected]f484f8d52014-06-12 08:38:181723 service()->GrantPermissionsAndEnableExtension(extension);
[email protected]8d888c12010-11-30 00:00:251724
[email protected]ad83ca242011-07-29 01:32:251725 ASSERT_FALSE(prefs->IsExtensionDisabled(extension_id));
[email protected]f484f8d52014-06-12 08:38:181726 ASSERT_TRUE(service()->IsExtensionEnabled(extension_id));
[email protected]8d888c12010-11-30 00:00:251727 ASSERT_FALSE(prefs->DidExtensionEscalatePermissions(extension_id));
1728
dchengc963c7142016-04-08 03:55:221729 std::unique_ptr<const PermissionSet> current_perms =
rdevlin.cronine2d0fd02015-09-24 22:35:491730 prefs->GetGrantedPermissions(extension_id);
[email protected]0d3e4a22011-06-23 19:02:521731 ASSERT_TRUE(current_perms.get());
1732 ASSERT_FALSE(current_perms->IsEmpty());
[email protected]0d3e4a22011-06-23 19:02:521733 ASSERT_EQ(expected_api_permissions, current_perms->apis());
[email protected]06e8b8ff2011-07-13 15:03:471734 ASSERT_EQ(expected_host_permissions, current_perms->effective_hosts());
[email protected]8d888c12010-11-30 00:00:251735
1736 // Tests that the extension is disabled when a host permission is missing from
1737 // the extension's granted host permissions preference. (This simulates
1738 // updating the browser to a version which recognizes additional host
1739 // permissions).
[email protected]8d888c12010-11-30 00:00:251740 host_permissions.clear();
[email protected]902fd7b2011-07-27 18:42:311741 current_perms = NULL;
[email protected]8d888c12010-11-30 00:00:251742
[email protected]8d888c12010-11-30 00:00:251743 host_permissions.insert("http://*.google.com/*");
1744 host_permissions.insert("https://*.google.com/*");
[email protected]d6a5c78c2010-12-07 05:18:151745 host_permissions.insert("http://*.google.com.hk/*");
[email protected]8d888c12010-11-30 00:00:251746
vabr9984ea62017-04-10 11:33:491747 auto api_permissions = base::MakeUnique<base::ListValue>();
dchengd9ea63862016-06-03 02:27:181748 api_permissions->AppendString("tabs");
vabr9984ea62017-04-10 11:33:491749 SetPref(extension_id, "granted_permissions.api", std::move(api_permissions),
1750 "granted_permissions.api");
[email protected]0d3e4a22011-06-23 19:02:521751 SetPrefStringSet(
1752 extension_id, "granted_permissions.scriptable_host", host_permissions);
[email protected]8d888c12010-11-30 00:00:251753
[email protected]f484f8d52014-06-12 08:38:181754 service()->ReloadExtensionsForTest();
[email protected]8d888c12010-11-30 00:00:251755
[email protected]f484f8d52014-06-12 08:38:181756 EXPECT_EQ(1u, registry()->disabled_extensions().size());
1757 extension = registry()->disabled_extensions().begin()->get();
[email protected]8d888c12010-11-30 00:00:251758
[email protected]ad83ca242011-07-29 01:32:251759 ASSERT_TRUE(prefs->IsExtensionDisabled(extension_id));
[email protected]f484f8d52014-06-12 08:38:181760 ASSERT_FALSE(service()->IsExtensionEnabled(extension_id));
[email protected]8d888c12010-11-30 00:00:251761 ASSERT_TRUE(prefs->DidExtensionEscalatePermissions(extension_id));
1762
1763 // Now grant and re-enable the extension, making sure the prefs are updated.
[email protected]f484f8d52014-06-12 08:38:181764 service()->GrantPermissionsAndEnableExtension(extension);
[email protected]8d888c12010-11-30 00:00:251765
[email protected]f484f8d52014-06-12 08:38:181766 ASSERT_TRUE(service()->IsExtensionEnabled(extension_id));
[email protected]8d888c12010-11-30 00:00:251767 ASSERT_FALSE(prefs->DidExtensionEscalatePermissions(extension_id));
1768
[email protected]902fd7b2011-07-27 18:42:311769 current_perms = prefs->GetGrantedPermissions(extension_id);
[email protected]0d3e4a22011-06-23 19:02:521770 ASSERT_TRUE(current_perms.get());
1771 ASSERT_FALSE(current_perms->IsEmpty());
[email protected]0d3e4a22011-06-23 19:02:521772 ASSERT_EQ(expected_api_permissions, current_perms->apis());
[email protected]06e8b8ff2011-07-13 15:03:471773 ASSERT_EQ(expected_host_permissions, current_perms->effective_hosts());
[email protected]8d888c12010-11-30 00:00:251774}
1775
[email protected]a17f9462009-06-09 02:56:411776// Test Packaging and installing an extension.
[email protected]d9a61e12012-11-14 02:43:471777TEST_F(ExtensionServiceTest, PackExtension) {
[email protected]eaa7dd182010-12-14 11:09:001778 InitializeEmptyExtensionService();
[email protected]f484f8d52014-06-12 08:38:181779 base::FilePath input_directory =
1780 data_dir()
1781 .AppendASCII("good")
1782 .AppendASCII("Extensions")
1783 .AppendASCII("behllobkkfkfnphdnhnkndlbkcpglgmj")
1784 .AppendASCII("1.0.0.0");
[email protected]a17f9462009-06-09 02:56:411785
[email protected]ea1a3f62012-11-16 20:34:231786 base::ScopedTempDir temp_dir;
[email protected]aca3e9b2009-11-03 01:14:211787 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
vabr9142fe22016-09-08 13:19:221788 base::FilePath output_directory = temp_dir.GetPath();
[email protected]aca3e9b2009-11-03 01:14:211789
[email protected]650b2d52013-02-10 03:41:451790 base::FilePath crx_path(output_directory.AppendASCII("ex1.crx"));
1791 base::FilePath privkey_path(output_directory.AppendASCII("privkey.pem"));
[email protected]a17f9462009-06-09 02:56:411792
dchengc963c7142016-04-08 03:55:221793 std::unique_ptr<ExtensionCreator> creator(new ExtensionCreator());
[email protected]650b2d52013-02-10 03:41:451794 ASSERT_TRUE(creator->Run(input_directory, crx_path, base::FilePath(),
[email protected]93d973a2012-01-08 07:38:261795 privkey_path, ExtensionCreator::kNoRunFlags));
[email protected]7567484142013-07-11 17:36:071796 ASSERT_TRUE(base::PathExists(crx_path));
1797 ASSERT_TRUE(base::PathExists(privkey_path));
[email protected]93d973a2012-01-08 07:38:261798
1799 // Repeat the run with the pem file gone, and no special flags
1800 // Should refuse to overwrite the existing crx.
[email protected]dd3aa792013-07-16 19:10:231801 base::DeleteFile(privkey_path, false);
[email protected]650b2d52013-02-10 03:41:451802 ASSERT_FALSE(creator->Run(input_directory, crx_path, base::FilePath(),
[email protected]93d973a2012-01-08 07:38:261803 privkey_path, ExtensionCreator::kNoRunFlags));
1804
1805 // OK, now try it with a flag to overwrite existing crx. Should work.
[email protected]650b2d52013-02-10 03:41:451806 ASSERT_TRUE(creator->Run(input_directory, crx_path, base::FilePath(),
[email protected]93d973a2012-01-08 07:38:261807 privkey_path, ExtensionCreator::kOverwriteCRX));
1808
1809 // Repeat the run allowing existing crx, but the existing pem is still
1810 // an error. Should fail.
[email protected]650b2d52013-02-10 03:41:451811 ASSERT_FALSE(creator->Run(input_directory, crx_path, base::FilePath(),
[email protected]93d973a2012-01-08 07:38:261812 privkey_path, ExtensionCreator::kOverwriteCRX));
[email protected]a17f9462009-06-09 02:56:411813
[email protected]7567484142013-07-11 17:36:071814 ASSERT_TRUE(base::PathExists(privkey_path));
[email protected]8f512c72011-11-22 21:02:501815 InstallCRX(crx_path, INSTALL_NEW);
[email protected]0dc2ca82009-11-17 07:06:161816
1817 // Try packing with invalid paths.
1818 creator.reset(new ExtensionCreator());
[email protected]650b2d52013-02-10 03:41:451819 ASSERT_FALSE(
1820 creator->Run(base::FilePath(), base::FilePath(), base::FilePath(),
1821 base::FilePath(), ExtensionCreator::kOverwriteCRX));
[email protected]0dc2ca82009-11-17 07:06:161822
1823 // Try packing an empty directory. Should fail because an empty directory is
1824 // not a valid extension.
[email protected]ea1a3f62012-11-16 20:34:231825 base::ScopedTempDir temp_dir2;
[email protected]0dc2ca82009-11-17 07:06:161826 ASSERT_TRUE(temp_dir2.CreateUniqueTempDir());
1827 creator.reset(new ExtensionCreator());
vabr9142fe22016-09-08 13:19:221828 ASSERT_FALSE(creator->Run(temp_dir2.GetPath(), crx_path, privkey_path,
[email protected]650b2d52013-02-10 03:41:451829 base::FilePath(), ExtensionCreator::kOverwriteCRX));
[email protected]0dc2ca82009-11-17 07:06:161830
1831 // Try packing with an invalid manifest.
1832 std::string invalid_manifest_content = "I am not a manifest.";
lazyboy8df84fc2017-04-12 02:12:481833 ASSERT_EQ(
1834 static_cast<int>(invalid_manifest_content.size()),
1835 base::WriteFile(temp_dir2.GetPath().Append(extensions::kManifestFilename),
1836 invalid_manifest_content.c_str(),
1837 invalid_manifest_content.size()));
[email protected]0dc2ca82009-11-17 07:06:161838 creator.reset(new ExtensionCreator());
vabr9142fe22016-09-08 13:19:221839 ASSERT_FALSE(creator->Run(temp_dir2.GetPath(), crx_path, privkey_path,
[email protected]650b2d52013-02-10 03:41:451840 base::FilePath(), ExtensionCreator::kOverwriteCRX));
[email protected]c21b5b612013-07-31 07:49:161841
1842 // Try packing with a private key that is a valid key, but invalid for the
1843 // extension.
[email protected]f484f8d52014-06-12 08:38:181844 base::FilePath bad_private_key_dir =
1845 data_dir().AppendASCII("bad_private_key");
[email protected]c21b5b612013-07-31 07:49:161846 crx_path = output_directory.AppendASCII("bad_private_key.crx");
[email protected]f484f8d52014-06-12 08:38:181847 privkey_path = data_dir().AppendASCII("bad_private_key.pem");
[email protected]c21b5b612013-07-31 07:49:161848 ASSERT_FALSE(creator->Run(bad_private_key_dir, crx_path, base::FilePath(),
1849 privkey_path, ExtensionCreator::kOverwriteCRX));
[email protected]a17f9462009-06-09 02:56:411850}
1851
[email protected]0349ab5d2010-08-11 21:41:571852// Test Packaging and installing an extension whose name contains punctuation.
[email protected]d9a61e12012-11-14 02:43:471853TEST_F(ExtensionServiceTest, PackPunctuatedExtension) {
[email protected]eaa7dd182010-12-14 11:09:001854 InitializeEmptyExtensionService();
[email protected]f484f8d52014-06-12 08:38:181855 base::FilePath input_directory = data_dir()
1856 .AppendASCII("good")
1857 .AppendASCII("Extensions")
1858 .AppendASCII(good0)
1859 .AppendASCII("1.0.0.0");
[email protected]0349ab5d2010-08-11 21:41:571860
[email protected]ea1a3f62012-11-16 20:34:231861 base::ScopedTempDir temp_dir;
[email protected]0349ab5d2010-08-11 21:41:571862 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
1863
1864 // Extension names containing punctuation, and the expected names for the
1865 // packed extensions.
[email protected]650b2d52013-02-10 03:41:451866 const base::FilePath punctuated_names[] = {
1867 base::FilePath(FILE_PATH_LITERAL("this.extensions.name.has.periods")),
1868 base::FilePath(FILE_PATH_LITERAL(".thisextensionsnamestartswithaperiod")),
1869 base::FilePath(FILE_PATH_LITERAL("thisextensionhasaslashinitsname/")).
[email protected]d9034ed22012-02-10 02:04:401870 NormalizePathSeparators(),
[email protected]0349ab5d2010-08-11 21:41:571871 };
[email protected]650b2d52013-02-10 03:41:451872 const base::FilePath expected_crx_names[] = {
1873 base::FilePath(FILE_PATH_LITERAL("this.extensions.name.has.periods.crx")),
1874 base::FilePath(
1875 FILE_PATH_LITERAL(".thisextensionsnamestartswithaperiod.crx")),
1876 base::FilePath(FILE_PATH_LITERAL("thisextensionhasaslashinitsname.crx")),
[email protected]0349ab5d2010-08-11 21:41:571877 };
[email protected]650b2d52013-02-10 03:41:451878 const base::FilePath expected_private_key_names[] = {
1879 base::FilePath(FILE_PATH_LITERAL("this.extensions.name.has.periods.pem")),
1880 base::FilePath(
1881 FILE_PATH_LITERAL(".thisextensionsnamestartswithaperiod.pem")),
1882 base::FilePath(FILE_PATH_LITERAL("thisextensionhasaslashinitsname.pem")),
[email protected]0349ab5d2010-08-11 21:41:571883 };
1884
1885 for (size_t i = 0; i < arraysize(punctuated_names); ++i) {
1886 SCOPED_TRACE(punctuated_names[i].value().c_str());
vabr9142fe22016-09-08 13:19:221887 base::FilePath output_dir = temp_dir.GetPath().Append(punctuated_names[i]);
[email protected]0349ab5d2010-08-11 21:41:571888
1889 // Copy the extension into the output directory, as PackExtensionJob doesn't
1890 // let us choose where to output the packed extension.
[email protected]f0ff2ad2013-07-09 17:42:261891 ASSERT_TRUE(base::CopyDirectory(input_directory, output_dir, true));
[email protected]0349ab5d2010-08-11 21:41:571892
[email protected]650b2d52013-02-10 03:41:451893 base::FilePath expected_crx_path =
vabr9142fe22016-09-08 13:19:221894 temp_dir.GetPath().Append(expected_crx_names[i]);
[email protected]650b2d52013-02-10 03:41:451895 base::FilePath expected_private_key_path =
vabr9142fe22016-09-08 13:19:221896 temp_dir.GetPath().Append(expected_private_key_names[i]);
[email protected]0349ab5d2010-08-11 21:41:571897 PackExtensionTestClient pack_client(expected_crx_path,
1898 expected_private_key_path);
Istiaque Ahmed6a06cc92017-08-12 00:31:331899 {
1900 extensions::PackExtensionJob packer(&pack_client, output_dir,
1901 base::FilePath(),
1902 ExtensionCreator::kOverwriteCRX);
1903 packer.Start();
[email protected]0349ab5d2010-08-11 21:41:571904
Istiaque Ahmed6a06cc92017-08-12 00:31:331905 // The packer will post a notification task to the current thread's
1906 // message loop when it is finished. We manually run the loop here so
1907 // that we block and catch the notification; otherwise, the process would
1908 // exit.
1909 // This call to |Run()| is matched by a call to |Quit()| in the
1910 // |PackExtensionTestClient|'s notification handling code.
1911 base::RunLoop().Run();
1912 }
[email protected]0349ab5d2010-08-11 21:41:571913
1914 if (HasFatalFailure())
1915 return;
1916
[email protected]8f512c72011-11-22 21:02:501917 InstallCRX(expected_crx_path, INSTALL_NEW);
[email protected]0349ab5d2010-08-11 21:41:571918 }
1919}
1920
[email protected]d9a61e12012-11-14 02:43:471921TEST_F(ExtensionServiceTest, PackExtensionContainingKeyFails) {
[email protected]ab55c2b2012-06-01 23:55:031922 InitializeEmptyExtensionService();
1923
[email protected]ea1a3f62012-11-16 20:34:231924 base::ScopedTempDir extension_temp_dir;
[email protected]ab55c2b2012-06-01 23:55:031925 ASSERT_TRUE(extension_temp_dir.CreateUniqueTempDir());
vabr9142fe22016-09-08 13:19:221926 base::FilePath input_directory =
1927 extension_temp_dir.GetPath().AppendASCII("ext");
[email protected]f484f8d52014-06-12 08:38:181928 ASSERT_TRUE(
1929 base::CopyDirectory(data_dir()
1930 .AppendASCII("good")
1931 .AppendASCII("Extensions")
1932 .AppendASCII("behllobkkfkfnphdnhnkndlbkcpglgmj")
1933 .AppendASCII("1.0.0.0"),
1934 input_directory,
1935 /*recursive=*/true));
[email protected]ab55c2b2012-06-01 23:55:031936
[email protected]ea1a3f62012-11-16 20:34:231937 base::ScopedTempDir output_temp_dir;
[email protected]ab55c2b2012-06-01 23:55:031938 ASSERT_TRUE(output_temp_dir.CreateUniqueTempDir());
vabr9142fe22016-09-08 13:19:221939 base::FilePath output_directory = output_temp_dir.GetPath();
[email protected]ab55c2b2012-06-01 23:55:031940
[email protected]650b2d52013-02-10 03:41:451941 base::FilePath crx_path(output_directory.AppendASCII("ex1.crx"));
1942 base::FilePath privkey_path(output_directory.AppendASCII("privkey.pem"));
[email protected]ab55c2b2012-06-01 23:55:031943
1944 // Pack the extension once to get a private key.
dchengc963c7142016-04-08 03:55:221945 std::unique_ptr<ExtensionCreator> creator(new ExtensionCreator());
[email protected]650b2d52013-02-10 03:41:451946 ASSERT_TRUE(creator->Run(input_directory, crx_path, base::FilePath(),
[email protected]ab55c2b2012-06-01 23:55:031947 privkey_path, ExtensionCreator::kNoRunFlags))
1948 << creator->error_message();
[email protected]7567484142013-07-11 17:36:071949 ASSERT_TRUE(base::PathExists(crx_path));
1950 ASSERT_TRUE(base::PathExists(privkey_path));
[email protected]ab55c2b2012-06-01 23:55:031951
[email protected]dd3aa792013-07-16 19:10:231952 base::DeleteFile(crx_path, false);
[email protected]ab55c2b2012-06-01 23:55:031953 // Move the pem file into the extension.
[email protected]5553d5b2013-07-01 23:07:361954 base::Move(privkey_path,
[email protected]ab55c2b2012-06-01 23:55:031955 input_directory.AppendASCII("privkey.pem"));
1956
1957 // This pack should fail because of the contained private key.
[email protected]650b2d52013-02-10 03:41:451958 EXPECT_FALSE(creator->Run(input_directory, crx_path, base::FilePath(),
[email protected]ab55c2b2012-06-01 23:55:031959 privkey_path, ExtensionCreator::kNoRunFlags));
1960 EXPECT_THAT(creator->error_message(),
1961 testing::ContainsRegex(
1962 "extension includes the key file.*privkey.pem"));
1963}
1964
[email protected]a17f9462009-06-09 02:56:411965// Test Packaging and installing an extension using an openssl generated key.
1966// The openssl is generated with the following:
[email protected]a1257b12009-06-12 02:51:341967// > openssl genrsa -out privkey.pem 1024
[email protected]df4956e2009-06-10 16:53:421968// > openssl pkcs8 -topk8 -nocrypt -in privkey.pem -out privkey_asn1.pem
[email protected]a1257b12009-06-12 02:51:341969// The privkey.pem is a PrivateKey, and the pcks8 -topk8 creates a
[email protected]a17f9462009-06-09 02:56:411970// PrivateKeyInfo ASN.1 structure, we our RSAPrivateKey expects.
[email protected]d9a61e12012-11-14 02:43:471971TEST_F(ExtensionServiceTest, PackExtensionOpenSSLKey) {
[email protected]eaa7dd182010-12-14 11:09:001972 InitializeEmptyExtensionService();
[email protected]f484f8d52014-06-12 08:38:181973 base::FilePath input_directory =
1974 data_dir()
1975 .AppendASCII("good")
1976 .AppendASCII("Extensions")
1977 .AppendASCII("behllobkkfkfnphdnhnkndlbkcpglgmj")
1978 .AppendASCII("1.0.0.0");
1979 base::FilePath privkey_path(
1980 data_dir().AppendASCII("openssl_privkey_asn1.pem"));
[email protected]7567484142013-07-11 17:36:071981 ASSERT_TRUE(base::PathExists(privkey_path));
[email protected]a17f9462009-06-09 02:56:411982
[email protected]ea1a3f62012-11-16 20:34:231983 base::ScopedTempDir temp_dir;
[email protected]aca3e9b2009-11-03 01:14:211984 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
vabr9142fe22016-09-08 13:19:221985 base::FilePath output_directory = temp_dir.GetPath();
[email protected]aca3e9b2009-11-03 01:14:211986
[email protected]650b2d52013-02-10 03:41:451987 base::FilePath crx_path(output_directory.AppendASCII("ex1.crx"));
[email protected]a17f9462009-06-09 02:56:411988
dchengc963c7142016-04-08 03:55:221989 std::unique_ptr<ExtensionCreator> creator(new ExtensionCreator());
[email protected]a17f9462009-06-09 02:56:411990 ASSERT_TRUE(creator->Run(input_directory, crx_path, privkey_path,
[email protected]650b2d52013-02-10 03:41:451991 base::FilePath(), ExtensionCreator::kOverwriteCRX));
[email protected]a17f9462009-06-09 02:56:411992
[email protected]8f512c72011-11-22 21:02:501993 InstallCRX(crx_path, INSTALL_NEW);
[email protected]a17f9462009-06-09 02:56:411994}
[email protected]a17f9462009-06-09 02:56:411995
rdevlin.cronin7217be52017-03-24 20:47:051996TEST_F(ExtensionServiceTest, TestInstallThemeWithExtensionsDisabled) {
1997 // Themes can be installed, even when extensions are disabled.
1998 InitializeExtensionServiceWithExtensionsDisabled();
1999 base::FilePath path = data_dir().AppendASCII("theme.crx");
2000 InstallCRX(path, INSTALL_NEW);
2001 ValidatePrefKeyCount(1);
2002 ValidateIntegerPref(theme_crx, "state", Extension::ENABLED);
2003 ValidateIntegerPref(theme_crx, "location", Manifest::INTERNAL);
2004}
2005
[email protected]87065922014-05-28 14:50:372006#if defined(THREAD_SANITIZER)
2007// Flaky under Tsan. http://crbug.com/377702
2008#define MAYBE_InstallTheme DISABLED_InstallTheme
2009#else
2010#define MAYBE_InstallTheme InstallTheme
2011#endif
2012
2013TEST_F(ExtensionServiceTest, MAYBE_InstallTheme) {
[email protected]eaa7dd182010-12-14 11:09:002014 InitializeEmptyExtensionService();
[email protected]f484f8d52014-06-12 08:38:182015 service()->Init();
[email protected]e2eb43112009-05-29 21:19:542016
2017 // A theme.
[email protected]f484f8d52014-06-12 08:38:182018 base::FilePath path = data_dir().AppendASCII("theme.crx");
[email protected]8f512c72011-11-22 21:02:502019 InstallCRX(path, INSTALL_NEW);
[email protected]25b34332009-06-05 21:53:192020 int pref_count = 0;
2021 ValidatePrefKeyCount(++pref_count);
[email protected]e2194742010-08-12 05:54:342022 ValidateIntegerPref(theme_crx, "state", Extension::ENABLED);
[email protected]1d5e58b2013-01-31 08:41:402023 ValidateIntegerPref(theme_crx, "location", Manifest::INTERNAL);
[email protected]e2eb43112009-05-29 21:19:542024
[email protected]f484f8d52014-06-12 08:38:182025 path = data_dir().AppendASCII("theme2.crx");
[email protected]8f512c72011-11-22 21:02:502026 InstallCRX(path, INSTALL_NEW);
[email protected]25b34332009-06-05 21:53:192027 ValidatePrefKeyCount(++pref_count);
[email protected]e2194742010-08-12 05:54:342028 ValidateIntegerPref(theme2_crx, "state", Extension::ENABLED);
[email protected]1d5e58b2013-01-31 08:41:402029 ValidateIntegerPref(theme2_crx, "location", Manifest::INTERNAL);
[email protected]494c06e2009-07-25 01:06:422030
[email protected]f6dec1e322012-04-30 21:04:512031 // A theme with extension elements. Themes cannot have extension elements,
2032 // so any such elements (like content scripts) should be ignored.
[email protected]f6dec1e322012-04-30 21:04:512033 {
[email protected]f484f8d52014-06-12 08:38:182034 path = data_dir().AppendASCII("theme_with_extension.crx");
[email protected]f6dec1e322012-04-30 21:04:512035 const Extension* extension = InstallCRX(path, INSTALL_NEW);
2036 ValidatePrefKeyCount(++pref_count);
2037 ASSERT_TRUE(extension);
2038 EXPECT_TRUE(extension->is_theme());
lazyboy12c77d72016-08-19 20:06:092039 EXPECT_TRUE(
2040 extensions::ContentScriptsInfo::GetContentScripts(extension).empty());
[email protected]f6dec1e322012-04-30 21:04:512041 }
[email protected]12198912009-06-05 03:41:222042
2043 // A theme with image resources missing (misspelt path).
[email protected]f484f8d52014-06-12 08:38:182044 path = data_dir().AppendASCII("theme_missing_image.crx");
[email protected]8f512c72011-11-22 21:02:502045 InstallCRX(path, INSTALL_FAILED);
[email protected]25b34332009-06-05 21:53:192046 ValidatePrefKeyCount(pref_count);
[email protected]e2eb43112009-05-29 21:19:542047}
2048
[email protected]d9a61e12012-11-14 02:43:472049TEST_F(ExtensionServiceTest, LoadLocalizedTheme) {
[email protected]584245e52010-06-17 01:08:132050 // Load.
[email protected]eaa7dd182010-12-14 11:09:002051 InitializeEmptyExtensionService();
[email protected]f484f8d52014-06-12 08:38:182052 service()->Init();
[email protected]b1ac70462013-08-14 21:33:302053
[email protected]f484f8d52014-06-12 08:38:182054 base::FilePath extension_path = data_dir().AppendASCII("theme_i18n");
[email protected]584245e52010-06-17 01:08:132055
[email protected]f484f8d52014-06-12 08:38:182056 extensions::UnpackedInstaller::Create(service())->Load(extension_path);
Gabriel Charette01507a22017-09-27 21:30:082057 content::RunAllTasksUntilIdle();
[email protected]584245e52010-06-17 01:08:132058 EXPECT_EQ(0u, GetErrors().size());
2059 ASSERT_EQ(1u, loaded_.size());
[email protected]f484f8d52014-06-12 08:38:182060 EXPECT_EQ(1u, registry()->enabled_extensions().size());
2061 const Extension* theme = registry()->enabled_extensions().begin()->get();
[email protected]8f512c72011-11-22 21:02:502062 EXPECT_EQ("name", theme->name());
2063 EXPECT_EQ("description", theme->description());
[email protected]a17dfcf2012-12-30 02:07:092064
estadeb8867dff2016-11-03 17:33:332065 // Cleanup the "Cached Theme.pak" file. Ideally, this would be installed in a
[email protected]a17dfcf2012-12-30 02:07:092066 // temporary directory, but it automatically installs to the extension's
2067 // directory, and we don't want to copy the whole extension for a unittest.
estadeb8867dff2016-11-03 17:33:332068 base::FilePath theme_file = extension_path.Append(chrome::kThemePackFilename);
[email protected]7567484142013-07-11 17:36:072069 ASSERT_TRUE(base::PathExists(theme_file));
[email protected]dd3aa792013-07-16 19:10:232070 ASSERT_TRUE(base::DeleteFile(theme_file, false)); // Not recursive.
[email protected]584245e52010-06-17 01:08:132071}
2072
[email protected]91236521162012-05-24 15:02:512073#if defined(OS_POSIX)
[email protected]d9a61e12012-11-14 02:43:472074TEST_F(ExtensionServiceTest, UnpackedExtensionMayContainSymlinkedFiles) {
[email protected]f484f8d52014-06-12 08:38:182075 base::FilePath source_data_dir =
2076 data_dir().AppendASCII("unpacked").AppendASCII("symlinks_allowed");
[email protected]91236521162012-05-24 15:02:512077
2078 // Paths to test data files.
[email protected]650b2d52013-02-10 03:41:452079 base::FilePath source_manifest = source_data_dir.AppendASCII("manifest.json");
[email protected]7567484142013-07-11 17:36:072080 ASSERT_TRUE(base::PathExists(source_manifest));
[email protected]650b2d52013-02-10 03:41:452081 base::FilePath source_icon = source_data_dir.AppendASCII("icon.png");
[email protected]7567484142013-07-11 17:36:072082 ASSERT_TRUE(base::PathExists(source_icon));
[email protected]91236521162012-05-24 15:02:512083
2084 // Set up the temporary extension directory.
[email protected]ea1a3f62012-11-16 20:34:232085 base::ScopedTempDir temp;
[email protected]91236521162012-05-24 15:02:512086 ASSERT_TRUE(temp.CreateUniqueTempDir());
vabr9142fe22016-09-08 13:19:222087 base::FilePath extension_path = temp.GetPath();
[email protected]993da5e2013-03-23 21:25:162088 base::FilePath manifest = extension_path.Append(
2089 extensions::kManifestFilename);
[email protected]650b2d52013-02-10 03:41:452090 base::FilePath icon_symlink = extension_path.AppendASCII("icon.png");
[email protected]f0ff2ad2013-07-09 17:42:262091 base::CopyFile(source_manifest, manifest);
[email protected]b264eab2013-11-27 23:22:082092 base::CreateSymbolicLink(source_icon, icon_symlink);
[email protected]91236521162012-05-24 15:02:512093
2094 // Load extension.
2095 InitializeEmptyExtensionService();
[email protected]f484f8d52014-06-12 08:38:182096 extensions::UnpackedInstaller::Create(service())->Load(extension_path);
Gabriel Charette01507a22017-09-27 21:30:082097 content::RunAllTasksUntilIdle();
[email protected]91236521162012-05-24 15:02:512098
2099 EXPECT_TRUE(GetErrors().empty());
2100 ASSERT_EQ(1u, loaded_.size());
[email protected]f484f8d52014-06-12 08:38:182101 EXPECT_EQ(1u, registry()->enabled_extensions().size());
[email protected]91236521162012-05-24 15:02:512102}
2103#endif
2104
Karan Bhatiafe12281a2017-09-16 02:38:182105// Tests than an unpacked extension with an empty kMetadataFolder loads
2106// successfully.
2107TEST_F(ExtensionServiceTest, UnpackedExtensionWithEmptyMetadataFolder) {
Catherine Mullingsf5f1a1332017-08-04 18:29:192108 InitializeEmptyExtensionService();
Karan Bhatiafe12281a2017-09-16 02:38:182109 base::ScopedTempDir temp_dir;
2110 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
2111 base::FilePath extension_dir = base::MakeAbsoluteFilePath(temp_dir.GetPath());
2112 base::FilePath metadata_dir =
2113 extension_dir.Append(extensions::kMetadataFolder);
2114 PersistExtensionWithPaths(extension_dir, {metadata_dir}, {});
2115 EXPECT_TRUE(base::DirectoryExists(metadata_dir));
2116
2117 extensions::UnpackedInstaller::Create(service())->Load(extension_dir);
Gabriel Charette01507a22017-09-27 21:30:082118 content::RunAllTasksUntilIdle();
Catherine Mullingsf5f1a1332017-08-04 18:29:192119 EXPECT_EQ(0u, GetErrors().size());
2120 EXPECT_EQ(1u, registry()->enabled_extensions().size());
Karan Bhatiafe12281a2017-09-16 02:38:182121
2122 // The kMetadataFolder should have been deleted since it did not contain
2123 // any non-reserved filenames.
2124 EXPECT_FALSE(base::DirectoryExists(metadata_dir));
Catherine Mullingsf5f1a1332017-08-04 18:29:192125}
2126
Karan Bhatiafe12281a2017-09-16 02:38:182127// Tests that an unpacked extension with only reserved filenames in the
2128// kMetadataFolder loads successfully.
2129TEST_F(ExtensionServiceTest, UnpackedExtensionWithReservedMetadataFiles) {
Karan Bhatia46178662017-09-11 21:50:452130 InitializeEmptyExtensionService();
Karan Bhatiafe12281a2017-09-16 02:38:182131 base::ScopedTempDir temp_dir;
2132 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
2133 base::FilePath extension_dir = base::MakeAbsoluteFilePath(temp_dir.GetPath());
2134 base::FilePath metadata_dir =
2135 extension_dir.Append(extensions::kMetadataFolder);
2136 PersistExtensionWithPaths(
2137 extension_dir, {metadata_dir},
2138 extensions::file_util::GetReservedMetadataFilePaths(extension_dir));
2139 EXPECT_TRUE(base::DirectoryExists(metadata_dir));
2140
2141 extensions::UnpackedInstaller::Create(service())->Load(extension_dir);
Gabriel Charette01507a22017-09-27 21:30:082142 content::RunAllTasksUntilIdle();
Karan Bhatiafe12281a2017-09-16 02:38:182143 EXPECT_EQ(0u, GetErrors().size());
2144 EXPECT_EQ(1u, registry()->enabled_extensions().size());
2145
2146 // The kMetadataFolder should have been deleted since it did not contain
2147 // any non-reserved filenames.
2148 EXPECT_FALSE(base::DirectoryExists(metadata_dir));
2149}
2150
2151// Tests that an unpacked extension with non-reserved files in the
2152// kMetadataFolder fails to load.
2153TEST_F(ExtensionServiceTest, UnpackedExtensionWithUserMetadataFiles) {
2154 InitializeEmptyExtensionService();
2155 base::ScopedTempDir temp_dir;
2156 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
2157 base::FilePath extension_dir = base::MakeAbsoluteFilePath(temp_dir.GetPath());
2158 base::FilePath metadata_dir =
2159 extension_dir.Append(extensions::kMetadataFolder);
2160 base::FilePath non_reserved_file =
2161 metadata_dir.Append(FILE_PATH_LITERAL("a.txt"));
2162 PersistExtensionWithPaths(
2163 extension_dir, {metadata_dir},
2164 {extensions::file_util::GetVerifiedContentsPath(extension_dir),
2165 non_reserved_file});
2166 EXPECT_TRUE(base::PathExists(non_reserved_file));
2167
2168 extensions::UnpackedInstaller::Create(service())->Load(extension_dir);
Gabriel Charette01507a22017-09-27 21:30:082169 content::RunAllTasksUntilIdle();
Karan Bhatiafe12281a2017-09-16 02:38:182170 ASSERT_EQ(1u, GetErrors().size());
2171
2172 // Format expected error string.
2173 std::string expected("Failed to load extension from: ");
2174 expected.append(extension_dir.MaybeAsASCII())
2175 .append(
2176 ". Cannot load extension with file or directory name _metadata. "
2177 "Filenames starting with \"_\" are reserved for use by the system.");
2178
2179 EXPECT_EQ(base::UTF8ToUTF16(expected), GetErrors()[0]);
2180 EXPECT_EQ(0u, registry()->enabled_extensions().size());
2181
2182 // Non-reserved filepaths inside the kMetadataFolder should not have been
2183 // deleted.
2184 EXPECT_TRUE(base::PathExists(non_reserved_file));
2185}
2186
2187// Tests than an unpacked extension with an empty kMetadataFolder and a folder
2188// beginning with "_" fails to load.
2189TEST_F(ExtensionServiceTest,
2190 UnpackedExtensionWithEmptyMetadataAndUnderscoreFolders) {
2191 InitializeEmptyExtensionService();
2192 base::ScopedTempDir temp_dir;
2193 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
2194 base::FilePath extension_dir = base::MakeAbsoluteFilePath(temp_dir.GetPath());
2195 base::FilePath metadata_dir =
2196 extension_dir.Append(extensions::kMetadataFolder);
2197 PersistExtensionWithPaths(
2198 extension_dir,
2199 {metadata_dir, extension_dir.Append(FILE_PATH_LITERAL("_badfolder"))},
2200 {});
2201
2202 extensions::UnpackedInstaller::Create(service())->Load(extension_dir);
Gabriel Charette01507a22017-09-27 21:30:082203 content::RunAllTasksUntilIdle();
Catherine Mullingsf5f1a1332017-08-04 18:29:192204 EXPECT_EQ(1u, GetErrors().size());
2205
2206 // Format expected error string.
2207 std::string expected("Failed to load extension from: ");
Karan Bhatiafe12281a2017-09-16 02:38:182208 expected.append(extension_dir.MaybeAsASCII())
Catherine Mullingsf5f1a1332017-08-04 18:29:192209 .append(
Karan Bhatiafe12281a2017-09-16 02:38:182210 ". Cannot load extension with file or directory name _badfolder. "
2211 "Filenames starting with \"_\" are reserved for use by the system.");
Catherine Mullingsf5f1a1332017-08-04 18:29:192212
Karan Bhatiafe12281a2017-09-16 02:38:182213 EXPECT_EQ(base::UTF8ToUTF16(expected), GetErrors()[0]);
Catherine Mullingsf5f1a1332017-08-04 18:29:192214 EXPECT_EQ(0u, registry()->enabled_extensions().size());
Karan Bhatiafe12281a2017-09-16 02:38:182215
2216 // The kMetadataFolder should have been deleted since it did not contain any
2217 // non-reserved filenames.
2218 EXPECT_FALSE(base::DirectoryExists(metadata_dir));
Catherine Mullingsf5f1a1332017-08-04 18:29:192219}
2220
Karan Bhatiafe12281a2017-09-16 02:38:182221// Tests that an unpacked extension with an arbitrary folder beginning with an
2222// underscore can't load.
[email protected]5ccca8ad2013-08-19 22:35:342223TEST_F(ExtensionServiceTest, UnpackedExtensionMayNotHaveUnderscore) {
2224 InitializeEmptyExtensionService();
Karan Bhatiafe12281a2017-09-16 02:38:182225 base::ScopedTempDir temp_dir;
2226 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
2227 base::FilePath extension_dir = base::MakeAbsoluteFilePath(temp_dir.GetPath());
2228 base::FilePath underscore_folder =
2229 extension_dir.Append(FILE_PATH_LITERAL("_badfolder"));
2230 PersistExtensionWithPaths(
2231 extension_dir, {underscore_folder},
2232 {underscore_folder.Append(FILE_PATH_LITERAL("a.js"))});
2233 EXPECT_TRUE(base::DirectoryExists(underscore_folder));
2234
2235 extensions::UnpackedInstaller::Create(service())->Load(extension_dir);
Gabriel Charette01507a22017-09-27 21:30:082236 content::RunAllTasksUntilIdle();
[email protected]5ccca8ad2013-08-19 22:35:342237 EXPECT_EQ(1u, GetErrors().size());
Catherine Mullingsf5f1a1332017-08-04 18:29:192238
2239 // Format expected error string.
2240 std::string expected("Failed to load extension from: ");
Karan Bhatiafe12281a2017-09-16 02:38:182241 expected.append(extension_dir.MaybeAsASCII())
Catherine Mullingsf5f1a1332017-08-04 18:29:192242 .append(
Karan Bhatiafe12281a2017-09-16 02:38:182243 ". Cannot load extension with file or directory name _badfolder. "
2244 "Filenames starting with \"_\" are reserved for use by the system.");
Catherine Mullingsf5f1a1332017-08-04 18:29:192245
Karan Bhatiafe12281a2017-09-16 02:38:182246 EXPECT_EQ(base::UTF8ToUTF16(expected), GetErrors()[0]);
[email protected]f484f8d52014-06-12 08:38:182247 EXPECT_EQ(0u, registry()->enabled_extensions().size());
[email protected]5ccca8ad2013-08-19 22:35:342248}
2249
[email protected]d9a61e12012-11-14 02:43:472250TEST_F(ExtensionServiceTest, InstallLocalizedTheme) {
[email protected]eaa7dd182010-12-14 11:09:002251 InitializeEmptyExtensionService();
[email protected]f484f8d52014-06-12 08:38:182252 service()->Init();
[email protected]b1ac70462013-08-14 21:33:302253
[email protected]f484f8d52014-06-12 08:38:182254 base::FilePath theme_path = data_dir().AppendASCII("theme_i18n");
[email protected]584245e52010-06-17 01:08:132255
[email protected]8f512c72011-11-22 21:02:502256 const Extension* theme = PackAndInstallCRX(theme_path, INSTALL_NEW);
[email protected]584245e52010-06-17 01:08:132257
[email protected]584245e52010-06-17 01:08:132258 EXPECT_EQ(0u, GetErrors().size());
[email protected]f484f8d52014-06-12 08:38:182259 EXPECT_EQ(1u, registry()->enabled_extensions().size());
[email protected]8f512c72011-11-22 21:02:502260 EXPECT_EQ("name", theme->name());
2261 EXPECT_EQ("description", theme->description());
[email protected]584245e52010-06-17 01:08:132262}
2263
[email protected]d9a61e12012-11-14 02:43:472264TEST_F(ExtensionServiceTest, InstallApps) {
[email protected]eaa7dd182010-12-14 11:09:002265 InitializeEmptyExtensionService();
[email protected]6d2e60bd2010-06-03 22:37:392266
2267 // An empty app.
[email protected]f484f8d52014-06-12 08:38:182268 const Extension* app =
2269 PackAndInstallCRX(data_dir().AppendASCII("app1"), INSTALL_NEW);
[email protected]6d2e60bd2010-06-03 22:37:392270 int pref_count = 0;
2271 ValidatePrefKeyCount(++pref_count);
[email protected]f484f8d52014-06-12 08:38:182272 ASSERT_EQ(1u, registry()->enabled_extensions().size());
[email protected]8f512c72011-11-22 21:02:502273 ValidateIntegerPref(app->id(), "state", Extension::ENABLED);
[email protected]1d5e58b2013-01-31 08:41:402274 ValidateIntegerPref(app->id(), "location", Manifest::INTERNAL);
[email protected]6d2e60bd2010-06-03 22:37:392275
2276 // Another app with non-overlapping extent. Should succeed.
[email protected]f484f8d52014-06-12 08:38:182277 PackAndInstallCRX(data_dir().AppendASCII("app2"), INSTALL_NEW);
[email protected]6d2e60bd2010-06-03 22:37:392278 ValidatePrefKeyCount(++pref_count);
2279
2280 // A third app whose extent overlaps the first. Should fail.
[email protected]f484f8d52014-06-12 08:38:182281 PackAndInstallCRX(data_dir().AppendASCII("app3"), INSTALL_FAILED);
[email protected]cd10e282010-10-26 21:04:512282 ValidatePrefKeyCount(pref_count);
[email protected]6d2e60bd2010-06-03 22:37:392283}
2284
[email protected]b6e64fd2011-08-09 19:49:192285// Tests that file access is OFF by default.
[email protected]d9a61e12012-11-14 02:43:472286TEST_F(ExtensionServiceTest, DefaultFileAccess) {
[email protected]b6e64fd2011-08-09 19:49:192287 InitializeEmptyExtensionService();
[email protected]f484f8d52014-06-12 08:38:182288 const Extension* extension = PackAndInstallCRX(
2289 data_dir().AppendASCII("permissions").AppendASCII("files"), INSTALL_NEW);
[email protected]b6e64fd2011-08-09 19:49:192290 EXPECT_EQ(0u, GetErrors().size());
[email protected]f484f8d52014-06-12 08:38:182291 EXPECT_EQ(1u, registry()->enabled_extensions().size());
[email protected]7c82539c2014-02-19 06:09:172292 EXPECT_FALSE(
[email protected]f484f8d52014-06-12 08:38:182293 ExtensionPrefs::Get(profile())->AllowFileAccess(extension->id()));
[email protected]b6e64fd2011-08-09 19:49:192294}
2295
[email protected]d9a61e12012-11-14 02:43:472296TEST_F(ExtensionServiceTest, UpdateApps) {
[email protected]15300d92011-01-19 18:44:302297 InitializeEmptyExtensionService();
[email protected]f484f8d52014-06-12 08:38:182298 base::FilePath extensions_path = data_dir().AppendASCII("app_update");
[email protected]15300d92011-01-19 18:44:302299
2300 // First install v1 of a hosted app.
[email protected]8f512c72011-11-22 21:02:502301 const Extension* extension =
2302 InstallCRX(extensions_path.AppendASCII("v1.crx"), INSTALL_NEW);
[email protected]f484f8d52014-06-12 08:38:182303 ASSERT_EQ(1u, registry()->enabled_extensions().size());
[email protected]8f512c72011-11-22 21:02:502304 std::string id = extension->id();
Devlin Cronin03bf2d22017-12-20 08:21:052305 ASSERT_EQ(std::string("1"), extension->version().GetString());
[email protected]15300d92011-01-19 18:44:302306
2307 // Now try updating to v2.
2308 UpdateExtension(id,
2309 extensions_path.AppendASCII("v2.crx"),
2310 ENABLED);
2311 ASSERT_EQ(std::string("2"),
Devlin Cronin03bf2d22017-12-20 08:21:052312 service()->GetExtensionById(id, false)->version().GetString());
[email protected]15300d92011-01-19 18:44:302313}
2314
[email protected]e6a6c2e02012-01-13 00:06:482315// Verifies that the NTP page and launch ordinals are kept when updating apps.
[email protected]d9a61e12012-11-14 02:43:472316TEST_F(ExtensionServiceTest, UpdateAppsRetainOrdinals) {
[email protected]e6a6c2e02012-01-13 00:06:482317 InitializeEmptyExtensionService();
deepak.m14ba69e62015-11-17 05:42:122318 AppSorting* sorting = ExtensionSystem::Get(profile())->app_sorting();
[email protected]f484f8d52014-06-12 08:38:182319 base::FilePath extensions_path = data_dir().AppendASCII("app_update");
[email protected]e6a6c2e02012-01-13 00:06:482320
2321 // First install v1 of a hosted app.
2322 const Extension* extension =
2323 InstallCRX(extensions_path.AppendASCII("v1.crx"), INSTALL_NEW);
[email protected]f484f8d52014-06-12 08:38:182324 ASSERT_EQ(1u, registry()->enabled_extensions().size());
[email protected]e6a6c2e02012-01-13 00:06:482325 std::string id = extension->id();
Devlin Cronin03bf2d22017-12-20 08:21:052326 ASSERT_EQ(std::string("1"), extension->version().GetString());
[email protected]e6a6c2e02012-01-13 00:06:482327
2328 // Modify the ordinals so we can distinguish them from the defaults.
[email protected]36b643212012-09-07 12:53:002329 syncer::StringOrdinal new_page_ordinal =
2330 sorting->GetPageOrdinal(id).CreateAfter();
2331 syncer::StringOrdinal new_launch_ordinal =
[email protected]e6a6c2e02012-01-13 00:06:482332 sorting->GetAppLaunchOrdinal(id).CreateBefore();
2333
2334 sorting->SetPageOrdinal(id, new_page_ordinal);
2335 sorting->SetAppLaunchOrdinal(id, new_launch_ordinal);
2336
2337 // Now try updating to v2.
2338 UpdateExtension(id, extensions_path.AppendASCII("v2.crx"), ENABLED);
2339 ASSERT_EQ(std::string("2"),
Devlin Cronin03bf2d22017-12-20 08:21:052340 service()->GetExtensionById(id, false)->version().GetString());
[email protected]e6a6c2e02012-01-13 00:06:482341
2342 // Verify that the ordinals match.
[email protected]36b643212012-09-07 12:53:002343 ASSERT_TRUE(new_page_ordinal.Equals(sorting->GetPageOrdinal(id)));
2344 ASSERT_TRUE(new_launch_ordinal.Equals(sorting->GetAppLaunchOrdinal(id)));
[email protected]e6a6c2e02012-01-13 00:06:482345}
2346
[email protected]b873cd92012-02-09 21:51:482347// Ensures that the CWS has properly initialized ordinals.
[email protected]d9a61e12012-11-14 02:43:472348TEST_F(ExtensionServiceTest, EnsureCWSOrdinalsInitialized) {
[email protected]b873cd92012-02-09 21:51:482349 InitializeEmptyExtensionService();
[email protected]f484f8d52014-06-12 08:38:182350 service()->component_loader()->Add(
[email protected]650b2d52013-02-10 03:41:452351 IDR_WEBSTORE_MANIFEST, base::FilePath(FILE_PATH_LITERAL("web_store")));
[email protected]f484f8d52014-06-12 08:38:182352 service()->Init();
[email protected]b873cd92012-02-09 21:51:482353
deepak.m14ba69e62015-11-17 05:42:122354 AppSorting* sorting = ExtensionSystem::Get(profile())->app_sorting();
[email protected]b873cd92012-02-09 21:51:482355 EXPECT_TRUE(
hanxia3182da2014-09-02 22:51:192356 sorting->GetPageOrdinal(extensions::kWebStoreAppId).IsValid());
[email protected]b873cd92012-02-09 21:51:482357 EXPECT_TRUE(
hanxia3182da2014-09-02 22:51:192358 sorting->GetAppLaunchOrdinal(extensions::kWebStoreAppId).IsValid());
[email protected]b873cd92012-02-09 21:51:482359}
2360
[email protected]fe10c202013-03-11 23:44:342361TEST_F(ExtensionServiceTest, InstallAppsWithUnlimitedStorage) {
[email protected]eaa7dd182010-12-14 11:09:002362 InitializeEmptyExtensionService();
[email protected]f484f8d52014-06-12 08:38:182363 EXPECT_TRUE(registry()->enabled_extensions().is_empty());
[email protected]c2c263c2010-08-13 21:59:482364
[email protected]c2c263c2010-08-13 21:59:482365 int pref_count = 0;
[email protected]c2c263c2010-08-13 21:59:482366
2367 // Install app1 with unlimited storage.
[email protected]8f512c72011-11-22 21:02:502368 const Extension* extension =
[email protected]f484f8d52014-06-12 08:38:182369 PackAndInstallCRX(data_dir().AppendASCII("app1"), INSTALL_NEW);
[email protected]c2c263c2010-08-13 21:59:482370 ValidatePrefKeyCount(++pref_count);
[email protected]f484f8d52014-06-12 08:38:182371 ASSERT_EQ(1u, registry()->enabled_extensions().size());
[email protected]c2c263c2010-08-13 21:59:482372 const std::string id1 = extension->id();
[email protected]076ebeda2014-06-06 21:47:262373 EXPECT_TRUE(extension->permissions_data()->HasAPIPermission(
[email protected]c2e66e12012-06-27 06:27:062374 APIPermission::kUnlimitedStorage));
[email protected]cced75a2011-05-20 08:31:122375 EXPECT_TRUE(extension->web_extent().MatchesURL(
[email protected]6b414c232013-06-05 07:53:342376 extensions::AppLaunchInfo::GetFullLaunchURL(extension)));
2377 const GURL origin1(
2378 extensions::AppLaunchInfo::GetFullLaunchURL(extension).GetOrigin());
[email protected]f484f8d52014-06-12 08:38:182379 EXPECT_TRUE(profile()->GetExtensionSpecialStoragePolicy()->IsStorageUnlimited(
2380 origin1));
[email protected]c2c263c2010-08-13 21:59:482381
2382 // Install app2 from the same origin with unlimited storage.
[email protected]f484f8d52014-06-12 08:38:182383 extension = PackAndInstallCRX(data_dir().AppendASCII("app2"), INSTALL_NEW);
[email protected]c2c263c2010-08-13 21:59:482384 ValidatePrefKeyCount(++pref_count);
[email protected]f484f8d52014-06-12 08:38:182385 ASSERT_EQ(2u, registry()->enabled_extensions().size());
[email protected]c2c263c2010-08-13 21:59:482386 const std::string id2 = extension->id();
[email protected]076ebeda2014-06-06 21:47:262387 EXPECT_TRUE(extension->permissions_data()->HasAPIPermission(
[email protected]c2e66e12012-06-27 06:27:062388 APIPermission::kUnlimitedStorage));
[email protected]cced75a2011-05-20 08:31:122389 EXPECT_TRUE(extension->web_extent().MatchesURL(
[email protected]6b414c232013-06-05 07:53:342390 extensions::AppLaunchInfo::GetFullLaunchURL(extension)));
2391 const GURL origin2(
2392 extensions::AppLaunchInfo::GetFullLaunchURL(extension).GetOrigin());
[email protected]c2c263c2010-08-13 21:59:482393 EXPECT_EQ(origin1, origin2);
[email protected]f484f8d52014-06-12 08:38:182394 EXPECT_TRUE(profile()->GetExtensionSpecialStoragePolicy()->IsStorageUnlimited(
2395 origin2));
[email protected]19eb80152011-02-26 00:28:432396
[email protected]c2c263c2010-08-13 21:59:482397 // Uninstall one of them, unlimited storage should still be granted
2398 // to the origin.
Devlin Cronin6fd1cd62017-12-05 19:13:572399 UninstallExtension(id1);
[email protected]f484f8d52014-06-12 08:38:182400 EXPECT_EQ(1u, registry()->enabled_extensions().size());
2401 EXPECT_TRUE(profile()->GetExtensionSpecialStoragePolicy()->IsStorageUnlimited(
2402 origin1));
[email protected]19eb80152011-02-26 00:28:432403
[email protected]c2c263c2010-08-13 21:59:482404 // Uninstall the other, unlimited storage should be revoked.
Devlin Cronin6fd1cd62017-12-05 19:13:572405 UninstallExtension(id2);
[email protected]f484f8d52014-06-12 08:38:182406 EXPECT_EQ(0u, registry()->enabled_extensions().size());
2407 EXPECT_FALSE(
2408 profile()->GetExtensionSpecialStoragePolicy()->IsStorageUnlimited(
2409 origin2));
[email protected]c2c263c2010-08-13 21:59:482410}
2411
[email protected]d9a61e12012-11-14 02:43:472412TEST_F(ExtensionServiceTest, InstallAppsAndCheckStorageProtection) {
[email protected]eaa7dd182010-12-14 11:09:002413 InitializeEmptyExtensionService();
[email protected]f484f8d52014-06-12 08:38:182414 EXPECT_TRUE(registry()->enabled_extensions().is_empty());
[email protected]654512b2010-09-01 02:09:422415
[email protected]654512b2010-09-01 02:09:422416 int pref_count = 0;
2417
[email protected]8f512c72011-11-22 21:02:502418 const Extension* extension =
[email protected]f484f8d52014-06-12 08:38:182419 PackAndInstallCRX(data_dir().AppendASCII("app1"), INSTALL_NEW);
[email protected]654512b2010-09-01 02:09:422420 ValidatePrefKeyCount(++pref_count);
[email protected]f484f8d52014-06-12 08:38:182421 ASSERT_EQ(1u, registry()->enabled_extensions().size());
[email protected]654512b2010-09-01 02:09:422422 EXPECT_TRUE(extension->is_app());
2423 const std::string id1 = extension->id();
[email protected]6b414c232013-06-05 07:53:342424 const GURL origin1(
2425 extensions::AppLaunchInfo::GetFullLaunchURL(extension).GetOrigin());
[email protected]f484f8d52014-06-12 08:38:182426 EXPECT_TRUE(profile()->GetExtensionSpecialStoragePolicy()->IsStorageProtected(
2427 origin1));
[email protected]654512b2010-09-01 02:09:422428
2429 // App 4 has a different origin (maps.google.com).
[email protected]f484f8d52014-06-12 08:38:182430 extension = PackAndInstallCRX(data_dir().AppendASCII("app4"), INSTALL_NEW);
[email protected]654512b2010-09-01 02:09:422431 ValidatePrefKeyCount(++pref_count);
[email protected]f484f8d52014-06-12 08:38:182432 ASSERT_EQ(2u, registry()->enabled_extensions().size());
[email protected]654512b2010-09-01 02:09:422433 const std::string id2 = extension->id();
[email protected]6b414c232013-06-05 07:53:342434 const GURL origin2(
2435 extensions::AppLaunchInfo::GetFullLaunchURL(extension).GetOrigin());
[email protected]654512b2010-09-01 02:09:422436 ASSERT_NE(origin1, origin2);
[email protected]f484f8d52014-06-12 08:38:182437 EXPECT_TRUE(profile()->GetExtensionSpecialStoragePolicy()->IsStorageProtected(
2438 origin2));
[email protected]654512b2010-09-01 02:09:422439
Devlin Cronin6fd1cd62017-12-05 19:13:572440 UninstallExtension(id1);
[email protected]f484f8d52014-06-12 08:38:182441 EXPECT_EQ(1u, registry()->enabled_extensions().size());
[email protected]654512b2010-09-01 02:09:422442
Devlin Cronin6fd1cd62017-12-05 19:13:572443 UninstallExtension(id2);
[email protected]654512b2010-09-01 02:09:422444
[email protected]f484f8d52014-06-12 08:38:182445 EXPECT_TRUE(registry()->enabled_extensions().is_empty());
2446 EXPECT_FALSE(
2447 profile()->GetExtensionSpecialStoragePolicy()->IsStorageProtected(
2448 origin1));
2449 EXPECT_FALSE(
2450 profile()->GetExtensionSpecialStoragePolicy()->IsStorageProtected(
2451 origin2));
[email protected]654512b2010-09-01 02:09:422452}
2453
[email protected]894bb502009-05-21 22:39:572454// Test that when an extension version is reinstalled, nothing happens.
[email protected]d9a61e12012-11-14 02:43:472455TEST_F(ExtensionServiceTest, Reinstall) {
[email protected]eaa7dd182010-12-14 11:09:002456 InitializeEmptyExtensionService();
[email protected]894bb502009-05-21 22:39:572457
2458 // A simple extension that should install without error.
[email protected]f484f8d52014-06-12 08:38:182459 base::FilePath path = data_dir().AppendASCII("good.crx");
[email protected]8f512c72011-11-22 21:02:502460 InstallCRX(path, INSTALL_NEW);
[email protected]894bb502009-05-21 22:39:572461
[email protected]25b34332009-06-05 21:53:192462 ValidatePrefKeyCount(1);
[email protected]e2194742010-08-12 05:54:342463 ValidateIntegerPref(good_crx, "state", Extension::ENABLED);
[email protected]1d5e58b2013-01-31 08:41:402464 ValidateIntegerPref(good_crx, "location", Manifest::INTERNAL);
[email protected]894bb502009-05-21 22:39:572465
[email protected]ca3dbf52010-05-19 22:27:062466 // Reinstall the same version, it should overwrite the previous one.
[email protected]8f512c72011-11-22 21:02:502467 InstallCRX(path, INSTALL_UPDATED);
[email protected]894bb502009-05-21 22:39:572468
[email protected]25b34332009-06-05 21:53:192469 ValidatePrefKeyCount(1);
[email protected]e2194742010-08-12 05:54:342470 ValidateIntegerPref(good_crx, "state", Extension::ENABLED);
[email protected]1d5e58b2013-01-31 08:41:402471 ValidateIntegerPref(good_crx, "location", Manifest::INTERNAL);
[email protected]894bb502009-05-21 22:39:572472}
2473
[email protected]620db1762011-07-15 21:57:342474// Test that we can determine if extensions came from the
2475// Chrome web store.
[email protected]d9a61e12012-11-14 02:43:472476TEST_F(ExtensionServiceTest, FromWebStore) {
[email protected]8266d662011-07-12 21:53:262477 InitializeEmptyExtensionService();
2478
2479 // A simple extension that should install without error.
[email protected]f484f8d52014-06-12 08:38:182480 base::FilePath path = data_dir().AppendASCII("good.crx");
[email protected]8f512c72011-11-22 21:02:502481 // Not from web store.
2482 const Extension* extension = InstallCRX(path, INSTALL_NEW);
2483 std::string id = extension->id();
[email protected]8266d662011-07-12 21:53:262484
[email protected]8266d662011-07-12 21:53:262485 ValidatePrefKeyCount(1);
[email protected]3f2a2fa2013-09-24 02:55:252486 ASSERT_TRUE(ValidateBooleanPref(good_crx, "from_webstore", false));
[email protected]620db1762011-07-15 21:57:342487 ASSERT_FALSE(extension->from_webstore());
[email protected]8266d662011-07-12 21:53:262488
[email protected]8266d662011-07-12 21:53:262489 // Test install from web store.
[email protected]8f512c72011-11-22 21:02:502490 InstallCRXFromWebStore(path, INSTALL_UPDATED); // From web store.
[email protected]8266d662011-07-12 21:53:262491
[email protected]8266d662011-07-12 21:53:262492 ValidatePrefKeyCount(1);
[email protected]3f2a2fa2013-09-24 02:55:252493 ASSERT_TRUE(ValidateBooleanPref(good_crx, "from_webstore", true));
[email protected]620db1762011-07-15 21:57:342494
2495 // Reload so extension gets reinitialized with new value.
[email protected]f484f8d52014-06-12 08:38:182496 service()->ReloadExtensionsForTest();
2497 extension = service()->GetExtensionById(id, false);
[email protected]620db1762011-07-15 21:57:342498 ASSERT_TRUE(extension->from_webstore());
[email protected]3d729722011-09-20 02:57:092499
2500 // Upgrade to version 2.0
[email protected]f484f8d52014-06-12 08:38:182501 path = data_dir().AppendASCII("good2.crx");
[email protected]3d729722011-09-20 02:57:092502 UpdateExtension(good_crx, path, ENABLED);
2503 ValidatePrefKeyCount(1);
[email protected]3f2a2fa2013-09-24 02:55:252504 ASSERT_TRUE(ValidateBooleanPref(good_crx, "from_webstore", true));
[email protected]8266d662011-07-12 21:53:262505}
2506
[email protected]fbcc40302009-06-12 20:45:452507// Test upgrading a signed extension.
[email protected]d9a61e12012-11-14 02:43:472508TEST_F(ExtensionServiceTest, UpgradeSignedGood) {
[email protected]eaa7dd182010-12-14 11:09:002509 InitializeEmptyExtensionService();
[email protected]fbcc40302009-06-12 20:45:452510
[email protected]f484f8d52014-06-12 08:38:182511 base::FilePath path = data_dir().AppendASCII("good.crx");
[email protected]8f512c72011-11-22 21:02:502512 const Extension* extension = InstallCRX(path, INSTALL_NEW);
2513 std::string id = extension->id();
[email protected]fbcc40302009-06-12 20:45:452514
Devlin Cronin03bf2d22017-12-20 08:21:052515 ASSERT_EQ("1.0.0.0", extension->version().GetString());
[email protected]fbcc40302009-06-12 20:45:452516 ASSERT_EQ(0u, GetErrors().size());
2517
[email protected]e7554c3f2013-05-29 00:36:562518 // Upgrade to version 1.0.0.1.
2519 // Also test that the extension's old and new title are correctly retrieved.
[email protected]f484f8d52014-06-12 08:38:182520 path = data_dir().AppendASCII("good2.crx");
[email protected]e7554c3f2013-05-29 00:36:562521 InstallCRX(path, INSTALL_UPDATED, Extension::NO_FLAGS, "My extension 1");
[email protected]f484f8d52014-06-12 08:38:182522 extension = service()->GetExtensionById(id, false);
[email protected]fbcc40302009-06-12 20:45:452523
Devlin Cronin03bf2d22017-12-20 08:21:052524 ASSERT_EQ("1.0.0.1", extension->version().GetString());
[email protected]e7554c3f2013-05-29 00:36:562525 ASSERT_EQ("My updated extension 1", extension->name());
[email protected]fbcc40302009-06-12 20:45:452526 ASSERT_EQ(0u, GetErrors().size());
2527}
2528
2529// Test upgrading a signed extension with a bad signature.
[email protected]d9a61e12012-11-14 02:43:472530TEST_F(ExtensionServiceTest, UpgradeSignedBad) {
[email protected]eaa7dd182010-12-14 11:09:002531 InitializeEmptyExtensionService();
[email protected]fbcc40302009-06-12 20:45:452532
[email protected]f484f8d52014-06-12 08:38:182533 base::FilePath path = data_dir().AppendASCII("good.crx");
[email protected]8f512c72011-11-22 21:02:502534 InstallCRX(path, INSTALL_NEW);
[email protected]fbcc40302009-06-12 20:45:452535
2536 // Try upgrading with a bad signature. This should fail during the unpack,
2537 // because the key will not match the signature.
[email protected]f484f8d52014-06-12 08:38:182538 path = data_dir().AppendASCII("bad_signature.crx");
[email protected]8f512c72011-11-22 21:02:502539 InstallCRX(path, INSTALL_FAILED);
[email protected]fbcc40302009-06-12 20:45:452540}
2541
[email protected]e957fe52009-06-23 16:51:052542// Test a normal update via the UpdateExtension API
[email protected]d9a61e12012-11-14 02:43:472543TEST_F(ExtensionServiceTest, UpdateExtension) {
[email protected]eaa7dd182010-12-14 11:09:002544 InitializeEmptyExtensionService();
[email protected]e957fe52009-06-23 16:51:052545
[email protected]f484f8d52014-06-12 08:38:182546 base::FilePath path = data_dir().AppendASCII("good.crx");
[email protected]e957fe52009-06-23 16:51:052547
[email protected]8f512c72011-11-22 21:02:502548 const Extension* good = InstallCRX(path, INSTALL_NEW);
[email protected]e957fe52009-06-23 16:51:052549 ASSERT_EQ("1.0.0.0", good->VersionString());
2550 ASSERT_EQ(good_crx, good->id());
2551
[email protected]f484f8d52014-06-12 08:38:182552 path = data_dir().AppendASCII("good2.crx");
[email protected]4416c5a2010-06-26 01:28:572553 UpdateExtension(good_crx, path, ENABLED);
[email protected]f484f8d52014-06-12 08:38:182554 ASSERT_EQ(
2555 "1.0.0.1",
Devlin Cronin03bf2d22017-12-20 08:21:052556 service()->GetExtensionById(good_crx, false)->version().GetString());
[email protected]e957fe52009-06-23 16:51:052557}
2558
[email protected]3c4abc82012-10-22 22:25:542559// Extensions should not be updated during browser shutdown.
[email protected]d9a61e12012-11-14 02:43:472560TEST_F(ExtensionServiceTest, UpdateExtensionDuringShutdown) {
[email protected]3c4abc82012-10-22 22:25:542561 InitializeEmptyExtensionService();
2562
2563 // Install an extension.
[email protected]f484f8d52014-06-12 08:38:182564 base::FilePath path = data_dir().AppendASCII("good.crx");
[email protected]3c4abc82012-10-22 22:25:542565 const Extension* good = InstallCRX(path, INSTALL_NEW);
2566 ASSERT_EQ(good_crx, good->id());
2567
2568 // Simulate shutdown.
[email protected]f484f8d52014-06-12 08:38:182569 service()->set_browser_terminating_for_test(true);
[email protected]3c4abc82012-10-22 22:25:542570
2571 // Update should fail and extension should not be updated.
[email protected]f484f8d52014-06-12 08:38:182572 path = data_dir().AppendASCII("good2.crx");
ginkage553af3202015-02-04 12:39:092573 bool updated = service()->UpdateExtension(
2574 extensions::CRXFileInfo(good_crx, path), true, NULL);
[email protected]3c4abc82012-10-22 22:25:542575 ASSERT_FALSE(updated);
[email protected]f484f8d52014-06-12 08:38:182576 ASSERT_EQ(
2577 "1.0.0.0",
Devlin Cronin03bf2d22017-12-20 08:21:052578 service()->GetExtensionById(good_crx, false)->version().GetString());
[email protected]3c4abc82012-10-22 22:25:542579}
2580
[email protected]e957fe52009-06-23 16:51:052581// Test updating a not-already-installed extension - this should fail
[email protected]d9a61e12012-11-14 02:43:472582TEST_F(ExtensionServiceTest, UpdateNotInstalledExtension) {
[email protected]eaa7dd182010-12-14 11:09:002583 InitializeEmptyExtensionService();
[email protected]e957fe52009-06-23 16:51:052584
[email protected]f484f8d52014-06-12 08:38:182585 base::FilePath path = data_dir().AppendASCII("good.crx");
[email protected]4416c5a2010-06-26 01:28:572586 UpdateExtension(good_crx, path, UPDATED);
Gabriel Charette01507a22017-09-27 21:30:082587 content::RunAllTasksUntilIdle();
[email protected]e957fe52009-06-23 16:51:052588
[email protected]f484f8d52014-06-12 08:38:182589 ASSERT_EQ(0u, registry()->enabled_extensions().size());
[email protected]e957fe52009-06-23 16:51:052590 ASSERT_FALSE(installed_);
2591 ASSERT_EQ(0u, loaded_.size());
2592}
2593
2594// Makes sure you can't downgrade an extension via UpdateExtension
[email protected]d9a61e12012-11-14 02:43:472595TEST_F(ExtensionServiceTest, UpdateWillNotDowngrade) {
[email protected]eaa7dd182010-12-14 11:09:002596 InitializeEmptyExtensionService();
[email protected]e957fe52009-06-23 16:51:052597
[email protected]f484f8d52014-06-12 08:38:182598 base::FilePath path = data_dir().AppendASCII("good2.crx");
[email protected]e957fe52009-06-23 16:51:052599
[email protected]8f512c72011-11-22 21:02:502600 const Extension* good = InstallCRX(path, INSTALL_NEW);
[email protected]e957fe52009-06-23 16:51:052601 ASSERT_EQ("1.0.0.1", good->VersionString());
2602 ASSERT_EQ(good_crx, good->id());
2603
2604 // Change path from good2.crx -> good.crx
[email protected]f484f8d52014-06-12 08:38:182605 path = data_dir().AppendASCII("good.crx");
[email protected]4416c5a2010-06-26 01:28:572606 UpdateExtension(good_crx, path, FAILED);
[email protected]f484f8d52014-06-12 08:38:182607 ASSERT_EQ(
2608 "1.0.0.1",
Devlin Cronin03bf2d22017-12-20 08:21:052609 service()->GetExtensionById(good_crx, false)->version().GetString());
[email protected]e957fe52009-06-23 16:51:052610}
2611
2612// Make sure calling update with an identical version does nothing
[email protected]d9a61e12012-11-14 02:43:472613TEST_F(ExtensionServiceTest, UpdateToSameVersionIsNoop) {
[email protected]eaa7dd182010-12-14 11:09:002614 InitializeEmptyExtensionService();
[email protected]e957fe52009-06-23 16:51:052615
[email protected]f484f8d52014-06-12 08:38:182616 base::FilePath path = data_dir().AppendASCII("good.crx");
[email protected]e957fe52009-06-23 16:51:052617
[email protected]8f512c72011-11-22 21:02:502618 const Extension* good = InstallCRX(path, INSTALL_NEW);
[email protected]e957fe52009-06-23 16:51:052619 ASSERT_EQ(good_crx, good->id());
[email protected]4416c5a2010-06-26 01:28:572620 UpdateExtension(good_crx, path, FAILED_SILENTLY);
[email protected]aa142702010-03-26 01:26:332621}
2622
[email protected]dbec3792010-08-10 00:08:452623// Tests that updating an extension does not clobber old state.
[email protected]d9a61e12012-11-14 02:43:472624TEST_F(ExtensionServiceTest, UpdateExtensionPreservesState) {
[email protected]eaa7dd182010-12-14 11:09:002625 InitializeEmptyExtensionService();
[email protected]dbec3792010-08-10 00:08:452626
[email protected]f484f8d52014-06-12 08:38:182627 base::FilePath path = data_dir().AppendASCII("good.crx");
[email protected]dbec3792010-08-10 00:08:452628
[email protected]8f512c72011-11-22 21:02:502629 const Extension* good = InstallCRX(path, INSTALL_NEW);
[email protected]dbec3792010-08-10 00:08:452630 ASSERT_EQ("1.0.0.0", good->VersionString());
2631 ASSERT_EQ(good_crx, good->id());
2632
2633 // Disable it and allow it to run in incognito. These settings should carry
2634 // over to the updated version.
Minh X. Nguyen45479012017-08-18 21:35:362635 service()->DisableExtension(good->id(),
2636 extensions::disable_reason::DISABLE_USER_ACTION);
[email protected]f484f8d52014-06-12 08:38:182637 extensions::util::SetIsIncognitoEnabled(good->id(), profile(), true);
[email protected]dbec3792010-08-10 00:08:452638
[email protected]f484f8d52014-06-12 08:38:182639 path = data_dir().AppendASCII("good2.crx");
[email protected]dbec3792010-08-10 00:08:452640 UpdateExtension(good_crx, path, INSTALLED);
[email protected]f484f8d52014-06-12 08:38:182641 ASSERT_EQ(1u, registry()->disabled_extensions().size());
2642 const Extension* good2 = service()->GetExtensionById(good_crx, true);
Devlin Cronin03bf2d22017-12-20 08:21:052643 ASSERT_EQ("1.0.0.1", good2->version().GetString());
[email protected]f484f8d52014-06-12 08:38:182644 EXPECT_TRUE(extensions::util::IsIncognitoEnabled(good2->id(), profile()));
Minh X. Nguyen45479012017-08-18 21:35:362645 EXPECT_EQ(extensions::disable_reason::DISABLE_USER_ACTION,
treibaac30ec2015-06-10 09:18:092646 ExtensionPrefs::Get(profile())->GetDisableReasons(good2->id()));
[email protected]dbec3792010-08-10 00:08:452647}
2648
[email protected]5eb375e92010-11-26 07:50:412649// Tests that updating preserves extension location.
[email protected]d9a61e12012-11-14 02:43:472650TEST_F(ExtensionServiceTest, UpdateExtensionPreservesLocation) {
[email protected]eaa7dd182010-12-14 11:09:002651 InitializeEmptyExtensionService();
[email protected]f484f8d52014-06-12 08:38:182652 base::FilePath path = data_dir().AppendASCII("good.crx");
[email protected]5eb375e92010-11-26 07:50:412653
catmullings22bc2372016-11-02 19:59:352654 const Extension* good = InstallCRX(path, Manifest::EXTERNAL_PREF, INSTALL_NEW,
2655 Extension::NO_FLAGS);
[email protected]5eb375e92010-11-26 07:50:412656
2657 ASSERT_EQ("1.0.0.0", good->VersionString());
2658 ASSERT_EQ(good_crx, good->id());
2659
[email protected]f484f8d52014-06-12 08:38:182660 path = data_dir().AppendASCII("good2.crx");
[email protected]5eb375e92010-11-26 07:50:412661 UpdateExtension(good_crx, path, ENABLED);
[email protected]f484f8d52014-06-12 08:38:182662 const Extension* good2 = service()->GetExtensionById(good_crx, false);
Devlin Cronin03bf2d22017-12-20 08:21:052663 ASSERT_EQ("1.0.0.1", good2->version().GetString());
[email protected]1d5e58b2013-01-31 08:41:402664 EXPECT_EQ(good2->location(), Manifest::EXTERNAL_PREF);
[email protected]5eb375e92010-11-26 07:50:412665}
2666
[email protected]66e26872010-12-03 20:07:252667// Makes sure that LOAD extension types can downgrade.
[email protected]d9a61e12012-11-14 02:43:472668TEST_F(ExtensionServiceTest, LoadExtensionsCanDowngrade) {
[email protected]eaa7dd182010-12-14 11:09:002669 InitializeEmptyExtensionService();
[email protected]66e26872010-12-03 20:07:252670
[email protected]ea1a3f62012-11-16 20:34:232671 base::ScopedTempDir temp;
[email protected]66e26872010-12-03 20:07:252672 ASSERT_TRUE(temp.CreateUniqueTempDir());
2673
2674 // We'll write the extension manifest dynamically to a temporary path
2675 // to make it easier to change the version number.
vabr9142fe22016-09-08 13:19:222676 base::FilePath extension_path = temp.GetPath();
[email protected]650b2d52013-02-10 03:41:452677 base::FilePath manifest_path =
[email protected]993da5e2013-03-23 21:25:162678 extension_path.Append(extensions::kManifestFilename);
[email protected]7567484142013-07-11 17:36:072679 ASSERT_FALSE(base::PathExists(manifest_path));
[email protected]66e26872010-12-03 20:07:252680
2681 // Start with version 2.0.
[email protected]023b3d12013-12-23 18:46:492682 base::DictionaryValue manifest;
[email protected]66e26872010-12-03 20:07:252683 manifest.SetString("version", "2.0");
2684 manifest.SetString("name", "LOAD Downgrade Test");
[email protected]b3d52852011-12-07 01:01:112685 manifest.SetInteger("manifest_version", 2);
[email protected]66e26872010-12-03 20:07:252686
2687 JSONFileValueSerializer serializer(manifest_path);
2688 ASSERT_TRUE(serializer.Serialize(manifest));
2689
[email protected]f484f8d52014-06-12 08:38:182690 extensions::UnpackedInstaller::Create(service())->Load(extension_path);
Gabriel Charette01507a22017-09-27 21:30:082691 content::RunAllTasksUntilIdle();
[email protected]66e26872010-12-03 20:07:252692
2693 EXPECT_EQ(0u, GetErrors().size());
2694 ASSERT_EQ(1u, loaded_.size());
[email protected]12075d12013-02-27 05:38:052695 EXPECT_EQ(Manifest::UNPACKED, loaded_[0]->location());
[email protected]f484f8d52014-06-12 08:38:182696 EXPECT_EQ(1u, registry()->enabled_extensions().size());
[email protected]66e26872010-12-03 20:07:252697 EXPECT_EQ("2.0", loaded_[0]->VersionString());
2698
2699 // Now set the version number to 1.0, reload the extensions and verify that
2700 // the downgrade was accepted.
2701 manifest.SetString("version", "1.0");
2702 ASSERT_TRUE(serializer.Serialize(manifest));
2703
[email protected]f484f8d52014-06-12 08:38:182704 extensions::UnpackedInstaller::Create(service())->Load(extension_path);
Gabriel Charette01507a22017-09-27 21:30:082705 content::RunAllTasksUntilIdle();
[email protected]66e26872010-12-03 20:07:252706
2707 EXPECT_EQ(0u, GetErrors().size());
2708 ASSERT_EQ(1u, loaded_.size());
[email protected]12075d12013-02-27 05:38:052709 EXPECT_EQ(Manifest::UNPACKED, loaded_[0]->location());
[email protected]f484f8d52014-06-12 08:38:182710 EXPECT_EQ(1u, registry()->enabled_extensions().size());
[email protected]66e26872010-12-03 20:07:252711 EXPECT_EQ("1.0", loaded_[0]->VersionString());
2712}
2713
[email protected]7fa19f82010-12-21 19:40:082714namespace {
2715
[email protected]8f3bcbd2013-06-05 08:42:402716bool IsExtension(const Extension* extension) {
2717 return extension->GetType() == Manifest::TYPE_EXTENSION;
[email protected]7fa19f82010-12-21 19:40:082718}
2719
[email protected]757d60a2014-05-23 00:11:442720#if defined(ENABLE_BLACKLIST_TESTS)
2721std::set<std::string> StringSet(const std::string& s) {
2722 std::set<std::string> set;
2723 set.insert(s);
2724 return set;
2725}
2726std::set<std::string> StringSet(const std::string& s1, const std::string& s2) {
2727 std::set<std::string> set = StringSet(s1);
2728 set.insert(s2);
2729 return set;
2730}
2731#endif // defined(ENABLE_BLACKLIST_TESTS)
2732
[email protected]7fa19f82010-12-21 19:40:082733} // namespace
2734
[email protected]aa142702010-03-26 01:26:332735// Test adding a pending extension.
[email protected]d9a61e12012-11-14 02:43:472736TEST_F(ExtensionServiceTest, AddPendingExtensionFromSync) {
[email protected]eaa7dd182010-12-14 11:09:002737 InitializeEmptyExtensionService();
[email protected]aa142702010-03-26 01:26:332738
[email protected]145a317b2011-04-12 16:03:462739 const std::string kFakeId(all_zero);
[email protected]aa142702010-03-26 01:26:332740 const GURL kFakeUpdateURL("http:://fake.update/url");
[email protected]21db9ef2014-05-16 02:06:272741 const bool kFakeRemoteInstall(false);
[email protected]aa142702010-03-26 01:26:332742
[email protected]21db9ef2014-05-16 02:06:272743 EXPECT_TRUE(
[email protected]6338fa32014-07-16 21:41:592744 service()->pending_extension_manager()->AddFromSync(
2745 kFakeId,
2746 kFakeUpdateURL,
treibe960e282015-09-11 10:38:082747 base::Version(),
[email protected]6338fa32014-07-16 21:41:592748 &IsExtension,
mamir192d7882016-06-22 17:10:162749 kFakeRemoteInstall));
[email protected]b2907fd2011-03-25 16:43:372750
[email protected]3f213ad2012-07-26 23:39:412751 const extensions::PendingExtensionInfo* pending_extension_info;
[email protected]f484f8d52014-06-12 08:38:182752 ASSERT_TRUE((pending_extension_info =
2753 service()->pending_extension_manager()->GetById(kFakeId)));
[email protected]51a3bf8b2012-06-08 22:53:062754 EXPECT_EQ(kFakeUpdateURL, pending_extension_info->update_url());
2755 EXPECT_EQ(&IsExtension, pending_extension_info->should_allow_install_);
[email protected]24b5fd42014-05-17 01:14:182756 // Use
2757 // EXPECT_TRUE(kFakeRemoteInstall == pending_extension_info->remote_install())
2758 // instead of
2759 // EXPECT_EQ(kFakeRemoteInstall, pending_extension_info->remote_install())
2760 // as gcc 4.7 issues the following warning on EXPECT_EQ(false, x), which is
2761 // turned into an error with -Werror=conversion-null:
2762 // converting 'false' to pointer type for argument 1 of
2763 // 'char testing::internal::IsNullLiteralHelper(testing::internal::Secret*)'
2764 // https://code.google.com/p/googletest/issues/detail?id=458
2765 EXPECT_TRUE(kFakeRemoteInstall == pending_extension_info->remote_install());
[email protected]aa142702010-03-26 01:26:332766}
2767
[email protected]aa142702010-03-26 01:26:332768namespace {
2769const char kGoodId[] = "ldnnhddmnhbkjipkidpdiheffobcpfmf";
2770const char kGoodUpdateURL[] = "http://good.update/url";
treibe960e282015-09-11 10:38:082771const char kGoodVersion[] = "1";
[email protected]8ef78fd2010-08-19 17:14:322772const bool kGoodIsFromSync = true;
[email protected]21db9ef2014-05-16 02:06:272773const bool kGoodRemoteInstall = false;
[email protected]aa142702010-03-26 01:26:332774} // namespace
2775
treibe960e282015-09-11 10:38:082776// Test installing a pending extension (this goes through
2777// ExtensionService::UpdateExtension).
[email protected]d9a61e12012-11-14 02:43:472778TEST_F(ExtensionServiceTest, UpdatePendingExtension) {
[email protected]eaa7dd182010-12-14 11:09:002779 InitializeEmptyExtensionService();
[email protected]21db9ef2014-05-16 02:06:272780 EXPECT_TRUE(
[email protected]6338fa32014-07-16 21:41:592781 service()->pending_extension_manager()->AddFromSync(
2782 kGoodId,
2783 GURL(kGoodUpdateURL),
treibe960e282015-09-11 10:38:082784 base::Version(kGoodVersion),
[email protected]6338fa32014-07-16 21:41:592785 &IsExtension,
mamir192d7882016-06-22 17:10:162786 kGoodRemoteInstall));
[email protected]f484f8d52014-06-12 08:38:182787 EXPECT_TRUE(service()->pending_extension_manager()->IsIdPending(kGoodId));
[email protected]aa142702010-03-26 01:26:332788
[email protected]f484f8d52014-06-12 08:38:182789 base::FilePath path = data_dir().AppendASCII("good.crx");
[email protected]6cc7dbae2011-04-29 21:18:332790 UpdateExtension(kGoodId, path, ENABLED);
[email protected]aa142702010-03-26 01:26:332791
[email protected]f484f8d52014-06-12 08:38:182792 EXPECT_FALSE(service()->pending_extension_manager()->IsIdPending(kGoodId));
[email protected]4416c5a2010-06-26 01:28:572793
[email protected]f484f8d52014-06-12 08:38:182794 const Extension* extension = service()->GetExtensionById(kGoodId, true);
treibe960e282015-09-11 10:38:082795 EXPECT_TRUE(extension);
2796}
2797
2798TEST_F(ExtensionServiceTest, UpdatePendingExtensionWrongVersion) {
2799 InitializeEmptyExtensionService();
2800 base::Version other_version("0.1");
2801 ASSERT_TRUE(other_version.IsValid());
robpercivaldcd8b102016-01-25 19:39:002802 ASSERT_NE(other_version, base::Version(kGoodVersion));
treibe960e282015-09-11 10:38:082803 EXPECT_TRUE(
2804 service()->pending_extension_manager()->AddFromSync(
2805 kGoodId,
2806 GURL(kGoodUpdateURL),
2807 other_version,
2808 &IsExtension,
mamir192d7882016-06-22 17:10:162809 kGoodRemoteInstall));
treibe960e282015-09-11 10:38:082810 EXPECT_TRUE(service()->pending_extension_manager()->IsIdPending(kGoodId));
2811
2812 base::FilePath path = data_dir().AppendASCII("good.crx");
2813 // After installation, the extension should be disabled, because it's missing
2814 // permissions.
2815 UpdateExtension(kGoodId, path, DISABLED);
2816
2817 EXPECT_TRUE(
2818 ExtensionPrefs::Get(profile())->DidExtensionEscalatePermissions(kGoodId));
2819
2820 // It should still have been installed though.
2821 EXPECT_FALSE(service()->pending_extension_manager()->IsIdPending(kGoodId));
2822
2823 const Extension* extension = service()->GetExtensionById(kGoodId, true);
2824 EXPECT_TRUE(extension);
[email protected]aa142702010-03-26 01:26:332825}
2826
[email protected]7fa19f82010-12-21 19:40:082827namespace {
2828
[email protected]8f3bcbd2013-06-05 08:42:402829bool IsTheme(const Extension* extension) {
2830 return extension->is_theme();
[email protected]7fa19f82010-12-21 19:40:082831}
2832
2833} // namespace
2834
[email protected]11edd1e2010-07-21 00:14:502835// Test updating a pending theme.
rdevlin.croninaa55dfc2017-03-23 15:03:042836TEST_F(ExtensionServiceTest, UpdatePendingTheme) {
[email protected]eaa7dd182010-12-14 11:09:002837 InitializeEmptyExtensionService();
[email protected]f484f8d52014-06-12 08:38:182838 EXPECT_TRUE(service()->pending_extension_manager()->AddFromSync(
mamir192d7882016-06-22 17:10:162839 theme_crx, GURL(), base::Version(), &IsTheme, false));
[email protected]f484f8d52014-06-12 08:38:182840 EXPECT_TRUE(service()->pending_extension_manager()->IsIdPending(theme_crx));
[email protected]11edd1e2010-07-21 00:14:502841
[email protected]f484f8d52014-06-12 08:38:182842 base::FilePath path = data_dir().AppendASCII("theme.crx");
[email protected]11edd1e2010-07-21 00:14:502843 UpdateExtension(theme_crx, path, ENABLED);
2844
[email protected]f484f8d52014-06-12 08:38:182845 EXPECT_FALSE(service()->pending_extension_manager()->IsIdPending(theme_crx));
[email protected]11edd1e2010-07-21 00:14:502846
[email protected]f484f8d52014-06-12 08:38:182847 const Extension* extension = service()->GetExtensionById(theme_crx, true);
[email protected]11edd1e2010-07-21 00:14:502848 ASSERT_TRUE(extension);
2849
[email protected]f484f8d52014-06-12 08:38:182850 EXPECT_FALSE(
2851 ExtensionPrefs::Get(profile())->IsExtensionDisabled(extension->id()));
2852 EXPECT_TRUE(service()->IsExtensionEnabled(theme_crx));
[email protected]11edd1e2010-07-21 00:14:502853}
2854
[email protected]8ef78fd2010-08-19 17:14:322855// Test updating a pending CRX as if the source is an external extension
2856// with an update URL. In this case we don't know if the CRX is a theme
2857// or not.
rdevlin.cronin1428ea02017-03-22 21:15:322858TEST_F(ExtensionServiceTest, UpdatePendingExternalCrx) {
[email protected]eaa7dd182010-12-14 11:09:002859 InitializeEmptyExtensionService();
[email protected]f484f8d52014-06-12 08:38:182860 EXPECT_TRUE(service()->pending_extension_manager()->AddFromExternalUpdateUrl(
[email protected]d8fd0fd2014-03-24 13:16:062861 theme_crx,
2862 std::string(),
2863 GURL(),
2864 Manifest::EXTERNAL_PREF_DOWNLOAD,
2865 Extension::NO_FLAGS,
[email protected]464213a2013-10-15 01:06:482866 false));
[email protected]8ef78fd2010-08-19 17:14:322867
[email protected]f484f8d52014-06-12 08:38:182868 EXPECT_TRUE(service()->pending_extension_manager()->IsIdPending(theme_crx));
[email protected]8ef78fd2010-08-19 17:14:322869
[email protected]f484f8d52014-06-12 08:38:182870 base::FilePath path = data_dir().AppendASCII("theme.crx");
[email protected]8ef78fd2010-08-19 17:14:322871 UpdateExtension(theme_crx, path, ENABLED);
2872
[email protected]f484f8d52014-06-12 08:38:182873 EXPECT_FALSE(service()->pending_extension_manager()->IsIdPending(theme_crx));
[email protected]8ef78fd2010-08-19 17:14:322874
[email protected]f484f8d52014-06-12 08:38:182875 const Extension* extension = service()->GetExtensionById(theme_crx, true);
[email protected]8ef78fd2010-08-19 17:14:322876 ASSERT_TRUE(extension);
2877
[email protected]f484f8d52014-06-12 08:38:182878 EXPECT_FALSE(
2879 ExtensionPrefs::Get(profile())->IsExtensionDisabled(extension->id()));
2880 EXPECT_TRUE(service()->IsExtensionEnabled(extension->id()));
2881 EXPECT_FALSE(
2882 extensions::util::IsIncognitoEnabled(extension->id(), profile()));
[email protected]8ef78fd2010-08-19 17:14:322883}
2884
[email protected]1afaf2a52010-11-02 19:29:172885// Test updating a pending CRX as if the source is an external extension
2886// with an update URL. The external update should overwrite a sync update,
2887// but a sync update should not overwrite a non-sync update.
[email protected]d9a61e12012-11-14 02:43:472888TEST_F(ExtensionServiceTest, UpdatePendingExternalCrxWinsOverSync) {
[email protected]eaa7dd182010-12-14 11:09:002889 InitializeEmptyExtensionService();
[email protected]1afaf2a52010-11-02 19:29:172890
2891 // Add a crx to be installed from the update mechanism.
[email protected]21db9ef2014-05-16 02:06:272892 EXPECT_TRUE(
[email protected]6338fa32014-07-16 21:41:592893 service()->pending_extension_manager()->AddFromSync(
2894 kGoodId,
2895 GURL(kGoodUpdateURL),
treibe960e282015-09-11 10:38:082896 base::Version(),
[email protected]6338fa32014-07-16 21:41:592897 &IsExtension,
mamir192d7882016-06-22 17:10:162898 kGoodRemoteInstall));
[email protected]1afaf2a52010-11-02 19:29:172899
2900 // Check that there is a pending crx, with is_from_sync set to true.
[email protected]3f213ad2012-07-26 23:39:412901 const extensions::PendingExtensionInfo* pending_extension_info;
[email protected]f484f8d52014-06-12 08:38:182902 ASSERT_TRUE((pending_extension_info =
2903 service()->pending_extension_manager()->GetById(kGoodId)));
[email protected]51a3bf8b2012-06-08 22:53:062904 EXPECT_TRUE(pending_extension_info->is_from_sync());
[email protected]1afaf2a52010-11-02 19:29:172905
2906 // Add a crx to be updated, with the same ID, from a non-sync source.
[email protected]f484f8d52014-06-12 08:38:182907 EXPECT_TRUE(service()->pending_extension_manager()->AddFromExternalUpdateUrl(
[email protected]d8fd0fd2014-03-24 13:16:062908 kGoodId,
2909 std::string(),
2910 GURL(kGoodUpdateURL),
2911 Manifest::EXTERNAL_PREF_DOWNLOAD,
2912 Extension::NO_FLAGS,
2913 false));
[email protected]1afaf2a52010-11-02 19:29:172914
2915 // Check that there is a pending crx, with is_from_sync set to false.
[email protected]f484f8d52014-06-12 08:38:182916 ASSERT_TRUE((pending_extension_info =
2917 service()->pending_extension_manager()->GetById(kGoodId)));
[email protected]51a3bf8b2012-06-08 22:53:062918 EXPECT_FALSE(pending_extension_info->is_from_sync());
[email protected]1d5e58b2013-01-31 08:41:402919 EXPECT_EQ(Manifest::EXTERNAL_PREF_DOWNLOAD,
[email protected]51a3bf8b2012-06-08 22:53:062920 pending_extension_info->install_source());
[email protected]1afaf2a52010-11-02 19:29:172921
2922 // Add a crx to be installed from the update mechanism.
[email protected]21db9ef2014-05-16 02:06:272923 EXPECT_FALSE(
[email protected]6338fa32014-07-16 21:41:592924 service()->pending_extension_manager()->AddFromSync(
2925 kGoodId,
2926 GURL(kGoodUpdateURL),
treibe960e282015-09-11 10:38:082927 base::Version(),
[email protected]6338fa32014-07-16 21:41:592928 &IsExtension,
mamir192d7882016-06-22 17:10:162929 kGoodRemoteInstall));
[email protected]1afaf2a52010-11-02 19:29:172930
2931 // Check that the external, non-sync update was not overridden.
[email protected]f484f8d52014-06-12 08:38:182932 ASSERT_TRUE((pending_extension_info =
2933 service()->pending_extension_manager()->GetById(kGoodId)));
[email protected]51a3bf8b2012-06-08 22:53:062934 EXPECT_FALSE(pending_extension_info->is_from_sync());
[email protected]1d5e58b2013-01-31 08:41:402935 EXPECT_EQ(Manifest::EXTERNAL_PREF_DOWNLOAD,
[email protected]51a3bf8b2012-06-08 22:53:062936 pending_extension_info->install_source());
[email protected]1afaf2a52010-11-02 19:29:172937}
2938
[email protected]8ef78fd2010-08-19 17:14:322939// Updating a theme should fail if the updater is explicitly told that
2940// the CRX is not a theme.
[email protected]d9a61e12012-11-14 02:43:472941TEST_F(ExtensionServiceTest, UpdatePendingCrxThemeMismatch) {
[email protected]eaa7dd182010-12-14 11:09:002942 InitializeEmptyExtensionService();
[email protected]f484f8d52014-06-12 08:38:182943 EXPECT_TRUE(service()->pending_extension_manager()->AddFromSync(
mamir192d7882016-06-22 17:10:162944 theme_crx, GURL(), base::Version(), &IsExtension, false));
[email protected]8ef78fd2010-08-19 17:14:322945
[email protected]f484f8d52014-06-12 08:38:182946 EXPECT_TRUE(service()->pending_extension_manager()->IsIdPending(theme_crx));
[email protected]8ef78fd2010-08-19 17:14:322947
[email protected]f484f8d52014-06-12 08:38:182948 base::FilePath path = data_dir().AppendASCII("theme.crx");
[email protected]8ef78fd2010-08-19 17:14:322949 UpdateExtension(theme_crx, path, FAILED_SILENTLY);
2950
[email protected]f484f8d52014-06-12 08:38:182951 EXPECT_FALSE(service()->pending_extension_manager()->IsIdPending(theme_crx));
[email protected]8ef78fd2010-08-19 17:14:322952
[email protected]f484f8d52014-06-12 08:38:182953 const Extension* extension = service()->GetExtensionById(theme_crx, true);
[email protected]8ef78fd2010-08-19 17:14:322954 ASSERT_FALSE(extension);
2955}
2956
[email protected]aa142702010-03-26 01:26:332957// TODO(akalin): Test updating a pending extension non-silently once
2958// we can mock out ExtensionInstallUI and inject our version into
2959// UpdateExtension().
2960
[email protected]7fa19f82010-12-21 19:40:082961// Test updating a pending extension which fails the should-install test.
[email protected]d9a61e12012-11-14 02:43:472962TEST_F(ExtensionServiceTest, UpdatePendingExtensionFailedShouldInstallTest) {
[email protected]eaa7dd182010-12-14 11:09:002963 InitializeEmptyExtensionService();
[email protected]aa142702010-03-26 01:26:332964 // Add pending extension with a flipped is_theme.
[email protected]21db9ef2014-05-16 02:06:272965 EXPECT_TRUE(
[email protected]6338fa32014-07-16 21:41:592966 service()->pending_extension_manager()->AddFromSync(
2967 kGoodId,
2968 GURL(kGoodUpdateURL),
treibe960e282015-09-11 10:38:082969 base::Version(),
[email protected]6338fa32014-07-16 21:41:592970 &IsTheme,
mamir192d7882016-06-22 17:10:162971 kGoodRemoteInstall));
[email protected]f484f8d52014-06-12 08:38:182972 EXPECT_TRUE(service()->pending_extension_manager()->IsIdPending(kGoodId));
[email protected]aa142702010-03-26 01:26:332973
[email protected]f484f8d52014-06-12 08:38:182974 base::FilePath path = data_dir().AppendASCII("good.crx");
[email protected]4416c5a2010-06-26 01:28:572975 UpdateExtension(kGoodId, path, UPDATED);
[email protected]aa142702010-03-26 01:26:332976
2977 // TODO(akalin): Figure out how to check that the extensions
2978 // directory is cleaned up properly in OnExtensionInstalled().
2979
[email protected]f484f8d52014-06-12 08:38:182980 EXPECT_FALSE(service()->pending_extension_manager()->IsIdPending(kGoodId));
[email protected]aa142702010-03-26 01:26:332981}
2982
[email protected]11edd1e2010-07-21 00:14:502983// TODO(akalin): Figure out how to test that installs of pending
2984// unsyncable extensions are blocked.
2985
[email protected]aa142702010-03-26 01:26:332986// Test updating a pending extension for one that is not pending.
[email protected]d9a61e12012-11-14 02:43:472987TEST_F(ExtensionServiceTest, UpdatePendingExtensionNotPending) {
[email protected]eaa7dd182010-12-14 11:09:002988 InitializeEmptyExtensionService();
[email protected]aa142702010-03-26 01:26:332989
[email protected]f484f8d52014-06-12 08:38:182990 base::FilePath path = data_dir().AppendASCII("good.crx");
[email protected]4416c5a2010-06-26 01:28:572991 UpdateExtension(kGoodId, path, UPDATED);
[email protected]aa142702010-03-26 01:26:332992
[email protected]f484f8d52014-06-12 08:38:182993 EXPECT_FALSE(service()->pending_extension_manager()->IsIdPending(kGoodId));
[email protected]aa142702010-03-26 01:26:332994}
2995
2996// Test updating a pending extension for one that is already
2997// installed.
[email protected]d9a61e12012-11-14 02:43:472998TEST_F(ExtensionServiceTest, UpdatePendingExtensionAlreadyInstalled) {
[email protected]eaa7dd182010-12-14 11:09:002999 InitializeEmptyExtensionService();
[email protected]aa142702010-03-26 01:26:333000
[email protected]f484f8d52014-06-12 08:38:183001 base::FilePath path = data_dir().AppendASCII("good.crx");
[email protected]8f512c72011-11-22 21:02:503002 const Extension* good = InstallCRX(path, INSTALL_NEW);
[email protected]f484f8d52014-06-12 08:38:183003 ASSERT_EQ(1u, registry()->enabled_extensions().size());
[email protected]aa142702010-03-26 01:26:333004
[email protected]8ef78fd2010-08-19 17:14:323005 EXPECT_FALSE(good->is_theme());
3006
[email protected]b2907fd2011-03-25 16:43:373007 // Use AddExtensionImpl() as AddFrom*() would balk.
[email protected]f484f8d52014-06-12 08:38:183008 service()->pending_extension_manager()->AddExtensionImpl(
[email protected]d8fd0fd2014-03-24 13:16:063009 good->id(),
3010 std::string(),
3011 extensions::ManifestURL::GetUpdateURL(good),
pwnallcbd73192016-08-22 18:59:173012 base::Version(),
[email protected]d8fd0fd2014-03-24 13:16:063013 &IsExtension,
3014 kGoodIsFromSync,
[email protected]d8fd0fd2014-03-24 13:16:063015 Manifest::INTERNAL,
mamir0128d5a2016-07-15 20:55:483016 Extension::NO_FLAGS,
[email protected]21db9ef2014-05-16 02:06:273017 false,
3018 kGoodRemoteInstall);
[email protected]6cc7dbae2011-04-29 21:18:333019 UpdateExtension(good->id(), path, ENABLED);
[email protected]aa142702010-03-26 01:26:333020
[email protected]f484f8d52014-06-12 08:38:183021 EXPECT_FALSE(service()->pending_extension_manager()->IsIdPending(kGoodId));
[email protected]e957fe52009-06-23 16:51:053022}
3023
[email protected]3f2a2fa2013-09-24 02:55:253024#if defined(ENABLE_BLACKLIST_TESTS)
3025// Tests blacklisting then unblacklisting extensions after the service has been
3026// initialized.
[email protected]7d9b4e32013-10-25 06:11:543027TEST_F(ExtensionServiceTest, SetUnsetBlacklistInPrefs) {
[email protected]f71b582c2014-01-10 17:03:153028 extensions::TestBlacklist test_blacklist;
[email protected]3f2a2fa2013-09-24 02:55:253029 // A profile with 3 extensions installed: good0, good1, and good2.
3030 InitializeGoodInstalledExtensionService();
[email protected]f484f8d52014-06-12 08:38:183031 test_blacklist.Attach(service()->blacklist_);
3032 service()->Init();
[email protected]e636c812013-09-23 05:51:083033
[email protected]5fdfa562013-12-27 17:43:593034 const extensions::ExtensionSet& enabled_extensions =
[email protected]f484f8d52014-06-12 08:38:183035 registry()->enabled_extensions();
[email protected]5fdfa562013-12-27 17:43:593036 const extensions::ExtensionSet& blacklisted_extensions =
[email protected]f484f8d52014-06-12 08:38:183037 registry()->blacklisted_extensions();
[email protected]e636c812013-09-23 05:51:083038
[email protected]5fdfa562013-12-27 17:43:593039 EXPECT_TRUE(enabled_extensions.Contains(good0) &&
3040 !blacklisted_extensions.Contains(good0));
3041 EXPECT_TRUE(enabled_extensions.Contains(good1) &&
3042 !blacklisted_extensions.Contains(good1));
3043 EXPECT_TRUE(enabled_extensions.Contains(good2) &&
3044 !blacklisted_extensions.Contains(good2));
[email protected]e636c812013-09-23 05:51:083045
[email protected]3f2a2fa2013-09-24 02:55:253046 EXPECT_FALSE(IsPrefExist(good0, "blacklist"));
[email protected]e636c812013-09-23 05:51:083047 EXPECT_FALSE(IsPrefExist(good1, "blacklist"));
[email protected]3f2a2fa2013-09-24 02:55:253048 EXPECT_FALSE(IsPrefExist(good2, "blacklist"));
3049 EXPECT_FALSE(IsPrefExist("invalid_id", "blacklist"));
3050
3051 // Blacklist good0 and good1 (and an invalid extension ID).
[email protected]f71b582c2014-01-10 17:03:153052 test_blacklist.SetBlacklistState(
3053 good0, extensions::BLACKLISTED_MALWARE, true);
3054 test_blacklist.SetBlacklistState(
3055 good1, extensions::BLACKLISTED_MALWARE, true);
3056 test_blacklist.SetBlacklistState(
3057 "invalid_id", extensions::BLACKLISTED_MALWARE, true);
Gabriel Charette01507a22017-09-27 21:30:083058 content::RunAllTasksUntilIdle();
[email protected]3f2a2fa2013-09-24 02:55:253059
[email protected]5fdfa562013-12-27 17:43:593060 EXPECT_TRUE(!enabled_extensions.Contains(good0) &&
3061 blacklisted_extensions.Contains(good0));
3062 EXPECT_TRUE(!enabled_extensions.Contains(good1) &&
3063 blacklisted_extensions.Contains(good1));
3064 EXPECT_TRUE(enabled_extensions.Contains(good2) &&
3065 !blacklisted_extensions.Contains(good2));
[email protected]3f2a2fa2013-09-24 02:55:253066
3067 EXPECT_TRUE(ValidateBooleanPref(good0, "blacklist", true));
3068 EXPECT_TRUE(ValidateBooleanPref(good1, "blacklist", true));
3069 EXPECT_FALSE(IsPrefExist(good2, "blacklist"));
3070 EXPECT_FALSE(IsPrefExist("invalid_id", "blacklist"));
3071
3072 // Un-blacklist good1 and blacklist good2.
[email protected]f71b582c2014-01-10 17:03:153073 test_blacklist.Clear(false);
3074 test_blacklist.SetBlacklistState(
3075 good0, extensions::BLACKLISTED_MALWARE, true);
3076 test_blacklist.SetBlacklistState(
3077 good2, extensions::BLACKLISTED_MALWARE, true);
3078 test_blacklist.SetBlacklistState(
3079 "invalid_id", extensions::BLACKLISTED_MALWARE, true);
Gabriel Charette01507a22017-09-27 21:30:083080 content::RunAllTasksUntilIdle();
[email protected]3f2a2fa2013-09-24 02:55:253081
[email protected]5fdfa562013-12-27 17:43:593082 EXPECT_TRUE(!enabled_extensions.Contains(good0) &&
3083 blacklisted_extensions.Contains(good0));
3084 EXPECT_TRUE(enabled_extensions.Contains(good1) &&
3085 !blacklisted_extensions.Contains(good1));
3086 EXPECT_TRUE(!enabled_extensions.Contains(good2) &&
3087 blacklisted_extensions.Contains(good2));
[email protected]3f2a2fa2013-09-24 02:55:253088
3089 EXPECT_TRUE(ValidateBooleanPref(good0, "blacklist", true));
3090 EXPECT_FALSE(IsPrefExist(good1, "blacklist"));
3091 EXPECT_TRUE(ValidateBooleanPref(good2, "blacklist", true));
[email protected]e2194742010-08-12 05:54:343092 EXPECT_FALSE(IsPrefExist("invalid_id", "blacklist"));
[email protected]6b75ec32009-08-14 06:37:183093}
[email protected]3f2a2fa2013-09-24 02:55:253094#endif // defined(ENABLE_BLACKLIST_TESTS)
[email protected]6b75ec32009-08-14 06:37:183095
[email protected]3f2a2fa2013-09-24 02:55:253096#if defined(ENABLE_BLACKLIST_TESTS)
3097// Tests trying to install a blacklisted extension.
[email protected]e0cbfc42013-11-07 00:10:103098TEST_F(ExtensionServiceTest, BlacklistedExtensionWillNotInstall) {
[email protected]3f2a2fa2013-09-24 02:55:253099 scoped_refptr<FakeSafeBrowsingDatabaseManager> blacklist_db(
3100 new FakeSafeBrowsingDatabaseManager(true));
3101 Blacklist::ScopedDatabaseManagerForTest scoped_blacklist_db(blacklist_db);
3102
[email protected]1747eac02013-09-23 10:58:423103 InitializeEmptyExtensionService();
[email protected]f484f8d52014-06-12 08:38:183104 service()->Init();
[email protected]1747eac02013-09-23 10:58:423105
[email protected]3f2a2fa2013-09-24 02:55:253106 // After blacklisting good_crx, we cannot install it.
3107 blacklist_db->SetUnsafe(good_crx).NotifyUpdate();
Gabriel Charette01507a22017-09-27 21:30:083108 content::RunAllTasksUntilIdle();
[email protected]1747eac02013-09-23 10:58:423109
[email protected]f484f8d52014-06-12 08:38:183110 base::FilePath path = data_dir().AppendASCII("good.crx");
[email protected]9f3c8532013-07-31 19:52:073111 // HACK: specify WAS_INSTALLED_BY_DEFAULT so that test machinery doesn't
3112 // decide to install this silently. Somebody should fix these tests, all
3113 // 6,000 lines of them. Hah!
3114 InstallCRX(path, INSTALL_FAILED, Extension::WAS_INSTALLED_BY_DEFAULT);
[email protected]f484f8d52014-06-12 08:38:183115 EXPECT_EQ(0u, registry()->enabled_extensions().size());
[email protected]6b75ec32009-08-14 06:37:183116}
[email protected]3f2a2fa2013-09-24 02:55:253117#endif // defined(ENABLE_BLACKLIST_TESTS)
[email protected]6b75ec32009-08-14 06:37:183118
[email protected]3f2a2fa2013-09-24 02:55:253119#if defined(ENABLE_BLACKLIST_TESTS)
atuchin6dc7c442016-07-20 07:04:343120// Tests that previously blacklisted extension will be enabled if it is removed
3121// from the blacklist. Also checks that all blacklisted preferences will be
3122// cleared in that case.
3123TEST_F(ExtensionServiceTest, RemoveExtensionFromBlacklist) {
3124 extensions::TestBlacklist test_blacklist;
3125 // A profile with 3 extensions installed: good0, good1, and good2.
3126 InitializeGoodInstalledExtensionService();
3127 test_blacklist.Attach(service()->blacklist_);
3128 service()->Init();
3129
3130 ASSERT_TRUE(registry()->enabled_extensions().Contains(good0));
3131 extensions::TestExtensionRegistryObserver observer(
3132 extensions::ExtensionRegistry::Get(profile()), good0);
3133
3134 // Add the extension to the blacklist.
3135 test_blacklist.SetBlacklistState(good0, extensions::BLACKLISTED_MALWARE,
3136 true);
3137 observer.WaitForExtensionUnloaded();
3138
3139 // The extension should be disabled, both "blacklist" and "blacklist_state"
3140 // prefs should be set.
vmpstrae72b082016-07-25 21:55:473141 auto* prefs = ExtensionPrefs::Get(profile());
atuchin6dc7c442016-07-20 07:04:343142 EXPECT_FALSE(registry()->enabled_extensions().Contains(good0));
3143 EXPECT_TRUE(prefs->IsExtensionBlacklisted(good0));
3144 EXPECT_EQ(extensions::BLACKLISTED_MALWARE,
3145 prefs->GetExtensionBlacklistState(good0));
3146
3147 // Remove the extension from the blacklist.
3148 test_blacklist.SetBlacklistState(good0, extensions::NOT_BLACKLISTED, true);
3149 observer.WaitForExtensionLoaded()->id();
3150
3151 // The extension should be enabled, both "blacklist" and "blacklist_state"
3152 // should be cleared.
3153 EXPECT_TRUE(registry()->enabled_extensions().Contains(good0));
3154 EXPECT_FALSE(prefs->IsExtensionBlacklisted(good0));
3155 EXPECT_EQ(extensions::NOT_BLACKLISTED,
3156 prefs->GetExtensionBlacklistState(good0));
3157}
3158#endif // defined(ENABLE_BLACKLIST_TESTS)
3159
3160#if defined(ENABLE_BLACKLIST_TESTS)
[email protected]4ee07c62012-08-21 12:40:423161// Unload blacklisted extension on policy change.
[email protected]d9a61e12012-11-14 02:43:473162TEST_F(ExtensionServiceTest, UnloadBlacklistedExtensionPolicy) {
[email protected]f71b582c2014-01-10 17:03:153163 extensions::TestBlacklist test_blacklist;
[email protected]3f2a2fa2013-09-24 02:55:253164
3165 // A profile with no extensions installed.
binjin1569c9b2014-09-05 13:33:183166 InitializeEmptyExtensionServiceWithTestingPrefs();
[email protected]f484f8d52014-06-12 08:38:183167 test_blacklist.Attach(service()->blacklist_);
[email protected]3f2a2fa2013-09-24 02:55:253168
[email protected]f484f8d52014-06-12 08:38:183169 base::FilePath path = data_dir().AppendASCII("good.crx");
[email protected]4ee07c62012-08-21 12:40:423170
3171 const Extension* good = InstallCRX(path, INSTALL_NEW);
3172 EXPECT_EQ(good_crx, good->id());
3173 UpdateExtension(good_crx, path, FAILED_SILENTLY);
[email protected]f484f8d52014-06-12 08:38:183174 EXPECT_EQ(1u, registry()->enabled_extensions().size());
[email protected]4ee07c62012-08-21 12:40:423175
binjinb2454382014-09-22 15:17:433176 {
3177 ManagementPrefUpdater pref(profile_->GetTestingPrefService());
3178 pref.SetIndividualExtensionInstallationAllowed(good_crx, true);
3179 }
[email protected]4ee07c62012-08-21 12:40:423180
[email protected]f71b582c2014-01-10 17:03:153181 test_blacklist.SetBlacklistState(
3182 good_crx, extensions::BLACKLISTED_MALWARE, true);
Gabriel Charette01507a22017-09-27 21:30:083183 content::RunAllTasksUntilIdle();
[email protected]4ee07c62012-08-21 12:40:423184
[email protected]695b5712012-12-06 23:55:283185 // The good_crx is blacklisted and the whitelist doesn't negate it.
[email protected]3f2a2fa2013-09-24 02:55:253186 ASSERT_TRUE(ValidateBooleanPref(good_crx, "blacklist", true));
[email protected]f484f8d52014-06-12 08:38:183187 EXPECT_EQ(0u, registry()->enabled_extensions().size());
[email protected]4ee07c62012-08-21 12:40:423188}
[email protected]3f2a2fa2013-09-24 02:55:253189#endif // defined(ENABLE_BLACKLIST_TESTS)
[email protected]4ee07c62012-08-21 12:40:423190
[email protected]3f2a2fa2013-09-24 02:55:253191#if defined(ENABLE_BLACKLIST_TESTS)
3192// Tests that a blacklisted extension is eventually unloaded on startup, if it
3193// wasn't already.
[email protected]e0cbfc42013-11-07 00:10:103194TEST_F(ExtensionServiceTest, WillNotLoadBlacklistedExtensionsFromDirectory) {
[email protected]f71b582c2014-01-10 17:03:153195 extensions::TestBlacklist test_blacklist;
[email protected]6b75ec32009-08-14 06:37:183196
[email protected]3f2a2fa2013-09-24 02:55:253197 // A profile with 3 extensions installed: good0, good1, and good2.
3198 InitializeGoodInstalledExtensionService();
[email protected]f484f8d52014-06-12 08:38:183199 test_blacklist.Attach(service()->blacklist_);
[email protected]fdd679b2012-11-15 20:49:393200
[email protected]3f2a2fa2013-09-24 02:55:253201 // Blacklist good1 before the service initializes.
[email protected]f71b582c2014-01-10 17:03:153202 test_blacklist.SetBlacklistState(
3203 good1, extensions::BLACKLISTED_MALWARE, false);
[email protected]6b75ec32009-08-14 06:37:183204
3205 // Load extensions.
[email protected]f484f8d52014-06-12 08:38:183206 service()->Init();
[email protected]3f2a2fa2013-09-24 02:55:253207 ASSERT_EQ(3u, loaded_.size()); // hasn't had time to blacklist yet
[email protected]6b75ec32009-08-14 06:37:183208
Gabriel Charette01507a22017-09-27 21:30:083209 content::RunAllTasksUntilIdle();
[email protected]6b75ec32009-08-14 06:37:183210
[email protected]f484f8d52014-06-12 08:38:183211 ASSERT_EQ(1u, registry()->blacklisted_extensions().size());
3212 ASSERT_EQ(2u, registry()->enabled_extensions().size());
[email protected]5fdfa562013-12-27 17:43:593213
[email protected]f484f8d52014-06-12 08:38:183214 ASSERT_TRUE(registry()->enabled_extensions().Contains(good0));
3215 ASSERT_TRUE(registry()->blacklisted_extensions().Contains(good1));
3216 ASSERT_TRUE(registry()->enabled_extensions().Contains(good2));
[email protected]6b75ec32009-08-14 06:37:183217}
[email protected]3f2a2fa2013-09-24 02:55:253218#endif // defined(ENABLE_BLACKLIST_TESTS)
3219
3220#if defined(ENABLE_BLACKLIST_TESTS)
3221// Tests extensions blacklisted in prefs on startup; one still blacklisted by
3222// safe browsing, the other not. The not-blacklisted one should recover.
3223TEST_F(ExtensionServiceTest, BlacklistedInPrefsFromStartup) {
[email protected]f71b582c2014-01-10 17:03:153224 extensions::TestBlacklist test_blacklist;
[email protected]3f2a2fa2013-09-24 02:55:253225
3226 InitializeGoodInstalledExtensionService();
[email protected]f484f8d52014-06-12 08:38:183227 test_blacklist.Attach(service()->blacklist_);
atuchin6dc7c442016-07-20 07:04:343228 ExtensionPrefs::Get(profile())->SetExtensionBlacklistState(
3229 good0, extensions::BLACKLISTED_MALWARE);
3230 ExtensionPrefs::Get(profile())->SetExtensionBlacklistState(
3231 good1, extensions::BLACKLISTED_MALWARE);
[email protected]3f2a2fa2013-09-24 02:55:253232
[email protected]f71b582c2014-01-10 17:03:153233 test_blacklist.SetBlacklistState(
3234 good1, extensions::BLACKLISTED_MALWARE, false);
[email protected]3f2a2fa2013-09-24 02:55:253235
[email protected]27e528322014-05-27 20:54:303236 // Extension service hasn't loaded yet, but IsExtensionEnabled reads out of
3237 // prefs. Ensure it takes into account the blacklist state (crbug.com/373842).
[email protected]f484f8d52014-06-12 08:38:183238 EXPECT_FALSE(service()->IsExtensionEnabled(good0));
3239 EXPECT_FALSE(service()->IsExtensionEnabled(good1));
3240 EXPECT_TRUE(service()->IsExtensionEnabled(good2));
[email protected]27e528322014-05-27 20:54:303241
[email protected]f484f8d52014-06-12 08:38:183242 service()->Init();
[email protected]3f2a2fa2013-09-24 02:55:253243
[email protected]f484f8d52014-06-12 08:38:183244 EXPECT_EQ(2u, registry()->blacklisted_extensions().size());
3245 EXPECT_EQ(1u, registry()->enabled_extensions().size());
[email protected]3f2a2fa2013-09-24 02:55:253246
[email protected]f484f8d52014-06-12 08:38:183247 EXPECT_TRUE(registry()->blacklisted_extensions().Contains(good0));
3248 EXPECT_TRUE(registry()->blacklisted_extensions().Contains(good1));
3249 EXPECT_TRUE(registry()->enabled_extensions().Contains(good2));
[email protected]3f2a2fa2013-09-24 02:55:253250
3251 // Give time for the blacklist to update.
Gabriel Charette01507a22017-09-27 21:30:083252 content::RunAllTasksUntilIdle();
[email protected]3f2a2fa2013-09-24 02:55:253253
[email protected]f484f8d52014-06-12 08:38:183254 EXPECT_EQ(1u, registry()->blacklisted_extensions().size());
3255 EXPECT_EQ(2u, registry()->enabled_extensions().size());
[email protected]3f2a2fa2013-09-24 02:55:253256
[email protected]f484f8d52014-06-12 08:38:183257 EXPECT_TRUE(registry()->enabled_extensions().Contains(good0));
3258 EXPECT_TRUE(registry()->blacklisted_extensions().Contains(good1));
3259 EXPECT_TRUE(registry()->enabled_extensions().Contains(good2));
[email protected]3f2a2fa2013-09-24 02:55:253260}
3261#endif // defined(ENABLE_BLACKLIST_TESTS)
[email protected]6b75ec32009-08-14 06:37:183262
[email protected]2d19eb6e2014-01-27 17:30:003263#if defined(ENABLE_BLACKLIST_TESTS)
3264// Extension is added to blacklist with BLACKLISTED_POTENTIALLY_UNWANTED state
3265// after it is installed. It is then successfully re-enabled by the user.
3266TEST_F(ExtensionServiceTest, GreylistedExtensionDisabled) {
3267 extensions::TestBlacklist test_blacklist;
3268 // A profile with 3 extensions installed: good0, good1, and good2.
3269 InitializeGoodInstalledExtensionService();
[email protected]f484f8d52014-06-12 08:38:183270 test_blacklist.Attach(service()->blacklist_);
3271 service()->Init();
[email protected]2d19eb6e2014-01-27 17:30:003272
[email protected]2d19eb6e2014-01-27 17:30:003273 const extensions::ExtensionSet& enabled_extensions =
[email protected]f484f8d52014-06-12 08:38:183274 registry()->enabled_extensions();
[email protected]2d19eb6e2014-01-27 17:30:003275 const extensions::ExtensionSet& disabled_extensions =
[email protected]f484f8d52014-06-12 08:38:183276 registry()->disabled_extensions();
[email protected]2d19eb6e2014-01-27 17:30:003277
3278 EXPECT_TRUE(enabled_extensions.Contains(good0));
3279 EXPECT_TRUE(enabled_extensions.Contains(good1));
3280 EXPECT_TRUE(enabled_extensions.Contains(good2));
3281
3282 // Blacklist good0 and good1 (and an invalid extension ID).
3283 test_blacklist.SetBlacklistState(
3284 good0, extensions::BLACKLISTED_CWS_POLICY_VIOLATION, true);
3285 test_blacklist.SetBlacklistState(
3286 good1, extensions::BLACKLISTED_POTENTIALLY_UNWANTED, true);
3287 test_blacklist.SetBlacklistState(
3288 "invalid_id", extensions::BLACKLISTED_MALWARE, true);
Gabriel Charette01507a22017-09-27 21:30:083289 content::RunAllTasksUntilIdle();
[email protected]2d19eb6e2014-01-27 17:30:003290
3291 EXPECT_FALSE(enabled_extensions.Contains(good0));
3292 EXPECT_TRUE(disabled_extensions.Contains(good0));
3293 EXPECT_FALSE(enabled_extensions.Contains(good1));
3294 EXPECT_TRUE(disabled_extensions.Contains(good1));
3295 EXPECT_TRUE(enabled_extensions.Contains(good2));
3296 EXPECT_FALSE(disabled_extensions.Contains(good2));
3297
3298 ValidateIntegerPref(
3299 good0, "blacklist_state", extensions::BLACKLISTED_CWS_POLICY_VIOLATION);
3300 ValidateIntegerPref(
3301 good1, "blacklist_state", extensions::BLACKLISTED_POTENTIALLY_UNWANTED);
3302
3303 // Now user enables good0.
[email protected]f484f8d52014-06-12 08:38:183304 service()->EnableExtension(good0);
[email protected]2d19eb6e2014-01-27 17:30:003305
3306 EXPECT_TRUE(enabled_extensions.Contains(good0));
3307 EXPECT_FALSE(disabled_extensions.Contains(good0));
3308 EXPECT_FALSE(enabled_extensions.Contains(good1));
3309 EXPECT_TRUE(disabled_extensions.Contains(good1));
3310
3311 // Remove extensions from blacklist.
3312 test_blacklist.SetBlacklistState(
3313 good0, extensions::NOT_BLACKLISTED, true);
3314 test_blacklist.SetBlacklistState(
3315 good1, extensions::NOT_BLACKLISTED, true);
Gabriel Charette01507a22017-09-27 21:30:083316 content::RunAllTasksUntilIdle();
[email protected]2d19eb6e2014-01-27 17:30:003317
3318 // All extensions are enabled.
3319 EXPECT_TRUE(enabled_extensions.Contains(good0));
3320 EXPECT_FALSE(disabled_extensions.Contains(good0));
3321 EXPECT_TRUE(enabled_extensions.Contains(good1));
3322 EXPECT_FALSE(disabled_extensions.Contains(good1));
3323 EXPECT_TRUE(enabled_extensions.Contains(good2));
3324 EXPECT_FALSE(disabled_extensions.Contains(good2));
3325}
3326#endif // defined(ENABLE_BLACKLIST_TESTS)
3327
3328#if defined(ENABLE_BLACKLIST_TESTS)
3329// When extension is removed from greylist, do not re-enable it if it is
3330// disabled by user.
3331TEST_F(ExtensionServiceTest, GreylistDontEnableManuallyDisabled) {
3332 extensions::TestBlacklist test_blacklist;
3333 // A profile with 3 extensions installed: good0, good1, and good2.
3334 InitializeGoodInstalledExtensionService();
[email protected]f484f8d52014-06-12 08:38:183335 test_blacklist.Attach(service()->blacklist_);
3336 service()->Init();
[email protected]2d19eb6e2014-01-27 17:30:003337
[email protected]2d19eb6e2014-01-27 17:30:003338 const extensions::ExtensionSet& enabled_extensions =
[email protected]f484f8d52014-06-12 08:38:183339 registry()->enabled_extensions();
[email protected]2d19eb6e2014-01-27 17:30:003340 const extensions::ExtensionSet& disabled_extensions =
[email protected]f484f8d52014-06-12 08:38:183341 registry()->disabled_extensions();
[email protected]2d19eb6e2014-01-27 17:30:003342
3343 // Manually disable.
[email protected]f484f8d52014-06-12 08:38:183344 service()->DisableExtension(good0,
Minh X. Nguyen45479012017-08-18 21:35:363345 extensions::disable_reason::DISABLE_USER_ACTION);
[email protected]2d19eb6e2014-01-27 17:30:003346
3347 test_blacklist.SetBlacklistState(
3348 good0, extensions::BLACKLISTED_CWS_POLICY_VIOLATION, true);
3349 test_blacklist.SetBlacklistState(
3350 good1, extensions::BLACKLISTED_POTENTIALLY_UNWANTED, true);
3351 test_blacklist.SetBlacklistState(
3352 good2, extensions::BLACKLISTED_SECURITY_VULNERABILITY, true);
Gabriel Charette01507a22017-09-27 21:30:083353 content::RunAllTasksUntilIdle();
[email protected]2d19eb6e2014-01-27 17:30:003354
3355 // All extensions disabled.
3356 EXPECT_FALSE(enabled_extensions.Contains(good0));
3357 EXPECT_TRUE(disabled_extensions.Contains(good0));
3358 EXPECT_FALSE(enabled_extensions.Contains(good1));
3359 EXPECT_TRUE(disabled_extensions.Contains(good1));
3360 EXPECT_FALSE(enabled_extensions.Contains(good2));
3361 EXPECT_TRUE(disabled_extensions.Contains(good2));
3362
3363 // Greylisted extension can be enabled.
[email protected]f484f8d52014-06-12 08:38:183364 service()->EnableExtension(good1);
[email protected]2d19eb6e2014-01-27 17:30:003365 EXPECT_TRUE(enabled_extensions.Contains(good1));
3366 EXPECT_FALSE(disabled_extensions.Contains(good1));
3367
3368 // good1 is now manually disabled.
[email protected]f484f8d52014-06-12 08:38:183369 service()->DisableExtension(good1,
Minh X. Nguyen45479012017-08-18 21:35:363370 extensions::disable_reason::DISABLE_USER_ACTION);
[email protected]2d19eb6e2014-01-27 17:30:003371 EXPECT_FALSE(enabled_extensions.Contains(good1));
3372 EXPECT_TRUE(disabled_extensions.Contains(good1));
3373
3374 // Remove extensions from blacklist.
3375 test_blacklist.SetBlacklistState(
3376 good0, extensions::NOT_BLACKLISTED, true);
3377 test_blacklist.SetBlacklistState(
3378 good1, extensions::NOT_BLACKLISTED, true);
3379 test_blacklist.SetBlacklistState(
3380 good2, extensions::NOT_BLACKLISTED, true);
Gabriel Charette01507a22017-09-27 21:30:083381 content::RunAllTasksUntilIdle();
[email protected]2d19eb6e2014-01-27 17:30:003382
3383 // good0 and good1 remain disabled.
3384 EXPECT_FALSE(enabled_extensions.Contains(good0));
3385 EXPECT_TRUE(disabled_extensions.Contains(good0));
3386 EXPECT_FALSE(enabled_extensions.Contains(good1));
3387 EXPECT_TRUE(disabled_extensions.Contains(good1));
3388 EXPECT_TRUE(enabled_extensions.Contains(good2));
3389 EXPECT_FALSE(disabled_extensions.Contains(good2));
3390}
3391#endif // defined(ENABLE_BLACKLIST_TESTS)
3392
3393#if defined(ENABLE_BLACKLIST_TESTS)
3394// Blacklisted extension with unknown state are not enabled/disabled.
3395TEST_F(ExtensionServiceTest, GreylistUnknownDontChange) {
3396 extensions::TestBlacklist test_blacklist;
3397 // A profile with 3 extensions installed: good0, good1, and good2.
3398 InitializeGoodInstalledExtensionService();
[email protected]f484f8d52014-06-12 08:38:183399 test_blacklist.Attach(service()->blacklist_);
3400 service()->Init();
[email protected]2d19eb6e2014-01-27 17:30:003401
[email protected]2d19eb6e2014-01-27 17:30:003402 const extensions::ExtensionSet& enabled_extensions =
[email protected]f484f8d52014-06-12 08:38:183403 registry()->enabled_extensions();
[email protected]2d19eb6e2014-01-27 17:30:003404 const extensions::ExtensionSet& disabled_extensions =
[email protected]f484f8d52014-06-12 08:38:183405 registry()->disabled_extensions();
[email protected]2d19eb6e2014-01-27 17:30:003406
3407 test_blacklist.SetBlacklistState(
3408 good0, extensions::BLACKLISTED_CWS_POLICY_VIOLATION, true);
3409 test_blacklist.SetBlacklistState(
3410 good1, extensions::BLACKLISTED_POTENTIALLY_UNWANTED, true);
Gabriel Charette01507a22017-09-27 21:30:083411 content::RunAllTasksUntilIdle();
[email protected]2d19eb6e2014-01-27 17:30:003412
3413 EXPECT_FALSE(enabled_extensions.Contains(good0));
3414 EXPECT_TRUE(disabled_extensions.Contains(good0));
3415 EXPECT_FALSE(enabled_extensions.Contains(good1));
3416 EXPECT_TRUE(disabled_extensions.Contains(good1));
3417 EXPECT_TRUE(enabled_extensions.Contains(good2));
3418 EXPECT_FALSE(disabled_extensions.Contains(good2));
3419
3420 test_blacklist.SetBlacklistState(
3421 good0, extensions::NOT_BLACKLISTED, true);
3422 test_blacklist.SetBlacklistState(
3423 good1, extensions::BLACKLISTED_UNKNOWN, true);
3424 test_blacklist.SetBlacklistState(
3425 good2, extensions::BLACKLISTED_UNKNOWN, true);
Gabriel Charette01507a22017-09-27 21:30:083426 content::RunAllTasksUntilIdle();
[email protected]2d19eb6e2014-01-27 17:30:003427
3428 // good0 re-enabled, other remain as they were.
3429 EXPECT_TRUE(enabled_extensions.Contains(good0));
3430 EXPECT_FALSE(disabled_extensions.Contains(good0));
3431 EXPECT_FALSE(enabled_extensions.Contains(good1));
3432 EXPECT_TRUE(disabled_extensions.Contains(good1));
3433 EXPECT_TRUE(enabled_extensions.Contains(good2));
3434 EXPECT_FALSE(disabled_extensions.Contains(good2));
3435}
[email protected]757d60a2014-05-23 00:11:443436
3437// Tests that blacklisted extensions cannot be reloaded, both those loaded
3438// before and after extension service startup.
3439TEST_F(ExtensionServiceTest, ReloadBlacklistedExtension) {
3440 extensions::TestBlacklist test_blacklist;
3441
3442 InitializeGoodInstalledExtensionService();
[email protected]f484f8d52014-06-12 08:38:183443 test_blacklist.Attach(service()->blacklist_);
[email protected]757d60a2014-05-23 00:11:443444
3445 test_blacklist.SetBlacklistState(
3446 good1, extensions::BLACKLISTED_MALWARE, false);
[email protected]f484f8d52014-06-12 08:38:183447 service()->Init();
[email protected]757d60a2014-05-23 00:11:443448 test_blacklist.SetBlacklistState(
3449 good2, extensions::BLACKLISTED_MALWARE, false);
Gabriel Charette01507a22017-09-27 21:30:083450 content::RunAllTasksUntilIdle();
[email protected]757d60a2014-05-23 00:11:443451
[email protected]f484f8d52014-06-12 08:38:183452 EXPECT_EQ(StringSet(good0), registry()->enabled_extensions().GetIDs());
[email protected]757d60a2014-05-23 00:11:443453 EXPECT_EQ(StringSet(good1, good2),
[email protected]f484f8d52014-06-12 08:38:183454 registry()->blacklisted_extensions().GetIDs());
[email protected]757d60a2014-05-23 00:11:443455
[email protected]f484f8d52014-06-12 08:38:183456 service()->ReloadExtension(good1);
3457 service()->ReloadExtension(good2);
Gabriel Charette01507a22017-09-27 21:30:083458 content::RunAllTasksUntilIdle();
[email protected]757d60a2014-05-23 00:11:443459
[email protected]f484f8d52014-06-12 08:38:183460 EXPECT_EQ(StringSet(good0), registry()->enabled_extensions().GetIDs());
[email protected]757d60a2014-05-23 00:11:443461 EXPECT_EQ(StringSet(good1, good2),
[email protected]f484f8d52014-06-12 08:38:183462 registry()->blacklisted_extensions().GetIDs());
[email protected]757d60a2014-05-23 00:11:443463}
[email protected]2d19eb6e2014-01-27 17:30:003464#endif // defined(ENABLE_BLACKLIST_TESTS)
3465
mlerman6a37b6a42014-11-26 22:10:533466// Tests blocking then unblocking enabled extensions after the service has been
3467// initialized.
3468TEST_F(ExtensionServiceTest, BlockAndUnblockEnabledExtension) {
3469 InitializeGoodInstalledExtensionService();
3470 service()->Init();
3471
3472 AssertExtensionBlocksAndUnblocks(true, good0);
3473}
3474
3475// Tests blocking then unblocking disabled extensions after the service has been
3476// initialized.
3477TEST_F(ExtensionServiceTest, BlockAndUnblockDisabledExtension) {
3478 InitializeGoodInstalledExtensionService();
3479 service()->Init();
3480
Minh X. Nguyen45479012017-08-18 21:35:363481 service()->DisableExtension(good0,
3482 extensions::disable_reason::DISABLE_RELOAD);
mlerman6a37b6a42014-11-26 22:10:533483
3484 AssertExtensionBlocksAndUnblocks(true, good0);
3485}
3486
3487// Tests blocking then unblocking terminated extensions after the service has
3488// been initialized.
3489TEST_F(ExtensionServiceTest, BlockAndUnblockTerminatedExtension) {
3490 InitializeGoodInstalledExtensionService();
3491 service()->Init();
3492
3493 TerminateExtension(good0);
3494
3495 AssertExtensionBlocksAndUnblocks(true, good0);
3496}
3497
3498// Tests blocking then unblocking policy-forced extensions after the service has
3499// been initialized.
3500TEST_F(ExtensionServiceTest, BlockAndUnblockPolicyExtension) {
3501 InitializeEmptyExtensionServiceWithTestingPrefs();
3502
3503 {
3504 ManagementPrefUpdater pref(profile_->GetTestingPrefService());
3505 // // Blacklist everything.
3506 // pref.SetBlacklistedByDefault(true);
3507 // Mark good.crx for force-installation.
3508 pref.SetIndividualExtensionAutoInstalled(
3509 good_crx, "http://example.com/update_url", true);
3510 }
3511
3512 // Have policy force-install an extension.
lazyboya00eafc2017-04-08 00:57:193513 MockExternalProvider* provider =
lazyboy8a08c9d2017-04-11 19:53:223514 AddMockExternalProvider(Manifest::EXTERNAL_POLICY_DOWNLOAD);
mlerman6a37b6a42014-11-26 22:10:533515 provider->UpdateOrAddExtension(
3516 good_crx, "1.0.0.0", data_dir().AppendASCII("good_crx"));
3517
3518 // Reloading extensions should find our externally registered extension
3519 // and install it.
lazyboy8a08c9d2017-04-11 19:53:223520 WaitForExternalExtensionInstalled();
mlerman6a37b6a42014-11-26 22:10:533521
3522 AssertExtensionBlocksAndUnblocks(false, good_crx);
3523}
3524
3525
3526#if defined(ENABLE_BLACKLIST_TESTS)
3527// Tests blocking then unblocking extensions that are blacklisted both before
3528// and after Init().
3529TEST_F(ExtensionServiceTest, BlockAndUnblockBlacklistedExtension) {
3530 extensions::TestBlacklist test_blacklist;
3531
3532 InitializeGoodInstalledExtensionService();
3533 test_blacklist.Attach(service()->blacklist_);
3534
3535 test_blacklist.SetBlacklistState(
3536 good0, extensions::BLACKLISTED_MALWARE, true);
Gabriel Charette01507a22017-09-27 21:30:083537 content::RunAllTasksUntilIdle();
mlerman6a37b6a42014-11-26 22:10:533538
3539 service()->Init();
3540
3541 test_blacklist.SetBlacklistState(
3542 good1, extensions::BLACKLISTED_MALWARE, true);
Gabriel Charette01507a22017-09-27 21:30:083543 content::RunAllTasksUntilIdle();
mlerman6a37b6a42014-11-26 22:10:533544
3545 // Blacklisted extensions stay blacklisted.
3546 AssertExtensionBlocksAndUnblocks(false, good0);
3547 AssertExtensionBlocksAndUnblocks(false, good1);
3548
3549 service()->BlockAllExtensions();
3550
3551 // Remove an extension from the blacklist while the service is blocked.
3552 test_blacklist.SetBlacklistState(
3553 good0, extensions::NOT_BLACKLISTED, true);
3554 // Add an extension to the blacklist while the service is blocked.
3555 test_blacklist.SetBlacklistState(
3556 good2, extensions::BLACKLISTED_MALWARE, true);
Gabriel Charette01507a22017-09-27 21:30:083557 content::RunAllTasksUntilIdle();
mlerman6a37b6a42014-11-26 22:10:533558
3559 // Go directly to blocked, do not pass go, do not collect $200.
3560 ASSERT_TRUE(IsBlocked(good0));
3561 // Get on the blacklist - even if you were blocked!
3562 ASSERT_FALSE(IsBlocked(good2));
3563}
3564#endif // defined(ENABLE_BLACKLIST_TESTS)
3565
3566// Tests blocking then unblocking enabled component extensions after the service
3567// has been initialized.
3568TEST_F(ExtensionServiceTest, BlockAndUnblockEnabledComponentExtension) {
3569 InitializeEmptyExtensionServiceWithTestingPrefs();
3570
3571 // Install a component extension.
3572 base::FilePath path = data_dir()
3573 .AppendASCII("good")
3574 .AppendASCII("Extensions")
3575 .AppendASCII(good0)
3576 .AppendASCII("1.0.0.0");
3577 std::string manifest;
3578 ASSERT_TRUE(base::ReadFileToString(
3579 path.Append(extensions::kManifestFilename), &manifest));
3580 service()->component_loader()->Add(manifest, path);
3581 service()->Init();
3582
3583 // Component extension should never block.
3584 AssertExtensionBlocksAndUnblocks(false, good0);
3585}
3586
3587// Tests blocking then unblocking a theme after the service has been
3588// initialized.
3589TEST_F(ExtensionServiceTest, BlockAndUnblockTheme) {
3590 InitializeEmptyExtensionService();
3591 service()->Init();
3592
3593 base::FilePath path = data_dir().AppendASCII("theme.crx");
3594 InstallCRX(path, INSTALL_NEW);
3595
3596 AssertExtensionBlocksAndUnblocks(true, theme_crx);
3597}
3598
3599// Tests that blocking extensions before Init() results in loading blocked
3600// extensions.
3601TEST_F(ExtensionServiceTest, WillNotLoadExtensionsWhenBlocked) {
3602 InitializeGoodInstalledExtensionService();
3603
3604 service()->BlockAllExtensions();
3605
3606 service()->Init();
3607
3608 ASSERT_TRUE(IsBlocked(good0));
3609 ASSERT_TRUE(IsBlocked(good0));
3610 ASSERT_TRUE(IsBlocked(good0));
3611}
3612
mlerman3690e5be2014-12-01 22:57:443613// Tests that IsEnabledExtension won't crash on an uninstalled extension.
3614TEST_F(ExtensionServiceTest, IsEnabledExtensionBlockedAndNotInstalled) {
3615 InitializeEmptyExtensionService();
3616
3617 service()->BlockAllExtensions();
3618
3619 service()->IsExtensionEnabled(theme_crx);
3620}
3621
[email protected]306a2bd2010-08-11 14:56:363622// Will not install extension blacklisted by policy.
[email protected]d9a61e12012-11-14 02:43:473623TEST_F(ExtensionServiceTest, BlacklistedByPolicyWillNotInstall) {
binjin1569c9b2014-09-05 13:33:183624 InitializeEmptyExtensionServiceWithTestingPrefs();
[email protected]306a2bd2010-08-11 14:56:363625
[email protected]306a2bd2010-08-11 14:56:363626 // Blacklist everything.
[email protected]43d3bf82011-04-11 07:46:583627 {
binjinb2454382014-09-22 15:17:433628 ManagementPrefUpdater pref(profile_->GetTestingPrefService());
3629 pref.SetBlacklistedByDefault(true);
[email protected]43d3bf82011-04-11 07:46:583630 }
[email protected]306a2bd2010-08-11 14:56:363631
3632 // Blacklist prevents us from installing good_crx.
[email protected]f484f8d52014-06-12 08:38:183633 base::FilePath path = data_dir().AppendASCII("good.crx");
[email protected]8f512c72011-11-22 21:02:503634 InstallCRX(path, INSTALL_FAILED);
[email protected]f484f8d52014-06-12 08:38:183635 EXPECT_EQ(0u, registry()->enabled_extensions().size());
[email protected]306a2bd2010-08-11 14:56:363636
3637 // Now whitelist this particular extension.
[email protected]43d3bf82011-04-11 07:46:583638 {
binjinb2454382014-09-22 15:17:433639 ManagementPrefUpdater pref(profile_->GetTestingPrefService());
3640 pref.SetIndividualExtensionInstallationAllowed(good_crx, true);
[email protected]43d3bf82011-04-11 07:46:583641 }
3642
[email protected]306a2bd2010-08-11 14:56:363643 // Ensure we can now install good_crx.
[email protected]8f512c72011-11-22 21:02:503644 InstallCRX(path, INSTALL_NEW);
[email protected]f484f8d52014-06-12 08:38:183645 EXPECT_EQ(1u, registry()->enabled_extensions().size());
[email protected]306a2bd2010-08-11 14:56:363646}
3647
[email protected]aa96d3a2010-08-21 08:45:253648// Extension blacklisted by policy get unloaded after installing.
[email protected]d9a61e12012-11-14 02:43:473649TEST_F(ExtensionServiceTest, BlacklistedByPolicyRemovedIfRunning) {
binjinb2454382014-09-22 15:17:433650 InitializeEmptyExtensionServiceWithTestingPrefs();
[email protected]aa96d3a2010-08-21 08:45:253651
3652 // Install good_crx.
[email protected]f484f8d52014-06-12 08:38:183653 base::FilePath path = data_dir().AppendASCII("good.crx");
[email protected]8f512c72011-11-22 21:02:503654 InstallCRX(path, INSTALL_NEW);
[email protected]f484f8d52014-06-12 08:38:183655 EXPECT_EQ(1u, registry()->enabled_extensions().size());
[email protected]aa96d3a2010-08-21 08:45:253656
binjinb2454382014-09-22 15:17:433657 {
3658 ManagementPrefUpdater pref(profile_->GetTestingPrefService());
[email protected]acd78969c2010-12-08 09:49:113659 // Blacklist this extension.
binjinb2454382014-09-22 15:17:433660 pref.SetIndividualExtensionInstallationAllowed(good_crx, false);
[email protected]acd78969c2010-12-08 09:49:113661 }
[email protected]aa96d3a2010-08-21 08:45:253662
3663 // Extension should not be running now.
Gabriel Charette01507a22017-09-27 21:30:083664 content::RunAllTasksUntilIdle();
[email protected]f484f8d52014-06-12 08:38:183665 EXPECT_EQ(0u, registry()->enabled_extensions().size());
[email protected]aa96d3a2010-08-21 08:45:253666}
3667
[email protected]05aad2da2011-10-28 10:12:373668// Tests that component extensions are not blacklisted by policy.
[email protected]d9a61e12012-11-14 02:43:473669TEST_F(ExtensionServiceTest, ComponentExtensionWhitelisted) {
binjinb2454382014-09-22 15:17:433670 InitializeEmptyExtensionServiceWithTestingPrefs();
[email protected]05aad2da2011-10-28 10:12:373671
3672 // Blacklist everything.
3673 {
binjinb2454382014-09-22 15:17:433674 ManagementPrefUpdater pref(profile_->GetTestingPrefService());
3675 pref.SetBlacklistedByDefault(true);
[email protected]05aad2da2011-10-28 10:12:373676 }
3677
3678 // Install a component extension.
[email protected]f484f8d52014-06-12 08:38:183679 base::FilePath path = data_dir()
3680 .AppendASCII("good")
3681 .AppendASCII("Extensions")
3682 .AppendASCII(good0)
3683 .AppendASCII("1.0.0.0");
[email protected]05aad2da2011-10-28 10:12:373684 std::string manifest;
[email protected]82f84b92013-08-30 18:23:503685 ASSERT_TRUE(base::ReadFileToString(
[email protected]993da5e2013-03-23 21:25:163686 path.Append(extensions::kManifestFilename), &manifest));
[email protected]f484f8d52014-06-12 08:38:183687 service()->component_loader()->Add(manifest, path);
3688 service()->Init();
[email protected]05aad2da2011-10-28 10:12:373689
3690 // Extension should be installed despite blacklist.
[email protected]f484f8d52014-06-12 08:38:183691 ASSERT_EQ(1u, registry()->enabled_extensions().size());
3692 EXPECT_TRUE(service()->GetExtensionById(good0, false));
[email protected]05aad2da2011-10-28 10:12:373693
3694 // Poke external providers and make sure the extension is still present.
[email protected]f484f8d52014-06-12 08:38:183695 service()->CheckForExternalUpdates();
3696 ASSERT_EQ(1u, registry()->enabled_extensions().size());
3697 EXPECT_TRUE(service()->GetExtensionById(good0, false));
[email protected]05aad2da2011-10-28 10:12:373698
3699 // Extension should not be uninstalled on blacklist changes.
3700 {
binjinb2454382014-09-22 15:17:433701 ManagementPrefUpdater pref(profile_->GetTestingPrefService());
3702 pref.SetIndividualExtensionInstallationAllowed(good0, false);
[email protected]05aad2da2011-10-28 10:12:373703 }
Gabriel Charette01507a22017-09-27 21:30:083704 content::RunAllTasksUntilIdle();
[email protected]f484f8d52014-06-12 08:38:183705 ASSERT_EQ(1u, registry()->enabled_extensions().size());
3706 EXPECT_TRUE(service()->GetExtensionById(good0, false));
[email protected]05aad2da2011-10-28 10:12:373707}
3708
Nick Peterson0203be4a2017-07-19 00:25:143709// Tests that active permissions are not revoked from component extensions
3710// by policy when the policy is updated. https://crbug.com/746017.
3711TEST_F(ExtensionServiceTest, ComponentExtensionWhitelistedPermission) {
3712 InitializeEmptyExtensionServiceWithTestingPrefs();
3713
3714 // Install a component extension.
3715 base::FilePath path = data_dir()
3716 .AppendASCII("good")
3717 .AppendASCII("Extensions")
3718 .AppendASCII(good0)
3719 .AppendASCII("1.0.0.0");
3720 std::string manifest;
3721 ASSERT_TRUE(base::ReadFileToString(path.Append(extensions::kManifestFilename),
3722 &manifest));
3723 service()->component_loader()->Add(manifest, path);
3724 service()->Init();
3725
3726 // Extension should have the "tabs" permission.
3727 EXPECT_TRUE(service()
3728 ->GetExtensionById(good0, false)
3729 ->permissions_data()
3730 ->active_permissions()
3731 .HasAPIPermission(extensions::APIPermission::kTab));
3732
3733 // Component should not lose permissions on policy change.
3734 {
3735 ManagementPrefUpdater pref(profile_->GetTestingPrefService());
3736 pref.AddBlockedPermission(good0, "tabs");
3737 }
3738
3739 service()->OnExtensionManagementSettingsChanged();
Gabriel Charette01507a22017-09-27 21:30:083740 content::RunAllTasksUntilIdle();
Nick Peterson0203be4a2017-07-19 00:25:143741 EXPECT_TRUE(service()
3742 ->GetExtensionById(good0, false)
3743 ->permissions_data()
3744 ->active_permissions()
3745 .HasAPIPermission(extensions::APIPermission::kTab));
3746}
3747
[email protected]05aad2da2011-10-28 10:12:373748// Tests that policy-installed extensions are not blacklisted by policy.
[email protected]d9a61e12012-11-14 02:43:473749TEST_F(ExtensionServiceTest, PolicyInstalledExtensionsWhitelisted) {
binjin1569c9b2014-09-05 13:33:183750 InitializeEmptyExtensionServiceWithTestingPrefs();
[email protected]05aad2da2011-10-28 10:12:373751
[email protected]05aad2da2011-10-28 10:12:373752 {
binjinb2454382014-09-22 15:17:433753 ManagementPrefUpdater pref(profile_->GetTestingPrefService());
[email protected]e410b5f2012-12-14 14:02:243754 // Blacklist everything.
binjinb2454382014-09-22 15:17:433755 pref.SetBlacklistedByDefault(true);
[email protected]e410b5f2012-12-14 14:02:243756 // Mark good.crx for force-installation.
binjinb2454382014-09-22 15:17:433757 pref.SetIndividualExtensionAutoInstalled(
3758 good_crx, "http://example.com/update_url", true);
[email protected]05aad2da2011-10-28 10:12:373759 }
3760
3761 // Have policy force-install an extension.
lazyboya00eafc2017-04-08 00:57:193762 MockExternalProvider* provider =
lazyboy8a08c9d2017-04-11 19:53:223763 AddMockExternalProvider(Manifest::EXTERNAL_POLICY_DOWNLOAD);
[email protected]f484f8d52014-06-12 08:38:183764 provider->UpdateOrAddExtension(
3765 good_crx, "1.0.0.0", data_dir().AppendASCII("good.crx"));
[email protected]05aad2da2011-10-28 10:12:373766
3767 // Reloading extensions should find our externally registered extension
3768 // and install it.
lazyboy8a08c9d2017-04-11 19:53:223769 WaitForExternalExtensionInstalled();
[email protected]05aad2da2011-10-28 10:12:373770
3771 // Extension should be installed despite blacklist.
[email protected]f484f8d52014-06-12 08:38:183772 ASSERT_EQ(1u, registry()->enabled_extensions().size());
3773 EXPECT_TRUE(service()->GetExtensionById(good_crx, false));
[email protected]05aad2da2011-10-28 10:12:373774
3775 // Blacklist update should not uninstall the extension.
3776 {
binjinb2454382014-09-22 15:17:433777 ManagementPrefUpdater pref(profile_->GetTestingPrefService());
3778 pref.SetIndividualExtensionInstallationAllowed(good0, false);
[email protected]05aad2da2011-10-28 10:12:373779 }
Gabriel Charette01507a22017-09-27 21:30:083780 content::RunAllTasksUntilIdle();
[email protected]f484f8d52014-06-12 08:38:183781 ASSERT_EQ(1u, registry()->enabled_extensions().size());
3782 EXPECT_TRUE(service()->GetExtensionById(good_crx, false));
[email protected]05aad2da2011-10-28 10:12:373783}
3784
[email protected]65187152012-06-02 13:14:143785// Tests that extensions cannot be installed if the policy provider prohibits
3786// it. This functionality is implemented in CrxInstaller::ConfirmInstall().
[email protected]d9a61e12012-11-14 02:43:473787TEST_F(ExtensionServiceTest, ManagementPolicyProhibitsInstall) {
[email protected]65187152012-06-02 13:14:143788 InitializeEmptyExtensionService();
3789
[email protected]f484f8d52014-06-12 08:38:183790 GetManagementPolicy()->UnregisterAllProviders();
[email protected]65187152012-06-02 13:14:143791 extensions::TestManagementPolicyProvider provider_(
3792 extensions::TestManagementPolicyProvider::PROHIBIT_LOAD);
[email protected]f484f8d52014-06-12 08:38:183793 GetManagementPolicy()->RegisterProvider(&provider_);
[email protected]65187152012-06-02 13:14:143794
[email protected]f484f8d52014-06-12 08:38:183795 InstallCRX(data_dir().AppendASCII("good.crx"), INSTALL_FAILED);
3796 EXPECT_EQ(0u, registry()->enabled_extensions().size());
[email protected]65187152012-06-02 13:14:143797}
3798
3799// Tests that extensions cannot be loaded from prefs if the policy provider
3800// prohibits it. This functionality is implemented in InstalledLoader::Load().
[email protected]d9a61e12012-11-14 02:43:473801TEST_F(ExtensionServiceTest, ManagementPolicyProhibitsLoadFromPrefs) {
[email protected]65187152012-06-02 13:14:143802 InitializeEmptyExtensionService();
3803
3804 // Create a fake extension to be loaded as though it were read from prefs.
[email protected]f484f8d52014-06-12 08:38:183805 base::FilePath path =
3806 data_dir().AppendASCII("management").AppendASCII("simple_extension");
[email protected]023b3d12013-12-23 18:46:493807 base::DictionaryValue manifest;
[email protected]65187152012-06-02 13:14:143808 manifest.SetString(keys::kName, "simple_extension");
3809 manifest.SetString(keys::kVersion, "1");
[email protected]12075d12013-02-27 05:38:053810 // UNPACKED is for extensions loaded from a directory. We use it here, even
[email protected]65187152012-06-02 13:14:143811 // though we're testing loading from prefs, so that we don't need to provide
3812 // an extension key.
[email protected]007b3f82013-04-09 08:46:453813 extensions::ExtensionInfo extension_info(
3814 &manifest, std::string(), path, Manifest::UNPACKED);
[email protected]65187152012-06-02 13:14:143815
3816 // Ensure we can load it with no management policy in place.
[email protected]f484f8d52014-06-12 08:38:183817 GetManagementPolicy()->UnregisterAllProviders();
3818 EXPECT_EQ(0u, registry()->enabled_extensions().size());
3819 extensions::InstalledLoader(service()).Load(extension_info, false);
3820 EXPECT_EQ(1u, registry()->enabled_extensions().size());
[email protected]65187152012-06-02 13:14:143821
[email protected]f484f8d52014-06-12 08:38:183822 const Extension* extension =
3823 (registry()->enabled_extensions().begin())->get();
[email protected]42d58f62014-07-31 01:32:453824 EXPECT_TRUE(
3825 service()->UninstallExtension(extension->id(),
3826 extensions::UNINSTALL_REASON_FOR_TESTING,
[email protected]42d58f62014-07-31 01:32:453827 NULL));
[email protected]f484f8d52014-06-12 08:38:183828 EXPECT_EQ(0u, registry()->enabled_extensions().size());
[email protected]65187152012-06-02 13:14:143829
3830 // Ensure we cannot load it if management policy prohibits installation.
3831 extensions::TestManagementPolicyProvider provider_(
3832 extensions::TestManagementPolicyProvider::PROHIBIT_LOAD);
[email protected]f484f8d52014-06-12 08:38:183833 GetManagementPolicy()->RegisterProvider(&provider_);
[email protected]65187152012-06-02 13:14:143834
[email protected]f484f8d52014-06-12 08:38:183835 extensions::InstalledLoader(service()).Load(extension_info, false);
3836 EXPECT_EQ(0u, registry()->enabled_extensions().size());
[email protected]65187152012-06-02 13:14:143837}
3838
3839// Tests disabling an extension when prohibited by the ManagementPolicy.
[email protected]d9a61e12012-11-14 02:43:473840TEST_F(ExtensionServiceTest, ManagementPolicyProhibitsDisable) {
[email protected]65187152012-06-02 13:14:143841 InitializeEmptyExtensionService();
3842
[email protected]f484f8d52014-06-12 08:38:183843 InstallCRX(data_dir().AppendASCII("good.crx"), INSTALL_NEW);
3844 EXPECT_EQ(1u, registry()->enabled_extensions().size());
3845 EXPECT_EQ(0u, registry()->disabled_extensions().size());
[email protected]65187152012-06-02 13:14:143846
[email protected]f484f8d52014-06-12 08:38:183847 GetManagementPolicy()->UnregisterAllProviders();
[email protected]65187152012-06-02 13:14:143848 extensions::TestManagementPolicyProvider provider(
3849 extensions::TestManagementPolicyProvider::PROHIBIT_MODIFY_STATUS);
[email protected]f484f8d52014-06-12 08:38:183850 GetManagementPolicy()->RegisterProvider(&provider);
[email protected]65187152012-06-02 13:14:143851
3852 // Attempt to disable it.
Minh X. Nguyen45479012017-08-18 21:35:363853 service()->DisableExtension(good_crx,
3854 extensions::disable_reason::DISABLE_USER_ACTION);
[email protected]65187152012-06-02 13:14:143855
[email protected]f484f8d52014-06-12 08:38:183856 EXPECT_EQ(1u, registry()->enabled_extensions().size());
3857 EXPECT_TRUE(service()->GetExtensionById(good_crx, false));
3858 EXPECT_EQ(0u, registry()->disabled_extensions().size());
Michael Giuffridab16cab32017-10-04 21:56:093859 EXPECT_EQ(extensions::disable_reason::DISABLE_NONE,
3860 ExtensionPrefs::Get(profile())->GetDisableReasons(good_crx));
3861
3862 // Internal disable reasons are allowed.
3863 service()->DisableExtension(
3864 good_crx, extensions::disable_reason::DISABLE_CORRUPTED |
3865 extensions::disable_reason::DISABLE_USER_ACTION);
3866
3867 EXPECT_EQ(0u, registry()->enabled_extensions().size());
3868 EXPECT_EQ(1u, registry()->disabled_extensions().size());
3869 EXPECT_TRUE(service()->GetExtensionById(good_crx, true));
3870 EXPECT_FALSE(service()->GetExtensionById(good_crx, false));
3871 EXPECT_EQ(extensions::disable_reason::DISABLE_CORRUPTED,
3872 ExtensionPrefs::Get(profile())->GetDisableReasons(good_crx));
[email protected]65187152012-06-02 13:14:143873}
3874
3875// Tests uninstalling an extension when prohibited by the ManagementPolicy.
[email protected]d9a61e12012-11-14 02:43:473876TEST_F(ExtensionServiceTest, ManagementPolicyProhibitsUninstall) {
[email protected]65187152012-06-02 13:14:143877 InitializeEmptyExtensionService();
3878
[email protected]f484f8d52014-06-12 08:38:183879 InstallCRX(data_dir().AppendASCII("good.crx"), INSTALL_NEW);
3880 EXPECT_EQ(1u, registry()->enabled_extensions().size());
3881 EXPECT_EQ(0u, registry()->disabled_extensions().size());
[email protected]65187152012-06-02 13:14:143882
[email protected]f484f8d52014-06-12 08:38:183883 GetManagementPolicy()->UnregisterAllProviders();
[email protected]65187152012-06-02 13:14:143884 extensions::TestManagementPolicyProvider provider(
3885 extensions::TestManagementPolicyProvider::PROHIBIT_MODIFY_STATUS);
[email protected]f484f8d52014-06-12 08:38:183886 GetManagementPolicy()->RegisterProvider(&provider);
[email protected]65187152012-06-02 13:14:143887
3888 // Attempt to uninstall it.
[email protected]42d58f62014-07-31 01:32:453889 EXPECT_FALSE(
3890 service()->UninstallExtension(good_crx,
3891 extensions::UNINSTALL_REASON_FOR_TESTING,
[email protected]42d58f62014-07-31 01:32:453892 NULL));
[email protected]65187152012-06-02 13:14:143893
[email protected]f484f8d52014-06-12 08:38:183894 EXPECT_EQ(1u, registry()->enabled_extensions().size());
3895 EXPECT_TRUE(service()->GetExtensionById(good_crx, false));
[email protected]65187152012-06-02 13:14:143896}
3897
3898// Tests that previously installed extensions that are now prohibited from
Karan Bhatia2a117232017-08-23 00:24:563899// being installed are disabled.
[email protected]d9a61e12012-11-14 02:43:473900TEST_F(ExtensionServiceTest, ManagementPolicyUnloadsAllProhibited) {
[email protected]65187152012-06-02 13:14:143901 InitializeEmptyExtensionService();
3902
[email protected]f484f8d52014-06-12 08:38:183903 InstallCRX(data_dir().AppendASCII("good.crx"), INSTALL_NEW);
3904 InstallCRX(data_dir().AppendASCII("page_action.crx"), INSTALL_NEW);
3905 EXPECT_EQ(2u, registry()->enabled_extensions().size());
3906 EXPECT_EQ(0u, registry()->disabled_extensions().size());
[email protected]65187152012-06-02 13:14:143907
[email protected]f484f8d52014-06-12 08:38:183908 GetManagementPolicy()->UnregisterAllProviders();
[email protected]65187152012-06-02 13:14:143909 extensions::TestManagementPolicyProvider provider(
3910 extensions::TestManagementPolicyProvider::PROHIBIT_LOAD);
[email protected]f484f8d52014-06-12 08:38:183911 GetManagementPolicy()->RegisterProvider(&provider);
[email protected]65187152012-06-02 13:14:143912
Karan Bhatia2a117232017-08-23 00:24:563913 ExtensionPrefs* prefs = ExtensionPrefs::Get(profile());
3914
[email protected]65187152012-06-02 13:14:143915 // Run the policy check.
[email protected]f484f8d52014-06-12 08:38:183916 service()->CheckManagementPolicy();
3917 EXPECT_EQ(0u, registry()->enabled_extensions().size());
Karan Bhatia2a117232017-08-23 00:24:563918 EXPECT_EQ(2u, registry()->disabled_extensions().size());
3919 EXPECT_EQ(extensions::disable_reason::DISABLE_BLOCKED_BY_POLICY,
3920 prefs->GetDisableReasons(good_crx));
3921 EXPECT_EQ(extensions::disable_reason::DISABLE_BLOCKED_BY_POLICY,
3922 prefs->GetDisableReasons(page_action));
3923
3924 // Removing the extensions from policy blacklist should re-enable them.
3925 GetManagementPolicy()->UnregisterAllProviders();
3926 service()->CheckManagementPolicy();
3927 EXPECT_EQ(2u, registry()->enabled_extensions().size());
[email protected]f484f8d52014-06-12 08:38:183928 EXPECT_EQ(0u, registry()->disabled_extensions().size());
[email protected]65187152012-06-02 13:14:143929}
3930
3931// Tests that previously disabled extensions that are now required to be
3932// enabled are re-enabled on reinstall.
[email protected]d9a61e12012-11-14 02:43:473933TEST_F(ExtensionServiceTest, ManagementPolicyRequiresEnable) {
[email protected]65187152012-06-02 13:14:143934 InitializeEmptyExtensionService();
3935
3936 // Install, then disable, an extension.
[email protected]f484f8d52014-06-12 08:38:183937 InstallCRX(data_dir().AppendASCII("good.crx"), INSTALL_NEW);
3938 EXPECT_EQ(1u, registry()->enabled_extensions().size());
Minh X. Nguyen45479012017-08-18 21:35:363939 service()->DisableExtension(good_crx,
3940 extensions::disable_reason::DISABLE_USER_ACTION);
[email protected]f484f8d52014-06-12 08:38:183941 EXPECT_EQ(1u, registry()->disabled_extensions().size());
[email protected]65187152012-06-02 13:14:143942
binjin47947f842014-11-18 12:10:243943 // Register an ExtensionManagementPolicy that requires the extension to remain
[email protected]65187152012-06-02 13:14:143944 // enabled.
[email protected]f484f8d52014-06-12 08:38:183945 GetManagementPolicy()->UnregisterAllProviders();
[email protected]65187152012-06-02 13:14:143946 extensions::TestManagementPolicyProvider provider(
3947 extensions::TestManagementPolicyProvider::MUST_REMAIN_ENABLED);
[email protected]f484f8d52014-06-12 08:38:183948 GetManagementPolicy()->RegisterProvider(&provider);
[email protected]65187152012-06-02 13:14:143949
3950 // Reinstall the extension.
[email protected]f484f8d52014-06-12 08:38:183951 InstallCRX(data_dir().AppendASCII("good.crx"), INSTALL_UPDATED);
3952 EXPECT_EQ(1u, registry()->enabled_extensions().size());
3953 EXPECT_EQ(0u, registry()->disabled_extensions().size());
[email protected]65187152012-06-02 13:14:143954}
3955
binjin47947f842014-11-18 12:10:243956// Tests that extensions disabled by management policy can be installed but
3957// will get disabled after installing.
3958TEST_F(ExtensionServiceTest, ManagementPolicyProhibitsEnableOnInstalled) {
3959 InitializeEmptyExtensionService();
3960
3961 // Register an ExtensionManagementPolicy that disables all extensions, with
Minh X. Nguyen45479012017-08-18 21:35:363962 // a specified disable_reason::DisableReason.
binjin47947f842014-11-18 12:10:243963 GetManagementPolicy()->UnregisterAllProviders();
3964 extensions::TestManagementPolicyProvider provider(
3965 extensions::TestManagementPolicyProvider::MUST_REMAIN_DISABLED);
Minh X. Nguyen45479012017-08-18 21:35:363966 provider.SetDisableReason(extensions::disable_reason::DISABLE_NOT_VERIFIED);
binjin47947f842014-11-18 12:10:243967 GetManagementPolicy()->RegisterProvider(&provider);
3968
3969 // Attempts to install an extensions, it should be installed but disabled.
3970 EXPECT_EQ(0u, registry()->enabled_extensions().size());
3971 EXPECT_EQ(0u, registry()->disabled_extensions().size());
3972 InstallCRX(data_dir().AppendASCII("good.crx"), INSTALL_WITHOUT_LOAD);
3973 EXPECT_EQ(0u, registry()->enabled_extensions().size());
3974 EXPECT_EQ(1u, registry()->disabled_extensions().size());
3975
3976 // Verifies that the disable reason is set properly.
Minh X. Nguyen45479012017-08-18 21:35:363977 EXPECT_EQ(extensions::disable_reason::DISABLE_NOT_VERIFIED,
binjin47947f842014-11-18 12:10:243978 service()->extension_prefs_->GetDisableReasons(kGoodId));
3979}
3980
binjine6b58b52014-10-31 01:55:573981// Tests that extensions with conflicting required permissions by enterprise
3982// policy cannot be installed.
3983TEST_F(ExtensionServiceTest, PolicyBlockedPermissionNewExtensionInstall) {
3984 InitializeEmptyExtensionServiceWithTestingPrefs();
3985 base::FilePath path = data_dir().AppendASCII("permissions_blocklist");
3986
3987 {
3988 // Update policy to block one of the required permissions of target.
3989 ManagementPrefUpdater pref(profile_->GetTestingPrefService());
3990 pref.AddBlockedPermission("*", "tabs");
3991 }
3992
3993 // The extension should be failed to install.
3994 PackAndInstallCRX(path, INSTALL_FAILED);
3995
3996 {
3997 // Update policy to block one of the optional permissions instead.
3998 ManagementPrefUpdater pref(profile_->GetTestingPrefService());
3999 pref.ClearBlockedPermissions("*");
4000 pref.AddBlockedPermission("*", "history");
4001 }
4002
4003 // The extension should succeed to install this time.
4004 std::string id = PackAndInstallCRX(path, INSTALL_NEW)->id();
4005
4006 // Uninstall the extension and update policy to block some arbitrary
4007 // unknown permission.
Devlin Cronin6fd1cd62017-12-05 19:13:574008 UninstallExtension(id);
binjine6b58b52014-10-31 01:55:574009 {
4010 ManagementPrefUpdater pref(profile_->GetTestingPrefService());
4011 pref.ClearBlockedPermissions("*");
4012 pref.AddBlockedPermission("*", "unknown.permission.for.testing");
4013 }
4014
4015 // The extension should succeed to install as well.
4016 PackAndInstallCRX(path, INSTALL_NEW);
4017}
4018
4019// Tests that extension supposed to be force installed but with conflicting
4020// required permissions cannot be installed.
4021TEST_F(ExtensionServiceTest, PolicyBlockedPermissionConflictsWithForceInstall) {
4022 InitializeEmptyExtensionServiceWithTestingPrefs();
4023
4024 // Pack the crx file.
4025 base::FilePath path = data_dir().AppendASCII("permissions_blocklist");
4026 base::FilePath pem_path = data_dir().AppendASCII("permissions_blocklist.pem");
4027 base::ScopedTempDir temp_dir;
4028 EXPECT_TRUE(temp_dir.CreateUniqueTempDir());
vabr9142fe22016-09-08 13:19:224029 base::FilePath crx_path = temp_dir.GetPath().AppendASCII("temp.crx");
binjine6b58b52014-10-31 01:55:574030
4031 PackCRX(path, pem_path, crx_path);
4032
4033 {
4034 // Block one of the required permissions.
4035 ManagementPrefUpdater pref(profile_->GetTestingPrefService());
4036 pref.AddBlockedPermission("*", "tabs");
4037 }
4038
lazyboya00eafc2017-04-08 00:57:194039 // Use MockExternalProvider to simulate force installing extension.
4040 MockExternalProvider* provider =
lazyboy8a08c9d2017-04-11 19:53:224041 AddMockExternalProvider(Manifest::EXTERNAL_POLICY_DOWNLOAD);
binjine6b58b52014-10-31 01:55:574042 provider->UpdateOrAddExtension(permissions_blocklist, "1.0", crx_path);
4043
lazyboy8a08c9d2017-04-11 19:53:224044 // Attempts to force install this extension.
4045 WaitForExternalExtensionInstalled();
binjine6b58b52014-10-31 01:55:574046
4047 // The extension should not be installed.
4048 ASSERT_FALSE(service()->GetInstalledExtension(permissions_blocklist));
4049
4050 // Remove this extension from pending extension manager as we would like to
4051 // give another attempt later.
4052 service()->pending_extension_manager()->Remove(permissions_blocklist);
4053
4054 {
4055 // Clears the permission block list.
4056 ManagementPrefUpdater pref(profile_->GetTestingPrefService());
4057 pref.ClearBlockedPermissions("*");
4058 }
4059
lazyboy8a08c9d2017-04-11 19:53:224060 // Attempts to force install this extension again.
4061 WaitForExternalExtensionInstalled();
binjine6b58b52014-10-31 01:55:574062
4063 const Extension* installed =
4064 service()->GetInstalledExtension(permissions_blocklist);
4065 ASSERT_TRUE(installed);
4066 EXPECT_EQ(installed->location(), Manifest::EXTERNAL_POLICY_DOWNLOAD);
4067}
4068
4069// Tests that newer versions of an extension with conflicting required
4070// permissions by enterprise policy cannot be updated to.
4071TEST_F(ExtensionServiceTest, PolicyBlockedPermissionExtensionUpdate) {
4072 InitializeEmptyExtensionServiceWithTestingPrefs();
4073
4074 base::FilePath path = data_dir().AppendASCII("permissions_blocklist");
4075 base::FilePath path2 = data_dir().AppendASCII("permissions_blocklist2");
4076 base::FilePath pem_path = data_dir().AppendASCII("permissions_blocklist.pem");
4077
4078 // Install 'permissions_blocklist'.
4079 const Extension* installed = PackAndInstallCRX(path, pem_path, INSTALL_NEW);
4080 EXPECT_EQ(installed->id(), permissions_blocklist);
4081
4082 {
4083 // Block one of the required permissions of 'permissions_blocklist2'.
4084 ManagementPrefUpdater pref(profile_->GetTestingPrefService());
4085 pref.AddBlockedPermission("*", "downloads");
4086 }
4087
4088 // Install 'permissions_blocklist' again, should be updated.
4089 const Extension* updated = PackAndInstallCRX(path, pem_path, INSTALL_UPDATED);
4090 EXPECT_EQ(updated->id(), permissions_blocklist);
4091
4092 std::string old_version = updated->VersionString();
4093
4094 // Attempts to update to 'permissions_blocklist2' should fail.
4095 PackAndInstallCRX(path2, pem_path, INSTALL_FAILED);
4096
4097 // Verify that the old version is still enabled.
4098 updated = service()->GetExtensionById(permissions_blocklist, false);
4099 ASSERT_TRUE(updated);
4100 EXPECT_EQ(old_version, updated->VersionString());
4101}
4102
4103// Tests that policy update with additional permissions blocked revoke
4104// conflicting granted optional permissions and unload extensions with
4105// conflicting required permissions, including the force installed ones.
4106TEST_F(ExtensionServiceTest, PolicyBlockedPermissionPolicyUpdate) {
4107 InitializeEmptyExtensionServiceWithTestingPrefs();
4108
4109 base::FilePath path = data_dir().AppendASCII("permissions_blocklist");
4110 base::FilePath path2 = data_dir().AppendASCII("permissions_blocklist2");
4111 base::FilePath pem_path = data_dir().AppendASCII("permissions_blocklist.pem");
4112
4113 // Pack the crx file.
4114 base::ScopedTempDir temp_dir;
4115 EXPECT_TRUE(temp_dir.CreateUniqueTempDir());
vabr9142fe22016-09-08 13:19:224116 base::FilePath crx_path = temp_dir.GetPath().AppendASCII("temp.crx");
binjine6b58b52014-10-31 01:55:574117
4118 PackCRX(path2, pem_path, crx_path);
4119
4120 // Install two arbitary extensions with specified manifest.
4121 std::string ext1 = PackAndInstallCRX(path, INSTALL_NEW)->id();
4122 std::string ext2 = PackAndInstallCRX(path2, INSTALL_NEW)->id();
4123 ASSERT_NE(ext1, permissions_blocklist);
4124 ASSERT_NE(ext2, permissions_blocklist);
4125 ASSERT_NE(ext1, ext2);
4126
4127 // Force install another extension with known id and same manifest as 'ext2'.
4128 std::string ext2_forced = permissions_blocklist;
lazyboya00eafc2017-04-08 00:57:194129 MockExternalProvider* provider =
lazyboy8a08c9d2017-04-11 19:53:224130 AddMockExternalProvider(Manifest::EXTERNAL_POLICY_DOWNLOAD);
binjine6b58b52014-10-31 01:55:574131 provider->UpdateOrAddExtension(ext2_forced, "2.0", crx_path);
lazyboy8a08c9d2017-04-11 19:53:224132 WaitForExternalExtensionInstalled();
binjine6b58b52014-10-31 01:55:574133
4134 extensions::ExtensionRegistry* registry =
4135 extensions::ExtensionRegistry::Get(profile());
4136
4137 // Verify all three extensions are installed and enabled.
4138 ASSERT_TRUE(registry->enabled_extensions().GetByID(ext1));
4139 ASSERT_TRUE(registry->enabled_extensions().GetByID(ext2));
4140 ASSERT_TRUE(registry->enabled_extensions().GetByID(ext2_forced));
4141
4142 // Grant all optional permissions to each extension.
4143 GrantAllOptionalPermissions(ext1);
4144 GrantAllOptionalPermissions(ext2);
4145 GrantAllOptionalPermissions(ext2_forced);
4146
dchengc963c7142016-04-08 03:55:224147 std::unique_ptr<const PermissionSet> active_permissions =
rdevlin.cronine2d0fd02015-09-24 22:35:494148 ExtensionPrefs::Get(profile())->GetActivePermissions(ext1);
binjine6b58b52014-10-31 01:55:574149 EXPECT_TRUE(active_permissions->HasAPIPermission(
4150 extensions::APIPermission::kDownloads));
4151
4152 // Set policy to block 'downloads' permission.
4153 {
4154 ManagementPrefUpdater pref(profile_->GetTestingPrefService());
4155 pref.AddBlockedPermission("*", "downloads");
4156 }
4157
Gabriel Charette01507a22017-09-27 21:30:084158 content::RunAllTasksUntilIdle();
binjine6b58b52014-10-31 01:55:574159
4160 // 'ext1' should still be enabled, but with 'downloads' permission revoked.
4161 EXPECT_TRUE(registry->enabled_extensions().GetByID(ext1));
4162 active_permissions =
4163 ExtensionPrefs::Get(profile())->GetActivePermissions(ext1);
4164 EXPECT_FALSE(active_permissions->HasAPIPermission(
4165 extensions::APIPermission::kDownloads));
4166
4167 // 'ext2' should be disabled because one of its required permissions is
4168 // blocked.
4169 EXPECT_FALSE(registry->enabled_extensions().GetByID(ext2));
4170
4171 // 'ext2_forced' should be handled the same as 'ext2'
4172 EXPECT_FALSE(registry->enabled_extensions().GetByID(ext2_forced));
4173}
4174
[email protected]ebc3aec2013-10-22 02:39:174175// Flaky on windows; http://crbug.com/309833
4176#if defined(OS_WIN)
4177#define MAYBE_ExternalExtensionAutoAcknowledgement DISABLED_ExternalExtensionAutoAcknowledgement
4178#else
4179#define MAYBE_ExternalExtensionAutoAcknowledgement ExternalExtensionAutoAcknowledgement
4180#endif
[email protected]893642c2014-02-03 06:53:134181TEST_F(ExtensionServiceTest, MAYBE_ExternalExtensionAutoAcknowledgement) {
[email protected]a39921b42012-02-28 03:42:544182 InitializeEmptyExtensionService();
[email protected]a39921b42012-02-28 03:42:544183
4184 {
4185 // Register and install an external extension.
lazyboya00eafc2017-04-08 00:57:194186 MockExternalProvider* provider =
lazyboy8a08c9d2017-04-11 19:53:224187 AddMockExternalProvider(Manifest::EXTERNAL_PREF);
[email protected]f484f8d52014-06-12 08:38:184188 provider->UpdateOrAddExtension(
4189 good_crx, "1.0.0.0", data_dir().AppendASCII("good.crx"));
[email protected]a39921b42012-02-28 03:42:544190 }
4191 {
4192 // Have policy force-install an extension.
lazyboya00eafc2017-04-08 00:57:194193 MockExternalProvider* provider =
lazyboy8a08c9d2017-04-11 19:53:224194 AddMockExternalProvider(Manifest::EXTERNAL_POLICY_DOWNLOAD);
[email protected]f484f8d52014-06-12 08:38:184195 provider->UpdateOrAddExtension(
4196 page_action, "1.0.0.0", data_dir().AppendASCII("page_action.crx"));
[email protected]a39921b42012-02-28 03:42:544197 }
4198
4199 // Providers are set up. Let them run.
[email protected]97d6a5c2013-11-11 23:51:244200 int count = 2;
4201 content::WindowedNotificationObserver observer(
[email protected]adf5a102014-07-31 12:44:064202 extensions::NOTIFICATION_CRX_INSTALLER_DONE,
[email protected]97d6a5c2013-11-11 23:51:244203 base::Bind(&WaitForCountNotificationsCallback, &count));
[email protected]f484f8d52014-06-12 08:38:184204 service()->CheckForExternalUpdates();
[email protected]6d057a0c2013-07-09 21:12:074205
[email protected]97d6a5c2013-11-11 23:51:244206 observer.Wait();
[email protected]a39921b42012-02-28 03:42:544207
[email protected]f484f8d52014-06-12 08:38:184208 ASSERT_EQ(2u, registry()->enabled_extensions().size());
4209 EXPECT_TRUE(service()->GetExtensionById(good_crx, false));
4210 EXPECT_TRUE(service()->GetExtensionById(page_action, false));
4211 ExtensionPrefs* prefs = ExtensionPrefs::Get(profile());
[email protected]a39921b42012-02-28 03:42:544212 ASSERT_TRUE(!prefs->IsExternalExtensionAcknowledged(good_crx));
4213 ASSERT_TRUE(prefs->IsExternalExtensionAcknowledged(page_action));
4214}
4215
rdevlin.cronine1456712016-12-29 22:47:284216// Tests that an extension added through an external source is initially
4217// disabled with the "prompt for external extensions" feature.
4218TEST_F(ExtensionServiceTest, ExternalExtensionDisabledOnInstallation) {
4219 FeatureSwitch::ScopedOverride external_prompt_override(
4220 FeatureSwitch::prompt_for_external_extensions(), true);
4221 InitializeEmptyExtensionService();
4222
4223 // Register and install an external extension.
lazyboya00eafc2017-04-08 00:57:194224 MockExternalProvider* provider =
lazyboy8a08c9d2017-04-11 19:53:224225 AddMockExternalProvider(Manifest::EXTERNAL_PREF); // Takes ownership.
rdevlin.cronine1456712016-12-29 22:47:284226 provider->UpdateOrAddExtension(good_crx, "1.0.0.0",
4227 data_dir().AppendASCII("good.crx"));
4228
4229 WaitForExternalExtensionInstalled();
4230
4231 EXPECT_TRUE(registry()->disabled_extensions().Contains(good_crx));
4232 ExtensionPrefs* prefs = ExtensionPrefs::Get(profile());
4233 EXPECT_FALSE(prefs->IsExternalExtensionAcknowledged(good_crx));
Minh X. Nguyen45479012017-08-18 21:35:364234 EXPECT_EQ(extensions::disable_reason::DISABLE_EXTERNAL_EXTENSION,
rdevlin.cronine1456712016-12-29 22:47:284235 prefs->GetDisableReasons(good_crx));
4236
4237 // Updating the extension shouldn't cause it to be enabled.
4238 provider->UpdateOrAddExtension(good_crx, "1.0.0.1",
4239 data_dir().AppendASCII("good2.crx"));
4240 WaitForExternalExtensionInstalled();
4241
4242 EXPECT_TRUE(registry()->disabled_extensions().Contains(good_crx));
4243 EXPECT_FALSE(prefs->IsExternalExtensionAcknowledged(good_crx));
Minh X. Nguyen45479012017-08-18 21:35:364244 EXPECT_EQ(extensions::disable_reason::DISABLE_EXTERNAL_EXTENSION,
rdevlin.cronine1456712016-12-29 22:47:284245 prefs->GetDisableReasons(good_crx));
4246 const Extension* extension =
4247 registry()->disabled_extensions().GetByID(good_crx);
4248 ASSERT_TRUE(extension);
4249 // Double check that we did, in fact, update the extension.
Devlin Cronin03bf2d22017-12-20 08:21:054250 EXPECT_EQ("1.0.0.1", extension->version().GetString());
rdevlin.cronine1456712016-12-29 22:47:284251}
4252
4253// Test that if an extension is installed before the "prompt for external
4254// extensions" feature is enabled, but is updated when the feature is
4255// enabled, the extension is not disabled.
4256TEST_F(ExtensionServiceTest, ExternalExtensionIsNotDisabledOnUpdate) {
4257 auto external_prompt_override =
4258 base::MakeUnique<FeatureSwitch::ScopedOverride>(
4259 FeatureSwitch::prompt_for_external_extensions(), false);
4260 InitializeEmptyExtensionService();
4261
4262 // Register and install an external extension.
lazyboya00eafc2017-04-08 00:57:194263 MockExternalProvider* provider =
lazyboy8a08c9d2017-04-11 19:53:224264 AddMockExternalProvider(Manifest::EXTERNAL_PREF);
rdevlin.cronine1456712016-12-29 22:47:284265 provider->UpdateOrAddExtension(good_crx, "1.0.0.0",
4266 data_dir().AppendASCII("good.crx"));
4267
4268 WaitForExternalExtensionInstalled();
4269
4270 EXPECT_TRUE(registry()->enabled_extensions().Contains(good_crx));
4271 ExtensionPrefs* prefs = ExtensionPrefs::Get(profile());
4272 EXPECT_FALSE(prefs->IsExternalExtensionAcknowledged(good_crx));
Minh X. Nguyen45479012017-08-18 21:35:364273 EXPECT_EQ(extensions::disable_reason::DISABLE_NONE,
4274 prefs->GetDisableReasons(good_crx));
rdevlin.cronine1456712016-12-29 22:47:284275
4276 provider->UpdateOrAddExtension(good_crx, "1.0.0.1",
4277 data_dir().AppendASCII("good2.crx"));
4278
4279 // We explicitly reset the override first. ScopedOverrides reset the value
4280 // to the original value on destruction, but if we reset by passing a new
4281 // object, the new object is constructed (overriding the current value)
4282 // before the old is destructed (which will immediately reset to the
4283 // original).
4284 external_prompt_override.reset();
4285 external_prompt_override = base::MakeUnique<FeatureSwitch::ScopedOverride>(
4286 FeatureSwitch::prompt_for_external_extensions(), true);
4287 WaitForExternalExtensionInstalled();
4288
4289 EXPECT_TRUE(registry()->enabled_extensions().Contains(good_crx));
4290 {
4291 const Extension* extension =
4292 registry()->enabled_extensions().GetByID(good_crx);
4293 ASSERT_TRUE(extension);
Devlin Cronin03bf2d22017-12-20 08:21:054294 EXPECT_EQ("1.0.0.1", extension->version().GetString());
rdevlin.cronine1456712016-12-29 22:47:284295 }
4296 EXPECT_FALSE(prefs->IsExternalExtensionAcknowledged(good_crx));
Minh X. Nguyen45479012017-08-18 21:35:364297 EXPECT_EQ(extensions::disable_reason::DISABLE_NONE,
4298 prefs->GetDisableReasons(good_crx));
rdevlin.cronine1456712016-12-29 22:47:284299}
4300
[email protected]a7cd28e2012-10-05 21:03:364301#if !defined(OS_CHROMEOS)
4302// This tests if default apps are installed correctly.
[email protected]d9a61e12012-11-14 02:43:474303TEST_F(ExtensionServiceTest, DefaultAppsInstall) {
[email protected]a7cd28e2012-10-05 21:03:364304 InitializeEmptyExtensionService();
[email protected]a7cd28e2012-10-05 21:03:364305
4306 {
4307 std::string json_data =
4308 "{"
4309 " \"ldnnhddmnhbkjipkidpdiheffobcpfmf\" : {"
4310 " \"external_crx\": \"good.crx\","
4311 " \"external_version\": \"1.0.0.0\","
4312 " \"is_bookmark_app\": false"
4313 " }"
4314 "}";
[email protected]f484f8d52014-06-12 08:38:184315 default_apps::Provider* provider = new default_apps::Provider(
4316 profile(),
4317 service(),
4318 new extensions::ExternalTestingLoader(json_data, data_dir()),
4319 Manifest::INTERNAL,
4320 Manifest::INVALID_LOCATION,
4321 Extension::FROM_WEBSTORE | Extension::WAS_INSTALLED_BY_DEFAULT);
[email protected]a7cd28e2012-10-05 21:03:364322
lazyboy8a08c9d2017-04-11 19:53:224323 service()->AddProviderForTesting(base::WrapUnique(provider));
[email protected]a7cd28e2012-10-05 21:03:364324 }
4325
[email protected]f484f8d52014-06-12 08:38:184326 ASSERT_EQ(0u, registry()->enabled_extensions().size());
lazyboy8a08c9d2017-04-11 19:53:224327 WaitForExternalExtensionInstalled();
[email protected]a7cd28e2012-10-05 21:03:364328
[email protected]f484f8d52014-06-12 08:38:184329 ASSERT_EQ(1u, registry()->enabled_extensions().size());
4330 EXPECT_TRUE(service()->GetExtensionById(good_crx, false));
4331 const Extension* extension = service()->GetExtensionById(good_crx, false);
[email protected]a7cd28e2012-10-05 21:03:364332 EXPECT_TRUE(extension->from_webstore());
4333 EXPECT_TRUE(extension->was_installed_by_default());
[email protected]a7cd28e2012-10-05 21:03:364334}
4335#endif
4336
timlohac695622017-04-03 07:52:584337// Crashes on Linux/CrOS. https://crbug.com/703712
4338#if defined(OS_LINUX) || defined(OS_CHROMEOS)
4339#define MAYBE_UpdatingPendingExternalExtensionWithFlags \
4340 DISABLED_UpdatingPendingExternalExtensionWithFlags
4341#else
4342#define MAYBE_UpdatingPendingExternalExtensionWithFlags \
4343 UpdatingPendingExternalExtensionWithFlags
4344#endif
4345
4346TEST_F(ExtensionServiceTest, MAYBE_UpdatingPendingExternalExtensionWithFlags) {
mamir0128d5a2016-07-15 20:55:484347 // Regression test for crbug.com/627522
4348 const char kPrefFromBookmark[] = "from_bookmark";
4349
4350 InitializeEmptyExtensionService();
4351
4352 base::FilePath path = data_dir().AppendASCII("good.crx");
mamir0128d5a2016-07-15 20:55:484353
4354 // Register and install an external extension.
Devlin Cronind4c2a8f32017-09-29 17:08:304355 base::Version version("1.0.0.0");
mamir0128d5a2016-07-15 20:55:484356 content::WindowedNotificationObserver observer(
4357 extensions::NOTIFICATION_CRX_INSTALLER_DONE,
4358 content::NotificationService::AllSources());
Devlin Cronin92c52cac2017-10-02 21:29:014359 ExternalInstallInfoFile info(good_crx, version, path, Manifest::EXTERNAL_PREF,
4360 Extension::FROM_BOOKMARK,
4361 false /* mark_acknowledged */,
4362 false /* install_immediately */);
4363 ASSERT_TRUE(service()->OnExternalExtensionFileFound(info));
mamir0128d5a2016-07-15 20:55:484364 EXPECT_TRUE(service()->pending_extension_manager()->IsIdPending(good_crx));
4365
4366 // Upgrade to version 2.0, the flag should be preserved.
4367 path = data_dir().AppendASCII("good2.crx");
4368 UpdateExtension(good_crx, path, ENABLED);
4369 ASSERT_TRUE(ValidateBooleanPref(good_crx, kPrefFromBookmark, true));
4370 const Extension* extension = service()->GetExtensionById(good_crx, false);
4371 ASSERT_TRUE(extension);
4372 ASSERT_TRUE(extension->from_bookmark());
4373}
4374
[email protected]cd500f72010-06-25 23:44:324375// Tests disabling extensions
[email protected]d9a61e12012-11-14 02:43:474376TEST_F(ExtensionServiceTest, DisableExtension) {
[email protected]eaa7dd182010-12-14 11:09:004377 InitializeEmptyExtensionService();
[email protected]cd500f72010-06-25 23:44:324378
[email protected]f484f8d52014-06-12 08:38:184379 InstallCRX(data_dir().AppendASCII("good.crx"), INSTALL_NEW);
4380 EXPECT_TRUE(service()->GetExtensionById(good_crx, true));
4381 EXPECT_TRUE(service()->GetExtensionById(good_crx, false));
[email protected]5fdfa562013-12-27 17:43:594382
[email protected]f484f8d52014-06-12 08:38:184383 EXPECT_EQ(1u, registry()->enabled_extensions().size());
4384 EXPECT_EQ(0u, registry()->disabled_extensions().size());
4385 EXPECT_EQ(0u, registry()->terminated_extensions().size());
4386 EXPECT_EQ(0u, registry()->blacklisted_extensions().size());
[email protected]cd500f72010-06-25 23:44:324387
4388 // Disable it.
Minh X. Nguyen45479012017-08-18 21:35:364389 service()->DisableExtension(good_crx,
4390 extensions::disable_reason::DISABLE_USER_ACTION);
[email protected]cd500f72010-06-25 23:44:324391
[email protected]f484f8d52014-06-12 08:38:184392 EXPECT_TRUE(service()->GetExtensionById(good_crx, true));
4393 EXPECT_FALSE(service()->GetExtensionById(good_crx, false));
4394 EXPECT_EQ(0u, registry()->enabled_extensions().size());
4395 EXPECT_EQ(1u, registry()->disabled_extensions().size());
4396 EXPECT_EQ(0u, registry()->terminated_extensions().size());
4397 EXPECT_EQ(0u, registry()->blacklisted_extensions().size());
[email protected]bb1bc9b32013-12-21 03:09:144398}
4399
4400TEST_F(ExtensionServiceTest, TerminateExtension) {
4401 InitializeEmptyExtensionService();
4402
[email protected]f484f8d52014-06-12 08:38:184403 InstallCRX(data_dir().AppendASCII("good.crx"), INSTALL_NEW);
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());
[email protected]bb1bc9b32013-12-21 03:09:144408
4409 TerminateExtension(good_crx);
4410
[email protected]f484f8d52014-06-12 08:38:184411 EXPECT_EQ(0u, registry()->enabled_extensions().size());
4412 EXPECT_EQ(0u, registry()->disabled_extensions().size());
4413 EXPECT_EQ(1u, registry()->terminated_extensions().size());
4414 EXPECT_EQ(0u, registry()->blacklisted_extensions().size());
[email protected]fa2416f2011-05-03 08:41:204415}
4416
[email protected]d9a61e12012-11-14 02:43:474417TEST_F(ExtensionServiceTest, DisableTerminatedExtension) {
[email protected]fa2416f2011-05-03 08:41:204418 InitializeEmptyExtensionService();
4419
[email protected]f484f8d52014-06-12 08:38:184420 InstallCRX(data_dir().AppendASCII("good.crx"), INSTALL_NEW);
[email protected]fa2416f2011-05-03 08:41:204421 TerminateExtension(good_crx);
[email protected]f484f8d52014-06-12 08:38:184422 EXPECT_TRUE(registry()->GetExtensionById(
[email protected]e066da32014-03-26 18:27:354423 good_crx, extensions::ExtensionRegistry::TERMINATED));
[email protected]fa2416f2011-05-03 08:41:204424
4425 // Disable it.
Minh X. Nguyen45479012017-08-18 21:35:364426 service()->DisableExtension(good_crx,
4427 extensions::disable_reason::DISABLE_USER_ACTION);
[email protected]fa2416f2011-05-03 08:41:204428
[email protected]f484f8d52014-06-12 08:38:184429 EXPECT_FALSE(registry()->GetExtensionById(
[email protected]e066da32014-03-26 18:27:354430 good_crx, extensions::ExtensionRegistry::TERMINATED));
[email protected]f484f8d52014-06-12 08:38:184431 EXPECT_TRUE(service()->GetExtensionById(good_crx, true));
[email protected]bb1bc9b32013-12-21 03:09:144432
[email protected]f484f8d52014-06-12 08:38:184433 EXPECT_EQ(0u, registry()->enabled_extensions().size());
4434 EXPECT_EQ(1u, registry()->disabled_extensions().size());
4435 EXPECT_EQ(0u, registry()->terminated_extensions().size());
4436 EXPECT_EQ(0u, registry()->blacklisted_extensions().size());
[email protected]cd500f72010-06-25 23:44:324437}
4438
rdevlin.cronin7217be52017-03-24 20:47:054439// Tests that with the kDisableExtensions flag, extensions are not loaded by
4440// the ExtensionService...
4441TEST_F(ExtensionServiceTest, PRE_DisableAllExtensions) {
4442 base::CommandLine::ForCurrentProcess()->AppendSwitch(
4443 switches::kDisableExtensions);
4444 InitializeGoodInstalledExtensionService();
4445 service()->Init();
4446 EXPECT_TRUE(registry()->GenerateInstalledExtensionsSet()->is_empty());
4447}
4448
4449// ... But, if we remove the switch, they are.
[email protected]d9a61e12012-11-14 02:43:474450TEST_F(ExtensionServiceTest, DisableAllExtensions) {
rdevlin.cronin7217be52017-03-24 20:47:054451 EXPECT_FALSE(base::CommandLine::ForCurrentProcess()->HasSwitch(
4452 switches::kDisableExtensions));
4453 InitializeGoodInstalledExtensionService();
4454 service()->Init();
4455 EXPECT_FALSE(registry()->GenerateInstalledExtensionsSet()->is_empty());
4456 EXPECT_FALSE(registry()->enabled_extensions().is_empty());
[email protected]d728e002010-12-08 04:46:234457}
4458
[email protected]8f512c72011-11-22 21:02:504459// Tests reloading extensions.
[email protected]d9a61e12012-11-14 02:43:474460TEST_F(ExtensionServiceTest, ReloadExtensions) {
[email protected]eaa7dd182010-12-14 11:09:004461 InitializeEmptyExtensionService();
[email protected]cd500f72010-06-25 23:44:324462
4463 // Simple extension that should install without error.
[email protected]f484f8d52014-06-12 08:38:184464 base::FilePath path = data_dir().AppendASCII("good.crx");
[email protected]26367b62012-10-04 23:03:324465 InstallCRX(path, INSTALL_NEW,
4466 Extension::FROM_WEBSTORE | Extension::WAS_INSTALLED_BY_DEFAULT);
thestig4b36dd32014-10-31 20:30:194467 const char* const extension_id = good_crx;
Minh X. Nguyen45479012017-08-18 21:35:364468 service()->DisableExtension(extension_id,
4469 extensions::disable_reason::DISABLE_USER_ACTION);
[email protected]cd500f72010-06-25 23:44:324470
[email protected]f484f8d52014-06-12 08:38:184471 EXPECT_EQ(0u, registry()->enabled_extensions().size());
4472 EXPECT_EQ(1u, registry()->disabled_extensions().size());
[email protected]cd500f72010-06-25 23:44:324473
[email protected]f484f8d52014-06-12 08:38:184474 service()->ReloadExtensionsForTest();
[email protected]cd500f72010-06-25 23:44:324475
[email protected]26367b62012-10-04 23:03:324476 // The creation flags should not change when reloading the extension.
[email protected]f484f8d52014-06-12 08:38:184477 const Extension* extension = service()->GetExtensionById(good_crx, true);
[email protected]26367b62012-10-04 23:03:324478 EXPECT_TRUE(extension->from_webstore());
4479 EXPECT_TRUE(extension->was_installed_by_default());
4480 EXPECT_FALSE(extension->from_bookmark());
4481
[email protected]cd500f72010-06-25 23:44:324482 // Extension counts shouldn't change.
[email protected]f484f8d52014-06-12 08:38:184483 EXPECT_EQ(0u, registry()->enabled_extensions().size());
4484 EXPECT_EQ(1u, registry()->disabled_extensions().size());
[email protected]cd500f72010-06-25 23:44:324485
[email protected]f484f8d52014-06-12 08:38:184486 service()->EnableExtension(extension_id);
[email protected]cd500f72010-06-25 23:44:324487
[email protected]f484f8d52014-06-12 08:38:184488 EXPECT_EQ(1u, registry()->enabled_extensions().size());
4489 EXPECT_EQ(0u, registry()->disabled_extensions().size());
[email protected]cd500f72010-06-25 23:44:324490
[email protected]5a73f902010-06-30 02:29:414491 // Need to clear |loaded_| manually before reloading as the
4492 // EnableExtension() call above inserted into it and
4493 // UnloadAllExtensions() doesn't send out notifications.
4494 loaded_.clear();
[email protected]f484f8d52014-06-12 08:38:184495 service()->ReloadExtensionsForTest();
[email protected]cd500f72010-06-25 23:44:324496
4497 // Extension counts shouldn't change.
[email protected]f484f8d52014-06-12 08:38:184498 EXPECT_EQ(1u, registry()->enabled_extensions().size());
4499 EXPECT_EQ(0u, registry()->disabled_extensions().size());
[email protected]cd500f72010-06-25 23:44:324500}
4501
[email protected]2a947202013-03-06 04:58:054502// Tests reloading an extension.
4503TEST_F(ExtensionServiceTest, ReloadExtension) {
4504 InitializeEmptyExtensionService();
[email protected]2a947202013-03-06 04:58:054505
4506 // Simple extension that should install without error.
thestig4b36dd32014-10-31 20:30:194507 const char extension_id[] = "behllobkkfkfnphdnhnkndlbkcpglgmj";
[email protected]f484f8d52014-06-12 08:38:184508 base::FilePath ext = data_dir()
4509 .AppendASCII("good")
4510 .AppendASCII("Extensions")
4511 .AppendASCII(extension_id)
4512 .AppendASCII("1.0.0.0");
4513 extensions::UnpackedInstaller::Create(service())->Load(ext);
Gabriel Charette01507a22017-09-27 21:30:084514 content::RunAllTasksUntilIdle();
[email protected]2a947202013-03-06 04:58:054515
[email protected]f484f8d52014-06-12 08:38:184516 EXPECT_EQ(1u, registry()->enabled_extensions().size());
4517 EXPECT_EQ(0u, registry()->disabled_extensions().size());
[email protected]2a947202013-03-06 04:58:054518
[email protected]f484f8d52014-06-12 08:38:184519 service()->ReloadExtension(extension_id);
[email protected]2a947202013-03-06 04:58:054520
4521 // Extension should be disabled now, waiting to be reloaded.
[email protected]f484f8d52014-06-12 08:38:184522 EXPECT_EQ(0u, registry()->enabled_extensions().size());
4523 EXPECT_EQ(1u, registry()->disabled_extensions().size());
Minh X. Nguyen45479012017-08-18 21:35:364524 EXPECT_EQ(extensions::disable_reason::DISABLE_RELOAD,
[email protected]f484f8d52014-06-12 08:38:184525 ExtensionPrefs::Get(profile())->GetDisableReasons(extension_id));
[email protected]2a947202013-03-06 04:58:054526
[email protected]a5768512013-04-12 19:35:354527 // Reloading again should not crash.
[email protected]f484f8d52014-06-12 08:38:184528 service()->ReloadExtension(extension_id);
[email protected]2a947202013-03-06 04:58:054529
4530 // Finish reloading
Gabriel Charette01507a22017-09-27 21:30:084531 content::RunAllTasksUntilIdle();
[email protected]2a947202013-03-06 04:58:054532
4533 // Extension should be enabled again.
[email protected]f484f8d52014-06-12 08:38:184534 EXPECT_EQ(1u, registry()->enabled_extensions().size());
4535 EXPECT_EQ(0u, registry()->disabled_extensions().size());
[email protected]2a947202013-03-06 04:58:054536}
4537
[email protected]d9a61e12012-11-14 02:43:474538TEST_F(ExtensionServiceTest, UninstallExtension) {
[email protected]eaa7dd182010-12-14 11:09:004539 InitializeEmptyExtensionService();
[email protected]f484f8d52014-06-12 08:38:184540 InstallCRX(data_dir().AppendASCII("good.crx"), INSTALL_NEW);
4541 EXPECT_EQ(1u, registry()->enabled_extensions().size());
Devlin Cronin6fd1cd62017-12-05 19:13:574542 UninstallExtension(good_crx);
[email protected]f484f8d52014-06-12 08:38:184543 EXPECT_EQ(0u, registry()->enabled_extensions().size());
limasdf0deef2042017-05-03 19:17:174544 EXPECT_EQ(UnloadedExtensionReason::UNINSTALL, unloaded_reason_);
[email protected]fa2416f2011-05-03 08:41:204545}
[email protected]631cf822009-05-15 07:01:254546
[email protected]d9a61e12012-11-14 02:43:474547TEST_F(ExtensionServiceTest, UninstallTerminatedExtension) {
[email protected]fa2416f2011-05-03 08:41:204548 InitializeEmptyExtensionService();
[email protected]f484f8d52014-06-12 08:38:184549 InstallCRX(data_dir().AppendASCII("good.crx"), INSTALL_NEW);
[email protected]fa2416f2011-05-03 08:41:204550 TerminateExtension(good_crx);
Devlin Cronin6fd1cd62017-12-05 19:13:574551 UninstallExtension(good_crx);
limasdf0deef2042017-05-03 19:17:174552 EXPECT_EQ(UnloadedExtensionReason::TERMINATE, unloaded_reason_);
[email protected]6aeac8342010-10-01 20:21:184553}
4554
[email protected]98270432012-09-11 20:51:244555// An extension disabled because of unsupported requirements should re-enabled
4556// if updated to a version with supported requirements as long as there are no
4557// other disable reasons.
[email protected]d9a61e12012-11-14 02:43:474558TEST_F(ExtensionServiceTest, UpgradingRequirementsEnabled) {
[email protected]98270432012-09-11 20:51:244559 InitializeEmptyExtensionService();
zmo2602bcf2017-04-01 00:12:034560 content::GpuDataManager::GetInstance()->BlacklistWebGLForTesting();
[email protected]98270432012-09-11 20:51:244561
[email protected]f484f8d52014-06-12 08:38:184562 base::FilePath path = data_dir().AppendASCII("requirements");
4563 base::FilePath pem_path =
4564 data_dir().AppendASCII("requirements").AppendASCII("v1_good.pem");
[email protected]98270432012-09-11 20:51:244565 const Extension* extension_v1 = PackAndInstallCRX(path.AppendASCII("v1_good"),
4566 pem_path,
4567 INSTALL_NEW);
4568 std::string id = extension_v1->id();
[email protected]f484f8d52014-06-12 08:38:184569 EXPECT_TRUE(service()->IsExtensionEnabled(id));
[email protected]98270432012-09-11 20:51:244570
[email protected]650b2d52013-02-10 03:41:454571 base::FilePath v2_bad_requirements_crx = GetTemporaryFile();
[email protected]a17dfcf2012-12-30 02:07:094572
4573 PackCRX(path.AppendASCII("v2_bad_requirements"),
4574 pem_path,
4575 v2_bad_requirements_crx);
4576 UpdateExtension(id, v2_bad_requirements_crx, INSTALLED);
[email protected]f484f8d52014-06-12 08:38:184577 EXPECT_FALSE(service()->IsExtensionEnabled(id));
[email protected]98270432012-09-11 20:51:244578
[email protected]650b2d52013-02-10 03:41:454579 base::FilePath v3_good_crx = GetTemporaryFile();
[email protected]a17dfcf2012-12-30 02:07:094580
4581 PackCRX(path.AppendASCII("v3_good"), pem_path, v3_good_crx);
4582 UpdateExtension(id, v3_good_crx, ENABLED);
[email protected]f484f8d52014-06-12 08:38:184583 EXPECT_TRUE(service()->IsExtensionEnabled(id));
[email protected]98270432012-09-11 20:51:244584}
4585
4586// Extensions disabled through user action should stay disabled.
[email protected]d9a61e12012-11-14 02:43:474587TEST_F(ExtensionServiceTest, UpgradingRequirementsDisabled) {
[email protected]98270432012-09-11 20:51:244588 InitializeEmptyExtensionService();
zmo2602bcf2017-04-01 00:12:034589 content::GpuDataManager::GetInstance()->BlacklistWebGLForTesting();
[email protected]98270432012-09-11 20:51:244590
[email protected]f484f8d52014-06-12 08:38:184591 base::FilePath path = data_dir().AppendASCII("requirements");
4592 base::FilePath pem_path =
4593 data_dir().AppendASCII("requirements").AppendASCII("v1_good.pem");
[email protected]98270432012-09-11 20:51:244594 const Extension* extension_v1 = PackAndInstallCRX(path.AppendASCII("v1_good"),
4595 pem_path,
4596 INSTALL_NEW);
4597 std::string id = extension_v1->id();
Minh X. Nguyen45479012017-08-18 21:35:364598 service()->DisableExtension(id,
4599 extensions::disable_reason::DISABLE_USER_ACTION);
[email protected]f484f8d52014-06-12 08:38:184600 EXPECT_FALSE(service()->IsExtensionEnabled(id));
[email protected]98270432012-09-11 20:51:244601
[email protected]650b2d52013-02-10 03:41:454602 base::FilePath v2_bad_requirements_crx = GetTemporaryFile();
[email protected]a17dfcf2012-12-30 02:07:094603
4604 PackCRX(path.AppendASCII("v2_bad_requirements"),
4605 pem_path,
4606 v2_bad_requirements_crx);
4607 UpdateExtension(id, v2_bad_requirements_crx, INSTALLED);
[email protected]f484f8d52014-06-12 08:38:184608 EXPECT_FALSE(service()->IsExtensionEnabled(id));
[email protected]98270432012-09-11 20:51:244609
[email protected]650b2d52013-02-10 03:41:454610 base::FilePath v3_good_crx = GetTemporaryFile();
[email protected]a17dfcf2012-12-30 02:07:094611
4612 PackCRX(path.AppendASCII("v3_good"), pem_path, v3_good_crx);
4613 UpdateExtension(id, v3_good_crx, INSTALLED);
[email protected]f484f8d52014-06-12 08:38:184614 EXPECT_FALSE(service()->IsExtensionEnabled(id));
[email protected]98270432012-09-11 20:51:244615}
4616
4617// The extension should not re-enabled because it was disabled from a
4618// permission increase.
[email protected]d9a61e12012-11-14 02:43:474619TEST_F(ExtensionServiceTest, UpgradingRequirementsPermissions) {
[email protected]98270432012-09-11 20:51:244620 InitializeEmptyExtensionService();
zmo2602bcf2017-04-01 00:12:034621 content::GpuDataManager::GetInstance()->BlacklistWebGLForTesting();
[email protected]98270432012-09-11 20:51:244622
[email protected]f484f8d52014-06-12 08:38:184623 base::FilePath path = data_dir().AppendASCII("requirements");
4624 base::FilePath pem_path =
4625 data_dir().AppendASCII("requirements").AppendASCII("v1_good.pem");
[email protected]98270432012-09-11 20:51:244626 const Extension* extension_v1 = PackAndInstallCRX(path.AppendASCII("v1_good"),
4627 pem_path,
4628 INSTALL_NEW);
4629 std::string id = extension_v1->id();
[email protected]f484f8d52014-06-12 08:38:184630 EXPECT_TRUE(service()->IsExtensionEnabled(id));
[email protected]98270432012-09-11 20:51:244631
[email protected]650b2d52013-02-10 03:41:454632 base::FilePath v2_bad_requirements_and_permissions_crx = GetTemporaryFile();
[email protected]a17dfcf2012-12-30 02:07:094633
4634 PackCRX(path.AppendASCII("v2_bad_requirements_and_permissions"),
4635 pem_path,
4636 v2_bad_requirements_and_permissions_crx);
4637 UpdateExtension(id, v2_bad_requirements_and_permissions_crx, INSTALLED);
[email protected]f484f8d52014-06-12 08:38:184638 EXPECT_FALSE(service()->IsExtensionEnabled(id));
[email protected]98270432012-09-11 20:51:244639
[email protected]650b2d52013-02-10 03:41:454640 base::FilePath v3_bad_permissions_crx = GetTemporaryFile();
[email protected]a17dfcf2012-12-30 02:07:094641
4642 PackCRX(path.AppendASCII("v3_bad_permissions"),
4643 pem_path,
4644 v3_bad_permissions_crx);
4645 UpdateExtension(id, v3_bad_permissions_crx, INSTALLED);
[email protected]f484f8d52014-06-12 08:38:184646 EXPECT_FALSE(service()->IsExtensionEnabled(id));
[email protected]98270432012-09-11 20:51:244647}
4648
4649// Unpacked extensions are not allowed to be installed if they have unsupported
4650// requirements.
[email protected]d9a61e12012-11-14 02:43:474651TEST_F(ExtensionServiceTest, UnpackedRequirements) {
[email protected]98270432012-09-11 20:51:244652 InitializeEmptyExtensionService();
zmo2602bcf2017-04-01 00:12:034653 content::GpuDataManager::GetInstance()->BlacklistWebGLForTesting();
[email protected]98270432012-09-11 20:51:244654
[email protected]f484f8d52014-06-12 08:38:184655 base::FilePath path =
4656 data_dir().AppendASCII("requirements").AppendASCII("v2_bad_requirements");
4657 extensions::UnpackedInstaller::Create(service())->Load(path);
Gabriel Charette01507a22017-09-27 21:30:084658 content::RunAllTasksUntilIdle();
[email protected]98270432012-09-11 20:51:244659 EXPECT_EQ(1u, GetErrors().size());
[email protected]f484f8d52014-06-12 08:38:184660 EXPECT_EQ(0u, registry()->enabled_extensions().size());
[email protected]98270432012-09-11 20:51:244661}
4662
[email protected]c4148a72011-08-09 23:04:204663class ExtensionCookieCallback {
4664 public:
Gabriel Charettee3769cf2017-07-27 16:52:504665 ExtensionCookieCallback() : result_(false) {}
[email protected]c4148a72011-08-09 23:04:204666
4667 void SetCookieCallback(bool result) {
[email protected]c4148a72011-08-09 23:04:204668 result_ = result;
4669 }
4670
4671 void GetAllCookiesCallback(const net::CookieList& list) {
[email protected]c4148a72011-08-09 23:04:204672 list_ = list;
4673 }
4674 net::CookieList list_;
4675 bool result_;
[email protected]c4148a72011-08-09 23:04:204676};
4677
Joshua Bell607cb142017-07-24 19:17:164678namespace {
4679// Helper to create (open, close, verify) a WebSQL database.
4680// Must be run on the DatabaseTracker's task runner.
4681void CreateDatabase(storage::DatabaseTracker* db_tracker,
4682 const std::string& origin_id) {
4683 DCHECK(db_tracker->task_runner()->RunsTasksInCurrentSequence());
4684 base::string16 db_name = base::UTF8ToUTF16("db");
4685 base::string16 description = base::UTF8ToUTF16("db_description");
4686 int64_t size;
4687 db_tracker->DatabaseOpened(origin_id, db_name, description, 1, &size);
4688 db_tracker->DatabaseClosed(origin_id, db_name);
4689 std::vector<storage::OriginInfo> origins;
4690 db_tracker->GetAllOriginsInfo(&origins);
4691 EXPECT_EQ(1U, origins.size());
4692 EXPECT_EQ(origin_id, origins[0].GetOriginIdentifier());
4693}
4694} // namespace
4695
[email protected]0d6ec3a72011-09-02 02:09:434696// Verifies extension state is removed upon uninstall.
[email protected]42d58f62014-07-31 01:32:454697TEST_F(ExtensionServiceTest, ClearExtensionData) {
[email protected]eaa7dd182010-12-14 11:09:004698 InitializeEmptyExtensionService();
[email protected]c4148a72011-08-09 23:04:204699 ExtensionCookieCallback callback;
[email protected]c10da4b02010-03-25 14:38:324700
4701 // Load a test extension.
[email protected]f484f8d52014-06-12 08:38:184702 base::FilePath path = data_dir();
[email protected]c10da4b02010-03-25 14:38:324703 path = path.AppendASCII("good.crx");
[email protected]8f512c72011-11-22 21:02:504704 const Extension* extension = InstallCRX(path, INSTALL_NEW);
[email protected]c10da4b02010-03-25 14:38:324705 ASSERT_TRUE(extension);
4706 GURL ext_url(extension->url());
[email protected]cd501a72014-08-22 19:58:314707 std::string origin_id = storage::GetIdentifierFromOrigin(ext_url);
[email protected]c10da4b02010-03-25 14:38:324708
4709 // Set a cookie for the extension.
mmenkee28c9aa2016-01-20 23:54:044710 net::CookieStore* cookie_store = profile()->GetRequestContextForExtensions()
4711 ->GetURLRequestContext()
4712 ->cookie_store();
4713 ASSERT_TRUE(cookie_store);
[email protected]c10da4b02010-03-25 14:38:324714 net::CookieOptions options;
mmenkee28c9aa2016-01-20 23:54:044715 cookie_store->SetCookieWithOptionsAsync(
[email protected]a5768512013-04-12 19:35:354716 ext_url, "dummy=value", options,
4717 base::Bind(&ExtensionCookieCallback::SetCookieCallback,
4718 base::Unretained(&callback)));
Gabriel Charette01507a22017-09-27 21:30:084719 content::RunAllTasksUntilIdle();
[email protected]c4148a72011-08-09 23:04:204720 EXPECT_TRUE(callback.result_);
4721
mmenkee28c9aa2016-01-20 23:54:044722 cookie_store->GetAllCookiesForURLAsync(
[email protected]c4148a72011-08-09 23:04:204723 ext_url,
4724 base::Bind(&ExtensionCookieCallback::GetAllCookiesCallback,
4725 base::Unretained(&callback)));
Gabriel Charette01507a22017-09-27 21:30:084726 content::RunAllTasksUntilIdle();
[email protected]c4148a72011-08-09 23:04:204727 EXPECT_EQ(1U, callback.list_.size());
[email protected]c10da4b02010-03-25 14:38:324728
4729 // Open a database.
[email protected]cd501a72014-08-22 19:58:314730 storage::DatabaseTracker* db_tracker =
[email protected]f484f8d52014-06-12 08:38:184731 BrowserContext::GetDefaultStoragePartition(profile())
4732 ->GetDatabaseTracker();
Joshua Bell607cb142017-07-24 19:17:164733 db_tracker->task_runner()->PostTask(
4734 FROM_HERE,
4735 base::BindOnce(&CreateDatabase, base::Unretained(db_tracker), origin_id));
Gabriel Charette01507a22017-09-27 21:30:084736 content::RunAllTasksUntilIdle();
[email protected]c10da4b02010-03-25 14:38:324737
[email protected]98823c682012-06-11 21:18:244738 // Create local storage. We only simulate this by creating the backing files.
4739 // Note: This test depends on details of how the dom_storage library
4740 // stores data in the host file system.
[email protected]650b2d52013-02-10 03:41:454741 base::FilePath lso_dir_path =
[email protected]f484f8d52014-06-12 08:38:184742 profile()->GetPath().AppendASCII("Local Storage");
[email protected]5e301592013-06-18 06:36:054743 base::FilePath lso_file_path = lso_dir_path.AppendASCII(origin_id)
4744 .AddExtension(FILE_PATH_LITERAL(".localstorage"));
[email protected]426d1c92013-12-03 20:08:544745 EXPECT_TRUE(base::CreateDirectory(lso_dir_path));
[email protected]e5c2a22e2014-03-06 20:42:304746 EXPECT_EQ(0, base::WriteFile(lso_file_path, NULL, 0));
[email protected]7567484142013-07-11 17:36:074747 EXPECT_TRUE(base::PathExists(lso_file_path));
[email protected]c10da4b02010-03-25 14:38:324748
[email protected]ab308092011-08-25 23:37:194749 // Create indexed db. Similarly, it is enough to only simulate this by
jsbellbd2caa02017-07-14 01:13:074750 // creating the directory on the disk, and resetting the caches of
4751 // "known" origins.
[email protected]f484f8d52014-06-12 08:38:184752 IndexedDBContext* idb_context = BrowserContext::GetDefaultStoragePartition(
4753 profile())->GetIndexedDBContext();
jsbellb8f7a912016-03-10 18:59:504754 base::FilePath idb_path = idb_context->GetFilePathForTesting(ext_url);
[email protected]426d1c92013-12-03 20:08:544755 EXPECT_TRUE(base::CreateDirectory(idb_path));
[email protected]dcd16612013-07-15 20:18:094756 EXPECT_TRUE(base::DirectoryExists(idb_path));
jsbellbd2caa02017-07-14 01:13:074757 idb_context->ResetCachesForTesting();
[email protected]e1dcf922010-11-22 19:12:124758
[email protected]c10da4b02010-03-25 14:38:324759 // Uninstall the extension.
[email protected]42d58f62014-07-31 01:32:454760 ASSERT_TRUE(
4761 service()->UninstallExtension(good_crx,
4762 extensions::UNINSTALL_REASON_FOR_TESTING,
[email protected]42d58f62014-07-31 01:32:454763 NULL));
Devlin Cronin218df7f2017-11-21 21:41:314764 // The data deletion happens on the IO thread; since we use a
4765 // TestBrowserThreadBundle (without REAL_IO_THREAD), the IO and UI threads are
4766 // the same, and RunAllTasksUntilIdle() should run IO thread tasks.
4767 content::RunAllTasksUntilIdle();
[email protected]c10da4b02010-03-25 14:38:324768
4769 // Check that the cookie is gone.
mmenkee28c9aa2016-01-20 23:54:044770 cookie_store->GetAllCookiesForURLAsync(
[email protected]c4148a72011-08-09 23:04:204771 ext_url,
4772 base::Bind(&ExtensionCookieCallback::GetAllCookiesCallback,
4773 base::Unretained(&callback)));
Gabriel Charette01507a22017-09-27 21:30:084774 content::RunAllTasksUntilIdle();
[email protected]c4148a72011-08-09 23:04:204775 EXPECT_EQ(0U, callback.list_.size());
[email protected]c10da4b02010-03-25 14:38:324776
4777 // The database should have vanished as well.
Joshua Bell607cb142017-07-24 19:17:164778 db_tracker->task_runner()->PostTask(
4779 FROM_HERE, base::BindOnce(
4780 [](storage::DatabaseTracker* db_tracker) {
4781 std::vector<storage::OriginInfo> origins;
4782 db_tracker->GetAllOriginsInfo(&origins);
4783 EXPECT_EQ(0U, origins.size());
4784 },
4785 base::Unretained(db_tracker)));
Gabriel Charette01507a22017-09-27 21:30:084786 content::RunAllTasksUntilIdle();
[email protected]c10da4b02010-03-25 14:38:324787
4788 // Check that the LSO file has been removed.
[email protected]7567484142013-07-11 17:36:074789 EXPECT_FALSE(base::PathExists(lso_file_path));
[email protected]e1dcf922010-11-22 19:12:124790
4791 // Check if the indexed db has disappeared too.
[email protected]dcd16612013-07-15 20:18:094792 EXPECT_FALSE(base::DirectoryExists(idb_path));
[email protected]c10da4b02010-03-25 14:38:324793}
4794
[email protected]0d6ec3a72011-09-02 02:09:434795// Verifies app state is removed upon uninstall.
[email protected]d9a61e12012-11-14 02:43:474796TEST_F(ExtensionServiceTest, ClearAppData) {
[email protected]0d6ec3a72011-09-02 02:09:434797 InitializeEmptyExtensionService();
[email protected]0d6ec3a72011-09-02 02:09:434798 ExtensionCookieCallback callback;
4799
4800 int pref_count = 0;
4801
4802 // Install app1 with unlimited storage.
[email protected]8f512c72011-11-22 21:02:504803 const Extension* extension =
[email protected]f484f8d52014-06-12 08:38:184804 PackAndInstallCRX(data_dir().AppendASCII("app1"), INSTALL_NEW);
[email protected]0d6ec3a72011-09-02 02:09:434805 ValidatePrefKeyCount(++pref_count);
[email protected]f484f8d52014-06-12 08:38:184806 ASSERT_EQ(1u, registry()->enabled_extensions().size());
[email protected]0d6ec3a72011-09-02 02:09:434807 const std::string id1 = extension->id();
[email protected]076ebeda2014-06-06 21:47:264808 EXPECT_TRUE(extension->permissions_data()->HasAPIPermission(
[email protected]c2e66e12012-06-27 06:27:064809 APIPermission::kUnlimitedStorage));
[email protected]6b414c232013-06-05 07:53:344810 const GURL origin1(
4811 extensions::AppLaunchInfo::GetFullLaunchURL(extension).GetOrigin());
[email protected]f484f8d52014-06-12 08:38:184812 EXPECT_TRUE(profile()->GetExtensionSpecialStoragePolicy()->IsStorageUnlimited(
4813 origin1));
[email protected]cd501a72014-08-22 19:58:314814 std::string origin_id = storage::GetIdentifierFromOrigin(origin1);
[email protected]0d6ec3a72011-09-02 02:09:434815
4816 // Install app2 from the same origin with unlimited storage.
[email protected]f484f8d52014-06-12 08:38:184817 extension = PackAndInstallCRX(data_dir().AppendASCII("app2"), INSTALL_NEW);
[email protected]0d6ec3a72011-09-02 02:09:434818 ValidatePrefKeyCount(++pref_count);
[email protected]f484f8d52014-06-12 08:38:184819 ASSERT_EQ(2u, registry()->enabled_extensions().size());
[email protected]0d6ec3a72011-09-02 02:09:434820 const std::string id2 = extension->id();
[email protected]076ebeda2014-06-06 21:47:264821 EXPECT_TRUE(extension->permissions_data()->HasAPIPermission(
[email protected]c2e66e12012-06-27 06:27:064822 APIPermission::kUnlimitedStorage));
[email protected]0d6ec3a72011-09-02 02:09:434823 EXPECT_TRUE(extension->web_extent().MatchesURL(
[email protected]6b414c232013-06-05 07:53:344824 extensions::AppLaunchInfo::GetFullLaunchURL(extension)));
4825 const GURL origin2(
4826 extensions::AppLaunchInfo::GetFullLaunchURL(extension).GetOrigin());
[email protected]0d6ec3a72011-09-02 02:09:434827 EXPECT_EQ(origin1, origin2);
[email protected]f484f8d52014-06-12 08:38:184828 EXPECT_TRUE(profile()->GetExtensionSpecialStoragePolicy()->IsStorageUnlimited(
4829 origin2));
[email protected]0d6ec3a72011-09-02 02:09:434830
4831 // Set a cookie for the extension.
mmenkee28c9aa2016-01-20 23:54:044832 net::CookieStore* cookie_store = profile()->GetRequestContext()
4833 ->GetURLRequestContext()
4834 ->cookie_store();
4835 ASSERT_TRUE(cookie_store);
[email protected]0d6ec3a72011-09-02 02:09:434836 net::CookieOptions options;
mmenkee28c9aa2016-01-20 23:54:044837 cookie_store->SetCookieWithOptionsAsync(
[email protected]0d6ec3a72011-09-02 02:09:434838 origin1, "dummy=value", options,
4839 base::Bind(&ExtensionCookieCallback::SetCookieCallback,
4840 base::Unretained(&callback)));
Gabriel Charette01507a22017-09-27 21:30:084841 content::RunAllTasksUntilIdle();
[email protected]0d6ec3a72011-09-02 02:09:434842 EXPECT_TRUE(callback.result_);
4843
mmenkee28c9aa2016-01-20 23:54:044844 cookie_store->GetAllCookiesForURLAsync(
[email protected]0d6ec3a72011-09-02 02:09:434845 origin1,
4846 base::Bind(&ExtensionCookieCallback::GetAllCookiesCallback,
4847 base::Unretained(&callback)));
Gabriel Charette01507a22017-09-27 21:30:084848 content::RunAllTasksUntilIdle();
[email protected]0d6ec3a72011-09-02 02:09:434849 EXPECT_EQ(1U, callback.list_.size());
4850
4851 // Open a database.
[email protected]cd501a72014-08-22 19:58:314852 storage::DatabaseTracker* db_tracker =
[email protected]f484f8d52014-06-12 08:38:184853 BrowserContext::GetDefaultStoragePartition(profile())
4854 ->GetDatabaseTracker();
Joshua Bell607cb142017-07-24 19:17:164855 db_tracker->task_runner()->PostTask(
4856 FROM_HERE,
4857 base::BindOnce(&CreateDatabase, base::Unretained(db_tracker), origin_id));
Gabriel Charette01507a22017-09-27 21:30:084858 content::RunAllTasksUntilIdle();
[email protected]0d6ec3a72011-09-02 02:09:434859
[email protected]98823c682012-06-11 21:18:244860 // Create local storage. We only simulate this by creating the backing files.
4861 // Note: This test depends on details of how the dom_storage library
4862 // stores data in the host file system.
[email protected]650b2d52013-02-10 03:41:454863 base::FilePath lso_dir_path =
[email protected]f484f8d52014-06-12 08:38:184864 profile()->GetPath().AppendASCII("Local Storage");
[email protected]5e301592013-06-18 06:36:054865 base::FilePath lso_file_path = lso_dir_path.AppendASCII(origin_id)
4866 .AddExtension(FILE_PATH_LITERAL(".localstorage"));
[email protected]426d1c92013-12-03 20:08:544867 EXPECT_TRUE(base::CreateDirectory(lso_dir_path));
[email protected]e5c2a22e2014-03-06 20:42:304868 EXPECT_EQ(0, base::WriteFile(lso_file_path, NULL, 0));
[email protected]7567484142013-07-11 17:36:074869 EXPECT_TRUE(base::PathExists(lso_file_path));
[email protected]0d6ec3a72011-09-02 02:09:434870
4871 // Create indexed db. Similarly, it is enough to only simulate this by
jsbellbd2caa02017-07-14 01:13:074872 // creating the directory on the disk, and resetting the caches of
4873 // "known" origins.
[email protected]f484f8d52014-06-12 08:38:184874 IndexedDBContext* idb_context = BrowserContext::GetDefaultStoragePartition(
4875 profile())->GetIndexedDBContext();
jsbellb8f7a912016-03-10 18:59:504876 base::FilePath idb_path = idb_context->GetFilePathForTesting(origin1);
[email protected]426d1c92013-12-03 20:08:544877 EXPECT_TRUE(base::CreateDirectory(idb_path));
[email protected]dcd16612013-07-15 20:18:094878 EXPECT_TRUE(base::DirectoryExists(idb_path));
jsbellbd2caa02017-07-14 01:13:074879 idb_context->ResetCachesForTesting();
[email protected]0d6ec3a72011-09-02 02:09:434880
4881 // Uninstall one of them, unlimited storage should still be granted
4882 // to the origin.
Devlin Cronin6fd1cd62017-12-05 19:13:574883 UninstallExtension(id1);
[email protected]f484f8d52014-06-12 08:38:184884 EXPECT_EQ(1u, registry()->enabled_extensions().size());
4885 EXPECT_TRUE(profile()->GetExtensionSpecialStoragePolicy()->IsStorageUnlimited(
4886 origin1));
[email protected]0d6ec3a72011-09-02 02:09:434887
4888 // Check that the cookie is still there.
mmenkee28c9aa2016-01-20 23:54:044889 cookie_store->GetAllCookiesForURLAsync(
[email protected]0d6ec3a72011-09-02 02:09:434890 origin1,
4891 base::Bind(&ExtensionCookieCallback::GetAllCookiesCallback,
4892 base::Unretained(&callback)));
Gabriel Charette01507a22017-09-27 21:30:084893 content::RunAllTasksUntilIdle();
[email protected]0d6ec3a72011-09-02 02:09:434894 EXPECT_EQ(1U, callback.list_.size());
4895
4896 // Now uninstall the other. Storage should be cleared for the apps.
Devlin Cronin6fd1cd62017-12-05 19:13:574897 UninstallExtension(id2);
[email protected]f484f8d52014-06-12 08:38:184898 EXPECT_EQ(0u, registry()->enabled_extensions().size());
4899 EXPECT_FALSE(
4900 profile()->GetExtensionSpecialStoragePolicy()->IsStorageUnlimited(
4901 origin1));
[email protected]0d6ec3a72011-09-02 02:09:434902
4903 // Check that the cookie is gone.
mmenkee28c9aa2016-01-20 23:54:044904 cookie_store->GetAllCookiesForURLAsync(
[email protected]0d6ec3a72011-09-02 02:09:434905 origin1,
4906 base::Bind(&ExtensionCookieCallback::GetAllCookiesCallback,
4907 base::Unretained(&callback)));
Gabriel Charette01507a22017-09-27 21:30:084908 content::RunAllTasksUntilIdle();
[email protected]0d6ec3a72011-09-02 02:09:434909 EXPECT_EQ(0U, callback.list_.size());
4910
4911 // The database should have vanished as well.
Joshua Bell607cb142017-07-24 19:17:164912 db_tracker->task_runner()->PostTask(
4913 FROM_HERE, base::BindOnce(
4914 [](storage::DatabaseTracker* db_tracker) {
4915 std::vector<storage::OriginInfo> origins;
4916 db_tracker->GetAllOriginsInfo(&origins);
4917 EXPECT_EQ(0U, origins.size());
4918 },
4919 base::Unretained(db_tracker)));
Gabriel Charette01507a22017-09-27 21:30:084920 content::RunAllTasksUntilIdle();
[email protected]0d6ec3a72011-09-02 02:09:434921
4922 // Check that the LSO file has been removed.
[email protected]7567484142013-07-11 17:36:074923 EXPECT_FALSE(base::PathExists(lso_file_path));
[email protected]0d6ec3a72011-09-02 02:09:434924
4925 // Check if the indexed db has disappeared too.
[email protected]dcd16612013-07-15 20:18:094926 EXPECT_FALSE(base::DirectoryExists(idb_path));
[email protected]0d6ec3a72011-09-02 02:09:434927}
4928
[email protected]894bb502009-05-21 22:39:574929// Tests loading single extensions (like --load-extension)
Devlin Cronina7baea22017-12-12 14:43:054930TEST_F(ExtensionServiceTest, LoadExtension) {
[email protected]eaa7dd182010-12-14 11:09:004931 InitializeEmptyExtensionService();
Devlin Cronina7baea22017-12-12 14:43:054932 extensions::TestExtensionDir good_extension_dir;
4933 good_extension_dir.WriteManifest(
4934 R"({
4935 "name": "Good Extension",
4936 "version": "0.1",
4937 "manifest_version": 2
4938 })");
[email protected]3cf4f0992009-02-03 23:00:304939
Devlin Cronina7baea22017-12-12 14:43:054940 {
4941 extensions::ChromeTestExtensionLoader loader(profile());
4942 loader.set_pack_extension(false);
4943 loader.LoadExtension(good_extension_dir.UnpackedPath());
4944 }
[email protected]bb28e062009-02-27 17:19:184945 EXPECT_EQ(0u, GetErrors().size());
[email protected]f484f8d52014-06-12 08:38:184946 EXPECT_EQ(1u, registry()->enabled_extensions().size());
[email protected]e8c729a2010-03-09 19:55:194947 ValidatePrefKeyCount(1);
[email protected]25b34332009-06-05 21:53:194948
Devlin Cronina7baea22017-12-12 14:43:054949 auto get_extension_by_name = [](const extensions::ExtensionSet& extensions,
4950 const std::string& name) {
4951 // NOTE: lambda type deduction doesn't recognize returning
4952 // const Extension* in one place and nullptr in another as the same type, so
4953 // we have to make sure to return an explicit type here.
4954 const extensions::Extension* result = nullptr;
4955 for (const auto& extension : extensions) {
4956 if (extension->name() == name) {
4957 result = extension.get();
4958 break;
4959 }
4960 }
4961 return result;
4962 };
4963 constexpr const char kGoodExtension[] = "Good Extension";
4964 {
4965 const Extension* extension =
4966 get_extension_by_name(registry()->enabled_extensions(), kGoodExtension);
4967 ASSERT_TRUE(extension);
4968 EXPECT_EQ(Manifest::UNPACKED, extension->location());
4969 }
[email protected]25b34332009-06-05 21:53:194970
Devlin Cronina7baea22017-12-12 14:43:054971 // Try loading an extension with no manifest. It should fail.
4972 extensions::TestExtensionDir bad_extension_dir;
4973 bad_extension_dir.WriteFile(FILE_PATH_LITERAL("background.js"), "// some JS");
4974 {
4975 extensions::ChromeTestExtensionLoader loader(profile());
4976 loader.set_pack_extension(false);
4977 loader.set_should_fail(true);
4978 loader.LoadExtension(bad_extension_dir.UnpackedPath());
4979 }
4980
4981 EXPECT_EQ(1u, GetErrors().size());
4982 EXPECT_EQ(1u, registry()->enabled_extensions().size());
4983 EXPECT_EQ(1u, registry()->GenerateInstalledExtensionsSet()->size());
4984 EXPECT_TRUE(
4985 get_extension_by_name(registry()->enabled_extensions(), kGoodExtension));
4986
4987 // Test uninstalling the good extension.
4988 const extensions::ExtensionId good_id =
4989 get_extension_by_name(registry()->enabled_extensions(), kGoodExtension)
4990 ->id();
4991 service()->UninstallExtension(
4992 good_id, extensions::UNINSTALL_REASON_FOR_TESTING, nullptr);
Gabriel Charette01507a22017-09-27 21:30:084993 content::RunAllTasksUntilIdle();
Devlin Cronina7baea22017-12-12 14:43:054994 EXPECT_TRUE(registry()->GenerateInstalledExtensionsSet()->is_empty());
[email protected]3cf4f0992009-02-03 23:00:304995}
[email protected]0b344962009-03-31 04:21:454996
[email protected]894bb502009-05-21 22:39:574997// Tests that we generate IDs when they are not specified in the manifest for
4998// --load-extension.
[email protected]d9a61e12012-11-14 02:43:474999TEST_F(ExtensionServiceTest, GenerateID) {
[email protected]eaa7dd182010-12-14 11:09:005000 InitializeEmptyExtensionService();
[email protected]fbcc40302009-06-12 20:45:455001
[email protected]f484f8d52014-06-12 08:38:185002 base::FilePath no_id_ext = data_dir().AppendASCII("no_id");
5003 extensions::UnpackedInstaller::Create(service())->Load(no_id_ext);
Gabriel Charette01507a22017-09-27 21:30:085004 content::RunAllTasksUntilIdle();
[email protected]0b344962009-03-31 04:21:455005 EXPECT_EQ(0u, GetErrors().size());
[email protected]894bb502009-05-21 22:39:575006 ASSERT_EQ(1u, loaded_.size());
[email protected]fdd28372014-08-21 02:27:265007 ASSERT_TRUE(crx_file::id_util::IdIsValid(loaded_[0]->id()));
[email protected]12075d12013-02-27 05:38:055008 EXPECT_EQ(loaded_[0]->location(), Manifest::UNPACKED);
[email protected]0b344962009-03-31 04:21:455009
[email protected]e8c729a2010-03-09 19:55:195010 ValidatePrefKeyCount(1);
[email protected]25b34332009-06-05 21:53:195011
[email protected]84ac7f32009-10-06 06:17:545012 std::string previous_id = loaded_[0]->id();
5013
5014 // If we reload the same path, we should get the same extension ID.
[email protected]f484f8d52014-06-12 08:38:185015 extensions::UnpackedInstaller::Create(service())->Load(no_id_ext);
Gabriel Charette01507a22017-09-27 21:30:085016 content::RunAllTasksUntilIdle();
[email protected]84ac7f32009-10-06 06:17:545017 ASSERT_EQ(1u, loaded_.size());
5018 ASSERT_EQ(previous_id, loaded_[0]->id());
[email protected]0b344962009-03-31 04:21:455019}
[email protected]894bb502009-05-21 22:39:575020
[email protected]557c7bd2013-09-18 21:51:255021TEST_F(ExtensionServiceTest, UnpackedValidatesLocales) {
5022 InitializeEmptyExtensionService();
5023
[email protected]f484f8d52014-06-12 08:38:185024 base::FilePath bad_locale =
5025 data_dir().AppendASCII("unpacked").AppendASCII("bad_messages_file");
5026 extensions::UnpackedInstaller::Create(service())->Load(bad_locale);
Gabriel Charette01507a22017-09-27 21:30:085027 content::RunAllTasksUntilIdle();
[email protected]557c7bd2013-09-18 21:51:255028 EXPECT_EQ(1u, GetErrors().size());
5029 base::FilePath ms_messages_file = bad_locale.AppendASCII("_locales")
5030 .AppendASCII("ms")
5031 .AppendASCII("messages.json");
[email protected]04338722013-12-24 23:18:055032 EXPECT_THAT(base::UTF16ToUTF8(GetErrors()[0]), testing::AllOf(
5033 testing::HasSubstr(
5034 base::UTF16ToUTF8(ms_messages_file.LossyDisplayName())),
[email protected]557c7bd2013-09-18 21:51:255035 testing::HasSubstr("Dictionary keys must be quoted.")));
5036 ASSERT_EQ(0u, loaded_.size());
5037}
5038
lazyboya00eafc2017-04-08 00:57:195039void ExtensionServiceTest::TestExternalProvider(MockExternalProvider* provider,
5040 Manifest::Location location) {
[email protected]a1257b12009-06-12 02:51:345041 // Verify that starting with no providers loads no extensions.
[email protected]f484f8d52014-06-12 08:38:185042 service()->Init();
[email protected]a1257b12009-06-12 02:51:345043 ASSERT_EQ(0u, loaded_.size());
5044
[email protected]0a60a2e2010-10-25 16:15:215045 provider->set_visit_count(0);
5046
[email protected]a1257b12009-06-12 02:51:345047 // Register a test extension externally using the mock registry provider.
[email protected]f484f8d52014-06-12 08:38:185048 base::FilePath source_path = data_dir().AppendASCII("good.crx");
[email protected]894bb502009-05-21 22:39:575049
[email protected]a1257b12009-06-12 02:51:345050 // Add the extension.
[email protected]d55e7602009-12-16 04:20:425051 provider->UpdateOrAddExtension(good_crx, "1.0.0.0", source_path);
[email protected]894bb502009-05-21 22:39:575052
[email protected]9f1087e2009-06-15 17:29:325053 // Reloading extensions should find our externally registered extension
[email protected]894bb502009-05-21 22:39:575054 // and install it.
lazyboy8a08c9d2017-04-11 19:53:225055 WaitForExternalExtensionInstalled();
[email protected]894bb502009-05-21 22:39:575056
5057 ASSERT_EQ(0u, GetErrors().size());
5058 ASSERT_EQ(1u, loaded_.size());
[email protected]d55e7602009-12-16 04:20:425059 ASSERT_EQ(location, loaded_[0]->location());
Devlin Cronin03bf2d22017-12-20 08:21:055060 ASSERT_EQ("1.0.0.0", loaded_[0]->version().GetString());
Devlin Cronin55fab54d2017-12-16 02:27:195061 ExtensionPrefs* prefs = ExtensionPrefs::Get(profile());
5062 EXPECT_TRUE(prefs->GetInstalledExtensionInfo(good_crx));
5063 // TODO(devlin): Testing the underlying values of the prefs for extensions
5064 // should be done in an ExtensionPrefs test, not here. This should only be
5065 // using the public ExtensionPrefs interfaces.
[email protected]e2194742010-08-12 05:54:345066 ValidateIntegerPref(good_crx, "state", Extension::ENABLED);
5067 ValidateIntegerPref(good_crx, "location", location);
[email protected]894bb502009-05-21 22:39:575068
[email protected]9f1087e2009-06-15 17:29:325069 // Reload extensions without changing anything. The extension should be
[email protected]894bb502009-05-21 22:39:575070 // loaded again.
5071 loaded_.clear();
[email protected]f484f8d52014-06-12 08:38:185072 service()->ReloadExtensionsForTest();
Gabriel Charette01507a22017-09-27 21:30:085073 content::RunAllTasksUntilIdle();
[email protected]894bb502009-05-21 22:39:575074 ASSERT_EQ(0u, GetErrors().size());
5075 ASSERT_EQ(1u, loaded_.size());
Devlin Cronin55fab54d2017-12-16 02:27:195076 EXPECT_TRUE(prefs->GetInstalledExtensionInfo(good_crx));
[email protected]e2194742010-08-12 05:54:345077 ValidateIntegerPref(good_crx, "state", Extension::ENABLED);
5078 ValidateIntegerPref(good_crx, "location", location);
[email protected]e2eb43112009-05-29 21:19:545079
[email protected]894bb502009-05-21 22:39:575080 // Now update the extension with a new version. We should get upgraded.
5081 source_path = source_path.DirName().AppendASCII("good2.crx");
[email protected]d55e7602009-12-16 04:20:425082 provider->UpdateOrAddExtension(good_crx, "1.0.0.1", source_path);
[email protected]894bb502009-05-21 22:39:575083
5084 loaded_.clear();
lazyboy8a08c9d2017-04-11 19:53:225085 WaitForExternalExtensionInstalled();
[email protected]894bb502009-05-21 22:39:575086 ASSERT_EQ(0u, GetErrors().size());
5087 ASSERT_EQ(1u, loaded_.size());
Devlin Cronin03bf2d22017-12-20 08:21:055088 ASSERT_EQ("1.0.0.1", loaded_[0]->version().GetString());
Devlin Cronin55fab54d2017-12-16 02:27:195089 EXPECT_TRUE(prefs->GetInstalledExtensionInfo(good_crx));
[email protected]e2194742010-08-12 05:54:345090 ValidateIntegerPref(good_crx, "state", Extension::ENABLED);
5091 ValidateIntegerPref(good_crx, "location", location);
[email protected]894bb502009-05-21 22:39:575092
[email protected]27b985d2009-06-25 17:53:155093 // Uninstall the extension and reload. Nothing should happen because the
[email protected]894bb502009-05-21 22:39:575094 // preference should prevent us from reinstalling.
5095 std::string id = loaded_[0]->id();
Devlin Cronin55fab54d2017-12-16 02:27:195096 EXPECT_EQ(id, good_crx);
[email protected]dc24976f2013-06-02 21:15:095097 bool no_uninstall =
[email protected]f484f8d52014-06-12 08:38:185098 GetManagementPolicy()->MustRemainEnabled(loaded_[0].get(), NULL);
[email protected]42d58f62014-07-31 01:32:455099 service()->UninstallExtension(id,
5100 extensions::UNINSTALL_REASON_FOR_TESTING,
[email protected]42d58f62014-07-31 01:32:455101 NULL);
Gabriel Charette01507a22017-09-27 21:30:085102 content::RunAllTasksUntilIdle();
[email protected]894bb502009-05-21 22:39:575103
[email protected]f484f8d52014-06-12 08:38:185104 base::FilePath install_path = extensions_install_dir().AppendASCII(id);
[email protected]e410b5f2012-12-14 14:02:245105 if (no_uninstall) {
[email protected]65187152012-06-02 13:14:145106 // Policy controlled extensions should not have been touched by uninstall.
[email protected]7567484142013-07-11 17:36:075107 ASSERT_TRUE(base::PathExists(install_path));
Devlin Cronin55fab54d2017-12-16 02:27:195108 EXPECT_TRUE(prefs->GetInstalledExtensionInfo(good_crx));
5109 EXPECT_FALSE(prefs->IsExternalExtensionUninstalled(good_crx));
[email protected]65187152012-06-02 13:14:145110 } else {
[email protected]95da88c42011-03-31 10:07:335111 // The extension should also be gone from the install directory.
[email protected]7567484142013-07-11 17:36:075112 ASSERT_FALSE(base::PathExists(install_path));
[email protected]95da88c42011-03-31 10:07:335113 loaded_.clear();
[email protected]f484f8d52014-06-12 08:38:185114 service()->CheckForExternalUpdates();
Gabriel Charette01507a22017-09-27 21:30:085115 content::RunAllTasksUntilIdle();
[email protected]95da88c42011-03-31 10:07:335116 ASSERT_EQ(0u, loaded_.size());
Devlin Cronin55fab54d2017-12-16 02:27:195117 EXPECT_TRUE(prefs->IsExternalExtensionUninstalled(good_crx));
5118 EXPECT_FALSE(prefs->GetInstalledExtensionInfo(good_crx));
[email protected]894bb502009-05-21 22:39:575119
[email protected]95da88c42011-03-31 10:07:335120 // Now clear the preference and reinstall.
Devlin Cronin55fab54d2017-12-16 02:27:195121 prefs->ClearExternalUninstallForTesting(good_crx);
[email protected]25b34332009-06-05 21:53:195122
[email protected]95da88c42011-03-31 10:07:335123 loaded_.clear();
lazyboy8a08c9d2017-04-11 19:53:225124 WaitForExternalExtensionInstalled();
[email protected]95da88c42011-03-31 10:07:335125 ASSERT_EQ(1u, loaded_.size());
[email protected]95da88c42011-03-31 10:07:335126 }
Devlin Cronin55fab54d2017-12-16 02:27:195127 EXPECT_TRUE(prefs->GetInstalledExtensionInfo(good_crx));
5128 EXPECT_FALSE(prefs->IsExternalExtensionUninstalled(good_crx));
[email protected]e2194742010-08-12 05:54:345129 ValidateIntegerPref(good_crx, "state", Extension::ENABLED);
5130 ValidateIntegerPref(good_crx, "location", location);
[email protected]25b34332009-06-05 21:53:195131
[email protected]f484f8d52014-06-12 08:38:185132 if (GetManagementPolicy()->MustRemainEnabled(loaded_[0].get(), NULL)) {
[email protected]65187152012-06-02 13:14:145133 EXPECT_EQ(2, provider->visit_count());
5134 } else {
[email protected]95da88c42011-03-31 10:07:335135 // Now test an externally triggered uninstall (deleting the registry key or
5136 // the pref entry).
5137 provider->RemoveExtension(good_crx);
[email protected]25b34332009-06-05 21:53:195138
[email protected]95da88c42011-03-31 10:07:335139 loaded_.clear();
[email protected]f484f8d52014-06-12 08:38:185140 service()->OnExternalProviderReady(provider);
Gabriel Charette01507a22017-09-27 21:30:085141 content::RunAllTasksUntilIdle();
[email protected]95da88c42011-03-31 10:07:335142 ASSERT_EQ(0u, loaded_.size());
Devlin Cronin55fab54d2017-12-16 02:27:195143 EXPECT_FALSE(prefs->IsExternalExtensionUninstalled(good_crx));
5144 EXPECT_FALSE(prefs->GetInstalledExtensionInfo(good_crx));
[email protected]25b34332009-06-05 21:53:195145
[email protected]95da88c42011-03-31 10:07:335146 // The extension should also be gone from the install directory.
[email protected]7567484142013-07-11 17:36:075147 ASSERT_FALSE(base::PathExists(install_path));
[email protected]abe7a8942009-06-23 05:14:295148
[email protected]95da88c42011-03-31 10:07:335149 // Now test the case where user uninstalls and then the extension is removed
5150 // from the external provider.
[email protected]05aad2da2011-10-28 10:12:375151 provider->UpdateOrAddExtension(good_crx, "1.0.0.1", source_path);
lazyboy8a08c9d2017-04-11 19:53:225152 WaitForExternalExtensionInstalled();
[email protected]abe7a8942009-06-23 05:14:295153
[email protected]95da88c42011-03-31 10:07:335154 ASSERT_EQ(1u, loaded_.size());
5155 ASSERT_EQ(0u, GetErrors().size());
[email protected]d55e7602009-12-16 04:20:425156
[email protected]95da88c42011-03-31 10:07:335157 // User uninstalls.
5158 loaded_.clear();
[email protected]42d58f62014-07-31 01:32:455159 service()->UninstallExtension(id,
5160 extensions::UNINSTALL_REASON_FOR_TESTING,
[email protected]42d58f62014-07-31 01:32:455161 NULL);
Gabriel Charette01507a22017-09-27 21:30:085162 content::RunAllTasksUntilIdle();
[email protected]95da88c42011-03-31 10:07:335163 ASSERT_EQ(0u, loaded_.size());
[email protected]d55e7602009-12-16 04:20:425164
[email protected]95da88c42011-03-31 10:07:335165 // Then remove the extension from the extension provider.
5166 provider->RemoveExtension(good_crx);
[email protected]d55e7602009-12-16 04:20:425167
[email protected]95da88c42011-03-31 10:07:335168 // Should still be at 0.
5169 loaded_.clear();
rdevlin.croninf2e1cb012017-05-27 01:27:595170 service()->ReloadExtensionsForTest();
Gabriel Charette01507a22017-09-27 21:30:085171 content::RunAllTasksUntilIdle();
[email protected]95da88c42011-03-31 10:07:335172 ASSERT_EQ(0u, loaded_.size());
Devlin Cronin55fab54d2017-12-16 02:27:195173
5174 EXPECT_FALSE(prefs->GetInstalledExtensionInfo(good_crx));
5175 EXPECT_TRUE(prefs->IsExternalExtensionUninstalled(good_crx));
[email protected]0a60a2e2010-10-25 16:15:215176
[email protected]95da88c42011-03-31 10:07:335177 EXPECT_EQ(5, provider->visit_count());
[email protected]95da88c42011-03-31 10:07:335178 }
[email protected]d55e7602009-12-16 04:20:425179}
5180
5181// Tests the external installation feature
5182#if defined(OS_WIN)
[email protected]d9a61e12012-11-14 02:43:475183TEST_F(ExtensionServiceTest, ExternalInstallRegistry) {
[email protected]aebe23a32010-12-10 22:15:485184 // This should all work, even when normal extension installation is disabled.
rdevlin.cronin7217be52017-03-24 20:47:055185 InitializeExtensionServiceWithExtensionsDisabled();
[email protected]d55e7602009-12-16 04:20:425186
5187 // Now add providers. Extension system takes ownership of the objects.
lazyboya00eafc2017-04-08 00:57:195188 MockExternalProvider* reg_provider =
lazyboy8a08c9d2017-04-11 19:53:225189 AddMockExternalProvider(Manifest::EXTERNAL_REGISTRY);
[email protected]1d5e58b2013-01-31 08:41:405190 TestExternalProvider(reg_provider, Manifest::EXTERNAL_REGISTRY);
[email protected]d55e7602009-12-16 04:20:425191}
5192#endif
5193
[email protected]d9a61e12012-11-14 02:43:475194TEST_F(ExtensionServiceTest, ExternalInstallPref) {
[email protected]eaa7dd182010-12-14 11:09:005195 InitializeEmptyExtensionService();
[email protected]d55e7602009-12-16 04:20:425196
5197 // Now add providers. Extension system takes ownership of the objects.
lazyboya00eafc2017-04-08 00:57:195198 MockExternalProvider* pref_provider =
lazyboy8a08c9d2017-04-11 19:53:225199 AddMockExternalProvider(Manifest::EXTERNAL_PREF);
[email protected]0a60a2e2010-10-25 16:15:215200
[email protected]1d5e58b2013-01-31 08:41:405201 TestExternalProvider(pref_provider, Manifest::EXTERNAL_PREF);
[email protected]27b985d2009-06-25 17:53:155202}
5203
[email protected]d9a61e12012-11-14 02:43:475204TEST_F(ExtensionServiceTest, ExternalInstallPrefUpdateUrl) {
[email protected]aebe23a32010-12-10 22:15:485205 // This should all work, even when normal extension installation is disabled.
rdevlin.cronin7217be52017-03-24 20:47:055206 InitializeExtensionServiceWithExtensionsDisabled();
[email protected]55196e92010-09-29 15:04:465207
[email protected]0a60a2e2010-10-25 16:15:215208 // TODO(skerner): The mock provider is not a good model of a provider
5209 // that works with update URLs, because it adds file and version info.
5210 // Extend the mock to work with update URLs. This test checks the
5211 // behavior that is common to all external extension visitors. The
5212 // browser test ExtensionManagementTest.ExternalUrlUpdate tests that
5213 // what the visitor does results in an extension being downloaded and
5214 // installed.
lazyboya00eafc2017-04-08 00:57:195215 MockExternalProvider* pref_provider =
lazyboy8a08c9d2017-04-11 19:53:225216 AddMockExternalProvider(Manifest::EXTERNAL_PREF_DOWNLOAD);
[email protected]1d5e58b2013-01-31 08:41:405217 TestExternalProvider(pref_provider, Manifest::EXTERNAL_PREF_DOWNLOAD);
[email protected]55196e92010-09-29 15:04:465218}
5219
[email protected]d9a61e12012-11-14 02:43:475220TEST_F(ExtensionServiceTest, ExternalInstallPolicyUpdateUrl) {
[email protected]95da88c42011-03-31 10:07:335221 // This should all work, even when normal extension installation is disabled.
rdevlin.cronin7217be52017-03-24 20:47:055222 InitializeExtensionServiceWithExtensionsDisabled();
[email protected]95da88c42011-03-31 10:07:335223
5224 // TODO(skerner): The mock provider is not a good model of a provider
5225 // that works with update URLs, because it adds file and version info.
5226 // Extend the mock to work with update URLs. This test checks the
5227 // behavior that is common to all external extension visitors. The
5228 // browser test ExtensionManagementTest.ExternalUrlUpdate tests that
5229 // what the visitor does results in an extension being downloaded and
5230 // installed.
lazyboya00eafc2017-04-08 00:57:195231 MockExternalProvider* pref_provider =
lazyboy8a08c9d2017-04-11 19:53:225232 AddMockExternalProvider(Manifest::EXTERNAL_POLICY_DOWNLOAD);
[email protected]1d5e58b2013-01-31 08:41:405233 TestExternalProvider(pref_provider, Manifest::EXTERNAL_POLICY_DOWNLOAD);
[email protected]95da88c42011-03-31 10:07:335234}
5235
[email protected]aebe23a32010-12-10 22:15:485236// Tests that external extensions get uninstalled when the external extension
5237// providers can't account for them.
[email protected]d9a61e12012-11-14 02:43:475238TEST_F(ExtensionServiceTest, ExternalUninstall) {
[email protected]aebe23a32010-12-10 22:15:485239 // Start the extensions service with one external extension already installed.
[email protected]f484f8d52014-06-12 08:38:185240 base::FilePath source_install_dir =
5241 data_dir().AppendASCII("good").AppendASCII("Extensions");
[email protected]650b2d52013-02-10 03:41:455242 base::FilePath pref_path = source_install_dir
[email protected]aebe23a32010-12-10 22:15:485243 .DirName()
5244 .AppendASCII("PreferencesExternal");
5245
[email protected]eaa7dd182010-12-14 11:09:005246 InitializeInstalledExtensionService(pref_path, source_install_dir);
[email protected]f484f8d52014-06-12 08:38:185247 service()->Init();
[email protected]aebe23a32010-12-10 22:15:485248
5249 ASSERT_EQ(0u, GetErrors().size());
[email protected]1f4728f2012-12-05 20:40:055250 ASSERT_EQ(0u, loaded_.size());
[email protected]aebe23a32010-12-10 22:15:485251}
5252
[email protected]a29a517a2011-01-21 21:11:125253// Test that running multiple update checks simultaneously does not
5254// keep the update from succeeding.
[email protected]d9a61e12012-11-14 02:43:475255TEST_F(ExtensionServiceTest, MultipleExternalUpdateCheck) {
[email protected]a29a517a2011-01-21 21:11:125256 InitializeEmptyExtensionService();
5257
lazyboya00eafc2017-04-08 00:57:195258 MockExternalProvider* provider =
lazyboy8a08c9d2017-04-11 19:53:225259 AddMockExternalProvider(Manifest::EXTERNAL_PREF);
[email protected]a29a517a2011-01-21 21:11:125260
5261 // Verify that starting with no providers loads no extensions.
[email protected]f484f8d52014-06-12 08:38:185262 service()->Init();
[email protected]a29a517a2011-01-21 21:11:125263 ASSERT_EQ(0u, loaded_.size());
5264
5265 // Start two checks for updates.
5266 provider->set_visit_count(0);
[email protected]f484f8d52014-06-12 08:38:185267 service()->CheckForExternalUpdates();
5268 service()->CheckForExternalUpdates();
Gabriel Charette01507a22017-09-27 21:30:085269 content::RunAllTasksUntilIdle();
[email protected]a29a517a2011-01-21 21:11:125270
5271 // Two calls should cause two checks for external extensions.
5272 EXPECT_EQ(2, provider->visit_count());
5273 EXPECT_EQ(0u, GetErrors().size());
5274 EXPECT_EQ(0u, loaded_.size());
5275
5276 // Register a test extension externally using the mock registry provider.
[email protected]f484f8d52014-06-12 08:38:185277 base::FilePath source_path = data_dir().AppendASCII("good.crx");
[email protected]a29a517a2011-01-21 21:11:125278 provider->UpdateOrAddExtension(good_crx, "1.0.0.0", source_path);
5279
5280 // Two checks for external updates should find the extension, and install it
5281 // once.
[email protected]97d6a5c2013-11-11 23:51:245282 content::WindowedNotificationObserver observer(
[email protected]adf5a102014-07-31 12:44:065283 extensions::NOTIFICATION_CRX_INSTALLER_DONE,
[email protected]97d6a5c2013-11-11 23:51:245284 content::NotificationService::AllSources());
[email protected]a29a517a2011-01-21 21:11:125285 provider->set_visit_count(0);
[email protected]f484f8d52014-06-12 08:38:185286 service()->CheckForExternalUpdates();
5287 service()->CheckForExternalUpdates();
[email protected]97d6a5c2013-11-11 23:51:245288 observer.Wait();
[email protected]a29a517a2011-01-21 21:11:125289 EXPECT_EQ(2, provider->visit_count());
5290 ASSERT_EQ(0u, GetErrors().size());
5291 ASSERT_EQ(1u, loaded_.size());
[email protected]1d5e58b2013-01-31 08:41:405292 ASSERT_EQ(Manifest::EXTERNAL_PREF, loaded_[0]->location());
Devlin Cronin03bf2d22017-12-20 08:21:055293 ASSERT_EQ("1.0.0.0", loaded_[0]->version().GetString());
[email protected]a29a517a2011-01-21 21:11:125294 ValidatePrefKeyCount(1);
5295 ValidateIntegerPref(good_crx, "state", Extension::ENABLED);
[email protected]1d5e58b2013-01-31 08:41:405296 ValidateIntegerPref(good_crx, "location", Manifest::EXTERNAL_PREF);
[email protected]a29a517a2011-01-21 21:11:125297
5298 provider->RemoveExtension(good_crx);
5299 provider->set_visit_count(0);
[email protected]f484f8d52014-06-12 08:38:185300 service()->CheckForExternalUpdates();
5301 service()->CheckForExternalUpdates();
Gabriel Charette01507a22017-09-27 21:30:085302 content::RunAllTasksUntilIdle();
[email protected]a29a517a2011-01-21 21:11:125303
5304 // Two calls should cause two checks for external extensions.
5305 // Because the external source no longer includes good_crx,
5306 // good_crx will be uninstalled. So, expect that no extensions
5307 // are loaded.
5308 EXPECT_EQ(2, provider->visit_count());
5309 EXPECT_EQ(0u, GetErrors().size());
5310 EXPECT_EQ(0u, loaded_.size());
5311}
5312
[email protected]d9a61e12012-11-14 02:43:475313TEST_F(ExtensionServiceTest, ExternalPrefProvider) {
[email protected]eaa7dd182010-12-14 11:09:005314 InitializeEmptyExtensionService();
[email protected]f0841cd2011-01-19 15:07:245315
5316 // Test some valid extension records.
5317 // Set a base path to avoid erroring out on relative paths.
5318 // Paths starting with // are absolute on every platform we support.
[email protected]650b2d52013-02-10 03:41:455319 base::FilePath base_path(FILE_PATH_LITERAL("//base/path"));
[email protected]f0841cd2011-01-19 15:07:245320 ASSERT_TRUE(base_path.IsAbsolute());
5321 MockProviderVisitor visitor(base_path);
[email protected]27b985d2009-06-25 17:53:155322 std::string json_data =
5323 "{"
[email protected]f0841cd2011-01-19 15:07:245324 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\": {"
[email protected]9e54cb572010-09-03 20:08:065325 " \"external_crx\": \"RandomExtension.crx\","
5326 " \"external_version\": \"1.0\""
5327 " },"
5328 " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\": {"
5329 " \"external_crx\": \"RandomExtension2.crx\","
5330 " \"external_version\": \"2.0\""
5331 " },"
5332 " \"cccccccccccccccccccccccccccccccc\": {"
[email protected]d8fd0fd2014-03-24 13:16:065333 " \"external_update_url\": \"http:\\\\foo.com/update\","
5334 " \"install_parameter\": \"id\""
[email protected]9e54cb572010-09-03 20:08:065335 " }"
[email protected]27b985d2009-06-25 17:53:155336 "}";
[email protected]f0841cd2011-01-19 15:07:245337 EXPECT_EQ(3, visitor.Visit(json_data));
[email protected]27b985d2009-06-25 17:53:155338
[email protected]9e54cb572010-09-03 20:08:065339 // Simulate an external_extensions.json file that contains seven invalid
[email protected]f0841cd2011-01-19 15:07:245340 // records:
[email protected]27b985d2009-06-25 17:53:155341 // - One that is missing the 'external_crx' key.
5342 // - One that is missing the 'external_version' key.
5343 // - One that is specifying .. in the path.
[email protected]8ef78fd2010-08-19 17:14:325344 // - One that specifies both a file and update URL.
5345 // - One that specifies no file or update URL.
5346 // - One that has an update URL that is not well formed.
[email protected]9e54cb572010-09-03 20:08:065347 // - One that contains a malformed version.
[email protected]ab22ba42011-01-14 16:36:385348 // - One that has an invalid id.
5349 // - One that has a non-dictionary value.
[email protected]0d461c52012-07-03 19:29:415350 // - One that has an integer 'external_version' instead of a string.
[email protected]9e54cb572010-09-03 20:08:065351 // The final extension is valid, and we check that it is read to make sure
5352 // failures don't stop valid records from being read.
[email protected]27b985d2009-06-25 17:53:155353 json_data =
5354 "{"
[email protected]9e54cb572010-09-03 20:08:065355 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\": {"
5356 " \"external_version\": \"1.0\""
5357 " },"
5358 " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\": {"
5359 " \"external_crx\": \"RandomExtension.crx\""
5360 " },"
5361 " \"cccccccccccccccccccccccccccccccc\": {"
5362 " \"external_crx\": \"..\\\\foo\\\\RandomExtension2.crx\","
5363 " \"external_version\": \"2.0\""
5364 " },"
5365 " \"dddddddddddddddddddddddddddddddd\": {"
5366 " \"external_crx\": \"RandomExtension2.crx\","
5367 " \"external_version\": \"2.0\","
5368 " \"external_update_url\": \"http:\\\\foo.com/update\""
5369 " },"
5370 " \"eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee\": {"
5371 " },"
5372 " \"ffffffffffffffffffffffffffffffff\": {"
5373 " \"external_update_url\": \"This string is not a valid URL\""
5374 " },"
5375 " \"gggggggggggggggggggggggggggggggg\": {"
5376 " \"external_crx\": \"RandomExtension3.crx\","
5377 " \"external_version\": \"This is not a valid version!\""
5378 " },"
[email protected]ab22ba42011-01-14 16:36:385379 " \"This is not a valid id!\": {},"
5380 " \"hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh\": true,"
[email protected]0d461c52012-07-03 19:29:415381 " \"iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii\": {"
5382 " \"external_crx\": \"RandomExtension4.crx\","
5383 " \"external_version\": 1.0"
5384 " },"
[email protected]ab22ba42011-01-14 16:36:385385 " \"pppppppppppppppppppppppppppppppp\": {"
[email protected]9e54cb572010-09-03 20:08:065386 " \"external_crx\": \"RandomValidExtension.crx\","
5387 " \"external_version\": \"1.0\""
5388 " }"
[email protected]27b985d2009-06-25 17:53:155389 "}";
[email protected]683d0702010-12-06 16:25:575390 EXPECT_EQ(1, visitor.Visit(json_data));
[email protected]f0841cd2011-01-19 15:07:245391
5392 // Check that if a base path is not provided, use of a relative
5393 // path fails.
[email protected]650b2d52013-02-10 03:41:455394 base::FilePath empty;
[email protected]f0841cd2011-01-19 15:07:245395 MockProviderVisitor visitor_no_relative_paths(empty);
5396
5397 // Use absolute paths. Expect success.
5398 json_data =
5399 "{"
5400 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\": {"
5401 " \"external_crx\": \"//RandomExtension1.crx\","
5402 " \"external_version\": \"3.0\""
5403 " },"
5404 " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\": {"
5405 " \"external_crx\": \"//path/to/RandomExtension2.crx\","
5406 " \"external_version\": \"3.0\""
5407 " }"
5408 "}";
5409 EXPECT_EQ(2, visitor_no_relative_paths.Visit(json_data));
5410
5411 // Use a relative path. Expect that it will error out.
5412 json_data =
5413 "{"
5414 " \"cccccccccccccccccccccccccccccccc\": {"
5415 " \"external_crx\": \"RandomExtension2.crx\","
5416 " \"external_version\": \"3.0\""
5417 " }"
5418 "}";
5419 EXPECT_EQ(0, visitor_no_relative_paths.Visit(json_data));
[email protected]9d32ded072011-10-11 16:31:055420
5421 // Test supported_locales.
5422 json_data =
5423 "{"
5424 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\": {"
5425 " \"external_crx\": \"RandomExtension.crx\","
5426 " \"external_version\": \"1.0\","
5427 " \"supported_locales\": [ \"en\" ]"
5428 " },"
5429 " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\": {"
5430 " \"external_crx\": \"RandomExtension2.crx\","
5431 " \"external_version\": \"2.0\","
5432 " \"supported_locales\": [ \"en-GB\" ]"
5433 " },"
5434 " \"cccccccccccccccccccccccccccccccc\": {"
5435 " \"external_crx\": \"RandomExtension2.crx\","
5436 " \"external_version\": \"3.0\","
5437 " \"supported_locales\": [ \"en_US\", \"fr\" ]"
5438 " }"
5439 "}";
5440 {
5441 ScopedBrowserLocale guard("en-US");
5442 EXPECT_EQ(2, visitor.Visit(json_data));
5443 }
[email protected]f121003b2012-05-04 21:57:475444
[email protected]19eac6d2013-05-30 06:51:035445 // Test keep_if_present.
5446 json_data =
5447 "{"
5448 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\": {"
5449 " \"external_crx\": \"RandomExtension.crx\","
5450 " \"external_version\": \"1.0\","
5451 " \"keep_if_present\": true"
5452 " }"
5453 "}";
5454 {
5455 EXPECT_EQ(0, visitor.Visit(json_data));
5456 }
5457
[email protected]f121003b2012-05-04 21:57:475458 // Test is_bookmark_app.
5459 MockProviderVisitor from_bookmark_visitor(
5460 base_path, Extension::FROM_BOOKMARK);
5461 json_data =
5462 "{"
5463 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\": {"
5464 " \"external_crx\": \"RandomExtension.crx\","
5465 " \"external_version\": \"1.0\","
5466 " \"is_bookmark_app\": true"
5467 " }"
5468 "}";
5469 EXPECT_EQ(1, from_bookmark_visitor.Visit(json_data));
[email protected]7425d7df2012-11-28 14:35:425470
5471 // Test is_from_webstore.
5472 MockProviderVisitor from_webstore_visitor(
5473 base_path, Extension::FROM_WEBSTORE);
5474 json_data =
5475 "{"
5476 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\": {"
5477 " \"external_crx\": \"RandomExtension.crx\","
5478 " \"external_version\": \"1.0\","
5479 " \"is_from_webstore\": true"
5480 " }"
5481 "}";
5482 EXPECT_EQ(1, from_webstore_visitor.Visit(json_data));
[email protected]bf9fd5ae2014-04-09 22:50:065483
5484 // Test was_installed_by_eom.
5485 MockProviderVisitor was_installed_by_eom_visitor(
5486 base_path, Extension::WAS_INSTALLED_BY_OEM);
5487 json_data =
5488 "{"
5489 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\": {"
5490 " \"external_crx\": \"RandomExtension.crx\","
5491 " \"external_version\": \"1.0\","
5492 " \"was_installed_by_oem\": true"
5493 " }"
5494 "}";
5495 EXPECT_EQ(1, was_installed_by_eom_visitor.Visit(json_data));
dpolukhin2c6ef2932015-05-12 16:06:135496
5497 // Test min_profile_created_by_version.
5498 MockProviderVisitor min_profile_created_by_version_visitor(base_path);
5499 json_data =
5500 "{"
5501 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\": {"
5502 " \"external_crx\": \"RandomExtension.crx\","
5503 " \"external_version\": \"1.0\","
5504 " \"min_profile_created_by_version\": \"42.0.0.1\""
5505 " },"
5506 " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\": {"
5507 " \"external_crx\": \"RandomExtension2.crx\","
5508 " \"external_version\": \"1.0\","
5509 " \"min_profile_created_by_version\": \"43.0.0.1\""
5510 " },"
5511 " \"cccccccccccccccccccccccccccccccc\": {"
5512 " \"external_crx\": \"RandomExtension3.crx\","
5513 " \"external_version\": \"3.0\","
5514 " \"min_profile_created_by_version\": \"44.0.0.1\""
5515 " }"
5516 "}";
5517 min_profile_created_by_version_visitor.profile()->GetPrefs()->SetString(
5518 prefs::kProfileCreatedByVersion, "40.0.0.1");
5519 EXPECT_EQ(0, min_profile_created_by_version_visitor.Visit(json_data));
5520 min_profile_created_by_version_visitor.profile()->GetPrefs()->SetString(
5521 prefs::kProfileCreatedByVersion, "43.0.0.1");
5522 EXPECT_EQ(2, min_profile_created_by_version_visitor.Visit(json_data));
5523 min_profile_created_by_version_visitor.profile()->GetPrefs()->SetString(
5524 prefs::kProfileCreatedByVersion, "45.0.0.1");
5525 EXPECT_EQ(3, min_profile_created_by_version_visitor.Visit(json_data));
[email protected]e18236b2009-06-22 21:32:105526}
[email protected]36a784c2009-06-23 06:21:085527
dpolukhin1687ef32015-06-22 11:12:375528TEST_F(ExtensionServiceTest, DoNotInstallForEnterprise) {
5529 InitializeEmptyExtensionService();
5530
5531 const base::FilePath base_path(FILE_PATH_LITERAL("//base/path"));
5532 ASSERT_TRUE(base_path.IsAbsolute());
5533 MockProviderVisitor visitor(base_path);
5534 policy::ProfilePolicyConnector* const connector =
5535 policy::ProfilePolicyConnectorFactory::GetForBrowserContext(
5536 visitor.profile());
5537 connector->OverrideIsManagedForTesting(true);
5538 EXPECT_TRUE(connector->IsManaged());
5539
5540 std::string json_data =
5541 "{"
5542 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\": {"
5543 " \"external_crx\": \"RandomExtension.crx\","
5544 " \"external_version\": \"1.0\","
5545 " \"do_not_install_for_enterprise\": true"
5546 " },"
5547 " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\": {"
5548 " \"external_crx\": \"RandomExtension2.crx\","
5549 " \"external_version\": \"1.0\""
5550 " }"
5551 "}";
5552 EXPECT_EQ(1, visitor.Visit(json_data));
5553}
5554
lazyboye8634172016-01-28 00:10:485555TEST_F(ExtensionServiceTest, IncrementalUpdateThroughRegistry) {
5556 InitializeEmptyExtensionService();
5557
5558 // Test some valid extension records.
5559 // Set a base path to avoid erroring out on relative paths.
5560 // Paths starting with // are absolute on every platform we support.
5561 base::FilePath base_path(FILE_PATH_LITERAL("//base/path"));
5562 ASSERT_TRUE(base_path.IsAbsolute());
5563 MockUpdateProviderVisitor visitor(base_path);
5564 std::string json_data =
5565 "{"
5566 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\": {"
5567 " \"external_crx\": \"RandomExtension.crx\","
5568 " \"external_version\": \"1.0\""
5569 " },"
5570 " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\": {"
5571 " \"external_crx\": \"RandomExtension2.crx\","
5572 " \"external_version\": \"2.0\""
5573 " },"
5574 " \"cccccccccccccccccccccccccccccccc\": {"
5575 " \"external_update_url\": \"http:\\\\foo.com/update\","
5576 " \"install_parameter\": \"id\""
5577 " }"
5578 "}";
5579 EXPECT_EQ(3, visitor.Visit(json_data, Manifest::EXTERNAL_REGISTRY,
5580 Manifest::EXTERNAL_PREF_DOWNLOAD));
5581
5582 // c* removed and d*, e*, f* added, a*, b* existing.
5583 json_data =
5584 "{"
5585 " \"dddddddddddddddddddddddddddddddd\": {"
5586 " \"external_crx\": \"RandomExtension3.crx\","
5587 " \"external_version\": \"1.0\""
5588 " },"
5589 " \"eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee\": {"
5590 " \"external_update_url\": \"http:\\\\foo.com/update\","
5591 " \"install_parameter\": \"id\""
5592 " },"
5593 " \"ffffffffffffffffffffffffffffffff\": {"
5594 " \"external_update_url\": \"http:\\\\bar.com/update\","
5595 " \"install_parameter\": \"id\""
5596 " },"
5597 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\": {"
5598 " \"external_crx\": \"RandomExtension.crx\","
5599 " \"external_version\": \"1.0\""
5600 " },"
5601 " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\": {"
5602 " \"external_crx\": \"RandomExtension2.crx\","
5603 " \"external_version\": \"2.0\""
5604 " }"
5605 "}";
5606
5607 // This will simulate registry loader observing new changes in registry and
5608 // hence will discover new extensions.
5609 visitor.VisitDueToUpdate(json_data);
5610
5611 // UpdateUrl.
5612 EXPECT_EQ(2u, visitor.GetUpdateURLExtensionCount());
5613 EXPECT_TRUE(
5614 visitor.HasSeenUpdateWithUpdateUrl("eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee"));
5615 EXPECT_TRUE(
5616 visitor.HasSeenUpdateWithUpdateUrl("ffffffffffffffffffffffffffffffff"));
5617
5618 // File.
5619 EXPECT_EQ(3u, visitor.GetFileExtensionCount());
5620 EXPECT_TRUE(
5621 visitor.HasSeenUpdateWithFile("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"));
5622 EXPECT_TRUE(
5623 visitor.HasSeenUpdateWithFile("bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"));
5624 EXPECT_TRUE(
5625 visitor.HasSeenUpdateWithFile("dddddddddddddddddddddddddddddddd"));
5626
5627 // Removed extensions.
5628 EXPECT_EQ(1u, visitor.GetRemovedExtensionCount());
5629 EXPECT_TRUE(visitor.HasSeenRemoval("cccccccccccccccccccccccccccccccc"));
5630
5631 // Simulate all 5 extensions being removed.
5632 json_data = "{}";
5633 visitor.VisitDueToUpdate(json_data);
5634 EXPECT_EQ(0u, visitor.GetUpdateURLExtensionCount());
5635 EXPECT_EQ(0u, visitor.GetFileExtensionCount());
5636 EXPECT_EQ(5u, visitor.GetRemovedExtensionCount());
5637}
5638
[email protected]c6d474f82009-12-16 21:11:065639// Test loading good extensions from the profile directory.
[email protected]d9a61e12012-11-14 02:43:475640TEST_F(ExtensionServiceTest, LoadAndRelocalizeExtensions) {
[email protected]6884a802012-08-07 03:55:225641 // Ensure we're testing in "en" and leave global state untouched.
5642 extension_l10n_util::ScopedLocaleForTest testLocale("en");
5643
[email protected]c6d474f82009-12-16 21:11:065644 // Initialize the test dir with a good Preferences/extensions.
[email protected]f484f8d52014-06-12 08:38:185645 base::FilePath source_install_dir = data_dir().AppendASCII("l10n");
[email protected]e96a0602014-02-15 08:27:425646 base::FilePath pref_path =
5647 source_install_dir.Append(chrome::kPreferencesFilename);
[email protected]eaa7dd182010-12-14 11:09:005648 InitializeInstalledExtensionService(pref_path, source_install_dir);
[email protected]c6d474f82009-12-16 21:11:065649
[email protected]f484f8d52014-06-12 08:38:185650 service()->Init();
[email protected]c6d474f82009-12-16 21:11:065651
5652 ASSERT_EQ(3u, loaded_.size());
5653
5654 // This was equal to "sr" on load.
5655 ValidateStringPref(loaded_[0]->id(), keys::kCurrentLocale, "en");
5656
5657 // These are untouched by re-localization.
5658 ValidateStringPref(loaded_[1]->id(), keys::kCurrentLocale, "en");
5659 EXPECT_FALSE(IsPrefExist(loaded_[1]->id(), keys::kCurrentLocale));
5660
5661 // This one starts with Serbian name, and gets re-localized into English.
5662 EXPECT_EQ("My name is simple.", loaded_[0]->name());
5663
5664 // These are untouched by re-localization.
5665 EXPECT_EQ("My name is simple.", loaded_[1]->name());
5666 EXPECT_EQ("no l10n", loaded_[2]->name());
5667}
5668
[email protected]6c2381d2011-10-19 02:52:535669class ExtensionsReadyRecorder : public content::NotificationObserver {
[email protected]f0488f2f2009-07-01 05:25:225670 public:
5671 ExtensionsReadyRecorder() : ready_(false) {
[email protected]adf5a102014-07-31 12:44:065672 registrar_.Add(this,
5673 extensions::NOTIFICATION_EXTENSIONS_READY_DEPRECATED,
[email protected]ad50def52011-10-19 23:17:075674 content::NotificationService::AllSources());
[email protected]f0488f2f2009-07-01 05:25:225675 }
5676
5677 void set_ready(bool value) { ready_ = value; }
5678 bool ready() { return ready_; }
5679
5680 private:
dchengae36a4a2014-10-21 12:36:365681 void Observe(int type,
5682 const content::NotificationSource& source,
5683 const content::NotificationDetails& details) override {
[email protected]432115822011-07-10 15:52:275684 switch (type) {
[email protected]adf5a102014-07-31 12:44:065685 case extensions::NOTIFICATION_EXTENSIONS_READY_DEPRECATED:
[email protected]f0488f2f2009-07-01 05:25:225686 ready_ = true;
5687 break;
5688 default:
5689 NOTREACHED();
5690 }
5691 }
5692
[email protected]6c2381d2011-10-19 02:52:535693 content::NotificationRegistrar registrar_;
[email protected]f0488f2f2009-07-01 05:25:225694 bool ready_;
5695};
5696
[email protected]36a784c2009-06-23 06:21:085697// Test that we get enabled/disabled correctly for all the pref/command-line
[email protected]eaa7dd182010-12-14 11:09:005698// combinations. We don't want to derive from the ExtensionServiceTest class
5699// for this test, so we use ExtensionServiceTestSimple.
[email protected]f0488f2f2009-07-01 05:25:225700//
5701// Also tests that we always fire EXTENSIONS_READY, no matter whether we are
5702// enabled or not.
skyostil0becb332015-04-27 17:59:375703class ExtensionServiceTestSimple : public testing::Test {
5704 content::TestBrowserThreadBundle thread_bundle_;
5705};
5706
5707TEST_F(ExtensionServiceTestSimple, Enabledness) {
[email protected]5a24d2b2012-11-14 20:38:315708 // Make sure the PluginService singleton is destroyed at the end of the test.
[email protected]39fdf5202012-11-14 00:38:175709 base::ShadowingAtExitManager at_exit_manager;
brettw4b461082016-11-19 18:55:165710#if BUILDFLAG(ENABLE_PLUGINS)
[email protected]8f3372122013-07-18 04:34:145711 content::PluginService::GetInstance()->Init();
[email protected]8f3372122013-07-18 04:34:145712#endif
[email protected]5a24d2b2012-11-14 20:38:315713
Devlin Cronin9722a722017-12-16 03:35:105714 extensions::LoadErrorReporter::Init(false); // no noisy errors
[email protected]f0488f2f2009-07-01 05:25:225715 ExtensionsReadyRecorder recorder;
dchengc963c7142016-04-08 03:55:225716 std::unique_ptr<TestingProfile> profile(new TestingProfile());
dchengc963c7142016-04-08 03:55:225717 std::unique_ptr<base::CommandLine> command_line;
[email protected]650b2d52013-02-10 03:41:455718 base::FilePath install_dir = profile->GetPath()
[email protected]836e2982013-05-16 08:07:425719 .AppendASCII(extensions::kInstallDirectoryName);
[email protected]be5a6db2012-11-13 14:39:115720
[email protected]6d60703b2009-08-29 01:29:235721 // By default, we are enabled.
avi3ef9ec9e2014-12-22 22:50:175722 command_line.reset(new base::CommandLine(base::CommandLine::NO_PROGRAM));
[email protected]bd306722012-07-11 20:43:595723 ExtensionService* service = static_cast<extensions::TestExtensionSystem*>(
[email protected]749d59a2012-04-05 00:23:245724 ExtensionSystem::Get(profile.get()))->
[email protected]31d8f5f22012-04-02 15:22:085725 CreateExtensionService(
5726 command_line.get(),
5727 install_dir,
5728 false);
[email protected]6d60703b2009-08-29 01:29:235729 EXPECT_TRUE(service->extensions_enabled());
5730 service->Init();
Gabriel Charette01507a22017-09-27 21:30:085731 content::RunAllTasksUntilIdle();
[email protected]6d60703b2009-08-29 01:29:235732 EXPECT_TRUE(recorder.ready());
5733
5734 // If either the command line or pref is set, we are disabled.
5735 recorder.set_ready(false);
[email protected]aa96d3a2010-08-21 08:45:255736 profile.reset(new TestingProfile());
[email protected]6d60703b2009-08-29 01:29:235737 command_line->AppendSwitch(switches::kDisableExtensions);
[email protected]bd306722012-07-11 20:43:595738 service = static_cast<extensions::TestExtensionSystem*>(
[email protected]749d59a2012-04-05 00:23:245739 ExtensionSystem::Get(profile.get()))->
[email protected]31d8f5f22012-04-02 15:22:085740 CreateExtensionService(
5741 command_line.get(),
5742 install_dir,
5743 false);
[email protected]36a784c2009-06-23 06:21:085744 EXPECT_FALSE(service->extensions_enabled());
[email protected]f0488f2f2009-07-01 05:25:225745 service->Init();
Gabriel Charette01507a22017-09-27 21:30:085746 content::RunAllTasksUntilIdle();
[email protected]f0488f2f2009-07-01 05:25:225747 EXPECT_TRUE(recorder.ready());
[email protected]36a784c2009-06-23 06:21:085748
[email protected]f0488f2f2009-07-01 05:25:225749 recorder.set_ready(false);
[email protected]aa96d3a2010-08-21 08:45:255750 profile.reset(new TestingProfile());
5751 profile->GetPrefs()->SetBoolean(prefs::kDisableExtensions, true);
[email protected]bd306722012-07-11 20:43:595752 service = static_cast<extensions::TestExtensionSystem*>(
[email protected]749d59a2012-04-05 00:23:245753 ExtensionSystem::Get(profile.get()))->
[email protected]31d8f5f22012-04-02 15:22:085754 CreateExtensionService(
5755 command_line.get(),
5756 install_dir,
5757 false);
[email protected]6d60703b2009-08-29 01:29:235758 EXPECT_FALSE(service->extensions_enabled());
[email protected]f0488f2f2009-07-01 05:25:225759 service->Init();
Gabriel Charette01507a22017-09-27 21:30:085760 content::RunAllTasksUntilIdle();
[email protected]f0488f2f2009-07-01 05:25:225761 EXPECT_TRUE(recorder.ready());
[email protected]36a784c2009-06-23 06:21:085762
[email protected]f0488f2f2009-07-01 05:25:225763 recorder.set_ready(false);
[email protected]aa96d3a2010-08-21 08:45:255764 profile.reset(new TestingProfile());
5765 profile->GetPrefs()->SetBoolean(prefs::kDisableExtensions, true);
avi3ef9ec9e2014-12-22 22:50:175766 command_line.reset(new base::CommandLine(base::CommandLine::NO_PROGRAM));
[email protected]bd306722012-07-11 20:43:595767 service = static_cast<extensions::TestExtensionSystem*>(
[email protected]749d59a2012-04-05 00:23:245768 ExtensionSystem::Get(profile.get()))->
[email protected]31d8f5f22012-04-02 15:22:085769 CreateExtensionService(
5770 command_line.get(),
5771 install_dir,
5772 false);
[email protected]6d60703b2009-08-29 01:29:235773 EXPECT_FALSE(service->extensions_enabled());
[email protected]f0488f2f2009-07-01 05:25:225774 service->Init();
Gabriel Charette01507a22017-09-27 21:30:085775 content::RunAllTasksUntilIdle();
[email protected]f0488f2f2009-07-01 05:25:225776 EXPECT_TRUE(recorder.ready());
[email protected]7e1951a2010-09-30 10:22:205777
5778 // Explicitly delete all the resources used in this test.
5779 profile.reset();
5780 service = NULL;
[email protected]060d4972012-07-19 17:22:395781 // Execute any pending deletion tasks.
Gabriel Charette01507a22017-09-27 21:30:085782 content::RunAllTasksUntilIdle();
[email protected]36a784c2009-06-23 06:21:085783}
[email protected]24b538a2010-02-27 01:22:445784
5785// Test loading extensions that require limited and unlimited storage quotas.
[email protected]d9a61e12012-11-14 02:43:475786TEST_F(ExtensionServiceTest, StorageQuota) {
[email protected]eaa7dd182010-12-14 11:09:005787 InitializeEmptyExtensionService();
[email protected]24b538a2010-02-27 01:22:445788
[email protected]f484f8d52014-06-12 08:38:185789 base::FilePath extensions_path = data_dir().AppendASCII("storage_quota");
[email protected]24b538a2010-02-27 01:22:445790
[email protected]650b2d52013-02-10 03:41:455791 base::FilePath limited_quota_ext =
[email protected]e85e34c32011-04-13 18:38:355792 extensions_path.AppendASCII("limited_quota")
[email protected]24b538a2010-02-27 01:22:445793 .AppendASCII("1.0");
[email protected]03b612f2010-08-13 21:09:215794
5795 // The old permission name for unlimited quota was "unlimited_storage", but
5796 // we changed it to "unlimitedStorage". This tests both versions.
[email protected]650b2d52013-02-10 03:41:455797 base::FilePath unlimited_quota_ext =
[email protected]e85e34c32011-04-13 18:38:355798 extensions_path.AppendASCII("unlimited_quota")
[email protected]24b538a2010-02-27 01:22:445799 .AppendASCII("1.0");
[email protected]650b2d52013-02-10 03:41:455800 base::FilePath unlimited_quota_ext2 =
[email protected]e85e34c32011-04-13 18:38:355801 extensions_path.AppendASCII("unlimited_quota")
[email protected]03b612f2010-08-13 21:09:215802 .AppendASCII("2.0");
[email protected]f484f8d52014-06-12 08:38:185803 extensions::UnpackedInstaller::Create(service())->Load(limited_quota_ext);
5804 extensions::UnpackedInstaller::Create(service())->Load(unlimited_quota_ext);
5805 extensions::UnpackedInstaller::Create(service())->Load(unlimited_quota_ext2);
Gabriel Charette01507a22017-09-27 21:30:085806 content::RunAllTasksUntilIdle();
[email protected]24b538a2010-02-27 01:22:445807
[email protected]03b612f2010-08-13 21:09:215808 ASSERT_EQ(3u, loaded_.size());
[email protected]f484f8d52014-06-12 08:38:185809 EXPECT_TRUE(profile());
5810 EXPECT_FALSE(profile()->IsOffTheRecord());
5811 EXPECT_FALSE(
5812 profile()->GetExtensionSpecialStoragePolicy()->IsStorageUnlimited(
5813 loaded_[0]->url()));
5814 EXPECT_TRUE(profile()->GetExtensionSpecialStoragePolicy()->IsStorageUnlimited(
[email protected]7c5f2ec2011-05-26 19:15:265815 loaded_[1]->url()));
[email protected]f484f8d52014-06-12 08:38:185816 EXPECT_TRUE(profile()->GetExtensionSpecialStoragePolicy()->IsStorageUnlimited(
[email protected]7c5f2ec2011-05-26 19:15:265817 loaded_[2]->url()));
[email protected]24b538a2010-02-27 01:22:445818}
[email protected]1952c7d2010-03-04 23:48:345819
[email protected]d8c8f25f2011-11-02 18:18:015820// Tests ComponentLoader::Add().
[email protected]d9a61e12012-11-14 02:43:475821TEST_F(ExtensionServiceTest, ComponentExtensions) {
[email protected]f0b97f12010-10-11 21:44:355822 // Component extensions should work even when extensions are disabled.
rdevlin.cronin7217be52017-03-24 20:47:055823 InitializeExtensionServiceWithExtensionsDisabled();
[email protected]f0b97f12010-10-11 21:44:355824
[email protected]f484f8d52014-06-12 08:38:185825 base::FilePath path = data_dir()
5826 .AppendASCII("good")
5827 .AppendASCII("Extensions")
5828 .AppendASCII("behllobkkfkfnphdnhnkndlbkcpglgmj")
5829 .AppendASCII("1.0.0.0");
[email protected]1952c7d2010-03-04 23:48:345830
5831 std::string manifest;
[email protected]82f84b92013-08-30 18:23:505832 ASSERT_TRUE(base::ReadFileToString(
[email protected]993da5e2013-03-23 21:25:165833 path.Append(extensions::kManifestFilename), &manifest));
[email protected]1952c7d2010-03-04 23:48:345834
[email protected]f484f8d52014-06-12 08:38:185835 service()->component_loader()->Add(manifest, path);
5836 service()->Init();
[email protected]1952c7d2010-03-04 23:48:345837
5838 // Note that we do not pump messages -- the extension should be loaded
5839 // immediately.
5840
5841 EXPECT_EQ(0u, GetErrors().size());
5842 ASSERT_EQ(1u, loaded_.size());
[email protected]1d5e58b2013-01-31 08:41:405843 EXPECT_EQ(Manifest::COMPONENT, loaded_[0]->location());
[email protected]f484f8d52014-06-12 08:38:185844 EXPECT_EQ(1u, registry()->enabled_extensions().size());
[email protected]1952c7d2010-03-04 23:48:345845
[email protected]8c484b742012-11-29 06:05:365846 // Component extensions get a prefs entry on first install.
5847 ValidatePrefKeyCount(1);
[email protected]1952c7d2010-03-04 23:48:345848
5849 // Reload all extensions, and make sure it comes back.
[email protected]f484f8d52014-06-12 08:38:185850 std::string extension_id = (*registry()->enabled_extensions().begin())->id();
[email protected]1952c7d2010-03-04 23:48:345851 loaded_.clear();
[email protected]f484f8d52014-06-12 08:38:185852 service()->ReloadExtensionsForTest();
5853 ASSERT_EQ(1u, registry()->enabled_extensions().size());
5854 EXPECT_EQ(extension_id, (*registry()->enabled_extensions().begin())->id());
[email protected]1952c7d2010-03-04 23:48:345855}
[email protected]145a317b2011-04-12 16:03:465856
[email protected]d9a61e12012-11-14 02:43:475857TEST_F(ExtensionServiceTest, InstallPriorityExternalUpdateUrl) {
[email protected]8a87a5332011-08-11 17:54:595858 InitializeEmptyExtensionService();
5859
[email protected]f484f8d52014-06-12 08:38:185860 base::FilePath path = data_dir().AppendASCII("good.crx");
[email protected]8f512c72011-11-22 21:02:505861 InstallCRX(path, INSTALL_NEW);
[email protected]8a87a5332011-08-11 17:54:595862 ValidatePrefKeyCount(1u);
5863 ValidateIntegerPref(good_crx, "state", Extension::ENABLED);
[email protected]1d5e58b2013-01-31 08:41:405864 ValidateIntegerPref(good_crx, "location", Manifest::INTERNAL);
[email protected]8a87a5332011-08-11 17:54:595865
[email protected]3f213ad2012-07-26 23:39:415866 extensions::PendingExtensionManager* pending =
[email protected]f484f8d52014-06-12 08:38:185867 service()->pending_extension_manager();
[email protected]8a87a5332011-08-11 17:54:595868 EXPECT_FALSE(pending->IsIdPending(kGoodId));
5869
5870 // Skip install when the location is the same.
Devlin Cronind4c2a8f32017-09-29 17:08:305871 GURL good_update_url(kGoodUpdateURL);
Devlin Cronin92c52cac2017-10-02 21:29:015872 ExternalInstallInfoUpdateUrl info(
5873 kGoodId, std::string(), std::move(good_update_url), Manifest::INTERNAL,
5874 Extension::NO_FLAGS, false);
5875 EXPECT_FALSE(service()->OnExternalExtensionUpdateUrlFound(info, true));
[email protected]8a87a5332011-08-11 17:54:595876 EXPECT_FALSE(pending->IsIdPending(kGoodId));
[email protected]9060d8b02012-01-13 02:14:305877
5878 // Install when the location has higher priority.
Devlin Cronin92c52cac2017-10-02 21:29:015879 info.download_location = Manifest::EXTERNAL_POLICY_DOWNLOAD;
5880 EXPECT_TRUE(service()->OnExternalExtensionUpdateUrlFound(info, true));
[email protected]8a87a5332011-08-11 17:54:595881 EXPECT_TRUE(pending->IsIdPending(kGoodId));
[email protected]9060d8b02012-01-13 02:14:305882
5883 // Try the low priority again. Should be rejected.
Devlin Cronin92c52cac2017-10-02 21:29:015884 info.download_location = Manifest::EXTERNAL_PREF_DOWNLOAD;
5885 EXPECT_FALSE(service()->OnExternalExtensionUpdateUrlFound(info, true));
[email protected]9060d8b02012-01-13 02:14:305886 // The existing record should still be present in the pending extension
5887 // manager.
5888 EXPECT_TRUE(pending->IsIdPending(kGoodId));
5889
[email protected]8a87a5332011-08-11 17:54:595890 pending->Remove(kGoodId);
[email protected]9060d8b02012-01-13 02:14:305891
5892 // Skip install when the location has the same priority as the installed
5893 // location.
Devlin Cronin92c52cac2017-10-02 21:29:015894 info.download_location = Manifest::INTERNAL;
5895 EXPECT_FALSE(service()->OnExternalExtensionUpdateUrlFound(info, true));
[email protected]9060d8b02012-01-13 02:14:305896
[email protected]8a87a5332011-08-11 17:54:595897 EXPECT_FALSE(pending->IsIdPending(kGoodId));
5898}
5899
[email protected]d9a61e12012-11-14 02:43:475900TEST_F(ExtensionServiceTest, InstallPriorityExternalLocalFile) {
pwnallcbd73192016-08-22 18:59:175901 base::Version older_version("0.1.0.0");
5902 base::Version newer_version("2.0.0.0");
[email protected]9060d8b02012-01-13 02:14:305903
5904 // We don't want the extension to be installed. A path that doesn't
5905 // point to a valid CRX ensures this.
ginkage553af3202015-02-04 12:39:095906 const base::FilePath kInvalidPathToCrx(FILE_PATH_LITERAL("invalid_path"));
[email protected]9060d8b02012-01-13 02:14:305907
5908 const int kCreationFlags = 0;
5909 const bool kDontMarkAcknowledged = false;
xiyuan4d82f7b62015-03-04 02:29:225910 const bool kDontInstallImmediately = false;
[email protected]9060d8b02012-01-13 02:14:305911
5912 InitializeEmptyExtensionService();
5913
5914 // The test below uses install source constants to test that
5915 // priority is enforced. It assumes a specific ranking of install
5916 // sources: Registry (EXTERNAL_REGISTRY) overrides external pref
5917 // (EXTERNAL_PREF), and external pref overrides user install (INTERNAL).
5918 // The following assertions verify these assumptions:
[email protected]1d5e58b2013-01-31 08:41:405919 ASSERT_EQ(Manifest::EXTERNAL_REGISTRY,
5920 Manifest::GetHigherPriorityLocation(Manifest::EXTERNAL_REGISTRY,
5921 Manifest::EXTERNAL_PREF));
5922 ASSERT_EQ(Manifest::EXTERNAL_REGISTRY,
5923 Manifest::GetHigherPriorityLocation(Manifest::EXTERNAL_REGISTRY,
5924 Manifest::INTERNAL));
5925 ASSERT_EQ(Manifest::EXTERNAL_PREF,
5926 Manifest::GetHigherPriorityLocation(Manifest::EXTERNAL_PREF,
5927 Manifest::INTERNAL));
[email protected]9060d8b02012-01-13 02:14:305928
[email protected]3f213ad2012-07-26 23:39:415929 extensions::PendingExtensionManager* pending =
[email protected]f484f8d52014-06-12 08:38:185930 service()->pending_extension_manager();
[email protected]9060d8b02012-01-13 02:14:305931 EXPECT_FALSE(pending->IsIdPending(kGoodId));
5932
Devlin Cronin92c52cac2017-10-02 21:29:015933 ExternalInstallInfoFile info(kGoodId, older_version, kInvalidPathToCrx,
5934 Manifest::INTERNAL, kCreationFlags,
5935 kDontMarkAcknowledged, kDontInstallImmediately);
[email protected]97d6a5c2013-11-11 23:51:245936 {
5937 // Simulate an external source adding the extension as INTERNAL.
5938 content::WindowedNotificationObserver observer(
[email protected]adf5a102014-07-31 12:44:065939 extensions::NOTIFICATION_CRX_INSTALLER_DONE,
[email protected]97d6a5c2013-11-11 23:51:245940 content::NotificationService::AllSources());
Devlin Cronin92c52cac2017-10-02 21:29:015941 EXPECT_TRUE(service()->OnExternalExtensionFileFound(info));
[email protected]97d6a5c2013-11-11 23:51:245942 EXPECT_TRUE(pending->IsIdPending(kGoodId));
5943 observer.Wait();
5944 VerifyCrxInstall(kInvalidPathToCrx, INSTALL_FAILED);
5945 }
[email protected]9060d8b02012-01-13 02:14:305946
[email protected]97d6a5c2013-11-11 23:51:245947 {
5948 // Simulate an external source adding the extension as EXTERNAL_PREF.
5949 content::WindowedNotificationObserver observer(
[email protected]adf5a102014-07-31 12:44:065950 extensions::NOTIFICATION_CRX_INSTALLER_DONE,
[email protected]97d6a5c2013-11-11 23:51:245951 content::NotificationService::AllSources());
Devlin Cronin92c52cac2017-10-02 21:29:015952 info.crx_location = Manifest::EXTERNAL_PREF;
5953 EXPECT_TRUE(service()->OnExternalExtensionFileFound(info));
[email protected]97d6a5c2013-11-11 23:51:245954 EXPECT_TRUE(pending->IsIdPending(kGoodId));
5955 observer.Wait();
5956 VerifyCrxInstall(kInvalidPathToCrx, INSTALL_FAILED);
5957 }
[email protected]9060d8b02012-01-13 02:14:305958
5959 // Simulate an external source adding as EXTERNAL_PREF again.
[email protected]4ec0f4b2012-12-07 09:41:465960 // This is rejected because the version and the location are the same as
5961 // the previous installation, which is still pending.
Devlin Cronin92c52cac2017-10-02 21:29:015962 EXPECT_FALSE(service()->OnExternalExtensionFileFound(info));
[email protected]9060d8b02012-01-13 02:14:305963 EXPECT_TRUE(pending->IsIdPending(kGoodId));
[email protected]9060d8b02012-01-13 02:14:305964
5965 // Try INTERNAL again. Should fail.
Devlin Cronin92c52cac2017-10-02 21:29:015966 info.crx_location = Manifest::INTERNAL;
5967 EXPECT_FALSE(service()->OnExternalExtensionFileFound(info));
[email protected]9060d8b02012-01-13 02:14:305968 EXPECT_TRUE(pending->IsIdPending(kGoodId));
5969
[email protected]97d6a5c2013-11-11 23:51:245970 {
5971 // Now the registry adds the extension.
5972 content::WindowedNotificationObserver observer(
[email protected]adf5a102014-07-31 12:44:065973 extensions::NOTIFICATION_CRX_INSTALLER_DONE,
[email protected]97d6a5c2013-11-11 23:51:245974 content::NotificationService::AllSources());
Devlin Cronin92c52cac2017-10-02 21:29:015975 info.crx_location = Manifest::EXTERNAL_REGISTRY;
5976 EXPECT_TRUE(service()->OnExternalExtensionFileFound(info));
[email protected]97d6a5c2013-11-11 23:51:245977 EXPECT_TRUE(pending->IsIdPending(kGoodId));
5978 observer.Wait();
5979 VerifyCrxInstall(kInvalidPathToCrx, INSTALL_FAILED);
5980 }
[email protected]9060d8b02012-01-13 02:14:305981
5982 // Registry outranks both external pref and internal, so both fail.
Devlin Cronin92c52cac2017-10-02 21:29:015983 info.crx_location = Manifest::EXTERNAL_PREF;
5984 EXPECT_FALSE(service()->OnExternalExtensionFileFound(info));
[email protected]9060d8b02012-01-13 02:14:305985 EXPECT_TRUE(pending->IsIdPending(kGoodId));
5986
Devlin Cronin92c52cac2017-10-02 21:29:015987 info.crx_location = Manifest::INTERNAL;
5988 EXPECT_FALSE(service()->OnExternalExtensionFileFound(info));
[email protected]9060d8b02012-01-13 02:14:305989 EXPECT_TRUE(pending->IsIdPending(kGoodId));
5990
5991 pending->Remove(kGoodId);
5992
5993 // Install the extension.
[email protected]f484f8d52014-06-12 08:38:185994 base::FilePath path = data_dir().AppendASCII("good.crx");
[email protected]9060d8b02012-01-13 02:14:305995 const Extension* ext = InstallCRX(path, INSTALL_NEW);
5996 ValidatePrefKeyCount(1u);
5997 ValidateIntegerPref(good_crx, "state", Extension::ENABLED);
[email protected]1d5e58b2013-01-31 08:41:405998 ValidateIntegerPref(good_crx, "location", Manifest::INTERNAL);
[email protected]9060d8b02012-01-13 02:14:305999
6000 // Now test the logic of OnExternalExtensionFileFound() when the extension
6001 // being added is already installed.
6002
6003 // Tests assume |older_version| is less than the installed version, and
6004 // |newer_version| is greater. Verify this:
Devlin Cronin03bf2d22017-12-20 08:21:056005 ASSERT_LT(older_version, ext->version());
6006 ASSERT_GT(newer_version, ext->version());
[email protected]9060d8b02012-01-13 02:14:306007
6008 // An external install for the same location should fail if the version is
6009 // older, or the same, and succeed if the version is newer.
6010
6011 // Older than the installed version...
Devlin Cronin92c52cac2017-10-02 21:29:016012 info.version = older_version;
6013 EXPECT_FALSE(service()->OnExternalExtensionFileFound(info));
[email protected]9060d8b02012-01-13 02:14:306014 EXPECT_FALSE(pending->IsIdPending(kGoodId));
6015
6016 // Same version as the installed version...
Devlin Cronin03bf2d22017-12-20 08:21:056017 info.version = ext->version();
Devlin Cronin92c52cac2017-10-02 21:29:016018 EXPECT_FALSE(service()->OnExternalExtensionFileFound(info));
[email protected]9060d8b02012-01-13 02:14:306019 EXPECT_FALSE(pending->IsIdPending(kGoodId));
6020
6021 // Newer than the installed version...
Devlin Cronin92c52cac2017-10-02 21:29:016022 info.version = newer_version;
6023 EXPECT_TRUE(service()->OnExternalExtensionFileFound(info));
[email protected]9060d8b02012-01-13 02:14:306024 EXPECT_TRUE(pending->IsIdPending(kGoodId));
6025
6026 // An external install for a higher priority install source should succeed
6027 // if the version is greater. |older_version| is not...
Devlin Cronin92c52cac2017-10-02 21:29:016028 info.version = older_version;
6029 info.crx_location = Manifest::EXTERNAL_PREF;
6030 EXPECT_FALSE(service()->OnExternalExtensionFileFound(info));
[email protected]9060d8b02012-01-13 02:14:306031 EXPECT_TRUE(pending->IsIdPending(kGoodId));
6032
6033 // |newer_version| is newer.
Devlin Cronin92c52cac2017-10-02 21:29:016034 info.version = newer_version;
6035 EXPECT_TRUE(service()->OnExternalExtensionFileFound(info));
[email protected]9060d8b02012-01-13 02:14:306036 EXPECT_TRUE(pending->IsIdPending(kGoodId));
6037
6038 // An external install for an even higher priority install source should
6039 // succeed if the version is greater.
Devlin Cronin92c52cac2017-10-02 21:29:016040 info.crx_location = Manifest::EXTERNAL_REGISTRY;
6041 EXPECT_TRUE(service()->OnExternalExtensionFileFound(info));
[email protected]9060d8b02012-01-13 02:14:306042 EXPECT_TRUE(pending->IsIdPending(kGoodId));
6043
6044 // Because EXTERNAL_PREF is a lower priority source than EXTERNAL_REGISTRY,
6045 // adding from external pref will now fail.
Devlin Cronin92c52cac2017-10-02 21:29:016046 info.crx_location = Manifest::EXTERNAL_PREF;
6047 EXPECT_FALSE(service()->OnExternalExtensionFileFound(info));
[email protected]9060d8b02012-01-13 02:14:306048 EXPECT_TRUE(pending->IsIdPending(kGoodId));
6049}
6050
[email protected]d9a61e12012-11-14 02:43:476051TEST_F(ExtensionServiceTest, ConcurrentExternalLocalFile) {
pwnallcbd73192016-08-22 18:59:176052 base::Version kVersion123("1.2.3");
6053 base::Version kVersion124("1.2.4");
6054 base::Version kVersion125("1.2.5");
ginkage553af3202015-02-04 12:39:096055 const base::FilePath kInvalidPathToCrx(FILE_PATH_LITERAL("invalid_path"));
[email protected]e3987852012-05-04 10:06:306056 const int kCreationFlags = 0;
6057 const bool kDontMarkAcknowledged = false;
xiyuan4d82f7b62015-03-04 02:29:226058 const bool kDontInstallImmediately = false;
[email protected]e3987852012-05-04 10:06:306059
6060 InitializeEmptyExtensionService();
6061
[email protected]3f213ad2012-07-26 23:39:416062 extensions::PendingExtensionManager* pending =
[email protected]f484f8d52014-06-12 08:38:186063 service()->pending_extension_manager();
[email protected]e3987852012-05-04 10:06:306064 EXPECT_FALSE(pending->IsIdPending(kGoodId));
6065
6066 // An external provider starts installing from a local crx.
Devlin Cronin92c52cac2017-10-02 21:29:016067 ExternalInstallInfoFile info(kGoodId, kVersion123, kInvalidPathToCrx,
6068 Manifest::EXTERNAL_PREF, kCreationFlags,
6069 kDontMarkAcknowledged, kDontInstallImmediately);
6070 EXPECT_TRUE(service()->OnExternalExtensionFileFound(info));
lazyboye8634172016-01-28 00:10:486071
6072 const extensions::PendingExtensionInfo* pending_info;
6073 EXPECT_TRUE((pending_info = pending->GetById(kGoodId)));
6074 EXPECT_TRUE(pending_info->version().IsValid());
6075 EXPECT_EQ(pending_info->version(), kVersion123);
[email protected]e3987852012-05-04 10:06:306076
6077 // Adding a newer version overrides the currently pending version.
Devlin Cronin92c52cac2017-10-02 21:29:016078 info.version = base::Version(kVersion124);
6079 EXPECT_TRUE(service()->OnExternalExtensionFileFound(info));
lazyboye8634172016-01-28 00:10:486080 EXPECT_TRUE((pending_info = pending->GetById(kGoodId)));
6081 EXPECT_TRUE(pending_info->version().IsValid());
6082 EXPECT_EQ(pending_info->version(), kVersion124);
[email protected]e3987852012-05-04 10:06:306083
6084 // Adding an older version fails.
Devlin Cronin92c52cac2017-10-02 21:29:016085 info.version = kVersion123;
6086 EXPECT_FALSE(service()->OnExternalExtensionFileFound(info));
lazyboye8634172016-01-28 00:10:486087 EXPECT_TRUE((pending_info = pending->GetById(kGoodId)));
6088 EXPECT_TRUE(pending_info->version().IsValid());
6089 EXPECT_EQ(pending_info->version(), kVersion124);
[email protected]e3987852012-05-04 10:06:306090
6091 // Adding an older version fails even when coming from a higher-priority
6092 // location.
Devlin Cronin92c52cac2017-10-02 21:29:016093 info.crx_location = Manifest::EXTERNAL_REGISTRY;
6094 EXPECT_FALSE(service()->OnExternalExtensionFileFound(info));
lazyboye8634172016-01-28 00:10:486095 EXPECT_TRUE((pending_info = pending->GetById(kGoodId)));
6096 EXPECT_TRUE(pending_info->version().IsValid());
6097 EXPECT_EQ(pending_info->version(), kVersion124);
[email protected]e3987852012-05-04 10:06:306098
6099 // Adding the latest version from the webstore overrides a specific version.
Devlin Cronin92c52cac2017-10-02 21:29:016100 GURL kUpdateUrl("http://example.com/update");
6101 ExternalInstallInfoUpdateUrl update_info(kGoodId, std::string(), kUpdateUrl,
6102 Manifest::EXTERNAL_POLICY_DOWNLOAD,
6103 Extension::NO_FLAGS, false);
6104 EXPECT_TRUE(service()->OnExternalExtensionUpdateUrlFound(update_info, true));
lazyboye8634172016-01-28 00:10:486105 EXPECT_TRUE((pending_info = pending->GetById(kGoodId)));
6106 EXPECT_FALSE(pending_info->version().IsValid());
[email protected]e3987852012-05-04 10:06:306107}
6108
[email protected]f5bf1842012-02-15 02:52:266109// This makes sure we can package and install CRX files that use whitelisted
6110// permissions.
[email protected]d9a61e12012-11-14 02:43:476111TEST_F(ExtensionServiceTest, InstallWhitelistedExtension) {
[email protected]f5bf1842012-02-15 02:52:266112 std::string test_id = "hdkklepkcpckhnpgjnmbdfhehckloojk";
avi3ef9ec9e2014-12-22 22:50:176113 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
[email protected]a612eb32014-03-31 22:09:176114 extensions::switches::kWhitelistedExtensionID, test_id);
[email protected]f5bf1842012-02-15 02:52:266115
6116 InitializeEmptyExtensionService();
[email protected]f484f8d52014-06-12 08:38:186117 base::FilePath path = data_dir().AppendASCII("permissions");
[email protected]650b2d52013-02-10 03:41:456118 base::FilePath pem_path = path
[email protected]f5bf1842012-02-15 02:52:266119 .AppendASCII("whitelist.pem");
6120 path = path
6121 .AppendASCII("whitelist");
6122
6123 const Extension* extension = PackAndInstallCRX(path, pem_path, INSTALL_NEW);
6124 EXPECT_EQ(0u, GetErrors().size());
[email protected]f484f8d52014-06-12 08:38:186125 ASSERT_EQ(1u, registry()->enabled_extensions().size());
[email protected]f5bf1842012-02-15 02:52:266126 EXPECT_EQ(test_id, extension->id());
6127}
6128
[email protected]145a317b2011-04-12 16:03:466129// Test that when multiple sources try to install an extension,
6130// we consistently choose the right one. To make tests easy to read,
6131// methods that fake requests to install crx files in several ways
6132// are provided.
6133class ExtensionSourcePriorityTest : public ExtensionServiceTest {
6134 public:
dcheng72191812014-10-28 20:49:566135 void SetUp() override {
[email protected]ed8ee722011-04-22 06:49:446136 ExtensionServiceTest::SetUp();
6137
[email protected]145a317b2011-04-12 16:03:466138 // All tests use a single extension. Put the id and path in member vars
6139 // that all methods can read.
6140 crx_id_ = kGoodId;
[email protected]f484f8d52014-06-12 08:38:186141 crx_path_ = data_dir().AppendASCII("good.crx");
[email protected]145a317b2011-04-12 16:03:466142 }
6143
6144 // Fake an external source adding a URL to fetch an extension from.
[email protected]9060d8b02012-01-13 02:14:306145 bool AddPendingExternalPrefUrl() {
[email protected]f484f8d52014-06-12 08:38:186146 return service()->pending_extension_manager()->AddFromExternalUpdateUrl(
[email protected]d8fd0fd2014-03-24 13:16:066147 crx_id_,
6148 std::string(),
6149 GURL(),
6150 Manifest::EXTERNAL_PREF_DOWNLOAD,
6151 Extension::NO_FLAGS,
6152 false);
[email protected]145a317b2011-04-12 16:03:466153 }
6154
6155 // Fake an external file from external_extensions.json.
[email protected]9060d8b02012-01-13 02:14:306156 bool AddPendingExternalPrefFileInstall() {
Devlin Cronin92c52cac2017-10-02 21:29:016157 ExternalInstallInfoFile info(crx_id_, base::Version("1.0.0.0"), crx_path_,
6158 Manifest::EXTERNAL_PREF, Extension::NO_FLAGS,
6159 false, false);
6160 return service()->OnExternalExtensionFileFound(info);
[email protected]145a317b2011-04-12 16:03:466161 }
6162
6163 // Fake a request from sync to install an extension.
6164 bool AddPendingSyncInstall() {
[email protected]f484f8d52014-06-12 08:38:186165 return service()->pending_extension_manager()->AddFromSync(
[email protected]21db9ef2014-05-16 02:06:276166 crx_id_,
6167 GURL(kGoodUpdateURL),
treibe960e282015-09-11 10:38:086168 base::Version(),
[email protected]21db9ef2014-05-16 02:06:276169 &IsExtension,
mamir192d7882016-06-22 17:10:166170 kGoodRemoteInstall);
[email protected]145a317b2011-04-12 16:03:466171 }
6172
[email protected]145a317b2011-04-12 16:03:466173 // Fake a policy install.
[email protected]9060d8b02012-01-13 02:14:306174 bool AddPendingPolicyInstall() {
[email protected]145a317b2011-04-12 16:03:466175 // Get path to the CRX with id |kGoodId|.
Devlin Cronin92c52cac2017-10-02 21:29:016176 ExternalInstallInfoUpdateUrl info(crx_id_, std::string(), GURL(),
6177 Manifest::EXTERNAL_POLICY_DOWNLOAD,
6178 Extension::NO_FLAGS, false);
6179 return service()->OnExternalExtensionUpdateUrlFound(info, true);
[email protected]145a317b2011-04-12 16:03:466180 }
6181
6182 // Get the install source of a pending extension.
[email protected]1d5e58b2013-01-31 08:41:406183 Manifest::Location GetPendingLocation() {
[email protected]3f213ad2012-07-26 23:39:416184 const extensions::PendingExtensionInfo* info;
[email protected]f484f8d52014-06-12 08:38:186185 EXPECT_TRUE(
6186 (info = service()->pending_extension_manager()->GetById(crx_id_)));
[email protected]51a3bf8b2012-06-08 22:53:066187 return info->install_source();
[email protected]145a317b2011-04-12 16:03:466188 }
6189
6190 // Is an extension pending from a sync request?
6191 bool GetPendingIsFromSync() {
[email protected]3f213ad2012-07-26 23:39:416192 const extensions::PendingExtensionInfo* info;
[email protected]f484f8d52014-06-12 08:38:186193 EXPECT_TRUE(
6194 (info = service()->pending_extension_manager()->GetById(crx_id_)));
[email protected]51a3bf8b2012-06-08 22:53:066195 return info->is_from_sync();
[email protected]145a317b2011-04-12 16:03:466196 }
6197
6198 // Is the CRX id these tests use pending?
6199 bool IsCrxPending() {
[email protected]f484f8d52014-06-12 08:38:186200 return service()->pending_extension_manager()->IsIdPending(crx_id_);
[email protected]145a317b2011-04-12 16:03:466201 }
6202
6203 // Is an extension installed?
6204 bool IsCrxInstalled() {
[email protected]f484f8d52014-06-12 08:38:186205 return (service()->GetExtensionById(crx_id_, true) != NULL);
[email protected]145a317b2011-04-12 16:03:466206 }
6207
6208 protected:
6209 // All tests use a single extension. Making the id and path member
6210 // vars avoids pasing the same argument to every method.
6211 std::string crx_id_;
[email protected]650b2d52013-02-10 03:41:456212 base::FilePath crx_path_;
[email protected]145a317b2011-04-12 16:03:466213};
6214
[email protected]3634ebd2011-04-20 00:34:126215// Test that a pending request for installation of an external CRX from
6216// an update URL overrides a pending request to install the same extension
6217// from sync.
[email protected]ed8ee722011-04-22 06:49:446218TEST_F(ExtensionSourcePriorityTest, PendingExternalFileOverSync) {
[email protected]145a317b2011-04-12 16:03:466219 InitializeEmptyExtensionService();
6220
6221 ASSERT_FALSE(IsCrxInstalled());
6222
6223 // Install pending extension from sync.
[email protected]97d6a5c2013-11-11 23:51:246224 content::WindowedNotificationObserver observer(
[email protected]adf5a102014-07-31 12:44:066225 extensions::NOTIFICATION_CRX_INSTALLER_DONE,
[email protected]97d6a5c2013-11-11 23:51:246226 content::NotificationService::AllSources());
[email protected]f4d5e1a2011-04-28 02:08:256227 EXPECT_TRUE(AddPendingSyncInstall());
[email protected]1d5e58b2013-01-31 08:41:406228 ASSERT_EQ(Manifest::INTERNAL, GetPendingLocation());
[email protected]145a317b2011-04-12 16:03:466229 EXPECT_TRUE(GetPendingIsFromSync());
6230 ASSERT_FALSE(IsCrxInstalled());
6231
6232 // Install pending as external prefs json would.
6233 AddPendingExternalPrefFileInstall();
[email protected]1d5e58b2013-01-31 08:41:406234 ASSERT_EQ(Manifest::EXTERNAL_PREF, GetPendingLocation());
[email protected]145a317b2011-04-12 16:03:466235 ASSERT_FALSE(IsCrxInstalled());
6236
[email protected]6d057a0c2013-07-09 21:12:076237 // Another request from sync should be ignored.
[email protected]f4d5e1a2011-04-28 02:08:256238 EXPECT_FALSE(AddPendingSyncInstall());
[email protected]1d5e58b2013-01-31 08:41:406239 ASSERT_EQ(Manifest::EXTERNAL_PREF, GetPendingLocation());
[email protected]145a317b2011-04-12 16:03:466240 ASSERT_FALSE(IsCrxInstalled());
6241
[email protected]97d6a5c2013-11-11 23:51:246242 observer.Wait();
6243 VerifyCrxInstall(crx_path_, INSTALL_NEW);
[email protected]145a317b2011-04-12 16:03:466244 ASSERT_TRUE(IsCrxInstalled());
6245}
6246
6247// Test that an install of an external CRX from an update overrides
6248// an install of the same extension from sync.
[email protected]ed8ee722011-04-22 06:49:446249TEST_F(ExtensionSourcePriorityTest, PendingExternalUrlOverSync) {
[email protected]145a317b2011-04-12 16:03:466250 InitializeEmptyExtensionService();
6251 ASSERT_FALSE(IsCrxInstalled());
6252
[email protected]f4d5e1a2011-04-28 02:08:256253 EXPECT_TRUE(AddPendingSyncInstall());
[email protected]1d5e58b2013-01-31 08:41:406254 ASSERT_EQ(Manifest::INTERNAL, GetPendingLocation());
[email protected]145a317b2011-04-12 16:03:466255 EXPECT_TRUE(GetPendingIsFromSync());
6256 ASSERT_FALSE(IsCrxInstalled());
6257
[email protected]9060d8b02012-01-13 02:14:306258 ASSERT_TRUE(AddPendingExternalPrefUrl());
[email protected]1d5e58b2013-01-31 08:41:406259 ASSERT_EQ(Manifest::EXTERNAL_PREF_DOWNLOAD, GetPendingLocation());
[email protected]145a317b2011-04-12 16:03:466260 EXPECT_FALSE(GetPendingIsFromSync());
6261 ASSERT_FALSE(IsCrxInstalled());
6262
[email protected]f4d5e1a2011-04-28 02:08:256263 EXPECT_FALSE(AddPendingSyncInstall());
[email protected]1d5e58b2013-01-31 08:41:406264 ASSERT_EQ(Manifest::EXTERNAL_PREF_DOWNLOAD, GetPendingLocation());
[email protected]145a317b2011-04-12 16:03:466265 EXPECT_FALSE(GetPendingIsFromSync());
6266 ASSERT_FALSE(IsCrxInstalled());
6267}
6268
[email protected]145a317b2011-04-12 16:03:466269// Test that an external install request stops sync from installing
6270// the same extension.
[email protected]ed8ee722011-04-22 06:49:446271TEST_F(ExtensionSourcePriorityTest, InstallExternalBlocksSyncRequest) {
[email protected]145a317b2011-04-12 16:03:466272 InitializeEmptyExtensionService();
6273 ASSERT_FALSE(IsCrxInstalled());
6274
6275 // External prefs starts an install.
6276 AddPendingExternalPrefFileInstall();
6277
6278 // Crx installer was made, but has not yet run.
6279 ASSERT_FALSE(IsCrxInstalled());
6280
6281 // Before the CRX installer runs, Sync requests that the same extension
6282 // be installed. Should fail, because an external source is pending.
[email protected]97d6a5c2013-11-11 23:51:246283 content::WindowedNotificationObserver observer(
[email protected]adf5a102014-07-31 12:44:066284 extensions::NOTIFICATION_CRX_INSTALLER_DONE,
[email protected]97d6a5c2013-11-11 23:51:246285 content::NotificationService::AllSources());
[email protected]145a317b2011-04-12 16:03:466286 ASSERT_FALSE(AddPendingSyncInstall());
6287
6288 // Wait for the external source to install.
[email protected]97d6a5c2013-11-11 23:51:246289 observer.Wait();
6290 VerifyCrxInstall(crx_path_, INSTALL_NEW);
[email protected]145a317b2011-04-12 16:03:466291 ASSERT_TRUE(IsCrxInstalled());
6292
6293 // Now that the extension is installed, sync request should fail
6294 // because the extension is already installed.
6295 ASSERT_FALSE(AddPendingSyncInstall());
6296}
[email protected]07c9f2f42012-02-29 18:45:226297
[email protected]612a1cb12012-10-17 13:18:036298// Test that installing an external extension displays a GlobalError.
[email protected]d9a61e12012-11-14 02:43:476299TEST_F(ExtensionServiceTest, ExternalInstallGlobalError) {
[email protected]00b5d0a52012-10-30 13:13:536300 FeatureSwitch::ScopedOverride prompt(
6301 FeatureSwitch::prompt_for_external_extensions(), true);
[email protected]612a1cb12012-10-17 13:18:036302
[email protected]07c9f2f42012-02-29 18:45:226303 InitializeEmptyExtensionService();
lazyboya00eafc2017-04-08 00:57:196304 MockExternalProvider* provider =
lazyboy8a08c9d2017-04-11 19:53:226305 AddMockExternalProvider(Manifest::EXTERNAL_PREF);
[email protected]07c9f2f42012-02-29 18:45:226306
[email protected]374ceb6f2014-07-02 19:25:346307 service()->external_install_manager()->UpdateExternalExtensionAlert();
[email protected]07c9f2f42012-02-29 18:45:226308 // Should return false, meaning there aren't any extensions that the user
6309 // needs to know about.
lazyboy0b9b30f2016-01-05 03:15:376310 EXPECT_FALSE(HasExternalInstallErrors(service()));
[email protected]07c9f2f42012-02-29 18:45:226311
6312 // This is a normal extension, installed normally.
6313 // This should NOT trigger an alert.
[email protected]f484f8d52014-06-12 08:38:186314 base::FilePath path = data_dir().AppendASCII("good.crx");
[email protected]07c9f2f42012-02-29 18:45:226315 InstallCRX(path, INSTALL_NEW);
6316
[email protected]f484f8d52014-06-12 08:38:186317 service()->CheckForExternalUpdates();
Gabriel Charette01507a22017-09-27 21:30:086318 content::RunAllTasksUntilIdle();
lazyboy0b9b30f2016-01-05 03:15:376319 EXPECT_FALSE(HasExternalInstallErrors(service()));
[email protected]07c9f2f42012-02-29 18:45:226320
6321 // A hosted app, installed externally.
[email protected]2c495c42013-01-04 21:49:546322 // This should NOT trigger an alert.
[email protected]f484f8d52014-06-12 08:38:186323 provider->UpdateOrAddExtension(
6324 hosted_app, "1.0.0.0", data_dir().AppendASCII("hosted_app.crx"));
[email protected]07c9f2f42012-02-29 18:45:226325
lazyboy8a08c9d2017-04-11 19:53:226326 WaitForExternalExtensionInstalled();
lazyboy0b9b30f2016-01-05 03:15:376327 EXPECT_FALSE(HasExternalInstallErrors(service()));
[email protected]07c9f2f42012-02-29 18:45:226328
[email protected]612a1cb12012-10-17 13:18:036329 // Another normal extension, but installed externally.
6330 // This SHOULD trigger an alert.
[email protected]f484f8d52014-06-12 08:38:186331 provider->UpdateOrAddExtension(
6332 page_action, "1.0.0.0", data_dir().AppendASCII("page_action.crx"));
[email protected]612a1cb12012-10-17 13:18:036333
lazyboy8a08c9d2017-04-11 19:53:226334 WaitForExternalExtensionInstalled();
lazyboy0b9b30f2016-01-05 03:15:376335 EXPECT_TRUE(HasExternalInstallErrors(service()));
[email protected]07c9f2f42012-02-29 18:45:226336}
[email protected]612a1cb12012-10-17 13:18:036337
6338// Test that external extensions are initially disabled, and that enabling
6339// them clears the prompt.
[email protected]d9a61e12012-11-14 02:43:476340TEST_F(ExtensionServiceTest, ExternalInstallInitiallyDisabled) {
[email protected]00b5d0a52012-10-30 13:13:536341 FeatureSwitch::ScopedOverride prompt(
6342 FeatureSwitch::prompt_for_external_extensions(), true);
[email protected]612a1cb12012-10-17 13:18:036343
6344 InitializeEmptyExtensionService();
lazyboya00eafc2017-04-08 00:57:196345 MockExternalProvider* provider =
lazyboy8a08c9d2017-04-11 19:53:226346 AddMockExternalProvider(Manifest::EXTERNAL_PREF);
[email protected]612a1cb12012-10-17 13:18:036347
[email protected]f484f8d52014-06-12 08:38:186348 provider->UpdateOrAddExtension(
6349 page_action, "1.0.0.0", data_dir().AppendASCII("page_action.crx"));
lazyboy8a08c9d2017-04-11 19:53:226350 WaitForExternalExtensionInstalled();
[email protected]612a1cb12012-10-17 13:18:036351
lazyboy0b9b30f2016-01-05 03:15:376352 EXPECT_TRUE(HasExternalInstallErrors(service()));
[email protected]f484f8d52014-06-12 08:38:186353 EXPECT_FALSE(service()->IsExtensionEnabled(page_action));
[email protected]612a1cb12012-10-17 13:18:036354
6355 const Extension* extension =
[email protected]f484f8d52014-06-12 08:38:186356 registry()->disabled_extensions().GetByID(page_action);
[email protected]612a1cb12012-10-17 13:18:036357 EXPECT_TRUE(extension);
6358 EXPECT_EQ(page_action, extension->id());
6359
[email protected]f484f8d52014-06-12 08:38:186360 service()->EnableExtension(page_action);
lazyboy0b9b30f2016-01-05 03:15:376361 EXPECT_FALSE(HasExternalInstallErrors(service()));
[email protected]f484f8d52014-06-12 08:38:186362 EXPECT_TRUE(service()->IsExtensionEnabled(page_action));
[email protected]612a1cb12012-10-17 13:18:036363}
6364
mtomasz622f70d22017-01-27 03:36:266365// As for components, only external component extensions can be disabled.
6366TEST_F(ExtensionServiceTest, DisablingComponentExtensions) {
6367 InitializeEmptyExtensionService();
6368 service_->Init();
6369
6370 scoped_refptr<Extension> external_component_extension = CreateExtension(
6371 base::ASCIIToUTF16("external_component_extension"),
6372 base::FilePath(FILE_PATH_LITERAL("//external_component_extension")),
6373 Manifest::EXTERNAL_COMPONENT);
6374 service_->AddExtension(external_component_extension.get());
6375 EXPECT_TRUE(registry()->enabled_extensions().Contains(
6376 external_component_extension->id()));
6377 service_->DisableExtension(external_component_extension->id(),
Minh X. Nguyen45479012017-08-18 21:35:366378 extensions::disable_reason::DISABLE_USER_ACTION);
mtomasz622f70d22017-01-27 03:36:266379 EXPECT_TRUE(registry()->disabled_extensions().Contains(
6380 external_component_extension->id()));
6381
6382 scoped_refptr<Extension> component_extension = CreateExtension(
6383 base::ASCIIToUTF16("component_extension"),
6384 base::FilePath(FILE_PATH_LITERAL("//component_extension")),
6385 Manifest::COMPONENT);
6386 service_->AddExtension(component_extension.get());
6387 EXPECT_TRUE(
6388 registry()->enabled_extensions().Contains(component_extension->id()));
6389 service_->DisableExtension(component_extension->id(),
Minh X. Nguyen45479012017-08-18 21:35:366390 extensions::disable_reason::DISABLE_USER_ACTION);
mtomasz622f70d22017-01-27 03:36:266391 EXPECT_FALSE(
6392 registry()->disabled_extensions().Contains(component_extension->id()));
6393}
6394
[email protected]612a1cb12012-10-17 13:18:036395// Test that installing multiple external extensions works.
[email protected]404d1a22013-09-20 17:28:076396// Flaky on windows; http://crbug.com/295757 .
jyasskin3220c322015-08-10 22:19:066397// Causes race conditions with an in-process utility thread, so disable under
6398// TSan: https://crbug.com/518957
6399#if defined(OS_WIN) || defined(THREAD_SANITIZER)
[email protected]404d1a22013-09-20 17:28:076400#define MAYBE_ExternalInstallMultiple DISABLED_ExternalInstallMultiple
6401#else
6402#define MAYBE_ExternalInstallMultiple ExternalInstallMultiple
6403#endif
6404TEST_F(ExtensionServiceTest, MAYBE_ExternalInstallMultiple) {
[email protected]00b5d0a52012-10-30 13:13:536405 FeatureSwitch::ScopedOverride prompt(
6406 FeatureSwitch::prompt_for_external_extensions(), true);
[email protected]612a1cb12012-10-17 13:18:036407
6408 InitializeEmptyExtensionService();
lazyboya00eafc2017-04-08 00:57:196409 MockExternalProvider* provider =
lazyboy8a08c9d2017-04-11 19:53:226410 AddMockExternalProvider(Manifest::EXTERNAL_PREF);
[email protected]612a1cb12012-10-17 13:18:036411
[email protected]f484f8d52014-06-12 08:38:186412 provider->UpdateOrAddExtension(
6413 page_action, "1.0.0.0", data_dir().AppendASCII("page_action.crx"));
6414 provider->UpdateOrAddExtension(
6415 good_crx, "1.0.0.0", data_dir().AppendASCII("good.crx"));
6416 provider->UpdateOrAddExtension(
6417 theme_crx, "2.0", data_dir().AppendASCII("theme.crx"));
[email protected]612a1cb12012-10-17 13:18:036418
[email protected]6d057a0c2013-07-09 21:12:076419 int count = 3;
[email protected]97d6a5c2013-11-11 23:51:246420 content::WindowedNotificationObserver observer(
[email protected]adf5a102014-07-31 12:44:066421 extensions::NOTIFICATION_CRX_INSTALLER_DONE,
[email protected]97d6a5c2013-11-11 23:51:246422 base::Bind(&WaitForCountNotificationsCallback, &count));
[email protected]f484f8d52014-06-12 08:38:186423 service()->CheckForExternalUpdates();
[email protected]97d6a5c2013-11-11 23:51:246424 observer.Wait();
lazyboy0b9b30f2016-01-05 03:15:376425 EXPECT_TRUE(HasExternalInstallErrors(service()));
[email protected]f484f8d52014-06-12 08:38:186426 EXPECT_FALSE(service()->IsExtensionEnabled(page_action));
6427 EXPECT_FALSE(service()->IsExtensionEnabled(good_crx));
6428 EXPECT_FALSE(service()->IsExtensionEnabled(theme_crx));
[email protected]612a1cb12012-10-17 13:18:036429
[email protected]f484f8d52014-06-12 08:38:186430 service()->EnableExtension(page_action);
lazyboy0b9b30f2016-01-05 03:15:376431 EXPECT_FALSE(GetError(page_action));
6432 EXPECT_TRUE(GetError(good_crx));
6433 EXPECT_TRUE(GetError(theme_crx));
6434 EXPECT_TRUE(HasExternalInstallErrors(service()));
6435 EXPECT_FALSE(HasExternalInstallBubble(service()));
[email protected]2894a512014-06-26 19:03:566436
[email protected]f484f8d52014-06-12 08:38:186437 service()->EnableExtension(theme_crx);
lazyboy0b9b30f2016-01-05 03:15:376438 EXPECT_FALSE(GetError(page_action));
6439 EXPECT_FALSE(GetError(theme_crx));
6440 EXPECT_TRUE(GetError(good_crx));
6441 EXPECT_TRUE(HasExternalInstallErrors(service()));
6442 EXPECT_FALSE(HasExternalInstallBubble(service()));
[email protected]2894a512014-06-26 19:03:566443
[email protected]f484f8d52014-06-12 08:38:186444 service()->EnableExtension(good_crx);
lazyboy0b9b30f2016-01-05 03:15:376445 EXPECT_FALSE(GetError(page_action));
6446 EXPECT_FALSE(GetError(good_crx));
6447 EXPECT_FALSE(GetError(theme_crx));
6448 EXPECT_FALSE(HasExternalInstallErrors(service()));
6449 EXPECT_FALSE(HasExternalInstallBubble(service()));
6450}
6451
6452TEST_F(ExtensionServiceTest, MultipleExternalInstallErrors) {
6453 FeatureSwitch::ScopedOverride prompt(
6454 FeatureSwitch::prompt_for_external_extensions(), true);
6455 InitializeEmptyExtensionService();
lazyboy0b9b30f2016-01-05 03:15:376456
lazyboya00eafc2017-04-08 00:57:196457 MockExternalProvider* reg_provider =
lazyboy8a08c9d2017-04-11 19:53:226458 AddMockExternalProvider(Manifest::EXTERNAL_REGISTRY);
lazyboy0b9b30f2016-01-05 03:15:376459
6460 std::string extension_info[][3] = {
6461 // {id, path, version}
6462 {good_crx, "1.0.0.0", "good.crx"},
6463 {page_action, "1.0.0.0", "page_action.crx"},
6464 {minimal_platform_app_crx, "0.1", "minimal_platform_app.crx"}};
6465
6466 for (size_t i = 0; i < arraysize(extension_info); ++i) {
6467 reg_provider->UpdateOrAddExtension(
6468 extension_info[i][0], extension_info[i][1],
6469 data_dir().AppendASCII(extension_info[i][2]));
lazyboy8a08c9d2017-04-11 19:53:226470 WaitForExternalExtensionInstalled();
lazyboy0b9b30f2016-01-05 03:15:376471 const size_t expected_error_count = i + 1u;
6472 EXPECT_EQ(
6473 expected_error_count,
6474 service()->external_install_manager()->GetErrorsForTesting().size());
6475 EXPECT_FALSE(service()->IsExtensionEnabled(extension_info[i][0]));
6476 }
6477
6478 std::string extension_ids[] = {
6479 extension_info[0][0], extension_info[1][0], extension_info[2][0]
6480 };
6481
6482 // Each extension should end up in error.
6483 ASSERT_TRUE(GetError(extension_ids[0]));
6484 EXPECT_TRUE(GetError(extension_ids[1]));
6485 EXPECT_TRUE(GetError(extension_ids[2]));
6486
6487 // Accept the first extension, this will remove the error associated with
6488 // this extension. Also verify the other errors still exist.
rdevlin.cronin41593052016-01-08 01:40:126489 GetError(extension_ids[0])->OnInstallPromptDone(
6490 ExtensionInstallPrompt::Result::ACCEPTED);
lazyboy0b9b30f2016-01-05 03:15:376491 EXPECT_FALSE(GetError(extension_ids[0]));
6492 ASSERT_TRUE(GetError(extension_ids[1]));
6493 EXPECT_TRUE(GetError(extension_ids[2]));
6494
6495 // Abort the second extension.
rdevlin.cronin41593052016-01-08 01:40:126496 GetError(extension_ids[1])->OnInstallPromptDone(
6497 ExtensionInstallPrompt::Result::USER_CANCELED);
lazyboy0b9b30f2016-01-05 03:15:376498 EXPECT_FALSE(GetError(extension_ids[0]));
6499 EXPECT_FALSE(GetError(extension_ids[1]));
6500 ASSERT_TRUE(GetError(extension_ids[2]));
6501
6502 // Finally, re-enable the third extension, all errors should be removed.
6503 service()->EnableExtension(extension_ids[2]);
6504 EXPECT_FALSE(GetError(extension_ids[0]));
6505 EXPECT_FALSE(GetError(extension_ids[1]));
6506 EXPECT_FALSE(GetError(extension_ids[2]));
6507
6508 EXPECT_FALSE(HasExternalInstallErrors(service_));
[email protected]aa55be7f2013-04-22 20:56:046509}
6510
Karan Bhatiae9d5166f2017-07-17 22:06:436511// Regression test for crbug.com/739142. Verifies that no UAF occurs when
6512// ExternalInstallError needs to be deleted asynchronously.
6513TEST_F(ExtensionServiceTest, InstallPromptAborted) {
6514 FeatureSwitch::ScopedOverride prompt(
6515 FeatureSwitch::prompt_for_external_extensions(), true);
6516 InitializeEmptyExtensionService();
6517
6518 MockExternalProvider* reg_provider =
6519 AddMockExternalProvider(Manifest::EXTERNAL_REGISTRY);
6520
6521 reg_provider->UpdateOrAddExtension(good_crx, "1.0.0.0",
6522 data_dir().AppendASCII("good.crx"));
6523 WaitForExternalExtensionInstalled();
6524 EXPECT_EQ(
6525 1u, service()->external_install_manager()->GetErrorsForTesting().size());
6526 EXPECT_FALSE(service()->IsExtensionEnabled(good_crx));
6527 EXPECT_TRUE(GetError(good_crx));
6528
6529 // Abort the extension install prompt. This should cause the
6530 // ExternalInstallError to be deleted asynchronously.
6531 GetError(good_crx)->OnInstallPromptDone(
6532 ExtensionInstallPrompt::Result::ABORTED);
6533 EXPECT_TRUE(GetError(good_crx));
6534 base::RunLoop().RunUntilIdle();
6535 EXPECT_FALSE(GetError(good_crx));
6536
6537 EXPECT_FALSE(HasExternalInstallErrors(service_));
6538}
6539
lazyboy1899eec42016-03-08 19:00:506540TEST_F(ExtensionServiceTest, MultipleExternalInstallBubbleErrors) {
6541 FeatureSwitch::ScopedOverride prompt(
6542 FeatureSwitch::prompt_for_external_extensions(), true);
6543 // This sets up the ExtensionPrefs used by our ExtensionService to be
6544 // post-first run.
6545 ExtensionServiceInitParams params = CreateDefaultInitParams();
6546 params.is_first_run = false;
6547 InitializeExtensionService(params);
6548
lazyboya00eafc2017-04-08 00:57:196549 MockExternalProvider* provider =
lazyboy8a08c9d2017-04-11 19:53:226550 AddMockExternalProvider(Manifest::EXTERNAL_PREF);
lazyboy1899eec42016-03-08 19:00:506551
6552 std::vector<BubbleErrorsTestData> data;
vabr9142fe22016-09-08 13:19:226553 data.push_back(BubbleErrorsTestData(
6554 updates_from_webstore, "1",
6555 temp_dir().GetPath().AppendASCII("webstore.crx"), 1u));
6556 data.push_back(BubbleErrorsTestData(
6557 updates_from_webstore2, "1",
6558 temp_dir().GetPath().AppendASCII("webstore2.crx"), 2u));
lazyboy1899eec42016-03-08 19:00:506559 data.push_back(BubbleErrorsTestData(good_crx, "1.0.0.0",
6560 data_dir().AppendASCII("good.crx"), 2u));
6561
6562 PackCRX(data_dir().AppendASCII("update_from_webstore"),
6563 data_dir().AppendASCII("update_from_webstore.pem"), data[0].crx_path);
6564 PackCRX(data_dir().AppendASCII("update_from_webstore2"),
6565 data_dir().AppendASCII("update_from_webstore2.pem"),
6566 data[1].crx_path);
6567
6568 // Install extensions from |data| one by one and expect each of them to result
6569 // in an error. The first two extensions are from webstore, so they will
6570 // trigger BUBBLE_ALERT type errors. After each step, we verify that we got
6571 // the expected number of errors in external_install_manager(). We also verify
6572 // that only the first BUBBLE_ALERT error is shown.
6573 for (size_t i = 0; i < data.size(); ++i) {
6574 content::WindowedNotificationObserver global_error_observer(
6575 chrome::NOTIFICATION_GLOBAL_ERRORS_CHANGED,
6576 content::NotificationService::AllSources());
lazyboy1899eec42016-03-08 19:00:506577 provider->UpdateOrAddExtension(data[i].id, data[i].version,
6578 data[i].crx_path);
lazyboy8a08c9d2017-04-11 19:53:226579 WaitForExternalExtensionInstalled();
lazyboy1899eec42016-03-08 19:00:506580 // Make sure ExternalInstallError::OnDialogReady() fires.
6581 global_error_observer.Wait();
6582
6583 const size_t expected_error_count = i + 1u;
6584 std::vector<ExternalInstallError*> errors =
6585 service_->external_install_manager()->GetErrorsForTesting();
6586 EXPECT_EQ(expected_error_count, errors.size());
6587 EXPECT_EQ(data[i].expected_bubble_error_count,
6588 GetExternalInstallBubbleCount(service()));
6589 EXPECT_TRUE(service()
6590 ->external_install_manager()
6591 ->has_currently_visible_install_alert());
6592 // Make sure that the first error is only being shown.
6593 EXPECT_EQ(errors[0], service()
6594 ->external_install_manager()
6595 ->currently_visible_install_alert_for_testing());
6596 EXPECT_FALSE(service()->IsExtensionEnabled(data[i].id));
6597 }
6598
6599 // Cancel all the install prompts.
6600 for (size_t i = 0; i < data.size(); ++i) {
6601 const std::string& extension_id = data[i].id;
6602 EXPECT_TRUE(GetError(extension_id));
6603 GetError(extension_id)
6604 ->OnInstallPromptDone(ExtensionInstallPrompt::Result::USER_CANCELED);
6605 EXPECT_FALSE(GetError(extension_id));
6606 }
6607 EXPECT_FALSE(service()
6608 ->external_install_manager()
6609 ->has_currently_visible_install_alert());
6610 EXPECT_EQ(0u, GetExternalInstallBubbleCount(service()));
6611 EXPECT_FALSE(HasExternalInstallErrors(service()));
6612
6613 // Add a new webstore install. Verify that this shows an error bubble since
6614 // there are no error bubbles pending at this point. Also verify that the
6615 // error bubble is for this newly added extension.
6616 {
6617 base::FilePath webstore_crx_three =
vabr9142fe22016-09-08 13:19:226618 temp_dir().GetPath().AppendASCII("webstore3.crx");
lazyboy1899eec42016-03-08 19:00:506619 PackCRX(data_dir().AppendASCII("update_from_webstore3"),
6620 data_dir().AppendASCII("update_from_webstore3.pem"),
6621 webstore_crx_three);
6622
6623 content::WindowedNotificationObserver global_error_observer(
6624 chrome::NOTIFICATION_GLOBAL_ERRORS_CHANGED,
6625 content::NotificationService::AllSources());
lazyboy1899eec42016-03-08 19:00:506626 provider->UpdateOrAddExtension(
6627 updates_from_webstore3, "1",
vabr9142fe22016-09-08 13:19:226628 temp_dir().GetPath().AppendASCII("webstore3.crx"));
lazyboy8a08c9d2017-04-11 19:53:226629 WaitForExternalExtensionInstalled();
lazyboy1899eec42016-03-08 19:00:506630 // Make sure ExternalInstallError::OnDialogReady() fires.
6631 global_error_observer.Wait();
6632
6633 std::vector<ExternalInstallError*> errors =
6634 service_->external_install_manager()->GetErrorsForTesting();
6635 EXPECT_EQ(1u, errors.size());
6636 EXPECT_EQ(1u, GetExternalInstallBubbleCount(service()));
6637 EXPECT_TRUE(service()
6638 ->external_install_manager()
6639 ->has_currently_visible_install_alert());
6640 // Verify that the visible alert is for the current error.
6641 EXPECT_EQ(errors[0], service()
6642 ->external_install_manager()
6643 ->currently_visible_install_alert_for_testing());
6644 EXPECT_FALSE(service()->IsExtensionEnabled(updates_from_webstore3));
6645 }
6646}
6647
6648// Verifies that an error alert of type BUBBLE_ALERT does not replace an
6649// existing visible alert that was previously opened by clicking menu item.
6650TEST_F(ExtensionServiceTest, BubbleAlertDoesNotHideAnotherAlertFromMenu) {
6651 FeatureSwitch::ScopedOverride prompt(
6652 FeatureSwitch::prompt_for_external_extensions(), true);
6653 // This sets up the ExtensionPrefs used by our ExtensionService to be
6654 // post-first run.
6655 ExtensionServiceInitParams params = CreateDefaultInitParams();
6656 params.is_first_run = false;
6657 InitializeExtensionService(params);
6658
lazyboya00eafc2017-04-08 00:57:196659 MockExternalProvider* provider =
lazyboy8a08c9d2017-04-11 19:53:226660 AddMockExternalProvider(Manifest::EXTERNAL_PREF);
lazyboy1899eec42016-03-08 19:00:506661
6662 std::vector<BubbleErrorsTestData> data;
vabr9142fe22016-09-08 13:19:226663 data.push_back(BubbleErrorsTestData(
6664 updates_from_webstore, "1",
6665 temp_dir().GetPath().AppendASCII("webstore.crx"), 1u));
6666 data.push_back(BubbleErrorsTestData(
6667 updates_from_webstore2, "1",
6668 temp_dir().GetPath().AppendASCII("webstore2.crx"), 2u));
lazyboy1899eec42016-03-08 19:00:506669
6670 PackCRX(data_dir().AppendASCII("update_from_webstore"),
6671 data_dir().AppendASCII("update_from_webstore.pem"), data[0].crx_path);
6672 PackCRX(data_dir().AppendASCII("update_from_webstore2"),
6673 data_dir().AppendASCII("update_from_webstore2.pem"),
6674 data[1].crx_path);
6675 {
6676 content::WindowedNotificationObserver global_error_observer(
6677 chrome::NOTIFICATION_GLOBAL_ERRORS_CHANGED,
6678 content::NotificationService::AllSources());
lazyboy1899eec42016-03-08 19:00:506679 provider->UpdateOrAddExtension(data[0].id, data[0].version,
6680 data[0].crx_path);
lazyboy8a08c9d2017-04-11 19:53:226681 WaitForExternalExtensionInstalled();
lazyboy1899eec42016-03-08 19:00:506682 // Make sure ExternalInstallError::OnDialogReady() fires.
6683 global_error_observer.Wait();
6684
6685 std::vector<ExternalInstallError*> errors =
6686 service_->external_install_manager()->GetErrorsForTesting();
6687 EXPECT_EQ(1u, errors.size());
6688 EXPECT_EQ(1u, GetExternalInstallBubbleCount(service()));
6689 EXPECT_TRUE(service()
6690 ->external_install_manager()
6691 ->has_currently_visible_install_alert());
6692 // Verify that the visible alert is for the current error.
6693 EXPECT_EQ(errors[0], service()
6694 ->external_install_manager()
6695 ->currently_visible_install_alert_for_testing());
6696 }
6697
6698 ExternalInstallError* first_extension_error = GetError(data[0].id);
6699
6700 // Close the bubble alert.
6701 GlobalError* global_error =
6702 GlobalErrorServiceFactory::GetForProfile(profile())
6703 ->GetHighestSeverityGlobalErrorWithAppMenuItem();
6704 first_extension_error->DidCloseBubbleView();
6705
6706 // Bring the bubble alert error again by clicking its menu item.
6707 global_error->ExecuteMenuItem(nullptr);
6708
6709 // Install another webstore extension that will trigger an erorr of type
6710 // BUBBLE_ALERT.
6711 // Make sure that this bubble alert does not replace the current bubble alert.
6712 {
6713 content::WindowedNotificationObserver global_error_observer(
6714 chrome::NOTIFICATION_GLOBAL_ERRORS_CHANGED,
6715 content::NotificationService::AllSources());
lazyboy1899eec42016-03-08 19:00:506716 provider->UpdateOrAddExtension(data[1].id, data[1].version,
6717 data[1].crx_path);
lazyboy8a08c9d2017-04-11 19:53:226718 WaitForExternalExtensionInstalled();
lazyboy1899eec42016-03-08 19:00:506719 // Make sure ExternalInstallError::OnDialogReady() fires.
6720 global_error_observer.Wait();
6721
6722 std::vector<ExternalInstallError*> errors =
6723 service_->external_install_manager()->GetErrorsForTesting();
6724 EXPECT_EQ(2u, errors.size());
6725 EXPECT_EQ(2u, GetExternalInstallBubbleCount(service()));
6726 EXPECT_TRUE(service()
6727 ->external_install_manager()
6728 ->has_currently_visible_install_alert());
6729 // Verify that the old bubble alert was *not* replaced by the new alert.
6730 EXPECT_EQ(first_extension_error,
6731 service()
6732 ->external_install_manager()
6733 ->currently_visible_install_alert_for_testing());
6734 }
6735}
6736
[email protected]aa55be7f2013-04-22 20:56:046737// Test that there is a bubble for external extensions that update
[email protected]b3aa7182013-04-25 04:45:236738// from the webstore if the profile is not new.
6739TEST_F(ExtensionServiceTest, ExternalInstallUpdatesFromWebstoreOldProfile) {
[email protected]aa55be7f2013-04-22 20:56:046740 FeatureSwitch::ScopedOverride prompt(
6741 FeatureSwitch::prompt_for_external_extensions(), true);
6742
[email protected]b3aa7182013-04-25 04:45:236743 // This sets up the ExtensionPrefs used by our ExtensionService to be
6744 // post-first run.
[email protected]371662e372013-10-17 22:05:226745 ExtensionServiceInitParams params = CreateDefaultInitParams();
6746 params.is_first_run = false;
6747 InitializeExtensionService(params);
[email protected]aa55be7f2013-04-22 20:56:046748
vabr9142fe22016-09-08 13:19:226749 base::FilePath crx_path = temp_dir().GetPath().AppendASCII("webstore.crx");
[email protected]f484f8d52014-06-12 08:38:186750 PackCRX(data_dir().AppendASCII("update_from_webstore"),
6751 data_dir().AppendASCII("update_from_webstore.pem"),
[email protected]aa55be7f2013-04-22 20:56:046752 crx_path);
6753
lazyboya00eafc2017-04-08 00:57:196754 MockExternalProvider* provider =
lazyboy8a08c9d2017-04-11 19:53:226755 AddMockExternalProvider(Manifest::EXTERNAL_PREF);
[email protected]aa55be7f2013-04-22 20:56:046756 provider->UpdateOrAddExtension(updates_from_webstore, "1", crx_path);
lazyboy8a08c9d2017-04-11 19:53:226757 WaitForExternalExtensionInstalled();
[email protected]460c6712013-04-24 07:20:016758
lazyboy0b9b30f2016-01-05 03:15:376759 EXPECT_TRUE(HasExternalInstallErrors(service()));
6760 ASSERT_TRUE(GetError(updates_from_webstore));
6761 EXPECT_EQ(ExternalInstallError::BUBBLE_ALERT,
6762 GetError(updates_from_webstore)->alert_type());
[email protected]f484f8d52014-06-12 08:38:186763 EXPECT_FALSE(service()->IsExtensionEnabled(updates_from_webstore));
[email protected]612a1cb12012-10-17 13:18:036764}
[email protected]460c6712013-04-24 07:20:016765
6766// Test that there is no bubble for external extensions if the profile is new.
6767TEST_F(ExtensionServiceTest, ExternalInstallUpdatesFromWebstoreNewProfile) {
6768 FeatureSwitch::ScopedOverride prompt(
6769 FeatureSwitch::prompt_for_external_extensions(), true);
6770
6771 InitializeEmptyExtensionService();
6772
vabr9142fe22016-09-08 13:19:226773 base::FilePath crx_path = temp_dir().GetPath().AppendASCII("webstore.crx");
[email protected]f484f8d52014-06-12 08:38:186774 PackCRX(data_dir().AppendASCII("update_from_webstore"),
6775 data_dir().AppendASCII("update_from_webstore.pem"),
[email protected]460c6712013-04-24 07:20:016776 crx_path);
6777
lazyboya00eafc2017-04-08 00:57:196778 MockExternalProvider* provider =
lazyboy8a08c9d2017-04-11 19:53:226779 AddMockExternalProvider(Manifest::EXTERNAL_PREF);
[email protected]460c6712013-04-24 07:20:016780 provider->UpdateOrAddExtension(updates_from_webstore, "1", crx_path);
lazyboy8a08c9d2017-04-11 19:53:226781 WaitForExternalExtensionInstalled();
[email protected]460c6712013-04-24 07:20:016782
lazyboy0b9b30f2016-01-05 03:15:376783 EXPECT_TRUE(HasExternalInstallErrors(service()));
6784 ASSERT_TRUE(GetError(updates_from_webstore));
6785 EXPECT_NE(ExternalInstallError::BUBBLE_ALERT,
6786 GetError(updates_from_webstore)->alert_type());
[email protected]f484f8d52014-06-12 08:38:186787 EXPECT_FALSE(service()->IsExtensionEnabled(updates_from_webstore));
[email protected]460c6712013-04-24 07:20:016788}
[email protected]9f3c8532013-07-31 19:52:076789
[email protected]2894a512014-06-26 19:03:566790// Test that clicking to remove the extension on an external install warning
6791// uninstalls the extension.
6792TEST_F(ExtensionServiceTest, ExternalInstallClickToRemove) {
6793 FeatureSwitch::ScopedOverride prompt(
6794 FeatureSwitch::prompt_for_external_extensions(), true);
6795
6796 ExtensionServiceInitParams params = CreateDefaultInitParams();
6797 params.is_first_run = false;
6798 InitializeExtensionService(params);
6799
vabr9142fe22016-09-08 13:19:226800 base::FilePath crx_path = temp_dir().GetPath().AppendASCII("webstore.crx");
[email protected]2894a512014-06-26 19:03:566801 PackCRX(data_dir().AppendASCII("update_from_webstore"),
6802 data_dir().AppendASCII("update_from_webstore.pem"),
6803 crx_path);
6804
lazyboya00eafc2017-04-08 00:57:196805 MockExternalProvider* provider =
lazyboy8a08c9d2017-04-11 19:53:226806 AddMockExternalProvider(Manifest::EXTERNAL_PREF);
[email protected]2894a512014-06-26 19:03:566807 provider->UpdateOrAddExtension(updates_from_webstore, "1", crx_path);
lazyboy8a08c9d2017-04-11 19:53:226808 WaitForExternalExtensionInstalled();
[email protected]2894a512014-06-26 19:03:566809
lazyboy0b9b30f2016-01-05 03:15:376810 EXPECT_TRUE(HasExternalInstallErrors(service_));
[email protected]2894a512014-06-26 19:03:566811
6812 // We check both enabled and disabled, since these are "eventually exclusive"
6813 // sets.
6814 EXPECT_TRUE(registry()->disabled_extensions().GetByID(updates_from_webstore));
6815 EXPECT_FALSE(registry()->enabled_extensions().GetByID(updates_from_webstore));
6816
6817 // Click the negative response.
lazyboy0b9b30f2016-01-05 03:15:376818 service_->external_install_manager()
6819 ->GetErrorsForTesting()[0]
rdevlin.cronin41593052016-01-08 01:40:126820 ->OnInstallPromptDone(ExtensionInstallPrompt::Result::USER_CANCELED);
[email protected]2894a512014-06-26 19:03:566821 // The Extension should be uninstalled.
6822 EXPECT_FALSE(registry()->GetExtensionById(updates_from_webstore,
6823 ExtensionRegistry::EVERYTHING));
6824 // The error should be removed.
lazyboy0b9b30f2016-01-05 03:15:376825 EXPECT_FALSE(HasExternalInstallErrors(service_));
[email protected]2894a512014-06-26 19:03:566826}
6827
6828// Test that clicking to keep the extension on an external install warning
6829// re-enables the extension.
6830TEST_F(ExtensionServiceTest, ExternalInstallClickToKeep) {
6831 FeatureSwitch::ScopedOverride prompt(
6832 FeatureSwitch::prompt_for_external_extensions(), true);
6833
6834 ExtensionServiceInitParams params = CreateDefaultInitParams();
6835 params.is_first_run = false;
6836 InitializeExtensionService(params);
6837
vabr9142fe22016-09-08 13:19:226838 base::FilePath crx_path = temp_dir().GetPath().AppendASCII("webstore.crx");
[email protected]2894a512014-06-26 19:03:566839 PackCRX(data_dir().AppendASCII("update_from_webstore"),
6840 data_dir().AppendASCII("update_from_webstore.pem"),
6841 crx_path);
6842
lazyboya00eafc2017-04-08 00:57:196843 MockExternalProvider* provider =
lazyboy8a08c9d2017-04-11 19:53:226844 AddMockExternalProvider(Manifest::EXTERNAL_PREF);
[email protected]2894a512014-06-26 19:03:566845 provider->UpdateOrAddExtension(updates_from_webstore, "1", crx_path);
lazyboy8a08c9d2017-04-11 19:53:226846 WaitForExternalExtensionInstalled();
[email protected]2894a512014-06-26 19:03:566847
lazyboy0b9b30f2016-01-05 03:15:376848 EXPECT_TRUE(HasExternalInstallErrors(service_));
[email protected]2894a512014-06-26 19:03:566849
6850 // We check both enabled and disabled, since these are "eventually exclusive"
6851 // sets.
6852 EXPECT_TRUE(registry()->disabled_extensions().GetByID(updates_from_webstore));
6853 EXPECT_FALSE(registry()->enabled_extensions().GetByID(updates_from_webstore));
6854
6855 // Accept the extension.
lazyboy0b9b30f2016-01-05 03:15:376856 service_->external_install_manager()
6857 ->GetErrorsForTesting()[0]
rdevlin.cronin41593052016-01-08 01:40:126858 ->OnInstallPromptDone(ExtensionInstallPrompt::Result::ACCEPTED);
[email protected]2894a512014-06-26 19:03:566859
6860 // It should be enabled again.
6861 EXPECT_TRUE(registry()->enabled_extensions().GetByID(updates_from_webstore));
6862 EXPECT_FALSE(
6863 registry()->disabled_extensions().GetByID(updates_from_webstore));
6864
6865 // The error should be removed.
lazyboy0b9b30f2016-01-05 03:15:376866 EXPECT_FALSE(HasExternalInstallErrors(service_));
[email protected]2894a512014-06-26 19:03:566867}
6868
rdevlin.cronine1456712016-12-29 22:47:286869// Test that the external install bubble only takes disabled extensions into
6870// account - enabled extensions, even those that weren't acknowledged, should
6871// not be warned about. This lets us grandfather extensions in.
6872TEST_F(ExtensionServiceTest,
6873 ExternalInstallBubbleDoesntShowForEnabledExtensions) {
6874 auto external_prompt_override =
6875 base::MakeUnique<FeatureSwitch::ScopedOverride>(
6876 FeatureSwitch::prompt_for_external_extensions(), false);
6877 InitializeEmptyExtensionService();
6878
6879 // Register and install an external extension.
lazyboya00eafc2017-04-08 00:57:196880 MockExternalProvider* provider =
lazyboy8a08c9d2017-04-11 19:53:226881 AddMockExternalProvider(Manifest::EXTERNAL_PREF);
rdevlin.cronine1456712016-12-29 22:47:286882 provider->UpdateOrAddExtension(good_crx, "1.0.0.0",
6883 data_dir().AppendASCII("good.crx"));
6884
6885 WaitForExternalExtensionInstalled();
6886
6887 EXPECT_TRUE(registry()->enabled_extensions().Contains(good_crx));
6888 ExtensionPrefs* prefs = ExtensionPrefs::Get(profile());
6889 EXPECT_FALSE(prefs->IsExternalExtensionAcknowledged(good_crx));
Minh X. Nguyen45479012017-08-18 21:35:366890 EXPECT_EQ(extensions::disable_reason::DISABLE_NONE,
6891 prefs->GetDisableReasons(good_crx));
rdevlin.cronine1456712016-12-29 22:47:286892
6893 // We explicitly reset the override first. ScopedOverrides reset the value
6894 // to the original value on destruction, but if we reset by passing a new
6895 // object, the new object is constructed (overriding the current value)
6896 // before the old is destructed (which will immediately reset to the
6897 // original).
6898 external_prompt_override.reset();
6899 external_prompt_override = base::MakeUnique<FeatureSwitch::ScopedOverride>(
6900 FeatureSwitch::prompt_for_external_extensions(), true);
6901
6902 extensions::ExternalInstallManager* external_manager =
6903 service()->external_install_manager();
6904 external_manager->UpdateExternalExtensionAlert();
6905 EXPECT_FALSE(external_manager->has_currently_visible_install_alert());
6906 EXPECT_TRUE(external_manager->GetErrorsForTesting().empty());
6907
6908 provider->UpdateOrAddExtension(good_crx, "1.0.0.1",
6909 data_dir().AppendASCII("good2.crx"));
6910
6911 WaitForExternalExtensionInstalled();
6912
6913 external_manager->UpdateExternalExtensionAlert();
6914 EXPECT_FALSE(external_manager->has_currently_visible_install_alert());
6915 EXPECT_TRUE(external_manager->GetErrorsForTesting().empty());
6916}
6917
[email protected]9f3c8532013-07-31 19:52:076918TEST_F(ExtensionServiceTest, InstallBlacklistedExtension) {
6919 InitializeEmptyExtensionService();
6920
Devlin Cronin05b047d2017-08-22 21:53:556921 scoped_refptr<Extension> extension = ExtensionBuilder("extension").Build();
[email protected]9f3c8532013-07-31 19:52:076922 ASSERT_TRUE(extension.get());
6923 const std::string& id = extension->id();
6924
6925 std::set<std::string> id_set;
6926 id_set.insert(id);
[email protected]9f3c8532013-07-31 19:52:076927
limasdfe1d046f2015-10-29 00:48:006928 extensions::TestExtensionRegistryObserver observer(
6929 extensions::ExtensionRegistry::Get(profile()));
[email protected]9f3c8532013-07-31 19:52:076930 // Installation should be allowed but the extension should never have been
6931 // loaded and it should be blacklisted in prefs.
[email protected]4a1d9c0d2014-06-13 12:50:116932 service()->OnExtensionInstalled(
6933 extension.get(),
6934 syncer::StringOrdinal(),
6935 (extensions::kInstallFlagIsBlacklistedForMalware |
6936 extensions::kInstallFlagInstallImmediately));
Gabriel Charette01507a22017-09-27 21:30:086937 content::RunAllTasksUntilIdle();
[email protected]9f3c8532013-07-31 19:52:076938
6939 // Extension was installed but not loaded.
limasdfe1d046f2015-10-29 00:48:006940 observer.WaitForExtensionWillBeInstalled();
[email protected]f484f8d52014-06-12 08:38:186941 EXPECT_TRUE(service()->GetInstalledExtension(id));
[email protected]5fdfa562013-12-27 17:43:596942
[email protected]f484f8d52014-06-12 08:38:186943 EXPECT_FALSE(registry()->enabled_extensions().Contains(id));
6944 EXPECT_TRUE(registry()->blacklisted_extensions().Contains(id));
[email protected]5fdfa562013-12-27 17:43:596945
[email protected]f484f8d52014-06-12 08:38:186946 EXPECT_TRUE(ExtensionPrefs::Get(profile())->IsExtensionBlacklisted(id));
6947 EXPECT_TRUE(
6948 ExtensionPrefs::Get(profile())->IsBlacklistedExtensionAcknowledged(id));
[email protected]9f3c8532013-07-31 19:52:076949}
[email protected]59ee99d2013-10-11 15:46:166950
asargent96c7ec42016-05-27 02:45:476951// Test that we won't allow enabling a blacklisted extension.
6952TEST_F(ExtensionServiceTest, CannotEnableBlacklistedExtension) {
6953 InitializeGoodInstalledExtensionService();
6954 service()->Init();
6955 ASSERT_FALSE(registry()->enabled_extensions().is_empty());
6956
6957 // Blacklist the first extension; then try enabling it.
6958 std::string id = (*(registry()->enabled_extensions().begin()))->id();
6959 service()->BlacklistExtensionForTest(id);
6960 EXPECT_FALSE(registry()->enabled_extensions().Contains(id));
6961 EXPECT_FALSE(registry()->disabled_extensions().Contains(id));
6962 service()->EnableExtension(id);
6963 EXPECT_FALSE(registry()->enabled_extensions().Contains(id));
6964 EXPECT_FALSE(registry()->disabled_extensions().Contains(id));
6965 EXPECT_TRUE(registry()->blacklisted_extensions().Contains(id));
6966 EXPECT_TRUE(ExtensionPrefs::Get(profile())->IsExtensionBlacklisted(id));
6967
Minh X. Nguyen45479012017-08-18 21:35:366968 service()->DisableExtension(id,
6969 extensions::disable_reason::DISABLE_USER_ACTION);
asargent96c7ec42016-05-27 02:45:476970 EXPECT_FALSE(registry()->enabled_extensions().Contains(id));
6971 EXPECT_FALSE(registry()->disabled_extensions().Contains(id));
6972 EXPECT_TRUE(registry()->blacklisted_extensions().Contains(id));
6973 EXPECT_TRUE(ExtensionPrefs::Get(profile())->IsExtensionBlacklisted(id));
6974}
6975
elijahtaylor49c09c5c2016-07-09 01:06:226976// Test that calls to disable Shared Modules do not work.
6977TEST_F(ExtensionServiceTest, CannotDisableSharedModules) {
6978 InitializeEmptyExtensionService();
Devlin Cronin05b047d2017-08-22 21:53:556979 std::unique_ptr<base::DictionaryValue> export_dict =
elijahtaylor49c09c5c2016-07-09 01:06:226980 extensions::DictionaryBuilder()
Devlin Cronin05b047d2017-08-22 21:53:556981 .Set("resources", extensions::ListBuilder().Append("foo.js").Build())
elijahtaylor49c09c5c2016-07-09 01:06:226982 .Build();
6983
Devlin Cronin05b047d2017-08-22 21:53:556984 scoped_refptr<Extension> extension =
6985 ExtensionBuilder("Shared Module")
6986 .MergeManifest(extensions::DictionaryBuilder()
6987 .Set("export", std::move(export_dict))
6988 .Build())
6989 .AddFlags(Extension::FROM_WEBSTORE)
6990 .Build();
elijahtaylor49c09c5c2016-07-09 01:06:226991
6992 service()->OnExtensionInstalled(extension.get(), syncer::StringOrdinal(),
6993 extensions::kInstallFlagInstallImmediately);
6994
6995 ASSERT_TRUE(registry()->enabled_extensions().Contains(extension->id()));
6996 // Try to disable the extension.
Minh X. Nguyen45479012017-08-18 21:35:366997 service()->DisableExtension(extension->id(),
6998 extensions::disable_reason::DISABLE_USER_ACTION);
elijahtaylor49c09c5c2016-07-09 01:06:226999 // Shared Module should still be enabled.
7000 EXPECT_TRUE(registry()->enabled_extensions().Contains(extension->id()));
7001}
7002
asargent96c7ec42016-05-27 02:45:477003// Make sure we can uninstall a blacklisted extension
7004TEST_F(ExtensionServiceTest, UninstallBlacklistedExtension) {
7005 InitializeGoodInstalledExtensionService();
7006 service()->Init();
7007 ASSERT_FALSE(registry()->enabled_extensions().is_empty());
7008
7009 // Blacklist the first extension; then try uninstalling it.
7010 std::string id = (*(registry()->enabled_extensions().begin()))->id();
7011 service()->BlacklistExtensionForTest(id);
7012 EXPECT_NE(nullptr, registry()->GetInstalledExtension(id));
7013 base::string16 error;
7014 EXPECT_TRUE(service()->UninstallExtension(
Devlin Cronin218df7f2017-11-21 21:41:317015 id, extensions::UNINSTALL_REASON_USER_INITIATED, nullptr));
asargent96c7ec42016-05-27 02:45:477016 EXPECT_EQ(nullptr, registry()->GetInstalledExtension(id));
7017}
7018
[email protected]ebe07772014-05-22 04:16:067019// Tests a profile being destroyed correctly disables extensions.
7020TEST_F(ExtensionServiceTest, DestroyingProfileClearsExtensions) {
7021 InitializeEmptyExtensionService();
7022
[email protected]f484f8d52014-06-12 08:38:187023 InstallCRX(data_dir().AppendASCII("good.crx"), INSTALL_NEW);
limasdf0deef2042017-05-03 19:17:177024 EXPECT_NE(UnloadedExtensionReason::PROFILE_SHUTDOWN, unloaded_reason_);
[email protected]f484f8d52014-06-12 08:38:187025 EXPECT_EQ(1u, registry()->enabled_extensions().size());
7026 EXPECT_EQ(0u, registry()->disabled_extensions().size());
7027 EXPECT_EQ(0u, registry()->terminated_extensions().size());
7028 EXPECT_EQ(0u, registry()->blacklisted_extensions().size());
[email protected]ebe07772014-05-22 04:16:067029
[email protected]f484f8d52014-06-12 08:38:187030 service()->Observe(chrome::NOTIFICATION_PROFILE_DESTRUCTION_STARTED,
7031 content::Source<Profile>(profile()),
7032 content::NotificationService::NoDetails());
limasdf0deef2042017-05-03 19:17:177033 EXPECT_EQ(UnloadedExtensionReason::PROFILE_SHUTDOWN, unloaded_reason_);
[email protected]f484f8d52014-06-12 08:38:187034 EXPECT_EQ(0u, registry()->enabled_extensions().size());
7035 EXPECT_EQ(0u, registry()->disabled_extensions().size());
7036 EXPECT_EQ(0u, registry()->terminated_extensions().size());
7037 EXPECT_EQ(0u, registry()->blacklisted_extensions().size());
[email protected]ebe07772014-05-22 04:16:067038}
asargente4de9f92016-09-15 01:40:067039
7040// Test that updating a corrupt extension removes the DISABLE_CORRUPTED disable
7041// reason.
7042TEST_F(ExtensionServiceTest, CorruptExtensionUpdate) {
7043 InitializeEmptyExtensionService();
7044
7045 base::FilePath v1_path = data_dir().AppendASCII("good.crx");
7046 const Extension* v1 = InstallCRX(v1_path, INSTALL_NEW);
7047 std::string id = v1->id();
7048
Minh X. Nguyen45479012017-08-18 21:35:367049 service()->DisableExtension(id,
7050 extensions::disable_reason::DISABLE_CORRUPTED);
asargente4de9f92016-09-15 01:40:067051
7052 ExtensionPrefs* prefs = ExtensionPrefs::Get(profile());
7053 EXPECT_TRUE(registry()->disabled_extensions().Contains(id));
Minh X. Nguyen45479012017-08-18 21:35:367054 EXPECT_TRUE(prefs->HasDisableReason(
7055 id, extensions::disable_reason::DISABLE_CORRUPTED));
asargente4de9f92016-09-15 01:40:067056
7057 base::FilePath v2_path = data_dir().AppendASCII("good2.crx");
7058 UpdateExtension(id, v2_path, ENABLED);
7059
7060 EXPECT_FALSE(registry()->disabled_extensions().Contains(id));
Minh X. Nguyen45479012017-08-18 21:35:367061 EXPECT_FALSE(prefs->HasDisableReason(
7062 id, extensions::disable_reason::DISABLE_CORRUPTED));
asargente4de9f92016-09-15 01:40:067063}
rdevlin.croninf87d15f2017-01-26 22:57:197064
7065// Try re-enabling a reloading extension. Regression test for crbug.com/676815.
7066TEST_F(ExtensionServiceTest, ReloadAndReEnableExtension) {
7067 InitializeEmptyExtensionService();
7068
7069 // Add an extension in an unpacked location.
7070 scoped_refptr<const Extension> extension =
7071 extensions::ChromeTestExtensionLoader(profile()).
7072 LoadExtension(data_dir().AppendASCII("simple_with_file"));
7073 const std::string kExtensionId = extension->id();
7074 ASSERT_TRUE(extension);
7075 ASSERT_TRUE(extensions::Manifest::IsUnpackedLocation(extension->location()));
7076 EXPECT_TRUE(registry()->enabled_extensions().Contains(kExtensionId));
7077
7078 // Begin the reload process.
7079 service()->ReloadExtension(extension->id());
7080 EXPECT_TRUE(registry()->disabled_extensions().Contains(kExtensionId));
7081
7082 // While the extension is reloading, try to re-enable it. This is the flow
7083 // that could happen if, e.g., the user hit the enable toggle in the
7084 // chrome://extensions page while it was reloading.
7085 service()->GrantPermissionsAndEnableExtension(extension.get());
7086 EXPECT_FALSE(registry()->enabled_extensions().Contains(kExtensionId));
7087
7088 // Wait for the reload to complete. This previously crashed (see
7089 // crbug.com/676815).
Gabriel Charette01507a22017-09-27 21:30:087090 content::RunAllTasksUntilIdle();
rdevlin.croninf87d15f2017-01-26 22:57:197091 // The extension should be enabled again...
7092 EXPECT_TRUE(registry()->enabled_extensions().Contains(kExtensionId));
7093 // ...and should have reloaded (for ease, we just compare the extension
7094 // objects).
7095 EXPECT_NE(extension, registry()->enabled_extensions().GetByID(kExtensionId));
7096}
rdevlin.cronin07f10c12017-02-08 19:37:587097
7098// Test reloading a shared module. Regression test for crbug.com/676815.
7099TEST_F(ExtensionServiceTest, ReloadSharedModule) {
7100 InitializeEmptyExtensionService();
7101
7102 // Add a shared module and an extension that depends on it (the latter is
7103 // important to ensure we don't remove the unused shared module).
7104 scoped_refptr<const Extension> shared_module =
7105 extensions::ChromeTestExtensionLoader(profile()).LoadExtension(
7106 data_dir().AppendASCII("api_test/shared_module/shared"));
7107 scoped_refptr<const Extension> dependent =
7108 extensions::ChromeTestExtensionLoader(profile()).LoadExtension(
7109 data_dir().AppendASCII("api_test/shared_module/import_pass"));
7110 ASSERT_TRUE(shared_module);
7111 ASSERT_TRUE(dependent);
7112 const std::string kExtensionId = shared_module->id();
7113 ASSERT_TRUE(
7114 extensions::Manifest::IsUnpackedLocation(shared_module->location()));
7115 ASSERT_EQ(extensions::Manifest::TYPE_SHARED_MODULE,
7116 shared_module->manifest()->type());
7117 EXPECT_TRUE(registry()->enabled_extensions().Contains(kExtensionId));
7118
7119 // Reload the extension and wait for it to complete. This previously crashed
7120 // (see crbug.com/676815).
7121 service()->ReloadExtension(kExtensionId);
Gabriel Charette01507a22017-09-27 21:30:087122 content::RunAllTasksUntilIdle();
rdevlin.cronin07f10c12017-02-08 19:37:587123 // The shared module should be enabled.
7124 EXPECT_TRUE(registry()->enabled_extensions().Contains(kExtensionId));
7125}
Takumi Fujimoto43c8c00f2017-07-26 22:48:567126
7127// Tests that extensions that have been migrated to component extensions can be
7128// uninstalled.
7129TEST_F(ExtensionServiceTest, UninstallMigratedExtensions) {
7130 InitializeEmptyExtensionService();
7131
7132 scoped_refptr<const Extension> cast_extension =
Devlin Cronin05b047d2017-08-22 21:53:557133 ExtensionBuilder("stable")
7134 .SetID(cast_stable)
7135 .SetLocation(Manifest::INTERNAL)
7136 .Build();
Takumi Fujimoto43c8c00f2017-07-26 22:48:567137 scoped_refptr<const Extension> cast_beta_extension =
Devlin Cronin05b047d2017-08-22 21:53:557138 ExtensionBuilder("beta")
7139 .SetID(cast_beta)
7140 .SetLocation(Manifest::INTERNAL)
7141 .Build();
Takumi Fujimoto43c8c00f2017-07-26 22:48:567142 service()->AddExtension(cast_extension.get());
7143 service()->AddExtension(cast_beta_extension.get());
7144 ASSERT_TRUE(registry()->enabled_extensions().Contains(cast_stable));
7145 ASSERT_TRUE(registry()->enabled_extensions().Contains(cast_beta));
7146
7147 service()->UninstallMigratedExtensionsForTest();
7148 EXPECT_FALSE(service()->GetInstalledExtension(cast_stable));
7149 EXPECT_FALSE(service()->GetInstalledExtension(cast_beta));
7150}
7151
7152// Tests that extensions that have been migrated to component extensions can be
7153// uninstalled even when they are disabled.
7154TEST_F(ExtensionServiceTest, UninstallDisabledMigratedExtension) {
7155 InitializeEmptyExtensionService();
7156
7157 scoped_refptr<const Extension> cast_extension =
Devlin Cronin05b047d2017-08-22 21:53:557158 ExtensionBuilder("stable")
7159 .SetID(cast_stable)
7160 .SetLocation(Manifest::INTERNAL)
7161 .Build();
Takumi Fujimoto43c8c00f2017-07-26 22:48:567162 service()->AddExtension(cast_extension.get());
Minh X. Nguyen45479012017-08-18 21:35:367163 service()->DisableExtension(cast_stable,
7164 extensions::disable_reason::DISABLE_USER_ACTION);
Takumi Fujimoto43c8c00f2017-07-26 22:48:567165 ASSERT_TRUE(registry()->disabled_extensions().Contains(cast_stable));
7166
7167 service()->UninstallMigratedExtensionsForTest();
7168 EXPECT_FALSE(service()->GetInstalledExtension(cast_stable));
7169}
Tatsuhisa Yamaguchi5ed1aece32017-10-26 10:18:567170
7171TEST_F(ExtensionServiceTest, EnableZipUnpackerExtension) {
7172 InitializeEmptyExtensionService();
7173
7174 scoped_refptr<const Extension> zip_unpacker_extension =
7175 ExtensionBuilder("stable")
7176 .SetID(zip_unpacker)
7177 .SetLocation(Manifest::EXTERNAL_COMPONENT)
7178 .Build();
7179 service()->AddExtension(zip_unpacker_extension.get());
7180 service()->DisableExtension(zip_unpacker,
7181 extensions::disable_reason::DISABLE_USER_ACTION);
7182 ASSERT_TRUE(registry()->disabled_extensions().Contains(zip_unpacker));
7183
7184 service()->EnableZipUnpackerExtensionForTest();
7185#if defined(OS_CHROMEOS)
7186 ASSERT_TRUE(registry()->enabled_extensions().Contains(zip_unpacker));
7187#else
7188 ASSERT_TRUE(registry()->disabled_extensions().Contains(zip_unpacker));
7189#endif
7190}
7191
7192TEST_F(ExtensionServiceTest, ShouldNotEnableZipUnpackerExtensionAgainstPolicy) {
7193 InitializeEmptyExtensionService();
7194
7195 GetManagementPolicy()->UnregisterAllProviders();
7196 extensions::TestManagementPolicyProvider provider_(
7197 extensions::TestManagementPolicyProvider::MUST_REMAIN_DISABLED);
7198 GetManagementPolicy()->RegisterProvider(&provider_);
7199
7200 scoped_refptr<const Extension> zip_unpacker_extension =
7201 ExtensionBuilder("stable")
7202 .SetID(zip_unpacker)
7203 .SetLocation(Manifest::EXTERNAL_COMPONENT)
7204 .Build();
7205 service()->AddExtension(zip_unpacker_extension.get());
7206 service()->DisableExtension(zip_unpacker,
7207 extensions::disable_reason::DISABLE_USER_ACTION);
7208 ASSERT_TRUE(registry()->disabled_extensions().Contains(zip_unpacker));
7209
7210 service()->EnableZipUnpackerExtensionForTest();
7211 ASSERT_FALSE(registry()->enabled_extensions().Contains(zip_unpacker));
7212}