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/files/file_util.h" | 6 #include "base/files/file_util.h" |
7 #include "base/path_service.h" | 7 #include "base/path_service.h" |
8 #include "base/strings/string_util.h" | 8 #include "base/strings/string_util.h" |
9 #include "base/strings/stringprintf.h" | 9 #include "base/strings/stringprintf.h" |
10 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
11 #include "net/base/net_errors.h" | 11 #include "net/base/net_errors.h" |
12 #include "net/base/net_log_unittest.h" | 12 #include "net/base/net_log_unittest.h" |
13 #include "net/proxy/proxy_info.h" | 13 #include "net/proxy/proxy_info.h" |
14 #include "net/proxy/proxy_resolver_v8.h" | 14 #include "net/proxy/proxy_resolver_v8.h" |
15 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
16 #include "url/gurl.h" | 16 #include "url/gurl.h" |
17 | 17 |
18 namespace net { | 18 namespace net { |
19 namespace { | 19 namespace { |
20 | 20 |
21 // Javascript bindings for ProxyResolverV8, which returns mock values. | 21 // Javascript bindings for ProxyResolverV8, which returns mock values. |
22 // 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 |
23 // list, for later verification. | 23 // list, for later verification. |
24 class MockJSBindings : public ProxyResolverV8::JSBindings { | 24 class MockJSBindings : public ProxyResolverV8::JSBindings { |
25 public: | 25 public: |
26 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), |
27 should_terminate(false) {} | 27 should_terminate(false) {} |
28 | 28 |
29 virtual void Alert(const base::string16& message) override { | 29 void Alert(const base::string16& message) override { |
30 VLOG(1) << "PAC-alert: " << message; // Helpful when debugging. | 30 VLOG(1) << "PAC-alert: " << message; // Helpful when debugging. |
31 alerts.push_back(base::UTF16ToUTF8(message)); | 31 alerts.push_back(base::UTF16ToUTF8(message)); |
32 } | 32 } |
33 | 33 |
34 virtual bool ResolveDns(const std::string& host, | 34 bool ResolveDns(const std::string& host, |
35 ResolveDnsOperation op, | 35 ResolveDnsOperation op, |
36 std::string* output, | 36 std::string* output, |
37 bool* terminate) override { | 37 bool* terminate) override { |
38 *terminate = should_terminate; | 38 *terminate = should_terminate; |
39 | 39 |
40 if (op == MY_IP_ADDRESS) { | 40 if (op == MY_IP_ADDRESS) { |
41 my_ip_address_count++; | 41 my_ip_address_count++; |
42 *output = my_ip_address_result; | 42 *output = my_ip_address_result; |
43 return !my_ip_address_result.empty(); | 43 return !my_ip_address_result.empty(); |
44 } | 44 } |
45 | 45 |
46 if (op == MY_IP_ADDRESS_EX) { | 46 if (op == MY_IP_ADDRESS_EX) { |
47 my_ip_address_ex_count++; | 47 my_ip_address_ex_count++; |
(...skipping 10 matching lines...) Expand all Loading... |
58 if (op == DNS_RESOLVE_EX) { | 58 if (op == DNS_RESOLVE_EX) { |
59 dns_resolves_ex.push_back(host); | 59 dns_resolves_ex.push_back(host); |
60 *output = dns_resolve_ex_result; | 60 *output = dns_resolve_ex_result; |
61 return !dns_resolve_ex_result.empty(); | 61 return !dns_resolve_ex_result.empty(); |
62 } | 62 } |
63 | 63 |
64 CHECK(false); | 64 CHECK(false); |
65 return false; | 65 return false; |
66 } | 66 } |
67 | 67 |
68 virtual void OnError(int line_number, | 68 void OnError(int line_number, const base::string16& message) override { |
69 const base::string16& message) override { | |
70 // Helpful when debugging. | 69 // Helpful when debugging. |
71 VLOG(1) << "PAC-error: [" << line_number << "] " << message; | 70 VLOG(1) << "PAC-error: [" << line_number << "] " << message; |
72 | 71 |
73 errors.push_back(base::UTF16ToUTF8(message)); | 72 errors.push_back(base::UTF16ToUTF8(message)); |
74 errors_line_number.push_back(line_number); | 73 errors_line_number.push_back(line_number); |
75 } | 74 } |
76 | 75 |
77 // Mock values to return. | 76 // Mock values to return. |
78 std::string my_ip_address_result; | 77 std::string my_ip_address_result; |
79 std::string my_ip_address_ex_result; | 78 std::string my_ip_address_ex_result; |
(...skipping 15 matching lines...) Expand all Loading... |
95 | 94 |
96 // This is the same as ProxyResolverV8, but it uses mock bindings in place of | 95 // This is the same as ProxyResolverV8, but it uses mock bindings in place of |
97 // the default bindings, and has a helper function to load PAC scripts from | 96 // the default bindings, and has a helper function to load PAC scripts from |
98 // disk. | 97 // disk. |
99 class ProxyResolverV8WithMockBindings : public ProxyResolverV8 { | 98 class ProxyResolverV8WithMockBindings : public ProxyResolverV8 { |
100 public: | 99 public: |
101 ProxyResolverV8WithMockBindings() { | 100 ProxyResolverV8WithMockBindings() { |
102 set_js_bindings(&mock_js_bindings_); | 101 set_js_bindings(&mock_js_bindings_); |
103 } | 102 } |
104 | 103 |
105 virtual ~ProxyResolverV8WithMockBindings() { | 104 ~ProxyResolverV8WithMockBindings() override {} |
106 } | |
107 | 105 |
108 MockJSBindings* mock_js_bindings() { | 106 MockJSBindings* mock_js_bindings() { |
109 return &mock_js_bindings_; | 107 return &mock_js_bindings_; |
110 } | 108 } |
111 | 109 |
112 // Initialize with the PAC script data at |filename|. | 110 // Initialize with the PAC script data at |filename|. |
113 int SetPacScriptFromDisk(const char* filename) { | 111 int SetPacScriptFromDisk(const char* filename) { |
114 base::FilePath path; | 112 base::FilePath path; |
115 PathService::Get(base::DIR_SOURCE_ROOT, &path); | 113 PathService::Get(base::DIR_SOURCE_ROOT, &path); |
116 path = path.AppendASCII("net"); | 114 path = path.AppendASCII("net"); |
(...skipping 501 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
618 GURL("http://kittens/"), &proxy_info, | 616 GURL("http://kittens/"), &proxy_info, |
619 CompletionCallback(), NULL, BoundNetLog()); | 617 CompletionCallback(), NULL, BoundNetLog()); |
620 | 618 |
621 EXPECT_EQ(OK, result); | 619 EXPECT_EQ(OK, result); |
622 EXPECT_EQ(0u, bindings->errors.size()); | 620 EXPECT_EQ(0u, bindings->errors.size()); |
623 EXPECT_EQ("kittens:88", proxy_info.proxy_server().ToURI()); | 621 EXPECT_EQ("kittens:88", proxy_info.proxy_server().ToURI()); |
624 } | 622 } |
625 | 623 |
626 } // namespace | 624 } // namespace |
627 } // namespace net | 625 } // namespace net |
OLD | NEW |