File: tpcd_support_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 (506 lines) | stat: -rw-r--r-- 22,248 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
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#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/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/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 TpcdTrialBrowserTest : 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, 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());
  }

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

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

    if (path.find("tpcd/") == 0) {
      content::URLLoaderInterceptor::WriteResponse(
          base::StrCat(
              {"chrome/test/data/", params->url_request.url.path_piece()}),
          params->client.get());
      return true;
    }

    if (host != kTrialEnabledDomain && host != kTrialEnabledSubdomain) {
      return false;
    }

    std::string headers =
        "HTTP/1.1 200 OK\n"
        "Content-type: text/html\n";

    if (path == kTrialEnabledIframePath) {
      if (host == kTrialEnabledDomain) {
        base::StrAppend(&headers, {"Origin-Trial: ", kTrialToken, "\n"});
      }

      if (host == kTrialEnabledSubdomain) {
        if (query == "etld_plus_1_token") {
          base::StrAppend(
              &headers, {"Origin-Trial: ", kTrialSubdomainMatchingToken, "\n"});
        } else {
          base::StrAppend(
              &headers,
              {"Origin-Trial: ", kSubdomainTrialSubdomainMatchingToken, "\n"});
        }
      }
    }

    content::URLLoaderInterceptor::WriteResponse(headers,
                                                 /*body=*/"",
                                                 params->client.get());
    return true;
  }

  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})};
};

