Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / extensions / browser / value_store / leveldb_value_store_unittest.cc
blob0604499fab01f7b4f0959b3bbfb6178037f73723
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.
5 #include "extensions/browser/value_store/value_store_unittest.h"
7 #include "base/files/file_enumerator.h"
8 #include "base/files/file_util.h"
9 #include "base/files/scoped_temp_dir.h"
10 #include "base/memory/ref_counted.h"
11 #include "base/message_loop/message_loop.h"
12 #include "base/values.h"
13 #include "content/public/test/test_browser_thread_bundle.h"
14 #include "extensions/browser/value_store/leveldb_value_store.h"
15 #include "testing/gtest/include/gtest/gtest.h"
16 #include "third_party/leveldatabase/src/include/leveldb/db.h"
17 #include "third_party/leveldatabase/src/include/leveldb/write_batch.h"
19 namespace {
21 const char kDatabaseUMAClientName[] = "Test";
23 ValueStore* Param(const base::FilePath& file_path) {
24 return new LeveldbValueStore(kDatabaseUMAClientName, file_path);
27 } // namespace
29 INSTANTIATE_TEST_CASE_P(
30 LeveldbValueStore,
31 ValueStoreTest,
32 testing::Values(&Param));
34 class LeveldbValueStoreUnitTest : public testing::Test {
35 public:
36 LeveldbValueStoreUnitTest() {}
37 ~LeveldbValueStoreUnitTest() override {}
39 protected:
40 void SetUp() override {
41 ASSERT_TRUE(database_dir_.CreateUniqueTempDir());
42 OpenStore();
43 ASSERT_FALSE(store_->Get()->HasError());
46 void TearDown() override {
47 store_->Clear();
48 store_.reset();
51 void CloseStore() { store_.reset(); }
53 void OpenStore() {
54 store_.reset(
55 new LeveldbValueStore(kDatabaseUMAClientName, database_path()));
58 LeveldbValueStore* store() { return store_.get(); }
59 const base::FilePath& database_path() { return database_dir_.path(); }
61 private:
62 scoped_ptr<LeveldbValueStore> store_;
63 base::ScopedTempDir database_dir_;
65 content::TestBrowserThreadBundle thread_bundle_;
68 // Check that we can restore a single corrupted key in the LeveldbValueStore.
69 TEST_F(LeveldbValueStoreUnitTest, RestoreKeyTest) {
70 const char kNotCorruptKey[] = "not-corrupt";
71 const char kValue[] = "value";
73 // Insert a valid pair.
74 scoped_ptr<base::Value> value(new base::StringValue(kValue));
75 ASSERT_FALSE(
76 store()->Set(ValueStore::DEFAULTS, kNotCorruptKey, *value)->HasError());
78 // Insert a corrupt pair.
79 const char kCorruptKey[] = "corrupt";
80 leveldb::WriteBatch batch;
81 batch.Put(kCorruptKey, "[{(.*+\"\'\\");
82 ASSERT_TRUE(store()->WriteToDbForTest(&batch));
84 // Verify corruption.
85 ValueStore::ReadResult result = store()->Get(kCorruptKey);
86 ASSERT_TRUE(result->HasError());
87 ASSERT_EQ(ValueStore::CORRUPTION, result->error().code);
89 // Restore and verify.
90 ASSERT_TRUE(store()->RestoreKey(kCorruptKey));
91 result = store()->Get(kCorruptKey);
92 EXPECT_FALSE(result->HasError());
93 EXPECT_TRUE(result->settings().empty());
95 // Verify that the valid pair is still present.
96 result = store()->Get(kNotCorruptKey);
97 EXPECT_FALSE(result->HasError());
98 EXPECT_TRUE(result->settings().HasKey(kNotCorruptKey));
99 std::string value_string;
100 EXPECT_TRUE(result->settings().GetString(kNotCorruptKey, &value_string));
101 EXPECT_EQ(kValue, value_string);
104 // Test that the Restore() method does not just delete the entire database
105 // (unless absolutely necessary), and instead only removes corrupted keys.
106 TEST_F(LeveldbValueStoreUnitTest, RestoreDoesMinimumNecessary) {
107 const char* kNotCorruptKeys[] = {"a", "n", "z"};
108 const size_t kNotCorruptKeysSize = 3u;
109 const char kCorruptKey1[] = "f";
110 const char kCorruptKey2[] = "s";
111 const char kValue[] = "value";
112 const char kCorruptValue[] = "[{(.*+\"\'\\";
114 // Insert a collection of non-corrupted pairs.
115 scoped_ptr<base::Value> value(new base::StringValue(kValue));
116 for (size_t i = 0; i < kNotCorruptKeysSize; ++i) {
117 ASSERT_FALSE(store()
118 ->Set(ValueStore::DEFAULTS, kNotCorruptKeys[i], *value)
119 ->HasError());
122 // Insert a few corrupted pairs.
123 leveldb::WriteBatch batch;
124 batch.Put(kCorruptKey1, kCorruptValue);
125 batch.Put(kCorruptKey2, kCorruptValue);
126 ASSERT_TRUE(store()->WriteToDbForTest(&batch));
128 // Verify that we broke it, and then fix it.
129 ValueStore::ReadResult result = store()->Get();
130 ASSERT_TRUE(result->HasError());
131 ASSERT_EQ(ValueStore::CORRUPTION, result->error().code);
133 ASSERT_TRUE(store()->Restore());
135 // We should still have all valid pairs present in the database.
136 std::string value_string;
137 for (size_t i = 0; i < kNotCorruptKeysSize; ++i) {
138 result = store()->Get(kNotCorruptKeys[i]);
139 EXPECT_FALSE(result->HasError());
140 EXPECT_TRUE(result->settings().HasKey(kNotCorruptKeys[i]));
141 EXPECT_TRUE(
142 result->settings().GetString(kNotCorruptKeys[i], &value_string));
143 EXPECT_EQ(kValue, value_string);
147 // Test that the LeveldbValueStore can recover in the case of a CATastrophic
148 // failure and we have total corruption. In this case, the database is plagued
149 // by LolCats.
150 // Full corruption has been known to happen occasionally in strange edge cases,
151 // such as after users use Windows Restore. We can't prevent it, but we need to
152 // be able to handle it smoothly.
153 TEST_F(LeveldbValueStoreUnitTest, RestoreFullDatabase) {
154 const std::string kLolCats("I can haz leveldb filez?");
155 const char* kNotCorruptKeys[] = {"a", "n", "z"};
156 const size_t kNotCorruptKeysSize = 3u;
157 const char kValue[] = "value";
159 // Generate a database.
160 scoped_ptr<base::Value> value(new base::StringValue(kValue));
161 for (size_t i = 0; i < kNotCorruptKeysSize; ++i) {
162 ASSERT_FALSE(store()
163 ->Set(ValueStore::DEFAULTS, kNotCorruptKeys[i], *value)
164 ->HasError());
167 // Close it (so we remove the lock), and replace all files with LolCats.
168 CloseStore();
169 base::FileEnumerator enumerator(
170 database_path(), true /* recursive */, base::FileEnumerator::FILES);
171 for (base::FilePath file = enumerator.Next(); !file.empty();
172 file = enumerator.Next()) {
173 // WriteFile() failure is a result of -1.
174 ASSERT_NE(base::WriteFile(file, kLolCats.c_str(), kLolCats.length()),
175 -1);
177 OpenStore();
179 // We should definitely have an error.
180 ValueStore::ReadResult result = store()->Get();
181 ASSERT_TRUE(result->HasError());
182 ASSERT_EQ(ValueStore::CORRUPTION, result->error().code);
184 ASSERT_TRUE(store()->Restore());
185 result = store()->Get();
186 EXPECT_FALSE(result->HasError());
187 // We couldn't recover anything, but we should be in a sane state again.
188 EXPECT_EQ(0u, result->settings().size());