OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 "base/file_util.h" | 5 #include "base/file_util.h" |
6 #include "base/files/file_enumerator.h" | 6 #include "base/files/file_enumerator.h" |
7 #include "base/files/file_path.h" | 7 #include "base/files/file_path.h" |
8 #include "base/prefs/scoped_user_pref_update.h" | 8 #include "base/prefs/scoped_user_pref_update.h" |
9 #include "base/threading/sequenced_worker_pool.h" | 9 #include "base/threading/sequenced_worker_pool.h" |
10 #include "base/values.h" | 10 #include "base/values.h" |
11 #include "chrome/browser/extensions/extension_garbage_collector.h" | 11 #include "chrome/browser/extensions/extension_garbage_collector.h" |
12 #include "chrome/browser/extensions/extension_service_unittest.h" | 12 #include "chrome/browser/extensions/extension_service.h" |
| 13 #include "chrome/browser/extensions/extension_service_test_base.h" |
13 #include "chrome/browser/extensions/install_tracker.h" | 14 #include "chrome/browser/extensions/install_tracker.h" |
14 #include "chrome/browser/profiles/profile.h" | 15 #include "chrome/browser/profiles/profile.h" |
15 #include "chrome/common/chrome_constants.h" | 16 #include "chrome/common/chrome_constants.h" |
16 #include "chrome/test/base/testing_profile.h" | 17 #include "chrome/test/base/testing_profile.h" |
17 #include "content/public/browser/browser_thread.h" | 18 #include "content/public/browser/browser_thread.h" |
18 #include "content/public/browser/plugin_service.h" | 19 #include "content/public/browser/plugin_service.h" |
19 #include "content/public/test/test_browser_thread_bundle.h" | 20 #include "content/public/test/test_browser_thread_bundle.h" |
20 #include "extensions/browser/extension_prefs.h" | 21 #include "extensions/browser/extension_prefs.h" |
21 | 22 |
22 namespace extensions { | 23 namespace extensions { |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
68 { | 69 { |
69 DictionaryPrefUpdate update(profile_->GetPrefs(), "extensions.settings"); | 70 DictionaryPrefUpdate update(profile_->GetPrefs(), "extensions.settings"); |
70 base::DictionaryValue* dict = update.Get(); | 71 base::DictionaryValue* dict = update.Get(); |
71 ASSERT_TRUE(dict != NULL); | 72 ASSERT_TRUE(dict != NULL); |
72 dict->Remove(kExtensionId, NULL); | 73 dict->Remove(kExtensionId, NULL); |
73 } | 74 } |
74 | 75 |
75 service_->Init(); | 76 service_->Init(); |
76 GarbageCollectExtensions(); | 77 GarbageCollectExtensions(); |
77 | 78 |
78 base::FileEnumerator dirs(extensions_install_dir_, | 79 base::FileEnumerator dirs(extensions_install_dir(), |
79 false, // not recursive | 80 false, // not recursive |
80 base::FileEnumerator::DIRECTORIES); | 81 base::FileEnumerator::DIRECTORIES); |
81 size_t count = 0; | 82 size_t count = 0; |
82 while (!dirs.Next().empty()) | 83 while (!dirs.Next().empty()) |
83 count++; | 84 count++; |
84 | 85 |
85 // We should have only gotten two extensions now. | 86 // We should have only gotten two extensions now. |
86 EXPECT_EQ(2u, count); | 87 EXPECT_EQ(2u, count); |
87 | 88 |
88 // And extension1 dir should now be toast. | 89 // And extension1 dir should now be toast. |
89 base::FilePath extension_dir = | 90 base::FilePath extension_dir = |
90 extensions_install_dir_.AppendASCII(kExtensionId); | 91 extensions_install_dir().AppendASCII(kExtensionId); |
91 ASSERT_FALSE(base::PathExists(extension_dir)); | 92 ASSERT_FALSE(base::PathExists(extension_dir)); |
92 } | 93 } |
93 | 94 |
94 // Test that garbage collection doesn't delete anything while a crx is being | 95 // Test that garbage collection doesn't delete anything while a crx is being |
95 // installed. | 96 // installed. |
96 TEST_F(ExtensionGarbageCollectorUnitTest, NoCleanupDuringInstall) { | 97 TEST_F(ExtensionGarbageCollectorUnitTest, NoCleanupDuringInstall) { |
97 const std::string kExtensionId = "behllobkkfkfnphdnhnkndlbkcpglgmj"; | 98 const std::string kExtensionId = "behllobkkfkfnphdnhnkndlbkcpglgmj"; |
98 | 99 |
99 InitPluginService(); | 100 InitPluginService(); |
100 InitializeGoodInstalledExtensionService(); | 101 InitializeGoodInstalledExtensionService(); |
101 InitFileTaskRunner(); | 102 InitFileTaskRunner(); |
102 | 103 |
103 // Simulate that one of them got partially deleted by clearing its pref. | 104 // Simulate that one of them got partially deleted by clearing its pref. |
104 { | 105 { |
105 DictionaryPrefUpdate update(profile_->GetPrefs(), "extensions.settings"); | 106 DictionaryPrefUpdate update(profile_->GetPrefs(), "extensions.settings"); |
106 base::DictionaryValue* dict = update.Get(); | 107 base::DictionaryValue* dict = update.Get(); |
107 ASSERT_TRUE(dict != NULL); | 108 ASSERT_TRUE(dict != NULL); |
108 dict->Remove(kExtensionId, NULL); | 109 dict->Remove(kExtensionId, NULL); |
109 } | 110 } |
110 | 111 |
111 service_->Init(); | 112 service_->Init(); |
112 | 113 |
113 // Simulate a CRX installation. | 114 // Simulate a CRX installation. |
114 InstallTracker::Get(profile_.get())->OnBeginCrxInstall(kExtensionId); | 115 InstallTracker::Get(profile_.get())->OnBeginCrxInstall(kExtensionId); |
115 | 116 |
116 GarbageCollectExtensions(); | 117 GarbageCollectExtensions(); |
117 | 118 |
118 // extension1 dir should still exist. | 119 // extension1 dir should still exist. |
119 base::FilePath extension_dir = | 120 base::FilePath extension_dir = |
120 extensions_install_dir_.AppendASCII(kExtensionId); | 121 extensions_install_dir().AppendASCII(kExtensionId); |
121 ASSERT_TRUE(base::PathExists(extension_dir)); | 122 ASSERT_TRUE(base::PathExists(extension_dir)); |
122 | 123 |
123 // Finish CRX installation and re-run garbage collection. | 124 // Finish CRX installation and re-run garbage collection. |
124 InstallTracker::Get(profile_.get())->OnFinishCrxInstall(kExtensionId, false); | 125 InstallTracker::Get(profile_.get())->OnFinishCrxInstall(kExtensionId, false); |
125 GarbageCollectExtensions(); | 126 GarbageCollectExtensions(); |
126 | 127 |
127 // extension1 dir should be gone | 128 // extension1 dir should be gone |
128 ASSERT_FALSE(base::PathExists(extension_dir)); | 129 ASSERT_FALSE(base::PathExists(extension_dir)); |
129 } | 130 } |
130 | 131 |
131 // Test that GarbageCollectExtensions deletes the right versions of an | 132 // Test that GarbageCollectExtensions deletes the right versions of an |
132 // extension. | 133 // extension. |
133 TEST_F(ExtensionGarbageCollectorUnitTest, GarbageCollectWithPendingUpdates) { | 134 TEST_F(ExtensionGarbageCollectorUnitTest, GarbageCollectWithPendingUpdates) { |
134 InitPluginService(); | 135 InitPluginService(); |
135 | 136 |
136 base::FilePath source_install_dir = | 137 base::FilePath source_install_dir = |
137 data_dir_.AppendASCII("pending_updates").AppendASCII("Extensions"); | 138 data_dir().AppendASCII("pending_updates").AppendASCII("Extensions"); |
138 base::FilePath pref_path = | 139 base::FilePath pref_path = |
139 source_install_dir.DirName().Append(chrome::kPreferencesFilename); | 140 source_install_dir.DirName().Append(chrome::kPreferencesFilename); |
140 | 141 |
141 InitializeInstalledExtensionService(pref_path, source_install_dir); | 142 InitializeInstalledExtensionService(pref_path, source_install_dir); |
142 InitFileTaskRunner(); | 143 InitFileTaskRunner(); |
143 | 144 |
144 // This is the directory that is going to be deleted, so make sure it actually | 145 // This is the directory that is going to be deleted, so make sure it actually |
145 // is there before the garbage collection. | 146 // is there before the garbage collection. |
146 ASSERT_TRUE(base::PathExists(extensions_install_dir_.AppendASCII( | 147 ASSERT_TRUE(base::PathExists(extensions_install_dir().AppendASCII( |
147 "hpiknbiabeeppbpihjehijgoemciehgk/3"))); | 148 "hpiknbiabeeppbpihjehijgoemciehgk/3"))); |
148 | 149 |
149 GarbageCollectExtensions(); | 150 GarbageCollectExtensions(); |
150 | 151 |
151 // Verify that the pending update for the first extension didn't get | 152 // Verify that the pending update for the first extension didn't get |
152 // deleted. | 153 // deleted. |
153 EXPECT_TRUE(base::PathExists(extensions_install_dir_.AppendASCII( | 154 EXPECT_TRUE(base::PathExists(extensions_install_dir().AppendASCII( |
154 "bjafgdebaacbbbecmhlhpofkepfkgcpa/1.0"))); | 155 "bjafgdebaacbbbecmhlhpofkepfkgcpa/1.0"))); |
155 EXPECT_TRUE(base::PathExists(extensions_install_dir_.AppendASCII( | 156 EXPECT_TRUE(base::PathExists(extensions_install_dir().AppendASCII( |
156 "bjafgdebaacbbbecmhlhpofkepfkgcpa/2.0"))); | 157 "bjafgdebaacbbbecmhlhpofkepfkgcpa/2.0"))); |
157 EXPECT_TRUE(base::PathExists(extensions_install_dir_.AppendASCII( | 158 EXPECT_TRUE(base::PathExists(extensions_install_dir().AppendASCII( |
158 "hpiknbiabeeppbpihjehijgoemciehgk/2"))); | 159 "hpiknbiabeeppbpihjehijgoemciehgk/2"))); |
159 EXPECT_FALSE(base::PathExists(extensions_install_dir_.AppendASCII( | 160 EXPECT_FALSE(base::PathExists(extensions_install_dir().AppendASCII( |
160 "hpiknbiabeeppbpihjehijgoemciehgk/3"))); | 161 "hpiknbiabeeppbpihjehijgoemciehgk/3"))); |
161 } | 162 } |
162 | 163 |
163 // Test that pending updates are properly handled on startup. | 164 // Test that pending updates are properly handled on startup. |
164 TEST_F(ExtensionGarbageCollectorUnitTest, UpdateOnStartup) { | 165 TEST_F(ExtensionGarbageCollectorUnitTest, UpdateOnStartup) { |
165 InitPluginService(); | 166 InitPluginService(); |
166 | 167 |
167 base::FilePath source_install_dir = | 168 base::FilePath source_install_dir = |
168 data_dir_.AppendASCII("pending_updates").AppendASCII("Extensions"); | 169 data_dir().AppendASCII("pending_updates").AppendASCII("Extensions"); |
169 base::FilePath pref_path = | 170 base::FilePath pref_path = |
170 source_install_dir.DirName().Append(chrome::kPreferencesFilename); | 171 source_install_dir.DirName().Append(chrome::kPreferencesFilename); |
171 | 172 |
172 InitializeInstalledExtensionService(pref_path, source_install_dir); | 173 InitializeInstalledExtensionService(pref_path, source_install_dir); |
173 InitFileTaskRunner(); | 174 InitFileTaskRunner(); |
174 | 175 |
175 // This is the directory that is going to be deleted, so make sure it actually | 176 // This is the directory that is going to be deleted, so make sure it actually |
176 // is there before the garbage collection. | 177 // is there before the garbage collection. |
177 ASSERT_TRUE(base::PathExists(extensions_install_dir_.AppendASCII( | 178 ASSERT_TRUE(base::PathExists(extensions_install_dir().AppendASCII( |
178 "hpiknbiabeeppbpihjehijgoemciehgk/3"))); | 179 "hpiknbiabeeppbpihjehijgoemciehgk/3"))); |
179 | 180 |
180 service_->Init(); | 181 service_->Init(); |
181 GarbageCollectExtensions(); | 182 GarbageCollectExtensions(); |
182 | 183 |
183 // Verify that the pending update for the first extension got installed. | 184 // Verify that the pending update for the first extension got installed. |
184 EXPECT_FALSE(base::PathExists(extensions_install_dir_.AppendASCII( | 185 EXPECT_FALSE(base::PathExists(extensions_install_dir().AppendASCII( |
185 "bjafgdebaacbbbecmhlhpofkepfkgcpa/1.0"))); | 186 "bjafgdebaacbbbecmhlhpofkepfkgcpa/1.0"))); |
186 EXPECT_TRUE(base::PathExists(extensions_install_dir_.AppendASCII( | 187 EXPECT_TRUE(base::PathExists(extensions_install_dir().AppendASCII( |
187 "bjafgdebaacbbbecmhlhpofkepfkgcpa/2.0"))); | 188 "bjafgdebaacbbbecmhlhpofkepfkgcpa/2.0"))); |
188 EXPECT_TRUE(base::PathExists(extensions_install_dir_.AppendASCII( | 189 EXPECT_TRUE(base::PathExists(extensions_install_dir().AppendASCII( |
189 "hpiknbiabeeppbpihjehijgoemciehgk/2"))); | 190 "hpiknbiabeeppbpihjehijgoemciehgk/2"))); |
190 EXPECT_FALSE(base::PathExists(extensions_install_dir_.AppendASCII( | 191 EXPECT_FALSE(base::PathExists(extensions_install_dir().AppendASCII( |
191 "hpiknbiabeeppbpihjehijgoemciehgk/3"))); | 192 "hpiknbiabeeppbpihjehijgoemciehgk/3"))); |
192 | 193 |
193 // Make sure update information got deleted. | 194 // Make sure update information got deleted. |
194 ExtensionPrefs* prefs = ExtensionPrefs::Get(profile_.get()); | 195 ExtensionPrefs* prefs = ExtensionPrefs::Get(profile_.get()); |
195 EXPECT_FALSE( | 196 EXPECT_FALSE( |
196 prefs->GetDelayedInstallInfo("bjafgdebaacbbbecmhlhpofkepfkgcpa")); | 197 prefs->GetDelayedInstallInfo("bjafgdebaacbbbecmhlhpofkepfkgcpa")); |
197 } | 198 } |
198 | 199 |
199 } // namespace extensions | 200 } // namespace extensions |
OLD | NEW |