xref: /aosp_15_r20/external/cronet/net/tools/tld_cleanup/tld_cleanup_util_unittest.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2013 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/tools/tld_cleanup/tld_cleanup_util.h"
6 
7 #include "base/files/file_path.h"
8 #include "testing/gmock/include/gmock/gmock.h"
9 #include "testing/gtest/include/gtest/gtest.h"
10 
11 namespace net::tld_cleanup {
12 
13 using testing::ElementsAre;
14 using testing::Pair;
15 
SetupData(const std::string & icann_domains,const std::string & private_domains)16 std::string SetupData(const std::string& icann_domains,
17                       const std::string& private_domains) {
18   return "// ===BEGIN ICANN DOMAINS===\n" +
19          icann_domains +
20          "// ===END ICANN DOMAINS===\n" +
21          "// ===BEGIN PRIVATE DOMAINS===\n" +
22          private_domains +
23          "// ===END PRIVATE DOMAINS===\n";
24 }
25 
TEST(TldCleanupUtilTest,TwoRealTldsSuccessfullyRead)26 TEST(TldCleanupUtilTest, TwoRealTldsSuccessfullyRead) {
27   std::string icann_domains =
28       "foo\n"
29       "bar\n";
30   std::string private_domains = "";
31   RuleMap rules;
32   ASSERT_EQ(
33       NormalizeDataToRuleMap(SetupData(icann_domains, private_domains), rules),
34       NormalizeResult::kSuccess);
35   EXPECT_THAT(
36       rules,
37       ElementsAre(Pair("bar", Rule{/*exception=*/false, /*wildcard=*/false,
38                                    /*is_private=*/false}),
39                   Pair("foo", Rule{/*exception=*/false, /*wildcard=*/false,
40                                    /*is_private=*/false})));
41 }
42 
TEST(TldCleanupUtilTest,TwoRealTldsSuccessfullyRead_WindowsEndings)43 TEST(TldCleanupUtilTest, TwoRealTldsSuccessfullyRead_WindowsEndings) {
44   std::string icann_domains =
45       "foo\r\n"
46       "bar\r\n";
47   std::string private_domains = "";
48   RuleMap rules;
49   ASSERT_EQ(
50       NormalizeDataToRuleMap(SetupData(icann_domains, private_domains), rules),
51       NormalizeResult::kSuccess);
52   EXPECT_THAT(
53       rules,
54       ElementsAre(Pair("bar", Rule{/*exception=*/false, /*wildcard=*/false,
55                                    /*is_private=*/false}),
56                   Pair("foo", Rule{/*exception=*/false, /*wildcard=*/false,
57                                    /*is_private=*/false})));
58 }
59 
TEST(TldCleanupUtilTest,RealTldAutomaticallyAddedForSubdomain)60 TEST(TldCleanupUtilTest, RealTldAutomaticallyAddedForSubdomain) {
61   std::string icann_domains = "foo.bar\n";
62   std::string private_domains = "";
63   RuleMap rules;
64   ASSERT_EQ(
65       NormalizeDataToRuleMap(SetupData(icann_domains, private_domains), rules),
66       NormalizeResult::kSuccess);
67   EXPECT_THAT(
68       rules,
69       ElementsAre(Pair("bar", Rule{/*exception=*/false, /*wildcard=*/false,
70                                    /*is_private=*/false}),
71                   Pair("foo.bar", Rule{/*exception=*/false, /*wildcard=*/false,
72                                        /*is_private=*/false})));
73 }
74 
TEST(TldCleanupUtilTest,PrivateTldMarkedAsPrivate)75 TEST(TldCleanupUtilTest, PrivateTldMarkedAsPrivate) {
76   std::string icann_domains =
77       "foo\n"
78       "bar\n";
79   std::string private_domains = "baz\n";
80   RuleMap rules;
81   ASSERT_EQ(
82       NormalizeDataToRuleMap(SetupData(icann_domains, private_domains), rules),
83       NormalizeResult::kSuccess);
84   EXPECT_THAT(
85       rules,
86       ElementsAre(Pair("bar", Rule{/*exception=*/false, /*wildcard=*/false,
87                                    /*is_private=*/false}),
88                   Pair("baz", Rule{/*exception=*/false, /*wildcard=*/false,
89                                    /*is_private=*/true}),
90                   Pair("foo", Rule{/*exception=*/false, /*wildcard=*/false,
91                                    /*is_private=*/false})));
92 }
93 
TEST(TldCleanupUtilTest,PrivateDomainMarkedAsPrivate)94 TEST(TldCleanupUtilTest, PrivateDomainMarkedAsPrivate) {
95   std::string icann_domains = "bar\n";
96   std::string private_domains = "foo.bar\n";
97   RuleMap rules;
98   ASSERT_EQ(
99       NormalizeDataToRuleMap(SetupData(icann_domains, private_domains), rules),
100       NormalizeResult::kSuccess);
101   EXPECT_THAT(
102       rules,
103       ElementsAre(Pair("bar", Rule{/*exception=*/false, /*wildcard=*/false,
104                                    /*is_private=*/false}),
105                   Pair("foo.bar", Rule{/*exception=*/false, /*wildcard=*/false,
106                                        /*is_private=*/true})));
107 }
108 
TEST(TldCleanupUtilTest,ExtraTldRuleIsNotMarkedPrivate)109 TEST(TldCleanupUtilTest, ExtraTldRuleIsNotMarkedPrivate) {
110   std::string icann_domains =
111       "foo.bar\n"
112       "baz.bar\n";
113   std::string private_domains = "qux.bar\n";
114   RuleMap rules;
115   ASSERT_EQ(
116       NormalizeDataToRuleMap(SetupData(icann_domains, private_domains), rules),
117       NormalizeResult::kSuccess);
118   EXPECT_THAT(
119       rules,
120       ElementsAre(Pair("bar", Rule{/*exception=*/false, /*wildcard=*/false,
121                                    /*is_private=*/false}),
122                   Pair("baz.bar", Rule{/*exception=*/false, /*wildcard=*/false,
123                                        /*is_private=*/false}),
124                   Pair("foo.bar", Rule{/*exception=*/false, /*wildcard=*/false,
125                                        /*is_private=*/false}),
126                   Pair("qux.bar", Rule{/*exception=*/false, /*wildcard=*/false,
127                                        /*is_private=*/true})));
128 }
129 
TEST(TldCleanupUtilTest,WildcardAndExceptionParsedCorrectly)130 TEST(TldCleanupUtilTest, WildcardAndExceptionParsedCorrectly) {
131   std::string icann_domains =
132       "*.bar\n"
133       "!foo.bar\n";
134   std::string private_domains = "!baz.bar\n";
135   RuleMap rules;
136   ASSERT_EQ(
137       NormalizeDataToRuleMap(SetupData(icann_domains, private_domains), rules),
138       NormalizeResult::kSuccess);
139   EXPECT_THAT(
140       rules,
141       ElementsAre(Pair("bar", Rule{/*exception=*/false, /*wildcard=*/true,
142                                    /*is_private=*/false}),
143                   Pair("baz.bar", Rule{/*exception=*/true, /*wildcard=*/false,
144                                        /*is_private=*/true}),
145                   Pair("foo.bar", Rule{/*exception=*/true, /*wildcard=*/false,
146                                        /*is_private=*/false})));
147 }
148 
TEST(TldCleanupUtilTest,RuleSerialization)149 TEST(TldCleanupUtilTest, RuleSerialization) {
150   EXPECT_THAT(
151       RulesToGperf({
152           {"domain0",
153            Rule{/*exception=*/false, /*wildcard=*/false, /*is_private=*/false}},
154           {"domain1",
155            Rule{/*exception=*/false, /*wildcard=*/false, /*is_private=*/true}},
156           {"domain2",
157            Rule{/*exception=*/false, /*wildcard=*/true, /*is_private=*/false}},
158           {"domain3",
159            Rule{/*exception=*/false, /*wildcard=*/true, /*is_private=*/true}},
160           {"domain4",
161            Rule{/*exception=*/true, /*wildcard=*/false, /*is_private=*/false}},
162           {"domain5",
163            Rule{/*exception=*/true, /*wildcard=*/false, /*is_private=*/true}},
164           {"domain6",
165            Rule{/*exception=*/true, /*wildcard=*/true, /*is_private=*/false}},
166           {"domain7",
167            Rule{/*exception=*/true, /*wildcard=*/true, /*is_private=*/true}},
168       }),
169       testing::EndsWith(
170           R"(%%
171 domain0, 0
172 domain1, 4
173 domain2, 2
174 domain3, 6
175 domain4, 1
176 domain5, 5
177 domain6, 1
178 domain7, 5
179 %%
180 )"));
181 }
182 
183 }  // namespace net::tld_cleanup
184