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

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

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

Powered by Google App Engine
This is Rietveld 408576698