OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "net/dns/host_resolver_impl.h" | |
6 | |
7 #include <algorithm> | |
8 #include <string> | |
9 | |
10 #include "base/bind.h" | |
11 #include "base/bind_helpers.h" | |
12 #include "base/memory/ref_counted.h" | |
13 #include "base/memory/scoped_vector.h" | |
14 #include "base/message_loop/message_loop.h" | |
15 #include "base/run_loop.h" | |
16 #include "base/strings/string_util.h" | |
17 #include "base/strings/stringprintf.h" | |
18 #include "base/synchronization/condition_variable.h" | |
19 #include "base/synchronization/lock.h" | |
20 #include "base/test/test_timeouts.h" | |
21 #include "base/time/time.h" | |
22 #include "net/base/address_list.h" | |
23 #include "net/base/net_errors.h" | |
24 #include "net/base/net_util.h" | |
25 #include "net/dns/dns_client.h" | |
26 #include "net/dns/dns_test_util.h" | |
27 #include "net/dns/mock_host_resolver.h" | |
28 #include "testing/gtest/include/gtest/gtest.h" | |
29 | |
30 namespace net { | |
31 | |
32 namespace { | |
33 | |
34 const size_t kMaxJobs = 10u; | |
35 const size_t kMaxRetryAttempts = 4u; | |
36 | |
37 HostResolver::Options DefaultOptions() { | |
38 HostResolver::Options options; | |
39 options.max_concurrent_resolves = kMaxJobs; | |
40 options.max_retry_attempts = kMaxRetryAttempts; | |
41 options.enable_caching = true; | |
42 return options; | |
43 } | |
44 | |
45 HostResolverImpl::ProcTaskParams DefaultParams( | |
46 HostResolverProc* resolver_proc) { | |
47 return HostResolverImpl::ProcTaskParams(resolver_proc, kMaxRetryAttempts); | |
48 } | |
49 | |
50 // A HostResolverProc that pushes each host mapped into a list and allows | |
51 // waiting for a specific number of requests. Unlike RuleBasedHostResolverProc | |
52 // it never calls SystemHostResolverCall. By default resolves all hostnames to | |
53 // "127.0.0.1". After AddRule(), it resolves only names explicitly specified. | |
54 class MockHostResolverProc : public HostResolverProc { | |
55 public: | |
56 struct ResolveKey { | |
57 ResolveKey(const std::string& hostname, AddressFamily address_family) | |
58 : hostname(hostname), address_family(address_family) {} | |
59 bool operator<(const ResolveKey& other) const { | |
60 return address_family < other.address_family || | |
61 (address_family == other.address_family && hostname < other.hostname); | |
62 } | |
63 std::string hostname; | |
64 AddressFamily address_family; | |
65 }; | |
66 | |
67 typedef std::vector<ResolveKey> CaptureList; | |
68 | |
69 MockHostResolverProc() | |
70 : HostResolverProc(NULL), | |
71 num_requests_waiting_(0), | |
72 num_slots_available_(0), | |
73 requests_waiting_(&lock_), | |
74 slots_available_(&lock_) { | |
75 } | |
76 | |
77 // Waits until |count| calls to |Resolve| are blocked. Returns false when | |
78 // timed out. | |
79 bool WaitFor(unsigned count) { | |
80 base::AutoLock lock(lock_); | |
81 base::Time start_time = base::Time::Now(); | |
82 while (num_requests_waiting_ < count) { | |
83 requests_waiting_.TimedWait(TestTimeouts::action_timeout()); | |
84 if (base::Time::Now() > start_time + TestTimeouts::action_timeout()) | |
85 return false; | |
86 } | |
87 return true; | |
88 } | |
89 | |
90 // Signals |count| waiting calls to |Resolve|. First come first served. | |
91 void SignalMultiple(unsigned count) { | |
92 base::AutoLock lock(lock_); | |
93 num_slots_available_ += count; | |
94 slots_available_.Broadcast(); | |
95 } | |
96 | |
97 // Signals all waiting calls to |Resolve|. Beware of races. | |
98 void SignalAll() { | |
99 base::AutoLock lock(lock_); | |
100 num_slots_available_ = num_requests_waiting_; | |
101 slots_available_.Broadcast(); | |
102 } | |
103 | |
104 void AddRule(const std::string& hostname, AddressFamily family, | |
105 const AddressList& result) { | |
106 base::AutoLock lock(lock_); | |
107 rules_[ResolveKey(hostname, family)] = result; | |
108 } | |
109 | |
110 void AddRule(const std::string& hostname, AddressFamily family, | |
111 const std::string& ip_list) { | |
112 AddressList result; | |
113 int rv = ParseAddressList(ip_list, std::string(), &result); | |
114 DCHECK_EQ(OK, rv); | |
115 AddRule(hostname, family, result); | |
116 } | |
117 | |
118 void AddRuleForAllFamilies(const std::string& hostname, | |
119 const std::string& ip_list) { | |
120 AddressList result; | |
121 int rv = ParseAddressList(ip_list, std::string(), &result); | |
122 DCHECK_EQ(OK, rv); | |
123 AddRule(hostname, ADDRESS_FAMILY_UNSPECIFIED, result); | |
124 AddRule(hostname, ADDRESS_FAMILY_IPV4, result); | |
125 AddRule(hostname, ADDRESS_FAMILY_IPV6, result); | |
126 } | |
127 | |
128 int Resolve(const std::string& hostname, | |
129 AddressFamily address_family, | |
130 HostResolverFlags host_resolver_flags, | |
131 AddressList* addrlist, | |
132 int* os_error) override { | |
133 base::AutoLock lock(lock_); | |
134 capture_list_.push_back(ResolveKey(hostname, address_family)); | |
135 ++num_requests_waiting_; | |
136 requests_waiting_.Broadcast(); | |
137 while (!num_slots_available_) | |
138 slots_available_.Wait(); | |
139 DCHECK_GT(num_requests_waiting_, 0u); | |
140 --num_slots_available_; | |
141 --num_requests_waiting_; | |
142 if (rules_.empty()) { | |
143 int rv = ParseAddressList("127.0.0.1", std::string(), addrlist); | |
144 DCHECK_EQ(OK, rv); | |
145 return OK; | |
146 } | |
147 ResolveKey key(hostname, address_family); | |
148 if (rules_.count(key) == 0) | |
149 return ERR_NAME_NOT_RESOLVED; | |
150 *addrlist = rules_[key]; | |
151 return OK; | |
152 } | |
153 | |
154 CaptureList GetCaptureList() const { | |
155 CaptureList copy; | |
156 { | |
157 base::AutoLock lock(lock_); | |
158 copy = capture_list_; | |
159 } | |
160 return copy; | |
161 } | |
162 | |
163 bool HasBlockedRequests() const { | |
164 base::AutoLock lock(lock_); | |
165 return num_requests_waiting_ > num_slots_available_; | |
166 } | |
167 | |
168 protected: | |
169 ~MockHostResolverProc() override {} | |
170 | |
171 private: | |
172 mutable base::Lock lock_; | |
173 std::map<ResolveKey, AddressList> rules_; | |
174 CaptureList capture_list_; | |
175 unsigned num_requests_waiting_; | |
176 unsigned num_slots_available_; | |
177 base::ConditionVariable requests_waiting_; | |
178 base::ConditionVariable slots_available_; | |
179 | |
180 DISALLOW_COPY_AND_ASSIGN(MockHostResolverProc); | |
181 }; | |
182 | |
183 bool AddressListContains(const AddressList& list, const std::string& address, | |
184 uint16 port) { | |
185 IPAddressNumber ip; | |
186 bool rv = ParseIPLiteralToNumber(address, &ip); | |
187 DCHECK(rv); | |
188 return std::find(list.begin(), | |
189 list.end(), | |
190 IPEndPoint(ip, port)) != list.end(); | |
191 } | |
192 | |
193 // A wrapper for requests to a HostResolver. | |
194 class Request { | |
195 public: | |
196 // Base class of handlers to be executed on completion of requests. | |
197 struct Handler { | |
198 virtual ~Handler() {} | |
199 virtual void Handle(Request* request) = 0; | |
200 }; | |
201 | |
202 Request(const HostResolver::RequestInfo& info, | |
203 RequestPriority priority, | |
204 size_t index, | |
205 HostResolver* resolver, | |
206 Handler* handler) | |
207 : info_(info), | |
208 priority_(priority), | |
209 index_(index), | |
210 resolver_(resolver), | |
211 handler_(handler), | |
212 quit_on_complete_(false), | |
213 result_(ERR_UNEXPECTED), | |
214 handle_(NULL) {} | |
215 | |
216 int Resolve() { | |
217 DCHECK(resolver_); | |
218 DCHECK(!handle_); | |
219 list_ = AddressList(); | |
220 result_ = resolver_->Resolve( | |
221 info_, | |
222 priority_, | |
223 &list_, | |
224 base::Bind(&Request::OnComplete, base::Unretained(this)), | |
225 &handle_, | |
226 BoundNetLog()); | |
227 if (!list_.empty()) | |
228 EXPECT_EQ(OK, result_); | |
229 return result_; | |
230 } | |
231 | |
232 int ResolveFromCache() { | |
233 DCHECK(resolver_); | |
234 DCHECK(!handle_); | |
235 return resolver_->ResolveFromCache(info_, &list_, BoundNetLog()); | |
236 } | |
237 | |
238 void Cancel() { | |
239 DCHECK(resolver_); | |
240 DCHECK(handle_); | |
241 resolver_->CancelRequest(handle_); | |
242 handle_ = NULL; | |
243 } | |
244 | |
245 const HostResolver::RequestInfo& info() const { return info_; } | |
246 size_t index() const { return index_; } | |
247 const AddressList& list() const { return list_; } | |
248 int result() const { return result_; } | |
249 bool completed() const { return result_ != ERR_IO_PENDING; } | |
250 bool pending() const { return handle_ != NULL; } | |
251 | |
252 bool HasAddress(const std::string& address, uint16 port) const { | |
253 return AddressListContains(list_, address, port); | |
254 } | |
255 | |
256 // Returns the number of addresses in |list_|. | |
257 unsigned NumberOfAddresses() const { | |
258 return list_.size(); | |
259 } | |
260 | |
261 bool HasOneAddress(const std::string& address, uint16 port) const { | |
262 return HasAddress(address, port) && (NumberOfAddresses() == 1u); | |
263 } | |
264 | |
265 // Returns ERR_UNEXPECTED if timed out. | |
266 int WaitForResult() { | |
267 if (completed()) | |
268 return result_; | |
269 base::CancelableClosure closure(base::MessageLoop::QuitClosure()); | |
270 base::MessageLoop::current()->PostDelayedTask( | |
271 FROM_HERE, closure.callback(), TestTimeouts::action_max_timeout()); | |
272 quit_on_complete_ = true; | |
273 base::MessageLoop::current()->Run(); | |
274 bool did_quit = !quit_on_complete_; | |
275 quit_on_complete_ = false; | |
276 closure.Cancel(); | |
277 if (did_quit) | |
278 return result_; | |
279 else | |
280 return ERR_UNEXPECTED; | |
281 } | |
282 | |
283 private: | |
284 void OnComplete(int rv) { | |
285 EXPECT_TRUE(pending()); | |
286 EXPECT_EQ(ERR_IO_PENDING, result_); | |
287 EXPECT_NE(ERR_IO_PENDING, rv); | |
288 result_ = rv; | |
289 handle_ = NULL; | |
290 if (!list_.empty()) { | |
291 EXPECT_EQ(OK, result_); | |
292 EXPECT_EQ(info_.port(), list_.front().port()); | |
293 } | |
294 if (handler_) | |
295 handler_->Handle(this); | |
296 if (quit_on_complete_) { | |
297 base::MessageLoop::current()->Quit(); | |
298 quit_on_complete_ = false; | |
299 } | |
300 } | |
301 | |
302 HostResolver::RequestInfo info_; | |
303 RequestPriority priority_; | |
304 size_t index_; | |
305 HostResolver* resolver_; | |
306 Handler* handler_; | |
307 bool quit_on_complete_; | |
308 | |
309 AddressList list_; | |
310 int result_; | |
311 HostResolver::RequestHandle handle_; | |
312 | |
313 DISALLOW_COPY_AND_ASSIGN(Request); | |
314 }; | |
315 | |
316 // Using LookupAttemptHostResolverProc simulate very long lookups, and control | |
317 // which attempt resolves the host. | |
318 class LookupAttemptHostResolverProc : public HostResolverProc { | |
319 public: | |
320 LookupAttemptHostResolverProc(HostResolverProc* previous, | |
321 int attempt_number_to_resolve, | |
322 int total_attempts) | |
323 : HostResolverProc(previous), | |
324 attempt_number_to_resolve_(attempt_number_to_resolve), | |
325 current_attempt_number_(0), | |
326 total_attempts_(total_attempts), | |
327 total_attempts_resolved_(0), | |
328 resolved_attempt_number_(0), | |
329 all_done_(&lock_) { | |
330 } | |
331 | |
332 // Test harness will wait for all attempts to finish before checking the | |
333 // results. | |
334 void WaitForAllAttemptsToFinish(const base::TimeDelta& wait_time) { | |
335 base::TimeTicks end_time = base::TimeTicks::Now() + wait_time; | |
336 { | |
337 base::AutoLock auto_lock(lock_); | |
338 while (total_attempts_resolved_ != total_attempts_ && | |
339 base::TimeTicks::Now() < end_time) { | |
340 all_done_.TimedWait(end_time - base::TimeTicks::Now()); | |
341 } | |
342 } | |
343 } | |
344 | |
345 // All attempts will wait for an attempt to resolve the host. | |
346 void WaitForAnAttemptToComplete() { | |
347 base::TimeDelta wait_time = base::TimeDelta::FromSeconds(60); | |
348 base::TimeTicks end_time = base::TimeTicks::Now() + wait_time; | |
349 { | |
350 base::AutoLock auto_lock(lock_); | |
351 while (resolved_attempt_number_ == 0 && base::TimeTicks::Now() < end_time) | |
352 all_done_.TimedWait(end_time - base::TimeTicks::Now()); | |
353 } | |
354 all_done_.Broadcast(); // Tell all waiting attempts to proceed. | |
355 } | |
356 | |
357 // Returns the number of attempts that have finished the Resolve() method. | |
358 int total_attempts_resolved() { return total_attempts_resolved_; } | |
359 | |
360 // Returns the first attempt that that has resolved the host. | |
361 int resolved_attempt_number() { return resolved_attempt_number_; } | |
362 | |
363 // HostResolverProc methods. | |
364 int Resolve(const std::string& host, | |
365 AddressFamily address_family, | |
366 HostResolverFlags host_resolver_flags, | |
367 AddressList* addrlist, | |
368 int* os_error) override { | |
369 bool wait_for_right_attempt_to_complete = true; | |
370 { | |
371 base::AutoLock auto_lock(lock_); | |
372 ++current_attempt_number_; | |
373 if (current_attempt_number_ == attempt_number_to_resolve_) { | |
374 resolved_attempt_number_ = current_attempt_number_; | |
375 wait_for_right_attempt_to_complete = false; | |
376 } | |
377 } | |
378 | |
379 if (wait_for_right_attempt_to_complete) | |
380 // Wait for the attempt_number_to_resolve_ attempt to resolve. | |
381 WaitForAnAttemptToComplete(); | |
382 | |
383 int result = ResolveUsingPrevious(host, address_family, host_resolver_flags, | |
384 addrlist, os_error); | |
385 | |
386 { | |
387 base::AutoLock auto_lock(lock_); | |
388 ++total_attempts_resolved_; | |
389 } | |
390 | |
391 all_done_.Broadcast(); // Tell all attempts to proceed. | |
392 | |
393 // Since any negative number is considered a network error, with -1 having | |
394 // special meaning (ERR_IO_PENDING). We could return the attempt that has | |
395 // resolved the host as a negative number. For example, if attempt number 3 | |
396 // resolves the host, then this method returns -4. | |
397 if (result == OK) | |
398 return -1 - resolved_attempt_number_; | |
399 else | |
400 return result; | |
401 } | |
402 | |
403 protected: | |
404 ~LookupAttemptHostResolverProc() override {} | |
405 | |
406 private: | |
407 int attempt_number_to_resolve_; | |
408 int current_attempt_number_; // Incremented whenever Resolve is called. | |
409 int total_attempts_; | |
410 int total_attempts_resolved_; | |
411 int resolved_attempt_number_; | |
412 | |
413 // All attempts wait for right attempt to be resolve. | |
414 base::Lock lock_; | |
415 base::ConditionVariable all_done_; | |
416 }; | |
417 | |
418 } // namespace | |
419 | |
420 class HostResolverImplTest : public testing::Test { | |
421 public: | |
422 static const int kDefaultPort = 80; | |
423 | |
424 HostResolverImplTest() : proc_(new MockHostResolverProc()) {} | |
425 | |
426 void CreateResolver() { | |
427 CreateResolverWithLimitsAndParams(kMaxJobs, | |
428 DefaultParams(proc_.get())); | |
429 } | |
430 | |
431 // This HostResolverImpl will only allow 1 outstanding resolve at a time and | |
432 // perform no retries. | |
433 void CreateSerialResolver() { | |
434 HostResolverImpl::ProcTaskParams params = DefaultParams(proc_.get()); | |
435 params.max_retry_attempts = 0u; | |
436 CreateResolverWithLimitsAndParams(1u, params); | |
437 } | |
438 | |
439 protected: | |
440 // A Request::Handler which is a proxy to the HostResolverImplTest fixture. | |
441 struct Handler : public Request::Handler { | |
442 ~Handler() override {} | |
443 | |
444 // Proxy functions so that classes derived from Handler can access them. | |
445 Request* CreateRequest(const HostResolver::RequestInfo& info, | |
446 RequestPriority priority) { | |
447 return test->CreateRequest(info, priority); | |
448 } | |
449 Request* CreateRequest(const std::string& hostname, int port) { | |
450 return test->CreateRequest(hostname, port); | |
451 } | |
452 Request* CreateRequest(const std::string& hostname) { | |
453 return test->CreateRequest(hostname); | |
454 } | |
455 ScopedVector<Request>& requests() { return test->requests_; } | |
456 | |
457 void DeleteResolver() { test->resolver_.reset(); } | |
458 | |
459 HostResolverImplTest* test; | |
460 }; | |
461 | |
462 // testing::Test implementation: | |
463 void SetUp() override { CreateResolver(); } | |
464 | |
465 void TearDown() override { | |
466 if (resolver_.get()) | |
467 EXPECT_EQ(0u, resolver_->num_running_dispatcher_jobs_for_tests()); | |
468 EXPECT_FALSE(proc_->HasBlockedRequests()); | |
469 } | |
470 | |
471 virtual void CreateResolverWithLimitsAndParams( | |
472 size_t max_concurrent_resolves, | |
473 const HostResolverImpl::ProcTaskParams& params) { | |
474 HostResolverImpl::Options options = DefaultOptions(); | |
475 options.max_concurrent_resolves = max_concurrent_resolves; | |
476 resolver_.reset(new HostResolverImpl(options, NULL)); | |
477 resolver_->set_proc_params_for_test(params); | |
478 } | |
479 | |
480 // The Request will not be made until a call to |Resolve()|, and the Job will | |
481 // not start until released by |proc_->SignalXXX|. | |
482 Request* CreateRequest(const HostResolver::RequestInfo& info, | |
483 RequestPriority priority) { | |
484 Request* req = new Request( | |
485 info, priority, requests_.size(), resolver_.get(), handler_.get()); | |
486 requests_.push_back(req); | |
487 return req; | |
488 } | |
489 | |
490 Request* CreateRequest(const std::string& hostname, | |
491 int port, | |
492 RequestPriority priority, | |
493 AddressFamily family) { | |
494 HostResolver::RequestInfo info(HostPortPair(hostname, port)); | |
495 info.set_address_family(family); | |
496 return CreateRequest(info, priority); | |
497 } | |
498 | |
499 Request* CreateRequest(const std::string& hostname, | |
500 int port, | |
501 RequestPriority priority) { | |
502 return CreateRequest(hostname, port, priority, ADDRESS_FAMILY_UNSPECIFIED); | |
503 } | |
504 | |
505 Request* CreateRequest(const std::string& hostname, int port) { | |
506 return CreateRequest(hostname, port, MEDIUM); | |
507 } | |
508 | |
509 Request* CreateRequest(const std::string& hostname) { | |
510 return CreateRequest(hostname, kDefaultPort); | |
511 } | |
512 | |
513 void set_handler(Handler* handler) { | |
514 handler_.reset(handler); | |
515 handler_->test = this; | |
516 } | |
517 | |
518 // Friendship is not inherited, so use proxies to access those. | |
519 size_t num_running_dispatcher_jobs() const { | |
520 DCHECK(resolver_.get()); | |
521 return resolver_->num_running_dispatcher_jobs_for_tests(); | |
522 } | |
523 | |
524 void set_fallback_to_proctask(bool fallback_to_proctask) { | |
525 DCHECK(resolver_.get()); | |
526 resolver_->fallback_to_proctask_ = fallback_to_proctask; | |
527 } | |
528 | |
529 static unsigned maximum_dns_failures() { | |
530 return HostResolverImpl::kMaximumDnsFailures; | |
531 } | |
532 | |
533 scoped_refptr<MockHostResolverProc> proc_; | |
534 scoped_ptr<HostResolverImpl> resolver_; | |
535 ScopedVector<Request> requests_; | |
536 | |
537 scoped_ptr<Handler> handler_; | |
538 }; | |
539 | |
540 TEST_F(HostResolverImplTest, AsynchronousLookup) { | |
541 proc_->AddRuleForAllFamilies("just.testing", "192.168.1.42"); | |
542 proc_->SignalMultiple(1u); | |
543 | |
544 Request* req = CreateRequest("just.testing", 80); | |
545 EXPECT_EQ(ERR_IO_PENDING, req->Resolve()); | |
546 EXPECT_EQ(OK, req->WaitForResult()); | |
547 | |
548 EXPECT_TRUE(req->HasOneAddress("192.168.1.42", 80)); | |
549 | |
550 EXPECT_EQ("just.testing", proc_->GetCaptureList()[0].hostname); | |
551 } | |
552 | |
553 TEST_F(HostResolverImplTest, EmptyListMeansNameNotResolved) { | |
554 proc_->AddRuleForAllFamilies("just.testing", ""); | |
555 proc_->SignalMultiple(1u); | |
556 | |
557 Request* req = CreateRequest("just.testing", 80); | |
558 EXPECT_EQ(ERR_IO_PENDING, req->Resolve()); | |
559 EXPECT_EQ(ERR_NAME_NOT_RESOLVED, req->WaitForResult()); | |
560 EXPECT_EQ(0u, req->NumberOfAddresses()); | |
561 EXPECT_EQ("just.testing", proc_->GetCaptureList()[0].hostname); | |
562 } | |
563 | |
564 TEST_F(HostResolverImplTest, FailedAsynchronousLookup) { | |
565 proc_->AddRuleForAllFamilies(std::string(), | |
566 "0.0.0.0"); // Default to failures. | |
567 proc_->SignalMultiple(1u); | |
568 | |
569 Request* req = CreateRequest("just.testing", 80); | |
570 EXPECT_EQ(ERR_IO_PENDING, req->Resolve()); | |
571 EXPECT_EQ(ERR_NAME_NOT_RESOLVED, req->WaitForResult()); | |
572 | |
573 EXPECT_EQ("just.testing", proc_->GetCaptureList()[0].hostname); | |
574 | |
575 // Also test that the error is not cached. | |
576 EXPECT_EQ(ERR_DNS_CACHE_MISS, req->ResolveFromCache()); | |
577 } | |
578 | |
579 TEST_F(HostResolverImplTest, AbortedAsynchronousLookup) { | |
580 Request* req0 = CreateRequest("just.testing", 80); | |
581 EXPECT_EQ(ERR_IO_PENDING, req0->Resolve()); | |
582 | |
583 EXPECT_TRUE(proc_->WaitFor(1u)); | |
584 | |
585 // Resolver is destroyed while job is running on WorkerPool. | |
586 resolver_.reset(); | |
587 | |
588 proc_->SignalAll(); | |
589 | |
590 // To ensure there was no spurious callback, complete with a new resolver. | |
591 CreateResolver(); | |
592 Request* req1 = CreateRequest("just.testing", 80); | |
593 EXPECT_EQ(ERR_IO_PENDING, req1->Resolve()); | |
594 | |
595 proc_->SignalMultiple(2u); | |
596 | |
597 EXPECT_EQ(OK, req1->WaitForResult()); | |
598 | |
599 // This request was canceled. | |
600 EXPECT_FALSE(req0->completed()); | |
601 } | |
602 | |
603 #if defined(THREAD_SANITIZER) | |
604 // Use of WorkerPool in HostResolverImpl causes a data race. crbug.com/334140 | |
605 #define MAYBE_NumericIPv4Address DISABLED_NumericIPv4Address | |
606 #else | |
607 #define MAYBE_NumericIPv4Address NumericIPv4Address | |
608 #endif | |
609 TEST_F(HostResolverImplTest, MAYBE_NumericIPv4Address) { | |
610 // Stevens says dotted quads with AI_UNSPEC resolve to a single sockaddr_in. | |
611 Request* req = CreateRequest("127.1.2.3", 5555); | |
612 EXPECT_EQ(OK, req->Resolve()); | |
613 | |
614 EXPECT_TRUE(req->HasOneAddress("127.1.2.3", 5555)); | |
615 } | |
616 | |
617 #if defined(THREAD_SANITIZER) | |
618 // Use of WorkerPool in HostResolverImpl causes a data race. crbug.com/334140 | |
619 #define MAYBE_NumericIPv6Address DISABLED_NumericIPv6Address | |
620 #else | |
621 #define MAYBE_NumericIPv6Address NumericIPv6Address | |
622 #endif | |
623 TEST_F(HostResolverImplTest, MAYBE_NumericIPv6Address) { | |
624 // Resolve a plain IPv6 address. Don't worry about [brackets], because | |
625 // the caller should have removed them. | |
626 Request* req = CreateRequest("2001:db8::1", 5555); | |
627 EXPECT_EQ(OK, req->Resolve()); | |
628 | |
629 EXPECT_TRUE(req->HasOneAddress("2001:db8::1", 5555)); | |
630 } | |
631 | |
632 #if defined(THREAD_SANITIZER) | |
633 // Use of WorkerPool in HostResolverImpl causes a data race. crbug.com/334140 | |
634 #define MAYBE_EmptyHost DISABLED_EmptyHost | |
635 #else | |
636 #define MAYBE_EmptyHost EmptyHost | |
637 #endif | |
638 TEST_F(HostResolverImplTest, MAYBE_EmptyHost) { | |
639 Request* req = CreateRequest(std::string(), 5555); | |
640 EXPECT_EQ(ERR_NAME_NOT_RESOLVED, req->Resolve()); | |
641 } | |
642 | |
643 #if defined(THREAD_SANITIZER) | |
644 // There's a data race in this test that may lead to use-after-free. | |
645 // If the test starts to crash without ThreadSanitizer it needs to be disabled | |
646 // globally. See http://crbug.com/268946 (stacks for this test in | |
647 // crbug.com/333567). | |
648 #define MAYBE_EmptyDotsHost DISABLED_EmptyDotsHost | |
649 #else | |
650 #define MAYBE_EmptyDotsHost EmptyDotsHost | |
651 #endif | |
652 TEST_F(HostResolverImplTest, MAYBE_EmptyDotsHost) { | |
653 for (int i = 0; i < 16; ++i) { | |
654 Request* req = CreateRequest(std::string(i, '.'), 5555); | |
655 EXPECT_EQ(ERR_NAME_NOT_RESOLVED, req->Resolve()); | |
656 } | |
657 } | |
658 | |
659 #if defined(THREAD_SANITIZER) | |
660 // There's a data race in this test that may lead to use-after-free. | |
661 // If the test starts to crash without ThreadSanitizer it needs to be disabled | |
662 // globally. See http://crbug.com/268946. | |
663 #define MAYBE_LongHost DISABLED_LongHost | |
664 #else | |
665 #define MAYBE_LongHost LongHost | |
666 #endif | |
667 TEST_F(HostResolverImplTest, MAYBE_LongHost) { | |
668 Request* req = CreateRequest(std::string(4097, 'a'), 5555); | |
669 EXPECT_EQ(ERR_NAME_NOT_RESOLVED, req->Resolve()); | |
670 } | |
671 | |
672 TEST_F(HostResolverImplTest, DeDupeRequests) { | |
673 // Start 5 requests, duplicating hosts "a" and "b". Since the resolver_proc is | |
674 // blocked, these should all pile up until we signal it. | |
675 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("a", 80)->Resolve()); | |
676 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("b", 80)->Resolve()); | |
677 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("b", 81)->Resolve()); | |
678 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("a", 82)->Resolve()); | |
679 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("b", 83)->Resolve()); | |
680 | |
681 proc_->SignalMultiple(2u); // One for "a", one for "b". | |
682 | |
683 for (size_t i = 0; i < requests_.size(); ++i) { | |
684 EXPECT_EQ(OK, requests_[i]->WaitForResult()) << i; | |
685 } | |
686 } | |
687 | |
688 TEST_F(HostResolverImplTest, CancelMultipleRequests) { | |
689 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("a", 80)->Resolve()); | |
690 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("b", 80)->Resolve()); | |
691 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("b", 81)->Resolve()); | |
692 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("a", 82)->Resolve()); | |
693 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("b", 83)->Resolve()); | |
694 | |
695 // Cancel everything except request for ("a", 82). | |
696 requests_[0]->Cancel(); | |
697 requests_[1]->Cancel(); | |
698 requests_[2]->Cancel(); | |
699 requests_[4]->Cancel(); | |
700 | |
701 proc_->SignalMultiple(2u); // One for "a", one for "b". | |
702 | |
703 EXPECT_EQ(OK, requests_[3]->WaitForResult()); | |
704 } | |
705 | |
706 TEST_F(HostResolverImplTest, CanceledRequestsReleaseJobSlots) { | |
707 // Fill up the dispatcher and queue. | |
708 for (unsigned i = 0; i < kMaxJobs + 1; ++i) { | |
709 std::string hostname = "a_"; | |
710 hostname[1] = 'a' + i; | |
711 EXPECT_EQ(ERR_IO_PENDING, CreateRequest(hostname, 80)->Resolve()); | |
712 EXPECT_EQ(ERR_IO_PENDING, CreateRequest(hostname, 81)->Resolve()); | |
713 } | |
714 | |
715 EXPECT_TRUE(proc_->WaitFor(kMaxJobs)); | |
716 | |
717 // Cancel all but last two. | |
718 for (unsigned i = 0; i < requests_.size() - 2; ++i) { | |
719 requests_[i]->Cancel(); | |
720 } | |
721 | |
722 EXPECT_TRUE(proc_->WaitFor(kMaxJobs + 1)); | |
723 | |
724 proc_->SignalAll(); | |
725 | |
726 size_t num_requests = requests_.size(); | |
727 EXPECT_EQ(OK, requests_[num_requests - 1]->WaitForResult()); | |
728 EXPECT_EQ(OK, requests_[num_requests - 2]->result()); | |
729 } | |
730 | |
731 TEST_F(HostResolverImplTest, CancelWithinCallback) { | |
732 struct MyHandler : public Handler { | |
733 void Handle(Request* req) override { | |
734 // Port 80 is the first request that the callback will be invoked for. | |
735 // While we are executing within that callback, cancel the other requests | |
736 // in the job and start another request. | |
737 if (req->index() == 0) { | |
738 // Once "a:80" completes, it will cancel "a:81" and "a:82". | |
739 requests()[1]->Cancel(); | |
740 requests()[2]->Cancel(); | |
741 } | |
742 } | |
743 }; | |
744 set_handler(new MyHandler()); | |
745 | |
746 for (size_t i = 0; i < 4; ++i) { | |
747 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("a", 80 + i)->Resolve()) << i; | |
748 } | |
749 | |
750 proc_->SignalMultiple(2u); // One for "a". One for "finalrequest". | |
751 | |
752 EXPECT_EQ(OK, requests_[0]->WaitForResult()); | |
753 | |
754 Request* final_request = CreateRequest("finalrequest", 70); | |
755 EXPECT_EQ(ERR_IO_PENDING, final_request->Resolve()); | |
756 EXPECT_EQ(OK, final_request->WaitForResult()); | |
757 EXPECT_TRUE(requests_[3]->completed()); | |
758 } | |
759 | |
760 TEST_F(HostResolverImplTest, DeleteWithinCallback) { | |
761 struct MyHandler : public Handler { | |
762 void Handle(Request* req) override { | |
763 EXPECT_EQ("a", req->info().hostname()); | |
764 EXPECT_EQ(80, req->info().port()); | |
765 | |
766 DeleteResolver(); | |
767 | |
768 // Quit after returning from OnCompleted (to give it a chance at | |
769 // incorrectly running the cancelled tasks). | |
770 base::MessageLoop::current()->PostTask(FROM_HERE, | |
771 base::MessageLoop::QuitClosure()); | |
772 } | |
773 }; | |
774 set_handler(new MyHandler()); | |
775 | |
776 for (size_t i = 0; i < 4; ++i) { | |
777 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("a", 80 + i)->Resolve()) << i; | |
778 } | |
779 | |
780 proc_->SignalMultiple(1u); // One for "a". | |
781 | |
782 // |MyHandler| will send quit message once all the requests have finished. | |
783 base::MessageLoop::current()->Run(); | |
784 } | |
785 | |
786 TEST_F(HostResolverImplTest, DeleteWithinAbortedCallback) { | |
787 struct MyHandler : public Handler { | |
788 void Handle(Request* req) override { | |
789 EXPECT_EQ("a", req->info().hostname()); | |
790 EXPECT_EQ(80, req->info().port()); | |
791 | |
792 DeleteResolver(); | |
793 | |
794 // Quit after returning from OnCompleted (to give it a chance at | |
795 // incorrectly running the cancelled tasks). | |
796 base::MessageLoop::current()->PostTask(FROM_HERE, | |
797 base::MessageLoop::QuitClosure()); | |
798 } | |
799 }; | |
800 set_handler(new MyHandler()); | |
801 | |
802 // This test assumes that the Jobs will be Aborted in order ["a", "b"] | |
803 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("a", 80)->Resolve()); | |
804 // HostResolverImpl will be deleted before later Requests can complete. | |
805 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("a", 81)->Resolve()); | |
806 // Job for 'b' will be aborted before it can complete. | |
807 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("b", 82)->Resolve()); | |
808 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("b", 83)->Resolve()); | |
809 | |
810 EXPECT_TRUE(proc_->WaitFor(1u)); | |
811 | |
812 // Triggering an IP address change. | |
813 NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests(); | |
814 | |
815 // |MyHandler| will send quit message once all the requests have finished. | |
816 base::MessageLoop::current()->Run(); | |
817 | |
818 EXPECT_EQ(ERR_NETWORK_CHANGED, requests_[0]->result()); | |
819 EXPECT_EQ(ERR_IO_PENDING, requests_[1]->result()); | |
820 EXPECT_EQ(ERR_IO_PENDING, requests_[2]->result()); | |
821 EXPECT_EQ(ERR_IO_PENDING, requests_[3]->result()); | |
822 // Clean up. | |
823 proc_->SignalMultiple(requests_.size()); | |
824 } | |
825 | |
826 TEST_F(HostResolverImplTest, StartWithinCallback) { | |
827 struct MyHandler : public Handler { | |
828 void Handle(Request* req) override { | |
829 if (req->index() == 0) { | |
830 // On completing the first request, start another request for "a". | |
831 // Since caching is disabled, this will result in another async request. | |
832 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("a", 70)->Resolve()); | |
833 } | |
834 } | |
835 }; | |
836 set_handler(new MyHandler()); | |
837 | |
838 // Turn off caching for this host resolver. | |
839 HostResolver::Options options = DefaultOptions(); | |
840 options.enable_caching = false; | |
841 resolver_.reset(new HostResolverImpl(options, NULL)); | |
842 resolver_->set_proc_params_for_test(DefaultParams(proc_.get())); | |
843 | |
844 for (size_t i = 0; i < 4; ++i) { | |
845 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("a", 80 + i)->Resolve()) << i; | |
846 } | |
847 | |
848 proc_->SignalMultiple(2u); // One for "a". One for the second "a". | |
849 | |
850 EXPECT_EQ(OK, requests_[0]->WaitForResult()); | |
851 ASSERT_EQ(5u, requests_.size()); | |
852 EXPECT_EQ(OK, requests_.back()->WaitForResult()); | |
853 | |
854 EXPECT_EQ(2u, proc_->GetCaptureList().size()); | |
855 } | |
856 | |
857 TEST_F(HostResolverImplTest, BypassCache) { | |
858 struct MyHandler : public Handler { | |
859 void Handle(Request* req) override { | |
860 if (req->index() == 0) { | |
861 // On completing the first request, start another request for "a". | |
862 // Since caching is enabled, this should complete synchronously. | |
863 std::string hostname = req->info().hostname(); | |
864 EXPECT_EQ(OK, CreateRequest(hostname, 70)->Resolve()); | |
865 EXPECT_EQ(OK, CreateRequest(hostname, 75)->ResolveFromCache()); | |
866 | |
867 // Ok good. Now make sure that if we ask to bypass the cache, it can no | |
868 // longer service the request synchronously. | |
869 HostResolver::RequestInfo info(HostPortPair(hostname, 71)); | |
870 info.set_allow_cached_response(false); | |
871 EXPECT_EQ(ERR_IO_PENDING, | |
872 CreateRequest(info, DEFAULT_PRIORITY)->Resolve()); | |
873 } else if (71 == req->info().port()) { | |
874 // Test is done. | |
875 base::MessageLoop::current()->Quit(); | |
876 } else { | |
877 FAIL() << "Unexpected request"; | |
878 } | |
879 } | |
880 }; | |
881 set_handler(new MyHandler()); | |
882 | |
883 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("a", 80)->Resolve()); | |
884 proc_->SignalMultiple(3u); // Only need two, but be generous. | |
885 | |
886 // |verifier| will send quit message once all the requests have finished. | |
887 base::MessageLoop::current()->Run(); | |
888 EXPECT_EQ(2u, proc_->GetCaptureList().size()); | |
889 } | |
890 | |
891 // Test that IP address changes flush the cache. | |
892 TEST_F(HostResolverImplTest, FlushCacheOnIPAddressChange) { | |
893 proc_->SignalMultiple(2u); // One before the flush, one after. | |
894 | |
895 Request* req = CreateRequest("host1", 70); | |
896 EXPECT_EQ(ERR_IO_PENDING, req->Resolve()); | |
897 EXPECT_EQ(OK, req->WaitForResult()); | |
898 | |
899 req = CreateRequest("host1", 75); | |
900 EXPECT_EQ(OK, req->Resolve()); // Should complete synchronously. | |
901 | |
902 // Flush cache by triggering an IP address change. | |
903 NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests(); | |
904 base::MessageLoop::current()->RunUntilIdle(); // Notification happens async. | |
905 | |
906 // Resolve "host1" again -- this time it won't be served from cache, so it | |
907 // will complete asynchronously. | |
908 req = CreateRequest("host1", 80); | |
909 EXPECT_EQ(ERR_IO_PENDING, req->Resolve()); | |
910 EXPECT_EQ(OK, req->WaitForResult()); | |
911 } | |
912 | |
913 // Test that IP address changes send ERR_NETWORK_CHANGED to pending requests. | |
914 TEST_F(HostResolverImplTest, AbortOnIPAddressChanged) { | |
915 Request* req = CreateRequest("host1", 70); | |
916 EXPECT_EQ(ERR_IO_PENDING, req->Resolve()); | |
917 | |
918 EXPECT_TRUE(proc_->WaitFor(1u)); | |
919 // Triggering an IP address change. | |
920 NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests(); | |
921 base::MessageLoop::current()->RunUntilIdle(); // Notification happens async. | |
922 proc_->SignalAll(); | |
923 | |
924 EXPECT_EQ(ERR_NETWORK_CHANGED, req->WaitForResult()); | |
925 EXPECT_EQ(0u, resolver_->GetHostCache()->size()); | |
926 } | |
927 | |
928 // Obey pool constraints after IP address has changed. | |
929 TEST_F(HostResolverImplTest, ObeyPoolConstraintsAfterIPAddressChange) { | |
930 // Runs at most one job at a time. | |
931 CreateSerialResolver(); | |
932 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("a")->Resolve()); | |
933 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("b")->Resolve()); | |
934 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("c")->Resolve()); | |
935 | |
936 EXPECT_TRUE(proc_->WaitFor(1u)); | |
937 // Triggering an IP address change. | |
938 NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests(); | |
939 base::MessageLoop::current()->RunUntilIdle(); // Notification happens async. | |
940 proc_->SignalMultiple(3u); // Let the false-start go so that we can catch it. | |
941 | |
942 EXPECT_EQ(ERR_NETWORK_CHANGED, requests_[0]->WaitForResult()); | |
943 | |
944 EXPECT_EQ(1u, num_running_dispatcher_jobs()); | |
945 | |
946 EXPECT_FALSE(requests_[1]->completed()); | |
947 EXPECT_FALSE(requests_[2]->completed()); | |
948 | |
949 EXPECT_EQ(OK, requests_[2]->WaitForResult()); | |
950 EXPECT_EQ(OK, requests_[1]->result()); | |
951 } | |
952 | |
953 // Tests that a new Request made from the callback of a previously aborted one | |
954 // will not be aborted. | |
955 TEST_F(HostResolverImplTest, AbortOnlyExistingRequestsOnIPAddressChange) { | |
956 struct MyHandler : public Handler { | |
957 void Handle(Request* req) override { | |
958 // Start new request for a different hostname to ensure that the order | |
959 // of jobs in HostResolverImpl is not stable. | |
960 std::string hostname; | |
961 if (req->index() == 0) | |
962 hostname = "zzz"; | |
963 else if (req->index() == 1) | |
964 hostname = "aaa"; | |
965 else if (req->index() == 2) | |
966 hostname = "eee"; | |
967 else | |
968 return; // A request started from within MyHandler. | |
969 EXPECT_EQ(ERR_IO_PENDING, CreateRequest(hostname)->Resolve()) << hostname; | |
970 } | |
971 }; | |
972 set_handler(new MyHandler()); | |
973 | |
974 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("bbb")->Resolve()); | |
975 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("eee")->Resolve()); | |
976 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("ccc")->Resolve()); | |
977 | |
978 // Wait until all are blocked; | |
979 EXPECT_TRUE(proc_->WaitFor(3u)); | |
980 // Trigger an IP address change. | |
981 NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests(); | |
982 // This should abort all running jobs. | |
983 base::MessageLoop::current()->RunUntilIdle(); | |
984 EXPECT_EQ(ERR_NETWORK_CHANGED, requests_[0]->result()); | |
985 EXPECT_EQ(ERR_NETWORK_CHANGED, requests_[1]->result()); | |
986 EXPECT_EQ(ERR_NETWORK_CHANGED, requests_[2]->result()); | |
987 ASSERT_EQ(6u, requests_.size()); | |
988 // Unblock all calls to proc. | |
989 proc_->SignalMultiple(requests_.size()); | |
990 // Run until the re-started requests finish. | |
991 EXPECT_EQ(OK, requests_[3]->WaitForResult()); | |
992 EXPECT_EQ(OK, requests_[4]->WaitForResult()); | |
993 EXPECT_EQ(OK, requests_[5]->WaitForResult()); | |
994 // Verify that results of aborted Jobs were not cached. | |
995 EXPECT_EQ(6u, proc_->GetCaptureList().size()); | |
996 EXPECT_EQ(3u, resolver_->GetHostCache()->size()); | |
997 } | |
998 | |
999 // Tests that when the maximum threads is set to 1, requests are dequeued | |
1000 // in order of priority. | |
1001 TEST_F(HostResolverImplTest, HigherPriorityRequestsStartedFirst) { | |
1002 CreateSerialResolver(); | |
1003 | |
1004 // Note that at this point the MockHostResolverProc is blocked, so any | |
1005 // requests we make will not complete. | |
1006 CreateRequest("req0", 80, LOW); | |
1007 CreateRequest("req1", 80, MEDIUM); | |
1008 CreateRequest("req2", 80, MEDIUM); | |
1009 CreateRequest("req3", 80, LOW); | |
1010 CreateRequest("req4", 80, HIGHEST); | |
1011 CreateRequest("req5", 80, LOW); | |
1012 CreateRequest("req6", 80, LOW); | |
1013 CreateRequest("req5", 80, HIGHEST); | |
1014 | |
1015 for (size_t i = 0; i < requests_.size(); ++i) { | |
1016 EXPECT_EQ(ERR_IO_PENDING, requests_[i]->Resolve()) << i; | |
1017 } | |
1018 | |
1019 // Unblock the resolver thread so the requests can run. | |
1020 proc_->SignalMultiple(requests_.size()); // More than needed. | |
1021 | |
1022 // Wait for all the requests to complete succesfully. | |
1023 for (size_t i = 0; i < requests_.size(); ++i) { | |
1024 EXPECT_EQ(OK, requests_[i]->WaitForResult()) << i; | |
1025 } | |
1026 | |
1027 // Since we have restricted to a single concurrent thread in the jobpool, | |
1028 // the requests should complete in order of priority (with the exception | |
1029 // of the first request, which gets started right away, since there is | |
1030 // nothing outstanding). | |
1031 MockHostResolverProc::CaptureList capture_list = proc_->GetCaptureList(); | |
1032 ASSERT_EQ(7u, capture_list.size()); | |
1033 | |
1034 EXPECT_EQ("req0", capture_list[0].hostname); | |
1035 EXPECT_EQ("req4", capture_list[1].hostname); | |
1036 EXPECT_EQ("req5", capture_list[2].hostname); | |
1037 EXPECT_EQ("req1", capture_list[3].hostname); | |
1038 EXPECT_EQ("req2", capture_list[4].hostname); | |
1039 EXPECT_EQ("req3", capture_list[5].hostname); | |
1040 EXPECT_EQ("req6", capture_list[6].hostname); | |
1041 } | |
1042 | |
1043 // Try cancelling a job which has not started yet. | |
1044 TEST_F(HostResolverImplTest, CancelPendingRequest) { | |
1045 CreateSerialResolver(); | |
1046 | |
1047 CreateRequest("req0", 80, LOWEST); | |
1048 CreateRequest("req1", 80, HIGHEST); // Will cancel. | |
1049 CreateRequest("req2", 80, MEDIUM); | |
1050 CreateRequest("req3", 80, LOW); | |
1051 CreateRequest("req4", 80, HIGHEST); // Will cancel. | |
1052 CreateRequest("req5", 80, LOWEST); // Will cancel. | |
1053 CreateRequest("req6", 80, MEDIUM); | |
1054 | |
1055 // Start all of the requests. | |
1056 for (size_t i = 0; i < requests_.size(); ++i) { | |
1057 EXPECT_EQ(ERR_IO_PENDING, requests_[i]->Resolve()) << i; | |
1058 } | |
1059 | |
1060 // Cancel some requests | |
1061 requests_[1]->Cancel(); | |
1062 requests_[4]->Cancel(); | |
1063 requests_[5]->Cancel(); | |
1064 | |
1065 // Unblock the resolver thread so the requests can run. | |
1066 proc_->SignalMultiple(requests_.size()); // More than needed. | |
1067 | |
1068 // Wait for all the requests to complete succesfully. | |
1069 for (size_t i = 0; i < requests_.size(); ++i) { | |
1070 if (!requests_[i]->pending()) | |
1071 continue; // Don't wait for the requests we cancelled. | |
1072 EXPECT_EQ(OK, requests_[i]->WaitForResult()) << i; | |
1073 } | |
1074 | |
1075 // Verify that they called out the the resolver proc (which runs on the | |
1076 // resolver thread) in the expected order. | |
1077 MockHostResolverProc::CaptureList capture_list = proc_->GetCaptureList(); | |
1078 ASSERT_EQ(4u, capture_list.size()); | |
1079 | |
1080 EXPECT_EQ("req0", capture_list[0].hostname); | |
1081 EXPECT_EQ("req2", capture_list[1].hostname); | |
1082 EXPECT_EQ("req6", capture_list[2].hostname); | |
1083 EXPECT_EQ("req3", capture_list[3].hostname); | |
1084 } | |
1085 | |
1086 // Test that when too many requests are enqueued, old ones start to be aborted. | |
1087 TEST_F(HostResolverImplTest, QueueOverflow) { | |
1088 CreateSerialResolver(); | |
1089 | |
1090 // Allow only 3 queued jobs. | |
1091 const size_t kMaxPendingJobs = 3u; | |
1092 resolver_->SetMaxQueuedJobs(kMaxPendingJobs); | |
1093 | |
1094 // Note that at this point the MockHostResolverProc is blocked, so any | |
1095 // requests we make will not complete. | |
1096 | |
1097 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("req0", 80, LOWEST)->Resolve()); | |
1098 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("req1", 80, HIGHEST)->Resolve()); | |
1099 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("req2", 80, MEDIUM)->Resolve()); | |
1100 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("req3", 80, MEDIUM)->Resolve()); | |
1101 | |
1102 // At this point, there are 3 enqueued jobs. | |
1103 // Insertion of subsequent requests will cause evictions | |
1104 // based on priority. | |
1105 | |
1106 EXPECT_EQ(ERR_HOST_RESOLVER_QUEUE_TOO_LARGE, | |
1107 CreateRequest("req4", 80, LOW)->Resolve()); // Evicts itself! | |
1108 | |
1109 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("req5", 80, MEDIUM)->Resolve()); | |
1110 EXPECT_EQ(ERR_HOST_RESOLVER_QUEUE_TOO_LARGE, requests_[2]->result()); | |
1111 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("req6", 80, HIGHEST)->Resolve()); | |
1112 EXPECT_EQ(ERR_HOST_RESOLVER_QUEUE_TOO_LARGE, requests_[3]->result()); | |
1113 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("req7", 80, MEDIUM)->Resolve()); | |
1114 EXPECT_EQ(ERR_HOST_RESOLVER_QUEUE_TOO_LARGE, requests_[5]->result()); | |
1115 | |
1116 // Unblock the resolver thread so the requests can run. | |
1117 proc_->SignalMultiple(4u); | |
1118 | |
1119 // The rest should succeed. | |
1120 EXPECT_EQ(OK, requests_[7]->WaitForResult()); | |
1121 EXPECT_EQ(OK, requests_[0]->result()); | |
1122 EXPECT_EQ(OK, requests_[1]->result()); | |
1123 EXPECT_EQ(OK, requests_[6]->result()); | |
1124 | |
1125 // Verify that they called out the the resolver proc (which runs on the | |
1126 // resolver thread) in the expected order. | |
1127 MockHostResolverProc::CaptureList capture_list = proc_->GetCaptureList(); | |
1128 ASSERT_EQ(4u, capture_list.size()); | |
1129 | |
1130 EXPECT_EQ("req0", capture_list[0].hostname); | |
1131 EXPECT_EQ("req1", capture_list[1].hostname); | |
1132 EXPECT_EQ("req6", capture_list[2].hostname); | |
1133 EXPECT_EQ("req7", capture_list[3].hostname); | |
1134 | |
1135 // Verify that the evicted (incomplete) requests were not cached. | |
1136 EXPECT_EQ(4u, resolver_->GetHostCache()->size()); | |
1137 | |
1138 for (size_t i = 0; i < requests_.size(); ++i) { | |
1139 EXPECT_TRUE(requests_[i]->completed()) << i; | |
1140 } | |
1141 } | |
1142 | |
1143 // Tests that after changing the default AddressFamily to IPV4, requests | |
1144 // with UNSPECIFIED address family map to IPV4. | |
1145 TEST_F(HostResolverImplTest, SetDefaultAddressFamily_IPv4) { | |
1146 CreateSerialResolver(); // To guarantee order of resolutions. | |
1147 | |
1148 proc_->AddRule("h1", ADDRESS_FAMILY_IPV4, "1.0.0.1"); | |
1149 proc_->AddRule("h1", ADDRESS_FAMILY_IPV6, "::2"); | |
1150 | |
1151 resolver_->SetDefaultAddressFamily(ADDRESS_FAMILY_IPV4); | |
1152 | |
1153 CreateRequest("h1", 80, MEDIUM, ADDRESS_FAMILY_UNSPECIFIED); | |
1154 CreateRequest("h1", 80, MEDIUM, ADDRESS_FAMILY_IPV4); | |
1155 CreateRequest("h1", 80, MEDIUM, ADDRESS_FAMILY_IPV6); | |
1156 | |
1157 // Start all of the requests. | |
1158 for (size_t i = 0; i < requests_.size(); ++i) { | |
1159 EXPECT_EQ(ERR_IO_PENDING, requests_[i]->Resolve()) << i; | |
1160 } | |
1161 | |
1162 proc_->SignalMultiple(requests_.size()); | |
1163 | |
1164 // Wait for all the requests to complete. | |
1165 for (size_t i = 0u; i < requests_.size(); ++i) { | |
1166 EXPECT_EQ(OK, requests_[i]->WaitForResult()) << i; | |
1167 } | |
1168 | |
1169 // Since the requests all had the same priority and we limited the thread | |
1170 // count to 1, they should have completed in the same order as they were | |
1171 // requested. Moreover, request0 and request1 will have been serviced by | |
1172 // the same job. | |
1173 | |
1174 MockHostResolverProc::CaptureList capture_list = proc_->GetCaptureList(); | |
1175 ASSERT_EQ(2u, capture_list.size()); | |
1176 | |
1177 EXPECT_EQ("h1", capture_list[0].hostname); | |
1178 EXPECT_EQ(ADDRESS_FAMILY_IPV4, capture_list[0].address_family); | |
1179 | |
1180 EXPECT_EQ("h1", capture_list[1].hostname); | |
1181 EXPECT_EQ(ADDRESS_FAMILY_IPV6, capture_list[1].address_family); | |
1182 | |
1183 // Now check that the correct resolved IP addresses were returned. | |
1184 EXPECT_TRUE(requests_[0]->HasOneAddress("1.0.0.1", 80)); | |
1185 EXPECT_TRUE(requests_[1]->HasOneAddress("1.0.0.1", 80)); | |
1186 EXPECT_TRUE(requests_[2]->HasOneAddress("::2", 80)); | |
1187 } | |
1188 | |
1189 // This is the exact same test as SetDefaultAddressFamily_IPv4, except the | |
1190 // default family is set to IPv6 and the family of requests is flipped where | |
1191 // specified. | |
1192 TEST_F(HostResolverImplTest, SetDefaultAddressFamily_IPv6) { | |
1193 CreateSerialResolver(); // To guarantee order of resolutions. | |
1194 | |
1195 // Don't use IPv6 replacements here since some systems don't support it. | |
1196 proc_->AddRule("h1", ADDRESS_FAMILY_IPV4, "1.0.0.1"); | |
1197 proc_->AddRule("h1", ADDRESS_FAMILY_IPV6, "::2"); | |
1198 | |
1199 resolver_->SetDefaultAddressFamily(ADDRESS_FAMILY_IPV6); | |
1200 | |
1201 CreateRequest("h1", 80, MEDIUM, ADDRESS_FAMILY_UNSPECIFIED); | |
1202 CreateRequest("h1", 80, MEDIUM, ADDRESS_FAMILY_IPV6); | |
1203 CreateRequest("h1", 80, MEDIUM, ADDRESS_FAMILY_IPV4); | |
1204 | |
1205 // Start all of the requests. | |
1206 for (size_t i = 0; i < requests_.size(); ++i) { | |
1207 EXPECT_EQ(ERR_IO_PENDING, requests_[i]->Resolve()) << i; | |
1208 } | |
1209 | |
1210 proc_->SignalMultiple(requests_.size()); | |
1211 | |
1212 // Wait for all the requests to complete. | |
1213 for (size_t i = 0u; i < requests_.size(); ++i) { | |
1214 EXPECT_EQ(OK, requests_[i]->WaitForResult()) << i; | |
1215 } | |
1216 | |
1217 // Since the requests all had the same priority and we limited the thread | |
1218 // count to 1, they should have completed in the same order as they were | |
1219 // requested. Moreover, request0 and request1 will have been serviced by | |
1220 // the same job. | |
1221 | |
1222 MockHostResolverProc::CaptureList capture_list = proc_->GetCaptureList(); | |
1223 ASSERT_EQ(2u, capture_list.size()); | |
1224 | |
1225 EXPECT_EQ("h1", capture_list[0].hostname); | |
1226 EXPECT_EQ(ADDRESS_FAMILY_IPV6, capture_list[0].address_family); | |
1227 | |
1228 EXPECT_EQ("h1", capture_list[1].hostname); | |
1229 EXPECT_EQ(ADDRESS_FAMILY_IPV4, capture_list[1].address_family); | |
1230 | |
1231 // Now check that the correct resolved IP addresses were returned. | |
1232 EXPECT_TRUE(requests_[0]->HasOneAddress("::2", 80)); | |
1233 EXPECT_TRUE(requests_[1]->HasOneAddress("::2", 80)); | |
1234 EXPECT_TRUE(requests_[2]->HasOneAddress("1.0.0.1", 80)); | |
1235 } | |
1236 | |
1237 // Make sure that the address family parameter is respected when raw IPs are | |
1238 // passed in. | |
1239 TEST_F(HostResolverImplTest, AddressFamilyWithRawIPs) { | |
1240 Request* request = | |
1241 CreateRequest("127.0.0.1", 80, MEDIUM, ADDRESS_FAMILY_IPV4); | |
1242 EXPECT_EQ(OK, request->Resolve()); | |
1243 EXPECT_TRUE(request->HasOneAddress("127.0.0.1", 80)); | |
1244 | |
1245 request = CreateRequest("127.0.0.1", 80, MEDIUM, ADDRESS_FAMILY_IPV6); | |
1246 EXPECT_EQ(ERR_NAME_NOT_RESOLVED, request->Resolve()); | |
1247 | |
1248 request = CreateRequest("127.0.0.1", 80, MEDIUM, ADDRESS_FAMILY_UNSPECIFIED); | |
1249 EXPECT_EQ(OK, request->Resolve()); | |
1250 EXPECT_TRUE(request->HasOneAddress("127.0.0.1", 80)); | |
1251 | |
1252 request = CreateRequest("::1", 80, MEDIUM, ADDRESS_FAMILY_IPV4); | |
1253 EXPECT_EQ(ERR_NAME_NOT_RESOLVED, request->Resolve()); | |
1254 | |
1255 request = CreateRequest("::1", 80, MEDIUM, ADDRESS_FAMILY_IPV6); | |
1256 EXPECT_EQ(OK, request->Resolve()); | |
1257 EXPECT_TRUE(request->HasOneAddress("::1", 80)); | |
1258 | |
1259 request = CreateRequest("::1", 80, MEDIUM, ADDRESS_FAMILY_UNSPECIFIED); | |
1260 EXPECT_EQ(OK, request->Resolve()); | |
1261 EXPECT_TRUE(request->HasOneAddress("::1", 80)); | |
1262 } | |
1263 | |
1264 TEST_F(HostResolverImplTest, ResolveFromCache) { | |
1265 proc_->AddRuleForAllFamilies("just.testing", "192.168.1.42"); | |
1266 proc_->SignalMultiple(1u); // Need only one. | |
1267 | |
1268 HostResolver::RequestInfo info(HostPortPair("just.testing", 80)); | |
1269 | |
1270 // First hit will miss the cache. | |
1271 EXPECT_EQ(ERR_DNS_CACHE_MISS, | |
1272 CreateRequest(info, DEFAULT_PRIORITY)->ResolveFromCache()); | |
1273 | |
1274 // This time, we fetch normally. | |
1275 EXPECT_EQ(ERR_IO_PENDING, CreateRequest(info, DEFAULT_PRIORITY)->Resolve()); | |
1276 EXPECT_EQ(OK, requests_[1]->WaitForResult()); | |
1277 | |
1278 // Now we should be able to fetch from the cache. | |
1279 EXPECT_EQ(OK, CreateRequest(info, DEFAULT_PRIORITY)->ResolveFromCache()); | |
1280 EXPECT_TRUE(requests_[2]->HasOneAddress("192.168.1.42", 80)); | |
1281 } | |
1282 | |
1283 // Test the retry attempts simulating host resolver proc that takes too long. | |
1284 TEST_F(HostResolverImplTest, MultipleAttempts) { | |
1285 // Total number of attempts would be 3 and we want the 3rd attempt to resolve | |
1286 // the host. First and second attempt will be forced to sleep until they get | |
1287 // word that a resolution has completed. The 3rd resolution attempt will try | |
1288 // to get done ASAP, and won't sleep.. | |
1289 int kAttemptNumberToResolve = 3; | |
1290 int kTotalAttempts = 3; | |
1291 | |
1292 scoped_refptr<LookupAttemptHostResolverProc> resolver_proc( | |
1293 new LookupAttemptHostResolverProc( | |
1294 NULL, kAttemptNumberToResolve, kTotalAttempts)); | |
1295 | |
1296 HostResolverImpl::ProcTaskParams params = DefaultParams(resolver_proc.get()); | |
1297 | |
1298 // Specify smaller interval for unresponsive_delay_ for HostResolverImpl so | |
1299 // that unit test runs faster. For example, this test finishes in 1.5 secs | |
1300 // (500ms * 3). | |
1301 params.unresponsive_delay = base::TimeDelta::FromMilliseconds(500); | |
1302 | |
1303 resolver_.reset(new HostResolverImpl(DefaultOptions(), NULL)); | |
1304 resolver_->set_proc_params_for_test(params); | |
1305 | |
1306 // Resolve "host1". | |
1307 HostResolver::RequestInfo info(HostPortPair("host1", 70)); | |
1308 Request* req = CreateRequest(info, DEFAULT_PRIORITY); | |
1309 EXPECT_EQ(ERR_IO_PENDING, req->Resolve()); | |
1310 | |
1311 // Resolve returns -4 to indicate that 3rd attempt has resolved the host. | |
1312 EXPECT_EQ(-4, req->WaitForResult()); | |
1313 | |
1314 resolver_proc->WaitForAllAttemptsToFinish( | |
1315 base::TimeDelta::FromMilliseconds(60000)); | |
1316 base::MessageLoop::current()->RunUntilIdle(); | |
1317 | |
1318 EXPECT_EQ(resolver_proc->total_attempts_resolved(), kTotalAttempts); | |
1319 EXPECT_EQ(resolver_proc->resolved_attempt_number(), kAttemptNumberToResolve); | |
1320 } | |
1321 | |
1322 DnsConfig CreateValidDnsConfig() { | |
1323 IPAddressNumber dns_ip; | |
1324 bool rv = ParseIPLiteralToNumber("192.168.1.0", &dns_ip); | |
1325 EXPECT_TRUE(rv); | |
1326 | |
1327 DnsConfig config; | |
1328 config.nameservers.push_back(IPEndPoint(dns_ip, dns_protocol::kDefaultPort)); | |
1329 EXPECT_TRUE(config.IsValid()); | |
1330 return config; | |
1331 } | |
1332 | |
1333 // Specialized fixture for tests of DnsTask. | |
1334 class HostResolverImplDnsTest : public HostResolverImplTest { | |
1335 public: | |
1336 HostResolverImplDnsTest() : dns_client_(NULL) {} | |
1337 | |
1338 protected: | |
1339 // testing::Test implementation: | |
1340 void SetUp() override { | |
1341 AddDnsRule("nx", dns_protocol::kTypeA, MockDnsClientRule::FAIL, false); | |
1342 AddDnsRule("nx", dns_protocol::kTypeAAAA, MockDnsClientRule::FAIL, false); | |
1343 AddDnsRule("ok", dns_protocol::kTypeA, MockDnsClientRule::OK, false); | |
1344 AddDnsRule("ok", dns_protocol::kTypeAAAA, MockDnsClientRule::OK, false); | |
1345 AddDnsRule("4ok", dns_protocol::kTypeA, MockDnsClientRule::OK, false); | |
1346 AddDnsRule("4ok", dns_protocol::kTypeAAAA, MockDnsClientRule::EMPTY, false); | |
1347 AddDnsRule("6ok", dns_protocol::kTypeA, MockDnsClientRule::EMPTY, false); | |
1348 AddDnsRule("6ok", dns_protocol::kTypeAAAA, MockDnsClientRule::OK, false); | |
1349 AddDnsRule("4nx", dns_protocol::kTypeA, MockDnsClientRule::OK, false); | |
1350 AddDnsRule("4nx", dns_protocol::kTypeAAAA, MockDnsClientRule::FAIL, false); | |
1351 AddDnsRule("empty", dns_protocol::kTypeA, MockDnsClientRule::EMPTY, false); | |
1352 AddDnsRule("empty", dns_protocol::kTypeAAAA, MockDnsClientRule::EMPTY, | |
1353 false); | |
1354 | |
1355 AddDnsRule("slow_nx", dns_protocol::kTypeA, MockDnsClientRule::FAIL, true); | |
1356 AddDnsRule("slow_nx", dns_protocol::kTypeAAAA, MockDnsClientRule::FAIL, | |
1357 true); | |
1358 | |
1359 AddDnsRule("4slow_ok", dns_protocol::kTypeA, MockDnsClientRule::OK, true); | |
1360 AddDnsRule("4slow_ok", dns_protocol::kTypeAAAA, MockDnsClientRule::OK, | |
1361 false); | |
1362 AddDnsRule("6slow_ok", dns_protocol::kTypeA, MockDnsClientRule::OK, false); | |
1363 AddDnsRule("6slow_ok", dns_protocol::kTypeAAAA, MockDnsClientRule::OK, | |
1364 true); | |
1365 AddDnsRule("4slow_4ok", dns_protocol::kTypeA, MockDnsClientRule::OK, true); | |
1366 AddDnsRule("4slow_4ok", dns_protocol::kTypeAAAA, MockDnsClientRule::EMPTY, | |
1367 false); | |
1368 AddDnsRule("4slow_4timeout", dns_protocol::kTypeA, | |
1369 MockDnsClientRule::TIMEOUT, true); | |
1370 AddDnsRule("4slow_4timeout", dns_protocol::kTypeAAAA, MockDnsClientRule::OK, | |
1371 false); | |
1372 AddDnsRule("4slow_6timeout", dns_protocol::kTypeA, | |
1373 MockDnsClientRule::OK, true); | |
1374 AddDnsRule("4slow_6timeout", dns_protocol::kTypeAAAA, | |
1375 MockDnsClientRule::TIMEOUT, false); | |
1376 CreateResolver(); | |
1377 } | |
1378 | |
1379 // HostResolverImplTest implementation: | |
1380 void CreateResolverWithLimitsAndParams( | |
1381 size_t max_concurrent_resolves, | |
1382 const HostResolverImpl::ProcTaskParams& params) override { | |
1383 HostResolverImpl::Options options = DefaultOptions(); | |
1384 options.max_concurrent_resolves = max_concurrent_resolves; | |
1385 resolver_.reset(new HostResolverImpl(options, NULL)); | |
1386 resolver_->set_proc_params_for_test(params); | |
1387 // Disable IPv6 support probing. | |
1388 resolver_->SetDefaultAddressFamily(ADDRESS_FAMILY_UNSPECIFIED); | |
1389 dns_client_ = new MockDnsClient(DnsConfig(), dns_rules_); | |
1390 resolver_->SetDnsClient(scoped_ptr<DnsClient>(dns_client_)); | |
1391 } | |
1392 | |
1393 // Adds a rule to |dns_rules_|. Must be followed by |CreateResolver| to apply. | |
1394 void AddDnsRule(const std::string& prefix, | |
1395 uint16 qtype, | |
1396 MockDnsClientRule::Result result, | |
1397 bool delay) { | |
1398 dns_rules_.push_back(MockDnsClientRule(prefix, qtype, result, delay)); | |
1399 } | |
1400 | |
1401 void ChangeDnsConfig(const DnsConfig& config) { | |
1402 NetworkChangeNotifier::SetDnsConfig(config); | |
1403 // Notification is delivered asynchronously. | |
1404 base::MessageLoop::current()->RunUntilIdle(); | |
1405 } | |
1406 | |
1407 MockDnsClientRuleList dns_rules_; | |
1408 // Owned by |resolver_|. | |
1409 MockDnsClient* dns_client_; | |
1410 }; | |
1411 | |
1412 // TODO(szym): Test AbortAllInProgressJobs due to DnsConfig change. | |
1413 | |
1414 // TODO(cbentzel): Test a mix of requests with different HostResolverFlags. | |
1415 | |
1416 // Test successful and fallback resolutions in HostResolverImpl::DnsTask. | |
1417 TEST_F(HostResolverImplDnsTest, DnsTask) { | |
1418 resolver_->SetDefaultAddressFamily(ADDRESS_FAMILY_IPV4); | |
1419 | |
1420 proc_->AddRuleForAllFamilies("nx_succeed", "192.168.1.102"); | |
1421 // All other hostnames will fail in proc_. | |
1422 | |
1423 // Initially there is no config, so client should not be invoked. | |
1424 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("ok_fail", 80)->Resolve()); | |
1425 proc_->SignalMultiple(requests_.size()); | |
1426 | |
1427 EXPECT_EQ(ERR_NAME_NOT_RESOLVED, requests_[0]->WaitForResult()); | |
1428 | |
1429 ChangeDnsConfig(CreateValidDnsConfig()); | |
1430 | |
1431 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("ok_fail", 80)->Resolve()); | |
1432 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("nx_fail", 80)->Resolve()); | |
1433 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("nx_succeed", 80)->Resolve()); | |
1434 | |
1435 proc_->SignalMultiple(requests_.size()); | |
1436 | |
1437 for (size_t i = 1; i < requests_.size(); ++i) | |
1438 EXPECT_NE(ERR_UNEXPECTED, requests_[i]->WaitForResult()) << i; | |
1439 | |
1440 EXPECT_EQ(OK, requests_[1]->result()); | |
1441 // Resolved by MockDnsClient. | |
1442 EXPECT_TRUE(requests_[1]->HasOneAddress("127.0.0.1", 80)); | |
1443 // Fallback to ProcTask. | |
1444 EXPECT_EQ(ERR_NAME_NOT_RESOLVED, requests_[2]->result()); | |
1445 EXPECT_EQ(OK, requests_[3]->result()); | |
1446 EXPECT_TRUE(requests_[3]->HasOneAddress("192.168.1.102", 80)); | |
1447 } | |
1448 | |
1449 // Test successful and failing resolutions in HostResolverImpl::DnsTask when | |
1450 // fallback to ProcTask is disabled. | |
1451 TEST_F(HostResolverImplDnsTest, NoFallbackToProcTask) { | |
1452 resolver_->SetDefaultAddressFamily(ADDRESS_FAMILY_IPV4); | |
1453 set_fallback_to_proctask(false); | |
1454 | |
1455 proc_->AddRuleForAllFamilies("nx_succeed", "192.168.1.102"); | |
1456 // All other hostnames will fail in proc_. | |
1457 | |
1458 // Set empty DnsConfig. | |
1459 ChangeDnsConfig(DnsConfig()); | |
1460 // Initially there is no config, so client should not be invoked. | |
1461 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("ok_fail", 80)->Resolve()); | |
1462 // There is no config, so fallback to ProcTask must work. | |
1463 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("nx_succeed", 80)->Resolve()); | |
1464 proc_->SignalMultiple(requests_.size()); | |
1465 | |
1466 EXPECT_EQ(ERR_NAME_NOT_RESOLVED, requests_[0]->WaitForResult()); | |
1467 EXPECT_EQ(OK, requests_[1]->WaitForResult()); | |
1468 EXPECT_TRUE(requests_[1]->HasOneAddress("192.168.1.102", 80)); | |
1469 | |
1470 ChangeDnsConfig(CreateValidDnsConfig()); | |
1471 | |
1472 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("ok_abort", 80)->Resolve()); | |
1473 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("nx_abort", 80)->Resolve()); | |
1474 | |
1475 // Simulate the case when the preference or policy has disabled the DNS client | |
1476 // causing AbortDnsTasks. | |
1477 resolver_->SetDnsClient( | |
1478 scoped_ptr<DnsClient>(new MockDnsClient(DnsConfig(), dns_rules_))); | |
1479 ChangeDnsConfig(CreateValidDnsConfig()); | |
1480 | |
1481 // First request is resolved by MockDnsClient, others should fail due to | |
1482 // disabled fallback to ProcTask. | |
1483 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("ok_fail", 80)->Resolve()); | |
1484 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("nx_fail", 80)->Resolve()); | |
1485 proc_->SignalMultiple(requests_.size()); | |
1486 | |
1487 // Aborted due to Network Change. | |
1488 EXPECT_EQ(ERR_NETWORK_CHANGED, requests_[2]->WaitForResult()); | |
1489 EXPECT_EQ(ERR_NETWORK_CHANGED, requests_[3]->WaitForResult()); | |
1490 // Resolved by MockDnsClient. | |
1491 EXPECT_EQ(OK, requests_[4]->WaitForResult()); | |
1492 EXPECT_TRUE(requests_[4]->HasOneAddress("127.0.0.1", 80)); | |
1493 // Fallback to ProcTask is disabled. | |
1494 EXPECT_EQ(ERR_NAME_NOT_RESOLVED, requests_[5]->WaitForResult()); | |
1495 } | |
1496 | |
1497 // Test behavior of OnDnsTaskFailure when Job is aborted. | |
1498 TEST_F(HostResolverImplDnsTest, OnDnsTaskFailureAbortedJob) { | |
1499 resolver_->SetDefaultAddressFamily(ADDRESS_FAMILY_IPV4); | |
1500 ChangeDnsConfig(CreateValidDnsConfig()); | |
1501 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("nx_abort", 80)->Resolve()); | |
1502 // Abort all jobs here. | |
1503 CreateResolver(); | |
1504 proc_->SignalMultiple(requests_.size()); | |
1505 // Run to completion. | |
1506 base::MessageLoop::current()->RunUntilIdle(); // Notification happens async. | |
1507 // It shouldn't crash during OnDnsTaskFailure callbacks. | |
1508 EXPECT_EQ(ERR_IO_PENDING, requests_[0]->result()); | |
1509 | |
1510 // Repeat test with Fallback to ProcTask disabled | |
1511 resolver_->SetDefaultAddressFamily(ADDRESS_FAMILY_IPV4); | |
1512 set_fallback_to_proctask(false); | |
1513 ChangeDnsConfig(CreateValidDnsConfig()); | |
1514 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("nx_abort", 80)->Resolve()); | |
1515 // Abort all jobs here. | |
1516 CreateResolver(); | |
1517 // Run to completion. | |
1518 base::MessageLoop::current()->RunUntilIdle(); // Notification happens async. | |
1519 // It shouldn't crash during OnDnsTaskFailure callbacks. | |
1520 EXPECT_EQ(ERR_IO_PENDING, requests_[1]->result()); | |
1521 } | |
1522 | |
1523 TEST_F(HostResolverImplDnsTest, DnsTaskUnspec) { | |
1524 ChangeDnsConfig(CreateValidDnsConfig()); | |
1525 | |
1526 proc_->AddRuleForAllFamilies("4nx", "192.168.1.101"); | |
1527 // All other hostnames will fail in proc_. | |
1528 | |
1529 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("ok", 80)->Resolve()); | |
1530 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("4ok", 80)->Resolve()); | |
1531 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("6ok", 80)->Resolve()); | |
1532 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("4nx", 80)->Resolve()); | |
1533 | |
1534 proc_->SignalMultiple(requests_.size()); | |
1535 | |
1536 for (size_t i = 0; i < requests_.size(); ++i) | |
1537 EXPECT_EQ(OK, requests_[i]->WaitForResult()) << i; | |
1538 | |
1539 EXPECT_EQ(2u, requests_[0]->NumberOfAddresses()); | |
1540 EXPECT_TRUE(requests_[0]->HasAddress("127.0.0.1", 80)); | |
1541 EXPECT_TRUE(requests_[0]->HasAddress("::1", 80)); | |
1542 EXPECT_EQ(1u, requests_[1]->NumberOfAddresses()); | |
1543 EXPECT_TRUE(requests_[1]->HasAddress("127.0.0.1", 80)); | |
1544 EXPECT_EQ(1u, requests_[2]->NumberOfAddresses()); | |
1545 EXPECT_TRUE(requests_[2]->HasAddress("::1", 80)); | |
1546 EXPECT_EQ(1u, requests_[3]->NumberOfAddresses()); | |
1547 EXPECT_TRUE(requests_[3]->HasAddress("192.168.1.101", 80)); | |
1548 } | |
1549 | |
1550 TEST_F(HostResolverImplDnsTest, ServeFromHosts) { | |
1551 // Initially, use empty HOSTS file. | |
1552 DnsConfig config = CreateValidDnsConfig(); | |
1553 ChangeDnsConfig(config); | |
1554 | |
1555 proc_->AddRuleForAllFamilies(std::string(), | |
1556 std::string()); // Default to failures. | |
1557 proc_->SignalMultiple(1u); // For the first request which misses. | |
1558 | |
1559 Request* req0 = CreateRequest("nx_ipv4", 80); | |
1560 EXPECT_EQ(ERR_IO_PENDING, req0->Resolve()); | |
1561 EXPECT_EQ(ERR_NAME_NOT_RESOLVED, req0->WaitForResult()); | |
1562 | |
1563 IPAddressNumber local_ipv4, local_ipv6; | |
1564 ASSERT_TRUE(ParseIPLiteralToNumber("127.0.0.1", &local_ipv4)); | |
1565 ASSERT_TRUE(ParseIPLiteralToNumber("::1", &local_ipv6)); | |
1566 | |
1567 DnsHosts hosts; | |
1568 hosts[DnsHostsKey("nx_ipv4", ADDRESS_FAMILY_IPV4)] = local_ipv4; | |
1569 hosts[DnsHostsKey("nx_ipv6", ADDRESS_FAMILY_IPV6)] = local_ipv6; | |
1570 hosts[DnsHostsKey("nx_both", ADDRESS_FAMILY_IPV4)] = local_ipv4; | |
1571 hosts[DnsHostsKey("nx_both", ADDRESS_FAMILY_IPV6)] = local_ipv6; | |
1572 | |
1573 // Update HOSTS file. | |
1574 config.hosts = hosts; | |
1575 ChangeDnsConfig(config); | |
1576 | |
1577 Request* req1 = CreateRequest("nx_ipv4", 80); | |
1578 EXPECT_EQ(OK, req1->Resolve()); | |
1579 EXPECT_TRUE(req1->HasOneAddress("127.0.0.1", 80)); | |
1580 | |
1581 Request* req2 = CreateRequest("nx_ipv6", 80); | |
1582 EXPECT_EQ(OK, req2->Resolve()); | |
1583 EXPECT_TRUE(req2->HasOneAddress("::1", 80)); | |
1584 | |
1585 Request* req3 = CreateRequest("nx_both", 80); | |
1586 EXPECT_EQ(OK, req3->Resolve()); | |
1587 EXPECT_TRUE(req3->HasAddress("127.0.0.1", 80) && | |
1588 req3->HasAddress("::1", 80)); | |
1589 | |
1590 // Requests with specified AddressFamily. | |
1591 Request* req4 = CreateRequest("nx_ipv4", 80, MEDIUM, ADDRESS_FAMILY_IPV4); | |
1592 EXPECT_EQ(OK, req4->Resolve()); | |
1593 EXPECT_TRUE(req4->HasOneAddress("127.0.0.1", 80)); | |
1594 | |
1595 Request* req5 = CreateRequest("nx_ipv6", 80, MEDIUM, ADDRESS_FAMILY_IPV6); | |
1596 EXPECT_EQ(OK, req5->Resolve()); | |
1597 EXPECT_TRUE(req5->HasOneAddress("::1", 80)); | |
1598 | |
1599 // Request with upper case. | |
1600 Request* req6 = CreateRequest("nx_IPV4", 80); | |
1601 EXPECT_EQ(OK, req6->Resolve()); | |
1602 EXPECT_TRUE(req6->HasOneAddress("127.0.0.1", 80)); | |
1603 } | |
1604 | |
1605 TEST_F(HostResolverImplDnsTest, BypassDnsTask) { | |
1606 ChangeDnsConfig(CreateValidDnsConfig()); | |
1607 | |
1608 proc_->AddRuleForAllFamilies(std::string(), | |
1609 std::string()); // Default to failures. | |
1610 | |
1611 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("ok.local", 80)->Resolve()); | |
1612 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("ok.local.", 80)->Resolve()); | |
1613 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("oklocal", 80)->Resolve()); | |
1614 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("oklocal.", 80)->Resolve()); | |
1615 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("ok", 80)->Resolve()); | |
1616 | |
1617 proc_->SignalMultiple(requests_.size()); | |
1618 | |
1619 for (size_t i = 0; i < 2; ++i) | |
1620 EXPECT_EQ(ERR_NAME_NOT_RESOLVED, requests_[i]->WaitForResult()) << i; | |
1621 | |
1622 for (size_t i = 2; i < requests_.size(); ++i) | |
1623 EXPECT_EQ(OK, requests_[i]->WaitForResult()) << i; | |
1624 } | |
1625 | |
1626 TEST_F(HostResolverImplDnsTest, SystemOnlyBypassesDnsTask) { | |
1627 ChangeDnsConfig(CreateValidDnsConfig()); | |
1628 | |
1629 proc_->AddRuleForAllFamilies(std::string(), std::string()); | |
1630 | |
1631 HostResolver::RequestInfo info_bypass(HostPortPair("ok", 80)); | |
1632 info_bypass.set_host_resolver_flags(HOST_RESOLVER_SYSTEM_ONLY); | |
1633 EXPECT_EQ(ERR_IO_PENDING, CreateRequest(info_bypass, MEDIUM)->Resolve()); | |
1634 | |
1635 HostResolver::RequestInfo info(HostPortPair("ok", 80)); | |
1636 EXPECT_EQ(ERR_IO_PENDING, CreateRequest(info, MEDIUM)->Resolve()); | |
1637 | |
1638 proc_->SignalMultiple(requests_.size()); | |
1639 | |
1640 EXPECT_EQ(ERR_NAME_NOT_RESOLVED, requests_[0]->WaitForResult()); | |
1641 EXPECT_EQ(OK, requests_[1]->WaitForResult()); | |
1642 } | |
1643 | |
1644 TEST_F(HostResolverImplDnsTest, DisableDnsClientOnPersistentFailure) { | |
1645 ChangeDnsConfig(CreateValidDnsConfig()); | |
1646 | |
1647 proc_->AddRuleForAllFamilies(std::string(), | |
1648 std::string()); // Default to failures. | |
1649 | |
1650 // Check that DnsTask works. | |
1651 Request* req = CreateRequest("ok_1", 80); | |
1652 EXPECT_EQ(ERR_IO_PENDING, req->Resolve()); | |
1653 EXPECT_EQ(OK, req->WaitForResult()); | |
1654 | |
1655 for (unsigned i = 0; i < maximum_dns_failures(); ++i) { | |
1656 // Use custom names to require separate Jobs. | |
1657 std::string hostname = base::StringPrintf("nx_%u", i); | |
1658 // Ensure fallback to ProcTask succeeds. | |
1659 proc_->AddRuleForAllFamilies(hostname, "192.168.1.101"); | |
1660 EXPECT_EQ(ERR_IO_PENDING, CreateRequest(hostname, 80)->Resolve()) << i; | |
1661 } | |
1662 | |
1663 proc_->SignalMultiple(requests_.size()); | |
1664 | |
1665 for (size_t i = 0; i < requests_.size(); ++i) | |
1666 EXPECT_EQ(OK, requests_[i]->WaitForResult()) << i; | |
1667 | |
1668 ASSERT_FALSE(proc_->HasBlockedRequests()); | |
1669 | |
1670 // DnsTask should be disabled by now. | |
1671 req = CreateRequest("ok_2", 80); | |
1672 EXPECT_EQ(ERR_IO_PENDING, req->Resolve()); | |
1673 proc_->SignalMultiple(1u); | |
1674 EXPECT_EQ(ERR_NAME_NOT_RESOLVED, req->WaitForResult()); | |
1675 | |
1676 // Check that it is re-enabled after DNS change. | |
1677 ChangeDnsConfig(CreateValidDnsConfig()); | |
1678 req = CreateRequest("ok_3", 80); | |
1679 EXPECT_EQ(ERR_IO_PENDING, req->Resolve()); | |
1680 EXPECT_EQ(OK, req->WaitForResult()); | |
1681 } | |
1682 | |
1683 TEST_F(HostResolverImplDnsTest, DontDisableDnsClientOnSporadicFailure) { | |
1684 ChangeDnsConfig(CreateValidDnsConfig()); | |
1685 | |
1686 // |proc_| defaults to successes. | |
1687 | |
1688 // 20 failures interleaved with 20 successes. | |
1689 for (unsigned i = 0; i < 40; ++i) { | |
1690 // Use custom names to require separate Jobs. | |
1691 std::string hostname = (i % 2) == 0 ? base::StringPrintf("nx_%u", i) | |
1692 : base::StringPrintf("ok_%u", i); | |
1693 EXPECT_EQ(ERR_IO_PENDING, CreateRequest(hostname, 80)->Resolve()) << i; | |
1694 } | |
1695 | |
1696 proc_->SignalMultiple(requests_.size()); | |
1697 | |
1698 for (size_t i = 0; i < requests_.size(); ++i) | |
1699 EXPECT_EQ(OK, requests_[i]->WaitForResult()) << i; | |
1700 | |
1701 // Make |proc_| default to failures. | |
1702 proc_->AddRuleForAllFamilies(std::string(), std::string()); | |
1703 | |
1704 // DnsTask should still be enabled. | |
1705 Request* req = CreateRequest("ok_last", 80); | |
1706 EXPECT_EQ(ERR_IO_PENDING, req->Resolve()); | |
1707 EXPECT_EQ(OK, req->WaitForResult()); | |
1708 } | |
1709 | |
1710 // Confirm that resolving "localhost" is unrestricted even if there are no | |
1711 // global IPv6 address. See SystemHostResolverCall for rationale. | |
1712 // Test both the DnsClient and system host resolver paths. | |
1713 TEST_F(HostResolverImplDnsTest, DualFamilyLocalhost) { | |
1714 // Use regular SystemHostResolverCall! | |
1715 scoped_refptr<HostResolverProc> proc(new SystemHostResolverProc()); | |
1716 resolver_.reset(new HostResolverImpl(DefaultOptions(), NULL)); | |
1717 resolver_->set_proc_params_for_test(DefaultParams(proc.get())); | |
1718 | |
1719 resolver_->SetDnsClient( | |
1720 scoped_ptr<DnsClient>(new MockDnsClient(DnsConfig(), dns_rules_))); | |
1721 resolver_->SetDefaultAddressFamily(ADDRESS_FAMILY_IPV4); | |
1722 | |
1723 // Get the expected output. | |
1724 AddressList addrlist; | |
1725 int rv = proc->Resolve("localhost", ADDRESS_FAMILY_UNSPECIFIED, 0, &addrlist, | |
1726 NULL); | |
1727 if (rv != OK) | |
1728 return; | |
1729 | |
1730 for (unsigned i = 0; i < addrlist.size(); ++i) | |
1731 LOG(WARNING) << addrlist[i].ToString(); | |
1732 | |
1733 bool saw_ipv4 = AddressListContains(addrlist, "127.0.0.1", 0); | |
1734 bool saw_ipv6 = AddressListContains(addrlist, "::1", 0); | |
1735 if (!saw_ipv4 && !saw_ipv6) | |
1736 return; | |
1737 | |
1738 HostResolver::RequestInfo info(HostPortPair("localhost", 80)); | |
1739 info.set_address_family(ADDRESS_FAMILY_UNSPECIFIED); | |
1740 info.set_host_resolver_flags(HOST_RESOLVER_DEFAULT_FAMILY_SET_DUE_TO_NO_IPV6); | |
1741 | |
1742 // Try without DnsClient. | |
1743 ChangeDnsConfig(DnsConfig()); | |
1744 Request* req = CreateRequest(info, DEFAULT_PRIORITY); | |
1745 // It is resolved via getaddrinfo, so expect asynchronous result. | |
1746 EXPECT_EQ(ERR_IO_PENDING, req->Resolve()); | |
1747 EXPECT_EQ(OK, req->WaitForResult()); | |
1748 | |
1749 EXPECT_EQ(saw_ipv4, req->HasAddress("127.0.0.1", 80)); | |
1750 EXPECT_EQ(saw_ipv6, req->HasAddress("::1", 80)); | |
1751 | |
1752 // Configure DnsClient with dual-host HOSTS file. | |
1753 DnsConfig config = CreateValidDnsConfig(); | |
1754 DnsHosts hosts; | |
1755 IPAddressNumber local_ipv4, local_ipv6; | |
1756 ASSERT_TRUE(ParseIPLiteralToNumber("127.0.0.1", &local_ipv4)); | |
1757 ASSERT_TRUE(ParseIPLiteralToNumber("::1", &local_ipv6)); | |
1758 if (saw_ipv4) | |
1759 hosts[DnsHostsKey("localhost", ADDRESS_FAMILY_IPV4)] = local_ipv4; | |
1760 if (saw_ipv6) | |
1761 hosts[DnsHostsKey("localhost", ADDRESS_FAMILY_IPV6)] = local_ipv6; | |
1762 config.hosts = hosts; | |
1763 | |
1764 ChangeDnsConfig(config); | |
1765 req = CreateRequest(info, DEFAULT_PRIORITY); | |
1766 // Expect synchronous resolution from DnsHosts. | |
1767 EXPECT_EQ(OK, req->Resolve()); | |
1768 | |
1769 EXPECT_EQ(saw_ipv4, req->HasAddress("127.0.0.1", 80)); | |
1770 EXPECT_EQ(saw_ipv6, req->HasAddress("::1", 80)); | |
1771 } | |
1772 | |
1773 // Cancel a request with a single DNS transaction active. | |
1774 TEST_F(HostResolverImplDnsTest, CancelWithOneTransactionActive) { | |
1775 resolver_->SetDefaultAddressFamily(ADDRESS_FAMILY_IPV4); | |
1776 ChangeDnsConfig(CreateValidDnsConfig()); | |
1777 | |
1778 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("ok", 80)->Resolve()); | |
1779 EXPECT_EQ(1u, num_running_dispatcher_jobs()); | |
1780 requests_[0]->Cancel(); | |
1781 | |
1782 // Dispatcher state checked in TearDown. | |
1783 } | |
1784 | |
1785 // Cancel a request with a single DNS transaction active and another pending. | |
1786 TEST_F(HostResolverImplDnsTest, CancelWithOneTransactionActiveOnePending) { | |
1787 CreateSerialResolver(); | |
1788 resolver_->SetDefaultAddressFamily(ADDRESS_FAMILY_UNSPECIFIED); | |
1789 ChangeDnsConfig(CreateValidDnsConfig()); | |
1790 | |
1791 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("ok", 80)->Resolve()); | |
1792 EXPECT_EQ(1u, num_running_dispatcher_jobs()); | |
1793 requests_[0]->Cancel(); | |
1794 | |
1795 // Dispatcher state checked in TearDown. | |
1796 } | |
1797 | |
1798 // Cancel a request with two DNS transactions active. | |
1799 TEST_F(HostResolverImplDnsTest, CancelWithTwoTransactionsActive) { | |
1800 resolver_->SetDefaultAddressFamily(ADDRESS_FAMILY_UNSPECIFIED); | |
1801 ChangeDnsConfig(CreateValidDnsConfig()); | |
1802 | |
1803 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("ok", 80)->Resolve()); | |
1804 EXPECT_EQ(2u, num_running_dispatcher_jobs()); | |
1805 requests_[0]->Cancel(); | |
1806 | |
1807 // Dispatcher state checked in TearDown. | |
1808 } | |
1809 | |
1810 // Delete a resolver with some active requests and some queued requests. | |
1811 TEST_F(HostResolverImplDnsTest, DeleteWithActiveTransactions) { | |
1812 // At most 10 Jobs active at once. | |
1813 CreateResolverWithLimitsAndParams(10u, DefaultParams(proc_.get())); | |
1814 | |
1815 resolver_->SetDefaultAddressFamily(ADDRESS_FAMILY_UNSPECIFIED); | |
1816 ChangeDnsConfig(CreateValidDnsConfig()); | |
1817 | |
1818 // First active job is an IPv4 request. | |
1819 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("ok", 80, MEDIUM, | |
1820 ADDRESS_FAMILY_IPV4)->Resolve()); | |
1821 | |
1822 // Add 10 more DNS lookups for different hostnames. First 4 should have two | |
1823 // active jobs, next one has a single active job, and one pending. Others | |
1824 // should all be queued. | |
1825 for (int i = 0; i < 10; ++i) { | |
1826 EXPECT_EQ(ERR_IO_PENDING, CreateRequest( | |
1827 base::StringPrintf("ok%i", i))->Resolve()); | |
1828 } | |
1829 EXPECT_EQ(10u, num_running_dispatcher_jobs()); | |
1830 | |
1831 resolver_.reset(); | |
1832 } | |
1833 | |
1834 // Cancel a request with only the IPv6 transaction active. | |
1835 TEST_F(HostResolverImplDnsTest, CancelWithIPv6TransactionActive) { | |
1836 resolver_->SetDefaultAddressFamily(ADDRESS_FAMILY_UNSPECIFIED); | |
1837 ChangeDnsConfig(CreateValidDnsConfig()); | |
1838 | |
1839 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("6slow_ok", 80)->Resolve()); | |
1840 EXPECT_EQ(2u, num_running_dispatcher_jobs()); | |
1841 | |
1842 // The IPv4 request should complete, the IPv6 request is still pending. | |
1843 base::RunLoop().RunUntilIdle(); | |
1844 EXPECT_EQ(1u, num_running_dispatcher_jobs()); | |
1845 requests_[0]->Cancel(); | |
1846 | |
1847 // Dispatcher state checked in TearDown. | |
1848 } | |
1849 | |
1850 // Cancel a request with only the IPv4 transaction pending. | |
1851 TEST_F(HostResolverImplDnsTest, CancelWithIPv4TransactionPending) { | |
1852 set_fallback_to_proctask(false); | |
1853 resolver_->SetDefaultAddressFamily(ADDRESS_FAMILY_UNSPECIFIED); | |
1854 ChangeDnsConfig(CreateValidDnsConfig()); | |
1855 | |
1856 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("4slow_ok", 80)->Resolve()); | |
1857 EXPECT_EQ(2u, num_running_dispatcher_jobs()); | |
1858 | |
1859 // The IPv6 request should complete, the IPv4 request is still pending. | |
1860 base::RunLoop().RunUntilIdle(); | |
1861 EXPECT_EQ(1u, num_running_dispatcher_jobs()); | |
1862 | |
1863 requests_[0]->Cancel(); | |
1864 } | |
1865 | |
1866 // Test cases where AAAA completes first. | |
1867 TEST_F(HostResolverImplDnsTest, AAAACompletesFirst) { | |
1868 set_fallback_to_proctask(false); | |
1869 resolver_->SetDefaultAddressFamily(ADDRESS_FAMILY_UNSPECIFIED); | |
1870 ChangeDnsConfig(CreateValidDnsConfig()); | |
1871 | |
1872 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("4slow_ok", 80)->Resolve()); | |
1873 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("4slow_4ok", 80)->Resolve()); | |
1874 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("4slow_4timeout", 80)->Resolve()); | |
1875 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("4slow_6timeout", 80)->Resolve()); | |
1876 | |
1877 base::RunLoop().RunUntilIdle(); | |
1878 EXPECT_FALSE(requests_[0]->completed()); | |
1879 EXPECT_FALSE(requests_[1]->completed()); | |
1880 EXPECT_FALSE(requests_[2]->completed()); | |
1881 // The IPv6 of the third request should have failed and resulted in cancelling | |
1882 // the IPv4 request. | |
1883 EXPECT_TRUE(requests_[3]->completed()); | |
1884 EXPECT_EQ(ERR_DNS_TIMED_OUT, requests_[3]->result()); | |
1885 EXPECT_EQ(3u, num_running_dispatcher_jobs()); | |
1886 | |
1887 dns_client_->CompleteDelayedTransactions(); | |
1888 EXPECT_TRUE(requests_[0]->completed()); | |
1889 EXPECT_EQ(OK, requests_[0]->result()); | |
1890 EXPECT_EQ(2u, requests_[0]->NumberOfAddresses()); | |
1891 EXPECT_TRUE(requests_[0]->HasAddress("127.0.0.1", 80)); | |
1892 EXPECT_TRUE(requests_[0]->HasAddress("::1", 80)); | |
1893 | |
1894 EXPECT_TRUE(requests_[1]->completed()); | |
1895 EXPECT_EQ(OK, requests_[1]->result()); | |
1896 EXPECT_EQ(1u, requests_[1]->NumberOfAddresses()); | |
1897 EXPECT_TRUE(requests_[1]->HasAddress("127.0.0.1", 80)); | |
1898 | |
1899 EXPECT_TRUE(requests_[2]->completed()); | |
1900 EXPECT_EQ(ERR_DNS_TIMED_OUT, requests_[2]->result()); | |
1901 } | |
1902 | |
1903 // Test the case where only a single transaction slot is available. | |
1904 TEST_F(HostResolverImplDnsTest, SerialResolver) { | |
1905 CreateSerialResolver(); | |
1906 set_fallback_to_proctask(false); | |
1907 resolver_->SetDefaultAddressFamily(ADDRESS_FAMILY_UNSPECIFIED); | |
1908 ChangeDnsConfig(CreateValidDnsConfig()); | |
1909 | |
1910 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("ok", 80)->Resolve()); | |
1911 EXPECT_EQ(1u, num_running_dispatcher_jobs()); | |
1912 | |
1913 base::RunLoop().RunUntilIdle(); | |
1914 EXPECT_TRUE(requests_[0]->completed()); | |
1915 EXPECT_EQ(OK, requests_[0]->result()); | |
1916 EXPECT_EQ(2u, requests_[0]->NumberOfAddresses()); | |
1917 EXPECT_TRUE(requests_[0]->HasAddress("127.0.0.1", 80)); | |
1918 EXPECT_TRUE(requests_[0]->HasAddress("::1", 80)); | |
1919 } | |
1920 | |
1921 // Test the case where the AAAA query is started when another transaction | |
1922 // completes. | |
1923 TEST_F(HostResolverImplDnsTest, AAAAStartsAfterOtherJobFinishes) { | |
1924 CreateResolverWithLimitsAndParams(2u, DefaultParams(proc_.get())); | |
1925 set_fallback_to_proctask(false); | |
1926 resolver_->SetDefaultAddressFamily(ADDRESS_FAMILY_UNSPECIFIED); | |
1927 ChangeDnsConfig(CreateValidDnsConfig()); | |
1928 | |
1929 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("ok", 80, MEDIUM, | |
1930 ADDRESS_FAMILY_IPV4)->Resolve()); | |
1931 EXPECT_EQ(ERR_IO_PENDING, | |
1932 CreateRequest("4slow_ok", 80, MEDIUM)->Resolve()); | |
1933 // An IPv4 request should have been started pending for each job. | |
1934 EXPECT_EQ(2u, num_running_dispatcher_jobs()); | |
1935 | |
1936 // Request 0's IPv4 request should complete, starting Request 1's IPv6 | |
1937 // request, which should also complete. | |
1938 base::RunLoop().RunUntilIdle(); | |
1939 EXPECT_EQ(1u, num_running_dispatcher_jobs()); | |
1940 EXPECT_TRUE(requests_[0]->completed()); | |
1941 EXPECT_FALSE(requests_[1]->completed()); | |
1942 | |
1943 dns_client_->CompleteDelayedTransactions(); | |
1944 EXPECT_TRUE(requests_[1]->completed()); | |
1945 EXPECT_EQ(OK, requests_[1]->result()); | |
1946 EXPECT_EQ(2u, requests_[1]->NumberOfAddresses()); | |
1947 EXPECT_TRUE(requests_[1]->HasAddress("127.0.0.1", 80)); | |
1948 EXPECT_TRUE(requests_[1]->HasAddress("::1", 80)); | |
1949 } | |
1950 | |
1951 // Tests the case that a Job with a single transaction receives an empty address | |
1952 // list, triggering fallback to ProcTask. | |
1953 TEST_F(HostResolverImplDnsTest, IPv4EmptyFallback) { | |
1954 ChangeDnsConfig(CreateValidDnsConfig()); | |
1955 proc_->AddRuleForAllFamilies("empty_fallback", "192.168.0.1"); | |
1956 proc_->SignalMultiple(1u); | |
1957 EXPECT_EQ(ERR_IO_PENDING, | |
1958 CreateRequest("empty_fallback", 80, MEDIUM, | |
1959 ADDRESS_FAMILY_IPV4)->Resolve()); | |
1960 EXPECT_EQ(OK, requests_[0]->WaitForResult()); | |
1961 EXPECT_TRUE(requests_[0]->HasOneAddress("192.168.0.1", 80)); | |
1962 } | |
1963 | |
1964 // Tests the case that a Job with two transactions receives two empty address | |
1965 // lists, triggering fallback to ProcTask. | |
1966 TEST_F(HostResolverImplDnsTest, UnspecEmptyFallback) { | |
1967 ChangeDnsConfig(CreateValidDnsConfig()); | |
1968 proc_->AddRuleForAllFamilies("empty_fallback", "192.168.0.1"); | |
1969 proc_->SignalMultiple(1u); | |
1970 EXPECT_EQ(ERR_IO_PENDING, | |
1971 CreateRequest("empty_fallback", 80, MEDIUM, | |
1972 ADDRESS_FAMILY_UNSPECIFIED)->Resolve()); | |
1973 EXPECT_EQ(OK, requests_[0]->WaitForResult()); | |
1974 EXPECT_TRUE(requests_[0]->HasOneAddress("192.168.0.1", 80)); | |
1975 } | |
1976 | |
1977 // Tests getting a new invalid DnsConfig while there are active DnsTasks. | |
1978 TEST_F(HostResolverImplDnsTest, InvalidDnsConfigWithPendingRequests) { | |
1979 // At most 3 jobs active at once. This number is important, since we want to | |
1980 // make sure that aborting the first HostResolverImpl::Job does not trigger | |
1981 // another DnsTransaction on the second Job when it releases its second | |
1982 // prioritized dispatcher slot. | |
1983 CreateResolverWithLimitsAndParams(3u, DefaultParams(proc_.get())); | |
1984 | |
1985 resolver_->SetDefaultAddressFamily(ADDRESS_FAMILY_UNSPECIFIED); | |
1986 ChangeDnsConfig(CreateValidDnsConfig()); | |
1987 | |
1988 proc_->AddRuleForAllFamilies("slow_nx1", "192.168.0.1"); | |
1989 proc_->AddRuleForAllFamilies("slow_nx2", "192.168.0.2"); | |
1990 proc_->AddRuleForAllFamilies("ok", "192.168.0.3"); | |
1991 | |
1992 // First active job gets two slots. | |
1993 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("slow_nx1")->Resolve()); | |
1994 // Next job gets one slot, and waits on another. | |
1995 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("slow_nx2")->Resolve()); | |
1996 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("ok")->Resolve()); | |
1997 | |
1998 EXPECT_EQ(3u, num_running_dispatcher_jobs()); | |
1999 | |
2000 // Clear DNS config. Two in-progress jobs should be aborted, and the next one | |
2001 // should use a ProcTask. | |
2002 ChangeDnsConfig(DnsConfig()); | |
2003 EXPECT_EQ(ERR_NETWORK_CHANGED, requests_[0]->WaitForResult()); | |
2004 EXPECT_EQ(ERR_NETWORK_CHANGED, requests_[1]->WaitForResult()); | |
2005 | |
2006 // Finish up the third job. Should bypass the DnsClient, and get its results | |
2007 // from MockHostResolverProc. | |
2008 EXPECT_FALSE(requests_[2]->completed()); | |
2009 proc_->SignalMultiple(1u); | |
2010 EXPECT_EQ(OK, requests_[2]->WaitForResult()); | |
2011 EXPECT_TRUE(requests_[2]->HasOneAddress("192.168.0.3", 80)); | |
2012 } | |
2013 | |
2014 // Tests the case that DnsClient is automatically disabled due to failures | |
2015 // while there are active DnsTasks. | |
2016 TEST_F(HostResolverImplDnsTest, | |
2017 AutomaticallyDisableDnsClientWithPendingRequests) { | |
2018 // Trying different limits is important for this test: Different limits | |
2019 // result in different behavior when aborting in-progress DnsTasks. Having | |
2020 // a DnsTask that has one job active and one in the queue when another job | |
2021 // occupying two slots has its DnsTask aborted is the case most likely to run | |
2022 // into problems. | |
2023 for (size_t limit = 1u; limit < 6u; ++limit) { | |
2024 CreateResolverWithLimitsAndParams(limit, DefaultParams(proc_.get())); | |
2025 | |
2026 resolver_->SetDefaultAddressFamily(ADDRESS_FAMILY_UNSPECIFIED); | |
2027 ChangeDnsConfig(CreateValidDnsConfig()); | |
2028 | |
2029 // Queue up enough failures to disable DnsTasks. These will all fall back | |
2030 // to ProcTasks, and succeed there. | |
2031 for (unsigned i = 0u; i < maximum_dns_failures(); ++i) { | |
2032 std::string host = base::StringPrintf("nx%u", i); | |
2033 proc_->AddRuleForAllFamilies(host, "192.168.0.1"); | |
2034 EXPECT_EQ(ERR_IO_PENDING, CreateRequest(host)->Resolve()); | |
2035 } | |
2036 | |
2037 // These requests should all bypass DnsTasks, due to the above failures, | |
2038 // so should end up using ProcTasks. | |
2039 proc_->AddRuleForAllFamilies("slow_ok1", "192.168.0.2"); | |
2040 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("slow_ok1")->Resolve()); | |
2041 proc_->AddRuleForAllFamilies("slow_ok2", "192.168.0.3"); | |
2042 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("slow_ok2")->Resolve()); | |
2043 proc_->AddRuleForAllFamilies("slow_ok3", "192.168.0.4"); | |
2044 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("slow_ok3")->Resolve()); | |
2045 proc_->SignalMultiple(maximum_dns_failures() + 3); | |
2046 | |
2047 for (size_t i = 0u; i < maximum_dns_failures(); ++i) { | |
2048 EXPECT_EQ(OK, requests_[i]->WaitForResult()); | |
2049 EXPECT_TRUE(requests_[i]->HasOneAddress("192.168.0.1", 80)); | |
2050 } | |
2051 | |
2052 EXPECT_EQ(OK, requests_[maximum_dns_failures()]->WaitForResult()); | |
2053 EXPECT_TRUE(requests_[maximum_dns_failures()]->HasOneAddress( | |
2054 "192.168.0.2", 80)); | |
2055 EXPECT_EQ(OK, requests_[maximum_dns_failures() + 1]->WaitForResult()); | |
2056 EXPECT_TRUE(requests_[maximum_dns_failures() + 1]->HasOneAddress( | |
2057 "192.168.0.3", 80)); | |
2058 EXPECT_EQ(OK, requests_[maximum_dns_failures() + 2]->WaitForResult()); | |
2059 EXPECT_TRUE(requests_[maximum_dns_failures() + 2]->HasOneAddress( | |
2060 "192.168.0.4", 80)); | |
2061 requests_.clear(); | |
2062 } | |
2063 } | |
2064 | |
2065 // Tests a call to SetDnsClient while there are active DnsTasks. | |
2066 TEST_F(HostResolverImplDnsTest, ManuallyDisableDnsClientWithPendingRequests) { | |
2067 // At most 3 jobs active at once. This number is important, since we want to | |
2068 // make sure that aborting the first HostResolverImpl::Job does not trigger | |
2069 // another DnsTransaction on the second Job when it releases its second | |
2070 // prioritized dispatcher slot. | |
2071 CreateResolverWithLimitsAndParams(3u, DefaultParams(proc_.get())); | |
2072 | |
2073 resolver_->SetDefaultAddressFamily(ADDRESS_FAMILY_UNSPECIFIED); | |
2074 ChangeDnsConfig(CreateValidDnsConfig()); | |
2075 | |
2076 proc_->AddRuleForAllFamilies("slow_ok1", "192.168.0.1"); | |
2077 proc_->AddRuleForAllFamilies("slow_ok2", "192.168.0.2"); | |
2078 proc_->AddRuleForAllFamilies("ok", "192.168.0.3"); | |
2079 | |
2080 // First active job gets two slots. | |
2081 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("slow_ok1")->Resolve()); | |
2082 // Next job gets one slot, and waits on another. | |
2083 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("slow_ok2")->Resolve()); | |
2084 // Next one is queued. | |
2085 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("ok")->Resolve()); | |
2086 | |
2087 EXPECT_EQ(3u, num_running_dispatcher_jobs()); | |
2088 | |
2089 // Clear DnsClient. The two in-progress jobs should fall back to a ProcTask, | |
2090 // and the next one should be started with a ProcTask. | |
2091 resolver_->SetDnsClient(scoped_ptr<DnsClient>()); | |
2092 | |
2093 // All three in-progress requests should now be running a ProcTask. | |
2094 EXPECT_EQ(3u, num_running_dispatcher_jobs()); | |
2095 proc_->SignalMultiple(3u); | |
2096 | |
2097 EXPECT_EQ(OK, requests_[0]->WaitForResult()); | |
2098 EXPECT_TRUE(requests_[0]->HasOneAddress("192.168.0.1", 80)); | |
2099 EXPECT_EQ(OK, requests_[1]->WaitForResult()); | |
2100 EXPECT_TRUE(requests_[1]->HasOneAddress("192.168.0.2", 80)); | |
2101 EXPECT_EQ(OK, requests_[2]->WaitForResult()); | |
2102 EXPECT_TRUE(requests_[2]->HasOneAddress("192.168.0.3", 80)); | |
2103 } | |
2104 | |
2105 } // namespace net | |
OLD | NEW |