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

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

Powered by Google App Engine
This is Rietveld 408576698