Chromium Code Reviews| 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/host_resolver_impl.h" | 5 #include "net/base/host_resolver_impl.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 21 #include "base/time.h" | 21 #include "base/time.h" |
| 22 #include "net/base/address_list.h" | 22 #include "net/base/address_list.h" |
| 23 #include "net/base/completion_callback.h" | 23 #include "net/base/completion_callback.h" |
| 24 #include "net/base/host_cache.h" | 24 #include "net/base/host_cache.h" |
| 25 #include "net/base/mock_host_resolver.h" | 25 #include "net/base/mock_host_resolver.h" |
| 26 #include "net/base/net_errors.h" | 26 #include "net/base/net_errors.h" |
| 27 #include "net/base/net_log_unittest.h" | 27 #include "net/base/net_log_unittest.h" |
| 28 #include "net/base/net_util.h" | 28 #include "net/base/net_util.h" |
| 29 #include "net/base/sys_addrinfo.h" | 29 #include "net/base/sys_addrinfo.h" |
| 30 #include "net/base/test_completion_callback.h" | 30 #include "net/base/test_completion_callback.h" |
| 31 #include "net/dns/dns_client.h" | |
| 32 #include "net/dns/dns_test_util.h" | |
| 31 #include "testing/gtest/include/gtest/gtest.h" | 33 #include "testing/gtest/include/gtest/gtest.h" |
| 32 | 34 |
| 33 namespace net { | 35 namespace net { |
| 36 namespace { | |
| 34 | 37 |
| 35 using base::TimeDelta; | 38 using base::TimeDelta; |
| 36 using base::TimeTicks; | 39 using base::TimeTicks; |
| 37 | 40 |
| 38 static const size_t kMaxJobs = 10u; | 41 const size_t kMaxJobs = 10u; |
| 39 static const size_t kMaxRetryAttempts = 4u; | 42 const size_t kMaxRetryAttempts = 4u; |
| 40 | 43 |
| 41 PrioritizedDispatcher::Limits DefaultLimits() { | 44 PrioritizedDispatcher::Limits DefaultLimits() { |
| 42 PrioritizedDispatcher::Limits limits(NUM_PRIORITIES, kMaxJobs); | 45 PrioritizedDispatcher::Limits limits(NUM_PRIORITIES, kMaxJobs); |
| 43 return limits; | 46 return limits; |
| 44 } | 47 } |
| 45 | 48 |
| 46 HostResolverImpl::ProcTaskParams DefaultParams( | 49 HostResolverImpl::ProcTaskParams DefaultParams( |
| 47 HostResolverProc* resolver_proc) { | 50 HostResolverProc* resolver_proc) { |
| 48 return HostResolverImpl::ProcTaskParams(resolver_proc, | 51 return HostResolverImpl::ProcTaskParams(resolver_proc, |
| 49 kMaxRetryAttempts); | 52 kMaxRetryAttempts); |
| 50 } | 53 } |
| 51 | 54 |
| 52 HostResolverImpl* CreateHostResolverImpl(HostResolverProc* resolver_proc) { | 55 HostResolverImpl* CreateHostResolverImpl(HostResolverProc* resolver_proc) { |
| 53 return new HostResolverImpl( | 56 return new HostResolverImpl( |
| 54 HostCache::CreateDefaultCache(), | 57 HostCache::CreateDefaultCache(), |
| 55 DefaultLimits(), | 58 DefaultLimits(), |
| 56 DefaultParams(resolver_proc), | 59 DefaultParams(resolver_proc), |
| 57 scoped_ptr<DnsConfigService>(NULL), | 60 scoped_ptr<DnsConfigService>(NULL), |
| 58 NULL); | 61 NULL); |
| 59 } | 62 } |
| 60 | 63 |
| 64 HostResolverImpl* CreateHostResolverImplWithDns( | |
|
mmenke
2012/03/13 15:56:27
nit: Suggest "CreateHostResolverImplWithDnsConfig
| |
| 65 HostResolverProc* resolver_proc, | |
| 66 scoped_ptr<DnsConfigService> config_service) { | |
| 67 return new HostResolverImpl( | |
| 68 HostCache::CreateDefaultCache(), | |
| 69 DefaultLimits(), | |
| 70 DefaultParams(resolver_proc), | |
| 71 config_service.Pass(), | |
| 72 NULL); | |
| 73 } | |
| 74 | |
| 61 // This HostResolverImpl will only allow 1 outstanding resolve at a time. | 75 // This HostResolverImpl will only allow 1 outstanding resolve at a time. |
| 62 HostResolverImpl* CreateSerialHostResolverImpl( | 76 HostResolverImpl* CreateSerialHostResolverImpl( |
| 63 HostResolverProc* resolver_proc) { | 77 HostResolverProc* resolver_proc) { |
| 64 HostResolverImpl::ProcTaskParams params = DefaultParams(resolver_proc); | 78 HostResolverImpl::ProcTaskParams params = DefaultParams(resolver_proc); |
| 65 params.max_retry_attempts = 0u; | 79 params.max_retry_attempts = 0u; |
| 66 | 80 |
| 67 PrioritizedDispatcher::Limits limits(NUM_PRIORITIES, 1); | 81 PrioritizedDispatcher::Limits limits(NUM_PRIORITIES, 1); |
| 68 | 82 |
| 69 return new HostResolverImpl(HostCache::CreateDefaultCache(), | 83 return new HostResolverImpl(HostCache::CreateDefaultCache(), |
| 70 limits, | 84 limits, |
| (...skipping 343 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 414 | 428 |
| 415 ResolveRequest(HostResolver* resolver, | 429 ResolveRequest(HostResolver* resolver, |
| 416 const HostResolver::RequestInfo& info, | 430 const HostResolver::RequestInfo& info, |
| 417 Delegate* delegate) | 431 Delegate* delegate) |
| 418 : info_(info), resolver_(resolver), delegate_(delegate) { | 432 : info_(info), resolver_(resolver), delegate_(delegate) { |
| 419 // Start the request. | 433 // Start the request. |
| 420 int err = resolver->Resolve( | 434 int err = resolver->Resolve( |
| 421 info, &addrlist_, | 435 info, &addrlist_, |
| 422 base::Bind(&ResolveRequest::OnLookupFinished, | 436 base::Bind(&ResolveRequest::OnLookupFinished, |
| 423 base::Unretained(this)), | 437 base::Unretained(this)), |
| 424 &req_, BoundNetLog()); | 438 &req_, BoundNetLog()); |
|
mmenke
2012/03/13 15:56:27
nit: While you're here, indent of the above line
| |
| 425 EXPECT_EQ(ERR_IO_PENDING, err); | 439 EXPECT_EQ(ERR_IO_PENDING, err); |
| 426 } | 440 } |
| 427 | 441 |
| 442 ResolveRequest(HostResolver* resolver, | |
|
mmenke
2012/03/13 15:56:27
Think you should comment that this is only for syn
| |
| 443 const std::string& hostname, | |
| 444 int port) | |
| 445 : info_(HostPortPair(hostname, port)), | |
| 446 resolver_(resolver), | |
| 447 delegate_(NULL) { | |
| 448 // Start the request. | |
| 449 result_ = resolver->Resolve( | |
| 450 info_, &addrlist_, | |
| 451 base::Bind(&ResolveRequest::OnLookupFinished, base::Unretained(this)), | |
| 452 &req_, BoundNetLog()); | |
| 453 EXPECT_NE(ERR_IO_PENDING, result_); | |
| 454 } | |
| 455 | |
| 428 void Cancel() { | 456 void Cancel() { |
| 429 resolver_->CancelRequest(req_); | 457 resolver_->CancelRequest(req_); |
| 430 } | 458 } |
| 431 | 459 |
| 432 const std::string& hostname() const { | 460 const std::string& hostname() const { |
| 433 return info_.hostname(); | 461 return info_.hostname(); |
| 434 } | 462 } |
| 435 | 463 |
| 436 int port() const { | 464 int port() const { |
| 437 return info_.port(); | 465 return info_.port(); |
| 438 } | 466 } |
| 439 | 467 |
| 440 int result() const { | 468 int result() const { |
| 441 return result_; | 469 return result_; |
| 442 } | 470 } |
| 443 | 471 |
| 444 const AddressList& addrlist() const { | 472 const AddressList& addrlist() const { |
| 445 return addrlist_; | 473 return addrlist_; |
| 446 } | 474 } |
| 447 | 475 |
| 448 HostResolver* resolver() const { | 476 HostResolver* resolver() const { |
| 449 return resolver_; | 477 return resolver_; |
| 450 } | 478 } |
| 451 | 479 |
| 452 private: | 480 private: |
| 453 void OnLookupFinished(int result) { | 481 void OnLookupFinished(int result) { |
| 482 EXPECT_TRUE(delegate_ != NULL); | |
| 483 if (delegate_ == NULL) | |
| 484 return; | |
| 454 result_ = result; | 485 result_ = result; |
| 455 delegate_->OnCompleted(this); | 486 delegate_->OnCompleted(this); |
| 456 } | 487 } |
| 457 | 488 |
| 458 // The request details. | 489 // The request details. |
| 459 HostResolver::RequestInfo info_; | 490 HostResolver::RequestInfo info_; |
| 460 HostResolver::RequestHandle req_; | 491 HostResolver::RequestHandle req_; |
| 461 | 492 |
| 462 // The result of the resolve. | 493 // The result of the resolve. |
| 463 int result_; | 494 int result_; |
| (...skipping 399 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 863 req3.Cancel(); | 894 req3.Cancel(); |
| 864 req5.Cancel(); | 895 req5.Cancel(); |
| 865 | 896 |
| 866 // Ready, Set, GO!!! | 897 // Ready, Set, GO!!! |
| 867 resolver_proc->Signal(); | 898 resolver_proc->Signal(); |
| 868 | 899 |
| 869 // |verifier| will send quit message once all the requests have finished. | 900 // |verifier| will send quit message once all the requests have finished. |
| 870 MessageLoop::current()->Run(); | 901 MessageLoop::current()->Run(); |
| 871 } | 902 } |
| 872 | 903 |
| 873 // Helper class used by HostResolverImplTest.CanceledRequestsReleaseJobSlots. | 904 // Delegate which allows to wait for specific number of requests to complete. |
| 905 // Used by HostResolverImplTest.CanceledRequestsReleaseJobSlots and .DnsTask. | |
| 874 class CountingDelegate : public ResolveRequest::Delegate { | 906 class CountingDelegate : public ResolveRequest::Delegate { |
| 875 public: | 907 public: |
| 876 CountingDelegate() : num_completions_(0) {} | 908 CountingDelegate() : num_completions_(0) {} |
| 877 | 909 |
| 878 virtual void OnCompleted(ResolveRequest* resolve) OVERRIDE { | 910 virtual void OnCompleted(ResolveRequest* resolve) OVERRIDE { |
| 879 ++num_completions_; | 911 ++num_completions_; |
| 880 MessageLoop::current()->Quit(); | 912 MessageLoop::current()->Quit(); |
| 881 } | 913 } |
| 882 | 914 |
| 883 unsigned num_completions() const { return num_completions_; } | 915 unsigned num_completions() const { return num_completions_; } |
| 884 | 916 |
| 917 void WaitUntil(unsigned completions) { | |
|
mmenke
2012/03/13 15:56:27
optional nit: Suggest "WaitFor" or "WaitForComple
| |
| 918 while (num_completions_ < completions) | |
| 919 MessageLoop::current()->Run(); | |
|
mmenke
2012/03/13 15:56:27
Rather than repeatedly entering message loops agai
szym
2012/03/13 18:14:29
I think it's the other way around: because anyone
mmenke
2012/03/13 18:27:50
You don't want anyone else exiting the message loo
| |
| 920 } | |
| 921 | |
| 885 private: | 922 private: |
| 886 unsigned num_completions_; | 923 unsigned num_completions_; |
| 887 }; | 924 }; |
| 888 | 925 |
| 889 TEST_F(HostResolverImplTest, CanceledRequestsReleaseJobSlots) { | 926 TEST_F(HostResolverImplTest, CanceledRequestsReleaseJobSlots) { |
| 890 scoped_refptr<CountingHostResolverProc> resolver_proc( | 927 scoped_refptr<CountingHostResolverProc> resolver_proc( |
| 891 new CountingHostResolverProc(NULL)); | 928 new CountingHostResolverProc(NULL)); |
| 892 | 929 |
| 893 scoped_ptr<HostResolverImpl> host_resolver( | 930 scoped_ptr<HostResolverImpl> host_resolver( |
| 894 CreateHostResolverImpl(resolver_proc)); | 931 CreateHostResolverImpl(resolver_proc)); |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 911 // Cancel all but last two. | 948 // Cancel all but last two. |
| 912 for (unsigned i = 0; i < requests.size() - 2; ++i) { | 949 for (unsigned i = 0; i < requests.size() - 2; ++i) { |
| 913 requests[i]->Cancel(); | 950 requests[i]->Cancel(); |
| 914 } | 951 } |
| 915 | 952 |
| 916 EXPECT_TRUE(resolver_proc->WaitFor(kMaxJobs + 1)); | 953 EXPECT_TRUE(resolver_proc->WaitFor(kMaxJobs + 1)); |
| 917 EXPECT_EQ(0u, delegate.num_completions()); | 954 EXPECT_EQ(0u, delegate.num_completions()); |
| 918 | 955 |
| 919 resolver_proc->SignalAll(); | 956 resolver_proc->SignalAll(); |
| 920 | 957 |
| 921 while (delegate.num_completions() < 2) | 958 delegate.WaitUntil(2); |
| 922 MessageLoop::current()->Run(); | |
| 923 | 959 |
| 924 EXPECT_EQ(0u, host_resolver->num_running_jobs_for_tests()); | 960 EXPECT_EQ(0u, host_resolver->num_running_jobs_for_tests()); |
| 925 } | 961 } |
| 926 | 962 |
| 927 // Helper class used by HostResolverImplTest.CancelWithinCallback. | 963 // Helper class used by HostResolverImplTest.CancelWithinCallback. |
| 928 class CancelWithinCallbackVerifier : public ResolveRequest::Delegate { | 964 class CancelWithinCallbackVerifier : public ResolveRequest::Delegate { |
| 929 public: | 965 public: |
| 930 CancelWithinCallbackVerifier() | 966 CancelWithinCallbackVerifier() |
| 931 : req_to_cancel1_(NULL), req_to_cancel2_(NULL), num_completions_(0) { | 967 : req_to_cancel1_(NULL), req_to_cancel2_(NULL), num_completions_(0) { |
| 932 } | 968 } |
| (...skipping 810 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1743 // Resolve returns -4 to indicate that 3rd attempt has resolved the host. | 1779 // Resolve returns -4 to indicate that 3rd attempt has resolved the host. |
| 1744 EXPECT_EQ(-4, callback.WaitForResult()); | 1780 EXPECT_EQ(-4, callback.WaitForResult()); |
| 1745 | 1781 |
| 1746 resolver_proc->WaitForAllAttemptsToFinish(TimeDelta::FromMilliseconds(60000)); | 1782 resolver_proc->WaitForAllAttemptsToFinish(TimeDelta::FromMilliseconds(60000)); |
| 1747 MessageLoop::current()->RunAllPending(); | 1783 MessageLoop::current()->RunAllPending(); |
| 1748 | 1784 |
| 1749 EXPECT_EQ(resolver_proc->total_attempts_resolved(), kTotalAttempts); | 1785 EXPECT_EQ(resolver_proc->total_attempts_resolved(), kTotalAttempts); |
| 1750 EXPECT_EQ(resolver_proc->resolved_attempt_number(), kAttemptNumberToResolve); | 1786 EXPECT_EQ(resolver_proc->resolved_attempt_number(), kAttemptNumberToResolve); |
| 1751 } | 1787 } |
| 1752 | 1788 |
| 1789 DnsConfig CreateValidDnsConfig() { | |
| 1790 IPAddressNumber dns_ip; | |
| 1791 bool rv = ParseIPLiteralToNumber("192.168.1.0", &dns_ip); | |
| 1792 EXPECT_TRUE(rv); | |
| 1793 | |
| 1794 DnsConfig config; | |
| 1795 config.nameservers.push_back(IPEndPoint(dns_ip, | |
| 1796 dns_protocol::kDefaultPort)); | |
| 1797 EXPECT_TRUE(config.IsValid()); | |
| 1798 return config; | |
| 1799 } | |
| 1800 | |
| 1801 // Test successful and fallback resolutions in HostResolverImpl::DnsTask. | |
| 1802 TEST_F(HostResolverImplTest, DnsTask) { | |
| 1803 scoped_refptr<RuleBasedHostResolverProc> resolver_proc( | |
| 1804 new RuleBasedHostResolverProc(NULL)); | |
| 1805 scoped_ptr<HostResolverImpl> host_resolver(CreateHostResolverImpl( | |
| 1806 resolver_proc)); | |
| 1807 | |
| 1808 resolver_proc->AddRule("er_succeed", "192.168.1.101"); | |
| 1809 resolver_proc->AddRule("nx_succeed", "192.168.1.102"); | |
| 1810 resolver_proc->AddSimulatedFailure("ok_fail"); | |
| 1811 resolver_proc->AddSimulatedFailure("er_fail"); | |
| 1812 resolver_proc->AddSimulatedFailure("nx_fail"); | |
| 1813 | |
| 1814 CountingDelegate delegate; | |
| 1815 | |
| 1816 // Initially there is no config, so client should not be invoked. | |
| 1817 ResolveRequest req1(host_resolver.get(), "ok_fail", 80, &delegate); | |
| 1818 | |
| 1819 delegate.WaitUntil(1); | |
| 1820 EXPECT_EQ(ERR_NAME_NOT_RESOLVED, req1.result()); | |
| 1821 | |
| 1822 host_resolver->set_dns_client_for_tests( | |
| 1823 CreateMockDnsClient(CreateValidDnsConfig())); | |
| 1824 | |
| 1825 ResolveRequest req2(host_resolver.get(), "ok_fail", 80, &delegate); | |
| 1826 ResolveRequest req3(host_resolver.get(), "er_fail", 80, &delegate); | |
| 1827 ResolveRequest req4(host_resolver.get(), "nx_fail", 80, &delegate); | |
| 1828 ResolveRequest req5(host_resolver.get(), "er_succeed", 80, &delegate); | |
| 1829 ResolveRequest req6(host_resolver.get(), "nx_succeed", 80, &delegate); | |
| 1830 | |
| 1831 delegate.WaitUntil(6); | |
| 1832 EXPECT_EQ(OK, req2.result()); | |
|
mmenke
2012/03/13 15:56:27
Should the OK checks be ASSERTs to prevent crashin
| |
| 1833 // Resolved by MockDnsClient. | |
| 1834 EXPECT_EQ("127.0.0.1", NetAddressToString(req2.addrlist().head())); | |
| 1835 EXPECT_EQ(ERR_NAME_NOT_RESOLVED, req3.result()); | |
| 1836 EXPECT_EQ(ERR_NAME_NOT_RESOLVED, req4.result()); | |
| 1837 EXPECT_EQ(OK, req5.result()); | |
| 1838 EXPECT_EQ("192.168.1.101", NetAddressToString(req5.addrlist().head())); | |
| 1839 EXPECT_EQ(OK, req6.result()); | |
| 1840 EXPECT_EQ("192.168.1.102", NetAddressToString(req6.addrlist().head())); | |
| 1841 } | |
| 1842 | |
| 1843 TEST_F(HostResolverImplTest, ServeFromHosts) { | |
| 1844 scoped_refptr<RuleBasedHostResolverProc> resolver_proc( | |
| 1845 new RuleBasedHostResolverProc(NULL)); | |
| 1846 MockDnsConfigService* config_service = new MockDnsConfigService(); | |
| 1847 scoped_ptr<HostResolverImpl> host_resolver(CreateHostResolverImplWithDns( | |
| 1848 resolver_proc, | |
| 1849 scoped_ptr<DnsConfigService>(config_service))); | |
| 1850 | |
| 1851 resolver_proc->AddSimulatedFailure("*"); | |
| 1852 | |
| 1853 DnsConfig config = CreateValidDnsConfig(); | |
| 1854 host_resolver->set_dns_client_for_tests(CreateMockDnsClient(config)); | |
| 1855 | |
| 1856 CountingDelegate delegate; | |
| 1857 | |
| 1858 ResolveRequest req1(host_resolver.get(), "er_ipv4", 80, &delegate); | |
| 1859 delegate.WaitUntil(1); | |
| 1860 EXPECT_EQ(ERR_NAME_NOT_RESOLVED, req1.result()); | |
| 1861 | |
| 1862 IPAddressNumber local_ipv4, local_ipv6; | |
| 1863 ASSERT_TRUE(ParseIPLiteralToNumber("127.0.0.1", &local_ipv4)); | |
| 1864 ASSERT_TRUE(ParseIPLiteralToNumber("::1", &local_ipv6)); | |
| 1865 | |
| 1866 DnsHosts hosts; | |
| 1867 hosts[DnsHostsKey("er_ipv4", ADDRESS_FAMILY_IPV4)] = local_ipv4; | |
| 1868 hosts[DnsHostsKey("er_ipv6", ADDRESS_FAMILY_IPV6)] = local_ipv6; | |
|
mmenke
2012/03/13 15:56:27
Suggest you add two or three ADDRESS_FAMILY_UNSPEC
szym
2012/03/13 18:14:29
DnsHosts will not have keys with ADDRESS_FAMILY_UN
mmenke
2012/03/13 18:27:50
Sorry, I was a little confused... You should have
szym
2012/03/13 19:04:00
I don't want to test the IPv4 preference unless we
| |
| 1869 | |
| 1870 config_service->ChangeConfig(config); | |
| 1871 config_service->ChangeHosts(hosts); | |
| 1872 | |
| 1873 ResolveRequest req2(host_resolver.get(), "er_ipv4", 80); | |
| 1874 EXPECT_EQ(OK, req2.result()); | |
| 1875 EXPECT_EQ("127.0.0.1", NetAddressToString(req2.addrlist().head())); | |
| 1876 ResolveRequest req3(host_resolver.get(), "er_ipv6", 80); | |
| 1877 EXPECT_EQ(OK, req3.result()); | |
| 1878 EXPECT_EQ("::1", NetAddressToString(req3.addrlist().head())); | |
| 1879 } | |
| 1880 | |
| 1881 // TODO(szym): Test AbortAllInProgressJobs due to DnsConfig change. | |
| 1882 | |
| 1753 // TODO(cbentzel): Test a mix of requests with different HostResolverFlags. | 1883 // TODO(cbentzel): Test a mix of requests with different HostResolverFlags. |
| 1754 | 1884 |
| 1885 } // namespace | |
| 1755 } // namespace net | 1886 } // namespace net |
| OLD | NEW |