chromium/components/content_settings/core/browser/cookie_settings_unittest.cc

// Copyright 2012 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/content_settings/core/browser/cookie_settings.h"

#include <cstddef>
#include <memory>
#include <string>
#include <tuple>

#include "base/feature_list.h"
#include "base/memory/raw_ptr.h"
#include "base/scoped_observation.h"
#include "base/test/bind.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/task_environment.h"
#include "base/values.h"
#include "build/build_config.h"
#include "components/content_settings/core/browser/content_settings_registry.h"
#include "components/content_settings/core/browser/host_content_settings_map.h"
#include "components/content_settings/core/common/content_settings.h"
#include "components/content_settings/core/common/content_settings_pattern.h"
#include "components/content_settings/core/common/content_settings_utils.h"
#include "components/content_settings/core/common/features.h"
#include "components/content_settings/core/common/pref_names.h"
#include "components/content_settings/core/test/content_settings_mock_provider.h"
#include "components/content_settings/core/test/content_settings_test_utils.h"
#include "components/prefs/testing_pref_service.h"
#include "components/privacy_sandbox/privacy_sandbox_features.h"
#include "components/privacy_sandbox/privacy_sandbox_prefs.h"
#include "components/privacy_sandbox/tracking_protection_prefs.h"
#include "components/privacy_sandbox/tracking_protection_settings.h"
#include "components/sync_preferences/testing_pref_service_syncable.h"
#include "components/tpcd/metadata/browser/manager.h"
#include "components/tpcd/metadata/browser/parser.h"
#include "extensions/buildflags/buildflags.h"
#include "net/cookies/cookie_constants.h"
#include "net/cookies/cookie_setting_override.h"
#include "net/cookies/cookie_util.h"
#include "net/cookies/site_for_cookies.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"
#include "url/origin.h"
#include "url/url_constants.h"

#if BUILDFLAG(IS_IOS)
#include "components/content_settings/core/common/features.h"
#else
#include "components/privacy_sandbox/privacy_sandbox_features.h"
#endif

namespace {
ProviderType;

const bool kSupports3pcBlocking =;

#if !BUILDFLAG(IS_IOS)
constexpr char kAllowedRequestsHistogram[] =;
#endif

// To avoid an explosion of test cases, please don't just add a boolean to
// the test features. Consider whether features can interact with each other and
// whether you really need all combinations.

// Controls features that can unblock 3p cookies.
enum GrantSource {};

class TestTpcdManagerDelegate : public tpcd::metadata::Manager::Delegate {};

}  // namespace

