| 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 206 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 217 RequestPriority priority, | 217 RequestPriority priority, |
| 218 size_t index, | 218 size_t index, |
| 219 HostResolverImpl* resolver, | 219 HostResolverImpl* resolver, |
| 220 Handler* handler) | 220 Handler* handler) |
| 221 : info_(info), | 221 : info_(info), |
| 222 priority_(priority), | 222 priority_(priority), |
| 223 index_(index), | 223 index_(index), |
| 224 resolver_(resolver), | 224 resolver_(resolver), |
| 225 handler_(handler), | 225 handler_(handler), |
| 226 quit_on_complete_(false), | 226 quit_on_complete_(false), |
| 227 result_(ERR_UNEXPECTED), | 227 result_(ERR_UNEXPECTED) {} |
| 228 handle_(NULL) {} | |
| 229 | 228 |
| 230 int Resolve() { | 229 int Resolve() { |
| 231 DCHECK(resolver_); | 230 DCHECK(resolver_); |
| 232 DCHECK(!handle_); | 231 DCHECK(!request_); |
| 233 list_ = AddressList(); | 232 list_ = AddressList(); |
| 234 result_ = resolver_->Resolve( | 233 result_ = resolver_->Resolve( |
| 235 info_, | 234 info_, priority_, &list_, |
| 236 priority_, | 235 base::Bind(&Request::OnComplete, base::Unretained(this)), &request_, |
| 237 &list_, | |
| 238 base::Bind(&Request::OnComplete, base::Unretained(this)), | |
| 239 &handle_, | |
| 240 BoundNetLog()); | 236 BoundNetLog()); |
| 241 if (!list_.empty()) | 237 if (!list_.empty()) |
| 242 EXPECT_THAT(result_, IsOk()); | 238 EXPECT_THAT(result_, IsOk()); |
| 243 return result_; | 239 return result_; |
| 244 } | 240 } |
| 245 | 241 |
| 246 int ResolveFromCache() { | 242 int ResolveFromCache() { |
| 247 DCHECK(resolver_); | 243 DCHECK(resolver_); |
| 248 DCHECK(!handle_); | 244 DCHECK(!request_); |
| 249 return resolver_->ResolveFromCache(info_, &list_, BoundNetLog()); | 245 return resolver_->ResolveFromCache(info_, &list_, BoundNetLog()); |
| 250 } | 246 } |
| 251 | 247 |
| 252 int ResolveStaleFromCache() { | 248 int ResolveStaleFromCache() { |
| 253 DCHECK(resolver_); | 249 DCHECK(resolver_); |
| 254 DCHECK(!handle_); | 250 DCHECK(!request_); |
| 255 return resolver_->ResolveStaleFromCache(info_, &list_, &staleness_, | 251 return resolver_->ResolveStaleFromCache(info_, &list_, &staleness_, |
| 256 BoundNetLog()); | 252 BoundNetLog()); |
| 257 } | 253 } |
| 258 | 254 |
| 259 void ChangePriority(RequestPriority priority) { | 255 void ChangePriority(RequestPriority priority) { |
| 260 DCHECK(resolver_); | 256 DCHECK(resolver_); |
| 261 DCHECK(handle_); | 257 DCHECK(request_); |
| 262 resolver_->ChangeRequestPriority(handle_, priority); | 258 request_->ChangeRequestPriority(priority); |
| 263 priority_ = priority; | 259 priority_ = priority; |
| 264 } | 260 } |
| 265 | 261 |
| 266 void Cancel() { | 262 void Cancel() { |
| 267 DCHECK(resolver_); | 263 DCHECK(resolver_); |
| 268 DCHECK(handle_); | 264 DCHECK(request_); |
| 269 resolver_->CancelRequest(handle_); | 265 request_.reset(); |
| 270 handle_ = NULL; | |
| 271 } | 266 } |
| 272 | 267 |
| 273 const HostResolver::RequestInfo& info() const { return info_; } | 268 const HostResolver::RequestInfo& info() const { return info_; } |
| 274 size_t index() const { return index_; } | 269 size_t index() const { return index_; } |
| 275 const AddressList& list() const { return list_; } | 270 const AddressList& list() const { return list_; } |
| 276 int result() const { return result_; } | 271 int result() const { return result_; } |
| 277 const HostCache::EntryStaleness staleness() const { return staleness_; } | 272 const HostCache::EntryStaleness staleness() const { return staleness_; } |
| 278 bool completed() const { return result_ != ERR_IO_PENDING; } | 273 bool completed() const { return result_ != ERR_IO_PENDING; } |
| 279 bool pending() const { return handle_ != NULL; } | 274 bool pending() const { return request_ != nullptr; } |
| 280 | 275 |
| 281 bool HasAddress(const std::string& address, uint16_t port) const { | 276 bool HasAddress(const std::string& address, uint16_t port) const { |
| 282 return AddressListContains(list_, address, port); | 277 return AddressListContains(list_, address, port); |
| 283 } | 278 } |
| 284 | 279 |
| 285 // Returns the number of addresses in |list_|. | 280 // Returns the number of addresses in |list_|. |
| 286 unsigned NumberOfAddresses() const { | 281 unsigned NumberOfAddresses() const { |
| 287 return list_.size(); | 282 return list_.size(); |
| 288 } | 283 } |
| 289 | 284 |
| (...skipping 18 matching lines...) Expand all Loading... |
| 308 else | 303 else |
| 309 return ERR_UNEXPECTED; | 304 return ERR_UNEXPECTED; |
| 310 } | 305 } |
| 311 | 306 |
| 312 private: | 307 private: |
| 313 void OnComplete(int rv) { | 308 void OnComplete(int rv) { |
| 314 EXPECT_TRUE(pending()); | 309 EXPECT_TRUE(pending()); |
| 315 EXPECT_THAT(result_, IsError(ERR_IO_PENDING)); | 310 EXPECT_THAT(result_, IsError(ERR_IO_PENDING)); |
| 316 EXPECT_NE(ERR_IO_PENDING, rv); | 311 EXPECT_NE(ERR_IO_PENDING, rv); |
| 317 result_ = rv; | 312 result_ = rv; |
| 318 handle_ = NULL; | 313 request_.reset(); |
| 319 if (!list_.empty()) { | 314 if (!list_.empty()) { |
| 320 EXPECT_THAT(result_, IsOk()); | 315 EXPECT_THAT(result_, IsOk()); |
| 321 EXPECT_EQ(info_.port(), list_.front().port()); | 316 EXPECT_EQ(info_.port(), list_.front().port()); |
| 322 } | 317 } |
| 323 if (handler_) | 318 if (handler_) |
| 324 handler_->Handle(this); | 319 handler_->Handle(this); |
| 325 if (quit_on_complete_) { | 320 if (quit_on_complete_) { |
| 326 base::MessageLoop::current()->QuitWhenIdle(); | 321 base::MessageLoop::current()->QuitWhenIdle(); |
| 327 quit_on_complete_ = false; | 322 quit_on_complete_ = false; |
| 328 } | 323 } |
| 329 } | 324 } |
| 330 | 325 |
| 331 HostResolver::RequestInfo info_; | 326 HostResolver::RequestInfo info_; |
| 332 RequestPriority priority_; | 327 RequestPriority priority_; |
| 333 size_t index_; | 328 size_t index_; |
| 334 HostResolverImpl* resolver_; | 329 HostResolverImpl* resolver_; |
| 335 Handler* handler_; | 330 Handler* handler_; |
| 336 bool quit_on_complete_; | 331 bool quit_on_complete_; |
| 337 | 332 |
| 338 AddressList list_; | 333 AddressList list_; |
| 339 int result_; | 334 int result_; |
| 340 HostResolver::RequestHandle handle_; | 335 std::unique_ptr<HostResolver::Request> request_; |
| 341 HostCache::EntryStaleness staleness_; | 336 HostCache::EntryStaleness staleness_; |
| 342 | 337 |
| 343 DISALLOW_COPY_AND_ASSIGN(Request); | 338 DISALLOW_COPY_AND_ASSIGN(Request); |
| 344 }; | 339 }; |
| 345 | 340 |
| 346 // Using LookupAttemptHostResolverProc simulate very long lookups, and control | 341 // Using LookupAttemptHostResolverProc simulate very long lookups, and control |
| 347 // which attempt resolves the host. | 342 // which attempt resolves the host. |
| 348 class LookupAttemptHostResolverProc : public HostResolverProc { | 343 class LookupAttemptHostResolverProc : public HostResolverProc { |
| 349 public: | 344 public: |
| 350 LookupAttemptHostResolverProc(HostResolverProc* previous, | 345 LookupAttemptHostResolverProc(HostResolverProc* previous, |
| (...skipping 282 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 633 | 628 |
| 634 TEST_F(HostResolverImplTest, AsynchronousLookup) { | 629 TEST_F(HostResolverImplTest, AsynchronousLookup) { |
| 635 proc_->AddRuleForAllFamilies("just.testing", "192.168.1.42"); | 630 proc_->AddRuleForAllFamilies("just.testing", "192.168.1.42"); |
| 636 proc_->SignalMultiple(1u); | 631 proc_->SignalMultiple(1u); |
| 637 | 632 |
| 638 Request* req = CreateRequest("just.testing", 80); | 633 Request* req = CreateRequest("just.testing", 80); |
| 639 EXPECT_THAT(req->Resolve(), IsError(ERR_IO_PENDING)); | 634 EXPECT_THAT(req->Resolve(), IsError(ERR_IO_PENDING)); |
| 640 EXPECT_THAT(req->WaitForResult(), IsOk()); | 635 EXPECT_THAT(req->WaitForResult(), IsOk()); |
| 641 | 636 |
| 642 EXPECT_TRUE(req->HasOneAddress("192.168.1.42", 80)); | 637 EXPECT_TRUE(req->HasOneAddress("192.168.1.42", 80)); |
| 643 | |
| 644 EXPECT_EQ("just.testing", proc_->GetCaptureList()[0].hostname); | 638 EXPECT_EQ("just.testing", proc_->GetCaptureList()[0].hostname); |
| 645 } | 639 } |
| 646 | 640 |
| 647 // RFC 6761 localhost names should always resolve to loopback. | 641 // RFC 6761 localhost names should always resolve to loopback. |
| 648 TEST_F(HostResolverImplTest, LocalhostLookup) { | 642 TEST_F(HostResolverImplTest, LocalhostLookup) { |
| 649 // Add a rule resolving localhost names to a non-loopback IP and test | 643 // Add a rule resolving localhost names to a non-loopback IP and test |
| 650 // that they still resolves to loopback. | 644 // that they still resolves to loopback. |
| 651 proc_->AddRuleForAllFamilies("foo.localhost", "192.168.1.42"); | 645 proc_->AddRuleForAllFamilies("foo.localhost", "192.168.1.42"); |
| 652 proc_->AddRuleForAllFamilies("localhost", "192.168.1.42"); | 646 proc_->AddRuleForAllFamilies("localhost", "192.168.1.42"); |
| 653 proc_->AddRuleForAllFamilies("localhost.", "192.168.1.42"); | 647 proc_->AddRuleForAllFamilies("localhost.", "192.168.1.42"); |
| (...skipping 1744 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2398 EXPECT_FALSE(ResolveLocalHostname("::1:1", kLocalhostLookupPort, &addresses)); | 2392 EXPECT_FALSE(ResolveLocalHostname("::1:1", kLocalhostLookupPort, &addresses)); |
| 2399 EXPECT_FALSE(ResolveLocalHostname("0:0:0:0:0:0:0:0:1", kLocalhostLookupPort, | 2393 EXPECT_FALSE(ResolveLocalHostname("0:0:0:0:0:0:0:0:1", kLocalhostLookupPort, |
| 2400 &addresses)); | 2394 &addresses)); |
| 2401 EXPECT_FALSE(ResolveLocalHostname("foo.localhost.com", kLocalhostLookupPort, | 2395 EXPECT_FALSE(ResolveLocalHostname("foo.localhost.com", kLocalhostLookupPort, |
| 2402 &addresses)); | 2396 &addresses)); |
| 2403 EXPECT_FALSE( | 2397 EXPECT_FALSE( |
| 2404 ResolveLocalHostname("foo.localhoste", kLocalhostLookupPort, &addresses)); | 2398 ResolveLocalHostname("foo.localhoste", kLocalhostLookupPort, &addresses)); |
| 2405 } | 2399 } |
| 2406 | 2400 |
| 2407 } // namespace net | 2401 } // namespace net |
| OLD | NEW |