| 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_EX) { |
| 41 my_ip_address_ex_count++; | 43 my_ip_address_ex_count++; |
| 42 *ip_address_list = my_ip_address_ex_result; | 44 *output = my_ip_address_ex_result; |
| 43 return !my_ip_address_ex_result.empty(); | 45 return !my_ip_address_ex_result.empty(); |
| 44 } | 46 } |
| 45 | 47 |
| 46 virtual bool DnsResolve(const std::string& host, std::string* ip_address) | 48 if (op == DNS_RESOLVE) { |
| 47 OVERRIDE { | 49 dns_resolves.push_back(host); |
| 48 dns_resolves.push_back(host); | 50 *output = dns_resolve_result; |
| 49 *ip_address = dns_resolve_result; | 51 return !dns_resolve_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_EX) { |
| 54 std::string* ip_address_list) OVERRIDE { | 55 dns_resolves_ex.push_back(host); |
| 55 dns_resolves_ex.push_back(host); | 56 *output = dns_resolve_ex_result; |
| 56 *ip_address_list = dns_resolve_ex_result; | 57 return !dns_resolve_ex_result.empty(); |
| 57 return !dns_resolve_ex_result.empty(); | 58 } |
| 59 |
| 60 CHECK(false); |
| 61 return false; |
| 58 } | 62 } |
| 59 | 63 |
| 60 virtual void OnError(int line_number, const string16& message) OVERRIDE { | 64 virtual void OnError(int line_number, const string16& message) OVERRIDE { |
| 61 // Helpful when debugging. | 65 // Helpful when debugging. |
| 62 VLOG(1) << "PAC-error: [" << line_number << "] " << message; | 66 VLOG(1) << "PAC-error: [" << line_number << "] " << message; |
| 63 | 67 |
| 64 errors.push_back(UTF16ToUTF8(message)); | 68 errors.push_back(UTF16ToUTF8(message)); |
| 65 errors_line_number.push_back(line_number); | 69 errors_line_number.push_back(line_number); |
| 66 } | 70 } |
| 67 | 71 |
| 68 virtual void Shutdown() OVERRIDE {} | |
| 69 | |
| 70 // Mock values to return. | 72 // Mock values to return. |
| 71 std::string my_ip_address_result; | 73 std::string my_ip_address_result; |
| 72 std::string my_ip_address_ex_result; | 74 std::string my_ip_address_ex_result; |
| 73 std::string dns_resolve_result; | 75 std::string dns_resolve_result; |
| 74 std::string dns_resolve_ex_result; | 76 std::string dns_resolve_ex_result; |
| 75 | 77 |
| 76 // Inputs we got called with. | 78 // Inputs we got called with. |
| 77 std::vector<std::string> alerts; | 79 std::vector<std::string> alerts; |
| 78 std::vector<std::string> errors; | 80 std::vector<std::string> errors; |
| 79 std::vector<int> errors_line_number; | 81 std::vector<int> errors_line_number; |
| 80 std::vector<std::string> dns_resolves; | 82 std::vector<std::string> dns_resolves; |
| 81 std::vector<std::string> dns_resolves_ex; | 83 std::vector<std::string> dns_resolves_ex; |
| 82 int my_ip_address_count; | 84 int my_ip_address_count; |
| 83 int my_ip_address_ex_count; | 85 int my_ip_address_ex_count; |
| 84 }; | 86 }; |
| 85 | 87 |
| 86 // This is the same as ProxyResolverV8, but it uses mock bindings in place of | 88 // 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 | 89 // the default bindings, and has a helper function to load PAC scripts from |
| 88 // disk. | 90 // disk. |
| 89 class ProxyResolverV8WithMockBindings : public ProxyResolverV8 { | 91 class ProxyResolverV8WithMockBindings : public ProxyResolverV8 { |
| 90 public: | 92 public: |
| 91 ProxyResolverV8WithMockBindings() : ProxyResolverV8(new MockJSBindings()) {} | 93 ProxyResolverV8WithMockBindings() { |
| 94 set_js_bindings(&mock_js_bindings_); |
| 95 } |
| 92 | 96 |
| 93 MockJSBindings* mock_js_bindings() const { | 97 MockJSBindings* mock_js_bindings() { |
| 94 return reinterpret_cast<MockJSBindings*>(js_bindings()); | 98 return &mock_js_bindings_; |
| 95 } | 99 } |
| 96 | 100 |
| 97 // Initialize with the PAC script data at |filename|. | 101 // Initialize with the PAC script data at |filename|. |
| 98 int SetPacScriptFromDisk(const char* filename) { | 102 int SetPacScriptFromDisk(const char* filename) { |
| 99 FilePath path; | 103 FilePath path; |
| 100 PathService::Get(base::DIR_SOURCE_ROOT, &path); | 104 PathService::Get(base::DIR_SOURCE_ROOT, &path); |
| 101 path = path.AppendASCII("net"); | 105 path = path.AppendASCII("net"); |
| 102 path = path.AppendASCII("data"); | 106 path = path.AppendASCII("data"); |
| 103 path = path.AppendASCII("proxy_resolver_v8_unittest"); | 107 path = path.AppendASCII("proxy_resolver_v8_unittest"); |
| 104 path = path.AppendASCII(filename); | 108 path = path.AppendASCII(filename); |
| 105 | 109 |
| 106 // Try to read the file from disk. | 110 // Try to read the file from disk. |
| 107 std::string file_contents; | 111 std::string file_contents; |
| 108 bool ok = file_util::ReadFileToString(path, &file_contents); | 112 bool ok = file_util::ReadFileToString(path, &file_contents); |
| 109 | 113 |
| 110 // If we can't load the file from disk, something is misconfigured. | 114 // If we can't load the file from disk, something is misconfigured. |
| 111 if (!ok) { | 115 if (!ok) { |
| 112 LOG(ERROR) << "Failed to read file: " << path.value(); | 116 LOG(ERROR) << "Failed to read file: " << path.value(); |
| 113 return ERR_UNEXPECTED; | 117 return ERR_UNEXPECTED; |
| 114 } | 118 } |
| 115 | 119 |
| 116 // Load the PAC script into the ProxyResolver. | 120 // Load the PAC script into the ProxyResolver. |
| 117 return SetPacScript(ProxyResolverScriptData::FromUTF8(file_contents), | 121 return SetPacScript(ProxyResolverScriptData::FromUTF8(file_contents), |
| 118 CompletionCallback()); | 122 CompletionCallback()); |
| 119 } | 123 } |
| 124 |
| 125 private: |
| 126 MockJSBindings mock_js_bindings_; |
| 120 }; | 127 }; |
| 121 | 128 |
| 122 // Doesn't really matter what these values are for many of the tests. | 129 // Doesn't really matter what these values are for many of the tests. |
| 123 const GURL kQueryUrl("http://www.google.com"); | 130 const GURL kQueryUrl("http://www.google.com"); |
| 124 const GURL kPacUrl; | 131 const GURL kPacUrl; |
| 125 | 132 |
| 126 | |
| 127 TEST(ProxyResolverV8Test, Direct) { | 133 TEST(ProxyResolverV8Test, Direct) { |
| 128 ProxyResolverV8WithMockBindings resolver; | 134 ProxyResolverV8WithMockBindings resolver; |
| 129 int result = resolver.SetPacScriptFromDisk("direct.js"); | 135 int result = resolver.SetPacScriptFromDisk("direct.js"); |
| 130 EXPECT_EQ(OK, result); | 136 EXPECT_EQ(OK, result); |
| 131 | 137 |
| 132 ProxyInfo proxy_info; | 138 ProxyInfo proxy_info; |
| 133 CapturingBoundNetLog log; | 139 CapturingBoundNetLog log; |
| 134 result = resolver.GetProxyForURL( | 140 result = resolver.GetProxyForURL( |
| 135 kQueryUrl, &proxy_info, CompletionCallback(), NULL, log.bound()); | 141 kQueryUrl, &proxy_info, CompletionCallback(), NULL, log.bound()); |
| 136 | 142 |
| (...skipping 425 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 562 EXPECT_TRUE(proxy_info.is_direct()); | 568 EXPECT_TRUE(proxy_info.is_direct()); |
| 563 | 569 |
| 564 // We called dnsResolveEx() exactly once, by passing through the "host" | 570 // We called dnsResolveEx() exactly once, by passing through the "host" |
| 565 // argument to FindProxyForURL(). The brackets should have been stripped. | 571 // argument to FindProxyForURL(). The brackets should have been stripped. |
| 566 ASSERT_EQ(1U, resolver.mock_js_bindings()->dns_resolves_ex.size()); | 572 ASSERT_EQ(1U, resolver.mock_js_bindings()->dns_resolves_ex.size()); |
| 567 EXPECT_EQ("abcd::efff", resolver.mock_js_bindings()->dns_resolves_ex[0]); | 573 EXPECT_EQ("abcd::efff", resolver.mock_js_bindings()->dns_resolves_ex[0]); |
| 568 } | 574 } |
| 569 | 575 |
| 570 } // namespace | 576 } // namespace |
| 571 } // namespace net | 577 } // namespace net |
| OLD | NEW |