File: validity_service_browsertest.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (842 lines) | stat: -rw-r--r-- 38,642 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/tpcd/support/validity_service.h"

#include "base/strings/strcat.h"
#include "base/test/bind.h"
#include "base/test/scoped_feature_list.h"
#include "chrome/browser/content_settings/cookie_settings_factory.h"
#include "chrome/browser/content_settings/host_content_settings_map_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/tpcd/support/top_level_trial_service.h"
#include "chrome/browser/tpcd/support/top_level_trial_service_factory.h"
#include "chrome/browser/tpcd/support/tpcd_support_service.h"
#include "chrome/browser/tpcd/support/tpcd_support_service_factory.h"
#include "chrome/browser/tpcd/support/trial_test_utils.h"
#include "chrome/test/base/chrome_test_utils.h"
#include "chrome/test/base/platform_browser_test.h"
#include "components/content_settings/core/browser/content_settings_observer.h"
#include "components/content_settings/core/browser/cookie_settings.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_types.h"
#include "components/content_settings/core/common/features.h"
#include "components/content_settings/core/common/pref_names.h"
#include "components/network_session_configurator/common/network_switches.h"
#include "components/prefs/pref_service.h"
#include "components/privacy_sandbox/tracking_protection_prefs.h"
#include "components/user_prefs/user_prefs.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/navigation_handle.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_contents_observer.h"
#include "content/public/common/content_features.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/url_loader_interceptor.h"
#include "net/base/features.h"
#include "net/cookies/cookie_util.h"
#include "net/dns/mock_host_resolver.h"
#include "net/test/embedded_test_server/embedded_test_server.h"

using content::URLLoaderInterceptor;
using content::WebContents;

namespace tpcd::trial {

class ValidityServiceBrowserTestBase : public PlatformBrowserTest {
 public:
  void SetUpCommandLine(base::CommandLine* command_line) override {
    command_line->AppendSwitchASCII("origin-trial-public-key",
                                    kTestTokenPublicKey);
    command_line->AppendSwitch(switches::kIgnoreCertificateErrors);
  }

  void SetUp() override {
    features_.InitWithFeaturesAndParameters(
        {{net::features::kTpcdTrialSettings, {}},
         {content_settings::features::kTrackingProtection3pcd, {}}},
        {});

    PlatformBrowserTest::SetUp();
  }

  void SetUpOnMainThread() override {
    ASSERT_TRUE(embedded_test_server()->Start());
    host_resolver()->AddRule("*", "127.0.0.1");

    https_server_ = std::make_unique<net::EmbeddedTestServer>(
        net::EmbeddedTestServer::TYPE_HTTPS);
    https_server_->SetSSLConfig(net::EmbeddedTestServer::CERT_TEST_NAMES);
    https_server_->AddDefaultHandlers(
        base::FilePath(FILE_PATH_LITERAL("chrome/test/data/")));
    ASSERT_TRUE(https_server_->Start());

    // We use a URLLoaderInterceptor in tests that actually use an origin trial
    // token, rather than the EmbeddedTestServer, since the origin trial token
    // in the response is associated with a fixed origin, whereas
    // EmbeddedTestServer serves content on a random port.
    url_loader_interceptor_ =
        std::make_unique<URLLoaderInterceptor>(base::BindLambdaForTesting(
            [this](URLLoaderInterceptor::RequestParams* params) {
              return OnRequest(params);
            }));

    GetPrefs()->SetBoolean(prefs::kTrackingProtection3pcdEnabled, true);
  }

  void TearDownOnMainThread() override {
    https_server_.reset();
    url_loader_interceptor_.reset();
    PlatformBrowserTest::TearDownOnMainThread();
  }

 protected:
  content::WebContents* GetActiveWebContents() {
    return chrome_test_utils::GetActiveWebContents(this);
  }

  Profile* GetProfile() {
    return Profile::FromBrowserContext(
        GetActiveWebContents()->GetBrowserContext());
  }

  PrefService* GetPrefs() {
    return user_prefs::UserPrefs::Get(
        GetActiveWebContents()->GetBrowserContext());
  }

  TopLevelTrialService* GetTopLevelTrialService() {
    return TopLevelTrialServiceFactory::GetForProfile(GetProfile());
  }

  TpcdTrialService* GetTpcdTrialService() {
    return TpcdTrialServiceFactory::GetForProfile(GetProfile());
  }

  ValidityService* GetValidityService() {
    return ValidityService::FromWebContents(GetActiveWebContents());
  }

  content::RenderFrameHost* GetIFrame() {
    content::WebContents* web_contents = GetActiveWebContents();
    return ChildFrameAt(web_contents->GetPrimaryMainFrame(), 0);
  }

  [[nodiscard]] bool NavigateIFrameAndWaitForCookieAccess(GURL iframe_url) {
    content::WebContents* web_contents = GetActiveWebContents();

    URLCookieAccessObserver observer(web_contents, iframe_url,
                                     CookieOperation::kChange);
    const std::string kIframeId = "test";  // defined in iframe_blank.html
    bool success =
        content::NavigateIframeToURL(web_contents, kIframeId, iframe_url);
    if (success) {
      observer.Wait();
    }
    return success;
  }

