| Index: chrome/browser/extensions/api/storage/storage_api_unittest.cc
|
| diff --git a/chrome/browser/extensions/api/storage/storage_api_unittest.cc b/chrome/browser/extensions/api/storage/storage_api_unittest.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..111629f2fee1b0fe0f95a5ffe09cf88a46585962
|
| --- /dev/null
|
| +++ b/chrome/browser/extensions/api/storage/storage_api_unittest.cc
|
| @@ -0,0 +1,105 @@
|
| +// Copyright 2014 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +#include "base/command_line.h"
|
| +#include "base/files/file_path.h"
|
| +#include "base/memory/ref_counted.h"
|
| +#include "base/strings/stringprintf.h"
|
| +#include "chrome/browser/extensions/api/storage/settings_storage_quota_enforcer.h"
|
| +#include "chrome/browser/extensions/api/storage/settings_test_util.h"
|
| +#include "chrome/browser/extensions/api/storage/storage_api.h"
|
| +#include "chrome/browser/extensions/extension_api_unittest.h"
|
| +#include "chrome/browser/extensions/extension_service.h"
|
| +#include "chrome/browser/extensions/test_extension_system.h"
|
| +#include "chrome/browser/value_store/leveldb_value_store.h"
|
| +#include "chrome/browser/value_store/value_store.h"
|
| +#include "extensions/browser/event_router.h"
|
| +#include "extensions/browser/extension_prefs.h"
|
| +#include "extensions/browser/extension_system.h"
|
| +#include "extensions/common/id_util.h"
|
| +#include "extensions/common/manifest.h"
|
| +#include "extensions/common/test_util.h"
|
| +#include "third_party/leveldatabase/src/include/leveldb/db.h"
|
| +#include "third_party/leveldatabase/src/include/leveldb/write_batch.h"
|
| +
|
| +namespace extensions {
|
| +
|
| +class StorageApiUnittest : public ExtensionApiUnittest {
|
| + public:
|
| + virtual void SetUp() OVERRIDE {
|
| + ExtensionApiUnittest::SetUp();
|
| + TestExtensionSystem* extension_system =
|
| + static_cast<TestExtensionSystem*>(ExtensionSystem::Get(profile()));
|
| + ExtensionService* extension_service = extension_system->extension_service();
|
| + if (!extension_service) {
|
| + extension_service = extension_system->CreateExtensionService(
|
| + CommandLine::ForCurrentProcess(), base::FilePath(), false);
|
| + CHECK(extension_service);
|
| + }
|
| +
|
| + if (!extension_system->event_router()) {
|
| + extension_system->SetEventRouter(scoped_ptr<EventRouter>(
|
| + new EventRouter(profile(), ExtensionPrefs::Get(profile()))));
|
| + }
|
| +
|
| + scoped_refptr<Extension> extension =
|
| + test_util::CreateExtensionWithID(id_util::GenerateId("ext"));
|
| + set_extension(extension);
|
| + extension_service->AddExtension(extension.get());
|
| + }
|
| +
|
| + protected:
|
| + void RunSetFunction(const std::string& key, const std::string& value) {
|
| + RunFunction(
|
| + new StorageStorageAreaSetFunction(),
|
| + base::StringPrintf(
|
| + "[\"local\", {\"%s\": \"%s\"}]", key.c_str(), value.c_str()));
|
| + }
|
| +
|
| + std::string RunGetFunction(const std::string& key) {
|
| + scoped_ptr<base::Value> value = RunFunctionAndReturnValue(
|
| + new StorageStorageAreaGetFunction(),
|
| + base::StringPrintf("[\"local\", \"%s\"]", key.c_str()));
|
| + std::string result;
|
| + base::DictionaryValue* dict = NULL;
|
| + DCHECK(value->GetAsDictionary(&dict));
|
| + DCHECK(dict->GetString(key, &result));
|
| + return result;
|
| + }
|
| +};
|
| +
|
| +TEST_F(StorageApiUnittest, RestoreCorruptedStorage) {
|
| + const char kKey[] = "key";
|
| + const char kValue[] = "value";
|
| +
|
| + // Do a simple set/get combo to make sure the API works.
|
| + RunSetFunction(kKey, kValue);
|
| + EXPECT_EQ(kValue, RunGetFunction(kKey));
|
| +
|
| + // Corrupt the store. This is not as pretty as ideal, because we use knowledge
|
| + // of the underlying structure, but there's no real good way to corrupt a
|
| + // store other than directly modifying the files.
|
| + ValueStore* store =
|
| + settings_test_util::GetStorage(extension()->id(),
|
| + settings_namespace::LOCAL,
|
| + ExtensionSystem::Get(profile())
|
| + ->extension_service()
|
| + ->settings_frontend());
|
| + ASSERT_TRUE(store);
|
| + SettingsStorageQuotaEnforcer* quota_store =
|
| + static_cast<SettingsStorageQuotaEnforcer*>(store);
|
| + LeveldbValueStore* leveldb_store =
|
| + static_cast<LeveldbValueStore*>(quota_store->get_delegate_for_test());
|
| + leveldb::WriteBatch batch;
|
| + batch.Put(kKey, "[{(.*+\"\'\\");
|
| + EXPECT_TRUE(leveldb_store->WriteToDbForTest(&batch));
|
| + EXPECT_TRUE(leveldb_store->Get(kKey)->IsCorruption());
|
| +
|
| + // Running another set should end up working (even though it will restore the
|
| + // store behind the scenes).
|
| + RunSetFunction(kKey, kValue);
|
| + EXPECT_EQ(kValue, RunGetFunction(kKey));
|
| +}
|
| +
|
| +} // namespace extensions
|
|
|