OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/value_store/value_store_unittest.h" | 5 #include "chrome/browser/value_store/value_store_unittest.h" |
6 | 6 |
7 #include "base/file_util.h" | |
8 #include "base/files/file_enumerator.h" | |
9 #include "base/files/scoped_temp_dir.h" | |
7 #include "base/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
11 #include "base/message_loop/message_loop.h" | |
12 #include "base/values.h" | |
8 #include "chrome/browser/value_store/leveldb_value_store.h" | 13 #include "chrome/browser/value_store/leveldb_value_store.h" |
14 #include "content/public/test/test_browser_thread.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" | |
9 | 18 |
10 namespace { | 19 namespace { |
11 | 20 |
12 ValueStore* Param(const base::FilePath& file_path) { | 21 ValueStore* Param(const base::FilePath& file_path) { |
13 return new LeveldbValueStore(file_path); | 22 return new LeveldbValueStore(file_path); |
14 } | 23 } |
15 | 24 |
16 } // namespace | 25 } // namespace |
17 | 26 |
18 INSTANTIATE_TEST_CASE_P( | 27 INSTANTIATE_TEST_CASE_P( |
19 LeveldbValueStore, | 28 LeveldbValueStore, |
20 ValueStoreTest, | 29 ValueStoreTest, |
21 testing::Values(&Param)); | 30 testing::Values(&Param)); |
not at google - send to devlin
2014/02/14 19:35:56
is there a test we can add to settings frontend to
Devlin
2014/02/18 23:55:22
Wow. That was more work than I expected. But I a
| |
31 | |
32 class LeveldbValueStoreUnitTest : public testing::Test { | |
33 public: | |
34 LeveldbValueStoreUnitTest() | |
35 : ui_thread_(content::BrowserThread::UI, base::MessageLoop::current()), | |
36 file_thread_(content::BrowserThread::FILE, | |
37 base::MessageLoop::current()) {} | |
38 virtual ~LeveldbValueStoreUnitTest() {} | |
39 | |
40 protected: | |
41 virtual void SetUp() OVERRIDE { | |
42 testing::Test::SetUp(); | |
not at google - send to devlin
2014/02/14 19:35:56
I don't think you're expected to do this.
Devlin
2014/02/18 23:55:22
We definitely need to for tests which inherit from
not at google - send to devlin
2014/02/19 21:25:29
Yes for all cases other than testing::Test you wou
Devlin
2014/02/19 23:12:28
Okay, done.
| |
43 | |
44 CHECK(database_dir_.CreateUniqueTempDir()); | |
45 OpenStore(); | |
46 CHECK(!store_->Get()->HasError()); | |
47 } | |
48 | |
49 virtual void TearDown() OVERRIDE { | |
50 store_->Clear(); | |
51 store_.reset(); | |
52 } | |
53 | |
54 void CloseStore() { store_.reset(); } | |
55 | |
56 void OpenStore() { store_.reset(new LeveldbValueStore(database_path())); } | |
not at google - send to devlin
2014/02/14 19:35:56
these aren't accessors so make them proper functio
Devlin
2014/02/18 23:55:22
Don't we usually do this on tests? Most I see (at
not at google - send to devlin
2014/02/19 21:25:29
Yes I was referring to the formatting :)
| |
57 | |
58 LeveldbValueStore* store() { return store_.get(); } | |
59 const base::FilePath& database_path() { return database_dir_.path(); } | |
60 | |
61 private: | |
62 scoped_ptr<LeveldbValueStore> store_; | |
63 base::ScopedTempDir database_dir_; | |
64 | |
65 // Need these so that the DCHECKs for running on FILE or UI threads pass. | |
not at google - send to devlin
2014/02/14 19:35:56
comment not necessary
Devlin
2014/02/18 23:55:22
Done.
| |
66 base::MessageLoop message_loop_; | |
67 content::TestBrowserThread ui_thread_; | |
68 content::TestBrowserThread file_thread_; | |
not at google - send to devlin
2014/02/14 19:35:56
can you just use a single content::TestBrowserThre
Devlin
2014/02/18 23:55:22
Done.
| |
69 }; | |
70 | |
71 // Check that we can restore a single corrupted key in the LeveldbValueStore. | |
72 TEST_F(LeveldbValueStoreUnitTest, RestoreKeyTest) { | |
73 const char kNotCorruptKey[] = "not-corrupt"; | |
74 const char kValue[] = "value"; | |
75 | |
76 // Insert a valid pair. | |
77 scoped_ptr<base::Value> value(base::Value::CreateStringValue(kValue)); | |
78 ASSERT_FALSE( | |
79 store()->Set(ValueStore::DEFAULTS, kNotCorruptKey, *value)->HasError()); | |
80 | |
81 // Insert a corrupt pair. | |
82 const char kCorruptKey[] = "corrupt"; | |
83 leveldb::WriteBatch batch; | |
84 batch.Put(kCorruptKey, "[{(.*+\"\'\\"); | |
85 ASSERT_TRUE(store()->WriteToDbForTest(&batch)); | |
86 | |
87 // Verify corruption. | |
88 ValueStore::ReadResult result = store()->Get(kCorruptKey); | |
89 ASSERT_TRUE(result->HasError()); | |
90 ASSERT_EQ(ValueStore::CORRUPTION, result->error().code); | |
91 | |
92 // Restore and verify. | |
93 ASSERT_TRUE(store()->RestoreKey(kCorruptKey)); | |
94 result = store()->Get(kCorruptKey); | |
95 EXPECT_FALSE(result->HasError()); | |
96 EXPECT_TRUE(result->settings().empty()); | |
97 | |
98 // Verify that the valid pair is still present. | |
99 result = store()->Get(kNotCorruptKey); | |
100 EXPECT_FALSE(result->HasError()); | |
101 EXPECT_TRUE(result->settings().HasKey(kNotCorruptKey)); | |
102 std::string value_string; | |
103 EXPECT_TRUE(result->settings().GetString(kNotCorruptKey, &value_string)); | |
104 EXPECT_EQ(kValue, value_string); | |
105 } | |
106 | |
107 // Test that the Restore() method does not just delete the entire database | |
108 // (unless absolutely necessary), and instead only removes corrupted keys. | |
109 TEST_F(LeveldbValueStoreUnitTest, RestoreDoesMinimumNecessary) { | |
110 const char* kNotCorruptKeys[] = {"a", "n", "z"}; | |
111 const size_t kNotCorruptKeysSize = 3u; | |
112 const char kCorruptKey1[] = "f"; | |
113 const char kCorruptKey2[] = "s"; | |
114 const char kValue[] = "value"; | |
115 const char kCorruptValue[] = "[{(.*+\"\'\\"; | |
116 | |
117 // Insert a collection of non-corrupted pairs. | |
118 scoped_ptr<base::Value> value(base::Value::CreateStringValue(kValue)); | |
119 for (size_t i = 0; i < kNotCorruptKeysSize; ++i) { | |
120 ASSERT_FALSE(store() | |
121 ->Set(ValueStore::DEFAULTS, kNotCorruptKeys[i], *value) | |
122 ->HasError()); | |
123 } | |
124 | |
125 // Insert a few corrupted pairs. | |
126 leveldb::WriteBatch batch; | |
127 batch.Put(kCorruptKey1, kCorruptValue); | |
128 batch.Put(kCorruptKey2, kCorruptValue); | |
129 ASSERT_TRUE(store()->WriteToDbForTest(&batch)); | |
130 | |
131 // Verify that we broke it, and then fix it. | |
132 ValueStore::ReadResult result = store()->Get(); | |
133 ASSERT_TRUE(result->HasError()); | |
134 ASSERT_EQ(ValueStore::CORRUPTION, result->error().code); | |
135 | |
136 ASSERT_TRUE(store()->Restore()); | |
137 | |
138 // We should still have all valid pairs present in the database. | |
139 std::string value_string; | |
140 for (size_t i = 0; i < kNotCorruptKeysSize; ++i) { | |
141 result = store()->Get(kNotCorruptKeys[i]); | |
142 EXPECT_FALSE(result->HasError()); | |
143 EXPECT_TRUE(result->settings().HasKey(kNotCorruptKeys[i])); | |
144 EXPECT_TRUE( | |
145 result->settings().GetString(kNotCorruptKeys[i], &value_string)); | |
146 EXPECT_EQ(kValue, value_string); | |
147 } | |
148 } | |
149 | |
150 // Test that the LeveldbValueStore can recover in the case of a CATastrophic | |
151 // failure and we have total corruption. In this case, the database is plagued | |
152 // by LolCats. | |
153 // Full corruption has been known to happen occasionally in strange edge cases, | |
154 // such as after users use Windows Restore. We can't prevent it, but we need to | |
155 // be able to handle it smoothly. | |
156 TEST_F(LeveldbValueStoreUnitTest, RestoreFullDatabase) { | |
157 const std::string kLolCats("I can haz leveldb filez?"); | |
158 const char* kNotCorruptKeys[] = {"a", "n", "z"}; | |
159 const size_t kNotCorruptKeysSize = 3u; | |
160 const char kValue[] = "value"; | |
161 | |
162 // Generate a database. | |
163 scoped_ptr<base::Value> value(base::Value::CreateStringValue(kValue)); | |
164 for (size_t i = 0; i < kNotCorruptKeysSize; ++i) { | |
165 ASSERT_FALSE(store() | |
166 ->Set(ValueStore::DEFAULTS, kNotCorruptKeys[i], *value) | |
167 ->HasError()); | |
168 } | |
169 | |
170 // Close it (so we remove the lock), and replace all files with LolCats. | |
171 CloseStore(); | |
172 base::FileEnumerator enumerator( | |
173 database_path(), true /* recursive */, base::FileEnumerator::FILES); | |
174 for (base::FilePath file = enumerator.Next(); !file.empty(); | |
175 file = enumerator.Next()) { | |
176 // WriteFile() failure is a result of -1. | |
177 ASSERT_NE(file_util::WriteFile(file, kLolCats.c_str(), kLolCats.length()), | |
178 -1); | |
179 } | |
180 OpenStore(); | |
181 | |
182 // We should definitely have an error. | |
183 ValueStore::ReadResult result = store()->Get(); | |
184 ASSERT_TRUE(result->HasError()); | |
185 ASSERT_EQ(ValueStore::CORRUPTION, result->error().code); | |
186 | |
187 ASSERT_TRUE(store()->Restore()); | |
188 result = store()->Get(); | |
189 EXPECT_FALSE(result->HasError()); | |
190 // We couldn't recover anything, but we should be in a sane state again. | |
191 EXPECT_EQ(0u, result->settings().size()); | |
192 } | |
OLD | NEW |