site_isolation_policy_unittest.cc 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // Copyright 2020 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "components/site_isolation/site_isolation_policy.h"
  5. #include "base/base_switches.h"
  6. #include "base/command_line.h"
  7. #include "base/system/sys_info.h"
  8. #include "base/test/scoped_feature_list.h"
  9. #include "build/build_config.h"
  10. #include "components/site_isolation/features.h"
  11. #include "components/site_isolation/preloaded_isolated_origins.h"
  12. #include "content/public/browser/child_process_security_policy.h"
  13. #include "content/public/browser/site_isolation_policy.h"
  14. #include "content/public/common/content_features.h"
  15. #include "content/public/common/content_switches.h"
  16. #include "content/public/test/browser_task_environment.h"
  17. #include "content/public/test/test_utils.h"
  18. #include "testing/gmock/include/gmock/gmock.h"
  19. #include "testing/gtest/include/gtest/gtest.h"
  20. namespace weblayer {
  21. namespace {
  22. using testing::UnorderedElementsAreArray;
  23. // Some command-line switches override field trials - the tests need to be
  24. // skipped in this case.
  25. bool ShouldSkipBecauseOfConflictingCommandLineSwitches() {
  26. if (base::CommandLine::ForCurrentProcess()->HasSwitch(
  27. switches::kSitePerProcess))
  28. return true;
  29. if (base::CommandLine::ForCurrentProcess()->HasSwitch(
  30. switches::kDisableSiteIsolation))
  31. return true;
  32. return false;
  33. }
  34. } // namespace
  35. class SiteIsolationPolicyTest : public testing::Test {
  36. public:
  37. SiteIsolationPolicyTest() = default;
  38. SiteIsolationPolicyTest(const SiteIsolationPolicyTest&) = delete;
  39. SiteIsolationPolicyTest& operator=(const SiteIsolationPolicyTest&) = delete;
  40. void SetUp() override {
  41. // This way the test always sees the same amount of physical memory
  42. // (kLowMemoryDeviceThresholdMB = 512MB), regardless of how much memory is
  43. // available in the testing environment.
  44. base::CommandLine::ForCurrentProcess()->AppendSwitch(
  45. switches::kEnableLowEndDeviceMode);
  46. EXPECT_EQ(512, base::SysInfo::AmountOfPhysicalMemoryMB());
  47. }
  48. // Sets the same memory threshold for both strict site isolation and partial
  49. // site isolation modes, since these tests care about both. For example,
  50. // UseDedicatedProcessesForAllSites() depends on the former, while preloaded
  51. // isolated origins use the latter.
  52. void SetMemoryThreshold(const std::string& threshold) {
  53. threshold_feature_.InitAndEnableFeatureWithParameters(
  54. site_isolation::features::kSiteIsolationMemoryThresholds,
  55. {{site_isolation::features::
  56. kStrictSiteIsolationMemoryThresholdParamName,
  57. threshold},
  58. {site_isolation::features::
  59. kPartialSiteIsolationMemoryThresholdParamName,
  60. threshold}});
  61. }
  62. private:
  63. content::BrowserTaskEnvironment task_environment_;
  64. base::test::ScopedFeatureList threshold_feature_;
  65. };
  66. TEST_F(SiteIsolationPolicyTest, NoIsolationBelowMemoryThreshold) {
  67. if (ShouldSkipBecauseOfConflictingCommandLineSwitches())
  68. return;
  69. SetMemoryThreshold("768");
  70. EXPECT_FALSE(
  71. content::SiteIsolationPolicy::UseDedicatedProcessesForAllSites());
  72. EXPECT_FALSE(
  73. content::SiteIsolationPolicy::ArePreloadedIsolatedOriginsEnabled());
  74. }
  75. TEST_F(SiteIsolationPolicyTest, IsolationAboveMemoryThreshold) {
  76. if (ShouldSkipBecauseOfConflictingCommandLineSwitches())
  77. return;
  78. SetMemoryThreshold("128");
  79. // Android should only use the preloaded origin list, while desktop should
  80. // isolate all sites.
  81. #if BUILDFLAG(IS_ANDROID)
  82. EXPECT_FALSE(
  83. content::SiteIsolationPolicy::UseDedicatedProcessesForAllSites());
  84. EXPECT_TRUE(
  85. content::SiteIsolationPolicy::ArePreloadedIsolatedOriginsEnabled());
  86. #else
  87. EXPECT_TRUE(content::SiteIsolationPolicy::UseDedicatedProcessesForAllSites());
  88. EXPECT_FALSE(
  89. content::SiteIsolationPolicy::ArePreloadedIsolatedOriginsEnabled());
  90. #endif
  91. }
  92. TEST_F(SiteIsolationPolicyTest, IsolatedOriginsContainPreloadedOrigins) {
  93. if (ShouldSkipBecauseOfConflictingCommandLineSwitches())
  94. return;
  95. content::SiteIsolationPolicy::ApplyGlobalIsolatedOrigins();
  96. std::vector<url::Origin> expected_embedder_origins =
  97. site_isolation::GetBrowserSpecificBuiltInIsolatedOrigins();
  98. auto* cpsp = content::ChildProcessSecurityPolicy::GetInstance();
  99. std::vector<url::Origin> isolated_origins = cpsp->GetIsolatedOrigins();
  100. EXPECT_THAT(expected_embedder_origins,
  101. UnorderedElementsAreArray(isolated_origins));
  102. }
  103. } // namespace weblayer