diff options
-rw-r--r-- | net/base/mock_host_resolver.cc | 32 | ||||
-rw-r--r-- | net/base/mock_host_resolver.h | 5 | ||||
-rw-r--r-- | net/proxy/proxy_resolver_js_bindings.cc | 10 | ||||
-rw-r--r-- | net/proxy/proxy_resolver_js_bindings_unittest.cc | 55 |
4 files changed, 87 insertions, 15 deletions
diff --git a/net/base/mock_host_resolver.cc b/net/base/mock_host_resolver.cc index c6d31e9..4a723a6 100644 --- a/net/base/mock_host_resolver.cc +++ b/net/base/mock_host_resolver.cc @@ -110,15 +110,18 @@ struct RuleBasedHostResolverProc::Rule { ResolverType resolver_type; std::string host_pattern; + AddressFamily address_family; std::string replacement; int latency_ms; // In milliseconds. Rule(ResolverType resolver_type, const std::string& host_pattern, + AddressFamily address_family, const std::string& replacement, int latency_ms) : resolver_type(resolver_type), host_pattern(host_pattern), + address_family(address_family), replacement(replacement), latency_ms(latency_ms) {} }; @@ -132,14 +135,24 @@ RuleBasedHostResolverProc::~RuleBasedHostResolverProc() { void RuleBasedHostResolverProc::AddRule(const std::string& host_pattern, const std::string& replacement) { + AddRuleForFamily(host_pattern, ADDRESS_FAMILY_UNSPECIFIED, replacement); +} + +void RuleBasedHostResolverProc::AddRuleForFamily( + const std::string& host_pattern, + AddressFamily address_family, + const std::string& replacement) { DCHECK(!replacement.empty()); - Rule rule(Rule::kResolverTypeSystem, host_pattern, replacement, 0); + Rule rule(Rule::kResolverTypeSystem, host_pattern, + address_family, replacement, 0); rules_.push_back(rule); } + void RuleBasedHostResolverProc::AddIPv6Rule(const std::string& host_pattern, const std::string& ipv6_literal) { - Rule rule(Rule::kResolverTypeIPV6Literal, host_pattern, ipv6_literal, 0); + Rule rule(Rule::kResolverTypeIPV6Literal, host_pattern, + ADDRESS_FAMILY_UNSPECIFIED, ipv6_literal, 0); rules_.push_back(rule); } @@ -148,19 +161,22 @@ void RuleBasedHostResolverProc::AddRuleWithLatency( const std::string& replacement, int latency_ms) { DCHECK(!replacement.empty()); - Rule rule(Rule::kResolverTypeSystem, host_pattern, replacement, latency_ms); + Rule rule(Rule::kResolverTypeSystem, host_pattern, + ADDRESS_FAMILY_UNSPECIFIED, replacement, latency_ms); rules_.push_back(rule); } void RuleBasedHostResolverProc::AllowDirectLookup( const std::string& host_pattern) { - Rule rule(Rule::kResolverTypeSystem, host_pattern, "", 0); + Rule rule(Rule::kResolverTypeSystem, host_pattern, + ADDRESS_FAMILY_UNSPECIFIED, "", 0); rules_.push_back(rule); } void RuleBasedHostResolverProc::AddSimulatedFailure( const std::string& host_pattern) { - Rule rule(Rule::kResolverTypeFail, host_pattern, "", 0); + Rule rule(Rule::kResolverTypeFail, host_pattern, + ADDRESS_FAMILY_UNSPECIFIED, "", 0); rules_.push_back(rule); } @@ -169,7 +185,11 @@ int RuleBasedHostResolverProc::Resolve(const std::string& host, AddressList* addrlist) { RuleList::iterator r; for (r = rules_.begin(); r != rules_.end(); ++r) { - if (MatchPattern(host, r->host_pattern)) { + bool matches_address_family = + r->address_family == ADDRESS_FAMILY_UNSPECIFIED || + r->address_family == address_family; + + if (matches_address_family && MatchPattern(host, r->host_pattern)) { if (r->latency_ms != 0) PlatformThread::Sleep(r->latency_ms); diff --git a/net/base/mock_host_resolver.h b/net/base/mock_host_resolver.h index de91576..96788cc 100644 --- a/net/base/mock_host_resolver.h +++ b/net/base/mock_host_resolver.h @@ -102,6 +102,11 @@ class RuleBasedHostResolverProc : public HostResolverProc { void AddRule(const std::string& host_pattern, const std::string& replacement); + // Same as AddRule(), but further restricts to |address_family|. + void AddRuleForFamily(const std::string& host_pattern, + AddressFamily address_family, + const std::string& replacement); + // Same as AddRule(), but the replacement is expected to be an IPV6 literal. // You should use this in place of AddRule(), since the system's host resolver // may not support IPv6 literals on all systems. Whereas this variant diff --git a/net/proxy/proxy_resolver_js_bindings.cc b/net/proxy/proxy_resolver_js_bindings.cc index 94d6448..cbeba08 100644 --- a/net/proxy/proxy_resolver_js_bindings.cc +++ b/net/proxy/proxy_resolver_js_bindings.cc @@ -32,9 +32,12 @@ class SyncHostResolverBridge } // Run the resolve on host_resolver_loop, and wait for result. - int Resolve(const std::string& hostname, net::AddressList* addresses) { + int Resolve(const std::string& hostname, + AddressFamily address_family, + net::AddressList* addresses) { // Port number doesn't matter. HostResolver::RequestInfo info(hostname, 80); + info.set_address_family(address_family); // Hack for tests -- run synchronously on current thread. if (!host_resolver_loop_) @@ -107,8 +110,11 @@ class DefaultJSBindings : public ProxyResolverJSBindings { return std::string(); // Do a sync resolve of the hostname. + // Disable IPv6 results, see http://crbug.com/24641 for motivation. net::AddressList address_list; - int result = host_resolver_->Resolve(host, &address_list); + int result = host_resolver_->Resolve(host, + ADDRESS_FAMILY_IPV4_ONLY, + &address_list); if (result != OK) return std::string(); // Failed. diff --git a/net/proxy/proxy_resolver_js_bindings_unittest.cc b/net/proxy/proxy_resolver_js_bindings_unittest.cc index 414ab77..4a8c5d2 100644 --- a/net/proxy/proxy_resolver_js_bindings_unittest.cc +++ b/net/proxy/proxy_resolver_js_bindings_unittest.cc @@ -5,17 +5,19 @@ #include "base/scoped_ptr.h" #include "net/base/mock_host_resolver.h" #include "net/base/net_errors.h" +#include "net/base/net_util.h" #include "net/proxy/proxy_resolver_js_bindings.h" #include "testing/gtest/include/gtest/gtest.h" +namespace net { namespace { TEST(ProxyResolverJSBindingsTest, DnsResolve) { - scoped_refptr<net::MockHostResolver> host_resolver(new net::MockHostResolver); + scoped_refptr<MockHostResolver> host_resolver(new MockHostResolver); // Get a hold of a DefaultJSBindings* (it is a hidden impl class). - scoped_ptr<net::ProxyResolverJSBindings> bindings( - net::ProxyResolverJSBindings::CreateDefault(host_resolver, NULL)); + scoped_ptr<ProxyResolverJSBindings> bindings( + ProxyResolverJSBindings::CreateDefault(host_resolver, NULL)); // Empty string is not considered a valid host (even though on some systems // requesting this will resolve to localhost). @@ -33,11 +35,11 @@ TEST(ProxyResolverJSBindingsTest, DnsResolve) { // won't work on all systems. } -TEST(ProxyResolverV8DefaultBindingsTest, MyIpAddress) { +TEST(ProxyResolverJSBindingsTest, MyIpAddress) { // Get a hold of a DefaultJSBindings* (it is a hidden impl class). - scoped_ptr<net::ProxyResolverJSBindings> bindings( - net::ProxyResolverJSBindings::CreateDefault( - new net::MockHostResolver, NULL)); + scoped_ptr<ProxyResolverJSBindings> bindings( + ProxyResolverJSBindings::CreateDefault( + new MockHostResolver, NULL)); // Our IP address is always going to be 127.0.0.1, since we are using a // mock host resolver. @@ -46,4 +48,43 @@ TEST(ProxyResolverV8DefaultBindingsTest, MyIpAddress) { EXPECT_EQ("127.0.0.1", my_ip_address); } +// Tests that myIpAddress() and dnsResolve() pass the flag +// ADDRESS_FAMILY_IPV4_ONLY to the host resolver, as we don't want them +// to return IPv6 results. +TEST(ProxyResolverJSBindingsTest, DontUseIPv6) { + scoped_refptr<MockHostResolver> host_resolver(new MockHostResolver); + + // Get a hold of a DefaultJSBindings* (it is a hidden impl class). + scoped_ptr<ProxyResolverJSBindings> bindings( + ProxyResolverJSBindings::CreateDefault( + host_resolver, NULL)); + + // Make it so requests resolve to particular address patterns based on family: + // IPV4_ONLY --> 192.168.1.* + // UNSPECIFIED --> 192.168.2.1 + host_resolver->rules()->AddRuleForFamily( + "foo", ADDRESS_FAMILY_IPV4_ONLY, "192.168.1.1"); + host_resolver->rules()->AddRuleForFamily( + "*", ADDRESS_FAMILY_IPV4_ONLY, "192.168.1.2"); + host_resolver->rules()->AddRuleForFamily( + "*", ADDRESS_FAMILY_UNSPECIFIED, "192.168.2.1"); + + // Verify that our mock setups works as expected, and we get different results + // depending if the address family was IPV4_ONLY or not. + HostResolver::RequestInfo info("foo", 80); + AddressList address_list; + EXPECT_EQ(OK, host_resolver->Resolve(info, &address_list, NULL, NULL, NULL)); + EXPECT_EQ("192.168.2.1", NetAddressToString(address_list.head())); + + info.set_address_family(ADDRESS_FAMILY_IPV4_ONLY); + EXPECT_EQ(OK, host_resolver->Resolve(info, &address_list, NULL, NULL, NULL)); + EXPECT_EQ("192.168.1.1", NetAddressToString(address_list.head())); + + // Now the actual test. + EXPECT_EQ("192.168.1.2", bindings->MyIpAddress()); + EXPECT_EQ("192.168.1.1", bindings->DnsResolve("foo")); + EXPECT_EQ("192.168.1.2", bindings->DnsResolve("foo2")); +} + } // namespace +} // namespace net |