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

Side by Side Diff: chrome/browser/extensions/extension_service_unittest.cc

Issue 9817018: Cleaning Up Extensions When Local Content Removed (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Cleaning Up Extensions When Local Content Removed Created 8 years, 9 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 "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 228 matching lines...) Expand 10 before | Expand all | Expand 10 after
239 mutable int visit_count_; 239 mutable int visit_count_;
240 240
241 int creation_flags_; 241 int creation_flags_;
242 242
243 DISALLOW_COPY_AND_ASSIGN(MockExtensionProvider); 243 DISALLOW_COPY_AND_ASSIGN(MockExtensionProvider);
244 }; 244 };
245 245
246 class MockProviderVisitor 246 class MockProviderVisitor
247 : public ExternalExtensionProviderInterface::VisitorInterface { 247 : public ExternalExtensionProviderInterface::VisitorInterface {
248 public: 248 public:
249
250 // The provider will return |fake_base_path| from 249 // The provider will return |fake_base_path| from
251 // GetBaseCrxFilePath(). User can test the behavior with 250 // GetBaseCrxFilePath(). User can test the behavior with
252 // and without an empty path using this parameter. 251 // and without an empty path using this parameter.
253 explicit MockProviderVisitor(FilePath fake_base_path) 252 explicit MockProviderVisitor(FilePath fake_base_path)
254 : ids_found_(0), 253 : ids_found_(0),
255 fake_base_path_(fake_base_path) { 254 fake_base_path_(fake_base_path) {
256 } 255 }
257 256
258 int Visit(const std::string& json_data) { 257 int Visit(const std::string& json_data) {
259 // Give the test json file to the provider for parsing. 258 // Give the test json file to the provider for parsing.
(...skipping 747 matching lines...) Expand 10 before | Expand all | Expand 10 after
1007 ASSERT_TRUE(file_util::PathExists(private_key_path)); 1006 ASSERT_TRUE(file_util::PathExists(private_key_path));
1008 } 1007 }
1009 1008
1010 // The tests are designed so that we never expect to see a packing error. 1009 // The tests are designed so that we never expect to see a packing error.
1011 void PackExtensionTestClient::OnPackFailure(const std::string& error_message, 1010 void PackExtensionTestClient::OnPackFailure(const std::string& error_message,
1012 ExtensionCreator::ErrorType type) { 1011 ExtensionCreator::ErrorType type) {
1013 if (type == ExtensionCreator::kCRXExists) 1012 if (type == ExtensionCreator::kCRXExists)
1014 FAIL() << "Packing should not fail."; 1013 FAIL() << "Packing should not fail.";
1015 else 1014 else
1016 FAIL() << "Existing CRX should have been overwritten."; 1015 FAIL() << "Existing CRX should have been overwritten.";
1017
1018 } 1016 }
1019 1017
1020 // Test loading good extensions from the profile directory. 1018 // Test loading good extensions from the profile directory.
1021 TEST_F(ExtensionServiceTest, LoadAllExtensionsFromDirectorySuccess) { 1019 TEST_F(ExtensionServiceTest, LoadAllExtensionsFromDirectorySuccess) {
1022 PluginService::GetInstance()->Init(); 1020 PluginService::GetInstance()->Init();
1023 1021
1024 // Initialize the test dir with a good Preferences/extensions. 1022 // Initialize the test dir with a good Preferences/extensions.
1025 FilePath source_install_dir = data_dir_ 1023 FilePath source_install_dir = data_dir_
1026 .AppendASCII("good") 1024 .AppendASCII("good")
1027 .AppendASCII("Extensions"); 1025 .AppendASCII("Extensions");
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after
1151 std::string("Could not load extension from '*'. ") + 1149 std::string("Could not load extension from '*'. ") +
1152 extension_manifest_errors::kMissingFile)) << 1150 extension_manifest_errors::kMissingFile)) <<
1153 UTF16ToUTF8(GetErrors()[2]); 1151 UTF16ToUTF8(GetErrors()[2]);
1154 1152
1155 EXPECT_TRUE(MatchPattern(UTF16ToUTF8(GetErrors()[3]), 1153 EXPECT_TRUE(MatchPattern(UTF16ToUTF8(GetErrors()[3]),
1156 std::string("Could not load extension from '*'. ") + 1154 std::string("Could not load extension from '*'. ") +
1157 extension_manifest_errors::kManifestUnreadable)) << 1155 extension_manifest_errors::kManifestUnreadable)) <<
1158 UTF16ToUTF8(GetErrors()[3]); 1156 UTF16ToUTF8(GetErrors()[3]);
1159 }; 1157 };
1160 1158
1161 // Test that partially deleted extensions are cleaned up during startup 1159 // Test that partially deleted extensions are cleaned up during startup.
Yoyo Zhou 2012/03/21 22:55:37 This test seems to be testing that extensions who
1162 // Test loading bad extensions from the profile directory. 1160 // Test loading bad extensions from the profile directory.
1163 TEST_F(ExtensionServiceTest, CleanupOnStartup) { 1161 TEST_F(ExtensionServiceTest, CleanupOnStartup) {
1164 PluginService::GetInstance()->Init(); 1162 PluginService::GetInstance()->Init();
1165 1163
1166 FilePath source_install_dir = data_dir_ 1164 FilePath source_install_dir = data_dir_
1167 .AppendASCII("good") 1165 .AppendASCII("good")
1168 .AppendASCII("Extensions"); 1166 .AppendASCII("Extensions");
1169 FilePath pref_path = source_install_dir 1167 FilePath pref_path = source_install_dir
1170 .DirName() 1168 .DirName()
1171 .AppendASCII("Preferences"); 1169 .AppendASCII("Preferences");
(...skipping 20 matching lines...) Expand all
1192 1190
1193 // We should have only gotten two extensions now. 1191 // We should have only gotten two extensions now.
1194 EXPECT_EQ(2u, count); 1192 EXPECT_EQ(2u, count);
1195 1193
1196 // And extension1 dir should now be toast. 1194 // And extension1 dir should now be toast.
1197 FilePath extension_dir = extensions_install_dir_ 1195 FilePath extension_dir = extensions_install_dir_
1198 .AppendASCII("behllobkkfkfnphdnhnkndlbkcpglgmj"); 1196 .AppendASCII("behllobkkfkfnphdnhnkndlbkcpglgmj");
1199 ASSERT_FALSE(file_util::PathExists(extension_dir)); 1197 ASSERT_FALSE(file_util::PathExists(extension_dir));
1200 } 1198 }
1201 1199
1200 // Test that internal extensions which are referenced in the preferences but
1201 // had their content deleted are cleaned up during startup.
1202 TEST_F(ExtensionServiceTest, CleanupInternalExtensionsMissingLocalContent) {
1203 PluginService::GetInstance()->Init();
1204
1205 FilePath source_dir = data_dir_.AppendASCII("good").AppendASCII("Extensions");
1206 FilePath pref_path = source_dir.DirName().AppendASCII("Preferences");
1207
1208 InitializeInstalledExtensionService(pref_path, source_dir);
1209
1210 // Delete the extension's directory.
1211 FilePath extension_dir = extensions_install_dir_
1212 .AppendASCII("behllobkkfkfnphdnhnkndlbkcpglgmj");
1213 ASSERT_TRUE(file_util::Delete(extension_dir, true));
1214
1215 // Run GarbageCollectExtensions.
1216 service_->Init();
1217 loop_.RunAllPending();
1218
1219 file_util::FileEnumerator dirs(extensions_install_dir_, false,
1220 file_util::FileEnumerator::DIRECTORIES);
1221 size_t count = 0;
1222 while (!dirs.Next().empty())
1223 count++;
1224
1225 // We should have only gotten two extensions now.
1226 EXPECT_EQ(2u, count);
1227
1228 DictionaryPrefUpdate update(profile_->GetPrefs(), "extensions.settings");
1229 DictionaryValue* dictionary = update.Get();
1230 ASSERT_TRUE(dictionary != NULL);
Yoyo Zhou 2012/03/21 22:55:37 nit: usually written as just ASSERT_TRUE(dictionar
1231 ASSERT_FALSE(dictionary->HasKey("behllobkkfkfnphdnhnkndlbkcpglgmj"));
1232 }
1233
1234 // Test that external extensions which are referenced in the preferences but
Yoyo Zhou 2012/03/21 22:55:37 nit: write 'unpacked' instead of 'external'
1235 // had their content deleted are cleaned up during startup.
1236 TEST_F(ExtensionServiceTest, CleanupUnpackedExtensionsMissingLocalContent) {
1237 InitializeEmptyExtensionService();
1238
1239 ScopedTempDir temp;
1240 ASSERT_TRUE(temp.CreateUniqueTempDir());
1241
1242 // Write the manifest dynamically, since we have to delete the file anyway.
1243 FilePath extension_path = temp.path();
1244 FilePath manifest_path = extension_path.Append(Extension::kManifestFilename);
1245 ASSERT_FALSE(file_util::PathExists(manifest_path));
1246
1247 // Construct a simple manifest and install it.
1248 DictionaryValue manifest;
1249 manifest.SetString("version", "1.0");
1250 manifest.SetString("name", "Cleanup Unpacked Extensions Missing Content");
1251 manifest.SetInteger("manifest_version", 2);
1252
1253 JSONFileValueSerializer serializer(manifest_path);
1254 ASSERT_TRUE(serializer.Serialize(manifest));
1255
1256 extensions::UnpackedInstaller::Create(service_)->Load(extension_path);
1257 loop_.RunAllPending();
1258
1259 // Make sure it installed correctly.
1260 EXPECT_EQ(0u, GetErrors().size());
1261 ASSERT_EQ(1u, loaded_.size());
1262 EXPECT_EQ(Extension::LOAD, loaded_[0]->location());
1263 EXPECT_EQ(1u, service_->extensions()->size());
1264
1265 std::string extension_id = loaded_[0]->id();
1266
1267 // Make sure it is in the preferences at this point.
1268 DictionaryPrefUpdate initial_update(
1269 profile_->GetPrefs(), "extensions.settings");
1270 DictionaryValue* initial_dictionary = initial_update.Get();
1271 ASSERT_TRUE(initial_dictionary != NULL);
1272 ASSERT_TRUE(initial_dictionary->HasKey(extension_id));
1273
1274 // Delete local content, GarbageCollectExtensions, and test whether the key
1275 // is still in the preferences.
1276 ASSERT_TRUE(file_util::Delete(extension_path, true));
1277 service_->GarbageCollectExtensions();
1278 loop_.RunAllPending();
1279
1280 DictionaryPrefUpdate final_update(
1281 profile_->GetPrefs(), "extensions.settings");
1282 DictionaryValue* final_dictionary = final_update.Get();
1283 ASSERT_TRUE(final_dictionary != NULL);
1284 ASSERT_FALSE(final_dictionary->HasKey(extension_id));
1285 }
1286
1202 // Test installing extensions. This test tries to install few extensions using 1287 // Test installing extensions. This test tries to install few extensions using
1203 // crx files. If you need to change those crx files, feel free to repackage 1288 // crx files. If you need to change those crx files, feel free to repackage
1204 // them, throw away the key used and change the id's above. 1289 // them, throw away the key used and change the id's above.
1205 TEST_F(ExtensionServiceTest, InstallExtension) { 1290 TEST_F(ExtensionServiceTest, InstallExtension) {
1206 InitializeEmptyExtensionService(); 1291 InitializeEmptyExtensionService();
1207 1292
1208 // Extensions not enabled. 1293 // Extensions not enabled.
1209 set_extensions_enabled(false); 1294 set_extensions_enabled(false);
1210 FilePath path = data_dir_.AppendASCII("good.crx"); 1295 FilePath path = data_dir_.AppendASCII("good.crx");
1211 InstallCRX(path, INSTALL_FAILED); 1296 InstallCRX(path, INSTALL_FAILED);
(...skipping 3645 matching lines...) Expand 10 before | Expand all | Expand 10 after
4857 provider->UpdateOrAddExtension(hosted_app, "1.0.0.0", 4942 provider->UpdateOrAddExtension(hosted_app, "1.0.0.0",
4858 data_dir_.AppendASCII("hosted_app.crx")); 4943 data_dir_.AppendASCII("hosted_app.crx"));
4859 4944
4860 service_->CheckForExternalUpdates(); 4945 service_->CheckForExternalUpdates();
4861 loop_.RunAllPending(); 4946 loop_.RunAllPending();
4862 4947
4863 ASSERT_TRUE(service_->PopulateExtensionGlobalError( 4948 ASSERT_TRUE(service_->PopulateExtensionGlobalError(
4864 extension_global_error.get())); 4949 extension_global_error.get()));
4865 ASSERT_EQ(1u, extension_global_error->get_external_extension_ids()->size()); 4950 ASSERT_EQ(1u, extension_global_error->get_external_extension_ids()->size());
4866 } 4951 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698