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 <stddef.h> | 5 #include <stddef.h> |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <cstring> | 8 #include <cstring> |
9 #include <string> | 9 #include <string> |
10 | 10 |
11 #include "base/files/file.h" | 11 #include "base/files/file.h" |
12 #include "base/files/file_path.h" | 12 #include "base/files/file_path.h" |
13 #include "base/files/scoped_temp_dir.h" | 13 #include "base/files/scoped_temp_dir.h" |
14 #include "base/strings/string16.h" | |
15 #include "base/strings/string_piece.h" | 14 #include "base/strings/string_piece.h" |
16 #include "content/browser/indexed_db/leveldb/leveldb_comparator.h" | 15 #include "content/browser/indexed_db/leveldb/leveldb_comparator.h" |
17 #include "content/browser/indexed_db/leveldb/leveldb_database.h" | 16 #include "content/browser/indexed_db/leveldb/leveldb_database.h" |
18 #include "content/browser/indexed_db/leveldb/leveldb_env.h" | 17 #include "content/browser/indexed_db/leveldb/leveldb_env.h" |
19 #include "content/browser/indexed_db/leveldb/leveldb_iterator.h" | |
20 #include "content/browser/indexed_db/leveldb/leveldb_transaction.h" | |
21 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
22 #include "third_party/leveldatabase/env_chromium.h" | 19 #include "third_party/leveldatabase/env_chromium.h" |
23 | 20 |
24 namespace content { | 21 namespace content { |
25 | 22 |
26 namespace { | 23 namespace { |
27 | 24 |
28 class SimpleComparator : public LevelDBComparator { | 25 class SimpleComparator : public LevelDBComparator { |
29 public: | 26 public: |
30 int Compare(const base::StringPiece& a, | 27 int Compare(const base::StringPiece& a, |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
82 | 79 |
83 status = | 80 status = |
84 LevelDBDatabase::Open(temp_directory.GetPath(), &comparator, &leveldb); | 81 LevelDBDatabase::Open(temp_directory.GetPath(), &comparator, &leveldb); |
85 EXPECT_TRUE(status.ok()); | 82 EXPECT_TRUE(status.ok()); |
86 EXPECT_TRUE(leveldb); | 83 EXPECT_TRUE(leveldb); |
87 status = leveldb->Get(key, &got_value, &found); | 84 status = leveldb->Get(key, &got_value, &found); |
88 EXPECT_TRUE(status.ok()); | 85 EXPECT_TRUE(status.ok()); |
89 EXPECT_FALSE(found); | 86 EXPECT_FALSE(found); |
90 } | 87 } |
91 | 88 |
92 TEST(LevelDBDatabaseTest, Transaction) { | |
93 base::ScopedTempDir temp_directory; | |
94 ASSERT_TRUE(temp_directory.CreateUniqueTempDir()); | |
95 | |
96 const std::string key("key"); | |
97 std::string got_value; | |
98 std::string put_value; | |
99 SimpleComparator comparator; | |
100 | |
101 std::unique_ptr<LevelDBDatabase> leveldb; | |
102 LevelDBDatabase::Open(temp_directory.GetPath(), &comparator, &leveldb); | |
103 EXPECT_TRUE(leveldb); | |
104 | |
105 const std::string old_value("value"); | |
106 put_value = old_value; | |
107 leveldb::Status status = leveldb->Put(key, &put_value); | |
108 EXPECT_TRUE(status.ok()); | |
109 | |
110 scoped_refptr<LevelDBTransaction> transaction = | |
111 new LevelDBTransaction(leveldb.get()); | |
112 | |
113 const std::string new_value("new value"); | |
114 put_value = new_value; | |
115 status = leveldb->Put(key, &put_value); | |
116 EXPECT_TRUE(status.ok()); | |
117 | |
118 bool found = false; | |
119 status = transaction->Get(key, &got_value, &found); | |
120 EXPECT_TRUE(status.ok()); | |
121 EXPECT_TRUE(found); | |
122 EXPECT_EQ(comparator.Compare(got_value, old_value), 0); | |
123 | |
124 found = false; | |
125 status = leveldb->Get(key, &got_value, &found); | |
126 EXPECT_TRUE(status.ok()); | |
127 EXPECT_TRUE(found); | |
128 EXPECT_EQ(comparator.Compare(got_value, new_value), 0); | |
129 | |
130 const std::string added_key("added key"); | |
131 const std::string added_value("added value"); | |
132 put_value = added_value; | |
133 status = leveldb->Put(added_key, &put_value); | |
134 EXPECT_TRUE(status.ok()); | |
135 | |
136 status = leveldb->Get(added_key, &got_value, &found); | |
137 EXPECT_TRUE(status.ok()); | |
138 EXPECT_TRUE(found); | |
139 EXPECT_EQ(comparator.Compare(got_value, added_value), 0); | |
140 | |
141 status = transaction->Get(added_key, &got_value, &found); | |
142 EXPECT_TRUE(status.ok()); | |
143 EXPECT_FALSE(found); | |
144 | |
145 const std::string another_key("another key"); | |
146 const std::string another_value("another value"); | |
147 put_value = another_value; | |
148 transaction->Put(another_key, &put_value); | |
149 | |
150 status = transaction->Get(another_key, &got_value, &found); | |
151 EXPECT_TRUE(status.ok()); | |
152 EXPECT_TRUE(found); | |
153 EXPECT_EQ(comparator.Compare(got_value, another_value), 0); | |
154 } | |
155 | |
156 TEST(LevelDBDatabaseTest, TransactionIterator) { | |
157 base::ScopedTempDir temp_directory; | |
158 ASSERT_TRUE(temp_directory.CreateUniqueTempDir()); | |
159 | |
160 const std::string key1("key1"); | |
161 const std::string value1("value1"); | |
162 const std::string key2("key2"); | |
163 const std::string value2("value2"); | |
164 std::string put_value; | |
165 SimpleComparator comparator; | |
166 | |
167 std::unique_ptr<LevelDBDatabase> leveldb; | |
168 LevelDBDatabase::Open(temp_directory.GetPath(), &comparator, &leveldb); | |
169 EXPECT_TRUE(leveldb); | |
170 | |
171 put_value = value1; | |
172 leveldb::Status s = leveldb->Put(key1, &put_value); | |
173 EXPECT_TRUE(s.ok()); | |
174 put_value = value2; | |
175 s = leveldb->Put(key2, &put_value); | |
176 EXPECT_TRUE(s.ok()); | |
177 | |
178 scoped_refptr<LevelDBTransaction> transaction = | |
179 new LevelDBTransaction(leveldb.get()); | |
180 | |
181 s = leveldb->Remove(key2); | |
182 EXPECT_TRUE(s.ok()); | |
183 | |
184 std::unique_ptr<LevelDBIterator> it = transaction->CreateIterator(); | |
185 | |
186 it->Seek(std::string()); | |
187 | |
188 EXPECT_TRUE(it->IsValid()); | |
189 EXPECT_EQ(comparator.Compare(it->Key(), key1), 0); | |
190 EXPECT_EQ(comparator.Compare(it->Value(), value1), 0); | |
191 | |
192 it->Next(); | |
193 | |
194 EXPECT_TRUE(it->IsValid()); | |
195 EXPECT_EQ(comparator.Compare(it->Key(), key2), 0); | |
196 EXPECT_EQ(comparator.Compare(it->Value(), value2), 0); | |
197 | |
198 it->Next(); | |
199 | |
200 EXPECT_FALSE(it->IsValid()); | |
201 } | |
202 | |
203 TEST(LevelDBDatabaseTest, TransactionCommitTest) { | |
204 base::ScopedTempDir temp_directory; | |
205 ASSERT_TRUE(temp_directory.CreateUniqueTempDir()); | |
206 | |
207 const std::string key1("key1"); | |
208 const std::string key2("key2"); | |
209 const std::string value1("value1"); | |
210 const std::string value2("value2"); | |
211 const std::string value3("value3"); | |
212 | |
213 std::string put_value; | |
214 std::string got_value; | |
215 SimpleComparator comparator; | |
216 bool found; | |
217 | |
218 std::unique_ptr<LevelDBDatabase> leveldb; | |
219 LevelDBDatabase::Open(temp_directory.GetPath(), &comparator, &leveldb); | |
220 EXPECT_TRUE(leveldb); | |
221 | |
222 scoped_refptr<LevelDBTransaction> transaction = | |
223 new LevelDBTransaction(leveldb.get()); | |
224 | |
225 put_value = value1; | |
226 transaction->Put(key1, &put_value); | |
227 | |
228 put_value = value2; | |
229 transaction->Put(key2, &put_value); | |
230 | |
231 put_value = value3; | |
232 transaction->Put(key2, &put_value); | |
233 | |
234 leveldb::Status status = transaction->Commit(); | |
235 EXPECT_TRUE(status.ok()); | |
236 | |
237 status = leveldb->Get(key1, &got_value, &found); | |
238 EXPECT_TRUE(status.ok()); | |
239 EXPECT_TRUE(found); | |
240 EXPECT_EQ(value1, got_value); | |
241 | |
242 status = leveldb->Get(key2, &got_value, &found); | |
243 EXPECT_TRUE(status.ok()); | |
244 EXPECT_TRUE(found); | |
245 EXPECT_EQ(value3, got_value); | |
246 } | |
247 | |
248 TEST(LevelDB, Locking) { | 89 TEST(LevelDB, Locking) { |
249 base::ScopedTempDir temp_directory; | 90 base::ScopedTempDir temp_directory; |
250 ASSERT_TRUE(temp_directory.CreateUniqueTempDir()); | 91 ASSERT_TRUE(temp_directory.CreateUniqueTempDir()); |
251 | 92 |
252 leveldb::Env* env = LevelDBEnv::Get(); | 93 leveldb::Env* env = LevelDBEnv::Get(); |
253 base::FilePath file = temp_directory.GetPath().AppendASCII("LOCK"); | 94 base::FilePath file = temp_directory.GetPath().AppendASCII("LOCK"); |
254 leveldb::FileLock* lock; | 95 leveldb::FileLock* lock; |
255 leveldb::Status status = env->LockFile(file.AsUTF8Unsafe(), &lock); | 96 leveldb::Status status = env->LockFile(file.AsUTF8Unsafe(), &lock); |
256 EXPECT_TRUE(status.ok()); | 97 EXPECT_TRUE(status.ok()); |
257 | 98 |
258 status = env->UnlockFile(lock); | 99 status = env->UnlockFile(lock); |
259 EXPECT_TRUE(status.ok()); | 100 EXPECT_TRUE(status.ok()); |
260 | 101 |
261 status = env->LockFile(file.AsUTF8Unsafe(), &lock); | 102 status = env->LockFile(file.AsUTF8Unsafe(), &lock); |
262 EXPECT_TRUE(status.ok()); | 103 EXPECT_TRUE(status.ok()); |
263 | 104 |
264 leveldb::FileLock* lock2; | 105 leveldb::FileLock* lock2; |
265 status = env->LockFile(file.AsUTF8Unsafe(), &lock2); | 106 status = env->LockFile(file.AsUTF8Unsafe(), &lock2); |
266 EXPECT_FALSE(status.ok()); | 107 EXPECT_FALSE(status.ok()); |
267 | 108 |
268 status = env->UnlockFile(lock); | 109 status = env->UnlockFile(lock); |
269 EXPECT_TRUE(status.ok()); | 110 EXPECT_TRUE(status.ok()); |
270 } | 111 } |
271 | 112 |
272 } // namespace content | 113 } // namespace content |
OLD | NEW |