Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / extensions / api / management / management_browsertest.cc
blobb2b35ef0ceff2e3a884e0912306f4d47404c80a8
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/bind.h"
6 #include "base/bind_helpers.h"
7 #include "base/memory/ref_counted.h"
8 #include "base/prefs/scoped_user_pref_update.h"
9 #include "base/run_loop.h"
10 #include "base/stl_util.h"
11 #include "base/strings/stringprintf.h"
12 #include "chrome/browser/extensions/extension_browsertest.h"
13 #include "chrome/browser/extensions/extension_management.h"
14 #include "chrome/browser/extensions/extension_service.h"
15 #include "chrome/browser/extensions/updater/extension_updater.h"
16 #include "chrome/browser/profiles/profile.h"
17 #include "chrome/browser/ui/browser.h"
18 #include "chrome/common/url_constants.h"
19 #include "components/policy/core/browser/browser_policy_connector.h"
20 #include "components/policy/core/common/mock_configuration_policy_provider.h"
21 #include "components/policy/core/common/policy_map.h"
22 #include "components/policy/core/common/policy_types.h"
23 #include "content/public/browser/browser_thread.h"
24 #include "content/public/browser/notification_service.h"
25 #include "content/public/browser/render_view_host.h"
26 #include "content/public/test/browser_test_utils.h"
27 #include "extensions/browser/extension_host.h"
28 #include "extensions/browser/extension_prefs.h"
29 #include "extensions/browser/extension_registry.h"
30 #include "extensions/browser/extension_system.h"
31 #include "extensions/browser/notification_types.h"
32 #include "extensions/browser/updater/extension_downloader.h"
33 #include "extensions/test/extension_test_message_listener.h"
34 #include "net/url_request/test_url_request_interceptor.h"
35 #include "policy/policy_constants.h"
36 #include "testing/gmock/include/gmock/gmock.h"
38 using content::BrowserThread;
39 using extensions::Extension;
40 using extensions::ExtensionRegistry;
41 using extensions::Manifest;
42 using policy::PolicyMap;
43 using testing::Return;
44 using testing::_;
46 namespace {
48 std::string BuildForceInstallPolicyValue(const char* extension_id,
49 const char* update_url) {
50 return base::StringPrintf("%s;%s", extension_id, update_url);
53 } // namespace
55 class ExtensionManagementTest : public ExtensionBrowserTest {
56 public:
57 void SetUpInProcessBrowserTestFixture() override {
58 EXPECT_CALL(policy_provider_, IsInitializationComplete(_))
59 .WillRepeatedly(Return(true));
60 policy::BrowserPolicyConnector::SetPolicyProviderForTesting(
61 &policy_provider_);
64 protected:
65 void UpdateProviderPolicy(const PolicyMap& policy) {
66 policy_provider_.UpdateChromePolicy(policy);
67 base::RunLoop().RunUntilIdle();
70 // Helper method that returns whether the extension is at the given version.
71 // This calls version(), which must be defined in the extension's bg page,
72 // as well as asking the extension itself.
74 // Note that 'version' here means something different than the version field
75 // in the extension's manifest. We use the version as reported by the
76 // background page to test how overinstalling crx files with the same
77 // manifest version works.
78 bool IsExtensionAtVersion(const Extension* extension,
79 const std::string& expected_version) {
80 // Test that the extension's version from the manifest and reported by the
81 // background page is correct. This is to ensure that the processes are in
82 // sync with the Extension.
83 extensions::ProcessManager* manager =
84 extensions::ProcessManager::Get(browser()->profile());
85 extensions::ExtensionHost* ext_host =
86 manager->GetBackgroundHostForExtension(extension->id());
87 EXPECT_TRUE(ext_host);
88 if (!ext_host)
89 return false;
91 std::string version_from_bg;
92 bool exec = content::ExecuteScriptAndExtractString(
93 ext_host->render_view_host(), "version()", &version_from_bg);
94 EXPECT_TRUE(exec);
95 if (!exec)
96 return false;
98 if (version_from_bg != expected_version ||
99 extension->VersionString() != expected_version)
100 return false;
101 return true;
104 private:
105 policy::MockConfigurationPolicyProvider policy_provider_;
108 #if defined(OS_LINUX) || defined(OS_WIN)
109 // Times out sometimes on Linux and Win XP. http://crbug.com/89727
110 #define MAYBE_InstallSameVersion DISABLED_InstallSameVersion
111 #else
112 #define MAYBE_InstallSameVersion InstallSameVersion
113 #endif
115 // Tests that installing the same version overwrites.
116 IN_PROC_BROWSER_TEST_F(ExtensionManagementTest, MAYBE_InstallSameVersion) {
117 const Extension* extension = InstallExtension(
118 test_data_dir_.AppendASCII("install/install.crx"), 1);
119 ASSERT_TRUE(extension);
120 base::FilePath old_path = extension->path();
122 // Install an extension with the same version. The previous install should be
123 // overwritten.
124 extension = InstallExtension(
125 test_data_dir_.AppendASCII("install/install_same_version.crx"), 0);
126 ASSERT_TRUE(extension);
127 base::FilePath new_path = extension->path();
129 EXPECT_FALSE(IsExtensionAtVersion(extension, "1.0"));
130 EXPECT_NE(old_path.value(), new_path.value());
133 IN_PROC_BROWSER_TEST_F(ExtensionManagementTest, InstallOlderVersion) {
134 const Extension* extension = InstallExtension(
135 test_data_dir_.AppendASCII("install/install.crx"), 1);
136 ASSERT_TRUE(extension);
137 ASSERT_FALSE(InstallExtension(
138 test_data_dir_.AppendASCII("install/install_older_version.crx"), 0));
139 EXPECT_TRUE(IsExtensionAtVersion(extension, "1.0"));
142 IN_PROC_BROWSER_TEST_F(ExtensionManagementTest, InstallThenCancel) {
143 const Extension* extension = InstallExtension(
144 test_data_dir_.AppendASCII("install/install.crx"), 1);
145 ASSERT_TRUE(extension);
147 // Cancel this install.
148 ASSERT_FALSE(StartInstallButCancel(
149 test_data_dir_.AppendASCII("install/install_v2.crx")));
150 EXPECT_TRUE(IsExtensionAtVersion(extension, "1.0"));
153 #if defined(OS_WIN)
154 // http://crbug.com/141913
155 #define MAYBE_InstallRequiresConfirm DISABLED_InstallRequiresConfirm
156 #else
157 #define MAYBE_InstallRequiresConfirm InstallRequiresConfirm
158 #endif
159 IN_PROC_BROWSER_TEST_F(ExtensionManagementTest, MAYBE_InstallRequiresConfirm) {
160 // Installing the extension without an auto confirming UI should result in
161 // it being disabled, since good.crx has permissions that require approval.
162 ExtensionService* service = extensions::ExtensionSystem::Get(
163 browser()->profile())->extension_service();
164 std::string id = "ldnnhddmnhbkjipkidpdiheffobcpfmf";
165 ASSERT_FALSE(InstallExtension(test_data_dir_.AppendASCII("good.crx"), 0));
166 ASSERT_TRUE(service->GetExtensionById(id, true));
167 UninstallExtension(id);
169 // And the install should succeed when the permissions are accepted.
170 ASSERT_TRUE(InstallExtensionWithUIAutoConfirm(
171 test_data_dir_.AppendASCII("good.crx"), 1, browser()));
172 UninstallExtension(id);
175 // Tests that disabling and re-enabling an extension works.
176 IN_PROC_BROWSER_TEST_F(ExtensionManagementTest, DisableEnable) {
177 extensions::ProcessManager* manager =
178 extensions::ProcessManager::Get(browser()->profile());
179 ExtensionRegistry* registry = ExtensionRegistry::Get(browser()->profile());
180 const size_t size_before = registry->enabled_extensions().size();
182 // Load an extension, expect the background page to be available.
183 std::string extension_id = "bjafgdebaacbbbecmhlhpofkepfkgcpa";
184 ASSERT_TRUE(LoadExtension(
185 test_data_dir_.AppendASCII("good").AppendASCII("Extensions")
186 .AppendASCII(extension_id)
187 .AppendASCII("1.0")));
188 ASSERT_EQ(size_before + 1, registry->enabled_extensions().size());
189 EXPECT_EQ(0u, registry->disabled_extensions().size());
190 EXPECT_TRUE(manager->GetBackgroundHostForExtension(extension_id));
192 // After disabling, the background page should go away.
193 DisableExtension(extension_id);
194 EXPECT_EQ(size_before, registry->enabled_extensions().size());
195 EXPECT_EQ(1u, registry->disabled_extensions().size());
196 EXPECT_FALSE(manager->GetBackgroundHostForExtension(extension_id));
198 // And bring it back.
199 EnableExtension(extension_id);
200 EXPECT_EQ(size_before + 1, registry->enabled_extensions().size());
201 EXPECT_EQ(0u, registry->disabled_extensions().size());
202 EXPECT_TRUE(manager->GetBackgroundHostForExtension(extension_id));
205 // Used for testing notifications sent during extension updates.
206 class NotificationListener : public content::NotificationObserver {
207 public:
208 NotificationListener() : started_(false), finished_(false) {
209 int types[] = {extensions::NOTIFICATION_EXTENSION_UPDATING_STARTED,
210 extensions::NOTIFICATION_EXTENSION_UPDATE_FOUND};
211 for (size_t i = 0; i < arraysize(types); i++) {
212 registrar_.Add(
213 this, types[i], content::NotificationService::AllSources());
216 ~NotificationListener() override {}
218 bool started() { return started_; }
220 bool finished() { return finished_; }
222 const std::set<std::string>& updates() { return updates_; }
224 void Reset() {
225 started_ = false;
226 finished_ = false;
227 updates_.clear();
230 // Implements content::NotificationObserver interface.
231 void Observe(int type,
232 const content::NotificationSource& source,
233 const content::NotificationDetails& details) override {
234 switch (type) {
235 case extensions::NOTIFICATION_EXTENSION_UPDATING_STARTED: {
236 EXPECT_FALSE(started_);
237 started_ = true;
238 break;
240 case extensions::NOTIFICATION_EXTENSION_UPDATE_FOUND: {
241 const std::string& id =
242 content::Details<extensions::UpdateDetails>(details)->id;
243 updates_.insert(id);
244 break;
246 default:
247 NOTREACHED();
251 void OnFinished() {
252 EXPECT_FALSE(finished_);
253 finished_ = true;
256 private:
257 content::NotificationRegistrar registrar_;
259 // Did we see EXTENSION_UPDATING_STARTED?
260 bool started_;
262 // Did we see EXTENSION_UPDATING_FINISHED?
263 bool finished_;
265 // The set of extension id's we've seen via EXTENSION_UPDATE_FOUND.
266 std::set<std::string> updates_;
269 #if defined(OS_WIN)
270 // Fails consistently on Windows XP, see: http://crbug.com/120640.
271 #define MAYBE_AutoUpdate DISABLED_AutoUpdate
272 #else
273 // See http://crbug.com/103371 and http://crbug.com/120640.
274 #if defined(ADDRESS_SANITIZER)
275 #define MAYBE_AutoUpdate DISABLED_AutoUpdate
276 #else
277 #define MAYBE_AutoUpdate AutoUpdate
278 #endif
279 #endif
281 // Tests extension autoupdate.
282 IN_PROC_BROWSER_TEST_F(ExtensionManagementTest, MAYBE_AutoUpdate) {
283 NotificationListener notification_listener;
284 base::FilePath basedir = test_data_dir_.AppendASCII("autoupdate");
286 // Note: This interceptor gets requests on the IO thread.
287 net::LocalHostTestURLRequestInterceptor interceptor(
288 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO),
289 BrowserThread::GetBlockingPool()->GetTaskRunnerWithShutdownBehavior(
290 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN));
292 interceptor.SetResponseIgnoreQuery(
293 GURL("http://localhost/autoupdate/manifest"),
294 basedir.AppendASCII("manifest_v2.xml"));
295 interceptor.SetResponseIgnoreQuery(GURL("http://localhost/autoupdate/v2.crx"),
296 basedir.AppendASCII("v2.crx"));
298 // Install version 1 of the extension.
299 ExtensionTestMessageListener listener1("v1 installed", false);
300 ExtensionService* service = extensions::ExtensionSystem::Get(
301 browser()->profile())->extension_service();
302 ExtensionRegistry* registry = ExtensionRegistry::Get(browser()->profile());
303 const size_t size_before = registry->enabled_extensions().size();
304 ASSERT_TRUE(registry->disabled_extensions().is_empty());
305 const Extension* extension =
306 InstallExtension(basedir.AppendASCII("v1.crx"), 1);
307 ASSERT_TRUE(extension);
308 listener1.WaitUntilSatisfied();
309 ASSERT_EQ(size_before + 1, registry->enabled_extensions().size());
310 ASSERT_EQ("ogjcoiohnmldgjemafoockdghcjciccf", extension->id());
311 ASSERT_EQ("1.0", extension->VersionString());
313 extensions::ExtensionUpdater::CheckParams params;
314 params.callback =
315 base::Bind(&NotificationListener::OnFinished,
316 base::Unretained(&notification_listener));
318 // Run autoupdate and make sure version 2 of the extension was installed.
319 ExtensionTestMessageListener listener2("v2 installed", false);
320 service->updater()->CheckNow(params);
321 ASSERT_TRUE(WaitForExtensionInstall());
322 listener2.WaitUntilSatisfied();
323 ASSERT_EQ(size_before + 1, registry->enabled_extensions().size());
324 extension = service->GetExtensionById(
325 "ogjcoiohnmldgjemafoockdghcjciccf", false);
326 ASSERT_TRUE(extension);
327 ASSERT_EQ("2.0", extension->VersionString());
328 ASSERT_TRUE(notification_listener.started());
329 ASSERT_TRUE(notification_listener.finished());
330 ASSERT_TRUE(ContainsKey(notification_listener.updates(),
331 "ogjcoiohnmldgjemafoockdghcjciccf"));
332 notification_listener.Reset();
334 // Now try doing an update to version 3, which has been incorrectly
335 // signed. This should fail.
336 interceptor.SetResponseIgnoreQuery(
337 GURL("http://localhost/autoupdate/manifest"),
338 basedir.AppendASCII("manifest_v3.xml"));
339 interceptor.SetResponseIgnoreQuery(GURL("http://localhost/autoupdate/v3.crx"),
340 basedir.AppendASCII("v3.crx"));
342 service->updater()->CheckNow(params);
343 ASSERT_TRUE(WaitForExtensionInstallError());
344 ASSERT_TRUE(notification_listener.started());
345 ASSERT_TRUE(notification_listener.finished());
346 ASSERT_TRUE(ContainsKey(notification_listener.updates(),
347 "ogjcoiohnmldgjemafoockdghcjciccf"));
349 // Make sure the extension state is the same as before.
350 ASSERT_EQ(size_before + 1, registry->enabled_extensions().size());
351 extension = service->GetExtensionById(
352 "ogjcoiohnmldgjemafoockdghcjciccf", false);
353 ASSERT_TRUE(extension);
354 ASSERT_EQ("2.0", extension->VersionString());
357 #if defined(OS_WIN)
358 // Fails consistently on Windows XP, see: http://crbug.com/120640.
359 #define MAYBE_AutoUpdateDisabledExtensions DISABLED_AutoUpdateDisabledExtensions
360 #else
361 #if defined(ADDRESS_SANITIZER)
362 #define MAYBE_AutoUpdateDisabledExtensions DISABLED_AutoUpdateDisabledExtensions
363 #else
364 #define MAYBE_AutoUpdateDisabledExtensions AutoUpdateDisabledExtensions
365 #endif
366 #endif
368 // Tests extension autoupdate.
369 IN_PROC_BROWSER_TEST_F(ExtensionManagementTest,
370 MAYBE_AutoUpdateDisabledExtensions) {
371 NotificationListener notification_listener;
372 base::FilePath basedir = test_data_dir_.AppendASCII("autoupdate");
374 // Note: This interceptor gets requests on the IO thread.
375 net::LocalHostTestURLRequestInterceptor interceptor(
376 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO),
377 BrowserThread::GetBlockingPool()->GetTaskRunnerWithShutdownBehavior(
378 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN));
380 interceptor.SetResponseIgnoreQuery(
381 GURL("http://localhost/autoupdate/manifest"),
382 basedir.AppendASCII("manifest_v2.xml"));
383 interceptor.SetResponseIgnoreQuery(GURL("http://localhost/autoupdate/v2.crx"),
384 basedir.AppendASCII("v2.crx"));
386 // Install version 1 of the extension.
387 ExtensionTestMessageListener listener1("v1 installed", false);
388 ExtensionService* service = extensions::ExtensionSystem::Get(
389 browser()->profile())->extension_service();
390 ExtensionRegistry* registry = ExtensionRegistry::Get(browser()->profile());
391 const size_t enabled_size_before = registry->enabled_extensions().size();
392 const size_t disabled_size_before = registry->disabled_extensions().size();
393 const Extension* extension =
394 InstallExtension(basedir.AppendASCII("v1.crx"), 1);
395 ASSERT_TRUE(extension);
396 listener1.WaitUntilSatisfied();
397 DisableExtension(extension->id());
398 ASSERT_EQ(disabled_size_before + 1, registry->disabled_extensions().size());
399 ASSERT_EQ(enabled_size_before, registry->enabled_extensions().size());
400 ASSERT_EQ("ogjcoiohnmldgjemafoockdghcjciccf", extension->id());
401 ASSERT_EQ("1.0", extension->VersionString());
403 extensions::ExtensionUpdater::CheckParams params;
404 params.callback =
405 base::Bind(&NotificationListener::OnFinished,
406 base::Unretained(&notification_listener));
408 ExtensionTestMessageListener listener2("v2 installed", false);
409 // Run autoupdate and make sure version 2 of the extension was installed but
410 // is still disabled.
411 service->updater()->CheckNow(params);
412 ASSERT_TRUE(WaitForExtensionInstall());
413 ASSERT_EQ(disabled_size_before + 1, registry->disabled_extensions().size());
414 ASSERT_EQ(enabled_size_before, registry->enabled_extensions().size());
415 extension = service->GetExtensionById(
416 "ogjcoiohnmldgjemafoockdghcjciccf", true);
417 ASSERT_TRUE(extension);
418 ASSERT_FALSE(service->GetExtensionById(
419 "ogjcoiohnmldgjemafoockdghcjciccf", false));
420 ASSERT_EQ("2.0", extension->VersionString());
422 // The extension should have not made the callback because it is disabled.
423 // When we enabled it, it should then make the callback.
424 ASSERT_FALSE(listener2.was_satisfied());
425 EnableExtension(extension->id());
426 listener2.WaitUntilSatisfied();
427 ASSERT_TRUE(notification_listener.started());
428 ASSERT_TRUE(notification_listener.finished());
429 ASSERT_TRUE(ContainsKey(notification_listener.updates(),
430 "ogjcoiohnmldgjemafoockdghcjciccf"));
431 notification_listener.Reset();
434 IN_PROC_BROWSER_TEST_F(ExtensionManagementTest, ExternalUrlUpdate) {
435 ExtensionService* service = extensions::ExtensionSystem::Get(
436 browser()->profile())->extension_service();
437 const char kExtensionId[] = "ogjcoiohnmldgjemafoockdghcjciccf";
438 extensions::ExtensionUpdater::CheckParams params;
440 base::FilePath basedir = test_data_dir_.AppendASCII("autoupdate");
442 // Note: This interceptor gets requests on the IO thread.
443 net::LocalHostTestURLRequestInterceptor interceptor(
444 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO),
445 BrowserThread::GetBlockingPool()->GetTaskRunnerWithShutdownBehavior(
446 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN));
448 interceptor.SetResponseIgnoreQuery(
449 GURL("http://localhost/autoupdate/manifest"),
450 basedir.AppendASCII("manifest_v2.xml"));
451 interceptor.SetResponseIgnoreQuery(GURL("http://localhost/autoupdate/v2.crx"),
452 basedir.AppendASCII("v2.crx"));
454 ExtensionRegistry* registry = ExtensionRegistry::Get(browser()->profile());
455 const size_t size_before = registry->enabled_extensions().size();
456 ASSERT_TRUE(registry->disabled_extensions().is_empty());
458 extensions::PendingExtensionManager* pending_extension_manager =
459 service->pending_extension_manager();
461 // The code that reads external_extensions.json uses this method to inform
462 // the ExtensionService of an extension to download. Using the real code
463 // is race-prone, because instantating the ExtensionService starts a read
464 // of external_extensions.json before this test function starts.
466 EXPECT_TRUE(pending_extension_manager->AddFromExternalUpdateUrl(
467 kExtensionId,
468 std::string(),
469 GURL("http://localhost/autoupdate/manifest"),
470 Manifest::EXTERNAL_PREF_DOWNLOAD,
471 Extension::NO_FLAGS,
472 false));
474 // Run autoupdate and make sure version 2 of the extension was installed.
475 service->updater()->CheckNow(params);
476 ASSERT_TRUE(WaitForExtensionInstall());
477 ASSERT_EQ(size_before + 1, registry->enabled_extensions().size());
478 const Extension* extension = service->GetExtensionById(kExtensionId, false);
479 ASSERT_TRUE(extension);
480 ASSERT_EQ("2.0", extension->VersionString());
482 // Uninstalling the extension should set a pref that keeps the extension from
483 // being installed again the next time external_extensions.json is read.
485 UninstallExtension(kExtensionId);
487 extensions::ExtensionPrefs* extension_prefs =
488 extensions::ExtensionPrefs::Get(browser()->profile());
489 EXPECT_TRUE(extension_prefs->IsExternalExtensionUninstalled(kExtensionId))
490 << "Uninstalling should set kill bit on externaly installed extension.";
492 // Try to install the extension again from an external source. It should fail
493 // because of the killbit.
494 EXPECT_FALSE(pending_extension_manager->AddFromExternalUpdateUrl(
495 kExtensionId,
496 std::string(),
497 GURL("http://localhost/autoupdate/manifest"),
498 Manifest::EXTERNAL_PREF_DOWNLOAD,
499 Extension::NO_FLAGS,
500 false));
501 EXPECT_FALSE(pending_extension_manager->IsIdPending(kExtensionId))
502 << "External reinstall of a killed extension shouldn't work.";
503 EXPECT_TRUE(extension_prefs->IsExternalExtensionUninstalled(kExtensionId))
504 << "External reinstall of a killed extension should leave it killed.";
506 // Installing from non-external source.
507 ASSERT_TRUE(InstallExtension(basedir.AppendASCII("v2.crx"), 1));
509 EXPECT_FALSE(extension_prefs->IsExternalExtensionUninstalled(kExtensionId))
510 << "Reinstalling should clear the kill bit.";
512 // Uninstalling from a non-external source should not set the kill bit.
513 UninstallExtension(kExtensionId);
515 EXPECT_FALSE(extension_prefs->IsExternalExtensionUninstalled(kExtensionId))
516 << "Uninstalling non-external extension should not set kill bit.";
519 namespace {
521 const char kForceInstallNotEmptyHelp[] =
522 "A policy may already be controlling the list of force-installed "
523 "extensions. Please remove all policy settings from your computer "
524 "before running tests. E.g. from /etc/chromium/policies Linux or "
525 "from the registry on Windows, etc.";
529 // See http://crbug.com/57378 for flakiness details.
530 IN_PROC_BROWSER_TEST_F(ExtensionManagementTest, ExternalPolicyRefresh) {
531 ExtensionService* service = extensions::ExtensionSystem::Get(
532 browser()->profile())->extension_service();
533 const char kExtensionId[] = "ogjcoiohnmldgjemafoockdghcjciccf";
535 base::FilePath basedir = test_data_dir_.AppendASCII("autoupdate");
537 // Note: This interceptor gets requests on the IO thread.
538 net::LocalHostTestURLRequestInterceptor interceptor(
539 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO),
540 BrowserThread::GetBlockingPool()->GetTaskRunnerWithShutdownBehavior(
541 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN));
543 interceptor.SetResponseIgnoreQuery(
544 GURL("http://localhost/autoupdate/manifest"),
545 basedir.AppendASCII("manifest_v2.xml"));
546 interceptor.SetResponseIgnoreQuery(GURL("http://localhost/autoupdate/v2.crx"),
547 basedir.AppendASCII("v2.crx"));
549 ExtensionRegistry* registry = ExtensionRegistry::Get(browser()->profile());
550 const size_t size_before = registry->enabled_extensions().size();
551 ASSERT_TRUE(registry->disabled_extensions().is_empty());
553 ASSERT_TRUE(extensions::ExtensionManagementFactory::GetForBrowserContext(
554 browser()->profile())
555 ->GetForceInstallList()
556 ->empty())
557 << kForceInstallNotEmptyHelp;
559 base::ListValue forcelist;
560 forcelist.AppendString(BuildForceInstallPolicyValue(
561 kExtensionId, "http://localhost/autoupdate/manifest"));
562 PolicyMap policies;
563 policies.Set(policy::key::kExtensionInstallForcelist,
564 policy::POLICY_LEVEL_MANDATORY,
565 policy::POLICY_SCOPE_USER,
566 policy::POLICY_SOURCE_CLOUD,
567 forcelist.DeepCopy(),
568 NULL);
569 UpdateProviderPolicy(policies);
571 // Check if the extension got installed.
572 ASSERT_TRUE(WaitForExtensionInstall());
573 ASSERT_EQ(size_before + 1, registry->enabled_extensions().size());
574 const Extension* extension = service->GetExtensionById(kExtensionId, false);
575 ASSERT_TRUE(extension);
576 ASSERT_EQ("2.0", extension->VersionString());
577 EXPECT_EQ(Manifest::EXTERNAL_POLICY_DOWNLOAD, extension->location());
579 // Try to disable and uninstall the extension which should fail.
580 DisableExtension(kExtensionId);
581 EXPECT_EQ(size_before + 1, registry->enabled_extensions().size());
582 EXPECT_EQ(0u, registry->disabled_extensions().size());
583 UninstallExtension(kExtensionId);
584 EXPECT_EQ(size_before + 1, registry->enabled_extensions().size());
585 EXPECT_EQ(0u, registry->disabled_extensions().size());
587 // Now try to disable it through the management api, again failing.
588 ExtensionTestMessageListener listener1("ready", false);
589 ASSERT_TRUE(LoadExtension(
590 test_data_dir_.AppendASCII("management/uninstall_extension")));
591 ASSERT_TRUE(listener1.WaitUntilSatisfied());
592 EXPECT_EQ(size_before + 2, registry->enabled_extensions().size());
593 EXPECT_EQ(0u, registry->disabled_extensions().size());
595 // Check that emptying the list triggers uninstall.
596 policies.Erase(policy::key::kExtensionInstallForcelist);
597 UpdateProviderPolicy(policies);
598 EXPECT_EQ(size_before + 1, registry->enabled_extensions().size());
599 EXPECT_FALSE(service->GetExtensionById(kExtensionId, true));
602 // See http://crbug.com/103371 and http://crbug.com/120640.
603 #if defined(ADDRESS_SANITIZER) || defined(OS_WIN)
604 #define MAYBE_PolicyOverridesUserInstall DISABLED_PolicyOverridesUserInstall
605 #else
606 #define MAYBE_PolicyOverridesUserInstall PolicyOverridesUserInstall
607 #endif
609 IN_PROC_BROWSER_TEST_F(ExtensionManagementTest,
610 MAYBE_PolicyOverridesUserInstall) {
611 ExtensionService* service = extensions::ExtensionSystem::Get(
612 browser()->profile())->extension_service();
613 ExtensionRegistry* registry = ExtensionRegistry::Get(browser()->profile());
614 const char kExtensionId[] = "ogjcoiohnmldgjemafoockdghcjciccf";
615 extensions::ExtensionUpdater::CheckParams params;
616 service->updater()->set_default_check_params(params);
617 const size_t size_before = registry->enabled_extensions().size();
618 base::FilePath basedir = test_data_dir_.AppendASCII("autoupdate");
619 ASSERT_TRUE(registry->disabled_extensions().is_empty());
621 // Note: This interceptor gets requests on the IO thread.
622 net::LocalHostTestURLRequestInterceptor interceptor(
623 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO),
624 BrowserThread::GetBlockingPool()->GetTaskRunnerWithShutdownBehavior(
625 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN));
627 interceptor.SetResponseIgnoreQuery(
628 GURL("http://localhost/autoupdate/manifest"),
629 basedir.AppendASCII("manifest_v2.xml"));
630 interceptor.SetResponseIgnoreQuery(GURL("http://localhost/autoupdate/v2.crx"),
631 basedir.AppendASCII("v2.crx"));
633 // Check that the policy is initially empty.
634 ASSERT_TRUE(extensions::ExtensionManagementFactory::GetForBrowserContext(
635 browser()->profile())
636 ->GetForceInstallList()
637 ->empty())
638 << kForceInstallNotEmptyHelp;
640 // User install of the extension.
641 ASSERT_TRUE(InstallExtension(basedir.AppendASCII("v2.crx"), 1));
642 ASSERT_EQ(size_before + 1, registry->enabled_extensions().size());
643 const Extension* extension = service->GetExtensionById(kExtensionId, false);
644 ASSERT_TRUE(extension);
645 EXPECT_EQ(Manifest::INTERNAL, extension->location());
646 EXPECT_TRUE(service->IsExtensionEnabled(kExtensionId));
648 // Setup the force install policy. It should override the location.
649 base::ListValue forcelist;
650 forcelist.AppendString(BuildForceInstallPolicyValue(
651 kExtensionId, "http://localhost/autoupdate/manifest"));
652 PolicyMap policies;
653 policies.Set(policy::key::kExtensionInstallForcelist,
654 policy::POLICY_LEVEL_MANDATORY,
655 policy::POLICY_SCOPE_USER,
656 policy::POLICY_SOURCE_CLOUD,
657 forcelist.DeepCopy(),
658 NULL);
659 UpdateProviderPolicy(policies);
661 ASSERT_TRUE(WaitForExtensionInstall());
662 ASSERT_EQ(size_before + 1, registry->enabled_extensions().size());
663 extension = service->GetExtensionById(kExtensionId, false);
664 ASSERT_TRUE(extension);
665 EXPECT_EQ(Manifest::EXTERNAL_POLICY_DOWNLOAD, extension->location());
666 EXPECT_TRUE(service->IsExtensionEnabled(kExtensionId));
668 // Remove the policy, and verify that the extension was uninstalled.
669 // TODO(joaodasilva): it would be nicer if the extension was kept instead,
670 // and reverted location to INTERNAL or whatever it was before the policy
671 // was applied.
672 policies.Erase(policy::key::kExtensionInstallForcelist);
673 UpdateProviderPolicy(policies);
674 ASSERT_EQ(size_before, registry->enabled_extensions().size());
675 extension = service->GetExtensionById(kExtensionId, true);
676 EXPECT_FALSE(extension);
678 // User install again, but have it disabled too before setting the policy.
679 ASSERT_TRUE(InstallExtension(basedir.AppendASCII("v2.crx"), 1));
680 ASSERT_EQ(size_before + 1, registry->enabled_extensions().size());
681 extension = service->GetExtensionById(kExtensionId, false);
682 ASSERT_TRUE(extension);
683 EXPECT_EQ(Manifest::INTERNAL, extension->location());
684 EXPECT_TRUE(service->IsExtensionEnabled(kExtensionId));
685 EXPECT_TRUE(registry->disabled_extensions().is_empty());
687 DisableExtension(kExtensionId);
688 EXPECT_EQ(1u, registry->disabled_extensions().size());
689 extension = service->GetExtensionById(kExtensionId, true);
690 EXPECT_TRUE(extension);
691 EXPECT_FALSE(service->IsExtensionEnabled(kExtensionId));
693 // Install the policy again. It should overwrite the extension's location,
694 // and force enable it too.
695 policies.Set(policy::key::kExtensionInstallForcelist,
696 policy::POLICY_LEVEL_MANDATORY,
697 policy::POLICY_SCOPE_USER,
698 policy::POLICY_SOURCE_CLOUD,
699 forcelist.DeepCopy(),
700 NULL);
701 UpdateProviderPolicy(policies);
703 ASSERT_TRUE(WaitForExtensionInstall());
704 ASSERT_EQ(size_before + 1, registry->enabled_extensions().size());
705 extension = service->GetExtensionById(kExtensionId, false);
706 ASSERT_TRUE(extension);
707 EXPECT_EQ(Manifest::EXTERNAL_POLICY_DOWNLOAD, extension->location());
708 EXPECT_TRUE(service->IsExtensionEnabled(kExtensionId));
709 EXPECT_TRUE(registry->disabled_extensions().is_empty());