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(
50 browser()->profile())->extension_service();
51 registry_
= ExtensionRegistry::Get(browser()->profile());
52 const base::FilePath test_dir
=
53 test_data_dir_
.AppendASCII("permissions_increase");
54 const base::FilePath pem_path
= test_dir
.AppendASCII("permissions.pem");
55 path_v1_
= PackExtensionWithOptions(
56 test_dir
.AppendASCII("v1"),
57 scoped_temp_dir_
.path().AppendASCII("permissions1.crx"),
60 path_v2_
= PackExtensionWithOptions(
61 test_dir
.AppendASCII("v2"),
62 scoped_temp_dir_
.path().AppendASCII("permissions2.crx"),
65 path_v3_
= PackExtensionWithOptions(
66 test_dir
.AppendASCII("v3"),
67 scoped_temp_dir_
.path().AppendASCII("permissions3.crx"),
72 // Returns the ExtensionDisabledGlobalError, if present.
73 // Caution: currently only supports one error at a time.
74 GlobalError
* GetExtensionDisabledGlobalError() {
75 return GlobalErrorServiceFactory::GetForProfile(browser()->profile())->
76 GetGlobalErrorByMenuItemCommandID(IDC_EXTENSION_DISABLED_FIRST
);
79 // Install the initial version, which should happen just fine.
80 const Extension
* InstallIncreasingPermissionExtensionV1() {
81 size_t size_before
= registry_
->enabled_extensions().size();
82 const Extension
* extension
= InstallExtension(path_v1_
, 1);
85 if (registry_
->enabled_extensions().size() != size_before
+ 1)
90 // Upgrade to a version that wants more permissions. We should disable the
91 // extension and prompt the user to reenable.
92 const Extension
* UpdateIncreasingPermissionExtension(
93 const Extension
* extension
,
94 const base::FilePath
& crx_path
,
95 int expected_change
) {
96 size_t size_before
= registry_
->enabled_extensions().size();
97 if (UpdateExtension(extension
->id(), crx_path
, expected_change
))
99 content::RunAllBlockingPoolTasksUntilIdle();
100 EXPECT_EQ(size_before
+ expected_change
,
101 registry_
->enabled_extensions().size());
102 if (registry_
->disabled_extensions().size() != 1u)
105 return registry_
->disabled_extensions().begin()->get();
108 // Helper function to install an extension and upgrade it to a version
109 // requiring additional permissions. Returns the new disabled Extension.
110 const Extension
* InstallAndUpdateIncreasingPermissionsExtension() {
111 const Extension
* extension
= InstallIncreasingPermissionExtensionV1();
112 extension
= UpdateIncreasingPermissionExtension(extension
, path_v2_
, -1);
116 ExtensionService
* service_
;
117 ExtensionRegistry
* registry_
;
118 base::ScopedTempDir scoped_temp_dir_
;
119 base::FilePath path_v1_
;
120 base::FilePath path_v2_
;
121 base::FilePath path_v3_
;
124 // Tests the process of updating an extension to one that requires higher
125 // permissions, and accepting the permissions.
126 IN_PROC_BROWSER_TEST_F(ExtensionDisabledGlobalErrorTest
, AcceptPermissions
) {
127 const Extension
* extension
= InstallAndUpdateIncreasingPermissionsExtension();
128 ASSERT_TRUE(extension
);
129 ASSERT_TRUE(GetExtensionDisabledGlobalError());
130 const size_t size_before
= registry_
->enabled_extensions().size();
132 service_
->GrantPermissionsAndEnableExtension(extension
);
133 EXPECT_EQ(size_before
+ 1, registry_
->enabled_extensions().size());
134 EXPECT_EQ(0u, registry_
->disabled_extensions().size());
135 ASSERT_FALSE(GetExtensionDisabledGlobalError());
138 // Tests uninstalling an extension that was disabled due to higher permissions.
139 IN_PROC_BROWSER_TEST_F(ExtensionDisabledGlobalErrorTest
, Uninstall
) {
140 const Extension
* extension
= InstallAndUpdateIncreasingPermissionsExtension();
141 ASSERT_TRUE(extension
);
142 ASSERT_TRUE(GetExtensionDisabledGlobalError());
143 const size_t size_before
= registry_
->enabled_extensions().size();
145 UninstallExtension(extension
->id());
146 EXPECT_EQ(size_before
, registry_
->enabled_extensions().size());
147 EXPECT_EQ(0u, registry_
->disabled_extensions().size());
148 ASSERT_FALSE(GetExtensionDisabledGlobalError());
151 // Tests uninstalling a disabled extension with an uninstall dialog.
152 IN_PROC_BROWSER_TEST_F(ExtensionDisabledGlobalErrorTest
, UninstallFromDialog
) {
153 extensions::ScopedTestDialogAutoConfirm
auto_confirm(
154 extensions::ScopedTestDialogAutoConfirm::ACCEPT
);
155 const Extension
* extension
= InstallAndUpdateIncreasingPermissionsExtension();
156 ASSERT_TRUE(extension
);
157 std::string extension_id
= extension
->id();
158 GlobalErrorWithStandardBubble
* error
=
159 static_cast<GlobalErrorWithStandardBubble
*>(
160 GetExtensionDisabledGlobalError());
163 // The "cancel" button is the uninstall button on the browser.
164 error
->BubbleViewCancelButtonPressed(browser());
165 content::RunAllBlockingPoolTasksUntilIdle();
167 EXPECT_FALSE(registry_
->GetExtensionById(extension_id
,
168 ExtensionRegistry::EVERYTHING
));
169 EXPECT_FALSE(GetExtensionDisabledGlobalError());
172 // Tests that no error appears if the user disabled the extension.
173 IN_PROC_BROWSER_TEST_F(ExtensionDisabledGlobalErrorTest
, UserDisabled
) {
174 const Extension
* extension
= InstallIncreasingPermissionExtensionV1();
175 DisableExtension(extension
->id());
176 extension
= UpdateIncreasingPermissionExtension(extension
, path_v2_
, 0);
177 ASSERT_FALSE(GetExtensionDisabledGlobalError());
180 // Test that an error appears if the extension gets disabled because a
181 // version with higher permissions was installed by sync.
182 IN_PROC_BROWSER_TEST_F(ExtensionDisabledGlobalErrorTest
,
183 HigherPermissionsFromSync
) {
184 // Get data for extension v2 (disabled) into sync.
185 const Extension
* extension
= InstallAndUpdateIncreasingPermissionsExtension();
186 std::string extension_id
= extension
->id();
187 ExtensionSyncService
* sync_service
= ExtensionSyncService::Get(
188 browser()->profile());
189 extensions::ExtensionSyncData sync_data
=
190 sync_service
->CreateSyncData(*extension
);
191 UninstallExtension(extension_id
);
194 // Install extension v1.
195 InstallIncreasingPermissionExtensionV1();
197 // Note: This interceptor gets requests on the IO thread.
198 net::LocalHostTestURLRequestInterceptor
interceptor(
199 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO
),
200 BrowserThread::GetBlockingPool()->GetTaskRunnerWithShutdownBehavior(
201 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN
));
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"));
210 extensions::ExtensionUpdater::CheckParams params
;
211 service_
->updater()->set_default_check_params(params
);
213 // Sync is replacing an older version, so it pends.
214 EXPECT_FALSE(sync_service
->ApplySyncData(sync_data
));
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";
232 ExtensionSyncService
* sync_service
=
233 ExtensionSyncService::Get(browser()->profile());
235 // Note: This interceptor gets requests on the IO thread.
236 net::LocalHostTestURLRequestInterceptor
interceptor(
237 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO
),
238 BrowserThread::GetBlockingPool()->GetTaskRunnerWithShutdownBehavior(
239 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN
));
240 interceptor
.SetResponseIgnoreQuery(
241 GURL("http://localhost/autoupdate/updates.xml"),
242 test_data_dir_
.AppendASCII("permissions_increase")
243 .AppendASCII("updates.xml"));
244 interceptor
.SetResponseIgnoreQuery(
245 GURL("http://localhost/autoupdate/v2.crx"),
246 scoped_temp_dir_
.path().AppendASCII("permissions2.crx"));
248 extensions::ExtensionUpdater::CheckParams params
;
249 service_
->updater()->set_default_check_params(params
);
251 sync_pb::EntitySpecifics specifics
;
252 specifics
.mutable_extension()->set_id(extension_id
);
253 specifics
.mutable_extension()->set_enabled(false);
254 specifics
.mutable_extension()->set_remote_install(true);
255 specifics
.mutable_extension()->set_update_url(
256 "http://localhost/autoupdate/updates.xml");
257 specifics
.mutable_extension()->set_version("2");
258 syncer::SyncData sync_data
=
259 syncer::SyncData::CreateRemoteData(1234567,
262 syncer::AttachmentIdList(),
263 syncer::AttachmentServiceProxy());
264 // Sync is installing a new extension, so it pends.
265 EXPECT_FALSE(sync_service
->ApplySyncData(
266 *extensions::ExtensionSyncData::CreateFromSyncData(sync_data
)));
268 WaitForExtensionInstall();
269 content::RunAllBlockingPoolTasksUntilIdle();
271 const Extension
* extension
= service_
->GetExtensionById(extension_id
, true);
272 ASSERT_TRUE(extension
);
273 EXPECT_EQ("2", extension
->VersionString());
274 EXPECT_EQ(1u, registry_
->disabled_extensions().size());
275 EXPECT_EQ(Extension::DISABLE_REMOTE_INSTALL
,
276 ExtensionPrefs::Get(service_
->profile())
277 ->GetDisableReasons(extension_id
));
278 EXPECT_TRUE(GetExtensionDisabledGlobalError());