| 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" |
| 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 381 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 406 }; | 407 }; |
| 407 | 408 |
| 408 } // namespace | 409 } // namespace |
| 409 | 410 |
| 410 class HostResolverImplTest : public testing::Test { | 411 class HostResolverImplTest : public testing::Test { |
| 411 public: | 412 public: |
| 412 static const int kDefaultPort = 80; | 413 static const int kDefaultPort = 80; |
| 413 | 414 |
| 414 HostResolverImplTest() : proc_(new MockHostResolverProc()) {} | 415 HostResolverImplTest() : proc_(new MockHostResolverProc()) {} |
| 415 | 416 |
| 417 void CreateResolver() { |
| 418 CreateResolverWithLimitsAndParams(DefaultLimits(), |
| 419 DefaultParams(proc_.get())); |
| 420 } |
| 421 |
| 422 // This HostResolverImpl will only allow 1 outstanding resolve at a time and |
| 423 // perform no retries. |
| 424 void CreateSerialResolver() { |
| 425 HostResolverImpl::ProcTaskParams params = DefaultParams(proc_.get()); |
| 426 params.max_retry_attempts = 0u; |
| 427 PrioritizedDispatcher::Limits limits(NUM_PRIORITIES, 1); |
| 428 CreateResolverWithLimitsAndParams(limits, params); |
| 429 } |
| 430 |
| 416 protected: | 431 protected: |
| 417 // A Request::Handler which is a proxy to the HostResolverImplTest fixture. | 432 // A Request::Handler which is a proxy to the HostResolverImplTest fixture. |
| 418 struct Handler : public Request::Handler { | 433 struct Handler : public Request::Handler { |
| 419 virtual ~Handler() {} | 434 virtual ~Handler() {} |
| 420 | 435 |
| 421 // Proxy functions so that classes derived from Handler can access them. | 436 // Proxy functions so that classes derived from Handler can access them. |
| 422 Request* CreateRequest(const HostResolver::RequestInfo& info) { | 437 Request* CreateRequest(const HostResolver::RequestInfo& info) { |
| 423 return test->CreateRequest(info); | 438 return test->CreateRequest(info); |
| 424 } | 439 } |
| 425 Request* CreateRequest(const std::string& hostname, int port) { | 440 Request* CreateRequest(const std::string& hostname, int port) { |
| 426 return test->CreateRequest(hostname, port); | 441 return test->CreateRequest(hostname, port); |
| 427 } | 442 } |
| 428 Request* CreateRequest(const std::string& hostname) { | 443 Request* CreateRequest(const std::string& hostname) { |
| 429 return test->CreateRequest(hostname); | 444 return test->CreateRequest(hostname); |
| 430 } | 445 } |
| 431 ScopedVector<Request>& requests() { return test->requests_; } | 446 ScopedVector<Request>& requests() { return test->requests_; } |
| 432 | 447 |
| 433 void DeleteResolver() { test->resolver_.reset(); } | 448 void DeleteResolver() { test->resolver_.reset(); } |
| 434 | 449 |
| 435 HostResolverImplTest* test; | 450 HostResolverImplTest* test; |
| 436 }; | 451 }; |
| 437 | 452 |
| 438 void CreateResolver() { | 453 // testing::Test implementation: |
| 439 resolver_.reset(new HostResolverImpl(HostCache::CreateDefaultCache(), | 454 virtual void SetUp() OVERRIDE { |
| 440 DefaultLimits(), | 455 CreateResolver(); |
| 441 DefaultParams(proc_.get()), | |
| 442 NULL)); | |
| 443 } | 456 } |
| 444 | 457 |
| 445 // This HostResolverImpl will only allow 1 outstanding resolve at a time and | 458 virtual void TearDown() OVERRIDE { |
| 446 // perform no retries. | 459 if (resolver_.get()) |
| 447 void CreateSerialResolver() { | 460 EXPECT_EQ(0u, resolver_->num_running_dispatcher_jobs_for_tests()); |
| 448 HostResolverImpl::ProcTaskParams params = DefaultParams(proc_.get()); | 461 EXPECT_FALSE(proc_->HasBlockedRequests()); |
| 449 params.max_retry_attempts = 0u; | 462 } |
| 450 PrioritizedDispatcher::Limits limits(NUM_PRIORITIES, 1); | 463 |
| 451 resolver_.reset(new HostResolverImpl( | 464 virtual void CreateResolverWithLimitsAndParams( |
| 452 HostCache::CreateDefaultCache(), | 465 const PrioritizedDispatcher::Limits& limits, |
| 453 limits, | 466 const HostResolverImpl::ProcTaskParams& params) { |
| 454 params, | 467 resolver_.reset(new HostResolverImpl(HostCache::CreateDefaultCache(), |
| 455 NULL)); | 468 limits, params, NULL)); |
| 456 } | 469 } |
| 457 | 470 |
| 458 // The Request will not be made until a call to |Resolve()|, and the Job will | 471 // The Request will not be made until a call to |Resolve()|, and the Job will |
| 459 // not start until released by |proc_->SignalXXX|. | 472 // not start until released by |proc_->SignalXXX|. |
| 460 Request* CreateRequest(const HostResolver::RequestInfo& info) { | 473 Request* CreateRequest(const HostResolver::RequestInfo& info) { |
| 461 Request* req = new Request(info, requests_.size(), resolver_.get(), | 474 Request* req = new Request(info, requests_.size(), resolver_.get(), |
| 462 handler_.get()); | 475 handler_.get()); |
| 463 requests_.push_back(req); | 476 requests_.push_back(req); |
| 464 return req; | 477 return req; |
| 465 } | 478 } |
| (...skipping 14 matching lines...) Expand all Loading... |
| 480 } | 493 } |
| 481 | 494 |
| 482 Request* CreateRequest(const std::string& hostname, int port) { | 495 Request* CreateRequest(const std::string& hostname, int port) { |
| 483 return CreateRequest(hostname, port, MEDIUM); | 496 return CreateRequest(hostname, port, MEDIUM); |
| 484 } | 497 } |
| 485 | 498 |
| 486 Request* CreateRequest(const std::string& hostname) { | 499 Request* CreateRequest(const std::string& hostname) { |
| 487 return CreateRequest(hostname, kDefaultPort); | 500 return CreateRequest(hostname, kDefaultPort); |
| 488 } | 501 } |
| 489 | 502 |
| 490 virtual void SetUp() OVERRIDE { | |
| 491 CreateResolver(); | |
| 492 } | |
| 493 | |
| 494 virtual void TearDown() OVERRIDE { | |
| 495 if (resolver_.get()) | |
| 496 EXPECT_EQ(0u, resolver_->num_running_jobs_for_tests()); | |
| 497 EXPECT_FALSE(proc_->HasBlockedRequests()); | |
| 498 } | |
| 499 | |
| 500 void set_handler(Handler* handler) { | 503 void set_handler(Handler* handler) { |
| 501 handler_.reset(handler); | 504 handler_.reset(handler); |
| 502 handler_->test = this; | 505 handler_->test = this; |
| 503 } | 506 } |
| 504 | 507 |
| 505 // Friendship is not inherited, so use proxies to access those. | 508 // Friendship is not inherited, so use proxies to access those. |
| 506 size_t num_running_jobs() const { | 509 size_t num_running_dispatcher_jobs() const { |
| 507 DCHECK(resolver_.get()); | 510 DCHECK(resolver_.get()); |
| 508 return resolver_->num_running_jobs_for_tests(); | 511 return resolver_->num_running_dispatcher_jobs_for_tests(); |
| 509 } | 512 } |
| 510 | 513 |
| 511 void set_fallback_to_proctask(bool fallback_to_proctask) { | 514 void set_fallback_to_proctask(bool fallback_to_proctask) { |
| 512 DCHECK(resolver_.get()); | 515 DCHECK(resolver_.get()); |
| 513 resolver_->fallback_to_proctask_ = fallback_to_proctask; | 516 resolver_->fallback_to_proctask_ = fallback_to_proctask; |
| 514 } | 517 } |
| 515 | 518 |
| 516 scoped_refptr<MockHostResolverProc> proc_; | 519 scoped_refptr<MockHostResolverProc> proc_; |
| 517 scoped_ptr<HostResolverImpl> resolver_; | 520 scoped_ptr<HostResolverImpl> resolver_; |
| 518 ScopedVector<Request> requests_; | 521 ScopedVector<Request> requests_; |
| (...skipping 363 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 882 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("c")->Resolve()); | 885 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("c")->Resolve()); |
| 883 | 886 |
| 884 EXPECT_TRUE(proc_->WaitFor(1u)); | 887 EXPECT_TRUE(proc_->WaitFor(1u)); |
| 885 // Triggering an IP address change. | 888 // Triggering an IP address change. |
| 886 NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests(); | 889 NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests(); |
| 887 base::MessageLoop::current()->RunUntilIdle(); // Notification happens async. | 890 base::MessageLoop::current()->RunUntilIdle(); // Notification happens async. |
| 888 proc_->SignalMultiple(3u); // Let the false-start go so that we can catch it. | 891 proc_->SignalMultiple(3u); // Let the false-start go so that we can catch it. |
| 889 | 892 |
| 890 EXPECT_EQ(ERR_NETWORK_CHANGED, requests_[0]->WaitForResult()); | 893 EXPECT_EQ(ERR_NETWORK_CHANGED, requests_[0]->WaitForResult()); |
| 891 | 894 |
| 892 EXPECT_EQ(1u, num_running_jobs()); | 895 EXPECT_EQ(1u, num_running_dispatcher_jobs()); |
| 893 | 896 |
| 894 EXPECT_FALSE(requests_[1]->completed()); | 897 EXPECT_FALSE(requests_[1]->completed()); |
| 895 EXPECT_FALSE(requests_[2]->completed()); | 898 EXPECT_FALSE(requests_[2]->completed()); |
| 896 | 899 |
| 897 EXPECT_EQ(OK, requests_[2]->WaitForResult()); | 900 EXPECT_EQ(OK, requests_[2]->WaitForResult()); |
| 898 EXPECT_EQ(OK, requests_[1]->result()); | 901 EXPECT_EQ(OK, requests_[1]->result()); |
| 899 } | 902 } |
| 900 | 903 |
| 901 // Tests that a new Request made from the callback of a previously aborted one | 904 // Tests that a new Request made from the callback of a previously aborted one |
| 902 // will not be aborted. | 905 // will not be aborted. |
| (...skipping 346 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1249 EXPECT_TRUE(rv); | 1252 EXPECT_TRUE(rv); |
| 1250 | 1253 |
| 1251 DnsConfig config; | 1254 DnsConfig config; |
| 1252 config.nameservers.push_back(IPEndPoint(dns_ip, dns_protocol::kDefaultPort)); | 1255 config.nameservers.push_back(IPEndPoint(dns_ip, dns_protocol::kDefaultPort)); |
| 1253 EXPECT_TRUE(config.IsValid()); | 1256 EXPECT_TRUE(config.IsValid()); |
| 1254 return config; | 1257 return config; |
| 1255 } | 1258 } |
| 1256 | 1259 |
| 1257 // Specialized fixture for tests of DnsTask. | 1260 // Specialized fixture for tests of DnsTask. |
| 1258 class HostResolverImplDnsTest : public HostResolverImplTest { | 1261 class HostResolverImplDnsTest : public HostResolverImplTest { |
| 1262 public: |
| 1263 HostResolverImplDnsTest() : dns_client_(NULL) {} |
| 1264 |
| 1259 protected: | 1265 protected: |
| 1266 // testing::Test implementation: |
| 1260 virtual void SetUp() OVERRIDE { | 1267 virtual void SetUp() OVERRIDE { |
| 1261 AddDnsRule("nx", dns_protocol::kTypeA, MockDnsClientRule::FAIL); | 1268 AddDnsRule("nx", dns_protocol::kTypeA, MockDnsClientRule::FAIL, false); |
| 1262 AddDnsRule("nx", dns_protocol::kTypeAAAA, MockDnsClientRule::FAIL); | 1269 AddDnsRule("nx", dns_protocol::kTypeAAAA, MockDnsClientRule::FAIL, false); |
| 1263 AddDnsRule("ok", dns_protocol::kTypeA, MockDnsClientRule::OK); | 1270 AddDnsRule("ok", dns_protocol::kTypeA, MockDnsClientRule::OK, false); |
| 1264 AddDnsRule("ok", dns_protocol::kTypeAAAA, MockDnsClientRule::OK); | 1271 AddDnsRule("ok", dns_protocol::kTypeAAAA, MockDnsClientRule::OK, false); |
| 1265 AddDnsRule("4ok", dns_protocol::kTypeA, MockDnsClientRule::OK); | 1272 AddDnsRule("4ok", dns_protocol::kTypeA, MockDnsClientRule::OK, false); |
| 1266 AddDnsRule("4ok", dns_protocol::kTypeAAAA, MockDnsClientRule::EMPTY); | 1273 AddDnsRule("4ok", dns_protocol::kTypeAAAA, MockDnsClientRule::EMPTY, false); |
| 1267 AddDnsRule("6ok", dns_protocol::kTypeA, MockDnsClientRule::EMPTY); | 1274 AddDnsRule("6ok", dns_protocol::kTypeA, MockDnsClientRule::EMPTY, false); |
| 1268 AddDnsRule("6ok", dns_protocol::kTypeAAAA, MockDnsClientRule::OK); | 1275 AddDnsRule("6ok", dns_protocol::kTypeAAAA, MockDnsClientRule::OK, false); |
| 1269 AddDnsRule("4nx", dns_protocol::kTypeA, MockDnsClientRule::OK); | 1276 AddDnsRule("4nx", dns_protocol::kTypeA, MockDnsClientRule::OK, false); |
| 1270 AddDnsRule("4nx", dns_protocol::kTypeAAAA, MockDnsClientRule::FAIL); | 1277 AddDnsRule("4nx", dns_protocol::kTypeAAAA, MockDnsClientRule::FAIL, false); |
| 1278 |
| 1279 AddDnsRule("4slow_ok", dns_protocol::kTypeA, MockDnsClientRule::OK, true); |
| 1280 AddDnsRule("4slow_ok", dns_protocol::kTypeAAAA, MockDnsClientRule::OK, |
| 1281 false); |
| 1282 AddDnsRule("6slow_ok", dns_protocol::kTypeA, MockDnsClientRule::OK, false); |
| 1283 AddDnsRule("6slow_ok", dns_protocol::kTypeAAAA, MockDnsClientRule::OK, |
| 1284 true); |
| 1285 AddDnsRule("4slow_4ok", dns_protocol::kTypeA, MockDnsClientRule::OK, true); |
| 1286 AddDnsRule("4slow_4ok", dns_protocol::kTypeAAAA, MockDnsClientRule::EMPTY, |
| 1287 false); |
| 1288 AddDnsRule("4slow_4timeout", dns_protocol::kTypeA, |
| 1289 MockDnsClientRule::TIMEOUT, true); |
| 1290 AddDnsRule("4slow_4timeout", dns_protocol::kTypeAAAA, MockDnsClientRule::OK, |
| 1291 false); |
| 1292 AddDnsRule("4slow_6timeout", dns_protocol::kTypeA, |
| 1293 MockDnsClientRule::OK, true); |
| 1294 AddDnsRule("4slow_6timeout", dns_protocol::kTypeAAAA, |
| 1295 MockDnsClientRule::TIMEOUT, false); |
| 1271 CreateResolver(); | 1296 CreateResolver(); |
| 1272 } | 1297 } |
| 1273 | 1298 |
| 1274 void CreateResolver() { | 1299 // HostResolverImplTest implementation: |
| 1300 virtual void CreateResolverWithLimitsAndParams( |
| 1301 const PrioritizedDispatcher::Limits& limits, |
| 1302 const HostResolverImpl::ProcTaskParams& params) OVERRIDE { |
| 1275 resolver_.reset(new HostResolverImpl(HostCache::CreateDefaultCache(), | 1303 resolver_.reset(new HostResolverImpl(HostCache::CreateDefaultCache(), |
| 1276 DefaultLimits(), | 1304 limits, |
| 1277 DefaultParams(proc_.get()), | 1305 params, |
| 1278 NULL)); | 1306 NULL)); |
| 1279 // Disable IPv6 support probing. | 1307 // Disable IPv6 support probing. |
| 1280 resolver_->SetDefaultAddressFamily(ADDRESS_FAMILY_UNSPECIFIED); | 1308 resolver_->SetDefaultAddressFamily(ADDRESS_FAMILY_UNSPECIFIED); |
| 1281 resolver_->SetDnsClient(CreateMockDnsClient(DnsConfig(), dns_rules_)); | 1309 dns_client_ = new MockDnsClient(DnsConfig(), dns_rules_); |
| 1310 resolver_->SetDnsClient(scoped_ptr<DnsClient>(dns_client_)); |
| 1282 } | 1311 } |
| 1283 | 1312 |
| 1284 // Adds a rule to |dns_rules_|. Must be followed by |CreateResolver| to apply. | 1313 // Adds a rule to |dns_rules_|. Must be followed by |CreateResolver| to apply. |
| 1285 void AddDnsRule(const std::string& prefix, | 1314 void AddDnsRule(const std::string& prefix, |
| 1286 uint16 qtype, | 1315 uint16 qtype, |
| 1287 MockDnsClientRule::Result result) { | 1316 MockDnsClientRule::Result result, |
| 1288 dns_rules_.push_back(MockDnsClientRule(prefix, qtype, result)); | 1317 bool delay) { |
| 1318 dns_rules_.push_back(MockDnsClientRule(prefix, qtype, result, delay)); |
| 1289 } | 1319 } |
| 1290 | 1320 |
| 1291 void ChangeDnsConfig(const DnsConfig& config) { | 1321 void ChangeDnsConfig(const DnsConfig& config) { |
| 1292 NetworkChangeNotifier::SetDnsConfig(config); | 1322 NetworkChangeNotifier::SetDnsConfig(config); |
| 1293 // Notification is delivered asynchronously. | 1323 // Notification is delivered asynchronously. |
| 1294 base::MessageLoop::current()->RunUntilIdle(); | 1324 base::MessageLoop::current()->RunUntilIdle(); |
| 1295 } | 1325 } |
| 1296 | 1326 |
| 1297 MockDnsClientRuleList dns_rules_; | 1327 MockDnsClientRuleList dns_rules_; |
| 1328 // Owned by |resolver_|. |
| 1329 MockDnsClient* dns_client_; |
| 1298 }; | 1330 }; |
| 1299 | 1331 |
| 1300 // TODO(szym): Test AbortAllInProgressJobs due to DnsConfig change. | 1332 // TODO(szym): Test AbortAllInProgressJobs due to DnsConfig change. |
| 1301 | 1333 |
| 1302 // TODO(cbentzel): Test a mix of requests with different HostResolverFlags. | 1334 // TODO(cbentzel): Test a mix of requests with different HostResolverFlags. |
| 1303 | 1335 |
| 1304 // Test successful and fallback resolutions in HostResolverImpl::DnsTask. | 1336 // Test successful and fallback resolutions in HostResolverImpl::DnsTask. |
| 1305 TEST_F(HostResolverImplDnsTest, DnsTask) { | 1337 TEST_F(HostResolverImplDnsTest, DnsTask) { |
| 1306 resolver_->SetDefaultAddressFamily(ADDRESS_FAMILY_IPV4); | 1338 resolver_->SetDefaultAddressFamily(ADDRESS_FAMILY_IPV4); |
| 1307 | 1339 |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1355 EXPECT_EQ(OK, requests_[1]->WaitForResult()); | 1387 EXPECT_EQ(OK, requests_[1]->WaitForResult()); |
| 1356 EXPECT_TRUE(requests_[1]->HasOneAddress("192.168.1.102", 80)); | 1388 EXPECT_TRUE(requests_[1]->HasOneAddress("192.168.1.102", 80)); |
| 1357 | 1389 |
| 1358 ChangeDnsConfig(CreateValidDnsConfig()); | 1390 ChangeDnsConfig(CreateValidDnsConfig()); |
| 1359 | 1391 |
| 1360 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("ok_abort", 80)->Resolve()); | 1392 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("ok_abort", 80)->Resolve()); |
| 1361 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("nx_abort", 80)->Resolve()); | 1393 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("nx_abort", 80)->Resolve()); |
| 1362 | 1394 |
| 1363 // Simulate the case when the preference or policy has disabled the DNS client | 1395 // Simulate the case when the preference or policy has disabled the DNS client |
| 1364 // causing AbortDnsTasks. | 1396 // causing AbortDnsTasks. |
| 1365 resolver_->SetDnsClient(CreateMockDnsClient(DnsConfig(), dns_rules_)); | 1397 resolver_->SetDnsClient( |
| 1398 scoped_ptr<DnsClient>(new MockDnsClient(DnsConfig(), dns_rules_))); |
| 1366 ChangeDnsConfig(CreateValidDnsConfig()); | 1399 ChangeDnsConfig(CreateValidDnsConfig()); |
| 1367 | 1400 |
| 1368 // First request is resolved by MockDnsClient, others should fail due to | 1401 // First request is resolved by MockDnsClient, others should fail due to |
| 1369 // disabled fallback to ProcTask. | 1402 // disabled fallback to ProcTask. |
| 1370 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("ok_fail", 80)->Resolve()); | 1403 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("ok_fail", 80)->Resolve()); |
| 1371 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("nx_fail", 80)->Resolve()); | 1404 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("nx_fail", 80)->Resolve()); |
| 1372 proc_->SignalMultiple(requests_.size()); | 1405 proc_->SignalMultiple(requests_.size()); |
| 1373 | 1406 |
| 1374 // Aborted due to Network Change. | 1407 // Aborted due to Network Change. |
| 1375 EXPECT_EQ(ERR_NETWORK_CHANGED, requests_[2]->WaitForResult()); | 1408 EXPECT_EQ(ERR_NETWORK_CHANGED, requests_[2]->WaitForResult()); |
| (...skipping 203 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1579 // Confirm that resolving "localhost" is unrestricted even if there are no | 1612 // Confirm that resolving "localhost" is unrestricted even if there are no |
| 1580 // global IPv6 address. See SystemHostResolverCall for rationale. | 1613 // global IPv6 address. See SystemHostResolverCall for rationale. |
| 1581 // Test both the DnsClient and system host resolver paths. | 1614 // Test both the DnsClient and system host resolver paths. |
| 1582 TEST_F(HostResolverImplDnsTest, DualFamilyLocalhost) { | 1615 TEST_F(HostResolverImplDnsTest, DualFamilyLocalhost) { |
| 1583 // Use regular SystemHostResolverCall! | 1616 // Use regular SystemHostResolverCall! |
| 1584 scoped_refptr<HostResolverProc> proc(new SystemHostResolverProc()); | 1617 scoped_refptr<HostResolverProc> proc(new SystemHostResolverProc()); |
| 1585 resolver_.reset(new HostResolverImpl(HostCache::CreateDefaultCache(), | 1618 resolver_.reset(new HostResolverImpl(HostCache::CreateDefaultCache(), |
| 1586 DefaultLimits(), | 1619 DefaultLimits(), |
| 1587 DefaultParams(proc.get()), | 1620 DefaultParams(proc.get()), |
| 1588 NULL)); | 1621 NULL)); |
| 1589 resolver_->SetDnsClient(CreateMockDnsClient(DnsConfig(), dns_rules_)); | 1622 resolver_->SetDnsClient( |
| 1623 scoped_ptr<DnsClient>(new MockDnsClient(DnsConfig(), dns_rules_))); |
| 1590 resolver_->SetDefaultAddressFamily(ADDRESS_FAMILY_IPV4); | 1624 resolver_->SetDefaultAddressFamily(ADDRESS_FAMILY_IPV4); |
| 1591 | 1625 |
| 1592 // Get the expected output. | 1626 // Get the expected output. |
| 1593 AddressList addrlist; | 1627 AddressList addrlist; |
| 1594 int rv = proc->Resolve("localhost", ADDRESS_FAMILY_UNSPECIFIED, 0, &addrlist, | 1628 int rv = proc->Resolve("localhost", ADDRESS_FAMILY_UNSPECIFIED, 0, &addrlist, |
| 1595 NULL); | 1629 NULL); |
| 1596 if (rv != OK) | 1630 if (rv != OK) |
| 1597 return; | 1631 return; |
| 1598 | 1632 |
| 1599 for (unsigned i = 0; i < addrlist.size(); ++i) | 1633 for (unsigned i = 0; i < addrlist.size(); ++i) |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1633 | 1667 |
| 1634 ChangeDnsConfig(config); | 1668 ChangeDnsConfig(config); |
| 1635 req = CreateRequest(info); | 1669 req = CreateRequest(info); |
| 1636 // Expect synchronous resolution from DnsHosts. | 1670 // Expect synchronous resolution from DnsHosts. |
| 1637 EXPECT_EQ(OK, req->Resolve()); | 1671 EXPECT_EQ(OK, req->Resolve()); |
| 1638 | 1672 |
| 1639 EXPECT_EQ(saw_ipv4, req->HasAddress("127.0.0.1", 80)); | 1673 EXPECT_EQ(saw_ipv4, req->HasAddress("127.0.0.1", 80)); |
| 1640 EXPECT_EQ(saw_ipv6, req->HasAddress("::1", 80)); | 1674 EXPECT_EQ(saw_ipv6, req->HasAddress("::1", 80)); |
| 1641 } | 1675 } |
| 1642 | 1676 |
| 1677 // Cancel a request with a single DNS transaction active. |
| 1678 TEST_F(HostResolverImplDnsTest, CancelWithOneTransactionActive) { |
| 1679 resolver_->SetDefaultAddressFamily(ADDRESS_FAMILY_IPV4); |
| 1680 ChangeDnsConfig(CreateValidDnsConfig()); |
| 1681 |
| 1682 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("ok", 80)->Resolve()); |
| 1683 EXPECT_EQ(1u, num_running_dispatcher_jobs()); |
| 1684 requests_[0]->Cancel(); |
| 1685 |
| 1686 // Dispatcher state checked in TearDown. |
| 1687 } |
| 1688 |
| 1689 // Cancel a request with a single DNS transaction active and another pending. |
| 1690 TEST_F(HostResolverImplDnsTest, CancelWithOneTransactionActiveOnePending) { |
| 1691 CreateSerialResolver(); |
| 1692 resolver_->SetDefaultAddressFamily(ADDRESS_FAMILY_UNSPECIFIED); |
| 1693 ChangeDnsConfig(CreateValidDnsConfig()); |
| 1694 |
| 1695 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("ok", 80)->Resolve()); |
| 1696 EXPECT_EQ(1u, num_running_dispatcher_jobs()); |
| 1697 requests_[0]->Cancel(); |
| 1698 |
| 1699 // Dispatcher state checked in TearDown. |
| 1700 } |
| 1701 |
| 1702 // Cancel a request with two DNS transactions active. |
| 1703 TEST_F(HostResolverImplDnsTest, CancelWithTwoTransactionsActive) { |
| 1704 resolver_->SetDefaultAddressFamily(ADDRESS_FAMILY_UNSPECIFIED); |
| 1705 ChangeDnsConfig(CreateValidDnsConfig()); |
| 1706 |
| 1707 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("ok", 80)->Resolve()); |
| 1708 EXPECT_EQ(2u, num_running_dispatcher_jobs()); |
| 1709 requests_[0]->Cancel(); |
| 1710 |
| 1711 // Dispatcher state checked in TearDown. |
| 1712 } |
| 1713 |
| 1714 // Delete a resolver with some active requests and some queued requests. |
| 1715 TEST_F(HostResolverImplDnsTest, DeleteWithActiveTransactions) { |
| 1716 // At most 10 Jobs active at once. |
| 1717 CreateResolverWithLimitsAndParams( |
| 1718 PrioritizedDispatcher::Limits(NUM_PRIORITIES, 10u), |
| 1719 DefaultParams(proc_.get())); |
| 1720 |
| 1721 resolver_->SetDefaultAddressFamily(ADDRESS_FAMILY_UNSPECIFIED); |
| 1722 ChangeDnsConfig(CreateValidDnsConfig()); |
| 1723 |
| 1724 // First active job is an IPv4 request. |
| 1725 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("ok", 80, MEDIUM, |
| 1726 ADDRESS_FAMILY_IPV4)->Resolve()); |
| 1727 |
| 1728 // Add 10 more DNS lookups for different hostnames. First 4 should have two |
| 1729 // active jobs, next one has a single active job, and one pending. Others |
| 1730 // should all be queued. |
| 1731 for (int i = 0; i < 10; ++i) { |
| 1732 EXPECT_EQ(ERR_IO_PENDING, CreateRequest( |
| 1733 base::StringPrintf("ok%i", i))->Resolve()); |
| 1734 } |
| 1735 EXPECT_EQ(10u, num_running_dispatcher_jobs()); |
| 1736 |
| 1737 resolver_.reset(); |
| 1738 } |
| 1739 |
| 1740 // Cancel a request with only the IPv6 transaction active. |
| 1741 TEST_F(HostResolverImplDnsTest, CancelWithIPv6TransactionActive) { |
| 1742 resolver_->SetDefaultAddressFamily(ADDRESS_FAMILY_UNSPECIFIED); |
| 1743 ChangeDnsConfig(CreateValidDnsConfig()); |
| 1744 |
| 1745 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("6slow_ok", 80)->Resolve()); |
| 1746 EXPECT_EQ(2u, num_running_dispatcher_jobs()); |
| 1747 |
| 1748 // The IPv4 request should complete, the IPv6 request is still pending. |
| 1749 base::RunLoop().RunUntilIdle(); |
| 1750 EXPECT_EQ(1u, num_running_dispatcher_jobs()); |
| 1751 requests_[0]->Cancel(); |
| 1752 |
| 1753 // Dispatcher state checked in TearDown. |
| 1754 } |
| 1755 |
| 1756 // Cancel a request with only the IPv4 transaction pending. |
| 1757 TEST_F(HostResolverImplDnsTest, CancelWithIPv4TransactionPending) { |
| 1758 set_fallback_to_proctask(false); |
| 1759 resolver_->SetDefaultAddressFamily(ADDRESS_FAMILY_UNSPECIFIED); |
| 1760 ChangeDnsConfig(CreateValidDnsConfig()); |
| 1761 |
| 1762 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("4slow_ok", 80)->Resolve()); |
| 1763 EXPECT_EQ(2u, num_running_dispatcher_jobs()); |
| 1764 |
| 1765 // The IPv6 request should complete, the IPv4 request is still pending. |
| 1766 base::RunLoop().RunUntilIdle(); |
| 1767 EXPECT_EQ(1u, num_running_dispatcher_jobs()); |
| 1768 |
| 1769 requests_[0]->Cancel(); |
| 1770 } |
| 1771 |
| 1772 // Test cases where AAAA completes first. |
| 1773 TEST_F(HostResolverImplDnsTest, AAAACompletesFirst) { |
| 1774 set_fallback_to_proctask(false); |
| 1775 resolver_->SetDefaultAddressFamily(ADDRESS_FAMILY_UNSPECIFIED); |
| 1776 ChangeDnsConfig(CreateValidDnsConfig()); |
| 1777 |
| 1778 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("4slow_ok", 80)->Resolve()); |
| 1779 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("4slow_4ok", 80)->Resolve()); |
| 1780 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("4slow_4timeout", 80)->Resolve()); |
| 1781 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("4slow_6timeout", 80)->Resolve()); |
| 1782 |
| 1783 base::RunLoop().RunUntilIdle(); |
| 1784 EXPECT_FALSE(requests_[0]->completed()); |
| 1785 EXPECT_FALSE(requests_[1]->completed()); |
| 1786 EXPECT_FALSE(requests_[2]->completed()); |
| 1787 // The IPv6 of the third request should have failed and resulted in cancelling |
| 1788 // the IPv4 request. |
| 1789 EXPECT_TRUE(requests_[3]->completed()); |
| 1790 EXPECT_EQ(ERR_DNS_TIMED_OUT, requests_[3]->result()); |
| 1791 EXPECT_EQ(3u, num_running_dispatcher_jobs()); |
| 1792 |
| 1793 dns_client_->CompleteDelayedTransactions(); |
| 1794 EXPECT_TRUE(requests_[0]->completed()); |
| 1795 EXPECT_EQ(OK, requests_[0]->result()); |
| 1796 EXPECT_EQ(2u, requests_[0]->NumberOfAddresses()); |
| 1797 EXPECT_TRUE(requests_[0]->HasAddress("127.0.0.1", 80)); |
| 1798 EXPECT_TRUE(requests_[0]->HasAddress("::1", 80)); |
| 1799 |
| 1800 EXPECT_TRUE(requests_[1]->completed()); |
| 1801 EXPECT_EQ(OK, requests_[1]->result()); |
| 1802 EXPECT_EQ(1u, requests_[1]->NumberOfAddresses()); |
| 1803 EXPECT_TRUE(requests_[1]->HasAddress("127.0.0.1", 80)); |
| 1804 |
| 1805 EXPECT_TRUE(requests_[2]->completed()); |
| 1806 EXPECT_EQ(ERR_DNS_TIMED_OUT, requests_[2]->result()); |
| 1807 } |
| 1808 |
| 1809 // Test the case where only a single transaction slot is available. |
| 1810 TEST_F(HostResolverImplDnsTest, SerialResolver) { |
| 1811 CreateSerialResolver(); |
| 1812 set_fallback_to_proctask(false); |
| 1813 resolver_->SetDefaultAddressFamily(ADDRESS_FAMILY_UNSPECIFIED); |
| 1814 ChangeDnsConfig(CreateValidDnsConfig()); |
| 1815 |
| 1816 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("ok", 80)->Resolve()); |
| 1817 EXPECT_EQ(1u, num_running_dispatcher_jobs()); |
| 1818 |
| 1819 base::RunLoop().RunUntilIdle(); |
| 1820 EXPECT_TRUE(requests_[0]->completed()); |
| 1821 EXPECT_EQ(OK, requests_[0]->result()); |
| 1822 EXPECT_EQ(2u, requests_[0]->NumberOfAddresses()); |
| 1823 EXPECT_TRUE(requests_[0]->HasAddress("127.0.0.1", 80)); |
| 1824 EXPECT_TRUE(requests_[0]->HasAddress("::1", 80)); |
| 1825 } |
| 1826 |
| 1827 // Test the case where the AAAA query is started when another transaction |
| 1828 // completes. |
| 1829 TEST_F(HostResolverImplDnsTest, AAAAStartsAfterOtherJobFinishes) { |
| 1830 CreateResolverWithLimitsAndParams( |
| 1831 PrioritizedDispatcher::Limits(NUM_PRIORITIES, 2), |
| 1832 DefaultParams(proc_.get())); |
| 1833 set_fallback_to_proctask(false); |
| 1834 resolver_->SetDefaultAddressFamily(ADDRESS_FAMILY_UNSPECIFIED); |
| 1835 ChangeDnsConfig(CreateValidDnsConfig()); |
| 1836 |
| 1837 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("ok", 80, MEDIUM, |
| 1838 ADDRESS_FAMILY_IPV4)->Resolve()); |
| 1839 EXPECT_EQ(ERR_IO_PENDING, |
| 1840 CreateRequest("4slow_ok", 80, MEDIUM)->Resolve()); |
| 1841 // An IPv4 request should have been started pending for each job. |
| 1842 EXPECT_EQ(2u, num_running_dispatcher_jobs()); |
| 1843 |
| 1844 // Request 0's IPv4 request should complete, starting Request 1's IPv6 |
| 1845 // request, which should also complete. |
| 1846 base::RunLoop().RunUntilIdle(); |
| 1847 EXPECT_EQ(1u, num_running_dispatcher_jobs()); |
| 1848 EXPECT_TRUE(requests_[0]->completed()); |
| 1849 EXPECT_FALSE(requests_[1]->completed()); |
| 1850 |
| 1851 dns_client_->CompleteDelayedTransactions(); |
| 1852 EXPECT_TRUE(requests_[1]->completed()); |
| 1853 EXPECT_EQ(OK, requests_[1]->result()); |
| 1854 EXPECT_EQ(2u, requests_[1]->NumberOfAddresses()); |
| 1855 EXPECT_TRUE(requests_[1]->HasAddress("127.0.0.1", 80)); |
| 1856 EXPECT_TRUE(requests_[1]->HasAddress("::1", 80)); |
| 1857 } |
| 1858 |
| 1643 } // namespace net | 1859 } // namespace net |
| OLD | NEW |