chromium/content/browser/cache_storage/cache_storage_manager_unittest.cc

// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "content/browser/cache_storage/cache_storage_manager.h"

#include <stddef.h>
#include <stdint.h>

#include <cstdint>
#include <list>
#include <utility>
#include <vector>

#include "base/containers/flat_map.h"
#include "base/containers/span.h"
#include "base/files/file_enumerator.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/functional/bind.h"
#include "base/memory/ptr_util.h"
#include "base/memory/scoped_refptr.h"
#include "base/path_service.h"
#include "base/run_loop.h"
#include "base/strings/string_number_conversions.h"
#include "base/task/single_thread_task_runner.h"
#include "base/test/bind.h"
#include "base/test/gmock_expected_support.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/test_future.h"
#include "base/time/time.h"
#include "base/uuid.h"
#include "build/build_config.h"
#include "components/services/storage/public/cpp/buckets/bucket_info.h"
#include "components/services/storage/public/cpp/buckets/bucket_locator.h"
#include "components/services/storage/public/cpp/buckets/constants.h"
#include "components/services/storage/public/cpp/constants.h"
#include "components/services/storage/public/cpp/quota_error_or.h"
#include "components/services/storage/public/mojom/cache_storage_control.mojom.h"
#include "components/services/storage/public/mojom/storage_usage_info.mojom.h"
#include "content/browser/blob_storage/chrome_blob_storage_context.h"
#include "content/browser/cache_storage/cache_storage.h"
#include "content/browser/cache_storage/cache_storage.pb.h"
#include "content/browser/cache_storage/cache_storage_cache_handle.h"
#include "content/browser/cache_storage/cache_storage_context_impl.h"
#include "content/browser/cache_storage/cache_storage_quota_client.h"
#include "content/browser/cache_storage/cache_storage_scheduler.h"
#include "content/common/background_fetch/background_fetch_types.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/test/browser_task_environment.h"
#include "content/public/test/test_browser_context.h"
#include "content/public/test/test_utils.h"
#include "mojo/public/cpp/bindings/pending_remote.h"
#include "mojo/public/cpp/bindings/receiver.h"
#include "mojo/public/cpp/bindings/remote.h"
#include "net/base/features.h"
#include "net/base/schemeful_site.h"
#include "net/disk_cache/disk_cache.h"
#include "net/http/http_connection_info.h"
#include "services/network/public/mojom/fetch_api.mojom.h"
#include "storage/browser/blob/blob_storage_context.h"
#include "storage/browser/quota/quota_client_type.h"
#include "storage/browser/quota/quota_manager_proxy.h"
#include "storage/browser/test/fake_blob.h"
#include "storage/browser/test/mock_quota_manager_proxy.h"
#include "storage/browser/test/mock_special_storage_policy.h"
#include "storage/common/quota/padding_key.h"
#include "testing/gmock/include/gmock/gmock-matchers.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/common/storage_key/storage_key.h"
#include "third_party/blink/public/mojom/cache_storage/cache_storage.mojom.h"
#include "third_party/blink/public/mojom/fetch/fetch_api_request.mojom.h"
#include "url/gurl.h"
#include "url/origin.h"

CacheStorageError;
CacheStorageVerboseErrorPtr;
FetchResponseType;

