privacy_sandbox_settings_unittest.cc 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004
  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/privacy_sandbox/privacy_sandbox_settings.h"
  5. #include "base/json/values_util.h"
  6. #include "base/test/gtest_util.h"
  7. #include "base/test/scoped_feature_list.h"
  8. #include "components/content_settings/core/browser/cookie_settings.h"
  9. #include "components/content_settings/core/browser/host_content_settings_map.h"
  10. #include "components/content_settings/core/common/pref_names.h"
  11. #include "components/content_settings/core/test/content_settings_mock_provider.h"
  12. #include "components/content_settings/core/test/content_settings_test_utils.h"
  13. #include "components/privacy_sandbox/privacy_sandbox_features.h"
  14. #include "components/privacy_sandbox/privacy_sandbox_prefs.h"
  15. #include "components/privacy_sandbox/privacy_sandbox_test_util.h"
  16. #include "components/sync_preferences/testing_pref_service_syncable.h"
  17. #include "content/public/test/browser_task_environment.h"
  18. #include "testing/gtest/include/gtest/gtest.h"
  19. #include "url/origin.h"
  20. namespace privacy_sandbox {
  21. using Topic = browsing_topics::Topic;
  22. class PrivacySandboxSettingsTest : public testing::TestWithParam<bool> {
  23. public:
  24. PrivacySandboxSettingsTest()
  25. : browser_task_environment_(
  26. base::test::TaskEnvironment::TimeSource::MOCK_TIME) {
  27. content_settings::CookieSettings::RegisterProfilePrefs(prefs()->registry());
  28. HostContentSettingsMap::RegisterProfilePrefs(prefs()->registry());
  29. privacy_sandbox::RegisterProfilePrefs(prefs()->registry());
  30. host_content_settings_map_ = new HostContentSettingsMap(
  31. &prefs_, false /* is_off_the_record */, false /* store_last_modified */,
  32. false /* restore_session */);
  33. cookie_settings_ = new content_settings::CookieSettings(
  34. host_content_settings_map_.get(), &prefs_, false, "chrome-extension");
  35. }
  36. ~PrivacySandboxSettingsTest() override {
  37. host_content_settings_map()->ShutdownOnUIThread();
  38. }
  39. void SetUp() override {
  40. auto mock_delegate = std::make_unique<testing::NiceMock<
  41. privacy_sandbox_test_util::MockPrivacySandboxSettingsDelegate>>();
  42. mock_delegate_ = mock_delegate.get();
  43. InitializePrefsBeforeStart();
  44. InitializeFeaturesBeforeStart();
  45. InitializeDelegateBeforeStart();
  46. privacy_sandbox_settings_ = std::make_unique<PrivacySandboxSettings>(
  47. std::move(mock_delegate), host_content_settings_map(), cookie_settings_,
  48. prefs(), IsIncognitoProfile());
  49. }
  50. virtual void InitializePrefsBeforeStart() {}
  51. virtual void InitializeFeaturesBeforeStart() {
  52. if (GetParam()) {
  53. feature_list_.InitAndEnableFeature(
  54. privacy_sandbox::kPrivacySandboxSettings3);
  55. } else {
  56. feature_list_.InitAndDisableFeature(
  57. privacy_sandbox::kPrivacySandboxSettings3);
  58. }
  59. }
  60. virtual void InitializeDelegateBeforeStart() {
  61. mock_delegate()->SetUpDefaultResponse(/*restricted=*/false);
  62. }
  63. virtual bool IsIncognitoProfile() { return false; }
  64. privacy_sandbox_test_util::MockPrivacySandboxSettingsDelegate*
  65. mock_delegate() {
  66. return mock_delegate_;
  67. }
  68. sync_preferences::TestingPrefServiceSyncable* prefs() { return &prefs_; }
  69. HostContentSettingsMap* host_content_settings_map() {
  70. return host_content_settings_map_.get();
  71. }
  72. content_settings::CookieSettings* cookie_settings() {
  73. return cookie_settings_.get();
  74. }
  75. PrivacySandboxSettings* privacy_sandbox_settings() {
  76. return privacy_sandbox_settings_.get();
  77. }
  78. content::BrowserTaskEnvironment* task_environment() {
  79. return &browser_task_environment_;
  80. }
  81. protected:
  82. base::test::ScopedFeatureList feature_list_;
  83. private:
  84. content::BrowserTaskEnvironment browser_task_environment_;
  85. raw_ptr<privacy_sandbox_test_util::MockPrivacySandboxSettingsDelegate>
  86. mock_delegate_;
  87. sync_preferences::TestingPrefServiceSyncable prefs_;
  88. scoped_refptr<HostContentSettingsMap> host_content_settings_map_;
  89. scoped_refptr<content_settings::CookieSettings> cookie_settings_;
  90. std::unique_ptr<PrivacySandboxSettings> privacy_sandbox_settings_;
  91. };
  92. TEST_P(PrivacySandboxSettingsTest, DefaultContentSettingBlockOverridePref) {
  93. // A block default content setting should override the Privacy Sandbox pref.
  94. privacy_sandbox_test_util::SetupTestState(
  95. prefs(), host_content_settings_map(),
  96. /*privacy_sandbox_enabled=*/true,
  97. /*block_third_party_cookies=*/false,
  98. /*default_cookie_setting=*/ContentSetting::CONTENT_SETTING_BLOCK,
  99. /*user_cookie_exceptions=*/{},
  100. /*managed_cookie_setting=*/privacy_sandbox_test_util::kNoSetting,
  101. /*managed_cookie_exceptions=*/{});
  102. EXPECT_FALSE(privacy_sandbox_settings()->IsTopicsAllowed());
  103. EXPECT_FALSE(privacy_sandbox_settings()->IsTopicsAllowedForContext(
  104. GURL("https://embedded.com"),
  105. url::Origin::Create(GURL("https://test.com"))));
  106. EXPECT_FALSE(privacy_sandbox_settings()->IsConversionMeasurementAllowed(
  107. url::Origin::Create(GURL("https://test.com")),
  108. url::Origin::Create(GURL("https://embedded.com"))));
  109. EXPECT_FALSE(privacy_sandbox_settings()->ShouldSendConversionReport(
  110. url::Origin::Create(GURL("https://test.com")),
  111. url::Origin::Create(GURL("https://another-test.com")),
  112. url::Origin::Create(GURL("https://embedded.com"))));
  113. EXPECT_FALSE(privacy_sandbox_settings()->IsPrivateAggregationAllowed(
  114. url::Origin::Create(GURL("https://test.com")),
  115. url::Origin::Create(GURL("https://embedded.com"))));
  116. EXPECT_FALSE(privacy_sandbox_settings()->IsFledgeAllowed(
  117. url::Origin::Create(GURL("https://test.com")),
  118. url::Origin::Create(GURL("https://embedded.com"))));
  119. EXPECT_EQ(std::vector<GURL>{},
  120. privacy_sandbox_settings()->FilterFledgeAllowedParties(
  121. url::Origin::Create(GURL("https://test.com")),
  122. {GURL("https://embedded.com"),
  123. GURL("https://another-embedded.com")}));
  124. EXPECT_FALSE(privacy_sandbox_settings()->IsSharedStorageAllowed(
  125. url::Origin::Create(GURL("https://test.com")),
  126. url::Origin::Create(GURL("https://embedded.com"))));
  127. // An allow default or exception, whether via user or policy, should not
  128. // override the preference value.
  129. privacy_sandbox_test_util::SetupTestState(
  130. prefs(), host_content_settings_map(),
  131. /*privacy_sandbox_enabled=*/false,
  132. /*block_third_party_cookies=*/false,
  133. /*default_cookie_setting=*/ContentSetting::CONTENT_SETTING_ALLOW,
  134. /*user_cookie_exceptions=*/
  135. {{"https://embedded.com", "https://test.com",
  136. ContentSetting::CONTENT_SETTING_ALLOW},
  137. {"https://another-embedded.com", "https://test.com",
  138. ContentSetting::CONTENT_SETTING_ALLOW},
  139. {"https://embedded.com", "https://another-test.com",
  140. ContentSetting::CONTENT_SETTING_ALLOW}},
  141. /*managed_cookie_setting=*/ContentSetting::CONTENT_SETTING_ALLOW,
  142. /*managed_cookie_exceptions=*/
  143. {{"https://embedded.com", "https://test.com",
  144. ContentSetting::CONTENT_SETTING_ALLOW},
  145. {"https://another-embedded.com", "https://test.com",
  146. ContentSetting::CONTENT_SETTING_ALLOW},
  147. {"https://embedded.com", "https://another-test.com",
  148. ContentSetting::CONTENT_SETTING_ALLOW}});
  149. EXPECT_FALSE(privacy_sandbox_settings()->IsTopicsAllowed());
  150. EXPECT_FALSE(privacy_sandbox_settings()->IsTopicsAllowedForContext(
  151. GURL("https://embedded.com"),
  152. url::Origin::Create(GURL("https://test.com"))));
  153. EXPECT_FALSE(privacy_sandbox_settings()->IsConversionMeasurementAllowed(
  154. url::Origin::Create(GURL("https://test.com")),
  155. url::Origin::Create(GURL("https://embedded.com"))));
  156. EXPECT_FALSE(privacy_sandbox_settings()->ShouldSendConversionReport(
  157. url::Origin::Create(GURL("https://test.com")),
  158. url::Origin::Create(GURL("https://another-test.com")),
  159. url::Origin::Create(GURL("https://embedded.com"))));
  160. EXPECT_FALSE(privacy_sandbox_settings()->IsPrivateAggregationAllowed(
  161. url::Origin::Create(GURL("https://test.com")),
  162. url::Origin::Create(GURL("https://embedded.com"))));
  163. EXPECT_FALSE(privacy_sandbox_settings()->IsFledgeAllowed(
  164. url::Origin::Create(GURL("https://test.com")),
  165. url::Origin::Create(GURL("https://embedded.com"))));
  166. EXPECT_EQ(std::vector<GURL>{},
  167. privacy_sandbox_settings()->FilterFledgeAllowedParties(
  168. url::Origin::Create(GURL("https://test.com")),
  169. {GURL("https://embedded.com"),
  170. GURL("https://another-embedded.com")}));
  171. EXPECT_FALSE(privacy_sandbox_settings()->IsSharedStorageAllowed(
  172. url::Origin::Create(GURL("https://test.com")),
  173. url::Origin::Create(GURL("https://embedded.com"))));
  174. }
  175. TEST_P(PrivacySandboxSettingsTest, CookieExceptionsApply) {
  176. // All cookie exceptions which disable access should apply to the Privacy
  177. // Sandbox. General topics calculations should however remain allowed.
  178. privacy_sandbox_test_util::SetupTestState(
  179. prefs(), host_content_settings_map(),
  180. /*privacy_sandbox_enabled=*/true,
  181. /*block_third_party_cookies=*/false,
  182. /*default_cookie_setting=*/ContentSetting::CONTENT_SETTING_ALLOW,
  183. /*user_cookie_exceptions=*/
  184. {{"https://embedded.com", "https://test.com",
  185. ContentSetting::CONTENT_SETTING_BLOCK},
  186. {"https://another-embedded.com", "*",
  187. ContentSetting::CONTENT_SETTING_BLOCK}},
  188. /*managed_cookie_setting=*/privacy_sandbox_test_util::kNoSetting,
  189. /*managed_cookie_exceptions=*/{});
  190. EXPECT_TRUE(privacy_sandbox_settings()->IsTopicsAllowed());
  191. EXPECT_FALSE(privacy_sandbox_settings()->IsTopicsAllowedForContext(
  192. GURL("https://embedded.com"),
  193. url::Origin::Create(GURL("https://test.com"))));
  194. EXPECT_FALSE(privacy_sandbox_settings()->IsConversionMeasurementAllowed(
  195. url::Origin::Create(GURL("https://test.com")),
  196. url::Origin::Create(GURL("https://embedded.com"))));
  197. EXPECT_FALSE(privacy_sandbox_settings()->ShouldSendConversionReport(
  198. url::Origin::Create(GURL("https://test.com")),
  199. url::Origin::Create(GURL("https://another-test.com")),
  200. url::Origin::Create(GURL("https://embedded.com"))));
  201. EXPECT_FALSE(privacy_sandbox_settings()->IsPrivateAggregationAllowed(
  202. url::Origin::Create(GURL("https://test.com")),
  203. url::Origin::Create(GURL("https://embedded.com"))));
  204. EXPECT_FALSE(privacy_sandbox_settings()->IsFledgeAllowed(
  205. url::Origin::Create(GURL("https://test.com")),
  206. url::Origin::Create(GURL("https://embedded.com"))));
  207. EXPECT_EQ(std::vector<GURL>{},
  208. privacy_sandbox_settings()->FilterFledgeAllowedParties(
  209. url::Origin::Create(GURL("https://test.com")),
  210. {GURL("https://embedded.com"),
  211. GURL("https://another-embedded.com")}));
  212. EXPECT_FALSE(privacy_sandbox_settings()->IsSharedStorageAllowed(
  213. url::Origin::Create(GURL("https://test.com")),
  214. url::Origin::Create(GURL("https://embedded.com"))));
  215. // The default managed content setting should apply, overriding any user ones,
  216. // and disabling Topics calculations.
  217. privacy_sandbox_test_util::SetupTestState(
  218. prefs(), host_content_settings_map(),
  219. /*privacy_sandbox_enabled=*/true,
  220. /*block_third_party_cookies=*/false,
  221. /*default_cookie_setting=*/ContentSetting::CONTENT_SETTING_ALLOW,
  222. /*user_cookie_exceptions=*/
  223. {{"https://embedded.com", "https://test.com",
  224. ContentSetting::CONTENT_SETTING_ALLOW},
  225. {"https://another-embedded.com", "https://test.com",
  226. ContentSetting::CONTENT_SETTING_ALLOW},
  227. {"https://embedded.com", "https://another-test.com",
  228. ContentSetting::CONTENT_SETTING_BLOCK}},
  229. /*managed_cookie_setting=*/ContentSetting::CONTENT_SETTING_BLOCK,
  230. /*managed_cookie_exceptions=*/{});
  231. EXPECT_FALSE(privacy_sandbox_settings()->IsTopicsAllowed());
  232. EXPECT_FALSE(privacy_sandbox_settings()->IsTopicsAllowedForContext(
  233. GURL("https://embedded.com"),
  234. url::Origin::Create(GURL("https://test.com"))));
  235. EXPECT_FALSE(privacy_sandbox_settings()->IsConversionMeasurementAllowed(
  236. url::Origin::Create(GURL("https://test.com")),
  237. url::Origin::Create(GURL("https://embedded.com"))));
  238. EXPECT_FALSE(privacy_sandbox_settings()->ShouldSendConversionReport(
  239. url::Origin::Create(GURL("https://test.com")),
  240. url::Origin::Create(GURL("https://another-test.com")),
  241. url::Origin::Create(GURL("https://embedded.com"))));
  242. EXPECT_FALSE(privacy_sandbox_settings()->IsPrivateAggregationAllowed(
  243. url::Origin::Create(GURL("https://test.com")),
  244. url::Origin::Create(GURL("https://embedded.com"))));
  245. EXPECT_FALSE(privacy_sandbox_settings()->IsFledgeAllowed(
  246. url::Origin::Create(GURL("https://test.com")),
  247. url::Origin::Create(GURL("https://embedded.com"))));
  248. EXPECT_EQ(std::vector<GURL>{},
  249. privacy_sandbox_settings()->FilterFledgeAllowedParties(
  250. url::Origin::Create(GURL("https://test.com")),
  251. {GURL("https://embedded.com"),
  252. GURL("https://another-embedded.com")}));
  253. EXPECT_FALSE(privacy_sandbox_settings()->IsSharedStorageAllowed(
  254. url::Origin::Create(GURL("https://test.com")),
  255. url::Origin::Create(GURL("https://embedded.com"))));
  256. // Managed content setting exceptions should override both the privacy
  257. // sandbox pref and any user settings.
  258. privacy_sandbox_test_util::SetupTestState(
  259. prefs(), host_content_settings_map(),
  260. /*privacy_sandbox_enabled=*/true,
  261. /*block_third_party_cookies=*/false,
  262. /*default_cookie_setting=*/ContentSetting::CONTENT_SETTING_ALLOW,
  263. /*user_cookie_exceptions=*/
  264. {{"https://embedded.com", "https://test.com",
  265. ContentSetting::CONTENT_SETTING_ALLOW},
  266. {"https://another-embedded.com", "https://test.com",
  267. ContentSetting::CONTENT_SETTING_ALLOW},
  268. {"https://embedded.com", "https://another-test.com",
  269. ContentSetting::CONTENT_SETTING_ALLOW}},
  270. /*managed_cookie_setting=*/ContentSetting::CONTENT_SETTING_ALLOW,
  271. {{"https://embedded.com", "https://test.com",
  272. ContentSetting::CONTENT_SETTING_BLOCK}});
  273. EXPECT_TRUE(privacy_sandbox_settings()->IsTopicsAllowed());
  274. EXPECT_FALSE(privacy_sandbox_settings()->IsTopicsAllowedForContext(
  275. GURL("https://embedded.com"),
  276. url::Origin::Create(GURL("https://test.com"))));
  277. EXPECT_TRUE(privacy_sandbox_settings()->IsTopicsAllowedForContext(
  278. GURL("https://unrelated.com"),
  279. url::Origin::Create(GURL("https://unrelated.com"))));
  280. EXPECT_FALSE(privacy_sandbox_settings()->IsConversionMeasurementAllowed(
  281. url::Origin::Create(GURL("https://test.com")),
  282. url::Origin::Create(GURL("https://embedded.com"))));
  283. EXPECT_FALSE(privacy_sandbox_settings()->ShouldSendConversionReport(
  284. url::Origin::Create(GURL("https://test.com")),
  285. url::Origin::Create(GURL("https://another-test.com")),
  286. url::Origin::Create(GURL("https://embedded.com"))));
  287. EXPECT_TRUE(privacy_sandbox_settings()->IsConversionMeasurementAllowed(
  288. url::Origin::Create(GURL("https://unrelated-a.com")),
  289. url::Origin::Create(GURL("https://unrelated-b.com"))));
  290. EXPECT_TRUE(privacy_sandbox_settings()->ShouldSendConversionReport(
  291. url::Origin::Create(GURL("https://unrelated-c.com")),
  292. url::Origin::Create(GURL("https://unrelated-d.com")),
  293. url::Origin::Create(GURL("https://unrelated-e.com"))));
  294. EXPECT_FALSE(privacy_sandbox_settings()->IsPrivateAggregationAllowed(
  295. url::Origin::Create(GURL("https://test.com")),
  296. url::Origin::Create(GURL("https://embedded.com"))));
  297. EXPECT_TRUE(privacy_sandbox_settings()->IsPrivateAggregationAllowed(
  298. url::Origin::Create(GURL("https://unrelated-a.com")),
  299. url::Origin::Create(GURL("https://unrelated-b.com"))));
  300. EXPECT_FALSE(privacy_sandbox_settings()->IsFledgeAllowed(
  301. url::Origin::Create(GURL("https://test.com")),
  302. url::Origin::Create(GURL("https://embedded.com"))));
  303. EXPECT_EQ(std::vector<GURL>{GURL("https://another-embedded.com")},
  304. privacy_sandbox_settings()->FilterFledgeAllowedParties(
  305. url::Origin::Create(GURL("https://test.com")),
  306. {GURL("https://embedded.com"),
  307. GURL("https://another-embedded.com")}));
  308. EXPECT_FALSE(privacy_sandbox_settings()->IsSharedStorageAllowed(
  309. url::Origin::Create(GURL("https://test.com")),
  310. url::Origin::Create(GURL("https://embedded.com"))));
  311. // A less specific block exception should not override a more specific allow
  312. // exception. The effective content setting in this scenario is still allow,
  313. // even though a block exception exists.
  314. privacy_sandbox_test_util::SetupTestState(
  315. prefs(), host_content_settings_map(),
  316. /*privacy_sandbox_enabled=*/true,
  317. /*block_third_party_cookies=*/false,
  318. /*default_cookie_setting=*/ContentSetting::CONTENT_SETTING_ALLOW,
  319. /*user_cookie_exceptions=*/
  320. {{"https://embedded.com", "https://test.com",
  321. ContentSetting::CONTENT_SETTING_ALLOW},
  322. {"https://embedded.com", "https://another-test.com",
  323. ContentSetting::CONTENT_SETTING_ALLOW},
  324. {"https://[*.]embedded.com", "https://[*.]test.com",
  325. ContentSetting::CONTENT_SETTING_BLOCK},
  326. {"https://[*.]embedded.com", "https://[*.]another-test.com",
  327. ContentSetting::CONTENT_SETTING_BLOCK}},
  328. /*managed_cookie_setting=*/privacy_sandbox_test_util::kNoSetting,
  329. /*managed_cookie_exceptions=*/{});
  330. EXPECT_TRUE(privacy_sandbox_settings()->IsTopicsAllowed());
  331. EXPECT_TRUE(privacy_sandbox_settings()->IsTopicsAllowedForContext(
  332. GURL("https://embedded.com"),
  333. url::Origin::Create(GURL("https://test.com"))));
  334. EXPECT_TRUE(privacy_sandbox_settings()->IsFledgeAllowed(
  335. url::Origin::Create(GURL("https://test.com")),
  336. url::Origin::Create(GURL("https://embedded.com"))));
  337. // Exceptions which specify a top frame origin should not match against other
  338. // top frame origins, or an empty origin.
  339. privacy_sandbox_test_util::SetupTestState(
  340. prefs(), host_content_settings_map(),
  341. /*privacy_sandbox_enabled=*/true,
  342. /*block_third_party_cookies=*/false,
  343. /*default_cookie_setting=*/ContentSetting::CONTENT_SETTING_ALLOW,
  344. /*user_cookie_exceptions=*/
  345. {{"https://embedded.com", "https://test.com",
  346. ContentSetting::CONTENT_SETTING_BLOCK}},
  347. /*managed_cookie_setting=*/privacy_sandbox_test_util::kNoSetting,
  348. /*managed_cookie_exceptions=*/
  349. {{"https://embedded.com", "https://test.com",
  350. ContentSetting::CONTENT_SETTING_BLOCK}});
  351. EXPECT_TRUE(privacy_sandbox_settings()->IsTopicsAllowed());
  352. EXPECT_TRUE(privacy_sandbox_settings()->IsTopicsAllowedForContext(
  353. GURL("https://embedded.com"), absl::nullopt));
  354. EXPECT_TRUE(privacy_sandbox_settings()->IsConversionMeasurementAllowed(
  355. url::Origin::Create(GURL("https://another-test.com")),
  356. url::Origin::Create(GURL("https://embedded.com"))));
  357. EXPECT_TRUE(privacy_sandbox_settings()->ShouldSendConversionReport(
  358. url::Origin::Create(GURL("https://another-test.com")),
  359. url::Origin::Create(GURL("https://yet-another-test.com")),
  360. url::Origin::Create(GURL("https://embedded.com"))));
  361. EXPECT_TRUE(privacy_sandbox_settings()->IsPrivateAggregationAllowed(
  362. url::Origin::Create(GURL("https://another-test.com")),
  363. url::Origin::Create(GURL("https://embedded.com"))));
  364. EXPECT_TRUE(privacy_sandbox_settings()->IsFledgeAllowed(
  365. url::Origin::Create(GURL("https://another-test.com")),
  366. url::Origin::Create(GURL("https://embedded.com"))));
  367. EXPECT_EQ(std::vector<GURL>{GURL("https://another-embedded.com/")},
  368. privacy_sandbox_settings()->FilterFledgeAllowedParties(
  369. url::Origin::Create(GURL("https://test.com")),
  370. {GURL("https://embedded.com"),
  371. GURL("https://another-embedded.com")}));
  372. EXPECT_TRUE(privacy_sandbox_settings()->IsSharedStorageAllowed(
  373. url::Origin::Create(GURL("https://another-test.com")),
  374. url::Origin::Create(GURL("https://embedded.com"))));
  375. // Exceptions which specify a wildcard top frame origin should match both
  376. // empty top frames and non empty top frames.
  377. privacy_sandbox_test_util::SetupTestState(
  378. prefs(), host_content_settings_map(),
  379. /*privacy_sandbox_enabled=*/true,
  380. /*block_third_party_cookies=*/false,
  381. /*default_cookie_setting=*/ContentSetting::CONTENT_SETTING_ALLOW,
  382. /*user_cookie_exceptions=*/
  383. {{"https://embedded.com", "*", ContentSetting::CONTENT_SETTING_BLOCK}},
  384. /*managed_cookie_setting=*/privacy_sandbox_test_util::kNoSetting,
  385. /*managed_cookie_exceptions=*/{});
  386. EXPECT_TRUE(privacy_sandbox_settings()->IsTopicsAllowed());
  387. EXPECT_FALSE(privacy_sandbox_settings()->IsTopicsAllowedForContext(
  388. GURL("https://embedded.com"), absl::nullopt));
  389. EXPECT_FALSE(privacy_sandbox_settings()->IsTopicsAllowedForContext(
  390. GURL("https://embedded.com"),
  391. url::Origin::Create(GURL("https://test.com"))));
  392. EXPECT_FALSE(privacy_sandbox_settings()->IsConversionMeasurementAllowed(
  393. url::Origin::Create(GURL("https://test.com")),
  394. url::Origin::Create(GURL("https://embedded.com"))));
  395. EXPECT_FALSE(privacy_sandbox_settings()->ShouldSendConversionReport(
  396. url::Origin::Create(GURL("https://test.com")),
  397. url::Origin::Create(GURL("https://another-test.com")),
  398. url::Origin::Create(GURL("https://embedded.com"))));
  399. EXPECT_FALSE(privacy_sandbox_settings()->IsPrivateAggregationAllowed(
  400. url::Origin::Create(GURL("https://test.com")),
  401. url::Origin::Create(GURL("https://embedded.com"))));
  402. EXPECT_FALSE(privacy_sandbox_settings()->IsFledgeAllowed(
  403. url::Origin::Create(GURL("https://test.com")),
  404. url::Origin::Create(GURL("https://embedded.com"))));
  405. EXPECT_EQ(std::vector<GURL>{GURL("https://another-embedded.com")},
  406. privacy_sandbox_settings()->FilterFledgeAllowedParties(
  407. url::Origin::Create(GURL("https://test.com")),
  408. {GURL("https://embedded.com"),
  409. GURL("https://another-embedded.com")}));
  410. EXPECT_FALSE(privacy_sandbox_settings()->IsSharedStorageAllowed(
  411. url::Origin::Create(GURL("https://test.com")),
  412. url::Origin::Create(GURL("https://embedded.com"))));
  413. }
  414. TEST_P(PrivacySandboxSettingsTest, ThirdPartyCookies) {
  415. // Privacy Sandbox APIs should be disabled if Third Party Cookies are blocked.
  416. privacy_sandbox_test_util::SetupTestState(
  417. prefs(), host_content_settings_map(),
  418. /*privacy_sandbox_enabled=*/true,
  419. /*block_third_party_cookies=*/true,
  420. /*default_cookie_setting=*/ContentSetting::CONTENT_SETTING_ALLOW,
  421. /*user_cookie_exceptions=*/{},
  422. /*managed_cookie_setting=*/privacy_sandbox_test_util::kNoSetting,
  423. /*managed_cookie_exceptions=*/{});
  424. EXPECT_FALSE(privacy_sandbox_settings()->IsTopicsAllowed());
  425. EXPECT_FALSE(privacy_sandbox_settings()->IsTopicsAllowedForContext(
  426. GURL("https://embedded.com"),
  427. url::Origin::Create(GURL("https://test.com"))));
  428. EXPECT_FALSE(privacy_sandbox_settings()->IsConversionMeasurementAllowed(
  429. url::Origin::Create(GURL("https://test.com")),
  430. url::Origin::Create(GURL("https://embedded.com"))));
  431. EXPECT_FALSE(privacy_sandbox_settings()->ShouldSendConversionReport(
  432. url::Origin::Create(GURL("https://test.com")),
  433. url::Origin::Create(GURL("https://another-test.com")),
  434. url::Origin::Create(GURL("https://embedded.com"))));
  435. EXPECT_FALSE(privacy_sandbox_settings()->IsPrivateAggregationAllowed(
  436. url::Origin::Create(GURL("https://test.com")),
  437. url::Origin::Create(GURL("https://embedded.com"))));
  438. EXPECT_FALSE(privacy_sandbox_settings()->IsFledgeAllowed(
  439. url::Origin::Create(GURL("https://test.com")),
  440. url::Origin::Create(GURL("https://embedded.com"))));
  441. EXPECT_EQ(std::vector<GURL>{},
  442. privacy_sandbox_settings()->FilterFledgeAllowedParties(
  443. url::Origin::Create(GURL("https://test.com")),
  444. {GURL("https://embedded.com"),
  445. GURL("https://another-embedded.com")}));
  446. EXPECT_FALSE(privacy_sandbox_settings()->IsSharedStorageAllowed(
  447. url::Origin::Create(GURL("https://test.com")),
  448. url::Origin::Create(GURL("https://embedded.com"))));
  449. // Privacy Sandbox APIs should be disabled if all cookies are blocked.
  450. privacy_sandbox_test_util::SetupTestState(
  451. prefs(), host_content_settings_map(),
  452. /*privacy_sandbox_enabled=*/true,
  453. /*block_third_party_cookies=*/false,
  454. /*default_cookie_setting=*/ContentSetting::CONTENT_SETTING_BLOCK,
  455. /*user_cookie_exceptions=*/{},
  456. /*managed_cookie_setting=*/privacy_sandbox_test_util::kNoSetting,
  457. /*managed_cookie_exceptions=*/{});
  458. EXPECT_FALSE(privacy_sandbox_settings()->IsTopicsAllowed());
  459. EXPECT_FALSE(privacy_sandbox_settings()->IsTopicsAllowedForContext(
  460. GURL("https://embedded.com"),
  461. url::Origin::Create(GURL("https://test.com"))));
  462. EXPECT_FALSE(privacy_sandbox_settings()->IsConversionMeasurementAllowed(
  463. url::Origin::Create(GURL("https://test.com")),
  464. url::Origin::Create(GURL("https://embedded.com"))));
  465. EXPECT_FALSE(privacy_sandbox_settings()->ShouldSendConversionReport(
  466. url::Origin::Create(GURL("https://test.com")),
  467. url::Origin::Create(GURL("https://another-test.com")),
  468. url::Origin::Create(GURL("https://embedded.com"))));
  469. EXPECT_FALSE(privacy_sandbox_settings()->IsPrivateAggregationAllowed(
  470. url::Origin::Create(GURL("https://test.com")),
  471. url::Origin::Create(GURL("https://embedded.com"))));
  472. EXPECT_FALSE(privacy_sandbox_settings()->IsFledgeAllowed(
  473. url::Origin::Create(GURL("https://test.com")),
  474. url::Origin::Create(GURL("https://embedded.com"))));
  475. EXPECT_EQ(std::vector<GURL>{},
  476. privacy_sandbox_settings()->FilterFledgeAllowedParties(
  477. url::Origin::Create(GURL("https://test.com")),
  478. {GURL("https://embedded.com"),
  479. GURL("https://another-embedded.com")}));
  480. EXPECT_FALSE(privacy_sandbox_settings()->IsSharedStorageAllowed(
  481. url::Origin::Create(GURL("https://test.com")),
  482. url::Origin::Create(GURL("https://embedded.com"))));
  483. // Privacy Sandbox APIs should be disabled if the privacy sandbox is disabled,
  484. // regardless of other cookie settings.
  485. privacy_sandbox_test_util::SetupTestState(
  486. prefs(), host_content_settings_map(),
  487. /*privacy_sandbox_enabled=*/false,
  488. /*block_third_party_cookies=*/false,
  489. /*default_cookie_setting=*/ContentSetting::CONTENT_SETTING_ALLOW,
  490. /*user_cookie_exceptions=*/
  491. {{"https://embedded.com", "https://test.com",
  492. ContentSetting::CONTENT_SETTING_ALLOW}},
  493. /*managed_cookie_setting=*/ContentSetting::CONTENT_SETTING_ALLOW,
  494. /*managed_cookie_exceptions=*/
  495. {{"https://embedded.com", "https://test.com",
  496. ContentSetting::CONTENT_SETTING_ALLOW}});
  497. EXPECT_FALSE(privacy_sandbox_settings()->IsTopicsAllowed());
  498. EXPECT_FALSE(privacy_sandbox_settings()->IsTopicsAllowedForContext(
  499. GURL("https://embedded.com"),
  500. url::Origin::Create(GURL("https://test.com"))));
  501. EXPECT_FALSE(privacy_sandbox_settings()->IsConversionMeasurementAllowed(
  502. url::Origin::Create(GURL("https://test.com")),
  503. url::Origin::Create(GURL("https://embedded.com"))));
  504. EXPECT_FALSE(privacy_sandbox_settings()->ShouldSendConversionReport(
  505. url::Origin::Create(GURL("https://test.com")),
  506. url::Origin::Create(GURL("https://another-test.com")),
  507. url::Origin::Create(GURL("https://embedded.com"))));
  508. EXPECT_FALSE(privacy_sandbox_settings()->IsPrivateAggregationAllowed(
  509. url::Origin::Create(GURL("https://test.com")),
  510. url::Origin::Create(GURL("https://embedded.com"))));
  511. EXPECT_FALSE(privacy_sandbox_settings()->IsFledgeAllowed(
  512. url::Origin::Create(GURL("https://test.com")),
  513. url::Origin::Create(GURL("https://embedded.com"))));
  514. EXPECT_EQ(std::vector<GURL>{},
  515. privacy_sandbox_settings()->FilterFledgeAllowedParties(
  516. url::Origin::Create(GURL("https://test.com")),
  517. {GURL("https://embedded.com"),
  518. GURL("https://another-embedded.com")}));
  519. EXPECT_FALSE(privacy_sandbox_settings()->IsSharedStorageAllowed(
  520. url::Origin::Create(GURL("https://test.com")),
  521. url::Origin::Create(GURL("https://embedded.com"))));
  522. }
  523. TEST_P(PrivacySandboxSettingsTest, IsPrivacySandboxEnabled) {
  524. // IsPrivacySandboxEnabled should directly reflect the state of the Privacy
  525. // Sandbox control.
  526. privacy_sandbox_test_util::SetupTestState(
  527. prefs(), host_content_settings_map(),
  528. /*privacy_sandbox_enabled=*/false,
  529. /*block_third_party_cookies=*/false,
  530. /*default_cookie_setting=*/ContentSetting::CONTENT_SETTING_ALLOW,
  531. /*user_cookie_exceptions=*/{},
  532. /*managed_cookie_setting=*/privacy_sandbox_test_util::kNoSetting,
  533. /*managed_cookie_exceptions=*/{});
  534. EXPECT_FALSE(privacy_sandbox_settings()->IsPrivacySandboxEnabled());
  535. privacy_sandbox_test_util::SetupTestState(
  536. prefs(), host_content_settings_map(),
  537. /*privacy_sandbox_enabled=*/false,
  538. /*block_third_party_cookies=*/true,
  539. /*default_cookie_setting=*/ContentSetting::CONTENT_SETTING_ALLOW,
  540. /*user_cookie_exceptions=*/{},
  541. /*managed_cookie_setting=*/privacy_sandbox_test_util::kNoSetting,
  542. /*managed_cookie_exceptions=*/{});
  543. EXPECT_FALSE(privacy_sandbox_settings()->IsPrivacySandboxEnabled());
  544. privacy_sandbox_test_util::SetupTestState(
  545. prefs(), host_content_settings_map(),
  546. /*privacy_sandbox_enabled=*/true,
  547. /*block_third_party_cookies=*/true,
  548. /*default_cookie_setting=*/ContentSetting::CONTENT_SETTING_BLOCK,
  549. /*user_cookie_exceptions=*/{},
  550. /*managed_cookie_setting=*/ContentSetting::CONTENT_SETTING_BLOCK,
  551. /*managed_cookie_exceptions=*/{});
  552. EXPECT_TRUE(privacy_sandbox_settings()->IsPrivacySandboxEnabled());
  553. }
  554. TEST_P(PrivacySandboxSettingsTest, TopicsDataAccessibleSince) {
  555. ASSERT_NE(base::Time(), base::Time::Now());
  556. EXPECT_EQ(base::Time(),
  557. privacy_sandbox_settings()->TopicsDataAccessibleSince());
  558. privacy_sandbox_settings()->OnCookiesCleared();
  559. EXPECT_EQ(base::Time::Now(),
  560. privacy_sandbox_settings()->TopicsDataAccessibleSince());
  561. }
  562. TEST_P(PrivacySandboxSettingsTest, FledgeJoiningAllowed) {
  563. // Whether or not a site can join a user to an interest group is independent
  564. // of any other profile state.
  565. privacy_sandbox_test_util::SetupTestState(
  566. prefs(), host_content_settings_map(),
  567. /*privacy_sandbox_enabled=*/false,
  568. /*block_third_party_cookies=*/true,
  569. /*default_cookie_setting=*/ContentSetting::CONTENT_SETTING_BLOCK,
  570. /*user_cookie_exceptions=*/
  571. {{"https://example.com", "*", ContentSetting::CONTENT_SETTING_BLOCK}},
  572. /*managed_cookie_setting=*/ContentSetting::CONTENT_SETTING_BLOCK,
  573. /*managed_cookie_exceptions=*/
  574. {{"https://example.com", "*", ContentSetting::CONTENT_SETTING_BLOCK}});
  575. EXPECT_TRUE(privacy_sandbox_settings()->IsFledgeJoiningAllowed(
  576. url::Origin::Create(GURL("https://example.com"))));
  577. // Settings should match at the eTLD + 1 level.
  578. privacy_sandbox_settings()->SetFledgeJoiningAllowed("example.com", false);
  579. EXPECT_FALSE(privacy_sandbox_settings()->IsFledgeJoiningAllowed(
  580. url::Origin::Create(GURL("https://subsite.example.com"))));
  581. EXPECT_FALSE(privacy_sandbox_settings()->IsFledgeJoiningAllowed(
  582. url::Origin::Create(GURL("http://example.com"))));
  583. EXPECT_FALSE(privacy_sandbox_settings()->IsFledgeJoiningAllowed(
  584. url::Origin::Create(GURL("https://example.com:888"))));
  585. EXPECT_FALSE(privacy_sandbox_settings()->IsFledgeJoiningAllowed(
  586. url::Origin::Create(GURL("https://example.com"))));
  587. EXPECT_TRUE(privacy_sandbox_settings()->IsFledgeJoiningAllowed(
  588. url::Origin::Create(GURL("https://example.com.au"))));
  589. privacy_sandbox_settings()->SetFledgeJoiningAllowed("example.com", true);
  590. EXPECT_TRUE(privacy_sandbox_settings()->IsFledgeJoiningAllowed(
  591. url::Origin::Create(GURL("https://example.com"))));
  592. EXPECT_TRUE(privacy_sandbox_settings()->IsFledgeJoiningAllowed(
  593. url::Origin::Create(GURL("https://subsite.example.com"))));
  594. EXPECT_TRUE(privacy_sandbox_settings()->IsFledgeJoiningAllowed(
  595. url::Origin::Create(GURL("http://example.com"))));
  596. EXPECT_TRUE(privacy_sandbox_settings()->IsFledgeJoiningAllowed(
  597. url::Origin::Create(GURL("https://example.com:888"))));
  598. EXPECT_TRUE(privacy_sandbox_settings()->IsFledgeJoiningAllowed(
  599. url::Origin::Create(GURL("https://example.com"))));
  600. EXPECT_TRUE(privacy_sandbox_settings()->IsFledgeJoiningAllowed(
  601. url::Origin::Create(GURL("https://example.com.au"))));
  602. }
  603. TEST_P(PrivacySandboxSettingsTest, NonEtldPlusOneBlocked) {
  604. // Confirm that, as a fallback, hosts are accepted by SetFledgeJoiningAllowed.
  605. privacy_sandbox_settings()->SetFledgeJoiningAllowed("subsite.example.com",
  606. false);
  607. // Applied setting should affect subdomaings.
  608. EXPECT_FALSE(privacy_sandbox_settings()->IsFledgeJoiningAllowed(
  609. url::Origin::Create(GURL("https://subsite.example.com"))));
  610. EXPECT_FALSE(privacy_sandbox_settings()->IsFledgeJoiningAllowed(
  611. url::Origin::Create(GURL("http://another.subsite.example.com"))));
  612. EXPECT_TRUE(privacy_sandbox_settings()->IsFledgeJoiningAllowed(
  613. url::Origin::Create(GURL("https://example.com"))));
  614. // When removing the setting, only an exact match, and not the associated
  615. // eTLD+1, should remove a setting.
  616. privacy_sandbox_settings()->SetFledgeJoiningAllowed("example.com", true);
  617. EXPECT_FALSE(privacy_sandbox_settings()->IsFledgeJoiningAllowed(
  618. url::Origin::Create(GURL("https://subsite.example.com"))));
  619. EXPECT_FALSE(privacy_sandbox_settings()->IsFledgeJoiningAllowed(
  620. url::Origin::Create(GURL("http://another.subsite.example.com"))));
  621. EXPECT_TRUE(privacy_sandbox_settings()->IsFledgeJoiningAllowed(
  622. url::Origin::Create(GURL("https://example.com"))));
  623. privacy_sandbox_settings()->SetFledgeJoiningAllowed("subsite.example.com",
  624. true);
  625. EXPECT_TRUE(privacy_sandbox_settings()->IsFledgeJoiningAllowed(
  626. url::Origin::Create(GURL("https://subsite.example.com"))));
  627. EXPECT_TRUE(privacy_sandbox_settings()->IsFledgeJoiningAllowed(
  628. url::Origin::Create(GURL("http://another.subsite.example.com"))));
  629. EXPECT_TRUE(privacy_sandbox_settings()->IsFledgeJoiningAllowed(
  630. url::Origin::Create(GURL("https://example.com"))));
  631. // IP addresses should also be accepted as a fallback.
  632. privacy_sandbox_settings()->SetFledgeJoiningAllowed("10.1.1.100", false);
  633. EXPECT_FALSE(privacy_sandbox_settings()->IsFledgeJoiningAllowed(
  634. url::Origin::Create(GURL("https://10.1.1.100"))));
  635. EXPECT_FALSE(privacy_sandbox_settings()->IsFledgeJoiningAllowed(
  636. url::Origin::Create(GURL("http://10.1.1.100:8080"))));
  637. EXPECT_TRUE(privacy_sandbox_settings()->IsFledgeJoiningAllowed(
  638. url::Origin::Create(GURL("https://10.2.2.200"))));
  639. }
  640. TEST_P(PrivacySandboxSettingsTest, FledgeJoinSettingTimeRangeDeletion) {
  641. // Confirm that time range deletions work appropriately for FLEDGE join
  642. // settings.
  643. privacy_sandbox_settings()->SetFledgeJoiningAllowed("first.com", false);
  644. task_environment()->AdvanceClock(base::Hours(1));
  645. const base::Time kSecondSettingTime = base::Time::Now();
  646. privacy_sandbox_settings()->SetFledgeJoiningAllowed("second.com", false);
  647. task_environment()->AdvanceClock(base::Hours(1));
  648. privacy_sandbox_settings()->SetFledgeJoiningAllowed("third.com", false);
  649. EXPECT_FALSE(privacy_sandbox_settings()->IsFledgeJoiningAllowed(
  650. url::Origin::Create(GURL("https://first.com"))));
  651. EXPECT_FALSE(privacy_sandbox_settings()->IsFledgeJoiningAllowed(
  652. url::Origin::Create(GURL("https://second.com"))));
  653. EXPECT_FALSE(privacy_sandbox_settings()->IsFledgeJoiningAllowed(
  654. url::Origin::Create(GURL("https://third.com"))));
  655. // Construct a deletion which only targets the second setting.
  656. privacy_sandbox_settings()->ClearFledgeJoiningAllowedSettings(
  657. kSecondSettingTime - base::Seconds(1),
  658. kSecondSettingTime + base::Seconds(1));
  659. EXPECT_FALSE(privacy_sandbox_settings()->IsFledgeJoiningAllowed(
  660. url::Origin::Create(GURL("https://first.com"))));
  661. EXPECT_TRUE(privacy_sandbox_settings()->IsFledgeJoiningAllowed(
  662. url::Origin::Create(GURL("https://second.com"))));
  663. EXPECT_FALSE(privacy_sandbox_settings()->IsFledgeJoiningAllowed(
  664. url::Origin::Create(GURL("https://third.com"))));
  665. // Perform a maximmal time range deletion, which should remove the two
  666. // remaining settings.
  667. privacy_sandbox_settings()->ClearFledgeJoiningAllowedSettings(
  668. base::Time(), base::Time::Max());
  669. EXPECT_TRUE(privacy_sandbox_settings()->IsFledgeJoiningAllowed(
  670. url::Origin::Create(GURL("https://first.com"))));
  671. EXPECT_TRUE(privacy_sandbox_settings()->IsFledgeJoiningAllowed(
  672. url::Origin::Create(GURL("https://second.com"))));
  673. EXPECT_TRUE(privacy_sandbox_settings()->IsFledgeJoiningAllowed(
  674. url::Origin::Create(GURL("https://third.com"))));
  675. }
  676. TEST_P(PrivacySandboxSettingsTest, TrustTokensAllowed) {
  677. // IsTrustTokensAllowed() should follow the top level privacy sandbox setting
  678. // as long as the release 3 feature is enabled, always returning true
  679. // otherwise
  680. base::test::ScopedFeatureList feature_list_;
  681. feature_list_.InitAndEnableFeature(privacy_sandbox::kPrivacySandboxSettings3);
  682. privacy_sandbox_test_util::MockPrivacySandboxObserver observer;
  683. privacy_sandbox_settings()->AddObserver(&observer);
  684. EXPECT_CALL(observer, OnTrustTokenBlockingChanged(/*blocked=*/true));
  685. privacy_sandbox_settings()->SetPrivacySandboxEnabled(false);
  686. EXPECT_FALSE(privacy_sandbox_settings()->IsTrustTokensAllowed());
  687. testing::Mock::VerifyAndClearExpectations(&observer);
  688. EXPECT_CALL(observer, OnTrustTokenBlockingChanged(/*blocked=*/false));
  689. privacy_sandbox_settings()->SetPrivacySandboxEnabled(true);
  690. EXPECT_TRUE(privacy_sandbox_settings()->IsTrustTokensAllowed());
  691. testing::Mock::VerifyAndClearExpectations(&observer);
  692. feature_list_.Reset();
  693. feature_list_.InitAndDisableFeature(
  694. privacy_sandbox::kPrivacySandboxSettings3);
  695. EXPECT_CALL(observer, OnTrustTokenBlockingChanged(testing::_)).Times(0);
  696. privacy_sandbox_settings()->SetPrivacySandboxEnabled(false);
  697. EXPECT_TRUE(privacy_sandbox_settings()->IsTrustTokensAllowed());
  698. privacy_sandbox_settings()->SetPrivacySandboxEnabled(true);
  699. EXPECT_TRUE(privacy_sandbox_settings()->IsTrustTokensAllowed());
  700. }
  701. TEST_P(PrivacySandboxSettingsTest, IsTopicAllowed) {
  702. // Confirm that allowing / blocking topics is correctly reflected by
  703. // IsTopicsAllowed().
  704. CanonicalTopic topic_one(Topic(1), CanonicalTopic::AVAILABLE_TAXONOMY);
  705. CanonicalTopic topic_two(Topic(2), CanonicalTopic::AVAILABLE_TAXONOMY);
  706. privacy_sandbox_settings()->SetTopicAllowed(topic_one, false);
  707. EXPECT_FALSE(privacy_sandbox_settings()->IsTopicAllowed(topic_one));
  708. EXPECT_TRUE(privacy_sandbox_settings()->IsTopicAllowed(topic_two));
  709. privacy_sandbox_settings()->SetTopicAllowed(topic_two, false);
  710. EXPECT_FALSE(privacy_sandbox_settings()->IsTopicAllowed(topic_one));
  711. EXPECT_FALSE(privacy_sandbox_settings()->IsTopicAllowed(topic_two));
  712. privacy_sandbox_settings()->SetTopicAllowed(topic_two, true);
  713. EXPECT_FALSE(privacy_sandbox_settings()->IsTopicAllowed(topic_one));
  714. EXPECT_TRUE(privacy_sandbox_settings()->IsTopicAllowed(topic_two));
  715. privacy_sandbox_settings()->SetTopicAllowed(topic_one, true);
  716. EXPECT_TRUE(privacy_sandbox_settings()->IsTopicAllowed(topic_one));
  717. EXPECT_TRUE(privacy_sandbox_settings()->IsTopicAllowed(topic_two));
  718. }
  719. TEST_P(PrivacySandboxSettingsTest, ClearingTopicSettings) {
  720. // Confirm that time range deletions affect the correct settings.
  721. CanonicalTopic topic_one(Topic(1), CanonicalTopic::AVAILABLE_TAXONOMY);
  722. CanonicalTopic topic_two(Topic(2), CanonicalTopic::AVAILABLE_TAXONOMY);
  723. CanonicalTopic topic_three(Topic(3), CanonicalTopic::AVAILABLE_TAXONOMY);
  724. EXPECT_TRUE(privacy_sandbox_settings()->IsTopicAllowed(topic_one));
  725. EXPECT_TRUE(privacy_sandbox_settings()->IsTopicAllowed(topic_two));
  726. EXPECT_TRUE(privacy_sandbox_settings()->IsTopicAllowed(topic_three));
  727. privacy_sandbox_settings()->SetTopicAllowed(topic_one, false);
  728. task_environment()->AdvanceClock(base::Hours(1));
  729. const auto kSecondSettingTime = base::Time::Now();
  730. privacy_sandbox_settings()->SetTopicAllowed(topic_two, false);
  731. task_environment()->AdvanceClock(base::Hours(1));
  732. privacy_sandbox_settings()->SetTopicAllowed(topic_three, false);
  733. EXPECT_FALSE(privacy_sandbox_settings()->IsTopicAllowed(topic_one));
  734. EXPECT_FALSE(privacy_sandbox_settings()->IsTopicAllowed(topic_two));
  735. EXPECT_FALSE(privacy_sandbox_settings()->IsTopicAllowed(topic_three));
  736. // Construct a deletion which only targets the second setting.
  737. privacy_sandbox_settings()->ClearTopicSettings(
  738. kSecondSettingTime - base::Seconds(1),
  739. kSecondSettingTime + base::Seconds(1));
  740. EXPECT_FALSE(privacy_sandbox_settings()->IsTopicAllowed(topic_one));
  741. EXPECT_TRUE(privacy_sandbox_settings()->IsTopicAllowed(topic_two));
  742. EXPECT_FALSE(privacy_sandbox_settings()->IsTopicAllowed(topic_three));
  743. // Perform a maximmal time range deletion, which should remove the two
  744. // remaining settings.
  745. privacy_sandbox_settings()->ClearTopicSettings(base::Time(),
  746. base::Time::Max());
  747. EXPECT_TRUE(privacy_sandbox_settings()->IsTopicAllowed(topic_one));
  748. EXPECT_TRUE(privacy_sandbox_settings()->IsTopicAllowed(topic_two));
  749. EXPECT_TRUE(privacy_sandbox_settings()->IsTopicAllowed(topic_three));
  750. }
  751. INSTANTIATE_TEST_SUITE_P(PrivacySandboxSettingsTestInstance,
  752. PrivacySandboxSettingsTest,
  753. testing::Bool());
  754. class PrivacySandboxSettingsTestCookiesClearOnExitTurnedOff
  755. : public PrivacySandboxSettingsTest {
  756. public:
  757. void InitializePrefsBeforeStart() override {
  758. prefs()->SetUserPref(prefs::kPrivacySandboxTopicsDataAccessibleSince,
  759. std::make_unique<base::Value>(::base::TimeToValue(
  760. base::Time::FromTimeT(12345))));
  761. }
  762. void InitializeFeaturesBeforeStart() override {}
  763. };
  764. TEST_P(PrivacySandboxSettingsTestCookiesClearOnExitTurnedOff,
  765. UseLastTopicsDataAccessibleSince) {
  766. EXPECT_EQ(base::Time::FromTimeT(12345),
  767. privacy_sandbox_settings()->TopicsDataAccessibleSince());
  768. }
  769. INSTANTIATE_TEST_SUITE_P(
  770. PrivacySandboxSettingsTestCookiesClearOnExitTurnedOffInstance,
  771. PrivacySandboxSettingsTestCookiesClearOnExitTurnedOff,
  772. testing::Bool());
  773. class PrivacySandboxSettingsTestCookiesClearOnExitTurnedOn
  774. : public PrivacySandboxSettingsTest {
  775. public:
  776. void InitializePrefsBeforeStart() override {
  777. host_content_settings_map()->SetDefaultContentSetting(
  778. ContentSettingsType::COOKIES,
  779. ContentSetting::CONTENT_SETTING_SESSION_ONLY);
  780. prefs()->SetUserPref(prefs::kPrivacySandboxTopicsDataAccessibleSince,
  781. std::make_unique<base::Value>(::base::TimeToValue(
  782. base::Time::FromTimeT(12345))));
  783. }
  784. };
  785. TEST_P(PrivacySandboxSettingsTestCookiesClearOnExitTurnedOn,
  786. UpdateTopicsDataAccessibleSince) {
  787. EXPECT_EQ(base::Time::Now(),
  788. privacy_sandbox_settings()->TopicsDataAccessibleSince());
  789. }
  790. INSTANTIATE_TEST_SUITE_P(
  791. PrivacySandboxSettingsTestCookiesClearOnExitTurnedOnInstance,
  792. PrivacySandboxSettingsTestCookiesClearOnExitTurnedOn,
  793. testing::Bool());
  794. class PrivacySandboxSettingsIncognitoTest : public PrivacySandboxSettingsTest {
  795. bool IsIncognitoProfile() override { return true; }
  796. };
  797. TEST_P(PrivacySandboxSettingsIncognitoTest, DisabledInIncognito) {
  798. // When the Release 3 flag is enabled, APIs should always be disabled in
  799. // incognito. The Release 3 flag is set based on the test param.
  800. privacy_sandbox_settings()->SetPrivacySandboxEnabled(true);
  801. if (GetParam())
  802. EXPECT_FALSE(privacy_sandbox_settings()->IsPrivacySandboxEnabled());
  803. else
  804. EXPECT_TRUE(privacy_sandbox_settings()->IsPrivacySandboxEnabled());
  805. }
  806. INSTANTIATE_TEST_SUITE_P(PrivacySandboxSettingsIncognitoTestInstance,
  807. PrivacySandboxSettingsIncognitoTest,
  808. testing::Bool());
  809. class PrivacySandboxSettingsMockDelegateTest
  810. : public PrivacySandboxSettingsTest {
  811. public:
  812. void InitializeDelegateBeforeStart() override {
  813. // Do not set default handlers so each call must be mocked.
  814. }
  815. };
  816. TEST_P(PrivacySandboxSettingsMockDelegateTest, IsPrivacySandboxRestricted) {
  817. // When the sandbox is otherwise enabled, the delegate returning true for
  818. // IsPrivacySandboxRestricted() should disable the sandbox.
  819. privacy_sandbox_settings()->SetPrivacySandboxEnabled(true);
  820. EXPECT_CALL(*mock_delegate(), IsPrivacySandboxRestricted())
  821. .Times(1)
  822. .WillOnce(testing::Return(true));
  823. EXPECT_FALSE(privacy_sandbox_settings()->IsPrivacySandboxEnabled());
  824. EXPECT_CALL(*mock_delegate(), IsPrivacySandboxRestricted())
  825. .Times(1)
  826. .WillOnce(testing::Return(false));
  827. EXPECT_TRUE(privacy_sandbox_settings()->IsPrivacySandboxEnabled());
  828. // The delegate should not override a disabled sandbox.
  829. privacy_sandbox_settings()->SetPrivacySandboxEnabled(false);
  830. EXPECT_CALL(*mock_delegate(), IsPrivacySandboxRestricted())
  831. .Times(1)
  832. .WillOnce(testing::Return(false));
  833. EXPECT_FALSE(privacy_sandbox_settings()->IsPrivacySandboxEnabled());
  834. }
  835. INSTANTIATE_TEST_SUITE_P(PrivacySandboxSettingsMockDelegateTestInstance,
  836. PrivacySandboxSettingsMockDelegateTest,
  837. testing::Bool());
  838. class PrivacySandboxSettingLocalOverrideTest
  839. : public PrivacySandboxSettingsTest {
  840. void InitializeFeaturesBeforeStart() override {
  841. if (GetParam()) {
  842. feature_list_.InitWithFeatures(
  843. {privacy_sandbox::kPrivacySandboxSettings3,
  844. privacy_sandbox::kOverridePrivacySandboxSettingsLocalTesting},
  845. /*disabled_features=*/{});
  846. } else {
  847. feature_list_.InitWithFeatures(
  848. {privacy_sandbox::kPrivacySandboxSettings3},
  849. {privacy_sandbox::kOverridePrivacySandboxSettingsLocalTesting});
  850. }
  851. }
  852. };
  853. TEST_P(PrivacySandboxSettingLocalOverrideTest, FollowsOverrideBehavior) {
  854. // When the Release 3 flag is enabled, APIs should always be disabled in
  855. // incognito. The Release 3 flag is set based on the test param.
  856. privacy_sandbox_settings()->SetPrivacySandboxEnabled(false);
  857. if (GetParam())
  858. EXPECT_TRUE(privacy_sandbox_settings()->IsPrivacySandboxEnabled());
  859. else
  860. EXPECT_FALSE(privacy_sandbox_settings()->IsPrivacySandboxEnabled());
  861. }
  862. INSTANTIATE_TEST_SUITE_P(PrivacySandboxSettingLocalOverrideTestInstance,
  863. PrivacySandboxSettingLocalOverrideTest,
  864. testing::Bool());
  865. } // namespace privacy_sandbox