Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(259)

Side by Side Diff: net/base/host_resolver_impl_unittest.cc

Issue 10309002: Reimplements net::AddressList without struct addrinfo. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: get_canonical_name -> canonical_name. iterator to indexing Created 8 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « net/base/host_resolver_impl.cc ('k') | net/base/host_resolver_proc.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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/host_resolver_impl.h" 5 #include "net/base/host_resolver_impl.h"
6 6
7 #include <algorithm>
7 #include <string> 8 #include <string>
8 9
9 #include "base/bind.h" 10 #include "base/bind.h"
10 #include "base/bind_helpers.h" 11 #include "base/bind_helpers.h"
11 #include "base/memory/ref_counted.h" 12 #include "base/memory/ref_counted.h"
12 #include "base/memory/scoped_vector.h" 13 #include "base/memory/scoped_vector.h"
13 #include "base/message_loop.h" 14 #include "base/message_loop.h"
14 #include "base/string_util.h" 15 #include "base/string_util.h"
15 #include "base/synchronization/condition_variable.h" 16 #include "base/synchronization/condition_variable.h"
16 #include "base/synchronization/lock.h" 17 #include "base/synchronization/lock.h"
17 #include "base/test/test_timeouts.h" 18 #include "base/test/test_timeouts.h"
18 #include "base/time.h" 19 #include "base/time.h"
19 #include "net/base/address_list.h" 20 #include "net/base/address_list.h"
20 #include "net/base/host_cache.h" 21 #include "net/base/host_cache.h"
21 #include "net/base/mock_host_resolver.h" 22 #include "net/base/mock_host_resolver.h"
22 #include "net/base/net_errors.h" 23 #include "net/base/net_errors.h"
23 #include "net/base/net_util.h" 24 #include "net/base/net_util.h"
24 #include "net/base/sys_addrinfo.h"
25 #include "net/dns/dns_client.h" 25 #include "net/dns/dns_client.h"
26 #include "net/dns/dns_test_util.h" 26 #include "net/dns/dns_test_util.h"
27 #include "testing/gtest/include/gtest/gtest.h" 27 #include "testing/gtest/include/gtest/gtest.h"
28 28
29 namespace net { 29 namespace net {
30 30
31 namespace { 31 namespace {
32 32
33 const size_t kMaxJobs = 10u; 33 const size_t kMaxJobs = 10u;
34 const size_t kMaxRetryAttempts = 4u; 34 const size_t kMaxRetryAttempts = 4u;
(...skipping 198 matching lines...) Expand 10 before | Expand all | Expand 10 after
233 result_(ERR_UNEXPECTED), 233 result_(ERR_UNEXPECTED),
234 handle_(NULL) {} 234 handle_(NULL) {}
235 235
236 int Resolve() { 236 int Resolve() {
237 DCHECK(resolver_); 237 DCHECK(resolver_);
238 DCHECK(!handle_); 238 DCHECK(!handle_);
239 list_ = AddressList(); 239 list_ = AddressList();
240 result_ = resolver_->Resolve( 240 result_ = resolver_->Resolve(
241 info_, &list_, base::Bind(&Request::OnComplete, base::Unretained(this)), 241 info_, &list_, base::Bind(&Request::OnComplete, base::Unretained(this)),
242 &handle_, BoundNetLog()); 242 &handle_, BoundNetLog());
243 if (list_.head()) 243 if (!list_.empty())
244 EXPECT_EQ(OK, result_); 244 EXPECT_EQ(OK, result_);
245 return result_; 245 return result_;
246 } 246 }
247 247
248 int ResolveFromCache() { 248 int ResolveFromCache() {
249 DCHECK(resolver_); 249 DCHECK(resolver_);
250 DCHECK(!handle_); 250 DCHECK(!handle_);
251 return resolver_->ResolveFromCache(info_, &list_, BoundNetLog()); 251 return resolver_->ResolveFromCache(info_, &list_, BoundNetLog());
252 } 252 }
253 253
254 void Cancel() { 254 void Cancel() {
255 DCHECK(resolver_); 255 DCHECK(resolver_);
256 DCHECK(handle_); 256 DCHECK(handle_);
257 resolver_->CancelRequest(handle_); 257 resolver_->CancelRequest(handle_);
258 handle_ = NULL; 258 handle_ = NULL;
259 } 259 }
260 260
261 const HostResolver::RequestInfo& info() const { return info_; } 261 const HostResolver::RequestInfo& info() const { return info_; }
262 size_t index() const { return index_; } 262 size_t index() const { return index_; }
263 const AddressList& list() const { return list_; } 263 const AddressList& list() const { return list_; }
264 int result() const { return result_; } 264 int result() const { return result_; }
265 bool completed() const { return result_ != ERR_IO_PENDING; } 265 bool completed() const { return result_ != ERR_IO_PENDING; }
266 bool pending() const { return handle_ != NULL; } 266 bool pending() const { return handle_ != NULL; }
267 267
268 bool HasAddress(const std::string& address, int port) const { 268 bool HasAddress(const std::string& address, int port) const {
269 IPAddressNumber ip; 269 IPAddressNumber ip;
270 bool rv = ParseIPLiteralToNumber(address, &ip); 270 bool rv = ParseIPLiteralToNumber(address, &ip);
271 DCHECK(rv); 271 DCHECK(rv);
272 IPEndPoint target(ip, port); 272 return std::find(list_.begin(),
273 for (const struct addrinfo* ai = list_.head(); 273 list_.end(),
274 ai != NULL; 274 IPEndPoint(ip, port)) != list_.end();
275 ai = ai->ai_next) {
276 IPEndPoint ipe;
277 rv = ipe.FromSockAddr(ai->ai_addr, ai->ai_addrlen);
278 DCHECK(rv);
279 if (target == ipe)
280 return true;
281 }
282 return false;
283 } 275 }
284 276
285 // Returns the number of addresses in |list_|. 277 // Returns the number of addresses in |list_|.
286 unsigned NumberOfAddresses() const { 278 unsigned NumberOfAddresses() const {
287 unsigned count = 0; 279 return list_.size();
288 for (const struct addrinfo* ai = list_.head();
289 ai != NULL;
290 ai = ai->ai_next) {
291 ++count;
292 }
293 return count;
294 } 280 }
295 281
296 bool HasOneAddress(const std::string& address, int port) const { 282 bool HasOneAddress(const std::string& address, int port) const {
297 return HasAddress(address, port) && (NumberOfAddresses() == 1u); 283 return HasAddress(address, port) && (NumberOfAddresses() == 1u);
298 } 284 }
299 285
300 // Returns ERR_UNEXPECTED if timed out. 286 // Returns ERR_UNEXPECTED if timed out.
301 int WaitForResult() { 287 int WaitForResult() {
302 if (completed()) 288 if (completed())
303 return result_; 289 return result_;
(...skipping 12 matching lines...) Expand all
316 return ERR_UNEXPECTED; 302 return ERR_UNEXPECTED;
317 } 303 }
318 304
319 private: 305 private:
320 void OnComplete(int rv) { 306 void OnComplete(int rv) {
321 EXPECT_TRUE(pending()); 307 EXPECT_TRUE(pending());
322 EXPECT_EQ(ERR_IO_PENDING, result_); 308 EXPECT_EQ(ERR_IO_PENDING, result_);
323 EXPECT_NE(ERR_IO_PENDING, rv); 309 EXPECT_NE(ERR_IO_PENDING, rv);
324 result_ = rv; 310 result_ = rv;
325 handle_ = NULL; 311 handle_ = NULL;
326 if (list_.head()) 312 if (!list_.empty()) {
327 EXPECT_EQ(OK, result_); 313 EXPECT_EQ(OK, result_);
328 if (OK == rv) 314 EXPECT_EQ(info_.port(), list_.front().port());
329 EXPECT_EQ(info_.port(), list_.GetPort()); 315 }
330 if (handler_) 316 if (handler_)
331 handler_->Handle(this); 317 handler_->Handle(this);
332 if (quit_on_complete_) { 318 if (quit_on_complete_) {
333 MessageLoop::current()->Quit(); 319 MessageLoop::current()->Quit();
334 quit_on_complete_ = false; 320 quit_on_complete_ = false;
335 } 321 }
336 } 322 }
337 323
338 HostResolver::RequestInfo info_; 324 HostResolver::RequestInfo info_;
339 size_t index_; 325 size_t index_;
(...skipping 1017 matching lines...) Expand 10 before | Expand all | Expand 10 after
1357 EXPECT_EQ(OK, req5->Resolve()); 1343 EXPECT_EQ(OK, req5->Resolve());
1358 EXPECT_TRUE(req5->HasOneAddress("::1", 80)); 1344 EXPECT_TRUE(req5->HasOneAddress("::1", 80));
1359 1345
1360 // Request with upper case. 1346 // Request with upper case.
1361 Request* req6 = CreateRequest("er_IPV4", 80); 1347 Request* req6 = CreateRequest("er_IPV4", 80);
1362 EXPECT_EQ(OK, req6->Resolve()); 1348 EXPECT_EQ(OK, req6->Resolve());
1363 EXPECT_TRUE(req6->HasOneAddress("127.0.0.1", 80)); 1349 EXPECT_TRUE(req6->HasOneAddress("127.0.0.1", 80));
1364 } 1350 }
1365 1351
1366 } // namespace net 1352 } // namespace net
OLDNEW
« no previous file with comments | « net/base/host_resolver_impl.cc ('k') | net/base/host_resolver_proc.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698