1 // Copyright 2022 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "net/first_party_sets/first_party_sets_context_config.h"
6
7 #include "net/base/schemeful_site.h"
8 #include "net/first_party_sets/first_party_set_entry.h"
9 #include "net/first_party_sets/first_party_set_entry_override.h"
10 #include "net/first_party_sets/first_party_sets_context_config.h"
11 #include "testing/gmock/include/gmock/gmock-matchers.h"
12 #include "testing/gmock/include/gmock/gmock.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14 #include "third_party/abseil-cpp/absl/types/optional.h"
15 #include "url/gurl.h"
16
17 using ::testing::Optional;
18
19 MATCHER_P(OverridesTo, entry, "") {
20 return !arg.IsDeletion() &&
21 testing::ExplainMatchResult(entry, arg.GetEntry(), result_listener);
22 }
23
24 namespace net {
25
TEST(FirstPartySetsContextConfigTest,FindOverride_empty)26 TEST(FirstPartySetsContextConfigTest, FindOverride_empty) {
27 EXPECT_EQ(FirstPartySetsContextConfig().FindOverride(
28 SchemefulSite(GURL("https://example.test"))),
29 absl::nullopt);
30 }
31
TEST(FirstPartySetsContextConfigTest,FindOverride_irrelevant)32 TEST(FirstPartySetsContextConfigTest, FindOverride_irrelevant) {
33 SchemefulSite example(GURL("https://example.test"));
34 FirstPartySetEntry entry(example, SiteType::kPrimary, absl::nullopt);
35 SchemefulSite foo(GURL("https://foo.test"));
36
37 EXPECT_EQ(FirstPartySetsContextConfig(
38 {{example, FirstPartySetEntryOverride(entry)}})
39 .FindOverride(foo),
40 absl::nullopt);
41 }
42
TEST(FirstPartySetsContextConfigTest,FindOverride_deletion)43 TEST(FirstPartySetsContextConfigTest, FindOverride_deletion) {
44 SchemefulSite example(GURL("https://example.test"));
45
46 EXPECT_THAT(
47 FirstPartySetsContextConfig({{example, FirstPartySetEntryOverride()}})
48 .FindOverride(example),
49 Optional(FirstPartySetEntryOverride()));
50 }
51
TEST(FirstPartySetsContextConfigTest,FindOverride_modification)52 TEST(FirstPartySetsContextConfigTest, FindOverride_modification) {
53 SchemefulSite example(GURL("https://example.test"));
54 FirstPartySetEntry entry(example, SiteType::kPrimary, absl::nullopt);
55
56 EXPECT_THAT(FirstPartySetsContextConfig(
57 {{example, FirstPartySetEntryOverride(entry)}})
58 .FindOverride(example),
59 Optional(OverridesTo(entry)));
60 }
61
TEST(FirstPartySetsContextConfigTest,Contains)62 TEST(FirstPartySetsContextConfigTest, Contains) {
63 SchemefulSite example(GURL("https://example.test"));
64 SchemefulSite decoy(GURL("https://decoy.test"));
65
66 FirstPartySetsContextConfig config({{example, FirstPartySetEntryOverride()}});
67
68 EXPECT_TRUE(config.Contains(example));
69 EXPECT_FALSE(config.Contains(decoy));
70 }
71
TEST(FirstPartySetsContextConfigTest,ForEachCustomizationEntry_FullIteration)72 TEST(FirstPartySetsContextConfigTest, ForEachCustomizationEntry_FullIteration) {
73 SchemefulSite example(GURL("https://example.test"));
74 SchemefulSite foo(GURL("https://foo.test"));
75
76 FirstPartySetsContextConfig config({{example, FirstPartySetEntryOverride()},
77 {foo, FirstPartySetEntryOverride()}});
78
79 int count = 0;
80 EXPECT_TRUE(config.ForEachCustomizationEntry(
81 [&](const SchemefulSite& site,
82 const FirstPartySetEntryOverride& override) {
83 ++count;
84 return true;
85 }));
86 EXPECT_EQ(count, 2);
87 }
88
TEST(FirstPartySetsContextConfigTest,ForEachCustomizationEntry_EarlyReturn)89 TEST(FirstPartySetsContextConfigTest, ForEachCustomizationEntry_EarlyReturn) {
90 SchemefulSite example(GURL("https://example.test"));
91 SchemefulSite foo(GURL("https://foo.test"));
92
93 FirstPartySetsContextConfig config({{example, FirstPartySetEntryOverride()},
94 {foo, FirstPartySetEntryOverride()}});
95
96 int count = 0;
97 EXPECT_FALSE(config.ForEachCustomizationEntry(
98 [&](const SchemefulSite& site,
99 const FirstPartySetEntryOverride& override) {
100 ++count;
101 return count < 1;
102 }));
103 EXPECT_EQ(count, 1);
104 }
105
106 } // namespace net
107