chromium/components/services/storage/service_worker/service_worker_database_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 "components/services/storage/service_worker/service_worker_database.h"

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

#include <string>

#include "base/containers/contains.h"
#include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/path_service.h"
#include "base/strings/string_number_conversions.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/scoped_feature_list.h"
#include "base/unguessable_token.h"
#include "components/services/storage/service_worker/service_worker_database.pb.h"
#include "net/base/features.h"
#include "services/network/public/cpp/web_sandbox_flags.h"
#include "services/network/public/mojom/fetch_api.mojom-shared.h"
#include "services/network/public/mojom/referrer_policy.mojom-shared.h"
#include "services/network/public/mojom/web_sandbox_flags.mojom-shared.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/common/safe_url_pattern.h"
#include "third_party/blink/public/common/service_worker/service_worker_router_rule.h"
#include "third_party/blink/public/mojom/frame/policy_container.mojom.h"
#include "third_party/blink/public/mojom/service_worker/service_worker_object.mojom.h"
#include "third_party/blink/public/mojom/service_worker/service_worker_registration.mojom.h"
#include "third_party/leveldatabase/src/include/leveldb/write_batch.h"
#include "url/origin.h"

namespace storage {

namespace {

ElementsAreArray;
IsEmpty;

RegistrationData;
RegistrationDataPtr;
ResourceRecordPtr;

struct AvailableIds {};

GURL URL(const GURL& origin, const std::string& path) {}

ResourceRecordPtr CreateResource(int64_t resource_id,
                                 const GURL& url,
                                 uint64_t size_bytes) {}

ServiceWorkerDatabase* CreateDatabase(const base::FilePath& path) {}

ServiceWorkerDatabase* CreateDatabaseInMemory() {}

void VerifyRegistrationData(const RegistrationData& expected,
                            const RegistrationData& actual) {}

void VerifyResourceRecords(const std::vector<ResourceRecordPtr>& expected,
                           const std::vector<ResourceRecordPtr>& actual) {}

network::CrossOriginEmbedderPolicy CrossOriginEmbedderPolicyNone() {}

network::CrossOriginEmbedderPolicy CrossOriginEmbedderPolicyRequireCorp() {}

network::CrossOriginEmbedderPolicy CrossOriginEmbedderPolicyCredentialless() {}

std::vector<mojom::ServiceWorkerUserDataPtr> CreateUserData(
    int64_t registration_id,
    const std::vector<std::pair<std::string, std::string>>& key_value_pairs) {}

}  // namespace

TEST(ServiceWorkerDatabaseTest, OpenDatabase) {}

TEST(ServiceWorkerDatabaseTest, OpenDatabase_InMemory) {}

TEST(ServiceWorkerDatabaseTest, DatabaseVersion_ValidSchemaVersion) {}

TEST(ServiceWorkerDatabaseTest, DatabaseVersion_ObsoleteSchemaVersion) {}

TEST(ServiceWorkerDatabaseTest, DatabaseVersion_CorruptedSchemaVersion) {}

TEST(ServiceWorkerDatabaseTest, GetNextAvailableIds) {}

TEST(ServiceWorkerDatabaseTest, GetStorageKeysWithRegistrations) {}

TEST(ServiceWorkerDatabaseTest, GetRegistrationsForStorageKey) {}

TEST(ServiceWorkerDatabaseTest, GetAllRegistrations) {}

TEST(ServiceWorkerDatabaseTest, Registration_Basic) {}

TEST(ServiceWorkerDatabaseTest, DeleteNonExistentRegistration) {}

TEST(ServiceWorkerDatabaseTest, Registration_Overwrite) {}

TEST(ServiceWorkerDatabaseTest, Registration_Multiple) {}

TEST(ServiceWorkerDatabaseTest, Registration_UninitializedDatabase) {}

TEST(ServiceWorkerDatabaseTest, Registration_ScriptType) {}

TEST(ServiceWorkerDatabaseTest, UserData_Basic) {}

TEST(ServiceWorkerDatabaseTest,
     UserData_ReadUserDataForAllRegistrationsByKeyPrefix) {}

TEST(ServiceWorkerDatabaseTest, ReadUserDataByKeyPrefix) {}

TEST(ServiceWorkerDatabaseTest, ReadUserKeysAndDataByKeyPrefix) {}

TEST(ServiceWorkerDatabaseTest, UserData_DeleteUserDataByKeyPrefixes) {}

TEST(ServiceWorkerDatabaseTest,
     UserData_DeleteUserDataForAllRegistrationsByKeyPrefix) {}

TEST(ServiceWorkerDatabaseTest, UserData_DataIsolation) {}

TEST(ServiceWorkerDatabaseTest, UserData_DeleteRegistration) {}

TEST(ServiceWorkerDatabaseTest, UserData_UninitializedDatabase) {}

TEST(ServiceWorkerDatabaseTest, UpdateVersionToActive) {}

TEST(ServiceWorkerDatabaseTest, UpdateLastCheckTime) {}

TEST(ServiceWorkerDatabaseTest, UpdateFetchHandlerType) {}

TEST(ServiceWorkerDatabaseTest, UpdateResourceSha256Checksums) {}

TEST(ServiceWorkerDatabaseTest, UncommittedAndPurgeableResourceIds) {}

namespace {
class DeleteAllDataForStorageKeyTest {};

void DeleteAllDataForStorageKeyTest::TestDeleteAllDataForStorageKey(
    std::string registered_key_origin,
    std::string registered_key_top_level_site,
    blink::mojom::AncestorChainBit registered_key_ancestor_chain_bit,
    std::string deleted_origin,
    bool expect_key_deleted) {}

void DeleteAllDataForStorageKeyTest::
    TestDeleteAllDataForStorageKeyWithMultipleKeys() {}

}  // namespace

// Tests for first-party keys, which are parameterized on whether
// `kThirdPartyStoragePartitioning` is enabled or disabled.
class DeleteAllDataForStorageKeyFirstPartyP
    : public DeleteAllDataForStorageKeyTest,
      public testing::TestWithParam<bool> {};

// Tests for third-party keys, which only exist when
// `kThirdPartyStoragePartitioning` is disabled.
class ServiceWorkerDatabaseTestDeleteAllDataForStorageKeyThirdParty
    : public DeleteAllDataForStorageKeyTest,
      public testing::Test {};

// A simple first-party key that matches the deleted origin should be
// deleted.
TEST_P(DeleteAllDataForStorageKeyFirstPartyP, Matching) {}

// A simple first-party key that does not match the deleted origin should
// not be deleted.
TEST_P(DeleteAllDataForStorageKeyFirstPartyP, NonMatching) {}

// A first party key where the key's origin is a subsite of the deleted
// origin should not be deleted, regardless of 3PSP.
TEST_P(DeleteAllDataForStorageKeyFirstPartyP, WithSubsiteOrigin) {}

// A first-party key, where the deleted origin is a subsite of the
// registered key's origin, should not be deleted regardless of 3PSP.
TEST_P(DeleteAllDataForStorageKeyFirstPartyP, WithSupersiteOfDeletedOrigin) {}

// A first-party key with a subsite origin, where the deleted origin is the
// same subsite, should be deleted regardless of 3PSP.
TEST_P(DeleteAllDataForStorageKeyFirstPartyP,
       WithSubsiteMatchingDeletedOrigin) {}

TEST_P(DeleteAllDataForStorageKeyFirstPartyP, CallWithMultipleKeys) {}

// A third-party key with a subsite origin not matching the top-level site,
// where the deleted origin matches the key origin, should be deleted.
TEST_F(ServiceWorkerDatabaseTestDeleteAllDataForStorageKeyThirdParty,
       WithSubsiteMatchingDeletedOrigin) {}

// A third-party key with a subsite origin matching the top-level site,
// where the deleted origin matches the key origin, should be deleted.
TEST_F(ServiceWorkerDatabaseTestDeleteAllDataForStorageKeyThirdParty,
       WithSubsiteMatchingDeletedOriginMatchingTopLevelSite) {}

// A third-party key with an origin equal to the top-level site,
// where the deleted origin is a subsite, should not be deleted.
TEST_F(ServiceWorkerDatabaseTestDeleteAllDataForStorageKeyThirdParty,
       SupersiteOfDeletedOrigin) {}

// A third-party key with an origin equal to the top-level site,
// where the deleted origin is a subsite, should be deleted.
TEST_F(ServiceWorkerDatabaseTestDeleteAllDataForStorageKeyThirdParty,
       SupersiteOfDeletedOriginMatchingTopLevelSite) {}

// A third-party key for a subsite of the deleted key should not be
// deleted.
TEST_F(ServiceWorkerDatabaseTestDeleteAllDataForStorageKeyThirdParty,
       SubsiteOfDeletedOrigin) {}

// A third-party key (per ancestor chain bit) for a subsite of the deleted
// key should be deleted.
TEST_F(ServiceWorkerDatabaseTestDeleteAllDataForStorageKeyThirdParty,
       PerAncestorChainBitSubsiteOfDeletedOrigin) {}

// A third-party key where the origin (but not the top-level site) matches
// the deleted origin should be deleted.
TEST_F(ServiceWorkerDatabaseTestDeleteAllDataForStorageKeyThirdParty,
       WithMatchingOrigin) {}

// A third-party key (per ancestor chain bit) for the deleted origin should
// be deleted.
TEST_F(ServiceWorkerDatabaseTestDeleteAllDataForStorageKeyThirdParty,
       PerAncestorChainBitWithMatchingOrigin) {}

// A third-party key for an unrelated origin should not be deleted..
TEST_F(ServiceWorkerDatabaseTestDeleteAllDataForStorageKeyThirdParty,
       ForOtherOrigin) {}

// A third-party key where the top-level-site (but not the origin) matches
// the deleted origin should be deleted.
TEST_F(ServiceWorkerDatabaseTestDeleteAllDataForStorageKeyThirdParty,
       WithMatchingTopLevelSite) {}

INSTANTIATE_TEST_SUITE_P();

TEST(ServiceWorkerDatabaseTest, DestroyDatabase) {}

TEST(ServiceWorkerDatabaseTest, Corruption_NoMainResource) {}

// Tests that GetRegistrationsForStorageKey() detects corruption without
// crashing. It must delete the database after freeing the iterator it uses to
// read all registrations. Regression test for https://crbug.com/909024.
TEST(ServiceWorkerDatabaseTest, Corruption_GetRegistrationsForStorageKey) {}

// Test that invalid WebFeatures on disk are ignored when reading a
// registration. See https://crbug.com/965944.
TEST(ServiceWorkerDatabaseTest, InvalidWebFeature) {}

// Check that every field of CrossOriginEmbedderPolicy can be properly
// serialized and deserialized.
TEST(ServiceWorkerDatabaseTest, CrossOriginEmbedderPolicyStoreRestore) {}

TEST(ServiceWorkerDatabaseTest, NoCrossOriginEmbedderPolicyValue) {}

const network::mojom::WebSandboxFlags kWebSandboxFlags[] =;

static_assert;

// Check that every field of PolicyContainerPolicies can be properly
// serialized and deserialized.
TEST(ServiceWorkerDatabaseTest, PolicyContainerPoliciesStoreRestore) {}

// As part of crbug.com/1199077 ServiceWorkerDataBase was refactored to use
// blink::StorageKey instead of url::Origin/GURL. The refactor is/should be a
// no-op but this test exists to confirms that a DB created with the Origin/GURL
// impl can still be correctly read by the blink::StorageKey impl.
TEST(ServiceWorkerDatabaseTest, StorageKeyImplCanReadPreviousOriginImplDB) {}

TEST(ServiceWorkerDatabaseTest, NoFetchHandlerType) {}

TEST(ServiceWorkerDatabaseTest, FetchHandlerType) {}

TEST(ServiceWorkerDatabaseTest, FetchHandlerTypeStoreRestore) {}

TEST(ServiceWorkerDatabaseTest, RouterRulesStoreRestore) {}

TEST(ServiceWorkerDatabaseTest, RouterRulesLegacyPathname) {}

}  // namespace storage