| 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 <utility> | 10 #include <utility> |
| 11 | 11 |
| 12 #include "base/files/file_path.h" | 12 #include "base/files/file_path.h" |
| 13 #include "base/files/file_util.h" | 13 #include "base/files/file_util.h" |
| 14 #include "base/files/scoped_temp_dir.h" | 14 #include "base/files/scoped_temp_dir.h" |
| 15 #include "base/guid.h" | 15 #include "base/guid.h" |
| 16 #include "base/macros.h" | 16 #include "base/macros.h" |
| 17 #include "base/memory/ptr_util.h" | 17 #include "base/memory/ptr_util.h" |
| 18 #include "base/run_loop.h" | 18 #include "base/run_loop.h" |
| 19 #include "base/sha1.h" | 19 #include "base/sha1.h" |
| 20 #include "base/stl_util.h" | 20 #include "base/stl_util.h" |
| 21 #include "base/strings/string_number_conversions.h" | 21 #include "base/strings/string_number_conversions.h" |
| 22 #include "base/threading/thread_task_runner_handle.h" | 22 #include "base/threading/thread_task_runner_handle.h" |
| 23 #include "content/browser/blob_storage/chrome_blob_storage_context.h" | 23 #include "content/browser/blob_storage/chrome_blob_storage_context.h" |
| 24 #include "content/browser/cache_storage/cache_storage.pb.h" | 24 #include "content/browser/cache_storage/cache_storage.pb.h" |
| 25 #include "content/browser/cache_storage/cache_storage_cache_handle.h" |
| 25 #include "content/browser/cache_storage/cache_storage_quota_client.h" | 26 #include "content/browser/cache_storage/cache_storage_quota_client.h" |
| 26 #include "content/browser/quota/mock_quota_manager_proxy.h" | 27 #include "content/browser/quota/mock_quota_manager_proxy.h" |
| 27 #include "content/public/browser/browser_thread.h" | 28 #include "content/public/browser/browser_thread.h" |
| 28 #include "content/public/browser/cache_storage_usage_info.h" | 29 #include "content/public/browser/cache_storage_usage_info.h" |
| 29 #include "content/public/browser/storage_partition.h" | 30 #include "content/public/browser/storage_partition.h" |
| 30 #include "content/public/test/mock_special_storage_policy.h" | 31 #include "content/public/test/mock_special_storage_policy.h" |
| 31 #include "content/public/test/test_browser_context.h" | 32 #include "content/public/test/test_browser_context.h" |
| 32 #include "content/public/test/test_browser_thread_bundle.h" | 33 #include "content/public/test/test_browser_thread_bundle.h" |
| 33 #include "net/url_request/url_request_context.h" | 34 #include "net/url_request/url_request_context.h" |
| 34 #include "net/url_request/url_request_context_getter.h" | 35 #include "net/url_request/url_request_context_getter.h" |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 114 virtual bool MemoryOnly() { return false; } | 115 virtual bool MemoryOnly() { return false; } |
| 115 | 116 |
| 116 void BoolAndErrorCallback(base::RunLoop* run_loop, | 117 void BoolAndErrorCallback(base::RunLoop* run_loop, |
| 117 bool value, | 118 bool value, |
| 118 CacheStorageError error) { | 119 CacheStorageError error) { |
| 119 callback_bool_ = value; | 120 callback_bool_ = value; |
| 120 callback_error_ = error; | 121 callback_error_ = error; |
| 121 run_loop->Quit(); | 122 run_loop->Quit(); |
| 122 } | 123 } |
| 123 | 124 |
| 124 void CacheAndErrorCallback(base::RunLoop* run_loop, | 125 void CacheAndErrorCallback( |
| 125 scoped_refptr<CacheStorageCache> cache, | 126 base::RunLoop* run_loop, |
| 126 CacheStorageError error) { | 127 std::unique_ptr<CacheStorageCacheHandle> cache_handle, |
| 127 callback_cache_ = std::move(cache); | 128 CacheStorageError error) { |
| 129 callback_cache_handle_ = std::move(cache_handle); |
| 128 callback_error_ = error; | 130 callback_error_ = error; |
| 129 run_loop->Quit(); | 131 run_loop->Quit(); |
| 130 } | 132 } |
| 131 | 133 |
| 132 void StringsAndErrorCallback(base::RunLoop* run_loop, | 134 void StringsAndErrorCallback(base::RunLoop* run_loop, |
| 133 const std::vector<std::string>& strings, | 135 const std::vector<std::string>& strings, |
| 134 CacheStorageError error) { | 136 CacheStorageError error) { |
| 135 callback_strings_ = strings; | 137 callback_strings_ = strings; |
| 136 callback_error_ = error; | 138 callback_error_ = error; |
| 137 run_loop->Quit(); | 139 run_loop->Quit(); |
| 138 } | 140 } |
| 139 | 141 |
| 140 void CachePutCallback(base::RunLoop* run_loop, CacheStorageError error) { | 142 void CachePutCallback(base::RunLoop* run_loop, CacheStorageError error) { |
| 141 callback_error_ = error; | 143 callback_error_ = error; |
| 142 run_loop->Quit(); | 144 run_loop->Quit(); |
| 143 } | 145 } |
| 144 | 146 |
| 145 void CacheMatchCallback( | 147 void CacheMatchCallback( |
| 146 base::RunLoop* run_loop, | 148 base::RunLoop* run_loop, |
| 147 CacheStorageError error, | 149 CacheStorageError error, |
| 148 std::unique_ptr<ServiceWorkerResponse> response, | 150 std::unique_ptr<ServiceWorkerResponse> response, |
| 149 std::unique_ptr<storage::BlobDataHandle> blob_data_handle) { | 151 std::unique_ptr<storage::BlobDataHandle> blob_data_handle) { |
| 150 callback_error_ = error; | 152 callback_error_ = error; |
| 151 callback_cache_response_ = std::move(response); | 153 callback_cache_handle_response_ = std::move(response); |
| 152 callback_data_handle_ = std::move(blob_data_handle); | 154 callback_data_handle_ = std::move(blob_data_handle); |
| 153 run_loop->Quit(); | 155 run_loop->Quit(); |
| 154 } | 156 } |
| 155 | 157 |
| 156 bool Open(const GURL& origin, const std::string& cache_name) { | 158 bool Open(const GURL& origin, const std::string& cache_name) { |
| 157 base::RunLoop loop; | 159 base::RunLoop loop; |
| 158 cache_manager_->OpenCache( | 160 cache_manager_->OpenCache( |
| 159 origin, cache_name, | 161 origin, cache_name, |
| 160 base::Bind(&CacheStorageManagerTest::CacheAndErrorCallback, | 162 base::Bind(&CacheStorageManagerTest::CacheAndErrorCallback, |
| 161 base::Unretained(this), base::Unretained(&loop))); | 163 base::Unretained(this), base::Unretained(&loop))); |
| 162 loop.Run(); | 164 loop.Run(); |
| 163 | 165 |
| 164 bool error = callback_error_ != CACHE_STORAGE_OK; | 166 bool error = callback_error_ != CACHE_STORAGE_OK; |
| 165 if (error) | 167 if (error) |
| 166 EXPECT_TRUE(!callback_cache_.get()); | 168 EXPECT_TRUE(!callback_cache_handle_); |
| 167 else | 169 else |
| 168 EXPECT_TRUE(callback_cache_.get()); | 170 EXPECT_TRUE(callback_cache_handle_); |
| 169 return !error; | 171 return !error; |
| 170 } | 172 } |
| 171 | 173 |
| 172 bool Has(const GURL& origin, const std::string& cache_name) { | 174 bool Has(const GURL& origin, const std::string& cache_name) { |
| 173 base::RunLoop loop; | 175 base::RunLoop loop; |
| 174 cache_manager_->HasCache( | 176 cache_manager_->HasCache( |
| 175 origin, cache_name, | 177 origin, cache_name, |
| 176 base::Bind(&CacheStorageManagerTest::BoolAndErrorCallback, | 178 base::Bind(&CacheStorageManagerTest::BoolAndErrorCallback, |
| 177 base::Unretained(this), base::Unretained(&loop))); | 179 base::Unretained(this), base::Unretained(&loop))); |
| 178 loop.Run(); | 180 loop.Run(); |
| (...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 343 TestBrowserThreadBundle browser_thread_bundle_; | 345 TestBrowserThreadBundle browser_thread_bundle_; |
| 344 TestBrowserContext browser_context_; | 346 TestBrowserContext browser_context_; |
| 345 std::unique_ptr<net::URLRequestJobFactoryImpl> url_request_job_factory_; | 347 std::unique_ptr<net::URLRequestJobFactoryImpl> url_request_job_factory_; |
| 346 storage::BlobStorageContext* blob_storage_context_; | 348 storage::BlobStorageContext* blob_storage_context_; |
| 347 | 349 |
| 348 scoped_refptr<MockSpecialStoragePolicy> quota_policy_; | 350 scoped_refptr<MockSpecialStoragePolicy> quota_policy_; |
| 349 scoped_refptr<MockQuotaManager> mock_quota_manager_; | 351 scoped_refptr<MockQuotaManager> mock_quota_manager_; |
| 350 scoped_refptr<MockQuotaManagerProxy> quota_manager_proxy_; | 352 scoped_refptr<MockQuotaManagerProxy> quota_manager_proxy_; |
| 351 std::unique_ptr<CacheStorageManager> cache_manager_; | 353 std::unique_ptr<CacheStorageManager> cache_manager_; |
| 352 | 354 |
| 353 scoped_refptr<CacheStorageCache> callback_cache_; | 355 std::unique_ptr<CacheStorageCacheHandle> callback_cache_handle_; |
| 354 int callback_bool_; | 356 int callback_bool_; |
| 355 CacheStorageError callback_error_; | 357 CacheStorageError callback_error_; |
| 356 std::unique_ptr<ServiceWorkerResponse> callback_cache_response_; | 358 std::unique_ptr<ServiceWorkerResponse> callback_cache_handle_response_; |
| 357 std::unique_ptr<storage::BlobDataHandle> callback_data_handle_; | 359 std::unique_ptr<storage::BlobDataHandle> callback_data_handle_; |
| 358 std::vector<std::string> callback_strings_; | 360 std::vector<std::string> callback_strings_; |
| 359 | 361 |
| 360 const GURL origin1_; | 362 const GURL origin1_; |
| 361 const GURL origin2_; | 363 const GURL origin2_; |
| 362 | 364 |
| 363 int64_t callback_usage_; | 365 int64_t callback_usage_; |
| 364 std::vector<CacheStorageUsageInfo> callback_all_origins_usage_; | 366 std::vector<CacheStorageUsageInfo> callback_all_origins_usage_; |
| 365 | 367 |
| 366 private: | 368 private: |
| (...skipping 19 matching lines...) Expand all Loading... |
| 386 EXPECT_TRUE(Open(origin1_, "foo")); | 388 EXPECT_TRUE(Open(origin1_, "foo")); |
| 387 } | 389 } |
| 388 | 390 |
| 389 TEST_P(CacheStorageManagerTestP, OpenTwoCaches) { | 391 TEST_P(CacheStorageManagerTestP, OpenTwoCaches) { |
| 390 EXPECT_TRUE(Open(origin1_, "foo")); | 392 EXPECT_TRUE(Open(origin1_, "foo")); |
| 391 EXPECT_TRUE(Open(origin1_, "bar")); | 393 EXPECT_TRUE(Open(origin1_, "bar")); |
| 392 } | 394 } |
| 393 | 395 |
| 394 TEST_P(CacheStorageManagerTestP, CachePointersDiffer) { | 396 TEST_P(CacheStorageManagerTestP, CachePointersDiffer) { |
| 395 EXPECT_TRUE(Open(origin1_, "foo")); | 397 EXPECT_TRUE(Open(origin1_, "foo")); |
| 396 scoped_refptr<CacheStorageCache> cache = callback_cache_; | 398 std::unique_ptr<CacheStorageCacheHandle> cache_handle = |
| 399 std::move(callback_cache_handle_); |
| 397 EXPECT_TRUE(Open(origin1_, "bar")); | 400 EXPECT_TRUE(Open(origin1_, "bar")); |
| 398 EXPECT_NE(callback_cache_.get(), cache.get()); | 401 EXPECT_NE(callback_cache_handle_->value(), cache_handle->value()); |
| 399 } | 402 } |
| 400 | 403 |
| 401 TEST_P(CacheStorageManagerTestP, Open2CachesSameNameDiffOrigins) { | 404 TEST_P(CacheStorageManagerTestP, Open2CachesSameNameDiffOrigins) { |
| 402 EXPECT_TRUE(Open(origin1_, "foo")); | 405 EXPECT_TRUE(Open(origin1_, "foo")); |
| 403 scoped_refptr<CacheStorageCache> cache = callback_cache_; | 406 std::unique_ptr<CacheStorageCacheHandle> cache_handle = |
| 407 std::move(callback_cache_handle_); |
| 404 EXPECT_TRUE(Open(origin2_, "foo")); | 408 EXPECT_TRUE(Open(origin2_, "foo")); |
| 405 EXPECT_NE(cache.get(), callback_cache_.get()); | 409 EXPECT_NE(cache_handle->value(), callback_cache_handle_->value()); |
| 406 } | 410 } |
| 407 | 411 |
| 408 TEST_P(CacheStorageManagerTestP, OpenExistingCache) { | 412 TEST_P(CacheStorageManagerTestP, OpenExistingCache) { |
| 409 EXPECT_TRUE(Open(origin1_, "foo")); | 413 EXPECT_TRUE(Open(origin1_, "foo")); |
| 410 scoped_refptr<CacheStorageCache> cache = callback_cache_; | 414 std::unique_ptr<CacheStorageCacheHandle> cache_handle = |
| 415 std::move(callback_cache_handle_); |
| 411 EXPECT_TRUE(Open(origin1_, "foo")); | 416 EXPECT_TRUE(Open(origin1_, "foo")); |
| 412 EXPECT_EQ(callback_cache_.get(), cache.get()); | 417 EXPECT_EQ(callback_cache_handle_->value(), cache_handle->value()); |
| 413 } | 418 } |
| 414 | 419 |
| 415 TEST_P(CacheStorageManagerTestP, HasCache) { | 420 TEST_P(CacheStorageManagerTestP, HasCache) { |
| 416 EXPECT_TRUE(Open(origin1_, "foo")); | 421 EXPECT_TRUE(Open(origin1_, "foo")); |
| 417 EXPECT_TRUE(Has(origin1_, "foo")); | 422 EXPECT_TRUE(Has(origin1_, "foo")); |
| 418 EXPECT_TRUE(callback_bool_); | 423 EXPECT_TRUE(callback_bool_); |
| 419 } | 424 } |
| 420 | 425 |
| 421 TEST_P(CacheStorageManagerTestP, HasNonExistent) { | 426 TEST_P(CacheStorageManagerTestP, HasNonExistent) { |
| 422 EXPECT_FALSE(Has(origin1_, "foo")); | 427 EXPECT_FALSE(Has(origin1_, "foo")); |
| 423 } | 428 } |
| 424 | 429 |
| 425 TEST_P(CacheStorageManagerTestP, DeleteCache) { | 430 TEST_P(CacheStorageManagerTestP, DeleteCache) { |
| 426 EXPECT_TRUE(Open(origin1_, "foo")); | 431 EXPECT_TRUE(Open(origin1_, "foo")); |
| 427 EXPECT_TRUE(Delete(origin1_, "foo")); | 432 EXPECT_TRUE(Delete(origin1_, "foo")); |
| 428 EXPECT_FALSE(Has(origin1_, "foo")); | 433 EXPECT_FALSE(Has(origin1_, "foo")); |
| 429 } | 434 } |
| 430 | 435 |
| 431 TEST_P(CacheStorageManagerTestP, DeleteTwice) { | 436 TEST_P(CacheStorageManagerTestP, DeleteTwice) { |
| 432 EXPECT_TRUE(Open(origin1_, "foo")); | 437 EXPECT_TRUE(Open(origin1_, "foo")); |
| 433 EXPECT_TRUE(Delete(origin1_, "foo")); | 438 EXPECT_TRUE(Delete(origin1_, "foo")); |
| 434 EXPECT_FALSE(Delete(origin1_, "foo")); | 439 EXPECT_FALSE(Delete(origin1_, "foo")); |
| 435 EXPECT_EQ(CACHE_STORAGE_ERROR_NOT_FOUND, callback_error_); | 440 EXPECT_EQ(CACHE_STORAGE_ERROR_NOT_FOUND, callback_error_); |
| 436 } | 441 } |
| 437 | 442 |
| 438 TEST_P(CacheStorageManagerTestP, DeleteCacheReducesOriginSize) { | 443 TEST_P(CacheStorageManagerTestP, DeleteCacheReducesOriginSize) { |
| 439 EXPECT_TRUE(Open(origin1_, "foo")); | 444 EXPECT_TRUE(Open(origin1_, "foo")); |
| 440 EXPECT_TRUE(CachePut(callback_cache_.get(), GURL("http://example.com/foo"))); | 445 EXPECT_TRUE(CachePut(callback_cache_handle_->value(), |
| 446 GURL("http://example.com/foo"))); |
| 441 // The quota manager gets updated after the put operation runs its callback so | 447 // The quota manager gets updated after the put operation runs its callback so |
| 442 // run the event loop. | 448 // run the event loop. |
| 443 base::RunLoop().RunUntilIdle(); | 449 base::RunLoop().RunUntilIdle(); |
| 444 int64_t put_delta = quota_manager_proxy_->last_notified_delta(); | 450 int64_t put_delta = quota_manager_proxy_->last_notified_delta(); |
| 445 EXPECT_LT(0, put_delta); | 451 EXPECT_LT(0, put_delta); |
| 446 EXPECT_TRUE(Delete(origin1_, "foo")); | 452 EXPECT_TRUE(Delete(origin1_, "foo")); |
| 447 EXPECT_EQ(put_delta, -1 * quota_manager_proxy_->last_notified_delta()); | 453 EXPECT_EQ(put_delta, -1 * quota_manager_proxy_->last_notified_delta()); |
| 448 } | 454 } |
| 449 | 455 |
| 450 TEST_P(CacheStorageManagerTestP, EmptyKeys) { | 456 TEST_P(CacheStorageManagerTestP, EmptyKeys) { |
| (...skipping 21 matching lines...) Expand all Loading... |
| 472 EXPECT_TRUE(Open(origin1_, "bar")); | 478 EXPECT_TRUE(Open(origin1_, "bar")); |
| 473 EXPECT_TRUE(Open(origin2_, "baz")); | 479 EXPECT_TRUE(Open(origin2_, "baz")); |
| 474 EXPECT_TRUE(Delete(origin1_, "bar")); | 480 EXPECT_TRUE(Delete(origin1_, "bar")); |
| 475 EXPECT_TRUE(Keys(origin1_)); | 481 EXPECT_TRUE(Keys(origin1_)); |
| 476 EXPECT_EQ(1u, callback_strings_.size()); | 482 EXPECT_EQ(1u, callback_strings_.size()); |
| 477 EXPECT_STREQ("foo", callback_strings_[0].c_str()); | 483 EXPECT_STREQ("foo", callback_strings_[0].c_str()); |
| 478 } | 484 } |
| 479 | 485 |
| 480 TEST_P(CacheStorageManagerTestP, StorageMatchEntryExists) { | 486 TEST_P(CacheStorageManagerTestP, StorageMatchEntryExists) { |
| 481 EXPECT_TRUE(Open(origin1_, "foo")); | 487 EXPECT_TRUE(Open(origin1_, "foo")); |
| 482 EXPECT_TRUE(CachePut(callback_cache_.get(), GURL("http://example.com/foo"))); | 488 EXPECT_TRUE(CachePut(callback_cache_handle_->value(), |
| 489 GURL("http://example.com/foo"))); |
| 483 EXPECT_TRUE(StorageMatch(origin1_, "foo", GURL("http://example.com/foo"))); | 490 EXPECT_TRUE(StorageMatch(origin1_, "foo", GURL("http://example.com/foo"))); |
| 484 } | 491 } |
| 485 | 492 |
| 486 TEST_P(CacheStorageManagerTestP, StorageMatchNoEntry) { | 493 TEST_P(CacheStorageManagerTestP, StorageMatchNoEntry) { |
| 487 EXPECT_TRUE(Open(origin1_, "foo")); | 494 EXPECT_TRUE(Open(origin1_, "foo")); |
| 488 EXPECT_TRUE(CachePut(callback_cache_.get(), GURL("http://example.com/foo"))); | 495 EXPECT_TRUE(CachePut(callback_cache_handle_->value(), |
| 496 GURL("http://example.com/foo"))); |
| 489 EXPECT_FALSE(StorageMatch(origin1_, "foo", GURL("http://example.com/bar"))); | 497 EXPECT_FALSE(StorageMatch(origin1_, "foo", GURL("http://example.com/bar"))); |
| 490 EXPECT_EQ(CACHE_STORAGE_ERROR_NOT_FOUND, callback_error_); | 498 EXPECT_EQ(CACHE_STORAGE_ERROR_NOT_FOUND, callback_error_); |
| 491 } | 499 } |
| 492 | 500 |
| 493 TEST_P(CacheStorageManagerTestP, StorageMatchNoCache) { | 501 TEST_P(CacheStorageManagerTestP, StorageMatchNoCache) { |
| 494 EXPECT_TRUE(Open(origin1_, "foo")); | 502 EXPECT_TRUE(Open(origin1_, "foo")); |
| 495 EXPECT_TRUE(CachePut(callback_cache_.get(), GURL("http://example.com/foo"))); | 503 EXPECT_TRUE(CachePut(callback_cache_handle_->value(), |
| 504 GURL("http://example.com/foo"))); |
| 496 EXPECT_FALSE(StorageMatch(origin1_, "bar", GURL("http://example.com/foo"))); | 505 EXPECT_FALSE(StorageMatch(origin1_, "bar", GURL("http://example.com/foo"))); |
| 497 EXPECT_EQ(CACHE_STORAGE_ERROR_CACHE_NAME_NOT_FOUND, callback_error_); | 506 EXPECT_EQ(CACHE_STORAGE_ERROR_CACHE_NAME_NOT_FOUND, callback_error_); |
| 498 } | 507 } |
| 499 | 508 |
| 500 TEST_P(CacheStorageManagerTestP, StorageMatchAllEntryExists) { | 509 TEST_P(CacheStorageManagerTestP, StorageMatchAllEntryExists) { |
| 501 EXPECT_TRUE(Open(origin1_, "foo")); | 510 EXPECT_TRUE(Open(origin1_, "foo")); |
| 502 EXPECT_TRUE(CachePut(callback_cache_.get(), GURL("http://example.com/foo"))); | 511 EXPECT_TRUE(CachePut(callback_cache_handle_->value(), |
| 512 GURL("http://example.com/foo"))); |
| 503 EXPECT_TRUE(StorageMatchAll(origin1_, GURL("http://example.com/foo"))); | 513 EXPECT_TRUE(StorageMatchAll(origin1_, GURL("http://example.com/foo"))); |
| 504 } | 514 } |
| 505 | 515 |
| 506 TEST_P(CacheStorageManagerTestP, StorageMatchAllNoEntry) { | 516 TEST_P(CacheStorageManagerTestP, StorageMatchAllNoEntry) { |
| 507 EXPECT_TRUE(Open(origin1_, "foo")); | 517 EXPECT_TRUE(Open(origin1_, "foo")); |
| 508 EXPECT_TRUE(CachePut(callback_cache_.get(), GURL("http://example.com/foo"))); | 518 EXPECT_TRUE(CachePut(callback_cache_handle_->value(), |
| 519 GURL("http://example.com/foo"))); |
| 509 EXPECT_FALSE(StorageMatchAll(origin1_, GURL("http://example.com/bar"))); | 520 EXPECT_FALSE(StorageMatchAll(origin1_, GURL("http://example.com/bar"))); |
| 510 EXPECT_EQ(CACHE_STORAGE_ERROR_NOT_FOUND, callback_error_); | 521 EXPECT_EQ(CACHE_STORAGE_ERROR_NOT_FOUND, callback_error_); |
| 511 } | 522 } |
| 512 | 523 |
| 513 TEST_P(CacheStorageManagerTestP, StorageMatchAllNoCaches) { | 524 TEST_P(CacheStorageManagerTestP, StorageMatchAllNoCaches) { |
| 514 EXPECT_FALSE(StorageMatchAll(origin1_, GURL("http://example.com/foo"))); | 525 EXPECT_FALSE(StorageMatchAll(origin1_, GURL("http://example.com/foo"))); |
| 515 EXPECT_EQ(CACHE_STORAGE_ERROR_NOT_FOUND, callback_error_); | 526 EXPECT_EQ(CACHE_STORAGE_ERROR_NOT_FOUND, callback_error_); |
| 516 } | 527 } |
| 517 | 528 |
| 518 TEST_F(CacheStorageManagerTest, StorageReuseCacheName) { | 529 TEST_F(CacheStorageManagerTest, StorageReuseCacheName) { |
| 519 // Deleting a cache and creating one with the same name and adding an entry | 530 // Deleting a cache and creating one with the same name and adding an entry |
| 520 // with the same URL should work. (see crbug.com/542668) | 531 // with the same URL should work. (see crbug.com/542668) |
| 521 const GURL kTestURL = GURL("http://example.com/foo"); | 532 const GURL kTestURL = GURL("http://example.com/foo"); |
| 522 EXPECT_TRUE(Open(origin1_, "foo")); | 533 EXPECT_TRUE(Open(origin1_, "foo")); |
| 523 EXPECT_TRUE(CachePut(callback_cache_.get(), kTestURL)); | 534 EXPECT_TRUE(CachePut(callback_cache_handle_->value(), kTestURL)); |
| 524 EXPECT_TRUE(CacheMatch(callback_cache_.get(), kTestURL)); | 535 EXPECT_TRUE(CacheMatch(callback_cache_handle_->value(), kTestURL)); |
| 525 std::unique_ptr<storage::BlobDataHandle> data_handle = | 536 std::unique_ptr<storage::BlobDataHandle> data_handle = |
| 526 std::move(callback_data_handle_); | 537 std::move(callback_data_handle_); |
| 527 | 538 |
| 528 EXPECT_TRUE(Delete(origin1_, "foo")); | 539 EXPECT_TRUE(Delete(origin1_, "foo")); |
| 529 // The cache is deleted but the handle to one of its entries is still | 540 // The cache is deleted but the handle to one of its entries is still |
| 530 // open. Creating a new cache in the same directory would fail on Windows. | 541 // open. Creating a new cache in the same directory would fail on Windows. |
| 531 EXPECT_TRUE(Open(origin1_, "foo")); | 542 EXPECT_TRUE(Open(origin1_, "foo")); |
| 532 EXPECT_TRUE(CachePut(callback_cache_.get(), kTestURL)); | 543 EXPECT_TRUE(CachePut(callback_cache_handle_->value(), kTestURL)); |
| 533 } | 544 } |
| 534 | 545 |
| 535 TEST_P(CacheStorageManagerTestP, StorageMatchAllEntryExistsTwice) { | 546 TEST_P(CacheStorageManagerTestP, StorageMatchAllEntryExistsTwice) { |
| 536 EXPECT_TRUE(Open(origin1_, "foo")); | 547 EXPECT_TRUE(Open(origin1_, "foo")); |
| 537 EXPECT_TRUE(CachePutWithStatusCode(callback_cache_.get(), | 548 EXPECT_TRUE(CachePutWithStatusCode(callback_cache_handle_->value(), |
| 538 GURL("http://example.com/foo"), 200)); | 549 GURL("http://example.com/foo"), 200)); |
| 539 EXPECT_TRUE(Open(origin1_, "bar")); | 550 EXPECT_TRUE(Open(origin1_, "bar")); |
| 540 EXPECT_TRUE(CachePutWithStatusCode(callback_cache_.get(), | 551 EXPECT_TRUE(CachePutWithStatusCode(callback_cache_handle_->value(), |
| 541 GURL("http://example.com/foo"), 201)); | 552 GURL("http://example.com/foo"), 201)); |
| 542 | 553 |
| 543 EXPECT_TRUE(StorageMatchAll(origin1_, GURL("http://example.com/foo"))); | 554 EXPECT_TRUE(StorageMatchAll(origin1_, GURL("http://example.com/foo"))); |
| 544 | 555 |
| 545 // The caches need to be searched in order of creation, so verify that the | 556 // The caches need to be searched in order of creation, so verify that the |
| 546 // response came from the first cache. | 557 // response came from the first cache. |
| 547 EXPECT_EQ(200, callback_cache_response_->status_code); | 558 EXPECT_EQ(200, callback_cache_handle_response_->status_code); |
| 548 } | 559 } |
| 549 | 560 |
| 550 TEST_P(CacheStorageManagerTestP, StorageMatchInOneOfMany) { | 561 TEST_P(CacheStorageManagerTestP, StorageMatchInOneOfMany) { |
| 551 EXPECT_TRUE(Open(origin1_, "foo")); | 562 EXPECT_TRUE(Open(origin1_, "foo")); |
| 552 EXPECT_TRUE(Open(origin1_, "bar")); | 563 EXPECT_TRUE(Open(origin1_, "bar")); |
| 553 EXPECT_TRUE(CachePut(callback_cache_.get(), GURL("http://example.com/foo"))); | 564 EXPECT_TRUE(CachePut(callback_cache_handle_->value(), |
| 565 GURL("http://example.com/foo"))); |
| 554 EXPECT_TRUE(Open(origin1_, "baz")); | 566 EXPECT_TRUE(Open(origin1_, "baz")); |
| 555 | 567 |
| 556 EXPECT_TRUE(StorageMatchAll(origin1_, GURL("http://example.com/foo"))); | 568 EXPECT_TRUE(StorageMatchAll(origin1_, GURL("http://example.com/foo"))); |
| 557 } | 569 } |
| 558 | 570 |
| 559 TEST_P(CacheStorageManagerTestP, Chinese) { | 571 TEST_P(CacheStorageManagerTestP, Chinese) { |
| 560 EXPECT_TRUE(Open(origin1_, "ä½ å¥½")); | 572 EXPECT_TRUE(Open(origin1_, "ä½ å¥½")); |
| 561 scoped_refptr<CacheStorageCache> cache = callback_cache_; | 573 std::unique_ptr<CacheStorageCacheHandle> cache_handle = |
| 574 std::move(callback_cache_handle_); |
| 562 EXPECT_TRUE(Open(origin1_, "ä½ å¥½")); | 575 EXPECT_TRUE(Open(origin1_, "ä½ å¥½")); |
| 563 EXPECT_EQ(callback_cache_.get(), cache.get()); | 576 EXPECT_EQ(callback_cache_handle_->value(), cache_handle->value()); |
| 564 EXPECT_TRUE(Keys(origin1_)); | 577 EXPECT_TRUE(Keys(origin1_)); |
| 565 EXPECT_EQ(1u, callback_strings_.size()); | 578 EXPECT_EQ(1u, callback_strings_.size()); |
| 566 EXPECT_STREQ("ä½ å¥½", callback_strings_[0].c_str()); | 579 EXPECT_STREQ("ä½ å¥½", callback_strings_[0].c_str()); |
| 567 } | 580 } |
| 568 | 581 |
| 569 TEST_F(CacheStorageManagerTest, EmptyKey) { | 582 TEST_F(CacheStorageManagerTest, EmptyKey) { |
| 570 EXPECT_TRUE(Open(origin1_, "")); | 583 EXPECT_TRUE(Open(origin1_, "")); |
| 571 scoped_refptr<CacheStorageCache> cache = callback_cache_; | 584 std::unique_ptr<CacheStorageCacheHandle> cache_handle = |
| 585 std::move(callback_cache_handle_); |
| 572 EXPECT_TRUE(Open(origin1_, "")); | 586 EXPECT_TRUE(Open(origin1_, "")); |
| 573 EXPECT_EQ(cache.get(), callback_cache_.get()); | 587 EXPECT_EQ(cache_handle->value(), callback_cache_handle_->value()); |
| 574 EXPECT_TRUE(Keys(origin1_)); | 588 EXPECT_TRUE(Keys(origin1_)); |
| 575 EXPECT_EQ(1u, callback_strings_.size()); | 589 EXPECT_EQ(1u, callback_strings_.size()); |
| 576 EXPECT_STREQ("", callback_strings_[0].c_str()); | 590 EXPECT_STREQ("", callback_strings_[0].c_str()); |
| 577 EXPECT_TRUE(Has(origin1_, "")); | 591 EXPECT_TRUE(Has(origin1_, "")); |
| 578 EXPECT_TRUE(Delete(origin1_, "")); | 592 EXPECT_TRUE(Delete(origin1_, "")); |
| 579 EXPECT_TRUE(Keys(origin1_)); | 593 EXPECT_TRUE(Keys(origin1_)); |
| 580 EXPECT_EQ(0u, callback_strings_.size()); | 594 EXPECT_EQ(0u, callback_strings_.size()); |
| 581 } | 595 } |
| 582 | 596 |
| 583 TEST_F(CacheStorageManagerTest, DataPersists) { | 597 TEST_F(CacheStorageManagerTest, DataPersists) { |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 621 GURL bad_origin("http://../../../../../../../../../../../../../../foo"); | 635 GURL bad_origin("http://../../../../../../../../../../../../../../foo"); |
| 622 EXPECT_TRUE(Open(bad_origin, "foo")); | 636 EXPECT_TRUE(Open(bad_origin, "foo")); |
| 623 EXPECT_TRUE(Keys(bad_origin)); | 637 EXPECT_TRUE(Keys(bad_origin)); |
| 624 EXPECT_EQ(1u, callback_strings_.size()); | 638 EXPECT_EQ(1u, callback_strings_.size()); |
| 625 EXPECT_STREQ("foo", callback_strings_[0].c_str()); | 639 EXPECT_STREQ("foo", callback_strings_[0].c_str()); |
| 626 } | 640 } |
| 627 | 641 |
| 628 TEST_P(CacheStorageManagerTestP, DeleteBeforeRelease) { | 642 TEST_P(CacheStorageManagerTestP, DeleteBeforeRelease) { |
| 629 EXPECT_TRUE(Open(origin1_, "foo")); | 643 EXPECT_TRUE(Open(origin1_, "foo")); |
| 630 EXPECT_TRUE(Delete(origin1_, "foo")); | 644 EXPECT_TRUE(Delete(origin1_, "foo")); |
| 631 EXPECT_TRUE(callback_cache_->AsWeakPtr()); | 645 EXPECT_TRUE(callback_cache_handle_->value()); |
| 632 } | 646 } |
| 633 | 647 |
| 634 TEST_P(CacheStorageManagerTestP, OpenRunsSerially) { | 648 TEST_P(CacheStorageManagerTestP, OpenRunsSerially) { |
| 635 EXPECT_FALSE(Delete(origin1_, "tmp")); // Init storage. | 649 EXPECT_FALSE(Delete(origin1_, "tmp")); // Init storage. |
| 636 CacheStorage* cache_storage = CacheStorageForOrigin(origin1_); | 650 CacheStorage* cache_storage = CacheStorageForOrigin(origin1_); |
| 637 cache_storage->StartAsyncOperationForTesting(); | 651 cache_storage->StartAsyncOperationForTesting(); |
| 638 | 652 |
| 639 base::RunLoop open_loop; | 653 base::RunLoop open_loop; |
| 640 cache_manager_->OpenCache( | 654 cache_manager_->OpenCache( |
| 641 origin1_, "foo", | 655 origin1_, "foo", |
| 642 base::Bind(&CacheStorageManagerTest::CacheAndErrorCallback, | 656 base::Bind(&CacheStorageManagerTest::CacheAndErrorCallback, |
| 643 base::Unretained(this), base::Unretained(&open_loop))); | 657 base::Unretained(this), base::Unretained(&open_loop))); |
| 644 | 658 |
| 645 base::RunLoop().RunUntilIdle(); | 659 base::RunLoop().RunUntilIdle(); |
| 646 EXPECT_FALSE(callback_cache_); | 660 EXPECT_FALSE(callback_cache_handle_); |
| 647 | 661 |
| 648 cache_storage->CompleteAsyncOperationForTesting(); | 662 cache_storage->CompleteAsyncOperationForTesting(); |
| 649 open_loop.Run(); | 663 open_loop.Run(); |
| 650 EXPECT_TRUE(callback_cache_); | 664 EXPECT_TRUE(callback_cache_handle_); |
| 651 } | 665 } |
| 652 | 666 |
| 653 TEST_P(CacheStorageManagerTestP, GetOriginUsage) { | 667 TEST_P(CacheStorageManagerTestP, GetOriginUsage) { |
| 654 EXPECT_EQ(0, GetOriginUsage(origin1_)); | 668 EXPECT_EQ(0, GetOriginUsage(origin1_)); |
| 655 EXPECT_TRUE(Open(origin1_, "foo")); | 669 EXPECT_TRUE(Open(origin1_, "foo")); |
| 656 EXPECT_EQ(0, GetOriginUsage(origin1_)); | 670 EXPECT_EQ(0, GetOriginUsage(origin1_)); |
| 657 EXPECT_TRUE(CachePut(callback_cache_.get(), GURL("http://example.com/foo"))); | 671 EXPECT_TRUE(CachePut(callback_cache_handle_->value(), |
| 672 GURL("http://example.com/foo"))); |
| 658 int64_t foo_size = GetOriginUsage(origin1_); | 673 int64_t foo_size = GetOriginUsage(origin1_); |
| 659 EXPECT_LT(0, GetOriginUsage(origin1_)); | 674 EXPECT_LT(0, GetOriginUsage(origin1_)); |
| 660 EXPECT_EQ(0, GetOriginUsage(origin2_)); | 675 EXPECT_EQ(0, GetOriginUsage(origin2_)); |
| 661 | 676 |
| 662 // Add the same entry into a second cache, the size should double. | 677 // Add the same entry into a second cache, the size should double. |
| 663 EXPECT_TRUE(Open(origin1_, "bar")); | 678 EXPECT_TRUE(Open(origin1_, "bar")); |
| 664 EXPECT_TRUE(CachePut(callback_cache_.get(), GURL("http://example.com/foo"))); | 679 EXPECT_TRUE(CachePut(callback_cache_handle_->value(), |
| 680 GURL("http://example.com/foo"))); |
| 665 EXPECT_EQ(2 * foo_size, GetOriginUsage(origin1_)); | 681 EXPECT_EQ(2 * foo_size, GetOriginUsage(origin1_)); |
| 666 } | 682 } |
| 667 | 683 |
| 668 TEST_P(CacheStorageManagerTestP, GetAllOriginsUsage) { | 684 TEST_P(CacheStorageManagerTestP, GetAllOriginsUsage) { |
| 669 EXPECT_EQ(0ULL, GetAllOriginsUsage().size()); | 685 EXPECT_EQ(0ULL, GetAllOriginsUsage().size()); |
| 670 // Put one entry in a cache on origin 1. | 686 // Put one entry in a cache on origin 1. |
| 671 EXPECT_TRUE(Open(origin1_, "foo")); | 687 EXPECT_TRUE(Open(origin1_, "foo")); |
| 672 EXPECT_TRUE(CachePut(callback_cache_.get(), GURL("http://example.com/foo"))); | 688 EXPECT_TRUE(CachePut(callback_cache_handle_->value(), |
| 689 GURL("http://example.com/foo"))); |
| 673 | 690 |
| 674 // Put two entries (of identical size) in a cache on origin 2. | 691 // Put two entries (of identical size) in a cache on origin 2. |
| 675 EXPECT_TRUE(Open(origin2_, "foo")); | 692 EXPECT_TRUE(Open(origin2_, "foo")); |
| 676 EXPECT_TRUE(CachePut(callback_cache_.get(), GURL("http://example.com/foo"))); | 693 EXPECT_TRUE(CachePut(callback_cache_handle_->value(), |
| 677 EXPECT_TRUE(CachePut(callback_cache_.get(), GURL("http://example.com/bar"))); | 694 GURL("http://example.com/foo"))); |
| 695 EXPECT_TRUE(CachePut(callback_cache_handle_->value(), |
| 696 GURL("http://example.com/bar"))); |
| 678 | 697 |
| 679 std::vector<CacheStorageUsageInfo> usage = GetAllOriginsUsage(); | 698 std::vector<CacheStorageUsageInfo> usage = GetAllOriginsUsage(); |
| 680 EXPECT_EQ(2ULL, usage.size()); | 699 EXPECT_EQ(2ULL, usage.size()); |
| 681 | 700 |
| 682 int origin1_index = usage[0].origin == origin1_ ? 0 : 1; | 701 int origin1_index = usage[0].origin == origin1_ ? 0 : 1; |
| 683 int origin2_index = usage[1].origin == origin2_ ? 1 : 0; | 702 int origin2_index = usage[1].origin == origin2_ ? 1 : 0; |
| 684 EXPECT_NE(origin1_index, origin2_index); | 703 EXPECT_NE(origin1_index, origin2_index); |
| 685 | 704 |
| 686 int64_t origin1_size = usage[origin1_index].total_size_bytes; | 705 int64_t origin1_size = usage[origin1_index].total_size_bytes; |
| 687 int64_t origin2_size = usage[origin2_index].total_size_bytes; | 706 int64_t origin2_size = usage[origin2_index].total_size_bytes; |
| 688 EXPECT_EQ(2 * origin1_size, origin2_size); | 707 EXPECT_EQ(2 * origin1_size, origin2_size); |
| 689 | 708 |
| 690 if (MemoryOnly()) { | 709 if (MemoryOnly()) { |
| 691 EXPECT_TRUE(usage[origin1_index].last_modified.is_null()); | 710 EXPECT_TRUE(usage[origin1_index].last_modified.is_null()); |
| 692 EXPECT_TRUE(usage[origin2_index].last_modified.is_null()); | 711 EXPECT_TRUE(usage[origin2_index].last_modified.is_null()); |
| 693 } else { | 712 } else { |
| 694 EXPECT_FALSE(usage[origin1_index].last_modified.is_null()); | 713 EXPECT_FALSE(usage[origin1_index].last_modified.is_null()); |
| 695 EXPECT_FALSE(usage[origin2_index].last_modified.is_null()); | 714 EXPECT_FALSE(usage[origin2_index].last_modified.is_null()); |
| 696 } | 715 } |
| 697 } | 716 } |
| 698 | 717 |
| 699 TEST_P(CacheStorageManagerTestP, GetSizeThenCloseAllCaches) { | 718 TEST_P(CacheStorageManagerTestP, GetSizeThenCloseAllCaches) { |
| 700 EXPECT_TRUE(Open(origin1_, "foo")); | 719 EXPECT_TRUE(Open(origin1_, "foo")); |
| 701 EXPECT_TRUE(CachePut(callback_cache_.get(), GURL("http://example.com/foo"))); | 720 EXPECT_TRUE(CachePut(callback_cache_handle_->value(), |
| 702 EXPECT_TRUE(CachePut(callback_cache_.get(), GURL("http://example.com/foo2"))); | 721 GURL("http://example.com/foo"))); |
| 722 EXPECT_TRUE(CachePut(callback_cache_handle_->value(), |
| 723 GURL("http://example.com/foo2"))); |
| 703 EXPECT_TRUE(Open(origin1_, "bar")); | 724 EXPECT_TRUE(Open(origin1_, "bar")); |
| 704 EXPECT_TRUE(CachePut(callback_cache_.get(), GURL("http://example.com/bar"))); | 725 EXPECT_TRUE(CachePut(callback_cache_handle_->value(), |
| 726 GURL("http://example.com/bar"))); |
| 705 | 727 |
| 706 int64_t origin_size = GetOriginUsage(origin1_); | 728 int64_t origin_size = GetOriginUsage(origin1_); |
| 707 EXPECT_LT(0, origin_size); | 729 EXPECT_LT(0, origin_size); |
| 708 | 730 |
| 709 EXPECT_EQ(origin_size, GetSizeThenCloseAllCaches(origin1_)); | 731 EXPECT_EQ(origin_size, GetSizeThenCloseAllCaches(origin1_)); |
| 710 EXPECT_FALSE(CachePut(callback_cache_.get(), GURL("http://example.com/baz"))); | 732 EXPECT_FALSE(CachePut(callback_cache_handle_->value(), |
| 733 GURL("http://example.com/baz"))); |
| 711 } | 734 } |
| 712 | 735 |
| 713 TEST_F(CacheStorageManagerTest, DeleteUnreferencedCacheDirectories) { | 736 TEST_F(CacheStorageManagerTest, DeleteUnreferencedCacheDirectories) { |
| 714 // Create a referenced cache. | 737 // Create a referenced cache. |
| 715 EXPECT_TRUE(Open(origin1_, "foo")); | 738 EXPECT_TRUE(Open(origin1_, "foo")); |
| 716 EXPECT_TRUE(CachePut(callback_cache_.get(), GURL("http://example.com/foo"))); | 739 EXPECT_TRUE(CachePut(callback_cache_handle_->value(), |
| 740 GURL("http://example.com/foo"))); |
| 717 | 741 |
| 718 // Create an unreferenced directory next to the referenced one. | 742 // Create an unreferenced directory next to the referenced one. |
| 719 base::FilePath origin_path = CacheStorageManager::ConstructOriginPath( | 743 base::FilePath origin_path = CacheStorageManager::ConstructOriginPath( |
| 720 cache_manager_->root_path(), origin1_); | 744 cache_manager_->root_path(), origin1_); |
| 721 base::FilePath unreferenced_path = origin_path.AppendASCII("bar"); | 745 base::FilePath unreferenced_path = origin_path.AppendASCII("bar"); |
| 722 EXPECT_TRUE(CreateDirectory(unreferenced_path)); | 746 EXPECT_TRUE(CreateDirectory(unreferenced_path)); |
| 723 EXPECT_TRUE(base::DirectoryExists(unreferenced_path)); | 747 EXPECT_TRUE(base::DirectoryExists(unreferenced_path)); |
| 724 | 748 |
| 725 // Create a new StorageManager so that the next time the cache is opened | 749 // Create a new StorageManager so that the next time the cache is opened |
| 726 // the unreferenced directory can be deleted. | 750 // the unreferenced directory can be deleted. |
| 727 quota_manager_proxy_->SimulateQuotaManagerDestroyed(); | 751 quota_manager_proxy_->SimulateQuotaManagerDestroyed(); |
| 728 cache_manager_ = CacheStorageManager::Create(cache_manager_.get()); | 752 cache_manager_ = CacheStorageManager::Create(cache_manager_.get()); |
| 729 | 753 |
| 730 // Verify that the referenced cache still works. | 754 // Verify that the referenced cache still works. |
| 731 EXPECT_TRUE(Open(origin1_, "foo")); | 755 EXPECT_TRUE(Open(origin1_, "foo")); |
| 732 EXPECT_TRUE( | 756 EXPECT_TRUE(CacheMatch(callback_cache_handle_->value(), |
| 733 CacheMatch(callback_cache_.get(), GURL("http://example.com/foo"))); | 757 GURL("http://example.com/foo"))); |
| 734 | 758 |
| 735 // Verify that the unreferenced cache is gone. | 759 // Verify that the unreferenced cache is gone. |
| 736 EXPECT_FALSE(base::DirectoryExists(unreferenced_path)); | 760 EXPECT_FALSE(base::DirectoryExists(unreferenced_path)); |
| 737 } | 761 } |
| 738 | 762 |
| 739 TEST_P(CacheStorageManagerTestP, OpenCacheStorageAccessed) { | 763 TEST_P(CacheStorageManagerTestP, OpenCacheStorageAccessed) { |
| 740 EXPECT_EQ(0, quota_manager_proxy_->notify_storage_accessed_count()); | 764 EXPECT_EQ(0, quota_manager_proxy_->notify_storage_accessed_count()); |
| 741 EXPECT_TRUE(Open(origin1_, "foo")); | 765 EXPECT_TRUE(Open(origin1_, "foo")); |
| 742 EXPECT_EQ(1, quota_manager_proxy_->notify_storage_accessed_count()); | 766 EXPECT_EQ(1, quota_manager_proxy_->notify_storage_accessed_count()); |
| 743 } | 767 } |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 797 // so that tests can verify the results of migration. | 821 // so that tests can verify the results of migration. |
| 798 legacy_path_ = CacheStorageManager::ConstructLegacyOriginPath( | 822 legacy_path_ = CacheStorageManager::ConstructLegacyOriginPath( |
| 799 cache_manager_->root_path(), origin1_); | 823 cache_manager_->root_path(), origin1_); |
| 800 new_path_ = CacheStorageManager::ConstructOriginPath( | 824 new_path_ = CacheStorageManager::ConstructOriginPath( |
| 801 cache_manager_->root_path(), origin1_); | 825 cache_manager_->root_path(), origin1_); |
| 802 | 826 |
| 803 ASSERT_FALSE(base::DirectoryExists(legacy_path_)); | 827 ASSERT_FALSE(base::DirectoryExists(legacy_path_)); |
| 804 ASSERT_FALSE(base::DirectoryExists(new_path_)); | 828 ASSERT_FALSE(base::DirectoryExists(new_path_)); |
| 805 ASSERT_TRUE(Open(origin1_, cache1_)); | 829 ASSERT_TRUE(Open(origin1_, cache1_)); |
| 806 ASSERT_TRUE(Open(origin1_, cache2_)); | 830 ASSERT_TRUE(Open(origin1_, cache2_)); |
| 807 callback_cache_ = nullptr; | 831 callback_cache_handle_.reset(); |
| 808 ASSERT_FALSE(base::DirectoryExists(legacy_path_)); | 832 ASSERT_FALSE(base::DirectoryExists(legacy_path_)); |
| 809 ASSERT_TRUE(base::DirectoryExists(new_path_)); | 833 ASSERT_TRUE(base::DirectoryExists(new_path_)); |
| 810 | 834 |
| 811 quota_manager_proxy_->SimulateQuotaManagerDestroyed(); | 835 quota_manager_proxy_->SimulateQuotaManagerDestroyed(); |
| 812 cache_manager_ = CacheStorageManager::Create(cache_manager_.get()); | 836 cache_manager_ = CacheStorageManager::Create(cache_manager_.get()); |
| 813 | 837 |
| 814 ASSERT_TRUE(base::Move(new_path_, legacy_path_)); | 838 ASSERT_TRUE(base::Move(new_path_, legacy_path_)); |
| 815 ASSERT_TRUE(base::DirectoryExists(legacy_path_)); | 839 ASSERT_TRUE(base::DirectoryExists(legacy_path_)); |
| 816 ASSERT_FALSE(base::DirectoryExists(new_path_)); | 840 ASSERT_FALSE(base::DirectoryExists(new_path_)); |
| 817 } | 841 } |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 887 void SetUp() override { | 911 void SetUp() override { |
| 888 CacheStorageManagerTest::SetUp(); | 912 CacheStorageManagerTest::SetUp(); |
| 889 | 913 |
| 890 // Create a cache that is stored on disk with the legacy naming scheme (hash | 914 // Create a cache that is stored on disk with the legacy naming scheme (hash |
| 891 // of the name) and without a directory name in the index. | 915 // of the name) and without a directory name in the index. |
| 892 base::FilePath origin_path = CacheStorageManager::ConstructOriginPath( | 916 base::FilePath origin_path = CacheStorageManager::ConstructOriginPath( |
| 893 cache_manager_->root_path(), origin1_); | 917 cache_manager_->root_path(), origin1_); |
| 894 | 918 |
| 895 // Populate a legacy cache. | 919 // Populate a legacy cache. |
| 896 ASSERT_TRUE(Open(origin1_, legacy_cache_name_)); | 920 ASSERT_TRUE(Open(origin1_, legacy_cache_name_)); |
| 897 EXPECT_TRUE(CachePut(callback_cache_.get(), stored_url_)); | 921 EXPECT_TRUE(CachePut(callback_cache_handle_->value(), stored_url_)); |
| 898 base::FilePath new_path = callback_cache_->path(); | 922 base::FilePath new_path = callback_cache_handle_->value()->path(); |
| 899 | 923 |
| 900 // Close the cache's backend so that the files can be moved. | 924 // Close the cache's backend so that the files can be moved. |
| 901 callback_cache_->Close(base::Bind(&base::DoNothing)); | 925 callback_cache_handle_->value()->Close(base::Bind(&base::DoNothing)); |
| 902 base::RunLoop().RunUntilIdle(); | 926 base::RunLoop().RunUntilIdle(); |
| 903 | 927 |
| 904 // Legacy index files didn't have the cache directory, so remove it from the | 928 // Legacy index files didn't have the cache directory, so remove it from the |
| 905 // index. | 929 // index. |
| 906 base::FilePath index_path = origin_path.AppendASCII("index.txt"); | 930 base::FilePath index_path = origin_path.AppendASCII("index.txt"); |
| 907 std::string index_contents; | 931 std::string index_contents; |
| 908 base::ReadFileToString(index_path, &index_contents); | 932 base::ReadFileToString(index_path, &index_contents); |
| 909 CacheStorageIndex index; | 933 CacheStorageIndex index; |
| 910 ASSERT_TRUE(index.ParseFromString(index_contents)); | 934 ASSERT_TRUE(index.ParseFromString(index_contents)); |
| 911 ASSERT_EQ(1, index.cache_size()); | 935 ASSERT_EQ(1, index.cache_size()); |
| (...skipping 28 matching lines...) Expand all Loading... |
| 940 DISALLOW_COPY_AND_ASSIGN(MigratedLegacyCacheDirectoryNameTest); | 964 DISALLOW_COPY_AND_ASSIGN(MigratedLegacyCacheDirectoryNameTest); |
| 941 }; | 965 }; |
| 942 | 966 |
| 943 TEST_F(MigratedLegacyCacheDirectoryNameTest, LegacyCacheMigrated) { | 967 TEST_F(MigratedLegacyCacheDirectoryNameTest, LegacyCacheMigrated) { |
| 944 EXPECT_TRUE(Open(origin1_, legacy_cache_name_)); | 968 EXPECT_TRUE(Open(origin1_, legacy_cache_name_)); |
| 945 | 969 |
| 946 // Verify that the cache migrated away from the legacy_path_ directory. | 970 // Verify that the cache migrated away from the legacy_path_ directory. |
| 947 ASSERT_FALSE(base::DirectoryExists(legacy_path_)); | 971 ASSERT_FALSE(base::DirectoryExists(legacy_path_)); |
| 948 | 972 |
| 949 // Verify that the existing entry still works. | 973 // Verify that the existing entry still works. |
| 950 EXPECT_TRUE(CacheMatch(callback_cache_.get(), stored_url_)); | 974 EXPECT_TRUE(CacheMatch(callback_cache_handle_->value(), stored_url_)); |
| 951 | 975 |
| 952 // Verify that adding new entries works. | 976 // Verify that adding new entries works. |
| 953 EXPECT_TRUE(CachePut(callback_cache_.get(), GURL("http://example.com/foo2"))); | 977 EXPECT_TRUE(CachePut(callback_cache_handle_->value(), |
| 954 EXPECT_TRUE( | 978 GURL("http://example.com/foo2"))); |
| 955 CacheMatch(callback_cache_.get(), GURL("http://example.com/foo2"))); | 979 EXPECT_TRUE(CacheMatch(callback_cache_handle_->value(), |
| 980 GURL("http://example.com/foo2"))); |
| 956 } | 981 } |
| 957 | 982 |
| 958 TEST_F(MigratedLegacyCacheDirectoryNameTest, | 983 TEST_F(MigratedLegacyCacheDirectoryNameTest, |
| 959 RandomDirectoryCacheSideBySideWithLegacy) { | 984 RandomDirectoryCacheSideBySideWithLegacy) { |
| 960 EXPECT_TRUE(Open(origin1_, legacy_cache_name_)); | 985 EXPECT_TRUE(Open(origin1_, legacy_cache_name_)); |
| 961 EXPECT_TRUE(Open(origin1_, "bar")); | 986 EXPECT_TRUE(Open(origin1_, "bar")); |
| 962 EXPECT_TRUE(CachePut(callback_cache_.get(), stored_url_)); | 987 EXPECT_TRUE(CachePut(callback_cache_handle_->value(), stored_url_)); |
| 963 EXPECT_TRUE(CacheMatch(callback_cache_.get(), stored_url_)); | 988 EXPECT_TRUE(CacheMatch(callback_cache_handle_->value(), stored_url_)); |
| 964 } | 989 } |
| 965 | 990 |
| 966 TEST_F(MigratedLegacyCacheDirectoryNameTest, DeleteLegacyCacheAndRecreateNew) { | 991 TEST_F(MigratedLegacyCacheDirectoryNameTest, DeleteLegacyCacheAndRecreateNew) { |
| 967 EXPECT_TRUE(Delete(origin1_, legacy_cache_name_)); | 992 EXPECT_TRUE(Delete(origin1_, legacy_cache_name_)); |
| 968 EXPECT_TRUE(Open(origin1_, legacy_cache_name_)); | 993 EXPECT_TRUE(Open(origin1_, legacy_cache_name_)); |
| 969 EXPECT_FALSE(CacheMatch(callback_cache_.get(), stored_url_)); | 994 EXPECT_FALSE(CacheMatch(callback_cache_handle_->value(), stored_url_)); |
| 970 EXPECT_TRUE(CachePut(callback_cache_.get(), GURL("http://example.com/foo2"))); | 995 EXPECT_TRUE(CachePut(callback_cache_handle_->value(), |
| 971 EXPECT_TRUE( | 996 GURL("http://example.com/foo2"))); |
| 972 CacheMatch(callback_cache_.get(), GURL("http://example.com/foo2"))); | 997 EXPECT_TRUE(CacheMatch(callback_cache_handle_->value(), |
| 998 GURL("http://example.com/foo2"))); |
| 973 } | 999 } |
| 974 | 1000 |
| 975 class CacheStorageQuotaClientTest : public CacheStorageManagerTest { | 1001 class CacheStorageQuotaClientTest : public CacheStorageManagerTest { |
| 976 protected: | 1002 protected: |
| 977 CacheStorageQuotaClientTest() {} | 1003 CacheStorageQuotaClientTest() {} |
| 978 | 1004 |
| 979 void SetUp() override { | 1005 void SetUp() override { |
| 980 CacheStorageManagerTest::SetUp(); | 1006 CacheStorageManagerTest::SetUp(); |
| 981 quota_client_.reset( | 1007 quota_client_.reset( |
| 982 new CacheStorageQuotaClient(cache_manager_->AsWeakPtr())); | 1008 new CacheStorageQuotaClient(cache_manager_->AsWeakPtr())); |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1061 bool MemoryOnly() override { return !GetParam(); } | 1087 bool MemoryOnly() override { return !GetParam(); } |
| 1062 }; | 1088 }; |
| 1063 | 1089 |
| 1064 TEST_P(CacheStorageQuotaClientTestP, QuotaID) { | 1090 TEST_P(CacheStorageQuotaClientTestP, QuotaID) { |
| 1065 EXPECT_EQ(storage::QuotaClient::kServiceWorkerCache, quota_client_->id()); | 1091 EXPECT_EQ(storage::QuotaClient::kServiceWorkerCache, quota_client_->id()); |
| 1066 } | 1092 } |
| 1067 | 1093 |
| 1068 TEST_P(CacheStorageQuotaClientTestP, QuotaGetOriginUsage) { | 1094 TEST_P(CacheStorageQuotaClientTestP, QuotaGetOriginUsage) { |
| 1069 EXPECT_EQ(0, QuotaGetOriginUsage(origin1_)); | 1095 EXPECT_EQ(0, QuotaGetOriginUsage(origin1_)); |
| 1070 EXPECT_TRUE(Open(origin1_, "foo")); | 1096 EXPECT_TRUE(Open(origin1_, "foo")); |
| 1071 EXPECT_TRUE(CachePut(callback_cache_.get(), GURL("http://example.com/foo"))); | 1097 EXPECT_TRUE(CachePut(callback_cache_handle_->value(), |
| 1098 GURL("http://example.com/foo"))); |
| 1072 EXPECT_LT(0, QuotaGetOriginUsage(origin1_)); | 1099 EXPECT_LT(0, QuotaGetOriginUsage(origin1_)); |
| 1073 } | 1100 } |
| 1074 | 1101 |
| 1075 TEST_P(CacheStorageQuotaClientTestP, QuotaGetOriginsForType) { | 1102 TEST_P(CacheStorageQuotaClientTestP, QuotaGetOriginsForType) { |
| 1076 EXPECT_EQ(0u, QuotaGetOriginsForType()); | 1103 EXPECT_EQ(0u, QuotaGetOriginsForType()); |
| 1077 EXPECT_TRUE(Open(origin1_, "foo")); | 1104 EXPECT_TRUE(Open(origin1_, "foo")); |
| 1078 EXPECT_TRUE(Open(origin1_, "bar")); | 1105 EXPECT_TRUE(Open(origin1_, "bar")); |
| 1079 EXPECT_TRUE(Open(origin2_, "foo")); | 1106 EXPECT_TRUE(Open(origin2_, "foo")); |
| 1080 EXPECT_EQ(2u, QuotaGetOriginsForType()); | 1107 EXPECT_EQ(2u, QuotaGetOriginsForType()); |
| 1081 } | 1108 } |
| 1082 | 1109 |
| 1083 TEST_P(CacheStorageQuotaClientTestP, QuotaGetOriginsForHost) { | 1110 TEST_P(CacheStorageQuotaClientTestP, QuotaGetOriginsForHost) { |
| 1084 EXPECT_EQ(0u, QuotaGetOriginsForHost("example.com")); | 1111 EXPECT_EQ(0u, QuotaGetOriginsForHost("example.com")); |
| 1085 EXPECT_TRUE(Open(GURL("http://example.com:8080"), "foo")); | 1112 EXPECT_TRUE(Open(GURL("http://example.com:8080"), "foo")); |
| 1086 EXPECT_TRUE(Open(GURL("http://example.com:9000"), "foo")); | 1113 EXPECT_TRUE(Open(GURL("http://example.com:9000"), "foo")); |
| 1087 EXPECT_TRUE(Open(GURL("ftp://example.com"), "foo")); | 1114 EXPECT_TRUE(Open(GURL("ftp://example.com"), "foo")); |
| 1088 EXPECT_TRUE(Open(GURL("http://example2.com"), "foo")); | 1115 EXPECT_TRUE(Open(GURL("http://example2.com"), "foo")); |
| 1089 EXPECT_EQ(3u, QuotaGetOriginsForHost("example.com")); | 1116 EXPECT_EQ(3u, QuotaGetOriginsForHost("example.com")); |
| 1090 EXPECT_EQ(1u, QuotaGetOriginsForHost("example2.com")); | 1117 EXPECT_EQ(1u, QuotaGetOriginsForHost("example2.com")); |
| 1091 EXPECT_TRUE(callback_origins_.find(GURL("http://example2.com")) != | 1118 EXPECT_TRUE(callback_origins_.find(GURL("http://example2.com")) != |
| 1092 callback_origins_.end()); | 1119 callback_origins_.end()); |
| 1093 EXPECT_EQ(0u, QuotaGetOriginsForHost("unknown.com")); | 1120 EXPECT_EQ(0u, QuotaGetOriginsForHost("unknown.com")); |
| 1094 } | 1121 } |
| 1095 | 1122 |
| 1096 TEST_P(CacheStorageQuotaClientTestP, QuotaDeleteOriginData) { | 1123 TEST_P(CacheStorageQuotaClientTestP, QuotaDeleteOriginData) { |
| 1097 EXPECT_EQ(0, QuotaGetOriginUsage(origin1_)); | 1124 EXPECT_EQ(0, QuotaGetOriginUsage(origin1_)); |
| 1098 EXPECT_TRUE(Open(origin1_, "foo")); | 1125 EXPECT_TRUE(Open(origin1_, "foo")); |
| 1099 // Call put to test that initialized caches are properly deleted too. | 1126 // Call put to test that initialized caches are properly deleted too. |
| 1100 EXPECT_TRUE(CachePut(callback_cache_.get(), GURL("http://example.com/foo"))); | 1127 EXPECT_TRUE(CachePut(callback_cache_handle_->value(), |
| 1128 GURL("http://example.com/foo"))); |
| 1101 EXPECT_TRUE(Open(origin1_, "bar")); | 1129 EXPECT_TRUE(Open(origin1_, "bar")); |
| 1102 EXPECT_TRUE(Open(origin2_, "baz")); | 1130 EXPECT_TRUE(Open(origin2_, "baz")); |
| 1103 | 1131 |
| 1104 int64_t origin1_size = QuotaGetOriginUsage(origin1_); | 1132 int64_t origin1_size = QuotaGetOriginUsage(origin1_); |
| 1105 EXPECT_LT(0, origin1_size); | 1133 EXPECT_LT(0, origin1_size); |
| 1106 | 1134 |
| 1107 EXPECT_TRUE(QuotaDeleteOriginData(origin1_)); | 1135 EXPECT_TRUE(QuotaDeleteOriginData(origin1_)); |
| 1108 | 1136 |
| 1109 EXPECT_EQ(-1 * origin1_size, quota_manager_proxy_->last_notified_delta()); | 1137 EXPECT_EQ(-1 * origin1_size, quota_manager_proxy_->last_notified_delta()); |
| 1110 EXPECT_EQ(0, QuotaGetOriginUsage(origin1_)); | 1138 EXPECT_EQ(0, QuotaGetOriginUsage(origin1_)); |
| 1111 EXPECT_FALSE(Has(origin1_, "foo")); | 1139 EXPECT_FALSE(Has(origin1_, "foo")); |
| 1112 EXPECT_FALSE(Has(origin1_, "bar")); | 1140 EXPECT_FALSE(Has(origin1_, "bar")); |
| 1113 EXPECT_TRUE(Has(origin2_, "baz")); | 1141 EXPECT_TRUE(Has(origin2_, "baz")); |
| 1114 EXPECT_TRUE(Open(origin1_, "foo")); | 1142 EXPECT_TRUE(Open(origin1_, "foo")); |
| 1115 } | 1143 } |
| 1116 | 1144 |
| 1117 TEST_P(CacheStorageQuotaClientTestP, QuotaDeleteEmptyOrigin) { | 1145 TEST_P(CacheStorageQuotaClientTestP, QuotaDeleteEmptyOrigin) { |
| 1118 EXPECT_TRUE(QuotaDeleteOriginData(origin1_)); | 1146 EXPECT_TRUE(QuotaDeleteOriginData(origin1_)); |
| 1119 } | 1147 } |
| 1120 | 1148 |
| 1121 TEST_F(CacheStorageQuotaClientDiskOnlyTest, QuotaDeleteUnloadedOriginData) { | 1149 TEST_F(CacheStorageQuotaClientDiskOnlyTest, QuotaDeleteUnloadedOriginData) { |
| 1122 EXPECT_TRUE(Open(origin1_, "foo")); | 1150 EXPECT_TRUE(Open(origin1_, "foo")); |
| 1123 // Call put to test that initialized caches are properly deleted too. | 1151 // Call put to test that initialized caches are properly deleted too. |
| 1124 EXPECT_TRUE(CachePut(callback_cache_.get(), GURL("http://example.com/foo"))); | 1152 EXPECT_TRUE(CachePut(callback_cache_handle_->value(), |
| 1153 GURL("http://example.com/foo"))); |
| 1125 | 1154 |
| 1126 // Close the cache backend so that it writes out its index to disk. | 1155 // Close the cache backend so that it writes out its index to disk. |
| 1127 base::RunLoop run_loop; | 1156 base::RunLoop run_loop; |
| 1128 callback_cache_->Close(run_loop.QuitClosure()); | 1157 callback_cache_handle_->value()->Close(run_loop.QuitClosure()); |
| 1129 run_loop.Run(); | 1158 run_loop.Run(); |
| 1130 | 1159 |
| 1131 // Create a new CacheStorageManager that hasn't yet loaded the origin. | 1160 // Create a new CacheStorageManager that hasn't yet loaded the origin. |
| 1132 quota_manager_proxy_->SimulateQuotaManagerDestroyed(); | 1161 quota_manager_proxy_->SimulateQuotaManagerDestroyed(); |
| 1133 cache_manager_ = CacheStorageManager::Create(cache_manager_.get()); | 1162 cache_manager_ = CacheStorageManager::Create(cache_manager_.get()); |
| 1134 quota_client_.reset(new CacheStorageQuotaClient(cache_manager_->AsWeakPtr())); | 1163 quota_client_.reset(new CacheStorageQuotaClient(cache_manager_->AsWeakPtr())); |
| 1135 | 1164 |
| 1136 EXPECT_TRUE(QuotaDeleteOriginData(origin1_)); | 1165 EXPECT_TRUE(QuotaDeleteOriginData(origin1_)); |
| 1137 EXPECT_EQ(0, QuotaGetOriginUsage(origin1_)); | 1166 EXPECT_EQ(0, QuotaGetOriginUsage(origin1_)); |
| 1138 } | 1167 } |
| 1139 | 1168 |
| 1140 TEST_P(CacheStorageQuotaClientTestP, QuotaDoesSupport) { | 1169 TEST_P(CacheStorageQuotaClientTestP, QuotaDoesSupport) { |
| 1141 EXPECT_TRUE(QuotaDoesSupport(storage::kStorageTypeTemporary)); | 1170 EXPECT_TRUE(QuotaDoesSupport(storage::kStorageTypeTemporary)); |
| 1142 EXPECT_FALSE(QuotaDoesSupport(storage::kStorageTypePersistent)); | 1171 EXPECT_FALSE(QuotaDoesSupport(storage::kStorageTypePersistent)); |
| 1143 EXPECT_FALSE(QuotaDoesSupport(storage::kStorageTypeSyncable)); | 1172 EXPECT_FALSE(QuotaDoesSupport(storage::kStorageTypeSyncable)); |
| 1144 EXPECT_FALSE(QuotaDoesSupport(storage::kStorageTypeQuotaNotManaged)); | 1173 EXPECT_FALSE(QuotaDoesSupport(storage::kStorageTypeQuotaNotManaged)); |
| 1145 EXPECT_FALSE(QuotaDoesSupport(storage::kStorageTypeUnknown)); | 1174 EXPECT_FALSE(QuotaDoesSupport(storage::kStorageTypeUnknown)); |
| 1146 } | 1175 } |
| 1147 | 1176 |
| 1148 INSTANTIATE_TEST_CASE_P(CacheStorageManagerTests, | 1177 INSTANTIATE_TEST_CASE_P(CacheStorageManagerTests, |
| 1149 CacheStorageManagerTestP, | 1178 CacheStorageManagerTestP, |
| 1150 ::testing::Values(false, true)); | 1179 ::testing::Values(false, true)); |
| 1151 | 1180 |
| 1152 INSTANTIATE_TEST_CASE_P(CacheStorageQuotaClientTests, | 1181 INSTANTIATE_TEST_CASE_P(CacheStorageQuotaClientTests, |
| 1153 CacheStorageQuotaClientTestP, | 1182 CacheStorageQuotaClientTestP, |
| 1154 ::testing::Values(false, true)); | 1183 ::testing::Values(false, true)); |
| 1155 | 1184 |
| 1156 } // namespace content | 1185 } // namespace content |
| OLD | NEW |