chromium/content/browser/cache_storage/cache_storage_cache_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.

#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/342213636): Remove this and spanify to fix the errors.
#pragma allow_unsafe_buffers
#endif

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

#include <limits>
#include <memory>
#include <set>
#include <utility>

#include "base/containers/contains.h"
#include "base/files/file_path.h"
#include "base/files/scoped_temp_dir.h"
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/memory/ptr_util.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/ref_counted.h"
#include "base/notreached.h"
#include "base/run_loop.h"
#include "base/strings/strcat.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_split.h"
#include "base/task/sequenced_task_runner.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/test_future.h"
#include "base/time/time.h"
#include "build/build_config.h"
#include "components/services/storage/public/cpp/buckets/constants.h"
#include "components/services/storage/public/mojom/cache_storage_control.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_cache.h"
#include "content/browser/cache_storage/cache_storage_cache_handle.h"
#include "content/browser/cache_storage/cache_storage_histogram_utils.h"
#include "content/browser/cache_storage/cache_storage_manager.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 "crypto/symmetric_key.h"
#include "mojo/public/cpp/bindings/remote.h"
#include "mojo/public/cpp/bindings/self_owned_receiver.h"
#include "mojo/public/cpp/system/data_pipe.h"
#include "mojo/public/cpp/system/data_pipe_drainer.h"
#include "net/base/test_completion_callback.h"
#include "net/base/url_util.h"
#include "net/disk_cache/disk_cache.h"
#include "net/http/http_connection_info.h"
#include "storage/browser/blob/blob_storage_context.h"
#include "storage/browser/quota/quota_manager_proxy.h"
#include "storage/browser/test/blob_test_utils.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/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/mojom/fetch/fetch_api_request.mojom.h"
#include "url/origin.h"

FetchAPIRequestHeadersMap;
CacheStorageError;
CacheStorageVerboseErrorPtr;