namespace content {
namespace cache_storage_manager_unittest {

enum class TestStorage {};

enum class StorageKeyAndBucketTestCase {};

StorageType;
ResponseHeaderMap;

class DelayedBlob : public storage::FakeBlob {};

// Scheduler implementation that will invoke a callback after the
// next operation has been started.
class CallbackScheduler : public CacheStorageScheduler {};

class MockCacheStorageQuotaManagerProxy
    : public storage::MockQuotaManagerProxy {};

bool IsIndexFileCurrent(const base::FilePath& cache_dir) {}

class TestCacheStorageObserver : public storage::mojom::CacheStorageObserver {};

class CacheStorageManagerTest : public testing::Test {};

class CacheStorageManagerMemoryOnlyTest : public CacheStorageManagerTest {};

class CacheStorageManagerTestP
    : public CacheStorageManagerTest,
      public testing::WithParamInterface<TestStorage> {};

class CacheStorageManagerStorageKeyAndBucketTestP
    : public CacheStorageManagerTest,
      public testing::WithParamInterface<StorageKeyAndBucketTestCase> {};

TEST_F(CacheStorageManagerTest, TestsRunOnIOThread) {}

TEST_P(CacheStorageManagerTestP, OpenCache) {}

TEST_P(CacheStorageManagerTestP, OpenTwoCaches) {}

TEST_P(CacheStorageManagerTestP, OpenSameCacheDifferentOwners) {}

TEST_P(CacheStorageManagerTestP, CachePointersDiffer) {}

TEST_P(CacheStorageManagerTestP, Open2CachesSameNameDiffKeys) {}

TEST_P(CacheStorageManagerTestP, OpenExistingCache) {}

TEST_P(CacheStorageManagerTestP, HasCache) {}

TEST_P(CacheStorageManagerTestP, HasCacheDifferentOwners) {}

TEST_P(CacheStorageManagerTestP, HasNonExistent) {}

TEST_P(CacheStorageManagerTestP, DeleteCache) {}

TEST_P(CacheStorageManagerTestP, DeleteTwice) {}

TEST_P(CacheStorageManagerTestP, DeleteCacheReducesKeySize) {}

TEST_P(CacheStorageManagerTestP, EmptyKeys) {}

TEST_P(CacheStorageManagerTestP, SomeKeys) {}

TEST_P(CacheStorageManagerTestP, DeletedKeysGone) {}

TEST_P(CacheStorageManagerTestP, StorageMatchEntryExists) {}

TEST_P(CacheStorageManagerTestP, StorageMatchNoEntry) {}

TEST_P(CacheStorageManagerTestP, StorageMatchNoCache) {}

TEST_P(CacheStorageManagerTestP, StorageMatchAllEntryExists) {}

TEST_P(CacheStorageManagerTestP, StorageMatchAllNoEntry) {}

TEST_P(CacheStorageManagerTestP, StorageMatchAllNoCaches) {}

TEST_P(CacheStorageManagerTestP, StorageMatchDifferentOwners) {}

TEST_F(CacheStorageManagerTest, StorageReuseCacheName) {}

TEST_P(CacheStorageManagerTestP, DropRefAfterNewCacheWithSameNameCreated) {}

TEST_P(CacheStorageManagerTestP, DeleteCorrectDirectory) {}

TEST_P(CacheStorageManagerTestP, StorageMatchAllEntryExistsTwice) {}

TEST_P(CacheStorageManagerTestP, StorageMatchInOneOfMany) {}

TEST_P(CacheStorageManagerTestP, Chinese) {}

TEST_F(CacheStorageManagerTest, EmptyKey) {}

TEST_P(CacheStorageManagerStorageKeyAndBucketTestP, DataPersists) {}

TEST_F(CacheStorageManagerMemoryOnlyTest, DataLostWhenMemoryOnly) {}

TEST_F(CacheStorageManagerTest, BadCacheName) {}

TEST_F(CacheStorageManagerTest, BadKeyName) {}

// Dropping a reference to a cache should not immediately destroy it.  These
// warm cache objects are kept alive to optimize the next open.
TEST_F(CacheStorageManagerTest, DropReference) {}

// Deleting a cache should remove any warmed caches that been kept alive
// without a reference.
TEST_F(CacheStorageManagerTest, DropReferenceAndDelete) {}

// Critical memory pressure should remove any warmed caches that been kept
// alive without a reference.
TEST_F(CacheStorageManagerTest, DropReferenceAndMemoryPressure) {}

TEST_F(CacheStorageManagerTest, DropReferenceDuringQuery) {}
// A cache continues to work so long as there is a handle to it. Only after the
// last cache handle is deleted can the cache be freed.
TEST_P(CacheStorageManagerTestP, CacheWorksAfterDelete) {}

// Deleted caches can still be modified, but all changes will eventually be
// thrown away when all references are released.
TEST_F(CacheStorageManagerTest, DeletedCacheIgnoredInIndex) {}

TEST_F(CacheStorageManagerTest, TestErrorInitializingCache) {}

TEST_P(CacheStorageManagerStorageKeyAndBucketTestP,
       CacheSizeCorrectAfterReopen) {}

TEST_F(CacheStorageManagerTest, CacheSizePaddedAfterReopen) {}

TEST_F(CacheStorageManagerTest, QuotaCorrectAfterReopen) {}

// With a memory cache the cache can't be freed from memory until the client
// calls delete.
TEST_F(CacheStorageManagerMemoryOnlyTest, MemoryLosesReferenceOnlyAfterDelete) {}

TEST_P(CacheStorageManagerTestP, DeleteBeforeRelease) {}

TEST_P(CacheStorageManagerTestP, OpenRunsSerially) {}

TEST_P(CacheStorageManagerTestP, GetBucketUsage) {}

TEST_P(CacheStorageManagerStorageKeyAndBucketTestP, GetBucketUsage) {}

TEST_F(CacheStorageManagerTest, GetBucketUsageConflictingBucketIds) {}

// TODO(crbug.com/40868994): Re-enable test for Windows.
#if BUILDFLAG(IS_WIN)
#define MAYBE_GetBucketUsageWithPadding
#else
#define MAYBE_GetBucketUsageWithPadding
#endif
TEST_P(CacheStorageManagerStorageKeyAndBucketTestP,
       MAYBE_GetBucketUsageWithPadding) {}

TEST_F(CacheStorageManagerTest, GetBucketUsageWithOldIndex) {}

TEST_P(CacheStorageManagerTestP, GetStorageKeysIgnoresKeysFromNamedBuckets) {}

TEST_F(CacheStorageManagerTest, GetKeySizeWithOldIndex) {}

TEST_P(CacheStorageManagerTestP, GetSizeThenCloseAllCaches) {}

TEST_P(CacheStorageManagerTestP, GetSizeThenCloseAllCachesTwoOwners) {}

TEST_P(CacheStorageManagerTestP, GetSizeThenCloseAllCachesAfterDelete) {}

TEST_F(CacheStorageManagerTest, DeleteUnreferencedCacheDirectories) {}

TEST_P(CacheStorageManagerTestP, OpenCacheStorageAccessed) {}

TEST_P(CacheStorageManagerTestP, HasStorageAccessed) {}

TEST_P(CacheStorageManagerTestP, DeleteStorageAccessed) {}

TEST_P(CacheStorageManagerTestP, KeysStorageAccessed) {}

TEST_P(CacheStorageManagerTestP, MatchCacheStorageAccessed) {}

TEST_P(CacheStorageManagerTestP, MatchAllCachesStorageAccessed) {}

TEST_P(CacheStorageManagerTestP, SizeStorageAccessed) {}

TEST_P(CacheStorageManagerTestP, SizeThenCloseStorageAccessed) {}

TEST_P(CacheStorageManagerTestP, NotifyCacheListChanged_Created) {}

TEST_P(CacheStorageManagerTestP, NotifyCacheListChanged_Deleted) {}

TEST_P(CacheStorageManagerTestP, NotifyCacheListChanged_DeletedThenCreated) {}

TEST_P(CacheStorageManagerTestP, NotifyCacheContentChanged_PutEntry) {}

TEST_P(CacheStorageManagerTestP, NotifyCacheContentChanged_DeleteEntry) {}

TEST_P(CacheStorageManagerTestP, NotifyCacheContentChanged_DeleteThenPutEntry) {}

TEST_P(CacheStorageManagerTestP, StorageMatch_IgnoreSearch) {}

TEST_P(CacheStorageManagerTestP, StorageMatch_IgnoreMethod) {}

TEST_P(CacheStorageManagerTestP, StorageMatch_IgnoreVary) {}

TEST_P(CacheStorageManagerTestP, StorageMatchAll_IgnoreSearch) {}

TEST_P(CacheStorageManagerTestP, StorageMatchAll_IgnoreMethod) {}

TEST_P(CacheStorageManagerTestP, StorageMatchAll_IgnoreVary) {}

TEST_P(CacheStorageManagerTestP, StorageWriteToCache) {}

TEST_F(CacheStorageManagerTest, WriteIndexOnlyScheduledWhenValueChanges) {}

TEST_P(CacheStorageManagerTestP, SlowPutCompletesWithoutExternalRef) {}

TEST_P(CacheStorageManagerTestP, StoragePutPartialContentForBackgroundFetch) {}

TEST_P(CacheStorageManagerTestP, DeleteOriginDataEmptyList) {}

TEST_P(CacheStorageManagerTestP, BatchDeleteOriginData) {}

TEST_F(CacheStorageManagerMemoryOnlyTest, DeleteBucketData) {}

TEST_P(CacheStorageManagerStorageKeyAndBucketTestP, DeleteBucketData) {}

TEST_F(CacheStorageManagerTest, DeleteBucketDataConflictingBucketIds) {}

class CacheStorageQuotaClientTest : public CacheStorageManagerTest {};

class CacheStorageQuotaClientDiskOnlyTest : public CacheStorageQuotaClientTest {};

class CacheStorageQuotaClientTestP
    : public CacheStorageQuotaClientTest,
      public testing::WithParamInterface<TestStorage> {};

TEST_P(CacheStorageQuotaClientTestP, QuotaGetBucketUsage) {}

TEST_P(CacheStorageQuotaClientTestP, QuotaGetStorageKeysForType) {}

TEST_P(CacheStorageQuotaClientTestP,
       QuotaGetStorageKeysForTypeDifferentOwners) {}

TEST_P(CacheStorageQuotaClientTestP, QuotaDeleteBucketData) {}

TEST_P(CacheStorageQuotaClientTestP, QuotaNonDefaultBucket) {}

TEST_P(CacheStorageQuotaClientTestP, QuotaDeleteEmptyBucket) {}

TEST_F(CacheStorageQuotaClientDiskOnlyTest, QuotaDeleteUnloadedKeyData) {}

class CacheStorageIndexMigrationTest : public CacheStorageManagerTest {};

TEST_F(CacheStorageIndexMigrationTest, PaddingMigration) {}

TEST_F(CacheStorageIndexMigrationTest, BucketMigration) {}

TEST_F(CacheStorageIndexMigrationTest, InvalidBucketId) {}

INSTANTIATE_TEST_SUITE_P();

INSTANTIATE_TEST_SUITE_P();

INSTANTIATE_TEST_SUITE_P();

}  // namespace cache_storage_manager_unittest
}  // namespace content