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