| 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 "content/browser/indexed_db/indexed_db_backing_store.h" | 5 #include "content/browser/indexed_db/indexed_db_backing_store.h" |
| 6 | 6 |
| 7 #include "base/file_util.h" | 7 #include "base/file_util.h" |
| 8 #include "base/files/scoped_temp_dir.h" | 8 #include "base/files/scoped_temp_dir.h" |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/strings/string16.h" | 10 #include "base/strings/string16.h" |
| (...skipping 303 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 314 IndexedDBIndexMetadata index = object_store.indexes[index_id]; | 314 IndexedDBIndexMetadata index = object_store.indexes[index_id]; |
| 315 EXPECT_EQ(index_name, index.name); | 315 EXPECT_EQ(index_name, index.name); |
| 316 EXPECT_EQ(index_key_path, index.key_path); | 316 EXPECT_EQ(index_key_path, index.key_path); |
| 317 EXPECT_EQ(unique, index.unique); | 317 EXPECT_EQ(unique, index.unique); |
| 318 EXPECT_EQ(multi_entry, index.multi_entry); | 318 EXPECT_EQ(multi_entry, index.multi_entry); |
| 319 } | 319 } |
| 320 } | 320 } |
| 321 | 321 |
| 322 class MockIDBFactory : public IndexedDBFactory { | 322 class MockIDBFactory : public IndexedDBFactory { |
| 323 public: | 323 public: |
| 324 static scoped_refptr<MockIDBFactory> Create() { | |
| 325 return make_scoped_refptr(new MockIDBFactory()); | |
| 326 } | |
| 327 | |
| 328 scoped_refptr<IndexedDBBackingStore> TestOpenBackingStore( | 324 scoped_refptr<IndexedDBBackingStore> TestOpenBackingStore( |
| 329 const GURL& origin, | 325 const GURL& origin, |
| 330 const base::FilePath& data_directory) { | 326 const base::FilePath& data_directory) { |
| 331 WebKit::WebIDBCallbacks::DataLoss data_loss = | 327 WebKit::WebIDBCallbacks::DataLoss data_loss = |
| 332 WebKit::WebIDBCallbacks::DataLossNone; | 328 WebKit::WebIDBCallbacks::DataLossNone; |
| 333 scoped_refptr<IndexedDBBackingStore> backing_store = | 329 scoped_refptr<IndexedDBBackingStore> backing_store = |
| 334 OpenBackingStore(webkit_database::GetIdentifierFromOrigin(origin), | 330 OpenBackingStore(webkit_database::GetIdentifierFromOrigin(origin), |
| 335 data_directory, | 331 data_directory, |
| 336 &data_loss); | 332 &data_loss); |
| 337 EXPECT_EQ(WebKit::WebIDBCallbacks::DataLossNone, data_loss); | 333 EXPECT_EQ(WebKit::WebIDBCallbacks::DataLossNone, data_loss); |
| 338 return backing_store; | 334 return backing_store; |
| 339 } | 335 } |
| 340 | 336 |
| 341 private: | 337 private: |
| 342 virtual ~MockIDBFactory() {} | 338 virtual ~MockIDBFactory() {} |
| 343 }; | 339 }; |
| 344 | 340 |
| 345 TEST(IndexedDBFactoryTest, BackingStoreLifetime) { | 341 TEST(IndexedDBFactoryTest, BackingStoreLifetime) { |
| 346 GURL origin1("http://localhost:81"); | 342 GURL origin1("http://localhost:81"); |
| 347 GURL origin2("http://localhost:82"); | 343 GURL origin2("http://localhost:82"); |
| 348 | 344 |
| 349 scoped_refptr<MockIDBFactory> factory = MockIDBFactory::Create(); | 345 scoped_refptr<MockIDBFactory> factory = new MockIDBFactory(); |
| 350 | 346 |
| 351 base::ScopedTempDir temp_directory; | 347 base::ScopedTempDir temp_directory; |
| 352 ASSERT_TRUE(temp_directory.CreateUniqueTempDir()); | 348 ASSERT_TRUE(temp_directory.CreateUniqueTempDir()); |
| 353 scoped_refptr<IndexedDBBackingStore> disk_store1 = | 349 scoped_refptr<IndexedDBBackingStore> disk_store1 = |
| 354 factory->TestOpenBackingStore(origin1, temp_directory.path()); | 350 factory->TestOpenBackingStore(origin1, temp_directory.path()); |
| 355 EXPECT_TRUE(disk_store1->HasOneRef()); | 351 EXPECT_TRUE(disk_store1->HasOneRef()); |
| 356 | 352 |
| 357 scoped_refptr<IndexedDBBackingStore> disk_store2 = | 353 scoped_refptr<IndexedDBBackingStore> disk_store2 = |
| 358 factory->TestOpenBackingStore(origin1, temp_directory.path()); | 354 factory->TestOpenBackingStore(origin1, temp_directory.path()); |
| 359 EXPECT_EQ(disk_store1.get(), disk_store2.get()); | 355 EXPECT_EQ(disk_store1.get(), disk_store2.get()); |
| 360 EXPECT_FALSE(disk_store2->HasOneRef()); | 356 EXPECT_FALSE(disk_store2->HasOneRef()); |
| 361 | 357 |
| 362 scoped_refptr<IndexedDBBackingStore> disk_store3 = | 358 scoped_refptr<IndexedDBBackingStore> disk_store3 = |
| 363 factory->TestOpenBackingStore(origin2, temp_directory.path()); | 359 factory->TestOpenBackingStore(origin2, temp_directory.path()); |
| 364 EXPECT_TRUE(disk_store3->HasOneRef()); | 360 EXPECT_TRUE(disk_store3->HasOneRef()); |
| 365 EXPECT_FALSE(disk_store1->HasOneRef()); | 361 EXPECT_FALSE(disk_store1->HasOneRef()); |
| 366 | 362 |
| 367 disk_store2 = NULL; | 363 disk_store2 = NULL; |
| 368 EXPECT_TRUE(disk_store1->HasOneRef()); | 364 EXPECT_TRUE(disk_store1->HasOneRef()); |
| 369 } | 365 } |
| 370 | 366 |
| 371 TEST(IndexedDBFactoryTest, MemoryBackingStoreLifetime) { | 367 TEST(IndexedDBFactoryTest, MemoryBackingStoreLifetime) { |
| 372 GURL origin1("http://localhost:81"); | 368 GURL origin1("http://localhost:81"); |
| 373 GURL origin2("http://localhost:82"); | 369 GURL origin2("http://localhost:82"); |
| 374 | 370 |
| 375 scoped_refptr<MockIDBFactory> factory = MockIDBFactory::Create(); | 371 scoped_refptr<MockIDBFactory> factory = new MockIDBFactory(); |
| 376 scoped_refptr<IndexedDBBackingStore> mem_store1 = | 372 scoped_refptr<IndexedDBBackingStore> mem_store1 = |
| 377 factory->TestOpenBackingStore(origin1, base::FilePath()); | 373 factory->TestOpenBackingStore(origin1, base::FilePath()); |
| 378 EXPECT_FALSE(mem_store1->HasOneRef()); // mem_store1 and factory | 374 EXPECT_FALSE(mem_store1->HasOneRef()); // mem_store1 and factory |
| 379 | 375 |
| 380 scoped_refptr<IndexedDBBackingStore> mem_store2 = | 376 scoped_refptr<IndexedDBBackingStore> mem_store2 = |
| 381 factory->TestOpenBackingStore(origin1, base::FilePath()); | 377 factory->TestOpenBackingStore(origin1, base::FilePath()); |
| 382 EXPECT_EQ(mem_store1.get(), mem_store2.get()); | 378 EXPECT_EQ(mem_store1.get(), mem_store2.get()); |
| 383 EXPECT_FALSE(mem_store1->HasOneRef()); // mem_store1, 2 and factory | 379 EXPECT_FALSE(mem_store1->HasOneRef()); // mem_store1, 2 and factory |
| 384 EXPECT_FALSE(mem_store2->HasOneRef()); // mem_store1, 2 and factory | 380 EXPECT_FALSE(mem_store2->HasOneRef()); // mem_store1, 2 and factory |
| 385 | 381 |
| 386 scoped_refptr<IndexedDBBackingStore> mem_store3 = | 382 scoped_refptr<IndexedDBBackingStore> mem_store3 = |
| 387 factory->TestOpenBackingStore(origin2, base::FilePath()); | 383 factory->TestOpenBackingStore(origin2, base::FilePath()); |
| 388 EXPECT_FALSE(mem_store1->HasOneRef()); // mem_store1, 2 and factory | 384 EXPECT_FALSE(mem_store1->HasOneRef()); // mem_store1, 2 and factory |
| 389 EXPECT_FALSE(mem_store3->HasOneRef()); // mem_store3 and factory | 385 EXPECT_FALSE(mem_store3->HasOneRef()); // mem_store3 and factory |
| 390 | 386 |
| 391 factory = NULL; | 387 factory = NULL; |
| 392 EXPECT_FALSE(mem_store1->HasOneRef()); // mem_store1 and 2 | 388 EXPECT_FALSE(mem_store1->HasOneRef()); // mem_store1 and 2 |
| 393 EXPECT_FALSE(mem_store2->HasOneRef()); // mem_store1 and 2 | 389 EXPECT_FALSE(mem_store2->HasOneRef()); // mem_store1 and 2 |
| 394 EXPECT_TRUE(mem_store3->HasOneRef()); | 390 EXPECT_TRUE(mem_store3->HasOneRef()); |
| 395 | 391 |
| 396 mem_store2 = NULL; | 392 mem_store2 = NULL; |
| 397 EXPECT_TRUE(mem_store1->HasOneRef()); | 393 EXPECT_TRUE(mem_store1->HasOneRef()); |
| 398 } | 394 } |
| 399 | 395 |
| 400 TEST(IndexedDBFactoryTest, RejectLongOrigins) { | 396 TEST(IndexedDBFactoryTest, RejectLongOrigins) { |
| 401 base::ScopedTempDir temp_directory; | 397 base::ScopedTempDir temp_directory; |
| 402 ASSERT_TRUE(temp_directory.CreateUniqueTempDir()); | 398 ASSERT_TRUE(temp_directory.CreateUniqueTempDir()); |
| 403 const base::FilePath base_path = temp_directory.path(); | 399 const base::FilePath base_path = temp_directory.path(); |
| 404 scoped_refptr<MockIDBFactory> factory = MockIDBFactory::Create(); | 400 scoped_refptr<MockIDBFactory> factory = new MockIDBFactory(); |
| 405 | 401 |
| 406 int limit = file_util::GetMaximumPathComponentLength(base_path); | 402 int limit = file_util::GetMaximumPathComponentLength(base_path); |
| 407 EXPECT_GT(limit, 0); | 403 EXPECT_GT(limit, 0); |
| 408 | 404 |
| 409 std::string origin(limit + 1, 'x'); | 405 std::string origin(limit + 1, 'x'); |
| 410 GURL too_long_origin("http://" + origin + ":81/"); | 406 GURL too_long_origin("http://" + origin + ":81/"); |
| 411 scoped_refptr<IndexedDBBackingStore> diskStore1 = | 407 scoped_refptr<IndexedDBBackingStore> diskStore1 = |
| 412 factory->TestOpenBackingStore(too_long_origin, base_path); | 408 factory->TestOpenBackingStore(too_long_origin, base_path); |
| 413 EXPECT_FALSE(diskStore1); | 409 EXPECT_FALSE(diskStore1); |
| 414 | 410 |
| 415 GURL ok_origin("http://someorigin.com:82/"); | 411 GURL ok_origin("http://someorigin.com:82/"); |
| 416 scoped_refptr<IndexedDBBackingStore> diskStore2 = | 412 scoped_refptr<IndexedDBBackingStore> diskStore2 = |
| 417 factory->TestOpenBackingStore(ok_origin, base_path); | 413 factory->TestOpenBackingStore(ok_origin, base_path); |
| 418 EXPECT_TRUE(diskStore2); | 414 EXPECT_TRUE(diskStore2); |
| 419 } | 415 } |
| 420 | 416 |
| 421 } // namespace | 417 } // namespace |
| 422 | 418 |
| 423 } // namespace content | 419 } // namespace content |
| OLD | NEW |