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_data.h"
12 #include "chrome/browser/extensions/extension_sync_service.h"
13 #include "chrome/browser/extensions/extension_uninstall_dialog.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 "extensions/browser/extension_dialog_auto_confirm.h"
24 #include "extensions/browser/extension_prefs.h"
25 #include "extensions/browser/extension_registry.h"
26 #include "extensions/browser/extension_system.h"
27 #include "extensions/common/extension.h"
28 #include "net/url_request/test_url_request_interceptor.h"
29 #include "sync/protocol/extension_specifics.pb.h"
30 #include "sync/protocol/sync.pb.h"
32 using content::BrowserThread
;
33 using extensions::Extension
;
34 using extensions::ExtensionRegistry
;
35 using extensions::ExtensionPrefs
;
36 using extensions::ExtensionSyncData
;
38 class ExtensionDisabledGlobalErrorTest
: public ExtensionBrowserTest
{
40 void SetUpCommandLine(base::CommandLine
* command_line
) override
{
41 ExtensionBrowserTest::SetUpCommandLine(command_line
);
42 command_line
->AppendSwitchASCII(switches::kAppsGalleryUpdateURL
,
43 "http://localhost/autoupdate/updates.xml");
46 void SetUpOnMainThread() override
{
47 ExtensionBrowserTest::SetUpOnMainThread();
48 EXPECT_TRUE(scoped_temp_dir_
.CreateUniqueTempDir());
49 service_
= extensions::ExtensionSystem::Get(profile())->extension_service();
50 registry_
= ExtensionRegistry::Get(profile());
51 const base::FilePath test_dir
=
52 test_data_dir_
.AppendASCII("permissions_increase");
53 const base::FilePath pem_path
= test_dir
.AppendASCII("permissions.pem");
54 path_v1_
= PackExtensionWithOptions(
55 test_dir
.AppendASCII("v1"),
56 scoped_temp_dir_
.path().AppendASCII("permissions1.crx"),
59 path_v2_
= PackExtensionWithOptions(
60 test_dir
.AppendASCII("v2"),
61 scoped_temp_dir_
.path().AppendASCII("permissions2.crx"),
64 path_v3_
= PackExtensionWithOptions(
65 test_dir
.AppendASCII("v3"),
66 scoped_temp_dir_
.path().AppendASCII("permissions3.crx"),
71 // Returns the ExtensionDisabledGlobalError, if present.
72 // Caution: currently only supports one error at a time.
73 GlobalError
* GetExtensionDisabledGlobalError() {
74 return GlobalErrorServiceFactory::GetForProfile(profile())->
75 GetGlobalErrorByMenuItemCommandID(IDC_EXTENSION_DISABLED_FIRST
);
78 // Install the initial version, which should happen just fine.
79 const Extension
* InstallIncreasingPermissionExtensionV1() {
80 size_t size_before
= registry_
->enabled_extensions().size();
81 const Extension
* extension
= InstallExtension(path_v1_
, 1);
84 if (registry_
->enabled_extensions().size() != size_before
+ 1)
89 // Upgrade to a version that wants more permissions. We should disable the
90 // extension and prompt the user to reenable.
91 const Extension
* UpdateIncreasingPermissionExtension(
92 const Extension
* extension
,
93 const base::FilePath
& crx_path
,
94 int expected_change
) {
95 size_t size_before
= registry_
->enabled_extensions().size();
96 if (UpdateExtension(extension
->id(), crx_path
, expected_change
))
98 content::RunAllBlockingPoolTasksUntilIdle();
99 EXPECT_EQ(size_before
+ expected_change
,
100 registry_
->enabled_extensions().size());
101 if (registry_
->disabled_extensions().size() != 1u)
104 return registry_
->disabled_extensions().begin()->get();
107 // Helper function to install an extension and upgrade it to a version
108 // requiring additional permissions. Returns the new disabled Extension.
109 const Extension
* InstallAndUpdateIncreasingPermissionsExtension() {
110 const Extension
* extension
= InstallIncreasingPermissionExtensionV1();
111 extension
= UpdateIncreasingPermissionExtension(extension
, path_v2_
, -1);
115 ExtensionService
* service_
;
116 ExtensionRegistry
* registry_
;
117 base::ScopedTempDir scoped_temp_dir_
;
118 base::FilePath path_v1_
;
119 base::FilePath path_v2_
;
120 base::FilePath path_v3_
;
123 // Tests the process of updating an extension to one that requires higher
124 // permissions, and accepting the permissions.
125 IN_PROC_BROWSER_TEST_F(ExtensionDisabledGlobalErrorTest
, AcceptPermissions
) {
126 const Extension
* extension
= InstallAndUpdateIncreasingPermissionsExtension();
127 ASSERT_TRUE(extension
);
128 ASSERT_TRUE(GetExtensionDisabledGlobalError());
129 const size_t size_before
= registry_
->enabled_extensions().size();
131 service_
->GrantPermissionsAndEnableExtension(extension
);
132 EXPECT_EQ(size_before
+ 1, registry_
->enabled_extensions().size());
133 EXPECT_EQ(0u, registry_
->disabled_extensions().size());
134 ASSERT_FALSE(GetExtensionDisabledGlobalError());
137 // Tests uninstalling an extension that was disabled due to higher permissions.
138 IN_PROC_BROWSER_TEST_F(ExtensionDisabledGlobalErrorTest
, Uninstall
) {
139 const Extension
* extension
= InstallAndUpdateIncreasingPermissionsExtension();
140 ASSERT_TRUE(extension
);
141 ASSERT_TRUE(GetExtensionDisabledGlobalError());
142 const size_t size_before
= registry_
->enabled_extensions().size();
144 UninstallExtension(extension
->id());
145 EXPECT_EQ(size_before
, registry_
->enabled_extensions().size());
146 EXPECT_EQ(0u, registry_
->disabled_extensions().size());
147 ASSERT_FALSE(GetExtensionDisabledGlobalError());
150 // Tests uninstalling a disabled extension with an uninstall dialog.
151 IN_PROC_BROWSER_TEST_F(ExtensionDisabledGlobalErrorTest
, UninstallFromDialog
) {
152 extensions::ScopedTestDialogAutoConfirm
auto_confirm(
153 extensions::ScopedTestDialogAutoConfirm::ACCEPT
);
154 const Extension
* extension
= InstallAndUpdateIncreasingPermissionsExtension();
155 ASSERT_TRUE(extension
);
156 std::string extension_id
= extension
->id();
157 GlobalErrorWithStandardBubble
* error
=
158 static_cast<GlobalErrorWithStandardBubble
*>(
159 GetExtensionDisabledGlobalError());
162 // The "cancel" button is the uninstall button on the browser.
163 error
->BubbleViewCancelButtonPressed(browser());
164 content::RunAllBlockingPoolTasksUntilIdle();
166 EXPECT_FALSE(registry_
->GetExtensionById(extension_id
,
167 ExtensionRegistry::EVERYTHING
));
168 EXPECT_FALSE(GetExtensionDisabledGlobalError());
171 // Tests that no error appears if the user disabled the extension.
172 IN_PROC_BROWSER_TEST_F(ExtensionDisabledGlobalErrorTest
, UserDisabled
) {
173 const Extension
* extension
= InstallIncreasingPermissionExtensionV1();
174 DisableExtension(extension
->id());
175 extension
= UpdateIncreasingPermissionExtension(extension
, path_v2_
, 0);
176 ASSERT_FALSE(GetExtensionDisabledGlobalError());
179 // Test that an error appears if the extension gets disabled because a
180 // version with higher permissions was installed by sync.
181 IN_PROC_BROWSER_TEST_F(ExtensionDisabledGlobalErrorTest
,
182 HigherPermissionsFromSync
) {
183 // Get sync data for extension v2 (disabled).
184 const Extension
* extension
= InstallAndUpdateIncreasingPermissionsExtension();
185 std::string extension_id
= extension
->id();
186 ExtensionSyncService
* sync_service
= ExtensionSyncService::Get(profile());
187 extensions::ExtensionSyncData sync_data
=
188 sync_service
->CreateSyncData(*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_service
->ProcessSyncChanges(
213 syncer::SyncChangeList(
214 1, sync_data
.GetSyncChange(syncer::SyncChange::ACTION_ADD
)));
216 WaitForExtensionInstall();
217 content::RunAllBlockingPoolTasksUntilIdle();
219 extension
= service_
->GetExtensionById(extension_id
, true);
220 ASSERT_TRUE(extension
);
221 EXPECT_EQ("2", extension
->VersionString());
222 EXPECT_EQ(1u, registry_
->disabled_extensions().size());
223 EXPECT_EQ(Extension::DISABLE_PERMISSIONS_INCREASE
,
224 ExtensionPrefs::Get(service_
->profile())
225 ->GetDisableReasons(extension_id
));
226 EXPECT_TRUE(GetExtensionDisabledGlobalError());
229 // Test that an error appears if an extension gets installed server side.
230 IN_PROC_BROWSER_TEST_F(ExtensionDisabledGlobalErrorTest
, RemoteInstall
) {
231 static const char extension_id
[] = "pgdpcfcocojkjfbgpiianjngphoopgmo";
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 ExtensionSyncService::Get(profile())->ProcessSyncChanges(
264 syncer::SyncChangeList(
265 1, syncer::SyncChange(FROM_HERE
,
266 syncer::SyncChange::ACTION_ADD
,
269 WaitForExtensionInstall();
270 content::RunAllBlockingPoolTasksUntilIdle();
272 const Extension
* extension
= service_
->GetExtensionById(extension_id
, true);
273 ASSERT_TRUE(extension
);
274 EXPECT_EQ("2", extension
->VersionString());
275 EXPECT_EQ(1u, registry_
->disabled_extensions().size());
276 EXPECT_EQ(Extension::DISABLE_REMOTE_INSTALL
,
277 ExtensionPrefs::Get(service_
->profile())
278 ->GetDisableReasons(extension_id
));
279 EXPECT_TRUE(GetExtensionDisabledGlobalError());