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 "base/compiler_specific.h" | 5 #include "base/compiler_specific.h" |
6 #include "base/file_util.h" | 6 #include "base/file_util.h" |
7 #include "base/path_service.h" | 7 #include "base/path_service.h" |
8 #include "base/string_util.h" | 8 #include "base/string_util.h" |
9 #include "base/stringprintf.h" | 9 #include "base/stringprintf.h" |
10 #include "base/utf_string_conversions.h" | 10 #include "base/utf_string_conversions.h" |
11 #include "googleurl/src/gurl.h" | 11 #include "googleurl/src/gurl.h" |
12 #include "net/base/net_errors.h" | 12 #include "net/base/net_errors.h" |
13 #include "net/base/net_log_unittest.h" | 13 #include "net/base/net_log_unittest.h" |
14 #include "net/proxy/proxy_info.h" | 14 #include "net/proxy/proxy_info.h" |
15 #include "net/proxy/proxy_resolver_js_bindings.h" | |
16 #include "net/proxy/proxy_resolver_v8.h" | 15 #include "net/proxy/proxy_resolver_v8.h" |
17 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
18 | 17 |
19 namespace net { | 18 namespace net { |
20 namespace { | 19 namespace { |
21 | 20 |
22 // Javascript bindings for ProxyResolverV8, which returns mock values. | 21 // Javascript bindings for ProxyResolverV8, which returns mock values. |
23 // Each time one of the bindings is called into, we push the input into a | 22 // Each time one of the bindings is called into, we push the input into a |
24 // list, for later verification. | 23 // list, for later verification. |
25 class MockJSBindings : public ProxyResolverJSBindings { | 24 class MockJSBindings : public ProxyResolverV8::JSBindings { |
26 public: | 25 public: |
27 MockJSBindings() : my_ip_address_count(0), my_ip_address_ex_count(0) {} | 26 MockJSBindings() : my_ip_address_count(0), my_ip_address_ex_count(0) {} |
28 | 27 |
29 virtual void Alert(const string16& message) OVERRIDE { | 28 virtual void Alert(const string16& message) OVERRIDE { |
30 VLOG(1) << "PAC-alert: " << message; // Helpful when debugging. | 29 VLOG(1) << "PAC-alert: " << message; // Helpful when debugging. |
31 alerts.push_back(UTF16ToUTF8(message)); | 30 alerts.push_back(UTF16ToUTF8(message)); |
32 } | 31 } |
33 | 32 |
34 virtual bool MyIpAddress(std::string* ip_address) OVERRIDE { | 33 virtual bool ResolveDns(const std::string& host, |
35 my_ip_address_count++; | 34 ResolveDnsOperation op, |
36 *ip_address = my_ip_address_result; | 35 std::string* output) OVERRIDE { |
37 return !my_ip_address_result.empty(); | 36 if (op == MY_IP_ADDRESS) { |
38 } | 37 my_ip_address_count++; |
| 38 *output = my_ip_address_result; |
| 39 return !my_ip_address_result.empty(); |
| 40 } |
39 | 41 |
40 virtual bool MyIpAddressEx(std::string* ip_address_list) OVERRIDE { | 42 if (op == MY_IP_ADDRESS) { |
41 my_ip_address_ex_count++; | 43 my_ip_address_count++; |
42 *ip_address_list = my_ip_address_ex_result; | 44 *output = my_ip_address_result; |
43 return !my_ip_address_ex_result.empty(); | 45 return !my_ip_address_result.empty(); |
44 } | 46 } |
45 | 47 |
46 virtual bool DnsResolve(const std::string& host, std::string* ip_address) | 48 if (op == MY_IP_ADDRESS_EX) { |
47 OVERRIDE { | 49 my_ip_address_ex_count++; |
48 dns_resolves.push_back(host); | 50 *output = my_ip_address_ex_result; |
49 *ip_address = dns_resolve_result; | 51 return !my_ip_address_ex_result.empty(); |
50 return !dns_resolve_result.empty(); | 52 } |
51 } | |
52 | 53 |
53 virtual bool DnsResolveEx(const std::string& host, | 54 if (op == DNS_RESOLVE) { |
54 std::string* ip_address_list) OVERRIDE { | 55 dns_resolves.push_back(host); |
55 dns_resolves_ex.push_back(host); | 56 *output = dns_resolve_result; |
56 *ip_address_list = dns_resolve_ex_result; | 57 return !dns_resolve_result.empty(); |
57 return !dns_resolve_ex_result.empty(); | 58 } |
| 59 |
| 60 if (op == DNS_RESOLVE_EX) { |
| 61 dns_resolves_ex.push_back(host); |
| 62 *output = dns_resolve_ex_result; |
| 63 return !dns_resolve_ex_result.empty(); |
| 64 } |
| 65 |
| 66 CHECK(false); |
| 67 return false; |
58 } | 68 } |
59 | 69 |
60 virtual void OnError(int line_number, const string16& message) OVERRIDE { | 70 virtual void OnError(int line_number, const string16& message) OVERRIDE { |
61 // Helpful when debugging. | 71 // Helpful when debugging. |
62 VLOG(1) << "PAC-error: [" << line_number << "] " << message; | 72 VLOG(1) << "PAC-error: [" << line_number << "] " << message; |
63 | 73 |
64 errors.push_back(UTF16ToUTF8(message)); | 74 errors.push_back(UTF16ToUTF8(message)); |
65 errors_line_number.push_back(line_number); | 75 errors_line_number.push_back(line_number); |
66 } | 76 } |
67 | 77 |
68 virtual void Shutdown() OVERRIDE {} | |
69 | |
70 // Mock values to return. | 78 // Mock values to return. |
71 std::string my_ip_address_result; | 79 std::string my_ip_address_result; |
72 std::string my_ip_address_ex_result; | 80 std::string my_ip_address_ex_result; |
73 std::string dns_resolve_result; | 81 std::string dns_resolve_result; |
74 std::string dns_resolve_ex_result; | 82 std::string dns_resolve_ex_result; |
75 | 83 |
76 // Inputs we got called with. | 84 // Inputs we got called with. |
77 std::vector<std::string> alerts; | 85 std::vector<std::string> alerts; |
78 std::vector<std::string> errors; | 86 std::vector<std::string> errors; |
79 std::vector<int> errors_line_number; | 87 std::vector<int> errors_line_number; |
80 std::vector<std::string> dns_resolves; | 88 std::vector<std::string> dns_resolves; |
81 std::vector<std::string> dns_resolves_ex; | 89 std::vector<std::string> dns_resolves_ex; |
82 int my_ip_address_count; | 90 int my_ip_address_count; |
83 int my_ip_address_ex_count; | 91 int my_ip_address_ex_count; |
84 }; | 92 }; |
85 | 93 |
86 // This is the same as ProxyResolverV8, but it uses mock bindings in place of | 94 // This is the same as ProxyResolverV8, but it uses mock bindings in place of |
87 // the default bindings, and has a helper function to load PAC scripts from | 95 // the default bindings, and has a helper function to load PAC scripts from |
88 // disk. | 96 // disk. |
89 class ProxyResolverV8WithMockBindings : public ProxyResolverV8 { | 97 class ProxyResolverV8WithMockBindings : public ProxyResolverV8 { |
90 public: | 98 public: |
91 ProxyResolverV8WithMockBindings() : ProxyResolverV8(new MockJSBindings()) {} | 99 ProxyResolverV8WithMockBindings() : ProxyResolverV8() { |
| 100 mock_js_bindings_.reset(new MockJSBindings()); |
| 101 set_js_bindings(mock_js_bindings_.get()); |
| 102 } |
92 | 103 |
93 MockJSBindings* mock_js_bindings() const { | 104 MockJSBindings* mock_js_bindings() const { |
94 return reinterpret_cast<MockJSBindings*>(js_bindings()); | 105 return mock_js_bindings_.get(); |
95 } | 106 } |
96 | 107 |
97 // Initialize with the PAC script data at |filename|. | 108 // Initialize with the PAC script data at |filename|. |
98 int SetPacScriptFromDisk(const char* filename) { | 109 int SetPacScriptFromDisk(const char* filename) { |
99 FilePath path; | 110 FilePath path; |
100 PathService::Get(base::DIR_SOURCE_ROOT, &path); | 111 PathService::Get(base::DIR_SOURCE_ROOT, &path); |
101 path = path.AppendASCII("net"); | 112 path = path.AppendASCII("net"); |
102 path = path.AppendASCII("data"); | 113 path = path.AppendASCII("data"); |
103 path = path.AppendASCII("proxy_resolver_v8_unittest"); | 114 path = path.AppendASCII("proxy_resolver_v8_unittest"); |
104 path = path.AppendASCII(filename); | 115 path = path.AppendASCII(filename); |
105 | 116 |
106 // Try to read the file from disk. | 117 // Try to read the file from disk. |
107 std::string file_contents; | 118 std::string file_contents; |
108 bool ok = file_util::ReadFileToString(path, &file_contents); | 119 bool ok = file_util::ReadFileToString(path, &file_contents); |
109 | 120 |
110 // If we can't load the file from disk, something is misconfigured. | 121 // If we can't load the file from disk, something is misconfigured. |
111 if (!ok) { | 122 if (!ok) { |
112 LOG(ERROR) << "Failed to read file: " << path.value(); | 123 LOG(ERROR) << "Failed to read file: " << path.value(); |
113 return ERR_UNEXPECTED; | 124 return ERR_UNEXPECTED; |
114 } | 125 } |
115 | 126 |
116 // Load the PAC script into the ProxyResolver. | 127 // Load the PAC script into the ProxyResolver. |
117 return SetPacScript(ProxyResolverScriptData::FromUTF8(file_contents), | 128 return SetPacScript(ProxyResolverScriptData::FromUTF8(file_contents), |
118 CompletionCallback()); | 129 CompletionCallback()); |
119 } | 130 } |
| 131 private: |
| 132 scoped_ptr<MockJSBindings> mock_js_bindings_; |
120 }; | 133 }; |
121 | 134 |
122 // Doesn't really matter what these values are for many of the tests. | 135 // Doesn't really matter what these values are for many of the tests. |
123 const GURL kQueryUrl("http://www.google.com"); | 136 const GURL kQueryUrl("http://www.google.com"); |
124 const GURL kPacUrl; | 137 const GURL kPacUrl; |
125 | 138 |
126 | |
127 TEST(ProxyResolverV8Test, Direct) { | 139 TEST(ProxyResolverV8Test, Direct) { |
128 ProxyResolverV8WithMockBindings resolver; | 140 ProxyResolverV8WithMockBindings resolver; |
129 int result = resolver.SetPacScriptFromDisk("direct.js"); | 141 int result = resolver.SetPacScriptFromDisk("direct.js"); |
130 EXPECT_EQ(OK, result); | 142 EXPECT_EQ(OK, result); |
131 | 143 |
132 ProxyInfo proxy_info; | 144 ProxyInfo proxy_info; |
133 CapturingBoundNetLog log; | 145 CapturingBoundNetLog log; |
134 result = resolver.GetProxyForURL( | 146 result = resolver.GetProxyForURL( |
135 kQueryUrl, &proxy_info, CompletionCallback(), NULL, log.bound()); | 147 kQueryUrl, &proxy_info, CompletionCallback(), NULL, log.bound()); |
136 | 148 |
(...skipping 425 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
562 EXPECT_TRUE(proxy_info.is_direct()); | 574 EXPECT_TRUE(proxy_info.is_direct()); |
563 | 575 |
564 // We called dnsResolveEx() exactly once, by passing through the "host" | 576 // We called dnsResolveEx() exactly once, by passing through the "host" |
565 // argument to FindProxyForURL(). The brackets should have been stripped. | 577 // argument to FindProxyForURL(). The brackets should have been stripped. |
566 ASSERT_EQ(1U, resolver.mock_js_bindings()->dns_resolves_ex.size()); | 578 ASSERT_EQ(1U, resolver.mock_js_bindings()->dns_resolves_ex.size()); |
567 EXPECT_EQ("abcd::efff", resolver.mock_js_bindings()->dns_resolves_ex[0]); | 579 EXPECT_EQ("abcd::efff", resolver.mock_js_bindings()->dns_resolves_ex[0]); |
568 } | 580 } |
569 | 581 |
570 } // namespace | 582 } // namespace |
571 } // namespace net | 583 } // namespace net |
OLD | NEW |