This source file includes following definitions.
- Param
- SetUp
- TearDown
- CloseStore
- OpenStore
- store
- database_path
- TEST_F
- TEST_F
- TEST_F
#include "extensions/browser/value_store/value_store_unittest.h"
#include "base/file_util.h"
#include "base/files/file_enumerator.h"
#include "base/files/scoped_temp_dir.h"
#include "base/memory/ref_counted.h"
#include "base/message_loop/message_loop.h"
#include "base/values.h"
#include "content/public/test/test_browser_thread_bundle.h"
#include "extensions/browser/value_store/leveldb_value_store.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/leveldatabase/src/include/leveldb/db.h"
#include "third_party/leveldatabase/src/include/leveldb/write_batch.h"
namespace {
ValueStore* Param(const base::FilePath& file_path) {
return new LeveldbValueStore(file_path);
}
}
INSTANTIATE_TEST_CASE_P(
LeveldbValueStore,
ValueStoreTest,
testing::Values(&Param));
class LeveldbValueStoreUnitTest : public testing::Test {
public:
LeveldbValueStoreUnitTest() {}
virtual ~LeveldbValueStoreUnitTest() {}
protected:
virtual void SetUp() OVERRIDE {
ASSERT_TRUE(database_dir_.CreateUniqueTempDir());
OpenStore();
ASSERT_FALSE(store_->Get()->HasError());
}
virtual void TearDown() OVERRIDE {
store_->Clear();
store_.reset();
}
void CloseStore() { store_.reset(); }
void OpenStore() { store_.reset(new LeveldbValueStore(database_path())); }
LeveldbValueStore* store() { return store_.get(); }
const base::FilePath& database_path() { return database_dir_.path(); }
private:
scoped_ptr<LeveldbValueStore> store_;
base::ScopedTempDir database_dir_;
content::TestBrowserThreadBundle thread_bundle_;
};
TEST_F(LeveldbValueStoreUnitTest, RestoreKeyTest) {
const char kNotCorruptKey[] = "not-corrupt";
const char kValue[] = "value";
scoped_ptr<base::Value> value(base::Value::CreateStringValue(kValue));
ASSERT_FALSE(
store()->Set(ValueStore::DEFAULTS, kNotCorruptKey, *value)->HasError());
const char kCorruptKey[] = "corrupt";
leveldb::WriteBatch batch;
batch.Put(kCorruptKey, "[{(.*+\"\'\\");
ASSERT_TRUE(store()->WriteToDbForTest(&batch));
ValueStore::ReadResult result = store()->Get(kCorruptKey);
ASSERT_TRUE(result->HasError());
ASSERT_EQ(ValueStore::CORRUPTION, result->error().code);
ASSERT_TRUE(store()->RestoreKey(kCorruptKey));
result = store()->Get(kCorruptKey);
EXPECT_FALSE(result->HasError());
EXPECT_TRUE(result->settings().empty());
result = store()->Get(kNotCorruptKey);
EXPECT_FALSE(result->HasError());
EXPECT_TRUE(result->settings().HasKey(kNotCorruptKey));
std::string value_string;
EXPECT_TRUE(result->settings().GetString(kNotCorruptKey, &value_string));
EXPECT_EQ(kValue, value_string);
}
TEST_F(LeveldbValueStoreUnitTest, RestoreDoesMinimumNecessary) {
const char* kNotCorruptKeys[] = {"a", "n", "z"};
const size_t kNotCorruptKeysSize = 3u;
const char kCorruptKey1[] = "f";
const char kCorruptKey2[] = "s";
const char kValue[] = "value";
const char kCorruptValue[] = "[{(.*+\"\'\\";
scoped_ptr<base::Value> value(base::Value::CreateStringValue(kValue));
for (size_t i = 0; i < kNotCorruptKeysSize; ++i) {
ASSERT_FALSE(store()
->Set(ValueStore::DEFAULTS, kNotCorruptKeys[i], *value)
->HasError());
}
leveldb::WriteBatch batch;
batch.Put(kCorruptKey1, kCorruptValue);
batch.Put(kCorruptKey2, kCorruptValue);
ASSERT_TRUE(store()->WriteToDbForTest(&batch));
ValueStore::ReadResult result = store()->Get();
ASSERT_TRUE(result->HasError());
ASSERT_EQ(ValueStore::CORRUPTION, result->error().code);
ASSERT_TRUE(store()->Restore());
std::string value_string;
for (size_t i = 0; i < kNotCorruptKeysSize; ++i) {
result = store()->Get(kNotCorruptKeys[i]);
EXPECT_FALSE(result->HasError());
EXPECT_TRUE(result->settings().HasKey(kNotCorruptKeys[i]));
EXPECT_TRUE(
result->settings().GetString(kNotCorruptKeys[i], &value_string));
EXPECT_EQ(kValue, value_string);
}
}
TEST_F(LeveldbValueStoreUnitTest, RestoreFullDatabase) {
const std::string kLolCats("I can haz leveldb filez?");
const char* kNotCorruptKeys[] = {"a", "n", "z"};
const size_t kNotCorruptKeysSize = 3u;
const char kValue[] = "value";
scoped_ptr<base::Value> value(base::Value::CreateStringValue(kValue));
for (size_t i = 0; i < kNotCorruptKeysSize; ++i) {
ASSERT_FALSE(store()
->Set(ValueStore::DEFAULTS, kNotCorruptKeys[i], *value)
->HasError());
}
CloseStore();
base::FileEnumerator enumerator(
database_path(), true , base::FileEnumerator::FILES);
for (base::FilePath file = enumerator.Next(); !file.empty();
file = enumerator.Next()) {
ASSERT_NE(base::WriteFile(file, kLolCats.c_str(), kLolCats.length()),
-1);
}
OpenStore();
ValueStore::ReadResult result = store()->Get();
ASSERT_TRUE(result->HasError());
ASSERT_EQ(ValueStore::CORRUPTION, result->error().code);
ASSERT_TRUE(store()->Restore());
result = store()->Get();
EXPECT_FALSE(result->HasError());
EXPECT_EQ(0u, result->settings().size());
}