blob: fb7ec15d0c55cc1a35f8518ef613555eac291165 [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]ea1a3f62012-11-16 20:34:2316#include "base/files/scoped_temp_dir.h"
[email protected]ffbec692012-02-26 20:26:4217#include "base/json/json_file_value_serializer.h"
[email protected]93d49d72009-10-23 20:00:2018#include "base/json/json_reader.h"
[email protected]ffbec692012-02-26 20:26:4219#include "base/json/json_string_value_serializer.h"
[email protected]3b63f8f42011-03-28 01:54:1520#include "base/memory/scoped_ptr.h"
[email protected]71cb8aa2011-12-29 19:14:0021#include "base/memory/weak_ptr.h"
[email protected]6014d672008-12-05 00:38:2522#include "base/message_loop.h"
23#include "base/path_service.h"
[email protected]7286e3fc2011-07-19 22:13:2424#include "base/stl_util.h"
[email protected]24b538a2010-02-27 01:22:4425#include "base/string16.h"
[email protected]6014d672008-12-05 00:38:2526#include "base/string_util.h"
[email protected]3ea1b182013-02-08 22:38:4127#include "base/strings/string_number_conversions.h"
[email protected]be1ce6a72010-08-03 14:35:2228#include "base/utf_string_conversions.h"
[email protected]aa142702010-03-26 01:26:3329#include "base/version.h"
[email protected]9d32ded072011-10-11 16:31:0530#include "chrome/browser/browser_process.h"
[email protected]5db9ada2012-04-11 13:48:2031#include "chrome/browser/extensions/app_sync_data.h"
[email protected]d8c8f25f2011-11-02 18:18:0132#include "chrome/browser/extensions/component_loader.h"
[email protected]eb6c7ef2011-12-12 23:12:2033#include "chrome/browser/extensions/crx_installer.h"
[email protected]a7cd28e2012-10-05 21:03:3634#include "chrome/browser/extensions/default_apps.h"
[email protected]a17f9462009-06-09 02:56:4135#include "chrome/browser/extensions/extension_creator.h"
[email protected]14a000d2010-04-29 21:44:2436#include "chrome/browser/extensions/extension_error_reporter.h"
[email protected]89226982012-07-16 20:09:1837#include "chrome/browser/extensions/extension_error_ui.h"
[email protected]eaa7dd182010-12-14 11:09:0038#include "chrome/browser/extensions/extension_service.h"
[email protected]1bcf30e2012-03-10 01:06:4139#include "chrome/browser/extensions/extension_sorting.h"
[email protected]19eb80152011-02-26 00:28:4340#include "chrome/browser/extensions/extension_special_storage_policy.h"
[email protected]90310d92011-04-17 07:35:0441#include "chrome/browser/extensions/extension_sync_data.h"
[email protected]31d8f5f22012-04-02 15:22:0842#include "chrome/browser/extensions/extension_system.h"
[email protected]612a1cb12012-10-17 13:18:0343#include "chrome/browser/extensions/external_install_ui.h"
[email protected]e410b5f2012-12-14 14:02:2444#include "chrome/browser/extensions/external_policy_loader.h"
[email protected]5df038b2012-07-16 19:03:2745#include "chrome/browser/extensions/external_pref_loader.h"
46#include "chrome/browser/extensions/external_provider_impl.h"
47#include "chrome/browser/extensions/external_provider_interface.h"
[email protected]d8c8f25f2011-11-02 18:18:0148#include "chrome/browser/extensions/installed_loader.h"
[email protected]fdd679b2012-11-15 20:49:3949#include "chrome/browser/extensions/management_policy.h"
[email protected]f0bfe622012-06-22 01:01:4450#include "chrome/browser/extensions/pack_extension_job.h"
[email protected]90310d92011-04-17 07:35:0451#include "chrome/browser/extensions/pending_extension_info.h"
52#include "chrome/browser/extensions/pending_extension_manager.h"
[email protected]31d8f5f22012-04-02 15:22:0853#include "chrome/browser/extensions/test_extension_system.h"
[email protected]65187152012-06-02 13:14:1454#include "chrome/browser/extensions/test_management_policy.h"
[email protected]d8c8f25f2011-11-02 18:18:0155#include "chrome/browser/extensions/unpacked_installer.h"
[email protected]42a08162012-03-16 18:09:1156#include "chrome/browser/extensions/updater/extension_updater.h"
[email protected]37858e52010-08-26 00:22:0257#include "chrome/browser/prefs/browser_prefs.h"
[email protected]f2d1f612010-12-09 15:10:1758#include "chrome/browser/prefs/pref_service_mock_builder.h"
[email protected]c753f142013-02-10 13:14:0459#include "chrome/browser/prefs/pref_service_syncable.h"
[email protected]f89ee342011-03-07 09:28:2760#include "chrome/browser/prefs/scoped_user_pref_update.h"
[email protected]31d8f5f22012-04-02 15:22:0861#include "chrome/common/chrome_constants.h"
[email protected]432115822011-07-10 15:52:2762#include "chrome/common/chrome_notification_types.h"
[email protected]37858e52010-08-26 00:22:0263#include "chrome/common/chrome_paths.h"
64#include "chrome/common/chrome_switches.h"
[email protected]098fa7a2013-03-08 22:11:1765#include "chrome/common/extensions/api/plugins/plugins_handler.h"
[email protected]9367eabc2013-03-01 01:29:2966#include "chrome/common/extensions/background_info.h"
[email protected]5b1a0e22009-05-26 19:00:5867#include "chrome/common/extensions/extension.h"
[email protected]6884a802012-08-07 03:55:2268#include "chrome/common/extensions/extension_l10n_util.h"
[email protected]a52c0e92012-03-23 06:02:2469#include "chrome/common/extensions/extension_manifest_constants.h"
[email protected]334ec0a2013-03-24 01:36:1670#include "chrome/common/extensions/manifest_handlers/content_scripts_handler.h"
[email protected]65348062013-01-15 07:27:2271#include "chrome/common/extensions/manifest_url_handler.h"
[email protected]bebe1d02012-08-02 20:17:0972#include "chrome/common/extensions/permissions/permission_set.h"
[email protected]36a784c2009-06-23 06:21:0873#include "chrome/common/pref_names.h"
[email protected]24b538a2010-02-27 01:22:4474#include "chrome/common/url_constants.h"
[email protected]a4ff9eae2011-08-01 19:58:1675#include "chrome/test/base/testing_profile.h"
[email protected]75fee372013-03-06 00:42:4476#include "components/user_prefs/pref_registry_syncable.h"
[email protected]35cc399e2012-02-23 18:19:2877#include "content/public/browser/dom_storage_context.h"
[email protected]98270432012-09-11 20:51:2478#include "content/public/browser/gpu_data_manager.h"
[email protected]35cc399e2012-02-23 18:19:2879#include "content/public/browser/indexed_db_context.h"
[email protected]6c2381d2011-10-19 02:52:5380#include "content/public/browser/notification_registrar.h"
[email protected]ad50def52011-10-19 23:17:0781#include "content/public/browser/notification_service.h"
[email protected]e67385f2011-12-21 06:00:5682#include "content/public/browser/plugin_service.h"
[email protected]5c8e67c2012-08-29 00:48:5283#include "content/public/browser/storage_partition.h"
[email protected]55eb70e762012-02-20 17:38:3984#include "content/public/common/content_constants.h"
[email protected]7e343152012-09-20 21:49:5385#include "content/public/common/gpu_info.h"
[email protected]e97882f2012-06-04 02:23:1786#include "content/public/test/test_browser_thread.h"
[email protected]993da5e2013-03-23 21:25:1687#include "extensions/common/constants.h"
88#include "extensions/common/extension_resource.h"
[email protected]885c0e92012-11-13 20:27:4289#include "extensions/common/url_pattern.h"
[email protected]24b538a2010-02-27 01:22:4490#include "googleurl/src/gurl.h"
[email protected]b873cd92012-02-09 21:51:4891#include "grit/browser_resources.h"
[email protected]5b9bc352012-07-18 13:13:3492#include "net/cookies/canonical_cookie.h"
[email protected]aa84a7e2012-03-15 21:29:0693#include "net/cookies/cookie_monster.h"
94#include "net/cookies/cookie_options.h"
[email protected]dbbad7a2010-08-13 18:18:3695#include "net/url_request/url_request_context.h"
[email protected]abe2c032011-03-31 18:49:3496#include "net/url_request/url_request_context_getter.h"
[email protected]36b643212012-09-07 12:53:0097#include "sync/api/string_ordinal.h"
[email protected]895a1e52012-05-15 02:50:1298#include "sync/api/sync_error_factory.h"
99#include "sync/api/sync_error_factory_mock.h"
[email protected]1bcf30e2012-03-10 01:06:41100#include "sync/protocol/app_specifics.pb.h"
101#include "sync/protocol/extension_specifics.pb.h"
102#include "sync/protocol/sync.pb.h"
[email protected]6014d672008-12-05 00:38:25103#include "testing/gtest/include/gtest/gtest.h"
[email protected]f66c110c2008-12-05 20:26:29104#include "testing/platform_test.h"
[email protected]391550b2013-05-10 04:34:12105#include "webkit/base/origin_url_conversions.h"
[email protected]24b538a2010-02-27 01:22:44106#include "webkit/database/database_tracker.h"
[email protected]eb6c7ef2011-12-12 23:12:20107#include "webkit/plugins/npapi/mock_plugin_list.h"
[email protected]c62983a72011-05-09 06:29:59108#include "webkit/quota/quota_manager.h"
[email protected]6014d672008-12-05 00:38:25109
[email protected]88e8ec9152013-01-17 04:05:18110#if defined(OS_CHROMEOS)
111#include "chrome/browser/chromeos/extensions/install_limiter.h"
[email protected]e4854dc2013-04-24 00:11:51112#include "chrome/browser/chromeos/login/user_manager.h"
113#include "chrome/browser/chromeos/settings/cros_settings.h"
114#include "chrome/browser/chromeos/settings/device_settings_service.h"
[email protected]88e8ec9152013-01-17 04:05:18115#endif
116
[email protected]55eb70e762012-02-20 17:38:39117using content::BrowserContext;
[email protected]631bb742011-11-02 11:29:39118using content::BrowserThread;
[email protected]35cc399e2012-02-23 18:19:28119using content::DOMStorageContext;
120using content::IndexedDBContext;
[email protected]e67385f2011-12-21 06:00:56121using content::PluginService;
[email protected]c2e66e12012-06-27 06:27:06122using extensions::APIPermission;
123using extensions::APIPermissionSet;
[email protected]bf3d9df2012-07-24 23:20:27124using extensions::CrxInstaller;
[email protected]1c321ee2012-05-21 03:02:34125using extensions::Extension;
[email protected]6d777492012-07-11 17:33:43126using extensions::ExtensionCreator;
[email protected]45759612012-07-10 17:21:23127using extensions::ExtensionPrefs;
[email protected]993da5e2013-03-23 21:25:16128using extensions::ExtensionResource;
[email protected]bd306722012-07-11 20:43:59129using extensions::ExtensionSystem;
[email protected]00b5d0a52012-10-30 13:13:53130using extensions::FeatureSwitch;
[email protected]1d5e58b2013-01-31 08:41:40131using extensions::Manifest;
[email protected]c2e66e12012-06-27 06:27:06132using extensions::PermissionSet;
[email protected]b3aa7182013-04-25 04:45:23133using extensions::TestExtensionSystem;
[email protected]e9f541a2012-11-19 21:52:31134using extensions::URLPatternSet;
[email protected]631bb742011-11-02 11:29:39135
[email protected]c6d474f82009-12-16 21:11:06136namespace keys = extension_manifest_keys;
137
[email protected]f0397fa2008-12-11 17:59:58138namespace {
139
[email protected]df4956e2009-06-10 16:53:42140// Extension ids used during testing.
[email protected]5a2721f62009-06-13 07:08:20141const char* const all_zero = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
142const char* const zero_n_one = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaab";
143const char* const good0 = "behllobkkfkfnphdnhnkndlbkcpglgmj";
144const char* const good1 = "hpiknbiabeeppbpihjehijgoemciehgk";
145const char* const good2 = "bjafgdebaacbbbecmhlhpofkepfkgcpa";
146const char* const good_crx = "ldnnhddmnhbkjipkidpdiheffobcpfmf";
[email protected]07c9f2f42012-02-29 18:45:22147const char* const hosted_app = "kbmnembihfiondgfjekmnmcbddelicoi";
[email protected]d7eaf572009-07-01 21:57:00148const char* const page_action = "obcimlgaoabeegjmmpldobjndiealpln";
[email protected]5a2721f62009-06-13 07:08:20149const char* const theme_crx = "iamefpfkojoapidjnbafmgkgncegbkad";
150const char* const theme2_crx = "pjpgmfcmabopnnfonnhmdjglfpjjfkbf";
[email protected]8d888c12010-11-30 00:00:25151const char* const permissions_crx = "eagpmdpfmaekmmcejjbmjoecnejeiiin";
[email protected]c24fb292012-02-01 22:52:11152const char* const unpacked = "cbcdidchbppangcjoddlpdjlenngjldk";
[email protected]aa55be7f2013-04-22 20:56:04153const char* const updates_from_webstore = "akjooamlhcgeopfifcmlggaebeocgokj";
[email protected]df4956e2009-06-10 16:53:42154
[email protected]f0397fa2008-12-11 17:59:58155struct ExtensionsOrder {
156 bool operator()(const Extension* a, const Extension* b) {
157 return a->name() < b->name();
158 }
159};
160
[email protected]fc670822011-12-17 09:33:49161static std::vector<string16> GetErrors() {
162 const std::vector<string16>* errors =
[email protected]bb28e062009-02-27 17:19:18163 ExtensionErrorReporter::GetInstance()->GetErrors();
[email protected]fc670822011-12-17 09:33:49164 std::vector<string16> ret_val;
[email protected]bb28e062009-02-27 17:19:18165
[email protected]fc670822011-12-17 09:33:49166 for (std::vector<string16>::const_iterator iter = errors->begin();
[email protected]bb28e062009-02-27 17:19:18167 iter != errors->end(); ++iter) {
[email protected]fc670822011-12-17 09:33:49168 std::string utf8_error = UTF16ToUTF8(*iter);
169 if (utf8_error.find(".svn") == std::string::npos) {
[email protected]bb28e062009-02-27 17:19:18170 ret_val.push_back(*iter);
171 }
172 }
173
174 // The tests rely on the errors being in a certain order, which can vary
175 // depending on how filesystem iteration works.
176 std::stable_sort(ret_val.begin(), ret_val.end());
177
178 return ret_val;
179}
180
[email protected]cced75a2011-05-20 08:31:12181static void AddPattern(URLPatternSet* extent, const std::string& pattern) {
[email protected]8d888c12010-11-30 00:00:25182 int schemes = URLPattern::SCHEME_ALL;
183 extent->AddPattern(URLPattern(schemes, pattern));
184}
185
[email protected]650b2d52013-02-10 03:41:45186base::FilePath GetTemporaryFile() {
187 base::FilePath temp_file;
[email protected]a17dfcf2012-12-30 02:07:09188 CHECK(file_util::CreateTemporaryFile(&temp_file));
189 return temp_file;
190}
191
[email protected]f0397fa2008-12-11 17:59:58192} // namespace
[email protected]6014d672008-12-05 00:38:25193
[email protected]5df038b2012-07-16 19:03:27194class MockExtensionProvider : public extensions::ExternalProviderInterface {
[email protected]a1257b12009-06-12 02:51:34195 public:
[email protected]a12ce8b22012-01-17 18:40:53196 MockExtensionProvider(
[email protected]8e4560b62011-01-14 10:09:14197 VisitorInterface* visitor,
[email protected]1d5e58b2013-01-31 08:41:40198 Manifest::Location location)
[email protected]f121003b2012-05-04 21:57:47199 : location_(location), visitor_(visitor), visit_count_(0) {
[email protected]a12ce8b22012-01-17 18:40:53200 }
201
[email protected]a1257b12009-06-12 02:51:34202 virtual ~MockExtensionProvider() {}
203
204 void UpdateOrAddExtension(const std::string& id,
205 const std::string& version,
[email protected]650b2d52013-02-10 03:41:45206 const base::FilePath& path) {
[email protected]a1257b12009-06-12 02:51:34207 extension_map_[id] = std::make_pair(version, path);
208 }
209
210 void RemoveExtension(const std::string& id) {
211 extension_map_.erase(id);
212 }
213
[email protected]5df038b2012-07-16 19:03:27214 // ExternalProvider implementation:
[email protected]a12ce8b22012-01-17 18:40:53215 virtual void VisitRegisteredExtension() OVERRIDE {
[email protected]0a60a2e2010-10-25 16:15:21216 visit_count_++;
[email protected]a1257b12009-06-12 02:51:34217 for (DataMap::const_iterator i = extension_map_.begin();
218 i != extension_map_.end(); ++i) {
[email protected]12126d372012-07-11 18:40:53219 Version version(i->second.first);
[email protected]a1257b12009-06-12 02:51:34220
[email protected]8e4560b62011-01-14 10:09:14221 visitor_->OnExternalExtensionFileFound(
[email protected]12126d372012-07-11 18:40:53222 i->first, &version, i->second.second, location_,
[email protected]f121003b2012-05-04 21:57:47223 Extension::NO_FLAGS, false);
[email protected]a1257b12009-06-12 02:51:34224 }
[email protected]50067e52011-10-20 23:17:07225 visitor_->OnExternalProviderReady(this);
[email protected]a1257b12009-06-12 02:51:34226 }
227
[email protected]a12ce8b22012-01-17 18:40:53228 virtual bool HasExtension(const std::string& id) const OVERRIDE {
[email protected]0a60a2e2010-10-25 16:15:21229 return extension_map_.find(id) != extension_map_.end();
230 }
231
[email protected]a12ce8b22012-01-17 18:40:53232 virtual bool GetExtensionDetails(
233 const std::string& id,
[email protected]1d5e58b2013-01-31 08:41:40234 Manifest::Location* location,
[email protected]a12ce8b22012-01-17 18:40:53235 scoped_ptr<Version>* version) const OVERRIDE {
[email protected]a1257b12009-06-12 02:51:34236 DataMap::const_iterator it = extension_map_.find(id);
237 if (it == extension_map_.end())
[email protected]0a60a2e2010-10-25 16:15:21238 return false;
239
240 if (version)
[email protected]12126d372012-07-11 18:40:53241 version->reset(new Version(it->second.first));
[email protected]a1257b12009-06-12 02:51:34242
243 if (location)
244 *location = location_;
[email protected]0a60a2e2010-10-25 16:15:21245
246 return true;
247 }
[email protected]8e4560b62011-01-14 10:09:14248
[email protected]a12ce8b22012-01-17 18:40:53249 virtual bool IsReady() const OVERRIDE {
[email protected]8e4560b62011-01-14 10:09:14250 return true;
251 }
252
[email protected]a12ce8b22012-01-17 18:40:53253 virtual void ServiceShutdown() OVERRIDE {
[email protected]8e4560b62011-01-14 10:09:14254 }
255
[email protected]0a60a2e2010-10-25 16:15:21256 int visit_count() const { return visit_count_; }
257 void set_visit_count(int visit_count) {
258 visit_count_ = visit_count;
[email protected]a1257b12009-06-12 02:51:34259 }
260
261 private:
[email protected]650b2d52013-02-10 03:41:45262 typedef std::map< std::string, std::pair<std::string, base::FilePath> >
263 DataMap;
[email protected]a1257b12009-06-12 02:51:34264 DataMap extension_map_;
[email protected]1d5e58b2013-01-31 08:41:40265 Manifest::Location location_;
[email protected]8e4560b62011-01-14 10:09:14266 VisitorInterface* visitor_;
[email protected]27b985d2009-06-25 17:53:15267
[email protected]0a60a2e2010-10-25 16:15:21268 // visit_count_ tracks the number of calls to VisitRegisteredExtension().
269 // Mutable because it must be incremented on each call to
270 // VisitRegisteredExtension(), which must be a const method to inherit
271 // from the class being mocked.
272 mutable int visit_count_;
273
[email protected]27b985d2009-06-25 17:53:15274 DISALLOW_COPY_AND_ASSIGN(MockExtensionProvider);
275};
276
[email protected]8e4560b62011-01-14 10:09:14277class MockProviderVisitor
[email protected]5df038b2012-07-16 19:03:27278 : public extensions::ExternalProviderInterface::VisitorInterface {
[email protected]27b985d2009-06-25 17:53:15279 public:
[email protected]f0841cd2011-01-19 15:07:24280 // The provider will return |fake_base_path| from
281 // GetBaseCrxFilePath(). User can test the behavior with
282 // and without an empty path using this parameter.
[email protected]650b2d52013-02-10 03:41:45283 explicit MockProviderVisitor(base::FilePath fake_base_path)
[email protected]f0841cd2011-01-19 15:07:24284 : ids_found_(0),
[email protected]f121003b2012-05-04 21:57:47285 fake_base_path_(fake_base_path),
286 expected_creation_flags_(Extension::NO_FLAGS) {
287 }
288
[email protected]650b2d52013-02-10 03:41:45289 MockProviderVisitor(base::FilePath fake_base_path,
290 int expected_creation_flags)
[email protected]f121003b2012-05-04 21:57:47291 : ids_found_(0),
292 fake_base_path_(fake_base_path),
293 expected_creation_flags_(expected_creation_flags) {
[email protected]27b985d2009-06-25 17:53:15294 }
295
[email protected]683d0702010-12-06 16:25:57296 int Visit(const std::string& json_data) {
[email protected]27b985d2009-06-25 17:53:15297 // Give the test json file to the provider for parsing.
[email protected]5df038b2012-07-16 19:03:27298 provider_.reset(new extensions::ExternalProviderImpl(
[email protected]8e4560b62011-01-14 10:09:14299 this,
[email protected]5df038b2012-07-16 19:03:27300 new extensions::ExternalTestingLoader(json_data, fake_base_path_),
[email protected]1d5e58b2013-01-31 08:41:40301 Manifest::EXTERNAL_PREF,
302 Manifest::EXTERNAL_PREF_DOWNLOAD,
[email protected]1bf73cc2011-10-26 22:38:31303 Extension::NO_FLAGS));
[email protected]27b985d2009-06-25 17:53:15304
305 // We also parse the file into a dictionary to compare what we get back
306 // from the provider.
307 JSONStringValueSerializer serializer(json_data);
[email protected]ba399672010-04-06 15:42:39308 Value* json_value = serializer.Deserialize(NULL, NULL);
[email protected]27b985d2009-06-25 17:53:15309
[email protected]ba399672010-04-06 15:42:39310 if (!json_value || !json_value->IsType(Value::TYPE_DICTIONARY)) {
[email protected]e2194742010-08-12 05:54:34311 NOTREACHED() << "Unable to deserialize json data";
[email protected]27b985d2009-06-25 17:53:15312 return -1;
313 } else {
314 DictionaryValue* external_extensions =
315 static_cast<DictionaryValue*>(json_value);
316 prefs_.reset(external_extensions);
317 }
318
319 // Reset our counter.
320 ids_found_ = 0;
[email protected]683d0702010-12-06 16:25:57321 // Ask the provider to look up all extensions and return them.
[email protected]8e4560b62011-01-14 10:09:14322 provider_->VisitRegisteredExtension();
[email protected]27b985d2009-06-25 17:53:15323
324 return ids_found_;
325 }
326
[email protected]9060d8b02012-01-13 02:14:30327 virtual bool OnExternalExtensionFileFound(const std::string& id,
[email protected]8ef78fd2010-08-19 17:14:32328 const Version* version,
[email protected]650b2d52013-02-10 03:41:45329 const base::FilePath& path,
[email protected]1d5e58b2013-01-31 08:41:40330 Manifest::Location unused,
[email protected]47fc70c2011-12-06 07:29:51331 int creation_flags,
[email protected]49aeab62013-02-07 02:53:11332 bool mark_acknowledged) OVERRIDE {
[email protected]f121003b2012-05-04 21:57:47333 EXPECT_EQ(expected_creation_flags_, creation_flags);
[email protected]1bf73cc2011-10-26 22:38:31334
[email protected]27b985d2009-06-25 17:53:15335 ++ids_found_;
336 DictionaryValue* pref;
337 // This tests is to make sure that the provider only notifies us of the
338 // values we gave it. So if the id we doesn't exist in our internal
339 // dictionary then something is wrong.
[email protected]e2194742010-08-12 05:54:34340 EXPECT_TRUE(prefs_->GetDictionary(id, &pref))
341 << "Got back ID (" << id.c_str() << ") we weren't expecting";
[email protected]27b985d2009-06-25 17:53:15342
[email protected]f0841cd2011-01-19 15:07:24343 EXPECT_TRUE(path.IsAbsolute());
344 if (!fake_base_path_.empty())
345 EXPECT_TRUE(fake_base_path_.IsParent(path));
346
[email protected]27b985d2009-06-25 17:53:15347 if (pref) {
[email protected]0a60a2e2010-10-25 16:15:21348 EXPECT_TRUE(provider_->HasExtension(id));
349
[email protected]27b985d2009-06-25 17:53:15350 // Ask provider if the extension we got back is registered.
[email protected]1d5e58b2013-01-31 08:41:40351 Manifest::Location location = Manifest::INVALID_LOCATION;
[email protected]0a60a2e2010-10-25 16:15:21352 scoped_ptr<Version> v1;
[email protected]650b2d52013-02-10 03:41:45353 base::FilePath crx_path;
[email protected]0a60a2e2010-10-25 16:15:21354
355 EXPECT_TRUE(provider_->GetExtensionDetails(id, NULL, &v1));
356 EXPECT_STREQ(version->GetString().c_str(), v1->GetString().c_str());
357
358 scoped_ptr<Version> v2;
359 EXPECT_TRUE(provider_->GetExtensionDetails(id, &location, &v2));
[email protected]27b985d2009-06-25 17:53:15360 EXPECT_STREQ(version->GetString().c_str(), v1->GetString().c_str());
361 EXPECT_STREQ(version->GetString().c_str(), v2->GetString().c_str());
[email protected]1d5e58b2013-01-31 08:41:40362 EXPECT_EQ(Manifest::EXTERNAL_PREF, location);
[email protected]27b985d2009-06-25 17:53:15363
364 // Remove it so we won't count it ever again.
[email protected]e2194742010-08-12 05:54:34365 prefs_->Remove(id, NULL);
[email protected]27b985d2009-06-25 17:53:15366 }
[email protected]9060d8b02012-01-13 02:14:30367 return true;
[email protected]27b985d2009-06-25 17:53:15368 }
369
[email protected]9060d8b02012-01-13 02:14:30370 virtual bool OnExternalExtensionUpdateUrlFound(
[email protected]21a5a672010-11-04 10:47:42371 const std::string& id, const GURL& update_url,
[email protected]49aeab62013-02-07 02:53:11372 Manifest::Location location) OVERRIDE {
[email protected]8ef78fd2010-08-19 17:14:32373 ++ids_found_;
374 DictionaryValue* pref;
375 // 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.
378 EXPECT_TRUE(prefs_->GetDictionary(id, &pref))
379 << L"Got back ID (" << id.c_str() << ") we weren't expecting";
[email protected]1d5e58b2013-01-31 08:41:40380 EXPECT_EQ(Manifest::EXTERNAL_PREF_DOWNLOAD, location);
[email protected]8ef78fd2010-08-19 17:14:32381
382 if (pref) {
[email protected]0a60a2e2010-10-25 16:15:21383 EXPECT_TRUE(provider_->HasExtension(id));
384
385 // External extensions with update URLs do not have versions.
386 scoped_ptr<Version> v1;
[email protected]1d5e58b2013-01-31 08:41:40387 Manifest::Location location1 = Manifest::INVALID_LOCATION;
[email protected]21a5a672010-11-04 10:47:42388 EXPECT_TRUE(provider_->GetExtensionDetails(id, &location1, &v1));
[email protected]0a60a2e2010-10-25 16:15:21389 EXPECT_FALSE(v1.get());
[email protected]1d5e58b2013-01-31 08:41:40390 EXPECT_EQ(Manifest::EXTERNAL_PREF_DOWNLOAD, location1);
[email protected]0a60a2e2010-10-25 16:15:21391
[email protected]8ef78fd2010-08-19 17:14:32392 // Remove it so we won't count it again.
393 prefs_->Remove(id, NULL);
394 }
[email protected]9060d8b02012-01-13 02:14:30395 return true;
[email protected]8ef78fd2010-08-19 17:14:32396 }
397
[email protected]50067e52011-10-20 23:17:07398 virtual void OnExternalProviderReady(
[email protected]49aeab62013-02-07 02:53:11399 const extensions::ExternalProviderInterface* provider) OVERRIDE {
[email protected]50067e52011-10-20 23:17:07400 EXPECT_EQ(provider, provider_.get());
401 EXPECT_TRUE(provider->IsReady());
[email protected]8e4560b62011-01-14 10:09:14402 }
403
[email protected]27b985d2009-06-25 17:53:15404 private:
405 int ids_found_;
[email protected]650b2d52013-02-10 03:41:45406 base::FilePath fake_base_path_;
[email protected]f121003b2012-05-04 21:57:47407 int expected_creation_flags_;
[email protected]5df038b2012-07-16 19:03:27408 scoped_ptr<extensions::ExternalProviderImpl> provider_;
[email protected]27b985d2009-06-25 17:53:15409 scoped_ptr<DictionaryValue> prefs_;
410
411 DISALLOW_COPY_AND_ASSIGN(MockProviderVisitor);
[email protected]a1257b12009-06-12 02:51:34412};
413
[email protected]b3aa7182013-04-25 04:45:23414ExtensionServiceTestBase::ExtensionServiceInitParams::
415ExtensionServiceInitParams()
416 : autoupdate_enabled(false), is_first_run(true) {
417}
418
[email protected]bf73f0b2010-02-10 19:26:59419// Our message loop may be used in tests which require it to be an IO loop.
[email protected]a5768512013-04-12 19:35:35420ExtensionServiceTestBase::ExtensionServiceTestBase()
421 : loop_(MessageLoop::TYPE_IO),
[email protected]32e2e9b2011-11-18 18:56:45422 service_(NULL),
[email protected]65187152012-06-02 13:14:14423 management_policy_(NULL),
[email protected]8f512c72011-11-22 21:02:50424 expected_extensions_count_(0),
[email protected]ca4b5fa32010-10-09 12:42:18425 ui_thread_(BrowserThread::UI, &loop_),
426 db_thread_(BrowserThread::DB, &loop_),
[email protected]e1dd5622011-12-20 12:28:58427 webkit_thread_(BrowserThread::WEBKIT_DEPRECATED, &loop_),
[email protected]ca4b5fa32010-10-09 12:42:18428 file_thread_(BrowserThread::FILE, &loop_),
[email protected]31dbf9d2011-12-07 01:25:30429 file_user_blocking_thread_(BrowserThread::FILE_USER_BLOCKING, &loop_),
[email protected]90c780ac2013-04-16 09:26:28430 io_thread_(BrowserThread::IO, &loop_) {
[email protected]650b2d52013-02-10 03:41:45431 base::FilePath test_data_dir;
[email protected]e85e34c32011-04-13 18:38:35432 if (!PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir)) {
433 ADD_FAILURE();
434 return;
435 }
436 data_dir_ = test_data_dir.AppendASCII("extensions");
[email protected]bf73f0b2010-02-10 19:26:59437}
438
[email protected]eaa7dd182010-12-14 11:09:00439ExtensionServiceTestBase::~ExtensionServiceTestBase() {
440 // Drop our reference to ExtensionService and TestingProfile, so that they
[email protected]ca4b5fa32010-10-09 12:42:18441 // can be destroyed while BrowserThreads and MessageLoop are still around
442 // (they are used in the destruction process).
[email protected]bf73f0b2010-02-10 19:26:59443 service_ = NULL;
[email protected]b8f50ce2012-11-17 12:37:57444 MessageLoop::current()->RunUntilIdle();
[email protected]c10da4b02010-03-25 14:38:32445 profile_.reset(NULL);
[email protected]b8f50ce2012-11-17 12:37:57446 MessageLoop::current()->RunUntilIdle();
[email protected]bf73f0b2010-02-10 19:26:59447}
448
[email protected]eaa7dd182010-12-14 11:09:00449void ExtensionServiceTestBase::InitializeExtensionService(
[email protected]b3aa7182013-04-25 04:45:23450 const ExtensionServiceTestBase::ExtensionServiceInitParams& params) {
[email protected]bb05cae12012-09-06 00:37:52451 TestingProfile::Builder profile_builder;
[email protected]f2d1f612010-12-09 15:10:17452 // Create a PrefService that only contains user defined preference values.
[email protected]5b199522012-12-22 17:24:44453 PrefServiceMockBuilder builder;
454 builder.WithUserFilePrefs(
[email protected]b3aa7182013-04-25 04:45:23455 params.pref_file, loop_.message_loop_proxy());
[email protected]443e9312013-05-06 06:17:34456 scoped_refptr<user_prefs::PrefRegistrySyncable> registry(
457 new user_prefs::PrefRegistrySyncable);
[email protected]c753f142013-02-10 13:14:04458 scoped_ptr<PrefServiceSyncable> prefs(builder.CreateSyncable(registry));
[email protected]5879cef2013-03-02 17:02:25459 chrome::RegisterUserPrefs(registry);
[email protected]bb05cae12012-09-06 00:37:52460 profile_builder.SetPrefService(prefs.Pass());
[email protected]b3aa7182013-04-25 04:45:23461 profile_builder.SetPath(params.profile_path);
[email protected]bb05cae12012-09-06 00:37:52462 profile_ = profile_builder.Build();
[email protected]bf73f0b2010-02-10 19:26:59463
[email protected]b3aa7182013-04-25 04:45:23464 TestExtensionSystem* system = static_cast<TestExtensionSystem*>(
465 ExtensionSystem::Get(profile_.get()));
466 if (!params.is_first_run) {
467 ExtensionPrefs* prefs = system->CreateExtensionPrefs(
468 CommandLine::ForCurrentProcess(),
469 params.extensions_install_dir);
470 prefs->SetAlertSystemFirstRun();
471 }
472
473 service_ = system->CreateExtensionService(
474 CommandLine::ForCurrentProcess(),
475 params.extensions_install_dir,
476 params.autoupdate_enabled);
[email protected]7f8f24f2012-11-15 19:40:14477 service_->SetFileTaskRunnerForTesting(loop_.message_loop_proxy());
[email protected]bf73f0b2010-02-10 19:26:59478 service_->set_extensions_enabled(true);
479 service_->set_show_extensions_prompts(false);
[email protected]fc332ae2012-11-14 20:17:33480 service_->set_install_updates_when_idle_for_test(false);
[email protected]bf73f0b2010-02-10 19:26:59481
[email protected]fdd679b2012-11-15 20:49:39482 management_policy_ =
483 ExtensionSystem::Get(profile_.get())->management_policy();
[email protected]65187152012-06-02 13:14:14484
[email protected]bf73f0b2010-02-10 19:26:59485 // When we start up, we want to make sure there is no external provider,
486 // since the ExtensionService on Windows will use the Registry as a default
487 // provider and if there is something already registered there then it will
488 // interfere with the tests. Those tests that need an external provider
489 // will register one specifically.
490 service_->ClearProvidersForTesting();
491
[email protected]88e8ec9152013-01-17 04:05:18492#if defined(OS_CHROMEOS)
493 extensions::InstallLimiter::Get(profile_.get())->DisableForTest();
494#endif
495
[email protected]8f512c72011-11-22 21:02:50496 expected_extensions_count_ = 0;
[email protected]bf73f0b2010-02-10 19:26:59497}
498
[email protected]eaa7dd182010-12-14 11:09:00499void ExtensionServiceTestBase::InitializeInstalledExtensionService(
[email protected]650b2d52013-02-10 03:41:45500 const base::FilePath& prefs_file,
501 const base::FilePath& source_install_dir) {
[email protected]bf73f0b2010-02-10 19:26:59502 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
[email protected]650b2d52013-02-10 03:41:45503 base::FilePath path = temp_dir_.path();
[email protected]bb05cae12012-09-06 00:37:52504 path = path.Append(FILE_PATH_LITERAL("TestingExtensionsPath"));
505 file_util::Delete(path, true);
506 file_util::CreateDirectory(path);
[email protected]650b2d52013-02-10 03:41:45507 base::FilePath temp_prefs = path.Append(FILE_PATH_LITERAL("Preferences"));
[email protected]bf73f0b2010-02-10 19:26:59508 file_util::CopyFile(prefs_file, temp_prefs);
509
[email protected]bb05cae12012-09-06 00:37:52510 extensions_install_dir_ = path.Append(FILE_PATH_LITERAL("Extensions"));
[email protected]bf73f0b2010-02-10 19:26:59511 file_util::Delete(extensions_install_dir_, true);
512 file_util::CopyDirectory(source_install_dir, extensions_install_dir_, true);
513
[email protected]b3aa7182013-04-25 04:45:23514 ExtensionServiceInitParams params;
515 params.profile_path = path;
516 params.pref_file = temp_prefs;
517 params.extensions_install_dir = extensions_install_dir_;
518 InitializeExtensionService(params);
[email protected]bf73f0b2010-02-10 19:26:59519}
520
[email protected]eaa7dd182010-12-14 11:09:00521void ExtensionServiceTestBase::InitializeEmptyExtensionService() {
[email protected]b3aa7182013-04-25 04:45:23522 InitializeExtensionServiceHelper(false, true);
[email protected]90310d92011-04-17 07:35:04523}
524
[email protected]406b5a92011-11-08 11:58:26525void ExtensionServiceTestBase::InitializeExtensionProcessManager() {
[email protected]bd306722012-07-11 20:43:59526 static_cast<extensions::TestExtensionSystem*>(
[email protected]749d59a2012-04-05 00:23:24527 ExtensionSystem::Get(profile_.get()))->
[email protected]31d8f5f22012-04-02 15:22:08528 CreateExtensionProcessManager();
[email protected]406b5a92011-11-08 11:58:26529}
530
[email protected]90310d92011-04-17 07:35:04531void ExtensionServiceTestBase::InitializeExtensionServiceWithUpdater() {
[email protected]b3aa7182013-04-25 04:45:23532 InitializeExtensionServiceHelper(true, true);
[email protected]90310d92011-04-17 07:35:04533 service_->updater()->Start();
534}
535
536void ExtensionServiceTestBase::InitializeExtensionServiceHelper(
[email protected]b3aa7182013-04-25 04:45:23537 bool autoupdate_enabled, bool is_first_run) {
[email protected]bf73f0b2010-02-10 19:26:59538 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
[email protected]650b2d52013-02-10 03:41:45539 base::FilePath path = temp_dir_.path();
[email protected]bb05cae12012-09-06 00:37:52540 path = path.Append(FILE_PATH_LITERAL("TestingExtensionsPath"));
541 file_util::Delete(path, true);
542 file_util::CreateDirectory(path);
[email protected]650b2d52013-02-10 03:41:45543 base::FilePath prefs_filename =
544 path.Append(FILE_PATH_LITERAL("TestPreferences"));
[email protected]bb05cae12012-09-06 00:37:52545 extensions_install_dir_ = path.Append(FILE_PATH_LITERAL("Extensions"));
[email protected]bf73f0b2010-02-10 19:26:59546 file_util::Delete(extensions_install_dir_, true);
547 file_util::CreateDirectory(extensions_install_dir_);
548
[email protected]b3aa7182013-04-25 04:45:23549 ExtensionServiceInitParams params;
550 params.profile_path = path;
551 params.pref_file = prefs_filename;
552 params.extensions_install_dir = extensions_install_dir_;
553 params.autoupdate_enabled = autoupdate_enabled;
554 params.is_first_run = is_first_run;
555 InitializeExtensionService(params);
[email protected]bf73f0b2010-02-10 19:26:59556}
557
[email protected]0d6ec3a72011-09-02 02:09:43558void ExtensionServiceTestBase::InitializeRequestContext() {
559 ASSERT_TRUE(profile_.get());
[email protected]31d8f5f22012-04-02 15:22:08560 TestingProfile* profile =
561 static_cast<TestingProfile*>(profile_.get());
[email protected]0d6ec3a72011-09-02 02:09:43562 profile->CreateRequestContext();
563}
564
[email protected]bf73f0b2010-02-10 19:26:59565// static
[email protected]eaa7dd182010-12-14 11:09:00566void ExtensionServiceTestBase::SetUpTestCase() {
[email protected]bf73f0b2010-02-10 19:26:59567 ExtensionErrorReporter::Init(false); // no noisy errors
568}
569
[email protected]eaa7dd182010-12-14 11:09:00570void ExtensionServiceTestBase::SetUp() {
[email protected]bf73f0b2010-02-10 19:26:59571 ExtensionErrorReporter::GetInstance()->ClearErrors();
[email protected]bf73f0b2010-02-10 19:26:59572}
573
[email protected]eaa7dd182010-12-14 11:09:00574class ExtensionServiceTest
[email protected]6c2381d2011-10-19 02:52:53575 : public ExtensionServiceTestBase, public content::NotificationObserver {
[email protected]bf73f0b2010-02-10 19:26:59576 public:
[email protected]612a1cb12012-10-17 13:18:03577 ExtensionServiceTest()
578 : installed_(NULL),
[email protected]41bb80bd2013-05-03 10:56:02579 was_update_(false),
[email protected]612a1cb12012-10-17 13:18:03580 override_external_install_prompt_(
[email protected]00b5d0a52012-10-30 13:13:53581 FeatureSwitch::prompt_for_external_extensions(), false) {
[email protected]432115822011-07-10 15:52:27582 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_LOADED,
[email protected]ad50def52011-10-19 23:17:07583 content::NotificationService::AllSources());
[email protected]432115822011-07-10 15:52:27584 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED,
[email protected]ad50def52011-10-19 23:17:07585 content::NotificationService::AllSources());
[email protected]432115822011-07-10 15:52:27586 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_INSTALLED,
[email protected]ad50def52011-10-19 23:17:07587 content::NotificationService::AllSources());
[email protected]a9b00ac2009-06-25 21:03:23588 }
[email protected]cc655912009-01-29 23:19:19589
[email protected]432115822011-07-10 15:52:27590 virtual void Observe(int type,
[email protected]6c2381d2011-10-19 02:52:53591 const content::NotificationSource& source,
[email protected]49aeab62013-02-07 02:53:11592 const content::NotificationDetails& details) OVERRIDE {
[email protected]432115822011-07-10 15:52:27593 switch (type) {
594 case chrome::NOTIFICATION_EXTENSION_LOADED: {
[email protected]6c2381d2011-10-19 02:52:53595 const Extension* extension =
596 content::Details<const Extension>(details).ptr();
[email protected]00cd9c42010-11-02 20:15:57597 loaded_.push_back(make_scoped_refptr(extension));
[email protected]894bb502009-05-21 22:39:57598 // The tests rely on the errors being in a certain order, which can vary
599 // depending on how filesystem iteration works.
600 std::stable_sort(loaded_.begin(), loaded_.end(), ExtensionsOrder());
601 break;
602 }
603
[email protected]432115822011-07-10 15:52:27604 case chrome::NOTIFICATION_EXTENSION_UNLOADED: {
[email protected]a9f39a312010-12-23 22:14:27605 const Extension* e =
[email protected]1c321ee2012-05-21 03:02:34606 content::Details<extensions::UnloadedExtensionInfo>(
607 details)->extension;
[email protected]9f1087e2009-06-15 17:29:32608 unloaded_id_ = e->id();
[email protected]1c321ee2012-05-21 03:02:34609 extensions::ExtensionList::iterator i =
[email protected]9f1087e2009-06-15 17:29:32610 std::find(loaded_.begin(), loaded_.end(), e);
611 // TODO(erikkay) fix so this can be an assert. Right now the tests
612 // are manually calling clear() on loaded_, so this isn't doable.
613 if (i == loaded_.end())
614 return;
615 loaded_.erase(i);
[email protected]894bb502009-05-21 22:39:57616 break;
[email protected]9f1087e2009-06-15 17:29:32617 }
[email protected]41bb80bd2013-05-03 10:56:02618 case chrome::NOTIFICATION_EXTENSION_INSTALLED: {
619 const extensions::InstalledExtensionInfo* installed_info =
620 content::Details<const extensions::InstalledExtensionInfo>(details)
621 .ptr();
622 installed_ = installed_info->extension;
623 was_update_ = installed_info->is_update;
[email protected]894bb502009-05-21 22:39:57624 break;
[email protected]41bb80bd2013-05-03 10:56:02625 }
[email protected]894bb502009-05-21 22:39:57626
[email protected]894bb502009-05-21 22:39:57627 default:
628 DCHECK(false);
[email protected]3acbd422008-12-08 18:25:00629 }
630 }
631
[email protected]5df038b2012-07-16 19:03:27632 void AddMockExternalProvider(
633 extensions::ExternalProviderInterface* provider) {
[email protected]0a60a2e2010-10-25 16:15:21634 service_->AddProviderForTesting(provider);
[email protected]a1257b12009-06-12 02:51:34635 }
636
[email protected]9197f3b2009-06-02 00:49:27637 protected:
[email protected]d55e7602009-12-16 04:20:42638 void TestExternalProvider(MockExtensionProvider* provider,
[email protected]1d5e58b2013-01-31 08:41:40639 Manifest::Location location);
[email protected]d55e7602009-12-16 04:20:42640
[email protected]650b2d52013-02-10 03:41:45641 void PackCRX(const base::FilePath& dir_path,
642 const base::FilePath& pem_path,
643 const base::FilePath& crx_path) {
[email protected]8d888c12010-11-30 00:00:25644 // Use the existing pem key, if provided.
[email protected]650b2d52013-02-10 03:41:45645 base::FilePath pem_output_path;
[email protected]8d888c12010-11-30 00:00:25646 if (pem_path.value().empty()) {
647 pem_output_path = crx_path.DirName().AppendASCII("temp.pem");
[email protected]8d888c12010-11-30 00:00:25648 } else {
649 ASSERT_TRUE(file_util::PathExists(pem_path));
650 }
[email protected]3ba0fd32010-06-19 05:39:10651
652 ASSERT_TRUE(file_util::Delete(crx_path, false));
[email protected]8d888c12010-11-30 00:00:25653
[email protected]3ba0fd32010-06-19 05:39:10654 scoped_ptr<ExtensionCreator> creator(new ExtensionCreator());
[email protected]8d888c12010-11-30 00:00:25655 ASSERT_TRUE(creator->Run(dir_path,
656 crx_path,
657 pem_path,
[email protected]93d973a2012-01-08 07:38:26658 pem_output_path,
659 ExtensionCreator::kOverwriteCRX));
[email protected]8d888c12010-11-30 00:00:25660
[email protected]3ba0fd32010-06-19 05:39:10661 ASSERT_TRUE(file_util::PathExists(crx_path));
[email protected]8d888c12010-11-30 00:00:25662 }
663
[email protected]53da2c962011-03-03 17:08:05664 // Create a CrxInstaller and start installation. To allow the install
[email protected]b8f50ce2012-11-17 12:37:57665 // to happen, use loop_.RunUntilIdle();. Most tests will not use this
[email protected]145a317b2011-04-12 16:03:46666 // method directly. Instead, use InstallCrx(), which waits for
[email protected]53da2c962011-03-03 17:08:05667 // the crx to be installed and does extra error checking.
[email protected]650b2d52013-02-10 03:41:45668 void StartCRXInstall(const base::FilePath& crx_path) {
[email protected]be083862012-09-01 03:53:45669 StartCRXInstall(crx_path, Extension::NO_FLAGS);
[email protected]8266d662011-07-12 21:53:26670 }
671
[email protected]650b2d52013-02-10 03:41:45672 void StartCRXInstall(const base::FilePath& crx_path, int creation_flags) {
[email protected]b2907fd2011-03-25 16:43:37673 ASSERT_TRUE(file_util::PathExists(crx_path))
674 << "Path does not exist: "<< crx_path.value().c_str();
[email protected]d8c8f25f2011-11-02 18:18:01675 scoped_refptr<CrxInstaller> installer(CrxInstaller::Create(service_, NULL));
[email protected]be083862012-09-01 03:53:45676 installer->set_creation_flags(creation_flags);
677 if (!(creation_flags & Extension::WAS_INSTALLED_BY_DEFAULT)) {
678 installer->set_allow_silent_install(true);
679 }
[email protected]53da2c962011-03-03 17:08:05680 installer->InstallCrx(crx_path);
681 }
682
[email protected]8f512c72011-11-22 21:02:50683 enum InstallState {
684 INSTALL_FAILED,
685 INSTALL_UPDATED,
[email protected]695b5712012-12-06 23:55:28686 INSTALL_NEW,
687 INSTALL_WITHOUT_LOAD,
[email protected]8f512c72011-11-22 21:02:50688 };
689
[email protected]650b2d52013-02-10 03:41:45690 const Extension* PackAndInstallCRX(const base::FilePath& dir_path,
691 const base::FilePath& pem_path,
[email protected]be083862012-09-01 03:53:45692 InstallState install_state,
693 int creation_flags) {
[email protected]650b2d52013-02-10 03:41:45694 base::FilePath crx_path;
[email protected]ea1a3f62012-11-16 20:34:23695 base::ScopedTempDir temp_dir;
[email protected]8f512c72011-11-22 21:02:50696 EXPECT_TRUE(temp_dir.CreateUniqueTempDir());
697 crx_path = temp_dir.path().AppendASCII("temp.crx");
698
699 PackCRX(dir_path, pem_path, crx_path);
[email protected]be083862012-09-01 03:53:45700 return InstallCRX(crx_path, install_state, creation_flags);
701 }
702
[email protected]650b2d52013-02-10 03:41:45703 const Extension* PackAndInstallCRX(const base::FilePath& dir_path,
704 const base::FilePath& pem_path,
[email protected]be083862012-09-01 03:53:45705 InstallState install_state) {
706 return PackAndInstallCRX(dir_path, pem_path, install_state,
707 Extension::NO_FLAGS);
[email protected]145a317b2011-04-12 16:03:46708 }
709
[email protected]650b2d52013-02-10 03:41:45710 const Extension* PackAndInstallCRX(const base::FilePath& dir_path,
[email protected]8f512c72011-11-22 21:02:50711 InstallState install_state) {
[email protected]650b2d52013-02-10 03:41:45712 return PackAndInstallCRX(dir_path, base::FilePath(), install_state,
[email protected]be083862012-09-01 03:53:45713 Extension::NO_FLAGS);
714 }
715
[email protected]650b2d52013-02-10 03:41:45716 const Extension* InstallCRX(const base::FilePath& path,
[email protected]be083862012-09-01 03:53:45717 InstallState install_state,
718 int creation_flags) {
719 StartCRXInstall(path, creation_flags);
720 return WaitForCrxInstall(path, install_state);
[email protected]8f512c72011-11-22 21:02:50721 }
722
[email protected]65187152012-06-02 13:14:14723 // Attempts to install an extension. Use INSTALL_FAILED if the installation
724 // is expected to fail.
[email protected]650b2d52013-02-10 03:41:45725 const Extension* InstallCRX(const base::FilePath& path,
[email protected]8f512c72011-11-22 21:02:50726 InstallState install_state) {
[email protected]be083862012-09-01 03:53:45727 return InstallCRX(path, install_state, Extension::NO_FLAGS);
[email protected]8f512c72011-11-22 21:02:50728 }
729
[email protected]650b2d52013-02-10 03:41:45730 const Extension* InstallCRXFromWebStore(const base::FilePath& path,
[email protected]8f512c72011-11-22 21:02:50731 InstallState install_state) {
[email protected]be083862012-09-01 03:53:45732 StartCRXInstall(path, Extension::FROM_WEBSTORE);
[email protected]8f512c72011-11-22 21:02:50733 return WaitForCrxInstall(path, install_state);
734 }
735
[email protected]650b2d52013-02-10 03:41:45736 const Extension* InstallCRXWithLocation(const base::FilePath& crx_path,
[email protected]1d5e58b2013-01-31 08:41:40737 Manifest::Location install_location,
[email protected]8f512c72011-11-22 21:02:50738 InstallState install_state) {
739 EXPECT_TRUE(file_util::PathExists(crx_path))
[email protected]145a317b2011-04-12 16:03:46740 << "Path does not exist: "<< crx_path.value().c_str();
[email protected]14908b72011-04-20 06:54:36741 // no client (silent install)
[email protected]d8c8f25f2011-11-02 18:18:01742 scoped_refptr<CrxInstaller> installer(CrxInstaller::Create(service_, NULL));
[email protected]145a317b2011-04-12 16:03:46743 installer->set_install_source(install_location);
744 installer->InstallCrx(crx_path);
745
[email protected]8f512c72011-11-22 21:02:50746 return WaitForCrxInstall(crx_path, install_state);
[email protected]145a317b2011-04-12 16:03:46747 }
748
[email protected]65187152012-06-02 13:14:14749 // Wait for a CrxInstaller to finish. Used by InstallCRX. Set the
750 // |install_state| to INSTALL_FAILED if the installation is expected to fail.
[email protected]8f512c72011-11-22 21:02:50751 // Returns an Extension pointer if the install succeeded, NULL otherwise.
[email protected]650b2d52013-02-10 03:41:45752 const Extension* WaitForCrxInstall(const base::FilePath& path,
[email protected]8f512c72011-11-22 21:02:50753 InstallState install_state) {
[email protected]b8f50ce2012-11-17 12:37:57754 loop_.RunUntilIdle();
[email protected]fc670822011-12-17 09:33:49755 std::vector<string16> errors = GetErrors();
[email protected]8f512c72011-11-22 21:02:50756 const Extension* extension = NULL;
757 if (install_state != INSTALL_FAILED) {
758 if (install_state == INSTALL_NEW)
759 ++expected_extensions_count_;
[email protected]902f7cd2009-05-22 19:02:19760
[email protected]a57209872009-05-04 22:53:14761 EXPECT_TRUE(installed_) << path.value();
[email protected]41bb80bd2013-05-03 10:56:02762 // If and only if INSTALL_UPDATED, it should have the is_update flag.
763 EXPECT_EQ(install_state == INSTALL_UPDATED, was_update_)
764 << path.value();
[email protected]bb28e062009-02-27 17:19:18765 EXPECT_EQ(0u, errors.size()) << path.value();
[email protected]695b5712012-12-06 23:55:28766
767 if (install_state == INSTALL_WITHOUT_LOAD) {
768 EXPECT_EQ(0u, loaded_.size()) << path.value();
769 } else {
770 EXPECT_EQ(1u, loaded_.size()) << path.value();
771 EXPECT_EQ(expected_extensions_count_, service_->extensions()->size()) <<
772 path.value();
773 extension = loaded_[0];
774 EXPECT_TRUE(service_->GetExtensionById(extension->id(), false)) <<
775 path.value();
776 }
777
[email protected]fc670822011-12-17 09:33:49778 for (std::vector<string16>::iterator err = errors.begin();
[email protected]bb28e062009-02-27 17:19:18779 err != errors.end(); ++err) {
[email protected]37eeb5a2009-02-26 23:36:17780 LOG(ERROR) << *err;
781 }
[email protected]cc655912009-01-29 23:19:19782 } else {
[email protected]a57209872009-05-04 22:53:14783 EXPECT_FALSE(installed_) << path.value();
[email protected]86a274072009-06-11 02:06:45784 EXPECT_EQ(0u, loaded_.size()) << path.value();
[email protected]bb28e062009-02-27 17:19:18785 EXPECT_EQ(1u, errors.size()) << path.value();
[email protected]cc655912009-01-29 23:19:19786 }
[email protected]bb28e062009-02-27 17:19:18787
[email protected]a57209872009-05-04 22:53:14788 installed_ = NULL;
[email protected]41bb80bd2013-05-03 10:56:02789 was_update_ = false;
[email protected]894bb502009-05-21 22:39:57790 loaded_.clear();
[email protected]bb28e062009-02-27 17:19:18791 ExtensionErrorReporter::GetInstance()->ClearErrors();
[email protected]8f512c72011-11-22 21:02:50792 return extension;
[email protected]cc655912009-01-29 23:19:19793 }
794
[email protected]4416c5a2010-06-26 01:28:57795 enum UpdateState {
796 FAILED_SILENTLY,
797 FAILED,
798 UPDATED,
799 INSTALLED,
800 ENABLED
801 };
802
[email protected]98270432012-09-11 20:51:24803 void BlackListWebGL() {
804 static const std::string json_blacklist =
805 "{\n"
806 " \"name\": \"gpu blacklist\",\n"
807 " \"version\": \"1.0\",\n"
808 " \"entries\": [\n"
809 " {\n"
810 " \"id\": 1,\n"
[email protected]3d346572013-03-22 19:59:55811 " \"features\": [\"webgl\"]\n"
[email protected]98270432012-09-11 20:51:24812 " }\n"
813 " ]\n"
814 "}";
[email protected]7e343152012-09-20 21:49:53815 content::GPUInfo gpu_info;
816 content::GpuDataManager::GetInstance()->InitializeForTesting(
817 json_blacklist, gpu_info);
[email protected]98270432012-09-11 20:51:24818 }
819
[email protected]650b2d52013-02-10 03:41:45820 void UpdateExtension(const std::string& id, const base::FilePath& in_path,
[email protected]4416c5a2010-06-26 01:28:57821 UpdateState expected_state) {
[email protected]7577a5c52009-07-30 06:21:58822 ASSERT_TRUE(file_util::PathExists(in_path));
[email protected]e957fe52009-06-23 16:51:05823
[email protected]7577a5c52009-07-30 06:21:58824 // We need to copy this to a temporary location because Update() will delete
825 // it.
[email protected]650b2d52013-02-10 03:41:45826 base::FilePath path = temp_dir_.path();
[email protected]a1295ba22009-09-02 03:33:39827 path = path.Append(in_path.BaseName());
[email protected]7577a5c52009-07-30 06:21:58828 ASSERT_TRUE(file_util::CopyFile(in_path, path));
[email protected]e957fe52009-06-23 16:51:05829
[email protected]4416c5a2010-06-26 01:28:57830 int previous_enabled_extension_count =
831 service_->extensions()->size();
832 int previous_installed_extension_count =
833 previous_enabled_extension_count +
834 service_->disabled_extensions()->size();
835
[email protected]420a0ec2011-06-01 01:07:03836 service_->UpdateExtension(id, path, GURL(), NULL);
[email protected]b8f50ce2012-11-17 12:37:57837 loop_.RunUntilIdle();
[email protected]f3113e232010-06-25 01:36:40838
[email protected]fc670822011-12-17 09:33:49839 std::vector<string16> errors = GetErrors();
[email protected]4416c5a2010-06-26 01:28:57840 int error_count = errors.size();
841 int enabled_extension_count =
842 service_->extensions()->size();
843 int installed_extension_count =
844 enabled_extension_count + service_->disabled_extensions()->size();
845
846 int expected_error_count = (expected_state == FAILED) ? 1 : 0;
847 EXPECT_EQ(expected_error_count, error_count) << path.value();
848
849 if (expected_state <= FAILED) {
850 EXPECT_EQ(previous_enabled_extension_count,
851 enabled_extension_count);
852 EXPECT_EQ(previous_installed_extension_count,
853 installed_extension_count);
[email protected]e957fe52009-06-23 16:51:05854 } else {
[email protected]4416c5a2010-06-26 01:28:57855 int expected_installed_extension_count =
856 (expected_state >= INSTALLED) ? 1 : 0;
857 int expected_enabled_extension_count =
858 (expected_state >= ENABLED) ? 1 : 0;
859 EXPECT_EQ(expected_installed_extension_count,
860 installed_extension_count);
861 EXPECT_EQ(expected_enabled_extension_count,
862 enabled_extension_count);
[email protected]e957fe52009-06-23 16:51:05863 }
[email protected]7577a5c52009-07-30 06:21:58864
[email protected]31d8f5f22012-04-02 15:22:08865 // Update() should the temporary input file.
[email protected]7577a5c52009-07-30 06:21:58866 EXPECT_FALSE(file_util::PathExists(path));
[email protected]e957fe52009-06-23 16:51:05867 }
868
[email protected]fa2416f2011-05-03 08:41:20869 void TerminateExtension(const std::string& id) {
870 const Extension* extension = service_->GetInstalledExtension(id);
871 if (!extension) {
872 ADD_FAILURE();
873 return;
874 }
875 service_->TrackTerminatedExtensionForTest(extension);
876 }
877
878 size_t GetPrefKeyCount() {
[email protected]43d3bf82011-04-11 07:46:58879 const DictionaryValue* dict =
880 profile_->GetPrefs()->GetDictionary("extensions.settings");
[email protected]fa2416f2011-05-03 08:41:20881 if (!dict) {
882 ADD_FAILURE();
883 return 0;
884 }
885 return dict->size();
886 }
887
888 void UninstallExtension(const std::string& id, bool use_helper) {
889 // Verify that the extension is installed.
[email protected]650b2d52013-02-10 03:41:45890 base::FilePath extension_path = extensions_install_dir_.AppendASCII(id);
[email protected]fa2416f2011-05-03 08:41:20891 EXPECT_TRUE(file_util::PathExists(extension_path));
892 size_t pref_key_count = GetPrefKeyCount();
893 EXPECT_GT(pref_key_count, 0u);
894 ValidateIntegerPref(id, "state", Extension::ENABLED);
895
896 // Uninstall it.
897 if (use_helper) {
898 EXPECT_TRUE(ExtensionService::UninstallExtensionHelper(service_, id));
899 } else {
900 EXPECT_TRUE(service_->UninstallExtension(id, false, NULL));
901 }
[email protected]8f512c72011-11-22 21:02:50902 --expected_extensions_count_;
[email protected]fa2416f2011-05-03 08:41:20903
904 // We should get an unload notification.
905 EXPECT_FALSE(unloaded_id_.empty());
906 EXPECT_EQ(id, unloaded_id_);
907
908 // Verify uninstalled state.
909 size_t new_pref_key_count = GetPrefKeyCount();
910 if (new_pref_key_count == pref_key_count) {
911 ValidateIntegerPref(id, "location",
912 Extension::EXTERNAL_EXTENSION_UNINSTALLED);
913 } else {
914 EXPECT_EQ(new_pref_key_count, pref_key_count - 1);
915 }
916
917 // The extension should not be in the service anymore.
918 EXPECT_FALSE(service_->GetInstalledExtension(id));
[email protected]b8f50ce2012-11-17 12:37:57919 loop_.RunUntilIdle();
[email protected]fa2416f2011-05-03 08:41:20920
921 // The directory should be gone.
922 EXPECT_FALSE(file_util::PathExists(extension_path));
923 }
924
925 void ValidatePrefKeyCount(size_t count) {
926 EXPECT_EQ(count, GetPrefKeyCount());
[email protected]25b34332009-06-05 21:53:19927 }
928
[email protected]6b75ec32009-08-14 06:37:18929 void ValidateBooleanPref(const std::string& extension_id,
[email protected]e2194742010-08-12 05:54:34930 const std::string& pref_path,
[email protected]c6d474f82009-12-16 21:11:06931 bool expected_val) {
[email protected]e2194742010-08-12 05:54:34932 std::string msg = " while checking: ";
933 msg += extension_id;
934 msg += " ";
[email protected]6b75ec32009-08-14 06:37:18935 msg += pref_path;
[email protected]e2194742010-08-12 05:54:34936 msg += " == ";
937 msg += expected_val ? "true" : "false";
[email protected]6b75ec32009-08-14 06:37:18938
[email protected]2fb7dc982010-09-29 12:24:28939 PrefService* prefs = profile_->GetPrefs();
940 const DictionaryValue* dict =
941 prefs->GetDictionary("extensions.settings");
[email protected]6b75ec32009-08-14 06:37:18942 ASSERT_TRUE(dict != NULL) << msg;
[email protected]a61890e2012-07-27 22:27:11943 const DictionaryValue* pref = NULL;
[email protected]e2194742010-08-12 05:54:34944 ASSERT_TRUE(dict->GetDictionary(extension_id, &pref)) << msg;
[email protected]6b75ec32009-08-14 06:37:18945 EXPECT_TRUE(pref != NULL) << msg;
946 bool val;
[email protected]4c91487e2009-10-02 04:11:04947 ASSERT_TRUE(pref->GetBoolean(pref_path, &val)) << msg;
[email protected]c6d474f82009-12-16 21:11:06948 EXPECT_EQ(expected_val, val) << msg;
[email protected]6b75ec32009-08-14 06:37:18949 }
950
951 bool IsPrefExist(const std::string& extension_id,
[email protected]e2194742010-08-12 05:54:34952 const std::string& pref_path) {
[email protected]2fb7dc982010-09-29 12:24:28953 const DictionaryValue* dict =
954 profile_->GetPrefs()->GetDictionary("extensions.settings");
[email protected]6b75ec32009-08-14 06:37:18955 if (dict == NULL) return false;
[email protected]a61890e2012-07-27 22:27:11956 const DictionaryValue* pref = NULL;
[email protected]e2194742010-08-12 05:54:34957 if (!dict->GetDictionary(extension_id, &pref)) {
[email protected]6b75ec32009-08-14 06:37:18958 return false;
959 }
960 if (pref == NULL) {
961 return false;
962 }
963 bool val;
964 if (!pref->GetBoolean(pref_path, &val)) {
965 return false;
966 }
967 return true;
968 }
969
970 void ValidateIntegerPref(const std::string& extension_id,
[email protected]e2194742010-08-12 05:54:34971 const std::string& pref_path,
[email protected]c6d474f82009-12-16 21:11:06972 int expected_val) {
[email protected]e2194742010-08-12 05:54:34973 std::string msg = " while checking: ";
974 msg += extension_id;
975 msg += " ";
[email protected]25b34332009-06-05 21:53:19976 msg += pref_path;
[email protected]e2194742010-08-12 05:54:34977 msg += " == ";
978 msg += base::IntToString(expected_val);
[email protected]25b34332009-06-05 21:53:19979
[email protected]2fb7dc982010-09-29 12:24:28980 PrefService* prefs = profile_->GetPrefs();
981 const DictionaryValue* dict =
982 prefs->GetDictionary("extensions.settings");
[email protected]25b34332009-06-05 21:53:19983 ASSERT_TRUE(dict != NULL) << msg;
[email protected]a61890e2012-07-27 22:27:11984 const DictionaryValue* pref = NULL;
[email protected]e2194742010-08-12 05:54:34985 ASSERT_TRUE(dict->GetDictionary(extension_id, &pref)) << msg;
[email protected]25b34332009-06-05 21:53:19986 EXPECT_TRUE(pref != NULL) << msg;
987 int val;
[email protected]4c91487e2009-10-02 04:11:04988 ASSERT_TRUE(pref->GetInteger(pref_path, &val)) << msg;
[email protected]c6d474f82009-12-16 21:11:06989 EXPECT_EQ(expected_val, val) << msg;
990 }
991
992 void ValidateStringPref(const std::string& extension_id,
[email protected]e2194742010-08-12 05:54:34993 const std::string& pref_path,
[email protected]c6d474f82009-12-16 21:11:06994 const std::string& expected_val) {
[email protected]e2194742010-08-12 05:54:34995 std::string msg = " while checking: ";
996 msg += extension_id;
997 msg += ".manifest.";
[email protected]c6d474f82009-12-16 21:11:06998 msg += pref_path;
[email protected]e2194742010-08-12 05:54:34999 msg += " == ";
1000 msg += expected_val;
[email protected]c6d474f82009-12-16 21:11:061001
[email protected]2fb7dc982010-09-29 12:24:281002 const DictionaryValue* dict =
1003 profile_->GetPrefs()->GetDictionary("extensions.settings");
[email protected]c6d474f82009-12-16 21:11:061004 ASSERT_TRUE(dict != NULL) << msg;
[email protected]a61890e2012-07-27 22:27:111005 const DictionaryValue* pref = NULL;
[email protected]c6d474f82009-12-16 21:11:061006 std::string manifest_path = extension_id + ".manifest";
[email protected]e2194742010-08-12 05:54:341007 ASSERT_TRUE(dict->GetDictionary(manifest_path, &pref)) << msg;
[email protected]c6d474f82009-12-16 21:11:061008 EXPECT_TRUE(pref != NULL) << msg;
1009 std::string val;
1010 ASSERT_TRUE(pref->GetString(pref_path, &val)) << msg;
1011 EXPECT_EQ(expected_val, val) << msg;
[email protected]25b34332009-06-05 21:53:191012 }
1013
[email protected]8d888c12010-11-30 00:00:251014 void SetPref(const std::string& extension_id,
1015 const std::string& pref_path,
1016 Value* value,
1017 const std::string& msg) {
[email protected]43d3bf82011-04-11 07:46:581018 DictionaryPrefUpdate update(profile_->GetPrefs(), "extensions.settings");
1019 DictionaryValue* dict = update.Get();
[email protected]8d888c12010-11-30 00:00:251020 ASSERT_TRUE(dict != NULL) << msg;
1021 DictionaryValue* pref = NULL;
1022 ASSERT_TRUE(dict->GetDictionary(extension_id, &pref)) << msg;
1023 EXPECT_TRUE(pref != NULL) << msg;
1024 pref->Set(pref_path, value);
1025 }
1026
[email protected]6b75ec32009-08-14 06:37:181027 void SetPrefInteg(const std::string& extension_id,
[email protected]e2194742010-08-12 05:54:341028 const std::string& pref_path,
[email protected]6b75ec32009-08-14 06:37:181029 int value) {
[email protected]e2194742010-08-12 05:54:341030 std::string msg = " while setting: ";
1031 msg += extension_id;
1032 msg += " ";
[email protected]a1257b12009-06-12 02:51:341033 msg += pref_path;
[email protected]e2194742010-08-12 05:54:341034 msg += " = ";
1035 msg += base::IntToString(value);
[email protected]a1257b12009-06-12 02:51:341036
[email protected]8d888c12010-11-30 00:00:251037 SetPref(extension_id, pref_path, Value::CreateIntegerValue(value), msg);
1038 }
1039
1040 void SetPrefBool(const std::string& extension_id,
1041 const std::string& pref_path,
1042 bool value) {
1043 std::string msg = " while setting: ";
1044 msg += extension_id + " " + pref_path;
1045 msg += " = ";
1046 msg += (value ? "true" : "false");
1047
1048 SetPref(extension_id, pref_path, Value::CreateBooleanValue(value), msg);
1049 }
1050
1051 void ClearPref(const std::string& extension_id,
1052 const std::string& pref_path) {
1053 std::string msg = " while clearing: ";
1054 msg += extension_id + " " + pref_path;
1055
[email protected]43d3bf82011-04-11 07:46:581056 DictionaryPrefUpdate update(profile_->GetPrefs(), "extensions.settings");
1057 DictionaryValue* dict = update.Get();
[email protected]a1257b12009-06-12 02:51:341058 ASSERT_TRUE(dict != NULL) << msg;
1059 DictionaryValue* pref = NULL;
[email protected]e2194742010-08-12 05:54:341060 ASSERT_TRUE(dict->GetDictionary(extension_id, &pref)) << msg;
[email protected]a1257b12009-06-12 02:51:341061 EXPECT_TRUE(pref != NULL) << msg;
[email protected]8d888c12010-11-30 00:00:251062 pref->Remove(pref_path, NULL);
1063 }
1064
1065 void SetPrefStringSet(const std::string& extension_id,
1066 const std::string& pref_path,
1067 const std::set<std::string>& value) {
1068 std::string msg = " while setting: ";
1069 msg += extension_id + " " + pref_path;
1070
1071 ListValue* list_value = new ListValue();
1072 for (std::set<std::string>::const_iterator iter = value.begin();
1073 iter != value.end(); ++iter)
1074 list_value->Append(Value::CreateStringValue(*iter));
1075
1076 SetPref(extension_id, pref_path, list_value, msg);
[email protected]a1257b12009-06-12 02:51:341077 }
1078
[email protected]ebd71962012-12-20 02:56:551079 void InitPluginService() {
1080#if defined(ENABLE_PLUGINS)
1081 PluginService::GetInstance()->Init();
1082#endif
1083 }
1084
[email protected]25b34332009-06-05 21:53:191085 protected:
[email protected]1c321ee2012-05-21 03:02:341086 extensions::ExtensionList loaded_;
[email protected]894bb502009-05-21 22:39:571087 std::string unloaded_id_;
[email protected]9adb9692010-10-29 23:14:021088 const Extension* installed_;
[email protected]41bb80bd2013-05-03 10:56:021089 bool was_update_;
[email protected]00b5d0a52012-10-30 13:13:531090 FeatureSwitch::ScopedOverride override_external_install_prompt_;
[email protected]894bb502009-05-21 22:39:571091
[email protected]6014d672008-12-05 00:38:251092 private:
[email protected]6c2381d2011-10-19 02:52:531093 content::NotificationRegistrar registrar_;
[email protected]bb28e062009-02-27 17:19:181094};
[email protected]6014d672008-12-05 00:38:251095
[email protected]0349ab5d2010-08-11 21:41:571096// Receives notifications from a PackExtensionJob, indicating either that
1097// packing succeeded or that there was some error.
[email protected]d9ede582012-08-14 19:21:381098class PackExtensionTestClient : public extensions::PackExtensionJob::Client {
[email protected]0349ab5d2010-08-11 21:41:571099 public:
[email protected]650b2d52013-02-10 03:41:451100 PackExtensionTestClient(const base::FilePath& expected_crx_path,
1101 const base::FilePath& expected_private_key_path);
1102 virtual void OnPackSuccess(const base::FilePath& crx_path,
1103 const base::FilePath& private_key_path) OVERRIDE;
[email protected]93d973a2012-01-08 07:38:261104 virtual void OnPackFailure(const std::string& error_message,
[email protected]49aeab62013-02-07 02:53:111105 ExtensionCreator::ErrorType type) OVERRIDE;
[email protected]0349ab5d2010-08-11 21:41:571106
1107 private:
[email protected]650b2d52013-02-10 03:41:451108 const base::FilePath expected_crx_path_;
1109 const base::FilePath expected_private_key_path_;
[email protected]0349ab5d2010-08-11 21:41:571110 DISALLOW_COPY_AND_ASSIGN(PackExtensionTestClient);
1111};
1112
1113PackExtensionTestClient::PackExtensionTestClient(
[email protected]650b2d52013-02-10 03:41:451114 const base::FilePath& expected_crx_path,
1115 const base::FilePath& expected_private_key_path)
[email protected]0349ab5d2010-08-11 21:41:571116 : expected_crx_path_(expected_crx_path),
1117 expected_private_key_path_(expected_private_key_path) {}
1118
1119// If packing succeeded, we make sure that the package names match our
1120// expectations.
[email protected]650b2d52013-02-10 03:41:451121void PackExtensionTestClient::OnPackSuccess(
1122 const base::FilePath& crx_path,
1123 const base::FilePath& private_key_path) {
[email protected]0349ab5d2010-08-11 21:41:571124 // We got the notification and processed it; we don't expect any further tasks
1125 // to be posted to the current thread, so we should stop blocking and continue
1126 // on with the rest of the test.
1127 // This call to |Quit()| matches the call to |Run()| in the
1128 // |PackPunctuatedExtension| test.
1129 MessageLoop::current()->Quit();
1130 EXPECT_EQ(expected_crx_path_.value(), crx_path.value());
1131 EXPECT_EQ(expected_private_key_path_.value(), private_key_path.value());
1132 ASSERT_TRUE(file_util::PathExists(private_key_path));
1133}
1134
1135// The tests are designed so that we never expect to see a packing error.
[email protected]93d973a2012-01-08 07:38:261136void PackExtensionTestClient::OnPackFailure(const std::string& error_message,
1137 ExtensionCreator::ErrorType type) {
1138 if (type == ExtensionCreator::kCRXExists)
1139 FAIL() << "Packing should not fail.";
1140 else
1141 FAIL() << "Existing CRX should have been overwritten.";
[email protected]0349ab5d2010-08-11 21:41:571142}
1143
[email protected]54cb3c92009-02-17 22:30:211144// Test loading good extensions from the profile directory.
[email protected]d9a61e12012-11-14 02:43:471145TEST_F(ExtensionServiceTest, LoadAllExtensionsFromDirectorySuccess) {
[email protected]ebd71962012-12-20 02:56:551146 InitPluginService();
[email protected]183d4b82011-11-11 18:50:261147
[email protected]a9b00ac2009-06-25 21:03:231148 // Initialize the test dir with a good Preferences/extensions.
[email protected]650b2d52013-02-10 03:41:451149 base::FilePath source_install_dir = data_dir_
[email protected]a9b00ac2009-06-25 21:03:231150 .AppendASCII("good")
1151 .AppendASCII("Extensions");
[email protected]650b2d52013-02-10 03:41:451152 base::FilePath pref_path = source_install_dir
[email protected]a9b00ac2009-06-25 21:03:231153 .DirName()
1154 .AppendASCII("Preferences");
[email protected]eaa7dd182010-12-14 11:09:001155 InitializeInstalledExtensionService(pref_path, source_install_dir);
[email protected]6014d672008-12-05 00:38:251156
[email protected]9f1087e2009-06-15 17:29:321157 service_->Init();
[email protected]6014d672008-12-05 00:38:251158
[email protected]e50013c32010-08-18 21:05:241159 uint32 expected_num_extensions = 3u;
[email protected]e50013c32010-08-18 21:05:241160 ASSERT_EQ(expected_num_extensions, loaded_.size());
[email protected]6014d672008-12-05 00:38:251161
[email protected]fbcc40302009-06-12 20:45:451162 EXPECT_EQ(std::string(good0), loaded_[0]->id());
[email protected]e1cec06c2008-12-18 01:22:231163 EXPECT_EQ(std::string("My extension 1"),
[email protected]894bb502009-05-21 22:39:571164 loaded_[0]->name());
[email protected]e1cec06c2008-12-18 01:22:231165 EXPECT_EQ(std::string("The first extension that I made."),
[email protected]894bb502009-05-21 22:39:571166 loaded_[0]->description());
[email protected]1d5e58b2013-01-31 08:41:401167 EXPECT_EQ(Manifest::INTERNAL, loaded_[0]->location());
[email protected]61b411612009-11-10 23:17:411168 EXPECT_TRUE(service_->GetExtensionById(loaded_[0]->id(), false));
[email protected]e50013c32010-08-18 21:05:241169 EXPECT_EQ(expected_num_extensions, service_->extensions()->size());
[email protected]eab9b452009-01-23 20:48:591170
[email protected]25b34332009-06-05 21:53:191171 ValidatePrefKeyCount(3);
[email protected]e2194742010-08-12 05:54:341172 ValidateIntegerPref(good0, "state", Extension::ENABLED);
[email protected]1d5e58b2013-01-31 08:41:401173 ValidateIntegerPref(good0, "location", Manifest::INTERNAL);
[email protected]e2194742010-08-12 05:54:341174 ValidateIntegerPref(good1, "state", Extension::ENABLED);
[email protected]1d5e58b2013-01-31 08:41:401175 ValidateIntegerPref(good1, "location", Manifest::INTERNAL);
[email protected]e2194742010-08-12 05:54:341176 ValidateIntegerPref(good2, "state", Extension::ENABLED);
[email protected]1d5e58b2013-01-31 08:41:401177 ValidateIntegerPref(good2, "location", Manifest::INTERNAL);
[email protected]25b34332009-06-05 21:53:191178
[email protected]06e8b8ff2011-07-13 15:03:471179 URLPatternSet expected_patterns;
1180 AddPattern(&expected_patterns, "file:///*");
1181 AddPattern(&expected_patterns, "http://*.google.com/*");
1182 AddPattern(&expected_patterns, "https://*.google.com/*");
[email protected]9adb9692010-10-29 23:14:021183 const Extension* extension = loaded_[0];
[email protected]334ec0a2013-03-24 01:36:161184 const extensions::UserScriptList& scripts =
1185 extensions::ContentScriptsInfo::GetContentScripts(extension);
[email protected]e66de892009-03-20 20:38:431186 ASSERT_EQ(2u, scripts.size());
[email protected]06e8b8ff2011-07-13 15:03:471187 EXPECT_EQ(expected_patterns, scripts[0].url_patterns());
[email protected]e66de892009-03-20 20:38:431188 EXPECT_EQ(2u, scripts[0].js_scripts().size());
[email protected]052c92702010-06-25 07:25:521189 ExtensionResource resource00(extension->id(),
1190 scripts[0].js_scripts()[0].extension_root(),
[email protected]9194b3f2009-10-20 15:27:211191 scripts[0].js_scripts()[0].relative_path());
[email protected]15476932013-04-12 05:17:151192 base::FilePath expected_path =
1193 base::MakeAbsoluteFilePath(extension->path().AppendASCII("script1.js"));
[email protected]a14b16b2009-10-28 12:41:291194 EXPECT_TRUE(resource00.ComparePathWithDefault(expected_path));
[email protected]052c92702010-06-25 07:25:521195 ExtensionResource resource01(extension->id(),
1196 scripts[0].js_scripts()[1].extension_root(),
[email protected]9194b3f2009-10-20 15:27:211197 scripts[0].js_scripts()[1].relative_path());
[email protected]15476932013-04-12 05:17:151198 expected_path =
1199 base::MakeAbsoluteFilePath(extension->path().AppendASCII("script2.js"));
[email protected]a14b16b2009-10-28 12:41:291200 EXPECT_TRUE(resource01.ComparePathWithDefault(expected_path));
[email protected]098fa7a2013-03-08 22:11:171201 EXPECT_TRUE(!extensions::PluginInfo::HasPlugins(extension));
[email protected]06e8b8ff2011-07-13 15:03:471202 EXPECT_EQ(1u, scripts[1].url_patterns().patterns().size());
1203 EXPECT_EQ("http://*.news.com/*",
1204 scripts[1].url_patterns().begin()->GetAsString());
[email protected]052c92702010-06-25 07:25:521205 ExtensionResource resource10(extension->id(),
1206 scripts[1].js_scripts()[0].extension_root(),
[email protected]9194b3f2009-10-20 15:27:211207 scripts[1].js_scripts()[0].relative_path());
[email protected]a14b16b2009-10-28 12:41:291208 expected_path =
1209 extension->path().AppendASCII("js_files").AppendASCII("script3.js");
[email protected]15476932013-04-12 05:17:151210 expected_path = base::MakeAbsoluteFilePath(expected_path);
[email protected]a14b16b2009-10-28 12:41:291211 EXPECT_TRUE(resource10.ComparePathWithDefault(expected_path));
[email protected]06e8b8ff2011-07-13 15:03:471212
1213 expected_patterns.ClearPatterns();
1214 AddPattern(&expected_patterns, "http://*.google.com/*");
1215 AddPattern(&expected_patterns, "https://*.google.com/*");
[email protected]902fd7b2011-07-27 18:42:311216 EXPECT_EQ(expected_patterns,
1217 extension->GetActivePermissions()->explicit_hosts());
[email protected]6014d672008-12-05 00:38:251218
[email protected]25b34332009-06-05 21:53:191219 EXPECT_EQ(std::string(good1), loaded_[1]->id());
[email protected]894bb502009-05-21 22:39:571220 EXPECT_EQ(std::string("My extension 2"), loaded_[1]->name());
[email protected]007b3f82013-04-09 08:46:451221 EXPECT_EQ(std::string(), loaded_[1]->description());
[email protected]81067e02009-07-27 15:12:091222 EXPECT_EQ(loaded_[1]->GetResourceURL("background.html"),
[email protected]9367eabc2013-03-01 01:29:291223 extensions::BackgroundInfo::GetBackgroundURL(loaded_[1]));
[email protected]334ec0a2013-03-24 01:36:161224 EXPECT_EQ(
1225 0u, extensions::ContentScriptsInfo::GetContentScripts(loaded_[1]).size());
[email protected]098fa7a2013-03-08 22:11:171226
[email protected]b8fb3032011-04-29 18:45:561227 // We don't parse the plugins section on Chrome OS.
1228#if defined(OS_CHROMEOS)
[email protected]098fa7a2013-03-08 22:11:171229 EXPECT_TRUE(!extensions::PluginInfo::HasPlugins(loaded_[1]));
[email protected]b8fb3032011-04-29 18:45:561230#else
[email protected]098fa7a2013-03-08 22:11:171231 ASSERT_TRUE(extensions::PluginInfo::HasPlugins(loaded_[1]));
1232 const std::vector<extensions::PluginInfo>* plugins =
1233 extensions::PluginInfo::GetPlugins(loaded_[1]);
1234 ASSERT_TRUE(plugins);
1235 ASSERT_EQ(2u, plugins->size());
[email protected]c533bb22009-06-03 19:06:111236 EXPECT_EQ(loaded_[1]->path().AppendASCII("content_plugin.dll").value(),
[email protected]098fa7a2013-03-08 22:11:171237 plugins->at(0).path.value());
1238 EXPECT_TRUE(plugins->at(0).is_public);
[email protected]c533bb22009-06-03 19:06:111239 EXPECT_EQ(loaded_[1]->path().AppendASCII("extension_plugin.dll").value(),
[email protected]098fa7a2013-03-08 22:11:171240 plugins->at(1).path.value());
1241 EXPECT_FALSE(plugins->at(1).is_public);
[email protected]e50013c32010-08-18 21:05:241242#endif
[email protected]18a12352009-01-31 01:33:281243
[email protected]1d5e58b2013-01-31 08:41:401244 EXPECT_EQ(Manifest::INTERNAL, loaded_[1]->location());
[email protected]b8fb3032011-04-29 18:45:561245
[email protected]e50013c32010-08-18 21:05:241246 int index = expected_num_extensions - 1;
1247 EXPECT_EQ(std::string(good2), loaded_[index]->id());
1248 EXPECT_EQ(std::string("My extension 3"), loaded_[index]->name());
[email protected]007b3f82013-04-09 08:46:451249 EXPECT_EQ(std::string(), loaded_[index]->description());
[email protected]334ec0a2013-03-24 01:36:161250 EXPECT_EQ(
1251 0u,
1252 extensions::ContentScriptsInfo::GetContentScripts(loaded_[index]).size());
[email protected]1d5e58b2013-01-31 08:41:401253 EXPECT_EQ(Manifest::INTERNAL, loaded_[index]->location());
[email protected]6014d672008-12-05 00:38:251254};
[email protected]cc655912009-01-29 23:19:191255
[email protected]54cb3c92009-02-17 22:30:211256// Test loading bad extensions from the profile directory.
[email protected]d9a61e12012-11-14 02:43:471257TEST_F(ExtensionServiceTest, LoadAllExtensionsFromDirectoryFail) {
[email protected]c6d474f82009-12-16 21:11:061258 // Initialize the test dir with a bad Preferences/extensions.
[email protected]650b2d52013-02-10 03:41:451259 base::FilePath source_install_dir = data_dir_
[email protected]a9b00ac2009-06-25 21:03:231260 .AppendASCII("bad")
1261 .AppendASCII("Extensions");
[email protected]650b2d52013-02-10 03:41:451262 base::FilePath pref_path = source_install_dir
[email protected]a9b00ac2009-06-25 21:03:231263 .DirName()
1264 .AppendASCII("Preferences");
[email protected]54cb3c92009-02-17 22:30:211265
[email protected]eaa7dd182010-12-14 11:09:001266 InitializeInstalledExtensionService(pref_path, source_install_dir);
[email protected]54cb3c92009-02-17 22:30:211267
[email protected]9f1087e2009-06-15 17:29:321268 service_->Init();
[email protected]54cb3c92009-02-17 22:30:211269
[email protected]a9b00ac2009-06-25 21:03:231270 ASSERT_EQ(4u, GetErrors().size());
1271 ASSERT_EQ(0u, loaded_.size());
[email protected]25b34332009-06-05 21:53:191272
[email protected]fc670822011-12-17 09:33:491273 EXPECT_TRUE(MatchPattern(UTF16ToUTF8(GetErrors()[0]),
[email protected]d7b36dc2009-10-29 21:47:401274 std::string("Could not load extension from '*'. ") +
[email protected]fc670822011-12-17 09:33:491275 extension_manifest_errors::kManifestUnreadable)) <<
1276 UTF16ToUTF8(GetErrors()[0]);
[email protected]8d6d9ff2009-02-20 08:14:391277
[email protected]fc670822011-12-17 09:33:491278 EXPECT_TRUE(MatchPattern(UTF16ToUTF8(GetErrors()[1]),
[email protected]8d6d9ff2009-02-20 08:14:391279 std::string("Could not load extension from '*'. ") +
[email protected]fc670822011-12-17 09:33:491280 extension_manifest_errors::kManifestUnreadable)) <<
1281 UTF16ToUTF8(GetErrors()[1]);
[email protected]8d6d9ff2009-02-20 08:14:391282
[email protected]fc670822011-12-17 09:33:491283 EXPECT_TRUE(MatchPattern(UTF16ToUTF8(GetErrors()[2]),
[email protected]8d6d9ff2009-02-20 08:14:391284 std::string("Could not load extension from '*'. ") +
[email protected]fc670822011-12-17 09:33:491285 extension_manifest_errors::kMissingFile)) <<
1286 UTF16ToUTF8(GetErrors()[2]);
[email protected]a9b00ac2009-06-25 21:03:231287
[email protected]fc670822011-12-17 09:33:491288 EXPECT_TRUE(MatchPattern(UTF16ToUTF8(GetErrors()[3]),
[email protected]a9b00ac2009-06-25 21:03:231289 std::string("Could not load extension from '*'. ") +
[email protected]fc670822011-12-17 09:33:491290 extension_manifest_errors::kManifestUnreadable)) <<
1291 UTF16ToUTF8(GetErrors()[3]);
[email protected]54cb3c92009-02-17 22:30:211292};
1293
[email protected]2f8757c32012-06-19 19:17:471294// Test that partially deleted extensions are cleaned up during startup
[email protected]894bb502009-05-21 22:39:571295// Test loading bad extensions from the profile directory.
[email protected]d9a61e12012-11-14 02:43:471296TEST_F(ExtensionServiceTest, CleanupOnStartup) {
[email protected]ebd71962012-12-20 02:56:551297 InitPluginService();
[email protected]183d4b82011-11-11 18:50:261298
[email protected]650b2d52013-02-10 03:41:451299 base::FilePath source_install_dir = data_dir_
[email protected]b6ab96d2009-08-20 18:58:191300 .AppendASCII("good")
1301 .AppendASCII("Extensions");
[email protected]650b2d52013-02-10 03:41:451302 base::FilePath pref_path = source_install_dir
[email protected]b6ab96d2009-08-20 18:58:191303 .DirName()
1304 .AppendASCII("Preferences");
[email protected]a9b00ac2009-06-25 21:03:231305
[email protected]eaa7dd182010-12-14 11:09:001306 InitializeInstalledExtensionService(pref_path, source_install_dir);
[email protected]894bb502009-05-21 22:39:571307
[email protected]b6ab96d2009-08-20 18:58:191308 // Simulate that one of them got partially deleted by clearing its pref.
[email protected]43d3bf82011-04-11 07:46:581309 {
1310 DictionaryPrefUpdate update(profile_->GetPrefs(), "extensions.settings");
1311 DictionaryValue* dict = update.Get();
[email protected]2f8757c32012-06-19 19:17:471312 ASSERT_TRUE(dict != NULL);
[email protected]43d3bf82011-04-11 07:46:581313 dict->Remove("behllobkkfkfnphdnhnkndlbkcpglgmj", NULL);
1314 }
[email protected]894bb502009-05-21 22:39:571315
1316 service_->Init();
[email protected]1f4728f2012-12-05 20:40:051317 // Wait for GarbageCollectExtensions task to complete.
[email protected]0db124b02012-11-07 04:55:051318 loop_.RunUntilIdle();
[email protected]894bb502009-05-21 22:39:571319
[email protected]9e66a9b2013-05-08 05:46:201320 file_util::FileEnumerator dirs(extensions_install_dir_, false,
1321 file_util::FileEnumerator::DIRECTORIES);
[email protected]9f1087e2009-06-15 17:29:321322 size_t count = 0;
1323 while (!dirs.Next().empty())
1324 count++;
1325
[email protected]894bb502009-05-21 22:39:571326 // We should have only gotten two extensions now.
[email protected]9f1087e2009-06-15 17:29:321327 EXPECT_EQ(2u, count);
[email protected]e2eb43112009-05-29 21:19:541328
[email protected]894bb502009-05-21 22:39:571329 // And extension1 dir should now be toast.
[email protected]650b2d52013-02-10 03:41:451330 base::FilePath extension_dir = extensions_install_dir_
[email protected]b6ab96d2009-08-20 18:58:191331 .AppendASCII("behllobkkfkfnphdnhnkndlbkcpglgmj");
1332 ASSERT_FALSE(file_util::PathExists(extension_dir));
[email protected]894bb502009-05-21 22:39:571333}
1334
[email protected]0db124b02012-11-07 04:55:051335// Test that GarbageCollectExtensions deletes the right versions of an
1336// extension.
1337TEST_F(ExtensionServiceTest, GarbageCollectWithPendingUpdates) {
[email protected]ebd71962012-12-20 02:56:551338 InitPluginService();
[email protected]0db124b02012-11-07 04:55:051339
[email protected]650b2d52013-02-10 03:41:451340 base::FilePath source_install_dir = data_dir_
[email protected]0db124b02012-11-07 04:55:051341 .AppendASCII("pending_updates")
1342 .AppendASCII("Extensions");
[email protected]650b2d52013-02-10 03:41:451343 base::FilePath pref_path = source_install_dir
[email protected]0db124b02012-11-07 04:55:051344 .DirName()
1345 .AppendASCII("Preferences");
1346
1347 InitializeInstalledExtensionService(pref_path, source_install_dir);
1348
1349 // This is the directory that is going to be deleted, so make sure it actually
1350 // is there before the garbage collection.
1351 ASSERT_TRUE(file_util::PathExists(extensions_install_dir_.AppendASCII(
1352 "hpiknbiabeeppbpihjehijgoemciehgk/3")));
1353
1354 service_->GarbageCollectExtensions();
[email protected]1f4728f2012-12-05 20:40:051355 // Wait for GarbageCollectExtensions task to complete.
[email protected]0db124b02012-11-07 04:55:051356 loop_.RunUntilIdle();
1357
1358 // Verify that the pending update for the first extension didn't get
1359 // deleted.
1360 EXPECT_TRUE(file_util::PathExists(extensions_install_dir_.AppendASCII(
1361 "bjafgdebaacbbbecmhlhpofkepfkgcpa/1.0")));
1362 EXPECT_TRUE(file_util::PathExists(extensions_install_dir_.AppendASCII(
1363 "bjafgdebaacbbbecmhlhpofkepfkgcpa/2.0")));
1364 EXPECT_TRUE(file_util::PathExists(extensions_install_dir_.AppendASCII(
1365 "hpiknbiabeeppbpihjehijgoemciehgk/2")));
1366 EXPECT_FALSE(file_util::PathExists(extensions_install_dir_.AppendASCII(
1367 "hpiknbiabeeppbpihjehijgoemciehgk/3")));
1368}
1369
1370// Test that pending updates are properly handled on startup.
1371TEST_F(ExtensionServiceTest, UpdateOnStartup) {
[email protected]ebd71962012-12-20 02:56:551372 InitPluginService();
[email protected]0db124b02012-11-07 04:55:051373
[email protected]650b2d52013-02-10 03:41:451374 base::FilePath source_install_dir = data_dir_
[email protected]0db124b02012-11-07 04:55:051375 .AppendASCII("pending_updates")
1376 .AppendASCII("Extensions");
[email protected]650b2d52013-02-10 03:41:451377 base::FilePath pref_path = source_install_dir
[email protected]0db124b02012-11-07 04:55:051378 .DirName()
1379 .AppendASCII("Preferences");
1380
1381 InitializeInstalledExtensionService(pref_path, source_install_dir);
1382
1383 // This is the directory that is going to be deleted, so make sure it actually
1384 // is there before the garbage collection.
1385 ASSERT_TRUE(file_util::PathExists(extensions_install_dir_.AppendASCII(
1386 "hpiknbiabeeppbpihjehijgoemciehgk/3")));
1387
1388 service_->Init();
[email protected]1f4728f2012-12-05 20:40:051389 // Wait for GarbageCollectExtensions task to complete.
[email protected]0db124b02012-11-07 04:55:051390 loop_.RunUntilIdle();
1391
1392 // Verify that the pending update for the first extension got installed.
1393 EXPECT_FALSE(file_util::PathExists(extensions_install_dir_.AppendASCII(
1394 "bjafgdebaacbbbecmhlhpofkepfkgcpa/1.0")));
1395 EXPECT_TRUE(file_util::PathExists(extensions_install_dir_.AppendASCII(
1396 "bjafgdebaacbbbecmhlhpofkepfkgcpa/2.0")));
1397 EXPECT_TRUE(file_util::PathExists(extensions_install_dir_.AppendASCII(
1398 "hpiknbiabeeppbpihjehijgoemciehgk/2")));
1399 EXPECT_FALSE(file_util::PathExists(extensions_install_dir_.AppendASCII(
1400 "hpiknbiabeeppbpihjehijgoemciehgk/3")));
1401
1402 // Make sure update information got deleted.
1403 ExtensionPrefs* prefs = service_->extension_prefs();
[email protected]399583b2012-12-11 09:33:421404 EXPECT_FALSE(
1405 prefs->GetDelayedInstallInfo("bjafgdebaacbbbecmhlhpofkepfkgcpa"));
[email protected]0db124b02012-11-07 04:55:051406}
1407
[email protected]d7eaf572009-07-01 21:57:001408// Test installing extensions. This test tries to install few extensions using
1409// crx files. If you need to change those crx files, feel free to repackage
1410// them, throw away the key used and change the id's above.
[email protected]d9a61e12012-11-14 02:43:471411TEST_F(ExtensionServiceTest, InstallExtension) {
[email protected]eaa7dd182010-12-14 11:09:001412 InitializeEmptyExtensionService();
[email protected]a9b00ac2009-06-25 21:03:231413
[email protected]e2eb43112009-05-29 21:19:541414 // Extensions not enabled.
[email protected]7577a5c52009-07-30 06:21:581415 set_extensions_enabled(false);
[email protected]650b2d52013-02-10 03:41:451416 base::FilePath path = data_dir_.AppendASCII("good.crx");
[email protected]8f512c72011-11-22 21:02:501417 InstallCRX(path, INSTALL_FAILED);
[email protected]7577a5c52009-07-30 06:21:581418 set_extensions_enabled(true);
[email protected]e2eb43112009-05-29 21:19:541419
[email protected]25b34332009-06-05 21:53:191420 ValidatePrefKeyCount(0);
1421
[email protected]e2eb43112009-05-29 21:19:541422 // A simple extension that should install without error.
[email protected]e85e34c32011-04-13 18:38:351423 path = data_dir_.AppendASCII("good.crx");
[email protected]8f512c72011-11-22 21:02:501424 InstallCRX(path, INSTALL_NEW);
[email protected]cc655912009-01-29 23:19:191425 // TODO(erikkay): verify the contents of the installed extension.
1426
[email protected]25b34332009-06-05 21:53:191427 int pref_count = 0;
1428 ValidatePrefKeyCount(++pref_count);
[email protected]e2194742010-08-12 05:54:341429 ValidateIntegerPref(good_crx, "state", Extension::ENABLED);
[email protected]1d5e58b2013-01-31 08:41:401430 ValidateIntegerPref(good_crx, "location", Manifest::INTERNAL);
[email protected]25b34332009-06-05 21:53:191431
[email protected]902f7cd2009-05-22 19:02:191432 // An extension with page actions.
[email protected]e85e34c32011-04-13 18:38:351433 path = data_dir_.AppendASCII("page_action.crx");
[email protected]8f512c72011-11-22 21:02:501434 InstallCRX(path, INSTALL_NEW);
[email protected]25b34332009-06-05 21:53:191435 ValidatePrefKeyCount(++pref_count);
[email protected]e2194742010-08-12 05:54:341436 ValidateIntegerPref(page_action, "state", Extension::ENABLED);
[email protected]1d5e58b2013-01-31 08:41:401437 ValidateIntegerPref(page_action, "location", Manifest::INTERNAL);
[email protected]902f7cd2009-05-22 19:02:191438
[email protected]9f1087e2009-06-15 17:29:321439 // Bad signature.
[email protected]e85e34c32011-04-13 18:38:351440 path = data_dir_.AppendASCII("bad_signature.crx");
[email protected]8f512c72011-11-22 21:02:501441 InstallCRX(path, INSTALL_FAILED);
[email protected]d7eaf572009-07-01 21:57:001442 ValidatePrefKeyCount(pref_count);
[email protected]fbcc40302009-06-12 20:45:451443
[email protected]cc655912009-01-29 23:19:191444 // 0-length extension file.
[email protected]e85e34c32011-04-13 18:38:351445 path = data_dir_.AppendASCII("not_an_extension.crx");
[email protected]8f512c72011-11-22 21:02:501446 InstallCRX(path, INSTALL_FAILED);
[email protected]25b34332009-06-05 21:53:191447 ValidatePrefKeyCount(pref_count);
[email protected]cc655912009-01-29 23:19:191448
1449 // Bad magic number.
[email protected]e85e34c32011-04-13 18:38:351450 path = data_dir_.AppendASCII("bad_magic.crx");
[email protected]8f512c72011-11-22 21:02:501451 InstallCRX(path, INSTALL_FAILED);
[email protected]25b34332009-06-05 21:53:191452 ValidatePrefKeyCount(pref_count);
[email protected]cc655912009-01-29 23:19:191453
[email protected]8ef78fd2010-08-19 17:14:321454 // Extensions cannot have folders or files that have underscores except in
[email protected]99872e32009-09-25 22:02:491455 // certain whitelisted cases (eg _locales). This is an example of a broader
1456 // class of validation that we do to the directory structure of the extension.
1457 // We did not used to handle this correctly for installation.
[email protected]e85e34c32011-04-13 18:38:351458 path = data_dir_.AppendASCII("bad_underscore.crx");
[email protected]8f512c72011-11-22 21:02:501459 InstallCRX(path, INSTALL_FAILED);
[email protected]99872e32009-09-25 22:02:491460 ValidatePrefKeyCount(pref_count);
1461
[email protected]cc655912009-01-29 23:19:191462 // TODO(erikkay): add more tests for many of the failure cases.
1463 // TODO(erikkay): add tests for upgrade cases.
1464}
1465
[email protected]1bf73cc2011-10-26 22:38:311466// Tests that flags passed to OnExternalExtensionFileFound() make it to the
1467// extension object.
[email protected]d9a61e12012-11-14 02:43:471468TEST_F(ExtensionServiceTest, InstallingExternalExtensionWithFlags) {
[email protected]a12ce8b22012-01-17 18:40:531469 const char kPrefFromBookmark[] = "from_bookmark";
1470
[email protected]1bf73cc2011-10-26 22:38:311471 InitializeEmptyExtensionService();
1472
[email protected]650b2d52013-02-10 03:41:451473 base::FilePath path = data_dir_.AppendASCII("good.crx");
[email protected]1bf73cc2011-10-26 22:38:311474 set_extensions_enabled(true);
1475
[email protected]a12ce8b22012-01-17 18:40:531476 // Register and install an external extension.
[email protected]12126d372012-07-11 18:40:531477 Version version("1.0.0.0");
[email protected]f121003b2012-05-04 21:57:471478 service_->OnExternalExtensionFileFound(
1479 good_crx,
[email protected]12126d372012-07-11 18:40:531480 &version,
[email protected]f121003b2012-05-04 21:57:471481 path,
[email protected]1d5e58b2013-01-31 08:41:401482 Manifest::EXTERNAL_PREF,
[email protected]f121003b2012-05-04 21:57:471483 Extension::FROM_BOOKMARK,
1484 false /* mark_acknowledged */);
[email protected]b8f50ce2012-11-17 12:37:571485 loop_.RunUntilIdle();
[email protected]1bf73cc2011-10-26 22:38:311486
1487 const Extension* extension = service_->GetExtensionById(good_crx, false);
1488 ASSERT_TRUE(extension);
[email protected]a12ce8b22012-01-17 18:40:531489 ASSERT_TRUE(extension->from_bookmark());
1490 ValidateBooleanPref(good_crx, kPrefFromBookmark, true);
1491
1492 // Upgrade to version 2.0, the flag should be preserved.
1493 path = data_dir_.AppendASCII("good2.crx");
1494 UpdateExtension(good_crx, path, ENABLED);
1495 ValidateBooleanPref(good_crx, kPrefFromBookmark, true);
1496 extension = service_->GetExtensionById(good_crx, false);
1497 ASSERT_TRUE(extension);
1498 ASSERT_TRUE(extension->from_bookmark());
[email protected]1bf73cc2011-10-26 22:38:311499}
1500
[email protected]79c833b52011-04-05 18:31:011501// Test the handling of Extension::EXTERNAL_EXTENSION_UNINSTALLED
[email protected]d9a61e12012-11-14 02:43:471502TEST_F(ExtensionServiceTest, UninstallingExternalExtensions) {
[email protected]eaa7dd182010-12-14 11:09:001503 InitializeEmptyExtensionService();
[email protected]683d0702010-12-06 16:25:571504
[email protected]650b2d52013-02-10 03:41:451505 base::FilePath path = data_dir_.AppendASCII("good.crx");
[email protected]683d0702010-12-06 16:25:571506 set_extensions_enabled(true);
1507
[email protected]12126d372012-07-11 18:40:531508 Version version("1.0.0.0");
[email protected]683d0702010-12-06 16:25:571509 // Install an external extension.
[email protected]12126d372012-07-11 18:40:531510 service_->OnExternalExtensionFileFound(good_crx, &version,
[email protected]1d5e58b2013-01-31 08:41:401511 path, Manifest::EXTERNAL_PREF,
[email protected]47fc70c2011-12-06 07:29:511512 Extension::NO_FLAGS, false);
[email protected]b8f50ce2012-11-17 12:37:571513 loop_.RunUntilIdle();
[email protected]604322d2011-03-22 16:51:561514 ASSERT_TRUE(service_->GetExtensionById(good_crx, false));
[email protected]683d0702010-12-06 16:25:571515
1516 // Uninstall it and check that its killbit gets set.
[email protected]fa2416f2011-05-03 08:41:201517 UninstallExtension(good_crx, false);
[email protected]79c833b52011-04-05 18:31:011518 ValidateIntegerPref(good_crx, "location",
1519 Extension::EXTERNAL_EXTENSION_UNINSTALLED);
[email protected]683d0702010-12-06 16:25:571520
1521 // Try to re-install it externally. This should fail because of the killbit.
[email protected]12126d372012-07-11 18:40:531522 service_->OnExternalExtensionFileFound(good_crx, &version,
[email protected]1d5e58b2013-01-31 08:41:401523 path, Manifest::EXTERNAL_PREF,
[email protected]47fc70c2011-12-06 07:29:511524 Extension::NO_FLAGS, false);
[email protected]b8f50ce2012-11-17 12:37:571525 loop_.RunUntilIdle();
[email protected]683d0702010-12-06 16:25:571526 ASSERT_TRUE(NULL == service_->GetExtensionById(good_crx, false));
[email protected]79c833b52011-04-05 18:31:011527 ValidateIntegerPref(good_crx, "location",
1528 Extension::EXTERNAL_EXTENSION_UNINSTALLED);
[email protected]683d0702010-12-06 16:25:571529
[email protected]12126d372012-07-11 18:40:531530 version = Version("1.0.0.1");
[email protected]683d0702010-12-06 16:25:571531 // Repeat the same thing with a newer version of the extension.
[email protected]e85e34c32011-04-13 18:38:351532 path = data_dir_.AppendASCII("good2.crx");
[email protected]12126d372012-07-11 18:40:531533 service_->OnExternalExtensionFileFound(good_crx, &version,
[email protected]1d5e58b2013-01-31 08:41:401534 path, Manifest::EXTERNAL_PREF,
[email protected]47fc70c2011-12-06 07:29:511535 Extension::NO_FLAGS, false);
[email protected]b8f50ce2012-11-17 12:37:571536 loop_.RunUntilIdle();
[email protected]683d0702010-12-06 16:25:571537 ASSERT_TRUE(NULL == service_->GetExtensionById(good_crx, false));
[email protected]79c833b52011-04-05 18:31:011538 ValidateIntegerPref(good_crx, "location",
1539 Extension::EXTERNAL_EXTENSION_UNINSTALLED);
[email protected]683d0702010-12-06 16:25:571540
1541 // Try adding the same extension from an external update URL.
[email protected]9060d8b02012-01-13 02:14:301542 ASSERT_FALSE(service_->pending_extension_manager()->AddFromExternalUpdateUrl(
[email protected]683d0702010-12-06 16:25:571543 good_crx,
1544 GURL("http:://fake.update/url"),
[email protected]1d5e58b2013-01-31 08:41:401545 Manifest::EXTERNAL_PREF_DOWNLOAD));
[email protected]b2907fd2011-03-25 16:43:371546
1547 ASSERT_FALSE(service_->pending_extension_manager()->IsIdPending(good_crx));
[email protected]683d0702010-12-06 16:25:571548}
1549
[email protected]0f48fca2011-05-19 18:46:351550// Test that uninstalling an external extension does not crash when
1551// the extension could not be loaded.
1552// This extension shown in preferences file requires an experimental permission.
1553// It could not be loaded without such permission.
[email protected]d9a61e12012-11-14 02:43:471554TEST_F(ExtensionServiceTest, UninstallingNotLoadedExtension) {
[email protected]650b2d52013-02-10 03:41:451555 base::FilePath source_install_dir = data_dir_
[email protected]0f48fca2011-05-19 18:46:351556 .AppendASCII("good")
1557 .AppendASCII("Extensions");
1558 // The preference contains an external extension
1559 // that requires 'experimental' permission.
[email protected]650b2d52013-02-10 03:41:451560 base::FilePath pref_path = source_install_dir
[email protected]0f48fca2011-05-19 18:46:351561 .DirName()
1562 .AppendASCII("PreferencesExperimental");
1563
1564 // Aforementioned extension will not be loaded if
1565 // there is no '--enable-experimental-extension-apis' command line flag.
1566 InitializeInstalledExtensionService(pref_path, source_install_dir);
1567
1568 service_->Init();
[email protected]0f48fca2011-05-19 18:46:351569
1570 // Check and try to uninstall it.
1571 // If we don't check whether the extension is loaded before we uninstall it
1572 // in CheckExternalUninstall, a crash will happen here because we will get or
1573 // dereference a NULL pointer (extension) inside UninstallExtension.
[email protected]1d5e58b2013-01-31 08:41:401574 MockExtensionProvider provider(NULL, Manifest::EXTERNAL_REGISTRY);
[email protected]50067e52011-10-20 23:17:071575 service_->OnExternalProviderReady(&provider);
[email protected]0f48fca2011-05-19 18:46:351576}
1577
[email protected]604322d2011-03-22 16:51:561578// Test that external extensions with incorrect IDs are not installed.
[email protected]d9a61e12012-11-14 02:43:471579TEST_F(ExtensionServiceTest, FailOnWrongId) {
[email protected]604322d2011-03-22 16:51:561580 InitializeEmptyExtensionService();
[email protected]650b2d52013-02-10 03:41:451581 base::FilePath path = data_dir_.AppendASCII("good.crx");
[email protected]604322d2011-03-22 16:51:561582 set_extensions_enabled(true);
1583
[email protected]12126d372012-07-11 18:40:531584 Version version("1.0.0.0");
[email protected]604322d2011-03-22 16:51:561585
1586 const std::string wrong_id = all_zero;
1587 const std::string correct_id = good_crx;
1588 ASSERT_NE(correct_id, wrong_id);
1589
1590 // Install an external extension with an ID from the external
1591 // source that is not equal to the ID in the extension manifest.
1592 service_->OnExternalExtensionFileFound(
[email protected]1d5e58b2013-01-31 08:41:401593 wrong_id, &version, path, Manifest::EXTERNAL_PREF,
[email protected]47fc70c2011-12-06 07:29:511594 Extension::NO_FLAGS, false);
[email protected]604322d2011-03-22 16:51:561595
[email protected]b8f50ce2012-11-17 12:37:571596 loop_.RunUntilIdle();
[email protected]604322d2011-03-22 16:51:561597 ASSERT_FALSE(service_->GetExtensionById(good_crx, false));
1598
1599 // Try again with the right ID. Expect success.
1600 service_->OnExternalExtensionFileFound(
[email protected]1d5e58b2013-01-31 08:41:401601 correct_id, &version, path, Manifest::EXTERNAL_PREF,
[email protected]47fc70c2011-12-06 07:29:511602 Extension::NO_FLAGS, false);
[email protected]b8f50ce2012-11-17 12:37:571603 loop_.RunUntilIdle();
[email protected]604322d2011-03-22 16:51:561604 ASSERT_TRUE(service_->GetExtensionById(good_crx, false));
1605}
1606
1607// Test that external extensions with incorrect versions are not installed.
[email protected]d9a61e12012-11-14 02:43:471608TEST_F(ExtensionServiceTest, FailOnWrongVersion) {
[email protected]604322d2011-03-22 16:51:561609 InitializeEmptyExtensionService();
[email protected]650b2d52013-02-10 03:41:451610 base::FilePath path = data_dir_.AppendASCII("good.crx");
[email protected]604322d2011-03-22 16:51:561611 set_extensions_enabled(true);
1612
1613 // Install an external extension with a version from the external
1614 // source that is not equal to the version in the extension manifest.
[email protected]12126d372012-07-11 18:40:531615 Version wrong_version("1.2.3.4");
[email protected]604322d2011-03-22 16:51:561616 service_->OnExternalExtensionFileFound(
[email protected]1d5e58b2013-01-31 08:41:401617 good_crx, &wrong_version, path, Manifest::EXTERNAL_PREF,
[email protected]47fc70c2011-12-06 07:29:511618 Extension::NO_FLAGS, false);
[email protected]604322d2011-03-22 16:51:561619
[email protected]b8f50ce2012-11-17 12:37:571620 loop_.RunUntilIdle();
[email protected]604322d2011-03-22 16:51:561621 ASSERT_FALSE(service_->GetExtensionById(good_crx, false));
1622
1623 // Try again with the right version. Expect success.
[email protected]e3987852012-05-04 10:06:301624 service_->pending_extension_manager()->Remove(good_crx);
[email protected]12126d372012-07-11 18:40:531625 Version correct_version("1.0.0.0");
[email protected]604322d2011-03-22 16:51:561626 service_->OnExternalExtensionFileFound(
[email protected]1d5e58b2013-01-31 08:41:401627 good_crx, &correct_version, path, Manifest::EXTERNAL_PREF,
[email protected]47fc70c2011-12-06 07:29:511628 Extension::NO_FLAGS, false);
[email protected]b8f50ce2012-11-17 12:37:571629 loop_.RunUntilIdle();
[email protected]604322d2011-03-22 16:51:561630 ASSERT_TRUE(service_->GetExtensionById(good_crx, false));
1631}
1632
[email protected]da0aa3b2009-12-06 21:41:031633// Install a user script (they get converted automatically to an extension)
[email protected]d9a61e12012-11-14 02:43:471634TEST_F(ExtensionServiceTest, InstallUserScript) {
[email protected]da0aa3b2009-12-06 21:41:031635 // The details of script conversion are tested elsewhere, this just tests
[email protected]eaa7dd182010-12-14 11:09:001636 // integration with ExtensionService.
1637 InitializeEmptyExtensionService();
[email protected]da0aa3b2009-12-06 21:41:031638
[email protected]650b2d52013-02-10 03:41:451639 base::FilePath path = data_dir_
[email protected]da0aa3b2009-12-06 21:41:031640 .AppendASCII("user_script_basic.user.js");
1641
1642 ASSERT_TRUE(file_util::PathExists(path));
[email protected]d8c8f25f2011-11-02 18:18:011643 scoped_refptr<CrxInstaller> installer(CrxInstaller::Create(service_, NULL));
[email protected]0d3e4a22011-06-23 19:02:521644 installer->set_allow_silent_install(true);
[email protected]6dfbbf82010-03-12 23:09:161645 installer->InstallUserScript(
[email protected]da0aa3b2009-12-06 21:41:031646 path,
[email protected]6dfbbf82010-03-12 23:09:161647 GURL("http://www.aaronboodman.com/scripts/user_script_basic.user.js"));
[email protected]da0aa3b2009-12-06 21:41:031648
[email protected]b8f50ce2012-11-17 12:37:571649 loop_.RunUntilIdle();
[email protected]fc670822011-12-17 09:33:491650 std::vector<string16> errors = GetErrors();
[email protected]da0aa3b2009-12-06 21:41:031651 EXPECT_TRUE(installed_) << "Nothing was installed.";
[email protected]41bb80bd2013-05-03 10:56:021652 EXPECT_FALSE(was_update_) << path.value();
[email protected]da0aa3b2009-12-06 21:41:031653 ASSERT_EQ(1u, loaded_.size()) << "Nothing was loaded.";
1654 EXPECT_EQ(0u, errors.size()) << "There were errors: "
1655 << JoinString(errors, ',');
1656 EXPECT_TRUE(service_->GetExtensionById(loaded_[0]->id(), false)) <<
1657 path.value();
1658
1659 installed_ = NULL;
[email protected]41bb80bd2013-05-03 10:56:021660 was_update_ = false;
[email protected]da0aa3b2009-12-06 21:41:031661 loaded_.clear();
1662 ExtensionErrorReporter::GetInstance()->ClearErrors();
1663}
1664
[email protected]3c4abc82012-10-22 22:25:541665// Extensions don't install during shutdown.
[email protected]d9a61e12012-11-14 02:43:471666TEST_F(ExtensionServiceTest, InstallExtensionDuringShutdown) {
[email protected]3c4abc82012-10-22 22:25:541667 InitializeEmptyExtensionService();
1668
1669 // Simulate shutdown.
1670 service_->set_browser_terminating_for_test(true);
1671
[email protected]650b2d52013-02-10 03:41:451672 base::FilePath path = data_dir_.AppendASCII("good.crx");
[email protected]3c4abc82012-10-22 22:25:541673 scoped_refptr<CrxInstaller> installer(CrxInstaller::Create(service_, NULL));
1674 installer->set_allow_silent_install(true);
1675 installer->InstallCrx(path);
[email protected]b8f50ce2012-11-17 12:37:571676 loop_.RunUntilIdle();
[email protected]3c4abc82012-10-22 22:25:541677
1678 EXPECT_FALSE(installed_) << "Extension installed during shutdown.";
1679 ASSERT_EQ(0u, loaded_.size()) << "Extension loaded during shutdown.";
1680}
1681
[email protected]8d888c12010-11-30 00:00:251682// This tests that the granted permissions preferences are correctly set when
1683// installing an extension.
[email protected]d9a61e12012-11-14 02:43:471684TEST_F(ExtensionServiceTest, GrantedPermissions) {
[email protected]eaa7dd182010-12-14 11:09:001685 InitializeEmptyExtensionService();
[email protected]650b2d52013-02-10 03:41:451686 base::FilePath path = data_dir_
[email protected]8d888c12010-11-30 00:00:251687 .AppendASCII("permissions");
1688
[email protected]650b2d52013-02-10 03:41:451689 base::FilePath pem_path = path.AppendASCII("unknown.pem");
[email protected]8d888c12010-11-30 00:00:251690 path = path.AppendASCII("unknown");
1691
1692 ASSERT_TRUE(file_util::PathExists(pem_path));
1693 ASSERT_TRUE(file_util::PathExists(path));
1694
1695 ExtensionPrefs* prefs = service_->extension_prefs();
1696
[email protected]c2e66e12012-06-27 06:27:061697 APIPermissionSet expected_api_perms;
[email protected]cced75a2011-05-20 08:31:121698 URLPatternSet expected_host_perms;
[email protected]8d888c12010-11-30 00:00:251699
1700 // Make sure there aren't any granted permissions before the
1701 // extension is installed.
[email protected]c2e66e12012-06-27 06:27:061702 scoped_refptr<PermissionSet> known_perms(
[email protected]0d3e4a22011-06-23 19:02:521703 prefs->GetGrantedPermissions(permissions_crx));
1704 EXPECT_FALSE(known_perms.get());
[email protected]8d888c12010-11-30 00:00:251705
[email protected]8f512c72011-11-22 21:02:501706 const Extension* extension = PackAndInstallCRX(path, pem_path, INSTALL_NEW);
[email protected]8d888c12010-11-30 00:00:251707
1708 EXPECT_EQ(0u, GetErrors().size());
1709 ASSERT_EQ(1u, service_->extensions()->size());
[email protected]8f512c72011-11-22 21:02:501710 EXPECT_EQ(permissions_crx, extension->id());
[email protected]8d888c12010-11-30 00:00:251711
[email protected]8d888c12010-11-30 00:00:251712 // Verify that the valid API permissions have been recognized.
[email protected]c2e66e12012-06-27 06:27:061713 expected_api_perms.insert(APIPermission::kTab);
[email protected]8d888c12010-11-30 00:00:251714
1715 AddPattern(&expected_host_perms, "http://*.google.com/*");
1716 AddPattern(&expected_host_perms, "https://*.google.com/*");
[email protected]d6a5c78c2010-12-07 05:18:151717 AddPattern(&expected_host_perms, "http://*.google.com.hk/*");
[email protected]8d888c12010-11-30 00:00:251718 AddPattern(&expected_host_perms, "http://www.example.com/*");
1719
[email protected]8f512c72011-11-22 21:02:501720 known_perms = prefs->GetGrantedPermissions(extension->id());
[email protected]0d3e4a22011-06-23 19:02:521721 EXPECT_TRUE(known_perms.get());
1722 EXPECT_FALSE(known_perms->IsEmpty());
1723 EXPECT_EQ(expected_api_perms, known_perms->apis());
1724 EXPECT_FALSE(known_perms->HasEffectiveFullAccess());
[email protected]06e8b8ff2011-07-13 15:03:471725 EXPECT_EQ(expected_host_perms, known_perms->effective_hosts());
[email protected]8d888c12010-11-30 00:00:251726}
1727
[email protected]be083862012-09-01 03:53:451728
1729#if !defined(OS_CHROMEOS)
1730// This tests that the granted permissions preferences are correctly set for
1731// default apps.
[email protected]d9a61e12012-11-14 02:43:471732TEST_F(ExtensionServiceTest, DefaultAppsGrantedPermissions) {
[email protected]be083862012-09-01 03:53:451733 InitializeEmptyExtensionService();
1734 InitializeRequestContext();
[email protected]650b2d52013-02-10 03:41:451735 base::FilePath path = data_dir_
[email protected]be083862012-09-01 03:53:451736 .AppendASCII("permissions");
1737
[email protected]650b2d52013-02-10 03:41:451738 base::FilePath pem_path = path.AppendASCII("unknown.pem");
[email protected]be083862012-09-01 03:53:451739 path = path.AppendASCII("unknown");
1740
1741 ASSERT_TRUE(file_util::PathExists(pem_path));
1742 ASSERT_TRUE(file_util::PathExists(path));
1743
1744 ExtensionPrefs* prefs = service_->extension_prefs();
1745
1746 APIPermissionSet expected_api_perms;
1747 URLPatternSet expected_host_perms;
1748
1749 // Make sure there aren't any granted permissions before the
1750 // extension is installed.
1751 scoped_refptr<PermissionSet> known_perms(
1752 prefs->GetGrantedPermissions(permissions_crx));
1753 EXPECT_FALSE(known_perms.get());
1754
1755 const Extension* extension = PackAndInstallCRX(
1756 path, pem_path, INSTALL_NEW, Extension::WAS_INSTALLED_BY_DEFAULT);
1757
1758 EXPECT_EQ(0u, GetErrors().size());
1759 ASSERT_EQ(1u, service_->extensions()->size());
1760 EXPECT_EQ(permissions_crx, extension->id());
1761
1762 // Verify that the valid API permissions have been recognized.
1763 expected_api_perms.insert(APIPermission::kTab);
1764
1765 known_perms = prefs->GetGrantedPermissions(extension->id());
1766 EXPECT_TRUE(known_perms.get());
1767 EXPECT_FALSE(known_perms->IsEmpty());
1768 EXPECT_EQ(expected_api_perms, known_perms->apis());
1769 EXPECT_FALSE(known_perms->HasEffectiveFullAccess());
1770}
1771#endif
1772
[email protected]8d888c12010-11-30 00:00:251773#if !defined(OS_CHROMEOS)
1774// Tests that the granted permissions full_access bit gets set correctly when
1775// an extension contains an NPAPI plugin. Don't run this test on Chrome OS
1776// since they don't support plugins.
[email protected]d9a61e12012-11-14 02:43:471777TEST_F(ExtensionServiceTest, GrantedFullAccessPermissions) {
[email protected]ebd71962012-12-20 02:56:551778 InitPluginService();
[email protected]183d4b82011-11-11 18:50:261779
[email protected]eaa7dd182010-12-14 11:09:001780 InitializeEmptyExtensionService();
[email protected]8d888c12010-11-30 00:00:251781
[email protected]650b2d52013-02-10 03:41:451782 base::FilePath path = data_dir_
[email protected]8d888c12010-11-30 00:00:251783 .AppendASCII("good")
1784 .AppendASCII("Extensions")
1785 .AppendASCII(good1)
1786 .AppendASCII("2");
1787
1788 ASSERT_TRUE(file_util::PathExists(path));
[email protected]8f512c72011-11-22 21:02:501789 const Extension* extension = PackAndInstallCRX(path, INSTALL_NEW);
[email protected]8d888c12010-11-30 00:00:251790 EXPECT_EQ(0u, GetErrors().size());
1791 EXPECT_EQ(1u, service_->extensions()->size());
[email protected]8d888c12010-11-30 00:00:251792 ExtensionPrefs* prefs = service_->extension_prefs();
1793
[email protected]c2e66e12012-06-27 06:27:061794 scoped_refptr<PermissionSet> permissions(
[email protected]8f512c72011-11-22 21:02:501795 prefs->GetGrantedPermissions(extension->id()));
[email protected]0d3e4a22011-06-23 19:02:521796 EXPECT_FALSE(permissions->IsEmpty());
1797 EXPECT_TRUE(permissions->HasEffectiveFullAccess());
1798 EXPECT_FALSE(permissions->apis().empty());
[email protected]c2e66e12012-06-27 06:27:061799 EXPECT_TRUE(permissions->HasAPIPermission(APIPermission::kPlugin));
[email protected]8d888c12010-11-30 00:00:251800
[email protected]0d3e4a22011-06-23 19:02:521801 // Full access implies full host access too...
1802 EXPECT_TRUE(permissions->HasEffectiveAccessToAllHosts());
[email protected]8d888c12010-11-30 00:00:251803}
1804#endif
1805
1806// Tests that the extension is disabled when permissions are missing from
1807// the extension's granted permissions preferences. (This simulates updating
1808// the browser to a version which recognizes more permissions).
[email protected]d9a61e12012-11-14 02:43:471809TEST_F(ExtensionServiceTest, GrantedAPIAndHostPermissions) {
[email protected]eaa7dd182010-12-14 11:09:001810 InitializeEmptyExtensionService();
[email protected]8d888c12010-11-30 00:00:251811
[email protected]650b2d52013-02-10 03:41:451812 base::FilePath path = data_dir_
[email protected]8d888c12010-11-30 00:00:251813 .AppendASCII("permissions")
1814 .AppendASCII("unknown");
1815
1816 ASSERT_TRUE(file_util::PathExists(path));
1817
[email protected]8f512c72011-11-22 21:02:501818 const Extension* extension = PackAndInstallCRX(path, INSTALL_NEW);
[email protected]8d888c12010-11-30 00:00:251819
1820 EXPECT_EQ(0u, GetErrors().size());
1821 EXPECT_EQ(1u, service_->extensions()->size());
[email protected]8d888c12010-11-30 00:00:251822 std::string extension_id = extension->id();
1823
1824 ExtensionPrefs* prefs = service_->extension_prefs();
1825
[email protected]c2e66e12012-06-27 06:27:061826 APIPermissionSet expected_api_permissions;
[email protected]cced75a2011-05-20 08:31:121827 URLPatternSet expected_host_permissions;
[email protected]8d888c12010-11-30 00:00:251828
[email protected]c2e66e12012-06-27 06:27:061829 expected_api_permissions.insert(APIPermission::kTab);
[email protected]8d888c12010-11-30 00:00:251830 AddPattern(&expected_host_permissions, "http://*.google.com/*");
1831 AddPattern(&expected_host_permissions, "https://*.google.com/*");
[email protected]d6a5c78c2010-12-07 05:18:151832 AddPattern(&expected_host_permissions, "http://*.google.com.hk/*");
[email protected]8d888c12010-11-30 00:00:251833 AddPattern(&expected_host_permissions, "http://www.example.com/*");
1834
[email protected]8d888c12010-11-30 00:00:251835 std::set<std::string> host_permissions;
1836
1837 // Test that the extension is disabled when an API permission is missing from
1838 // the extension's granted api permissions preference. (This simulates
1839 // updating the browser to a version which recognizes a new API permission).
[email protected]0d3e4a22011-06-23 19:02:521840 SetPref(extension_id, "granted_permissions.api",
1841 new ListValue(), "granted_permissions.api");
[email protected]8d888c12010-11-30 00:00:251842 service_->ReloadExtensions();
1843
1844 EXPECT_EQ(1u, service_->disabled_extensions()->size());
[email protected]8f512c72011-11-22 21:02:501845 extension = *service_->disabled_extensions()->begin();
[email protected]8d888c12010-11-30 00:00:251846
[email protected]ad83ca242011-07-29 01:32:251847 ASSERT_TRUE(prefs->IsExtensionDisabled(extension_id));
[email protected]36429da2011-07-11 20:25:181848 ASSERT_FALSE(service_->IsExtensionEnabled(extension_id));
[email protected]8d888c12010-11-30 00:00:251849 ASSERT_TRUE(prefs->DidExtensionEscalatePermissions(extension_id));
1850
1851 // Now grant and re-enable the extension, making sure the prefs are updated.
[email protected]009633c2013-03-07 22:08:281852 service_->GrantPermissionsAndEnableExtension(extension);
[email protected]8d888c12010-11-30 00:00:251853
[email protected]ad83ca242011-07-29 01:32:251854 ASSERT_FALSE(prefs->IsExtensionDisabled(extension_id));
[email protected]36429da2011-07-11 20:25:181855 ASSERT_TRUE(service_->IsExtensionEnabled(extension_id));
[email protected]8d888c12010-11-30 00:00:251856 ASSERT_FALSE(prefs->DidExtensionEscalatePermissions(extension_id));
1857
[email protected]c2e66e12012-06-27 06:27:061858 scoped_refptr<PermissionSet> current_perms(
[email protected]0d3e4a22011-06-23 19:02:521859 prefs->GetGrantedPermissions(extension_id));
1860 ASSERT_TRUE(current_perms.get());
1861 ASSERT_FALSE(current_perms->IsEmpty());
1862 ASSERT_FALSE(current_perms->HasEffectiveFullAccess());
1863 ASSERT_EQ(expected_api_permissions, current_perms->apis());
[email protected]06e8b8ff2011-07-13 15:03:471864 ASSERT_EQ(expected_host_permissions, current_perms->effective_hosts());
[email protected]8d888c12010-11-30 00:00:251865
1866 // Tests that the extension is disabled when a host permission is missing from
1867 // the extension's granted host permissions preference. (This simulates
1868 // updating the browser to a version which recognizes additional host
1869 // permissions).
[email protected]8d888c12010-11-30 00:00:251870 host_permissions.clear();
[email protected]902fd7b2011-07-27 18:42:311871 current_perms = NULL;
[email protected]8d888c12010-11-30 00:00:251872
[email protected]8d888c12010-11-30 00:00:251873 host_permissions.insert("http://*.google.com/*");
1874 host_permissions.insert("https://*.google.com/*");
[email protected]d6a5c78c2010-12-07 05:18:151875 host_permissions.insert("http://*.google.com.hk/*");
[email protected]8d888c12010-11-30 00:00:251876
[email protected]0d3e4a22011-06-23 19:02:521877 ListValue* api_permissions = new ListValue();
1878 api_permissions->Append(
[email protected]1d8b79a2012-08-16 20:22:541879 Value::CreateStringValue("tabs"));
[email protected]0d3e4a22011-06-23 19:02:521880 SetPref(extension_id, "granted_permissions.api",
1881 api_permissions, "granted_permissions.api");
1882 SetPrefStringSet(
1883 extension_id, "granted_permissions.scriptable_host", host_permissions);
[email protected]8d888c12010-11-30 00:00:251884
1885 service_->ReloadExtensions();
1886
1887 EXPECT_EQ(1u, service_->disabled_extensions()->size());
[email protected]8f512c72011-11-22 21:02:501888 extension = *service_->disabled_extensions()->begin();
[email protected]8d888c12010-11-30 00:00:251889
[email protected]ad83ca242011-07-29 01:32:251890 ASSERT_TRUE(prefs->IsExtensionDisabled(extension_id));
[email protected]36429da2011-07-11 20:25:181891 ASSERT_FALSE(service_->IsExtensionEnabled(extension_id));
[email protected]8d888c12010-11-30 00:00:251892 ASSERT_TRUE(prefs->DidExtensionEscalatePermissions(extension_id));
1893
1894 // Now grant and re-enable the extension, making sure the prefs are updated.
[email protected]009633c2013-03-07 22:08:281895 service_->GrantPermissionsAndEnableExtension(extension);
[email protected]8d888c12010-11-30 00:00:251896
[email protected]36429da2011-07-11 20:25:181897 ASSERT_TRUE(service_->IsExtensionEnabled(extension_id));
[email protected]8d888c12010-11-30 00:00:251898 ASSERT_FALSE(prefs->DidExtensionEscalatePermissions(extension_id));
1899
[email protected]902fd7b2011-07-27 18:42:311900 current_perms = prefs->GetGrantedPermissions(extension_id);
[email protected]0d3e4a22011-06-23 19:02:521901 ASSERT_TRUE(current_perms.get());
1902 ASSERT_FALSE(current_perms->IsEmpty());
1903 ASSERT_FALSE(current_perms->HasEffectiveFullAccess());
1904 ASSERT_EQ(expected_api_permissions, current_perms->apis());
[email protected]06e8b8ff2011-07-13 15:03:471905 ASSERT_EQ(expected_host_permissions, current_perms->effective_hosts());
[email protected]8d888c12010-11-30 00:00:251906}
1907
[email protected]a17f9462009-06-09 02:56:411908// Test Packaging and installing an extension.
[email protected]d9a61e12012-11-14 02:43:471909TEST_F(ExtensionServiceTest, PackExtension) {
[email protected]eaa7dd182010-12-14 11:09:001910 InitializeEmptyExtensionService();
[email protected]650b2d52013-02-10 03:41:451911 base::FilePath input_directory = data_dir_
[email protected]a9b00ac2009-06-25 21:03:231912 .AppendASCII("good")
1913 .AppendASCII("Extensions")
1914 .AppendASCII("behllobkkfkfnphdnhnkndlbkcpglgmj")
1915 .AppendASCII("1.0.0.0");
[email protected]a17f9462009-06-09 02:56:411916
[email protected]ea1a3f62012-11-16 20:34:231917 base::ScopedTempDir temp_dir;
[email protected]aca3e9b2009-11-03 01:14:211918 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
[email protected]650b2d52013-02-10 03:41:451919 base::FilePath output_directory = temp_dir.path();
[email protected]aca3e9b2009-11-03 01:14:211920
[email protected]650b2d52013-02-10 03:41:451921 base::FilePath crx_path(output_directory.AppendASCII("ex1.crx"));
1922 base::FilePath privkey_path(output_directory.AppendASCII("privkey.pem"));
[email protected]a17f9462009-06-09 02:56:411923
1924 scoped_ptr<ExtensionCreator> creator(new ExtensionCreator());
[email protected]650b2d52013-02-10 03:41:451925 ASSERT_TRUE(creator->Run(input_directory, crx_path, base::FilePath(),
[email protected]93d973a2012-01-08 07:38:261926 privkey_path, ExtensionCreator::kNoRunFlags));
1927 ASSERT_TRUE(file_util::PathExists(crx_path));
1928 ASSERT_TRUE(file_util::PathExists(privkey_path));
1929
1930 // Repeat the run with the pem file gone, and no special flags
1931 // Should refuse to overwrite the existing crx.
1932 file_util::Delete(privkey_path, false);
[email protected]650b2d52013-02-10 03:41:451933 ASSERT_FALSE(creator->Run(input_directory, crx_path, base::FilePath(),
[email protected]93d973a2012-01-08 07:38:261934 privkey_path, ExtensionCreator::kNoRunFlags));
1935
1936 // OK, now try it with a flag to overwrite existing crx. Should work.
[email protected]650b2d52013-02-10 03:41:451937 ASSERT_TRUE(creator->Run(input_directory, crx_path, base::FilePath(),
[email protected]93d973a2012-01-08 07:38:261938 privkey_path, ExtensionCreator::kOverwriteCRX));
1939
1940 // Repeat the run allowing existing crx, but the existing pem is still
1941 // an error. Should fail.
[email protected]650b2d52013-02-10 03:41:451942 ASSERT_FALSE(creator->Run(input_directory, crx_path, base::FilePath(),
[email protected]93d973a2012-01-08 07:38:261943 privkey_path, ExtensionCreator::kOverwriteCRX));
[email protected]a17f9462009-06-09 02:56:411944
[email protected]a17f9462009-06-09 02:56:411945 ASSERT_TRUE(file_util::PathExists(privkey_path));
[email protected]8f512c72011-11-22 21:02:501946 InstallCRX(crx_path, INSTALL_NEW);
[email protected]0dc2ca82009-11-17 07:06:161947
1948 // Try packing with invalid paths.
1949 creator.reset(new ExtensionCreator());
[email protected]650b2d52013-02-10 03:41:451950 ASSERT_FALSE(
1951 creator->Run(base::FilePath(), base::FilePath(), base::FilePath(),
1952 base::FilePath(), ExtensionCreator::kOverwriteCRX));
[email protected]0dc2ca82009-11-17 07:06:161953
1954 // Try packing an empty directory. Should fail because an empty directory is
1955 // not a valid extension.
[email protected]ea1a3f62012-11-16 20:34:231956 base::ScopedTempDir temp_dir2;
[email protected]0dc2ca82009-11-17 07:06:161957 ASSERT_TRUE(temp_dir2.CreateUniqueTempDir());
1958 creator.reset(new ExtensionCreator());
1959 ASSERT_FALSE(creator->Run(temp_dir2.path(), crx_path, privkey_path,
[email protected]650b2d52013-02-10 03:41:451960 base::FilePath(), ExtensionCreator::kOverwriteCRX));
[email protected]0dc2ca82009-11-17 07:06:161961
1962 // Try packing with an invalid manifest.
1963 std::string invalid_manifest_content = "I am not a manifest.";
1964 ASSERT_TRUE(file_util::WriteFile(
[email protected]993da5e2013-03-23 21:25:161965 temp_dir2.path().Append(extensions::kManifestFilename),
[email protected]0dc2ca82009-11-17 07:06:161966 invalid_manifest_content.c_str(), invalid_manifest_content.size()));
1967 creator.reset(new ExtensionCreator());
1968 ASSERT_FALSE(creator->Run(temp_dir2.path(), crx_path, privkey_path,
[email protected]650b2d52013-02-10 03:41:451969 base::FilePath(), ExtensionCreator::kOverwriteCRX));
[email protected]a17f9462009-06-09 02:56:411970}
1971
[email protected]0349ab5d2010-08-11 21:41:571972// Test Packaging and installing an extension whose name contains punctuation.
[email protected]d9a61e12012-11-14 02:43:471973TEST_F(ExtensionServiceTest, PackPunctuatedExtension) {
[email protected]eaa7dd182010-12-14 11:09:001974 InitializeEmptyExtensionService();
[email protected]650b2d52013-02-10 03:41:451975 base::FilePath input_directory = data_dir_
[email protected]0349ab5d2010-08-11 21:41:571976 .AppendASCII("good")
1977 .AppendASCII("Extensions")
1978 .AppendASCII(good0)
1979 .AppendASCII("1.0.0.0");
1980
[email protected]ea1a3f62012-11-16 20:34:231981 base::ScopedTempDir temp_dir;
[email protected]0349ab5d2010-08-11 21:41:571982 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
1983
1984 // Extension names containing punctuation, and the expected names for the
1985 // packed extensions.
[email protected]650b2d52013-02-10 03:41:451986 const base::FilePath punctuated_names[] = {
1987 base::FilePath(FILE_PATH_LITERAL("this.extensions.name.has.periods")),
1988 base::FilePath(FILE_PATH_LITERAL(".thisextensionsnamestartswithaperiod")),
1989 base::FilePath(FILE_PATH_LITERAL("thisextensionhasaslashinitsname/")).
[email protected]d9034ed22012-02-10 02:04:401990 NormalizePathSeparators(),
[email protected]0349ab5d2010-08-11 21:41:571991 };
[email protected]650b2d52013-02-10 03:41:451992 const base::FilePath expected_crx_names[] = {
1993 base::FilePath(FILE_PATH_LITERAL("this.extensions.name.has.periods.crx")),
1994 base::FilePath(
1995 FILE_PATH_LITERAL(".thisextensionsnamestartswithaperiod.crx")),
1996 base::FilePath(FILE_PATH_LITERAL("thisextensionhasaslashinitsname.crx")),
[email protected]0349ab5d2010-08-11 21:41:571997 };
[email protected]650b2d52013-02-10 03:41:451998 const base::FilePath expected_private_key_names[] = {
1999 base::FilePath(FILE_PATH_LITERAL("this.extensions.name.has.periods.pem")),
2000 base::FilePath(
2001 FILE_PATH_LITERAL(".thisextensionsnamestartswithaperiod.pem")),
2002 base::FilePath(FILE_PATH_LITERAL("thisextensionhasaslashinitsname.pem")),
[email protected]0349ab5d2010-08-11 21:41:572003 };
2004
2005 for (size_t i = 0; i < arraysize(punctuated_names); ++i) {
2006 SCOPED_TRACE(punctuated_names[i].value().c_str());
[email protected]650b2d52013-02-10 03:41:452007 base::FilePath output_dir = temp_dir.path().Append(punctuated_names[i]);
[email protected]0349ab5d2010-08-11 21:41:572008
2009 // Copy the extension into the output directory, as PackExtensionJob doesn't
2010 // let us choose where to output the packed extension.
2011 ASSERT_TRUE(file_util::CopyDirectory(input_directory, output_dir, true));
2012
[email protected]650b2d52013-02-10 03:41:452013 base::FilePath expected_crx_path =
2014 temp_dir.path().Append(expected_crx_names[i]);
2015 base::FilePath expected_private_key_path =
[email protected]0349ab5d2010-08-11 21:41:572016 temp_dir.path().Append(expected_private_key_names[i]);
2017 PackExtensionTestClient pack_client(expected_crx_path,
2018 expected_private_key_path);
[email protected]d9ede582012-08-14 19:21:382019 scoped_refptr<extensions::PackExtensionJob> packer(
[email protected]650b2d52013-02-10 03:41:452020 new extensions::PackExtensionJob(&pack_client, output_dir,
2021 base::FilePath(),
[email protected]d9ede582012-08-14 19:21:382022 ExtensionCreator::kOverwriteCRX));
[email protected]0349ab5d2010-08-11 21:41:572023 packer->Start();
2024
2025 // The packer will post a notification task to the current thread's message
2026 // loop when it is finished. We manually run the loop here so that we
2027 // block and catch the notification; otherwise, the process would exit.
2028 // This call to |Run()| is matched by a call to |Quit()| in the
2029 // |PackExtensionTestClient|'s notification handling code.
2030 MessageLoop::current()->Run();
2031
2032 if (HasFatalFailure())
2033 return;
2034
[email protected]8f512c72011-11-22 21:02:502035 InstallCRX(expected_crx_path, INSTALL_NEW);
[email protected]0349ab5d2010-08-11 21:41:572036 }
2037}
2038
[email protected]d9a61e12012-11-14 02:43:472039TEST_F(ExtensionServiceTest, PackExtensionContainingKeyFails) {
[email protected]ab55c2b2012-06-01 23:55:032040 InitializeEmptyExtensionService();
2041
[email protected]ea1a3f62012-11-16 20:34:232042 base::ScopedTempDir extension_temp_dir;
[email protected]ab55c2b2012-06-01 23:55:032043 ASSERT_TRUE(extension_temp_dir.CreateUniqueTempDir());
[email protected]650b2d52013-02-10 03:41:452044 base::FilePath input_directory = extension_temp_dir.path().AppendASCII("ext");
[email protected]ab55c2b2012-06-01 23:55:032045 ASSERT_TRUE(file_util::CopyDirectory(
2046 data_dir_
2047 .AppendASCII("good")
2048 .AppendASCII("Extensions")
2049 .AppendASCII("behllobkkfkfnphdnhnkndlbkcpglgmj")
2050 .AppendASCII("1.0.0.0"),
2051 input_directory,
2052 /*recursive=*/true));
2053
[email protected]ea1a3f62012-11-16 20:34:232054 base::ScopedTempDir output_temp_dir;
[email protected]ab55c2b2012-06-01 23:55:032055 ASSERT_TRUE(output_temp_dir.CreateUniqueTempDir());
[email protected]650b2d52013-02-10 03:41:452056 base::FilePath output_directory = output_temp_dir.path();
[email protected]ab55c2b2012-06-01 23:55:032057
[email protected]650b2d52013-02-10 03:41:452058 base::FilePath crx_path(output_directory.AppendASCII("ex1.crx"));
2059 base::FilePath privkey_path(output_directory.AppendASCII("privkey.pem"));
[email protected]ab55c2b2012-06-01 23:55:032060
2061 // Pack the extension once to get a private key.
2062 scoped_ptr<ExtensionCreator> creator(new ExtensionCreator());
[email protected]650b2d52013-02-10 03:41:452063 ASSERT_TRUE(creator->Run(input_directory, crx_path, base::FilePath(),
[email protected]ab55c2b2012-06-01 23:55:032064 privkey_path, ExtensionCreator::kNoRunFlags))
2065 << creator->error_message();
2066 ASSERT_TRUE(file_util::PathExists(crx_path));
2067 ASSERT_TRUE(file_util::PathExists(privkey_path));
2068
2069 file_util::Delete(crx_path, false);
2070 // Move the pem file into the extension.
2071 file_util::Move(privkey_path,
2072 input_directory.AppendASCII("privkey.pem"));
2073
2074 // This pack should fail because of the contained private key.
[email protected]650b2d52013-02-10 03:41:452075 EXPECT_FALSE(creator->Run(input_directory, crx_path, base::FilePath(),
[email protected]ab55c2b2012-06-01 23:55:032076 privkey_path, ExtensionCreator::kNoRunFlags));
2077 EXPECT_THAT(creator->error_message(),
2078 testing::ContainsRegex(
2079 "extension includes the key file.*privkey.pem"));
2080}
2081
[email protected]a17f9462009-06-09 02:56:412082// Test Packaging and installing an extension using an openssl generated key.
2083// The openssl is generated with the following:
[email protected]a1257b12009-06-12 02:51:342084// > openssl genrsa -out privkey.pem 1024
[email protected]df4956e2009-06-10 16:53:422085// > openssl pkcs8 -topk8 -nocrypt -in privkey.pem -out privkey_asn1.pem
[email protected]a1257b12009-06-12 02:51:342086// The privkey.pem is a PrivateKey, and the pcks8 -topk8 creates a
[email protected]a17f9462009-06-09 02:56:412087// PrivateKeyInfo ASN.1 structure, we our RSAPrivateKey expects.
[email protected]d9a61e12012-11-14 02:43:472088TEST_F(ExtensionServiceTest, PackExtensionOpenSSLKey) {
[email protected]eaa7dd182010-12-14 11:09:002089 InitializeEmptyExtensionService();
[email protected]650b2d52013-02-10 03:41:452090 base::FilePath input_directory = data_dir_
[email protected]a9b00ac2009-06-25 21:03:232091 .AppendASCII("good")
2092 .AppendASCII("Extensions")
2093 .AppendASCII("behllobkkfkfnphdnhnkndlbkcpglgmj")
2094 .AppendASCII("1.0.0.0");
[email protected]650b2d52013-02-10 03:41:452095 base::FilePath privkey_path(data_dir_.AppendASCII(
[email protected]a17f9462009-06-09 02:56:412096 "openssl_privkey_asn1.pem"));
2097 ASSERT_TRUE(file_util::PathExists(privkey_path));
2098
[email protected]ea1a3f62012-11-16 20:34:232099 base::ScopedTempDir temp_dir;
[email protected]aca3e9b2009-11-03 01:14:212100 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
[email protected]650b2d52013-02-10 03:41:452101 base::FilePath output_directory = temp_dir.path();
[email protected]aca3e9b2009-11-03 01:14:212102
[email protected]650b2d52013-02-10 03:41:452103 base::FilePath crx_path(output_directory.AppendASCII("ex1.crx"));
[email protected]a17f9462009-06-09 02:56:412104
2105 scoped_ptr<ExtensionCreator> creator(new ExtensionCreator());
2106 ASSERT_TRUE(creator->Run(input_directory, crx_path, privkey_path,
[email protected]650b2d52013-02-10 03:41:452107 base::FilePath(), ExtensionCreator::kOverwriteCRX));
[email protected]a17f9462009-06-09 02:56:412108
[email protected]8f512c72011-11-22 21:02:502109 InstallCRX(crx_path, INSTALL_NEW);
[email protected]a17f9462009-06-09 02:56:412110}
[email protected]a17f9462009-06-09 02:56:412111
[email protected]d9a61e12012-11-14 02:43:472112TEST_F(ExtensionServiceTest, InstallTheme) {
[email protected]eaa7dd182010-12-14 11:09:002113 InitializeEmptyExtensionService();
[email protected]e2eb43112009-05-29 21:19:542114
2115 // A theme.
[email protected]650b2d52013-02-10 03:41:452116 base::FilePath path = data_dir_.AppendASCII("theme.crx");
[email protected]8f512c72011-11-22 21:02:502117 InstallCRX(path, INSTALL_NEW);
[email protected]25b34332009-06-05 21:53:192118 int pref_count = 0;
2119 ValidatePrefKeyCount(++pref_count);
[email protected]e2194742010-08-12 05:54:342120 ValidateIntegerPref(theme_crx, "state", Extension::ENABLED);
[email protected]1d5e58b2013-01-31 08:41:402121 ValidateIntegerPref(theme_crx, "location", Manifest::INTERNAL);
[email protected]e2eb43112009-05-29 21:19:542122
[email protected]6ef635e42009-07-26 06:16:122123 // A theme when extensions are disabled. Themes can be installed, even when
2124 // extensions are disabled.
[email protected]7577a5c52009-07-30 06:21:582125 set_extensions_enabled(false);
[email protected]e85e34c32011-04-13 18:38:352126 path = data_dir_.AppendASCII("theme2.crx");
[email protected]8f512c72011-11-22 21:02:502127 InstallCRX(path, INSTALL_NEW);
[email protected]25b34332009-06-05 21:53:192128 ValidatePrefKeyCount(++pref_count);
[email protected]e2194742010-08-12 05:54:342129 ValidateIntegerPref(theme2_crx, "state", Extension::ENABLED);
[email protected]1d5e58b2013-01-31 08:41:402130 ValidateIntegerPref(theme2_crx, "location", Manifest::INTERNAL);
[email protected]494c06e2009-07-25 01:06:422131
[email protected]f6dec1e322012-04-30 21:04:512132 // A theme with extension elements. Themes cannot have extension elements,
2133 // so any such elements (like content scripts) should be ignored.
[email protected]7577a5c52009-07-30 06:21:582134 set_extensions_enabled(true);
[email protected]f6dec1e322012-04-30 21:04:512135 {
2136 path = data_dir_.AppendASCII("theme_with_extension.crx");
2137 const Extension* extension = InstallCRX(path, INSTALL_NEW);
2138 ValidatePrefKeyCount(++pref_count);
2139 ASSERT_TRUE(extension);
2140 EXPECT_TRUE(extension->is_theme());
[email protected]334ec0a2013-03-24 01:36:162141 EXPECT_EQ(
2142 0u,
2143 extensions::ContentScriptsInfo::GetContentScripts(extension).size());
[email protected]f6dec1e322012-04-30 21:04:512144 }
[email protected]12198912009-06-05 03:41:222145
2146 // A theme with image resources missing (misspelt path).
[email protected]e85e34c32011-04-13 18:38:352147 path = data_dir_.AppendASCII("theme_missing_image.crx");
[email protected]8f512c72011-11-22 21:02:502148 InstallCRX(path, INSTALL_FAILED);
[email protected]25b34332009-06-05 21:53:192149 ValidatePrefKeyCount(pref_count);
[email protected]e2eb43112009-05-29 21:19:542150}
2151
[email protected]d9a61e12012-11-14 02:43:472152TEST_F(ExtensionServiceTest, LoadLocalizedTheme) {
[email protected]584245e52010-06-17 01:08:132153 // Load.
[email protected]eaa7dd182010-12-14 11:09:002154 InitializeEmptyExtensionService();
[email protected]650b2d52013-02-10 03:41:452155 base::FilePath extension_path = data_dir_
[email protected]584245e52010-06-17 01:08:132156 .AppendASCII("theme_i18n");
2157
[email protected]d8c8f25f2011-11-02 18:18:012158 extensions::UnpackedInstaller::Create(service_)->Load(extension_path);
[email protected]b8f50ce2012-11-17 12:37:572159 loop_.RunUntilIdle();
[email protected]584245e52010-06-17 01:08:132160 EXPECT_EQ(0u, GetErrors().size());
2161 ASSERT_EQ(1u, loaded_.size());
2162 EXPECT_EQ(1u, service_->extensions()->size());
[email protected]8f512c72011-11-22 21:02:502163 const Extension* theme = *service_->extensions()->begin();
2164 EXPECT_EQ("name", theme->name());
2165 EXPECT_EQ("description", theme->description());
[email protected]a17dfcf2012-12-30 02:07:092166
2167 // Cleanup the "Cached Theme.pak" file. Ideally, this would be installed in a
2168 // temporary directory, but it automatically installs to the extension's
2169 // directory, and we don't want to copy the whole extension for a unittest.
[email protected]650b2d52013-02-10 03:41:452170 base::FilePath theme_file = extension_path.Append(chrome::kThemePackFilename);
[email protected]a17dfcf2012-12-30 02:07:092171 ASSERT_TRUE(file_util::PathExists(theme_file));
2172 ASSERT_TRUE(file_util::Delete(theme_file, false)); // Not recursive.
[email protected]584245e52010-06-17 01:08:132173}
2174
[email protected]c24fb292012-02-01 22:52:112175// Tests that we can change the ID of an unpacked extension by adding a key
2176// to its manifest.
[email protected]d9a61e12012-11-14 02:43:472177TEST_F(ExtensionServiceTest, UnpackedExtensionCanChangeID) {
[email protected]c24fb292012-02-01 22:52:112178 InitializeEmptyExtensionService();
2179
[email protected]ea1a3f62012-11-16 20:34:232180 base::ScopedTempDir temp;
[email protected]c24fb292012-02-01 22:52:112181 ASSERT_TRUE(temp.CreateUniqueTempDir());
2182
[email protected]650b2d52013-02-10 03:41:452183 base::FilePath extension_path = temp.path();
2184 base::FilePath manifest_path =
[email protected]993da5e2013-03-23 21:25:162185 extension_path.Append(extensions::kManifestFilename);
[email protected]650b2d52013-02-10 03:41:452186 base::FilePath manifest_no_key = data_dir_.
[email protected]c24fb292012-02-01 22:52:112187 AppendASCII("unpacked").
2188 AppendASCII("manifest_no_key.json");
2189
[email protected]650b2d52013-02-10 03:41:452190 base::FilePath manifest_with_key = data_dir_.
[email protected]c24fb292012-02-01 22:52:112191 AppendASCII("unpacked").
2192 AppendASCII("manifest_with_key.json");
2193
2194 ASSERT_TRUE(file_util::PathExists(manifest_no_key));
2195 ASSERT_TRUE(file_util::PathExists(manifest_with_key));
2196
2197 // Load the unpacked extension with no key.
2198 file_util::CopyFile(manifest_no_key, manifest_path);
2199 extensions::UnpackedInstaller::Create(service_)->Load(extension_path);
2200
[email protected]b8f50ce2012-11-17 12:37:572201 loop_.RunUntilIdle();
[email protected]c24fb292012-02-01 22:52:112202 EXPECT_EQ(0u, GetErrors().size());
2203 ASSERT_EQ(1u, loaded_.size());
2204 EXPECT_EQ(1u, service_->extensions()->size());
2205
2206 // Add the key to the manifest.
2207 file_util::CopyFile(manifest_with_key, manifest_path);
2208 loaded_.clear();
2209
2210 // Reload the extensions.
2211 service_->ReloadExtensions();
2212 const Extension* extension = service_->GetExtensionById(unpacked, false);
2213 EXPECT_EQ(unpacked, extension->id());
2214 ASSERT_EQ(1u, loaded_.size());
2215
2216 // TODO(jstritar): Right now this just makes sure we don't crash and burn, but
2217 // we should also test that preferences are preserved.
2218}
2219
[email protected]91236521162012-05-24 15:02:512220#if defined(OS_POSIX)
[email protected]d9a61e12012-11-14 02:43:472221TEST_F(ExtensionServiceTest, UnpackedExtensionMayContainSymlinkedFiles) {
[email protected]650b2d52013-02-10 03:41:452222 base::FilePath source_data_dir = data_dir_.
[email protected]91236521162012-05-24 15:02:512223 AppendASCII("unpacked").
2224 AppendASCII("symlinks_allowed");
2225
2226 // Paths to test data files.
[email protected]650b2d52013-02-10 03:41:452227 base::FilePath source_manifest = source_data_dir.AppendASCII("manifest.json");
[email protected]91236521162012-05-24 15:02:512228 ASSERT_TRUE(file_util::PathExists(source_manifest));
[email protected]650b2d52013-02-10 03:41:452229 base::FilePath source_icon = source_data_dir.AppendASCII("icon.png");
[email protected]91236521162012-05-24 15:02:512230 ASSERT_TRUE(file_util::PathExists(source_icon));
2231
2232 // Set up the temporary extension directory.
[email protected]ea1a3f62012-11-16 20:34:232233 base::ScopedTempDir temp;
[email protected]91236521162012-05-24 15:02:512234 ASSERT_TRUE(temp.CreateUniqueTempDir());
[email protected]650b2d52013-02-10 03:41:452235 base::FilePath extension_path = temp.path();
[email protected]993da5e2013-03-23 21:25:162236 base::FilePath manifest = extension_path.Append(
2237 extensions::kManifestFilename);
[email protected]650b2d52013-02-10 03:41:452238 base::FilePath icon_symlink = extension_path.AppendASCII("icon.png");
[email protected]91236521162012-05-24 15:02:512239 file_util::CopyFile(source_manifest, manifest);
2240 file_util::CreateSymbolicLink(source_icon, icon_symlink);
2241
2242 // Load extension.
2243 InitializeEmptyExtensionService();
2244 extensions::UnpackedInstaller::Create(service_)->Load(extension_path);
[email protected]b8f50ce2012-11-17 12:37:572245 loop_.RunUntilIdle();
[email protected]91236521162012-05-24 15:02:512246
2247 EXPECT_TRUE(GetErrors().empty());
2248 ASSERT_EQ(1u, loaded_.size());
2249 EXPECT_EQ(1u, service_->extensions()->size());
2250}
2251#endif
2252
[email protected]d9a61e12012-11-14 02:43:472253TEST_F(ExtensionServiceTest, InstallLocalizedTheme) {
[email protected]eaa7dd182010-12-14 11:09:002254 InitializeEmptyExtensionService();
[email protected]650b2d52013-02-10 03:41:452255 base::FilePath theme_path = data_dir_
[email protected]584245e52010-06-17 01:08:132256 .AppendASCII("theme_i18n");
2257
[email protected]8f512c72011-11-22 21:02:502258 const Extension* theme = PackAndInstallCRX(theme_path, INSTALL_NEW);
[email protected]584245e52010-06-17 01:08:132259
[email protected]584245e52010-06-17 01:08:132260 EXPECT_EQ(0u, GetErrors().size());
2261 EXPECT_EQ(1u, service_->extensions()->size());
[email protected]8f512c72011-11-22 21:02:502262 EXPECT_EQ("name", theme->name());
2263 EXPECT_EQ("description", theme->description());
[email protected]584245e52010-06-17 01:08:132264}
2265
[email protected]d9a61e12012-11-14 02:43:472266TEST_F(ExtensionServiceTest, InstallApps) {
[email protected]eaa7dd182010-12-14 11:09:002267 InitializeEmptyExtensionService();
[email protected]6d2e60bd2010-06-03 22:37:392268
2269 // An empty app.
[email protected]8f512c72011-11-22 21:02:502270 const Extension* app = PackAndInstallCRX(data_dir_.AppendASCII("app1"),
2271 INSTALL_NEW);
[email protected]6d2e60bd2010-06-03 22:37:392272 int pref_count = 0;
2273 ValidatePrefKeyCount(++pref_count);
[email protected]3ba0fd32010-06-19 05:39:102274 ASSERT_EQ(1u, service_->extensions()->size());
[email protected]8f512c72011-11-22 21:02:502275 ValidateIntegerPref(app->id(), "state", Extension::ENABLED);
[email protected]1d5e58b2013-01-31 08:41:402276 ValidateIntegerPref(app->id(), "location", Manifest::INTERNAL);
[email protected]6d2e60bd2010-06-03 22:37:392277
2278 // Another app with non-overlapping extent. Should succeed.
[email protected]8f512c72011-11-22 21:02:502279 PackAndInstallCRX(data_dir_.AppendASCII("app2"), INSTALL_NEW);
[email protected]6d2e60bd2010-06-03 22:37:392280 ValidatePrefKeyCount(++pref_count);
2281
2282 // A third app whose extent overlaps the first. Should fail.
[email protected]8f512c72011-11-22 21:02:502283 PackAndInstallCRX(data_dir_.AppendASCII("app3"), INSTALL_FAILED);
[email protected]cd10e282010-10-26 21:04:512284 ValidatePrefKeyCount(pref_count);
[email protected]6d2e60bd2010-06-03 22:37:392285}
2286
[email protected]b6e64fd2011-08-09 19:49:192287// Tests that file access is OFF by default.
[email protected]d9a61e12012-11-14 02:43:472288TEST_F(ExtensionServiceTest, DefaultFileAccess) {
[email protected]b6e64fd2011-08-09 19:49:192289 InitializeEmptyExtensionService();
[email protected]8f512c72011-11-22 21:02:502290 const Extension* extension =
2291 PackAndInstallCRX(data_dir_
2292 .AppendASCII("permissions")
2293 .AppendASCII("files"),
2294 INSTALL_NEW);
[email protected]b6e64fd2011-08-09 19:49:192295 EXPECT_EQ(0u, GetErrors().size());
2296 EXPECT_EQ(1u, service_->extensions()->size());
[email protected]8f512c72011-11-22 21:02:502297 EXPECT_FALSE(service_->extension_prefs()->AllowFileAccess(extension->id()));
[email protected]b6e64fd2011-08-09 19:49:192298}
2299
[email protected]d9a61e12012-11-14 02:43:472300TEST_F(ExtensionServiceTest, UpdateApps) {
[email protected]15300d92011-01-19 18:44:302301 InitializeEmptyExtensionService();
[email protected]650b2d52013-02-10 03:41:452302 base::FilePath extensions_path = data_dir_.AppendASCII("app_update");
[email protected]15300d92011-01-19 18:44:302303
2304 // First install v1 of a hosted app.
[email protected]8f512c72011-11-22 21:02:502305 const Extension* extension =
2306 InstallCRX(extensions_path.AppendASCII("v1.crx"), INSTALL_NEW);
[email protected]15300d92011-01-19 18:44:302307 ASSERT_EQ(1u, service_->extensions()->size());
[email protected]8f512c72011-11-22 21:02:502308 std::string id = extension->id();
2309 ASSERT_EQ(std::string("1"), extension->version()->GetString());
[email protected]15300d92011-01-19 18:44:302310
2311 // Now try updating to v2.
2312 UpdateExtension(id,
2313 extensions_path.AppendASCII("v2.crx"),
2314 ENABLED);
2315 ASSERT_EQ(std::string("2"),
[email protected]8f512c72011-11-22 21:02:502316 service_->GetExtensionById(id, false)->version()->GetString());
[email protected]15300d92011-01-19 18:44:302317}
2318
[email protected]e6a6c2e02012-01-13 00:06:482319// Verifies that the NTP page and launch ordinals are kept when updating apps.
[email protected]d9a61e12012-11-14 02:43:472320TEST_F(ExtensionServiceTest, UpdateAppsRetainOrdinals) {
[email protected]e6a6c2e02012-01-13 00:06:482321 InitializeEmptyExtensionService();
2322 ExtensionSorting* sorting = service_->extension_prefs()->extension_sorting();
[email protected]650b2d52013-02-10 03:41:452323 base::FilePath extensions_path = data_dir_.AppendASCII("app_update");
[email protected]e6a6c2e02012-01-13 00:06:482324
2325 // First install v1 of a hosted app.
2326 const Extension* extension =
2327 InstallCRX(extensions_path.AppendASCII("v1.crx"), INSTALL_NEW);
2328 ASSERT_EQ(1u, service_->extensions()->size());
2329 std::string id = extension->id();
2330 ASSERT_EQ(std::string("1"), extension->version()->GetString());
2331
2332 // Modify the ordinals so we can distinguish them from the defaults.
[email protected]36b643212012-09-07 12:53:002333 syncer::StringOrdinal new_page_ordinal =
2334 sorting->GetPageOrdinal(id).CreateAfter();
2335 syncer::StringOrdinal new_launch_ordinal =
[email protected]e6a6c2e02012-01-13 00:06:482336 sorting->GetAppLaunchOrdinal(id).CreateBefore();
2337
2338 sorting->SetPageOrdinal(id, new_page_ordinal);
2339 sorting->SetAppLaunchOrdinal(id, new_launch_ordinal);
2340
2341 // Now try updating to v2.
2342 UpdateExtension(id, extensions_path.AppendASCII("v2.crx"), ENABLED);
2343 ASSERT_EQ(std::string("2"),
2344 service_->GetExtensionById(id, false)->version()->GetString());
2345
2346 // Verify that the ordinals match.
[email protected]36b643212012-09-07 12:53:002347 ASSERT_TRUE(new_page_ordinal.Equals(sorting->GetPageOrdinal(id)));
2348 ASSERT_TRUE(new_launch_ordinal.Equals(sorting->GetAppLaunchOrdinal(id)));
[email protected]e6a6c2e02012-01-13 00:06:482349}
2350
[email protected]b873cd92012-02-09 21:51:482351// Ensures that the CWS has properly initialized ordinals.
[email protected]d9a61e12012-11-14 02:43:472352TEST_F(ExtensionServiceTest, EnsureCWSOrdinalsInitialized) {
[email protected]b873cd92012-02-09 21:51:482353 InitializeEmptyExtensionService();
[email protected]650b2d52013-02-10 03:41:452354 service_->component_loader()->Add(
2355 IDR_WEBSTORE_MANIFEST, base::FilePath(FILE_PATH_LITERAL("web_store")));
[email protected]b873cd92012-02-09 21:51:482356 service_->Init();
2357
[email protected]b873cd92012-02-09 21:51:482358 ExtensionSorting* sorting = service_->extension_prefs()->extension_sorting();
2359 EXPECT_TRUE(
2360 sorting->GetPageOrdinal(extension_misc::kWebStoreAppId).IsValid());
2361 EXPECT_TRUE(
2362 sorting->GetAppLaunchOrdinal(extension_misc::kWebStoreAppId).IsValid());
2363}
2364
[email protected]fe10c202013-03-11 23:44:342365TEST_F(ExtensionServiceTest, InstallAppsWithUnlimitedStorage) {
[email protected]eaa7dd182010-12-14 11:09:002366 InitializeEmptyExtensionService();
[email protected]0d6ec3a72011-09-02 02:09:432367 InitializeRequestContext();
[email protected]84df8332011-12-06 18:22:462368 EXPECT_TRUE(service_->extensions()->is_empty());
[email protected]c2c263c2010-08-13 21:59:482369
[email protected]c2c263c2010-08-13 21:59:482370 int pref_count = 0;
[email protected]c2c263c2010-08-13 21:59:482371
2372 // Install app1 with unlimited storage.
[email protected]8f512c72011-11-22 21:02:502373 const Extension* extension =
2374 PackAndInstallCRX(data_dir_.AppendASCII("app1"), INSTALL_NEW);
[email protected]c2c263c2010-08-13 21:59:482375 ValidatePrefKeyCount(++pref_count);
2376 ASSERT_EQ(1u, service_->extensions()->size());
[email protected]c2c263c2010-08-13 21:59:482377 const std::string id1 = extension->id();
[email protected]0d3e4a22011-06-23 19:02:522378 EXPECT_TRUE(extension->HasAPIPermission(
[email protected]c2e66e12012-06-27 06:27:062379 APIPermission::kUnlimitedStorage));
[email protected]cced75a2011-05-20 08:31:122380 EXPECT_TRUE(extension->web_extent().MatchesURL(
[email protected]c2c263c2010-08-13 21:59:482381 extension->GetFullLaunchURL()));
2382 const GURL origin1(extension->GetFullLaunchURL().GetOrigin());
[email protected]19eb80152011-02-26 00:28:432383 EXPECT_TRUE(profile_->GetExtensionSpecialStoragePolicy()->
2384 IsStorageUnlimited(origin1));
[email protected]c2c263c2010-08-13 21:59:482385
2386 // Install app2 from the same origin with unlimited storage.
[email protected]8f512c72011-11-22 21:02:502387 extension = PackAndInstallCRX(data_dir_.AppendASCII("app2"), INSTALL_NEW);
[email protected]c2c263c2010-08-13 21:59:482388 ValidatePrefKeyCount(++pref_count);
2389 ASSERT_EQ(2u, service_->extensions()->size());
[email protected]c2c263c2010-08-13 21:59:482390 const std::string id2 = extension->id();
[email protected]0d3e4a22011-06-23 19:02:522391 EXPECT_TRUE(extension->HasAPIPermission(
[email protected]c2e66e12012-06-27 06:27:062392 APIPermission::kUnlimitedStorage));
[email protected]cced75a2011-05-20 08:31:122393 EXPECT_TRUE(extension->web_extent().MatchesURL(
[email protected]c2c263c2010-08-13 21:59:482394 extension->GetFullLaunchURL()));
2395 const GURL origin2(extension->GetFullLaunchURL().GetOrigin());
2396 EXPECT_EQ(origin1, origin2);
[email protected]19eb80152011-02-26 00:28:432397 EXPECT_TRUE(profile_->GetExtensionSpecialStoragePolicy()->
2398 IsStorageUnlimited(origin2));
2399
[email protected]c2c263c2010-08-13 21:59:482400
2401 // Uninstall one of them, unlimited storage should still be granted
2402 // to the origin.
[email protected]fa2416f2011-05-03 08:41:202403 UninstallExtension(id1, false);
[email protected]c2c263c2010-08-13 21:59:482404 EXPECT_EQ(1u, service_->extensions()->size());
[email protected]19eb80152011-02-26 00:28:432405 EXPECT_TRUE(profile_->GetExtensionSpecialStoragePolicy()->
2406 IsStorageUnlimited(origin1));
2407
[email protected]c2c263c2010-08-13 21:59:482408 // Uninstall the other, unlimited storage should be revoked.
[email protected]fa2416f2011-05-03 08:41:202409 UninstallExtension(id2, false);
[email protected]c2c263c2010-08-13 21:59:482410 EXPECT_EQ(0u, service_->extensions()->size());
[email protected]19eb80152011-02-26 00:28:432411 EXPECT_FALSE(profile_->GetExtensionSpecialStoragePolicy()->
2412 IsStorageUnlimited(origin2));
[email protected]c2c263c2010-08-13 21:59:482413}
2414
[email protected]d9a61e12012-11-14 02:43:472415TEST_F(ExtensionServiceTest, InstallAppsAndCheckStorageProtection) {
[email protected]eaa7dd182010-12-14 11:09:002416 InitializeEmptyExtensionService();
[email protected]0d6ec3a72011-09-02 02:09:432417 InitializeRequestContext();
[email protected]84df8332011-12-06 18:22:462418 EXPECT_TRUE(service_->extensions()->is_empty());
[email protected]654512b2010-09-01 02:09:422419
[email protected]654512b2010-09-01 02:09:422420 int pref_count = 0;
2421
[email protected]8f512c72011-11-22 21:02:502422 const Extension* extension =
2423 PackAndInstallCRX(data_dir_.AppendASCII("app1"), INSTALL_NEW);
[email protected]654512b2010-09-01 02:09:422424 ValidatePrefKeyCount(++pref_count);
2425 ASSERT_EQ(1u, service_->extensions()->size());
[email protected]654512b2010-09-01 02:09:422426 EXPECT_TRUE(extension->is_app());
2427 const std::string id1 = extension->id();
[email protected]654512b2010-09-01 02:09:422428 const GURL origin1(extension->GetFullLaunchURL().GetOrigin());
[email protected]19eb80152011-02-26 00:28:432429 EXPECT_TRUE(profile_->GetExtensionSpecialStoragePolicy()->
2430 IsStorageProtected(origin1));
[email protected]654512b2010-09-01 02:09:422431
2432 // App 4 has a different origin (maps.google.com).
[email protected]8f512c72011-11-22 21:02:502433 extension = PackAndInstallCRX(data_dir_.AppendASCII("app4"), INSTALL_NEW);
[email protected]654512b2010-09-01 02:09:422434 ValidatePrefKeyCount(++pref_count);
2435 ASSERT_EQ(2u, service_->extensions()->size());
[email protected]654512b2010-09-01 02:09:422436 const std::string id2 = extension->id();
[email protected]654512b2010-09-01 02:09:422437 const GURL origin2(extension->GetFullLaunchURL().GetOrigin());
2438 ASSERT_NE(origin1, origin2);
[email protected]19eb80152011-02-26 00:28:432439 EXPECT_TRUE(profile_->GetExtensionSpecialStoragePolicy()->
2440 IsStorageProtected(origin2));
[email protected]654512b2010-09-01 02:09:422441
[email protected]fa2416f2011-05-03 08:41:202442 UninstallExtension(id1, false);
[email protected]654512b2010-09-01 02:09:422443 EXPECT_EQ(1u, service_->extensions()->size());
[email protected]654512b2010-09-01 02:09:422444
[email protected]fa2416f2011-05-03 08:41:202445 UninstallExtension(id2, false);
[email protected]654512b2010-09-01 02:09:422446
[email protected]84df8332011-12-06 18:22:462447 EXPECT_TRUE(service_->extensions()->is_empty());
[email protected]19eb80152011-02-26 00:28:432448 EXPECT_FALSE(profile_->GetExtensionSpecialStoragePolicy()->
2449 IsStorageProtected(origin1));
2450 EXPECT_FALSE(profile_->GetExtensionSpecialStoragePolicy()->
2451 IsStorageProtected(origin2));
[email protected]654512b2010-09-01 02:09:422452}
2453
[email protected]894bb502009-05-21 22:39:572454// Test that when an extension version is reinstalled, nothing happens.
[email protected]d9a61e12012-11-14 02:43:472455TEST_F(ExtensionServiceTest, Reinstall) {
[email protected]eaa7dd182010-12-14 11:09:002456 InitializeEmptyExtensionService();
[email protected]894bb502009-05-21 22:39:572457
2458 // A simple extension that should install without error.
[email protected]650b2d52013-02-10 03:41:452459 base::FilePath path = data_dir_.AppendASCII("good.crx");
[email protected]8f512c72011-11-22 21:02:502460 InstallCRX(path, INSTALL_NEW);
[email protected]894bb502009-05-21 22:39:572461
[email protected]25b34332009-06-05 21:53:192462 ValidatePrefKeyCount(1);
[email protected]e2194742010-08-12 05:54:342463 ValidateIntegerPref(good_crx, "state", Extension::ENABLED);
[email protected]1d5e58b2013-01-31 08:41:402464 ValidateIntegerPref(good_crx, "location", Manifest::INTERNAL);
[email protected]894bb502009-05-21 22:39:572465
[email protected]ca3dbf52010-05-19 22:27:062466 // Reinstall the same version, it should overwrite the previous one.
[email protected]8f512c72011-11-22 21:02:502467 InstallCRX(path, INSTALL_UPDATED);
[email protected]894bb502009-05-21 22:39:572468
[email protected]25b34332009-06-05 21:53:192469 ValidatePrefKeyCount(1);
[email protected]e2194742010-08-12 05:54:342470 ValidateIntegerPref(good_crx, "state", Extension::ENABLED);
[email protected]1d5e58b2013-01-31 08:41:402471 ValidateIntegerPref(good_crx, "location", Manifest::INTERNAL);
[email protected]894bb502009-05-21 22:39:572472}
2473
[email protected]620db1762011-07-15 21:57:342474// Test that we can determine if extensions came from the
2475// Chrome web store.
[email protected]d9a61e12012-11-14 02:43:472476TEST_F(ExtensionServiceTest, FromWebStore) {
[email protected]8266d662011-07-12 21:53:262477 InitializeEmptyExtensionService();
2478
2479 // A simple extension that should install without error.
[email protected]650b2d52013-02-10 03:41:452480 base::FilePath path = data_dir_.AppendASCII("good.crx");
[email protected]8f512c72011-11-22 21:02:502481 // Not from web store.
2482 const Extension* extension = InstallCRX(path, INSTALL_NEW);
2483 std::string id = extension->id();
[email protected]8266d662011-07-12 21:53:262484
[email protected]8266d662011-07-12 21:53:262485 ValidatePrefKeyCount(1);
2486 ValidateBooleanPref(good_crx, "from_webstore", false);
[email protected]620db1762011-07-15 21:57:342487 ASSERT_FALSE(extension->from_webstore());
[email protected]8266d662011-07-12 21:53:262488
[email protected]8266d662011-07-12 21:53:262489 // Test install from web store.
[email protected]8f512c72011-11-22 21:02:502490 InstallCRXFromWebStore(path, INSTALL_UPDATED); // From web store.
[email protected]8266d662011-07-12 21:53:262491
[email protected]8266d662011-07-12 21:53:262492 ValidatePrefKeyCount(1);
2493 ValidateBooleanPref(good_crx, "from_webstore", true);
[email protected]620db1762011-07-15 21:57:342494
2495 // Reload so extension gets reinitialized with new value.
2496 service_->ReloadExtensions();
[email protected]8f512c72011-11-22 21:02:502497 extension = service_->GetExtensionById(id, false);
[email protected]620db1762011-07-15 21:57:342498 ASSERT_TRUE(extension->from_webstore());
[email protected]3d729722011-09-20 02:57:092499
2500 // Upgrade to version 2.0
2501 path = data_dir_.AppendASCII("good2.crx");
2502 UpdateExtension(good_crx, path, ENABLED);
2503 ValidatePrefKeyCount(1);
2504 ValidateBooleanPref(good_crx, "from_webstore", true);
[email protected]8266d662011-07-12 21:53:262505}
2506
[email protected]fbcc40302009-06-12 20:45:452507// Test upgrading a signed extension.
[email protected]d9a61e12012-11-14 02:43:472508TEST_F(ExtensionServiceTest, UpgradeSignedGood) {
[email protected]eaa7dd182010-12-14 11:09:002509 InitializeEmptyExtensionService();
[email protected]fbcc40302009-06-12 20:45:452510
[email protected]650b2d52013-02-10 03:41:452511 base::FilePath path = data_dir_.AppendASCII("good.crx");
[email protected]8f512c72011-11-22 21:02:502512 const Extension* extension = InstallCRX(path, INSTALL_NEW);
2513 std::string id = extension->id();
[email protected]fbcc40302009-06-12 20:45:452514
[email protected]8f512c72011-11-22 21:02:502515 ASSERT_EQ("1.0.0.0", extension->version()->GetString());
[email protected]fbcc40302009-06-12 20:45:452516 ASSERT_EQ(0u, GetErrors().size());
2517
[email protected]8f512c72011-11-22 21:02:502518 // Upgrade to version 1.0.0.1
[email protected]e85e34c32011-04-13 18:38:352519 path = data_dir_.AppendASCII("good2.crx");
[email protected]8f512c72011-11-22 21:02:502520 InstallCRX(path, INSTALL_UPDATED);
2521 extension = service_->GetExtensionById(id, false);
[email protected]fbcc40302009-06-12 20:45:452522
[email protected]8f512c72011-11-22 21:02:502523 ASSERT_EQ("1.0.0.1", extension->version()->GetString());
[email protected]fbcc40302009-06-12 20:45:452524 ASSERT_EQ(0u, GetErrors().size());
2525}
2526
2527// Test upgrading a signed extension with a bad signature.
[email protected]d9a61e12012-11-14 02:43:472528TEST_F(ExtensionServiceTest, UpgradeSignedBad) {
[email protected]eaa7dd182010-12-14 11:09:002529 InitializeEmptyExtensionService();
[email protected]fbcc40302009-06-12 20:45:452530
[email protected]650b2d52013-02-10 03:41:452531 base::FilePath path = data_dir_.AppendASCII("good.crx");
[email protected]8f512c72011-11-22 21:02:502532 InstallCRX(path, INSTALL_NEW);
[email protected]fbcc40302009-06-12 20:45:452533
2534 // Try upgrading with a bad signature. This should fail during the unpack,
2535 // because the key will not match the signature.
[email protected]e85e34c32011-04-13 18:38:352536 path = data_dir_.AppendASCII("bad_signature.crx");
[email protected]8f512c72011-11-22 21:02:502537 InstallCRX(path, INSTALL_FAILED);
[email protected]fbcc40302009-06-12 20:45:452538}
2539
[email protected]e957fe52009-06-23 16:51:052540// Test a normal update via the UpdateExtension API
[email protected]d9a61e12012-11-14 02:43:472541TEST_F(ExtensionServiceTest, UpdateExtension) {
[email protected]eaa7dd182010-12-14 11:09:002542 InitializeEmptyExtensionService();
[email protected]e957fe52009-06-23 16:51:052543
[email protected]650b2d52013-02-10 03:41:452544 base::FilePath path = data_dir_.AppendASCII("good.crx");
[email protected]e957fe52009-06-23 16:51:052545
[email protected]8f512c72011-11-22 21:02:502546 const Extension* good = InstallCRX(path, INSTALL_NEW);
[email protected]e957fe52009-06-23 16:51:052547 ASSERT_EQ("1.0.0.0", good->VersionString());
2548 ASSERT_EQ(good_crx, good->id());
2549
[email protected]e85e34c32011-04-13 18:38:352550 path = data_dir_.AppendASCII("good2.crx");
[email protected]4416c5a2010-06-26 01:28:572551 UpdateExtension(good_crx, path, ENABLED);
[email protected]8f512c72011-11-22 21:02:502552 ASSERT_EQ("1.0.0.1",
2553 service_->GetExtensionById(good_crx, false)->
2554 version()->GetString());
[email protected]e957fe52009-06-23 16:51:052555}
2556
[email protected]3c4abc82012-10-22 22:25:542557// Extensions should not be updated during browser shutdown.
[email protected]d9a61e12012-11-14 02:43:472558TEST_F(ExtensionServiceTest, UpdateExtensionDuringShutdown) {
[email protected]3c4abc82012-10-22 22:25:542559 InitializeEmptyExtensionService();
2560
2561 // Install an extension.
[email protected]650b2d52013-02-10 03:41:452562 base::FilePath path = data_dir_.AppendASCII("good.crx");
[email protected]3c4abc82012-10-22 22:25:542563 const Extension* good = InstallCRX(path, INSTALL_NEW);
2564 ASSERT_EQ(good_crx, good->id());
2565
2566 // Simulate shutdown.
2567 service_->set_browser_terminating_for_test(true);
2568
2569 // Update should fail and extension should not be updated.
2570 path = data_dir_.AppendASCII("good2.crx");
2571 bool updated = service_->UpdateExtension(good_crx, path, GURL(), NULL);
2572 ASSERT_FALSE(updated);
2573 ASSERT_EQ("1.0.0.0",
2574 service_->GetExtensionById(good_crx, false)->
2575 version()->GetString());
2576}
2577
[email protected]e957fe52009-06-23 16:51:052578// Test updating a not-already-installed extension - this should fail
[email protected]d9a61e12012-11-14 02:43:472579TEST_F(ExtensionServiceTest, UpdateNotInstalledExtension) {
[email protected]eaa7dd182010-12-14 11:09:002580 InitializeEmptyExtensionService();
[email protected]e957fe52009-06-23 16:51:052581
[email protected]650b2d52013-02-10 03:41:452582 base::FilePath path = data_dir_.AppendASCII("good.crx");
[email protected]4416c5a2010-06-26 01:28:572583 UpdateExtension(good_crx, path, UPDATED);
[email protected]b8f50ce2012-11-17 12:37:572584 loop_.RunUntilIdle();
[email protected]e957fe52009-06-23 16:51:052585
2586 ASSERT_EQ(0u, service_->extensions()->size());
2587 ASSERT_FALSE(installed_);
2588 ASSERT_EQ(0u, loaded_.size());
2589}
2590
2591// Makes sure you can't downgrade an extension via UpdateExtension
[email protected]d9a61e12012-11-14 02:43:472592TEST_F(ExtensionServiceTest, UpdateWillNotDowngrade) {
[email protected]eaa7dd182010-12-14 11:09:002593 InitializeEmptyExtensionService();
[email protected]e957fe52009-06-23 16:51:052594
[email protected]650b2d52013-02-10 03:41:452595 base::FilePath path = data_dir_.AppendASCII("good2.crx");
[email protected]e957fe52009-06-23 16:51:052596
[email protected]8f512c72011-11-22 21:02:502597 const Extension* good = InstallCRX(path, INSTALL_NEW);
[email protected]e957fe52009-06-23 16:51:052598 ASSERT_EQ("1.0.0.1", good->VersionString());
2599 ASSERT_EQ(good_crx, good->id());
2600
2601 // Change path from good2.crx -> good.crx
[email protected]e85e34c32011-04-13 18:38:352602 path = data_dir_.AppendASCII("good.crx");
[email protected]4416c5a2010-06-26 01:28:572603 UpdateExtension(good_crx, path, FAILED);
[email protected]8f512c72011-11-22 21:02:502604 ASSERT_EQ("1.0.0.1",
2605 service_->GetExtensionById(good_crx, false)->
2606 version()->GetString());
[email protected]e957fe52009-06-23 16:51:052607}
2608
2609// Make sure calling update with an identical version does nothing
[email protected]d9a61e12012-11-14 02:43:472610TEST_F(ExtensionServiceTest, UpdateToSameVersionIsNoop) {
[email protected]eaa7dd182010-12-14 11:09:002611 InitializeEmptyExtensionService();
[email protected]e957fe52009-06-23 16:51:052612
[email protected]650b2d52013-02-10 03:41:452613 base::FilePath path = data_dir_.AppendASCII("good.crx");
[email protected]e957fe52009-06-23 16:51:052614
[email protected]8f512c72011-11-22 21:02:502615 const Extension* good = InstallCRX(path, INSTALL_NEW);
[email protected]e957fe52009-06-23 16:51:052616 ASSERT_EQ(good_crx, good->id());
[email protected]4416c5a2010-06-26 01:28:572617 UpdateExtension(good_crx, path, FAILED_SILENTLY);
[email protected]aa142702010-03-26 01:26:332618}
2619
[email protected]dbec3792010-08-10 00:08:452620// Tests that updating an extension does not clobber old state.
[email protected]d9a61e12012-11-14 02:43:472621TEST_F(ExtensionServiceTest, UpdateExtensionPreservesState) {
[email protected]eaa7dd182010-12-14 11:09:002622 InitializeEmptyExtensionService();
[email protected]dbec3792010-08-10 00:08:452623
[email protected]650b2d52013-02-10 03:41:452624 base::FilePath path = data_dir_.AppendASCII("good.crx");
[email protected]dbec3792010-08-10 00:08:452625
[email protected]8f512c72011-11-22 21:02:502626 const Extension* good = InstallCRX(path, INSTALL_NEW);
[email protected]dbec3792010-08-10 00:08:452627 ASSERT_EQ("1.0.0.0", good->VersionString());
2628 ASSERT_EQ(good_crx, good->id());
2629
2630 // Disable it and allow it to run in incognito. These settings should carry
2631 // over to the updated version.
[email protected]44d62b62012-04-11 00:06:032632 service_->DisableExtension(good->id(), Extension::DISABLE_USER_ACTION);
[email protected]c3cfb012011-04-06 22:07:352633 service_->SetIsIncognitoEnabled(good->id(), true);
[email protected]c38fd352012-03-28 04:06:122634 service_->extension_prefs()->SetDidExtensionEscalatePermissions(good, true);
[email protected]dbec3792010-08-10 00:08:452635
[email protected]e85e34c32011-04-13 18:38:352636 path = data_dir_.AppendASCII("good2.crx");
[email protected]dbec3792010-08-10 00:08:452637 UpdateExtension(good_crx, path, INSTALLED);
[email protected]98270432012-09-11 20:51:242638 ASSERT_EQ(1u, service_->disabled_extensions()->size());\
[email protected]8f512c72011-11-22 21:02:502639 const Extension* good2 = service_->GetExtensionById(good_crx, true);
[email protected]dbec3792010-08-10 00:08:452640 ASSERT_EQ("1.0.0.1", good2->version()->GetString());
[email protected]c3cfb012011-04-06 22:07:352641 EXPECT_TRUE(service_->IsIncognitoEnabled(good2->id()));
[email protected]c38fd352012-03-28 04:06:122642 EXPECT_TRUE(service_->extension_prefs()->DidExtensionEscalatePermissions(
2643 good2->id()));
[email protected]dbec3792010-08-10 00:08:452644}
2645
[email protected]5eb375e92010-11-26 07:50:412646// Tests that updating preserves extension location.
[email protected]d9a61e12012-11-14 02:43:472647TEST_F(ExtensionServiceTest, UpdateExtensionPreservesLocation) {
[email protected]eaa7dd182010-12-14 11:09:002648 InitializeEmptyExtensionService();
[email protected]5eb375e92010-11-26 07:50:412649
[email protected]650b2d52013-02-10 03:41:452650 base::FilePath path = data_dir_.AppendASCII("good.crx");
[email protected]5eb375e92010-11-26 07:50:412651
[email protected]d41e2152012-02-24 04:20:272652 const Extension* good =
[email protected]1d5e58b2013-01-31 08:41:402653 InstallCRXWithLocation(path, Manifest::EXTERNAL_PREF, INSTALL_NEW);
[email protected]5eb375e92010-11-26 07:50:412654
2655 ASSERT_EQ("1.0.0.0", good->VersionString());
2656 ASSERT_EQ(good_crx, good->id());
2657
[email protected]e85e34c32011-04-13 18:38:352658 path = data_dir_.AppendASCII("good2.crx");
[email protected]5eb375e92010-11-26 07:50:412659 UpdateExtension(good_crx, path, ENABLED);
[email protected]8f512c72011-11-22 21:02:502660 const Extension* good2 = service_->GetExtensionById(good_crx, false);
[email protected]5eb375e92010-11-26 07:50:412661 ASSERT_EQ("1.0.0.1", good2->version()->GetString());
[email protected]1d5e58b2013-01-31 08:41:402662 EXPECT_EQ(good2->location(), Manifest::EXTERNAL_PREF);
[email protected]5eb375e92010-11-26 07:50:412663}
2664
[email protected]66e26872010-12-03 20:07:252665// Makes sure that LOAD extension types can downgrade.
[email protected]d9a61e12012-11-14 02:43:472666TEST_F(ExtensionServiceTest, LoadExtensionsCanDowngrade) {
[email protected]eaa7dd182010-12-14 11:09:002667 InitializeEmptyExtensionService();
[email protected]66e26872010-12-03 20:07:252668
[email protected]ea1a3f62012-11-16 20:34:232669 base::ScopedTempDir temp;
[email protected]66e26872010-12-03 20:07:252670 ASSERT_TRUE(temp.CreateUniqueTempDir());
2671
2672 // We'll write the extension manifest dynamically to a temporary path
2673 // to make it easier to change the version number.
[email protected]650b2d52013-02-10 03:41:452674 base::FilePath extension_path = temp.path();
2675 base::FilePath manifest_path =
[email protected]993da5e2013-03-23 21:25:162676 extension_path.Append(extensions::kManifestFilename);
[email protected]66e26872010-12-03 20:07:252677 ASSERT_FALSE(file_util::PathExists(manifest_path));
2678
2679 // Start with version 2.0.
2680 DictionaryValue manifest;
2681 manifest.SetString("version", "2.0");
2682 manifest.SetString("name", "LOAD Downgrade Test");
[email protected]b3d52852011-12-07 01:01:112683 manifest.SetInteger("manifest_version", 2);
[email protected]66e26872010-12-03 20:07:252684
2685 JSONFileValueSerializer serializer(manifest_path);
2686 ASSERT_TRUE(serializer.Serialize(manifest));
2687
[email protected]d8c8f25f2011-11-02 18:18:012688 extensions::UnpackedInstaller::Create(service_)->Load(extension_path);
[email protected]b8f50ce2012-11-17 12:37:572689 loop_.RunUntilIdle();
[email protected]66e26872010-12-03 20:07:252690
2691 EXPECT_EQ(0u, GetErrors().size());
2692 ASSERT_EQ(1u, loaded_.size());
[email protected]12075d12013-02-27 05:38:052693 EXPECT_EQ(Manifest::UNPACKED, loaded_[0]->location());
[email protected]66e26872010-12-03 20:07:252694 EXPECT_EQ(1u, service_->extensions()->size());
2695 EXPECT_EQ("2.0", loaded_[0]->VersionString());
2696
2697 // Now set the version number to 1.0, reload the extensions and verify that
2698 // the downgrade was accepted.
2699 manifest.SetString("version", "1.0");
2700 ASSERT_TRUE(serializer.Serialize(manifest));
2701
[email protected]d8c8f25f2011-11-02 18:18:012702 extensions::UnpackedInstaller::Create(service_)->Load(extension_path);
[email protected]b8f50ce2012-11-17 12:37:572703 loop_.RunUntilIdle();
[email protected]66e26872010-12-03 20:07:252704
2705 EXPECT_EQ(0u, GetErrors().size());
2706 ASSERT_EQ(1u, loaded_.size());
[email protected]12075d12013-02-27 05:38:052707 EXPECT_EQ(Manifest::UNPACKED, loaded_[0]->location());
[email protected]66e26872010-12-03 20:07:252708 EXPECT_EQ(1u, service_->extensions()->size());
2709 EXPECT_EQ("1.0", loaded_[0]->VersionString());
2710}
2711
[email protected]0d904312012-01-25 23:00:162712#if !defined(OS_CHROMEOS)
2713// LOAD extensions with plugins require approval.
[email protected]d9a61e12012-11-14 02:43:472714TEST_F(ExtensionServiceTest, LoadExtensionsWithPlugins) {
[email protected]650b2d52013-02-10 03:41:452715 base::FilePath extension_with_plugin_path = data_dir_
[email protected]0d904312012-01-25 23:00:162716 .AppendASCII("good")
2717 .AppendASCII("Extensions")
2718 .AppendASCII(good1)
2719 .AppendASCII("2");
[email protected]650b2d52013-02-10 03:41:452720 base::FilePath extension_no_plugin_path = data_dir_
[email protected]0d904312012-01-25 23:00:162721 .AppendASCII("good")
2722 .AppendASCII("Extensions")
2723 .AppendASCII(good2)
2724 .AppendASCII("1.0");
2725
[email protected]ebd71962012-12-20 02:56:552726 InitPluginService();
[email protected]0d904312012-01-25 23:00:162727 InitializeEmptyExtensionService();
2728 InitializeExtensionProcessManager();
2729 service_->set_show_extensions_prompts(true);
2730
2731 // Start by canceling any install prompts.
2732 CommandLine::ForCurrentProcess()->AppendSwitchASCII(
2733 switches::kAppsGalleryInstallAutoConfirmForTests,
2734 "cancel");
2735
2736 // The extension that has a plugin should not install.
2737 extensions::UnpackedInstaller::Create(service_)->Load(
2738 extension_with_plugin_path);
[email protected]b8f50ce2012-11-17 12:37:572739 loop_.RunUntilIdle();
[email protected]0d904312012-01-25 23:00:162740 EXPECT_EQ(0u, GetErrors().size());
2741 EXPECT_EQ(0u, loaded_.size());
2742 EXPECT_EQ(0u, service_->extensions()->size());
2743 EXPECT_EQ(0u, service_->disabled_extensions()->size());
2744
2745 // But the extension with no plugin should since there's no prompt.
2746 extensions::UnpackedInstaller::Create(service_)->Load(
2747 extension_no_plugin_path);
[email protected]b8f50ce2012-11-17 12:37:572748 loop_.RunUntilIdle();
[email protected]0d904312012-01-25 23:00:162749 EXPECT_EQ(0u, GetErrors().size());
2750 EXPECT_EQ(1u, loaded_.size());
2751 EXPECT_EQ(1u, service_->extensions()->size());
2752 EXPECT_EQ(0u, service_->disabled_extensions()->size());
2753 EXPECT_TRUE(service_->extensions()->Contains(good2));
2754
2755 // The plugin extension should install if we accept the dialog.
2756 CommandLine::ForCurrentProcess()->AppendSwitchASCII(
2757 switches::kAppsGalleryInstallAutoConfirmForTests,
2758 "accept");
2759
2760 extensions::UnpackedInstaller::Create(service_)->Load(
2761 extension_with_plugin_path);
[email protected]b8f50ce2012-11-17 12:37:572762 loop_.RunUntilIdle();
[email protected]0d904312012-01-25 23:00:162763 EXPECT_EQ(0u, GetErrors().size());
2764 EXPECT_EQ(2u, loaded_.size());
2765 EXPECT_EQ(2u, service_->extensions()->size());
2766 EXPECT_EQ(0u, service_->disabled_extensions()->size());
2767 EXPECT_TRUE(service_->extensions()->Contains(good1));
2768 EXPECT_TRUE(service_->extensions()->Contains(good2));
2769
2770 // Make sure the granted permissions have been setup.
[email protected]c2e66e12012-06-27 06:27:062771 scoped_refptr<PermissionSet> permissions(
[email protected]0d904312012-01-25 23:00:162772 service_->extension_prefs()->GetGrantedPermissions(good1));
2773 EXPECT_FALSE(permissions->IsEmpty());
2774 EXPECT_TRUE(permissions->HasEffectiveFullAccess());
2775 EXPECT_FALSE(permissions->apis().empty());
[email protected]c2e66e12012-06-27 06:27:062776 EXPECT_TRUE(permissions->HasAPIPermission(APIPermission::kPlugin));
[email protected]0d904312012-01-25 23:00:162777
2778 // We should be able to reload the extension without getting another prompt.
2779 loaded_.clear();
2780 CommandLine::ForCurrentProcess()->AppendSwitchASCII(
2781 switches::kAppsGalleryInstallAutoConfirmForTests,
2782 "cancel");
2783
2784 service_->ReloadExtension(good1);
[email protected]b8f50ce2012-11-17 12:37:572785 loop_.RunUntilIdle();
[email protected]0d904312012-01-25 23:00:162786 EXPECT_EQ(1u, loaded_.size());
2787 EXPECT_EQ(2u, service_->extensions()->size());
2788 EXPECT_EQ(0u, service_->disabled_extensions()->size());
2789}
2790#endif
2791
[email protected]7fa19f82010-12-21 19:40:082792namespace {
2793
2794bool IsExtension(const Extension& extension) {
[email protected]1d5e58b2013-01-31 08:41:402795 return extension.GetType() == Manifest::TYPE_EXTENSION;
[email protected]7fa19f82010-12-21 19:40:082796}
2797
2798} // namespace
2799
[email protected]aa142702010-03-26 01:26:332800// Test adding a pending extension.
[email protected]d9a61e12012-11-14 02:43:472801TEST_F(ExtensionServiceTest, AddPendingExtensionFromSync) {
[email protected]eaa7dd182010-12-14 11:09:002802 InitializeEmptyExtensionService();
[email protected]aa142702010-03-26 01:26:332803
[email protected]145a317b2011-04-12 16:03:462804 const std::string kFakeId(all_zero);
[email protected]aa142702010-03-26 01:26:332805 const GURL kFakeUpdateURL("http:://fake.update/url");
[email protected]aa142702010-03-26 01:26:332806 const bool kFakeInstallSilently(true);
2807
[email protected]f4d5e1a2011-04-28 02:08:252808 EXPECT_TRUE(service_->pending_extension_manager()->AddFromSync(
[email protected]7fa19f82010-12-21 19:40:082809 kFakeId, kFakeUpdateURL, &IsExtension,
[email protected]6cc7dbae2011-04-29 21:18:332810 kFakeInstallSilently));
[email protected]b2907fd2011-03-25 16:43:372811
[email protected]3f213ad2012-07-26 23:39:412812 const extensions::PendingExtensionInfo* pending_extension_info;
[email protected]51a3bf8b2012-06-08 22:53:062813 ASSERT_TRUE((pending_extension_info = service_->pending_extension_manager()->
2814 GetById(kFakeId)));
2815 EXPECT_EQ(kFakeUpdateURL, pending_extension_info->update_url());
2816 EXPECT_EQ(&IsExtension, pending_extension_info->should_allow_install_);
2817 EXPECT_EQ(kFakeInstallSilently, pending_extension_info->install_silently());
[email protected]aa142702010-03-26 01:26:332818}
2819
[email protected]aa142702010-03-26 01:26:332820namespace {
2821const char kGoodId[] = "ldnnhddmnhbkjipkidpdiheffobcpfmf";
2822const char kGoodUpdateURL[] = "http://good.update/url";
[email protected]8ef78fd2010-08-19 17:14:322823const bool kGoodIsFromSync = true;
[email protected]aa142702010-03-26 01:26:332824const bool kGoodInstallSilently = true;
[email protected]aa142702010-03-26 01:26:332825} // namespace
2826
2827// Test updating a pending extension.
[email protected]d9a61e12012-11-14 02:43:472828TEST_F(ExtensionServiceTest, UpdatePendingExtension) {
[email protected]eaa7dd182010-12-14 11:09:002829 InitializeEmptyExtensionService();
[email protected]f4d5e1a2011-04-28 02:08:252830 EXPECT_TRUE(service_->pending_extension_manager()->AddFromSync(
[email protected]7fa19f82010-12-21 19:40:082831 kGoodId, GURL(kGoodUpdateURL), &IsExtension,
[email protected]6cc7dbae2011-04-29 21:18:332832 kGoodInstallSilently));
[email protected]b2907fd2011-03-25 16:43:372833 EXPECT_TRUE(service_->pending_extension_manager()->IsIdPending(kGoodId));
[email protected]aa142702010-03-26 01:26:332834
[email protected]650b2d52013-02-10 03:41:452835 base::FilePath path = data_dir_.AppendASCII("good.crx");
[email protected]6cc7dbae2011-04-29 21:18:332836 UpdateExtension(kGoodId, path, ENABLED);
[email protected]aa142702010-03-26 01:26:332837
[email protected]b2907fd2011-03-25 16:43:372838 EXPECT_FALSE(service_->pending_extension_manager()->IsIdPending(kGoodId));
[email protected]4416c5a2010-06-26 01:28:572839
[email protected]9adb9692010-10-29 23:14:022840 const Extension* extension = service_->GetExtensionById(kGoodId, true);
[email protected]4416c5a2010-06-26 01:28:572841 ASSERT_TRUE(extension);
[email protected]aa142702010-03-26 01:26:332842}
2843
[email protected]7fa19f82010-12-21 19:40:082844namespace {
2845
2846bool IsTheme(const Extension& extension) {
2847 return extension.is_theme();
2848}
2849
2850} // namespace
2851
[email protected]11edd1e2010-07-21 00:14:502852// Test updating a pending theme.
[email protected]7f2c5552011-12-20 22:53:162853// Disabled due to ASAN failure. http://crbug.com/108320
[email protected]d9a61e12012-11-14 02:43:472854TEST_F(ExtensionServiceTest, DISABLED_UpdatePendingTheme) {
[email protected]eaa7dd182010-12-14 11:09:002855 InitializeEmptyExtensionService();
[email protected]f4d5e1a2011-04-28 02:08:252856 EXPECT_TRUE(service_->pending_extension_manager()->AddFromSync(
[email protected]6cc7dbae2011-04-29 21:18:332857 theme_crx, GURL(), &IsTheme, false));
[email protected]b2907fd2011-03-25 16:43:372858 EXPECT_TRUE(service_->pending_extension_manager()->IsIdPending(theme_crx));
[email protected]11edd1e2010-07-21 00:14:502859
[email protected]650b2d52013-02-10 03:41:452860 base::FilePath path = data_dir_.AppendASCII("theme.crx");
[email protected]11edd1e2010-07-21 00:14:502861 UpdateExtension(theme_crx, path, ENABLED);
2862
[email protected]b2907fd2011-03-25 16:43:372863 EXPECT_FALSE(service_->pending_extension_manager()->IsIdPending(theme_crx));
[email protected]11edd1e2010-07-21 00:14:502864
[email protected]9adb9692010-10-29 23:14:022865 const Extension* extension = service_->GetExtensionById(theme_crx, true);
[email protected]11edd1e2010-07-21 00:14:502866 ASSERT_TRUE(extension);
2867
[email protected]ad83ca242011-07-29 01:32:252868 EXPECT_FALSE(
2869 service_->extension_prefs()->IsExtensionDisabled(extension->id()));
[email protected]36429da2011-07-11 20:25:182870 EXPECT_TRUE(service_->IsExtensionEnabled(theme_crx));
[email protected]11edd1e2010-07-21 00:14:502871}
2872
[email protected]d74f92e2011-04-18 15:39:232873#if defined(OS_CHROMEOS)
2874// Always fails on ChromeOS: http://crbug.com/79737
[email protected]e00ccc92012-11-01 17:32:302875#define MAYBE_UpdatePendingExternalCrx DISABLED_UpdatePendingExternalCrx
[email protected]d74f92e2011-04-18 15:39:232876#else
2877#define MAYBE_UpdatePendingExternalCrx UpdatePendingExternalCrx
2878#endif
[email protected]8ef78fd2010-08-19 17:14:322879// Test updating a pending CRX as if the source is an external extension
2880// with an update URL. In this case we don't know if the CRX is a theme
2881// or not.
[email protected]d9a61e12012-11-14 02:43:472882TEST_F(ExtensionServiceTest, MAYBE_UpdatePendingExternalCrx) {
[email protected]eaa7dd182010-12-14 11:09:002883 InitializeEmptyExtensionService();
[email protected]9060d8b02012-01-13 02:14:302884 EXPECT_TRUE(service_->pending_extension_manager()->AddFromExternalUpdateUrl(
[email protected]1d5e58b2013-01-31 08:41:402885 theme_crx, GURL(), Manifest::EXTERNAL_PREF_DOWNLOAD));
[email protected]8ef78fd2010-08-19 17:14:322886
[email protected]b2907fd2011-03-25 16:43:372887 EXPECT_TRUE(service_->pending_extension_manager()->IsIdPending(theme_crx));
[email protected]8ef78fd2010-08-19 17:14:322888
[email protected]650b2d52013-02-10 03:41:452889 base::FilePath path = data_dir_.AppendASCII("theme.crx");
[email protected]8ef78fd2010-08-19 17:14:322890 UpdateExtension(theme_crx, path, ENABLED);
2891
[email protected]b2907fd2011-03-25 16:43:372892 EXPECT_FALSE(service_->pending_extension_manager()->IsIdPending(theme_crx));
[email protected]8ef78fd2010-08-19 17:14:322893
[email protected]9adb9692010-10-29 23:14:022894 const Extension* extension = service_->GetExtensionById(theme_crx, true);
[email protected]8ef78fd2010-08-19 17:14:322895 ASSERT_TRUE(extension);
2896
[email protected]ad83ca242011-07-29 01:32:252897 EXPECT_FALSE(
2898 service_->extension_prefs()->IsExtensionDisabled(extension->id()));
[email protected]36429da2011-07-11 20:25:182899 EXPECT_TRUE(service_->IsExtensionEnabled(extension->id()));
[email protected]c3cfb012011-04-06 22:07:352900 EXPECT_FALSE(service_->IsIncognitoEnabled(extension->id()));
[email protected]8ef78fd2010-08-19 17:14:322901}
2902
[email protected]1afaf2a52010-11-02 19:29:172903// Test updating a pending CRX as if the source is an external extension
2904// with an update URL. The external update should overwrite a sync update,
2905// but a sync update should not overwrite a non-sync update.
[email protected]d9a61e12012-11-14 02:43:472906TEST_F(ExtensionServiceTest, UpdatePendingExternalCrxWinsOverSync) {
[email protected]eaa7dd182010-12-14 11:09:002907 InitializeEmptyExtensionService();
[email protected]1afaf2a52010-11-02 19:29:172908
2909 // Add a crx to be installed from the update mechanism.
[email protected]f4d5e1a2011-04-28 02:08:252910 EXPECT_TRUE(service_->pending_extension_manager()->AddFromSync(
[email protected]7fa19f82010-12-21 19:40:082911 kGoodId, GURL(kGoodUpdateURL), &IsExtension,
[email protected]6cc7dbae2011-04-29 21:18:332912 kGoodInstallSilently));
[email protected]1afaf2a52010-11-02 19:29:172913
2914 // Check that there is a pending crx, with is_from_sync set to true.
[email protected]3f213ad2012-07-26 23:39:412915 const extensions::PendingExtensionInfo* pending_extension_info;
[email protected]51a3bf8b2012-06-08 22:53:062916 ASSERT_TRUE((pending_extension_info = service_->pending_extension_manager()->
2917 GetById(kGoodId)));
2918 EXPECT_TRUE(pending_extension_info->is_from_sync());
[email protected]1afaf2a52010-11-02 19:29:172919
2920 // Add a crx to be updated, with the same ID, from a non-sync source.
[email protected]9060d8b02012-01-13 02:14:302921 EXPECT_TRUE(service_->pending_extension_manager()->AddFromExternalUpdateUrl(
[email protected]1d5e58b2013-01-31 08:41:402922 kGoodId, GURL(kGoodUpdateURL), Manifest::EXTERNAL_PREF_DOWNLOAD));
[email protected]1afaf2a52010-11-02 19:29:172923
2924 // Check that there is a pending crx, with is_from_sync set to false.
[email protected]51a3bf8b2012-06-08 22:53:062925 ASSERT_TRUE((pending_extension_info = service_->pending_extension_manager()->
2926 GetById(kGoodId)));
2927 EXPECT_FALSE(pending_extension_info->is_from_sync());
[email protected]1d5e58b2013-01-31 08:41:402928 EXPECT_EQ(Manifest::EXTERNAL_PREF_DOWNLOAD,
[email protected]51a3bf8b2012-06-08 22:53:062929 pending_extension_info->install_source());
[email protected]1afaf2a52010-11-02 19:29:172930
2931 // Add a crx to be installed from the update mechanism.
[email protected]f4d5e1a2011-04-28 02:08:252932 EXPECT_FALSE(service_->pending_extension_manager()->AddFromSync(
[email protected]7fa19f82010-12-21 19:40:082933 kGoodId, GURL(kGoodUpdateURL), &IsExtension,
[email protected]6cc7dbae2011-04-29 21:18:332934 kGoodInstallSilently));
[email protected]1afaf2a52010-11-02 19:29:172935
2936 // Check that the external, non-sync update was not overridden.
[email protected]51a3bf8b2012-06-08 22:53:062937 ASSERT_TRUE((pending_extension_info = service_->pending_extension_manager()->
2938 GetById(kGoodId)));
2939 EXPECT_FALSE(pending_extension_info->is_from_sync());
[email protected]1d5e58b2013-01-31 08:41:402940 EXPECT_EQ(Manifest::EXTERNAL_PREF_DOWNLOAD,
[email protected]51a3bf8b2012-06-08 22:53:062941 pending_extension_info->install_source());
[email protected]1afaf2a52010-11-02 19:29:172942}
2943
[email protected]8ef78fd2010-08-19 17:14:322944// Updating a theme should fail if the updater is explicitly told that
2945// the CRX is not a theme.
[email protected]d9a61e12012-11-14 02:43:472946TEST_F(ExtensionServiceTest, UpdatePendingCrxThemeMismatch) {
[email protected]eaa7dd182010-12-14 11:09:002947 InitializeEmptyExtensionService();
[email protected]f4d5e1a2011-04-28 02:08:252948 EXPECT_TRUE(service_->pending_extension_manager()->AddFromSync(
[email protected]6cc7dbae2011-04-29 21:18:332949 theme_crx, GURL(), &IsExtension, true));
[email protected]8ef78fd2010-08-19 17:14:322950
[email protected]b2907fd2011-03-25 16:43:372951 EXPECT_TRUE(service_->pending_extension_manager()->IsIdPending(theme_crx));
[email protected]8ef78fd2010-08-19 17:14:322952
[email protected]650b2d52013-02-10 03:41:452953 base::FilePath path = data_dir_.AppendASCII("theme.crx");
[email protected]8ef78fd2010-08-19 17:14:322954 UpdateExtension(theme_crx, path, FAILED_SILENTLY);
2955
[email protected]b2907fd2011-03-25 16:43:372956 EXPECT_FALSE(service_->pending_extension_manager()->IsIdPending(theme_crx));
[email protected]8ef78fd2010-08-19 17:14:322957
[email protected]9adb9692010-10-29 23:14:022958 const Extension* extension = service_->GetExtensionById(theme_crx, true);
[email protected]8ef78fd2010-08-19 17:14:322959 ASSERT_FALSE(extension);
2960}
2961
[email protected]aa142702010-03-26 01:26:332962// TODO(akalin): Test updating a pending extension non-silently once
2963// we can mock out ExtensionInstallUI and inject our version into
2964// UpdateExtension().
2965
[email protected]7fa19f82010-12-21 19:40:082966// Test updating a pending extension which fails the should-install test.
[email protected]d9a61e12012-11-14 02:43:472967TEST_F(ExtensionServiceTest, UpdatePendingExtensionFailedShouldInstallTest) {
[email protected]eaa7dd182010-12-14 11:09:002968 InitializeEmptyExtensionService();
[email protected]aa142702010-03-26 01:26:332969 // Add pending extension with a flipped is_theme.
[email protected]f4d5e1a2011-04-28 02:08:252970 EXPECT_TRUE(service_->pending_extension_manager()->AddFromSync(
[email protected]6cc7dbae2011-04-29 21:18:332971 kGoodId, GURL(kGoodUpdateURL), &IsTheme, kGoodInstallSilently));
[email protected]b2907fd2011-03-25 16:43:372972 EXPECT_TRUE(service_->pending_extension_manager()->IsIdPending(kGoodId));
[email protected]aa142702010-03-26 01:26:332973
[email protected]650b2d52013-02-10 03:41:452974 base::FilePath path = data_dir_.AppendASCII("good.crx");
[email protected]4416c5a2010-06-26 01:28:572975 UpdateExtension(kGoodId, path, UPDATED);
[email protected]aa142702010-03-26 01:26:332976
2977 // TODO(akalin): Figure out how to check that the extensions
2978 // directory is cleaned up properly in OnExtensionInstalled().
2979
[email protected]b2907fd2011-03-25 16:43:372980 EXPECT_FALSE(service_->pending_extension_manager()->IsIdPending(kGoodId));
[email protected]aa142702010-03-26 01:26:332981}
2982
[email protected]11edd1e2010-07-21 00:14:502983// TODO(akalin): Figure out how to test that installs of pending
2984// unsyncable extensions are blocked.
2985
[email protected]aa142702010-03-26 01:26:332986// Test updating a pending extension for one that is not pending.
[email protected]d9a61e12012-11-14 02:43:472987TEST_F(ExtensionServiceTest, UpdatePendingExtensionNotPending) {
[email protected]eaa7dd182010-12-14 11:09:002988 InitializeEmptyExtensionService();
[email protected]aa142702010-03-26 01:26:332989
[email protected]650b2d52013-02-10 03:41:452990 base::FilePath path = data_dir_.AppendASCII("good.crx");
[email protected]4416c5a2010-06-26 01:28:572991 UpdateExtension(kGoodId, path, UPDATED);
[email protected]aa142702010-03-26 01:26:332992
[email protected]b2907fd2011-03-25 16:43:372993 EXPECT_FALSE(service_->pending_extension_manager()->IsIdPending(kGoodId));
[email protected]aa142702010-03-26 01:26:332994}
2995
2996// Test updating a pending extension for one that is already
2997// installed.
[email protected]d9a61e12012-11-14 02:43:472998TEST_F(ExtensionServiceTest, UpdatePendingExtensionAlreadyInstalled) {
[email protected]eaa7dd182010-12-14 11:09:002999 InitializeEmptyExtensionService();
[email protected]aa142702010-03-26 01:26:333000
[email protected]650b2d52013-02-10 03:41:453001 base::FilePath path = data_dir_.AppendASCII("good.crx");
[email protected]8f512c72011-11-22 21:02:503002 const Extension* good = InstallCRX(path, INSTALL_NEW);
[email protected]aa142702010-03-26 01:26:333003 ASSERT_EQ(1u, service_->extensions()->size());
[email protected]aa142702010-03-26 01:26:333004
[email protected]8ef78fd2010-08-19 17:14:323005 EXPECT_FALSE(good->is_theme());
3006
[email protected]b2907fd2011-03-25 16:43:373007 // Use AddExtensionImpl() as AddFrom*() would balk.
3008 service_->pending_extension_manager()->AddExtensionImpl(
[email protected]65348062013-01-15 07:27:223009 good->id(), extensions::ManifestURL::GetUpdateURL(good),
3010 Version(), &IsExtension, kGoodIsFromSync,
[email protected]1d5e58b2013-01-31 08:41:403011 kGoodInstallSilently, Manifest::INTERNAL);
[email protected]6cc7dbae2011-04-29 21:18:333012 UpdateExtension(good->id(), path, ENABLED);
[email protected]aa142702010-03-26 01:26:333013
[email protected]b2907fd2011-03-25 16:43:373014 EXPECT_FALSE(service_->pending_extension_manager()->IsIdPending(kGoodId));
[email protected]e957fe52009-06-23 16:51:053015}
3016
[email protected]6b75ec32009-08-14 06:37:183017// Test pref settings for blacklist and unblacklist extensions.
[email protected]d9a61e12012-11-14 02:43:473018TEST_F(ExtensionServiceTest, SetUnsetBlacklistInPrefs) {
[email protected]eaa7dd182010-12-14 11:09:003019 InitializeEmptyExtensionService();
[email protected]6b75ec32009-08-14 06:37:183020 std::vector<std::string> blacklist;
3021 blacklist.push_back(good0);
3022 blacklist.push_back("invalid_id"); // an invalid id
3023 blacklist.push_back(good1);
[email protected]fdd679b2012-11-15 20:49:393024 ExtensionSystem::Get(profile_.get())->blacklist()->SetFromUpdater(blacklist,
3025 "v1");
3026
[email protected]6b75ec32009-08-14 06:37:183027 // Make sure pref is updated
[email protected]b8f50ce2012-11-17 12:37:573028 loop_.RunUntilIdle();
[email protected]6b75ec32009-08-14 06:37:183029
3030 // blacklist is set for good0,1,2
[email protected]e2194742010-08-12 05:54:343031 ValidateBooleanPref(good0, "blacklist", true);
3032 ValidateBooleanPref(good1, "blacklist", true);
[email protected]6b75ec32009-08-14 06:37:183033 // invalid_id should not be inserted to pref.
[email protected]e2194742010-08-12 05:54:343034 EXPECT_FALSE(IsPrefExist("invalid_id", "blacklist"));
[email protected]6b75ec32009-08-14 06:37:183035
3036 // remove good1, add good2
3037 blacklist.pop_back();
3038 blacklist.push_back(good2);
[email protected]fdd679b2012-11-15 20:49:393039 ExtensionSystem::Get(profile_.get())->blacklist()->SetFromUpdater(blacklist,
3040 "v2");
[email protected]6b75ec32009-08-14 06:37:183041
[email protected]6b75ec32009-08-14 06:37:183042 // only good0 and good1 should be set
[email protected]e2194742010-08-12 05:54:343043 ValidateBooleanPref(good0, "blacklist", true);
3044 EXPECT_FALSE(IsPrefExist(good1, "blacklist"));
3045 ValidateBooleanPref(good2, "blacklist", true);
3046 EXPECT_FALSE(IsPrefExist("invalid_id", "blacklist"));
[email protected]6b75ec32009-08-14 06:37:183047}
3048
3049// Unload installed extension from blacklist.
[email protected]d9a61e12012-11-14 02:43:473050TEST_F(ExtensionServiceTest, UnloadBlacklistedExtension) {
[email protected]eaa7dd182010-12-14 11:09:003051 InitializeEmptyExtensionService();
[email protected]6b75ec32009-08-14 06:37:183052
[email protected]650b2d52013-02-10 03:41:453053 base::FilePath path = data_dir_.AppendASCII("good.crx");
[email protected]6b75ec32009-08-14 06:37:183054
[email protected]8f512c72011-11-22 21:02:503055 const Extension* good = InstallCRX(path, INSTALL_NEW);
[email protected]6b75ec32009-08-14 06:37:183056 EXPECT_EQ(good_crx, good->id());
[email protected]4416c5a2010-06-26 01:28:573057 UpdateExtension(good_crx, path, FAILED_SILENTLY);
[email protected]6b75ec32009-08-14 06:37:183058
3059 std::vector<std::string> blacklist;
3060 blacklist.push_back(good_crx);
[email protected]fdd679b2012-11-15 20:49:393061 ExtensionSystem::Get(profile_.get())->blacklist()->SetFromUpdater(blacklist,
3062 "v1");
3063
[email protected]6b75ec32009-08-14 06:37:183064 // Make sure pref is updated
[email protected]b8f50ce2012-11-17 12:37:573065 loop_.RunUntilIdle();
[email protected]6b75ec32009-08-14 06:37:183066
3067 // Now, the good_crx is blacklisted.
[email protected]e2194742010-08-12 05:54:343068 ValidateBooleanPref(good_crx, "blacklist", true);
[email protected]6b75ec32009-08-14 06:37:183069 EXPECT_EQ(0u, service_->extensions()->size());
3070
3071 // Remove good_crx from blacklist
3072 blacklist.pop_back();
[email protected]fdd679b2012-11-15 20:49:393073 ExtensionSystem::Get(profile_.get())->blacklist()->SetFromUpdater(blacklist,
3074 "v2");
3075
[email protected]6b75ec32009-08-14 06:37:183076 // Make sure pref is updated
[email protected]b8f50ce2012-11-17 12:37:573077 loop_.RunUntilIdle();
[email protected]6b75ec32009-08-14 06:37:183078 // blacklist value should not be set for good_crx
[email protected]e2194742010-08-12 05:54:343079 EXPECT_FALSE(IsPrefExist(good_crx, "blacklist"));
[email protected]6b75ec32009-08-14 06:37:183080}
3081
3082// Unload installed extension from blacklist.
[email protected]d9a61e12012-11-14 02:43:473083TEST_F(ExtensionServiceTest, BlacklistedExtensionWillNotInstall) {
[email protected]eaa7dd182010-12-14 11:09:003084 InitializeEmptyExtensionService();
[email protected]6b75ec32009-08-14 06:37:183085 std::vector<std::string> blacklist;
3086 blacklist.push_back(good_crx);
[email protected]fdd679b2012-11-15 20:49:393087 ExtensionSystem::Get(profile_.get())->blacklist()->SetFromUpdater(blacklist,
3088 "v1");
3089
[email protected]6b75ec32009-08-14 06:37:183090 // Make sure pref is updated
[email protected]b8f50ce2012-11-17 12:37:573091 loop_.RunUntilIdle();
[email protected]6b75ec32009-08-14 06:37:183092
3093 // Now, the good_crx is blacklisted.
[email protected]e2194742010-08-12 05:54:343094 ValidateBooleanPref(good_crx, "blacklist", true);
[email protected]6b75ec32009-08-14 06:37:183095
3096 // We can not install good_crx.
[email protected]650b2d52013-02-10 03:41:453097 base::FilePath path = data_dir_.AppendASCII("good.crx");
[email protected]bc151cf92013-02-12 04:57:263098 InstallCRX(path, INSTALL_FAILED);
[email protected]6b75ec32009-08-14 06:37:183099 EXPECT_EQ(0u, service_->extensions()->size());
[email protected]e2194742010-08-12 05:54:343100 ValidateBooleanPref(good_crx, "blacklist", true);
[email protected]6b75ec32009-08-14 06:37:183101}
3102
[email protected]4ee07c62012-08-21 12:40:423103// Unload blacklisted extension on policy change.
[email protected]d9a61e12012-11-14 02:43:473104TEST_F(ExtensionServiceTest, UnloadBlacklistedExtensionPolicy) {
[email protected]4ee07c62012-08-21 12:40:423105 InitializeEmptyExtensionService();
[email protected]650b2d52013-02-10 03:41:453106 base::FilePath path = data_dir_.AppendASCII("good.crx");
[email protected]4ee07c62012-08-21 12:40:423107
3108 const Extension* good = InstallCRX(path, INSTALL_NEW);
3109 EXPECT_EQ(good_crx, good->id());
3110 UpdateExtension(good_crx, path, FAILED_SILENTLY);
3111 EXPECT_EQ(1u, service_->extensions()->size());
3112
3113 base::ListValue whitelist;
3114 PrefService* prefs = service_->extension_prefs()->pref_service();
3115 whitelist.Append(base::Value::CreateStringValue(good_crx));
3116 prefs->Set(prefs::kExtensionInstallAllowList, whitelist);
3117
3118 std::vector<std::string> blacklist;
3119 blacklist.push_back(good_crx);
[email protected]fdd679b2012-11-15 20:49:393120 ExtensionSystem::Get(profile_.get())->blacklist()->SetFromUpdater(blacklist,
3121 "v1");
3122
[email protected]4ee07c62012-08-21 12:40:423123 // Make sure pref is updated
[email protected]695b5712012-12-06 23:55:283124 loop_.RunUntilIdle();
[email protected]4ee07c62012-08-21 12:40:423125
[email protected]695b5712012-12-06 23:55:283126 // The good_crx is blacklisted and the whitelist doesn't negate it.
[email protected]4ee07c62012-08-21 12:40:423127 ValidateBooleanPref(good_crx, "blacklist", true);
3128 EXPECT_EQ(0u, service_->extensions()->size());
3129}
3130
[email protected]6b75ec32009-08-14 06:37:183131// Test loading extensions from the profile directory, except
3132// blacklisted ones.
[email protected]d9a61e12012-11-14 02:43:473133TEST_F(ExtensionServiceTest, WillNotLoadBlacklistedExtensionsFromDirectory) {
[email protected]6b75ec32009-08-14 06:37:183134 // Initialize the test dir with a good Preferences/extensions.
[email protected]650b2d52013-02-10 03:41:453135 base::FilePath source_install_dir = data_dir_
[email protected]6b75ec32009-08-14 06:37:183136 .AppendASCII("good")
3137 .AppendASCII("Extensions");
[email protected]650b2d52013-02-10 03:41:453138 base::FilePath pref_path = source_install_dir
[email protected]6b75ec32009-08-14 06:37:183139 .DirName()
3140 .AppendASCII("Preferences");
[email protected]eaa7dd182010-12-14 11:09:003141 InitializeInstalledExtensionService(pref_path, source_install_dir);
[email protected]6b75ec32009-08-14 06:37:183142
[email protected]e50013c32010-08-18 21:05:243143 // Blacklist good1.
[email protected]6b75ec32009-08-14 06:37:183144 std::vector<std::string> blacklist;
[email protected]e50013c32010-08-18 21:05:243145 blacklist.push_back(good1);
[email protected]fdd679b2012-11-15 20:49:393146 ExtensionSystem::Get(profile_.get())->blacklist()->SetFromUpdater(blacklist,
3147 "v1");
3148
[email protected]6b75ec32009-08-14 06:37:183149 // Make sure pref is updated
[email protected]b8f50ce2012-11-17 12:37:573150 loop_.RunUntilIdle();
[email protected]6b75ec32009-08-14 06:37:183151
[email protected]e50013c32010-08-18 21:05:243152 ValidateBooleanPref(good1, "blacklist", true);
[email protected]6b75ec32009-08-14 06:37:183153
3154 // Load extensions.
3155 service_->Init();
[email protected]6b75ec32009-08-14 06:37:183156
[email protected]fc670822011-12-17 09:33:493157 std::vector<string16> errors = GetErrors();
3158 for (std::vector<string16>::iterator err = errors.begin();
[email protected]6b75ec32009-08-14 06:37:183159 err != errors.end(); ++err) {
3160 LOG(ERROR) << *err;
3161 }
[email protected]d7b36dc2009-10-29 21:47:403162 ASSERT_EQ(2u, loaded_.size());
[email protected]6b75ec32009-08-14 06:37:183163
[email protected]695b5712012-12-06 23:55:283164 EXPECT_TRUE(service_->GetInstalledExtension(good1));
3165 int include_mask = ExtensionService::INCLUDE_EVERYTHING &
3166 ~ExtensionService::INCLUDE_BLACKLISTED;
3167 EXPECT_FALSE(service_->GetExtensionById(good1, include_mask));
[email protected]6b75ec32009-08-14 06:37:183168}
3169
[email protected]306a2bd2010-08-11 14:56:363170// Will not install extension blacklisted by policy.
[email protected]d9a61e12012-11-14 02:43:473171TEST_F(ExtensionServiceTest, BlacklistedByPolicyWillNotInstall) {
[email protected]eaa7dd182010-12-14 11:09:003172 InitializeEmptyExtensionService();
[email protected]306a2bd2010-08-11 14:56:363173
[email protected]306a2bd2010-08-11 14:56:363174 // Blacklist everything.
[email protected]43d3bf82011-04-11 07:46:583175 {
3176 ListPrefUpdate update(profile_->GetPrefs(),
3177 prefs::kExtensionInstallDenyList);
3178 ListValue* blacklist = update.Get();
3179 blacklist->Append(Value::CreateStringValue("*"));
3180 }
[email protected]306a2bd2010-08-11 14:56:363181
3182 // Blacklist prevents us from installing good_crx.
[email protected]650b2d52013-02-10 03:41:453183 base::FilePath path = data_dir_.AppendASCII("good.crx");
[email protected]8f512c72011-11-22 21:02:503184 InstallCRX(path, INSTALL_FAILED);
[email protected]306a2bd2010-08-11 14:56:363185 EXPECT_EQ(0u, service_->extensions()->size());
3186
3187 // Now whitelist this particular extension.
[email protected]43d3bf82011-04-11 07:46:583188 {
3189 ListPrefUpdate update(profile_->GetPrefs(),
3190 prefs::kExtensionInstallAllowList);
3191 ListValue* whitelist = update.Get();
3192 whitelist->Append(Value::CreateStringValue(good_crx));
3193 }
3194
[email protected]306a2bd2010-08-11 14:56:363195 // Ensure we can now install good_crx.
[email protected]8f512c72011-11-22 21:02:503196 InstallCRX(path, INSTALL_NEW);
[email protected]306a2bd2010-08-11 14:56:363197 EXPECT_EQ(1u, service_->extensions()->size());
3198}
3199
[email protected]aa96d3a2010-08-21 08:45:253200// Extension blacklisted by policy get unloaded after installing.
[email protected]d9a61e12012-11-14 02:43:473201TEST_F(ExtensionServiceTest, BlacklistedByPolicyRemovedIfRunning) {
[email protected]eaa7dd182010-12-14 11:09:003202 InitializeEmptyExtensionService();
[email protected]aa96d3a2010-08-21 08:45:253203
3204 // Install good_crx.
[email protected]650b2d52013-02-10 03:41:453205 base::FilePath path = data_dir_.AppendASCII("good.crx");
[email protected]8f512c72011-11-22 21:02:503206 InstallCRX(path, INSTALL_NEW);
[email protected]aa96d3a2010-08-21 08:45:253207 EXPECT_EQ(1u, service_->extensions()->size());
3208
[email protected]acd78969c2010-12-08 09:49:113209 { // Scope for pref update notification.
3210 PrefService* prefs = profile_->GetPrefs();
[email protected]43d3bf82011-04-11 07:46:583211 ListPrefUpdate update(prefs, prefs::kExtensionInstallDenyList);
3212 ListValue* blacklist = update.Get();
[email protected]acd78969c2010-12-08 09:49:113213 ASSERT_TRUE(blacklist != NULL);
[email protected]aa96d3a2010-08-21 08:45:253214
[email protected]acd78969c2010-12-08 09:49:113215 // Blacklist this extension.
3216 blacklist->Append(Value::CreateStringValue(good_crx));
[email protected]acd78969c2010-12-08 09:49:113217 }
[email protected]aa96d3a2010-08-21 08:45:253218
3219 // Extension should not be running now.
[email protected]b8f50ce2012-11-17 12:37:573220 loop_.RunUntilIdle();
[email protected]aa96d3a2010-08-21 08:45:253221 EXPECT_EQ(0u, service_->extensions()->size());
3222}
3223
[email protected]05aad2da2011-10-28 10:12:373224// Tests that component extensions are not blacklisted by policy.
[email protected]d9a61e12012-11-14 02:43:473225TEST_F(ExtensionServiceTest, ComponentExtensionWhitelisted) {
[email protected]05aad2da2011-10-28 10:12:373226 InitializeEmptyExtensionService();
3227
3228 // Blacklist everything.
3229 {
3230 ListPrefUpdate update(profile_->GetPrefs(),
3231 prefs::kExtensionInstallDenyList);
3232 ListValue* blacklist = update.Get();
3233 blacklist->Append(Value::CreateStringValue("*"));
3234 }
3235
3236 // Install a component extension.
[email protected]650b2d52013-02-10 03:41:453237 base::FilePath path = data_dir_
[email protected]05aad2da2011-10-28 10:12:373238 .AppendASCII("good")
3239 .AppendASCII("Extensions")
3240 .AppendASCII(good0)
3241 .AppendASCII("1.0.0.0");
3242 std::string manifest;
3243 ASSERT_TRUE(file_util::ReadFileToString(
[email protected]993da5e2013-03-23 21:25:163244 path.Append(extensions::kManifestFilename), &manifest));
[email protected]d8c8f25f2011-11-02 18:18:013245 service_->component_loader()->Add(manifest, path);
[email protected]05aad2da2011-10-28 10:12:373246 service_->Init();
3247
3248 // Extension should be installed despite blacklist.
3249 ASSERT_EQ(1u, service_->extensions()->size());
[email protected]8f512c72011-11-22 21:02:503250 EXPECT_TRUE(service_->GetExtensionById(good0, false));
[email protected]05aad2da2011-10-28 10:12:373251
3252 // Poke external providers and make sure the extension is still present.
3253 service_->CheckForExternalUpdates();
3254 ASSERT_EQ(1u, service_->extensions()->size());
[email protected]8f512c72011-11-22 21:02:503255 EXPECT_TRUE(service_->GetExtensionById(good0, false));
[email protected]05aad2da2011-10-28 10:12:373256
3257 // Extension should not be uninstalled on blacklist changes.
3258 {
3259 ListPrefUpdate update(profile_->GetPrefs(),
3260 prefs::kExtensionInstallDenyList);
3261 ListValue* blacklist = update.Get();
3262 blacklist->Append(Value::CreateStringValue(good0));
3263 }
[email protected]b8f50ce2012-11-17 12:37:573264 loop_.RunUntilIdle();
[email protected]05aad2da2011-10-28 10:12:373265 ASSERT_EQ(1u, service_->extensions()->size());
[email protected]8f512c72011-11-22 21:02:503266 EXPECT_TRUE(service_->GetExtensionById(good0, false));
[email protected]05aad2da2011-10-28 10:12:373267}
3268
3269// Tests that policy-installed extensions are not blacklisted by policy.
[email protected]d9a61e12012-11-14 02:43:473270TEST_F(ExtensionServiceTest, PolicyInstalledExtensionsWhitelisted) {
[email protected]05aad2da2011-10-28 10:12:373271 InitializeEmptyExtensionService();
3272
[email protected]05aad2da2011-10-28 10:12:373273 {
[email protected]e410b5f2012-12-14 14:02:243274 // Blacklist everything.
3275 ListPrefUpdate blacklist_update(profile_->GetPrefs(),
3276 prefs::kExtensionInstallDenyList);
3277 ListValue* blacklist = blacklist_update.Get();
3278 blacklist->AppendString("*");
3279
3280 // Mark good.crx for force-installation.
3281 DictionaryPrefUpdate forcelist_update(profile_->GetPrefs(),
3282 prefs::kExtensionInstallForceList);
3283 extensions::ExternalPolicyLoader::AddExtension(
3284 forcelist_update.Get(), good_crx, "http://example.com/update_url");
[email protected]05aad2da2011-10-28 10:12:373285 }
3286
3287 // Have policy force-install an extension.
3288 MockExtensionProvider* provider =
3289 new MockExtensionProvider(service_,
[email protected]1d5e58b2013-01-31 08:41:403290 Manifest::EXTERNAL_POLICY_DOWNLOAD);
[email protected]05aad2da2011-10-28 10:12:373291 AddMockExternalProvider(provider);
3292 provider->UpdateOrAddExtension(good_crx, "1.0.0.0",
3293 data_dir_.AppendASCII("good.crx"));
3294
3295 // Reloading extensions should find our externally registered extension
3296 // and install it.
3297 service_->CheckForExternalUpdates();
[email protected]b8f50ce2012-11-17 12:37:573298 loop_.RunUntilIdle();
[email protected]05aad2da2011-10-28 10:12:373299
3300 // Extension should be installed despite blacklist.
3301 ASSERT_EQ(1u, service_->extensions()->size());
[email protected]8f512c72011-11-22 21:02:503302 EXPECT_TRUE(service_->GetExtensionById(good_crx, false));
[email protected]05aad2da2011-10-28 10:12:373303
3304 // Blacklist update should not uninstall the extension.
3305 {
3306 ListPrefUpdate update(profile_->GetPrefs(),
3307 prefs::kExtensionInstallDenyList);
3308 ListValue* blacklist = update.Get();
3309 blacklist->Append(Value::CreateStringValue(good0));
3310 }
[email protected]b8f50ce2012-11-17 12:37:573311 loop_.RunUntilIdle();
[email protected]05aad2da2011-10-28 10:12:373312 ASSERT_EQ(1u, service_->extensions()->size());
[email protected]8f512c72011-11-22 21:02:503313 EXPECT_TRUE(service_->GetExtensionById(good_crx, false));
[email protected]05aad2da2011-10-28 10:12:373314}
3315
[email protected]65187152012-06-02 13:14:143316// Tests that extensions cannot be installed if the policy provider prohibits
3317// it. This functionality is implemented in CrxInstaller::ConfirmInstall().
[email protected]d9a61e12012-11-14 02:43:473318TEST_F(ExtensionServiceTest, ManagementPolicyProhibitsInstall) {
[email protected]65187152012-06-02 13:14:143319 InitializeEmptyExtensionService();
3320
3321 management_policy_->UnregisterAllProviders();
3322 extensions::TestManagementPolicyProvider provider_(
3323 extensions::TestManagementPolicyProvider::PROHIBIT_LOAD);
3324 management_policy_->RegisterProvider(&provider_);
3325
3326 InstallCRX(data_dir_.AppendASCII("good.crx"), INSTALL_FAILED);
3327 EXPECT_EQ(0u, service_->extensions()->size());
3328}
3329
3330// Tests that extensions cannot be loaded from prefs if the policy provider
3331// prohibits it. This functionality is implemented in InstalledLoader::Load().
[email protected]d9a61e12012-11-14 02:43:473332TEST_F(ExtensionServiceTest, ManagementPolicyProhibitsLoadFromPrefs) {
[email protected]65187152012-06-02 13:14:143333 InitializeEmptyExtensionService();
3334
3335 // Create a fake extension to be loaded as though it were read from prefs.
[email protected]650b2d52013-02-10 03:41:453336 base::FilePath path = data_dir_.AppendASCII("management")
[email protected]65187152012-06-02 13:14:143337 .AppendASCII("simple_extension");
3338 DictionaryValue manifest;
3339 manifest.SetString(keys::kName, "simple_extension");
3340 manifest.SetString(keys::kVersion, "1");
[email protected]12075d12013-02-27 05:38:053341 // UNPACKED is for extensions loaded from a directory. We use it here, even
[email protected]65187152012-06-02 13:14:143342 // though we're testing loading from prefs, so that we don't need to provide
3343 // an extension key.
[email protected]007b3f82013-04-09 08:46:453344 extensions::ExtensionInfo extension_info(
3345 &manifest, std::string(), path, Manifest::UNPACKED);
[email protected]65187152012-06-02 13:14:143346
3347 // Ensure we can load it with no management policy in place.
3348 management_policy_->UnregisterAllProviders();
3349 EXPECT_EQ(0u, service_->extensions()->size());
3350 extensions::InstalledLoader(service_).Load(extension_info, false);
3351 EXPECT_EQ(1u, service_->extensions()->size());
3352
3353 const Extension* extension = *(service_->extensions()->begin());
3354 EXPECT_TRUE(service_->UninstallExtension(extension->id(), false, NULL));
3355 EXPECT_EQ(0u, service_->extensions()->size());
3356
3357 // Ensure we cannot load it if management policy prohibits installation.
3358 extensions::TestManagementPolicyProvider provider_(
3359 extensions::TestManagementPolicyProvider::PROHIBIT_LOAD);
3360 management_policy_->RegisterProvider(&provider_);
3361
3362 extensions::InstalledLoader(service_).Load(extension_info, false);
3363 EXPECT_EQ(0u, service_->extensions()->size());
3364}
3365
3366// Tests disabling an extension when prohibited by the ManagementPolicy.
[email protected]d9a61e12012-11-14 02:43:473367TEST_F(ExtensionServiceTest, ManagementPolicyProhibitsDisable) {
[email protected]65187152012-06-02 13:14:143368 InitializeEmptyExtensionService();
3369
3370 InstallCRX(data_dir_.AppendASCII("good.crx"), INSTALL_NEW);
3371 EXPECT_EQ(1u, service_->extensions()->size());
3372 EXPECT_EQ(0u, service_->disabled_extensions()->size());
3373
3374 management_policy_->UnregisterAllProviders();
3375 extensions::TestManagementPolicyProvider provider(
3376 extensions::TestManagementPolicyProvider::PROHIBIT_MODIFY_STATUS);
3377 management_policy_->RegisterProvider(&provider);
3378
3379 // Attempt to disable it.
3380 service_->DisableExtension(good_crx, Extension::DISABLE_USER_ACTION);
3381
3382 EXPECT_EQ(1u, service_->extensions()->size());
3383 EXPECT_TRUE(service_->GetExtensionById(good_crx, false));
3384 EXPECT_EQ(0u, service_->disabled_extensions()->size());
3385}
3386
3387// Tests uninstalling an extension when prohibited by the ManagementPolicy.
[email protected]d9a61e12012-11-14 02:43:473388TEST_F(ExtensionServiceTest, ManagementPolicyProhibitsUninstall) {
[email protected]65187152012-06-02 13:14:143389 InitializeEmptyExtensionService();
3390
3391 InstallCRX(data_dir_.AppendASCII("good.crx"), INSTALL_NEW);
3392 EXPECT_EQ(1u, service_->extensions()->size());
3393 EXPECT_EQ(0u, service_->disabled_extensions()->size());
3394
3395 management_policy_->UnregisterAllProviders();
3396 extensions::TestManagementPolicyProvider provider(
3397 extensions::TestManagementPolicyProvider::PROHIBIT_MODIFY_STATUS);
3398 management_policy_->RegisterProvider(&provider);
3399
3400 // Attempt to uninstall it.
3401 EXPECT_FALSE(service_->UninstallExtension(good_crx, false, NULL));
3402
3403 EXPECT_EQ(1u, service_->extensions()->size());
3404 EXPECT_TRUE(service_->GetExtensionById(good_crx, false));
3405}
3406
3407// Tests that previously installed extensions that are now prohibited from
3408// being installed are removed.
[email protected]d9a61e12012-11-14 02:43:473409TEST_F(ExtensionServiceTest, ManagementPolicyUnloadsAllProhibited) {
[email protected]65187152012-06-02 13:14:143410 InitializeEmptyExtensionService();
3411
3412 InstallCRX(data_dir_.AppendASCII("good.crx"), INSTALL_NEW);
3413 InstallCRX(data_dir_.AppendASCII("page_action.crx"), INSTALL_NEW);
3414 EXPECT_EQ(2u, service_->extensions()->size());
3415 EXPECT_EQ(0u, service_->disabled_extensions()->size());
3416
3417 management_policy_->UnregisterAllProviders();
3418 extensions::TestManagementPolicyProvider provider(
3419 extensions::TestManagementPolicyProvider::PROHIBIT_LOAD);
3420 management_policy_->RegisterProvider(&provider);
3421
3422 // Run the policy check.
[email protected]4ee07c62012-08-21 12:40:423423 service_->CheckManagementPolicy();
[email protected]65187152012-06-02 13:14:143424 EXPECT_EQ(0u, service_->extensions()->size());
3425 EXPECT_EQ(0u, service_->disabled_extensions()->size());
3426}
3427
3428// Tests that previously disabled extensions that are now required to be
3429// enabled are re-enabled on reinstall.
[email protected]d9a61e12012-11-14 02:43:473430TEST_F(ExtensionServiceTest, ManagementPolicyRequiresEnable) {
[email protected]65187152012-06-02 13:14:143431 InitializeEmptyExtensionService();
3432
3433 // Install, then disable, an extension.
3434 InstallCRX(data_dir_.AppendASCII("good.crx"), INSTALL_NEW);
3435 EXPECT_EQ(1u, service_->extensions()->size());
3436 service_->DisableExtension(good_crx, Extension::DISABLE_USER_ACTION);
3437 EXPECT_EQ(1u, service_->disabled_extensions()->size());
3438
3439 // Register an ExtensionMnagementPolicy that requires the extension to remain
3440 // enabled.
3441 management_policy_->UnregisterAllProviders();
3442 extensions::TestManagementPolicyProvider provider(
3443 extensions::TestManagementPolicyProvider::MUST_REMAIN_ENABLED);
3444 management_policy_->RegisterProvider(&provider);
3445
3446 // Reinstall the extension.
3447 InstallCRX(data_dir_.AppendASCII("good.crx"), INSTALL_UPDATED);
3448 EXPECT_EQ(1u, service_->extensions()->size());
3449 EXPECT_EQ(0u, service_->disabled_extensions()->size());
3450}
3451
[email protected]d9a61e12012-11-14 02:43:473452TEST_F(ExtensionServiceTest, ExternalExtensionAutoAcknowledgement) {
[email protected]a39921b42012-02-28 03:42:543453 InitializeEmptyExtensionService();
3454 set_extensions_enabled(true);
3455
3456 {
3457 // Register and install an external extension.
3458 MockExtensionProvider* provider =
[email protected]1d5e58b2013-01-31 08:41:403459 new MockExtensionProvider(service_, Manifest::EXTERNAL_PREF);
[email protected]a39921b42012-02-28 03:42:543460 AddMockExternalProvider(provider);
3461 provider->UpdateOrAddExtension(good_crx, "1.0.0.0",
3462 data_dir_.AppendASCII("good.crx"));
3463 }
3464 {
3465 // Have policy force-install an extension.
3466 MockExtensionProvider* provider =
3467 new MockExtensionProvider(service_,
[email protected]1d5e58b2013-01-31 08:41:403468 Manifest::EXTERNAL_POLICY_DOWNLOAD);
[email protected]a39921b42012-02-28 03:42:543469 AddMockExternalProvider(provider);
3470 provider->UpdateOrAddExtension(page_action, "1.0.0.0",
3471 data_dir_.AppendASCII("page_action.crx"));
3472 }
3473
3474 // Providers are set up. Let them run.
3475 service_->CheckForExternalUpdates();
[email protected]b8f50ce2012-11-17 12:37:573476 loop_.RunUntilIdle();
[email protected]a39921b42012-02-28 03:42:543477
3478 ASSERT_EQ(2u, service_->extensions()->size());
3479 EXPECT_TRUE(service_->GetExtensionById(good_crx, false));
3480 EXPECT_TRUE(service_->GetExtensionById(page_action, false));
3481 ExtensionPrefs* prefs = service_->extension_prefs();
3482 ASSERT_TRUE(!prefs->IsExternalExtensionAcknowledged(good_crx));
3483 ASSERT_TRUE(prefs->IsExternalExtensionAcknowledged(page_action));
3484}
3485
[email protected]a7cd28e2012-10-05 21:03:363486#if !defined(OS_CHROMEOS)
3487// This tests if default apps are installed correctly.
[email protected]d9a61e12012-11-14 02:43:473488TEST_F(ExtensionServiceTest, DefaultAppsInstall) {
[email protected]a7cd28e2012-10-05 21:03:363489 InitializeEmptyExtensionService();
3490 InitializeRequestContext();
3491 set_extensions_enabled(true);
3492
3493 {
3494 std::string json_data =
3495 "{"
3496 " \"ldnnhddmnhbkjipkidpdiheffobcpfmf\" : {"
3497 " \"external_crx\": \"good.crx\","
3498 " \"external_version\": \"1.0.0.0\","
3499 " \"is_bookmark_app\": false"
3500 " }"
3501 "}";
3502 default_apps::Provider* provider =
3503 new default_apps::Provider(
3504 profile_.get(),
3505 service_,
3506 new extensions::ExternalTestingLoader(json_data, data_dir_),
[email protected]1d5e58b2013-01-31 08:41:403507 Manifest::INTERNAL,
3508 Manifest::INVALID_LOCATION,
[email protected]a7cd28e2012-10-05 21:03:363509 Extension::FROM_WEBSTORE | Extension::WAS_INSTALLED_BY_DEFAULT);
3510
3511 AddMockExternalProvider(provider);
3512 }
3513
3514 ASSERT_EQ(0u, service_->extensions()->size());
3515 service_->CheckForExternalUpdates();
[email protected]b8f50ce2012-11-17 12:37:573516 loop_.RunUntilIdle();
[email protected]a7cd28e2012-10-05 21:03:363517
3518 ASSERT_EQ(1u, service_->extensions()->size());
3519 EXPECT_TRUE(service_->GetExtensionById(good_crx, false));
3520 const Extension* extension = service_->GetExtensionById(good_crx, false);
3521 EXPECT_TRUE(extension->from_webstore());
3522 EXPECT_TRUE(extension->was_installed_by_default());
3523
3524}
3525#endif
3526
[email protected]cd500f72010-06-25 23:44:323527// Tests disabling extensions
[email protected]d9a61e12012-11-14 02:43:473528TEST_F(ExtensionServiceTest, DisableExtension) {
[email protected]eaa7dd182010-12-14 11:09:003529 InitializeEmptyExtensionService();
[email protected]cd500f72010-06-25 23:44:323530
[email protected]8f512c72011-11-22 21:02:503531 InstallCRX(data_dir_.AppendASCII("good.crx"), INSTALL_NEW);
[email protected]84df8332011-12-06 18:22:463532 EXPECT_FALSE(service_->extensions()->is_empty());
[email protected]8f512c72011-11-22 21:02:503533 EXPECT_TRUE(service_->GetExtensionById(good_crx, true));
3534 EXPECT_TRUE(service_->GetExtensionById(good_crx, false));
[email protected]84df8332011-12-06 18:22:463535 EXPECT_TRUE(service_->disabled_extensions()->is_empty());
[email protected]cd500f72010-06-25 23:44:323536
3537 // Disable it.
[email protected]44d62b62012-04-11 00:06:033538 service_->DisableExtension(good_crx, Extension::DISABLE_USER_ACTION);
[email protected]cd500f72010-06-25 23:44:323539
[email protected]84df8332011-12-06 18:22:463540 EXPECT_TRUE(service_->extensions()->is_empty());
[email protected]8f512c72011-11-22 21:02:503541 EXPECT_TRUE(service_->GetExtensionById(good_crx, true));
3542 EXPECT_FALSE(service_->GetExtensionById(good_crx, false));
[email protected]84df8332011-12-06 18:22:463543 EXPECT_FALSE(service_->disabled_extensions()->is_empty());
[email protected]fa2416f2011-05-03 08:41:203544}
3545
[email protected]d9a61e12012-11-14 02:43:473546TEST_F(ExtensionServiceTest, DisableTerminatedExtension) {
[email protected]fa2416f2011-05-03 08:41:203547 InitializeEmptyExtensionService();
3548
[email protected]8f512c72011-11-22 21:02:503549 InstallCRX(data_dir_.AppendASCII("good.crx"), INSTALL_NEW);
[email protected]fa2416f2011-05-03 08:41:203550 TerminateExtension(good_crx);
3551 EXPECT_TRUE(service_->GetTerminatedExtension(good_crx));
3552
3553 // Disable it.
[email protected]44d62b62012-04-11 00:06:033554 service_->DisableExtension(good_crx, Extension::DISABLE_USER_ACTION);
[email protected]fa2416f2011-05-03 08:41:203555
3556 EXPECT_FALSE(service_->GetTerminatedExtension(good_crx));
[email protected]8f512c72011-11-22 21:02:503557 EXPECT_TRUE(service_->GetExtensionById(good_crx, true));
[email protected]84df8332011-12-06 18:22:463558 EXPECT_FALSE(service_->disabled_extensions()->is_empty());
[email protected]cd500f72010-06-25 23:44:323559}
3560
[email protected]d728e002010-12-08 04:46:233561// Tests disabling all extensions (simulating --disable-extensions flag).
[email protected]d9a61e12012-11-14 02:43:473562TEST_F(ExtensionServiceTest, DisableAllExtensions) {
[email protected]eaa7dd182010-12-14 11:09:003563 InitializeEmptyExtensionService();
[email protected]d728e002010-12-08 04:46:233564
[email protected]650b2d52013-02-10 03:41:453565 base::FilePath path = data_dir_.AppendASCII("good.crx");
[email protected]8f512c72011-11-22 21:02:503566 InstallCRX(path, INSTALL_NEW);
[email protected]d728e002010-12-08 04:46:233567
3568 EXPECT_EQ(1u, service_->extensions()->size());
3569 EXPECT_EQ(0u, service_->disabled_extensions()->size());
3570
3571 // Disable extensions.
3572 service_->set_extensions_enabled(false);
3573 service_->ReloadExtensions();
3574
3575 // There shouldn't be extensions in either list.
3576 EXPECT_EQ(0u, service_->extensions()->size());
3577 EXPECT_EQ(0u, service_->disabled_extensions()->size());
3578
3579 // This shouldn't do anything when all extensions are disabled.
3580 service_->EnableExtension(good_crx);
3581 service_->ReloadExtensions();
3582
3583 // There still shouldn't be extensions in either list.
3584 EXPECT_EQ(0u, service_->extensions()->size());
3585 EXPECT_EQ(0u, service_->disabled_extensions()->size());
3586
3587 // And then re-enable the extensions.
3588 service_->set_extensions_enabled(true);
3589 service_->ReloadExtensions();
3590
3591 EXPECT_EQ(1u, service_->extensions()->size());
3592 EXPECT_EQ(0u, service_->disabled_extensions()->size());
3593}
3594
[email protected]8f512c72011-11-22 21:02:503595// Tests reloading extensions.
[email protected]d9a61e12012-11-14 02:43:473596TEST_F(ExtensionServiceTest, ReloadExtensions) {
[email protected]eaa7dd182010-12-14 11:09:003597 InitializeEmptyExtensionService();
[email protected]26367b62012-10-04 23:03:323598 InitializeRequestContext();
[email protected]cd500f72010-06-25 23:44:323599
3600 // Simple extension that should install without error.
[email protected]650b2d52013-02-10 03:41:453601 base::FilePath path = data_dir_.AppendASCII("good.crx");
[email protected]26367b62012-10-04 23:03:323602 InstallCRX(path, INSTALL_NEW,
3603 Extension::FROM_WEBSTORE | Extension::WAS_INSTALLED_BY_DEFAULT);
[email protected]cd500f72010-06-25 23:44:323604 const char* extension_id = good_crx;
[email protected]44d62b62012-04-11 00:06:033605 service_->DisableExtension(extension_id, Extension::DISABLE_USER_ACTION);
[email protected]cd500f72010-06-25 23:44:323606
3607 EXPECT_EQ(0u, service_->extensions()->size());
3608 EXPECT_EQ(1u, service_->disabled_extensions()->size());
3609
3610 service_->ReloadExtensions();
3611
[email protected]26367b62012-10-04 23:03:323612 // The creation flags should not change when reloading the extension.
3613 const Extension* extension = service_->GetExtensionById(good_crx, true);
3614 EXPECT_TRUE(extension->from_webstore());
3615 EXPECT_TRUE(extension->was_installed_by_default());
3616 EXPECT_FALSE(extension->from_bookmark());
3617
[email protected]cd500f72010-06-25 23:44:323618 // Extension counts shouldn't change.
3619 EXPECT_EQ(0u, service_->extensions()->size());
3620 EXPECT_EQ(1u, service_->disabled_extensions()->size());
3621
3622 service_->EnableExtension(extension_id);
3623
3624 EXPECT_EQ(1u, service_->extensions()->size());
3625 EXPECT_EQ(0u, service_->disabled_extensions()->size());
3626
[email protected]5a73f902010-06-30 02:29:413627 // Need to clear |loaded_| manually before reloading as the
3628 // EnableExtension() call above inserted into it and
3629 // UnloadAllExtensions() doesn't send out notifications.
3630 loaded_.clear();
[email protected]cd500f72010-06-25 23:44:323631 service_->ReloadExtensions();
3632
3633 // Extension counts shouldn't change.
3634 EXPECT_EQ(1u, service_->extensions()->size());
3635 EXPECT_EQ(0u, service_->disabled_extensions()->size());
3636}
3637
[email protected]2a947202013-03-06 04:58:053638// Tests reloading an extension.
3639TEST_F(ExtensionServiceTest, ReloadExtension) {
3640 InitializeEmptyExtensionService();
3641 InitializeExtensionProcessManager();
3642
3643 // Simple extension that should install without error.
3644 const char* extension_id = "behllobkkfkfnphdnhnkndlbkcpglgmj";
3645 base::FilePath ext = data_dir_
3646 .AppendASCII("good")
3647 .AppendASCII("Extensions")
3648 .AppendASCII(extension_id)
3649 .AppendASCII("1.0.0.0");
3650 extensions::UnpackedInstaller::Create(service_)->Load(ext);
3651 loop_.RunUntilIdle();
3652
3653 EXPECT_EQ(1u, service_->extensions()->size());
3654 EXPECT_EQ(0u, service_->disabled_extensions()->size());
3655
3656 service_->ReloadExtension(extension_id);
3657
3658 // Extension should be disabled now, waiting to be reloaded.
3659 EXPECT_EQ(0u, service_->extensions()->size());
3660 EXPECT_EQ(1u, service_->disabled_extensions()->size());
3661 EXPECT_EQ(Extension::DISABLE_RELOAD,
3662 service_->extension_prefs()->GetDisableReasons(extension_id));
3663
[email protected]a5768512013-04-12 19:35:353664 // Reloading again should not crash.
[email protected]2a947202013-03-06 04:58:053665 service_->ReloadExtension(extension_id);
3666
3667 // Finish reloading
3668 loop_.RunUntilIdle();
3669
3670 // Extension should be enabled again.
3671 EXPECT_EQ(1u, service_->extensions()->size());
3672 EXPECT_EQ(0u, service_->disabled_extensions()->size());
3673}
3674
[email protected]d9a61e12012-11-14 02:43:473675TEST_F(ExtensionServiceTest, UninstallExtension) {
[email protected]eaa7dd182010-12-14 11:09:003676 InitializeEmptyExtensionService();
[email protected]8f512c72011-11-22 21:02:503677 InstallCRX(data_dir_.AppendASCII("good.crx"), INSTALL_NEW);
[email protected]65187152012-06-02 13:14:143678 EXPECT_EQ(1u, service_->extensions()->size());
[email protected]fa2416f2011-05-03 08:41:203679 UninstallExtension(good_crx, false);
[email protected]65187152012-06-02 13:14:143680 EXPECT_EQ(0u, service_->extensions()->size());
[email protected]fa2416f2011-05-03 08:41:203681}
[email protected]631cf822009-05-15 07:01:253682
[email protected]d9a61e12012-11-14 02:43:473683TEST_F(ExtensionServiceTest, UninstallTerminatedExtension) {
[email protected]fa2416f2011-05-03 08:41:203684 InitializeEmptyExtensionService();
[email protected]8f512c72011-11-22 21:02:503685 InstallCRX(data_dir_.AppendASCII("good.crx"), INSTALL_NEW);
[email protected]fa2416f2011-05-03 08:41:203686 TerminateExtension(good_crx);
3687 UninstallExtension(good_crx, false);
[email protected]631cf822009-05-15 07:01:253688}
3689
[email protected]6aeac8342010-10-01 20:21:183690// Tests the uninstaller helper.
[email protected]d9a61e12012-11-14 02:43:473691TEST_F(ExtensionServiceTest, UninstallExtensionHelper) {
[email protected]eaa7dd182010-12-14 11:09:003692 InitializeEmptyExtensionService();
[email protected]8f512c72011-11-22 21:02:503693 InstallCRX(data_dir_.AppendASCII("good.crx"), INSTALL_NEW);
[email protected]fa2416f2011-05-03 08:41:203694 UninstallExtension(good_crx, true);
3695}
[email protected]6aeac8342010-10-01 20:21:183696
[email protected]d9a61e12012-11-14 02:43:473697TEST_F(ExtensionServiceTest, UninstallExtensionHelperTerminated) {
[email protected]fa2416f2011-05-03 08:41:203698 InitializeEmptyExtensionService();
[email protected]8f512c72011-11-22 21:02:503699 InstallCRX(data_dir_.AppendASCII("good.crx"), INSTALL_NEW);
[email protected]fa2416f2011-05-03 08:41:203700 TerminateExtension(good_crx);
3701 UninstallExtension(good_crx, true);
[email protected]6aeac8342010-10-01 20:21:183702}
3703
[email protected]98270432012-09-11 20:51:243704// An extension disabled because of unsupported requirements should re-enabled
3705// if updated to a version with supported requirements as long as there are no
3706// other disable reasons.
[email protected]d9a61e12012-11-14 02:43:473707TEST_F(ExtensionServiceTest, UpgradingRequirementsEnabled) {
[email protected]98270432012-09-11 20:51:243708 InitializeEmptyExtensionService();
3709 BlackListWebGL();
3710
[email protected]650b2d52013-02-10 03:41:453711 base::FilePath path = data_dir_.AppendASCII("requirements");
3712 base::FilePath pem_path = data_dir_.AppendASCII("requirements")
[email protected]98270432012-09-11 20:51:243713 .AppendASCII("v1_good.pem");
3714 const Extension* extension_v1 = PackAndInstallCRX(path.AppendASCII("v1_good"),
3715 pem_path,
3716 INSTALL_NEW);
3717 std::string id = extension_v1->id();
3718 EXPECT_TRUE(service_->IsExtensionEnabled(id));
3719
[email protected]650b2d52013-02-10 03:41:453720 base::FilePath v2_bad_requirements_crx = GetTemporaryFile();
[email protected]a17dfcf2012-12-30 02:07:093721
3722 PackCRX(path.AppendASCII("v2_bad_requirements"),
3723 pem_path,
3724 v2_bad_requirements_crx);
3725 UpdateExtension(id, v2_bad_requirements_crx, INSTALLED);
[email protected]98270432012-09-11 20:51:243726 EXPECT_FALSE(service_->IsExtensionEnabled(id));
3727
[email protected]650b2d52013-02-10 03:41:453728 base::FilePath v3_good_crx = GetTemporaryFile();
[email protected]a17dfcf2012-12-30 02:07:093729
3730 PackCRX(path.AppendASCII("v3_good"), pem_path, v3_good_crx);
3731 UpdateExtension(id, v3_good_crx, ENABLED);
[email protected]98270432012-09-11 20:51:243732 EXPECT_TRUE(service_->IsExtensionEnabled(id));
3733}
3734
3735// Extensions disabled through user action should stay disabled.
[email protected]d9a61e12012-11-14 02:43:473736TEST_F(ExtensionServiceTest, UpgradingRequirementsDisabled) {
[email protected]98270432012-09-11 20:51:243737 InitializeEmptyExtensionService();
3738 BlackListWebGL();
3739
[email protected]650b2d52013-02-10 03:41:453740 base::FilePath path = data_dir_.AppendASCII("requirements");
3741 base::FilePath pem_path = data_dir_.AppendASCII("requirements")
[email protected]98270432012-09-11 20:51:243742 .AppendASCII("v1_good.pem");
3743 const Extension* extension_v1 = PackAndInstallCRX(path.AppendASCII("v1_good"),
3744 pem_path,
3745 INSTALL_NEW);
3746 std::string id = extension_v1->id();
3747 service_->DisableExtension(id, Extension::DISABLE_USER_ACTION);
3748 EXPECT_FALSE(service_->IsExtensionEnabled(id));
3749
[email protected]650b2d52013-02-10 03:41:453750 base::FilePath v2_bad_requirements_crx = GetTemporaryFile();
[email protected]a17dfcf2012-12-30 02:07:093751
3752 PackCRX(path.AppendASCII("v2_bad_requirements"),
3753 pem_path,
3754 v2_bad_requirements_crx);
3755 UpdateExtension(id, v2_bad_requirements_crx, INSTALLED);
[email protected]98270432012-09-11 20:51:243756 EXPECT_FALSE(service_->IsExtensionEnabled(id));
3757
[email protected]650b2d52013-02-10 03:41:453758 base::FilePath v3_good_crx = GetTemporaryFile();
[email protected]a17dfcf2012-12-30 02:07:093759
3760 PackCRX(path.AppendASCII("v3_good"), pem_path, v3_good_crx);
3761 UpdateExtension(id, v3_good_crx, INSTALLED);
[email protected]98270432012-09-11 20:51:243762 EXPECT_FALSE(service_->IsExtensionEnabled(id));
3763}
3764
3765// The extension should not re-enabled because it was disabled from a
3766// permission increase.
[email protected]d9a61e12012-11-14 02:43:473767TEST_F(ExtensionServiceTest, UpgradingRequirementsPermissions) {
[email protected]98270432012-09-11 20:51:243768 InitializeEmptyExtensionService();
3769 BlackListWebGL();
3770
[email protected]650b2d52013-02-10 03:41:453771 base::FilePath path = data_dir_.AppendASCII("requirements");
3772 base::FilePath pem_path = data_dir_.AppendASCII("requirements")
[email protected]98270432012-09-11 20:51:243773 .AppendASCII("v1_good.pem");
3774 const Extension* extension_v1 = PackAndInstallCRX(path.AppendASCII("v1_good"),
3775 pem_path,
3776 INSTALL_NEW);
3777 std::string id = extension_v1->id();
3778 EXPECT_TRUE(service_->IsExtensionEnabled(id));
3779
[email protected]650b2d52013-02-10 03:41:453780 base::FilePath v2_bad_requirements_and_permissions_crx = GetTemporaryFile();
[email protected]a17dfcf2012-12-30 02:07:093781
3782 PackCRX(path.AppendASCII("v2_bad_requirements_and_permissions"),
3783 pem_path,
3784 v2_bad_requirements_and_permissions_crx);
3785 UpdateExtension(id, v2_bad_requirements_and_permissions_crx, INSTALLED);
[email protected]98270432012-09-11 20:51:243786 EXPECT_FALSE(service_->IsExtensionEnabled(id));
3787
[email protected]650b2d52013-02-10 03:41:453788 base::FilePath v3_bad_permissions_crx = GetTemporaryFile();
[email protected]a17dfcf2012-12-30 02:07:093789
3790 PackCRX(path.AppendASCII("v3_bad_permissions"),
3791 pem_path,
3792 v3_bad_permissions_crx);
3793 UpdateExtension(id, v3_bad_permissions_crx, INSTALLED);
[email protected]98270432012-09-11 20:51:243794 EXPECT_FALSE(service_->IsExtensionEnabled(id));
3795}
3796
3797// Unpacked extensions are not allowed to be installed if they have unsupported
3798// requirements.
[email protected]d9a61e12012-11-14 02:43:473799TEST_F(ExtensionServiceTest, UnpackedRequirements) {
[email protected]98270432012-09-11 20:51:243800 InitializeEmptyExtensionService();
3801 BlackListWebGL();
3802
[email protected]650b2d52013-02-10 03:41:453803 base::FilePath path = data_dir_.AppendASCII("requirements")
[email protected]98270432012-09-11 20:51:243804 .AppendASCII("v2_bad_requirements");
3805 extensions::UnpackedInstaller::Create(service_)->Load(path);
[email protected]b8f50ce2012-11-17 12:37:573806 loop_.RunUntilIdle();
[email protected]98270432012-09-11 20:51:243807 EXPECT_EQ(1u, GetErrors().size());
3808 EXPECT_EQ(0u, service_->extensions()->size());
3809}
3810
[email protected]c4148a72011-08-09 23:04:203811class ExtensionCookieCallback {
3812 public:
3813 ExtensionCookieCallback()
[email protected]a5768512013-04-12 19:35:353814 : result_(false),
3815 weak_factory_(MessageLoop::current()) {}
[email protected]c4148a72011-08-09 23:04:203816
3817 void SetCookieCallback(bool result) {
3818 MessageLoop::current()->PostTask(
[email protected]51d02152011-12-24 05:55:223819 FROM_HERE, base::Bind(&MessageLoop::Quit, weak_factory_.GetWeakPtr()));
[email protected]c4148a72011-08-09 23:04:203820 result_ = result;
3821 }
3822
3823 void GetAllCookiesCallback(const net::CookieList& list) {
3824 MessageLoop::current()->PostTask(
[email protected]51d02152011-12-24 05:55:223825 FROM_HERE, base::Bind(&MessageLoop::Quit, weak_factory_.GetWeakPtr()));
[email protected]c4148a72011-08-09 23:04:203826 list_ = list;
3827 }
3828 net::CookieList list_;
3829 bool result_;
[email protected]51d02152011-12-24 05:55:223830 base::WeakPtrFactory<MessageLoop> weak_factory_;
[email protected]c4148a72011-08-09 23:04:203831};
3832
[email protected]0d6ec3a72011-09-02 02:09:433833// Verifies extension state is removed upon uninstall.
[email protected]d9a61e12012-11-14 02:43:473834TEST_F(ExtensionServiceTest, ClearExtensionData) {
[email protected]eaa7dd182010-12-14 11:09:003835 InitializeEmptyExtensionService();
[email protected]c4148a72011-08-09 23:04:203836 ExtensionCookieCallback callback;
[email protected]c10da4b02010-03-25 14:38:323837
3838 // Load a test extension.
[email protected]650b2d52013-02-10 03:41:453839 base::FilePath path = data_dir_;
[email protected]c10da4b02010-03-25 14:38:323840 path = path.AppendASCII("good.crx");
[email protected]8f512c72011-11-22 21:02:503841 const Extension* extension = InstallCRX(path, INSTALL_NEW);
[email protected]c10da4b02010-03-25 14:38:323842 ASSERT_TRUE(extension);
3843 GURL ext_url(extension->url());
[email protected]391550b2013-05-10 04:34:123844 base::string16 origin_id = webkit_base::GetOriginIdentifierFromURL(ext_url);
[email protected]c10da4b02010-03-25 14:38:323845
3846 // Set a cookie for the extension.
[email protected]a5768512013-04-12 19:35:353847 net::CookieMonster* cookie_monster =
3848 profile_->GetRequestContextForExtensions()->GetURLRequestContext()->
3849 cookie_store()->GetCookieMonster();
[email protected]c10da4b02010-03-25 14:38:323850 ASSERT_TRUE(cookie_monster);
3851 net::CookieOptions options;
[email protected]c4148a72011-08-09 23:04:203852 cookie_monster->SetCookieWithOptionsAsync(
[email protected]a5768512013-04-12 19:35:353853 ext_url, "dummy=value", options,
3854 base::Bind(&ExtensionCookieCallback::SetCookieCallback,
3855 base::Unretained(&callback)));
3856 loop_.RunUntilIdle();
[email protected]c4148a72011-08-09 23:04:203857 EXPECT_TRUE(callback.result_);
3858
3859 cookie_monster->GetAllCookiesForURLAsync(
3860 ext_url,
3861 base::Bind(&ExtensionCookieCallback::GetAllCookiesCallback,
3862 base::Unretained(&callback)));
[email protected]a5768512013-04-12 19:35:353863 loop_.RunUntilIdle();
[email protected]c4148a72011-08-09 23:04:203864 EXPECT_EQ(1U, callback.list_.size());
[email protected]c10da4b02010-03-25 14:38:323865
3866 // Open a database.
[email protected]55eb70e762012-02-20 17:38:393867 webkit_database::DatabaseTracker* db_tracker =
[email protected]5c8e67c2012-08-29 00:48:523868 BrowserContext::GetDefaultStoragePartition(profile_.get())->
3869 GetDatabaseTracker();
[email protected]c10da4b02010-03-25 14:38:323870 string16 db_name = UTF8ToUTF16("db");
3871 string16 description = UTF8ToUTF16("db_description");
3872 int64 size;
[email protected]7c5f2ec2011-05-26 19:15:263873 db_tracker->DatabaseOpened(origin_id, db_name, description, 1, &size);
[email protected]c10da4b02010-03-25 14:38:323874 db_tracker->DatabaseClosed(origin_id, db_name);
3875 std::vector<webkit_database::OriginInfo> origins;
3876 db_tracker->GetAllOriginsInfo(&origins);
3877 EXPECT_EQ(1U, origins.size());
3878 EXPECT_EQ(origin_id, origins[0].GetOrigin());
3879
[email protected]98823c682012-06-11 21:18:243880 // Create local storage. We only simulate this by creating the backing files.
3881 // Note: This test depends on details of how the dom_storage library
3882 // stores data in the host file system.
[email protected]650b2d52013-02-10 03:41:453883 base::FilePath lso_dir_path =
3884 profile_->GetPath().AppendASCII("Local Storage");
3885 base::FilePath lso_file_path = lso_dir_path.AppendASCII(
[email protected]98823c682012-06-11 21:18:243886 UTF16ToUTF8(origin_id) + ".localstorage");
3887 EXPECT_TRUE(file_util::CreateDirectory(lso_dir_path));
3888 EXPECT_EQ(0, file_util::WriteFile(lso_file_path, NULL, 0));
3889 EXPECT_TRUE(file_util::PathExists(lso_file_path));
[email protected]c10da4b02010-03-25 14:38:323890
[email protected]ab308092011-08-25 23:37:193891 // Create indexed db. Similarly, it is enough to only simulate this by
3892 // creating the directory on the disk.
[email protected]b1b502e2012-09-16 07:31:433893 IndexedDBContext* idb_context =
3894 BrowserContext::GetDefaultStoragePartition(profile_.get())->
3895 GetIndexedDBContext();
[email protected]650b2d52013-02-10 03:41:453896 base::FilePath idb_path = idb_context->GetFilePathForTesting(origin_id);
[email protected]ab308092011-08-25 23:37:193897 EXPECT_TRUE(file_util::CreateDirectory(idb_path));
3898 EXPECT_TRUE(file_util::DirectoryExists(idb_path));
[email protected]e1dcf922010-11-22 19:12:123899
[email protected]c10da4b02010-03-25 14:38:323900 // Uninstall the extension.
[email protected]d6ebc9792011-04-07 18:18:333901 service_->UninstallExtension(good_crx, false, NULL);
[email protected]b8f50ce2012-11-17 12:37:573902 loop_.RunUntilIdle();
[email protected]c10da4b02010-03-25 14:38:323903
3904 // Check that the cookie is gone.
[email protected]c4148a72011-08-09 23:04:203905 cookie_monster->GetAllCookiesForURLAsync(
3906 ext_url,
3907 base::Bind(&ExtensionCookieCallback::GetAllCookiesCallback,
3908 base::Unretained(&callback)));
[email protected]b8f50ce2012-11-17 12:37:573909 loop_.RunUntilIdle();
[email protected]c4148a72011-08-09 23:04:203910 EXPECT_EQ(0U, callback.list_.size());
[email protected]c10da4b02010-03-25 14:38:323911
3912 // The database should have vanished as well.
3913 origins.clear();
3914 db_tracker->GetAllOriginsInfo(&origins);
3915 EXPECT_EQ(0U, origins.size());
3916
3917 // Check that the LSO file has been removed.
[email protected]98823c682012-06-11 21:18:243918 EXPECT_FALSE(file_util::PathExists(lso_file_path));
[email protected]e1dcf922010-11-22 19:12:123919
3920 // Check if the indexed db has disappeared too.
[email protected]ab308092011-08-25 23:37:193921 EXPECT_FALSE(file_util::DirectoryExists(idb_path));
[email protected]c10da4b02010-03-25 14:38:323922}
3923
[email protected]0d6ec3a72011-09-02 02:09:433924// Verifies app state is removed upon uninstall.
[email protected]d9a61e12012-11-14 02:43:473925TEST_F(ExtensionServiceTest, ClearAppData) {
[email protected]0d6ec3a72011-09-02 02:09:433926 InitializeEmptyExtensionService();
3927 InitializeRequestContext();
3928 ExtensionCookieCallback callback;
3929
3930 int pref_count = 0;
3931
3932 // Install app1 with unlimited storage.
[email protected]8f512c72011-11-22 21:02:503933 const Extension* extension =
3934 PackAndInstallCRX(data_dir_.AppendASCII("app1"), INSTALL_NEW);
[email protected]0d6ec3a72011-09-02 02:09:433935 ValidatePrefKeyCount(++pref_count);
3936 ASSERT_EQ(1u, service_->extensions()->size());
[email protected]0d6ec3a72011-09-02 02:09:433937 const std::string id1 = extension->id();
3938 EXPECT_TRUE(extension->HasAPIPermission(
[email protected]c2e66e12012-06-27 06:27:063939 APIPermission::kUnlimitedStorage));
[email protected]0d6ec3a72011-09-02 02:09:433940 const GURL origin1(extension->GetFullLaunchURL().GetOrigin());
3941 EXPECT_TRUE(profile_->GetExtensionSpecialStoragePolicy()->
3942 IsStorageUnlimited(origin1));
[email protected]391550b2013-05-10 04:34:123943 base::string16 origin_id = webkit_base::GetOriginIdentifierFromURL(origin1);
[email protected]0d6ec3a72011-09-02 02:09:433944
3945 // Install app2 from the same origin with unlimited storage.
[email protected]8f512c72011-11-22 21:02:503946 extension = PackAndInstallCRX(data_dir_.AppendASCII("app2"), INSTALL_NEW);
[email protected]0d6ec3a72011-09-02 02:09:433947 ValidatePrefKeyCount(++pref_count);
3948 ASSERT_EQ(2u, service_->extensions()->size());
[email protected]0d6ec3a72011-09-02 02:09:433949 const std::string id2 = extension->id();
3950 EXPECT_TRUE(extension->HasAPIPermission(
[email protected]c2e66e12012-06-27 06:27:063951 APIPermission::kUnlimitedStorage));
[email protected]0d6ec3a72011-09-02 02:09:433952 EXPECT_TRUE(extension->web_extent().MatchesURL(
3953 extension->GetFullLaunchURL()));
3954 const GURL origin2(extension->GetFullLaunchURL().GetOrigin());
3955 EXPECT_EQ(origin1, origin2);
3956 EXPECT_TRUE(profile_->GetExtensionSpecialStoragePolicy()->
3957 IsStorageUnlimited(origin2));
3958
3959 // Set a cookie for the extension.
[email protected]a5768512013-04-12 19:35:353960 net::CookieMonster* cookie_monster =
3961 profile_->GetRequestContext()->GetURLRequestContext()->
3962 cookie_store()->GetCookieMonster();
[email protected]0d6ec3a72011-09-02 02:09:433963 ASSERT_TRUE(cookie_monster);
3964 net::CookieOptions options;
3965 cookie_monster->SetCookieWithOptionsAsync(
3966 origin1, "dummy=value", options,
3967 base::Bind(&ExtensionCookieCallback::SetCookieCallback,
3968 base::Unretained(&callback)));
[email protected]b8f50ce2012-11-17 12:37:573969 loop_.RunUntilIdle();
[email protected]0d6ec3a72011-09-02 02:09:433970 EXPECT_TRUE(callback.result_);
3971
3972 cookie_monster->GetAllCookiesForURLAsync(
3973 origin1,
3974 base::Bind(&ExtensionCookieCallback::GetAllCookiesCallback,
3975 base::Unretained(&callback)));
[email protected]b8f50ce2012-11-17 12:37:573976 loop_.RunUntilIdle();
[email protected]0d6ec3a72011-09-02 02:09:433977 EXPECT_EQ(1U, callback.list_.size());
3978
3979 // Open a database.
[email protected]55eb70e762012-02-20 17:38:393980 webkit_database::DatabaseTracker* db_tracker =
[email protected]5c8e67c2012-08-29 00:48:523981 BrowserContext::GetDefaultStoragePartition(profile_.get())->
3982 GetDatabaseTracker();
[email protected]0d6ec3a72011-09-02 02:09:433983 string16 db_name = UTF8ToUTF16("db");
3984 string16 description = UTF8ToUTF16("db_description");
3985 int64 size;
3986 db_tracker->DatabaseOpened(origin_id, db_name, description, 1, &size);
3987 db_tracker->DatabaseClosed(origin_id, db_name);
3988 std::vector<webkit_database::OriginInfo> origins;
3989 db_tracker->GetAllOriginsInfo(&origins);
3990 EXPECT_EQ(1U, origins.size());
3991 EXPECT_EQ(origin_id, origins[0].GetOrigin());
3992
[email protected]98823c682012-06-11 21:18:243993 // Create local storage. We only simulate this by creating the backing files.
3994 // Note: This test depends on details of how the dom_storage library
3995 // stores data in the host file system.
[email protected]650b2d52013-02-10 03:41:453996 base::FilePath lso_dir_path =
3997 profile_->GetPath().AppendASCII("Local Storage");
3998 base::FilePath lso_file_path = lso_dir_path.AppendASCII(
[email protected]98823c682012-06-11 21:18:243999 UTF16ToUTF8(origin_id) + ".localstorage");
4000 EXPECT_TRUE(file_util::CreateDirectory(lso_dir_path));
4001 EXPECT_EQ(0, file_util::WriteFile(lso_file_path, NULL, 0));
4002 EXPECT_TRUE(file_util::PathExists(lso_file_path));
[email protected]0d6ec3a72011-09-02 02:09:434003
4004 // Create indexed db. Similarly, it is enough to only simulate this by
4005 // creating the directory on the disk.
[email protected]b1b502e2012-09-16 07:31:434006 IndexedDBContext* idb_context =
4007 BrowserContext::GetDefaultStoragePartition(profile_.get())->
4008 GetIndexedDBContext();
[email protected]650b2d52013-02-10 03:41:454009 base::FilePath idb_path = idb_context->GetFilePathForTesting(origin_id);
[email protected]0d6ec3a72011-09-02 02:09:434010 EXPECT_TRUE(file_util::CreateDirectory(idb_path));
4011 EXPECT_TRUE(file_util::DirectoryExists(idb_path));
4012
4013 // Uninstall one of them, unlimited storage should still be granted
4014 // to the origin.
4015 UninstallExtension(id1, false);
4016 EXPECT_EQ(1u, service_->extensions()->size());
4017 EXPECT_TRUE(profile_->GetExtensionSpecialStoragePolicy()->
4018 IsStorageUnlimited(origin1));
4019
4020 // Check that the cookie is still there.
4021 cookie_monster->GetAllCookiesForURLAsync(
4022 origin1,
4023 base::Bind(&ExtensionCookieCallback::GetAllCookiesCallback,
4024 base::Unretained(&callback)));
[email protected]b8f50ce2012-11-17 12:37:574025 loop_.RunUntilIdle();
[email protected]0d6ec3a72011-09-02 02:09:434026 EXPECT_EQ(1U, callback.list_.size());
4027
4028 // Now uninstall the other. Storage should be cleared for the apps.
4029 UninstallExtension(id2, false);
4030 EXPECT_EQ(0u, service_->extensions()->size());
4031 EXPECT_FALSE(profile_->GetExtensionSpecialStoragePolicy()->
4032 IsStorageUnlimited(origin1));
4033
4034 // Check that the cookie is gone.
4035 cookie_monster->GetAllCookiesForURLAsync(
4036 origin1,
4037 base::Bind(&ExtensionCookieCallback::GetAllCookiesCallback,
4038 base::Unretained(&callback)));
[email protected]b8f50ce2012-11-17 12:37:574039 loop_.RunUntilIdle();
[email protected]0d6ec3a72011-09-02 02:09:434040 EXPECT_EQ(0U, callback.list_.size());
4041
4042 // The database should have vanished as well.
4043 origins.clear();
4044 db_tracker->GetAllOriginsInfo(&origins);
4045 EXPECT_EQ(0U, origins.size());
4046
4047 // Check that the LSO file has been removed.
[email protected]98823c682012-06-11 21:18:244048 EXPECT_FALSE(file_util::PathExists(lso_file_path));
[email protected]0d6ec3a72011-09-02 02:09:434049
4050 // Check if the indexed db has disappeared too.
4051 EXPECT_FALSE(file_util::DirectoryExists(idb_path));
4052}
4053
[email protected]894bb502009-05-21 22:39:574054// Tests loading single extensions (like --load-extension)
[email protected]e3f610b2013-05-03 02:22:384055// Flaky crashes. http://crbug.com/231806
4056TEST_F(ExtensionServiceTest, DISABLED_LoadExtension) {
[email protected]eaa7dd182010-12-14 11:09:004057 InitializeEmptyExtensionService();
[email protected]3cf4f0992009-02-03 23:00:304058
[email protected]650b2d52013-02-10 03:41:454059 base::FilePath ext1 = data_dir_
[email protected]a9b00ac2009-06-25 21:03:234060 .AppendASCII("good")
4061 .AppendASCII("Extensions")
[email protected]5a2721f62009-06-13 07:08:204062 .AppendASCII("behllobkkfkfnphdnhnkndlbkcpglgmj")
4063 .AppendASCII("1.0.0.0");
[email protected]d8c8f25f2011-11-02 18:18:014064 extensions::UnpackedInstaller::Create(service_)->Load(ext1);
[email protected]b8f50ce2012-11-17 12:37:574065 loop_.RunUntilIdle();
[email protected]bb28e062009-02-27 17:19:184066 EXPECT_EQ(0u, GetErrors().size());
[email protected]894bb502009-05-21 22:39:574067 ASSERT_EQ(1u, loaded_.size());
[email protected]12075d12013-02-27 05:38:054068 EXPECT_EQ(Manifest::UNPACKED, loaded_[0]->location());
[email protected]9f1087e2009-06-15 17:29:324069 EXPECT_EQ(1u, service_->extensions()->size());
[email protected]3cf4f0992009-02-03 23:00:304070
[email protected]e8c729a2010-03-09 19:55:194071 ValidatePrefKeyCount(1);
[email protected]25b34332009-06-05 21:53:194072
[email protected]650b2d52013-02-10 03:41:454073 base::FilePath no_manifest = data_dir_
[email protected]a9b00ac2009-06-25 21:03:234074 .AppendASCII("bad")
[email protected]93fd78f42009-07-10 16:43:174075 // .AppendASCII("Extensions")
[email protected]a9b00ac2009-06-25 21:03:234076 .AppendASCII("cccccccccccccccccccccccccccccccc")
4077 .AppendASCII("1");
[email protected]d8c8f25f2011-11-02 18:18:014078 extensions::UnpackedInstaller::Create(service_)->Load(no_manifest);
[email protected]b8f50ce2012-11-17 12:37:574079 loop_.RunUntilIdle();
[email protected]bb28e062009-02-27 17:19:184080 EXPECT_EQ(1u, GetErrors().size());
[email protected]894bb502009-05-21 22:39:574081 ASSERT_EQ(1u, loaded_.size());
[email protected]9f1087e2009-06-15 17:29:324082 EXPECT_EQ(1u, service_->extensions()->size());
[email protected]25b34332009-06-05 21:53:194083
4084 // Test uninstall.
[email protected]894bb502009-05-21 22:39:574085 std::string id = loaded_[0]->id();
4086 EXPECT_FALSE(unloaded_id_.length());
[email protected]d6ebc9792011-04-07 18:18:334087 service_->UninstallExtension(id, false, NULL);
[email protected]b8f50ce2012-11-17 12:37:574088 loop_.RunUntilIdle();
[email protected]894bb502009-05-21 22:39:574089 EXPECT_EQ(id, unloaded_id_);
[email protected]9f1087e2009-06-15 17:29:324090 ASSERT_EQ(0u, loaded_.size());
4091 EXPECT_EQ(0u, service_->extensions()->size());
[email protected]3cf4f0992009-02-03 23:00:304092}
[email protected]0b344962009-03-31 04:21:454093
[email protected]894bb502009-05-21 22:39:574094// Tests that we generate IDs when they are not specified in the manifest for
4095// --load-extension.
[email protected]d9a61e12012-11-14 02:43:474096TEST_F(ExtensionServiceTest, GenerateID) {
[email protected]eaa7dd182010-12-14 11:09:004097 InitializeEmptyExtensionService();
[email protected]fbcc40302009-06-12 20:45:454098
[email protected]650b2d52013-02-10 03:41:454099 base::FilePath no_id_ext = data_dir_.AppendASCII("no_id");
[email protected]d8c8f25f2011-11-02 18:18:014100 extensions::UnpackedInstaller::Create(service_)->Load(no_id_ext);
[email protected]b8f50ce2012-11-17 12:37:574101 loop_.RunUntilIdle();
[email protected]0b344962009-03-31 04:21:454102 EXPECT_EQ(0u, GetErrors().size());
[email protected]894bb502009-05-21 22:39:574103 ASSERT_EQ(1u, loaded_.size());
[email protected]84ac7f32009-10-06 06:17:544104 ASSERT_TRUE(Extension::IdIsValid(loaded_[0]->id()));
[email protected]12075d12013-02-27 05:38:054105 EXPECT_EQ(loaded_[0]->location(), Manifest::UNPACKED);
[email protected]0b344962009-03-31 04:21:454106
[email protected]e8c729a2010-03-09 19:55:194107 ValidatePrefKeyCount(1);
[email protected]25b34332009-06-05 21:53:194108
[email protected]84ac7f32009-10-06 06:17:544109 std::string previous_id = loaded_[0]->id();
4110
4111 // If we reload the same path, we should get the same extension ID.
[email protected]d8c8f25f2011-11-02 18:18:014112 extensions::UnpackedInstaller::Create(service_)->Load(no_id_ext);
[email protected]b8f50ce2012-11-17 12:37:574113 loop_.RunUntilIdle();
[email protected]84ac7f32009-10-06 06:17:544114 ASSERT_EQ(1u, loaded_.size());
4115 ASSERT_EQ(previous_id, loaded_[0]->id());
[email protected]0b344962009-03-31 04:21:454116}
[email protected]894bb502009-05-21 22:39:574117
[email protected]eaa7dd182010-12-14 11:09:004118void ExtensionServiceTest::TestExternalProvider(
[email protected]1d5e58b2013-01-31 08:41:404119 MockExtensionProvider* provider, Manifest::Location location) {
[email protected]a1257b12009-06-12 02:51:344120 // Verify that starting with no providers loads no extensions.
4121 service_->Init();
[email protected]a1257b12009-06-12 02:51:344122 ASSERT_EQ(0u, loaded_.size());
4123
[email protected]0a60a2e2010-10-25 16:15:214124 provider->set_visit_count(0);
4125
[email protected]a1257b12009-06-12 02:51:344126 // Register a test extension externally using the mock registry provider.
[email protected]650b2d52013-02-10 03:41:454127 base::FilePath source_path = data_dir_.AppendASCII("good.crx");
[email protected]894bb502009-05-21 22:39:574128
[email protected]a1257b12009-06-12 02:51:344129 // Add the extension.
[email protected]d55e7602009-12-16 04:20:424130 provider->UpdateOrAddExtension(good_crx, "1.0.0.0", source_path);
[email protected]894bb502009-05-21 22:39:574131
[email protected]9f1087e2009-06-15 17:29:324132 // Reloading extensions should find our externally registered extension
[email protected]894bb502009-05-21 22:39:574133 // and install it.
[email protected]93fd78f42009-07-10 16:43:174134 service_->CheckForExternalUpdates();
[email protected]b8f50ce2012-11-17 12:37:574135 loop_.RunUntilIdle();
[email protected]894bb502009-05-21 22:39:574136
4137 ASSERT_EQ(0u, GetErrors().size());
4138 ASSERT_EQ(1u, loaded_.size());
[email protected]d55e7602009-12-16 04:20:424139 ASSERT_EQ(location, loaded_[0]->location());
[email protected]894bb502009-05-21 22:39:574140 ASSERT_EQ("1.0.0.0", loaded_[0]->version()->GetString());
[email protected]25b34332009-06-05 21:53:194141 ValidatePrefKeyCount(1);
[email protected]e2194742010-08-12 05:54:344142 ValidateIntegerPref(good_crx, "state", Extension::ENABLED);
4143 ValidateIntegerPref(good_crx, "location", location);
[email protected]894bb502009-05-21 22:39:574144
[email protected]9f1087e2009-06-15 17:29:324145 // Reload extensions without changing anything. The extension should be
[email protected]894bb502009-05-21 22:39:574146 // loaded again.
4147 loaded_.clear();
[email protected]9f1087e2009-06-15 17:29:324148 service_->ReloadExtensions();
[email protected]b8f50ce2012-11-17 12:37:574149 loop_.RunUntilIdle();
[email protected]894bb502009-05-21 22:39:574150 ASSERT_EQ(0u, GetErrors().size());
4151 ASSERT_EQ(1u, loaded_.size());
[email protected]25b34332009-06-05 21:53:194152 ValidatePrefKeyCount(1);
[email protected]e2194742010-08-12 05:54:344153 ValidateIntegerPref(good_crx, "state", Extension::ENABLED);
4154 ValidateIntegerPref(good_crx, "location", location);
[email protected]e2eb43112009-05-29 21:19:544155
[email protected]894bb502009-05-21 22:39:574156 // Now update the extension with a new version. We should get upgraded.
4157 source_path = source_path.DirName().AppendASCII("good2.crx");
[email protected]d55e7602009-12-16 04:20:424158 provider->UpdateOrAddExtension(good_crx, "1.0.0.1", source_path);
[email protected]894bb502009-05-21 22:39:574159
4160 loaded_.clear();
[email protected]93fd78f42009-07-10 16:43:174161 service_->CheckForExternalUpdates();
[email protected]b8f50ce2012-11-17 12:37:574162 loop_.RunUntilIdle();
[email protected]894bb502009-05-21 22:39:574163 ASSERT_EQ(0u, GetErrors().size());
4164 ASSERT_EQ(1u, loaded_.size());
4165 ASSERT_EQ("1.0.0.1", loaded_[0]->version()->GetString());
[email protected]25b34332009-06-05 21:53:194166 ValidatePrefKeyCount(1);
[email protected]e2194742010-08-12 05:54:344167 ValidateIntegerPref(good_crx, "state", Extension::ENABLED);
4168 ValidateIntegerPref(good_crx, "location", location);
[email protected]894bb502009-05-21 22:39:574169
[email protected]27b985d2009-06-25 17:53:154170 // Uninstall the extension and reload. Nothing should happen because the
[email protected]894bb502009-05-21 22:39:574171 // preference should prevent us from reinstalling.
4172 std::string id = loaded_[0]->id();
[email protected]e410b5f2012-12-14 14:02:244173 bool no_uninstall = management_policy_->MustRemainEnabled(loaded_[0], NULL);
[email protected]d6ebc9792011-04-07 18:18:334174 service_->UninstallExtension(id, false, NULL);
[email protected]b8f50ce2012-11-17 12:37:574175 loop_.RunUntilIdle();
[email protected]894bb502009-05-21 22:39:574176
[email protected]650b2d52013-02-10 03:41:454177 base::FilePath install_path = extensions_install_dir_.AppendASCII(id);
[email protected]e410b5f2012-12-14 14:02:244178 if (no_uninstall) {
[email protected]65187152012-06-02 13:14:144179 // Policy controlled extensions should not have been touched by uninstall.
4180 ASSERT_TRUE(file_util::PathExists(install_path));
4181 } else {
[email protected]95da88c42011-03-31 10:07:334182 // The extension should also be gone from the install directory.
4183 ASSERT_FALSE(file_util::PathExists(install_path));
4184 loaded_.clear();
4185 service_->CheckForExternalUpdates();
[email protected]b8f50ce2012-11-17 12:37:574186 loop_.RunUntilIdle();
[email protected]95da88c42011-03-31 10:07:334187 ASSERT_EQ(0u, loaded_.size());
4188 ValidatePrefKeyCount(1);
[email protected]79c833b52011-04-05 18:31:014189 ValidateIntegerPref(good_crx, "state",
4190 Extension::EXTERNAL_EXTENSION_UNINSTALLED);
[email protected]95da88c42011-03-31 10:07:334191 ValidateIntegerPref(good_crx, "location", location);
[email protected]894bb502009-05-21 22:39:574192
[email protected]95da88c42011-03-31 10:07:334193 // Now clear the preference and reinstall.
4194 SetPrefInteg(good_crx, "state", Extension::ENABLED);
[email protected]25b34332009-06-05 21:53:194195
[email protected]95da88c42011-03-31 10:07:334196 loaded_.clear();
4197 service_->CheckForExternalUpdates();
[email protected]b8f50ce2012-11-17 12:37:574198 loop_.RunUntilIdle();
[email protected]95da88c42011-03-31 10:07:334199 ASSERT_EQ(1u, loaded_.size());
[email protected]95da88c42011-03-31 10:07:334200 }
[email protected]25b34332009-06-05 21:53:194201 ValidatePrefKeyCount(1);
[email protected]e2194742010-08-12 05:54:344202 ValidateIntegerPref(good_crx, "state", Extension::ENABLED);
4203 ValidateIntegerPref(good_crx, "location", location);
[email protected]25b34332009-06-05 21:53:194204
[email protected]e410b5f2012-12-14 14:02:244205 if (management_policy_->MustRemainEnabled(loaded_[0], NULL)) {
[email protected]65187152012-06-02 13:14:144206 EXPECT_EQ(2, provider->visit_count());
4207 } else {
[email protected]95da88c42011-03-31 10:07:334208 // Now test an externally triggered uninstall (deleting the registry key or
4209 // the pref entry).
4210 provider->RemoveExtension(good_crx);
[email protected]25b34332009-06-05 21:53:194211
[email protected]95da88c42011-03-31 10:07:334212 loaded_.clear();
[email protected]50067e52011-10-20 23:17:074213 service_->OnExternalProviderReady(provider);
[email protected]b8f50ce2012-11-17 12:37:574214 loop_.RunUntilIdle();
[email protected]95da88c42011-03-31 10:07:334215 ASSERT_EQ(0u, loaded_.size());
4216 ValidatePrefKeyCount(0);
[email protected]25b34332009-06-05 21:53:194217
[email protected]95da88c42011-03-31 10:07:334218 // The extension should also be gone from the install directory.
4219 ASSERT_FALSE(file_util::PathExists(install_path));
[email protected]abe7a8942009-06-23 05:14:294220
[email protected]95da88c42011-03-31 10:07:334221 // Now test the case where user uninstalls and then the extension is removed
4222 // from the external provider.
[email protected]05aad2da2011-10-28 10:12:374223 provider->UpdateOrAddExtension(good_crx, "1.0.0.1", source_path);
[email protected]95da88c42011-03-31 10:07:334224 service_->CheckForExternalUpdates();
[email protected]b8f50ce2012-11-17 12:37:574225 loop_.RunUntilIdle();
[email protected]abe7a8942009-06-23 05:14:294226
[email protected]95da88c42011-03-31 10:07:334227 ASSERT_EQ(1u, loaded_.size());
4228 ASSERT_EQ(0u, GetErrors().size());
[email protected]d55e7602009-12-16 04:20:424229
[email protected]95da88c42011-03-31 10:07:334230 // User uninstalls.
4231 loaded_.clear();
[email protected]d6ebc9792011-04-07 18:18:334232 service_->UninstallExtension(id, false, NULL);
[email protected]b8f50ce2012-11-17 12:37:574233 loop_.RunUntilIdle();
[email protected]95da88c42011-03-31 10:07:334234 ASSERT_EQ(0u, loaded_.size());
[email protected]d55e7602009-12-16 04:20:424235
[email protected]95da88c42011-03-31 10:07:334236 // Then remove the extension from the extension provider.
4237 provider->RemoveExtension(good_crx);
[email protected]d55e7602009-12-16 04:20:424238
[email protected]95da88c42011-03-31 10:07:334239 // Should still be at 0.
4240 loaded_.clear();
[email protected]d8c8f25f2011-11-02 18:18:014241 extensions::InstalledLoader(service_).LoadAllExtensions();
[email protected]b8f50ce2012-11-17 12:37:574242 loop_.RunUntilIdle();
[email protected]95da88c42011-03-31 10:07:334243 ASSERT_EQ(0u, loaded_.size());
4244 ValidatePrefKeyCount(1);
[email protected]0a60a2e2010-10-25 16:15:214245
[email protected]95da88c42011-03-31 10:07:334246 EXPECT_EQ(5, provider->visit_count());
[email protected]95da88c42011-03-31 10:07:334247 }
[email protected]d55e7602009-12-16 04:20:424248}
4249
4250// Tests the external installation feature
4251#if defined(OS_WIN)
[email protected]d9a61e12012-11-14 02:43:474252TEST_F(ExtensionServiceTest, ExternalInstallRegistry) {
[email protected]aebe23a32010-12-10 22:15:484253 // This should all work, even when normal extension installation is disabled.
[email protected]eaa7dd182010-12-14 11:09:004254 InitializeEmptyExtensionService();
[email protected]aebe23a32010-12-10 22:15:484255 set_extensions_enabled(false);
[email protected]d55e7602009-12-16 04:20:424256
4257 // Now add providers. Extension system takes ownership of the objects.
4258 MockExtensionProvider* reg_provider =
[email protected]1d5e58b2013-01-31 08:41:404259 new MockExtensionProvider(service_, Manifest::EXTERNAL_REGISTRY);
[email protected]0a60a2e2010-10-25 16:15:214260 AddMockExternalProvider(reg_provider);
[email protected]1d5e58b2013-01-31 08:41:404261 TestExternalProvider(reg_provider, Manifest::EXTERNAL_REGISTRY);
[email protected]d55e7602009-12-16 04:20:424262}
4263#endif
4264
[email protected]d9a61e12012-11-14 02:43:474265TEST_F(ExtensionServiceTest, ExternalInstallPref) {
[email protected]eaa7dd182010-12-14 11:09:004266 InitializeEmptyExtensionService();
[email protected]d55e7602009-12-16 04:20:424267
4268 // Now add providers. Extension system takes ownership of the objects.
4269 MockExtensionProvider* pref_provider =
[email protected]1d5e58b2013-01-31 08:41:404270 new MockExtensionProvider(service_, Manifest::EXTERNAL_PREF);
[email protected]0a60a2e2010-10-25 16:15:214271
4272 AddMockExternalProvider(pref_provider);
[email protected]1d5e58b2013-01-31 08:41:404273 TestExternalProvider(pref_provider, Manifest::EXTERNAL_PREF);
[email protected]27b985d2009-06-25 17:53:154274}
4275
[email protected]d9a61e12012-11-14 02:43:474276TEST_F(ExtensionServiceTest, ExternalInstallPrefUpdateUrl) {
[email protected]aebe23a32010-12-10 22:15:484277 // This should all work, even when normal extension installation is disabled.
[email protected]eaa7dd182010-12-14 11:09:004278 InitializeEmptyExtensionService();
[email protected]aebe23a32010-12-10 22:15:484279 set_extensions_enabled(false);
[email protected]55196e92010-09-29 15:04:464280
[email protected]0a60a2e2010-10-25 16:15:214281 // TODO(skerner): The mock provider is not a good model of a provider
4282 // that works with update URLs, because it adds file and version info.
4283 // Extend the mock to work with update URLs. This test checks the
4284 // behavior that is common to all external extension visitors. The
4285 // browser test ExtensionManagementTest.ExternalUrlUpdate tests that
4286 // what the visitor does results in an extension being downloaded and
4287 // installed.
[email protected]55196e92010-09-29 15:04:464288 MockExtensionProvider* pref_provider =
[email protected]14908b72011-04-20 06:54:364289 new MockExtensionProvider(service_,
[email protected]1d5e58b2013-01-31 08:41:404290 Manifest::EXTERNAL_PREF_DOWNLOAD);
[email protected]0a60a2e2010-10-25 16:15:214291 AddMockExternalProvider(pref_provider);
[email protected]1d5e58b2013-01-31 08:41:404292 TestExternalProvider(pref_provider, Manifest::EXTERNAL_PREF_DOWNLOAD);
[email protected]55196e92010-09-29 15:04:464293}
4294
[email protected]d9a61e12012-11-14 02:43:474295TEST_F(ExtensionServiceTest, ExternalInstallPolicyUpdateUrl) {
[email protected]95da88c42011-03-31 10:07:334296 // This should all work, even when normal extension installation is disabled.
4297 InitializeEmptyExtensionService();
4298 set_extensions_enabled(false);
4299
4300 // TODO(skerner): The mock provider is not a good model of a provider
4301 // that works with update URLs, because it adds file and version info.
4302 // Extend the mock to work with update URLs. This test checks the
4303 // behavior that is common to all external extension visitors. The
4304 // browser test ExtensionManagementTest.ExternalUrlUpdate tests that
4305 // what the visitor does results in an extension being downloaded and
4306 // installed.
4307 MockExtensionProvider* pref_provider =
[email protected]14908b72011-04-20 06:54:364308 new MockExtensionProvider(service_,
[email protected]1d5e58b2013-01-31 08:41:404309 Manifest::EXTERNAL_POLICY_DOWNLOAD);
[email protected]95da88c42011-03-31 10:07:334310 AddMockExternalProvider(pref_provider);
[email protected]1d5e58b2013-01-31 08:41:404311 TestExternalProvider(pref_provider, Manifest::EXTERNAL_POLICY_DOWNLOAD);
[email protected]95da88c42011-03-31 10:07:334312}
4313
[email protected]aebe23a32010-12-10 22:15:484314// Tests that external extensions get uninstalled when the external extension
4315// providers can't account for them.
[email protected]d9a61e12012-11-14 02:43:474316TEST_F(ExtensionServiceTest, ExternalUninstall) {
[email protected]aebe23a32010-12-10 22:15:484317 // Start the extensions service with one external extension already installed.
[email protected]650b2d52013-02-10 03:41:454318 base::FilePath source_install_dir = data_dir_
[email protected]aebe23a32010-12-10 22:15:484319 .AppendASCII("good")
4320 .AppendASCII("Extensions");
[email protected]650b2d52013-02-10 03:41:454321 base::FilePath pref_path = source_install_dir
[email protected]aebe23a32010-12-10 22:15:484322 .DirName()
4323 .AppendASCII("PreferencesExternal");
4324
[email protected]5df038b2012-07-16 19:03:274325 // This initializes the extensions service with no ExternalProviders.
[email protected]eaa7dd182010-12-14 11:09:004326 InitializeInstalledExtensionService(pref_path, source_install_dir);
[email protected]aebe23a32010-12-10 22:15:484327 set_extensions_enabled(false);
4328
4329 service_->Init();
[email protected]aebe23a32010-12-10 22:15:484330
4331 ASSERT_EQ(0u, GetErrors().size());
[email protected]1f4728f2012-12-05 20:40:054332 ASSERT_EQ(0u, loaded_.size());
[email protected]aebe23a32010-12-10 22:15:484333
4334 // Verify that it's not the disabled extensions flag causing it not to load.
4335 set_extensions_enabled(true);
4336 service_->ReloadExtensions();
[email protected]b8f50ce2012-11-17 12:37:574337 loop_.RunUntilIdle();
[email protected]aebe23a32010-12-10 22:15:484338
4339 ASSERT_EQ(0u, GetErrors().size());
4340 ASSERT_EQ(0u, loaded_.size());
4341}
4342
[email protected]a29a517a2011-01-21 21:11:124343// Test that running multiple update checks simultaneously does not
4344// keep the update from succeeding.
[email protected]d9a61e12012-11-14 02:43:474345TEST_F(ExtensionServiceTest, MultipleExternalUpdateCheck) {
[email protected]a29a517a2011-01-21 21:11:124346 InitializeEmptyExtensionService();
4347
4348 MockExtensionProvider* provider =
[email protected]1d5e58b2013-01-31 08:41:404349 new MockExtensionProvider(service_, Manifest::EXTERNAL_PREF);
[email protected]a29a517a2011-01-21 21:11:124350 AddMockExternalProvider(provider);
4351
4352 // Verify that starting with no providers loads no extensions.
4353 service_->Init();
[email protected]a29a517a2011-01-21 21:11:124354 ASSERT_EQ(0u, loaded_.size());
4355
4356 // Start two checks for updates.
4357 provider->set_visit_count(0);
4358 service_->CheckForExternalUpdates();
4359 service_->CheckForExternalUpdates();
[email protected]b8f50ce2012-11-17 12:37:574360 loop_.RunUntilIdle();
[email protected]a29a517a2011-01-21 21:11:124361
4362 // Two calls should cause two checks for external extensions.
4363 EXPECT_EQ(2, provider->visit_count());
4364 EXPECT_EQ(0u, GetErrors().size());
4365 EXPECT_EQ(0u, loaded_.size());
4366
4367 // Register a test extension externally using the mock registry provider.
[email protected]650b2d52013-02-10 03:41:454368 base::FilePath source_path = data_dir_.AppendASCII("good.crx");
[email protected]a29a517a2011-01-21 21:11:124369 provider->UpdateOrAddExtension(good_crx, "1.0.0.0", source_path);
4370
4371 // Two checks for external updates should find the extension, and install it
4372 // once.
4373 provider->set_visit_count(0);
4374 service_->CheckForExternalUpdates();
4375 service_->CheckForExternalUpdates();
[email protected]b8f50ce2012-11-17 12:37:574376 loop_.RunUntilIdle();
[email protected]a29a517a2011-01-21 21:11:124377 EXPECT_EQ(2, provider->visit_count());
4378 ASSERT_EQ(0u, GetErrors().size());
4379 ASSERT_EQ(1u, loaded_.size());
[email protected]1d5e58b2013-01-31 08:41:404380 ASSERT_EQ(Manifest::EXTERNAL_PREF, loaded_[0]->location());
[email protected]a29a517a2011-01-21 21:11:124381 ASSERT_EQ("1.0.0.0", loaded_[0]->version()->GetString());
4382 ValidatePrefKeyCount(1);
4383 ValidateIntegerPref(good_crx, "state", Extension::ENABLED);
[email protected]1d5e58b2013-01-31 08:41:404384 ValidateIntegerPref(good_crx, "location", Manifest::EXTERNAL_PREF);
[email protected]a29a517a2011-01-21 21:11:124385
4386 provider->RemoveExtension(good_crx);
4387 provider->set_visit_count(0);
4388 service_->CheckForExternalUpdates();
4389 service_->CheckForExternalUpdates();
[email protected]b8f50ce2012-11-17 12:37:574390 loop_.RunUntilIdle();
[email protected]a29a517a2011-01-21 21:11:124391
4392 // Two calls should cause two checks for external extensions.
4393 // Because the external source no longer includes good_crx,
4394 // good_crx will be uninstalled. So, expect that no extensions
4395 // are loaded.
4396 EXPECT_EQ(2, provider->visit_count());
4397 EXPECT_EQ(0u, GetErrors().size());
4398 EXPECT_EQ(0u, loaded_.size());
4399}
4400
[email protected]9d32ded072011-10-11 16:31:054401namespace {
4402 class ScopedBrowserLocale {
4403 public:
4404 explicit ScopedBrowserLocale(const std::string& new_locale)
4405 : old_locale_(g_browser_process->GetApplicationLocale()) {
4406 g_browser_process->SetApplicationLocale(new_locale);
4407 }
4408
4409 ~ScopedBrowserLocale() {
4410 g_browser_process->SetApplicationLocale(old_locale_);
4411 }
4412
4413 private:
4414 std::string old_locale_;
4415 };
4416}
4417
[email protected]d9a61e12012-11-14 02:43:474418TEST_F(ExtensionServiceTest, ExternalPrefProvider) {
[email protected]eaa7dd182010-12-14 11:09:004419 InitializeEmptyExtensionService();
[email protected]f0841cd2011-01-19 15:07:244420
4421 // Test some valid extension records.
4422 // Set a base path to avoid erroring out on relative paths.
4423 // Paths starting with // are absolute on every platform we support.
[email protected]650b2d52013-02-10 03:41:454424 base::FilePath base_path(FILE_PATH_LITERAL("//base/path"));
[email protected]f0841cd2011-01-19 15:07:244425 ASSERT_TRUE(base_path.IsAbsolute());
4426 MockProviderVisitor visitor(base_path);
[email protected]27b985d2009-06-25 17:53:154427 std::string json_data =
4428 "{"
[email protected]f0841cd2011-01-19 15:07:244429 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\": {"
[email protected]9e54cb572010-09-03 20:08:064430 " \"external_crx\": \"RandomExtension.crx\","
4431 " \"external_version\": \"1.0\""
4432 " },"
4433 " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\": {"
4434 " \"external_crx\": \"RandomExtension2.crx\","
4435 " \"external_version\": \"2.0\""
4436 " },"
4437 " \"cccccccccccccccccccccccccccccccc\": {"
[email protected]a424d84c2010-09-24 09:31:154438 " \"external_update_url\": \"http:\\\\foo.com/update\""
[email protected]9e54cb572010-09-03 20:08:064439 " }"
[email protected]27b985d2009-06-25 17:53:154440 "}";
[email protected]f0841cd2011-01-19 15:07:244441 EXPECT_EQ(3, visitor.Visit(json_data));
[email protected]27b985d2009-06-25 17:53:154442
[email protected]9e54cb572010-09-03 20:08:064443 // Simulate an external_extensions.json file that contains seven invalid
[email protected]f0841cd2011-01-19 15:07:244444 // records:
[email protected]27b985d2009-06-25 17:53:154445 // - One that is missing the 'external_crx' key.
4446 // - One that is missing the 'external_version' key.
4447 // - One that is specifying .. in the path.
[email protected]8ef78fd2010-08-19 17:14:324448 // - One that specifies both a file and update URL.
4449 // - One that specifies no file or update URL.
4450 // - One that has an update URL that is not well formed.
[email protected]9e54cb572010-09-03 20:08:064451 // - One that contains a malformed version.
[email protected]ab22ba42011-01-14 16:36:384452 // - One that has an invalid id.
4453 // - One that has a non-dictionary value.
[email protected]0d461c52012-07-03 19:29:414454 // - One that has an integer 'external_version' instead of a string.
[email protected]9e54cb572010-09-03 20:08:064455 // The final extension is valid, and we check that it is read to make sure
4456 // failures don't stop valid records from being read.
[email protected]27b985d2009-06-25 17:53:154457 json_data =
4458 "{"
[email protected]9e54cb572010-09-03 20:08:064459 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\": {"
4460 " \"external_version\": \"1.0\""
4461 " },"
4462 " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\": {"
4463 " \"external_crx\": \"RandomExtension.crx\""
4464 " },"
4465 " \"cccccccccccccccccccccccccccccccc\": {"
4466 " \"external_crx\": \"..\\\\foo\\\\RandomExtension2.crx\","
4467 " \"external_version\": \"2.0\""
4468 " },"
4469 " \"dddddddddddddddddddddddddddddddd\": {"
4470 " \"external_crx\": \"RandomExtension2.crx\","
4471 " \"external_version\": \"2.0\","
4472 " \"external_update_url\": \"http:\\\\foo.com/update\""
4473 " },"
4474 " \"eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee\": {"
4475 " },"
4476 " \"ffffffffffffffffffffffffffffffff\": {"
4477 " \"external_update_url\": \"This string is not a valid URL\""
4478 " },"
4479 " \"gggggggggggggggggggggggggggggggg\": {"
4480 " \"external_crx\": \"RandomExtension3.crx\","
4481 " \"external_version\": \"This is not a valid version!\""
4482 " },"
[email protected]ab22ba42011-01-14 16:36:384483 " \"This is not a valid id!\": {},"
4484 " \"hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh\": true,"
[email protected]0d461c52012-07-03 19:29:414485 " \"iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii\": {"
4486 " \"external_crx\": \"RandomExtension4.crx\","
4487 " \"external_version\": 1.0"
4488 " },"
[email protected]ab22ba42011-01-14 16:36:384489 " \"pppppppppppppppppppppppppppppppp\": {"
[email protected]9e54cb572010-09-03 20:08:064490 " \"external_crx\": \"RandomValidExtension.crx\","
4491 " \"external_version\": \"1.0\""
4492 " }"
[email protected]27b985d2009-06-25 17:53:154493 "}";
[email protected]683d0702010-12-06 16:25:574494 EXPECT_EQ(1, visitor.Visit(json_data));
[email protected]f0841cd2011-01-19 15:07:244495
4496 // Check that if a base path is not provided, use of a relative
4497 // path fails.
[email protected]650b2d52013-02-10 03:41:454498 base::FilePath empty;
[email protected]f0841cd2011-01-19 15:07:244499 MockProviderVisitor visitor_no_relative_paths(empty);
4500
4501 // Use absolute paths. Expect success.
4502 json_data =
4503 "{"
4504 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\": {"
4505 " \"external_crx\": \"//RandomExtension1.crx\","
4506 " \"external_version\": \"3.0\""
4507 " },"
4508 " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\": {"
4509 " \"external_crx\": \"//path/to/RandomExtension2.crx\","
4510 " \"external_version\": \"3.0\""
4511 " }"
4512 "}";
4513 EXPECT_EQ(2, visitor_no_relative_paths.Visit(json_data));
4514
4515 // Use a relative path. Expect that it will error out.
4516 json_data =
4517 "{"
4518 " \"cccccccccccccccccccccccccccccccc\": {"
4519 " \"external_crx\": \"RandomExtension2.crx\","
4520 " \"external_version\": \"3.0\""
4521 " }"
4522 "}";
4523 EXPECT_EQ(0, visitor_no_relative_paths.Visit(json_data));
[email protected]9d32ded072011-10-11 16:31:054524
4525 // Test supported_locales.
4526 json_data =
4527 "{"
4528 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\": {"
4529 " \"external_crx\": \"RandomExtension.crx\","
4530 " \"external_version\": \"1.0\","
4531 " \"supported_locales\": [ \"en\" ]"
4532 " },"
4533 " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\": {"
4534 " \"external_crx\": \"RandomExtension2.crx\","
4535 " \"external_version\": \"2.0\","
4536 " \"supported_locales\": [ \"en-GB\" ]"
4537 " },"
4538 " \"cccccccccccccccccccccccccccccccc\": {"
4539 " \"external_crx\": \"RandomExtension2.crx\","
4540 " \"external_version\": \"3.0\","
4541 " \"supported_locales\": [ \"en_US\", \"fr\" ]"
4542 " }"
4543 "}";
4544 {
4545 ScopedBrowserLocale guard("en-US");
4546 EXPECT_EQ(2, visitor.Visit(json_data));
4547 }
[email protected]f121003b2012-05-04 21:57:474548
4549 // Test is_bookmark_app.
4550 MockProviderVisitor from_bookmark_visitor(
4551 base_path, Extension::FROM_BOOKMARK);
4552 json_data =
4553 "{"
4554 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\": {"
4555 " \"external_crx\": \"RandomExtension.crx\","
4556 " \"external_version\": \"1.0\","
4557 " \"is_bookmark_app\": true"
4558 " }"
4559 "}";
4560 EXPECT_EQ(1, from_bookmark_visitor.Visit(json_data));
[email protected]7425d7df2012-11-28 14:35:424561
4562 // Test is_from_webstore.
4563 MockProviderVisitor from_webstore_visitor(
4564 base_path, Extension::FROM_WEBSTORE);
4565 json_data =
4566 "{"
4567 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\": {"
4568 " \"external_crx\": \"RandomExtension.crx\","
4569 " \"external_version\": \"1.0\","
4570 " \"is_from_webstore\": true"
4571 " }"
4572 "}";
4573 EXPECT_EQ(1, from_webstore_visitor.Visit(json_data));
[email protected]e18236b2009-06-22 21:32:104574}
[email protected]36a784c2009-06-23 06:21:084575
[email protected]c6d474f82009-12-16 21:11:064576// Test loading good extensions from the profile directory.
[email protected]d9a61e12012-11-14 02:43:474577TEST_F(ExtensionServiceTest, LoadAndRelocalizeExtensions) {
[email protected]6884a802012-08-07 03:55:224578 // Ensure we're testing in "en" and leave global state untouched.
4579 extension_l10n_util::ScopedLocaleForTest testLocale("en");
4580
[email protected]c6d474f82009-12-16 21:11:064581 // Initialize the test dir with a good Preferences/extensions.
[email protected]650b2d52013-02-10 03:41:454582 base::FilePath source_install_dir = data_dir_
[email protected]c6d474f82009-12-16 21:11:064583 .AppendASCII("l10n");
[email protected]650b2d52013-02-10 03:41:454584 base::FilePath pref_path = source_install_dir.AppendASCII("Preferences");
[email protected]eaa7dd182010-12-14 11:09:004585 InitializeInstalledExtensionService(pref_path, source_install_dir);
[email protected]c6d474f82009-12-16 21:11:064586
4587 service_->Init();
[email protected]c6d474f82009-12-16 21:11:064588
4589 ASSERT_EQ(3u, loaded_.size());
4590
4591 // This was equal to "sr" on load.
4592 ValidateStringPref(loaded_[0]->id(), keys::kCurrentLocale, "en");
4593
4594 // These are untouched by re-localization.
4595 ValidateStringPref(loaded_[1]->id(), keys::kCurrentLocale, "en");
4596 EXPECT_FALSE(IsPrefExist(loaded_[1]->id(), keys::kCurrentLocale));
4597
4598 // This one starts with Serbian name, and gets re-localized into English.
4599 EXPECT_EQ("My name is simple.", loaded_[0]->name());
4600
4601 // These are untouched by re-localization.
4602 EXPECT_EQ("My name is simple.", loaded_[1]->name());
4603 EXPECT_EQ("no l10n", loaded_[2]->name());
4604}
4605
[email protected]6c2381d2011-10-19 02:52:534606class ExtensionsReadyRecorder : public content::NotificationObserver {
[email protected]f0488f2f2009-07-01 05:25:224607 public:
4608 ExtensionsReadyRecorder() : ready_(false) {
[email protected]432115822011-07-10 15:52:274609 registrar_.Add(this, chrome::NOTIFICATION_EXTENSIONS_READY,
[email protected]ad50def52011-10-19 23:17:074610 content::NotificationService::AllSources());
[email protected]f0488f2f2009-07-01 05:25:224611 }
4612
4613 void set_ready(bool value) { ready_ = value; }
4614 bool ready() { return ready_; }
4615
4616 private:
[email protected]432115822011-07-10 15:52:274617 virtual void Observe(int type,
[email protected]6c2381d2011-10-19 02:52:534618 const content::NotificationSource& source,
[email protected]49aeab62013-02-07 02:53:114619 const content::NotificationDetails& details) OVERRIDE {
[email protected]432115822011-07-10 15:52:274620 switch (type) {
4621 case chrome::NOTIFICATION_EXTENSIONS_READY:
[email protected]f0488f2f2009-07-01 05:25:224622 ready_ = true;
4623 break;
4624 default:
4625 NOTREACHED();
4626 }
4627 }
4628
[email protected]6c2381d2011-10-19 02:52:534629 content::NotificationRegistrar registrar_;
[email protected]f0488f2f2009-07-01 05:25:224630 bool ready_;
4631};
4632
[email protected]36a784c2009-06-23 06:21:084633// Test that we get enabled/disabled correctly for all the pref/command-line
[email protected]eaa7dd182010-12-14 11:09:004634// combinations. We don't want to derive from the ExtensionServiceTest class
4635// for this test, so we use ExtensionServiceTestSimple.
[email protected]f0488f2f2009-07-01 05:25:224636//
4637// Also tests that we always fire EXTENSIONS_READY, no matter whether we are
4638// enabled or not.
[email protected]39fdf5202012-11-14 00:38:174639TEST(ExtensionServiceTestSimple, Enabledness) {
[email protected]5a24d2b2012-11-14 20:38:314640 // Make sure the PluginService singleton is destroyed at the end of the test.
[email protected]39fdf5202012-11-14 00:38:174641 base::ShadowingAtExitManager at_exit_manager;
[email protected]5a24d2b2012-11-14 20:38:314642
[email protected]ed8ee722011-04-22 06:49:444643 ExtensionErrorReporter::Init(false); // no noisy errors
[email protected]f0488f2f2009-07-01 05:25:224644 ExtensionsReadyRecorder recorder;
[email protected]aa96d3a2010-08-21 08:45:254645 scoped_ptr<TestingProfile> profile(new TestingProfile());
[email protected]36a784c2009-06-23 06:21:084646 MessageLoop loop;
[email protected]c38831a12011-10-28 12:44:494647 content::TestBrowserThread ui_thread(BrowserThread::UI, &loop);
4648 content::TestBrowserThread file_thread(BrowserThread::FILE, &loop);
[email protected]e4854dc2013-04-24 00:11:514649#if defined OS_CHROMEOS
4650 chromeos::ScopedTestDeviceSettingsService device_settings_service;
4651 chromeos::ScopedTestCrosSettings cros_settings;
4652 scoped_ptr<chromeos::ScopedTestUserManager> user_manager(
4653 new chromeos::ScopedTestUserManager);
4654#endif
[email protected]36a784c2009-06-23 06:21:084655 scoped_ptr<CommandLine> command_line;
[email protected]650b2d52013-02-10 03:41:454656 base::FilePath install_dir = profile->GetPath()
[email protected]eaa7dd182010-12-14 11:09:004657 .AppendASCII(ExtensionService::kInstallDirectoryName);
[email protected]be5a6db2012-11-13 14:39:114658
[email protected]ebd71962012-12-20 02:56:554659#if defined(ENABLE_PLUGINS)
[email protected]0b300172012-09-27 16:11:524660 webkit::npapi::MockPluginList plugin_list;
[email protected]eb6c7ef2011-12-12 23:12:204661 PluginService::GetInstance()->SetPluginListForTesting(&plugin_list);
[email protected]ebd71962012-12-20 02:56:554662#endif
[email protected]36a784c2009-06-23 06:21:084663
[email protected]6d60703b2009-08-29 01:29:234664 // By default, we are enabled.
[email protected]947446b2010-10-21 03:36:314665 command_line.reset(new CommandLine(CommandLine::NO_PROGRAM));
[email protected]bd306722012-07-11 20:43:594666 ExtensionService* service = static_cast<extensions::TestExtensionSystem*>(
[email protected]749d59a2012-04-05 00:23:244667 ExtensionSystem::Get(profile.get()))->
[email protected]31d8f5f22012-04-02 15:22:084668 CreateExtensionService(
4669 command_line.get(),
4670 install_dir,
4671 false);
[email protected]6d60703b2009-08-29 01:29:234672 EXPECT_TRUE(service->extensions_enabled());
4673 service->Init();
[email protected]b8f50ce2012-11-17 12:37:574674 loop.RunUntilIdle();
[email protected]6d60703b2009-08-29 01:29:234675 EXPECT_TRUE(recorder.ready());
[email protected]e4854dc2013-04-24 00:11:514676#if defined OS_CHROMEOS
4677 user_manager.reset();
4678#endif
[email protected]6d60703b2009-08-29 01:29:234679
4680 // If either the command line or pref is set, we are disabled.
4681 recorder.set_ready(false);
[email protected]aa96d3a2010-08-21 08:45:254682 profile.reset(new TestingProfile());
[email protected]6d60703b2009-08-29 01:29:234683 command_line->AppendSwitch(switches::kDisableExtensions);
[email protected]bd306722012-07-11 20:43:594684 service = static_cast<extensions::TestExtensionSystem*>(
[email protected]749d59a2012-04-05 00:23:244685 ExtensionSystem::Get(profile.get()))->
[email protected]31d8f5f22012-04-02 15:22:084686 CreateExtensionService(
4687 command_line.get(),
4688 install_dir,
4689 false);
[email protected]36a784c2009-06-23 06:21:084690 EXPECT_FALSE(service->extensions_enabled());
[email protected]f0488f2f2009-07-01 05:25:224691 service->Init();
[email protected]b8f50ce2012-11-17 12:37:574692 loop.RunUntilIdle();
[email protected]f0488f2f2009-07-01 05:25:224693 EXPECT_TRUE(recorder.ready());
[email protected]36a784c2009-06-23 06:21:084694
[email protected]f0488f2f2009-07-01 05:25:224695 recorder.set_ready(false);
[email protected]aa96d3a2010-08-21 08:45:254696 profile.reset(new TestingProfile());
4697 profile->GetPrefs()->SetBoolean(prefs::kDisableExtensions, true);
[email protected]bd306722012-07-11 20:43:594698 service = static_cast<extensions::TestExtensionSystem*>(
[email protected]749d59a2012-04-05 00:23:244699 ExtensionSystem::Get(profile.get()))->
[email protected]31d8f5f22012-04-02 15:22:084700 CreateExtensionService(
4701 command_line.get(),
4702 install_dir,
4703 false);
[email protected]6d60703b2009-08-29 01:29:234704 EXPECT_FALSE(service->extensions_enabled());
[email protected]f0488f2f2009-07-01 05:25:224705 service->Init();
[email protected]b8f50ce2012-11-17 12:37:574706 loop.RunUntilIdle();
[email protected]f0488f2f2009-07-01 05:25:224707 EXPECT_TRUE(recorder.ready());
[email protected]36a784c2009-06-23 06:21:084708
[email protected]f0488f2f2009-07-01 05:25:224709 recorder.set_ready(false);
[email protected]aa96d3a2010-08-21 08:45:254710 profile.reset(new TestingProfile());
4711 profile->GetPrefs()->SetBoolean(prefs::kDisableExtensions, true);
[email protected]947446b2010-10-21 03:36:314712 command_line.reset(new CommandLine(CommandLine::NO_PROGRAM));
[email protected]bd306722012-07-11 20:43:594713 service = static_cast<extensions::TestExtensionSystem*>(
[email protected]749d59a2012-04-05 00:23:244714 ExtensionSystem::Get(profile.get()))->
[email protected]31d8f5f22012-04-02 15:22:084715 CreateExtensionService(
4716 command_line.get(),
4717 install_dir,
4718 false);
[email protected]6d60703b2009-08-29 01:29:234719 EXPECT_FALSE(service->extensions_enabled());
[email protected]f0488f2f2009-07-01 05:25:224720 service->Init();
[email protected]b8f50ce2012-11-17 12:37:574721 loop.RunUntilIdle();
[email protected]f0488f2f2009-07-01 05:25:224722 EXPECT_TRUE(recorder.ready());
[email protected]7e1951a2010-09-30 10:22:204723
4724 // Explicitly delete all the resources used in this test.
4725 profile.reset();
4726 service = NULL;
[email protected]060d4972012-07-19 17:22:394727 // Execute any pending deletion tasks.
[email protected]b8f50ce2012-11-17 12:37:574728 loop.RunUntilIdle();
[email protected]5a24d2b2012-11-14 20:38:314729
[email protected]ebd71962012-12-20 02:56:554730#if defined(ENABLE_PLUGINS)
[email protected]5a24d2b2012-11-14 20:38:314731 // Ensure that even if the PluginService is re-used for a later test, it
4732 // won't still hold a reference to the stack position of our MockPluginList.
4733 // See crbug.com/159754.
4734 PluginService::GetInstance()->SetPluginListForTesting(NULL);
[email protected]ebd71962012-12-20 02:56:554735#endif
[email protected]36a784c2009-06-23 06:21:084736}
[email protected]24b538a2010-02-27 01:22:444737
4738// Test loading extensions that require limited and unlimited storage quotas.
[email protected]d9a61e12012-11-14 02:43:474739TEST_F(ExtensionServiceTest, StorageQuota) {
[email protected]eaa7dd182010-12-14 11:09:004740 InitializeEmptyExtensionService();
[email protected]24b538a2010-02-27 01:22:444741
[email protected]650b2d52013-02-10 03:41:454742 base::FilePath extensions_path = data_dir_
[email protected]24b538a2010-02-27 01:22:444743 .AppendASCII("storage_quota");
4744
[email protected]650b2d52013-02-10 03:41:454745 base::FilePath limited_quota_ext =
[email protected]e85e34c32011-04-13 18:38:354746 extensions_path.AppendASCII("limited_quota")
[email protected]24b538a2010-02-27 01:22:444747 .AppendASCII("1.0");
[email protected]03b612f2010-08-13 21:09:214748
4749 // The old permission name for unlimited quota was "unlimited_storage", but
4750 // we changed it to "unlimitedStorage". This tests both versions.
[email protected]650b2d52013-02-10 03:41:454751 base::FilePath unlimited_quota_ext =
[email protected]e85e34c32011-04-13 18:38:354752 extensions_path.AppendASCII("unlimited_quota")
[email protected]24b538a2010-02-27 01:22:444753 .AppendASCII("1.0");
[email protected]650b2d52013-02-10 03:41:454754 base::FilePath unlimited_quota_ext2 =
[email protected]e85e34c32011-04-13 18:38:354755 extensions_path.AppendASCII("unlimited_quota")
[email protected]03b612f2010-08-13 21:09:214756 .AppendASCII("2.0");
[email protected]d8c8f25f2011-11-02 18:18:014757 extensions::UnpackedInstaller::Create(service_)->Load(limited_quota_ext);
4758 extensions::UnpackedInstaller::Create(service_)->Load(unlimited_quota_ext);
4759 extensions::UnpackedInstaller::Create(service_)->Load(unlimited_quota_ext2);
[email protected]b8f50ce2012-11-17 12:37:574760 loop_.RunUntilIdle();
[email protected]24b538a2010-02-27 01:22:444761
[email protected]03b612f2010-08-13 21:09:214762 ASSERT_EQ(3u, loaded_.size());
[email protected]24b538a2010-02-27 01:22:444763 EXPECT_TRUE(profile_.get());
4764 EXPECT_FALSE(profile_->IsOffTheRecord());
[email protected]7c5f2ec2011-05-26 19:15:264765 EXPECT_FALSE(profile_->GetExtensionSpecialStoragePolicy()->IsStorageUnlimited(
4766 loaded_[0]->url()));
4767 EXPECT_TRUE(profile_->GetExtensionSpecialStoragePolicy()->IsStorageUnlimited(
4768 loaded_[1]->url()));
4769 EXPECT_TRUE(profile_->GetExtensionSpecialStoragePolicy()->IsStorageUnlimited(
4770 loaded_[2]->url()));
[email protected]24b538a2010-02-27 01:22:444771}
[email protected]1952c7d2010-03-04 23:48:344772
[email protected]d8c8f25f2011-11-02 18:18:014773// Tests ComponentLoader::Add().
[email protected]d9a61e12012-11-14 02:43:474774TEST_F(ExtensionServiceTest, ComponentExtensions) {
[email protected]eaa7dd182010-12-14 11:09:004775 InitializeEmptyExtensionService();
[email protected]1952c7d2010-03-04 23:48:344776
[email protected]f0b97f12010-10-11 21:44:354777 // Component extensions should work even when extensions are disabled.
4778 set_extensions_enabled(false);
4779
[email protected]650b2d52013-02-10 03:41:454780 base::FilePath path = data_dir_
[email protected]1952c7d2010-03-04 23:48:344781 .AppendASCII("good")
4782 .AppendASCII("Extensions")
4783 .AppendASCII("behllobkkfkfnphdnhnkndlbkcpglgmj")
4784 .AppendASCII("1.0.0.0");
4785
4786 std::string manifest;
4787 ASSERT_TRUE(file_util::ReadFileToString(
[email protected]993da5e2013-03-23 21:25:164788 path.Append(extensions::kManifestFilename), &manifest));
[email protected]1952c7d2010-03-04 23:48:344789
[email protected]d8c8f25f2011-11-02 18:18:014790 service_->component_loader()->Add(manifest, path);
[email protected]1952c7d2010-03-04 23:48:344791 service_->Init();
4792
4793 // Note that we do not pump messages -- the extension should be loaded
4794 // immediately.
4795
4796 EXPECT_EQ(0u, GetErrors().size());
4797 ASSERT_EQ(1u, loaded_.size());
[email protected]1d5e58b2013-01-31 08:41:404798 EXPECT_EQ(Manifest::COMPONENT, loaded_[0]->location());
[email protected]1952c7d2010-03-04 23:48:344799 EXPECT_EQ(1u, service_->extensions()->size());
4800
[email protected]8c484b742012-11-29 06:05:364801 // Component extensions get a prefs entry on first install.
4802 ValidatePrefKeyCount(1);
[email protected]1952c7d2010-03-04 23:48:344803
4804 // Reload all extensions, and make sure it comes back.
[email protected]8f512c72011-11-22 21:02:504805 std::string extension_id = (*service_->extensions()->begin())->id();
[email protected]1952c7d2010-03-04 23:48:344806 loaded_.clear();
4807 service_->ReloadExtensions();
4808 ASSERT_EQ(1u, service_->extensions()->size());
[email protected]8f512c72011-11-22 21:02:504809 EXPECT_EQ(extension_id, (*service_->extensions()->begin())->id());
[email protected]1952c7d2010-03-04 23:48:344810}
[email protected]145a317b2011-04-12 16:03:464811
[email protected]90310d92011-04-17 07:35:044812namespace {
[email protected]65f173552012-06-28 22:43:584813 class TestSyncProcessorStub : public syncer::SyncChangeProcessor {
4814 virtual syncer::SyncError ProcessSyncChanges(
[email protected]3bdba0d2011-08-23 07:17:304815 const tracked_objects::Location& from_here,
[email protected]65f173552012-06-28 22:43:584816 const syncer::SyncChangeList& change_list) OVERRIDE {
4817 return syncer::SyncError();
[email protected]3bdba0d2011-08-23 07:17:304818 }
4819 };
[email protected]90310d92011-04-17 07:35:044820}
4821
[email protected]d9a61e12012-11-14 02:43:474822TEST_F(ExtensionServiceTest, GetSyncData) {
[email protected]b05fb9ff2011-04-23 00:07:564823 InitializeEmptyExtensionService();
[email protected]8f512c72011-11-22 21:02:504824 InstallCRX(data_dir_.AppendASCII("good.crx"), INSTALL_NEW);
[email protected]45b6fee2011-05-03 09:41:314825 const Extension* extension = service_->GetInstalledExtension(good_crx);
[email protected]b05fb9ff2011-04-23 00:07:564826 ASSERT_TRUE(extension);
[email protected]85fc9202011-05-05 00:04:594827
[email protected]cb02f612012-06-27 03:15:504828 service_->MergeDataAndStartSyncing(
[email protected]a4a147652012-07-03 23:41:324829 syncer::EXTENSIONS, syncer::SyncDataList(),
[email protected]65f173552012-06-28 22:43:584830 scoped_ptr<syncer::SyncChangeProcessor>(new TestSyncProcessorStub),
4831 scoped_ptr<syncer::SyncErrorFactory>(new syncer::SyncErrorFactoryMock()));
[email protected]3bdba0d2011-08-23 07:17:304832
[email protected]a4a147652012-07-03 23:41:324833 syncer::SyncDataList list = service_->GetAllSyncData(syncer::EXTENSIONS);
[email protected]3bdba0d2011-08-23 07:17:304834 ASSERT_EQ(list.size(), 1U);
[email protected]5db9ada2012-04-11 13:48:204835 extensions::ExtensionSyncData data(list[0]);
[email protected]3bdba0d2011-08-23 07:17:304836 EXPECT_EQ(extension->id(), data.id());
4837 EXPECT_FALSE(data.uninstalled());
4838 EXPECT_EQ(service_->IsExtensionEnabled(good_crx), data.enabled());
4839 EXPECT_EQ(service_->IsIncognitoEnabled(good_crx), data.incognito_enabled());
4840 EXPECT_TRUE(data.version().Equals(*extension->version()));
[email protected]65348062013-01-15 07:27:224841 EXPECT_EQ(extensions::ManifestURL::GetUpdateURL(extension),
4842 data.update_url());
[email protected]3bdba0d2011-08-23 07:17:304843 EXPECT_EQ(extension->name(), data.name());
[email protected]b05fb9ff2011-04-23 00:07:564844}
4845
[email protected]d9a61e12012-11-14 02:43:474846TEST_F(ExtensionServiceTest, GetSyncDataTerminated) {
[email protected]45b6fee2011-05-03 09:41:314847 InitializeEmptyExtensionService();
[email protected]8f512c72011-11-22 21:02:504848 InstallCRX(data_dir_.AppendASCII("good.crx"), INSTALL_NEW);
[email protected]45b6fee2011-05-03 09:41:314849 TerminateExtension(good_crx);
[email protected]85fc9202011-05-05 00:04:594850 const Extension* extension = service_->GetInstalledExtension(good_crx);
4851 ASSERT_TRUE(extension);
[email protected]3bdba0d2011-08-23 07:17:304852
4853 TestSyncProcessorStub processor;
[email protected]cb02f612012-06-27 03:15:504854 service_->MergeDataAndStartSyncing(
[email protected]a4a147652012-07-03 23:41:324855 syncer::EXTENSIONS, syncer::SyncDataList(),
[email protected]65f173552012-06-28 22:43:584856 scoped_ptr<syncer::SyncChangeProcessor>(new TestSyncProcessorStub),
4857 scoped_ptr<syncer::SyncErrorFactory>(new syncer::SyncErrorFactoryMock()));
[email protected]3bdba0d2011-08-23 07:17:304858
[email protected]a4a147652012-07-03 23:41:324859 syncer::SyncDataList list = service_->GetAllSyncData(syncer::EXTENSIONS);
[email protected]3bdba0d2011-08-23 07:17:304860 ASSERT_EQ(list.size(), 1U);
[email protected]5db9ada2012-04-11 13:48:204861 extensions::ExtensionSyncData data(list[0]);
[email protected]3bdba0d2011-08-23 07:17:304862 EXPECT_EQ(extension->id(), data.id());
4863 EXPECT_FALSE(data.uninstalled());
4864 EXPECT_EQ(service_->IsExtensionEnabled(good_crx), data.enabled());
4865 EXPECT_EQ(service_->IsIncognitoEnabled(good_crx), data.incognito_enabled());
4866 EXPECT_TRUE(data.version().Equals(*extension->version()));
[email protected]65348062013-01-15 07:27:224867 EXPECT_EQ(extensions::ManifestURL::GetUpdateURL(extension),
4868 data.update_url());
[email protected]3bdba0d2011-08-23 07:17:304869 EXPECT_EQ(extension->name(), data.name());
[email protected]45b6fee2011-05-03 09:41:314870}
4871
[email protected]d9a61e12012-11-14 02:43:474872TEST_F(ExtensionServiceTest, GetSyncDataFilter) {
[email protected]b05fb9ff2011-04-23 00:07:564873 InitializeEmptyExtensionService();
[email protected]8f512c72011-11-22 21:02:504874 InstallCRX(data_dir_.AppendASCII("good.crx"), INSTALL_NEW);
[email protected]85fc9202011-05-05 00:04:594875 const Extension* extension = service_->GetInstalledExtension(good_crx);
4876 ASSERT_TRUE(extension);
[email protected]3bdba0d2011-08-23 07:17:304877
4878 TestSyncProcessorStub processor;
[email protected]a4a147652012-07-03 23:41:324879 service_->MergeDataAndStartSyncing(syncer::APPS, syncer::SyncDataList(),
[email protected]65f173552012-06-28 22:43:584880 scoped_ptr<syncer::SyncChangeProcessor>(new TestSyncProcessorStub),
4881 scoped_ptr<syncer::SyncErrorFactory>(new syncer::SyncErrorFactoryMock()));
[email protected]3bdba0d2011-08-23 07:17:304882
[email protected]a4a147652012-07-03 23:41:324883 syncer::SyncDataList list = service_->GetAllSyncData(syncer::EXTENSIONS);
[email protected]3bdba0d2011-08-23 07:17:304884 ASSERT_EQ(list.size(), 0U);
[email protected]b05fb9ff2011-04-23 00:07:564885}
4886
[email protected]d9a61e12012-11-14 02:43:474887TEST_F(ExtensionServiceTest, GetSyncExtensionDataUserSettings) {
[email protected]b05fb9ff2011-04-23 00:07:564888 InitializeEmptyExtensionService();
[email protected]8f512c72011-11-22 21:02:504889 InstallCRX(data_dir_.AppendASCII("good.crx"), INSTALL_NEW);
[email protected]85fc9202011-05-05 00:04:594890 const Extension* extension = service_->GetInstalledExtension(good_crx);
4891 ASSERT_TRUE(extension);
[email protected]b05fb9ff2011-04-23 00:07:564892
[email protected]3bdba0d2011-08-23 07:17:304893 TestSyncProcessorStub processor;
[email protected]cb02f612012-06-27 03:15:504894 service_->MergeDataAndStartSyncing(
[email protected]a4a147652012-07-03 23:41:324895 syncer::EXTENSIONS, syncer::SyncDataList(),
[email protected]65f173552012-06-28 22:43:584896 scoped_ptr<syncer::SyncChangeProcessor>(new TestSyncProcessorStub),
4897 scoped_ptr<syncer::SyncErrorFactory>(new syncer::SyncErrorFactoryMock()));
[email protected]3bdba0d2011-08-23 07:17:304898
[email protected]b05fb9ff2011-04-23 00:07:564899 {
[email protected]a4a147652012-07-03 23:41:324900 syncer::SyncDataList list = service_->GetAllSyncData(syncer::EXTENSIONS);
[email protected]3bdba0d2011-08-23 07:17:304901 ASSERT_EQ(list.size(), 1U);
[email protected]5db9ada2012-04-11 13:48:204902 extensions::ExtensionSyncData data(list[0]);
[email protected]3bdba0d2011-08-23 07:17:304903 EXPECT_TRUE(data.enabled());
4904 EXPECT_FALSE(data.incognito_enabled());
[email protected]b05fb9ff2011-04-23 00:07:564905 }
4906
[email protected]44d62b62012-04-11 00:06:034907 service_->DisableExtension(good_crx, Extension::DISABLE_USER_ACTION);
[email protected]b05fb9ff2011-04-23 00:07:564908 {
[email protected]a4a147652012-07-03 23:41:324909 syncer::SyncDataList list = service_->GetAllSyncData(syncer::EXTENSIONS);
[email protected]3bdba0d2011-08-23 07:17:304910 ASSERT_EQ(list.size(), 1U);
[email protected]5db9ada2012-04-11 13:48:204911 extensions::ExtensionSyncData data(list[0]);
[email protected]3bdba0d2011-08-23 07:17:304912 EXPECT_FALSE(data.enabled());
4913 EXPECT_FALSE(data.incognito_enabled());
[email protected]b05fb9ff2011-04-23 00:07:564914 }
4915
4916 service_->SetIsIncognitoEnabled(good_crx, true);
4917 {
[email protected]a4a147652012-07-03 23:41:324918 syncer::SyncDataList list = service_->GetAllSyncData(syncer::EXTENSIONS);
[email protected]3bdba0d2011-08-23 07:17:304919 ASSERT_EQ(list.size(), 1U);
[email protected]5db9ada2012-04-11 13:48:204920 extensions::ExtensionSyncData data(list[0]);
[email protected]3bdba0d2011-08-23 07:17:304921 EXPECT_FALSE(data.enabled());
4922 EXPECT_TRUE(data.incognito_enabled());
[email protected]b05fb9ff2011-04-23 00:07:564923 }
4924
4925 service_->EnableExtension(good_crx);
4926 {
[email protected]a4a147652012-07-03 23:41:324927 syncer::SyncDataList list = service_->GetAllSyncData(syncer::EXTENSIONS);
[email protected]3bdba0d2011-08-23 07:17:304928 ASSERT_EQ(list.size(), 1U);
[email protected]5db9ada2012-04-11 13:48:204929 extensions::ExtensionSyncData data(list[0]);
[email protected]3bdba0d2011-08-23 07:17:304930 EXPECT_TRUE(data.enabled());
4931 EXPECT_TRUE(data.incognito_enabled());
[email protected]b05fb9ff2011-04-23 00:07:564932 }
4933}
4934
[email protected]d9a61e12012-11-14 02:43:474935TEST_F(ExtensionServiceTest, SyncForUninstalledExternalExtension) {
[email protected]e8b0f252012-05-05 09:44:424936 InitializeEmptyExtensionService();
4937 InstallCRXWithLocation(data_dir_.AppendASCII("good.crx"),
[email protected]1d5e58b2013-01-31 08:41:404938 Manifest::EXTERNAL_PREF, INSTALL_NEW);
[email protected]e8b0f252012-05-05 09:44:424939 const Extension* extension = service_->GetInstalledExtension(good_crx);
4940 ASSERT_TRUE(extension);
4941
4942 TestSyncProcessorStub processor;
[email protected]cb02f612012-06-27 03:15:504943 service_->MergeDataAndStartSyncing(
[email protected]a4a147652012-07-03 23:41:324944 syncer::EXTENSIONS, syncer::SyncDataList(),
[email protected]65f173552012-06-28 22:43:584945 scoped_ptr<syncer::SyncChangeProcessor>(new TestSyncProcessorStub),
4946 scoped_ptr<syncer::SyncErrorFactory>(new syncer::SyncErrorFactoryMock()));
[email protected]e8b0f252012-05-05 09:44:424947
4948 UninstallExtension(good_crx, false);
4949 EXPECT_TRUE(service_->IsExternalExtensionUninstalled(good_crx));
4950
4951 sync_pb::EntitySpecifics specifics;
4952 sync_pb::AppSpecifics* app_specifics = specifics.mutable_app();
4953 sync_pb::ExtensionSpecifics* extension_specifics =
4954 app_specifics->mutable_extension();
4955 extension_specifics->set_id(good_crx);
4956 extension_specifics->set_version("1.0");
4957 extension_specifics->set_enabled(true);
4958
[email protected]65f173552012-06-28 22:43:584959 syncer::SyncData sync_data =
4960 syncer::SyncData::CreateLocalData(good_crx, "Name", specifics);
[email protected]b78170f2012-07-11 03:34:264961 syncer::SyncChange sync_change(FROM_HERE,
4962 syncer::SyncChange::ACTION_UPDATE,
4963 sync_data);
[email protected]65f173552012-06-28 22:43:584964 syncer::SyncChangeList list(1);
[email protected]e8b0f252012-05-05 09:44:424965 list[0] = sync_change;
4966
4967 service_->ProcessSyncChanges(FROM_HERE, list);
4968 EXPECT_TRUE(service_->IsExternalExtensionUninstalled(good_crx));
4969}
4970
[email protected]d9a61e12012-11-14 02:43:474971TEST_F(ExtensionServiceTest, GetSyncAppDataUserSettings) {
[email protected]168389f2011-12-20 17:12:484972 InitializeEmptyExtensionService();
4973 const Extension* app =
4974 PackAndInstallCRX(data_dir_.AppendASCII("app"), INSTALL_NEW);
4975 ASSERT_TRUE(app);
4976 ASSERT_TRUE(app->is_app());
4977
4978 TestSyncProcessorStub processor;
[email protected]a4a147652012-07-03 23:41:324979 service_->MergeDataAndStartSyncing(syncer::APPS, syncer::SyncDataList(),
[email protected]65f173552012-06-28 22:43:584980 scoped_ptr<syncer::SyncChangeProcessor>(new TestSyncProcessorStub),
4981 scoped_ptr<syncer::SyncErrorFactory>(new syncer::SyncErrorFactoryMock()));
[email protected]168389f2011-12-20 17:12:484982
[email protected]36b643212012-09-07 12:53:004983 syncer::StringOrdinal initial_ordinal =
4984 syncer::StringOrdinal::CreateInitialOrdinal();
[email protected]168389f2011-12-20 17:12:484985 {
[email protected]a4a147652012-07-03 23:41:324986 syncer::SyncDataList list = service_->GetAllSyncData(syncer::APPS);
[email protected]168389f2011-12-20 17:12:484987 ASSERT_EQ(list.size(), 1U);
[email protected]5db9ada2012-04-11 13:48:204988
4989 extensions::AppSyncData app_sync_data(list[0]);
[email protected]36b643212012-09-07 12:53:004990 EXPECT_TRUE(initial_ordinal.Equals(app_sync_data.app_launch_ordinal()));
4991 EXPECT_TRUE(initial_ordinal.Equals(app_sync_data.page_ordinal()));
[email protected]168389f2011-12-20 17:12:484992 }
4993
[email protected]fb82dcd2012-03-21 14:15:464994 ExtensionSorting* sorting = service_->extension_prefs()->extension_sorting();
4995 sorting->SetAppLaunchOrdinal(app->id(), initial_ordinal.CreateAfter());
[email protected]168389f2011-12-20 17:12:484996 {
[email protected]a4a147652012-07-03 23:41:324997 syncer::SyncDataList list = service_->GetAllSyncData(syncer::APPS);
[email protected]168389f2011-12-20 17:12:484998 ASSERT_EQ(list.size(), 1U);
[email protected]5db9ada2012-04-11 13:48:204999
5000 extensions::AppSyncData app_sync_data(list[0]);
5001 EXPECT_TRUE(initial_ordinal.LessThan(app_sync_data.app_launch_ordinal()));
[email protected]36b643212012-09-07 12:53:005002 EXPECT_TRUE(initial_ordinal.Equals(app_sync_data.page_ordinal()));
[email protected]168389f2011-12-20 17:12:485003 }
5004
[email protected]fb82dcd2012-03-21 14:15:465005 sorting->SetPageOrdinal(app->id(), initial_ordinal.CreateAfter());
[email protected]168389f2011-12-20 17:12:485006 {
[email protected]a4a147652012-07-03 23:41:325007 syncer::SyncDataList list = service_->GetAllSyncData(syncer::APPS);
[email protected]168389f2011-12-20 17:12:485008 ASSERT_EQ(list.size(), 1U);
[email protected]5db9ada2012-04-11 13:48:205009
5010 extensions::AppSyncData app_sync_data(list[0]);
5011 EXPECT_TRUE(initial_ordinal.LessThan(app_sync_data.app_launch_ordinal()));
5012 EXPECT_TRUE(initial_ordinal.LessThan(app_sync_data.page_ordinal()));
[email protected]168389f2011-12-20 17:12:485013 }
5014}
5015
[email protected]d9a61e12012-11-14 02:43:475016TEST_F(ExtensionServiceTest, GetSyncAppDataUserSettingsOnExtensionMoved) {
[email protected]168389f2011-12-20 17:12:485017 InitializeEmptyExtensionService();
5018 const size_t kAppCount = 3;
5019 const Extension* apps[kAppCount];
5020 apps[0] = PackAndInstallCRX(data_dir_.AppendASCII("app1"), INSTALL_NEW);
5021 apps[1] = PackAndInstallCRX(data_dir_.AppendASCII("app2"), INSTALL_NEW);
5022 apps[2] = PackAndInstallCRX(data_dir_.AppendASCII("app4"), INSTALL_NEW);
5023 for (size_t i = 0; i < kAppCount; ++i) {
5024 ASSERT_TRUE(apps[i]);
5025 ASSERT_TRUE(apps[i]->is_app());
5026 }
5027
5028 TestSyncProcessorStub processor;
[email protected]a4a147652012-07-03 23:41:325029 service_->MergeDataAndStartSyncing(syncer::APPS, syncer::SyncDataList(),
[email protected]65f173552012-06-28 22:43:585030 scoped_ptr<syncer::SyncChangeProcessor>(new TestSyncProcessorStub),
5031 scoped_ptr<syncer::SyncErrorFactory>(new syncer::SyncErrorFactoryMock()));
[email protected]168389f2011-12-20 17:12:485032
5033 service_->OnExtensionMoved(apps[0]->id(), apps[1]->id(), apps[2]->id());
5034 {
[email protected]a4a147652012-07-03 23:41:325035 syncer::SyncDataList list = service_->GetAllSyncData(syncer::APPS);
[email protected]168389f2011-12-20 17:12:485036 ASSERT_EQ(list.size(), 3U);
[email protected]5db9ada2012-04-11 13:48:205037
5038 extensions::AppSyncData data[kAppCount];
[email protected]bb05cae12012-09-06 00:37:525039 for (size_t i = 0; i < kAppCount; ++i) {
[email protected]5db9ada2012-04-11 13:48:205040 data[i] = extensions::AppSyncData(list[i]);
5041 }
[email protected]168389f2011-12-20 17:12:485042
5043 // The sync data is not always in the same order our apps were installed in,
5044 // so we do that sorting here so we can make sure the values are changed as
5045 // expected.
[email protected]36b643212012-09-07 12:53:005046 syncer::StringOrdinal app_launch_ordinals[kAppCount];
[email protected]168389f2011-12-20 17:12:485047 for (size_t i = 0; i < kAppCount; ++i) {
5048 for (size_t j = 0; j < kAppCount; ++j) {
5049 if (apps[i]->id() == data[j].id())
5050 app_launch_ordinals[i] = data[j].app_launch_ordinal();
5051 }
5052 }
5053
5054 EXPECT_TRUE(app_launch_ordinals[1].LessThan(app_launch_ordinals[0]));
5055 EXPECT_TRUE(app_launch_ordinals[0].LessThan(app_launch_ordinals[2]));
5056 }
5057}
5058
[email protected]d9a61e12012-11-14 02:43:475059TEST_F(ExtensionServiceTest, GetSyncDataList) {
[email protected]b05fb9ff2011-04-23 00:07:565060 InitializeEmptyExtensionService();
[email protected]8f512c72011-11-22 21:02:505061 InstallCRX(data_dir_.AppendASCII("good.crx"), INSTALL_NEW);
5062 InstallCRX(data_dir_.AppendASCII("page_action.crx"), INSTALL_NEW);
5063 InstallCRX(data_dir_.AppendASCII("theme.crx"), INSTALL_NEW);
5064 InstallCRX(data_dir_.AppendASCII("theme2.crx"), INSTALL_NEW);
[email protected]b05fb9ff2011-04-23 00:07:565065
[email protected]3bdba0d2011-08-23 07:17:305066 TestSyncProcessorStub processor;
[email protected]a4a147652012-07-03 23:41:325067 service_->MergeDataAndStartSyncing(syncer::APPS, syncer::SyncDataList(),
[email protected]65f173552012-06-28 22:43:585068 scoped_ptr<syncer::SyncChangeProcessor>(new TestSyncProcessorStub),
5069 scoped_ptr<syncer::SyncErrorFactory>(new syncer::SyncErrorFactoryMock()));
[email protected]cb02f612012-06-27 03:15:505070 service_->MergeDataAndStartSyncing(
[email protected]a4a147652012-07-03 23:41:325071 syncer::EXTENSIONS, syncer::SyncDataList(),
[email protected]65f173552012-06-28 22:43:585072 scoped_ptr<syncer::SyncChangeProcessor>(new TestSyncProcessorStub),
5073 scoped_ptr<syncer::SyncErrorFactory>(new syncer::SyncErrorFactoryMock()));
[email protected]3bdba0d2011-08-23 07:17:305074
[email protected]44d62b62012-04-11 00:06:035075 service_->DisableExtension(page_action, Extension::DISABLE_USER_ACTION);
[email protected]45b6fee2011-05-03 09:41:315076 TerminateExtension(theme2_crx);
[email protected]b05fb9ff2011-04-23 00:07:565077
[email protected]a4a147652012-07-03 23:41:325078 EXPECT_EQ(0u, service_->GetAllSyncData(syncer::APPS).size());
5079 EXPECT_EQ(2u, service_->GetAllSyncData(syncer::EXTENSIONS).size());
[email protected]b05fb9ff2011-04-23 00:07:565080}
5081
[email protected]d9a61e12012-11-14 02:43:475082TEST_F(ExtensionServiceTest, ProcessSyncDataUninstall) {
[email protected]90310d92011-04-17 07:35:045083 InitializeEmptyExtensionService();
[email protected]3bdba0d2011-08-23 07:17:305084 TestSyncProcessorStub processor;
[email protected]cb02f612012-06-27 03:15:505085 service_->MergeDataAndStartSyncing(
[email protected]a4a147652012-07-03 23:41:325086 syncer::EXTENSIONS, syncer::SyncDataList(),
[email protected]65f173552012-06-28 22:43:585087 scoped_ptr<syncer::SyncChangeProcessor>(new TestSyncProcessorStub),
5088 scoped_ptr<syncer::SyncErrorFactory>(new syncer::SyncErrorFactoryMock()));
[email protected]90310d92011-04-17 07:35:045089
[email protected]3bdba0d2011-08-23 07:17:305090 sync_pb::EntitySpecifics specifics;
[email protected]4557d222012-03-04 23:33:365091 sync_pb::ExtensionSpecifics* ext_specifics = specifics.mutable_extension();
[email protected]3bdba0d2011-08-23 07:17:305092 ext_specifics->set_id(good_crx);
5093 ext_specifics->set_version("1.0");
[email protected]65f173552012-06-28 22:43:585094 syncer::SyncData sync_data =
5095 syncer::SyncData::CreateLocalData(good_crx, "Name", specifics);
[email protected]b78170f2012-07-11 03:34:265096 syncer::SyncChange sync_change(FROM_HERE,
5097 syncer::SyncChange::ACTION_DELETE,
5098 sync_data);
[email protected]65f173552012-06-28 22:43:585099 syncer::SyncChangeList list(1);
[email protected]3bdba0d2011-08-23 07:17:305100 list[0] = sync_change;
[email protected]90310d92011-04-17 07:35:045101
5102 // Should do nothing.
[email protected]3bdba0d2011-08-23 07:17:305103 service_->ProcessSyncChanges(FROM_HERE, list);
5104 EXPECT_FALSE(service_->GetExtensionById(good_crx, true));
[email protected]90310d92011-04-17 07:35:045105
5106 // Install the extension.
[email protected]650b2d52013-02-10 03:41:455107 base::FilePath extension_path = data_dir_.AppendASCII("good.crx");
[email protected]8f512c72011-11-22 21:02:505108 InstallCRX(extension_path, INSTALL_NEW);
[email protected]90310d92011-04-17 07:35:045109 EXPECT_TRUE(service_->GetExtensionById(good_crx, true));
5110
5111 // Should uninstall the extension.
[email protected]3bdba0d2011-08-23 07:17:305112 service_->ProcessSyncChanges(FROM_HERE, list);
[email protected]90310d92011-04-17 07:35:045113 EXPECT_FALSE(service_->GetExtensionById(good_crx, true));
5114
5115 // Should again do nothing.
[email protected]3bdba0d2011-08-23 07:17:305116 service_->ProcessSyncChanges(FROM_HERE, list);
5117 EXPECT_FALSE(service_->GetExtensionById(good_crx, true));
[email protected]90310d92011-04-17 07:35:045118}
5119
[email protected]d9a61e12012-11-14 02:43:475120TEST_F(ExtensionServiceTest, ProcessSyncDataWrongType) {
[email protected]96e989b2011-08-30 19:35:065121 InitializeEmptyExtensionService();
5122
5123 // Install the extension.
[email protected]650b2d52013-02-10 03:41:455124 base::FilePath extension_path = data_dir_.AppendASCII("good.crx");
[email protected]8f512c72011-11-22 21:02:505125 InstallCRX(extension_path, INSTALL_NEW);
[email protected]96e989b2011-08-30 19:35:065126 EXPECT_TRUE(service_->GetExtensionById(good_crx, true));
5127
5128 sync_pb::EntitySpecifics specifics;
[email protected]4557d222012-03-04 23:33:365129 sync_pb::AppSpecifics* app_specifics = specifics.mutable_app();
[email protected]96e989b2011-08-30 19:35:065130 sync_pb::ExtensionSpecifics* extension_specifics =
5131 app_specifics->mutable_extension();
5132 extension_specifics->set_id(good_crx);
5133 extension_specifics->set_version(
5134 service_->GetInstalledExtension(good_crx)->version()->GetString());
5135
5136 {
5137 extension_specifics->set_enabled(true);
[email protected]65f173552012-06-28 22:43:585138 syncer::SyncData sync_data =
5139 syncer::SyncData::CreateLocalData(good_crx, "Name", specifics);
[email protected]b78170f2012-07-11 03:34:265140 syncer::SyncChange sync_change(FROM_HERE,
5141 syncer::SyncChange::ACTION_DELETE,
5142 sync_data);
[email protected]65f173552012-06-28 22:43:585143 syncer::SyncChangeList list(1);
[email protected]96e989b2011-08-30 19:35:065144 list[0] = sync_change;
5145
5146 // Should do nothing
5147 service_->ProcessSyncChanges(FROM_HERE, list);
5148 EXPECT_TRUE(service_->GetExtensionById(good_crx, true));
5149 }
5150
5151 {
5152 extension_specifics->set_enabled(false);
[email protected]65f173552012-06-28 22:43:585153 syncer::SyncData sync_data =
5154 syncer::SyncData::CreateLocalData(good_crx, "Name", specifics);
[email protected]b78170f2012-07-11 03:34:265155 syncer::SyncChange sync_change(FROM_HERE,
5156 syncer::SyncChange::ACTION_UPDATE,
5157 sync_data);
[email protected]65f173552012-06-28 22:43:585158 syncer::SyncChangeList list(1);
[email protected]96e989b2011-08-30 19:35:065159 list[0] = sync_change;
5160
5161 // Should again do nothing.
5162 service_->ProcessSyncChanges(FROM_HERE, list);
5163 EXPECT_TRUE(service_->GetExtensionById(good_crx, false));
5164 }
5165}
5166
[email protected]d9a61e12012-11-14 02:43:475167TEST_F(ExtensionServiceTest, ProcessSyncDataSettings) {
[email protected]90310d92011-04-17 07:35:045168 InitializeEmptyExtensionService();
[email protected]406b5a92011-11-08 11:58:265169 InitializeExtensionProcessManager();
[email protected]3bdba0d2011-08-23 07:17:305170 TestSyncProcessorStub processor;
[email protected]cb02f612012-06-27 03:15:505171 service_->MergeDataAndStartSyncing(
[email protected]a4a147652012-07-03 23:41:325172 syncer::EXTENSIONS, syncer::SyncDataList(),
[email protected]65f173552012-06-28 22:43:585173 scoped_ptr<syncer::SyncChangeProcessor>(new TestSyncProcessorStub),
5174 scoped_ptr<syncer::SyncErrorFactory>(new syncer::SyncErrorFactoryMock()));
[email protected]90310d92011-04-17 07:35:045175
[email protected]8f512c72011-11-22 21:02:505176 InstallCRX(data_dir_.AppendASCII("good.crx"), INSTALL_NEW);
[email protected]90310d92011-04-17 07:35:045177 EXPECT_TRUE(service_->IsExtensionEnabled(good_crx));
5178 EXPECT_FALSE(service_->IsIncognitoEnabled(good_crx));
5179
[email protected]3bdba0d2011-08-23 07:17:305180 sync_pb::EntitySpecifics specifics;
[email protected]4557d222012-03-04 23:33:365181 sync_pb::ExtensionSpecifics* ext_specifics = specifics.mutable_extension();
[email protected]3bdba0d2011-08-23 07:17:305182 ext_specifics->set_id(good_crx);
5183 ext_specifics->set_version(
5184 service_->GetInstalledExtension(good_crx)->version()->GetString());
5185 ext_specifics->set_enabled(false);
[email protected]90310d92011-04-17 07:35:045186
[email protected]3bdba0d2011-08-23 07:17:305187 {
[email protected]65f173552012-06-28 22:43:585188 syncer::SyncData sync_data =
5189 syncer::SyncData::CreateLocalData(good_crx, "Name", specifics);
[email protected]b78170f2012-07-11 03:34:265190 syncer::SyncChange sync_change(FROM_HERE,
5191 syncer::SyncChange::ACTION_UPDATE,
5192 sync_data);
[email protected]65f173552012-06-28 22:43:585193 syncer::SyncChangeList list(1);
[email protected]3bdba0d2011-08-23 07:17:305194 list[0] = sync_change;
5195 service_->ProcessSyncChanges(FROM_HERE, list);
5196 EXPECT_FALSE(service_->IsExtensionEnabled(good_crx));
5197 EXPECT_FALSE(service_->IsIncognitoEnabled(good_crx));
5198 }
[email protected]90310d92011-04-17 07:35:045199
[email protected]3bdba0d2011-08-23 07:17:305200 {
5201 ext_specifics->set_enabled(true);
5202 ext_specifics->set_incognito_enabled(true);
[email protected]65f173552012-06-28 22:43:585203 syncer::SyncData sync_data =
5204 syncer::SyncData::CreateLocalData(good_crx, "Name", specifics);
[email protected]b78170f2012-07-11 03:34:265205 syncer::SyncChange sync_change(FROM_HERE,
5206 syncer::SyncChange::ACTION_UPDATE,
5207 sync_data);
[email protected]65f173552012-06-28 22:43:585208 syncer::SyncChangeList list(1);
[email protected]3bdba0d2011-08-23 07:17:305209 list[0] = sync_change;
5210 service_->ProcessSyncChanges(FROM_HERE, list);
5211 EXPECT_TRUE(service_->IsExtensionEnabled(good_crx));
5212 EXPECT_TRUE(service_->IsIncognitoEnabled(good_crx));
5213 }
[email protected]90310d92011-04-17 07:35:045214
[email protected]3bdba0d2011-08-23 07:17:305215 {
5216 ext_specifics->set_enabled(false);
5217 ext_specifics->set_incognito_enabled(true);
[email protected]65f173552012-06-28 22:43:585218 syncer::SyncData sync_data =
5219 syncer::SyncData::CreateLocalData(good_crx, "Name", specifics);
[email protected]b78170f2012-07-11 03:34:265220 syncer::SyncChange sync_change(FROM_HERE,
5221 syncer::SyncChange::ACTION_UPDATE,
5222 sync_data);
[email protected]65f173552012-06-28 22:43:585223 syncer::SyncChangeList list(1);
[email protected]3bdba0d2011-08-23 07:17:305224 list[0] = sync_change;
5225 service_->ProcessSyncChanges(FROM_HERE, list);
5226 EXPECT_FALSE(service_->IsExtensionEnabled(good_crx));
5227 EXPECT_TRUE(service_->IsIncognitoEnabled(good_crx));
5228 }
[email protected]90310d92011-04-17 07:35:045229
5230 EXPECT_FALSE(service_->pending_extension_manager()->IsIdPending(good_crx));
5231}
5232
[email protected]d9a61e12012-11-14 02:43:475233TEST_F(ExtensionServiceTest, ProcessSyncDataTerminatedExtension) {
[email protected]fa2416f2011-05-03 08:41:205234 InitializeExtensionServiceWithUpdater();
[email protected]3bdba0d2011-08-23 07:17:305235 TestSyncProcessorStub processor;
[email protected]cb02f612012-06-27 03:15:505236 service_->MergeDataAndStartSyncing(
[email protected]a4a147652012-07-03 23:41:325237 syncer::EXTENSIONS, syncer::SyncDataList(),
[email protected]65f173552012-06-28 22:43:585238 scoped_ptr<syncer::SyncChangeProcessor>(new TestSyncProcessorStub),
5239 scoped_ptr<syncer::SyncErrorFactory>(new syncer::SyncErrorFactoryMock()));
[email protected]fa2416f2011-05-03 08:41:205240
[email protected]8f512c72011-11-22 21:02:505241 InstallCRX(data_dir_.AppendASCII("good.crx"), INSTALL_NEW);
[email protected]fa2416f2011-05-03 08:41:205242 TerminateExtension(good_crx);
5243 EXPECT_TRUE(service_->IsExtensionEnabled(good_crx));
5244 EXPECT_FALSE(service_->IsIncognitoEnabled(good_crx));
5245
[email protected]3bdba0d2011-08-23 07:17:305246 sync_pb::EntitySpecifics specifics;
[email protected]4557d222012-03-04 23:33:365247 sync_pb::ExtensionSpecifics* ext_specifics = specifics.mutable_extension();
[email protected]3bdba0d2011-08-23 07:17:305248 ext_specifics->set_id(good_crx);
5249 ext_specifics->set_version(
5250 service_->GetInstalledExtension(good_crx)->version()->GetString());
5251 ext_specifics->set_enabled(false);
5252 ext_specifics->set_incognito_enabled(true);
[email protected]65f173552012-06-28 22:43:585253 syncer::SyncData sync_data =
5254 syncer::SyncData::CreateLocalData(good_crx, "Name", specifics);
[email protected]b78170f2012-07-11 03:34:265255 syncer::SyncChange sync_change(FROM_HERE,
5256 syncer::SyncChange::ACTION_UPDATE,
5257 sync_data);
[email protected]65f173552012-06-28 22:43:585258 syncer::SyncChangeList list(1);
[email protected]3bdba0d2011-08-23 07:17:305259 list[0] = sync_change;
5260
5261 service_->ProcessSyncChanges(FROM_HERE, list);
[email protected]fa2416f2011-05-03 08:41:205262 EXPECT_FALSE(service_->IsExtensionEnabled(good_crx));
5263 EXPECT_TRUE(service_->IsIncognitoEnabled(good_crx));
5264
5265 EXPECT_FALSE(service_->pending_extension_manager()->IsIdPending(good_crx));
5266}
5267
[email protected]d9a61e12012-11-14 02:43:475268TEST_F(ExtensionServiceTest, ProcessSyncDataVersionCheck) {
[email protected]90310d92011-04-17 07:35:045269 InitializeExtensionServiceWithUpdater();
[email protected]314c3e22012-02-21 03:57:425270 InitializeRequestContext();
[email protected]3bdba0d2011-08-23 07:17:305271 TestSyncProcessorStub processor;
[email protected]cb02f612012-06-27 03:15:505272 service_->MergeDataAndStartSyncing(
[email protected]a4a147652012-07-03 23:41:325273 syncer::EXTENSIONS, syncer::SyncDataList(),
[email protected]65f173552012-06-28 22:43:585274 scoped_ptr<syncer::SyncChangeProcessor>(new TestSyncProcessorStub),
5275 scoped_ptr<syncer::SyncErrorFactory>(new syncer::SyncErrorFactoryMock()));
[email protected]90310d92011-04-17 07:35:045276
[email protected]8f512c72011-11-22 21:02:505277 InstallCRX(data_dir_.AppendASCII("good.crx"), INSTALL_NEW);
[email protected]90310d92011-04-17 07:35:045278 EXPECT_TRUE(service_->IsExtensionEnabled(good_crx));
5279 EXPECT_FALSE(service_->IsIncognitoEnabled(good_crx));
5280
[email protected]3bdba0d2011-08-23 07:17:305281 sync_pb::EntitySpecifics specifics;
[email protected]4557d222012-03-04 23:33:365282 sync_pb::ExtensionSpecifics* ext_specifics = specifics.mutable_extension();
[email protected]3bdba0d2011-08-23 07:17:305283 ext_specifics->set_id(good_crx);
5284 ext_specifics->set_enabled(true);
[email protected]90310d92011-04-17 07:35:045285
[email protected]3bdba0d2011-08-23 07:17:305286 {
5287 ext_specifics->set_version(
5288 service_->GetInstalledExtension(good_crx)->version()->GetString());
[email protected]65f173552012-06-28 22:43:585289 syncer::SyncData sync_data =
5290 syncer::SyncData::CreateLocalData(good_crx, "Name", specifics);
[email protected]b78170f2012-07-11 03:34:265291 syncer::SyncChange sync_change(FROM_HERE,
5292 syncer::SyncChange::ACTION_UPDATE,
5293 sync_data);
[email protected]65f173552012-06-28 22:43:585294 syncer::SyncChangeList list(1);
[email protected]3bdba0d2011-08-23 07:17:305295 list[0] = sync_change;
5296
5297 // Should do nothing if extension version == sync version.
5298 service_->ProcessSyncChanges(FROM_HERE, list);
5299 EXPECT_FALSE(service_->updater()->WillCheckSoon());
5300 }
[email protected]90310d92011-04-17 07:35:045301
5302 // Should do nothing if extension version > sync version (but see
[email protected]3bdba0d2011-08-23 07:17:305303 // the TODO in ProcessExtensionSyncData).
[email protected]90310d92011-04-17 07:35:045304 {
[email protected]3bdba0d2011-08-23 07:17:305305 ext_specifics->set_version("0.0.0.0");
[email protected]65f173552012-06-28 22:43:585306 syncer::SyncData sync_data =
5307 syncer::SyncData::CreateLocalData(good_crx, "Name", specifics);
[email protected]b78170f2012-07-11 03:34:265308 syncer::SyncChange sync_change(FROM_HERE,
5309 syncer::SyncChange::ACTION_UPDATE,
5310 sync_data);
[email protected]65f173552012-06-28 22:43:585311 syncer::SyncChangeList list(1);
[email protected]3bdba0d2011-08-23 07:17:305312 list[0] = sync_change;
5313
5314 service_->ProcessSyncChanges(FROM_HERE, list);
[email protected]90310d92011-04-17 07:35:045315 EXPECT_FALSE(service_->updater()->WillCheckSoon());
5316 }
5317
5318 // Should kick off an update if extension version < sync version.
5319 {
[email protected]3bdba0d2011-08-23 07:17:305320 ext_specifics->set_version("9.9.9.9");
[email protected]65f173552012-06-28 22:43:585321 syncer::SyncData sync_data =
5322 syncer::SyncData::CreateLocalData(good_crx, "Name", specifics);
[email protected]b78170f2012-07-11 03:34:265323 syncer::SyncChange sync_change(FROM_HERE,
5324 syncer::SyncChange::ACTION_UPDATE,
5325 sync_data);
[email protected]65f173552012-06-28 22:43:585326 syncer::SyncChangeList list(1);
[email protected]3bdba0d2011-08-23 07:17:305327 list[0] = sync_change;
5328
5329 service_->ProcessSyncChanges(FROM_HERE, list);
[email protected]90310d92011-04-17 07:35:045330 EXPECT_TRUE(service_->updater()->WillCheckSoon());
5331 }
5332
5333 EXPECT_FALSE(service_->pending_extension_manager()->IsIdPending(good_crx));
5334}
5335
[email protected]d9a61e12012-11-14 02:43:475336TEST_F(ExtensionServiceTest, ProcessSyncDataNotInstalled) {
[email protected]90310d92011-04-17 07:35:045337 InitializeExtensionServiceWithUpdater();
[email protected]314c3e22012-02-21 03:57:425338 InitializeRequestContext();
[email protected]3bdba0d2011-08-23 07:17:305339 TestSyncProcessorStub processor;
[email protected]cb02f612012-06-27 03:15:505340 service_->MergeDataAndStartSyncing(
[email protected]a4a147652012-07-03 23:41:325341 syncer::EXTENSIONS, syncer::SyncDataList(),
[email protected]65f173552012-06-28 22:43:585342 scoped_ptr<syncer::SyncChangeProcessor>(new TestSyncProcessorStub),
5343 scoped_ptr<syncer::SyncErrorFactory>(new syncer::SyncErrorFactoryMock()));
[email protected]90310d92011-04-17 07:35:045344
[email protected]3bdba0d2011-08-23 07:17:305345 sync_pb::EntitySpecifics specifics;
[email protected]4557d222012-03-04 23:33:365346 sync_pb::ExtensionSpecifics* ext_specifics = specifics.mutable_extension();
[email protected]3bdba0d2011-08-23 07:17:305347 ext_specifics->set_id(good_crx);
5348 ext_specifics->set_enabled(false);
5349 ext_specifics->set_incognito_enabled(true);
5350 ext_specifics->set_update_url("http://www.google.com/");
5351 ext_specifics->set_version("1.2.3.4");
[email protected]65f173552012-06-28 22:43:585352 syncer::SyncData sync_data =
5353 syncer::SyncData::CreateLocalData(good_crx, "Name", specifics);
[email protected]b78170f2012-07-11 03:34:265354 syncer::SyncChange sync_change(FROM_HERE,
5355 syncer::SyncChange::ACTION_UPDATE,
5356 sync_data);
[email protected]65f173552012-06-28 22:43:585357 syncer::SyncChangeList list(1);
[email protected]3bdba0d2011-08-23 07:17:305358 list[0] = sync_change;
5359
[email protected]90310d92011-04-17 07:35:045360
[email protected]06f92562011-04-29 19:27:315361 EXPECT_TRUE(service_->IsExtensionEnabled(good_crx));
[email protected]81b14cc2011-04-29 00:39:375362 EXPECT_FALSE(service_->IsIncognitoEnabled(good_crx));
[email protected]3bdba0d2011-08-23 07:17:305363 service_->ProcessSyncChanges(FROM_HERE, list);
[email protected]90310d92011-04-17 07:35:045364 EXPECT_TRUE(service_->updater()->WillCheckSoon());
[email protected]06f92562011-04-29 19:27:315365 EXPECT_FALSE(service_->IsExtensionEnabled(good_crx));
[email protected]81b14cc2011-04-29 00:39:375366 EXPECT_TRUE(service_->IsIncognitoEnabled(good_crx));
[email protected]90310d92011-04-17 07:35:045367
[email protected]3f213ad2012-07-26 23:39:415368 const extensions::PendingExtensionInfo* info;
[email protected]51a3bf8b2012-06-08 22:53:065369 EXPECT_TRUE((info = service_->pending_extension_manager()->
5370 GetById(good_crx)));
5371 EXPECT_EQ(ext_specifics->update_url(), info->update_url().spec());
5372 EXPECT_TRUE(info->is_from_sync());
5373 EXPECT_TRUE(info->install_silently());
[email protected]1d5e58b2013-01-31 08:41:405374 EXPECT_EQ(Manifest::INTERNAL, info->install_source());
[email protected]90310d92011-04-17 07:35:045375 // TODO(akalin): Figure out a way to test |info.ShouldAllowInstall()|.
5376}
5377
[email protected]d9a61e12012-11-14 02:43:475378TEST_F(ExtensionServiceTest, InstallPriorityExternalUpdateUrl) {
[email protected]8a87a5332011-08-11 17:54:595379 InitializeEmptyExtensionService();
5380
[email protected]650b2d52013-02-10 03:41:455381 base::FilePath path = data_dir_.AppendASCII("good.crx");
[email protected]8f512c72011-11-22 21:02:505382 InstallCRX(path, INSTALL_NEW);
[email protected]8a87a5332011-08-11 17:54:595383 ValidatePrefKeyCount(1u);
5384 ValidateIntegerPref(good_crx, "state", Extension::ENABLED);
[email protected]1d5e58b2013-01-31 08:41:405385 ValidateIntegerPref(good_crx, "location", Manifest::INTERNAL);
[email protected]8a87a5332011-08-11 17:54:595386
[email protected]3f213ad2012-07-26 23:39:415387 extensions::PendingExtensionManager* pending =
5388 service_->pending_extension_manager();
[email protected]8a87a5332011-08-11 17:54:595389 EXPECT_FALSE(pending->IsIdPending(kGoodId));
5390
5391 // Skip install when the location is the same.
[email protected]9060d8b02012-01-13 02:14:305392 EXPECT_FALSE(
5393 service_->OnExternalExtensionUpdateUrlFound(
[email protected]1d5e58b2013-01-31 08:41:405394 kGoodId, GURL(kGoodUpdateURL), Manifest::INTERNAL));
[email protected]8a87a5332011-08-11 17:54:595395 EXPECT_FALSE(pending->IsIdPending(kGoodId));
[email protected]9060d8b02012-01-13 02:14:305396
5397 // Install when the location has higher priority.
5398 EXPECT_TRUE(
5399 service_->OnExternalExtensionUpdateUrlFound(
[email protected]1d5e58b2013-01-31 08:41:405400 kGoodId, GURL(kGoodUpdateURL), Manifest::EXTERNAL_POLICY_DOWNLOAD));
[email protected]8a87a5332011-08-11 17:54:595401 EXPECT_TRUE(pending->IsIdPending(kGoodId));
[email protected]9060d8b02012-01-13 02:14:305402
5403 // Try the low priority again. Should be rejected.
5404 EXPECT_FALSE(
5405 service_->OnExternalExtensionUpdateUrlFound(
[email protected]1d5e58b2013-01-31 08:41:405406 kGoodId, GURL(kGoodUpdateURL), Manifest::EXTERNAL_PREF_DOWNLOAD));
[email protected]9060d8b02012-01-13 02:14:305407 // The existing record should still be present in the pending extension
5408 // manager.
5409 EXPECT_TRUE(pending->IsIdPending(kGoodId));
5410
[email protected]8a87a5332011-08-11 17:54:595411 pending->Remove(kGoodId);
[email protected]9060d8b02012-01-13 02:14:305412
5413 // Skip install when the location has the same priority as the installed
5414 // location.
5415 EXPECT_FALSE(service_->OnExternalExtensionUpdateUrlFound(
[email protected]1d5e58b2013-01-31 08:41:405416 kGoodId, GURL(kGoodUpdateURL), Manifest::INTERNAL));
[email protected]9060d8b02012-01-13 02:14:305417
[email protected]8a87a5332011-08-11 17:54:595418 EXPECT_FALSE(pending->IsIdPending(kGoodId));
5419}
5420
[email protected]d9a61e12012-11-14 02:43:475421TEST_F(ExtensionServiceTest, InstallPriorityExternalLocalFile) {
[email protected]12126d372012-07-11 18:40:535422 Version older_version("0.1.0.0");
5423 Version newer_version("2.0.0.0");
[email protected]9060d8b02012-01-13 02:14:305424
5425 // We don't want the extension to be installed. A path that doesn't
5426 // point to a valid CRX ensures this.
[email protected]650b2d52013-02-10 03:41:455427 const base::FilePath kInvalidPathToCrx = base::FilePath();
[email protected]9060d8b02012-01-13 02:14:305428
5429 const int kCreationFlags = 0;
5430 const bool kDontMarkAcknowledged = false;
5431
5432 InitializeEmptyExtensionService();
5433
5434 // The test below uses install source constants to test that
5435 // priority is enforced. It assumes a specific ranking of install
5436 // sources: Registry (EXTERNAL_REGISTRY) overrides external pref
5437 // (EXTERNAL_PREF), and external pref overrides user install (INTERNAL).
5438 // The following assertions verify these assumptions:
[email protected]1d5e58b2013-01-31 08:41:405439 ASSERT_EQ(Manifest::EXTERNAL_REGISTRY,
5440 Manifest::GetHigherPriorityLocation(Manifest::EXTERNAL_REGISTRY,
5441 Manifest::EXTERNAL_PREF));
5442 ASSERT_EQ(Manifest::EXTERNAL_REGISTRY,
5443 Manifest::GetHigherPriorityLocation(Manifest::EXTERNAL_REGISTRY,
5444 Manifest::INTERNAL));
5445 ASSERT_EQ(Manifest::EXTERNAL_PREF,
5446 Manifest::GetHigherPriorityLocation(Manifest::EXTERNAL_PREF,
5447 Manifest::INTERNAL));
[email protected]9060d8b02012-01-13 02:14:305448
[email protected]3f213ad2012-07-26 23:39:415449 extensions::PendingExtensionManager* pending =
5450 service_->pending_extension_manager();
[email protected]9060d8b02012-01-13 02:14:305451 EXPECT_FALSE(pending->IsIdPending(kGoodId));
5452
5453 // Simulate an external source adding the extension as INTERNAL.
5454 EXPECT_TRUE(
5455 service_->OnExternalExtensionFileFound(
[email protected]12126d372012-07-11 18:40:535456 kGoodId, &older_version, kInvalidPathToCrx,
[email protected]1d5e58b2013-01-31 08:41:405457 Manifest::INTERNAL, kCreationFlags, kDontMarkAcknowledged));
[email protected]9060d8b02012-01-13 02:14:305458 EXPECT_TRUE(pending->IsIdPending(kGoodId));
5459 WaitForCrxInstall(kInvalidPathToCrx, INSTALL_FAILED);
5460
5461 // Simulate an external source adding the extension as EXTERNAL_PREF.
5462 EXPECT_TRUE(
5463 service_->OnExternalExtensionFileFound(
[email protected]12126d372012-07-11 18:40:535464 kGoodId, &older_version, kInvalidPathToCrx,
[email protected]1d5e58b2013-01-31 08:41:405465 Manifest::EXTERNAL_PREF, kCreationFlags, kDontMarkAcknowledged));
[email protected]9060d8b02012-01-13 02:14:305466 EXPECT_TRUE(pending->IsIdPending(kGoodId));
5467 WaitForCrxInstall(kInvalidPathToCrx, INSTALL_FAILED);
5468
5469 // Simulate an external source adding as EXTERNAL_PREF again.
[email protected]4ec0f4b2012-12-07 09:41:465470 // This is rejected because the version and the location are the same as
5471 // the previous installation, which is still pending.
5472 EXPECT_FALSE(
[email protected]9060d8b02012-01-13 02:14:305473 service_->OnExternalExtensionFileFound(
[email protected]12126d372012-07-11 18:40:535474 kGoodId, &older_version, kInvalidPathToCrx,
[email protected]1d5e58b2013-01-31 08:41:405475 Manifest::EXTERNAL_PREF, kCreationFlags, kDontMarkAcknowledged));
[email protected]9060d8b02012-01-13 02:14:305476 EXPECT_TRUE(pending->IsIdPending(kGoodId));
[email protected]9060d8b02012-01-13 02:14:305477
5478 // Try INTERNAL again. Should fail.
5479 EXPECT_FALSE(
5480 service_->OnExternalExtensionFileFound(
[email protected]12126d372012-07-11 18:40:535481 kGoodId, &older_version, kInvalidPathToCrx,
[email protected]1d5e58b2013-01-31 08:41:405482 Manifest::INTERNAL, kCreationFlags, kDontMarkAcknowledged));
[email protected]9060d8b02012-01-13 02:14:305483 EXPECT_TRUE(pending->IsIdPending(kGoodId));
5484
5485 // Now the registry adds the extension.
5486 EXPECT_TRUE(
5487 service_->OnExternalExtensionFileFound(
[email protected]12126d372012-07-11 18:40:535488 kGoodId, &older_version, kInvalidPathToCrx,
[email protected]1d5e58b2013-01-31 08:41:405489 Manifest::EXTERNAL_REGISTRY, kCreationFlags, kDontMarkAcknowledged));
[email protected]9060d8b02012-01-13 02:14:305490 EXPECT_TRUE(pending->IsIdPending(kGoodId));
5491 WaitForCrxInstall(kInvalidPathToCrx, INSTALL_FAILED);
5492
5493 // Registry outranks both external pref and internal, so both fail.
5494 EXPECT_FALSE(
5495 service_->OnExternalExtensionFileFound(
[email protected]12126d372012-07-11 18:40:535496 kGoodId, &older_version, kInvalidPathToCrx,
[email protected]1d5e58b2013-01-31 08:41:405497 Manifest::EXTERNAL_PREF, kCreationFlags, kDontMarkAcknowledged));
[email protected]9060d8b02012-01-13 02:14:305498 EXPECT_TRUE(pending->IsIdPending(kGoodId));
5499
5500 EXPECT_FALSE(
5501 service_->OnExternalExtensionFileFound(
[email protected]12126d372012-07-11 18:40:535502 kGoodId, &older_version, kInvalidPathToCrx,
[email protected]1d5e58b2013-01-31 08:41:405503 Manifest::INTERNAL, kCreationFlags, kDontMarkAcknowledged));
[email protected]9060d8b02012-01-13 02:14:305504 EXPECT_TRUE(pending->IsIdPending(kGoodId));
5505
5506 pending->Remove(kGoodId);
5507
5508 // Install the extension.
[email protected]650b2d52013-02-10 03:41:455509 base::FilePath path = data_dir_.AppendASCII("good.crx");
[email protected]9060d8b02012-01-13 02:14:305510 const Extension* ext = InstallCRX(path, INSTALL_NEW);
5511 ValidatePrefKeyCount(1u);
5512 ValidateIntegerPref(good_crx, "state", Extension::ENABLED);
[email protected]1d5e58b2013-01-31 08:41:405513 ValidateIntegerPref(good_crx, "location", Manifest::INTERNAL);
[email protected]9060d8b02012-01-13 02:14:305514
5515 // Now test the logic of OnExternalExtensionFileFound() when the extension
5516 // being added is already installed.
5517
5518 // Tests assume |older_version| is less than the installed version, and
5519 // |newer_version| is greater. Verify this:
[email protected]12126d372012-07-11 18:40:535520 ASSERT_TRUE(older_version.IsOlderThan(ext->VersionString()));
5521 ASSERT_TRUE(ext->version()->IsOlderThan(newer_version.GetString()));
[email protected]9060d8b02012-01-13 02:14:305522
5523 // An external install for the same location should fail if the version is
5524 // older, or the same, and succeed if the version is newer.
5525
5526 // Older than the installed version...
5527 EXPECT_FALSE(
5528 service_->OnExternalExtensionFileFound(
[email protected]12126d372012-07-11 18:40:535529 kGoodId, &older_version, kInvalidPathToCrx,
[email protected]1d5e58b2013-01-31 08:41:405530 Manifest::INTERNAL, kCreationFlags, kDontMarkAcknowledged));
[email protected]9060d8b02012-01-13 02:14:305531 EXPECT_FALSE(pending->IsIdPending(kGoodId));
5532
5533 // Same version as the installed version...
5534 EXPECT_FALSE(
5535 service_->OnExternalExtensionFileFound(
5536 kGoodId, ext->version(), kInvalidPathToCrx,
[email protected]1d5e58b2013-01-31 08:41:405537 Manifest::INTERNAL, kCreationFlags, kDontMarkAcknowledged));
[email protected]9060d8b02012-01-13 02:14:305538 EXPECT_FALSE(pending->IsIdPending(kGoodId));
5539
5540 // Newer than the installed version...
5541 EXPECT_TRUE(
5542 service_->OnExternalExtensionFileFound(
[email protected]12126d372012-07-11 18:40:535543 kGoodId, &newer_version, kInvalidPathToCrx,
[email protected]1d5e58b2013-01-31 08:41:405544 Manifest::INTERNAL, kCreationFlags, kDontMarkAcknowledged));
[email protected]9060d8b02012-01-13 02:14:305545 EXPECT_TRUE(pending->IsIdPending(kGoodId));
5546
5547 // An external install for a higher priority install source should succeed
5548 // if the version is greater. |older_version| is not...
5549 EXPECT_FALSE(
5550 service_->OnExternalExtensionFileFound(
[email protected]12126d372012-07-11 18:40:535551 kGoodId, &older_version, kInvalidPathToCrx,
[email protected]1d5e58b2013-01-31 08:41:405552 Manifest::EXTERNAL_PREF, kCreationFlags, kDontMarkAcknowledged));
[email protected]9060d8b02012-01-13 02:14:305553 EXPECT_TRUE(pending->IsIdPending(kGoodId));
5554
5555 // |newer_version| is newer.
5556 EXPECT_TRUE(
5557 service_->OnExternalExtensionFileFound(
[email protected]12126d372012-07-11 18:40:535558 kGoodId, &newer_version, kInvalidPathToCrx,
[email protected]1d5e58b2013-01-31 08:41:405559 Manifest::EXTERNAL_PREF, kCreationFlags, kDontMarkAcknowledged));
[email protected]9060d8b02012-01-13 02:14:305560 EXPECT_TRUE(pending->IsIdPending(kGoodId));
5561
5562 // An external install for an even higher priority install source should
5563 // succeed if the version is greater.
5564 EXPECT_TRUE(
5565 service_->OnExternalExtensionFileFound(
[email protected]12126d372012-07-11 18:40:535566 kGoodId, &newer_version, kInvalidPathToCrx,
[email protected]1d5e58b2013-01-31 08:41:405567 Manifest::EXTERNAL_REGISTRY, kCreationFlags, kDontMarkAcknowledged));
[email protected]9060d8b02012-01-13 02:14:305568 EXPECT_TRUE(pending->IsIdPending(kGoodId));
5569
5570 // Because EXTERNAL_PREF is a lower priority source than EXTERNAL_REGISTRY,
5571 // adding from external pref will now fail.
5572 EXPECT_FALSE(
5573 service_->OnExternalExtensionFileFound(
[email protected]12126d372012-07-11 18:40:535574 kGoodId, &newer_version, kInvalidPathToCrx,
[email protected]1d5e58b2013-01-31 08:41:405575 Manifest::EXTERNAL_PREF, kCreationFlags, kDontMarkAcknowledged));
[email protected]9060d8b02012-01-13 02:14:305576 EXPECT_TRUE(pending->IsIdPending(kGoodId));
5577}
5578
[email protected]d9a61e12012-11-14 02:43:475579TEST_F(ExtensionServiceTest, ConcurrentExternalLocalFile) {
[email protected]e3987852012-05-04 10:06:305580 Version kVersion123("1.2.3");
5581 Version kVersion124("1.2.4");
5582 Version kVersion125("1.2.5");
[email protected]650b2d52013-02-10 03:41:455583 const base::FilePath kInvalidPathToCrx = base::FilePath();
[email protected]e3987852012-05-04 10:06:305584 const int kCreationFlags = 0;
5585 const bool kDontMarkAcknowledged = false;
5586
5587 InitializeEmptyExtensionService();
5588
[email protected]3f213ad2012-07-26 23:39:415589 extensions::PendingExtensionManager* pending =
5590 service_->pending_extension_manager();
[email protected]e3987852012-05-04 10:06:305591 EXPECT_FALSE(pending->IsIdPending(kGoodId));
5592
5593 // An external provider starts installing from a local crx.
5594 EXPECT_TRUE(
5595 service_->OnExternalExtensionFileFound(
5596 kGoodId, &kVersion123, kInvalidPathToCrx,
[email protected]1d5e58b2013-01-31 08:41:405597 Manifest::EXTERNAL_PREF, kCreationFlags, kDontMarkAcknowledged));
[email protected]3f213ad2012-07-26 23:39:415598 const extensions::PendingExtensionInfo* info;
[email protected]51a3bf8b2012-06-08 22:53:065599 EXPECT_TRUE((info = pending->GetById(kGoodId)));
5600 EXPECT_TRUE(info->version().IsValid());
5601 EXPECT_TRUE(info->version().Equals(kVersion123));
[email protected]e3987852012-05-04 10:06:305602
5603 // Adding a newer version overrides the currently pending version.
5604 EXPECT_TRUE(
5605 service_->OnExternalExtensionFileFound(
5606 kGoodId, &kVersion124, kInvalidPathToCrx,
[email protected]1d5e58b2013-01-31 08:41:405607 Manifest::EXTERNAL_PREF, kCreationFlags, kDontMarkAcknowledged));
[email protected]51a3bf8b2012-06-08 22:53:065608 EXPECT_TRUE((info = pending->GetById(kGoodId)));
5609 EXPECT_TRUE(info->version().IsValid());
5610 EXPECT_TRUE(info->version().Equals(kVersion124));
[email protected]e3987852012-05-04 10:06:305611
5612 // Adding an older version fails.
5613 EXPECT_FALSE(
5614 service_->OnExternalExtensionFileFound(
5615 kGoodId, &kVersion123, kInvalidPathToCrx,
[email protected]1d5e58b2013-01-31 08:41:405616 Manifest::EXTERNAL_PREF, kCreationFlags, kDontMarkAcknowledged));
[email protected]51a3bf8b2012-06-08 22:53:065617 EXPECT_TRUE((info = pending->GetById(kGoodId)));
5618 EXPECT_TRUE(info->version().IsValid());
5619 EXPECT_TRUE(info->version().Equals(kVersion124));
[email protected]e3987852012-05-04 10:06:305620
5621 // Adding an older version fails even when coming from a higher-priority
5622 // location.
5623 EXPECT_FALSE(
5624 service_->OnExternalExtensionFileFound(
5625 kGoodId, &kVersion123, kInvalidPathToCrx,
[email protected]1d5e58b2013-01-31 08:41:405626 Manifest::EXTERNAL_REGISTRY, kCreationFlags, kDontMarkAcknowledged));
[email protected]51a3bf8b2012-06-08 22:53:065627 EXPECT_TRUE((info = pending->GetById(kGoodId)));
5628 EXPECT_TRUE(info->version().IsValid());
5629 EXPECT_TRUE(info->version().Equals(kVersion124));
[email protected]e3987852012-05-04 10:06:305630
5631 // Adding the latest version from the webstore overrides a specific version.
5632 GURL kUpdateUrl("http://example.com/update");
5633 EXPECT_TRUE(
5634 service_->OnExternalExtensionUpdateUrlFound(
[email protected]1d5e58b2013-01-31 08:41:405635 kGoodId, kUpdateUrl, Manifest::EXTERNAL_POLICY_DOWNLOAD));
[email protected]51a3bf8b2012-06-08 22:53:065636 EXPECT_TRUE((info = pending->GetById(kGoodId)));
5637 EXPECT_FALSE(info->version().IsValid());
[email protected]e3987852012-05-04 10:06:305638}
5639
[email protected]f5bf1842012-02-15 02:52:265640// This makes sure we can package and install CRX files that use whitelisted
5641// permissions.
[email protected]d9a61e12012-11-14 02:43:475642TEST_F(ExtensionServiceTest, InstallWhitelistedExtension) {
[email protected]f5bf1842012-02-15 02:52:265643 std::string test_id = "hdkklepkcpckhnpgjnmbdfhehckloojk";
5644 CommandLine::ForCurrentProcess()->AppendSwitchASCII(
5645 switches::kWhitelistedExtensionID, test_id);
5646
5647 InitializeEmptyExtensionService();
[email protected]650b2d52013-02-10 03:41:455648 base::FilePath path = data_dir_
[email protected]f5bf1842012-02-15 02:52:265649 .AppendASCII("permissions");
[email protected]650b2d52013-02-10 03:41:455650 base::FilePath pem_path = path
[email protected]f5bf1842012-02-15 02:52:265651 .AppendASCII("whitelist.pem");
5652 path = path
5653 .AppendASCII("whitelist");
5654
5655 const Extension* extension = PackAndInstallCRX(path, pem_path, INSTALL_NEW);
5656 EXPECT_EQ(0u, GetErrors().size());
5657 ASSERT_EQ(1u, service_->extensions()->size());
5658 EXPECT_EQ(test_id, extension->id());
5659}
5660
[email protected]145a317b2011-04-12 16:03:465661// Test that when multiple sources try to install an extension,
5662// we consistently choose the right one. To make tests easy to read,
5663// methods that fake requests to install crx files in several ways
5664// are provided.
5665class ExtensionSourcePriorityTest : public ExtensionServiceTest {
5666 public:
[email protected]49aeab62013-02-07 02:53:115667 virtual void SetUp() {
[email protected]ed8ee722011-04-22 06:49:445668 ExtensionServiceTest::SetUp();
5669
[email protected]145a317b2011-04-12 16:03:465670 // All tests use a single extension. Put the id and path in member vars
5671 // that all methods can read.
5672 crx_id_ = kGoodId;
[email protected]e85e34c32011-04-13 18:38:355673 crx_path_ = data_dir_.AppendASCII("good.crx");
[email protected]145a317b2011-04-12 16:03:465674 }
5675
5676 // Fake an external source adding a URL to fetch an extension from.
[email protected]9060d8b02012-01-13 02:14:305677 bool AddPendingExternalPrefUrl() {
5678 return service_->pending_extension_manager()->AddFromExternalUpdateUrl(
[email protected]1d5e58b2013-01-31 08:41:405679 crx_id_, GURL(), Manifest::EXTERNAL_PREF_DOWNLOAD);
[email protected]145a317b2011-04-12 16:03:465680 }
5681
5682 // Fake an external file from external_extensions.json.
[email protected]9060d8b02012-01-13 02:14:305683 bool AddPendingExternalPrefFileInstall() {
[email protected]12126d372012-07-11 18:40:535684 Version version("1.0.0.0");
[email protected]145a317b2011-04-12 16:03:465685
[email protected]9060d8b02012-01-13 02:14:305686 return service_->OnExternalExtensionFileFound(
[email protected]1d5e58b2013-01-31 08:41:405687 crx_id_, &version, crx_path_, Manifest::EXTERNAL_PREF,
[email protected]47fc70c2011-12-06 07:29:515688 Extension::NO_FLAGS, false);
[email protected]145a317b2011-04-12 16:03:465689 }
5690
5691 // Fake a request from sync to install an extension.
5692 bool AddPendingSyncInstall() {
5693 return service_->pending_extension_manager()->AddFromSync(
[email protected]6cc7dbae2011-04-29 21:18:335694 crx_id_, GURL(kGoodUpdateURL), &IsExtension, kGoodInstallSilently);
[email protected]145a317b2011-04-12 16:03:465695 }
5696
[email protected]145a317b2011-04-12 16:03:465697 // Fake a policy install.
[email protected]9060d8b02012-01-13 02:14:305698 bool AddPendingPolicyInstall() {
[email protected]145a317b2011-04-12 16:03:465699 // Get path to the CRX with id |kGoodId|.
[email protected]9060d8b02012-01-13 02:14:305700 return service_->OnExternalExtensionUpdateUrlFound(
[email protected]1d5e58b2013-01-31 08:41:405701 crx_id_, GURL(), Manifest::EXTERNAL_POLICY_DOWNLOAD);
[email protected]145a317b2011-04-12 16:03:465702 }
5703
5704 // Get the install source of a pending extension.
[email protected]1d5e58b2013-01-31 08:41:405705 Manifest::Location GetPendingLocation() {
[email protected]3f213ad2012-07-26 23:39:415706 const extensions::PendingExtensionInfo* info;
[email protected]51a3bf8b2012-06-08 22:53:065707 EXPECT_TRUE((info = service_->pending_extension_manager()->
5708 GetById(crx_id_)));
5709 return info->install_source();
[email protected]145a317b2011-04-12 16:03:465710 }
5711
5712 // Is an extension pending from a sync request?
5713 bool GetPendingIsFromSync() {
[email protected]3f213ad2012-07-26 23:39:415714 const extensions::PendingExtensionInfo* info;
[email protected]51a3bf8b2012-06-08 22:53:065715 EXPECT_TRUE((info = service_->pending_extension_manager()->
5716 GetById(crx_id_)));
5717 return info->is_from_sync();
[email protected]145a317b2011-04-12 16:03:465718 }
5719
5720 // Is the CRX id these tests use pending?
5721 bool IsCrxPending() {
5722 return service_->pending_extension_manager()->IsIdPending(crx_id_);
5723 }
5724
5725 // Is an extension installed?
5726 bool IsCrxInstalled() {
5727 return (service_->GetExtensionById(crx_id_, true) != NULL);
5728 }
5729
5730 protected:
5731 // All tests use a single extension. Making the id and path member
5732 // vars avoids pasing the same argument to every method.
5733 std::string crx_id_;
[email protected]650b2d52013-02-10 03:41:455734 base::FilePath crx_path_;
[email protected]145a317b2011-04-12 16:03:465735};
5736
[email protected]3634ebd2011-04-20 00:34:125737// Test that a pending request for installation of an external CRX from
5738// an update URL overrides a pending request to install the same extension
5739// from sync.
[email protected]ed8ee722011-04-22 06:49:445740TEST_F(ExtensionSourcePriorityTest, PendingExternalFileOverSync) {
[email protected]145a317b2011-04-12 16:03:465741 InitializeEmptyExtensionService();
5742
5743 ASSERT_FALSE(IsCrxInstalled());
5744
5745 // Install pending extension from sync.
[email protected]f4d5e1a2011-04-28 02:08:255746 EXPECT_TRUE(AddPendingSyncInstall());
[email protected]1d5e58b2013-01-31 08:41:405747 ASSERT_EQ(Manifest::INTERNAL, GetPendingLocation());
[email protected]145a317b2011-04-12 16:03:465748 EXPECT_TRUE(GetPendingIsFromSync());
5749 ASSERT_FALSE(IsCrxInstalled());
5750
5751 // Install pending as external prefs json would.
5752 AddPendingExternalPrefFileInstall();
[email protected]1d5e58b2013-01-31 08:41:405753 ASSERT_EQ(Manifest::EXTERNAL_PREF, GetPendingLocation());
[email protected]145a317b2011-04-12 16:03:465754 ASSERT_FALSE(IsCrxInstalled());
5755
5756 // Another request from sync should be ignorred.
[email protected]f4d5e1a2011-04-28 02:08:255757 EXPECT_FALSE(AddPendingSyncInstall());
[email protected]1d5e58b2013-01-31 08:41:405758 ASSERT_EQ(Manifest::EXTERNAL_PREF, GetPendingLocation());
[email protected]145a317b2011-04-12 16:03:465759 ASSERT_FALSE(IsCrxInstalled());
5760
[email protected]8f512c72011-11-22 21:02:505761 WaitForCrxInstall(crx_path_, INSTALL_NEW);
[email protected]145a317b2011-04-12 16:03:465762 ASSERT_TRUE(IsCrxInstalled());
5763}
5764
5765// Test that an install of an external CRX from an update overrides
5766// an install of the same extension from sync.
[email protected]ed8ee722011-04-22 06:49:445767TEST_F(ExtensionSourcePriorityTest, PendingExternalUrlOverSync) {
[email protected]145a317b2011-04-12 16:03:465768 InitializeEmptyExtensionService();
5769 ASSERT_FALSE(IsCrxInstalled());
5770
[email protected]f4d5e1a2011-04-28 02:08:255771 EXPECT_TRUE(AddPendingSyncInstall());
[email protected]1d5e58b2013-01-31 08:41:405772 ASSERT_EQ(Manifest::INTERNAL, GetPendingLocation());
[email protected]145a317b2011-04-12 16:03:465773 EXPECT_TRUE(GetPendingIsFromSync());
5774 ASSERT_FALSE(IsCrxInstalled());
5775
[email protected]9060d8b02012-01-13 02:14:305776 ASSERT_TRUE(AddPendingExternalPrefUrl());
[email protected]1d5e58b2013-01-31 08:41:405777 ASSERT_EQ(Manifest::EXTERNAL_PREF_DOWNLOAD, GetPendingLocation());
[email protected]145a317b2011-04-12 16:03:465778 EXPECT_FALSE(GetPendingIsFromSync());
5779 ASSERT_FALSE(IsCrxInstalled());
5780
[email protected]f4d5e1a2011-04-28 02:08:255781 EXPECT_FALSE(AddPendingSyncInstall());
[email protected]1d5e58b2013-01-31 08:41:405782 ASSERT_EQ(Manifest::EXTERNAL_PREF_DOWNLOAD, GetPendingLocation());
[email protected]145a317b2011-04-12 16:03:465783 EXPECT_FALSE(GetPendingIsFromSync());
5784 ASSERT_FALSE(IsCrxInstalled());
5785}
5786
[email protected]145a317b2011-04-12 16:03:465787// Test that an external install request stops sync from installing
5788// the same extension.
[email protected]ed8ee722011-04-22 06:49:445789TEST_F(ExtensionSourcePriorityTest, InstallExternalBlocksSyncRequest) {
[email protected]145a317b2011-04-12 16:03:465790 InitializeEmptyExtensionService();
5791 ASSERT_FALSE(IsCrxInstalled());
5792
5793 // External prefs starts an install.
5794 AddPendingExternalPrefFileInstall();
5795
5796 // Crx installer was made, but has not yet run.
5797 ASSERT_FALSE(IsCrxInstalled());
5798
5799 // Before the CRX installer runs, Sync requests that the same extension
5800 // be installed. Should fail, because an external source is pending.
5801 ASSERT_FALSE(AddPendingSyncInstall());
5802
5803 // Wait for the external source to install.
[email protected]8f512c72011-11-22 21:02:505804 WaitForCrxInstall(crx_path_, INSTALL_NEW);
[email protected]145a317b2011-04-12 16:03:465805 ASSERT_TRUE(IsCrxInstalled());
5806
5807 // Now that the extension is installed, sync request should fail
5808 // because the extension is already installed.
5809 ASSERT_FALSE(AddPendingSyncInstall());
5810}
[email protected]07c9f2f42012-02-29 18:45:225811
[email protected]612a1cb12012-10-17 13:18:035812// Test that installing an external extension displays a GlobalError.
[email protected]d9a61e12012-11-14 02:43:475813TEST_F(ExtensionServiceTest, ExternalInstallGlobalError) {
[email protected]00b5d0a52012-10-30 13:13:535814 FeatureSwitch::ScopedOverride prompt(
5815 FeatureSwitch::prompt_for_external_extensions(), true);
[email protected]612a1cb12012-10-17 13:18:035816
[email protected]07c9f2f42012-02-29 18:45:225817 InitializeEmptyExtensionService();
[email protected]07c9f2f42012-02-29 18:45:225818 MockExtensionProvider* provider =
[email protected]1d5e58b2013-01-31 08:41:405819 new MockExtensionProvider(service_, Manifest::EXTERNAL_PREF);
[email protected]07c9f2f42012-02-29 18:45:225820 AddMockExternalProvider(provider);
5821
[email protected]612a1cb12012-10-17 13:18:035822 service_->UpdateExternalExtensionAlert();
[email protected]07c9f2f42012-02-29 18:45:225823 // Should return false, meaning there aren't any extensions that the user
5824 // needs to know about.
[email protected]612a1cb12012-10-17 13:18:035825 EXPECT_FALSE(extensions::HasExternalInstallError(service_));
[email protected]07c9f2f42012-02-29 18:45:225826
5827 // This is a normal extension, installed normally.
5828 // This should NOT trigger an alert.
5829 set_extensions_enabled(true);
[email protected]650b2d52013-02-10 03:41:455830 base::FilePath path = data_dir_.AppendASCII("good.crx");
[email protected]07c9f2f42012-02-29 18:45:225831 InstallCRX(path, INSTALL_NEW);
5832
[email protected]612a1cb12012-10-17 13:18:035833 service_->CheckForExternalUpdates();
[email protected]b8f50ce2012-11-17 12:37:575834 loop_.RunUntilIdle();
[email protected]612a1cb12012-10-17 13:18:035835 EXPECT_FALSE(extensions::HasExternalInstallError(service_));
[email protected]07c9f2f42012-02-29 18:45:225836
5837 // A hosted app, installed externally.
[email protected]2c495c42013-01-04 21:49:545838 // This should NOT trigger an alert.
[email protected]07c9f2f42012-02-29 18:45:225839 provider->UpdateOrAddExtension(hosted_app, "1.0.0.0",
5840 data_dir_.AppendASCII("hosted_app.crx"));
5841
5842 service_->CheckForExternalUpdates();
[email protected]b8f50ce2012-11-17 12:37:575843 loop_.RunUntilIdle();
[email protected]612a1cb12012-10-17 13:18:035844 EXPECT_FALSE(extensions::HasExternalInstallError(service_));
[email protected]07c9f2f42012-02-29 18:45:225845
[email protected]612a1cb12012-10-17 13:18:035846 // Another normal extension, but installed externally.
5847 // This SHOULD trigger an alert.
5848 provider->UpdateOrAddExtension(page_action, "1.0.0.0",
5849 data_dir_.AppendASCII("page_action.crx"));
5850
5851 service_->CheckForExternalUpdates();
[email protected]b8f50ce2012-11-17 12:37:575852 loop_.RunUntilIdle();
[email protected]612a1cb12012-10-17 13:18:035853 EXPECT_TRUE(extensions::HasExternalInstallError(service_));
[email protected]07c9f2f42012-02-29 18:45:225854}
[email protected]612a1cb12012-10-17 13:18:035855
5856// Test that external extensions are initially disabled, and that enabling
5857// them clears the prompt.
[email protected]d9a61e12012-11-14 02:43:475858TEST_F(ExtensionServiceTest, ExternalInstallInitiallyDisabled) {
[email protected]00b5d0a52012-10-30 13:13:535859 FeatureSwitch::ScopedOverride prompt(
5860 FeatureSwitch::prompt_for_external_extensions(), true);
[email protected]612a1cb12012-10-17 13:18:035861
5862 InitializeEmptyExtensionService();
5863 MockExtensionProvider* provider =
[email protected]1d5e58b2013-01-31 08:41:405864 new MockExtensionProvider(service_, Manifest::EXTERNAL_PREF);
[email protected]612a1cb12012-10-17 13:18:035865 AddMockExternalProvider(provider);
5866
5867 provider->UpdateOrAddExtension(page_action, "1.0.0.0",
5868 data_dir_.AppendASCII("page_action.crx"));
5869
5870 service_->CheckForExternalUpdates();
[email protected]b8f50ce2012-11-17 12:37:575871 loop_.RunUntilIdle();
[email protected]612a1cb12012-10-17 13:18:035872 EXPECT_TRUE(extensions::HasExternalInstallError(service_));
5873 EXPECT_FALSE(service_->IsExtensionEnabled(page_action));
5874
5875 const Extension* extension =
5876 service_->disabled_extensions()->GetByID(page_action);
5877 EXPECT_TRUE(extension);
5878 EXPECT_EQ(page_action, extension->id());
5879
5880 service_->EnableExtension(page_action);
5881 EXPECT_FALSE(extensions::HasExternalInstallError(service_));
5882 EXPECT_TRUE(service_->IsExtensionEnabled(page_action));
5883}
5884
5885// Test that installing multiple external extensions works.
[email protected]d9a61e12012-11-14 02:43:475886TEST_F(ExtensionServiceTest, ExternalInstallMultiple) {
[email protected]00b5d0a52012-10-30 13:13:535887 FeatureSwitch::ScopedOverride prompt(
5888 FeatureSwitch::prompt_for_external_extensions(), true);
[email protected]612a1cb12012-10-17 13:18:035889
5890 InitializeEmptyExtensionService();
5891 MockExtensionProvider* provider =
[email protected]1d5e58b2013-01-31 08:41:405892 new MockExtensionProvider(service_, Manifest::EXTERNAL_PREF);
[email protected]612a1cb12012-10-17 13:18:035893 AddMockExternalProvider(provider);
5894
5895 provider->UpdateOrAddExtension(page_action, "1.0.0.0",
5896 data_dir_.AppendASCII("page_action.crx"));
5897 provider->UpdateOrAddExtension(good_crx, "1.0.0.0",
5898 data_dir_.AppendASCII("good.crx"));
5899 provider->UpdateOrAddExtension(theme_crx, "2.0",
5900 data_dir_.AppendASCII("theme.crx"));
5901
5902 service_->CheckForExternalUpdates();
[email protected]b8f50ce2012-11-17 12:37:575903 loop_.RunUntilIdle();
[email protected]612a1cb12012-10-17 13:18:035904 EXPECT_TRUE(extensions::HasExternalInstallError(service_));
5905 EXPECT_FALSE(service_->IsExtensionEnabled(page_action));
5906 EXPECT_FALSE(service_->IsExtensionEnabled(good_crx));
5907 EXPECT_FALSE(service_->IsExtensionEnabled(theme_crx));
5908
5909 service_->EnableExtension(page_action);
5910 EXPECT_TRUE(extensions::HasExternalInstallError(service_));
[email protected]aa55be7f2013-04-22 20:56:045911 EXPECT_FALSE(extensions::HasExternalInstallBubble(service_));
[email protected]612a1cb12012-10-17 13:18:035912 service_->EnableExtension(theme_crx);
5913 EXPECT_TRUE(extensions::HasExternalInstallError(service_));
[email protected]aa55be7f2013-04-22 20:56:045914 EXPECT_FALSE(extensions::HasExternalInstallBubble(service_));
[email protected]612a1cb12012-10-17 13:18:035915 service_->EnableExtension(good_crx);
5916 EXPECT_FALSE(extensions::HasExternalInstallError(service_));
[email protected]aa55be7f2013-04-22 20:56:045917 EXPECT_FALSE(extensions::HasExternalInstallBubble(service_));
5918}
5919
5920// Test that there is a bubble for external extensions that update
[email protected]b3aa7182013-04-25 04:45:235921// from the webstore if the profile is not new.
5922TEST_F(ExtensionServiceTest, ExternalInstallUpdatesFromWebstoreOldProfile) {
[email protected]aa55be7f2013-04-22 20:56:045923 FeatureSwitch::ScopedOverride prompt(
5924 FeatureSwitch::prompt_for_external_extensions(), true);
5925
[email protected]b3aa7182013-04-25 04:45:235926 // This sets up the ExtensionPrefs used by our ExtensionService to be
5927 // post-first run.
5928 InitializeExtensionServiceHelper(false, false);
[email protected]aa55be7f2013-04-22 20:56:045929
[email protected]aa55be7f2013-04-22 20:56:045930 base::FilePath crx_path = temp_dir_.path().AppendASCII("webstore.crx");
5931 PackCRX(data_dir_.AppendASCII("update_from_webstore"),
5932 data_dir_.AppendASCII("update_from_webstore.pem"),
5933 crx_path);
5934
5935 MockExtensionProvider* provider =
5936 new MockExtensionProvider(service_, Manifest::EXTERNAL_PREF);
5937 AddMockExternalProvider(provider);
[email protected]aa55be7f2013-04-22 20:56:045938 provider->UpdateOrAddExtension(updates_from_webstore, "1", crx_path);
[email protected]460c6712013-04-24 07:20:015939
[email protected]aa55be7f2013-04-22 20:56:045940 service_->CheckForExternalUpdates();
5941 loop_.RunUntilIdle();
5942 EXPECT_TRUE(extensions::HasExternalInstallError(service_));
5943 EXPECT_TRUE(extensions::HasExternalInstallBubble(service_));
5944 EXPECT_FALSE(service_->IsExtensionEnabled(updates_from_webstore));
[email protected]612a1cb12012-10-17 13:18:035945}
[email protected]460c6712013-04-24 07:20:015946
5947// Test that there is no bubble for external extensions if the profile is new.
5948TEST_F(ExtensionServiceTest, ExternalInstallUpdatesFromWebstoreNewProfile) {
5949 FeatureSwitch::ScopedOverride prompt(
5950 FeatureSwitch::prompt_for_external_extensions(), true);
5951
5952 InitializeEmptyExtensionService();
5953
5954 base::FilePath crx_path = temp_dir_.path().AppendASCII("webstore.crx");
5955 PackCRX(data_dir_.AppendASCII("update_from_webstore"),
5956 data_dir_.AppendASCII("update_from_webstore.pem"),
5957 crx_path);
5958
5959 MockExtensionProvider* provider =
5960 new MockExtensionProvider(service_, Manifest::EXTERNAL_PREF);
5961 AddMockExternalProvider(provider);
5962 provider->UpdateOrAddExtension(updates_from_webstore, "1", crx_path);
5963
5964 service_->CheckForExternalUpdates();
5965 loop_.RunUntilIdle();
5966 EXPECT_TRUE(extensions::HasExternalInstallError(service_));
5967 EXPECT_FALSE(extensions::HasExternalInstallBubble(service_));
5968 EXPECT_FALSE(service_->IsExtensionEnabled(updates_from_webstore));
5969}