Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(246)

Side by Side Diff: net/dns/host_resolver_impl_unittest.cc

Issue 1177933002: Resolve RFC 6761 localhost names to loopback (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: style tweaks, simplification Created 5 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« net/dns/host_resolver_impl.cc ('K') | « net/dns/host_resolver_impl.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 561 matching lines...) Expand 10 before | Expand all | Expand 10 after
572 572
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 // RFC 6761 localhost names should always resolve to loopback.
582 TEST_F(HostResolverImplTest, LocalhostLookup) { 583 TEST_F(HostResolverImplTest, LocalhostLookup) {
583 proc_->SignalMultiple(1u); 584 // Add a rule resolving localhost names to a non-loopback IP and test
584 Request* req = CreateRequest("foo.localhost", 80); 585 // that they still resolves to loopback.
585 EXPECT_EQ(ERR_IO_PENDING, req->Resolve()); 586 proc_->AddRuleForAllFamilies("foo.localhost", "192.168.1.42");
586 EXPECT_EQ(OK, req->WaitForResult()); 587 proc_->AddRuleForAllFamilies("localhost", "192.168.1.42");
588 proc_->AddRuleForAllFamilies("localhost.", "192.168.1.42");
587 589
588 EXPECT_TRUE(req->HasOneAddress("127.0.0.1", 80)); 590 Request* req0 = CreateRequest("foo.localhost", 80);
591 EXPECT_EQ(OK, req0->Resolve());
592 EXPECT_TRUE(req0->HasAddress("127.0.0.1", 80));
593 EXPECT_TRUE(req0->HasAddress("::1", 80));
589 594
590 EXPECT_EQ("localhost.", proc_->GetCaptureList()[0].hostname); 595 Request* req1 = CreateRequest("localhost", 80);
596 EXPECT_EQ(OK, req1->Resolve());
597 EXPECT_TRUE(req1->HasAddress("127.0.0.1", 80));
598 EXPECT_TRUE(req1->HasAddress("::1", 80));
599
600 Request* req2 = CreateRequest("localhost.", 80);
601 EXPECT_EQ(OK, req2->Resolve());
602 EXPECT_TRUE(req2->HasAddress("127.0.0.1", 80));
603 EXPECT_TRUE(req2->HasAddress("::1", 80));
604 }
605
606 TEST_F(HostResolverImplTest, LocalhostIPV4IPV6Lookup) {
607 Request* req1 = CreateRequest("localhost6", 80, MEDIUM, ADDRESS_FAMILY_IPV4);
608 EXPECT_EQ(OK, req1->Resolve());
609 EXPECT_EQ(0u, req1->NumberOfAddresses());
610
611 Request* req2 = CreateRequest("localhost6", 80, MEDIUM, ADDRESS_FAMILY_IPV6);
612 EXPECT_EQ(OK, req2->Resolve());
613 EXPECT_TRUE(req2->HasOneAddress("::1", 80));
614
615 Request* req3 =
616 CreateRequest("localhost6", 80, MEDIUM, ADDRESS_FAMILY_UNSPECIFIED);
617 EXPECT_EQ(OK, req3->Resolve());
618 EXPECT_TRUE(req3->HasOneAddress("::1", 80));
619
620 Request* req4 = CreateRequest("localhost", 80, MEDIUM, ADDRESS_FAMILY_IPV4);
621 EXPECT_EQ(OK, req4->Resolve());
622 EXPECT_TRUE(req4->HasOneAddress("127.0.0.1", 80));
623
624 Request* req5 = CreateRequest("localhost", 80, MEDIUM, ADDRESS_FAMILY_IPV6);
625 EXPECT_EQ(OK, req5->Resolve());
626 EXPECT_TRUE(req5->HasOneAddress("::1", 80));
591 } 627 }
592 628
593 TEST_F(HostResolverImplTest, EmptyListMeansNameNotResolved) { 629 TEST_F(HostResolverImplTest, EmptyListMeansNameNotResolved) {
594 proc_->AddRuleForAllFamilies("just.testing", ""); 630 proc_->AddRuleForAllFamilies("just.testing", "");
595 proc_->SignalMultiple(1u); 631 proc_->SignalMultiple(1u);
596 632
597 Request* req = CreateRequest("just.testing", 80); 633 Request* req = CreateRequest("just.testing", 80);
598 EXPECT_EQ(ERR_IO_PENDING, req->Resolve()); 634 EXPECT_EQ(ERR_IO_PENDING, req->Resolve());
599 EXPECT_EQ(ERR_NAME_NOT_RESOLVED, req->WaitForResult()); 635 EXPECT_EQ(ERR_NAME_NOT_RESOLVED, req->WaitForResult());
600 EXPECT_EQ(0u, req->NumberOfAddresses()); 636 EXPECT_EQ(0u, req->NumberOfAddresses());
(...skipping 1179 matching lines...) Expand 10 before | Expand all | Expand 10 after
1780 1816
1781 // Try without DnsClient. 1817 // Try without DnsClient.
1782 DnsConfig config = CreateValidDnsConfig(); 1818 DnsConfig config = CreateValidDnsConfig();
1783 config.use_local_ipv6 = false; 1819 config.use_local_ipv6 = false;
1784 ChangeDnsConfig(config); 1820 ChangeDnsConfig(config);
1785 HostResolver::RequestInfo info_proc(HostPortPair("localhost", 80)); 1821 HostResolver::RequestInfo info_proc(HostPortPair("localhost", 80));
1786 info_proc.set_address_family(ADDRESS_FAMILY_UNSPECIFIED); 1822 info_proc.set_address_family(ADDRESS_FAMILY_UNSPECIFIED);
1787 info_proc.set_host_resolver_flags(HOST_RESOLVER_SYSTEM_ONLY); 1823 info_proc.set_host_resolver_flags(HOST_RESOLVER_SYSTEM_ONLY);
1788 Request* req = CreateRequest(info_proc, DEFAULT_PRIORITY); 1824 Request* req = CreateRequest(info_proc, DEFAULT_PRIORITY);
1789 1825
1790 // It is resolved via getaddrinfo, so expect asynchronous result. 1826 EXPECT_EQ(OK, req->Resolve());
1791 EXPECT_EQ(ERR_IO_PENDING, req->Resolve());
1792 EXPECT_EQ(OK, req->WaitForResult());
1793 1827
1794 EXPECT_EQ(saw_ipv4, req->HasAddress("127.0.0.1", 80)); 1828 EXPECT_TRUE(req->HasAddress("127.0.0.1", 80));
1795 EXPECT_EQ(saw_ipv6, req->HasAddress("::1", 80)); 1829 EXPECT_TRUE(req->HasAddress("::1", 80));
1796 1830
1797 // Configure DnsClient with dual-host HOSTS file. 1831 // Configure DnsClient with dual-host HOSTS file.
1798 DnsConfig config_hosts = CreateValidDnsConfig(); 1832 DnsConfig config_hosts = CreateValidDnsConfig();
1799 DnsHosts hosts; 1833 DnsHosts hosts;
1800 IPAddressNumber local_ipv4, local_ipv6; 1834 IPAddressNumber local_ipv4, local_ipv6;
1801 ASSERT_TRUE(ParseIPLiteralToNumber("127.0.0.1", &local_ipv4)); 1835 ASSERT_TRUE(ParseIPLiteralToNumber("127.0.0.1", &local_ipv4));
1802 ASSERT_TRUE(ParseIPLiteralToNumber("::1", &local_ipv6)); 1836 ASSERT_TRUE(ParseIPLiteralToNumber("::1", &local_ipv6));
1803 if (saw_ipv4) 1837 if (saw_ipv4)
1804 hosts[DnsHostsKey("localhost", ADDRESS_FAMILY_IPV4)] = local_ipv4; 1838 hosts[DnsHostsKey("localhost", ADDRESS_FAMILY_IPV4)] = local_ipv4;
1805 if (saw_ipv6) 1839 if (saw_ipv6)
(...skipping 326 matching lines...) Expand 10 before | Expand all | Expand 10 after
2132 2166
2133 EXPECT_EQ(OK, requests_[0]->WaitForResult()); 2167 EXPECT_EQ(OK, requests_[0]->WaitForResult());
2134 EXPECT_TRUE(requests_[0]->HasOneAddress("192.168.0.1", 80)); 2168 EXPECT_TRUE(requests_[0]->HasOneAddress("192.168.0.1", 80));
2135 EXPECT_EQ(OK, requests_[1]->WaitForResult()); 2169 EXPECT_EQ(OK, requests_[1]->WaitForResult());
2136 EXPECT_TRUE(requests_[1]->HasOneAddress("192.168.0.2", 80)); 2170 EXPECT_TRUE(requests_[1]->HasOneAddress("192.168.0.2", 80));
2137 EXPECT_EQ(OK, requests_[2]->WaitForResult()); 2171 EXPECT_EQ(OK, requests_[2]->WaitForResult());
2138 EXPECT_TRUE(requests_[2]->HasOneAddress("192.168.0.3", 80)); 2172 EXPECT_TRUE(requests_[2]->HasOneAddress("192.168.0.3", 80));
2139 } 2173 }
2140 2174
2141 } // namespace net 2175 } // namespace net
OLDNEW
« net/dns/host_resolver_impl.cc ('K') | « net/dns/host_resolver_impl.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698