Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(460)

Side by Side Diff: content/browser/indexed_db/indexed_db_backing_store_unittest.cc

Issue 15659013: Revert "Migrate the IndexedDB backend from Blink to Chromium" (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "content/browser/indexed_db/indexed_db_backing_store.h"
6
7 #include "base/files/scoped_temp_dir.h"
8 #include "base/logging.h"
9 #include "base/string16.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "content/browser/indexed_db/indexed_db_factory_impl.h"
12 #include "content/browser/indexed_db/indexed_db_leveldb_coding.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14 #include "third_party/WebKit/Source/Platform/chromium/public/WebData.h"
15 #include "third_party/WebKit/Source/WebKit/chromium/public/WebSecurityOrigin.h"
16
17 using WebKit::WebSecurityOrigin;
18 using WebKit::WebIDBKey;
19
20 namespace content {
21
22 namespace {
23
24 class IndexedDBBackingStoreTest : public testing::Test {
25 public:
26 IndexedDBBackingStoreTest() {}
27 virtual void SetUp() {
28 string16 file_identifier;
29 backing_store_ = IndexedDBBackingStore::OpenInMemory(file_identifier);
30
31 // useful keys and values during tests
32 const char raw_value1[] = "value1";
33
34 const char raw_value2[] = "value2";
35 const char raw_value3[] = "value3";
36 m_value1.insert(
37 m_value1.end(), &raw_value1[0], &raw_value1[0] + sizeof(raw_value1));
38 m_value2.insert(
39 m_value2.end(), &raw_value2[0], &raw_value2[0] + sizeof(raw_value2));
40 m_value3.insert(
41 m_value3.end(), &raw_value3[0], &raw_value3[0] + sizeof(raw_value3));
42 m_key1 = IndexedDBKey(99, WebIDBKey::NumberType);
43 m_key2 = IndexedDBKey(ASCIIToUTF16("key2"));
44 m_key3 = IndexedDBKey(ASCIIToUTF16("key3"));
45 }
46
47 protected:
48 scoped_refptr<IndexedDBBackingStore> backing_store_;
49
50 // Sample keys and values that are consistent.
51 IndexedDBKey m_key1;
52 IndexedDBKey m_key2;
53 IndexedDBKey m_key3;
54 std::vector<char> m_value1;
55 std::vector<char> m_value2;
56 std::vector<char> m_value3;
57 };
58
59 TEST_F(IndexedDBBackingStoreTest, PutGetConsistency) {
60 {
61 IndexedDBBackingStore::Transaction transaction1(backing_store_.get());
62 transaction1.begin();
63 IndexedDBBackingStore::RecordIdentifier record;
64 bool ok = backing_store_->PutRecord(
65 &transaction1, 1, 1, m_key1, m_value1, &record);
66 EXPECT_TRUE(ok);
67 transaction1.Commit();
68 }
69
70 {
71 IndexedDBBackingStore::Transaction transaction2(backing_store_.get());
72 transaction2.begin();
73 std::vector<char> result_value;
74 bool ok =
75 backing_store_->GetRecord(&transaction2, 1, 1, m_key1, result_value);
76 transaction2.Commit();
77 EXPECT_TRUE(ok);
78 EXPECT_EQ(m_value1, result_value);
79 }
80 }
81
82 // Make sure that using very high ( more than 32 bit ) values for database_id
83 // and object_store_id still work.
84 TEST_F(IndexedDBBackingStoreTest, HighIds) {
85 const int64 high_database_id = 1ULL << 35;
86 const int64 high_object_store_id = 1ULL << 39;
87 // index_ids are capped at 32 bits for storage purposes.
88 const int64 high_index_id = 1ULL << 29;
89
90 const int64 invalid_high_index_id = 1ULL << 37;
91
92 const IndexedDBKey& index_key = m_key2;
93 std::vector<char> index_key_raw = EncodeIDBKey(index_key);
94 {
95 IndexedDBBackingStore::Transaction transaction1(backing_store_.get());
96 transaction1.begin();
97 IndexedDBBackingStore::RecordIdentifier record;
98 bool ok = backing_store_->PutRecord(&transaction1,
99 high_database_id,
100 high_object_store_id,
101 m_key1,
102 m_value1,
103 &record);
104 EXPECT_TRUE(ok);
105
106 ok = backing_store_->PutIndexDataForRecord(&transaction1,
107 high_database_id,
108 high_object_store_id,
109 invalid_high_index_id,
110 index_key,
111 record);
112 EXPECT_FALSE(ok);
113
114 ok = backing_store_->PutIndexDataForRecord(&transaction1,
115 high_database_id,
116 high_object_store_id,
117 high_index_id,
118 index_key,
119 record);
120 EXPECT_TRUE(ok);
121
122 ok = transaction1.Commit();
123 EXPECT_TRUE(ok);
124 }
125
126 {
127 IndexedDBBackingStore::Transaction transaction2(backing_store_.get());
128 transaction2.begin();
129 std::vector<char> result_value;
130 bool ok = backing_store_->GetRecord(&transaction2,
131 high_database_id,
132 high_object_store_id,
133 m_key1,
134 result_value);
135 EXPECT_TRUE(ok);
136 EXPECT_EQ(m_value1, result_value);
137
138 scoped_ptr<IndexedDBKey> new_primary_key;
139 ok = backing_store_->GetPrimaryKeyViaIndex(&transaction2,
140 high_database_id,
141 high_object_store_id,
142 invalid_high_index_id,
143 index_key,
144 &new_primary_key);
145 EXPECT_FALSE(ok);
146
147 ok = backing_store_->GetPrimaryKeyViaIndex(&transaction2,
148 high_database_id,
149 high_object_store_id,
150 high_index_id,
151 index_key,
152 &new_primary_key);
153 EXPECT_TRUE(ok);
154 EXPECT_TRUE(new_primary_key->IsEqual(m_key1));
155
156 ok = transaction2.Commit();
157 EXPECT_TRUE(ok);
158 }
159 }
160
161 // Make sure that other invalid ids do not crash.
162 TEST_F(IndexedDBBackingStoreTest, InvalidIds) {
163 // valid ids for use when testing invalid ids
164 const int64 database_id = 1;
165 const int64 object_store_id = 1;
166 const int64 index_id = kMinimumIndexId;
167 const int64 invalid_low_index_id = 19; // index_ids must be > kMinimumIndexId
168
169 std::vector<char> result_value;
170
171 IndexedDBBackingStore::Transaction transaction1(backing_store_.get());
172 transaction1.begin();
173
174 IndexedDBBackingStore::RecordIdentifier record;
175 bool ok = backing_store_->PutRecord(&transaction1,
176 database_id,
177 KeyPrefix::kInvalidId,
178 m_key1,
179 m_value1,
180 &record);
181 EXPECT_FALSE(ok);
182 ok = backing_store_->PutRecord(
183 &transaction1, database_id, 0, m_key1, m_value1, &record);
184 EXPECT_FALSE(ok);
185 ok = backing_store_->PutRecord(&transaction1,
186 KeyPrefix::kInvalidId,
187 object_store_id,
188 m_key1,
189 m_value1,
190 &record);
191 EXPECT_FALSE(ok);
192 ok = backing_store_->PutRecord(
193 &transaction1, 0, object_store_id, m_key1, m_value1, &record);
194 EXPECT_FALSE(ok);
195
196 ok = backing_store_->GetRecord(
197 &transaction1, database_id, KeyPrefix::kInvalidId, m_key1, result_value);
198 EXPECT_FALSE(ok);
199 ok = backing_store_->GetRecord(
200 &transaction1, database_id, 0, m_key1, result_value);
201 EXPECT_FALSE(ok);
202 ok = backing_store_->GetRecord(&transaction1,
203 KeyPrefix::kInvalidId,
204 object_store_id,
205 m_key1,
206 result_value);
207 EXPECT_FALSE(ok);
208 ok = backing_store_->GetRecord(
209 &transaction1, 0, object_store_id, m_key1, result_value);
210 EXPECT_FALSE(ok);
211
212 scoped_ptr<IndexedDBKey> new_primary_key;
213 ok = backing_store_->GetPrimaryKeyViaIndex(&transaction1,
214 database_id,
215 object_store_id,
216 KeyPrefix::kInvalidId,
217 m_key1,
218 &new_primary_key);
219 EXPECT_FALSE(ok);
220 ok = backing_store_->GetPrimaryKeyViaIndex(&transaction1,
221 database_id,
222 object_store_id,
223 invalid_low_index_id,
224 m_key1,
225 &new_primary_key);
226 EXPECT_FALSE(ok);
227 ok = backing_store_->GetPrimaryKeyViaIndex(
228 &transaction1, database_id, object_store_id, 0, m_key1, &new_primary_key);
229 EXPECT_FALSE(ok);
230
231 ok = backing_store_->GetPrimaryKeyViaIndex(&transaction1,
232 KeyPrefix::kInvalidId,
233 object_store_id,
234 index_id,
235 m_key1,
236 &new_primary_key);
237 EXPECT_FALSE(ok);
238 ok = backing_store_->GetPrimaryKeyViaIndex(&transaction1,
239 database_id,
240 KeyPrefix::kInvalidId,
241 index_id,
242 m_key1,
243 &new_primary_key);
244 EXPECT_FALSE(ok);
245 }
246
247 TEST_F(IndexedDBBackingStoreTest, CreateDatabase) {
248 const string16 database_name(ASCIIToUTF16("db1"));
249 int64 database_id;
250 const string16 version(ASCIIToUTF16("old_string_version"));
251 const int64 int_version = 9;
252
253 const int64 object_store_id = 99;
254 const string16 object_store_name(ASCIIToUTF16("object_store1"));
255 const bool auto_increment = true;
256 const IndexedDBKeyPath object_store_key_path(
257 ASCIIToUTF16("object_store_key"));
258
259 const int64 index_id = 999;
260 const string16 index_name(ASCIIToUTF16("index1"));
261 const bool unique = true;
262 const bool multi_entry = true;
263 const IndexedDBKeyPath index_key_path(ASCIIToUTF16("index_key"));
264
265 {
266 bool ok = backing_store_->CreateIDBDatabaseMetaData(
267 database_name, version, int_version, database_id);
268 EXPECT_TRUE(ok);
269 EXPECT_GT(database_id, 0);
270
271 IndexedDBBackingStore::Transaction transaction(backing_store_.get());
272 transaction.begin();
273
274 ok = backing_store_->CreateObjectStore(&transaction,
275 database_id,
276 object_store_id,
277 object_store_name,
278 object_store_key_path,
279 auto_increment);
280 EXPECT_TRUE(ok);
281
282 ok = backing_store_->CreateIndex(&transaction,
283 database_id,
284 object_store_id,
285 index_id,
286 index_name,
287 index_key_path,
288 unique,
289 multi_entry);
290 EXPECT_TRUE(ok);
291
292 ok = transaction.Commit();
293 EXPECT_TRUE(ok);
294 }
295
296 {
297 IndexedDBDatabaseMetadata database;
298 bool found;
299 bool ok =
300 backing_store_->GetIDBDatabaseMetaData(database_name, &database, found);
301 EXPECT_TRUE(ok);
302 EXPECT_TRUE(found);
303
304 // database.name is not filled in by the implementation.
305 EXPECT_EQ(version, database.version);
306 EXPECT_EQ(int_version, database.int_version);
307 EXPECT_EQ(database_id, database.id);
308
309 ok = backing_store_->GetObjectStores(database.id, &database.object_stores);
310 EXPECT_TRUE(ok);
311
312 EXPECT_EQ(1UL, database.object_stores.size());
313 IndexedDBObjectStoreMetadata object_store =
314 database.object_stores[object_store_id];
315 EXPECT_EQ(object_store_name, object_store.name);
316 EXPECT_EQ(object_store_key_path, object_store.key_path);
317 EXPECT_EQ(auto_increment, object_store.auto_increment);
318
319 EXPECT_EQ(1UL, object_store.indexes.size());
320 IndexedDBIndexMetadata index = object_store.indexes[index_id];
321 EXPECT_EQ(index_name, index.name);
322 EXPECT_EQ(index_key_path, index.key_path);
323 EXPECT_EQ(unique, index.unique);
324 EXPECT_EQ(multi_entry, index.multi_entry);
325 }
326 }
327
328 class MockIDBFactory : public IndexedDBFactoryImpl {
329 public:
330 static scoped_refptr<MockIDBFactory> Create() {
331 return make_scoped_refptr(new MockIDBFactory());
332 }
333
334 scoped_refptr<IndexedDBBackingStore> TestOpenBackingStore(
335 const WebSecurityOrigin& origin,
336 const base::FilePath& data_directory) {
337 string16 path = UTF8ToUTF16(data_directory.AsUTF8Unsafe());
338 return OpenBackingStore(origin.databaseIdentifier(), path);
339 }
340
341 private:
342 virtual ~MockIDBFactory() {}
343 };
344
345 TEST(IndexedDBFactoryTest, BackingStoreLifetime) {
346 WebSecurityOrigin origin1 =
347 WebSecurityOrigin::createFromString("http://localhost:81");
348 WebSecurityOrigin origin2 =
349 WebSecurityOrigin::createFromString("http://localhost:82");
350
351 scoped_refptr<MockIDBFactory> factory = MockIDBFactory::Create();
352
353 base::ScopedTempDir temp_directory;
354 ASSERT_TRUE(temp_directory.CreateUniqueTempDir());
355 scoped_refptr<IndexedDBBackingStore> disk_store1 =
356 factory->TestOpenBackingStore(origin1, temp_directory.path());
357 EXPECT_TRUE(disk_store1->HasOneRef());
358
359 scoped_refptr<IndexedDBBackingStore> disk_store2 =
360 factory->TestOpenBackingStore(origin1, temp_directory.path());
361 EXPECT_EQ(disk_store1.get(), disk_store2.get());
362 // TODO(alecflett): Verify refcount indirectly.
363 // EXPECT_EQ(2, disk_store2->RefCount());
364
365 scoped_refptr<IndexedDBBackingStore> disk_store3 =
366 factory->TestOpenBackingStore(origin2, temp_directory.path());
367 EXPECT_TRUE(disk_store3->HasOneRef());
368 // TODO(alecflett): Verify refcount indirectly.
369 // EXPECT_EQ(2, disk_store1->RefCount());
370 }
371
372 TEST(IndexedDBFactoryTest, MemoryBackingStoreLifetime) {
373 WebSecurityOrigin origin1 =
374 WebSecurityOrigin::createFromString("http://localhost:81");
375 WebSecurityOrigin origin2 =
376 WebSecurityOrigin::createFromString("http://localhost:82");
377
378 scoped_refptr<MockIDBFactory> factory = MockIDBFactory::Create();
379 scoped_refptr<IndexedDBBackingStore> mem_store1 =
380 factory->TestOpenBackingStore(origin1, base::FilePath());
381 // TODO(alecflett): Verify refcount indirectly.
382 // EXPECT_EQ(2, mem_store1->RefCount());
383 scoped_refptr<IndexedDBBackingStore> mem_store2 =
384 factory->TestOpenBackingStore(origin1, base::FilePath());
385 EXPECT_EQ(mem_store1.get(), mem_store2.get());
386 // TODO(alecflett): Verify refcount indirectly.
387 // EXPECT_EQ(3, mem_store2->RefCount());
388
389 scoped_refptr<IndexedDBBackingStore> mem_store3 =
390 factory->TestOpenBackingStore(origin2, base::FilePath());
391 // TODO(alecflett): Verify refcount indirectly.
392 // EXPECT_EQ(2, mem_store3->RefCount());
393 // EXPECT_EQ(3, mem_store1->RefCount());
394
395 factory = NULL;
396 // TODO(alecflett): Verify refcount indirectly.
397 // EXPECT_EQ(2, mem_store1->RefCount());
398 // EXPECT_EQ(1, mem_store3->RefCount());
399 }
400
401 } // namespace
402
403 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/indexed_db/indexed_db_backing_store.cc ('k') | content/browser/indexed_db/indexed_db_callbacks_wrapper.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698