| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/leveldb_scoped_database.h" | 5 #include "extensions/browser/value_store/leveldb_scoped_database.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <map> | 9 #include <map> |
| 10 #include <string> | 10 #include <string> |
| (...skipping 23 matching lines...) Expand all Loading... |
| 34 void TearDown() override { | 34 void TearDown() override { |
| 35 db_ = nullptr; | 35 db_ = nullptr; |
| 36 base::DeleteFile(database_dir_.path(), true); | 36 base::DeleteFile(database_dir_.path(), true); |
| 37 } | 37 } |
| 38 | 38 |
| 39 ValueStore::Status ReadAllValues( | 39 ValueStore::Status ReadAllValues( |
| 40 std::map<std::string, std::string>* values) const { | 40 std::map<std::string, std::string>* values) const { |
| 41 values->clear(); | 41 values->clear(); |
| 42 leveldb::ReadOptions read_options; | 42 leveldb::ReadOptions read_options; |
| 43 read_options.verify_checksums = true; | 43 read_options.verify_checksums = true; |
| 44 scoped_ptr<leveldb::Iterator> iterator; | 44 std::unique_ptr<leveldb::Iterator> iterator; |
| 45 ValueStore::Status status = db_->CreateIterator(read_options, &iterator); | 45 ValueStore::Status status = db_->CreateIterator(read_options, &iterator); |
| 46 if (!status.ok()) | 46 if (!status.ok()) |
| 47 return status; | 47 return status; |
| 48 iterator->SeekToFirst(); | 48 iterator->SeekToFirst(); |
| 49 while (iterator->Valid()) { | 49 while (iterator->Valid()) { |
| 50 // The LeveldbProfileDatabase writes all values as JSON strings. | 50 // The LeveldbProfileDatabase writes all values as JSON strings. |
| 51 // This method returns the encoded strings. | 51 // This method returns the encoded strings. |
| 52 (*values)[iterator->key().ToString()] = iterator->value().ToString(); | 52 (*values)[iterator->key().ToString()] = iterator->value().ToString(); |
| 53 iterator->Next(); | 53 iterator->Next(); |
| 54 } | 54 } |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 152 base::DictionaryValue read_s2_vals; | 152 base::DictionaryValue read_s2_vals; |
| 153 EXPECT_TRUE(db_->Read("scope2", &read_s2_vals).ok()); | 153 EXPECT_TRUE(db_->Read("scope2", &read_s2_vals).ok()); |
| 154 EXPECT_TRUE(scope2_values.Equals(&read_s2_vals)); | 154 EXPECT_TRUE(scope2_values.Equals(&read_s2_vals)); |
| 155 } | 155 } |
| 156 | 156 |
| 157 TEST_F(LeveldbScopedDatabaseUnitTest, TestEmptyValue) { | 157 TEST_F(LeveldbScopedDatabaseUnitTest, TestEmptyValue) { |
| 158 base::DictionaryValue values; | 158 base::DictionaryValue values; |
| 159 values.SetString("s1_key1", ""); | 159 values.SetString("s1_key1", ""); |
| 160 EXPECT_TRUE(db_->Write("scope1", values).ok()); | 160 EXPECT_TRUE(db_->Write("scope1", values).ok()); |
| 161 | 161 |
| 162 scoped_ptr<base::Value> value; | 162 std::unique_ptr<base::Value> value; |
| 163 ASSERT_TRUE(db_->Read("scope1", "s1_key1", &value).ok()); | 163 ASSERT_TRUE(db_->Read("scope1", "s1_key1", &value).ok()); |
| 164 std::string str; | 164 std::string str; |
| 165 EXPECT_TRUE(value->GetAsString(&str)); | 165 EXPECT_TRUE(value->GetAsString(&str)); |
| 166 EXPECT_EQ(str, ""); | 166 EXPECT_EQ(str, ""); |
| 167 } | 167 } |
| 168 | 168 |
| 169 TEST_F(LeveldbScopedDatabaseUnitTest, TestValueContainingDelimiter) { | 169 TEST_F(LeveldbScopedDatabaseUnitTest, TestValueContainingDelimiter) { |
| 170 base::DictionaryValue values; | 170 base::DictionaryValue values; |
| 171 values.SetString("s1_key1", "with:delimiter"); | 171 values.SetString("s1_key1", "with:delimiter"); |
| 172 EXPECT_TRUE(db_->Write("scope1", values).ok()); | 172 EXPECT_TRUE(db_->Write("scope1", values).ok()); |
| 173 | 173 |
| 174 scoped_ptr<base::Value> value; | 174 std::unique_ptr<base::Value> value; |
| 175 ASSERT_TRUE(db_->Read("scope1", "s1_key1", &value).ok()); | 175 ASSERT_TRUE(db_->Read("scope1", "s1_key1", &value).ok()); |
| 176 std::string str; | 176 std::string str; |
| 177 EXPECT_TRUE(value->GetAsString(&str)); | 177 EXPECT_TRUE(value->GetAsString(&str)); |
| 178 EXPECT_EQ(str, "with:delimiter"); | 178 EXPECT_EQ(str, "with:delimiter"); |
| 179 } | 179 } |
| 180 | 180 |
| 181 TEST_F(LeveldbScopedDatabaseUnitTest, TestDeleteValues) { | 181 TEST_F(LeveldbScopedDatabaseUnitTest, TestDeleteValues) { |
| 182 base::DictionaryValue scope1_values; | 182 base::DictionaryValue scope1_values; |
| 183 scope1_values.SetString("s1_key1", "s1_value1"); | 183 scope1_values.SetString("s1_key1", "s1_value1"); |
| 184 scope1_values.SetString("s1_key2", "s1_value2"); | 184 scope1_values.SetString("s1_key2", "s1_value2"); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 196 EXPECT_TRUE(db_->DeleteValues("scope2", keys).ok()); | 196 EXPECT_TRUE(db_->DeleteValues("scope2", keys).ok()); |
| 197 | 197 |
| 198 base::DictionaryValue read_s1_vals; | 198 base::DictionaryValue read_s1_vals; |
| 199 EXPECT_TRUE(db_->Read("scope1", &read_s1_vals).ok()); | 199 EXPECT_TRUE(db_->Read("scope1", &read_s1_vals).ok()); |
| 200 EXPECT_TRUE(scope1_values.Equals(&read_s1_vals)); | 200 EXPECT_TRUE(scope1_values.Equals(&read_s1_vals)); |
| 201 | 201 |
| 202 base::DictionaryValue read_s2_vals; | 202 base::DictionaryValue read_s2_vals; |
| 203 EXPECT_TRUE(db_->Read("scope2", &read_s2_vals).ok()); | 203 EXPECT_TRUE(db_->Read("scope2", &read_s2_vals).ok()); |
| 204 EXPECT_TRUE(read_s2_vals.empty()); | 204 EXPECT_TRUE(read_s2_vals.empty()); |
| 205 } | 205 } |
| OLD | NEW |