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

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

Issue 19498003: [net/dns] Perform A/AAAA queries for AF_UNSPEC resolutions in parallel. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: sync Created 7 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« net/dns/host_resolver_impl.cc ('K') | « net/dns/host_resolver_impl.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "net/dns/host_resolver_impl.h" 5 #include "net/dns/host_resolver_impl.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <string> 8 #include <string>
9 9
10 #include "base/bind.h" 10 #include "base/bind.h"
11 #include "base/bind_helpers.h" 11 #include "base/bind_helpers.h"
12 #include "base/memory/ref_counted.h" 12 #include "base/memory/ref_counted.h"
13 #include "base/memory/scoped_vector.h" 13 #include "base/memory/scoped_vector.h"
14 #include "base/message_loop/message_loop.h" 14 #include "base/message_loop/message_loop.h"
15 #include "base/run_loop.h"
15 #include "base/strings/string_util.h" 16 #include "base/strings/string_util.h"
16 #include "base/strings/stringprintf.h" 17 #include "base/strings/stringprintf.h"
17 #include "base/synchronization/condition_variable.h" 18 #include "base/synchronization/condition_variable.h"
18 #include "base/synchronization/lock.h" 19 #include "base/synchronization/lock.h"
19 #include "base/test/test_timeouts.h" 20 #include "base/test/test_timeouts.h"
20 #include "base/time/time.h" 21 #include "base/time/time.h"
21 #include "net/base/address_list.h" 22 #include "net/base/address_list.h"
22 #include "net/base/net_errors.h" 23 #include "net/base/net_errors.h"
23 #include "net/base/net_util.h" 24 #include "net/base/net_util.h"
24 #include "net/dns/dns_client.h" 25 #include "net/dns/dns_client.h"
(...skipping 388 matching lines...) Expand 10 before | Expand all | Expand 10 after
413 }; 414 };
414 415
415 } // namespace 416 } // namespace
416 417
417 class HostResolverImplTest : public testing::Test { 418 class HostResolverImplTest : public testing::Test {
418 public: 419 public:
419 static const int kDefaultPort = 80; 420 static const int kDefaultPort = 80;
420 421
421 HostResolverImplTest() : proc_(new MockHostResolverProc()) {} 422 HostResolverImplTest() : proc_(new MockHostResolverProc()) {}
422 423
424 void CreateResolver() {
425 CreateResolverWithLimitsAndParams(DefaultLimits(),
426 DefaultParams(proc_.get()));
427 }
428
429 // This HostResolverImpl will only allow 1 outstanding resolve at a time and
430 // perform no retries.
431 void CreateSerialResolver() {
432 HostResolverImpl::ProcTaskParams params = DefaultParams(proc_.get());
433 params.max_retry_attempts = 0u;
434 PrioritizedDispatcher::Limits limits(NUM_PRIORITIES, 1);
435 CreateResolverWithLimitsAndParams(limits, params);
436 }
437
423 protected: 438 protected:
424 // A Request::Handler which is a proxy to the HostResolverImplTest fixture. 439 // A Request::Handler which is a proxy to the HostResolverImplTest fixture.
425 struct Handler : public Request::Handler { 440 struct Handler : public Request::Handler {
426 virtual ~Handler() {} 441 virtual ~Handler() {}
427 442
428 // Proxy functions so that classes derived from Handler can access them. 443 // Proxy functions so that classes derived from Handler can access them.
429 Request* CreateRequest(const HostResolver::RequestInfo& info, 444 Request* CreateRequest(const HostResolver::RequestInfo& info,
430 RequestPriority priority) { 445 RequestPriority priority) {
431 return test->CreateRequest(info, priority); 446 return test->CreateRequest(info, priority);
432 } 447 }
433 Request* CreateRequest(const std::string& hostname, int port) { 448 Request* CreateRequest(const std::string& hostname, int port) {
434 return test->CreateRequest(hostname, port); 449 return test->CreateRequest(hostname, port);
435 } 450 }
436 Request* CreateRequest(const std::string& hostname) { 451 Request* CreateRequest(const std::string& hostname) {
437 return test->CreateRequest(hostname); 452 return test->CreateRequest(hostname);
438 } 453 }
439 ScopedVector<Request>& requests() { return test->requests_; } 454 ScopedVector<Request>& requests() { return test->requests_; }
440 455
441 void DeleteResolver() { test->resolver_.reset(); } 456 void DeleteResolver() { test->resolver_.reset(); }
442 457
443 HostResolverImplTest* test; 458 HostResolverImplTest* test;
444 }; 459 };
445 460
446 void CreateResolver() { 461 // testing::Test implementation:
447 resolver_.reset(new HostResolverImpl(HostCache::CreateDefaultCache(), 462 virtual void SetUp() OVERRIDE {
448 DefaultLimits(), 463 CreateResolver();
449 DefaultParams(proc_.get()),
450 NULL));
451 } 464 }
452 465
453 // This HostResolverImpl will only allow 1 outstanding resolve at a time and 466 virtual void TearDown() OVERRIDE {
454 // perform no retries. 467 if (resolver_.get())
455 void CreateSerialResolver() { 468 EXPECT_EQ(0u, resolver_->num_running_dispatcher_jobs_for_tests());
456 HostResolverImpl::ProcTaskParams params = DefaultParams(proc_.get()); 469 EXPECT_FALSE(proc_->HasBlockedRequests());
457 params.max_retry_attempts = 0u; 470 }
458 PrioritizedDispatcher::Limits limits(NUM_PRIORITIES, 1); 471
459 resolver_.reset(new HostResolverImpl( 472 virtual void CreateResolverWithLimitsAndParams(
460 HostCache::CreateDefaultCache(), 473 const PrioritizedDispatcher::Limits& limits,
461 limits, 474 const HostResolverImpl::ProcTaskParams& params) {
462 params, 475 resolver_.reset(new HostResolverImpl(HostCache::CreateDefaultCache(),
463 NULL)); 476 limits, params, NULL));
464 } 477 }
465 478
466 // The Request will not be made until a call to |Resolve()|, and the Job will 479 // The Request will not be made until a call to |Resolve()|, and the Job will
467 // not start until released by |proc_->SignalXXX|. 480 // not start until released by |proc_->SignalXXX|.
468 Request* CreateRequest(const HostResolver::RequestInfo& info, 481 Request* CreateRequest(const HostResolver::RequestInfo& info,
469 RequestPriority priority) { 482 RequestPriority priority) {
470 Request* req = new Request( 483 Request* req = new Request(
471 info, priority, requests_.size(), resolver_.get(), handler_.get()); 484 info, priority, requests_.size(), resolver_.get(), handler_.get());
472 requests_.push_back(req); 485 requests_.push_back(req);
473 return req; 486 return req;
(...skipping 15 matching lines...) Expand all
489 } 502 }
490 503
491 Request* CreateRequest(const std::string& hostname, int port) { 504 Request* CreateRequest(const std::string& hostname, int port) {
492 return CreateRequest(hostname, port, MEDIUM); 505 return CreateRequest(hostname, port, MEDIUM);
493 } 506 }
494 507
495 Request* CreateRequest(const std::string& hostname) { 508 Request* CreateRequest(const std::string& hostname) {
496 return CreateRequest(hostname, kDefaultPort); 509 return CreateRequest(hostname, kDefaultPort);
497 } 510 }
498 511
499 virtual void SetUp() OVERRIDE {
500 CreateResolver();
501 }
502
503 virtual void TearDown() OVERRIDE {
504 if (resolver_.get())
505 EXPECT_EQ(0u, resolver_->num_running_jobs_for_tests());
506 EXPECT_FALSE(proc_->HasBlockedRequests());
507 }
508
509 void set_handler(Handler* handler) { 512 void set_handler(Handler* handler) {
510 handler_.reset(handler); 513 handler_.reset(handler);
511 handler_->test = this; 514 handler_->test = this;
512 } 515 }
513 516
514 // Friendship is not inherited, so use proxies to access those. 517 // Friendship is not inherited, so use proxies to access those.
515 size_t num_running_jobs() const { 518 size_t num_running_dispatcher_jobs() const {
516 DCHECK(resolver_.get()); 519 DCHECK(resolver_.get());
517 return resolver_->num_running_jobs_for_tests(); 520 return resolver_->num_running_dispatcher_jobs_for_tests();
518 } 521 }
519 522
520 void set_fallback_to_proctask(bool fallback_to_proctask) { 523 void set_fallback_to_proctask(bool fallback_to_proctask) {
521 DCHECK(resolver_.get()); 524 DCHECK(resolver_.get());
522 resolver_->fallback_to_proctask_ = fallback_to_proctask; 525 resolver_->fallback_to_proctask_ = fallback_to_proctask;
523 } 526 }
524 527
525 scoped_refptr<MockHostResolverProc> proc_; 528 scoped_refptr<MockHostResolverProc> proc_;
526 scoped_ptr<HostResolverImpl> resolver_; 529 scoped_ptr<HostResolverImpl> resolver_;
527 ScopedVector<Request> requests_; 530 ScopedVector<Request> requests_;
(...skipping 363 matching lines...) Expand 10 before | Expand all | Expand 10 after
891 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("c")->Resolve()); 894 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("c")->Resolve());
892 895
893 EXPECT_TRUE(proc_->WaitFor(1u)); 896 EXPECT_TRUE(proc_->WaitFor(1u));
894 // Triggering an IP address change. 897 // Triggering an IP address change.
895 NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests(); 898 NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests();
896 base::MessageLoop::current()->RunUntilIdle(); // Notification happens async. 899 base::MessageLoop::current()->RunUntilIdle(); // Notification happens async.
897 proc_->SignalMultiple(3u); // Let the false-start go so that we can catch it. 900 proc_->SignalMultiple(3u); // Let the false-start go so that we can catch it.
898 901
899 EXPECT_EQ(ERR_NETWORK_CHANGED, requests_[0]->WaitForResult()); 902 EXPECT_EQ(ERR_NETWORK_CHANGED, requests_[0]->WaitForResult());
900 903
901 EXPECT_EQ(1u, num_running_jobs()); 904 EXPECT_EQ(1u, num_running_dispatcher_jobs());
902 905
903 EXPECT_FALSE(requests_[1]->completed()); 906 EXPECT_FALSE(requests_[1]->completed());
904 EXPECT_FALSE(requests_[2]->completed()); 907 EXPECT_FALSE(requests_[2]->completed());
905 908
906 EXPECT_EQ(OK, requests_[2]->WaitForResult()); 909 EXPECT_EQ(OK, requests_[2]->WaitForResult());
907 EXPECT_EQ(OK, requests_[1]->result()); 910 EXPECT_EQ(OK, requests_[1]->result());
908 } 911 }
909 912
910 // Tests that a new Request made from the callback of a previously aborted one 913 // Tests that a new Request made from the callback of a previously aborted one
911 // will not be aborted. 914 // will not be aborted.
(...skipping 346 matching lines...) Expand 10 before | Expand all | Expand 10 after
1258 EXPECT_TRUE(rv); 1261 EXPECT_TRUE(rv);
1259 1262
1260 DnsConfig config; 1263 DnsConfig config;
1261 config.nameservers.push_back(IPEndPoint(dns_ip, dns_protocol::kDefaultPort)); 1264 config.nameservers.push_back(IPEndPoint(dns_ip, dns_protocol::kDefaultPort));
1262 EXPECT_TRUE(config.IsValid()); 1265 EXPECT_TRUE(config.IsValid());
1263 return config; 1266 return config;
1264 } 1267 }
1265 1268
1266 // Specialized fixture for tests of DnsTask. 1269 // Specialized fixture for tests of DnsTask.
1267 class HostResolverImplDnsTest : public HostResolverImplTest { 1270 class HostResolverImplDnsTest : public HostResolverImplTest {
1271 public:
1272 HostResolverImplDnsTest() : dns_client_(NULL) {}
1273
1268 protected: 1274 protected:
1275 // testing::Test implementation:
1269 virtual void SetUp() OVERRIDE { 1276 virtual void SetUp() OVERRIDE {
1270 AddDnsRule("nx", dns_protocol::kTypeA, MockDnsClientRule::FAIL); 1277 AddDnsRule("nx", dns_protocol::kTypeA, MockDnsClientRule::FAIL, false);
1271 AddDnsRule("nx", dns_protocol::kTypeAAAA, MockDnsClientRule::FAIL); 1278 AddDnsRule("nx", dns_protocol::kTypeAAAA, MockDnsClientRule::FAIL, false);
1272 AddDnsRule("ok", dns_protocol::kTypeA, MockDnsClientRule::OK); 1279 AddDnsRule("ok", dns_protocol::kTypeA, MockDnsClientRule::OK, false);
1273 AddDnsRule("ok", dns_protocol::kTypeAAAA, MockDnsClientRule::OK); 1280 AddDnsRule("ok", dns_protocol::kTypeAAAA, MockDnsClientRule::OK, false);
1274 AddDnsRule("4ok", dns_protocol::kTypeA, MockDnsClientRule::OK); 1281 AddDnsRule("4ok", dns_protocol::kTypeA, MockDnsClientRule::OK, false);
1275 AddDnsRule("4ok", dns_protocol::kTypeAAAA, MockDnsClientRule::EMPTY); 1282 AddDnsRule("4ok", dns_protocol::kTypeAAAA, MockDnsClientRule::EMPTY, false);
1276 AddDnsRule("6ok", dns_protocol::kTypeA, MockDnsClientRule::EMPTY); 1283 AddDnsRule("6ok", dns_protocol::kTypeA, MockDnsClientRule::EMPTY, false);
1277 AddDnsRule("6ok", dns_protocol::kTypeAAAA, MockDnsClientRule::OK); 1284 AddDnsRule("6ok", dns_protocol::kTypeAAAA, MockDnsClientRule::OK, false);
1278 AddDnsRule("4nx", dns_protocol::kTypeA, MockDnsClientRule::OK); 1285 AddDnsRule("4nx", dns_protocol::kTypeA, MockDnsClientRule::OK, false);
1279 AddDnsRule("4nx", dns_protocol::kTypeAAAA, MockDnsClientRule::FAIL); 1286 AddDnsRule("4nx", dns_protocol::kTypeAAAA, MockDnsClientRule::FAIL, false);
1287
1288 AddDnsRule("4slow_ok", dns_protocol::kTypeA, MockDnsClientRule::OK, true);
1289 AddDnsRule("4slow_ok", dns_protocol::kTypeAAAA, MockDnsClientRule::OK,
1290 false);
1291 AddDnsRule("6slow_ok", dns_protocol::kTypeA, MockDnsClientRule::OK, false);
1292 AddDnsRule("6slow_ok", dns_protocol::kTypeAAAA, MockDnsClientRule::OK,
1293 true);
1294 AddDnsRule("4slow_4ok", dns_protocol::kTypeA, MockDnsClientRule::OK, true);
1295 AddDnsRule("4slow_4ok", dns_protocol::kTypeAAAA, MockDnsClientRule::EMPTY,
1296 false);
1297 AddDnsRule("4slow_4timeout", dns_protocol::kTypeA,
1298 MockDnsClientRule::TIMEOUT, true);
1299 AddDnsRule("4slow_4timeout", dns_protocol::kTypeAAAA, MockDnsClientRule::OK,
1300 false);
1301 AddDnsRule("4slow_6timeout", dns_protocol::kTypeA,
1302 MockDnsClientRule::OK, true);
1303 AddDnsRule("4slow_6timeout", dns_protocol::kTypeAAAA,
1304 MockDnsClientRule::TIMEOUT, false);
1280 CreateResolver(); 1305 CreateResolver();
1281 } 1306 }
1282 1307
1283 void CreateResolver() { 1308 // HostResolverImplTest implementation:
1309 virtual void CreateResolverWithLimitsAndParams(
1310 const PrioritizedDispatcher::Limits& limits,
1311 const HostResolverImpl::ProcTaskParams& params) OVERRIDE {
1284 resolver_.reset(new HostResolverImpl(HostCache::CreateDefaultCache(), 1312 resolver_.reset(new HostResolverImpl(HostCache::CreateDefaultCache(),
1285 DefaultLimits(), 1313 limits,
1286 DefaultParams(proc_.get()), 1314 params,
1287 NULL)); 1315 NULL));
1288 // Disable IPv6 support probing. 1316 // Disable IPv6 support probing.
1289 resolver_->SetDefaultAddressFamily(ADDRESS_FAMILY_UNSPECIFIED); 1317 resolver_->SetDefaultAddressFamily(ADDRESS_FAMILY_UNSPECIFIED);
1290 resolver_->SetDnsClient(CreateMockDnsClient(DnsConfig(), dns_rules_)); 1318 dns_client_ = new MockDnsClient(DnsConfig(), dns_rules_);
1319 resolver_->SetDnsClient(scoped_ptr<DnsClient>(dns_client_));
1291 } 1320 }
1292 1321
1293 // Adds a rule to |dns_rules_|. Must be followed by |CreateResolver| to apply. 1322 // Adds a rule to |dns_rules_|. Must be followed by |CreateResolver| to apply.
1294 void AddDnsRule(const std::string& prefix, 1323 void AddDnsRule(const std::string& prefix,
1295 uint16 qtype, 1324 uint16 qtype,
1296 MockDnsClientRule::Result result) { 1325 MockDnsClientRule::Result result,
1297 dns_rules_.push_back(MockDnsClientRule(prefix, qtype, result)); 1326 bool delay) {
1327 dns_rules_.push_back(MockDnsClientRule(prefix, qtype, result, delay));
1298 } 1328 }
1299 1329
1300 void ChangeDnsConfig(const DnsConfig& config) { 1330 void ChangeDnsConfig(const DnsConfig& config) {
1301 NetworkChangeNotifier::SetDnsConfig(config); 1331 NetworkChangeNotifier::SetDnsConfig(config);
1302 // Notification is delivered asynchronously. 1332 // Notification is delivered asynchronously.
1303 base::MessageLoop::current()->RunUntilIdle(); 1333 base::MessageLoop::current()->RunUntilIdle();
1304 } 1334 }
1305 1335
1306 MockDnsClientRuleList dns_rules_; 1336 MockDnsClientRuleList dns_rules_;
1337 // Owned by |resolver_|.
1338 MockDnsClient* dns_client_;
1307 }; 1339 };
1308 1340
1309 // TODO(szym): Test AbortAllInProgressJobs due to DnsConfig change. 1341 // TODO(szym): Test AbortAllInProgressJobs due to DnsConfig change.
1310 1342
1311 // TODO(cbentzel): Test a mix of requests with different HostResolverFlags. 1343 // TODO(cbentzel): Test a mix of requests with different HostResolverFlags.
1312 1344
1313 // Test successful and fallback resolutions in HostResolverImpl::DnsTask. 1345 // Test successful and fallback resolutions in HostResolverImpl::DnsTask.
1314 TEST_F(HostResolverImplDnsTest, DnsTask) { 1346 TEST_F(HostResolverImplDnsTest, DnsTask) {
1315 resolver_->SetDefaultAddressFamily(ADDRESS_FAMILY_IPV4); 1347 resolver_->SetDefaultAddressFamily(ADDRESS_FAMILY_IPV4);
1316 1348
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
1364 EXPECT_EQ(OK, requests_[1]->WaitForResult()); 1396 EXPECT_EQ(OK, requests_[1]->WaitForResult());
1365 EXPECT_TRUE(requests_[1]->HasOneAddress("192.168.1.102", 80)); 1397 EXPECT_TRUE(requests_[1]->HasOneAddress("192.168.1.102", 80));
1366 1398
1367 ChangeDnsConfig(CreateValidDnsConfig()); 1399 ChangeDnsConfig(CreateValidDnsConfig());
1368 1400
1369 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("ok_abort", 80)->Resolve()); 1401 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("ok_abort", 80)->Resolve());
1370 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("nx_abort", 80)->Resolve()); 1402 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("nx_abort", 80)->Resolve());
1371 1403
1372 // Simulate the case when the preference or policy has disabled the DNS client 1404 // Simulate the case when the preference or policy has disabled the DNS client
1373 // causing AbortDnsTasks. 1405 // causing AbortDnsTasks.
1374 resolver_->SetDnsClient(CreateMockDnsClient(DnsConfig(), dns_rules_)); 1406 resolver_->SetDnsClient(
1407 scoped_ptr<DnsClient>(new MockDnsClient(DnsConfig(), dns_rules_)));
1375 ChangeDnsConfig(CreateValidDnsConfig()); 1408 ChangeDnsConfig(CreateValidDnsConfig());
1376 1409
1377 // First request is resolved by MockDnsClient, others should fail due to 1410 // First request is resolved by MockDnsClient, others should fail due to
1378 // disabled fallback to ProcTask. 1411 // disabled fallback to ProcTask.
1379 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("ok_fail", 80)->Resolve()); 1412 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("ok_fail", 80)->Resolve());
1380 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("nx_fail", 80)->Resolve()); 1413 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("nx_fail", 80)->Resolve());
1381 proc_->SignalMultiple(requests_.size()); 1414 proc_->SignalMultiple(requests_.size());
1382 1415
1383 // Aborted due to Network Change. 1416 // Aborted due to Network Change.
1384 EXPECT_EQ(ERR_NETWORK_CHANGED, requests_[2]->WaitForResult()); 1417 EXPECT_EQ(ERR_NETWORK_CHANGED, requests_[2]->WaitForResult());
(...skipping 203 matching lines...) Expand 10 before | Expand all | Expand 10 after
1588 // Confirm that resolving "localhost" is unrestricted even if there are no 1621 // Confirm that resolving "localhost" is unrestricted even if there are no
1589 // global IPv6 address. See SystemHostResolverCall for rationale. 1622 // global IPv6 address. See SystemHostResolverCall for rationale.
1590 // Test both the DnsClient and system host resolver paths. 1623 // Test both the DnsClient and system host resolver paths.
1591 TEST_F(HostResolverImplDnsTest, DualFamilyLocalhost) { 1624 TEST_F(HostResolverImplDnsTest, DualFamilyLocalhost) {
1592 // Use regular SystemHostResolverCall! 1625 // Use regular SystemHostResolverCall!
1593 scoped_refptr<HostResolverProc> proc(new SystemHostResolverProc()); 1626 scoped_refptr<HostResolverProc> proc(new SystemHostResolverProc());
1594 resolver_.reset(new HostResolverImpl(HostCache::CreateDefaultCache(), 1627 resolver_.reset(new HostResolverImpl(HostCache::CreateDefaultCache(),
1595 DefaultLimits(), 1628 DefaultLimits(),
1596 DefaultParams(proc.get()), 1629 DefaultParams(proc.get()),
1597 NULL)); 1630 NULL));
1598 resolver_->SetDnsClient(CreateMockDnsClient(DnsConfig(), dns_rules_)); 1631 resolver_->SetDnsClient(
1632 scoped_ptr<DnsClient>(new MockDnsClient(DnsConfig(), dns_rules_)));
1599 resolver_->SetDefaultAddressFamily(ADDRESS_FAMILY_IPV4); 1633 resolver_->SetDefaultAddressFamily(ADDRESS_FAMILY_IPV4);
1600 1634
1601 // Get the expected output. 1635 // Get the expected output.
1602 AddressList addrlist; 1636 AddressList addrlist;
1603 int rv = proc->Resolve("localhost", ADDRESS_FAMILY_UNSPECIFIED, 0, &addrlist, 1637 int rv = proc->Resolve("localhost", ADDRESS_FAMILY_UNSPECIFIED, 0, &addrlist,
1604 NULL); 1638 NULL);
1605 if (rv != OK) 1639 if (rv != OK)
1606 return; 1640 return;
1607 1641
1608 for (unsigned i = 0; i < addrlist.size(); ++i) 1642 for (unsigned i = 0; i < addrlist.size(); ++i)
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
1641 1675
1642 ChangeDnsConfig(config); 1676 ChangeDnsConfig(config);
1643 req = CreateRequest(info, DEFAULT_PRIORITY); 1677 req = CreateRequest(info, DEFAULT_PRIORITY);
1644 // Expect synchronous resolution from DnsHosts. 1678 // Expect synchronous resolution from DnsHosts.
1645 EXPECT_EQ(OK, req->Resolve()); 1679 EXPECT_EQ(OK, req->Resolve());
1646 1680
1647 EXPECT_EQ(saw_ipv4, req->HasAddress("127.0.0.1", 80)); 1681 EXPECT_EQ(saw_ipv4, req->HasAddress("127.0.0.1", 80));
1648 EXPECT_EQ(saw_ipv6, req->HasAddress("::1", 80)); 1682 EXPECT_EQ(saw_ipv6, req->HasAddress("::1", 80));
1649 } 1683 }
1650 1684
1685 // Cancel a request with a single DNS transaction active.
1686 TEST_F(HostResolverImplDnsTest, CancelWithOneTransactionActive) {
1687 resolver_->SetDefaultAddressFamily(ADDRESS_FAMILY_IPV4);
1688 ChangeDnsConfig(CreateValidDnsConfig());
1689
1690 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("ok", 80)->Resolve());
1691 EXPECT_EQ(1u, num_running_dispatcher_jobs());
1692 requests_[0]->Cancel();
1693
1694 // Dispatcher state checked in TearDown.
1695 }
1696
1697 // Cancel a request with a single DNS transaction active and another pending.
1698 TEST_F(HostResolverImplDnsTest, CancelWithOneTransactionActiveOnePending) {
1699 CreateSerialResolver();
1700 resolver_->SetDefaultAddressFamily(ADDRESS_FAMILY_UNSPECIFIED);
1701 ChangeDnsConfig(CreateValidDnsConfig());
1702
1703 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("ok", 80)->Resolve());
1704 EXPECT_EQ(1u, num_running_dispatcher_jobs());
1705 requests_[0]->Cancel();
1706
1707 // Dispatcher state checked in TearDown.
1708 }
1709
1710 // Cancel a request with two DNS transactions active.
1711 TEST_F(HostResolverImplDnsTest, CancelWithTwoTransactionsActive) {
1712 resolver_->SetDefaultAddressFamily(ADDRESS_FAMILY_UNSPECIFIED);
1713 ChangeDnsConfig(CreateValidDnsConfig());
1714
1715 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("ok", 80)->Resolve());
1716 EXPECT_EQ(2u, num_running_dispatcher_jobs());
1717 requests_[0]->Cancel();
1718
1719 // Dispatcher state checked in TearDown.
1720 }
1721
1722 // Delete a resolver with some active requests and some queued requests.
1723 TEST_F(HostResolverImplDnsTest, DeleteWithActiveTransactions) {
1724 // At most 10 Jobs active at once.
1725 CreateResolverWithLimitsAndParams(
1726 PrioritizedDispatcher::Limits(NUM_PRIORITIES, 10u),
1727 DefaultParams(proc_.get()));
1728
1729 resolver_->SetDefaultAddressFamily(ADDRESS_FAMILY_UNSPECIFIED);
1730 ChangeDnsConfig(CreateValidDnsConfig());
1731
1732 // First active job is an IPv4 request.
1733 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("ok", 80, MEDIUM,
1734 ADDRESS_FAMILY_IPV4)->Resolve());
1735
1736 // Add 10 more DNS lookups for different hostnames. First 4 should have two
1737 // active jobs, next one has a single active job, and one pending. Others
1738 // should all be queued.
1739 for (int i = 0; i < 10; ++i) {
1740 EXPECT_EQ(ERR_IO_PENDING, CreateRequest(
1741 base::StringPrintf("ok%i", i))->Resolve());
1742 }
1743 EXPECT_EQ(10u, num_running_dispatcher_jobs());
1744
1745 resolver_.reset();
1746 }
1747
1748 // Cancel a request with only the IPv6 transaction active.
1749 TEST_F(HostResolverImplDnsTest, CancelWithIPv6TransactionActive) {
1750 resolver_->SetDefaultAddressFamily(ADDRESS_FAMILY_UNSPECIFIED);
1751 ChangeDnsConfig(CreateValidDnsConfig());
1752
1753 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("6slow_ok", 80)->Resolve());
1754 EXPECT_EQ(2u, num_running_dispatcher_jobs());
1755
1756 // The IPv4 request should complete, the IPv6 request is still pending.
1757 base::RunLoop().RunUntilIdle();
1758 EXPECT_EQ(1u, num_running_dispatcher_jobs());
1759 requests_[0]->Cancel();
1760
1761 // Dispatcher state checked in TearDown.
1762 }
1763
1764 // Cancel a request with only the IPv4 transaction pending.
1765 TEST_F(HostResolverImplDnsTest, CancelWithIPv4TransactionPending) {
1766 set_fallback_to_proctask(false);
1767 resolver_->SetDefaultAddressFamily(ADDRESS_FAMILY_UNSPECIFIED);
1768 ChangeDnsConfig(CreateValidDnsConfig());
1769
1770 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("4slow_ok", 80)->Resolve());
1771 EXPECT_EQ(2u, num_running_dispatcher_jobs());
1772
1773 // The IPv6 request should complete, the IPv4 request is still pending.
1774 base::RunLoop().RunUntilIdle();
1775 EXPECT_EQ(1u, num_running_dispatcher_jobs());
1776
1777 requests_[0]->Cancel();
1778 }
1779
1780 // Test cases where AAAA completes first.
1781 TEST_F(HostResolverImplDnsTest, AAAACompletesFirst) {
1782 set_fallback_to_proctask(false);
1783 resolver_->SetDefaultAddressFamily(ADDRESS_FAMILY_UNSPECIFIED);
1784 ChangeDnsConfig(CreateValidDnsConfig());
1785
1786 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("4slow_ok", 80)->Resolve());
1787 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("4slow_4ok", 80)->Resolve());
1788 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("4slow_4timeout", 80)->Resolve());
1789 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("4slow_6timeout", 80)->Resolve());
1790
1791 base::RunLoop().RunUntilIdle();
1792 EXPECT_FALSE(requests_[0]->completed());
1793 EXPECT_FALSE(requests_[1]->completed());
1794 EXPECT_FALSE(requests_[2]->completed());
1795 // The IPv6 of the third request should have failed and resulted in cancelling
1796 // the IPv4 request.
1797 EXPECT_TRUE(requests_[3]->completed());
1798 EXPECT_EQ(ERR_DNS_TIMED_OUT, requests_[3]->result());
1799 EXPECT_EQ(3u, num_running_dispatcher_jobs());
1800
1801 dns_client_->CompleteDelayedTransactions();
1802 EXPECT_TRUE(requests_[0]->completed());
1803 EXPECT_EQ(OK, requests_[0]->result());
1804 EXPECT_EQ(2u, requests_[0]->NumberOfAddresses());
1805 EXPECT_TRUE(requests_[0]->HasAddress("127.0.0.1", 80));
1806 EXPECT_TRUE(requests_[0]->HasAddress("::1", 80));
1807
1808 EXPECT_TRUE(requests_[1]->completed());
1809 EXPECT_EQ(OK, requests_[1]->result());
1810 EXPECT_EQ(1u, requests_[1]->NumberOfAddresses());
1811 EXPECT_TRUE(requests_[1]->HasAddress("127.0.0.1", 80));
1812
1813 EXPECT_TRUE(requests_[2]->completed());
1814 EXPECT_EQ(ERR_DNS_TIMED_OUT, requests_[2]->result());
1815 }
1816
1817 // Test the case where only a single transaction slot is available.
1818 TEST_F(HostResolverImplDnsTest, SerialResolver) {
1819 CreateSerialResolver();
1820 set_fallback_to_proctask(false);
1821 resolver_->SetDefaultAddressFamily(ADDRESS_FAMILY_UNSPECIFIED);
1822 ChangeDnsConfig(CreateValidDnsConfig());
1823
1824 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("ok", 80)->Resolve());
1825 EXPECT_EQ(1u, num_running_dispatcher_jobs());
1826
1827 base::RunLoop().RunUntilIdle();
1828 EXPECT_TRUE(requests_[0]->completed());
1829 EXPECT_EQ(OK, requests_[0]->result());
1830 EXPECT_EQ(2u, requests_[0]->NumberOfAddresses());
1831 EXPECT_TRUE(requests_[0]->HasAddress("127.0.0.1", 80));
1832 EXPECT_TRUE(requests_[0]->HasAddress("::1", 80));
1833 }
1834
1835 // Test the case where the AAAA query is started when another transaction
1836 // completes.
1837 TEST_F(HostResolverImplDnsTest, AAAAStartsAfterOtherJobFinishes) {
1838 CreateResolverWithLimitsAndParams(
1839 PrioritizedDispatcher::Limits(NUM_PRIORITIES, 2),
1840 DefaultParams(proc_.get()));
1841 set_fallback_to_proctask(false);
1842 resolver_->SetDefaultAddressFamily(ADDRESS_FAMILY_UNSPECIFIED);
1843 ChangeDnsConfig(CreateValidDnsConfig());
1844
1845 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("ok", 80, MEDIUM,
1846 ADDRESS_FAMILY_IPV4)->Resolve());
1847 EXPECT_EQ(ERR_IO_PENDING,
1848 CreateRequest("4slow_ok", 80, MEDIUM)->Resolve());
1849 // An IPv4 request should have been started pending for each job.
1850 EXPECT_EQ(2u, num_running_dispatcher_jobs());
1851
1852 // Request 0's IPv4 request should complete, starting Request 1's IPv6
1853 // request, which should also complete.
1854 base::RunLoop().RunUntilIdle();
1855 EXPECT_EQ(1u, num_running_dispatcher_jobs());
1856 EXPECT_TRUE(requests_[0]->completed());
1857 EXPECT_FALSE(requests_[1]->completed());
1858
1859 dns_client_->CompleteDelayedTransactions();
1860 EXPECT_TRUE(requests_[1]->completed());
1861 EXPECT_EQ(OK, requests_[1]->result());
1862 EXPECT_EQ(2u, requests_[1]->NumberOfAddresses());
1863 EXPECT_TRUE(requests_[1]->HasAddress("127.0.0.1", 80));
1864 EXPECT_TRUE(requests_[1]->HasAddress("::1", 80));
1865 }
1866
1651 } // namespace net 1867 } // namespace net
OLDNEW
« net/dns/host_resolver_impl.cc ('K') | « net/dns/host_resolver_impl.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698