namespace content {
namespace cache_storage_cache_unittest {

const char kTestData[] =;
const char kCacheName[] =;
const FetchAPIRequestHeadersMap kHeaders({});

void SizeCallback(base::RunLoop* run_loop,
                  bool* callback_called,
                  int64_t* out_size,
                  int64_t size) {}

// A blob that never finishes writing to its pipe.
class SlowBlob : public storage::FakeBlob {};

// A disk_cache::Backend wrapper that can delay operations.
class DelayableBackend : public disk_cache::Backend {};

class FailableCacheEntry : public disk_cache::Entry {};

class FailableBackend : public disk_cache::Backend {};

std::string CopySideData(blink::mojom::Blob* actual_blob) {}

bool ResponseMetadataEqual(const blink::mojom::FetchAPIResponse& expected,
                           const blink::mojom::FetchAPIResponse& actual) {}

blink::mojom::FetchAPIResponsePtr SetCacheName(
    blink::mojom::FetchAPIResponsePtr response) {}

void OnBadMessage(std::string* result) {}

// A CacheStorageCache that can optionally delay during backend creation.
class TestCacheStorageCache : public CacheStorageCache {};

class MockCacheStorage : public CacheStorage {};

class CacheStorageCacheTest : public testing::Test {};

class CacheStorageCacheTestP : public CacheStorageCacheTest,
                               public testing::WithParamInterface<bool> {};

TEST_P(CacheStorageCacheTestP, PutNoBody) {}

TEST_P(CacheStorageCacheTestP, PutBody) {}

TEST_P(CacheStorageCacheTestP, PutBody_Multiple) {}

TEST_P(CacheStorageCacheTestP, MatchLimit) {}

TEST_P(CacheStorageCacheTestP, MatchAllLimit) {}

TEST_P(CacheStorageCacheTestP, MatchWithFragment) {}

TEST_P(CacheStorageCacheTestP, KeysLimit) {}

// TODO(nhiroki): Add a test for the case where one of PUT operations fails.
// Currently there is no handy way to fail only one operation in a batch.
// This could be easily achieved after adding some security checks in the
// browser side (http://crbug.com/425505).

TEST_P(CacheStorageCacheTestP, ResponseURLDiffersFromRequestURL) {}

TEST_P(CacheStorageCacheTestP, ResponseURLEmpty) {}

TEST_P(CacheStorageCacheTestP, PutBodyDropBlobRef) {}

TEST_P(CacheStorageCacheTestP, PutBadMessage) {}

TEST_P(CacheStorageCacheTestP, PutReplace) {}

TEST_P(CacheStorageCacheTestP, PutReplaceInBatchFails) {}

TEST_P(CacheStorageCacheTestP, MatchNoBody) {}

TEST_P(CacheStorageCacheTestP, MatchBody) {}

TEST_P(CacheStorageCacheTestP, MatchBodyHead) {}

TEST_P(CacheStorageCacheTestP, MatchAll_Empty) {}

TEST_P(CacheStorageCacheTestP, MatchAll_NoBody) {}

TEST_P(CacheStorageCacheTestP, MatchAll_Body) {}

TEST_P(CacheStorageCacheTestP, MatchAll_TwoResponsesThenOne) {}

TEST_P(CacheStorageCacheTestP, Match_IgnoreSearch) {}

TEST_P(CacheStorageCacheTestP, Match_IgnoreMethod) {}

TEST_P(CacheStorageCacheTestP, Match_IgnoreVary) {}

TEST_P(CacheStorageCacheTestP, GetAllMatchedEntries_RequestsIncluded) {}

TEST_P(CacheStorageCacheTestP, Keys_IgnoreSearch) {}

TEST_P(CacheStorageCacheTestP, Keys_IgnoreMethod) {}

TEST_P(CacheStorageCacheTestP, Keys_IgnoreVary) {}

TEST_P(CacheStorageCacheTestP, Delete_IgnoreSearch) {}

TEST_P(CacheStorageCacheTestP, Delete_IgnoreMethod) {}

TEST_P(CacheStorageCacheTestP, Delete_IgnoreVary) {}

TEST_P(CacheStorageCacheTestP, MatchAll_IgnoreMethod) {}

TEST_P(CacheStorageCacheTestP, MatchAll_IgnoreVary) {}

TEST_P(CacheStorageCacheTestP, MatchAll_IgnoreSearch) {}

TEST_P(CacheStorageCacheTestP, MatchAll_Head) {}

TEST_P(CacheStorageCacheTestP, Vary) {}

TEST_P(CacheStorageCacheTestP, EmptyVary) {}

TEST_P(CacheStorageCacheTestP, NoVaryButDiffHeaders) {}

TEST_P(CacheStorageCacheTestP, VaryMultiple) {}

TEST_P(CacheStorageCacheTestP, VaryNewHeader) {}

TEST_P(CacheStorageCacheTestP, VaryStar) {}

TEST_P(CacheStorageCacheTestP, EmptyKeys) {}

TEST_P(CacheStorageCacheTestP, TwoKeys) {}

TEST_P(CacheStorageCacheTestP, TwoKeysThenOne) {}

TEST_P(CacheStorageCacheTestP, KeysWithIgnoreSearchTrue) {}

TEST_P(CacheStorageCacheTestP, KeysWithIgnoreSearchFalse) {}

TEST_P(CacheStorageCacheTestP, DeleteNoBody) {}

TEST_P(CacheStorageCacheTestP, DeleteBody) {}

TEST_P(CacheStorageCacheTestP, DeleteWithIgnoreSearchTrue) {}

TEST_P(CacheStorageCacheTestP, DeleteWithIgnoreSearchFalse) {}

TEST_P(CacheStorageCacheTestP, QuickStressNoBody) {}

TEST_P(CacheStorageCacheTestP, QuickStressBody) {}

TEST_P(CacheStorageCacheTestP, PutResponseType) {}

TEST_P(CacheStorageCacheTestP, PutWithSideData) {}

TEST_P(CacheStorageCacheTestP, PutWithSideData_QuotaExceeded) {}

TEST_P(CacheStorageCacheTestP, PutWithSideData_QuotaExceededSkipSideData) {}

TEST_P(CacheStorageCacheTestP, PutWithSideData_BadMessage) {}

TEST_P(CacheStorageCacheTestP, WriteSideData) {}

TEST_P(CacheStorageCacheTestP, WriteSideData_QuotaExceeded) {}

TEST_P(CacheStorageCacheTestP, WriteSideData_QuotaManagerModified) {}

TEST_P(CacheStorageCacheTestP, WriteSideData_DifferentTimeStamp) {}

TEST_P(CacheStorageCacheTestP, WriteSideData_NotFound) {}

TEST_F(CacheStorageCacheTest, CaselessServiceWorkerFetchRequestHeaders) {}

TEST_P(CacheStorageCacheTestP, QuotaManagerModified) {}

TEST_P(CacheStorageCacheTestP, PutObeysQuotaLimits) {}

TEST_P(CacheStorageCacheTestP, PutObeysBucketQuotaLimits) {}

TEST_P(CacheStorageCacheTestP, PutObeysQuotaLimitsWithEmptyResponse) {}

TEST_P(CacheStorageCacheTestP, PutSafeSpaceIsEnough) {}

TEST_P(CacheStorageCacheTestP, PutRequestUrlObeysQuotaLimits) {}

TEST_P(CacheStorageCacheTestP, PutRequestMethodObeysQuotaLimits) {}

TEST_P(CacheStorageCacheTestP, PutRequestHeadersObeyQuotaLimits) {}

TEST_P(CacheStorageCacheTestP, PutResponseStatusObeysQuotaLimits) {}

TEST_P(CacheStorageCacheTestP, PutResponseBlobObeysQuotaLimits) {}

TEST_P(CacheStorageCacheTestP, PutResponseHeadersObeyQuotaLimits) {}

TEST_P(CacheStorageCacheTestP, PutResponseCorsHeadersObeyQuotaLimits) {}

TEST_P(CacheStorageCacheTestP, PutResponseUrlListObeysQuotaLimits) {}

TEST_P(CacheStorageCacheTestP, PutObeysQuotaLimitsWithEmptyResponseTinyQuota) {}

TEST_P(CacheStorageCacheTestP, Size) {}

TEST_F(CacheStorageCacheTest, VerifyOpaqueSizePadding) {}

TEST_F(CacheStorageCacheTest, TestDifferentOpaqueSideDataSizes) {}

TEST_F(CacheStorageCacheTest, TestDoubleOpaquePut) {}

TEST_P(CacheStorageCacheTestP, GetSizeThenClose) {}

TEST_P(CacheStorageCacheTestP, OpsFailOnClosedBackend) {}

// Shutdown the cache in the middle of its writing the response body. Upon
// restarting, that response shouldn't be available. See crbug.com/617683.
TEST_P(CacheStorageCacheTestP, UnfinishedPutsShouldNotBeReusable) {}

TEST_P(CacheStorageCacheTestP, BlobReferenceDelaysClose) {}

TEST_P(CacheStorageCacheTestP, VerifySerialScheduling) {}

#if BUILDFLAG(IS_WIN)
// TODO(crbug.com/41443751): Flaky on Windows.
#define MAYBE_KeysWithManyCacheEntries
#else
#define MAYBE_KeysWithManyCacheEntries
#endif
TEST_P(CacheStorageCacheTestP, MAYBE_KeysWithManyCacheEntries) {}

TEST_P(CacheStorageCacheTestP, SelfRefsDuringMatch) {}

TEST_P(CacheStorageCacheTestP, SelfRefsDuringMatchAll) {}

TEST_P(CacheStorageCacheTestP, SelfRefsDuringWriteSideData) {}

TEST_P(CacheStorageCacheTestP, SelfRefsDuringBatchOperation) {}

TEST_P(CacheStorageCacheTestP, SelfRefsDuringKeys) {}

TEST_P(CacheStorageCacheTestP, SelfRefsDuringPut) {}

TEST_P(CacheStorageCacheTestP, PutFailCreateEntry) {}

TEST_P(CacheStorageCacheTestP, PutFailWriteHeaders) {}

INSTANTIATE_TEST_SUITE_P();
}  // namespace cache_storage_cache_unittest
}  // namespace content