  void NavigateToPageWithIFrame(const std::string& iframe_host,
                                const std::string& embedding_host) {
    content::WebContents* web_contents = GetActiveWebContents();

    // Navigate the top-level page to |embedding_site|.
    GURL embedding_site =
        https_server_->GetURL(embedding_host, "/iframe_blank.html");
    ASSERT_TRUE(content::NavigateToURL(web_contents, embedding_site));

    // Open an |iframe_host| iframe.
    GURL iframe_url = https_server_->GetURL(iframe_host, "/title1.html");
    const std::string kIframeId = "test";  // defined in iframe_blank.html
    ASSERT_TRUE(
        content::NavigateIframeToURL(web_contents, kIframeId, iframe_url));
  }

  // Most other cookie-related content settings compare their primary patterns'
  // against embedded/requesting sites and their secondary patterns'
  // against top-level sites. This convenience function helps avoid confusion
  // since |TOP_LEVEL_TPCD_TRIAL| content settings only use a primary pattern
  // which is compared against top-level sites.
  ContentSettingChangeObserver CreateTopLevelTrialSettingsObserver(GURL url) {
    return ContentSettingChangeObserver(
        GetProfile(), url, GURL(), ContentSettingsType::TOP_LEVEL_TPCD_TRIAL);
  }

  // Creates a |TPCD_TRIAL| content setting allowing
  // |embedded_url| to access third-party cookies under |top_level_url|
  // without actually providing an origin trial token and enabling the
  // associated origin trial.
  void CreateAndVerifyThirdPartyTrialGrant(const GURL& embedded_url,
                                           const GURL& top_level_url,
                                           bool match_subdomains) {
    // Create the content setting.
    ContentSettingChangeObserver setting_observer(
        GetActiveWebContents()->GetBrowserContext(), embedded_url,
        top_level_url, ContentSettingsType::TPCD_TRIAL);
    GetTpcdTrialService()->Update3pcdTrialSettingsForTesting(
        OriginTrialStatusChangeDetails(url::Origin::Create(embedded_url),
                                       top_level_url.spec(), match_subdomains,
                                       /*enabled=*/true,
                                       /*source_id=*/std::nullopt));
    setting_observer.Wait();

    // Verify that a |TPCD_TRIAL| content setting now allows |embedded_url|
    // access to cookies as a third-party when embedded by |top_level_url|.
    content_settings::CookieSettings* settings =
        CookieSettingsFactory::GetForProfile(GetProfile()).get();
    ASSERT_EQ(settings->GetCookieSetting(embedded_url, net::SiteForCookies(),
                                         top_level_url, {}, nullptr),
              CONTENT_SETTING_ALLOW);
    ASSERT_EQ(settings->GetThirdPartyCookieAllowMechanism(
                  embedded_url, net::SiteForCookies::FromUrl(top_level_url),
                  top_level_url, {}, nullptr),
              content_settings::CookieSettingsBase::
                  ThirdPartyCookieAllowMechanism::kAllowBy3PCD);
  }

  // Creates a |TOP_LEVEL_TPCD_TRIAL| content setting allowing sites embedded
  // under |top_level_url| to access third-party cookies without actually
  // providing an origin trial token and enabling the associated origin trial.
  void CreateAndVerifyFirstPartyTrialGrant(const GURL& top_level_url,
                                           bool match_subdomains) {
    // Create the content setting.
    ContentSettingChangeObserver setting_observer =
        CreateTopLevelTrialSettingsObserver(top_level_url);
    GetTopLevelTrialService()->UpdateTopLevelTrialSettingsForTesting(
        url::Origin::Create(top_level_url), match_subdomains, /*enabled=*/true);
    setting_observer.Wait();

    // Verify that a |TOP_LEVEL_TPCD_TRIAL| content setting now allows all sites
    // access to cookies as a third-party when embedded by |top_level_url|.
    content_settings::CookieSettings* settings =
        CookieSettingsFactory::GetForProfile(GetProfile()).get();
    ASSERT_EQ(settings->GetCookieSetting(GURL(), net::SiteForCookies(),
                                         top_level_url, {}, nullptr),
              CONTENT_SETTING_ALLOW);
    ASSERT_EQ(settings->GetThirdPartyCookieAllowMechanism(
                  GURL(), net::SiteForCookies(), top_level_url, {}, nullptr),
              content_settings::CookieSettingsBase::
                  ThirdPartyCookieAllowMechanism::kAllowByTopLevel3PCD);
  }

  virtual bool OnRequest(content::URLLoaderInterceptor::RequestParams* params) {
    return false;
  }

