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 <string.h> | 7 #include <string.h> |
8 | 8 |
9 #include <ostream> | 9 #include <ostream> |
10 | 10 |
11 #include "base/files/file_path.h" | 11 #include "base/files/file_path.h" |
12 #include "base/format_macros.h" | 12 #include "base/format_macros.h" |
13 #include "base/scoped_native_library.h" | 13 #include "base/scoped_native_library.h" |
14 #include "base/strings/string_number_conversions.h" | 14 #include "base/strings/string_number_conversions.h" |
15 #include "base/strings/string_util.h" | 15 #include "base/strings/string_util.h" |
16 #include "base/strings/stringprintf.h" | 16 #include "base/strings/stringprintf.h" |
17 #include "base/strings/utf_string_conversions.h" | 17 #include "base/strings/utf_string_conversions.h" |
18 #include "base/sys_byteorder.h" | 18 #include "base/sys_byteorder.h" |
19 #include "base/time/time.h" | 19 #include "base/time/time.h" |
| 20 #include "net/base/address_list.h" |
20 #include "net/base/ip_endpoint.h" | 21 #include "net/base/ip_endpoint.h" |
21 | 22 |
22 #if !defined(OS_NACL) && !defined(OS_WIN) | 23 #if !defined(OS_NACL) && !defined(OS_WIN) |
23 #include <net/if.h> | 24 #include <net/if.h> |
24 #include <netinet/in.h> | 25 #include <netinet/in.h> |
25 #if defined(OS_MACOSX) | 26 #if defined(OS_MACOSX) |
26 #include <ifaddrs.h> | 27 #include <ifaddrs.h> |
27 #if !defined(OS_IOS) | 28 #if !defined(OS_IOS) |
28 #include <netinet/in_var.h> | 29 #include <netinet/in_var.h> |
29 #endif // !OS_IOS | 30 #endif // !OS_IOS |
(...skipping 17 matching lines...) Expand all Loading... |
47 | 48 |
48 namespace net { | 49 namespace net { |
49 | 50 |
50 namespace { | 51 namespace { |
51 | 52 |
52 struct HeaderCase { | 53 struct HeaderCase { |
53 const char* const header_name; | 54 const char* const header_name; |
54 const char* const expected; | 55 const char* const expected; |
55 }; | 56 }; |
56 | 57 |
| 58 const unsigned char kLocalhostIPv4[] = {127, 0, 0, 1}; |
| 59 const unsigned char kLocalhostIPv6[] = |
| 60 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}; |
| 61 const uint16_t kLocalhostLookupPort = 80; |
| 62 |
57 // Fills in sockaddr for the given 32-bit address (IPv4.) | 63 // Fills in sockaddr for the given 32-bit address (IPv4.) |
58 // |bytes| should be an array of length 4. | 64 // |bytes| should be an array of length 4. |
59 void MakeIPv4Address(const uint8_t* bytes, int port, SockaddrStorage* storage) { | 65 void MakeIPv4Address(const uint8_t* bytes, int port, SockaddrStorage* storage) { |
60 memset(&storage->addr_storage, 0, sizeof(storage->addr_storage)); | 66 memset(&storage->addr_storage, 0, sizeof(storage->addr_storage)); |
61 storage->addr_len = sizeof(struct sockaddr_in); | 67 storage->addr_len = sizeof(struct sockaddr_in); |
62 struct sockaddr_in* addr4 = reinterpret_cast<sockaddr_in*>(storage->addr); | 68 struct sockaddr_in* addr4 = reinterpret_cast<sockaddr_in*>(storage->addr); |
63 addr4->sin_port = base::HostToNet16(port); | 69 addr4->sin_port = base::HostToNet16(port); |
64 addr4->sin_family = AF_INET; | 70 addr4->sin_family = AF_INET; |
65 memcpy(&addr4->sin_addr, bytes, 4); | 71 memcpy(&addr4->sin_addr, bytes, 4); |
66 } | 72 } |
67 | 73 |
68 // Fills in sockaddr for the given 128-bit address (IPv6.) | 74 // Fills in sockaddr for the given 128-bit address (IPv6.) |
69 // |bytes| should be an array of length 16. | 75 // |bytes| should be an array of length 16. |
70 void MakeIPv6Address(const uint8_t* bytes, int port, SockaddrStorage* storage) { | 76 void MakeIPv6Address(const uint8_t* bytes, int port, SockaddrStorage* storage) { |
71 memset(&storage->addr_storage, 0, sizeof(storage->addr_storage)); | 77 memset(&storage->addr_storage, 0, sizeof(storage->addr_storage)); |
72 storage->addr_len = sizeof(struct sockaddr_in6); | 78 storage->addr_len = sizeof(struct sockaddr_in6); |
73 struct sockaddr_in6* addr6 = reinterpret_cast<sockaddr_in6*>(storage->addr); | 79 struct sockaddr_in6* addr6 = reinterpret_cast<sockaddr_in6*>(storage->addr); |
74 addr6->sin6_port = base::HostToNet16(port); | 80 addr6->sin6_port = base::HostToNet16(port); |
75 addr6->sin6_family = AF_INET6; | 81 addr6->sin6_family = AF_INET6; |
76 memcpy(&addr6->sin6_addr, bytes, 16); | 82 memcpy(&addr6->sin6_addr, bytes, 16); |
77 } | 83 } |
78 | 84 |
| 85 bool HasEndpoint(const IPEndPoint& endpoint, const AddressList& addresses) { |
| 86 for (const auto& address : addresses) { |
| 87 if (endpoint == address) |
| 88 return true; |
| 89 } |
| 90 return false; |
| 91 } |
| 92 |
| 93 void TestBothLoopbackIPs(const std::string& host) { |
| 94 IPEndPoint localhost_ipv4( |
| 95 IPAddressNumber(kLocalhostIPv4, |
| 96 kLocalhostIPv4 + arraysize(kLocalhostIPv4)), |
| 97 kLocalhostLookupPort); |
| 98 IPEndPoint localhost_ipv6( |
| 99 IPAddressNumber(kLocalhostIPv6, |
| 100 kLocalhostIPv6 + arraysize(kLocalhostIPv6)), |
| 101 kLocalhostLookupPort); |
| 102 |
| 103 AddressList addresses; |
| 104 EXPECT_TRUE(ResolveLocalHostname(host, kLocalhostLookupPort, &addresses)); |
| 105 EXPECT_EQ(2u, addresses.size()); |
| 106 EXPECT_TRUE(HasEndpoint(localhost_ipv4, addresses)); |
| 107 EXPECT_TRUE(HasEndpoint(localhost_ipv6, addresses)); |
| 108 } |
| 109 |
| 110 void TestIPv6LoopbackOnly(const std::string& host) { |
| 111 IPEndPoint localhost_ipv6( |
| 112 IPAddressNumber(kLocalhostIPv6, |
| 113 kLocalhostIPv6 + arraysize(kLocalhostIPv6)), |
| 114 kLocalhostLookupPort); |
| 115 |
| 116 AddressList addresses; |
| 117 EXPECT_TRUE(ResolveLocalHostname(host, kLocalhostLookupPort, &addresses)); |
| 118 EXPECT_EQ(1u, addresses.size()); |
| 119 EXPECT_TRUE(HasEndpoint(localhost_ipv6, addresses)); |
| 120 } |
| 121 |
79 } // anonymous namespace | 122 } // anonymous namespace |
80 | 123 |
81 TEST(NetUtilTest, GetIdentityFromURL) { | 124 TEST(NetUtilTest, GetIdentityFromURL) { |
82 struct { | 125 struct { |
83 const char* const input_url; | 126 const char* const input_url; |
84 const char* const expected_username; | 127 const char* const expected_username; |
85 const char* const expected_password; | 128 const char* const expected_password; |
86 } tests[] = { | 129 } tests[] = { |
87 { | 130 { |
88 "http://username:password@google.com", | 131 "http://username:password@google.com", |
(...skipping 381 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
470 TEST(NetUtilTest, GetAddressFamily) { | 513 TEST(NetUtilTest, GetAddressFamily) { |
471 IPAddressNumber number; | 514 IPAddressNumber number; |
472 EXPECT_TRUE(ParseIPLiteralToNumber("192.168.0.1", &number)); | 515 EXPECT_TRUE(ParseIPLiteralToNumber("192.168.0.1", &number)); |
473 EXPECT_EQ(ADDRESS_FAMILY_IPV4, GetAddressFamily(number)); | 516 EXPECT_EQ(ADDRESS_FAMILY_IPV4, GetAddressFamily(number)); |
474 EXPECT_TRUE(ParseIPLiteralToNumber("1:abcd::3:4:ff", &number)); | 517 EXPECT_TRUE(ParseIPLiteralToNumber("1:abcd::3:4:ff", &number)); |
475 EXPECT_EQ(ADDRESS_FAMILY_IPV6, GetAddressFamily(number)); | 518 EXPECT_EQ(ADDRESS_FAMILY_IPV6, GetAddressFamily(number)); |
476 } | 519 } |
477 | 520 |
478 TEST(NetUtilTest, IsLocalhost) { | 521 TEST(NetUtilTest, IsLocalhost) { |
479 EXPECT_TRUE(IsLocalhost("localhost")); | 522 EXPECT_TRUE(IsLocalhost("localhost")); |
| 523 EXPECT_TRUE(IsLocalhost("localHosT")); |
| 524 EXPECT_TRUE(IsLocalhost("localhost.")); |
| 525 EXPECT_TRUE(IsLocalhost("localHost.")); |
480 EXPECT_TRUE(IsLocalhost("localhost.localdomain")); | 526 EXPECT_TRUE(IsLocalhost("localhost.localdomain")); |
| 527 EXPECT_TRUE(IsLocalhost("localhost.localDOMain")); |
| 528 EXPECT_TRUE(IsLocalhost("localhost.localdomain.")); |
481 EXPECT_TRUE(IsLocalhost("localhost6")); | 529 EXPECT_TRUE(IsLocalhost("localhost6")); |
| 530 EXPECT_TRUE(IsLocalhost("localhost6.")); |
482 EXPECT_TRUE(IsLocalhost("localhost6.localdomain6")); | 531 EXPECT_TRUE(IsLocalhost("localhost6.localdomain6")); |
| 532 EXPECT_TRUE(IsLocalhost("localhost6.localdomain6.")); |
483 EXPECT_TRUE(IsLocalhost("127.0.0.1")); | 533 EXPECT_TRUE(IsLocalhost("127.0.0.1")); |
484 EXPECT_TRUE(IsLocalhost("127.0.1.0")); | 534 EXPECT_TRUE(IsLocalhost("127.0.1.0")); |
485 EXPECT_TRUE(IsLocalhost("127.1.0.0")); | 535 EXPECT_TRUE(IsLocalhost("127.1.0.0")); |
486 EXPECT_TRUE(IsLocalhost("127.0.0.255")); | 536 EXPECT_TRUE(IsLocalhost("127.0.0.255")); |
487 EXPECT_TRUE(IsLocalhost("127.0.255.0")); | 537 EXPECT_TRUE(IsLocalhost("127.0.255.0")); |
488 EXPECT_TRUE(IsLocalhost("127.255.0.0")); | 538 EXPECT_TRUE(IsLocalhost("127.255.0.0")); |
489 EXPECT_TRUE(IsLocalhost("::1")); | 539 EXPECT_TRUE(IsLocalhost("::1")); |
490 EXPECT_TRUE(IsLocalhost("0:0:0:0:0:0:0:1")); | 540 EXPECT_TRUE(IsLocalhost("0:0:0:0:0:0:0:1")); |
491 EXPECT_TRUE(IsLocalhost("foo.localhost")); | 541 EXPECT_TRUE(IsLocalhost("foo.localhost")); |
| 542 EXPECT_TRUE(IsLocalhost("foo.localhost.")); |
492 | 543 |
493 EXPECT_FALSE(IsLocalhost("localhostx")); | 544 EXPECT_FALSE(IsLocalhost("localhostx")); |
| 545 EXPECT_FALSE(IsLocalhost("localhost.x")); |
494 EXPECT_FALSE(IsLocalhost("foo.localdomain")); | 546 EXPECT_FALSE(IsLocalhost("foo.localdomain")); |
| 547 EXPECT_FALSE(IsLocalhost("foo.localdomain.x")); |
495 EXPECT_FALSE(IsLocalhost("localhost6x")); | 548 EXPECT_FALSE(IsLocalhost("localhost6x")); |
496 EXPECT_FALSE(IsLocalhost("localhost.localdomain6")); | 549 EXPECT_FALSE(IsLocalhost("localhost.localdomain6")); |
497 EXPECT_FALSE(IsLocalhost("localhost6.localdomain")); | 550 EXPECT_FALSE(IsLocalhost("localhost6.localdomain")); |
498 EXPECT_FALSE(IsLocalhost("127.0.0.1.1")); | 551 EXPECT_FALSE(IsLocalhost("127.0.0.1.1")); |
499 EXPECT_FALSE(IsLocalhost(".127.0.0.255")); | 552 EXPECT_FALSE(IsLocalhost(".127.0.0.255")); |
500 EXPECT_FALSE(IsLocalhost("::2")); | 553 EXPECT_FALSE(IsLocalhost("::2")); |
501 EXPECT_FALSE(IsLocalhost("::1:1")); | 554 EXPECT_FALSE(IsLocalhost("::1:1")); |
502 EXPECT_FALSE(IsLocalhost("0:0:0:0:1:0:0:1")); | 555 EXPECT_FALSE(IsLocalhost("0:0:0:0:1:0:0:1")); |
503 EXPECT_FALSE(IsLocalhost("::1:1")); | 556 EXPECT_FALSE(IsLocalhost("::1:1")); |
504 EXPECT_FALSE(IsLocalhost("0:0:0:0:0:0:0:0:1")); | 557 EXPECT_FALSE(IsLocalhost("0:0:0:0:0:0:0:0:1")); |
505 EXPECT_FALSE(IsLocalhost("foo.localhost.com")); | 558 EXPECT_FALSE(IsLocalhost("foo.localhost.com")); |
506 EXPECT_FALSE(IsLocalhost("foo.localhoste")); | 559 EXPECT_FALSE(IsLocalhost("foo.localhoste")); |
507 } | 560 } |
508 | 561 |
| 562 TEST(NetUtilTest, ResolveLocalHostname) { |
| 563 AddressList addresses; |
| 564 |
| 565 TestBothLoopbackIPs("localhost"); |
| 566 TestBothLoopbackIPs("localhoST"); |
| 567 TestBothLoopbackIPs("localhost."); |
| 568 TestBothLoopbackIPs("localhoST."); |
| 569 TestBothLoopbackIPs("localhost.localdomain"); |
| 570 TestBothLoopbackIPs("localhost.localdomAIn"); |
| 571 TestBothLoopbackIPs("localhost.localdomain."); |
| 572 TestBothLoopbackIPs("localhost.localdomAIn."); |
| 573 TestBothLoopbackIPs("foo.localhost"); |
| 574 TestBothLoopbackIPs("foo.localhOSt"); |
| 575 TestBothLoopbackIPs("foo.localhost."); |
| 576 TestBothLoopbackIPs("foo.localhOSt."); |
| 577 |
| 578 TestIPv6LoopbackOnly("localhost6"); |
| 579 TestIPv6LoopbackOnly("localhoST6"); |
| 580 TestIPv6LoopbackOnly("localhost6."); |
| 581 TestIPv6LoopbackOnly("localhost6.localdomain6"); |
| 582 TestIPv6LoopbackOnly("localhost6.localdomain6."); |
| 583 |
| 584 EXPECT_FALSE( |
| 585 ResolveLocalHostname("127.0.0.1", kLocalhostLookupPort, &addresses)); |
| 586 EXPECT_FALSE(ResolveLocalHostname("::1", kLocalhostLookupPort, &addresses)); |
| 587 EXPECT_FALSE(ResolveLocalHostname("0:0:0:0:0:0:0:1", kLocalhostLookupPort, |
| 588 &addresses)); |
| 589 EXPECT_FALSE( |
| 590 ResolveLocalHostname("localhostx", kLocalhostLookupPort, &addresses)); |
| 591 EXPECT_FALSE( |
| 592 ResolveLocalHostname("localhost.x", kLocalhostLookupPort, &addresses)); |
| 593 EXPECT_FALSE(ResolveLocalHostname("foo.localdomain", kLocalhostLookupPort, |
| 594 &addresses)); |
| 595 EXPECT_FALSE(ResolveLocalHostname("foo.localdomain.x", kLocalhostLookupPort, |
| 596 &addresses)); |
| 597 EXPECT_FALSE( |
| 598 ResolveLocalHostname("localhost6x", kLocalhostLookupPort, &addresses)); |
| 599 EXPECT_FALSE(ResolveLocalHostname("localhost.localdomain6", |
| 600 kLocalhostLookupPort, &addresses)); |
| 601 EXPECT_FALSE(ResolveLocalHostname("localhost6.localdomain", |
| 602 kLocalhostLookupPort, &addresses)); |
| 603 EXPECT_FALSE( |
| 604 ResolveLocalHostname("127.0.0.1.1", kLocalhostLookupPort, &addresses)); |
| 605 EXPECT_FALSE( |
| 606 ResolveLocalHostname(".127.0.0.255", kLocalhostLookupPort, &addresses)); |
| 607 EXPECT_FALSE(ResolveLocalHostname("::2", kLocalhostLookupPort, &addresses)); |
| 608 EXPECT_FALSE(ResolveLocalHostname("::1:1", kLocalhostLookupPort, &addresses)); |
| 609 EXPECT_FALSE(ResolveLocalHostname("0:0:0:0:1:0:0:1", kLocalhostLookupPort, |
| 610 &addresses)); |
| 611 EXPECT_FALSE(ResolveLocalHostname("::1:1", kLocalhostLookupPort, &addresses)); |
| 612 EXPECT_FALSE(ResolveLocalHostname("0:0:0:0:0:0:0:0:1", kLocalhostLookupPort, |
| 613 &addresses)); |
| 614 EXPECT_FALSE(ResolveLocalHostname("foo.localhost.com", kLocalhostLookupPort, |
| 615 &addresses)); |
| 616 EXPECT_FALSE( |
| 617 ResolveLocalHostname("foo.localhoste", kLocalhostLookupPort, &addresses)); |
| 618 } |
| 619 |
509 TEST(NetUtilTest, IsLocalhostTLD) { | 620 TEST(NetUtilTest, IsLocalhostTLD) { |
510 EXPECT_TRUE(IsLocalhostTLD("foo.localhost")); | 621 EXPECT_TRUE(IsLocalhostTLD("foo.localhost")); |
| 622 EXPECT_TRUE(IsLocalhostTLD("foo.localhoST")); |
511 EXPECT_TRUE(IsLocalhostTLD("foo.localhost.")); | 623 EXPECT_TRUE(IsLocalhostTLD("foo.localhost.")); |
| 624 EXPECT_TRUE(IsLocalhostTLD("foo.localhoST.")); |
512 EXPECT_FALSE(IsLocalhostTLD("foo.localhos")); | 625 EXPECT_FALSE(IsLocalhostTLD("foo.localhos")); |
513 EXPECT_FALSE(IsLocalhostTLD("foo.localhost.com")); | 626 EXPECT_FALSE(IsLocalhostTLD("foo.localhost.com")); |
514 EXPECT_FALSE(IsLocalhost("foo.localhoste")); | 627 EXPECT_FALSE(IsLocalhost("foo.localhoste")); |
515 } | 628 } |
516 | 629 |
517 TEST(NetUtilTest, GoogleHost) { | 630 TEST(NetUtilTest, GoogleHost) { |
518 struct GoogleHostCase { | 631 struct GoogleHostCase { |
519 GURL url; | 632 GURL url; |
520 bool expected_output; | 633 bool expected_output; |
521 }; | 634 }; |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
624 TEST_P(NetUtilNonUniqueNameTest, IsHostnameNonUnique) { | 737 TEST_P(NetUtilNonUniqueNameTest, IsHostnameNonUnique) { |
625 const NonUniqueNameTestData& test_data = GetParam(); | 738 const NonUniqueNameTestData& test_data = GetParam(); |
626 | 739 |
627 EXPECT_EQ(test_data.is_unique, IsUnique(test_data.hostname)); | 740 EXPECT_EQ(test_data.is_unique, IsUnique(test_data.hostname)); |
628 } | 741 } |
629 | 742 |
630 INSTANTIATE_TEST_CASE_P(, NetUtilNonUniqueNameTest, | 743 INSTANTIATE_TEST_CASE_P(, NetUtilNonUniqueNameTest, |
631 testing::ValuesIn(kNonUniqueNameTestData)); | 744 testing::ValuesIn(kNonUniqueNameTestData)); |
632 | 745 |
633 } // namespace net | 746 } // namespace net |
OLD | NEW |