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