Chromium Code Reviews| 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_bundle.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)); |
| 31 | |
| 32 class LeveldbValueStoreUnitTest : public testing::Test { | |
| 33 public: | |
| 34 LeveldbValueStoreUnitTest() {} | |
| 35 virtual ~LeveldbValueStoreUnitTest() {} | |
| 36 | |
| 37 protected: | |
| 38 virtual void SetUp() OVERRIDE { | |
| 39 testing::Test::SetUp(); | |
| 40 | |
| 41 CHECK(database_dir_.CreateUniqueTempDir()); | |
|
Matt Perry
2014/02/19 01:34:22
Don't use CHECK in a unittest. It'll crash the who
Devlin
2014/02/19 17:43:19
Done.
| |
| 42 OpenStore(); | |
| 43 CHECK(!store_->Get()->HasError()); | |
| 44 } | |
| 45 | |
| 46 virtual void TearDown() OVERRIDE { | |
| 47 store_->Clear(); | |
| 48 store_.reset(); | |
| 49 } | |
| 50 | |
| 51 void CloseStore() { store_.reset(); } | |
| 52 | |
| 53 void OpenStore() { store_.reset(new LeveldbValueStore(database_path())); } | |
| 54 | |
| 55 LeveldbValueStore* store() { return store_.get(); } | |
| 56 const base::FilePath& database_path() { return database_dir_.path(); } | |
| 57 | |
| 58 private: | |
| 59 scoped_ptr<LeveldbValueStore> store_; | |
| 60 base::ScopedTempDir database_dir_; | |
| 61 | |
| 62 base::MessageLoop message_loop_; | |
| 63 content::TestBrowserThreadBundle thread_bundle_; | |
| 64 }; | |
| 65 | |
| 66 // Check that we can restore a single corrupted key in the LeveldbValueStore. | |
| 67 TEST_F(LeveldbValueStoreUnitTest, RestoreKeyTest) { | |
| 68 const char kNotCorruptKey[] = "not-corrupt"; | |
| 69 const char kValue[] = "value"; | |
| 70 | |
| 71 // Insert a valid pair. | |
| 72 scoped_ptr<base::Value> value(base::Value::CreateStringValue(kValue)); | |
| 73 ASSERT_FALSE( | |
| 74 store()->Set(ValueStore::DEFAULTS, kNotCorruptKey, *value)->HasError()); | |
| 75 | |
| 76 // Insert a corrupt pair. | |
| 77 const char kCorruptKey[] = "corrupt"; | |
| 78 leveldb::WriteBatch batch; | |
| 79 batch.Put(kCorruptKey, "[{(.*+\"\'\\"); | |
| 80 ASSERT_TRUE(store()->WriteToDbForTest(&batch)); | |
| 81 | |
| 82 // Verify corruption. | |
| 83 ValueStore::ReadResult result = store()->Get(kCorruptKey); | |
| 84 ASSERT_TRUE(result->HasError()); | |
| 85 ASSERT_EQ(ValueStore::CORRUPTION, result->error().code); | |
| 86 | |
| 87 // Restore and verify. | |
| 88 ASSERT_TRUE(store()->RestoreKey(kCorruptKey)); | |
| 89 result = store()->Get(kCorruptKey); | |
| 90 EXPECT_FALSE(result->HasError()); | |
| 91 EXPECT_TRUE(result->settings().empty()); | |
| 92 | |
| 93 // Verify that the valid pair is still present. | |
| 94 result = store()->Get(kNotCorruptKey); | |
| 95 EXPECT_FALSE(result->HasError()); | |
| 96 EXPECT_TRUE(result->settings().HasKey(kNotCorruptKey)); | |
| 97 std::string value_string; | |
| 98 EXPECT_TRUE(result->settings().GetString(kNotCorruptKey, &value_string)); | |
| 99 EXPECT_EQ(kValue, value_string); | |
| 100 } | |
| 101 | |
| 102 // Test that the Restore() method does not just delete the entire database | |
| 103 // (unless absolutely necessary), and instead only removes corrupted keys. | |
| 104 TEST_F(LeveldbValueStoreUnitTest, RestoreDoesMinimumNecessary) { | |
| 105 const char* kNotCorruptKeys[] = {"a", "n", "z"}; | |
| 106 const size_t kNotCorruptKeysSize = 3u; | |
| 107 const char kCorruptKey1[] = "f"; | |
| 108 const char kCorruptKey2[] = "s"; | |
| 109 const char kValue[] = "value"; | |
| 110 const char kCorruptValue[] = "[{(.*+\"\'\\"; | |
| 111 | |
| 112 // Insert a collection of non-corrupted pairs. | |
| 113 scoped_ptr<base::Value> value(base::Value::CreateStringValue(kValue)); | |
| 114 for (size_t i = 0; i < kNotCorruptKeysSize; ++i) { | |
| 115 ASSERT_FALSE(store() | |
| 116 ->Set(ValueStore::DEFAULTS, kNotCorruptKeys[i], *value) | |
| 117 ->HasError()); | |
| 118 } | |
| 119 | |
| 120 // Insert a few corrupted pairs. | |
| 121 leveldb::WriteBatch batch; | |
| 122 batch.Put(kCorruptKey1, kCorruptValue); | |
| 123 batch.Put(kCorruptKey2, kCorruptValue); | |
| 124 ASSERT_TRUE(store()->WriteToDbForTest(&batch)); | |
| 125 | |
| 126 // Verify that we broke it, and then fix it. | |
| 127 ValueStore::ReadResult result = store()->Get(); | |
| 128 ASSERT_TRUE(result->HasError()); | |
| 129 ASSERT_EQ(ValueStore::CORRUPTION, result->error().code); | |
| 130 | |
| 131 ASSERT_TRUE(store()->Restore()); | |
| 132 | |
| 133 // We should still have all valid pairs present in the database. | |
| 134 std::string value_string; | |
| 135 for (size_t i = 0; i < kNotCorruptKeysSize; ++i) { | |
| 136 result = store()->Get(kNotCorruptKeys[i]); | |
| 137 EXPECT_FALSE(result->HasError()); | |
| 138 EXPECT_TRUE(result->settings().HasKey(kNotCorruptKeys[i])); | |
| 139 EXPECT_TRUE( | |
| 140 result->settings().GetString(kNotCorruptKeys[i], &value_string)); | |
| 141 EXPECT_EQ(kValue, value_string); | |
| 142 } | |
| 143 } | |
| 144 | |
| 145 // Test that the LeveldbValueStore can recover in the case of a CATastrophic | |
| 146 // failure and we have total corruption. In this case, the database is plagued | |
| 147 // by LolCats. | |
| 148 // Full corruption has been known to happen occasionally in strange edge cases, | |
| 149 // such as after users use Windows Restore. We can't prevent it, but we need to | |
| 150 // be able to handle it smoothly. | |
| 151 TEST_F(LeveldbValueStoreUnitTest, RestoreFullDatabase) { | |
| 152 const std::string kLolCats("I can haz leveldb filez?"); | |
| 153 const char* kNotCorruptKeys[] = {"a", "n", "z"}; | |
| 154 const size_t kNotCorruptKeysSize = 3u; | |
| 155 const char kValue[] = "value"; | |
| 156 | |
| 157 // Generate a database. | |
| 158 scoped_ptr<base::Value> value(base::Value::CreateStringValue(kValue)); | |
| 159 for (size_t i = 0; i < kNotCorruptKeysSize; ++i) { | |
| 160 ASSERT_FALSE(store() | |
| 161 ->Set(ValueStore::DEFAULTS, kNotCorruptKeys[i], *value) | |
| 162 ->HasError()); | |
| 163 } | |
| 164 | |
| 165 // Close it (so we remove the lock), and replace all files with LolCats. | |
| 166 CloseStore(); | |
| 167 base::FileEnumerator enumerator( | |
| 168 database_path(), true /* recursive */, base::FileEnumerator::FILES); | |
| 169 for (base::FilePath file = enumerator.Next(); !file.empty(); | |
| 170 file = enumerator.Next()) { | |
| 171 // WriteFile() failure is a result of -1. | |
| 172 ASSERT_NE(file_util::WriteFile(file, kLolCats.c_str(), kLolCats.length()), | |
| 173 -1); | |
| 174 } | |
| 175 OpenStore(); | |
| 176 | |
| 177 // We should definitely have an error. | |
| 178 ValueStore::ReadResult result = store()->Get(); | |
| 179 ASSERT_TRUE(result->HasError()); | |
| 180 ASSERT_EQ(ValueStore::CORRUPTION, result->error().code); | |
| 181 | |
| 182 ASSERT_TRUE(store()->Restore()); | |
| 183 result = store()->Get(); | |
| 184 EXPECT_FALSE(result->HasError()); | |
| 185 // We couldn't recover anything, but we should be in a sane state again. | |
| 186 EXPECT_EQ(0u, result->settings().size()); | |
| 187 } | |
| OLD | NEW |