 Chromium Code Reviews
 Chromium Code Reviews Issue 2416713002:
  Write out CacheStorageCache size to index file.  (Closed)
    
  
    Issue 2416713002:
  Write out CacheStorageCache size to index file.  (Closed) 
  | 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 void CopyCacheStorageIndex(CacheStorageIndex* dest, | |
| 75 const CacheStorageIndex& src) { | |
| 76 DCHECK_EQ(0U, dest->num_entries()); | |
| 77 for (const auto& cache_metadata : src.ordered_cache_metadata()) | |
| 78 dest->Insert(cache_metadata); | |
| 79 } | |
| 80 | |
| 81 } // anonymous namespace | |
| 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 if (!MemoryOnly()) | |
| 106 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | |
| 107 | |
| 108 CreateStorageManager(); | |
| 109 } | |
| 110 | |
| 111 void TearDown() override { DestroyStorageManager(); } | |
| 112 | |
| 113 virtual bool MemoryOnly() { return false; } | |
| 114 | |
| 115 void BoolCallback(base::RunLoop* run_loop, bool value) { | |
| 116 callback_bool_ = value; | |
| 117 run_loop->Quit(); | |
| 118 } | |
| 119 | |
| 120 void BoolAndErrorCallback(base::RunLoop* run_loop, | |
| 121 bool value, | |
| 122 CacheStorageError error) { | |
| 123 callback_bool_ = value; | |
| 124 callback_error_ = error; | |
| 125 run_loop->Quit(); | |
| 126 } | |
| 127 | |
| 128 void CacheAndErrorCallback( | |
| 129 base::RunLoop* run_loop, | |
| 130 std::unique_ptr<CacheStorageCacheHandle> cache_handle, | |
| 131 CacheStorageError error) { | |
| 132 callback_cache_handle_ = std::move(cache_handle); | |
| 133 callback_error_ = error; | |
| 134 run_loop->Quit(); | |
| 135 } | |
| 136 | |
| 137 void CacheMetadataCallback(base::RunLoop* run_loop, | |
| 138 const CacheStorageIndex& cache_index) { | |
| 139 callback_cache_index_ = CacheStorageIndex(); | |
| 140 CopyCacheStorageIndex(&callback_cache_index_, cache_index); | |
| 141 run_loop->Quit(); | |
| 142 } | |
| 143 | |
| 144 const std::string& GetFirstIndexName() const { | |
| 145 return callback_cache_index_.ordered_cache_metadata().front().name; | |
| 146 } | |
| 147 | |
| 148 std::vector<std::string> GetIndexNames() const { | |
| 149 std::vector<std::string> cache_names; | |
| 150 for (const auto& metadata : callback_cache_index_.ordered_cache_metadata()) | |
| 151 cache_names.push_back(metadata.name); | |
| 152 return cache_names; | |
| 153 } | |
| 154 | |
| 155 void CachePutCallback(base::RunLoop* run_loop, CacheStorageError error) { | |
| 156 callback_error_ = error; | |
| 157 run_loop->Quit(); | |
| 158 } | |
| 159 | |
| 160 void CacheMatchCallback( | |
| 161 base::RunLoop* run_loop, | |
| 162 CacheStorageError error, | |
| 163 std::unique_ptr<ServiceWorkerResponse> response, | |
| 164 std::unique_ptr<storage::BlobDataHandle> blob_data_handle) { | |
| 165 callback_error_ = error; | |
| 166 callback_cache_handle_response_ = std::move(response); | |
| 167 callback_data_handle_ = std::move(blob_data_handle); | |
| 168 run_loop->Quit(); | |
| 169 } | |
| 170 | |
| 171 void CreateStorageManager() { | |
| 68 ChromeBlobStorageContext* blob_storage_context( | 172 ChromeBlobStorageContext* blob_storage_context( | 
| 69 ChromeBlobStorageContext::GetFor(&browser_context_)); | 173 ChromeBlobStorageContext::GetFor(&browser_context_)); | 
| 70 // Wait for ChromeBlobStorageContext to finish initializing. | 174 // Wait for ChromeBlobStorageContext to finish initializing. | 
| 71 base::RunLoop().RunUntilIdle(); | 175 base::RunLoop().RunUntilIdle(); | 
| 72 | 176 | 
| 73 blob_storage_context_ = blob_storage_context->context(); | 177 blob_storage_context_ = blob_storage_context->context(); | 
| 74 | 178 | 
| 75 url_request_job_factory_.reset(new net::URLRequestJobFactoryImpl); | 179 url_request_job_factory_.reset(new net::URLRequestJobFactoryImpl); | 
| 76 url_request_job_factory_->SetProtocolHandler( | 180 url_request_job_factory_->SetProtocolHandler( | 
| 77 "blob", CreateMockBlobProtocolHandler(blob_storage_context->context())); | 181 "blob", CreateMockBlobProtocolHandler(blob_storage_context->context())); | 
| 78 | 182 | 
| 79 net::URLRequestContext* url_request_context = | 183 net::URLRequestContext* url_request_context = | 
| 80 BrowserContext::GetDefaultStoragePartition(&browser_context_)-> | 184 BrowserContext::GetDefaultStoragePartition(&browser_context_)-> | 
| 81 GetURLRequestContext()->GetURLRequestContext(); | 185 GetURLRequestContext()->GetURLRequestContext(); | 
| 82 | 186 | 
| 83 url_request_context->set_job_factory(url_request_job_factory_.get()); | 187 url_request_context->set_job_factory(url_request_job_factory_.get()); | 
| 84 | 188 | 
| 85 const bool is_incognito = MemoryOnly(); | |
| 86 base::FilePath temp_dir_path; | 189 base::FilePath temp_dir_path; | 
| 87 if (!is_incognito) { | 190 if (!MemoryOnly()) | 
| 88 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | |
| 89 temp_dir_path = temp_dir_.GetPath(); | 191 temp_dir_path = temp_dir_.GetPath(); | 
| 90 } | |
| 91 | 192 | 
| 92 quota_policy_ = new MockSpecialStoragePolicy; | 193 quota_policy_ = new MockSpecialStoragePolicy; | 
| 93 mock_quota_manager_ = new MockQuotaManager( | 194 mock_quota_manager_ = new MockQuotaManager( | 
| 94 is_incognito, temp_dir_path, base::ThreadTaskRunnerHandle::Get().get(), | 195 MemoryOnly(), temp_dir_path, base::ThreadTaskRunnerHandle::Get().get(), | 
| 95 base::ThreadTaskRunnerHandle::Get().get(), quota_policy_.get()); | 196 base::ThreadTaskRunnerHandle::Get().get(), quota_policy_.get()); | 
| 96 mock_quota_manager_->SetQuota( | 197 mock_quota_manager_->SetQuota( | 
| 97 GURL(origin1_), storage::kStorageTypeTemporary, 1024 * 1024 * 100); | 198 GURL(origin1_), storage::kStorageTypeTemporary, 1024 * 1024 * 100); | 
| 98 mock_quota_manager_->SetQuota( | 199 mock_quota_manager_->SetQuota( | 
| 99 GURL(origin2_), storage::kStorageTypeTemporary, 1024 * 1024 * 100); | 200 GURL(origin2_), storage::kStorageTypeTemporary, 1024 * 1024 * 100); | 
| 100 | 201 | 
| 101 quota_manager_proxy_ = new MockQuotaManagerProxy( | 202 quota_manager_proxy_ = new MockQuotaManagerProxy( | 
| 102 mock_quota_manager_.get(), base::ThreadTaskRunnerHandle::Get().get()); | 203 mock_quota_manager_.get(), base::ThreadTaskRunnerHandle::Get().get()); | 
| 103 | 204 | 
| 104 cache_manager_ = CacheStorageManager::Create( | 205 cache_manager_ = CacheStorageManager::Create( | 
| 105 temp_dir_path, base::ThreadTaskRunnerHandle::Get(), | 206 temp_dir_path, base::ThreadTaskRunnerHandle::Get(), | 
| 106 quota_manager_proxy_); | 207 quota_manager_proxy_); | 
| 107 | 208 | 
| 108 cache_manager_->SetBlobParametersForCache( | 209 cache_manager_->SetBlobParametersForCache( | 
| 109 BrowserContext::GetDefaultStoragePartition(&browser_context_)-> | 210 BrowserContext::GetDefaultStoragePartition(&browser_context_) | 
| 110 GetURLRequestContext(), | 211 ->GetURLRequestContext(), | 
| 111 blob_storage_context->context()->AsWeakPtr()); | 212 blob_storage_context->context()->AsWeakPtr()); | 
| 112 } | 213 } | 
| 113 | 214 | 
| 114 void TearDown() override { | 215 bool FlushCacheStorageIndex(const GURL& origin) { | 
| 115 quota_manager_proxy_->SimulateQuotaManagerDestroyed(); | 216 callback_bool_ = false; | 
| 116 base::RunLoop().RunUntilIdle(); | 217 base::RunLoop loop; | 
| 218 bool write_was_scheduled = | |
| 219 CacheStorageForOrigin(origin)->InitiateScheduledIndexWriteForTest( | |
| 220 base::Bind(&CacheStorageManagerTest::BoolCallback, | |
| 221 base::Unretained(this), &loop)); | |
| 222 loop.Run(); | |
| 223 DCHECK(callback_bool_); | |
| 224 return write_was_scheduled; | |
| 117 } | 225 } | 
| 118 | 226 | 
| 119 virtual bool MemoryOnly() { return false; } | 227 void DestroyStorageManager() { | 
| 228 if (quota_manager_proxy_) | |
| 229 quota_manager_proxy_->SimulateQuotaManagerDestroyed(); | |
| 230 base::RunLoop().RunUntilIdle(); | |
| 231 quota_manager_proxy_ = nullptr; | |
| 120 | 232 | 
| 121 void BoolAndErrorCallback(base::RunLoop* run_loop, | 233 url_request_job_factory_.reset(); | 
| 122 bool value, | 234 blob_storage_context_ = nullptr; | 
| 123 CacheStorageError error) { | |
| 124 callback_bool_ = value; | |
| 125 callback_error_ = error; | |
| 126 run_loop->Quit(); | |
| 127 } | |
| 128 | 235 | 
| 129 void CacheAndErrorCallback( | 236 quota_policy_ = nullptr; | 
| 130 base::RunLoop* run_loop, | 237 mock_quota_manager_ = nullptr; | 
| 131 std::unique_ptr<CacheStorageCacheHandle> cache_handle, | |
| 132 CacheStorageError error) { | |
| 133 callback_cache_handle_ = std::move(cache_handle); | |
| 134 callback_error_ = error; | |
| 135 run_loop->Quit(); | |
| 136 } | |
| 137 | 238 | 
| 138 void StringsCallback(base::RunLoop* run_loop, | 239 callback_cache_handle_ = nullptr; | 
| 139 const std::vector<std::string>& strings) { | 240 callback_bool_ = false; | 
| 140 callback_strings_ = strings; | 241 callback_cache_handle_response_ = nullptr; | 
| 141 run_loop->Quit(); | 242 callback_data_handle_ = nullptr; | 
| 142 } | 243 callback_cache_index_ = CacheStorageIndex(); | 
| 244 callback_all_origins_usage_.clear(); | |
| 143 | 245 | 
| 144 void CachePutCallback(base::RunLoop* run_loop, CacheStorageError error) { | 246 cache_manager_ = nullptr; | 
| 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 } | 247 } | 
| 159 | 248 | 
| 160 bool Open(const GURL& origin, const std::string& cache_name) { | 249 bool Open(const GURL& origin, const std::string& cache_name) { | 
| 161 base::RunLoop loop; | 250 base::RunLoop loop; | 
| 162 cache_manager_->OpenCache( | 251 cache_manager_->OpenCache( | 
| 163 origin, cache_name, | 252 origin, cache_name, | 
| 164 base::Bind(&CacheStorageManagerTest::CacheAndErrorCallback, | 253 base::Bind(&CacheStorageManagerTest::CacheAndErrorCallback, | 
| 165 base::Unretained(this), base::Unretained(&loop))); | 254 base::Unretained(this), base::Unretained(&loop))); | 
| 166 loop.Run(); | 255 loop.Run(); | 
| 167 | 256 | 
| (...skipping 23 matching lines...) Expand all Loading... | |
| 191 base::Bind(&CacheStorageManagerTest::BoolAndErrorCallback, | 280 base::Bind(&CacheStorageManagerTest::BoolAndErrorCallback, | 
| 192 base::Unretained(this), base::Unretained(&loop))); | 281 base::Unretained(this), base::Unretained(&loop))); | 
| 193 loop.Run(); | 282 loop.Run(); | 
| 194 | 283 | 
| 195 return callback_bool_; | 284 return callback_bool_; | 
| 196 } | 285 } | 
| 197 | 286 | 
| 198 size_t Keys(const GURL& origin) { | 287 size_t Keys(const GURL& origin) { | 
| 199 base::RunLoop loop; | 288 base::RunLoop loop; | 
| 200 cache_manager_->EnumerateCaches( | 289 cache_manager_->EnumerateCaches( | 
| 201 origin, base::Bind(&CacheStorageManagerTest::StringsCallback, | 290 origin, base::Bind(&CacheStorageManagerTest::CacheMetadataCallback, | 
| 202 base::Unretained(this), base::Unretained(&loop))); | 291 base::Unretained(this), base::Unretained(&loop))); | 
| 203 loop.Run(); | 292 loop.Run(); | 
| 204 return callback_strings_.size(); | 293 return callback_cache_index_.num_entries(); | 
| 205 } | 294 } | 
| 206 | 295 | 
| 207 bool StorageMatch(const GURL& origin, | 296 bool StorageMatch(const GURL& origin, | 
| 208 const std::string& cache_name, | 297 const std::string& cache_name, | 
| 209 const GURL& url, | 298 const GURL& url, | 
| 210 const CacheStorageCacheQueryParams& match_params = | 299 const CacheStorageCacheQueryParams& match_params = | 
| 211 CacheStorageCacheQueryParams()) { | 300 CacheStorageCacheQueryParams()) { | 
| 212 ServiceWorkerFetchRequest request; | 301 ServiceWorkerFetchRequest request; | 
| 213 request.url = url; | 302 request.url = url; | 
| 214 return StorageMatchWithRequest(origin, cache_name, request, match_params); | 303 return StorageMatchWithRequest(origin, cache_name, request, match_params); | 
| (...skipping 176 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 391 scoped_refptr<MockSpecialStoragePolicy> quota_policy_; | 480 scoped_refptr<MockSpecialStoragePolicy> quota_policy_; | 
| 392 scoped_refptr<MockQuotaManager> mock_quota_manager_; | 481 scoped_refptr<MockQuotaManager> mock_quota_manager_; | 
| 393 scoped_refptr<MockQuotaManagerProxy> quota_manager_proxy_; | 482 scoped_refptr<MockQuotaManagerProxy> quota_manager_proxy_; | 
| 394 std::unique_ptr<CacheStorageManager> cache_manager_; | 483 std::unique_ptr<CacheStorageManager> cache_manager_; | 
| 395 | 484 | 
| 396 std::unique_ptr<CacheStorageCacheHandle> callback_cache_handle_; | 485 std::unique_ptr<CacheStorageCacheHandle> callback_cache_handle_; | 
| 397 int callback_bool_; | 486 int callback_bool_; | 
| 398 CacheStorageError callback_error_; | 487 CacheStorageError callback_error_; | 
| 399 std::unique_ptr<ServiceWorkerResponse> callback_cache_handle_response_; | 488 std::unique_ptr<ServiceWorkerResponse> callback_cache_handle_response_; | 
| 400 std::unique_ptr<storage::BlobDataHandle> callback_data_handle_; | 489 std::unique_ptr<storage::BlobDataHandle> callback_data_handle_; | 
| 401 std::vector<std::string> callback_strings_; | 490 CacheStorageIndex callback_cache_index_; | 
| 402 | 491 | 
| 403 const GURL origin1_; | 492 const GURL origin1_; | 
| 404 const GURL origin2_; | 493 const GURL origin2_; | 
| 405 | 494 | 
| 406 int64_t callback_usage_; | 495 int64_t callback_usage_; | 
| 407 std::vector<CacheStorageUsageInfo> callback_all_origins_usage_; | 496 std::vector<CacheStorageUsageInfo> callback_all_origins_usage_; | 
| 408 | 497 | 
| 409 private: | 498 private: | 
| 410 DISALLOW_COPY_AND_ASSIGN(CacheStorageManagerTest); | 499 DISALLOW_COPY_AND_ASSIGN(CacheStorageManagerTest); | 
| 411 }; | 500 }; | 
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 504 } | 593 } | 
| 505 | 594 | 
| 506 TEST_P(CacheStorageManagerTestP, SomeKeys) { | 595 TEST_P(CacheStorageManagerTestP, SomeKeys) { | 
| 507 EXPECT_TRUE(Open(origin1_, "foo")); | 596 EXPECT_TRUE(Open(origin1_, "foo")); | 
| 508 EXPECT_TRUE(Open(origin1_, "bar")); | 597 EXPECT_TRUE(Open(origin1_, "bar")); | 
| 509 EXPECT_TRUE(Open(origin2_, "baz")); | 598 EXPECT_TRUE(Open(origin2_, "baz")); | 
| 510 EXPECT_EQ(2u, Keys(origin1_)); | 599 EXPECT_EQ(2u, Keys(origin1_)); | 
| 511 std::vector<std::string> expected_keys; | 600 std::vector<std::string> expected_keys; | 
| 512 expected_keys.push_back("foo"); | 601 expected_keys.push_back("foo"); | 
| 513 expected_keys.push_back("bar"); | 602 expected_keys.push_back("bar"); | 
| 514 EXPECT_EQ(expected_keys, callback_strings_); | 603 EXPECT_EQ(expected_keys, GetIndexNames()); | 
| 515 EXPECT_EQ(1u, Keys(origin2_)); | 604 EXPECT_EQ(1u, Keys(origin2_)); | 
| 516 EXPECT_STREQ("baz", callback_strings_[0].c_str()); | 605 EXPECT_STREQ("baz", GetFirstIndexName().c_str()); | 
| 517 } | 606 } | 
| 518 | 607 | 
| 519 TEST_P(CacheStorageManagerTestP, DeletedKeysGone) { | 608 TEST_P(CacheStorageManagerTestP, DeletedKeysGone) { | 
| 520 EXPECT_TRUE(Open(origin1_, "foo")); | 609 EXPECT_TRUE(Open(origin1_, "foo")); | 
| 521 EXPECT_TRUE(Open(origin1_, "bar")); | 610 EXPECT_TRUE(Open(origin1_, "bar")); | 
| 522 EXPECT_TRUE(Open(origin2_, "baz")); | 611 EXPECT_TRUE(Open(origin2_, "baz")); | 
| 523 EXPECT_TRUE(Delete(origin1_, "bar")); | 612 EXPECT_TRUE(Delete(origin1_, "bar")); | 
| 524 EXPECT_EQ(1u, Keys(origin1_)); | 613 EXPECT_EQ(1u, Keys(origin1_)); | 
| 525 EXPECT_STREQ("foo", callback_strings_[0].c_str()); | 614 EXPECT_STREQ("foo", GetFirstIndexName().c_str()); | 
| 526 } | 615 } | 
| 527 | 616 | 
| 528 TEST_P(CacheStorageManagerTestP, StorageMatchEntryExists) { | 617 TEST_P(CacheStorageManagerTestP, StorageMatchEntryExists) { | 
| 529 EXPECT_TRUE(Open(origin1_, "foo")); | 618 EXPECT_TRUE(Open(origin1_, "foo")); | 
| 530 EXPECT_TRUE(CachePut(callback_cache_handle_->value(), | 619 EXPECT_TRUE(CachePut(callback_cache_handle_->value(), | 
| 531 GURL("http://example.com/foo"))); | 620 GURL("http://example.com/foo"))); | 
| 532 EXPECT_TRUE(StorageMatch(origin1_, "foo", GURL("http://example.com/foo"))); | 621 EXPECT_TRUE(StorageMatch(origin1_, "foo", GURL("http://example.com/foo"))); | 
| 533 } | 622 } | 
| 534 | 623 | 
| 535 TEST_P(CacheStorageManagerTestP, StorageMatchNoEntry) { | 624 TEST_P(CacheStorageManagerTestP, StorageMatchNoEntry) { | 
| (...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 661 EXPECT_TRUE(StorageMatchAll(origin1_, GURL("http://example.com/foo"))); | 750 EXPECT_TRUE(StorageMatchAll(origin1_, GURL("http://example.com/foo"))); | 
| 662 } | 751 } | 
| 663 | 752 | 
| 664 TEST_P(CacheStorageManagerTestP, Chinese) { | 753 TEST_P(CacheStorageManagerTestP, Chinese) { | 
| 665 EXPECT_TRUE(Open(origin1_, "ä½ å¥½")); | 754 EXPECT_TRUE(Open(origin1_, "ä½ å¥½")); | 
| 666 std::unique_ptr<CacheStorageCacheHandle> cache_handle = | 755 std::unique_ptr<CacheStorageCacheHandle> cache_handle = | 
| 667 std::move(callback_cache_handle_); | 756 std::move(callback_cache_handle_); | 
| 668 EXPECT_TRUE(Open(origin1_, "ä½ å¥½")); | 757 EXPECT_TRUE(Open(origin1_, "ä½ å¥½")); | 
| 669 EXPECT_EQ(callback_cache_handle_->value(), cache_handle->value()); | 758 EXPECT_EQ(callback_cache_handle_->value(), cache_handle->value()); | 
| 670 EXPECT_EQ(1u, Keys(origin1_)); | 759 EXPECT_EQ(1u, Keys(origin1_)); | 
| 671 EXPECT_STREQ("ä½ å¥½", callback_strings_[0].c_str()); | 760 EXPECT_STREQ("ä½ å¥½", GetFirstIndexName().c_str()); | 
| 672 } | 761 } | 
| 673 | 762 | 
| 674 TEST_F(CacheStorageManagerTest, EmptyKey) { | 763 TEST_F(CacheStorageManagerTest, EmptyKey) { | 
| 675 EXPECT_TRUE(Open(origin1_, "")); | 764 EXPECT_TRUE(Open(origin1_, "")); | 
| 676 std::unique_ptr<CacheStorageCacheHandle> cache_handle = | 765 std::unique_ptr<CacheStorageCacheHandle> cache_handle = | 
| 677 std::move(callback_cache_handle_); | 766 std::move(callback_cache_handle_); | 
| 678 EXPECT_TRUE(Open(origin1_, "")); | 767 EXPECT_TRUE(Open(origin1_, "")); | 
| 679 EXPECT_EQ(cache_handle->value(), callback_cache_handle_->value()); | 768 EXPECT_EQ(cache_handle->value(), callback_cache_handle_->value()); | 
| 680 EXPECT_EQ(1u, Keys(origin1_)); | 769 EXPECT_EQ(1u, Keys(origin1_)); | 
| 681 EXPECT_STREQ("", callback_strings_[0].c_str()); | 770 EXPECT_STREQ("", GetFirstIndexName().c_str()); | 
| 682 EXPECT_TRUE(Has(origin1_, "")); | 771 EXPECT_TRUE(Has(origin1_, "")); | 
| 683 EXPECT_TRUE(Delete(origin1_, "")); | 772 EXPECT_TRUE(Delete(origin1_, "")); | 
| 684 EXPECT_EQ(0u, Keys(origin1_)); | 773 EXPECT_EQ(0u, Keys(origin1_)); | 
| 685 } | 774 } | 
| 686 | 775 | 
| 687 TEST_F(CacheStorageManagerTest, DataPersists) { | 776 TEST_F(CacheStorageManagerTest, DataPersists) { | 
| 688 EXPECT_TRUE(Open(origin1_, "foo")); | 777 EXPECT_TRUE(Open(origin1_, "foo")); | 
| 689 EXPECT_TRUE(Open(origin1_, "bar")); | 778 EXPECT_TRUE(Open(origin1_, "bar")); | 
| 690 EXPECT_TRUE(Open(origin1_, "baz")); | 779 EXPECT_TRUE(Open(origin1_, "baz")); | 
| 691 EXPECT_TRUE(Open(origin2_, "raz")); | 780 EXPECT_TRUE(Open(origin2_, "raz")); | 
| 692 EXPECT_TRUE(Delete(origin1_, "bar")); | 781 EXPECT_TRUE(Delete(origin1_, "bar")); | 
| 693 quota_manager_proxy_->SimulateQuotaManagerDestroyed(); | 782 quota_manager_proxy_->SimulateQuotaManagerDestroyed(); | 
| 694 cache_manager_ = CacheStorageManager::Create(cache_manager_.get()); | 783 cache_manager_ = CacheStorageManager::Create(cache_manager_.get()); | 
| 695 EXPECT_EQ(2u, Keys(origin1_)); | 784 EXPECT_EQ(2u, Keys(origin1_)); | 
| 696 std::vector<std::string> expected_keys; | 785 std::vector<std::string> expected_keys; | 
| 697 expected_keys.push_back("foo"); | 786 expected_keys.push_back("foo"); | 
| 698 expected_keys.push_back("baz"); | 787 expected_keys.push_back("baz"); | 
| 699 EXPECT_EQ(expected_keys, callback_strings_); | 788 EXPECT_EQ(expected_keys, GetIndexNames()); | 
| 700 } | 789 } | 
| 701 | 790 | 
| 702 TEST_F(CacheStorageManagerMemoryOnlyTest, DataLostWhenMemoryOnly) { | 791 TEST_F(CacheStorageManagerMemoryOnlyTest, DataLostWhenMemoryOnly) { | 
| 703 EXPECT_TRUE(Open(origin1_, "foo")); | 792 EXPECT_TRUE(Open(origin1_, "foo")); | 
| 704 EXPECT_TRUE(Open(origin2_, "baz")); | 793 EXPECT_TRUE(Open(origin2_, "baz")); | 
| 705 quota_manager_proxy_->SimulateQuotaManagerDestroyed(); | 794 quota_manager_proxy_->SimulateQuotaManagerDestroyed(); | 
| 706 cache_manager_ = CacheStorageManager::Create(cache_manager_.get()); | 795 cache_manager_ = CacheStorageManager::Create(cache_manager_.get()); | 
| 707 EXPECT_EQ(0u, Keys(origin1_)); | 796 EXPECT_EQ(0u, Keys(origin1_)); | 
| 708 } | 797 } | 
| 709 | 798 | 
| 710 TEST_F(CacheStorageManagerTest, BadCacheName) { | 799 TEST_F(CacheStorageManagerTest, BadCacheName) { | 
| 711 // Since the implementation writes cache names to disk, ensure that we don't | 800 // Since the implementation writes cache names to disk, ensure that we don't | 
| 712 // escape the directory. | 801 // escape the directory. | 
| 713 const std::string bad_name = "../../../../../../../../../../../../../../foo"; | 802 const std::string bad_name = "../../../../../../../../../../../../../../foo"; | 
| 714 EXPECT_TRUE(Open(origin1_, bad_name)); | 803 EXPECT_TRUE(Open(origin1_, bad_name)); | 
| 715 EXPECT_EQ(1u, Keys(origin1_)); | 804 EXPECT_EQ(1u, Keys(origin1_)); | 
| 716 EXPECT_STREQ(bad_name.c_str(), callback_strings_[0].c_str()); | 805 EXPECT_STREQ(bad_name.c_str(), GetFirstIndexName().c_str()); | 
| 717 } | 806 } | 
| 718 | 807 | 
| 719 TEST_F(CacheStorageManagerTest, BadOriginName) { | 808 TEST_F(CacheStorageManagerTest, BadOriginName) { | 
| 720 // Since the implementation writes origin names to disk, ensure that we don't | 809 // Since the implementation writes origin names to disk, ensure that we don't | 
| 721 // escape the directory. | 810 // escape the directory. | 
| 722 GURL bad_origin("http://../../../../../../../../../../../../../../foo"); | 811 GURL bad_origin("http://../../../../../../../../../../../../../../foo"); | 
| 723 EXPECT_TRUE(Open(bad_origin, "foo")); | 812 EXPECT_TRUE(Open(bad_origin, "foo")); | 
| 724 EXPECT_EQ(1u, Keys(bad_origin)); | 813 EXPECT_EQ(1u, Keys(bad_origin)); | 
| 725 EXPECT_STREQ("foo", callback_strings_[0].c_str()); | 814 EXPECT_STREQ("foo", GetFirstIndexName().c_str()); | 
| 726 } | 815 } | 
| 727 | 816 | 
| 728 // With a persistent cache if the client drops its reference to a | 817 // With a persistent cache if the client drops its reference to a | 
| 729 // CacheStorageCache it should be deleted. | 818 // CacheStorageCache it should be deleted. | 
| 730 TEST_F(CacheStorageManagerTest, DropReference) { | 819 TEST_F(CacheStorageManagerTest, DropReference) { | 
| 731 EXPECT_TRUE(Open(origin1_, "foo")); | 820 EXPECT_TRUE(Open(origin1_, "foo")); | 
| 732 base::WeakPtr<CacheStorageCache> cache = | 821 base::WeakPtr<CacheStorageCache> cache = | 
| 733 callback_cache_handle_->value()->AsWeakPtr(); | 822 callback_cache_handle_->value()->AsWeakPtr(); | 
| 734 // Run a cache operation to ensure that the cache has finished initializing so | 823 // Run a cache operation to ensure that the cache has finished initializing so | 
| 735 // that when the handle is dropped it can close immediately. | 824 // that when the handle is dropped it can close immediately. | 
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 769 | 858 | 
| 770 EXPECT_FALSE(CacheMatch(new_handle->value(), kFooURL)); | 859 EXPECT_FALSE(CacheMatch(new_handle->value(), kFooURL)); | 
| 771 EXPECT_FALSE(CacheMatch(new_handle->value(), kBarURL)); | 860 EXPECT_FALSE(CacheMatch(new_handle->value(), kBarURL)); | 
| 772 EXPECT_TRUE(CacheMatch(new_handle->value(), kBazURL)); | 861 EXPECT_TRUE(CacheMatch(new_handle->value(), kBazURL)); | 
| 773 | 862 | 
| 774 EXPECT_TRUE(CacheMatch(original_handle->value(), kFooURL)); | 863 EXPECT_TRUE(CacheMatch(original_handle->value(), kFooURL)); | 
| 775 EXPECT_TRUE(CacheMatch(original_handle->value(), kBarURL)); | 864 EXPECT_TRUE(CacheMatch(original_handle->value(), kBarURL)); | 
| 776 EXPECT_FALSE(CacheMatch(original_handle->value(), kBazURL)); | 865 EXPECT_FALSE(CacheMatch(original_handle->value(), kBazURL)); | 
| 777 } | 866 } | 
| 778 | 867 | 
| 868 // Deleted caches can still be modified, but all changes will eventually be | |
| 869 // thrown away when all references are released. | |
| 870 TEST_F(CacheStorageManagerTest, DeletedCacheIgnoredInIndex) { | |
| 871 const GURL kFooURL("http://example.com/foo"); | |
| 872 const GURL kBarURL("http://example.com/bar"); | |
| 873 const GURL kBazURL("http://example.com/baz"); | |
| 874 const std::string kCacheName = "foo"; | |
| 875 | |
| 876 EXPECT_TRUE(Open(origin1_, kCacheName)); | |
| 877 auto original_handle = std::move(callback_cache_handle_); | |
| 878 EXPECT_TRUE(CachePut(original_handle->value(), kFooURL)); | |
| 879 EXPECT_TRUE(Delete(origin1_, kCacheName)); | |
| 880 | |
| 881 // Now a second cache using the same name, but with different data. | |
| 882 EXPECT_TRUE(Open(origin1_, kCacheName)); | |
| 883 auto new_handle = std::move(callback_cache_handle_); | |
| 884 EXPECT_TRUE(CachePut(new_handle->value(), kFooURL)); | |
| 885 EXPECT_TRUE(CachePut(new_handle->value(), kBarURL)); | |
| 886 EXPECT_TRUE(CachePut(new_handle->value(), kBazURL)); | |
| 887 auto new_cache_size = Size(origin1_); | |
| 888 | |
| 889 // Now modify the first cache. | |
| 890 EXPECT_TRUE(CachePut(original_handle->value(), kBarURL)); | |
| 891 | |
| 892 // Now deref both caches, and recreate the storage manager. | |
| 893 original_handle = nullptr; | |
| 894 new_handle = nullptr; | |
| 895 EXPECT_TRUE(FlushCacheStorageIndex(origin1_)); | |
| 896 DestroyStorageManager(); | |
| 897 CreateStorageManager(); | |
| 898 | |
| 899 EXPECT_TRUE(Open(origin1_, kCacheName)); | |
| 900 EXPECT_EQ(new_cache_size, Size(origin1_)); | |
| 901 } | |
| 902 | |
| 903 TEST_F(CacheStorageManagerTest, CacheSizeCorrectAfterReopen) { | |
| 904 const GURL kFooURL("http://example.com/foo"); | |
| 905 const std::string kCacheName = "foo"; | |
| 906 | |
| 907 EXPECT_TRUE(Open(origin1_, kCacheName)); | |
| 908 auto original_handle = std::move(callback_cache_handle_); | |
| 909 EXPECT_TRUE(CachePut(original_handle->value(), kFooURL)); | |
| 910 auto size_before_close = Size(origin1_); | |
| 911 EXPECT_GT(size_before_close, 0); | |
| 912 | |
| 913 DestroyStorageManager(); | |
| 914 CreateStorageManager(); | |
| 915 | |
| 916 EXPECT_TRUE(Open(origin1_, kCacheName)); | |
| 917 EXPECT_EQ(size_before_close, Size(origin1_)); | |
| 918 } | |
| 919 | |
| 779 // With a memory cache the cache can't be freed from memory until the client | 920 // With a memory cache the cache can't be freed from memory until the client | 
| 780 // calls delete. | 921 // calls delete. | 
| 781 TEST_F(CacheStorageManagerMemoryOnlyTest, MemoryLosesReferenceOnlyAfterDelete) { | 922 TEST_F(CacheStorageManagerMemoryOnlyTest, MemoryLosesReferenceOnlyAfterDelete) { | 
| 782 EXPECT_TRUE(Open(origin1_, "foo")); | 923 EXPECT_TRUE(Open(origin1_, "foo")); | 
| 783 base::WeakPtr<CacheStorageCache> cache = | 924 base::WeakPtr<CacheStorageCache> cache = | 
| 784 callback_cache_handle_->value()->AsWeakPtr(); | 925 callback_cache_handle_->value()->AsWeakPtr(); | 
| 785 callback_cache_handle_ = nullptr; | 926 callback_cache_handle_ = nullptr; | 
| 786 EXPECT_TRUE(cache); | 927 EXPECT_TRUE(cache); | 
| 787 EXPECT_TRUE(Delete(origin1_, "foo")); | 928 EXPECT_TRUE(Delete(origin1_, "foo")); | 
| 788 base::RunLoop().RunUntilIdle(); | 929 base::RunLoop().RunUntilIdle(); | 
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 858 | 999 | 
| 859 if (MemoryOnly()) { | 1000 if (MemoryOnly()) { | 
| 860 EXPECT_TRUE(usage[origin1_index].last_modified.is_null()); | 1001 EXPECT_TRUE(usage[origin1_index].last_modified.is_null()); | 
| 861 EXPECT_TRUE(usage[origin2_index].last_modified.is_null()); | 1002 EXPECT_TRUE(usage[origin2_index].last_modified.is_null()); | 
| 862 } else { | 1003 } else { | 
| 863 EXPECT_FALSE(usage[origin1_index].last_modified.is_null()); | 1004 EXPECT_FALSE(usage[origin1_index].last_modified.is_null()); | 
| 864 EXPECT_FALSE(usage[origin2_index].last_modified.is_null()); | 1005 EXPECT_FALSE(usage[origin2_index].last_modified.is_null()); | 
| 865 } | 1006 } | 
| 866 } | 1007 } | 
| 867 | 1008 | 
| 1009 TEST_F(CacheStorageManagerTest, GetAllOriginsUsageWithOldIndex) { | |
| 1010 // Write a single value (V1) to the cache. | |
| 1011 const GURL kFooURL = origin1_.Resolve("foo"); | |
| 1012 const std::string kCacheName = "foo"; | |
| 1013 EXPECT_TRUE(Open(origin1_, kCacheName)); | |
| 1014 std::unique_ptr<CacheStorageCacheHandle> original_handle = | |
| 1015 std::move(callback_cache_handle_); | |
| 1016 | |
| 1017 EXPECT_TRUE(CachePut(original_handle->value(), kFooURL)); | |
| 1018 int64_t cache_size_v1 = Size(origin1_); | |
| 1019 base::FilePath storage_dir = original_handle->value()->path().DirName(); | |
| 1020 original_handle = nullptr; | |
| 
jkarlin
2016/12/22 13:17:59
Are you saying that this change fixed something? A
 
cmumford
2016/12/22 14:22:11
No, reset() is equivalent, but release just makes
 
jkarlin
2016/12/22 14:27:20
Ah, right. I was thinking about reset().
 | |
| 1021 EXPECT_GE(cache_size_v1, 0); | |
| 1022 | |
| 1023 // Close the caches and cache manager. | |
| 1024 EXPECT_TRUE(FlushCacheStorageIndex(origin1_)); | |
| 1025 DestroyStorageManager(); | |
| 1026 | |
| 1027 // Save a copy of the V1 index. | |
| 1028 EXPECT_TRUE(IsIndexFileCurrent(storage_dir)); | |
| 1029 base::FilePath index_path = storage_dir.AppendASCII("index.txt"); | |
| 1030 EXPECT_TRUE(base::PathExists(index_path)); | |
| 1031 base::FilePath backup_index_path = storage_dir.AppendASCII("index.txt.bak"); | |
| 1032 EXPECT_TRUE(base::CopyFile(index_path, backup_index_path)); | |
| 1033 | |
| 1034 // Create a new CacheStorageManager that hasn't yet loaded the origin. | |
| 1035 CreateStorageManager(); | |
| 1036 quota_manager_proxy_->SimulateQuotaManagerDestroyed(); | |
| 1037 cache_manager_ = CacheStorageManager::Create(cache_manager_.get()); | |
| 1038 | |
| 1039 // Create a second value (V2) in the cache. | |
| 1040 EXPECT_TRUE(Open(origin1_, kCacheName)); | |
| 1041 original_handle = std::move(callback_cache_handle_); | |
| 1042 const GURL kBarURL = origin1_.Resolve("bar"); | |
| 1043 EXPECT_TRUE(CachePut(original_handle->value(), kBarURL)); | |
| 1044 original_handle = nullptr; | |
| 1045 | |
| 1046 std::vector<CacheStorageUsageInfo> usage = GetAllOriginsUsage(); | |
| 1047 ASSERT_EQ(1ULL, usage.size()); | |
| 1048 int64_t usage_before_close = usage[0].total_size_bytes; | |
| 1049 EXPECT_GT(usage_before_close, 0); | |
| 1050 | |
| 1051 // Close the caches and cache manager. | |
| 1052 DestroyStorageManager(); | |
| 1053 | |
| 1054 // Restore the index to the V1 state. Make the access/mod times of index file | |
| 1055 // older than the other directories in the store to trigger size | |
| 1056 // recalculation. | |
| 1057 EXPECT_TRUE(base::CopyFile(backup_index_path, index_path)); | |
| 1058 base::Time t = base::Time::Now() - base::TimeDelta::FromHours(1); | |
| 1059 EXPECT_TRUE(base::TouchFile(index_path, t, t)); | |
| 1060 EXPECT_FALSE(IsIndexFileCurrent(storage_dir)); | |
| 1061 | |
| 1062 CreateStorageManager(); | |
| 1063 usage = GetAllOriginsUsage(); | |
| 1064 ASSERT_EQ(1ULL, usage.size()); | |
| 1065 | |
| 1066 EXPECT_EQ(usage_before_close, usage[0].total_size_bytes); | |
| 1067 | |
| 1068 EXPECT_FALSE(usage[0].last_modified.is_null()); | |
| 1069 } | |
| 1070 | |
| 1071 TEST_F(CacheStorageManagerTest, GetOriginSizeWithOldIndex) { | |
| 1072 // Write a single value (V1) to the cache. | |
| 1073 const GURL kFooURL = origin1_.Resolve("foo"); | |
| 1074 const std::string kCacheName = "foo"; | |
| 1075 EXPECT_TRUE(Open(origin1_, kCacheName)); | |
| 1076 std::unique_ptr<CacheStorageCacheHandle> original_handle = | |
| 1077 std::move(callback_cache_handle_); | |
| 1078 | |
| 1079 EXPECT_TRUE(CachePut(original_handle->value(), kFooURL)); | |
| 1080 int64_t cache_size_v1 = Size(origin1_); | |
| 1081 base::FilePath storage_dir = original_handle->value()->path().DirName(); | |
| 1082 original_handle = nullptr; | |
| 1083 EXPECT_GE(cache_size_v1, 0); | |
| 1084 | |
| 1085 // Close the caches and cache manager. | |
| 1086 EXPECT_TRUE(FlushCacheStorageIndex(origin1_)); | |
| 1087 DestroyStorageManager(); | |
| 1088 | |
| 1089 // Save a copy of the V1 index. | |
| 1090 EXPECT_TRUE(IsIndexFileCurrent(storage_dir)); | |
| 1091 base::FilePath index_path = storage_dir.AppendASCII("index.txt"); | |
| 1092 EXPECT_TRUE(base::PathExists(index_path)); | |
| 1093 base::FilePath backup_index_path = storage_dir.AppendASCII("index.txt.bak"); | |
| 1094 EXPECT_TRUE(base::CopyFile(index_path, backup_index_path)); | |
| 1095 | |
| 1096 // Create a new CacheStorageManager that hasn't yet loaded the origin. | |
| 1097 CreateStorageManager(); | |
| 1098 quota_manager_proxy_->SimulateQuotaManagerDestroyed(); | |
| 1099 cache_manager_ = CacheStorageManager::Create(cache_manager_.get()); | |
| 1100 | |
| 1101 // Reopen the cache and write a second value (V2). | |
| 1102 EXPECT_TRUE(Open(origin1_, kCacheName)); | |
| 1103 original_handle = std::move(callback_cache_handle_); | |
| 1104 const GURL kBarURL = origin1_.Resolve("bar"); | |
| 1105 EXPECT_TRUE(CachePut(original_handle->value(), kBarURL)); | |
| 1106 original_handle = nullptr; | |
| 1107 int64_t cache_size_v2 = Size(origin1_); | |
| 1108 EXPECT_GE(cache_size_v2, 0); | |
| 1109 | |
| 1110 // Close the caches and cache manager. | |
| 1111 DestroyStorageManager(); | |
| 1112 | |
| 1113 // Restore the index to the V1 state. | |
| 1114 EXPECT_TRUE(base::CopyFile(backup_index_path, index_path)); | |
| 1115 | |
| 1116 // Make the access/mod times of index file older than the other files in the | |
| 1117 // cache to trigger size recalculation. | |
| 1118 base::Time t = base::Time::Now() - base::TimeDelta::FromHours(1); | |
| 1119 EXPECT_TRUE(base::TouchFile(index_path, t, t)); | |
| 1120 EXPECT_FALSE(IsIndexFileCurrent(storage_dir)); | |
| 1121 | |
| 1122 // Reopen the cache and ensure the size is correct for the V2 value. | |
| 1123 CreateStorageManager(); | |
| 1124 EXPECT_TRUE(Open(origin1_, kCacheName)); | |
| 1125 EXPECT_EQ(cache_size_v2, Size(origin1_)); | |
| 1126 } | |
| 1127 | |
| 868 TEST_P(CacheStorageManagerTestP, GetSizeThenCloseAllCaches) { | 1128 TEST_P(CacheStorageManagerTestP, GetSizeThenCloseAllCaches) { | 
| 869 EXPECT_TRUE(Open(origin1_, "foo")); | 1129 EXPECT_TRUE(Open(origin1_, "foo")); | 
| 870 EXPECT_TRUE(CachePut(callback_cache_handle_->value(), | 1130 EXPECT_TRUE(CachePut(callback_cache_handle_->value(), | 
| 871 GURL("http://example.com/foo"))); | 1131 GURL("http://example.com/foo"))); | 
| 872 EXPECT_TRUE(CachePut(callback_cache_handle_->value(), | 1132 EXPECT_TRUE(CachePut(callback_cache_handle_->value(), | 
| 873 GURL("http://example.com/foo2"))); | 1133 GURL("http://example.com/foo2"))); | 
| 874 EXPECT_TRUE(Open(origin1_, "bar")); | 1134 EXPECT_TRUE(Open(origin1_, "bar")); | 
| 875 EXPECT_TRUE(CachePut(callback_cache_handle_->value(), | 1135 EXPECT_TRUE(CachePut(callback_cache_handle_->value(), | 
| 876 GURL("http://example.com/bar"))); | 1136 GURL("http://example.com/bar"))); | 
| 877 | 1137 | 
| (...skipping 364 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1242 | 1502 | 
| 1243 INSTANTIATE_TEST_CASE_P(CacheStorageManagerTests, | 1503 INSTANTIATE_TEST_CASE_P(CacheStorageManagerTests, | 
| 1244 CacheStorageManagerTestP, | 1504 CacheStorageManagerTestP, | 
| 1245 ::testing::Values(false, true)); | 1505 ::testing::Values(false, true)); | 
| 1246 | 1506 | 
| 1247 INSTANTIATE_TEST_CASE_P(CacheStorageQuotaClientTests, | 1507 INSTANTIATE_TEST_CASE_P(CacheStorageQuotaClientTests, | 
| 1248 CacheStorageQuotaClientTestP, | 1508 CacheStorageQuotaClientTestP, | 
| 1249 ::testing::Values(false, true)); | 1509 ::testing::Values(false, true)); | 
| 1250 | 1510 | 
| 1251 } // namespace content | 1511 } // namespace content | 
| OLD | NEW |