Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "base/file_util.h" | |
| 6 #include "base/files/file_enumerator.h" | |
| 7 #include "base/files/file_path.h" | |
| 8 #include "base/prefs/scoped_user_pref_update.h" | |
| 9 #include "base/values.h" | |
| 10 #include "chrome/browser/extensions/extension_garbage_collector.h" | |
| 11 #include "chrome/browser/extensions/extension_service_unittest.h" | |
| 12 #include "chrome/browser/profiles/profile.h" | |
| 13 #include "chrome/common/chrome_constants.h" | |
| 14 #include "chrome/test/base/testing_profile.h" | |
| 15 #include "content/public/browser/plugin_service.h" | |
| 16 | |
| 17 namespace extensions { | |
| 18 | |
| 19 class ExtensionGarbageCollectorUnitTest : public ExtensionServiceTestBase { | |
| 20 protected: | |
| 21 void InitPluginService() { | |
| 22 #if defined(ENABLE_PLUGINS) | |
| 23 content::PluginService::GetInstance()->Init(); | |
| 24 #endif | |
| 25 } | |
| 26 | |
| 27 // A delayed task to call GarbageCollectExtensions is posted by | |
| 28 // ExtensionGarbageCollector's constructor. But, as the test won't wait for | |
| 29 // the delayed task to be called, we have to call it manually instead. | |
| 30 void GarbageCollectExtensions() { | |
| 31 service_->garbage_collector()->GarbageCollectExtensionsForTest(); | |
| 32 // Wait for GarbageCollectExtensions task to complete. | |
| 33 base::RunLoop().RunUntilIdle(); | |
| 34 } | |
| 35 }; | |
| 36 | |
| 37 // Test that partially deleted extensions are cleaned up during startup | |
| 38 // Test loading bad extensions from the profile directory. | |
| 39 // Note: Was "ExtensionServiceTest.CleanupOnStartup" in a past life. | |
|
Yoyo Zhou
2014/03/26 01:54:12
You don't need these historical footnotes.
Devlin
2014/03/26 23:56:32
Done.
| |
| 40 TEST_F(ExtensionGarbageCollectorUnitTest, CleanupOnStartup) { | |
| 41 const std::string kExtensionId = "behllobkkfkfnphdnhnkndlbkcpglgmj"; | |
| 42 | |
| 43 InitPluginService(); | |
| 44 InitializeGoodInstalledExtensionService(); | |
| 45 | |
| 46 // Simulate that one of them got partially deleted by clearing its pref. | |
| 47 { | |
| 48 DictionaryPrefUpdate update(profile_->GetPrefs(), "extensions.settings"); | |
| 49 base::DictionaryValue* dict = update.Get(); | |
| 50 ASSERT_TRUE(dict != NULL); | |
| 51 dict->Remove(kExtensionId, NULL); | |
| 52 } | |
| 53 | |
| 54 service_->Init(); | |
| 55 GarbageCollectExtensions(); | |
| 56 | |
| 57 base::FileEnumerator dirs(extensions_install_dir_, | |
| 58 false, // not recursive | |
| 59 base::FileEnumerator::DIRECTORIES); | |
| 60 size_t count = 0; | |
| 61 while (!dirs.Next().empty()) | |
| 62 count++; | |
| 63 | |
| 64 // We should have only gotten two extensions now. | |
| 65 EXPECT_EQ(2u, count); | |
| 66 | |
| 67 // And extension1 dir should now be toast. | |
| 68 base::FilePath extension_dir = | |
| 69 extensions_install_dir_.AppendASCII(kExtensionId); | |
| 70 ASSERT_FALSE(base::PathExists(extension_dir)); | |
| 71 } | |
| 72 | |
| 73 // Test that GarbageCollectExtensions deletes the right versions of an | |
| 74 // extension. | |
| 75 // Note: Was "ExtensionServiceTest.GarbageCollectWithPendingUpdates" in a past | |
| 76 // life. | |
| 77 TEST_F(ExtensionGarbageCollectorUnitTest, GarbageCollectWithPendingUpdates) { | |
| 78 InitPluginService(); | |
| 79 | |
| 80 base::FilePath source_install_dir = | |
| 81 data_dir_.AppendASCII("pending_updates").AppendASCII("Extensions"); | |
| 82 base::FilePath pref_path = | |
| 83 source_install_dir.DirName().Append(chrome::kPreferencesFilename); | |
| 84 | |
| 85 InitializeInstalledExtensionService(pref_path, source_install_dir); | |
| 86 | |
| 87 // This is the directory that is going to be deleted, so make sure it actually | |
| 88 // is there before the garbage collection. | |
| 89 ASSERT_TRUE(base::PathExists(extensions_install_dir_.AppendASCII( | |
| 90 "hpiknbiabeeppbpihjehijgoemciehgk/3"))); | |
| 91 | |
| 92 GarbageCollectExtensions(); | |
| 93 | |
| 94 // Verify that the pending update for the first extension didn't get | |
| 95 // deleted. | |
| 96 EXPECT_TRUE(base::PathExists(extensions_install_dir_.AppendASCII( | |
| 97 "bjafgdebaacbbbecmhlhpofkepfkgcpa/1.0"))); | |
| 98 EXPECT_TRUE(base::PathExists(extensions_install_dir_.AppendASCII( | |
| 99 "bjafgdebaacbbbecmhlhpofkepfkgcpa/2.0"))); | |
| 100 EXPECT_TRUE(base::PathExists(extensions_install_dir_.AppendASCII( | |
| 101 "hpiknbiabeeppbpihjehijgoemciehgk/2"))); | |
| 102 EXPECT_FALSE(base::PathExists(extensions_install_dir_.AppendASCII( | |
| 103 "hpiknbiabeeppbpihjehijgoemciehgk/3"))); | |
| 104 } | |
| 105 | |
| 106 // Test that pending updates are properly handled on startup. | |
| 107 // Note: This was "ExtensionServiceTest.UpdateOnStartup" in a past life. | |
| 108 TEST_F(ExtensionGarbageCollectorUnitTest, UpdateOnStartup) { | |
| 109 InitPluginService(); | |
| 110 | |
| 111 base::FilePath source_install_dir = | |
| 112 data_dir_.AppendASCII("pending_updates").AppendASCII("Extensions"); | |
| 113 base::FilePath pref_path = | |
| 114 source_install_dir.DirName().Append(chrome::kPreferencesFilename); | |
| 115 | |
| 116 InitializeInstalledExtensionService(pref_path, source_install_dir); | |
| 117 | |
| 118 // This is the directory that is going to be deleted, so make sure it actually | |
| 119 // is there before the garbage collection. | |
| 120 ASSERT_TRUE(base::PathExists(extensions_install_dir_.AppendASCII( | |
| 121 "hpiknbiabeeppbpihjehijgoemciehgk/3"))); | |
| 122 | |
| 123 service_->Init(); | |
| 124 GarbageCollectExtensions(); | |
| 125 | |
| 126 // Verify that the pending update for the first extension got installed. | |
| 127 EXPECT_FALSE(base::PathExists(extensions_install_dir_.AppendASCII( | |
| 128 "bjafgdebaacbbbecmhlhpofkepfkgcpa/1.0"))); | |
| 129 EXPECT_TRUE(base::PathExists(extensions_install_dir_.AppendASCII( | |
| 130 "bjafgdebaacbbbecmhlhpofkepfkgcpa/2.0"))); | |
| 131 EXPECT_TRUE(base::PathExists(extensions_install_dir_.AppendASCII( | |
| 132 "hpiknbiabeeppbpihjehijgoemciehgk/2"))); | |
| 133 EXPECT_FALSE(base::PathExists(extensions_install_dir_.AppendASCII( | |
| 134 "hpiknbiabeeppbpihjehijgoemciehgk/3"))); | |
| 135 | |
| 136 // Make sure update information got deleted. | |
| 137 ExtensionPrefs* prefs = ExtensionPrefs::Get(profile_.get()); | |
| 138 EXPECT_FALSE( | |
| 139 prefs->GetDelayedInstallInfo("bjafgdebaacbbbecmhlhpofkepfkgcpa")); | |
| 140 } | |
| 141 | |
| 142 } // namespace extensions | |
| OLD | NEW |