OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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 <algorithm> | 5 #include <algorithm> |
6 #include <cstring> | 6 #include <cstring> |
7 #include <vector> | 7 #include <string> |
8 | 8 |
9 #include "base/files/file_path.h" | 9 #include "base/files/file_path.h" |
10 #include "base/files/scoped_temp_dir.h" | 10 #include "base/files/scoped_temp_dir.h" |
11 #include "base/platform_file.h" | 11 #include "base/platform_file.h" |
12 #include "base/strings/string16.h" | 12 #include "base/strings/string16.h" |
| 13 #include "base/strings/string_piece.h" |
13 #include "content/browser/indexed_db/leveldb/leveldb_comparator.h" | 14 #include "content/browser/indexed_db/leveldb/leveldb_comparator.h" |
14 #include "content/browser/indexed_db/leveldb/leveldb_database.h" | 15 #include "content/browser/indexed_db/leveldb/leveldb_database.h" |
15 #include "content/browser/indexed_db/leveldb/leveldb_iterator.h" | 16 #include "content/browser/indexed_db/leveldb/leveldb_iterator.h" |
16 #include "content/browser/indexed_db/leveldb/leveldb_slice.h" | |
17 #include "content/browser/indexed_db/leveldb/leveldb_transaction.h" | 17 #include "content/browser/indexed_db/leveldb/leveldb_transaction.h" |
18 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
19 | 19 |
20 namespace content { | 20 namespace content { |
21 | 21 |
22 namespace { | 22 namespace { |
23 | 23 |
24 class SimpleComparator : public LevelDBComparator { | 24 class SimpleComparator : public LevelDBComparator { |
25 public: | 25 public: |
26 virtual int Compare(const LevelDBSlice& a, const LevelDBSlice& b) const | 26 virtual int Compare(const base::StringPiece& a, |
27 OVERRIDE { | 27 const base::StringPiece& b) const OVERRIDE { |
28 size_t len = std::min(a.end() - a.begin(), b.end() - b.begin()); | 28 size_t len = std::min(a.size(), b.size()); |
29 return memcmp(a.begin(), b.begin(), len); | 29 return memcmp(a.begin(), b.begin(), len); |
30 } | 30 } |
31 virtual const char* Name() const OVERRIDE { return "temp_comparator"; } | 31 virtual const char* Name() const OVERRIDE { return "temp_comparator"; } |
32 }; | 32 }; |
33 | 33 |
34 std::vector<char> EncodeString(const std::string& s) { | |
35 std::vector<char> ret(s.size()); | |
36 for (size_t i = 0; i < s.size(); ++i) | |
37 ret[i] = s[i]; | |
38 return ret; | |
39 } | |
40 | |
41 TEST(LevelDBDatabaseTest, CorruptionTest) { | 34 TEST(LevelDBDatabaseTest, CorruptionTest) { |
42 base::ScopedTempDir temp_directory; | 35 base::ScopedTempDir temp_directory; |
43 ASSERT_TRUE(temp_directory.CreateUniqueTempDir()); | 36 ASSERT_TRUE(temp_directory.CreateUniqueTempDir()); |
44 | 37 |
45 const std::vector<char> key = EncodeString("key"); | 38 const std::string key("key"); |
46 const std::vector<char> value = EncodeString("value"); | 39 const std::string value("value"); |
47 std::vector<char> put_value; | 40 std::string put_value; |
48 std::string got_value; | 41 std::string got_value; |
49 SimpleComparator comparator; | 42 SimpleComparator comparator; |
50 | 43 |
51 scoped_ptr<LevelDBDatabase> leveldb = | 44 scoped_ptr<LevelDBDatabase> leveldb = |
52 LevelDBDatabase::Open(temp_directory.path(), &comparator); | 45 LevelDBDatabase::Open(temp_directory.path(), &comparator); |
53 EXPECT_TRUE(leveldb); | 46 EXPECT_TRUE(leveldb); |
54 put_value = value; | 47 put_value = value; |
55 bool success = leveldb->Put(LevelDBSlice(key), &put_value); | 48 bool success = leveldb->Put(key, &put_value); |
56 EXPECT_TRUE(success); | 49 EXPECT_TRUE(success); |
57 leveldb.Pass(); | 50 leveldb.Pass(); |
58 EXPECT_FALSE(leveldb); | 51 EXPECT_FALSE(leveldb); |
59 | 52 |
60 leveldb = LevelDBDatabase::Open(temp_directory.path(), &comparator); | 53 leveldb = LevelDBDatabase::Open(temp_directory.path(), &comparator); |
61 EXPECT_TRUE(leveldb); | 54 EXPECT_TRUE(leveldb); |
62 bool found = false; | 55 bool found = false; |
63 success = leveldb->Get(LevelDBSlice(key), &got_value, &found); | 56 success = leveldb->Get(key, &got_value, &found); |
64 EXPECT_TRUE(success); | 57 EXPECT_TRUE(success); |
65 EXPECT_TRUE(found); | 58 EXPECT_TRUE(found); |
66 EXPECT_EQ(value, std::vector<char>(got_value.begin(), got_value.end())); | 59 EXPECT_EQ(value, got_value); |
67 leveldb.Pass(); | 60 leveldb.Pass(); |
68 EXPECT_FALSE(leveldb); | 61 EXPECT_FALSE(leveldb); |
69 | 62 |
70 base::FilePath file_path = temp_directory.path().AppendASCII("CURRENT"); | 63 base::FilePath file_path = temp_directory.path().AppendASCII("CURRENT"); |
71 base::PlatformFile handle = base::CreatePlatformFile( | 64 base::PlatformFile handle = base::CreatePlatformFile( |
72 file_path, | 65 file_path, |
73 base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_WRITE, | 66 base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_WRITE, |
74 NULL, | 67 NULL, |
75 NULL); | 68 NULL); |
76 base::TruncatePlatformFile(handle, 0); | 69 base::TruncatePlatformFile(handle, 0); |
77 base::ClosePlatformFile(handle); | 70 base::ClosePlatformFile(handle); |
78 | 71 |
79 leveldb = LevelDBDatabase::Open(temp_directory.path(), &comparator); | 72 leveldb = LevelDBDatabase::Open(temp_directory.path(), &comparator); |
80 EXPECT_FALSE(leveldb); | 73 EXPECT_FALSE(leveldb); |
81 | 74 |
82 bool destroyed = LevelDBDatabase::Destroy(temp_directory.path()); | 75 bool destroyed = LevelDBDatabase::Destroy(temp_directory.path()); |
83 EXPECT_TRUE(destroyed); | 76 EXPECT_TRUE(destroyed); |
84 | 77 |
85 leveldb = LevelDBDatabase::Open(temp_directory.path(), &comparator); | 78 leveldb = LevelDBDatabase::Open(temp_directory.path(), &comparator); |
86 EXPECT_TRUE(leveldb); | 79 EXPECT_TRUE(leveldb); |
87 success = leveldb->Get(LevelDBSlice(key), &got_value, &found); | 80 success = leveldb->Get(key, &got_value, &found); |
88 EXPECT_TRUE(success); | 81 EXPECT_TRUE(success); |
89 EXPECT_FALSE(found); | 82 EXPECT_FALSE(found); |
90 } | 83 } |
91 | 84 |
92 TEST(LevelDBDatabaseTest, Transaction) { | 85 TEST(LevelDBDatabaseTest, Transaction) { |
93 base::ScopedTempDir temp_directory; | 86 base::ScopedTempDir temp_directory; |
94 ASSERT_TRUE(temp_directory.CreateUniqueTempDir()); | 87 ASSERT_TRUE(temp_directory.CreateUniqueTempDir()); |
95 | 88 |
96 const std::vector<char> key = EncodeString("key"); | 89 const std::string key("key"); |
97 std::string got_value; | 90 std::string got_value; |
98 std::vector<char> put_value; | 91 std::string put_value; |
99 SimpleComparator comparator; | 92 SimpleComparator comparator; |
100 | 93 |
101 scoped_ptr<LevelDBDatabase> leveldb = | 94 scoped_ptr<LevelDBDatabase> leveldb = |
102 LevelDBDatabase::Open(temp_directory.path(), &comparator); | 95 LevelDBDatabase::Open(temp_directory.path(), &comparator); |
103 EXPECT_TRUE(leveldb); | 96 EXPECT_TRUE(leveldb); |
104 | 97 |
105 const std::vector<char> old_value = EncodeString("value"); | 98 const std::string old_value("value"); |
106 put_value = old_value; | 99 put_value = old_value; |
107 bool success = leveldb->Put(LevelDBSlice(key), &put_value); | 100 bool success = leveldb->Put(key, &put_value); |
108 EXPECT_TRUE(success); | 101 EXPECT_TRUE(success); |
109 | 102 |
110 scoped_refptr<LevelDBTransaction> transaction = | 103 scoped_refptr<LevelDBTransaction> transaction = |
111 LevelDBTransaction::Create(leveldb.get()); | 104 LevelDBTransaction::Create(leveldb.get()); |
112 | 105 |
113 const std::vector<char> new_value = EncodeString("new value"); | 106 const std::string new_value("new value"); |
114 put_value = new_value; | 107 put_value = new_value; |
115 success = leveldb->Put(LevelDBSlice(key), &put_value); | 108 success = leveldb->Put(key, &put_value); |
116 EXPECT_TRUE(success); | 109 EXPECT_TRUE(success); |
117 | 110 |
118 bool found = false; | 111 bool found = false; |
119 success = transaction->Get(LevelDBSlice(key), &got_value, &found); | 112 success = transaction->Get(key, &got_value, &found); |
120 EXPECT_TRUE(success); | 113 EXPECT_TRUE(success); |
121 EXPECT_TRUE(found); | 114 EXPECT_TRUE(found); |
122 EXPECT_EQ( | 115 EXPECT_EQ(comparator.Compare(got_value, old_value), 0); |
123 comparator.Compare(LevelDBSlice(got_value), LevelDBSlice(old_value)), 0); | |
124 | 116 |
125 found = false; | 117 found = false; |
126 success = leveldb->Get(LevelDBSlice(key), &got_value, &found); | 118 success = leveldb->Get(key, &got_value, &found); |
127 EXPECT_TRUE(success); | 119 EXPECT_TRUE(success); |
128 EXPECT_TRUE(found); | 120 EXPECT_TRUE(found); |
129 EXPECT_EQ( | 121 EXPECT_EQ(comparator.Compare(got_value, new_value), 0); |
130 comparator.Compare(LevelDBSlice(got_value), LevelDBSlice(new_value)), 0); | |
131 | 122 |
132 const std::vector<char> added_key = EncodeString("added key"); | 123 const std::string added_key("added key"); |
133 const std::vector<char> added_value = EncodeString("added value"); | 124 const std::string added_value("added value"); |
134 put_value = added_value; | 125 put_value = added_value; |
135 success = leveldb->Put(LevelDBSlice(added_key), &put_value); | 126 success = leveldb->Put(added_key, &put_value); |
136 EXPECT_TRUE(success); | 127 EXPECT_TRUE(success); |
137 | 128 |
138 success = leveldb->Get(LevelDBSlice(added_key), &got_value, &found); | 129 success = leveldb->Get(added_key, &got_value, &found); |
139 EXPECT_TRUE(success); | 130 EXPECT_TRUE(success); |
140 EXPECT_TRUE(found); | 131 EXPECT_TRUE(found); |
141 EXPECT_EQ( | 132 EXPECT_EQ(comparator.Compare(got_value, added_value), 0); |
142 comparator.Compare(LevelDBSlice(got_value), LevelDBSlice(added_value)), | |
143 0); | |
144 | 133 |
145 success = transaction->Get(LevelDBSlice(added_key), &got_value, &found); | 134 success = transaction->Get(added_key, &got_value, &found); |
146 EXPECT_TRUE(success); | 135 EXPECT_TRUE(success); |
147 EXPECT_FALSE(found); | 136 EXPECT_FALSE(found); |
148 | 137 |
149 const std::vector<char> another_key = EncodeString("another key"); | 138 const std::string another_key("another key"); |
150 const std::vector<char> another_value = EncodeString("another value"); | 139 const std::string another_value("another value"); |
151 put_value = another_value; | 140 put_value = another_value; |
152 transaction->Put(LevelDBSlice(another_key), &put_value); | 141 transaction->Put(another_key, &put_value); |
153 | 142 |
154 success = transaction->Get(LevelDBSlice(another_key), &got_value, &found); | 143 success = transaction->Get(another_key, &got_value, &found); |
155 EXPECT_TRUE(success); | 144 EXPECT_TRUE(success); |
156 EXPECT_TRUE(found); | 145 EXPECT_TRUE(found); |
157 EXPECT_EQ( | 146 EXPECT_EQ(comparator.Compare(got_value, another_value), 0); |
158 comparator.Compare(LevelDBSlice(got_value), LevelDBSlice(another_value)), | |
159 0); | |
160 } | 147 } |
161 | 148 |
162 TEST(LevelDBDatabaseTest, TransactionIterator) { | 149 TEST(LevelDBDatabaseTest, TransactionIterator) { |
163 base::ScopedTempDir temp_directory; | 150 base::ScopedTempDir temp_directory; |
164 ASSERT_TRUE(temp_directory.CreateUniqueTempDir()); | 151 ASSERT_TRUE(temp_directory.CreateUniqueTempDir()); |
165 | 152 |
166 const std::vector<char> key1 = EncodeString("key1"); | 153 const std::string key1("key1"); |
167 const std::vector<char> value1 = EncodeString("value1"); | 154 const std::string value1("value1"); |
168 const std::vector<char> key2 = EncodeString("key2"); | 155 const std::string key2("key2"); |
169 const std::vector<char> value2 = EncodeString("value2"); | 156 const std::string value2("value2"); |
170 std::vector<char> put_value; | 157 std::string put_value; |
171 SimpleComparator comparator; | 158 SimpleComparator comparator; |
172 bool success; | 159 bool success; |
173 | 160 |
174 scoped_ptr<LevelDBDatabase> leveldb = | 161 scoped_ptr<LevelDBDatabase> leveldb = |
175 LevelDBDatabase::Open(temp_directory.path(), &comparator); | 162 LevelDBDatabase::Open(temp_directory.path(), &comparator); |
176 EXPECT_TRUE(leveldb); | 163 EXPECT_TRUE(leveldb); |
177 | 164 |
178 put_value = value1; | 165 put_value = value1; |
179 success = leveldb->Put(LevelDBSlice(key1), &put_value); | 166 success = leveldb->Put(key1, &put_value); |
180 EXPECT_TRUE(success); | 167 EXPECT_TRUE(success); |
181 put_value = value2; | 168 put_value = value2; |
182 success = leveldb->Put(LevelDBSlice(key2), &put_value); | 169 success = leveldb->Put(key2, &put_value); |
183 EXPECT_TRUE(success); | 170 EXPECT_TRUE(success); |
184 | 171 |
185 scoped_refptr<LevelDBTransaction> transaction = | 172 scoped_refptr<LevelDBTransaction> transaction = |
186 LevelDBTransaction::Create(leveldb.get()); | 173 LevelDBTransaction::Create(leveldb.get()); |
187 | 174 |
188 success = leveldb->Remove(LevelDBSlice(key2)); | 175 success = leveldb->Remove(key2); |
189 EXPECT_TRUE(success); | 176 EXPECT_TRUE(success); |
190 | 177 |
191 scoped_ptr<LevelDBIterator> it = transaction->CreateIterator(); | 178 scoped_ptr<LevelDBIterator> it = transaction->CreateIterator(); |
192 | 179 |
193 it->Seek(LevelDBSlice(std::string())); | 180 it->Seek(std::string()); |
194 | 181 |
195 EXPECT_TRUE(it->IsValid()); | 182 EXPECT_TRUE(it->IsValid()); |
196 EXPECT_EQ(comparator.Compare(LevelDBSlice(it->Key()), LevelDBSlice(key1)), 0); | 183 EXPECT_EQ(comparator.Compare(it->Key(), key1), 0); |
197 EXPECT_EQ(comparator.Compare(it->Value(), LevelDBSlice(value1)), 0); | 184 EXPECT_EQ(comparator.Compare(it->Value(), value1), 0); |
198 | 185 |
199 it->Next(); | 186 it->Next(); |
200 | 187 |
201 EXPECT_TRUE(it->IsValid()); | 188 EXPECT_TRUE(it->IsValid()); |
202 EXPECT_EQ(comparator.Compare(it->Key(), LevelDBSlice(key2)), 0); | 189 EXPECT_EQ(comparator.Compare(it->Key(), key2), 0); |
203 EXPECT_EQ(comparator.Compare(it->Value(), LevelDBSlice(value2)), 0); | 190 EXPECT_EQ(comparator.Compare(it->Value(), value2), 0); |
204 | 191 |
205 it->Next(); | 192 it->Next(); |
206 | 193 |
207 EXPECT_FALSE(it->IsValid()); | 194 EXPECT_FALSE(it->IsValid()); |
208 } | 195 } |
209 | 196 |
210 } // namespace | 197 } // namespace |
211 | 198 |
212 } // namespace content | 199 } // namespace content |
OLD | NEW |