#include "components/site_isolation/site_isolation_policy.h"
#include "base/base_switches.h"
#include "base/command_line.h"
#include "base/json/values_util.h"
#include "base/memory/raw_ptr.h"
#include "base/no_destructor.h"
#include "base/system/sys_info.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/mock_entropy_provider.h"
#include "base/test/scoped_feature_list.h"
#include "base/time/time.h"
#include "build/branding_buildflags.h"
#include "build/build_config.h"
#include "components/prefs/pref_registry_simple.h"
#include "components/prefs/scoped_user_pref_update.h"
#include "components/prefs/testing_pref_service.h"
#include "components/site_isolation/features.h"
#include "components/site_isolation/pref_names.h"
#include "components/site_isolation/preloaded_isolated_origins.h"
#include "components/user_prefs/user_prefs.h"
#include "components/variations/variations_switches.h"
#include "content/public/browser/child_process_security_policy.h"
#include "content/public/browser/site_instance.h"
#include "content/public/browser/site_isolation_policy.h"
#include "content/public/common/content_client.h"
#include "content/public/common/content_features.h"
#include "content/public/common/content_switches.h"
#include "content/public/test/browser_task_environment.h"
#include "content/public/test/navigation_simulator.h"
#include "content/public/test/test_browser_context.h"
#include "content/public/test/test_renderer_host.h"
#include "content/public/test/test_utils.h"
#include "content/public/test/web_contents_tester.h"
#include "net/http/http_response_headers.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace site_isolation {
namespace {
IsolatedOriginSource;
bool ShouldSkipBecauseOfConflictingCommandLineSwitches() { … }
}
class BaseSiteIsolationTest : public testing::Test { … };
class OriginKeyedProcessesByDefaultSiteIsolationPolicyTest
: public BaseSiteIsolationTest { … };
TEST_F(OriginKeyedProcessesByDefaultSiteIsolationPolicyTest,
RequiresStrictSiteIsolation) { … }
class SiteIsolationPolicyTest : public BaseSiteIsolationTest { … };
class WebTriggeredIsolatedOriginsPolicyTest : public SiteIsolationPolicyTest { … };
TEST_F(WebTriggeredIsolatedOriginsPolicyTest, PersistIsolatedOrigin) { … }
TEST_F(WebTriggeredIsolatedOriginsPolicyTest, UpdatedMaxSize) { … }
TEST_F(WebTriggeredIsolatedOriginsPolicyTest, Expiration) { … }
class PasswordSiteIsolationPolicyTest : public SiteIsolationPolicyTest { … };
TEST_F(PasswordSiteIsolationPolicyTest, ApplyPersistedIsolatedOrigins) { … }
class NoPasswordSiteIsolationPolicyTest : public SiteIsolationPolicyTest { … };
TEST_F(NoPasswordSiteIsolationPolicyTest,
PersistedIsolatedOriginsIgnoredWithoutPasswordIsolation) { … }
enum class SitePerProcessMemoryThreshold { … };
enum class SitePerProcessMode { … };
struct SitePerProcessMemoryThresholdBrowserTestParams { … };
const url::Origin& GetTrialOrigin() { … }
class SitePerProcessMemoryThresholdBrowserTest
: public BaseSiteIsolationTest,
public ::testing::WithParamInterface<
SitePerProcessMemoryThresholdBrowserTestParams> { … };
SitePerProcessMemoryThresholdBrowserTestNoIsolation;
TEST_P(SitePerProcessMemoryThresholdBrowserTestNoIsolation, NoIsolation) { … }
SitePerProcessMemoryThresholdBrowserTestIsolation;
TEST_P(SitePerProcessMemoryThresholdBrowserTestIsolation, Isolation) { … }
INSTANTIATE_TEST_SUITE_P(…);
INSTANTIATE_TEST_SUITE_P(…);
SitePerProcessMemoryThresholdBrowserTestNoIsolatedOrigin;
TEST_P(SitePerProcessMemoryThresholdBrowserTestNoIsolatedOrigin,
TrialNoIsolatedOrigin) { … }
SitePerProcessMemoryThresholdBrowserTestIsolatedOrigin;
TEST_P(SitePerProcessMemoryThresholdBrowserTestIsolatedOrigin,
TrialIsolatedOrigin) { … }
INSTANTIATE_TEST_SUITE_P(…);
INSTANTIATE_TEST_SUITE_P(…);
class PasswordSiteIsolationFieldTrialTest : public BaseSiteIsolationTest { … };
class EnabledPasswordSiteIsolationFieldTrialTest
: public PasswordSiteIsolationFieldTrialTest { … };
class DisabledPasswordSiteIsolationFieldTrialTest
: public PasswordSiteIsolationFieldTrialTest { … };
TEST_F(EnabledPasswordSiteIsolationFieldTrialTest, BelowThreshold) { … }
TEST_F(EnabledPasswordSiteIsolationFieldTrialTest, AboveThreshold) { … }
TEST_F(DisabledPasswordSiteIsolationFieldTrialTest,
CommandLineOverride_BelowThreshold) { … }
TEST_F(DisabledPasswordSiteIsolationFieldTrialTest,
CommandLineOverride_AboveThreshold) { … }
class StrictOriginIsolationFieldTrialTest : public BaseSiteIsolationTest { … };
class EnabledStrictOriginIsolationFieldTrialTest
: public StrictOriginIsolationFieldTrialTest { … };
class DisabledStrictOriginIsolationFieldTrialTest
: public StrictOriginIsolationFieldTrialTest { … };
TEST_F(EnabledStrictOriginIsolationFieldTrialTest,
DisabledViaCommandLineOverride) { … }
TEST_F(DisabledStrictOriginIsolationFieldTrialTest,
EnabledViaCommandLineOverride_BelowThreshold) { … }
#if BUILDFLAG(GOOGLE_CHROME_BRANDING) && BUILDFLAG(IS_ANDROID)
class BuiltInIsolatedOriginsTest : public SiteIsolationPolicyTest {
public:
BuiltInIsolatedOriginsTest() = default;
BuiltInIsolatedOriginsTest(const BuiltInIsolatedOriginsTest&) = delete;
BuiltInIsolatedOriginsTest& operator=(const BuiltInIsolatedOriginsTest&) =
delete;
protected:
void SetUp() override {
base::CommandLine::ForCurrentProcess()->AppendSwitch(
switches::kEnableLowEndDeviceMode);
EXPECT_EQ(512, base::SysInfo::AmountOfPhysicalMemoryMB());
SiteIsolationPolicyTest::SetUp();
}
};
TEST_F(BuiltInIsolatedOriginsTest, DefaultThreshold) {
if (ShouldSkipBecauseOfConflictingCommandLineSwitches())
return;
base::test::ScopedFeatureList memory_feature;
memory_feature.InitAndEnableFeatureWithParameters(
features::kSiteIsolationMemoryThresholds,
{{features::kPartialSiteIsolationMemoryThresholdParamName, "128"}});
content::SiteIsolationPolicy::ApplyGlobalIsolatedOrigins();
EXPECT_TRUE(
content::SiteIsolationPolicy::ArePreloadedIsolatedOriginsEnabled());
auto* cpsp = content::ChildProcessSecurityPolicy::GetInstance();
std::vector<url::Origin> isolated_origins = cpsp->GetIsolatedOrigins(
content::ChildProcessSecurityPolicy::IsolatedOriginSource::BUILT_IN);
EXPECT_GT(isolated_origins.size(), 10u);
EXPECT_THAT(
isolated_origins,
::testing::Contains(url::Origin::Create(GURL("https://google.com/"))));
EXPECT_THAT(
isolated_origins,
::testing::Contains(url::Origin::Create(GURL("https://amazon.com/"))));
EXPECT_THAT(
isolated_origins,
::testing::Contains(url::Origin::Create(GURL("https://facebook.com/"))));
cpsp->ClearIsolatedOriginsForTesting();
}
TEST_F(BuiltInIsolatedOriginsTest, BelowThreshold) {
if (ShouldSkipBecauseOfConflictingCommandLineSwitches())
return;
base::test::ScopedFeatureList memory_feature;
memory_feature.InitAndEnableFeatureWithParameters(
features::kSiteIsolationMemoryThresholds,
{{features::kPartialSiteIsolationMemoryThresholdParamName, "768"}});
content::SiteIsolationPolicy::ApplyGlobalIsolatedOrigins();
EXPECT_FALSE(
content::SiteIsolationPolicy::ArePreloadedIsolatedOriginsEnabled());
auto* cpsp = content::ChildProcessSecurityPolicy::GetInstance();
std::vector<url::Origin> isolated_origins = cpsp->GetIsolatedOrigins(
content::ChildProcessSecurityPolicy::IsolatedOriginSource::BUILT_IN);
EXPECT_EQ(isolated_origins.size(), 0u);
cpsp->ClearIsolatedOriginsForTesting();
}
TEST_F(BuiltInIsolatedOriginsTest, NotAppliedWithFullSiteIsolation) {
content::IsolateAllSitesForTesting(base::CommandLine::ForCurrentProcess());
base::test::ScopedFeatureList memory_feature;
memory_feature.InitAndEnableFeatureWithParameters(
features::kSiteIsolationMemoryThresholds,
{{features::kPartialSiteIsolationMemoryThresholdParamName, "128"}});
content::SiteIsolationPolicy::ApplyGlobalIsolatedOrigins();
EXPECT_FALSE(
content::SiteIsolationPolicy::ArePreloadedIsolatedOriginsEnabled());
auto* cpsp = content::ChildProcessSecurityPolicy::GetInstance();
std::vector<url::Origin> isolated_origins = cpsp->GetIsolatedOrigins(
content::ChildProcessSecurityPolicy::IsolatedOriginSource::BUILT_IN);
EXPECT_EQ(isolated_origins.size(), 0u);
}
#endif
class OptInOriginIsolationPolicyTest : public BaseSiteIsolationTest { … };
TEST_F(OptInOriginIsolationPolicyTest, BelowThreshold) { … }
TEST_F(OptInOriginIsolationPolicyTest, AboveThreshold) { … }
}