| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/extensions_service_unittest.h" | 5 #include "chrome/browser/extensions/extensions_service_unittest.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 91 } | 91 } |
| 92 } | 92 } |
| 93 | 93 |
| 94 // The tests rely on the errors being in a certain order, which can vary | 94 // The tests rely on the errors being in a certain order, which can vary |
| 95 // depending on how filesystem iteration works. | 95 // depending on how filesystem iteration works. |
| 96 std::stable_sort(ret_val.begin(), ret_val.end()); | 96 std::stable_sort(ret_val.begin(), ret_val.end()); |
| 97 | 97 |
| 98 return ret_val; | 98 return ret_val; |
| 99 } | 99 } |
| 100 | 100 |
| 101 static void AddPattern(ExtensionExtent* extent, const std::string& pattern) { |
| 102 int schemes = URLPattern::SCHEME_ALL; |
| 103 extent->AddPattern(URLPattern(schemes, pattern)); |
| 104 } |
| 105 |
| 106 static void AssertEqualExtents(ExtensionExtent* extent1, |
| 107 ExtensionExtent* extent2) { |
| 108 std::vector<URLPattern> patterns1 = extent1->patterns(); |
| 109 std::vector<URLPattern> patterns2 = extent2->patterns(); |
| 110 std::set<std::string> strings1; |
| 111 EXPECT_EQ(patterns1.size(), patterns2.size()); |
| 112 |
| 113 for (size_t i = 0; i < patterns1.size(); ++i) |
| 114 strings1.insert(patterns1.at(i).GetAsString()); |
| 115 |
| 116 std::set<std::string> strings2; |
| 117 for (size_t i = 0; i < patterns2.size(); ++i) |
| 118 strings2.insert(patterns2.at(i).GetAsString()); |
| 119 |
| 120 EXPECT_EQ(strings1, strings2); |
| 121 } |
| 122 |
| 101 } // namespace | 123 } // namespace |
| 102 | 124 |
| 103 class MockExtensionProvider : public ExternalExtensionProvider { | 125 class MockExtensionProvider : public ExternalExtensionProvider { |
| 104 public: | 126 public: |
| 105 explicit MockExtensionProvider(Extension::Location location) | 127 explicit MockExtensionProvider(Extension::Location location) |
| 106 : location_(location), visit_count_(0) {} | 128 : location_(location), visit_count_(0) {} |
| 107 virtual ~MockExtensionProvider() {} | 129 virtual ~MockExtensionProvider() {} |
| 108 | 130 |
| 109 void UpdateOrAddExtension(const std::string& id, | 131 void UpdateOrAddExtension(const std::string& id, |
| 110 const std::string& version, | 132 const std::string& version, |
| (...skipping 536 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 647 ASSERT_TRUE(dict != NULL) << msg; | 669 ASSERT_TRUE(dict != NULL) << msg; |
| 648 DictionaryValue* pref = NULL; | 670 DictionaryValue* pref = NULL; |
| 649 std::string manifest_path = extension_id + ".manifest"; | 671 std::string manifest_path = extension_id + ".manifest"; |
| 650 ASSERT_TRUE(dict->GetDictionary(manifest_path, &pref)) << msg; | 672 ASSERT_TRUE(dict->GetDictionary(manifest_path, &pref)) << msg; |
| 651 EXPECT_TRUE(pref != NULL) << msg; | 673 EXPECT_TRUE(pref != NULL) << msg; |
| 652 std::string val; | 674 std::string val; |
| 653 ASSERT_TRUE(pref->GetString(pref_path, &val)) << msg; | 675 ASSERT_TRUE(pref->GetString(pref_path, &val)) << msg; |
| 654 EXPECT_EQ(expected_val, val) << msg; | 676 EXPECT_EQ(expected_val, val) << msg; |
| 655 } | 677 } |
| 656 | 678 |
| 679 void SetPref(const std::string& extension_id, |
| 680 const std::string& pref_path, |
| 681 Value* value, |
| 682 const std::string& msg) { |
| 683 const DictionaryValue* dict = |
| 684 profile_->GetPrefs()->GetMutableDictionary("extensions.settings"); |
| 685 ASSERT_TRUE(dict != NULL) << msg; |
| 686 DictionaryValue* pref = NULL; |
| 687 ASSERT_TRUE(dict->GetDictionary(extension_id, &pref)) << msg; |
| 688 EXPECT_TRUE(pref != NULL) << msg; |
| 689 pref->Set(pref_path, value); |
| 690 } |
| 691 |
| 657 void SetPrefInteg(const std::string& extension_id, | 692 void SetPrefInteg(const std::string& extension_id, |
| 658 const std::string& pref_path, | 693 const std::string& pref_path, |
| 659 int value) { | 694 int value) { |
| 660 std::string msg = " while setting: "; | 695 std::string msg = " while setting: "; |
| 661 msg += extension_id; | 696 msg += extension_id; |
| 662 msg += " "; | 697 msg += " "; |
| 663 msg += pref_path; | 698 msg += pref_path; |
| 664 msg += " = "; | 699 msg += " = "; |
| 665 msg += base::IntToString(value); | 700 msg += base::IntToString(value); |
| 666 | 701 |
| 702 SetPref(extension_id, pref_path, Value::CreateIntegerValue(value), msg); |
| 703 } |
| 704 |
| 705 void SetPrefBool(const std::string& extension_id, |
| 706 const std::string& pref_path, |
| 707 bool value) { |
| 708 std::string msg = " while setting: "; |
| 709 msg += extension_id + " " + pref_path; |
| 710 msg += " = "; |
| 711 msg += (value ? "true" : "false"); |
| 712 |
| 713 SetPref(extension_id, pref_path, Value::CreateBooleanValue(value), msg); |
| 714 } |
| 715 |
| 716 void ClearPref(const std::string& extension_id, |
| 717 const std::string& pref_path) { |
| 718 std::string msg = " while clearing: "; |
| 719 msg += extension_id + " " + pref_path; |
| 720 |
| 667 const DictionaryValue* dict = | 721 const DictionaryValue* dict = |
| 668 profile_->GetPrefs()->GetMutableDictionary("extensions.settings"); | 722 profile_->GetPrefs()->GetMutableDictionary("extensions.settings"); |
| 669 ASSERT_TRUE(dict != NULL) << msg; | 723 ASSERT_TRUE(dict != NULL) << msg; |
| 670 DictionaryValue* pref = NULL; | 724 DictionaryValue* pref = NULL; |
| 671 ASSERT_TRUE(dict->GetDictionary(extension_id, &pref)) << msg; | 725 ASSERT_TRUE(dict->GetDictionary(extension_id, &pref)) << msg; |
| 672 EXPECT_TRUE(pref != NULL) << msg; | 726 EXPECT_TRUE(pref != NULL) << msg; |
| 673 pref->SetInteger(pref_path, value); | 727 pref->Remove(pref_path, NULL); |
| 728 } |
| 729 |
| 730 void SetPrefStringSet(const std::string& extension_id, |
| 731 const std::string& pref_path, |
| 732 const std::set<std::string>& value) { |
| 733 std::string msg = " while setting: "; |
| 734 msg += extension_id + " " + pref_path; |
| 735 |
| 736 ListValue* list_value = new ListValue(); |
| 737 for (std::set<std::string>::const_iterator iter = value.begin(); |
| 738 iter != value.end(); ++iter) |
| 739 list_value->Append(Value::CreateStringValue(*iter)); |
| 740 |
| 741 SetPref(extension_id, pref_path, list_value, msg); |
| 674 } | 742 } |
| 675 | 743 |
| 676 protected: | 744 protected: |
| 677 ExtensionList loaded_; | 745 ExtensionList loaded_; |
| 678 std::string unloaded_id_; | 746 std::string unloaded_id_; |
| 679 const Extension* installed_; | 747 const Extension* installed_; |
| 680 | 748 |
| 681 private: | 749 private: |
| 682 NotificationRegistrar registrar_; | 750 NotificationRegistrar registrar_; |
| 683 }; | 751 }; |
| (...skipping 318 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1002 EXPECT_EQ(0u, errors.size()) << "There were errors: " | 1070 EXPECT_EQ(0u, errors.size()) << "There were errors: " |
| 1003 << JoinString(errors, ','); | 1071 << JoinString(errors, ','); |
| 1004 EXPECT_TRUE(service_->GetExtensionById(loaded_[0]->id(), false)) << | 1072 EXPECT_TRUE(service_->GetExtensionById(loaded_[0]->id(), false)) << |
| 1005 path.value(); | 1073 path.value(); |
| 1006 | 1074 |
| 1007 installed_ = NULL; | 1075 installed_ = NULL; |
| 1008 loaded_.clear(); | 1076 loaded_.clear(); |
| 1009 ExtensionErrorReporter::GetInstance()->ClearErrors(); | 1077 ExtensionErrorReporter::GetInstance()->ClearErrors(); |
| 1010 } | 1078 } |
| 1011 | 1079 |
| 1080 // This tests that the granted permissions preferences are correctly set when |
| 1081 // installing an extension. |
| 1082 TEST_F(ExtensionsServiceTest, GrantedPermissions) { |
| 1083 InitializeEmptyExtensionsService(); |
| 1084 FilePath path; |
| 1085 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &path)); |
| 1086 path = path.AppendASCII("extensions") |
| 1087 .AppendASCII("permissions") |
| 1088 .AppendASCII("unknown"); |
| 1089 |
| 1090 ASSERT_TRUE(file_util::PathExists(path)); |
| 1091 |
| 1092 PackAndInstallExtension(path, true); |
| 1093 |
| 1094 EXPECT_EQ(0u, GetErrors().size()); |
| 1095 EXPECT_EQ(1u, service_->extensions()->size()); |
| 1096 std::string extension_id = service_->extensions()->at(0)->id(); |
| 1097 |
| 1098 ExtensionPrefs* prefs = service_->extension_prefs(); |
| 1099 |
| 1100 // Verify that the valid API permissions have been recognized. |
| 1101 std::set<std::string> known_api_perms; |
| 1102 ExtensionExtent known_host_perms; |
| 1103 |
| 1104 std::set<std::string> expected_api_perms; |
| 1105 expected_api_perms.insert("tabs"); |
| 1106 |
| 1107 ExtensionExtent expected_host_perms; |
| 1108 AddPattern(&expected_host_perms, "http://*.google.com/*"); |
| 1109 AddPattern(&expected_host_perms, "https://*.google.com/*"); |
| 1110 AddPattern(&expected_host_perms, "http://www.example.com/*"); |
| 1111 |
| 1112 EXPECT_TRUE(prefs->GetGrantedPermissions(extension_id, |
| 1113 &known_api_perms, |
| 1114 &known_host_perms)); |
| 1115 |
| 1116 EXPECT_EQ(expected_api_perms, known_api_perms); |
| 1117 AssertEqualExtents(&expected_host_perms, &known_host_perms); |
| 1118 } |
| 1119 |
| 1120 // Tests that the extension is disabled when permissions are missing from |
| 1121 // the extension's granted permissions preferences. (This simulates updating |
| 1122 // the browser to a version which recognizes more permissions). |
| 1123 TEST_F(ExtensionsServiceTest, GrantedAPIPermissions) { |
| 1124 InitializeEmptyExtensionsService(); |
| 1125 |
| 1126 FilePath path; |
| 1127 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &path)); |
| 1128 path = path.AppendASCII("extensions") |
| 1129 .AppendASCII("permissions") |
| 1130 .AppendASCII("unknown"); |
| 1131 |
| 1132 ASSERT_TRUE(file_util::PathExists(path)); |
| 1133 |
| 1134 PackAndInstallExtension(path, true); |
| 1135 |
| 1136 EXPECT_EQ(0u, GetErrors().size()); |
| 1137 EXPECT_EQ(1u, service_->extensions()->size()); |
| 1138 const Extension* extension = service_->extensions()->at(0); |
| 1139 std::string extension_id = extension->id(); |
| 1140 |
| 1141 ExtensionPrefs* prefs = service_->extension_prefs(); |
| 1142 |
| 1143 std::set<std::string> expected_api_permissions; |
| 1144 ExtensionExtent expected_host_permissions; |
| 1145 |
| 1146 expected_api_permissions.insert("tabs"); |
| 1147 AddPattern(&expected_host_permissions, "http://*.google.com/*"); |
| 1148 AddPattern(&expected_host_permissions, "https://*.google.com/*"); |
| 1149 AddPattern(&expected_host_permissions, "http://www.example.com/*"); |
| 1150 |
| 1151 std::set<std::string> api_permissions; |
| 1152 std::set<std::string> host_permissions; |
| 1153 |
| 1154 // Test that the extension is disabled when an API permission is missing from |
| 1155 // the extension's granted api permissions preference. (This simulates |
| 1156 // updating the browser to a version which recognizes a new API permission). |
| 1157 host_permissions.insert("http://*.google.com/*"); |
| 1158 host_permissions.insert("https://*.google.com/*"); |
| 1159 host_permissions.insert("http://www.example.com/*"); |
| 1160 |
| 1161 SetPrefBool(extension_id, "granted_permissions.initialized", true); |
| 1162 SetPrefStringSet(extension_id, "granted_permissions.api", api_permissions); |
| 1163 SetPrefStringSet(extension_id, "granted_permissions.host", host_permissions); |
| 1164 |
| 1165 service_->ReloadExtensions(); |
| 1166 |
| 1167 ASSERT_TRUE(prefs->GetExtensionState(extension_id) == Extension::DISABLED); |
| 1168 ASSERT_TRUE(prefs->DidExtensionEscalatePermissions(extension_id)); |
| 1169 |
| 1170 // Now grant and re-enable the extension, making sure the prefs are updated. |
| 1171 service_->GrantPermissionsAndEnableExtension(extension); |
| 1172 |
| 1173 ASSERT_TRUE(prefs->GetExtensionState(extension_id) == Extension::ENABLED); |
| 1174 ASSERT_FALSE(prefs->DidExtensionEscalatePermissions(extension_id)); |
| 1175 |
| 1176 std::set<std::string> current_api_permissions; |
| 1177 ExtensionExtent current_host_permissions; |
| 1178 |
| 1179 ASSERT_TRUE(prefs->GetGrantedPermissions( |
| 1180 extension_id, ¤t_api_permissions, ¤t_host_permissions)); |
| 1181 |
| 1182 ASSERT_EQ(expected_api_permissions, current_api_permissions); |
| 1183 AssertEqualExtents(&expected_host_permissions, ¤t_host_permissions); |
| 1184 |
| 1185 // Tests that the extension is disabled when a host permission is missing from |
| 1186 // the extension's granted host permissions preference. (This simulates |
| 1187 // updating the browser to a version which recognizes additional host |
| 1188 // permissions). |
| 1189 api_permissions.clear(); |
| 1190 host_permissions.clear(); |
| 1191 current_api_permissions.clear(); |
| 1192 current_host_permissions.ClearPaths(); |
| 1193 |
| 1194 api_permissions.insert("tabs"); |
| 1195 host_permissions.insert("http://*.google.com/*"); |
| 1196 host_permissions.insert("https://*.google.com/*"); |
| 1197 |
| 1198 SetPrefBool(extension_id, "granted_permissions.initialized", true); |
| 1199 SetPrefStringSet(extension_id, "granted_permissions.api", api_permissions); |
| 1200 SetPrefStringSet(extension_id, "granted_permissions.host", host_permissions); |
| 1201 |
| 1202 service_->ReloadExtensions(); |
| 1203 |
| 1204 ASSERT_TRUE(prefs->GetExtensionState(extension_id) == Extension::DISABLED); |
| 1205 ASSERT_TRUE(prefs->DidExtensionEscalatePermissions(extension_id)); |
| 1206 |
| 1207 // Now grant and re-enable the extension, making sure the prefs are updated. |
| 1208 service_->GrantPermissionsAndEnableExtension(extension); |
| 1209 |
| 1210 ASSERT_TRUE(prefs->GetExtensionState(extension_id) == Extension::ENABLED); |
| 1211 ASSERT_FALSE(prefs->DidExtensionEscalatePermissions(extension_id)); |
| 1212 |
| 1213 ASSERT_TRUE(prefs->GetGrantedPermissions( |
| 1214 extension_id, ¤t_api_permissions, ¤t_host_permissions)); |
| 1215 |
| 1216 ASSERT_EQ(expected_api_permissions, current_api_permissions); |
| 1217 AssertEqualExtents(&expected_host_permissions, ¤t_host_permissions); |
| 1218 |
| 1219 // Tests that the granted permissions preferences are initialized when |
| 1220 // migrating from the old pref schema. |
| 1221 current_api_permissions.clear(); |
| 1222 current_host_permissions.ClearPaths(); |
| 1223 |
| 1224 ClearPref(extension_id, "granted_permissions"); |
| 1225 |
| 1226 service_->ReloadExtensions(); |
| 1227 |
| 1228 ASSERT_TRUE(prefs->GetExtensionState(extension_id) == Extension::ENABLED); |
| 1229 ASSERT_FALSE(prefs->DidExtensionEscalatePermissions(extension_id)); |
| 1230 |
| 1231 ASSERT_TRUE(prefs->GetGrantedPermissions( |
| 1232 extension_id, ¤t_api_permissions, ¤t_host_permissions)); |
| 1233 |
| 1234 ASSERT_EQ(expected_api_permissions, current_api_permissions); |
| 1235 AssertEqualExtents(&expected_host_permissions, ¤t_host_permissions); |
| 1236 } |
| 1237 |
| 1012 // Test Packaging and installing an extension. | 1238 // Test Packaging and installing an extension. |
| 1013 TEST_F(ExtensionsServiceTest, PackExtension) { | 1239 TEST_F(ExtensionsServiceTest, PackExtension) { |
| 1014 InitializeEmptyExtensionsService(); | 1240 InitializeEmptyExtensionsService(); |
| 1015 FilePath extensions_path; | 1241 FilePath extensions_path; |
| 1016 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &extensions_path)); | 1242 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &extensions_path)); |
| 1017 extensions_path = extensions_path.AppendASCII("extensions"); | 1243 extensions_path = extensions_path.AppendASCII("extensions"); |
| 1018 FilePath input_directory = extensions_path | 1244 FilePath input_directory = extensions_path |
| 1019 .AppendASCII("good") | 1245 .AppendASCII("good") |
| 1020 .AppendASCII("Extensions") | 1246 .AppendASCII("Extensions") |
| 1021 .AppendASCII("behllobkkfkfnphdnhnkndlbkcpglgmj") | 1247 .AppendASCII("behllobkkfkfnphdnhnkndlbkcpglgmj") |
| (...skipping 1730 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2752 // Component extensions shouldn't get recourded in the prefs. | 2978 // Component extensions shouldn't get recourded in the prefs. |
| 2753 ValidatePrefKeyCount(0); | 2979 ValidatePrefKeyCount(0); |
| 2754 | 2980 |
| 2755 // Reload all extensions, and make sure it comes back. | 2981 // Reload all extensions, and make sure it comes back. |
| 2756 std::string extension_id = service_->extensions()->at(0)->id(); | 2982 std::string extension_id = service_->extensions()->at(0)->id(); |
| 2757 loaded_.clear(); | 2983 loaded_.clear(); |
| 2758 service_->ReloadExtensions(); | 2984 service_->ReloadExtensions(); |
| 2759 ASSERT_EQ(1u, service_->extensions()->size()); | 2985 ASSERT_EQ(1u, service_->extensions()->size()); |
| 2760 EXPECT_EQ(extension_id, service_->extensions()->at(0)->id()); | 2986 EXPECT_EQ(extension_id, service_->extensions()->at(0)->id()); |
| 2761 } | 2987 } |
| OLD | NEW |