OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/url_util.h" | 5 #include "net/base/url_util.h" |
6 | 6 |
| 7 #include <ostream> |
| 8 |
7 #include "base/format_macros.h" | 9 #include "base/format_macros.h" |
8 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
9 #include "base/strings/stringprintf.h" | 11 #include "base/strings/stringprintf.h" |
10 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
11 #include "url/gurl.h" | 13 #include "url/gurl.h" |
12 | 14 |
13 using base::ASCIIToUTF16; | 15 using base::ASCIIToUTF16; |
14 using base::WideToUTF16; | 16 using base::WideToUTF16; |
15 | 17 |
16 namespace net { | 18 namespace net { |
(...skipping 282 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
299 {"1.2.3.4.5", true}, | 301 {"1.2.3.4.5", true}, |
300 {"1.2.3.4.5.", true}, | 302 {"1.2.3.4.5.", true}, |
301 }; | 303 }; |
302 | 304 |
303 for (size_t i = 0; i < arraysize(compliant_host_cases); ++i) { | 305 for (size_t i = 0; i < arraysize(compliant_host_cases); ++i) { |
304 EXPECT_EQ(compliant_host_cases[i].expected_output, | 306 EXPECT_EQ(compliant_host_cases[i].expected_output, |
305 IsCanonicalizedHostCompliant(compliant_host_cases[i].host)); | 307 IsCanonicalizedHostCompliant(compliant_host_cases[i].host)); |
306 } | 308 } |
307 } | 309 } |
308 | 310 |
| 311 struct NonUniqueNameTestData { |
| 312 bool is_unique; |
| 313 const char* const hostname; |
| 314 }; |
| 315 |
| 316 // Google Test pretty-printer. |
| 317 void PrintTo(const NonUniqueNameTestData& data, std::ostream* os) { |
| 318 ASSERT_TRUE(data.hostname); |
| 319 *os << " hostname: " << testing::PrintToString(data.hostname) |
| 320 << "; is_unique: " << testing::PrintToString(data.is_unique); |
| 321 } |
| 322 |
| 323 const NonUniqueNameTestData kNonUniqueNameTestData[] = { |
| 324 // Domains under ICANN-assigned domains. |
| 325 { true, "google.com" }, |
| 326 { true, "google.co.uk" }, |
| 327 // Domains under private registries. |
| 328 { true, "appspot.com" }, |
| 329 { true, "test.appspot.com" }, |
| 330 // Unreserved IPv4 addresses (in various forms). |
| 331 { true, "8.8.8.8" }, |
| 332 { true, "99.64.0.0" }, |
| 333 { true, "212.15.0.0" }, |
| 334 { true, "212.15" }, |
| 335 { true, "212.15.0" }, |
| 336 { true, "3557752832" }, |
| 337 // Reserved IPv4 addresses (in various forms). |
| 338 { false, "192.168.0.0" }, |
| 339 { false, "192.168.0.6" }, |
| 340 { false, "10.0.0.5" }, |
| 341 { false, "10.0" }, |
| 342 { false, "10.0.0" }, |
| 343 { false, "3232235526" }, |
| 344 // Unreserved IPv6 addresses. |
| 345 { true, "FFC0:ba98:7654:3210:FEDC:BA98:7654:3210" }, |
| 346 { true, "2000:ba98:7654:2301:EFCD:BA98:7654:3210" }, |
| 347 // Reserved IPv6 addresses. |
| 348 { false, "::192.9.5.5" }, |
| 349 { false, "FEED::BEEF" }, |
| 350 { false, "FEC0:ba98:7654:3210:FEDC:BA98:7654:3210" }, |
| 351 // 'internal'/non-IANA assigned domains. |
| 352 { false, "intranet" }, |
| 353 { false, "intranet." }, |
| 354 { false, "intranet.example" }, |
| 355 { false, "host.intranet.example" }, |
| 356 // gTLDs under discussion, but not yet assigned. |
| 357 { false, "intranet.corp" }, |
| 358 { false, "intranet.internal" }, |
| 359 // Invalid host names are treated as unique - but expected to be |
| 360 // filtered out before then. |
| 361 { true, "junk)(£)$*!@~#" }, |
| 362 { true, "w$w.example.com" }, |
| 363 { true, "nocolonsallowed:example" }, |
| 364 { true, "[::4.5.6.9]" }, |
| 365 }; |
| 366 |
| 367 class UrlUtilNonUniqueNameTest |
| 368 : public testing::TestWithParam<NonUniqueNameTestData> { |
| 369 public: |
| 370 virtual ~UrlUtilNonUniqueNameTest() {} |
| 371 |
| 372 protected: |
| 373 bool IsUnique(const std::string& hostname) { |
| 374 return !IsHostnameNonUnique(hostname); |
| 375 } |
| 376 }; |
| 377 |
| 378 // Test that internal/non-unique names are properly identified as such, but |
| 379 // that IP addresses and hosts beneath registry-controlled domains are flagged |
| 380 // as unique names. |
| 381 TEST_P(UrlUtilNonUniqueNameTest, IsHostnameNonUnique) { |
| 382 const NonUniqueNameTestData& test_data = GetParam(); |
| 383 |
| 384 EXPECT_EQ(test_data.is_unique, IsUnique(test_data.hostname)); |
| 385 } |
| 386 |
| 387 INSTANTIATE_TEST_CASE_P(, UrlUtilNonUniqueNameTest, |
| 388 testing::ValuesIn(kNonUniqueNameTestData)); |
| 389 |
309 TEST(UrlUtilTest, SimplifyUrlForRequest) { | 390 TEST(UrlUtilTest, SimplifyUrlForRequest) { |
310 struct { | 391 struct { |
311 const char* const input_url; | 392 const char* const input_url; |
312 const char* const expected_simplified_url; | 393 const char* const expected_simplified_url; |
313 } tests[] = { | 394 } tests[] = { |
314 { | 395 { |
315 // Reference section should be stripped. | 396 // Reference section should be stripped. |
316 "http://www.google.com:78/foobar?query=1#hash", | 397 "http://www.google.com:78/foobar?query=1#hash", |
317 "http://www.google.com:78/foobar?query=1", | 398 "http://www.google.com:78/foobar?query=1", |
318 }, | 399 }, |
(...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
452 }; | 533 }; |
453 | 534 |
454 for (size_t i = 0; i < arraysize(google_host_cases); ++i) { | 535 for (size_t i = 0; i < arraysize(google_host_cases); ++i) { |
455 EXPECT_EQ(google_host_cases[i].expected_output, | 536 EXPECT_EQ(google_host_cases[i].expected_output, |
456 HasGoogleHost(google_host_cases[i].url)); | 537 HasGoogleHost(google_host_cases[i].url)); |
457 } | 538 } |
458 } | 539 } |
459 | 540 |
460 } // namespace | 541 } // namespace |
461 } // namespace net | 542 } // namespace net |
OLD | NEW |