|
| 1 | +// Copyright (c) 2025 The Brave Authors. All rights reserved. |
| 2 | +// This Source Code Form is subject to the terms of the Mozilla Public |
| 3 | +// License, v. 2.0. If a copy of the MPL was not distributed with this file, |
| 4 | +// You can obtain one at https://mozilla.org/MPL/2.0/. |
| 5 | + |
| 6 | +#include "brave/components/brave_shields/content/browser/brave_shields_util.h" |
| 7 | +#include "chrome/browser/content_settings/cookie_settings_factory.h" |
| 8 | +#include "chrome/browser/content_settings/host_content_settings_map_factory.h" |
| 9 | +#include "chrome/browser/profiles/profile.h" |
| 10 | +#include "chrome/test/base/testing_browser_process.h" |
| 11 | +#include "chrome/test/base/testing_profile.h" |
| 12 | +#include "chrome/test/base/testing_profile_manager.h" |
| 13 | +#include "components/content_settings/core/browser/cookie_settings.h" |
| 14 | +#include "components/content_settings/core/browser/host_content_settings_map.h" |
| 15 | +#include "content/public/test/browser_task_environment.h" |
| 16 | +#include "testing/gtest/include/gtest/gtest.h" |
| 17 | +#include "url/gurl.h" |
| 18 | + |
| 19 | +namespace { |
| 20 | + |
| 21 | +using ScopedIncognitoProfile = |
| 22 | + std::unique_ptr<Profile, decltype([](Profile* p) { |
| 23 | + ASSERT_TRUE(!!p->IsOffTheRecord()); |
| 24 | + p->GetOriginalProfile()->DestroyOffTheRecordProfile(p); |
| 25 | + })>; |
| 26 | + |
| 27 | +testing::Message& operator<<(testing::Message& m, |
| 28 | + brave_shields::ControlType c) { |
| 29 | + return m << brave_shields::ControlTypeToString(c); |
| 30 | +} |
| 31 | + |
| 32 | +} // namespace |
| 33 | + |
| 34 | +namespace brave_shields { |
| 35 | + |
| 36 | +class BraveShieldsUtilProfilesTest : public testing::Test { |
| 37 | + public: |
| 38 | + BraveShieldsUtilProfilesTest() |
| 39 | + : local_state_(TestingBrowserProcess::GetGlobal()) {} |
| 40 | + ~BraveShieldsUtilProfilesTest() override = default; |
| 41 | + |
| 42 | + TestingProfile* regular_profile() { return &profile_; } |
| 43 | + ScopedIncognitoProfile incognito_profile() { |
| 44 | + return ScopedIncognitoProfile(regular_profile()->GetOffTheRecordProfile( |
| 45 | + Profile::OTRProfileID::PrimaryID(), |
| 46 | + /*create_if_needed=*/true)); |
| 47 | + } |
| 48 | + |
| 49 | + HostContentSettingsMap* hcsm(Profile* profile) { |
| 50 | + return HostContentSettingsMapFactory::GetForProfile(profile); |
| 51 | + } |
| 52 | + |
| 53 | + HostContentSettingsMap* hcsm(ScopedIncognitoProfile& profile) { |
| 54 | + return hcsm(profile.get()); |
| 55 | + } |
| 56 | + |
| 57 | + const GURL test_url{"https://example.com"}; |
| 58 | + |
| 59 | + template <typename ValueT, typename SetFn, typename GetFn> |
| 60 | + void RunTest(base::span<const std::pair<ValueT, ValueT>> cases, |
| 61 | + SetFn setter, |
| 62 | + GetFn getter) { |
| 63 | + testing::Message issues; |
| 64 | + for (const auto& [value, expect] : cases) { |
| 65 | + setter(hcsm(regular_profile()), value); |
| 66 | + const auto get = getter(hcsm(regular_profile())); |
| 67 | + if (expect != get) { |
| 68 | + issues << "Regular profile: Set " << value << " Get " << get |
| 69 | + << " Expected " << expect << "\n"; |
| 70 | + } |
| 71 | + |
| 72 | + auto incognito = incognito_profile(); |
| 73 | + // Now change value for incognito and expect that values are not depended |
| 74 | + // on the regular profile. |
| 75 | + for (const auto& [ivalue, iexpect] : cases) { |
| 76 | + setter(hcsm(incognito), ivalue); |
| 77 | + const auto iget = getter(hcsm(incognito)); |
| 78 | + if (iexpect != iget) { |
| 79 | + issues << "Incognito profile: Set " << ivalue << " Get " << iget |
| 80 | + << " Expected " << iexpect << " Regular profile value " |
| 81 | + << value << "\n"; |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + const auto message = issues.GetString(); |
| 86 | + EXPECT_TRUE(message.empty()) << message; |
| 87 | + } |
| 88 | + |
| 89 | + private: |
| 90 | + content::BrowserTaskEnvironment task_environment_; |
| 91 | + TestingProfile profile_; |
| 92 | + ScopedTestingLocalState local_state_; |
| 93 | +}; |
| 94 | + |
| 95 | +TEST_F(BraveShieldsUtilProfilesTest, SetBraveShieldsEnabled) { |
| 96 | + constexpr std::pair<bool, bool> kExpects[] = {{true, true}, {false, false}}; |
| 97 | + |
| 98 | + auto set = [this](HostContentSettingsMap* map, bool value) { |
| 99 | + SetBraveShieldsEnabled(map, value, test_url); |
| 100 | + }; |
| 101 | + auto get = [this](HostContentSettingsMap* map) { |
| 102 | + return GetBraveShieldsEnabled(map, test_url); |
| 103 | + }; |
| 104 | + |
| 105 | + RunTest<bool>(kExpects, std::move(set), std::move(get)); |
| 106 | +} |
| 107 | + |
| 108 | +TEST_F(BraveShieldsUtilProfilesTest, SetAdControlType) { |
| 109 | + constexpr std::pair<ControlType, ControlType> kExpects[] = {{ALLOW, ALLOW}, |
| 110 | + {BLOCK, BLOCK}}; |
| 111 | + |
| 112 | + auto set = [this](HostContentSettingsMap* map, ControlType value) { |
| 113 | + SetAdControlType(map, value, test_url); |
| 114 | + }; |
| 115 | + auto get = [this](HostContentSettingsMap* map) { |
| 116 | + return GetAdControlType(map, test_url); |
| 117 | + }; |
| 118 | + |
| 119 | + RunTest<ControlType>(kExpects, std::move(set), std::move(get)); |
| 120 | +} |
| 121 | + |
| 122 | +TEST_F(BraveShieldsUtilProfilesTest, SetCosmeticFilteringControlType) { |
| 123 | + constexpr std::pair<ControlType, ControlType> kExpects[] = { |
| 124 | + {ALLOW, ALLOW}, {BLOCK, BLOCK}, {BLOCK_THIRD_PARTY, BLOCK_THIRD_PARTY}}; |
| 125 | + |
| 126 | + auto set = [this](HostContentSettingsMap* map, ControlType value) { |
| 127 | + SetCosmeticFilteringControlType(map, value, test_url); |
| 128 | + }; |
| 129 | + auto get = [this](HostContentSettingsMap* map) { |
| 130 | + return GetCosmeticFilteringControlType(map, test_url); |
| 131 | + }; |
| 132 | + |
| 133 | + RunTest<ControlType>(kExpects, std::move(set), std::move(get)); |
| 134 | +} |
| 135 | + |
| 136 | +TEST_F(BraveShieldsUtilProfilesTest, SetCookieControlType) { |
| 137 | + constexpr std::pair<ControlType, ControlType> kExpects[] = { |
| 138 | + {ALLOW, ALLOW}, {BLOCK, BLOCK}, {BLOCK_THIRD_PARTY, BLOCK_THIRD_PARTY}}; |
| 139 | + |
| 140 | + auto set = [this](HostContentSettingsMap* map, ControlType value) { |
| 141 | + SetCookieControlType(map, regular_profile()->GetPrefs(), value, test_url); |
| 142 | + }; |
| 143 | + |
| 144 | + auto cookie_settings = |
| 145 | + CookieSettingsFactory::GetForProfile(regular_profile()); |
| 146 | + |
| 147 | + auto get = [this, cookie_settings](HostContentSettingsMap* map) { |
| 148 | + return GetCookieControlType(map, cookie_settings.get(), test_url); |
| 149 | + }; |
| 150 | + |
| 151 | + RunTest<ControlType>(kExpects, std::move(set), std::move(get)); |
| 152 | +} |
| 153 | + |
| 154 | +TEST_F(BraveShieldsUtilProfilesTest, SetFingerprintingControlType) { |
| 155 | + constexpr std::pair<ControlType, ControlType> kExpects[] = { |
| 156 | + {ALLOW, ALLOW}, |
| 157 | + {BLOCK, DEFAULT}, |
| 158 | + {BLOCK_THIRD_PARTY, DEFAULT}, |
| 159 | + {DEFAULT, DEFAULT}}; |
| 160 | + |
| 161 | + auto set = [this](HostContentSettingsMap* map, ControlType value) { |
| 162 | + SetFingerprintingControlType(map, value, test_url); |
| 163 | + }; |
| 164 | + auto get = [this](HostContentSettingsMap* map) { |
| 165 | + return GetFingerprintingControlType(map, test_url); |
| 166 | + }; |
| 167 | + |
| 168 | + RunTest<ControlType>(kExpects, std::move(set), std::move(get)); |
| 169 | +} |
| 170 | + |
| 171 | +TEST_F(BraveShieldsUtilProfilesTest, SetHttpsUpgradeControlType) { |
| 172 | + constexpr std::pair<ControlType, ControlType> kExpects[] = { |
| 173 | + {ALLOW, ALLOW}, {BLOCK, BLOCK}, {BLOCK_THIRD_PARTY, BLOCK_THIRD_PARTY}}; |
| 174 | + |
| 175 | + auto set = [this](HostContentSettingsMap* map, ControlType value) { |
| 176 | + SetHttpsUpgradeControlType(map, value, test_url); |
| 177 | + }; |
| 178 | + auto get = [this](HostContentSettingsMap* map) { |
| 179 | + return GetHttpsUpgradeControlType(map, test_url); |
| 180 | + }; |
| 181 | + |
| 182 | + RunTest<ControlType>(kExpects, std::move(set), std::move(get)); |
| 183 | +} |
| 184 | + |
| 185 | +TEST_F(BraveShieldsUtilProfilesTest, SetNoScriptControlType) { |
| 186 | + constexpr std::pair<ControlType, ControlType> kExpects[] = { |
| 187 | + {ALLOW, ALLOW}, {BLOCK, BLOCK}, {DEFAULT, BLOCK}}; |
| 188 | + |
| 189 | + auto set = [this](HostContentSettingsMap* map, ControlType value) { |
| 190 | + SetNoScriptControlType(map, value, test_url); |
| 191 | + }; |
| 192 | + auto get = [this](HostContentSettingsMap* map) { |
| 193 | + return GetNoScriptControlType(map, test_url); |
| 194 | + }; |
| 195 | + |
| 196 | + RunTest<ControlType>(kExpects, std::move(set), std::move(get)); |
| 197 | +} |
| 198 | + |
| 199 | +TEST_F(BraveShieldsUtilProfilesTest, SetForgetFirstPartyStorageEnabled) { |
| 200 | + constexpr std::pair<bool, bool> kExpects[] = {{true, true}, {false, false}}; |
| 201 | + |
| 202 | + auto set = [this](HostContentSettingsMap* map, bool value) { |
| 203 | + SetForgetFirstPartyStorageEnabled(map, value, test_url); |
| 204 | + }; |
| 205 | + auto get = [this](HostContentSettingsMap* map) { |
| 206 | + return GetForgetFirstPartyStorageEnabled(map, test_url); |
| 207 | + }; |
| 208 | + |
| 209 | + RunTest<bool>(kExpects, std::move(set), std::move(get)); |
| 210 | +} |
| 211 | + |
| 212 | +} // namespace brave_shields |
0 commit comments