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

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

Issue 1163903002: Remove "Default Address Family" behavior from the HostResolver. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: TestHostResolverImpl style nits Created 5 years, 6 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
« no previous file with comments | « 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"
(...skipping 398 matching lines...) Expand 10 before | Expand all | Expand 10 after
409 int current_attempt_number_; // Incremented whenever Resolve is called. 409 int current_attempt_number_; // Incremented whenever Resolve is called.
410 int total_attempts_; 410 int total_attempts_;
411 int total_attempts_resolved_; 411 int total_attempts_resolved_;
412 int resolved_attempt_number_; 412 int resolved_attempt_number_;
413 413
414 // All attempts wait for right attempt to be resolve. 414 // All attempts wait for right attempt to be resolve.
415 base::Lock lock_; 415 base::Lock lock_;
416 base::ConditionVariable all_done_; 416 base::ConditionVariable all_done_;
417 }; 417 };
418 418
419 // TestHostResolverImpl's sole purpose is to mock the IPv6 reachability test.
420 // By default, this pretends that IPv6 is globally reachable.
421 // This class is necessary so unit tests run the same on dual-stack machines as
422 // well as IPv4 only machines.
423 class TestHostResolverImpl : public HostResolverImpl {
424 public:
425 TestHostResolverImpl(const Options& options, NetLog* net_log)
426 : TestHostResolverImpl(options, net_log, true) {}
427
428 TestHostResolverImpl(const Options& options,
429 NetLog* net_log,
430 bool ipv6_reachable)
431 : HostResolverImpl(options, net_log), ipv6_reachable_(ipv6_reachable) {}
432
433 ~TestHostResolverImpl() override {}
434
435 private:
436 const bool ipv6_reachable_;
437
438 bool IsIPv6Reachable(const BoundNetLog& net_log) override {
439 return ipv6_reachable_;
440 }
441 };
442
419 } // namespace 443 } // namespace
420 444
421 class HostResolverImplTest : public testing::Test { 445 class HostResolverImplTest : public testing::Test {
422 public: 446 public:
423 static const int kDefaultPort = 80; 447 static const int kDefaultPort = 80;
424 448
425 HostResolverImplTest() : proc_(new MockHostResolverProc()) {} 449 HostResolverImplTest() : proc_(new MockHostResolverProc()) {}
426 450
427 void CreateResolver() { 451 void CreateResolver() {
428 CreateResolverWithLimitsAndParams(kMaxJobs, 452 CreateResolverWithLimitsAndParams(kMaxJobs,
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
467 if (resolver_.get()) 491 if (resolver_.get())
468 EXPECT_EQ(0u, resolver_->num_running_dispatcher_jobs_for_tests()); 492 EXPECT_EQ(0u, resolver_->num_running_dispatcher_jobs_for_tests());
469 EXPECT_FALSE(proc_->HasBlockedRequests()); 493 EXPECT_FALSE(proc_->HasBlockedRequests());
470 } 494 }
471 495
472 virtual void CreateResolverWithLimitsAndParams( 496 virtual void CreateResolverWithLimitsAndParams(
473 size_t max_concurrent_resolves, 497 size_t max_concurrent_resolves,
474 const HostResolverImpl::ProcTaskParams& params) { 498 const HostResolverImpl::ProcTaskParams& params) {
475 HostResolverImpl::Options options = DefaultOptions(); 499 HostResolverImpl::Options options = DefaultOptions();
476 options.max_concurrent_resolves = max_concurrent_resolves; 500 options.max_concurrent_resolves = max_concurrent_resolves;
477 resolver_.reset(new HostResolverImpl(options, NULL)); 501 resolver_.reset(new TestHostResolverImpl(options, NULL));
478 resolver_->set_proc_params_for_test(params); 502 resolver_->set_proc_params_for_test(params);
479 } 503 }
480 504
481 // The Request will not be made until a call to |Resolve()|, and the Job will 505 // The Request will not be made until a call to |Resolve()|, and the Job will
482 // not start until released by |proc_->SignalXXX|. 506 // not start until released by |proc_->SignalXXX|.
483 Request* CreateRequest(const HostResolver::RequestInfo& info, 507 Request* CreateRequest(const HostResolver::RequestInfo& info,
484 RequestPriority priority) { 508 RequestPriority priority) {
485 Request* req = new Request( 509 Request* req = new Request(
486 info, priority, requests_.size(), resolver_.get(), handler_.get()); 510 info, priority, requests_.size(), resolver_.get(), handler_.get());
487 requests_.push_back(req); 511 requests_.push_back(req);
(...skipping 359 matching lines...) Expand 10 before | Expand all | Expand 10 after
847 // Since caching is disabled, this will result in another async request. 871 // Since caching is disabled, this will result in another async request.
848 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("a", 70)->Resolve()); 872 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("a", 70)->Resolve());
849 } 873 }
850 } 874 }
851 }; 875 };
852 set_handler(new MyHandler()); 876 set_handler(new MyHandler());
853 877
854 // Turn off caching for this host resolver. 878 // Turn off caching for this host resolver.
855 HostResolver::Options options = DefaultOptions(); 879 HostResolver::Options options = DefaultOptions();
856 options.enable_caching = false; 880 options.enable_caching = false;
857 resolver_.reset(new HostResolverImpl(options, NULL)); 881 resolver_.reset(new TestHostResolverImpl(options, NULL));
858 resolver_->set_proc_params_for_test(DefaultParams(proc_.get())); 882 resolver_->set_proc_params_for_test(DefaultParams(proc_.get()));
859 883
860 for (size_t i = 0; i < 4; ++i) { 884 for (size_t i = 0; i < 4; ++i) {
861 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("a", 80 + i)->Resolve()) << i; 885 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("a", 80 + i)->Resolve()) << i;
862 } 886 }
863 887
864 proc_->SignalMultiple(2u); // One for "a". One for the second "a". 888 proc_->SignalMultiple(2u); // One for "a". One for the second "a".
865 889
866 EXPECT_EQ(OK, requests_[0]->WaitForResult()); 890 EXPECT_EQ(OK, requests_[0]->WaitForResult());
867 ASSERT_EQ(5u, requests_.size()); 891 ASSERT_EQ(5u, requests_.size());
(...skipping 301 matching lines...) Expand 10 before | Expand all | Expand 10 after
1169 EXPECT_EQ("req7", capture_list[3].hostname); 1193 EXPECT_EQ("req7", capture_list[3].hostname);
1170 1194
1171 // Verify that the evicted (incomplete) requests were not cached. 1195 // Verify that the evicted (incomplete) requests were not cached.
1172 EXPECT_EQ(4u, resolver_->GetHostCache()->size()); 1196 EXPECT_EQ(4u, resolver_->GetHostCache()->size());
1173 1197
1174 for (size_t i = 0; i < requests_.size(); ++i) { 1198 for (size_t i = 0; i < requests_.size(); ++i) {
1175 EXPECT_TRUE(requests_[i]->completed()) << i; 1199 EXPECT_TRUE(requests_[i]->completed()) << i;
1176 } 1200 }
1177 } 1201 }
1178 1202
1179 // Tests that after changing the default AddressFamily to IPV4, requests
1180 // with UNSPECIFIED address family map to IPV4.
1181 TEST_F(HostResolverImplTest, SetDefaultAddressFamily_IPv4) {
1182 CreateSerialResolver(); // To guarantee order of resolutions.
1183
1184 proc_->AddRule("h1", ADDRESS_FAMILY_IPV4, "1.0.0.1");
1185 proc_->AddRule("h1", ADDRESS_FAMILY_IPV6, "::2");
1186
1187 resolver_->SetDefaultAddressFamily(ADDRESS_FAMILY_IPV4);
1188
1189 CreateRequest("h1", 80, MEDIUM, ADDRESS_FAMILY_UNSPECIFIED);
1190 CreateRequest("h1", 80, MEDIUM, ADDRESS_FAMILY_IPV4);
1191 CreateRequest("h1", 80, MEDIUM, ADDRESS_FAMILY_IPV6);
1192
1193 // Start all of the requests.
1194 for (size_t i = 0; i < requests_.size(); ++i) {
1195 EXPECT_EQ(ERR_IO_PENDING, requests_[i]->Resolve()) << i;
1196 }
1197
1198 proc_->SignalMultiple(requests_.size());
1199
1200 // Wait for all the requests to complete.
1201 for (size_t i = 0u; i < requests_.size(); ++i) {
1202 EXPECT_EQ(OK, requests_[i]->WaitForResult()) << i;
1203 }
1204
1205 // Since the requests all had the same priority and we limited the thread
1206 // count to 1, they should have completed in the same order as they were
1207 // requested. Moreover, request0 and request1 will have been serviced by
1208 // the same job.
1209
1210 MockHostResolverProc::CaptureList capture_list = proc_->GetCaptureList();
1211 ASSERT_EQ(2u, capture_list.size());
1212
1213 EXPECT_EQ("h1", capture_list[0].hostname);
1214 EXPECT_EQ(ADDRESS_FAMILY_IPV4, capture_list[0].address_family);
1215
1216 EXPECT_EQ("h1", capture_list[1].hostname);
1217 EXPECT_EQ(ADDRESS_FAMILY_IPV6, capture_list[1].address_family);
1218
1219 // Now check that the correct resolved IP addresses were returned.
1220 EXPECT_TRUE(requests_[0]->HasOneAddress("1.0.0.1", 80));
1221 EXPECT_TRUE(requests_[1]->HasOneAddress("1.0.0.1", 80));
1222 EXPECT_TRUE(requests_[2]->HasOneAddress("::2", 80));
1223 }
1224
1225 // This is the exact same test as SetDefaultAddressFamily_IPv4, except the
1226 // default family is set to IPv6 and the family of requests is flipped where
1227 // specified.
1228 TEST_F(HostResolverImplTest, SetDefaultAddressFamily_IPv6) {
1229 CreateSerialResolver(); // To guarantee order of resolutions.
1230
1231 // Don't use IPv6 replacements here since some systems don't support it.
1232 proc_->AddRule("h1", ADDRESS_FAMILY_IPV4, "1.0.0.1");
1233 proc_->AddRule("h1", ADDRESS_FAMILY_IPV6, "::2");
1234
1235 resolver_->SetDefaultAddressFamily(ADDRESS_FAMILY_IPV6);
1236
1237 CreateRequest("h1", 80, MEDIUM, ADDRESS_FAMILY_UNSPECIFIED);
1238 CreateRequest("h1", 80, MEDIUM, ADDRESS_FAMILY_IPV6);
1239 CreateRequest("h1", 80, MEDIUM, ADDRESS_FAMILY_IPV4);
1240
1241 // Start all of the requests.
1242 for (size_t i = 0; i < requests_.size(); ++i) {
1243 EXPECT_EQ(ERR_IO_PENDING, requests_[i]->Resolve()) << i;
1244 }
1245
1246 proc_->SignalMultiple(requests_.size());
1247
1248 // Wait for all the requests to complete.
1249 for (size_t i = 0u; i < requests_.size(); ++i) {
1250 EXPECT_EQ(OK, requests_[i]->WaitForResult()) << i;
1251 }
1252
1253 // Since the requests all had the same priority and we limited the thread
1254 // count to 1, they should have completed in the same order as they were
1255 // requested. Moreover, request0 and request1 will have been serviced by
1256 // the same job.
1257
1258 MockHostResolverProc::CaptureList capture_list = proc_->GetCaptureList();
1259 ASSERT_EQ(2u, capture_list.size());
1260
1261 EXPECT_EQ("h1", capture_list[0].hostname);
1262 EXPECT_EQ(ADDRESS_FAMILY_IPV6, capture_list[0].address_family);
1263
1264 EXPECT_EQ("h1", capture_list[1].hostname);
1265 EXPECT_EQ(ADDRESS_FAMILY_IPV4, capture_list[1].address_family);
1266
1267 // Now check that the correct resolved IP addresses were returned.
1268 EXPECT_TRUE(requests_[0]->HasOneAddress("::2", 80));
1269 EXPECT_TRUE(requests_[1]->HasOneAddress("::2", 80));
1270 EXPECT_TRUE(requests_[2]->HasOneAddress("1.0.0.1", 80));
1271 }
1272
1273 // Make sure that the address family parameter is respected when raw IPs are 1203 // Make sure that the address family parameter is respected when raw IPs are
1274 // passed in. 1204 // passed in.
1275 TEST_F(HostResolverImplTest, AddressFamilyWithRawIPs) { 1205 TEST_F(HostResolverImplTest, AddressFamilyWithRawIPs) {
1276 Request* request = 1206 Request* request =
1277 CreateRequest("127.0.0.1", 80, MEDIUM, ADDRESS_FAMILY_IPV4); 1207 CreateRequest("127.0.0.1", 80, MEDIUM, ADDRESS_FAMILY_IPV4);
1278 EXPECT_EQ(OK, request->Resolve()); 1208 EXPECT_EQ(OK, request->Resolve());
1279 EXPECT_TRUE(request->HasOneAddress("127.0.0.1", 80)); 1209 EXPECT_TRUE(request->HasOneAddress("127.0.0.1", 80));
1280 1210
1281 request = CreateRequest("127.0.0.1", 80, MEDIUM, ADDRESS_FAMILY_IPV6); 1211 request = CreateRequest("127.0.0.1", 80, MEDIUM, ADDRESS_FAMILY_IPV6);
1282 EXPECT_EQ(ERR_NAME_NOT_RESOLVED, request->Resolve()); 1212 EXPECT_EQ(ERR_NAME_NOT_RESOLVED, request->Resolve());
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
1329 new LookupAttemptHostResolverProc( 1259 new LookupAttemptHostResolverProc(
1330 NULL, kAttemptNumberToResolve, kTotalAttempts)); 1260 NULL, kAttemptNumberToResolve, kTotalAttempts));
1331 1261
1332 HostResolverImpl::ProcTaskParams params = DefaultParams(resolver_proc.get()); 1262 HostResolverImpl::ProcTaskParams params = DefaultParams(resolver_proc.get());
1333 1263
1334 // Specify smaller interval for unresponsive_delay_ for HostResolverImpl so 1264 // Specify smaller interval for unresponsive_delay_ for HostResolverImpl so
1335 // that unit test runs faster. For example, this test finishes in 1.5 secs 1265 // that unit test runs faster. For example, this test finishes in 1.5 secs
1336 // (500ms * 3). 1266 // (500ms * 3).
1337 params.unresponsive_delay = base::TimeDelta::FromMilliseconds(500); 1267 params.unresponsive_delay = base::TimeDelta::FromMilliseconds(500);
1338 1268
1339 resolver_.reset(new HostResolverImpl(DefaultOptions(), NULL)); 1269 resolver_.reset(new TestHostResolverImpl(DefaultOptions(), NULL));
1340 resolver_->set_proc_params_for_test(params); 1270 resolver_->set_proc_params_for_test(params);
1341 1271
1342 // Resolve "host1". 1272 // Resolve "host1".
1343 HostResolver::RequestInfo info(HostPortPair("host1", 70)); 1273 HostResolver::RequestInfo info(HostPortPair("host1", 70));
1344 Request* req = CreateRequest(info, DEFAULT_PRIORITY); 1274 Request* req = CreateRequest(info, DEFAULT_PRIORITY);
1345 EXPECT_EQ(ERR_IO_PENDING, req->Resolve()); 1275 EXPECT_EQ(ERR_IO_PENDING, req->Resolve());
1346 1276
1347 // Resolve returns -4 to indicate that 3rd attempt has resolved the host. 1277 // Resolve returns -4 to indicate that 3rd attempt has resolved the host.
1348 EXPECT_EQ(-4, req->WaitForResult()); 1278 EXPECT_EQ(-4, req->WaitForResult());
1349 1279
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
1396 request = CreateRequest("not_reserved2"); 1326 request = CreateRequest("not_reserved2");
1397 EXPECT_EQ(ERR_IO_PENDING, request->Resolve()); 1327 EXPECT_EQ(ERR_IO_PENDING, request->Resolve());
1398 EXPECT_EQ(OK, request->WaitForResult()); 1328 EXPECT_EQ(OK, request->WaitForResult());
1399 1329
1400 request = CreateRequest("not_reserved3"); 1330 request = CreateRequest("not_reserved3");
1401 EXPECT_EQ(ERR_IO_PENDING, request->Resolve()); 1331 EXPECT_EQ(ERR_IO_PENDING, request->Resolve());
1402 EXPECT_EQ(OK, request->WaitForResult()); 1332 EXPECT_EQ(OK, request->WaitForResult());
1403 } 1333 }
1404 1334
1405 TEST_F(HostResolverImplTest, IsIPv6Reachable) { 1335 TEST_F(HostResolverImplTest, IsIPv6Reachable) {
1336 // The real HostResolverImpl is needed since TestHostResolverImpl will
1337 // bypass the IPv6 reachability tests.
1338 resolver_.reset(new HostResolverImpl(DefaultOptions(), nullptr));
1339
1406 // Verify that two consecutive calls return the same value. 1340 // Verify that two consecutive calls return the same value.
1407 TestNetLog net_log; 1341 TestNetLog net_log;
1408 BoundNetLog bound_net_log = BoundNetLog::Make(&net_log, NetLog::SOURCE_NONE); 1342 BoundNetLog bound_net_log = BoundNetLog::Make(&net_log, NetLog::SOURCE_NONE);
1409 bool result1 = IsIPv6Reachable(bound_net_log); 1343 bool result1 = IsIPv6Reachable(bound_net_log);
1410 bool result2 = IsIPv6Reachable(bound_net_log); 1344 bool result2 = IsIPv6Reachable(bound_net_log);
1411 EXPECT_EQ(result1, result2); 1345 EXPECT_EQ(result1, result2);
1412 1346
1413 // Filter reachability check events and verify that there are two of them. 1347 // Filter reachability check events and verify that there are two of them.
1414 TestNetLogEntry::List event_list; 1348 TestNetLogEntry::List event_list;
1415 net_log.GetEntries(&event_list); 1349 net_log.GetEntries(&event_list);
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
1485 MockDnsClientRule::TIMEOUT, false); 1419 MockDnsClientRule::TIMEOUT, false);
1486 CreateResolver(); 1420 CreateResolver();
1487 } 1421 }
1488 1422
1489 // HostResolverImplTest implementation: 1423 // HostResolverImplTest implementation:
1490 void CreateResolverWithLimitsAndParams( 1424 void CreateResolverWithLimitsAndParams(
1491 size_t max_concurrent_resolves, 1425 size_t max_concurrent_resolves,
1492 const HostResolverImpl::ProcTaskParams& params) override { 1426 const HostResolverImpl::ProcTaskParams& params) override {
1493 HostResolverImpl::Options options = DefaultOptions(); 1427 HostResolverImpl::Options options = DefaultOptions();
1494 options.max_concurrent_resolves = max_concurrent_resolves; 1428 options.max_concurrent_resolves = max_concurrent_resolves;
1495 resolver_.reset(new HostResolverImpl(options, NULL)); 1429 resolver_.reset(new TestHostResolverImpl(options, NULL));
1496 resolver_->set_proc_params_for_test(params); 1430 resolver_->set_proc_params_for_test(params);
1497 // Disable IPv6 support probing.
1498 resolver_->SetDefaultAddressFamily(ADDRESS_FAMILY_UNSPECIFIED);
1499 dns_client_ = new MockDnsClient(DnsConfig(), dns_rules_); 1431 dns_client_ = new MockDnsClient(DnsConfig(), dns_rules_);
1500 resolver_->SetDnsClient(scoped_ptr<DnsClient>(dns_client_)); 1432 resolver_->SetDnsClient(scoped_ptr<DnsClient>(dns_client_));
1501 } 1433 }
1502 1434
1503 // Adds a rule to |dns_rules_|. Must be followed by |CreateResolver| to apply. 1435 // Adds a rule to |dns_rules_|. Must be followed by |CreateResolver| to apply.
1504 void AddDnsRule(const std::string& prefix, 1436 void AddDnsRule(const std::string& prefix,
1505 uint16 qtype, 1437 uint16 qtype,
1506 MockDnsClientRule::Result result, 1438 MockDnsClientRule::Result result,
1507 bool delay) { 1439 bool delay) {
1508 dns_rules_.push_back(MockDnsClientRule(prefix, qtype, result, delay)); 1440 dns_rules_.push_back(MockDnsClientRule(prefix, qtype, result, delay));
1509 } 1441 }
1510 1442
1511 void ChangeDnsConfig(const DnsConfig& config) { 1443 void ChangeDnsConfig(const DnsConfig& config) {
1512 NetworkChangeNotifier::SetDnsConfig(config); 1444 NetworkChangeNotifier::SetDnsConfig(config);
1513 // Notification is delivered asynchronously. 1445 // Notification is delivered asynchronously.
1514 base::MessageLoop::current()->RunUntilIdle(); 1446 base::MessageLoop::current()->RunUntilIdle();
1515 } 1447 }
1516 1448
1517 MockDnsClientRuleList dns_rules_; 1449 MockDnsClientRuleList dns_rules_;
1518 // Owned by |resolver_|. 1450 // Owned by |resolver_|.
1519 MockDnsClient* dns_client_; 1451 MockDnsClient* dns_client_;
1520 }; 1452 };
1521 1453
1522 // TODO(szym): Test AbortAllInProgressJobs due to DnsConfig change. 1454 // TODO(szym): Test AbortAllInProgressJobs due to DnsConfig change.
1523 1455
1524 // TODO(cbentzel): Test a mix of requests with different HostResolverFlags. 1456 // TODO(cbentzel): Test a mix of requests with different HostResolverFlags.
1525 1457
1526 // Test successful and fallback resolutions in HostResolverImpl::DnsTask. 1458 // Test successful and fallback resolutions in HostResolverImpl::DnsTask.
1527 TEST_F(HostResolverImplDnsTest, DnsTask) { 1459 TEST_F(HostResolverImplDnsTest, DnsTask) {
1528 resolver_->SetDefaultAddressFamily(ADDRESS_FAMILY_IPV4);
1529
1530 proc_->AddRuleForAllFamilies("nx_succeed", "192.168.1.102"); 1460 proc_->AddRuleForAllFamilies("nx_succeed", "192.168.1.102");
1531 // All other hostnames will fail in proc_. 1461 // All other hostnames will fail in proc_.
1532 1462
1533 // Initially there is no config, so client should not be invoked. 1463 // Initially there is no config, so client should not be invoked.
1534 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("ok_fail", 80)->Resolve()); 1464 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("ok_fail", 80)->Resolve());
1535 proc_->SignalMultiple(requests_.size()); 1465 proc_->SignalMultiple(requests_.size());
1536 1466
1537 EXPECT_EQ(ERR_NAME_NOT_RESOLVED, requests_[0]->WaitForResult()); 1467 EXPECT_EQ(ERR_NAME_NOT_RESOLVED, requests_[0]->WaitForResult());
1538 1468
1539 ChangeDnsConfig(CreateValidDnsConfig()); 1469 ChangeDnsConfig(CreateValidDnsConfig());
1540 1470
1541 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("ok_fail", 80)->Resolve()); 1471 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("ok_fail", 80, MEDIUM,
1542 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("nx_fail", 80)->Resolve()); 1472 ADDRESS_FAMILY_IPV4)->Resolve());
1543 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("nx_succeed", 80)->Resolve()); 1473 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("nx_fail", 80, MEDIUM,
1474 ADDRESS_FAMILY_IPV4)->Resolve());
1475 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("nx_succeed", 80, MEDIUM,
1476 ADDRESS_FAMILY_IPV4)->Resolve());
1544 1477
1545 proc_->SignalMultiple(requests_.size()); 1478 proc_->SignalMultiple(requests_.size());
1546 1479
1547 for (size_t i = 1; i < requests_.size(); ++i) 1480 for (size_t i = 1; i < requests_.size(); ++i)
1548 EXPECT_NE(ERR_UNEXPECTED, requests_[i]->WaitForResult()) << i; 1481 EXPECT_NE(ERR_UNEXPECTED, requests_[i]->WaitForResult()) << i;
1549 1482
1550 EXPECT_EQ(OK, requests_[1]->result()); 1483 EXPECT_EQ(OK, requests_[1]->result());
1551 // Resolved by MockDnsClient. 1484 // Resolved by MockDnsClient.
1552 EXPECT_TRUE(requests_[1]->HasOneAddress("127.0.0.1", 80)); 1485 EXPECT_TRUE(requests_[1]->HasOneAddress("127.0.0.1", 80));
1553 // Fallback to ProcTask. 1486 // Fallback to ProcTask.
1554 EXPECT_EQ(ERR_NAME_NOT_RESOLVED, requests_[2]->result()); 1487 EXPECT_EQ(ERR_NAME_NOT_RESOLVED, requests_[2]->result());
1555 EXPECT_EQ(OK, requests_[3]->result()); 1488 EXPECT_EQ(OK, requests_[3]->result());
1556 EXPECT_TRUE(requests_[3]->HasOneAddress("192.168.1.102", 80)); 1489 EXPECT_TRUE(requests_[3]->HasOneAddress("192.168.1.102", 80));
1557 } 1490 }
1558 1491
1559 // Test successful and failing resolutions in HostResolverImpl::DnsTask when 1492 // Test successful and failing resolutions in HostResolverImpl::DnsTask when
1560 // fallback to ProcTask is disabled. 1493 // fallback to ProcTask is disabled.
1561 TEST_F(HostResolverImplDnsTest, NoFallbackToProcTask) { 1494 TEST_F(HostResolverImplDnsTest, NoFallbackToProcTask) {
1562 resolver_->SetDefaultAddressFamily(ADDRESS_FAMILY_IPV4);
1563 set_fallback_to_proctask(false); 1495 set_fallback_to_proctask(false);
1564 1496
1565 proc_->AddRuleForAllFamilies("nx_succeed", "192.168.1.102"); 1497 proc_->AddRuleForAllFamilies("nx_succeed", "192.168.1.102");
1566 // All other hostnames will fail in proc_. 1498 // All other hostnames will fail in proc_.
1567 1499
1568 // Set empty DnsConfig. 1500 // Set empty DnsConfig.
1569 ChangeDnsConfig(DnsConfig()); 1501 ChangeDnsConfig(DnsConfig());
1570 // Initially there is no config, so client should not be invoked. 1502 // Initially there is no config, so client should not be invoked.
1571 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("ok_fail", 80)->Resolve()); 1503 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("ok_fail", 80)->Resolve());
1572 // There is no config, so fallback to ProcTask must work. 1504 // There is no config, so fallback to ProcTask must work.
1573 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("nx_succeed", 80)->Resolve()); 1505 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("nx_succeed", 80)->Resolve());
1574 proc_->SignalMultiple(requests_.size()); 1506 proc_->SignalMultiple(requests_.size());
1575 1507
1576 EXPECT_EQ(ERR_NAME_NOT_RESOLVED, requests_[0]->WaitForResult()); 1508 EXPECT_EQ(ERR_NAME_NOT_RESOLVED, requests_[0]->WaitForResult());
1577 EXPECT_EQ(OK, requests_[1]->WaitForResult()); 1509 EXPECT_EQ(OK, requests_[1]->WaitForResult());
1578 EXPECT_TRUE(requests_[1]->HasOneAddress("192.168.1.102", 80)); 1510 EXPECT_TRUE(requests_[1]->HasOneAddress("192.168.1.102", 80));
1579 1511
1580 ChangeDnsConfig(CreateValidDnsConfig()); 1512 ChangeDnsConfig(CreateValidDnsConfig());
1581 1513
1582 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("ok_abort", 80)->Resolve()); 1514 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("ok_abort", 80, MEDIUM,
1583 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("nx_abort", 80)->Resolve()); 1515 ADDRESS_FAMILY_IPV4)->Resolve());
1516 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("nx_abort", 80, MEDIUM,
1517 ADDRESS_FAMILY_IPV4)->Resolve());
1584 1518
1585 // Simulate the case when the preference or policy has disabled the DNS client 1519 // Simulate the case when the preference or policy has disabled the DNS client
1586 // causing AbortDnsTasks. 1520 // causing AbortDnsTasks.
1587 resolver_->SetDnsClient( 1521 resolver_->SetDnsClient(
1588 scoped_ptr<DnsClient>(new MockDnsClient(DnsConfig(), dns_rules_))); 1522 scoped_ptr<DnsClient>(new MockDnsClient(DnsConfig(), dns_rules_)));
1589 ChangeDnsConfig(CreateValidDnsConfig()); 1523 ChangeDnsConfig(CreateValidDnsConfig());
1590 1524
1591 // First request is resolved by MockDnsClient, others should fail due to 1525 // First request is resolved by MockDnsClient, others should fail due to
1592 // disabled fallback to ProcTask. 1526 // disabled fallback to ProcTask.
1593 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("ok_fail", 80)->Resolve()); 1527 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("ok_fail", 80, MEDIUM,
1594 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("nx_fail", 80)->Resolve()); 1528 ADDRESS_FAMILY_IPV4)->Resolve());
1529 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("nx_fail", 80, MEDIUM,
1530 ADDRESS_FAMILY_IPV4)->Resolve());
1595 proc_->SignalMultiple(requests_.size()); 1531 proc_->SignalMultiple(requests_.size());
1596 1532
1597 // Aborted due to Network Change. 1533 // Aborted due to Network Change.
1598 EXPECT_EQ(ERR_NETWORK_CHANGED, requests_[2]->WaitForResult()); 1534 EXPECT_EQ(ERR_NETWORK_CHANGED, requests_[2]->WaitForResult());
1599 EXPECT_EQ(ERR_NETWORK_CHANGED, requests_[3]->WaitForResult()); 1535 EXPECT_EQ(ERR_NETWORK_CHANGED, requests_[3]->WaitForResult());
1600 // Resolved by MockDnsClient. 1536 // Resolved by MockDnsClient.
1601 EXPECT_EQ(OK, requests_[4]->WaitForResult()); 1537 EXPECT_EQ(OK, requests_[4]->WaitForResult());
1602 EXPECT_TRUE(requests_[4]->HasOneAddress("127.0.0.1", 80)); 1538 EXPECT_TRUE(requests_[4]->HasOneAddress("127.0.0.1", 80));
1603 // Fallback to ProcTask is disabled. 1539 // Fallback to ProcTask is disabled.
1604 EXPECT_EQ(ERR_NAME_NOT_RESOLVED, requests_[5]->WaitForResult()); 1540 EXPECT_EQ(ERR_NAME_NOT_RESOLVED, requests_[5]->WaitForResult());
1605 } 1541 }
1606 1542
1607 // Test behavior of OnDnsTaskFailure when Job is aborted. 1543 // Test behavior of OnDnsTaskFailure when Job is aborted.
1608 TEST_F(HostResolverImplDnsTest, OnDnsTaskFailureAbortedJob) { 1544 TEST_F(HostResolverImplDnsTest, OnDnsTaskFailureAbortedJob) {
1609 resolver_->SetDefaultAddressFamily(ADDRESS_FAMILY_IPV4);
1610 ChangeDnsConfig(CreateValidDnsConfig()); 1545 ChangeDnsConfig(CreateValidDnsConfig());
1611 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("nx_abort", 80)->Resolve()); 1546 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("nx_abort", 80)->Resolve());
1612 // Abort all jobs here. 1547 // Abort all jobs here.
1613 CreateResolver(); 1548 CreateResolver();
1614 proc_->SignalMultiple(requests_.size()); 1549 proc_->SignalMultiple(requests_.size());
1615 // Run to completion. 1550 // Run to completion.
1616 base::MessageLoop::current()->RunUntilIdle(); // Notification happens async. 1551 base::MessageLoop::current()->RunUntilIdle(); // Notification happens async.
1617 // It shouldn't crash during OnDnsTaskFailure callbacks. 1552 // It shouldn't crash during OnDnsTaskFailure callbacks.
1618 EXPECT_EQ(ERR_IO_PENDING, requests_[0]->result()); 1553 EXPECT_EQ(ERR_IO_PENDING, requests_[0]->result());
1619 1554
1620 // Repeat test with Fallback to ProcTask disabled 1555 // Repeat test with Fallback to ProcTask disabled
1621 resolver_->SetDefaultAddressFamily(ADDRESS_FAMILY_IPV4);
1622 set_fallback_to_proctask(false); 1556 set_fallback_to_proctask(false);
1623 ChangeDnsConfig(CreateValidDnsConfig()); 1557 ChangeDnsConfig(CreateValidDnsConfig());
1624 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("nx_abort", 80)->Resolve()); 1558 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("nx_abort", 80)->Resolve());
1625 // Abort all jobs here. 1559 // Abort all jobs here.
1626 CreateResolver(); 1560 CreateResolver();
1627 // Run to completion. 1561 // Run to completion.
1628 base::MessageLoop::current()->RunUntilIdle(); // Notification happens async. 1562 base::MessageLoop::current()->RunUntilIdle(); // Notification happens async.
1629 // It shouldn't crash during OnDnsTaskFailure callbacks. 1563 // It shouldn't crash during OnDnsTaskFailure callbacks.
1630 EXPECT_EQ(ERR_IO_PENDING, requests_[1]->result()); 1564 EXPECT_EQ(ERR_IO_PENDING, requests_[1]->result());
1631 } 1565 }
(...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after
1816 EXPECT_EQ(ERR_IO_PENDING, req->Resolve()); 1750 EXPECT_EQ(ERR_IO_PENDING, req->Resolve());
1817 EXPECT_EQ(OK, req->WaitForResult()); 1751 EXPECT_EQ(OK, req->WaitForResult());
1818 } 1752 }
1819 1753
1820 // Confirm that resolving "localhost" is unrestricted even if there are no 1754 // Confirm that resolving "localhost" is unrestricted even if there are no
1821 // global IPv6 address. See SystemHostResolverCall for rationale. 1755 // global IPv6 address. See SystemHostResolverCall for rationale.
1822 // Test both the DnsClient and system host resolver paths. 1756 // Test both the DnsClient and system host resolver paths.
1823 TEST_F(HostResolverImplDnsTest, DualFamilyLocalhost) { 1757 TEST_F(HostResolverImplDnsTest, DualFamilyLocalhost) {
1824 // Use regular SystemHostResolverCall! 1758 // Use regular SystemHostResolverCall!
1825 scoped_refptr<HostResolverProc> proc(new SystemHostResolverProc()); 1759 scoped_refptr<HostResolverProc> proc(new SystemHostResolverProc());
1826 resolver_.reset(new HostResolverImpl(DefaultOptions(), NULL)); 1760 resolver_.reset(new TestHostResolverImpl(DefaultOptions(), NULL, false));
1827 resolver_->set_proc_params_for_test(DefaultParams(proc.get())); 1761 resolver_->set_proc_params_for_test(DefaultParams(proc.get()));
1828 1762
1829 resolver_->SetDnsClient( 1763 resolver_->SetDnsClient(
1830 scoped_ptr<DnsClient>(new MockDnsClient(DnsConfig(), dns_rules_))); 1764 scoped_ptr<DnsClient>(new MockDnsClient(DnsConfig(), dns_rules_)));
1831 resolver_->SetDefaultAddressFamily(ADDRESS_FAMILY_IPV4);
1832 1765
1833 // Get the expected output. 1766 // Get the expected output.
1834 AddressList addrlist; 1767 AddressList addrlist;
1835 int rv = proc->Resolve("localhost", ADDRESS_FAMILY_UNSPECIFIED, 0, &addrlist, 1768 int rv = proc->Resolve("localhost", ADDRESS_FAMILY_UNSPECIFIED, 0, &addrlist,
1836 NULL); 1769 NULL);
1837 if (rv != OK) 1770 if (rv != OK)
1838 return; 1771 return;
1839 1772
1840 for (unsigned i = 0; i < addrlist.size(); ++i) 1773 for (unsigned i = 0; i < addrlist.size(); ++i)
1841 LOG(WARNING) << addrlist[i].ToString(); 1774 LOG(WARNING) << addrlist[i].ToString();
1842 1775
1843 bool saw_ipv4 = AddressListContains(addrlist, "127.0.0.1", 0); 1776 bool saw_ipv4 = AddressListContains(addrlist, "127.0.0.1", 0);
1844 bool saw_ipv6 = AddressListContains(addrlist, "::1", 0); 1777 bool saw_ipv6 = AddressListContains(addrlist, "::1", 0);
1845 if (!saw_ipv4 && !saw_ipv6) 1778 if (!saw_ipv4 && !saw_ipv6)
1846 return; 1779 return;
1847 1780
1848 HostResolver::RequestInfo info(HostPortPair("localhost", 80)); 1781 // Try without DnsClient.
1849 info.set_address_family(ADDRESS_FAMILY_UNSPECIFIED); 1782 DnsConfig config = CreateValidDnsConfig();
1850 info.set_host_resolver_flags(HOST_RESOLVER_DEFAULT_FAMILY_SET_DUE_TO_NO_IPV6); 1783 config.use_local_ipv6 = false;
1784 ChangeDnsConfig(config);
1785 HostResolver::RequestInfo info_proc(HostPortPair("localhost", 80));
1786 info_proc.set_address_family(ADDRESS_FAMILY_UNSPECIFIED);
1787 info_proc.set_host_resolver_flags(HOST_RESOLVER_SYSTEM_ONLY);
1788 Request* req = CreateRequest(info_proc, DEFAULT_PRIORITY);
1851 1789
1852 // Try without DnsClient.
1853 ChangeDnsConfig(DnsConfig());
1854 Request* req = CreateRequest(info, DEFAULT_PRIORITY);
1855 // It is resolved via getaddrinfo, so expect asynchronous result. 1790 // It is resolved via getaddrinfo, so expect asynchronous result.
1856 EXPECT_EQ(ERR_IO_PENDING, req->Resolve()); 1791 EXPECT_EQ(ERR_IO_PENDING, req->Resolve());
1857 EXPECT_EQ(OK, req->WaitForResult()); 1792 EXPECT_EQ(OK, req->WaitForResult());
1858 1793
1859 EXPECT_EQ(saw_ipv4, req->HasAddress("127.0.0.1", 80)); 1794 EXPECT_EQ(saw_ipv4, req->HasAddress("127.0.0.1", 80));
1860 EXPECT_EQ(saw_ipv6, req->HasAddress("::1", 80)); 1795 EXPECT_EQ(saw_ipv6, req->HasAddress("::1", 80));
1861 1796
1862 // Configure DnsClient with dual-host HOSTS file. 1797 // Configure DnsClient with dual-host HOSTS file.
1863 DnsConfig config = CreateValidDnsConfig(); 1798 DnsConfig config_hosts = CreateValidDnsConfig();
1864 DnsHosts hosts; 1799 DnsHosts hosts;
1865 IPAddressNumber local_ipv4, local_ipv6; 1800 IPAddressNumber local_ipv4, local_ipv6;
1866 ASSERT_TRUE(ParseIPLiteralToNumber("127.0.0.1", &local_ipv4)); 1801 ASSERT_TRUE(ParseIPLiteralToNumber("127.0.0.1", &local_ipv4));
1867 ASSERT_TRUE(ParseIPLiteralToNumber("::1", &local_ipv6)); 1802 ASSERT_TRUE(ParseIPLiteralToNumber("::1", &local_ipv6));
1868 if (saw_ipv4) 1803 if (saw_ipv4)
1869 hosts[DnsHostsKey("localhost", ADDRESS_FAMILY_IPV4)] = local_ipv4; 1804 hosts[DnsHostsKey("localhost", ADDRESS_FAMILY_IPV4)] = local_ipv4;
1870 if (saw_ipv6) 1805 if (saw_ipv6)
1871 hosts[DnsHostsKey("localhost", ADDRESS_FAMILY_IPV6)] = local_ipv6; 1806 hosts[DnsHostsKey("localhost", ADDRESS_FAMILY_IPV6)] = local_ipv6;
1872 config.hosts = hosts; 1807 config_hosts.hosts = hosts;
1873 1808
1874 ChangeDnsConfig(config); 1809 ChangeDnsConfig(config_hosts);
1875 req = CreateRequest(info, DEFAULT_PRIORITY); 1810 HostResolver::RequestInfo info_hosts(HostPortPair("localhost", 80));
1811 info_hosts.set_address_family(ADDRESS_FAMILY_UNSPECIFIED);
1812 req = CreateRequest(info_hosts, DEFAULT_PRIORITY);
1876 // Expect synchronous resolution from DnsHosts. 1813 // Expect synchronous resolution from DnsHosts.
1877 EXPECT_EQ(OK, req->Resolve()); 1814 EXPECT_EQ(OK, req->Resolve());
1878 1815
1879 EXPECT_EQ(saw_ipv4, req->HasAddress("127.0.0.1", 80)); 1816 EXPECT_EQ(saw_ipv4, req->HasAddress("127.0.0.1", 80));
1880 EXPECT_EQ(saw_ipv6, req->HasAddress("::1", 80)); 1817 EXPECT_EQ(saw_ipv6, req->HasAddress("::1", 80));
1881 } 1818 }
1882 1819
1883 // Cancel a request with a single DNS transaction active. 1820 // Cancel a request with a single DNS transaction active.
1884 TEST_F(HostResolverImplDnsTest, CancelWithOneTransactionActive) { 1821 TEST_F(HostResolverImplDnsTest, CancelWithOneTransactionActive) {
1885 resolver_->SetDefaultAddressFamily(ADDRESS_FAMILY_IPV4);
1886 ChangeDnsConfig(CreateValidDnsConfig()); 1822 ChangeDnsConfig(CreateValidDnsConfig());
1887 1823
1888 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("ok", 80)->Resolve()); 1824 EXPECT_EQ(ERR_IO_PENDING,
1825 CreateRequest("ok", 80, MEDIUM, ADDRESS_FAMILY_IPV4)->Resolve());
1889 EXPECT_EQ(1u, num_running_dispatcher_jobs()); 1826 EXPECT_EQ(1u, num_running_dispatcher_jobs());
1890 requests_[0]->Cancel(); 1827 requests_[0]->Cancel();
1891 1828
1892 // Dispatcher state checked in TearDown. 1829 // Dispatcher state checked in TearDown.
1893 } 1830 }
1894 1831
1895 // Cancel a request with a single DNS transaction active and another pending. 1832 // Cancel a request with a single DNS transaction active and another pending.
1896 TEST_F(HostResolverImplDnsTest, CancelWithOneTransactionActiveOnePending) { 1833 TEST_F(HostResolverImplDnsTest, CancelWithOneTransactionActiveOnePending) {
1897 CreateSerialResolver(); 1834 CreateSerialResolver();
1898 resolver_->SetDefaultAddressFamily(ADDRESS_FAMILY_UNSPECIFIED);
1899 ChangeDnsConfig(CreateValidDnsConfig()); 1835 ChangeDnsConfig(CreateValidDnsConfig());
1900 1836
1901 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("ok", 80)->Resolve()); 1837 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("ok", 80)->Resolve());
1902 EXPECT_EQ(1u, num_running_dispatcher_jobs()); 1838 EXPECT_EQ(1u, num_running_dispatcher_jobs());
1903 requests_[0]->Cancel(); 1839 requests_[0]->Cancel();
1904 1840
1905 // Dispatcher state checked in TearDown. 1841 // Dispatcher state checked in TearDown.
1906 } 1842 }
1907 1843
1908 // Cancel a request with two DNS transactions active. 1844 // Cancel a request with two DNS transactions active.
1909 TEST_F(HostResolverImplDnsTest, CancelWithTwoTransactionsActive) { 1845 TEST_F(HostResolverImplDnsTest, CancelWithTwoTransactionsActive) {
1910 resolver_->SetDefaultAddressFamily(ADDRESS_FAMILY_UNSPECIFIED);
1911 ChangeDnsConfig(CreateValidDnsConfig()); 1846 ChangeDnsConfig(CreateValidDnsConfig());
1912 1847
1913 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("ok", 80)->Resolve()); 1848 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("ok", 80)->Resolve());
1914 EXPECT_EQ(2u, num_running_dispatcher_jobs()); 1849 EXPECT_EQ(2u, num_running_dispatcher_jobs());
1915 requests_[0]->Cancel(); 1850 requests_[0]->Cancel();
1916 1851
1917 // Dispatcher state checked in TearDown. 1852 // Dispatcher state checked in TearDown.
1918 } 1853 }
1919 1854
1920 // Delete a resolver with some active requests and some queued requests. 1855 // Delete a resolver with some active requests and some queued requests.
1921 TEST_F(HostResolverImplDnsTest, DeleteWithActiveTransactions) { 1856 TEST_F(HostResolverImplDnsTest, DeleteWithActiveTransactions) {
1922 // At most 10 Jobs active at once. 1857 // At most 10 Jobs active at once.
1923 CreateResolverWithLimitsAndParams(10u, DefaultParams(proc_.get())); 1858 CreateResolverWithLimitsAndParams(10u, DefaultParams(proc_.get()));
1924 1859
1925 resolver_->SetDefaultAddressFamily(ADDRESS_FAMILY_UNSPECIFIED);
1926 ChangeDnsConfig(CreateValidDnsConfig()); 1860 ChangeDnsConfig(CreateValidDnsConfig());
1927 1861
1928 // First active job is an IPv4 request. 1862 // First active job is an IPv4 request.
1929 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("ok", 80, MEDIUM, 1863 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("ok", 80, MEDIUM,
1930 ADDRESS_FAMILY_IPV4)->Resolve()); 1864 ADDRESS_FAMILY_IPV4)->Resolve());
1931 1865
1932 // Add 10 more DNS lookups for different hostnames. First 4 should have two 1866 // Add 10 more DNS lookups for different hostnames. First 4 should have two
1933 // active jobs, next one has a single active job, and one pending. Others 1867 // active jobs, next one has a single active job, and one pending. Others
1934 // should all be queued. 1868 // should all be queued.
1935 for (int i = 0; i < 10; ++i) { 1869 for (int i = 0; i < 10; ++i) {
1936 EXPECT_EQ(ERR_IO_PENDING, CreateRequest( 1870 EXPECT_EQ(ERR_IO_PENDING, CreateRequest(
1937 base::StringPrintf("ok%i", i))->Resolve()); 1871 base::StringPrintf("ok%i", i))->Resolve());
1938 } 1872 }
1939 EXPECT_EQ(10u, num_running_dispatcher_jobs()); 1873 EXPECT_EQ(10u, num_running_dispatcher_jobs());
1940 1874
1941 resolver_.reset(); 1875 resolver_.reset();
1942 } 1876 }
1943 1877
1944 // Cancel a request with only the IPv6 transaction active. 1878 // Cancel a request with only the IPv6 transaction active.
1945 TEST_F(HostResolverImplDnsTest, CancelWithIPv6TransactionActive) { 1879 TEST_F(HostResolverImplDnsTest, CancelWithIPv6TransactionActive) {
1946 resolver_->SetDefaultAddressFamily(ADDRESS_FAMILY_UNSPECIFIED);
1947 ChangeDnsConfig(CreateValidDnsConfig()); 1880 ChangeDnsConfig(CreateValidDnsConfig());
1948 1881
1949 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("6slow_ok", 80)->Resolve()); 1882 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("6slow_ok", 80)->Resolve());
1950 EXPECT_EQ(2u, num_running_dispatcher_jobs()); 1883 EXPECT_EQ(2u, num_running_dispatcher_jobs());
1951 1884
1952 // The IPv4 request should complete, the IPv6 request is still pending. 1885 // The IPv4 request should complete, the IPv6 request is still pending.
1953 base::RunLoop().RunUntilIdle(); 1886 base::RunLoop().RunUntilIdle();
1954 EXPECT_EQ(1u, num_running_dispatcher_jobs()); 1887 EXPECT_EQ(1u, num_running_dispatcher_jobs());
1955 requests_[0]->Cancel(); 1888 requests_[0]->Cancel();
1956 1889
1957 // Dispatcher state checked in TearDown. 1890 // Dispatcher state checked in TearDown.
1958 } 1891 }
1959 1892
1960 // Cancel a request with only the IPv4 transaction pending. 1893 // Cancel a request with only the IPv4 transaction pending.
1961 TEST_F(HostResolverImplDnsTest, CancelWithIPv4TransactionPending) { 1894 TEST_F(HostResolverImplDnsTest, CancelWithIPv4TransactionPending) {
1962 set_fallback_to_proctask(false); 1895 set_fallback_to_proctask(false);
1963 resolver_->SetDefaultAddressFamily(ADDRESS_FAMILY_UNSPECIFIED);
1964 ChangeDnsConfig(CreateValidDnsConfig()); 1896 ChangeDnsConfig(CreateValidDnsConfig());
1965 1897
1966 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("4slow_ok", 80)->Resolve()); 1898 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("4slow_ok", 80)->Resolve());
1967 EXPECT_EQ(2u, num_running_dispatcher_jobs()); 1899 EXPECT_EQ(2u, num_running_dispatcher_jobs());
1968 1900
1969 // The IPv6 request should complete, the IPv4 request is still pending. 1901 // The IPv6 request should complete, the IPv4 request is still pending.
1970 base::RunLoop().RunUntilIdle(); 1902 base::RunLoop().RunUntilIdle();
1971 EXPECT_EQ(1u, num_running_dispatcher_jobs()); 1903 EXPECT_EQ(1u, num_running_dispatcher_jobs());
1972 1904
1973 requests_[0]->Cancel(); 1905 requests_[0]->Cancel();
1974 } 1906 }
1975 1907
1976 // Test cases where AAAA completes first. 1908 // Test cases where AAAA completes first.
1977 TEST_F(HostResolverImplDnsTest, AAAACompletesFirst) { 1909 TEST_F(HostResolverImplDnsTest, AAAACompletesFirst) {
1978 set_fallback_to_proctask(false); 1910 set_fallback_to_proctask(false);
1979 resolver_->SetDefaultAddressFamily(ADDRESS_FAMILY_UNSPECIFIED);
1980 ChangeDnsConfig(CreateValidDnsConfig()); 1911 ChangeDnsConfig(CreateValidDnsConfig());
1981 1912
1982 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("4slow_ok", 80)->Resolve()); 1913 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("4slow_ok", 80)->Resolve());
1983 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("4slow_4ok", 80)->Resolve()); 1914 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("4slow_4ok", 80)->Resolve());
1984 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("4slow_4timeout", 80)->Resolve()); 1915 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("4slow_4timeout", 80)->Resolve());
1985 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("4slow_6timeout", 80)->Resolve()); 1916 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("4slow_6timeout", 80)->Resolve());
1986 1917
1987 base::RunLoop().RunUntilIdle(); 1918 base::RunLoop().RunUntilIdle();
1988 EXPECT_FALSE(requests_[0]->completed()); 1919 EXPECT_FALSE(requests_[0]->completed());
1989 EXPECT_FALSE(requests_[1]->completed()); 1920 EXPECT_FALSE(requests_[1]->completed());
(...skipping 17 matching lines...) Expand all
2007 EXPECT_TRUE(requests_[1]->HasAddress("127.0.0.1", 80)); 1938 EXPECT_TRUE(requests_[1]->HasAddress("127.0.0.1", 80));
2008 1939
2009 EXPECT_TRUE(requests_[2]->completed()); 1940 EXPECT_TRUE(requests_[2]->completed());
2010 EXPECT_EQ(ERR_DNS_TIMED_OUT, requests_[2]->result()); 1941 EXPECT_EQ(ERR_DNS_TIMED_OUT, requests_[2]->result());
2011 } 1942 }
2012 1943
2013 // Test the case where only a single transaction slot is available. 1944 // Test the case where only a single transaction slot is available.
2014 TEST_F(HostResolverImplDnsTest, SerialResolver) { 1945 TEST_F(HostResolverImplDnsTest, SerialResolver) {
2015 CreateSerialResolver(); 1946 CreateSerialResolver();
2016 set_fallback_to_proctask(false); 1947 set_fallback_to_proctask(false);
2017 resolver_->SetDefaultAddressFamily(ADDRESS_FAMILY_UNSPECIFIED);
2018 ChangeDnsConfig(CreateValidDnsConfig()); 1948 ChangeDnsConfig(CreateValidDnsConfig());
2019 1949
2020 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("ok", 80)->Resolve()); 1950 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("ok", 80)->Resolve());
2021 EXPECT_EQ(1u, num_running_dispatcher_jobs()); 1951 EXPECT_EQ(1u, num_running_dispatcher_jobs());
2022 1952
2023 base::RunLoop().RunUntilIdle(); 1953 base::RunLoop().RunUntilIdle();
2024 EXPECT_TRUE(requests_[0]->completed()); 1954 EXPECT_TRUE(requests_[0]->completed());
2025 EXPECT_EQ(OK, requests_[0]->result()); 1955 EXPECT_EQ(OK, requests_[0]->result());
2026 EXPECT_EQ(2u, requests_[0]->NumberOfAddresses()); 1956 EXPECT_EQ(2u, requests_[0]->NumberOfAddresses());
2027 EXPECT_TRUE(requests_[0]->HasAddress("127.0.0.1", 80)); 1957 EXPECT_TRUE(requests_[0]->HasAddress("127.0.0.1", 80));
2028 EXPECT_TRUE(requests_[0]->HasAddress("::1", 80)); 1958 EXPECT_TRUE(requests_[0]->HasAddress("::1", 80));
2029 } 1959 }
2030 1960
2031 // Test the case where the AAAA query is started when another transaction 1961 // Test the case where the AAAA query is started when another transaction
2032 // completes. 1962 // completes.
2033 TEST_F(HostResolverImplDnsTest, AAAAStartsAfterOtherJobFinishes) { 1963 TEST_F(HostResolverImplDnsTest, AAAAStartsAfterOtherJobFinishes) {
2034 CreateResolverWithLimitsAndParams(2u, DefaultParams(proc_.get())); 1964 CreateResolverWithLimitsAndParams(2u, DefaultParams(proc_.get()));
2035 set_fallback_to_proctask(false); 1965 set_fallback_to_proctask(false);
2036 resolver_->SetDefaultAddressFamily(ADDRESS_FAMILY_UNSPECIFIED);
2037 ChangeDnsConfig(CreateValidDnsConfig()); 1966 ChangeDnsConfig(CreateValidDnsConfig());
2038 1967
2039 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("ok", 80, MEDIUM, 1968 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("ok", 80, MEDIUM,
2040 ADDRESS_FAMILY_IPV4)->Resolve()); 1969 ADDRESS_FAMILY_IPV4)->Resolve());
2041 EXPECT_EQ(ERR_IO_PENDING, 1970 EXPECT_EQ(ERR_IO_PENDING,
2042 CreateRequest("4slow_ok", 80, MEDIUM)->Resolve()); 1971 CreateRequest("4slow_ok", 80, MEDIUM)->Resolve());
2043 // An IPv4 request should have been started pending for each job. 1972 // An IPv4 request should have been started pending for each job.
2044 EXPECT_EQ(2u, num_running_dispatcher_jobs()); 1973 EXPECT_EQ(2u, num_running_dispatcher_jobs());
2045 1974
2046 // Request 0's IPv4 request should complete, starting Request 1's IPv6 1975 // Request 0's IPv4 request should complete, starting Request 1's IPv6
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
2085 } 2014 }
2086 2015
2087 // Tests getting a new invalid DnsConfig while there are active DnsTasks. 2016 // Tests getting a new invalid DnsConfig while there are active DnsTasks.
2088 TEST_F(HostResolverImplDnsTest, InvalidDnsConfigWithPendingRequests) { 2017 TEST_F(HostResolverImplDnsTest, InvalidDnsConfigWithPendingRequests) {
2089 // At most 3 jobs active at once. This number is important, since we want to 2018 // At most 3 jobs active at once. This number is important, since we want to
2090 // make sure that aborting the first HostResolverImpl::Job does not trigger 2019 // make sure that aborting the first HostResolverImpl::Job does not trigger
2091 // another DnsTransaction on the second Job when it releases its second 2020 // another DnsTransaction on the second Job when it releases its second
2092 // prioritized dispatcher slot. 2021 // prioritized dispatcher slot.
2093 CreateResolverWithLimitsAndParams(3u, DefaultParams(proc_.get())); 2022 CreateResolverWithLimitsAndParams(3u, DefaultParams(proc_.get()));
2094 2023
2095 resolver_->SetDefaultAddressFamily(ADDRESS_FAMILY_UNSPECIFIED);
2096 ChangeDnsConfig(CreateValidDnsConfig()); 2024 ChangeDnsConfig(CreateValidDnsConfig());
2097 2025
2098 proc_->AddRuleForAllFamilies("slow_nx1", "192.168.0.1"); 2026 proc_->AddRuleForAllFamilies("slow_nx1", "192.168.0.1");
2099 proc_->AddRuleForAllFamilies("slow_nx2", "192.168.0.2"); 2027 proc_->AddRuleForAllFamilies("slow_nx2", "192.168.0.2");
2100 proc_->AddRuleForAllFamilies("ok", "192.168.0.3"); 2028 proc_->AddRuleForAllFamilies("ok", "192.168.0.3");
2101 2029
2102 // First active job gets two slots. 2030 // First active job gets two slots.
2103 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("slow_nx1")->Resolve()); 2031 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("slow_nx1")->Resolve());
2104 // Next job gets one slot, and waits on another. 2032 // Next job gets one slot, and waits on another.
2105 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("slow_nx2")->Resolve()); 2033 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("slow_nx2")->Resolve());
(...skipping 20 matching lines...) Expand all
2126 TEST_F(HostResolverImplDnsTest, 2054 TEST_F(HostResolverImplDnsTest,
2127 AutomaticallyDisableDnsClientWithPendingRequests) { 2055 AutomaticallyDisableDnsClientWithPendingRequests) {
2128 // Trying different limits is important for this test: Different limits 2056 // Trying different limits is important for this test: Different limits
2129 // result in different behavior when aborting in-progress DnsTasks. Having 2057 // result in different behavior when aborting in-progress DnsTasks. Having
2130 // a DnsTask that has one job active and one in the queue when another job 2058 // a DnsTask that has one job active and one in the queue when another job
2131 // occupying two slots has its DnsTask aborted is the case most likely to run 2059 // occupying two slots has its DnsTask aborted is the case most likely to run
2132 // into problems. 2060 // into problems.
2133 for (size_t limit = 1u; limit < 6u; ++limit) { 2061 for (size_t limit = 1u; limit < 6u; ++limit) {
2134 CreateResolverWithLimitsAndParams(limit, DefaultParams(proc_.get())); 2062 CreateResolverWithLimitsAndParams(limit, DefaultParams(proc_.get()));
2135 2063
2136 resolver_->SetDefaultAddressFamily(ADDRESS_FAMILY_UNSPECIFIED);
2137 ChangeDnsConfig(CreateValidDnsConfig()); 2064 ChangeDnsConfig(CreateValidDnsConfig());
2138 2065
2139 // Queue up enough failures to disable DnsTasks. These will all fall back 2066 // Queue up enough failures to disable DnsTasks. These will all fall back
2140 // to ProcTasks, and succeed there. 2067 // to ProcTasks, and succeed there.
2141 for (unsigned i = 0u; i < maximum_dns_failures(); ++i) { 2068 for (unsigned i = 0u; i < maximum_dns_failures(); ++i) {
2142 std::string host = base::StringPrintf("nx%u", i); 2069 std::string host = base::StringPrintf("nx%u", i);
2143 proc_->AddRuleForAllFamilies(host, "192.168.0.1"); 2070 proc_->AddRuleForAllFamilies(host, "192.168.0.1");
2144 EXPECT_EQ(ERR_IO_PENDING, CreateRequest(host)->Resolve()); 2071 EXPECT_EQ(ERR_IO_PENDING, CreateRequest(host)->Resolve());
2145 } 2072 }
2146 2073
(...skipping 26 matching lines...) Expand all
2173 } 2100 }
2174 2101
2175 // Tests a call to SetDnsClient while there are active DnsTasks. 2102 // Tests a call to SetDnsClient while there are active DnsTasks.
2176 TEST_F(HostResolverImplDnsTest, ManuallyDisableDnsClientWithPendingRequests) { 2103 TEST_F(HostResolverImplDnsTest, ManuallyDisableDnsClientWithPendingRequests) {
2177 // At most 3 jobs active at once. This number is important, since we want to 2104 // At most 3 jobs active at once. This number is important, since we want to
2178 // make sure that aborting the first HostResolverImpl::Job does not trigger 2105 // make sure that aborting the first HostResolverImpl::Job does not trigger
2179 // another DnsTransaction on the second Job when it releases its second 2106 // another DnsTransaction on the second Job when it releases its second
2180 // prioritized dispatcher slot. 2107 // prioritized dispatcher slot.
2181 CreateResolverWithLimitsAndParams(3u, DefaultParams(proc_.get())); 2108 CreateResolverWithLimitsAndParams(3u, DefaultParams(proc_.get()));
2182 2109
2183 resolver_->SetDefaultAddressFamily(ADDRESS_FAMILY_UNSPECIFIED);
2184 ChangeDnsConfig(CreateValidDnsConfig()); 2110 ChangeDnsConfig(CreateValidDnsConfig());
2185 2111
2186 proc_->AddRuleForAllFamilies("slow_ok1", "192.168.0.1"); 2112 proc_->AddRuleForAllFamilies("slow_ok1", "192.168.0.1");
2187 proc_->AddRuleForAllFamilies("slow_ok2", "192.168.0.2"); 2113 proc_->AddRuleForAllFamilies("slow_ok2", "192.168.0.2");
2188 proc_->AddRuleForAllFamilies("ok", "192.168.0.3"); 2114 proc_->AddRuleForAllFamilies("ok", "192.168.0.3");
2189 2115
2190 // First active job gets two slots. 2116 // First active job gets two slots.
2191 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("slow_ok1")->Resolve()); 2117 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("slow_ok1")->Resolve());
2192 // Next job gets one slot, and waits on another. 2118 // Next job gets one slot, and waits on another.
2193 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("slow_ok2")->Resolve()); 2119 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("slow_ok2")->Resolve());
(...skipping 12 matching lines...) Expand all
2206 2132
2207 EXPECT_EQ(OK, requests_[0]->WaitForResult()); 2133 EXPECT_EQ(OK, requests_[0]->WaitForResult());
2208 EXPECT_TRUE(requests_[0]->HasOneAddress("192.168.0.1", 80)); 2134 EXPECT_TRUE(requests_[0]->HasOneAddress("192.168.0.1", 80));
2209 EXPECT_EQ(OK, requests_[1]->WaitForResult()); 2135 EXPECT_EQ(OK, requests_[1]->WaitForResult());
2210 EXPECT_TRUE(requests_[1]->HasOneAddress("192.168.0.2", 80)); 2136 EXPECT_TRUE(requests_[1]->HasOneAddress("192.168.0.2", 80));
2211 EXPECT_EQ(OK, requests_[2]->WaitForResult()); 2137 EXPECT_EQ(OK, requests_[2]->WaitForResult());
2212 EXPECT_TRUE(requests_[2]->HasOneAddress("192.168.0.3", 80)); 2138 EXPECT_TRUE(requests_[2]->HasOneAddress("192.168.0.3", 80));
2213 } 2139 }
2214 2140
2215 } // namespace net 2141 } // namespace net
OLDNEW
« no previous file with comments | « net/dns/host_resolver_impl.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698