xref: /aosp_15_r20/external/cronet/net/first_party_sets/local_set_declaration_unittest.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
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/local_set_declaration.h"
6 
7 #include <optional>
8 
9 #include "net/base/schemeful_site.h"
10 #include "net/first_party_sets/first_party_set_entry.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 "url/gurl.h"
15 
16 using ::testing::IsEmpty;
17 using ::testing::Pair;
18 using ::testing::UnorderedElementsAre;
19 
20 namespace net {
21 
TEST(LocalSetDeclarationTest,Valid_EmptySet)22 TEST(LocalSetDeclarationTest, Valid_EmptySet) {
23   EXPECT_THAT(LocalSetDeclaration(), IsEmpty());
24 }
25 
TEST(LocalSetDeclarationTest,Valid_Basic)26 TEST(LocalSetDeclarationTest, Valid_Basic) {
27   SchemefulSite primary(GURL("https://primary.test"));
28   SchemefulSite associated(GURL("https://associated.test"));
29 
30   base::flat_map<SchemefulSite, FirstPartySetEntry> entries({
31       {primary, FirstPartySetEntry(primary, SiteType::kPrimary, std::nullopt)},
32       {associated, FirstPartySetEntry(primary, SiteType::kAssociated, 0)},
33   });
34 
35   EXPECT_THAT(LocalSetDeclaration(entries, /*aliases=*/{}).entries(),
36               UnorderedElementsAre(
37                   Pair(primary, FirstPartySetEntry(primary, SiteType::kPrimary,
38                                                    std::nullopt)),
39                   Pair(associated,
40                        FirstPartySetEntry(primary, SiteType::kAssociated, 0))));
41 }
42 
TEST(LocalSetDeclarationTest,Valid_BasicWithAliases)43 TEST(LocalSetDeclarationTest, Valid_BasicWithAliases) {
44   SchemefulSite primary(GURL("https://primary.test"));
45   SchemefulSite primary_cctld(GURL("https://primary.cctld"));
46   SchemefulSite associated(GURL("https://associated.test"));
47   SchemefulSite associated_cctld(GURL("https://associated.cctld"));
48 
49   base::flat_map<SchemefulSite, FirstPartySetEntry> entries({
50       {primary, FirstPartySetEntry(primary, SiteType::kPrimary, std::nullopt)},
51       {associated, FirstPartySetEntry(primary, SiteType::kAssociated, 0)},
52   });
53 
54   base::flat_map<SchemefulSite, SchemefulSite> aliases(
55       {{primary_cctld, primary}, {associated_cctld, associated}});
56 
57   LocalSetDeclaration local_set(entries, aliases);
58 
59   // LocalSetDeclaration should allow these to pass through, after passing
60   // validation.
61   EXPECT_THAT(local_set.entries(),
62               UnorderedElementsAre(
63                   Pair(primary, FirstPartySetEntry(primary, SiteType::kPrimary,
64                                                    std::nullopt)),
65                   Pair(associated,
66                        FirstPartySetEntry(primary, SiteType::kAssociated, 0))));
67 
68   EXPECT_THAT(local_set.aliases(),
69               UnorderedElementsAre(Pair(associated_cctld, associated),
70                                    Pair(primary_cctld, primary)));
71 }
72 
73 }  // namespace net
74