Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(63)

Side by Side Diff: chrome/browser/extensions/api/management/management_browsertest.cc

Issue 536573003: Add ExtensionManagement based ExternalLoader (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@ext-2
Patch Set: fixes to #14 Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/bind.h" 5 #include "base/bind.h"
6 #include "base/bind_helpers.h" 6 #include "base/bind_helpers.h"
7 #include "base/memory/ref_counted.h" 7 #include "base/memory/ref_counted.h"
8 #include "base/prefs/pref_service.h" 8 #include "base/message_loop/message_loop.h"
9 #include "base/prefs/scoped_user_pref_update.h" 9 #include "base/prefs/scoped_user_pref_update.h"
10 #include "base/run_loop.h"
10 #include "base/stl_util.h" 11 #include "base/stl_util.h"
12 #include "base/strings/stringprintf.h"
11 #include "chrome/browser/extensions/extension_browsertest.h" 13 #include "chrome/browser/extensions/extension_browsertest.h"
14 #include "chrome/browser/extensions/extension_management.h"
12 #include "chrome/browser/extensions/extension_service.h" 15 #include "chrome/browser/extensions/extension_service.h"
13 #include "chrome/browser/extensions/extension_test_message_listener.h" 16 #include "chrome/browser/extensions/extension_test_message_listener.h"
14 #include "chrome/browser/extensions/external_policy_loader.h"
15 #include "chrome/browser/extensions/updater/extension_downloader.h" 17 #include "chrome/browser/extensions/updater/extension_downloader.h"
16 #include "chrome/browser/extensions/updater/extension_updater.h" 18 #include "chrome/browser/extensions/updater/extension_updater.h"
17 #include "chrome/browser/profiles/profile.h" 19 #include "chrome/browser/profiles/profile.h"
18 #include "chrome/browser/ui/browser.h" 20 #include "chrome/browser/ui/browser.h"
19 #include "chrome/common/pref_names.h" 21 #include "chrome/common/pref_names.h"
20 #include "chrome/common/url_constants.h" 22 #include "chrome/common/url_constants.h"
21 #include "chrome/test/base/ui_test_utils.h" 23 #include "chrome/test/base/ui_test_utils.h"
24 #include "components/policy/core/browser/browser_policy_connector.h"
25 #include "components/policy/core/common/mock_configuration_policy_provider.h"
26 #include "components/policy/core/common/policy_map.h"
22 #include "content/public/browser/notification_service.h" 27 #include "content/public/browser/notification_service.h"
23 #include "content/public/browser/render_view_host.h" 28 #include "content/public/browser/render_view_host.h"
24 #include "content/public/test/browser_test_utils.h" 29 #include "content/public/test/browser_test_utils.h"
25 #include "content/test/net/url_request_prepackaged_interceptor.h" 30 #include "content/test/net/url_request_prepackaged_interceptor.h"
26 #include "extensions/browser/extension_host.h" 31 #include "extensions/browser/extension_host.h"
27 #include "extensions/browser/extension_prefs.h" 32 #include "extensions/browser/extension_prefs.h"
28 #include "extensions/browser/extension_registry.h" 33 #include "extensions/browser/extension_registry.h"
29 #include "extensions/browser/extension_system.h" 34 #include "extensions/browser/extension_system.h"
30 #include "extensions/browser/notification_types.h" 35 #include "extensions/browser/notification_types.h"
31 #include "extensions/browser/pref_names.h"
32 #include "net/url_request/url_fetcher.h" 36 #include "net/url_request/url_fetcher.h"
37 #include "policy/policy_constants.h"
38 #include "testing/gmock/include/gmock/gmock.h"
33 39
34 using extensions::Extension; 40 using extensions::Extension;
35 using extensions::ExtensionRegistry; 41 using extensions::ExtensionRegistry;
36 using extensions::Manifest; 42 using extensions::Manifest;
43 using policy::PolicyMap;
44 using testing::Return;
45 using testing::_;
46
47 namespace {
48
49 std::string BuildForceInstallPolicyValue(const char* extension_id,
50 const char* update_url) {
51 return base::StringPrintf("%s;%s", extension_id, update_url);
52 }
53
54 } // namespace
37 55
38 class ExtensionManagementTest : public ExtensionBrowserTest { 56 class ExtensionManagementTest : public ExtensionBrowserTest {
57 public:
58 virtual void SetUpInProcessBrowserTestFixture() OVERRIDE {
59 EXPECT_CALL(policy_provider_, IsInitializationComplete(_))
60 .WillRepeatedly(Return(true));
61 policy::BrowserPolicyConnector::SetPolicyProviderForTesting(
62 &policy_provider_);
63 }
64
65 void UpdateProviderPolicy(const PolicyMap& policy) {
Joao da Silva 2014/09/08 14:59:47 This is a helper method like the method below, so
binjin 2014/09/08 15:08:47 Done.
66 policy_provider_.UpdateChromePolicy(policy);
67 DCHECK(base::MessageLoop::current());
Joao da Silva 2014/09/08 14:59:47 This DCHECK isn't needed (nor #include message_loo
binjin 2014/09/08 15:08:47 Done.
68 base::RunLoop loop;
69 loop.RunUntilIdle();
Joao da Silva 2014/09/08 14:59:47 These 2 lines can be combined: base::RunLoop().Ru
binjin 2014/09/08 15:08:47 Done.
70 }
71
39 protected: 72 protected:
40 // Helper method that returns whether the extension is at the given version. 73 // Helper method that returns whether the extension is at the given version.
41 // This calls version(), which must be defined in the extension's bg page, 74 // This calls version(), which must be defined in the extension's bg page,
42 // as well as asking the extension itself. 75 // as well as asking the extension itself.
43 // 76 //
44 // Note that 'version' here means something different than the version field 77 // Note that 'version' here means something different than the version field
45 // in the extension's manifest. We use the version as reported by the 78 // in the extension's manifest. We use the version as reported by the
46 // background page to test how overinstalling crx files with the same 79 // background page to test how overinstalling crx files with the same
47 // manifest version works. 80 // manifest version works.
48 bool IsExtensionAtVersion(const Extension* extension, 81 bool IsExtensionAtVersion(const Extension* extension,
(...skipping 15 matching lines...) Expand all
64 ext_host->render_view_host(), "version()", &version_from_bg); 97 ext_host->render_view_host(), "version()", &version_from_bg);
65 EXPECT_TRUE(exec); 98 EXPECT_TRUE(exec);
66 if (!exec) 99 if (!exec)
67 return false; 100 return false;
68 101
69 if (version_from_bg != expected_version || 102 if (version_from_bg != expected_version ||
70 extension->VersionString() != expected_version) 103 extension->VersionString() != expected_version)
71 return false; 104 return false;
72 return true; 105 return true;
73 } 106 }
107
108 private:
109 policy::MockConfigurationPolicyProvider policy_provider_;
74 }; 110 };
75 111
76 #if defined(OS_LINUX) 112 #if defined(OS_LINUX)
77 // Times out sometimes on Linux. http://crbug.com/89727 113 // Times out sometimes on Linux. http://crbug.com/89727
78 #define MAYBE_InstallSameVersion DISABLED_InstallSameVersion 114 #define MAYBE_InstallSameVersion DISABLED_InstallSameVersion
79 #else 115 #else
80 #define MAYBE_InstallSameVersion InstallSameVersion 116 #define MAYBE_InstallSameVersion InstallSameVersion
81 #endif 117 #endif
82 118
83 // Tests that installing the same version overwrites. 119 // Tests that installing the same version overwrites.
(...skipping 417 matching lines...) Expand 10 before | Expand all | Expand 10 after
501 interceptor.SetResponseIgnoreQuery( 537 interceptor.SetResponseIgnoreQuery(
502 GURL("http://localhost/autoupdate/manifest"), 538 GURL("http://localhost/autoupdate/manifest"),
503 basedir.AppendASCII("manifest_v2.xml")); 539 basedir.AppendASCII("manifest_v2.xml"));
504 interceptor.SetResponseIgnoreQuery(GURL("http://localhost/autoupdate/v2.crx"), 540 interceptor.SetResponseIgnoreQuery(GURL("http://localhost/autoupdate/v2.crx"),
505 basedir.AppendASCII("v2.crx")); 541 basedir.AppendASCII("v2.crx"));
506 542
507 ExtensionRegistry* registry = ExtensionRegistry::Get(browser()->profile()); 543 ExtensionRegistry* registry = ExtensionRegistry::Get(browser()->profile());
508 const size_t size_before = registry->enabled_extensions().size(); 544 const size_t size_before = registry->enabled_extensions().size();
509 ASSERT_TRUE(registry->disabled_extensions().is_empty()); 545 ASSERT_TRUE(registry->disabled_extensions().is_empty());
510 546
511 PrefService* prefs = browser()->profile()->GetPrefs(); 547 ASSERT_TRUE(extensions::ExtensionManagementFactory::GetForBrowserContext(
512 const base::DictionaryValue* forcelist = 548 browser()->profile())
513 prefs->GetDictionary(extensions::pref_names::kInstallForceList); 549 ->GetForceInstallList()
514 ASSERT_TRUE(forcelist->empty()) << kForceInstallNotEmptyHelp; 550 ->empty())
551 << kForceInstallNotEmptyHelp;
515 552
516 { 553 base::ListValue forcelist;
517 // Set the policy as a user preference and fire notification observers. 554 forcelist.AppendString(BuildForceInstallPolicyValue(
518 DictionaryPrefUpdate pref_update(prefs, 555 kExtensionId, "http://localhost/autoupdate/manifest"));
519 extensions::pref_names::kInstallForceList); 556 PolicyMap policies;
520 base::DictionaryValue* forcelist = pref_update.Get(); 557 policies.Set(policy::key::kExtensionInstallForcelist,
521 extensions::ExternalPolicyLoader::AddExtension( 558 policy::POLICY_LEVEL_MANDATORY,
522 forcelist, kExtensionId, "http://localhost/autoupdate/manifest"); 559 policy::POLICY_SCOPE_USER,
523 } 560 forcelist.DeepCopy(),
561 NULL);
562 UpdateProviderPolicy(policies);
524 563
525 // Check if the extension got installed. 564 // Check if the extension got installed.
526 ASSERT_TRUE(WaitForExtensionInstall()); 565 ASSERT_TRUE(WaitForExtensionInstall());
527 ASSERT_EQ(size_before + 1, registry->enabled_extensions().size()); 566 ASSERT_EQ(size_before + 1, registry->enabled_extensions().size());
528 const Extension* extension = service->GetExtensionById(kExtensionId, false); 567 const Extension* extension = service->GetExtensionById(kExtensionId, false);
529 ASSERT_TRUE(extension); 568 ASSERT_TRUE(extension);
530 ASSERT_EQ("2.0", extension->VersionString()); 569 ASSERT_EQ("2.0", extension->VersionString());
531 EXPECT_EQ(Manifest::EXTERNAL_POLICY_DOWNLOAD, extension->location()); 570 EXPECT_EQ(Manifest::EXTERNAL_POLICY_DOWNLOAD, extension->location());
532 571
533 // Try to disable and uninstall the extension which should fail. 572 // Try to disable and uninstall the extension which should fail.
534 DisableExtension(kExtensionId); 573 DisableExtension(kExtensionId);
535 EXPECT_EQ(size_before + 1, registry->enabled_extensions().size()); 574 EXPECT_EQ(size_before + 1, registry->enabled_extensions().size());
536 EXPECT_EQ(0u, registry->disabled_extensions().size()); 575 EXPECT_EQ(0u, registry->disabled_extensions().size());
537 UninstallExtension(kExtensionId); 576 UninstallExtension(kExtensionId);
538 EXPECT_EQ(size_before + 1, registry->enabled_extensions().size()); 577 EXPECT_EQ(size_before + 1, registry->enabled_extensions().size());
539 EXPECT_EQ(0u, registry->disabled_extensions().size()); 578 EXPECT_EQ(0u, registry->disabled_extensions().size());
540 579
541 // Now try to disable it through the management api, again failing. 580 // Now try to disable it through the management api, again failing.
542 ExtensionTestMessageListener listener1("ready", false); 581 ExtensionTestMessageListener listener1("ready", false);
543 ASSERT_TRUE(LoadExtension( 582 ASSERT_TRUE(LoadExtension(
544 test_data_dir_.AppendASCII("management/uninstall_extension"))); 583 test_data_dir_.AppendASCII("management/uninstall_extension")));
545 ASSERT_TRUE(listener1.WaitUntilSatisfied()); 584 ASSERT_TRUE(listener1.WaitUntilSatisfied());
546 EXPECT_EQ(size_before + 2, registry->enabled_extensions().size()); 585 EXPECT_EQ(size_before + 2, registry->enabled_extensions().size());
547 EXPECT_EQ(0u, registry->disabled_extensions().size()); 586 EXPECT_EQ(0u, registry->disabled_extensions().size());
548 587
549 // Check that emptying the list triggers uninstall. 588 // Check that emptying the list triggers uninstall.
550 prefs->ClearPref(extensions::pref_names::kInstallForceList); 589 policies.Erase(policy::key::kExtensionInstallForcelist);
590 UpdateProviderPolicy(policies);
551 EXPECT_EQ(size_before + 1, registry->enabled_extensions().size()); 591 EXPECT_EQ(size_before + 1, registry->enabled_extensions().size());
552 EXPECT_FALSE(service->GetExtensionById(kExtensionId, true)); 592 EXPECT_FALSE(service->GetExtensionById(kExtensionId, true));
553 } 593 }
554 594
555 // See http://crbug.com/103371 and http://crbug.com/120640. 595 // See http://crbug.com/103371 and http://crbug.com/120640.
556 #if defined(ADDRESS_SANITIZER) || defined(OS_WIN) 596 #if defined(ADDRESS_SANITIZER) || defined(OS_WIN)
557 #define MAYBE_PolicyOverridesUserInstall DISABLED_PolicyOverridesUserInstall 597 #define MAYBE_PolicyOverridesUserInstall DISABLED_PolicyOverridesUserInstall
558 #else 598 #else
559 #define MAYBE_PolicyOverridesUserInstall PolicyOverridesUserInstall 599 #define MAYBE_PolicyOverridesUserInstall PolicyOverridesUserInstall
560 #endif 600 #endif
(...skipping 14 matching lines...) Expand all
575 content::URLLocalHostRequestPrepackagedInterceptor interceptor; 615 content::URLLocalHostRequestPrepackagedInterceptor interceptor;
576 net::URLFetcher::SetEnableInterceptionForTests(true); 616 net::URLFetcher::SetEnableInterceptionForTests(true);
577 617
578 interceptor.SetResponseIgnoreQuery( 618 interceptor.SetResponseIgnoreQuery(
579 GURL("http://localhost/autoupdate/manifest"), 619 GURL("http://localhost/autoupdate/manifest"),
580 basedir.AppendASCII("manifest_v2.xml")); 620 basedir.AppendASCII("manifest_v2.xml"));
581 interceptor.SetResponseIgnoreQuery(GURL("http://localhost/autoupdate/v2.crx"), 621 interceptor.SetResponseIgnoreQuery(GURL("http://localhost/autoupdate/v2.crx"),
582 basedir.AppendASCII("v2.crx")); 622 basedir.AppendASCII("v2.crx"));
583 623
584 // Check that the policy is initially empty. 624 // Check that the policy is initially empty.
585 PrefService* prefs = browser()->profile()->GetPrefs(); 625 ASSERT_TRUE(extensions::ExtensionManagementFactory::GetForBrowserContext(
586 const base::DictionaryValue* forcelist = 626 browser()->profile())
587 prefs->GetDictionary(extensions::pref_names::kInstallForceList); 627 ->GetForceInstallList()
588 ASSERT_TRUE(forcelist->empty()) << kForceInstallNotEmptyHelp; 628 ->empty())
629 << kForceInstallNotEmptyHelp;
589 630
590 // User install of the extension. 631 // User install of the extension.
591 ASSERT_TRUE(InstallExtension(basedir.AppendASCII("v2.crx"), 1)); 632 ASSERT_TRUE(InstallExtension(basedir.AppendASCII("v2.crx"), 1));
592 ASSERT_EQ(size_before + 1, registry->enabled_extensions().size()); 633 ASSERT_EQ(size_before + 1, registry->enabled_extensions().size());
593 const Extension* extension = service->GetExtensionById(kExtensionId, false); 634 const Extension* extension = service->GetExtensionById(kExtensionId, false);
594 ASSERT_TRUE(extension); 635 ASSERT_TRUE(extension);
595 EXPECT_EQ(Manifest::INTERNAL, extension->location()); 636 EXPECT_EQ(Manifest::INTERNAL, extension->location());
596 EXPECT_TRUE(service->IsExtensionEnabled(kExtensionId)); 637 EXPECT_TRUE(service->IsExtensionEnabled(kExtensionId));
597 638
598 // Setup the force install policy. It should override the location. 639 // Setup the force install policy. It should override the location.
599 { 640 base::ListValue forcelist;
600 DictionaryPrefUpdate pref_update(prefs, 641 forcelist.AppendString(BuildForceInstallPolicyValue(
601 extensions::pref_names::kInstallForceList); 642 kExtensionId, "http://localhost/autoupdate/manifest"));
602 extensions::ExternalPolicyLoader::AddExtension( 643 PolicyMap policies;
603 pref_update.Get(), kExtensionId, 644 policies.Set(policy::key::kExtensionInstallForcelist,
604 "http://localhost/autoupdate/manifest"); 645 policy::POLICY_LEVEL_MANDATORY,
605 } 646 policy::POLICY_SCOPE_USER,
647 forcelist.DeepCopy(),
648 NULL);
649 UpdateProviderPolicy(policies);
650
606 ASSERT_TRUE(WaitForExtensionInstall()); 651 ASSERT_TRUE(WaitForExtensionInstall());
607 ASSERT_EQ(size_before + 1, registry->enabled_extensions().size()); 652 ASSERT_EQ(size_before + 1, registry->enabled_extensions().size());
608 extension = service->GetExtensionById(kExtensionId, false); 653 extension = service->GetExtensionById(kExtensionId, false);
609 ASSERT_TRUE(extension); 654 ASSERT_TRUE(extension);
610 EXPECT_EQ(Manifest::EXTERNAL_POLICY_DOWNLOAD, extension->location()); 655 EXPECT_EQ(Manifest::EXTERNAL_POLICY_DOWNLOAD, extension->location());
611 EXPECT_TRUE(service->IsExtensionEnabled(kExtensionId)); 656 EXPECT_TRUE(service->IsExtensionEnabled(kExtensionId));
612 657
613 // Remove the policy, and verify that the extension was uninstalled. 658 // Remove the policy, and verify that the extension was uninstalled.
614 // TODO(joaodasilva): it would be nicer if the extension was kept instead, 659 // TODO(joaodasilva): it would be nicer if the extension was kept instead,
615 // and reverted location to INTERNAL or whatever it was before the policy 660 // and reverted location to INTERNAL or whatever it was before the policy
616 // was applied. 661 // was applied.
617 prefs->ClearPref(extensions::pref_names::kInstallForceList); 662 policies.Erase(policy::key::kExtensionInstallForcelist);
663 UpdateProviderPolicy(policies);
618 ASSERT_EQ(size_before, registry->enabled_extensions().size()); 664 ASSERT_EQ(size_before, registry->enabled_extensions().size());
619 extension = service->GetExtensionById(kExtensionId, true); 665 extension = service->GetExtensionById(kExtensionId, true);
620 EXPECT_FALSE(extension); 666 EXPECT_FALSE(extension);
621 667
622 // User install again, but have it disabled too before setting the policy. 668 // User install again, but have it disabled too before setting the policy.
623 ASSERT_TRUE(InstallExtension(basedir.AppendASCII("v2.crx"), 1)); 669 ASSERT_TRUE(InstallExtension(basedir.AppendASCII("v2.crx"), 1));
624 ASSERT_EQ(size_before + 1, registry->enabled_extensions().size()); 670 ASSERT_EQ(size_before + 1, registry->enabled_extensions().size());
625 extension = service->GetExtensionById(kExtensionId, false); 671 extension = service->GetExtensionById(kExtensionId, false);
626 ASSERT_TRUE(extension); 672 ASSERT_TRUE(extension);
627 EXPECT_EQ(Manifest::INTERNAL, extension->location()); 673 EXPECT_EQ(Manifest::INTERNAL, extension->location());
628 EXPECT_TRUE(service->IsExtensionEnabled(kExtensionId)); 674 EXPECT_TRUE(service->IsExtensionEnabled(kExtensionId));
629 EXPECT_TRUE(registry->disabled_extensions().is_empty()); 675 EXPECT_TRUE(registry->disabled_extensions().is_empty());
630 676
631 DisableExtension(kExtensionId); 677 DisableExtension(kExtensionId);
632 EXPECT_EQ(1u, registry->disabled_extensions().size()); 678 EXPECT_EQ(1u, registry->disabled_extensions().size());
633 extension = service->GetExtensionById(kExtensionId, true); 679 extension = service->GetExtensionById(kExtensionId, true);
634 EXPECT_TRUE(extension); 680 EXPECT_TRUE(extension);
635 EXPECT_FALSE(service->IsExtensionEnabled(kExtensionId)); 681 EXPECT_FALSE(service->IsExtensionEnabled(kExtensionId));
636 682
637 // Install the policy again. It should overwrite the extension's location, 683 // Install the policy again. It should overwrite the extension's location,
638 // and force enable it too. 684 // and force enable it too.
639 { 685 policies.Set(policy::key::kExtensionInstallForcelist,
640 DictionaryPrefUpdate pref_update(prefs, 686 policy::POLICY_LEVEL_MANDATORY,
641 extensions::pref_names::kInstallForceList); 687 policy::POLICY_SCOPE_USER,
642 base::DictionaryValue* forcelist = pref_update.Get(); 688 forcelist.DeepCopy(),
643 extensions::ExternalPolicyLoader::AddExtension( 689 NULL);
644 forcelist, kExtensionId, "http://localhost/autoupdate/manifest"); 690 UpdateProviderPolicy(policies);
645 } 691
646 ASSERT_TRUE(WaitForExtensionInstall()); 692 ASSERT_TRUE(WaitForExtensionInstall());
647 ASSERT_EQ(size_before + 1, registry->enabled_extensions().size()); 693 ASSERT_EQ(size_before + 1, registry->enabled_extensions().size());
648 extension = service->GetExtensionById(kExtensionId, false); 694 extension = service->GetExtensionById(kExtensionId, false);
649 ASSERT_TRUE(extension); 695 ASSERT_TRUE(extension);
650 EXPECT_EQ(Manifest::EXTERNAL_POLICY_DOWNLOAD, extension->location()); 696 EXPECT_EQ(Manifest::EXTERNAL_POLICY_DOWNLOAD, extension->location());
651 EXPECT_TRUE(service->IsExtensionEnabled(kExtensionId)); 697 EXPECT_TRUE(service->IsExtensionEnabled(kExtensionId));
652 EXPECT_TRUE(registry->disabled_extensions().is_empty()); 698 EXPECT_TRUE(registry->disabled_extensions().is_empty());
653 } 699 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698