1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "googleurl/src/gurl.h"
6 #include "net/base/registry_controlled_domains/registry_controlled_domain.h"
7 #include "testing/gtest/include/gtest/gtest.h"
9 #include "effective_tld_names_unittest1.cc"
11 #undef MIN_WORD_LENGTH
12 #undef MAX_WORD_LENGTH
15 #include "effective_tld_names_unittest2.cc"
20 std::string
GetDomainFromURL(const std::string
& url
) {
21 return RegistryControlledDomainService::GetDomainAndRegistry(GURL(url
));
24 std::string
GetDomainFromHost(const std::string
& host
) {
25 return RegistryControlledDomainService::GetDomainAndRegistry(host
);
28 size_t GetRegistryLengthFromURL(const std::string
& url
,
29 bool allow_unknown_registries
) {
30 return RegistryControlledDomainService::GetRegistryLength(GURL(url
),
31 allow_unknown_registries
);
34 size_t GetRegistryLengthFromHost(const std::string
& host
,
35 bool allow_unknown_registries
) {
36 return RegistryControlledDomainService::GetRegistryLength(host
,
37 allow_unknown_registries
);
40 bool CompareDomains(const std::string
& url1
, const std::string
& url2
) {
43 return RegistryControlledDomainService::SameDomainOrHost(g1
, g2
);
48 class RegistryControlledDomainTest
: public testing::Test
{
50 typedef RegistryControlledDomainService::FindDomainPtr FindDomainPtr
;
51 void UseDomainData(FindDomainPtr function
) {
52 RegistryControlledDomainService::UseFindDomainFunction(function
);
55 virtual void TearDown() {
56 RegistryControlledDomainService::UseFindDomainFunction(NULL
);
60 TEST_F(RegistryControlledDomainTest
, TestGetDomainAndRegistry
) {
61 UseDomainData(Perfect_Hash_Test1::FindDomain
);
63 // Test GURL version of GetDomainAndRegistry().
64 EXPECT_EQ("baz.jp", GetDomainFromURL("http://a.baz.jp/file.html")); // 1
65 EXPECT_EQ("baz.jp.", GetDomainFromURL("http://a.baz.jp./file.html")); // 1
66 EXPECT_EQ("", GetDomainFromURL("http://ac.jp")); // 2
67 EXPECT_EQ("", GetDomainFromURL("http://a.bar.jp")); // 3
68 EXPECT_EQ("", GetDomainFromURL("http://bar.jp")); // 3
69 EXPECT_EQ("", GetDomainFromURL("http://baz.bar.jp")); // 3 4
70 EXPECT_EQ("a.b.baz.bar.jp", GetDomainFromURL("http://a.b.baz.bar.jp"));
72 EXPECT_EQ("pref.bar.jp", GetDomainFromURL("http://baz.pref.bar.jp")); // 5
73 EXPECT_EQ("b.bar.baz.com.", GetDomainFromURL("http://a.b.bar.baz.com."));
75 EXPECT_EQ("a.d.c", GetDomainFromURL("http://a.d.c")); // 7
76 EXPECT_EQ("a.d.c", GetDomainFromURL("http://.a.d.c")); // 7
77 EXPECT_EQ("a.d.c", GetDomainFromURL("http://..a.d.c")); // 7
78 EXPECT_EQ("b.c", GetDomainFromURL("http://a.b.c")); // 7 8
79 EXPECT_EQ("baz.com", GetDomainFromURL("http://baz.com")); // none
80 EXPECT_EQ("baz.com.", GetDomainFromURL("http://baz.com.")); // none
82 EXPECT_EQ("", GetDomainFromURL(""));
83 EXPECT_EQ("", GetDomainFromURL("http://"));
84 EXPECT_EQ("", GetDomainFromURL("file:///C:/file.html"));
85 EXPECT_EQ("", GetDomainFromURL("http://foo.com.."));
86 EXPECT_EQ("", GetDomainFromURL("http://..."));
87 EXPECT_EQ("", GetDomainFromURL("http://192.168.0.1"));
88 EXPECT_EQ("", GetDomainFromURL("http://localhost"));
89 EXPECT_EQ("", GetDomainFromURL("http://localhost."));
90 EXPECT_EQ("", GetDomainFromURL("http:////Comment"));
92 // Test std::string version of GetDomainAndRegistry(). Uses the same
93 // underpinnings as the GURL version, so this is really more of a check of
94 // CanonicalizeHost().
95 EXPECT_EQ("baz.jp", GetDomainFromHost("a.baz.jp")); // 1
96 EXPECT_EQ("baz.jp.", GetDomainFromHost("a.baz.jp.")); // 1
97 EXPECT_EQ("", GetDomainFromHost("ac.jp")); // 2
98 EXPECT_EQ("", GetDomainFromHost("a.bar.jp")); // 3
99 EXPECT_EQ("", GetDomainFromHost("bar.jp")); // 3
100 EXPECT_EQ("", GetDomainFromHost("baz.bar.jp")); // 3 4
101 EXPECT_EQ("a.b.baz.bar.jp", GetDomainFromHost("a.b.baz.bar.jp")); // 3 4
102 EXPECT_EQ("pref.bar.jp", GetDomainFromHost("baz.pref.bar.jp")); // 5
103 EXPECT_EQ("b.bar.baz.com.", GetDomainFromHost("a.b.bar.baz.com.")); // 6
104 EXPECT_EQ("a.d.c", GetDomainFromHost("a.d.c")); // 7
105 EXPECT_EQ("a.d.c", GetDomainFromHost(".a.d.c")); // 7
106 EXPECT_EQ("a.d.c", GetDomainFromHost("..a.d.c")); // 7
107 EXPECT_EQ("b.c", GetDomainFromHost("a.b.c")); // 7 8
108 EXPECT_EQ("baz.com", GetDomainFromHost("baz.com")); // none
109 EXPECT_EQ("baz.com.", GetDomainFromHost("baz.com.")); // none
111 EXPECT_EQ("", GetDomainFromHost(""));
112 EXPECT_EQ("", GetDomainFromHost("foo.com.."));
113 EXPECT_EQ("", GetDomainFromHost("..."));
114 EXPECT_EQ("", GetDomainFromHost("192.168.0.1"));
115 EXPECT_EQ("", GetDomainFromHost("localhost."));
116 EXPECT_EQ("", GetDomainFromHost(".localhost."));
119 TEST_F(RegistryControlledDomainTest
, TestGetRegistryLength
) {
120 UseDomainData(Perfect_Hash_Test1::FindDomain
);
122 // Test GURL version of GetRegistryLength().
123 EXPECT_EQ(2U, GetRegistryLengthFromURL("http://a.baz.jp/file.html", false));
125 EXPECT_EQ(3U, GetRegistryLengthFromURL("http://a.baz.jp./file.html", false));
127 EXPECT_EQ(0U, GetRegistryLengthFromURL("http://ac.jp", false)); // 2
128 EXPECT_EQ(0U, GetRegistryLengthFromURL("http://a.bar.jp", false)); // 3
129 EXPECT_EQ(0U, GetRegistryLengthFromURL("http://bar.jp", false)); // 3
130 EXPECT_EQ(0U, GetRegistryLengthFromURL("http://baz.bar.jp", false)); // 3 4
131 EXPECT_EQ(12U, GetRegistryLengthFromURL("http://a.b.baz.bar.jp", false));
133 EXPECT_EQ(6U, GetRegistryLengthFromURL("http://baz.pref.bar.jp", false));
135 EXPECT_EQ(11U, GetRegistryLengthFromURL("http://a.b.bar.baz.com", false));
137 EXPECT_EQ(3U, GetRegistryLengthFromURL("http://a.d.c", false)); // 7
138 EXPECT_EQ(3U, GetRegistryLengthFromURL("http://.a.d.c", false)); // 7
139 EXPECT_EQ(3U, GetRegistryLengthFromURL("http://..a.d.c", false)); // 7
140 EXPECT_EQ(1U, GetRegistryLengthFromURL("http://a.b.c", false)); // 7 8
141 EXPECT_EQ(0U, GetRegistryLengthFromURL("http://baz.com", false)); // none
142 EXPECT_EQ(0U, GetRegistryLengthFromURL("http://baz.com.", false)); // none
143 EXPECT_EQ(3U, GetRegistryLengthFromURL("http://baz.com", true)); // none
144 EXPECT_EQ(4U, GetRegistryLengthFromURL("http://baz.com.", true)); // none
146 EXPECT_EQ(std::string::npos
, GetRegistryLengthFromURL("", false));
147 EXPECT_EQ(std::string::npos
, GetRegistryLengthFromURL("http://", false));
148 EXPECT_EQ(std::string::npos
,
149 GetRegistryLengthFromURL("file:///C:/file.html", false));
150 EXPECT_EQ(0U, GetRegistryLengthFromURL("http://foo.com..", false));
151 EXPECT_EQ(0U, GetRegistryLengthFromURL("http://...", false));
152 EXPECT_EQ(0U, GetRegistryLengthFromURL("http://192.168.0.1", false));
153 EXPECT_EQ(0U, GetRegistryLengthFromURL("http://localhost", false));
154 EXPECT_EQ(0U, GetRegistryLengthFromURL("http://localhost", true));
155 EXPECT_EQ(0U, GetRegistryLengthFromURL("http://localhost.", false));
156 EXPECT_EQ(0U, GetRegistryLengthFromURL("http://localhost.", true));
157 EXPECT_EQ(0U, GetRegistryLengthFromURL("http:////Comment", false));
159 // Test std::string version of GetRegistryLength(). Uses the same
160 // underpinnings as the GURL version, so this is really more of a check of
161 // CanonicalizeHost().
162 EXPECT_EQ(2U, GetRegistryLengthFromHost("a.baz.jp", false)); // 1
163 EXPECT_EQ(3U, GetRegistryLengthFromHost("a.baz.jp.", false)); // 1
164 EXPECT_EQ(0U, GetRegistryLengthFromHost("ac.jp", false)); // 2
165 EXPECT_EQ(0U, GetRegistryLengthFromHost("a.bar.jp", false)); // 3
166 EXPECT_EQ(0U, GetRegistryLengthFromHost("bar.jp", false)); // 3
167 EXPECT_EQ(0U, GetRegistryLengthFromHost("baz.bar.jp", false)); // 3 4
168 EXPECT_EQ(12U, GetRegistryLengthFromHost("a.b.baz.bar.jp", false)); // 4
169 EXPECT_EQ(6U, GetRegistryLengthFromHost("baz.pref.bar.jp", false)); // 5
170 EXPECT_EQ(11U, GetRegistryLengthFromHost("a.b.bar.baz.com", false)); // 6
171 EXPECT_EQ(3U, GetRegistryLengthFromHost("a.d.c", false)); // 7
172 EXPECT_EQ(3U, GetRegistryLengthFromHost(".a.d.c", false)); // 7
173 EXPECT_EQ(3U, GetRegistryLengthFromHost("..a.d.c", false)); // 7
174 EXPECT_EQ(1U, GetRegistryLengthFromHost("a.b.c", false)); // 7 8
175 EXPECT_EQ(0U, GetRegistryLengthFromHost("baz.com", false)); // none
176 EXPECT_EQ(0U, GetRegistryLengthFromHost("baz.com.", false)); // none
177 EXPECT_EQ(3U, GetRegistryLengthFromHost("baz.com", true)); // none
178 EXPECT_EQ(4U, GetRegistryLengthFromHost("baz.com.", true)); // none
180 EXPECT_EQ(std::string::npos
, GetRegistryLengthFromHost("", false));
181 EXPECT_EQ(0U, GetRegistryLengthFromHost("foo.com..", false));
182 EXPECT_EQ(0U, GetRegistryLengthFromHost("..", false));
183 EXPECT_EQ(0U, GetRegistryLengthFromHost("192.168.0.1", false));
184 EXPECT_EQ(0U, GetRegistryLengthFromHost("localhost", false));
185 EXPECT_EQ(0U, GetRegistryLengthFromHost("localhost", true));
186 EXPECT_EQ(0U, GetRegistryLengthFromHost("localhost.", false));
187 EXPECT_EQ(0U, GetRegistryLengthFromHost("localhost.", true));
190 TEST_F(RegistryControlledDomainTest
, TestSameDomainOrHost
) {
191 UseDomainData(Perfect_Hash_Test2::FindDomain
);
193 EXPECT_TRUE(CompareDomains("http://a.b.bar.jp/file.html",
194 "http://a.b.bar.jp/file.html")); // b.bar.jp
195 EXPECT_TRUE(CompareDomains("http://a.b.bar.jp/file.html",
196 "http://b.b.bar.jp/file.html")); // b.bar.jp
197 EXPECT_FALSE(CompareDomains("http://a.foo.jp/file.html", // foo.jp
198 "http://a.not.jp/file.html")); // not.jp
199 EXPECT_FALSE(CompareDomains("http://a.foo.jp/file.html", // foo.jp
200 "http://a.foo.jp./file.html")); // foo.jp.
201 EXPECT_FALSE(CompareDomains("http://a.com/file.html", // a.com
202 "http://b.com/file.html")); // b.com
203 EXPECT_TRUE(CompareDomains("http://a.x.com/file.html",
204 "http://b.x.com/file.html")); // x.com
205 EXPECT_TRUE(CompareDomains("http://a.x.com/file.html",
206 "http://.x.com/file.html")); // x.com
207 EXPECT_TRUE(CompareDomains("http://a.x.com/file.html",
208 "http://..b.x.com/file.html")); // x.com
209 EXPECT_TRUE(CompareDomains("http://intranet/file.html",
210 "http://intranet/file.html")); // intranet
211 EXPECT_TRUE(CompareDomains("http://127.0.0.1/file.html",
212 "http://127.0.0.1/file.html")); // 127.0.0.1
213 EXPECT_FALSE(CompareDomains("http://192.168.0.1/file.html", // 192.168.0.1
214 "http://127.0.0.1/file.html")); // 127.0.0.1
215 EXPECT_FALSE(CompareDomains("file:///C:/file.html",
216 "file:///C:/file.html")); // no host
219 TEST_F(RegistryControlledDomainTest
, TestDefaultData
) {
220 // Note that no data is set: we're using the default rules.
221 EXPECT_EQ(3U, GetRegistryLengthFromURL("http://google.com", false));
222 EXPECT_EQ(3U, GetRegistryLengthFromURL("http://stanford.edu", false));
223 EXPECT_EQ(3U, GetRegistryLengthFromURL("http://ustreas.gov", false));
224 EXPECT_EQ(3U, GetRegistryLengthFromURL("http://icann.net", false));
225 EXPECT_EQ(3U, GetRegistryLengthFromURL("http://ferretcentral.org", false));
226 EXPECT_EQ(0U, GetRegistryLengthFromURL("http://nowhere.foo", false));
227 EXPECT_EQ(3U, GetRegistryLengthFromURL("http://nowhere.foo", true));