| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/file_path.h" |
| 6 #include "base/scoped_temp_dir.h" |
| 5 #include "chrome/app/chrome_command_ids.h" | 7 #include "chrome/app/chrome_command_ids.h" |
| 6 #include "chrome/browser/extensions/extension_browsertest.h" | 8 #include "chrome/browser/extensions/extension_browsertest.h" |
| 9 #include "chrome/browser/extensions/extension_prefs.h" |
| 7 #include "chrome/browser/extensions/extension_service.h" | 10 #include "chrome/browser/extensions/extension_service.h" |
| 8 #include "chrome/browser/profiles/profile.h" | 11 #include "chrome/browser/profiles/profile.h" |
| 9 #include "chrome/browser/ui/browser.h" | 12 #include "chrome/browser/ui/browser.h" |
| 13 #include "chrome/browser/ui/global_error.h" |
| 10 #include "chrome/browser/ui/global_error_service.h" | 14 #include "chrome/browser/ui/global_error_service.h" |
| 11 #include "chrome/browser/ui/global_error_service_factory.h" | 15 #include "chrome/browser/ui/global_error_service_factory.h" |
| 12 #include "chrome/common/extensions/extension.h" | 16 #include "chrome/common/extensions/extension.h" |
| 13 | 17 |
| 14 class ExtensionDisabledGlobalErrorTest : public ExtensionBrowserTest { | 18 class ExtensionDisabledGlobalErrorTest : public ExtensionBrowserTest { |
| 15 protected: | 19 protected: |
| 16 void SetUpOnMainThread() { | 20 void SetUpOnMainThread() { |
| 21 EXPECT_TRUE(scoped_temp_dir_.CreateUniqueTempDir()); |
| 17 service_ = browser()->profile()->GetExtensionService(); | 22 service_ = browser()->profile()->GetExtensionService(); |
| 23 FilePath pem_path = test_data_dir_. |
| 24 AppendASCII("permissions_increase").AppendASCII("permissions.pem"); |
| 25 path_v1_ = PackExtensionWithOptions( |
| 26 test_data_dir_.AppendASCII("permissions_increase").AppendASCII("v1"), |
| 27 scoped_temp_dir_.path().AppendASCII("permissions1.crx"), |
| 28 pem_path, |
| 29 FilePath()); |
| 30 path_v2_ = PackExtensionWithOptions( |
| 31 test_data_dir_.AppendASCII("permissions_increase").AppendASCII("v2"), |
| 32 scoped_temp_dir_.path().AppendASCII("permissions2.crx"), |
| 33 pem_path, |
| 34 FilePath()); |
| 35 path_v3_ = PackExtensionWithOptions( |
| 36 test_data_dir_.AppendASCII("permissions_increase").AppendASCII("v3"), |
| 37 scoped_temp_dir_.path().AppendASCII("permissions3.crx"), |
| 38 pem_path, |
| 39 FilePath()); |
| 18 } | 40 } |
| 19 | 41 |
| 20 // Returns the ExtensionDisabledGlobalError, if present. | 42 // Returns the ExtensionDisabledGlobalError, if present. |
| 21 // Caution: currently only supports one error at a time. | 43 // Caution: currently only supports one error at a time. |
| 22 GlobalError* GetExtensionDisabledGlobalError() { | 44 GlobalError* GetExtensionDisabledGlobalError() { |
| 23 return GlobalErrorServiceFactory::GetForProfile(browser()->profile())-> | 45 return GlobalErrorServiceFactory::GetForProfile(browser()->profile())-> |
| 24 GetGlobalErrorByMenuItemCommandID(IDC_EXTENSION_DISABLED_FIRST); | 46 GetGlobalErrorByMenuItemCommandID(IDC_EXTENSION_DISABLED_FIRST); |
| 25 } | 47 } |
| 26 | 48 |
| 27 // Helper function to install an extension and upgrade it to a version | 49 // Install the initial version, which should happen just fine. |
| 28 // requiring additional permissions. Returns the new disabled Extension. | 50 const Extension* InstallIncreasingPermissionExtensionV1() { |
| 29 const Extension* InstallAndUpdateIncreasingPermissionsExtension() { | |
| 30 size_t size_before = service_->extensions()->size(); | 51 size_t size_before = service_->extensions()->size(); |
| 31 | 52 const Extension* extension = InstallExtension(path_v1_, 1); |
| 32 // Install the initial version, which should happen just fine. | |
| 33 const Extension* extension = InstallExtension( | |
| 34 test_data_dir_.AppendASCII("permissions-low-v1.crx"), 1); | |
| 35 if (!extension) | 53 if (!extension) |
| 36 return NULL; | 54 return NULL; |
| 37 if (service_->extensions()->size() != size_before + 1) | 55 if (service_->extensions()->size() != size_before + 1) |
| 38 return NULL; | 56 return NULL; |
| 57 return extension; |
| 58 } |
| 39 | 59 |
| 40 // Upgrade to a version that wants more permissions. We should disable the | 60 // Upgrade to a version that wants more permissions. We should disable the |
| 41 // extension and prompt the user to reenable. | 61 // extension and prompt the user to reenable. |
| 42 if (UpdateExtension( | 62 const Extension* UpdateIncreasingPermissionExtension( |
| 43 extension->id(), | 63 const Extension* extension, |
| 44 test_data_dir_.AppendASCII("permissions-high-v2.crx"), -1)) | 64 const FilePath& crx_path, |
| 65 int expected_change) { |
| 66 size_t size_before = service_->extensions()->size(); |
| 67 if (UpdateExtension(extension->id(), crx_path, expected_change)) |
| 45 return NULL; | 68 return NULL; |
| 46 EXPECT_EQ(size_before, service_->extensions()->size()); | 69 EXPECT_EQ(size_before + expected_change, service_->extensions()->size()); |
| 47 if (service_->disabled_extensions()->size() != 1u) | 70 if (service_->disabled_extensions()->size() != 1u) |
| 48 return NULL; | 71 return NULL; |
| 49 | 72 |
| 50 return *service_->disabled_extensions()->begin(); | 73 return *service_->disabled_extensions()->begin(); |
| 51 } | 74 } |
| 52 | 75 |
| 76 // Helper function to install an extension and upgrade it to a version |
| 77 // requiring additional permissions. Returns the new disabled Extension. |
| 78 const Extension* InstallAndUpdateIncreasingPermissionsExtension() { |
| 79 const Extension* extension = InstallIncreasingPermissionExtensionV1(); |
| 80 extension = UpdateIncreasingPermissionExtension(extension, path_v2_, -1); |
| 81 return extension; |
| 82 } |
| 83 |
| 53 ExtensionService* service_; | 84 ExtensionService* service_; |
| 85 ScopedTempDir scoped_temp_dir_; |
| 86 FilePath path_v1_; |
| 87 FilePath path_v2_; |
| 88 FilePath path_v3_; |
| 54 }; | 89 }; |
| 55 | 90 |
| 56 // Tests the process of updating an extension to one that requires higher | 91 // Tests the process of updating an extension to one that requires higher |
| 57 // permissions, and accepting the permissions. | 92 // permissions, and accepting the permissions. |
| 58 IN_PROC_BROWSER_TEST_F(ExtensionDisabledGlobalErrorTest, AcceptPermissions) { | 93 IN_PROC_BROWSER_TEST_F(ExtensionDisabledGlobalErrorTest, AcceptPermissions) { |
| 59 const Extension* extension = InstallAndUpdateIncreasingPermissionsExtension(); | 94 const Extension* extension = InstallAndUpdateIncreasingPermissionsExtension(); |
| 60 ASSERT_TRUE(extension); | 95 ASSERT_TRUE(extension); |
| 61 ASSERT_TRUE(GetExtensionDisabledGlobalError()); | 96 ASSERT_TRUE(GetExtensionDisabledGlobalError()); |
| 62 const size_t size_before = service_->extensions()->size(); | 97 const size_t size_before = service_->extensions()->size(); |
| 63 | 98 |
| 64 service_->GrantPermissionsAndEnableExtension(extension); | 99 service_->GrantPermissionsAndEnableExtension(extension); |
| 65 EXPECT_EQ(size_before + 1, service_->extensions()->size()); | 100 EXPECT_EQ(size_before + 1, service_->extensions()->size()); |
| 66 EXPECT_EQ(0u, service_->disabled_extensions()->size()); | 101 EXPECT_EQ(0u, service_->disabled_extensions()->size()); |
| 67 ASSERT_FALSE(GetExtensionDisabledGlobalError()); | 102 ASSERT_FALSE(GetExtensionDisabledGlobalError()); |
| 68 } | 103 } |
| 69 | 104 |
| 70 // Tests uninstalling an extension that was disabled due to higher permissions. | 105 // Tests uninstalling an extension that was disabled due to higher permissions. |
| 71 IN_PROC_BROWSER_TEST_F(ExtensionDisabledGlobalErrorTest, Uninstall) { | 106 IN_PROC_BROWSER_TEST_F(ExtensionDisabledGlobalErrorTest, Uninstall) { |
| 72 const Extension* extension = InstallAndUpdateIncreasingPermissionsExtension(); | 107 const Extension* extension = InstallAndUpdateIncreasingPermissionsExtension(); |
| 73 ASSERT_TRUE(extension); | 108 ASSERT_TRUE(extension); |
| 74 ASSERT_TRUE(GetExtensionDisabledGlobalError()); | 109 ASSERT_TRUE(GetExtensionDisabledGlobalError()); |
| 75 const size_t size_before = service_->extensions()->size(); | 110 const size_t size_before = service_->extensions()->size(); |
| 76 | 111 |
| 77 UninstallExtension(extension->id()); | 112 UninstallExtension(extension->id()); |
| 78 EXPECT_EQ(size_before, service_->extensions()->size()); | 113 EXPECT_EQ(size_before, service_->extensions()->size()); |
| 79 EXPECT_EQ(0u, service_->disabled_extensions()->size()); | 114 EXPECT_EQ(0u, service_->disabled_extensions()->size()); |
| 80 ASSERT_FALSE(GetExtensionDisabledGlobalError()); | 115 ASSERT_FALSE(GetExtensionDisabledGlobalError()); |
| 81 } | 116 } |
| 117 |
| 118 // Tests that no error appears if the user disabled the extension. |
| 119 IN_PROC_BROWSER_TEST_F(ExtensionDisabledGlobalErrorTest, UserDisabled) { |
| 120 const Extension* extension = InstallIncreasingPermissionExtensionV1(); |
| 121 DisableExtension(extension->id()); |
| 122 extension = UpdateIncreasingPermissionExtension(extension, path_v2_, 0); |
| 123 ASSERT_FALSE(GetExtensionDisabledGlobalError()); |
| 124 } |
| 125 |
| 126 // Test that no error appears if the disable reason is unknown |
| 127 // (but probably was by the user). |
| 128 IN_PROC_BROWSER_TEST_F(ExtensionDisabledGlobalErrorTest, |
| 129 UnknownReasonSamePermissions) { |
| 130 const Extension* extension = InstallIncreasingPermissionExtensionV1(); |
| 131 DisableExtension(extension->id()); |
| 132 // Clear disable reason to simulate legacy disables. |
| 133 service_->extension_prefs()->RemoveDisableReason(extension->id()); |
| 134 // Upgrade to version 2. Infer from version 1 having the same permissions |
| 135 // granted by the user that it was disabled by the user. |
| 136 extension = UpdateIncreasingPermissionExtension(extension, path_v2_, 0); |
| 137 ASSERT_TRUE(extension); |
| 138 ASSERT_FALSE(GetExtensionDisabledGlobalError()); |
| 139 } |
| 140 |
| 141 // Test that an error appears if the disable reason is unknown |
| 142 // (but probably was for increased permissions). |
| 143 IN_PROC_BROWSER_TEST_F(ExtensionDisabledGlobalErrorTest, |
| 144 UnknownReasonHigherPermissions) { |
| 145 const Extension* extension = InstallAndUpdateIncreasingPermissionsExtension(); |
| 146 // Clear disable reason to simulate legacy disables. |
| 147 service_->extension_prefs()->RemoveDisableReason(extension->id()); |
| 148 // We now have version 2 but only accepted permissions for version 1. |
| 149 GlobalError* error = GetExtensionDisabledGlobalError(); |
| 150 ASSERT_TRUE(error); |
| 151 // Also, remove the upgrade error for version 2. |
| 152 GlobalErrorServiceFactory::GetForProfile(browser()->profile())-> |
| 153 RemoveGlobalError(error); |
| 154 delete error; |
| 155 // Upgrade to version 3, with even higher permissions. Infer from |
| 156 // version 2 having higher-than-granted permissions that it was disabled |
| 157 // for permissions increase. |
| 158 extension = UpdateIncreasingPermissionExtension(extension, path_v3_, 0); |
| 159 ASSERT_TRUE(extension); |
| 160 ASSERT_TRUE(GetExtensionDisabledGlobalError()); |
| 161 } |
| OLD | NEW |