| 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 "testing/gtest/include/gtest/gtest.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/file_util.h" | |
| 9 #include "base/files/scoped_temp_dir.h" | |
| 10 #include "base/memory/scoped_ptr.h" | |
| 11 #include "base/message_loop.h" | |
| 12 #include "base/stringprintf.h" | |
| 13 #include "chrome/browser/extensions/settings/leveldb_settings_storage_factory.h" | |
| 14 #include "chrome/browser/extensions/settings/settings_frontend.h" | |
| 15 #include "chrome/browser/extensions/settings/settings_namespace.h" | |
| 16 #include "chrome/browser/extensions/settings/settings_test_util.h" | |
| 17 #include "chrome/browser/value_store/value_store.h" | |
| 18 #include "chrome/common/chrome_notification_types.h" | |
| 19 #include "content/public/test/test_browser_thread.h" | |
| 20 | |
| 21 using content::BrowserThread; | |
| 22 | |
| 23 namespace extensions { | |
| 24 | |
| 25 namespace settings = settings_namespace; | |
| 26 namespace util = settings_test_util; | |
| 27 | |
| 28 namespace { | |
| 29 | |
| 30 // To save typing ValueStore::DEFAULTS everywhere. | |
| 31 const ValueStore::WriteOptions DEFAULTS = ValueStore::DEFAULTS; | |
| 32 | |
| 33 // Creates a kilobyte of data. | |
| 34 scoped_ptr<Value> CreateKilobyte() { | |
| 35 std::string kilobyte_string; | |
| 36 for (int i = 0; i < 1024; ++i) { | |
| 37 kilobyte_string += "a"; | |
| 38 } | |
| 39 return scoped_ptr<Value>(Value::CreateStringValue(kilobyte_string)); | |
| 40 } | |
| 41 | |
| 42 // Creates a megabyte of data. | |
| 43 scoped_ptr<Value> CreateMegabyte() { | |
| 44 ListValue* megabyte = new ListValue(); | |
| 45 for (int i = 0; i < 1000; ++i) { | |
| 46 megabyte->Append(CreateKilobyte().release()); | |
| 47 } | |
| 48 return scoped_ptr<Value>(megabyte); | |
| 49 } | |
| 50 | |
| 51 } | |
| 52 | |
| 53 class ExtensionSettingsFrontendTest : public testing::Test { | |
| 54 public: | |
| 55 ExtensionSettingsFrontendTest() | |
| 56 : storage_factory_(new util::ScopedSettingsStorageFactory()), | |
| 57 ui_thread_(BrowserThread::UI, MessageLoop::current()), | |
| 58 file_thread_(BrowserThread::FILE, MessageLoop::current()) {} | |
| 59 | |
| 60 virtual void SetUp() OVERRIDE { | |
| 61 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | |
| 62 profile_.reset(new util::MockProfile(temp_dir_.path())); | |
| 63 ResetFrontend(); | |
| 64 } | |
| 65 | |
| 66 virtual void TearDown() OVERRIDE { | |
| 67 frontend_.reset(); | |
| 68 profile_.reset(); | |
| 69 // Execute any pending deletion tasks. | |
| 70 message_loop_.RunUntilIdle(); | |
| 71 } | |
| 72 | |
| 73 protected: | |
| 74 void ResetFrontend() { | |
| 75 storage_factory_->Reset(new LeveldbSettingsStorageFactory()); | |
| 76 frontend_.reset( | |
| 77 SettingsFrontend::Create(storage_factory_.get(), profile_.get())); | |
| 78 } | |
| 79 | |
| 80 base::ScopedTempDir temp_dir_; | |
| 81 scoped_ptr<util::MockProfile> profile_; | |
| 82 scoped_ptr<SettingsFrontend> frontend_; | |
| 83 scoped_refptr<util::ScopedSettingsStorageFactory> storage_factory_; | |
| 84 | |
| 85 private: | |
| 86 MessageLoop message_loop_; | |
| 87 content::TestBrowserThread ui_thread_; | |
| 88 content::TestBrowserThread file_thread_; | |
| 89 }; | |
| 90 | |
| 91 // Get a semblance of coverage for both extension and app settings by | |
| 92 // alternating in each test. | |
| 93 // TODO(kalman): explicitly test the two interact correctly. | |
| 94 | |
| 95 TEST_F(ExtensionSettingsFrontendTest, SettingsPreservedAcrossReconstruction) { | |
| 96 const std::string id = "ext"; | |
| 97 ExtensionServiceInterface* esi = | |
| 98 extensions::ExtensionSystem::Get(profile_.get())->extension_service(); | |
| 99 static_cast<extensions::settings_test_util::MockExtensionService*>(esi)-> | |
| 100 AddExtensionWithId(id, Extension::TYPE_EXTENSION); | |
| 101 | |
| 102 ValueStore* storage = util::GetStorage(id, frontend_.get()); | |
| 103 | |
| 104 // The correctness of Get/Set/Remove/Clear is tested elsewhere so no need to | |
| 105 // be too rigorous. | |
| 106 { | |
| 107 StringValue bar("bar"); | |
| 108 ValueStore::WriteResult result = storage->Set(DEFAULTS, "foo", bar); | |
| 109 ASSERT_FALSE(result->HasError()); | |
| 110 } | |
| 111 | |
| 112 { | |
| 113 ValueStore::ReadResult result = storage->Get(); | |
| 114 ASSERT_FALSE(result->HasError()); | |
| 115 EXPECT_FALSE(result->settings()->empty()); | |
| 116 } | |
| 117 | |
| 118 ResetFrontend(); | |
| 119 storage = util::GetStorage(id, frontend_.get()); | |
| 120 | |
| 121 { | |
| 122 ValueStore::ReadResult result = storage->Get(); | |
| 123 ASSERT_FALSE(result->HasError()); | |
| 124 EXPECT_FALSE(result->settings()->empty()); | |
| 125 } | |
| 126 } | |
| 127 | |
| 128 TEST_F(ExtensionSettingsFrontendTest, SettingsClearedOnUninstall) { | |
| 129 const std::string id = "ext"; | |
| 130 ExtensionServiceInterface* esi = | |
| 131 extensions::ExtensionSystem::Get(profile_.get())->extension_service(); | |
| 132 static_cast<extensions::settings_test_util::MockExtensionService*>(esi)-> | |
| 133 AddExtensionWithId(id, Extension::TYPE_LEGACY_PACKAGED_APP); | |
| 134 | |
| 135 ValueStore* storage = util::GetStorage(id, frontend_.get()); | |
| 136 | |
| 137 { | |
| 138 StringValue bar("bar"); | |
| 139 ValueStore::WriteResult result = storage->Set(DEFAULTS, "foo", bar); | |
| 140 ASSERT_FALSE(result->HasError()); | |
| 141 } | |
| 142 | |
| 143 // This would be triggered by extension uninstall via a DataDeleter. | |
| 144 frontend_->DeleteStorageSoon(id); | |
| 145 MessageLoop::current()->RunUntilIdle(); | |
| 146 | |
| 147 // The storage area may no longer be valid post-uninstall, so re-request. | |
| 148 storage = util::GetStorage(id, frontend_.get()); | |
| 149 { | |
| 150 ValueStore::ReadResult result = storage->Get(); | |
| 151 ASSERT_FALSE(result->HasError()); | |
| 152 EXPECT_TRUE(result->settings()->empty()); | |
| 153 } | |
| 154 } | |
| 155 | |
| 156 TEST_F(ExtensionSettingsFrontendTest, LeveldbDatabaseDeletedFromDiskOnClear) { | |
| 157 const std::string id = "ext"; | |
| 158 ExtensionServiceInterface* esi = | |
| 159 extensions::ExtensionSystem::Get(profile_.get())->extension_service(); | |
| 160 static_cast<extensions::settings_test_util::MockExtensionService*>(esi)-> | |
| 161 AddExtensionWithId(id, Extension::TYPE_EXTENSION); | |
| 162 | |
| 163 ValueStore* storage = util::GetStorage(id, frontend_.get()); | |
| 164 | |
| 165 { | |
| 166 StringValue bar("bar"); | |
| 167 ValueStore::WriteResult result = storage->Set(DEFAULTS, "foo", bar); | |
| 168 ASSERT_FALSE(result->HasError()); | |
| 169 EXPECT_TRUE(file_util::PathExists(temp_dir_.path())); | |
| 170 } | |
| 171 | |
| 172 // Should need to both clear the database and delete the frontend for the | |
| 173 // leveldb database to be deleted from disk. | |
| 174 { | |
| 175 ValueStore::WriteResult result = storage->Clear(); | |
| 176 ASSERT_FALSE(result->HasError()); | |
| 177 EXPECT_TRUE(file_util::PathExists(temp_dir_.path())); | |
| 178 } | |
| 179 | |
| 180 frontend_.reset(); | |
| 181 MessageLoop::current()->RunUntilIdle(); | |
| 182 // TODO(kalman): Figure out why this fails, despite appearing to work. | |
| 183 // Leaving this commented out rather than disabling the whole test so that the | |
| 184 // deletion code paths are at least exercised. | |
| 185 //EXPECT_FALSE(file_util::PathExists(temp_dir_.path())); | |
| 186 } | |
| 187 | |
| 188 #if defined(OS_WIN) | |
| 189 // Failing on vista dbg. http://crbug.com/111100, http://crbug.com/108724 | |
| 190 #define QuotaLimitsEnforcedCorrectlyForSyncAndLocal \ | |
| 191 DISABLED_QuotaLimitsEnforcedCorrectlyForSyncAndLocal | |
| 192 #endif | |
| 193 TEST_F(ExtensionSettingsFrontendTest, | |
| 194 QuotaLimitsEnforcedCorrectlyForSyncAndLocal) { | |
| 195 const std::string id = "ext"; | |
| 196 ExtensionServiceInterface* esi = | |
| 197 extensions::ExtensionSystem::Get(profile_.get())->extension_service(); | |
| 198 static_cast<extensions::settings_test_util::MockExtensionService*>(esi)-> | |
| 199 AddExtensionWithId(id, Extension::TYPE_EXTENSION); | |
| 200 | |
| 201 ValueStore* sync_storage = | |
| 202 util::GetStorage(id, settings::SYNC, frontend_.get()); | |
| 203 ValueStore* local_storage = | |
| 204 util::GetStorage(id, settings::LOCAL, frontend_.get()); | |
| 205 | |
| 206 // Sync storage should run out after ~100K. | |
| 207 scoped_ptr<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<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<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<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 // Failing on vista dbg. http://crbug.com/111100, http://crbug.com/108724 | |
| 268 #define UnlimitedStorageForLocalButNotSync DISABLED_UnlimitedStorageForLocalButN
otSync | |
| 269 #endif | |
| 270 TEST_F(ExtensionSettingsFrontendTest, | |
| 271 UnlimitedStorageForLocalButNotSync) { | |
| 272 const std::string id = "ext"; | |
| 273 std::set<std::string> permissions; | |
| 274 permissions.insert("unlimitedStorage"); | |
| 275 ExtensionServiceInterface* esi = | |
| 276 extensions::ExtensionSystem::Get(profile_.get())->extension_service(); | |
| 277 static_cast<extensions::settings_test_util::MockExtensionService*>(esi)-> | |
| 278 AddExtensionWithIdAndPermissions(id, Extension::TYPE_EXTENSION, | |
| 279 permissions); | |
| 280 | |
| 281 frontend_->RunWithStorage( | |
| 282 id, settings::SYNC, base::Bind(&UnlimitedSyncStorageTestCallback)); | |
| 283 frontend_->RunWithStorage( | |
| 284 id, settings::LOCAL, base::Bind(&UnlimitedLocalStorageTestCallback)); | |
| 285 | |
| 286 MessageLoop::current()->RunUntilIdle(); | |
| 287 } | |
| 288 | |
| 289 } // namespace extensions | |
| OLD | NEW |