OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" | 5 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" |
6 #include "testing/gtest/include/gtest/gtest.h" | 6 #include "testing/gtest/include/gtest/gtest.h" |
7 #include "url/gurl.h" | 7 #include "url/gurl.h" |
8 | 8 |
9 #include "effective_tld_names_unittest1.cc" | 9 namespace { |
10 static const char* const Perfect_Hash_Test1_stringpool = stringpool1; | 10 namespace test1 { |
11 #undef TOTAL_KEYWORDS | 11 #include "effective_tld_names_unittest1-inc.cc" |
12 #undef MIN_WORD_LENGTH | 12 } |
13 #undef MAX_WORD_LENGTH | 13 namespace test2 { |
14 #undef MIN_HASH_VALUE | 14 #include "effective_tld_names_unittest2-inc.cc" |
15 #undef MAX_HASH_VALUE | 15 } |
16 #include "effective_tld_names_unittest2.cc" | 16 namespace test3 { |
17 static const char* const Perfect_Hash_Test2_stringpool = stringpool2; | 17 #include "effective_tld_names_unittest3-inc.cc" |
18 } | |
19 namespace test4 { | |
20 #include "effective_tld_names_unittest4-inc.cc" | |
21 } | |
22 namespace test5 { | |
23 #include "effective_tld_names_unittest5-inc.cc" | |
24 } | |
25 namespace test6 { | |
26 #include "effective_tld_names_unittest6-inc.cc" | |
27 } | |
28 } // namespace | |
18 | 29 |
19 namespace net { | 30 namespace net { |
20 namespace registry_controlled_domains { | 31 namespace registry_controlled_domains { |
21 namespace { | 32 namespace { |
22 | 33 |
23 std::string GetDomainFromURL(const std::string& url) { | 34 std::string GetDomainFromURL(const std::string& url) { |
24 return GetDomainAndRegistry(GURL(url), EXCLUDE_PRIVATE_REGISTRIES); | 35 return GetDomainAndRegistry(GURL(url), EXCLUDE_PRIVATE_REGISTRIES); |
25 } | 36 } |
26 | 37 |
27 std::string GetDomainFromHost(const std::string& host) { | 38 std::string GetDomainFromHost(const std::string& host) { |
(...skipping 15 matching lines...) Expand all Loading... | |
43 unknown_filter, | 54 unknown_filter, |
44 INCLUDE_PRIVATE_REGISTRIES); | 55 INCLUDE_PRIVATE_REGISTRIES); |
45 } | 56 } |
46 | 57 |
47 size_t GetRegistryLengthFromHost( | 58 size_t GetRegistryLengthFromHost( |
48 const std::string& host, | 59 const std::string& host, |
49 UnknownRegistryFilter unknown_filter) { | 60 UnknownRegistryFilter unknown_filter) { |
50 return GetRegistryLength(host, unknown_filter, EXCLUDE_PRIVATE_REGISTRIES); | 61 return GetRegistryLength(host, unknown_filter, EXCLUDE_PRIVATE_REGISTRIES); |
51 } | 62 } |
52 | 63 |
64 size_t GetRegistryLengthFromHostIncludingPrivate( | |
65 const std::string& host, UnknownRegistryFilter unknown_filter) { | |
Ryan Sleevi
2014/04/23 01:52:18
Did you run "clang-format" over this (eg: via "git
| |
66 return GetRegistryLength(host, unknown_filter, INCLUDE_PRIVATE_REGISTRIES); | |
67 } | |
68 | |
53 bool CompareDomains(const std::string& url1, const std::string& url2) { | 69 bool CompareDomains(const std::string& url1, const std::string& url2) { |
54 GURL g1 = GURL(url1); | 70 GURL g1 = GURL(url1); |
55 GURL g2 = GURL(url2); | 71 GURL g2 = GURL(url2); |
56 return SameDomainOrHost(g1, g2, EXCLUDE_PRIVATE_REGISTRIES); | 72 return SameDomainOrHost(g1, g2, EXCLUDE_PRIVATE_REGISTRIES); |
57 } | 73 } |
58 | 74 |
59 } // namespace | 75 } // namespace |
60 | 76 |
61 class RegistryControlledDomainTest : public testing::Test { | 77 class RegistryControlledDomainTest : public testing::Test { |
62 protected: | 78 protected: |
63 void UseDomainData(FindDomainPtr function, const char* const stringpool) { | 79 template <typename Graph> |
64 SetFindDomainFunctionAndStringPoolForTesting(function, stringpool); | 80 void UseDomainData(const Graph &graph) { |
Ryan Sleevi
2014/04/23 01:52:18
style nit: const Graph& graph
| |
81 SetFindDomainGraph(graph, sizeof(Graph)); | |
65 } | 82 } |
66 | 83 |
67 virtual void TearDown() { | 84 virtual void TearDown() { SetFindDomainGraph(); } |
68 SetFindDomainFunctionAndStringPoolForTesting(NULL, NULL); | |
69 } | |
70 }; | 85 }; |
71 | 86 |
72 TEST_F(RegistryControlledDomainTest, TestGetDomainAndRegistry) { | 87 TEST_F(RegistryControlledDomainTest, TestGetDomainAndRegistry) { |
73 UseDomainData(Perfect_Hash_Test1::FindDomain, Perfect_Hash_Test1_stringpool); | 88 UseDomainData(test1::kDafsa); |
74 | 89 |
75 // Test GURL version of GetDomainAndRegistry(). | 90 // Test GURL version of GetDomainAndRegistry(). |
76 EXPECT_EQ("baz.jp", GetDomainFromURL("http://a.baz.jp/file.html")); // 1 | 91 EXPECT_EQ("baz.jp", GetDomainFromURL("http://a.baz.jp/file.html")); // 1 |
77 EXPECT_EQ("baz.jp.", GetDomainFromURL("http://a.baz.jp./file.html")); // 1 | 92 EXPECT_EQ("baz.jp.", GetDomainFromURL("http://a.baz.jp./file.html")); // 1 |
78 EXPECT_EQ("", GetDomainFromURL("http://ac.jp")); // 2 | 93 EXPECT_EQ("", GetDomainFromURL("http://ac.jp")); // 2 |
79 EXPECT_EQ("", GetDomainFromURL("http://a.bar.jp")); // 3 | 94 EXPECT_EQ("", GetDomainFromURL("http://a.bar.jp")); // 3 |
80 EXPECT_EQ("", GetDomainFromURL("http://bar.jp")); // 3 | 95 EXPECT_EQ("", GetDomainFromURL("http://bar.jp")); // 3 |
81 EXPECT_EQ("", GetDomainFromURL("http://baz.bar.jp")); // 3 4 | 96 EXPECT_EQ("", GetDomainFromURL("http://baz.bar.jp")); // 3 4 |
82 EXPECT_EQ("a.b.baz.bar.jp", GetDomainFromURL("http://a.b.baz.bar.jp")); | 97 EXPECT_EQ("a.b.baz.bar.jp", GetDomainFromURL("http://a.b.baz.bar.jp")); |
83 // 4 | 98 // 4 |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
122 | 137 |
123 EXPECT_EQ("", GetDomainFromHost(std::string())); | 138 EXPECT_EQ("", GetDomainFromHost(std::string())); |
124 EXPECT_EQ("", GetDomainFromHost("foo.com..")); | 139 EXPECT_EQ("", GetDomainFromHost("foo.com..")); |
125 EXPECT_EQ("", GetDomainFromHost("...")); | 140 EXPECT_EQ("", GetDomainFromHost("...")); |
126 EXPECT_EQ("", GetDomainFromHost("192.168.0.1")); | 141 EXPECT_EQ("", GetDomainFromHost("192.168.0.1")); |
127 EXPECT_EQ("", GetDomainFromHost("localhost.")); | 142 EXPECT_EQ("", GetDomainFromHost("localhost.")); |
128 EXPECT_EQ("", GetDomainFromHost(".localhost.")); | 143 EXPECT_EQ("", GetDomainFromHost(".localhost.")); |
129 } | 144 } |
130 | 145 |
131 TEST_F(RegistryControlledDomainTest, TestGetRegistryLength) { | 146 TEST_F(RegistryControlledDomainTest, TestGetRegistryLength) { |
132 UseDomainData(Perfect_Hash_Test1::FindDomain, Perfect_Hash_Test1_stringpool); | 147 UseDomainData(test1::kDafsa); |
133 | 148 |
134 // Test GURL version of GetRegistryLength(). | 149 // Test GURL version of GetRegistryLength(). |
135 EXPECT_EQ(2U, GetRegistryLengthFromURL("http://a.baz.jp/file.html", | 150 EXPECT_EQ(2U, GetRegistryLengthFromURL("http://a.baz.jp/file.html", |
136 EXCLUDE_UNKNOWN_REGISTRIES)); // 1 | 151 EXCLUDE_UNKNOWN_REGISTRIES)); // 1 |
137 EXPECT_EQ(3U, GetRegistryLengthFromURL("http://a.baz.jp./file.html", | 152 EXPECT_EQ(3U, GetRegistryLengthFromURL("http://a.baz.jp./file.html", |
138 EXCLUDE_UNKNOWN_REGISTRIES)); // 1 | 153 EXCLUDE_UNKNOWN_REGISTRIES)); // 1 |
139 EXPECT_EQ(0U, GetRegistryLengthFromURL("http://ac.jp", | 154 EXPECT_EQ(0U, GetRegistryLengthFromURL("http://ac.jp", |
140 EXCLUDE_UNKNOWN_REGISTRIES)); // 2 | 155 EXCLUDE_UNKNOWN_REGISTRIES)); // 2 |
141 EXPECT_EQ(0U, GetRegistryLengthFromURL("http://a.bar.jp", | 156 EXPECT_EQ(0U, GetRegistryLengthFromURL("http://a.bar.jp", |
142 EXCLUDE_UNKNOWN_REGISTRIES)); // 3 | 157 EXCLUDE_UNKNOWN_REGISTRIES)); // 3 |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
241 EXCLUDE_UNKNOWN_REGISTRIES)); | 256 EXCLUDE_UNKNOWN_REGISTRIES)); |
242 EXPECT_EQ(0U, GetRegistryLengthFromHost("localhost", | 257 EXPECT_EQ(0U, GetRegistryLengthFromHost("localhost", |
243 INCLUDE_UNKNOWN_REGISTRIES)); | 258 INCLUDE_UNKNOWN_REGISTRIES)); |
244 EXPECT_EQ(0U, GetRegistryLengthFromHost("localhost.", | 259 EXPECT_EQ(0U, GetRegistryLengthFromHost("localhost.", |
245 EXCLUDE_UNKNOWN_REGISTRIES)); | 260 EXCLUDE_UNKNOWN_REGISTRIES)); |
246 EXPECT_EQ(0U, GetRegistryLengthFromHost("localhost.", | 261 EXPECT_EQ(0U, GetRegistryLengthFromHost("localhost.", |
247 INCLUDE_UNKNOWN_REGISTRIES)); | 262 INCLUDE_UNKNOWN_REGISTRIES)); |
248 } | 263 } |
249 | 264 |
250 TEST_F(RegistryControlledDomainTest, TestSameDomainOrHost) { | 265 TEST_F(RegistryControlledDomainTest, TestSameDomainOrHost) { |
251 UseDomainData(Perfect_Hash_Test2::FindDomain, Perfect_Hash_Test2_stringpool); | 266 UseDomainData(test2::kDafsa); |
252 | 267 |
253 EXPECT_TRUE(CompareDomains("http://a.b.bar.jp/file.html", | 268 EXPECT_TRUE(CompareDomains("http://a.b.bar.jp/file.html", |
254 "http://a.b.bar.jp/file.html")); // b.bar.jp | 269 "http://a.b.bar.jp/file.html")); // b.bar.jp |
255 EXPECT_TRUE(CompareDomains("http://a.b.bar.jp/file.html", | 270 EXPECT_TRUE(CompareDomains("http://a.b.bar.jp/file.html", |
256 "http://b.b.bar.jp/file.html")); // b.bar.jp | 271 "http://b.b.bar.jp/file.html")); // b.bar.jp |
257 EXPECT_FALSE(CompareDomains("http://a.foo.jp/file.html", // foo.jp | 272 EXPECT_FALSE(CompareDomains("http://a.foo.jp/file.html", // foo.jp |
258 "http://a.not.jp/file.html")); // not.jp | 273 "http://a.not.jp/file.html")); // not.jp |
259 EXPECT_FALSE(CompareDomains("http://a.foo.jp/file.html", // foo.jp | 274 EXPECT_FALSE(CompareDomains("http://a.foo.jp/file.html", // foo.jp |
260 "http://a.foo.jp./file.html")); // foo.jp. | 275 "http://a.foo.jp./file.html")); // foo.jp. |
261 EXPECT_FALSE(CompareDomains("http://a.com/file.html", // a.com | 276 EXPECT_FALSE(CompareDomains("http://a.com/file.html", // a.com |
(...skipping 26 matching lines...) Expand all Loading... | |
288 EXCLUDE_UNKNOWN_REGISTRIES)); | 303 EXCLUDE_UNKNOWN_REGISTRIES)); |
289 EXPECT_EQ(3U, GetRegistryLengthFromURL("http://ferretcentral.org", | 304 EXPECT_EQ(3U, GetRegistryLengthFromURL("http://ferretcentral.org", |
290 EXCLUDE_UNKNOWN_REGISTRIES)); | 305 EXCLUDE_UNKNOWN_REGISTRIES)); |
291 EXPECT_EQ(0U, GetRegistryLengthFromURL("http://nowhere.foo", | 306 EXPECT_EQ(0U, GetRegistryLengthFromURL("http://nowhere.foo", |
292 EXCLUDE_UNKNOWN_REGISTRIES)); | 307 EXCLUDE_UNKNOWN_REGISTRIES)); |
293 EXPECT_EQ(3U, GetRegistryLengthFromURL("http://nowhere.foo", | 308 EXPECT_EQ(3U, GetRegistryLengthFromURL("http://nowhere.foo", |
294 INCLUDE_UNKNOWN_REGISTRIES)); | 309 INCLUDE_UNKNOWN_REGISTRIES)); |
295 } | 310 } |
296 | 311 |
297 TEST_F(RegistryControlledDomainTest, TestPrivateRegistryHandling) { | 312 TEST_F(RegistryControlledDomainTest, TestPrivateRegistryHandling) { |
298 UseDomainData(Perfect_Hash_Test1::FindDomain, Perfect_Hash_Test1_stringpool); | 313 UseDomainData(test1::kDafsa); |
299 | 314 |
300 // Testing the same dataset for INCLUDE_PRIVATE_REGISTRIES and | 315 // Testing the same dataset for INCLUDE_PRIVATE_REGISTRIES and |
301 // EXCLUDE_PRIVATE_REGISTRIES arguments. | 316 // EXCLUDE_PRIVATE_REGISTRIES arguments. |
302 // For the domain data used for this test, the private registries are | 317 // For the domain data used for this test, the private registries are |
303 // 'priv.no' and 'private'. | 318 // 'priv.no' and 'private'. |
304 | 319 |
305 // Non-private registries. | 320 // Non-private registries. |
306 EXPECT_EQ(2U, GetRegistryLengthFromURL("http://priv.no", | 321 EXPECT_EQ(2U, GetRegistryLengthFromURL("http://priv.no", |
307 EXCLUDE_UNKNOWN_REGISTRIES)); | 322 EXCLUDE_UNKNOWN_REGISTRIES)); |
308 EXPECT_EQ(2U, GetRegistryLengthFromURL("http://foo.priv.no", | 323 EXPECT_EQ(2U, GetRegistryLengthFromURL("http://foo.priv.no", |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
340 GetRegistryLengthFromURLIncludingPrivate("http://foo.private", | 355 GetRegistryLengthFromURLIncludingPrivate("http://foo.private", |
341 EXCLUDE_UNKNOWN_REGISTRIES)); | 356 EXCLUDE_UNKNOWN_REGISTRIES)); |
342 EXPECT_EQ(0U, | 357 EXPECT_EQ(0U, |
343 GetRegistryLengthFromURLIncludingPrivate("http://private", | 358 GetRegistryLengthFromURLIncludingPrivate("http://private", |
344 INCLUDE_UNKNOWN_REGISTRIES)); | 359 INCLUDE_UNKNOWN_REGISTRIES)); |
345 EXPECT_EQ(7U, | 360 EXPECT_EQ(7U, |
346 GetRegistryLengthFromURLIncludingPrivate("http://foo.private", | 361 GetRegistryLengthFromURLIncludingPrivate("http://foo.private", |
347 INCLUDE_UNKNOWN_REGISTRIES)); | 362 INCLUDE_UNKNOWN_REGISTRIES)); |
348 } | 363 } |
349 | 364 |
365 TEST_F(RegistryControlledDomainTest, TestDafsaTwoByteOffsets) { | |
Ryan Sleevi
2014/04/23 01:52:18
Can you add comments to each of these tests indica
| |
366 UseDomainData(test3::kDafsa); | |
350 | 367 |
368 const char* key0 = "a.b.6____________________________________________________" | |
369 "________________________________________________6"; | |
370 const char* key1 = "a.b.7____________________________________________________" | |
371 "________________________________________________7"; | |
372 const char* key2 = "a.b.a____________________________________________________" | |
373 "________________________________________________8"; | |
374 | |
375 EXPECT_EQ(102U, GetRegistryLengthFromHost(key0, EXCLUDE_UNKNOWN_REGISTRIES)); | |
376 EXPECT_EQ(0U, GetRegistryLengthFromHost(key1, EXCLUDE_UNKNOWN_REGISTRIES)); | |
377 EXPECT_EQ(102U, GetRegistryLengthFromHostIncludingPrivate( | |
378 key1, EXCLUDE_UNKNOWN_REGISTRIES)); | |
379 EXPECT_EQ(0U, GetRegistryLengthFromHost(key2, EXCLUDE_UNKNOWN_REGISTRIES)); | |
380 } | |
381 | |
382 TEST_F(RegistryControlledDomainTest, TestDafsaThreeByteOffsets) { | |
383 UseDomainData(test4::kDafsa); | |
384 | |
385 const char* key0 = "a.b.Z6___________________________________________________" | |
386 "_________________________________________________Z6"; | |
387 const char* key1 = "a.b.Z7___________________________________________________" | |
388 "_________________________________________________Z7"; | |
389 const char* key2 = "a.b.Za___________________________________________________" | |
390 "_________________________________________________Z8"; | |
391 | |
392 EXPECT_EQ(104U, GetRegistryLengthFromHost(key0, EXCLUDE_UNKNOWN_REGISTRIES)); | |
393 EXPECT_EQ(0U, GetRegistryLengthFromHost(key1, EXCLUDE_UNKNOWN_REGISTRIES)); | |
394 EXPECT_EQ(104U, GetRegistryLengthFromHostIncludingPrivate( | |
395 key1, EXCLUDE_UNKNOWN_REGISTRIES)); | |
396 EXPECT_EQ(0U, GetRegistryLengthFromHost(key2, EXCLUDE_UNKNOWN_REGISTRIES)); | |
397 } | |
398 | |
399 TEST_F(RegistryControlledDomainTest, TestDafsaJoinedPrefixes) { | |
400 UseDomainData(test5::kDafsa); | |
401 | |
402 const char* key0 = "a.b.ai"; | |
403 const char* key1 = "a.b.bj"; | |
404 const char* key2 = "a.b.aak"; | |
405 const char* key3 = "a.b.bbl"; | |
406 const char* key4 = "a.b.aaa"; | |
407 const char* key5 = "a.b.bbb"; | |
408 const char* key6 = "a.b.aaaam"; | |
409 const char* key7 = "a.b.bbbbn"; | |
410 | |
411 EXPECT_EQ(2U, GetRegistryLengthFromHost(key0, EXCLUDE_UNKNOWN_REGISTRIES)); | |
412 EXPECT_EQ(0U, GetRegistryLengthFromHost(key1, EXCLUDE_UNKNOWN_REGISTRIES)); | |
413 EXPECT_EQ(2U, GetRegistryLengthFromHostIncludingPrivate( | |
414 key1, EXCLUDE_UNKNOWN_REGISTRIES)); | |
415 EXPECT_EQ(3U, GetRegistryLengthFromHost(key2, EXCLUDE_UNKNOWN_REGISTRIES)); | |
416 EXPECT_EQ(0U, GetRegistryLengthFromHost(key3, EXCLUDE_UNKNOWN_REGISTRIES)); | |
417 EXPECT_EQ(3U, GetRegistryLengthFromHostIncludingPrivate( | |
418 key3, EXCLUDE_UNKNOWN_REGISTRIES)); | |
419 EXPECT_EQ(0U, GetRegistryLengthFromHostIncludingPrivate( | |
420 key4, EXCLUDE_UNKNOWN_REGISTRIES)); | |
421 EXPECT_EQ(0U, GetRegistryLengthFromHostIncludingPrivate( | |
422 key5, EXCLUDE_UNKNOWN_REGISTRIES)); | |
423 EXPECT_EQ(5U, GetRegistryLengthFromHost(key6, EXCLUDE_UNKNOWN_REGISTRIES)); | |
424 EXPECT_EQ(5U, GetRegistryLengthFromHost(key7, EXCLUDE_UNKNOWN_REGISTRIES)); | |
425 } | |
426 | |
427 TEST_F(RegistryControlledDomainTest, TestDafsaJoinedSuffixes) { | |
428 UseDomainData(test6::kDafsa); | |
429 | |
430 const char* key0 = "a.b.ia"; | |
431 const char* key1 = "a.b.jb"; | |
432 const char* key2 = "a.b.kaa"; | |
433 const char* key3 = "a.b.lbb"; | |
434 const char* key4 = "a.b.aaa"; | |
435 const char* key5 = "a.b.bbb"; | |
436 const char* key6 = "a.b.maaaa"; | |
437 const char* key7 = "a.b.nbbbb"; | |
438 | |
439 EXPECT_EQ(2U, GetRegistryLengthFromHost(key0, EXCLUDE_UNKNOWN_REGISTRIES)); | |
440 EXPECT_EQ(0U, GetRegistryLengthFromHost(key1, EXCLUDE_UNKNOWN_REGISTRIES)); | |
441 EXPECT_EQ(2U, GetRegistryLengthFromHostIncludingPrivate( | |
442 key1, EXCLUDE_UNKNOWN_REGISTRIES)); | |
443 EXPECT_EQ(3U, GetRegistryLengthFromHost(key2, EXCLUDE_UNKNOWN_REGISTRIES)); | |
444 EXPECT_EQ(0U, GetRegistryLengthFromHost(key3, EXCLUDE_UNKNOWN_REGISTRIES)); | |
445 EXPECT_EQ(3U, GetRegistryLengthFromHostIncludingPrivate( | |
446 key3, EXCLUDE_UNKNOWN_REGISTRIES)); | |
447 EXPECT_EQ(0U, GetRegistryLengthFromHostIncludingPrivate( | |
448 key4, EXCLUDE_UNKNOWN_REGISTRIES)); | |
449 EXPECT_EQ(0U, GetRegistryLengthFromHostIncludingPrivate( | |
450 key5, EXCLUDE_UNKNOWN_REGISTRIES)); | |
451 EXPECT_EQ(5U, GetRegistryLengthFromHost(key6, EXCLUDE_UNKNOWN_REGISTRIES)); | |
452 EXPECT_EQ(5U, GetRegistryLengthFromHost(key7, EXCLUDE_UNKNOWN_REGISTRIES)); | |
453 } | |
351 } // namespace registry_controlled_domains | 454 } // namespace registry_controlled_domains |
352 } // namespace net | 455 } // namespace net |
OLD | NEW |