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 "base/strings/utf_string_conversions.h" |
5 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" | 6 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" |
6 #include "testing/gtest/include/gtest/gtest.h" | 7 #include "testing/gtest/include/gtest/gtest.h" |
7 #include "url/gurl.h" | 8 #include "url/gurl.h" |
8 #include "url/origin.h" | 9 #include "url/origin.h" |
| 10 #include "url/url_features.h" |
9 | 11 |
10 namespace { | 12 namespace { |
| 13 |
11 namespace test1 { | 14 namespace test1 { |
12 #include "net/base/registry_controlled_domains/effective_tld_names_unittest1-inc
.cc" | 15 #include "net/base/registry_controlled_domains/effective_tld_names_unittest1-inc
.cc" |
13 } | 16 } |
14 namespace test2 { | 17 namespace test2 { |
15 #include "net/base/registry_controlled_domains/effective_tld_names_unittest2-inc
.cc" | 18 #include "net/base/registry_controlled_domains/effective_tld_names_unittest2-inc
.cc" |
16 } | 19 } |
17 namespace test3 { | 20 namespace test3 { |
18 #include "net/base/registry_controlled_domains/effective_tld_names_unittest3-inc
.cc" | 21 #include "net/base/registry_controlled_domains/effective_tld_names_unittest3-inc
.cc" |
19 } | 22 } |
20 namespace test4 { | 23 namespace test4 { |
21 #include "net/base/registry_controlled_domains/effective_tld_names_unittest4-inc
.cc" | 24 #include "net/base/registry_controlled_domains/effective_tld_names_unittest4-inc
.cc" |
22 } | 25 } |
23 namespace test5 { | 26 namespace test5 { |
24 #include "net/base/registry_controlled_domains/effective_tld_names_unittest5-inc
.cc" | 27 #include "net/base/registry_controlled_domains/effective_tld_names_unittest5-inc
.cc" |
25 } | 28 } |
26 namespace test6 { | 29 namespace test6 { |
27 #include "net/base/registry_controlled_domains/effective_tld_names_unittest6-inc
.cc" | 30 #include "net/base/registry_controlled_domains/effective_tld_names_unittest6-inc
.cc" |
28 } | 31 } |
| 32 |
29 } // namespace | 33 } // namespace |
30 | 34 |
31 namespace net { | 35 namespace net { |
32 namespace registry_controlled_domains { | 36 namespace registry_controlled_domains { |
| 37 |
33 namespace { | 38 namespace { |
34 | 39 |
35 std::string GetDomainFromURL(const std::string& url) { | 40 std::string GetDomainFromURL(const std::string& url) { |
36 return GetDomainAndRegistry(GURL(url), EXCLUDE_PRIVATE_REGISTRIES); | 41 return GetDomainAndRegistry(GURL(url), EXCLUDE_PRIVATE_REGISTRIES); |
37 } | 42 } |
38 | 43 |
39 std::string GetDomainFromHost(const std::string& host) { | 44 std::string GetDomainFromHost(const std::string& host) { |
40 return GetDomainAndRegistry(host, EXCLUDE_PRIVATE_REGISTRIES); | 45 return GetDomainAndRegistry(host, EXCLUDE_PRIVATE_REGISTRIES); |
41 } | 46 } |
42 | 47 |
43 size_t GetRegistryLengthFromURL( | 48 size_t GetRegistryLengthFromURL( |
44 const std::string& url, | 49 const std::string& url, |
45 UnknownRegistryFilter unknown_filter) { | 50 UnknownRegistryFilter unknown_filter) { |
46 return GetRegistryLength(GURL(url), | 51 return GetRegistryLength(GURL(url), |
47 unknown_filter, | 52 unknown_filter, |
48 EXCLUDE_PRIVATE_REGISTRIES); | 53 EXCLUDE_PRIVATE_REGISTRIES); |
49 } | 54 } |
50 | 55 |
51 size_t GetRegistryLengthFromURLIncludingPrivate( | 56 size_t GetRegistryLengthFromURLIncludingPrivate( |
52 const std::string& url, | 57 const std::string& url, |
53 UnknownRegistryFilter unknown_filter) { | 58 UnknownRegistryFilter unknown_filter) { |
54 return GetRegistryLength(GURL(url), | 59 return GetRegistryLength(GURL(url), |
55 unknown_filter, | 60 unknown_filter, |
56 INCLUDE_PRIVATE_REGISTRIES); | 61 INCLUDE_PRIVATE_REGISTRIES); |
57 } | 62 } |
58 | 63 |
59 size_t GetRegistryLengthFromHost( | 64 size_t PermissiveGetHostRegistryLength(base::StringPiece host) { |
60 const std::string& host, | 65 return PermissiveGetHostRegistryLength(host, EXCLUDE_UNKNOWN_REGISTRIES, |
61 UnknownRegistryFilter unknown_filter) { | 66 EXCLUDE_PRIVATE_REGISTRIES); |
62 return GetRegistryLength(host, unknown_filter, EXCLUDE_PRIVATE_REGISTRIES); | |
63 } | 67 } |
64 | 68 |
65 size_t GetRegistryLengthFromHostIncludingPrivate( | 69 size_t PermissiveGetHostRegistryLength(base::StringPiece16 host) { |
66 const std::string& host, | 70 return PermissiveGetHostRegistryLength(host, EXCLUDE_UNKNOWN_REGISTRIES, |
67 UnknownRegistryFilter unknown_filter) { | 71 EXCLUDE_PRIVATE_REGISTRIES); |
68 return GetRegistryLength(host, unknown_filter, INCLUDE_PRIVATE_REGISTRIES); | 72 } |
| 73 |
| 74 size_t GetCanonicalHostRegistryLength(const std::string& host, |
| 75 UnknownRegistryFilter unknown_filter) { |
| 76 return GetCanonicalHostRegistryLength(host, unknown_filter, |
| 77 EXCLUDE_PRIVATE_REGISTRIES); |
| 78 } |
| 79 |
| 80 size_t GetCanonicalHostRegistryLengthIncludingPrivate(const std::string& host) { |
| 81 return GetCanonicalHostRegistryLength(host, EXCLUDE_UNKNOWN_REGISTRIES, |
| 82 INCLUDE_PRIVATE_REGISTRIES); |
69 } | 83 } |
70 | 84 |
71 } // namespace | 85 } // namespace |
72 | 86 |
73 class RegistryControlledDomainTest : public testing::Test { | 87 class RegistryControlledDomainTest : public testing::Test { |
74 protected: | 88 protected: |
75 template <typename Graph> | 89 template <typename Graph> |
76 void UseDomainData(const Graph& graph) { | 90 void UseDomainData(const Graph& graph) { |
77 SetFindDomainGraph(graph, sizeof(Graph)); | 91 SetFindDomainGraph(graph, sizeof(Graph)); |
78 } | 92 } |
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
209 EXPECT_EQ(0U, GetRegistryLengthFromURL("http://localhost.", | 223 EXPECT_EQ(0U, GetRegistryLengthFromURL("http://localhost.", |
210 EXCLUDE_UNKNOWN_REGISTRIES)); | 224 EXCLUDE_UNKNOWN_REGISTRIES)); |
211 EXPECT_EQ(0U, GetRegistryLengthFromURL("http://localhost.", | 225 EXPECT_EQ(0U, GetRegistryLengthFromURL("http://localhost.", |
212 INCLUDE_UNKNOWN_REGISTRIES)); | 226 INCLUDE_UNKNOWN_REGISTRIES)); |
213 EXPECT_EQ(0U, GetRegistryLengthFromURL("http:////Comment", | 227 EXPECT_EQ(0U, GetRegistryLengthFromURL("http:////Comment", |
214 EXCLUDE_UNKNOWN_REGISTRIES)); | 228 EXCLUDE_UNKNOWN_REGISTRIES)); |
215 | 229 |
216 // Test std::string version of GetRegistryLength(). Uses the same | 230 // Test std::string version of GetRegistryLength(). Uses the same |
217 // underpinnings as the GURL version, so this is really more of a check of | 231 // underpinnings as the GURL version, so this is really more of a check of |
218 // CanonicalizeHost(). | 232 // CanonicalizeHost(). |
219 EXPECT_EQ(2U, GetRegistryLengthFromHost("a.baz.jp", | 233 EXPECT_EQ(2U, GetCanonicalHostRegistryLength( |
220 EXCLUDE_UNKNOWN_REGISTRIES)); // 1 | 234 "a.baz.jp", EXCLUDE_UNKNOWN_REGISTRIES)); // 1 |
221 EXPECT_EQ(3U, GetRegistryLengthFromHost("a.baz.jp.", | 235 EXPECT_EQ(3U, GetCanonicalHostRegistryLength( |
222 EXCLUDE_UNKNOWN_REGISTRIES)); // 1 | 236 "a.baz.jp.", EXCLUDE_UNKNOWN_REGISTRIES)); // 1 |
223 EXPECT_EQ(0U, GetRegistryLengthFromHost("ac.jp", | 237 EXPECT_EQ(0U, GetCanonicalHostRegistryLength( |
224 EXCLUDE_UNKNOWN_REGISTRIES)); // 2 | 238 "ac.jp", EXCLUDE_UNKNOWN_REGISTRIES)); // 2 |
225 EXPECT_EQ(0U, GetRegistryLengthFromHost("a.bar.jp", | 239 EXPECT_EQ(0U, GetCanonicalHostRegistryLength( |
226 EXCLUDE_UNKNOWN_REGISTRIES)); // 3 | 240 "a.bar.jp", EXCLUDE_UNKNOWN_REGISTRIES)); // 3 |
227 EXPECT_EQ(0U, GetRegistryLengthFromHost("bar.jp", | 241 EXPECT_EQ(0U, GetCanonicalHostRegistryLength( |
228 EXCLUDE_UNKNOWN_REGISTRIES)); // 3 | 242 "bar.jp", EXCLUDE_UNKNOWN_REGISTRIES)); // 3 |
229 EXPECT_EQ(0U, GetRegistryLengthFromHost("baz.bar.jp", | 243 EXPECT_EQ(0U, GetCanonicalHostRegistryLength( |
230 EXCLUDE_UNKNOWN_REGISTRIES)); // 3 4 | 244 "baz.bar.jp", EXCLUDE_UNKNOWN_REGISTRIES)); // 3 4 |
231 EXPECT_EQ(12U, GetRegistryLengthFromHost("a.b.baz.bar.jp", | 245 EXPECT_EQ(12U, GetCanonicalHostRegistryLength( |
232 EXCLUDE_UNKNOWN_REGISTRIES)); // 4 | 246 "a.b.baz.bar.jp", EXCLUDE_UNKNOWN_REGISTRIES)); // 4 |
233 EXPECT_EQ(6U, GetRegistryLengthFromHost("baz.pref.bar.jp", | 247 EXPECT_EQ(6U, GetCanonicalHostRegistryLength( |
234 EXCLUDE_UNKNOWN_REGISTRIES)); // 5 | 248 "baz.pref.bar.jp", EXCLUDE_UNKNOWN_REGISTRIES)); // 5 |
235 EXPECT_EQ(11U, GetRegistryLengthFromHost("a.b.bar.baz.com", | 249 EXPECT_EQ(11U, GetCanonicalHostRegistryLength( |
236 EXCLUDE_UNKNOWN_REGISTRIES)); // 6 | 250 "a.b.bar.baz.com", EXCLUDE_UNKNOWN_REGISTRIES)); // 6 |
237 EXPECT_EQ(3U, GetRegistryLengthFromHost("a.d.c", | 251 EXPECT_EQ(3U, GetCanonicalHostRegistryLength( |
238 EXCLUDE_UNKNOWN_REGISTRIES)); // 7 | 252 "a.d.c", EXCLUDE_UNKNOWN_REGISTRIES)); // 7 |
239 EXPECT_EQ(3U, GetRegistryLengthFromHost(".a.d.c", | 253 EXPECT_EQ(3U, GetCanonicalHostRegistryLength( |
240 EXCLUDE_UNKNOWN_REGISTRIES)); // 7 | 254 ".a.d.c", EXCLUDE_UNKNOWN_REGISTRIES)); // 7 |
241 EXPECT_EQ(3U, GetRegistryLengthFromHost("..a.d.c", | 255 EXPECT_EQ(3U, GetCanonicalHostRegistryLength( |
242 EXCLUDE_UNKNOWN_REGISTRIES)); // 7 | 256 "..a.d.c", EXCLUDE_UNKNOWN_REGISTRIES)); // 7 |
243 EXPECT_EQ(1U, GetRegistryLengthFromHost("a.b.c", | 257 EXPECT_EQ(1U, GetCanonicalHostRegistryLength( |
244 EXCLUDE_UNKNOWN_REGISTRIES)); // 7 8 | 258 "a.b.c", EXCLUDE_UNKNOWN_REGISTRIES)); // 7 8 |
245 EXPECT_EQ(0U, GetRegistryLengthFromHost("baz.com", | 259 EXPECT_EQ(0U, GetCanonicalHostRegistryLength( |
246 EXCLUDE_UNKNOWN_REGISTRIES)); // none | 260 "baz.com", EXCLUDE_UNKNOWN_REGISTRIES)); // none |
247 EXPECT_EQ(0U, GetRegistryLengthFromHost("baz.com.", | 261 EXPECT_EQ(0U, GetCanonicalHostRegistryLength( |
248 EXCLUDE_UNKNOWN_REGISTRIES)); // none | 262 "baz.com.", EXCLUDE_UNKNOWN_REGISTRIES)); // none |
249 EXPECT_EQ(3U, GetRegistryLengthFromHost("baz.com", | 263 EXPECT_EQ(3U, GetCanonicalHostRegistryLength( |
250 INCLUDE_UNKNOWN_REGISTRIES)); // none | 264 "baz.com", INCLUDE_UNKNOWN_REGISTRIES)); // none |
251 EXPECT_EQ(4U, GetRegistryLengthFromHost("baz.com.", | 265 EXPECT_EQ(4U, GetCanonicalHostRegistryLength( |
252 INCLUDE_UNKNOWN_REGISTRIES)); // none | 266 "baz.com.", INCLUDE_UNKNOWN_REGISTRIES)); // none |
253 | 267 |
254 EXPECT_EQ(std::string::npos, | 268 EXPECT_EQ(std::string::npos, GetCanonicalHostRegistryLength( |
255 GetRegistryLengthFromHost(std::string(), EXCLUDE_UNKNOWN_REGISTRIES)); | 269 std::string(), EXCLUDE_UNKNOWN_REGISTRIES)); |
256 EXPECT_EQ(0U, GetRegistryLengthFromHost("foo.com..", | 270 EXPECT_EQ(0U, GetCanonicalHostRegistryLength("foo.com..", |
257 EXCLUDE_UNKNOWN_REGISTRIES)); | 271 EXCLUDE_UNKNOWN_REGISTRIES)); |
258 EXPECT_EQ(0U, GetRegistryLengthFromHost("..", | 272 EXPECT_EQ(0U, |
259 EXCLUDE_UNKNOWN_REGISTRIES)); | 273 GetCanonicalHostRegistryLength("..", EXCLUDE_UNKNOWN_REGISTRIES)); |
260 EXPECT_EQ(0U, GetRegistryLengthFromHost("192.168.0.1", | 274 EXPECT_EQ(0U, GetCanonicalHostRegistryLength("192.168.0.1", |
261 EXCLUDE_UNKNOWN_REGISTRIES)); | 275 EXCLUDE_UNKNOWN_REGISTRIES)); |
262 EXPECT_EQ(0U, GetRegistryLengthFromHost("localhost", | 276 EXPECT_EQ(0U, GetCanonicalHostRegistryLength("localhost", |
263 EXCLUDE_UNKNOWN_REGISTRIES)); | 277 EXCLUDE_UNKNOWN_REGISTRIES)); |
264 EXPECT_EQ(0U, GetRegistryLengthFromHost("localhost", | 278 EXPECT_EQ(0U, GetCanonicalHostRegistryLength("localhost", |
265 INCLUDE_UNKNOWN_REGISTRIES)); | 279 INCLUDE_UNKNOWN_REGISTRIES)); |
266 EXPECT_EQ(0U, GetRegistryLengthFromHost("localhost.", | 280 EXPECT_EQ(0U, GetCanonicalHostRegistryLength("localhost.", |
267 EXCLUDE_UNKNOWN_REGISTRIES)); | 281 EXCLUDE_UNKNOWN_REGISTRIES)); |
268 EXPECT_EQ(0U, GetRegistryLengthFromHost("localhost.", | 282 EXPECT_EQ(0U, GetCanonicalHostRegistryLength("localhost.", |
269 INCLUDE_UNKNOWN_REGISTRIES)); | 283 INCLUDE_UNKNOWN_REGISTRIES)); |
| 284 |
| 285 // IDN case. |
| 286 EXPECT_EQ(10U, GetCanonicalHostRegistryLength("foo.xn--fiqs8s", |
| 287 EXCLUDE_UNKNOWN_REGISTRIES)); |
| 288 } |
| 289 |
| 290 TEST_F(RegistryControlledDomainTest, HostHasRegistryControlledDomain) { |
| 291 UseDomainData(test1::kDafsa); |
| 292 |
| 293 // Invalid hosts. |
| 294 EXPECT_FALSE(HostHasRegistryControlledDomain( |
| 295 std::string(), EXCLUDE_UNKNOWN_REGISTRIES, EXCLUDE_PRIVATE_REGISTRIES)); |
| 296 EXPECT_FALSE(HostHasRegistryControlledDomain( |
| 297 "%00asdf", EXCLUDE_UNKNOWN_REGISTRIES, EXCLUDE_PRIVATE_REGISTRIES)); |
| 298 |
| 299 // Invalid host but valid R.C.D. |
| 300 EXPECT_TRUE(HostHasRegistryControlledDomain( |
| 301 "%00foo.jp", EXCLUDE_UNKNOWN_REGISTRIES, EXCLUDE_PRIVATE_REGISTRIES)); |
| 302 |
| 303 // Valid R.C.D. when canonicalized, even with an invalid prefix and an |
| 304 // escaped dot. |
| 305 EXPECT_TRUE(HostHasRegistryControlledDomain("%00foo.Google%2EjP", |
| 306 EXCLUDE_UNKNOWN_REGISTRIES, |
| 307 EXCLUDE_PRIVATE_REGISTRIES)); |
| 308 |
| 309 // Regular, no match. |
| 310 EXPECT_FALSE(HostHasRegistryControlledDomain( |
| 311 "bar.notatld", EXCLUDE_UNKNOWN_REGISTRIES, EXCLUDE_PRIVATE_REGISTRIES)); |
| 312 |
| 313 // Regular, match. |
| 314 EXPECT_TRUE(HostHasRegistryControlledDomain( |
| 315 "www.Google.Jp", EXCLUDE_UNKNOWN_REGISTRIES, EXCLUDE_PRIVATE_REGISTRIES)); |
270 } | 316 } |
271 | 317 |
272 TEST_F(RegistryControlledDomainTest, TestSameDomainOrHost) { | 318 TEST_F(RegistryControlledDomainTest, TestSameDomainOrHost) { |
273 UseDomainData(test2::kDafsa); | 319 UseDomainData(test2::kDafsa); |
274 | 320 |
275 EXPECT_TRUE(CompareDomains("http://a.b.bar.jp/file.html", | 321 EXPECT_TRUE(CompareDomains("http://a.b.bar.jp/file.html", |
276 "http://a.b.bar.jp/file.html")); // b.bar.jp | 322 "http://a.b.bar.jp/file.html")); // b.bar.jp |
277 EXPECT_TRUE(CompareDomains("http://a.b.bar.jp/file.html", | 323 EXPECT_TRUE(CompareDomains("http://a.b.bar.jp/file.html", |
278 "http://b.b.bar.jp/file.html")); // b.bar.jp | 324 "http://b.b.bar.jp/file.html")); // b.bar.jp |
279 EXPECT_FALSE(CompareDomains("http://a.foo.jp/file.html", // foo.jp | 325 EXPECT_FALSE(CompareDomains("http://a.foo.jp/file.html", // foo.jp |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
381 const char key0[] = | 427 const char key0[] = |
382 "a.b.6____________________________________________________" | 428 "a.b.6____________________________________________________" |
383 "________________________________________________6"; | 429 "________________________________________________6"; |
384 const char key1[] = | 430 const char key1[] = |
385 "a.b.7____________________________________________________" | 431 "a.b.7____________________________________________________" |
386 "________________________________________________7"; | 432 "________________________________________________7"; |
387 const char key2[] = | 433 const char key2[] = |
388 "a.b.a____________________________________________________" | 434 "a.b.a____________________________________________________" |
389 "________________________________________________8"; | 435 "________________________________________________8"; |
390 | 436 |
391 EXPECT_EQ(102U, GetRegistryLengthFromHost(key0, EXCLUDE_UNKNOWN_REGISTRIES)); | |
392 EXPECT_EQ(0U, GetRegistryLengthFromHost(key1, EXCLUDE_UNKNOWN_REGISTRIES)); | |
393 EXPECT_EQ(102U, | 437 EXPECT_EQ(102U, |
394 GetRegistryLengthFromHostIncludingPrivate( | 438 GetCanonicalHostRegistryLength(key0, EXCLUDE_UNKNOWN_REGISTRIES)); |
395 key1, EXCLUDE_UNKNOWN_REGISTRIES)); | 439 EXPECT_EQ(0U, |
396 EXPECT_EQ(0U, GetRegistryLengthFromHost(key2, EXCLUDE_UNKNOWN_REGISTRIES)); | 440 GetCanonicalHostRegistryLength(key1, EXCLUDE_UNKNOWN_REGISTRIES)); |
| 441 EXPECT_EQ(102U, GetCanonicalHostRegistryLengthIncludingPrivate(key1)); |
| 442 EXPECT_EQ(0U, |
| 443 GetCanonicalHostRegistryLength(key2, EXCLUDE_UNKNOWN_REGISTRIES)); |
397 } | 444 } |
398 | 445 |
399 TEST_F(RegistryControlledDomainTest, TestDafsaThreeByteOffsets) { | 446 TEST_F(RegistryControlledDomainTest, TestDafsaThreeByteOffsets) { |
400 UseDomainData(test4::kDafsa); | 447 UseDomainData(test4::kDafsa); |
401 | 448 |
402 // Testing to lookup keys in a DAFSA with three byte offsets. | 449 // Testing to lookup keys in a DAFSA with three byte offsets. |
403 // This DAFSA is constructed so that labels begin and end with unique | 450 // This DAFSA is constructed so that labels begin and end with unique |
404 // characters, which makes it impossible to merge labels. The byte array | 451 // characters, which makes it impossible to merge labels. The byte array |
405 // has a size of ~54k. A two byte offset can add at most add 8k to the | 452 // has a size of ~54k. A two byte offset can add at most add 8k to the |
406 // previous offset. Since we can skip only forward in memory, the nodes | 453 // previous offset. Since we can skip only forward in memory, the nodes |
407 // representing the return values must be located near the end of the byte | 454 // representing the return values must be located near the end of the byte |
408 // array. The probability that we can reach from an arbitrary inner node to | 455 // array. The probability that we can reach from an arbitrary inner node to |
409 // a return value without using a three byte offset is small (but not zero). | 456 // a return value without using a three byte offset is small (but not zero). |
410 // The test is repeated with some different keys and with a reasonable | 457 // The test is repeated with some different keys and with a reasonable |
411 // probability at least one of the tested paths has go over a three byte | 458 // probability at least one of the tested paths has go over a three byte |
412 // offset. | 459 // offset. |
413 | 460 |
414 const char key0[] = | 461 const char key0[] = |
415 "a.b.Z6___________________________________________________" | 462 "a.b.z6___________________________________________________" |
416 "_________________________________________________Z6"; | 463 "_________________________________________________z6"; |
417 const char key1[] = | 464 const char key1[] = |
418 "a.b.Z7___________________________________________________" | 465 "a.b.z7___________________________________________________" |
419 "_________________________________________________Z7"; | 466 "_________________________________________________z7"; |
420 const char key2[] = | 467 const char key2[] = |
421 "a.b.Za___________________________________________________" | 468 "a.b.za___________________________________________________" |
422 "_________________________________________________Z8"; | 469 "_________________________________________________z8"; |
423 | 470 |
424 EXPECT_EQ(104U, GetRegistryLengthFromHost(key0, EXCLUDE_UNKNOWN_REGISTRIES)); | |
425 EXPECT_EQ(0U, GetRegistryLengthFromHost(key1, EXCLUDE_UNKNOWN_REGISTRIES)); | |
426 EXPECT_EQ(104U, | 471 EXPECT_EQ(104U, |
427 GetRegistryLengthFromHostIncludingPrivate( | 472 GetCanonicalHostRegistryLength(key0, EXCLUDE_UNKNOWN_REGISTRIES)); |
428 key1, EXCLUDE_UNKNOWN_REGISTRIES)); | 473 EXPECT_EQ(0U, |
429 EXPECT_EQ(0U, GetRegistryLengthFromHost(key2, EXCLUDE_UNKNOWN_REGISTRIES)); | 474 GetCanonicalHostRegistryLength(key1, EXCLUDE_UNKNOWN_REGISTRIES)); |
| 475 EXPECT_EQ(104U, GetCanonicalHostRegistryLengthIncludingPrivate(key1)); |
| 476 EXPECT_EQ(0U, |
| 477 GetCanonicalHostRegistryLength(key2, EXCLUDE_UNKNOWN_REGISTRIES)); |
430 } | 478 } |
431 | 479 |
432 TEST_F(RegistryControlledDomainTest, TestDafsaJoinedPrefixes) { | 480 TEST_F(RegistryControlledDomainTest, TestDafsaJoinedPrefixes) { |
433 UseDomainData(test5::kDafsa); | 481 UseDomainData(test5::kDafsa); |
434 | 482 |
435 // Testing to lookup keys in a DAFSA with compressed prefixes. | 483 // Testing to lookup keys in a DAFSA with compressed prefixes. |
436 // This DAFSA is constructed from words with similar prefixes but distinct | 484 // This DAFSA is constructed from words with similar prefixes but distinct |
437 // suffixes. The DAFSA will then form a trie with the implicit source node | 485 // suffixes. The DAFSA will then form a trie with the implicit source node |
438 // as root. | 486 // as root. |
439 | 487 |
440 const char key0[] = "a.b.ai"; | 488 const char key0[] = "a.b.ai"; |
441 const char key1[] = "a.b.bj"; | 489 const char key1[] = "a.b.bj"; |
442 const char key2[] = "a.b.aak"; | 490 const char key2[] = "a.b.aak"; |
443 const char key3[] = "a.b.bbl"; | 491 const char key3[] = "a.b.bbl"; |
444 const char key4[] = "a.b.aaa"; | 492 const char key4[] = "a.b.aaa"; |
445 const char key5[] = "a.b.bbb"; | 493 const char key5[] = "a.b.bbb"; |
446 const char key6[] = "a.b.aaaam"; | 494 const char key6[] = "a.b.aaaam"; |
447 const char key7[] = "a.b.bbbbn"; | 495 const char key7[] = "a.b.bbbbn"; |
448 | 496 |
449 EXPECT_EQ(2U, GetRegistryLengthFromHost(key0, EXCLUDE_UNKNOWN_REGISTRIES)); | |
450 EXPECT_EQ(0U, GetRegistryLengthFromHost(key1, EXCLUDE_UNKNOWN_REGISTRIES)); | |
451 EXPECT_EQ(2U, | 497 EXPECT_EQ(2U, |
452 GetRegistryLengthFromHostIncludingPrivate( | 498 GetCanonicalHostRegistryLength(key0, EXCLUDE_UNKNOWN_REGISTRIES)); |
453 key1, EXCLUDE_UNKNOWN_REGISTRIES)); | 499 EXPECT_EQ(0U, |
454 EXPECT_EQ(3U, GetRegistryLengthFromHost(key2, EXCLUDE_UNKNOWN_REGISTRIES)); | 500 GetCanonicalHostRegistryLength(key1, EXCLUDE_UNKNOWN_REGISTRIES)); |
455 EXPECT_EQ(0U, GetRegistryLengthFromHost(key3, EXCLUDE_UNKNOWN_REGISTRIES)); | 501 EXPECT_EQ(2U, GetCanonicalHostRegistryLengthIncludingPrivate(key1)); |
456 EXPECT_EQ(3U, | 502 EXPECT_EQ(3U, |
457 GetRegistryLengthFromHostIncludingPrivate( | 503 GetCanonicalHostRegistryLength(key2, EXCLUDE_UNKNOWN_REGISTRIES)); |
458 key3, EXCLUDE_UNKNOWN_REGISTRIES)); | |
459 EXPECT_EQ(0U, | 504 EXPECT_EQ(0U, |
460 GetRegistryLengthFromHostIncludingPrivate( | 505 GetCanonicalHostRegistryLength(key3, EXCLUDE_UNKNOWN_REGISTRIES)); |
461 key4, EXCLUDE_UNKNOWN_REGISTRIES)); | 506 EXPECT_EQ(3U, GetCanonicalHostRegistryLengthIncludingPrivate(key3)); |
462 EXPECT_EQ(0U, | 507 EXPECT_EQ(0U, GetCanonicalHostRegistryLengthIncludingPrivate(key4)); |
463 GetRegistryLengthFromHostIncludingPrivate( | 508 EXPECT_EQ(0U, GetCanonicalHostRegistryLengthIncludingPrivate(key5)); |
464 key5, EXCLUDE_UNKNOWN_REGISTRIES)); | 509 EXPECT_EQ(5U, |
465 EXPECT_EQ(5U, GetRegistryLengthFromHost(key6, EXCLUDE_UNKNOWN_REGISTRIES)); | 510 GetCanonicalHostRegistryLength(key6, EXCLUDE_UNKNOWN_REGISTRIES)); |
466 EXPECT_EQ(5U, GetRegistryLengthFromHost(key7, EXCLUDE_UNKNOWN_REGISTRIES)); | 511 EXPECT_EQ(5U, |
| 512 GetCanonicalHostRegistryLength(key7, EXCLUDE_UNKNOWN_REGISTRIES)); |
467 } | 513 } |
468 | 514 |
469 TEST_F(RegistryControlledDomainTest, TestDafsaJoinedSuffixes) { | 515 TEST_F(RegistryControlledDomainTest, TestDafsaJoinedSuffixes) { |
470 UseDomainData(test6::kDafsa); | 516 UseDomainData(test6::kDafsa); |
471 | 517 |
472 // Testing to lookup keys in a DAFSA with compressed suffixes. | 518 // Testing to lookup keys in a DAFSA with compressed suffixes. |
473 // This DAFSA is constructed from words with similar suffixes but distinct | 519 // This DAFSA is constructed from words with similar suffixes but distinct |
474 // prefixes. The DAFSA will then form a trie with the implicit sink node as | 520 // prefixes. The DAFSA will then form a trie with the implicit sink node as |
475 // root. | 521 // root. |
476 | 522 |
477 const char key0[] = "a.b.ia"; | 523 const char key0[] = "a.b.ia"; |
478 const char key1[] = "a.b.jb"; | 524 const char key1[] = "a.b.jb"; |
479 const char key2[] = "a.b.kaa"; | 525 const char key2[] = "a.b.kaa"; |
480 const char key3[] = "a.b.lbb"; | 526 const char key3[] = "a.b.lbb"; |
481 const char key4[] = "a.b.aaa"; | 527 const char key4[] = "a.b.aaa"; |
482 const char key5[] = "a.b.bbb"; | 528 const char key5[] = "a.b.bbb"; |
483 const char key6[] = "a.b.maaaa"; | 529 const char key6[] = "a.b.maaaa"; |
484 const char key7[] = "a.b.nbbbb"; | 530 const char key7[] = "a.b.nbbbb"; |
485 | 531 |
486 EXPECT_EQ(2U, GetRegistryLengthFromHost(key0, EXCLUDE_UNKNOWN_REGISTRIES)); | |
487 EXPECT_EQ(0U, GetRegistryLengthFromHost(key1, EXCLUDE_UNKNOWN_REGISTRIES)); | |
488 EXPECT_EQ(2U, | 532 EXPECT_EQ(2U, |
489 GetRegistryLengthFromHostIncludingPrivate( | 533 GetCanonicalHostRegistryLength(key0, EXCLUDE_UNKNOWN_REGISTRIES)); |
490 key1, EXCLUDE_UNKNOWN_REGISTRIES)); | 534 EXPECT_EQ(0U, |
491 EXPECT_EQ(3U, GetRegistryLengthFromHost(key2, EXCLUDE_UNKNOWN_REGISTRIES)); | 535 GetCanonicalHostRegistryLength(key1, EXCLUDE_UNKNOWN_REGISTRIES)); |
492 EXPECT_EQ(0U, GetRegistryLengthFromHost(key3, EXCLUDE_UNKNOWN_REGISTRIES)); | 536 EXPECT_EQ(2U, GetCanonicalHostRegistryLengthIncludingPrivate(key1)); |
493 EXPECT_EQ(3U, | 537 EXPECT_EQ(3U, |
494 GetRegistryLengthFromHostIncludingPrivate( | 538 GetCanonicalHostRegistryLength(key2, EXCLUDE_UNKNOWN_REGISTRIES)); |
495 key3, EXCLUDE_UNKNOWN_REGISTRIES)); | |
496 EXPECT_EQ(0U, | 539 EXPECT_EQ(0U, |
497 GetRegistryLengthFromHostIncludingPrivate( | 540 GetCanonicalHostRegistryLength(key3, EXCLUDE_UNKNOWN_REGISTRIES)); |
498 key4, EXCLUDE_UNKNOWN_REGISTRIES)); | 541 EXPECT_EQ(3U, GetCanonicalHostRegistryLengthIncludingPrivate(key3)); |
499 EXPECT_EQ(0U, | 542 EXPECT_EQ(0U, GetCanonicalHostRegistryLengthIncludingPrivate(key4)); |
500 GetRegistryLengthFromHostIncludingPrivate( | 543 EXPECT_EQ(0U, GetCanonicalHostRegistryLengthIncludingPrivate(key5)); |
501 key5, EXCLUDE_UNKNOWN_REGISTRIES)); | 544 EXPECT_EQ(5U, |
502 EXPECT_EQ(5U, GetRegistryLengthFromHost(key6, EXCLUDE_UNKNOWN_REGISTRIES)); | 545 GetCanonicalHostRegistryLength(key6, EXCLUDE_UNKNOWN_REGISTRIES)); |
503 EXPECT_EQ(5U, GetRegistryLengthFromHost(key7, EXCLUDE_UNKNOWN_REGISTRIES)); | 546 EXPECT_EQ(5U, |
| 547 GetCanonicalHostRegistryLength(key7, EXCLUDE_UNKNOWN_REGISTRIES)); |
504 } | 548 } |
| 549 |
| 550 TEST_F(RegistryControlledDomainTest, Permissive) { |
| 551 UseDomainData(test1::kDafsa); |
| 552 |
| 553 EXPECT_EQ(std::string::npos, PermissiveGetHostRegistryLength("")); |
| 554 |
| 555 // Regular non-canonical host name. |
| 556 EXPECT_EQ(2U, PermissiveGetHostRegistryLength("Www.Google.Jp")); |
| 557 EXPECT_EQ(3U, PermissiveGetHostRegistryLength("Www.Google.Jp.")); |
| 558 |
| 559 // Empty returns npos. |
| 560 EXPECT_EQ(std::string::npos, PermissiveGetHostRegistryLength("")); |
| 561 |
| 562 // Trailing spaces are counted as part of the hostname, meaning this will |
| 563 // not match a known registry. |
| 564 EXPECT_EQ(0U, PermissiveGetHostRegistryLength("Www.Google.Jp ")); |
| 565 |
| 566 // Invalid characters at the beginning are OK if the suffix still matches. |
| 567 EXPECT_EQ(2U, PermissiveGetHostRegistryLength("*%00#?.Jp")); |
| 568 |
| 569 // Escaped period, this will add new components. |
| 570 EXPECT_EQ(4U, PermissiveGetHostRegistryLength("Www.Googl%45%2e%4Ap")); |
| 571 |
| 572 // IDN cases (not supported when not linking ICU). |
| 573 #if !BUILDFLAG(USE_PLATFORM_ICU_ALTERNATIVES) |
| 574 EXPECT_EQ(10U, PermissiveGetHostRegistryLength("foo.xn--fiqs8s")); |
| 575 EXPECT_EQ(11U, PermissiveGetHostRegistryLength("foo.xn--fiqs8s.")); |
| 576 EXPECT_EQ(18U, PermissiveGetHostRegistryLength("foo.%E4%B8%AD%E5%9B%BD")); |
| 577 EXPECT_EQ(19U, PermissiveGetHostRegistryLength("foo.%E4%B8%AD%E5%9B%BD.")); |
| 578 EXPECT_EQ(6U, |
| 579 PermissiveGetHostRegistryLength("foo.\xE4\xB8\xAD\xE5\x9B\xBD")); |
| 580 EXPECT_EQ(7U, |
| 581 PermissiveGetHostRegistryLength("foo.\xE4\xB8\xAD\xE5\x9B\xBD.")); |
| 582 // UTF-16 IDN. |
| 583 EXPECT_EQ(2U, PermissiveGetHostRegistryLength( |
| 584 base::WideToUTF16(L"foo.\x4e2d\x56fd"))); |
| 585 |
| 586 // Fullwidth dot (u+FF0E) that will get canonicalized to a dot. |
| 587 EXPECT_EQ(2U, PermissiveGetHostRegistryLength("Www.Google\xEF\xBC\x8Ejp")); |
| 588 // Same but also ending in a fullwidth dot. |
| 589 EXPECT_EQ(5U, PermissiveGetHostRegistryLength( |
| 590 "Www.Google\xEF\xBC\x8Ejp\xEF\xBC\x8E")); |
| 591 // Escaped UTF-8, also with an escaped fullwidth "Jp". |
| 592 // "Jp" = U+FF2A, U+FF50, UTF-8 = EF BC AA EF BD 90 |
| 593 EXPECT_EQ(27U, PermissiveGetHostRegistryLength( |
| 594 "Www.Google%EF%BC%8E%EF%BC%AA%EF%BD%90%EF%BC%8E")); |
| 595 // UTF-16 (ending in a dot). |
| 596 EXPECT_EQ(3U, PermissiveGetHostRegistryLength( |
| 597 base::WideToUTF16(L"Www.Google\xFF0E\xFF2A\xFF50\xFF0E"))); |
| 598 #endif |
| 599 } |
| 600 |
505 } // namespace registry_controlled_domains | 601 } // namespace registry_controlled_domains |
506 } // namespace net | 602 } // namespace net |
OLD | NEW |