Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(96)

Side by Side Diff: net/base/registry_controlled_domains/registry_controlled_domain_unittest.cc

Issue 197183002: Reduce footprint of registry controlled domain table (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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) {
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 void UseDomainData(const unsigned char* graph) { SetFindDomainGraph(graph); }
64 SetFindDomainFunctionAndStringPoolForTesting(function, stringpool);
65 }
66 80
67 virtual void TearDown() { 81 virtual void TearDown() { SetFindDomainGraph(0); }
68 SetFindDomainFunctionAndStringPoolForTesting(NULL, NULL);
69 }
70 }; 82 };
71 83
72 TEST_F(RegistryControlledDomainTest, TestGetDomainAndRegistry) { 84 TEST_F(RegistryControlledDomainTest, TestGetDomainAndRegistry) {
73 UseDomainData(Perfect_Hash_Test1::FindDomain, Perfect_Hash_Test1_stringpool); 85 UseDomainData(test1::kDafsa);
74 86
75 // Test GURL version of GetDomainAndRegistry(). 87 // Test GURL version of GetDomainAndRegistry().
76 EXPECT_EQ("baz.jp", GetDomainFromURL("http://a.baz.jp/file.html")); // 1 88 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 89 EXPECT_EQ("baz.jp.", GetDomainFromURL("http://a.baz.jp./file.html")); // 1
78 EXPECT_EQ("", GetDomainFromURL("http://ac.jp")); // 2 90 EXPECT_EQ("", GetDomainFromURL("http://ac.jp")); // 2
79 EXPECT_EQ("", GetDomainFromURL("http://a.bar.jp")); // 3 91 EXPECT_EQ("", GetDomainFromURL("http://a.bar.jp")); // 3
80 EXPECT_EQ("", GetDomainFromURL("http://bar.jp")); // 3 92 EXPECT_EQ("", GetDomainFromURL("http://bar.jp")); // 3
81 EXPECT_EQ("", GetDomainFromURL("http://baz.bar.jp")); // 3 4 93 EXPECT_EQ("", GetDomainFromURL("http://baz.bar.jp")); // 3 4
82 EXPECT_EQ("a.b.baz.bar.jp", GetDomainFromURL("http://a.b.baz.bar.jp")); 94 EXPECT_EQ("a.b.baz.bar.jp", GetDomainFromURL("http://a.b.baz.bar.jp"));
83 // 4 95 // 4
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
122 134
123 EXPECT_EQ("", GetDomainFromHost(std::string())); 135 EXPECT_EQ("", GetDomainFromHost(std::string()));
124 EXPECT_EQ("", GetDomainFromHost("foo.com..")); 136 EXPECT_EQ("", GetDomainFromHost("foo.com.."));
125 EXPECT_EQ("", GetDomainFromHost("...")); 137 EXPECT_EQ("", GetDomainFromHost("..."));
126 EXPECT_EQ("", GetDomainFromHost("192.168.0.1")); 138 EXPECT_EQ("", GetDomainFromHost("192.168.0.1"));
127 EXPECT_EQ("", GetDomainFromHost("localhost.")); 139 EXPECT_EQ("", GetDomainFromHost("localhost."));
128 EXPECT_EQ("", GetDomainFromHost(".localhost.")); 140 EXPECT_EQ("", GetDomainFromHost(".localhost."));
129 } 141 }
130 142
131 TEST_F(RegistryControlledDomainTest, TestGetRegistryLength) { 143 TEST_F(RegistryControlledDomainTest, TestGetRegistryLength) {
132 UseDomainData(Perfect_Hash_Test1::FindDomain, Perfect_Hash_Test1_stringpool); 144 UseDomainData(test1::kDafsa);
133 145
134 // Test GURL version of GetRegistryLength(). 146 // Test GURL version of GetRegistryLength().
135 EXPECT_EQ(2U, GetRegistryLengthFromURL("http://a.baz.jp/file.html", 147 EXPECT_EQ(2U, GetRegistryLengthFromURL("http://a.baz.jp/file.html",
136 EXCLUDE_UNKNOWN_REGISTRIES)); // 1 148 EXCLUDE_UNKNOWN_REGISTRIES)); // 1
137 EXPECT_EQ(3U, GetRegistryLengthFromURL("http://a.baz.jp./file.html", 149 EXPECT_EQ(3U, GetRegistryLengthFromURL("http://a.baz.jp./file.html",
138 EXCLUDE_UNKNOWN_REGISTRIES)); // 1 150 EXCLUDE_UNKNOWN_REGISTRIES)); // 1
139 EXPECT_EQ(0U, GetRegistryLengthFromURL("http://ac.jp", 151 EXPECT_EQ(0U, GetRegistryLengthFromURL("http://ac.jp",
140 EXCLUDE_UNKNOWN_REGISTRIES)); // 2 152 EXCLUDE_UNKNOWN_REGISTRIES)); // 2
141 EXPECT_EQ(0U, GetRegistryLengthFromURL("http://a.bar.jp", 153 EXPECT_EQ(0U, GetRegistryLengthFromURL("http://a.bar.jp",
142 EXCLUDE_UNKNOWN_REGISTRIES)); // 3 154 EXCLUDE_UNKNOWN_REGISTRIES)); // 3
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after
241 EXCLUDE_UNKNOWN_REGISTRIES)); 253 EXCLUDE_UNKNOWN_REGISTRIES));
242 EXPECT_EQ(0U, GetRegistryLengthFromHost("localhost", 254 EXPECT_EQ(0U, GetRegistryLengthFromHost("localhost",
243 INCLUDE_UNKNOWN_REGISTRIES)); 255 INCLUDE_UNKNOWN_REGISTRIES));
244 EXPECT_EQ(0U, GetRegistryLengthFromHost("localhost.", 256 EXPECT_EQ(0U, GetRegistryLengthFromHost("localhost.",
245 EXCLUDE_UNKNOWN_REGISTRIES)); 257 EXCLUDE_UNKNOWN_REGISTRIES));
246 EXPECT_EQ(0U, GetRegistryLengthFromHost("localhost.", 258 EXPECT_EQ(0U, GetRegistryLengthFromHost("localhost.",
247 INCLUDE_UNKNOWN_REGISTRIES)); 259 INCLUDE_UNKNOWN_REGISTRIES));
248 } 260 }
249 261
250 TEST_F(RegistryControlledDomainTest, TestSameDomainOrHost) { 262 TEST_F(RegistryControlledDomainTest, TestSameDomainOrHost) {
251 UseDomainData(Perfect_Hash_Test2::FindDomain, Perfect_Hash_Test2_stringpool); 263 UseDomainData(test2::kDafsa);
252 264
253 EXPECT_TRUE(CompareDomains("http://a.b.bar.jp/file.html", 265 EXPECT_TRUE(CompareDomains("http://a.b.bar.jp/file.html",
254 "http://a.b.bar.jp/file.html")); // b.bar.jp 266 "http://a.b.bar.jp/file.html")); // b.bar.jp
255 EXPECT_TRUE(CompareDomains("http://a.b.bar.jp/file.html", 267 EXPECT_TRUE(CompareDomains("http://a.b.bar.jp/file.html",
256 "http://b.b.bar.jp/file.html")); // b.bar.jp 268 "http://b.b.bar.jp/file.html")); // b.bar.jp
257 EXPECT_FALSE(CompareDomains("http://a.foo.jp/file.html", // foo.jp 269 EXPECT_FALSE(CompareDomains("http://a.foo.jp/file.html", // foo.jp
258 "http://a.not.jp/file.html")); // not.jp 270 "http://a.not.jp/file.html")); // not.jp
259 EXPECT_FALSE(CompareDomains("http://a.foo.jp/file.html", // foo.jp 271 EXPECT_FALSE(CompareDomains("http://a.foo.jp/file.html", // foo.jp
260 "http://a.foo.jp./file.html")); // foo.jp. 272 "http://a.foo.jp./file.html")); // foo.jp.
261 EXPECT_FALSE(CompareDomains("http://a.com/file.html", // a.com 273 EXPECT_FALSE(CompareDomains("http://a.com/file.html", // a.com
(...skipping 26 matching lines...) Expand all
288 EXCLUDE_UNKNOWN_REGISTRIES)); 300 EXCLUDE_UNKNOWN_REGISTRIES));
289 EXPECT_EQ(3U, GetRegistryLengthFromURL("http://ferretcentral.org", 301 EXPECT_EQ(3U, GetRegistryLengthFromURL("http://ferretcentral.org",
290 EXCLUDE_UNKNOWN_REGISTRIES)); 302 EXCLUDE_UNKNOWN_REGISTRIES));
291 EXPECT_EQ(0U, GetRegistryLengthFromURL("http://nowhere.foo", 303 EXPECT_EQ(0U, GetRegistryLengthFromURL("http://nowhere.foo",
292 EXCLUDE_UNKNOWN_REGISTRIES)); 304 EXCLUDE_UNKNOWN_REGISTRIES));
293 EXPECT_EQ(3U, GetRegistryLengthFromURL("http://nowhere.foo", 305 EXPECT_EQ(3U, GetRegistryLengthFromURL("http://nowhere.foo",
294 INCLUDE_UNKNOWN_REGISTRIES)); 306 INCLUDE_UNKNOWN_REGISTRIES));
295 } 307 }
296 308
297 TEST_F(RegistryControlledDomainTest, TestPrivateRegistryHandling) { 309 TEST_F(RegistryControlledDomainTest, TestPrivateRegistryHandling) {
298 UseDomainData(Perfect_Hash_Test1::FindDomain, Perfect_Hash_Test1_stringpool); 310 UseDomainData(test1::kDafsa);
299 311
300 // Testing the same dataset for INCLUDE_PRIVATE_REGISTRIES and 312 // Testing the same dataset for INCLUDE_PRIVATE_REGISTRIES and
301 // EXCLUDE_PRIVATE_REGISTRIES arguments. 313 // EXCLUDE_PRIVATE_REGISTRIES arguments.
302 // For the domain data used for this test, the private registries are 314 // For the domain data used for this test, the private registries are
303 // 'priv.no' and 'private'. 315 // 'priv.no' and 'private'.
304 316
305 // Non-private registries. 317 // Non-private registries.
306 EXPECT_EQ(2U, GetRegistryLengthFromURL("http://priv.no", 318 EXPECT_EQ(2U, GetRegistryLengthFromURL("http://priv.no",
307 EXCLUDE_UNKNOWN_REGISTRIES)); 319 EXCLUDE_UNKNOWN_REGISTRIES));
308 EXPECT_EQ(2U, GetRegistryLengthFromURL("http://foo.priv.no", 320 EXPECT_EQ(2U, GetRegistryLengthFromURL("http://foo.priv.no",
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
340 GetRegistryLengthFromURLIncludingPrivate("http://foo.private", 352 GetRegistryLengthFromURLIncludingPrivate("http://foo.private",
341 EXCLUDE_UNKNOWN_REGISTRIES)); 353 EXCLUDE_UNKNOWN_REGISTRIES));
342 EXPECT_EQ(0U, 354 EXPECT_EQ(0U,
343 GetRegistryLengthFromURLIncludingPrivate("http://private", 355 GetRegistryLengthFromURLIncludingPrivate("http://private",
344 INCLUDE_UNKNOWN_REGISTRIES)); 356 INCLUDE_UNKNOWN_REGISTRIES));
345 EXPECT_EQ(7U, 357 EXPECT_EQ(7U,
346 GetRegistryLengthFromURLIncludingPrivate("http://foo.private", 358 GetRegistryLengthFromURLIncludingPrivate("http://foo.private",
347 INCLUDE_UNKNOWN_REGISTRIES)); 359 INCLUDE_UNKNOWN_REGISTRIES));
348 } 360 }
349 361
362 TEST_F(RegistryControlledDomainTest, TestDafsaTwoByteOffsets) {
363 UseDomainData(test3::kDafsa);
350 364
365 const char* key0 = "a.b.6____________________________________________________"
366 "________________________________________________6";
367 const char* key1 = "a.b.7____________________________________________________"
368 "________________________________________________7";
369 const char* key2 = "a.b.a____________________________________________________"
370 "________________________________________________8";
371
372 EXPECT_EQ(102U, GetRegistryLengthFromHost(key0, EXCLUDE_UNKNOWN_REGISTRIES));
373 EXPECT_EQ(0U, GetRegistryLengthFromHost(key1, EXCLUDE_UNKNOWN_REGISTRIES));
374 EXPECT_EQ(102U, GetRegistryLengthFromHostIncludingPrivate(
375 key1, EXCLUDE_UNKNOWN_REGISTRIES));
376 EXPECT_EQ(0U, GetRegistryLengthFromHost(key2, EXCLUDE_UNKNOWN_REGISTRIES));
377 }
378
379 TEST_F(RegistryControlledDomainTest, TestDafsaThreeByteOffsets) {
380 UseDomainData(test4::kDafsa);
381
382 const char* key0 = "a.b.Z6___________________________________________________"
383 "_________________________________________________Z6";
384 const char* key1 = "a.b.Z7___________________________________________________"
385 "_________________________________________________Z7";
386 const char* key2 = "a.b.Za___________________________________________________"
387 "_________________________________________________Z8";
388
389 EXPECT_EQ(104U, GetRegistryLengthFromHost(key0, EXCLUDE_UNKNOWN_REGISTRIES));
390 EXPECT_EQ(0U, GetRegistryLengthFromHost(key1, EXCLUDE_UNKNOWN_REGISTRIES));
391 EXPECT_EQ(104U, GetRegistryLengthFromHostIncludingPrivate(
392 key1, EXCLUDE_UNKNOWN_REGISTRIES));
393 EXPECT_EQ(0U, GetRegistryLengthFromHost(key2, EXCLUDE_UNKNOWN_REGISTRIES));
394 }
395
396 TEST_F(RegistryControlledDomainTest, TestDafsaJoinedPrefixes) {
397 UseDomainData(test5::kDafsa);
398
399 const char* key0 = "a.b.ai";
400 const char* key1 = "a.b.bj";
401 const char* key2 = "a.b.aak";
402 const char* key3 = "a.b.bbl";
403 const char* key4 = "a.b.aaa";
404 const char* key5 = "a.b.bbb";
405 const char* key6 = "a.b.aaaam";
406 const char* key7 = "a.b.bbbbn";
407
408 EXPECT_EQ(2U, GetRegistryLengthFromHost(key0, EXCLUDE_UNKNOWN_REGISTRIES));
409 EXPECT_EQ(0U, GetRegistryLengthFromHost(key1, EXCLUDE_UNKNOWN_REGISTRIES));
410 EXPECT_EQ(2U, GetRegistryLengthFromHostIncludingPrivate(
411 key1, EXCLUDE_UNKNOWN_REGISTRIES));
412 EXPECT_EQ(3U, GetRegistryLengthFromHost(key2, EXCLUDE_UNKNOWN_REGISTRIES));
413 EXPECT_EQ(0U, GetRegistryLengthFromHost(key3, EXCLUDE_UNKNOWN_REGISTRIES));
414 EXPECT_EQ(3U, GetRegistryLengthFromHostIncludingPrivate(
415 key3, EXCLUDE_UNKNOWN_REGISTRIES));
416 EXPECT_EQ(0U, GetRegistryLengthFromHostIncludingPrivate(
417 key4, EXCLUDE_UNKNOWN_REGISTRIES));
418 EXPECT_EQ(0U, GetRegistryLengthFromHostIncludingPrivate(
419 key5, EXCLUDE_UNKNOWN_REGISTRIES));
420 EXPECT_EQ(5U, GetRegistryLengthFromHost(key6, EXCLUDE_UNKNOWN_REGISTRIES));
421 EXPECT_EQ(5U, GetRegistryLengthFromHost(key7, EXCLUDE_UNKNOWN_REGISTRIES));
422 }
423
424 TEST_F(RegistryControlledDomainTest, TestDafsaJoinedSuffixes) {
425 UseDomainData(test6::kDafsa);
426
427 const char* key0 = "a.b.ia";
428 const char* key1 = "a.b.jb";
429 const char* key2 = "a.b.kaa";
430 const char* key3 = "a.b.lbb";
431 const char* key4 = "a.b.aaa";
432 const char* key5 = "a.b.bbb";
433 const char* key6 = "a.b.maaaa";
434 const char* key7 = "a.b.nbbbb";
435
436 EXPECT_EQ(2U, GetRegistryLengthFromHost(key0, EXCLUDE_UNKNOWN_REGISTRIES));
437 EXPECT_EQ(0U, GetRegistryLengthFromHost(key1, EXCLUDE_UNKNOWN_REGISTRIES));
438 EXPECT_EQ(2U, GetRegistryLengthFromHostIncludingPrivate(
439 key1, EXCLUDE_UNKNOWN_REGISTRIES));
440 EXPECT_EQ(3U, GetRegistryLengthFromHost(key2, EXCLUDE_UNKNOWN_REGISTRIES));
441 EXPECT_EQ(0U, GetRegistryLengthFromHost(key3, EXCLUDE_UNKNOWN_REGISTRIES));
442 EXPECT_EQ(3U, GetRegistryLengthFromHostIncludingPrivate(
443 key3, EXCLUDE_UNKNOWN_REGISTRIES));
444 EXPECT_EQ(0U, GetRegistryLengthFromHostIncludingPrivate(
445 key4, EXCLUDE_UNKNOWN_REGISTRIES));
446 EXPECT_EQ(0U, GetRegistryLengthFromHostIncludingPrivate(
447 key5, EXCLUDE_UNKNOWN_REGISTRIES));
448 EXPECT_EQ(5U, GetRegistryLengthFromHost(key6, EXCLUDE_UNKNOWN_REGISTRIES));
449 EXPECT_EQ(5U, GetRegistryLengthFromHost(key7, EXCLUDE_UNKNOWN_REGISTRIES));
450 }
351 } // namespace registry_controlled_domains 451 } // namespace registry_controlled_domains
352 } // namespace net 452 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698