OLD | NEW |
(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_t high_database_id = 1ULL << 35; |
| 86 const int64_t high_object_store_id = 1ULL << 39; |
| 87 // index_ids are capped at 32 bits for storage purposes. |
| 88 const int64_t high_index_id = 1ULL << 29; |
| 89 |
| 90 const int64_t 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_t database_id = 1; |
| 165 const int64_t object_store_id = 1; |
| 166 const int64_t index_id = kMinimumIndexId; |
| 167 const int64_t invalid_low_index_id = |
| 168 19; // index_ids must be > kMinimumIndexId |
| 169 |
| 170 std::vector<char> result_value; |
| 171 |
| 172 IndexedDBBackingStore::Transaction transaction1(backing_store_.get()); |
| 173 transaction1.begin(); |
| 174 |
| 175 IndexedDBBackingStore::RecordIdentifier record; |
| 176 bool ok = backing_store_->PutRecord(&transaction1, |
| 177 database_id, |
| 178 KeyPrefix::kInvalidId, |
| 179 m_key1, |
| 180 m_value1, |
| 181 &record); |
| 182 EXPECT_FALSE(ok); |
| 183 ok = backing_store_->PutRecord( |
| 184 &transaction1, database_id, 0, m_key1, m_value1, &record); |
| 185 EXPECT_FALSE(ok); |
| 186 ok = backing_store_->PutRecord(&transaction1, |
| 187 KeyPrefix::kInvalidId, |
| 188 object_store_id, |
| 189 m_key1, |
| 190 m_value1, |
| 191 &record); |
| 192 EXPECT_FALSE(ok); |
| 193 ok = backing_store_->PutRecord( |
| 194 &transaction1, 0, object_store_id, m_key1, m_value1, &record); |
| 195 EXPECT_FALSE(ok); |
| 196 |
| 197 ok = backing_store_->GetRecord( |
| 198 &transaction1, database_id, KeyPrefix::kInvalidId, m_key1, result_value); |
| 199 EXPECT_FALSE(ok); |
| 200 ok = backing_store_->GetRecord( |
| 201 &transaction1, database_id, 0, m_key1, result_value); |
| 202 EXPECT_FALSE(ok); |
| 203 ok = backing_store_->GetRecord(&transaction1, |
| 204 KeyPrefix::kInvalidId, |
| 205 object_store_id, |
| 206 m_key1, |
| 207 result_value); |
| 208 EXPECT_FALSE(ok); |
| 209 ok = backing_store_->GetRecord( |
| 210 &transaction1, 0, object_store_id, m_key1, result_value); |
| 211 EXPECT_FALSE(ok); |
| 212 |
| 213 scoped_ptr<IndexedDBKey> new_primary_key; |
| 214 ok = backing_store_->GetPrimaryKeyViaIndex(&transaction1, |
| 215 database_id, |
| 216 object_store_id, |
| 217 KeyPrefix::kInvalidId, |
| 218 m_key1, |
| 219 &new_primary_key); |
| 220 EXPECT_FALSE(ok); |
| 221 ok = backing_store_->GetPrimaryKeyViaIndex(&transaction1, |
| 222 database_id, |
| 223 object_store_id, |
| 224 invalid_low_index_id, |
| 225 m_key1, |
| 226 &new_primary_key); |
| 227 EXPECT_FALSE(ok); |
| 228 ok = backing_store_->GetPrimaryKeyViaIndex( |
| 229 &transaction1, database_id, object_store_id, 0, m_key1, &new_primary_key); |
| 230 EXPECT_FALSE(ok); |
| 231 |
| 232 ok = backing_store_->GetPrimaryKeyViaIndex(&transaction1, |
| 233 KeyPrefix::kInvalidId, |
| 234 object_store_id, |
| 235 index_id, |
| 236 m_key1, |
| 237 &new_primary_key); |
| 238 EXPECT_FALSE(ok); |
| 239 ok = backing_store_->GetPrimaryKeyViaIndex(&transaction1, |
| 240 database_id, |
| 241 KeyPrefix::kInvalidId, |
| 242 index_id, |
| 243 m_key1, |
| 244 &new_primary_key); |
| 245 EXPECT_FALSE(ok); |
| 246 } |
| 247 |
| 248 TEST_F(IndexedDBBackingStoreTest, CreateDatabase) { |
| 249 const string16 database_name(ASCIIToUTF16("db1")); |
| 250 int64_t database_id; |
| 251 const string16 version(ASCIIToUTF16("old_string_version")); |
| 252 const int64_t int_version = 9; |
| 253 |
| 254 const int64_t object_store_id = 99; |
| 255 const string16 object_store_name(ASCIIToUTF16("object_store1")); |
| 256 const bool auto_increment = true; |
| 257 const IndexedDBKeyPath object_store_key_path( |
| 258 ASCIIToUTF16("object_store_key")); |
| 259 |
| 260 const int64_t index_id = 999; |
| 261 const string16 index_name(ASCIIToUTF16("index1")); |
| 262 const bool unique = true; |
| 263 const bool multi_entry = true; |
| 264 const IndexedDBKeyPath index_key_path(ASCIIToUTF16("index_key")); |
| 265 |
| 266 { |
| 267 bool ok = backing_store_->CreateIDBDatabaseMetaData( |
| 268 database_name, version, int_version, database_id); |
| 269 EXPECT_TRUE(ok); |
| 270 EXPECT_GT(database_id, 0); |
| 271 |
| 272 IndexedDBBackingStore::Transaction transaction(backing_store_.get()); |
| 273 transaction.begin(); |
| 274 |
| 275 ok = backing_store_->CreateObjectStore(&transaction, |
| 276 database_id, |
| 277 object_store_id, |
| 278 object_store_name, |
| 279 object_store_key_path, |
| 280 auto_increment); |
| 281 EXPECT_TRUE(ok); |
| 282 |
| 283 ok = backing_store_->CreateIndex(&transaction, |
| 284 database_id, |
| 285 object_store_id, |
| 286 index_id, |
| 287 index_name, |
| 288 index_key_path, |
| 289 unique, |
| 290 multi_entry); |
| 291 EXPECT_TRUE(ok); |
| 292 |
| 293 ok = transaction.Commit(); |
| 294 EXPECT_TRUE(ok); |
| 295 } |
| 296 |
| 297 { |
| 298 IndexedDBDatabaseMetadata database; |
| 299 bool found; |
| 300 bool ok = |
| 301 backing_store_->GetIDBDatabaseMetaData(database_name, &database, found); |
| 302 EXPECT_TRUE(ok); |
| 303 EXPECT_TRUE(found); |
| 304 |
| 305 // database.name is not filled in by the implementation. |
| 306 EXPECT_EQ(version, database.version); |
| 307 EXPECT_EQ(int_version, database.int_version); |
| 308 EXPECT_EQ(database_id, database.id); |
| 309 |
| 310 ok = backing_store_->GetObjectStores(database.id, &database.object_stores); |
| 311 EXPECT_TRUE(ok); |
| 312 |
| 313 EXPECT_EQ(1UL, database.object_stores.size()); |
| 314 IndexedDBObjectStoreMetadata object_store = |
| 315 database.object_stores[object_store_id]; |
| 316 EXPECT_EQ(object_store_name, object_store.name); |
| 317 EXPECT_EQ(object_store_key_path, object_store.key_path); |
| 318 EXPECT_EQ(auto_increment, object_store.auto_increment); |
| 319 |
| 320 EXPECT_EQ(1UL, object_store.indexes.size()); |
| 321 IndexedDBIndexMetadata index = object_store.indexes[index_id]; |
| 322 EXPECT_EQ(index_name, index.name); |
| 323 EXPECT_EQ(index_key_path, index.key_path); |
| 324 EXPECT_EQ(unique, index.unique); |
| 325 EXPECT_EQ(multi_entry, index.multi_entry); |
| 326 } |
| 327 } |
| 328 |
| 329 class MockIDBFactory : public IndexedDBFactoryImpl { |
| 330 public: |
| 331 static scoped_refptr<MockIDBFactory> Create() { |
| 332 return make_scoped_refptr(new MockIDBFactory()); |
| 333 } |
| 334 |
| 335 scoped_refptr<IndexedDBBackingStore> TestOpenBackingStore( |
| 336 const WebSecurityOrigin& origin, |
| 337 const base::FilePath& data_directory) { |
| 338 string16 path = UTF8ToUTF16(data_directory.AsUTF8Unsafe()); |
| 339 return OpenBackingStore(origin.databaseIdentifier(), path); |
| 340 } |
| 341 |
| 342 private: |
| 343 virtual ~MockIDBFactory() {} |
| 344 }; |
| 345 |
| 346 TEST(IndexedDBFactoryTest, BackingStoreLifetime) { |
| 347 WebSecurityOrigin origin1 = |
| 348 WebSecurityOrigin::createFromString("http://localhost:81"); |
| 349 WebSecurityOrigin origin2 = |
| 350 WebSecurityOrigin::createFromString("http://localhost:82"); |
| 351 |
| 352 scoped_refptr<MockIDBFactory> factory = MockIDBFactory::Create(); |
| 353 |
| 354 base::ScopedTempDir temp_directory; |
| 355 DCHECK(temp_directory.CreateUniqueTempDir()); |
| 356 scoped_refptr<IndexedDBBackingStore> disk_store1 = |
| 357 factory->TestOpenBackingStore(origin1, temp_directory.path()); |
| 358 EXPECT_TRUE(disk_store1->HasOneRef()); |
| 359 |
| 360 scoped_refptr<IndexedDBBackingStore> disk_store2 = |
| 361 factory->TestOpenBackingStore(origin1, temp_directory.path()); |
| 362 EXPECT_EQ(disk_store1.get(), disk_store2.get()); |
| 363 // TODO(alecflett): Verify refcount indirectly. |
| 364 // EXPECT_EQ(2, disk_store2->RefCount()); |
| 365 |
| 366 scoped_refptr<IndexedDBBackingStore> disk_store3 = |
| 367 factory->TestOpenBackingStore(origin2, temp_directory.path()); |
| 368 EXPECT_TRUE(disk_store3->HasOneRef()); |
| 369 // TODO(alecflett): Verify refcount indirectly. |
| 370 // EXPECT_EQ(2, disk_store1->RefCount()); |
| 371 } |
| 372 |
| 373 TEST(IndexedDBFactoryTest, MemoryBackingStoreLifetime) { |
| 374 WebSecurityOrigin origin1 = |
| 375 WebSecurityOrigin::createFromString("http://localhost:81"); |
| 376 WebSecurityOrigin origin2 = |
| 377 WebSecurityOrigin::createFromString("http://localhost:82"); |
| 378 |
| 379 scoped_refptr<MockIDBFactory> factory = MockIDBFactory::Create(); |
| 380 scoped_refptr<IndexedDBBackingStore> mem_store1 = |
| 381 factory->TestOpenBackingStore(origin1, base::FilePath()); |
| 382 // TODO(alecflett): Verify refcount indirectly. |
| 383 // EXPECT_EQ(2, mem_store1->RefCount()); |
| 384 scoped_refptr<IndexedDBBackingStore> mem_store2 = |
| 385 factory->TestOpenBackingStore(origin1, base::FilePath()); |
| 386 EXPECT_EQ(mem_store1.get(), mem_store2.get()); |
| 387 // TODO(alecflett): Verify refcount indirectly. |
| 388 // EXPECT_EQ(3, mem_store2->RefCount()); |
| 389 |
| 390 scoped_refptr<IndexedDBBackingStore> mem_store3 = |
| 391 factory->TestOpenBackingStore(origin2, base::FilePath()); |
| 392 // TODO(alecflett): Verify refcount indirectly. |
| 393 // EXPECT_EQ(2, mem_store3->RefCount()); |
| 394 // EXPECT_EQ(3, mem_store1->RefCount()); |
| 395 |
| 396 factory = NULL; |
| 397 // TODO(alecflett): Verify refcount indirectly. |
| 398 // EXPECT_EQ(2, mem_store1->RefCount()); |
| 399 // EXPECT_EQ(1, mem_store3->RefCount()); |
| 400 } |
| 401 |
| 402 } // namespace |
| 403 |
| 404 } // namespace content |
OLD | NEW |