blob: 49edb1effd4cde611b5a3bcfe62791058a583d5b [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
[email protected]eaa7dd182010-12-14 11:09:005#include "chrome/browser/extensions/extension_service_unittest.h"
[email protected]bf73f0b2010-02-10 19:26:596
[email protected]f0397fa2008-12-11 17:59:587#include <algorithm>
[email protected]7fa19f82010-12-21 19:40:088#include <set>
[email protected]6014d672008-12-05 00:38:259#include <vector>
10
[email protected]be5a6db2012-11-13 14:39:1111#include "base/at_exit.h"
[email protected]24b538a2010-02-27 01:22:4412#include "base/basictypes.h"
[email protected]c4148a72011-08-09 23:04:2013#include "base/bind.h"
[email protected]36a784c2009-06-23 06:21:0814#include "base/command_line.h"
[email protected]6014d672008-12-05 00:38:2515#include "base/file_util.h"
[email protected]25a4c1c2013-06-08 04:53:3616#include "base/files/file_enumerator.h"
[email protected]ea1a3f62012-11-16 20:34:2317#include "base/files/scoped_temp_dir.h"
[email protected]ffbec692012-02-26 20:26:4218#include "base/json/json_file_value_serializer.h"
[email protected]93d49d72009-10-23 20:00:2019#include "base/json/json_reader.h"
[email protected]ffbec692012-02-26 20:26:4220#include "base/json/json_string_value_serializer.h"
[email protected]3b63f8f42011-03-28 01:54:1521#include "base/memory/scoped_ptr.h"
[email protected]71cb8aa2011-12-29 19:14:0022#include "base/memory/weak_ptr.h"
[email protected]b19fe572013-07-18 04:54:2623#include "base/message_loop/message_loop.h"
[email protected]6014d672008-12-05 00:38:2524#include "base/path_service.h"
[email protected]9eec53fe2013-10-30 20:21:1725#include "base/prefs/scoped_user_pref_update.h"
[email protected]7286e3fc2011-07-19 22:13:2426#include "base/stl_util.h"
[email protected]00e7bef2013-06-10 20:35:1727#include "base/strings/string16.h"
[email protected]3ea1b182013-02-08 22:38:4128#include "base/strings/string_number_conversions.h"
[email protected]00e7bef2013-06-10 20:35:1729#include "base/strings/string_util.h"
[email protected]112158af2013-06-07 23:46:1830#include "base/strings/utf_string_conversions.h"
[email protected]aa142702010-03-26 01:26:3331#include "base/version.h"
[email protected]9d32ded072011-10-11 16:31:0532#include "chrome/browser/browser_process.h"
[email protected]9ea0cd32013-07-12 01:50:3633#include "chrome/browser/chrome_notification_types.h"
[email protected]5db9ada2012-04-11 13:48:2034#include "chrome/browser/extensions/app_sync_data.h"
[email protected]3f2a2fa2013-09-24 02:55:2535#include "chrome/browser/extensions/blacklist.h"
[email protected]74474042013-11-21 12:03:5436#include "chrome/browser/extensions/chrome_app_sorting.h"
[email protected]d8c8f25f2011-11-02 18:18:0137#include "chrome/browser/extensions/component_loader.h"
[email protected]eb6c7ef2011-12-12 23:12:2038#include "chrome/browser/extensions/crx_installer.h"
[email protected]a7cd28e2012-10-05 21:03:3639#include "chrome/browser/extensions/default_apps.h"
[email protected]a17f9462009-06-09 02:56:4140#include "chrome/browser/extensions/extension_creator.h"
[email protected]14a000d2010-04-29 21:44:2441#include "chrome/browser/extensions/extension_error_reporter.h"
[email protected]89226982012-07-16 20:09:1842#include "chrome/browser/extensions/extension_error_ui.h"
[email protected]9f3c8532013-07-31 19:52:0743#include "chrome/browser/extensions/extension_notification_observer.h"
[email protected]eaa7dd182010-12-14 11:09:0044#include "chrome/browser/extensions/extension_service.h"
[email protected]19eb80152011-02-26 00:28:4345#include "chrome/browser/extensions/extension_special_storage_policy.h"
[email protected]90310d92011-04-17 07:35:0446#include "chrome/browser/extensions/extension_sync_data.h"
[email protected]31d8f5f22012-04-02 15:22:0847#include "chrome/browser/extensions/extension_system.h"
[email protected]a7ff4b72013-10-17 20:56:0248#include "chrome/browser/extensions/extension_util.h"
[email protected]612a1cb12012-10-17 13:18:0349#include "chrome/browser/extensions/external_install_ui.h"
[email protected]e410b5f2012-12-14 14:02:2450#include "chrome/browser/extensions/external_policy_loader.h"
[email protected]5df038b2012-07-16 19:03:2751#include "chrome/browser/extensions/external_pref_loader.h"
52#include "chrome/browser/extensions/external_provider_impl.h"
[email protected]3f2a2fa2013-09-24 02:55:2553#include "chrome/browser/extensions/fake_safe_browsing_database_manager.h"
[email protected]0d55b9a2013-07-19 10:40:2654#include "chrome/browser/extensions/install_observer.h"
55#include "chrome/browser/extensions/install_tracker.h"
56#include "chrome/browser/extensions/install_tracker_factory.h"
[email protected]d8c8f25f2011-11-02 18:18:0157#include "chrome/browser/extensions/installed_loader.h"
[email protected]f0bfe622012-06-22 01:01:4458#include "chrome/browser/extensions/pack_extension_job.h"
[email protected]f71b582c2014-01-10 17:03:1559#include "chrome/browser/extensions/test_blacklist.h"
[email protected]31d8f5f22012-04-02 15:22:0860#include "chrome/browser/extensions/test_extension_system.h"
[email protected]d8c8f25f2011-11-02 18:18:0161#include "chrome/browser/extensions/unpacked_installer.h"
[email protected]42a08162012-03-16 18:09:1162#include "chrome/browser/extensions/updater/extension_updater.h"
[email protected]37858e52010-08-26 00:22:0263#include "chrome/browser/prefs/browser_prefs.h"
[email protected]e90a01f2013-11-19 04:22:1264#include "chrome/browser/prefs/pref_service_mock_factory.h"
[email protected]c753f142013-02-10 13:14:0465#include "chrome/browser/prefs/pref_service_syncable.h"
[email protected]50736a12013-09-26 08:58:3466#include "chrome/browser/sync/profile_sync_service.h"
67#include "chrome/browser/sync/profile_sync_service_factory.h"
[email protected]31d8f5f22012-04-02 15:22:0868#include "chrome/common/chrome_constants.h"
[email protected]37858e52010-08-26 00:22:0269#include "chrome/common/chrome_paths.h"
70#include "chrome/common/chrome_switches.h"
[email protected]098fa7a2013-03-08 22:11:1771#include "chrome/common/extensions/api/plugins/plugins_handler.h"
[email protected]6884a802012-08-07 03:55:2272#include "chrome/common/extensions/extension_l10n_util.h"
[email protected]6b414c232013-06-05 07:53:3473#include "chrome/common/extensions/manifest_handlers/app_launch_info.h"
[email protected]334ec0a2013-03-24 01:36:1674#include "chrome/common/extensions/manifest_handlers/content_scripts_handler.h"
[email protected]65348062013-01-15 07:27:2275#include "chrome/common/extensions/manifest_url_handler.h"
[email protected]36a784c2009-06-23 06:21:0876#include "chrome/common/pref_names.h"
[email protected]24b538a2010-02-27 01:22:4477#include "chrome/common/url_constants.h"
[email protected]078c3192013-08-22 08:18:0578#include "chrome/test/base/scoped_browser_locale.h"
[email protected]a4ff9eae2011-08-01 19:58:1679#include "chrome/test/base/testing_profile.h"
[email protected]75fee372013-03-06 00:42:4480#include "components/user_prefs/pref_registry_syncable.h"
[email protected]35cc399e2012-02-23 18:19:2881#include "content/public/browser/dom_storage_context.h"
[email protected]98270432012-09-11 20:51:2482#include "content/public/browser/gpu_data_manager.h"
[email protected]35cc399e2012-02-23 18:19:2883#include "content/public/browser/indexed_db_context.h"
[email protected]6c2381d2011-10-19 02:52:5384#include "content/public/browser/notification_registrar.h"
[email protected]ad50def52011-10-19 23:17:0785#include "content/public/browser/notification_service.h"
[email protected]e67385f2011-12-21 06:00:5686#include "content/public/browser/plugin_service.h"
[email protected]6d057a0c2013-07-09 21:12:0787#include "content/public/browser/render_process_host.h"
[email protected]5c8e67c2012-08-29 00:48:5288#include "content/public/browser/storage_partition.h"
[email protected]55eb70e762012-02-20 17:38:3989#include "content/public/common/content_constants.h"
[email protected]6d057a0c2013-07-09 21:12:0790#include "content/public/test/test_utils.h"
[email protected]5fdfa562013-12-27 17:43:5991#include "extensions/browser/extension_registry.h"
[email protected]301116c62013-11-26 10:37:4592#include "extensions/browser/external_provider_interface.h"
93#include "extensions/browser/management_policy.h"
[email protected]b52f8ca2013-11-28 08:25:2294#include "extensions/browser/pending_extension_info.h"
95#include "extensions/browser/pending_extension_manager.h"
[email protected]234fc5ff2014-01-16 23:32:2896#include "extensions/browser/pref_names.h"
[email protected]301116c62013-11-26 10:37:4597#include "extensions/browser/test_management_policy.h"
[email protected]993da5e2013-03-23 21:25:1698#include "extensions/common/constants.h"
[email protected]e4452d32013-11-15 23:07:4199#include "extensions/common/extension.h"
[email protected]22b7b2c2013-11-05 22:52:42100#include "extensions/common/extension_builder.h"
[email protected]993da5e2013-03-23 21:25:16101#include "extensions/common/extension_resource.h"
[email protected]0c3c9732013-09-16 08:53:41102#include "extensions/common/manifest_constants.h"
[email protected]558878cc82013-11-09 01:25:51103#include "extensions/common/manifest_handlers/background_info.h"
[email protected]5a55f3f2013-10-29 01:08:29104#include "extensions/common/permissions/permission_set.h"
[email protected]885c0e92012-11-13 20:27:42105#include "extensions/common/url_pattern.h"
[email protected]22b7b2c2013-11-05 22:52:42106#include "extensions/common/value_builder.h"
[email protected]d7b5cc72013-05-23 20:05:00107#include "gpu/config/gpu_info.h"
[email protected]b873cd92012-02-09 21:51:48108#include "grit/browser_resources.h"
[email protected]5b9bc352012-07-18 13:13:34109#include "net/cookies/canonical_cookie.h"
[email protected]aa84a7e2012-03-15 21:29:06110#include "net/cookies/cookie_monster.h"
111#include "net/cookies/cookie_options.h"
[email protected]dbbad7a2010-08-13 18:18:36112#include "net/url_request/url_request_context.h"
[email protected]abe2c032011-03-31 18:49:34113#include "net/url_request/url_request_context_getter.h"
[email protected]36b643212012-09-07 12:53:00114#include "sync/api/string_ordinal.h"
[email protected]50736a12013-09-26 08:58:34115#include "sync/api/sync_data.h"
[email protected]895a1e52012-05-15 02:50:12116#include "sync/api/sync_error_factory.h"
117#include "sync/api/sync_error_factory_mock.h"
[email protected]6d147912013-07-09 08:02:14118#include "sync/api/syncable_service.h"
[email protected]1bcf30e2012-03-10 01:06:41119#include "sync/protocol/app_specifics.pb.h"
120#include "sync/protocol/extension_specifics.pb.h"
121#include "sync/protocol/sync.pb.h"
[email protected]6014d672008-12-05 00:38:25122#include "testing/gtest/include/gtest/gtest.h"
[email protected]f66c110c2008-12-05 20:26:29123#include "testing/platform_test.h"
[email protected]a6483d22013-07-03 22:11:00124#include "url/gurl.h"
[email protected]08b1f75f2013-05-22 22:02:38125#include "webkit/browser/database/database_tracker.h"
[email protected]7660ec92013-05-30 05:12:39126#include "webkit/browser/quota/quota_manager.h"
[email protected]2a9644c2013-06-19 06:19:34127#include "webkit/common/database/database_identifier.h"
[email protected]6014d672008-12-05 00:38:25128
[email protected]88e8ec9152013-01-17 04:05:18129#if defined(OS_CHROMEOS)
130#include "chrome/browser/chromeos/extensions/install_limiter.h"
[email protected]e4854dc2013-04-24 00:11:51131#include "chrome/browser/chromeos/login/user_manager.h"
132#include "chrome/browser/chromeos/settings/cros_settings.h"
133#include "chrome/browser/chromeos/settings/device_settings_service.h"
[email protected]88e8ec9152013-01-17 04:05:18134#endif
135
[email protected]3f2a2fa2013-09-24 02:55:25136// The blacklist tests rely on safe browsing.
137#if defined(FULL_SAFE_BROWSING) || defined(MOBILE_SAFE_BROWSING)
138#define ENABLE_BLACKLIST_TESTS
139#endif
140
[email protected]aeca23f2013-06-21 22:34:41141using base::DictionaryValue;
142using base::ListValue;
143using base::Value;
[email protected]55eb70e762012-02-20 17:38:39144using content::BrowserContext;
[email protected]631bb742011-11-02 11:29:39145using content::BrowserThread;
[email protected]35cc399e2012-02-23 18:19:28146using content::DOMStorageContext;
147using content::IndexedDBContext;
[email protected]e67385f2011-12-21 06:00:56148using content::PluginService;
[email protected]c2e66e12012-06-27 06:27:06149using extensions::APIPermission;
150using extensions::APIPermissionSet;
[email protected]74474042013-11-21 12:03:54151using extensions::AppSorting;
[email protected]3f2a2fa2013-09-24 02:55:25152using extensions::Blacklist;
[email protected]bf3d9df2012-07-24 23:20:27153using extensions::CrxInstaller;
[email protected]1c321ee52012-05-21 03:02:34154using extensions::Extension;
[email protected]6d777492012-07-11 17:33:43155using extensions::ExtensionCreator;
[email protected]45759612012-07-10 17:21:23156using extensions::ExtensionPrefs;
[email protected]5fdfa562013-12-27 17:43:59157using extensions::ExtensionRegistry;
[email protected]993da5e2013-03-23 21:25:16158using extensions::ExtensionResource;
[email protected]bd306722012-07-11 20:43:59159using extensions::ExtensionSystem;
[email protected]3f2a2fa2013-09-24 02:55:25160using extensions::FakeSafeBrowsingDatabaseManager;
[email protected]00b5d0a52012-10-30 13:13:53161using extensions::FeatureSwitch;
[email protected]1d5e58b2013-01-31 08:41:40162using extensions::Manifest;
[email protected]c2e66e12012-06-27 06:27:06163using extensions::PermissionSet;
[email protected]b3aa7182013-04-25 04:45:23164using extensions::TestExtensionSystem;
[email protected]e9f541a2012-11-19 21:52:31165using extensions::URLPatternSet;
[email protected]631bb742011-11-02 11:29:39166
[email protected]6bf90612013-08-15 00:36:27167namespace keys = extensions::manifest_keys;
[email protected]c6d474f82009-12-16 21:11:06168
[email protected]f0397fa2008-12-11 17:59:58169namespace {
170
[email protected]df4956e2009-06-10 16:53:42171// Extension ids used during testing.
[email protected]095ccbe42013-09-26 00:06:42172const char good0[] = "behllobkkfkfnphdnhnkndlbkcpglgmj";
173const char good1[] = "hpiknbiabeeppbpihjehijgoemciehgk";
174const char good2[] = "bjafgdebaacbbbecmhlhpofkepfkgcpa";
[email protected]aa5219d2013-11-09 19:25:41175const char all_zero[] = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
[email protected]095ccbe42013-09-26 00:06:42176const char good2048[] = "nmgjhmhbleinmjpbdhgajfjkbijcmgbh";
177const char good_crx[] = "ldnnhddmnhbkjipkidpdiheffobcpfmf";
178const char hosted_app[] = "kbmnembihfiondgfjekmnmcbddelicoi";
179const char page_action[] = "obcimlgaoabeegjmmpldobjndiealpln";
180const char theme_crx[] = "iamefpfkojoapidjnbafmgkgncegbkad";
181const char theme2_crx[] = "pjpgmfcmabopnnfonnhmdjglfpjjfkbf";
182const char permissions_crx[] = "eagpmdpfmaekmmcejjbmjoecnejeiiin";
183const char unpacked[] = "cbcdidchbppangcjoddlpdjlenngjldk";
184const char updates_from_webstore[] = "akjooamlhcgeopfifcmlggaebeocgokj";
[email protected]df4956e2009-06-10 16:53:42185
[email protected]f0397fa2008-12-11 17:59:58186struct ExtensionsOrder {
[email protected]198b5902013-06-27 10:36:11187 bool operator()(const scoped_refptr<const Extension>& a,
188 const scoped_refptr<const Extension>& b) {
[email protected]f0397fa2008-12-11 17:59:58189 return a->name() < b->name();
190 }
191};
192
[email protected]d2065e062013-12-12 23:49:52193static std::vector<base::string16> GetErrors() {
194 const std::vector<base::string16>* errors =
[email protected]bb28e062009-02-27 17:19:18195 ExtensionErrorReporter::GetInstance()->GetErrors();
[email protected]d2065e062013-12-12 23:49:52196 std::vector<base::string16> ret_val;
[email protected]bb28e062009-02-27 17:19:18197
[email protected]d2065e062013-12-12 23:49:52198 for (std::vector<base::string16>::const_iterator iter = errors->begin();
[email protected]bb28e062009-02-27 17:19:18199 iter != errors->end(); ++iter) {
[email protected]04338722013-12-24 23:18:05200 std::string utf8_error = base::UTF16ToUTF8(*iter);
[email protected]fc670822011-12-17 09:33:49201 if (utf8_error.find(".svn") == std::string::npos) {
[email protected]bb28e062009-02-27 17:19:18202 ret_val.push_back(*iter);
203 }
204 }
205
206 // The tests rely on the errors being in a certain order, which can vary
207 // depending on how filesystem iteration works.
208 std::stable_sort(ret_val.begin(), ret_val.end());
209
210 return ret_val;
211}
212
[email protected]cced75a2011-05-20 08:31:12213static void AddPattern(URLPatternSet* extent, const std::string& pattern) {
[email protected]8d888c12010-11-30 00:00:25214 int schemes = URLPattern::SCHEME_ALL;
215 extent->AddPattern(URLPattern(schemes, pattern));
216}
[email protected]8d888c12010-11-30 00:00:25217
[email protected]650b2d52013-02-10 03:41:45218base::FilePath GetTemporaryFile() {
219 base::FilePath temp_file;
[email protected]03d9afc02013-12-03 17:55:52220 CHECK(base::CreateTemporaryFile(&temp_file));
[email protected]a17dfcf2012-12-30 02:07:09221 return temp_file;
222}
223
[email protected]6d057a0c2013-07-09 21:12:07224bool WaitForCountNotificationsCallback(int *count) {
225 return --(*count) == 0;
226}
[email protected]6d057a0c2013-07-09 21:12:07227
[email protected]f0397fa2008-12-11 17:59:58228} // namespace
[email protected]6014d672008-12-05 00:38:25229
[email protected]5df038b2012-07-16 19:03:27230class MockExtensionProvider : public extensions::ExternalProviderInterface {
[email protected]a1257b12009-06-12 02:51:34231 public:
[email protected]a12ce8b22012-01-17 18:40:53232 MockExtensionProvider(
[email protected]8e4560b62011-01-14 10:09:14233 VisitorInterface* visitor,
[email protected]1d5e58b2013-01-31 08:41:40234 Manifest::Location location)
[email protected]f121003b2012-05-04 21:57:47235 : location_(location), visitor_(visitor), visit_count_(0) {
[email protected]a12ce8b22012-01-17 18:40:53236 }
237
[email protected]a1257b12009-06-12 02:51:34238 virtual ~MockExtensionProvider() {}
239
240 void UpdateOrAddExtension(const std::string& id,
241 const std::string& version,
[email protected]650b2d52013-02-10 03:41:45242 const base::FilePath& path) {
[email protected]a1257b12009-06-12 02:51:34243 extension_map_[id] = std::make_pair(version, path);
244 }
245
246 void RemoveExtension(const std::string& id) {
247 extension_map_.erase(id);
248 }
249
[email protected]5df038b2012-07-16 19:03:27250 // ExternalProvider implementation:
[email protected]a12ce8b22012-01-17 18:40:53251 virtual void VisitRegisteredExtension() OVERRIDE {
[email protected]0a60a2e2010-10-25 16:15:21252 visit_count_++;
[email protected]a1257b12009-06-12 02:51:34253 for (DataMap::const_iterator i = extension_map_.begin();
254 i != extension_map_.end(); ++i) {
[email protected]c5e4a2222014-01-03 16:06:13255 Version version(i->second.first);
[email protected]a1257b12009-06-12 02:51:34256
[email protected]8e4560b62011-01-14 10:09:14257 visitor_->OnExternalExtensionFileFound(
[email protected]12126d372012-07-11 18:40:53258 i->first, &version, i->second.second, location_,
[email protected]f121003b2012-05-04 21:57:47259 Extension::NO_FLAGS, false);
[email protected]a1257b12009-06-12 02:51:34260 }
[email protected]50067e52011-10-20 23:17:07261 visitor_->OnExternalProviderReady(this);
[email protected]a1257b12009-06-12 02:51:34262 }
263
[email protected]a12ce8b22012-01-17 18:40:53264 virtual bool HasExtension(const std::string& id) const OVERRIDE {
[email protected]0a60a2e2010-10-25 16:15:21265 return extension_map_.find(id) != extension_map_.end();
266 }
267
[email protected]a12ce8b22012-01-17 18:40:53268 virtual bool GetExtensionDetails(
269 const std::string& id,
[email protected]1d5e58b2013-01-31 08:41:40270 Manifest::Location* location,
[email protected]c5e4a2222014-01-03 16:06:13271 scoped_ptr<Version>* version) const OVERRIDE {
[email protected]a1257b12009-06-12 02:51:34272 DataMap::const_iterator it = extension_map_.find(id);
273 if (it == extension_map_.end())
[email protected]0a60a2e2010-10-25 16:15:21274 return false;
275
276 if (version)
[email protected]c5e4a2222014-01-03 16:06:13277 version->reset(new Version(it->second.first));
[email protected]a1257b12009-06-12 02:51:34278
279 if (location)
280 *location = location_;
[email protected]0a60a2e2010-10-25 16:15:21281
282 return true;
283 }
[email protected]8e4560b62011-01-14 10:09:14284
[email protected]a12ce8b22012-01-17 18:40:53285 virtual bool IsReady() const OVERRIDE {
[email protected]8e4560b62011-01-14 10:09:14286 return true;
287 }
288
[email protected]a12ce8b22012-01-17 18:40:53289 virtual void ServiceShutdown() OVERRIDE {
[email protected]8e4560b62011-01-14 10:09:14290 }
291
[email protected]0a60a2e2010-10-25 16:15:21292 int visit_count() const { return visit_count_; }
293 void set_visit_count(int visit_count) {
294 visit_count_ = visit_count;
[email protected]a1257b12009-06-12 02:51:34295 }
296
297 private:
[email protected]650b2d52013-02-10 03:41:45298 typedef std::map< std::string, std::pair<std::string, base::FilePath> >
299 DataMap;
[email protected]a1257b12009-06-12 02:51:34300 DataMap extension_map_;
[email protected]1d5e58b2013-01-31 08:41:40301 Manifest::Location location_;
[email protected]8e4560b62011-01-14 10:09:14302 VisitorInterface* visitor_;
[email protected]27b985d2009-06-25 17:53:15303
[email protected]0a60a2e2010-10-25 16:15:21304 // visit_count_ tracks the number of calls to VisitRegisteredExtension().
305 // Mutable because it must be incremented on each call to
306 // VisitRegisteredExtension(), which must be a const method to inherit
307 // from the class being mocked.
308 mutable int visit_count_;
309
[email protected]27b985d2009-06-25 17:53:15310 DISALLOW_COPY_AND_ASSIGN(MockExtensionProvider);
311};
312
[email protected]8e4560b62011-01-14 10:09:14313class MockProviderVisitor
[email protected]5df038b2012-07-16 19:03:27314 : public extensions::ExternalProviderInterface::VisitorInterface {
[email protected]27b985d2009-06-25 17:53:15315 public:
[email protected]f0841cd2011-01-19 15:07:24316 // The provider will return |fake_base_path| from
317 // GetBaseCrxFilePath(). User can test the behavior with
318 // and without an empty path using this parameter.
[email protected]650b2d52013-02-10 03:41:45319 explicit MockProviderVisitor(base::FilePath fake_base_path)
[email protected]f0841cd2011-01-19 15:07:24320 : ids_found_(0),
[email protected]f121003b2012-05-04 21:57:47321 fake_base_path_(fake_base_path),
322 expected_creation_flags_(Extension::NO_FLAGS) {
[email protected]19eac6d2013-05-30 06:51:03323 profile_.reset(new TestingProfile);
[email protected]f121003b2012-05-04 21:57:47324 }
325
[email protected]650b2d52013-02-10 03:41:45326 MockProviderVisitor(base::FilePath fake_base_path,
327 int expected_creation_flags)
[email protected]f121003b2012-05-04 21:57:47328 : ids_found_(0),
329 fake_base_path_(fake_base_path),
330 expected_creation_flags_(expected_creation_flags) {
[email protected]27b985d2009-06-25 17:53:15331 }
332
[email protected]683d0702010-12-06 16:25:57333 int Visit(const std::string& json_data) {
[email protected]27b985d2009-06-25 17:53:15334 // Give the test json file to the provider for parsing.
[email protected]5df038b2012-07-16 19:03:27335 provider_.reset(new extensions::ExternalProviderImpl(
[email protected]8e4560b62011-01-14 10:09:14336 this,
[email protected]5df038b2012-07-16 19:03:27337 new extensions::ExternalTestingLoader(json_data, fake_base_path_),
[email protected]19eac6d2013-05-30 06:51:03338 profile_.get(),
[email protected]1d5e58b2013-01-31 08:41:40339 Manifest::EXTERNAL_PREF,
340 Manifest::EXTERNAL_PREF_DOWNLOAD,
[email protected]1bf73cc32011-10-26 22:38:31341 Extension::NO_FLAGS));
[email protected]27b985d2009-06-25 17:53:15342
343 // We also parse the file into a dictionary to compare what we get back
344 // from the provider.
345 JSONStringValueSerializer serializer(json_data);
[email protected]023b3d12013-12-23 18:46:49346 base::Value* json_value = serializer.Deserialize(NULL, NULL);
[email protected]27b985d2009-06-25 17:53:15347
[email protected]023b3d12013-12-23 18:46:49348 if (!json_value || !json_value->IsType(base::Value::TYPE_DICTIONARY)) {
[email protected]e2194742010-08-12 05:54:34349 NOTREACHED() << "Unable to deserialize json data";
[email protected]27b985d2009-06-25 17:53:15350 return -1;
351 } else {
[email protected]023b3d12013-12-23 18:46:49352 base::DictionaryValue* external_extensions =
353 static_cast<base::DictionaryValue*>(json_value);
[email protected]27b985d2009-06-25 17:53:15354 prefs_.reset(external_extensions);
355 }
356
357 // Reset our counter.
358 ids_found_ = 0;
[email protected]683d0702010-12-06 16:25:57359 // Ask the provider to look up all extensions and return them.
[email protected]8e4560b62011-01-14 10:09:14360 provider_->VisitRegisteredExtension();
[email protected]27b985d2009-06-25 17:53:15361
362 return ids_found_;
363 }
364
[email protected]9060d8b02012-01-13 02:14:30365 virtual bool OnExternalExtensionFileFound(const std::string& id,
[email protected]c5e4a2222014-01-03 16:06:13366 const Version* version,
[email protected]650b2d52013-02-10 03:41:45367 const base::FilePath& path,
[email protected]1d5e58b2013-01-31 08:41:40368 Manifest::Location unused,
[email protected]47fc70c2011-12-06 07:29:51369 int creation_flags,
[email protected]49aeab62013-02-07 02:53:11370 bool mark_acknowledged) OVERRIDE {
[email protected]f121003b2012-05-04 21:57:47371 EXPECT_EQ(expected_creation_flags_, creation_flags);
[email protected]1bf73cc32011-10-26 22:38:31372
[email protected]27b985d2009-06-25 17:53:15373 ++ids_found_;
[email protected]023b3d12013-12-23 18:46:49374 base::DictionaryValue* pref;
[email protected]27b985d2009-06-25 17:53:15375 // This tests is to make sure that the provider only notifies us of the
376 // values we gave it. So if the id we doesn't exist in our internal
377 // dictionary then something is wrong.
[email protected]e2194742010-08-12 05:54:34378 EXPECT_TRUE(prefs_->GetDictionary(id, &pref))
379 << "Got back ID (" << id.c_str() << ") we weren't expecting";
[email protected]27b985d2009-06-25 17:53:15380
[email protected]f0841cd2011-01-19 15:07:24381 EXPECT_TRUE(path.IsAbsolute());
382 if (!fake_base_path_.empty())
383 EXPECT_TRUE(fake_base_path_.IsParent(path));
384
[email protected]27b985d2009-06-25 17:53:15385 if (pref) {
[email protected]0a60a2e2010-10-25 16:15:21386 EXPECT_TRUE(provider_->HasExtension(id));
387
[email protected]27b985d2009-06-25 17:53:15388 // Ask provider if the extension we got back is registered.
[email protected]1d5e58b2013-01-31 08:41:40389 Manifest::Location location = Manifest::INVALID_LOCATION;
[email protected]c5e4a2222014-01-03 16:06:13390 scoped_ptr<Version> v1;
[email protected]650b2d52013-02-10 03:41:45391 base::FilePath crx_path;
[email protected]0a60a2e2010-10-25 16:15:21392
393 EXPECT_TRUE(provider_->GetExtensionDetails(id, NULL, &v1));
394 EXPECT_STREQ(version->GetString().c_str(), v1->GetString().c_str());
395
[email protected]c5e4a2222014-01-03 16:06:13396 scoped_ptr<Version> v2;
[email protected]0a60a2e2010-10-25 16:15:21397 EXPECT_TRUE(provider_->GetExtensionDetails(id, &location, &v2));
[email protected]27b985d2009-06-25 17:53:15398 EXPECT_STREQ(version->GetString().c_str(), v1->GetString().c_str());
399 EXPECT_STREQ(version->GetString().c_str(), v2->GetString().c_str());
[email protected]1d5e58b2013-01-31 08:41:40400 EXPECT_EQ(Manifest::EXTERNAL_PREF, location);
[email protected]27b985d2009-06-25 17:53:15401
402 // Remove it so we won't count it ever again.
[email protected]e2194742010-08-12 05:54:34403 prefs_->Remove(id, NULL);
[email protected]27b985d2009-06-25 17:53:15404 }
[email protected]9060d8b02012-01-13 02:14:30405 return true;
[email protected]27b985d2009-06-25 17:53:15406 }
407
[email protected]9060d8b02012-01-13 02:14:30408 virtual bool OnExternalExtensionUpdateUrlFound(
[email protected]21a5a672010-11-04 10:47:42409 const std::string& id, const GURL& update_url,
[email protected]464213a2013-10-15 01:06:48410 Manifest::Location location,
411 int creation_flags,
412 bool mark_acknowledged) OVERRIDE {
[email protected]8ef78fd2010-08-19 17:14:32413 ++ids_found_;
[email protected]023b3d12013-12-23 18:46:49414 base::DictionaryValue* pref;
[email protected]8ef78fd2010-08-19 17:14:32415 // This tests is to make sure that the provider only notifies us of the
416 // values we gave it. So if the id we doesn't exist in our internal
417 // dictionary then something is wrong.
418 EXPECT_TRUE(prefs_->GetDictionary(id, &pref))
419 << L"Got back ID (" << id.c_str() << ") we weren't expecting";
[email protected]1d5e58b2013-01-31 08:41:40420 EXPECT_EQ(Manifest::EXTERNAL_PREF_DOWNLOAD, location);
[email protected]8ef78fd2010-08-19 17:14:32421
422 if (pref) {
[email protected]0a60a2e2010-10-25 16:15:21423 EXPECT_TRUE(provider_->HasExtension(id));
424
425 // External extensions with update URLs do not have versions.
[email protected]c5e4a2222014-01-03 16:06:13426 scoped_ptr<Version> v1;
[email protected]1d5e58b2013-01-31 08:41:40427 Manifest::Location location1 = Manifest::INVALID_LOCATION;
[email protected]21a5a672010-11-04 10:47:42428 EXPECT_TRUE(provider_->GetExtensionDetails(id, &location1, &v1));
[email protected]0a60a2e2010-10-25 16:15:21429 EXPECT_FALSE(v1.get());
[email protected]1d5e58b2013-01-31 08:41:40430 EXPECT_EQ(Manifest::EXTERNAL_PREF_DOWNLOAD, location1);
[email protected]0a60a2e2010-10-25 16:15:21431
[email protected]8ef78fd2010-08-19 17:14:32432 // Remove it so we won't count it again.
433 prefs_->Remove(id, NULL);
434 }
[email protected]9060d8b02012-01-13 02:14:30435 return true;
[email protected]8ef78fd2010-08-19 17:14:32436 }
437
[email protected]50067e52011-10-20 23:17:07438 virtual void OnExternalProviderReady(
[email protected]49aeab62013-02-07 02:53:11439 const extensions::ExternalProviderInterface* provider) OVERRIDE {
[email protected]50067e52011-10-20 23:17:07440 EXPECT_EQ(provider, provider_.get());
441 EXPECT_TRUE(provider->IsReady());
[email protected]8e4560b62011-01-14 10:09:14442 }
443
[email protected]27b985d2009-06-25 17:53:15444 private:
445 int ids_found_;
[email protected]650b2d52013-02-10 03:41:45446 base::FilePath fake_base_path_;
[email protected]f121003b2012-05-04 21:57:47447 int expected_creation_flags_;
[email protected]5df038b2012-07-16 19:03:27448 scoped_ptr<extensions::ExternalProviderImpl> provider_;
[email protected]023b3d12013-12-23 18:46:49449 scoped_ptr<base::DictionaryValue> prefs_;
[email protected]19eac6d2013-05-30 06:51:03450 scoped_ptr<TestingProfile> profile_;
[email protected]27b985d2009-06-25 17:53:15451
452 DISALLOW_COPY_AND_ASSIGN(MockProviderVisitor);
[email protected]a1257b12009-06-12 02:51:34453};
454
[email protected]b3aa7182013-04-25 04:45:23455ExtensionServiceTestBase::ExtensionServiceInitParams::
456ExtensionServiceInitParams()
[email protected]371662e372013-10-17 22:05:22457 : autoupdate_enabled(false), is_first_run(true), profile_is_managed(false) {
[email protected]b3aa7182013-04-25 04:45:23458}
459
[email protected]bf73f0b2010-02-10 19:26:59460// Our message loop may be used in tests which require it to be an IO loop.
[email protected]a5768512013-04-12 19:35:35461ExtensionServiceTestBase::ExtensionServiceTestBase()
[email protected]eedc1c752013-08-09 18:51:55462 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP),
[email protected]32e2e9b2011-11-18 18:56:45463 service_(NULL),
[email protected]65187152012-06-02 13:14:14464 management_policy_(NULL),
[email protected]eedc1c752013-08-09 18:51:55465 expected_extensions_count_(0) {
[email protected]650b2d52013-02-10 03:41:45466 base::FilePath test_data_dir;
[email protected]e85e34c32011-04-13 18:38:35467 if (!PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir)) {
468 ADD_FAILURE();
469 return;
470 }
471 data_dir_ = test_data_dir.AppendASCII("extensions");
[email protected]bf73f0b2010-02-10 19:26:59472}
473
[email protected]eaa7dd182010-12-14 11:09:00474ExtensionServiceTestBase::~ExtensionServiceTestBase() {
[email protected]bf73f0b2010-02-10 19:26:59475 service_ = NULL;
[email protected]bf73f0b2010-02-10 19:26:59476}
477
[email protected]eaa7dd182010-12-14 11:09:00478void ExtensionServiceTestBase::InitializeExtensionService(
[email protected]b3aa7182013-04-25 04:45:23479 const ExtensionServiceTestBase::ExtensionServiceInitParams& params) {
[email protected]82ac12c22013-11-21 13:05:21480 profile_ = CreateTestingProfile(params);
481 service_ = InitializeExtensionServiceForProfile(params, profile_.get());
482 management_policy_ =
483 ExtensionSystem::Get(profile_.get())->management_policy();
484 extensions_install_dir_ = params.extensions_install_dir;
485 expected_extensions_count_ = 0;
486}
487
488// static
489scoped_ptr<TestingProfile> ExtensionServiceTestBase::CreateTestingProfile(
490 const ExtensionServiceInitParams& params) {
[email protected]bb05cae12012-09-06 00:37:52491 TestingProfile::Builder profile_builder;
[email protected]f2d1f612010-12-09 15:10:17492 // Create a PrefService that only contains user defined preference values.
[email protected]e90a01f2013-11-19 04:22:12493 PrefServiceMockFactory factory;
[email protected]a50e38f2013-07-22 16:47:00494 // If pref_file is empty, TestingProfile automatically creates
495 // TestingPrefServiceSyncable instance.
496 if (!params.pref_file.empty()) {
[email protected]e90a01f2013-11-19 04:22:12497 factory.SetUserPrefsFile(params.pref_file,
498 base::MessageLoopProxy::current().get());
[email protected]a50e38f2013-07-22 16:47:00499 scoped_refptr<user_prefs::PrefRegistrySyncable> registry(
500 new user_prefs::PrefRegistrySyncable);
501 scoped_ptr<PrefServiceSyncable> prefs(
[email protected]e90a01f2013-11-19 04:22:12502 factory.CreateSyncable(registry.get()));
[email protected]a50e38f2013-07-22 16:47:00503 chrome::RegisterUserProfilePrefs(registry.get());
504 profile_builder.SetPrefService(prefs.Pass());
505 }
[email protected]371662e372013-10-17 22:05:22506
507 if (params.profile_is_managed)
508 profile_builder.SetManagedUserId("asdf");
509
[email protected]b3aa7182013-04-25 04:45:23510 profile_builder.SetPath(params.profile_path);
[email protected]82ac12c22013-11-21 13:05:21511 return profile_builder.Build();
512}
[email protected]bf73f0b2010-02-10 19:26:59513
[email protected]82ac12c22013-11-21 13:05:21514// static
515ExtensionService*
516ExtensionServiceTestBase::InitializeExtensionServiceForProfile(
517 const ExtensionServiceInitParams& params,
518 Profile* profile) {
[email protected]b3aa7182013-04-25 04:45:23519 TestExtensionSystem* system = static_cast<TestExtensionSystem*>(
[email protected]82ac12c22013-11-21 13:05:21520 ExtensionSystem::Get(profile));
[email protected]b3aa7182013-04-25 04:45:23521 if (!params.is_first_run) {
522 ExtensionPrefs* prefs = system->CreateExtensionPrefs(
523 CommandLine::ForCurrentProcess(),
524 params.extensions_install_dir);
525 prefs->SetAlertSystemFirstRun();
526 }
527
[email protected]82ac12c22013-11-21 13:05:21528 ExtensionService* service = system->CreateExtensionService(
[email protected]b3aa7182013-04-25 04:45:23529 CommandLine::ForCurrentProcess(),
530 params.extensions_install_dir,
531 params.autoupdate_enabled);
[email protected]f8aefb132013-10-30 09:29:52532
[email protected]82ac12c22013-11-21 13:05:21533 service->SetFileTaskRunnerForTesting(
[email protected]eedc1c752013-08-09 18:51:55534 base::MessageLoopProxy::current().get());
[email protected]82ac12c22013-11-21 13:05:21535 service->set_extensions_enabled(true);
536 service->set_show_extensions_prompts(false);
537 service->set_install_updates_when_idle_for_test(false);
[email protected]371662e372013-10-17 22:05:22538
[email protected]bf73f0b2010-02-10 19:26:59539 // When we start up, we want to make sure there is no external provider,
540 // since the ExtensionService on Windows will use the Registry as a default
541 // provider and if there is something already registered there then it will
542 // interfere with the tests. Those tests that need an external provider
543 // will register one specifically.
[email protected]82ac12c22013-11-21 13:05:21544 service->ClearProvidersForTesting();
[email protected]bf73f0b2010-02-10 19:26:59545
[email protected]88e8ec9152013-01-17 04:05:18546#if defined(OS_CHROMEOS)
[email protected]82ac12c22013-11-21 13:05:21547 extensions::InstallLimiter::Get(profile)->DisableForTest();
[email protected]88e8ec9152013-01-17 04:05:18548#endif
[email protected]82ac12c22013-11-21 13:05:21549 return service;
[email protected]bf73f0b2010-02-10 19:26:59550}
551
[email protected]eaa7dd182010-12-14 11:09:00552void ExtensionServiceTestBase::InitializeInstalledExtensionService(
[email protected]650b2d52013-02-10 03:41:45553 const base::FilePath& prefs_file,
554 const base::FilePath& source_install_dir) {
[email protected]371662e372013-10-17 22:05:22555 EXPECT_TRUE(temp_dir_.CreateUniqueTempDir());
[email protected]650b2d52013-02-10 03:41:45556 base::FilePath path = temp_dir_.path();
[email protected]bb05cae12012-09-06 00:37:52557 path = path.Append(FILE_PATH_LITERAL("TestingExtensionsPath"));
[email protected]371662e372013-10-17 22:05:22558 EXPECT_TRUE(base::DeleteFile(path, true));
[email protected]54124ed02014-01-07 10:06:58559 base::File::Error error = base::File::FILE_OK;
[email protected]426d1c92013-12-03 20:08:54560 EXPECT_TRUE(base::CreateDirectoryAndGetError(path, &error)) << error;
[email protected]650b2d52013-02-10 03:41:45561 base::FilePath temp_prefs = path.Append(FILE_PATH_LITERAL("Preferences"));
[email protected]371662e372013-10-17 22:05:22562 EXPECT_TRUE(base::CopyFile(prefs_file, temp_prefs));
[email protected]bf73f0b2010-02-10 19:26:59563
[email protected]371662e372013-10-17 22:05:22564 base::FilePath extensions_install_dir =
565 path.Append(FILE_PATH_LITERAL("Extensions"));
566 EXPECT_TRUE(base::DeleteFile(extensions_install_dir, true));
567 EXPECT_TRUE(
568 base::CopyDirectory(source_install_dir, extensions_install_dir, true));
[email protected]bf73f0b2010-02-10 19:26:59569
[email protected]b3aa7182013-04-25 04:45:23570 ExtensionServiceInitParams params;
571 params.profile_path = path;
572 params.pref_file = temp_prefs;
[email protected]371662e372013-10-17 22:05:22573 params.extensions_install_dir = extensions_install_dir;
[email protected]b3aa7182013-04-25 04:45:23574 InitializeExtensionService(params);
[email protected]bf73f0b2010-02-10 19:26:59575}
576
[email protected]3f2a2fa2013-09-24 02:55:25577void ExtensionServiceTestBase::InitializeGoodInstalledExtensionService() {
578 base::FilePath source_install_dir = data_dir_
579 .AppendASCII("good")
580 .AppendASCII("Extensions");
581 base::FilePath pref_path = source_install_dir
582 .DirName()
583 .AppendASCII("Preferences");
584 InitializeInstalledExtensionService(pref_path, source_install_dir);
585}
586
[email protected]eaa7dd182010-12-14 11:09:00587void ExtensionServiceTestBase::InitializeEmptyExtensionService() {
[email protected]371662e372013-10-17 22:05:22588 InitializeExtensionService(CreateDefaultInitParams());
[email protected]90310d92011-04-17 07:35:04589}
590
[email protected]98b6d942013-11-10 00:34:07591void ExtensionServiceTestBase::InitializeProcessManager() {
[email protected]bd306722012-07-11 20:43:59592 static_cast<extensions::TestExtensionSystem*>(
[email protected]749d59a2012-04-05 00:23:24593 ExtensionSystem::Get(profile_.get()))->
[email protected]98b6d942013-11-10 00:34:07594 CreateProcessManager();
[email protected]406b5a92011-11-08 11:58:26595}
596
[email protected]90310d92011-04-17 07:35:04597void ExtensionServiceTestBase::InitializeExtensionServiceWithUpdater() {
[email protected]371662e372013-10-17 22:05:22598 ExtensionServiceInitParams params = CreateDefaultInitParams();
599 params.autoupdate_enabled = true;
600 InitializeExtensionService(params);
[email protected]90310d92011-04-17 07:35:04601 service_->updater()->Start();
602}
603
[email protected]f8aefb132013-10-30 09:29:52604void ExtensionServiceTestBase::InitializeExtensionSyncService() {
605 extension_sync_service_.reset(new ExtensionSyncService(
606 profile_.get(), service_->extension_prefs(), service_));
607}
608
609// static
610void ExtensionServiceTestBase::SetUpTestCase() {
611 ExtensionErrorReporter::Init(false); // no noisy errors
612}
613
614void ExtensionServiceTestBase::SetUp() {
615 ExtensionErrorReporter::GetInstance()->ClearErrors();
[email protected]f8aefb132013-10-30 09:29:52616}
617
618void ExtensionServiceTestBase::TearDown() {
[email protected]f8aefb132013-10-30 09:29:52619}
620
[email protected]371662e372013-10-17 22:05:22621ExtensionServiceTestBase::ExtensionServiceInitParams
622ExtensionServiceTestBase::CreateDefaultInitParams() {
[email protected]82ac12c22013-11-21 13:05:21623 return CreateDefaultInitParamsInTempDir(&temp_dir_);
624}
625
626// static
627ExtensionServiceTestBase::ExtensionServiceInitParams
628ExtensionServiceTestBase::CreateDefaultInitParamsInTempDir(
629 base::ScopedTempDir* temp_dir) {
[email protected]371662e372013-10-17 22:05:22630 ExtensionServiceInitParams params;
[email protected]82ac12c22013-11-21 13:05:21631 EXPECT_TRUE(temp_dir->CreateUniqueTempDir());
632 base::FilePath path = temp_dir->path();
[email protected]bb05cae12012-09-06 00:37:52633 path = path.Append(FILE_PATH_LITERAL("TestingExtensionsPath"));
[email protected]371662e372013-10-17 22:05:22634 EXPECT_TRUE(base::DeleteFile(path, true));
[email protected]54124ed02014-01-07 10:06:58635 base::File::Error error = base::File::FILE_OK;
[email protected]426d1c92013-12-03 20:08:54636 EXPECT_TRUE(base::CreateDirectoryAndGetError(path, &error)) << error;
[email protected]650b2d52013-02-10 03:41:45637 base::FilePath prefs_filename =
638 path.Append(FILE_PATH_LITERAL("TestPreferences"));
[email protected]371662e372013-10-17 22:05:22639 base::FilePath extensions_install_dir =
640 path.Append(FILE_PATH_LITERAL("Extensions"));
641 EXPECT_TRUE(base::DeleteFile(extensions_install_dir, true));
[email protected]426d1c92013-12-03 20:08:54642 EXPECT_TRUE(base::CreateDirectoryAndGetError(extensions_install_dir,
643 &error)) << error;
[email protected]bf73f0b2010-02-10 19:26:59644
[email protected]b3aa7182013-04-25 04:45:23645 params.profile_path = path;
646 params.pref_file = prefs_filename;
[email protected]371662e372013-10-17 22:05:22647 params.extensions_install_dir = extensions_install_dir;
648 return params;
[email protected]bf73f0b2010-02-10 19:26:59649}
650
[email protected]eaa7dd182010-12-14 11:09:00651class ExtensionServiceTest
[email protected]6c2381d2011-10-19 02:52:53652 : public ExtensionServiceTestBase, public content::NotificationObserver {
[email protected]bf73f0b2010-02-10 19:26:59653 public:
[email protected]612a1cb12012-10-17 13:18:03654 ExtensionServiceTest()
655 : installed_(NULL),
[email protected]41bb80bd2013-05-03 10:56:02656 was_update_(false),
[email protected]612a1cb12012-10-17 13:18:03657 override_external_install_prompt_(
[email protected]00b5d0a52012-10-30 13:13:53658 FeatureSwitch::prompt_for_external_extensions(), false) {
[email protected]432115822011-07-10 15:52:27659 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_LOADED,
[email protected]ad50def52011-10-19 23:17:07660 content::NotificationService::AllSources());
[email protected]432115822011-07-10 15:52:27661 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED,
[email protected]ad50def52011-10-19 23:17:07662 content::NotificationService::AllSources());
[email protected]432115822011-07-10 15:52:27663 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_INSTALLED,
[email protected]ad50def52011-10-19 23:17:07664 content::NotificationService::AllSources());
[email protected]a9b00ac2009-06-25 21:03:23665 }
[email protected]cc655912009-01-29 23:19:19666
[email protected]432115822011-07-10 15:52:27667 virtual void Observe(int type,
[email protected]6c2381d2011-10-19 02:52:53668 const content::NotificationSource& source,
[email protected]49aeab62013-02-07 02:53:11669 const content::NotificationDetails& details) OVERRIDE {
[email protected]432115822011-07-10 15:52:27670 switch (type) {
671 case chrome::NOTIFICATION_EXTENSION_LOADED: {
[email protected]6c2381d2011-10-19 02:52:53672 const Extension* extension =
673 content::Details<const Extension>(details).ptr();
[email protected]00cd9c42010-11-02 20:15:57674 loaded_.push_back(make_scoped_refptr(extension));
[email protected]894bb502009-05-21 22:39:57675 // The tests rely on the errors being in a certain order, which can vary
676 // depending on how filesystem iteration works.
677 std::stable_sort(loaded_.begin(), loaded_.end(), ExtensionsOrder());
678 break;
679 }
680
[email protected]432115822011-07-10 15:52:27681 case chrome::NOTIFICATION_EXTENSION_UNLOADED: {
[email protected]a9f39a312010-12-23 22:14:27682 const Extension* e =
[email protected]1c321ee52012-05-21 03:02:34683 content::Details<extensions::UnloadedExtensionInfo>(
684 details)->extension;
[email protected]9f1087e2009-06-15 17:29:32685 unloaded_id_ = e->id();
[email protected]1c321ee52012-05-21 03:02:34686 extensions::ExtensionList::iterator i =
[email protected]9f1087e2009-06-15 17:29:32687 std::find(loaded_.begin(), loaded_.end(), e);
688 // TODO(erikkay) fix so this can be an assert. Right now the tests
689 // are manually calling clear() on loaded_, so this isn't doable.
690 if (i == loaded_.end())
691 return;
692 loaded_.erase(i);
[email protected]894bb502009-05-21 22:39:57693 break;
[email protected]9f1087e2009-06-15 17:29:32694 }
[email protected]41bb80bd2013-05-03 10:56:02695 case chrome::NOTIFICATION_EXTENSION_INSTALLED: {
696 const extensions::InstalledExtensionInfo* installed_info =
697 content::Details<const extensions::InstalledExtensionInfo>(details)
698 .ptr();
699 installed_ = installed_info->extension;
700 was_update_ = installed_info->is_update;
[email protected]e7554c3f2013-05-29 00:36:56701 old_name_ = installed_info->old_name;
[email protected]894bb502009-05-21 22:39:57702 break;
[email protected]41bb80bd2013-05-03 10:56:02703 }
[email protected]894bb502009-05-21 22:39:57704
[email protected]894bb502009-05-21 22:39:57705 default:
706 DCHECK(false);
[email protected]3acbd422008-12-08 18:25:00707 }
708 }
709
[email protected]5df038b2012-07-16 19:03:27710 void AddMockExternalProvider(
711 extensions::ExternalProviderInterface* provider) {
[email protected]0a60a2e2010-10-25 16:15:21712 service_->AddProviderForTesting(provider);
[email protected]a1257b12009-06-12 02:51:34713 }
714
[email protected]6d147912013-07-09 08:02:14715 void MockSyncStartFlare(bool* was_called,
716 syncer::ModelType* model_type_passed_in,
717 syncer::ModelType model_type) {
718 *was_called = true;
719 *model_type_passed_in = model_type;
720 }
721
[email protected]9197f3b2009-06-02 00:49:27722 protected:
[email protected]3f2a2fa2013-09-24 02:55:25723 // Paths to some of the fake extensions.
724 base::FilePath good0_path() {
725 return data_dir_.AppendASCII("good").AppendASCII("Extensions")
726 .AppendASCII(good0).AppendASCII("1.0.0.0");
727 }
728
729 base::FilePath good1_path() {
730 return data_dir_.AppendASCII("good").AppendASCII("Extensions")
731 .AppendASCII(good1).AppendASCII("2");
732 }
733
734 base::FilePath good2_path() {
735 return data_dir_.AppendASCII("good").AppendASCII("Extensions")
736 .AppendASCII(good2).AppendASCII("1.0");
737 }
738
[email protected]d55e7602009-12-16 04:20:42739 void TestExternalProvider(MockExtensionProvider* provider,
[email protected]1d5e58b2013-01-31 08:41:40740 Manifest::Location location);
[email protected]d55e7602009-12-16 04:20:42741
[email protected]650b2d52013-02-10 03:41:45742 void PackCRX(const base::FilePath& dir_path,
743 const base::FilePath& pem_path,
744 const base::FilePath& crx_path) {
[email protected]8d888c12010-11-30 00:00:25745 // Use the existing pem key, if provided.
[email protected]650b2d52013-02-10 03:41:45746 base::FilePath pem_output_path;
[email protected]8d888c12010-11-30 00:00:25747 if (pem_path.value().empty()) {
748 pem_output_path = crx_path.DirName().AppendASCII("temp.pem");
[email protected]8d888c12010-11-30 00:00:25749 } else {
[email protected]7567484142013-07-11 17:36:07750 ASSERT_TRUE(base::PathExists(pem_path));
[email protected]8d888c12010-11-30 00:00:25751 }
[email protected]3ba0fd32010-06-19 05:39:10752
[email protected]dd3aa792013-07-16 19:10:23753 ASSERT_TRUE(base::DeleteFile(crx_path, false));
[email protected]8d888c12010-11-30 00:00:25754
[email protected]3ba0fd32010-06-19 05:39:10755 scoped_ptr<ExtensionCreator> creator(new ExtensionCreator());
[email protected]8d888c12010-11-30 00:00:25756 ASSERT_TRUE(creator->Run(dir_path,
757 crx_path,
758 pem_path,
[email protected]93d973a2012-01-08 07:38:26759 pem_output_path,
760 ExtensionCreator::kOverwriteCRX));
[email protected]8d888c12010-11-30 00:00:25761
[email protected]7567484142013-07-11 17:36:07762 ASSERT_TRUE(base::PathExists(crx_path));
[email protected]8d888c12010-11-30 00:00:25763 }
764
[email protected]8f512c72011-11-22 21:02:50765 enum InstallState {
766 INSTALL_FAILED,
767 INSTALL_UPDATED,
[email protected]695b5712012-12-06 23:55:28768 INSTALL_NEW,
769 INSTALL_WITHOUT_LOAD,
[email protected]8f512c72011-11-22 21:02:50770 };
771
[email protected]650b2d52013-02-10 03:41:45772 const Extension* PackAndInstallCRX(const base::FilePath& dir_path,
773 const base::FilePath& pem_path,
[email protected]be083862012-09-01 03:53:45774 InstallState install_state,
775 int creation_flags) {
[email protected]650b2d52013-02-10 03:41:45776 base::FilePath crx_path;
[email protected]ea1a3f62012-11-16 20:34:23777 base::ScopedTempDir temp_dir;
[email protected]8f512c72011-11-22 21:02:50778 EXPECT_TRUE(temp_dir.CreateUniqueTempDir());
779 crx_path = temp_dir.path().AppendASCII("temp.crx");
780
781 PackCRX(dir_path, pem_path, crx_path);
[email protected]be083862012-09-01 03:53:45782 return InstallCRX(crx_path, install_state, creation_flags);
783 }
784
[email protected]650b2d52013-02-10 03:41:45785 const Extension* PackAndInstallCRX(const base::FilePath& dir_path,
786 const base::FilePath& pem_path,
[email protected]be083862012-09-01 03:53:45787 InstallState install_state) {
788 return PackAndInstallCRX(dir_path, pem_path, install_state,
789 Extension::NO_FLAGS);
[email protected]145a317b2011-04-12 16:03:46790 }
791
[email protected]650b2d52013-02-10 03:41:45792 const Extension* PackAndInstallCRX(const base::FilePath& dir_path,
[email protected]8f512c72011-11-22 21:02:50793 InstallState install_state) {
[email protected]650b2d52013-02-10 03:41:45794 return PackAndInstallCRX(dir_path, base::FilePath(), install_state,
[email protected]be083862012-09-01 03:53:45795 Extension::NO_FLAGS);
796 }
797
[email protected]e7554c3f2013-05-29 00:36:56798 // Attempts to install an extension. Use INSTALL_FAILED if the installation
799 // is expected to fail.
800 // If |install_state| is INSTALL_UPDATED, and |expected_old_name| is
801 // non-empty, expects that the existing extension's title was
802 // |expected_old_name|.
803 const Extension* InstallCRX(const base::FilePath& path,
804 InstallState install_state,
805 int creation_flags,
806 const std::string& expected_old_name) {
[email protected]97d6a5c2013-11-11 23:51:24807 InstallCRXInternal(path, creation_flags);
808 return VerifyCrxInstall(path, install_state, expected_old_name);
[email protected]e7554c3f2013-05-29 00:36:56809 }
810
811 // Attempts to install an extension. Use INSTALL_FAILED if the installation
812 // is expected to fail.
[email protected]650b2d52013-02-10 03:41:45813 const Extension* InstallCRX(const base::FilePath& path,
[email protected]be083862012-09-01 03:53:45814 InstallState install_state,
815 int creation_flags) {
[email protected]97d6a5c2013-11-11 23:51:24816 return InstallCRX(path, install_state, creation_flags, std::string());
[email protected]8f512c72011-11-22 21:02:50817 }
818
[email protected]65187152012-06-02 13:14:14819 // Attempts to install an extension. Use INSTALL_FAILED if the installation
820 // is expected to fail.
[email protected]650b2d52013-02-10 03:41:45821 const Extension* InstallCRX(const base::FilePath& path,
[email protected]8f512c72011-11-22 21:02:50822 InstallState install_state) {
[email protected]be083862012-09-01 03:53:45823 return InstallCRX(path, install_state, Extension::NO_FLAGS);
[email protected]8f512c72011-11-22 21:02:50824 }
825
[email protected]650b2d52013-02-10 03:41:45826 const Extension* InstallCRXFromWebStore(const base::FilePath& path,
[email protected]8f512c72011-11-22 21:02:50827 InstallState install_state) {
[email protected]97d6a5c2013-11-11 23:51:24828 InstallCRXInternal(path, Extension::FROM_WEBSTORE);
829 return VerifyCrxInstall(path, install_state);
[email protected]8f512c72011-11-22 21:02:50830 }
831
[email protected]650b2d52013-02-10 03:41:45832 const Extension* InstallCRXWithLocation(const base::FilePath& crx_path,
[email protected]1d5e58b2013-01-31 08:41:40833 Manifest::Location install_location,
[email protected]8f512c72011-11-22 21:02:50834 InstallState install_state) {
[email protected]7567484142013-07-11 17:36:07835 EXPECT_TRUE(base::PathExists(crx_path))
[email protected]145a317b2011-04-12 16:03:46836 << "Path does not exist: "<< crx_path.value().c_str();
[email protected]14908b72011-04-20 06:54:36837 // no client (silent install)
[email protected]f8636f92013-08-09 21:02:37838 scoped_refptr<CrxInstaller> installer(CrxInstaller::CreateSilent(service_));
[email protected]145a317b2011-04-12 16:03:46839 installer->set_install_source(install_location);
[email protected]145a317b2011-04-12 16:03:46840
[email protected]97d6a5c2013-11-11 23:51:24841 content::WindowedNotificationObserver observer(
842 chrome::NOTIFICATION_CRX_INSTALLER_DONE,
843 content::NotificationService::AllSources());
844 installer->InstallCrx(crx_path);
845 observer.Wait();
846
847 return VerifyCrxInstall(crx_path, install_state);
[email protected]145a317b2011-04-12 16:03:46848 }
849
[email protected]97d6a5c2013-11-11 23:51:24850 // Verifies the result of a CRX installation. Used by InstallCRX. Set the
[email protected]65187152012-06-02 13:14:14851 // |install_state| to INSTALL_FAILED if the installation is expected to fail.
[email protected]8f512c72011-11-22 21:02:50852 // Returns an Extension pointer if the install succeeded, NULL otherwise.
[email protected]97d6a5c2013-11-11 23:51:24853 const Extension* VerifyCrxInstall(const base::FilePath& path,
854 InstallState install_state) {
855 return VerifyCrxInstall(path, install_state, std::string());
[email protected]e7554c3f2013-05-29 00:36:56856 }
857
[email protected]97d6a5c2013-11-11 23:51:24858 // Verifies the result of a CRX installation. Used by InstallCRX. Set the
[email protected]e7554c3f2013-05-29 00:36:56859 // |install_state| to INSTALL_FAILED if the installation is expected to fail.
860 // If |install_state| is INSTALL_UPDATED, and |expected_old_name| is
861 // non-empty, expects that the existing extension's title was
862 // |expected_old_name|.
863 // Returns an Extension pointer if the install succeeded, NULL otherwise.
[email protected]97d6a5c2013-11-11 23:51:24864 const Extension* VerifyCrxInstall(const base::FilePath& path,
865 InstallState install_state,
866 const std::string& expected_old_name) {
[email protected]d2065e062013-12-12 23:49:52867 std::vector<base::string16> errors = GetErrors();
[email protected]8f512c72011-11-22 21:02:50868 const Extension* extension = NULL;
869 if (install_state != INSTALL_FAILED) {
870 if (install_state == INSTALL_NEW)
871 ++expected_extensions_count_;
[email protected]902f7cd2009-05-22 19:02:19872
[email protected]a57209872009-05-04 22:53:14873 EXPECT_TRUE(installed_) << path.value();
[email protected]41bb80bd2013-05-03 10:56:02874 // If and only if INSTALL_UPDATED, it should have the is_update flag.
875 EXPECT_EQ(install_state == INSTALL_UPDATED, was_update_)
876 << path.value();
[email protected]e7554c3f2013-05-29 00:36:56877 // If INSTALL_UPDATED, old_name_ should match the given string.
878 if (install_state == INSTALL_UPDATED && !expected_old_name.empty())
879 EXPECT_EQ(expected_old_name, old_name_);
[email protected]bb28e062009-02-27 17:19:18880 EXPECT_EQ(0u, errors.size()) << path.value();
[email protected]695b5712012-12-06 23:55:28881
882 if (install_state == INSTALL_WITHOUT_LOAD) {
883 EXPECT_EQ(0u, loaded_.size()) << path.value();
884 } else {
885 EXPECT_EQ(1u, loaded_.size()) << path.value();
[email protected]b1ac70462013-08-14 21:33:30886 size_t actual_extension_count = service_->extensions()->size() +
887 service_->disabled_extensions()->size();
888 EXPECT_EQ(expected_extensions_count_, actual_extension_count) <<
[email protected]695b5712012-12-06 23:55:28889 path.value();
[email protected]dc24976f2013-06-02 21:15:09890 extension = loaded_[0].get();
891 EXPECT_TRUE(service_->GetExtensionById(extension->id(), false))
892 << path.value();
[email protected]695b5712012-12-06 23:55:28893 }
894
[email protected]d2065e062013-12-12 23:49:52895 for (std::vector<base::string16>::iterator err = errors.begin();
[email protected]bb28e062009-02-27 17:19:18896 err != errors.end(); ++err) {
[email protected]37eeb5a2009-02-26 23:36:17897 LOG(ERROR) << *err;
898 }
[email protected]cc655912009-01-29 23:19:19899 } else {
[email protected]a57209872009-05-04 22:53:14900 EXPECT_FALSE(installed_) << path.value();
[email protected]86a274072009-06-11 02:06:45901 EXPECT_EQ(0u, loaded_.size()) << path.value();
[email protected]bb28e062009-02-27 17:19:18902 EXPECT_EQ(1u, errors.size()) << path.value();
[email protected]cc655912009-01-29 23:19:19903 }
[email protected]bb28e062009-02-27 17:19:18904
[email protected]a57209872009-05-04 22:53:14905 installed_ = NULL;
[email protected]41bb80bd2013-05-03 10:56:02906 was_update_ = false;
[email protected]e7554c3f2013-05-29 00:36:56907 old_name_ = "";
[email protected]894bb502009-05-21 22:39:57908 loaded_.clear();
[email protected]bb28e062009-02-27 17:19:18909 ExtensionErrorReporter::GetInstance()->ClearErrors();
[email protected]8f512c72011-11-22 21:02:50910 return extension;
[email protected]cc655912009-01-29 23:19:19911 }
912
[email protected]4416c5a2010-06-26 01:28:57913 enum UpdateState {
914 FAILED_SILENTLY,
915 FAILED,
916 UPDATED,
917 INSTALLED,
918 ENABLED
919 };
920
[email protected]98270432012-09-11 20:51:24921 void BlackListWebGL() {
922 static const std::string json_blacklist =
923 "{\n"
924 " \"name\": \"gpu blacklist\",\n"
925 " \"version\": \"1.0\",\n"
926 " \"entries\": [\n"
927 " {\n"
928 " \"id\": 1,\n"
[email protected]3d346572013-03-22 19:59:55929 " \"features\": [\"webgl\"]\n"
[email protected]98270432012-09-11 20:51:24930 " }\n"
931 " ]\n"
932 "}";
[email protected]d7b5cc72013-05-23 20:05:00933 gpu::GPUInfo gpu_info;
[email protected]7e343152012-09-20 21:49:53934 content::GpuDataManager::GetInstance()->InitializeForTesting(
935 json_blacklist, gpu_info);
[email protected]98270432012-09-11 20:51:24936 }
937
[email protected]97d6a5c2013-11-11 23:51:24938 // Helper method to set up a WindowedNotificationObserver to wait for a
939 // specific CrxInstaller to finish if we don't know the value of the
940 // |installer| yet.
941 static bool IsCrxInstallerDone(extensions::CrxInstaller** installer,
942 const content::NotificationSource& source,
943 const content::NotificationDetails& details) {
944 return content::Source<extensions::CrxInstaller>(source).ptr() ==
945 *installer;
946 }
947
948 void UpdateExtension(const std::string& id,
949 const base::FilePath& in_path,
[email protected]4416c5a2010-06-26 01:28:57950 UpdateState expected_state) {
[email protected]7567484142013-07-11 17:36:07951 ASSERT_TRUE(base::PathExists(in_path));
[email protected]e957fe52009-06-23 16:51:05952
[email protected]7577a5c52009-07-30 06:21:58953 // We need to copy this to a temporary location because Update() will delete
954 // it.
[email protected]650b2d52013-02-10 03:41:45955 base::FilePath path = temp_dir_.path();
[email protected]a1295ba22009-09-02 03:33:39956 path = path.Append(in_path.BaseName());
[email protected]f0ff2ad2013-07-09 17:42:26957 ASSERT_TRUE(base::CopyFile(in_path, path));
[email protected]e957fe52009-06-23 16:51:05958
[email protected]4416c5a2010-06-26 01:28:57959 int previous_enabled_extension_count =
960 service_->extensions()->size();
961 int previous_installed_extension_count =
962 previous_enabled_extension_count +
963 service_->disabled_extensions()->size();
964
[email protected]6d057a0c2013-07-09 21:12:07965 extensions::CrxInstaller* installer = NULL;
[email protected]97d6a5c2013-11-11 23:51:24966 content::WindowedNotificationObserver observer(
967 chrome::NOTIFICATION_CRX_INSTALLER_DONE,
968 base::Bind(&IsCrxInstallerDone, &installer));
[email protected]044e86992014-01-24 22:59:11969 service_->UpdateExtension(id, path, true, GURL(), &installer);
[email protected]6d057a0c2013-07-09 21:12:07970
[email protected]97d6a5c2013-11-11 23:51:24971 if (installer)
972 observer.Wait();
973 else
[email protected]eedc1c752013-08-09 18:51:55974 base::RunLoop().RunUntilIdle();
[email protected]f3113e232010-06-25 01:36:40975
[email protected]d2065e062013-12-12 23:49:52976 std::vector<base::string16> errors = GetErrors();
[email protected]4416c5a2010-06-26 01:28:57977 int error_count = errors.size();
978 int enabled_extension_count =
979 service_->extensions()->size();
980 int installed_extension_count =
981 enabled_extension_count + service_->disabled_extensions()->size();
982
983 int expected_error_count = (expected_state == FAILED) ? 1 : 0;
984 EXPECT_EQ(expected_error_count, error_count) << path.value();
985
986 if (expected_state <= FAILED) {
987 EXPECT_EQ(previous_enabled_extension_count,
988 enabled_extension_count);
989 EXPECT_EQ(previous_installed_extension_count,
990 installed_extension_count);
[email protected]e957fe52009-06-23 16:51:05991 } else {
[email protected]4416c5a2010-06-26 01:28:57992 int expected_installed_extension_count =
993 (expected_state >= INSTALLED) ? 1 : 0;
994 int expected_enabled_extension_count =
995 (expected_state >= ENABLED) ? 1 : 0;
996 EXPECT_EQ(expected_installed_extension_count,
997 installed_extension_count);
998 EXPECT_EQ(expected_enabled_extension_count,
999 enabled_extension_count);
[email protected]e957fe52009-06-23 16:51:051000 }
[email protected]7577a5c52009-07-30 06:21:581001
[email protected]31d8f5f22012-04-02 15:22:081002 // Update() should the temporary input file.
[email protected]7567484142013-07-11 17:36:071003 EXPECT_FALSE(base::PathExists(path));
[email protected]e957fe52009-06-23 16:51:051004 }
1005
[email protected]fa2416f2011-05-03 08:41:201006 void TerminateExtension(const std::string& id) {
1007 const Extension* extension = service_->GetInstalledExtension(id);
1008 if (!extension) {
1009 ADD_FAILURE();
1010 return;
1011 }
1012 service_->TrackTerminatedExtensionForTest(extension);
1013 }
1014
1015 size_t GetPrefKeyCount() {
[email protected]023b3d12013-12-23 18:46:491016 const base::DictionaryValue* dict =
[email protected]43d3bf82011-04-11 07:46:581017 profile_->GetPrefs()->GetDictionary("extensions.settings");
[email protected]fa2416f2011-05-03 08:41:201018 if (!dict) {
1019 ADD_FAILURE();
1020 return 0;
1021 }
1022 return dict->size();
1023 }
1024
1025 void UninstallExtension(const std::string& id, bool use_helper) {
1026 // Verify that the extension is installed.
[email protected]650b2d52013-02-10 03:41:451027 base::FilePath extension_path = extensions_install_dir_.AppendASCII(id);
[email protected]7567484142013-07-11 17:36:071028 EXPECT_TRUE(base::PathExists(extension_path));
[email protected]fa2416f2011-05-03 08:41:201029 size_t pref_key_count = GetPrefKeyCount();
1030 EXPECT_GT(pref_key_count, 0u);
1031 ValidateIntegerPref(id, "state", Extension::ENABLED);
1032
1033 // Uninstall it.
1034 if (use_helper) {
1035 EXPECT_TRUE(ExtensionService::UninstallExtensionHelper(service_, id));
1036 } else {
1037 EXPECT_TRUE(service_->UninstallExtension(id, false, NULL));
1038 }
[email protected]8f512c72011-11-22 21:02:501039 --expected_extensions_count_;
[email protected]fa2416f2011-05-03 08:41:201040
1041 // We should get an unload notification.
1042 EXPECT_FALSE(unloaded_id_.empty());
1043 EXPECT_EQ(id, unloaded_id_);
1044
1045 // Verify uninstalled state.
1046 size_t new_pref_key_count = GetPrefKeyCount();
1047 if (new_pref_key_count == pref_key_count) {
1048 ValidateIntegerPref(id, "location",
1049 Extension::EXTERNAL_EXTENSION_UNINSTALLED);
1050 } else {
1051 EXPECT_EQ(new_pref_key_count, pref_key_count - 1);
1052 }
1053
1054 // The extension should not be in the service anymore.
1055 EXPECT_FALSE(service_->GetInstalledExtension(id));
[email protected]eedc1c752013-08-09 18:51:551056 base::RunLoop().RunUntilIdle();
[email protected]fa2416f2011-05-03 08:41:201057
1058 // The directory should be gone.
[email protected]7567484142013-07-11 17:36:071059 EXPECT_FALSE(base::PathExists(extension_path));
[email protected]fa2416f2011-05-03 08:41:201060 }
1061
1062 void ValidatePrefKeyCount(size_t count) {
1063 EXPECT_EQ(count, GetPrefKeyCount());
[email protected]25b34332009-06-05 21:53:191064 }
1065
[email protected]3f2a2fa2013-09-24 02:55:251066 testing::AssertionResult ValidateBooleanPref(
1067 const std::string& extension_id,
1068 const std::string& pref_path,
1069 bool expected_val) {
1070 std::string msg = "while checking: ";
[email protected]e2194742010-08-12 05:54:341071 msg += extension_id;
1072 msg += " ";
[email protected]6b75ec32009-08-14 06:37:181073 msg += pref_path;
[email protected]e2194742010-08-12 05:54:341074 msg += " == ";
1075 msg += expected_val ? "true" : "false";
[email protected]6b75ec32009-08-14 06:37:181076
[email protected]2fb7dc982010-09-29 12:24:281077 PrefService* prefs = profile_->GetPrefs();
[email protected]023b3d12013-12-23 18:46:491078 const base::DictionaryValue* dict =
[email protected]2fb7dc982010-09-29 12:24:281079 prefs->GetDictionary("extensions.settings");
[email protected]3f2a2fa2013-09-24 02:55:251080 if (!dict) {
1081 return testing::AssertionFailure()
1082 << "extension.settings does not exist " << msg;
1083 }
1084
[email protected]023b3d12013-12-23 18:46:491085 const base::DictionaryValue* pref = NULL;
[email protected]3f2a2fa2013-09-24 02:55:251086 if (!dict->GetDictionary(extension_id, &pref)) {
1087 return testing::AssertionFailure()
1088 << "extension pref does not exist " << msg;
1089 }
1090
[email protected]6b75ec32009-08-14 06:37:181091 bool val;
[email protected]3f2a2fa2013-09-24 02:55:251092 if (!pref->GetBoolean(pref_path, &val)) {
1093 return testing::AssertionFailure()
1094 << pref_path << " pref not found " << msg;
1095 }
1096
1097 return expected_val == val
1098 ? testing::AssertionSuccess()
[email protected]023b3d12013-12-23 18:46:491099 : testing::AssertionFailure() << "base::Value is incorrect " << msg;
[email protected]6b75ec32009-08-14 06:37:181100 }
1101
1102 bool IsPrefExist(const std::string& extension_id,
[email protected]e2194742010-08-12 05:54:341103 const std::string& pref_path) {
[email protected]023b3d12013-12-23 18:46:491104 const base::DictionaryValue* dict =
[email protected]2fb7dc982010-09-29 12:24:281105 profile_->GetPrefs()->GetDictionary("extensions.settings");
[email protected]6b75ec32009-08-14 06:37:181106 if (dict == NULL) return false;
[email protected]023b3d12013-12-23 18:46:491107 const base::DictionaryValue* pref = NULL;
[email protected]e2194742010-08-12 05:54:341108 if (!dict->GetDictionary(extension_id, &pref)) {
[email protected]6b75ec32009-08-14 06:37:181109 return false;
1110 }
1111 if (pref == NULL) {
1112 return false;
1113 }
1114 bool val;
1115 if (!pref->GetBoolean(pref_path, &val)) {
1116 return false;
1117 }
1118 return true;
1119 }
1120
1121 void ValidateIntegerPref(const std::string& extension_id,
[email protected]e2194742010-08-12 05:54:341122 const std::string& pref_path,
[email protected]c6d474f82009-12-16 21:11:061123 int expected_val) {
[email protected]e2194742010-08-12 05:54:341124 std::string msg = " while checking: ";
1125 msg += extension_id;
1126 msg += " ";
[email protected]25b34332009-06-05 21:53:191127 msg += pref_path;
[email protected]e2194742010-08-12 05:54:341128 msg += " == ";
1129 msg += base::IntToString(expected_val);
[email protected]25b34332009-06-05 21:53:191130
[email protected]2fb7dc982010-09-29 12:24:281131 PrefService* prefs = profile_->GetPrefs();
[email protected]023b3d12013-12-23 18:46:491132 const base::DictionaryValue* dict =
[email protected]2fb7dc982010-09-29 12:24:281133 prefs->GetDictionary("extensions.settings");
[email protected]25b34332009-06-05 21:53:191134 ASSERT_TRUE(dict != NULL) << msg;
[email protected]023b3d12013-12-23 18:46:491135 const base::DictionaryValue* pref = NULL;
[email protected]e2194742010-08-12 05:54:341136 ASSERT_TRUE(dict->GetDictionary(extension_id, &pref)) << msg;
[email protected]25b34332009-06-05 21:53:191137 EXPECT_TRUE(pref != NULL) << msg;
1138 int val;
[email protected]4c91487e2009-10-02 04:11:041139 ASSERT_TRUE(pref->GetInteger(pref_path, &val)) << msg;
[email protected]c6d474f82009-12-16 21:11:061140 EXPECT_EQ(expected_val, val) << msg;
1141 }
1142
1143 void ValidateStringPref(const std::string& extension_id,
[email protected]e2194742010-08-12 05:54:341144 const std::string& pref_path,
[email protected]c6d474f82009-12-16 21:11:061145 const std::string& expected_val) {
[email protected]e2194742010-08-12 05:54:341146 std::string msg = " while checking: ";
1147 msg += extension_id;
1148 msg += ".manifest.";
[email protected]c6d474f82009-12-16 21:11:061149 msg += pref_path;
[email protected]e2194742010-08-12 05:54:341150 msg += " == ";
1151 msg += expected_val;
[email protected]c6d474f82009-12-16 21:11:061152
[email protected]023b3d12013-12-23 18:46:491153 const base::DictionaryValue* dict =
[email protected]2fb7dc982010-09-29 12:24:281154 profile_->GetPrefs()->GetDictionary("extensions.settings");
[email protected]c6d474f82009-12-16 21:11:061155 ASSERT_TRUE(dict != NULL) << msg;
[email protected]023b3d12013-12-23 18:46:491156 const base::DictionaryValue* pref = NULL;
[email protected]c6d474f82009-12-16 21:11:061157 std::string manifest_path = extension_id + ".manifest";
[email protected]e2194742010-08-12 05:54:341158 ASSERT_TRUE(dict->GetDictionary(manifest_path, &pref)) << msg;
[email protected]c6d474f82009-12-16 21:11:061159 EXPECT_TRUE(pref != NULL) << msg;
1160 std::string val;
1161 ASSERT_TRUE(pref->GetString(pref_path, &val)) << msg;
1162 EXPECT_EQ(expected_val, val) << msg;
[email protected]25b34332009-06-05 21:53:191163 }
1164
[email protected]8d888c12010-11-30 00:00:251165 void SetPref(const std::string& extension_id,
1166 const std::string& pref_path,
[email protected]023b3d12013-12-23 18:46:491167 base::Value* value,
[email protected]8d888c12010-11-30 00:00:251168 const std::string& msg) {
[email protected]43d3bf82011-04-11 07:46:581169 DictionaryPrefUpdate update(profile_->GetPrefs(), "extensions.settings");
[email protected]023b3d12013-12-23 18:46:491170 base::DictionaryValue* dict = update.Get();
[email protected]8d888c12010-11-30 00:00:251171 ASSERT_TRUE(dict != NULL) << msg;
[email protected]023b3d12013-12-23 18:46:491172 base::DictionaryValue* pref = NULL;
[email protected]8d888c12010-11-30 00:00:251173 ASSERT_TRUE(dict->GetDictionary(extension_id, &pref)) << msg;
1174 EXPECT_TRUE(pref != NULL) << msg;
1175 pref->Set(pref_path, value);
1176 }
1177
[email protected]6b75ec32009-08-14 06:37:181178 void SetPrefInteg(const std::string& extension_id,
[email protected]e2194742010-08-12 05:54:341179 const std::string& pref_path,
[email protected]6b75ec32009-08-14 06:37:181180 int value) {
[email protected]e2194742010-08-12 05:54:341181 std::string msg = " while setting: ";
1182 msg += extension_id;
1183 msg += " ";
[email protected]a1257b12009-06-12 02:51:341184 msg += pref_path;
[email protected]e2194742010-08-12 05:54:341185 msg += " = ";
1186 msg += base::IntToString(value);
[email protected]a1257b12009-06-12 02:51:341187
[email protected]aa15e2e2013-08-14 02:13:581188 SetPref(extension_id, pref_path, new base::FundamentalValue(value), msg);
[email protected]8d888c12010-11-30 00:00:251189 }
1190
1191 void SetPrefBool(const std::string& extension_id,
1192 const std::string& pref_path,
1193 bool value) {
1194 std::string msg = " while setting: ";
1195 msg += extension_id + " " + pref_path;
1196 msg += " = ";
1197 msg += (value ? "true" : "false");
1198
[email protected]aa15e2e2013-08-14 02:13:581199 SetPref(extension_id, pref_path, new base::FundamentalValue(value), msg);
[email protected]8d888c12010-11-30 00:00:251200 }
1201
1202 void ClearPref(const std::string& extension_id,
1203 const std::string& pref_path) {
1204 std::string msg = " while clearing: ";
1205 msg += extension_id + " " + pref_path;
1206
[email protected]43d3bf82011-04-11 07:46:581207 DictionaryPrefUpdate update(profile_->GetPrefs(), "extensions.settings");
[email protected]023b3d12013-12-23 18:46:491208 base::DictionaryValue* dict = update.Get();
[email protected]a1257b12009-06-12 02:51:341209 ASSERT_TRUE(dict != NULL) << msg;
[email protected]023b3d12013-12-23 18:46:491210 base::DictionaryValue* pref = NULL;
[email protected]e2194742010-08-12 05:54:341211 ASSERT_TRUE(dict->GetDictionary(extension_id, &pref)) << msg;
[email protected]a1257b12009-06-12 02:51:341212 EXPECT_TRUE(pref != NULL) << msg;
[email protected]8d888c12010-11-30 00:00:251213 pref->Remove(pref_path, NULL);
1214 }
1215
1216 void SetPrefStringSet(const std::string& extension_id,
1217 const std::string& pref_path,
1218 const std::set<std::string>& value) {
1219 std::string msg = " while setting: ";
1220 msg += extension_id + " " + pref_path;
1221
[email protected]023b3d12013-12-23 18:46:491222 base::ListValue* list_value = new base::ListValue();
[email protected]8d888c12010-11-30 00:00:251223 for (std::set<std::string>::const_iterator iter = value.begin();
1224 iter != value.end(); ++iter)
[email protected]aa15e2e2013-08-14 02:13:581225 list_value->Append(new base::StringValue(*iter));
[email protected]8d888c12010-11-30 00:00:251226
1227 SetPref(extension_id, pref_path, list_value, msg);
[email protected]a1257b12009-06-12 02:51:341228 }
1229
[email protected]ebd71962012-12-20 02:56:551230 void InitPluginService() {
1231#if defined(ENABLE_PLUGINS)
1232 PluginService::GetInstance()->Init();
1233#endif
1234 }
1235
[email protected]25b34332009-06-05 21:53:191236 protected:
[email protected]1c321ee52012-05-21 03:02:341237 extensions::ExtensionList loaded_;
[email protected]894bb502009-05-21 22:39:571238 std::string unloaded_id_;
[email protected]9adb9692010-10-29 23:14:021239 const Extension* installed_;
[email protected]41bb80bd2013-05-03 10:56:021240 bool was_update_;
[email protected]e7554c3f2013-05-29 00:36:561241 std::string old_name_;
[email protected]00b5d0a52012-10-30 13:13:531242 FeatureSwitch::ScopedOverride override_external_install_prompt_;
[email protected]894bb502009-05-21 22:39:571243
[email protected]6014d672008-12-05 00:38:251244 private:
[email protected]97d6a5c2013-11-11 23:51:241245 // Create a CrxInstaller and install the CRX file.
1246 // Instead of calling this method yourself, use InstallCRX(), which does extra
1247 // error checking.
1248 void InstallCRXInternal(const base::FilePath& crx_path) {
1249 InstallCRXInternal(crx_path, Extension::NO_FLAGS);
1250 }
1251
1252 void InstallCRXInternal(const base::FilePath& crx_path, int creation_flags) {
1253 ASSERT_TRUE(base::PathExists(crx_path))
1254 << "Path does not exist: "<< crx_path.value().c_str();
1255 scoped_refptr<CrxInstaller> installer(CrxInstaller::CreateSilent(service_));
1256 installer->set_creation_flags(creation_flags);
1257 if (!(creation_flags & Extension::WAS_INSTALLED_BY_DEFAULT))
1258 installer->set_allow_silent_install(true);
1259
1260 content::WindowedNotificationObserver observer(
1261 chrome::NOTIFICATION_CRX_INSTALLER_DONE,
1262 content::Source<extensions::CrxInstaller>(installer));
1263
1264 installer->InstallCrx(crx_path);
1265
1266 observer.Wait();
1267 }
1268
[email protected]6c2381d2011-10-19 02:52:531269 content::NotificationRegistrar registrar_;
[email protected]bb28e062009-02-27 17:19:181270};
[email protected]6014d672008-12-05 00:38:251271
[email protected]0349ab5d2010-08-11 21:41:571272// Receives notifications from a PackExtensionJob, indicating either that
1273// packing succeeded or that there was some error.
[email protected]d9ede582012-08-14 19:21:381274class PackExtensionTestClient : public extensions::PackExtensionJob::Client {
[email protected]0349ab5d2010-08-11 21:41:571275 public:
[email protected]650b2d52013-02-10 03:41:451276 PackExtensionTestClient(const base::FilePath& expected_crx_path,
1277 const base::FilePath& expected_private_key_path);
1278 virtual void OnPackSuccess(const base::FilePath& crx_path,
1279 const base::FilePath& private_key_path) OVERRIDE;
[email protected]93d973a2012-01-08 07:38:261280 virtual void OnPackFailure(const std::string& error_message,
[email protected]49aeab62013-02-07 02:53:111281 ExtensionCreator::ErrorType type) OVERRIDE;
[email protected]0349ab5d2010-08-11 21:41:571282
1283 private:
[email protected]650b2d52013-02-10 03:41:451284 const base::FilePath expected_crx_path_;
1285 const base::FilePath expected_private_key_path_;
[email protected]0349ab5d2010-08-11 21:41:571286 DISALLOW_COPY_AND_ASSIGN(PackExtensionTestClient);
1287};
1288
1289PackExtensionTestClient::PackExtensionTestClient(
[email protected]650b2d52013-02-10 03:41:451290 const base::FilePath& expected_crx_path,
1291 const base::FilePath& expected_private_key_path)
[email protected]0349ab5d2010-08-11 21:41:571292 : expected_crx_path_(expected_crx_path),
1293 expected_private_key_path_(expected_private_key_path) {}
1294
1295// If packing succeeded, we make sure that the package names match our
1296// expectations.
[email protected]650b2d52013-02-10 03:41:451297void PackExtensionTestClient::OnPackSuccess(
1298 const base::FilePath& crx_path,
1299 const base::FilePath& private_key_path) {
[email protected]0349ab5d2010-08-11 21:41:571300 // We got the notification and processed it; we don't expect any further tasks
1301 // to be posted to the current thread, so we should stop blocking and continue
1302 // on with the rest of the test.
1303 // This call to |Quit()| matches the call to |Run()| in the
1304 // |PackPunctuatedExtension| test.
[email protected]b3a25092013-05-28 22:08:161305 base::MessageLoop::current()->Quit();
[email protected]0349ab5d2010-08-11 21:41:571306 EXPECT_EQ(expected_crx_path_.value(), crx_path.value());
1307 EXPECT_EQ(expected_private_key_path_.value(), private_key_path.value());
[email protected]7567484142013-07-11 17:36:071308 ASSERT_TRUE(base::PathExists(private_key_path));
[email protected]0349ab5d2010-08-11 21:41:571309}
1310
1311// The tests are designed so that we never expect to see a packing error.
[email protected]93d973a2012-01-08 07:38:261312void PackExtensionTestClient::OnPackFailure(const std::string& error_message,
1313 ExtensionCreator::ErrorType type) {
1314 if (type == ExtensionCreator::kCRXExists)
1315 FAIL() << "Packing should not fail.";
1316 else
1317 FAIL() << "Existing CRX should have been overwritten.";
[email protected]0349ab5d2010-08-11 21:41:571318}
1319
[email protected]54cb3c92009-02-17 22:30:211320// Test loading good extensions from the profile directory.
[email protected]d9a61e12012-11-14 02:43:471321TEST_F(ExtensionServiceTest, LoadAllExtensionsFromDirectorySuccess) {
[email protected]ebd71962012-12-20 02:56:551322 InitPluginService();
[email protected]3f2a2fa2013-09-24 02:55:251323 InitializeGoodInstalledExtensionService();
[email protected]9f1087e2009-06-15 17:29:321324 service_->Init();
[email protected]6014d672008-12-05 00:38:251325
[email protected]e50013c32010-08-18 21:05:241326 uint32 expected_num_extensions = 3u;
[email protected]e50013c32010-08-18 21:05:241327 ASSERT_EQ(expected_num_extensions, loaded_.size());
[email protected]6014d672008-12-05 00:38:251328
[email protected]fbcc40302009-06-12 20:45:451329 EXPECT_EQ(std::string(good0), loaded_[0]->id());
[email protected]e1cec06c2008-12-18 01:22:231330 EXPECT_EQ(std::string("My extension 1"),
[email protected]894bb502009-05-21 22:39:571331 loaded_[0]->name());
[email protected]e1cec06c2008-12-18 01:22:231332 EXPECT_EQ(std::string("The first extension that I made."),
[email protected]894bb502009-05-21 22:39:571333 loaded_[0]->description());
[email protected]1d5e58b2013-01-31 08:41:401334 EXPECT_EQ(Manifest::INTERNAL, loaded_[0]->location());
[email protected]61b411612009-11-10 23:17:411335 EXPECT_TRUE(service_->GetExtensionById(loaded_[0]->id(), false));
[email protected]e50013c32010-08-18 21:05:241336 EXPECT_EQ(expected_num_extensions, service_->extensions()->size());
[email protected]eab9b452009-01-23 20:48:591337
[email protected]25b34332009-06-05 21:53:191338 ValidatePrefKeyCount(3);
[email protected]e2194742010-08-12 05:54:341339 ValidateIntegerPref(good0, "state", Extension::ENABLED);
[email protected]1d5e58b2013-01-31 08:41:401340 ValidateIntegerPref(good0, "location", Manifest::INTERNAL);
[email protected]e2194742010-08-12 05:54:341341 ValidateIntegerPref(good1, "state", Extension::ENABLED);
[email protected]1d5e58b2013-01-31 08:41:401342 ValidateIntegerPref(good1, "location", Manifest::INTERNAL);
[email protected]e2194742010-08-12 05:54:341343 ValidateIntegerPref(good2, "state", Extension::ENABLED);
[email protected]1d5e58b2013-01-31 08:41:401344 ValidateIntegerPref(good2, "location", Manifest::INTERNAL);
[email protected]25b34332009-06-05 21:53:191345
[email protected]06e8b8ff2011-07-13 15:03:471346 URLPatternSet expected_patterns;
1347 AddPattern(&expected_patterns, "file:///*");
1348 AddPattern(&expected_patterns, "http://*.google.com/*");
1349 AddPattern(&expected_patterns, "https://*.google.com/*");
[email protected]dc24976f2013-06-02 21:15:091350 const Extension* extension = loaded_[0].get();
[email protected]334ec0a2013-03-24 01:36:161351 const extensions::UserScriptList& scripts =
1352 extensions::ContentScriptsInfo::GetContentScripts(extension);
[email protected]e66de892009-03-20 20:38:431353 ASSERT_EQ(2u, scripts.size());
[email protected]06e8b8ff2011-07-13 15:03:471354 EXPECT_EQ(expected_patterns, scripts[0].url_patterns());
[email protected]e66de892009-03-20 20:38:431355 EXPECT_EQ(2u, scripts[0].js_scripts().size());
[email protected]052c92702010-06-25 07:25:521356 ExtensionResource resource00(extension->id(),
1357 scripts[0].js_scripts()[0].extension_root(),
[email protected]9194b3f2009-10-20 15:27:211358 scripts[0].js_scripts()[0].relative_path());
[email protected]15476932013-04-12 05:17:151359 base::FilePath expected_path =
1360 base::MakeAbsoluteFilePath(extension->path().AppendASCII("script1.js"));
[email protected]a14b16b2009-10-28 12:41:291361 EXPECT_TRUE(resource00.ComparePathWithDefault(expected_path));
[email protected]052c92702010-06-25 07:25:521362 ExtensionResource resource01(extension->id(),
1363 scripts[0].js_scripts()[1].extension_root(),
[email protected]9194b3f2009-10-20 15:27:211364 scripts[0].js_scripts()[1].relative_path());
[email protected]15476932013-04-12 05:17:151365 expected_path =
1366 base::MakeAbsoluteFilePath(extension->path().AppendASCII("script2.js"));
[email protected]a14b16b2009-10-28 12:41:291367 EXPECT_TRUE(resource01.ComparePathWithDefault(expected_path));
[email protected]098fa7a2013-03-08 22:11:171368 EXPECT_TRUE(!extensions::PluginInfo::HasPlugins(extension));
[email protected]06e8b8ff2011-07-13 15:03:471369 EXPECT_EQ(1u, scripts[1].url_patterns().patterns().size());
1370 EXPECT_EQ("http://*.news.com/*",
1371 scripts[1].url_patterns().begin()->GetAsString());
[email protected]052c92702010-06-25 07:25:521372 ExtensionResource resource10(extension->id(),
1373 scripts[1].js_scripts()[0].extension_root(),
[email protected]9194b3f2009-10-20 15:27:211374 scripts[1].js_scripts()[0].relative_path());
[email protected]a14b16b2009-10-28 12:41:291375 expected_path =
1376 extension->path().AppendASCII("js_files").AppendASCII("script3.js");
[email protected]15476932013-04-12 05:17:151377 expected_path = base::MakeAbsoluteFilePath(expected_path);
[email protected]a14b16b2009-10-28 12:41:291378 EXPECT_TRUE(resource10.ComparePathWithDefault(expected_path));
[email protected]06e8b8ff2011-07-13 15:03:471379
1380 expected_patterns.ClearPatterns();
1381 AddPattern(&expected_patterns, "http://*.google.com/*");
1382 AddPattern(&expected_patterns, "https://*.google.com/*");
[email protected]902fd7b2011-07-27 18:42:311383 EXPECT_EQ(expected_patterns,
1384 extension->GetActivePermissions()->explicit_hosts());
[email protected]6014d672008-12-05 00:38:251385
[email protected]25b34332009-06-05 21:53:191386 EXPECT_EQ(std::string(good1), loaded_[1]->id());
[email protected]894bb502009-05-21 22:39:571387 EXPECT_EQ(std::string("My extension 2"), loaded_[1]->name());
[email protected]007b3f82013-04-09 08:46:451388 EXPECT_EQ(std::string(), loaded_[1]->description());
[email protected]81067e02009-07-27 15:12:091389 EXPECT_EQ(loaded_[1]->GetResourceURL("background.html"),
[email protected]dc24976f2013-06-02 21:15:091390 extensions::BackgroundInfo::GetBackgroundURL(loaded_[1].get()));
1391 EXPECT_EQ(0u,
1392 extensions::ContentScriptsInfo::GetContentScripts(loaded_[1].get())
1393 .size());
[email protected]098fa7a2013-03-08 22:11:171394
[email protected]b8fb3032011-04-29 18:45:561395 // We don't parse the plugins section on Chrome OS.
1396#if defined(OS_CHROMEOS)
[email protected]144b6c42013-06-14 07:30:381397 EXPECT_TRUE(!extensions::PluginInfo::HasPlugins(loaded_[1].get()));
[email protected]b8fb3032011-04-29 18:45:561398#else
[email protected]dc24976f2013-06-02 21:15:091399 ASSERT_TRUE(extensions::PluginInfo::HasPlugins(loaded_[1].get()));
[email protected]098fa7a2013-03-08 22:11:171400 const std::vector<extensions::PluginInfo>* plugins =
[email protected]dc24976f2013-06-02 21:15:091401 extensions::PluginInfo::GetPlugins(loaded_[1].get());
[email protected]098fa7a2013-03-08 22:11:171402 ASSERT_TRUE(plugins);
1403 ASSERT_EQ(2u, plugins->size());
[email protected]c533bb22009-06-03 19:06:111404 EXPECT_EQ(loaded_[1]->path().AppendASCII("content_plugin.dll").value(),
[email protected]098fa7a2013-03-08 22:11:171405 plugins->at(0).path.value());
1406 EXPECT_TRUE(plugins->at(0).is_public);
[email protected]c533bb22009-06-03 19:06:111407 EXPECT_EQ(loaded_[1]->path().AppendASCII("extension_plugin.dll").value(),
[email protected]098fa7a2013-03-08 22:11:171408 plugins->at(1).path.value());
1409 EXPECT_FALSE(plugins->at(1).is_public);
[email protected]e50013c32010-08-18 21:05:241410#endif
[email protected]18a12352009-01-31 01:33:281411
[email protected]1d5e58b2013-01-31 08:41:401412 EXPECT_EQ(Manifest::INTERNAL, loaded_[1]->location());
[email protected]b8fb3032011-04-29 18:45:561413
[email protected]e50013c32010-08-18 21:05:241414 int index = expected_num_extensions - 1;
1415 EXPECT_EQ(std::string(good2), loaded_[index]->id());
1416 EXPECT_EQ(std::string("My extension 3"), loaded_[index]->name());
[email protected]007b3f82013-04-09 08:46:451417 EXPECT_EQ(std::string(), loaded_[index]->description());
[email protected]dc24976f2013-06-02 21:15:091418 EXPECT_EQ(0u,
1419 extensions::ContentScriptsInfo::GetContentScripts(
1420 loaded_[index].get()).size());
[email protected]1d5e58b2013-01-31 08:41:401421 EXPECT_EQ(Manifest::INTERNAL, loaded_[index]->location());
[email protected]6014d672008-12-05 00:38:251422};
[email protected]cc655912009-01-29 23:19:191423
[email protected]54cb3c92009-02-17 22:30:211424// Test loading bad extensions from the profile directory.
[email protected]d9a61e12012-11-14 02:43:471425TEST_F(ExtensionServiceTest, LoadAllExtensionsFromDirectoryFail) {
[email protected]c6d474f82009-12-16 21:11:061426 // Initialize the test dir with a bad Preferences/extensions.
[email protected]650b2d52013-02-10 03:41:451427 base::FilePath source_install_dir = data_dir_
[email protected]a9b00ac2009-06-25 21:03:231428 .AppendASCII("bad")
1429 .AppendASCII("Extensions");
[email protected]650b2d52013-02-10 03:41:451430 base::FilePath pref_path = source_install_dir
[email protected]a9b00ac2009-06-25 21:03:231431 .DirName()
1432 .AppendASCII("Preferences");
[email protected]54cb3c92009-02-17 22:30:211433
[email protected]eaa7dd182010-12-14 11:09:001434 InitializeInstalledExtensionService(pref_path, source_install_dir);
[email protected]54cb3c92009-02-17 22:30:211435
[email protected]9f1087e2009-06-15 17:29:321436 service_->Init();
[email protected]54cb3c92009-02-17 22:30:211437
[email protected]a9b00ac2009-06-25 21:03:231438 ASSERT_EQ(4u, GetErrors().size());
1439 ASSERT_EQ(0u, loaded_.size());
[email protected]25b34332009-06-05 21:53:191440
[email protected]04338722013-12-24 23:18:051441 EXPECT_TRUE(MatchPattern(base::UTF16ToUTF8(GetErrors()[0]),
[email protected]d7b36dc2009-10-29 21:47:401442 std::string("Could not load extension from '*'. ") +
[email protected]0c3c9732013-09-16 08:53:411443 extensions::manifest_errors::kManifestUnreadable)) <<
[email protected]04338722013-12-24 23:18:051444 base::UTF16ToUTF8(GetErrors()[0]);
[email protected]8d6d9ff2009-02-20 08:14:391445
[email protected]04338722013-12-24 23:18:051446 EXPECT_TRUE(MatchPattern(base::UTF16ToUTF8(GetErrors()[1]),
[email protected]8d6d9ff2009-02-20 08:14:391447 std::string("Could not load extension from '*'. ") +
[email protected]0c3c9732013-09-16 08:53:411448 extensions::manifest_errors::kManifestUnreadable)) <<
[email protected]04338722013-12-24 23:18:051449 base::UTF16ToUTF8(GetErrors()[1]);
[email protected]8d6d9ff2009-02-20 08:14:391450
[email protected]04338722013-12-24 23:18:051451 EXPECT_TRUE(MatchPattern(base::UTF16ToUTF8(GetErrors()[2]),
[email protected]8d6d9ff2009-02-20 08:14:391452 std::string("Could not load extension from '*'. ") +
[email protected]0c3c9732013-09-16 08:53:411453 extensions::manifest_errors::kMissingFile)) <<
[email protected]04338722013-12-24 23:18:051454 base::UTF16ToUTF8(GetErrors()[2]);
[email protected]a9b00ac2009-06-25 21:03:231455
[email protected]04338722013-12-24 23:18:051456 EXPECT_TRUE(MatchPattern(base::UTF16ToUTF8(GetErrors()[3]),
[email protected]a9b00ac2009-06-25 21:03:231457 std::string("Could not load extension from '*'. ") +
[email protected]0c3c9732013-09-16 08:53:411458 extensions::manifest_errors::kManifestUnreadable)) <<
[email protected]04338722013-12-24 23:18:051459 base::UTF16ToUTF8(GetErrors()[3]);
[email protected]54cb3c92009-02-17 22:30:211460};
1461
[email protected]2f8757c32012-06-19 19:17:471462// Test that partially deleted extensions are cleaned up during startup
[email protected]894bb502009-05-21 22:39:571463// Test loading bad extensions from the profile directory.
[email protected]d9a61e12012-11-14 02:43:471464TEST_F(ExtensionServiceTest, CleanupOnStartup) {
[email protected]ebd71962012-12-20 02:56:551465 InitPluginService();
[email protected]3f2a2fa2013-09-24 02:55:251466 InitializeGoodInstalledExtensionService();
[email protected]894bb502009-05-21 22:39:571467
[email protected]b6ab96d2009-08-20 18:58:191468 // Simulate that one of them got partially deleted by clearing its pref.
[email protected]43d3bf82011-04-11 07:46:581469 {
1470 DictionaryPrefUpdate update(profile_->GetPrefs(), "extensions.settings");
[email protected]023b3d12013-12-23 18:46:491471 base::DictionaryValue* dict = update.Get();
[email protected]2f8757c32012-06-19 19:17:471472 ASSERT_TRUE(dict != NULL);
[email protected]43d3bf82011-04-11 07:46:581473 dict->Remove("behllobkkfkfnphdnhnkndlbkcpglgmj", NULL);
1474 }
[email protected]894bb502009-05-21 22:39:571475
1476 service_->Init();
[email protected]36fc54f2013-06-14 02:49:341477 // A delayed task to call GarbageCollectExtensions is posted by
1478 // ExtensionService::Init. As the test won't wait for the delayed task to
1479 // be called, call it manually instead.
1480 service_->GarbageCollectExtensions();
[email protected]1f4728f2012-12-05 20:40:051481 // Wait for GarbageCollectExtensions task to complete.
[email protected]eedc1c752013-08-09 18:51:551482 base::RunLoop().RunUntilIdle();
[email protected]894bb502009-05-21 22:39:571483
[email protected]25a4c1c2013-06-08 04:53:361484 base::FileEnumerator dirs(extensions_install_dir_, false,
1485 base::FileEnumerator::DIRECTORIES);
[email protected]9f1087e2009-06-15 17:29:321486 size_t count = 0;
1487 while (!dirs.Next().empty())
1488 count++;
1489
[email protected]894bb502009-05-21 22:39:571490 // We should have only gotten two extensions now.
[email protected]9f1087e2009-06-15 17:29:321491 EXPECT_EQ(2u, count);
[email protected]e2eb43112009-05-29 21:19:541492
[email protected]894bb502009-05-21 22:39:571493 // And extension1 dir should now be toast.
[email protected]650b2d52013-02-10 03:41:451494 base::FilePath extension_dir = extensions_install_dir_
[email protected]b6ab96d2009-08-20 18:58:191495 .AppendASCII("behllobkkfkfnphdnhnkndlbkcpglgmj");
[email protected]7567484142013-07-11 17:36:071496 ASSERT_FALSE(base::PathExists(extension_dir));
[email protected]894bb502009-05-21 22:39:571497}
1498
[email protected]0db124b02012-11-07 04:55:051499// Test that GarbageCollectExtensions deletes the right versions of an
1500// extension.
1501TEST_F(ExtensionServiceTest, GarbageCollectWithPendingUpdates) {
[email protected]ebd71962012-12-20 02:56:551502 InitPluginService();
[email protected]0db124b02012-11-07 04:55:051503
[email protected]650b2d52013-02-10 03:41:451504 base::FilePath source_install_dir = data_dir_
[email protected]0db124b02012-11-07 04:55:051505 .AppendASCII("pending_updates")
1506 .AppendASCII("Extensions");
[email protected]650b2d52013-02-10 03:41:451507 base::FilePath pref_path = source_install_dir
[email protected]0db124b02012-11-07 04:55:051508 .DirName()
1509 .AppendASCII("Preferences");
1510
1511 InitializeInstalledExtensionService(pref_path, source_install_dir);
1512
1513 // This is the directory that is going to be deleted, so make sure it actually
1514 // is there before the garbage collection.
[email protected]7567484142013-07-11 17:36:071515 ASSERT_TRUE(base::PathExists(extensions_install_dir_.AppendASCII(
[email protected]0db124b02012-11-07 04:55:051516 "hpiknbiabeeppbpihjehijgoemciehgk/3")));
1517
1518 service_->GarbageCollectExtensions();
[email protected]1f4728f2012-12-05 20:40:051519 // Wait for GarbageCollectExtensions task to complete.
[email protected]eedc1c752013-08-09 18:51:551520 base::RunLoop().RunUntilIdle();
[email protected]0db124b02012-11-07 04:55:051521
1522 // Verify that the pending update for the first extension didn't get
1523 // deleted.
[email protected]7567484142013-07-11 17:36:071524 EXPECT_TRUE(base::PathExists(extensions_install_dir_.AppendASCII(
[email protected]0db124b02012-11-07 04:55:051525 "bjafgdebaacbbbecmhlhpofkepfkgcpa/1.0")));
[email protected]7567484142013-07-11 17:36:071526 EXPECT_TRUE(base::PathExists(extensions_install_dir_.AppendASCII(
[email protected]0db124b02012-11-07 04:55:051527 "bjafgdebaacbbbecmhlhpofkepfkgcpa/2.0")));
[email protected]7567484142013-07-11 17:36:071528 EXPECT_TRUE(base::PathExists(extensions_install_dir_.AppendASCII(
[email protected]0db124b02012-11-07 04:55:051529 "hpiknbiabeeppbpihjehijgoemciehgk/2")));
[email protected]7567484142013-07-11 17:36:071530 EXPECT_FALSE(base::PathExists(extensions_install_dir_.AppendASCII(
[email protected]0db124b02012-11-07 04:55:051531 "hpiknbiabeeppbpihjehijgoemciehgk/3")));
1532}
1533
1534// Test that pending updates are properly handled on startup.
1535TEST_F(ExtensionServiceTest, UpdateOnStartup) {
[email protected]ebd71962012-12-20 02:56:551536 InitPluginService();
[email protected]0db124b02012-11-07 04:55:051537
[email protected]650b2d52013-02-10 03:41:451538 base::FilePath source_install_dir = data_dir_
[email protected]0db124b02012-11-07 04:55:051539 .AppendASCII("pending_updates")
1540 .AppendASCII("Extensions");
[email protected]650b2d52013-02-10 03:41:451541 base::FilePath pref_path = source_install_dir
[email protected]0db124b02012-11-07 04:55:051542 .DirName()
1543 .AppendASCII("Preferences");
1544
1545 InitializeInstalledExtensionService(pref_path, source_install_dir);
1546
1547 // This is the directory that is going to be deleted, so make sure it actually
1548 // is there before the garbage collection.
[email protected]7567484142013-07-11 17:36:071549 ASSERT_TRUE(base::PathExists(extensions_install_dir_.AppendASCII(
[email protected]0db124b02012-11-07 04:55:051550 "hpiknbiabeeppbpihjehijgoemciehgk/3")));
1551
1552 service_->Init();
[email protected]36fc54f2013-06-14 02:49:341553 // A delayed task to call GarbageCollectExtensions is posted by
1554 // ExtensionService::Init. As the test won't wait for the delayed task to
1555 // be called, call it manually instead.
1556 service_->GarbageCollectExtensions();
[email protected]1f4728f2012-12-05 20:40:051557 // Wait for GarbageCollectExtensions task to complete.
[email protected]eedc1c752013-08-09 18:51:551558 base::RunLoop().RunUntilIdle();
[email protected]0db124b02012-11-07 04:55:051559
1560 // Verify that the pending update for the first extension got installed.
[email protected]7567484142013-07-11 17:36:071561 EXPECT_FALSE(base::PathExists(extensions_install_dir_.AppendASCII(
[email protected]0db124b02012-11-07 04:55:051562 "bjafgdebaacbbbecmhlhpofkepfkgcpa/1.0")));
[email protected]7567484142013-07-11 17:36:071563 EXPECT_TRUE(base::PathExists(extensions_install_dir_.AppendASCII(
[email protected]0db124b02012-11-07 04:55:051564 "bjafgdebaacbbbecmhlhpofkepfkgcpa/2.0")));
[email protected]7567484142013-07-11 17:36:071565 EXPECT_TRUE(base::PathExists(extensions_install_dir_.AppendASCII(
[email protected]0db124b02012-11-07 04:55:051566 "hpiknbiabeeppbpihjehijgoemciehgk/2")));
[email protected]7567484142013-07-11 17:36:071567 EXPECT_FALSE(base::PathExists(extensions_install_dir_.AppendASCII(
[email protected]0db124b02012-11-07 04:55:051568 "hpiknbiabeeppbpihjehijgoemciehgk/3")));
1569
1570 // Make sure update information got deleted.
1571 ExtensionPrefs* prefs = service_->extension_prefs();
[email protected]399583b2012-12-11 09:33:421572 EXPECT_FALSE(
1573 prefs->GetDelayedInstallInfo("bjafgdebaacbbbecmhlhpofkepfkgcpa"));
[email protected]0db124b02012-11-07 04:55:051574}
1575
[email protected]9f4e4f082013-06-21 07:11:191576// Test various cases for delayed install because of missing imports.
1577TEST_F(ExtensionServiceTest, PendingImports) {
1578 InitPluginService();
1579
1580 base::FilePath source_install_dir = data_dir_
1581 .AppendASCII("pending_updates_with_imports")
1582 .AppendASCII("Extensions");
1583 base::FilePath pref_path = source_install_dir
1584 .DirName()
1585 .AppendASCII("Preferences");
1586
1587 InitializeInstalledExtensionService(pref_path, source_install_dir);
1588
1589 // Verify there are no pending extensions initially.
1590 EXPECT_FALSE(service_->pending_extension_manager()->HasPendingExtensions());
1591
1592 service_->Init();
1593 // Wait for GarbageCollectExtensions task to complete.
[email protected]eedc1c752013-08-09 18:51:551594 base::RunLoop().RunUntilIdle();
[email protected]9f4e4f082013-06-21 07:11:191595
1596 // These extensions are used by the extensions we test below, they must be
1597 // installed.
[email protected]7567484142013-07-11 17:36:071598 EXPECT_TRUE(base::PathExists(extensions_install_dir_.AppendASCII(
[email protected]9f4e4f082013-06-21 07:11:191599 "bjafgdebaacbbbecmhlhpofkepfkgcpa/1.0")));
[email protected]7567484142013-07-11 17:36:071600 EXPECT_TRUE(base::PathExists(extensions_install_dir_.AppendASCII(
[email protected]9f4e4f082013-06-21 07:11:191601 "hpiknbiabeeppbpihjehijgoemciehgk/2")));
1602
1603 // Each of these extensions should have been rejected because of dependencies
1604 // that cannot be satisfied.
1605 ExtensionPrefs* prefs = service_->extension_prefs();
1606 EXPECT_FALSE(
1607 prefs->GetDelayedInstallInfo("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"));
1608 EXPECT_FALSE(
1609 prefs->GetInstalledExtensionInfo("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"));
1610 EXPECT_FALSE(
1611 prefs->GetDelayedInstallInfo("bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"));
1612 EXPECT_FALSE(
1613 prefs->GetInstalledExtensionInfo("bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"));
1614 EXPECT_FALSE(
1615 prefs->GetDelayedInstallInfo("cccccccccccccccccccccccccccccccc"));
1616 EXPECT_FALSE(
1617 prefs->GetInstalledExtensionInfo("cccccccccccccccccccccccccccccccc"));
1618
1619 // Make sure the import started for the extension with a dependency.
1620 EXPECT_TRUE(
1621 prefs->GetDelayedInstallInfo("behllobkkfkfnphdnhnkndlbkcpglgmj"));
1622 EXPECT_EQ(ExtensionPrefs::DELAY_REASON_WAIT_FOR_IMPORTS,
1623 prefs->GetDelayedInstallReason("behllobkkfkfnphdnhnkndlbkcpglgmj"));
1624
[email protected]7567484142013-07-11 17:36:071625 EXPECT_FALSE(base::PathExists(extensions_install_dir_.AppendASCII(
[email protected]9f4e4f082013-06-21 07:11:191626 "behllobkkfkfnphdnhnkndlbkcpglgmj/1.0.0.0")));
1627
1628 EXPECT_TRUE(service_->pending_extension_manager()->HasPendingExtensions());
1629 std::string pending_id("eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee");
1630 EXPECT_TRUE(service_->pending_extension_manager()->IsIdPending(pending_id));
1631 // Remove it because we are not testing the pending extension manager's
1632 // ability to download and install extensions.
1633 EXPECT_TRUE(service_->pending_extension_manager()->Remove(pending_id));
1634}
1635
[email protected]d7eaf572009-07-01 21:57:001636// Test installing extensions. This test tries to install few extensions using
1637// crx files. If you need to change those crx files, feel free to repackage
1638// them, throw away the key used and change the id's above.
[email protected]d9a61e12012-11-14 02:43:471639TEST_F(ExtensionServiceTest, InstallExtension) {
[email protected]eaa7dd182010-12-14 11:09:001640 InitializeEmptyExtensionService();
[email protected]a9b00ac2009-06-25 21:03:231641
[email protected]e2eb43112009-05-29 21:19:541642 // Extensions not enabled.
[email protected]7577a5c52009-07-30 06:21:581643 set_extensions_enabled(false);
[email protected]650b2d52013-02-10 03:41:451644 base::FilePath path = data_dir_.AppendASCII("good.crx");
[email protected]8f512c72011-11-22 21:02:501645 InstallCRX(path, INSTALL_FAILED);
[email protected]7577a5c52009-07-30 06:21:581646 set_extensions_enabled(true);
[email protected]e2eb43112009-05-29 21:19:541647
[email protected]25b34332009-06-05 21:53:191648 ValidatePrefKeyCount(0);
1649
[email protected]e2eb43112009-05-29 21:19:541650 // A simple extension that should install without error.
[email protected]e85e34c32011-04-13 18:38:351651 path = data_dir_.AppendASCII("good.crx");
[email protected]8f512c72011-11-22 21:02:501652 InstallCRX(path, INSTALL_NEW);
[email protected]cc655912009-01-29 23:19:191653 // TODO(erikkay): verify the contents of the installed extension.
1654
[email protected]25b34332009-06-05 21:53:191655 int pref_count = 0;
1656 ValidatePrefKeyCount(++pref_count);
[email protected]e2194742010-08-12 05:54:341657 ValidateIntegerPref(good_crx, "state", Extension::ENABLED);
[email protected]1d5e58b2013-01-31 08:41:401658 ValidateIntegerPref(good_crx, "location", Manifest::INTERNAL);
[email protected]25b34332009-06-05 21:53:191659
[email protected]902f7cd2009-05-22 19:02:191660 // An extension with page actions.
[email protected]e85e34c32011-04-13 18:38:351661 path = data_dir_.AppendASCII("page_action.crx");
[email protected]8f512c72011-11-22 21:02:501662 InstallCRX(path, INSTALL_NEW);
[email protected]25b34332009-06-05 21:53:191663 ValidatePrefKeyCount(++pref_count);
[email protected]e2194742010-08-12 05:54:341664 ValidateIntegerPref(page_action, "state", Extension::ENABLED);
[email protected]1d5e58b2013-01-31 08:41:401665 ValidateIntegerPref(page_action, "location", Manifest::INTERNAL);
[email protected]902f7cd2009-05-22 19:02:191666
[email protected]9f1087e2009-06-15 17:29:321667 // Bad signature.
[email protected]e85e34c32011-04-13 18:38:351668 path = data_dir_.AppendASCII("bad_signature.crx");
[email protected]8f512c72011-11-22 21:02:501669 InstallCRX(path, INSTALL_FAILED);
[email protected]d7eaf572009-07-01 21:57:001670 ValidatePrefKeyCount(pref_count);
[email protected]fbcc40302009-06-12 20:45:451671
[email protected]cc655912009-01-29 23:19:191672 // 0-length extension file.
[email protected]e85e34c32011-04-13 18:38:351673 path = data_dir_.AppendASCII("not_an_extension.crx");
[email protected]8f512c72011-11-22 21:02:501674 InstallCRX(path, INSTALL_FAILED);
[email protected]25b34332009-06-05 21:53:191675 ValidatePrefKeyCount(pref_count);
[email protected]cc655912009-01-29 23:19:191676
1677 // Bad magic number.
[email protected]e85e34c32011-04-13 18:38:351678 path = data_dir_.AppendASCII("bad_magic.crx");
[email protected]8f512c72011-11-22 21:02:501679 InstallCRX(path, INSTALL_FAILED);
[email protected]25b34332009-06-05 21:53:191680 ValidatePrefKeyCount(pref_count);
[email protected]cc655912009-01-29 23:19:191681
[email protected]622813ee02013-08-21 21:00:291682 // Packed extensions may have folders or files that have underscores.
1683 // This will only cause a warning, rather than a fatal error.
[email protected]e85e34c32011-04-13 18:38:351684 path = data_dir_.AppendASCII("bad_underscore.crx");
[email protected]622813ee02013-08-21 21:00:291685 InstallCRX(path, INSTALL_NEW);
1686 ValidatePrefKeyCount(++pref_count);
[email protected]99872e32009-09-25 22:02:491687
[email protected]5fda91942013-09-24 06:47:531688 // A test for an extension with a 2048-bit public key.
1689 path = data_dir_.AppendASCII("good2048.crx");
1690 InstallCRX(path, INSTALL_NEW);
1691 ValidatePrefKeyCount(++pref_count);
1692 ValidateIntegerPref(good2048, "state", Extension::ENABLED);
1693 ValidateIntegerPref(good2048, "location", Manifest::INTERNAL);
1694
[email protected]cc655912009-01-29 23:19:191695 // TODO(erikkay): add more tests for many of the failure cases.
1696 // TODO(erikkay): add tests for upgrade cases.
1697}
1698
[email protected]0d55b9a2013-07-19 10:40:261699struct MockInstallObserver : public extensions::InstallObserver {
1700 MockInstallObserver() {
1701 }
1702
1703 virtual ~MockInstallObserver() {
1704 }
1705
1706 virtual void OnBeginExtensionInstall(
[email protected]fa517d02013-11-18 09:33:091707 const ExtensionInstallParams& params) OVERRIDE {
[email protected]0d55b9a2013-07-19 10:40:261708 }
1709
1710 virtual void OnDownloadProgress(const std::string& extension_id,
1711 int percent_downloaded) OVERRIDE {
1712 }
1713
1714 virtual void OnExtensionInstalled(const Extension* extension) OVERRIDE {
1715 last_extension_installed = extension->id();
1716 }
1717
1718 virtual void OnInstallFailure(const std::string& extension_id) OVERRIDE {
1719 }
1720
1721 virtual void OnExtensionLoaded(const Extension* extension) OVERRIDE {
1722 }
1723
1724 virtual void OnExtensionUnloaded(const Extension* extension) OVERRIDE {
1725 }
1726
1727 virtual void OnExtensionUninstalled(const Extension* extension) OVERRIDE {
1728 last_extension_uninstalled = extension->id();
1729 }
1730
1731 virtual void OnAppsReordered() OVERRIDE {
1732 }
1733
1734 virtual void OnAppInstalledToAppList(
1735 const std::string& extension_id) OVERRIDE {
1736 }
1737
1738 virtual void OnShutdown() OVERRIDE {
1739 }
1740
1741 std::string last_extension_installed;
1742 std::string last_extension_uninstalled;
1743};
1744
1745// Test that correct notifications are sent to InstallTracker observers on
1746// extension install and uninstall.
1747TEST_F(ExtensionServiceTest, InstallObserverNotified) {
1748 InitializeEmptyExtensionService();
1749
1750 extensions::InstallTracker* tracker(
1751 extensions::InstallTrackerFactory::GetForProfile(profile_.get()));
1752 MockInstallObserver observer;
1753 tracker->AddObserver(&observer);
1754
1755 // A simple extension that should install without error.
1756 ASSERT_TRUE(observer.last_extension_installed.empty());
1757 base::FilePath path = data_dir_.AppendASCII("good.crx");
1758 InstallCRX(path, INSTALL_NEW);
1759 ASSERT_EQ(good_crx, observer.last_extension_installed);
1760
1761 // Uninstall the extension.
1762 ASSERT_TRUE(observer.last_extension_uninstalled.empty());
1763 UninstallExtension(good_crx, false);
1764 ASSERT_EQ(good_crx, observer.last_extension_uninstalled);
1765
1766 tracker->RemoveObserver(&observer);
1767}
1768
[email protected]1bf73cc32011-10-26 22:38:311769// Tests that flags passed to OnExternalExtensionFileFound() make it to the
1770// extension object.
[email protected]d9a61e12012-11-14 02:43:471771TEST_F(ExtensionServiceTest, InstallingExternalExtensionWithFlags) {
[email protected]a12ce8b22012-01-17 18:40:531772 const char kPrefFromBookmark[] = "from_bookmark";
1773
[email protected]1bf73cc32011-10-26 22:38:311774 InitializeEmptyExtensionService();
1775
[email protected]650b2d52013-02-10 03:41:451776 base::FilePath path = data_dir_.AppendASCII("good.crx");
[email protected]1bf73cc32011-10-26 22:38:311777 set_extensions_enabled(true);
1778
[email protected]a12ce8b22012-01-17 18:40:531779 // Register and install an external extension.
[email protected]c5e4a2222014-01-03 16:06:131780 Version version("1.0.0.0");
[email protected]97d6a5c2013-11-11 23:51:241781 content::WindowedNotificationObserver observer(
1782 chrome::NOTIFICATION_CRX_INSTALLER_DONE,
1783 content::NotificationService::AllSources());
[email protected]6d057a0c2013-07-09 21:12:071784 if (service_->OnExternalExtensionFileFound(
1785 good_crx,
1786 &version,
1787 path,
1788 Manifest::EXTERNAL_PREF,
1789 Extension::FROM_BOOKMARK,
1790 false /* mark_acknowledged */)) {
[email protected]97d6a5c2013-11-11 23:51:241791 observer.Wait();
[email protected]6d057a0c2013-07-09 21:12:071792 }
[email protected]1bf73cc32011-10-26 22:38:311793
1794 const Extension* extension = service_->GetExtensionById(good_crx, false);
1795 ASSERT_TRUE(extension);
[email protected]a12ce8b22012-01-17 18:40:531796 ASSERT_TRUE(extension->from_bookmark());
[email protected]3f2a2fa2013-09-24 02:55:251797 ASSERT_TRUE(ValidateBooleanPref(good_crx, kPrefFromBookmark, true));
[email protected]a12ce8b22012-01-17 18:40:531798
1799 // Upgrade to version 2.0, the flag should be preserved.
1800 path = data_dir_.AppendASCII("good2.crx");
1801 UpdateExtension(good_crx, path, ENABLED);
[email protected]3f2a2fa2013-09-24 02:55:251802 ASSERT_TRUE(ValidateBooleanPref(good_crx, kPrefFromBookmark, true));
[email protected]a12ce8b22012-01-17 18:40:531803 extension = service_->GetExtensionById(good_crx, false);
1804 ASSERT_TRUE(extension);
1805 ASSERT_TRUE(extension->from_bookmark());
[email protected]1bf73cc32011-10-26 22:38:311806}
1807
[email protected]79c833b52011-04-05 18:31:011808// Test the handling of Extension::EXTERNAL_EXTENSION_UNINSTALLED
[email protected]d9a61e12012-11-14 02:43:471809TEST_F(ExtensionServiceTest, UninstallingExternalExtensions) {
[email protected]eaa7dd182010-12-14 11:09:001810 InitializeEmptyExtensionService();
[email protected]683d0702010-12-06 16:25:571811
[email protected]650b2d52013-02-10 03:41:451812 base::FilePath path = data_dir_.AppendASCII("good.crx");
[email protected]683d0702010-12-06 16:25:571813 set_extensions_enabled(true);
1814
1815 // Install an external extension.
[email protected]c5e4a2222014-01-03 16:06:131816 Version version("1.0.0.0");
[email protected]97d6a5c2013-11-11 23:51:241817 content::WindowedNotificationObserver observer(
1818 chrome::NOTIFICATION_CRX_INSTALLER_DONE,
1819 content::NotificationService::AllSources());
[email protected]6d057a0c2013-07-09 21:12:071820 if (service_->OnExternalExtensionFileFound(good_crx, &version,
1821 path, Manifest::EXTERNAL_PREF,
1822 Extension::NO_FLAGS, false)) {
[email protected]97d6a5c2013-11-11 23:51:241823 observer.Wait();
[email protected]6d057a0c2013-07-09 21:12:071824 }
1825
[email protected]604322d2011-03-22 16:51:561826 ASSERT_TRUE(service_->GetExtensionById(good_crx, false));
[email protected]683d0702010-12-06 16:25:571827
1828 // Uninstall it and check that its killbit gets set.
[email protected]fa2416f2011-05-03 08:41:201829 UninstallExtension(good_crx, false);
[email protected]79c833b52011-04-05 18:31:011830 ValidateIntegerPref(good_crx, "location",
1831 Extension::EXTERNAL_EXTENSION_UNINSTALLED);
[email protected]683d0702010-12-06 16:25:571832
1833 // Try to re-install it externally. This should fail because of the killbit.
[email protected]12126d372012-07-11 18:40:531834 service_->OnExternalExtensionFileFound(good_crx, &version,
[email protected]1d5e58b2013-01-31 08:41:401835 path, Manifest::EXTERNAL_PREF,
[email protected]47fc70c2011-12-06 07:29:511836 Extension::NO_FLAGS, false);
[email protected]eedc1c752013-08-09 18:51:551837 base::RunLoop().RunUntilIdle();
[email protected]683d0702010-12-06 16:25:571838 ASSERT_TRUE(NULL == service_->GetExtensionById(good_crx, false));
[email protected]79c833b52011-04-05 18:31:011839 ValidateIntegerPref(good_crx, "location",
1840 Extension::EXTERNAL_EXTENSION_UNINSTALLED);
[email protected]683d0702010-12-06 16:25:571841
[email protected]c5e4a2222014-01-03 16:06:131842 version = Version("1.0.0.1");
[email protected]683d0702010-12-06 16:25:571843 // Repeat the same thing with a newer version of the extension.
[email protected]e85e34c32011-04-13 18:38:351844 path = data_dir_.AppendASCII("good2.crx");
[email protected]12126d372012-07-11 18:40:531845 service_->OnExternalExtensionFileFound(good_crx, &version,
[email protected]1d5e58b2013-01-31 08:41:401846 path, Manifest::EXTERNAL_PREF,
[email protected]47fc70c2011-12-06 07:29:511847 Extension::NO_FLAGS, false);
[email protected]eedc1c752013-08-09 18:51:551848 base::RunLoop().RunUntilIdle();
[email protected]683d0702010-12-06 16:25:571849 ASSERT_TRUE(NULL == service_->GetExtensionById(good_crx, false));
[email protected]79c833b52011-04-05 18:31:011850 ValidateIntegerPref(good_crx, "location",
1851 Extension::EXTERNAL_EXTENSION_UNINSTALLED);
[email protected]683d0702010-12-06 16:25:571852
1853 // Try adding the same extension from an external update URL.
[email protected]9060d8b02012-01-13 02:14:301854 ASSERT_FALSE(service_->pending_extension_manager()->AddFromExternalUpdateUrl(
[email protected]683d0702010-12-06 16:25:571855 good_crx,
1856 GURL("http:://fake.update/url"),
[email protected]464213a2013-10-15 01:06:481857 Manifest::EXTERNAL_PREF_DOWNLOAD,
1858 Extension::NO_FLAGS,
1859 false));
[email protected]b2907fd2011-03-25 16:43:371860
1861 ASSERT_FALSE(service_->pending_extension_manager()->IsIdPending(good_crx));
[email protected]683d0702010-12-06 16:25:571862}
1863
[email protected]0f48fca2011-05-19 18:46:351864// Test that uninstalling an external extension does not crash when
1865// the extension could not be loaded.
1866// This extension shown in preferences file requires an experimental permission.
1867// It could not be loaded without such permission.
[email protected]d9a61e12012-11-14 02:43:471868TEST_F(ExtensionServiceTest, UninstallingNotLoadedExtension) {
[email protected]650b2d52013-02-10 03:41:451869 base::FilePath source_install_dir = data_dir_
[email protected]0f48fca2011-05-19 18:46:351870 .AppendASCII("good")
1871 .AppendASCII("Extensions");
1872 // The preference contains an external extension
1873 // that requires 'experimental' permission.
[email protected]650b2d52013-02-10 03:41:451874 base::FilePath pref_path = source_install_dir
[email protected]0f48fca2011-05-19 18:46:351875 .DirName()
1876 .AppendASCII("PreferencesExperimental");
1877
1878 // Aforementioned extension will not be loaded if
1879 // there is no '--enable-experimental-extension-apis' command line flag.
1880 InitializeInstalledExtensionService(pref_path, source_install_dir);
1881
1882 service_->Init();
[email protected]0f48fca2011-05-19 18:46:351883
1884 // Check and try to uninstall it.
1885 // If we don't check whether the extension is loaded before we uninstall it
1886 // in CheckExternalUninstall, a crash will happen here because we will get or
1887 // dereference a NULL pointer (extension) inside UninstallExtension.
[email protected]1d5e58b2013-01-31 08:41:401888 MockExtensionProvider provider(NULL, Manifest::EXTERNAL_REGISTRY);
[email protected]50067e52011-10-20 23:17:071889 service_->OnExternalProviderReady(&provider);
[email protected]0f48fca2011-05-19 18:46:351890}
1891
[email protected]604322d2011-03-22 16:51:561892// Test that external extensions with incorrect IDs are not installed.
[email protected]d9a61e12012-11-14 02:43:471893TEST_F(ExtensionServiceTest, FailOnWrongId) {
[email protected]604322d2011-03-22 16:51:561894 InitializeEmptyExtensionService();
[email protected]650b2d52013-02-10 03:41:451895 base::FilePath path = data_dir_.AppendASCII("good.crx");
[email protected]604322d2011-03-22 16:51:561896 set_extensions_enabled(true);
1897
[email protected]c5e4a2222014-01-03 16:06:131898 Version version("1.0.0.0");
[email protected]604322d2011-03-22 16:51:561899
1900 const std::string wrong_id = all_zero;
1901 const std::string correct_id = good_crx;
1902 ASSERT_NE(correct_id, wrong_id);
1903
1904 // Install an external extension with an ID from the external
1905 // source that is not equal to the ID in the extension manifest.
[email protected]97d6a5c2013-11-11 23:51:241906 content::WindowedNotificationObserver observer(
1907 chrome::NOTIFICATION_CRX_INSTALLER_DONE,
1908 content::NotificationService::AllSources());
[email protected]604322d2011-03-22 16:51:561909 service_->OnExternalExtensionFileFound(
[email protected]1d5e58b2013-01-31 08:41:401910 wrong_id, &version, path, Manifest::EXTERNAL_PREF,
[email protected]47fc70c2011-12-06 07:29:511911 Extension::NO_FLAGS, false);
[email protected]604322d2011-03-22 16:51:561912
[email protected]97d6a5c2013-11-11 23:51:241913 observer.Wait();
[email protected]604322d2011-03-22 16:51:561914 ASSERT_FALSE(service_->GetExtensionById(good_crx, false));
1915
1916 // Try again with the right ID. Expect success.
[email protected]97d6a5c2013-11-11 23:51:241917 content::WindowedNotificationObserver observer2(
1918 chrome::NOTIFICATION_CRX_INSTALLER_DONE,
1919 content::NotificationService::AllSources());
[email protected]6d057a0c2013-07-09 21:12:071920 if (service_->OnExternalExtensionFileFound(
1921 correct_id, &version, path, Manifest::EXTERNAL_PREF,
1922 Extension::NO_FLAGS, false)) {
[email protected]97d6a5c2013-11-11 23:51:241923 observer2.Wait();
[email protected]6d057a0c2013-07-09 21:12:071924 }
[email protected]604322d2011-03-22 16:51:561925 ASSERT_TRUE(service_->GetExtensionById(good_crx, false));
1926}
1927
1928// Test that external extensions with incorrect versions are not installed.
[email protected]d9a61e12012-11-14 02:43:471929TEST_F(ExtensionServiceTest, FailOnWrongVersion) {
[email protected]604322d2011-03-22 16:51:561930 InitializeEmptyExtensionService();
[email protected]650b2d52013-02-10 03:41:451931 base::FilePath path = data_dir_.AppendASCII("good.crx");
[email protected]604322d2011-03-22 16:51:561932 set_extensions_enabled(true);
1933
1934 // Install an external extension with a version from the external
1935 // source that is not equal to the version in the extension manifest.
[email protected]c5e4a2222014-01-03 16:06:131936 Version wrong_version("1.2.3.4");
[email protected]97d6a5c2013-11-11 23:51:241937 content::WindowedNotificationObserver observer(
1938 chrome::NOTIFICATION_CRX_INSTALLER_DONE,
1939 content::NotificationService::AllSources());
[email protected]604322d2011-03-22 16:51:561940 service_->OnExternalExtensionFileFound(
[email protected]1d5e58b2013-01-31 08:41:401941 good_crx, &wrong_version, path, Manifest::EXTERNAL_PREF,
[email protected]47fc70c2011-12-06 07:29:511942 Extension::NO_FLAGS, false);
[email protected]604322d2011-03-22 16:51:561943
[email protected]97d6a5c2013-11-11 23:51:241944 observer.Wait();
[email protected]604322d2011-03-22 16:51:561945 ASSERT_FALSE(service_->GetExtensionById(good_crx, false));
1946
1947 // Try again with the right version. Expect success.
[email protected]e3987852012-05-04 10:06:301948 service_->pending_extension_manager()->Remove(good_crx);
[email protected]c5e4a2222014-01-03 16:06:131949 Version correct_version("1.0.0.0");
[email protected]97d6a5c2013-11-11 23:51:241950 content::WindowedNotificationObserver observer2(
1951 chrome::NOTIFICATION_CRX_INSTALLER_DONE,
1952 content::NotificationService::AllSources());
[email protected]6d057a0c2013-07-09 21:12:071953 if (service_->OnExternalExtensionFileFound(
1954 good_crx, &correct_version, path, Manifest::EXTERNAL_PREF,
1955 Extension::NO_FLAGS, false)) {
[email protected]97d6a5c2013-11-11 23:51:241956 observer2.Wait();
[email protected]6d057a0c2013-07-09 21:12:071957 }
[email protected]604322d2011-03-22 16:51:561958 ASSERT_TRUE(service_->GetExtensionById(good_crx, false));
1959}
1960
[email protected]da0aa3b2009-12-06 21:41:031961// Install a user script (they get converted automatically to an extension)
[email protected]d9a61e12012-11-14 02:43:471962TEST_F(ExtensionServiceTest, InstallUserScript) {
[email protected]da0aa3b2009-12-06 21:41:031963 // The details of script conversion are tested elsewhere, this just tests
[email protected]eaa7dd182010-12-14 11:09:001964 // integration with ExtensionService.
1965 InitializeEmptyExtensionService();
[email protected]da0aa3b2009-12-06 21:41:031966
[email protected]650b2d52013-02-10 03:41:451967 base::FilePath path = data_dir_
[email protected]da0aa3b2009-12-06 21:41:031968 .AppendASCII("user_script_basic.user.js");
1969
[email protected]7567484142013-07-11 17:36:071970 ASSERT_TRUE(base::PathExists(path));
[email protected]f8636f92013-08-09 21:02:371971 scoped_refptr<CrxInstaller> installer(CrxInstaller::CreateSilent(service_));
[email protected]0d3e4a22011-06-23 19:02:521972 installer->set_allow_silent_install(true);
[email protected]6dfbbf82010-03-12 23:09:161973 installer->InstallUserScript(
[email protected]da0aa3b2009-12-06 21:41:031974 path,
[email protected]6dfbbf82010-03-12 23:09:161975 GURL("http://www.aaronboodman.com/scripts/user_script_basic.user.js"));
[email protected]da0aa3b2009-12-06 21:41:031976
[email protected]eedc1c752013-08-09 18:51:551977 base::RunLoop().RunUntilIdle();
[email protected]d2065e062013-12-12 23:49:521978 std::vector<base::string16> errors = GetErrors();
[email protected]da0aa3b2009-12-06 21:41:031979 EXPECT_TRUE(installed_) << "Nothing was installed.";
[email protected]41bb80bd2013-05-03 10:56:021980 EXPECT_FALSE(was_update_) << path.value();
[email protected]da0aa3b2009-12-06 21:41:031981 ASSERT_EQ(1u, loaded_.size()) << "Nothing was loaded.";
1982 EXPECT_EQ(0u, errors.size()) << "There were errors: "
1983 << JoinString(errors, ',');
1984 EXPECT_TRUE(service_->GetExtensionById(loaded_[0]->id(), false)) <<
1985 path.value();
1986
1987 installed_ = NULL;
[email protected]41bb80bd2013-05-03 10:56:021988 was_update_ = false;
[email protected]da0aa3b2009-12-06 21:41:031989 loaded_.clear();
1990 ExtensionErrorReporter::GetInstance()->ClearErrors();
1991}
1992
[email protected]3c4abc82012-10-22 22:25:541993// Extensions don't install during shutdown.
[email protected]d9a61e12012-11-14 02:43:471994TEST_F(ExtensionServiceTest, InstallExtensionDuringShutdown) {
[email protected]3c4abc82012-10-22 22:25:541995 InitializeEmptyExtensionService();
1996
1997 // Simulate shutdown.
1998 service_->set_browser_terminating_for_test(true);
1999
[email protected]650b2d52013-02-10 03:41:452000 base::FilePath path = data_dir_.AppendASCII("good.crx");
[email protected]f8636f92013-08-09 21:02:372001 scoped_refptr<CrxInstaller> installer(CrxInstaller::CreateSilent(service_));
[email protected]3c4abc82012-10-22 22:25:542002 installer->set_allow_silent_install(true);
2003 installer->InstallCrx(path);
[email protected]eedc1c752013-08-09 18:51:552004 base::RunLoop().RunUntilIdle();
[email protected]3c4abc82012-10-22 22:25:542005
2006 EXPECT_FALSE(installed_) << "Extension installed during shutdown.";
2007 ASSERT_EQ(0u, loaded_.size()) << "Extension loaded during shutdown.";
2008}
2009
[email protected]8d888c12010-11-30 00:00:252010// This tests that the granted permissions preferences are correctly set when
2011// installing an extension.
[email protected]d9a61e12012-11-14 02:43:472012TEST_F(ExtensionServiceTest, GrantedPermissions) {
[email protected]eaa7dd182010-12-14 11:09:002013 InitializeEmptyExtensionService();
[email protected]650b2d52013-02-10 03:41:452014 base::FilePath path = data_dir_
[email protected]8d888c12010-11-30 00:00:252015 .AppendASCII("permissions");
2016
[email protected]650b2d52013-02-10 03:41:452017 base::FilePath pem_path = path.AppendASCII("unknown.pem");
[email protected]8d888c12010-11-30 00:00:252018 path = path.AppendASCII("unknown");
2019
[email protected]7567484142013-07-11 17:36:072020 ASSERT_TRUE(base::PathExists(pem_path));
2021 ASSERT_TRUE(base::PathExists(path));
[email protected]8d888c12010-11-30 00:00:252022
2023 ExtensionPrefs* prefs = service_->extension_prefs();
2024
[email protected]c2e66e12012-06-27 06:27:062025 APIPermissionSet expected_api_perms;
[email protected]cced75a2011-05-20 08:31:122026 URLPatternSet expected_host_perms;
[email protected]8d888c12010-11-30 00:00:252027
2028 // Make sure there aren't any granted permissions before the
2029 // extension is installed.
[email protected]c2e66e12012-06-27 06:27:062030 scoped_refptr<PermissionSet> known_perms(
[email protected]0d3e4a22011-06-23 19:02:522031 prefs->GetGrantedPermissions(permissions_crx));
2032 EXPECT_FALSE(known_perms.get());
[email protected]8d888c12010-11-30 00:00:252033
[email protected]8f512c72011-11-22 21:02:502034 const Extension* extension = PackAndInstallCRX(path, pem_path, INSTALL_NEW);
[email protected]8d888c12010-11-30 00:00:252035
2036 EXPECT_EQ(0u, GetErrors().size());
2037 ASSERT_EQ(1u, service_->extensions()->size());
[email protected]8f512c72011-11-22 21:02:502038 EXPECT_EQ(permissions_crx, extension->id());
[email protected]8d888c12010-11-30 00:00:252039
[email protected]8d888c12010-11-30 00:00:252040 // Verify that the valid API permissions have been recognized.
[email protected]c2e66e12012-06-27 06:27:062041 expected_api_perms.insert(APIPermission::kTab);
[email protected]8d888c12010-11-30 00:00:252042
2043 AddPattern(&expected_host_perms, "http://*.google.com/*");
2044 AddPattern(&expected_host_perms, "https://*.google.com/*");
[email protected]d6a5c78c2010-12-07 05:18:152045 AddPattern(&expected_host_perms, "http://*.google.com.hk/*");
[email protected]8d888c12010-11-30 00:00:252046 AddPattern(&expected_host_perms, "http://www.example.com/*");
2047
[email protected]8f512c72011-11-22 21:02:502048 known_perms = prefs->GetGrantedPermissions(extension->id());
[email protected]0d3e4a22011-06-23 19:02:522049 EXPECT_TRUE(known_perms.get());
2050 EXPECT_FALSE(known_perms->IsEmpty());
2051 EXPECT_EQ(expected_api_perms, known_perms->apis());
2052 EXPECT_FALSE(known_perms->HasEffectiveFullAccess());
[email protected]06e8b8ff2011-07-13 15:03:472053 EXPECT_EQ(expected_host_perms, known_perms->effective_hosts());
[email protected]8d888c12010-11-30 00:00:252054}
2055
[email protected]be083862012-09-01 03:53:452056
2057#if !defined(OS_CHROMEOS)
2058// This tests that the granted permissions preferences are correctly set for
2059// default apps.
[email protected]d9a61e12012-11-14 02:43:472060TEST_F(ExtensionServiceTest, DefaultAppsGrantedPermissions) {
[email protected]be083862012-09-01 03:53:452061 InitializeEmptyExtensionService();
[email protected]650b2d52013-02-10 03:41:452062 base::FilePath path = data_dir_
[email protected]be083862012-09-01 03:53:452063 .AppendASCII("permissions");
2064
[email protected]650b2d52013-02-10 03:41:452065 base::FilePath pem_path = path.AppendASCII("unknown.pem");
[email protected]be083862012-09-01 03:53:452066 path = path.AppendASCII("unknown");
2067
[email protected]7567484142013-07-11 17:36:072068 ASSERT_TRUE(base::PathExists(pem_path));
2069 ASSERT_TRUE(base::PathExists(path));
[email protected]be083862012-09-01 03:53:452070
2071 ExtensionPrefs* prefs = service_->extension_prefs();
2072
2073 APIPermissionSet expected_api_perms;
2074 URLPatternSet expected_host_perms;
2075
2076 // Make sure there aren't any granted permissions before the
2077 // extension is installed.
2078 scoped_refptr<PermissionSet> known_perms(
2079 prefs->GetGrantedPermissions(permissions_crx));
2080 EXPECT_FALSE(known_perms.get());
2081
2082 const Extension* extension = PackAndInstallCRX(
2083 path, pem_path, INSTALL_NEW, Extension::WAS_INSTALLED_BY_DEFAULT);
2084
2085 EXPECT_EQ(0u, GetErrors().size());
2086 ASSERT_EQ(1u, service_->extensions()->size());
2087 EXPECT_EQ(permissions_crx, extension->id());
2088
2089 // Verify that the valid API permissions have been recognized.
2090 expected_api_perms.insert(APIPermission::kTab);
2091
2092 known_perms = prefs->GetGrantedPermissions(extension->id());
2093 EXPECT_TRUE(known_perms.get());
2094 EXPECT_FALSE(known_perms->IsEmpty());
2095 EXPECT_EQ(expected_api_perms, known_perms->apis());
2096 EXPECT_FALSE(known_perms->HasEffectiveFullAccess());
2097}
2098#endif
2099
[email protected]8d888c12010-11-30 00:00:252100#if !defined(OS_CHROMEOS)
2101// Tests that the granted permissions full_access bit gets set correctly when
2102// an extension contains an NPAPI plugin. Don't run this test on Chrome OS
2103// since they don't support plugins.
[email protected]d9a61e12012-11-14 02:43:472104TEST_F(ExtensionServiceTest, GrantedFullAccessPermissions) {
[email protected]ebd71962012-12-20 02:56:552105 InitPluginService();
[email protected]183d4b82011-11-11 18:50:262106
[email protected]eaa7dd182010-12-14 11:09:002107 InitializeEmptyExtensionService();
[email protected]8d888c12010-11-30 00:00:252108
[email protected]3f2a2fa2013-09-24 02:55:252109 ASSERT_TRUE(base::PathExists(good1_path()));
2110 const Extension* extension = PackAndInstallCRX(good1_path(), INSTALL_NEW);
[email protected]8d888c12010-11-30 00:00:252111 EXPECT_EQ(0u, GetErrors().size());
2112 EXPECT_EQ(1u, service_->extensions()->size());
[email protected]8d888c12010-11-30 00:00:252113 ExtensionPrefs* prefs = service_->extension_prefs();
2114
[email protected]c2e66e12012-06-27 06:27:062115 scoped_refptr<PermissionSet> permissions(
[email protected]8f512c72011-11-22 21:02:502116 prefs->GetGrantedPermissions(extension->id()));
[email protected]0d3e4a22011-06-23 19:02:522117 EXPECT_FALSE(permissions->IsEmpty());
2118 EXPECT_TRUE(permissions->HasEffectiveFullAccess());
2119 EXPECT_FALSE(permissions->apis().empty());
[email protected]c2e66e12012-06-27 06:27:062120 EXPECT_TRUE(permissions->HasAPIPermission(APIPermission::kPlugin));
[email protected]8d888c12010-11-30 00:00:252121
[email protected]0d3e4a22011-06-23 19:02:522122 // Full access implies full host access too...
2123 EXPECT_TRUE(permissions->HasEffectiveAccessToAllHosts());
[email protected]8d888c12010-11-30 00:00:252124}
2125#endif
2126
2127// Tests that the extension is disabled when permissions are missing from
2128// the extension's granted permissions preferences. (This simulates updating
2129// the browser to a version which recognizes more permissions).
[email protected]d9a61e12012-11-14 02:43:472130TEST_F(ExtensionServiceTest, GrantedAPIAndHostPermissions) {
[email protected]eaa7dd182010-12-14 11:09:002131 InitializeEmptyExtensionService();
[email protected]8d888c12010-11-30 00:00:252132
[email protected]650b2d52013-02-10 03:41:452133 base::FilePath path = data_dir_
[email protected]8d888c12010-11-30 00:00:252134 .AppendASCII("permissions")
2135 .AppendASCII("unknown");
2136
[email protected]7567484142013-07-11 17:36:072137 ASSERT_TRUE(base::PathExists(path));
[email protected]8d888c12010-11-30 00:00:252138
[email protected]8f512c72011-11-22 21:02:502139 const Extension* extension = PackAndInstallCRX(path, INSTALL_NEW);
[email protected]8d888c12010-11-30 00:00:252140
2141 EXPECT_EQ(0u, GetErrors().size());
2142 EXPECT_EQ(1u, service_->extensions()->size());
[email protected]8d888c12010-11-30 00:00:252143 std::string extension_id = extension->id();
2144
2145 ExtensionPrefs* prefs = service_->extension_prefs();
2146
[email protected]c2e66e12012-06-27 06:27:062147 APIPermissionSet expected_api_permissions;
[email protected]cced75a2011-05-20 08:31:122148 URLPatternSet expected_host_permissions;
[email protected]8d888c12010-11-30 00:00:252149
[email protected]c2e66e12012-06-27 06:27:062150 expected_api_permissions.insert(APIPermission::kTab);
[email protected]8d888c12010-11-30 00:00:252151 AddPattern(&expected_host_permissions, "http://*.google.com/*");
2152 AddPattern(&expected_host_permissions, "https://*.google.com/*");
[email protected]d6a5c78c2010-12-07 05:18:152153 AddPattern(&expected_host_permissions, "http://*.google.com.hk/*");
[email protected]8d888c12010-11-30 00:00:252154 AddPattern(&expected_host_permissions, "http://www.example.com/*");
2155
[email protected]8d888c12010-11-30 00:00:252156 std::set<std::string> host_permissions;
2157
2158 // Test that the extension is disabled when an API permission is missing from
2159 // the extension's granted api permissions preference. (This simulates
2160 // updating the browser to a version which recognizes a new API permission).
[email protected]0d3e4a22011-06-23 19:02:522161 SetPref(extension_id, "granted_permissions.api",
[email protected]023b3d12013-12-23 18:46:492162 new base::ListValue(), "granted_permissions.api");
[email protected]bb1bc9b32013-12-21 03:09:142163 service_->ReloadExtensionsForTest();
[email protected]8d888c12010-11-30 00:00:252164
2165 EXPECT_EQ(1u, service_->disabled_extensions()->size());
[email protected]cadac622013-06-11 16:46:362166 extension = service_->disabled_extensions()->begin()->get();
[email protected]8d888c12010-11-30 00:00:252167
[email protected]ad83ca242011-07-29 01:32:252168 ASSERT_TRUE(prefs->IsExtensionDisabled(extension_id));
[email protected]36429da2011-07-11 20:25:182169 ASSERT_FALSE(service_->IsExtensionEnabled(extension_id));
[email protected]8d888c12010-11-30 00:00:252170 ASSERT_TRUE(prefs->DidExtensionEscalatePermissions(extension_id));
2171
2172 // Now grant and re-enable the extension, making sure the prefs are updated.
[email protected]009633c2013-03-07 22:08:282173 service_->GrantPermissionsAndEnableExtension(extension);
[email protected]8d888c12010-11-30 00:00:252174
[email protected]ad83ca242011-07-29 01:32:252175 ASSERT_FALSE(prefs->IsExtensionDisabled(extension_id));
[email protected]36429da2011-07-11 20:25:182176 ASSERT_TRUE(service_->IsExtensionEnabled(extension_id));
[email protected]8d888c12010-11-30 00:00:252177 ASSERT_FALSE(prefs->DidExtensionEscalatePermissions(extension_id));
2178
[email protected]c2e66e12012-06-27 06:27:062179 scoped_refptr<PermissionSet> current_perms(
[email protected]0d3e4a22011-06-23 19:02:522180 prefs->GetGrantedPermissions(extension_id));
2181 ASSERT_TRUE(current_perms.get());
2182 ASSERT_FALSE(current_perms->IsEmpty());
2183 ASSERT_FALSE(current_perms->HasEffectiveFullAccess());
2184 ASSERT_EQ(expected_api_permissions, current_perms->apis());
[email protected]06e8b8ff2011-07-13 15:03:472185 ASSERT_EQ(expected_host_permissions, current_perms->effective_hosts());
[email protected]8d888c12010-11-30 00:00:252186
2187 // Tests that the extension is disabled when a host permission is missing from
2188 // the extension's granted host permissions preference. (This simulates
2189 // updating the browser to a version which recognizes additional host
2190 // permissions).
[email protected]8d888c12010-11-30 00:00:252191 host_permissions.clear();
[email protected]902fd7b2011-07-27 18:42:312192 current_perms = NULL;
[email protected]8d888c12010-11-30 00:00:252193
[email protected]8d888c12010-11-30 00:00:252194 host_permissions.insert("http://*.google.com/*");
2195 host_permissions.insert("https://*.google.com/*");
[email protected]d6a5c78c2010-12-07 05:18:152196 host_permissions.insert("http://*.google.com.hk/*");
[email protected]8d888c12010-11-30 00:00:252197
[email protected]023b3d12013-12-23 18:46:492198 base::ListValue* api_permissions = new base::ListValue();
[email protected]0d3e4a22011-06-23 19:02:522199 api_permissions->Append(
[email protected]aa15e2e2013-08-14 02:13:582200 new base::StringValue("tabs"));
[email protected]0d3e4a22011-06-23 19:02:522201 SetPref(extension_id, "granted_permissions.api",
2202 api_permissions, "granted_permissions.api");
2203 SetPrefStringSet(
2204 extension_id, "granted_permissions.scriptable_host", host_permissions);
[email protected]8d888c12010-11-30 00:00:252205
[email protected]bb1bc9b32013-12-21 03:09:142206 service_->ReloadExtensionsForTest();
[email protected]8d888c12010-11-30 00:00:252207
2208 EXPECT_EQ(1u, service_->disabled_extensions()->size());
[email protected]cadac622013-06-11 16:46:362209 extension = service_->disabled_extensions()->begin()->get();
[email protected]8d888c12010-11-30 00:00:252210
[email protected]ad83ca242011-07-29 01:32:252211 ASSERT_TRUE(prefs->IsExtensionDisabled(extension_id));
[email protected]36429da2011-07-11 20:25:182212 ASSERT_FALSE(service_->IsExtensionEnabled(extension_id));
[email protected]8d888c12010-11-30 00:00:252213 ASSERT_TRUE(prefs->DidExtensionEscalatePermissions(extension_id));
2214
2215 // Now grant and re-enable the extension, making sure the prefs are updated.
[email protected]009633c2013-03-07 22:08:282216 service_->GrantPermissionsAndEnableExtension(extension);
[email protected]8d888c12010-11-30 00:00:252217
[email protected]36429da2011-07-11 20:25:182218 ASSERT_TRUE(service_->IsExtensionEnabled(extension_id));
[email protected]8d888c12010-11-30 00:00:252219 ASSERT_FALSE(prefs->DidExtensionEscalatePermissions(extension_id));
2220
[email protected]902fd7b2011-07-27 18:42:312221 current_perms = prefs->GetGrantedPermissions(extension_id);
[email protected]0d3e4a22011-06-23 19:02:522222 ASSERT_TRUE(current_perms.get());
2223 ASSERT_FALSE(current_perms->IsEmpty());
2224 ASSERT_FALSE(current_perms->HasEffectiveFullAccess());
2225 ASSERT_EQ(expected_api_permissions, current_perms->apis());
[email protected]06e8b8ff2011-07-13 15:03:472226 ASSERT_EQ(expected_host_permissions, current_perms->effective_hosts());
[email protected]8d888c12010-11-30 00:00:252227}
2228
[email protected]a17f9462009-06-09 02:56:412229// Test Packaging and installing an extension.
[email protected]d9a61e12012-11-14 02:43:472230TEST_F(ExtensionServiceTest, PackExtension) {
[email protected]eaa7dd182010-12-14 11:09:002231 InitializeEmptyExtensionService();
[email protected]650b2d52013-02-10 03:41:452232 base::FilePath input_directory = data_dir_
[email protected]a9b00ac2009-06-25 21:03:232233 .AppendASCII("good")
2234 .AppendASCII("Extensions")
2235 .AppendASCII("behllobkkfkfnphdnhnkndlbkcpglgmj")
2236 .AppendASCII("1.0.0.0");
[email protected]a17f9462009-06-09 02:56:412237
[email protected]ea1a3f62012-11-16 20:34:232238 base::ScopedTempDir temp_dir;
[email protected]aca3e9b2009-11-03 01:14:212239 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
[email protected]650b2d52013-02-10 03:41:452240 base::FilePath output_directory = temp_dir.path();
[email protected]aca3e9b2009-11-03 01:14:212241
[email protected]650b2d52013-02-10 03:41:452242 base::FilePath crx_path(output_directory.AppendASCII("ex1.crx"));
2243 base::FilePath privkey_path(output_directory.AppendASCII("privkey.pem"));
[email protected]a17f9462009-06-09 02:56:412244
2245 scoped_ptr<ExtensionCreator> creator(new ExtensionCreator());
[email protected]650b2d52013-02-10 03:41:452246 ASSERT_TRUE(creator->Run(input_directory, crx_path, base::FilePath(),
[email protected]93d973a2012-01-08 07:38:262247 privkey_path, ExtensionCreator::kNoRunFlags));
[email protected]7567484142013-07-11 17:36:072248 ASSERT_TRUE(base::PathExists(crx_path));
2249 ASSERT_TRUE(base::PathExists(privkey_path));
[email protected]93d973a2012-01-08 07:38:262250
2251 // Repeat the run with the pem file gone, and no special flags
2252 // Should refuse to overwrite the existing crx.
[email protected]dd3aa792013-07-16 19:10:232253 base::DeleteFile(privkey_path, false);
[email protected]650b2d52013-02-10 03:41:452254 ASSERT_FALSE(creator->Run(input_directory, crx_path, base::FilePath(),
[email protected]93d973a2012-01-08 07:38:262255 privkey_path, ExtensionCreator::kNoRunFlags));
2256
2257 // OK, now try it with a flag to overwrite existing crx. Should work.
[email protected]650b2d52013-02-10 03:41:452258 ASSERT_TRUE(creator->Run(input_directory, crx_path, base::FilePath(),
[email protected]93d973a2012-01-08 07:38:262259 privkey_path, ExtensionCreator::kOverwriteCRX));
2260
2261 // Repeat the run allowing existing crx, but the existing pem is still
2262 // an error. Should fail.
[email protected]650b2d52013-02-10 03:41:452263 ASSERT_FALSE(creator->Run(input_directory, crx_path, base::FilePath(),
[email protected]93d973a2012-01-08 07:38:262264 privkey_path, ExtensionCreator::kOverwriteCRX));
[email protected]a17f9462009-06-09 02:56:412265
[email protected]7567484142013-07-11 17:36:072266 ASSERT_TRUE(base::PathExists(privkey_path));
[email protected]8f512c72011-11-22 21:02:502267 InstallCRX(crx_path, INSTALL_NEW);
[email protected]0dc2ca82009-11-17 07:06:162268
2269 // Try packing with invalid paths.
2270 creator.reset(new ExtensionCreator());
[email protected]650b2d52013-02-10 03:41:452271 ASSERT_FALSE(
2272 creator->Run(base::FilePath(), base::FilePath(), base::FilePath(),
2273 base::FilePath(), ExtensionCreator::kOverwriteCRX));
[email protected]0dc2ca82009-11-17 07:06:162274
2275 // Try packing an empty directory. Should fail because an empty directory is
2276 // not a valid extension.
[email protected]ea1a3f62012-11-16 20:34:232277 base::ScopedTempDir temp_dir2;
[email protected]0dc2ca82009-11-17 07:06:162278 ASSERT_TRUE(temp_dir2.CreateUniqueTempDir());
2279 creator.reset(new ExtensionCreator());
2280 ASSERT_FALSE(creator->Run(temp_dir2.path(), crx_path, privkey_path,
[email protected]650b2d52013-02-10 03:41:452281 base::FilePath(), ExtensionCreator::kOverwriteCRX));
[email protected]0dc2ca82009-11-17 07:06:162282
2283 // Try packing with an invalid manifest.
2284 std::string invalid_manifest_content = "I am not a manifest.";
2285 ASSERT_TRUE(file_util::WriteFile(
[email protected]993da5e2013-03-23 21:25:162286 temp_dir2.path().Append(extensions::kManifestFilename),
[email protected]0dc2ca82009-11-17 07:06:162287 invalid_manifest_content.c_str(), invalid_manifest_content.size()));
2288 creator.reset(new ExtensionCreator());
2289 ASSERT_FALSE(creator->Run(temp_dir2.path(), crx_path, privkey_path,
[email protected]650b2d52013-02-10 03:41:452290 base::FilePath(), ExtensionCreator::kOverwriteCRX));
[email protected]c21b5b612013-07-31 07:49:162291
2292 // Try packing with a private key that is a valid key, but invalid for the
2293 // extension.
2294 base::FilePath bad_private_key_dir = data_dir_.AppendASCII("bad_private_key");
2295 crx_path = output_directory.AppendASCII("bad_private_key.crx");
2296 privkey_path = data_dir_.AppendASCII("bad_private_key.pem");
2297 ASSERT_FALSE(creator->Run(bad_private_key_dir, crx_path, base::FilePath(),
2298 privkey_path, ExtensionCreator::kOverwriteCRX));
[email protected]a17f9462009-06-09 02:56:412299}
2300
[email protected]0349ab5d2010-08-11 21:41:572301// Test Packaging and installing an extension whose name contains punctuation.
[email protected]d9a61e12012-11-14 02:43:472302TEST_F(ExtensionServiceTest, PackPunctuatedExtension) {
[email protected]eaa7dd182010-12-14 11:09:002303 InitializeEmptyExtensionService();
[email protected]650b2d52013-02-10 03:41:452304 base::FilePath input_directory = data_dir_
[email protected]0349ab5d2010-08-11 21:41:572305 .AppendASCII("good")
2306 .AppendASCII("Extensions")
2307 .AppendASCII(good0)
2308 .AppendASCII("1.0.0.0");
2309
[email protected]ea1a3f62012-11-16 20:34:232310 base::ScopedTempDir temp_dir;
[email protected]0349ab5d2010-08-11 21:41:572311 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
2312
2313 // Extension names containing punctuation, and the expected names for the
2314 // packed extensions.
[email protected]650b2d52013-02-10 03:41:452315 const base::FilePath punctuated_names[] = {
2316 base::FilePath(FILE_PATH_LITERAL("this.extensions.name.has.periods")),
2317 base::FilePath(FILE_PATH_LITERAL(".thisextensionsnamestartswithaperiod")),
2318 base::FilePath(FILE_PATH_LITERAL("thisextensionhasaslashinitsname/")).
[email protected]d9034ed22012-02-10 02:04:402319 NormalizePathSeparators(),
[email protected]0349ab5d2010-08-11 21:41:572320 };
[email protected]650b2d52013-02-10 03:41:452321 const base::FilePath expected_crx_names[] = {
2322 base::FilePath(FILE_PATH_LITERAL("this.extensions.name.has.periods.crx")),
2323 base::FilePath(
2324 FILE_PATH_LITERAL(".thisextensionsnamestartswithaperiod.crx")),
2325 base::FilePath(FILE_PATH_LITERAL("thisextensionhasaslashinitsname.crx")),
[email protected]0349ab5d2010-08-11 21:41:572326 };
[email protected]650b2d52013-02-10 03:41:452327 const base::FilePath expected_private_key_names[] = {
2328 base::FilePath(FILE_PATH_LITERAL("this.extensions.name.has.periods.pem")),
2329 base::FilePath(
2330 FILE_PATH_LITERAL(".thisextensionsnamestartswithaperiod.pem")),
2331 base::FilePath(FILE_PATH_LITERAL("thisextensionhasaslashinitsname.pem")),
[email protected]0349ab5d2010-08-11 21:41:572332 };
2333
2334 for (size_t i = 0; i < arraysize(punctuated_names); ++i) {
2335 SCOPED_TRACE(punctuated_names[i].value().c_str());
[email protected]650b2d52013-02-10 03:41:452336 base::FilePath output_dir = temp_dir.path().Append(punctuated_names[i]);
[email protected]0349ab5d2010-08-11 21:41:572337
2338 // Copy the extension into the output directory, as PackExtensionJob doesn't
2339 // let us choose where to output the packed extension.
[email protected]f0ff2ad2013-07-09 17:42:262340 ASSERT_TRUE(base::CopyDirectory(input_directory, output_dir, true));
[email protected]0349ab5d2010-08-11 21:41:572341
[email protected]650b2d52013-02-10 03:41:452342 base::FilePath expected_crx_path =
2343 temp_dir.path().Append(expected_crx_names[i]);
2344 base::FilePath expected_private_key_path =
[email protected]0349ab5d2010-08-11 21:41:572345 temp_dir.path().Append(expected_private_key_names[i]);
2346 PackExtensionTestClient pack_client(expected_crx_path,
2347 expected_private_key_path);
[email protected]d9ede582012-08-14 19:21:382348 scoped_refptr<extensions::PackExtensionJob> packer(
[email protected]650b2d52013-02-10 03:41:452349 new extensions::PackExtensionJob(&pack_client, output_dir,
2350 base::FilePath(),
[email protected]d9ede582012-08-14 19:21:382351 ExtensionCreator::kOverwriteCRX));
[email protected]0349ab5d2010-08-11 21:41:572352 packer->Start();
2353
2354 // The packer will post a notification task to the current thread's message
2355 // loop when it is finished. We manually run the loop here so that we
2356 // block and catch the notification; otherwise, the process would exit.
2357 // This call to |Run()| is matched by a call to |Quit()| in the
2358 // |PackExtensionTestClient|'s notification handling code.
[email protected]b3a25092013-05-28 22:08:162359 base::MessageLoop::current()->Run();
[email protected]0349ab5d2010-08-11 21:41:572360
2361 if (HasFatalFailure())
2362 return;
2363
[email protected]8f512c72011-11-22 21:02:502364 InstallCRX(expected_crx_path, INSTALL_NEW);
[email protected]0349ab5d2010-08-11 21:41:572365 }
2366}
2367
[email protected]d9a61e12012-11-14 02:43:472368TEST_F(ExtensionServiceTest, PackExtensionContainingKeyFails) {
[email protected]ab55c2b2012-06-01 23:55:032369 InitializeEmptyExtensionService();
2370
[email protected]ea1a3f62012-11-16 20:34:232371 base::ScopedTempDir extension_temp_dir;
[email protected]ab55c2b2012-06-01 23:55:032372 ASSERT_TRUE(extension_temp_dir.CreateUniqueTempDir());
[email protected]650b2d52013-02-10 03:41:452373 base::FilePath input_directory = extension_temp_dir.path().AppendASCII("ext");
[email protected]f0ff2ad2013-07-09 17:42:262374 ASSERT_TRUE(base::CopyDirectory(
[email protected]ab55c2b2012-06-01 23:55:032375 data_dir_
2376 .AppendASCII("good")
2377 .AppendASCII("Extensions")
2378 .AppendASCII("behllobkkfkfnphdnhnkndlbkcpglgmj")
2379 .AppendASCII("1.0.0.0"),
2380 input_directory,
2381 /*recursive=*/true));
2382
[email protected]ea1a3f62012-11-16 20:34:232383 base::ScopedTempDir output_temp_dir;
[email protected]ab55c2b2012-06-01 23:55:032384 ASSERT_TRUE(output_temp_dir.CreateUniqueTempDir());
[email protected]650b2d52013-02-10 03:41:452385 base::FilePath output_directory = output_temp_dir.path();
[email protected]ab55c2b2012-06-01 23:55:032386
[email protected]650b2d52013-02-10 03:41:452387 base::FilePath crx_path(output_directory.AppendASCII("ex1.crx"));
2388 base::FilePath privkey_path(output_directory.AppendASCII("privkey.pem"));
[email protected]ab55c2b2012-06-01 23:55:032389
2390 // Pack the extension once to get a private key.
2391 scoped_ptr<ExtensionCreator> creator(new ExtensionCreator());
[email protected]650b2d52013-02-10 03:41:452392 ASSERT_TRUE(creator->Run(input_directory, crx_path, base::FilePath(),
[email protected]ab55c2b2012-06-01 23:55:032393 privkey_path, ExtensionCreator::kNoRunFlags))
2394 << creator->error_message();
[email protected]7567484142013-07-11 17:36:072395 ASSERT_TRUE(base::PathExists(crx_path));
2396 ASSERT_TRUE(base::PathExists(privkey_path));
[email protected]ab55c2b2012-06-01 23:55:032397
[email protected]dd3aa792013-07-16 19:10:232398 base::DeleteFile(crx_path, false);
[email protected]ab55c2b2012-06-01 23:55:032399 // Move the pem file into the extension.
[email protected]5553d5b2013-07-01 23:07:362400 base::Move(privkey_path,
[email protected]ab55c2b2012-06-01 23:55:032401 input_directory.AppendASCII("privkey.pem"));
2402
2403 // This pack should fail because of the contained private key.
[email protected]650b2d52013-02-10 03:41:452404 EXPECT_FALSE(creator->Run(input_directory, crx_path, base::FilePath(),
[email protected]ab55c2b2012-06-01 23:55:032405 privkey_path, ExtensionCreator::kNoRunFlags));
2406 EXPECT_THAT(creator->error_message(),
2407 testing::ContainsRegex(
2408 "extension includes the key file.*privkey.pem"));
2409}
2410
[email protected]a17f9462009-06-09 02:56:412411// Test Packaging and installing an extension using an openssl generated key.
2412// The openssl is generated with the following:
[email protected]a1257b12009-06-12 02:51:342413// > openssl genrsa -out privkey.pem 1024
[email protected]df4956e2009-06-10 16:53:422414// > openssl pkcs8 -topk8 -nocrypt -in privkey.pem -out privkey_asn1.pem
[email protected]a1257b12009-06-12 02:51:342415// The privkey.pem is a PrivateKey, and the pcks8 -topk8 creates a
[email protected]a17f9462009-06-09 02:56:412416// PrivateKeyInfo ASN.1 structure, we our RSAPrivateKey expects.
[email protected]d9a61e12012-11-14 02:43:472417TEST_F(ExtensionServiceTest, PackExtensionOpenSSLKey) {
[email protected]eaa7dd182010-12-14 11:09:002418 InitializeEmptyExtensionService();
[email protected]650b2d52013-02-10 03:41:452419 base::FilePath input_directory = data_dir_
[email protected]a9b00ac2009-06-25 21:03:232420 .AppendASCII("good")
2421 .AppendASCII("Extensions")
2422 .AppendASCII("behllobkkfkfnphdnhnkndlbkcpglgmj")
2423 .AppendASCII("1.0.0.0");
[email protected]650b2d52013-02-10 03:41:452424 base::FilePath privkey_path(data_dir_.AppendASCII(
[email protected]a17f9462009-06-09 02:56:412425 "openssl_privkey_asn1.pem"));
[email protected]7567484142013-07-11 17:36:072426 ASSERT_TRUE(base::PathExists(privkey_path));
[email protected]a17f9462009-06-09 02:56:412427
[email protected]ea1a3f62012-11-16 20:34:232428 base::ScopedTempDir temp_dir;
[email protected]aca3e9b2009-11-03 01:14:212429 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
[email protected]650b2d52013-02-10 03:41:452430 base::FilePath output_directory = temp_dir.path();
[email protected]aca3e9b2009-11-03 01:14:212431
[email protected]650b2d52013-02-10 03:41:452432 base::FilePath crx_path(output_directory.AppendASCII("ex1.crx"));
[email protected]a17f9462009-06-09 02:56:412433
2434 scoped_ptr<ExtensionCreator> creator(new ExtensionCreator());
2435 ASSERT_TRUE(creator->Run(input_directory, crx_path, privkey_path,
[email protected]650b2d52013-02-10 03:41:452436 base::FilePath(), ExtensionCreator::kOverwriteCRX));
[email protected]a17f9462009-06-09 02:56:412437
[email protected]8f512c72011-11-22 21:02:502438 InstallCRX(crx_path, INSTALL_NEW);
[email protected]a17f9462009-06-09 02:56:412439}
[email protected]a17f9462009-06-09 02:56:412440
[email protected]d9a61e12012-11-14 02:43:472441TEST_F(ExtensionServiceTest, InstallTheme) {
[email protected]eaa7dd182010-12-14 11:09:002442 InitializeEmptyExtensionService();
[email protected]b1ac70462013-08-14 21:33:302443 service_->Init();
[email protected]e2eb43112009-05-29 21:19:542444
2445 // A theme.
[email protected]650b2d52013-02-10 03:41:452446 base::FilePath path = data_dir_.AppendASCII("theme.crx");
[email protected]8f512c72011-11-22 21:02:502447 InstallCRX(path, INSTALL_NEW);
[email protected]25b34332009-06-05 21:53:192448 int pref_count = 0;
2449 ValidatePrefKeyCount(++pref_count);
[email protected]e2194742010-08-12 05:54:342450 ValidateIntegerPref(theme_crx, "state", Extension::ENABLED);
[email protected]1d5e58b2013-01-31 08:41:402451 ValidateIntegerPref(theme_crx, "location", Manifest::INTERNAL);
[email protected]e2eb43112009-05-29 21:19:542452
[email protected]6ef635e42009-07-26 06:16:122453 // A theme when extensions are disabled. Themes can be installed, even when
2454 // extensions are disabled.
[email protected]7577a5c52009-07-30 06:21:582455 set_extensions_enabled(false);
[email protected]e85e34c32011-04-13 18:38:352456 path = data_dir_.AppendASCII("theme2.crx");
[email protected]8f512c72011-11-22 21:02:502457 InstallCRX(path, INSTALL_NEW);
[email protected]25b34332009-06-05 21:53:192458 ValidatePrefKeyCount(++pref_count);
[email protected]e2194742010-08-12 05:54:342459 ValidateIntegerPref(theme2_crx, "state", Extension::ENABLED);
[email protected]1d5e58b2013-01-31 08:41:402460 ValidateIntegerPref(theme2_crx, "location", Manifest::INTERNAL);
[email protected]494c06e2009-07-25 01:06:422461
[email protected]f6dec1e322012-04-30 21:04:512462 // A theme with extension elements. Themes cannot have extension elements,
2463 // so any such elements (like content scripts) should be ignored.
[email protected]7577a5c52009-07-30 06:21:582464 set_extensions_enabled(true);
[email protected]f6dec1e322012-04-30 21:04:512465 {
2466 path = data_dir_.AppendASCII("theme_with_extension.crx");
2467 const Extension* extension = InstallCRX(path, INSTALL_NEW);
2468 ValidatePrefKeyCount(++pref_count);
2469 ASSERT_TRUE(extension);
2470 EXPECT_TRUE(extension->is_theme());
[email protected]334ec0a2013-03-24 01:36:162471 EXPECT_EQ(
2472 0u,
2473 extensions::ContentScriptsInfo::GetContentScripts(extension).size());
[email protected]f6dec1e322012-04-30 21:04:512474 }
[email protected]12198912009-06-05 03:41:222475
2476 // A theme with image resources missing (misspelt path).
[email protected]e85e34c32011-04-13 18:38:352477 path = data_dir_.AppendASCII("theme_missing_image.crx");
[email protected]8f512c72011-11-22 21:02:502478 InstallCRX(path, INSTALL_FAILED);
[email protected]25b34332009-06-05 21:53:192479 ValidatePrefKeyCount(pref_count);
[email protected]e2eb43112009-05-29 21:19:542480}
2481
[email protected]d9a61e12012-11-14 02:43:472482TEST_F(ExtensionServiceTest, LoadLocalizedTheme) {
[email protected]584245e52010-06-17 01:08:132483 // Load.
[email protected]eaa7dd182010-12-14 11:09:002484 InitializeEmptyExtensionService();
[email protected]b1ac70462013-08-14 21:33:302485 service_->Init();
2486
[email protected]650b2d52013-02-10 03:41:452487 base::FilePath extension_path = data_dir_
[email protected]584245e52010-06-17 01:08:132488 .AppendASCII("theme_i18n");
2489
[email protected]d8c8f25f2011-11-02 18:18:012490 extensions::UnpackedInstaller::Create(service_)->Load(extension_path);
[email protected]eedc1c752013-08-09 18:51:552491 base::RunLoop().RunUntilIdle();
[email protected]584245e52010-06-17 01:08:132492 EXPECT_EQ(0u, GetErrors().size());
2493 ASSERT_EQ(1u, loaded_.size());
2494 EXPECT_EQ(1u, service_->extensions()->size());
[email protected]cadac622013-06-11 16:46:362495 const Extension* theme = service_->extensions()->begin()->get();
[email protected]8f512c72011-11-22 21:02:502496 EXPECT_EQ("name", theme->name());
2497 EXPECT_EQ("description", theme->description());
[email protected]a17dfcf2012-12-30 02:07:092498
2499 // Cleanup the "Cached Theme.pak" file. Ideally, this would be installed in a
2500 // temporary directory, but it automatically installs to the extension's
2501 // directory, and we don't want to copy the whole extension for a unittest.
[email protected]650b2d52013-02-10 03:41:452502 base::FilePath theme_file = extension_path.Append(chrome::kThemePackFilename);
[email protected]7567484142013-07-11 17:36:072503 ASSERT_TRUE(base::PathExists(theme_file));
[email protected]dd3aa792013-07-16 19:10:232504 ASSERT_TRUE(base::DeleteFile(theme_file, false)); // Not recursive.
[email protected]584245e52010-06-17 01:08:132505}
2506
[email protected]c24fb292012-02-01 22:52:112507// Tests that we can change the ID of an unpacked extension by adding a key
2508// to its manifest.
[email protected]d9a61e12012-11-14 02:43:472509TEST_F(ExtensionServiceTest, UnpackedExtensionCanChangeID) {
[email protected]c24fb292012-02-01 22:52:112510 InitializeEmptyExtensionService();
2511
[email protected]ea1a3f62012-11-16 20:34:232512 base::ScopedTempDir temp;
[email protected]c24fb292012-02-01 22:52:112513 ASSERT_TRUE(temp.CreateUniqueTempDir());
2514
[email protected]650b2d52013-02-10 03:41:452515 base::FilePath extension_path = temp.path();
2516 base::FilePath manifest_path =
[email protected]993da5e2013-03-23 21:25:162517 extension_path.Append(extensions::kManifestFilename);
[email protected]650b2d52013-02-10 03:41:452518 base::FilePath manifest_no_key = data_dir_.
[email protected]c24fb292012-02-01 22:52:112519 AppendASCII("unpacked").
2520 AppendASCII("manifest_no_key.json");
2521
[email protected]650b2d52013-02-10 03:41:452522 base::FilePath manifest_with_key = data_dir_.
[email protected]c24fb292012-02-01 22:52:112523 AppendASCII("unpacked").
2524 AppendASCII("manifest_with_key.json");
2525
[email protected]7567484142013-07-11 17:36:072526 ASSERT_TRUE(base::PathExists(manifest_no_key));
2527 ASSERT_TRUE(base::PathExists(manifest_with_key));
[email protected]c24fb292012-02-01 22:52:112528
2529 // Load the unpacked extension with no key.
[email protected]f0ff2ad2013-07-09 17:42:262530 base::CopyFile(manifest_no_key, manifest_path);
[email protected]c24fb292012-02-01 22:52:112531 extensions::UnpackedInstaller::Create(service_)->Load(extension_path);
2532
[email protected]eedc1c752013-08-09 18:51:552533 base::RunLoop().RunUntilIdle();
[email protected]c24fb292012-02-01 22:52:112534 EXPECT_EQ(0u, GetErrors().size());
2535 ASSERT_EQ(1u, loaded_.size());
2536 EXPECT_EQ(1u, service_->extensions()->size());
2537
2538 // Add the key to the manifest.
[email protected]f0ff2ad2013-07-09 17:42:262539 base::CopyFile(manifest_with_key, manifest_path);
[email protected]c24fb292012-02-01 22:52:112540 loaded_.clear();
2541
2542 // Reload the extensions.
[email protected]bb1bc9b32013-12-21 03:09:142543 service_->ReloadExtensionsForTest();
[email protected]c24fb292012-02-01 22:52:112544 const Extension* extension = service_->GetExtensionById(unpacked, false);
2545 EXPECT_EQ(unpacked, extension->id());
2546 ASSERT_EQ(1u, loaded_.size());
2547
2548 // TODO(jstritar): Right now this just makes sure we don't crash and burn, but
2549 // we should also test that preferences are preserved.
2550}
2551
[email protected]91236521162012-05-24 15:02:512552#if defined(OS_POSIX)
[email protected]d9a61e12012-11-14 02:43:472553TEST_F(ExtensionServiceTest, UnpackedExtensionMayContainSymlinkedFiles) {
[email protected]650b2d52013-02-10 03:41:452554 base::FilePath source_data_dir = data_dir_.
[email protected]91236521162012-05-24 15:02:512555 AppendASCII("unpacked").
2556 AppendASCII("symlinks_allowed");
2557
2558 // Paths to test data files.
[email protected]650b2d52013-02-10 03:41:452559 base::FilePath source_manifest = source_data_dir.AppendASCII("manifest.json");
[email protected]7567484142013-07-11 17:36:072560 ASSERT_TRUE(base::PathExists(source_manifest));
[email protected]650b2d52013-02-10 03:41:452561 base::FilePath source_icon = source_data_dir.AppendASCII("icon.png");
[email protected]7567484142013-07-11 17:36:072562 ASSERT_TRUE(base::PathExists(source_icon));
[email protected]91236521162012-05-24 15:02:512563
2564 // Set up the temporary extension directory.
[email protected]ea1a3f62012-11-16 20:34:232565 base::ScopedTempDir temp;
[email protected]91236521162012-05-24 15:02:512566 ASSERT_TRUE(temp.CreateUniqueTempDir());
[email protected]650b2d52013-02-10 03:41:452567 base::FilePath extension_path = temp.path();
[email protected]993da5e2013-03-23 21:25:162568 base::FilePath manifest = extension_path.Append(
2569 extensions::kManifestFilename);
[email protected]650b2d52013-02-10 03:41:452570 base::FilePath icon_symlink = extension_path.AppendASCII("icon.png");
[email protected]f0ff2ad2013-07-09 17:42:262571 base::CopyFile(source_manifest, manifest);
[email protected]b264eab2013-11-27 23:22:082572 base::CreateSymbolicLink(source_icon, icon_symlink);
[email protected]91236521162012-05-24 15:02:512573
2574 // Load extension.
2575 InitializeEmptyExtensionService();
2576 extensions::UnpackedInstaller::Create(service_)->Load(extension_path);
[email protected]eedc1c752013-08-09 18:51:552577 base::RunLoop().RunUntilIdle();
[email protected]91236521162012-05-24 15:02:512578
2579 EXPECT_TRUE(GetErrors().empty());
2580 ASSERT_EQ(1u, loaded_.size());
2581 EXPECT_EQ(1u, service_->extensions()->size());
2582}
2583#endif
2584
[email protected]5ccca8ad2013-08-19 22:35:342585TEST_F(ExtensionServiceTest, UnpackedExtensionMayNotHaveUnderscore) {
2586 InitializeEmptyExtensionService();
2587 base::FilePath extension_path = data_dir_
2588 .AppendASCII("underscore_name");
2589 extensions::UnpackedInstaller::Create(service_)->Load(extension_path);
2590 base::RunLoop().RunUntilIdle();
2591 EXPECT_EQ(1u, GetErrors().size());
2592 EXPECT_EQ(0u, service_->extensions()->size());
2593}
2594
[email protected]d9a61e12012-11-14 02:43:472595TEST_F(ExtensionServiceTest, InstallLocalizedTheme) {
[email protected]eaa7dd182010-12-14 11:09:002596 InitializeEmptyExtensionService();
[email protected]b1ac70462013-08-14 21:33:302597 service_->Init();
2598
[email protected]650b2d52013-02-10 03:41:452599 base::FilePath theme_path = data_dir_
[email protected]584245e52010-06-17 01:08:132600 .AppendASCII("theme_i18n");
2601
[email protected]8f512c72011-11-22 21:02:502602 const Extension* theme = PackAndInstallCRX(theme_path, INSTALL_NEW);
[email protected]584245e52010-06-17 01:08:132603
[email protected]584245e52010-06-17 01:08:132604 EXPECT_EQ(0u, GetErrors().size());
2605 EXPECT_EQ(1u, service_->extensions()->size());
[email protected]8f512c72011-11-22 21:02:502606 EXPECT_EQ("name", theme->name());
2607 EXPECT_EQ("description", theme->description());
[email protected]584245e52010-06-17 01:08:132608}
2609
[email protected]d9a61e12012-11-14 02:43:472610TEST_F(ExtensionServiceTest, InstallApps) {
[email protected]eaa7dd182010-12-14 11:09:002611 InitializeEmptyExtensionService();
[email protected]6d2e60bd2010-06-03 22:37:392612
2613 // An empty app.
[email protected]8f512c72011-11-22 21:02:502614 const Extension* app = PackAndInstallCRX(data_dir_.AppendASCII("app1"),
2615 INSTALL_NEW);
[email protected]6d2e60bd2010-06-03 22:37:392616 int pref_count = 0;
2617 ValidatePrefKeyCount(++pref_count);
[email protected]3ba0fd32010-06-19 05:39:102618 ASSERT_EQ(1u, service_->extensions()->size());
[email protected]8f512c72011-11-22 21:02:502619 ValidateIntegerPref(app->id(), "state", Extension::ENABLED);
[email protected]1d5e58b2013-01-31 08:41:402620 ValidateIntegerPref(app->id(), "location", Manifest::INTERNAL);
[email protected]6d2e60bd2010-06-03 22:37:392621
2622 // Another app with non-overlapping extent. Should succeed.
[email protected]8f512c72011-11-22 21:02:502623 PackAndInstallCRX(data_dir_.AppendASCII("app2"), INSTALL_NEW);
[email protected]6d2e60bd2010-06-03 22:37:392624 ValidatePrefKeyCount(++pref_count);
2625
2626 // A third app whose extent overlaps the first. Should fail.
[email protected]8f512c72011-11-22 21:02:502627 PackAndInstallCRX(data_dir_.AppendASCII("app3"), INSTALL_FAILED);
[email protected]cd10e282010-10-26 21:04:512628 ValidatePrefKeyCount(pref_count);
[email protected]6d2e60bd2010-06-03 22:37:392629}
2630
[email protected]b6e64fd2011-08-09 19:49:192631// Tests that file access is OFF by default.
[email protected]d9a61e12012-11-14 02:43:472632TEST_F(ExtensionServiceTest, DefaultFileAccess) {
[email protected]b6e64fd2011-08-09 19:49:192633 InitializeEmptyExtensionService();
[email protected]8f512c72011-11-22 21:02:502634 const Extension* extension =
2635 PackAndInstallCRX(data_dir_
2636 .AppendASCII("permissions")
2637 .AppendASCII("files"),
2638 INSTALL_NEW);
[email protected]b6e64fd2011-08-09 19:49:192639 EXPECT_EQ(0u, GetErrors().size());
2640 EXPECT_EQ(1u, service_->extensions()->size());
[email protected]8f512c72011-11-22 21:02:502641 EXPECT_FALSE(service_->extension_prefs()->AllowFileAccess(extension->id()));
[email protected]b6e64fd2011-08-09 19:49:192642}
2643
[email protected]d9a61e12012-11-14 02:43:472644TEST_F(ExtensionServiceTest, UpdateApps) {
[email protected]15300d92011-01-19 18:44:302645 InitializeEmptyExtensionService();
[email protected]650b2d52013-02-10 03:41:452646 base::FilePath extensions_path = data_dir_.AppendASCII("app_update");
[email protected]15300d92011-01-19 18:44:302647
2648 // First install v1 of a hosted app.
[email protected]8f512c72011-11-22 21:02:502649 const Extension* extension =
2650 InstallCRX(extensions_path.AppendASCII("v1.crx"), INSTALL_NEW);
[email protected]15300d92011-01-19 18:44:302651 ASSERT_EQ(1u, service_->extensions()->size());
[email protected]8f512c72011-11-22 21:02:502652 std::string id = extension->id();
2653 ASSERT_EQ(std::string("1"), extension->version()->GetString());
[email protected]15300d92011-01-19 18:44:302654
2655 // Now try updating to v2.
2656 UpdateExtension(id,
2657 extensions_path.AppendASCII("v2.crx"),
2658 ENABLED);
2659 ASSERT_EQ(std::string("2"),
[email protected]8f512c72011-11-22 21:02:502660 service_->GetExtensionById(id, false)->version()->GetString());
[email protected]15300d92011-01-19 18:44:302661}
2662
[email protected]e6a6c2e02012-01-13 00:06:482663// Verifies that the NTP page and launch ordinals are kept when updating apps.
[email protected]d9a61e12012-11-14 02:43:472664TEST_F(ExtensionServiceTest, UpdateAppsRetainOrdinals) {
[email protected]e6a6c2e02012-01-13 00:06:482665 InitializeEmptyExtensionService();
[email protected]74474042013-11-21 12:03:542666 AppSorting* sorting = service_->extension_prefs()->app_sorting();
[email protected]650b2d52013-02-10 03:41:452667 base::FilePath extensions_path = data_dir_.AppendASCII("app_update");
[email protected]e6a6c2e02012-01-13 00:06:482668
2669 // First install v1 of a hosted app.
2670 const Extension* extension =
2671 InstallCRX(extensions_path.AppendASCII("v1.crx"), INSTALL_NEW);
2672 ASSERT_EQ(1u, service_->extensions()->size());
2673 std::string id = extension->id();
2674 ASSERT_EQ(std::string("1"), extension->version()->GetString());
2675
2676 // Modify the ordinals so we can distinguish them from the defaults.
[email protected]36b643212012-09-07 12:53:002677 syncer::StringOrdinal new_page_ordinal =
2678 sorting->GetPageOrdinal(id).CreateAfter();
2679 syncer::StringOrdinal new_launch_ordinal =
[email protected]e6a6c2e02012-01-13 00:06:482680 sorting->GetAppLaunchOrdinal(id).CreateBefore();
2681
2682 sorting->SetPageOrdinal(id, new_page_ordinal);
2683 sorting->SetAppLaunchOrdinal(id, new_launch_ordinal);
2684
2685 // Now try updating to v2.
2686 UpdateExtension(id, extensions_path.AppendASCII("v2.crx"), ENABLED);
2687 ASSERT_EQ(std::string("2"),
2688 service_->GetExtensionById(id, false)->version()->GetString());
2689
2690 // Verify that the ordinals match.
[email protected]36b643212012-09-07 12:53:002691 ASSERT_TRUE(new_page_ordinal.Equals(sorting->GetPageOrdinal(id)));
2692 ASSERT_TRUE(new_launch_ordinal.Equals(sorting->GetAppLaunchOrdinal(id)));
[email protected]e6a6c2e02012-01-13 00:06:482693}
2694
[email protected]b873cd92012-02-09 21:51:482695// Ensures that the CWS has properly initialized ordinals.
[email protected]d9a61e12012-11-14 02:43:472696TEST_F(ExtensionServiceTest, EnsureCWSOrdinalsInitialized) {
[email protected]b873cd92012-02-09 21:51:482697 InitializeEmptyExtensionService();
[email protected]650b2d52013-02-10 03:41:452698 service_->component_loader()->Add(
2699 IDR_WEBSTORE_MANIFEST, base::FilePath(FILE_PATH_LITERAL("web_store")));
[email protected]b873cd92012-02-09 21:51:482700 service_->Init();
2701
[email protected]74474042013-11-21 12:03:542702 AppSorting* sorting = service_->extension_prefs()->app_sorting();
[email protected]b873cd92012-02-09 21:51:482703 EXPECT_TRUE(
2704 sorting->GetPageOrdinal(extension_misc::kWebStoreAppId).IsValid());
2705 EXPECT_TRUE(
2706 sorting->GetAppLaunchOrdinal(extension_misc::kWebStoreAppId).IsValid());
2707}
2708
[email protected]fe10c202013-03-11 23:44:342709TEST_F(ExtensionServiceTest, InstallAppsWithUnlimitedStorage) {
[email protected]eaa7dd182010-12-14 11:09:002710 InitializeEmptyExtensionService();
[email protected]84df8332011-12-06 18:22:462711 EXPECT_TRUE(service_->extensions()->is_empty());
[email protected]c2c263c2010-08-13 21:59:482712
[email protected]c2c263c2010-08-13 21:59:482713 int pref_count = 0;
[email protected]c2c263c2010-08-13 21:59:482714
2715 // Install app1 with unlimited storage.
[email protected]8f512c72011-11-22 21:02:502716 const Extension* extension =
2717 PackAndInstallCRX(data_dir_.AppendASCII("app1"), INSTALL_NEW);
[email protected]c2c263c2010-08-13 21:59:482718 ValidatePrefKeyCount(++pref_count);
2719 ASSERT_EQ(1u, service_->extensions()->size());
[email protected]c2c263c2010-08-13 21:59:482720 const std::string id1 = extension->id();
[email protected]0d3e4a22011-06-23 19:02:522721 EXPECT_TRUE(extension->HasAPIPermission(
[email protected]c2e66e12012-06-27 06:27:062722 APIPermission::kUnlimitedStorage));
[email protected]cced75a2011-05-20 08:31:122723 EXPECT_TRUE(extension->web_extent().MatchesURL(
[email protected]6b414c232013-06-05 07:53:342724 extensions::AppLaunchInfo::GetFullLaunchURL(extension)));
2725 const GURL origin1(
2726 extensions::AppLaunchInfo::GetFullLaunchURL(extension).GetOrigin());
[email protected]19eb80152011-02-26 00:28:432727 EXPECT_TRUE(profile_->GetExtensionSpecialStoragePolicy()->
2728 IsStorageUnlimited(origin1));
[email protected]c2c263c2010-08-13 21:59:482729
2730 // Install app2 from the same origin with unlimited storage.
[email protected]8f512c72011-11-22 21:02:502731 extension = PackAndInstallCRX(data_dir_.AppendASCII("app2"), INSTALL_NEW);
[email protected]c2c263c2010-08-13 21:59:482732 ValidatePrefKeyCount(++pref_count);
2733 ASSERT_EQ(2u, service_->extensions()->size());
[email protected]c2c263c2010-08-13 21:59:482734 const std::string id2 = extension->id();
[email protected]0d3e4a22011-06-23 19:02:522735 EXPECT_TRUE(extension->HasAPIPermission(
[email protected]c2e66e12012-06-27 06:27:062736 APIPermission::kUnlimitedStorage));
[email protected]cced75a2011-05-20 08:31:122737 EXPECT_TRUE(extension->web_extent().MatchesURL(
[email protected]6b414c232013-06-05 07:53:342738 extensions::AppLaunchInfo::GetFullLaunchURL(extension)));
2739 const GURL origin2(
2740 extensions::AppLaunchInfo::GetFullLaunchURL(extension).GetOrigin());
[email protected]c2c263c2010-08-13 21:59:482741 EXPECT_EQ(origin1, origin2);
[email protected]19eb80152011-02-26 00:28:432742 EXPECT_TRUE(profile_->GetExtensionSpecialStoragePolicy()->
2743 IsStorageUnlimited(origin2));
2744
[email protected]c2c263c2010-08-13 21:59:482745
2746 // Uninstall one of them, unlimited storage should still be granted
2747 // to the origin.
[email protected]fa2416f2011-05-03 08:41:202748 UninstallExtension(id1, false);
[email protected]c2c263c2010-08-13 21:59:482749 EXPECT_EQ(1u, service_->extensions()->size());
[email protected]19eb80152011-02-26 00:28:432750 EXPECT_TRUE(profile_->GetExtensionSpecialStoragePolicy()->
2751 IsStorageUnlimited(origin1));
2752
[email protected]c2c263c2010-08-13 21:59:482753 // Uninstall the other, unlimited storage should be revoked.
[email protected]fa2416f2011-05-03 08:41:202754 UninstallExtension(id2, false);
[email protected]c2c263c2010-08-13 21:59:482755 EXPECT_EQ(0u, service_->extensions()->size());
[email protected]19eb80152011-02-26 00:28:432756 EXPECT_FALSE(profile_->GetExtensionSpecialStoragePolicy()->
2757 IsStorageUnlimited(origin2));
[email protected]c2c263c2010-08-13 21:59:482758}
2759
[email protected]d9a61e12012-11-14 02:43:472760TEST_F(ExtensionServiceTest, InstallAppsAndCheckStorageProtection) {
[email protected]eaa7dd182010-12-14 11:09:002761 InitializeEmptyExtensionService();
[email protected]84df8332011-12-06 18:22:462762 EXPECT_TRUE(service_->extensions()->is_empty());
[email protected]654512b2010-09-01 02:09:422763
[email protected]654512b2010-09-01 02:09:422764 int pref_count = 0;
2765
[email protected]8f512c72011-11-22 21:02:502766 const Extension* extension =
2767 PackAndInstallCRX(data_dir_.AppendASCII("app1"), INSTALL_NEW);
[email protected]654512b2010-09-01 02:09:422768 ValidatePrefKeyCount(++pref_count);
2769 ASSERT_EQ(1u, service_->extensions()->size());
[email protected]654512b2010-09-01 02:09:422770 EXPECT_TRUE(extension->is_app());
2771 const std::string id1 = extension->id();
[email protected]6b414c232013-06-05 07:53:342772 const GURL origin1(
2773 extensions::AppLaunchInfo::GetFullLaunchURL(extension).GetOrigin());
[email protected]19eb80152011-02-26 00:28:432774 EXPECT_TRUE(profile_->GetExtensionSpecialStoragePolicy()->
2775 IsStorageProtected(origin1));
[email protected]654512b2010-09-01 02:09:422776
2777 // App 4 has a different origin (maps.google.com).
[email protected]8f512c72011-11-22 21:02:502778 extension = PackAndInstallCRX(data_dir_.AppendASCII("app4"), INSTALL_NEW);
[email protected]654512b2010-09-01 02:09:422779 ValidatePrefKeyCount(++pref_count);
2780 ASSERT_EQ(2u, service_->extensions()->size());
[email protected]654512b2010-09-01 02:09:422781 const std::string id2 = extension->id();
[email protected]6b414c232013-06-05 07:53:342782 const GURL origin2(
2783 extensions::AppLaunchInfo::GetFullLaunchURL(extension).GetOrigin());
[email protected]654512b2010-09-01 02:09:422784 ASSERT_NE(origin1, origin2);
[email protected]19eb80152011-02-26 00:28:432785 EXPECT_TRUE(profile_->GetExtensionSpecialStoragePolicy()->
2786 IsStorageProtected(origin2));
[email protected]654512b2010-09-01 02:09:422787
[email protected]fa2416f2011-05-03 08:41:202788 UninstallExtension(id1, false);
[email protected]654512b2010-09-01 02:09:422789 EXPECT_EQ(1u, service_->extensions()->size());
[email protected]654512b2010-09-01 02:09:422790
[email protected]fa2416f2011-05-03 08:41:202791 UninstallExtension(id2, false);
[email protected]654512b2010-09-01 02:09:422792
[email protected]84df8332011-12-06 18:22:462793 EXPECT_TRUE(service_->extensions()->is_empty());
[email protected]19eb80152011-02-26 00:28:432794 EXPECT_FALSE(profile_->GetExtensionSpecialStoragePolicy()->
2795 IsStorageProtected(origin1));
2796 EXPECT_FALSE(profile_->GetExtensionSpecialStoragePolicy()->
2797 IsStorageProtected(origin2));
[email protected]654512b2010-09-01 02:09:422798}
2799
[email protected]894bb502009-05-21 22:39:572800// Test that when an extension version is reinstalled, nothing happens.
[email protected]d9a61e12012-11-14 02:43:472801TEST_F(ExtensionServiceTest, Reinstall) {
[email protected]eaa7dd182010-12-14 11:09:002802 InitializeEmptyExtensionService();
[email protected]894bb502009-05-21 22:39:572803
2804 // A simple extension that should install without error.
[email protected]650b2d52013-02-10 03:41:452805 base::FilePath path = data_dir_.AppendASCII("good.crx");
[email protected]8f512c72011-11-22 21:02:502806 InstallCRX(path, INSTALL_NEW);
[email protected]894bb502009-05-21 22:39:572807
[email protected]25b34332009-06-05 21:53:192808 ValidatePrefKeyCount(1);
[email protected]e2194742010-08-12 05:54:342809 ValidateIntegerPref(good_crx, "state", Extension::ENABLED);
[email protected]1d5e58b2013-01-31 08:41:402810 ValidateIntegerPref(good_crx, "location", Manifest::INTERNAL);
[email protected]894bb502009-05-21 22:39:572811
[email protected]ca3dbf52010-05-19 22:27:062812 // Reinstall the same version, it should overwrite the previous one.
[email protected]8f512c72011-11-22 21:02:502813 InstallCRX(path, INSTALL_UPDATED);
[email protected]894bb502009-05-21 22:39:572814
[email protected]25b34332009-06-05 21:53:192815 ValidatePrefKeyCount(1);
[email protected]e2194742010-08-12 05:54:342816 ValidateIntegerPref(good_crx, "state", Extension::ENABLED);
[email protected]1d5e58b2013-01-31 08:41:402817 ValidateIntegerPref(good_crx, "location", Manifest::INTERNAL);
[email protected]894bb502009-05-21 22:39:572818}
2819
[email protected]620db1762011-07-15 21:57:342820// Test that we can determine if extensions came from the
2821// Chrome web store.
[email protected]d9a61e12012-11-14 02:43:472822TEST_F(ExtensionServiceTest, FromWebStore) {
[email protected]8266d662011-07-12 21:53:262823 InitializeEmptyExtensionService();
2824
2825 // A simple extension that should install without error.
[email protected]650b2d52013-02-10 03:41:452826 base::FilePath path = data_dir_.AppendASCII("good.crx");
[email protected]8f512c72011-11-22 21:02:502827 // Not from web store.
2828 const Extension* extension = InstallCRX(path, INSTALL_NEW);
2829 std::string id = extension->id();
[email protected]8266d662011-07-12 21:53:262830
[email protected]8266d662011-07-12 21:53:262831 ValidatePrefKeyCount(1);
[email protected]3f2a2fa2013-09-24 02:55:252832 ASSERT_TRUE(ValidateBooleanPref(good_crx, "from_webstore", false));
[email protected]620db1762011-07-15 21:57:342833 ASSERT_FALSE(extension->from_webstore());
[email protected]8266d662011-07-12 21:53:262834
[email protected]8266d662011-07-12 21:53:262835 // Test install from web store.
[email protected]8f512c72011-11-22 21:02:502836 InstallCRXFromWebStore(path, INSTALL_UPDATED); // From web store.
[email protected]8266d662011-07-12 21:53:262837
[email protected]8266d662011-07-12 21:53:262838 ValidatePrefKeyCount(1);
[email protected]3f2a2fa2013-09-24 02:55:252839 ASSERT_TRUE(ValidateBooleanPref(good_crx, "from_webstore", true));
[email protected]620db1762011-07-15 21:57:342840
2841 // Reload so extension gets reinitialized with new value.
[email protected]bb1bc9b32013-12-21 03:09:142842 service_->ReloadExtensionsForTest();
[email protected]8f512c72011-11-22 21:02:502843 extension = service_->GetExtensionById(id, false);
[email protected]620db1762011-07-15 21:57:342844 ASSERT_TRUE(extension->from_webstore());
[email protected]3d729722011-09-20 02:57:092845
2846 // Upgrade to version 2.0
2847 path = data_dir_.AppendASCII("good2.crx");
2848 UpdateExtension(good_crx, path, ENABLED);
2849 ValidatePrefKeyCount(1);
[email protected]3f2a2fa2013-09-24 02:55:252850 ASSERT_TRUE(ValidateBooleanPref(good_crx, "from_webstore", true));
[email protected]8266d662011-07-12 21:53:262851}
2852
[email protected]fbcc40302009-06-12 20:45:452853// Test upgrading a signed extension.
[email protected]d9a61e12012-11-14 02:43:472854TEST_F(ExtensionServiceTest, UpgradeSignedGood) {
[email protected]eaa7dd182010-12-14 11:09:002855 InitializeEmptyExtensionService();
[email protected]fbcc40302009-06-12 20:45:452856
[email protected]650b2d52013-02-10 03:41:452857 base::FilePath path = data_dir_.AppendASCII("good.crx");
[email protected]8f512c72011-11-22 21:02:502858 const Extension* extension = InstallCRX(path, INSTALL_NEW);
2859 std::string id = extension->id();
[email protected]fbcc40302009-06-12 20:45:452860
[email protected]8f512c72011-11-22 21:02:502861 ASSERT_EQ("1.0.0.0", extension->version()->GetString());
[email protected]fbcc40302009-06-12 20:45:452862 ASSERT_EQ(0u, GetErrors().size());
2863
[email protected]e7554c3f2013-05-29 00:36:562864 // Upgrade to version 1.0.0.1.
2865 // Also test that the extension's old and new title are correctly retrieved.
[email protected]e85e34c32011-04-13 18:38:352866 path = data_dir_.AppendASCII("good2.crx");
[email protected]e7554c3f2013-05-29 00:36:562867 InstallCRX(path, INSTALL_UPDATED, Extension::NO_FLAGS, "My extension 1");
[email protected]8f512c72011-11-22 21:02:502868 extension = service_->GetExtensionById(id, false);
[email protected]fbcc40302009-06-12 20:45:452869
[email protected]8f512c72011-11-22 21:02:502870 ASSERT_EQ("1.0.0.1", extension->version()->GetString());
[email protected]e7554c3f2013-05-29 00:36:562871 ASSERT_EQ("My updated extension 1", extension->name());
[email protected]fbcc40302009-06-12 20:45:452872 ASSERT_EQ(0u, GetErrors().size());
2873}
2874
2875// Test upgrading a signed extension with a bad signature.
[email protected]d9a61e12012-11-14 02:43:472876TEST_F(ExtensionServiceTest, UpgradeSignedBad) {
[email protected]eaa7dd182010-12-14 11:09:002877 InitializeEmptyExtensionService();
[email protected]fbcc40302009-06-12 20:45:452878
[email protected]650b2d52013-02-10 03:41:452879 base::FilePath path = data_dir_.AppendASCII("good.crx");
[email protected]8f512c72011-11-22 21:02:502880 InstallCRX(path, INSTALL_NEW);
[email protected]fbcc40302009-06-12 20:45:452881
2882 // Try upgrading with a bad signature. This should fail during the unpack,
2883 // because the key will not match the signature.
[email protected]e85e34c32011-04-13 18:38:352884 path = data_dir_.AppendASCII("bad_signature.crx");
[email protected]8f512c72011-11-22 21:02:502885 InstallCRX(path, INSTALL_FAILED);
[email protected]fbcc40302009-06-12 20:45:452886}
2887
[email protected]e957fe52009-06-23 16:51:052888// Test a normal update via the UpdateExtension API
[email protected]d9a61e12012-11-14 02:43:472889TEST_F(ExtensionServiceTest, UpdateExtension) {
[email protected]eaa7dd182010-12-14 11:09:002890 InitializeEmptyExtensionService();
[email protected]e957fe52009-06-23 16:51:052891
[email protected]650b2d52013-02-10 03:41:452892 base::FilePath path = data_dir_.AppendASCII("good.crx");
[email protected]e957fe52009-06-23 16:51:052893
[email protected]8f512c72011-11-22 21:02:502894 const Extension* good = InstallCRX(path, INSTALL_NEW);
[email protected]e957fe52009-06-23 16:51:052895 ASSERT_EQ("1.0.0.0", good->VersionString());
2896 ASSERT_EQ(good_crx, good->id());
2897
[email protected]e85e34c32011-04-13 18:38:352898 path = data_dir_.AppendASCII("good2.crx");
[email protected]4416c5a2010-06-26 01:28:572899 UpdateExtension(good_crx, path, ENABLED);
[email protected]8f512c72011-11-22 21:02:502900 ASSERT_EQ("1.0.0.1",
2901 service_->GetExtensionById(good_crx, false)->
2902 version()->GetString());
[email protected]e957fe52009-06-23 16:51:052903}
2904
[email protected]3c4abc82012-10-22 22:25:542905// Extensions should not be updated during browser shutdown.
[email protected]d9a61e12012-11-14 02:43:472906TEST_F(ExtensionServiceTest, UpdateExtensionDuringShutdown) {
[email protected]3c4abc82012-10-22 22:25:542907 InitializeEmptyExtensionService();
2908
2909 // Install an extension.
[email protected]650b2d52013-02-10 03:41:452910 base::FilePath path = data_dir_.AppendASCII("good.crx");
[email protected]3c4abc82012-10-22 22:25:542911 const Extension* good = InstallCRX(path, INSTALL_NEW);
2912 ASSERT_EQ(good_crx, good->id());
2913
2914 // Simulate shutdown.
2915 service_->set_browser_terminating_for_test(true);
2916
2917 // Update should fail and extension should not be updated.
2918 path = data_dir_.AppendASCII("good2.crx");
[email protected]044e86992014-01-24 22:59:112919 bool updated = service_->UpdateExtension(good_crx, path, true, GURL(), NULL);
[email protected]3c4abc82012-10-22 22:25:542920 ASSERT_FALSE(updated);
2921 ASSERT_EQ("1.0.0.0",
2922 service_->GetExtensionById(good_crx, false)->
2923 version()->GetString());
2924}
2925
[email protected]e957fe52009-06-23 16:51:052926// Test updating a not-already-installed extension - this should fail
[email protected]d9a61e12012-11-14 02:43:472927TEST_F(ExtensionServiceTest, UpdateNotInstalledExtension) {
[email protected]eaa7dd182010-12-14 11:09:002928 InitializeEmptyExtensionService();
[email protected]e957fe52009-06-23 16:51:052929
[email protected]650b2d52013-02-10 03:41:452930 base::FilePath path = data_dir_.AppendASCII("good.crx");
[email protected]4416c5a2010-06-26 01:28:572931 UpdateExtension(good_crx, path, UPDATED);
[email protected]eedc1c752013-08-09 18:51:552932 base::RunLoop().RunUntilIdle();
[email protected]e957fe52009-06-23 16:51:052933
2934 ASSERT_EQ(0u, service_->extensions()->size());
2935 ASSERT_FALSE(installed_);
2936 ASSERT_EQ(0u, loaded_.size());
2937}
2938
2939// Makes sure you can't downgrade an extension via UpdateExtension
[email protected]d9a61e12012-11-14 02:43:472940TEST_F(ExtensionServiceTest, UpdateWillNotDowngrade) {
[email protected]eaa7dd182010-12-14 11:09:002941 InitializeEmptyExtensionService();
[email protected]e957fe52009-06-23 16:51:052942
[email protected]650b2d52013-02-10 03:41:452943 base::FilePath path = data_dir_.AppendASCII("good2.crx");
[email protected]e957fe52009-06-23 16:51:052944
[email protected]8f512c72011-11-22 21:02:502945 const Extension* good = InstallCRX(path, INSTALL_NEW);
[email protected]e957fe52009-06-23 16:51:052946 ASSERT_EQ("1.0.0.1", good->VersionString());
2947 ASSERT_EQ(good_crx, good->id());
2948
2949 // Change path from good2.crx -> good.crx
[email protected]e85e34c32011-04-13 18:38:352950 path = data_dir_.AppendASCII("good.crx");
[email protected]4416c5a2010-06-26 01:28:572951 UpdateExtension(good_crx, path, FAILED);
[email protected]8f512c72011-11-22 21:02:502952 ASSERT_EQ("1.0.0.1",
2953 service_->GetExtensionById(good_crx, false)->
2954 version()->GetString());
[email protected]e957fe52009-06-23 16:51:052955}
2956
2957// Make sure calling update with an identical version does nothing
[email protected]d9a61e12012-11-14 02:43:472958TEST_F(ExtensionServiceTest, UpdateToSameVersionIsNoop) {
[email protected]eaa7dd182010-12-14 11:09:002959 InitializeEmptyExtensionService();
[email protected]e957fe52009-06-23 16:51:052960
[email protected]650b2d52013-02-10 03:41:452961 base::FilePath path = data_dir_.AppendASCII("good.crx");
[email protected]e957fe52009-06-23 16:51:052962
[email protected]8f512c72011-11-22 21:02:502963 const Extension* good = InstallCRX(path, INSTALL_NEW);
[email protected]e957fe52009-06-23 16:51:052964 ASSERT_EQ(good_crx, good->id());
[email protected]4416c5a2010-06-26 01:28:572965 UpdateExtension(good_crx, path, FAILED_SILENTLY);
[email protected]aa142702010-03-26 01:26:332966}
2967
[email protected]dbec3792010-08-10 00:08:452968// Tests that updating an extension does not clobber old state.
[email protected]d9a61e12012-11-14 02:43:472969TEST_F(ExtensionServiceTest, UpdateExtensionPreservesState) {
[email protected]eaa7dd182010-12-14 11:09:002970 InitializeEmptyExtensionService();
[email protected]dbec3792010-08-10 00:08:452971
[email protected]650b2d52013-02-10 03:41:452972 base::FilePath path = data_dir_.AppendASCII("good.crx");
[email protected]dbec3792010-08-10 00:08:452973
[email protected]8f512c72011-11-22 21:02:502974 const Extension* good = InstallCRX(path, INSTALL_NEW);
[email protected]dbec3792010-08-10 00:08:452975 ASSERT_EQ("1.0.0.0", good->VersionString());
2976 ASSERT_EQ(good_crx, good->id());
2977
2978 // Disable it and allow it to run in incognito. These settings should carry
2979 // over to the updated version.
[email protected]44d62b62012-04-11 00:06:032980 service_->DisableExtension(good->id(), Extension::DISABLE_USER_ACTION);
[email protected]1d5cf4142014-01-24 18:25:222981 extensions::util::SetIsIncognitoEnabled(good->id(), profile_.get(), true);
[email protected]c38fd352012-03-28 04:06:122982 service_->extension_prefs()->SetDidExtensionEscalatePermissions(good, true);
[email protected]dbec3792010-08-10 00:08:452983
[email protected]e85e34c32011-04-13 18:38:352984 path = data_dir_.AppendASCII("good2.crx");
[email protected]dbec3792010-08-10 00:08:452985 UpdateExtension(good_crx, path, INSTALLED);
[email protected]98270432012-09-11 20:51:242986 ASSERT_EQ(1u, service_->disabled_extensions()->size());\
[email protected]8f512c72011-11-22 21:02:502987 const Extension* good2 = service_->GetExtensionById(good_crx, true);
[email protected]dbec3792010-08-10 00:08:452988 ASSERT_EQ("1.0.0.1", good2->version()->GetString());
[email protected]1d5cf4142014-01-24 18:25:222989 EXPECT_TRUE(extensions::util::IsIncognitoEnabled(
2990 good2->id(), profile_.get()));
[email protected]c38fd352012-03-28 04:06:122991 EXPECT_TRUE(service_->extension_prefs()->DidExtensionEscalatePermissions(
2992 good2->id()));
[email protected]dbec3792010-08-10 00:08:452993}
2994
[email protected]5eb375e92010-11-26 07:50:412995// Tests that updating preserves extension location.
[email protected]d9a61e12012-11-14 02:43:472996TEST_F(ExtensionServiceTest, UpdateExtensionPreservesLocation) {
[email protected]eaa7dd182010-12-14 11:09:002997 InitializeEmptyExtensionService();
[email protected]5eb375e92010-11-26 07:50:412998
[email protected]650b2d52013-02-10 03:41:452999 base::FilePath path = data_dir_.AppendASCII("good.crx");
[email protected]5eb375e92010-11-26 07:50:413000
[email protected]d41e2152012-02-24 04:20:273001 const Extension* good =
[email protected]1d5e58b2013-01-31 08:41:403002 InstallCRXWithLocation(path, Manifest::EXTERNAL_PREF, INSTALL_NEW);
[email protected]5eb375e92010-11-26 07:50:413003
3004 ASSERT_EQ("1.0.0.0", good->VersionString());
3005 ASSERT_EQ(good_crx, good->id());
3006
[email protected]e85e34c32011-04-13 18:38:353007 path = data_dir_.AppendASCII("good2.crx");
[email protected]5eb375e92010-11-26 07:50:413008 UpdateExtension(good_crx, path, ENABLED);
[email protected]8f512c72011-11-22 21:02:503009 const Extension* good2 = service_->GetExtensionById(good_crx, false);
[email protected]5eb375e92010-11-26 07:50:413010 ASSERT_EQ("1.0.0.1", good2->version()->GetString());
[email protected]1d5e58b2013-01-31 08:41:403011 EXPECT_EQ(good2->location(), Manifest::EXTERNAL_PREF);
[email protected]5eb375e92010-11-26 07:50:413012}
3013
[email protected]66e26872010-12-03 20:07:253014// Makes sure that LOAD extension types can downgrade.
[email protected]d9a61e12012-11-14 02:43:473015TEST_F(ExtensionServiceTest, LoadExtensionsCanDowngrade) {
[email protected]eaa7dd182010-12-14 11:09:003016 InitializeEmptyExtensionService();
[email protected]66e26872010-12-03 20:07:253017
[email protected]ea1a3f62012-11-16 20:34:233018 base::ScopedTempDir temp;
[email protected]66e26872010-12-03 20:07:253019 ASSERT_TRUE(temp.CreateUniqueTempDir());
3020
3021 // We'll write the extension manifest dynamically to a temporary path
3022 // to make it easier to change the version number.
[email protected]650b2d52013-02-10 03:41:453023 base::FilePath extension_path = temp.path();
3024 base::FilePath manifest_path =
[email protected]993da5e2013-03-23 21:25:163025 extension_path.Append(extensions::kManifestFilename);
[email protected]7567484142013-07-11 17:36:073026 ASSERT_FALSE(base::PathExists(manifest_path));
[email protected]66e26872010-12-03 20:07:253027
3028 // Start with version 2.0.
[email protected]023b3d12013-12-23 18:46:493029 base::DictionaryValue manifest;
[email protected]66e26872010-12-03 20:07:253030 manifest.SetString("version", "2.0");
3031 manifest.SetString("name", "LOAD Downgrade Test");
[email protected]b3d52852011-12-07 01:01:113032 manifest.SetInteger("manifest_version", 2);
[email protected]66e26872010-12-03 20:07:253033
3034 JSONFileValueSerializer serializer(manifest_path);
3035 ASSERT_TRUE(serializer.Serialize(manifest));
3036
[email protected]d8c8f25f2011-11-02 18:18:013037 extensions::UnpackedInstaller::Create(service_)->Load(extension_path);
[email protected]eedc1c752013-08-09 18:51:553038 base::RunLoop().RunUntilIdle();
[email protected]66e26872010-12-03 20:07:253039
3040 EXPECT_EQ(0u, GetErrors().size());
3041 ASSERT_EQ(1u, loaded_.size());
[email protected]12075d12013-02-27 05:38:053042 EXPECT_EQ(Manifest::UNPACKED, loaded_[0]->location());
[email protected]66e26872010-12-03 20:07:253043 EXPECT_EQ(1u, service_->extensions()->size());
3044 EXPECT_EQ("2.0", loaded_[0]->VersionString());
3045
3046 // Now set the version number to 1.0, reload the extensions and verify that
3047 // the downgrade was accepted.
3048 manifest.SetString("version", "1.0");
3049 ASSERT_TRUE(serializer.Serialize(manifest));
3050
[email protected]d8c8f25f2011-11-02 18:18:013051 extensions::UnpackedInstaller::Create(service_)->Load(extension_path);
[email protected]eedc1c752013-08-09 18:51:553052 base::RunLoop().RunUntilIdle();
[email protected]66e26872010-12-03 20:07:253053
3054 EXPECT_EQ(0u, GetErrors().size());
3055 ASSERT_EQ(1u, loaded_.size());
[email protected]12075d12013-02-27 05:38:053056 EXPECT_EQ(Manifest::UNPACKED, loaded_[0]->location());
[email protected]66e26872010-12-03 20:07:253057 EXPECT_EQ(1u, service_->extensions()->size());
3058 EXPECT_EQ("1.0", loaded_[0]->VersionString());
3059}
3060
[email protected]0d904312012-01-25 23:00:163061#if !defined(OS_CHROMEOS)
3062// LOAD extensions with plugins require approval.
[email protected]d9a61e12012-11-14 02:43:473063TEST_F(ExtensionServiceTest, LoadExtensionsWithPlugins) {
[email protected]3f2a2fa2013-09-24 02:55:253064 base::FilePath extension_with_plugin_path = good1_path();
3065 base::FilePath extension_no_plugin_path = good2_path();
[email protected]0d904312012-01-25 23:00:163066
[email protected]ebd71962012-12-20 02:56:553067 InitPluginService();
[email protected]0d904312012-01-25 23:00:163068 InitializeEmptyExtensionService();
[email protected]98b6d942013-11-10 00:34:073069 InitializeProcessManager();
[email protected]0d904312012-01-25 23:00:163070 service_->set_show_extensions_prompts(true);
3071
3072 // Start by canceling any install prompts.
3073 CommandLine::ForCurrentProcess()->AppendSwitchASCII(
3074 switches::kAppsGalleryInstallAutoConfirmForTests,
3075 "cancel");
3076
3077 // The extension that has a plugin should not install.
3078 extensions::UnpackedInstaller::Create(service_)->Load(
3079 extension_with_plugin_path);
[email protected]eedc1c752013-08-09 18:51:553080 base::RunLoop().RunUntilIdle();
[email protected]0d904312012-01-25 23:00:163081 EXPECT_EQ(0u, GetErrors().size());
3082 EXPECT_EQ(0u, loaded_.size());
3083 EXPECT_EQ(0u, service_->extensions()->size());
3084 EXPECT_EQ(0u, service_->disabled_extensions()->size());
3085
3086 // But the extension with no plugin should since there's no prompt.
[email protected]252873a2013-07-19 01:39:483087 ExtensionErrorReporter::GetInstance()->ClearErrors();
[email protected]0d904312012-01-25 23:00:163088 extensions::UnpackedInstaller::Create(service_)->Load(
3089 extension_no_plugin_path);
[email protected]eedc1c752013-08-09 18:51:553090 base::RunLoop().RunUntilIdle();
[email protected]0d904312012-01-25 23:00:163091 EXPECT_EQ(0u, GetErrors().size());
3092 EXPECT_EQ(1u, loaded_.size());
3093 EXPECT_EQ(1u, service_->extensions()->size());
3094 EXPECT_EQ(0u, service_->disabled_extensions()->size());
3095 EXPECT_TRUE(service_->extensions()->Contains(good2));
3096
3097 // The plugin extension should install if we accept the dialog.
3098 CommandLine::ForCurrentProcess()->AppendSwitchASCII(
3099 switches::kAppsGalleryInstallAutoConfirmForTests,
3100 "accept");
3101
[email protected]252873a2013-07-19 01:39:483102 ExtensionErrorReporter::GetInstance()->ClearErrors();
[email protected]0d904312012-01-25 23:00:163103 extensions::UnpackedInstaller::Create(service_)->Load(
3104 extension_with_plugin_path);
[email protected]eedc1c752013-08-09 18:51:553105 base::RunLoop().RunUntilIdle();
[email protected]0d904312012-01-25 23:00:163106 EXPECT_EQ(0u, GetErrors().size());
3107 EXPECT_EQ(2u, loaded_.size());
3108 EXPECT_EQ(2u, service_->extensions()->size());
3109 EXPECT_EQ(0u, service_->disabled_extensions()->size());
3110 EXPECT_TRUE(service_->extensions()->Contains(good1));
3111 EXPECT_TRUE(service_->extensions()->Contains(good2));
3112
3113 // Make sure the granted permissions have been setup.
[email protected]c2e66e12012-06-27 06:27:063114 scoped_refptr<PermissionSet> permissions(
[email protected]0d904312012-01-25 23:00:163115 service_->extension_prefs()->GetGrantedPermissions(good1));
3116 EXPECT_FALSE(permissions->IsEmpty());
3117 EXPECT_TRUE(permissions->HasEffectiveFullAccess());
3118 EXPECT_FALSE(permissions->apis().empty());
[email protected]c2e66e12012-06-27 06:27:063119 EXPECT_TRUE(permissions->HasAPIPermission(APIPermission::kPlugin));
[email protected]0d904312012-01-25 23:00:163120
3121 // We should be able to reload the extension without getting another prompt.
3122 loaded_.clear();
3123 CommandLine::ForCurrentProcess()->AppendSwitchASCII(
3124 switches::kAppsGalleryInstallAutoConfirmForTests,
3125 "cancel");
3126
3127 service_->ReloadExtension(good1);
[email protected]eedc1c752013-08-09 18:51:553128 base::RunLoop().RunUntilIdle();
[email protected]0d904312012-01-25 23:00:163129 EXPECT_EQ(1u, loaded_.size());
3130 EXPECT_EQ(2u, service_->extensions()->size());
3131 EXPECT_EQ(0u, service_->disabled_extensions()->size());
3132}
[email protected]64bbd6c2013-11-12 05:05:413133#endif // !defined(OS_CHROMEOS)
[email protected]0d904312012-01-25 23:00:163134
[email protected]7fa19f82010-12-21 19:40:083135namespace {
3136
[email protected]8f3bcbd2013-06-05 08:42:403137bool IsExtension(const Extension* extension) {
3138 return extension->GetType() == Manifest::TYPE_EXTENSION;
[email protected]7fa19f82010-12-21 19:40:083139}
3140
3141} // namespace
3142
[email protected]aa142702010-03-26 01:26:333143// Test adding a pending extension.
[email protected]d9a61e12012-11-14 02:43:473144TEST_F(ExtensionServiceTest, AddPendingExtensionFromSync) {
[email protected]eaa7dd182010-12-14 11:09:003145 InitializeEmptyExtensionService();
[email protected]aa142702010-03-26 01:26:333146
[email protected]145a317b2011-04-12 16:03:463147 const std::string kFakeId(all_zero);
[email protected]aa142702010-03-26 01:26:333148 const GURL kFakeUpdateURL("http:://fake.update/url");
[email protected]aa142702010-03-26 01:26:333149 const bool kFakeInstallSilently(true);
3150
[email protected]f4d5e1a2011-04-28 02:08:253151 EXPECT_TRUE(service_->pending_extension_manager()->AddFromSync(
[email protected]7fa19f82010-12-21 19:40:083152 kFakeId, kFakeUpdateURL, &IsExtension,
[email protected]6cc7dbae2011-04-29 21:18:333153 kFakeInstallSilently));
[email protected]b2907fd2011-03-25 16:43:373154
[email protected]3f213ad2012-07-26 23:39:413155 const extensions::PendingExtensionInfo* pending_extension_info;
[email protected]51a3bf8b2012-06-08 22:53:063156 ASSERT_TRUE((pending_extension_info = service_->pending_extension_manager()->
3157 GetById(kFakeId)));
3158 EXPECT_EQ(kFakeUpdateURL, pending_extension_info->update_url());
3159 EXPECT_EQ(&IsExtension, pending_extension_info->should_allow_install_);
3160 EXPECT_EQ(kFakeInstallSilently, pending_extension_info->install_silently());
[email protected]aa142702010-03-26 01:26:333161}
3162
[email protected]aa142702010-03-26 01:26:333163namespace {
3164const char kGoodId[] = "ldnnhddmnhbkjipkidpdiheffobcpfmf";
3165const char kGoodUpdateURL[] = "http://good.update/url";
[email protected]8ef78fd2010-08-19 17:14:323166const bool kGoodIsFromSync = true;
[email protected]aa142702010-03-26 01:26:333167const bool kGoodInstallSilently = true;
[email protected]aa142702010-03-26 01:26:333168} // namespace
3169
3170// Test updating a pending extension.
[email protected]d9a61e12012-11-14 02:43:473171TEST_F(ExtensionServiceTest, UpdatePendingExtension) {
[email protected]eaa7dd182010-12-14 11:09:003172 InitializeEmptyExtensionService();
[email protected]f4d5e1a2011-04-28 02:08:253173 EXPECT_TRUE(service_->pending_extension_manager()->AddFromSync(
[email protected]7fa19f82010-12-21 19:40:083174 kGoodId, GURL(kGoodUpdateURL), &IsExtension,
[email protected]6cc7dbae2011-04-29 21:18:333175 kGoodInstallSilently));
[email protected]b2907fd2011-03-25 16:43:373176 EXPECT_TRUE(service_->pending_extension_manager()->IsIdPending(kGoodId));
[email protected]aa142702010-03-26 01:26:333177
[email protected]650b2d52013-02-10 03:41:453178 base::FilePath path = data_dir_.AppendASCII("good.crx");
[email protected]6cc7dbae2011-04-29 21:18:333179 UpdateExtension(kGoodId, path, ENABLED);
[email protected]aa142702010-03-26 01:26:333180
[email protected]b2907fd2011-03-25 16:43:373181 EXPECT_FALSE(service_->pending_extension_manager()->IsIdPending(kGoodId));
[email protected]4416c5a2010-06-26 01:28:573182
[email protected]9adb9692010-10-29 23:14:023183 const Extension* extension = service_->GetExtensionById(kGoodId, true);
[email protected]4416c5a2010-06-26 01:28:573184 ASSERT_TRUE(extension);
[email protected]aa142702010-03-26 01:26:333185}
3186
[email protected]7fa19f82010-12-21 19:40:083187namespace {
3188
[email protected]8f3bcbd2013-06-05 08:42:403189bool IsTheme(const Extension* extension) {
3190 return extension->is_theme();
[email protected]7fa19f82010-12-21 19:40:083191}
3192
3193} // namespace
3194
[email protected]11edd1e2010-07-21 00:14:503195// Test updating a pending theme.
[email protected]7f2c5552011-12-20 22:53:163196// Disabled due to ASAN failure. http://crbug.com/108320
[email protected]d9a61e12012-11-14 02:43:473197TEST_F(ExtensionServiceTest, DISABLED_UpdatePendingTheme) {
[email protected]eaa7dd182010-12-14 11:09:003198 InitializeEmptyExtensionService();
[email protected]f4d5e1a2011-04-28 02:08:253199 EXPECT_TRUE(service_->pending_extension_manager()->AddFromSync(
[email protected]6cc7dbae2011-04-29 21:18:333200 theme_crx, GURL(), &IsTheme, false));
[email protected]b2907fd2011-03-25 16:43:373201 EXPECT_TRUE(service_->pending_extension_manager()->IsIdPending(theme_crx));
[email protected]11edd1e2010-07-21 00:14:503202
[email protected]650b2d52013-02-10 03:41:453203 base::FilePath path = data_dir_.AppendASCII("theme.crx");
[email protected]11edd1e2010-07-21 00:14:503204 UpdateExtension(theme_crx, path, ENABLED);
3205
[email protected]b2907fd2011-03-25 16:43:373206 EXPECT_FALSE(service_->pending_extension_manager()->IsIdPending(theme_crx));
[email protected]11edd1e2010-07-21 00:14:503207
[email protected]9adb9692010-10-29 23:14:023208 const Extension* extension = service_->GetExtensionById(theme_crx, true);
[email protected]11edd1e2010-07-21 00:14:503209 ASSERT_TRUE(extension);
3210
[email protected]ad83ca242011-07-29 01:32:253211 EXPECT_FALSE(
3212 service_->extension_prefs()->IsExtensionDisabled(extension->id()));
[email protected]36429da2011-07-11 20:25:183213 EXPECT_TRUE(service_->IsExtensionEnabled(theme_crx));
[email protected]11edd1e2010-07-21 00:14:503214}
3215
[email protected]d74f92e2011-04-18 15:39:233216#if defined(OS_CHROMEOS)
3217// Always fails on ChromeOS: http://crbug.com/79737
[email protected]e00ccc92012-11-01 17:32:303218#define MAYBE_UpdatePendingExternalCrx DISABLED_UpdatePendingExternalCrx
[email protected]d74f92e2011-04-18 15:39:233219#else
3220#define MAYBE_UpdatePendingExternalCrx UpdatePendingExternalCrx
3221#endif
[email protected]8ef78fd2010-08-19 17:14:323222// Test updating a pending CRX as if the source is an external extension
3223// with an update URL. In this case we don't know if the CRX is a theme
3224// or not.
[email protected]d9a61e12012-11-14 02:43:473225TEST_F(ExtensionServiceTest, MAYBE_UpdatePendingExternalCrx) {
[email protected]eaa7dd182010-12-14 11:09:003226 InitializeEmptyExtensionService();
[email protected]9060d8b02012-01-13 02:14:303227 EXPECT_TRUE(service_->pending_extension_manager()->AddFromExternalUpdateUrl(
[email protected]464213a2013-10-15 01:06:483228 theme_crx, GURL(), Manifest::EXTERNAL_PREF_DOWNLOAD, Extension::NO_FLAGS,
3229 false));
[email protected]8ef78fd2010-08-19 17:14:323230
[email protected]b2907fd2011-03-25 16:43:373231 EXPECT_TRUE(service_->pending_extension_manager()->IsIdPending(theme_crx));
[email protected]8ef78fd2010-08-19 17:14:323232
[email protected]650b2d52013-02-10 03:41:453233 base::FilePath path = data_dir_.AppendASCII("theme.crx");
[email protected]8ef78fd2010-08-19 17:14:323234 UpdateExtension(theme_crx, path, ENABLED);
3235
[email protected]b2907fd2011-03-25 16:43:373236 EXPECT_FALSE(service_->pending_extension_manager()->IsIdPending(theme_crx));
[email protected]8ef78fd2010-08-19 17:14:323237
[email protected]9adb9692010-10-29 23:14:023238 const Extension* extension = service_->GetExtensionById(theme_crx, true);
[email protected]8ef78fd2010-08-19 17:14:323239 ASSERT_TRUE(extension);
3240
[email protected]ad83ca242011-07-29 01:32:253241 EXPECT_FALSE(
3242 service_->extension_prefs()->IsExtensionDisabled(extension->id()));
[email protected]36429da2011-07-11 20:25:183243 EXPECT_TRUE(service_->IsExtensionEnabled(extension->id()));
[email protected]1d5cf4142014-01-24 18:25:223244 EXPECT_FALSE(extensions::util::IsIncognitoEnabled(extension->id(),
3245 profile_.get()));
[email protected]8ef78fd2010-08-19 17:14:323246}
3247
[email protected]1afaf2a52010-11-02 19:29:173248// Test updating a pending CRX as if the source is an external extension
3249// with an update URL. The external update should overwrite a sync update,
3250// but a sync update should not overwrite a non-sync update.
[email protected]d9a61e12012-11-14 02:43:473251TEST_F(ExtensionServiceTest, UpdatePendingExternalCrxWinsOverSync) {
[email protected]eaa7dd182010-12-14 11:09:003252 InitializeEmptyExtensionService();
[email protected]1afaf2a52010-11-02 19:29:173253
3254 // Add a crx to be installed from the update mechanism.
[email protected]f4d5e1a2011-04-28 02:08:253255 EXPECT_TRUE(service_->pending_extension_manager()->AddFromSync(
[email protected]7fa19f82010-12-21 19:40:083256 kGoodId, GURL(kGoodUpdateURL), &IsExtension,
[email protected]6cc7dbae2011-04-29 21:18:333257 kGoodInstallSilently));
[email protected]1afaf2a52010-11-02 19:29:173258
3259 // Check that there is a pending crx, with is_from_sync set to true.
[email protected]3f213ad2012-07-26 23:39:413260 const extensions::PendingExtensionInfo* pending_extension_info;
[email protected]51a3bf8b2012-06-08 22:53:063261 ASSERT_TRUE((pending_extension_info = service_->pending_extension_manager()->
3262 GetById(kGoodId)));
3263 EXPECT_TRUE(pending_extension_info->is_from_sync());
[email protected]1afaf2a52010-11-02 19:29:173264
3265 // Add a crx to be updated, with the same ID, from a non-sync source.
[email protected]9060d8b02012-01-13 02:14:303266 EXPECT_TRUE(service_->pending_extension_manager()->AddFromExternalUpdateUrl(
[email protected]464213a2013-10-15 01:06:483267 kGoodId, GURL(kGoodUpdateURL), Manifest::EXTERNAL_PREF_DOWNLOAD,
3268 Extension::NO_FLAGS, false));
[email protected]1afaf2a52010-11-02 19:29:173269
3270 // Check that there is a pending crx, with is_from_sync set to false.
[email protected]51a3bf8b2012-06-08 22:53:063271 ASSERT_TRUE((pending_extension_info = service_->pending_extension_manager()->
3272 GetById(kGoodId)));
3273 EXPECT_FALSE(pending_extension_info->is_from_sync());
[email protected]1d5e58b2013-01-31 08:41:403274 EXPECT_EQ(Manifest::EXTERNAL_PREF_DOWNLOAD,
[email protected]51a3bf8b2012-06-08 22:53:063275 pending_extension_info->install_source());
[email protected]1afaf2a52010-11-02 19:29:173276
3277 // Add a crx to be installed from the update mechanism.
[email protected]f4d5e1a2011-04-28 02:08:253278 EXPECT_FALSE(service_->pending_extension_manager()->AddFromSync(
[email protected]7fa19f82010-12-21 19:40:083279 kGoodId, GURL(kGoodUpdateURL), &IsExtension,
[email protected]6cc7dbae2011-04-29 21:18:333280 kGoodInstallSilently));
[email protected]1afaf2a52010-11-02 19:29:173281
3282 // Check that the external, non-sync update was not overridden.
[email protected]51a3bf8b2012-06-08 22:53:063283 ASSERT_TRUE((pending_extension_info = service_->pending_extension_manager()->
3284 GetById(kGoodId)));
3285 EXPECT_FALSE(pending_extension_info->is_from_sync());
[email protected]1d5e58b2013-01-31 08:41:403286 EXPECT_EQ(Manifest::EXTERNAL_PREF_DOWNLOAD,
[email protected]51a3bf8b2012-06-08 22:53:063287 pending_extension_info->install_source());
[email protected]1afaf2a52010-11-02 19:29:173288}
3289
[email protected]8ef78fd2010-08-19 17:14:323290// Updating a theme should fail if the updater is explicitly told that
3291// the CRX is not a theme.
[email protected]d9a61e12012-11-14 02:43:473292TEST_F(ExtensionServiceTest, UpdatePendingCrxThemeMismatch) {
[email protected]eaa7dd182010-12-14 11:09:003293 InitializeEmptyExtensionService();
[email protected]f4d5e1a2011-04-28 02:08:253294 EXPECT_TRUE(service_->pending_extension_manager()->AddFromSync(
[email protected]6cc7dbae2011-04-29 21:18:333295 theme_crx, GURL(), &IsExtension, true));
[email protected]8ef78fd2010-08-19 17:14:323296
[email protected]b2907fd2011-03-25 16:43:373297 EXPECT_TRUE(service_->pending_extension_manager()->IsIdPending(theme_crx));
[email protected]8ef78fd2010-08-19 17:14:323298
[email protected]650b2d52013-02-10 03:41:453299 base::FilePath path = data_dir_.AppendASCII("theme.crx");
[email protected]8ef78fd2010-08-19 17:14:323300 UpdateExtension(theme_crx, path, FAILED_SILENTLY);
3301
[email protected]b2907fd2011-03-25 16:43:373302 EXPECT_FALSE(service_->pending_extension_manager()->IsIdPending(theme_crx));
[email protected]8ef78fd2010-08-19 17:14:323303
[email protected]9adb9692010-10-29 23:14:023304 const Extension* extension = service_->GetExtensionById(theme_crx, true);
[email protected]8ef78fd2010-08-19 17:14:323305 ASSERT_FALSE(extension);
3306}
3307
[email protected]aa142702010-03-26 01:26:333308// TODO(akalin): Test updating a pending extension non-silently once
3309// we can mock out ExtensionInstallUI and inject our version into
3310// UpdateExtension().
3311
[email protected]7fa19f82010-12-21 19:40:083312// Test updating a pending extension which fails the should-install test.
[email protected]d9a61e12012-11-14 02:43:473313TEST_F(ExtensionServiceTest, UpdatePendingExtensionFailedShouldInstallTest) {
[email protected]eaa7dd182010-12-14 11:09:003314 InitializeEmptyExtensionService();
[email protected]aa142702010-03-26 01:26:333315 // Add pending extension with a flipped is_theme.
[email protected]f4d5e1a2011-04-28 02:08:253316 EXPECT_TRUE(service_->pending_extension_manager()->AddFromSync(
[email protected]6cc7dbae2011-04-29 21:18:333317 kGoodId, GURL(kGoodUpdateURL), &IsTheme, kGoodInstallSilently));
[email protected]b2907fd2011-03-25 16:43:373318 EXPECT_TRUE(service_->pending_extension_manager()->IsIdPending(kGoodId));
[email protected]aa142702010-03-26 01:26:333319
[email protected]650b2d52013-02-10 03:41:453320 base::FilePath path = data_dir_.AppendASCII("good.crx");
[email protected]4416c5a2010-06-26 01:28:573321 UpdateExtension(kGoodId, path, UPDATED);
[email protected]aa142702010-03-26 01:26:333322
3323 // TODO(akalin): Figure out how to check that the extensions
3324 // directory is cleaned up properly in OnExtensionInstalled().
3325
[email protected]b2907fd2011-03-25 16:43:373326 EXPECT_FALSE(service_->pending_extension_manager()->IsIdPending(kGoodId));
[email protected]aa142702010-03-26 01:26:333327}
3328
[email protected]11edd1e2010-07-21 00:14:503329// TODO(akalin): Figure out how to test that installs of pending
3330// unsyncable extensions are blocked.
3331
[email protected]aa142702010-03-26 01:26:333332// Test updating a pending extension for one that is not pending.
[email protected]d9a61e12012-11-14 02:43:473333TEST_F(ExtensionServiceTest, UpdatePendingExtensionNotPending) {
[email protected]eaa7dd182010-12-14 11:09:003334 InitializeEmptyExtensionService();
[email protected]aa142702010-03-26 01:26:333335
[email protected]650b2d52013-02-10 03:41:453336 base::FilePath path = data_dir_.AppendASCII("good.crx");
[email protected]4416c5a2010-06-26 01:28:573337 UpdateExtension(kGoodId, path, UPDATED);
[email protected]aa142702010-03-26 01:26:333338
[email protected]b2907fd2011-03-25 16:43:373339 EXPECT_FALSE(service_->pending_extension_manager()->IsIdPending(kGoodId));
[email protected]aa142702010-03-26 01:26:333340}
3341
3342// Test updating a pending extension for one that is already
3343// installed.
[email protected]d9a61e12012-11-14 02:43:473344TEST_F(ExtensionServiceTest, UpdatePendingExtensionAlreadyInstalled) {
[email protected]eaa7dd182010-12-14 11:09:003345 InitializeEmptyExtensionService();
[email protected]aa142702010-03-26 01:26:333346
[email protected]650b2d52013-02-10 03:41:453347 base::FilePath path = data_dir_.AppendASCII("good.crx");
[email protected]8f512c72011-11-22 21:02:503348 const Extension* good = InstallCRX(path, INSTALL_NEW);
[email protected]aa142702010-03-26 01:26:333349 ASSERT_EQ(1u, service_->extensions()->size());
[email protected]aa142702010-03-26 01:26:333350
[email protected]8ef78fd2010-08-19 17:14:323351 EXPECT_FALSE(good->is_theme());
3352
[email protected]b2907fd2011-03-25 16:43:373353 // Use AddExtensionImpl() as AddFrom*() would balk.
3354 service_->pending_extension_manager()->AddExtensionImpl(
[email protected]65348062013-01-15 07:27:223355 good->id(), extensions::ManifestURL::GetUpdateURL(good),
[email protected]c5e4a2222014-01-03 16:06:133356 Version(), &IsExtension, kGoodIsFromSync,
[email protected]464213a2013-10-15 01:06:483357 kGoodInstallSilently, Manifest::INTERNAL,
3358 Extension::NO_FLAGS, false);
[email protected]6cc7dbae2011-04-29 21:18:333359 UpdateExtension(good->id(), path, ENABLED);
[email protected]aa142702010-03-26 01:26:333360
[email protected]b2907fd2011-03-25 16:43:373361 EXPECT_FALSE(service_->pending_extension_manager()->IsIdPending(kGoodId));
[email protected]e957fe52009-06-23 16:51:053362}
3363
[email protected]3f2a2fa2013-09-24 02:55:253364#if defined(ENABLE_BLACKLIST_TESTS)
3365// Tests blacklisting then unblacklisting extensions after the service has been
3366// initialized.
[email protected]7d9b4e32013-10-25 06:11:543367TEST_F(ExtensionServiceTest, SetUnsetBlacklistInPrefs) {
[email protected]f71b582c2014-01-10 17:03:153368 extensions::TestBlacklist test_blacklist;
[email protected]3f2a2fa2013-09-24 02:55:253369 // A profile with 3 extensions installed: good0, good1, and good2.
3370 InitializeGoodInstalledExtensionService();
[email protected]f71b582c2014-01-10 17:03:153371 test_blacklist.Attach(service_->blacklist_);
[email protected]3f2a2fa2013-09-24 02:55:253372 service_->Init();
[email protected]e636c812013-09-23 05:51:083373
[email protected]5fdfa562013-12-27 17:43:593374 ExtensionRegistry* registry = ExtensionRegistry::Get(profile_.get());
3375 const extensions::ExtensionSet& enabled_extensions =
3376 registry->enabled_extensions();
3377 const extensions::ExtensionSet& blacklisted_extensions =
3378 registry->blacklisted_extensions();
[email protected]e636c812013-09-23 05:51:083379
[email protected]5fdfa562013-12-27 17:43:593380 EXPECT_TRUE(enabled_extensions.Contains(good0) &&
3381 !blacklisted_extensions.Contains(good0));
3382 EXPECT_TRUE(enabled_extensions.Contains(good1) &&
3383 !blacklisted_extensions.Contains(good1));
3384 EXPECT_TRUE(enabled_extensions.Contains(good2) &&
3385 !blacklisted_extensions.Contains(good2));
[email protected]e636c812013-09-23 05:51:083386
[email protected]3f2a2fa2013-09-24 02:55:253387 EXPECT_FALSE(IsPrefExist(good0, "blacklist"));
[email protected]e636c812013-09-23 05:51:083388 EXPECT_FALSE(IsPrefExist(good1, "blacklist"));
[email protected]3f2a2fa2013-09-24 02:55:253389 EXPECT_FALSE(IsPrefExist(good2, "blacklist"));
3390 EXPECT_FALSE(IsPrefExist("invalid_id", "blacklist"));
3391
3392 // Blacklist good0 and good1 (and an invalid extension ID).
[email protected]f71b582c2014-01-10 17:03:153393 test_blacklist.SetBlacklistState(
3394 good0, extensions::BLACKLISTED_MALWARE, true);
3395 test_blacklist.SetBlacklistState(
3396 good1, extensions::BLACKLISTED_MALWARE, true);
3397 test_blacklist.SetBlacklistState(
3398 "invalid_id", extensions::BLACKLISTED_MALWARE, true);
[email protected]3f2a2fa2013-09-24 02:55:253399 base::RunLoop().RunUntilIdle();
3400
[email protected]5fdfa562013-12-27 17:43:593401 EXPECT_TRUE(!enabled_extensions.Contains(good0) &&
3402 blacklisted_extensions.Contains(good0));
3403 EXPECT_TRUE(!enabled_extensions.Contains(good1) &&
3404 blacklisted_extensions.Contains(good1));
3405 EXPECT_TRUE(enabled_extensions.Contains(good2) &&
3406 !blacklisted_extensions.Contains(good2));
[email protected]3f2a2fa2013-09-24 02:55:253407
3408 EXPECT_TRUE(ValidateBooleanPref(good0, "blacklist", true));
3409 EXPECT_TRUE(ValidateBooleanPref(good1, "blacklist", true));
3410 EXPECT_FALSE(IsPrefExist(good2, "blacklist"));
3411 EXPECT_FALSE(IsPrefExist("invalid_id", "blacklist"));
3412
3413 // Un-blacklist good1 and blacklist good2.
[email protected]f71b582c2014-01-10 17:03:153414 test_blacklist.Clear(false);
3415 test_blacklist.SetBlacklistState(
3416 good0, extensions::BLACKLISTED_MALWARE, true);
3417 test_blacklist.SetBlacklistState(
3418 good2, extensions::BLACKLISTED_MALWARE, true);
3419 test_blacklist.SetBlacklistState(
3420 "invalid_id", extensions::BLACKLISTED_MALWARE, true);
[email protected]3f2a2fa2013-09-24 02:55:253421 base::RunLoop().RunUntilIdle();
3422
[email protected]5fdfa562013-12-27 17:43:593423 EXPECT_TRUE(!enabled_extensions.Contains(good0) &&
3424 blacklisted_extensions.Contains(good0));
3425 EXPECT_TRUE(enabled_extensions.Contains(good1) &&
3426 !blacklisted_extensions.Contains(good1));
3427 EXPECT_TRUE(!enabled_extensions.Contains(good2) &&
3428 blacklisted_extensions.Contains(good2));
[email protected]3f2a2fa2013-09-24 02:55:253429
3430 EXPECT_TRUE(ValidateBooleanPref(good0, "blacklist", true));
3431 EXPECT_FALSE(IsPrefExist(good1, "blacklist"));
3432 EXPECT_TRUE(ValidateBooleanPref(good2, "blacklist", true));
[email protected]e2194742010-08-12 05:54:343433 EXPECT_FALSE(IsPrefExist("invalid_id", "blacklist"));
[email protected]6b75ec32009-08-14 06:37:183434}
[email protected]3f2a2fa2013-09-24 02:55:253435#endif // defined(ENABLE_BLACKLIST_TESTS)
[email protected]6b75ec32009-08-14 06:37:183436
[email protected]3f2a2fa2013-09-24 02:55:253437#if defined(ENABLE_BLACKLIST_TESTS)
3438// Tests trying to install a blacklisted extension.
[email protected]e0cbfc42013-11-07 00:10:103439TEST_F(ExtensionServiceTest, BlacklistedExtensionWillNotInstall) {
[email protected]3f2a2fa2013-09-24 02:55:253440 scoped_refptr<FakeSafeBrowsingDatabaseManager> blacklist_db(
3441 new FakeSafeBrowsingDatabaseManager(true));
3442 Blacklist::ScopedDatabaseManagerForTest scoped_blacklist_db(blacklist_db);
3443
[email protected]1747eac02013-09-23 10:58:423444 InitializeEmptyExtensionService();
[email protected]3f2a2fa2013-09-24 02:55:253445 service_->Init();
[email protected]1747eac02013-09-23 10:58:423446
[email protected]3f2a2fa2013-09-24 02:55:253447 // After blacklisting good_crx, we cannot install it.
3448 blacklist_db->SetUnsafe(good_crx).NotifyUpdate();
3449 base::RunLoop().RunUntilIdle();
[email protected]1747eac02013-09-23 10:58:423450
[email protected]650b2d52013-02-10 03:41:453451 base::FilePath path = data_dir_.AppendASCII("good.crx");
[email protected]9f3c8532013-07-31 19:52:073452 // HACK: specify WAS_INSTALLED_BY_DEFAULT so that test machinery doesn't
3453 // decide to install this silently. Somebody should fix these tests, all
3454 // 6,000 lines of them. Hah!
3455 InstallCRX(path, INSTALL_FAILED, Extension::WAS_INSTALLED_BY_DEFAULT);
[email protected]6b75ec32009-08-14 06:37:183456 EXPECT_EQ(0u, service_->extensions()->size());
[email protected]6b75ec32009-08-14 06:37:183457}
[email protected]3f2a2fa2013-09-24 02:55:253458#endif // defined(ENABLE_BLACKLIST_TESTS)
[email protected]6b75ec32009-08-14 06:37:183459
[email protected]3f2a2fa2013-09-24 02:55:253460#if defined(ENABLE_BLACKLIST_TESTS)
[email protected]4ee07c62012-08-21 12:40:423461// Unload blacklisted extension on policy change.
[email protected]d9a61e12012-11-14 02:43:473462TEST_F(ExtensionServiceTest, UnloadBlacklistedExtensionPolicy) {
[email protected]f71b582c2014-01-10 17:03:153463 extensions::TestBlacklist test_blacklist;
[email protected]3f2a2fa2013-09-24 02:55:253464
3465 // A profile with no extensions installed.
[email protected]4ee07c62012-08-21 12:40:423466 InitializeEmptyExtensionService();
[email protected]f71b582c2014-01-10 17:03:153467 test_blacklist.Attach(service_->blacklist_);
[email protected]3f2a2fa2013-09-24 02:55:253468
[email protected]650b2d52013-02-10 03:41:453469 base::FilePath path = data_dir_.AppendASCII("good.crx");
[email protected]4ee07c62012-08-21 12:40:423470
3471 const Extension* good = InstallCRX(path, INSTALL_NEW);
3472 EXPECT_EQ(good_crx, good->id());
3473 UpdateExtension(good_crx, path, FAILED_SILENTLY);
3474 EXPECT_EQ(1u, service_->extensions()->size());
3475
3476 base::ListValue whitelist;
3477 PrefService* prefs = service_->extension_prefs()->pref_service();
[email protected]aa15e2e2013-08-14 02:13:583478 whitelist.Append(new base::StringValue(good_crx));
[email protected]234fc5ff2014-01-16 23:32:283479 prefs->Set(extensions::pref_names::kInstallAllowList, whitelist);
[email protected]4ee07c62012-08-21 12:40:423480
[email protected]f71b582c2014-01-10 17:03:153481 test_blacklist.SetBlacklistState(
3482 good_crx, extensions::BLACKLISTED_MALWARE, true);
[email protected]eedc1c752013-08-09 18:51:553483 base::RunLoop().RunUntilIdle();
[email protected]4ee07c62012-08-21 12:40:423484
[email protected]695b5712012-12-06 23:55:283485 // The good_crx is blacklisted and the whitelist doesn't negate it.
[email protected]3f2a2fa2013-09-24 02:55:253486 ASSERT_TRUE(ValidateBooleanPref(good_crx, "blacklist", true));
[email protected]4ee07c62012-08-21 12:40:423487 EXPECT_EQ(0u, service_->extensions()->size());
3488}
[email protected]3f2a2fa2013-09-24 02:55:253489#endif // defined(ENABLE_BLACKLIST_TESTS)
[email protected]4ee07c62012-08-21 12:40:423490
[email protected]3f2a2fa2013-09-24 02:55:253491#if defined(ENABLE_BLACKLIST_TESTS)
3492// Tests that a blacklisted extension is eventually unloaded on startup, if it
3493// wasn't already.
[email protected]e0cbfc42013-11-07 00:10:103494TEST_F(ExtensionServiceTest, WillNotLoadBlacklistedExtensionsFromDirectory) {
[email protected]f71b582c2014-01-10 17:03:153495 extensions::TestBlacklist test_blacklist;
[email protected]6b75ec32009-08-14 06:37:183496
[email protected]3f2a2fa2013-09-24 02:55:253497 // A profile with 3 extensions installed: good0, good1, and good2.
3498 InitializeGoodInstalledExtensionService();
[email protected]f71b582c2014-01-10 17:03:153499 test_blacklist.Attach(service_->blacklist_);
[email protected]fdd679b2012-11-15 20:49:393500
[email protected]3f2a2fa2013-09-24 02:55:253501 // Blacklist good1 before the service initializes.
[email protected]f71b582c2014-01-10 17:03:153502 test_blacklist.SetBlacklistState(
3503 good1, extensions::BLACKLISTED_MALWARE, false);
[email protected]6b75ec32009-08-14 06:37:183504
3505 // Load extensions.
3506 service_->Init();
[email protected]3f2a2fa2013-09-24 02:55:253507 ASSERT_EQ(3u, loaded_.size()); // hasn't had time to blacklist yet
[email protected]6b75ec32009-08-14 06:37:183508
[email protected]3f2a2fa2013-09-24 02:55:253509 base::RunLoop().RunUntilIdle();
[email protected]6b75ec32009-08-14 06:37:183510
[email protected]5fdfa562013-12-27 17:43:593511 ExtensionRegistry* registry = ExtensionRegistry::Get(profile_.get());
3512 ASSERT_EQ(1u, registry->blacklisted_extensions().size());
3513 ASSERT_EQ(2u, registry->enabled_extensions().size());
3514
3515 ASSERT_TRUE(registry->enabled_extensions().Contains(good0));
3516 ASSERT_TRUE(registry->blacklisted_extensions().Contains(good1));
3517 ASSERT_TRUE(registry->enabled_extensions().Contains(good2));
[email protected]6b75ec32009-08-14 06:37:183518}
[email protected]3f2a2fa2013-09-24 02:55:253519#endif // defined(ENABLE_BLACKLIST_TESTS)
3520
3521#if defined(ENABLE_BLACKLIST_TESTS)
3522// Tests extensions blacklisted in prefs on startup; one still blacklisted by
3523// safe browsing, the other not. The not-blacklisted one should recover.
3524TEST_F(ExtensionServiceTest, BlacklistedInPrefsFromStartup) {
[email protected]f71b582c2014-01-10 17:03:153525 extensions::TestBlacklist test_blacklist;
[email protected]3f2a2fa2013-09-24 02:55:253526
3527 InitializeGoodInstalledExtensionService();
[email protected]f71b582c2014-01-10 17:03:153528 test_blacklist.Attach(service_->blacklist_);
[email protected]3f2a2fa2013-09-24 02:55:253529 service_->extension_prefs()->SetExtensionBlacklisted(good0, true);
3530 service_->extension_prefs()->SetExtensionBlacklisted(good1, true);
3531
[email protected]f71b582c2014-01-10 17:03:153532 test_blacklist.SetBlacklistState(
3533 good1, extensions::BLACKLISTED_MALWARE, false);
[email protected]3f2a2fa2013-09-24 02:55:253534
3535 service_->Init();
3536
[email protected]5fdfa562013-12-27 17:43:593537 ExtensionRegistry* registry = ExtensionRegistry::Get(profile_.get());
3538 ASSERT_EQ(2u, registry->blacklisted_extensions().size());
3539 ASSERT_EQ(1u, registry->enabled_extensions().size());
[email protected]3f2a2fa2013-09-24 02:55:253540
[email protected]5fdfa562013-12-27 17:43:593541 ASSERT_TRUE(registry->blacklisted_extensions().Contains(good0));
3542 ASSERT_TRUE(registry->blacklisted_extensions().Contains(good1));
3543 ASSERT_TRUE(registry->enabled_extensions().Contains(good2));
[email protected]3f2a2fa2013-09-24 02:55:253544
3545 // Give time for the blacklist to update.
3546 base::RunLoop().RunUntilIdle();
3547
[email protected]5fdfa562013-12-27 17:43:593548 ASSERT_EQ(1u, registry->blacklisted_extensions().size());
3549 ASSERT_EQ(2u, registry->enabled_extensions().size());
[email protected]3f2a2fa2013-09-24 02:55:253550
[email protected]5fdfa562013-12-27 17:43:593551 ASSERT_TRUE(registry->enabled_extensions().Contains(good0));
3552 ASSERT_TRUE(registry->blacklisted_extensions().Contains(good1));
3553 ASSERT_TRUE(registry->enabled_extensions().Contains(good2));
[email protected]3f2a2fa2013-09-24 02:55:253554}
3555#endif // defined(ENABLE_BLACKLIST_TESTS)
[email protected]6b75ec32009-08-14 06:37:183556
[email protected]2d19eb6e2014-01-27 17:30:003557#if defined(ENABLE_BLACKLIST_TESTS)
3558// Extension is added to blacklist with BLACKLISTED_POTENTIALLY_UNWANTED state
3559// after it is installed. It is then successfully re-enabled by the user.
3560TEST_F(ExtensionServiceTest, GreylistedExtensionDisabled) {
3561 extensions::TestBlacklist test_blacklist;
3562 // A profile with 3 extensions installed: good0, good1, and good2.
3563 InitializeGoodInstalledExtensionService();
3564 test_blacklist.Attach(service_->blacklist_);
3565 service_->Init();
3566
3567 ExtensionRegistry* registry = ExtensionRegistry::Get(profile_.get());
3568 const extensions::ExtensionSet& enabled_extensions =
3569 registry->enabled_extensions();
3570 const extensions::ExtensionSet& disabled_extensions =
3571 registry->disabled_extensions();
3572
3573 EXPECT_TRUE(enabled_extensions.Contains(good0));
3574 EXPECT_TRUE(enabled_extensions.Contains(good1));
3575 EXPECT_TRUE(enabled_extensions.Contains(good2));
3576
3577 // Blacklist good0 and good1 (and an invalid extension ID).
3578 test_blacklist.SetBlacklistState(
3579 good0, extensions::BLACKLISTED_CWS_POLICY_VIOLATION, true);
3580 test_blacklist.SetBlacklistState(
3581 good1, extensions::BLACKLISTED_POTENTIALLY_UNWANTED, true);
3582 test_blacklist.SetBlacklistState(
3583 "invalid_id", extensions::BLACKLISTED_MALWARE, true);
3584 base::RunLoop().RunUntilIdle();
3585
3586 EXPECT_FALSE(enabled_extensions.Contains(good0));
3587 EXPECT_TRUE(disabled_extensions.Contains(good0));
3588 EXPECT_FALSE(enabled_extensions.Contains(good1));
3589 EXPECT_TRUE(disabled_extensions.Contains(good1));
3590 EXPECT_TRUE(enabled_extensions.Contains(good2));
3591 EXPECT_FALSE(disabled_extensions.Contains(good2));
3592
3593 ValidateIntegerPref(
3594 good0, "blacklist_state", extensions::BLACKLISTED_CWS_POLICY_VIOLATION);
3595 ValidateIntegerPref(
3596 good1, "blacklist_state", extensions::BLACKLISTED_POTENTIALLY_UNWANTED);
3597
3598 // Now user enables good0.
3599 service_->EnableExtension(good0);
3600
3601 EXPECT_TRUE(enabled_extensions.Contains(good0));
3602 EXPECT_FALSE(disabled_extensions.Contains(good0));
3603 EXPECT_FALSE(enabled_extensions.Contains(good1));
3604 EXPECT_TRUE(disabled_extensions.Contains(good1));
3605
3606 // Remove extensions from blacklist.
3607 test_blacklist.SetBlacklistState(
3608 good0, extensions::NOT_BLACKLISTED, true);
3609 test_blacklist.SetBlacklistState(
3610 good1, extensions::NOT_BLACKLISTED, true);
3611 base::RunLoop().RunUntilIdle();
3612
3613 // All extensions are enabled.
3614 EXPECT_TRUE(enabled_extensions.Contains(good0));
3615 EXPECT_FALSE(disabled_extensions.Contains(good0));
3616 EXPECT_TRUE(enabled_extensions.Contains(good1));
3617 EXPECT_FALSE(disabled_extensions.Contains(good1));
3618 EXPECT_TRUE(enabled_extensions.Contains(good2));
3619 EXPECT_FALSE(disabled_extensions.Contains(good2));
3620}
3621#endif // defined(ENABLE_BLACKLIST_TESTS)
3622
3623#if defined(ENABLE_BLACKLIST_TESTS)
3624// When extension is removed from greylist, do not re-enable it if it is
3625// disabled by user.
3626TEST_F(ExtensionServiceTest, GreylistDontEnableManuallyDisabled) {
3627 extensions::TestBlacklist test_blacklist;
3628 // A profile with 3 extensions installed: good0, good1, and good2.
3629 InitializeGoodInstalledExtensionService();
3630 test_blacklist.Attach(service_->blacklist_);
3631 service_->Init();
3632
3633 ExtensionRegistry* registry = ExtensionRegistry::Get(profile_.get());
3634 const extensions::ExtensionSet& enabled_extensions =
3635 registry->enabled_extensions();
3636 const extensions::ExtensionSet& disabled_extensions =
3637 registry->disabled_extensions();
3638
3639 // Manually disable.
3640 service_->DisableExtension(good0, extensions::Extension::DISABLE_USER_ACTION);
3641
3642 test_blacklist.SetBlacklistState(
3643 good0, extensions::BLACKLISTED_CWS_POLICY_VIOLATION, true);
3644 test_blacklist.SetBlacklistState(
3645 good1, extensions::BLACKLISTED_POTENTIALLY_UNWANTED, true);
3646 test_blacklist.SetBlacklistState(
3647 good2, extensions::BLACKLISTED_SECURITY_VULNERABILITY, true);
3648 base::RunLoop().RunUntilIdle();
3649
3650 // All extensions disabled.
3651 EXPECT_FALSE(enabled_extensions.Contains(good0));
3652 EXPECT_TRUE(disabled_extensions.Contains(good0));
3653 EXPECT_FALSE(enabled_extensions.Contains(good1));
3654 EXPECT_TRUE(disabled_extensions.Contains(good1));
3655 EXPECT_FALSE(enabled_extensions.Contains(good2));
3656 EXPECT_TRUE(disabled_extensions.Contains(good2));
3657
3658 // Greylisted extension can be enabled.
3659 service_->EnableExtension(good1);
3660 EXPECT_TRUE(enabled_extensions.Contains(good1));
3661 EXPECT_FALSE(disabled_extensions.Contains(good1));
3662
3663 // good1 is now manually disabled.
3664 service_->DisableExtension(good1, extensions::Extension::DISABLE_USER_ACTION);
3665 EXPECT_FALSE(enabled_extensions.Contains(good1));
3666 EXPECT_TRUE(disabled_extensions.Contains(good1));
3667
3668 // Remove extensions from blacklist.
3669 test_blacklist.SetBlacklistState(
3670 good0, extensions::NOT_BLACKLISTED, true);
3671 test_blacklist.SetBlacklistState(
3672 good1, extensions::NOT_BLACKLISTED, true);
3673 test_blacklist.SetBlacklistState(
3674 good2, extensions::NOT_BLACKLISTED, true);
3675 base::RunLoop().RunUntilIdle();
3676
3677 // good0 and good1 remain disabled.
3678 EXPECT_FALSE(enabled_extensions.Contains(good0));
3679 EXPECT_TRUE(disabled_extensions.Contains(good0));
3680 EXPECT_FALSE(enabled_extensions.Contains(good1));
3681 EXPECT_TRUE(disabled_extensions.Contains(good1));
3682 EXPECT_TRUE(enabled_extensions.Contains(good2));
3683 EXPECT_FALSE(disabled_extensions.Contains(good2));
3684}
3685#endif // defined(ENABLE_BLACKLIST_TESTS)
3686
3687#if defined(ENABLE_BLACKLIST_TESTS)
3688// Blacklisted extension with unknown state are not enabled/disabled.
3689TEST_F(ExtensionServiceTest, GreylistUnknownDontChange) {
3690 extensions::TestBlacklist test_blacklist;
3691 // A profile with 3 extensions installed: good0, good1, and good2.
3692 InitializeGoodInstalledExtensionService();
3693 test_blacklist.Attach(service_->blacklist_);
3694 service_->Init();
3695
3696 ExtensionRegistry* registry = ExtensionRegistry::Get(profile_.get());
3697 const extensions::ExtensionSet& enabled_extensions =
3698 registry->enabled_extensions();
3699 const extensions::ExtensionSet& disabled_extensions =
3700 registry->disabled_extensions();
3701
3702 test_blacklist.SetBlacklistState(
3703 good0, extensions::BLACKLISTED_CWS_POLICY_VIOLATION, true);
3704 test_blacklist.SetBlacklistState(
3705 good1, extensions::BLACKLISTED_POTENTIALLY_UNWANTED, true);
3706 base::RunLoop().RunUntilIdle();
3707
3708 EXPECT_FALSE(enabled_extensions.Contains(good0));
3709 EXPECT_TRUE(disabled_extensions.Contains(good0));
3710 EXPECT_FALSE(enabled_extensions.Contains(good1));
3711 EXPECT_TRUE(disabled_extensions.Contains(good1));
3712 EXPECT_TRUE(enabled_extensions.Contains(good2));
3713 EXPECT_FALSE(disabled_extensions.Contains(good2));
3714
3715 test_blacklist.SetBlacklistState(
3716 good0, extensions::NOT_BLACKLISTED, true);
3717 test_blacklist.SetBlacklistState(
3718 good1, extensions::BLACKLISTED_UNKNOWN, true);
3719 test_blacklist.SetBlacklistState(
3720 good2, extensions::BLACKLISTED_UNKNOWN, true);
3721 base::RunLoop().RunUntilIdle();
3722
3723 // good0 re-enabled, other remain as they were.
3724 EXPECT_TRUE(enabled_extensions.Contains(good0));
3725 EXPECT_FALSE(disabled_extensions.Contains(good0));
3726 EXPECT_FALSE(enabled_extensions.Contains(good1));
3727 EXPECT_TRUE(disabled_extensions.Contains(good1));
3728 EXPECT_TRUE(enabled_extensions.Contains(good2));
3729 EXPECT_FALSE(disabled_extensions.Contains(good2));
3730}
3731#endif // defined(ENABLE_BLACKLIST_TESTS)
3732
[email protected]306a2bd2010-08-11 14:56:363733// Will not install extension blacklisted by policy.
[email protected]d9a61e12012-11-14 02:43:473734TEST_F(ExtensionServiceTest, BlacklistedByPolicyWillNotInstall) {
[email protected]eaa7dd182010-12-14 11:09:003735 InitializeEmptyExtensionService();
[email protected]306a2bd2010-08-11 14:56:363736
[email protected]306a2bd2010-08-11 14:56:363737 // Blacklist everything.
[email protected]43d3bf82011-04-11 07:46:583738 {
3739 ListPrefUpdate update(profile_->GetPrefs(),
[email protected]234fc5ff2014-01-16 23:32:283740 extensions::pref_names::kInstallDenyList);
[email protected]023b3d12013-12-23 18:46:493741 base::ListValue* blacklist = update.Get();
[email protected]aa15e2e2013-08-14 02:13:583742 blacklist->Append(new base::StringValue("*"));
[email protected]43d3bf82011-04-11 07:46:583743 }
[email protected]306a2bd2010-08-11 14:56:363744
3745 // Blacklist prevents us from installing good_crx.
[email protected]650b2d52013-02-10 03:41:453746 base::FilePath path = data_dir_.AppendASCII("good.crx");
[email protected]8f512c72011-11-22 21:02:503747 InstallCRX(path, INSTALL_FAILED);
[email protected]306a2bd2010-08-11 14:56:363748 EXPECT_EQ(0u, service_->extensions()->size());
3749
3750 // Now whitelist this particular extension.
[email protected]43d3bf82011-04-11 07:46:583751 {
3752 ListPrefUpdate update(profile_->GetPrefs(),
[email protected]234fc5ff2014-01-16 23:32:283753 extensions::pref_names::kInstallAllowList);
[email protected]023b3d12013-12-23 18:46:493754 base::ListValue* whitelist = update.Get();
[email protected]aa15e2e2013-08-14 02:13:583755 whitelist->Append(new base::StringValue(good_crx));
[email protected]43d3bf82011-04-11 07:46:583756 }
3757
[email protected]306a2bd2010-08-11 14:56:363758 // Ensure we can now install good_crx.
[email protected]8f512c72011-11-22 21:02:503759 InstallCRX(path, INSTALL_NEW);
[email protected]306a2bd2010-08-11 14:56:363760 EXPECT_EQ(1u, service_->extensions()->size());
3761}
3762
[email protected]aa96d3a2010-08-21 08:45:253763// Extension blacklisted by policy get unloaded after installing.
[email protected]d9a61e12012-11-14 02:43:473764TEST_F(ExtensionServiceTest, BlacklistedByPolicyRemovedIfRunning) {
[email protected]eaa7dd182010-12-14 11:09:003765 InitializeEmptyExtensionService();
[email protected]aa96d3a2010-08-21 08:45:253766
3767 // Install good_crx.
[email protected]650b2d52013-02-10 03:41:453768 base::FilePath path = data_dir_.AppendASCII("good.crx");
[email protected]8f512c72011-11-22 21:02:503769 InstallCRX(path, INSTALL_NEW);
[email protected]aa96d3a2010-08-21 08:45:253770 EXPECT_EQ(1u, service_->extensions()->size());
3771
[email protected]acd78969c2010-12-08 09:49:113772 { // Scope for pref update notification.
3773 PrefService* prefs = profile_->GetPrefs();
[email protected]234fc5ff2014-01-16 23:32:283774 ListPrefUpdate update(prefs, extensions::pref_names::kInstallDenyList);
[email protected]023b3d12013-12-23 18:46:493775 base::ListValue* blacklist = update.Get();
[email protected]acd78969c2010-12-08 09:49:113776 ASSERT_TRUE(blacklist != NULL);
[email protected]aa96d3a2010-08-21 08:45:253777
[email protected]acd78969c2010-12-08 09:49:113778 // Blacklist this extension.
[email protected]aa15e2e2013-08-14 02:13:583779 blacklist->Append(new base::StringValue(good_crx));
[email protected]acd78969c2010-12-08 09:49:113780 }
[email protected]aa96d3a2010-08-21 08:45:253781
3782 // Extension should not be running now.
[email protected]eedc1c752013-08-09 18:51:553783 base::RunLoop().RunUntilIdle();
[email protected]aa96d3a2010-08-21 08:45:253784 EXPECT_EQ(0u, service_->extensions()->size());
3785}
3786
[email protected]05aad2da2011-10-28 10:12:373787// Tests that component extensions are not blacklisted by policy.
[email protected]d9a61e12012-11-14 02:43:473788TEST_F(ExtensionServiceTest, ComponentExtensionWhitelisted) {
[email protected]05aad2da2011-10-28 10:12:373789 InitializeEmptyExtensionService();
3790
3791 // Blacklist everything.
3792 {
3793 ListPrefUpdate update(profile_->GetPrefs(),
[email protected]234fc5ff2014-01-16 23:32:283794 extensions::pref_names::kInstallDenyList);
[email protected]023b3d12013-12-23 18:46:493795 base::ListValue* blacklist = update.Get();
[email protected]aa15e2e2013-08-14 02:13:583796 blacklist->Append(new base::StringValue("*"));
[email protected]05aad2da2011-10-28 10:12:373797 }
3798
3799 // Install a component extension.
[email protected]650b2d52013-02-10 03:41:453800 base::FilePath path = data_dir_
[email protected]05aad2da2011-10-28 10:12:373801 .AppendASCII("good")
3802 .AppendASCII("Extensions")
3803 .AppendASCII(good0)
3804 .AppendASCII("1.0.0.0");
3805 std::string manifest;
[email protected]82f84b92013-08-30 18:23:503806 ASSERT_TRUE(base::ReadFileToString(
[email protected]993da5e2013-03-23 21:25:163807 path.Append(extensions::kManifestFilename), &manifest));
[email protected]d8c8f25f2011-11-02 18:18:013808 service_->component_loader()->Add(manifest, path);
[email protected]05aad2da2011-10-28 10:12:373809 service_->Init();
3810
3811 // Extension should be installed despite blacklist.
3812 ASSERT_EQ(1u, service_->extensions()->size());
[email protected]8f512c72011-11-22 21:02:503813 EXPECT_TRUE(service_->GetExtensionById(good0, false));
[email protected]05aad2da2011-10-28 10:12:373814
3815 // Poke external providers and make sure the extension is still present.
3816 service_->CheckForExternalUpdates();
3817 ASSERT_EQ(1u, service_->extensions()->size());
[email protected]8f512c72011-11-22 21:02:503818 EXPECT_TRUE(service_->GetExtensionById(good0, false));
[email protected]05aad2da2011-10-28 10:12:373819
3820 // Extension should not be uninstalled on blacklist changes.
3821 {
3822 ListPrefUpdate update(profile_->GetPrefs(),
[email protected]234fc5ff2014-01-16 23:32:283823 extensions::pref_names::kInstallDenyList);
[email protected]023b3d12013-12-23 18:46:493824 base::ListValue* blacklist = update.Get();
[email protected]aa15e2e2013-08-14 02:13:583825 blacklist->Append(new base::StringValue(good0));
[email protected]05aad2da2011-10-28 10:12:373826 }
[email protected]eedc1c752013-08-09 18:51:553827 base::RunLoop().RunUntilIdle();
[email protected]05aad2da2011-10-28 10:12:373828 ASSERT_EQ(1u, service_->extensions()->size());
[email protected]8f512c72011-11-22 21:02:503829 EXPECT_TRUE(service_->GetExtensionById(good0, false));
[email protected]05aad2da2011-10-28 10:12:373830}
3831
3832// Tests that policy-installed extensions are not blacklisted by policy.
[email protected]d9a61e12012-11-14 02:43:473833TEST_F(ExtensionServiceTest, PolicyInstalledExtensionsWhitelisted) {
[email protected]05aad2da2011-10-28 10:12:373834 InitializeEmptyExtensionService();
3835
[email protected]05aad2da2011-10-28 10:12:373836 {
[email protected]e410b5f2012-12-14 14:02:243837 // Blacklist everything.
[email protected]234fc5ff2014-01-16 23:32:283838 ListPrefUpdate blacklist_update(
3839 profile_->GetPrefs(), extensions::pref_names::kInstallDenyList);
[email protected]023b3d12013-12-23 18:46:493840 base::ListValue* blacklist = blacklist_update.Get();
[email protected]e410b5f2012-12-14 14:02:243841 blacklist->AppendString("*");
3842
3843 // Mark good.crx for force-installation.
[email protected]234fc5ff2014-01-16 23:32:283844 DictionaryPrefUpdate forcelist_update(
3845 profile_->GetPrefs(),
3846 extensions::pref_names::kInstallForceList);
[email protected]e410b5f2012-12-14 14:02:243847 extensions::ExternalPolicyLoader::AddExtension(
3848 forcelist_update.Get(), good_crx, "http://example.com/update_url");
[email protected]05aad2da2011-10-28 10:12:373849 }
3850
3851 // Have policy force-install an extension.
3852 MockExtensionProvider* provider =
3853 new MockExtensionProvider(service_,
[email protected]1d5e58b2013-01-31 08:41:403854 Manifest::EXTERNAL_POLICY_DOWNLOAD);
[email protected]05aad2da2011-10-28 10:12:373855 AddMockExternalProvider(provider);
3856 provider->UpdateOrAddExtension(good_crx, "1.0.0.0",
3857 data_dir_.AppendASCII("good.crx"));
3858
3859 // Reloading extensions should find our externally registered extension
3860 // and install it.
[email protected]97d6a5c2013-11-11 23:51:243861 content::WindowedNotificationObserver observer(
[email protected]6d057a0c2013-07-09 21:12:073862 chrome::NOTIFICATION_CRX_INSTALLER_DONE,
[email protected]97d6a5c2013-11-11 23:51:243863 content::NotificationService::AllSources());
3864 service_->CheckForExternalUpdates();
3865 observer.Wait();
[email protected]05aad2da2011-10-28 10:12:373866
3867 // Extension should be installed despite blacklist.
3868 ASSERT_EQ(1u, service_->extensions()->size());
[email protected]8f512c72011-11-22 21:02:503869 EXPECT_TRUE(service_->GetExtensionById(good_crx, false));
[email protected]05aad2da2011-10-28 10:12:373870
3871 // Blacklist update should not uninstall the extension.
3872 {
3873 ListPrefUpdate update(profile_->GetPrefs(),
[email protected]234fc5ff2014-01-16 23:32:283874 extensions::pref_names::kInstallDenyList);
[email protected]023b3d12013-12-23 18:46:493875 base::ListValue* blacklist = update.Get();
[email protected]aa15e2e2013-08-14 02:13:583876 blacklist->Append(new base::StringValue(good0));
[email protected]05aad2da2011-10-28 10:12:373877 }
[email protected]eedc1c752013-08-09 18:51:553878 base::RunLoop().RunUntilIdle();
[email protected]05aad2da2011-10-28 10:12:373879 ASSERT_EQ(1u, service_->extensions()->size());
[email protected]8f512c72011-11-22 21:02:503880 EXPECT_TRUE(service_->GetExtensionById(good_crx, false));
[email protected]05aad2da2011-10-28 10:12:373881}
3882
[email protected]65187152012-06-02 13:14:143883// Tests that extensions cannot be installed if the policy provider prohibits
3884// it. This functionality is implemented in CrxInstaller::ConfirmInstall().
[email protected]d9a61e12012-11-14 02:43:473885TEST_F(ExtensionServiceTest, ManagementPolicyProhibitsInstall) {
[email protected]65187152012-06-02 13:14:143886 InitializeEmptyExtensionService();
3887
3888 management_policy_->UnregisterAllProviders();
3889 extensions::TestManagementPolicyProvider provider_(
3890 extensions::TestManagementPolicyProvider::PROHIBIT_LOAD);
3891 management_policy_->RegisterProvider(&provider_);
3892
3893 InstallCRX(data_dir_.AppendASCII("good.crx"), INSTALL_FAILED);
3894 EXPECT_EQ(0u, service_->extensions()->size());
3895}
3896
3897// Tests that extensions cannot be loaded from prefs if the policy provider
3898// prohibits it. This functionality is implemented in InstalledLoader::Load().
[email protected]d9a61e12012-11-14 02:43:473899TEST_F(ExtensionServiceTest, ManagementPolicyProhibitsLoadFromPrefs) {
[email protected]65187152012-06-02 13:14:143900 InitializeEmptyExtensionService();
3901
3902 // Create a fake extension to be loaded as though it were read from prefs.
[email protected]650b2d52013-02-10 03:41:453903 base::FilePath path = data_dir_.AppendASCII("management")
[email protected]65187152012-06-02 13:14:143904 .AppendASCII("simple_extension");
[email protected]023b3d12013-12-23 18:46:493905 base::DictionaryValue manifest;
[email protected]65187152012-06-02 13:14:143906 manifest.SetString(keys::kName, "simple_extension");
3907 manifest.SetString(keys::kVersion, "1");
[email protected]12075d12013-02-27 05:38:053908 // UNPACKED is for extensions loaded from a directory. We use it here, even
[email protected]65187152012-06-02 13:14:143909 // though we're testing loading from prefs, so that we don't need to provide
3910 // an extension key.
[email protected]007b3f82013-04-09 08:46:453911 extensions::ExtensionInfo extension_info(
3912 &manifest, std::string(), path, Manifest::UNPACKED);
[email protected]65187152012-06-02 13:14:143913
3914 // Ensure we can load it with no management policy in place.
3915 management_policy_->UnregisterAllProviders();
3916 EXPECT_EQ(0u, service_->extensions()->size());
3917 extensions::InstalledLoader(service_).Load(extension_info, false);
3918 EXPECT_EQ(1u, service_->extensions()->size());
3919
[email protected]cadac622013-06-11 16:46:363920 const Extension* extension = (service_->extensions()->begin())->get();
[email protected]65187152012-06-02 13:14:143921 EXPECT_TRUE(service_->UninstallExtension(extension->id(), false, NULL));
3922 EXPECT_EQ(0u, service_->extensions()->size());
3923
3924 // Ensure we cannot load it if management policy prohibits installation.
3925 extensions::TestManagementPolicyProvider provider_(
3926 extensions::TestManagementPolicyProvider::PROHIBIT_LOAD);
3927 management_policy_->RegisterProvider(&provider_);
3928
3929 extensions::InstalledLoader(service_).Load(extension_info, false);
3930 EXPECT_EQ(0u, service_->extensions()->size());
3931}
3932
3933// Tests disabling an extension when prohibited by the ManagementPolicy.
[email protected]d9a61e12012-11-14 02:43:473934TEST_F(ExtensionServiceTest, ManagementPolicyProhibitsDisable) {
[email protected]65187152012-06-02 13:14:143935 InitializeEmptyExtensionService();
3936
3937 InstallCRX(data_dir_.AppendASCII("good.crx"), INSTALL_NEW);
3938 EXPECT_EQ(1u, service_->extensions()->size());
3939 EXPECT_EQ(0u, service_->disabled_extensions()->size());
3940
3941 management_policy_->UnregisterAllProviders();
3942 extensions::TestManagementPolicyProvider provider(
3943 extensions::TestManagementPolicyProvider::PROHIBIT_MODIFY_STATUS);
3944 management_policy_->RegisterProvider(&provider);
3945
3946 // Attempt to disable it.
3947 service_->DisableExtension(good_crx, Extension::DISABLE_USER_ACTION);
3948
3949 EXPECT_EQ(1u, service_->extensions()->size());
3950 EXPECT_TRUE(service_->GetExtensionById(good_crx, false));
3951 EXPECT_EQ(0u, service_->disabled_extensions()->size());
3952}
3953
3954// Tests uninstalling an extension when prohibited by the ManagementPolicy.
[email protected]d9a61e12012-11-14 02:43:473955TEST_F(ExtensionServiceTest, ManagementPolicyProhibitsUninstall) {
[email protected]65187152012-06-02 13:14:143956 InitializeEmptyExtensionService();
3957
3958 InstallCRX(data_dir_.AppendASCII("good.crx"), INSTALL_NEW);
3959 EXPECT_EQ(1u, service_->extensions()->size());
3960 EXPECT_EQ(0u, service_->disabled_extensions()->size());
3961
3962 management_policy_->UnregisterAllProviders();
3963 extensions::TestManagementPolicyProvider provider(
3964 extensions::TestManagementPolicyProvider::PROHIBIT_MODIFY_STATUS);
3965 management_policy_->RegisterProvider(&provider);
3966
3967 // Attempt to uninstall it.
3968 EXPECT_FALSE(service_->UninstallExtension(good_crx, false, NULL));
3969
3970 EXPECT_EQ(1u, service_->extensions()->size());
3971 EXPECT_TRUE(service_->GetExtensionById(good_crx, false));
3972}
3973
3974// Tests that previously installed extensions that are now prohibited from
3975// being installed are removed.
[email protected]d9a61e12012-11-14 02:43:473976TEST_F(ExtensionServiceTest, ManagementPolicyUnloadsAllProhibited) {
[email protected]65187152012-06-02 13:14:143977 InitializeEmptyExtensionService();
3978
3979 InstallCRX(data_dir_.AppendASCII("good.crx"), INSTALL_NEW);
3980 InstallCRX(data_dir_.AppendASCII("page_action.crx"), INSTALL_NEW);
3981 EXPECT_EQ(2u, service_->extensions()->size());
3982 EXPECT_EQ(0u, service_->disabled_extensions()->size());
3983
3984 management_policy_->UnregisterAllProviders();
3985 extensions::TestManagementPolicyProvider provider(
3986 extensions::TestManagementPolicyProvider::PROHIBIT_LOAD);
3987 management_policy_->RegisterProvider(&provider);
3988
3989 // Run the policy check.
[email protected]4ee07c62012-08-21 12:40:423990 service_->CheckManagementPolicy();
[email protected]65187152012-06-02 13:14:143991 EXPECT_EQ(0u, service_->extensions()->size());
3992 EXPECT_EQ(0u, service_->disabled_extensions()->size());
3993}
3994
3995// Tests that previously disabled extensions that are now required to be
3996// enabled are re-enabled on reinstall.
[email protected]d9a61e12012-11-14 02:43:473997TEST_F(ExtensionServiceTest, ManagementPolicyRequiresEnable) {
[email protected]65187152012-06-02 13:14:143998 InitializeEmptyExtensionService();
3999
4000 // Install, then disable, an extension.
4001 InstallCRX(data_dir_.AppendASCII("good.crx"), INSTALL_NEW);
4002 EXPECT_EQ(1u, service_->extensions()->size());
4003 service_->DisableExtension(good_crx, Extension::DISABLE_USER_ACTION);
4004 EXPECT_EQ(1u, service_->disabled_extensions()->size());
4005
4006 // Register an ExtensionMnagementPolicy that requires the extension to remain
4007 // enabled.
4008 management_policy_->UnregisterAllProviders();
4009 extensions::TestManagementPolicyProvider provider(
4010 extensions::TestManagementPolicyProvider::MUST_REMAIN_ENABLED);
4011 management_policy_->RegisterProvider(&provider);
4012
4013 // Reinstall the extension.
4014 InstallCRX(data_dir_.AppendASCII("good.crx"), INSTALL_UPDATED);
4015 EXPECT_EQ(1u, service_->extensions()->size());
4016 EXPECT_EQ(0u, service_->disabled_extensions()->size());
4017}
4018
[email protected]ebc3aec2013-10-22 02:39:174019// Flaky on windows; http://crbug.com/309833
4020#if defined(OS_WIN)
4021#define MAYBE_ExternalExtensionAutoAcknowledgement DISABLED_ExternalExtensionAutoAcknowledgement
4022#else
4023#define MAYBE_ExternalExtensionAutoAcknowledgement ExternalExtensionAutoAcknowledgement
4024#endif
[email protected]d9a61e12012-11-14 02:43:474025TEST_F(ExtensionServiceTest, ExternalExtensionAutoAcknowledgement) {
[email protected]a39921b42012-02-28 03:42:544026 InitializeEmptyExtensionService();
4027 set_extensions_enabled(true);
4028
4029 {
4030 // Register and install an external extension.
4031 MockExtensionProvider* provider =
[email protected]1d5e58b2013-01-31 08:41:404032 new MockExtensionProvider(service_, Manifest::EXTERNAL_PREF);
[email protected]a39921b42012-02-28 03:42:544033 AddMockExternalProvider(provider);
4034 provider->UpdateOrAddExtension(good_crx, "1.0.0.0",
4035 data_dir_.AppendASCII("good.crx"));
4036 }
4037 {
4038 // Have policy force-install an extension.
4039 MockExtensionProvider* provider =
4040 new MockExtensionProvider(service_,
[email protected]1d5e58b2013-01-31 08:41:404041 Manifest::EXTERNAL_POLICY_DOWNLOAD);
[email protected]a39921b42012-02-28 03:42:544042 AddMockExternalProvider(provider);
4043 provider->UpdateOrAddExtension(page_action, "1.0.0.0",
4044 data_dir_.AppendASCII("page_action.crx"));
4045 }
4046
4047 // Providers are set up. Let them run.
[email protected]97d6a5c2013-11-11 23:51:244048 int count = 2;
4049 content::WindowedNotificationObserver observer(
4050 chrome::NOTIFICATION_CRX_INSTALLER_DONE,
4051 base::Bind(&WaitForCountNotificationsCallback, &count));
[email protected]a39921b42012-02-28 03:42:544052 service_->CheckForExternalUpdates();
[email protected]6d057a0c2013-07-09 21:12:074053
[email protected]97d6a5c2013-11-11 23:51:244054 observer.Wait();
[email protected]a39921b42012-02-28 03:42:544055
4056 ASSERT_EQ(2u, service_->extensions()->size());
4057 EXPECT_TRUE(service_->GetExtensionById(good_crx, false));
4058 EXPECT_TRUE(service_->GetExtensionById(page_action, false));
4059 ExtensionPrefs* prefs = service_->extension_prefs();
4060 ASSERT_TRUE(!prefs->IsExternalExtensionAcknowledged(good_crx));
4061 ASSERT_TRUE(prefs->IsExternalExtensionAcknowledged(page_action));
4062}
4063
[email protected]a7cd28e2012-10-05 21:03:364064#if !defined(OS_CHROMEOS)
4065// This tests if default apps are installed correctly.
[email protected]d9a61e12012-11-14 02:43:474066TEST_F(ExtensionServiceTest, DefaultAppsInstall) {
[email protected]a7cd28e2012-10-05 21:03:364067 InitializeEmptyExtensionService();
[email protected]a7cd28e2012-10-05 21:03:364068 set_extensions_enabled(true);
4069
4070 {
4071 std::string json_data =
4072 "{"
4073 " \"ldnnhddmnhbkjipkidpdiheffobcpfmf\" : {"
4074 " \"external_crx\": \"good.crx\","
4075 " \"external_version\": \"1.0.0.0\","
4076 " \"is_bookmark_app\": false"
4077 " }"
4078 "}";
4079 default_apps::Provider* provider =
4080 new default_apps::Provider(
4081 profile_.get(),
4082 service_,
4083 new extensions::ExternalTestingLoader(json_data, data_dir_),
[email protected]1d5e58b2013-01-31 08:41:404084 Manifest::INTERNAL,
4085 Manifest::INVALID_LOCATION,
[email protected]a7cd28e2012-10-05 21:03:364086 Extension::FROM_WEBSTORE | Extension::WAS_INSTALLED_BY_DEFAULT);
4087
4088 AddMockExternalProvider(provider);
4089 }
4090
4091 ASSERT_EQ(0u, service_->extensions()->size());
[email protected]97d6a5c2013-11-11 23:51:244092 content::WindowedNotificationObserver observer(
[email protected]6d057a0c2013-07-09 21:12:074093 chrome::NOTIFICATION_CRX_INSTALLER_DONE,
[email protected]97d6a5c2013-11-11 23:51:244094 content::NotificationService::AllSources());
4095 service_->CheckForExternalUpdates();
4096 observer.Wait();
[email protected]a7cd28e2012-10-05 21:03:364097
4098 ASSERT_EQ(1u, service_->extensions()->size());
4099 EXPECT_TRUE(service_->GetExtensionById(good_crx, false));
4100 const Extension* extension = service_->GetExtensionById(good_crx, false);
4101 EXPECT_TRUE(extension->from_webstore());
4102 EXPECT_TRUE(extension->was_installed_by_default());
[email protected]a7cd28e2012-10-05 21:03:364103}
4104#endif
4105
[email protected]cd500f72010-06-25 23:44:324106// Tests disabling extensions
[email protected]d9a61e12012-11-14 02:43:474107TEST_F(ExtensionServiceTest, DisableExtension) {
[email protected]eaa7dd182010-12-14 11:09:004108 InitializeEmptyExtensionService();
[email protected]cd500f72010-06-25 23:44:324109
[email protected]8f512c72011-11-22 21:02:504110 InstallCRX(data_dir_.AppendASCII("good.crx"), INSTALL_NEW);
[email protected]8f512c72011-11-22 21:02:504111 EXPECT_TRUE(service_->GetExtensionById(good_crx, true));
4112 EXPECT_TRUE(service_->GetExtensionById(good_crx, false));
[email protected]5fdfa562013-12-27 17:43:594113
4114 ExtensionRegistry* registry = ExtensionRegistry::Get(profile_.get());
4115 EXPECT_EQ(1u, registry->enabled_extensions().size());
4116 EXPECT_EQ(0u, registry->disabled_extensions().size());
4117 EXPECT_EQ(0u, registry->terminated_extensions().size());
4118 EXPECT_EQ(0u, registry->blacklisted_extensions().size());
[email protected]cd500f72010-06-25 23:44:324119
4120 // Disable it.
[email protected]44d62b62012-04-11 00:06:034121 service_->DisableExtension(good_crx, Extension::DISABLE_USER_ACTION);
[email protected]cd500f72010-06-25 23:44:324122
[email protected]8f512c72011-11-22 21:02:504123 EXPECT_TRUE(service_->GetExtensionById(good_crx, true));
4124 EXPECT_FALSE(service_->GetExtensionById(good_crx, false));
[email protected]5fdfa562013-12-27 17:43:594125 EXPECT_EQ(0u, registry->enabled_extensions().size());
4126 EXPECT_EQ(1u, registry->disabled_extensions().size());
4127 EXPECT_EQ(0u, registry->terminated_extensions().size());
4128 EXPECT_EQ(0u, registry->blacklisted_extensions().size());
[email protected]bb1bc9b32013-12-21 03:09:144129}
4130
4131TEST_F(ExtensionServiceTest, TerminateExtension) {
4132 InitializeEmptyExtensionService();
4133
4134 InstallCRX(data_dir_.AppendASCII("good.crx"), INSTALL_NEW);
[email protected]5fdfa562013-12-27 17:43:594135 ExtensionRegistry* registry = ExtensionRegistry::Get(profile_.get());
4136 EXPECT_EQ(1u, registry->enabled_extensions().size());
4137 EXPECT_EQ(0u, registry->disabled_extensions().size());
4138 EXPECT_EQ(0u, registry->terminated_extensions().size());
4139 EXPECT_EQ(0u, registry->blacklisted_extensions().size());
[email protected]bb1bc9b32013-12-21 03:09:144140
4141 TerminateExtension(good_crx);
4142
[email protected]5fdfa562013-12-27 17:43:594143 EXPECT_EQ(0u, registry->enabled_extensions().size());
4144 EXPECT_EQ(0u, registry->disabled_extensions().size());
4145 EXPECT_EQ(1u, registry->terminated_extensions().size());
4146 EXPECT_EQ(0u, registry->blacklisted_extensions().size());
[email protected]fa2416f2011-05-03 08:41:204147}
4148
[email protected]d9a61e12012-11-14 02:43:474149TEST_F(ExtensionServiceTest, DisableTerminatedExtension) {
[email protected]fa2416f2011-05-03 08:41:204150 InitializeEmptyExtensionService();
4151
[email protected]8f512c72011-11-22 21:02:504152 InstallCRX(data_dir_.AppendASCII("good.crx"), INSTALL_NEW);
[email protected]fa2416f2011-05-03 08:41:204153 TerminateExtension(good_crx);
4154 EXPECT_TRUE(service_->GetTerminatedExtension(good_crx));
4155
4156 // Disable it.
[email protected]44d62b62012-04-11 00:06:034157 service_->DisableExtension(good_crx, Extension::DISABLE_USER_ACTION);
[email protected]fa2416f2011-05-03 08:41:204158
4159 EXPECT_FALSE(service_->GetTerminatedExtension(good_crx));
[email protected]8f512c72011-11-22 21:02:504160 EXPECT_TRUE(service_->GetExtensionById(good_crx, true));
[email protected]bb1bc9b32013-12-21 03:09:144161
[email protected]5fdfa562013-12-27 17:43:594162 ExtensionRegistry* registry = ExtensionRegistry::Get(profile_.get());
4163 EXPECT_EQ(0u, registry->enabled_extensions().size());
4164 EXPECT_EQ(1u, registry->disabled_extensions().size());
4165 EXPECT_EQ(0u, registry->terminated_extensions().size());
4166 EXPECT_EQ(0u, registry->blacklisted_extensions().size());
[email protected]cd500f72010-06-25 23:44:324167}
4168
[email protected]d728e002010-12-08 04:46:234169// Tests disabling all extensions (simulating --disable-extensions flag).
[email protected]d9a61e12012-11-14 02:43:474170TEST_F(ExtensionServiceTest, DisableAllExtensions) {
[email protected]eaa7dd182010-12-14 11:09:004171 InitializeEmptyExtensionService();
[email protected]d728e002010-12-08 04:46:234172
[email protected]650b2d52013-02-10 03:41:454173 base::FilePath path = data_dir_.AppendASCII("good.crx");
[email protected]8f512c72011-11-22 21:02:504174 InstallCRX(path, INSTALL_NEW);
[email protected]d728e002010-12-08 04:46:234175
4176 EXPECT_EQ(1u, service_->extensions()->size());
4177 EXPECT_EQ(0u, service_->disabled_extensions()->size());
4178
4179 // Disable extensions.
4180 service_->set_extensions_enabled(false);
[email protected]bb1bc9b32013-12-21 03:09:144181 service_->ReloadExtensionsForTest();
[email protected]d728e002010-12-08 04:46:234182
4183 // There shouldn't be extensions in either list.
4184 EXPECT_EQ(0u, service_->extensions()->size());
4185 EXPECT_EQ(0u, service_->disabled_extensions()->size());
4186
4187 // This shouldn't do anything when all extensions are disabled.
4188 service_->EnableExtension(good_crx);
[email protected]bb1bc9b32013-12-21 03:09:144189 service_->ReloadExtensionsForTest();
[email protected]d728e002010-12-08 04:46:234190
4191 // There still shouldn't be extensions in either list.
4192 EXPECT_EQ(0u, service_->extensions()->size());
4193 EXPECT_EQ(0u, service_->disabled_extensions()->size());
4194
4195 // And then re-enable the extensions.
4196 service_->set_extensions_enabled(true);
[email protected]bb1bc9b32013-12-21 03:09:144197 service_->ReloadExtensionsForTest();
[email protected]d728e002010-12-08 04:46:234198
4199 EXPECT_EQ(1u, service_->extensions()->size());
4200 EXPECT_EQ(0u, service_->disabled_extensions()->size());
4201}
4202
[email protected]8f512c72011-11-22 21:02:504203// Tests reloading extensions.
[email protected]d9a61e12012-11-14 02:43:474204TEST_F(ExtensionServiceTest, ReloadExtensions) {
[email protected]eaa7dd182010-12-14 11:09:004205 InitializeEmptyExtensionService();
[email protected]cd500f72010-06-25 23:44:324206
4207 // Simple extension that should install without error.
[email protected]650b2d52013-02-10 03:41:454208 base::FilePath path = data_dir_.AppendASCII("good.crx");
[email protected]26367b62012-10-04 23:03:324209 InstallCRX(path, INSTALL_NEW,
4210 Extension::FROM_WEBSTORE | Extension::WAS_INSTALLED_BY_DEFAULT);
[email protected]cd500f72010-06-25 23:44:324211 const char* extension_id = good_crx;
[email protected]44d62b62012-04-11 00:06:034212 service_->DisableExtension(extension_id, Extension::DISABLE_USER_ACTION);
[email protected]cd500f72010-06-25 23:44:324213
4214 EXPECT_EQ(0u, service_->extensions()->size());
4215 EXPECT_EQ(1u, service_->disabled_extensions()->size());
4216
[email protected]bb1bc9b32013-12-21 03:09:144217 service_->ReloadExtensionsForTest();
[email protected]cd500f72010-06-25 23:44:324218
[email protected]26367b62012-10-04 23:03:324219 // The creation flags should not change when reloading the extension.
4220 const Extension* extension = service_->GetExtensionById(good_crx, true);
4221 EXPECT_TRUE(extension->from_webstore());
4222 EXPECT_TRUE(extension->was_installed_by_default());
4223 EXPECT_FALSE(extension->from_bookmark());
4224
[email protected]cd500f72010-06-25 23:44:324225 // Extension counts shouldn't change.
4226 EXPECT_EQ(0u, service_->extensions()->size());
4227 EXPECT_EQ(1u, service_->disabled_extensions()->size());
4228
4229 service_->EnableExtension(extension_id);
4230
4231 EXPECT_EQ(1u, service_->extensions()->size());
4232 EXPECT_EQ(0u, service_->disabled_extensions()->size());
4233
[email protected]5a73f902010-06-30 02:29:414234 // Need to clear |loaded_| manually before reloading as the
4235 // EnableExtension() call above inserted into it and
4236 // UnloadAllExtensions() doesn't send out notifications.
4237 loaded_.clear();
[email protected]bb1bc9b32013-12-21 03:09:144238 service_->ReloadExtensionsForTest();
[email protected]cd500f72010-06-25 23:44:324239
4240 // Extension counts shouldn't change.
4241 EXPECT_EQ(1u, service_->extensions()->size());
4242 EXPECT_EQ(0u, service_->disabled_extensions()->size());
4243}
4244
[email protected]2a947202013-03-06 04:58:054245// Tests reloading an extension.
4246TEST_F(ExtensionServiceTest, ReloadExtension) {
4247 InitializeEmptyExtensionService();
[email protected]98b6d942013-11-10 00:34:074248 InitializeProcessManager();
[email protected]2a947202013-03-06 04:58:054249
4250 // Simple extension that should install without error.
4251 const char* extension_id = "behllobkkfkfnphdnhnkndlbkcpglgmj";
4252 base::FilePath ext = data_dir_
4253 .AppendASCII("good")
4254 .AppendASCII("Extensions")
4255 .AppendASCII(extension_id)
4256 .AppendASCII("1.0.0.0");
4257 extensions::UnpackedInstaller::Create(service_)->Load(ext);
[email protected]eedc1c752013-08-09 18:51:554258 base::RunLoop().RunUntilIdle();
[email protected]2a947202013-03-06 04:58:054259
4260 EXPECT_EQ(1u, service_->extensions()->size());
4261 EXPECT_EQ(0u, service_->disabled_extensions()->size());
4262
4263 service_->ReloadExtension(extension_id);
4264
4265 // Extension should be disabled now, waiting to be reloaded.
4266 EXPECT_EQ(0u, service_->extensions()->size());
4267 EXPECT_EQ(1u, service_->disabled_extensions()->size());
4268 EXPECT_EQ(Extension::DISABLE_RELOAD,
4269 service_->extension_prefs()->GetDisableReasons(extension_id));
4270
[email protected]a5768512013-04-12 19:35:354271 // Reloading again should not crash.
[email protected]2a947202013-03-06 04:58:054272 service_->ReloadExtension(extension_id);
4273
4274 // Finish reloading
[email protected]eedc1c752013-08-09 18:51:554275 base::RunLoop().RunUntilIdle();
[email protected]2a947202013-03-06 04:58:054276
4277 // Extension should be enabled again.
4278 EXPECT_EQ(1u, service_->extensions()->size());
4279 EXPECT_EQ(0u, service_->disabled_extensions()->size());
4280}
4281
[email protected]d9a61e12012-11-14 02:43:474282TEST_F(ExtensionServiceTest, UninstallExtension) {
[email protected]eaa7dd182010-12-14 11:09:004283 InitializeEmptyExtensionService();
[email protected]8f512c72011-11-22 21:02:504284 InstallCRX(data_dir_.AppendASCII("good.crx"), INSTALL_NEW);
[email protected]65187152012-06-02 13:14:144285 EXPECT_EQ(1u, service_->extensions()->size());
[email protected]fa2416f2011-05-03 08:41:204286 UninstallExtension(good_crx, false);
[email protected]65187152012-06-02 13:14:144287 EXPECT_EQ(0u, service_->extensions()->size());
[email protected]fa2416f2011-05-03 08:41:204288}
[email protected]631cf822009-05-15 07:01:254289
[email protected]d9a61e12012-11-14 02:43:474290TEST_F(ExtensionServiceTest, UninstallTerminatedExtension) {
[email protected]fa2416f2011-05-03 08:41:204291 InitializeEmptyExtensionService();
[email protected]8f512c72011-11-22 21:02:504292 InstallCRX(data_dir_.AppendASCII("good.crx"), INSTALL_NEW);
[email protected]fa2416f2011-05-03 08:41:204293 TerminateExtension(good_crx);
4294 UninstallExtension(good_crx, false);
[email protected]631cf822009-05-15 07:01:254295}
4296
[email protected]6aeac8342010-10-01 20:21:184297// Tests the uninstaller helper.
[email protected]d9a61e12012-11-14 02:43:474298TEST_F(ExtensionServiceTest, UninstallExtensionHelper) {
[email protected]eaa7dd182010-12-14 11:09:004299 InitializeEmptyExtensionService();
[email protected]8f512c72011-11-22 21:02:504300 InstallCRX(data_dir_.AppendASCII("good.crx"), INSTALL_NEW);
[email protected]fa2416f2011-05-03 08:41:204301 UninstallExtension(good_crx, true);
4302}
[email protected]6aeac8342010-10-01 20:21:184303
[email protected]d9a61e12012-11-14 02:43:474304TEST_F(ExtensionServiceTest, UninstallExtensionHelperTerminated) {
[email protected]fa2416f2011-05-03 08:41:204305 InitializeEmptyExtensionService();
[email protected]8f512c72011-11-22 21:02:504306 InstallCRX(data_dir_.AppendASCII("good.crx"), INSTALL_NEW);
[email protected]fa2416f2011-05-03 08:41:204307 TerminateExtension(good_crx);
4308 UninstallExtension(good_crx, true);
[email protected]6aeac8342010-10-01 20:21:184309}
4310
[email protected]98270432012-09-11 20:51:244311// An extension disabled because of unsupported requirements should re-enabled
4312// if updated to a version with supported requirements as long as there are no
4313// other disable reasons.
[email protected]d9a61e12012-11-14 02:43:474314TEST_F(ExtensionServiceTest, UpgradingRequirementsEnabled) {
[email protected]98270432012-09-11 20:51:244315 InitializeEmptyExtensionService();
4316 BlackListWebGL();
4317
[email protected]650b2d52013-02-10 03:41:454318 base::FilePath path = data_dir_.AppendASCII("requirements");
4319 base::FilePath pem_path = data_dir_.AppendASCII("requirements")
[email protected]98270432012-09-11 20:51:244320 .AppendASCII("v1_good.pem");
4321 const Extension* extension_v1 = PackAndInstallCRX(path.AppendASCII("v1_good"),
4322 pem_path,
4323 INSTALL_NEW);
4324 std::string id = extension_v1->id();
4325 EXPECT_TRUE(service_->IsExtensionEnabled(id));
4326
[email protected]650b2d52013-02-10 03:41:454327 base::FilePath v2_bad_requirements_crx = GetTemporaryFile();
[email protected]a17dfcf2012-12-30 02:07:094328
4329 PackCRX(path.AppendASCII("v2_bad_requirements"),
4330 pem_path,
4331 v2_bad_requirements_crx);
4332 UpdateExtension(id, v2_bad_requirements_crx, INSTALLED);
[email protected]98270432012-09-11 20:51:244333 EXPECT_FALSE(service_->IsExtensionEnabled(id));
4334
[email protected]650b2d52013-02-10 03:41:454335 base::FilePath v3_good_crx = GetTemporaryFile();
[email protected]a17dfcf2012-12-30 02:07:094336
4337 PackCRX(path.AppendASCII("v3_good"), pem_path, v3_good_crx);
4338 UpdateExtension(id, v3_good_crx, ENABLED);
[email protected]98270432012-09-11 20:51:244339 EXPECT_TRUE(service_->IsExtensionEnabled(id));
4340}
4341
4342// Extensions disabled through user action should stay disabled.
[email protected]d9a61e12012-11-14 02:43:474343TEST_F(ExtensionServiceTest, UpgradingRequirementsDisabled) {
[email protected]98270432012-09-11 20:51:244344 InitializeEmptyExtensionService();
4345 BlackListWebGL();
4346
[email protected]650b2d52013-02-10 03:41:454347 base::FilePath path = data_dir_.AppendASCII("requirements");
4348 base::FilePath pem_path = data_dir_.AppendASCII("requirements")
[email protected]98270432012-09-11 20:51:244349 .AppendASCII("v1_good.pem");
4350 const Extension* extension_v1 = PackAndInstallCRX(path.AppendASCII("v1_good"),
4351 pem_path,
4352 INSTALL_NEW);
4353 std::string id = extension_v1->id();
4354 service_->DisableExtension(id, Extension::DISABLE_USER_ACTION);
4355 EXPECT_FALSE(service_->IsExtensionEnabled(id));
4356
[email protected]650b2d52013-02-10 03:41:454357 base::FilePath v2_bad_requirements_crx = GetTemporaryFile();
[email protected]a17dfcf2012-12-30 02:07:094358
4359 PackCRX(path.AppendASCII("v2_bad_requirements"),
4360 pem_path,
4361 v2_bad_requirements_crx);
4362 UpdateExtension(id, v2_bad_requirements_crx, INSTALLED);
[email protected]98270432012-09-11 20:51:244363 EXPECT_FALSE(service_->IsExtensionEnabled(id));
4364
[email protected]650b2d52013-02-10 03:41:454365 base::FilePath v3_good_crx = GetTemporaryFile();
[email protected]a17dfcf2012-12-30 02:07:094366
4367 PackCRX(path.AppendASCII("v3_good"), pem_path, v3_good_crx);
4368 UpdateExtension(id, v3_good_crx, INSTALLED);
[email protected]98270432012-09-11 20:51:244369 EXPECT_FALSE(service_->IsExtensionEnabled(id));
4370}
4371
4372// The extension should not re-enabled because it was disabled from a
4373// permission increase.
[email protected]d9a61e12012-11-14 02:43:474374TEST_F(ExtensionServiceTest, UpgradingRequirementsPermissions) {
[email protected]98270432012-09-11 20:51:244375 InitializeEmptyExtensionService();
4376 BlackListWebGL();
4377
[email protected]650b2d52013-02-10 03:41:454378 base::FilePath path = data_dir_.AppendASCII("requirements");
4379 base::FilePath pem_path = data_dir_.AppendASCII("requirements")
[email protected]98270432012-09-11 20:51:244380 .AppendASCII("v1_good.pem");
4381 const Extension* extension_v1 = PackAndInstallCRX(path.AppendASCII("v1_good"),
4382 pem_path,
4383 INSTALL_NEW);
4384 std::string id = extension_v1->id();
4385 EXPECT_TRUE(service_->IsExtensionEnabled(id));
4386
[email protected]650b2d52013-02-10 03:41:454387 base::FilePath v2_bad_requirements_and_permissions_crx = GetTemporaryFile();
[email protected]a17dfcf2012-12-30 02:07:094388
4389 PackCRX(path.AppendASCII("v2_bad_requirements_and_permissions"),
4390 pem_path,
4391 v2_bad_requirements_and_permissions_crx);
4392 UpdateExtension(id, v2_bad_requirements_and_permissions_crx, INSTALLED);
[email protected]98270432012-09-11 20:51:244393 EXPECT_FALSE(service_->IsExtensionEnabled(id));
4394
[email protected]650b2d52013-02-10 03:41:454395 base::FilePath v3_bad_permissions_crx = GetTemporaryFile();
[email protected]a17dfcf2012-12-30 02:07:094396
4397 PackCRX(path.AppendASCII("v3_bad_permissions"),
4398 pem_path,
4399 v3_bad_permissions_crx);
4400 UpdateExtension(id, v3_bad_permissions_crx, INSTALLED);
[email protected]98270432012-09-11 20:51:244401 EXPECT_FALSE(service_->IsExtensionEnabled(id));
4402}
4403
4404// Unpacked extensions are not allowed to be installed if they have unsupported
4405// requirements.
[email protected]d9a61e12012-11-14 02:43:474406TEST_F(ExtensionServiceTest, UnpackedRequirements) {
[email protected]98270432012-09-11 20:51:244407 InitializeEmptyExtensionService();
4408 BlackListWebGL();
4409
[email protected]650b2d52013-02-10 03:41:454410 base::FilePath path = data_dir_.AppendASCII("requirements")
[email protected]98270432012-09-11 20:51:244411 .AppendASCII("v2_bad_requirements");
4412 extensions::UnpackedInstaller::Create(service_)->Load(path);
[email protected]eedc1c752013-08-09 18:51:554413 base::RunLoop().RunUntilIdle();
[email protected]98270432012-09-11 20:51:244414 EXPECT_EQ(1u, GetErrors().size());
4415 EXPECT_EQ(0u, service_->extensions()->size());
4416}
4417
[email protected]c4148a72011-08-09 23:04:204418class ExtensionCookieCallback {
4419 public:
4420 ExtensionCookieCallback()
[email protected]a5768512013-04-12 19:35:354421 : result_(false),
[email protected]b3a25092013-05-28 22:08:164422 weak_factory_(base::MessageLoop::current()) {}
[email protected]c4148a72011-08-09 23:04:204423
4424 void SetCookieCallback(bool result) {
[email protected]b3a25092013-05-28 22:08:164425 base::MessageLoop::current()->PostTask(FROM_HERE,
4426 base::Bind(&base::MessageLoop::Quit, weak_factory_.GetWeakPtr()));
[email protected]c4148a72011-08-09 23:04:204427 result_ = result;
4428 }
4429
4430 void GetAllCookiesCallback(const net::CookieList& list) {
[email protected]b3a25092013-05-28 22:08:164431 base::MessageLoop::current()->PostTask(FROM_HERE,
4432 base::Bind(&base::MessageLoop::Quit, weak_factory_.GetWeakPtr()));
[email protected]c4148a72011-08-09 23:04:204433 list_ = list;
4434 }
4435 net::CookieList list_;
4436 bool result_;
[email protected]b3a25092013-05-28 22:08:164437 base::WeakPtrFactory<base::MessageLoop> weak_factory_;
[email protected]c4148a72011-08-09 23:04:204438};
4439
[email protected]0d6ec3a72011-09-02 02:09:434440// Verifies extension state is removed upon uninstall.
[email protected]d9a61e12012-11-14 02:43:474441TEST_F(ExtensionServiceTest, ClearExtensionData) {
[email protected]eaa7dd182010-12-14 11:09:004442 InitializeEmptyExtensionService();
[email protected]c4148a72011-08-09 23:04:204443 ExtensionCookieCallback callback;
[email protected]c10da4b02010-03-25 14:38:324444
4445 // Load a test extension.
[email protected]650b2d52013-02-10 03:41:454446 base::FilePath path = data_dir_;
[email protected]c10da4b02010-03-25 14:38:324447 path = path.AppendASCII("good.crx");
[email protected]8f512c72011-11-22 21:02:504448 const Extension* extension = InstallCRX(path, INSTALL_NEW);
[email protected]c10da4b02010-03-25 14:38:324449 ASSERT_TRUE(extension);
4450 GURL ext_url(extension->url());
[email protected]2a9644c2013-06-19 06:19:344451 std::string origin_id = webkit_database::GetIdentifierFromOrigin(ext_url);
[email protected]c10da4b02010-03-25 14:38:324452
4453 // Set a cookie for the extension.
[email protected]a5768512013-04-12 19:35:354454 net::CookieMonster* cookie_monster =
[email protected]33ad6ce92013-08-27 14:39:084455 profile_->GetRequestContextForExtensions()->GetURLRequestContext()->
4456 cookie_store()->GetCookieMonster();
[email protected]c10da4b02010-03-25 14:38:324457 ASSERT_TRUE(cookie_monster);
4458 net::CookieOptions options;
[email protected]c4148a72011-08-09 23:04:204459 cookie_monster->SetCookieWithOptionsAsync(
[email protected]a5768512013-04-12 19:35:354460 ext_url, "dummy=value", options,
4461 base::Bind(&ExtensionCookieCallback::SetCookieCallback,
4462 base::Unretained(&callback)));
[email protected]eedc1c752013-08-09 18:51:554463 base::RunLoop().RunUntilIdle();
[email protected]c4148a72011-08-09 23:04:204464 EXPECT_TRUE(callback.result_);
4465
4466 cookie_monster->GetAllCookiesForURLAsync(
4467 ext_url,
4468 base::Bind(&ExtensionCookieCallback::GetAllCookiesCallback,
4469 base::Unretained(&callback)));
[email protected]eedc1c752013-08-09 18:51:554470 base::RunLoop().RunUntilIdle();
[email protected]c4148a72011-08-09 23:04:204471 EXPECT_EQ(1U, callback.list_.size());
[email protected]c10da4b02010-03-25 14:38:324472
4473 // Open a database.
[email protected]55eb70e762012-02-20 17:38:394474 webkit_database::DatabaseTracker* db_tracker =
[email protected]5c8e67c2012-08-29 00:48:524475 BrowserContext::GetDefaultStoragePartition(profile_.get())->
4476 GetDatabaseTracker();
[email protected]04338722013-12-24 23:18:054477 base::string16 db_name = base::UTF8ToUTF16("db");
4478 base::string16 description = base::UTF8ToUTF16("db_description");
[email protected]c10da4b02010-03-25 14:38:324479 int64 size;
[email protected]7c5f2ec2011-05-26 19:15:264480 db_tracker->DatabaseOpened(origin_id, db_name, description, 1, &size);
[email protected]c10da4b02010-03-25 14:38:324481 db_tracker->DatabaseClosed(origin_id, db_name);
4482 std::vector<webkit_database::OriginInfo> origins;
4483 db_tracker->GetAllOriginsInfo(&origins);
4484 EXPECT_EQ(1U, origins.size());
[email protected]5e301592013-06-18 06:36:054485 EXPECT_EQ(origin_id, origins[0].GetOriginIdentifier());
[email protected]c10da4b02010-03-25 14:38:324486
[email protected]98823c682012-06-11 21:18:244487 // Create local storage. We only simulate this by creating the backing files.
4488 // Note: This test depends on details of how the dom_storage library
4489 // stores data in the host file system.
[email protected]650b2d52013-02-10 03:41:454490 base::FilePath lso_dir_path =
4491 profile_->GetPath().AppendASCII("Local Storage");
[email protected]5e301592013-06-18 06:36:054492 base::FilePath lso_file_path = lso_dir_path.AppendASCII(origin_id)
4493 .AddExtension(FILE_PATH_LITERAL(".localstorage"));
[email protected]426d1c92013-12-03 20:08:544494 EXPECT_TRUE(base::CreateDirectory(lso_dir_path));
[email protected]98823c682012-06-11 21:18:244495 EXPECT_EQ(0, file_util::WriteFile(lso_file_path, NULL, 0));
[email protected]7567484142013-07-11 17:36:074496 EXPECT_TRUE(base::PathExists(lso_file_path));
[email protected]c10da4b02010-03-25 14:38:324497
[email protected]ab308092011-08-25 23:37:194498 // Create indexed db. Similarly, it is enough to only simulate this by
4499 // creating the directory on the disk.
[email protected]b1b502e2012-09-16 07:31:434500 IndexedDBContext* idb_context =
4501 BrowserContext::GetDefaultStoragePartition(profile_.get())->
4502 GetIndexedDBContext();
[email protected]89acda82013-06-25 20:52:504503 idb_context->SetTaskRunnerForTesting(
[email protected]198b5902013-06-27 10:36:114504 base::MessageLoop::current()->message_loop_proxy().get());
[email protected]650b2d52013-02-10 03:41:454505 base::FilePath idb_path = idb_context->GetFilePathForTesting(origin_id);
[email protected]426d1c92013-12-03 20:08:544506 EXPECT_TRUE(base::CreateDirectory(idb_path));
[email protected]dcd16612013-07-15 20:18:094507 EXPECT_TRUE(base::DirectoryExists(idb_path));
[email protected]e1dcf922010-11-22 19:12:124508
[email protected]c10da4b02010-03-25 14:38:324509 // Uninstall the extension.
[email protected]d6ebc9792011-04-07 18:18:334510 service_->UninstallExtension(good_crx, false, NULL);
[email protected]eedc1c752013-08-09 18:51:554511 base::RunLoop().RunUntilIdle();
[email protected]c10da4b02010-03-25 14:38:324512
4513 // Check that the cookie is gone.
[email protected]c4148a72011-08-09 23:04:204514 cookie_monster->GetAllCookiesForURLAsync(
4515 ext_url,
4516 base::Bind(&ExtensionCookieCallback::GetAllCookiesCallback,
4517 base::Unretained(&callback)));
[email protected]eedc1c752013-08-09 18:51:554518 base::RunLoop().RunUntilIdle();
[email protected]c4148a72011-08-09 23:04:204519 EXPECT_EQ(0U, callback.list_.size());
[email protected]c10da4b02010-03-25 14:38:324520
4521 // The database should have vanished as well.
4522 origins.clear();
4523 db_tracker->GetAllOriginsInfo(&origins);
4524 EXPECT_EQ(0U, origins.size());
4525
4526 // Check that the LSO file has been removed.
[email protected]7567484142013-07-11 17:36:074527 EXPECT_FALSE(base::PathExists(lso_file_path));
[email protected]e1dcf922010-11-22 19:12:124528
4529 // Check if the indexed db has disappeared too.
[email protected]dcd16612013-07-15 20:18:094530 EXPECT_FALSE(base::DirectoryExists(idb_path));
[email protected]c10da4b02010-03-25 14:38:324531}
4532
[email protected]0d6ec3a72011-09-02 02:09:434533// Verifies app state is removed upon uninstall.
[email protected]d9a61e12012-11-14 02:43:474534TEST_F(ExtensionServiceTest, ClearAppData) {
[email protected]0d6ec3a72011-09-02 02:09:434535 InitializeEmptyExtensionService();
[email protected]0d6ec3a72011-09-02 02:09:434536 ExtensionCookieCallback callback;
4537
4538 int pref_count = 0;
4539
4540 // Install app1 with unlimited storage.
[email protected]8f512c72011-11-22 21:02:504541 const Extension* extension =
4542 PackAndInstallCRX(data_dir_.AppendASCII("app1"), INSTALL_NEW);
[email protected]0d6ec3a72011-09-02 02:09:434543 ValidatePrefKeyCount(++pref_count);
4544 ASSERT_EQ(1u, service_->extensions()->size());
[email protected]0d6ec3a72011-09-02 02:09:434545 const std::string id1 = extension->id();
4546 EXPECT_TRUE(extension->HasAPIPermission(
[email protected]c2e66e12012-06-27 06:27:064547 APIPermission::kUnlimitedStorage));
[email protected]6b414c232013-06-05 07:53:344548 const GURL origin1(
4549 extensions::AppLaunchInfo::GetFullLaunchURL(extension).GetOrigin());
[email protected]0d6ec3a72011-09-02 02:09:434550 EXPECT_TRUE(profile_->GetExtensionSpecialStoragePolicy()->
4551 IsStorageUnlimited(origin1));
[email protected]2a9644c2013-06-19 06:19:344552 std::string origin_id = webkit_database::GetIdentifierFromOrigin(origin1);
[email protected]0d6ec3a72011-09-02 02:09:434553
4554 // Install app2 from the same origin with unlimited storage.
[email protected]8f512c72011-11-22 21:02:504555 extension = PackAndInstallCRX(data_dir_.AppendASCII("app2"), INSTALL_NEW);
[email protected]0d6ec3a72011-09-02 02:09:434556 ValidatePrefKeyCount(++pref_count);
4557 ASSERT_EQ(2u, service_->extensions()->size());
[email protected]0d6ec3a72011-09-02 02:09:434558 const std::string id2 = extension->id();
4559 EXPECT_TRUE(extension->HasAPIPermission(
[email protected]c2e66e12012-06-27 06:27:064560 APIPermission::kUnlimitedStorage));
[email protected]0d6ec3a72011-09-02 02:09:434561 EXPECT_TRUE(extension->web_extent().MatchesURL(
[email protected]6b414c232013-06-05 07:53:344562 extensions::AppLaunchInfo::GetFullLaunchURL(extension)));
4563 const GURL origin2(
4564 extensions::AppLaunchInfo::GetFullLaunchURL(extension).GetOrigin());
[email protected]0d6ec3a72011-09-02 02:09:434565 EXPECT_EQ(origin1, origin2);
4566 EXPECT_TRUE(profile_->GetExtensionSpecialStoragePolicy()->
4567 IsStorageUnlimited(origin2));
4568
4569 // Set a cookie for the extension.
[email protected]a5768512013-04-12 19:35:354570 net::CookieMonster* cookie_monster =
[email protected]33ad6ce92013-08-27 14:39:084571 profile_->GetRequestContext()->GetURLRequestContext()->
4572 cookie_store()->GetCookieMonster();
[email protected]0d6ec3a72011-09-02 02:09:434573 ASSERT_TRUE(cookie_monster);
4574 net::CookieOptions options;
4575 cookie_monster->SetCookieWithOptionsAsync(
4576 origin1, "dummy=value", options,
4577 base::Bind(&ExtensionCookieCallback::SetCookieCallback,
4578 base::Unretained(&callback)));
[email protected]eedc1c752013-08-09 18:51:554579 base::RunLoop().RunUntilIdle();
[email protected]0d6ec3a72011-09-02 02:09:434580 EXPECT_TRUE(callback.result_);
4581
4582 cookie_monster->GetAllCookiesForURLAsync(
4583 origin1,
4584 base::Bind(&ExtensionCookieCallback::GetAllCookiesCallback,
4585 base::Unretained(&callback)));
[email protected]eedc1c752013-08-09 18:51:554586 base::RunLoop().RunUntilIdle();
[email protected]0d6ec3a72011-09-02 02:09:434587 EXPECT_EQ(1U, callback.list_.size());
4588
4589 // Open a database.
[email protected]55eb70e762012-02-20 17:38:394590 webkit_database::DatabaseTracker* db_tracker =
[email protected]5c8e67c2012-08-29 00:48:524591 BrowserContext::GetDefaultStoragePartition(profile_.get())->
4592 GetDatabaseTracker();
[email protected]04338722013-12-24 23:18:054593 base::string16 db_name = base::UTF8ToUTF16("db");
4594 base::string16 description = base::UTF8ToUTF16("db_description");
[email protected]0d6ec3a72011-09-02 02:09:434595 int64 size;
4596 db_tracker->DatabaseOpened(origin_id, db_name, description, 1, &size);
4597 db_tracker->DatabaseClosed(origin_id, db_name);
4598 std::vector<webkit_database::OriginInfo> origins;
4599 db_tracker->GetAllOriginsInfo(&origins);
4600 EXPECT_EQ(1U, origins.size());
[email protected]5e301592013-06-18 06:36:054601 EXPECT_EQ(origin_id, origins[0].GetOriginIdentifier());
[email protected]0d6ec3a72011-09-02 02:09:434602
[email protected]98823c682012-06-11 21:18:244603 // Create local storage. We only simulate this by creating the backing files.
4604 // Note: This test depends on details of how the dom_storage library
4605 // stores data in the host file system.
[email protected]650b2d52013-02-10 03:41:454606 base::FilePath lso_dir_path =
4607 profile_->GetPath().AppendASCII("Local Storage");
[email protected]5e301592013-06-18 06:36:054608 base::FilePath lso_file_path = lso_dir_path.AppendASCII(origin_id)
4609 .AddExtension(FILE_PATH_LITERAL(".localstorage"));
[email protected]426d1c92013-12-03 20:08:544610 EXPECT_TRUE(base::CreateDirectory(lso_dir_path));
[email protected]98823c682012-06-11 21:18:244611 EXPECT_EQ(0, file_util::WriteFile(lso_file_path, NULL, 0));
[email protected]7567484142013-07-11 17:36:074612 EXPECT_TRUE(base::PathExists(lso_file_path));
[email protected]0d6ec3a72011-09-02 02:09:434613
4614 // Create indexed db. Similarly, it is enough to only simulate this by
4615 // creating the directory on the disk.
[email protected]b1b502e2012-09-16 07:31:434616 IndexedDBContext* idb_context =
4617 BrowserContext::GetDefaultStoragePartition(profile_.get())->
4618 GetIndexedDBContext();
[email protected]89acda82013-06-25 20:52:504619 idb_context->SetTaskRunnerForTesting(
[email protected]198b5902013-06-27 10:36:114620 base::MessageLoop::current()->message_loop_proxy().get());
[email protected]650b2d52013-02-10 03:41:454621 base::FilePath idb_path = idb_context->GetFilePathForTesting(origin_id);
[email protected]426d1c92013-12-03 20:08:544622 EXPECT_TRUE(base::CreateDirectory(idb_path));
[email protected]dcd16612013-07-15 20:18:094623 EXPECT_TRUE(base::DirectoryExists(idb_path));
[email protected]0d6ec3a72011-09-02 02:09:434624
4625 // Uninstall one of them, unlimited storage should still be granted
4626 // to the origin.
4627 UninstallExtension(id1, false);
4628 EXPECT_EQ(1u, service_->extensions()->size());
4629 EXPECT_TRUE(profile_->GetExtensionSpecialStoragePolicy()->
4630 IsStorageUnlimited(origin1));
4631
4632 // Check that the cookie is still there.
4633 cookie_monster->GetAllCookiesForURLAsync(
4634 origin1,
4635 base::Bind(&ExtensionCookieCallback::GetAllCookiesCallback,
4636 base::Unretained(&callback)));
[email protected]eedc1c752013-08-09 18:51:554637 base::RunLoop().RunUntilIdle();
[email protected]0d6ec3a72011-09-02 02:09:434638 EXPECT_EQ(1U, callback.list_.size());
4639
4640 // Now uninstall the other. Storage should be cleared for the apps.
4641 UninstallExtension(id2, false);
4642 EXPECT_EQ(0u, service_->extensions()->size());
4643 EXPECT_FALSE(profile_->GetExtensionSpecialStoragePolicy()->
4644 IsStorageUnlimited(origin1));
4645
4646 // Check that the cookie is gone.
4647 cookie_monster->GetAllCookiesForURLAsync(
4648 origin1,
4649 base::Bind(&ExtensionCookieCallback::GetAllCookiesCallback,
4650 base::Unretained(&callback)));
[email protected]eedc1c752013-08-09 18:51:554651 base::RunLoop().RunUntilIdle();
[email protected]0d6ec3a72011-09-02 02:09:434652 EXPECT_EQ(0U, callback.list_.size());
4653
4654 // The database should have vanished as well.
4655 origins.clear();
4656 db_tracker->GetAllOriginsInfo(&origins);
4657 EXPECT_EQ(0U, origins.size());
4658
4659 // Check that the LSO file has been removed.
[email protected]7567484142013-07-11 17:36:074660 EXPECT_FALSE(base::PathExists(lso_file_path));
[email protected]0d6ec3a72011-09-02 02:09:434661
4662 // Check if the indexed db has disappeared too.
[email protected]dcd16612013-07-15 20:18:094663 EXPECT_FALSE(base::DirectoryExists(idb_path));
[email protected]0d6ec3a72011-09-02 02:09:434664}
4665
[email protected]894bb502009-05-21 22:39:574666// Tests loading single extensions (like --load-extension)
[email protected]e3f610b2013-05-03 02:22:384667// Flaky crashes. http://crbug.com/231806
4668TEST_F(ExtensionServiceTest, DISABLED_LoadExtension) {
[email protected]eaa7dd182010-12-14 11:09:004669 InitializeEmptyExtensionService();
[email protected]3cf4f0992009-02-03 23:00:304670
[email protected]650b2d52013-02-10 03:41:454671 base::FilePath ext1 = data_dir_
[email protected]a9b00ac2009-06-25 21:03:234672 .AppendASCII("good")
4673 .AppendASCII("Extensions")
[email protected]5a2721f62009-06-13 07:08:204674 .AppendASCII("behllobkkfkfnphdnhnkndlbkcpglgmj")
4675 .AppendASCII("1.0.0.0");
[email protected]d8c8f25f2011-11-02 18:18:014676 extensions::UnpackedInstaller::Create(service_)->Load(ext1);
[email protected]eedc1c752013-08-09 18:51:554677 base::RunLoop().RunUntilIdle();
[email protected]bb28e062009-02-27 17:19:184678 EXPECT_EQ(0u, GetErrors().size());
[email protected]894bb502009-05-21 22:39:574679 ASSERT_EQ(1u, loaded_.size());
[email protected]12075d12013-02-27 05:38:054680 EXPECT_EQ(Manifest::UNPACKED, loaded_[0]->location());
[email protected]9f1087e2009-06-15 17:29:324681 EXPECT_EQ(1u, service_->extensions()->size());
[email protected]3cf4f0992009-02-03 23:00:304682
[email protected]e8c729a2010-03-09 19:55:194683 ValidatePrefKeyCount(1);
[email protected]25b34332009-06-05 21:53:194684
[email protected]650b2d52013-02-10 03:41:454685 base::FilePath no_manifest = data_dir_
[email protected]a9b00ac2009-06-25 21:03:234686 .AppendASCII("bad")
[email protected]93fd78f42009-07-10 16:43:174687 // .AppendASCII("Extensions")
[email protected]a9b00ac2009-06-25 21:03:234688 .AppendASCII("cccccccccccccccccccccccccccccccc")
4689 .AppendASCII("1");
[email protected]d8c8f25f2011-11-02 18:18:014690 extensions::UnpackedInstaller::Create(service_)->Load(no_manifest);
[email protected]eedc1c752013-08-09 18:51:554691 base::RunLoop().RunUntilIdle();
[email protected]bb28e062009-02-27 17:19:184692 EXPECT_EQ(1u, GetErrors().size());
[email protected]894bb502009-05-21 22:39:574693 ASSERT_EQ(1u, loaded_.size());
[email protected]9f1087e2009-06-15 17:29:324694 EXPECT_EQ(1u, service_->extensions()->size());
[email protected]25b34332009-06-05 21:53:194695
4696 // Test uninstall.
[email protected]894bb502009-05-21 22:39:574697 std::string id = loaded_[0]->id();
4698 EXPECT_FALSE(unloaded_id_.length());
[email protected]d6ebc9792011-04-07 18:18:334699 service_->UninstallExtension(id, false, NULL);
[email protected]eedc1c752013-08-09 18:51:554700 base::RunLoop().RunUntilIdle();
[email protected]894bb502009-05-21 22:39:574701 EXPECT_EQ(id, unloaded_id_);
[email protected]9f1087e2009-06-15 17:29:324702 ASSERT_EQ(0u, loaded_.size());
4703 EXPECT_EQ(0u, service_->extensions()->size());
[email protected]3cf4f0992009-02-03 23:00:304704}
[email protected]0b344962009-03-31 04:21:454705
[email protected]894bb502009-05-21 22:39:574706// Tests that we generate IDs when they are not specified in the manifest for
4707// --load-extension.
[email protected]d9a61e12012-11-14 02:43:474708TEST_F(ExtensionServiceTest, GenerateID) {
[email protected]eaa7dd182010-12-14 11:09:004709 InitializeEmptyExtensionService();
[email protected]fbcc40302009-06-12 20:45:454710
[email protected]650b2d52013-02-10 03:41:454711 base::FilePath no_id_ext = data_dir_.AppendASCII("no_id");
[email protected]d8c8f25f2011-11-02 18:18:014712 extensions::UnpackedInstaller::Create(service_)->Load(no_id_ext);
[email protected]eedc1c752013-08-09 18:51:554713 base::RunLoop().RunUntilIdle();
[email protected]0b344962009-03-31 04:21:454714 EXPECT_EQ(0u, GetErrors().size());
[email protected]894bb502009-05-21 22:39:574715 ASSERT_EQ(1u, loaded_.size());
[email protected]84ac7f32009-10-06 06:17:544716 ASSERT_TRUE(Extension::IdIsValid(loaded_[0]->id()));
[email protected]12075d12013-02-27 05:38:054717 EXPECT_EQ(loaded_[0]->location(), Manifest::UNPACKED);
[email protected]0b344962009-03-31 04:21:454718
[email protected]e8c729a2010-03-09 19:55:194719 ValidatePrefKeyCount(1);
[email protected]25b34332009-06-05 21:53:194720
[email protected]84ac7f32009-10-06 06:17:544721 std::string previous_id = loaded_[0]->id();
4722
4723 // If we reload the same path, we should get the same extension ID.
[email protected]d8c8f25f2011-11-02 18:18:014724 extensions::UnpackedInstaller::Create(service_)->Load(no_id_ext);
[email protected]eedc1c752013-08-09 18:51:554725 base::RunLoop().RunUntilIdle();
[email protected]84ac7f32009-10-06 06:17:544726 ASSERT_EQ(1u, loaded_.size());
4727 ASSERT_EQ(previous_id, loaded_[0]->id());
[email protected]0b344962009-03-31 04:21:454728}
[email protected]894bb502009-05-21 22:39:574729
[email protected]557c7bd2013-09-18 21:51:254730TEST_F(ExtensionServiceTest, UnpackedValidatesLocales) {
4731 InitializeEmptyExtensionService();
4732
4733 base::FilePath bad_locale = data_dir_.AppendASCII("unpacked").
4734 AppendASCII("bad_messages_file");
4735 extensions::UnpackedInstaller::Create(service_)->Load(bad_locale);
4736 base::RunLoop().RunUntilIdle();
4737 EXPECT_EQ(1u, GetErrors().size());
4738 base::FilePath ms_messages_file = bad_locale.AppendASCII("_locales")
4739 .AppendASCII("ms")
4740 .AppendASCII("messages.json");
[email protected]04338722013-12-24 23:18:054741 EXPECT_THAT(base::UTF16ToUTF8(GetErrors()[0]), testing::AllOf(
4742 testing::HasSubstr(
4743 base::UTF16ToUTF8(ms_messages_file.LossyDisplayName())),
[email protected]557c7bd2013-09-18 21:51:254744 testing::HasSubstr("Dictionary keys must be quoted.")));
4745 ASSERT_EQ(0u, loaded_.size());
4746}
4747
[email protected]eaa7dd182010-12-14 11:09:004748void ExtensionServiceTest::TestExternalProvider(
[email protected]1d5e58b2013-01-31 08:41:404749 MockExtensionProvider* provider, Manifest::Location location) {
[email protected]a1257b12009-06-12 02:51:344750 // Verify that starting with no providers loads no extensions.
4751 service_->Init();
[email protected]a1257b12009-06-12 02:51:344752 ASSERT_EQ(0u, loaded_.size());
4753
[email protected]0a60a2e2010-10-25 16:15:214754 provider->set_visit_count(0);
4755
[email protected]a1257b12009-06-12 02:51:344756 // Register a test extension externally using the mock registry provider.
[email protected]650b2d52013-02-10 03:41:454757 base::FilePath source_path = data_dir_.AppendASCII("good.crx");
[email protected]894bb502009-05-21 22:39:574758
[email protected]a1257b12009-06-12 02:51:344759 // Add the extension.
[email protected]d55e7602009-12-16 04:20:424760 provider->UpdateOrAddExtension(good_crx, "1.0.0.0", source_path);
[email protected]894bb502009-05-21 22:39:574761
[email protected]9f1087e2009-06-15 17:29:324762 // Reloading extensions should find our externally registered extension
[email protected]894bb502009-05-21 22:39:574763 // and install it.
[email protected]97d6a5c2013-11-11 23:51:244764 content::WindowedNotificationObserver observer(
[email protected]6d057a0c2013-07-09 21:12:074765 chrome::NOTIFICATION_CRX_INSTALLER_DONE,
[email protected]97d6a5c2013-11-11 23:51:244766 content::NotificationService::AllSources());
4767 service_->CheckForExternalUpdates();
4768 observer.Wait();
[email protected]894bb502009-05-21 22:39:574769
4770 ASSERT_EQ(0u, GetErrors().size());
4771 ASSERT_EQ(1u, loaded_.size());
[email protected]d55e7602009-12-16 04:20:424772 ASSERT_EQ(location, loaded_[0]->location());
[email protected]894bb502009-05-21 22:39:574773 ASSERT_EQ("1.0.0.0", loaded_[0]->version()->GetString());
[email protected]25b34332009-06-05 21:53:194774 ValidatePrefKeyCount(1);
[email protected]e2194742010-08-12 05:54:344775 ValidateIntegerPref(good_crx, "state", Extension::ENABLED);
4776 ValidateIntegerPref(good_crx, "location", location);
[email protected]894bb502009-05-21 22:39:574777
[email protected]9f1087e2009-06-15 17:29:324778 // Reload extensions without changing anything. The extension should be
[email protected]894bb502009-05-21 22:39:574779 // loaded again.
4780 loaded_.clear();
[email protected]bb1bc9b32013-12-21 03:09:144781 service_->ReloadExtensionsForTest();
[email protected]eedc1c752013-08-09 18:51:554782 base::RunLoop().RunUntilIdle();
[email protected]894bb502009-05-21 22:39:574783 ASSERT_EQ(0u, GetErrors().size());
4784 ASSERT_EQ(1u, loaded_.size());
[email protected]25b34332009-06-05 21:53:194785 ValidatePrefKeyCount(1);
[email protected]e2194742010-08-12 05:54:344786 ValidateIntegerPref(good_crx, "state", Extension::ENABLED);
4787 ValidateIntegerPref(good_crx, "location", location);
[email protected]e2eb43112009-05-29 21:19:544788
[email protected]894bb502009-05-21 22:39:574789 // Now update the extension with a new version. We should get upgraded.
4790 source_path = source_path.DirName().AppendASCII("good2.crx");
[email protected]d55e7602009-12-16 04:20:424791 provider->UpdateOrAddExtension(good_crx, "1.0.0.1", source_path);
[email protected]894bb502009-05-21 22:39:574792
4793 loaded_.clear();
[email protected]97d6a5c2013-11-11 23:51:244794 content::WindowedNotificationObserver observer_2(
[email protected]6d057a0c2013-07-09 21:12:074795 chrome::NOTIFICATION_CRX_INSTALLER_DONE,
[email protected]97d6a5c2013-11-11 23:51:244796 content::NotificationService::AllSources());
4797 service_->CheckForExternalUpdates();
4798 observer_2.Wait();
[email protected]894bb502009-05-21 22:39:574799 ASSERT_EQ(0u, GetErrors().size());
4800 ASSERT_EQ(1u, loaded_.size());
4801 ASSERT_EQ("1.0.0.1", loaded_[0]->version()->GetString());
[email protected]25b34332009-06-05 21:53:194802 ValidatePrefKeyCount(1);
[email protected]e2194742010-08-12 05:54:344803 ValidateIntegerPref(good_crx, "state", Extension::ENABLED);
4804 ValidateIntegerPref(good_crx, "location", location);
[email protected]894bb502009-05-21 22:39:574805
[email protected]27b985d2009-06-25 17:53:154806 // Uninstall the extension and reload. Nothing should happen because the
[email protected]894bb502009-05-21 22:39:574807 // preference should prevent us from reinstalling.
4808 std::string id = loaded_[0]->id();
[email protected]dc24976f2013-06-02 21:15:094809 bool no_uninstall =
4810 management_policy_->MustRemainEnabled(loaded_[0].get(), NULL);
[email protected]d6ebc9792011-04-07 18:18:334811 service_->UninstallExtension(id, false, NULL);
[email protected]eedc1c752013-08-09 18:51:554812 base::RunLoop().RunUntilIdle();
[email protected]894bb502009-05-21 22:39:574813
[email protected]650b2d52013-02-10 03:41:454814 base::FilePath install_path = extensions_install_dir_.AppendASCII(id);
[email protected]e410b5f2012-12-14 14:02:244815 if (no_uninstall) {
[email protected]65187152012-06-02 13:14:144816 // Policy controlled extensions should not have been touched by uninstall.
[email protected]7567484142013-07-11 17:36:074817 ASSERT_TRUE(base::PathExists(install_path));
[email protected]65187152012-06-02 13:14:144818 } else {
[email protected]95da88c42011-03-31 10:07:334819 // The extension should also be gone from the install directory.
[email protected]7567484142013-07-11 17:36:074820 ASSERT_FALSE(base::PathExists(install_path));
[email protected]95da88c42011-03-31 10:07:334821 loaded_.clear();
4822 service_->CheckForExternalUpdates();
[email protected]eedc1c752013-08-09 18:51:554823 base::RunLoop().RunUntilIdle();
[email protected]95da88c42011-03-31 10:07:334824 ASSERT_EQ(0u, loaded_.size());
4825 ValidatePrefKeyCount(1);
[email protected]79c833b52011-04-05 18:31:014826 ValidateIntegerPref(good_crx, "state",
4827 Extension::EXTERNAL_EXTENSION_UNINSTALLED);
[email protected]95da88c42011-03-31 10:07:334828 ValidateIntegerPref(good_crx, "location", location);
[email protected]894bb502009-05-21 22:39:574829
[email protected]95da88c42011-03-31 10:07:334830 // Now clear the preference and reinstall.
4831 SetPrefInteg(good_crx, "state", Extension::ENABLED);
[email protected]25b34332009-06-05 21:53:194832
[email protected]95da88c42011-03-31 10:07:334833 loaded_.clear();
[email protected]97d6a5c2013-11-11 23:51:244834 content::WindowedNotificationObserver observer(
[email protected]6d057a0c2013-07-09 21:12:074835 chrome::NOTIFICATION_CRX_INSTALLER_DONE,
[email protected]97d6a5c2013-11-11 23:51:244836 content::NotificationService::AllSources());
4837 service_->CheckForExternalUpdates();
4838 observer.Wait();
[email protected]95da88c42011-03-31 10:07:334839 ASSERT_EQ(1u, loaded_.size());
[email protected]95da88c42011-03-31 10:07:334840 }
[email protected]25b34332009-06-05 21:53:194841 ValidatePrefKeyCount(1);
[email protected]e2194742010-08-12 05:54:344842 ValidateIntegerPref(good_crx, "state", Extension::ENABLED);
4843 ValidateIntegerPref(good_crx, "location", location);
[email protected]25b34332009-06-05 21:53:194844
[email protected]dc24976f2013-06-02 21:15:094845 if (management_policy_->MustRemainEnabled(loaded_[0].get(), NULL)) {
[email protected]65187152012-06-02 13:14:144846 EXPECT_EQ(2, provider->visit_count());
4847 } else {
[email protected]95da88c42011-03-31 10:07:334848 // Now test an externally triggered uninstall (deleting the registry key or
4849 // the pref entry).
4850 provider->RemoveExtension(good_crx);
[email protected]25b34332009-06-05 21:53:194851
[email protected]95da88c42011-03-31 10:07:334852 loaded_.clear();
[email protected]50067e52011-10-20 23:17:074853 service_->OnExternalProviderReady(provider);
[email protected]eedc1c752013-08-09 18:51:554854 base::RunLoop().RunUntilIdle();
[email protected]95da88c42011-03-31 10:07:334855 ASSERT_EQ(0u, loaded_.size());
4856 ValidatePrefKeyCount(0);
[email protected]25b34332009-06-05 21:53:194857
[email protected]95da88c42011-03-31 10:07:334858 // The extension should also be gone from the install directory.
[email protected]7567484142013-07-11 17:36:074859 ASSERT_FALSE(base::PathExists(install_path));
[email protected]abe7a8942009-06-23 05:14:294860
[email protected]95da88c42011-03-31 10:07:334861 // Now test the case where user uninstalls and then the extension is removed
4862 // from the external provider.
[email protected]97d6a5c2013-11-11 23:51:244863 content::WindowedNotificationObserver observer(
4864 chrome::NOTIFICATION_CRX_INSTALLER_DONE,
4865 content::NotificationService::AllSources());
[email protected]05aad2da2011-10-28 10:12:374866 provider->UpdateOrAddExtension(good_crx, "1.0.0.1", source_path);
[email protected]95da88c42011-03-31 10:07:334867 service_->CheckForExternalUpdates();
[email protected]97d6a5c2013-11-11 23:51:244868 observer.Wait();
[email protected]abe7a8942009-06-23 05:14:294869
[email protected]95da88c42011-03-31 10:07:334870 ASSERT_EQ(1u, loaded_.size());
4871 ASSERT_EQ(0u, GetErrors().size());
[email protected]d55e7602009-12-16 04:20:424872
[email protected]95da88c42011-03-31 10:07:334873 // User uninstalls.
4874 loaded_.clear();
[email protected]d6ebc9792011-04-07 18:18:334875 service_->UninstallExtension(id, false, NULL);
[email protected]eedc1c752013-08-09 18:51:554876 base::RunLoop().RunUntilIdle();
[email protected]95da88c42011-03-31 10:07:334877 ASSERT_EQ(0u, loaded_.size());
[email protected]d55e7602009-12-16 04:20:424878
[email protected]95da88c42011-03-31 10:07:334879 // Then remove the extension from the extension provider.
4880 provider->RemoveExtension(good_crx);
[email protected]d55e7602009-12-16 04:20:424881
[email protected]95da88c42011-03-31 10:07:334882 // Should still be at 0.
4883 loaded_.clear();
[email protected]d8c8f25f2011-11-02 18:18:014884 extensions::InstalledLoader(service_).LoadAllExtensions();
[email protected]eedc1c752013-08-09 18:51:554885 base::RunLoop().RunUntilIdle();
[email protected]95da88c42011-03-31 10:07:334886 ASSERT_EQ(0u, loaded_.size());
4887 ValidatePrefKeyCount(1);
[email protected]0a60a2e2010-10-25 16:15:214888
[email protected]95da88c42011-03-31 10:07:334889 EXPECT_EQ(5, provider->visit_count());
[email protected]95da88c42011-03-31 10:07:334890 }
[email protected]d55e7602009-12-16 04:20:424891}
4892
4893// Tests the external installation feature
4894#if defined(OS_WIN)
[email protected]d9a61e12012-11-14 02:43:474895TEST_F(ExtensionServiceTest, ExternalInstallRegistry) {
[email protected]aebe23a32010-12-10 22:15:484896 // This should all work, even when normal extension installation is disabled.
[email protected]eaa7dd182010-12-14 11:09:004897 InitializeEmptyExtensionService();
[email protected]aebe23a32010-12-10 22:15:484898 set_extensions_enabled(false);
[email protected]d55e7602009-12-16 04:20:424899
4900 // Now add providers. Extension system takes ownership of the objects.
4901 MockExtensionProvider* reg_provider =
[email protected]1d5e58b2013-01-31 08:41:404902 new MockExtensionProvider(service_, Manifest::EXTERNAL_REGISTRY);
[email protected]0a60a2e2010-10-25 16:15:214903 AddMockExternalProvider(reg_provider);
[email protected]1d5e58b2013-01-31 08:41:404904 TestExternalProvider(reg_provider, Manifest::EXTERNAL_REGISTRY);
[email protected]d55e7602009-12-16 04:20:424905}
4906#endif
4907
[email protected]d9a61e12012-11-14 02:43:474908TEST_F(ExtensionServiceTest, ExternalInstallPref) {
[email protected]eaa7dd182010-12-14 11:09:004909 InitializeEmptyExtensionService();
[email protected]d55e7602009-12-16 04:20:424910
4911 // Now add providers. Extension system takes ownership of the objects.
4912 MockExtensionProvider* pref_provider =
[email protected]1d5e58b2013-01-31 08:41:404913 new MockExtensionProvider(service_, Manifest::EXTERNAL_PREF);
[email protected]0a60a2e2010-10-25 16:15:214914
4915 AddMockExternalProvider(pref_provider);
[email protected]1d5e58b2013-01-31 08:41:404916 TestExternalProvider(pref_provider, Manifest::EXTERNAL_PREF);
[email protected]27b985d2009-06-25 17:53:154917}
4918
[email protected]d9a61e12012-11-14 02:43:474919TEST_F(ExtensionServiceTest, ExternalInstallPrefUpdateUrl) {
[email protected]aebe23a32010-12-10 22:15:484920 // This should all work, even when normal extension installation is disabled.
[email protected]eaa7dd182010-12-14 11:09:004921 InitializeEmptyExtensionService();
[email protected]aebe23a32010-12-10 22:15:484922 set_extensions_enabled(false);
[email protected]55196e92010-09-29 15:04:464923
[email protected]0a60a2e2010-10-25 16:15:214924 // TODO(skerner): The mock provider is not a good model of a provider
4925 // that works with update URLs, because it adds file and version info.
4926 // Extend the mock to work with update URLs. This test checks the
4927 // behavior that is common to all external extension visitors. The
4928 // browser test ExtensionManagementTest.ExternalUrlUpdate tests that
4929 // what the visitor does results in an extension being downloaded and
4930 // installed.
[email protected]55196e92010-09-29 15:04:464931 MockExtensionProvider* pref_provider =
[email protected]14908b72011-04-20 06:54:364932 new MockExtensionProvider(service_,
[email protected]1d5e58b2013-01-31 08:41:404933 Manifest::EXTERNAL_PREF_DOWNLOAD);
[email protected]0a60a2e2010-10-25 16:15:214934 AddMockExternalProvider(pref_provider);
[email protected]1d5e58b2013-01-31 08:41:404935 TestExternalProvider(pref_provider, Manifest::EXTERNAL_PREF_DOWNLOAD);
[email protected]55196e92010-09-29 15:04:464936}
4937
[email protected]d9a61e12012-11-14 02:43:474938TEST_F(ExtensionServiceTest, ExternalInstallPolicyUpdateUrl) {
[email protected]95da88c42011-03-31 10:07:334939 // This should all work, even when normal extension installation is disabled.
4940 InitializeEmptyExtensionService();
4941 set_extensions_enabled(false);
4942
4943 // TODO(skerner): The mock provider is not a good model of a provider
4944 // that works with update URLs, because it adds file and version info.
4945 // Extend the mock to work with update URLs. This test checks the
4946 // behavior that is common to all external extension visitors. The
4947 // browser test ExtensionManagementTest.ExternalUrlUpdate tests that
4948 // what the visitor does results in an extension being downloaded and
4949 // installed.
4950 MockExtensionProvider* pref_provider =
[email protected]14908b72011-04-20 06:54:364951 new MockExtensionProvider(service_,
[email protected]1d5e58b2013-01-31 08:41:404952 Manifest::EXTERNAL_POLICY_DOWNLOAD);
[email protected]95da88c42011-03-31 10:07:334953 AddMockExternalProvider(pref_provider);
[email protected]1d5e58b2013-01-31 08:41:404954 TestExternalProvider(pref_provider, Manifest::EXTERNAL_POLICY_DOWNLOAD);
[email protected]95da88c42011-03-31 10:07:334955}
4956
[email protected]aebe23a32010-12-10 22:15:484957// Tests that external extensions get uninstalled when the external extension
4958// providers can't account for them.
[email protected]d9a61e12012-11-14 02:43:474959TEST_F(ExtensionServiceTest, ExternalUninstall) {
[email protected]aebe23a32010-12-10 22:15:484960 // Start the extensions service with one external extension already installed.
[email protected]650b2d52013-02-10 03:41:454961 base::FilePath source_install_dir = data_dir_
[email protected]aebe23a32010-12-10 22:15:484962 .AppendASCII("good")
4963 .AppendASCII("Extensions");
[email protected]650b2d52013-02-10 03:41:454964 base::FilePath pref_path = source_install_dir
[email protected]aebe23a32010-12-10 22:15:484965 .DirName()
4966 .AppendASCII("PreferencesExternal");
4967
[email protected]5df038b2012-07-16 19:03:274968 // This initializes the extensions service with no ExternalProviders.
[email protected]eaa7dd182010-12-14 11:09:004969 InitializeInstalledExtensionService(pref_path, source_install_dir);
[email protected]aebe23a32010-12-10 22:15:484970 set_extensions_enabled(false);
4971
4972 service_->Init();
[email protected]aebe23a32010-12-10 22:15:484973
4974 ASSERT_EQ(0u, GetErrors().size());
[email protected]1f4728f2012-12-05 20:40:054975 ASSERT_EQ(0u, loaded_.size());
[email protected]aebe23a32010-12-10 22:15:484976
4977 // Verify that it's not the disabled extensions flag causing it not to load.
4978 set_extensions_enabled(true);
[email protected]bb1bc9b32013-12-21 03:09:144979 service_->ReloadExtensionsForTest();
[email protected]eedc1c752013-08-09 18:51:554980 base::RunLoop().RunUntilIdle();
[email protected]aebe23a32010-12-10 22:15:484981
4982 ASSERT_EQ(0u, GetErrors().size());
4983 ASSERT_EQ(0u, loaded_.size());
4984}
4985
[email protected]a29a517a2011-01-21 21:11:124986// Test that running multiple update checks simultaneously does not
4987// keep the update from succeeding.
[email protected]d9a61e12012-11-14 02:43:474988TEST_F(ExtensionServiceTest, MultipleExternalUpdateCheck) {
[email protected]a29a517a2011-01-21 21:11:124989 InitializeEmptyExtensionService();
4990
4991 MockExtensionProvider* provider =
[email protected]1d5e58b2013-01-31 08:41:404992 new MockExtensionProvider(service_, Manifest::EXTERNAL_PREF);
[email protected]a29a517a2011-01-21 21:11:124993 AddMockExternalProvider(provider);
4994
4995 // Verify that starting with no providers loads no extensions.
4996 service_->Init();
[email protected]a29a517a2011-01-21 21:11:124997 ASSERT_EQ(0u, loaded_.size());
4998
4999 // Start two checks for updates.
5000 provider->set_visit_count(0);
5001 service_->CheckForExternalUpdates();
5002 service_->CheckForExternalUpdates();
[email protected]eedc1c752013-08-09 18:51:555003 base::RunLoop().RunUntilIdle();
[email protected]a29a517a2011-01-21 21:11:125004
5005 // Two calls should cause two checks for external extensions.
5006 EXPECT_EQ(2, provider->visit_count());
5007 EXPECT_EQ(0u, GetErrors().size());
5008 EXPECT_EQ(0u, loaded_.size());
5009
5010 // Register a test extension externally using the mock registry provider.
[email protected]650b2d52013-02-10 03:41:455011 base::FilePath source_path = data_dir_.AppendASCII("good.crx");
[email protected]a29a517a2011-01-21 21:11:125012 provider->UpdateOrAddExtension(good_crx, "1.0.0.0", source_path);
5013
5014 // Two checks for external updates should find the extension, and install it
5015 // once.
[email protected]97d6a5c2013-11-11 23:51:245016 content::WindowedNotificationObserver observer(
5017 chrome::NOTIFICATION_CRX_INSTALLER_DONE,
5018 content::NotificationService::AllSources());
[email protected]a29a517a2011-01-21 21:11:125019 provider->set_visit_count(0);
5020 service_->CheckForExternalUpdates();
5021 service_->CheckForExternalUpdates();
[email protected]97d6a5c2013-11-11 23:51:245022 observer.Wait();
[email protected]a29a517a2011-01-21 21:11:125023 EXPECT_EQ(2, provider->visit_count());
5024 ASSERT_EQ(0u, GetErrors().size());
5025 ASSERT_EQ(1u, loaded_.size());
[email protected]1d5e58b2013-01-31 08:41:405026 ASSERT_EQ(Manifest::EXTERNAL_PREF, loaded_[0]->location());
[email protected]a29a517a2011-01-21 21:11:125027 ASSERT_EQ("1.0.0.0", loaded_[0]->version()->GetString());
5028 ValidatePrefKeyCount(1);
5029 ValidateIntegerPref(good_crx, "state", Extension::ENABLED);
[email protected]1d5e58b2013-01-31 08:41:405030 ValidateIntegerPref(good_crx, "location", Manifest::EXTERNAL_PREF);
[email protected]a29a517a2011-01-21 21:11:125031
5032 provider->RemoveExtension(good_crx);
5033 provider->set_visit_count(0);
5034 service_->CheckForExternalUpdates();
5035 service_->CheckForExternalUpdates();
[email protected]eedc1c752013-08-09 18:51:555036 base::RunLoop().RunUntilIdle();
[email protected]a29a517a2011-01-21 21:11:125037
5038 // Two calls should cause two checks for external extensions.
5039 // Because the external source no longer includes good_crx,
5040 // good_crx will be uninstalled. So, expect that no extensions
5041 // are loaded.
5042 EXPECT_EQ(2, provider->visit_count());
5043 EXPECT_EQ(0u, GetErrors().size());
5044 EXPECT_EQ(0u, loaded_.size());
5045}
5046
[email protected]d9a61e12012-11-14 02:43:475047TEST_F(ExtensionServiceTest, ExternalPrefProvider) {
[email protected]eaa7dd182010-12-14 11:09:005048 InitializeEmptyExtensionService();
[email protected]f0841cd2011-01-19 15:07:245049
5050 // Test some valid extension records.
5051 // Set a base path to avoid erroring out on relative paths.
5052 // Paths starting with // are absolute on every platform we support.
[email protected]650b2d52013-02-10 03:41:455053 base::FilePath base_path(FILE_PATH_LITERAL("//base/path"));
[email protected]f0841cd2011-01-19 15:07:245054 ASSERT_TRUE(base_path.IsAbsolute());
5055 MockProviderVisitor visitor(base_path);
[email protected]27b985d2009-06-25 17:53:155056 std::string json_data =
5057 "{"
[email protected]f0841cd2011-01-19 15:07:245058 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\": {"
[email protected]9e54cb572010-09-03 20:08:065059 " \"external_crx\": \"RandomExtension.crx\","
5060 " \"external_version\": \"1.0\""
5061 " },"
5062 " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\": {"
5063 " \"external_crx\": \"RandomExtension2.crx\","
5064 " \"external_version\": \"2.0\""
5065 " },"
5066 " \"cccccccccccccccccccccccccccccccc\": {"
[email protected]a424d84c2010-09-24 09:31:155067 " \"external_update_url\": \"http:\\\\foo.com/update\""
[email protected]9e54cb572010-09-03 20:08:065068 " }"
[email protected]27b985d2009-06-25 17:53:155069 "}";
[email protected]f0841cd2011-01-19 15:07:245070 EXPECT_EQ(3, visitor.Visit(json_data));
[email protected]27b985d2009-06-25 17:53:155071
[email protected]9e54cb572010-09-03 20:08:065072 // Simulate an external_extensions.json file that contains seven invalid
[email protected]f0841cd2011-01-19 15:07:245073 // records:
[email protected]27b985d2009-06-25 17:53:155074 // - One that is missing the 'external_crx' key.
5075 // - One that is missing the 'external_version' key.
5076 // - One that is specifying .. in the path.
[email protected]8ef78fd2010-08-19 17:14:325077 // - One that specifies both a file and update URL.
5078 // - One that specifies no file or update URL.
5079 // - One that has an update URL that is not well formed.
[email protected]9e54cb572010-09-03 20:08:065080 // - One that contains a malformed version.
[email protected]ab22ba42011-01-14 16:36:385081 // - One that has an invalid id.
5082 // - One that has a non-dictionary value.
[email protected]0d461c52012-07-03 19:29:415083 // - One that has an integer 'external_version' instead of a string.
[email protected]9e54cb572010-09-03 20:08:065084 // The final extension is valid, and we check that it is read to make sure
5085 // failures don't stop valid records from being read.
[email protected]27b985d2009-06-25 17:53:155086 json_data =
5087 "{"
[email protected]9e54cb572010-09-03 20:08:065088 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\": {"
5089 " \"external_version\": \"1.0\""
5090 " },"
5091 " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\": {"
5092 " \"external_crx\": \"RandomExtension.crx\""
5093 " },"
5094 " \"cccccccccccccccccccccccccccccccc\": {"
5095 " \"external_crx\": \"..\\\\foo\\\\RandomExtension2.crx\","
5096 " \"external_version\": \"2.0\""
5097 " },"
5098 " \"dddddddddddddddddddddddddddddddd\": {"
5099 " \"external_crx\": \"RandomExtension2.crx\","
5100 " \"external_version\": \"2.0\","
5101 " \"external_update_url\": \"http:\\\\foo.com/update\""
5102 " },"
5103 " \"eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee\": {"
5104 " },"
5105 " \"ffffffffffffffffffffffffffffffff\": {"
5106 " \"external_update_url\": \"This string is not a valid URL\""
5107 " },"
5108 " \"gggggggggggggggggggggggggggggggg\": {"
5109 " \"external_crx\": \"RandomExtension3.crx\","
5110 " \"external_version\": \"This is not a valid version!\""
5111 " },"
[email protected]ab22ba42011-01-14 16:36:385112 " \"This is not a valid id!\": {},"
5113 " \"hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh\": true,"
[email protected]0d461c52012-07-03 19:29:415114 " \"iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii\": {"
5115 " \"external_crx\": \"RandomExtension4.crx\","
5116 " \"external_version\": 1.0"
5117 " },"
[email protected]ab22ba42011-01-14 16:36:385118 " \"pppppppppppppppppppppppppppppppp\": {"
[email protected]9e54cb572010-09-03 20:08:065119 " \"external_crx\": \"RandomValidExtension.crx\","
5120 " \"external_version\": \"1.0\""
5121 " }"
[email protected]27b985d2009-06-25 17:53:155122 "}";
[email protected]683d0702010-12-06 16:25:575123 EXPECT_EQ(1, visitor.Visit(json_data));
[email protected]f0841cd2011-01-19 15:07:245124
5125 // Check that if a base path is not provided, use of a relative
5126 // path fails.
[email protected]650b2d52013-02-10 03:41:455127 base::FilePath empty;
[email protected]f0841cd2011-01-19 15:07:245128 MockProviderVisitor visitor_no_relative_paths(empty);
5129
5130 // Use absolute paths. Expect success.
5131 json_data =
5132 "{"
5133 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\": {"
5134 " \"external_crx\": \"//RandomExtension1.crx\","
5135 " \"external_version\": \"3.0\""
5136 " },"
5137 " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\": {"
5138 " \"external_crx\": \"//path/to/RandomExtension2.crx\","
5139 " \"external_version\": \"3.0\""
5140 " }"
5141 "}";
5142 EXPECT_EQ(2, visitor_no_relative_paths.Visit(json_data));
5143
5144 // Use a relative path. Expect that it will error out.
5145 json_data =
5146 "{"
5147 " \"cccccccccccccccccccccccccccccccc\": {"
5148 " \"external_crx\": \"RandomExtension2.crx\","
5149 " \"external_version\": \"3.0\""
5150 " }"
5151 "}";
5152 EXPECT_EQ(0, visitor_no_relative_paths.Visit(json_data));
[email protected]9d32ded072011-10-11 16:31:055153
5154 // Test supported_locales.
5155 json_data =
5156 "{"
5157 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\": {"
5158 " \"external_crx\": \"RandomExtension.crx\","
5159 " \"external_version\": \"1.0\","
5160 " \"supported_locales\": [ \"en\" ]"
5161 " },"
5162 " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\": {"
5163 " \"external_crx\": \"RandomExtension2.crx\","
5164 " \"external_version\": \"2.0\","
5165 " \"supported_locales\": [ \"en-GB\" ]"
5166 " },"
5167 " \"cccccccccccccccccccccccccccccccc\": {"
5168 " \"external_crx\": \"RandomExtension2.crx\","
5169 " \"external_version\": \"3.0\","
5170 " \"supported_locales\": [ \"en_US\", \"fr\" ]"
5171 " }"
5172 "}";
5173 {
5174 ScopedBrowserLocale guard("en-US");
5175 EXPECT_EQ(2, visitor.Visit(json_data));
5176 }
[email protected]f121003b2012-05-04 21:57:475177
[email protected]19eac6d2013-05-30 06:51:035178 // Test keep_if_present.
5179 json_data =
5180 "{"
5181 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\": {"
5182 " \"external_crx\": \"RandomExtension.crx\","
5183 " \"external_version\": \"1.0\","
5184 " \"keep_if_present\": true"
5185 " }"
5186 "}";
5187 {
5188 EXPECT_EQ(0, visitor.Visit(json_data));
5189 }
5190
[email protected]f121003b2012-05-04 21:57:475191 // Test is_bookmark_app.
5192 MockProviderVisitor from_bookmark_visitor(
5193 base_path, Extension::FROM_BOOKMARK);
5194 json_data =
5195 "{"
5196 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\": {"
5197 " \"external_crx\": \"RandomExtension.crx\","
5198 " \"external_version\": \"1.0\","
5199 " \"is_bookmark_app\": true"
5200 " }"
5201 "}";
5202 EXPECT_EQ(1, from_bookmark_visitor.Visit(json_data));
[email protected]7425d7df2012-11-28 14:35:425203
5204 // Test is_from_webstore.
5205 MockProviderVisitor from_webstore_visitor(
5206 base_path, Extension::FROM_WEBSTORE);
5207 json_data =
5208 "{"
5209 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\": {"
5210 " \"external_crx\": \"RandomExtension.crx\","
5211 " \"external_version\": \"1.0\","
5212 " \"is_from_webstore\": true"
5213 " }"
5214 "}";
5215 EXPECT_EQ(1, from_webstore_visitor.Visit(json_data));
[email protected]e18236b2009-06-22 21:32:105216}
[email protected]36a784c2009-06-23 06:21:085217
[email protected]c6d474f82009-12-16 21:11:065218// Test loading good extensions from the profile directory.
[email protected]d9a61e12012-11-14 02:43:475219TEST_F(ExtensionServiceTest, LoadAndRelocalizeExtensions) {
[email protected]6884a802012-08-07 03:55:225220 // Ensure we're testing in "en" and leave global state untouched.
5221 extension_l10n_util::ScopedLocaleForTest testLocale("en");
5222
[email protected]c6d474f82009-12-16 21:11:065223 // Initialize the test dir with a good Preferences/extensions.
[email protected]650b2d52013-02-10 03:41:455224 base::FilePath source_install_dir = data_dir_
[email protected]c6d474f82009-12-16 21:11:065225 .AppendASCII("l10n");
[email protected]650b2d52013-02-10 03:41:455226 base::FilePath pref_path = source_install_dir.AppendASCII("Preferences");
[email protected]eaa7dd182010-12-14 11:09:005227 InitializeInstalledExtensionService(pref_path, source_install_dir);
[email protected]c6d474f82009-12-16 21:11:065228
5229 service_->Init();
[email protected]c6d474f82009-12-16 21:11:065230
5231 ASSERT_EQ(3u, loaded_.size());
5232
5233 // This was equal to "sr" on load.
5234 ValidateStringPref(loaded_[0]->id(), keys::kCurrentLocale, "en");
5235
5236 // These are untouched by re-localization.
5237 ValidateStringPref(loaded_[1]->id(), keys::kCurrentLocale, "en");
5238 EXPECT_FALSE(IsPrefExist(loaded_[1]->id(), keys::kCurrentLocale));
5239
5240 // This one starts with Serbian name, and gets re-localized into English.
5241 EXPECT_EQ("My name is simple.", loaded_[0]->name());
5242
5243 // These are untouched by re-localization.
5244 EXPECT_EQ("My name is simple.", loaded_[1]->name());
5245 EXPECT_EQ("no l10n", loaded_[2]->name());
5246}
5247
[email protected]6c2381d2011-10-19 02:52:535248class ExtensionsReadyRecorder : public content::NotificationObserver {
[email protected]f0488f2f2009-07-01 05:25:225249 public:
5250 ExtensionsReadyRecorder() : ready_(false) {
[email protected]432115822011-07-10 15:52:275251 registrar_.Add(this, chrome::NOTIFICATION_EXTENSIONS_READY,
[email protected]ad50def52011-10-19 23:17:075252 content::NotificationService::AllSources());
[email protected]f0488f2f2009-07-01 05:25:225253 }
5254
5255 void set_ready(bool value) { ready_ = value; }
5256 bool ready() { return ready_; }
5257
5258 private:
[email protected]432115822011-07-10 15:52:275259 virtual void Observe(int type,
[email protected]6c2381d2011-10-19 02:52:535260 const content::NotificationSource& source,
[email protected]49aeab62013-02-07 02:53:115261 const content::NotificationDetails& details) OVERRIDE {
[email protected]432115822011-07-10 15:52:275262 switch (type) {
5263 case chrome::NOTIFICATION_EXTENSIONS_READY:
[email protected]f0488f2f2009-07-01 05:25:225264 ready_ = true;
5265 break;
5266 default:
5267 NOTREACHED();
5268 }
5269 }
5270
[email protected]6c2381d2011-10-19 02:52:535271 content::NotificationRegistrar registrar_;
[email protected]f0488f2f2009-07-01 05:25:225272 bool ready_;
5273};
5274
[email protected]36a784c2009-06-23 06:21:085275// Test that we get enabled/disabled correctly for all the pref/command-line
[email protected]eaa7dd182010-12-14 11:09:005276// combinations. We don't want to derive from the ExtensionServiceTest class
5277// for this test, so we use ExtensionServiceTestSimple.
[email protected]f0488f2f2009-07-01 05:25:225278//
5279// Also tests that we always fire EXTENSIONS_READY, no matter whether we are
5280// enabled or not.
[email protected]39fdf5202012-11-14 00:38:175281TEST(ExtensionServiceTestSimple, Enabledness) {
[email protected]5a24d2b2012-11-14 20:38:315282 // Make sure the PluginService singleton is destroyed at the end of the test.
[email protected]39fdf5202012-11-14 00:38:175283 base::ShadowingAtExitManager at_exit_manager;
[email protected]8f3372122013-07-18 04:34:145284#if defined(ENABLE_PLUGINS)
5285 content::PluginService::GetInstance()->Init();
5286 content::PluginService::GetInstance()->DisablePluginsDiscoveryForTesting();
5287#endif
[email protected]5a24d2b2012-11-14 20:38:315288
[email protected]ed8ee722011-04-22 06:49:445289 ExtensionErrorReporter::Init(false); // no noisy errors
[email protected]f0488f2f2009-07-01 05:25:225290 ExtensionsReadyRecorder recorder;
[email protected]aa96d3a2010-08-21 08:45:255291 scoped_ptr<TestingProfile> profile(new TestingProfile());
[email protected]33ad6ce92013-08-27 14:39:085292 content::TestBrowserThreadBundle thread_bundle_;
[email protected]e4854dc2013-04-24 00:11:515293#if defined OS_CHROMEOS
5294 chromeos::ScopedTestDeviceSettingsService device_settings_service;
5295 chromeos::ScopedTestCrosSettings cros_settings;
5296 scoped_ptr<chromeos::ScopedTestUserManager> user_manager(
5297 new chromeos::ScopedTestUserManager);
5298#endif
[email protected]36a784c2009-06-23 06:21:085299 scoped_ptr<CommandLine> command_line;
[email protected]650b2d52013-02-10 03:41:455300 base::FilePath install_dir = profile->GetPath()
[email protected]836e2982013-05-16 08:07:425301 .AppendASCII(extensions::kInstallDirectoryName);
[email protected]be5a6db2012-11-13 14:39:115302
[email protected]6d60703b2009-08-29 01:29:235303 // By default, we are enabled.
[email protected]947446b2010-10-21 03:36:315304 command_line.reset(new CommandLine(CommandLine::NO_PROGRAM));
[email protected]bd306722012-07-11 20:43:595305 ExtensionService* service = static_cast<extensions::TestExtensionSystem*>(
[email protected]749d59a2012-04-05 00:23:245306 ExtensionSystem::Get(profile.get()))->
[email protected]31d8f5f22012-04-02 15:22:085307 CreateExtensionService(
5308 command_line.get(),
5309 install_dir,
5310 false);
[email protected]6d60703b2009-08-29 01:29:235311 EXPECT_TRUE(service->extensions_enabled());
5312 service->Init();
[email protected]eedc1c752013-08-09 18:51:555313 base::RunLoop().RunUntilIdle();
[email protected]6d60703b2009-08-29 01:29:235314 EXPECT_TRUE(recorder.ready());
[email protected]e4854dc2013-04-24 00:11:515315#if defined OS_CHROMEOS
5316 user_manager.reset();
5317#endif
[email protected]6d60703b2009-08-29 01:29:235318
5319 // If either the command line or pref is set, we are disabled.
5320 recorder.set_ready(false);
[email protected]aa96d3a2010-08-21 08:45:255321 profile.reset(new TestingProfile());
[email protected]6d60703b2009-08-29 01:29:235322 command_line->AppendSwitch(switches::kDisableExtensions);
[email protected]bd306722012-07-11 20:43:595323 service = static_cast<extensions::TestExtensionSystem*>(
[email protected]749d59a2012-04-05 00:23:245324 ExtensionSystem::Get(profile.get()))->
[email protected]31d8f5f22012-04-02 15:22:085325 CreateExtensionService(
5326 command_line.get(),
5327 install_dir,
5328 false);
[email protected]36a784c2009-06-23 06:21:085329 EXPECT_FALSE(service->extensions_enabled());
[email protected]f0488f2f2009-07-01 05:25:225330 service->Init();
[email protected]eedc1c752013-08-09 18:51:555331 base::RunLoop().RunUntilIdle();
[email protected]f0488f2f2009-07-01 05:25:225332 EXPECT_TRUE(recorder.ready());
[email protected]36a784c2009-06-23 06:21:085333
[email protected]f0488f2f2009-07-01 05:25:225334 recorder.set_ready(false);
[email protected]aa96d3a2010-08-21 08:45:255335 profile.reset(new TestingProfile());
5336 profile->GetPrefs()->SetBoolean(prefs::kDisableExtensions, true);
[email protected]bd306722012-07-11 20:43:595337 service = static_cast<extensions::TestExtensionSystem*>(
[email protected]749d59a2012-04-05 00:23:245338 ExtensionSystem::Get(profile.get()))->
[email protected]31d8f5f22012-04-02 15:22:085339 CreateExtensionService(
5340 command_line.get(),
5341 install_dir,
5342 false);
[email protected]6d60703b2009-08-29 01:29:235343 EXPECT_FALSE(service->extensions_enabled());
[email protected]f0488f2f2009-07-01 05:25:225344 service->Init();
[email protected]eedc1c752013-08-09 18:51:555345 base::RunLoop().RunUntilIdle();
[email protected]f0488f2f2009-07-01 05:25:225346 EXPECT_TRUE(recorder.ready());
[email protected]36a784c2009-06-23 06:21:085347
[email protected]f0488f2f2009-07-01 05:25:225348 recorder.set_ready(false);
[email protected]aa96d3a2010-08-21 08:45:255349 profile.reset(new TestingProfile());
5350 profile->GetPrefs()->SetBoolean(prefs::kDisableExtensions, true);
[email protected]947446b2010-10-21 03:36:315351 command_line.reset(new CommandLine(CommandLine::NO_PROGRAM));
[email protected]bd306722012-07-11 20:43:595352 service = static_cast<extensions::TestExtensionSystem*>(
[email protected]749d59a2012-04-05 00:23:245353 ExtensionSystem::Get(profile.get()))->
[email protected]31d8f5f22012-04-02 15:22:085354 CreateExtensionService(
5355 command_line.get(),
5356 install_dir,
5357 false);
[email protected]6d60703b2009-08-29 01:29:235358 EXPECT_FALSE(service->extensions_enabled());
[email protected]f0488f2f2009-07-01 05:25:225359 service->Init();
[email protected]eedc1c752013-08-09 18:51:555360 base::RunLoop().RunUntilIdle();
[email protected]f0488f2f2009-07-01 05:25:225361 EXPECT_TRUE(recorder.ready());
[email protected]7e1951a2010-09-30 10:22:205362
5363 // Explicitly delete all the resources used in this test.
5364 profile.reset();
5365 service = NULL;
[email protected]060d4972012-07-19 17:22:395366 // Execute any pending deletion tasks.
[email protected]eedc1c752013-08-09 18:51:555367 base::RunLoop().RunUntilIdle();
[email protected]36a784c2009-06-23 06:21:085368}
[email protected]24b538a2010-02-27 01:22:445369
5370// Test loading extensions that require limited and unlimited storage quotas.
[email protected]d9a61e12012-11-14 02:43:475371TEST_F(ExtensionServiceTest, StorageQuota) {
[email protected]eaa7dd182010-12-14 11:09:005372 InitializeEmptyExtensionService();
[email protected]24b538a2010-02-27 01:22:445373
[email protected]650b2d52013-02-10 03:41:455374 base::FilePath extensions_path = data_dir_
[email protected]24b538a2010-02-27 01:22:445375 .AppendASCII("storage_quota");
5376
[email protected]650b2d52013-02-10 03:41:455377 base::FilePath limited_quota_ext =
[email protected]e85e34c32011-04-13 18:38:355378 extensions_path.AppendASCII("limited_quota")
[email protected]24b538a2010-02-27 01:22:445379 .AppendASCII("1.0");
[email protected]03b612f2010-08-13 21:09:215380
5381 // The old permission name for unlimited quota was "unlimited_storage", but
5382 // we changed it to "unlimitedStorage". This tests both versions.
[email protected]650b2d52013-02-10 03:41:455383 base::FilePath unlimited_quota_ext =
[email protected]e85e34c32011-04-13 18:38:355384 extensions_path.AppendASCII("unlimited_quota")
[email protected]24b538a2010-02-27 01:22:445385 .AppendASCII("1.0");
[email protected]650b2d52013-02-10 03:41:455386 base::FilePath unlimited_quota_ext2 =
[email protected]e85e34c32011-04-13 18:38:355387 extensions_path.AppendASCII("unlimited_quota")
[email protected]03b612f2010-08-13 21:09:215388 .AppendASCII("2.0");
[email protected]d8c8f25f2011-11-02 18:18:015389 extensions::UnpackedInstaller::Create(service_)->Load(limited_quota_ext);
5390 extensions::UnpackedInstaller::Create(service_)->Load(unlimited_quota_ext);
5391 extensions::UnpackedInstaller::Create(service_)->Load(unlimited_quota_ext2);
[email protected]eedc1c752013-08-09 18:51:555392 base::RunLoop().RunUntilIdle();
[email protected]24b538a2010-02-27 01:22:445393
[email protected]03b612f2010-08-13 21:09:215394 ASSERT_EQ(3u, loaded_.size());
[email protected]24b538a2010-02-27 01:22:445395 EXPECT_TRUE(profile_.get());
5396 EXPECT_FALSE(profile_->IsOffTheRecord());
[email protected]7c5f2ec2011-05-26 19:15:265397 EXPECT_FALSE(profile_->GetExtensionSpecialStoragePolicy()->IsStorageUnlimited(
5398 loaded_[0]->url()));
5399 EXPECT_TRUE(profile_->GetExtensionSpecialStoragePolicy()->IsStorageUnlimited(
5400 loaded_[1]->url()));
5401 EXPECT_TRUE(profile_->GetExtensionSpecialStoragePolicy()->IsStorageUnlimited(
5402 loaded_[2]->url()));
[email protected]24b538a2010-02-27 01:22:445403}
[email protected]1952c7d2010-03-04 23:48:345404
[email protected]d8c8f25f2011-11-02 18:18:015405// Tests ComponentLoader::Add().
[email protected]d9a61e12012-11-14 02:43:475406TEST_F(ExtensionServiceTest, ComponentExtensions) {
[email protected]eaa7dd182010-12-14 11:09:005407 InitializeEmptyExtensionService();
[email protected]1952c7d2010-03-04 23:48:345408
[email protected]f0b97f12010-10-11 21:44:355409 // Component extensions should work even when extensions are disabled.
5410 set_extensions_enabled(false);
5411
[email protected]650b2d52013-02-10 03:41:455412 base::FilePath path = data_dir_
[email protected]1952c7d2010-03-04 23:48:345413 .AppendASCII("good")
5414 .AppendASCII("Extensions")
5415 .AppendASCII("behllobkkfkfnphdnhnkndlbkcpglgmj")
5416 .AppendASCII("1.0.0.0");
5417
5418 std::string manifest;
[email protected]82f84b92013-08-30 18:23:505419 ASSERT_TRUE(base::ReadFileToString(
[email protected]993da5e2013-03-23 21:25:165420 path.Append(extensions::kManifestFilename), &manifest));
[email protected]1952c7d2010-03-04 23:48:345421
[email protected]d8c8f25f2011-11-02 18:18:015422 service_->component_loader()->Add(manifest, path);
[email protected]1952c7d2010-03-04 23:48:345423 service_->Init();
5424
5425 // Note that we do not pump messages -- the extension should be loaded
5426 // immediately.
5427
5428 EXPECT_EQ(0u, GetErrors().size());
5429 ASSERT_EQ(1u, loaded_.size());
[email protected]1d5e58b2013-01-31 08:41:405430 EXPECT_EQ(Manifest::COMPONENT, loaded_[0]->location());
[email protected]1952c7d2010-03-04 23:48:345431 EXPECT_EQ(1u, service_->extensions()->size());
5432
[email protected]8c484b742012-11-29 06:05:365433 // Component extensions get a prefs entry on first install.
5434 ValidatePrefKeyCount(1);
[email protected]1952c7d2010-03-04 23:48:345435
5436 // Reload all extensions, and make sure it comes back.
[email protected]8f512c72011-11-22 21:02:505437 std::string extension_id = (*service_->extensions()->begin())->id();
[email protected]1952c7d2010-03-04 23:48:345438 loaded_.clear();
[email protected]bb1bc9b32013-12-21 03:09:145439 service_->ReloadExtensionsForTest();
[email protected]1952c7d2010-03-04 23:48:345440 ASSERT_EQ(1u, service_->extensions()->size());
[email protected]8f512c72011-11-22 21:02:505441 EXPECT_EQ(extension_id, (*service_->extensions()->begin())->id());
[email protected]1952c7d2010-03-04 23:48:345442}
[email protected]145a317b2011-04-12 16:03:465443
[email protected]90310d92011-04-17 07:35:045444namespace {
[email protected]fe4b4ca2013-09-05 23:30:015445
[email protected]0c3c9732013-09-16 08:53:415446class TestSyncProcessorStub : public syncer::SyncChangeProcessor {
5447 virtual syncer::SyncError ProcessSyncChanges(
5448 const tracked_objects::Location& from_here,
5449 const syncer::SyncChangeList& change_list) OVERRIDE {
5450 return syncer::SyncError();
5451 }
5452
5453 virtual syncer::SyncDataList GetAllSyncData(
5454 syncer::ModelType type) const OVERRIDE {
5455 return syncer::SyncDataList();
5456 }
5457};
5458
5459} // namespace
[email protected]90310d92011-04-17 07:35:045460
[email protected]6d147912013-07-09 08:02:145461TEST_F(ExtensionServiceTest, DeferredSyncStartupPreInstalledComponent) {
5462 InitializeEmptyExtensionService();
[email protected]f8aefb132013-10-30 09:29:525463 InitializeExtensionSyncService();
[email protected]6d147912013-07-09 08:02:145464
5465 bool flare_was_called = false;
5466 syncer::ModelType triggered_type(syncer::UNSPECIFIED);
5467 base::WeakPtrFactory<ExtensionServiceTest> factory(this);
[email protected]f8aefb132013-10-30 09:29:525468 extension_sync_service_->SetSyncStartFlare(
[email protected]6d147912013-07-09 08:02:145469 base::Bind(&ExtensionServiceTest::MockSyncStartFlare,
5470 factory.GetWeakPtr(),
5471 &flare_was_called, // Safe due to WeakPtrFactory scope.
5472 &triggered_type)); // Safe due to WeakPtrFactory scope.
5473
5474 // Install a component extension.
[email protected]6d147912013-07-09 08:02:145475 std::string manifest;
[email protected]82f84b92013-08-30 18:23:505476 ASSERT_TRUE(base::ReadFileToString(
[email protected]3f2a2fa2013-09-24 02:55:255477 good0_path().Append(extensions::kManifestFilename), &manifest));
5478 service_->component_loader()->Add(manifest, good0_path());
[email protected]6d147912013-07-09 08:02:145479 ASSERT_FALSE(service_->is_ready());
5480 service_->Init();
5481 ASSERT_TRUE(service_->is_ready());
5482
5483 // Extensions added before service is_ready() don't trigger sync startup.
5484 EXPECT_FALSE(flare_was_called);
5485 ASSERT_EQ(syncer::UNSPECIFIED, triggered_type);
5486}
5487
5488TEST_F(ExtensionServiceTest, DeferredSyncStartupPreInstalledNormal) {
[email protected]3f2a2fa2013-09-24 02:55:255489 InitializeGoodInstalledExtensionService();
[email protected]f8aefb132013-10-30 09:29:525490 InitializeExtensionSyncService();
[email protected]6d147912013-07-09 08:02:145491
5492 bool flare_was_called = false;
5493 syncer::ModelType triggered_type(syncer::UNSPECIFIED);
5494 base::WeakPtrFactory<ExtensionServiceTest> factory(this);
[email protected]f8aefb132013-10-30 09:29:525495 extension_sync_service_->SetSyncStartFlare(
[email protected]6d147912013-07-09 08:02:145496 base::Bind(&ExtensionServiceTest::MockSyncStartFlare,
5497 factory.GetWeakPtr(),
5498 &flare_was_called, // Safe due to WeakPtrFactory scope.
5499 &triggered_type)); // Safe due to WeakPtrFactory scope.
5500
5501 ASSERT_FALSE(service_->is_ready());
5502 service_->Init();
[email protected]50736a12013-09-26 08:58:345503 ASSERT_EQ(3u, loaded_.size());
[email protected]6d147912013-07-09 08:02:145504 ASSERT_TRUE(service_->is_ready());
5505
5506 // Extensions added before service is_ready() don't trigger sync startup.
5507 EXPECT_FALSE(flare_was_called);
5508 ASSERT_EQ(syncer::UNSPECIFIED, triggered_type);
5509}
5510
5511TEST_F(ExtensionServiceTest, DeferredSyncStartupOnInstall) {
5512 InitializeEmptyExtensionService();
[email protected]f8aefb132013-10-30 09:29:525513 InitializeExtensionSyncService();
[email protected]6d147912013-07-09 08:02:145514 service_->Init();
5515 ASSERT_TRUE(service_->is_ready());
5516
5517 bool flare_was_called = false;
5518 syncer::ModelType triggered_type(syncer::UNSPECIFIED);
5519 base::WeakPtrFactory<ExtensionServiceTest> factory(this);
[email protected]f8aefb132013-10-30 09:29:525520 extension_sync_service_->SetSyncStartFlare(
[email protected]6d147912013-07-09 08:02:145521 base::Bind(&ExtensionServiceTest::MockSyncStartFlare,
5522 factory.GetWeakPtr(),
5523 &flare_was_called, // Safe due to WeakPtrFactory scope.
5524 &triggered_type)); // Safe due to WeakPtrFactory scope.
5525
5526 base::FilePath path = data_dir_.AppendASCII("good.crx");
5527 InstallCRX(path, INSTALL_NEW);
5528
5529 EXPECT_TRUE(flare_was_called);
5530 EXPECT_EQ(syncer::EXTENSIONS, triggered_type);
5531
5532 // Reset.
5533 flare_was_called = false;
5534 triggered_type = syncer::UNSPECIFIED;
5535
5536 // Once sync starts, flare should no longer be invoked.
[email protected]f8aefb132013-10-30 09:29:525537 extension_sync_service_->MergeDataAndStartSyncing(
[email protected]6d147912013-07-09 08:02:145538 syncer::EXTENSIONS, syncer::SyncDataList(),
5539 scoped_ptr<syncer::SyncChangeProcessor>(new TestSyncProcessorStub),
5540 scoped_ptr<syncer::SyncErrorFactory>(new syncer::SyncErrorFactoryMock()));
5541 path = data_dir_.AppendASCII("page_action.crx");
5542 InstallCRX(path, INSTALL_NEW);
5543 EXPECT_FALSE(flare_was_called);
5544 ASSERT_EQ(syncer::UNSPECIFIED, triggered_type);
5545}
5546
[email protected]386db3572013-11-08 12:26:445547TEST_F(ExtensionServiceTest, DisableExtensionFromSync) {
5548 // Start the extensions service with one external extension already installed.
5549 base::FilePath source_install_dir = data_dir_
5550 .AppendASCII("good")
5551 .AppendASCII("Extensions");
5552 base::FilePath pref_path = source_install_dir
5553 .DirName()
5554 .AppendASCII("Preferences");
5555
5556 InitializeInstalledExtensionService(pref_path, source_install_dir);
5557 InitializeExtensionSyncService();
5558
5559 // The user has enabled sync.
5560 ProfileSyncService* sync_service =
5561 ProfileSyncServiceFactory::GetForProfile(profile_.get());
5562 sync_service->SetSyncSetupCompleted();
5563
5564 service_->Init();
5565 ASSERT_TRUE(service_->is_ready());
5566
5567 ASSERT_EQ(3u, loaded_.size());
5568
5569 // We start enabled.
5570 const Extension* extension = service_->GetExtensionById(good0, true);
5571 ASSERT_TRUE(extension);
5572 ASSERT_TRUE(service_->IsExtensionEnabled(good0));
5573 extensions::ExtensionSyncData disable_good_crx(*extension, false, false);
5574
5575 // Then sync data arrives telling us to disable |good0|.
5576 syncer::SyncDataList sync_data;
5577 sync_data.push_back(disable_good_crx.GetSyncData());
5578 extension_sync_service_->MergeDataAndStartSyncing(
5579 syncer::EXTENSIONS, sync_data,
5580 scoped_ptr<syncer::SyncChangeProcessor>(new TestSyncProcessorStub),
5581 scoped_ptr<syncer::SyncErrorFactory>(new syncer::SyncErrorFactoryMock()));
5582 ASSERT_FALSE(service_->IsExtensionEnabled(good0));
5583}
5584
5585TEST_F(ExtensionServiceTest, DontDisableExtensionWithPendingEnableFromSync) {
5586 // Start the extensions service with one external extension already installed.
5587 base::FilePath source_install_dir = data_dir_
5588 .AppendASCII("good")
5589 .AppendASCII("Extensions");
5590 base::FilePath pref_path = source_install_dir
5591 .DirName()
5592 .AppendASCII("Preferences");
5593
5594 InitializeInstalledExtensionService(pref_path, source_install_dir);
5595 InitializeExtensionSyncService();
5596
5597 // The user has enabled sync.
5598 ProfileSyncService* sync_service =
5599 ProfileSyncServiceFactory::GetForProfile(profile_.get());
5600 sync_service->SetSyncSetupCompleted();
5601
5602 service_->Init();
5603 ASSERT_TRUE(service_->is_ready());
5604 ASSERT_EQ(3u, loaded_.size());
5605
5606 const Extension* extension = service_->GetExtensionById(good0, true);
5607 ASSERT_TRUE(service_->IsExtensionEnabled(good0));
5608
5609 // Disable extension before first sync data arrives.
5610 service_->DisableExtension(good0, Extension::DISABLE_USER_ACTION);
5611 ASSERT_FALSE(service_->IsExtensionEnabled(good0));
5612
5613 // Enable extension - this is now the most recent state.
5614 service_->EnableExtension(good0);
5615 ASSERT_TRUE(service_->IsExtensionEnabled(good0));
5616
5617 // Now sync data comes in that says to disable good0. This should be
5618 // ignored.
5619 extensions::ExtensionSyncData disable_good_crx(*extension, false, false);
5620 syncer::SyncDataList sync_data;
5621 sync_data.push_back(disable_good_crx.GetSyncData());
5622 extension_sync_service_->MergeDataAndStartSyncing(
5623 syncer::EXTENSIONS, sync_data,
5624 scoped_ptr<syncer::SyncChangeProcessor>(new TestSyncProcessorStub),
5625 scoped_ptr<syncer::SyncErrorFactory>(new syncer::SyncErrorFactoryMock()));
5626
5627 // The extension was enabled locally before the sync data arrived, so it
5628 // should still be enabled now.
5629 ASSERT_TRUE(service_->IsExtensionEnabled(good0));
5630}
5631
[email protected]d9a61e12012-11-14 02:43:475632TEST_F(ExtensionServiceTest, GetSyncData) {
[email protected]b05fb9ff2011-04-23 00:07:565633 InitializeEmptyExtensionService();
[email protected]f8aefb132013-10-30 09:29:525634 InitializeExtensionSyncService();
[email protected]8f512c72011-11-22 21:02:505635 InstallCRX(data_dir_.AppendASCII("good.crx"), INSTALL_NEW);
[email protected]45b6fee2011-05-03 09:41:315636 const Extension* extension = service_->GetInstalledExtension(good_crx);
[email protected]b05fb9ff2011-04-23 00:07:565637 ASSERT_TRUE(extension);
[email protected]85fc9202011-05-05 00:04:595638
[email protected]f8aefb132013-10-30 09:29:525639 extension_sync_service_->MergeDataAndStartSyncing(
[email protected]a4a147652012-07-03 23:41:325640 syncer::EXTENSIONS, syncer::SyncDataList(),
[email protected]65f173552012-06-28 22:43:585641 scoped_ptr<syncer::SyncChangeProcessor>(new TestSyncProcessorStub),
5642 scoped_ptr<syncer::SyncErrorFactory>(new syncer::SyncErrorFactoryMock()));
[email protected]3bdba0d2011-08-23 07:17:305643
[email protected]f8aefb132013-10-30 09:29:525644 syncer::SyncDataList list = extension_sync_service_->GetAllSyncData(
5645 syncer::EXTENSIONS);
[email protected]3bdba0d2011-08-23 07:17:305646 ASSERT_EQ(list.size(), 1U);
[email protected]5db9ada2012-04-11 13:48:205647 extensions::ExtensionSyncData data(list[0]);
[email protected]3bdba0d2011-08-23 07:17:305648 EXPECT_EQ(extension->id(), data.id());
5649 EXPECT_FALSE(data.uninstalled());
[email protected]386db3572013-11-08 12:26:445650 EXPECT_EQ(service_->IsExtensionEnabled(good_crx), data.enabled());
[email protected]1d5cf4142014-01-24 18:25:225651 EXPECT_EQ(extensions::util::IsIncognitoEnabled(good_crx, profile_.get()),
[email protected]a7ff4b72013-10-17 20:56:025652 data.incognito_enabled());
[email protected]3bdba0d2011-08-23 07:17:305653 EXPECT_TRUE(data.version().Equals(*extension->version()));
[email protected]65348062013-01-15 07:27:225654 EXPECT_EQ(extensions::ManifestURL::GetUpdateURL(extension),
5655 data.update_url());
[email protected]3bdba0d2011-08-23 07:17:305656 EXPECT_EQ(extension->name(), data.name());
[email protected]b05fb9ff2011-04-23 00:07:565657}
5658
[email protected]d9a61e12012-11-14 02:43:475659TEST_F(ExtensionServiceTest, GetSyncDataTerminated) {
[email protected]45b6fee2011-05-03 09:41:315660 InitializeEmptyExtensionService();
[email protected]f8aefb132013-10-30 09:29:525661 InitializeExtensionSyncService();
[email protected]8f512c72011-11-22 21:02:505662 InstallCRX(data_dir_.AppendASCII("good.crx"), INSTALL_NEW);
[email protected]45b6fee2011-05-03 09:41:315663 TerminateExtension(good_crx);
[email protected]85fc9202011-05-05 00:04:595664 const Extension* extension = service_->GetInstalledExtension(good_crx);
5665 ASSERT_TRUE(extension);
[email protected]3bdba0d2011-08-23 07:17:305666
5667 TestSyncProcessorStub processor;
[email protected]f8aefb132013-10-30 09:29:525668 extension_sync_service_->MergeDataAndStartSyncing(
[email protected]a4a147652012-07-03 23:41:325669 syncer::EXTENSIONS, syncer::SyncDataList(),
[email protected]65f173552012-06-28 22:43:585670 scoped_ptr<syncer::SyncChangeProcessor>(new TestSyncProcessorStub),
5671 scoped_ptr<syncer::SyncErrorFactory>(new syncer::SyncErrorFactoryMock()));
[email protected]3bdba0d2011-08-23 07:17:305672
[email protected]f8aefb132013-10-30 09:29:525673 syncer::SyncDataList list = extension_sync_service_->GetAllSyncData(
5674 syncer::EXTENSIONS);
[email protected]3bdba0d2011-08-23 07:17:305675 ASSERT_EQ(list.size(), 1U);
[email protected]5db9ada2012-04-11 13:48:205676 extensions::ExtensionSyncData data(list[0]);
[email protected]3bdba0d2011-08-23 07:17:305677 EXPECT_EQ(extension->id(), data.id());
5678 EXPECT_FALSE(data.uninstalled());
[email protected]386db3572013-11-08 12:26:445679 EXPECT_EQ(service_->IsExtensionEnabled(good_crx), data.enabled());
[email protected]1d5cf4142014-01-24 18:25:225680 EXPECT_EQ(extensions::util::IsIncognitoEnabled(good_crx, profile_.get()),
[email protected]a7ff4b72013-10-17 20:56:025681 data.incognito_enabled());
[email protected]3bdba0d2011-08-23 07:17:305682 EXPECT_TRUE(data.version().Equals(*extension->version()));
[email protected]65348062013-01-15 07:27:225683 EXPECT_EQ(extensions::ManifestURL::GetUpdateURL(extension),
5684 data.update_url());
[email protected]3bdba0d2011-08-23 07:17:305685 EXPECT_EQ(extension->name(), data.name());
[email protected]45b6fee2011-05-03 09:41:315686}
5687
[email protected]d9a61e12012-11-14 02:43:475688TEST_F(ExtensionServiceTest, GetSyncDataFilter) {
[email protected]b05fb9ff2011-04-23 00:07:565689 InitializeEmptyExtensionService();
[email protected]f8aefb132013-10-30 09:29:525690 InitializeExtensionSyncService();
[email protected]8f512c72011-11-22 21:02:505691 InstallCRX(data_dir_.AppendASCII("good.crx"), INSTALL_NEW);
[email protected]85fc9202011-05-05 00:04:595692 const Extension* extension = service_->GetInstalledExtension(good_crx);
5693 ASSERT_TRUE(extension);
[email protected]3bdba0d2011-08-23 07:17:305694
5695 TestSyncProcessorStub processor;
[email protected]f8aefb132013-10-30 09:29:525696 extension_sync_service_->MergeDataAndStartSyncing(syncer::APPS,
5697 syncer::SyncDataList(),
[email protected]65f173552012-06-28 22:43:585698 scoped_ptr<syncer::SyncChangeProcessor>(new TestSyncProcessorStub),
5699 scoped_ptr<syncer::SyncErrorFactory>(new syncer::SyncErrorFactoryMock()));
[email protected]3bdba0d2011-08-23 07:17:305700
[email protected]f8aefb132013-10-30 09:29:525701 syncer::SyncDataList list = extension_sync_service_->GetAllSyncData(
5702 syncer::EXTENSIONS);
[email protected]3bdba0d2011-08-23 07:17:305703 ASSERT_EQ(list.size(), 0U);
[email protected]b05fb9ff2011-04-23 00:07:565704}
5705
[email protected]d9a61e12012-11-14 02:43:475706TEST_F(ExtensionServiceTest, GetSyncExtensionDataUserSettings) {
[email protected]b05fb9ff2011-04-23 00:07:565707 InitializeEmptyExtensionService();
[email protected]98b6d942013-11-10 00:34:075708 InitializeProcessManager();
[email protected]f8aefb132013-10-30 09:29:525709 InitializeExtensionSyncService();
[email protected]8f512c72011-11-22 21:02:505710 InstallCRX(data_dir_.AppendASCII("good.crx"), INSTALL_NEW);
[email protected]85fc9202011-05-05 00:04:595711 const Extension* extension = service_->GetInstalledExtension(good_crx);
5712 ASSERT_TRUE(extension);
[email protected]b05fb9ff2011-04-23 00:07:565713
[email protected]3bdba0d2011-08-23 07:17:305714 TestSyncProcessorStub processor;
[email protected]f8aefb132013-10-30 09:29:525715 extension_sync_service_->MergeDataAndStartSyncing(
[email protected]a4a147652012-07-03 23:41:325716 syncer::EXTENSIONS, syncer::SyncDataList(),
[email protected]65f173552012-06-28 22:43:585717 scoped_ptr<syncer::SyncChangeProcessor>(new TestSyncProcessorStub),
5718 scoped_ptr<syncer::SyncErrorFactory>(new syncer::SyncErrorFactoryMock()));
[email protected]3bdba0d2011-08-23 07:17:305719
[email protected]b05fb9ff2011-04-23 00:07:565720 {
[email protected]f8aefb132013-10-30 09:29:525721 syncer::SyncDataList list = extension_sync_service_->GetAllSyncData(
5722 syncer::EXTENSIONS);
[email protected]3bdba0d2011-08-23 07:17:305723 ASSERT_EQ(list.size(), 1U);
[email protected]5db9ada2012-04-11 13:48:205724 extensions::ExtensionSyncData data(list[0]);
[email protected]386db3572013-11-08 12:26:445725 EXPECT_TRUE(data.enabled());
5726 EXPECT_FALSE(data.incognito_enabled());
5727 }
5728
5729 service_->DisableExtension(good_crx, Extension::DISABLE_USER_ACTION);
5730 {
5731 syncer::SyncDataList list = extension_sync_service_->GetAllSyncData(
5732 syncer::EXTENSIONS);
5733 ASSERT_EQ(list.size(), 1U);
5734 extensions::ExtensionSyncData data(list[0]);
5735 EXPECT_FALSE(data.enabled());
[email protected]3bdba0d2011-08-23 07:17:305736 EXPECT_FALSE(data.incognito_enabled());
[email protected]b05fb9ff2011-04-23 00:07:565737 }
5738
[email protected]1d5cf4142014-01-24 18:25:225739 extensions::util::SetIsIncognitoEnabled(good_crx, profile_.get(), true);
[email protected]b05fb9ff2011-04-23 00:07:565740 {
[email protected]f8aefb132013-10-30 09:29:525741 syncer::SyncDataList list = extension_sync_service_->GetAllSyncData(
5742 syncer::EXTENSIONS);
[email protected]3bdba0d2011-08-23 07:17:305743 ASSERT_EQ(list.size(), 1U);
[email protected]5db9ada2012-04-11 13:48:205744 extensions::ExtensionSyncData data(list[0]);
[email protected]386db3572013-11-08 12:26:445745 EXPECT_FALSE(data.enabled());
5746 EXPECT_TRUE(data.incognito_enabled());
5747 }
5748
5749 service_->EnableExtension(good_crx);
5750 {
5751 syncer::SyncDataList list = extension_sync_service_->GetAllSyncData(
5752 syncer::EXTENSIONS);
5753 ASSERT_EQ(list.size(), 1U);
5754 extensions::ExtensionSyncData data(list[0]);
5755 EXPECT_TRUE(data.enabled());
[email protected]3bdba0d2011-08-23 07:17:305756 EXPECT_TRUE(data.incognito_enabled());
[email protected]b05fb9ff2011-04-23 00:07:565757 }
5758}
5759
[email protected]d9a61e12012-11-14 02:43:475760TEST_F(ExtensionServiceTest, SyncForUninstalledExternalExtension) {
[email protected]e8b0f252012-05-05 09:44:425761 InitializeEmptyExtensionService();
[email protected]f8aefb132013-10-30 09:29:525762 InitializeExtensionSyncService();
[email protected]e8b0f252012-05-05 09:44:425763 InstallCRXWithLocation(data_dir_.AppendASCII("good.crx"),
[email protected]1d5e58b2013-01-31 08:41:405764 Manifest::EXTERNAL_PREF, INSTALL_NEW);
[email protected]e8b0f252012-05-05 09:44:425765 const Extension* extension = service_->GetInstalledExtension(good_crx);
5766 ASSERT_TRUE(extension);
5767
5768 TestSyncProcessorStub processor;
[email protected]f8aefb132013-10-30 09:29:525769 extension_sync_service_->MergeDataAndStartSyncing(
[email protected]a4a147652012-07-03 23:41:325770 syncer::EXTENSIONS, syncer::SyncDataList(),
[email protected]65f173552012-06-28 22:43:585771 scoped_ptr<syncer::SyncChangeProcessor>(new TestSyncProcessorStub),
5772 scoped_ptr<syncer::SyncErrorFactory>(new syncer::SyncErrorFactoryMock()));
[email protected]e8b0f252012-05-05 09:44:425773
5774 UninstallExtension(good_crx, false);
5775 EXPECT_TRUE(service_->IsExternalExtensionUninstalled(good_crx));
5776
5777 sync_pb::EntitySpecifics specifics;
5778 sync_pb::AppSpecifics* app_specifics = specifics.mutable_app();
5779 sync_pb::ExtensionSpecifics* extension_specifics =
5780 app_specifics->mutable_extension();
5781 extension_specifics->set_id(good_crx);
5782 extension_specifics->set_version("1.0");
[email protected]386db3572013-11-08 12:26:445783 extension_specifics->set_enabled(true);
[email protected]e8b0f252012-05-05 09:44:425784
[email protected]65f173552012-06-28 22:43:585785 syncer::SyncData sync_data =
5786 syncer::SyncData::CreateLocalData(good_crx, "Name", specifics);
[email protected]b78170f2012-07-11 03:34:265787 syncer::SyncChange sync_change(FROM_HERE,
5788 syncer::SyncChange::ACTION_UPDATE,
5789 sync_data);
[email protected]65f173552012-06-28 22:43:585790 syncer::SyncChangeList list(1);
[email protected]e8b0f252012-05-05 09:44:425791 list[0] = sync_change;
5792
[email protected]f8aefb132013-10-30 09:29:525793 extension_sync_service_->ProcessSyncChanges(FROM_HERE, list);
[email protected]e8b0f252012-05-05 09:44:425794 EXPECT_TRUE(service_->IsExternalExtensionUninstalled(good_crx));
5795}
5796
[email protected]d9a61e12012-11-14 02:43:475797TEST_F(ExtensionServiceTest, GetSyncAppDataUserSettings) {
[email protected]168389f2011-12-20 17:12:485798 InitializeEmptyExtensionService();
[email protected]f8aefb132013-10-30 09:29:525799 InitializeExtensionSyncService();
[email protected]168389f2011-12-20 17:12:485800 const Extension* app =
5801 PackAndInstallCRX(data_dir_.AppendASCII("app"), INSTALL_NEW);
5802 ASSERT_TRUE(app);
5803 ASSERT_TRUE(app->is_app());
5804
5805 TestSyncProcessorStub processor;
[email protected]f8aefb132013-10-30 09:29:525806 extension_sync_service_->MergeDataAndStartSyncing(syncer::APPS,
5807 syncer::SyncDataList(),
[email protected]65f173552012-06-28 22:43:585808 scoped_ptr<syncer::SyncChangeProcessor>(new TestSyncProcessorStub),
5809 scoped_ptr<syncer::SyncErrorFactory>(new syncer::SyncErrorFactoryMock()));
[email protected]168389f2011-12-20 17:12:485810
[email protected]36b643212012-09-07 12:53:005811 syncer::StringOrdinal initial_ordinal =
5812 syncer::StringOrdinal::CreateInitialOrdinal();
[email protected]168389f2011-12-20 17:12:485813 {
[email protected]f8aefb132013-10-30 09:29:525814 syncer::SyncDataList list = extension_sync_service_->GetAllSyncData(
5815 syncer::APPS);
[email protected]168389f2011-12-20 17:12:485816 ASSERT_EQ(list.size(), 1U);
[email protected]5db9ada2012-04-11 13:48:205817
5818 extensions::AppSyncData app_sync_data(list[0]);
[email protected]36b643212012-09-07 12:53:005819 EXPECT_TRUE(initial_ordinal.Equals(app_sync_data.app_launch_ordinal()));
5820 EXPECT_TRUE(initial_ordinal.Equals(app_sync_data.page_ordinal()));
[email protected]168389f2011-12-20 17:12:485821 }
5822
[email protected]74474042013-11-21 12:03:545823 AppSorting* sorting = service_->extension_prefs()->app_sorting();
[email protected]fb82dcd2012-03-21 14:15:465824 sorting->SetAppLaunchOrdinal(app->id(), initial_ordinal.CreateAfter());
[email protected]168389f2011-12-20 17:12:485825 {
[email protected]f8aefb132013-10-30 09:29:525826 syncer::SyncDataList list = extension_sync_service_->GetAllSyncData(
5827 syncer::APPS);
[email protected]168389f2011-12-20 17:12:485828 ASSERT_EQ(list.size(), 1U);
[email protected]5db9ada2012-04-11 13:48:205829
5830 extensions::AppSyncData app_sync_data(list[0]);
5831 EXPECT_TRUE(initial_ordinal.LessThan(app_sync_data.app_launch_ordinal()));
[email protected]36b643212012-09-07 12:53:005832 EXPECT_TRUE(initial_ordinal.Equals(app_sync_data.page_ordinal()));
[email protected]168389f2011-12-20 17:12:485833 }
5834
[email protected]fb82dcd2012-03-21 14:15:465835 sorting->SetPageOrdinal(app->id(), initial_ordinal.CreateAfter());
[email protected]168389f2011-12-20 17:12:485836 {
[email protected]f8aefb132013-10-30 09:29:525837 syncer::SyncDataList list = extension_sync_service_->GetAllSyncData(
5838 syncer::APPS);
[email protected]168389f2011-12-20 17:12:485839 ASSERT_EQ(list.size(), 1U);
[email protected]5db9ada2012-04-11 13:48:205840
5841 extensions::AppSyncData app_sync_data(list[0]);
5842 EXPECT_TRUE(initial_ordinal.LessThan(app_sync_data.app_launch_ordinal()));
5843 EXPECT_TRUE(initial_ordinal.LessThan(app_sync_data.page_ordinal()));
[email protected]168389f2011-12-20 17:12:485844 }
5845}
5846
[email protected]d9a61e12012-11-14 02:43:475847TEST_F(ExtensionServiceTest, GetSyncAppDataUserSettingsOnExtensionMoved) {
[email protected]168389f2011-12-20 17:12:485848 InitializeEmptyExtensionService();
[email protected]f8aefb132013-10-30 09:29:525849 InitializeExtensionSyncService();
[email protected]168389f2011-12-20 17:12:485850 const size_t kAppCount = 3;
5851 const Extension* apps[kAppCount];
5852 apps[0] = PackAndInstallCRX(data_dir_.AppendASCII("app1"), INSTALL_NEW);
5853 apps[1] = PackAndInstallCRX(data_dir_.AppendASCII("app2"), INSTALL_NEW);
5854 apps[2] = PackAndInstallCRX(data_dir_.AppendASCII("app4"), INSTALL_NEW);
5855 for (size_t i = 0; i < kAppCount; ++i) {
5856 ASSERT_TRUE(apps[i]);
5857 ASSERT_TRUE(apps[i]->is_app());
5858 }
5859
5860 TestSyncProcessorStub processor;
[email protected]f8aefb132013-10-30 09:29:525861 extension_sync_service_->MergeDataAndStartSyncing(syncer::APPS,
5862 syncer::SyncDataList(),
[email protected]65f173552012-06-28 22:43:585863 scoped_ptr<syncer::SyncChangeProcessor>(new TestSyncProcessorStub),
5864 scoped_ptr<syncer::SyncErrorFactory>(new syncer::SyncErrorFactoryMock()));
[email protected]168389f2011-12-20 17:12:485865
5866 service_->OnExtensionMoved(apps[0]->id(), apps[1]->id(), apps[2]->id());
5867 {
[email protected]f8aefb132013-10-30 09:29:525868 syncer::SyncDataList list = extension_sync_service_->GetAllSyncData(
5869 syncer::APPS);
[email protected]168389f2011-12-20 17:12:485870 ASSERT_EQ(list.size(), 3U);
[email protected]5db9ada2012-04-11 13:48:205871
5872 extensions::AppSyncData data[kAppCount];
[email protected]bb05cae12012-09-06 00:37:525873 for (size_t i = 0; i < kAppCount; ++i) {
[email protected]5db9ada2012-04-11 13:48:205874 data[i] = extensions::AppSyncData(list[i]);
5875 }
[email protected]168389f2011-12-20 17:12:485876
5877 // The sync data is not always in the same order our apps were installed in,
5878 // so we do that sorting here so we can make sure the values are changed as
5879 // expected.
[email protected]36b643212012-09-07 12:53:005880 syncer::StringOrdinal app_launch_ordinals[kAppCount];
[email protected]168389f2011-12-20 17:12:485881 for (size_t i = 0; i < kAppCount; ++i) {
5882 for (size_t j = 0; j < kAppCount; ++j) {
5883 if (apps[i]->id() == data[j].id())
5884 app_launch_ordinals[i] = data[j].app_launch_ordinal();
5885 }
5886 }
5887
5888 EXPECT_TRUE(app_launch_ordinals[1].LessThan(app_launch_ordinals[0]));
5889 EXPECT_TRUE(app_launch_ordinals[0].LessThan(app_launch_ordinals[2]));
5890 }
5891}
5892
[email protected]d9a61e12012-11-14 02:43:475893TEST_F(ExtensionServiceTest, GetSyncDataList) {
[email protected]b05fb9ff2011-04-23 00:07:565894 InitializeEmptyExtensionService();
[email protected]f8aefb132013-10-30 09:29:525895 InitializeExtensionSyncService();
[email protected]8f512c72011-11-22 21:02:505896 InstallCRX(data_dir_.AppendASCII("good.crx"), INSTALL_NEW);
5897 InstallCRX(data_dir_.AppendASCII("page_action.crx"), INSTALL_NEW);
5898 InstallCRX(data_dir_.AppendASCII("theme.crx"), INSTALL_NEW);
5899 InstallCRX(data_dir_.AppendASCII("theme2.crx"), INSTALL_NEW);
[email protected]b05fb9ff2011-04-23 00:07:565900
[email protected]3bdba0d2011-08-23 07:17:305901 TestSyncProcessorStub processor;
[email protected]f8aefb132013-10-30 09:29:525902 extension_sync_service_->MergeDataAndStartSyncing(
5903 syncer::APPS, syncer::SyncDataList(),
[email protected]65f173552012-06-28 22:43:585904 scoped_ptr<syncer::SyncChangeProcessor>(new TestSyncProcessorStub),
5905 scoped_ptr<syncer::SyncErrorFactory>(new syncer::SyncErrorFactoryMock()));
[email protected]f8aefb132013-10-30 09:29:525906 extension_sync_service_->MergeDataAndStartSyncing(
[email protected]a4a147652012-07-03 23:41:325907 syncer::EXTENSIONS, syncer::SyncDataList(),
[email protected]65f173552012-06-28 22:43:585908 scoped_ptr<syncer::SyncChangeProcessor>(new TestSyncProcessorStub),
5909 scoped_ptr<syncer::SyncErrorFactory>(new syncer::SyncErrorFactoryMock()));
[email protected]3bdba0d2011-08-23 07:17:305910
[email protected]44d62b62012-04-11 00:06:035911 service_->DisableExtension(page_action, Extension::DISABLE_USER_ACTION);
[email protected]45b6fee2011-05-03 09:41:315912 TerminateExtension(theme2_crx);
[email protected]b05fb9ff2011-04-23 00:07:565913
[email protected]f8aefb132013-10-30 09:29:525914 EXPECT_EQ(0u, extension_sync_service_->GetAllSyncData(syncer::APPS).size());
5915 EXPECT_EQ(2u, extension_sync_service_->
5916 GetAllSyncData(syncer::EXTENSIONS).size());
[email protected]b05fb9ff2011-04-23 00:07:565917}
5918
[email protected]d9a61e12012-11-14 02:43:475919TEST_F(ExtensionServiceTest, ProcessSyncDataUninstall) {
[email protected]90310d92011-04-17 07:35:045920 InitializeEmptyExtensionService();
[email protected]f8aefb132013-10-30 09:29:525921 InitializeExtensionSyncService();
[email protected]3bdba0d2011-08-23 07:17:305922 TestSyncProcessorStub processor;
[email protected]f8aefb132013-10-30 09:29:525923 extension_sync_service_->MergeDataAndStartSyncing(
[email protected]a4a147652012-07-03 23:41:325924 syncer::EXTENSIONS, syncer::SyncDataList(),
[email protected]65f173552012-06-28 22:43:585925 scoped_ptr<syncer::SyncChangeProcessor>(new TestSyncProcessorStub),
5926 scoped_ptr<syncer::SyncErrorFactory>(new syncer::SyncErrorFactoryMock()));
[email protected]90310d92011-04-17 07:35:045927
[email protected]3bdba0d2011-08-23 07:17:305928 sync_pb::EntitySpecifics specifics;
[email protected]4557d222012-03-04 23:33:365929 sync_pb::ExtensionSpecifics* ext_specifics = specifics.mutable_extension();
[email protected]3bdba0d2011-08-23 07:17:305930 ext_specifics->set_id(good_crx);
5931 ext_specifics->set_version("1.0");
[email protected]65f173552012-06-28 22:43:585932 syncer::SyncData sync_data =
5933 syncer::SyncData::CreateLocalData(good_crx, "Name", specifics);
[email protected]b78170f2012-07-11 03:34:265934 syncer::SyncChange sync_change(FROM_HERE,
5935 syncer::SyncChange::ACTION_DELETE,
5936 sync_data);
[email protected]65f173552012-06-28 22:43:585937 syncer::SyncChangeList list(1);
[email protected]3bdba0d2011-08-23 07:17:305938 list[0] = sync_change;
[email protected]90310d92011-04-17 07:35:045939
5940 // Should do nothing.
[email protected]f8aefb132013-10-30 09:29:525941 extension_sync_service_->ProcessSyncChanges(FROM_HERE, list);
[email protected]3bdba0d2011-08-23 07:17:305942 EXPECT_FALSE(service_->GetExtensionById(good_crx, true));
[email protected]90310d92011-04-17 07:35:045943
5944 // Install the extension.
[email protected]650b2d52013-02-10 03:41:455945 base::FilePath extension_path = data_dir_.AppendASCII("good.crx");
[email protected]8f512c72011-11-22 21:02:505946 InstallCRX(extension_path, INSTALL_NEW);
[email protected]90310d92011-04-17 07:35:045947 EXPECT_TRUE(service_->GetExtensionById(good_crx, true));
5948
5949 // Should uninstall the extension.
[email protected]f8aefb132013-10-30 09:29:525950 extension_sync_service_->ProcessSyncChanges(FROM_HERE, list);
[email protected]90310d92011-04-17 07:35:045951 EXPECT_FALSE(service_->GetExtensionById(good_crx, true));
5952
5953 // Should again do nothing.
[email protected]f8aefb132013-10-30 09:29:525954 extension_sync_service_->ProcessSyncChanges(FROM_HERE, list);
[email protected]3bdba0d2011-08-23 07:17:305955 EXPECT_FALSE(service_->GetExtensionById(good_crx, true));
[email protected]90310d92011-04-17 07:35:045956}
5957
[email protected]d9a61e12012-11-14 02:43:475958TEST_F(ExtensionServiceTest, ProcessSyncDataWrongType) {
[email protected]96e989b2011-08-30 19:35:065959 InitializeEmptyExtensionService();
[email protected]f8aefb132013-10-30 09:29:525960 InitializeExtensionSyncService();
[email protected]96e989b2011-08-30 19:35:065961
5962 // Install the extension.
[email protected]650b2d52013-02-10 03:41:455963 base::FilePath extension_path = data_dir_.AppendASCII("good.crx");
[email protected]8f512c72011-11-22 21:02:505964 InstallCRX(extension_path, INSTALL_NEW);
[email protected]96e989b2011-08-30 19:35:065965 EXPECT_TRUE(service_->GetExtensionById(good_crx, true));
5966
5967 sync_pb::EntitySpecifics specifics;
[email protected]4557d222012-03-04 23:33:365968 sync_pb::AppSpecifics* app_specifics = specifics.mutable_app();
[email protected]96e989b2011-08-30 19:35:065969 sync_pb::ExtensionSpecifics* extension_specifics =
5970 app_specifics->mutable_extension();
5971 extension_specifics->set_id(good_crx);
5972 extension_specifics->set_version(
5973 service_->GetInstalledExtension(good_crx)->version()->GetString());
5974
5975 {
[email protected]386db3572013-11-08 12:26:445976 extension_specifics->set_enabled(true);
[email protected]65f173552012-06-28 22:43:585977 syncer::SyncData sync_data =
5978 syncer::SyncData::CreateLocalData(good_crx, "Name", specifics);
[email protected]b78170f2012-07-11 03:34:265979 syncer::SyncChange sync_change(FROM_HERE,
5980 syncer::SyncChange::ACTION_DELETE,
5981 sync_data);
[email protected]65f173552012-06-28 22:43:585982 syncer::SyncChangeList list(1);
[email protected]96e989b2011-08-30 19:35:065983 list[0] = sync_change;
5984
5985 // Should do nothing
[email protected]f8aefb132013-10-30 09:29:525986 extension_sync_service_->ProcessSyncChanges(FROM_HERE, list);
[email protected]96e989b2011-08-30 19:35:065987 EXPECT_TRUE(service_->GetExtensionById(good_crx, true));
5988 }
5989
5990 {
[email protected]386db3572013-11-08 12:26:445991 extension_specifics->set_enabled(false);
[email protected]65f173552012-06-28 22:43:585992 syncer::SyncData sync_data =
5993 syncer::SyncData::CreateLocalData(good_crx, "Name", specifics);
[email protected]b78170f2012-07-11 03:34:265994 syncer::SyncChange sync_change(FROM_HERE,
5995 syncer::SyncChange::ACTION_UPDATE,
5996 sync_data);
[email protected]65f173552012-06-28 22:43:585997 syncer::SyncChangeList list(1);
[email protected]96e989b2011-08-30 19:35:065998 list[0] = sync_change;
5999
6000 // Should again do nothing.
[email protected]f8aefb132013-10-30 09:29:526001 extension_sync_service_->ProcessSyncChanges(FROM_HERE, list);
[email protected]96e989b2011-08-30 19:35:066002 EXPECT_TRUE(service_->GetExtensionById(good_crx, false));
6003 }
6004}
6005
[email protected]d9a61e12012-11-14 02:43:476006TEST_F(ExtensionServiceTest, ProcessSyncDataSettings) {
[email protected]90310d92011-04-17 07:35:046007 InitializeEmptyExtensionService();
[email protected]98b6d942013-11-10 00:34:076008 InitializeProcessManager();
[email protected]f8aefb132013-10-30 09:29:526009 InitializeExtensionSyncService();
[email protected]3bdba0d2011-08-23 07:17:306010 TestSyncProcessorStub processor;
[email protected]f8aefb132013-10-30 09:29:526011 extension_sync_service_->MergeDataAndStartSyncing(
[email protected]a4a147652012-07-03 23:41:326012 syncer::EXTENSIONS, syncer::SyncDataList(),
[email protected]65f173552012-06-28 22:43:586013 scoped_ptr<syncer::SyncChangeProcessor>(new TestSyncProcessorStub),
6014 scoped_ptr<syncer::SyncErrorFactory>(new syncer::SyncErrorFactoryMock()));
[email protected]90310d92011-04-17 07:35:046015
[email protected]8f512c72011-11-22 21:02:506016 InstallCRX(data_dir_.AppendASCII("good.crx"), INSTALL_NEW);
[email protected]90310d92011-04-17 07:35:046017 EXPECT_TRUE(service_->IsExtensionEnabled(good_crx));
[email protected]1d5cf4142014-01-24 18:25:226018 EXPECT_FALSE(extensions::util::IsIncognitoEnabled(good_crx, profile_.get()));
[email protected]90310d92011-04-17 07:35:046019
[email protected]3bdba0d2011-08-23 07:17:306020 sync_pb::EntitySpecifics specifics;
[email protected]4557d222012-03-04 23:33:366021 sync_pb::ExtensionSpecifics* ext_specifics = specifics.mutable_extension();
[email protected]3bdba0d2011-08-23 07:17:306022 ext_specifics->set_id(good_crx);
6023 ext_specifics->set_version(
6024 service_->GetInstalledExtension(good_crx)->version()->GetString());
[email protected]386db3572013-11-08 12:26:446025 ext_specifics->set_enabled(false);
[email protected]90310d92011-04-17 07:35:046026
[email protected]3bdba0d2011-08-23 07:17:306027 {
[email protected]65f173552012-06-28 22:43:586028 syncer::SyncData sync_data =
6029 syncer::SyncData::CreateLocalData(good_crx, "Name", specifics);
[email protected]b78170f2012-07-11 03:34:266030 syncer::SyncChange sync_change(FROM_HERE,
6031 syncer::SyncChange::ACTION_UPDATE,
6032 sync_data);
[email protected]65f173552012-06-28 22:43:586033 syncer::SyncChangeList list(1);
[email protected]3bdba0d2011-08-23 07:17:306034 list[0] = sync_change;
[email protected]f8aefb132013-10-30 09:29:526035 extension_sync_service_->ProcessSyncChanges(FROM_HERE, list);
[email protected]386db3572013-11-08 12:26:446036 EXPECT_FALSE(service_->IsExtensionEnabled(good_crx));
[email protected]1d5cf4142014-01-24 18:25:226037 EXPECT_FALSE(
6038 extensions::util::IsIncognitoEnabled(good_crx, profile_.get()));
[email protected]3bdba0d2011-08-23 07:17:306039 }
[email protected]90310d92011-04-17 07:35:046040
[email protected]3bdba0d2011-08-23 07:17:306041 {
[email protected]386db3572013-11-08 12:26:446042 ext_specifics->set_enabled(true);
[email protected]3bdba0d2011-08-23 07:17:306043 ext_specifics->set_incognito_enabled(true);
[email protected]65f173552012-06-28 22:43:586044 syncer::SyncData sync_data =
6045 syncer::SyncData::CreateLocalData(good_crx, "Name", specifics);
[email protected]b78170f2012-07-11 03:34:266046 syncer::SyncChange sync_change(FROM_HERE,
6047 syncer::SyncChange::ACTION_UPDATE,
6048 sync_data);
[email protected]65f173552012-06-28 22:43:586049 syncer::SyncChangeList list(1);
[email protected]3bdba0d2011-08-23 07:17:306050 list[0] = sync_change;
[email protected]f8aefb132013-10-30 09:29:526051 extension_sync_service_->ProcessSyncChanges(FROM_HERE, list);
[email protected]60d8d382013-10-27 02:03:366052 EXPECT_TRUE(service_->IsExtensionEnabled(good_crx));
[email protected]1d5cf4142014-01-24 18:25:226053 EXPECT_TRUE(extensions::util::IsIncognitoEnabled(good_crx, profile_.get()));
[email protected]3bdba0d2011-08-23 07:17:306054 }
[email protected]90310d92011-04-17 07:35:046055
[email protected]386db3572013-11-08 12:26:446056 {
6057 ext_specifics->set_enabled(false);
6058 ext_specifics->set_incognito_enabled(true);
6059 syncer::SyncData sync_data =
6060 syncer::SyncData::CreateLocalData(good_crx, "Name", specifics);
6061 syncer::SyncChange sync_change(FROM_HERE,
6062 syncer::SyncChange::ACTION_UPDATE,
6063 sync_data);
6064 syncer::SyncChangeList list(1);
6065 list[0] = sync_change;
6066 extension_sync_service_->ProcessSyncChanges(FROM_HERE, list);
6067 EXPECT_FALSE(service_->IsExtensionEnabled(good_crx));
[email protected]1d5cf4142014-01-24 18:25:226068 EXPECT_TRUE(extensions::util::IsIncognitoEnabled(good_crx, profile_.get()));
[email protected]386db3572013-11-08 12:26:446069 }
6070
[email protected]90310d92011-04-17 07:35:046071 EXPECT_FALSE(service_->pending_extension_manager()->IsIdPending(good_crx));
6072}
6073
[email protected]d9a61e12012-11-14 02:43:476074TEST_F(ExtensionServiceTest, ProcessSyncDataTerminatedExtension) {
[email protected]fa2416f2011-05-03 08:41:206075 InitializeExtensionServiceWithUpdater();
[email protected]f8aefb132013-10-30 09:29:526076 InitializeExtensionSyncService();
[email protected]3bdba0d2011-08-23 07:17:306077 TestSyncProcessorStub processor;
[email protected]f8aefb132013-10-30 09:29:526078 extension_sync_service_->MergeDataAndStartSyncing(
[email protected]a4a147652012-07-03 23:41:326079 syncer::EXTENSIONS, syncer::SyncDataList(),
[email protected]65f173552012-06-28 22:43:586080 scoped_ptr<syncer::SyncChangeProcessor>(new TestSyncProcessorStub),
6081 scoped_ptr<syncer::SyncErrorFactory>(new syncer::SyncErrorFactoryMock()));
[email protected]fa2416f2011-05-03 08:41:206082
[email protected]8f512c72011-11-22 21:02:506083 InstallCRX(data_dir_.AppendASCII("good.crx"), INSTALL_NEW);
[email protected]fa2416f2011-05-03 08:41:206084 TerminateExtension(good_crx);
6085 EXPECT_TRUE(service_->IsExtensionEnabled(good_crx));
[email protected]1d5cf4142014-01-24 18:25:226086 EXPECT_FALSE(extensions::util::IsIncognitoEnabled(good_crx, profile_.get()));
[email protected]fa2416f2011-05-03 08:41:206087
[email protected]3bdba0d2011-08-23 07:17:306088 sync_pb::EntitySpecifics specifics;
[email protected]4557d222012-03-04 23:33:366089 sync_pb::ExtensionSpecifics* ext_specifics = specifics.mutable_extension();
[email protected]3bdba0d2011-08-23 07:17:306090 ext_specifics->set_id(good_crx);
6091 ext_specifics->set_version(
6092 service_->GetInstalledExtension(good_crx)->version()->GetString());
[email protected]386db3572013-11-08 12:26:446093 ext_specifics->set_enabled(false);
[email protected]3bdba0d2011-08-23 07:17:306094 ext_specifics->set_incognito_enabled(true);
[email protected]65f173552012-06-28 22:43:586095 syncer::SyncData sync_data =
6096 syncer::SyncData::CreateLocalData(good_crx, "Name", specifics);
[email protected]b78170f2012-07-11 03:34:266097 syncer::SyncChange sync_change(FROM_HERE,
6098 syncer::SyncChange::ACTION_UPDATE,
6099 sync_data);
[email protected]65f173552012-06-28 22:43:586100 syncer::SyncChangeList list(1);
[email protected]3bdba0d2011-08-23 07:17:306101 list[0] = sync_change;
6102
[email protected]f8aefb132013-10-30 09:29:526103 extension_sync_service_->ProcessSyncChanges(FROM_HERE, list);
[email protected]386db3572013-11-08 12:26:446104 EXPECT_FALSE(service_->IsExtensionEnabled(good_crx));
[email protected]1d5cf4142014-01-24 18:25:226105 EXPECT_TRUE(extensions::util::IsIncognitoEnabled(good_crx, profile_.get()));
[email protected]fa2416f2011-05-03 08:41:206106
6107 EXPECT_FALSE(service_->pending_extension_manager()->IsIdPending(good_crx));
6108}
6109
[email protected]d9a61e12012-11-14 02:43:476110TEST_F(ExtensionServiceTest, ProcessSyncDataVersionCheck) {
[email protected]90310d92011-04-17 07:35:046111 InitializeExtensionServiceWithUpdater();
[email protected]f8aefb132013-10-30 09:29:526112 InitializeExtensionSyncService();
[email protected]3bdba0d2011-08-23 07:17:306113 TestSyncProcessorStub processor;
[email protected]f8aefb132013-10-30 09:29:526114 extension_sync_service_->MergeDataAndStartSyncing(
[email protected]a4a147652012-07-03 23:41:326115 syncer::EXTENSIONS, syncer::SyncDataList(),
[email protected]65f173552012-06-28 22:43:586116 scoped_ptr<syncer::SyncChangeProcessor>(new TestSyncProcessorStub),
6117 scoped_ptr<syncer::SyncErrorFactory>(new syncer::SyncErrorFactoryMock()));
[email protected]90310d92011-04-17 07:35:046118
[email protected]8f512c72011-11-22 21:02:506119 InstallCRX(data_dir_.AppendASCII("good.crx"), INSTALL_NEW);
[email protected]90310d92011-04-17 07:35:046120 EXPECT_TRUE(service_->IsExtensionEnabled(good_crx));
[email protected]1d5cf4142014-01-24 18:25:226121 EXPECT_FALSE(extensions::util::IsIncognitoEnabled(good_crx, profile_.get()));
[email protected]90310d92011-04-17 07:35:046122
[email protected]3bdba0d2011-08-23 07:17:306123 sync_pb::EntitySpecifics specifics;
[email protected]4557d222012-03-04 23:33:366124 sync_pb::ExtensionSpecifics* ext_specifics = specifics.mutable_extension();
[email protected]3bdba0d2011-08-23 07:17:306125 ext_specifics->set_id(good_crx);
[email protected]386db3572013-11-08 12:26:446126 ext_specifics->set_enabled(true);
[email protected]90310d92011-04-17 07:35:046127
[email protected]3bdba0d2011-08-23 07:17:306128 {
6129 ext_specifics->set_version(
6130 service_->GetInstalledExtension(good_crx)->version()->GetString());
[email protected]65f173552012-06-28 22:43:586131 syncer::SyncData sync_data =
6132 syncer::SyncData::CreateLocalData(good_crx, "Name", specifics);
[email protected]b78170f2012-07-11 03:34:266133 syncer::SyncChange sync_change(FROM_HERE,
6134 syncer::SyncChange::ACTION_UPDATE,
6135 sync_data);
[email protected]65f173552012-06-28 22:43:586136 syncer::SyncChangeList list(1);
[email protected]3bdba0d2011-08-23 07:17:306137 list[0] = sync_change;
6138
6139 // Should do nothing if extension version == sync version.
[email protected]f8aefb132013-10-30 09:29:526140 extension_sync_service_->ProcessSyncChanges(FROM_HERE, list);
[email protected]3bdba0d2011-08-23 07:17:306141 EXPECT_FALSE(service_->updater()->WillCheckSoon());
6142 }
[email protected]90310d92011-04-17 07:35:046143
6144 // Should do nothing if extension version > sync version (but see
[email protected]3bdba0d2011-08-23 07:17:306145 // the TODO in ProcessExtensionSyncData).
[email protected]90310d92011-04-17 07:35:046146 {
[email protected]3bdba0d2011-08-23 07:17:306147 ext_specifics->set_version("0.0.0.0");
[email protected]65f173552012-06-28 22:43:586148 syncer::SyncData sync_data =
6149 syncer::SyncData::CreateLocalData(good_crx, "Name", specifics);
[email protected]b78170f2012-07-11 03:34:266150 syncer::SyncChange sync_change(FROM_HERE,
6151 syncer::SyncChange::ACTION_UPDATE,
6152 sync_data);
[email protected]65f173552012-06-28 22:43:586153 syncer::SyncChangeList list(1);
[email protected]3bdba0d2011-08-23 07:17:306154 list[0] = sync_change;
6155
[email protected]f8aefb132013-10-30 09:29:526156 extension_sync_service_->ProcessSyncChanges(FROM_HERE, list);
[email protected]90310d92011-04-17 07:35:046157 EXPECT_FALSE(service_->updater()->WillCheckSoon());
6158 }
6159
6160 // Should kick off an update if extension version < sync version.
6161 {
[email protected]3bdba0d2011-08-23 07:17:306162 ext_specifics->set_version("9.9.9.9");
[email protected]65f173552012-06-28 22:43:586163 syncer::SyncData sync_data =
6164 syncer::SyncData::CreateLocalData(good_crx, "Name", specifics);
[email protected]b78170f2012-07-11 03:34:266165 syncer::SyncChange sync_change(FROM_HERE,
6166 syncer::SyncChange::ACTION_UPDATE,
6167 sync_data);
[email protected]65f173552012-06-28 22:43:586168 syncer::SyncChangeList list(1);
[email protected]3bdba0d2011-08-23 07:17:306169 list[0] = sync_change;
6170
[email protected]f8aefb132013-10-30 09:29:526171 extension_sync_service_->ProcessSyncChanges(FROM_HERE, list);
[email protected]90310d92011-04-17 07:35:046172 EXPECT_TRUE(service_->updater()->WillCheckSoon());
6173 }
6174
6175 EXPECT_FALSE(service_->pending_extension_manager()->IsIdPending(good_crx));
6176}
6177
[email protected]d9a61e12012-11-14 02:43:476178TEST_F(ExtensionServiceTest, ProcessSyncDataNotInstalled) {
[email protected]90310d92011-04-17 07:35:046179 InitializeExtensionServiceWithUpdater();
[email protected]f8aefb132013-10-30 09:29:526180 InitializeExtensionSyncService();
[email protected]3bdba0d2011-08-23 07:17:306181 TestSyncProcessorStub processor;
[email protected]f8aefb132013-10-30 09:29:526182 extension_sync_service_->MergeDataAndStartSyncing(
[email protected]a4a147652012-07-03 23:41:326183 syncer::EXTENSIONS, syncer::SyncDataList(),
[email protected]65f173552012-06-28 22:43:586184 scoped_ptr<syncer::SyncChangeProcessor>(new TestSyncProcessorStub),
6185 scoped_ptr<syncer::SyncErrorFactory>(new syncer::SyncErrorFactoryMock()));
[email protected]90310d92011-04-17 07:35:046186
[email protected]3bdba0d2011-08-23 07:17:306187 sync_pb::EntitySpecifics specifics;
[email protected]4557d222012-03-04 23:33:366188 sync_pb::ExtensionSpecifics* ext_specifics = specifics.mutable_extension();
[email protected]3bdba0d2011-08-23 07:17:306189 ext_specifics->set_id(good_crx);
[email protected]386db3572013-11-08 12:26:446190 ext_specifics->set_enabled(false);
[email protected]3bdba0d2011-08-23 07:17:306191 ext_specifics->set_incognito_enabled(true);
6192 ext_specifics->set_update_url("http://www.google.com/");
6193 ext_specifics->set_version("1.2.3.4");
[email protected]65f173552012-06-28 22:43:586194 syncer::SyncData sync_data =
6195 syncer::SyncData::CreateLocalData(good_crx, "Name", specifics);
[email protected]b78170f2012-07-11 03:34:266196 syncer::SyncChange sync_change(FROM_HERE,
6197 syncer::SyncChange::ACTION_UPDATE,
6198 sync_data);
[email protected]65f173552012-06-28 22:43:586199 syncer::SyncChangeList list(1);
[email protected]3bdba0d2011-08-23 07:17:306200 list[0] = sync_change;
6201
[email protected]90310d92011-04-17 07:35:046202
[email protected]06f92562011-04-29 19:27:316203 EXPECT_TRUE(service_->IsExtensionEnabled(good_crx));
[email protected]1d5cf4142014-01-24 18:25:226204 EXPECT_FALSE(extensions::util::IsIncognitoEnabled(good_crx, profile_.get()));
[email protected]f8aefb132013-10-30 09:29:526205 extension_sync_service_->ProcessSyncChanges(FROM_HERE, list);
[email protected]90310d92011-04-17 07:35:046206 EXPECT_TRUE(service_->updater()->WillCheckSoon());
[email protected]386db3572013-11-08 12:26:446207 EXPECT_FALSE(service_->IsExtensionEnabled(good_crx));
[email protected]1d5cf4142014-01-24 18:25:226208 EXPECT_TRUE(extensions::util::IsIncognitoEnabled(good_crx, profile_.get()));
[email protected]90310d92011-04-17 07:35:046209
[email protected]3f213ad2012-07-26 23:39:416210 const extensions::PendingExtensionInfo* info;
[email protected]51a3bf8b2012-06-08 22:53:066211 EXPECT_TRUE((info = service_->pending_extension_manager()->
6212 GetById(good_crx)));
6213 EXPECT_EQ(ext_specifics->update_url(), info->update_url().spec());
6214 EXPECT_TRUE(info->is_from_sync());
6215 EXPECT_TRUE(info->install_silently());
[email protected]1d5e58b2013-01-31 08:41:406216 EXPECT_EQ(Manifest::INTERNAL, info->install_source());
[email protected]90310d92011-04-17 07:35:046217 // TODO(akalin): Figure out a way to test |info.ShouldAllowInstall()|.
6218}
6219
[email protected]d9a61e12012-11-14 02:43:476220TEST_F(ExtensionServiceTest, InstallPriorityExternalUpdateUrl) {
[email protected]8a87a5332011-08-11 17:54:596221 InitializeEmptyExtensionService();
6222
[email protected]650b2d52013-02-10 03:41:456223 base::FilePath path = data_dir_.AppendASCII("good.crx");
[email protected]8f512c72011-11-22 21:02:506224 InstallCRX(path, INSTALL_NEW);
[email protected]8a87a5332011-08-11 17:54:596225 ValidatePrefKeyCount(1u);
6226 ValidateIntegerPref(good_crx, "state", Extension::ENABLED);
[email protected]1d5e58b2013-01-31 08:41:406227 ValidateIntegerPref(good_crx, "location", Manifest::INTERNAL);
[email protected]8a87a5332011-08-11 17:54:596228
[email protected]3f213ad2012-07-26 23:39:416229 extensions::PendingExtensionManager* pending =
6230 service_->pending_extension_manager();
[email protected]8a87a5332011-08-11 17:54:596231 EXPECT_FALSE(pending->IsIdPending(kGoodId));
6232
6233 // Skip install when the location is the same.
[email protected]9060d8b02012-01-13 02:14:306234 EXPECT_FALSE(
6235 service_->OnExternalExtensionUpdateUrlFound(
[email protected]464213a2013-10-15 01:06:486236 kGoodId, GURL(kGoodUpdateURL), Manifest::INTERNAL,
6237 Extension::NO_FLAGS, false));
[email protected]8a87a5332011-08-11 17:54:596238 EXPECT_FALSE(pending->IsIdPending(kGoodId));
[email protected]9060d8b02012-01-13 02:14:306239
6240 // Install when the location has higher priority.
6241 EXPECT_TRUE(
6242 service_->OnExternalExtensionUpdateUrlFound(
[email protected]464213a2013-10-15 01:06:486243 kGoodId, GURL(kGoodUpdateURL), Manifest::EXTERNAL_POLICY_DOWNLOAD,
6244 Extension::NO_FLAGS, false));
[email protected]8a87a5332011-08-11 17:54:596245 EXPECT_TRUE(pending->IsIdPending(kGoodId));
[email protected]9060d8b02012-01-13 02:14:306246
6247 // Try the low priority again. Should be rejected.
6248 EXPECT_FALSE(
6249 service_->OnExternalExtensionUpdateUrlFound(
[email protected]464213a2013-10-15 01:06:486250 kGoodId, GURL(kGoodUpdateURL), Manifest::EXTERNAL_PREF_DOWNLOAD,
6251 Extension::NO_FLAGS, false));
[email protected]9060d8b02012-01-13 02:14:306252 // The existing record should still be present in the pending extension
6253 // manager.
6254 EXPECT_TRUE(pending->IsIdPending(kGoodId));
6255
[email protected]8a87a5332011-08-11 17:54:596256 pending->Remove(kGoodId);
[email protected]9060d8b02012-01-13 02:14:306257
6258 // Skip install when the location has the same priority as the installed
6259 // location.
6260 EXPECT_FALSE(service_->OnExternalExtensionUpdateUrlFound(
[email protected]464213a2013-10-15 01:06:486261 kGoodId, GURL(kGoodUpdateURL), Manifest::INTERNAL,
6262 Extension::NO_FLAGS, false));
[email protected]9060d8b02012-01-13 02:14:306263
[email protected]8a87a5332011-08-11 17:54:596264 EXPECT_FALSE(pending->IsIdPending(kGoodId));
6265}
6266
[email protected]d9a61e12012-11-14 02:43:476267TEST_F(ExtensionServiceTest, InstallPriorityExternalLocalFile) {
[email protected]c5e4a2222014-01-03 16:06:136268 Version older_version("0.1.0.0");
6269 Version newer_version("2.0.0.0");
[email protected]9060d8b02012-01-13 02:14:306270
6271 // We don't want the extension to be installed. A path that doesn't
6272 // point to a valid CRX ensures this.
[email protected]650b2d52013-02-10 03:41:456273 const base::FilePath kInvalidPathToCrx = base::FilePath();
[email protected]9060d8b02012-01-13 02:14:306274
6275 const int kCreationFlags = 0;
6276 const bool kDontMarkAcknowledged = false;
6277
6278 InitializeEmptyExtensionService();
6279
6280 // The test below uses install source constants to test that
6281 // priority is enforced. It assumes a specific ranking of install
6282 // sources: Registry (EXTERNAL_REGISTRY) overrides external pref
6283 // (EXTERNAL_PREF), and external pref overrides user install (INTERNAL).
6284 // The following assertions verify these assumptions:
[email protected]1d5e58b2013-01-31 08:41:406285 ASSERT_EQ(Manifest::EXTERNAL_REGISTRY,
6286 Manifest::GetHigherPriorityLocation(Manifest::EXTERNAL_REGISTRY,
6287 Manifest::EXTERNAL_PREF));
6288 ASSERT_EQ(Manifest::EXTERNAL_REGISTRY,
6289 Manifest::GetHigherPriorityLocation(Manifest::EXTERNAL_REGISTRY,
6290 Manifest::INTERNAL));
6291 ASSERT_EQ(Manifest::EXTERNAL_PREF,
6292 Manifest::GetHigherPriorityLocation(Manifest::EXTERNAL_PREF,
6293 Manifest::INTERNAL));
[email protected]9060d8b02012-01-13 02:14:306294
[email protected]3f213ad2012-07-26 23:39:416295 extensions::PendingExtensionManager* pending =
6296 service_->pending_extension_manager();
[email protected]9060d8b02012-01-13 02:14:306297 EXPECT_FALSE(pending->IsIdPending(kGoodId));
6298
[email protected]97d6a5c2013-11-11 23:51:246299 {
6300 // Simulate an external source adding the extension as INTERNAL.
6301 content::WindowedNotificationObserver observer(
6302 chrome::NOTIFICATION_CRX_INSTALLER_DONE,
6303 content::NotificationService::AllSources());
6304 EXPECT_TRUE(
6305 service_->OnExternalExtensionFileFound(
6306 kGoodId, &older_version, kInvalidPathToCrx,
6307 Manifest::INTERNAL, kCreationFlags, kDontMarkAcknowledged));
6308 EXPECT_TRUE(pending->IsIdPending(kGoodId));
6309 observer.Wait();
6310 VerifyCrxInstall(kInvalidPathToCrx, INSTALL_FAILED);
6311 }
[email protected]9060d8b02012-01-13 02:14:306312
[email protected]97d6a5c2013-11-11 23:51:246313 {
6314 // Simulate an external source adding the extension as EXTERNAL_PREF.
6315 content::WindowedNotificationObserver observer(
6316 chrome::NOTIFICATION_CRX_INSTALLER_DONE,
6317 content::NotificationService::AllSources());
6318 EXPECT_TRUE(
6319 service_->OnExternalExtensionFileFound(
6320 kGoodId, &older_version, kInvalidPathToCrx,
6321 Manifest::EXTERNAL_PREF, kCreationFlags, kDontMarkAcknowledged));
6322 EXPECT_TRUE(pending->IsIdPending(kGoodId));
6323 observer.Wait();
6324 VerifyCrxInstall(kInvalidPathToCrx, INSTALL_FAILED);
6325 }
[email protected]9060d8b02012-01-13 02:14:306326
6327 // Simulate an external source adding as EXTERNAL_PREF again.
[email protected]4ec0f4b2012-12-07 09:41:466328 // This is rejected because the version and the location are the same as
6329 // the previous installation, which is still pending.
6330 EXPECT_FALSE(
[email protected]9060d8b02012-01-13 02:14:306331 service_->OnExternalExtensionFileFound(
[email protected]12126d372012-07-11 18:40:536332 kGoodId, &older_version, kInvalidPathToCrx,
[email protected]1d5e58b2013-01-31 08:41:406333 Manifest::EXTERNAL_PREF, kCreationFlags, kDontMarkAcknowledged));
[email protected]9060d8b02012-01-13 02:14:306334 EXPECT_TRUE(pending->IsIdPending(kGoodId));
[email protected]9060d8b02012-01-13 02:14:306335
6336 // Try INTERNAL again. Should fail.
6337 EXPECT_FALSE(
6338 service_->OnExternalExtensionFileFound(
[email protected]12126d372012-07-11 18:40:536339 kGoodId, &older_version, kInvalidPathToCrx,
[email protected]1d5e58b2013-01-31 08:41:406340 Manifest::INTERNAL, kCreationFlags, kDontMarkAcknowledged));
[email protected]9060d8b02012-01-13 02:14:306341 EXPECT_TRUE(pending->IsIdPending(kGoodId));
6342
[email protected]97d6a5c2013-11-11 23:51:246343 {
6344 // Now the registry adds the extension.
6345 content::WindowedNotificationObserver observer(
6346 chrome::NOTIFICATION_CRX_INSTALLER_DONE,
6347 content::NotificationService::AllSources());
6348 EXPECT_TRUE(
6349 service_->OnExternalExtensionFileFound(kGoodId,
6350 &older_version,
6351 kInvalidPathToCrx,
6352 Manifest::EXTERNAL_REGISTRY,
6353 kCreationFlags,
6354 kDontMarkAcknowledged));
6355 EXPECT_TRUE(pending->IsIdPending(kGoodId));
6356 observer.Wait();
6357 VerifyCrxInstall(kInvalidPathToCrx, INSTALL_FAILED);
6358 }
[email protected]9060d8b02012-01-13 02:14:306359
6360 // Registry outranks both external pref and internal, so both fail.
6361 EXPECT_FALSE(
6362 service_->OnExternalExtensionFileFound(
[email protected]12126d372012-07-11 18:40:536363 kGoodId, &older_version, kInvalidPathToCrx,
[email protected]1d5e58b2013-01-31 08:41:406364 Manifest::EXTERNAL_PREF, kCreationFlags, kDontMarkAcknowledged));
[email protected]9060d8b02012-01-13 02:14:306365 EXPECT_TRUE(pending->IsIdPending(kGoodId));
6366
6367 EXPECT_FALSE(
6368 service_->OnExternalExtensionFileFound(
[email protected]12126d372012-07-11 18:40:536369 kGoodId, &older_version, kInvalidPathToCrx,
[email protected]1d5e58b2013-01-31 08:41:406370 Manifest::INTERNAL, kCreationFlags, kDontMarkAcknowledged));
[email protected]9060d8b02012-01-13 02:14:306371 EXPECT_TRUE(pending->IsIdPending(kGoodId));
6372
6373 pending->Remove(kGoodId);
6374
6375 // Install the extension.
[email protected]650b2d52013-02-10 03:41:456376 base::FilePath path = data_dir_.AppendASCII("good.crx");
[email protected]9060d8b02012-01-13 02:14:306377 const Extension* ext = InstallCRX(path, INSTALL_NEW);
6378 ValidatePrefKeyCount(1u);
6379 ValidateIntegerPref(good_crx, "state", Extension::ENABLED);
[email protected]1d5e58b2013-01-31 08:41:406380 ValidateIntegerPref(good_crx, "location", Manifest::INTERNAL);
[email protected]9060d8b02012-01-13 02:14:306381
6382 // Now test the logic of OnExternalExtensionFileFound() when the extension
6383 // being added is already installed.
6384
6385 // Tests assume |older_version| is less than the installed version, and
6386 // |newer_version| is greater. Verify this:
[email protected]12126d372012-07-11 18:40:536387 ASSERT_TRUE(older_version.IsOlderThan(ext->VersionString()));
6388 ASSERT_TRUE(ext->version()->IsOlderThan(newer_version.GetString()));
[email protected]9060d8b02012-01-13 02:14:306389
6390 // An external install for the same location should fail if the version is
6391 // older, or the same, and succeed if the version is newer.
6392
6393 // Older than the installed version...
6394 EXPECT_FALSE(
6395 service_->OnExternalExtensionFileFound(
[email protected]12126d372012-07-11 18:40:536396 kGoodId, &older_version, kInvalidPathToCrx,
[email protected]1d5e58b2013-01-31 08:41:406397 Manifest::INTERNAL, kCreationFlags, kDontMarkAcknowledged));
[email protected]9060d8b02012-01-13 02:14:306398 EXPECT_FALSE(pending->IsIdPending(kGoodId));
6399
6400 // Same version as the installed version...
6401 EXPECT_FALSE(
6402 service_->OnExternalExtensionFileFound(
6403 kGoodId, ext->version(), kInvalidPathToCrx,
[email protected]1d5e58b2013-01-31 08:41:406404 Manifest::INTERNAL, kCreationFlags, kDontMarkAcknowledged));
[email protected]9060d8b02012-01-13 02:14:306405 EXPECT_FALSE(pending->IsIdPending(kGoodId));
6406
6407 // Newer than the installed version...
6408 EXPECT_TRUE(
6409 service_->OnExternalExtensionFileFound(
[email protected]12126d372012-07-11 18:40:536410 kGoodId, &newer_version, kInvalidPathToCrx,
[email protected]1d5e58b2013-01-31 08:41:406411 Manifest::INTERNAL, kCreationFlags, kDontMarkAcknowledged));
[email protected]9060d8b02012-01-13 02:14:306412 EXPECT_TRUE(pending->IsIdPending(kGoodId));
6413
6414 // An external install for a higher priority install source should succeed
6415 // if the version is greater. |older_version| is not...
6416 EXPECT_FALSE(
6417 service_->OnExternalExtensionFileFound(
[email protected]12126d372012-07-11 18:40:536418 kGoodId, &older_version, kInvalidPathToCrx,
[email protected]1d5e58b2013-01-31 08:41:406419 Manifest::EXTERNAL_PREF, kCreationFlags, kDontMarkAcknowledged));
[email protected]9060d8b02012-01-13 02:14:306420 EXPECT_TRUE(pending->IsIdPending(kGoodId));
6421
6422 // |newer_version| is newer.
6423 EXPECT_TRUE(
6424 service_->OnExternalExtensionFileFound(
[email protected]12126d372012-07-11 18:40:536425 kGoodId, &newer_version, kInvalidPathToCrx,
[email protected]1d5e58b2013-01-31 08:41:406426 Manifest::EXTERNAL_PREF, kCreationFlags, kDontMarkAcknowledged));
[email protected]9060d8b02012-01-13 02:14:306427 EXPECT_TRUE(pending->IsIdPending(kGoodId));
6428
6429 // An external install for an even higher priority install source should
6430 // succeed if the version is greater.
6431 EXPECT_TRUE(
6432 service_->OnExternalExtensionFileFound(
[email protected]12126d372012-07-11 18:40:536433 kGoodId, &newer_version, kInvalidPathToCrx,
[email protected]1d5e58b2013-01-31 08:41:406434 Manifest::EXTERNAL_REGISTRY, kCreationFlags, kDontMarkAcknowledged));
[email protected]9060d8b02012-01-13 02:14:306435 EXPECT_TRUE(pending->IsIdPending(kGoodId));
6436
6437 // Because EXTERNAL_PREF is a lower priority source than EXTERNAL_REGISTRY,
6438 // adding from external pref will now fail.
6439 EXPECT_FALSE(
6440 service_->OnExternalExtensionFileFound(
[email protected]12126d372012-07-11 18:40:536441 kGoodId, &newer_version, kInvalidPathToCrx,
[email protected]1d5e58b2013-01-31 08:41:406442 Manifest::EXTERNAL_PREF, kCreationFlags, kDontMarkAcknowledged));
[email protected]9060d8b02012-01-13 02:14:306443 EXPECT_TRUE(pending->IsIdPending(kGoodId));
6444}
6445
[email protected]d9a61e12012-11-14 02:43:476446TEST_F(ExtensionServiceTest, ConcurrentExternalLocalFile) {
[email protected]c5e4a2222014-01-03 16:06:136447 Version kVersion123("1.2.3");
6448 Version kVersion124("1.2.4");
6449 Version kVersion125("1.2.5");
[email protected]650b2d52013-02-10 03:41:456450 const base::FilePath kInvalidPathToCrx = base::FilePath();
[email protected]e3987852012-05-04 10:06:306451 const int kCreationFlags = 0;
6452 const bool kDontMarkAcknowledged = false;
6453
6454 InitializeEmptyExtensionService();
6455
[email protected]3f213ad2012-07-26 23:39:416456 extensions::PendingExtensionManager* pending =
6457 service_->pending_extension_manager();
[email protected]e3987852012-05-04 10:06:306458 EXPECT_FALSE(pending->IsIdPending(kGoodId));
6459
6460 // An external provider starts installing from a local crx.
6461 EXPECT_TRUE(
6462 service_->OnExternalExtensionFileFound(
6463 kGoodId, &kVersion123, kInvalidPathToCrx,
[email protected]1d5e58b2013-01-31 08:41:406464 Manifest::EXTERNAL_PREF, kCreationFlags, kDontMarkAcknowledged));
[email protected]3f213ad2012-07-26 23:39:416465 const extensions::PendingExtensionInfo* info;
[email protected]51a3bf8b2012-06-08 22:53:066466 EXPECT_TRUE((info = pending->GetById(kGoodId)));
6467 EXPECT_TRUE(info->version().IsValid());
6468 EXPECT_TRUE(info->version().Equals(kVersion123));
[email protected]e3987852012-05-04 10:06:306469
6470 // Adding a newer version overrides the currently pending version.
6471 EXPECT_TRUE(
6472 service_->OnExternalExtensionFileFound(
6473 kGoodId, &kVersion124, kInvalidPathToCrx,
[email protected]1d5e58b2013-01-31 08:41:406474 Manifest::EXTERNAL_PREF, kCreationFlags, kDontMarkAcknowledged));
[email protected]51a3bf8b2012-06-08 22:53:066475 EXPECT_TRUE((info = pending->GetById(kGoodId)));
6476 EXPECT_TRUE(info->version().IsValid());
6477 EXPECT_TRUE(info->version().Equals(kVersion124));
[email protected]e3987852012-05-04 10:06:306478
6479 // Adding an older version fails.
6480 EXPECT_FALSE(
6481 service_->OnExternalExtensionFileFound(
6482 kGoodId, &kVersion123, kInvalidPathToCrx,
[email protected]1d5e58b2013-01-31 08:41:406483 Manifest::EXTERNAL_PREF, kCreationFlags, kDontMarkAcknowledged));
[email protected]51a3bf8b2012-06-08 22:53:066484 EXPECT_TRUE((info = pending->GetById(kGoodId)));
6485 EXPECT_TRUE(info->version().IsValid());
6486 EXPECT_TRUE(info->version().Equals(kVersion124));
[email protected]e3987852012-05-04 10:06:306487
6488 // Adding an older version fails even when coming from a higher-priority
6489 // location.
6490 EXPECT_FALSE(
6491 service_->OnExternalExtensionFileFound(
6492 kGoodId, &kVersion123, kInvalidPathToCrx,
[email protected]1d5e58b2013-01-31 08:41:406493 Manifest::EXTERNAL_REGISTRY, kCreationFlags, kDontMarkAcknowledged));
[email protected]51a3bf8b2012-06-08 22:53:066494 EXPECT_TRUE((info = pending->GetById(kGoodId)));
6495 EXPECT_TRUE(info->version().IsValid());
6496 EXPECT_TRUE(info->version().Equals(kVersion124));
[email protected]e3987852012-05-04 10:06:306497
6498 // Adding the latest version from the webstore overrides a specific version.
6499 GURL kUpdateUrl("http://example.com/update");
6500 EXPECT_TRUE(
6501 service_->OnExternalExtensionUpdateUrlFound(
[email protected]464213a2013-10-15 01:06:486502 kGoodId, kUpdateUrl, Manifest::EXTERNAL_POLICY_DOWNLOAD,
6503 Extension::NO_FLAGS, false));
[email protected]51a3bf8b2012-06-08 22:53:066504 EXPECT_TRUE((info = pending->GetById(kGoodId)));
6505 EXPECT_FALSE(info->version().IsValid());
[email protected]e3987852012-05-04 10:06:306506}
6507
[email protected]f5bf1842012-02-15 02:52:266508// This makes sure we can package and install CRX files that use whitelisted
6509// permissions.
[email protected]d9a61e12012-11-14 02:43:476510TEST_F(ExtensionServiceTest, InstallWhitelistedExtension) {
[email protected]f5bf1842012-02-15 02:52:266511 std::string test_id = "hdkklepkcpckhnpgjnmbdfhehckloojk";
6512 CommandLine::ForCurrentProcess()->AppendSwitchASCII(
6513 switches::kWhitelistedExtensionID, test_id);
6514
6515 InitializeEmptyExtensionService();
[email protected]650b2d52013-02-10 03:41:456516 base::FilePath path = data_dir_
[email protected]f5bf1842012-02-15 02:52:266517 .AppendASCII("permissions");
[email protected]650b2d52013-02-10 03:41:456518 base::FilePath pem_path = path
[email protected]f5bf1842012-02-15 02:52:266519 .AppendASCII("whitelist.pem");
6520 path = path
6521 .AppendASCII("whitelist");
6522
6523 const Extension* extension = PackAndInstallCRX(path, pem_path, INSTALL_NEW);
6524 EXPECT_EQ(0u, GetErrors().size());
6525 ASSERT_EQ(1u, service_->extensions()->size());
6526 EXPECT_EQ(test_id, extension->id());
6527}
6528
[email protected]145a317b2011-04-12 16:03:466529// Test that when multiple sources try to install an extension,
6530// we consistently choose the right one. To make tests easy to read,
6531// methods that fake requests to install crx files in several ways
6532// are provided.
6533class ExtensionSourcePriorityTest : public ExtensionServiceTest {
6534 public:
[email protected]49aeab62013-02-07 02:53:116535 virtual void SetUp() {
[email protected]ed8ee722011-04-22 06:49:446536 ExtensionServiceTest::SetUp();
6537
[email protected]145a317b2011-04-12 16:03:466538 // All tests use a single extension. Put the id and path in member vars
6539 // that all methods can read.
6540 crx_id_ = kGoodId;
[email protected]e85e34c32011-04-13 18:38:356541 crx_path_ = data_dir_.AppendASCII("good.crx");
[email protected]145a317b2011-04-12 16:03:466542 }
6543
6544 // Fake an external source adding a URL to fetch an extension from.
[email protected]9060d8b02012-01-13 02:14:306545 bool AddPendingExternalPrefUrl() {
6546 return service_->pending_extension_manager()->AddFromExternalUpdateUrl(
[email protected]464213a2013-10-15 01:06:486547 crx_id_, GURL(), Manifest::EXTERNAL_PREF_DOWNLOAD,
6548 Extension::NO_FLAGS, false);
[email protected]145a317b2011-04-12 16:03:466549 }
6550
6551 // Fake an external file from external_extensions.json.
[email protected]9060d8b02012-01-13 02:14:306552 bool AddPendingExternalPrefFileInstall() {
[email protected]c5e4a2222014-01-03 16:06:136553 Version version("1.0.0.0");
[email protected]145a317b2011-04-12 16:03:466554
[email protected]9060d8b02012-01-13 02:14:306555 return service_->OnExternalExtensionFileFound(
[email protected]1d5e58b2013-01-31 08:41:406556 crx_id_, &version, crx_path_, Manifest::EXTERNAL_PREF,
[email protected]47fc70c2011-12-06 07:29:516557 Extension::NO_FLAGS, false);
[email protected]145a317b2011-04-12 16:03:466558 }
6559
6560 // Fake a request from sync to install an extension.
6561 bool AddPendingSyncInstall() {
6562 return service_->pending_extension_manager()->AddFromSync(
[email protected]6cc7dbae2011-04-29 21:18:336563 crx_id_, GURL(kGoodUpdateURL), &IsExtension, kGoodInstallSilently);
[email protected]145a317b2011-04-12 16:03:466564 }
6565
[email protected]145a317b2011-04-12 16:03:466566 // Fake a policy install.
[email protected]9060d8b02012-01-13 02:14:306567 bool AddPendingPolicyInstall() {
[email protected]145a317b2011-04-12 16:03:466568 // Get path to the CRX with id |kGoodId|.
[email protected]9060d8b02012-01-13 02:14:306569 return service_->OnExternalExtensionUpdateUrlFound(
[email protected]464213a2013-10-15 01:06:486570 crx_id_, GURL(), Manifest::EXTERNAL_POLICY_DOWNLOAD,
6571 Extension::NO_FLAGS, false);
[email protected]145a317b2011-04-12 16:03:466572 }
6573
6574 // Get the install source of a pending extension.
[email protected]1d5e58b2013-01-31 08:41:406575 Manifest::Location GetPendingLocation() {
[email protected]3f213ad2012-07-26 23:39:416576 const extensions::PendingExtensionInfo* info;
[email protected]51a3bf8b2012-06-08 22:53:066577 EXPECT_TRUE((info = service_->pending_extension_manager()->
6578 GetById(crx_id_)));
6579 return info->install_source();
[email protected]145a317b2011-04-12 16:03:466580 }
6581
6582 // Is an extension pending from a sync request?
6583 bool GetPendingIsFromSync() {
[email protected]3f213ad2012-07-26 23:39:416584 const extensions::PendingExtensionInfo* info;
[email protected]51a3bf8b2012-06-08 22:53:066585 EXPECT_TRUE((info = service_->pending_extension_manager()->
6586 GetById(crx_id_)));
6587 return info->is_from_sync();
[email protected]145a317b2011-04-12 16:03:466588 }
6589
6590 // Is the CRX id these tests use pending?
6591 bool IsCrxPending() {
6592 return service_->pending_extension_manager()->IsIdPending(crx_id_);
6593 }
6594
6595 // Is an extension installed?
6596 bool IsCrxInstalled() {
6597 return (service_->GetExtensionById(crx_id_, true) != NULL);
6598 }
6599
6600 protected:
6601 // All tests use a single extension. Making the id and path member
6602 // vars avoids pasing the same argument to every method.
6603 std::string crx_id_;
[email protected]650b2d52013-02-10 03:41:456604 base::FilePath crx_path_;
[email protected]145a317b2011-04-12 16:03:466605};
6606
[email protected]3634ebd2011-04-20 00:34:126607// Test that a pending request for installation of an external CRX from
6608// an update URL overrides a pending request to install the same extension
6609// from sync.
[email protected]ed8ee722011-04-22 06:49:446610TEST_F(ExtensionSourcePriorityTest, PendingExternalFileOverSync) {
[email protected]145a317b2011-04-12 16:03:466611 InitializeEmptyExtensionService();
6612
6613 ASSERT_FALSE(IsCrxInstalled());
6614
6615 // Install pending extension from sync.
[email protected]97d6a5c2013-11-11 23:51:246616 content::WindowedNotificationObserver observer(
6617 chrome::NOTIFICATION_CRX_INSTALLER_DONE,
6618 content::NotificationService::AllSources());
[email protected]f4d5e1a2011-04-28 02:08:256619 EXPECT_TRUE(AddPendingSyncInstall());
[email protected]1d5e58b2013-01-31 08:41:406620 ASSERT_EQ(Manifest::INTERNAL, GetPendingLocation());
[email protected]145a317b2011-04-12 16:03:466621 EXPECT_TRUE(GetPendingIsFromSync());
6622 ASSERT_FALSE(IsCrxInstalled());
6623
6624 // Install pending as external prefs json would.
6625 AddPendingExternalPrefFileInstall();
[email protected]1d5e58b2013-01-31 08:41:406626 ASSERT_EQ(Manifest::EXTERNAL_PREF, GetPendingLocation());
[email protected]145a317b2011-04-12 16:03:466627 ASSERT_FALSE(IsCrxInstalled());
6628
[email protected]6d057a0c2013-07-09 21:12:076629 // Another request from sync should be ignored.
[email protected]f4d5e1a2011-04-28 02:08:256630 EXPECT_FALSE(AddPendingSyncInstall());
[email protected]1d5e58b2013-01-31 08:41:406631 ASSERT_EQ(Manifest::EXTERNAL_PREF, GetPendingLocation());
[email protected]145a317b2011-04-12 16:03:466632 ASSERT_FALSE(IsCrxInstalled());
6633
[email protected]97d6a5c2013-11-11 23:51:246634 observer.Wait();
6635 VerifyCrxInstall(crx_path_, INSTALL_NEW);
[email protected]145a317b2011-04-12 16:03:466636 ASSERT_TRUE(IsCrxInstalled());
6637}
6638
6639// Test that an install of an external CRX from an update overrides
6640// an install of the same extension from sync.
[email protected]ed8ee722011-04-22 06:49:446641TEST_F(ExtensionSourcePriorityTest, PendingExternalUrlOverSync) {
[email protected]145a317b2011-04-12 16:03:466642 InitializeEmptyExtensionService();
6643 ASSERT_FALSE(IsCrxInstalled());
6644
[email protected]f4d5e1a2011-04-28 02:08:256645 EXPECT_TRUE(AddPendingSyncInstall());
[email protected]1d5e58b2013-01-31 08:41:406646 ASSERT_EQ(Manifest::INTERNAL, GetPendingLocation());
[email protected]145a317b2011-04-12 16:03:466647 EXPECT_TRUE(GetPendingIsFromSync());
6648 ASSERT_FALSE(IsCrxInstalled());
6649
[email protected]9060d8b02012-01-13 02:14:306650 ASSERT_TRUE(AddPendingExternalPrefUrl());
[email protected]1d5e58b2013-01-31 08:41:406651 ASSERT_EQ(Manifest::EXTERNAL_PREF_DOWNLOAD, GetPendingLocation());
[email protected]145a317b2011-04-12 16:03:466652 EXPECT_FALSE(GetPendingIsFromSync());
6653 ASSERT_FALSE(IsCrxInstalled());
6654
[email protected]f4d5e1a2011-04-28 02:08:256655 EXPECT_FALSE(AddPendingSyncInstall());
[email protected]1d5e58b2013-01-31 08:41:406656 ASSERT_EQ(Manifest::EXTERNAL_PREF_DOWNLOAD, GetPendingLocation());
[email protected]145a317b2011-04-12 16:03:466657 EXPECT_FALSE(GetPendingIsFromSync());
6658 ASSERT_FALSE(IsCrxInstalled());
6659}
6660
[email protected]145a317b2011-04-12 16:03:466661// Test that an external install request stops sync from installing
6662// the same extension.
[email protected]ed8ee722011-04-22 06:49:446663TEST_F(ExtensionSourcePriorityTest, InstallExternalBlocksSyncRequest) {
[email protected]145a317b2011-04-12 16:03:466664 InitializeEmptyExtensionService();
6665 ASSERT_FALSE(IsCrxInstalled());
6666
6667 // External prefs starts an install.
6668 AddPendingExternalPrefFileInstall();
6669
6670 // Crx installer was made, but has not yet run.
6671 ASSERT_FALSE(IsCrxInstalled());
6672
6673 // Before the CRX installer runs, Sync requests that the same extension
6674 // be installed. Should fail, because an external source is pending.
[email protected]97d6a5c2013-11-11 23:51:246675 content::WindowedNotificationObserver observer(
6676 chrome::NOTIFICATION_CRX_INSTALLER_DONE,
6677 content::NotificationService::AllSources());
[email protected]145a317b2011-04-12 16:03:466678 ASSERT_FALSE(AddPendingSyncInstall());
6679
6680 // Wait for the external source to install.
[email protected]97d6a5c2013-11-11 23:51:246681 observer.Wait();
6682 VerifyCrxInstall(crx_path_, INSTALL_NEW);
[email protected]145a317b2011-04-12 16:03:466683 ASSERT_TRUE(IsCrxInstalled());
6684
6685 // Now that the extension is installed, sync request should fail
6686 // because the extension is already installed.
6687 ASSERT_FALSE(AddPendingSyncInstall());
6688}
[email protected]07c9f2f42012-02-29 18:45:226689
[email protected]612a1cb12012-10-17 13:18:036690// Test that installing an external extension displays a GlobalError.
[email protected]d9a61e12012-11-14 02:43:476691TEST_F(ExtensionServiceTest, ExternalInstallGlobalError) {
[email protected]00b5d0a52012-10-30 13:13:536692 FeatureSwitch::ScopedOverride prompt(
6693 FeatureSwitch::prompt_for_external_extensions(), true);
[email protected]612a1cb12012-10-17 13:18:036694
[email protected]07c9f2f42012-02-29 18:45:226695 InitializeEmptyExtensionService();
[email protected]07c9f2f42012-02-29 18:45:226696 MockExtensionProvider* provider =
[email protected]1d5e58b2013-01-31 08:41:406697 new MockExtensionProvider(service_, Manifest::EXTERNAL_PREF);
[email protected]07c9f2f42012-02-29 18:45:226698 AddMockExternalProvider(provider);
6699
[email protected]612a1cb12012-10-17 13:18:036700 service_->UpdateExternalExtensionAlert();
[email protected]07c9f2f42012-02-29 18:45:226701 // Should return false, meaning there aren't any extensions that the user
6702 // needs to know about.
[email protected]612a1cb12012-10-17 13:18:036703 EXPECT_FALSE(extensions::HasExternalInstallError(service_));
[email protected]07c9f2f42012-02-29 18:45:226704
6705 // This is a normal extension, installed normally.
6706 // This should NOT trigger an alert.
6707 set_extensions_enabled(true);
[email protected]650b2d52013-02-10 03:41:456708 base::FilePath path = data_dir_.AppendASCII("good.crx");
[email protected]07c9f2f42012-02-29 18:45:226709 InstallCRX(path, INSTALL_NEW);
6710
[email protected]612a1cb12012-10-17 13:18:036711 service_->CheckForExternalUpdates();
[email protected]eedc1c752013-08-09 18:51:556712 base::RunLoop().RunUntilIdle();
[email protected]612a1cb12012-10-17 13:18:036713 EXPECT_FALSE(extensions::HasExternalInstallError(service_));
[email protected]07c9f2f42012-02-29 18:45:226714
6715 // A hosted app, installed externally.
[email protected]2c495c42013-01-04 21:49:546716 // This should NOT trigger an alert.
[email protected]07c9f2f42012-02-29 18:45:226717 provider->UpdateOrAddExtension(hosted_app, "1.0.0.0",
6718 data_dir_.AppendASCII("hosted_app.crx"));
6719
[email protected]97d6a5c2013-11-11 23:51:246720 content::WindowedNotificationObserver observer(
[email protected]6d057a0c2013-07-09 21:12:076721 chrome::NOTIFICATION_CRX_INSTALLER_DONE,
[email protected]97d6a5c2013-11-11 23:51:246722 content::NotificationService::AllSources());
6723 service_->CheckForExternalUpdates();
6724 observer.Wait();
[email protected]612a1cb12012-10-17 13:18:036725 EXPECT_FALSE(extensions::HasExternalInstallError(service_));
[email protected]07c9f2f42012-02-29 18:45:226726
[email protected]612a1cb12012-10-17 13:18:036727 // Another normal extension, but installed externally.
6728 // This SHOULD trigger an alert.
6729 provider->UpdateOrAddExtension(page_action, "1.0.0.0",
6730 data_dir_.AppendASCII("page_action.crx"));
6731
[email protected]97d6a5c2013-11-11 23:51:246732 content::WindowedNotificationObserver observer2(
[email protected]6d057a0c2013-07-09 21:12:076733 chrome::NOTIFICATION_CRX_INSTALLER_DONE,
[email protected]97d6a5c2013-11-11 23:51:246734 content::NotificationService::AllSources());
6735 service_->CheckForExternalUpdates();
6736 observer2.Wait();
[email protected]612a1cb12012-10-17 13:18:036737 EXPECT_TRUE(extensions::HasExternalInstallError(service_));
[email protected]07c9f2f42012-02-29 18:45:226738}
[email protected]612a1cb12012-10-17 13:18:036739
6740// Test that external extensions are initially disabled, and that enabling
6741// them clears the prompt.
[email protected]d9a61e12012-11-14 02:43:476742TEST_F(ExtensionServiceTest, ExternalInstallInitiallyDisabled) {
[email protected]00b5d0a52012-10-30 13:13:536743 FeatureSwitch::ScopedOverride prompt(
6744 FeatureSwitch::prompt_for_external_extensions(), true);
[email protected]612a1cb12012-10-17 13:18:036745
6746 InitializeEmptyExtensionService();
6747 MockExtensionProvider* provider =
[email protected]1d5e58b2013-01-31 08:41:406748 new MockExtensionProvider(service_, Manifest::EXTERNAL_PREF);
[email protected]612a1cb12012-10-17 13:18:036749 AddMockExternalProvider(provider);
6750
6751 provider->UpdateOrAddExtension(page_action, "1.0.0.0",
6752 data_dir_.AppendASCII("page_action.crx"));
6753
[email protected]97d6a5c2013-11-11 23:51:246754 content::WindowedNotificationObserver observer(
[email protected]6d057a0c2013-07-09 21:12:076755 chrome::NOTIFICATION_CRX_INSTALLER_DONE,
[email protected]97d6a5c2013-11-11 23:51:246756 content::NotificationService::AllSources());
6757 service_->CheckForExternalUpdates();
6758 observer.Wait();
[email protected]612a1cb12012-10-17 13:18:036759 EXPECT_TRUE(extensions::HasExternalInstallError(service_));
6760 EXPECT_FALSE(service_->IsExtensionEnabled(page_action));
6761
6762 const Extension* extension =
6763 service_->disabled_extensions()->GetByID(page_action);
6764 EXPECT_TRUE(extension);
6765 EXPECT_EQ(page_action, extension->id());
6766
6767 service_->EnableExtension(page_action);
6768 EXPECT_FALSE(extensions::HasExternalInstallError(service_));
6769 EXPECT_TRUE(service_->IsExtensionEnabled(page_action));
6770}
6771
6772// Test that installing multiple external extensions works.
[email protected]404d1a22013-09-20 17:28:076773// Flaky on windows; http://crbug.com/295757 .
6774#if defined(OS_WIN)
6775#define MAYBE_ExternalInstallMultiple DISABLED_ExternalInstallMultiple
6776#else
6777#define MAYBE_ExternalInstallMultiple ExternalInstallMultiple
6778#endif
6779TEST_F(ExtensionServiceTest, MAYBE_ExternalInstallMultiple) {
[email protected]00b5d0a52012-10-30 13:13:536780 FeatureSwitch::ScopedOverride prompt(
6781 FeatureSwitch::prompt_for_external_extensions(), true);
[email protected]612a1cb12012-10-17 13:18:036782
6783 InitializeEmptyExtensionService();
6784 MockExtensionProvider* provider =
[email protected]1d5e58b2013-01-31 08:41:406785 new MockExtensionProvider(service_, Manifest::EXTERNAL_PREF);
[email protected]612a1cb12012-10-17 13:18:036786 AddMockExternalProvider(provider);
6787
6788 provider->UpdateOrAddExtension(page_action, "1.0.0.0",
6789 data_dir_.AppendASCII("page_action.crx"));
6790 provider->UpdateOrAddExtension(good_crx, "1.0.0.0",
6791 data_dir_.AppendASCII("good.crx"));
6792 provider->UpdateOrAddExtension(theme_crx, "2.0",
6793 data_dir_.AppendASCII("theme.crx"));
6794
[email protected]6d057a0c2013-07-09 21:12:076795 int count = 3;
[email protected]97d6a5c2013-11-11 23:51:246796 content::WindowedNotificationObserver observer(
[email protected]6d057a0c2013-07-09 21:12:076797 chrome::NOTIFICATION_CRX_INSTALLER_DONE,
[email protected]97d6a5c2013-11-11 23:51:246798 base::Bind(&WaitForCountNotificationsCallback, &count));
6799 service_->CheckForExternalUpdates();
6800 observer.Wait();
[email protected]612a1cb12012-10-17 13:18:036801 EXPECT_TRUE(extensions::HasExternalInstallError(service_));
6802 EXPECT_FALSE(service_->IsExtensionEnabled(page_action));
6803 EXPECT_FALSE(service_->IsExtensionEnabled(good_crx));
6804 EXPECT_FALSE(service_->IsExtensionEnabled(theme_crx));
6805
6806 service_->EnableExtension(page_action);
6807 EXPECT_TRUE(extensions::HasExternalInstallError(service_));
[email protected]aa55be7f2013-04-22 20:56:046808 EXPECT_FALSE(extensions::HasExternalInstallBubble(service_));
[email protected]612a1cb12012-10-17 13:18:036809 service_->EnableExtension(theme_crx);
6810 EXPECT_TRUE(extensions::HasExternalInstallError(service_));
[email protected]aa55be7f2013-04-22 20:56:046811 EXPECT_FALSE(extensions::HasExternalInstallBubble(service_));
[email protected]612a1cb12012-10-17 13:18:036812 service_->EnableExtension(good_crx);
6813 EXPECT_FALSE(extensions::HasExternalInstallError(service_));
[email protected]aa55be7f2013-04-22 20:56:046814 EXPECT_FALSE(extensions::HasExternalInstallBubble(service_));
6815}
6816
6817// Test that there is a bubble for external extensions that update
[email protected]b3aa7182013-04-25 04:45:236818// from the webstore if the profile is not new.
6819TEST_F(ExtensionServiceTest, ExternalInstallUpdatesFromWebstoreOldProfile) {
[email protected]aa55be7f2013-04-22 20:56:046820 FeatureSwitch::ScopedOverride prompt(
6821 FeatureSwitch::prompt_for_external_extensions(), true);
6822
[email protected]b3aa7182013-04-25 04:45:236823 // This sets up the ExtensionPrefs used by our ExtensionService to be
6824 // post-first run.
[email protected]371662e372013-10-17 22:05:226825 ExtensionServiceInitParams params = CreateDefaultInitParams();
6826 params.is_first_run = false;
6827 InitializeExtensionService(params);
[email protected]aa55be7f2013-04-22 20:56:046828
[email protected]aa55be7f2013-04-22 20:56:046829 base::FilePath crx_path = temp_dir_.path().AppendASCII("webstore.crx");
6830 PackCRX(data_dir_.AppendASCII("update_from_webstore"),
6831 data_dir_.AppendASCII("update_from_webstore.pem"),
6832 crx_path);
6833
6834 MockExtensionProvider* provider =
6835 new MockExtensionProvider(service_, Manifest::EXTERNAL_PREF);
6836 AddMockExternalProvider(provider);
[email protected]aa55be7f2013-04-22 20:56:046837 provider->UpdateOrAddExtension(updates_from_webstore, "1", crx_path);
[email protected]460c6712013-04-24 07:20:016838
[email protected]97d6a5c2013-11-11 23:51:246839 content::WindowedNotificationObserver observer(
[email protected]6d057a0c2013-07-09 21:12:076840 chrome::NOTIFICATION_CRX_INSTALLER_DONE,
[email protected]97d6a5c2013-11-11 23:51:246841 content::NotificationService::AllSources());
6842 service_->CheckForExternalUpdates();
6843 observer.Wait();
[email protected]aa55be7f2013-04-22 20:56:046844 EXPECT_TRUE(extensions::HasExternalInstallError(service_));
6845 EXPECT_TRUE(extensions::HasExternalInstallBubble(service_));
6846 EXPECT_FALSE(service_->IsExtensionEnabled(updates_from_webstore));
[email protected]612a1cb12012-10-17 13:18:036847}
[email protected]460c6712013-04-24 07:20:016848
6849// Test that there is no bubble for external extensions if the profile is new.
6850TEST_F(ExtensionServiceTest, ExternalInstallUpdatesFromWebstoreNewProfile) {
6851 FeatureSwitch::ScopedOverride prompt(
6852 FeatureSwitch::prompt_for_external_extensions(), true);
6853
6854 InitializeEmptyExtensionService();
6855
6856 base::FilePath crx_path = temp_dir_.path().AppendASCII("webstore.crx");
6857 PackCRX(data_dir_.AppendASCII("update_from_webstore"),
6858 data_dir_.AppendASCII("update_from_webstore.pem"),
6859 crx_path);
6860
6861 MockExtensionProvider* provider =
6862 new MockExtensionProvider(service_, Manifest::EXTERNAL_PREF);
6863 AddMockExternalProvider(provider);
6864 provider->UpdateOrAddExtension(updates_from_webstore, "1", crx_path);
6865
[email protected]97d6a5c2013-11-11 23:51:246866 content::WindowedNotificationObserver observer(
[email protected]6d057a0c2013-07-09 21:12:076867 chrome::NOTIFICATION_CRX_INSTALLER_DONE,
[email protected]97d6a5c2013-11-11 23:51:246868 content::NotificationService::AllSources());
6869 service_->CheckForExternalUpdates();
6870 observer.Wait();
[email protected]460c6712013-04-24 07:20:016871 EXPECT_TRUE(extensions::HasExternalInstallError(service_));
6872 EXPECT_FALSE(extensions::HasExternalInstallBubble(service_));
6873 EXPECT_FALSE(service_->IsExtensionEnabled(updates_from_webstore));
6874}
[email protected]9f3c8532013-07-31 19:52:076875
6876TEST_F(ExtensionServiceTest, InstallBlacklistedExtension) {
6877 InitializeEmptyExtensionService();
6878
6879 scoped_refptr<Extension> extension = extensions::ExtensionBuilder()
6880 .SetManifest(extensions::DictionaryBuilder()
6881 .Set("name", "extension")
6882 .Set("version", "1.0")
6883 .Set("manifest_version", 2).Build())
6884 .Build();
6885 ASSERT_TRUE(extension.get());
6886 const std::string& id = extension->id();
6887
6888 std::set<std::string> id_set;
6889 id_set.insert(id);
6890 extensions::ExtensionNotificationObserver notifications(
6891 content::NotificationService::AllSources(), id_set);
6892
6893 // Installation should be allowed but the extension should never have been
6894 // loaded and it should be blacklisted in prefs.
6895 service_->OnExtensionInstalled(
6896 extension.get(),
6897 syncer::StringOrdinal(),
6898 false /* has requirement errors */,
[email protected]8e289f0b2013-12-17 17:49:076899 extensions::BLACKLISTED_MALWARE,
[email protected]9f3c8532013-07-31 19:52:076900 false /* wait for idle */);
[email protected]eedc1c752013-08-09 18:51:556901 base::RunLoop().RunUntilIdle();
[email protected]9f3c8532013-07-31 19:52:076902
6903 // Extension was installed but not loaded.
6904 EXPECT_TRUE(notifications.CheckNotifications(
6905 chrome::NOTIFICATION_EXTENSION_INSTALLED));
[email protected]9f3c8532013-07-31 19:52:076906 EXPECT_TRUE(service_->GetInstalledExtension(id));
[email protected]5fdfa562013-12-27 17:43:596907
6908 ExtensionRegistry* registry = ExtensionRegistry::Get(profile_.get());
6909 EXPECT_FALSE(registry->enabled_extensions().Contains(id));
6910 EXPECT_TRUE(registry->blacklisted_extensions().Contains(id));
6911
[email protected]9f3c8532013-07-31 19:52:076912 EXPECT_TRUE(service_->extension_prefs()->IsExtensionBlacklisted(id));
6913 EXPECT_TRUE(
6914 service_->extension_prefs()->IsBlacklistedExtensionAcknowledged(id));
6915}
[email protected]59ee99d2013-10-11 15:46:166916
6917TEST_F(ExtensionServiceTest, ReconcileKnownDisabledNoneDisabled) {
6918 // A profile with 3 extensions installed: good0, good1, and good2.
6919 InitializeGoodInstalledExtensionService();
6920
6921 // Initializing shouldn't disable any extensions if none are known to be
6922 // disabled.
6923 service_->Init();
6924
6925 extensions::ExtensionIdSet expected_extensions;
6926 expected_extensions.insert(good0);
6927 expected_extensions.insert(good1);
6928 expected_extensions.insert(good2);
6929
6930 extensions::ExtensionIdSet expected_disabled_extensions;
6931
6932 EXPECT_EQ(expected_extensions, service_->extensions()->GetIDs());
6933 EXPECT_EQ(expected_disabled_extensions,
6934 service_->disabled_extensions()->GetIDs());
6935}
6936
6937TEST_F(ExtensionServiceTest, ReconcileKnownDisabledWithSideEnable) {
6938 // A profile with 3 extensions installed: good0, good1, and good2.
6939 InitializeGoodInstalledExtensionService();
6940
6941 ExtensionPrefs* extension_prefs = service_->extension_prefs();
6942
6943 // Disable good1.
6944 extension_prefs->SetExtensionState(good1, Extension::DISABLED);
6945
6946 // Mark both good1 and good2 as "known_disabled" (effectively making good2
6947 // look as if it had been side-enabled).
6948 extensions::ExtensionIdSet known_disabled;
6949 known_disabled.insert(good1);
6950 known_disabled.insert(good2);
6951 extension_prefs->SetKnownDisabled(known_disabled);
6952
6953 // Initialize the service (which should disable good2 since it's known to be
6954 // disabled).
6955 service_->Init();
6956
6957 extensions::ExtensionIdSet expected_extensions;
6958 expected_extensions.insert(good0);
6959
6960 extensions::ExtensionIdSet expected_disabled_extensions;
6961 expected_disabled_extensions.insert(good1);
6962 expected_disabled_extensions.insert(good2);
6963
6964 EXPECT_EQ(expected_extensions, service_->extensions()->GetIDs());
6965 EXPECT_EQ(expected_disabled_extensions,
6966 service_->disabled_extensions()->GetIDs());
6967
6968 // Make sure that re-enabling an extension sticks across calls to
6969 // ReconcileKnownDisabled().
6970 service_->EnableExtension(good2);
6971 service_->ReconcileKnownDisabled();
6972 expected_extensions.insert(good2);
6973 expected_disabled_extensions.erase(good2);
6974
6975 EXPECT_EQ(expected_extensions, service_->extensions()->GetIDs());
6976 EXPECT_EQ(expected_disabled_extensions,
6977 service_->disabled_extensions()->GetIDs());
6978}