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