| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "base/file_path.h" | 5 #include "base/file_path.h" |
| 6 #include "base/file_util.h" | 6 #include "base/file_util.h" |
| 7 #include "base/memory/scoped_ptr.h" | 7 #include "base/memory/scoped_ptr.h" |
| 8 #include "base/memory/scoped_temp_dir.h" | 8 #include "base/memory/scoped_temp_dir.h" |
| 9 #include "base/time.h" | 9 #include "base/time.h" |
| 10 #include "base/utf_string_conversions.h" | 10 #include "base/utf_string_conversions.h" |
| 11 #include "net/base/net_errors.h" | 11 #include "net/base/net_errors.h" |
| 12 #include "net/base/test_completion_callback.h" | 12 #include "net/base/test_completion_callback.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
| 14 #include "webkit/database/database_tracker.h" | 14 #include "webkit/database/database_tracker.h" |
| 15 #include "webkit/database/database_util.h" | 15 #include "webkit/database/database_util.h" |
| 16 #include "webkit/quota/quota_manager.h" |
| 16 #include "webkit/quota/special_storage_policy.h" | 17 #include "webkit/quota/special_storage_policy.h" |
| 17 | 18 |
| 18 namespace { | 19 namespace { |
| 19 | 20 |
| 20 const char kOrigin1Url[] = "http://origin1"; | 21 const char kOrigin1Url[] = "http://origin1"; |
| 21 const char kOrigin2Url[] = "http://protected_origin2"; | 22 const char kOrigin2Url[] = "http://protected_origin2"; |
| 22 | 23 |
| 23 class TestSpecialStoragePolicy : public quota::SpecialStoragePolicy { | 24 class TestSpecialStoragePolicy : public quota::SpecialStoragePolicy { |
| 24 public: | 25 public: |
| 25 virtual bool IsStorageProtected(const GURL& origin) { | 26 virtual bool IsStorageProtected(const GURL& origin) { |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 82 EXPECT_EQ(expected_origin_identifier, | 83 EXPECT_EQ(expected_origin_identifier, |
| 83 observer->GetNotificationOriginIdentifier()); | 84 observer->GetNotificationOriginIdentifier()); |
| 84 EXPECT_EQ(expected_database_name, | 85 EXPECT_EQ(expected_database_name, |
| 85 observer->GetNotificationDatabaseName()); | 86 observer->GetNotificationDatabaseName()); |
| 86 EXPECT_EQ(expected_database_size, | 87 EXPECT_EQ(expected_database_size, |
| 87 observer->GetNotificationDatabaseSize()); | 88 observer->GetNotificationDatabaseSize()); |
| 88 EXPECT_EQ(expected_space_available, | 89 EXPECT_EQ(expected_space_available, |
| 89 observer->GetNotificationSpaceAvailable()); | 90 observer->GetNotificationSpaceAvailable()); |
| 90 } | 91 } |
| 91 | 92 |
| 93 class TestQuotaManagerProxy : public quota::QuotaManagerProxy { |
| 94 public: |
| 95 TestQuotaManagerProxy() |
| 96 : QuotaManagerProxy(NULL, NULL), |
| 97 registered_client_(NULL) { |
| 98 } |
| 99 |
| 100 virtual ~TestQuotaManagerProxy() { |
| 101 EXPECT_FALSE(registered_client_); |
| 102 } |
| 103 |
| 104 virtual void RegisterClient(quota::QuotaClient* client) { |
| 105 EXPECT_FALSE(registered_client_); |
| 106 registered_client_ = client; |
| 107 } |
| 108 |
| 109 virtual void NotifyStorageAccessed(quota::QuotaClient::ID client_id, |
| 110 const GURL& origin, |
| 111 quota::StorageType type) { |
| 112 EXPECT_EQ(quota::QuotaClient::kDatabase, client_id); |
| 113 EXPECT_EQ(quota::kStorageTypeTemporary, type); |
| 114 accesses_[origin] += 1; |
| 115 } |
| 116 |
| 117 virtual void NotifyStorageModified(quota::QuotaClient::ID client_id, |
| 118 const GURL& origin, |
| 119 quota::StorageType type, |
| 120 int64 delta) { |
| 121 EXPECT_EQ(quota::QuotaClient::kDatabase, client_id); |
| 122 EXPECT_EQ(quota::kStorageTypeTemporary, type); |
| 123 modifications_[origin].first += 1; |
| 124 modifications_[origin].second += delta; |
| 125 } |
| 126 |
| 127 // Not needed for our tests. |
| 128 virtual void NotifyOriginInUse(const GURL& origin) {} |
| 129 virtual void NotifyOriginNoLongerInUse(const GURL& origin) {} |
| 130 |
| 131 void SimulateQuotaManagerDestroyed() { |
| 132 if (registered_client_) { |
| 133 registered_client_->OnQuotaManagerDestroyed(); |
| 134 registered_client_ = NULL; |
| 135 } |
| 136 } |
| 137 |
| 138 bool WasAccessNotified(const GURL& origin) { |
| 139 return accesses_[origin] != 0; |
| 140 } |
| 141 |
| 142 bool WasModificationNotified(const GURL& origin, int64 amount) { |
| 143 return modifications_[origin].first != 0 && |
| 144 modifications_[origin].second == amount; |
| 145 } |
| 146 |
| 147 void reset() { |
| 148 accesses_.clear(); |
| 149 modifications_.clear(); |
| 150 } |
| 151 |
| 152 quota::QuotaClient* registered_client_; |
| 153 |
| 154 // Map from origin to count of access notifications. |
| 155 std::map<GURL, int> accesses_; |
| 156 |
| 157 // Map from origin to <count, sum of deltas> |
| 158 std::map<GURL, std::pair<int, int64> > modifications_; |
| 159 }; |
| 160 |
| 161 |
| 162 bool EnsureFileOfSize(const FilePath& file_path, int64 length) { |
| 163 base::PlatformFileError error_code(base::PLATFORM_FILE_ERROR_FAILED); |
| 164 base::PlatformFile file = |
| 165 base::CreatePlatformFile( |
| 166 file_path, |
| 167 base::PLATFORM_FILE_OPEN_ALWAYS | base::PLATFORM_FILE_WRITE, |
| 168 NULL, |
| 169 &error_code); |
| 170 if (error_code != base::PLATFORM_FILE_OK) |
| 171 return false; |
| 172 if (!base::TruncatePlatformFile(file, length)) |
| 173 error_code = base::PLATFORM_FILE_ERROR_FAILED; |
| 174 base::ClosePlatformFile(file); |
| 175 return error_code == base::PLATFORM_FILE_OK; |
| 176 } |
| 177 |
| 92 } // namespace | 178 } // namespace |
| 93 | 179 |
| 94 namespace webkit_database { | 180 namespace webkit_database { |
| 95 | 181 |
| 96 // We declare a helper class, and make it a friend of DatabaseTracker using | 182 // We declare a helper class, and make it a friend of DatabaseTracker using |
| 97 // the FRIEND_TEST() macro, and we implement all tests we want to run as | 183 // the FRIEND_TEST() macro, and we implement all tests we want to run as |
| 98 // static methods of this class. Then we make our TEST() targets call these | 184 // static methods of this class. Then we make our TEST() targets call these |
| 99 // static functions. This allows us to run each test in normal mode and | 185 // static functions. This allows us to run each test in normal mode and |
| 100 // incognito mode without writing the same code twice. | 186 // incognito mode without writing the same code twice. |
| 101 class DatabaseTracker_TestHelper_Test { | 187 class DatabaseTracker_TestHelper_Test { |
| (...skipping 297 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 399 EXPECT_TRUE(tracker->GetAllOriginsInfo(&origins_info)); | 485 EXPECT_TRUE(tracker->GetAllOriginsInfo(&origins_info)); |
| 400 EXPECT_EQ(size_t(1), origins_info.size()); | 486 EXPECT_EQ(size_t(1), origins_info.size()); |
| 401 EXPECT_EQ(kOrigin2, origins_info[0].GetOrigin()); | 487 EXPECT_EQ(kOrigin2, origins_info[0].GetOrigin()); |
| 402 | 488 |
| 403 origin1_info = tracker->GetCachedOriginInfo(kOrigin1); | 489 origin1_info = tracker->GetCachedOriginInfo(kOrigin1); |
| 404 EXPECT_TRUE(origin1_info); | 490 EXPECT_TRUE(origin1_info); |
| 405 EXPECT_EQ(origin1_quota, origin1_info->Quota()); | 491 EXPECT_EQ(origin1_quota, origin1_info->Quota()); |
| 406 EXPECT_EQ(0, origin1_info->TotalSize()); | 492 EXPECT_EQ(0, origin1_info->TotalSize()); |
| 407 EXPECT_EQ(origin1_quota, tracker->GetOriginSpaceAvailable(kOrigin1)); | 493 EXPECT_EQ(origin1_quota, tracker->GetOriginSpaceAvailable(kOrigin1)); |
| 408 } | 494 } |
| 495 |
| 496 static void DatabaseTrackerQuotaIntegration() { |
| 497 const GURL kOrigin(kOrigin1Url); |
| 498 const string16 kOriginId = DatabaseUtil::GetOriginIdentifier(kOrigin); |
| 499 const string16 kName = ASCIIToUTF16("name"); |
| 500 const string16 kDescription = ASCIIToUTF16("description"); |
| 501 |
| 502 ScopedTempDir temp_dir; |
| 503 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 504 |
| 505 // Initialize the tracker with a QuotaManagerProxy |
| 506 scoped_refptr<TestQuotaManagerProxy> test_quota_proxy( |
| 507 new TestQuotaManagerProxy); |
| 508 scoped_refptr<DatabaseTracker> tracker( |
| 509 new DatabaseTracker(temp_dir.path(), false /* incognito */, |
| 510 NULL, test_quota_proxy, NULL)); |
| 511 EXPECT_TRUE(test_quota_proxy->registered_client_); |
| 512 |
| 513 // Create a database and modify it a couple of times, close it, |
| 514 // then delete it. Observe the tracker notifies accordingly. |
| 515 |
| 516 int64 database_size = 0; |
| 517 int64 space_available = 0; |
| 518 tracker->DatabaseOpened(kOriginId, kName, kDescription, 0, |
| 519 &database_size, &space_available); |
| 520 EXPECT_TRUE(test_quota_proxy->WasAccessNotified(kOrigin)); |
| 521 test_quota_proxy->reset(); |
| 522 |
| 523 FilePath db_file(tracker->GetFullDBFilePath(kOriginId, kName)); |
| 524 EXPECT_TRUE(file_util::CreateDirectory(db_file.DirName())); |
| 525 EXPECT_TRUE(EnsureFileOfSize(db_file, 10)); |
| 526 tracker->DatabaseModified(kOriginId, kName); |
| 527 EXPECT_TRUE(test_quota_proxy->WasModificationNotified(kOrigin, 10)); |
| 528 test_quota_proxy->reset(); |
| 529 |
| 530 EXPECT_TRUE(EnsureFileOfSize(db_file, 100)); |
| 531 tracker->DatabaseModified(kOriginId, kName); |
| 532 EXPECT_TRUE(test_quota_proxy->WasModificationNotified(kOrigin, 90)); |
| 533 test_quota_proxy->reset(); |
| 534 |
| 535 tracker->DatabaseClosed(kOriginId, kName); |
| 536 EXPECT_EQ(net::OK, tracker->DeleteDatabase(kOriginId, kName, NULL)); |
| 537 EXPECT_TRUE(test_quota_proxy->WasModificationNotified(kOrigin, -100)); |
| 538 |
| 539 // Create a database and modify it, try to delete it while open, |
| 540 // then close it (at which time deletion will actually occur). |
| 541 // Observe the tracker notifies accordingly. |
| 542 |
| 543 tracker->DatabaseOpened(kOriginId, kName, kDescription, 0, |
| 544 &database_size, &space_available); |
| 545 EXPECT_TRUE(test_quota_proxy->WasAccessNotified(kOrigin)); |
| 546 test_quota_proxy->reset(); |
| 547 |
| 548 db_file = tracker->GetFullDBFilePath(kOriginId, kName); |
| 549 EXPECT_TRUE(file_util::CreateDirectory(db_file.DirName())); |
| 550 EXPECT_TRUE(EnsureFileOfSize(db_file, 100)); |
| 551 tracker->DatabaseModified(kOriginId, kName); |
| 552 EXPECT_TRUE(test_quota_proxy->WasModificationNotified(kOrigin, 100)); |
| 553 test_quota_proxy->reset(); |
| 554 |
| 555 EXPECT_EQ(net::ERR_IO_PENDING, |
| 556 tracker->DeleteDatabase(kOriginId, kName, NULL)); |
| 557 EXPECT_FALSE(test_quota_proxy->WasModificationNotified(kOrigin, -100)); |
| 558 |
| 559 tracker->DatabaseClosed(kOriginId, kName); |
| 560 EXPECT_TRUE(test_quota_proxy->WasModificationNotified(kOrigin, -100)); |
| 561 |
| 562 // Create a database and up the file size without telling |
| 563 // the tracker about the modification, than simulate a |
| 564 // a renderer crash. |
| 565 // Observe the tracker notifies accordingly. |
| 566 |
| 567 tracker->DatabaseOpened(kOriginId, kName, kDescription, 0, |
| 568 &database_size, &space_available); |
| 569 EXPECT_TRUE(test_quota_proxy->WasAccessNotified(kOrigin)); |
| 570 test_quota_proxy->reset(); |
| 571 db_file = tracker->GetFullDBFilePath(kOriginId, kName); |
| 572 EXPECT_TRUE(file_util::CreateDirectory(db_file.DirName())); |
| 573 EXPECT_TRUE(EnsureFileOfSize(db_file, 100)); |
| 574 DatabaseConnections crashed_renderer_connections; |
| 575 crashed_renderer_connections.AddConnection(kOriginId, kName); |
| 576 EXPECT_FALSE(test_quota_proxy->WasModificationNotified(kOrigin, 100)); |
| 577 tracker->CloseDatabases(crashed_renderer_connections); |
| 578 EXPECT_TRUE(test_quota_proxy->WasModificationNotified(kOrigin, 100)); |
| 579 |
| 580 // Cleanup. |
| 581 crashed_renderer_connections.RemoveAllConnections(); |
| 582 test_quota_proxy->SimulateQuotaManagerDestroyed(); |
| 583 } |
| 409 }; | 584 }; |
| 410 | 585 |
| 411 TEST(DatabaseTrackerTest, DeleteOpenDatabase) { | 586 TEST(DatabaseTrackerTest, DeleteOpenDatabase) { |
| 412 DatabaseTracker_TestHelper_Test::TestDeleteOpenDatabase(false); | 587 DatabaseTracker_TestHelper_Test::TestDeleteOpenDatabase(false); |
| 413 } | 588 } |
| 414 | 589 |
| 415 TEST(DatabaseTrackerTest, DeleteOpenDatabaseIncognitoMode) { | 590 TEST(DatabaseTrackerTest, DeleteOpenDatabaseIncognitoMode) { |
| 416 DatabaseTracker_TestHelper_Test::TestDeleteOpenDatabase(true); | 591 DatabaseTracker_TestHelper_Test::TestDeleteOpenDatabase(true); |
| 417 } | 592 } |
| 418 | 593 |
| 419 TEST(DatabaseTrackerTest, DatabaseTracker) { | 594 TEST(DatabaseTrackerTest, DatabaseTracker) { |
| 420 DatabaseTracker_TestHelper_Test::TestDatabaseTracker(false); | 595 DatabaseTracker_TestHelper_Test::TestDatabaseTracker(false); |
| 421 } | 596 } |
| 422 | 597 |
| 423 TEST(DatabaseTrackerTest, DatabaseTrackerIncognitoMode) { | 598 TEST(DatabaseTrackerTest, DatabaseTrackerIncognitoMode) { |
| 424 DatabaseTracker_TestHelper_Test::TestDatabaseTracker(true); | 599 DatabaseTracker_TestHelper_Test::TestDatabaseTracker(true); |
| 425 } | 600 } |
| 426 | 601 |
| 602 TEST(DatabaseTrackerTest, DatabaseTrackerQuotaIntegration) { |
| 603 // There is no difference in behavior between incognito and not. |
| 604 DatabaseTracker_TestHelper_Test::DatabaseTrackerQuotaIntegration(); |
| 605 } |
| 606 |
| 427 } // namespace webkit_database | 607 } // namespace webkit_database |
| OLD | NEW |