IN_PROC_BROWSER_TEST_F(TpcdTrialBrowserTest,
                       EnabledAfterCrossSiteIframeResponse) {
  content::WebContents* web_contents = GetActiveWebContents();
  GURL embedding_site =
      embedded_test_server()->GetURL("a.test", "/iframe_blank.html");

  // Verify `kTrialEnabledSite` does not have cookie access as a third-party.
  content_settings::CookieSettings* settings =
      CookieSettingsFactory::GetForProfile(GetProfile()).get();
  EXPECT_EQ(settings->GetCookieSetting(kTrialEnabledSite, net::SiteForCookies(),
                                       GURL(), {}, nullptr),
            CONTENT_SETTING_BLOCK);
  EXPECT_EQ(settings->GetCookieSetting(kTrialEnabledSite, net::SiteForCookies(),
                                       embedding_site, {}, nullptr),
            CONTENT_SETTING_BLOCK);

  // Navigate the top-level page to `embedding_site` and update it to have an
  // `kTrialEnabledSite` iframe that returns the origin trial token in it's HTTP
  // response headers.
  ASSERT_TRUE(content::NavigateToURL(web_contents, embedding_site));
  const std::string kIframeId = "test";  // defined in iframe_blank.html
  {
    ContentSettingChangeObserver setting_observer(
        web_contents->GetBrowserContext(), kTrialEnabledSite, embedding_site,
        ContentSettingsType::TPCD_TRIAL);

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

  // Check that `kTrialEnabledSite` now has access to cookies as a third-party
  // when embedded by `embedding_site`.
  EXPECT_EQ(settings->GetCookieSetting(kTrialEnabledSite, net::SiteForCookies(),
                                       embedding_site, {}, nullptr),
            CONTENT_SETTING_ALLOW);

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

  // Check cookie access for `kTrialEnabledSite` with a different path.
  GURL enabled_site_diff_path =
      GURL(kTrialEnabledSite.spec() + "iframe_blank.html");

  EXPECT_EQ(
      settings->GetCookieSetting(enabled_site_diff_path, net::SiteForCookies(),
                                 embedding_site, {}, nullptr),
      CONTENT_SETTING_ALLOW);

  // Verify that a subsequent load of a resource from `kTrialEnabledSite` on the
  // embedding site without the token (`enabled_site_diff_path`) removes the
  // `TPCD_TRIAL` content setting for it it.
  {
    ContentSettingChangeObserver setting_observer(
        web_contents->GetBrowserContext(), enabled_site_diff_path,
        embedding_site, ContentSettingsType::TPCD_TRIAL);
    ASSERT_TRUE(content::NavigateIframeToURL(web_contents, kIframeId,
                                             enabled_site_diff_path));
    setting_observer.Wait();
  }

  // Verify `kTrialEnabledSite` no longer has cookie access.
  EXPECT_EQ(settings->GetCookieSetting(kTrialEnabledSite, net::SiteForCookies(),
                                       embedding_site, {}, nullptr),
            CONTENT_SETTING_BLOCK);
  EXPECT_EQ(
      settings->GetCookieSetting(enabled_site_diff_path, net::SiteForCookies(),
                                 embedding_site, {}, nullptr),
      CONTENT_SETTING_BLOCK);
}

IN_PROC_BROWSER_TEST_F(TpcdTrialBrowserTest,
                       EnabledAfterCrossSiteIframeResponseWithSubdomainToken) {
  content::WebContents* web_contents = GetActiveWebContents();
  GURL embedding_site =
      embedded_test_server()->GetURL("a.test", "/iframe_blank.html");

  // Verify `kTrialEnabledSite` and `kTrialEnabledSiteSubdomain` do not have
  // cookie access as third-parties.
  content_settings::CookieSettings* settings =
      CookieSettingsFactory::GetForProfile(GetProfile()).get();
  EXPECT_EQ(settings->GetCookieSetting(kTrialEnabledSite, net::SiteForCookies(),
                                       GURL(), {}, nullptr),
            CONTENT_SETTING_BLOCK);
  EXPECT_EQ(settings->GetCookieSetting(kTrialEnabledSite, net::SiteForCookies(),
                                       embedding_site, {}, nullptr),
            CONTENT_SETTING_BLOCK);
  EXPECT_EQ(
      settings->GetCookieSetting(kTrialEnabledSiteSubdomain,
                                 net::SiteForCookies(), GURL(), {}, nullptr),
      CONTENT_SETTING_BLOCK);
  EXPECT_EQ(settings->GetCookieSetting(kTrialEnabledSiteSubdomain,
                                       net::SiteForCookies(), embedding_site,
                                       {}, nullptr),
            CONTENT_SETTING_BLOCK);

  // Navigate the top-level page to `embedding_site` and update it to have an
  // `kTrialEnabledSiteSubdomain` iframe that returns the subdomain matching
  // origin trial token for it's eTLD+1 (`kTrialEnabledSite`) in it's HTTP
  // response headers.
  ASSERT_TRUE(content::NavigateToURL(web_contents, embedding_site));
  const std::string kIframeId = "test";  // defined in iframe_blank.html
  {
    ContentSettingChangeObserver setting_observer(
        web_contents->GetBrowserContext(), kTrialEnabledSiteSubdomain,
        embedding_site, ContentSettingsType::TPCD_TRIAL);

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

  // Check that both `kTrialEnabledSiteSubdomain` and it's eTLD+1
  // (`kTrialEnabledSite`) now has access to cookies as a third-party when
  // embedded by `embedding_site`.
  EXPECT_EQ(settings->GetCookieSetting(kTrialEnabledSiteSubdomain,
                                       net::SiteForCookies(), embedding_site,
                                       {}, nullptr),
            CONTENT_SETTING_ALLOW);
  EXPECT_EQ(settings->GetCookieSetting(kTrialEnabledSite, net::SiteForCookies(),
                                       embedding_site, {}, nullptr),
            CONTENT_SETTING_ALLOW);

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

  // Check cookie access for `kTrialEnabledSiteSubdomain` with a different path.
  EXPECT_EQ(settings->GetCookieSetting(
                GURL(kTrialEnabledSiteSubdomain.spec() + "iframe_blank.html"),
                net::SiteForCookies(), embedding_site, {}, nullptr),
            CONTENT_SETTING_ALLOW);

  GURL enabled_site_no_token =
      GURL(kTrialEnabledSite.spec() + "iframe_blank.html");
  // Verify that a subsequent load of a resource from
  // `kTrialEnabledSiteSubdomain`'s eTLD+1 (`kTrialEnabledSite`) on the
  // embedding site without the token (`enabled_site_diff_path`) removes the
  // `TPCD_TRIAL` content setting for it.
  {
    ContentSettingChangeObserver setting_observer(
        web_contents->GetBrowserContext(), enabled_site_no_token,
        embedding_site, ContentSettingsType::TPCD_TRIAL);
    ASSERT_TRUE(content::NavigateIframeToURL(web_contents, kIframeId,
                                             enabled_site_no_token));
    setting_observer.Wait();
  }

  // Verify `kTrialEnabledSiteSubdomain` and it's eTLD+1 (`kTrialEnabledSite`)
  // no longer have cookie access.
  EXPECT_EQ(settings->GetCookieSetting(kTrialEnabledSiteSubdomain,
                                       net::SiteForCookies(), embedding_site,
                                       {}, nullptr),
            CONTENT_SETTING_BLOCK);
  EXPECT_EQ(settings->GetCookieSetting(kTrialEnabledSite, net::SiteForCookies(),
                                       embedding_site, {}, nullptr),
            CONTENT_SETTING_BLOCK);
}

IN_PROC_BROWSER_TEST_F(TpcdTrialBrowserTest, EnabledAfterMetaTagAppend) {
  content::WebContents* web_contents = GetActiveWebContents();
  GURL embedding_site{
      base::StrCat({"https://a.test/", kEmbeddedScriptPagePath})};

  // Navigate to a page with an embedded script (sourced from
  // `kTrialEnabledSite`), that enables the trial for `kTrialEnabledSite` by
  // appending a meta tag containing `kTrialEnabledSite`'s third-party origin
  // trial token to the head of the page.
  {
    ContentSettingChangeObserver setting_observer(
        web_contents->GetBrowserContext(), kTrialEnabledSite, embedding_site,
        ContentSettingsType::TPCD_TRIAL);

    ASSERT_TRUE(content::NavigateToURL(web_contents, embedding_site));
    setting_observer.Wait();
  }

  // Verify that `kTrialEnabledSite` now has access to cookies as a third-party
  // when embedded by `embedding_site`.
  content_settings::CookieSettings* settings =
      CookieSettingsFactory::GetForProfile(GetProfile()).get();
  EXPECT_EQ(settings->GetCookieSetting(kTrialEnabledSite, net::SiteForCookies(),
                                       embedding_site, {}, nullptr),
            CONTENT_SETTING_ALLOW);
  EXPECT_EQ(settings->GetThirdPartyCookieAllowMechanism(
                kTrialEnabledSite, net::SiteForCookies::FromUrl(embedding_site),
                embedding_site, {}, nullptr),
            content_settings::CookieSettingsBase::
                ThirdPartyCookieAllowMechanism::kAllowBy3PCD);
}

IN_PROC_BROWSER_TEST_F(TpcdTrialBrowserTest,
                       EnabledAfterMetaTagAppendWithSubdomainToken) {
  content::WebContents* web_contents = GetActiveWebContents();
  GURL embedding_site{base::StrCat(
      {"https://a.test/", kSubdomainMatchingEmbeddedScriptPagePath})};

  // Navigate to a page with an embedded script (sourced from
  // `kTrialEnabledSite`), that enables the trial for `kTrialEnabledSite` and
  // subdomains of it by appending a meta tag containing `kTrialEnabledSite`'s
  // subdomain-matching third-party origin trial token to the head of the page.
  {
    ContentSettingChangeObserver setting_observer(
        web_contents->GetBrowserContext(), kTrialEnabledSite, embedding_site,
        ContentSettingsType::TPCD_TRIAL);

    ASSERT_TRUE(content::NavigateToURL(web_contents, embedding_site));
    setting_observer.Wait();
  }

  // Verify that `kTrialEnabledSite` and `kTrialEnabledSiteSubdomain` now have
  // access to cookies as a third-party when embedded by `embedding_site`.
  content_settings::CookieSettings* settings =
      CookieSettingsFactory::GetForProfile(GetProfile()).get();
  EXPECT_EQ(settings->GetCookieSetting(kTrialEnabledSite, net::SiteForCookies(),
                                       embedding_site, {}, nullptr),
            CONTENT_SETTING_ALLOW);
  EXPECT_EQ(settings->GetCookieSetting(kTrialEnabledSiteSubdomain,
                                       net::SiteForCookies(), embedding_site,
                                       {}, nullptr),
            CONTENT_SETTING_ALLOW);

  EXPECT_EQ(settings->GetThirdPartyCookieAllowMechanism(
                kTrialEnabledSite, net::SiteForCookies::FromUrl(embedding_site),
                embedding_site, {}, nullptr),
            content_settings::CookieSettingsBase::
                ThirdPartyCookieAllowMechanism::kAllowBy3PCD);
  EXPECT_EQ(settings->GetThirdPartyCookieAllowMechanism(
                kTrialEnabledSiteSubdomain,
                net::SiteForCookies::FromUrl(embedding_site), embedding_site,
                {}, nullptr),
            content_settings::CookieSettingsBase::
                ThirdPartyCookieAllowMechanism::kAllowBy3PCD);
}

// This test verifies that TPCD_TRIAL content settings are scoped to the
// embedded origin, in the case where a non-subdomain-matching origin trial
// token is used to enable the trial.
IN_PROC_BROWSER_TEST_F(TpcdTrialBrowserTest, TrialEnabledForTokenOriginScope) {
  content::WebContents* web_contents = GetActiveWebContents();
  GURL embedding_site =
      embedded_test_server()->GetURL("a.test", "/iframe_blank.html");

  // Navigate the top-level page to `embedding_site` and update it to have an
  // `kTrialEnabledSite` iframe that returns the origin trial token in it's HTTP
  // response headers.
  ASSERT_TRUE(content::NavigateToURL(web_contents, embedding_site));
  const std::string kIframeId = "test";  // defined in iframe_blank.html
  {
    ContentSettingChangeObserver setting_observer(
        web_contents->GetBrowserContext(), kTrialEnabledSite, embedding_site,
        ContentSettingsType::TPCD_TRIAL);

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

  // Verify the format of the `TPCD_TRIAL` content setting.
  HostContentSettingsMap* settings_map =
      HostContentSettingsMapFactory::GetForProfile(
          GetActiveWebContents()->GetBrowserContext());

  content_settings::SettingInfo setting_info;
  ASSERT_EQ(settings_map->GetContentSetting(kTrialEnabledSite, embedding_site,
                                            ContentSettingsType::TPCD_TRIAL,
                                            &setting_info),
            CONTENT_SETTING_ALLOW);

  // `setting_info.primary_pattern` should only match
  // `kTrialEnabledSite`.
  EXPECT_EQ(setting_info.primary_pattern,
            ContentSettingsPattern::FromURLNoWildcard(kTrialEnabledSite));
  EXPECT_TRUE(setting_info.primary_pattern.Matches(kTrialEnabledSite));
  EXPECT_FALSE(
      setting_info.primary_pattern.Matches(kTrialEnabledSiteSubdomain));

  // `setting_info.secondary_pattern` should match origins that are
  // same-site with `embedding_site`.
  EXPECT_EQ(
      setting_info.secondary_pattern,
      ContentSettingsPattern::FromURLToSchemefulSitePattern(embedding_site));
}

// This test verifies that TPCD_TRIAL content settings are scoped to
// subdomains of the token origin, when created as a result of a subdomain
// matching origin trial token being used.
IN_PROC_BROWSER_TEST_F(
    TpcdTrialBrowserTest,
    SubdomainMatchingTokenEnablesTrialOnlyForSubdomainsOfTokenOrigin) {
  content::WebContents* web_contents = GetActiveWebContents();
  GURL embedding_site =
      embedded_test_server()->GetURL("a.test", "/iframe_blank.html");

  // Navigate the top-level page to `embedding_site` and update it to have an
  // `kTrialEnabledSiteSubdomain` iframe that returns
  // `kTrialEnabledSiteSubdomain`'s subdomain matching origin trial token in
  // it's HTTP response headers.
  ASSERT_TRUE(content::NavigateToURL(web_contents, embedding_site));
  const std::string kIframeId = "test";  // defined in iframe_blank.html
  {
    ContentSettingChangeObserver setting_observer(
        web_contents->GetBrowserContext(), kTrialEnabledSiteSubdomain,
        embedding_site, ContentSettingsType::TPCD_TRIAL);

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

  // Verify the format of the `TPCD_TRIAL` content setting.
  HostContentSettingsMap* settings_map =
      HostContentSettingsMapFactory::GetForProfile(
          GetActiveWebContents()->GetBrowserContext());

  content_settings::SettingInfo setting_info;
  ASSERT_EQ(settings_map->GetContentSetting(
                kTrialEnabledSiteSubdomain, embedding_site,
                ContentSettingsType::TPCD_TRIAL, &setting_info),
            CONTENT_SETTING_ALLOW);

  // `setting_info.primary_pattern` should only match
  // `kTrialEnabledSiteSubdomain` (https://sub.example.test) and subdomains of
  // it.
  EXPECT_EQ(setting_info.primary_pattern,
            ContentSettingsPattern::FromURL(kTrialEnabledSiteSubdomain));
  EXPECT_TRUE(setting_info.primary_pattern.Matches(
      GURL("https://foo.sub.example.test")));
  EXPECT_FALSE(
      setting_info.primary_pattern.Matches(GURL("https://www.example.test")));
  EXPECT_FALSE(setting_info.primary_pattern.Matches(kTrialEnabledSite));

  // `setting_info.secondary_pattern` should match origins that are
  // same-site with `embedding_site`.
  EXPECT_EQ(
      setting_info.secondary_pattern,
      ContentSettingsPattern::FromURLToSchemefulSitePattern(embedding_site));
}

}  // namespace tpcd::trial