OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/appcache/appcache_storage_impl.h" | 5 #include "content/browser/appcache/appcache_storage_impl.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 | 8 |
9 #include <memory> | 9 #include <memory> |
10 #include <stack> | 10 #include <stack> |
(...skipping 1657 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1668 void Reinitialize3() { | 1668 void Reinitialize3() { |
1669 // Recover from a corrupt sql database discovered while | 1669 // Recover from a corrupt sql database discovered while |
1670 // installing a new appcache. | 1670 // installing a new appcache. |
1671 Reinitialize(CORRUPT_SQL_ON_INSTALL); | 1671 Reinitialize(CORRUPT_SQL_ON_INSTALL); |
1672 } | 1672 } |
1673 | 1673 |
1674 void Reinitialize(ReinitTestCase test_case) { | 1674 void Reinitialize(ReinitTestCase test_case) { |
1675 // Unlike all of the other tests, this one actually read/write files. | 1675 // Unlike all of the other tests, this one actually read/write files. |
1676 ASSERT_TRUE(temp_directory_.CreateUniqueTempDir()); | 1676 ASSERT_TRUE(temp_directory_.CreateUniqueTempDir()); |
1677 | 1677 |
1678 AppCacheDatabase db(temp_directory_.path().AppendASCII("Index")); | 1678 AppCacheDatabase db(temp_directory_.GetPath().AppendASCII("Index")); |
1679 EXPECT_TRUE(db.LazyOpen(true)); | 1679 EXPECT_TRUE(db.LazyOpen(true)); |
1680 | 1680 |
1681 if (test_case == CORRUPT_CACHE_ON_INSTALL || | 1681 if (test_case == CORRUPT_CACHE_ON_INSTALL || |
1682 test_case == CORRUPT_CACHE_ON_LOAD_EXISTING) { | 1682 test_case == CORRUPT_CACHE_ON_LOAD_EXISTING) { |
1683 // Create a corrupt/unopenable disk_cache index file. | 1683 // Create a corrupt/unopenable disk_cache index file. |
1684 const std::string kCorruptData("deadbeef"); | 1684 const std::string kCorruptData("deadbeef"); |
1685 base::FilePath disk_cache_directory = | 1685 base::FilePath disk_cache_directory = |
1686 temp_directory_.path().AppendASCII("Cache"); | 1686 temp_directory_.GetPath().AppendASCII("Cache"); |
1687 ASSERT_TRUE(base::CreateDirectory(disk_cache_directory)); | 1687 ASSERT_TRUE(base::CreateDirectory(disk_cache_directory)); |
1688 base::FilePath index_file = disk_cache_directory.AppendASCII("index"); | 1688 base::FilePath index_file = disk_cache_directory.AppendASCII("index"); |
1689 EXPECT_EQ(static_cast<int>(kCorruptData.length()), | 1689 EXPECT_EQ(static_cast<int>(kCorruptData.length()), |
1690 base::WriteFile( | 1690 base::WriteFile( |
1691 index_file, kCorruptData.data(), kCorruptData.length())); | 1691 index_file, kCorruptData.data(), kCorruptData.length())); |
1692 } | 1692 } |
1693 | 1693 |
1694 // Create records for a degenerate cached manifest that only contains | 1694 // Create records for a degenerate cached manifest that only contains |
1695 // one entry for the manifest file resource. | 1695 // one entry for the manifest file resource. |
1696 if (test_case == CORRUPT_CACHE_ON_LOAD_EXISTING) { | 1696 if (test_case == CORRUPT_CACHE_ON_LOAD_EXISTING) { |
1697 AppCacheDatabase db(temp_directory_.path().AppendASCII("Index")); | 1697 AppCacheDatabase db(temp_directory_.GetPath().AppendASCII("Index")); |
1698 GURL manifest_url = MockHttpServer::GetMockUrl("manifest"); | 1698 GURL manifest_url = MockHttpServer::GetMockUrl("manifest"); |
1699 | 1699 |
1700 AppCacheDatabase::GroupRecord group_record; | 1700 AppCacheDatabase::GroupRecord group_record; |
1701 group_record.group_id = 1; | 1701 group_record.group_id = 1; |
1702 group_record.manifest_url = manifest_url; | 1702 group_record.manifest_url = manifest_url; |
1703 group_record.origin = manifest_url.GetOrigin(); | 1703 group_record.origin = manifest_url.GetOrigin(); |
1704 EXPECT_TRUE(db.InsertGroup(&group_record)); | 1704 EXPECT_TRUE(db.InsertGroup(&group_record)); |
1705 AppCacheDatabase::CacheRecord cache_record; | 1705 AppCacheDatabase::CacheRecord cache_record; |
1706 cache_record.cache_id = 1; | 1706 cache_record.cache_id = 1; |
1707 cache_record.group_id = 1; | 1707 cache_record.group_id = 1; |
1708 cache_record.online_wildcard = false; | 1708 cache_record.online_wildcard = false; |
1709 cache_record.update_time = kZeroTime; | 1709 cache_record.update_time = kZeroTime; |
1710 cache_record.cache_size = kDefaultEntrySize; | 1710 cache_record.cache_size = kDefaultEntrySize; |
1711 EXPECT_TRUE(db.InsertCache(&cache_record)); | 1711 EXPECT_TRUE(db.InsertCache(&cache_record)); |
1712 AppCacheDatabase::EntryRecord entry_record; | 1712 AppCacheDatabase::EntryRecord entry_record; |
1713 entry_record.cache_id = 1; | 1713 entry_record.cache_id = 1; |
1714 entry_record.url = manifest_url; | 1714 entry_record.url = manifest_url; |
1715 entry_record.flags = AppCacheEntry::MANIFEST; | 1715 entry_record.flags = AppCacheEntry::MANIFEST; |
1716 entry_record.response_id = 1; | 1716 entry_record.response_id = 1; |
1717 entry_record.response_size = kDefaultEntrySize; | 1717 entry_record.response_size = kDefaultEntrySize; |
1718 EXPECT_TRUE(db.InsertEntry(&entry_record)); | 1718 EXPECT_TRUE(db.InsertEntry(&entry_record)); |
1719 } | 1719 } |
1720 | 1720 |
1721 // Recreate the service to point at the db and corruption on disk. | 1721 // Recreate the service to point at the db and corruption on disk. |
1722 service_.reset(new AppCacheServiceImpl(NULL)); | 1722 service_.reset(new AppCacheServiceImpl(NULL)); |
1723 service_->set_request_context(io_thread->request_context()); | 1723 service_->set_request_context(io_thread->request_context()); |
1724 service_->Initialize(temp_directory_.path(), | 1724 service_->Initialize(temp_directory_.GetPath(), db_thread->task_runner(), |
1725 db_thread->task_runner(), | |
1726 db_thread->task_runner()); | 1725 db_thread->task_runner()); |
1727 mock_quota_manager_proxy_ = new MockQuotaManagerProxy(); | 1726 mock_quota_manager_proxy_ = new MockQuotaManagerProxy(); |
1728 service_->quota_manager_proxy_ = mock_quota_manager_proxy_; | 1727 service_->quota_manager_proxy_ = mock_quota_manager_proxy_; |
1729 delegate_.reset(new MockStorageDelegate(this)); | 1728 delegate_.reset(new MockStorageDelegate(this)); |
1730 | 1729 |
1731 // Additional setup to observe reinitailize happens. | 1730 // Additional setup to observe reinitailize happens. |
1732 observer_.reset(new MockServiceObserver(this)); | 1731 observer_.reset(new MockServiceObserver(this)); |
1733 service_->AddObserver(observer_.get()); | 1732 service_->AddObserver(observer_.get()); |
1734 | 1733 |
1735 // We continue after the init task is complete including the callback | 1734 // We continue after the init task is complete including the callback |
1736 // on the current thread. | 1735 // on the current thread. |
1737 FlushDbThreadTasks(); | 1736 FlushDbThreadTasks(); |
1738 base::ThreadTaskRunnerHandle::Get()->PostTask( | 1737 base::ThreadTaskRunnerHandle::Get()->PostTask( |
1739 FROM_HERE, base::Bind(&AppCacheStorageImplTest::Continue_Reinitialize, | 1738 FROM_HERE, base::Bind(&AppCacheStorageImplTest::Continue_Reinitialize, |
1740 base::Unretained(this), test_case)); | 1739 base::Unretained(this), test_case)); |
1741 } | 1740 } |
1742 | 1741 |
1743 void Continue_Reinitialize(ReinitTestCase test_case) { | 1742 void Continue_Reinitialize(ReinitTestCase test_case) { |
1744 const int kMockProcessId = 1; | 1743 const int kMockProcessId = 1; |
1745 backend_.reset(new AppCacheBackendImpl); | 1744 backend_.reset(new AppCacheBackendImpl); |
1746 backend_->Initialize(service_.get(), &frontend_, kMockProcessId); | 1745 backend_->Initialize(service_.get(), &frontend_, kMockProcessId); |
1747 | 1746 |
1748 if (test_case == CORRUPT_SQL_ON_INSTALL) { | 1747 if (test_case == CORRUPT_SQL_ON_INSTALL) { |
1749 // Break the db file | 1748 // Break the db file |
1750 EXPECT_FALSE(database()->was_corruption_detected()); | 1749 EXPECT_FALSE(database()->was_corruption_detected()); |
1751 ASSERT_TRUE(sql::test::CorruptSizeInHeader( | 1750 ASSERT_TRUE(sql::test::CorruptSizeInHeader( |
1752 temp_directory_.path().AppendASCII("Index"))); | 1751 temp_directory_.GetPath().AppendASCII("Index"))); |
1753 } | 1752 } |
1754 | 1753 |
1755 if (test_case == CORRUPT_CACHE_ON_INSTALL || | 1754 if (test_case == CORRUPT_CACHE_ON_INSTALL || |
1756 test_case == CORRUPT_SQL_ON_INSTALL) { | 1755 test_case == CORRUPT_SQL_ON_INSTALL) { |
1757 // Try to create a new appcache, the resulting update job will | 1756 // Try to create a new appcache, the resulting update job will |
1758 // eventually fail when it gets to disk cache initialization. | 1757 // eventually fail when it gets to disk cache initialization. |
1759 backend_->RegisterHost(1); | 1758 backend_->RegisterHost(1); |
1760 AppCacheHost* host1 = backend_->GetHost(1); | 1759 AppCacheHost* host1 = backend_->GetHost(1); |
1761 const GURL kEmptyPageUrl(MockHttpServer::GetMockUrl("empty.html")); | 1760 const GURL kEmptyPageUrl(MockHttpServer::GetMockUrl("empty.html")); |
1762 host1->first_party_url_ = kEmptyPageUrl; | 1761 host1->first_party_url_ = kEmptyPageUrl; |
(...skipping 23 matching lines...) Expand all Loading... |
1786 base::Unretained(this), | 1785 base::Unretained(this), |
1787 test_case)); | 1786 test_case)); |
1788 } | 1787 } |
1789 | 1788 |
1790 void Verify_Reinitialized(ReinitTestCase test_case) { | 1789 void Verify_Reinitialized(ReinitTestCase test_case) { |
1791 // Verify we got notified of reinit and a new storage instance is created, | 1790 // Verify we got notified of reinit and a new storage instance is created, |
1792 // and that the old data has been deleted. | 1791 // and that the old data has been deleted. |
1793 EXPECT_TRUE(observer_->observed_old_storage_.get()); | 1792 EXPECT_TRUE(observer_->observed_old_storage_.get()); |
1794 EXPECT_TRUE(observer_->observed_old_storage_->storage() != storage()); | 1793 EXPECT_TRUE(observer_->observed_old_storage_->storage() != storage()); |
1795 EXPECT_FALSE(PathExists( | 1794 EXPECT_FALSE(PathExists( |
1796 temp_directory_.path().AppendASCII("Cache").AppendASCII("index"))); | 1795 temp_directory_.GetPath().AppendASCII("Cache").AppendASCII("index"))); |
1797 EXPECT_FALSE(PathExists( | 1796 EXPECT_FALSE(PathExists(temp_directory_.GetPath().AppendASCII("Index"))); |
1798 temp_directory_.path().AppendASCII("Index"))); | |
1799 | 1797 |
1800 if (test_case == CORRUPT_SQL_ON_INSTALL) { | 1798 if (test_case == CORRUPT_SQL_ON_INSTALL) { |
1801 AppCacheStorageImpl* storage = static_cast<AppCacheStorageImpl*>( | 1799 AppCacheStorageImpl* storage = static_cast<AppCacheStorageImpl*>( |
1802 observer_->observed_old_storage_->storage()); | 1800 observer_->observed_old_storage_->storage()); |
1803 EXPECT_TRUE(storage->database_->was_corruption_detected()); | 1801 EXPECT_TRUE(storage->database_->was_corruption_detected()); |
1804 } | 1802 } |
1805 | 1803 |
1806 // Verify that the hosts saw appropriate events. | 1804 // Verify that the hosts saw appropriate events. |
1807 if (test_case == CORRUPT_CACHE_ON_INSTALL || | 1805 if (test_case == CORRUPT_CACHE_ON_INSTALL || |
1808 test_case == CORRUPT_SQL_ON_INSTALL) { | 1806 test_case == CORRUPT_SQL_ON_INSTALL) { |
(...skipping 220 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2029 RunTestOnIOThread(&AppCacheStorageImplTest::Reinitialize2); | 2027 RunTestOnIOThread(&AppCacheStorageImplTest::Reinitialize2); |
2030 } | 2028 } |
2031 | 2029 |
2032 TEST_F(AppCacheStorageImplTest, Reinitialize3) { | 2030 TEST_F(AppCacheStorageImplTest, Reinitialize3) { |
2033 RunTestOnIOThread(&AppCacheStorageImplTest::Reinitialize3); | 2031 RunTestOnIOThread(&AppCacheStorageImplTest::Reinitialize3); |
2034 } | 2032 } |
2035 | 2033 |
2036 // That's all folks! | 2034 // That's all folks! |
2037 | 2035 |
2038 } // namespace content | 2036 } // namespace content |
OLD | NEW |