| 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 "base/file_util.h" | 7 #include "base/file_util.h" |
| 8 #include "base/files/file_enumerator.h" | 8 #include "base/files/file_enumerator.h" |
| 9 #include "base/files/scoped_temp_dir.h" | 9 #include "base/files/scoped_temp_dir.h" |
| 10 #include "base/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 59 | 59 |
| 60 content::TestBrowserThreadBundle thread_bundle_; | 60 content::TestBrowserThreadBundle thread_bundle_; |
| 61 }; | 61 }; |
| 62 | 62 |
| 63 // Check that we can restore a single corrupted key in the LeveldbValueStore. | 63 // Check that we can restore a single corrupted key in the LeveldbValueStore. |
| 64 TEST_F(LeveldbValueStoreUnitTest, RestoreKeyTest) { | 64 TEST_F(LeveldbValueStoreUnitTest, RestoreKeyTest) { |
| 65 const char kNotCorruptKey[] = "not-corrupt"; | 65 const char kNotCorruptKey[] = "not-corrupt"; |
| 66 const char kValue[] = "value"; | 66 const char kValue[] = "value"; |
| 67 | 67 |
| 68 // Insert a valid pair. | 68 // Insert a valid pair. |
| 69 scoped_ptr<base::Value> value(base::Value::CreateStringValue(kValue)); | 69 scoped_ptr<base::Value> value(new base::StringValue(kValue)); |
| 70 ASSERT_FALSE( | 70 ASSERT_FALSE( |
| 71 store()->Set(ValueStore::DEFAULTS, kNotCorruptKey, *value)->HasError()); | 71 store()->Set(ValueStore::DEFAULTS, kNotCorruptKey, *value)->HasError()); |
| 72 | 72 |
| 73 // Insert a corrupt pair. | 73 // Insert a corrupt pair. |
| 74 const char kCorruptKey[] = "corrupt"; | 74 const char kCorruptKey[] = "corrupt"; |
| 75 leveldb::WriteBatch batch; | 75 leveldb::WriteBatch batch; |
| 76 batch.Put(kCorruptKey, "[{(.*+\"\'\\"); | 76 batch.Put(kCorruptKey, "[{(.*+\"\'\\"); |
| 77 ASSERT_TRUE(store()->WriteToDbForTest(&batch)); | 77 ASSERT_TRUE(store()->WriteToDbForTest(&batch)); |
| 78 | 78 |
| 79 // Verify corruption. | 79 // Verify corruption. |
| (...skipping 20 matching lines...) Expand all Loading... |
| 100 // (unless absolutely necessary), and instead only removes corrupted keys. | 100 // (unless absolutely necessary), and instead only removes corrupted keys. |
| 101 TEST_F(LeveldbValueStoreUnitTest, RestoreDoesMinimumNecessary) { | 101 TEST_F(LeveldbValueStoreUnitTest, RestoreDoesMinimumNecessary) { |
| 102 const char* kNotCorruptKeys[] = {"a", "n", "z"}; | 102 const char* kNotCorruptKeys[] = {"a", "n", "z"}; |
| 103 const size_t kNotCorruptKeysSize = 3u; | 103 const size_t kNotCorruptKeysSize = 3u; |
| 104 const char kCorruptKey1[] = "f"; | 104 const char kCorruptKey1[] = "f"; |
| 105 const char kCorruptKey2[] = "s"; | 105 const char kCorruptKey2[] = "s"; |
| 106 const char kValue[] = "value"; | 106 const char kValue[] = "value"; |
| 107 const char kCorruptValue[] = "[{(.*+\"\'\\"; | 107 const char kCorruptValue[] = "[{(.*+\"\'\\"; |
| 108 | 108 |
| 109 // Insert a collection of non-corrupted pairs. | 109 // Insert a collection of non-corrupted pairs. |
| 110 scoped_ptr<base::Value> value(base::Value::CreateStringValue(kValue)); | 110 scoped_ptr<base::Value> value(new base::StringValue(kValue)); |
| 111 for (size_t i = 0; i < kNotCorruptKeysSize; ++i) { | 111 for (size_t i = 0; i < kNotCorruptKeysSize; ++i) { |
| 112 ASSERT_FALSE(store() | 112 ASSERT_FALSE(store() |
| 113 ->Set(ValueStore::DEFAULTS, kNotCorruptKeys[i], *value) | 113 ->Set(ValueStore::DEFAULTS, kNotCorruptKeys[i], *value) |
| 114 ->HasError()); | 114 ->HasError()); |
| 115 } | 115 } |
| 116 | 116 |
| 117 // Insert a few corrupted pairs. | 117 // Insert a few corrupted pairs. |
| 118 leveldb::WriteBatch batch; | 118 leveldb::WriteBatch batch; |
| 119 batch.Put(kCorruptKey1, kCorruptValue); | 119 batch.Put(kCorruptKey1, kCorruptValue); |
| 120 batch.Put(kCorruptKey2, kCorruptValue); | 120 batch.Put(kCorruptKey2, kCorruptValue); |
| (...skipping 24 matching lines...) Expand all Loading... |
| 145 // Full corruption has been known to happen occasionally in strange edge cases, | 145 // Full corruption has been known to happen occasionally in strange edge cases, |
| 146 // such as after users use Windows Restore. We can't prevent it, but we need to | 146 // such as after users use Windows Restore. We can't prevent it, but we need to |
| 147 // be able to handle it smoothly. | 147 // be able to handle it smoothly. |
| 148 TEST_F(LeveldbValueStoreUnitTest, RestoreFullDatabase) { | 148 TEST_F(LeveldbValueStoreUnitTest, RestoreFullDatabase) { |
| 149 const std::string kLolCats("I can haz leveldb filez?"); | 149 const std::string kLolCats("I can haz leveldb filez?"); |
| 150 const char* kNotCorruptKeys[] = {"a", "n", "z"}; | 150 const char* kNotCorruptKeys[] = {"a", "n", "z"}; |
| 151 const size_t kNotCorruptKeysSize = 3u; | 151 const size_t kNotCorruptKeysSize = 3u; |
| 152 const char kValue[] = "value"; | 152 const char kValue[] = "value"; |
| 153 | 153 |
| 154 // Generate a database. | 154 // Generate a database. |
| 155 scoped_ptr<base::Value> value(base::Value::CreateStringValue(kValue)); | 155 scoped_ptr<base::Value> value(new base::StringValue(kValue)); |
| 156 for (size_t i = 0; i < kNotCorruptKeysSize; ++i) { | 156 for (size_t i = 0; i < kNotCorruptKeysSize; ++i) { |
| 157 ASSERT_FALSE(store() | 157 ASSERT_FALSE(store() |
| 158 ->Set(ValueStore::DEFAULTS, kNotCorruptKeys[i], *value) | 158 ->Set(ValueStore::DEFAULTS, kNotCorruptKeys[i], *value) |
| 159 ->HasError()); | 159 ->HasError()); |
| 160 } | 160 } |
| 161 | 161 |
| 162 // Close it (so we remove the lock), and replace all files with LolCats. | 162 // Close it (so we remove the lock), and replace all files with LolCats. |
| 163 CloseStore(); | 163 CloseStore(); |
| 164 base::FileEnumerator enumerator( | 164 base::FileEnumerator enumerator( |
| 165 database_path(), true /* recursive */, base::FileEnumerator::FILES); | 165 database_path(), true /* recursive */, base::FileEnumerator::FILES); |
| 166 for (base::FilePath file = enumerator.Next(); !file.empty(); | 166 for (base::FilePath file = enumerator.Next(); !file.empty(); |
| 167 file = enumerator.Next()) { | 167 file = enumerator.Next()) { |
| 168 // WriteFile() failure is a result of -1. | 168 // WriteFile() failure is a result of -1. |
| 169 ASSERT_NE(base::WriteFile(file, kLolCats.c_str(), kLolCats.length()), | 169 ASSERT_NE(base::WriteFile(file, kLolCats.c_str(), kLolCats.length()), |
| 170 -1); | 170 -1); |
| 171 } | 171 } |
| 172 OpenStore(); | 172 OpenStore(); |
| 173 | 173 |
| 174 // We should definitely have an error. | 174 // We should definitely have an error. |
| 175 ValueStore::ReadResult result = store()->Get(); | 175 ValueStore::ReadResult result = store()->Get(); |
| 176 ASSERT_TRUE(result->HasError()); | 176 ASSERT_TRUE(result->HasError()); |
| 177 ASSERT_EQ(ValueStore::CORRUPTION, result->error().code); | 177 ASSERT_EQ(ValueStore::CORRUPTION, result->error().code); |
| 178 | 178 |
| 179 ASSERT_TRUE(store()->Restore()); | 179 ASSERT_TRUE(store()->Restore()); |
| 180 result = store()->Get(); | 180 result = store()->Get(); |
| 181 EXPECT_FALSE(result->HasError()); | 181 EXPECT_FALSE(result->HasError()); |
| 182 // We couldn't recover anything, but we should be in a sane state again. | 182 // We couldn't recover anything, but we should be in a sane state again. |
| 183 EXPECT_EQ(0u, result->settings().size()); | 183 EXPECT_EQ(0u, result->settings().size()); |
| 184 } | 184 } |
| OLD | NEW |