| 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 "net/base/net_util.h" | 5 #include "net/base/net_util.h" |
| 6 | 6 |
| 7 #include <ostream> | 7 #include <ostream> |
| 8 | 8 |
| 9 #include "base/format_macros.h" | 9 #include "base/format_macros.h" |
| 10 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
| 11 #include "base/strings/string_util.h" | 11 #include "base/strings/string_util.h" |
| 12 #include "base/strings/stringprintf.h" | 12 #include "base/strings/stringprintf.h" |
| 13 #include "base/strings/utf_string_conversions.h" | |
| 14 #include "net/base/address_list.h" | 13 #include "net/base/address_list.h" |
| 15 #include "net/base/ip_endpoint.h" | 14 #include "net/base/ip_endpoint.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
| 17 #include "url/gurl.h" | 16 #include "url/gurl.h" |
| 18 | 17 |
| 19 #if !defined(OS_NACL) && !defined(OS_WIN) | 18 #if !defined(OS_NACL) && !defined(OS_WIN) |
| 20 #include <net/if.h> | 19 #include <net/if.h> |
| 21 #include <netinet/in.h> | 20 #include <netinet/in.h> |
| 22 #if defined(OS_MACOSX) | 21 #if defined(OS_MACOSX) |
| 23 #include <ifaddrs.h> | 22 #include <ifaddrs.h> |
| 24 #if !defined(OS_IOS) | 23 #if !defined(OS_IOS) |
| 25 #include <netinet/in_var.h> | 24 #include <netinet/in_var.h> |
| 26 #endif // !OS_IOS | 25 #endif // !OS_IOS |
| 27 #endif // OS_MACOSX | 26 #endif // OS_MACOSX |
| 28 #endif // !OS_NACL && !OS_WIN | 27 #endif // !OS_NACL && !OS_WIN |
| 29 | 28 |
| 30 #if defined(OS_WIN) | |
| 31 #include <iphlpapi.h> | |
| 32 #include <objbase.h> | |
| 33 #include "base/win/windows_version.h" | |
| 34 #endif // OS_WIN | |
| 35 | |
| 36 #if !defined(OS_MACOSX) && !defined(OS_NACL) && !defined(OS_WIN) | 29 #if !defined(OS_MACOSX) && !defined(OS_NACL) && !defined(OS_WIN) |
| 37 #include "net/base/address_tracker_linux.h" | 30 #include "net/base/address_tracker_linux.h" |
| 38 #endif // !OS_MACOSX && !OS_NACL && !OS_WIN | 31 #endif // !OS_MACOSX && !OS_NACL && !OS_WIN |
| 39 | 32 |
| 40 using base::ASCIIToUTF16; | |
| 41 using base::WideToUTF16; | |
| 42 | |
| 43 namespace net { | 33 namespace net { |
| 44 | 34 |
| 45 namespace { | 35 namespace { |
| 46 | 36 |
| 47 const unsigned char kLocalhostIPv4[] = {127, 0, 0, 1}; | 37 const unsigned char kLocalhostIPv4[] = {127, 0, 0, 1}; |
| 48 const unsigned char kLocalhostIPv6[] = | 38 const unsigned char kLocalhostIPv6[] = |
| 49 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}; | 39 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}; |
| 50 const uint16_t kLocalhostLookupPort = 80; | 40 const uint16_t kLocalhostLookupPort = 80; |
| 51 | 41 |
| 52 bool HasEndpoint(const IPEndPoint& endpoint, const AddressList& addresses) { | 42 bool HasEndpoint(const IPEndPoint& endpoint, const AddressList& addresses) { |
| (...skipping 28 matching lines...) Expand all Loading... |
| 81 kLocalhostLookupPort); | 71 kLocalhostLookupPort); |
| 82 | 72 |
| 83 AddressList addresses; | 73 AddressList addresses; |
| 84 EXPECT_TRUE(ResolveLocalHostname(host, kLocalhostLookupPort, &addresses)); | 74 EXPECT_TRUE(ResolveLocalHostname(host, kLocalhostLookupPort, &addresses)); |
| 85 EXPECT_EQ(1u, addresses.size()); | 75 EXPECT_EQ(1u, addresses.size()); |
| 86 EXPECT_TRUE(HasEndpoint(localhost_ipv6, addresses)); | 76 EXPECT_TRUE(HasEndpoint(localhost_ipv6, addresses)); |
| 87 } | 77 } |
| 88 | 78 |
| 89 } // anonymous namespace | 79 } // anonymous namespace |
| 90 | 80 |
| 91 TEST(NetUtilTest, GetIdentityFromURL) { | 81 TEST(NetUtilTest, CompliantHost) { |
| 92 struct { | 82 struct { |
| 93 const char* const input_url; | |
| 94 const char* const expected_username; | |
| 95 const char* const expected_password; | |
| 96 } tests[] = { | |
| 97 { | |
| 98 "http://username:password@google.com", | |
| 99 "username", | |
| 100 "password", | |
| 101 }, | |
| 102 { // Test for http://crbug.com/19200 | |
| 103 "http://username:p@ssword@google.com", | |
| 104 "username", | |
| 105 "p@ssword", | |
| 106 }, | |
| 107 { // Special URL characters should be unescaped. | |
| 108 "http://username:p%3fa%26s%2fs%23@google.com", | |
| 109 "username", | |
| 110 "p?a&s/s#", | |
| 111 }, | |
| 112 { // Username contains %20. | |
| 113 "http://use rname:password@google.com", | |
| 114 "use rname", | |
| 115 "password", | |
| 116 }, | |
| 117 { // Keep %00 as is. | |
| 118 "http://use%00rname:password@google.com", | |
| 119 "use%00rname", | |
| 120 "password", | |
| 121 }, | |
| 122 { // Use a '+' in the username. | |
| 123 "http://use+rname:password@google.com", | |
| 124 "use+rname", | |
| 125 "password", | |
| 126 }, | |
| 127 { // Use a '&' in the password. | |
| 128 "http://username:p&ssword@google.com", | |
| 129 "username", | |
| 130 "p&ssword", | |
| 131 }, | |
| 132 }; | |
| 133 for (size_t i = 0; i < arraysize(tests); ++i) { | |
| 134 SCOPED_TRACE(base::StringPrintf("Test[%" PRIuS "]: %s", i, | |
| 135 tests[i].input_url)); | |
| 136 GURL url(tests[i].input_url); | |
| 137 | |
| 138 base::string16 username, password; | |
| 139 GetIdentityFromURL(url, &username, &password); | |
| 140 | |
| 141 EXPECT_EQ(ASCIIToUTF16(tests[i].expected_username), username); | |
| 142 EXPECT_EQ(ASCIIToUTF16(tests[i].expected_password), password); | |
| 143 } | |
| 144 } | |
| 145 | |
| 146 // Try extracting a username which was encoded with UTF8. | |
| 147 TEST(NetUtilTest, GetIdentityFromURL_UTF8) { | |
| 148 GURL url(WideToUTF16(L"http://foo:\x4f60\x597d@blah.com")); | |
| 149 | |
| 150 EXPECT_EQ("foo", url.username()); | |
| 151 EXPECT_EQ("%E4%BD%A0%E5%A5%BD", url.password()); | |
| 152 | |
| 153 // Extract the unescaped identity. | |
| 154 base::string16 username, password; | |
| 155 GetIdentityFromURL(url, &username, &password); | |
| 156 | |
| 157 // Verify that it was decoded as UTF8. | |
| 158 EXPECT_EQ(ASCIIToUTF16("foo"), username); | |
| 159 EXPECT_EQ(WideToUTF16(L"\x4f60\x597d"), password); | |
| 160 } | |
| 161 | |
| 162 TEST(NetUtilTest, CompliantHost) { | |
| 163 struct CompliantHostCase { | |
| 164 const char* const host; | 83 const char* const host; |
| 165 bool expected_output; | 84 bool expected_output; |
| 166 }; | 85 } compliant_host_cases[] = { |
| 167 | |
| 168 const CompliantHostCase compliant_host_cases[] = { | |
| 169 {"", false}, | 86 {"", false}, |
| 170 {"a", true}, | 87 {"a", true}, |
| 171 {"-", false}, | 88 {"-", false}, |
| 172 {"_", false}, | 89 {"_", false}, |
| 173 {".", false}, | 90 {".", false}, |
| 174 {"9", true}, | 91 {"9", true}, |
| 175 {"9a", true}, | 92 {"9a", true}, |
| 176 {"9_", true}, | 93 {"9_", true}, |
| 177 {"a.", true}, | 94 {"a.", true}, |
| 178 {"a.a", true}, | 95 {"a.a", true}, |
| (...skipping 273 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 452 EXPECT_FALSE(ResolveLocalHostname("::1:1", kLocalhostLookupPort, &addresses)); | 369 EXPECT_FALSE(ResolveLocalHostname("::1:1", kLocalhostLookupPort, &addresses)); |
| 453 EXPECT_FALSE(ResolveLocalHostname("0:0:0:0:0:0:0:0:1", kLocalhostLookupPort, | 370 EXPECT_FALSE(ResolveLocalHostname("0:0:0:0:0:0:0:0:1", kLocalhostLookupPort, |
| 454 &addresses)); | 371 &addresses)); |
| 455 EXPECT_FALSE(ResolveLocalHostname("foo.localhost.com", kLocalhostLookupPort, | 372 EXPECT_FALSE(ResolveLocalHostname("foo.localhost.com", kLocalhostLookupPort, |
| 456 &addresses)); | 373 &addresses)); |
| 457 EXPECT_FALSE( | 374 EXPECT_FALSE( |
| 458 ResolveLocalHostname("foo.localhoste", kLocalhostLookupPort, &addresses)); | 375 ResolveLocalHostname("foo.localhoste", kLocalhostLookupPort, &addresses)); |
| 459 } | 376 } |
| 460 | 377 |
| 461 TEST(NetUtilTest, GoogleHost) { | 378 TEST(NetUtilTest, GoogleHost) { |
| 462 struct GoogleHostCase { | 379 struct { |
| 463 GURL url; | 380 GURL url; |
| 464 bool expected_output; | 381 bool expected_output; |
| 465 }; | 382 } google_host_cases[] = { |
| 466 | |
| 467 const GoogleHostCase google_host_cases[] = { | |
| 468 {GURL("http://.google.com"), true}, | 383 {GURL("http://.google.com"), true}, |
| 469 {GURL("http://.youtube.com"), true}, | 384 {GURL("http://.youtube.com"), true}, |
| 470 {GURL("http://.gmail.com"), true}, | 385 {GURL("http://.gmail.com"), true}, |
| 471 {GURL("http://.doubleclick.net"), true}, | 386 {GURL("http://.doubleclick.net"), true}, |
| 472 {GURL("http://.gstatic.com"), true}, | 387 {GURL("http://.gstatic.com"), true}, |
| 473 {GURL("http://.googlevideo.com"), true}, | 388 {GURL("http://.googlevideo.com"), true}, |
| 474 {GURL("http://.googleusercontent.com"), true}, | 389 {GURL("http://.googleusercontent.com"), true}, |
| 475 {GURL("http://.googlesyndication.com"), true}, | 390 {GURL("http://.googlesyndication.com"), true}, |
| 476 {GURL("http://.google-analytics.com"), true}, | 391 {GURL("http://.google-analytics.com"), true}, |
| 477 {GURL("http://.googleadservices.com"), true}, | 392 {GURL("http://.googleadservices.com"), true}, |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 568 TEST_P(NetUtilNonUniqueNameTest, IsHostnameNonUnique) { | 483 TEST_P(NetUtilNonUniqueNameTest, IsHostnameNonUnique) { |
| 569 const NonUniqueNameTestData& test_data = GetParam(); | 484 const NonUniqueNameTestData& test_data = GetParam(); |
| 570 | 485 |
| 571 EXPECT_EQ(test_data.is_unique, IsUnique(test_data.hostname)); | 486 EXPECT_EQ(test_data.is_unique, IsUnique(test_data.hostname)); |
| 572 } | 487 } |
| 573 | 488 |
| 574 INSTANTIATE_TEST_CASE_P(, NetUtilNonUniqueNameTest, | 489 INSTANTIATE_TEST_CASE_P(, NetUtilNonUniqueNameTest, |
| 575 testing::ValuesIn(kNonUniqueNameTestData)); | 490 testing::ValuesIn(kNonUniqueNameTestData)); |
| 576 | 491 |
| 577 } // namespace net | 492 } // namespace net |
| OLD | NEW |