| Index: content/browser/cache_storage/cache_storage_manager_unittest.cc
|
| diff --git a/content/browser/cache_storage/cache_storage_manager_unittest.cc b/content/browser/cache_storage/cache_storage_manager_unittest.cc
|
| index f1fbe6cfe360f60a0cd9b97f9ae1ecd9d307a6e7..175236fdbc77db3c3d4837f95da1f181ff94ba3f 100644
|
| --- a/content/browser/cache_storage/cache_storage_manager_unittest.cc
|
| +++ b/content/browser/cache_storage/cache_storage_manager_unittest.cc
|
| @@ -50,6 +50,8 @@
|
| #include "storage/common/blob_storage/blob_handle.h"
|
| #include "testing/gtest/include/gtest/gtest.h"
|
|
|
| +using network::mojom::FetchResponseType;
|
| +
|
| namespace content {
|
|
|
| namespace {
|
| @@ -355,25 +357,31 @@ class CacheStorageManagerTest : public testing::Test {
|
| return callback_error_ == CACHE_STORAGE_OK;
|
| }
|
|
|
| - bool CachePut(CacheStorageCache* cache, const GURL& url) {
|
| + bool CachePut(CacheStorageCache* cache,
|
| + const GURL& url,
|
| + FetchResponseType response_type = FetchResponseType::kDefault) {
|
| ServiceWorkerFetchRequest request;
|
| request.url = url;
|
|
|
| - return CachePutWithStatusCode(cache, request, 200);
|
| + return CachePutWithStatusCode(cache, request, 200, response_type);
|
| }
|
|
|
| bool CachePutWithRequestAndHeaders(
|
| CacheStorageCache* cache,
|
| const ServiceWorkerFetchRequest& request,
|
| - const ServiceWorkerHeaderMap& response_headers) {
|
| - return CachePutWithStatusCode(cache, request, 200, response_headers);
|
| + const ServiceWorkerHeaderMap& response_headers,
|
| + FetchResponseType response_type = FetchResponseType::kDefault) {
|
| + return CachePutWithStatusCode(cache, request, 200, response_type,
|
| + response_headers);
|
| }
|
|
|
| - bool CachePutWithStatusCode(CacheStorageCache* cache,
|
| - const ServiceWorkerFetchRequest& request,
|
| - int status_code,
|
| - const ServiceWorkerHeaderMap& response_headers =
|
| - ServiceWorkerHeaderMap()) {
|
| + bool CachePutWithStatusCode(
|
| + CacheStorageCache* cache,
|
| + const ServiceWorkerFetchRequest& request,
|
| + int status_code,
|
| + FetchResponseType response_type = FetchResponseType::kDefault,
|
| + const ServiceWorkerHeaderMap& response_headers =
|
| + ServiceWorkerHeaderMap()) {
|
| std::unique_ptr<storage::BlobDataBuilder> blob_data(
|
| new storage::BlobDataBuilder(base::GenerateGUID()));
|
| blob_data->AppendData(request.url.spec());
|
| @@ -384,8 +392,7 @@ class CacheStorageManagerTest : public testing::Test {
|
| base::MakeUnique<std::vector<GURL>>();
|
| url_list->push_back(request.url);
|
| ServiceWorkerResponse response(
|
| - std::move(url_list), status_code, "OK",
|
| - network::mojom::FetchResponseType::kDefault,
|
| + std::move(url_list), status_code, "OK", response_type,
|
| base::MakeUnique<ServiceWorkerHeaderMap>(response_headers),
|
| blob_handle->uuid(), request.url.spec().size(), nullptr /* blob */,
|
| blink::kWebServiceWorkerResponseErrorUnknown, base::Time(),
|
| @@ -476,6 +483,28 @@ class CacheStorageManagerTest : public testing::Test {
|
| return callback_usage_;
|
| }
|
|
|
| + int64_t GetQuotaOriginUsage(const GURL& origin) {
|
| + int64_t usage(CacheStorage::kSizeUnknown);
|
| + base::RunLoop loop;
|
| + quota_manager_proxy_->GetUsageAndQuota(
|
| + base::ThreadTaskRunnerHandle::Get().get(), origin,
|
| + StorageType::kStorageTypeTemporary,
|
| + base::Bind(&CacheStorageManagerTest::DidGetQuotaOriginUsage,
|
| + base::Unretained(this), base::Unretained(&usage), &loop));
|
| + loop.Run();
|
| + return usage;
|
| + }
|
| +
|
| + void DidGetQuotaOriginUsage(int64_t* out_usage,
|
| + base::RunLoop* run_loop,
|
| + QuotaStatusCode status_code,
|
| + int64_t usage,
|
| + int64_t quota) {
|
| + if (status_code == storage::kQuotaStatusOk)
|
| + *out_usage = usage;
|
| + run_loop->Quit();
|
| + }
|
| +
|
| protected:
|
| // Temporary directory must be allocated first so as to be destroyed last.
|
| base::ScopedTempDir temp_dir_;
|
| @@ -925,6 +954,107 @@ TEST_F(CacheStorageManagerTest, CacheSizeCorrectAfterReopen) {
|
| EXPECT_EQ(size_before_close, Size(origin1_));
|
| }
|
|
|
| +TEST_F(CacheStorageManagerTest, CacheSizePaddedAfterReopen) {
|
| + const GURL kFooURL = origin1_.Resolve("foo");
|
| + const std::string kCacheName = "foo";
|
| +
|
| + int64_t put_delta = quota_manager_proxy_->last_notified_delta();
|
| + EXPECT_EQ(0, put_delta);
|
| + EXPECT_EQ(0, quota_manager_proxy_->notify_storage_modified_count());
|
| +
|
| + EXPECT_TRUE(Open(origin1_, kCacheName));
|
| + std::unique_ptr<CacheStorageCacheHandle> original_handle =
|
| + std::move(callback_cache_handle_);
|
| +
|
| + base::RunLoop().RunUntilIdle();
|
| + put_delta += quota_manager_proxy_->last_notified_delta();
|
| + EXPECT_EQ(0, put_delta);
|
| + EXPECT_EQ(0, quota_manager_proxy_->notify_storage_modified_count());
|
| +
|
| + EXPECT_TRUE(
|
| + CachePut(original_handle->value(), kFooURL, FetchResponseType::kOpaque));
|
| + int64_t cache_size_before_close = Size(origin1_);
|
| + base::FilePath storage_dir = original_handle->value()->path().DirName();
|
| + original_handle = nullptr;
|
| + EXPECT_GT(cache_size_before_close, 0);
|
| +
|
| + base::RunLoop().RunUntilIdle();
|
| + EXPECT_EQ(cache_size_before_close, GetQuotaOriginUsage(origin1_));
|
| +
|
| + base::RunLoop().RunUntilIdle();
|
| + put_delta = quota_manager_proxy_->last_notified_delta();
|
| + EXPECT_GT(put_delta, 0);
|
| + EXPECT_EQ(1, quota_manager_proxy_->notify_storage_modified_count());
|
| +
|
| + EXPECT_EQ(GetQuotaOriginUsage(origin1_), put_delta);
|
| +
|
| + // Close the caches and cache manager.
|
| + EXPECT_TRUE(FlushCacheStorageIndex(origin1_));
|
| + DestroyStorageManager();
|
| +
|
| + // Create a new CacheStorageManager that hasn't yet loaded the origin.
|
| + CreateStorageManager();
|
| + quota_manager_proxy_->SimulateQuotaManagerDestroyed();
|
| + cache_manager_ = CacheStorageManager::Create(cache_manager_.get());
|
| + EXPECT_TRUE(Open(origin1_, kCacheName));
|
| +
|
| + base::RunLoop().RunUntilIdle();
|
| + put_delta = quota_manager_proxy_->last_notified_delta();
|
| + EXPECT_EQ(0, put_delta);
|
| + EXPECT_EQ(0, quota_manager_proxy_->notify_storage_modified_count());
|
| +
|
| + EXPECT_EQ(cache_size_before_close, Size(origin1_));
|
| +}
|
| +
|
| +TEST_F(CacheStorageManagerTest, PersistedCacheKeyUsed) {
|
| + const GURL kFooURL = origin1_.Resolve("foo");
|
| + const std::string kCacheName = "foo";
|
| +
|
| + EXPECT_TRUE(Open(origin1_, kCacheName));
|
| + std::unique_ptr<CacheStorageCacheHandle> original_handle =
|
| + std::move(callback_cache_handle_);
|
| +
|
| + EXPECT_TRUE(
|
| + CachePut(original_handle->value(), kFooURL, FetchResponseType::kOpaque));
|
| +
|
| + int64_t cache_size_after_put = Size(origin1_);
|
| + EXPECT_LT(0, cache_size_after_put);
|
| +
|
| + // Close the caches and cache manager.
|
| + EXPECT_TRUE(FlushCacheStorageIndex(origin1_));
|
| + DestroyStorageManager();
|
| +
|
| + // GenerateNewKeyForTest isn't thread safe so
|
| + base::RunLoop().RunUntilIdle();
|
| + CacheStorage::GenerateNewKeyForTesting();
|
| +
|
| + // Create a new CacheStorageManager that hasn't yet loaded the origin.
|
| + CreateStorageManager();
|
| + quota_manager_proxy_->SimulateQuotaManagerDestroyed();
|
| + cache_manager_ = CacheStorageManager::Create(cache_manager_.get());
|
| +
|
| + // Reopening the origin/cache creates a new CacheStorage instance with a new
|
| + // random key.
|
| + EXPECT_TRUE(Open(origin1_, kCacheName));
|
| +
|
| + // Size (before any change) should be the same as before it was closed.
|
| + EXPECT_EQ(cache_size_after_put, Size(origin1_));
|
| +
|
| + // Delete the value. If the new padding key was used to deduct the padded size
|
| + // then after deletion we would expect to see a non-zero cache size.
|
| + EXPECT_TRUE(Delete(origin1_, "foo"));
|
| + EXPECT_EQ(0, Size(origin1_));
|
| +
|
| + // Now put the exact same resource back into the cache. This time we expect to
|
| + // see a different size as the padding is calculated with a different key.
|
| + std::unique_ptr<CacheStorageCacheHandle> new_handle =
|
| + std::move(callback_cache_handle_);
|
| + EXPECT_TRUE(
|
| + CachePut(new_handle->value(), kFooURL, FetchResponseType::kOpaque));
|
| +
|
| + EXPECT_NE(cache_size_after_put, Size(origin1_));
|
| +}
|
| +
|
| // With a memory cache the cache can't be freed from memory until the client
|
| // calls delete.
|
| TEST_F(CacheStorageManagerMemoryOnlyTest, MemoryLosesReferenceOnlyAfterDelete) {
|
|
|