| 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/base/host_resolver_impl.h" | 5 #include "net/base/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 417 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 428 void DeleteResolver() { test->resolver_.reset(); } | 428 void DeleteResolver() { test->resolver_.reset(); } |
| 429 | 429 |
| 430 HostResolverImplTest* test; | 430 HostResolverImplTest* test; |
| 431 }; | 431 }; |
| 432 | 432 |
| 433 void CreateResolver() { | 433 void CreateResolver() { |
| 434 resolver_.reset(new HostResolverImpl( | 434 resolver_.reset(new HostResolverImpl( |
| 435 HostCache::CreateDefaultCache(), | 435 HostCache::CreateDefaultCache(), |
| 436 DefaultLimits(), | 436 DefaultLimits(), |
| 437 DefaultParams(proc_), | 437 DefaultParams(proc_), |
| 438 scoped_ptr<DnsConfigService>(NULL), | |
| 439 scoped_ptr<DnsClient>(NULL), | 438 scoped_ptr<DnsClient>(NULL), |
| 440 NULL)); | 439 NULL)); |
| 441 } | 440 } |
| 442 | 441 |
| 443 // This HostResolverImpl will only allow 1 outstanding resolve at a time and | 442 // This HostResolverImpl will only allow 1 outstanding resolve at a time and |
| 444 // perform no retries. | 443 // perform no retries. |
| 445 void CreateSerialResolver() { | 444 void CreateSerialResolver() { |
| 446 HostResolverImpl::ProcTaskParams params = DefaultParams(proc_); | 445 HostResolverImpl::ProcTaskParams params = DefaultParams(proc_); |
| 447 params.max_retry_attempts = 0u; | 446 params.max_retry_attempts = 0u; |
| 448 PrioritizedDispatcher::Limits limits(NUM_PRIORITIES, 1); | 447 PrioritizedDispatcher::Limits limits(NUM_PRIORITIES, 1); |
| 449 resolver_.reset(new HostResolverImpl( | 448 resolver_.reset(new HostResolverImpl( |
| 450 HostCache::CreateDefaultCache(), | 449 HostCache::CreateDefaultCache(), |
| 451 limits, | 450 limits, |
| 452 params, | 451 params, |
| 453 scoped_ptr<DnsConfigService>(NULL), | |
| 454 scoped_ptr<DnsClient>(NULL), | 452 scoped_ptr<DnsClient>(NULL), |
| 455 NULL)); | 453 NULL)); |
| 456 } | 454 } |
| 457 | 455 |
| 458 // The Request will not be made until a call to |Resolve()|, and the Job will | 456 // The Request will not be made until a call to |Resolve()|, and the Job will |
| 459 // not start until released by |proc_->SignalXXX|. | 457 // not start until released by |proc_->SignalXXX|. |
| 460 Request* CreateRequest(const HostResolver::RequestInfo& info) { | 458 Request* CreateRequest(const HostResolver::RequestInfo& info) { |
| 461 Request* req = new Request(info, requests_.size(), resolver_.get(), | 459 Request* req = new Request(info, requests_.size(), resolver_.get(), |
| 462 handler_.get()); | 460 handler_.get()); |
| 463 requests_.push_back(req); | 461 requests_.push_back(req); |
| (...skipping 292 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 756 } | 754 } |
| 757 } | 755 } |
| 758 }; | 756 }; |
| 759 set_handler(new MyHandler()); | 757 set_handler(new MyHandler()); |
| 760 | 758 |
| 761 // Turn off caching for this host resolver. | 759 // Turn off caching for this host resolver. |
| 762 resolver_.reset(new HostResolverImpl( | 760 resolver_.reset(new HostResolverImpl( |
| 763 NULL, | 761 NULL, |
| 764 DefaultLimits(), | 762 DefaultLimits(), |
| 765 DefaultParams(proc_), | 763 DefaultParams(proc_), |
| 766 scoped_ptr<DnsConfigService>(NULL), | |
| 767 scoped_ptr<DnsClient>(NULL), | 764 scoped_ptr<DnsClient>(NULL), |
| 768 NULL)); | 765 NULL)); |
| 769 | 766 |
| 770 for (size_t i = 0; i < 4; ++i) { | 767 for (size_t i = 0; i < 4; ++i) { |
| 771 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("a", 80 + i)->Resolve()) << i; | 768 EXPECT_EQ(ERR_IO_PENDING, CreateRequest("a", 80 + i)->Resolve()) << i; |
| 772 } | 769 } |
| 773 | 770 |
| 774 proc_->SignalMultiple(2u); // One for "a". One for the second "a". | 771 proc_->SignalMultiple(2u); // One for "a". One for the second "a". |
| 775 | 772 |
| 776 EXPECT_EQ(OK, requests_[0]->WaitForResult()); | 773 EXPECT_EQ(OK, requests_[0]->WaitForResult()); |
| (...skipping 417 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1194 | 1191 |
| 1195 // Specify smaller interval for unresponsive_delay_ for HostResolverImpl so | 1192 // Specify smaller interval for unresponsive_delay_ for HostResolverImpl so |
| 1196 // that unit test runs faster. For example, this test finishes in 1.5 secs | 1193 // that unit test runs faster. For example, this test finishes in 1.5 secs |
| 1197 // (500ms * 3). | 1194 // (500ms * 3). |
| 1198 params.unresponsive_delay = base::TimeDelta::FromMilliseconds(500); | 1195 params.unresponsive_delay = base::TimeDelta::FromMilliseconds(500); |
| 1199 | 1196 |
| 1200 resolver_.reset( | 1197 resolver_.reset( |
| 1201 new HostResolverImpl(HostCache::CreateDefaultCache(), | 1198 new HostResolverImpl(HostCache::CreateDefaultCache(), |
| 1202 DefaultLimits(), | 1199 DefaultLimits(), |
| 1203 params, | 1200 params, |
| 1204 scoped_ptr<DnsConfigService>(NULL), | |
| 1205 scoped_ptr<DnsClient>(NULL), | 1201 scoped_ptr<DnsClient>(NULL), |
| 1206 NULL)); | 1202 NULL)); |
| 1207 | 1203 |
| 1208 // Resolve "host1". | 1204 // Resolve "host1". |
| 1209 HostResolver::RequestInfo info(HostPortPair("host1", 70)); | 1205 HostResolver::RequestInfo info(HostPortPair("host1", 70)); |
| 1210 Request* req = CreateRequest(info); | 1206 Request* req = CreateRequest(info); |
| 1211 EXPECT_EQ(ERR_IO_PENDING, req->Resolve()); | 1207 EXPECT_EQ(ERR_IO_PENDING, req->Resolve()); |
| 1212 | 1208 |
| 1213 // Resolve returns -4 to indicate that 3rd attempt has resolved the host. | 1209 // Resolve returns -4 to indicate that 3rd attempt has resolved the host. |
| 1214 EXPECT_EQ(-4, req->WaitForResult()); | 1210 EXPECT_EQ(-4, req->WaitForResult()); |
| (...skipping 30 matching lines...) Expand all Loading... |
| 1245 AddDnsRule("4ok", dns_protocol::kTypeA, MockDnsClientRule::OK); | 1241 AddDnsRule("4ok", dns_protocol::kTypeA, MockDnsClientRule::OK); |
| 1246 AddDnsRule("4ok", dns_protocol::kTypeAAAA, MockDnsClientRule::EMPTY); | 1242 AddDnsRule("4ok", dns_protocol::kTypeAAAA, MockDnsClientRule::EMPTY); |
| 1247 AddDnsRule("6ok", dns_protocol::kTypeA, MockDnsClientRule::EMPTY); | 1243 AddDnsRule("6ok", dns_protocol::kTypeA, MockDnsClientRule::EMPTY); |
| 1248 AddDnsRule("6ok", dns_protocol::kTypeAAAA, MockDnsClientRule::OK); | 1244 AddDnsRule("6ok", dns_protocol::kTypeAAAA, MockDnsClientRule::OK); |
| 1249 AddDnsRule("4nx", dns_protocol::kTypeA, MockDnsClientRule::OK); | 1245 AddDnsRule("4nx", dns_protocol::kTypeA, MockDnsClientRule::OK); |
| 1250 AddDnsRule("4nx", dns_protocol::kTypeAAAA, MockDnsClientRule::FAIL_ASYNC); | 1246 AddDnsRule("4nx", dns_protocol::kTypeAAAA, MockDnsClientRule::FAIL_ASYNC); |
| 1251 CreateResolver(); | 1247 CreateResolver(); |
| 1252 } | 1248 } |
| 1253 | 1249 |
| 1254 void CreateResolver() { | 1250 void CreateResolver() { |
| 1255 config_service_ = new MockDnsConfigService(); | |
| 1256 resolver_.reset(new HostResolverImpl( | 1251 resolver_.reset(new HostResolverImpl( |
| 1257 HostCache::CreateDefaultCache(), | 1252 HostCache::CreateDefaultCache(), |
| 1258 DefaultLimits(), | 1253 DefaultLimits(), |
| 1259 DefaultParams(proc_), | 1254 DefaultParams(proc_), |
| 1260 scoped_ptr<DnsConfigService>(config_service_), | |
| 1261 CreateMockDnsClient(DnsConfig(), dns_rules_), | 1255 CreateMockDnsClient(DnsConfig(), dns_rules_), |
| 1262 NULL)); | 1256 NULL)); |
| 1263 } | 1257 } |
| 1264 | 1258 |
| 1265 // Adds a rule to |dns_rules_|. Must be followed by |CreateResolver| to apply. | 1259 // Adds a rule to |dns_rules_|. Must be followed by |CreateResolver| to apply. |
| 1266 void AddDnsRule(const std::string& prefix, | 1260 void AddDnsRule(const std::string& prefix, |
| 1267 uint16 qtype, | 1261 uint16 qtype, |
| 1268 MockDnsClientRule::Result result) { | 1262 MockDnsClientRule::Result result) { |
| 1269 MockDnsClientRule rule = { prefix, qtype, result }; | 1263 MockDnsClientRule rule = { prefix, qtype, result }; |
| 1270 dns_rules_.push_back(rule); | 1264 dns_rules_.push_back(rule); |
| 1271 } | 1265 } |
| 1272 | 1266 |
| 1273 void ChangeDnsConfig(const DnsConfig& config) { | 1267 void ChangeDnsConfig(const DnsConfig& config) { |
| 1274 config_service_->ChangeConfig(config); | 1268 NetworkChangeNotifier::SetDnsConfig(config); |
| 1275 config_service_->ChangeHosts(config.hosts); | 1269 // Notification is delivered asynchronously. |
| 1270 MessageLoop::current()->RunAllPending(); |
| 1276 } | 1271 } |
| 1277 | 1272 |
| 1278 MockDnsClientRuleList dns_rules_; | 1273 MockDnsClientRuleList dns_rules_; |
| 1279 // Owned by |resolver_|. | |
| 1280 MockDnsConfigService* config_service_; | |
| 1281 }; | 1274 }; |
| 1282 | 1275 |
| 1283 // TODO(szym): Test AbortAllInProgressJobs due to DnsConfig change. | 1276 // TODO(szym): Test AbortAllInProgressJobs due to DnsConfig change. |
| 1284 | 1277 |
| 1285 // TODO(cbentzel): Test a mix of requests with different HostResolverFlags. | 1278 // TODO(cbentzel): Test a mix of requests with different HostResolverFlags. |
| 1286 | 1279 |
| 1287 // Test successful and fallback resolutions in HostResolverImpl::DnsTask. | 1280 // Test successful and fallback resolutions in HostResolverImpl::DnsTask. |
| 1288 TEST_F(HostResolverImplDnsTest, DnsTask) { | 1281 TEST_F(HostResolverImplDnsTest, DnsTask) { |
| 1289 resolver_->SetDefaultAddressFamily(ADDRESS_FAMILY_IPV4); | 1282 resolver_->SetDefaultAddressFamily(ADDRESS_FAMILY_IPV4); |
| 1290 | 1283 |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1345 EXPECT_EQ(1u, requests_[1]->NumberOfAddresses()); | 1338 EXPECT_EQ(1u, requests_[1]->NumberOfAddresses()); |
| 1346 EXPECT_TRUE(requests_[1]->HasAddress("127.0.0.1", 80)); | 1339 EXPECT_TRUE(requests_[1]->HasAddress("127.0.0.1", 80)); |
| 1347 EXPECT_EQ(1u, requests_[2]->NumberOfAddresses()); | 1340 EXPECT_EQ(1u, requests_[2]->NumberOfAddresses()); |
| 1348 EXPECT_TRUE(requests_[2]->HasAddress("::1", 80)); | 1341 EXPECT_TRUE(requests_[2]->HasAddress("::1", 80)); |
| 1349 EXPECT_EQ(1u, requests_[3]->NumberOfAddresses()); | 1342 EXPECT_EQ(1u, requests_[3]->NumberOfAddresses()); |
| 1350 EXPECT_TRUE(requests_[3]->HasAddress("192.168.1.101", 80)); | 1343 EXPECT_TRUE(requests_[3]->HasAddress("192.168.1.101", 80)); |
| 1351 } | 1344 } |
| 1352 | 1345 |
| 1353 TEST_F(HostResolverImplDnsTest, ServeFromHosts) { | 1346 TEST_F(HostResolverImplDnsTest, ServeFromHosts) { |
| 1354 // Initially, use empty HOSTS file. | 1347 // Initially, use empty HOSTS file. |
| 1355 ChangeDnsConfig(CreateValidDnsConfig()); | 1348 DnsConfig config = CreateValidDnsConfig(); |
| 1349 ChangeDnsConfig(config); |
| 1356 | 1350 |
| 1357 proc_->AddRuleForAllFamilies("", ""); // Default to failures. | 1351 proc_->AddRuleForAllFamilies("", ""); // Default to failures. |
| 1358 proc_->SignalMultiple(1u); // For the first request which misses. | 1352 proc_->SignalMultiple(1u); // For the first request which misses. |
| 1359 | 1353 |
| 1360 Request* req0 = CreateRequest("er_ipv4", 80); | 1354 Request* req0 = CreateRequest("er_ipv4", 80); |
| 1361 EXPECT_EQ(ERR_IO_PENDING, req0->Resolve()); | 1355 EXPECT_EQ(ERR_IO_PENDING, req0->Resolve()); |
| 1362 EXPECT_EQ(ERR_NAME_NOT_RESOLVED, req0->WaitForResult()); | 1356 EXPECT_EQ(ERR_NAME_NOT_RESOLVED, req0->WaitForResult()); |
| 1363 | 1357 |
| 1364 IPAddressNumber local_ipv4, local_ipv6; | 1358 IPAddressNumber local_ipv4, local_ipv6; |
| 1365 ASSERT_TRUE(ParseIPLiteralToNumber("127.0.0.1", &local_ipv4)); | 1359 ASSERT_TRUE(ParseIPLiteralToNumber("127.0.0.1", &local_ipv4)); |
| 1366 ASSERT_TRUE(ParseIPLiteralToNumber("::1", &local_ipv6)); | 1360 ASSERT_TRUE(ParseIPLiteralToNumber("::1", &local_ipv6)); |
| 1367 | 1361 |
| 1368 DnsHosts hosts; | 1362 DnsHosts hosts; |
| 1369 hosts[DnsHostsKey("er_ipv4", ADDRESS_FAMILY_IPV4)] = local_ipv4; | 1363 hosts[DnsHostsKey("er_ipv4", ADDRESS_FAMILY_IPV4)] = local_ipv4; |
| 1370 hosts[DnsHostsKey("er_ipv6", ADDRESS_FAMILY_IPV6)] = local_ipv6; | 1364 hosts[DnsHostsKey("er_ipv6", ADDRESS_FAMILY_IPV6)] = local_ipv6; |
| 1371 hosts[DnsHostsKey("er_both", ADDRESS_FAMILY_IPV4)] = local_ipv4; | 1365 hosts[DnsHostsKey("er_both", ADDRESS_FAMILY_IPV4)] = local_ipv4; |
| 1372 hosts[DnsHostsKey("er_both", ADDRESS_FAMILY_IPV6)] = local_ipv6; | 1366 hosts[DnsHostsKey("er_both", ADDRESS_FAMILY_IPV6)] = local_ipv6; |
| 1373 | 1367 |
| 1374 // Update HOSTS file. | 1368 // Update HOSTS file. |
| 1375 config_service_->ChangeHosts(hosts); | 1369 config.hosts = hosts; |
| 1370 ChangeDnsConfig(config); |
| 1376 | 1371 |
| 1377 Request* req1 = CreateRequest("er_ipv4", 80); | 1372 Request* req1 = CreateRequest("er_ipv4", 80); |
| 1378 EXPECT_EQ(OK, req1->Resolve()); | 1373 EXPECT_EQ(OK, req1->Resolve()); |
| 1379 EXPECT_TRUE(req1->HasOneAddress("127.0.0.1", 80)); | 1374 EXPECT_TRUE(req1->HasOneAddress("127.0.0.1", 80)); |
| 1380 | 1375 |
| 1381 Request* req2 = CreateRequest("er_ipv6", 80); | 1376 Request* req2 = CreateRequest("er_ipv6", 80); |
| 1382 EXPECT_EQ(OK, req2->Resolve()); | 1377 EXPECT_EQ(OK, req2->Resolve()); |
| 1383 EXPECT_TRUE(req2->HasOneAddress("::1", 80)); | 1378 EXPECT_TRUE(req2->HasOneAddress("::1", 80)); |
| 1384 | 1379 |
| 1385 Request* req3 = CreateRequest("er_both", 80); | 1380 Request* req3 = CreateRequest("er_both", 80); |
| (...skipping 30 matching lines...) Expand all Loading... |
| 1416 proc_->SignalMultiple(requests_.size()); | 1411 proc_->SignalMultiple(requests_.size()); |
| 1417 | 1412 |
| 1418 for (size_t i = 0; i < 2; ++i) | 1413 for (size_t i = 0; i < 2; ++i) |
| 1419 EXPECT_EQ(ERR_NAME_NOT_RESOLVED, requests_[i]->WaitForResult()) << i; | 1414 EXPECT_EQ(ERR_NAME_NOT_RESOLVED, requests_[i]->WaitForResult()) << i; |
| 1420 | 1415 |
| 1421 for (size_t i = 2; i < requests_.size(); ++i) | 1416 for (size_t i = 2; i < requests_.size(); ++i) |
| 1422 EXPECT_EQ(OK, requests_[i]->WaitForResult()) << i; | 1417 EXPECT_EQ(OK, requests_[i]->WaitForResult()) << i; |
| 1423 } | 1418 } |
| 1424 | 1419 |
| 1425 } // namespace net | 1420 } // namespace net |
| OLD | NEW |