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/cache_storage/cache_storage_manager.h" | 5 #include "content/browser/cache_storage/cache_storage_manager.h" |
6 | 6 |
7 #include "base/files/file_path.h" | 7 #include "base/files/file_path.h" |
8 #include "base/files/file_util.h" | 8 #include "base/files/file_util.h" |
9 #include "base/files/scoped_temp_dir.h" | 9 #include "base/files/scoped_temp_dir.h" |
10 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
11 #include "base/sha1.h" | |
11 #include "base/stl_util.h" | 12 #include "base/stl_util.h" |
13 #include "base/strings/string_number_conversions.h" | |
12 #include "base/thread_task_runner_handle.h" | 14 #include "base/thread_task_runner_handle.h" |
15 #include "content/browser/cache_storage/cache_storage.pb.h" | |
13 #include "content/browser/cache_storage/cache_storage_quota_client.h" | 16 #include "content/browser/cache_storage/cache_storage_quota_client.h" |
14 #include "content/browser/fileapi/chrome_blob_storage_context.h" | 17 #include "content/browser/fileapi/chrome_blob_storage_context.h" |
15 #include "content/browser/quota/mock_quota_manager_proxy.h" | 18 #include "content/browser/quota/mock_quota_manager_proxy.h" |
16 #include "content/public/browser/browser_thread.h" | 19 #include "content/public/browser/browser_thread.h" |
17 #include "content/public/browser/cache_storage_usage_info.h" | 20 #include "content/public/browser/cache_storage_usage_info.h" |
18 #include "content/public/test/test_browser_context.h" | 21 #include "content/public/test/test_browser_context.h" |
19 #include "content/public/test/test_browser_thread_bundle.h" | 22 #include "content/public/test/test_browser_thread_bundle.h" |
20 #include "net/url_request/url_request_context_getter.h" | 23 #include "net/url_request/url_request_context_getter.h" |
21 #include "storage/browser/blob/blob_storage_context.h" | 24 #include "storage/browser/blob/blob_storage_context.h" |
22 #include "storage/browser/quota/quota_manager_proxy.h" | 25 #include "storage/browser/quota/quota_manager_proxy.h" |
(...skipping 655 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
678 EXPECT_FALSE(base::DirectoryExists(legacy_path_)); | 681 EXPECT_FALSE(base::DirectoryExists(legacy_path_)); |
679 EXPECT_TRUE(base::DirectoryExists(new_path_)); | 682 EXPECT_TRUE(base::DirectoryExists(new_path_)); |
680 | 683 |
681 EXPECT_TRUE(Keys(origin1_)); | 684 EXPECT_TRUE(Keys(origin1_)); |
682 std::vector<std::string> expected_keys; | 685 std::vector<std::string> expected_keys; |
683 expected_keys.push_back(cache1_); | 686 expected_keys.push_back(cache1_); |
684 expected_keys.push_back(cache2_); | 687 expected_keys.push_back(cache2_); |
685 EXPECT_EQ(expected_keys, callback_strings_); | 688 EXPECT_EQ(expected_keys, callback_strings_); |
686 } | 689 } |
687 | 690 |
691 // Tests that legacy caches created via a hash of the cache name instead of a | |
692 // random name continue to function in a new world of random cache names. | |
693 class LegacyCacheDirectoryNameTest : public CacheStorageManagerTest { | |
694 protected: | |
695 LegacyCacheDirectoryNameTest() | |
696 : legacy_cache_name_("foo"), stored_url_("http://example.com/foo") {} | |
697 | |
698 void SetUp() override { | |
699 CacheStorageManagerTest::SetUp(); | |
700 | |
701 // Create a cache that is stored on disk with the legacy naming scheme (hash | |
702 // of the name) and without a directory name in the index. | |
703 base::FilePath origin_path = CacheStorageManager::ConstructOriginPath( | |
704 cache_manager_->root_path(), origin1_); | |
705 | |
706 // Populate a cache. | |
707 ASSERT_TRUE(Open(origin1_, legacy_cache_name_)); | |
708 EXPECT_TRUE(CachePut(callback_cache_, stored_url_)); | |
709 base::FilePath new_path = callback_cache_->path(); | |
710 | |
711 // Close the cache. | |
712 callback_cache_ = nullptr; | |
713 base::RunLoop().RunUntilIdle(); | |
714 | |
715 // Legacy index files didn't have the cache directory, so remove it from the | |
716 // index. | |
717 base::FilePath index_path = origin_path.AppendASCII("index.txt"); | |
718 std::string index_contents; | |
719 base::ReadFileToString(index_path, &index_contents); | |
720 CacheStorageIndex index; | |
721 ASSERT_TRUE(index.ParseFromString(index_contents)); | |
722 ASSERT_EQ(1, index.cache_size()); | |
723 index.mutable_cache(0)->release_cache_dir(); | |
724 ASSERT_TRUE(index.SerializeToString(&index_contents)); | |
725 index.ParseFromString(index_contents); | |
726 ASSERT_EQ(index_contents.size(), | |
727 (uint32_t)base::WriteFile(index_path, index_contents.c_str(), | |
728 index_contents.size())); | |
729 | |
730 // Move the cache to the legacy location. | |
731 legacy_path_ = origin_path.AppendASCII(HexedHash(legacy_cache_name_)); | |
732 ASSERT_FALSE(base::DirectoryExists(legacy_path_)); | |
733 ASSERT_TRUE(base::Move(new_path, legacy_path_)); | |
734 | |
735 // Create a new manager to reread the index file. | |
736 quota_manager_proxy_->SimulateQuotaManagerDestroyed(); | |
737 cache_manager_ = CacheStorageManager::Create(cache_manager_.get()); | |
738 } | |
739 | |
740 static std::string HexedHash(const std::string& value) { | |
741 std::string value_hash = base::SHA1HashString(value); | |
742 std::string valued_hexed_hash = base::ToLowerASCII( | |
743 base::HexEncode(value_hash.c_str(), value_hash.length())); | |
744 return valued_hexed_hash; | |
745 } | |
746 | |
747 base::FilePath legacy_path_; | |
748 const std::string legacy_cache_name_; | |
749 const GURL stored_url_; | |
750 | |
751 DISALLOW_COPY_AND_ASSIGN(LegacyCacheDirectoryNameTest); | |
752 }; | |
753 | |
754 TEST_F(LegacyCacheDirectoryNameTest, LegacyCacheWorks) { | |
755 EXPECT_TRUE(Open(origin1_, legacy_cache_name_)); | |
756 EXPECT_TRUE(CacheMatch(callback_cache_, stored_url_)); | |
757 EXPECT_TRUE(CachePut(callback_cache_, GURL("http://example.com/foo2"))); | |
758 EXPECT_TRUE(CacheMatch(callback_cache_, GURL("http://example.com/foo2"))); | |
759 } | |
760 | |
761 TEST_F(LegacyCacheDirectoryNameTest, RandomDirectoryCacheSideBySideWithLegacy) { | |
762 EXPECT_TRUE(Open(origin1_, legacy_cache_name_)); | |
763 EXPECT_TRUE(Open(origin1_, "bar")); | |
764 EXPECT_TRUE(CachePut(callback_cache_, stored_url_)); | |
765 EXPECT_TRUE(CacheMatch(callback_cache_, stored_url_)); | |
766 } | |
767 | |
768 TEST_F(LegacyCacheDirectoryNameTest, DeleteLegacyCacheAndRecreateNew) { | |
769 EXPECT_TRUE(Delete(origin1_, legacy_cache_name_)); | |
jsbell
2015/10/22 16:38:52
This exercises the failure seen on Windows, right?
jkarlin
2015/10/23 17:23:09
No, but I've since added the test StorageReuseCach
| |
770 EXPECT_TRUE(Open(origin1_, legacy_cache_name_)); | |
771 EXPECT_FALSE(CacheMatch(callback_cache_, stored_url_)); | |
772 EXPECT_TRUE(CachePut(callback_cache_, GURL("http://example.com/foo2"))); | |
773 EXPECT_TRUE(CacheMatch(callback_cache_, GURL("http://example.com/foo2"))); | |
774 } | |
775 | |
688 class CacheStorageQuotaClientTest : public CacheStorageManagerTest { | 776 class CacheStorageQuotaClientTest : public CacheStorageManagerTest { |
689 protected: | 777 protected: |
690 CacheStorageQuotaClientTest() {} | 778 CacheStorageQuotaClientTest() {} |
691 | 779 |
692 void SetUp() override { | 780 void SetUp() override { |
693 CacheStorageManagerTest::SetUp(); | 781 CacheStorageManagerTest::SetUp(); |
694 quota_client_.reset( | 782 quota_client_.reset( |
695 new CacheStorageQuotaClient(cache_manager_->AsWeakPtr())); | 783 new CacheStorageQuotaClient(cache_manager_->AsWeakPtr())); |
696 } | 784 } |
697 | 785 |
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
830 | 918 |
831 INSTANTIATE_TEST_CASE_P(CacheStorageManagerTests, | 919 INSTANTIATE_TEST_CASE_P(CacheStorageManagerTests, |
832 CacheStorageManagerTestP, | 920 CacheStorageManagerTestP, |
833 ::testing::Values(false, true)); | 921 ::testing::Values(false, true)); |
834 | 922 |
835 INSTANTIATE_TEST_CASE_P(CacheStorageQuotaClientTests, | 923 INSTANTIATE_TEST_CASE_P(CacheStorageQuotaClientTests, |
836 CacheStorageQuotaClientTestP, | 924 CacheStorageQuotaClientTestP, |
837 ::testing::Values(false, true)); | 925 ::testing::Values(false, true)); |
838 | 926 |
839 } // namespace content | 927 } // namespace content |
OLD | NEW |