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 TEST_F(ExtensionGarbageCollectorUnitTest, CleanupOnStartup) { | |
40 const std::string kExtensionId = "behllobkkfkfnphdnhnkndlbkcpglgmj"; | |
41 | |
42 InitPluginService(); | |
43 InitializeGoodInstalledExtensionService(); | |
44 | |
45 // Simulate that one of them got partially deleted by clearing its pref. | |
46 { | |
47 DictionaryPrefUpdate update(profile_->GetPrefs(), "extensions.settings"); | |
48 base::DictionaryValue* dict = update.Get(); | |
49 ASSERT_TRUE(dict != NULL); | |
50 dict->Remove(kExtensionId, NULL); | |
51 } | |
52 | |
53 service_->Init(); | |
54 GarbageCollectExtensions(); | |
55 | |
56 base::FileEnumerator dirs(extensions_install_dir_, | |
57 false, // not recursive | |
58 base::FileEnumerator::DIRECTORIES); | |
59 size_t count = 0; | |
60 while (!dirs.Next().empty()) | |
61 count++; | |
62 | |
63 // We should have only gotten two extensions now. | |
64 EXPECT_EQ(2u, count); | |
65 | |
66 // And extension1 dir should now be toast. | |
67 base::FilePath extension_dir = | |
68 extensions_install_dir_.AppendASCII(kExtensionId); | |
69 ASSERT_FALSE(base::PathExists(extension_dir)); | |
70 } | |
71 | |
72 // Test that GarbageCollectExtensions deletes the right versions of an | |
73 // extension. | |
74 TEST_F(ExtensionGarbageCollectorUnitTest, GarbageCollectWithPendingUpdates) { | |
75 InitPluginService(); | |
76 | |
77 base::FilePath source_install_dir = | |
78 data_dir_.AppendASCII("pending_updates").AppendASCII("Extensions"); | |
79 base::FilePath pref_path = | |
80 source_install_dir.DirName().Append(chrome::kPreferencesFilename); | |
81 | |
82 InitializeInstalledExtensionService(pref_path, source_install_dir); | |
83 | |
84 // This is the directory that is going to be deleted, so make sure it actually | |
85 // is there before the garbage collection. | |
86 ASSERT_TRUE(base::PathExists(extensions_install_dir_.AppendASCII( | |
87 "hpiknbiabeeppbpihjehijgoemciehgk/3"))); | |
88 | |
89 GarbageCollectExtensions(); | |
90 | |
91 // Verify that the pending update for the first extension didn't get | |
92 // deleted. | |
93 EXPECT_TRUE(base::PathExists(extensions_install_dir_.AppendASCII( | |
94 "bjafgdebaacbbbecmhlhpofkepfkgcpa/1.0"))); | |
95 EXPECT_TRUE(base::PathExists(extensions_install_dir_.AppendASCII( | |
96 "bjafgdebaacbbbecmhlhpofkepfkgcpa/2.0"))); | |
97 EXPECT_TRUE(base::PathExists(extensions_install_dir_.AppendASCII( | |
98 "hpiknbiabeeppbpihjehijgoemciehgk/2"))); | |
99 EXPECT_FALSE(base::PathExists(extensions_install_dir_.AppendASCII( | |
100 "hpiknbiabeeppbpihjehijgoemciehgk/3"))); | |
101 } | |
102 | |
103 // Test that pending updates are properly handled on startup. | |
104 TEST_F(ExtensionGarbageCollectorUnitTest, UpdateOnStartup) { | |
105 InitPluginService(); | |
106 | |
107 base::FilePath source_install_dir = | |
108 data_dir_.AppendASCII("pending_updates").AppendASCII("Extensions"); | |
109 base::FilePath pref_path = | |
110 source_install_dir.DirName().Append(chrome::kPreferencesFilename); | |
111 | |
112 InitializeInstalledExtensionService(pref_path, source_install_dir); | |
113 | |
114 // This is the directory that is going to be deleted, so make sure it actually | |
115 // is there before the garbage collection. | |
116 ASSERT_TRUE(base::PathExists(extensions_install_dir_.AppendASCII( | |
117 "hpiknbiabeeppbpihjehijgoemciehgk/3"))); | |
118 | |
119 service_->Init(); | |
120 GarbageCollectExtensions(); | |
121 | |
122 // Verify that the pending update for the first extension got installed. | |
123 EXPECT_FALSE(base::PathExists(extensions_install_dir_.AppendASCII( | |
124 "bjafgdebaacbbbecmhlhpofkepfkgcpa/1.0"))); | |
125 EXPECT_TRUE(base::PathExists(extensions_install_dir_.AppendASCII( | |
126 "bjafgdebaacbbbecmhlhpofkepfkgcpa/2.0"))); | |
127 EXPECT_TRUE(base::PathExists(extensions_install_dir_.AppendASCII( | |
128 "hpiknbiabeeppbpihjehijgoemciehgk/2"))); | |
129 EXPECT_FALSE(base::PathExists(extensions_install_dir_.AppendASCII( | |
130 "hpiknbiabeeppbpihjehijgoemciehgk/3"))); | |
131 | |
132 // Make sure update information got deleted. | |
133 ExtensionPrefs* prefs = ExtensionPrefs::Get(profile_.get()); | |
134 EXPECT_FALSE( | |
135 prefs->GetDelayedInstallInfo("bjafgdebaacbbbecmhlhpofkepfkgcpa")); | |
136 } | |
137 | |
138 } // namespace extensions | |
OLD | NEW |