| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 "extensions/browser/value_store/value_store_unittest.h" | 5 #include "extensions/browser/value_store/value_store_unittest.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include "base/files/file_enumerator.h" | 9 #include "base/files/file_enumerator.h" |
| 10 #include "base/files/file_util.h" | 10 #include "base/files/file_util.h" |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 56 | 56 |
| 57 void CreateStore() { | 57 void CreateStore() { |
| 58 store_.reset( | 58 store_.reset( |
| 59 new LeveldbValueStore(kDatabaseUMAClientName, database_path())); | 59 new LeveldbValueStore(kDatabaseUMAClientName, database_path())); |
| 60 } | 60 } |
| 61 | 61 |
| 62 LeveldbValueStore* store() { return store_.get(); } | 62 LeveldbValueStore* store() { return store_.get(); } |
| 63 const base::FilePath& database_path() { return database_dir_.path(); } | 63 const base::FilePath& database_path() { return database_dir_.path(); } |
| 64 | 64 |
| 65 private: | 65 private: |
| 66 scoped_ptr<LeveldbValueStore> store_; | 66 std::unique_ptr<LeveldbValueStore> store_; |
| 67 base::ScopedTempDir database_dir_; | 67 base::ScopedTempDir database_dir_; |
| 68 | 68 |
| 69 content::TestBrowserThreadBundle thread_bundle_; | 69 content::TestBrowserThreadBundle thread_bundle_; |
| 70 }; | 70 }; |
| 71 | 71 |
| 72 // Check that we can restore a single corrupted key in the LeveldbValueStore. | 72 // Check that we can restore a single corrupted key in the LeveldbValueStore. |
| 73 TEST_F(LeveldbValueStoreUnitTest, RestoreKeyTest) { | 73 TEST_F(LeveldbValueStoreUnitTest, RestoreKeyTest) { |
| 74 const char kNotCorruptKey[] = "not-corrupt"; | 74 const char kNotCorruptKey[] = "not-corrupt"; |
| 75 const char kValue[] = "value"; | 75 const char kValue[] = "value"; |
| 76 | 76 |
| 77 // Insert a valid pair. | 77 // Insert a valid pair. |
| 78 scoped_ptr<base::Value> value(new base::StringValue(kValue)); | 78 std::unique_ptr<base::Value> value(new base::StringValue(kValue)); |
| 79 ASSERT_TRUE(store() | 79 ASSERT_TRUE(store() |
| 80 ->Set(ValueStore::DEFAULTS, kNotCorruptKey, *value) | 80 ->Set(ValueStore::DEFAULTS, kNotCorruptKey, *value) |
| 81 ->status().ok()); | 81 ->status().ok()); |
| 82 | 82 |
| 83 // Insert a corrupt pair. | 83 // Insert a corrupt pair. |
| 84 const char kCorruptKey[] = "corrupt"; | 84 const char kCorruptKey[] = "corrupt"; |
| 85 leveldb::WriteBatch batch; | 85 leveldb::WriteBatch batch; |
| 86 batch.Put(kCorruptKey, "[{(.*+\"\'\\"); | 86 batch.Put(kCorruptKey, "[{(.*+\"\'\\"); |
| 87 ASSERT_TRUE(store()->WriteToDbForTest(&batch)); | 87 ASSERT_TRUE(store()->WriteToDbForTest(&batch)); |
| 88 | 88 |
| (...skipping 21 matching lines...) Expand all Loading... |
| 110 // (unless absolutely necessary), and instead only removes corrupted keys. | 110 // (unless absolutely necessary), and instead only removes corrupted keys. |
| 111 TEST_F(LeveldbValueStoreUnitTest, RestoreDoesMinimumNecessary) { | 111 TEST_F(LeveldbValueStoreUnitTest, RestoreDoesMinimumNecessary) { |
| 112 const char* kNotCorruptKeys[] = {"a", "n", "z"}; | 112 const char* kNotCorruptKeys[] = {"a", "n", "z"}; |
| 113 const size_t kNotCorruptKeysSize = 3u; | 113 const size_t kNotCorruptKeysSize = 3u; |
| 114 const char kCorruptKey1[] = "f"; | 114 const char kCorruptKey1[] = "f"; |
| 115 const char kCorruptKey2[] = "s"; | 115 const char kCorruptKey2[] = "s"; |
| 116 const char kValue[] = "value"; | 116 const char kValue[] = "value"; |
| 117 const char kCorruptValue[] = "[{(.*+\"\'\\"; | 117 const char kCorruptValue[] = "[{(.*+\"\'\\"; |
| 118 | 118 |
| 119 // Insert a collection of non-corrupted pairs. | 119 // Insert a collection of non-corrupted pairs. |
| 120 scoped_ptr<base::Value> value(new base::StringValue(kValue)); | 120 std::unique_ptr<base::Value> value(new base::StringValue(kValue)); |
| 121 for (size_t i = 0; i < kNotCorruptKeysSize; ++i) { | 121 for (size_t i = 0; i < kNotCorruptKeysSize; ++i) { |
| 122 ASSERT_TRUE(store() | 122 ASSERT_TRUE(store() |
| 123 ->Set(ValueStore::DEFAULTS, kNotCorruptKeys[i], *value) | 123 ->Set(ValueStore::DEFAULTS, kNotCorruptKeys[i], *value) |
| 124 ->status().ok()); | 124 ->status().ok()); |
| 125 } | 125 } |
| 126 | 126 |
| 127 // Insert a few corrupted pairs. | 127 // Insert a few corrupted pairs. |
| 128 leveldb::WriteBatch batch; | 128 leveldb::WriteBatch batch; |
| 129 batch.Put(kCorruptKey1, kCorruptValue); | 129 batch.Put(kCorruptKey1, kCorruptValue); |
| 130 batch.Put(kCorruptKey2, kCorruptValue); | 130 batch.Put(kCorruptKey2, kCorruptValue); |
| (...skipping 25 matching lines...) Expand all Loading... |
| 156 // Full corruption has been known to happen occasionally in strange edge cases, | 156 // Full corruption has been known to happen occasionally in strange edge cases, |
| 157 // such as after users use Windows Restore. We can't prevent it, but we need to | 157 // such as after users use Windows Restore. We can't prevent it, but we need to |
| 158 // be able to handle it smoothly. | 158 // be able to handle it smoothly. |
| 159 TEST_F(LeveldbValueStoreUnitTest, RestoreFullDatabase) { | 159 TEST_F(LeveldbValueStoreUnitTest, RestoreFullDatabase) { |
| 160 const std::string kLolCats("I can haz leveldb filez?"); | 160 const std::string kLolCats("I can haz leveldb filez?"); |
| 161 const char* kNotCorruptKeys[] = {"a", "n", "z"}; | 161 const char* kNotCorruptKeys[] = {"a", "n", "z"}; |
| 162 const size_t kNotCorruptKeysSize = 3u; | 162 const size_t kNotCorruptKeysSize = 3u; |
| 163 const char kValue[] = "value"; | 163 const char kValue[] = "value"; |
| 164 | 164 |
| 165 // Generate a database. | 165 // Generate a database. |
| 166 scoped_ptr<base::Value> value(new base::StringValue(kValue)); | 166 std::unique_ptr<base::Value> value(new base::StringValue(kValue)); |
| 167 for (size_t i = 0; i < kNotCorruptKeysSize; ++i) { | 167 for (size_t i = 0; i < kNotCorruptKeysSize; ++i) { |
| 168 ASSERT_TRUE(store() | 168 ASSERT_TRUE(store() |
| 169 ->Set(ValueStore::DEFAULTS, kNotCorruptKeys[i], *value) | 169 ->Set(ValueStore::DEFAULTS, kNotCorruptKeys[i], *value) |
| 170 ->status().ok()); | 170 ->status().ok()); |
| 171 } | 171 } |
| 172 | 172 |
| 173 // Close it (so we remove the lock), and replace all files with LolCats. | 173 // Close it (so we remove the lock), and replace all files with LolCats. |
| 174 CloseStore(); | 174 CloseStore(); |
| 175 base::FileEnumerator enumerator( | 175 base::FileEnumerator enumerator( |
| 176 database_path(), true /* recursive */, base::FileEnumerator::FILES); | 176 database_path(), true /* recursive */, base::FileEnumerator::FILES); |
| 177 for (base::FilePath file = enumerator.Next(); !file.empty(); | 177 for (base::FilePath file = enumerator.Next(); !file.empty(); |
| 178 file = enumerator.Next()) { | 178 file = enumerator.Next()) { |
| 179 // WriteFile() failure is a result of -1. | 179 // WriteFile() failure is a result of -1. |
| 180 ASSERT_NE(base::WriteFile(file, kLolCats.c_str(), kLolCats.length()), -1); | 180 ASSERT_NE(base::WriteFile(file, kLolCats.c_str(), kLolCats.length()), -1); |
| 181 } | 181 } |
| 182 CreateStore(); | 182 CreateStore(); |
| 183 | 183 |
| 184 // We couldn't recover anything, but we should be in a sane state again. | 184 // We couldn't recover anything, but we should be in a sane state again. |
| 185 ValueStore::ReadResult result = store()->Get(); | 185 ValueStore::ReadResult result = store()->Get(); |
| 186 ASSERT_EQ(ValueStore::DB_RESTORE_REPAIR_SUCCESS, | 186 ASSERT_EQ(ValueStore::DB_RESTORE_REPAIR_SUCCESS, |
| 187 result->status().restore_status); | 187 result->status().restore_status); |
| 188 EXPECT_TRUE(result->status().ok()); | 188 EXPECT_TRUE(result->status().ok()); |
| 189 EXPECT_EQ(0u, result->settings().size()); | 189 EXPECT_EQ(0u, result->settings().size()); |
| 190 } | 190 } |
| OLD | NEW |