| 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/dns/host_resolver_impl.h" | 5 #include "net/dns/host_resolver_impl.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| (...skipping 562 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 573 Request* req = CreateRequest("just.testing", 80); | 573 Request* req = CreateRequest("just.testing", 80); |
| 574 EXPECT_EQ(ERR_IO_PENDING, req->Resolve()); | 574 EXPECT_EQ(ERR_IO_PENDING, req->Resolve()); |
| 575 EXPECT_EQ(OK, req->WaitForResult()); | 575 EXPECT_EQ(OK, req->WaitForResult()); |
| 576 | 576 |
| 577 EXPECT_TRUE(req->HasOneAddress("192.168.1.42", 80)); | 577 EXPECT_TRUE(req->HasOneAddress("192.168.1.42", 80)); |
| 578 | 578 |
| 579 EXPECT_EQ("just.testing", proc_->GetCaptureList()[0].hostname); | 579 EXPECT_EQ("just.testing", proc_->GetCaptureList()[0].hostname); |
| 580 } | 580 } |
| 581 | 581 |
| 582 TEST_F(HostResolverImplTest, LocalhostLookup) { | 582 TEST_F(HostResolverImplTest, LocalhostLookup) { |
| 583 // Add a rule resolving "foo.localhost" to a non-loopback IP and test |
| 584 // that "foo.localhost" still resolves to loopback. |
| 585 proc_->AddRuleForAllFamilies("foo.localhost", "192.168.1.42"); |
| 586 Request* req0 = CreateRequest("foo.localhost", 80); |
| 587 EXPECT_EQ(OK, req0->Resolve()); |
| 588 |
| 589 EXPECT_TRUE(req0->HasAddress("127.0.0.1", 80)); |
| 590 EXPECT_TRUE(req0->HasAddress("::1", 80)); |
| 591 |
| 592 Request* req1 = CreateRequest("localhost6", 80, MEDIUM, ADDRESS_FAMILY_IPV4); |
| 593 EXPECT_EQ(OK, req1->Resolve()); |
| 594 EXPECT_EQ(0u, req1->NumberOfAddresses()); |
| 595 |
| 596 Request* req2 = CreateRequest("localhost6", 80, MEDIUM, ADDRESS_FAMILY_IPV6); |
| 597 EXPECT_EQ(OK, req2->Resolve()); |
| 598 EXPECT_TRUE(req2->HasOneAddress("::1", 80)); |
| 599 |
| 600 Request* req3 = |
| 601 CreateRequest("localhost6", 80, MEDIUM, ADDRESS_FAMILY_UNSPECIFIED); |
| 602 EXPECT_EQ(OK, req3->Resolve()); |
| 603 EXPECT_TRUE(req3->HasOneAddress("::1", 80)); |
| 604 |
| 605 Request* req4 = CreateRequest("localhost", 80, MEDIUM, ADDRESS_FAMILY_IPV4); |
| 606 EXPECT_EQ(OK, req4->Resolve()); |
| 607 EXPECT_TRUE(req4->HasOneAddress("127.0.0.1", 80)); |
| 608 |
| 609 Request* req5 = CreateRequest("localhost", 80, MEDIUM, ADDRESS_FAMILY_IPV6); |
| 610 EXPECT_EQ(OK, req5->Resolve()); |
| 611 EXPECT_TRUE(req5->HasOneAddress("::1", 80)); |
| 612 } |
| 613 |
| 614 // RFC 6761 localhost names should always resolve to loopback. |
| 615 TEST_F(HostResolverImplTest, LocalhostShortCircuitsToLoopback) { |
| 583 proc_->SignalMultiple(1u); | 616 proc_->SignalMultiple(1u); |
| 584 Request* req = CreateRequest("foo.localhost", 80); | 617 proc_->AddRuleForAllFamilies("localhost", "192.168.1.42"); |
| 585 EXPECT_EQ(ERR_IO_PENDING, req->Resolve()); | 618 proc_->AddRuleForAllFamilies("localhost.", "192.168.1.42"); |
| 586 EXPECT_EQ(OK, req->WaitForResult()); | |
| 587 | 619 |
| 588 EXPECT_TRUE(req->HasOneAddress("127.0.0.1", 80)); | 620 Request* req0 = CreateRequest("localhost", 80); |
| 621 EXPECT_EQ(OK, req0->Resolve()); |
| 622 EXPECT_TRUE(req0->HasAddress("127.0.0.1", 80)); |
| 623 EXPECT_TRUE(req0->HasAddress("::1", 80)); |
| 589 | 624 |
| 590 EXPECT_EQ("localhost.", proc_->GetCaptureList()[0].hostname); | 625 Request* req1 = CreateRequest("localhost.", 80); |
| 626 EXPECT_EQ(OK, req1->Resolve()); |
| 627 EXPECT_TRUE(req1->HasAddress("127.0.0.1", 80)); |
| 628 EXPECT_TRUE(req1->HasAddress("::1", 80)); |
| 629 |
| 630 Request* req2 = CreateRequest("foo.localhost", 80); |
| 631 EXPECT_EQ(OK, req2->Resolve()); |
| 632 EXPECT_TRUE(req2->HasAddress("127.0.0.1", 80)); |
| 633 EXPECT_TRUE(req2->HasAddress("::1", 80)); |
| 591 } | 634 } |
| 592 | 635 |
| 593 TEST_F(HostResolverImplTest, EmptyListMeansNameNotResolved) { | 636 TEST_F(HostResolverImplTest, EmptyListMeansNameNotResolved) { |
| 594 proc_->AddRuleForAllFamilies("just.testing", ""); | 637 proc_->AddRuleForAllFamilies("just.testing", ""); |
| 595 proc_->SignalMultiple(1u); | 638 proc_->SignalMultiple(1u); |
| 596 | 639 |
| 597 Request* req = CreateRequest("just.testing", 80); | 640 Request* req = CreateRequest("just.testing", 80); |
| 598 EXPECT_EQ(ERR_IO_PENDING, req->Resolve()); | 641 EXPECT_EQ(ERR_IO_PENDING, req->Resolve()); |
| 599 EXPECT_EQ(ERR_NAME_NOT_RESOLVED, req->WaitForResult()); | 642 EXPECT_EQ(ERR_NAME_NOT_RESOLVED, req->WaitForResult()); |
| 600 EXPECT_EQ(0u, req->NumberOfAddresses()); | 643 EXPECT_EQ(0u, req->NumberOfAddresses()); |
| (...skipping 1179 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1780 | 1823 |
| 1781 // Try without DnsClient. | 1824 // Try without DnsClient. |
| 1782 DnsConfig config = CreateValidDnsConfig(); | 1825 DnsConfig config = CreateValidDnsConfig(); |
| 1783 config.use_local_ipv6 = false; | 1826 config.use_local_ipv6 = false; |
| 1784 ChangeDnsConfig(config); | 1827 ChangeDnsConfig(config); |
| 1785 HostResolver::RequestInfo info_proc(HostPortPair("localhost", 80)); | 1828 HostResolver::RequestInfo info_proc(HostPortPair("localhost", 80)); |
| 1786 info_proc.set_address_family(ADDRESS_FAMILY_UNSPECIFIED); | 1829 info_proc.set_address_family(ADDRESS_FAMILY_UNSPECIFIED); |
| 1787 info_proc.set_host_resolver_flags(HOST_RESOLVER_SYSTEM_ONLY); | 1830 info_proc.set_host_resolver_flags(HOST_RESOLVER_SYSTEM_ONLY); |
| 1788 Request* req = CreateRequest(info_proc, DEFAULT_PRIORITY); | 1831 Request* req = CreateRequest(info_proc, DEFAULT_PRIORITY); |
| 1789 | 1832 |
| 1790 // It is resolved via getaddrinfo, so expect asynchronous result. | 1833 EXPECT_EQ(OK, req->Resolve()); |
| 1791 EXPECT_EQ(ERR_IO_PENDING, req->Resolve()); | |
| 1792 EXPECT_EQ(OK, req->WaitForResult()); | |
| 1793 | 1834 |
| 1794 EXPECT_EQ(saw_ipv4, req->HasAddress("127.0.0.1", 80)); | 1835 EXPECT_TRUE(req->HasAddress("127.0.0.1", 80)); |
| 1795 EXPECT_EQ(saw_ipv6, req->HasAddress("::1", 80)); | 1836 EXPECT_TRUE(req->HasAddress("::1", 80)); |
| 1796 | 1837 |
| 1797 // Configure DnsClient with dual-host HOSTS file. | 1838 // Configure DnsClient with dual-host HOSTS file. |
| 1798 DnsConfig config_hosts = CreateValidDnsConfig(); | 1839 DnsConfig config_hosts = CreateValidDnsConfig(); |
| 1799 DnsHosts hosts; | 1840 DnsHosts hosts; |
| 1800 IPAddressNumber local_ipv4, local_ipv6; | 1841 IPAddressNumber local_ipv4, local_ipv6; |
| 1801 ASSERT_TRUE(ParseIPLiteralToNumber("127.0.0.1", &local_ipv4)); | 1842 ASSERT_TRUE(ParseIPLiteralToNumber("127.0.0.1", &local_ipv4)); |
| 1802 ASSERT_TRUE(ParseIPLiteralToNumber("::1", &local_ipv6)); | 1843 ASSERT_TRUE(ParseIPLiteralToNumber("::1", &local_ipv6)); |
| 1803 if (saw_ipv4) | 1844 if (saw_ipv4) |
| 1804 hosts[DnsHostsKey("localhost", ADDRESS_FAMILY_IPV4)] = local_ipv4; | 1845 hosts[DnsHostsKey("localhost", ADDRESS_FAMILY_IPV4)] = local_ipv4; |
| 1805 if (saw_ipv6) | 1846 if (saw_ipv6) |
| (...skipping 326 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2132 | 2173 |
| 2133 EXPECT_EQ(OK, requests_[0]->WaitForResult()); | 2174 EXPECT_EQ(OK, requests_[0]->WaitForResult()); |
| 2134 EXPECT_TRUE(requests_[0]->HasOneAddress("192.168.0.1", 80)); | 2175 EXPECT_TRUE(requests_[0]->HasOneAddress("192.168.0.1", 80)); |
| 2135 EXPECT_EQ(OK, requests_[1]->WaitForResult()); | 2176 EXPECT_EQ(OK, requests_[1]->WaitForResult()); |
| 2136 EXPECT_TRUE(requests_[1]->HasOneAddress("192.168.0.2", 80)); | 2177 EXPECT_TRUE(requests_[1]->HasOneAddress("192.168.0.2", 80)); |
| 2137 EXPECT_EQ(OK, requests_[2]->WaitForResult()); | 2178 EXPECT_EQ(OK, requests_[2]->WaitForResult()); |
| 2138 EXPECT_TRUE(requests_[2]->HasOneAddress("192.168.0.3", 80)); | 2179 EXPECT_TRUE(requests_[2]->HasOneAddress("192.168.0.3", 80)); |
| 2139 } | 2180 } |
| 2140 | 2181 |
| 2141 } // namespace net | 2182 } // namespace net |
| OLD | NEW |