File: masked_domain_list_manager_unittest.cc

package info (click to toggle)
chromium 139.0.7258.127-2
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 6,122,156 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 (735 lines) | stat: -rw-r--r-- 29,895 bytes parent folder | download | duplicates (3)
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
// 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 "components/ip_protection/common/masked_domain_list_manager.h"

#include <map>
#include <string>
#include <string_view>
#include <tuple>
#include <vector>

#include "base/containers/contains.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/strings/strcat.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/scoped_feature_list.h"
#include "base/unguessable_token.h"
#include "components/ip_protection/common/ip_protection_data_types.h"
#include "components/ip_protection/common/masked_domain_list.h"
#include "components/privacy_sandbox/masked_domain_list/masked_domain_list.pb.h"
#include "net/base/features.h"
#include "net/base/network_anonymization_key.h"
#include "net/base/schemeful_site.h"
#include "services/network/public/cpp/features.h"
#include "services/network/public/mojom/proxy_config.mojom-shared.h"
#include "testing/gmock/include/gmock/gmock-matchers.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"
#include "url/url_util.h"

namespace ip_protection {
namespace {
using ::masked_domain_list::Resource;
using ::masked_domain_list::ResourceOwner;
using ::testing::Eq;

enum class MdlImpl {
  kUrlMatcherImpl,
  kFlatbufferImpl,
};

struct MatchTest {
  std::string name;
  std::string req;
  std::string top;
  bool matches;
  bool matches_with_bypass;
};

const std::vector<MatchTest> kMatchTests = {
    MatchTest{
        "ExcludedFromResource",
        "experiment.com",
        "top.com",
        false,
        false,
    },
    MatchTest{
        "ResourceMatch",
        "example.com",
        "top.com",
        true,
        true,
    },
    // Public suffix list testcases.
    // Assumes that "googleapis.com" is added as a resource owner that owns
    // itself only.
    //
    // The `googleapis.com` domain is in the owned_resources of an MDL entry,
    // but the top level site is not owned by it so this is a 3rd party request
    // and should be proxied.
    MatchTest{
        "PslAddedAsResource_OnOwnedResources_TopToDifferentDomain",
        "googleapis.com",
        "top.com",
        true,
        true,
    },
    // The `googleapis.com` domain is in the owned_resources of an MDL entry.
    // This is a request to a subdomain of an owned_resources (known tracker)
    // The top level site is not owned by the same owner so this is a 3rd
    // party request and should be proxied.
    MatchTest{
        "PslAddedAsResource_OnOwnedResources_TopToDifferentDomainSubDomain",
        "sub.googleapis.com",
        "top.com",
        true,
        true,
    },
    // Request from one site that is added as a PSL entry to another site that
    // is added as a PSL entry.
    // `co.jp` is added as a PSL.
    // No MDL entry claims ownership of `co.jp`
    // An MDL entry claims ownership of `sub.co.jp`
    // Should be proxied because `co.jp` is listed as a resource that owns
    // itself and the top_frame_site has the same suffix but is not same-site.
    MatchTest{
        "PslAddedAsResource_MatchingOwnedResources_TopToSubOnSameDomain"
        "OwnerClaimsSubdomain",
        "sub.co.jp",
        "other.co.jp",
        true,
        true,
    },
    // Request from an owned property to an owned resource.
    // The owned resource is a subdomain of a PSL entry resource.
    // Should be proxied but not if bypass is allowed, because while
    // `co.jp` is listed as a PSL resource, the subdomain is
    // privately owned and an MDL entry claims ownership of it and this is a
    // request between an owned property to an owned resource of the same
    // owner.
    MatchTest{
        "PslAddedAsResource_MatchingOwnedResources_SubdomainNotOnPsl",
        "sub.co.jp",
        "owned_property.com",
        true,
        false,
    },
    // Request from one PSL entry site to another PSL entry site.
    // `co.jp` is listed as a resource that owns itself.
    // No MDL entry claims ownership of `co.jp`
    // No MDL entry claims ownership of `site.co.jp`
    // Bypasses the proxy for same-site check of request and top_frame_site.
    MatchTest{
        "PslAddedAsResource_SameSiteRequest"
        "OwnerClaimsSubdomain",
        "same.site.co.jp",
        "site.co.jp",
        true,
        false,
    },
    // Ensure fully-qualified domain names (FQDNs) match correctly.
    MatchTest{
        "ResourceMatch_FQDN",
        "example.com.",
        "top.com.",
        true,
        true,
    },
    MatchTest{
        "ResourceMatch_FQDN_Req",
        "example.com.",
        "top.com",
        true,
        true,
    },
    MatchTest{
        "ResourceMatch_FQDN_Top",
        "example.com",
        "top.com.",
        true,
        true,
    },
};

constexpr std::string_view kTestDomain = "example.com";

const char kFirstUpdateTimeHistogram[] =
    "NetworkService.MaskedDomainList.FirstUpdateTime";

}  // namespace

class MaskedDomainListManagerBaseTest : public testing::Test {
 protected:
  virtual bool UseFlatbuffer() = 0;

