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 MockDnsClient : public DnsTransactionFactory, |
| 52 public base::SupportsWeakPtr<MockDnsClient> { | 52 public base::SupportsWeakPtr<MockDnsClient> { |
| 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, |
| 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<MockDnsClient>& client) |
| 63 : Request(qname, qtype, callback), started_(false), client_(client) { | 63 : hostname_(hostname), |
| 64 } | 64 qtype_(qtype), |
| 65 callback_(callback), | |
| 66 started_(false), | |
| 67 client_(client) { | |
| 65 | 68 |
|
mmenke
2012/01/19 17:24:47
nit: Remove line break
| |
| 66 virtual int Start() OVERRIDE { | |
| 67 EXPECT_FALSE(started_); | 69 EXPECT_FALSE(started_); |
| 68 started_ = true; | 70 started_ = true; |
| 69 MessageLoop::current()->PostTask( | 71 MessageLoop::current()->PostTask( |
| 70 FROM_HERE, | 72 FROM_HERE, |
| 71 base::Bind(&MockRequest::Finish, AsWeakPtr())); | 73 base::Bind(&MockRequest::Finish, AsWeakPtr())); |
| 74 } | |
| 75 | |
| 76 virtual const std::string& GetHostname() const OVERRIDE { | |
| 77 return hostname_; | |
| 78 } | |
| 79 | |
| 80 virtual uint16 GetType() const OVERRIDE { | |
| 81 return qtype_; | |
| 82 } | |
| 83 | |
| 84 virtual int Start() OVERRIDE { | |
| 72 return ERR_IO_PENDING; | 85 return ERR_IO_PENDING; |
| 73 } | 86 } |
| 74 | 87 |
| 75 private: | 88 private: |
| 76 void Finish() { | 89 void Finish() { |
| 77 if (!client_) { | 90 if (!client_) { |
| 78 DoCallback(ERR_DNS_SERVER_FAILED, NULL); | 91 callback_.Run(this, ERR_DNS_SERVER_FAILED, NULL); |
| 79 return; | 92 return; |
| 80 } | 93 } |
| 81 DoCallback(OK, client_->responses[Key(qname(), qtype())]); | 94 callback_.Run(this, |
| 95 OK, | |
| 96 client_->responses[Key(GetHostname(), GetType())]); | |
| 82 } | 97 } |
| 83 | 98 |
| 99 std::string hostname_; | |
| 100 uint16 qtype_; | |
|
mmenke
2012/01/19 17:24:47
nit: You can make some of these const.
| |
| 101 DnsTransactionFactory::CallbackType callback_; | |
| 84 bool started_; | 102 bool started_; |
| 85 base::WeakPtr<MockDnsClient> client_; | 103 base::WeakPtr<MockDnsClient> client_; |
| 86 }; | 104 }; |
| 87 | 105 |
| 88 typedef std::pair<std::string, uint16> Key; | 106 typedef std::pair<std::string, uint16> Key; |
| 89 | 107 |
| 90 MockDnsClient() : num_requests(0) {} | 108 MockDnsClient() : num_requests(0) {} |
| 91 ~MockDnsClient() { | 109 ~MockDnsClient() { |
| 92 STLDeleteValues(&responses); | 110 STLDeleteValues(&responses); |
| 93 } | 111 } |
| 94 | 112 |
| 95 Request* CreateRequest(const base::StringPiece& qname, | 113 scoped_ptr<DnsTransaction> CreateTransaction( |
| 96 uint16 qtype, | 114 const std::string& qname, |
| 97 const RequestCallback& callback, | 115 uint16 qtype, |
| 98 const BoundNetLog&) { | 116 const DnsTransactionFactory::CallbackType& callback, |
| 117 const BoundNetLog&) { | |
| 99 ++num_requests; | 118 ++num_requests; |
| 100 return new MockRequest(qname, qtype, callback, AsWeakPtr()); | 119 return scoped_ptr<DnsTransaction>( |
| 120 new MockRequest(qname, qtype, callback, AsWeakPtr())); | |
| 101 } | 121 } |
| 102 | 122 |
| 103 int num_requests; | 123 int num_requests; |
| 104 std::map<Key, DnsResponse*> responses; | 124 std::map<Key, DnsResponse*> responses; |
|
mmenke
2012/01/19 17:24:47
This isn't used anywhere, as MockDnsClient now tra
szym
2012/01/20 19:38:27
Used in Finish().
| |
| 105 }; | 125 }; |
| 106 | 126 |
| 107 } // namespace | 127 } // namespace |
| 108 | 128 |
| 109 | 129 |
| 110 // The following fixture sets up an environment for four different lookups | 130 // 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 | 131 // 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 | 132 // predefined variables instead of each defining their own, to avoid |
| 113 // boilerplate code in every test. Assuming every coming query is for a | 133 // boilerplate code in every test. Assuming every coming query is for a |
| 114 // distinct hostname, as |kMaxTransactions| is set to 2 and | 134 // distinct hostname, as |kMaxTransactions| is set to 2 and |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 130 ip_addresses2_(kT2IpAddresses, | 150 ip_addresses2_(kT2IpAddresses, |
| 131 kT2IpAddresses + arraysize(kT2IpAddresses)), | 151 kT2IpAddresses + arraysize(kT2IpAddresses)), |
| 132 ip_addresses3_(kT3IpAddresses, | 152 ip_addresses3_(kT3IpAddresses, |
| 133 kT3IpAddresses + arraysize(kT3IpAddresses)) { | 153 kT3IpAddresses + arraysize(kT3IpAddresses)) { |
| 134 // AF_INET only for now. | 154 // AF_INET only for now. |
| 135 info0_.set_address_family(ADDRESS_FAMILY_IPV4); | 155 info0_.set_address_family(ADDRESS_FAMILY_IPV4); |
| 136 info1_.set_address_family(ADDRESS_FAMILY_IPV4); | 156 info1_.set_address_family(ADDRESS_FAMILY_IPV4); |
| 137 info2_.set_address_family(ADDRESS_FAMILY_IPV4); | 157 info2_.set_address_family(ADDRESS_FAMILY_IPV4); |
| 138 info3_.set_address_family(ADDRESS_FAMILY_IPV4); | 158 info3_.set_address_family(ADDRESS_FAMILY_IPV4); |
| 139 | 159 |
| 140 client_.reset(new MockDnsClient()); | 160 client_ = new MockDnsClient(); |
| 141 | 161 |
| 142 AddResponse(std::string(kT0DnsName, arraysize(kT0DnsName)), kT0Qtype, | 162 AddResponse(kT0HostName, kT0Qtype, |
| 143 new DnsResponse(reinterpret_cast<const char*>(kT0ResponseDatagram), | 163 new DnsResponse(reinterpret_cast<const char*>(kT0ResponseDatagram), |
| 144 arraysize(kT0ResponseDatagram), | 164 arraysize(kT0ResponseDatagram), |
| 145 arraysize(kT0QueryDatagram))); | 165 arraysize(kT0QueryDatagram))); |
| 146 | 166 |
| 147 AddResponse(std::string(kT1DnsName, arraysize(kT1DnsName)), kT1Qtype, | 167 AddResponse(kT1HostName, kT1Qtype, |
| 148 new DnsResponse(reinterpret_cast<const char*>(kT1ResponseDatagram), | 168 new DnsResponse(reinterpret_cast<const char*>(kT1ResponseDatagram), |
| 149 arraysize(kT1ResponseDatagram), | 169 arraysize(kT1ResponseDatagram), |
| 150 arraysize(kT1QueryDatagram))); | 170 arraysize(kT1QueryDatagram))); |
| 151 | 171 |
| 152 AddResponse(std::string(kT2DnsName, arraysize(kT2DnsName)), kT2Qtype, | 172 AddResponse(kT2HostName, kT2Qtype, |
| 153 new DnsResponse(reinterpret_cast<const char*>(kT2ResponseDatagram), | 173 new DnsResponse(reinterpret_cast<const char*>(kT2ResponseDatagram), |
| 154 arraysize(kT2ResponseDatagram), | 174 arraysize(kT2ResponseDatagram), |
| 155 arraysize(kT2QueryDatagram))); | 175 arraysize(kT2QueryDatagram))); |
| 156 | 176 |
| 157 AddResponse(std::string(kT3DnsName, arraysize(kT3DnsName)), kT3Qtype, | 177 AddResponse(kT3HostName, kT3Qtype, |
| 158 new DnsResponse(reinterpret_cast<const char*>(kT3ResponseDatagram), | 178 new DnsResponse(reinterpret_cast<const char*>(kT3ResponseDatagram), |
| 159 arraysize(kT3ResponseDatagram), | 179 arraysize(kT3ResponseDatagram), |
| 160 arraysize(kT3QueryDatagram))); | 180 arraysize(kT3QueryDatagram))); |
| 161 | 181 |
| 162 resolver_.reset( | 182 resolver_.reset( |
| 163 new AsyncHostResolver(kMaxTransactions, kMaxPendingRequests, | 183 new AsyncHostResolver(kMaxTransactions, kMaxPendingRequests, |
| 164 HostCache::CreateDefaultCache(), | 184 HostCache::CreateDefaultCache(), |
| 165 client_.get(), NULL)); | 185 scoped_ptr<DnsTransactionFactory>(client_), NULL)); |
| 166 } | 186 } |
| 167 | 187 |
| 168 void AddResponse(const std::string& name, uint8 type, DnsResponse* response) { | 188 void AddResponse(const std::string& name, uint8 type, DnsResponse* response) { |
| 169 client_->responses[MockDnsClient::Key(name, type)] = response; | 189 client_->responses[MockDnsClient::Key(name, type)] = response; |
| 170 } | 190 } |
| 171 | 191 |
| 172 protected: | 192 protected: |
| 173 AddressList addrlist0_, addrlist1_, addrlist2_, addrlist3_; | 193 AddressList addrlist0_, addrlist1_, addrlist2_, addrlist3_; |
| 174 HostResolver::RequestInfo info0_, info1_, info2_, info3_; | 194 HostResolver::RequestInfo info0_, info1_, info2_, info3_; |
| 175 std::vector<const char*> ip_addresses0_, ip_addresses1_, | 195 std::vector<const char*> ip_addresses0_, ip_addresses1_, |
| 176 ip_addresses2_, ip_addresses3_; | 196 ip_addresses2_, ip_addresses3_; |
| 177 scoped_ptr<HostResolver> resolver_; | 197 scoped_ptr<HostResolver> resolver_; |
| 178 scoped_ptr<MockDnsClient> client_; | 198 MockDnsClient* client_; // Owned by the AsyncHostResolver. |
| 179 TestCompletionCallback callback0_, callback1_, callback2_, callback3_; | 199 TestCompletionCallback callback0_, callback1_, callback2_, callback3_; |
| 180 }; | 200 }; |
| 181 | 201 |
| 182 TEST_F(AsyncHostResolverTest, EmptyHostLookup) { | 202 TEST_F(AsyncHostResolverTest, EmptyHostLookup) { |
| 183 info0_.set_host_port_pair(HostPortPair("", kPortNum)); | 203 info0_.set_host_port_pair(HostPortPair("", kPortNum)); |
| 184 int rv = resolver_->Resolve(info0_, &addrlist0_, callback0_.callback(), NULL, | 204 int rv = resolver_->Resolve(info0_, &addrlist0_, callback0_.callback(), NULL, |
| 185 BoundNetLog()); | 205 BoundNetLog()); |
| 186 EXPECT_EQ(ERR_NAME_NOT_RESOLVED, rv); | 206 EXPECT_EQ(ERR_NAME_NOT_RESOLVED, rv); |
| 187 } | 207 } |
| 188 | 208 |
| (...skipping 29 matching lines...) Expand all Loading... | |
| 218 EXPECT_EQ(OK, rv); | 238 EXPECT_EQ(OK, rv); |
| 219 VerifyAddressList(ip_addresses0_, kPortNum, addrlist0_); | 239 VerifyAddressList(ip_addresses0_, kPortNum, addrlist0_); |
| 220 | 240 |
| 221 // Now lookup |info0_| from cache only, store results in |addrlist1_|, | 241 // Now lookup |info0_| from cache only, store results in |addrlist1_|, |
| 222 // should succeed synchronously. | 242 // should succeed synchronously. |
| 223 rv = resolver_->ResolveFromCache(info0_, &addrlist1_, BoundNetLog()); | 243 rv = resolver_->ResolveFromCache(info0_, &addrlist1_, BoundNetLog()); |
| 224 EXPECT_EQ(OK, rv); | 244 EXPECT_EQ(OK, rv); |
| 225 VerifyAddressList(ip_addresses0_, kPortNum, addrlist1_); | 245 VerifyAddressList(ip_addresses0_, kPortNum, addrlist1_); |
| 226 } | 246 } |
| 227 | 247 |
| 228 TEST_F(AsyncHostResolverTest, InvalidHostNameLookup) { | 248 // TODO(szym): This tests DnsClient not AsyncHostResolver. Remove or move to |
| 249 // dns_client_unittest.cc | |
| 250 TEST_F(AsyncHostResolverTest, DISABLED_InvalidHostNameLookup) { | |
| 229 const std::string kHostName1(64, 'a'); | 251 const std::string kHostName1(64, 'a'); |
| 230 info0_.set_host_port_pair(HostPortPair(kHostName1, kPortNum)); | 252 info0_.set_host_port_pair(HostPortPair(kHostName1, kPortNum)); |
| 231 int rv = resolver_->Resolve(info0_, &addrlist0_, callback0_.callback(), NULL, | 253 int rv = resolver_->Resolve(info0_, &addrlist0_, callback0_.callback(), NULL, |
| 232 BoundNetLog()); | 254 BoundNetLog()); |
| 233 EXPECT_EQ(ERR_NAME_NOT_RESOLVED, rv); | 255 EXPECT_EQ(ERR_INVALID_ARGUMENT, rv); |
| 234 | 256 |
| 235 const std::string kHostName2(4097, 'b'); | 257 const std::string kHostName2(4097, 'b'); |
| 236 info0_.set_host_port_pair(HostPortPair(kHostName2, kPortNum)); | 258 info0_.set_host_port_pair(HostPortPair(kHostName2, kPortNum)); |
| 237 rv = resolver_->Resolve(info0_, &addrlist0_, callback0_.callback(), NULL, | 259 rv = resolver_->Resolve(info0_, &addrlist0_, callback0_.callback(), NULL, |
| 238 BoundNetLog()); | 260 BoundNetLog()); |
| 239 EXPECT_EQ(ERR_NAME_NOT_RESOLVED, rv); | 261 EXPECT_EQ(ERR_INVALID_ARGUMENT, rv); |
| 240 } | 262 } |
| 241 | 263 |
| 242 TEST_F(AsyncHostResolverTest, Lookup) { | 264 TEST_F(AsyncHostResolverTest, Lookup) { |
| 243 int rv = resolver_->Resolve(info0_, &addrlist0_, callback0_.callback(), NULL, | 265 int rv = resolver_->Resolve(info0_, &addrlist0_, callback0_.callback(), NULL, |
| 244 BoundNetLog()); | 266 BoundNetLog()); |
| 245 EXPECT_EQ(ERR_IO_PENDING, rv); | 267 EXPECT_EQ(ERR_IO_PENDING, rv); |
| 246 rv = callback0_.WaitForResult(); | 268 rv = callback0_.WaitForResult(); |
| 247 EXPECT_EQ(OK, rv); | 269 EXPECT_EQ(OK, rv); |
| 248 VerifyAddressList(ip_addresses0_, kPortNum, addrlist0_); | 270 VerifyAddressList(ip_addresses0_, kPortNum, addrlist0_); |
| 249 } | 271 } |
| (...skipping 260 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 510 rv_fail = callback_fail.WaitForResult(); | 532 rv_fail = callback_fail.WaitForResult(); |
| 511 EXPECT_EQ(ERR_HOST_RESOLVER_QUEUE_TOO_LARGE, rv_fail); | 533 EXPECT_EQ(ERR_HOST_RESOLVER_QUEUE_TOO_LARGE, rv_fail); |
| 512 EXPECT_EQ(static_cast<addrinfo*>(NULL), addrlist_fail.head()); | 534 EXPECT_EQ(static_cast<addrinfo*>(NULL), addrlist_fail.head()); |
| 513 | 535 |
| 514 rv2 = callback2_.WaitForResult(); | 536 rv2 = callback2_.WaitForResult(); |
| 515 EXPECT_EQ(OK, rv2); | 537 EXPECT_EQ(OK, rv2); |
| 516 VerifyAddressList(ip_addresses2_, kPortNum, addrlist2_); | 538 VerifyAddressList(ip_addresses2_, kPortNum, addrlist2_); |
| 517 } | 539 } |
| 518 | 540 |
| 519 } // namespace net | 541 } // namespace net |
| OLD | NEW |