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