Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(200)

Side by Side Diff: content/browser/cache_storage/cache_storage_manager_unittest.cc

Issue 1422363004: [CacheStorage] Give cache directories unique names (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@2526
Patch Set: Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « content/browser/cache_storage/cache_storage_manager.h ('k') | content/content_tests.gypi » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/guid.h"
10 #include "base/run_loop.h" 11 #include "base/run_loop.h"
12 #include "base/sha1.h"
11 #include "base/stl_util.h" 13 #include "base/stl_util.h"
14 #include "base/strings/string_number_conversions.h"
12 #include "base/thread_task_runner_handle.h" 15 #include "base/thread_task_runner_handle.h"
16 #include "content/browser/cache_storage/cache_storage.pb.h"
13 #include "content/browser/cache_storage/cache_storage_quota_client.h" 17 #include "content/browser/cache_storage/cache_storage_quota_client.h"
14 #include "content/browser/fileapi/chrome_blob_storage_context.h" 18 #include "content/browser/fileapi/chrome_blob_storage_context.h"
15 #include "content/browser/quota/mock_quota_manager_proxy.h" 19 #include "content/browser/quota/mock_quota_manager_proxy.h"
16 #include "content/public/browser/browser_thread.h" 20 #include "content/public/browser/browser_thread.h"
17 #include "content/public/browser/cache_storage_usage_info.h" 21 #include "content/public/browser/cache_storage_usage_info.h"
18 #include "content/public/test/test_browser_context.h" 22 #include "content/public/test/test_browser_context.h"
19 #include "content/public/test/test_browser_thread_bundle.h" 23 #include "content/public/test/test_browser_thread_bundle.h"
24 #include "net/url_request/url_request_context.h"
20 #include "net/url_request/url_request_context_getter.h" 25 #include "net/url_request/url_request_context_getter.h"
26 #include "net/url_request/url_request_job_factory_impl.h"
27 #include "storage/browser/blob/blob_data_builder.h"
28 #include "storage/browser/blob/blob_data_handle.h"
21 #include "storage/browser/blob/blob_storage_context.h" 29 #include "storage/browser/blob/blob_storage_context.h"
30 #include "storage/browser/blob/blob_url_request_job_factory.h"
22 #include "storage/browser/quota/quota_manager_proxy.h" 31 #include "storage/browser/quota/quota_manager_proxy.h"
23 #include "testing/gtest/include/gtest/gtest.h" 32 #include "testing/gtest/include/gtest/gtest.h"
24 33
25 namespace content { 34 namespace content {
26 35
36 // Returns a BlobProtocolHandler that uses |blob_storage_context|. Caller owns
37 // the memory.
38 scoped_ptr<storage::BlobProtocolHandler> CreateMockBlobProtocolHandler(
39 storage::BlobStorageContext* blob_storage_context) {
40 // The FileSystemContext and thread task runner are not actually used but a
41 // task runner is needed to avoid a DCHECK in BlobURLRequestJob ctor.
42 return make_scoped_ptr(new storage::BlobProtocolHandler(
43 blob_storage_context, NULL, base::ThreadTaskRunnerHandle::Get().get()));
44 }
45
27 class CacheStorageManagerTest : public testing::Test { 46 class CacheStorageManagerTest : public testing::Test {
28 public: 47 public:
29 CacheStorageManagerTest() 48 CacheStorageManagerTest()
30 : browser_thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP), 49 : browser_thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP),
50 blob_storage_context_(nullptr),
31 callback_bool_(false), 51 callback_bool_(false),
32 callback_error_(CACHE_STORAGE_OK), 52 callback_error_(CACHE_STORAGE_OK),
33 origin1_("http://example1.com"), 53 origin1_("http://example1.com"),
34 origin2_("http://example2.com") {} 54 origin2_("http://example2.com") {}
35 55
36 void SetUp() override { 56 void SetUp() override {
37 ChromeBlobStorageContext* blob_storage_context( 57 ChromeBlobStorageContext* blob_storage_context(
38 ChromeBlobStorageContext::GetFor(&browser_context_)); 58 ChromeBlobStorageContext::GetFor(&browser_context_));
39 // Wait for ChromeBlobStorageContext to finish initializing. 59 // Wait for ChromeBlobStorageContext to finish initializing.
40 base::RunLoop().RunUntilIdle(); 60 base::RunLoop().RunUntilIdle();
41 61
62 blob_storage_context_ = blob_storage_context->context();
63
64 url_request_job_factory_.reset(new net::URLRequestJobFactoryImpl);
65 url_request_job_factory_->SetProtocolHandler(
66 "blob", CreateMockBlobProtocolHandler(blob_storage_context->context()));
67
68 net::URLRequestContext* url_request_context =
69 browser_context_.GetRequestContext()->GetURLRequestContext();
70
71 url_request_context->set_job_factory(url_request_job_factory_.get());
72
42 quota_manager_proxy_ = new MockQuotaManagerProxy( 73 quota_manager_proxy_ = new MockQuotaManagerProxy(
43 nullptr, base::ThreadTaskRunnerHandle::Get().get()); 74 nullptr, base::ThreadTaskRunnerHandle::Get().get());
44 75
45 if (MemoryOnly()) { 76 if (MemoryOnly()) {
46 cache_manager_ = CacheStorageManager::Create( 77 cache_manager_ = CacheStorageManager::Create(
47 base::FilePath(), base::ThreadTaskRunnerHandle::Get(), 78 base::FilePath(), base::ThreadTaskRunnerHandle::Get(),
48 quota_manager_proxy_); 79 quota_manager_proxy_);
49 } else { 80 } else {
50 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); 81 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
51 cache_manager_ = CacheStorageManager::Create( 82 cache_manager_ = CacheStorageManager::Create(
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
94 run_loop->Quit(); 125 run_loop->Quit();
95 } 126 }
96 127
97 void CacheMatchCallback( 128 void CacheMatchCallback(
98 base::RunLoop* run_loop, 129 base::RunLoop* run_loop,
99 CacheStorageError error, 130 CacheStorageError error,
100 scoped_ptr<ServiceWorkerResponse> response, 131 scoped_ptr<ServiceWorkerResponse> response,
101 scoped_ptr<storage::BlobDataHandle> blob_data_handle) { 132 scoped_ptr<storage::BlobDataHandle> blob_data_handle) {
102 callback_error_ = error; 133 callback_error_ = error;
103 callback_cache_response_ = response.Pass(); 134 callback_cache_response_ = response.Pass();
104 // Deliberately drop the data handle as only the url is being tested. 135 callback_data_handle_ = blob_data_handle.Pass();
105 run_loop->Quit(); 136 run_loop->Quit();
106 } 137 }
107 138
108 bool Open(const GURL& origin, const std::string& cache_name) { 139 bool Open(const GURL& origin, const std::string& cache_name) {
109 base::RunLoop loop; 140 base::RunLoop loop;
110 cache_manager_->OpenCache( 141 cache_manager_->OpenCache(
111 origin, cache_name, 142 origin, cache_name,
112 base::Bind(&CacheStorageManagerTest::CacheAndErrorCallback, 143 base::Bind(&CacheStorageManagerTest::CacheAndErrorCallback,
113 base::Unretained(this), base::Unretained(&loop))); 144 base::Unretained(this), base::Unretained(&loop)));
114 loop.Run(); 145 loop.Run();
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
179 base::Bind(&CacheStorageManagerTest::CacheMatchCallback, 210 base::Bind(&CacheStorageManagerTest::CacheMatchCallback,
180 base::Unretained(this), base::Unretained(&loop))); 211 base::Unretained(this), base::Unretained(&loop)));
181 loop.Run(); 212 loop.Run();
182 213
183 return callback_error_ == CACHE_STORAGE_OK; 214 return callback_error_ == CACHE_STORAGE_OK;
184 } 215 }
185 216
186 bool CachePut(const scoped_refptr<CacheStorageCache>& cache, 217 bool CachePut(const scoped_refptr<CacheStorageCache>& cache,
187 const GURL& url) { 218 const GURL& url) {
188 ServiceWorkerFetchRequest request; 219 ServiceWorkerFetchRequest request;
189 ServiceWorkerResponse response;
190 request.url = url; 220 request.url = url;
191 response.url = url; 221
222 scoped_ptr<storage::BlobDataBuilder> blob_data(
223 new storage::BlobDataBuilder(base::GenerateGUID()));
224 blob_data->AppendData(url.spec());
225
226 scoped_ptr<storage::BlobDataHandle> blob_handle =
227 blob_storage_context_->AddFinishedBlob(blob_data.get());
228 ServiceWorkerResponse response(
229 url, 200, "OK", blink::WebServiceWorkerResponseTypeDefault,
230 ServiceWorkerHeaderMap(), blob_handle->uuid(), url.spec().size(),
231 GURL(), blink::WebServiceWorkerResponseErrorUnknown);
192 232
193 CacheStorageBatchOperation operation; 233 CacheStorageBatchOperation operation;
194 operation.operation_type = CACHE_STORAGE_CACHE_OPERATION_TYPE_PUT; 234 operation.operation_type = CACHE_STORAGE_CACHE_OPERATION_TYPE_PUT;
195 operation.request = request; 235 operation.request = request;
196 operation.response = response; 236 operation.response = response;
197 237
198 base::RunLoop loop; 238 base::RunLoop loop;
199 cache->BatchOperation( 239 cache->BatchOperation(
200 std::vector<CacheStorageBatchOperation>(1, operation), 240 std::vector<CacheStorageBatchOperation>(1, operation),
201 base::Bind(&CacheStorageManagerTest::CachePutCallback, 241 base::Bind(&CacheStorageManagerTest::CachePutCallback,
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
249 void AllOriginsUsageCallback( 289 void AllOriginsUsageCallback(
250 base::RunLoop* run_loop, 290 base::RunLoop* run_loop,
251 const std::vector<CacheStorageUsageInfo>& usage) { 291 const std::vector<CacheStorageUsageInfo>& usage) {
252 callback_all_origins_usage_ = usage; 292 callback_all_origins_usage_ = usage;
253 run_loop->Quit(); 293 run_loop->Quit();
254 } 294 }
255 295
256 protected: 296 protected:
257 TestBrowserContext browser_context_; 297 TestBrowserContext browser_context_;
258 TestBrowserThreadBundle browser_thread_bundle_; 298 TestBrowserThreadBundle browser_thread_bundle_;
299 scoped_ptr<net::URLRequestJobFactoryImpl> url_request_job_factory_;
300 storage::BlobStorageContext* blob_storage_context_;
259 301
260 base::ScopedTempDir temp_dir_; 302 base::ScopedTempDir temp_dir_;
261 scoped_refptr<MockQuotaManagerProxy> quota_manager_proxy_; 303 scoped_refptr<MockQuotaManagerProxy> quota_manager_proxy_;
262 scoped_ptr<CacheStorageManager> cache_manager_; 304 scoped_ptr<CacheStorageManager> cache_manager_;
263 305
264 scoped_refptr<CacheStorageCache> callback_cache_; 306 scoped_refptr<CacheStorageCache> callback_cache_;
265 int callback_bool_; 307 int callback_bool_;
266 CacheStorageError callback_error_; 308 CacheStorageError callback_error_;
267 scoped_ptr<ServiceWorkerResponse> callback_cache_response_; 309 scoped_ptr<ServiceWorkerResponse> callback_cache_response_;
310 scoped_ptr<storage::BlobDataHandle> callback_data_handle_;
268 std::vector<std::string> callback_strings_; 311 std::vector<std::string> callback_strings_;
269 312
270 const GURL origin1_; 313 const GURL origin1_;
271 const GURL origin2_; 314 const GURL origin2_;
272 315
273 int64 callback_usage_; 316 int64 callback_usage_;
274 std::vector<CacheStorageUsageInfo> callback_all_origins_usage_; 317 std::vector<CacheStorageUsageInfo> callback_all_origins_usage_;
275 318
276 private: 319 private:
277 DISALLOW_COPY_AND_ASSIGN(CacheStorageManagerTest); 320 DISALLOW_COPY_AND_ASSIGN(CacheStorageManagerTest);
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after
406 EXPECT_TRUE(CachePut(callback_cache_, GURL("http://example.com/foo"))); 449 EXPECT_TRUE(CachePut(callback_cache_, GURL("http://example.com/foo")));
407 EXPECT_FALSE(StorageMatchAll(origin1_, GURL("http://example.com/bar"))); 450 EXPECT_FALSE(StorageMatchAll(origin1_, GURL("http://example.com/bar")));
408 EXPECT_EQ(CACHE_STORAGE_ERROR_NOT_FOUND, callback_error_); 451 EXPECT_EQ(CACHE_STORAGE_ERROR_NOT_FOUND, callback_error_);
409 } 452 }
410 453
411 TEST_P(CacheStorageManagerTestP, StorageMatchAllNoCaches) { 454 TEST_P(CacheStorageManagerTestP, StorageMatchAllNoCaches) {
412 EXPECT_FALSE(StorageMatchAll(origin1_, GURL("http://example.com/foo"))); 455 EXPECT_FALSE(StorageMatchAll(origin1_, GURL("http://example.com/foo")));
413 EXPECT_EQ(CACHE_STORAGE_ERROR_NOT_FOUND, callback_error_); 456 EXPECT_EQ(CACHE_STORAGE_ERROR_NOT_FOUND, callback_error_);
414 } 457 }
415 458
459 TEST_F(CacheStorageManagerTest, StorageReuseCacheName) {
460 // Deleting a cache and creating one with the same name and adding an entry
461 // with the same URL should work. (see crbug.com/542668)
462 const GURL kTestURL = GURL("http://example.com/foo");
463 EXPECT_TRUE(Open(origin1_, "foo"));
464 EXPECT_TRUE(CachePut(callback_cache_, kTestURL));
465 EXPECT_TRUE(CacheMatch(callback_cache_, kTestURL));
466 scoped_ptr<storage::BlobDataHandle> data_handle =
467 callback_data_handle_.Pass();
468
469 EXPECT_TRUE(Delete(origin1_, "foo"));
470 // The cache is deleted but the handle to one of its entries is still
471 // open. Creating a new cache in the same directory would fail on Windows.
472 EXPECT_TRUE(Open(origin1_, "foo"));
473 EXPECT_TRUE(CachePut(callback_cache_, kTestURL));
474 }
475
416 TEST_P(CacheStorageManagerTestP, StorageMatchAllEntryExistsTwice) { 476 TEST_P(CacheStorageManagerTestP, StorageMatchAllEntryExistsTwice) {
417 EXPECT_TRUE(Open(origin1_, "foo")); 477 EXPECT_TRUE(Open(origin1_, "foo"));
418 EXPECT_TRUE(CachePut(callback_cache_, GURL("http://example.com/foo"))); 478 EXPECT_TRUE(CachePut(callback_cache_, GURL("http://example.com/foo")));
419 EXPECT_TRUE(Open(origin1_, "bar")); 479 EXPECT_TRUE(Open(origin1_, "bar"));
420 EXPECT_TRUE(CachePut(callback_cache_, GURL("http://example.com/foo"))); 480 EXPECT_TRUE(CachePut(callback_cache_, GURL("http://example.com/foo")));
421 481
422 EXPECT_TRUE(StorageMatchAll(origin1_, GURL("http://example.com/foo"))); 482 EXPECT_TRUE(StorageMatchAll(origin1_, GURL("http://example.com/foo")));
423 } 483 }
424 484
425 TEST_P(CacheStorageManagerTestP, StorageMatchInOneOfMany) { 485 TEST_P(CacheStorageManagerTestP, StorageMatchInOneOfMany) {
(...skipping 252 matching lines...) Expand 10 before | Expand all | Expand 10 after
678 EXPECT_FALSE(base::DirectoryExists(legacy_path_)); 738 EXPECT_FALSE(base::DirectoryExists(legacy_path_));
679 EXPECT_TRUE(base::DirectoryExists(new_path_)); 739 EXPECT_TRUE(base::DirectoryExists(new_path_));
680 740
681 EXPECT_TRUE(Keys(origin1_)); 741 EXPECT_TRUE(Keys(origin1_));
682 std::vector<std::string> expected_keys; 742 std::vector<std::string> expected_keys;
683 expected_keys.push_back(cache1_); 743 expected_keys.push_back(cache1_);
684 expected_keys.push_back(cache2_); 744 expected_keys.push_back(cache2_);
685 EXPECT_EQ(expected_keys, callback_strings_); 745 EXPECT_EQ(expected_keys, callback_strings_);
686 } 746 }
687 747
748 // Tests that legacy caches created via a hash of the cache name instead of a
749 // random name are migrated and continue to function in a new world of random
750 // cache names.
751 class MigratedLegacyCacheDirectoryNameTest : public CacheStorageManagerTest {
752 protected:
753 MigratedLegacyCacheDirectoryNameTest()
754 : legacy_cache_name_("foo"), stored_url_("http://example.com/foo") {}
755
756 void SetUp() override {
757 CacheStorageManagerTest::SetUp();
758
759 // Create a cache that is stored on disk with the legacy naming scheme (hash
760 // of the name) and without a directory name in the index.
761 base::FilePath origin_path = CacheStorageManager::ConstructOriginPath(
762 cache_manager_->root_path(), origin1_);
763
764 // Populate a legacy cache.
765 ASSERT_TRUE(Open(origin1_, legacy_cache_name_));
766 EXPECT_TRUE(CachePut(callback_cache_, stored_url_));
767 base::FilePath new_path = callback_cache_->path();
768
769 // Close the cache.
770 callback_cache_ = nullptr;
771 base::RunLoop().RunUntilIdle();
772
773 // Legacy index files didn't have the cache directory, so remove it from the
774 // index.
775 base::FilePath index_path = origin_path.AppendASCII("index.txt");
776 std::string index_contents;
777 base::ReadFileToString(index_path, &index_contents);
778 CacheStorageIndex index;
779 ASSERT_TRUE(index.ParseFromString(index_contents));
780 ASSERT_EQ(1, index.cache_size());
781 index.mutable_cache(0)->clear_cache_dir();
782 ASSERT_TRUE(index.SerializeToString(&index_contents));
783 index.ParseFromString(index_contents);
784 ASSERT_EQ(index_contents.size(),
785 (uint32_t)base::WriteFile(index_path, index_contents.c_str(),
786 index_contents.size()));
787
788 // Move the cache to the legacy location.
789 legacy_path_ = origin_path.AppendASCII(HexedHash(legacy_cache_name_));
790 ASSERT_FALSE(base::DirectoryExists(legacy_path_));
791 ASSERT_TRUE(base::Move(new_path, legacy_path_));
792
793 // Create a new manager to reread the index file and force migration.
794 quota_manager_proxy_->SimulateQuotaManagerDestroyed();
795 cache_manager_ = CacheStorageManager::Create(cache_manager_.get());
796 }
797
798 static std::string HexedHash(const std::string& value) {
799 std::string value_hash = base::SHA1HashString(value);
800 std::string valued_hexed_hash = base::ToLowerASCII(
801 base::HexEncode(value_hash.c_str(), value_hash.length()));
802 return valued_hexed_hash;
803 }
804
805 base::FilePath legacy_path_;
806 const std::string legacy_cache_name_;
807 const GURL stored_url_;
808
809 DISALLOW_COPY_AND_ASSIGN(MigratedLegacyCacheDirectoryNameTest);
810 };
811
812 TEST_F(MigratedLegacyCacheDirectoryNameTest, LegacyCacheMigrated) {
813 EXPECT_TRUE(Open(origin1_, legacy_cache_name_));
814
815 // Verify that the cache migrated away from the legacy_path_ directory.
816 ASSERT_FALSE(base::DirectoryExists(legacy_path_));
817
818 // Verify that the existing entry still works.
819 EXPECT_TRUE(CacheMatch(callback_cache_, stored_url_));
820
821 // Verify that adding new entries works.
822 EXPECT_TRUE(CachePut(callback_cache_, GURL("http://example.com/foo2")));
823 EXPECT_TRUE(CacheMatch(callback_cache_, GURL("http://example.com/foo2")));
824 }
825
826 TEST_F(MigratedLegacyCacheDirectoryNameTest,
827 RandomDirectoryCacheSideBySideWithLegacy) {
828 EXPECT_TRUE(Open(origin1_, legacy_cache_name_));
829 EXPECT_TRUE(Open(origin1_, "bar"));
830 EXPECT_TRUE(CachePut(callback_cache_, stored_url_));
831 EXPECT_TRUE(CacheMatch(callback_cache_, stored_url_));
832 }
833
834 TEST_F(MigratedLegacyCacheDirectoryNameTest, DeleteLegacyCacheAndRecreateNew) {
835 EXPECT_TRUE(Delete(origin1_, legacy_cache_name_));
836 EXPECT_TRUE(Open(origin1_, legacy_cache_name_));
837 EXPECT_FALSE(CacheMatch(callback_cache_, stored_url_));
838 EXPECT_TRUE(CachePut(callback_cache_, GURL("http://example.com/foo2")));
839 EXPECT_TRUE(CacheMatch(callback_cache_, GURL("http://example.com/foo2")));
840 }
841
688 class CacheStorageQuotaClientTest : public CacheStorageManagerTest { 842 class CacheStorageQuotaClientTest : public CacheStorageManagerTest {
689 protected: 843 protected:
690 CacheStorageQuotaClientTest() {} 844 CacheStorageQuotaClientTest() {}
691 845
692 void SetUp() override { 846 void SetUp() override {
693 CacheStorageManagerTest::SetUp(); 847 CacheStorageManagerTest::SetUp();
694 quota_client_.reset( 848 quota_client_.reset(
695 new CacheStorageQuotaClient(cache_manager_->AsWeakPtr())); 849 new CacheStorageQuotaClient(cache_manager_->AsWeakPtr()));
696 } 850 }
697 851
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after
830 984
831 INSTANTIATE_TEST_CASE_P(CacheStorageManagerTests, 985 INSTANTIATE_TEST_CASE_P(CacheStorageManagerTests,
832 CacheStorageManagerTestP, 986 CacheStorageManagerTestP,
833 ::testing::Values(false, true)); 987 ::testing::Values(false, true));
834 988
835 INSTANTIATE_TEST_CASE_P(CacheStorageQuotaClientTests, 989 INSTANTIATE_TEST_CASE_P(CacheStorageQuotaClientTests,
836 CacheStorageQuotaClientTestP, 990 CacheStorageQuotaClientTestP,
837 ::testing::Values(false, true)); 991 ::testing::Values(false, true));
838 992
839 } // namespace content 993 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/cache_storage/cache_storage_manager.h ('k') | content/content_tests.gypi » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698