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/command_line.h" |
| 6 #include "base/files/file_path.h" |
| 7 #include "base/memory/ref_counted.h" |
| 8 #include "base/strings/stringprintf.h" |
| 9 #include "chrome/browser/extensions/api/storage/settings_storage_quota_enforcer.
h" |
| 10 #include "chrome/browser/extensions/api/storage/settings_test_util.h" |
| 11 #include "chrome/browser/extensions/api/storage/storage_api.h" |
| 12 #include "chrome/browser/extensions/extension_api_unittest.h" |
| 13 #include "chrome/browser/extensions/extension_service.h" |
| 14 #include "chrome/browser/extensions/test_extension_system.h" |
| 15 #include "chrome/browser/value_store/leveldb_value_store.h" |
| 16 #include "chrome/browser/value_store/value_store.h" |
| 17 #include "extensions/browser/event_router.h" |
| 18 #include "extensions/browser/extension_prefs.h" |
| 19 #include "extensions/browser/extension_system.h" |
| 20 #include "extensions/common/id_util.h" |
| 21 #include "extensions/common/manifest.h" |
| 22 #include "extensions/common/test_util.h" |
| 23 #include "third_party/leveldatabase/src/include/leveldb/db.h" |
| 24 #include "third_party/leveldatabase/src/include/leveldb/write_batch.h" |
| 25 |
| 26 namespace extensions { |
| 27 |
| 28 class StorageApiUnittest : public ExtensionApiUnittest { |
| 29 public: |
| 30 virtual void SetUp() OVERRIDE { |
| 31 ExtensionApiUnittest::SetUp(); |
| 32 TestExtensionSystem* extension_system = |
| 33 static_cast<TestExtensionSystem*>(ExtensionSystem::Get(profile())); |
| 34 ExtensionService* extension_service = extension_system->extension_service(); |
| 35 if (!extension_service) { |
| 36 extension_service = extension_system->CreateExtensionService( |
| 37 CommandLine::ForCurrentProcess(), base::FilePath(), false); |
| 38 CHECK(extension_service); |
| 39 } |
| 40 |
| 41 if (!extension_system->event_router()) { |
| 42 extension_system->SetEventRouter(scoped_ptr<EventRouter>( |
| 43 new EventRouter(profile(), ExtensionPrefs::Get(profile())))); |
| 44 } |
| 45 |
| 46 scoped_refptr<Extension> extension = |
| 47 test_util::CreateExtensionWithID(id_util::GenerateId("ext")); |
| 48 set_extension(extension); |
| 49 extension_service->AddExtension(extension.get()); |
| 50 } |
| 51 |
| 52 protected: |
| 53 void RunSetFunction(const std::string& key, const std::string& value) { |
| 54 RunFunction( |
| 55 new StorageStorageAreaSetFunction(), |
| 56 base::StringPrintf( |
| 57 "[\"local\", {\"%s\": \"%s\"}]", key.c_str(), value.c_str())); |
| 58 } |
| 59 |
| 60 std::string RunGetFunction(const std::string& key) { |
| 61 scoped_ptr<base::Value> value = RunFunctionAndReturnValue( |
| 62 new StorageStorageAreaGetFunction(), |
| 63 base::StringPrintf("[\"local\", \"%s\"]", key.c_str())); |
| 64 std::string result; |
| 65 base::DictionaryValue* dict = NULL; |
| 66 DCHECK(value->GetAsDictionary(&dict)); |
| 67 DCHECK(dict->GetString(key, &result)); |
| 68 return result; |
| 69 } |
| 70 }; |
| 71 |
| 72 TEST_F(StorageApiUnittest, RestoreCorruptedStorage) { |
| 73 const char kKey[] = "key"; |
| 74 const char kValue[] = "value"; |
| 75 |
| 76 // Do a simple set/get combo to make sure the API works. |
| 77 RunSetFunction(kKey, kValue); |
| 78 EXPECT_EQ(kValue, RunGetFunction(kKey)); |
| 79 |
| 80 // Corrupt the store. This is not as pretty as ideal, because we use knowledge |
| 81 // of the underlying structure, but there's no real good way to corrupt a |
| 82 // store other than directly modifying the files. |
| 83 ValueStore* store = |
| 84 settings_test_util::GetStorage(extension()->id(), |
| 85 settings_namespace::LOCAL, |
| 86 ExtensionSystem::Get(profile()) |
| 87 ->extension_service() |
| 88 ->settings_frontend()); |
| 89 ASSERT_TRUE(store); |
| 90 SettingsStorageQuotaEnforcer* quota_store = |
| 91 static_cast<SettingsStorageQuotaEnforcer*>(store); |
| 92 LeveldbValueStore* leveldb_store = |
| 93 static_cast<LeveldbValueStore*>(quota_store->get_delegate_for_test()); |
| 94 leveldb::WriteBatch batch; |
| 95 batch.Put(kKey, "[{(.*+\"\'\\"); |
| 96 EXPECT_TRUE(leveldb_store->WriteToDbForTest(&batch)); |
| 97 EXPECT_TRUE(leveldb_store->Get(kKey)->IsCorruption()); |
| 98 |
| 99 // Running another set should end up working (even though it will restore the |
| 100 // store behind the scenes). |
| 101 RunSetFunction(kKey, kValue); |
| 102 EXPECT_EQ(kValue, RunGetFunction(kKey)); |
| 103 } |
| 104 |
| 105 } // namespace extensions |
OLD | NEW |