1// Copyright (c) 2013 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include <algorithm>
6#include <set>
7#include <vector>
8
9#include "base/at_exit.h"
10#include "base/basictypes.h"
11#include "base/bind.h"
12#include "base/command_line.h"
13#include "base/files/file_enumerator.h"
14#include "base/files/file_util.h"
15#include "base/files/scoped_temp_dir.h"
16#include "base/json/json_file_value_serializer.h"
17#include "base/json/json_reader.h"
18#include "base/json/json_string_value_serializer.h"
19#include "base/memory/scoped_ptr.h"
20#include "base/memory/weak_ptr.h"
21#include "base/message_loop/message_loop.h"
22#include "base/prefs/scoped_user_pref_update.h"
23#include "base/stl_util.h"
24#include "base/strings/string16.h"
25#include "base/strings/string_number_conversions.h"
26#include "base/strings/string_util.h"
27#include "base/strings/utf_string_conversions.h"
28#include "base/version.h"
29#include "chrome/browser/browser_process.h"
30#include "chrome/browser/chrome_notification_types.h"
31#include "chrome/browser/extensions/app_sync_data.h"
32#include "chrome/browser/extensions/blacklist.h"
33#include "chrome/browser/extensions/chrome_app_sorting.h"
34#include "chrome/browser/extensions/component_loader.h"
35#include "chrome/browser/extensions/crx_installer.h"
36#include "chrome/browser/extensions/default_apps.h"
37#include "chrome/browser/extensions/extension_creator.h"
38#include "chrome/browser/extensions/extension_error_reporter.h"
39#include "chrome/browser/extensions/extension_error_ui.h"
40#include "chrome/browser/extensions/extension_management_test_util.h"
41#include "chrome/browser/extensions/extension_notification_observer.h"
42#include "chrome/browser/extensions/extension_service.h"
43#include "chrome/browser/extensions/extension_service_test_base.h"
44#include "chrome/browser/extensions/extension_special_storage_policy.h"
45#include "chrome/browser/extensions/extension_sync_data.h"
46#include "chrome/browser/extensions/extension_sync_service.h"
47#include "chrome/browser/extensions/extension_util.h"
48#include "chrome/browser/extensions/external_install_error.h"
49#include "chrome/browser/extensions/external_install_manager.h"
50#include "chrome/browser/extensions/external_policy_loader.h"
51#include "chrome/browser/extensions/external_pref_loader.h"
52#include "chrome/browser/extensions/external_provider_impl.h"
53#include "chrome/browser/extensions/fake_safe_browsing_database_manager.h"
54#include "chrome/browser/extensions/installed_loader.h"
55#include "chrome/browser/extensions/pack_extension_job.h"
56#include "chrome/browser/extensions/pending_extension_info.h"
57#include "chrome/browser/extensions/pending_extension_manager.h"
58#include "chrome/browser/extensions/test_blacklist.h"
59#include "chrome/browser/extensions/test_extension_system.h"
60#include "chrome/browser/extensions/unpacked_installer.h"
61#include "chrome/browser/extensions/updater/extension_updater.h"
62#include "chrome/browser/prefs/pref_service_syncable.h"
63#include "chrome/browser/supervised_user/supervised_user_service.h"
64#include "chrome/browser/supervised_user/supervised_user_service_factory.h"
65#include "chrome/browser/sync/profile_sync_service.h"
66#include "chrome/browser/sync/profile_sync_service_factory.h"
67#include "chrome/common/chrome_constants.h"
68#include "chrome/common/chrome_switches.h"
69#include "chrome/common/extensions/api/plugins/plugins_handler.h"
70#include "chrome/common/extensions/manifest_handlers/app_launch_info.h"
71#include "chrome/common/extensions/manifest_handlers/content_scripts_handler.h"
72#include "chrome/common/extensions/manifest_url_handler.h"
73#include "chrome/common/pref_names.h"
74#include "chrome/common/url_constants.h"
75#include "chrome/test/base/scoped_browser_locale.h"
76#include "chrome/test/base/testing_pref_service_syncable.h"
77#include "chrome/test/base/testing_profile.h"
78#include "components/crx_file/id_util.h"
79#include "components/pref_registry/pref_registry_syncable.h"
80#include "content/public/browser/dom_storage_context.h"
81#include "content/public/browser/gpu_data_manager.h"
82#include "content/public/browser/indexed_db_context.h"
83#include "content/public/browser/notification_registrar.h"
84#include "content/public/browser/notification_service.h"
85#include "content/public/browser/plugin_service.h"
86#include "content/public/browser/render_process_host.h"
87#include "content/public/browser/storage_partition.h"
88#include "content/public/common/content_constants.h"
89#include "content/public/test/test_utils.h"
90#include "extensions/browser/extension_registry.h"
91#include "extensions/browser/extension_system.h"
92#include "extensions/browser/external_provider_interface.h"
93#include "extensions/browser/install_flag.h"
94#include "extensions/browser/management_policy.h"
95#include "extensions/browser/test_management_policy.h"
96#include "extensions/browser/uninstall_reason.h"
97#include "extensions/common/constants.h"
98#include "extensions/common/extension.h"
99#include "extensions/common/extension_builder.h"
100#include "extensions/common/extension_l10n_util.h"
101#include "extensions/common/extension_resource.h"
102#include "extensions/common/feature_switch.h"
103#include "extensions/common/manifest_constants.h"
104#include "extensions/common/manifest_handlers/background_info.h"
105#include "extensions/common/permissions/permission_set.h"
106#include "extensions/common/permissions/permissions_data.h"
107#include "extensions/common/switches.h"
108#include "extensions/common/url_pattern.h"
109#include "extensions/common/value_builder.h"
110#include "gpu/config/gpu_info.h"
111#include "grit/browser_resources.h"
112#include "net/cookies/canonical_cookie.h"
113#include "net/cookies/cookie_monster.h"
114#include "net/cookies/cookie_options.h"
115#include "net/url_request/url_request_context.h"
116#include "net/url_request/url_request_context_getter.h"
117#include "storage/browser/database/database_tracker.h"
118#include "storage/browser/quota/quota_manager.h"
119#include "storage/common/database/database_identifier.h"
120#include "sync/api/fake_sync_change_processor.h"
121#include "sync/api/string_ordinal.h"
122#include "sync/api/sync_data.h"
123#include "sync/api/sync_error_factory.h"
124#include "sync/api/sync_error_factory_mock.h"
125#include "sync/api/syncable_service.h"
126#include "sync/protocol/app_specifics.pb.h"
127#include "sync/protocol/extension_specifics.pb.h"
128#include "sync/protocol/sync.pb.h"
129#include "testing/gtest/include/gtest/gtest.h"
130#include "testing/platform_test.h"
131#include "url/gurl.h"
132
133#if defined(OS_CHROMEOS)
134#include "chrome/browser/chromeos/login/users/scoped_test_user_manager.h"
135#include "chrome/browser/chromeos/settings/cros_settings.h"
136#include "chrome/browser/chromeos/settings/device_settings_service.h"
137#endif
138
139// The blacklist tests rely on safe browsing.
140#if defined(FULL_SAFE_BROWSING) || defined(MOBILE_SAFE_BROWSING)
141#define ENABLE_BLACKLIST_TESTS
142#endif
143
144using base::DictionaryValue;
145using base::ListValue;
146using base::Value;
147using content::BrowserContext;
148using content::BrowserThread;
149using content::DOMStorageContext;
150using content::IndexedDBContext;
151using content::PluginService;
152using extensions::APIPermission;
153using extensions::APIPermissionSet;
154using extensions::AppSorting;
155using extensions::Blacklist;
156using extensions::CrxInstaller;
157using extensions::Extension;
158using extensions::ExtensionCreator;
159using extensions::ExtensionPrefs;
160using extensions::ExtensionRegistry;
161using extensions::ExtensionResource;
162using extensions::ExtensionSystem;
163using extensions::FakeSafeBrowsingDatabaseManager;
164using extensions::FeatureSwitch;
165using extensions::Manifest;
166using extensions::PermissionSet;
167using extensions::TestExtensionSystem;
168using extensions::UnloadedExtensionInfo;
169using extensions::URLPatternSet;
170
171namespace keys = extensions::manifest_keys;
172
173namespace {
174
175// Extension ids used during testing.
176const char good0[] = "behllobkkfkfnphdnhnkndlbkcpglgmj";
177const char good1[] = "hpiknbiabeeppbpihjehijgoemciehgk";
178const char good2[] = "bjafgdebaacbbbecmhlhpofkepfkgcpa";
179const char all_zero[] = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
180const char good2048[] = "nmgjhmhbleinmjpbdhgajfjkbijcmgbh";
181const char good_crx[] = "ldnnhddmnhbkjipkidpdiheffobcpfmf";
182const char hosted_app[] = "kbmnembihfiondgfjekmnmcbddelicoi";
183const char page_action[] = "obcimlgaoabeegjmmpldobjndiealpln";
184const char theme_crx[] = "iamefpfkojoapidjnbafmgkgncegbkad";
185const char theme2_crx[] = "pjpgmfcmabopnnfonnhmdjglfpjjfkbf";
186const char permissions_crx[] = "eagpmdpfmaekmmcejjbmjoecnejeiiin";
187const char unpacked[] = "cbcdidchbppangcjoddlpdjlenngjldk";
188const char updates_from_webstore[] = "akjooamlhcgeopfifcmlggaebeocgokj";
189
190struct ExtensionsOrder {
191  bool operator()(const scoped_refptr<const Extension>& a,
192                  const scoped_refptr<const Extension>& b) {
193    return a->name() < b->name();
194  }
195};
196
197static std::vector<base::string16> GetErrors() {
198  const std::vector<base::string16>* errors =
199      ExtensionErrorReporter::GetInstance()->GetErrors();
200  std::vector<base::string16> ret_val;
201
202  for (std::vector<base::string16>::const_iterator iter = errors->begin();
203       iter != errors->end(); ++iter) {
204    std::string utf8_error = base::UTF16ToUTF8(*iter);
205    if (utf8_error.find(".svn") == std::string::npos) {
206      ret_val.push_back(*iter);
207    }
208  }
209
210  // The tests rely on the errors being in a certain order, which can vary
211  // depending on how filesystem iteration works.
212  std::stable_sort(ret_val.begin(), ret_val.end());
213
214  return ret_val;
215}
216
217static void AddPattern(URLPatternSet* extent, const std::string& pattern) {
218  int schemes = URLPattern::SCHEME_ALL;
219  extent->AddPattern(URLPattern(schemes, pattern));
220}
221
222base::FilePath GetTemporaryFile() {
223  base::FilePath temp_file;
224  CHECK(base::CreateTemporaryFile(&temp_file));
225  return temp_file;
226}
227
228bool WaitForCountNotificationsCallback(int *count) {
229  return --(*count) == 0;
230}
231
232}  // namespace
233
234class MockExtensionProvider : public extensions::ExternalProviderInterface {
235 public:
236  MockExtensionProvider(
237      VisitorInterface* visitor,
238      Manifest::Location location)
239    : location_(location), visitor_(visitor), visit_count_(0) {
240  }
241
242  virtual ~MockExtensionProvider() {}
243
244  void UpdateOrAddExtension(const std::string& id,
245                            const std::string& version,
246                            const base::FilePath& path) {
247    extension_map_[id] = std::make_pair(version, path);
248  }
249
250  void RemoveExtension(const std::string& id) {
251    extension_map_.erase(id);
252  }
253
254  // ExternalProvider implementation:
255  virtual void VisitRegisteredExtension() OVERRIDE {
256    visit_count_++;
257    for (DataMap::const_iterator i = extension_map_.begin();
258         i != extension_map_.end(); ++i) {
259      Version version(i->second.first);
260
261      visitor_->OnExternalExtensionFileFound(
262          i->first, &version, i->second.second, location_,
263          Extension::NO_FLAGS, false);
264    }
265    visitor_->OnExternalProviderReady(this);
266  }
267
268  virtual bool HasExtension(const std::string& id) const OVERRIDE {
269    return extension_map_.find(id) != extension_map_.end();
270  }
271
272  virtual bool GetExtensionDetails(
273      const std::string& id,
274      Manifest::Location* location,
275      scoped_ptr<Version>* version) const OVERRIDE {
276    DataMap::const_iterator it = extension_map_.find(id);
277    if (it == extension_map_.end())
278      return false;
279
280    if (version)
281      version->reset(new Version(it->second.first));
282
283    if (location)
284      *location = location_;
285
286    return true;
287  }
288
289  virtual bool IsReady() const OVERRIDE {
290    return true;
291  }
292
293  virtual void ServiceShutdown() OVERRIDE {
294  }
295
296  int visit_count() const { return visit_count_; }
297  void set_visit_count(int visit_count) {
298    visit_count_ = visit_count;
299  }
300
301 private:
302  typedef std::map< std::string, std::pair<std::string, base::FilePath> >
303      DataMap;
304  DataMap extension_map_;
305  Manifest::Location location_;
306  VisitorInterface* visitor_;
307
308  // visit_count_ tracks the number of calls to VisitRegisteredExtension().
309  // Mutable because it must be incremented on each call to
310  // VisitRegisteredExtension(), which must be a const method to inherit
311  // from the class being mocked.
312  mutable int visit_count_;
313
314  DISALLOW_COPY_AND_ASSIGN(MockExtensionProvider);
315};
316
317class MockProviderVisitor
318    : public extensions::ExternalProviderInterface::VisitorInterface {
319 public:
320  // The provider will return |fake_base_path| from
321  // GetBaseCrxFilePath().  User can test the behavior with
322  // and without an empty path using this parameter.
323  explicit MockProviderVisitor(base::FilePath fake_base_path)
324      : ids_found_(0),
325        fake_base_path_(fake_base_path),
326        expected_creation_flags_(Extension::NO_FLAGS) {
327    profile_.reset(new TestingProfile);
328  }
329
330  MockProviderVisitor(base::FilePath fake_base_path,
331                      int expected_creation_flags)
332      : ids_found_(0),
333        fake_base_path_(fake_base_path),
334        expected_creation_flags_(expected_creation_flags) {
335  }
336
337  int Visit(const std::string& json_data) {
338    // Give the test json file to the provider for parsing.
339    provider_.reset(new extensions::ExternalProviderImpl(
340        this,
341        new extensions::ExternalTestingLoader(json_data, fake_base_path_),
342        profile_.get(),
343        Manifest::EXTERNAL_PREF,
344        Manifest::EXTERNAL_PREF_DOWNLOAD,
345        Extension::NO_FLAGS));
346
347    // We also parse the file into a dictionary to compare what we get back
348    // from the provider.
349    JSONStringValueSerializer serializer(json_data);
350    base::Value* json_value = serializer.Deserialize(NULL, NULL);
351
352    if (!json_value || !json_value->IsType(base::Value::TYPE_DICTIONARY)) {
353      NOTREACHED() << "Unable to deserialize json data";
354      return -1;
355    } else {
356      base::DictionaryValue* external_extensions =
357          static_cast<base::DictionaryValue*>(json_value);
358      prefs_.reset(external_extensions);
359    }
360
361    // Reset our counter.
362    ids_found_ = 0;
363    // Ask the provider to look up all extensions and return them.
364    provider_->VisitRegisteredExtension();
365
366    return ids_found_;
367  }
368
369  virtual bool OnExternalExtensionFileFound(const std::string& id,
370                                            const Version* version,
371                                            const base::FilePath& path,
372                                            Manifest::Location unused,
373                                            int creation_flags,
374                                            bool mark_acknowledged) OVERRIDE {
375    EXPECT_EQ(expected_creation_flags_, creation_flags);
376
377    ++ids_found_;
378    base::DictionaryValue* pref;
379    // This tests is to make sure that the provider only notifies us of the
380    // values we gave it. So if the id we doesn't exist in our internal
381    // dictionary then something is wrong.
382    EXPECT_TRUE(prefs_->GetDictionary(id, &pref))
383       << "Got back ID (" << id.c_str() << ") we weren't expecting";
384
385    EXPECT_TRUE(path.IsAbsolute());
386    if (!fake_base_path_.empty())
387      EXPECT_TRUE(fake_base_path_.IsParent(path));
388
389    if (pref) {
390      EXPECT_TRUE(provider_->HasExtension(id));
391
392      // Ask provider if the extension we got back is registered.
393      Manifest::Location location = Manifest::INVALID_LOCATION;
394      scoped_ptr<Version> v1;
395      base::FilePath crx_path;
396
397      EXPECT_TRUE(provider_->GetExtensionDetails(id, NULL, &v1));
398      EXPECT_STREQ(version->GetString().c_str(), v1->GetString().c_str());
399
400      scoped_ptr<Version> v2;
401      EXPECT_TRUE(provider_->GetExtensionDetails(id, &location, &v2));
402      EXPECT_STREQ(version->GetString().c_str(), v1->GetString().c_str());
403      EXPECT_STREQ(version->GetString().c_str(), v2->GetString().c_str());
404      EXPECT_EQ(Manifest::EXTERNAL_PREF, location);
405
406      // Remove it so we won't count it ever again.
407      prefs_->Remove(id, NULL);
408    }
409    return true;
410  }
411
412  virtual bool OnExternalExtensionUpdateUrlFound(
413      const std::string& id,
414      const std::string& install_parameter,
415      const GURL& update_url,
416      Manifest::Location location,
417      int creation_flags,
418      bool mark_acknowledged) OVERRIDE {
419    ++ids_found_;
420    base::DictionaryValue* pref;
421    // This tests is to make sure that the provider only notifies us of the
422    // values we gave it. So if the id we doesn't exist in our internal
423    // dictionary then something is wrong.
424    EXPECT_TRUE(prefs_->GetDictionary(id, &pref))
425       << L"Got back ID (" << id.c_str() << ") we weren't expecting";
426    EXPECT_EQ(Manifest::EXTERNAL_PREF_DOWNLOAD, location);
427
428    if (pref) {
429      EXPECT_TRUE(provider_->HasExtension(id));
430
431      // External extensions with update URLs do not have versions.
432      scoped_ptr<Version> v1;
433      Manifest::Location location1 = Manifest::INVALID_LOCATION;
434      EXPECT_TRUE(provider_->GetExtensionDetails(id, &location1, &v1));
435      EXPECT_FALSE(v1.get());
436      EXPECT_EQ(Manifest::EXTERNAL_PREF_DOWNLOAD, location1);
437
438      std::string parsed_install_parameter;
439      pref->GetString("install_parameter", &parsed_install_parameter);
440      EXPECT_EQ(parsed_install_parameter, install_parameter);
441
442      // Remove it so we won't count it again.
443      prefs_->Remove(id, NULL);
444    }
445    return true;
446  }
447
448  virtual void OnExternalProviderReady(
449      const extensions::ExternalProviderInterface* provider) OVERRIDE {
450    EXPECT_EQ(provider, provider_.get());
451    EXPECT_TRUE(provider->IsReady());
452  }
453
454 private:
455  int ids_found_;
456  base::FilePath fake_base_path_;
457  int expected_creation_flags_;
458  scoped_ptr<extensions::ExternalProviderImpl> provider_;
459  scoped_ptr<base::DictionaryValue> prefs_;
460  scoped_ptr<TestingProfile> profile_;
461
462  DISALLOW_COPY_AND_ASSIGN(MockProviderVisitor);
463};
464
465class ExtensionServiceTest : public extensions::ExtensionServiceTestBase,
466                             public content::NotificationObserver {
467 public:
468  ExtensionServiceTest()
469      : unloaded_reason_(UnloadedExtensionInfo::REASON_UNDEFINED),
470        installed_(NULL),
471        was_update_(false),
472        override_external_install_prompt_(
473            FeatureSwitch::prompt_for_external_extensions(),
474            false),
475        expected_extensions_count_(0) {
476    registrar_.Add(this,
477                   extensions::NOTIFICATION_EXTENSION_LOADED_DEPRECATED,
478                   content::NotificationService::AllSources());
479    registrar_.Add(this,
480                   extensions::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED,
481                   content::NotificationService::AllSources());
482    registrar_.Add(
483        this,
484        extensions::NOTIFICATION_EXTENSION_WILL_BE_INSTALLED_DEPRECATED,
485        content::NotificationService::AllSources());
486  }
487
488  virtual void Observe(int type,
489                       const content::NotificationSource& source,
490                       const content::NotificationDetails& details) OVERRIDE {
491    switch (type) {
492      case extensions::NOTIFICATION_EXTENSION_LOADED_DEPRECATED: {
493        const Extension* extension =
494            content::Details<const Extension>(details).ptr();
495        loaded_.push_back(make_scoped_refptr(extension));
496        // The tests rely on the errors being in a certain order, which can vary
497        // depending on how filesystem iteration works.
498        std::stable_sort(loaded_.begin(), loaded_.end(), ExtensionsOrder());
499        break;
500      }
501
502      case extensions::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED: {
503        UnloadedExtensionInfo* unloaded_info =
504            content::Details<UnloadedExtensionInfo>(details).ptr();
505        const Extension* e = unloaded_info->extension;
506        unloaded_id_ = e->id();
507        unloaded_reason_ = unloaded_info->reason;
508        extensions::ExtensionList::iterator i =
509            std::find(loaded_.begin(), loaded_.end(), e);
510        // TODO(erikkay) fix so this can be an assert.  Right now the tests
511        // are manually calling clear() on loaded_, so this isn't doable.
512        if (i == loaded_.end())
513          return;
514        loaded_.erase(i);
515        break;
516      }
517      case extensions::NOTIFICATION_EXTENSION_WILL_BE_INSTALLED_DEPRECATED: {
518        const extensions::InstalledExtensionInfo* installed_info =
519            content::Details<const extensions::InstalledExtensionInfo>(details)
520                .ptr();
521        installed_ = installed_info->extension;
522        was_update_ = installed_info->is_update;
523        old_name_ = installed_info->old_name;
524        break;
525      }
526
527      default:
528        DCHECK(false);
529    }
530  }
531
532  void AddMockExternalProvider(
533      extensions::ExternalProviderInterface* provider) {
534    service()->AddProviderForTesting(provider);
535  }
536
537  void MockSyncStartFlare(bool* was_called,
538                          syncer::ModelType* model_type_passed_in,
539                          syncer::ModelType model_type) {
540    *was_called = true;
541    *model_type_passed_in = model_type;
542  }
543
544 protected:
545  // Paths to some of the fake extensions.
546  base::FilePath good0_path() {
547    return data_dir()
548        .AppendASCII("good")
549        .AppendASCII("Extensions")
550        .AppendASCII(good0)
551        .AppendASCII("1.0.0.0");
552  }
553
554  base::FilePath good1_path() {
555    return data_dir()
556        .AppendASCII("good")
557        .AppendASCII("Extensions")
558        .AppendASCII(good1)
559        .AppendASCII("2");
560  }
561
562  base::FilePath good2_path() {
563    return data_dir()
564        .AppendASCII("good")
565        .AppendASCII("Extensions")
566        .AppendASCII(good2)
567        .AppendASCII("1.0");
568  }
569
570  void TestExternalProvider(MockExtensionProvider* provider,
571                            Manifest::Location location);
572
573  void PackCRX(const base::FilePath& dir_path,
574               const base::FilePath& pem_path,
575               const base::FilePath& crx_path) {
576    // Use the existing pem key, if provided.
577    base::FilePath pem_output_path;
578    if (pem_path.value().empty()) {
579      pem_output_path = crx_path.DirName().AppendASCII("temp.pem");
580    } else {
581      ASSERT_TRUE(base::PathExists(pem_path));
582    }
583
584    ASSERT_TRUE(base::DeleteFile(crx_path, false));
585
586    scoped_ptr<ExtensionCreator> creator(new ExtensionCreator());
587    ASSERT_TRUE(creator->Run(dir_path,
588                             crx_path,
589                             pem_path,
590                             pem_output_path,
591                             ExtensionCreator::kOverwriteCRX));
592
593    ASSERT_TRUE(base::PathExists(crx_path));
594  }
595
596  enum InstallState {
597    INSTALL_FAILED,
598    INSTALL_UPDATED,
599    INSTALL_NEW,
600    INSTALL_WITHOUT_LOAD,
601  };
602
603  const Extension* PackAndInstallCRX(const base::FilePath& dir_path,
604                                     const base::FilePath& pem_path,
605                                     InstallState install_state,
606                                     int creation_flags) {
607    base::FilePath crx_path;
608    base::ScopedTempDir temp_dir;
609    EXPECT_TRUE(temp_dir.CreateUniqueTempDir());
610    crx_path = temp_dir.path().AppendASCII("temp.crx");
611
612    PackCRX(dir_path, pem_path, crx_path);
613    return InstallCRX(crx_path, install_state, creation_flags);
614  }
615
616  const Extension* PackAndInstallCRX(const base::FilePath& dir_path,
617                                     const base::FilePath& pem_path,
618                                     InstallState install_state) {
619    return PackAndInstallCRX(dir_path, pem_path, install_state,
620                             Extension::NO_FLAGS);
621  }
622
623  const Extension* PackAndInstallCRX(const base::FilePath& dir_path,
624                                     InstallState install_state) {
625    return PackAndInstallCRX(dir_path, base::FilePath(), install_state,
626                             Extension::NO_FLAGS);
627  }
628
629  // Attempts to install an extension. Use INSTALL_FAILED if the installation
630  // is expected to fail.
631  // If |install_state| is INSTALL_UPDATED, and |expected_old_name| is
632  // non-empty, expects that the existing extension's title was
633  // |expected_old_name|.
634  const Extension* InstallCRX(const base::FilePath& path,
635                              InstallState install_state,
636                              int creation_flags,
637                              const std::string& expected_old_name) {
638    InstallCRXInternal(path, creation_flags);
639    return VerifyCrxInstall(path, install_state, expected_old_name);
640  }
641
642  // Attempts to install an extension. Use INSTALL_FAILED if the installation
643  // is expected to fail.
644  const Extension* InstallCRX(const base::FilePath& path,
645                              InstallState install_state,
646                              int creation_flags) {
647    return InstallCRX(path, install_state, creation_flags, std::string());
648  }
649
650  // Attempts to install an extension. Use INSTALL_FAILED if the installation
651  // is expected to fail.
652  const Extension* InstallCRX(const base::FilePath& path,
653                              InstallState install_state) {
654    return InstallCRX(path, install_state, Extension::NO_FLAGS);
655  }
656
657  const Extension* InstallCRXFromWebStore(const base::FilePath& path,
658                                          InstallState install_state) {
659    InstallCRXInternal(path, Extension::FROM_WEBSTORE);
660    return VerifyCrxInstall(path, install_state);
661  }
662
663  const Extension* InstallCRXWithLocation(const base::FilePath& crx_path,
664                                          Manifest::Location install_location,
665                                          InstallState install_state) {
666    EXPECT_TRUE(base::PathExists(crx_path))
667        << "Path does not exist: "<< crx_path.value().c_str();
668    // no client (silent install)
669    scoped_refptr<CrxInstaller> installer(
670        CrxInstaller::CreateSilent(service()));
671    installer->set_install_source(install_location);
672
673    content::WindowedNotificationObserver observer(
674        extensions::NOTIFICATION_CRX_INSTALLER_DONE,
675        content::NotificationService::AllSources());
676    installer->InstallCrx(crx_path);
677    observer.Wait();
678
679    return VerifyCrxInstall(crx_path, install_state);
680  }
681
682  // Verifies the result of a CRX installation. Used by InstallCRX. Set the
683  // |install_state| to INSTALL_FAILED if the installation is expected to fail.
684  // Returns an Extension pointer if the install succeeded, NULL otherwise.
685  const Extension* VerifyCrxInstall(const base::FilePath& path,
686                                    InstallState install_state) {
687    return VerifyCrxInstall(path, install_state, std::string());
688  }
689
690  // Verifies the result of a CRX installation. Used by InstallCRX. Set the
691  // |install_state| to INSTALL_FAILED if the installation is expected to fail.
692  // If |install_state| is INSTALL_UPDATED, and |expected_old_name| is
693  // non-empty, expects that the existing extension's title was
694  // |expected_old_name|.
695  // Returns an Extension pointer if the install succeeded, NULL otherwise.
696  const Extension* VerifyCrxInstall(const base::FilePath& path,
697                                    InstallState install_state,
698                                    const std::string& expected_old_name) {
699    std::vector<base::string16> errors = GetErrors();
700    const Extension* extension = NULL;
701    if (install_state != INSTALL_FAILED) {
702      if (install_state == INSTALL_NEW)
703        ++expected_extensions_count_;
704
705      EXPECT_TRUE(installed_) << path.value();
706      // If and only if INSTALL_UPDATED, it should have the is_update flag.
707      EXPECT_EQ(install_state == INSTALL_UPDATED, was_update_)
708          << path.value();
709      // If INSTALL_UPDATED, old_name_ should match the given string.
710      if (install_state == INSTALL_UPDATED && !expected_old_name.empty())
711        EXPECT_EQ(expected_old_name, old_name_);
712      EXPECT_EQ(0u, errors.size()) << path.value();
713
714      if (install_state == INSTALL_WITHOUT_LOAD) {
715        EXPECT_EQ(0u, loaded_.size()) << path.value();
716      } else {
717        EXPECT_EQ(1u, loaded_.size()) << path.value();
718        size_t actual_extension_count =
719            registry()->enabled_extensions().size() +
720            registry()->disabled_extensions().size();
721        EXPECT_EQ(expected_extensions_count_, actual_extension_count) <<
722            path.value();
723        extension = loaded_[0].get();
724        EXPECT_TRUE(service()->GetExtensionById(extension->id(), false))
725            << path.value();
726      }
727
728      for (std::vector<base::string16>::iterator err = errors.begin();
729        err != errors.end(); ++err) {
730        LOG(ERROR) << *err;
731      }
732    } else {
733      EXPECT_FALSE(installed_) << path.value();
734      EXPECT_EQ(0u, loaded_.size()) << path.value();
735      EXPECT_EQ(1u, errors.size()) << path.value();
736    }
737
738    installed_ = NULL;
739    was_update_ = false;
740    old_name_ = "";
741    loaded_.clear();
742    ExtensionErrorReporter::GetInstance()->ClearErrors();
743    return extension;
744  }
745
746  enum UpdateState {
747    FAILED_SILENTLY,
748    FAILED,
749    UPDATED,
750    INSTALLED,
751    DISABLED,
752    ENABLED
753  };
754
755  void BlackListWebGL() {
756    static const std::string json_blacklist =
757      "{\n"
758      "  \"name\": \"gpu blacklist\",\n"
759      "  \"version\": \"1.0\",\n"
760      "  \"entries\": [\n"
761      "    {\n"
762      "      \"id\": 1,\n"
763      "      \"features\": [\"webgl\"]\n"
764      "    }\n"
765      "  ]\n"
766      "}";
767    gpu::GPUInfo gpu_info;
768    content::GpuDataManager::GetInstance()->InitializeForTesting(
769        json_blacklist, gpu_info);
770  }
771
772  // Helper method to set up a WindowedNotificationObserver to wait for a
773  // specific CrxInstaller to finish if we don't know the value of the
774  // |installer| yet.
775  static bool IsCrxInstallerDone(extensions::CrxInstaller** installer,
776                                 const content::NotificationSource& source,
777                                 const content::NotificationDetails& details) {
778    return content::Source<extensions::CrxInstaller>(source).ptr() ==
779           *installer;
780  }
781
782  void PackCRXAndUpdateExtension(const std::string& id,
783                                 const base::FilePath& dir_path,
784                                 const base::FilePath& pem_path,
785                                 UpdateState expected_state) {
786    base::ScopedTempDir temp_dir;
787    EXPECT_TRUE(temp_dir.CreateUniqueTempDir());
788    base::FilePath crx_path = temp_dir.path().AppendASCII("temp.crx");
789
790    PackCRX(dir_path, pem_path, crx_path);
791    UpdateExtension(id, crx_path, expected_state);
792  }
793
794  void UpdateExtension(const std::string& id,
795                       const base::FilePath& in_path,
796                       UpdateState expected_state) {
797    ASSERT_TRUE(base::PathExists(in_path));
798
799    // We need to copy this to a temporary location because Update() will delete
800    // it.
801    base::FilePath path = temp_dir().path();
802    path = path.Append(in_path.BaseName());
803    ASSERT_TRUE(base::CopyFile(in_path, path));
804
805    int previous_enabled_extension_count =
806        registry()->enabled_extensions().size();
807    int previous_installed_extension_count =
808        previous_enabled_extension_count +
809        registry()->disabled_extensions().size();
810
811    extensions::CrxInstaller* installer = NULL;
812    content::WindowedNotificationObserver observer(
813        extensions::NOTIFICATION_CRX_INSTALLER_DONE,
814        base::Bind(&IsCrxInstallerDone, &installer));
815    service()->UpdateExtension(id, path, true, &installer);
816
817    if (installer)
818      observer.Wait();
819    else
820      base::RunLoop().RunUntilIdle();
821
822    std::vector<base::string16> errors = GetErrors();
823    int error_count = errors.size();
824    int enabled_extension_count = registry()->enabled_extensions().size();
825    int installed_extension_count =
826        enabled_extension_count + registry()->disabled_extensions().size();
827
828    int expected_error_count = (expected_state == FAILED) ? 1 : 0;
829    EXPECT_EQ(expected_error_count, error_count) << path.value();
830
831    if (expected_state <= FAILED) {
832      EXPECT_EQ(previous_enabled_extension_count,
833                enabled_extension_count);
834      EXPECT_EQ(previous_installed_extension_count,
835                installed_extension_count);
836    } else {
837      int expected_installed_extension_count =
838          (expected_state >= INSTALLED) ? 1 : 0;
839      int expected_enabled_extension_count =
840          (expected_state >= ENABLED) ? 1 : 0;
841      EXPECT_EQ(expected_installed_extension_count,
842                installed_extension_count);
843      EXPECT_EQ(expected_enabled_extension_count,
844                enabled_extension_count);
845    }
846
847    // Update() should the temporary input file.
848    EXPECT_FALSE(base::PathExists(path));
849  }
850
851  void TerminateExtension(const std::string& id) {
852    const Extension* extension = service()->GetInstalledExtension(id);
853    if (!extension) {
854      ADD_FAILURE();
855      return;
856    }
857    service()->TrackTerminatedExtensionForTest(extension);
858  }
859
860  size_t GetPrefKeyCount() {
861    const base::DictionaryValue* dict =
862        profile()->GetPrefs()->GetDictionary("extensions.settings");
863    if (!dict) {
864      ADD_FAILURE();
865      return 0;
866    }
867    return dict->size();
868  }
869
870  void UninstallExtension(const std::string& id, bool use_helper) {
871    // Verify that the extension is installed.
872    base::FilePath extension_path = extensions_install_dir().AppendASCII(id);
873    EXPECT_TRUE(base::PathExists(extension_path));
874    size_t pref_key_count = GetPrefKeyCount();
875    EXPECT_GT(pref_key_count, 0u);
876    ValidateIntegerPref(id, "state", Extension::ENABLED);
877
878    // Uninstall it.
879    if (use_helper) {
880      EXPECT_TRUE(ExtensionService::UninstallExtensionHelper(
881          service(), id, extensions::UNINSTALL_REASON_FOR_TESTING));
882    } else {
883      EXPECT_TRUE(service()->UninstallExtension(
884          id,
885          extensions::UNINSTALL_REASON_FOR_TESTING,
886          base::Bind(&base::DoNothing),
887          NULL));
888    }
889    --expected_extensions_count_;
890
891    // We should get an unload notification.
892    EXPECT_FALSE(unloaded_id_.empty());
893    EXPECT_EQ(id, unloaded_id_);
894
895    // Verify uninstalled state.
896    size_t new_pref_key_count = GetPrefKeyCount();
897    if (new_pref_key_count == pref_key_count) {
898      ValidateIntegerPref(id, "state",
899                          Extension::EXTERNAL_EXTENSION_UNINSTALLED);
900    } else {
901      EXPECT_EQ(new_pref_key_count, pref_key_count - 1);
902    }
903
904    // The extension should not be in the service anymore.
905    EXPECT_FALSE(service()->GetInstalledExtension(id));
906    base::RunLoop().RunUntilIdle();
907
908    // The directory should be gone.
909    EXPECT_FALSE(base::PathExists(extension_path));
910  }
911
912  void ValidatePrefKeyCount(size_t count) {
913    EXPECT_EQ(count, GetPrefKeyCount());
914  }
915
916  testing::AssertionResult ValidateBooleanPref(
917      const std::string& extension_id,
918      const std::string& pref_path,
919      bool expected_val) {
920    std::string msg = "while checking: ";
921    msg += extension_id;
922    msg += " ";
923    msg += pref_path;
924    msg += " == ";
925    msg += expected_val ? "true" : "false";
926
927    PrefService* prefs = profile()->GetPrefs();
928    const base::DictionaryValue* dict =
929        prefs->GetDictionary("extensions.settings");
930    if (!dict) {
931      return testing::AssertionFailure()
932          << "extension.settings does not exist " << msg;
933    }
934
935    const base::DictionaryValue* pref = NULL;
936    if (!dict->GetDictionary(extension_id, &pref)) {
937      return testing::AssertionFailure()
938          << "extension pref does not exist " << msg;
939    }
940
941    bool val;
942    if (!pref->GetBoolean(pref_path, &val)) {
943      return testing::AssertionFailure()
944          << pref_path << " pref not found " << msg;
945    }
946
947    return expected_val == val
948        ? testing::AssertionSuccess()
949        : testing::AssertionFailure() << "base::Value is incorrect " << msg;
950  }
951
952  bool IsPrefExist(const std::string& extension_id,
953                   const std::string& pref_path) {
954    const base::DictionaryValue* dict =
955        profile()->GetPrefs()->GetDictionary("extensions.settings");
956    if (dict == NULL) return false;
957    const base::DictionaryValue* pref = NULL;
958    if (!dict->GetDictionary(extension_id, &pref)) {
959      return false;
960    }
961    if (pref == NULL) {
962      return false;
963    }
964    bool val;
965    if (!pref->GetBoolean(pref_path, &val)) {
966      return false;
967    }
968    return true;
969  }
970
971  void ValidateIntegerPref(const std::string& extension_id,
972                           const std::string& pref_path,
973                           int expected_val) {
974    std::string msg = " while checking: ";
975    msg += extension_id;
976    msg += " ";
977    msg += pref_path;
978    msg += " == ";
979    msg += base::IntToString(expected_val);
980
981    PrefService* prefs = profile()->GetPrefs();
982    const base::DictionaryValue* dict =
983        prefs->GetDictionary("extensions.settings");
984    ASSERT_TRUE(dict != NULL) << msg;
985    const base::DictionaryValue* pref = NULL;
986    ASSERT_TRUE(dict->GetDictionary(extension_id, &pref)) << msg;
987    EXPECT_TRUE(pref != NULL) << msg;
988    int val;
989    ASSERT_TRUE(pref->GetInteger(pref_path, &val)) << msg;
990    EXPECT_EQ(expected_val, val) << msg;
991  }
992
993  void ValidateStringPref(const std::string& extension_id,
994                          const std::string& pref_path,
995                          const std::string& expected_val) {
996    std::string msg = " while checking: ";
997    msg += extension_id;
998    msg += ".manifest.";
999    msg += pref_path;
1000    msg += " == ";
1001    msg += expected_val;
1002
1003    const base::DictionaryValue* dict =
1004        profile()->GetPrefs()->GetDictionary("extensions.settings");
1005    ASSERT_TRUE(dict != NULL) << msg;
1006    const base::DictionaryValue* pref = NULL;
1007    std::string manifest_path = extension_id + ".manifest";
1008    ASSERT_TRUE(dict->GetDictionary(manifest_path, &pref)) << msg;
1009    EXPECT_TRUE(pref != NULL) << msg;
1010    std::string val;
1011    ASSERT_TRUE(pref->GetString(pref_path, &val)) << msg;
1012    EXPECT_EQ(expected_val, val) << msg;
1013  }
1014
1015  void SetPref(const std::string& extension_id,
1016               const std::string& pref_path,
1017               base::Value* value,
1018               const std::string& msg) {
1019    DictionaryPrefUpdate update(profile()->GetPrefs(), "extensions.settings");
1020    base::DictionaryValue* dict = update.Get();
1021    ASSERT_TRUE(dict != NULL) << msg;
1022    base::DictionaryValue* pref = NULL;
1023    ASSERT_TRUE(dict->GetDictionary(extension_id, &pref)) << msg;
1024    EXPECT_TRUE(pref != NULL) << msg;
1025    pref->Set(pref_path, value);
1026  }
1027
1028  void SetPrefInteg(const std::string& extension_id,
1029                    const std::string& pref_path,
1030                    int value) {
1031    std::string msg = " while setting: ";
1032    msg += extension_id;
1033    msg += " ";
1034    msg += pref_path;
1035    msg += " = ";
1036    msg += base::IntToString(value);
1037
1038    SetPref(extension_id, pref_path, new base::FundamentalValue(value), msg);
1039  }
1040
1041  void SetPrefBool(const std::string& extension_id,
1042                   const std::string& pref_path,
1043                   bool value) {
1044    std::string msg = " while setting: ";
1045    msg += extension_id + " " + pref_path;
1046    msg += " = ";
1047    msg += (value ? "true" : "false");
1048
1049    SetPref(extension_id, pref_path, new base::FundamentalValue(value), msg);
1050  }
1051
1052  void ClearPref(const std::string& extension_id,
1053                 const std::string& pref_path) {
1054    std::string msg = " while clearing: ";
1055    msg += extension_id + " " + pref_path;
1056
1057    DictionaryPrefUpdate update(profile()->GetPrefs(), "extensions.settings");
1058    base::DictionaryValue* dict = update.Get();
1059    ASSERT_TRUE(dict != NULL) << msg;
1060    base::DictionaryValue* pref = NULL;
1061    ASSERT_TRUE(dict->GetDictionary(extension_id, &pref)) << msg;
1062    EXPECT_TRUE(pref != NULL) << msg;
1063    pref->Remove(pref_path, NULL);
1064  }
1065
1066  void SetPrefStringSet(const std::string& extension_id,
1067                        const std::string& pref_path,
1068                        const std::set<std::string>& value) {
1069    std::string msg = " while setting: ";
1070    msg += extension_id + " " + pref_path;
1071
1072    base::ListValue* list_value = new base::ListValue();
1073    for (std::set<std::string>::const_iterator iter = value.begin();
1074         iter != value.end(); ++iter)
1075      list_value->Append(new base::StringValue(*iter));
1076
1077    SetPref(extension_id, pref_path, list_value, msg);
1078  }
1079
1080  void InitPluginService() {
1081#if defined(ENABLE_PLUGINS)
1082    PluginService::GetInstance()->Init();
1083#endif
1084  }
1085
1086  void InitializeExtensionSyncService() {
1087    extension_sync_service_.reset(new ExtensionSyncService(
1088        profile(), ExtensionPrefs::Get(browser_context()), service()));
1089  }
1090
1091  void InitializeEmptyExtensionServiceWithTestingPrefs() {
1092    ExtensionServiceTestBase::ExtensionServiceInitParams params =
1093        CreateDefaultInitParams();
1094    params.pref_file = base::FilePath();
1095    InitializeExtensionService(params);
1096  }
1097
1098  extensions::ManagementPolicy* GetManagementPolicy() {
1099    return ExtensionSystem::Get(browser_context())->management_policy();
1100  }
1101
1102  ExtensionSyncService* extension_sync_service() {
1103    return extension_sync_service_.get();
1104  }
1105
1106 protected:
1107  typedef extensions::ExtensionManagementPrefUpdater<TestingPrefServiceSyncable>
1108      ManagementPrefUpdater;
1109  scoped_ptr<ExtensionSyncService> extension_sync_service_;
1110  extensions::ExtensionList loaded_;
1111  std::string unloaded_id_;
1112  UnloadedExtensionInfo::Reason unloaded_reason_;
1113  const Extension* installed_;
1114  bool was_update_;
1115  std::string old_name_;
1116  FeatureSwitch::ScopedOverride override_external_install_prompt_;
1117
1118 private:
1119  // Create a CrxInstaller and install the CRX file.
1120  // Instead of calling this method yourself, use InstallCRX(), which does extra
1121  // error checking.
1122  void InstallCRXInternal(const base::FilePath& crx_path) {
1123    InstallCRXInternal(crx_path, Extension::NO_FLAGS);
1124  }
1125
1126  void InstallCRXInternal(const base::FilePath& crx_path, int creation_flags) {
1127    ASSERT_TRUE(base::PathExists(crx_path))
1128        << "Path does not exist: "<< crx_path.value().c_str();
1129    scoped_refptr<CrxInstaller> installer(
1130        CrxInstaller::CreateSilent(service()));
1131    installer->set_creation_flags(creation_flags);
1132    if (!(creation_flags & Extension::WAS_INSTALLED_BY_DEFAULT))
1133      installer->set_allow_silent_install(true);
1134
1135    content::WindowedNotificationObserver observer(
1136        extensions::NOTIFICATION_CRX_INSTALLER_DONE,
1137        content::Source<extensions::CrxInstaller>(installer.get()));
1138
1139    installer->InstallCrx(crx_path);
1140
1141    observer.Wait();
1142  }
1143
1144  size_t expected_extensions_count_;
1145  content::NotificationRegistrar registrar_;
1146};
1147
1148// Receives notifications from a PackExtensionJob, indicating either that
1149// packing succeeded or that there was some error.
1150class PackExtensionTestClient : public extensions::PackExtensionJob::Client {
1151 public:
1152  PackExtensionTestClient(const base::FilePath& expected_crx_path,
1153                          const base::FilePath& expected_private_key_path);
1154  virtual void OnPackSuccess(const base::FilePath& crx_path,
1155                             const base::FilePath& private_key_path) OVERRIDE;
1156  virtual void OnPackFailure(const std::string& error_message,
1157                             ExtensionCreator::ErrorType type) OVERRIDE;
1158
1159 private:
1160  const base::FilePath expected_crx_path_;
1161  const base::FilePath expected_private_key_path_;
1162  DISALLOW_COPY_AND_ASSIGN(PackExtensionTestClient);
1163};
1164
1165PackExtensionTestClient::PackExtensionTestClient(
1166    const base::FilePath& expected_crx_path,
1167    const base::FilePath& expected_private_key_path)
1168    : expected_crx_path_(expected_crx_path),
1169      expected_private_key_path_(expected_private_key_path) {}
1170
1171// If packing succeeded, we make sure that the package names match our
1172// expectations.
1173void PackExtensionTestClient::OnPackSuccess(
1174    const base::FilePath& crx_path,
1175    const base::FilePath& private_key_path) {
1176  // We got the notification and processed it; we don't expect any further tasks
1177  // to be posted to the current thread, so we should stop blocking and continue
1178  // on with the rest of the test.
1179  // This call to |Quit()| matches the call to |Run()| in the
1180  // |PackPunctuatedExtension| test.
1181  base::MessageLoop::current()->Quit();
1182  EXPECT_EQ(expected_crx_path_.value(), crx_path.value());
1183  EXPECT_EQ(expected_private_key_path_.value(), private_key_path.value());
1184  ASSERT_TRUE(base::PathExists(private_key_path));
1185}
1186
1187// The tests are designed so that we never expect to see a packing error.
1188void PackExtensionTestClient::OnPackFailure(const std::string& error_message,
1189                                            ExtensionCreator::ErrorType type) {
1190  if (type == ExtensionCreator::kCRXExists)
1191     FAIL() << "Packing should not fail.";
1192  else
1193     FAIL() << "Existing CRX should have been overwritten.";
1194}
1195
1196// Test loading good extensions from the profile directory.
1197TEST_F(ExtensionServiceTest, LoadAllExtensionsFromDirectorySuccess) {
1198  InitPluginService();
1199  InitializeGoodInstalledExtensionService();
1200  service()->Init();
1201
1202  uint32 expected_num_extensions = 3u;
1203  ASSERT_EQ(expected_num_extensions, loaded_.size());
1204
1205  EXPECT_EQ(std::string(good0), loaded_[0]->id());
1206  EXPECT_EQ(std::string("My extension 1"),
1207            loaded_[0]->name());
1208  EXPECT_EQ(std::string("The first extension that I made."),
1209            loaded_[0]->description());
1210  EXPECT_EQ(Manifest::INTERNAL, loaded_[0]->location());
1211  EXPECT_TRUE(service()->GetExtensionById(loaded_[0]->id(), false));
1212  EXPECT_EQ(expected_num_extensions, registry()->enabled_extensions().size());
1213
1214  ValidatePrefKeyCount(3);
1215  ValidateIntegerPref(good0, "state", Extension::ENABLED);
1216  ValidateIntegerPref(good0, "location", Manifest::INTERNAL);
1217  ValidateIntegerPref(good1, "state", Extension::ENABLED);
1218  ValidateIntegerPref(good1, "location", Manifest::INTERNAL);
1219  ValidateIntegerPref(good2, "state", Extension::ENABLED);
1220  ValidateIntegerPref(good2, "location", Manifest::INTERNAL);
1221
1222  URLPatternSet expected_patterns;
1223  AddPattern(&expected_patterns, "file:///*");
1224  AddPattern(&expected_patterns, "http://*.google.com/*");
1225  AddPattern(&expected_patterns, "https://*.google.com/*");
1226  const Extension* extension = loaded_[0].get();
1227  const extensions::UserScriptList& scripts =
1228      extensions::ContentScriptsInfo::GetContentScripts(extension);
1229  ASSERT_EQ(2u, scripts.size());
1230  EXPECT_EQ(expected_patterns, scripts[0].url_patterns());
1231  EXPECT_EQ(2u, scripts[0].js_scripts().size());
1232  ExtensionResource resource00(extension->id(),
1233                               scripts[0].js_scripts()[0].extension_root(),
1234                               scripts[0].js_scripts()[0].relative_path());
1235  base::FilePath expected_path =
1236      base::MakeAbsoluteFilePath(extension->path().AppendASCII("script1.js"));
1237  EXPECT_TRUE(resource00.ComparePathWithDefault(expected_path));
1238  ExtensionResource resource01(extension->id(),
1239                               scripts[0].js_scripts()[1].extension_root(),
1240                               scripts[0].js_scripts()[1].relative_path());
1241  expected_path =
1242      base::MakeAbsoluteFilePath(extension->path().AppendASCII("script2.js"));
1243  EXPECT_TRUE(resource01.ComparePathWithDefault(expected_path));
1244  EXPECT_TRUE(!extensions::PluginInfo::HasPlugins(extension));
1245  EXPECT_EQ(1u, scripts[1].url_patterns().patterns().size());
1246  EXPECT_EQ("http://*.news.com/*",
1247            scripts[1].url_patterns().begin()->GetAsString());
1248  ExtensionResource resource10(extension->id(),
1249                               scripts[1].js_scripts()[0].extension_root(),
1250                               scripts[1].js_scripts()[0].relative_path());
1251  expected_path =
1252      extension->path().AppendASCII("js_files").AppendASCII("script3.js");
1253  expected_path = base::MakeAbsoluteFilePath(expected_path);
1254  EXPECT_TRUE(resource10.ComparePathWithDefault(expected_path));
1255
1256  expected_patterns.ClearPatterns();
1257  AddPattern(&expected_patterns, "http://*.google.com/*");
1258  AddPattern(&expected_patterns, "https://*.google.com/*");
1259  EXPECT_EQ(
1260      expected_patterns,
1261      extension->permissions_data()->active_permissions()->explicit_hosts());
1262
1263  EXPECT_EQ(std::string(good1), loaded_[1]->id());
1264  EXPECT_EQ(std::string("My extension 2"), loaded_[1]->name());
1265  EXPECT_EQ(std::string(), loaded_[1]->description());
1266  EXPECT_EQ(loaded_[1]->GetResourceURL("background.html"),
1267            extensions::BackgroundInfo::GetBackgroundURL(loaded_[1].get()));
1268  EXPECT_EQ(0u,
1269            extensions::ContentScriptsInfo::GetContentScripts(loaded_[1].get())
1270                .size());
1271
1272  // We don't parse the plugins section on Chrome OS.
1273#if defined(OS_CHROMEOS)
1274  EXPECT_TRUE(!extensions::PluginInfo::HasPlugins(loaded_[1].get()));
1275#else
1276  ASSERT_TRUE(extensions::PluginInfo::HasPlugins(loaded_[1].get()));
1277  const std::vector<extensions::PluginInfo>* plugins =
1278      extensions::PluginInfo::GetPlugins(loaded_[1].get());
1279  ASSERT_TRUE(plugins);
1280  ASSERT_EQ(2u, plugins->size());
1281  EXPECT_EQ(loaded_[1]->path().AppendASCII("content_plugin.dll").value(),
1282            plugins->at(0).path.value());
1283  EXPECT_TRUE(plugins->at(0).is_public);
1284  EXPECT_EQ(loaded_[1]->path().AppendASCII("extension_plugin.dll").value(),
1285            plugins->at(1).path.value());
1286  EXPECT_FALSE(plugins->at(1).is_public);
1287#endif
1288
1289  EXPECT_EQ(Manifest::INTERNAL, loaded_[1]->location());
1290
1291  int index = expected_num_extensions - 1;
1292  EXPECT_EQ(std::string(good2), loaded_[index]->id());
1293  EXPECT_EQ(std::string("My extension 3"), loaded_[index]->name());
1294  EXPECT_EQ(std::string(), loaded_[index]->description());
1295  EXPECT_EQ(0u,
1296            extensions::ContentScriptsInfo::GetContentScripts(
1297                loaded_[index].get()).size());
1298  EXPECT_EQ(Manifest::INTERNAL, loaded_[index]->location());
1299};
1300
1301// Test loading bad extensions from the profile directory.
1302TEST_F(ExtensionServiceTest, LoadAllExtensionsFromDirectoryFail) {
1303  // Initialize the test dir with a bad Preferences/extensions.
1304  base::FilePath source_install_dir =
1305      data_dir().AppendASCII("bad").AppendASCII("Extensions");
1306  base::FilePath pref_path =
1307      source_install_dir.DirName().Append(chrome::kPreferencesFilename);
1308
1309  InitializeInstalledExtensionService(pref_path, source_install_dir);
1310
1311  service()->Init();
1312
1313  ASSERT_EQ(4u, GetErrors().size());
1314  ASSERT_EQ(0u, loaded_.size());
1315
1316  EXPECT_TRUE(MatchPattern(base::UTF16ToUTF8(GetErrors()[0]),
1317      std::string("Could not load extension from '*'. ") +
1318      extensions::manifest_errors::kManifestUnreadable)) <<
1319      base::UTF16ToUTF8(GetErrors()[0]);
1320
1321  EXPECT_TRUE(MatchPattern(base::UTF16ToUTF8(GetErrors()[1]),
1322      std::string("Could not load extension from '*'. ") +
1323      extensions::manifest_errors::kManifestUnreadable)) <<
1324      base::UTF16ToUTF8(GetErrors()[1]);
1325
1326  EXPECT_TRUE(MatchPattern(base::UTF16ToUTF8(GetErrors()[2]),
1327      std::string("Could not load extension from '*'. ") +
1328      extensions::manifest_errors::kMissingFile)) <<
1329      base::UTF16ToUTF8(GetErrors()[2]);
1330
1331  EXPECT_TRUE(MatchPattern(base::UTF16ToUTF8(GetErrors()[3]),
1332      std::string("Could not load extension from '*'. ") +
1333      extensions::manifest_errors::kManifestUnreadable)) <<
1334      base::UTF16ToUTF8(GetErrors()[3]);
1335};
1336
1337// Test various cases for delayed install because of missing imports.
1338TEST_F(ExtensionServiceTest, PendingImports) {
1339  InitPluginService();
1340
1341  base::FilePath source_install_dir =
1342      data_dir().AppendASCII("pending_updates_with_imports").AppendASCII(
1343          "Extensions");
1344  base::FilePath pref_path =
1345      source_install_dir.DirName().Append(chrome::kPreferencesFilename);
1346
1347  InitializeInstalledExtensionService(pref_path, source_install_dir);
1348
1349  // Verify there are no pending extensions initially.
1350  EXPECT_FALSE(service()->pending_extension_manager()->HasPendingExtensions());
1351
1352  service()->Init();
1353  // Wait for GarbageCollectExtensions task to complete.
1354  base::RunLoop().RunUntilIdle();
1355
1356  // These extensions are used by the extensions we test below, they must be
1357  // installed.
1358  EXPECT_TRUE(base::PathExists(extensions_install_dir().AppendASCII(
1359      "bjafgdebaacbbbecmhlhpofkepfkgcpa/1.0")));
1360  EXPECT_TRUE(base::PathExists(extensions_install_dir().AppendASCII(
1361      "hpiknbiabeeppbpihjehijgoemciehgk/2")));
1362
1363  // Each of these extensions should have been rejected because of dependencies
1364  // that cannot be satisfied.
1365  ExtensionPrefs* prefs = ExtensionPrefs::Get(profile());
1366  EXPECT_FALSE(
1367      prefs->GetDelayedInstallInfo("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"));
1368  EXPECT_FALSE(
1369      prefs->GetInstalledExtensionInfo("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"));
1370  EXPECT_FALSE(
1371      prefs->GetDelayedInstallInfo("bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"));
1372  EXPECT_FALSE(
1373      prefs->GetInstalledExtensionInfo("bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"));
1374  EXPECT_FALSE(
1375      prefs->GetDelayedInstallInfo("cccccccccccccccccccccccccccccccc"));
1376  EXPECT_FALSE(
1377      prefs->GetInstalledExtensionInfo("cccccccccccccccccccccccccccccccc"));
1378
1379  // Make sure the import started for the extension with a dependency.
1380  EXPECT_TRUE(
1381      prefs->GetDelayedInstallInfo("behllobkkfkfnphdnhnkndlbkcpglgmj"));
1382  EXPECT_EQ(ExtensionPrefs::DELAY_REASON_WAIT_FOR_IMPORTS,
1383      prefs->GetDelayedInstallReason("behllobkkfkfnphdnhnkndlbkcpglgmj"));
1384
1385  EXPECT_FALSE(base::PathExists(extensions_install_dir().AppendASCII(
1386      "behllobkkfkfnphdnhnkndlbkcpglgmj/1.0.0.0")));
1387
1388  EXPECT_TRUE(service()->pending_extension_manager()->HasPendingExtensions());
1389  std::string pending_id("eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee");
1390  EXPECT_TRUE(service()->pending_extension_manager()->IsIdPending(pending_id));
1391  // Remove it because we are not testing the pending extension manager's
1392  // ability to download and install extensions.
1393  EXPECT_TRUE(service()->pending_extension_manager()->Remove(pending_id));
1394}
1395
1396// Test installing extensions. This test tries to install few extensions using
1397// crx files. If you need to change those crx files, feel free to repackage
1398// them, throw away the key used and change the id's above.
1399TEST_F(ExtensionServiceTest, InstallExtension) {
1400  InitializeEmptyExtensionService();
1401
1402  // Extensions not enabled.
1403  service()->set_extensions_enabled(false);
1404  base::FilePath path = data_dir().AppendASCII("good.crx");
1405  InstallCRX(path, INSTALL_FAILED);
1406  service()->set_extensions_enabled(true);
1407
1408  ValidatePrefKeyCount(0);
1409
1410  // A simple extension that should install without error.
1411  path = data_dir().AppendASCII("good.crx");
1412  InstallCRX(path, INSTALL_NEW);
1413  // TODO(erikkay): verify the contents of the installed extension.
1414
1415  int pref_count = 0;
1416  ValidatePrefKeyCount(++pref_count);
1417  ValidateIntegerPref(good_crx, "state", Extension::ENABLED);
1418  ValidateIntegerPref(good_crx, "location", Manifest::INTERNAL);
1419
1420  // An extension with page actions.
1421  path = data_dir().AppendASCII("page_action.crx");
1422  InstallCRX(path, INSTALL_NEW);
1423  ValidatePrefKeyCount(++pref_count);
1424  ValidateIntegerPref(page_action, "state", Extension::ENABLED);
1425  ValidateIntegerPref(page_action, "location", Manifest::INTERNAL);
1426
1427  // Bad signature.
1428  path = data_dir().AppendASCII("bad_signature.crx");
1429  InstallCRX(path, INSTALL_FAILED);
1430  ValidatePrefKeyCount(pref_count);
1431
1432  // 0-length extension file.
1433  path = data_dir().AppendASCII("not_an_extension.crx");
1434  InstallCRX(path, INSTALL_FAILED);
1435  ValidatePrefKeyCount(pref_count);
1436
1437  // Bad magic number.
1438  path = data_dir().AppendASCII("bad_magic.crx");
1439  InstallCRX(path, INSTALL_FAILED);
1440  ValidatePrefKeyCount(pref_count);
1441
1442  // Packed extensions may have folders or files that have underscores.
1443  // This will only cause a warning, rather than a fatal error.
1444  path = data_dir().AppendASCII("bad_underscore.crx");
1445  InstallCRX(path, INSTALL_NEW);
1446  ValidatePrefKeyCount(++pref_count);
1447
1448  // A test for an extension with a 2048-bit public key.
1449  path = data_dir().AppendASCII("good2048.crx");
1450  InstallCRX(path, INSTALL_NEW);
1451  ValidatePrefKeyCount(++pref_count);
1452  ValidateIntegerPref(good2048, "state", Extension::ENABLED);
1453  ValidateIntegerPref(good2048, "location", Manifest::INTERNAL);
1454
1455  // TODO(erikkay): add more tests for many of the failure cases.
1456  // TODO(erikkay): add tests for upgrade cases.
1457}
1458
1459struct MockExtensionRegistryObserver
1460    : public extensions::ExtensionRegistryObserver {
1461  virtual void OnExtensionWillBeInstalled(
1462      content::BrowserContext* browser_context,
1463      const Extension* extension,
1464      bool is_update,
1465      bool from_ephemeral,
1466      const std::string& old_name) OVERRIDE {
1467    last_extension_installed = extension->id();
1468  }
1469
1470  virtual void OnExtensionUninstalled(
1471      content::BrowserContext* browser_context,
1472      const Extension* extension,
1473      extensions::UninstallReason reason) OVERRIDE {
1474    last_extension_uninstalled = extension->id();
1475  }
1476
1477  std::string last_extension_installed;
1478  std::string last_extension_uninstalled;
1479};
1480
1481// Test that correct notifications are sent to ExtensionRegistryObserver on
1482// extension install and uninstall.
1483TEST_F(ExtensionServiceTest, InstallObserverNotified) {
1484  InitializeEmptyExtensionService();
1485
1486  extensions::ExtensionRegistry* registry(
1487      extensions::ExtensionRegistry::Get(profile()));
1488  MockExtensionRegistryObserver observer;
1489  registry->AddObserver(&observer);
1490
1491  // A simple extension that should install without error.
1492  ASSERT_TRUE(observer.last_extension_installed.empty());
1493  base::FilePath path = data_dir().AppendASCII("good.crx");
1494  InstallCRX(path, INSTALL_NEW);
1495  ASSERT_EQ(good_crx, observer.last_extension_installed);
1496
1497  // Uninstall the extension.
1498  ASSERT_TRUE(observer.last_extension_uninstalled.empty());
1499  UninstallExtension(good_crx, false);
1500  ASSERT_EQ(good_crx, observer.last_extension_uninstalled);
1501
1502  registry->RemoveObserver(&observer);
1503}
1504
1505// Tests that flags passed to OnExternalExtensionFileFound() make it to the
1506// extension object.
1507TEST_F(ExtensionServiceTest, InstallingExternalExtensionWithFlags) {
1508  const char kPrefFromBookmark[] = "from_bookmark";
1509
1510  InitializeEmptyExtensionService();
1511
1512  base::FilePath path = data_dir().AppendASCII("good.crx");
1513  service()->set_extensions_enabled(true);
1514
1515  // Register and install an external extension.
1516  Version version("1.0.0.0");
1517  content::WindowedNotificationObserver observer(
1518      extensions::NOTIFICATION_CRX_INSTALLER_DONE,
1519      content::NotificationService::AllSources());
1520  if (service()->OnExternalExtensionFileFound(good_crx,
1521                                              &version,
1522                                              path,
1523                                              Manifest::EXTERNAL_PREF,
1524                                              Extension::FROM_BOOKMARK,
1525                                              false /* mark_acknowledged */)) {
1526    observer.Wait();
1527  }
1528
1529  const Extension* extension = service()->GetExtensionById(good_crx, false);
1530  ASSERT_TRUE(extension);
1531  ASSERT_TRUE(extension->from_bookmark());
1532  ASSERT_TRUE(ValidateBooleanPref(good_crx, kPrefFromBookmark, true));
1533
1534  // Upgrade to version 2.0, the flag should be preserved.
1535  path = data_dir().AppendASCII("good2.crx");
1536  UpdateExtension(good_crx, path, ENABLED);
1537  ASSERT_TRUE(ValidateBooleanPref(good_crx, kPrefFromBookmark, true));
1538  extension = service()->GetExtensionById(good_crx, false);
1539  ASSERT_TRUE(extension);
1540  ASSERT_TRUE(extension->from_bookmark());
1541}
1542
1543// Test the handling of Extension::EXTERNAL_EXTENSION_UNINSTALLED
1544TEST_F(ExtensionServiceTest, UninstallingExternalExtensions) {
1545  InitializeEmptyExtensionService();
1546
1547  base::FilePath path = data_dir().AppendASCII("good.crx");
1548  service()->set_extensions_enabled(true);
1549
1550  // Install an external extension.
1551  Version version("1.0.0.0");
1552  content::WindowedNotificationObserver observer(
1553      extensions::NOTIFICATION_CRX_INSTALLER_DONE,
1554      content::NotificationService::AllSources());
1555  if (service()->OnExternalExtensionFileFound(good_crx,
1556                                              &version,
1557                                              path,
1558                                              Manifest::EXTERNAL_PREF,
1559                                              Extension::NO_FLAGS,
1560                                              false)) {
1561    observer.Wait();
1562  }
1563
1564  ASSERT_TRUE(service()->GetExtensionById(good_crx, false));
1565
1566  // Uninstall it and check that its killbit gets set.
1567  UninstallExtension(good_crx, false);
1568  ValidateIntegerPref(good_crx, "state",
1569                      Extension::EXTERNAL_EXTENSION_UNINSTALLED);
1570
1571  // Try to re-install it externally. This should fail because of the killbit.
1572  service()->OnExternalExtensionFileFound(good_crx,
1573                                          &version,
1574                                          path,
1575                                          Manifest::EXTERNAL_PREF,
1576                                          Extension::NO_FLAGS,
1577                                          false);
1578  base::RunLoop().RunUntilIdle();
1579  ASSERT_TRUE(NULL == service()->GetExtensionById(good_crx, false));
1580  ValidateIntegerPref(good_crx, "state",
1581                      Extension::EXTERNAL_EXTENSION_UNINSTALLED);
1582
1583  version = Version("1.0.0.1");
1584  // Repeat the same thing with a newer version of the extension.
1585  path = data_dir().AppendASCII("good2.crx");
1586  service()->OnExternalExtensionFileFound(good_crx,
1587                                          &version,
1588                                          path,
1589                                          Manifest::EXTERNAL_PREF,
1590                                          Extension::NO_FLAGS,
1591                                          false);
1592  base::RunLoop().RunUntilIdle();
1593  ASSERT_TRUE(NULL == service()->GetExtensionById(good_crx, false));
1594  ValidateIntegerPref(good_crx, "state",
1595                      Extension::EXTERNAL_EXTENSION_UNINSTALLED);
1596
1597  // Try adding the same extension from an external update URL.
1598  ASSERT_FALSE(service()->pending_extension_manager()->AddFromExternalUpdateUrl(
1599      good_crx,
1600      std::string(),
1601      GURL("http:://fake.update/url"),
1602      Manifest::EXTERNAL_PREF_DOWNLOAD,
1603      Extension::NO_FLAGS,
1604      false));
1605
1606  ASSERT_FALSE(service()->pending_extension_manager()->IsIdPending(good_crx));
1607}
1608
1609// Test that uninstalling an external extension does not crash when
1610// the extension could not be loaded.
1611// This extension shown in preferences file requires an experimental permission.
1612// It could not be loaded without such permission.
1613TEST_F(ExtensionServiceTest, UninstallingNotLoadedExtension) {
1614  base::FilePath source_install_dir =
1615      data_dir().AppendASCII("good").AppendASCII("Extensions");
1616  // The preference contains an external extension
1617  // that requires 'experimental' permission.
1618  base::FilePath pref_path = source_install_dir
1619      .DirName()
1620      .AppendASCII("PreferencesExperimental");
1621
1622  // Aforementioned extension will not be loaded if
1623  // there is no '--enable-experimental-extension-apis' command line flag.
1624  InitializeInstalledExtensionService(pref_path, source_install_dir);
1625
1626  service()->Init();
1627
1628  // Check and try to uninstall it.
1629  // If we don't check whether the extension is loaded before we uninstall it
1630  // in CheckExternalUninstall, a crash will happen here because we will get or
1631  // dereference a NULL pointer (extension) inside UninstallExtension.
1632  MockExtensionProvider provider(NULL, Manifest::EXTERNAL_REGISTRY);
1633  service()->OnExternalProviderReady(&provider);
1634}
1635
1636// Test that external extensions with incorrect IDs are not installed.
1637TEST_F(ExtensionServiceTest, FailOnWrongId) {
1638  InitializeEmptyExtensionService();
1639  base::FilePath path = data_dir().AppendASCII("good.crx");
1640  service()->set_extensions_enabled(true);
1641
1642  Version version("1.0.0.0");
1643
1644  const std::string wrong_id = all_zero;
1645  const std::string correct_id = good_crx;
1646  ASSERT_NE(correct_id, wrong_id);
1647
1648  // Install an external extension with an ID from the external
1649  // source that is not equal to the ID in the extension manifest.
1650  content::WindowedNotificationObserver observer(
1651      extensions::NOTIFICATION_CRX_INSTALLER_DONE,
1652      content::NotificationService::AllSources());
1653  service()->OnExternalExtensionFileFound(wrong_id,
1654                                          &version,
1655                                          path,
1656                                          Manifest::EXTERNAL_PREF,
1657                                          Extension::NO_FLAGS,
1658                                          false);
1659
1660  observer.Wait();
1661  ASSERT_FALSE(service()->GetExtensionById(good_crx, false));
1662
1663  // Try again with the right ID. Expect success.
1664  content::WindowedNotificationObserver observer2(
1665      extensions::NOTIFICATION_CRX_INSTALLER_DONE,
1666      content::NotificationService::AllSources());
1667  if (service()->OnExternalExtensionFileFound(correct_id,
1668                                              &version,
1669                                              path,
1670                                              Manifest::EXTERNAL_PREF,
1671                                              Extension::NO_FLAGS,
1672                                              false)) {
1673    observer2.Wait();
1674  }
1675  ASSERT_TRUE(service()->GetExtensionById(good_crx, false));
1676}
1677
1678// Test that external extensions with incorrect versions are not installed.
1679TEST_F(ExtensionServiceTest, FailOnWrongVersion) {
1680  InitializeEmptyExtensionService();
1681  base::FilePath path = data_dir().AppendASCII("good.crx");
1682  service()->set_extensions_enabled(true);
1683
1684  // Install an external extension with a version from the external
1685  // source that is not equal to the version in the extension manifest.
1686  Version wrong_version("1.2.3.4");
1687  content::WindowedNotificationObserver observer(
1688      extensions::NOTIFICATION_CRX_INSTALLER_DONE,
1689      content::NotificationService::AllSources());
1690  service()->OnExternalExtensionFileFound(good_crx,
1691                                          &wrong_version,
1692                                          path,
1693                                          Manifest::EXTERNAL_PREF,
1694                                          Extension::NO_FLAGS,
1695                                          false);
1696
1697  observer.Wait();
1698  ASSERT_FALSE(service()->GetExtensionById(good_crx, false));
1699
1700  // Try again with the right version. Expect success.
1701  service()->pending_extension_manager()->Remove(good_crx);
1702  Version correct_version("1.0.0.0");
1703  content::WindowedNotificationObserver observer2(
1704      extensions::NOTIFICATION_CRX_INSTALLER_DONE,
1705      content::NotificationService::AllSources());
1706  if (service()->OnExternalExtensionFileFound(good_crx,
1707                                              &correct_version,
1708                                              path,
1709                                              Manifest::EXTERNAL_PREF,
1710                                              Extension::NO_FLAGS,
1711                                              false)) {
1712    observer2.Wait();
1713  }
1714  ASSERT_TRUE(service()->GetExtensionById(good_crx, false));
1715}
1716
1717// Install a user script (they get converted automatically to an extension)
1718TEST_F(ExtensionServiceTest, InstallUserScript) {
1719  // The details of script conversion are tested elsewhere, this just tests
1720  // integration with ExtensionService.
1721  InitializeEmptyExtensionService();
1722
1723  base::FilePath path = data_dir().AppendASCII("user_script_basic.user.js");
1724
1725  ASSERT_TRUE(base::PathExists(path));
1726  scoped_refptr<CrxInstaller> installer(CrxInstaller::CreateSilent(service()));
1727  installer->set_allow_silent_install(true);
1728  installer->InstallUserScript(
1729      path,
1730      GURL("http://www.aaronboodman.com/scripts/user_script_basic.user.js"));
1731
1732  base::RunLoop().RunUntilIdle();
1733  std::vector<base::string16> errors = GetErrors();
1734  EXPECT_TRUE(installed_) << "Nothing was installed.";
1735  EXPECT_FALSE(was_update_) << path.value();
1736  ASSERT_EQ(1u, loaded_.size()) << "Nothing was loaded.";
1737  EXPECT_EQ(0u, errors.size()) << "There were errors: "
1738                               << JoinString(errors, ',');
1739  EXPECT_TRUE(service()->GetExtensionById(loaded_[0]->id(), false))
1740      << path.value();
1741
1742  installed_ = NULL;
1743  was_update_ = false;
1744  loaded_.clear();
1745  ExtensionErrorReporter::GetInstance()->ClearErrors();
1746}
1747
1748// Extensions don't install during shutdown.
1749TEST_F(ExtensionServiceTest, InstallExtensionDuringShutdown) {
1750  InitializeEmptyExtensionService();
1751
1752  // Simulate shutdown.
1753  service()->set_browser_terminating_for_test(true);
1754
1755  base::FilePath path = data_dir().AppendASCII("good.crx");
1756  scoped_refptr<CrxInstaller> installer(CrxInstaller::CreateSilent(service()));
1757  installer->set_allow_silent_install(true);
1758  installer->InstallCrx(path);
1759  base::RunLoop().RunUntilIdle();
1760
1761  EXPECT_FALSE(installed_) << "Extension installed during shutdown.";
1762  ASSERT_EQ(0u, loaded_.size()) << "Extension loaded during shutdown.";
1763}
1764
1765// This tests that the granted permissions preferences are correctly set when
1766// installing an extension.
1767TEST_F(ExtensionServiceTest, GrantedPermissions) {
1768  InitializeEmptyExtensionService();
1769  base::FilePath path = data_dir().AppendASCII("permissions");
1770
1771  base::FilePath pem_path = path.AppendASCII("unknown.pem");
1772  path = path.AppendASCII("unknown");
1773
1774  ASSERT_TRUE(base::PathExists(pem_path));
1775  ASSERT_TRUE(base::PathExists(path));
1776
1777  ExtensionPrefs* prefs = ExtensionPrefs::Get(profile());
1778
1779  APIPermissionSet expected_api_perms;
1780  URLPatternSet expected_host_perms;
1781
1782  // Make sure there aren't any granted permissions before the
1783  // extension is installed.
1784  scoped_refptr<PermissionSet> known_perms(
1785      prefs->GetGrantedPermissions(permissions_crx));
1786  EXPECT_FALSE(known_perms.get());
1787
1788  const Extension* extension = PackAndInstallCRX(path, pem_path, INSTALL_NEW);
1789
1790  EXPECT_EQ(0u, GetErrors().size());
1791  ASSERT_EQ(1u, registry()->enabled_extensions().size());
1792  EXPECT_EQ(permissions_crx, extension->id());
1793
1794  // Verify that the valid API permissions have been recognized.
1795  expected_api_perms.insert(APIPermission::kTab);
1796
1797  AddPattern(&expected_host_perms, "http://*.google.com/*");
1798  AddPattern(&expected_host_perms, "https://*.google.com/*");
1799  AddPattern(&expected_host_perms, "http://*.google.com.hk/*");
1800  AddPattern(&expected_host_perms, "http://www.example.com/*");
1801
1802  known_perms = prefs->GetGrantedPermissions(extension->id());
1803  EXPECT_TRUE(known_perms.get());
1804  EXPECT_FALSE(known_perms->IsEmpty());
1805  EXPECT_EQ(expected_api_perms, known_perms->apis());
1806  EXPECT_FALSE(known_perms->HasEffectiveFullAccess());
1807  EXPECT_EQ(expected_host_perms, known_perms->effective_hosts());
1808}
1809
1810
1811#if !defined(OS_CHROMEOS)
1812// This tests that the granted permissions preferences are correctly set for
1813// default apps.
1814TEST_F(ExtensionServiceTest, DefaultAppsGrantedPermissions) {
1815  InitializeEmptyExtensionService();
1816  base::FilePath path = data_dir().AppendASCII("permissions");
1817
1818  base::FilePath pem_path = path.AppendASCII("unknown.pem");
1819  path = path.AppendASCII("unknown");
1820
1821  ASSERT_TRUE(base::PathExists(pem_path));
1822  ASSERT_TRUE(base::PathExists(path));
1823
1824  ExtensionPrefs* prefs = ExtensionPrefs::Get(profile());
1825
1826  APIPermissionSet expected_api_perms;
1827  URLPatternSet expected_host_perms;
1828
1829  // Make sure there aren't any granted permissions before the
1830  // extension is installed.
1831  scoped_refptr<PermissionSet> known_perms(
1832      prefs->GetGrantedPermissions(permissions_crx));
1833  EXPECT_FALSE(known_perms.get());
1834
1835  const Extension* extension = PackAndInstallCRX(
1836      path, pem_path, INSTALL_NEW, Extension::WAS_INSTALLED_BY_DEFAULT);
1837
1838  EXPECT_EQ(0u, GetErrors().size());
1839  ASSERT_EQ(1u, registry()->enabled_extensions().size());
1840  EXPECT_EQ(permissions_crx, extension->id());
1841
1842  // Verify that the valid API permissions have been recognized.
1843  expected_api_perms.insert(APIPermission::kTab);
1844
1845  known_perms = prefs->GetGrantedPermissions(extension->id());
1846  EXPECT_TRUE(known_perms.get());
1847  EXPECT_FALSE(known_perms->IsEmpty());
1848  EXPECT_EQ(expected_api_perms, known_perms->apis());
1849  EXPECT_FALSE(known_perms->HasEffectiveFullAccess());
1850}
1851#endif
1852
1853#if !defined(OS_POSIX) || defined(OS_MACOSX)
1854// Tests that the granted permissions full_access bit gets set correctly when
1855// an extension contains an NPAPI plugin.
1856// Only run this on platforms that support NPAPI plugins.
1857TEST_F(ExtensionServiceTest, GrantedFullAccessPermissions) {
1858  InitPluginService();
1859
1860  InitializeEmptyExtensionService();
1861
1862  ASSERT_TRUE(base::PathExists(good1_path()));
1863  const Extension* extension = PackAndInstallCRX(good1_path(), INSTALL_NEW);
1864  EXPECT_EQ(0u, GetErrors().size());
1865  EXPECT_EQ(1u, registry()->enabled_extensions().size());
1866  ExtensionPrefs* prefs = ExtensionPrefs::Get(profile());
1867
1868  scoped_refptr<PermissionSet> permissions(
1869      prefs->GetGrantedPermissions(extension->id()));
1870  EXPECT_FALSE(permissions->IsEmpty());
1871  EXPECT_TRUE(permissions->HasEffectiveFullAccess());
1872  EXPECT_FALSE(permissions->apis().empty());
1873  EXPECT_TRUE(permissions->HasAPIPermission(APIPermission::kPlugin));
1874
1875  // Full access implies full host access too...
1876  EXPECT_TRUE(permissions->HasEffectiveAccessToAllHosts());
1877}
1878#endif
1879
1880// Tests that the extension is disabled when permissions are missing from
1881// the extension's granted permissions preferences. (This simulates updating
1882// the browser to a version which recognizes more permissions).
1883TEST_F(ExtensionServiceTest, GrantedAPIAndHostPermissions) {
1884  InitializeEmptyExtensionService();
1885
1886  base::FilePath path =
1887      data_dir().AppendASCII("permissions").AppendASCII("unknown");
1888
1889  ASSERT_TRUE(base::PathExists(path));
1890
1891  const Extension* extension = PackAndInstallCRX(path, INSTALL_NEW);
1892
1893  EXPECT_EQ(0u, GetErrors().size());
1894  EXPECT_EQ(1u, registry()->enabled_extensions().size());
1895  std::string extension_id = extension->id();
1896
1897  ExtensionPrefs* prefs = ExtensionPrefs::Get(profile());
1898
1899  APIPermissionSet expected_api_permissions;
1900  URLPatternSet expected_host_permissions;
1901
1902  expected_api_permissions.insert(APIPermission::kTab);
1903  AddPattern(&expected_host_permissions, "http://*.google.com/*");
1904  AddPattern(&expected_host_permissions, "https://*.google.com/*");
1905  AddPattern(&expected_host_permissions, "http://*.google.com.hk/*");
1906  AddPattern(&expected_host_permissions, "http://www.example.com/*");
1907
1908  std::set<std::string> host_permissions;
1909
1910  // Test that the extension is disabled when an API permission is missing from
1911  // the extension's granted api permissions preference. (This simulates
1912  // updating the browser to a version which recognizes a new API permission).
1913  SetPref(extension_id, "granted_permissions.api",
1914          new base::ListValue(), "granted_permissions.api");
1915  service()->ReloadExtensionsForTest();
1916
1917  EXPECT_EQ(1u, registry()->disabled_extensions().size());
1918  extension = registry()->disabled_extensions().begin()->get();
1919
1920  ASSERT_TRUE(prefs->IsExtensionDisabled(extension_id));
1921  ASSERT_FALSE(service()->IsExtensionEnabled(extension_id));
1922  ASSERT_TRUE(prefs->DidExtensionEscalatePermissions(extension_id));
1923
1924  // Now grant and re-enable the extension, making sure the prefs are updated.
1925  service()->GrantPermissionsAndEnableExtension(extension);
1926
1927  ASSERT_FALSE(prefs->IsExtensionDisabled(extension_id));
1928  ASSERT_TRUE(service()->IsExtensionEnabled(extension_id));
1929  ASSERT_FALSE(prefs->DidExtensionEscalatePermissions(extension_id));
1930
1931  scoped_refptr<PermissionSet> current_perms(
1932      prefs->GetGrantedPermissions(extension_id));
1933  ASSERT_TRUE(current_perms.get());
1934  ASSERT_FALSE(current_perms->IsEmpty());
1935  ASSERT_FALSE(current_perms->HasEffectiveFullAccess());
1936  ASSERT_EQ(expected_api_permissions, current_perms->apis());
1937  ASSERT_EQ(expected_host_permissions, current_perms->effective_hosts());
1938
1939  // Tests that the extension is disabled when a host permission is missing from
1940  // the extension's granted host permissions preference. (This simulates
1941  // updating the browser to a version which recognizes additional host
1942  // permissions).
1943  host_permissions.clear();
1944  current_perms = NULL;
1945
1946  host_permissions.insert("http://*.google.com/*");
1947  host_permissions.insert("https://*.google.com/*");
1948  host_permissions.insert("http://*.google.com.hk/*");
1949
1950  base::ListValue* api_permissions = new base::ListValue();
1951  api_permissions->Append(
1952      new base::StringValue("tabs"));
1953  SetPref(extension_id, "granted_permissions.api",
1954          api_permissions, "granted_permissions.api");
1955  SetPrefStringSet(
1956      extension_id, "granted_permissions.scriptable_host", host_permissions);
1957
1958  service()->ReloadExtensionsForTest();
1959
1960  EXPECT_EQ(1u, registry()->disabled_extensions().size());
1961  extension = registry()->disabled_extensions().begin()->get();
1962
1963  ASSERT_TRUE(prefs->IsExtensionDisabled(extension_id));
1964  ASSERT_FALSE(service()->IsExtensionEnabled(extension_id));
1965  ASSERT_TRUE(prefs->DidExtensionEscalatePermissions(extension_id));
1966
1967  // Now grant and re-enable the extension, making sure the prefs are updated.
1968  service()->GrantPermissionsAndEnableExtension(extension);
1969
1970  ASSERT_TRUE(service()->IsExtensionEnabled(extension_id));
1971  ASSERT_FALSE(prefs->DidExtensionEscalatePermissions(extension_id));
1972
1973  current_perms = prefs->GetGrantedPermissions(extension_id);
1974  ASSERT_TRUE(current_perms.get());
1975  ASSERT_FALSE(current_perms->IsEmpty());
1976  ASSERT_FALSE(current_perms->HasEffectiveFullAccess());
1977  ASSERT_EQ(expected_api_permissions, current_perms->apis());
1978  ASSERT_EQ(expected_host_permissions, current_perms->effective_hosts());
1979}
1980
1981// Test Packaging and installing an extension.
1982TEST_F(ExtensionServiceTest, PackExtension) {
1983  InitializeEmptyExtensionService();
1984  base::FilePath input_directory =
1985      data_dir()
1986          .AppendASCII("good")
1987          .AppendASCII("Extensions")
1988          .AppendASCII("behllobkkfkfnphdnhnkndlbkcpglgmj")
1989          .AppendASCII("1.0.0.0");
1990
1991  base::ScopedTempDir temp_dir;
1992  ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
1993  base::FilePath output_directory = temp_dir.path();
1994
1995  base::FilePath crx_path(output_directory.AppendASCII("ex1.crx"));
1996  base::FilePath privkey_path(output_directory.AppendASCII("privkey.pem"));
1997
1998  scoped_ptr<ExtensionCreator> creator(new ExtensionCreator());
1999  ASSERT_TRUE(creator->Run(input_directory, crx_path, base::FilePath(),
2000      privkey_path, ExtensionCreator::kNoRunFlags));
2001  ASSERT_TRUE(base::PathExists(crx_path));
2002  ASSERT_TRUE(base::PathExists(privkey_path));
2003
2004  // Repeat the run with the pem file gone, and no special flags
2005  // Should refuse to overwrite the existing crx.
2006  base::DeleteFile(privkey_path, false);
2007  ASSERT_FALSE(creator->Run(input_directory, crx_path, base::FilePath(),
2008      privkey_path, ExtensionCreator::kNoRunFlags));
2009
2010  // OK, now try it with a flag to overwrite existing crx.  Should work.
2011  ASSERT_TRUE(creator->Run(input_directory, crx_path, base::FilePath(),
2012      privkey_path, ExtensionCreator::kOverwriteCRX));
2013
2014  // Repeat the run allowing existing crx, but the existing pem is still
2015  // an error.  Should fail.
2016  ASSERT_FALSE(creator->Run(input_directory, crx_path, base::FilePath(),
2017      privkey_path, ExtensionCreator::kOverwriteCRX));
2018
2019  ASSERT_TRUE(base::PathExists(privkey_path));
2020  InstallCRX(crx_path, INSTALL_NEW);
2021
2022  // Try packing with invalid paths.
2023  creator.reset(new ExtensionCreator());
2024  ASSERT_FALSE(
2025      creator->Run(base::FilePath(), base::FilePath(), base::FilePath(),
2026                   base::FilePath(), ExtensionCreator::kOverwriteCRX));
2027
2028  // Try packing an empty directory. Should fail because an empty directory is
2029  // not a valid extension.
2030  base::ScopedTempDir temp_dir2;
2031  ASSERT_TRUE(temp_dir2.CreateUniqueTempDir());
2032  creator.reset(new ExtensionCreator());
2033  ASSERT_FALSE(creator->Run(temp_dir2.path(), crx_path, privkey_path,
2034                            base::FilePath(), ExtensionCreator::kOverwriteCRX));
2035
2036  // Try packing with an invalid manifest.
2037  std::string invalid_manifest_content = "I am not a manifest.";
2038  ASSERT_TRUE(base::WriteFile(
2039      temp_dir2.path().Append(extensions::kManifestFilename),
2040      invalid_manifest_content.c_str(), invalid_manifest_content.size()));
2041  creator.reset(new ExtensionCreator());
2042  ASSERT_FALSE(creator->Run(temp_dir2.path(), crx_path, privkey_path,
2043                            base::FilePath(), ExtensionCreator::kOverwriteCRX));
2044
2045  // Try packing with a private key that is a valid key, but invalid for the
2046  // extension.
2047  base::FilePath bad_private_key_dir =
2048      data_dir().AppendASCII("bad_private_key");
2049  crx_path = output_directory.AppendASCII("bad_private_key.crx");
2050  privkey_path = data_dir().AppendASCII("bad_private_key.pem");
2051  ASSERT_FALSE(creator->Run(bad_private_key_dir, crx_path, base::FilePath(),
2052      privkey_path, ExtensionCreator::kOverwriteCRX));
2053}
2054
2055// Test Packaging and installing an extension whose name contains punctuation.
2056TEST_F(ExtensionServiceTest, PackPunctuatedExtension) {
2057  InitializeEmptyExtensionService();
2058  base::FilePath input_directory = data_dir()
2059                                       .AppendASCII("good")
2060                                       .AppendASCII("Extensions")
2061                                       .AppendASCII(good0)
2062                                       .AppendASCII("1.0.0.0");
2063
2064  base::ScopedTempDir temp_dir;
2065  ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
2066
2067  // Extension names containing punctuation, and the expected names for the
2068  // packed extensions.
2069  const base::FilePath punctuated_names[] = {
2070    base::FilePath(FILE_PATH_LITERAL("this.extensions.name.has.periods")),
2071    base::FilePath(FILE_PATH_LITERAL(".thisextensionsnamestartswithaperiod")),
2072    base::FilePath(FILE_PATH_LITERAL("thisextensionhasaslashinitsname/")).
2073        NormalizePathSeparators(),
2074  };
2075  const base::FilePath expected_crx_names[] = {
2076    base::FilePath(FILE_PATH_LITERAL("this.extensions.name.has.periods.crx")),
2077    base::FilePath(
2078        FILE_PATH_LITERAL(".thisextensionsnamestartswithaperiod.crx")),
2079    base::FilePath(FILE_PATH_LITERAL("thisextensionhasaslashinitsname.crx")),
2080  };
2081  const base::FilePath expected_private_key_names[] = {
2082    base::FilePath(FILE_PATH_LITERAL("this.extensions.name.has.periods.pem")),
2083    base::FilePath(
2084        FILE_PATH_LITERAL(".thisextensionsnamestartswithaperiod.pem")),
2085    base::FilePath(FILE_PATH_LITERAL("thisextensionhasaslashinitsname.pem")),
2086  };
2087
2088  for (size_t i = 0; i < arraysize(punctuated_names); ++i) {
2089    SCOPED_TRACE(punctuated_names[i].value().c_str());
2090    base::FilePath output_dir = temp_dir.path().Append(punctuated_names[i]);
2091
2092    // Copy the extension into the output directory, as PackExtensionJob doesn't
2093    // let us choose where to output the packed extension.
2094    ASSERT_TRUE(base::CopyDirectory(input_directory, output_dir, true));
2095
2096    base::FilePath expected_crx_path =
2097        temp_dir.path().Append(expected_crx_names[i]);
2098    base::FilePath expected_private_key_path =
2099        temp_dir.path().Append(expected_private_key_names[i]);
2100    PackExtensionTestClient pack_client(expected_crx_path,
2101                                        expected_private_key_path);
2102    scoped_refptr<extensions::PackExtensionJob> packer(
2103        new extensions::PackExtensionJob(&pack_client, output_dir,
2104                                         base::FilePath(),
2105                                         ExtensionCreator::kOverwriteCRX));
2106    packer->Start();
2107
2108    // The packer will post a notification task to the current thread's message
2109    // loop when it is finished.  We manually run the loop here so that we
2110    // block and catch the notification; otherwise, the process would exit.
2111    // This call to |Run()| is matched by a call to |Quit()| in the
2112    // |PackExtensionTestClient|'s notification handling code.
2113    base::MessageLoop::current()->Run();
2114
2115    if (HasFatalFailure())
2116      return;
2117
2118    InstallCRX(expected_crx_path, INSTALL_NEW);
2119  }
2120}
2121
2122TEST_F(ExtensionServiceTest, PackExtensionContainingKeyFails) {
2123  InitializeEmptyExtensionService();
2124
2125  base::ScopedTempDir extension_temp_dir;
2126  ASSERT_TRUE(extension_temp_dir.CreateUniqueTempDir());
2127  base::FilePath input_directory = extension_temp_dir.path().AppendASCII("ext");
2128  ASSERT_TRUE(
2129      base::CopyDirectory(data_dir()
2130                              .AppendASCII("good")
2131                              .AppendASCII("Extensions")
2132                              .AppendASCII("behllobkkfkfnphdnhnkndlbkcpglgmj")
2133                              .AppendASCII("1.0.0.0"),
2134                          input_directory,
2135                          /*recursive=*/true));
2136
2137  base::ScopedTempDir output_temp_dir;
2138  ASSERT_TRUE(output_temp_dir.CreateUniqueTempDir());
2139  base::FilePath output_directory = output_temp_dir.path();
2140
2141  base::FilePath crx_path(output_directory.AppendASCII("ex1.crx"));
2142  base::FilePath privkey_path(output_directory.AppendASCII("privkey.pem"));
2143
2144  // Pack the extension once to get a private key.
2145  scoped_ptr<ExtensionCreator> creator(new ExtensionCreator());
2146  ASSERT_TRUE(creator->Run(input_directory, crx_path, base::FilePath(),
2147      privkey_path, ExtensionCreator::kNoRunFlags))
2148      << creator->error_message();
2149  ASSERT_TRUE(base::PathExists(crx_path));
2150  ASSERT_TRUE(base::PathExists(privkey_path));
2151
2152  base::DeleteFile(crx_path, false);
2153  // Move the pem file into the extension.
2154  base::Move(privkey_path,
2155                  input_directory.AppendASCII("privkey.pem"));
2156
2157  // This pack should fail because of the contained private key.
2158  EXPECT_FALSE(creator->Run(input_directory, crx_path, base::FilePath(),
2159      privkey_path, ExtensionCreator::kNoRunFlags));
2160  EXPECT_THAT(creator->error_message(),
2161              testing::ContainsRegex(
2162                  "extension includes the key file.*privkey.pem"));
2163}
2164
2165// Test Packaging and installing an extension using an openssl generated key.
2166// The openssl is generated with the following:
2167// > openssl genrsa -out privkey.pem 1024
2168// > openssl pkcs8 -topk8 -nocrypt -in privkey.pem -out privkey_asn1.pem
2169// The privkey.pem is a PrivateKey, and the pcks8 -topk8 creates a
2170// PrivateKeyInfo ASN.1 structure, we our RSAPrivateKey expects.
2171TEST_F(ExtensionServiceTest, PackExtensionOpenSSLKey) {
2172  InitializeEmptyExtensionService();
2173  base::FilePath input_directory =
2174      data_dir()
2175          .AppendASCII("good")
2176          .AppendASCII("Extensions")
2177          .AppendASCII("behllobkkfkfnphdnhnkndlbkcpglgmj")
2178          .AppendASCII("1.0.0.0");
2179  base::FilePath privkey_path(
2180      data_dir().AppendASCII("openssl_privkey_asn1.pem"));
2181  ASSERT_TRUE(base::PathExists(privkey_path));
2182
2183  base::ScopedTempDir temp_dir;
2184  ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
2185  base::FilePath output_directory = temp_dir.path();
2186
2187  base::FilePath crx_path(output_directory.AppendASCII("ex1.crx"));
2188
2189  scoped_ptr<ExtensionCreator> creator(new ExtensionCreator());
2190  ASSERT_TRUE(creator->Run(input_directory, crx_path, privkey_path,
2191      base::FilePath(), ExtensionCreator::kOverwriteCRX));
2192
2193  InstallCRX(crx_path, INSTALL_NEW);
2194}
2195
2196#if defined(THREAD_SANITIZER)
2197// Flaky under Tsan. http://crbug.com/377702
2198#define MAYBE_InstallTheme DISABLED_InstallTheme
2199#else
2200#define MAYBE_InstallTheme InstallTheme
2201#endif
2202
2203TEST_F(ExtensionServiceTest, MAYBE_InstallTheme) {
2204  InitializeEmptyExtensionService();
2205  service()->Init();
2206
2207  // A theme.
2208  base::FilePath path = data_dir().AppendASCII("theme.crx");
2209  InstallCRX(path, INSTALL_NEW);
2210  int pref_count = 0;
2211  ValidatePrefKeyCount(++pref_count);
2212  ValidateIntegerPref(theme_crx, "state", Extension::ENABLED);
2213  ValidateIntegerPref(theme_crx, "location", Manifest::INTERNAL);
2214
2215  // A theme when extensions are disabled. Themes can be installed, even when
2216  // extensions are disabled.
2217  service()->set_extensions_enabled(false);
2218  path = data_dir().AppendASCII("theme2.crx");
2219  InstallCRX(path, INSTALL_NEW);
2220  ValidatePrefKeyCount(++pref_count);
2221  ValidateIntegerPref(theme2_crx, "state", Extension::ENABLED);
2222  ValidateIntegerPref(theme2_crx, "location", Manifest::INTERNAL);
2223
2224  // A theme with extension elements. Themes cannot have extension elements,
2225  // so any such elements (like content scripts) should be ignored.
2226  service()->set_extensions_enabled(true);
2227  {
2228    path = data_dir().AppendASCII("theme_with_extension.crx");
2229    const Extension* extension = InstallCRX(path, INSTALL_NEW);
2230    ValidatePrefKeyCount(++pref_count);
2231    ASSERT_TRUE(extension);
2232    EXPECT_TRUE(extension->is_theme());
2233    EXPECT_EQ(
2234        0u,
2235        extensions::ContentScriptsInfo::GetContentScripts(extension).size());
2236  }
2237
2238  // A theme with image resources missing (misspelt path).
2239  path = data_dir().AppendASCII("theme_missing_image.crx");
2240  InstallCRX(path, INSTALL_FAILED);
2241  ValidatePrefKeyCount(pref_count);
2242}
2243
2244TEST_F(ExtensionServiceTest, LoadLocalizedTheme) {
2245  // Load.
2246  InitializeEmptyExtensionService();
2247  service()->Init();
2248
2249  base::FilePath extension_path = data_dir().AppendASCII("theme_i18n");
2250
2251  extensions::UnpackedInstaller::Create(service())->Load(extension_path);
2252  base::RunLoop().RunUntilIdle();
2253  EXPECT_EQ(0u, GetErrors().size());
2254  ASSERT_EQ(1u, loaded_.size());
2255  EXPECT_EQ(1u, registry()->enabled_extensions().size());
2256  const Extension* theme = registry()->enabled_extensions().begin()->get();
2257  EXPECT_EQ("name", theme->name());
2258  EXPECT_EQ("description", theme->description());
2259
2260  // Cleanup the "Cached Theme.pak" file. Ideally, this would be installed in a
2261  // temporary directory, but it automatically installs to the extension's
2262  // directory, and we don't want to copy the whole extension for a unittest.
2263  base::FilePath theme_file = extension_path.Append(chrome::kThemePackFilename);
2264  ASSERT_TRUE(base::PathExists(theme_file));
2265  ASSERT_TRUE(base::DeleteFile(theme_file, false));  // Not recursive.
2266}
2267
2268// Tests that we can change the ID of an unpacked extension by adding a key
2269// to its manifest.
2270TEST_F(ExtensionServiceTest, UnpackedExtensionCanChangeID) {
2271  InitializeEmptyExtensionService();
2272
2273  base::ScopedTempDir temp;
2274  ASSERT_TRUE(temp.CreateUniqueTempDir());
2275
2276  base::FilePath extension_path = temp.path();
2277  base::FilePath manifest_path =
2278      extension_path.Append(extensions::kManifestFilename);
2279  base::FilePath manifest_no_key =
2280      data_dir().AppendASCII("unpacked").AppendASCII("manifest_no_key.json");
2281
2282  base::FilePath manifest_with_key =
2283      data_dir().AppendASCII("unpacked").AppendASCII("manifest_with_key.json");
2284
2285  ASSERT_TRUE(base::PathExists(manifest_no_key));
2286  ASSERT_TRUE(base::PathExists(manifest_with_key));
2287
2288  // Load the unpacked extension with no key.
2289  base::CopyFile(manifest_no_key, manifest_path);
2290  extensions::UnpackedInstaller::Create(service())->Load(extension_path);
2291
2292  base::RunLoop().RunUntilIdle();
2293  EXPECT_EQ(0u, GetErrors().size());
2294  ASSERT_EQ(1u, loaded_.size());
2295  EXPECT_EQ(1u, registry()->enabled_extensions().size());
2296
2297  // Add the key to the manifest.
2298  base::CopyFile(manifest_with_key, manifest_path);
2299  loaded_.clear();
2300
2301  // Reload the extensions.
2302  service()->ReloadExtensionsForTest();
2303  const Extension* extension = service()->GetExtensionById(unpacked, false);
2304  EXPECT_EQ(unpacked, extension->id());
2305  ASSERT_EQ(1u, loaded_.size());
2306
2307  // TODO(jstritar): Right now this just makes sure we don't crash and burn, but
2308  // we should also test that preferences are preserved.
2309}
2310
2311#if defined(OS_POSIX)
2312TEST_F(ExtensionServiceTest, UnpackedExtensionMayContainSymlinkedFiles) {
2313  base::FilePath source_data_dir =
2314      data_dir().AppendASCII("unpacked").AppendASCII("symlinks_allowed");
2315
2316  // Paths to test data files.
2317  base::FilePath source_manifest = source_data_dir.AppendASCII("manifest.json");
2318  ASSERT_TRUE(base::PathExists(source_manifest));
2319  base::FilePath source_icon = source_data_dir.AppendASCII("icon.png");
2320  ASSERT_TRUE(base::PathExists(source_icon));
2321
2322  // Set up the temporary extension directory.
2323  base::ScopedTempDir temp;
2324  ASSERT_TRUE(temp.CreateUniqueTempDir());
2325  base::FilePath extension_path = temp.path();
2326  base::FilePath manifest = extension_path.Append(
2327      extensions::kManifestFilename);
2328  base::FilePath icon_symlink = extension_path.AppendASCII("icon.png");
2329  base::CopyFile(source_manifest, manifest);
2330  base::CreateSymbolicLink(source_icon, icon_symlink);
2331
2332  // Load extension.
2333  InitializeEmptyExtensionService();
2334  extensions::UnpackedInstaller::Create(service())->Load(extension_path);
2335  base::RunLoop().RunUntilIdle();
2336
2337  EXPECT_TRUE(GetErrors().empty());
2338  ASSERT_EQ(1u, loaded_.size());
2339  EXPECT_EQ(1u, registry()->enabled_extensions().size());
2340}
2341#endif
2342
2343TEST_F(ExtensionServiceTest, UnpackedExtensionMayNotHaveUnderscore) {
2344  InitializeEmptyExtensionService();
2345  base::FilePath extension_path = data_dir().AppendASCII("underscore_name");
2346  extensions::UnpackedInstaller::Create(service())->Load(extension_path);
2347  base::RunLoop().RunUntilIdle();
2348  EXPECT_EQ(1u, GetErrors().size());
2349  EXPECT_EQ(0u, registry()->enabled_extensions().size());
2350}
2351
2352TEST_F(ExtensionServiceTest, InstallLocalizedTheme) {
2353  InitializeEmptyExtensionService();
2354  service()->Init();
2355
2356  base::FilePath theme_path = data_dir().AppendASCII("theme_i18n");
2357
2358  const Extension* theme = PackAndInstallCRX(theme_path, INSTALL_NEW);
2359
2360  EXPECT_EQ(0u, GetErrors().size());
2361  EXPECT_EQ(1u, registry()->enabled_extensions().size());
2362  EXPECT_EQ("name", theme->name());
2363  EXPECT_EQ("description", theme->description());
2364}
2365
2366TEST_F(ExtensionServiceTest, InstallApps) {
2367  InitializeEmptyExtensionService();
2368
2369  // An empty app.
2370  const Extension* app =
2371      PackAndInstallCRX(data_dir().AppendASCII("app1"), INSTALL_NEW);
2372  int pref_count = 0;
2373  ValidatePrefKeyCount(++pref_count);
2374  ASSERT_EQ(1u, registry()->enabled_extensions().size());
2375  ValidateIntegerPref(app->id(), "state", Extension::ENABLED);
2376  ValidateIntegerPref(app->id(), "location", Manifest::INTERNAL);
2377
2378  // Another app with non-overlapping extent. Should succeed.
2379  PackAndInstallCRX(data_dir().AppendASCII("app2"), INSTALL_NEW);
2380  ValidatePrefKeyCount(++pref_count);
2381
2382  // A third app whose extent overlaps the first. Should fail.
2383  PackAndInstallCRX(data_dir().AppendASCII("app3"), INSTALL_FAILED);
2384  ValidatePrefKeyCount(pref_count);
2385}
2386
2387// Tests that file access is OFF by default.
2388TEST_F(ExtensionServiceTest, DefaultFileAccess) {
2389  InitializeEmptyExtensionService();
2390  const Extension* extension = PackAndInstallCRX(
2391      data_dir().AppendASCII("permissions").AppendASCII("files"), INSTALL_NEW);
2392  EXPECT_EQ(0u, GetErrors().size());
2393  EXPECT_EQ(1u, registry()->enabled_extensions().size());
2394  EXPECT_FALSE(
2395      ExtensionPrefs::Get(profile())->AllowFileAccess(extension->id()));
2396}
2397
2398TEST_F(ExtensionServiceTest, UpdateApps) {
2399  InitializeEmptyExtensionService();
2400  base::FilePath extensions_path = data_dir().AppendASCII("app_update");
2401
2402  // First install v1 of a hosted app.
2403  const Extension* extension =
2404      InstallCRX(extensions_path.AppendASCII("v1.crx"), INSTALL_NEW);
2405  ASSERT_EQ(1u, registry()->enabled_extensions().size());
2406  std::string id = extension->id();
2407  ASSERT_EQ(std::string("1"), extension->version()->GetString());
2408
2409  // Now try updating to v2.
2410  UpdateExtension(id,
2411                  extensions_path.AppendASCII("v2.crx"),
2412                  ENABLED);
2413  ASSERT_EQ(std::string("2"),
2414            service()->GetExtensionById(id, false)->version()->GetString());
2415}
2416
2417// Verifies that the NTP page and launch ordinals are kept when updating apps.
2418TEST_F(ExtensionServiceTest, UpdateAppsRetainOrdinals) {
2419  InitializeEmptyExtensionService();
2420  AppSorting* sorting = ExtensionPrefs::Get(profile())->app_sorting();
2421  base::FilePath extensions_path = data_dir().AppendASCII("app_update");
2422
2423  // First install v1 of a hosted app.
2424  const Extension* extension =
2425      InstallCRX(extensions_path.AppendASCII("v1.crx"), INSTALL_NEW);
2426  ASSERT_EQ(1u, registry()->enabled_extensions().size());
2427  std::string id = extension->id();
2428  ASSERT_EQ(std::string("1"), extension->version()->GetString());
2429
2430  // Modify the ordinals so we can distinguish them from the defaults.
2431  syncer::StringOrdinal new_page_ordinal =
2432      sorting->GetPageOrdinal(id).CreateAfter();
2433  syncer::StringOrdinal new_launch_ordinal =
2434      sorting->GetAppLaunchOrdinal(id).CreateBefore();
2435
2436  sorting->SetPageOrdinal(id, new_page_ordinal);
2437  sorting->SetAppLaunchOrdinal(id, new_launch_ordinal);
2438
2439  // Now try updating to v2.
2440  UpdateExtension(id, extensions_path.AppendASCII("v2.crx"), ENABLED);
2441  ASSERT_EQ(std::string("2"),
2442            service()->GetExtensionById(id, false)->version()->GetString());
2443
2444  // Verify that the ordinals match.
2445  ASSERT_TRUE(new_page_ordinal.Equals(sorting->GetPageOrdinal(id)));
2446  ASSERT_TRUE(new_launch_ordinal.Equals(sorting->GetAppLaunchOrdinal(id)));
2447}
2448
2449// Ensures that the CWS has properly initialized ordinals.
2450TEST_F(ExtensionServiceTest, EnsureCWSOrdinalsInitialized) {
2451  InitializeEmptyExtensionService();
2452  service()->component_loader()->Add(
2453      IDR_WEBSTORE_MANIFEST, base::FilePath(FILE_PATH_LITERAL("web_store")));
2454  service()->Init();
2455
2456  AppSorting* sorting = ExtensionPrefs::Get(profile())->app_sorting();
2457  EXPECT_TRUE(
2458      sorting->GetPageOrdinal(extensions::kWebStoreAppId).IsValid());
2459  EXPECT_TRUE(
2460      sorting->GetAppLaunchOrdinal(extensions::kWebStoreAppId).IsValid());
2461}
2462
2463TEST_F(ExtensionServiceTest, InstallAppsWithUnlimitedStorage) {
2464  InitializeEmptyExtensionService();
2465  EXPECT_TRUE(registry()->enabled_extensions().is_empty());
2466
2467  int pref_count = 0;
2468
2469  // Install app1 with unlimited storage.
2470  const Extension* extension =
2471      PackAndInstallCRX(data_dir().AppendASCII("app1"), INSTALL_NEW);
2472  ValidatePrefKeyCount(++pref_count);
2473  ASSERT_EQ(1u, registry()->enabled_extensions().size());
2474  const std::string id1 = extension->id();
2475  EXPECT_TRUE(extension->permissions_data()->HasAPIPermission(
2476      APIPermission::kUnlimitedStorage));
2477  EXPECT_TRUE(extension->web_extent().MatchesURL(
2478      extensions::AppLaunchInfo::GetFullLaunchURL(extension)));
2479  const GURL origin1(
2480      extensions::AppLaunchInfo::GetFullLaunchURL(extension).GetOrigin());
2481  EXPECT_TRUE(profile()->GetExtensionSpecialStoragePolicy()->IsStorageUnlimited(
2482      origin1));
2483
2484  // Install app2 from the same origin with unlimited storage.
2485  extension = PackAndInstallCRX(data_dir().AppendASCII("app2"), INSTALL_NEW);
2486  ValidatePrefKeyCount(++pref_count);
2487  ASSERT_EQ(2u, registry()->enabled_extensions().size());
2488  const std::string id2 = extension->id();
2489  EXPECT_TRUE(extension->permissions_data()->HasAPIPermission(
2490      APIPermission::kUnlimitedStorage));
2491  EXPECT_TRUE(extension->web_extent().MatchesURL(
2492      extensions::AppLaunchInfo::GetFullLaunchURL(extension)));
2493  const GURL origin2(
2494      extensions::AppLaunchInfo::GetFullLaunchURL(extension).GetOrigin());
2495  EXPECT_EQ(origin1, origin2);
2496  EXPECT_TRUE(profile()->GetExtensionSpecialStoragePolicy()->IsStorageUnlimited(
2497      origin2));
2498
2499  // Uninstall one of them, unlimited storage should still be granted
2500  // to the origin.
2501  UninstallExtension(id1, false);
2502  EXPECT_EQ(1u, registry()->enabled_extensions().size());
2503  EXPECT_TRUE(profile()->GetExtensionSpecialStoragePolicy()->IsStorageUnlimited(
2504      origin1));
2505
2506  // Uninstall the other, unlimited storage should be revoked.
2507  UninstallExtension(id2, false);
2508  EXPECT_EQ(0u, registry()->enabled_extensions().size());
2509  EXPECT_FALSE(
2510      profile()->GetExtensionSpecialStoragePolicy()->IsStorageUnlimited(
2511          origin2));
2512}
2513
2514TEST_F(ExtensionServiceTest, InstallAppsAndCheckStorageProtection) {
2515  InitializeEmptyExtensionService();
2516  EXPECT_TRUE(registry()->enabled_extensions().is_empty());
2517
2518  int pref_count = 0;
2519
2520  const Extension* extension =
2521      PackAndInstallCRX(data_dir().AppendASCII("app1"), INSTALL_NEW);
2522  ValidatePrefKeyCount(++pref_count);
2523  ASSERT_EQ(1u, registry()->enabled_extensions().size());
2524  EXPECT_TRUE(extension->is_app());
2525  const std::string id1 = extension->id();
2526  const GURL origin1(
2527      extensions::AppLaunchInfo::GetFullLaunchURL(extension).GetOrigin());
2528  EXPECT_TRUE(profile()->GetExtensionSpecialStoragePolicy()->IsStorageProtected(
2529      origin1));
2530
2531  // App 4 has a different origin (maps.google.com).
2532  extension = PackAndInstallCRX(data_dir().AppendASCII("app4"), INSTALL_NEW);
2533  ValidatePrefKeyCount(++pref_count);
2534  ASSERT_EQ(2u, registry()->enabled_extensions().size());
2535  const std::string id2 = extension->id();
2536  const GURL origin2(
2537      extensions::AppLaunchInfo::GetFullLaunchURL(extension).GetOrigin());
2538  ASSERT_NE(origin1, origin2);
2539  EXPECT_TRUE(profile()->GetExtensionSpecialStoragePolicy()->IsStorageProtected(
2540      origin2));
2541
2542  UninstallExtension(id1, false);
2543  EXPECT_EQ(1u, registry()->enabled_extensions().size());
2544
2545  UninstallExtension(id2, false);
2546
2547  EXPECT_TRUE(registry()->enabled_extensions().is_empty());
2548  EXPECT_FALSE(
2549      profile()->GetExtensionSpecialStoragePolicy()->IsStorageProtected(
2550          origin1));
2551  EXPECT_FALSE(
2552      profile()->GetExtensionSpecialStoragePolicy()->IsStorageProtected(
2553          origin2));
2554}
2555
2556// Test that when an extension version is reinstalled, nothing happens.
2557TEST_F(ExtensionServiceTest, Reinstall) {
2558  InitializeEmptyExtensionService();
2559
2560  // A simple extension that should install without error.
2561  base::FilePath path = data_dir().AppendASCII("good.crx");
2562  InstallCRX(path, INSTALL_NEW);
2563
2564  ValidatePrefKeyCount(1);
2565  ValidateIntegerPref(good_crx, "state", Extension::ENABLED);
2566  ValidateIntegerPref(good_crx, "location", Manifest::INTERNAL);
2567
2568  // Reinstall the same version, it should overwrite the previous one.
2569  InstallCRX(path, INSTALL_UPDATED);
2570
2571  ValidatePrefKeyCount(1);
2572  ValidateIntegerPref(good_crx, "state", Extension::ENABLED);
2573  ValidateIntegerPref(good_crx, "location", Manifest::INTERNAL);
2574}
2575
2576// Test that we can determine if extensions came from the
2577// Chrome web store.
2578TEST_F(ExtensionServiceTest, FromWebStore) {
2579  InitializeEmptyExtensionService();
2580
2581  // A simple extension that should install without error.
2582  base::FilePath path = data_dir().AppendASCII("good.crx");
2583  // Not from web store.
2584  const Extension* extension = InstallCRX(path, INSTALL_NEW);
2585  std::string id = extension->id();
2586
2587  ValidatePrefKeyCount(1);
2588  ASSERT_TRUE(ValidateBooleanPref(good_crx, "from_webstore", false));
2589  ASSERT_FALSE(extension->from_webstore());
2590
2591  // Test install from web store.
2592  InstallCRXFromWebStore(path, INSTALL_UPDATED);  // From web store.
2593
2594  ValidatePrefKeyCount(1);
2595  ASSERT_TRUE(ValidateBooleanPref(good_crx, "from_webstore", true));
2596
2597  // Reload so extension gets reinitialized with new value.
2598  service()->ReloadExtensionsForTest();
2599  extension = service()->GetExtensionById(id, false);
2600  ASSERT_TRUE(extension->from_webstore());
2601
2602  // Upgrade to version 2.0
2603  path = data_dir().AppendASCII("good2.crx");
2604  UpdateExtension(good_crx, path, ENABLED);
2605  ValidatePrefKeyCount(1);
2606  ASSERT_TRUE(ValidateBooleanPref(good_crx, "from_webstore", true));
2607}
2608
2609// Test upgrading a signed extension.
2610TEST_F(ExtensionServiceTest, UpgradeSignedGood) {
2611  InitializeEmptyExtensionService();
2612
2613  base::FilePath path = data_dir().AppendASCII("good.crx");
2614  const Extension* extension = InstallCRX(path, INSTALL_NEW);
2615  std::string id = extension->id();
2616
2617  ASSERT_EQ("1.0.0.0", extension->version()->GetString());
2618  ASSERT_EQ(0u, GetErrors().size());
2619
2620  // Upgrade to version 1.0.0.1.
2621  // Also test that the extension's old and new title are correctly retrieved.
2622  path = data_dir().AppendASCII("good2.crx");
2623  InstallCRX(path, INSTALL_UPDATED, Extension::NO_FLAGS, "My extension 1");
2624  extension = service()->GetExtensionById(id, false);
2625
2626  ASSERT_EQ("1.0.0.1", extension->version()->GetString());
2627  ASSERT_EQ("My updated extension 1", extension->name());
2628  ASSERT_EQ(0u, GetErrors().size());
2629}
2630
2631// Test upgrading a signed extension with a bad signature.
2632TEST_F(ExtensionServiceTest, UpgradeSignedBad) {
2633  InitializeEmptyExtensionService();
2634
2635  base::FilePath path = data_dir().AppendASCII("good.crx");
2636  InstallCRX(path, INSTALL_NEW);
2637
2638  // Try upgrading with a bad signature. This should fail during the unpack,
2639  // because the key will not match the signature.
2640  path = data_dir().AppendASCII("bad_signature.crx");
2641  InstallCRX(path, INSTALL_FAILED);
2642}
2643
2644// Test a normal update via the UpdateExtension API
2645TEST_F(ExtensionServiceTest, UpdateExtension) {
2646  InitializeEmptyExtensionService();
2647
2648  base::FilePath path = data_dir().AppendASCII("good.crx");
2649
2650  const Extension* good = InstallCRX(path, INSTALL_NEW);
2651  ASSERT_EQ("1.0.0.0", good->VersionString());
2652  ASSERT_EQ(good_crx, good->id());
2653
2654  path = data_dir().AppendASCII("good2.crx");
2655  UpdateExtension(good_crx, path, ENABLED);
2656  ASSERT_EQ(
2657      "1.0.0.1",
2658      service()->GetExtensionById(good_crx, false)->version()->GetString());
2659}
2660
2661// Extensions should not be updated during browser shutdown.
2662TEST_F(ExtensionServiceTest, UpdateExtensionDuringShutdown) {
2663  InitializeEmptyExtensionService();
2664
2665  // Install an extension.
2666  base::FilePath path = data_dir().AppendASCII("good.crx");
2667  const Extension* good = InstallCRX(path, INSTALL_NEW);
2668  ASSERT_EQ(good_crx, good->id());
2669
2670  // Simulate shutdown.
2671  service()->set_browser_terminating_for_test(true);
2672
2673  // Update should fail and extension should not be updated.
2674  path = data_dir().AppendASCII("good2.crx");
2675  bool updated = service()->UpdateExtension(good_crx, path, true, NULL);
2676  ASSERT_FALSE(updated);
2677  ASSERT_EQ(
2678      "1.0.0.0",
2679      service()->GetExtensionById(good_crx, false)->version()->GetString());
2680}
2681
2682// Test updating a not-already-installed extension - this should fail
2683TEST_F(ExtensionServiceTest, UpdateNotInstalledExtension) {
2684  InitializeEmptyExtensionService();
2685
2686  base::FilePath path = data_dir().AppendASCII("good.crx");
2687  UpdateExtension(good_crx, path, UPDATED);
2688  base::RunLoop().RunUntilIdle();
2689
2690  ASSERT_EQ(0u, registry()->enabled_extensions().size());
2691  ASSERT_FALSE(installed_);
2692  ASSERT_EQ(0u, loaded_.size());
2693}
2694
2695// Makes sure you can't downgrade an extension via UpdateExtension
2696TEST_F(ExtensionServiceTest, UpdateWillNotDowngrade) {
2697  InitializeEmptyExtensionService();
2698
2699  base::FilePath path = data_dir().AppendASCII("good2.crx");
2700
2701  const Extension* good = InstallCRX(path, INSTALL_NEW);
2702  ASSERT_EQ("1.0.0.1", good->VersionString());
2703  ASSERT_EQ(good_crx, good->id());
2704
2705  // Change path from good2.crx -> good.crx
2706  path = data_dir().AppendASCII("good.crx");
2707  UpdateExtension(good_crx, path, FAILED);
2708  ASSERT_EQ(
2709      "1.0.0.1",
2710      service()->GetExtensionById(good_crx, false)->version()->GetString());
2711}
2712
2713// Make sure calling update with an identical version does nothing
2714TEST_F(ExtensionServiceTest, UpdateToSameVersionIsNoop) {
2715  InitializeEmptyExtensionService();
2716
2717  base::FilePath path = data_dir().AppendASCII("good.crx");
2718
2719  const Extension* good = InstallCRX(path, INSTALL_NEW);
2720  ASSERT_EQ(good_crx, good->id());
2721  UpdateExtension(good_crx, path, FAILED_SILENTLY);
2722}
2723
2724// Tests that updating an extension does not clobber old state.
2725TEST_F(ExtensionServiceTest, UpdateExtensionPreservesState) {
2726  InitializeEmptyExtensionService();
2727
2728  base::FilePath path = data_dir().AppendASCII("good.crx");
2729
2730  const Extension* good = InstallCRX(path, INSTALL_NEW);
2731  ASSERT_EQ("1.0.0.0", good->VersionString());
2732  ASSERT_EQ(good_crx, good->id());
2733
2734  // Disable it and allow it to run in incognito. These settings should carry
2735  // over to the updated version.
2736  service()->DisableExtension(good->id(), Extension::DISABLE_USER_ACTION);
2737  extensions::util::SetIsIncognitoEnabled(good->id(), profile(), true);
2738  ExtensionPrefs::Get(profile())
2739      ->SetDidExtensionEscalatePermissions(good, true);
2740
2741  path = data_dir().AppendASCII("good2.crx");
2742  UpdateExtension(good_crx, path, INSTALLED);
2743  ASSERT_EQ(1u, registry()->disabled_extensions().size());
2744  const Extension* good2 = service()->GetExtensionById(good_crx, true);
2745  ASSERT_EQ("1.0.0.1", good2->version()->GetString());
2746  EXPECT_TRUE(extensions::util::IsIncognitoEnabled(good2->id(), profile()));
2747  EXPECT_TRUE(ExtensionPrefs::Get(profile())
2748                  ->DidExtensionEscalatePermissions(good2->id()));
2749}
2750
2751// Tests that updating preserves extension location.
2752TEST_F(ExtensionServiceTest, UpdateExtensionPreservesLocation) {
2753  InitializeEmptyExtensionService();
2754
2755  base::FilePath path = data_dir().AppendASCII("good.crx");
2756
2757  const Extension* good =
2758      InstallCRXWithLocation(path, Manifest::EXTERNAL_PREF, INSTALL_NEW);
2759
2760  ASSERT_EQ("1.0.0.0", good->VersionString());
2761  ASSERT_EQ(good_crx, good->id());
2762
2763  path = data_dir().AppendASCII("good2.crx");
2764  UpdateExtension(good_crx, path, ENABLED);
2765  const Extension* good2 = service()->GetExtensionById(good_crx, false);
2766  ASSERT_EQ("1.0.0.1", good2->version()->GetString());
2767  EXPECT_EQ(good2->location(), Manifest::EXTERNAL_PREF);
2768}
2769
2770// Makes sure that LOAD extension types can downgrade.
2771TEST_F(ExtensionServiceTest, LoadExtensionsCanDowngrade) {
2772  InitializeEmptyExtensionService();
2773
2774  base::ScopedTempDir temp;
2775  ASSERT_TRUE(temp.CreateUniqueTempDir());
2776
2777  // We'll write the extension manifest dynamically to a temporary path
2778  // to make it easier to change the version number.
2779  base::FilePath extension_path = temp.path();
2780  base::FilePath manifest_path =
2781      extension_path.Append(extensions::kManifestFilename);
2782  ASSERT_FALSE(base::PathExists(manifest_path));
2783
2784  // Start with version 2.0.
2785  base::DictionaryValue manifest;
2786  manifest.SetString("version", "2.0");
2787  manifest.SetString("name", "LOAD Downgrade Test");
2788  manifest.SetInteger("manifest_version", 2);
2789
2790  JSONFileValueSerializer serializer(manifest_path);
2791  ASSERT_TRUE(serializer.Serialize(manifest));
2792
2793  extensions::UnpackedInstaller::Create(service())->Load(extension_path);
2794  base::RunLoop().RunUntilIdle();
2795
2796  EXPECT_EQ(0u, GetErrors().size());
2797  ASSERT_EQ(1u, loaded_.size());
2798  EXPECT_EQ(Manifest::UNPACKED, loaded_[0]->location());
2799  EXPECT_EQ(1u, registry()->enabled_extensions().size());
2800  EXPECT_EQ("2.0", loaded_[0]->VersionString());
2801
2802  // Now set the version number to 1.0, reload the extensions and verify that
2803  // the downgrade was accepted.
2804  manifest.SetString("version", "1.0");
2805  ASSERT_TRUE(serializer.Serialize(manifest));
2806
2807  extensions::UnpackedInstaller::Create(service())->Load(extension_path);
2808  base::RunLoop().RunUntilIdle();
2809
2810  EXPECT_EQ(0u, GetErrors().size());
2811  ASSERT_EQ(1u, loaded_.size());
2812  EXPECT_EQ(Manifest::UNPACKED, loaded_[0]->location());
2813  EXPECT_EQ(1u, registry()->enabled_extensions().size());
2814  EXPECT_EQ("1.0", loaded_[0]->VersionString());
2815}
2816
2817#if !defined(OS_POSIX) || defined(OS_MACOSX)
2818// LOAD extensions with plugins require approval.
2819// Only run this on platforms that support NPAPI plugins.
2820TEST_F(ExtensionServiceTest, LoadExtensionsWithPlugins) {
2821  base::FilePath extension_with_plugin_path = good1_path();
2822  base::FilePath extension_no_plugin_path = good2_path();
2823
2824  InitPluginService();
2825  InitializeEmptyExtensionService();
2826  InitializeProcessManager();
2827  service()->set_show_extensions_prompts(true);
2828
2829  // Start by canceling any install prompts.
2830  ExtensionInstallPrompt::g_auto_confirm_for_tests =
2831      ExtensionInstallPrompt::CANCEL;
2832
2833  // The extension that has a plugin should not install.
2834  extensions::UnpackedInstaller::Create(service())
2835      ->Load(extension_with_plugin_path);
2836  base::RunLoop().RunUntilIdle();
2837  EXPECT_EQ(0u, GetErrors().size());
2838  EXPECT_EQ(0u, loaded_.size());
2839  EXPECT_EQ(0u, registry()->enabled_extensions().size());
2840  EXPECT_EQ(0u, registry()->disabled_extensions().size());
2841
2842  // But the extension with no plugin should since there's no prompt.
2843  ExtensionErrorReporter::GetInstance()->ClearErrors();
2844  extensions::UnpackedInstaller::Create(service())
2845      ->Load(extension_no_plugin_path);
2846  base::RunLoop().RunUntilIdle();
2847  EXPECT_EQ(0u, GetErrors().size());
2848  EXPECT_EQ(1u, loaded_.size());
2849  EXPECT_EQ(1u, registry()->enabled_extensions().size());
2850  EXPECT_EQ(0u, registry()->disabled_extensions().size());
2851  EXPECT_TRUE(registry()->enabled_extensions().Contains(good2));
2852
2853  // The plugin extension should install if we accept the dialog.
2854  ExtensionInstallPrompt::g_auto_confirm_for_tests =
2855      ExtensionInstallPrompt::ACCEPT;
2856
2857  ExtensionErrorReporter::GetInstance()->ClearErrors();
2858  extensions::UnpackedInstaller::Create(service())
2859      ->Load(extension_with_plugin_path);
2860  base::RunLoop().RunUntilIdle();
2861  EXPECT_EQ(0u, GetErrors().size());
2862  EXPECT_EQ(2u, loaded_.size());
2863  EXPECT_EQ(2u, registry()->enabled_extensions().size());
2864  EXPECT_EQ(0u, registry()->disabled_extensions().size());
2865  EXPECT_TRUE(registry()->enabled_extensions().Contains(good1));
2866  EXPECT_TRUE(registry()->enabled_extensions().Contains(good2));
2867
2868  // Make sure the granted permissions have been setup.
2869  scoped_refptr<PermissionSet> permissions(
2870      ExtensionPrefs::Get(profile())->GetGrantedPermissions(good1));
2871  EXPECT_FALSE(permissions->IsEmpty());
2872  EXPECT_TRUE(permissions->HasEffectiveFullAccess());
2873  EXPECT_FALSE(permissions->apis().empty());
2874  EXPECT_TRUE(permissions->HasAPIPermission(APIPermission::kPlugin));
2875
2876  // We should be able to reload the extension without getting another prompt.
2877  loaded_.clear();
2878  ExtensionInstallPrompt::g_auto_confirm_for_tests =
2879      ExtensionInstallPrompt::CANCEL;
2880
2881  service()->ReloadExtension(good1);
2882  base::RunLoop().RunUntilIdle();
2883  EXPECT_EQ(1u, loaded_.size());
2884  EXPECT_EQ(2u, registry()->enabled_extensions().size());
2885  EXPECT_EQ(0u, registry()->disabled_extensions().size());
2886}
2887#endif  // !defined(OS_POSIX) || defined(OS_MACOSX)
2888
2889namespace {
2890
2891bool IsExtension(const Extension* extension) {
2892  return extension->GetType() == Manifest::TYPE_EXTENSION;
2893}
2894
2895#if defined(ENABLE_BLACKLIST_TESTS)
2896std::set<std::string> StringSet(const std::string& s) {
2897  std::set<std::string> set;
2898  set.insert(s);
2899  return set;
2900}
2901std::set<std::string> StringSet(const std::string& s1, const std::string& s2) {
2902  std::set<std::string> set = StringSet(s1);
2903  set.insert(s2);
2904  return set;
2905}
2906#endif  // defined(ENABLE_BLACKLIST_TESTS)
2907
2908}  // namespace
2909
2910// Test adding a pending extension.
2911TEST_F(ExtensionServiceTest, AddPendingExtensionFromSync) {
2912  InitializeEmptyExtensionService();
2913
2914  const std::string kFakeId(all_zero);
2915  const GURL kFakeUpdateURL("http:://fake.update/url");
2916  const bool kFakeInstallSilently(true);
2917  const bool kFakeRemoteInstall(false);
2918  const bool kFakeInstalledByCustodian(false);
2919
2920  EXPECT_TRUE(
2921      service()->pending_extension_manager()->AddFromSync(
2922          kFakeId,
2923          kFakeUpdateURL,
2924          &IsExtension,
2925          kFakeInstallSilently,
2926          kFakeRemoteInstall,
2927          kFakeInstalledByCustodian));
2928
2929  const extensions::PendingExtensionInfo* pending_extension_info;
2930  ASSERT_TRUE((pending_extension_info =
2931                   service()->pending_extension_manager()->GetById(kFakeId)));
2932  EXPECT_EQ(kFakeUpdateURL, pending_extension_info->update_url());
2933  EXPECT_EQ(&IsExtension, pending_extension_info->should_allow_install_);
2934  EXPECT_EQ(kFakeInstallSilently, pending_extension_info->install_silently());
2935  // Use
2936  // EXPECT_TRUE(kFakeRemoteInstall == pending_extension_info->remote_install())
2937  // instead of
2938  // EXPECT_EQ(kFakeRemoteInstall, pending_extension_info->remote_install())
2939  // as gcc 4.7 issues the following warning on EXPECT_EQ(false, x), which is
2940  // turned into an error with -Werror=conversion-null:
2941  //   converting 'false' to pointer type for argument 1 of
2942  //   'char testing::internal::IsNullLiteralHelper(testing::internal::Secret*)'
2943  // https://code.google.com/p/googletest/issues/detail?id=458
2944  EXPECT_TRUE(kFakeRemoteInstall == pending_extension_info->remote_install());
2945}
2946
2947namespace {
2948const char kGoodId[] = "ldnnhddmnhbkjipkidpdiheffobcpfmf";
2949const char kGoodUpdateURL[] = "http://good.update/url";
2950const bool kGoodIsFromSync = true;
2951const bool kGoodInstallSilently = true;
2952const bool kGoodRemoteInstall = false;
2953const bool kGoodInstalledByCustodian = false;
2954}  // namespace
2955
2956// Test updating a pending extension.
2957TEST_F(ExtensionServiceTest, UpdatePendingExtension) {
2958  InitializeEmptyExtensionService();
2959  EXPECT_TRUE(
2960      service()->pending_extension_manager()->AddFromSync(
2961          kGoodId,
2962          GURL(kGoodUpdateURL),
2963          &IsExtension,
2964          kGoodInstallSilently,
2965          kGoodRemoteInstall,
2966          kGoodInstalledByCustodian));
2967  EXPECT_TRUE(service()->pending_extension_manager()->IsIdPending(kGoodId));
2968
2969  base::FilePath path = data_dir().AppendASCII("good.crx");
2970  UpdateExtension(kGoodId, path, ENABLED);
2971
2972  EXPECT_FALSE(service()->pending_extension_manager()->IsIdPending(kGoodId));
2973
2974  const Extension* extension = service()->GetExtensionById(kGoodId, true);
2975  ASSERT_TRUE(extension);
2976}
2977
2978namespace {
2979
2980bool IsTheme(const Extension* extension) {
2981  return extension->is_theme();
2982}
2983
2984}  // namespace
2985
2986// Test updating a pending theme.
2987// Disabled due to ASAN failure. http://crbug.com/108320
2988TEST_F(ExtensionServiceTest, DISABLED_UpdatePendingTheme) {
2989  InitializeEmptyExtensionService();
2990  EXPECT_TRUE(service()->pending_extension_manager()->AddFromSync(
2991      theme_crx, GURL(), &IsTheme, false, false, false));
2992  EXPECT_TRUE(service()->pending_extension_manager()->IsIdPending(theme_crx));
2993
2994  base::FilePath path = data_dir().AppendASCII("theme.crx");
2995  UpdateExtension(theme_crx, path, ENABLED);
2996
2997  EXPECT_FALSE(service()->pending_extension_manager()->IsIdPending(theme_crx));
2998
2999  const Extension* extension = service()->GetExtensionById(theme_crx, true);
3000  ASSERT_TRUE(extension);
3001
3002  EXPECT_FALSE(
3003      ExtensionPrefs::Get(profile())->IsExtensionDisabled(extension->id()));
3004  EXPECT_TRUE(service()->IsExtensionEnabled(theme_crx));
3005}
3006
3007#if defined(OS_CHROMEOS)
3008// Always fails on ChromeOS: http://crbug.com/79737
3009#define MAYBE_UpdatePendingExternalCrx DISABLED_UpdatePendingExternalCrx
3010#else
3011#define MAYBE_UpdatePendingExternalCrx UpdatePendingExternalCrx
3012#endif
3013// Test updating a pending CRX as if the source is an external extension
3014// with an update URL.  In this case we don't know if the CRX is a theme
3015// or not.
3016TEST_F(ExtensionServiceTest, MAYBE_UpdatePendingExternalCrx) {
3017  InitializeEmptyExtensionService();
3018  EXPECT_TRUE(service()->pending_extension_manager()->AddFromExternalUpdateUrl(
3019      theme_crx,
3020      std::string(),
3021      GURL(),
3022      Manifest::EXTERNAL_PREF_DOWNLOAD,
3023      Extension::NO_FLAGS,
3024      false));
3025
3026  EXPECT_TRUE(service()->pending_extension_manager()->IsIdPending(theme_crx));
3027
3028  base::FilePath path = data_dir().AppendASCII("theme.crx");
3029  UpdateExtension(theme_crx, path, ENABLED);
3030
3031  EXPECT_FALSE(service()->pending_extension_manager()->IsIdPending(theme_crx));
3032
3033  const Extension* extension = service()->GetExtensionById(theme_crx, true);
3034  ASSERT_TRUE(extension);
3035
3036  EXPECT_FALSE(
3037      ExtensionPrefs::Get(profile())->IsExtensionDisabled(extension->id()));
3038  EXPECT_TRUE(service()->IsExtensionEnabled(extension->id()));
3039  EXPECT_FALSE(
3040      extensions::util::IsIncognitoEnabled(extension->id(), profile()));
3041}
3042
3043// Test updating a pending CRX as if the source is an external extension
3044// with an update URL.  The external update should overwrite a sync update,
3045// but a sync update should not overwrite a non-sync update.
3046TEST_F(ExtensionServiceTest, UpdatePendingExternalCrxWinsOverSync) {
3047  InitializeEmptyExtensionService();
3048
3049  // Add a crx to be installed from the update mechanism.
3050  EXPECT_TRUE(
3051      service()->pending_extension_manager()->AddFromSync(
3052          kGoodId,
3053          GURL(kGoodUpdateURL),
3054          &IsExtension,
3055          kGoodInstallSilently,
3056          kGoodRemoteInstall,
3057          kGoodInstalledByCustodian));
3058
3059  // Check that there is a pending crx, with is_from_sync set to true.
3060  const extensions::PendingExtensionInfo* pending_extension_info;
3061  ASSERT_TRUE((pending_extension_info =
3062                   service()->pending_extension_manager()->GetById(kGoodId)));
3063  EXPECT_TRUE(pending_extension_info->is_from_sync());
3064
3065  // Add a crx to be updated, with the same ID, from a non-sync source.
3066  EXPECT_TRUE(service()->pending_extension_manager()->AddFromExternalUpdateUrl(
3067      kGoodId,
3068      std::string(),
3069      GURL(kGoodUpdateURL),
3070      Manifest::EXTERNAL_PREF_DOWNLOAD,
3071      Extension::NO_FLAGS,
3072      false));
3073
3074  // Check that there is a pending crx, with is_from_sync set to false.
3075  ASSERT_TRUE((pending_extension_info =
3076                   service()->pending_extension_manager()->GetById(kGoodId)));
3077  EXPECT_FALSE(pending_extension_info->is_from_sync());
3078  EXPECT_EQ(Manifest::EXTERNAL_PREF_DOWNLOAD,
3079            pending_extension_info->install_source());
3080
3081  // Add a crx to be installed from the update mechanism.
3082  EXPECT_FALSE(
3083      service()->pending_extension_manager()->AddFromSync(
3084          kGoodId,
3085          GURL(kGoodUpdateURL),
3086          &IsExtension,
3087          kGoodInstallSilently,
3088          kGoodRemoteInstall,
3089          kGoodInstalledByCustodian));
3090
3091  // Check that the external, non-sync update was not overridden.
3092  ASSERT_TRUE((pending_extension_info =
3093                   service()->pending_extension_manager()->GetById(kGoodId)));
3094  EXPECT_FALSE(pending_extension_info->is_from_sync());
3095  EXPECT_EQ(Manifest::EXTERNAL_PREF_DOWNLOAD,
3096            pending_extension_info->install_source());
3097}
3098
3099// Updating a theme should fail if the updater is explicitly told that
3100// the CRX is not a theme.
3101TEST_F(ExtensionServiceTest, UpdatePendingCrxThemeMismatch) {
3102  InitializeEmptyExtensionService();
3103  EXPECT_TRUE(service()->pending_extension_manager()->AddFromSync(
3104      theme_crx, GURL(), &IsExtension, true, false, false));
3105
3106  EXPECT_TRUE(service()->pending_extension_manager()->IsIdPending(theme_crx));
3107
3108  base::FilePath path = data_dir().AppendASCII("theme.crx");
3109  UpdateExtension(theme_crx, path, FAILED_SILENTLY);
3110
3111  EXPECT_FALSE(service()->pending_extension_manager()->IsIdPending(theme_crx));
3112
3113  const Extension* extension = service()->GetExtensionById(theme_crx, true);
3114  ASSERT_FALSE(extension);
3115}
3116
3117// TODO(akalin): Test updating a pending extension non-silently once
3118// we can mock out ExtensionInstallUI and inject our version into
3119// UpdateExtension().
3120
3121// Test updating a pending extension which fails the should-install test.
3122TEST_F(ExtensionServiceTest, UpdatePendingExtensionFailedShouldInstallTest) {
3123  InitializeEmptyExtensionService();
3124  // Add pending extension with a flipped is_theme.
3125  EXPECT_TRUE(
3126      service()->pending_extension_manager()->AddFromSync(
3127          kGoodId,
3128          GURL(kGoodUpdateURL),
3129          &IsTheme,
3130          kGoodInstallSilently,
3131          kGoodRemoteInstall,
3132          kGoodInstalledByCustodian));
3133  EXPECT_TRUE(service()->pending_extension_manager()->IsIdPending(kGoodId));
3134
3135  base::FilePath path = data_dir().AppendASCII("good.crx");
3136  UpdateExtension(kGoodId, path, UPDATED);
3137
3138  // TODO(akalin): Figure out how to check that the extensions
3139  // directory is cleaned up properly in OnExtensionInstalled().
3140
3141  EXPECT_FALSE(service()->pending_extension_manager()->IsIdPending(kGoodId));
3142}
3143
3144// TODO(akalin): Figure out how to test that installs of pending
3145// unsyncable extensions are blocked.
3146
3147// Test updating a pending extension for one that is not pending.
3148TEST_F(ExtensionServiceTest, UpdatePendingExtensionNotPending) {
3149  InitializeEmptyExtensionService();
3150
3151  base::FilePath path = data_dir().AppendASCII("good.crx");
3152  UpdateExtension(kGoodId, path, UPDATED);
3153
3154  EXPECT_FALSE(service()->pending_extension_manager()->IsIdPending(kGoodId));
3155}
3156
3157// Test updating a pending extension for one that is already
3158// installed.
3159TEST_F(ExtensionServiceTest, UpdatePendingExtensionAlreadyInstalled) {
3160  InitializeEmptyExtensionService();
3161
3162  base::FilePath path = data_dir().AppendASCII("good.crx");
3163  const Extension* good = InstallCRX(path, INSTALL_NEW);
3164  ASSERT_EQ(1u, registry()->enabled_extensions().size());
3165
3166  EXPECT_FALSE(good->is_theme());
3167
3168  // Use AddExtensionImpl() as AddFrom*() would balk.
3169  service()->pending_extension_manager()->AddExtensionImpl(
3170      good->id(),
3171      std::string(),
3172      extensions::ManifestURL::GetUpdateURL(good),
3173      Version(),
3174      &IsExtension,
3175      kGoodIsFromSync,
3176      kGoodInstallSilently,
3177      Manifest::INTERNAL,
3178      Extension::NO_FLAGS,
3179      false,
3180      kGoodRemoteInstall);
3181  UpdateExtension(good->id(), path, ENABLED);
3182
3183  EXPECT_FALSE(service()->pending_extension_manager()->IsIdPending(kGoodId));
3184}
3185
3186#if defined(ENABLE_BLACKLIST_TESTS)
3187// Tests blacklisting then unblacklisting extensions after the service has been
3188// initialized.
3189TEST_F(ExtensionServiceTest, SetUnsetBlacklistInPrefs) {
3190  extensions::TestBlacklist test_blacklist;
3191  // A profile with 3 extensions installed: good0, good1, and good2.
3192  InitializeGoodInstalledExtensionService();
3193  test_blacklist.Attach(service()->blacklist_);
3194  service()->Init();
3195
3196  const extensions::ExtensionSet& enabled_extensions =
3197      registry()->enabled_extensions();
3198  const extensions::ExtensionSet& blacklisted_extensions =
3199      registry()->blacklisted_extensions();
3200
3201  EXPECT_TRUE(enabled_extensions.Contains(good0) &&
3202              !blacklisted_extensions.Contains(good0));
3203  EXPECT_TRUE(enabled_extensions.Contains(good1) &&
3204              !blacklisted_extensions.Contains(good1));
3205  EXPECT_TRUE(enabled_extensions.Contains(good2) &&
3206              !blacklisted_extensions.Contains(good2));
3207
3208  EXPECT_FALSE(IsPrefExist(good0, "blacklist"));
3209  EXPECT_FALSE(IsPrefExist(good1, "blacklist"));
3210  EXPECT_FALSE(IsPrefExist(good2, "blacklist"));
3211  EXPECT_FALSE(IsPrefExist("invalid_id", "blacklist"));
3212
3213  // Blacklist good0 and good1 (and an invalid extension ID).
3214  test_blacklist.SetBlacklistState(
3215      good0, extensions::BLACKLISTED_MALWARE, true);
3216  test_blacklist.SetBlacklistState(
3217      good1, extensions::BLACKLISTED_MALWARE, true);
3218  test_blacklist.SetBlacklistState(
3219      "invalid_id", extensions::BLACKLISTED_MALWARE, true);
3220  base::RunLoop().RunUntilIdle();
3221
3222  EXPECT_TRUE(!enabled_extensions.Contains(good0) &&
3223              blacklisted_extensions.Contains(good0));
3224  EXPECT_TRUE(!enabled_extensions.Contains(good1) &&
3225              blacklisted_extensions.Contains(good1));
3226  EXPECT_TRUE(enabled_extensions.Contains(good2) &&
3227              !blacklisted_extensions.Contains(good2));
3228
3229  EXPECT_TRUE(ValidateBooleanPref(good0, "blacklist", true));
3230  EXPECT_TRUE(ValidateBooleanPref(good1, "blacklist", true));
3231  EXPECT_FALSE(IsPrefExist(good2, "blacklist"));
3232  EXPECT_FALSE(IsPrefExist("invalid_id", "blacklist"));
3233
3234  // Un-blacklist good1 and blacklist good2.
3235  test_blacklist.Clear(false);
3236  test_blacklist.SetBlacklistState(
3237      good0, extensions::BLACKLISTED_MALWARE, true);
3238  test_blacklist.SetBlacklistState(
3239      good2, extensions::BLACKLISTED_MALWARE, true);
3240  test_blacklist.SetBlacklistState(
3241      "invalid_id", extensions::BLACKLISTED_MALWARE, true);
3242  base::RunLoop().RunUntilIdle();
3243
3244  EXPECT_TRUE(!enabled_extensions.Contains(good0) &&
3245              blacklisted_extensions.Contains(good0));
3246  EXPECT_TRUE(enabled_extensions.Contains(good1) &&
3247              !blacklisted_extensions.Contains(good1));
3248  EXPECT_TRUE(!enabled_extensions.Contains(good2) &&
3249              blacklisted_extensions.Contains(good2));
3250
3251  EXPECT_TRUE(ValidateBooleanPref(good0, "blacklist", true));
3252  EXPECT_FALSE(IsPrefExist(good1, "blacklist"));
3253  EXPECT_TRUE(ValidateBooleanPref(good2, "blacklist", true));
3254  EXPECT_FALSE(IsPrefExist("invalid_id", "blacklist"));
3255}
3256#endif  // defined(ENABLE_BLACKLIST_TESTS)
3257
3258#if defined(ENABLE_BLACKLIST_TESTS)
3259// Tests trying to install a blacklisted extension.
3260TEST_F(ExtensionServiceTest, BlacklistedExtensionWillNotInstall) {
3261  scoped_refptr<FakeSafeBrowsingDatabaseManager> blacklist_db(
3262      new FakeSafeBrowsingDatabaseManager(true));
3263  Blacklist::ScopedDatabaseManagerForTest scoped_blacklist_db(blacklist_db);
3264
3265  InitializeEmptyExtensionService();
3266  service()->Init();
3267
3268  // After blacklisting good_crx, we cannot install it.
3269  blacklist_db->SetUnsafe(good_crx).NotifyUpdate();
3270  base::RunLoop().RunUntilIdle();
3271
3272  base::FilePath path = data_dir().AppendASCII("good.crx");
3273  // HACK: specify WAS_INSTALLED_BY_DEFAULT so that test machinery doesn't
3274  // decide to install this silently. Somebody should fix these tests, all
3275  // 6,000 lines of them. Hah!
3276  InstallCRX(path, INSTALL_FAILED, Extension::WAS_INSTALLED_BY_DEFAULT);
3277  EXPECT_EQ(0u, registry()->enabled_extensions().size());
3278}
3279#endif  // defined(ENABLE_BLACKLIST_TESTS)
3280
3281#if defined(ENABLE_BLACKLIST_TESTS)
3282// Unload blacklisted extension on policy change.
3283TEST_F(ExtensionServiceTest, UnloadBlacklistedExtensionPolicy) {
3284  extensions::TestBlacklist test_blacklist;
3285
3286  // A profile with no extensions installed.
3287  InitializeEmptyExtensionServiceWithTestingPrefs();
3288  test_blacklist.Attach(service()->blacklist_);
3289
3290  base::FilePath path = data_dir().AppendASCII("good.crx");
3291
3292  const Extension* good = InstallCRX(path, INSTALL_NEW);
3293  EXPECT_EQ(good_crx, good->id());
3294  UpdateExtension(good_crx, path, FAILED_SILENTLY);
3295  EXPECT_EQ(1u, registry()->enabled_extensions().size());
3296
3297  {
3298    ManagementPrefUpdater pref(profile_->GetTestingPrefService());
3299    pref.SetIndividualExtensionInstallationAllowed(good_crx, true);
3300  }
3301
3302  test_blacklist.SetBlacklistState(
3303      good_crx, extensions::BLACKLISTED_MALWARE, true);
3304  base::RunLoop().RunUntilIdle();
3305
3306  // The good_crx is blacklisted and the whitelist doesn't negate it.
3307  ASSERT_TRUE(ValidateBooleanPref(good_crx, "blacklist", true));
3308  EXPECT_EQ(0u, registry()->enabled_extensions().size());
3309}
3310#endif  // defined(ENABLE_BLACKLIST_TESTS)
3311
3312#if defined(ENABLE_BLACKLIST_TESTS)
3313// Tests that a blacklisted extension is eventually unloaded on startup, if it
3314// wasn't already.
3315TEST_F(ExtensionServiceTest, WillNotLoadBlacklistedExtensionsFromDirectory) {
3316  extensions::TestBlacklist test_blacklist;
3317
3318  // A profile with 3 extensions installed: good0, good1, and good2.
3319  InitializeGoodInstalledExtensionService();
3320  test_blacklist.Attach(service()->blacklist_);
3321
3322  // Blacklist good1 before the service initializes.
3323  test_blacklist.SetBlacklistState(
3324      good1, extensions::BLACKLISTED_MALWARE, false);
3325
3326  // Load extensions.
3327  service()->Init();
3328  ASSERT_EQ(3u, loaded_.size());  // hasn't had time to blacklist yet
3329
3330  base::RunLoop().RunUntilIdle();
3331
3332  ASSERT_EQ(1u, registry()->blacklisted_extensions().size());
3333  ASSERT_EQ(2u, registry()->enabled_extensions().size());
3334
3335  ASSERT_TRUE(registry()->enabled_extensions().Contains(good0));
3336  ASSERT_TRUE(registry()->blacklisted_extensions().Contains(good1));
3337  ASSERT_TRUE(registry()->enabled_extensions().Contains(good2));
3338}
3339#endif  // defined(ENABLE_BLACKLIST_TESTS)
3340
3341#if defined(ENABLE_BLACKLIST_TESTS)
3342// Tests extensions blacklisted in prefs on startup; one still blacklisted by
3343// safe browsing, the other not. The not-blacklisted one should recover.
3344TEST_F(ExtensionServiceTest, BlacklistedInPrefsFromStartup) {
3345  extensions::TestBlacklist test_blacklist;
3346
3347  InitializeGoodInstalledExtensionService();
3348  test_blacklist.Attach(service()->blacklist_);
3349  ExtensionPrefs::Get(profile())->SetExtensionBlacklisted(good0, true);
3350  ExtensionPrefs::Get(profile())->SetExtensionBlacklisted(good1, true);
3351
3352  test_blacklist.SetBlacklistState(
3353      good1, extensions::BLACKLISTED_MALWARE, false);
3354
3355  // Extension service hasn't loaded yet, but IsExtensionEnabled reads out of
3356  // prefs. Ensure it takes into account the blacklist state (crbug.com/373842).
3357  EXPECT_FALSE(service()->IsExtensionEnabled(good0));
3358  EXPECT_FALSE(service()->IsExtensionEnabled(good1));
3359  EXPECT_TRUE(service()->IsExtensionEnabled(good2));
3360
3361  service()->Init();
3362
3363  EXPECT_EQ(2u, registry()->blacklisted_extensions().size());
3364  EXPECT_EQ(1u, registry()->enabled_extensions().size());
3365
3366  EXPECT_TRUE(registry()->blacklisted_extensions().Contains(good0));
3367  EXPECT_TRUE(registry()->blacklisted_extensions().Contains(good1));
3368  EXPECT_TRUE(registry()->enabled_extensions().Contains(good2));
3369
3370  // Give time for the blacklist to update.
3371  base::RunLoop().RunUntilIdle();
3372
3373  EXPECT_EQ(1u, registry()->blacklisted_extensions().size());
3374  EXPECT_EQ(2u, registry()->enabled_extensions().size());
3375
3376  EXPECT_TRUE(registry()->enabled_extensions().Contains(good0));
3377  EXPECT_TRUE(registry()->blacklisted_extensions().Contains(good1));
3378  EXPECT_TRUE(registry()->enabled_extensions().Contains(good2));
3379}
3380#endif  // defined(ENABLE_BLACKLIST_TESTS)
3381
3382#if defined(ENABLE_BLACKLIST_TESTS)
3383// Extension is added to blacklist with BLACKLISTED_POTENTIALLY_UNWANTED state
3384// after it is installed. It is then successfully re-enabled by the user.
3385TEST_F(ExtensionServiceTest, GreylistedExtensionDisabled) {
3386  extensions::TestBlacklist test_blacklist;
3387  // A profile with 3 extensions installed: good0, good1, and good2.
3388  InitializeGoodInstalledExtensionService();
3389  test_blacklist.Attach(service()->blacklist_);
3390  service()->Init();
3391
3392  const extensions::ExtensionSet& enabled_extensions =
3393      registry()->enabled_extensions();
3394  const extensions::ExtensionSet& disabled_extensions =
3395      registry()->disabled_extensions();
3396
3397  EXPECT_TRUE(enabled_extensions.Contains(good0));
3398  EXPECT_TRUE(enabled_extensions.Contains(good1));
3399  EXPECT_TRUE(enabled_extensions.Contains(good2));
3400
3401  // Blacklist good0 and good1 (and an invalid extension ID).
3402  test_blacklist.SetBlacklistState(
3403      good0, extensions::BLACKLISTED_CWS_POLICY_VIOLATION, true);
3404  test_blacklist.SetBlacklistState(
3405      good1, extensions::BLACKLISTED_POTENTIALLY_UNWANTED, true);
3406  test_blacklist.SetBlacklistState(
3407      "invalid_id", extensions::BLACKLISTED_MALWARE, true);
3408  base::RunLoop().RunUntilIdle();
3409
3410  EXPECT_FALSE(enabled_extensions.Contains(good0));
3411  EXPECT_TRUE(disabled_extensions.Contains(good0));
3412  EXPECT_FALSE(enabled_extensions.Contains(good1));
3413  EXPECT_TRUE(disabled_extensions.Contains(good1));
3414  EXPECT_TRUE(enabled_extensions.Contains(good2));
3415  EXPECT_FALSE(disabled_extensions.Contains(good2));
3416
3417  ValidateIntegerPref(
3418      good0, "blacklist_state", extensions::BLACKLISTED_CWS_POLICY_VIOLATION);
3419  ValidateIntegerPref(
3420      good1, "blacklist_state", extensions::BLACKLISTED_POTENTIALLY_UNWANTED);
3421
3422  // Now user enables good0.
3423  service()->EnableExtension(good0);
3424
3425  EXPECT_TRUE(enabled_extensions.Contains(good0));
3426  EXPECT_FALSE(disabled_extensions.Contains(good0));
3427  EXPECT_FALSE(enabled_extensions.Contains(good1));
3428  EXPECT_TRUE(disabled_extensions.Contains(good1));
3429
3430  // Remove extensions from blacklist.
3431  test_blacklist.SetBlacklistState(
3432      good0, extensions::NOT_BLACKLISTED, true);
3433  test_blacklist.SetBlacklistState(
3434      good1, extensions::NOT_BLACKLISTED, true);
3435  base::RunLoop().RunUntilIdle();
3436
3437  // All extensions are enabled.
3438  EXPECT_TRUE(enabled_extensions.Contains(good0));
3439  EXPECT_FALSE(disabled_extensions.Contains(good0));
3440  EXPECT_TRUE(enabled_extensions.Contains(good1));
3441  EXPECT_FALSE(disabled_extensions.Contains(good1));
3442  EXPECT_TRUE(enabled_extensions.Contains(good2));
3443  EXPECT_FALSE(disabled_extensions.Contains(good2));
3444}
3445#endif  // defined(ENABLE_BLACKLIST_TESTS)
3446
3447#if defined(ENABLE_BLACKLIST_TESTS)
3448// When extension is removed from greylist, do not re-enable it if it is
3449// disabled by user.
3450TEST_F(ExtensionServiceTest, GreylistDontEnableManuallyDisabled) {
3451  extensions::TestBlacklist test_blacklist;
3452  // A profile with 3 extensions installed: good0, good1, and good2.
3453  InitializeGoodInstalledExtensionService();
3454  test_blacklist.Attach(service()->blacklist_);
3455  service()->Init();
3456
3457  const extensions::ExtensionSet& enabled_extensions =
3458      registry()->enabled_extensions();
3459  const extensions::ExtensionSet& disabled_extensions =
3460      registry()->disabled_extensions();
3461
3462  // Manually disable.
3463  service()->DisableExtension(good0,
3464                              extensions::Extension::DISABLE_USER_ACTION);
3465
3466  test_blacklist.SetBlacklistState(
3467      good0, extensions::BLACKLISTED_CWS_POLICY_VIOLATION, true);
3468  test_blacklist.SetBlacklistState(
3469      good1, extensions::BLACKLISTED_POTENTIALLY_UNWANTED, true);
3470  test_blacklist.SetBlacklistState(
3471      good2, extensions::BLACKLISTED_SECURITY_VULNERABILITY, true);
3472  base::RunLoop().RunUntilIdle();
3473
3474  // All extensions disabled.
3475  EXPECT_FALSE(enabled_extensions.Contains(good0));
3476  EXPECT_TRUE(disabled_extensions.Contains(good0));
3477  EXPECT_FALSE(enabled_extensions.Contains(good1));
3478  EXPECT_TRUE(disabled_extensions.Contains(good1));
3479  EXPECT_FALSE(enabled_extensions.Contains(good2));
3480  EXPECT_TRUE(disabled_extensions.Contains(good2));
3481
3482  // Greylisted extension can be enabled.
3483  service()->EnableExtension(good1);
3484  EXPECT_TRUE(enabled_extensions.Contains(good1));
3485  EXPECT_FALSE(disabled_extensions.Contains(good1));
3486
3487  // good1 is now manually disabled.
3488  service()->DisableExtension(good1,
3489                              extensions::Extension::DISABLE_USER_ACTION);
3490  EXPECT_FALSE(enabled_extensions.Contains(good1));
3491  EXPECT_TRUE(disabled_extensions.Contains(good1));
3492
3493  // Remove extensions from blacklist.
3494  test_blacklist.SetBlacklistState(
3495      good0, extensions::NOT_BLACKLISTED, true);
3496  test_blacklist.SetBlacklistState(
3497      good1, extensions::NOT_BLACKLISTED, true);
3498  test_blacklist.SetBlacklistState(
3499      good2, extensions::NOT_BLACKLISTED, true);
3500  base::RunLoop().RunUntilIdle();
3501
3502  // good0 and good1 remain disabled.
3503  EXPECT_FALSE(enabled_extensions.Contains(good0));
3504  EXPECT_TRUE(disabled_extensions.Contains(good0));
3505  EXPECT_FALSE(enabled_extensions.Contains(good1));
3506  EXPECT_TRUE(disabled_extensions.Contains(good1));
3507  EXPECT_TRUE(enabled_extensions.Contains(good2));
3508  EXPECT_FALSE(disabled_extensions.Contains(good2));
3509}
3510#endif  // defined(ENABLE_BLACKLIST_TESTS)
3511
3512#if defined(ENABLE_BLACKLIST_TESTS)
3513// Blacklisted extension with unknown state are not enabled/disabled.
3514TEST_F(ExtensionServiceTest, GreylistUnknownDontChange) {
3515  extensions::TestBlacklist test_blacklist;
3516  // A profile with 3 extensions installed: good0, good1, and good2.
3517  InitializeGoodInstalledExtensionService();
3518  test_blacklist.Attach(service()->blacklist_);
3519  service()->Init();
3520
3521  const extensions::ExtensionSet& enabled_extensions =
3522      registry()->enabled_extensions();
3523  const extensions::ExtensionSet& disabled_extensions =
3524      registry()->disabled_extensions();
3525
3526  test_blacklist.SetBlacklistState(
3527      good0, extensions::BLACKLISTED_CWS_POLICY_VIOLATION, true);
3528  test_blacklist.SetBlacklistState(
3529      good1, extensions::BLACKLISTED_POTENTIALLY_UNWANTED, true);
3530  base::RunLoop().RunUntilIdle();
3531
3532  EXPECT_FALSE(enabled_extensions.Contains(good0));
3533  EXPECT_TRUE(disabled_extensions.Contains(good0));
3534  EXPECT_FALSE(enabled_extensions.Contains(good1));
3535  EXPECT_TRUE(disabled_extensions.Contains(good1));
3536  EXPECT_TRUE(enabled_extensions.Contains(good2));
3537  EXPECT_FALSE(disabled_extensions.Contains(good2));
3538
3539  test_blacklist.SetBlacklistState(
3540      good0, extensions::NOT_BLACKLISTED, true);
3541  test_blacklist.SetBlacklistState(
3542      good1, extensions::BLACKLISTED_UNKNOWN, true);
3543  test_blacklist.SetBlacklistState(
3544      good2, extensions::BLACKLISTED_UNKNOWN, true);
3545  base::RunLoop().RunUntilIdle();
3546
3547  // good0 re-enabled, other remain as they were.
3548  EXPECT_TRUE(enabled_extensions.Contains(good0));
3549  EXPECT_FALSE(disabled_extensions.Contains(good0));
3550  EXPECT_FALSE(enabled_extensions.Contains(good1));
3551  EXPECT_TRUE(disabled_extensions.Contains(good1));
3552  EXPECT_TRUE(enabled_extensions.Contains(good2));
3553  EXPECT_FALSE(disabled_extensions.Contains(good2));
3554}
3555
3556// Tests that blacklisted extensions cannot be reloaded, both those loaded
3557// before and after extension service startup.
3558TEST_F(ExtensionServiceTest, ReloadBlacklistedExtension) {
3559  extensions::TestBlacklist test_blacklist;
3560
3561  InitializeGoodInstalledExtensionService();
3562  test_blacklist.Attach(service()->blacklist_);
3563
3564  test_blacklist.SetBlacklistState(
3565      good1, extensions::BLACKLISTED_MALWARE, false);
3566  service()->Init();
3567  test_blacklist.SetBlacklistState(
3568      good2, extensions::BLACKLISTED_MALWARE, false);
3569  base::RunLoop().RunUntilIdle();
3570
3571  EXPECT_EQ(StringSet(good0), registry()->enabled_extensions().GetIDs());
3572  EXPECT_EQ(StringSet(good1, good2),
3573            registry()->blacklisted_extensions().GetIDs());
3574
3575  service()->ReloadExtension(good1);
3576  service()->ReloadExtension(good2);
3577  base::RunLoop().RunUntilIdle();
3578
3579  EXPECT_EQ(StringSet(good0), registry()->enabled_extensions().GetIDs());
3580  EXPECT_EQ(StringSet(good1, good2),
3581            registry()->blacklisted_extensions().GetIDs());
3582}
3583
3584#endif  // defined(ENABLE_BLACKLIST_TESTS)
3585
3586// Will not install extension blacklisted by policy.
3587TEST_F(ExtensionServiceTest, BlacklistedByPolicyWillNotInstall) {
3588  InitializeEmptyExtensionServiceWithTestingPrefs();
3589
3590  // Blacklist everything.
3591  {
3592    ManagementPrefUpdater pref(profile_->GetTestingPrefService());
3593    pref.SetBlacklistedByDefault(true);
3594  }
3595
3596  // Blacklist prevents us from installing good_crx.
3597  base::FilePath path = data_dir().AppendASCII("good.crx");
3598  InstallCRX(path, INSTALL_FAILED);
3599  EXPECT_EQ(0u, registry()->enabled_extensions().size());
3600
3601  // Now whitelist this particular extension.
3602  {
3603    ManagementPrefUpdater pref(profile_->GetTestingPrefService());
3604    pref.SetIndividualExtensionInstallationAllowed(good_crx, true);
3605  }
3606
3607  // Ensure we can now install good_crx.
3608  InstallCRX(path, INSTALL_NEW);
3609  EXPECT_EQ(1u, registry()->enabled_extensions().size());
3610}
3611
3612// Extension blacklisted by policy get unloaded after installing.
3613TEST_F(ExtensionServiceTest, BlacklistedByPolicyRemovedIfRunning) {
3614  InitializeEmptyExtensionServiceWithTestingPrefs();
3615
3616  // Install good_crx.
3617  base::FilePath path = data_dir().AppendASCII("good.crx");
3618  InstallCRX(path, INSTALL_NEW);
3619  EXPECT_EQ(1u, registry()->enabled_extensions().size());
3620
3621  {
3622    ManagementPrefUpdater pref(profile_->GetTestingPrefService());
3623    // Blacklist this extension.
3624    pref.SetIndividualExtensionInstallationAllowed(good_crx, false);
3625  }
3626
3627  // Extension should not be running now.
3628  base::RunLoop().RunUntilIdle();
3629  EXPECT_EQ(0u, registry()->enabled_extensions().size());
3630}
3631
3632// Tests that component extensions are not blacklisted by policy.
3633TEST_F(ExtensionServiceTest, ComponentExtensionWhitelisted) {
3634  InitializeEmptyExtensionServiceWithTestingPrefs();
3635
3636  // Blacklist everything.
3637  {
3638    ManagementPrefUpdater pref(profile_->GetTestingPrefService());
3639    pref.SetBlacklistedByDefault(true);
3640  }
3641
3642  // Install a component extension.
3643  base::FilePath path = data_dir()
3644                            .AppendASCII("good")
3645                            .AppendASCII("Extensions")
3646                            .AppendASCII(good0)
3647                            .AppendASCII("1.0.0.0");
3648  std::string manifest;
3649  ASSERT_TRUE(base::ReadFileToString(
3650      path.Append(extensions::kManifestFilename), &manifest));
3651  service()->component_loader()->Add(manifest, path);
3652  service()->Init();
3653
3654  // Extension should be installed despite blacklist.
3655  ASSERT_EQ(1u, registry()->enabled_extensions().size());
3656  EXPECT_TRUE(service()->GetExtensionById(good0, false));
3657
3658  // Poke external providers and make sure the extension is still present.
3659  service()->CheckForExternalUpdates();
3660  ASSERT_EQ(1u, registry()->enabled_extensions().size());
3661  EXPECT_TRUE(service()->GetExtensionById(good0, false));
3662
3663  // Extension should not be uninstalled on blacklist changes.
3664  {
3665    ManagementPrefUpdater pref(profile_->GetTestingPrefService());
3666    pref.SetIndividualExtensionInstallationAllowed(good0, false);
3667  }
3668  base::RunLoop().RunUntilIdle();
3669  ASSERT_EQ(1u, registry()->enabled_extensions().size());
3670  EXPECT_TRUE(service()->GetExtensionById(good0, false));
3671}
3672
3673// Tests that policy-installed extensions are not blacklisted by policy.
3674TEST_F(ExtensionServiceTest, PolicyInstalledExtensionsWhitelisted) {
3675  InitializeEmptyExtensionServiceWithTestingPrefs();
3676
3677  {
3678    ManagementPrefUpdater pref(profile_->GetTestingPrefService());
3679    // Blacklist everything.
3680    pref.SetBlacklistedByDefault(true);
3681    // Mark good.crx for force-installation.
3682    pref.SetIndividualExtensionAutoInstalled(
3683        good_crx, "http://example.com/update_url", true);
3684  }
3685
3686  // Have policy force-install an extension.
3687  MockExtensionProvider* provider =
3688      new MockExtensionProvider(service(), Manifest::EXTERNAL_POLICY_DOWNLOAD);
3689  AddMockExternalProvider(provider);
3690  provider->UpdateOrAddExtension(
3691      good_crx, "1.0.0.0", data_dir().AppendASCII("good.crx"));
3692
3693  // Reloading extensions should find our externally registered extension
3694  // and install it.
3695  content::WindowedNotificationObserver observer(
3696      extensions::NOTIFICATION_CRX_INSTALLER_DONE,
3697      content::NotificationService::AllSources());
3698  service()->CheckForExternalUpdates();
3699  observer.Wait();
3700
3701  // Extension should be installed despite blacklist.
3702  ASSERT_EQ(1u, registry()->enabled_extensions().size());
3703  EXPECT_TRUE(service()->GetExtensionById(good_crx, false));
3704
3705  // Blacklist update should not uninstall the extension.
3706  {
3707    ManagementPrefUpdater pref(profile_->GetTestingPrefService());
3708    pref.SetIndividualExtensionInstallationAllowed(good0, false);
3709  }
3710  base::RunLoop().RunUntilIdle();
3711  ASSERT_EQ(1u, registry()->enabled_extensions().size());
3712  EXPECT_TRUE(service()->GetExtensionById(good_crx, false));
3713}
3714
3715// Tests that extensions cannot be installed if the policy provider prohibits
3716// it. This functionality is implemented in CrxInstaller::ConfirmInstall().
3717TEST_F(ExtensionServiceTest, ManagementPolicyProhibitsInstall) {
3718  InitializeEmptyExtensionService();
3719
3720  GetManagementPolicy()->UnregisterAllProviders();
3721  extensions::TestManagementPolicyProvider provider_(
3722      extensions::TestManagementPolicyProvider::PROHIBIT_LOAD);
3723  GetManagementPolicy()->RegisterProvider(&provider_);
3724
3725  InstallCRX(data_dir().AppendASCII("good.crx"), INSTALL_FAILED);
3726  EXPECT_EQ(0u, registry()->enabled_extensions().size());
3727}
3728
3729// Tests that extensions cannot be loaded from prefs if the policy provider
3730// prohibits it. This functionality is implemented in InstalledLoader::Load().
3731TEST_F(ExtensionServiceTest, ManagementPolicyProhibitsLoadFromPrefs) {
3732  InitializeEmptyExtensionService();
3733
3734  // Create a fake extension to be loaded as though it were read from prefs.
3735  base::FilePath path =
3736      data_dir().AppendASCII("management").AppendASCII("simple_extension");
3737  base::DictionaryValue manifest;
3738  manifest.SetString(keys::kName, "simple_extension");
3739  manifest.SetString(keys::kVersion, "1");
3740  // UNPACKED is for extensions loaded from a directory. We use it here, even
3741  // though we're testing loading from prefs, so that we don't need to provide
3742  // an extension key.
3743  extensions::ExtensionInfo extension_info(
3744      &manifest, std::string(), path, Manifest::UNPACKED);
3745
3746  // Ensure we can load it with no management policy in place.
3747  GetManagementPolicy()->UnregisterAllProviders();
3748  EXPECT_EQ(0u, registry()->enabled_extensions().size());
3749  extensions::InstalledLoader(service()).Load(extension_info, false);
3750  EXPECT_EQ(1u, registry()->enabled_extensions().size());
3751
3752  const Extension* extension =
3753      (registry()->enabled_extensions().begin())->get();
3754  EXPECT_TRUE(
3755      service()->UninstallExtension(extension->id(),
3756                                    extensions::UNINSTALL_REASON_FOR_TESTING,
3757                                    base::Bind(&base::DoNothing),
3758                                    NULL));
3759  EXPECT_EQ(0u, registry()->enabled_extensions().size());
3760
3761  // Ensure we cannot load it if management policy prohibits installation.
3762  extensions::TestManagementPolicyProvider provider_(
3763      extensions::TestManagementPolicyProvider::PROHIBIT_LOAD);
3764  GetManagementPolicy()->RegisterProvider(&provider_);
3765
3766  extensions::InstalledLoader(service()).Load(extension_info, false);
3767  EXPECT_EQ(0u, registry()->enabled_extensions().size());
3768}
3769
3770// Tests disabling an extension when prohibited by the ManagementPolicy.
3771TEST_F(ExtensionServiceTest, ManagementPolicyProhibitsDisable) {
3772  InitializeEmptyExtensionService();
3773
3774  InstallCRX(data_dir().AppendASCII("good.crx"), INSTALL_NEW);
3775  EXPECT_EQ(1u, registry()->enabled_extensions().size());
3776  EXPECT_EQ(0u, registry()->disabled_extensions().size());
3777
3778  GetManagementPolicy()->UnregisterAllProviders();
3779  extensions::TestManagementPolicyProvider provider(
3780      extensions::TestManagementPolicyProvider::PROHIBIT_MODIFY_STATUS);
3781  GetManagementPolicy()->RegisterProvider(&provider);
3782
3783  // Attempt to disable it.
3784  service()->DisableExtension(good_crx, Extension::DISABLE_USER_ACTION);
3785
3786  EXPECT_EQ(1u, registry()->enabled_extensions().size());
3787  EXPECT_TRUE(service()->GetExtensionById(good_crx, false));
3788  EXPECT_EQ(0u, registry()->disabled_extensions().size());
3789}
3790
3791// Tests uninstalling an extension when prohibited by the ManagementPolicy.
3792TEST_F(ExtensionServiceTest, ManagementPolicyProhibitsUninstall) {
3793  InitializeEmptyExtensionService();
3794
3795  InstallCRX(data_dir().AppendASCII("good.crx"), INSTALL_NEW);
3796  EXPECT_EQ(1u, registry()->enabled_extensions().size());
3797  EXPECT_EQ(0u, registry()->disabled_extensions().size());
3798
3799  GetManagementPolicy()->UnregisterAllProviders();
3800  extensions::TestManagementPolicyProvider provider(
3801      extensions::TestManagementPolicyProvider::PROHIBIT_MODIFY_STATUS);
3802  GetManagementPolicy()->RegisterProvider(&provider);
3803
3804  // Attempt to uninstall it.
3805  EXPECT_FALSE(
3806      service()->UninstallExtension(good_crx,
3807                                    extensions::UNINSTALL_REASON_FOR_TESTING,
3808                                    base::Bind(&base::DoNothing),
3809                                    NULL));
3810
3811  EXPECT_EQ(1u, registry()->enabled_extensions().size());
3812  EXPECT_TRUE(service()->GetExtensionById(good_crx, false));
3813}
3814
3815// Tests that previously installed extensions that are now prohibited from
3816// being installed are removed.
3817TEST_F(ExtensionServiceTest, ManagementPolicyUnloadsAllProhibited) {
3818  InitializeEmptyExtensionService();
3819
3820  InstallCRX(data_dir().AppendASCII("good.crx"), INSTALL_NEW);
3821  InstallCRX(data_dir().AppendASCII("page_action.crx"), INSTALL_NEW);
3822  EXPECT_EQ(2u, registry()->enabled_extensions().size());
3823  EXPECT_EQ(0u, registry()->disabled_extensions().size());
3824
3825  GetManagementPolicy()->UnregisterAllProviders();
3826  extensions::TestManagementPolicyProvider provider(
3827      extensions::TestManagementPolicyProvider::PROHIBIT_LOAD);
3828  GetManagementPolicy()->RegisterProvider(&provider);
3829
3830  // Run the policy check.
3831  service()->CheckManagementPolicy();
3832  EXPECT_EQ(0u, registry()->enabled_extensions().size());
3833  EXPECT_EQ(0u, registry()->disabled_extensions().size());
3834}
3835
3836// Tests that previously disabled extensions that are now required to be
3837// enabled are re-enabled on reinstall.
3838TEST_F(ExtensionServiceTest, ManagementPolicyRequiresEnable) {
3839  InitializeEmptyExtensionService();
3840
3841  // Install, then disable, an extension.
3842  InstallCRX(data_dir().AppendASCII("good.crx"), INSTALL_NEW);
3843  EXPECT_EQ(1u, registry()->enabled_extensions().size());
3844  service()->DisableExtension(good_crx, Extension::DISABLE_USER_ACTION);
3845  EXPECT_EQ(1u, registry()->disabled_extensions().size());
3846
3847  // Register an ExtensionMnagementPolicy that requires the extension to remain
3848  // enabled.
3849  GetManagementPolicy()->UnregisterAllProviders();
3850  extensions::TestManagementPolicyProvider provider(
3851      extensions::TestManagementPolicyProvider::MUST_REMAIN_ENABLED);
3852  GetManagementPolicy()->RegisterProvider(&provider);
3853
3854  // Reinstall the extension.
3855  InstallCRX(data_dir().AppendASCII("good.crx"), INSTALL_UPDATED);
3856  EXPECT_EQ(1u, registry()->enabled_extensions().size());
3857  EXPECT_EQ(0u, registry()->disabled_extensions().size());
3858}
3859
3860// Flaky on windows; http://crbug.com/309833
3861#if defined(OS_WIN)
3862#define MAYBE_ExternalExtensionAutoAcknowledgement DISABLED_ExternalExtensionAutoAcknowledgement
3863#else
3864#define MAYBE_ExternalExtensionAutoAcknowledgement ExternalExtensionAutoAcknowledgement
3865#endif
3866TEST_F(ExtensionServiceTest, MAYBE_ExternalExtensionAutoAcknowledgement) {
3867  InitializeEmptyExtensionService();
3868  service()->set_extensions_enabled(true);
3869
3870  {
3871    // Register and install an external extension.
3872    MockExtensionProvider* provider =
3873        new MockExtensionProvider(service(), Manifest::EXTERNAL_PREF);
3874    AddMockExternalProvider(provider);
3875    provider->UpdateOrAddExtension(
3876        good_crx, "1.0.0.0", data_dir().AppendASCII("good.crx"));
3877  }
3878  {
3879    // Have policy force-install an extension.
3880    MockExtensionProvider* provider = new MockExtensionProvider(
3881        service(), Manifest::EXTERNAL_POLICY_DOWNLOAD);
3882    AddMockExternalProvider(provider);
3883    provider->UpdateOrAddExtension(
3884        page_action, "1.0.0.0", data_dir().AppendASCII("page_action.crx"));
3885  }
3886
3887  // Providers are set up. Let them run.
3888  int count = 2;
3889  content::WindowedNotificationObserver observer(
3890      extensions::NOTIFICATION_CRX_INSTALLER_DONE,
3891      base::Bind(&WaitForCountNotificationsCallback, &count));
3892  service()->CheckForExternalUpdates();
3893
3894  observer.Wait();
3895
3896  ASSERT_EQ(2u, registry()->enabled_extensions().size());
3897  EXPECT_TRUE(service()->GetExtensionById(good_crx, false));
3898  EXPECT_TRUE(service()->GetExtensionById(page_action, false));
3899  ExtensionPrefs* prefs = ExtensionPrefs::Get(profile());
3900  ASSERT_TRUE(!prefs->IsExternalExtensionAcknowledged(good_crx));
3901  ASSERT_TRUE(prefs->IsExternalExtensionAcknowledged(page_action));
3902}
3903
3904#if !defined(OS_CHROMEOS)
3905// This tests if default apps are installed correctly.
3906TEST_F(ExtensionServiceTest, DefaultAppsInstall) {
3907  InitializeEmptyExtensionService();
3908  service()->set_extensions_enabled(true);
3909
3910  {
3911    std::string json_data =
3912        "{"
3913        "  \"ldnnhddmnhbkjipkidpdiheffobcpfmf\" : {"
3914        "    \"external_crx\": \"good.crx\","
3915        "    \"external_version\": \"1.0.0.0\","
3916        "    \"is_bookmark_app\": false"
3917        "  }"
3918        "}";
3919    default_apps::Provider* provider = new default_apps::Provider(
3920        profile(),
3921        service(),
3922        new extensions::ExternalTestingLoader(json_data, data_dir()),
3923        Manifest::INTERNAL,
3924        Manifest::INVALID_LOCATION,
3925        Extension::FROM_WEBSTORE | Extension::WAS_INSTALLED_BY_DEFAULT);
3926
3927    AddMockExternalProvider(provider);
3928  }
3929
3930  ASSERT_EQ(0u, registry()->enabled_extensions().size());
3931  content::WindowedNotificationObserver observer(
3932      extensions::NOTIFICATION_CRX_INSTALLER_DONE,
3933      content::NotificationService::AllSources());
3934  service()->CheckForExternalUpdates();
3935  observer.Wait();
3936
3937  ASSERT_EQ(1u, registry()->enabled_extensions().size());
3938  EXPECT_TRUE(service()->GetExtensionById(good_crx, false));
3939  const Extension* extension = service()->GetExtensionById(good_crx, false);
3940  EXPECT_TRUE(extension->from_webstore());
3941  EXPECT_TRUE(extension->was_installed_by_default());
3942}
3943#endif
3944
3945// Tests disabling extensions
3946TEST_F(ExtensionServiceTest, DisableExtension) {
3947  InitializeEmptyExtensionService();
3948
3949  InstallCRX(data_dir().AppendASCII("good.crx"), INSTALL_NEW);
3950  EXPECT_TRUE(service()->GetExtensionById(good_crx, true));
3951  EXPECT_TRUE(service()->GetExtensionById(good_crx, false));
3952
3953  EXPECT_EQ(1u, registry()->enabled_extensions().size());
3954  EXPECT_EQ(0u, registry()->disabled_extensions().size());
3955  EXPECT_EQ(0u, registry()->terminated_extensions().size());
3956  EXPECT_EQ(0u, registry()->blacklisted_extensions().size());
3957
3958  // Disable it.
3959  service()->DisableExtension(good_crx, Extension::DISABLE_USER_ACTION);
3960
3961  EXPECT_TRUE(service()->GetExtensionById(good_crx, true));
3962  EXPECT_FALSE(service()->GetExtensionById(good_crx, false));
3963  EXPECT_EQ(0u, registry()->enabled_extensions().size());
3964  EXPECT_EQ(1u, registry()->disabled_extensions().size());
3965  EXPECT_EQ(0u, registry()->terminated_extensions().size());
3966  EXPECT_EQ(0u, registry()->blacklisted_extensions().size());
3967}
3968
3969TEST_F(ExtensionServiceTest, TerminateExtension) {
3970  InitializeEmptyExtensionService();
3971
3972  InstallCRX(data_dir().AppendASCII("good.crx"), INSTALL_NEW);
3973  EXPECT_EQ(1u, registry()->enabled_extensions().size());
3974  EXPECT_EQ(0u, registry()->disabled_extensions().size());
3975  EXPECT_EQ(0u, registry()->terminated_extensions().size());
3976  EXPECT_EQ(0u, registry()->blacklisted_extensions().size());
3977
3978  TerminateExtension(good_crx);
3979
3980  EXPECT_EQ(0u, registry()->enabled_extensions().size());
3981  EXPECT_EQ(0u, registry()->disabled_extensions().size());
3982  EXPECT_EQ(1u, registry()->terminated_extensions().size());
3983  EXPECT_EQ(0u, registry()->blacklisted_extensions().size());
3984}
3985
3986TEST_F(ExtensionServiceTest, DisableTerminatedExtension) {
3987  InitializeEmptyExtensionService();
3988
3989  InstallCRX(data_dir().AppendASCII("good.crx"), INSTALL_NEW);
3990  TerminateExtension(good_crx);
3991  EXPECT_TRUE(registry()->GetExtensionById(
3992      good_crx, extensions::ExtensionRegistry::TERMINATED));
3993
3994  // Disable it.
3995  service()->DisableExtension(good_crx, Extension::DISABLE_USER_ACTION);
3996
3997  EXPECT_FALSE(registry()->GetExtensionById(
3998      good_crx, extensions::ExtensionRegistry::TERMINATED));
3999  EXPECT_TRUE(service()->GetExtensionById(good_crx, true));
4000
4001  EXPECT_EQ(0u, registry()->enabled_extensions().size());
4002  EXPECT_EQ(1u, registry()->disabled_extensions().size());
4003  EXPECT_EQ(0u, registry()->terminated_extensions().size());
4004  EXPECT_EQ(0u, registry()->blacklisted_extensions().size());
4005}
4006
4007// Tests disabling all extensions (simulating --disable-extensions flag).
4008TEST_F(ExtensionServiceTest, DisableAllExtensions) {
4009  InitializeEmptyExtensionService();
4010
4011  base::FilePath path = data_dir().AppendASCII("good.crx");
4012  InstallCRX(path, INSTALL_NEW);
4013
4014  EXPECT_EQ(1u, registry()->enabled_extensions().size());
4015  EXPECT_EQ(0u, registry()->disabled_extensions().size());
4016
4017  // Disable extensions.
4018  service()->set_extensions_enabled(false);
4019  service()->ReloadExtensionsForTest();
4020
4021  // There shouldn't be extensions in either list.
4022  EXPECT_EQ(0u, registry()->enabled_extensions().size());
4023  EXPECT_EQ(0u, registry()->disabled_extensions().size());
4024
4025  // This shouldn't do anything when all extensions are disabled.
4026  service()->EnableExtension(good_crx);
4027  service()->ReloadExtensionsForTest();
4028
4029  // There still shouldn't be extensions in either list.
4030  EXPECT_EQ(0u, registry()->enabled_extensions().size());
4031  EXPECT_EQ(0u, registry()->disabled_extensions().size());
4032
4033  // And then re-enable the extensions.
4034  service()->set_extensions_enabled(true);
4035  service()->ReloadExtensionsForTest();
4036
4037  EXPECT_EQ(1u, registry()->enabled_extensions().size());
4038  EXPECT_EQ(0u, registry()->disabled_extensions().size());
4039}
4040
4041// Tests reloading extensions.
4042TEST_F(ExtensionServiceTest, ReloadExtensions) {
4043  InitializeEmptyExtensionService();
4044
4045  // Simple extension that should install without error.
4046  base::FilePath path = data_dir().AppendASCII("good.crx");
4047  InstallCRX(path, INSTALL_NEW,
4048             Extension::FROM_WEBSTORE | Extension::WAS_INSTALLED_BY_DEFAULT);
4049  const char* extension_id = good_crx;
4050  service()->DisableExtension(extension_id, Extension::DISABLE_USER_ACTION);
4051
4052  EXPECT_EQ(0u, registry()->enabled_extensions().size());
4053  EXPECT_EQ(1u, registry()->disabled_extensions().size());
4054
4055  service()->ReloadExtensionsForTest();
4056
4057  // The creation flags should not change when reloading the extension.
4058  const Extension* extension = service()->GetExtensionById(good_crx, true);
4059  EXPECT_TRUE(extension->from_webstore());
4060  EXPECT_TRUE(extension->was_installed_by_default());
4061  EXPECT_FALSE(extension->from_bookmark());
4062
4063  // Extension counts shouldn't change.
4064  EXPECT_EQ(0u, registry()->enabled_extensions().size());
4065  EXPECT_EQ(1u, registry()->disabled_extensions().size());
4066
4067  service()->EnableExtension(extension_id);
4068
4069  EXPECT_EQ(1u, registry()->enabled_extensions().size());
4070  EXPECT_EQ(0u, registry()->disabled_extensions().size());
4071
4072  // Need to clear |loaded_| manually before reloading as the
4073  // EnableExtension() call above inserted into it and
4074  // UnloadAllExtensions() doesn't send out notifications.
4075  loaded_.clear();
4076  service()->ReloadExtensionsForTest();
4077
4078  // Extension counts shouldn't change.
4079  EXPECT_EQ(1u, registry()->enabled_extensions().size());
4080  EXPECT_EQ(0u, registry()->disabled_extensions().size());
4081}
4082
4083// Tests reloading an extension.
4084TEST_F(ExtensionServiceTest, ReloadExtension) {
4085  InitializeEmptyExtensionService();
4086  InitializeProcessManager();
4087
4088  // Simple extension that should install without error.
4089  const char* extension_id = "behllobkkfkfnphdnhnkndlbkcpglgmj";
4090  base::FilePath ext = data_dir()
4091                           .AppendASCII("good")
4092                           .AppendASCII("Extensions")
4093                           .AppendASCII(extension_id)
4094                           .AppendASCII("1.0.0.0");
4095  extensions::UnpackedInstaller::Create(service())->Load(ext);
4096  base::RunLoop().RunUntilIdle();
4097
4098  EXPECT_EQ(1u, registry()->enabled_extensions().size());
4099  EXPECT_EQ(0u, registry()->disabled_extensions().size());
4100
4101  service()->ReloadExtension(extension_id);
4102
4103  // Extension should be disabled now, waiting to be reloaded.
4104  EXPECT_EQ(0u, registry()->enabled_extensions().size());
4105  EXPECT_EQ(1u, registry()->disabled_extensions().size());
4106  EXPECT_EQ(Extension::DISABLE_RELOAD,
4107            ExtensionPrefs::Get(profile())->GetDisableReasons(extension_id));
4108
4109  // Reloading again should not crash.
4110  service()->ReloadExtension(extension_id);
4111
4112  // Finish reloading
4113  base::RunLoop().RunUntilIdle();
4114
4115  // Extension should be enabled again.
4116  EXPECT_EQ(1u, registry()->enabled_extensions().size());
4117  EXPECT_EQ(0u, registry()->disabled_extensions().size());
4118}
4119
4120TEST_F(ExtensionServiceTest, UninstallExtension) {
4121  InitializeEmptyExtensionService();
4122  InstallCRX(data_dir().AppendASCII("good.crx"), INSTALL_NEW);
4123  EXPECT_EQ(1u, registry()->enabled_extensions().size());
4124  UninstallExtension(good_crx, false);
4125  EXPECT_EQ(0u, registry()->enabled_extensions().size());
4126  EXPECT_EQ(UnloadedExtensionInfo::REASON_UNINSTALL, unloaded_reason_);
4127}
4128
4129TEST_F(ExtensionServiceTest, UninstallTerminatedExtension) {
4130  InitializeEmptyExtensionService();
4131  InstallCRX(data_dir().AppendASCII("good.crx"), INSTALL_NEW);
4132  TerminateExtension(good_crx);
4133  UninstallExtension(good_crx, false);
4134  EXPECT_EQ(UnloadedExtensionInfo::REASON_TERMINATE, unloaded_reason_);
4135}
4136
4137// Tests the uninstaller helper.
4138TEST_F(ExtensionServiceTest, UninstallExtensionHelper) {
4139  InitializeEmptyExtensionService();
4140  InstallCRX(data_dir().AppendASCII("good.crx"), INSTALL_NEW);
4141  UninstallExtension(good_crx, true);
4142  EXPECT_EQ(UnloadedExtensionInfo::REASON_UNINSTALL, unloaded_reason_);
4143}
4144
4145TEST_F(ExtensionServiceTest, UninstallExtensionHelperTerminated) {
4146  InitializeEmptyExtensionService();
4147  InstallCRX(data_dir().AppendASCII("good.crx"), INSTALL_NEW);
4148  TerminateExtension(good_crx);
4149  UninstallExtension(good_crx, true);
4150  EXPECT_EQ(UnloadedExtensionInfo::REASON_TERMINATE, unloaded_reason_);
4151}
4152
4153// An extension disabled because of unsupported requirements should re-enabled
4154// if updated to a version with supported requirements as long as there are no
4155// other disable reasons.
4156TEST_F(ExtensionServiceTest, UpgradingRequirementsEnabled) {
4157  InitializeEmptyExtensionService();
4158  BlackListWebGL();
4159
4160  base::FilePath path = data_dir().AppendASCII("requirements");
4161  base::FilePath pem_path =
4162      data_dir().AppendASCII("requirements").AppendASCII("v1_good.pem");
4163  const Extension* extension_v1 = PackAndInstallCRX(path.AppendASCII("v1_good"),
4164                                                    pem_path,
4165                                                    INSTALL_NEW);
4166  std::string id = extension_v1->id();
4167  EXPECT_TRUE(service()->IsExtensionEnabled(id));
4168
4169  base::FilePath v2_bad_requirements_crx = GetTemporaryFile();
4170
4171  PackCRX(path.AppendASCII("v2_bad_requirements"),
4172          pem_path,
4173          v2_bad_requirements_crx);
4174  UpdateExtension(id, v2_bad_requirements_crx, INSTALLED);
4175  EXPECT_FALSE(service()->IsExtensionEnabled(id));
4176
4177  base::FilePath v3_good_crx = GetTemporaryFile();
4178
4179  PackCRX(path.AppendASCII("v3_good"), pem_path, v3_good_crx);
4180  UpdateExtension(id, v3_good_crx, ENABLED);
4181  EXPECT_TRUE(service()->IsExtensionEnabled(id));
4182}
4183
4184// Extensions disabled through user action should stay disabled.
4185TEST_F(ExtensionServiceTest, UpgradingRequirementsDisabled) {
4186  InitializeEmptyExtensionService();
4187  BlackListWebGL();
4188
4189  base::FilePath path = data_dir().AppendASCII("requirements");
4190  base::FilePath pem_path =
4191      data_dir().AppendASCII("requirements").AppendASCII("v1_good.pem");
4192  const Extension* extension_v1 = PackAndInstallCRX(path.AppendASCII("v1_good"),
4193                                                    pem_path,
4194                                                    INSTALL_NEW);
4195  std::string id = extension_v1->id();
4196  service()->DisableExtension(id, Extension::DISABLE_USER_ACTION);
4197  EXPECT_FALSE(service()->IsExtensionEnabled(id));
4198
4199  base::FilePath v2_bad_requirements_crx = GetTemporaryFile();
4200
4201  PackCRX(path.AppendASCII("v2_bad_requirements"),
4202          pem_path,
4203          v2_bad_requirements_crx);
4204  UpdateExtension(id, v2_bad_requirements_crx, INSTALLED);
4205  EXPECT_FALSE(service()->IsExtensionEnabled(id));
4206
4207  base::FilePath v3_good_crx = GetTemporaryFile();
4208
4209  PackCRX(path.AppendASCII("v3_good"), pem_path, v3_good_crx);
4210  UpdateExtension(id, v3_good_crx, INSTALLED);
4211  EXPECT_FALSE(service()->IsExtensionEnabled(id));
4212}
4213
4214// The extension should not re-enabled because it was disabled from a
4215// permission increase.
4216TEST_F(ExtensionServiceTest, UpgradingRequirementsPermissions) {
4217  InitializeEmptyExtensionService();
4218  BlackListWebGL();
4219
4220  base::FilePath path = data_dir().AppendASCII("requirements");
4221  base::FilePath pem_path =
4222      data_dir().AppendASCII("requirements").AppendASCII("v1_good.pem");
4223  const Extension* extension_v1 = PackAndInstallCRX(path.AppendASCII("v1_good"),
4224                                                    pem_path,
4225                                                    INSTALL_NEW);
4226  std::string id = extension_v1->id();
4227  EXPECT_TRUE(service()->IsExtensionEnabled(id));
4228
4229  base::FilePath v2_bad_requirements_and_permissions_crx = GetTemporaryFile();
4230
4231  PackCRX(path.AppendASCII("v2_bad_requirements_and_permissions"),
4232          pem_path,
4233          v2_bad_requirements_and_permissions_crx);
4234  UpdateExtension(id, v2_bad_requirements_and_permissions_crx, INSTALLED);
4235  EXPECT_FALSE(service()->IsExtensionEnabled(id));
4236
4237  base::FilePath v3_bad_permissions_crx = GetTemporaryFile();
4238
4239  PackCRX(path.AppendASCII("v3_bad_permissions"),
4240          pem_path,
4241          v3_bad_permissions_crx);
4242  UpdateExtension(id, v3_bad_permissions_crx, INSTALLED);
4243  EXPECT_FALSE(service()->IsExtensionEnabled(id));
4244}
4245
4246// Unpacked extensions are not allowed to be installed if they have unsupported
4247// requirements.
4248TEST_F(ExtensionServiceTest, UnpackedRequirements) {
4249  InitializeEmptyExtensionService();
4250  BlackListWebGL();
4251
4252  base::FilePath path =
4253      data_dir().AppendASCII("requirements").AppendASCII("v2_bad_requirements");
4254  extensions::UnpackedInstaller::Create(service())->Load(path);
4255  base::RunLoop().RunUntilIdle();
4256  EXPECT_EQ(1u, GetErrors().size());
4257  EXPECT_EQ(0u, registry()->enabled_extensions().size());
4258}
4259
4260class ExtensionCookieCallback {
4261 public:
4262  ExtensionCookieCallback()
4263    : result_(false),
4264      weak_factory_(base::MessageLoop::current()) {}
4265
4266  void SetCookieCallback(bool result) {
4267    base::MessageLoop::current()->PostTask(FROM_HERE,
4268        base::Bind(&base::MessageLoop::Quit, weak_factory_.GetWeakPtr()));
4269    result_ = result;
4270  }
4271
4272  void GetAllCookiesCallback(const net::CookieList& list) {
4273    base::MessageLoop::current()->PostTask(FROM_HERE,
4274        base::Bind(&base::MessageLoop::Quit, weak_factory_.GetWeakPtr()));
4275    list_ = list;
4276  }
4277  net::CookieList list_;
4278  bool result_;
4279  base::WeakPtrFactory<base::MessageLoop> weak_factory_;
4280};
4281
4282// Verifies extension state is removed upon uninstall.
4283TEST_F(ExtensionServiceTest, ClearExtensionData) {
4284  InitializeEmptyExtensionService();
4285  ExtensionCookieCallback callback;
4286
4287  // Load a test extension.
4288  base::FilePath path = data_dir();
4289  path = path.AppendASCII("good.crx");
4290  const Extension* extension = InstallCRX(path, INSTALL_NEW);
4291  ASSERT_TRUE(extension);
4292  GURL ext_url(extension->url());
4293  std::string origin_id = storage::GetIdentifierFromOrigin(ext_url);
4294
4295  // Set a cookie for the extension.
4296  net::CookieMonster* cookie_monster = profile()
4297                                           ->GetRequestContextForExtensions()
4298                                           ->GetURLRequestContext()
4299                                           ->cookie_store()
4300                                           ->GetCookieMonster();
4301  ASSERT_TRUE(cookie_monster);
4302  net::CookieOptions options;
4303  cookie_monster->SetCookieWithOptionsAsync(
4304       ext_url, "dummy=value", options,
4305       base::Bind(&ExtensionCookieCallback::SetCookieCallback,
4306                  base::Unretained(&callback)));
4307  base::RunLoop().RunUntilIdle();
4308  EXPECT_TRUE(callback.result_);
4309
4310  cookie_monster->GetAllCookiesForURLAsync(
4311      ext_url,
4312      base::Bind(&ExtensionCookieCallback::GetAllCookiesCallback,
4313                 base::Unretained(&callback)));
4314  base::RunLoop().RunUntilIdle();
4315  EXPECT_EQ(1U, callback.list_.size());
4316
4317  // Open a database.
4318  storage::DatabaseTracker* db_tracker =
4319      BrowserContext::GetDefaultStoragePartition(profile())
4320          ->GetDatabaseTracker();
4321  base::string16 db_name = base::UTF8ToUTF16("db");
4322  base::string16 description = base::UTF8ToUTF16("db_description");
4323  int64 size;
4324  db_tracker->DatabaseOpened(origin_id, db_name, description, 1, &size);
4325  db_tracker->DatabaseClosed(origin_id, db_name);
4326  std::vector<storage::OriginInfo> origins;
4327  db_tracker->GetAllOriginsInfo(&origins);
4328  EXPECT_EQ(1U, origins.size());
4329  EXPECT_EQ(origin_id, origins[0].GetOriginIdentifier());
4330
4331  // Create local storage. We only simulate this by creating the backing files.
4332  // Note: This test depends on details of how the dom_storage library
4333  // stores data in the host file system.
4334  base::FilePath lso_dir_path =
4335      profile()->GetPath().AppendASCII("Local Storage");
4336  base::FilePath lso_file_path = lso_dir_path.AppendASCII(origin_id)
4337      .AddExtension(FILE_PATH_LITERAL(".localstorage"));
4338  EXPECT_TRUE(base::CreateDirectory(lso_dir_path));
4339  EXPECT_EQ(0, base::WriteFile(lso_file_path, NULL, 0));
4340  EXPECT_TRUE(base::PathExists(lso_file_path));
4341
4342  // Create indexed db. Similarly, it is enough to only simulate this by
4343  // creating the directory on the disk.
4344  IndexedDBContext* idb_context = BrowserContext::GetDefaultStoragePartition(
4345                                      profile())->GetIndexedDBContext();
4346  idb_context->SetTaskRunnerForTesting(
4347      base::MessageLoop::current()->message_loop_proxy().get());
4348  base::FilePath idb_path = idb_context->GetFilePathForTesting(origin_id);
4349  EXPECT_TRUE(base::CreateDirectory(idb_path));
4350  EXPECT_TRUE(base::DirectoryExists(idb_path));
4351
4352  // Uninstall the extension.
4353  base::RunLoop run_loop;
4354  ASSERT_TRUE(
4355      service()->UninstallExtension(good_crx,
4356                                    extensions::UNINSTALL_REASON_FOR_TESTING,
4357                                    run_loop.QuitClosure(),
4358                                    NULL));
4359  // The data deletion happens on the IO thread.
4360  run_loop.Run();
4361
4362  // Check that the cookie is gone.
4363  cookie_monster->GetAllCookiesForURLAsync(
4364       ext_url,
4365       base::Bind(&ExtensionCookieCallback::GetAllCookiesCallback,
4366                  base::Unretained(&callback)));
4367  base::RunLoop().RunUntilIdle();
4368  EXPECT_EQ(0U, callback.list_.size());
4369
4370  // The database should have vanished as well.
4371  origins.clear();
4372  db_tracker->GetAllOriginsInfo(&origins);
4373  EXPECT_EQ(0U, origins.size());
4374
4375  // Check that the LSO file has been removed.
4376  EXPECT_FALSE(base::PathExists(lso_file_path));
4377
4378  // Check if the indexed db has disappeared too.
4379  EXPECT_FALSE(base::DirectoryExists(idb_path));
4380}
4381
4382// Verifies app state is removed upon uninstall.
4383TEST_F(ExtensionServiceTest, ClearAppData) {
4384  InitializeEmptyExtensionService();
4385  ExtensionCookieCallback callback;
4386
4387  int pref_count = 0;
4388
4389  // Install app1 with unlimited storage.
4390  const Extension* extension =
4391      PackAndInstallCRX(data_dir().AppendASCII("app1"), INSTALL_NEW);
4392  ValidatePrefKeyCount(++pref_count);
4393  ASSERT_EQ(1u, registry()->enabled_extensions().size());
4394  const std::string id1 = extension->id();
4395  EXPECT_TRUE(extension->permissions_data()->HasAPIPermission(
4396      APIPermission::kUnlimitedStorage));
4397  const GURL origin1(
4398      extensions::AppLaunchInfo::GetFullLaunchURL(extension).GetOrigin());
4399  EXPECT_TRUE(profile()->GetExtensionSpecialStoragePolicy()->IsStorageUnlimited(
4400      origin1));
4401  std::string origin_id = storage::GetIdentifierFromOrigin(origin1);
4402
4403  // Install app2 from the same origin with unlimited storage.
4404  extension = PackAndInstallCRX(data_dir().AppendASCII("app2"), INSTALL_NEW);
4405  ValidatePrefKeyCount(++pref_count);
4406  ASSERT_EQ(2u, registry()->enabled_extensions().size());
4407  const std::string id2 = extension->id();
4408  EXPECT_TRUE(extension->permissions_data()->HasAPIPermission(
4409      APIPermission::kUnlimitedStorage));
4410  EXPECT_TRUE(extension->web_extent().MatchesURL(
4411      extensions::AppLaunchInfo::GetFullLaunchURL(extension)));
4412  const GURL origin2(
4413      extensions::AppLaunchInfo::GetFullLaunchURL(extension).GetOrigin());
4414  EXPECT_EQ(origin1, origin2);
4415  EXPECT_TRUE(profile()->GetExtensionSpecialStoragePolicy()->IsStorageUnlimited(
4416      origin2));
4417
4418  // Set a cookie for the extension.
4419  net::CookieMonster* cookie_monster = profile()
4420                                           ->GetRequestContext()
4421                                           ->GetURLRequestContext()
4422                                           ->cookie_store()
4423                                           ->GetCookieMonster();
4424  ASSERT_TRUE(cookie_monster);
4425  net::CookieOptions options;
4426  cookie_monster->SetCookieWithOptionsAsync(
4427       origin1, "dummy=value", options,
4428       base::Bind(&ExtensionCookieCallback::SetCookieCallback,
4429                  base::Unretained(&callback)));
4430  base::RunLoop().RunUntilIdle();
4431  EXPECT_TRUE(callback.result_);
4432
4433  cookie_monster->GetAllCookiesForURLAsync(
4434      origin1,
4435      base::Bind(&ExtensionCookieCallback::GetAllCookiesCallback,
4436                 base::Unretained(&callback)));
4437  base::RunLoop().RunUntilIdle();
4438  EXPECT_EQ(1U, callback.list_.size());
4439
4440  // Open a database.
4441  storage::DatabaseTracker* db_tracker =
4442      BrowserContext::GetDefaultStoragePartition(profile())
4443          ->GetDatabaseTracker();
4444  base::string16 db_name = base::UTF8ToUTF16("db");
4445  base::string16 description = base::UTF8ToUTF16("db_description");
4446  int64 size;
4447  db_tracker->DatabaseOpened(origin_id, db_name, description, 1, &size);
4448  db_tracker->DatabaseClosed(origin_id, db_name);
4449  std::vector<storage::OriginInfo> origins;
4450  db_tracker->GetAllOriginsInfo(&origins);
4451  EXPECT_EQ(1U, origins.size());
4452  EXPECT_EQ(origin_id, origins[0].GetOriginIdentifier());
4453
4454  // Create local storage. We only simulate this by creating the backing files.
4455  // Note: This test depends on details of how the dom_storage library
4456  // stores data in the host file system.
4457  base::FilePath lso_dir_path =
4458      profile()->GetPath().AppendASCII("Local Storage");
4459  base::FilePath lso_file_path = lso_dir_path.AppendASCII(origin_id)
4460      .AddExtension(FILE_PATH_LITERAL(".localstorage"));
4461  EXPECT_TRUE(base::CreateDirectory(lso_dir_path));
4462  EXPECT_EQ(0, base::WriteFile(lso_file_path, NULL, 0));
4463  EXPECT_TRUE(base::PathExists(lso_file_path));
4464
4465  // Create indexed db. Similarly, it is enough to only simulate this by
4466  // creating the directory on the disk.
4467  IndexedDBContext* idb_context = BrowserContext::GetDefaultStoragePartition(
4468                                      profile())->GetIndexedDBContext();
4469  idb_context->SetTaskRunnerForTesting(
4470      base::MessageLoop::current()->message_loop_proxy().get());
4471  base::FilePath idb_path = idb_context->GetFilePathForTesting(origin_id);
4472  EXPECT_TRUE(base::CreateDirectory(idb_path));
4473  EXPECT_TRUE(base::DirectoryExists(idb_path));
4474
4475  // Uninstall one of them, unlimited storage should still be granted
4476  // to the origin.
4477  UninstallExtension(id1, false);
4478  EXPECT_EQ(1u, registry()->enabled_extensions().size());
4479  EXPECT_TRUE(profile()->GetExtensionSpecialStoragePolicy()->IsStorageUnlimited(
4480      origin1));
4481
4482  // Check that the cookie is still there.
4483  cookie_monster->GetAllCookiesForURLAsync(
4484       origin1,
4485       base::Bind(&ExtensionCookieCallback::GetAllCookiesCallback,
4486                  base::Unretained(&callback)));
4487  base::RunLoop().RunUntilIdle();
4488  EXPECT_EQ(1U, callback.list_.size());
4489
4490  // Now uninstall the other. Storage should be cleared for the apps.
4491  UninstallExtension(id2, false);
4492  EXPECT_EQ(0u, registry()->enabled_extensions().size());
4493  EXPECT_FALSE(
4494      profile()->GetExtensionSpecialStoragePolicy()->IsStorageUnlimited(
4495          origin1));
4496
4497  // Check that the cookie is gone.
4498  cookie_monster->GetAllCookiesForURLAsync(
4499       origin1,
4500       base::Bind(&ExtensionCookieCallback::GetAllCookiesCallback,
4501                  base::Unretained(&callback)));
4502  base::RunLoop().RunUntilIdle();
4503  EXPECT_EQ(0U, callback.list_.size());
4504
4505  // The database should have vanished as well.
4506  origins.clear();
4507  db_tracker->GetAllOriginsInfo(&origins);
4508  EXPECT_EQ(0U, origins.size());
4509
4510  // Check that the LSO file has been removed.
4511  EXPECT_FALSE(base::PathExists(lso_file_path));
4512
4513  // Check if the indexed db has disappeared too.
4514  EXPECT_FALSE(base::DirectoryExists(idb_path));
4515}
4516
4517// Tests loading single extensions (like --load-extension)
4518// Flaky crashes. http://crbug.com/231806
4519TEST_F(ExtensionServiceTest, DISABLED_LoadExtension) {
4520  InitializeEmptyExtensionService();
4521
4522  base::FilePath ext1 = data_dir()
4523                            .AppendASCII("good")
4524                            .AppendASCII("Extensions")
4525                            .AppendASCII("behllobkkfkfnphdnhnkndlbkcpglgmj")
4526                            .AppendASCII("1.0.0.0");
4527  extensions::UnpackedInstaller::Create(service())->Load(ext1);
4528  base::RunLoop().RunUntilIdle();
4529  EXPECT_EQ(0u, GetErrors().size());
4530  ASSERT_EQ(1u, loaded_.size());
4531  EXPECT_EQ(Manifest::UNPACKED, loaded_[0]->location());
4532  EXPECT_EQ(1u, registry()->enabled_extensions().size());
4533
4534  ValidatePrefKeyCount(1);
4535
4536  base::FilePath no_manifest =
4537      data_dir()
4538          .AppendASCII("bad")
4539          // .AppendASCII("Extensions")
4540          .AppendASCII("cccccccccccccccccccccccccccccccc")
4541          .AppendASCII("1");
4542  extensions::UnpackedInstaller::Create(service())->Load(no_manifest);
4543  base::RunLoop().RunUntilIdle();
4544  EXPECT_EQ(1u, GetErrors().size());
4545  ASSERT_EQ(1u, loaded_.size());
4546  EXPECT_EQ(1u, registry()->enabled_extensions().size());
4547
4548  // Test uninstall.
4549  std::string id = loaded_[0]->id();
4550  EXPECT_FALSE(unloaded_id_.length());
4551  service()->UninstallExtension(id,
4552                                extensions::UNINSTALL_REASON_FOR_TESTING,
4553                                base::Bind(&base::DoNothing),
4554                                NULL);
4555  base::RunLoop().RunUntilIdle();
4556  EXPECT_EQ(id, unloaded_id_);
4557  ASSERT_EQ(0u, loaded_.size());
4558  EXPECT_EQ(0u, registry()->enabled_extensions().size());
4559}
4560
4561// Tests that we generate IDs when they are not specified in the manifest for
4562// --load-extension.
4563TEST_F(ExtensionServiceTest, GenerateID) {
4564  InitializeEmptyExtensionService();
4565
4566  base::FilePath no_id_ext = data_dir().AppendASCII("no_id");
4567  extensions::UnpackedInstaller::Create(service())->Load(no_id_ext);
4568  base::RunLoop().RunUntilIdle();
4569  EXPECT_EQ(0u, GetErrors().size());
4570  ASSERT_EQ(1u, loaded_.size());
4571  ASSERT_TRUE(crx_file::id_util::IdIsValid(loaded_[0]->id()));
4572  EXPECT_EQ(loaded_[0]->location(), Manifest::UNPACKED);
4573
4574  ValidatePrefKeyCount(1);
4575
4576  std::string previous_id = loaded_[0]->id();
4577
4578  // If we reload the same path, we should get the same extension ID.
4579  extensions::UnpackedInstaller::Create(service())->Load(no_id_ext);
4580  base::RunLoop().RunUntilIdle();
4581  ASSERT_EQ(1u, loaded_.size());
4582  ASSERT_EQ(previous_id, loaded_[0]->id());
4583}
4584
4585TEST_F(ExtensionServiceTest, UnpackedValidatesLocales) {
4586  InitializeEmptyExtensionService();
4587
4588  base::FilePath bad_locale =
4589      data_dir().AppendASCII("unpacked").AppendASCII("bad_messages_file");
4590  extensions::UnpackedInstaller::Create(service())->Load(bad_locale);
4591  base::RunLoop().RunUntilIdle();
4592  EXPECT_EQ(1u, GetErrors().size());
4593  base::FilePath ms_messages_file = bad_locale.AppendASCII("_locales")
4594                                              .AppendASCII("ms")
4595                                              .AppendASCII("messages.json");
4596  EXPECT_THAT(base::UTF16ToUTF8(GetErrors()[0]), testing::AllOf(
4597       testing::HasSubstr(
4598           base::UTF16ToUTF8(ms_messages_file.LossyDisplayName())),
4599       testing::HasSubstr("Dictionary keys must be quoted.")));
4600  ASSERT_EQ(0u, loaded_.size());
4601}
4602
4603void ExtensionServiceTest::TestExternalProvider(
4604    MockExtensionProvider* provider, Manifest::Location location) {
4605  // Verify that starting with no providers loads no extensions.
4606  service()->Init();
4607  ASSERT_EQ(0u, loaded_.size());
4608
4609  provider->set_visit_count(0);
4610
4611  // Register a test extension externally using the mock registry provider.
4612  base::FilePath source_path = data_dir().AppendASCII("good.crx");
4613
4614  // Add the extension.
4615  provider->UpdateOrAddExtension(good_crx, "1.0.0.0", source_path);
4616
4617  // Reloading extensions should find our externally registered extension
4618  // and install it.
4619  content::WindowedNotificationObserver observer(
4620      extensions::NOTIFICATION_CRX_INSTALLER_DONE,
4621      content::NotificationService::AllSources());
4622  service()->CheckForExternalUpdates();
4623  observer.Wait();
4624
4625  ASSERT_EQ(0u, GetErrors().size());
4626  ASSERT_EQ(1u, loaded_.size());
4627  ASSERT_EQ(location, loaded_[0]->location());
4628  ASSERT_EQ("1.0.0.0", loaded_[0]->version()->GetString());
4629  ValidatePrefKeyCount(1);
4630  ValidateIntegerPref(good_crx, "state", Extension::ENABLED);
4631  ValidateIntegerPref(good_crx, "location", location);
4632
4633  // Reload extensions without changing anything. The extension should be
4634  // loaded again.
4635  loaded_.clear();
4636  service()->ReloadExtensionsForTest();
4637  base::RunLoop().RunUntilIdle();
4638  ASSERT_EQ(0u, GetErrors().size());
4639  ASSERT_EQ(1u, loaded_.size());
4640  ValidatePrefKeyCount(1);
4641  ValidateIntegerPref(good_crx, "state", Extension::ENABLED);
4642  ValidateIntegerPref(good_crx, "location", location);
4643
4644  // Now update the extension with a new version. We should get upgraded.
4645  source_path = source_path.DirName().AppendASCII("good2.crx");
4646  provider->UpdateOrAddExtension(good_crx, "1.0.0.1", source_path);
4647
4648  loaded_.clear();
4649  content::WindowedNotificationObserver observer_2(
4650      extensions::NOTIFICATION_CRX_INSTALLER_DONE,
4651      content::NotificationService::AllSources());
4652  service()->CheckForExternalUpdates();
4653  observer_2.Wait();
4654  ASSERT_EQ(0u, GetErrors().size());
4655  ASSERT_EQ(1u, loaded_.size());
4656  ASSERT_EQ("1.0.0.1", loaded_[0]->version()->GetString());
4657  ValidatePrefKeyCount(1);
4658  ValidateIntegerPref(good_crx, "state", Extension::ENABLED);
4659  ValidateIntegerPref(good_crx, "location", location);
4660
4661  // Uninstall the extension and reload. Nothing should happen because the
4662  // preference should prevent us from reinstalling.
4663  std::string id = loaded_[0]->id();
4664  bool no_uninstall =
4665      GetManagementPolicy()->MustRemainEnabled(loaded_[0].get(), NULL);
4666  service()->UninstallExtension(id,
4667                                extensions::UNINSTALL_REASON_FOR_TESTING,
4668                                base::Bind(&base::DoNothing),
4669                                NULL);
4670  base::RunLoop().RunUntilIdle();
4671
4672  base::FilePath install_path = extensions_install_dir().AppendASCII(id);
4673  if (no_uninstall) {
4674    // Policy controlled extensions should not have been touched by uninstall.
4675    ASSERT_TRUE(base::PathExists(install_path));
4676  } else {
4677    // The extension should also be gone from the install directory.
4678    ASSERT_FALSE(base::PathExists(install_path));
4679    loaded_.clear();
4680    service()->CheckForExternalUpdates();
4681    base::RunLoop().RunUntilIdle();
4682    ASSERT_EQ(0u, loaded_.size());
4683    ValidatePrefKeyCount(1);
4684    ValidateIntegerPref(good_crx, "state",
4685                        Extension::EXTERNAL_EXTENSION_UNINSTALLED);
4686    ValidateIntegerPref(good_crx, "location", location);
4687
4688    // Now clear the preference and reinstall.
4689    SetPrefInteg(good_crx, "state", Extension::ENABLED);
4690
4691    loaded_.clear();
4692    content::WindowedNotificationObserver observer(
4693        extensions::NOTIFICATION_CRX_INSTALLER_DONE,
4694        content::NotificationService::AllSources());
4695    service()->CheckForExternalUpdates();
4696    observer.Wait();
4697    ASSERT_EQ(1u, loaded_.size());
4698  }
4699  ValidatePrefKeyCount(1);
4700  ValidateIntegerPref(good_crx, "state", Extension::ENABLED);
4701  ValidateIntegerPref(good_crx, "location", location);
4702
4703  if (GetManagementPolicy()->MustRemainEnabled(loaded_[0].get(), NULL)) {
4704    EXPECT_EQ(2, provider->visit_count());
4705  } else {
4706    // Now test an externally triggered uninstall (deleting the registry key or
4707    // the pref entry).
4708    provider->RemoveExtension(good_crx);
4709
4710    loaded_.clear();
4711    service()->OnExternalProviderReady(provider);
4712    base::RunLoop().RunUntilIdle();
4713    ASSERT_EQ(0u, loaded_.size());
4714    ValidatePrefKeyCount(0);
4715
4716    // The extension should also be gone from the install directory.
4717    ASSERT_FALSE(base::PathExists(install_path));
4718
4719    // Now test the case where user uninstalls and then the extension is removed
4720    // from the external provider.
4721    content::WindowedNotificationObserver observer(
4722        extensions::NOTIFICATION_CRX_INSTALLER_DONE,
4723        content::NotificationService::AllSources());
4724    provider->UpdateOrAddExtension(good_crx, "1.0.0.1", source_path);
4725    service()->CheckForExternalUpdates();
4726    observer.Wait();
4727
4728    ASSERT_EQ(1u, loaded_.size());
4729    ASSERT_EQ(0u, GetErrors().size());
4730
4731    // User uninstalls.
4732    loaded_.clear();
4733    service()->UninstallExtension(id,
4734                                  extensions::UNINSTALL_REASON_FOR_TESTING,
4735                                  base::Bind(&base::DoNothing),
4736                                  NULL);
4737    base::RunLoop().RunUntilIdle();
4738    ASSERT_EQ(0u, loaded_.size());
4739
4740    // Then remove the extension from the extension provider.
4741    provider->RemoveExtension(good_crx);
4742
4743    // Should still be at 0.
4744    loaded_.clear();
4745    extensions::InstalledLoader(service()).LoadAllExtensions();
4746    base::RunLoop().RunUntilIdle();
4747    ASSERT_EQ(0u, loaded_.size());
4748    ValidatePrefKeyCount(1);
4749
4750    EXPECT_EQ(5, provider->visit_count());
4751  }
4752}
4753
4754// Tests the external installation feature
4755#if defined(OS_WIN)