OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "base/bind.h" | |
6 #include "base/file_util.h" | |
7 #include "base/files/scoped_temp_dir.h" | |
8 #include "base/memory/scoped_ptr.h" | |
9 #include "base/message_loop/message_loop.h" | |
10 #include "base/strings/stringprintf.h" | |
11 #include "chrome/browser/chrome_notification_types.h" | |
12 #include "chrome/browser/extensions/api/storage/leveldb_settings_storage_factory
.h" | |
13 #include "chrome/browser/extensions/api/storage/settings_frontend.h" | |
14 #include "chrome/browser/extensions/api/storage/settings_test_util.h" | |
15 #include "content/public/test/test_browser_thread.h" | |
16 #include "extensions/browser/api/storage/settings_namespace.h" | |
17 #include "extensions/browser/value_store/value_store.h" | |
18 #include "testing/gtest/include/gtest/gtest.h" | |
19 | |
20 using content::BrowserThread; | |
21 | |
22 namespace extensions { | |
23 | |
24 namespace settings = settings_namespace; | |
25 namespace util = settings_test_util; | |
26 | |
27 namespace { | |
28 | |
29 // To save typing ValueStore::DEFAULTS everywhere. | |
30 const ValueStore::WriteOptions DEFAULTS = ValueStore::DEFAULTS; | |
31 | |
32 // Creates a kilobyte of data. | |
33 scoped_ptr<base::Value> CreateKilobyte() { | |
34 std::string kilobyte_string; | |
35 for (int i = 0; i < 1024; ++i) { | |
36 kilobyte_string += "a"; | |
37 } | |
38 return scoped_ptr<base::Value>(new base::StringValue(kilobyte_string)); | |
39 } | |
40 | |
41 // Creates a megabyte of data. | |
42 scoped_ptr<base::Value> CreateMegabyte() { | |
43 base::ListValue* megabyte = new base::ListValue(); | |
44 for (int i = 0; i < 1000; ++i) { | |
45 megabyte->Append(CreateKilobyte().release()); | |
46 } | |
47 return scoped_ptr<base::Value>(megabyte); | |
48 } | |
49 | |
50 } // namespace | |
51 | |
52 class ExtensionSettingsFrontendTest : public testing::Test { | |
53 public: | |
54 ExtensionSettingsFrontendTest() | |
55 : storage_factory_(new util::ScopedSettingsStorageFactory()), | |
56 ui_thread_(BrowserThread::UI, base::MessageLoop::current()), | |
57 file_thread_(BrowserThread::FILE, base::MessageLoop::current()) {} | |
58 | |
59 virtual void SetUp() OVERRIDE { | |
60 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | |
61 profile_.reset(new util::MockProfile(temp_dir_.path())); | |
62 ResetFrontend(); | |
63 } | |
64 | |
65 virtual void TearDown() OVERRIDE { | |
66 frontend_.reset(); | |
67 profile_.reset(); | |
68 // Execute any pending deletion tasks. | |
69 message_loop_.RunUntilIdle(); | |
70 } | |
71 | |
72 protected: | |
73 Profile* profile() { return profile_.get(); } | |
74 | |
75 void ResetFrontend() { | |
76 storage_factory_->Reset(new LeveldbSettingsStorageFactory()); | |
77 frontend_.reset( | |
78 SettingsFrontend::CreateForTesting(storage_factory_, profile_.get())); | |
79 } | |
80 | |
81 base::ScopedTempDir temp_dir_; | |
82 scoped_ptr<util::MockProfile> profile_; | |
83 scoped_ptr<SettingsFrontend> frontend_; | |
84 scoped_refptr<util::ScopedSettingsStorageFactory> storage_factory_; | |
85 | |
86 private: | |
87 base::MessageLoop message_loop_; | |
88 content::TestBrowserThread ui_thread_; | |
89 content::TestBrowserThread file_thread_; | |
90 }; | |
91 | |
92 // Get a semblance of coverage for both extension and app settings by | |
93 // alternating in each test. | |
94 // TODO(kalman): explicitly test the two interact correctly. | |
95 | |
96 // Tests that the frontend is set up correctly. | |
97 TEST_F(ExtensionSettingsFrontendTest, Basics) { | |
98 // Local storage is always enabled. | |
99 EXPECT_TRUE(frontend_->IsStorageEnabled(settings::LOCAL)); | |
100 EXPECT_TRUE(frontend_->GetValueStoreCache(settings::LOCAL)); | |
101 | |
102 // Invalid storage areas are not available. | |
103 EXPECT_FALSE(frontend_->IsStorageEnabled(settings::INVALID)); | |
104 EXPECT_FALSE(frontend_->GetValueStoreCache(settings::INVALID)); | |
105 } | |
106 | |
107 TEST_F(ExtensionSettingsFrontendTest, SettingsPreservedAcrossReconstruction) { | |
108 const std::string id = "ext"; | |
109 scoped_refptr<const Extension> extension = | |
110 util::AddExtensionWithId(profile(), id, Manifest::TYPE_EXTENSION); | |
111 | |
112 ValueStore* storage = util::GetStorage(extension, frontend_.get()); | |
113 | |
114 // The correctness of Get/Set/Remove/Clear is tested elsewhere so no need to | |
115 // be too rigorous. | |
116 { | |
117 base::StringValue bar("bar"); | |
118 ValueStore::WriteResult result = storage->Set(DEFAULTS, "foo", bar); | |
119 ASSERT_FALSE(result->HasError()); | |
120 } | |
121 | |
122 { | |
123 ValueStore::ReadResult result = storage->Get(); | |
124 ASSERT_FALSE(result->HasError()); | |
125 EXPECT_FALSE(result->settings().empty()); | |
126 } | |
127 | |
128 ResetFrontend(); | |
129 storage = util::GetStorage(extension, frontend_.get()); | |
130 | |
131 { | |
132 ValueStore::ReadResult result = storage->Get(); | |
133 ASSERT_FALSE(result->HasError()); | |
134 EXPECT_FALSE(result->settings().empty()); | |
135 } | |
136 } | |
137 | |
138 TEST_F(ExtensionSettingsFrontendTest, SettingsClearedOnUninstall) { | |
139 const std::string id = "ext"; | |
140 scoped_refptr<const Extension> extension = util::AddExtensionWithId( | |
141 profile(), id, Manifest::TYPE_LEGACY_PACKAGED_APP); | |
142 | |
143 ValueStore* storage = util::GetStorage(extension, frontend_.get()); | |
144 | |
145 { | |
146 base::StringValue bar("bar"); | |
147 ValueStore::WriteResult result = storage->Set(DEFAULTS, "foo", bar); | |
148 ASSERT_FALSE(result->HasError()); | |
149 } | |
150 | |
151 // This would be triggered by extension uninstall via a DataDeleter. | |
152 frontend_->DeleteStorageSoon(id); | |
153 base::MessageLoop::current()->RunUntilIdle(); | |
154 | |
155 // The storage area may no longer be valid post-uninstall, so re-request. | |
156 storage = util::GetStorage(extension, frontend_.get()); | |
157 { | |
158 ValueStore::ReadResult result = storage->Get(); | |
159 ASSERT_FALSE(result->HasError()); | |
160 EXPECT_TRUE(result->settings().empty()); | |
161 } | |
162 } | |
163 | |
164 TEST_F(ExtensionSettingsFrontendTest, LeveldbDatabaseDeletedFromDiskOnClear) { | |
165 const std::string id = "ext"; | |
166 scoped_refptr<const Extension> extension = | |
167 util::AddExtensionWithId(profile(), id, Manifest::TYPE_EXTENSION); | |
168 | |
169 ValueStore* storage = util::GetStorage(extension, frontend_.get()); | |
170 | |
171 { | |
172 base::StringValue bar("bar"); | |
173 ValueStore::WriteResult result = storage->Set(DEFAULTS, "foo", bar); | |
174 ASSERT_FALSE(result->HasError()); | |
175 EXPECT_TRUE(base::PathExists(temp_dir_.path())); | |
176 } | |
177 | |
178 // Should need to both clear the database and delete the frontend for the | |
179 // leveldb database to be deleted from disk. | |
180 { | |
181 ValueStore::WriteResult result = storage->Clear(); | |
182 ASSERT_FALSE(result->HasError()); | |
183 EXPECT_TRUE(base::PathExists(temp_dir_.path())); | |
184 } | |
185 | |
186 frontend_.reset(); | |
187 base::MessageLoop::current()->RunUntilIdle(); | |
188 // TODO(kalman): Figure out why this fails, despite appearing to work. | |
189 // Leaving this commented out rather than disabling the whole test so that the | |
190 // deletion code paths are at least exercised. | |
191 //EXPECT_FALSE(base::PathExists(temp_dir_.path())); | |
192 } | |
193 | |
194 // Disabled (slow), http://crbug.com/322751 . | |
195 TEST_F(ExtensionSettingsFrontendTest, | |
196 DISABLED_QuotaLimitsEnforcedCorrectlyForSyncAndLocal) { | |
197 const std::string id = "ext"; | |
198 scoped_refptr<const Extension> extension = | |
199 util::AddExtensionWithId(profile(), id, Manifest::TYPE_EXTENSION); | |
200 | |
201 ValueStore* sync_storage = | |
202 util::GetStorage(extension, settings::SYNC, frontend_.get()); | |
203 ValueStore* local_storage = | |
204 util::GetStorage(extension, settings::LOCAL, frontend_.get()); | |
205 | |
206 // Sync storage should run out after ~100K. | |
207 scoped_ptr<base::Value> kilobyte = CreateKilobyte(); | |
208 for (int i = 0; i < 100; ++i) { | |
209 sync_storage->Set( | |
210 ValueStore::DEFAULTS, base::StringPrintf("%d", i), *kilobyte); | |
211 } | |
212 | |
213 EXPECT_TRUE(sync_storage->Set( | |
214 ValueStore::DEFAULTS, "WillError", *kilobyte)->HasError()); | |
215 | |
216 // Local storage shouldn't run out after ~100K. | |
217 for (int i = 0; i < 100; ++i) { | |
218 local_storage->Set( | |
219 ValueStore::DEFAULTS, base::StringPrintf("%d", i), *kilobyte); | |
220 } | |
221 | |
222 EXPECT_FALSE(local_storage->Set( | |
223 ValueStore::DEFAULTS, "WontError", *kilobyte)->HasError()); | |
224 | |
225 // Local storage should run out after ~5MB. | |
226 scoped_ptr<base::Value> megabyte = CreateMegabyte(); | |
227 for (int i = 0; i < 5; ++i) { | |
228 local_storage->Set( | |
229 ValueStore::DEFAULTS, base::StringPrintf("%d", i), *megabyte); | |
230 } | |
231 | |
232 EXPECT_TRUE(local_storage->Set( | |
233 ValueStore::DEFAULTS, "WillError", *megabyte)->HasError()); | |
234 } | |
235 | |
236 // In other tests, we assume that the result of GetStorage is a pointer to the | |
237 // a Storage owned by a Frontend object, but for the unlimitedStorage case, this | |
238 // might not be true. So, write the tests in a "callback" style. | |
239 // We should really rewrite all tests to be asynchronous in this way. | |
240 | |
241 static void UnlimitedSyncStorageTestCallback(ValueStore* sync_storage) { | |
242 // Sync storage should still run out after ~100K; the unlimitedStorage | |
243 // permission can't apply to sync. | |
244 scoped_ptr<base::Value> kilobyte = CreateKilobyte(); | |
245 for (int i = 0; i < 100; ++i) { | |
246 sync_storage->Set( | |
247 ValueStore::DEFAULTS, base::StringPrintf("%d", i), *kilobyte); | |
248 } | |
249 | |
250 EXPECT_TRUE(sync_storage->Set( | |
251 ValueStore::DEFAULTS, "WillError", *kilobyte)->HasError()); | |
252 } | |
253 | |
254 static void UnlimitedLocalStorageTestCallback(ValueStore* local_storage) { | |
255 // Local storage should never run out. | |
256 scoped_ptr<base::Value> megabyte = CreateMegabyte(); | |
257 for (int i = 0; i < 7; ++i) { | |
258 local_storage->Set( | |
259 ValueStore::DEFAULTS, base::StringPrintf("%d", i), *megabyte); | |
260 } | |
261 | |
262 EXPECT_FALSE(local_storage->Set( | |
263 ValueStore::DEFAULTS, "WontError", *megabyte)->HasError()); | |
264 } | |
265 | |
266 #if defined(OS_WIN) | |
267 // See: http://crbug.com/227296 | |
268 #define MAYBE_UnlimitedStorageForLocalButNotSync \ | |
269 DISABLED_UnlimitedStorageForLocalButNotSync | |
270 #else | |
271 #define MAYBE_UnlimitedStorageForLocalButNotSync \ | |
272 UnlimitedStorageForLocalButNotSync | |
273 #endif | |
274 | |
275 TEST_F(ExtensionSettingsFrontendTest, | |
276 MAYBE_UnlimitedStorageForLocalButNotSync) { | |
277 const std::string id = "ext"; | |
278 std::set<std::string> permissions; | |
279 permissions.insert("unlimitedStorage"); | |
280 scoped_refptr<const Extension> extension = | |
281 util::AddExtensionWithIdAndPermissions( | |
282 profile(), id, Manifest::TYPE_EXTENSION, permissions); | |
283 | |
284 frontend_->RunWithStorage( | |
285 extension, settings::SYNC, base::Bind(&UnlimitedSyncStorageTestCallback)); | |
286 frontend_->RunWithStorage(extension, | |
287 settings::LOCAL, | |
288 base::Bind(&UnlimitedLocalStorageTestCallback)); | |
289 | |
290 base::MessageLoop::current()->RunUntilIdle(); | |
291 } | |
292 | |
293 } // namespace extensions | |
OLD | NEW |