  // Call either `UpdateMaskedDomainList` or `UpdateMaskedDomainListFlatbuffer`,
  // depending on  `UseFlatbuffer`.
  void UpdateMaskedDomainList(masked_domain_list::MaskedDomainList& mdl,
                              MaskedDomainListManager& mdl_manager) {
    if (UseFlatbuffer()) {
      base::FilePath default_mdl_file_path;
      base::CreateTemporaryFile(&default_mdl_file_path);
      base::FilePath regular_browsing_mdl_file_path;
      base::CreateTemporaryFile(&regular_browsing_mdl_file_path);
      EXPECT_TRUE(ip_protection::MaskedDomainList::BuildFromProto(
          mdl, default_mdl_file_path, regular_browsing_mdl_file_path));

      base::File default_mdl_file(default_mdl_file_path,
                                  base::File::Flags::FLAG_OPEN |
                                      base::File::Flags::FLAG_READ |
                                      base::File::Flags::FLAG_DELETE_ON_CLOSE);
      base::File regular_browsing_mdl_file(
          regular_browsing_mdl_file_path,
          base::File::Flags::FLAG_OPEN | base::File::Flags::FLAG_READ |
              base::File::Flags::FLAG_DELETE_ON_CLOSE);
      mdl_manager.UpdateMaskedDomainListFlatbuffer(
          default_mdl_file.Duplicate(), default_mdl_file.GetLength(),
          regular_browsing_mdl_file.Duplicate(),
          regular_browsing_mdl_file.GetLength());
    } else {
      mdl_manager.UpdateMaskedDomainList(
          mdl, /*exclusion_list=*/std::vector<std::string>());
    }
  }
};

class MaskedDomainListManagerTest
    : public MaskedDomainListManagerBaseTest,
      public testing::WithParamInterface<MdlImpl> {
 public:
  MaskedDomainListManagerTest()
      : allow_list_no_bypass_(
            network::mojom::IpProtectionProxyBypassPolicy::kNone),
        allow_list_first_party_bypass_(
            network::mojom::IpProtectionProxyBypassPolicy::
                kFirstPartyToTopLevelFrame) {}

  void SetUp() override {
    scoped_feature_list_.InitWithFeatureState(
        network::features::kMaskedDomainListFlatbufferImpl,
        GetParam() == MdlImpl::kFlatbufferImpl);

    masked_domain_list::MaskedDomainList mdl;
    auto* resource_owner = mdl.add_resource_owners();
    resource_owner->set_owner_name("foo");
    resource_owner->add_owned_properties("property.com");
    resource_owner->add_owned_resources()->set_domain(std::string(kTestDomain));
    UpdateMaskedDomainList(mdl, allow_list_no_bypass_);
    UpdateMaskedDomainList(mdl, allow_list_first_party_bypass_);
  }

  bool UseFlatbuffer() override {
    return GetParam() == MdlImpl::kFlatbufferImpl;
  }

 protected:
  base::test::ScopedFeatureList scoped_feature_list_;
  MaskedDomainListManager allow_list_no_bypass_;
  MaskedDomainListManager allow_list_first_party_bypass_;
};

INSTANTIATE_TEST_SUITE_P(All,
                         MaskedDomainListManagerTest,
                         testing::Values(MdlImpl::kFlatbufferImpl,
                                         MdlImpl::kUrlMatcherImpl),
                         [](const testing::TestParamInfo<MdlImpl>& info) {
                           return info.param == MdlImpl::kUrlMatcherImpl
                                      ? "UrlMatcher"
                                      : "Flatbuffer";
                         });

TEST_P(MaskedDomainListManagerTest, IsNotEnabledByDefault) {
  base::test::ScopedFeatureList scoped_feature_list;
  scoped_feature_list.InitAndDisableFeature(
      network::features::kMaskedDomainList);

  MaskedDomainListManager allow_list_no_bypass(
      network::mojom::IpProtectionProxyBypassPolicy::kNone);
  MaskedDomainListManager allow_list_first_party_bypass(
      network::mojom::IpProtectionProxyBypassPolicy::
          kFirstPartyToTopLevelFrame);

  EXPECT_FALSE(allow_list_no_bypass.IsEnabled());
  EXPECT_FALSE(allow_list_first_party_bypass.IsEnabled());
}

TEST_P(MaskedDomainListManagerTest, IsEnabledWhenManuallySet) {
  base::test::ScopedFeatureList scoped_feature_list;
  scoped_feature_list.InitWithFeatures({net::features::kEnableIpProtectionProxy,
                                        network::features::kMaskedDomainList},
                                       {});

  MaskedDomainListManager allow_list(
      network::mojom::IpProtectionProxyBypassPolicy::kNone);

  EXPECT_TRUE(allow_list.IsEnabled());
}

TEST_P(MaskedDomainListManagerTest, AllowListIsNotPopulatedByDefault) {
  MaskedDomainListManager allow_list(
      network::mojom::IpProtectionProxyBypassPolicy::kNone);
  EXPECT_FALSE(allow_list.IsPopulated());
}

TEST_P(MaskedDomainListManagerTest, AllowlistIsPopulated_MdlHasResourceOwners) {
  MaskedDomainListManager mdl_manager(
      network::mojom::IpProtectionProxyBypassPolicy::kNone);
  masked_domain_list::MaskedDomainList mdl;
  auto* resource_owner = mdl.add_resource_owners();
  resource_owner->set_owner_name("foo");
  resource_owner->add_owned_resources()->set_domain("example.com");
  UpdateMaskedDomainList(mdl, mdl_manager);

  EXPECT_TRUE(mdl_manager.IsPopulated());
}

TEST_P(MaskedDomainListManagerTest, AllowlistAcceptsMultipleUpdates) {
  base::HistogramTester histogram_tester;
  MaskedDomainListManager mdl_manager(
      network::mojom::IpProtectionProxyBypassPolicy::kNone);
  masked_domain_list::MaskedDomainList mdl1;
  {
    auto* resource_owner = mdl1.add_resource_owners();
    resource_owner->set_owner_name("foo");
    resource_owner->add_owned_resources()->set_domain("example.com");
  }
  masked_domain_list::MaskedDomainList mdl2;
  {
    auto* resource_owner = mdl2.add_resource_owners();
    resource_owner->set_owner_name("foo");
    resource_owner->add_owned_resources()->set_domain("example.net");
  }
  const auto kHttpsRequestUrl1 = GURL("https://example.com");
  const auto kHttpsRequestUrl2 = GURL("https://example.net");
  const auto kEmptyNak = net::NetworkAnonymizationKey();

  // No updates yet.
  histogram_tester.ExpectTotalCount(kFirstUpdateTimeHistogram, 0);

  // First update.
  UpdateMaskedDomainList(mdl1, mdl_manager);

  EXPECT_TRUE(mdl_manager.IsPopulated());
  EXPECT_TRUE(
      mdl_manager.Matches(kHttpsRequestUrl1, kEmptyNak, MdlType::kIncognito));
  EXPECT_FALSE(
      mdl_manager.Matches(kHttpsRequestUrl2, kEmptyNak, MdlType::kIncognito));
  histogram_tester.ExpectTotalCount(kFirstUpdateTimeHistogram, 1);

  // Second update. Removes old rules, adds new ones.
  UpdateMaskedDomainList(mdl2, mdl_manager);

  EXPECT_TRUE(mdl_manager.IsPopulated());
  EXPECT_TRUE(
      mdl_manager.Matches(kHttpsRequestUrl2, kEmptyNak, MdlType::kIncognito));
  EXPECT_FALSE(
      mdl_manager.Matches(kHttpsRequestUrl1, kEmptyNak, MdlType::kIncognito));
  histogram_tester.ExpectTotalCount(kFirstUpdateTimeHistogram, 1);
}

TEST_P(MaskedDomainListManagerTest, ShouldMatchHttp) {
  const auto kHttpRequestUrl = GURL(base::StrCat({"http://", kTestDomain}));
  const auto kHttpCrossSiteNak = net::NetworkAnonymizationKey::CreateCrossSite(
      net::SchemefulSite(GURL("http://top.com")));

  EXPECT_TRUE(allow_list_no_bypass_.Matches(kHttpRequestUrl, kHttpCrossSiteNak,
                                            MdlType::kIncognito));
  EXPECT_TRUE(allow_list_first_party_bypass_.Matches(
      kHttpRequestUrl, kHttpCrossSiteNak, MdlType::kIncognito));
}

TEST_P(MaskedDomainListManagerTest, ShouldMatchWss) {
  const auto kWssRequestUrl = GURL(base::StrCat({"wss://", kTestDomain}));
  const auto kHttpCrossSiteNak = net::NetworkAnonymizationKey::CreateCrossSite(
      net::SchemefulSite(GURL("http://top.com")));

  EXPECT_TRUE(allow_list_no_bypass_.Matches(kWssRequestUrl, kHttpCrossSiteNak,
                                            MdlType::kIncognito));
  EXPECT_TRUE(allow_list_first_party_bypass_.Matches(
      kWssRequestUrl, kHttpCrossSiteNak, MdlType::kIncognito));
}

TEST_P(MaskedDomainListManagerTest, ShouldMatchWs) {
  const auto kWsRequestUrl = GURL(base::StrCat({"ws://", kTestDomain}));
  const auto kHttpCrossSiteNak = net::NetworkAnonymizationKey::CreateCrossSite(
      net::SchemefulSite(GURL("http://top.com")));

  EXPECT_TRUE(allow_list_no_bypass_.Matches(kWsRequestUrl, kHttpCrossSiteNak,
                                            MdlType::kIncognito));
  EXPECT_TRUE(allow_list_first_party_bypass_.Matches(
      kWsRequestUrl, kHttpCrossSiteNak, MdlType::kIncognito));
}

TEST_P(MaskedDomainListManagerTest, ShouldMatchThirdPartyToTopLevelFrame) {
  const auto kHttpsCrossSiteNak = net::NetworkAnonymizationKey::CreateCrossSite(
      net::SchemefulSite(GURL("https://top.com")));
  const auto kHttpsSameSiteNak = net::NetworkAnonymizationKey::CreateSameSite(
      net::SchemefulSite(GURL("https://top.com")));
  const auto kHttpsThirdPartyRequestUrl =
      GURL(base::StrCat({"https://", kTestDomain}));

  // Regardless of whether the NAK is cross-site, the request URL should be
  // considered third-party because the request URL doesn't match the top-level
  // site.
  EXPECT_TRUE(allow_list_no_bypass_.Matches(
      kHttpsThirdPartyRequestUrl, kHttpsCrossSiteNak, MdlType::kIncognito));
  EXPECT_TRUE(allow_list_first_party_bypass_.Matches(
      kHttpsThirdPartyRequestUrl, kHttpsCrossSiteNak, MdlType::kIncognito));

  EXPECT_TRUE(allow_list_no_bypass_.Matches(
      kHttpsThirdPartyRequestUrl, kHttpsSameSiteNak, MdlType::kIncognito));
  EXPECT_TRUE(allow_list_first_party_bypass_.Matches(
      kHttpsThirdPartyRequestUrl, kHttpsSameSiteNak, MdlType::kIncognito));
}

TEST_P(MaskedDomainListManagerTest,
       MatchFirstPartyToTopLevelFrameDependsOnBypass) {
  const auto kHttpsFirstPartyRequestUrl =
      GURL(base::StrCat({"https://", kTestDomain}));
  const auto kHttpsSameSiteNak = net::NetworkAnonymizationKey::CreateSameSite(
      net::SchemefulSite(kHttpsFirstPartyRequestUrl));
  const auto kHttpsCrossSiteNak = net::NetworkAnonymizationKey::CreateCrossSite(
      net::SchemefulSite(kHttpsFirstPartyRequestUrl));

  EXPECT_TRUE(allow_list_no_bypass_.Matches(
      kHttpsFirstPartyRequestUrl, kHttpsSameSiteNak, MdlType::kIncognito));
  EXPECT_FALSE(allow_list_first_party_bypass_.Matches(
      kHttpsFirstPartyRequestUrl, kHttpsSameSiteNak, MdlType::kIncognito));
  EXPECT_TRUE(allow_list_no_bypass_.Matches(
      kHttpsFirstPartyRequestUrl, kHttpsCrossSiteNak, MdlType::kIncognito));
  EXPECT_FALSE(allow_list_first_party_bypass_.Matches(
      kHttpsFirstPartyRequestUrl, kHttpsCrossSiteNak, MdlType::kIncognito));
}

TEST_P(MaskedDomainListManagerTest,
       MatchFirstPartyToTopLevelFrameIfEmptyNakDependsOnBypass) {
  const auto kHttpsRequestUrl = GURL(base::StrCat({"https://", kTestDomain}));
  const auto kEmptyNak = net::NetworkAnonymizationKey();

  EXPECT_TRUE(allow_list_no_bypass_.Matches(kHttpsRequestUrl, kEmptyNak,
                                            MdlType::kIncognito));
  EXPECT_FALSE(allow_list_first_party_bypass_.Matches(
      kHttpsRequestUrl, kEmptyNak, MdlType::kIncognito));
}

TEST_P(MaskedDomainListManagerTest,
       ShouldNotMatchWithFencedFrameNakIfUrlDoesNotMatch) {
  const auto kHttpsOtherRequestUrl = GURL("https://other.com");
  const auto kNakWithNonce = net::NetworkAnonymizationKey::CreateFromParts(
      net::SchemefulSite(), /*is_cross_site=*/true,
      base::UnguessableToken::Create());

  EXPECT_FALSE(allow_list_no_bypass_.Matches(
      kHttpsOtherRequestUrl, kNakWithNonce, MdlType::kIncognito));
  EXPECT_FALSE(allow_list_first_party_bypass_.Matches(
      kHttpsOtherRequestUrl, kNakWithNonce, MdlType::kIncognito));
}

TEST_P(MaskedDomainListManagerTest, ShouldMatchWithFencedFrameNakIfUrlMatches) {
  const auto kHttpsRequestUrl = GURL(base::StrCat({"https://", kTestDomain}));
  const auto kNakWithNonce = net::NetworkAnonymizationKey::CreateFromParts(
      net::SchemefulSite(), /*is_cross_site=*/true,
      base::UnguessableToken::Create());

  EXPECT_TRUE(allow_list_no_bypass_.Matches(kHttpsRequestUrl, kNakWithNonce,
                                            MdlType::kIncognito));
  EXPECT_TRUE(allow_list_first_party_bypass_.Matches(
      kHttpsRequestUrl, kNakWithNonce, MdlType::kIncognito));
}

TEST_P(MaskedDomainListManagerTest, CustomSchemeTopLevelSite) {
  url::ScopedSchemeRegistryForTests scoped_registry;
  url::ClearSchemesForTests();
  url::AddStandardScheme("chrome-extension", url::SCHEME_WITH_HOST);

  const auto kHttpsRequestUrl = GURL(base::StrCat({"https://", kTestDomain}));
  const auto kExtensionUrlNak =
      net::NetworkAnonymizationKey::CreateCrossSite(net::SchemefulSite(
          GURL("chrome-extension://abcdefghijklmnopqrstuvwxyzabcdef/")));
  ASSERT_FALSE(kExtensionUrlNak.IsTransient());

  EXPECT_TRUE(allow_list_no_bypass_.Matches(kHttpsRequestUrl, kExtensionUrlNak,
                                            MdlType::kIncognito));

  {
    base::test::ScopedFeatureList scoped_feature_list;
    scoped_feature_list.InitAndEnableFeatureWithParameters(
        net::features::kEnableIpProtectionProxy,
        {{net::features::kIpPrivacyRestrictTopLevelSiteSchemes.name, "true"}});
    EXPECT_FALSE(allow_list_first_party_bypass_.Matches(
        kHttpsRequestUrl, kExtensionUrlNak, MdlType::kIncognito));
  }

  {
    base::test::ScopedFeatureList scoped_feature_list;
    scoped_feature_list.InitAndEnableFeatureWithParameters(
        net::features::kEnableIpProtectionProxy,
        {{net::features::kIpPrivacyRestrictTopLevelSiteSchemes.name, "false"}});
    EXPECT_TRUE(allow_list_first_party_bypass_.Matches(
        kHttpsRequestUrl, kExtensionUrlNak, MdlType::kIncognito));
  }
}

// Test whether third-party requests from pages with a data: URL top-level site
// (where the corresponding NAK is transient) should be proxied.
TEST_P(MaskedDomainListManagerTest, DataUrlTopLevelSite) {
  const auto kHttpsRequestUrl = GURL(base::StrCat({"https://", kTestDomain}));
  const auto kDataUrlNak = net::NetworkAnonymizationKey::CreateCrossSite(
      net::SchemefulSite(GURL("data:text/html,<html></html>")));
  ASSERT_TRUE(kDataUrlNak.IsTransient());

  EXPECT_TRUE(allow_list_no_bypass_.Matches(kHttpsRequestUrl, kDataUrlNak,
                                            MdlType::kIncognito));

  {
    base::test::ScopedFeatureList scoped_feature_list;
    scoped_feature_list.InitAndEnableFeatureWithParameters(
        net::features::kEnableIpProtectionProxy,
        {{net::features::kIpPrivacyRestrictTopLevelSiteSchemes.name, "true"}});
    EXPECT_FALSE(allow_list_first_party_bypass_.Matches(
        kHttpsRequestUrl, kDataUrlNak, MdlType::kIncognito));
  }

  {
    base::test::ScopedFeatureList scoped_feature_list;
    scoped_feature_list.InitAndEnableFeatureWithParameters(
        net::features::kEnableIpProtectionProxy,
        {{net::features::kIpPrivacyRestrictTopLevelSiteSchemes.name, "false"}});
    EXPECT_TRUE(allow_list_first_party_bypass_.Matches(
        kHttpsRequestUrl, kDataUrlNak, MdlType::kIncognito));
  }
}

TEST_P(MaskedDomainListManagerTest, AllowListWithoutBypassUsesLessMemory) {
  if (GetParam() == MdlImpl::kFlatbufferImpl) {
    GTEST_SKIP() << "Flatbuffer impl does not measure memory usage";
  }
  EXPECT_GT(allow_list_first_party_bypass_.EstimateMemoryUsage(),
            allow_list_no_bypass_.EstimateMemoryUsage());
}

TEST_P(MaskedDomainListManagerTest, Matches_MdlType_MatchesCorrectly) {
  MaskedDomainListManager mdl_manager(
      network::mojom::IpProtectionProxyBypassPolicy::kNone);

  masked_domain_list::MaskedDomainList mdl;

  ResourceOwner* resource_owner = mdl.add_resource_owners();
  resource_owner->set_owner_name("example");
  Resource* resource = resource_owner->add_owned_resources();
  std::string default_mdl_domain = "default-mdl.com";
  resource->set_domain(default_mdl_domain);
  resource = resource_owner->add_owned_resources();
  std::string regular_mdl_domain = "regular-mdl.com";
  resource->set_domain(regular_mdl_domain);
  resource->add_experiments(
      Resource::Experiment::Resource_Experiment_EXPERIMENT_EXTERNAL_REGULAR);
  resource->set_exclude_default_group(true);

  UpdateMaskedDomainList(mdl, mdl_manager);

  // The default MDL resource should ONLY match for mdl type kIncognito.
  EXPECT_TRUE(
      mdl_manager.Matches(GURL(base::StrCat({"https://", default_mdl_domain})),
                          net::NetworkAnonymizationKey(), MdlType::kIncognito));
  EXPECT_FALSE(mdl_manager.Matches(
      GURL(base::StrCat({"https://", default_mdl_domain})),
      net::NetworkAnonymizationKey(), MdlType::kRegularBrowsing));

  // The regular MDL resource should ONLY match for mdl type kRegularBrowsing.
  EXPECT_FALSE(
      mdl_manager.Matches(GURL(base::StrCat({"https://", regular_mdl_domain})),
                          net::NetworkAnonymizationKey(), MdlType::kIncognito));
  EXPECT_TRUE(mdl_manager.Matches(
      GURL(base::StrCat({"https://", regular_mdl_domain})),
      net::NetworkAnonymizationKey(), MdlType::kRegularBrowsing));
}

class MaskedDomainListManagerMatchTest
    : public MaskedDomainListManagerBaseTest,
      public testing::WithParamInterface<std::tuple<MatchTest, MdlImpl>> {
  bool UseFlatbuffer() override {
    return std::get<1>(GetParam()) == MdlImpl::kFlatbufferImpl;
  }
};

TEST_P(MaskedDomainListManagerMatchTest, Match) {
  auto& [match_test, mdl_impl] = GetParam();

  base::test::ScopedFeatureList scoped_feature_list;
  std::vector<base::test::FeatureRef> enabled_features = {
      network::features::kMaskedDomainList};
  std::vector<base::test::FeatureRef> disabled_features;
  (mdl_impl == MdlImpl::kFlatbufferImpl ? enabled_features : disabled_features)
      .push_back(network::features::kMaskedDomainListFlatbufferImpl);
  scoped_feature_list.InitWithFeatures(enabled_features, disabled_features);

  MaskedDomainListManager allow_list_no_bypass(
      network::mojom::IpProtectionProxyBypassPolicy::kNone);
  MaskedDomainListManager allow_list_first_party_bypass(
      network::mojom::IpProtectionProxyBypassPolicy::
          kFirstPartyToTopLevelFrame);

  masked_domain_list::MaskedDomainList mdl;

  ResourceOwner* resource_owner = mdl.add_resource_owners();
  resource_owner->set_owner_name("example");
  Resource* resource = resource_owner->add_owned_resources();
  resource->set_domain("example.com");

  // Public Suffix List domains are added to the MDL as ResourceOwners.
  resource_owner = mdl.add_resource_owners();
  resource_owner->set_owner_name("googleapis.com");
  resource = resource_owner->add_owned_resources();
  resource->set_domain("googleapis.com");
  resource_owner = mdl.add_resource_owners();
  resource_owner->set_owner_name("co.jp");
  resource = resource_owner->add_owned_resources();
  resource->set_domain("co.jp");

  // Additional ResourceOwner - Includes resources which are on the public
  // suffix list.
  resource_owner = mdl.add_resource_owners();
  resource_owner->set_owner_name("public_suffix");
  resource_owner->add_owned_properties("owned_property.com");
  // Claim a subdomain on the PSL.
  resource = resource_owner->add_owned_resources();
  resource->set_domain("sub.co.jp");

  UpdateMaskedDomainList(mdl, allow_list_no_bypass);
  UpdateMaskedDomainList(mdl, allow_list_first_party_bypass);

  GURL request_url(base::StrCat({"https://", match_test.req}));
  auto network_anonymization_key =
      net::NetworkAnonymizationKey::CreateCrossSite(
          net::SchemefulSite(GURL(base::StrCat({"https://", match_test.top}))));

  EXPECT_EQ(match_test.matches,
            allow_list_no_bypass.Matches(request_url, network_anonymization_key,
                                         MdlType::kIncognito));
  EXPECT_EQ(match_test.matches_with_bypass,
            allow_list_first_party_bypass.Matches(
                request_url, network_anonymization_key, MdlType::kIncognito));
}

INSTANTIATE_TEST_SUITE_P(
    All,
    MaskedDomainListManagerMatchTest,
    testing::Combine(testing::ValuesIn(kMatchTests),
                     testing::Values(MdlImpl::kFlatbufferImpl,
                                     MdlImpl::kUrlMatcherImpl)),
    [](const testing::TestParamInfo<std::tuple<MatchTest, MdlImpl>>& info) {
      return base::StrCat({std::get<0>(info.param).name, "_",
                           std::get<1>(info.param) == MdlImpl::kUrlMatcherImpl
                               ? "UrlMatcher"
                               : "Flatbuffer"});
    });

TEST_P(MaskedDomainListManagerTest, ExclusionSetDomainsRemovedFromMDL) {
  if (GetParam() == MdlImpl::kFlatbufferImpl) {
    GTEST_SKIP() << "Flatbuffer impl does not support exclusion sets";
  }
  auto mdl_manager = MaskedDomainListManager(
      network::mojom::IpProtectionProxyBypassPolicy::kExclusionList);

  // Determine domains that should be excluded from the MDL.
  std::string example_com = "example.com";
  std::string excluded_tld = "excluded-tld";
  std::string irrelevant_tld = "irrelevant-tld";  // Not in the MDL.
  std::vector<std::string> exclusion_list = {example_com, excluded_tld,
                                             irrelevant_tld};

  // The following map contains domains as keys and any subdomains as
  // values. This will be used to create the MDL being tested. Any domains
  // excluded from the MDL will be tracked in the exclusion list. Excluded
  // domains and their subdomains should not be matched on.
  std::map<std::string, std::vector<std::string>> tld_map_with_domains = {
      {example_com,
       {
           "subdomain.example.com",
           "sub.subdomain.example.com",
       }},
      {"example.net",
       {
           "subdomain.example.net",
           "example.com.example.net",
       }},
      {"included-tld",
       {
           "subdomain.included-tld",
       }},
      {excluded_tld,
       {
           "subdomain.excluded-tld",
       }},
      {"unrelated-example.com", {}},  // No subdomains for this domain.
      {"com", {}}};

  // Create an MDL with domains above:
  masked_domain_list::MaskedDomainList mdl;
  for (auto const& [domain, subdomains] : tld_map_with_domains) {
    // Create a ResourceOwner for the domain. The owner name here is abritrary,
    // but needs to be set. The more important thing is the owned resources must
    // include the domain itself as well as any subdomains.
    ResourceOwner* resource_owner = mdl.add_resource_owners();
    resource_owner->set_owner_name(domain);
    resource_owner->add_owned_resources()->set_domain(domain);
    for (auto const& subdomain : subdomains) {
      resource_owner->add_owned_resources()->set_domain(subdomain);
    }
  }

  // First update the MDL and provide an empty exclusion list.
  mdl_manager.UpdateMaskedDomainListForTesting(mdl);

  // Every domain in the domain map should be matched on b/c no domains are
  // excluded.
  for (auto const& [domain, subdomains] : tld_map_with_domains) {
    EXPECT_TRUE(mdl_manager.Matches(GURL(base::StrCat({"https://", domain})),
                                    net::NetworkAnonymizationKey(),
                                    MdlType::kIncognito));
    EXPECT_TRUE(mdl_manager.Matches(GURL(base::StrCat({"wss://", domain})),
                                    net::NetworkAnonymizationKey(),
                                    MdlType::kIncognito));
    EXPECT_TRUE(mdl_manager.Matches(GURL(base::StrCat({"ws://", domain})),
                                    net::NetworkAnonymizationKey(),
                                    MdlType::kIncognito));
    for (auto const& subdomain : subdomains) {
      EXPECT_TRUE(mdl_manager.Matches(
          GURL(base::StrCat({"https://", subdomain})),
          net::NetworkAnonymizationKey(), MdlType::kIncognito));
      EXPECT_TRUE(mdl_manager.Matches(GURL(base::StrCat({"wss://", subdomain})),
                                      net::NetworkAnonymizationKey(),
                                      MdlType::kIncognito));
      EXPECT_TRUE(mdl_manager.Matches(GURL(base::StrCat({"ws://", subdomain})),
                                      net::NetworkAnonymizationKey(),
                                      MdlType::kIncognito));
    }
  }

  // Now update the MDL with an exclusion list.
  mdl_manager.UpdateMaskedDomainListForTesting(mdl, exclusion_list);

  // An excluded domain nor its subdomains should not be matched on.
  for (auto const& [domain, subdomains] : tld_map_with_domains) {
    bool should_match = !base::Contains(exclusion_list, domain);
    net::NetworkAnonymizationKey nak;

    // If the domain is excluded, it should not be matched on and vice versa.
    EXPECT_EQ(should_match,
              mdl_manager.Matches(GURL(base::StrCat({"https://", domain})), nak,
                                  MdlType::kIncognito));

    for (auto const& subdomain : subdomains) {
      EXPECT_EQ(should_match,
                mdl_manager.Matches(GURL(base::StrCat({"https://", subdomain})),
                                    nak, MdlType::kIncognito));
    }
  }
}

}  // namespace ip_protection