| 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 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 133 const std::string& version, | 133 const std::string& version, |
| 134 const FilePath& path) { | 134 const FilePath& path) { |
| 135 extension_map_[id] = std::make_pair(version, path); | 135 extension_map_[id] = std::make_pair(version, path); |
| 136 } | 136 } |
| 137 | 137 |
| 138 void RemoveExtension(const std::string& id) { | 138 void RemoveExtension(const std::string& id) { |
| 139 extension_map_.erase(id); | 139 extension_map_.erase(id); |
| 140 } | 140 } |
| 141 | 141 |
| 142 // ExternalExtensionProvider implementation: | 142 // ExternalExtensionProvider implementation: |
| 143 virtual void VisitRegisteredExtension( | 143 virtual void VisitRegisteredExtension(Visitor* visitor) const { |
| 144 Visitor* visitor, const std::set<std::string>& ids_to_ignore) const { | |
| 145 visit_count_++; | 144 visit_count_++; |
| 146 for (DataMap::const_iterator i = extension_map_.begin(); | 145 for (DataMap::const_iterator i = extension_map_.begin(); |
| 147 i != extension_map_.end(); ++i) { | 146 i != extension_map_.end(); ++i) { |
| 148 if (ids_to_ignore.find(i->first) != ids_to_ignore.end()) | |
| 149 continue; | |
| 150 scoped_ptr<Version> version; | 147 scoped_ptr<Version> version; |
| 151 version.reset(Version::GetVersionFromString(i->second.first)); | 148 version.reset(Version::GetVersionFromString(i->second.first)); |
| 152 | 149 |
| 153 visitor->OnExternalExtensionFileFound( | 150 visitor->OnExternalExtensionFileFound( |
| 154 i->first, version.get(), i->second.second, location_); | 151 i->first, version.get(), i->second.second, location_); |
| 155 } | 152 } |
| 156 } | 153 } |
| 157 | 154 |
| 158 virtual bool HasExtension(const std::string& id) const { | 155 virtual bool HasExtension(const std::string& id) const { |
| 159 return extension_map_.find(id) != extension_map_.end(); | 156 return extension_map_.find(id) != extension_map_.end(); |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 191 mutable int visit_count_; | 188 mutable int visit_count_; |
| 192 | 189 |
| 193 DISALLOW_COPY_AND_ASSIGN(MockExtensionProvider); | 190 DISALLOW_COPY_AND_ASSIGN(MockExtensionProvider); |
| 194 }; | 191 }; |
| 195 | 192 |
| 196 class MockProviderVisitor : public ExternalExtensionProvider::Visitor { | 193 class MockProviderVisitor : public ExternalExtensionProvider::Visitor { |
| 197 public: | 194 public: |
| 198 MockProviderVisitor() : ids_found_(0) { | 195 MockProviderVisitor() : ids_found_(0) { |
| 199 } | 196 } |
| 200 | 197 |
| 201 int Visit(const std::string& json_data, | 198 int Visit(const std::string& json_data) { |
| 202 const std::set<std::string>& ignore_list) { | |
| 203 // Give the test json file to the provider for parsing. | 199 // Give the test json file to the provider for parsing. |
| 204 provider_.reset(new ExternalPrefExtensionProvider()); | 200 provider_.reset(new ExternalPrefExtensionProvider()); |
| 205 provider_->SetPreferencesForTesting(json_data); | 201 provider_->SetPreferencesForTesting(json_data); |
| 206 | 202 |
| 207 // We also parse the file into a dictionary to compare what we get back | 203 // We also parse the file into a dictionary to compare what we get back |
| 208 // from the provider. | 204 // from the provider. |
| 209 JSONStringValueSerializer serializer(json_data); | 205 JSONStringValueSerializer serializer(json_data); |
| 210 Value* json_value = serializer.Deserialize(NULL, NULL); | 206 Value* json_value = serializer.Deserialize(NULL, NULL); |
| 211 | 207 |
| 212 if (!json_value || !json_value->IsType(Value::TYPE_DICTIONARY)) { | 208 if (!json_value || !json_value->IsType(Value::TYPE_DICTIONARY)) { |
| 213 NOTREACHED() << "Unable to deserialize json data"; | 209 NOTREACHED() << "Unable to deserialize json data"; |
| 214 return -1; | 210 return -1; |
| 215 } else { | 211 } else { |
| 216 DictionaryValue* external_extensions = | 212 DictionaryValue* external_extensions = |
| 217 static_cast<DictionaryValue*>(json_value); | 213 static_cast<DictionaryValue*>(json_value); |
| 218 prefs_.reset(external_extensions); | 214 prefs_.reset(external_extensions); |
| 219 } | 215 } |
| 220 | 216 |
| 221 // Reset our counter. | 217 // Reset our counter. |
| 222 ids_found_ = 0; | 218 ids_found_ = 0; |
| 223 // Ask the provider to look up all extensions (and return the ones | 219 // Ask the provider to look up all extensions and return them. |
| 224 // found (that are not on the ignore list). | 220 provider_->VisitRegisteredExtension(this); |
| 225 provider_->VisitRegisteredExtension(this, ignore_list); | |
| 226 | 221 |
| 227 return ids_found_; | 222 return ids_found_; |
| 228 } | 223 } |
| 229 | 224 |
| 230 virtual void OnExternalExtensionFileFound(const std::string& id, | 225 virtual void OnExternalExtensionFileFound(const std::string& id, |
| 231 const Version* version, | 226 const Version* version, |
| 232 const FilePath& path, | 227 const FilePath& path, |
| 233 Extension::Location unused) { | 228 Extension::Location unused) { |
| 234 ++ids_found_; | 229 ++ids_found_; |
| 235 DictionaryValue* pref; | 230 DictionaryValue* pref; |
| (...skipping 819 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1055 // class of validation that we do to the directory structure of the extension. | 1050 // class of validation that we do to the directory structure of the extension. |
| 1056 // We did not used to handle this correctly for installation. | 1051 // We did not used to handle this correctly for installation. |
| 1057 path = extensions_path.AppendASCII("bad_underscore.crx"); | 1052 path = extensions_path.AppendASCII("bad_underscore.crx"); |
| 1058 InstallExtension(path, false); | 1053 InstallExtension(path, false); |
| 1059 ValidatePrefKeyCount(pref_count); | 1054 ValidatePrefKeyCount(pref_count); |
| 1060 | 1055 |
| 1061 // TODO(erikkay): add more tests for many of the failure cases. | 1056 // TODO(erikkay): add more tests for many of the failure cases. |
| 1062 // TODO(erikkay): add tests for upgrade cases. | 1057 // TODO(erikkay): add tests for upgrade cases. |
| 1063 } | 1058 } |
| 1064 | 1059 |
| 1060 // Test the handling of killed extensions. |
| 1061 TEST_F(ExtensionsServiceTest, KilledExtensions) { |
| 1062 InitializeEmptyExtensionsService(); |
| 1063 |
| 1064 FilePath extensions_path; |
| 1065 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &extensions_path)); |
| 1066 extensions_path = extensions_path.AppendASCII("extensions"); |
| 1067 FilePath path = extensions_path.AppendASCII("good.crx"); |
| 1068 set_extensions_enabled(true); |
| 1069 |
| 1070 // Install an external extension. |
| 1071 service_->OnExternalExtensionFileFound(good_crx, "1.0.0.0", |
| 1072 path, Extension::EXTERNAL_PREF); |
| 1073 loop_.RunAllPending(); |
| 1074 ASSERT_TRUE(NULL != service_->GetExtensionById(good_crx, false)); |
| 1075 |
| 1076 // Uninstall it and check that its killbit gets set. |
| 1077 service_->UninstallExtension(good_crx, false); |
| 1078 loop_.RunAllPending(); |
| 1079 ValidateIntegerPref(good_crx, "location", Extension::KILLBIT); |
| 1080 |
| 1081 // Try to re-install it externally. This should fail because of the killbit. |
| 1082 service_->OnExternalExtensionFileFound(good_crx, "1.0.0.0", |
| 1083 path, Extension::EXTERNAL_PREF); |
| 1084 loop_.RunAllPending(); |
| 1085 ASSERT_TRUE(NULL == service_->GetExtensionById(good_crx, false)); |
| 1086 ValidateIntegerPref(good_crx, "location", Extension::KILLBIT); |
| 1087 |
| 1088 // Repeat the same thing with a newer version of the extension. |
| 1089 path = extensions_path.AppendASCII("good2.crx"); |
| 1090 service_->OnExternalExtensionFileFound(good_crx, "1.0.0.1", |
| 1091 path, Extension::EXTERNAL_PREF); |
| 1092 loop_.RunAllPending(); |
| 1093 ASSERT_TRUE(NULL == service_->GetExtensionById(good_crx, false)); |
| 1094 ValidateIntegerPref(good_crx, "location", Extension::KILLBIT); |
| 1095 |
| 1096 // Try adding the same extension from an external update URL. |
| 1097 service_->AddPendingExtensionFromExternalUpdateUrl( |
| 1098 good_crx, |
| 1099 GURL("http:://fake.update/url"), |
| 1100 Extension::EXTERNAL_PREF_DOWNLOAD); |
| 1101 const PendingExtensionMap& pending_extensions = |
| 1102 service_->pending_extensions(); |
| 1103 ASSERT_TRUE(pending_extensions.find(good_crx) == pending_extensions.end()); |
| 1104 } |
| 1105 |
| 1065 // Install a user script (they get converted automatically to an extension) | 1106 // Install a user script (they get converted automatically to an extension) |
| 1066 TEST_F(ExtensionsServiceTest, InstallUserScript) { | 1107 TEST_F(ExtensionsServiceTest, InstallUserScript) { |
| 1067 // The details of script conversion are tested elsewhere, this just tests | 1108 // The details of script conversion are tested elsewhere, this just tests |
| 1068 // integration with ExtensionsService. | 1109 // integration with ExtensionsService. |
| 1069 InitializeEmptyExtensionsService(); | 1110 InitializeEmptyExtensionsService(); |
| 1070 | 1111 |
| 1071 FilePath path; | 1112 FilePath path; |
| 1072 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &path)); | 1113 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &path)); |
| 1073 path = path.AppendASCII("extensions") | 1114 path = path.AppendASCII("extensions") |
| 1074 .AppendASCII("user_script_basic.user.js"); | 1115 .AppendASCII("user_script_basic.user.js"); |
| (...skipping 1789 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2864 " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\": {" | 2905 " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\": {" |
| 2865 " \"external_crx\": \"RandomExtension2.crx\"," | 2906 " \"external_crx\": \"RandomExtension2.crx\"," |
| 2866 " \"external_version\": \"2.0\"" | 2907 " \"external_version\": \"2.0\"" |
| 2867 " }," | 2908 " }," |
| 2868 " \"cccccccccccccccccccccccccccccccc\": {" | 2909 " \"cccccccccccccccccccccccccccccccc\": {" |
| 2869 " \"external_update_url\": \"http:\\\\foo.com/update\"" | 2910 " \"external_update_url\": \"http:\\\\foo.com/update\"" |
| 2870 " }" | 2911 " }" |
| 2871 "}"; | 2912 "}"; |
| 2872 | 2913 |
| 2873 MockProviderVisitor visitor; | 2914 MockProviderVisitor visitor; |
| 2874 std::set<std::string> ignore_list; | |
| 2875 EXPECT_EQ(3, visitor.Visit(json_data, ignore_list)); | |
| 2876 ignore_list.insert("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"); | |
| 2877 EXPECT_EQ(2, visitor.Visit(json_data, ignore_list)); | |
| 2878 ignore_list.insert("bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"); | |
| 2879 EXPECT_EQ(1, visitor.Visit(json_data, ignore_list)); | |
| 2880 ignore_list.insert("cccccccccccccccccccccccccccccccc"); | |
| 2881 EXPECT_EQ(0, visitor.Visit(json_data, ignore_list)); | |
| 2882 | 2915 |
| 2883 // Simulate an external_extensions.json file that contains seven invalid | 2916 // Simulate an external_extensions.json file that contains seven invalid |
| 2884 // extensions: | 2917 // extensions: |
| 2885 // - One that is missing the 'external_crx' key. | 2918 // - One that is missing the 'external_crx' key. |
| 2886 // - One that is missing the 'external_version' key. | 2919 // - One that is missing the 'external_version' key. |
| 2887 // - One that is specifying .. in the path. | 2920 // - One that is specifying .. in the path. |
| 2888 // - One that specifies both a file and update URL. | 2921 // - One that specifies both a file and update URL. |
| 2889 // - One that specifies no file or update URL. | 2922 // - One that specifies no file or update URL. |
| 2890 // - One that has an update URL that is not well formed. | 2923 // - One that has an update URL that is not well formed. |
| 2891 // - One that contains a malformed version. | 2924 // - One that contains a malformed version. |
| (...skipping 23 matching lines...) Expand all Loading... |
| 2915 " }," | 2948 " }," |
| 2916 " \"gggggggggggggggggggggggggggggggg\": {" | 2949 " \"gggggggggggggggggggggggggggggggg\": {" |
| 2917 " \"external_crx\": \"RandomExtension3.crx\"," | 2950 " \"external_crx\": \"RandomExtension3.crx\"," |
| 2918 " \"external_version\": \"This is not a valid version!\"" | 2951 " \"external_version\": \"This is not a valid version!\"" |
| 2919 " }," | 2952 " }," |
| 2920 " \"hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh\": {" | 2953 " \"hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh\": {" |
| 2921 " \"external_crx\": \"RandomValidExtension.crx\"," | 2954 " \"external_crx\": \"RandomValidExtension.crx\"," |
| 2922 " \"external_version\": \"1.0\"" | 2955 " \"external_version\": \"1.0\"" |
| 2923 " }" | 2956 " }" |
| 2924 "}"; | 2957 "}"; |
| 2925 ignore_list.clear(); | 2958 EXPECT_EQ(1, visitor.Visit(json_data)); |
| 2926 EXPECT_EQ(1, visitor.Visit(json_data, ignore_list)); | |
| 2927 } | 2959 } |
| 2928 | 2960 |
| 2929 // Test loading good extensions from the profile directory. | 2961 // Test loading good extensions from the profile directory. |
| 2930 TEST_F(ExtensionsServiceTest, LoadAndRelocalizeExtensions) { | 2962 TEST_F(ExtensionsServiceTest, LoadAndRelocalizeExtensions) { |
| 2931 // Initialize the test dir with a good Preferences/extensions. | 2963 // Initialize the test dir with a good Preferences/extensions. |
| 2932 FilePath source_install_dir; | 2964 FilePath source_install_dir; |
| 2933 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &source_install_dir)); | 2965 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &source_install_dir)); |
| 2934 source_install_dir = source_install_dir | 2966 source_install_dir = source_install_dir |
| 2935 .AppendASCII("extensions") | 2967 .AppendASCII("extensions") |
| 2936 .AppendASCII("l10n"); | 2968 .AppendASCII("l10n"); |
| (...skipping 204 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3141 // Component extensions shouldn't get recourded in the prefs. | 3173 // Component extensions shouldn't get recourded in the prefs. |
| 3142 ValidatePrefKeyCount(0); | 3174 ValidatePrefKeyCount(0); |
| 3143 | 3175 |
| 3144 // Reload all extensions, and make sure it comes back. | 3176 // Reload all extensions, and make sure it comes back. |
| 3145 std::string extension_id = service_->extensions()->at(0)->id(); | 3177 std::string extension_id = service_->extensions()->at(0)->id(); |
| 3146 loaded_.clear(); | 3178 loaded_.clear(); |
| 3147 service_->ReloadExtensions(); | 3179 service_->ReloadExtensions(); |
| 3148 ASSERT_EQ(1u, service_->extensions()->size()); | 3180 ASSERT_EQ(1u, service_->extensions()->size()); |
| 3149 EXPECT_EQ(extension_id, service_->extensions()->at(0)->id()); | 3181 EXPECT_EQ(extension_id, service_->extensions()->at(0)->id()); |
| 3150 } | 3182 } |
| OLD | NEW |