| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/proxy/proxy_resolver_js_bindings.h" |
| 6 |
| 5 #include "base/scoped_ptr.h" | 7 #include "base/scoped_ptr.h" |
| 6 #include "base/string_util.h" | 8 #include "base/string_util.h" |
| 7 #include "net/base/address_list.h" | 9 #include "net/base/address_list.h" |
| 8 #include "net/base/mock_host_resolver.h" | 10 #include "net/base/mock_host_resolver.h" |
| 9 #include "net/base/net_errors.h" | 11 #include "net/base/net_errors.h" |
| 10 #include "net/base/net_log.h" | 12 #include "net/base/net_log.h" |
| 13 #include "net/base/net_log_unittest.h" |
| 11 #include "net/base/net_util.h" | 14 #include "net/base/net_util.h" |
| 12 #include "net/base/sys_addrinfo.h" | 15 #include "net/base/sys_addrinfo.h" |
| 13 #include "net/proxy/proxy_resolver_js_bindings.h" | |
| 14 #include "net/proxy/proxy_resolver_request_context.h" | 16 #include "net/proxy/proxy_resolver_request_context.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
| 16 | 18 |
| 17 namespace net { | 19 namespace net { |
| 20 |
| 18 namespace { | 21 namespace { |
| 19 | 22 |
| 20 // This is a HostResolver that synchronously resolves all hosts to the | 23 // This is a HostResolver that synchronously resolves all hosts to the |
| 21 // following address list of length 3: | 24 // following address list of length 3: |
| 22 // 192.168.1.1 | 25 // 192.168.1.1 |
| 23 // 172.22.34.1 | 26 // 172.22.34.1 |
| 24 // 200.100.1.2 | 27 // 200.100.1.2 |
| 25 class MockHostResolverWithMultipleResults : public HostResolver { | 28 class MockHostResolverWithMultipleResults : public HostResolver { |
| 26 public: | 29 public: |
| 27 // HostResolver methods: | 30 // HostResolver methods: |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 105 | 108 |
| 106 private: | 109 private: |
| 107 int count_; | 110 int count_; |
| 108 }; | 111 }; |
| 109 | 112 |
| 110 TEST(ProxyResolverJSBindingsTest, DnsResolve) { | 113 TEST(ProxyResolverJSBindingsTest, DnsResolve) { |
| 111 scoped_refptr<MockHostResolver> host_resolver(new MockHostResolver); | 114 scoped_refptr<MockHostResolver> host_resolver(new MockHostResolver); |
| 112 | 115 |
| 113 // Get a hold of a DefaultJSBindings* (it is a hidden impl class). | 116 // Get a hold of a DefaultJSBindings* (it is a hidden impl class). |
| 114 scoped_ptr<ProxyResolverJSBindings> bindings( | 117 scoped_ptr<ProxyResolverJSBindings> bindings( |
| 115 ProxyResolverJSBindings::CreateDefault(host_resolver)); | 118 ProxyResolverJSBindings::CreateDefault(host_resolver, NULL)); |
| 116 | 119 |
| 117 std::string ip_address; | 120 std::string ip_address; |
| 118 | 121 |
| 119 // Empty string is not considered a valid host (even though on some systems | 122 // Empty string is not considered a valid host (even though on some systems |
| 120 // requesting this will resolve to localhost). | 123 // requesting this will resolve to localhost). |
| 121 host_resolver->rules()->AddSimulatedFailure(""); | 124 host_resolver->rules()->AddSimulatedFailure(""); |
| 122 EXPECT_FALSE(bindings->DnsResolve("", &ip_address)); | 125 EXPECT_FALSE(bindings->DnsResolve("", &ip_address)); |
| 123 | 126 |
| 124 // Should call through to the HostResolver. | 127 // Should call through to the HostResolver. |
| 125 host_resolver->rules()->AddRule("google.com", "192.168.1.1"); | 128 host_resolver->rules()->AddRule("google.com", "192.168.1.1"); |
| 126 EXPECT_TRUE(bindings->DnsResolve("google.com", &ip_address)); | 129 EXPECT_TRUE(bindings->DnsResolve("google.com", &ip_address)); |
| 127 EXPECT_EQ("192.168.1.1", ip_address); | 130 EXPECT_EQ("192.168.1.1", ip_address); |
| 128 | 131 |
| 129 // Resolve failures should give empty string. | 132 // Resolve failures should give empty string. |
| 130 host_resolver->rules()->AddSimulatedFailure("fail"); | 133 host_resolver->rules()->AddSimulatedFailure("fail"); |
| 131 EXPECT_FALSE(bindings->DnsResolve("fail", &ip_address)); | 134 EXPECT_FALSE(bindings->DnsResolve("fail", &ip_address)); |
| 132 | 135 |
| 133 // TODO(eroman): would be nice to have an IPV6 test here too, but that | 136 // TODO(eroman): would be nice to have an IPV6 test here too, but that |
| 134 // won't work on all systems. | 137 // won't work on all systems. |
| 135 } | 138 } |
| 136 | 139 |
| 137 TEST(ProxyResolverJSBindingsTest, MyIpAddress) { | 140 TEST(ProxyResolverJSBindingsTest, MyIpAddress) { |
| 138 // Get a hold of a DefaultJSBindings* (it is a hidden impl class). | 141 // Get a hold of a DefaultJSBindings* (it is a hidden impl class). |
| 139 scoped_ptr<ProxyResolverJSBindings> bindings( | 142 scoped_ptr<ProxyResolverJSBindings> bindings( |
| 140 ProxyResolverJSBindings::CreateDefault(new MockHostResolver)); | 143 ProxyResolverJSBindings::CreateDefault(new MockHostResolver, NULL)); |
| 141 | 144 |
| 142 // Our IP address is always going to be 127.0.0.1, since we are using a | 145 // Our IP address is always going to be 127.0.0.1, since we are using a |
| 143 // mock host resolver. | 146 // mock host resolver. |
| 144 std::string my_ip_address; | 147 std::string my_ip_address; |
| 145 EXPECT_TRUE(bindings->MyIpAddress(&my_ip_address)); | 148 EXPECT_TRUE(bindings->MyIpAddress(&my_ip_address)); |
| 146 | 149 |
| 147 EXPECT_EQ("127.0.0.1", my_ip_address); | 150 EXPECT_EQ("127.0.0.1", my_ip_address); |
| 148 } | 151 } |
| 149 | 152 |
| 150 // Tests that the regular PAC functions restrict results to IPv4, | 153 // Tests that the regular PAC functions restrict results to IPv4, |
| 151 // but that the Microsoft extensions to PAC do not. We test this | 154 // but that the Microsoft extensions to PAC do not. We test this |
| 152 // by seeing whether ADDRESS_FAMILY_IPV4 or ADDRESS_FAMILY_UNSPECIFIED | 155 // by seeing whether ADDRESS_FAMILY_IPV4 or ADDRESS_FAMILY_UNSPECIFIED |
| 153 // was passed into to the host resolver. | 156 // was passed into to the host resolver. |
| 154 // | 157 // |
| 155 // Restricted to IPv4 address family: | 158 // Restricted to IPv4 address family: |
| 156 // myIpAddress() | 159 // myIpAddress() |
| 157 // dnsResolve() | 160 // dnsResolve() |
| 158 // | 161 // |
| 159 // Unrestricted address family: | 162 // Unrestricted address family: |
| 160 // myIpAddressEx() | 163 // myIpAddressEx() |
| 161 // dnsResolveEx() | 164 // dnsResolveEx() |
| 162 TEST(ProxyResolverJSBindingsTest, RestrictAddressFamily) { | 165 TEST(ProxyResolverJSBindingsTest, RestrictAddressFamily) { |
| 163 scoped_refptr<MockHostResolver> host_resolver(new MockHostResolver); | 166 scoped_refptr<MockHostResolver> host_resolver(new MockHostResolver); |
| 164 | 167 |
| 165 // Get a hold of a DefaultJSBindings* (it is a hidden impl class). | 168 // Get a hold of a DefaultJSBindings* (it is a hidden impl class). |
| 166 scoped_ptr<ProxyResolverJSBindings> bindings( | 169 scoped_ptr<ProxyResolverJSBindings> bindings( |
| 167 ProxyResolverJSBindings::CreateDefault(host_resolver)); | 170 ProxyResolverJSBindings::CreateDefault(host_resolver, NULL)); |
| 168 | 171 |
| 169 // Make it so requests resolve to particular address patterns based on family: | 172 // Make it so requests resolve to particular address patterns based on family: |
| 170 // IPV4_ONLY --> 192.168.1.* | 173 // IPV4_ONLY --> 192.168.1.* |
| 171 // UNSPECIFIED --> 192.168.2.1 | 174 // UNSPECIFIED --> 192.168.2.1 |
| 172 host_resolver->rules()->AddRuleForAddressFamily( | 175 host_resolver->rules()->AddRuleForAddressFamily( |
| 173 "foo", ADDRESS_FAMILY_IPV4, "192.168.1.1"); | 176 "foo", ADDRESS_FAMILY_IPV4, "192.168.1.1"); |
| 174 host_resolver->rules()->AddRuleForAddressFamily( | 177 host_resolver->rules()->AddRuleForAddressFamily( |
| 175 "*", ADDRESS_FAMILY_IPV4, "192.168.1.2"); | 178 "*", ADDRESS_FAMILY_IPV4, "192.168.1.2"); |
| 176 host_resolver->rules()->AddRuleForAddressFamily( | 179 host_resolver->rules()->AddRuleForAddressFamily( |
| 177 "foo", ADDRESS_FAMILY_UNSPECIFIED, "192.168.2.1"); | 180 "foo", ADDRESS_FAMILY_UNSPECIFIED, "192.168.2.1"); |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 214 | 217 |
| 215 // Test that myIpAddressEx() and dnsResolveEx() both return a semi-colon | 218 // Test that myIpAddressEx() and dnsResolveEx() both return a semi-colon |
| 216 // separated list of addresses (as opposed to the non-Ex versions which | 219 // separated list of addresses (as opposed to the non-Ex versions which |
| 217 // just return the first result). | 220 // just return the first result). |
| 218 TEST(ProxyResolverJSBindingsTest, ExFunctionsReturnList) { | 221 TEST(ProxyResolverJSBindingsTest, ExFunctionsReturnList) { |
| 219 scoped_refptr<HostResolver> host_resolver( | 222 scoped_refptr<HostResolver> host_resolver( |
| 220 new MockHostResolverWithMultipleResults); | 223 new MockHostResolverWithMultipleResults); |
| 221 | 224 |
| 222 // Get a hold of a DefaultJSBindings* (it is a hidden impl class). | 225 // Get a hold of a DefaultJSBindings* (it is a hidden impl class). |
| 223 scoped_ptr<ProxyResolverJSBindings> bindings( | 226 scoped_ptr<ProxyResolverJSBindings> bindings( |
| 224 ProxyResolverJSBindings::CreateDefault(host_resolver)); | 227 ProxyResolverJSBindings::CreateDefault(host_resolver, NULL)); |
| 225 | 228 |
| 226 std::string ip_addresses; | 229 std::string ip_addresses; |
| 227 | 230 |
| 228 EXPECT_TRUE(bindings->MyIpAddressEx(&ip_addresses)); | 231 EXPECT_TRUE(bindings->MyIpAddressEx(&ip_addresses)); |
| 229 EXPECT_EQ("192.168.1.1;172.22.34.1;200.100.1.2", ip_addresses); | 232 EXPECT_EQ("192.168.1.1;172.22.34.1;200.100.1.2", ip_addresses); |
| 230 | 233 |
| 231 EXPECT_TRUE(bindings->DnsResolveEx("FOO", &ip_addresses)); | 234 EXPECT_TRUE(bindings->DnsResolveEx("FOO", &ip_addresses)); |
| 232 EXPECT_EQ("192.168.1.1;172.22.34.1;200.100.1.2", ip_addresses); | 235 EXPECT_EQ("192.168.1.1;172.22.34.1;200.100.1.2", ip_addresses); |
| 233 } | 236 } |
| 234 | 237 |
| 235 TEST(ProxyResolverJSBindingsTest, PerRequestDNSCache) { | 238 TEST(ProxyResolverJSBindingsTest, PerRequestDNSCache) { |
| 236 scoped_refptr<MockFailingHostResolver> host_resolver( | 239 scoped_refptr<MockFailingHostResolver> host_resolver( |
| 237 new MockFailingHostResolver); | 240 new MockFailingHostResolver); |
| 238 | 241 |
| 239 // Get a hold of a DefaultJSBindings* (it is a hidden impl class). | 242 // Get a hold of a DefaultJSBindings* (it is a hidden impl class). |
| 240 scoped_ptr<ProxyResolverJSBindings> bindings( | 243 scoped_ptr<ProxyResolverJSBindings> bindings( |
| 241 ProxyResolverJSBindings::CreateDefault(host_resolver)); | 244 ProxyResolverJSBindings::CreateDefault(host_resolver, NULL)); |
| 242 | 245 |
| 243 std::string ip_address; | 246 std::string ip_address; |
| 244 | 247 |
| 245 // Call DnsResolve() 4 times for the same hostname -- this should issue | 248 // Call DnsResolve() 4 times for the same hostname -- this should issue |
| 246 // 4 separate calls to the underlying host resolver, since there is no | 249 // 4 separate calls to the underlying host resolver, since there is no |
| 247 // current request context. | 250 // current request context. |
| 248 EXPECT_FALSE(bindings->DnsResolve("foo", &ip_address)); | 251 EXPECT_FALSE(bindings->DnsResolve("foo", &ip_address)); |
| 249 EXPECT_FALSE(bindings->DnsResolve("foo", &ip_address)); | 252 EXPECT_FALSE(bindings->DnsResolve("foo", &ip_address)); |
| 250 EXPECT_FALSE(bindings->DnsResolve("foo", &ip_address)); | 253 EXPECT_FALSE(bindings->DnsResolve("foo", &ip_address)); |
| 251 EXPECT_FALSE(bindings->DnsResolve("foo", &ip_address)); | 254 EXPECT_FALSE(bindings->DnsResolve("foo", &ip_address)); |
| (...skipping 22 matching lines...) Expand all Loading... |
| 274 // are different it won't reuse this particular entry. | 277 // are different it won't reuse this particular entry. |
| 275 EXPECT_FALSE(bindings->DnsResolveEx("foo", &ip_address)); | 278 EXPECT_FALSE(bindings->DnsResolveEx("foo", &ip_address)); |
| 276 EXPECT_EQ(1, host_resolver->count()); | 279 EXPECT_EQ(1, host_resolver->count()); |
| 277 EXPECT_FALSE(bindings->DnsResolveEx("foo", &ip_address)); | 280 EXPECT_FALSE(bindings->DnsResolveEx("foo", &ip_address)); |
| 278 EXPECT_FALSE(bindings->DnsResolveEx("foo", &ip_address)); | 281 EXPECT_FALSE(bindings->DnsResolveEx("foo", &ip_address)); |
| 279 EXPECT_EQ(1, host_resolver->count()); | 282 EXPECT_EQ(1, host_resolver->count()); |
| 280 | 283 |
| 281 bindings->set_current_request_context(NULL); | 284 bindings->set_current_request_context(NULL); |
| 282 } | 285 } |
| 283 | 286 |
| 287 // Test that when a binding is called, it logs to the per-request NetLog. |
| 288 TEST(ProxyResolverJSBindingsTest, NetLog) { |
| 289 scoped_refptr<MockFailingHostResolver> host_resolver( |
| 290 new MockFailingHostResolver); |
| 291 |
| 292 CapturingNetLog global_log(CapturingNetLog::kUnbounded); |
| 293 |
| 294 // Get a hold of a DefaultJSBindings* (it is a hidden impl class). |
| 295 scoped_ptr<ProxyResolverJSBindings> bindings( |
| 296 ProxyResolverJSBindings::CreateDefault(host_resolver, &global_log)); |
| 297 |
| 298 // Attach a capturing NetLog as the current request's log stream. |
| 299 CapturingNetLog log(CapturingNetLog::kUnbounded); |
| 300 BoundNetLog bound_log(NetLog::Source(NetLog::SOURCE_NONE, 0), &log); |
| 301 ProxyResolverRequestContext context(&bound_log, NULL); |
| 302 bindings->set_current_request_context(&context); |
| 303 |
| 304 std::string ip_address; |
| 305 |
| 306 ASSERT_EQ(0u, log.entries().size()); |
| 307 |
| 308 // Call all the bindings. Each call should be logging something to |
| 309 // our NetLog. |
| 310 |
| 311 bindings->MyIpAddress(&ip_address); |
| 312 EXPECT_EQ(2u, log.entries().size()); |
| 313 EXPECT_TRUE(LogContainsBeginEvent( |
| 314 log.entries(), 0, NetLog::TYPE_PAC_JAVASCRIPT_MY_IP_ADDRESS)); |
| 315 EXPECT_TRUE(LogContainsEndEvent( |
| 316 log.entries(), 1, NetLog::TYPE_PAC_JAVASCRIPT_MY_IP_ADDRESS)); |
| 317 |
| 318 bindings->MyIpAddressEx(&ip_address); |
| 319 EXPECT_EQ(4u, log.entries().size()); |
| 320 EXPECT_TRUE(LogContainsBeginEvent( |
| 321 log.entries(), 2, NetLog::TYPE_PAC_JAVASCRIPT_MY_IP_ADDRESS_EX)); |
| 322 EXPECT_TRUE(LogContainsEndEvent( |
| 323 log.entries(), 3, NetLog::TYPE_PAC_JAVASCRIPT_MY_IP_ADDRESS_EX)); |
| 324 |
| 325 bindings->DnsResolve("foo", &ip_address); |
| 326 EXPECT_EQ(6u, log.entries().size()); |
| 327 EXPECT_TRUE(LogContainsBeginEvent( |
| 328 log.entries(), 4, NetLog::TYPE_PAC_JAVASCRIPT_DNS_RESOLVE)); |
| 329 EXPECT_TRUE(LogContainsEndEvent( |
| 330 log.entries(), 5, NetLog::TYPE_PAC_JAVASCRIPT_DNS_RESOLVE)); |
| 331 |
| 332 bindings->DnsResolveEx("foo", &ip_address); |
| 333 EXPECT_EQ(8u, log.entries().size()); |
| 334 EXPECT_TRUE(LogContainsBeginEvent( |
| 335 log.entries(), 6, NetLog::TYPE_PAC_JAVASCRIPT_DNS_RESOLVE_EX)); |
| 336 EXPECT_TRUE(LogContainsEndEvent( |
| 337 log.entries(), 7, NetLog::TYPE_PAC_JAVASCRIPT_DNS_RESOLVE_EX)); |
| 338 |
| 339 // Nothing has been emitted globally yet. |
| 340 EXPECT_EQ(0u, global_log.entries().size()); |
| 341 |
| 342 bindings->OnError(30, string16()); |
| 343 EXPECT_EQ(9u, log.entries().size()); |
| 344 EXPECT_TRUE(LogContainsEvent( |
| 345 log.entries(), 8, NetLog::TYPE_PAC_JAVASCRIPT_ERROR, |
| 346 NetLog::PHASE_NONE)); |
| 347 |
| 348 // We also emit errors to the top-level log stream. |
| 349 EXPECT_EQ(1u, global_log.entries().size()); |
| 350 EXPECT_TRUE(LogContainsEvent( |
| 351 global_log.entries(), 0, NetLog::TYPE_PAC_JAVASCRIPT_ERROR, |
| 352 NetLog::PHASE_NONE)); |
| 353 |
| 354 bindings->Alert(string16()); |
| 355 EXPECT_EQ(10u, log.entries().size()); |
| 356 EXPECT_TRUE(LogContainsEvent( |
| 357 log.entries(), 9, NetLog::TYPE_PAC_JAVASCRIPT_ALERT, |
| 358 NetLog::PHASE_NONE)); |
| 359 |
| 360 // We also emit javascript alerts to the top-level log stream. |
| 361 EXPECT_EQ(2u, global_log.entries().size()); |
| 362 EXPECT_TRUE(LogContainsEvent( |
| 363 global_log.entries(), 1, NetLog::TYPE_PAC_JAVASCRIPT_ALERT, |
| 364 NetLog::PHASE_NONE)); |
| 365 } |
| 366 |
| 284 } // namespace | 367 } // namespace |
| 368 |
| 285 } // namespace net | 369 } // namespace net |
| OLD | NEW |