| 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 <stddef.h> | 7 #include <stddef.h> |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <list> |
| 10 #include <set> | 11 #include <set> |
| 11 #include <utility> | 12 #include <utility> |
| 12 | 13 |
| 14 #include "base/files/file_enumerator.h" |
| 13 #include "base/files/file_path.h" | 15 #include "base/files/file_path.h" |
| 14 #include "base/files/file_util.h" | 16 #include "base/files/file_util.h" |
| 15 #include "base/files/scoped_temp_dir.h" | 17 #include "base/files/scoped_temp_dir.h" |
| 16 #include "base/guid.h" | 18 #include "base/guid.h" |
| 17 #include "base/macros.h" | 19 #include "base/macros.h" |
| 18 #include "base/memory/ptr_util.h" | 20 #include "base/memory/ptr_util.h" |
| 19 #include "base/run_loop.h" | 21 #include "base/run_loop.h" |
| 20 #include "base/sha1.h" | 22 #include "base/sha1.h" |
| 21 #include "base/stl_util.h" | 23 #include "base/stl_util.h" |
| 22 #include "base/strings/string_number_conversions.h" | 24 #include "base/strings/string_number_conversions.h" |
| 23 #include "base/threading/thread_task_runner_handle.h" | 25 #include "base/threading/thread_task_runner_handle.h" |
| 24 #include "content/browser/blob_storage/chrome_blob_storage_context.h" | 26 #include "content/browser/blob_storage/chrome_blob_storage_context.h" |
| 27 #include "content/browser/cache_storage/cache_storage.h" |
| 25 #include "content/browser/cache_storage/cache_storage.pb.h" | 28 #include "content/browser/cache_storage/cache_storage.pb.h" |
| 26 #include "content/browser/cache_storage/cache_storage_cache_handle.h" | 29 #include "content/browser/cache_storage/cache_storage_cache_handle.h" |
| 27 #include "content/browser/cache_storage/cache_storage_quota_client.h" | 30 #include "content/browser/cache_storage/cache_storage_quota_client.h" |
| 28 #include "content/browser/quota/mock_quota_manager_proxy.h" | 31 #include "content/browser/quota/mock_quota_manager_proxy.h" |
| 29 #include "content/public/browser/browser_thread.h" | 32 #include "content/public/browser/browser_thread.h" |
| 30 #include "content/public/browser/cache_storage_usage_info.h" | 33 #include "content/public/browser/cache_storage_usage_info.h" |
| 31 #include "content/public/browser/storage_partition.h" | 34 #include "content/public/browser/storage_partition.h" |
| 32 #include "content/public/test/mock_special_storage_policy.h" | 35 #include "content/public/test/mock_special_storage_policy.h" |
| 33 #include "content/public/test/test_browser_context.h" | 36 #include "content/public/test/test_browser_context.h" |
| 34 #include "content/public/test/test_browser_thread_bundle.h" | 37 #include "content/public/test/test_browser_thread_bundle.h" |
| 35 #include "net/url_request/url_request_context.h" | 38 #include "net/url_request/url_request_context.h" |
| 36 #include "net/url_request/url_request_context_getter.h" | 39 #include "net/url_request/url_request_context_getter.h" |
| 37 #include "net/url_request/url_request_job_factory_impl.h" | 40 #include "net/url_request/url_request_job_factory_impl.h" |
| 38 #include "storage/browser/blob/blob_data_builder.h" | 41 #include "storage/browser/blob/blob_data_builder.h" |
| 39 #include "storage/browser/blob/blob_data_handle.h" | 42 #include "storage/browser/blob/blob_data_handle.h" |
| 40 #include "storage/browser/blob/blob_storage_context.h" | 43 #include "storage/browser/blob/blob_storage_context.h" |
| 41 #include "storage/browser/blob/blob_url_request_job_factory.h" | 44 #include "storage/browser/blob/blob_url_request_job_factory.h" |
| 42 #include "storage/browser/quota/quota_manager_proxy.h" | 45 #include "storage/browser/quota/quota_manager_proxy.h" |
| 43 #include "testing/gtest/include/gtest/gtest.h" | 46 #include "testing/gtest/include/gtest/gtest.h" |
| 44 | 47 |
| 45 namespace content { | 48 namespace content { |
| 46 | 49 |
| 50 namespace { |
| 51 |
| 52 bool IsIndexFileCurrent(const base::FilePath& cache_dir) { |
| 53 base::File::Info info; |
| 54 const base::FilePath index_path = |
| 55 cache_dir.AppendASCII(CacheStorage::kIndexFileName); |
| 56 if (!GetFileInfo(index_path, &info)) |
| 57 return false; |
| 58 base::Time index_last_modified = info.last_modified; |
| 59 |
| 60 base::FileEnumerator enumerator(cache_dir, false, |
| 61 base::FileEnumerator::DIRECTORIES); |
| 62 for (base::FilePath file_path = enumerator.Next(); !file_path.empty(); |
| 63 file_path = enumerator.Next()) { |
| 64 if (!GetFileInfo(file_path, &info)) |
| 65 return false; |
| 66 if (index_last_modified < info.last_modified) |
| 67 return false; |
| 68 } |
| 69 |
| 70 return true; |
| 71 } |
| 72 |
| 73 } // anonymous namespace |
| 74 |
| 75 std::vector<std::string> GetCacheInfoNames( |
| 76 const std::list<CacheStorage::CacheInfo>& cache_info) { |
| 77 std::vector<std::string> cache_names; |
| 78 for (const auto& info : cache_info) |
| 79 cache_names.push_back(info.name); |
| 80 return cache_names; |
| 81 } |
| 82 |
| 47 // Returns a BlobProtocolHandler that uses |blob_storage_context|. Caller owns | 83 // Returns a BlobProtocolHandler that uses |blob_storage_context|. Caller owns |
| 48 // the memory. | 84 // the memory. |
| 49 std::unique_ptr<storage::BlobProtocolHandler> CreateMockBlobProtocolHandler( | 85 std::unique_ptr<storage::BlobProtocolHandler> CreateMockBlobProtocolHandler( |
| 50 storage::BlobStorageContext* blob_storage_context) { | 86 storage::BlobStorageContext* blob_storage_context) { |
| 51 // The FileSystemContext and thread task runner are not actually used but a | 87 // The FileSystemContext and thread task runner are not actually used but a |
| 52 // task runner is needed to avoid a DCHECK in BlobURLRequestJob ctor. | 88 // task runner is needed to avoid a DCHECK in BlobURLRequestJob ctor. |
| 53 return base::WrapUnique(new storage::BlobProtocolHandler( | 89 return base::WrapUnique(new storage::BlobProtocolHandler( |
| 54 blob_storage_context, NULL, base::ThreadTaskRunnerHandle::Get().get())); | 90 blob_storage_context, NULL, base::ThreadTaskRunnerHandle::Get().get())); |
| 55 } | 91 } |
| 56 | 92 |
| 57 class CacheStorageManagerTest : public testing::Test { | 93 class CacheStorageManagerTest : public testing::Test { |
| 58 public: | 94 public: |
| 59 CacheStorageManagerTest() | 95 CacheStorageManagerTest() |
| 60 : browser_thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP), | 96 : browser_thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP), |
| 61 blob_storage_context_(nullptr), | 97 blob_storage_context_(nullptr), |
| 62 callback_bool_(false), | 98 callback_bool_(false), |
| 63 callback_error_(CACHE_STORAGE_OK), | 99 callback_error_(CACHE_STORAGE_OK), |
| 64 origin1_("http://example1.com"), | 100 origin1_("http://example1.com"), |
| 65 origin2_("http://example2.com") {} | 101 origin2_("http://example2.com") {} |
| 66 | 102 |
| 67 void SetUp() override { | 103 void SetUp() override { |
| 104 base::FilePath temp_dir_path; |
| 105 const bool is_incognito = MemoryOnly(); |
| 106 if (!is_incognito) |
| 107 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 108 |
| 109 CreateStorageManager(); |
| 110 } |
| 111 |
| 112 void TearDown() override { DestroyStorageManager(); } |
| 113 |
| 114 virtual bool MemoryOnly() { return false; } |
| 115 |
| 116 void BoolAndErrorCallback(base::RunLoop* run_loop, |
| 117 bool value, |
| 118 CacheStorageError error) { |
| 119 callback_bool_ = value; |
| 120 callback_error_ = error; |
| 121 run_loop->Quit(); |
| 122 } |
| 123 |
| 124 void CacheAndErrorCallback( |
| 125 base::RunLoop* run_loop, |
| 126 std::unique_ptr<CacheStorageCacheHandle> cache_handle, |
| 127 CacheStorageError error) { |
| 128 callback_cache_handle_ = std::move(cache_handle); |
| 129 callback_error_ = error; |
| 130 run_loop->Quit(); |
| 131 } |
| 132 |
| 133 void CacheInfoCallback(base::RunLoop* run_loop, |
| 134 const std::list<CacheStorage::CacheInfo>& cache_info) { |
| 135 cache_info_ = cache_info; |
| 136 run_loop->Quit(); |
| 137 } |
| 138 |
| 139 void CachePutCallback(base::RunLoop* run_loop, CacheStorageError error) { |
| 140 callback_error_ = error; |
| 141 run_loop->Quit(); |
| 142 } |
| 143 |
| 144 void CacheMatchCallback( |
| 145 base::RunLoop* run_loop, |
| 146 CacheStorageError error, |
| 147 std::unique_ptr<ServiceWorkerResponse> response, |
| 148 std::unique_ptr<storage::BlobDataHandle> blob_data_handle) { |
| 149 callback_error_ = error; |
| 150 callback_cache_handle_response_ = std::move(response); |
| 151 callback_data_handle_ = std::move(blob_data_handle); |
| 152 run_loop->Quit(); |
| 153 } |
| 154 |
| 155 void CreateStorageManager() { |
| 68 ChromeBlobStorageContext* blob_storage_context( | 156 ChromeBlobStorageContext* blob_storage_context( |
| 69 ChromeBlobStorageContext::GetFor(&browser_context_)); | 157 ChromeBlobStorageContext::GetFor(&browser_context_)); |
| 70 // Wait for ChromeBlobStorageContext to finish initializing. | 158 // Wait for ChromeBlobStorageContext to finish initializing. |
| 71 base::RunLoop().RunUntilIdle(); | 159 base::RunLoop().RunUntilIdle(); |
| 72 | 160 |
| 73 blob_storage_context_ = blob_storage_context->context(); | 161 blob_storage_context_ = blob_storage_context->context(); |
| 74 | 162 |
| 75 url_request_job_factory_.reset(new net::URLRequestJobFactoryImpl); | 163 url_request_job_factory_.reset(new net::URLRequestJobFactoryImpl); |
| 76 url_request_job_factory_->SetProtocolHandler( | 164 url_request_job_factory_->SetProtocolHandler( |
| 77 "blob", CreateMockBlobProtocolHandler(blob_storage_context->context())); | 165 "blob", CreateMockBlobProtocolHandler(blob_storage_context->context())); |
| 78 | 166 |
| 79 net::URLRequestContext* url_request_context = | 167 net::URLRequestContext* url_request_context = |
| 80 BrowserContext::GetDefaultStoragePartition(&browser_context_)-> | 168 BrowserContext::GetDefaultStoragePartition(&browser_context_) |
| 81 GetURLRequestContext()->GetURLRequestContext(); | 169 ->GetURLRequestContext() |
| 170 ->GetURLRequestContext(); |
| 82 | 171 |
| 83 url_request_context->set_job_factory(url_request_job_factory_.get()); | 172 url_request_context->set_job_factory(url_request_job_factory_.get()); |
| 84 | 173 |
| 174 base::FilePath temp_dir_path; |
| 85 const bool is_incognito = MemoryOnly(); | 175 const bool is_incognito = MemoryOnly(); |
| 86 base::FilePath temp_dir_path; | 176 if (!is_incognito) |
| 87 if (!is_incognito) { | |
| 88 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | |
| 89 temp_dir_path = temp_dir_.GetPath(); | 177 temp_dir_path = temp_dir_.GetPath(); |
| 90 } | |
| 91 | 178 |
| 92 quota_policy_ = new MockSpecialStoragePolicy; | 179 quota_policy_ = new MockSpecialStoragePolicy; |
| 93 mock_quota_manager_ = new MockQuotaManager( | 180 mock_quota_manager_ = new MockQuotaManager( |
| 94 is_incognito, temp_dir_path, base::ThreadTaskRunnerHandle::Get().get(), | 181 is_incognito, temp_dir_path, base::ThreadTaskRunnerHandle::Get().get(), |
| 95 base::ThreadTaskRunnerHandle::Get().get(), quota_policy_.get()); | 182 base::ThreadTaskRunnerHandle::Get().get(), quota_policy_.get()); |
| 96 mock_quota_manager_->SetQuota( | 183 mock_quota_manager_->SetQuota( |
| 97 GURL(origin1_), storage::kStorageTypeTemporary, 1024 * 1024 * 100); | 184 GURL(origin1_), storage::kStorageTypeTemporary, 1024 * 1024 * 100); |
| 98 mock_quota_manager_->SetQuota( | 185 mock_quota_manager_->SetQuota( |
| 99 GURL(origin2_), storage::kStorageTypeTemporary, 1024 * 1024 * 100); | 186 GURL(origin2_), storage::kStorageTypeTemporary, 1024 * 1024 * 100); |
| 100 | 187 |
| 101 quota_manager_proxy_ = new MockQuotaManagerProxy( | 188 quota_manager_proxy_ = new MockQuotaManagerProxy( |
| 102 mock_quota_manager_.get(), base::ThreadTaskRunnerHandle::Get().get()); | 189 mock_quota_manager_.get(), base::ThreadTaskRunnerHandle::Get().get()); |
| 103 | 190 |
| 104 cache_manager_ = CacheStorageManager::Create( | 191 cache_manager_ = CacheStorageManager::Create( |
| 105 temp_dir_path, base::ThreadTaskRunnerHandle::Get(), | 192 temp_dir_path, base::ThreadTaskRunnerHandle::Get(), |
| 106 quota_manager_proxy_); | 193 quota_manager_proxy_); |
| 107 | 194 |
| 108 cache_manager_->SetBlobParametersForCache( | 195 cache_manager_->SetBlobParametersForCache( |
| 109 BrowserContext::GetDefaultStoragePartition(&browser_context_)-> | 196 BrowserContext::GetDefaultStoragePartition(&browser_context_) |
| 110 GetURLRequestContext(), | 197 ->GetURLRequestContext(), |
| 111 blob_storage_context->context()->AsWeakPtr()); | 198 blob_storage_context->context()->AsWeakPtr()); |
| 112 } | 199 } |
| 113 | 200 |
| 114 void TearDown() override { | 201 void DestroyStorageManager() { |
| 115 quota_manager_proxy_->SimulateQuotaManagerDestroyed(); | 202 quota_manager_proxy_->SimulateQuotaManagerDestroyed(); |
| 116 base::RunLoop().RunUntilIdle(); | 203 base::RunLoop().RunUntilIdle(); |
| 117 } | 204 quota_manager_proxy_ = nullptr; |
| 118 | 205 |
| 119 virtual bool MemoryOnly() { return false; } | 206 url_request_job_factory_.reset(); |
| 207 blob_storage_context_ = nullptr; |
| 120 | 208 |
| 121 void BoolAndErrorCallback(base::RunLoop* run_loop, | 209 quota_policy_ = nullptr; |
| 122 bool value, | 210 mock_quota_manager_ = nullptr; |
| 123 CacheStorageError error) { | 211 quota_manager_proxy_ = nullptr; |
| 124 callback_bool_ = value; | |
| 125 callback_error_ = error; | |
| 126 run_loop->Quit(); | |
| 127 } | |
| 128 | 212 |
| 129 void CacheAndErrorCallback( | 213 callback_cache_handle_ = nullptr; |
| 130 base::RunLoop* run_loop, | 214 callback_bool_ = false; |
| 131 std::unique_ptr<CacheStorageCacheHandle> cache_handle, | 215 callback_cache_handle_response_ = nullptr; |
| 132 CacheStorageError error) { | 216 callback_data_handle_ = nullptr; |
| 133 callback_cache_handle_ = std::move(cache_handle); | 217 cache_info_.clear(); |
| 134 callback_error_ = error; | 218 callback_all_origins_usage_.clear(); |
| 135 run_loop->Quit(); | |
| 136 } | |
| 137 | 219 |
| 138 void StringsCallback(base::RunLoop* run_loop, | 220 cache_manager_ = nullptr; |
| 139 const std::vector<std::string>& strings) { | |
| 140 callback_strings_ = strings; | |
| 141 run_loop->Quit(); | |
| 142 } | |
| 143 | |
| 144 void CachePutCallback(base::RunLoop* run_loop, CacheStorageError error) { | |
| 145 callback_error_ = error; | |
| 146 run_loop->Quit(); | |
| 147 } | |
| 148 | |
| 149 void CacheMatchCallback( | |
| 150 base::RunLoop* run_loop, | |
| 151 CacheStorageError error, | |
| 152 std::unique_ptr<ServiceWorkerResponse> response, | |
| 153 std::unique_ptr<storage::BlobDataHandle> blob_data_handle) { | |
| 154 callback_error_ = error; | |
| 155 callback_cache_handle_response_ = std::move(response); | |
| 156 callback_data_handle_ = std::move(blob_data_handle); | |
| 157 run_loop->Quit(); | |
| 158 } | 221 } |
| 159 | 222 |
| 160 bool Open(const GURL& origin, const std::string& cache_name) { | 223 bool Open(const GURL& origin, const std::string& cache_name) { |
| 161 base::RunLoop loop; | 224 base::RunLoop loop; |
| 162 cache_manager_->OpenCache( | 225 cache_manager_->OpenCache( |
| 163 origin, cache_name, | 226 origin, cache_name, |
| 164 base::Bind(&CacheStorageManagerTest::CacheAndErrorCallback, | 227 base::Bind(&CacheStorageManagerTest::CacheAndErrorCallback, |
| 165 base::Unretained(this), base::Unretained(&loop))); | 228 base::Unretained(this), base::Unretained(&loop))); |
| 166 loop.Run(); | 229 loop.Run(); |
| 167 | 230 |
| (...skipping 23 matching lines...) Expand all Loading... |
| 191 base::Bind(&CacheStorageManagerTest::BoolAndErrorCallback, | 254 base::Bind(&CacheStorageManagerTest::BoolAndErrorCallback, |
| 192 base::Unretained(this), base::Unretained(&loop))); | 255 base::Unretained(this), base::Unretained(&loop))); |
| 193 loop.Run(); | 256 loop.Run(); |
| 194 | 257 |
| 195 return callback_bool_; | 258 return callback_bool_; |
| 196 } | 259 } |
| 197 | 260 |
| 198 size_t Keys(const GURL& origin) { | 261 size_t Keys(const GURL& origin) { |
| 199 base::RunLoop loop; | 262 base::RunLoop loop; |
| 200 cache_manager_->EnumerateCaches( | 263 cache_manager_->EnumerateCaches( |
| 201 origin, base::Bind(&CacheStorageManagerTest::StringsCallback, | 264 origin, base::Bind(&CacheStorageManagerTest::CacheInfoCallback, |
| 202 base::Unretained(this), base::Unretained(&loop))); | 265 base::Unretained(this), base::Unretained(&loop))); |
| 203 loop.Run(); | 266 loop.Run(); |
| 204 return callback_strings_.size(); | 267 return cache_info_.size(); |
| 205 } | 268 } |
| 206 | 269 |
| 207 bool StorageMatch(const GURL& origin, | 270 bool StorageMatch(const GURL& origin, |
| 208 const std::string& cache_name, | 271 const std::string& cache_name, |
| 209 const GURL& url, | 272 const GURL& url, |
| 210 const CacheStorageCacheQueryParams& match_params = | 273 const CacheStorageCacheQueryParams& match_params = |
| 211 CacheStorageCacheQueryParams()) { | 274 CacheStorageCacheQueryParams()) { |
| 212 ServiceWorkerFetchRequest request; | 275 ServiceWorkerFetchRequest request; |
| 213 request.url = url; | 276 request.url = url; |
| 214 return StorageMatchWithRequest(origin, cache_name, request, match_params); | 277 return StorageMatchWithRequest(origin, cache_name, request, match_params); |
| (...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 386 scoped_refptr<MockSpecialStoragePolicy> quota_policy_; | 449 scoped_refptr<MockSpecialStoragePolicy> quota_policy_; |
| 387 scoped_refptr<MockQuotaManager> mock_quota_manager_; | 450 scoped_refptr<MockQuotaManager> mock_quota_manager_; |
| 388 scoped_refptr<MockQuotaManagerProxy> quota_manager_proxy_; | 451 scoped_refptr<MockQuotaManagerProxy> quota_manager_proxy_; |
| 389 std::unique_ptr<CacheStorageManager> cache_manager_; | 452 std::unique_ptr<CacheStorageManager> cache_manager_; |
| 390 | 453 |
| 391 std::unique_ptr<CacheStorageCacheHandle> callback_cache_handle_; | 454 std::unique_ptr<CacheStorageCacheHandle> callback_cache_handle_; |
| 392 int callback_bool_; | 455 int callback_bool_; |
| 393 CacheStorageError callback_error_; | 456 CacheStorageError callback_error_; |
| 394 std::unique_ptr<ServiceWorkerResponse> callback_cache_handle_response_; | 457 std::unique_ptr<ServiceWorkerResponse> callback_cache_handle_response_; |
| 395 std::unique_ptr<storage::BlobDataHandle> callback_data_handle_; | 458 std::unique_ptr<storage::BlobDataHandle> callback_data_handle_; |
| 396 std::vector<std::string> callback_strings_; | 459 std::list<CacheStorage::CacheInfo> cache_info_; |
| 397 | 460 |
| 398 const GURL origin1_; | 461 const GURL origin1_; |
| 399 const GURL origin2_; | 462 const GURL origin2_; |
| 400 | 463 |
| 401 int64_t callback_usage_; | 464 int64_t callback_usage_; |
| 402 std::vector<CacheStorageUsageInfo> callback_all_origins_usage_; | 465 std::vector<CacheStorageUsageInfo> callback_all_origins_usage_; |
| 403 | 466 |
| 404 private: | 467 private: |
| 405 DISALLOW_COPY_AND_ASSIGN(CacheStorageManagerTest); | 468 DISALLOW_COPY_AND_ASSIGN(CacheStorageManagerTest); |
| 406 }; | 469 }; |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 499 } | 562 } |
| 500 | 563 |
| 501 TEST_P(CacheStorageManagerTestP, SomeKeys) { | 564 TEST_P(CacheStorageManagerTestP, SomeKeys) { |
| 502 EXPECT_TRUE(Open(origin1_, "foo")); | 565 EXPECT_TRUE(Open(origin1_, "foo")); |
| 503 EXPECT_TRUE(Open(origin1_, "bar")); | 566 EXPECT_TRUE(Open(origin1_, "bar")); |
| 504 EXPECT_TRUE(Open(origin2_, "baz")); | 567 EXPECT_TRUE(Open(origin2_, "baz")); |
| 505 EXPECT_EQ(2u, Keys(origin1_)); | 568 EXPECT_EQ(2u, Keys(origin1_)); |
| 506 std::vector<std::string> expected_keys; | 569 std::vector<std::string> expected_keys; |
| 507 expected_keys.push_back("foo"); | 570 expected_keys.push_back("foo"); |
| 508 expected_keys.push_back("bar"); | 571 expected_keys.push_back("bar"); |
| 509 EXPECT_EQ(expected_keys, callback_strings_); | 572 EXPECT_EQ(expected_keys, GetCacheInfoNames(cache_info_)); |
| 510 EXPECT_EQ(1u, Keys(origin2_)); | 573 EXPECT_EQ(1u, Keys(origin2_)); |
| 511 EXPECT_STREQ("baz", callback_strings_[0].c_str()); | 574 EXPECT_STREQ("baz", cache_info_.front().name.c_str()); |
| 512 } | 575 } |
| 513 | 576 |
| 514 TEST_P(CacheStorageManagerTestP, DeletedKeysGone) { | 577 TEST_P(CacheStorageManagerTestP, DeletedKeysGone) { |
| 515 EXPECT_TRUE(Open(origin1_, "foo")); | 578 EXPECT_TRUE(Open(origin1_, "foo")); |
| 516 EXPECT_TRUE(Open(origin1_, "bar")); | 579 EXPECT_TRUE(Open(origin1_, "bar")); |
| 517 EXPECT_TRUE(Open(origin2_, "baz")); | 580 EXPECT_TRUE(Open(origin2_, "baz")); |
| 518 EXPECT_TRUE(Delete(origin1_, "bar")); | 581 EXPECT_TRUE(Delete(origin1_, "bar")); |
| 519 EXPECT_EQ(1u, Keys(origin1_)); | 582 EXPECT_EQ(1u, Keys(origin1_)); |
| 520 EXPECT_STREQ("foo", callback_strings_[0].c_str()); | 583 EXPECT_STREQ("foo", cache_info_.front().name.c_str()); |
| 521 } | 584 } |
| 522 | 585 |
| 523 TEST_P(CacheStorageManagerTestP, StorageMatchEntryExists) { | 586 TEST_P(CacheStorageManagerTestP, StorageMatchEntryExists) { |
| 524 EXPECT_TRUE(Open(origin1_, "foo")); | 587 EXPECT_TRUE(Open(origin1_, "foo")); |
| 525 EXPECT_TRUE(CachePut(callback_cache_handle_->value(), | 588 EXPECT_TRUE(CachePut(callback_cache_handle_->value(), |
| 526 GURL("http://example.com/foo"))); | 589 GURL("http://example.com/foo"))); |
| 527 EXPECT_TRUE(StorageMatch(origin1_, "foo", GURL("http://example.com/foo"))); | 590 EXPECT_TRUE(StorageMatch(origin1_, "foo", GURL("http://example.com/foo"))); |
| 528 } | 591 } |
| 529 | 592 |
| 530 TEST_P(CacheStorageManagerTestP, StorageMatchNoEntry) { | 593 TEST_P(CacheStorageManagerTestP, StorageMatchNoEntry) { |
| (...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 656 EXPECT_TRUE(StorageMatchAll(origin1_, GURL("http://example.com/foo"))); | 719 EXPECT_TRUE(StorageMatchAll(origin1_, GURL("http://example.com/foo"))); |
| 657 } | 720 } |
| 658 | 721 |
| 659 TEST_P(CacheStorageManagerTestP, Chinese) { | 722 TEST_P(CacheStorageManagerTestP, Chinese) { |
| 660 EXPECT_TRUE(Open(origin1_, "你好")); | 723 EXPECT_TRUE(Open(origin1_, "你好")); |
| 661 std::unique_ptr<CacheStorageCacheHandle> cache_handle = | 724 std::unique_ptr<CacheStorageCacheHandle> cache_handle = |
| 662 std::move(callback_cache_handle_); | 725 std::move(callback_cache_handle_); |
| 663 EXPECT_TRUE(Open(origin1_, "你好")); | 726 EXPECT_TRUE(Open(origin1_, "你好")); |
| 664 EXPECT_EQ(callback_cache_handle_->value(), cache_handle->value()); | 727 EXPECT_EQ(callback_cache_handle_->value(), cache_handle->value()); |
| 665 EXPECT_EQ(1u, Keys(origin1_)); | 728 EXPECT_EQ(1u, Keys(origin1_)); |
| 666 EXPECT_STREQ("你好", callback_strings_[0].c_str()); | 729 EXPECT_STREQ("你好", cache_info_.front().name.c_str()); |
| 667 } | 730 } |
| 668 | 731 |
| 669 TEST_F(CacheStorageManagerTest, EmptyKey) { | 732 TEST_F(CacheStorageManagerTest, EmptyKey) { |
| 670 EXPECT_TRUE(Open(origin1_, "")); | 733 EXPECT_TRUE(Open(origin1_, "")); |
| 671 std::unique_ptr<CacheStorageCacheHandle> cache_handle = | 734 std::unique_ptr<CacheStorageCacheHandle> cache_handle = |
| 672 std::move(callback_cache_handle_); | 735 std::move(callback_cache_handle_); |
| 673 EXPECT_TRUE(Open(origin1_, "")); | 736 EXPECT_TRUE(Open(origin1_, "")); |
| 674 EXPECT_EQ(cache_handle->value(), callback_cache_handle_->value()); | 737 EXPECT_EQ(cache_handle->value(), callback_cache_handle_->value()); |
| 675 EXPECT_EQ(1u, Keys(origin1_)); | 738 EXPECT_EQ(1u, Keys(origin1_)); |
| 676 EXPECT_STREQ("", callback_strings_[0].c_str()); | 739 EXPECT_STREQ("", cache_info_.front().name.c_str()); |
| 677 EXPECT_TRUE(Has(origin1_, "")); | 740 EXPECT_TRUE(Has(origin1_, "")); |
| 678 EXPECT_TRUE(Delete(origin1_, "")); | 741 EXPECT_TRUE(Delete(origin1_, "")); |
| 679 EXPECT_EQ(0u, Keys(origin1_)); | 742 EXPECT_EQ(0u, Keys(origin1_)); |
| 680 } | 743 } |
| 681 | 744 |
| 682 TEST_F(CacheStorageManagerTest, DataPersists) { | 745 TEST_F(CacheStorageManagerTest, DataPersists) { |
| 683 EXPECT_TRUE(Open(origin1_, "foo")); | 746 EXPECT_TRUE(Open(origin1_, "foo")); |
| 684 EXPECT_TRUE(Open(origin1_, "bar")); | 747 EXPECT_TRUE(Open(origin1_, "bar")); |
| 685 EXPECT_TRUE(Open(origin1_, "baz")); | 748 EXPECT_TRUE(Open(origin1_, "baz")); |
| 686 EXPECT_TRUE(Open(origin2_, "raz")); | 749 EXPECT_TRUE(Open(origin2_, "raz")); |
| 687 EXPECT_TRUE(Delete(origin1_, "bar")); | 750 EXPECT_TRUE(Delete(origin1_, "bar")); |
| 688 quota_manager_proxy_->SimulateQuotaManagerDestroyed(); | 751 quota_manager_proxy_->SimulateQuotaManagerDestroyed(); |
| 689 cache_manager_ = CacheStorageManager::Create(cache_manager_.get()); | 752 cache_manager_ = CacheStorageManager::Create(cache_manager_.get()); |
| 690 EXPECT_EQ(2u, Keys(origin1_)); | 753 EXPECT_EQ(2u, Keys(origin1_)); |
| 691 std::vector<std::string> expected_keys; | 754 std::vector<std::string> expected_keys; |
| 692 expected_keys.push_back("foo"); | 755 expected_keys.push_back("foo"); |
| 693 expected_keys.push_back("baz"); | 756 expected_keys.push_back("baz"); |
| 694 EXPECT_EQ(expected_keys, callback_strings_); | 757 EXPECT_EQ(expected_keys, GetCacheInfoNames(cache_info_)); |
| 695 } | 758 } |
| 696 | 759 |
| 697 TEST_F(CacheStorageManagerMemoryOnlyTest, DataLostWhenMemoryOnly) { | 760 TEST_F(CacheStorageManagerMemoryOnlyTest, DataLostWhenMemoryOnly) { |
| 698 EXPECT_TRUE(Open(origin1_, "foo")); | 761 EXPECT_TRUE(Open(origin1_, "foo")); |
| 699 EXPECT_TRUE(Open(origin2_, "baz")); | 762 EXPECT_TRUE(Open(origin2_, "baz")); |
| 700 quota_manager_proxy_->SimulateQuotaManagerDestroyed(); | 763 quota_manager_proxy_->SimulateQuotaManagerDestroyed(); |
| 701 cache_manager_ = CacheStorageManager::Create(cache_manager_.get()); | 764 cache_manager_ = CacheStorageManager::Create(cache_manager_.get()); |
| 702 EXPECT_EQ(0u, Keys(origin1_)); | 765 EXPECT_EQ(0u, Keys(origin1_)); |
| 703 } | 766 } |
| 704 | 767 |
| 705 TEST_F(CacheStorageManagerTest, BadCacheName) { | 768 TEST_F(CacheStorageManagerTest, BadCacheName) { |
| 706 // Since the implementation writes cache names to disk, ensure that we don't | 769 // Since the implementation writes cache names to disk, ensure that we don't |
| 707 // escape the directory. | 770 // escape the directory. |
| 708 const std::string bad_name = "../../../../../../../../../../../../../../foo"; | 771 const std::string bad_name = "../../../../../../../../../../../../../../foo"; |
| 709 EXPECT_TRUE(Open(origin1_, bad_name)); | 772 EXPECT_TRUE(Open(origin1_, bad_name)); |
| 710 EXPECT_EQ(1u, Keys(origin1_)); | 773 EXPECT_EQ(1u, Keys(origin1_)); |
| 711 EXPECT_STREQ(bad_name.c_str(), callback_strings_[0].c_str()); | 774 EXPECT_STREQ(bad_name.c_str(), cache_info_.front().name.c_str()); |
| 712 } | 775 } |
| 713 | 776 |
| 714 TEST_F(CacheStorageManagerTest, BadOriginName) { | 777 TEST_F(CacheStorageManagerTest, BadOriginName) { |
| 715 // Since the implementation writes origin names to disk, ensure that we don't | 778 // Since the implementation writes origin names to disk, ensure that we don't |
| 716 // escape the directory. | 779 // escape the directory. |
| 717 GURL bad_origin("http://../../../../../../../../../../../../../../foo"); | 780 GURL bad_origin("http://../../../../../../../../../../../../../../foo"); |
| 718 EXPECT_TRUE(Open(bad_origin, "foo")); | 781 EXPECT_TRUE(Open(bad_origin, "foo")); |
| 719 EXPECT_EQ(1u, Keys(bad_origin)); | 782 EXPECT_EQ(1u, Keys(bad_origin)); |
| 720 EXPECT_STREQ("foo", callback_strings_[0].c_str()); | 783 EXPECT_STREQ("foo", cache_info_.front().name.c_str()); |
| 721 } | 784 } |
| 722 | 785 |
| 723 // With a persistent cache if the client drops its reference to a | 786 // With a persistent cache if the client drops its reference to a |
| 724 // CacheStorageCache it should be deleted. | 787 // CacheStorageCache it should be deleted. |
| 725 TEST_F(CacheStorageManagerTest, DropReference) { | 788 TEST_F(CacheStorageManagerTest, DropReference) { |
| 726 EXPECT_TRUE(Open(origin1_, "foo")); | 789 EXPECT_TRUE(Open(origin1_, "foo")); |
| 727 base::WeakPtr<CacheStorageCache> cache = | 790 base::WeakPtr<CacheStorageCache> cache = |
| 728 callback_cache_handle_->value()->AsWeakPtr(); | 791 callback_cache_handle_->value()->AsWeakPtr(); |
| 729 // Run a cache operation to ensure that the cache has finished initializing so | 792 // Run a cache operation to ensure that the cache has finished initializing so |
| 730 // that when the handle is dropped it can close immediately. | 793 // that when the handle is dropped it can close immediately. |
| (...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 853 | 916 |
| 854 if (MemoryOnly()) { | 917 if (MemoryOnly()) { |
| 855 EXPECT_TRUE(usage[origin1_index].last_modified.is_null()); | 918 EXPECT_TRUE(usage[origin1_index].last_modified.is_null()); |
| 856 EXPECT_TRUE(usage[origin2_index].last_modified.is_null()); | 919 EXPECT_TRUE(usage[origin2_index].last_modified.is_null()); |
| 857 } else { | 920 } else { |
| 858 EXPECT_FALSE(usage[origin1_index].last_modified.is_null()); | 921 EXPECT_FALSE(usage[origin1_index].last_modified.is_null()); |
| 859 EXPECT_FALSE(usage[origin2_index].last_modified.is_null()); | 922 EXPECT_FALSE(usage[origin2_index].last_modified.is_null()); |
| 860 } | 923 } |
| 861 } | 924 } |
| 862 | 925 |
| 926 TEST_P(CacheStorageManagerTestP, GetAllOriginsUsageWithOldIndex) { |
| 927 // in-memory caches aren't persisted, so skip those. |
| 928 if (MemoryOnly()) |
| 929 return; |
| 930 |
| 931 // Write a single value (V1) to the cache. |
| 932 const GURL kFooURL = origin1_.Resolve("foo"); |
| 933 const std::string kCacheName = "foo"; |
| 934 EXPECT_TRUE(Open(origin1_, kCacheName)); |
| 935 std::unique_ptr<CacheStorageCacheHandle> original_handle = |
| 936 std::move(callback_cache_handle_); |
| 937 |
| 938 EXPECT_TRUE(CachePut(original_handle->value(), kFooURL)); |
| 939 int64_t cache_size_v1 = Size(origin1_); |
| 940 base::FilePath storage_dir = original_handle->value()->path().DirName(); |
| 941 original_handle.release(); |
| 942 EXPECT_GE(cache_size_v1, 0); |
| 943 |
| 944 // Close the caches and cache manager. |
| 945 DestroyStorageManager(); |
| 946 |
| 947 // Save a copy of the V1 index. |
| 948 EXPECT_TRUE(IsIndexFileCurrent(storage_dir)); |
| 949 base::FilePath index_path = storage_dir.AppendASCII("index.txt"); |
| 950 EXPECT_TRUE(base::PathExists(index_path)); |
| 951 base::FilePath backup_index_path = storage_dir.AppendASCII("index.txt.bak"); |
| 952 EXPECT_TRUE(base::CopyFile(index_path, backup_index_path)); |
| 953 |
| 954 // Create a new CacheStorageManager that hasn't yet loaded the origin. |
| 955 CreateStorageManager(); |
| 956 quota_manager_proxy_->SimulateQuotaManagerDestroyed(); |
| 957 cache_manager_ = CacheStorageManager::Create(cache_manager_.get()); |
| 958 |
| 959 // Create a second value (V2) in the cache. |
| 960 EXPECT_TRUE(Open(origin1_, kCacheName)); |
| 961 original_handle = std::move(callback_cache_handle_); |
| 962 const GURL kBarURL = origin1_.Resolve("bar"); |
| 963 EXPECT_TRUE(CachePut(original_handle->value(), kBarURL)); |
| 964 original_handle.release(); |
| 965 |
| 966 std::vector<CacheStorageUsageInfo> usage = GetAllOriginsUsage(); |
| 967 ASSERT_EQ(1ULL, usage.size()); |
| 968 int64_t usage_before_close = usage[0].total_size_bytes; |
| 969 EXPECT_GT(usage_before_close, 0); |
| 970 |
| 971 // Close the caches and cache manager. |
| 972 DestroyStorageManager(); |
| 973 |
| 974 // Restore the index to the V1 state. Make the access/mod times of index file |
| 975 // "older" than the other directories in the store to trigger size |
| 976 // recalculation. |
| 977 EXPECT_TRUE(IsIndexFileCurrent(storage_dir)); |
| 978 EXPECT_TRUE(base::CopyFile(backup_index_path, index_path)); |
| 979 base::Time t = base::Time::Now() - base::TimeDelta::FromHours(1); |
| 980 EXPECT_TRUE(base::TouchFile(index_path, t, t)); |
| 981 EXPECT_FALSE(IsIndexFileCurrent(storage_dir)); |
| 982 |
| 983 CreateStorageManager(); |
| 984 usage = GetAllOriginsUsage(); |
| 985 ASSERT_EQ(1ULL, usage.size()); |
| 986 |
| 987 EXPECT_EQ(usage_before_close, usage[0].total_size_bytes); |
| 988 |
| 989 EXPECT_FALSE(usage[0].last_modified.is_null()); |
| 990 } |
| 991 |
| 992 TEST_P(CacheStorageManagerTestP, GetOriginSizeWithOldIndex) { |
| 993 // in-memory caches aren't persisted, so skip those. |
| 994 if (MemoryOnly()) |
| 995 return; |
| 996 |
| 997 // Write a single value (V1) to the cache. |
| 998 const GURL kFooURL = origin1_.Resolve("foo"); |
| 999 const std::string kCacheName = "foo"; |
| 1000 EXPECT_TRUE(Open(origin1_, kCacheName)); |
| 1001 std::unique_ptr<CacheStorageCacheHandle> original_handle = |
| 1002 std::move(callback_cache_handle_); |
| 1003 |
| 1004 EXPECT_TRUE(CachePut(original_handle->value(), kFooURL)); |
| 1005 int64_t cache_size_v1 = Size(origin1_); |
| 1006 base::FilePath storage_dir = original_handle->value()->path().DirName(); |
| 1007 original_handle.release(); |
| 1008 EXPECT_GE(cache_size_v1, 0); |
| 1009 |
| 1010 // Close the caches and cache manager. |
| 1011 DestroyStorageManager(); |
| 1012 |
| 1013 // Save a copy of the V1 index. |
| 1014 EXPECT_TRUE(IsIndexFileCurrent(storage_dir)); |
| 1015 base::FilePath index_path = storage_dir.AppendASCII("index.txt"); |
| 1016 EXPECT_TRUE(base::PathExists(index_path)); |
| 1017 base::FilePath backup_index_path = storage_dir.AppendASCII("index.txt.bak"); |
| 1018 EXPECT_TRUE(base::CopyFile(index_path, backup_index_path)); |
| 1019 |
| 1020 // Create a new CacheStorageManager that hasn't yet loaded the origin. |
| 1021 CreateStorageManager(); |
| 1022 quota_manager_proxy_->SimulateQuotaManagerDestroyed(); |
| 1023 cache_manager_ = CacheStorageManager::Create(cache_manager_.get()); |
| 1024 |
| 1025 // Reopen the cache and write a second value (V2). |
| 1026 EXPECT_TRUE(Open(origin1_, kCacheName)); |
| 1027 original_handle = std::move(callback_cache_handle_); |
| 1028 const GURL kBarURL = origin1_.Resolve("bar"); |
| 1029 EXPECT_TRUE(CachePut(original_handle->value(), kBarURL)); |
| 1030 original_handle.release(); |
| 1031 int64_t cache_size_v2 = GetSizeThenCloseAllCaches(origin1_); |
| 1032 EXPECT_GE(cache_size_v2, 0); |
| 1033 |
| 1034 // Close the caches and cache manager. |
| 1035 DestroyStorageManager(); |
| 1036 |
| 1037 // Restore the index to the V1 state. |
| 1038 EXPECT_TRUE(IsIndexFileCurrent(storage_dir)); |
| 1039 EXPECT_TRUE(base::CopyFile(backup_index_path, index_path)); |
| 1040 |
| 1041 // Make the access/mod times of index file "older" than the other files in the |
| 1042 // cache to trigger size recalculation. |
| 1043 base::Time t = base::Time::Now() - base::TimeDelta::FromHours(1); |
| 1044 EXPECT_TRUE(base::TouchFile(index_path, t, t)); |
| 1045 EXPECT_FALSE(IsIndexFileCurrent(storage_dir)); |
| 1046 |
| 1047 // Reopen the cache and ensure the size is correct for the V2 value. |
| 1048 CreateStorageManager(); |
| 1049 EXPECT_TRUE(Open(origin1_, kCacheName)); |
| 1050 EXPECT_EQ(cache_size_v2, Size(origin1_)); |
| 1051 } |
| 1052 |
| 863 TEST_P(CacheStorageManagerTestP, GetSizeThenCloseAllCaches) { | 1053 TEST_P(CacheStorageManagerTestP, GetSizeThenCloseAllCaches) { |
| 864 EXPECT_TRUE(Open(origin1_, "foo")); | 1054 EXPECT_TRUE(Open(origin1_, "foo")); |
| 865 EXPECT_TRUE(CachePut(callback_cache_handle_->value(), | 1055 EXPECT_TRUE(CachePut(callback_cache_handle_->value(), |
| 866 GURL("http://example.com/foo"))); | 1056 GURL("http://example.com/foo"))); |
| 867 EXPECT_TRUE(CachePut(callback_cache_handle_->value(), | 1057 EXPECT_TRUE(CachePut(callback_cache_handle_->value(), |
| 868 GURL("http://example.com/foo2"))); | 1058 GURL("http://example.com/foo2"))); |
| 869 EXPECT_TRUE(Open(origin1_, "bar")); | 1059 EXPECT_TRUE(Open(origin1_, "bar")); |
| 870 EXPECT_TRUE(CachePut(callback_cache_handle_->value(), | 1060 EXPECT_TRUE(CachePut(callback_cache_handle_->value(), |
| 871 GURL("http://example.com/bar"))); | 1061 GURL("http://example.com/bar"))); |
| 872 | 1062 |
| (...skipping 364 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1237 | 1427 |
| 1238 INSTANTIATE_TEST_CASE_P(CacheStorageManagerTests, | 1428 INSTANTIATE_TEST_CASE_P(CacheStorageManagerTests, |
| 1239 CacheStorageManagerTestP, | 1429 CacheStorageManagerTestP, |
| 1240 ::testing::Values(false, true)); | 1430 ::testing::Values(false, true)); |
| 1241 | 1431 |
| 1242 INSTANTIATE_TEST_CASE_P(CacheStorageQuotaClientTests, | 1432 INSTANTIATE_TEST_CASE_P(CacheStorageQuotaClientTests, |
| 1243 CacheStorageQuotaClientTestP, | 1433 CacheStorageQuotaClientTestP, |
| 1244 ::testing::Values(false, true)); | 1434 ::testing::Values(false, true)); |
| 1245 | 1435 |
| 1246 } // namespace content | 1436 } // namespace content |
| OLD | NEW |