| 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" |
| 11 #include "base/strings/utf_string_conversions.h" | 11 #include "base/strings/utf_string_conversions.h" |
| 12 #include "content/browser/indexed_db/indexed_db_factory.h" | 12 #include "content/browser/indexed_db/indexed_db_factory.h" |
| 13 #include "content/browser/indexed_db/indexed_db_leveldb_coding.h" | 13 #include "content/browser/indexed_db/indexed_db_leveldb_coding.h" |
| 14 #include "googleurl/src/gurl.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
| 15 #include "third_party/WebKit/public/web/WebSecurityOrigin.h" | 16 #include "webkit/common/database/database_identifier.h" |
| 16 | 17 |
| 17 using WebKit::WebSecurityOrigin; | |
| 18 using WebKit::WebIDBKey; | 18 using WebKit::WebIDBKey; |
| 19 | 19 |
| 20 namespace content { | 20 namespace content { |
| 21 | 21 |
| 22 namespace { | 22 namespace { |
| 23 | 23 |
| 24 class IndexedDBBackingStoreTest : public testing::Test { | 24 class IndexedDBBackingStoreTest : public testing::Test { |
| 25 public: | 25 public: |
| 26 IndexedDBBackingStoreTest() {} | 26 IndexedDBBackingStoreTest() {} |
| 27 virtual void SetUp() { | 27 virtual void SetUp() { |
| 28 string16 file_identifier; | 28 std::string file_identifier; |
| 29 backing_store_ = IndexedDBBackingStore::OpenInMemory(file_identifier); | 29 backing_store_ = IndexedDBBackingStore::OpenInMemory(file_identifier); |
| 30 | 30 |
| 31 // useful keys and values during tests | 31 // useful keys and values during tests |
| 32 const char raw_value1[] = "value1"; | 32 const char raw_value1[] = "value1"; |
| 33 | 33 |
| 34 const char raw_value2[] = "value2"; | 34 const char raw_value2[] = "value2"; |
| 35 const char raw_value3[] = "value3"; | 35 const char raw_value3[] = "value3"; |
| 36 m_value1.insert( | 36 m_value1.insert( |
| 37 m_value1.end(), &raw_value1[0], &raw_value1[0] + sizeof(raw_value1)); | 37 m_value1.end(), &raw_value1[0], &raw_value1[0] + sizeof(raw_value1)); |
| 38 m_value2.insert( | 38 m_value2.insert( |
| (...skipping 287 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 326 } | 326 } |
| 327 } | 327 } |
| 328 | 328 |
| 329 class MockIDBFactory : public IndexedDBFactory { | 329 class MockIDBFactory : public IndexedDBFactory { |
| 330 public: | 330 public: |
| 331 static scoped_refptr<MockIDBFactory> Create() { | 331 static scoped_refptr<MockIDBFactory> Create() { |
| 332 return make_scoped_refptr(new MockIDBFactory()); | 332 return make_scoped_refptr(new MockIDBFactory()); |
| 333 } | 333 } |
| 334 | 334 |
| 335 scoped_refptr<IndexedDBBackingStore> TestOpenBackingStore( | 335 scoped_refptr<IndexedDBBackingStore> TestOpenBackingStore( |
| 336 const WebSecurityOrigin& origin, | 336 const GURL& origin, |
| 337 const base::FilePath& data_directory) { | 337 const base::FilePath& data_directory) { |
| 338 WebKit::WebIDBCallbacks::DataLoss data_loss = | 338 WebKit::WebIDBCallbacks::DataLoss data_loss = |
| 339 WebKit::WebIDBCallbacks::DataLossNone; | 339 WebKit::WebIDBCallbacks::DataLossNone; |
| 340 scoped_refptr<IndexedDBBackingStore> backing_store = OpenBackingStore( | 340 scoped_refptr<IndexedDBBackingStore> backing_store = |
| 341 origin.databaseIdentifier(), data_directory, &data_loss); | 341 OpenBackingStore(webkit_database::GetIdentifierFromOrigin(origin), |
| 342 data_directory, |
| 343 &data_loss); |
| 342 EXPECT_EQ(WebKit::WebIDBCallbacks::DataLossNone, data_loss); | 344 EXPECT_EQ(WebKit::WebIDBCallbacks::DataLossNone, data_loss); |
| 343 return backing_store; | 345 return backing_store; |
| 344 } | 346 } |
| 345 | 347 |
| 346 private: | 348 private: |
| 347 virtual ~MockIDBFactory() {} | 349 virtual ~MockIDBFactory() {} |
| 348 }; | 350 }; |
| 349 | 351 |
| 350 TEST(IndexedDBFactoryTest, BackingStoreLifetime) { | 352 TEST(IndexedDBFactoryTest, BackingStoreLifetime) { |
| 351 WebSecurityOrigin origin1 = | 353 GURL origin1("http://localhost:81"); |
| 352 WebSecurityOrigin::createFromString("http://localhost:81"); | 354 GURL origin2("http://localhost:82"); |
| 353 WebSecurityOrigin origin2 = | |
| 354 WebSecurityOrigin::createFromString("http://localhost:82"); | |
| 355 | 355 |
| 356 scoped_refptr<MockIDBFactory> factory = MockIDBFactory::Create(); | 356 scoped_refptr<MockIDBFactory> factory = MockIDBFactory::Create(); |
| 357 | 357 |
| 358 base::ScopedTempDir temp_directory; | 358 base::ScopedTempDir temp_directory; |
| 359 ASSERT_TRUE(temp_directory.CreateUniqueTempDir()); | 359 ASSERT_TRUE(temp_directory.CreateUniqueTempDir()); |
| 360 scoped_refptr<IndexedDBBackingStore> disk_store1 = | 360 scoped_refptr<IndexedDBBackingStore> disk_store1 = |
| 361 factory->TestOpenBackingStore(origin1, temp_directory.path()); | 361 factory->TestOpenBackingStore(origin1, temp_directory.path()); |
| 362 EXPECT_TRUE(disk_store1->HasOneRef()); | 362 EXPECT_TRUE(disk_store1->HasOneRef()); |
| 363 | 363 |
| 364 scoped_refptr<IndexedDBBackingStore> disk_store2 = | 364 scoped_refptr<IndexedDBBackingStore> disk_store2 = |
| 365 factory->TestOpenBackingStore(origin1, temp_directory.path()); | 365 factory->TestOpenBackingStore(origin1, temp_directory.path()); |
| 366 EXPECT_EQ(disk_store1.get(), disk_store2.get()); | 366 EXPECT_EQ(disk_store1.get(), disk_store2.get()); |
| 367 EXPECT_FALSE(disk_store2->HasOneRef()); | 367 EXPECT_FALSE(disk_store2->HasOneRef()); |
| 368 | 368 |
| 369 scoped_refptr<IndexedDBBackingStore> disk_store3 = | 369 scoped_refptr<IndexedDBBackingStore> disk_store3 = |
| 370 factory->TestOpenBackingStore(origin2, temp_directory.path()); | 370 factory->TestOpenBackingStore(origin2, temp_directory.path()); |
| 371 EXPECT_TRUE(disk_store3->HasOneRef()); | 371 EXPECT_TRUE(disk_store3->HasOneRef()); |
| 372 EXPECT_FALSE(disk_store1->HasOneRef()); | 372 EXPECT_FALSE(disk_store1->HasOneRef()); |
| 373 | 373 |
| 374 disk_store2 = NULL; | 374 disk_store2 = NULL; |
| 375 EXPECT_TRUE(disk_store1->HasOneRef()); | 375 EXPECT_TRUE(disk_store1->HasOneRef()); |
| 376 } | 376 } |
| 377 | 377 |
| 378 TEST(IndexedDBFactoryTest, MemoryBackingStoreLifetime) { | 378 TEST(IndexedDBFactoryTest, MemoryBackingStoreLifetime) { |
| 379 WebSecurityOrigin origin1 = | 379 GURL origin1("http://localhost:81"); |
| 380 WebSecurityOrigin::createFromString("http://localhost:81"); | 380 GURL origin2("http://localhost:82"); |
| 381 WebSecurityOrigin origin2 = | |
| 382 WebSecurityOrigin::createFromString("http://localhost:82"); | |
| 383 | 381 |
| 384 scoped_refptr<MockIDBFactory> factory = MockIDBFactory::Create(); | 382 scoped_refptr<MockIDBFactory> factory = MockIDBFactory::Create(); |
| 385 scoped_refptr<IndexedDBBackingStore> mem_store1 = | 383 scoped_refptr<IndexedDBBackingStore> mem_store1 = |
| 386 factory->TestOpenBackingStore(origin1, base::FilePath()); | 384 factory->TestOpenBackingStore(origin1, base::FilePath()); |
| 387 EXPECT_FALSE(mem_store1->HasOneRef()); // mem_store1 and factory | 385 EXPECT_FALSE(mem_store1->HasOneRef()); // mem_store1 and factory |
| 388 | 386 |
| 389 scoped_refptr<IndexedDBBackingStore> mem_store2 = | 387 scoped_refptr<IndexedDBBackingStore> mem_store2 = |
| 390 factory->TestOpenBackingStore(origin1, base::FilePath()); | 388 factory->TestOpenBackingStore(origin1, base::FilePath()); |
| 391 EXPECT_EQ(mem_store1.get(), mem_store2.get()); | 389 EXPECT_EQ(mem_store1.get(), mem_store2.get()); |
| 392 EXPECT_FALSE(mem_store1->HasOneRef()); // mem_store1, 2 and factory | 390 EXPECT_FALSE(mem_store1->HasOneRef()); // mem_store1, 2 and factory |
| (...skipping 16 matching lines...) Expand all Loading... |
| 409 TEST(IndexedDBFactoryTest, RejectLongOrigins) { | 407 TEST(IndexedDBFactoryTest, RejectLongOrigins) { |
| 410 base::ScopedTempDir temp_directory; | 408 base::ScopedTempDir temp_directory; |
| 411 ASSERT_TRUE(temp_directory.CreateUniqueTempDir()); | 409 ASSERT_TRUE(temp_directory.CreateUniqueTempDir()); |
| 412 const base::FilePath base_path = temp_directory.path(); | 410 const base::FilePath base_path = temp_directory.path(); |
| 413 scoped_refptr<MockIDBFactory> factory = MockIDBFactory::Create(); | 411 scoped_refptr<MockIDBFactory> factory = MockIDBFactory::Create(); |
| 414 | 412 |
| 415 int limit = file_util::GetMaximumPathComponentLength(base_path); | 413 int limit = file_util::GetMaximumPathComponentLength(base_path); |
| 416 EXPECT_GT(limit, 0); | 414 EXPECT_GT(limit, 0); |
| 417 | 415 |
| 418 std::string origin(limit + 1, 'x'); | 416 std::string origin(limit + 1, 'x'); |
| 419 WebSecurityOrigin too_long_origin = | 417 GURL too_long_origin("http://" + origin + ":81/"); |
| 420 WebSecurityOrigin::createFromString(WebKit::WebString::fromUTF8( | |
| 421 std::string("http://" + origin + ":81/").c_str())); | |
| 422 scoped_refptr<IndexedDBBackingStore> diskStore1 = | 418 scoped_refptr<IndexedDBBackingStore> diskStore1 = |
| 423 factory->TestOpenBackingStore(too_long_origin, base_path); | 419 factory->TestOpenBackingStore(too_long_origin, base_path); |
| 424 EXPECT_FALSE(diskStore1); | 420 EXPECT_FALSE(diskStore1); |
| 425 | 421 |
| 426 WebSecurityOrigin ok_origin = | 422 GURL ok_origin("http://someorigin.com:82/"); |
| 427 WebSecurityOrigin::createFromString("http://someorigin.com:82/"); | |
| 428 scoped_refptr<IndexedDBBackingStore> diskStore2 = | 423 scoped_refptr<IndexedDBBackingStore> diskStore2 = |
| 429 factory->TestOpenBackingStore(ok_origin, base_path); | 424 factory->TestOpenBackingStore(ok_origin, base_path); |
| 430 EXPECT_TRUE(diskStore2); | 425 EXPECT_TRUE(diskStore2); |
| 431 } | 426 } |
| 432 | 427 |
| 433 } // namespace | 428 } // namespace |
| 434 | 429 |
| 435 } // namespace content | 430 } // namespace content |
| OLD | NEW |