Home | History | Annotate | Download | only in extensions
      1 // Copyright (c) 2012 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 "base/files/file_path.h"
      6 #include "base/files/scoped_temp_dir.h"
      7 #include "base/run_loop.h"
      8 #include "base/threading/sequenced_worker_pool.h"
      9 #include "chrome/app/chrome_command_ids.h"
     10 #include "chrome/browser/extensions/extension_browsertest.h"
     11 #include "chrome/browser/extensions/extension_prefs.h"
     12 #include "chrome/browser/extensions/extension_service.h"
     13 #include "chrome/browser/extensions/extension_sync_service.h"
     14 #include "chrome/browser/extensions/updater/extension_updater.h"
     15 #include "chrome/browser/profiles/profile.h"
     16 #include "chrome/browser/ui/browser.h"
     17 #include "chrome/browser/ui/global_error/global_error.h"
     18 #include "chrome/browser/ui/global_error/global_error_service.h"
     19 #include "chrome/browser/ui/global_error/global_error_service_factory.h"
     20 #include "chrome/common/chrome_switches.h"
     21 #include "content/public/browser/browser_thread.h"
     22 #include "content/public/test/test_utils.h"
     23 #include "content/test/net/url_request_prepackaged_interceptor.h"
     24 #include "extensions/common/extension.h"
     25 #include "net/url_request/url_fetcher.h"
     26 
     27 using extensions::Extension;
     28 
     29 class ExtensionDisabledGlobalErrorTest : public ExtensionBrowserTest {
     30  protected:
     31   virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
     32     ExtensionBrowserTest::SetUpCommandLine(command_line);
     33     command_line->AppendSwitchASCII(switches::kAppsGalleryUpdateURL,
     34                                     "http://localhost/autoupdate/updates.xml");
     35   }
     36 
     37   virtual void SetUpOnMainThread() OVERRIDE {
     38     EXPECT_TRUE(scoped_temp_dir_.CreateUniqueTempDir());
     39     service_ = browser()->profile()->GetExtensionService();
     40     base::FilePath pem_path = test_data_dir_.
     41         AppendASCII("permissions_increase").AppendASCII("permissions.pem");
     42     path_v1_ = PackExtensionWithOptions(
     43         test_data_dir_.AppendASCII("permissions_increase").AppendASCII("v1"),
     44         scoped_temp_dir_.path().AppendASCII("permissions1.crx"),
     45         pem_path,
     46         base::FilePath());
     47     path_v2_ = PackExtensionWithOptions(
     48         test_data_dir_.AppendASCII("permissions_increase").AppendASCII("v2"),
     49         scoped_temp_dir_.path().AppendASCII("permissions2.crx"),
     50         pem_path,
     51         base::FilePath());
     52     path_v3_ = PackExtensionWithOptions(
     53         test_data_dir_.AppendASCII("permissions_increase").AppendASCII("v3"),
     54         scoped_temp_dir_.path().AppendASCII("permissions3.crx"),
     55         pem_path,
     56         base::FilePath());
     57   }
     58 
     59   // Returns the ExtensionDisabledGlobalError, if present.
     60   // Caution: currently only supports one error at a time.
     61   GlobalError* GetExtensionDisabledGlobalError() {
     62     return GlobalErrorServiceFactory::GetForProfile(browser()->profile())->
     63         GetGlobalErrorByMenuItemCommandID(IDC_EXTENSION_DISABLED_FIRST);
     64   }
     65 
     66   // Install the initial version, which should happen just fine.
     67   const Extension* InstallIncreasingPermissionExtensionV1() {
     68     size_t size_before = service_->extensions()->size();
     69     const Extension* extension = InstallExtension(path_v1_, 1);
     70     if (!extension)
     71       return NULL;
     72     if (service_->extensions()->size() != size_before + 1)
     73       return NULL;
     74     return extension;
     75   }
     76 
     77   // Upgrade to a version that wants more permissions. We should disable the
     78   // extension and prompt the user to reenable.
     79   const Extension* UpdateIncreasingPermissionExtension(
     80       const Extension* extension,
     81       const base::FilePath& crx_path,
     82       int expected_change) {
     83     size_t size_before = service_->extensions()->size();
     84     if (UpdateExtension(extension->id(), crx_path, expected_change))
     85       return NULL;
     86     content::BrowserThread::GetBlockingPool()->FlushForTesting();
     87     base::RunLoop().RunUntilIdle();
     88     EXPECT_EQ(size_before + expected_change, service_->extensions()->size());
     89     if (service_->disabled_extensions()->size() != 1u)
     90       return NULL;
     91 
     92     return service_->disabled_extensions()->begin()->get();
     93   }
     94 
     95   // Helper function to install an extension and upgrade it to a version
     96   // requiring additional permissions. Returns the new disabled Extension.
     97   const Extension* InstallAndUpdateIncreasingPermissionsExtension() {
     98     const Extension* extension = InstallIncreasingPermissionExtensionV1();
     99     extension = UpdateIncreasingPermissionExtension(extension, path_v2_, -1);
    100     return extension;
    101   }
    102 
    103   ExtensionService* service_;
    104   base::ScopedTempDir scoped_temp_dir_;
    105   base::FilePath path_v1_;
    106   base::FilePath path_v2_;
    107   base::FilePath path_v3_;
    108 };
    109 
    110 // Tests the process of updating an extension to one that requires higher
    111 // permissions, and accepting the permissions.
    112 IN_PROC_BROWSER_TEST_F(ExtensionDisabledGlobalErrorTest, AcceptPermissions) {
    113   const Extension* extension = InstallAndUpdateIncreasingPermissionsExtension();
    114   ASSERT_TRUE(extension);
    115   ASSERT_TRUE(GetExtensionDisabledGlobalError());
    116   const size_t size_before = service_->extensions()->size();
    117 
    118   service_->GrantPermissionsAndEnableExtension(extension);
    119   EXPECT_EQ(size_before + 1, service_->extensions()->size());
    120   EXPECT_EQ(0u, service_->disabled_extensions()->size());
    121   ASSERT_FALSE(GetExtensionDisabledGlobalError());
    122 }
    123 
    124 // Tests uninstalling an extension that was disabled due to higher permissions.
    125 IN_PROC_BROWSER_TEST_F(ExtensionDisabledGlobalErrorTest, Uninstall) {
    126   const Extension* extension = InstallAndUpdateIncreasingPermissionsExtension();
    127   ASSERT_TRUE(extension);
    128   ASSERT_TRUE(GetExtensionDisabledGlobalError());
    129   const size_t size_before = service_->extensions()->size();
    130 
    131   UninstallExtension(extension->id());
    132   EXPECT_EQ(size_before, service_->extensions()->size());
    133   EXPECT_EQ(0u, service_->disabled_extensions()->size());
    134   ASSERT_FALSE(GetExtensionDisabledGlobalError());
    135 }
    136 
    137 // Tests that no error appears if the user disabled the extension.
    138 IN_PROC_BROWSER_TEST_F(ExtensionDisabledGlobalErrorTest, UserDisabled) {
    139   const Extension* extension = InstallIncreasingPermissionExtensionV1();
    140   DisableExtension(extension->id());
    141   extension = UpdateIncreasingPermissionExtension(extension, path_v2_, 0);
    142   ASSERT_FALSE(GetExtensionDisabledGlobalError());
    143 }
    144 
    145 // Test that no error appears if the disable reason is unknown
    146 // (but probably was by the user).
    147 IN_PROC_BROWSER_TEST_F(ExtensionDisabledGlobalErrorTest,
    148                        UnknownReasonSamePermissions) {
    149   const Extension* extension = InstallIncreasingPermissionExtensionV1();
    150   DisableExtension(extension->id());
    151   // Clear disable reason to simulate legacy disables.
    152   service_->extension_prefs()->ClearDisableReasons(extension->id());
    153   // Upgrade to version 2. Infer from version 1 having the same permissions
    154   // granted by the user that it was disabled by the user.
    155   extension = UpdateIncreasingPermissionExtension(extension, path_v2_, 0);
    156   ASSERT_TRUE(extension);
    157   ASSERT_FALSE(GetExtensionDisabledGlobalError());
    158 }
    159 
    160 // Test that an error appears if the disable reason is unknown
    161 // (but probably was for increased permissions).
    162 IN_PROC_BROWSER_TEST_F(ExtensionDisabledGlobalErrorTest,
    163                        UnknownReasonHigherPermissions) {
    164   const Extension* extension = InstallAndUpdateIncreasingPermissionsExtension();
    165   // Clear disable reason to simulate legacy disables.
    166   service_->extension_prefs()->ClearDisableReasons(extension->id());
    167   // We now have version 2 but only accepted permissions for version 1.
    168   GlobalError* error = GetExtensionDisabledGlobalError();
    169   ASSERT_TRUE(error);
    170   // Also, remove the upgrade error for version 2.
    171   GlobalErrorServiceFactory::GetForProfile(browser()->profile())->
    172       RemoveGlobalError(error);
    173   delete error;
    174   // Upgrade to version 3, with even higher permissions. Infer from
    175   // version 2 having higher-than-granted permissions that it was disabled
    176   // for permissions increase.
    177   extension = UpdateIncreasingPermissionExtension(extension, path_v3_, 0);
    178   ASSERT_TRUE(extension);
    179   ASSERT_TRUE(GetExtensionDisabledGlobalError());
    180 }
    181 
    182 // Test that an error appears if the extension gets disabled because a
    183 // version with higher permissions was installed by sync.
    184 IN_PROC_BROWSER_TEST_F(ExtensionDisabledGlobalErrorTest,
    185                        HigherPermissionsFromSync) {
    186   // Get data for extension v2 (disabled) into sync.
    187   const Extension* extension = InstallAndUpdateIncreasingPermissionsExtension();
    188   std::string extension_id = extension->id();
    189   ExtensionSyncService* sync_service = ExtensionSyncService::Get(
    190       browser()->profile());
    191   extensions::ExtensionSyncData sync_data =
    192       sync_service->GetExtensionSyncData(*extension);
    193   UninstallExtension(extension_id);
    194   extension = NULL;
    195 
    196   // Install extension v1.
    197   InstallIncreasingPermissionExtensionV1();
    198 
    199   // Note: This interceptor gets requests on the IO thread.
    200   content::URLLocalHostRequestPrepackagedInterceptor interceptor;
    201   net::URLFetcher::SetEnableInterceptionForTests(true);
    202   interceptor.SetResponseIgnoreQuery(
    203       GURL("http://localhost/autoupdate/updates.xml"),
    204       test_data_dir_.AppendASCII("permissions_increase")
    205                     .AppendASCII("updates.xml"));
    206   interceptor.SetResponseIgnoreQuery(
    207       GURL("http://localhost/autoupdate/v2.crx"),
    208       scoped_temp_dir_.path().AppendASCII("permissions2.crx"));
    209 
    210   extensions::ExtensionUpdater::CheckParams params;
    211   service_->updater()->set_default_check_params(params);
    212 
    213   // Sync is replacing an older version, so it pends.
    214   EXPECT_FALSE(sync_service->ProcessExtensionSyncData(sync_data));
    215 
    216   WaitForExtensionInstall();
    217   content::BrowserThread::GetBlockingPool()->FlushForTesting();
    218   base::RunLoop().RunUntilIdle();
    219 
    220   extension = service_->GetExtensionById(extension_id, true);
    221   ASSERT_TRUE(extension);
    222   EXPECT_EQ("2", extension->VersionString());
    223   EXPECT_EQ(1u, service_->disabled_extensions()->size());
    224   EXPECT_EQ(Extension::DISABLE_PERMISSIONS_INCREASE,
    225             service_->extension_prefs()->GetDisableReasons(extension_id));
    226   EXPECT_TRUE(GetExtensionDisabledGlobalError());
    227 }
    228