| OLD | NEW |
| 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/dns/host_resolver_impl.h" | 5 #include "net/dns/host_resolver_impl.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <memory> | 8 #include <memory> |
| 9 #include <string> | 9 #include <string> |
| 10 #include <tuple> | 10 #include <tuple> |
| (...skipping 193 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 204 public: | 204 public: |
| 205 // Base class of handlers to be executed on completion of requests. | 205 // Base class of handlers to be executed on completion of requests. |
| 206 struct Handler { | 206 struct Handler { |
| 207 virtual ~Handler() {} | 207 virtual ~Handler() {} |
| 208 virtual void Handle(Request* request) = 0; | 208 virtual void Handle(Request* request) = 0; |
| 209 }; | 209 }; |
| 210 | 210 |
| 211 Request(const HostResolver::RequestInfo& info, | 211 Request(const HostResolver::RequestInfo& info, |
| 212 RequestPriority priority, | 212 RequestPriority priority, |
| 213 size_t index, | 213 size_t index, |
| 214 HostResolver* resolver, | 214 HostResolverImpl* resolver, |
| 215 Handler* handler) | 215 Handler* handler) |
| 216 : info_(info), | 216 : info_(info), |
| 217 priority_(priority), | 217 priority_(priority), |
| 218 index_(index), | 218 index_(index), |
| 219 resolver_(resolver), | 219 resolver_(resolver), |
| 220 handler_(handler), | 220 handler_(handler), |
| 221 quit_on_complete_(false), | 221 quit_on_complete_(false), |
| 222 result_(ERR_UNEXPECTED), | 222 result_(ERR_UNEXPECTED), |
| 223 handle_(NULL) {} | 223 handle_(NULL) {} |
| 224 | 224 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 237 EXPECT_EQ(OK, result_); | 237 EXPECT_EQ(OK, result_); |
| 238 return result_; | 238 return result_; |
| 239 } | 239 } |
| 240 | 240 |
| 241 int ResolveFromCache() { | 241 int ResolveFromCache() { |
| 242 DCHECK(resolver_); | 242 DCHECK(resolver_); |
| 243 DCHECK(!handle_); | 243 DCHECK(!handle_); |
| 244 return resolver_->ResolveFromCache(info_, &list_, BoundNetLog()); | 244 return resolver_->ResolveFromCache(info_, &list_, BoundNetLog()); |
| 245 } | 245 } |
| 246 | 246 |
| 247 int ResolveStaleFromCache() { |
| 248 DCHECK(resolver_); |
| 249 DCHECK(!handle_); |
| 250 return resolver_->ResolveStaleFromCache(info_, &list_, &staleness_, |
| 251 BoundNetLog()); |
| 252 } |
| 253 |
| 247 void ChangePriority(RequestPriority priority) { | 254 void ChangePriority(RequestPriority priority) { |
| 248 DCHECK(resolver_); | 255 DCHECK(resolver_); |
| 249 DCHECK(handle_); | 256 DCHECK(handle_); |
| 250 resolver_->ChangeRequestPriority(handle_, priority); | 257 resolver_->ChangeRequestPriority(handle_, priority); |
| 251 priority_ = priority; | 258 priority_ = priority; |
| 252 } | 259 } |
| 253 | 260 |
| 254 void Cancel() { | 261 void Cancel() { |
| 255 DCHECK(resolver_); | 262 DCHECK(resolver_); |
| 256 DCHECK(handle_); | 263 DCHECK(handle_); |
| 257 resolver_->CancelRequest(handle_); | 264 resolver_->CancelRequest(handle_); |
| 258 handle_ = NULL; | 265 handle_ = NULL; |
| 259 } | 266 } |
| 260 | 267 |
| 261 const HostResolver::RequestInfo& info() const { return info_; } | 268 const HostResolver::RequestInfo& info() const { return info_; } |
| 262 size_t index() const { return index_; } | 269 size_t index() const { return index_; } |
| 263 const AddressList& list() const { return list_; } | 270 const AddressList& list() const { return list_; } |
| 264 int result() const { return result_; } | 271 int result() const { return result_; } |
| 272 const HostCache::EntryStaleness staleness() const { return staleness_; } |
| 265 bool completed() const { return result_ != ERR_IO_PENDING; } | 273 bool completed() const { return result_ != ERR_IO_PENDING; } |
| 266 bool pending() const { return handle_ != NULL; } | 274 bool pending() const { return handle_ != NULL; } |
| 267 | 275 |
| 268 bool HasAddress(const std::string& address, uint16_t port) const { | 276 bool HasAddress(const std::string& address, uint16_t port) const { |
| 269 return AddressListContains(list_, address, port); | 277 return AddressListContains(list_, address, port); |
| 270 } | 278 } |
| 271 | 279 |
| 272 // Returns the number of addresses in |list_|. | 280 // Returns the number of addresses in |list_|. |
| 273 unsigned NumberOfAddresses() const { | 281 unsigned NumberOfAddresses() const { |
| 274 return list_.size(); | 282 return list_.size(); |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 311 handler_->Handle(this); | 319 handler_->Handle(this); |
| 312 if (quit_on_complete_) { | 320 if (quit_on_complete_) { |
| 313 base::MessageLoop::current()->QuitWhenIdle(); | 321 base::MessageLoop::current()->QuitWhenIdle(); |
| 314 quit_on_complete_ = false; | 322 quit_on_complete_ = false; |
| 315 } | 323 } |
| 316 } | 324 } |
| 317 | 325 |
| 318 HostResolver::RequestInfo info_; | 326 HostResolver::RequestInfo info_; |
| 319 RequestPriority priority_; | 327 RequestPriority priority_; |
| 320 size_t index_; | 328 size_t index_; |
| 321 HostResolver* resolver_; | 329 HostResolverImpl* resolver_; |
| 322 Handler* handler_; | 330 Handler* handler_; |
| 323 bool quit_on_complete_; | 331 bool quit_on_complete_; |
| 324 | 332 |
| 325 AddressList list_; | 333 AddressList list_; |
| 326 int result_; | 334 int result_; |
| 327 HostResolver::RequestHandle handle_; | 335 HostResolver::RequestHandle handle_; |
| 336 HostCache::EntryStaleness staleness_; |
| 328 | 337 |
| 329 DISALLOW_COPY_AND_ASSIGN(Request); | 338 DISALLOW_COPY_AND_ASSIGN(Request); |
| 330 }; | 339 }; |
| 331 | 340 |
| 332 // Using LookupAttemptHostResolverProc simulate very long lookups, and control | 341 // Using LookupAttemptHostResolverProc simulate very long lookups, and control |
| 333 // which attempt resolves the host. | 342 // which attempt resolves the host. |
| 334 class LookupAttemptHostResolverProc : public HostResolverProc { | 343 class LookupAttemptHostResolverProc : public HostResolverProc { |
| 335 public: | 344 public: |
| 336 LookupAttemptHostResolverProc(HostResolverProc* previous, | 345 LookupAttemptHostResolverProc(HostResolverProc* previous, |
| 337 int attempt_number_to_resolve, | 346 int attempt_number_to_resolve, |
| (...skipping 260 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 598 } | 607 } |
| 599 | 608 |
| 600 static unsigned maximum_dns_failures() { | 609 static unsigned maximum_dns_failures() { |
| 601 return HostResolverImpl::kMaximumDnsFailures; | 610 return HostResolverImpl::kMaximumDnsFailures; |
| 602 } | 611 } |
| 603 | 612 |
| 604 bool IsIPv6Reachable(const BoundNetLog& net_log) { | 613 bool IsIPv6Reachable(const BoundNetLog& net_log) { |
| 605 return resolver_->IsIPv6Reachable(net_log); | 614 return resolver_->IsIPv6Reachable(net_log); |
| 606 } | 615 } |
| 607 | 616 |
| 617 void MakeCacheStale() { |
| 618 DCHECK(resolver_.get()); |
| 619 resolver_->GetHostCache()->OnNetworkChange(); |
| 620 } |
| 621 |
| 608 scoped_refptr<MockHostResolverProc> proc_; | 622 scoped_refptr<MockHostResolverProc> proc_; |
| 609 std::unique_ptr<HostResolverImpl> resolver_; | 623 std::unique_ptr<HostResolverImpl> resolver_; |
| 610 std::vector<std::unique_ptr<Request>> requests_; | 624 std::vector<std::unique_ptr<Request>> requests_; |
| 611 | 625 |
| 612 std::unique_ptr<Handler> handler_; | 626 std::unique_ptr<Handler> handler_; |
| 613 }; | 627 }; |
| 614 | 628 |
| 615 TEST_F(HostResolverImplTest, AsynchronousLookup) { | 629 TEST_F(HostResolverImplTest, AsynchronousLookup) { |
| 616 proc_->AddRuleForAllFamilies("just.testing", "192.168.1.42"); | 630 proc_->AddRuleForAllFamilies("just.testing", "192.168.1.42"); |
| 617 proc_->SignalMultiple(1u); | 631 proc_->SignalMultiple(1u); |
| (...skipping 754 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1372 | 1386 |
| 1373 // This time, we fetch normally. | 1387 // This time, we fetch normally. |
| 1374 EXPECT_EQ(ERR_IO_PENDING, CreateRequest(info, DEFAULT_PRIORITY)->Resolve()); | 1388 EXPECT_EQ(ERR_IO_PENDING, CreateRequest(info, DEFAULT_PRIORITY)->Resolve()); |
| 1375 EXPECT_EQ(OK, requests_[1]->WaitForResult()); | 1389 EXPECT_EQ(OK, requests_[1]->WaitForResult()); |
| 1376 | 1390 |
| 1377 // Now we should be able to fetch from the cache. | 1391 // Now we should be able to fetch from the cache. |
| 1378 EXPECT_EQ(OK, CreateRequest(info, DEFAULT_PRIORITY)->ResolveFromCache()); | 1392 EXPECT_EQ(OK, CreateRequest(info, DEFAULT_PRIORITY)->ResolveFromCache()); |
| 1379 EXPECT_TRUE(requests_[2]->HasOneAddress("192.168.1.42", 80)); | 1393 EXPECT_TRUE(requests_[2]->HasOneAddress("192.168.1.42", 80)); |
| 1380 } | 1394 } |
| 1381 | 1395 |
| 1396 TEST_F(HostResolverImplTest, ResolveStaleFromCache) { |
| 1397 proc_->AddRuleForAllFamilies("just.testing", "192.168.1.42"); |
| 1398 proc_->SignalMultiple(1u); // Need only one. |
| 1399 |
| 1400 HostResolver::RequestInfo info(HostPortPair("just.testing", 80)); |
| 1401 |
| 1402 // First hit will miss the cache. |
| 1403 EXPECT_EQ(ERR_DNS_CACHE_MISS, |
| 1404 CreateRequest(info, DEFAULT_PRIORITY)->ResolveFromCache()); |
| 1405 |
| 1406 // This time, we fetch normally. |
| 1407 EXPECT_EQ(ERR_IO_PENDING, CreateRequest(info, DEFAULT_PRIORITY)->Resolve()); |
| 1408 EXPECT_EQ(OK, requests_[1]->WaitForResult()); |
| 1409 |
| 1410 // Now we should be able to fetch from the cache. |
| 1411 EXPECT_EQ(OK, CreateRequest(info, DEFAULT_PRIORITY)->ResolveFromCache()); |
| 1412 EXPECT_TRUE(requests_[2]->HasOneAddress("192.168.1.42", 80)); |
| 1413 EXPECT_EQ(OK, CreateRequest(info, DEFAULT_PRIORITY)->ResolveStaleFromCache()); |
| 1414 EXPECT_TRUE(requests_[3]->HasOneAddress("192.168.1.42", 80)); |
| 1415 EXPECT_FALSE(requests_[3]->staleness().is_stale()); |
| 1416 |
| 1417 MakeCacheStale(); |
| 1418 |
| 1419 // Now we should be able to fetch from the cache only if we use |
| 1420 // ResolveStaleFromCache. |
| 1421 EXPECT_EQ(ERR_DNS_CACHE_MISS, |
| 1422 CreateRequest(info, DEFAULT_PRIORITY)->ResolveFromCache()); |
| 1423 EXPECT_EQ(OK, CreateRequest(info, DEFAULT_PRIORITY)->ResolveStaleFromCache()); |
| 1424 EXPECT_TRUE(requests_[5]->HasOneAddress("192.168.1.42", 80)); |
| 1425 EXPECT_TRUE(requests_[5]->staleness().is_stale()); |
| 1426 } |
| 1427 |
| 1382 // Test the retry attempts simulating host resolver proc that takes too long. | 1428 // Test the retry attempts simulating host resolver proc that takes too long. |
| 1383 TEST_F(HostResolverImplTest, MultipleAttempts) { | 1429 TEST_F(HostResolverImplTest, MultipleAttempts) { |
| 1384 // Total number of attempts would be 3 and we want the 3rd attempt to resolve | 1430 // Total number of attempts would be 3 and we want the 3rd attempt to resolve |
| 1385 // the host. First and second attempt will be forced to sleep until they get | 1431 // the host. First and second attempt will be forced to sleep until they get |
| 1386 // word that a resolution has completed. The 3rd resolution attempt will try | 1432 // word that a resolution has completed. The 3rd resolution attempt will try |
| 1387 // to get done ASAP, and won't sleep.. | 1433 // to get done ASAP, and won't sleep.. |
| 1388 int kAttemptNumberToResolve = 3; | 1434 int kAttemptNumberToResolve = 3; |
| 1389 int kTotalAttempts = 3; | 1435 int kTotalAttempts = 3; |
| 1390 | 1436 |
| 1391 scoped_refptr<LookupAttemptHostResolverProc> resolver_proc( | 1437 scoped_refptr<LookupAttemptHostResolverProc> resolver_proc( |
| (...skipping 924 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2316 EXPECT_FALSE(ResolveLocalHostname("::1:1", kLocalhostLookupPort, &addresses)); | 2362 EXPECT_FALSE(ResolveLocalHostname("::1:1", kLocalhostLookupPort, &addresses)); |
| 2317 EXPECT_FALSE(ResolveLocalHostname("0:0:0:0:0:0:0:0:1", kLocalhostLookupPort, | 2363 EXPECT_FALSE(ResolveLocalHostname("0:0:0:0:0:0:0:0:1", kLocalhostLookupPort, |
| 2318 &addresses)); | 2364 &addresses)); |
| 2319 EXPECT_FALSE(ResolveLocalHostname("foo.localhost.com", kLocalhostLookupPort, | 2365 EXPECT_FALSE(ResolveLocalHostname("foo.localhost.com", kLocalhostLookupPort, |
| 2320 &addresses)); | 2366 &addresses)); |
| 2321 EXPECT_FALSE( | 2367 EXPECT_FALSE( |
| 2322 ResolveLocalHostname("foo.localhoste", kLocalhostLookupPort, &addresses)); | 2368 ResolveLocalHostname("foo.localhoste", kLocalhostLookupPort, &addresses)); |
| 2323 } | 2369 } |
| 2324 | 2370 |
| 2325 } // namespace net | 2371 } // namespace net |
| OLD | NEW |