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/file_util.h" | 5 #include "base/file_util.h" |
6 #include "base/string_util.h" | 6 #include "base/string_util.h" |
7 #include "base/path_service.h" | 7 #include "base/path_service.h" |
8 #include "googleurl/src/gurl.h" | 8 #include "googleurl/src/gurl.h" |
9 #include "net/base/load_log_unittest.h" | 9 #include "net/base/load_log_unittest.h" |
10 #include "net/base/net_errors.h" | 10 #include "net/base/net_errors.h" |
11 #include "net/proxy/proxy_info.h" | 11 #include "net/proxy/proxy_info.h" |
12 #include "net/proxy/proxy_resolver_js_bindings.h" | 12 #include "net/proxy/proxy_resolver_js_bindings.h" |
13 #include "net/proxy/proxy_resolver_v8.h" | 13 #include "net/proxy/proxy_resolver_v8.h" |
14 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
15 | 15 |
16 namespace net { | 16 namespace net { |
17 namespace { | 17 namespace { |
18 | 18 |
19 // Javascript bindings for ProxyResolverV8, which returns mock values. | 19 // Javascript bindings for ProxyResolverV8, which returns mock values. |
20 // Each time one of the bindings is called into, we push the input into a | 20 // Each time one of the bindings is called into, we push the input into a |
21 // list, for later verification. | 21 // list, for later verification. |
22 class MockJSBindings : public ProxyResolverJSBindings { | 22 class MockJSBindings : public ProxyResolverJSBindings { |
23 public: | 23 public: |
24 MockJSBindings() : my_ip_address_count(0) {} | 24 MockJSBindings() : my_ip_address_count(0), my_ip_address_ex_count(0) {} |
25 | 25 |
26 virtual void Alert(const std::string& message) { | 26 virtual void Alert(const std::string& message) { |
27 LOG(INFO) << "PAC-alert: " << message; // Helpful when debugging. | 27 LOG(INFO) << "PAC-alert: " << message; // Helpful when debugging. |
28 alerts.push_back(message); | 28 alerts.push_back(message); |
29 } | 29 } |
30 | 30 |
31 virtual std::string MyIpAddress() { | 31 virtual std::string MyIpAddress() { |
32 my_ip_address_count++; | 32 my_ip_address_count++; |
33 return my_ip_address_result; | 33 return my_ip_address_result; |
34 } | 34 } |
35 | 35 |
| 36 virtual std::string MyIpAddressEx() { |
| 37 my_ip_address_ex_count++; |
| 38 return my_ip_address_ex_result; |
| 39 } |
| 40 |
36 virtual std::string DnsResolve(const std::string& host) { | 41 virtual std::string DnsResolve(const std::string& host) { |
37 dns_resolves.push_back(host); | 42 dns_resolves.push_back(host); |
38 return dns_resolve_result; | 43 return dns_resolve_result; |
39 } | 44 } |
40 | 45 |
| 46 virtual std::string DnsResolveEx(const std::string& host) { |
| 47 dns_resolves_ex.push_back(host); |
| 48 return dns_resolve_ex_result; |
| 49 } |
| 50 |
41 virtual void OnError(int line_number, const std::string& message) { | 51 virtual void OnError(int line_number, const std::string& message) { |
42 // Helpful when debugging. | 52 // Helpful when debugging. |
43 LOG(INFO) << "PAC-error: [" << line_number << "] " << message; | 53 LOG(INFO) << "PAC-error: [" << line_number << "] " << message; |
44 | 54 |
45 errors.push_back(message); | 55 errors.push_back(message); |
46 errors_line_number.push_back(line_number); | 56 errors_line_number.push_back(line_number); |
47 } | 57 } |
48 | 58 |
49 // Mock values to return. | 59 // Mock values to return. |
50 std::string my_ip_address_result; | 60 std::string my_ip_address_result; |
| 61 std::string my_ip_address_ex_result; |
51 std::string dns_resolve_result; | 62 std::string dns_resolve_result; |
| 63 std::string dns_resolve_ex_result; |
52 | 64 |
53 // Inputs we got called with. | 65 // Inputs we got called with. |
54 std::vector<std::string> alerts; | 66 std::vector<std::string> alerts; |
55 std::vector<std::string> errors; | 67 std::vector<std::string> errors; |
56 std::vector<int> errors_line_number; | 68 std::vector<int> errors_line_number; |
57 std::vector<std::string> dns_resolves; | 69 std::vector<std::string> dns_resolves; |
| 70 std::vector<std::string> dns_resolves_ex; |
58 int my_ip_address_count; | 71 int my_ip_address_count; |
| 72 int my_ip_address_ex_count; |
59 }; | 73 }; |
60 | 74 |
61 // This is the same as ProxyResolverV8, but it uses mock bindings in place of | 75 // This is the same as ProxyResolverV8, but it uses mock bindings in place of |
62 // the default bindings, and has a helper function to load PAC scripts from | 76 // the default bindings, and has a helper function to load PAC scripts from |
63 // disk. | 77 // disk. |
64 class ProxyResolverV8WithMockBindings : public ProxyResolverV8 { | 78 class ProxyResolverV8WithMockBindings : public ProxyResolverV8 { |
65 public: | 79 public: |
66 ProxyResolverV8WithMockBindings() : ProxyResolverV8(new MockJSBindings()) {} | 80 ProxyResolverV8WithMockBindings() : ProxyResolverV8(new MockJSBindings()) {} |
67 | 81 |
68 MockJSBindings* mock_js_bindings() const { | 82 MockJSBindings* mock_js_bindings() const { |
(...skipping 315 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
384 EXPECT_EQ("function fn() {}", bindings->dns_resolves[5]); | 398 EXPECT_EQ("function fn() {}", bindings->dns_resolves[5]); |
385 | 399 |
386 // TODO(eroman): This isn't quite right... should probably stringize | 400 // TODO(eroman): This isn't quite right... should probably stringize |
387 // to something like "['3']". | 401 // to something like "['3']". |
388 EXPECT_EQ("3", bindings->dns_resolves[6]); | 402 EXPECT_EQ("3", bindings->dns_resolves[6]); |
389 | 403 |
390 EXPECT_EQ("arg1", bindings->dns_resolves[7]); | 404 EXPECT_EQ("arg1", bindings->dns_resolves[7]); |
391 | 405 |
392 // MyIpAddress was called two times. | 406 // MyIpAddress was called two times. |
393 EXPECT_EQ(2, bindings->my_ip_address_count); | 407 EXPECT_EQ(2, bindings->my_ip_address_count); |
| 408 |
| 409 // MyIpAddressEx was called once. |
| 410 EXPECT_EQ(1, bindings->my_ip_address_ex_count); |
| 411 |
| 412 // DnsResolveEx was called 2 times. |
| 413 ASSERT_EQ(2U, bindings->dns_resolves_ex.size()); |
| 414 EXPECT_EQ("is_resolvable", bindings->dns_resolves_ex[0]); |
| 415 EXPECT_EQ("foobar", bindings->dns_resolves_ex[1]); |
394 } | 416 } |
395 | 417 |
396 // Test that calls to the myIpAddress() and dnsResolve() bindings get | 418 // Test that calls to the myIpAddress() and dnsResolve() bindings get |
397 // logged to the LoadLog parameter. | 419 // logged to the LoadLog parameter. |
398 TEST(ProxyResolverV8Test, LoadLog) { | 420 TEST(ProxyResolverV8Test, LoadLog) { |
399 ProxyResolverV8WithMockBindings resolver; | 421 ProxyResolverV8WithMockBindings resolver; |
400 int result = resolver.SetPacScriptFromDisk("simple.js"); | 422 int result = resolver.SetPacScriptFromDisk("simple.js"); |
401 EXPECT_EQ(OK, result); | 423 EXPECT_EQ(OK, result); |
402 | 424 |
403 ProxyInfo proxy_info; | 425 ProxyInfo proxy_info; |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
457 | 479 |
458 ProxyInfo proxy_info; | 480 ProxyInfo proxy_info; |
459 scoped_refptr<LoadLog> log(new LoadLog); | 481 scoped_refptr<LoadLog> log(new LoadLog); |
460 result = resolver.GetProxyForURL(kQueryUrl, &proxy_info, NULL, NULL, log); | 482 result = resolver.GetProxyForURL(kQueryUrl, &proxy_info, NULL, NULL, log); |
461 | 483 |
462 EXPECT_EQ(OK, result); | 484 EXPECT_EQ(OK, result); |
463 EXPECT_FALSE(proxy_info.is_direct()); | 485 EXPECT_FALSE(proxy_info.is_direct()); |
464 EXPECT_EQ("success:3", proxy_info.proxy_server().ToURI()); | 486 EXPECT_EQ("success:3", proxy_info.proxy_server().ToURI()); |
465 } | 487 } |
466 | 488 |
| 489 // Test the return values from myIpAddress(), myIpAddressEx(), dnsResolve(), |
| 490 // dnsResolveEx(), isResolvable(), isResolvableEx(), when the the binding |
| 491 // returns empty string (failure). This simulates the return values from |
| 492 // those functions when the underlying DNS resolution fails. |
| 493 TEST(ProxyResolverV8Test, DNSResolutionFailure) { |
| 494 ProxyResolverV8WithMockBindings resolver; |
| 495 int result = resolver.SetPacScriptFromDisk("dns_fail.js"); |
| 496 EXPECT_EQ(OK, result); |
| 497 |
| 498 ProxyInfo proxy_info; |
| 499 result = resolver.GetProxyForURL(kQueryUrl, &proxy_info, NULL, NULL, NULL); |
| 500 |
| 501 EXPECT_EQ(OK, result); |
| 502 EXPECT_FALSE(proxy_info.is_direct()); |
| 503 EXPECT_EQ("success:80", proxy_info.proxy_server().ToURI()); |
| 504 } |
| 505 |
467 } // namespace | 506 } // namespace |
468 } // namespace net | 507 } // namespace net |
OLD | NEW |