OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 "base/scoped_ptr.h" | 5 #include "base/scoped_ptr.h" |
6 #include "net/base/mock_host_resolver.h" | 6 #include "net/base/mock_host_resolver.h" |
7 #include "net/base/net_errors.h" | 7 #include "net/base/net_errors.h" |
| 8 #include "net/base/net_util.h" |
8 #include "net/proxy/proxy_resolver_js_bindings.h" | 9 #include "net/proxy/proxy_resolver_js_bindings.h" |
9 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
10 | 11 |
| 12 namespace net { |
11 namespace { | 13 namespace { |
12 | 14 |
13 TEST(ProxyResolverJSBindingsTest, DnsResolve) { | 15 TEST(ProxyResolverJSBindingsTest, DnsResolve) { |
14 scoped_refptr<net::MockHostResolver> host_resolver(new net::MockHostResolver); | 16 scoped_refptr<MockHostResolver> host_resolver(new MockHostResolver); |
15 | 17 |
16 // Get a hold of a DefaultJSBindings* (it is a hidden impl class). | 18 // Get a hold of a DefaultJSBindings* (it is a hidden impl class). |
17 scoped_ptr<net::ProxyResolverJSBindings> bindings( | 19 scoped_ptr<ProxyResolverJSBindings> bindings( |
18 net::ProxyResolverJSBindings::CreateDefault(host_resolver, NULL)); | 20 ProxyResolverJSBindings::CreateDefault(host_resolver, NULL)); |
19 | 21 |
20 // Empty string is not considered a valid host (even though on some systems | 22 // Empty string is not considered a valid host (even though on some systems |
21 // requesting this will resolve to localhost). | 23 // requesting this will resolve to localhost). |
22 EXPECT_EQ("", bindings->DnsResolve("")); | 24 EXPECT_EQ("", bindings->DnsResolve("")); |
23 | 25 |
24 // Should call through to the HostResolver. | 26 // Should call through to the HostResolver. |
25 host_resolver->rules()->AddRule("google.com", "192.168.1.1"); | 27 host_resolver->rules()->AddRule("google.com", "192.168.1.1"); |
26 EXPECT_EQ("192.168.1.1", bindings->DnsResolve("google.com")); | 28 EXPECT_EQ("192.168.1.1", bindings->DnsResolve("google.com")); |
27 | 29 |
28 // Resolve failures should give empty string. | 30 // Resolve failures should give empty string. |
29 host_resolver->rules()->AddSimulatedFailure("fail"); | 31 host_resolver->rules()->AddSimulatedFailure("fail"); |
30 EXPECT_EQ("", bindings->DnsResolve("fail")); | 32 EXPECT_EQ("", bindings->DnsResolve("fail")); |
31 | 33 |
32 // TODO(eroman): would be nice to have an IPV6 test here too, but that | 34 // TODO(eroman): would be nice to have an IPV6 test here too, but that |
33 // won't work on all systems. | 35 // won't work on all systems. |
34 } | 36 } |
35 | 37 |
36 TEST(ProxyResolverV8DefaultBindingsTest, MyIpAddress) { | 38 TEST(ProxyResolverJSBindingsTest, MyIpAddress) { |
37 // Get a hold of a DefaultJSBindings* (it is a hidden impl class). | 39 // Get a hold of a DefaultJSBindings* (it is a hidden impl class). |
38 scoped_ptr<net::ProxyResolverJSBindings> bindings( | 40 scoped_ptr<ProxyResolverJSBindings> bindings( |
39 net::ProxyResolverJSBindings::CreateDefault( | 41 ProxyResolverJSBindings::CreateDefault( |
40 new net::MockHostResolver, NULL)); | 42 new MockHostResolver, NULL)); |
41 | 43 |
42 // Our IP address is always going to be 127.0.0.1, since we are using a | 44 // Our IP address is always going to be 127.0.0.1, since we are using a |
43 // mock host resolver. | 45 // mock host resolver. |
44 std::string my_ip_address = bindings->MyIpAddress(); | 46 std::string my_ip_address = bindings->MyIpAddress(); |
45 | 47 |
46 EXPECT_EQ("127.0.0.1", my_ip_address); | 48 EXPECT_EQ("127.0.0.1", my_ip_address); |
47 } | 49 } |
48 | 50 |
| 51 // Tests that myIpAddress() and dnsResolve() pass the flag |
| 52 // ADDRESS_FAMILY_IPV4_ONLY to the host resolver, as we don't want them |
| 53 // to return IPv6 results. |
| 54 TEST(ProxyResolverJSBindingsTest, DontUseIPv6) { |
| 55 scoped_refptr<MockHostResolver> host_resolver(new MockHostResolver); |
| 56 |
| 57 // Get a hold of a DefaultJSBindings* (it is a hidden impl class). |
| 58 scoped_ptr<ProxyResolverJSBindings> bindings( |
| 59 ProxyResolverJSBindings::CreateDefault( |
| 60 host_resolver, NULL)); |
| 61 |
| 62 // Make it so requests resolve to particular address patterns based on family: |
| 63 // IPV4_ONLY --> 192.168.1.* |
| 64 // UNSPECIFIED --> 192.168.2.1 |
| 65 host_resolver->rules()->AddRuleForFamily( |
| 66 "foo", ADDRESS_FAMILY_IPV4_ONLY, "192.168.1.1"); |
| 67 host_resolver->rules()->AddRuleForFamily( |
| 68 "*", ADDRESS_FAMILY_IPV4_ONLY, "192.168.1.2"); |
| 69 host_resolver->rules()->AddRuleForFamily( |
| 70 "*", ADDRESS_FAMILY_UNSPECIFIED, "192.168.2.1"); |
| 71 |
| 72 // Verify that our mock setups works as expected, and we get different results |
| 73 // depending if the address family was IPV4_ONLY or not. |
| 74 HostResolver::RequestInfo info("foo", 80); |
| 75 AddressList address_list; |
| 76 EXPECT_EQ(OK, host_resolver->Resolve(info, &address_list, NULL, NULL, NULL)); |
| 77 EXPECT_EQ("192.168.2.1", NetAddressToString(address_list.head())); |
| 78 |
| 79 info.set_address_family(ADDRESS_FAMILY_IPV4_ONLY); |
| 80 EXPECT_EQ(OK, host_resolver->Resolve(info, &address_list, NULL, NULL, NULL)); |
| 81 EXPECT_EQ("192.168.1.1", NetAddressToString(address_list.head())); |
| 82 |
| 83 // Now the actual test. |
| 84 EXPECT_EQ("192.168.1.2", bindings->MyIpAddress()); |
| 85 EXPECT_EQ("192.168.1.1", bindings->DnsResolve("foo")); |
| 86 EXPECT_EQ("192.168.1.2", bindings->DnsResolve("foo2")); |
| 87 } |
| 88 |
49 } // namespace | 89 } // namespace |
| 90 } // namespace net |
OLD | NEW |