Chromium Code Reviews| 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/net_log.h" | 10 #include "net/base/net_log.h" |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 31 EXPECT_EQ(sizeof(struct sockaddr_in), ainfo->ai_addrlen); | 31 EXPECT_EQ(sizeof(struct sockaddr_in), ainfo->ai_addrlen); |
| 32 | 32 |
| 33 const struct sockaddr* sa = ainfo->ai_addr; | 33 const struct sockaddr* sa = ainfo->ai_addr; |
| 34 const struct sockaddr_in* sa_in = (const struct sockaddr_in*) sa; | 34 const struct sockaddr_in* sa_in = (const struct sockaddr_in*) sa; |
| 35 EXPECT_TRUE(htons(port) == sa_in->sin_port); | 35 EXPECT_TRUE(htons(port) == sa_in->sin_port); |
| 36 EXPECT_STREQ(*i, NetAddressToString(sa, ainfo->ai_addrlen).c_str()); | 36 EXPECT_STREQ(*i, NetAddressToString(sa, ainfo->ai_addrlen).c_str()); |
| 37 } | 37 } |
| 38 ASSERT_EQ(static_cast<addrinfo*>(NULL), ainfo); | 38 ASSERT_EQ(static_cast<addrinfo*>(NULL), ainfo); |
| 39 } | 39 } |
| 40 | 40 |
| 41 HostCache* CreateDefaultCache() { | |
| 42 return new HostCache( | |
| 43 100, // max cache entries. | |
| 44 base::TimeDelta::FromMinutes(1), | |
| 45 base::TimeDelta::FromSeconds(0)); | |
| 46 } | |
| 47 | |
| 41 } // namespace | 48 } // namespace |
| 42 | 49 |
| 43 static const int kPortNum = 80; | 50 static const int kPortNum = 80; |
| 44 static const size_t kMaxTransactions = 2; | 51 static const size_t kMaxTransactions = 2; |
| 45 static const size_t kMaxPendingRequests = 1; | 52 static const size_t kMaxPendingRequests = 1; |
| 46 static int transaction_ids[] = {0, 1, 2, 3}; | 53 static int transaction_ids[] = {0, 1, 2, 3}; |
| 47 | 54 |
| 48 // The following fixture sets up an environment for four different lookups | 55 // The following fixture sets up an environment for four different lookups |
| 49 // with their data defined in dns_test_util.h. All tests make use of these | 56 // with their data defined in dns_test_util.h. All tests make use of these |
| 50 // predefined variables instead of each defining their own, to avoid | 57 // predefined variables instead of each defining their own, to avoid |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 64 ip_addresses0_(kT0IpAddresses, | 71 ip_addresses0_(kT0IpAddresses, |
| 65 kT0IpAddresses + arraysize(kT0IpAddresses)), | 72 kT0IpAddresses + arraysize(kT0IpAddresses)), |
| 66 ip_addresses1_(kT1IpAddresses, | 73 ip_addresses1_(kT1IpAddresses, |
| 67 kT1IpAddresses + arraysize(kT1IpAddresses)), | 74 kT1IpAddresses + arraysize(kT1IpAddresses)), |
| 68 ip_addresses2_(kT2IpAddresses, | 75 ip_addresses2_(kT2IpAddresses, |
| 69 kT2IpAddresses + arraysize(kT2IpAddresses)), | 76 kT2IpAddresses + arraysize(kT2IpAddresses)), |
| 70 ip_addresses3_(kT3IpAddresses, | 77 ip_addresses3_(kT3IpAddresses, |
| 71 kT3IpAddresses + arraysize(kT3IpAddresses)), | 78 kT3IpAddresses + arraysize(kT3IpAddresses)), |
| 72 test_prng_(std::deque<int>( | 79 test_prng_(std::deque<int>( |
| 73 transaction_ids, transaction_ids + arraysize(transaction_ids))) { | 80 transaction_ids, transaction_ids + arraysize(transaction_ids))) { |
| 74 | |
| 75 rand_int_cb_ = base::Bind(&TestPrng::GetNext, | 81 rand_int_cb_ = base::Bind(&TestPrng::GetNext, |
| 76 base::Unretained(&test_prng_)); | 82 base::Unretained(&test_prng_)); |
| 77 // AF_INET only for now. | 83 // AF_INET only for now. |
| 78 info0_.set_address_family(ADDRESS_FAMILY_IPV4); | 84 info0_.set_address_family(ADDRESS_FAMILY_IPV4); |
| 79 info1_.set_address_family(ADDRESS_FAMILY_IPV4); | 85 info1_.set_address_family(ADDRESS_FAMILY_IPV4); |
| 80 info2_.set_address_family(ADDRESS_FAMILY_IPV4); | 86 info2_.set_address_family(ADDRESS_FAMILY_IPV4); |
| 81 info3_.set_address_family(ADDRESS_FAMILY_IPV4); | 87 info3_.set_address_family(ADDRESS_FAMILY_IPV4); |
| 82 | 88 |
| 83 // Setup socket read/writes for transaction 0. | 89 // Setup socket read/writes for transaction 0. |
| 84 writes0_.push_back( | 90 writes0_.push_back( |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 125 factory_.AddSocketDataProvider(data2_.get()); | 131 factory_.AddSocketDataProvider(data2_.get()); |
| 126 factory_.AddSocketDataProvider(data3_.get()); | 132 factory_.AddSocketDataProvider(data3_.get()); |
| 127 | 133 |
| 128 IPEndPoint dns_server; | 134 IPEndPoint dns_server; |
| 129 bool rv0 = CreateDnsAddress(kDnsIp, kDnsPort, &dns_server); | 135 bool rv0 = CreateDnsAddress(kDnsIp, kDnsPort, &dns_server); |
| 130 DCHECK(rv0); | 136 DCHECK(rv0); |
| 131 | 137 |
| 132 resolver_.reset( | 138 resolver_.reset( |
| 133 new AsyncHostResolver( | 139 new AsyncHostResolver( |
| 134 dns_server, kMaxTransactions, kMaxPendingRequests, rand_int_cb_, | 140 dns_server, kMaxTransactions, kMaxPendingRequests, rand_int_cb_, |
| 135 &factory_, NULL)); | 141 CreateDefaultCache(), &factory_, NULL)); |
| 136 } | 142 } |
| 137 | 143 |
| 138 protected: | 144 protected: |
| 139 AddressList addrlist0_, addrlist1_, addrlist2_, addrlist3_; | 145 AddressList addrlist0_, addrlist1_, addrlist2_, addrlist3_; |
| 140 HostResolver::RequestInfo info0_, info1_, info2_, info3_; | 146 HostResolver::RequestInfo info0_, info1_, info2_, info3_; |
| 141 std::vector<MockWrite> writes0_, writes1_, writes2_, writes3_; | 147 std::vector<MockWrite> writes0_, writes1_, writes2_, writes3_; |
| 142 std::vector<MockRead> reads0_, reads1_, reads2_, reads3_; | 148 std::vector<MockRead> reads0_, reads1_, reads2_, reads3_; |
| 143 scoped_ptr<StaticSocketDataProvider> data0_, data1_, data2_, data3_; | 149 scoped_ptr<StaticSocketDataProvider> data0_, data1_, data2_, data3_; |
| 144 std::vector<const char*> ip_addresses0_, ip_addresses1_, | 150 std::vector<const char*> ip_addresses0_, ip_addresses1_, |
| 145 ip_addresses2_, ip_addresses3_; | 151 ip_addresses2_, ip_addresses3_; |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 170 } | 176 } |
| 171 | 177 |
| 172 TEST_F(AsyncHostResolverTest, IPv6LiteralLookup) { | 178 TEST_F(AsyncHostResolverTest, IPv6LiteralLookup) { |
| 173 info0_.set_host_port_pair(HostPortPair("2001:db8:0::42", kPortNum)); | 179 info0_.set_host_port_pair(HostPortPair("2001:db8:0::42", kPortNum)); |
| 174 int rv = resolver_->Resolve(info0_, &addrlist0_, &callback0_, NULL, | 180 int rv = resolver_->Resolve(info0_, &addrlist0_, &callback0_, NULL, |
| 175 BoundNetLog()); | 181 BoundNetLog()); |
| 176 // When support for IPv6 is added, this should succeed. | 182 // When support for IPv6 is added, this should succeed. |
| 177 EXPECT_EQ(ERR_NAME_NOT_RESOLVED, rv); | 183 EXPECT_EQ(ERR_NAME_NOT_RESOLVED, rv); |
| 178 } | 184 } |
| 179 | 185 |
| 180 TEST_F(AsyncHostResolverTest, CachedOnlyLookup) { | 186 TEST_F(AsyncHostResolverTest, CachedLookup) { |
| 181 info0_.set_only_use_cached_response(true); | 187 info0_.set_only_use_cached_response(true); |
| 182 int rv = resolver_->Resolve(info0_, &addrlist0_, &callback0_, NULL, | 188 int rv = resolver_->Resolve(info0_, &addrlist0_, NULL, NULL, |
| 183 BoundNetLog()); | 189 BoundNetLog()); |
| 184 // When caching is added, this should succeed. | |
| 185 EXPECT_EQ(ERR_NAME_NOT_RESOLVED, rv); | 190 EXPECT_EQ(ERR_NAME_NOT_RESOLVED, rv); |
| 191 | |
| 192 // Cache the result of |info0_| lookup. | |
| 193 info0_.set_only_use_cached_response(false); | |
| 194 rv = resolver_->Resolve(info0_, &addrlist0_, &callback0_, NULL, | |
| 195 BoundNetLog()); | |
| 196 EXPECT_EQ(ERR_IO_PENDING, rv); | |
| 197 rv = callback0_.WaitForResult(); | |
| 198 EXPECT_EQ(OK, rv); | |
| 199 VerifyAddressList(ip_addresses0_, kPortNum, addrlist0_); | |
| 200 | |
| 201 // Now lookup |info0_| from cache only, store results in |addrlist1_|, | |
|
cbentzel
2011/07/21 15:31:10
Could you add tests for cache expiration as well?
agayev
2011/07/21 17:48:03
Like creating a cache with 10ms TTL and then sleep
cbentzel
2011/07/21 18:22:15
yeah, you're right.
| |
| 202 // should succeed synchronously. | |
| 203 info0_.set_only_use_cached_response(true); | |
| 204 rv = resolver_->Resolve(info0_, &addrlist1_, NULL, NULL, | |
| 205 BoundNetLog()); | |
| 206 EXPECT_EQ(OK, rv); | |
| 207 VerifyAddressList(ip_addresses0_, kPortNum, addrlist1_); | |
| 186 } | 208 } |
| 187 | 209 |
| 188 TEST_F(AsyncHostResolverTest, InvalidHostNameLookup) { | 210 TEST_F(AsyncHostResolverTest, InvalidHostNameLookup) { |
| 189 const std::string kHostName1(64, 'a'); | 211 const std::string kHostName1(64, 'a'); |
| 190 info0_.set_host_port_pair(HostPortPair(kHostName1, kPortNum)); | 212 info0_.set_host_port_pair(HostPortPair(kHostName1, kPortNum)); |
| 191 int rv = resolver_->Resolve(info0_, &addrlist0_, &callback0_, NULL, | 213 int rv = resolver_->Resolve(info0_, &addrlist0_, &callback0_, NULL, |
| 192 BoundNetLog()); | 214 BoundNetLog()); |
| 193 EXPECT_EQ(ERR_NAME_NOT_RESOLVED, rv); | 215 EXPECT_EQ(ERR_NAME_NOT_RESOLVED, rv); |
| 194 | 216 |
| 195 const std::string kHostName2(4097, 'b'); | 217 const std::string kHostName2(4097, 'b'); |
| (...skipping 369 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 565 | 587 |
| 566 EXPECT_EQ(4u, observer.start_log.size()); // Was incremented by 1. | 588 EXPECT_EQ(4u, observer.start_log.size()); // Was incremented by 1. |
| 567 EXPECT_EQ(2u, observer.finish_log.size()); | 589 EXPECT_EQ(2u, observer.finish_log.size()); |
| 568 EXPECT_EQ(1u, observer.cancel_log.size()); | 590 EXPECT_EQ(1u, observer.cancel_log.size()); |
| 569 | 591 |
| 570 EXPECT_TRUE(observer.start_log[3] == | 592 EXPECT_TRUE(observer.start_log[3] == |
| 571 TestHostResolverObserver::StartOrCancelEntry(4, info3_)); | 593 TestHostResolverObserver::StartOrCancelEntry(4, info3_)); |
| 572 } | 594 } |
| 573 | 595 |
| 574 } // namespace net | 596 } // namespace net |
| OLD | NEW |