OLD | NEW |
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 "chrome/browser/extensions/extension_service_unittest.h" | 5 #include "chrome/browser/extensions/extension_service_unittest.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <set> | 8 #include <set> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
(...skipping 608 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
619 installer->set_creation_flags(creation_flags); | 619 installer->set_creation_flags(creation_flags); |
620 if (!(creation_flags & Extension::WAS_INSTALLED_BY_DEFAULT)) { | 620 if (!(creation_flags & Extension::WAS_INSTALLED_BY_DEFAULT)) { |
621 installer->set_allow_silent_install(true); | 621 installer->set_allow_silent_install(true); |
622 } | 622 } |
623 installer->InstallCrx(crx_path); | 623 installer->InstallCrx(crx_path); |
624 } | 624 } |
625 | 625 |
626 enum InstallState { | 626 enum InstallState { |
627 INSTALL_FAILED, | 627 INSTALL_FAILED, |
628 INSTALL_UPDATED, | 628 INSTALL_UPDATED, |
629 INSTALL_NEW | 629 INSTALL_NEW, |
| 630 INSTALL_WITHOUT_LOAD, |
630 }; | 631 }; |
631 | 632 |
632 const Extension* PackAndInstallCRX(const FilePath& dir_path, | 633 const Extension* PackAndInstallCRX(const FilePath& dir_path, |
633 const FilePath& pem_path, | 634 const FilePath& pem_path, |
634 InstallState install_state, | 635 InstallState install_state, |
635 int creation_flags) { | 636 int creation_flags) { |
636 FilePath crx_path; | 637 FilePath crx_path; |
637 base::ScopedTempDir temp_dir; | 638 base::ScopedTempDir temp_dir; |
638 EXPECT_TRUE(temp_dir.CreateUniqueTempDir()); | 639 EXPECT_TRUE(temp_dir.CreateUniqueTempDir()); |
639 crx_path = temp_dir.path().AppendASCII("temp.crx"); | 640 crx_path = temp_dir.path().AppendASCII("temp.crx"); |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
694 const Extension* WaitForCrxInstall(const FilePath& path, | 695 const Extension* WaitForCrxInstall(const FilePath& path, |
695 InstallState install_state) { | 696 InstallState install_state) { |
696 loop_.RunUntilIdle(); | 697 loop_.RunUntilIdle(); |
697 std::vector<string16> errors = GetErrors(); | 698 std::vector<string16> errors = GetErrors(); |
698 const Extension* extension = NULL; | 699 const Extension* extension = NULL; |
699 if (install_state != INSTALL_FAILED) { | 700 if (install_state != INSTALL_FAILED) { |
700 if (install_state == INSTALL_NEW) | 701 if (install_state == INSTALL_NEW) |
701 ++expected_extensions_count_; | 702 ++expected_extensions_count_; |
702 | 703 |
703 EXPECT_TRUE(installed_) << path.value(); | 704 EXPECT_TRUE(installed_) << path.value(); |
| 705 EXPECT_EQ(0u, errors.size()) << path.value(); |
704 | 706 |
705 EXPECT_EQ(1u, loaded_.size()) << path.value(); | 707 if (install_state == INSTALL_WITHOUT_LOAD) { |
706 EXPECT_EQ(0u, errors.size()) << path.value(); | 708 EXPECT_EQ(0u, loaded_.size()) << path.value(); |
707 EXPECT_EQ(expected_extensions_count_, service_->extensions()->size()) << | 709 } else { |
708 path.value(); | 710 EXPECT_EQ(1u, loaded_.size()) << path.value(); |
709 extension = loaded_[0]; | 711 EXPECT_EQ(expected_extensions_count_, service_->extensions()->size()) << |
710 EXPECT_TRUE(service_->GetExtensionById(extension->id(), false)) << | 712 path.value(); |
711 path.value(); | 713 extension = loaded_[0]; |
| 714 EXPECT_TRUE(service_->GetExtensionById(extension->id(), false)) << |
| 715 path.value(); |
| 716 } |
| 717 |
712 for (std::vector<string16>::iterator err = errors.begin(); | 718 for (std::vector<string16>::iterator err = errors.begin(); |
713 err != errors.end(); ++err) { | 719 err != errors.end(); ++err) { |
714 LOG(ERROR) << *err; | 720 LOG(ERROR) << *err; |
715 } | 721 } |
716 } else { | 722 } else { |
717 EXPECT_FALSE(installed_) << path.value(); | 723 EXPECT_FALSE(installed_) << path.value(); |
718 EXPECT_EQ(0u, loaded_.size()) << path.value(); | 724 EXPECT_EQ(0u, loaded_.size()) << path.value(); |
719 EXPECT_EQ(1u, errors.size()) << path.value(); | 725 EXPECT_EQ(1u, errors.size()) << path.value(); |
720 } | 726 } |
721 | 727 |
(...skipping 2269 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2991 "v1"); | 2997 "v1"); |
2992 | 2998 |
2993 // Make sure pref is updated | 2999 // Make sure pref is updated |
2994 loop_.RunUntilIdle(); | 3000 loop_.RunUntilIdle(); |
2995 | 3001 |
2996 // Now, the good_crx is blacklisted. | 3002 // Now, the good_crx is blacklisted. |
2997 ValidateBooleanPref(good_crx, "blacklist", true); | 3003 ValidateBooleanPref(good_crx, "blacklist", true); |
2998 | 3004 |
2999 // We can not install good_crx. | 3005 // We can not install good_crx. |
3000 FilePath path = data_dir_.AppendASCII("good.crx"); | 3006 FilePath path = data_dir_.AppendASCII("good.crx"); |
3001 InstallCRX(path, INSTALL_FAILED); | 3007 InstallCRX(path, INSTALL_WITHOUT_LOAD); |
3002 EXPECT_EQ(0u, service_->extensions()->size()); | 3008 EXPECT_EQ(0u, service_->extensions()->size()); |
3003 ValidateBooleanPref(good_crx, "blacklist", true); | 3009 ValidateBooleanPref(good_crx, "blacklist", true); |
3004 } | 3010 } |
3005 | 3011 |
3006 // Unload blacklisted extension on policy change. | 3012 // Unload blacklisted extension on policy change. |
3007 TEST_F(ExtensionServiceTest, UnloadBlacklistedExtensionPolicy) { | 3013 TEST_F(ExtensionServiceTest, UnloadBlacklistedExtensionPolicy) { |
3008 InitializeEmptyExtensionService(); | 3014 InitializeEmptyExtensionService(); |
3009 FilePath path = data_dir_.AppendASCII("good.crx"); | 3015 FilePath path = data_dir_.AppendASCII("good.crx"); |
3010 | 3016 |
3011 const Extension* good = InstallCRX(path, INSTALL_NEW); | 3017 const Extension* good = InstallCRX(path, INSTALL_NEW); |
3012 EXPECT_EQ(good_crx, good->id()); | 3018 EXPECT_EQ(good_crx, good->id()); |
3013 UpdateExtension(good_crx, path, FAILED_SILENTLY); | 3019 UpdateExtension(good_crx, path, FAILED_SILENTLY); |
3014 EXPECT_EQ(1u, service_->extensions()->size()); | 3020 EXPECT_EQ(1u, service_->extensions()->size()); |
3015 | 3021 |
3016 base::ListValue whitelist; | 3022 base::ListValue whitelist; |
3017 PrefService* prefs = service_->extension_prefs()->pref_service(); | 3023 PrefService* prefs = service_->extension_prefs()->pref_service(); |
3018 whitelist.Append(base::Value::CreateStringValue(good_crx)); | 3024 whitelist.Append(base::Value::CreateStringValue(good_crx)); |
3019 prefs->Set(prefs::kExtensionInstallAllowList, whitelist); | 3025 prefs->Set(prefs::kExtensionInstallAllowList, whitelist); |
3020 | 3026 |
3021 std::vector<std::string> blacklist; | 3027 std::vector<std::string> blacklist; |
3022 blacklist.push_back(good_crx); | 3028 blacklist.push_back(good_crx); |
3023 ExtensionSystem::Get(profile_.get())->blacklist()->SetFromUpdater(blacklist, | 3029 ExtensionSystem::Get(profile_.get())->blacklist()->SetFromUpdater(blacklist, |
3024 "v1"); | 3030 "v1"); |
3025 | 3031 |
3026 // Make sure pref is updated | 3032 // Make sure pref is updated |
| 3033 loop_.RunUntilIdle(); |
3027 | 3034 |
3028 // Now, the good_crx is blacklisted but whitelist negates it. | 3035 // The good_crx is blacklisted and the whitelist doesn't negate it. |
3029 ValidateBooleanPref(good_crx, "blacklist", true); | |
3030 EXPECT_EQ(1u, service_->extensions()->size()); | |
3031 | |
3032 whitelist.Clear(); | |
3033 prefs->Set(prefs::kExtensionInstallAllowList, whitelist); | |
3034 | |
3035 // Now, the good_crx is blacklisted for good. | |
3036 ValidateBooleanPref(good_crx, "blacklist", true); | 3036 ValidateBooleanPref(good_crx, "blacklist", true); |
3037 EXPECT_EQ(0u, service_->extensions()->size()); | 3037 EXPECT_EQ(0u, service_->extensions()->size()); |
3038 } | 3038 } |
3039 | 3039 |
3040 // Allow Google-blacklisted extension if policy explicitly allows it (blacklist | |
3041 // then set policy). | |
3042 TEST_F(ExtensionServiceTest, WhitelistGoogleBlacklistedExtension) { | |
3043 InitializeEmptyExtensionService(); | |
3044 | |
3045 std::vector<std::string> blacklist; | |
3046 blacklist.push_back(good_crx); | |
3047 ExtensionSystem::Get(profile_.get())->blacklist()->SetFromUpdater(blacklist, | |
3048 "v1"); | |
3049 | |
3050 FilePath path = data_dir_.AppendASCII("good.crx"); | |
3051 InstallCRX(path, INSTALL_FAILED); | |
3052 | |
3053 base::ListValue whitelist; | |
3054 whitelist.Append(base::Value::CreateStringValue(good_crx)); | |
3055 service_->extension_prefs()->pref_service()->Set( | |
3056 prefs::kExtensionInstallAllowList, whitelist); | |
3057 | |
3058 InstallCRX(path, INSTALL_NEW); | |
3059 } | |
3060 | |
3061 // Allow Google-blacklisted extension if policy requires it (blacklist then set | |
3062 // policy). | |
3063 TEST_F(ExtensionServiceTest, ForcelistGoogleBlacklistedExtension) { | |
3064 InitializeEmptyExtensionService(); | |
3065 | |
3066 std::vector<std::string> blacklist; | |
3067 blacklist.push_back(good_crx); | |
3068 ExtensionSystem::Get(profile_.get())->blacklist()->SetFromUpdater(blacklist, | |
3069 "v1"); | |
3070 | |
3071 FilePath path = data_dir_.AppendASCII("good.crx"); | |
3072 InstallCRX(path, INSTALL_FAILED); | |
3073 | |
3074 base::ListValue forcelist; | |
3075 forcelist.Append(base::Value::CreateStringValue(good_crx)); | |
3076 service_->extension_prefs()->pref_service()->Set( | |
3077 prefs::kExtensionInstallAllowList, forcelist); | |
3078 | |
3079 InstallCRX(path, INSTALL_NEW); | |
3080 } | |
3081 | |
3082 // Allow Google-blacklisted extension if policy explicitly allows it (set policy | |
3083 // then blacklist). | |
3084 TEST_F(ExtensionServiceTest, GoogleBlacklistWhitelistedExtension) { | |
3085 InitializeEmptyExtensionService(); | |
3086 | |
3087 base::ListValue whitelist; | |
3088 whitelist.Append(base::Value::CreateStringValue(good_crx)); | |
3089 service_->extension_prefs()->pref_service()->Set( | |
3090 prefs::kExtensionInstallAllowList, whitelist); | |
3091 | |
3092 std::vector<std::string> blacklist; | |
3093 blacklist.push_back(good_crx); | |
3094 ExtensionSystem::Get(profile_.get())->blacklist()->SetFromUpdater(blacklist, | |
3095 "v1"); | |
3096 | |
3097 InstallCRX(data_dir_.AppendASCII("good.crx"), INSTALL_NEW); | |
3098 } | |
3099 | |
3100 // Allow Google-blacklisted extension if policy requires it (set policy then | |
3101 // blacklist). | |
3102 TEST_F(ExtensionServiceTest, GoogleBlacklistForcelistedExtension) { | |
3103 InitializeEmptyExtensionService(); | |
3104 | |
3105 base::ListValue forcelist; | |
3106 forcelist.Append(base::Value::CreateStringValue(good_crx)); | |
3107 service_->extension_prefs()->pref_service()->Set( | |
3108 prefs::kExtensionInstallAllowList, forcelist); | |
3109 | |
3110 std::vector<std::string> blacklist; | |
3111 blacklist.push_back(good_crx); | |
3112 ExtensionSystem::Get(profile_.get())->blacklist()->SetFromUpdater(blacklist, | |
3113 "v1"); | |
3114 | |
3115 InstallCRX(data_dir_.AppendASCII("good.crx"), INSTALL_NEW); | |
3116 } | |
3117 | |
3118 // Test loading extensions from the profile directory, except | 3040 // Test loading extensions from the profile directory, except |
3119 // blacklisted ones. | 3041 // blacklisted ones. |
3120 TEST_F(ExtensionServiceTest, WillNotLoadBlacklistedExtensionsFromDirectory) { | 3042 TEST_F(ExtensionServiceTest, WillNotLoadBlacklistedExtensionsFromDirectory) { |
3121 // Initialize the test dir with a good Preferences/extensions. | 3043 // Initialize the test dir with a good Preferences/extensions. |
3122 FilePath source_install_dir = data_dir_ | 3044 FilePath source_install_dir = data_dir_ |
3123 .AppendASCII("good") | 3045 .AppendASCII("good") |
3124 .AppendASCII("Extensions"); | 3046 .AppendASCII("Extensions"); |
3125 FilePath pref_path = source_install_dir | 3047 FilePath pref_path = source_install_dir |
3126 .DirName() | 3048 .DirName() |
3127 .AppendASCII("Preferences"); | 3049 .AppendASCII("Preferences"); |
(...skipping 13 matching lines...) Expand all Loading... |
3141 // Load extensions. | 3063 // Load extensions. |
3142 service_->Init(); | 3064 service_->Init(); |
3143 | 3065 |
3144 std::vector<string16> errors = GetErrors(); | 3066 std::vector<string16> errors = GetErrors(); |
3145 for (std::vector<string16>::iterator err = errors.begin(); | 3067 for (std::vector<string16>::iterator err = errors.begin(); |
3146 err != errors.end(); ++err) { | 3068 err != errors.end(); ++err) { |
3147 LOG(ERROR) << *err; | 3069 LOG(ERROR) << *err; |
3148 } | 3070 } |
3149 ASSERT_EQ(2u, loaded_.size()); | 3071 ASSERT_EQ(2u, loaded_.size()); |
3150 | 3072 |
3151 EXPECT_FALSE(service_->GetExtensionById(good1, true)); | 3073 EXPECT_TRUE(service_->GetInstalledExtension(good1)); |
| 3074 int include_mask = ExtensionService::INCLUDE_EVERYTHING & |
| 3075 ~ExtensionService::INCLUDE_BLACKLISTED; |
| 3076 EXPECT_FALSE(service_->GetExtensionById(good1, include_mask)); |
3152 } | 3077 } |
3153 | 3078 |
3154 // Will not install extension blacklisted by policy. | 3079 // Will not install extension blacklisted by policy. |
3155 TEST_F(ExtensionServiceTest, BlacklistedByPolicyWillNotInstall) { | 3080 TEST_F(ExtensionServiceTest, BlacklistedByPolicyWillNotInstall) { |
3156 InitializeEmptyExtensionService(); | 3081 InitializeEmptyExtensionService(); |
3157 | 3082 |
3158 // Blacklist everything. | 3083 // Blacklist everything. |
3159 { | 3084 { |
3160 ListPrefUpdate update(profile_->GetPrefs(), | 3085 ListPrefUpdate update(profile_->GetPrefs(), |
3161 prefs::kExtensionInstallDenyList); | 3086 prefs::kExtensionInstallDenyList); |
(...skipping 2688 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
5850 EXPECT_FALSE(extensions::HasExternalInstallError(service_)); | 5775 EXPECT_FALSE(extensions::HasExternalInstallError(service_)); |
5851 EXPECT_FALSE(service_->IsExtensionEnabled(good_crx)); | 5776 EXPECT_FALSE(service_->IsExtensionEnabled(good_crx)); |
5852 EXPECT_TRUE(service_->IsExtensionEnabled(page_action)); | 5777 EXPECT_TRUE(service_->IsExtensionEnabled(page_action)); |
5853 | 5778 |
5854 ExtensionPrefs* prefs = service_->extension_prefs(); | 5779 ExtensionPrefs* prefs = service_->extension_prefs(); |
5855 EXPECT_NE(0, prefs->GetDisableReasons(good_crx) & | 5780 EXPECT_NE(0, prefs->GetDisableReasons(good_crx) & |
5856 Extension::DISABLE_SIDELOAD_WIPEOUT); | 5781 Extension::DISABLE_SIDELOAD_WIPEOUT); |
5857 EXPECT_EQ(0, prefs->GetDisableReasons(page_action) & | 5782 EXPECT_EQ(0, prefs->GetDisableReasons(page_action) & |
5858 Extension::DISABLE_SIDELOAD_WIPEOUT); | 5783 Extension::DISABLE_SIDELOAD_WIPEOUT); |
5859 } | 5784 } |
OLD | NEW |