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