  base::test::ScopedFeatureList features_;
  std::unique_ptr<net::EmbeddedTestServer> https_server_;
  std::unique_ptr<URLLoaderInterceptor> url_loader_interceptor_;
  const GURL kTrialEnabledSite{base::StrCat({"https://", kTrialEnabledDomain})};
  const GURL kTrialEnabledSiteSubdomain{
      base::StrCat({"https://", kTrialEnabledSubdomain})};
};

class ValidityService3pTrialBrowserTest
    : public ValidityServiceBrowserTestBase {
  void SetUp() override {
    features_.InitWithFeaturesAndParameters(
        {{net::features::kTpcdTrialSettings, {}},
         {content_settings::features::kTrackingProtection3pcd, {}}},
        {});

    PlatformBrowserTest::SetUp();
  }

  bool OnRequest(
      content::URLLoaderInterceptor::RequestParams* params) override {
    std::string host = params->url_request.url.host();
    std::string path = params->url_request.url.path().substr(1);

    if (host != kTrialEnabledDomain || path != kTrialEnabledIframePath) {
      return false;
    }

    URLLoaderInterceptor::WriteResponse(
        base::StrCat({"HTTP/1.1 200 OK\n", "Content-type: text/html\n",
                      "Origin-Trial: ", kTrialToken, "\n", "\n"}),
        "", params->client.get());
    return true;
  }
};

IN_PROC_BROWSER_TEST_F(ValidityService3pTrialBrowserTest,
                       RemovesInvalidSettingOnJsCookieAccess) {
  content::WebContents* web_contents = GetActiveWebContents();
  content_settings::CookieSettings* settings =
      CookieSettingsFactory::GetForProfile(GetProfile()).get();

  NavigateToPageWithIFrame(kTrialEnabledSite.host(), "a.test");

  // Create a |TPCD_TRIAL| setting for |iframe_url| under |top_level_url|
  // without actually enabling the "Tpcd" trial.
  GURL top_level_url =
      web_contents->GetPrimaryMainFrame()->GetLastCommittedURL();
  GURL iframe_url = GetIFrame()->GetLastCommittedURL();
  CreateAndVerifyThirdPartyTrialGrant(iframe_url, top_level_url,
                                      /*match_subdomains=*/false);

  // Access cookies via javascript in the iframe, which should cause the setting
  // to be removed.
  ContentSettingChangeObserver setting_observer(
      web_contents->GetBrowserContext(), iframe_url, top_level_url,
      ContentSettingsType::TPCD_TRIAL);
  AccessCookieViaJsIn(web_contents, GetIFrame());
  setting_observer.Wait();

  // Verify |iframe_url| no longer has third-party cookie access when
  // embedded by |top_level_url|.
  EXPECT_EQ(settings->GetCookieSetting(iframe_url, net::SiteForCookies(),
                                       top_level_url, {}, nullptr),
            CONTENT_SETTING_BLOCK);
}

IN_PROC_BROWSER_TEST_F(ValidityService3pTrialBrowserTest,
                       RemoveInvalidSubdomainMatchingSettingOnJsCookieAccess) {
  content::WebContents* web_contents = GetActiveWebContents();
  content_settings::CookieSettings* settings =
      CookieSettingsFactory::GetForProfile(GetProfile()).get();

  NavigateToPageWithIFrame(kTrialEnabledSiteSubdomain.host(), "a.test");

  // Create a subdomain-matching |TPCD_TRIAL| setting for |grant_url|
  // (which |iframe_url| is a subdomain of) under |top_level_url| without
  // actually enabling the "Tpcd" trial.
  GURL top_level_url =
      web_contents->GetPrimaryMainFrame()->GetLastCommittedURL();
  GURL iframe_url = GetIFrame()->GetLastCommittedURL();
  GURL grant_url = GURL(base::StrCat(
      {"https://", kTrialEnabledSite.host(), ":", iframe_url.port()}));
  CreateAndVerifyThirdPartyTrialGrant(grant_url, top_level_url,
                                      /*match_subdomains=*/true);

  // Access cookies via javascript in the iframe, which should cause the setting
  // to be removed.
  ContentSettingChangeObserver setting_observer(
      web_contents->GetBrowserContext(), iframe_url, top_level_url,
      ContentSettingsType::TPCD_TRIAL);
  AccessCookieViaJsIn(web_contents, GetIFrame());
  setting_observer.Wait();

  // Verify |iframe_url| and |grant_url| no longer have
  // third-party cookie access when embedded by |top_level_url|.
  EXPECT_EQ(settings->GetCookieSetting(iframe_url, net::SiteForCookies(),
                                       top_level_url, {}, nullptr),
            CONTENT_SETTING_BLOCK);
  EXPECT_EQ(settings->GetCookieSetting(grant_url, net::SiteForCookies(),
                                       top_level_url, {}, nullptr),
            CONTENT_SETTING_BLOCK);
}

IN_PROC_BROWSER_TEST_F(ValidityService3pTrialBrowserTest,
                       RemoveInvalidSettingOnNavigationCookieAccess) {
  content::WebContents* web_contents = GetActiveWebContents();
  content_settings::CookieSettings* settings =
      CookieSettingsFactory::GetForProfile(GetProfile()).get();

  // Navigate the top-level page to |top_level_url|.
  GURL top_level_url = https_server_->GetURL("a.test", "/iframe_blank.html");
  ASSERT_TRUE(content::NavigateToURL(web_contents, top_level_url));

  // Create a |TPCD_TRIAL| setting for |iframe_url| under |top_level_url|
  // without actually enabling the "Tpcd" trial.
  GURL iframe_url = https_server_->GetURL(
      kTrialEnabledSite.host(), "/set-cookie?name=value;Secure;SameSite=None");
  CreateAndVerifyThirdPartyTrialGrant(iframe_url, top_level_url,
                                      /*match_subdomains=*/false);

  // Navigate the iframe to |iframe_url| to set a cookie via a network response,
  // which should cause the setting to be removed.
  ContentSettingChangeObserver setting_observer(
      web_contents->GetBrowserContext(), iframe_url, top_level_url,
      ContentSettingsType::TPCD_TRIAL);
  ASSERT_TRUE(NavigateIFrameAndWaitForCookieAccess(iframe_url));
  setting_observer.Wait();

  // Verify |iframe_url| no longer has third-party cookie access when embedded
  // by |top_level_url|.
  EXPECT_EQ(settings->GetCookieSetting(iframe_url, net::SiteForCookies(),
                                       top_level_url, {}, nullptr),
            CONTENT_SETTING_BLOCK);
}

IN_PROC_BROWSER_TEST_F(
    ValidityService3pTrialBrowserTest,
    RemoveInvalidSubdomainMatchingSettingOnNavigationCookieAccess) {
  content::WebContents* web_contents = GetActiveWebContents();
  content_settings::CookieSettings* settings =
      CookieSettingsFactory::GetForProfile(GetProfile()).get();

  // Navigate the top-level page to |top_level_url|.
  GURL top_level_url = https_server_->GetURL("a.test", "/iframe_blank.html");
  ASSERT_TRUE(content::NavigateToURL(web_contents, top_level_url));

  // Create a subdomain-matching |TPCD_TRIAL| setting for |grant_url|
  // (which |iframe_url| is a subdomain of) under |top_level_url| without
  // actually enabling the "Tpcd" trial.
  GURL iframe_url =
      https_server_->GetURL(kTrialEnabledSiteSubdomain.host(),
                            "/set-cookie?name=value;Secure;SameSite=None");
  GURL grant_url = GURL(base::StrCat(
      {"https://", kTrialEnabledSite.host(), ":", iframe_url.port()}));
  CreateAndVerifyThirdPartyTrialGrant(grant_url, top_level_url,
                                      /*match_subdomains=*/true);

  // Navigate the iframe to |iframe_url| to set a cookie via a network response,
  // which should cause the setting to be removed.
  ContentSettingChangeObserver setting_observer(
      web_contents->GetBrowserContext(), iframe_url, top_level_url,
      ContentSettingsType::TPCD_TRIAL);
  ASSERT_TRUE(NavigateIFrameAndWaitForCookieAccess(iframe_url));
  setting_observer.Wait();

  // Verify |iframe_url| and |grant_url| no longer have
  // third-party cookie access when embedded by |top_level_url|.
  EXPECT_EQ(settings->GetCookieSetting(iframe_url, net::SiteForCookies(),
                                       top_level_url, {}, nullptr),
            CONTENT_SETTING_BLOCK);
  EXPECT_EQ(settings->GetCookieSetting(grant_url, net::SiteForCookies(),
                                       top_level_url, {}, nullptr),
            CONTENT_SETTING_BLOCK);
}

IN_PROC_BROWSER_TEST_F(ValidityService3pTrialBrowserTest,
                       RemoveAllSettingsCreatedUsingAffectedToken) {
  content::WebContents* web_contents = GetActiveWebContents();
  content_settings::CookieSettings* settings =
      CookieSettingsFactory::GetForProfile(GetProfile()).get();

  NavigateToPageWithIFrame(kTrialEnabledSite.host(), "a.test");
  GURL iframe_url = GetIFrame()->GetLastCommittedURL();
  GURL top_level_url =
      web_contents->GetPrimaryMainFrame()->GetLastCommittedURL();

  // Create |TPCD_TRIAL| settings for |iframe_url| under various top-level
  // sites.
  CreateAndVerifyThirdPartyTrialGrant(iframe_url, top_level_url,
                                      /*match_subdomains=*/false);
  CreateAndVerifyThirdPartyTrialGrant(iframe_url, GURL("https://b.test"),
                                      /*match_subdomains=*/false);
  CreateAndVerifyThirdPartyTrialGrant(iframe_url, GURL("https://c.test"),
                                      /*match_subdomains=*/false);
  // Note: this setting matches subdomains, while the others don't, meaning it
  // would've been created using a different origin trial token.
  GURL other_top_level_url("https://other-top-level.test");
  CreateAndVerifyThirdPartyTrialGrant(iframe_url, other_top_level_url,
                                      /*match_subdomains=*/true);

  // Also, create a |TPCD_TRIAL| setting for |other_embedded_url| under
  // |top_level_url|.
  GURL other_embedded_url("https://another-embedded-site.test");
  CreateAndVerifyThirdPartyTrialGrant(other_embedded_url, top_level_url,
                                      /*match_subdomains=*/false);

  // Access cookies via javascript in the iframe, which should cause the setting
  // to be removed.
  ContentSettingChangeObserver setting_observer(
      web_contents->GetBrowserContext(), iframe_url, top_level_url,
      ContentSettingsType::TPCD_TRIAL);
  AccessCookieViaJsIn(web_contents, GetIFrame());
  setting_observer.Wait();

  // Verify |TPCD_TRIAL| content settings with the same primary pattern as the
  // setting that allowed 3PC access in the iframe have been removed.
  EXPECT_EQ(settings->GetCookieSetting(iframe_url, net::SiteForCookies(),
                                       top_level_url, {}, nullptr),
            CONTENT_SETTING_BLOCK);
  EXPECT_EQ(settings->GetCookieSetting(iframe_url, net::SiteForCookies(),
                                       GURL("https://b.test"), {}, nullptr),
            CONTENT_SETTING_BLOCK);
  EXPECT_EQ(settings->GetCookieSetting(iframe_url, net::SiteForCookies(),
                                       GURL("https://c.test"), {}, nullptr),
            CONTENT_SETTING_BLOCK);

  // Note: since the setting allowing |iframe_url| to access 3PC under
  // |other_top_level_url| matches subdomains and the setting that
  // allowed access in the iframe context did not, the setting for 3PC access
  // under |other_top_level_url| must've been created with another
  // token and should NOT be removed.
  EXPECT_EQ(settings->GetCookieSetting(iframe_url, net::SiteForCookies(),
                                       other_top_level_url, {}, nullptr),
            CONTENT_SETTING_ALLOW);
  EXPECT_EQ(settings->GetThirdPartyCookieAllowMechanism(
                iframe_url, net::SiteForCookies::FromUrl(other_top_level_url),
                other_top_level_url, {}, nullptr),
            content_settings::CookieSettingsBase::
                ThirdPartyCookieAllowMechanism::kAllowBy3PCD);

  // Verify |other_embedded_url| still has a |TPCD_TRIAL| grant for third-party
  // cookie access when embedded by |top_level_url|.
  EXPECT_EQ(
      settings->GetCookieSetting(other_embedded_url, net::SiteForCookies(),
                                 top_level_url, {}, nullptr),
      CONTENT_SETTING_ALLOW);
  EXPECT_EQ(settings->GetThirdPartyCookieAllowMechanism(
                other_embedded_url, net::SiteForCookies::FromUrl(top_level_url),
                top_level_url, {}, nullptr),
            content_settings::CookieSettingsBase::
                ThirdPartyCookieAllowMechanism::kAllowBy3PCD);
}

IN_PROC_BROWSER_TEST_F(
    ValidityService3pTrialBrowserTest,
    RemoveAllSettingsCreatedUsingAffectedSubdomainMatchingToken) {
  content::WebContents* web_contents = GetActiveWebContents();
  content_settings::CookieSettings* settings =
      CookieSettingsFactory::GetForProfile(GetProfile()).get();

  NavigateToPageWithIFrame(kTrialEnabledSite.host(), "a.test");
  GURL iframe_url = GetIFrame()->GetLastCommittedURL();
  GURL top_level_url =
      web_contents->GetPrimaryMainFrame()->GetLastCommittedURL();

  // Create |TPCD_TRIAL| settings for |iframe_url| (and subdomains of it) under
  // various top-level sites.
  CreateAndVerifyThirdPartyTrialGrant(iframe_url, top_level_url,
                                      /*match_subdomains=*/true);
  CreateAndVerifyThirdPartyTrialGrant(iframe_url, GURL("https://b.test"),
                                      /*match_subdomains=*/true);
  CreateAndVerifyThirdPartyTrialGrant(iframe_url, GURL("https://c.test"),
                                      /*match_subdomains=*/true);
  // Note: this setting does NOT match subdomains, while the others do, meaning
  // it would've been created using a different origin trial token.
  GURL other_top_level_url("https://other-top-level.test");
  CreateAndVerifyThirdPartyTrialGrant(iframe_url, other_top_level_url,
                                      /*match_subdomains=*/false);

  // Also, create a |TPCD_TRIAL| setting for |other_embedded_url| under
  // |top_level_url|.
  GURL other_embedded_url("https://another-embedded-site.test");
  CreateAndVerifyThirdPartyTrialGrant(other_embedded_url, top_level_url,
                                      /*match_subdomains=*/true);

  // Access cookies via javascript in the iframe, which should cause the setting
  // to be removed.
  ContentSettingChangeObserver setting_observer(
      web_contents->GetBrowserContext(), iframe_url, top_level_url,
      ContentSettingsType::TPCD_TRIAL);
  AccessCookieViaJsIn(web_contents, GetIFrame());
  setting_observer.Wait();

  // Verify |TPCD_TRIAL| content settings with the same primary pattern as the
  // setting that allowed 3PC access in the iframe have been removed.
  EXPECT_EQ(settings->GetCookieSetting(iframe_url, net::SiteForCookies(),
                                       top_level_url, {}, nullptr),
            CONTENT_SETTING_BLOCK);
  EXPECT_EQ(settings->GetCookieSetting(iframe_url, net::SiteForCookies(),
                                       GURL("https://b.test"), {}, nullptr),
            CONTENT_SETTING_BLOCK);
  EXPECT_EQ(settings->GetCookieSetting(iframe_url, net::SiteForCookies(),
                                       GURL("https://c.test"), {}, nullptr),
            CONTENT_SETTING_BLOCK);

  // Note: since the setting allowing |iframe_url| to access 3PC under
  // |other_top_level_url| does NOT match subdomains and the setting
  // that allowed access in the iframe context did, the setting for 3PC access
  // under |other_top_level_url| must've been created with another
  // token and should NOT be removed.
  EXPECT_EQ(settings->GetCookieSetting(iframe_url, net::SiteForCookies(),
                                       other_top_level_url, {}, nullptr),
            CONTENT_SETTING_ALLOW);
  EXPECT_EQ(settings->GetThirdPartyCookieAllowMechanism(
                iframe_url, net::SiteForCookies::FromUrl(other_top_level_url),
                other_top_level_url, {}, nullptr),
            content_settings::CookieSettingsBase::
                ThirdPartyCookieAllowMechanism::kAllowBy3PCD);

  // Verify |other_embedded_url| still has a |TPCD_TRIAL| grant for
  // third-party cookie access when embedded by |top_level_url|.
  EXPECT_EQ(
      settings->GetCookieSetting(other_embedded_url, net::SiteForCookies(),
                                 top_level_url, {}, nullptr),
      CONTENT_SETTING_ALLOW);
  EXPECT_EQ(settings->GetThirdPartyCookieAllowMechanism(
                other_embedded_url, net::SiteForCookies::FromUrl(top_level_url),
                top_level_url, {}, nullptr),
            content_settings::CookieSettingsBase::
                ThirdPartyCookieAllowMechanism::kAllowBy3PCD);
}

IN_PROC_BROWSER_TEST_F(ValidityService3pTrialBrowserTest,
                       PreserveValidSettings) {
  content::WebContents* web_contents = GetActiveWebContents();
  content_settings::CookieSettings* settings =
      CookieSettingsFactory::GetForProfile(GetProfile()).get();
  GURL top_level_url =
      embedded_test_server()->GetURL("a.test", "/iframe_blank.html");
  const std::string kIframeId = "test";  // defined in iframe_blank.html

  // Navigate the top-level page to |top_level_url| and update it to have an
  // |kTrialEnabledSite| iframe that returns the origin trial token in its HTTP
  // response headers.
  {
    ASSERT_TRUE(content::NavigateToURL(web_contents, top_level_url));

    ContentSettingChangeObserver setting_observer(
        web_contents->GetBrowserContext(), kTrialEnabledSite, top_level_url,
        ContentSettingsType::TPCD_TRIAL);

    GURL iframe_url = GURL(kTrialEnabledSite.spec() + kTrialEnabledIframePath);
    ASSERT_TRUE(
        content::NavigateIframeToURL(web_contents, kIframeId, iframe_url));
    setting_observer.Wait();
  }

  // Write a third-party cookie from the |kTrialEnabledSite| iframe.
  AccessCookieViaJsIn(web_contents, GetIFrame());

  // Since we can't deterministically wait for the ValidityService to do nothing
  // in response to a third-party cookie access permitted by a valid
  // |TPCD_TRIAL| content setting, also trigger a cookie access for a different
  // origin with an invalid setting, then after the invalid setting has been
  // removed, check that the |kTrialEnabledSite| content setting still remains.
  {
    GURL iframe_url =
        https_server_->GetURL("different-host.test", "/title1.html");
    ASSERT_TRUE(
        content::NavigateIframeToURL(web_contents, kIframeId, iframe_url));

    // Create a |TPCD_TRIAL| setting for |iframe_url| under |top_level_url|
    // without actually enabling the "Tpcd" trial.
    CreateAndVerifyThirdPartyTrialGrant(iframe_url, top_level_url,
                                        /*match_subdomains=*/false);

    // Access cookies via javascript in the iframe, which should cause the
    // setting to be removed.
    ContentSettingChangeObserver setting_observer(
        web_contents->GetBrowserContext(), iframe_url, top_level_url,
        ContentSettingsType::TPCD_TRIAL);
    AccessCookieViaJsIn(web_contents, GetIFrame());
    setting_observer.Wait();

    // Verify |iframe_url| no longer has access to third-party cookies when
    // embedded by |top_level_url|.
    EXPECT_EQ(settings->GetCookieSetting(iframe_url, net::SiteForCookies(),
                                         top_level_url, {}, nullptr),
              CONTENT_SETTING_BLOCK);
  }

  // Verify |kTrialEnabledSite| still has access to third-party cookies when
  // embedded by |top_level_url|.
  EXPECT_EQ(settings->GetCookieSetting(kTrialEnabledSite, net::SiteForCookies(),
                                       top_level_url, {}, nullptr),
            CONTENT_SETTING_ALLOW);
}

class ValidityService1pTrialBrowserTest
    : public ValidityServiceBrowserTestBase {
  void SetUp() override {
    features_.InitWithFeaturesAndParameters(
        {{net::features::kTopLevelTpcdTrialSettings, {}},
         {content_settings::features::kTrackingProtection3pcd, {}}},
        {});

    PlatformBrowserTest::SetUp();
  }

  bool OnRequest(
      content::URLLoaderInterceptor::RequestParams* params) override {
    std::string host = params->url_request.url.host();
    std::string path = params->url_request.url.path().substr(1);

    if (host != kTrialEnabledDomain || path != kTrialEnabledTopLevelPath) {
      return false;
    }

    URLLoaderInterceptor::WriteResponse(
        base::StrCat({"HTTP/1.1 200 OK\n", "Content-type: text/html\n",
                      "Origin-Trial: ", k1pDeprecationTrialToken, "\n", "\n"}),
        ("<html><head><title>Trial enabled page with iframe</title></head>"
         "<body>"
         "<iframe id='test'></iframe>"
         "</body></html>"),
        params->client.get());
    return true;
  }
};

IN_PROC_BROWSER_TEST_F(ValidityService1pTrialBrowserTest,
                       RemovesInvalidSettingOnJsCookieAccess) {
  content::WebContents* web_contents = GetActiveWebContents();
  content_settings::CookieSettings* settings =
      CookieSettingsFactory::GetForProfile(GetProfile()).get();

  NavigateToPageWithIFrame(kTrialEnabledSite.host(), "a.test");

  // Create a |TOP_LEVEL_TPCD_TRIAL| setting for |top_level_url| without
  // actually enabling the "TopLevelTpcd" trial.
  GURL top_level_url =
      web_contents->GetPrimaryMainFrame()->GetLastCommittedURL();
  GURL iframe_url = GetIFrame()->GetLastCommittedURL();
  CreateAndVerifyFirstPartyTrialGrant(top_level_url,
                                      /*match_subdomains=*/false);

  // Access third-party cookies via javascript in the iframe, which should cause
  // the setting to be removed.
  ContentSettingChangeObserver setting_observer =
      CreateTopLevelTrialSettingsObserver(top_level_url);
  AccessCookieViaJsIn(web_contents, GetIFrame());
  setting_observer.Wait();

  // Verify third-party cookie access is no longer permitted under
  // |top_level_url|.
  EXPECT_EQ(settings->GetCookieSetting(GURL(), net::SiteForCookies(),
                                       top_level_url, {}, nullptr),
            CONTENT_SETTING_BLOCK);
}

IN_PROC_BROWSER_TEST_F(ValidityService1pTrialBrowserTest,
                       RemoveInvalidSettingOnNavigationCookieAccess) {
  content::WebContents* web_contents = GetActiveWebContents();
  content_settings::CookieSettings* settings =
      CookieSettingsFactory::GetForProfile(GetProfile()).get();

  // Navigate to |top_level_url|, which has a blank iframe.
  GURL top_level_url =
      https_server_->GetURL(kTrialEnabledSite.host(), "/iframe_blank.html");
  ASSERT_TRUE(content::NavigateToURL(web_contents, top_level_url));

  // Create a |TOP_LEVEL_TPCD_TRIAL| setting for |top_level_url| without
  // actually enabling the "TopLevelTpcd" trial.
  CreateAndVerifyFirstPartyTrialGrant(top_level_url,
                                      /*match_subdomains=*/false);

  // Navigate the iframe to |iframe_url| to set a cookie via a network response,
  // which should cause the setting to be removed.
  ContentSettingChangeObserver setting_observer =
      CreateTopLevelTrialSettingsObserver(top_level_url);
  GURL iframe_url = https_server_->GetURL(
      "a.test", "/set-cookie?name=value;Secure;SameSite=None");
  ASSERT_TRUE(NavigateIFrameAndWaitForCookieAccess(iframe_url));
  setting_observer.Wait();

  // Verify third-party cookie access is no longer permitted under
  // |top_level_url|.
  EXPECT_EQ(settings->GetCookieSetting(GURL(), net::SiteForCookies(),
                                       top_level_url, {}, nullptr),
            CONTENT_SETTING_BLOCK);
}

IN_PROC_BROWSER_TEST_F(ValidityService1pTrialBrowserTest,
                       RemoveInvalidSubdomainMatchingSettingOnCookieAccess) {
  content::WebContents* web_contents = GetActiveWebContents();
  content_settings::CookieSettings* settings =
      CookieSettingsFactory::GetForProfile(GetProfile()).get();

  NavigateToPageWithIFrame("a.test", kTrialEnabledSiteSubdomain.host());

  // Create a subdomain-matching |TOP_LEVEL_TPCD_TRIAL| setting for |grant_url|
  // (which |top_level_url| is a subdomain of) without actually enabling the
  // "TopLevelTpcd" trial.
  GURL top_level_url =
      web_contents->GetPrimaryMainFrame()->GetLastCommittedURL();
  GURL iframe_url = GetIFrame()->GetLastCommittedURL();
  GURL grant_url = GURL(base::StrCat(
      {"https://", kTrialEnabledSite.host(), ":", top_level_url.port()}));

  CreateAndVerifyFirstPartyTrialGrant(grant_url,
                                      /*match_subdomains=*/true);

  // Access cookies via javascript in the iframe, which should cause the setting
  // to be removed.
  ContentSettingChangeObserver setting_observer =
      CreateTopLevelTrialSettingsObserver(top_level_url);
  AccessCookieViaJsIn(web_contents, GetIFrame());
  setting_observer.Wait();

  // Verify third-party cookie access is no longer permitted under
  // |top_level_url| or |grant_url|.
  EXPECT_EQ(settings->GetCookieSetting(GURL(), net::SiteForCookies(),
                                       top_level_url, {}, nullptr),
            CONTENT_SETTING_BLOCK);
  EXPECT_EQ(settings->GetCookieSetting(GURL(), net::SiteForCookies(), grant_url,
                                       {}, nullptr),
            CONTENT_SETTING_BLOCK);
}

IN_PROC_BROWSER_TEST_F(ValidityService1pTrialBrowserTest,
                       PreserveValidSettings) {
  content::WebContents* web_contents = GetActiveWebContents();
  content_settings::CookieSettings* settings =
      CookieSettingsFactory::GetForProfile(GetProfile()).get();
  GURL top_level_url =
      GURL(kTrialEnabledSite.spec() + kTrialEnabledTopLevelPath);
  GURL iframe_url = https_server_->GetURL("a.test", "/title1.html");
  const std::string kIframeId = "test";

  // Navigate to a |top_level_url| page that returns its origin trial
  // token in its HTTP response headers and has an iframe.
  {
    ContentSettingChangeObserver setting_observer =
        CreateTopLevelTrialSettingsObserver(top_level_url);

    ASSERT_TRUE(content::NavigateToURL(web_contents, top_level_url));
    ASSERT_TRUE(
        content::NavigateIframeToURL(web_contents, kIframeId, iframe_url));

    setting_observer.Wait();

    // Verify third-party cookie access is now permitted under
    // |top_level_url|.
    ASSERT_EQ(settings->GetCookieSetting(GURL(), net::SiteForCookies(),
                                         top_level_url, {}, nullptr),
              CONTENT_SETTING_ALLOW);
    ASSERT_EQ(settings->GetThirdPartyCookieAllowMechanism(
                  GURL(), net::SiteForCookies(), top_level_url, {}, nullptr),
              content_settings::CookieSettingsBase::
                  ThirdPartyCookieAllowMechanism::kAllowByTopLevel3PCD);
  }

  // Write a third-party cookie from the iframe.
  AccessCookieViaJsIn(web_contents, GetIFrame());

  // Since we can't deterministically wait for the ValidityService to do nothing
  // in response to a third-party cookie access permitted by a valid
  // |TOP_LEVEL_TPCD_TRIAL| content setting, navigate to a different top-level
  // site (with an invalid setting) and trigger a third-party cookie access,
  // then after the invalid setting has been removed, check that the
  // |kTrialEnabledSite| content setting still remains.
  {
    GURL other_top_level_url =
        https_server_->GetURL("different-host.test", "/iframe_blank.html");
    ASSERT_TRUE(content::NavigateToURL(web_contents, other_top_level_url));
    ASSERT_TRUE(
        content::NavigateIframeToURL(web_contents, kIframeId, iframe_url));

    // Create a |TOP_LEVEL_TPCD_TRIAL| setting for |other_top_level_url| without
    // actually enabling the "TopLevelTpcd" trial.
    CreateAndVerifyFirstPartyTrialGrant(other_top_level_url,
                                        /*match_subdomains=*/false);

    // Access cookies via javascript in the iframe, which should cause the
    // setting to be removed.
    ContentSettingChangeObserver setting_observer =
        CreateTopLevelTrialSettingsObserver(other_top_level_url);
    AccessCookieViaJsIn(web_contents, GetIFrame());
    setting_observer.Wait();

    // Verify third-party cookie access is no longer permitted under
    // |other_top_level_url|.
    EXPECT_EQ(settings->GetCookieSetting(GURL(), net::SiteForCookies(),
                                         other_top_level_url, {}, nullptr),
              CONTENT_SETTING_BLOCK);
  }

  // Verify third-party cookie access is still permitted under
  // |top_level_url|.
  EXPECT_EQ(settings->GetCookieSetting(GURL(), net::SiteForCookies(),
                                       top_level_url, {}, nullptr),
            CONTENT_SETTING_ALLOW);

  HostContentSettingsMap* settings_map =
      HostContentSettingsMapFactory::GetForProfile(GetProfile());
  EXPECT_EQ(settings_map->GetContentSetting(
                top_level_url, GURL(),
                ContentSettingsType::TOP_LEVEL_TPCD_TRIAL, nullptr),
            CONTENT_SETTING_ALLOW);
}
}  // namespace tpcd::trial