This source file includes following definitions.
- CreateStorageFrontendForTesting
- SetUp
- RunSetFunction
- RunGetFunction
- TEST_F
#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/extension_api_unittest.h"
#include "chrome/browser/extensions/test_extension_system.h"
#include "extensions/browser/api/storage/leveldb_settings_storage_factory.h"
#include "extensions/browser/api/storage/settings_storage_quota_enforcer.h"
#include "extensions/browser/api/storage/settings_test_util.h"
#include "extensions/browser/api/storage/storage_api.h"
#include "extensions/browser/api/storage/storage_frontend.h"
#include "extensions/browser/event_router.h"
#include "extensions/browser/extension_prefs.h"
#include "extensions/browser/extension_system.h"
#include "extensions/browser/value_store/leveldb_value_store.h"
#include "extensions/browser/value_store/value_store.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 {
namespace {
KeyedService* CreateStorageFrontendForTesting(
content::BrowserContext* context) {
return StorageFrontend::CreateForTesting(new LeveldbSettingsStorageFactory(),
context);
}
}
class StorageApiUnittest : public ExtensionApiUnittest {
public:
virtual void SetUp() OVERRIDE {
ExtensionApiUnittest::SetUp();
TestExtensionSystem* extension_system =
static_cast<TestExtensionSystem*>(ExtensionSystem::Get(profile()));
extension_system->SetEventRouter(scoped_ptr<EventRouter>(
new EventRouter(profile(), ExtensionPrefs::Get(profile()))));
}
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()));
}
testing::AssertionResult RunGetFunction(const std::string& key,
std::string* value) {
scoped_ptr<base::Value> result = RunFunctionAndReturnValue(
new StorageStorageAreaGetFunction(),
base::StringPrintf("[\"local\", \"%s\"]", key.c_str()));
base::DictionaryValue* dict = NULL;
if (!result->GetAsDictionary(&dict))
return testing::AssertionFailure() << result << " was not a dictionary.";
if (!dict->GetString(key, value)) {
return testing::AssertionFailure() << " could not retrieve a string from"
<< dict << " at " << key;
}
return testing::AssertionSuccess();
}
};
TEST_F(StorageApiUnittest, RestoreCorruptedStorage) {
StorageFrontend::GetFactoryInstance()->SetTestingFactory(
profile(), &CreateStorageFrontendForTesting);
const char kKey[] = "key";
const char kValue[] = "value";
std::string result;
RunSetFunction(kKey, kValue);
EXPECT_TRUE(RunGetFunction(kKey, &result));
EXPECT_EQ(kValue, result);
ValueStore* store =
settings_test_util::GetStorage(extension_ref(),
settings_namespace::LOCAL,
StorageFrontend::Get(profile()));
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)->IsCorrupted());
RunSetFunction(kKey, kValue);
EXPECT_TRUE(RunGetFunction(kKey, &result));
EXPECT_EQ(kValue, result);
}
}