| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/async_host_resolver.h" | 5 #include "net/base/async_host_resolver.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/scoped_ptr.h" | 8 #include "base/scoped_ptr.h" |
| 9 #include "net/base/dns_test_util.h" | 9 #include "net/base/dns_test_util.h" |
| 10 #include "net/base/host_cache.h" |
| 10 #include "net/base/net_log.h" | 11 #include "net/base/net_log.h" |
| 11 #include "net/base/rand_callback.h" | 12 #include "net/base/rand_callback.h" |
| 12 #include "net/base/sys_addrinfo.h" | 13 #include "net/base/sys_addrinfo.h" |
| 13 #include "net/socket/socket_test_util.h" | 14 #include "net/socket/socket_test_util.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
| 15 | 16 |
| 16 namespace net { | 17 namespace net { |
| 17 | 18 |
| 18 namespace { | 19 namespace { |
| 19 | 20 |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 64 ip_addresses0_(kT0IpAddresses, | 65 ip_addresses0_(kT0IpAddresses, |
| 65 kT0IpAddresses + arraysize(kT0IpAddresses)), | 66 kT0IpAddresses + arraysize(kT0IpAddresses)), |
| 66 ip_addresses1_(kT1IpAddresses, | 67 ip_addresses1_(kT1IpAddresses, |
| 67 kT1IpAddresses + arraysize(kT1IpAddresses)), | 68 kT1IpAddresses + arraysize(kT1IpAddresses)), |
| 68 ip_addresses2_(kT2IpAddresses, | 69 ip_addresses2_(kT2IpAddresses, |
| 69 kT2IpAddresses + arraysize(kT2IpAddresses)), | 70 kT2IpAddresses + arraysize(kT2IpAddresses)), |
| 70 ip_addresses3_(kT3IpAddresses, | 71 ip_addresses3_(kT3IpAddresses, |
| 71 kT3IpAddresses + arraysize(kT3IpAddresses)), | 72 kT3IpAddresses + arraysize(kT3IpAddresses)), |
| 72 test_prng_(std::deque<int>( | 73 test_prng_(std::deque<int>( |
| 73 transaction_ids, transaction_ids + arraysize(transaction_ids))) { | 74 transaction_ids, transaction_ids + arraysize(transaction_ids))) { |
| 74 | |
| 75 rand_int_cb_ = base::Bind(&TestPrng::GetNext, | 75 rand_int_cb_ = base::Bind(&TestPrng::GetNext, |
| 76 base::Unretained(&test_prng_)); | 76 base::Unretained(&test_prng_)); |
| 77 // AF_INET only for now. | 77 // AF_INET only for now. |
| 78 info0_.set_address_family(ADDRESS_FAMILY_IPV4); | 78 info0_.set_address_family(ADDRESS_FAMILY_IPV4); |
| 79 info1_.set_address_family(ADDRESS_FAMILY_IPV4); | 79 info1_.set_address_family(ADDRESS_FAMILY_IPV4); |
| 80 info2_.set_address_family(ADDRESS_FAMILY_IPV4); | 80 info2_.set_address_family(ADDRESS_FAMILY_IPV4); |
| 81 info3_.set_address_family(ADDRESS_FAMILY_IPV4); | 81 info3_.set_address_family(ADDRESS_FAMILY_IPV4); |
| 82 | 82 |
| 83 // Setup socket read/writes for transaction 0. | 83 // Setup socket read/writes for transaction 0. |
| 84 writes0_.push_back( | 84 writes0_.push_back( |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 125 factory_.AddSocketDataProvider(data2_.get()); | 125 factory_.AddSocketDataProvider(data2_.get()); |
| 126 factory_.AddSocketDataProvider(data3_.get()); | 126 factory_.AddSocketDataProvider(data3_.get()); |
| 127 | 127 |
| 128 IPEndPoint dns_server; | 128 IPEndPoint dns_server; |
| 129 bool rv0 = CreateDnsAddress(kDnsIp, kDnsPort, &dns_server); | 129 bool rv0 = CreateDnsAddress(kDnsIp, kDnsPort, &dns_server); |
| 130 DCHECK(rv0); | 130 DCHECK(rv0); |
| 131 | 131 |
| 132 resolver_.reset( | 132 resolver_.reset( |
| 133 new AsyncHostResolver( | 133 new AsyncHostResolver( |
| 134 dns_server, kMaxTransactions, kMaxPendingRequests, rand_int_cb_, | 134 dns_server, kMaxTransactions, kMaxPendingRequests, rand_int_cb_, |
| 135 &factory_, NULL)); | 135 HostCache::CreateDefaultCache(), &factory_, NULL)); |
| 136 } | 136 } |
| 137 | 137 |
| 138 protected: | 138 protected: |
| 139 AddressList addrlist0_, addrlist1_, addrlist2_, addrlist3_; | 139 AddressList addrlist0_, addrlist1_, addrlist2_, addrlist3_; |
| 140 HostResolver::RequestInfo info0_, info1_, info2_, info3_; | 140 HostResolver::RequestInfo info0_, info1_, info2_, info3_; |
| 141 std::vector<MockWrite> writes0_, writes1_, writes2_, writes3_; | 141 std::vector<MockWrite> writes0_, writes1_, writes2_, writes3_; |
| 142 std::vector<MockRead> reads0_, reads1_, reads2_, reads3_; | 142 std::vector<MockRead> reads0_, reads1_, reads2_, reads3_; |
| 143 scoped_ptr<StaticSocketDataProvider> data0_, data1_, data2_, data3_; | 143 scoped_ptr<StaticSocketDataProvider> data0_, data1_, data2_, data3_; |
| 144 std::vector<const char*> ip_addresses0_, ip_addresses1_, | 144 std::vector<const char*> ip_addresses0_, ip_addresses1_, |
| 145 ip_addresses2_, ip_addresses3_; | 145 ip_addresses2_, ip_addresses3_; |
| (...skipping 24 matching lines...) Expand all Loading... |
| 170 } | 170 } |
| 171 | 171 |
| 172 TEST_F(AsyncHostResolverTest, IPv6LiteralLookup) { | 172 TEST_F(AsyncHostResolverTest, IPv6LiteralLookup) { |
| 173 info0_.set_host_port_pair(HostPortPair("2001:db8:0::42", kPortNum)); | 173 info0_.set_host_port_pair(HostPortPair("2001:db8:0::42", kPortNum)); |
| 174 int rv = resolver_->Resolve(info0_, &addrlist0_, &callback0_, NULL, | 174 int rv = resolver_->Resolve(info0_, &addrlist0_, &callback0_, NULL, |
| 175 BoundNetLog()); | 175 BoundNetLog()); |
| 176 // When support for IPv6 is added, this should succeed. | 176 // When support for IPv6 is added, this should succeed. |
| 177 EXPECT_EQ(ERR_NAME_NOT_RESOLVED, rv); | 177 EXPECT_EQ(ERR_NAME_NOT_RESOLVED, rv); |
| 178 } | 178 } |
| 179 | 179 |
| 180 TEST_F(AsyncHostResolverTest, CachedOnlyLookup) { | 180 TEST_F(AsyncHostResolverTest, CachedLookup) { |
| 181 info0_.set_only_use_cached_response(true); | 181 info0_.set_only_use_cached_response(true); |
| 182 int rv = resolver_->Resolve(info0_, &addrlist0_, &callback0_, NULL, | 182 int rv = resolver_->Resolve(info0_, &addrlist0_, NULL, NULL, |
| 183 BoundNetLog()); | 183 BoundNetLog()); |
| 184 // When caching is added, this should succeed. | |
| 185 EXPECT_EQ(ERR_NAME_NOT_RESOLVED, rv); | 184 EXPECT_EQ(ERR_NAME_NOT_RESOLVED, rv); |
| 185 |
| 186 // Cache the result of |info0_| lookup. |
| 187 info0_.set_only_use_cached_response(false); |
| 188 rv = resolver_->Resolve(info0_, &addrlist0_, &callback0_, NULL, |
| 189 BoundNetLog()); |
| 190 EXPECT_EQ(ERR_IO_PENDING, rv); |
| 191 rv = callback0_.WaitForResult(); |
| 192 EXPECT_EQ(OK, rv); |
| 193 VerifyAddressList(ip_addresses0_, kPortNum, addrlist0_); |
| 194 |
| 195 // Now lookup |info0_| from cache only, store results in |addrlist1_|, |
| 196 // should succeed synchronously. |
| 197 info0_.set_only_use_cached_response(true); |
| 198 rv = resolver_->Resolve(info0_, &addrlist1_, NULL, NULL, |
| 199 BoundNetLog()); |
| 200 EXPECT_EQ(OK, rv); |
| 201 VerifyAddressList(ip_addresses0_, kPortNum, addrlist1_); |
| 186 } | 202 } |
| 187 | 203 |
| 188 TEST_F(AsyncHostResolverTest, InvalidHostNameLookup) { | 204 TEST_F(AsyncHostResolverTest, InvalidHostNameLookup) { |
| 189 const std::string kHostName1(64, 'a'); | 205 const std::string kHostName1(64, 'a'); |
| 190 info0_.set_host_port_pair(HostPortPair(kHostName1, kPortNum)); | 206 info0_.set_host_port_pair(HostPortPair(kHostName1, kPortNum)); |
| 191 int rv = resolver_->Resolve(info0_, &addrlist0_, &callback0_, NULL, | 207 int rv = resolver_->Resolve(info0_, &addrlist0_, &callback0_, NULL, |
| 192 BoundNetLog()); | 208 BoundNetLog()); |
| 193 EXPECT_EQ(ERR_NAME_NOT_RESOLVED, rv); | 209 EXPECT_EQ(ERR_NAME_NOT_RESOLVED, rv); |
| 194 | 210 |
| 195 const std::string kHostName2(4097, 'b'); | 211 const std::string kHostName2(4097, 'b'); |
| (...skipping 369 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 565 | 581 |
| 566 EXPECT_EQ(4u, observer.start_log.size()); // Was incremented by 1. | 582 EXPECT_EQ(4u, observer.start_log.size()); // Was incremented by 1. |
| 567 EXPECT_EQ(2u, observer.finish_log.size()); | 583 EXPECT_EQ(2u, observer.finish_log.size()); |
| 568 EXPECT_EQ(1u, observer.cancel_log.size()); | 584 EXPECT_EQ(1u, observer.cancel_log.size()); |
| 569 | 585 |
| 570 EXPECT_TRUE(observer.start_log[3] == | 586 EXPECT_TRUE(observer.start_log[3] == |
| 571 TestHostResolverObserver::StartOrCancelEntry(4, info3_)); | 587 TestHostResolverObserver::StartOrCancelEntry(4, info3_)); |
| 572 } | 588 } |
| 573 | 589 |
| 574 } // namespace net | 590 } // namespace net |
| OLD | NEW |