OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/dns/host_resolver_impl.h" | 5 #include "net/dns/host_resolver_impl.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <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" | |
16 #include "base/strings/string_util.h" | 15 #include "base/strings/string_util.h" |
17 #include "base/strings/stringprintf.h" | 16 #include "base/strings/stringprintf.h" |
18 #include "base/synchronization/condition_variable.h" | 17 #include "base/synchronization/condition_variable.h" |
19 #include "base/synchronization/lock.h" | 18 #include "base/synchronization/lock.h" |
20 #include "base/test/test_timeouts.h" | 19 #include "base/test/test_timeouts.h" |
21 #include "base/time/time.h" | 20 #include "base/time/time.h" |
22 #include "net/base/address_list.h" | 21 #include "net/base/address_list.h" |
23 #include "net/base/net_errors.h" | 22 #include "net/base/net_errors.h" |
24 #include "net/base/net_util.h" | 23 #include "net/base/net_util.h" |
25 #include "net/dns/dns_client.h" | 24 #include "net/dns/dns_client.h" |
(...skipping 388 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
414 }; | 413 }; |
415 | 414 |
416 } // namespace | 415 } // namespace |
417 | 416 |
418 class HostResolverImplTest : public testing::Test { | 417 class HostResolverImplTest : public testing::Test { |
419 public: | 418 public: |
420 static const int kDefaultPort = 80; | 419 static const int kDefaultPort = 80; |
421 | 420 |
422 HostResolverImplTest() : proc_(new MockHostResolverProc()) {} | 421 HostResolverImplTest() : proc_(new MockHostResolverProc()) {} |
423 | 422 |
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 | |
438 protected: | 423 protected: |
439 // A Request::Handler which is a proxy to the HostResolverImplTest fixture. | 424 // A Request::Handler which is a proxy to the HostResolverImplTest fixture. |
440 struct Handler : public Request::Handler { | 425 struct Handler : public Request::Handler { |
441 virtual ~Handler() {} | 426 virtual ~Handler() {} |
442 | 427 |
443 // Proxy functions so that classes derived from Handler can access them. | 428 // Proxy functions so that classes derived from Handler can access them. |
444 Request* CreateRequest(const HostResolver::RequestInfo& info, | 429 Request* CreateRequest(const HostResolver::RequestInfo& info, |
445 RequestPriority priority) { | 430 RequestPriority priority) { |
446 return test->CreateRequest(info, priority); | 431 return test->CreateRequest(info, priority); |
447 } | 432 } |
448 Request* CreateRequest(const std::string& hostname, int port) { | 433 Request* CreateRequest(const std::string& hostname, int port) { |
449 return test->CreateRequest(hostname, port); | 434 return test->CreateRequest(hostname, port); |
450 } | 435 } |
451 Request* CreateRequest(const std::string& hostname) { | 436 Request* CreateRequest(const std::string& hostname) { |
452 return test->CreateRequest(hostname); | 437 return test->CreateRequest(hostname); |
453 } | 438 } |
454 ScopedVector<Request>& requests() { return test->requests_; } | 439 ScopedVector<Request>& requests() { return test->requests_; } |
455 | 440 |
456 void DeleteResolver() { test->resolver_.reset(); } | 441 void DeleteResolver() { test->resolver_.reset(); } |
457 | 442 |
458 HostResolverImplTest* test; | 443 HostResolverImplTest* test; |
459 }; | 444 }; |
460 | 445 |
461 // testing::Test implementation: | 446 void CreateResolver() { |
462 virtual void SetUp() OVERRIDE { | 447 resolver_.reset(new HostResolverImpl(HostCache::CreateDefaultCache(), |
463 CreateResolver(); | 448 DefaultLimits(), |
| 449 DefaultParams(proc_.get()), |
| 450 NULL)); |
464 } | 451 } |
465 | 452 |
466 virtual void TearDown() OVERRIDE { | 453 // This HostResolverImpl will only allow 1 outstanding resolve at a time and |
467 if (resolver_.get()) | 454 // perform no retries. |
468 EXPECT_EQ(0u, resolver_->num_running_dispatcher_jobs_for_tests()); | 455 void CreateSerialResolver() { |
469 EXPECT_FALSE(proc_->HasBlockedRequests()); | 456 HostResolverImpl::ProcTaskParams params = DefaultParams(proc_.get()); |
470 } | 457 params.max_retry_attempts = 0u; |
471 | 458 PrioritizedDispatcher::Limits limits(NUM_PRIORITIES, 1); |
472 virtual void CreateResolverWithLimitsAndParams( | 459 resolver_.reset(new HostResolverImpl( |
473 const PrioritizedDispatcher::Limits& limits, | 460 HostCache::CreateDefaultCache(), |
474 const HostResolverImpl::ProcTaskParams& params) { | 461 limits, |
475 resolver_.reset(new HostResolverImpl(HostCache::CreateDefaultCache(), | 462 params, |
476 limits, params, NULL)); | 463 NULL)); |
477 } | 464 } |
478 | 465 |
479 // The Request will not be made until a call to |Resolve()|, and the Job will | 466 // The Request will not be made until a call to |Resolve()|, and the Job will |
480 // not start until released by |proc_->SignalXXX|. | 467 // not start until released by |proc_->SignalXXX|. |
481 Request* CreateRequest(const HostResolver::RequestInfo& info, | 468 Request* CreateRequest(const HostResolver::RequestInfo& info, |
482 RequestPriority priority) { | 469 RequestPriority priority) { |
483 Request* req = new Request( | 470 Request* req = new Request( |
484 info, priority, requests_.size(), resolver_.get(), handler_.get()); | 471 info, priority, requests_.size(), resolver_.get(), handler_.get()); |
485 requests_.push_back(req); | 472 requests_.push_back(req); |
486 return req; | 473 return req; |
(...skipping 15 matching lines...) Expand all Loading... |
502 } | 489 } |
503 | 490 |
504 Request* CreateRequest(const std::string& hostname, int port) { | 491 Request* CreateRequest(const std::string& hostname, int port) { |
505 return CreateRequest(hostname, port, MEDIUM); | 492 return CreateRequest(hostname, port, MEDIUM); |
506 } | 493 } |
507 | 494 |
508 Request* CreateRequest(const std::string& hostname) { | 495 Request* CreateRequest(const std::string& hostname) { |
509 return CreateRequest(hostname, kDefaultPort); | 496 return CreateRequest(hostname, kDefaultPort); |
510 } | 497 } |
511 | 498 |
| 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 |
512 void set_handler(Handler* handler) { | 509 void set_handler(Handler* handler) { |
513 handler_.reset(handler); | 510 handler_.reset(handler); |
514 handler_->test = this; | 511 handler_->test = this; |
515 } | 512 } |
516 | 513 |
517 // Friendship is not inherited, so use proxies to access those. | 514 // Friendship is not inherited, so use proxies to access those. |
518 size_t num_running_dispatcher_jobs() const { | 515 size_t num_running_jobs() const { |
519 DCHECK(resolver_.get()); | 516 DCHECK(resolver_.get()); |
520 return resolver_->num_running_dispatcher_jobs_for_tests(); | 517 return resolver_->num_running_jobs_for_tests(); |
521 } | 518 } |
522 | 519 |
523 void set_fallback_to_proctask(bool fallback_to_proctask) { | 520 void set_fallback_to_proctask(bool fallback_to_proctask) { |
524 DCHECK(resolver_.get()); | 521 DCHECK(resolver_.get()); |
525 resolver_->fallback_to_proctask_ = fallback_to_proctask; | 522 resolver_->fallback_to_proctask_ = fallback_to_proctask; |
526 } | 523 } |
527 | 524 |
528 scoped_refptr<MockHostResolverProc> proc_; | 525 scoped_refptr<MockHostResolverProc> proc_; |
529 scoped_ptr<HostResolverImpl> resolver_; | 526 scoped_ptr<HostResolverImpl> resolver_; |
530 ScopedVector<Request> requests_; | 527 ScopedVector<Request> requests_; |
(...skipping 363 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
894 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("c")->Resolve()); | 891 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("c")->Resolve()); |
895 | 892 |
896 EXPECT_TRUE(proc_->WaitFor(1u)); | 893 EXPECT_TRUE(proc_->WaitFor(1u)); |
897 // Triggering an IP address change. | 894 // Triggering an IP address change. |
898 NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests(); | 895 NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests(); |
899 base::MessageLoop::current()->RunUntilIdle(); // Notification happens async. | 896 base::MessageLoop::current()->RunUntilIdle(); // Notification happens async. |
900 proc_->SignalMultiple(3u); // Let the false-start go so that we can catch it. | 897 proc_->SignalMultiple(3u); // Let the false-start go so that we can catch it. |
901 | 898 |
902 EXPECT_EQ(ERR_NETWORK_CHANGED, requests_[0]->WaitForResult()); | 899 EXPECT_EQ(ERR_NETWORK_CHANGED, requests_[0]->WaitForResult()); |
903 | 900 |
904 EXPECT_EQ(1u, num_running_dispatcher_jobs()); | 901 EXPECT_EQ(1u, num_running_jobs()); |
905 | 902 |
906 EXPECT_FALSE(requests_[1]->completed()); | 903 EXPECT_FALSE(requests_[1]->completed()); |
907 EXPECT_FALSE(requests_[2]->completed()); | 904 EXPECT_FALSE(requests_[2]->completed()); |
908 | 905 |
909 EXPECT_EQ(OK, requests_[2]->WaitForResult()); | 906 EXPECT_EQ(OK, requests_[2]->WaitForResult()); |
910 EXPECT_EQ(OK, requests_[1]->result()); | 907 EXPECT_EQ(OK, requests_[1]->result()); |
911 } | 908 } |
912 | 909 |
913 // Tests that a new Request made from the callback of a previously aborted one | 910 // Tests that a new Request made from the callback of a previously aborted one |
914 // will not be aborted. | 911 // will not be aborted. |
(...skipping 346 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1261 EXPECT_TRUE(rv); | 1258 EXPECT_TRUE(rv); |
1262 | 1259 |
1263 DnsConfig config; | 1260 DnsConfig config; |
1264 config.nameservers.push_back(IPEndPoint(dns_ip, dns_protocol::kDefaultPort)); | 1261 config.nameservers.push_back(IPEndPoint(dns_ip, dns_protocol::kDefaultPort)); |
1265 EXPECT_TRUE(config.IsValid()); | 1262 EXPECT_TRUE(config.IsValid()); |
1266 return config; | 1263 return config; |
1267 } | 1264 } |
1268 | 1265 |
1269 // Specialized fixture for tests of DnsTask. | 1266 // Specialized fixture for tests of DnsTask. |
1270 class HostResolverImplDnsTest : public HostResolverImplTest { | 1267 class HostResolverImplDnsTest : public HostResolverImplTest { |
1271 public: | |
1272 HostResolverImplDnsTest() : dns_client_(NULL) {} | |
1273 | |
1274 protected: | 1268 protected: |
1275 // testing::Test implementation: | |
1276 virtual void SetUp() OVERRIDE { | 1269 virtual void SetUp() OVERRIDE { |
1277 AddDnsRule("nx", dns_protocol::kTypeA, MockDnsClientRule::FAIL, false); | 1270 AddDnsRule("nx", dns_protocol::kTypeA, MockDnsClientRule::FAIL); |
1278 AddDnsRule("nx", dns_protocol::kTypeAAAA, MockDnsClientRule::FAIL, false); | 1271 AddDnsRule("nx", dns_protocol::kTypeAAAA, MockDnsClientRule::FAIL); |
1279 AddDnsRule("ok", dns_protocol::kTypeA, MockDnsClientRule::OK, false); | 1272 AddDnsRule("ok", dns_protocol::kTypeA, MockDnsClientRule::OK); |
1280 AddDnsRule("ok", dns_protocol::kTypeAAAA, MockDnsClientRule::OK, false); | 1273 AddDnsRule("ok", dns_protocol::kTypeAAAA, MockDnsClientRule::OK); |
1281 AddDnsRule("4ok", dns_protocol::kTypeA, MockDnsClientRule::OK, false); | 1274 AddDnsRule("4ok", dns_protocol::kTypeA, MockDnsClientRule::OK); |
1282 AddDnsRule("4ok", dns_protocol::kTypeAAAA, MockDnsClientRule::EMPTY, false); | 1275 AddDnsRule("4ok", dns_protocol::kTypeAAAA, MockDnsClientRule::EMPTY); |
1283 AddDnsRule("6ok", dns_protocol::kTypeA, MockDnsClientRule::EMPTY, false); | 1276 AddDnsRule("6ok", dns_protocol::kTypeA, MockDnsClientRule::EMPTY); |
1284 AddDnsRule("6ok", dns_protocol::kTypeAAAA, MockDnsClientRule::OK, false); | 1277 AddDnsRule("6ok", dns_protocol::kTypeAAAA, MockDnsClientRule::OK); |
1285 AddDnsRule("4nx", dns_protocol::kTypeA, MockDnsClientRule::OK, false); | 1278 AddDnsRule("4nx", dns_protocol::kTypeA, MockDnsClientRule::OK); |
1286 AddDnsRule("4nx", dns_protocol::kTypeAAAA, MockDnsClientRule::FAIL, false); | 1279 AddDnsRule("4nx", dns_protocol::kTypeAAAA, MockDnsClientRule::FAIL); |
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); | |
1305 CreateResolver(); | 1280 CreateResolver(); |
1306 } | 1281 } |
1307 | 1282 |
1308 // HostResolverImplTest implementation: | 1283 void CreateResolver() { |
1309 virtual void CreateResolverWithLimitsAndParams( | |
1310 const PrioritizedDispatcher::Limits& limits, | |
1311 const HostResolverImpl::ProcTaskParams& params) OVERRIDE { | |
1312 resolver_.reset(new HostResolverImpl(HostCache::CreateDefaultCache(), | 1284 resolver_.reset(new HostResolverImpl(HostCache::CreateDefaultCache(), |
1313 limits, | 1285 DefaultLimits(), |
1314 params, | 1286 DefaultParams(proc_.get()), |
1315 NULL)); | 1287 NULL)); |
1316 // Disable IPv6 support probing. | 1288 // Disable IPv6 support probing. |
1317 resolver_->SetDefaultAddressFamily(ADDRESS_FAMILY_UNSPECIFIED); | 1289 resolver_->SetDefaultAddressFamily(ADDRESS_FAMILY_UNSPECIFIED); |
1318 dns_client_ = new MockDnsClient(DnsConfig(), dns_rules_); | 1290 resolver_->SetDnsClient(CreateMockDnsClient(DnsConfig(), dns_rules_)); |
1319 resolver_->SetDnsClient(scoped_ptr<DnsClient>(dns_client_)); | |
1320 } | 1291 } |
1321 | 1292 |
1322 // Adds a rule to |dns_rules_|. Must be followed by |CreateResolver| to apply. | 1293 // Adds a rule to |dns_rules_|. Must be followed by |CreateResolver| to apply. |
1323 void AddDnsRule(const std::string& prefix, | 1294 void AddDnsRule(const std::string& prefix, |
1324 uint16 qtype, | 1295 uint16 qtype, |
1325 MockDnsClientRule::Result result, | 1296 MockDnsClientRule::Result result) { |
1326 bool delay) { | 1297 dns_rules_.push_back(MockDnsClientRule(prefix, qtype, result)); |
1327 dns_rules_.push_back(MockDnsClientRule(prefix, qtype, result, delay)); | |
1328 } | 1298 } |
1329 | 1299 |
1330 void ChangeDnsConfig(const DnsConfig& config) { | 1300 void ChangeDnsConfig(const DnsConfig& config) { |
1331 NetworkChangeNotifier::SetDnsConfig(config); | 1301 NetworkChangeNotifier::SetDnsConfig(config); |
1332 // Notification is delivered asynchronously. | 1302 // Notification is delivered asynchronously. |
1333 base::MessageLoop::current()->RunUntilIdle(); | 1303 base::MessageLoop::current()->RunUntilIdle(); |
1334 } | 1304 } |
1335 | 1305 |
1336 MockDnsClientRuleList dns_rules_; | 1306 MockDnsClientRuleList dns_rules_; |
1337 // Owned by |resolver_|. | |
1338 MockDnsClient* dns_client_; | |
1339 }; | 1307 }; |
1340 | 1308 |
1341 // TODO(szym): Test AbortAllInProgressJobs due to DnsConfig change. | 1309 // TODO(szym): Test AbortAllInProgressJobs due to DnsConfig change. |
1342 | 1310 |
1343 // TODO(cbentzel): Test a mix of requests with different HostResolverFlags. | 1311 // TODO(cbentzel): Test a mix of requests with different HostResolverFlags. |
1344 | 1312 |
1345 // Test successful and fallback resolutions in HostResolverImpl::DnsTask. | 1313 // Test successful and fallback resolutions in HostResolverImpl::DnsTask. |
1346 TEST_F(HostResolverImplDnsTest, DnsTask) { | 1314 TEST_F(HostResolverImplDnsTest, DnsTask) { |
1347 resolver_->SetDefaultAddressFamily(ADDRESS_FAMILY_IPV4); | 1315 resolver_->SetDefaultAddressFamily(ADDRESS_FAMILY_IPV4); |
1348 | 1316 |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1396 EXPECT_EQ(OK, requests_[1]->WaitForResult()); | 1364 EXPECT_EQ(OK, requests_[1]->WaitForResult()); |
1397 EXPECT_TRUE(requests_[1]->HasOneAddress("192.168.1.102", 80)); | 1365 EXPECT_TRUE(requests_[1]->HasOneAddress("192.168.1.102", 80)); |
1398 | 1366 |
1399 ChangeDnsConfig(CreateValidDnsConfig()); | 1367 ChangeDnsConfig(CreateValidDnsConfig()); |
1400 | 1368 |
1401 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("ok_abort", 80)->Resolve()); | 1369 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("ok_abort", 80)->Resolve()); |
1402 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("nx_abort", 80)->Resolve()); | 1370 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("nx_abort", 80)->Resolve()); |
1403 | 1371 |
1404 // Simulate the case when the preference or policy has disabled the DNS client | 1372 // Simulate the case when the preference or policy has disabled the DNS client |
1405 // causing AbortDnsTasks. | 1373 // causing AbortDnsTasks. |
1406 resolver_->SetDnsClient( | 1374 resolver_->SetDnsClient(CreateMockDnsClient(DnsConfig(), dns_rules_)); |
1407 scoped_ptr<DnsClient>(new MockDnsClient(DnsConfig(), dns_rules_))); | |
1408 ChangeDnsConfig(CreateValidDnsConfig()); | 1375 ChangeDnsConfig(CreateValidDnsConfig()); |
1409 | 1376 |
1410 // First request is resolved by MockDnsClient, others should fail due to | 1377 // First request is resolved by MockDnsClient, others should fail due to |
1411 // disabled fallback to ProcTask. | 1378 // disabled fallback to ProcTask. |
1412 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("ok_fail", 80)->Resolve()); | 1379 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("ok_fail", 80)->Resolve()); |
1413 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("nx_fail", 80)->Resolve()); | 1380 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("nx_fail", 80)->Resolve()); |
1414 proc_->SignalMultiple(requests_.size()); | 1381 proc_->SignalMultiple(requests_.size()); |
1415 | 1382 |
1416 // Aborted due to Network Change. | 1383 // Aborted due to Network Change. |
1417 EXPECT_EQ(ERR_NETWORK_CHANGED, requests_[2]->WaitForResult()); | 1384 EXPECT_EQ(ERR_NETWORK_CHANGED, requests_[2]->WaitForResult()); |
(...skipping 203 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1621 // Confirm that resolving "localhost" is unrestricted even if there are no | 1588 // Confirm that resolving "localhost" is unrestricted even if there are no |
1622 // global IPv6 address. See SystemHostResolverCall for rationale. | 1589 // global IPv6 address. See SystemHostResolverCall for rationale. |
1623 // Test both the DnsClient and system host resolver paths. | 1590 // Test both the DnsClient and system host resolver paths. |
1624 TEST_F(HostResolverImplDnsTest, DualFamilyLocalhost) { | 1591 TEST_F(HostResolverImplDnsTest, DualFamilyLocalhost) { |
1625 // Use regular SystemHostResolverCall! | 1592 // Use regular SystemHostResolverCall! |
1626 scoped_refptr<HostResolverProc> proc(new SystemHostResolverProc()); | 1593 scoped_refptr<HostResolverProc> proc(new SystemHostResolverProc()); |
1627 resolver_.reset(new HostResolverImpl(HostCache::CreateDefaultCache(), | 1594 resolver_.reset(new HostResolverImpl(HostCache::CreateDefaultCache(), |
1628 DefaultLimits(), | 1595 DefaultLimits(), |
1629 DefaultParams(proc.get()), | 1596 DefaultParams(proc.get()), |
1630 NULL)); | 1597 NULL)); |
1631 resolver_->SetDnsClient( | 1598 resolver_->SetDnsClient(CreateMockDnsClient(DnsConfig(), dns_rules_)); |
1632 scoped_ptr<DnsClient>(new MockDnsClient(DnsConfig(), dns_rules_))); | |
1633 resolver_->SetDefaultAddressFamily(ADDRESS_FAMILY_IPV4); | 1599 resolver_->SetDefaultAddressFamily(ADDRESS_FAMILY_IPV4); |
1634 | 1600 |
1635 // Get the expected output. | 1601 // Get the expected output. |
1636 AddressList addrlist; | 1602 AddressList addrlist; |
1637 int rv = proc->Resolve("localhost", ADDRESS_FAMILY_UNSPECIFIED, 0, &addrlist, | 1603 int rv = proc->Resolve("localhost", ADDRESS_FAMILY_UNSPECIFIED, 0, &addrlist, |
1638 NULL); | 1604 NULL); |
1639 if (rv != OK) | 1605 if (rv != OK) |
1640 return; | 1606 return; |
1641 | 1607 |
1642 for (unsigned i = 0; i < addrlist.size(); ++i) | 1608 for (unsigned i = 0; i < addrlist.size(); ++i) |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1675 | 1641 |
1676 ChangeDnsConfig(config); | 1642 ChangeDnsConfig(config); |
1677 req = CreateRequest(info, DEFAULT_PRIORITY); | 1643 req = CreateRequest(info, DEFAULT_PRIORITY); |
1678 // Expect synchronous resolution from DnsHosts. | 1644 // Expect synchronous resolution from DnsHosts. |
1679 EXPECT_EQ(OK, req->Resolve()); | 1645 EXPECT_EQ(OK, req->Resolve()); |
1680 | 1646 |
1681 EXPECT_EQ(saw_ipv4, req->HasAddress("127.0.0.1", 80)); | 1647 EXPECT_EQ(saw_ipv4, req->HasAddress("127.0.0.1", 80)); |
1682 EXPECT_EQ(saw_ipv6, req->HasAddress("::1", 80)); | 1648 EXPECT_EQ(saw_ipv6, req->HasAddress("::1", 80)); |
1683 } | 1649 } |
1684 | 1650 |
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 | |
1867 } // namespace net | 1651 } // namespace net |
OLD | NEW |