namespace content_settings {
class CookieSettingsObserver : public CookieSettings::Observer {};

class CookieSettingsTestBase : public testing::Test {};

// Default test class to be used by most tests. If you want to add a new
// parameter, consider whether all test cases actually require this parameter
// or whether it is sufficient to add a new subclass of CookieSettingsTestBase.
class CookieSettingsTest : public CookieSettingsTestBase {};

// Parameterized class that tests combinations of StorageAccess grants and 3pcd
// grants. Tests that don't need the whole range of combinations should create
// their own parameterized subclasses.
class CookieSettingsTestP : public CookieSettingsTestBase,
                            public testing::WithParamInterface<GrantSource> {};

TEST_F(CookieSettingsTest, CustomExceptionsNoWildcardLessSpecificDomain) {}

TEST_F(CookieSettingsTest, CustomExceptionsNoWildcardMatchingDomain) {}

TEST_F(CookieSettingsTest, CustomExceptionsWildcardMatchingDomain) {}

TEST_F(CookieSettingsTest, CustomExceptionsWildcardLessSpecificDomain) {}

TEST_F(CookieSettingsTest, CustomExceptionsDotComWildcard) {}

TEST_F(CookieSettingsTest, TestAllowlistedScheme) {}

TEST_F(CookieSettingsTest, CookiesBlockSingle) {}

TEST_P(CookieSettingsTestP, CookiesBlockThirdParty) {}

TEST_F(CookieSettingsTest, CookiesControlsDefault) {}

TEST_F(CookieSettingsTest, CookiesControlsDisabled) {}

TEST_F(CookieSettingsTest, CookiesControlsEnabledForIncognito) {}

TEST_F(CookieSettingsTest, TestThirdPartyCookiePhaseout) {}

TEST_F(CookieSettingsTest, CookiesAllowThirdParty) {}

TEST_F(CookieSettingsTest, CookiesExplicitBlockSingleThirdParty) {}

TEST_F(CookieSettingsTest, CookiesExplicitSessionOnly) {}

TEST_F(CookieSettingsTest, ThirdPartyExceptionSessionOnly) {}

class CookieSettingsTestUserBypass : public CookieSettingsTest {};

// UserBypass is a desktop and android-only feature
#if !BUILDFLAG(IS_IOS)
TEST_F(CookieSettingsTestUserBypass, UserBypassTemporaryExceptions) {}

TEST_F(CookieSettingsTestUserBypass,
       TrackingProtectionUserBypassTemporaryExceptions) {}
#endif

TEST_F(CookieSettingsTestUserBypass,
       UserBypassThirdPartyCookiesTemporaryExceptions) {}

TEST_F(CookieSettingsTestUserBypass, ResetThirdPartyCookiesExceptions) {}

TEST_F(CookieSettingsTestUserBypass,
       UserBypassThirdPartyCookiesIncognitoExceptions) {}

TEST_F(CookieSettingsTest, KeepBlocked) {}

TEST_F(CookieSettingsTest, DeleteSessionOnly) {}

TEST_F(CookieSettingsTest, DeleteSessionOnlyWithThirdPartyBlocking) {}

TEST_F(CookieSettingsTest, DeletionWithDifferentPorts) {}

TEST_F(CookieSettingsTest, DeletionWithSubDomains) {}

TEST_F(CookieSettingsTest, DeleteCookiesWithThirdPartyException) {}

TEST_F(CookieSettingsTest, CookiesThirdPartyBlockedExplicitAllow) {}

TEST_F(CookieSettingsTest, CookiesThirdPartyBlockedAllSitesAllowed) {}

TEST_F(CookieSettingsTest, CookiesBlockEverything) {}

TEST_F(CookieSettingsTest, CookiesBlockEverythingExceptAllowed) {}

#if !BUILDFLAG(IS_IOS)
TEST_F(CookieSettingsTest, GetCookieSettingAllowedTelemetry) {}

TEST_P(CookieSettingsTestP, GetCookieSettingSAA) {}

// Test that http exceptions also affect websocket requests.
TEST_P(CookieSettingsTestP, GetCookieSettingSAAWebsocket) {}

TEST_P(CookieSettingsTestP, GetCookieSettingSAAViaFedCM) {}

// A top-level storage access grant should behave similarly to standard SAA
// grants.
TEST_P(CookieSettingsTestP, GetCookieSettingTopLevelStorageAccess) {}

// Subdomains of the granted resource url should not gain access if a valid
// grant exists; the grant should also not apply on different schemes.
TEST_P(CookieSettingsTestP, GetCookieSettingSAAResourceWildcards) {}

// Subdomains of the granted top level url should not grant access if a valid
// grant exists; the grant should also not apply on different schemes.
TEST_P(CookieSettingsTestP, GetCookieSettingSAATopLevelWildcards) {}

// Explicit settings should be respected regardless of whether Storage Access
// API is enabled and/or has grants.
TEST_P(CookieSettingsTestP, GetCookieSettingRespectsExplicitSettings) {}

// Once a grant expires access should no longer be given.
TEST_P(CookieSettingsTestP, GetCookieSettingSAAExpiredGrant) {}

TEST_P(CookieSettingsTestP, GetCookieSetting3pcdTrial) {}

TEST_P(CookieSettingsTestP, GetCookieSetting3pcdMetadataGrants) {}

TEST_P(CookieSettingsTestP, GetCookieSetting3pcdHeuristicsGrants) {}

TEST_F(CookieSettingsTest, SetTemporaryCookieGrantForHeuristicOverrides) {}

TEST_F(CookieSettingsTest, SetTemporaryCookieGrantForHeuristicSchemeless) {}
#endif

TEST_F(CookieSettingsTest, ExtensionsRegularSettings) {}

TEST_F(CookieSettingsTest, ExtensionsOwnCookies) {}

TEST_F(CookieSettingsTest, ExtensionsThirdParty) {}

TEST_F(CookieSettingsTest, ThirdPartyException) {}

// The TRACKING_PROTECTION content setting is not registered on iOS
#if !BUILDFLAG(IS_IOS)
TEST_F(CookieSettingsTest,
       TrackingProtectionExceptionReadWhenNoCookieException) {}
#endif

TEST_F(CookieSettingsTest, ManagedThirdPartyException) {}

TEST_F(CookieSettingsTest, ThirdPartySettingObserver) {}

TEST_F(CookieSettingsTest, PreservesBlockingStateFrom3pcdOnOffboarding) {}

TEST_F(CookieSettingsTest, LegacyCookieAccessAllowAll) {}

TEST_F(CookieSettingsTest, LegacyCookieAccessBlockAll) {}

TEST_F(CookieSettingsTest, LegacyCookieAccessAllowDomainPattern) {}

TEST_F(CookieSettingsTest, LegacyCookieAccessAllowDomainWildcardPattern) {}

TEST_F(CookieSettingsTest, GetStorageAccessStatus) {}

// NOTE: These tests will fail if their FINAL name is of length greater than 256
// characters. Thus, try to avoid (unnecessary) generalized parameterization
// when possible.
std::string CustomTestName(
    const testing::TestParamInfo<CookieSettingsTestP::ParamType>& info) {}

INSTANTIATE_TEST_SUITE_P();

#if !BUILDFLAG(IS_IOS)
class CookieSettingsTopLevelTpcdTrialTest
    : public CookieSettingsTestBase,
      public testing::
          WithParamInterface</*net::features::kTopLevelTpcdTrialSettings:*/
                             bool> {};

TEST_P(CookieSettingsTopLevelTpcdTrialTest, GetCookieSettingTopLevel3pcdTrial) {}

INSTANTIATE_TEST_SUITE_P();

#endif

#if !BUILDFLAG(IS_IOS)
class CookieSettingsTopLevelTpcdOriginTrialTest
    : public CookieSettingsTestBase {};

TEST_F(CookieSettingsTopLevelTpcdOriginTrialTest,
       GetCookieSetting3pcdOriginTrial) {}

#endif

}  // namespace content_settings