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/threading/sequenced_worker_pool.h"
8 #include "chrome/app/chrome_command_ids.h"
9 #include "chrome/browser/extensions/extension_browsertest.h"
10 #include "chrome/browser/extensions/extension_service.h"
11 #include "chrome/browser/extensions/extension_sync_service.h"
12 #include "chrome/browser/extensions/extension_uninstall_dialog.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 "extensions/browser/extension_dialog_auto_confirm.h"
23 #include "extensions/browser/extension_prefs.h"
24 #include "extensions/browser/extension_registry.h"
25 #include "extensions/browser/extension_system.h"
26 #include "extensions/common/extension.h"
27 #include "net/url_request/test_url_request_interceptor.h"
28 #include "sync/protocol/extension_specifics.pb.h"
29 #include "sync/protocol/sync.pb.h"
31 using content::BrowserThread
;
32 using extensions::Extension
;
33 using extensions::ExtensionRegistry
;
34 using extensions::ExtensionPrefs
;
36 class ExtensionDisabledGlobalErrorTest
: public ExtensionBrowserTest
{
38 void SetUpCommandLine(base::CommandLine
* command_line
) override
{
39 ExtensionBrowserTest::SetUpCommandLine(command_line
);
40 command_line
->AppendSwitchASCII(switches::kAppsGalleryUpdateURL
,
41 "http://localhost/autoupdate/updates.xml");
44 void SetUpOnMainThread() override
{
45 ExtensionBrowserTest::SetUpOnMainThread();
46 EXPECT_TRUE(scoped_temp_dir_
.CreateUniqueTempDir());
47 service_
= extensions::ExtensionSystem::Get(
48 browser()->profile())->extension_service();
49 registry_
= ExtensionRegistry::Get(browser()->profile());
50 const base::FilePath test_dir
=
51 test_data_dir_
.AppendASCII("permissions_increase");
52 const base::FilePath pem_path
= test_dir
.AppendASCII("permissions.pem");
53 path_v1_
= PackExtensionWithOptions(
54 test_dir
.AppendASCII("v1"),
55 scoped_temp_dir_
.path().AppendASCII("permissions1.crx"),
58 path_v2_
= PackExtensionWithOptions(
59 test_dir
.AppendASCII("v2"),
60 scoped_temp_dir_
.path().AppendASCII("permissions2.crx"),
63 path_v3_
= PackExtensionWithOptions(
64 test_dir
.AppendASCII("v3"),
65 scoped_temp_dir_
.path().AppendASCII("permissions3.crx"),
70 // Returns the ExtensionDisabledGlobalError, if present.
71 // Caution: currently only supports one error at a time.
72 GlobalError
* GetExtensionDisabledGlobalError() {
73 return GlobalErrorServiceFactory::GetForProfile(browser()->profile())->
74 GetGlobalErrorByMenuItemCommandID(IDC_EXTENSION_DISABLED_FIRST
);
77 // Install the initial version, which should happen just fine.
78 const Extension
* InstallIncreasingPermissionExtensionV1() {
79 size_t size_before
= registry_
->enabled_extensions().size();
80 const Extension
* extension
= InstallExtension(path_v1_
, 1);
83 if (registry_
->enabled_extensions().size() != size_before
+ 1)
88 // Upgrade to a version that wants more permissions. We should disable the
89 // extension and prompt the user to reenable.
90 const Extension
* UpdateIncreasingPermissionExtension(
91 const Extension
* extension
,
92 const base::FilePath
& crx_path
,
93 int expected_change
) {
94 size_t size_before
= registry_
->enabled_extensions().size();
95 if (UpdateExtension(extension
->id(), crx_path
, expected_change
))
97 content::RunAllBlockingPoolTasksUntilIdle();
98 EXPECT_EQ(size_before
+ expected_change
,
99 registry_
->enabled_extensions().size());
100 if (registry_
->disabled_extensions().size() != 1u)
103 return registry_
->disabled_extensions().begin()->get();
106 // Helper function to install an extension and upgrade it to a version
107 // requiring additional permissions. Returns the new disabled Extension.
108 const Extension
* InstallAndUpdateIncreasingPermissionsExtension() {
109 const Extension
* extension
= InstallIncreasingPermissionExtensionV1();
110 extension
= UpdateIncreasingPermissionExtension(extension
, path_v2_
, -1);
114 ExtensionService
* service_
;
115 ExtensionRegistry
* registry_
;
116 base::ScopedTempDir scoped_temp_dir_
;
117 base::FilePath path_v1_
;
118 base::FilePath path_v2_
;
119 base::FilePath path_v3_
;
122 // Tests the process of updating an extension to one that requires higher
123 // permissions, and accepting the permissions.
124 IN_PROC_BROWSER_TEST_F(ExtensionDisabledGlobalErrorTest
, AcceptPermissions
) {
125 const Extension
* extension
= InstallAndUpdateIncreasingPermissionsExtension();
126 ASSERT_TRUE(extension
);
127 ASSERT_TRUE(GetExtensionDisabledGlobalError());
128 const size_t size_before
= registry_
->enabled_extensions().size();
130 service_
->GrantPermissionsAndEnableExtension(extension
);
131 EXPECT_EQ(size_before
+ 1, registry_
->enabled_extensions().size());
132 EXPECT_EQ(0u, registry_
->disabled_extensions().size());
133 ASSERT_FALSE(GetExtensionDisabledGlobalError());
136 // Tests uninstalling an extension that was disabled due to higher permissions.
137 IN_PROC_BROWSER_TEST_F(ExtensionDisabledGlobalErrorTest
, Uninstall
) {
138 const Extension
* extension
= InstallAndUpdateIncreasingPermissionsExtension();
139 ASSERT_TRUE(extension
);
140 ASSERT_TRUE(GetExtensionDisabledGlobalError());
141 const size_t size_before
= registry_
->enabled_extensions().size();
143 UninstallExtension(extension
->id());
144 EXPECT_EQ(size_before
, registry_
->enabled_extensions().size());
145 EXPECT_EQ(0u, registry_
->disabled_extensions().size());
146 ASSERT_FALSE(GetExtensionDisabledGlobalError());
149 // Tests uninstalling a disabled extension with an uninstall dialog.
150 IN_PROC_BROWSER_TEST_F(ExtensionDisabledGlobalErrorTest
, UninstallFromDialog
) {
151 extensions::ScopedTestDialogAutoConfirm
auto_confirm(
152 extensions::ScopedTestDialogAutoConfirm::ACCEPT
);
153 const Extension
* extension
= InstallAndUpdateIncreasingPermissionsExtension();
154 ASSERT_TRUE(extension
);
155 std::string extension_id
= extension
->id();
156 GlobalErrorWithStandardBubble
* error
=
157 static_cast<GlobalErrorWithStandardBubble
*>(
158 GetExtensionDisabledGlobalError());
161 // The "cancel" button is the uninstall button on the browser.
162 error
->BubbleViewCancelButtonPressed(browser());
163 content::RunAllBlockingPoolTasksUntilIdle();
165 EXPECT_FALSE(registry_
->GetExtensionById(extension_id
,
166 ExtensionRegistry::EVERYTHING
));
167 EXPECT_FALSE(GetExtensionDisabledGlobalError());
170 // Tests that no error appears if the user disabled the extension.
171 IN_PROC_BROWSER_TEST_F(ExtensionDisabledGlobalErrorTest
, UserDisabled
) {
172 const Extension
* extension
= InstallIncreasingPermissionExtensionV1();
173 DisableExtension(extension
->id());
174 extension
= UpdateIncreasingPermissionExtension(extension
, path_v2_
, 0);
175 ASSERT_FALSE(GetExtensionDisabledGlobalError());
178 // Test that an error appears if the extension gets disabled because a
179 // version with higher permissions was installed by sync.
180 IN_PROC_BROWSER_TEST_F(ExtensionDisabledGlobalErrorTest
,
181 HigherPermissionsFromSync
) {
182 // Get data for extension v2 (disabled) into sync.
183 const Extension
* extension
= InstallAndUpdateIncreasingPermissionsExtension();
184 std::string extension_id
= extension
->id();
185 ExtensionSyncService
* sync_service
= ExtensionSyncService::Get(
186 browser()->profile());
187 extensions::ExtensionSyncData sync_data
=
188 sync_service
->GetExtensionSyncData(*extension
);
189 UninstallExtension(extension_id
);
192 // Install extension v1.
193 InstallIncreasingPermissionExtensionV1();
195 // Note: This interceptor gets requests on the IO thread.
196 net::LocalHostTestURLRequestInterceptor
interceptor(
197 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO
),
198 BrowserThread::GetBlockingPool()->GetTaskRunnerWithShutdownBehavior(
199 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN
));
200 interceptor
.SetResponseIgnoreQuery(
201 GURL("http://localhost/autoupdate/updates.xml"),
202 test_data_dir_
.AppendASCII("permissions_increase")
203 .AppendASCII("updates.xml"));
204 interceptor
.SetResponseIgnoreQuery(
205 GURL("http://localhost/autoupdate/v2.crx"),
206 scoped_temp_dir_
.path().AppendASCII("permissions2.crx"));
208 extensions::ExtensionUpdater::CheckParams params
;
209 service_
->updater()->set_default_check_params(params
);
211 // Sync is replacing an older version, so it pends.
212 EXPECT_FALSE(sync_service
->ProcessExtensionSyncData(sync_data
));
214 WaitForExtensionInstall();
215 content::RunAllBlockingPoolTasksUntilIdle();
217 extension
= service_
->GetExtensionById(extension_id
, true);
218 ASSERT_TRUE(extension
);
219 EXPECT_EQ("2", extension
->VersionString());
220 EXPECT_EQ(1u, registry_
->disabled_extensions().size());
221 EXPECT_EQ(Extension::DISABLE_PERMISSIONS_INCREASE
,
222 ExtensionPrefs::Get(service_
->profile())
223 ->GetDisableReasons(extension_id
));
224 EXPECT_TRUE(GetExtensionDisabledGlobalError());
227 // Test that an error appears if an extension gets installed server side.
228 IN_PROC_BROWSER_TEST_F(ExtensionDisabledGlobalErrorTest
, RemoteInstall
) {
229 static const char extension_id
[] = "pgdpcfcocojkjfbgpiianjngphoopgmo";
230 ExtensionSyncService
* sync_service
=
231 ExtensionSyncService::Get(browser()->profile());
233 // Note: This interceptor gets requests on the IO thread.
234 net::LocalHostTestURLRequestInterceptor
interceptor(
235 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO
),
236 BrowserThread::GetBlockingPool()->GetTaskRunnerWithShutdownBehavior(
237 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN
));
238 interceptor
.SetResponseIgnoreQuery(
239 GURL("http://localhost/autoupdate/updates.xml"),
240 test_data_dir_
.AppendASCII("permissions_increase")
241 .AppendASCII("updates.xml"));
242 interceptor
.SetResponseIgnoreQuery(
243 GURL("http://localhost/autoupdate/v2.crx"),
244 scoped_temp_dir_
.path().AppendASCII("permissions2.crx"));
246 extensions::ExtensionUpdater::CheckParams params
;
247 service_
->updater()->set_default_check_params(params
);
249 sync_pb::EntitySpecifics specifics
;
250 specifics
.mutable_extension()->set_id(extension_id
);
251 specifics
.mutable_extension()->set_enabled(false);
252 specifics
.mutable_extension()->set_remote_install(true);
253 specifics
.mutable_extension()->set_update_url(
254 "http://localhost/autoupdate/updates.xml");
255 specifics
.mutable_extension()->set_version("2");
256 syncer::SyncData sync_data
=
257 syncer::SyncData::CreateRemoteData(1234567,
260 syncer::AttachmentIdList(),
261 syncer::AttachmentServiceProxy());
262 // Sync is installing a new extension, so it pends.
263 EXPECT_FALSE(sync_service
->ProcessExtensionSyncData(
264 *extensions::ExtensionSyncData::CreateFromSyncData(sync_data
)));
266 WaitForExtensionInstall();
267 content::RunAllBlockingPoolTasksUntilIdle();
269 const Extension
* extension
= service_
->GetExtensionById(extension_id
, true);
270 ASSERT_TRUE(extension
);
271 EXPECT_EQ("2", extension
->VersionString());
272 EXPECT_EQ(1u, registry_
->disabled_extensions().size());
273 EXPECT_EQ(Extension::DISABLE_REMOTE_INSTALL
,
274 ExtensionPrefs::Get(service_
->profile())
275 ->GetDisableReasons(extension_id
));
276 EXPECT_TRUE(GetExtensionDisabledGlobalError());