OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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 <algorithm> | 5 #include <algorithm> |
6 #include <set> | 6 #include <set> |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/at_exit.h" | 9 #include "base/at_exit.h" |
10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
(...skipping 853 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
864 | 864 |
865 void TerminateExtension(const std::string& id) { | 865 void TerminateExtension(const std::string& id) { |
866 const Extension* extension = service()->GetInstalledExtension(id); | 866 const Extension* extension = service()->GetInstalledExtension(id); |
867 if (!extension) { | 867 if (!extension) { |
868 ADD_FAILURE(); | 868 ADD_FAILURE(); |
869 return; | 869 return; |
870 } | 870 } |
871 service()->TrackTerminatedExtensionForTest(extension); | 871 service()->TrackTerminatedExtensionForTest(extension); |
872 } | 872 } |
873 | 873 |
| 874 testing::AssertionResult IsBlocked(const std::string& id) { |
| 875 scoped_ptr<extensions::ExtensionSet> all_unblocked_extensions = |
| 876 registry()->GenerateInstalledExtensionsSet( |
| 877 ExtensionRegistry::EVERYTHING & ~ExtensionRegistry::BLOCKED); |
| 878 if (all_unblocked_extensions.get()->Contains(id)) |
| 879 return testing::AssertionFailure() << id << " is still unblocked!"; |
| 880 if (!registry()->blocked_extensions().Contains(id)) |
| 881 return testing::AssertionFailure() << id << " is not blocked!"; |
| 882 return testing::AssertionSuccess(); |
| 883 } |
| 884 |
| 885 // Helper method to test that an extension moves through being blocked and |
| 886 // unblocked as appropriate for its type. |
| 887 void AssertExtensionBlocksAndUnblocks( |
| 888 bool should_block, const std::string extension_id) { |
| 889 // Assume we start in an unblocked state. |
| 890 EXPECT_FALSE(IsBlocked(extension_id)); |
| 891 |
| 892 // Block the extensions. |
| 893 service()->BlockAllExtensions(); |
| 894 base::RunLoop().RunUntilIdle(); |
| 895 |
| 896 if (should_block) |
| 897 ASSERT_TRUE(IsBlocked(extension_id)); |
| 898 else |
| 899 ASSERT_FALSE(IsBlocked(extension_id)); |
| 900 |
| 901 service()->UnblockAllExtensions(); |
| 902 base::RunLoop().RunUntilIdle(); |
| 903 |
| 904 ASSERT_FALSE(IsBlocked(extension_id)); |
| 905 } |
| 906 |
874 size_t GetPrefKeyCount() { | 907 size_t GetPrefKeyCount() { |
875 const base::DictionaryValue* dict = | 908 const base::DictionaryValue* dict = |
876 profile()->GetPrefs()->GetDictionary("extensions.settings"); | 909 profile()->GetPrefs()->GetDictionary("extensions.settings"); |
877 if (!dict) { | 910 if (!dict) { |
878 ADD_FAILURE(); | 911 ADD_FAILURE(); |
879 return 0; | 912 return 0; |
880 } | 913 } |
881 return dict->size(); | 914 return dict->size(); |
882 } | 915 } |
883 | 916 |
(...skipping 2694 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3578 service()->ReloadExtension(good2); | 3611 service()->ReloadExtension(good2); |
3579 base::RunLoop().RunUntilIdle(); | 3612 base::RunLoop().RunUntilIdle(); |
3580 | 3613 |
3581 EXPECT_EQ(StringSet(good0), registry()->enabled_extensions().GetIDs()); | 3614 EXPECT_EQ(StringSet(good0), registry()->enabled_extensions().GetIDs()); |
3582 EXPECT_EQ(StringSet(good1, good2), | 3615 EXPECT_EQ(StringSet(good1, good2), |
3583 registry()->blacklisted_extensions().GetIDs()); | 3616 registry()->blacklisted_extensions().GetIDs()); |
3584 } | 3617 } |
3585 | 3618 |
3586 #endif // defined(ENABLE_BLACKLIST_TESTS) | 3619 #endif // defined(ENABLE_BLACKLIST_TESTS) |
3587 | 3620 |
| 3621 // Tests blocking then unblocking enabled extensions after the service has been |
| 3622 // initialized. |
| 3623 TEST_F(ExtensionServiceTest, BlockAndUnblockEnabledExtension) { |
| 3624 InitializeGoodInstalledExtensionService(); |
| 3625 service()->Init(); |
| 3626 |
| 3627 AssertExtensionBlocksAndUnblocks(true, good0); |
| 3628 } |
| 3629 |
| 3630 // Tests blocking then unblocking disabled extensions after the service has been |
| 3631 // initialized. |
| 3632 TEST_F(ExtensionServiceTest, BlockAndUnblockDisabledExtension) { |
| 3633 InitializeGoodInstalledExtensionService(); |
| 3634 service()->Init(); |
| 3635 |
| 3636 service()->DisableExtension(good0, Extension::DISABLE_RELOAD); |
| 3637 |
| 3638 AssertExtensionBlocksAndUnblocks(true, good0); |
| 3639 } |
| 3640 |
| 3641 // Tests blocking then unblocking terminated extensions after the service has |
| 3642 // been initialized. |
| 3643 TEST_F(ExtensionServiceTest, BlockAndUnblockTerminatedExtension) { |
| 3644 InitializeGoodInstalledExtensionService(); |
| 3645 service()->Init(); |
| 3646 |
| 3647 TerminateExtension(good0); |
| 3648 |
| 3649 AssertExtensionBlocksAndUnblocks(true, good0); |
| 3650 } |
| 3651 |
| 3652 // Tests blocking then unblocking policy-forced extensions after the service has |
| 3653 // been initialized. |
| 3654 TEST_F(ExtensionServiceTest, BlockAndUnblockPolicyExtension) { |
| 3655 InitializeEmptyExtensionServiceWithTestingPrefs(); |
| 3656 |
| 3657 { |
| 3658 ManagementPrefUpdater pref(profile_->GetTestingPrefService()); |
| 3659 // // Blacklist everything. |
| 3660 // pref.SetBlacklistedByDefault(true); |
| 3661 // Mark good.crx for force-installation. |
| 3662 pref.SetIndividualExtensionAutoInstalled( |
| 3663 good_crx, "http://example.com/update_url", true); |
| 3664 } |
| 3665 |
| 3666 // Have policy force-install an extension. |
| 3667 MockExtensionProvider* provider = |
| 3668 new MockExtensionProvider(service(), Manifest::EXTERNAL_POLICY_DOWNLOAD); |
| 3669 AddMockExternalProvider(provider); |
| 3670 provider->UpdateOrAddExtension( |
| 3671 good_crx, "1.0.0.0", data_dir().AppendASCII("good_crx")); |
| 3672 |
| 3673 // Reloading extensions should find our externally registered extension |
| 3674 // and install it. |
| 3675 content::WindowedNotificationObserver observer( |
| 3676 extensions::NOTIFICATION_CRX_INSTALLER_DONE, |
| 3677 content::NotificationService::AllSources()); |
| 3678 service()->CheckForExternalUpdates(); |
| 3679 observer.Wait(); |
| 3680 |
| 3681 AssertExtensionBlocksAndUnblocks(false, good_crx); |
| 3682 } |
| 3683 |
| 3684 |
| 3685 #if defined(ENABLE_BLACKLIST_TESTS) |
| 3686 // Tests blocking then unblocking extensions that are blacklisted both before |
| 3687 // and after Init(). |
| 3688 TEST_F(ExtensionServiceTest, BlockAndUnblockBlacklistedExtension) { |
| 3689 extensions::TestBlacklist test_blacklist; |
| 3690 |
| 3691 InitializeGoodInstalledExtensionService(); |
| 3692 test_blacklist.Attach(service()->blacklist_); |
| 3693 |
| 3694 test_blacklist.SetBlacklistState( |
| 3695 good0, extensions::BLACKLISTED_MALWARE, true); |
| 3696 base::RunLoop().RunUntilIdle(); |
| 3697 |
| 3698 service()->Init(); |
| 3699 |
| 3700 test_blacklist.SetBlacklistState( |
| 3701 good1, extensions::BLACKLISTED_MALWARE, true); |
| 3702 base::RunLoop().RunUntilIdle(); |
| 3703 |
| 3704 // Blacklisted extensions stay blacklisted. |
| 3705 AssertExtensionBlocksAndUnblocks(false, good0); |
| 3706 AssertExtensionBlocksAndUnblocks(false, good1); |
| 3707 |
| 3708 service()->BlockAllExtensions(); |
| 3709 |
| 3710 // Remove an extension from the blacklist while the service is blocked. |
| 3711 test_blacklist.SetBlacklistState( |
| 3712 good0, extensions::NOT_BLACKLISTED, true); |
| 3713 // Add an extension to the blacklist while the service is blocked. |
| 3714 test_blacklist.SetBlacklistState( |
| 3715 good2, extensions::BLACKLISTED_MALWARE, true); |
| 3716 base::RunLoop().RunUntilIdle(); |
| 3717 |
| 3718 // Go directly to blocked, do not pass go, do not collect $200. |
| 3719 ASSERT_TRUE(IsBlocked(good0)); |
| 3720 // Get on the blacklist - even if you were blocked! |
| 3721 ASSERT_FALSE(IsBlocked(good2)); |
| 3722 } |
| 3723 #endif // defined(ENABLE_BLACKLIST_TESTS) |
| 3724 |
| 3725 // Tests blocking then unblocking enabled component extensions after the service |
| 3726 // has been initialized. |
| 3727 TEST_F(ExtensionServiceTest, BlockAndUnblockEnabledComponentExtension) { |
| 3728 InitializeEmptyExtensionServiceWithTestingPrefs(); |
| 3729 |
| 3730 // Install a component extension. |
| 3731 base::FilePath path = data_dir() |
| 3732 .AppendASCII("good") |
| 3733 .AppendASCII("Extensions") |
| 3734 .AppendASCII(good0) |
| 3735 .AppendASCII("1.0.0.0"); |
| 3736 std::string manifest; |
| 3737 ASSERT_TRUE(base::ReadFileToString( |
| 3738 path.Append(extensions::kManifestFilename), &manifest)); |
| 3739 service()->component_loader()->Add(manifest, path); |
| 3740 service()->Init(); |
| 3741 |
| 3742 // Component extension should never block. |
| 3743 AssertExtensionBlocksAndUnblocks(false, good0); |
| 3744 } |
| 3745 |
| 3746 // Tests blocking then unblocking a theme after the service has been |
| 3747 // initialized. |
| 3748 TEST_F(ExtensionServiceTest, BlockAndUnblockTheme) { |
| 3749 InitializeEmptyExtensionService(); |
| 3750 service()->Init(); |
| 3751 |
| 3752 base::FilePath path = data_dir().AppendASCII("theme.crx"); |
| 3753 InstallCRX(path, INSTALL_NEW); |
| 3754 |
| 3755 AssertExtensionBlocksAndUnblocks(true, theme_crx); |
| 3756 } |
| 3757 |
| 3758 // Tests that blocking extensions before Init() results in loading blocked |
| 3759 // extensions. |
| 3760 TEST_F(ExtensionServiceTest, WillNotLoadExtensionsWhenBlocked) { |
| 3761 InitializeGoodInstalledExtensionService(); |
| 3762 |
| 3763 service()->BlockAllExtensions(); |
| 3764 |
| 3765 service()->Init(); |
| 3766 |
| 3767 ASSERT_TRUE(IsBlocked(good0)); |
| 3768 ASSERT_TRUE(IsBlocked(good0)); |
| 3769 ASSERT_TRUE(IsBlocked(good0)); |
| 3770 } |
| 3771 |
3588 // Will not install extension blacklisted by policy. | 3772 // Will not install extension blacklisted by policy. |
3589 TEST_F(ExtensionServiceTest, BlacklistedByPolicyWillNotInstall) { | 3773 TEST_F(ExtensionServiceTest, BlacklistedByPolicyWillNotInstall) { |
3590 InitializeEmptyExtensionServiceWithTestingPrefs(); | 3774 InitializeEmptyExtensionServiceWithTestingPrefs(); |
3591 | 3775 |
3592 // Blacklist everything. | 3776 // Blacklist everything. |
3593 { | 3777 { |
3594 ManagementPrefUpdater pref(profile_->GetTestingPrefService()); | 3778 ManagementPrefUpdater pref(profile_->GetTestingPrefService()); |
3595 pref.SetBlacklistedByDefault(true); | 3779 pref.SetBlacklistedByDefault(true); |
3596 } | 3780 } |
3597 | 3781 |
(...skipping 3783 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
7381 | 7565 |
7382 service()->Observe(chrome::NOTIFICATION_PROFILE_DESTRUCTION_STARTED, | 7566 service()->Observe(chrome::NOTIFICATION_PROFILE_DESTRUCTION_STARTED, |
7383 content::Source<Profile>(profile()), | 7567 content::Source<Profile>(profile()), |
7384 content::NotificationService::NoDetails()); | 7568 content::NotificationService::NoDetails()); |
7385 EXPECT_EQ(UnloadedExtensionInfo::REASON_PROFILE_SHUTDOWN, unloaded_reason_); | 7569 EXPECT_EQ(UnloadedExtensionInfo::REASON_PROFILE_SHUTDOWN, unloaded_reason_); |
7386 EXPECT_EQ(0u, registry()->enabled_extensions().size()); | 7570 EXPECT_EQ(0u, registry()->enabled_extensions().size()); |
7387 EXPECT_EQ(0u, registry()->disabled_extensions().size()); | 7571 EXPECT_EQ(0u, registry()->disabled_extensions().size()); |
7388 EXPECT_EQ(0u, registry()->terminated_extensions().size()); | 7572 EXPECT_EQ(0u, registry()->terminated_extensions().size()); |
7389 EXPECT_EQ(0u, registry()->blacklisted_extensions().size()); | 7573 EXPECT_EQ(0u, registry()->blacklisted_extensions().size()); |
7390 } | 7574 } |
OLD | NEW |