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/dns/async_host_resolver.h" | 5 #include "net/dns/async_host_resolver.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
| 9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
| 10 #include "base/stl_util.h" | 10 #include "base/stl_util.h" |
| 11 #include "net/base/host_cache.h" | 11 #include "net/base/host_cache.h" |
| 12 #include "net/base/net_errors.h" | 12 #include "net/base/net_errors.h" |
| 13 #include "net/base/net_log.h" | 13 #include "net/base/net_log.h" |
| 14 #include "net/base/sys_addrinfo.h" | 14 #include "net/base/sys_addrinfo.h" |
| 15 #include "net/base/test_completion_callback.h" | 15 #include "net/base/test_completion_callback.h" |
| 16 #include "net/dns/dns_client.h" | |
| 17 #include "net/dns/dns_query.h" | 16 #include "net/dns/dns_query.h" |
| 18 #include "net/dns/dns_response.h" | 17 #include "net/dns/dns_response.h" |
| 19 #include "net/dns/dns_test_util.h" | 18 #include "net/dns/dns_test_util.h" |
| 19 #include "net/dns/dns_transaction.h" | |
| 20 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
| 21 | 21 |
| 22 namespace net { | 22 namespace net { |
| 23 | 23 |
| 24 namespace { | 24 namespace { |
| 25 | 25 |
| 26 const int kPortNum = 80; | 26 const int kPortNum = 80; |
| 27 const size_t kMaxTransactions = 2; | 27 const size_t kMaxTransactions = 2; |
| 28 const size_t kMaxPendingRequests = 1; | 28 const size_t kMaxPendingRequests = 1; |
| 29 | 29 |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 41 EXPECT_EQ(sizeof(struct sockaddr_in), ainfo->ai_addrlen); | 41 EXPECT_EQ(sizeof(struct sockaddr_in), ainfo->ai_addrlen); |
| 42 | 42 |
| 43 const struct sockaddr* sa = ainfo->ai_addr; | 43 const struct sockaddr* sa = ainfo->ai_addr; |
| 44 const struct sockaddr_in* sa_in = (const struct sockaddr_in*) sa; | 44 const struct sockaddr_in* sa_in = (const struct sockaddr_in*) sa; |
| 45 EXPECT_TRUE(htons(port) == sa_in->sin_port); | 45 EXPECT_TRUE(htons(port) == sa_in->sin_port); |
| 46 EXPECT_STREQ(*i, NetAddressToString(sa, ainfo->ai_addrlen).c_str()); | 46 EXPECT_STREQ(*i, NetAddressToString(sa, ainfo->ai_addrlen).c_str()); |
| 47 } | 47 } |
| 48 ASSERT_EQ(static_cast<addrinfo*>(NULL), ainfo); | 48 ASSERT_EQ(static_cast<addrinfo*>(NULL), ainfo); |
| 49 } | 49 } |
| 50 | 50 |
| 51 class MockDnsClient : public DnsClient, | 51 class MockDnsTransactionFactory : public DnsTransactionFactory, |
| 52 public base::SupportsWeakPtr<MockDnsClient> { | 52 public base::SupportsWeakPtr<MockDnsTransactionFactory> { |
|
cbentzel
2012/01/21 03:41:16
Nit: maybe just indent by 4 from line start here.
| |
| 53 public: | 53 public: |
| 54 // Using WeakPtr to support cancellation. | 54 // Using WeakPtr to support cancellation. |
| 55 // All MockRequests succeed unless canceled or MockDnsClient is destroyed. | 55 // All MockRequests succeed unless canceled or MockDnsClient is destroyed. |
| 56 class MockRequest : public DnsClient::Request, | 56 class MockRequest : public DnsTransaction, |
|
mmenke
2012/01/24 18:27:01
nit: Might want to call this MockTransaction.
| |
| 57 public base::SupportsWeakPtr<MockRequest> { | 57 public base::SupportsWeakPtr<MockRequest> { |
| 58 public: | 58 public: |
| 59 MockRequest(const base::StringPiece& qname, | 59 MockRequest(const std::string& hostname, |
| 60 uint16 qtype, | 60 uint16 qtype, |
| 61 const RequestCallback& callback, | 61 const DnsTransactionFactory::CallbackType& callback, |
| 62 const base::WeakPtr<MockDnsClient>& client) | 62 const base::WeakPtr<MockDnsTransactionFactory>& factory) |
| 63 : Request(qname, qtype, callback), started_(false), client_(client) { | 63 : hostname_(hostname), |
| 64 } | 64 qtype_(qtype), |
| 65 | 65 callback_(callback), |
| 66 virtual int Start() OVERRIDE { | 66 started_(false), |
| 67 factory_(factory) { | |
| 67 EXPECT_FALSE(started_); | 68 EXPECT_FALSE(started_); |
| 68 started_ = true; | 69 started_ = true; |
| 69 MessageLoop::current()->PostTask( | 70 MessageLoop::current()->PostTask( |
| 70 FROM_HERE, | 71 FROM_HERE, |
| 71 base::Bind(&MockRequest::Finish, AsWeakPtr())); | 72 base::Bind(&MockRequest::Finish, AsWeakPtr())); |
| 73 } | |
| 74 | |
| 75 virtual const std::string& GetHostname() const OVERRIDE { | |
| 76 return hostname_; | |
| 77 } | |
| 78 | |
| 79 virtual uint16 GetType() const OVERRIDE { | |
| 80 return qtype_; | |
| 81 } | |
| 82 | |
| 83 virtual int Start() OVERRIDE { | |
| 72 return ERR_IO_PENDING; | 84 return ERR_IO_PENDING; |
| 73 } | 85 } |
| 74 | 86 |
| 75 private: | 87 private: |
| 76 void Finish() { | 88 void Finish() { |
| 77 if (!client_) { | 89 if (!factory_) { |
| 78 DoCallback(ERR_DNS_SERVER_FAILED, NULL); | 90 callback_.Run(this, ERR_DNS_SERVER_FAILED, NULL); |
| 79 return; | 91 return; |
| 80 } | 92 } |
| 81 DoCallback(OK, client_->responses[Key(qname(), qtype())]); | 93 callback_.Run(this, |
| 94 OK, | |
| 95 factory_->responses[Key(GetHostname(), GetType())]); | |
| 82 } | 96 } |
| 83 | 97 |
| 98 const std::string hostname_; | |
| 99 const uint16 qtype_; | |
| 100 DnsTransactionFactory::CallbackType callback_; | |
| 84 bool started_; | 101 bool started_; |
| 85 base::WeakPtr<MockDnsClient> client_; | 102 const base::WeakPtr<MockDnsTransactionFactory> factory_; |
| 86 }; | 103 }; |
| 87 | 104 |
| 88 typedef std::pair<std::string, uint16> Key; | 105 typedef std::pair<std::string, uint16> Key; |
| 89 | 106 |
| 90 MockDnsClient() : num_requests(0) {} | 107 MockDnsTransactionFactory() : num_requests(0) {} |
| 91 ~MockDnsClient() { | 108 ~MockDnsTransactionFactory() { |
| 92 STLDeleteValues(&responses); | 109 STLDeleteValues(&responses); |
| 93 } | 110 } |
| 94 | 111 |
| 95 Request* CreateRequest(const base::StringPiece& qname, | 112 scoped_ptr<DnsTransaction> CreateTransaction( |
| 96 uint16 qtype, | 113 const std::string& qname, |
| 97 const RequestCallback& callback, | 114 uint16 qtype, |
| 98 const BoundNetLog&) { | 115 const DnsTransactionFactory::CallbackType& callback, |
| 116 const BoundNetLog&) { | |
| 99 ++num_requests; | 117 ++num_requests; |
| 100 return new MockRequest(qname, qtype, callback, AsWeakPtr()); | 118 return scoped_ptr<DnsTransaction>( |
| 119 new MockRequest(qname, qtype, callback, AsWeakPtr())); | |
| 101 } | 120 } |
| 102 | 121 |
| 103 int num_requests; | 122 int num_requests; |
| 104 std::map<Key, DnsResponse*> responses; | 123 std::map<Key, DnsResponse*> responses; |
|
mmenke
2012/01/24 18:27:01
While you're here, could you rename these to end i
| |
| 105 }; | 124 }; |
| 106 | 125 |
| 107 } // namespace | 126 } // namespace |
| 108 | 127 |
| 109 | 128 |
| 110 // The following fixture sets up an environment for four different lookups | 129 // The following fixture sets up an environment for four different lookups |
| 111 // with their data defined in dns_test_util.h. All tests make use of these | 130 // with their data defined in dns_test_util.h. All tests make use of these |
| 112 // predefined variables instead of each defining their own, to avoid | 131 // predefined variables instead of each defining their own, to avoid |
| 113 // boilerplate code in every test. Assuming every coming query is for a | 132 // boilerplate code in every test. Assuming every coming query is for a |
| 114 // distinct hostname, as |kMaxTransactions| is set to 2 and | 133 // distinct hostname, as |kMaxTransactions| is set to 2 and |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 130 ip_addresses2_(kT2IpAddresses, | 149 ip_addresses2_(kT2IpAddresses, |
| 131 kT2IpAddresses + arraysize(kT2IpAddresses)), | 150 kT2IpAddresses + arraysize(kT2IpAddresses)), |
| 132 ip_addresses3_(kT3IpAddresses, | 151 ip_addresses3_(kT3IpAddresses, |
| 133 kT3IpAddresses + arraysize(kT3IpAddresses)) { | 152 kT3IpAddresses + arraysize(kT3IpAddresses)) { |
| 134 // AF_INET only for now. | 153 // AF_INET only for now. |
| 135 info0_.set_address_family(ADDRESS_FAMILY_IPV4); | 154 info0_.set_address_family(ADDRESS_FAMILY_IPV4); |
| 136 info1_.set_address_family(ADDRESS_FAMILY_IPV4); | 155 info1_.set_address_family(ADDRESS_FAMILY_IPV4); |
| 137 info2_.set_address_family(ADDRESS_FAMILY_IPV4); | 156 info2_.set_address_family(ADDRESS_FAMILY_IPV4); |
| 138 info3_.set_address_family(ADDRESS_FAMILY_IPV4); | 157 info3_.set_address_family(ADDRESS_FAMILY_IPV4); |
| 139 | 158 |
| 140 client_.reset(new MockDnsClient()); | 159 client_ = new MockDnsTransactionFactory(); |
| 141 | 160 |
| 142 AddResponse(std::string(kT0DnsName, arraysize(kT0DnsName)), kT0Qtype, | 161 AddResponse(kT0HostName, kT0Qtype, |
| 143 new DnsResponse(reinterpret_cast<const char*>(kT0ResponseDatagram), | 162 new DnsResponse(reinterpret_cast<const char*>(kT0ResponseDatagram), |
| 144 arraysize(kT0ResponseDatagram), | 163 arraysize(kT0ResponseDatagram), |
| 145 arraysize(kT0QueryDatagram))); | 164 arraysize(kT0QueryDatagram))); |
| 146 | 165 |
| 147 AddResponse(std::string(kT1DnsName, arraysize(kT1DnsName)), kT1Qtype, | 166 AddResponse(kT1HostName, kT1Qtype, |
| 148 new DnsResponse(reinterpret_cast<const char*>(kT1ResponseDatagram), | 167 new DnsResponse(reinterpret_cast<const char*>(kT1ResponseDatagram), |
| 149 arraysize(kT1ResponseDatagram), | 168 arraysize(kT1ResponseDatagram), |
| 150 arraysize(kT1QueryDatagram))); | 169 arraysize(kT1QueryDatagram))); |
| 151 | 170 |
| 152 AddResponse(std::string(kT2DnsName, arraysize(kT2DnsName)), kT2Qtype, | 171 AddResponse(kT2HostName, kT2Qtype, |
| 153 new DnsResponse(reinterpret_cast<const char*>(kT2ResponseDatagram), | 172 new DnsResponse(reinterpret_cast<const char*>(kT2ResponseDatagram), |
| 154 arraysize(kT2ResponseDatagram), | 173 arraysize(kT2ResponseDatagram), |
| 155 arraysize(kT2QueryDatagram))); | 174 arraysize(kT2QueryDatagram))); |
| 156 | 175 |
| 157 AddResponse(std::string(kT3DnsName, arraysize(kT3DnsName)), kT3Qtype, | 176 AddResponse(kT3HostName, kT3Qtype, |
| 158 new DnsResponse(reinterpret_cast<const char*>(kT3ResponseDatagram), | 177 new DnsResponse(reinterpret_cast<const char*>(kT3ResponseDatagram), |
| 159 arraysize(kT3ResponseDatagram), | 178 arraysize(kT3ResponseDatagram), |
| 160 arraysize(kT3QueryDatagram))); | 179 arraysize(kT3QueryDatagram))); |
| 161 | 180 |
| 162 resolver_.reset( | 181 resolver_.reset( |
| 163 new AsyncHostResolver(kMaxTransactions, kMaxPendingRequests, | 182 new AsyncHostResolver(kMaxTransactions, kMaxPendingRequests, |
| 164 HostCache::CreateDefaultCache(), | 183 HostCache::CreateDefaultCache(), |
| 165 client_.get(), NULL)); | 184 scoped_ptr<DnsTransactionFactory>(client_), NULL)); |
| 166 } | 185 } |
| 167 | 186 |
| 168 void AddResponse(const std::string& name, uint8 type, DnsResponse* response) { | 187 void AddResponse(const std::string& name, uint8 type, DnsResponse* response) { |
| 169 client_->responses[MockDnsClient::Key(name, type)] = response; | 188 client_->responses[MockDnsTransactionFactory::Key(name, type)] = response; |
| 170 } | 189 } |
| 171 | 190 |
| 172 protected: | 191 protected: |
| 173 AddressList addrlist0_, addrlist1_, addrlist2_, addrlist3_; | 192 AddressList addrlist0_, addrlist1_, addrlist2_, addrlist3_; |
| 174 HostResolver::RequestInfo info0_, info1_, info2_, info3_; | 193 HostResolver::RequestInfo info0_, info1_, info2_, info3_; |
| 175 std::vector<const char*> ip_addresses0_, ip_addresses1_, | 194 std::vector<const char*> ip_addresses0_, ip_addresses1_, |
| 176 ip_addresses2_, ip_addresses3_; | 195 ip_addresses2_, ip_addresses3_; |
| 177 scoped_ptr<HostResolver> resolver_; | 196 scoped_ptr<HostResolver> resolver_; |
| 178 scoped_ptr<MockDnsClient> client_; | 197 MockDnsTransactionFactory* client_; // Owned by the AsyncHostResolver. |
| 179 TestCompletionCallback callback0_, callback1_, callback2_, callback3_; | 198 TestCompletionCallback callback0_, callback1_, callback2_, callback3_; |
| 180 }; | 199 }; |
| 181 | 200 |
| 182 TEST_F(AsyncHostResolverTest, EmptyHostLookup) { | 201 TEST_F(AsyncHostResolverTest, EmptyHostLookup) { |
| 183 info0_.set_host_port_pair(HostPortPair("", kPortNum)); | 202 info0_.set_host_port_pair(HostPortPair("", kPortNum)); |
| 184 int rv = resolver_->Resolve(info0_, &addrlist0_, callback0_.callback(), NULL, | 203 int rv = resolver_->Resolve(info0_, &addrlist0_, callback0_.callback(), NULL, |
| 185 BoundNetLog()); | 204 BoundNetLog()); |
| 186 EXPECT_EQ(ERR_NAME_NOT_RESOLVED, rv); | 205 EXPECT_EQ(ERR_NAME_NOT_RESOLVED, rv); |
| 187 } | 206 } |
| 188 | 207 |
| (...skipping 29 matching lines...) Expand all Loading... | |
| 218 EXPECT_EQ(OK, rv); | 237 EXPECT_EQ(OK, rv); |
| 219 VerifyAddressList(ip_addresses0_, kPortNum, addrlist0_); | 238 VerifyAddressList(ip_addresses0_, kPortNum, addrlist0_); |
| 220 | 239 |
| 221 // Now lookup |info0_| from cache only, store results in |addrlist1_|, | 240 // Now lookup |info0_| from cache only, store results in |addrlist1_|, |
| 222 // should succeed synchronously. | 241 // should succeed synchronously. |
| 223 rv = resolver_->ResolveFromCache(info0_, &addrlist1_, BoundNetLog()); | 242 rv = resolver_->ResolveFromCache(info0_, &addrlist1_, BoundNetLog()); |
| 224 EXPECT_EQ(OK, rv); | 243 EXPECT_EQ(OK, rv); |
| 225 VerifyAddressList(ip_addresses0_, kPortNum, addrlist1_); | 244 VerifyAddressList(ip_addresses0_, kPortNum, addrlist1_); |
| 226 } | 245 } |
| 227 | 246 |
| 228 TEST_F(AsyncHostResolverTest, InvalidHostNameLookup) { | 247 // TODO(szym): This tests DnsClient not AsyncHostResolver. Remove or move to |
| 248 // dns_client_unittest.cc | |
| 249 TEST_F(AsyncHostResolverTest, DISABLED_InvalidHostNameLookup) { | |
| 229 const std::string kHostName1(64, 'a'); | 250 const std::string kHostName1(64, 'a'); |
| 230 info0_.set_host_port_pair(HostPortPair(kHostName1, kPortNum)); | 251 info0_.set_host_port_pair(HostPortPair(kHostName1, kPortNum)); |
| 231 int rv = resolver_->Resolve(info0_, &addrlist0_, callback0_.callback(), NULL, | 252 int rv = resolver_->Resolve(info0_, &addrlist0_, callback0_.callback(), NULL, |
| 232 BoundNetLog()); | 253 BoundNetLog()); |
| 233 EXPECT_EQ(ERR_NAME_NOT_RESOLVED, rv); | 254 EXPECT_EQ(ERR_INVALID_ARGUMENT, rv); |
| 234 | 255 |
| 235 const std::string kHostName2(4097, 'b'); | 256 const std::string kHostName2(4097, 'b'); |
| 236 info0_.set_host_port_pair(HostPortPair(kHostName2, kPortNum)); | 257 info0_.set_host_port_pair(HostPortPair(kHostName2, kPortNum)); |
| 237 rv = resolver_->Resolve(info0_, &addrlist0_, callback0_.callback(), NULL, | 258 rv = resolver_->Resolve(info0_, &addrlist0_, callback0_.callback(), NULL, |
| 238 BoundNetLog()); | 259 BoundNetLog()); |
| 239 EXPECT_EQ(ERR_NAME_NOT_RESOLVED, rv); | 260 EXPECT_EQ(ERR_INVALID_ARGUMENT, rv); |
| 240 } | 261 } |
| 241 | 262 |
| 242 TEST_F(AsyncHostResolverTest, Lookup) { | 263 TEST_F(AsyncHostResolverTest, Lookup) { |
| 243 int rv = resolver_->Resolve(info0_, &addrlist0_, callback0_.callback(), NULL, | 264 int rv = resolver_->Resolve(info0_, &addrlist0_, callback0_.callback(), NULL, |
| 244 BoundNetLog()); | 265 BoundNetLog()); |
| 245 EXPECT_EQ(ERR_IO_PENDING, rv); | 266 EXPECT_EQ(ERR_IO_PENDING, rv); |
| 246 rv = callback0_.WaitForResult(); | 267 rv = callback0_.WaitForResult(); |
| 247 EXPECT_EQ(OK, rv); | 268 EXPECT_EQ(OK, rv); |
| 248 VerifyAddressList(ip_addresses0_, kPortNum, addrlist0_); | 269 VerifyAddressList(ip_addresses0_, kPortNum, addrlist0_); |
| 249 } | 270 } |
| (...skipping 260 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 510 rv_fail = callback_fail.WaitForResult(); | 531 rv_fail = callback_fail.WaitForResult(); |
| 511 EXPECT_EQ(ERR_HOST_RESOLVER_QUEUE_TOO_LARGE, rv_fail); | 532 EXPECT_EQ(ERR_HOST_RESOLVER_QUEUE_TOO_LARGE, rv_fail); |
| 512 EXPECT_EQ(static_cast<addrinfo*>(NULL), addrlist_fail.head()); | 533 EXPECT_EQ(static_cast<addrinfo*>(NULL), addrlist_fail.head()); |
| 513 | 534 |
| 514 rv2 = callback2_.WaitForResult(); | 535 rv2 = callback2_.WaitForResult(); |
| 515 EXPECT_EQ(OK, rv2); | 536 EXPECT_EQ(OK, rv2); |
| 516 VerifyAddressList(ip_addresses2_, kPortNum, addrlist2_); | 537 VerifyAddressList(ip_addresses2_, kPortNum, addrlist2_); |
| 517 } | 538 } |
| 518 | 539 |
| 519 } // namespace net | 540 } // namespace net |
| OLD | NEW |