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