[email protected] | ad425c5 | 2012-02-11 00:05:31 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 943c808 | 2009-02-23 19:10:45 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Lily Houghton | 582d462 | 2018-01-22 22:43:40 | [diff] [blame] | 5 | #include "net/proxy_resolution/proxy_resolver_v8.h" |
[email protected] | c4c1b48 | 2011-07-22 17:24:26 | [diff] [blame] | 6 | #include "base/compiler_specific.h" |
thestig | d8df033 | 2014-09-04 06:33:29 | [diff] [blame] | 7 | #include "base/files/file_util.h" |
[email protected] | 943c808 | 2009-02-23 19:10:45 | [diff] [blame] | 8 | #include "base/path_service.h" |
[email protected] | fc9be580 | 2013-06-11 10:56:51 | [diff] [blame] | 9 | #include "base/strings/string_util.h" |
| 10 | #include "base/strings/stringprintf.h" |
[email protected] | 750b2f3c | 2013-06-07 18:41:05 | [diff] [blame] | 11 | #include "base/strings/utf_string_conversions.h" |
[email protected] | 943c808 | 2009-02-23 19:10:45 | [diff] [blame] | 12 | #include "net/base/net_errors.h" |
Lily Houghton | 582d462 | 2018-01-22 22:43:40 | [diff] [blame] | 13 | #include "net/proxy_resolution/pac_file_data.h" |
| 14 | #include "net/proxy_resolution/proxy_info.h" |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 15 | #include "net/test/gtest_util.h" |
Bence Béky | 98447b1 | 2018-05-08 03:14:01 | [diff] [blame] | 16 | #include "net/test/test_with_scoped_task_environment.h" |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 17 | #include "testing/gmock/include/gmock/gmock.h" |
[email protected] | 943c808 | 2009-02-23 19:10:45 | [diff] [blame] | 18 | #include "testing/gtest/include/gtest/gtest.h" |
[email protected] | f89276a7 | 2013-07-12 06:41:54 | [diff] [blame] | 19 | #include "url/gurl.h" |
[email protected] | 943c808 | 2009-02-23 19:10:45 | [diff] [blame] | 20 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 21 | using net::test::IsError; |
| 22 | using net::test::IsOk; |
Eric Roman | 9513f52 | 2018-04-04 23:49:47 | [diff] [blame] | 23 | using ::testing::IsEmpty; |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 24 | |
[email protected] | 52ae80c | 2009-09-10 21:27:00 | [diff] [blame] | 25 | namespace net { |
[email protected] | 943c808 | 2009-02-23 19:10:45 | [diff] [blame] | 26 | namespace { |
| 27 | |
[email protected] | 50d7d728 | 2009-03-02 21:45:18 | [diff] [blame] | 28 | // Javascript bindings for ProxyResolverV8, which returns mock values. |
| 29 | // Each time one of the bindings is called into, we push the input into a |
| 30 | // list, for later verification. |
[email protected] | 501e2d4 | 2013-01-30 22:30:49 | [diff] [blame] | 31 | class MockJSBindings : public ProxyResolverV8::JSBindings { |
[email protected] | 50d7d728 | 2009-03-02 21:45:18 | [diff] [blame] | 32 | public: |
sammc | d2284e5 | 2015-05-27 03:14:46 | [diff] [blame] | 33 | MockJSBindings() |
| 34 | : my_ip_address_count(0), |
| 35 | my_ip_address_ex_count(0), |
| 36 | should_terminate(false) {} |
[email protected] | 943c808 | 2009-02-23 19:10:45 | [diff] [blame] | 37 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 38 | void Alert(const base::string16& message) override { |
[email protected] | b30a3f5 | 2010-10-16 01:05:46 | [diff] [blame] | 39 | VLOG(1) << "PAC-alert: " << message; // Helpful when debugging. |
[email protected] | ad65a3e | 2013-12-25 18:18:01 | [diff] [blame] | 40 | alerts.push_back(base::UTF16ToUTF8(message)); |
[email protected] | 50d7d728 | 2009-03-02 21:45:18 | [diff] [blame] | 41 | } |
[email protected] | 943c808 | 2009-02-23 19:10:45 | [diff] [blame] | 42 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 43 | bool ResolveDns(const std::string& host, |
| 44 | ResolveDnsOperation op, |
| 45 | std::string* output, |
| 46 | bool* terminate) override { |
[email protected] | 8cdc881 | 2013-02-21 05:29:49 | [diff] [blame] | 47 | *terminate = should_terminate; |
| 48 | |
[email protected] | 501e2d4 | 2013-01-30 22:30:49 | [diff] [blame] | 49 | if (op == MY_IP_ADDRESS) { |
| 50 | my_ip_address_count++; |
| 51 | *output = my_ip_address_result; |
| 52 | return !my_ip_address_result.empty(); |
| 53 | } |
[email protected] | 943c808 | 2009-02-23 19:10:45 | [diff] [blame] | 54 | |
[email protected] | 501e2d4 | 2013-01-30 22:30:49 | [diff] [blame] | 55 | if (op == MY_IP_ADDRESS_EX) { |
| 56 | my_ip_address_ex_count++; |
| 57 | *output = my_ip_address_ex_result; |
| 58 | return !my_ip_address_ex_result.empty(); |
| 59 | } |
[email protected] | 21f20c89 | 2009-10-26 23:51:28 | [diff] [blame] | 60 | |
[email protected] | 501e2d4 | 2013-01-30 22:30:49 | [diff] [blame] | 61 | if (op == DNS_RESOLVE) { |
| 62 | dns_resolves.push_back(host); |
| 63 | *output = dns_resolve_result; |
| 64 | return !dns_resolve_result.empty(); |
| 65 | } |
[email protected] | 50d7d728 | 2009-03-02 21:45:18 | [diff] [blame] | 66 | |
[email protected] | 501e2d4 | 2013-01-30 22:30:49 | [diff] [blame] | 67 | if (op == DNS_RESOLVE_EX) { |
| 68 | dns_resolves_ex.push_back(host); |
| 69 | *output = dns_resolve_ex_result; |
| 70 | return !dns_resolve_ex_result.empty(); |
| 71 | } |
| 72 | |
| 73 | CHECK(false); |
| 74 | return false; |
[email protected] | 21f20c89 | 2009-10-26 23:51:28 | [diff] [blame] | 75 | } |
| 76 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 77 | void OnError(int line_number, const base::string16& message) override { |
[email protected] | 50d7d728 | 2009-03-02 21:45:18 | [diff] [blame] | 78 | // Helpful when debugging. |
[email protected] | b30a3f5 | 2010-10-16 01:05:46 | [diff] [blame] | 79 | VLOG(1) << "PAC-error: [" << line_number << "] " << message; |
[email protected] | 50d7d728 | 2009-03-02 21:45:18 | [diff] [blame] | 80 | |
[email protected] | ad65a3e | 2013-12-25 18:18:01 | [diff] [blame] | 81 | errors.push_back(base::UTF16ToUTF8(message)); |
[email protected] | 50d7d728 | 2009-03-02 21:45:18 | [diff] [blame] | 82 | errors_line_number.push_back(line_number); |
| 83 | } |
| 84 | |
| 85 | // Mock values to return. |
| 86 | std::string my_ip_address_result; |
[email protected] | 21f20c89 | 2009-10-26 23:51:28 | [diff] [blame] | 87 | std::string my_ip_address_ex_result; |
[email protected] | 50d7d728 | 2009-03-02 21:45:18 | [diff] [blame] | 88 | std::string dns_resolve_result; |
[email protected] | 21f20c89 | 2009-10-26 23:51:28 | [diff] [blame] | 89 | std::string dns_resolve_ex_result; |
[email protected] | 50d7d728 | 2009-03-02 21:45:18 | [diff] [blame] | 90 | |
| 91 | // Inputs we got called with. |
| 92 | std::vector<std::string> alerts; |
| 93 | std::vector<std::string> errors; |
| 94 | std::vector<int> errors_line_number; |
| 95 | std::vector<std::string> dns_resolves; |
[email protected] | 21f20c89 | 2009-10-26 23:51:28 | [diff] [blame] | 96 | std::vector<std::string> dns_resolves_ex; |
[email protected] | 50d7d728 | 2009-03-02 21:45:18 | [diff] [blame] | 97 | int my_ip_address_count; |
[email protected] | 21f20c89 | 2009-10-26 23:51:28 | [diff] [blame] | 98 | int my_ip_address_ex_count; |
[email protected] | 8cdc881 | 2013-02-21 05:29:49 | [diff] [blame] | 99 | |
| 100 | // Whether ResolveDns() should terminate script execution. |
| 101 | bool should_terminate; |
[email protected] | 50d7d728 | 2009-03-02 21:45:18 | [diff] [blame] | 102 | }; |
| 103 | |
Bence Béky | 98447b1 | 2018-05-08 03:14:01 | [diff] [blame] | 104 | class ProxyResolverV8Test : public TestWithScopedTaskEnvironment { |
[email protected] | 50d7d728 | 2009-03-02 21:45:18 | [diff] [blame] | 105 | public: |
sammc | d2284e5 | 2015-05-27 03:14:46 | [diff] [blame] | 106 | // Creates a ProxyResolverV8 using the PAC script contained in |filename|. If |
| 107 | // called more than once, the previous ProxyResolverV8 is deleted. |
| 108 | int CreateResolver(const char* filename) { |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 109 | base::FilePath path; |
Avi Drissman | 5c80d83 | 2018-05-01 17:01:19 | [diff] [blame] | 110 | base::PathService::Get(base::DIR_SOURCE_ROOT, &path); |
[email protected] | 50d7d728 | 2009-03-02 21:45:18 | [diff] [blame] | 111 | path = path.AppendASCII("net"); |
| 112 | path = path.AppendASCII("data"); |
| 113 | path = path.AppendASCII("proxy_resolver_v8_unittest"); |
| 114 | path = path.AppendASCII(filename); |
| 115 | |
| 116 | // Try to read the file from disk. |
| 117 | std::string file_contents; |
[email protected] | 82f84b9 | 2013-08-30 18:23:50 | [diff] [blame] | 118 | bool ok = base::ReadFileToString(path, &file_contents); |
[email protected] | 50d7d728 | 2009-03-02 21:45:18 | [diff] [blame] | 119 | |
| 120 | // If we can't load the file from disk, something is misconfigured. |
[email protected] | 620f571 | 2009-08-04 22:43:12 | [diff] [blame] | 121 | if (!ok) { |
| 122 | LOG(ERROR) << "Failed to read file: " << path.value(); |
sammc | d2284e5 | 2015-05-27 03:14:46 | [diff] [blame] | 123 | return ERR_FAILED; |
[email protected] | 620f571 | 2009-08-04 22:43:12 | [diff] [blame] | 124 | } |
[email protected] | 50d7d728 | 2009-03-02 21:45:18 | [diff] [blame] | 125 | |
sammc | d2284e5 | 2015-05-27 03:14:46 | [diff] [blame] | 126 | // Create the ProxyResolver using the PAC script. |
Lily Houghton | 9959786 | 2018-03-07 16:40:42 | [diff] [blame] | 127 | return ProxyResolverV8::Create(PacFileData::FromUTF8(file_contents), |
| 128 | bindings(), &resolver_); |
[email protected] | 50d7d728 | 2009-03-02 21:45:18 | [diff] [blame] | 129 | } |
[email protected] | 501e2d4 | 2013-01-30 22:30:49 | [diff] [blame] | 130 | |
sammc | d2284e5 | 2015-05-27 03:14:46 | [diff] [blame] | 131 | ProxyResolverV8& resolver() { |
| 132 | DCHECK(resolver_); |
| 133 | return *resolver_; |
| 134 | } |
| 135 | |
| 136 | MockJSBindings* bindings() { return &js_bindings_; } |
| 137 | |
[email protected] | 501e2d4 | 2013-01-30 22:30:49 | [diff] [blame] | 138 | private: |
sammc | d2284e5 | 2015-05-27 03:14:46 | [diff] [blame] | 139 | MockJSBindings js_bindings_; |
danakj | 8a98ca2 | 2016-04-16 02:47:36 | [diff] [blame] | 140 | std::unique_ptr<ProxyResolverV8> resolver_; |
[email protected] | 50d7d728 | 2009-03-02 21:45:18 | [diff] [blame] | 141 | }; |
[email protected] | 943c808 | 2009-02-23 19:10:45 | [diff] [blame] | 142 | |
| 143 | // Doesn't really matter what these values are for many of the tests. |
| 144 | const GURL kQueryUrl("http://www.google.com"); |
| 145 | const GURL kPacUrl; |
| 146 | |
sammc | d2284e5 | 2015-05-27 03:14:46 | [diff] [blame] | 147 | TEST_F(ProxyResolverV8Test, Direct) { |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 148 | ASSERT_THAT(CreateResolver("direct.js"), IsOk()); |
[email protected] | 943c808 | 2009-02-23 19:10:45 | [diff] [blame] | 149 | |
[email protected] | 52ae80c | 2009-09-10 21:27:00 | [diff] [blame] | 150 | ProxyInfo proxy_info; |
sammc | d2284e5 | 2015-05-27 03:14:46 | [diff] [blame] | 151 | int result = resolver().GetProxyForURL(kQueryUrl, &proxy_info, bindings()); |
[email protected] | 943c808 | 2009-02-23 19:10:45 | [diff] [blame] | 152 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 153 | EXPECT_THAT(result, IsOk()); |
[email protected] | 943c808 | 2009-02-23 19:10:45 | [diff] [blame] | 154 | EXPECT_TRUE(proxy_info.is_direct()); |
[email protected] | 50d7d728 | 2009-03-02 21:45:18 | [diff] [blame] | 155 | |
sammc | d2284e5 | 2015-05-27 03:14:46 | [diff] [blame] | 156 | EXPECT_EQ(0U, bindings()->alerts.size()); |
| 157 | EXPECT_EQ(0U, bindings()->errors.size()); |
[email protected] | 943c808 | 2009-02-23 19:10:45 | [diff] [blame] | 158 | } |
| 159 | |
sammc | d2284e5 | 2015-05-27 03:14:46 | [diff] [blame] | 160 | TEST_F(ProxyResolverV8Test, ReturnEmptyString) { |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 161 | ASSERT_THAT(CreateResolver("return_empty_string.js"), IsOk()); |
[email protected] | 943c808 | 2009-02-23 19:10:45 | [diff] [blame] | 162 | |
[email protected] | 52ae80c | 2009-09-10 21:27:00 | [diff] [blame] | 163 | ProxyInfo proxy_info; |
sammc | d2284e5 | 2015-05-27 03:14:46 | [diff] [blame] | 164 | int result = resolver().GetProxyForURL(kQueryUrl, &proxy_info, bindings()); |
[email protected] | 943c808 | 2009-02-23 19:10:45 | [diff] [blame] | 165 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 166 | EXPECT_THAT(result, IsOk()); |
[email protected] | 943c808 | 2009-02-23 19:10:45 | [diff] [blame] | 167 | EXPECT_TRUE(proxy_info.is_direct()); |
[email protected] | 50d7d728 | 2009-03-02 21:45:18 | [diff] [blame] | 168 | |
sammc | d2284e5 | 2015-05-27 03:14:46 | [diff] [blame] | 169 | EXPECT_EQ(0U, bindings()->alerts.size()); |
| 170 | EXPECT_EQ(0U, bindings()->errors.size()); |
[email protected] | 943c808 | 2009-02-23 19:10:45 | [diff] [blame] | 171 | } |
| 172 | |
sammc | d2284e5 | 2015-05-27 03:14:46 | [diff] [blame] | 173 | TEST_F(ProxyResolverV8Test, Basic) { |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 174 | ASSERT_THAT(CreateResolver("passthrough.js"), IsOk()); |
[email protected] | 943c808 | 2009-02-23 19:10:45 | [diff] [blame] | 175 | |
| 176 | // The "FindProxyForURL" of this PAC script simply concatenates all of the |
| 177 | // arguments into a pseudo-host. The purpose of this test is to verify that |
| 178 | // the correct arguments are being passed to FindProxyForURL(). |
| 179 | { |
[email protected] | 52ae80c | 2009-09-10 21:27:00 | [diff] [blame] | 180 | ProxyInfo proxy_info; |
sammc | d2284e5 | 2015-05-27 03:14:46 | [diff] [blame] | 181 | int result = resolver().GetProxyForURL(GURL("http://query.com/path"), |
| 182 | &proxy_info, bindings()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 183 | EXPECT_THAT(result, IsOk()); |
[email protected] | 943c808 | 2009-02-23 19:10:45 | [diff] [blame] | 184 | EXPECT_EQ("http.query.com.path.query.com:80", |
| 185 | proxy_info.proxy_server().ToURI()); |
| 186 | } |
| 187 | { |
[email protected] | 52ae80c | 2009-09-10 21:27:00 | [diff] [blame] | 188 | ProxyInfo proxy_info; |
sammc | d2284e5 | 2015-05-27 03:14:46 | [diff] [blame] | 189 | int result = resolver().GetProxyForURL(GURL("ftp://query.com:90/path"), |
| 190 | &proxy_info, bindings()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 191 | EXPECT_THAT(result, IsOk()); |
[email protected] | 943c808 | 2009-02-23 19:10:45 | [diff] [blame] | 192 | // Note that FindProxyForURL(url, host) does not expect |host| to contain |
| 193 | // the port number. |
| 194 | EXPECT_EQ("ftp.query.com.90.path.query.com:80", |
| 195 | proxy_info.proxy_server().ToURI()); |
[email protected] | 50d7d728 | 2009-03-02 21:45:18 | [diff] [blame] | 196 | |
sammc | d2284e5 | 2015-05-27 03:14:46 | [diff] [blame] | 197 | EXPECT_EQ(0U, bindings()->alerts.size()); |
| 198 | EXPECT_EQ(0U, bindings()->errors.size()); |
[email protected] | 943c808 | 2009-02-23 19:10:45 | [diff] [blame] | 199 | } |
| 200 | } |
| 201 | |
sammc | d2284e5 | 2015-05-27 03:14:46 | [diff] [blame] | 202 | TEST_F(ProxyResolverV8Test, BadReturnType) { |
[email protected] | 943c808 | 2009-02-23 19:10:45 | [diff] [blame] | 203 | // These are the filenames of PAC scripts which each return a non-string |
| 204 | // types for FindProxyForURL(). They should all fail with |
| 205 | // ERR_PAC_SCRIPT_FAILED. |
| 206 | static const char* const filenames[] = { |
| 207 | "return_undefined.js", |
| 208 | "return_integer.js", |
| 209 | "return_function.js", |
| 210 | "return_object.js", |
| 211 | // TODO(eroman): Should 'null' be considered equivalent to "DIRECT" ? |
sammc | d2284e5 | 2015-05-27 03:14:46 | [diff] [blame] | 212 | "return_null.js"}; |
[email protected] | 943c808 | 2009-02-23 19:10:45 | [diff] [blame] | 213 | |
| 214 | for (size_t i = 0; i < arraysize(filenames); ++i) { |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 215 | ASSERT_THAT(CreateResolver(filenames[i]), IsOk()); |
[email protected] | 943c808 | 2009-02-23 19:10:45 | [diff] [blame] | 216 | |
sammc | d2284e5 | 2015-05-27 03:14:46 | [diff] [blame] | 217 | MockJSBindings bindings; |
[email protected] | 52ae80c | 2009-09-10 21:27:00 | [diff] [blame] | 218 | ProxyInfo proxy_info; |
sammc | d2284e5 | 2015-05-27 03:14:46 | [diff] [blame] | 219 | int result = resolver().GetProxyForURL(kQueryUrl, &proxy_info, &bindings); |
[email protected] | 943c808 | 2009-02-23 19:10:45 | [diff] [blame] | 220 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 221 | EXPECT_THAT(result, IsError(ERR_PAC_SCRIPT_FAILED)); |
[email protected] | 50d7d728 | 2009-03-02 21:45:18 | [diff] [blame] | 222 | |
sammc | d2284e5 | 2015-05-27 03:14:46 | [diff] [blame] | 223 | EXPECT_EQ(0U, bindings.alerts.size()); |
| 224 | ASSERT_EQ(1U, bindings.errors.size()); |
| 225 | EXPECT_EQ("FindProxyForURL() did not return a string.", bindings.errors[0]); |
| 226 | EXPECT_EQ(-1, bindings.errors_line_number[0]); |
[email protected] | 943c808 | 2009-02-23 19:10:45 | [diff] [blame] | 227 | } |
| 228 | } |
| 229 | |
| 230 | // Try using a PAC script which defines no "FindProxyForURL" function. |
sammc | d2284e5 | 2015-05-27 03:14:46 | [diff] [blame] | 231 | TEST_F(ProxyResolverV8Test, NoEntryPoint) { |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 232 | EXPECT_THAT(CreateResolver("no_entrypoint.js"), |
| 233 | IsError(ERR_PAC_SCRIPT_FAILED)); |
[email protected] | 50d7d728 | 2009-03-02 21:45:18 | [diff] [blame] | 234 | |
sammc | d2284e5 | 2015-05-27 03:14:46 | [diff] [blame] | 235 | ASSERT_EQ(1U, bindings()->errors.size()); |
eroman | 8df9fd80 | 2015-02-27 23:26:28 | [diff] [blame] | 236 | EXPECT_EQ("FindProxyForURL is undefined or not a function.", |
sammc | d2284e5 | 2015-05-27 03:14:46 | [diff] [blame] | 237 | bindings()->errors[0]); |
| 238 | EXPECT_EQ(-1, bindings()->errors_line_number[0]); |
[email protected] | 943c808 | 2009-02-23 19:10:45 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | // Try loading a malformed PAC script. |
sammc | d2284e5 | 2015-05-27 03:14:46 | [diff] [blame] | 242 | TEST_F(ProxyResolverV8Test, ParseError) { |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 243 | EXPECT_THAT(CreateResolver("missing_close_brace.js"), |
| 244 | IsError(ERR_PAC_SCRIPT_FAILED)); |
[email protected] | 943c808 | 2009-02-23 19:10:45 | [diff] [blame] | 245 | |
sammc | d2284e5 | 2015-05-27 03:14:46 | [diff] [blame] | 246 | EXPECT_EQ(0U, bindings()->alerts.size()); |
[email protected] | 50d7d728 | 2009-03-02 21:45:18 | [diff] [blame] | 247 | |
[email protected] | 620f571 | 2009-08-04 22:43:12 | [diff] [blame] | 248 | // We get one error during compilation. |
sammc | d2284e5 | 2015-05-27 03:14:46 | [diff] [blame] | 249 | ASSERT_EQ(1U, bindings()->errors.size()); |
[email protected] | 50d7d728 | 2009-03-02 21:45:18 | [diff] [blame] | 250 | |
| 251 | EXPECT_EQ("Uncaught SyntaxError: Unexpected end of input", |
sammc | d2284e5 | 2015-05-27 03:14:46 | [diff] [blame] | 252 | bindings()->errors[0]); |
| 253 | EXPECT_EQ(5, bindings()->errors_line_number[0]); |
[email protected] | 943c808 | 2009-02-23 19:10:45 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | // Run a PAC script several times, which has side-effects. |
sammc | d2284e5 | 2015-05-27 03:14:46 | [diff] [blame] | 257 | TEST_F(ProxyResolverV8Test, SideEffects) { |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 258 | ASSERT_THAT(CreateResolver("side_effects.js"), IsOk()); |
[email protected] | 943c808 | 2009-02-23 19:10:45 | [diff] [blame] | 259 | |
| 260 | // The PAC script increments a counter each time we invoke it. |
| 261 | for (int i = 0; i < 3; ++i) { |
[email protected] | 52ae80c | 2009-09-10 21:27:00 | [diff] [blame] | 262 | ProxyInfo proxy_info; |
sammc | d2284e5 | 2015-05-27 03:14:46 | [diff] [blame] | 263 | int result = resolver().GetProxyForURL(kQueryUrl, &proxy_info, bindings()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 264 | EXPECT_THAT(result, IsOk()); |
[email protected] | d8eb8424 | 2010-09-25 02:25:06 | [diff] [blame] | 265 | EXPECT_EQ(base::StringPrintf("sideffect_%d:80", i), |
[email protected] | 943c808 | 2009-02-23 19:10:45 | [diff] [blame] | 266 | proxy_info.proxy_server().ToURI()); |
| 267 | } |
| 268 | |
| 269 | // Reload the script -- the javascript environment should be reset, hence |
| 270 | // the counter starts over. |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 271 | ASSERT_THAT(CreateResolver("side_effects.js"), IsOk()); |
[email protected] | 943c808 | 2009-02-23 19:10:45 | [diff] [blame] | 272 | |
| 273 | for (int i = 0; i < 3; ++i) { |
[email protected] | 52ae80c | 2009-09-10 21:27:00 | [diff] [blame] | 274 | ProxyInfo proxy_info; |
sammc | d2284e5 | 2015-05-27 03:14:46 | [diff] [blame] | 275 | int result = resolver().GetProxyForURL(kQueryUrl, &proxy_info, bindings()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 276 | EXPECT_THAT(result, IsOk()); |
[email protected] | d8eb8424 | 2010-09-25 02:25:06 | [diff] [blame] | 277 | EXPECT_EQ(base::StringPrintf("sideffect_%d:80", i), |
[email protected] | 943c808 | 2009-02-23 19:10:45 | [diff] [blame] | 278 | proxy_info.proxy_server().ToURI()); |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | // Execute a PAC script which throws an exception in FindProxyForURL. |
sammc | d2284e5 | 2015-05-27 03:14:46 | [diff] [blame] | 283 | TEST_F(ProxyResolverV8Test, UnhandledException) { |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 284 | ASSERT_THAT(CreateResolver("unhandled_exception.js"), IsOk()); |
[email protected] | 943c808 | 2009-02-23 19:10:45 | [diff] [blame] | 285 | |
[email protected] | 52ae80c | 2009-09-10 21:27:00 | [diff] [blame] | 286 | ProxyInfo proxy_info; |
sammc | d2284e5 | 2015-05-27 03:14:46 | [diff] [blame] | 287 | int result = resolver().GetProxyForURL(kQueryUrl, &proxy_info, bindings()); |
[email protected] | 943c808 | 2009-02-23 19:10:45 | [diff] [blame] | 288 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 289 | EXPECT_THAT(result, IsError(ERR_PAC_SCRIPT_FAILED)); |
[email protected] | 50d7d728 | 2009-03-02 21:45:18 | [diff] [blame] | 290 | |
sammc | d2284e5 | 2015-05-27 03:14:46 | [diff] [blame] | 291 | EXPECT_EQ(0U, bindings()->alerts.size()); |
| 292 | ASSERT_EQ(1U, bindings()->errors.size()); |
[email protected] | 50d7d728 | 2009-03-02 21:45:18 | [diff] [blame] | 293 | EXPECT_EQ("Uncaught ReferenceError: undefined_variable is not defined", |
sammc | d2284e5 | 2015-05-27 03:14:46 | [diff] [blame] | 294 | bindings()->errors[0]); |
| 295 | EXPECT_EQ(3, bindings()->errors_line_number[0]); |
[email protected] | 943c808 | 2009-02-23 19:10:45 | [diff] [blame] | 296 | } |
| 297 | |
eroman | 8df9fd80 | 2015-02-27 23:26:28 | [diff] [blame] | 298 | // Execute a PAC script which throws an exception when first accessing |
| 299 | // FindProxyForURL |
sammc | d2284e5 | 2015-05-27 03:14:46 | [diff] [blame] | 300 | TEST_F(ProxyResolverV8Test, ExceptionAccessingFindProxyForURLDuringInit) { |
| 301 | EXPECT_EQ(ERR_PAC_SCRIPT_FAILED, |
| 302 | CreateResolver("exception_findproxyforurl_during_init.js")); |
eroman | 8df9fd80 | 2015-02-27 23:26:28 | [diff] [blame] | 303 | |
sammc | d2284e5 | 2015-05-27 03:14:46 | [diff] [blame] | 304 | ASSERT_EQ(2U, bindings()->errors.size()); |
| 305 | EXPECT_EQ("Uncaught crash!", bindings()->errors[0]); |
| 306 | EXPECT_EQ(9, bindings()->errors_line_number[0]); |
eroman | 8df9fd80 | 2015-02-27 23:26:28 | [diff] [blame] | 307 | EXPECT_EQ("Accessing FindProxyForURL threw an exception.", |
sammc | d2284e5 | 2015-05-27 03:14:46 | [diff] [blame] | 308 | bindings()->errors[1]); |
| 309 | EXPECT_EQ(-1, bindings()->errors_line_number[1]); |
eroman | 8df9fd80 | 2015-02-27 23:26:28 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | // Execute a PAC script which throws an exception during the second access to |
| 313 | // FindProxyForURL |
sammc | d2284e5 | 2015-05-27 03:14:46 | [diff] [blame] | 314 | TEST_F(ProxyResolverV8Test, ExceptionAccessingFindProxyForURLDuringResolve) { |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 315 | ASSERT_THAT(CreateResolver("exception_findproxyforurl_during_resolve.js"), |
| 316 | IsOk()); |
eroman | 8df9fd80 | 2015-02-27 23:26:28 | [diff] [blame] | 317 | |
| 318 | ProxyInfo proxy_info; |
sammc | d2284e5 | 2015-05-27 03:14:46 | [diff] [blame] | 319 | int result = resolver().GetProxyForURL(kQueryUrl, &proxy_info, bindings()); |
eroman | 8df9fd80 | 2015-02-27 23:26:28 | [diff] [blame] | 320 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 321 | EXPECT_THAT(result, IsError(ERR_PAC_SCRIPT_FAILED)); |
eroman | 8df9fd80 | 2015-02-27 23:26:28 | [diff] [blame] | 322 | |
sammc | d2284e5 | 2015-05-27 03:14:46 | [diff] [blame] | 323 | ASSERT_EQ(2U, bindings()->errors.size()); |
| 324 | EXPECT_EQ("Uncaught crash!", bindings()->errors[0]); |
| 325 | EXPECT_EQ(17, bindings()->errors_line_number[0]); |
eroman | 8df9fd80 | 2015-02-27 23:26:28 | [diff] [blame] | 326 | EXPECT_EQ("Accessing FindProxyForURL threw an exception.", |
sammc | d2284e5 | 2015-05-27 03:14:46 | [diff] [blame] | 327 | bindings()->errors[1]); |
| 328 | EXPECT_EQ(-1, bindings()->errors_line_number[1]); |
eroman | 8df9fd80 | 2015-02-27 23:26:28 | [diff] [blame] | 329 | } |
| 330 | |
sammc | d2284e5 | 2015-05-27 03:14:46 | [diff] [blame] | 331 | TEST_F(ProxyResolverV8Test, ReturnUnicode) { |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 332 | ASSERT_THAT(CreateResolver("return_unicode.js"), IsOk()); |
[email protected] | 943c808 | 2009-02-23 19:10:45 | [diff] [blame] | 333 | |
[email protected] | 52ae80c | 2009-09-10 21:27:00 | [diff] [blame] | 334 | ProxyInfo proxy_info; |
sammc | d2284e5 | 2015-05-27 03:14:46 | [diff] [blame] | 335 | int result = resolver().GetProxyForURL(kQueryUrl, &proxy_info, bindings()); |
[email protected] | 943c808 | 2009-02-23 19:10:45 | [diff] [blame] | 336 | |
| 337 | // The result from this resolve was unparseable, because it |
[email protected] | 51e413c | 2010-06-23 00:15:58 | [diff] [blame] | 338 | // wasn't ASCII. |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 339 | EXPECT_THAT(result, IsError(ERR_PAC_SCRIPT_FAILED)); |
[email protected] | 943c808 | 2009-02-23 19:10:45 | [diff] [blame] | 340 | } |
| 341 | |
[email protected] | 23578681 | 2011-12-20 02:15:31 | [diff] [blame] | 342 | // Test the PAC library functions that we expose in the JS environment. |
sammc | d2284e5 | 2015-05-27 03:14:46 | [diff] [blame] | 343 | TEST_F(ProxyResolverV8Test, JavascriptLibrary) { |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 344 | ASSERT_THAT(CreateResolver("pac_library_unittest.js"), IsOk()); |
[email protected] | 943c808 | 2009-02-23 19:10:45 | [diff] [blame] | 345 | |
[email protected] | 52ae80c | 2009-09-10 21:27:00 | [diff] [blame] | 346 | ProxyInfo proxy_info; |
sammc | d2284e5 | 2015-05-27 03:14:46 | [diff] [blame] | 347 | int result = resolver().GetProxyForURL(kQueryUrl, &proxy_info, bindings()); |
[email protected] | 943c808 | 2009-02-23 19:10:45 | [diff] [blame] | 348 | |
| 349 | // If the javascript side of this unit-test fails, it will throw a javascript |
| 350 | // exception. Otherwise it will return "PROXY success:80". |
Eric Roman | 9513f52 | 2018-04-04 23:49:47 | [diff] [blame] | 351 | EXPECT_THAT(bindings()->alerts, IsEmpty()); |
| 352 | EXPECT_THAT(bindings()->errors, IsEmpty()); |
[email protected] | 50d7d728 | 2009-03-02 21:45:18 | [diff] [blame] | 353 | |
Eric Roman | 9513f52 | 2018-04-04 23:49:47 | [diff] [blame] | 354 | ASSERT_THAT(result, IsOk()); |
| 355 | EXPECT_EQ("success:80", proxy_info.proxy_server().ToURI()); |
[email protected] | 50d7d728 | 2009-03-02 21:45:18 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | // Test marshalling/un-marshalling of values between C++/V8. |
sammc | d2284e5 | 2015-05-27 03:14:46 | [diff] [blame] | 359 | TEST_F(ProxyResolverV8Test, V8Bindings) { |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 360 | ASSERT_THAT(CreateResolver("bindings.js"), IsOk()); |
sammc | d2284e5 | 2015-05-27 03:14:46 | [diff] [blame] | 361 | bindings()->dns_resolve_result = "127.0.0.1"; |
[email protected] | 50d7d728 | 2009-03-02 21:45:18 | [diff] [blame] | 362 | |
[email protected] | 52ae80c | 2009-09-10 21:27:00 | [diff] [blame] | 363 | ProxyInfo proxy_info; |
sammc | d2284e5 | 2015-05-27 03:14:46 | [diff] [blame] | 364 | int result = resolver().GetProxyForURL(kQueryUrl, &proxy_info, bindings()); |
[email protected] | 50d7d728 | 2009-03-02 21:45:18 | [diff] [blame] | 365 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 366 | EXPECT_THAT(result, IsOk()); |
[email protected] | 50d7d728 | 2009-03-02 21:45:18 | [diff] [blame] | 367 | EXPECT_TRUE(proxy_info.is_direct()); |
| 368 | |
sammc | d2284e5 | 2015-05-27 03:14:46 | [diff] [blame] | 369 | EXPECT_EQ(0U, bindings()->errors.size()); |
[email protected] | 50d7d728 | 2009-03-02 21:45:18 | [diff] [blame] | 370 | |
| 371 | // Alert was called 5 times. |
sammc | d2284e5 | 2015-05-27 03:14:46 | [diff] [blame] | 372 | ASSERT_EQ(5U, bindings()->alerts.size()); |
| 373 | EXPECT_EQ("undefined", bindings()->alerts[0]); |
| 374 | EXPECT_EQ("null", bindings()->alerts[1]); |
| 375 | EXPECT_EQ("undefined", bindings()->alerts[2]); |
| 376 | EXPECT_EQ("[object Object]", bindings()->alerts[3]); |
| 377 | EXPECT_EQ("exception from calling toString()", bindings()->alerts[4]); |
[email protected] | 50d7d728 | 2009-03-02 21:45:18 | [diff] [blame] | 378 | |
[email protected] | d35c366 | 2010-05-05 20:04:34 | [diff] [blame] | 379 | // DnsResolve was called 8 times, however only 2 of those were string |
| 380 | // parameters. (so 6 of them failed immediately). |
sammc | d2284e5 | 2015-05-27 03:14:46 | [diff] [blame] | 381 | ASSERT_EQ(2U, bindings()->dns_resolves.size()); |
| 382 | EXPECT_EQ("", bindings()->dns_resolves[0]); |
| 383 | EXPECT_EQ("arg1", bindings()->dns_resolves[1]); |
[email protected] | 50d7d728 | 2009-03-02 21:45:18 | [diff] [blame] | 384 | |
| 385 | // MyIpAddress was called two times. |
sammc | d2284e5 | 2015-05-27 03:14:46 | [diff] [blame] | 386 | EXPECT_EQ(2, bindings()->my_ip_address_count); |
[email protected] | 21f20c89 | 2009-10-26 23:51:28 | [diff] [blame] | 387 | |
| 388 | // MyIpAddressEx was called once. |
sammc | d2284e5 | 2015-05-27 03:14:46 | [diff] [blame] | 389 | EXPECT_EQ(1, bindings()->my_ip_address_ex_count); |
[email protected] | 21f20c89 | 2009-10-26 23:51:28 | [diff] [blame] | 390 | |
| 391 | // DnsResolveEx was called 2 times. |
sammc | d2284e5 | 2015-05-27 03:14:46 | [diff] [blame] | 392 | ASSERT_EQ(2U, bindings()->dns_resolves_ex.size()); |
| 393 | EXPECT_EQ("is_resolvable", bindings()->dns_resolves_ex[0]); |
| 394 | EXPECT_EQ("foobar", bindings()->dns_resolves_ex[1]); |
[email protected] | 50d7d728 | 2009-03-02 21:45:18 | [diff] [blame] | 395 | } |
| 396 | |
[email protected] | a5e9f2c | 2010-03-31 22:10:11 | [diff] [blame] | 397 | // Test calling a binding (myIpAddress()) from the script's global scope. |
| 398 | // http://crbug.com/40026 |
sammc | d2284e5 | 2015-05-27 03:14:46 | [diff] [blame] | 399 | TEST_F(ProxyResolverV8Test, BindingCalledDuringInitialization) { |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 400 | ASSERT_THAT(CreateResolver("binding_from_global.js"), IsOk()); |
[email protected] | a5e9f2c | 2010-03-31 22:10:11 | [diff] [blame] | 401 | |
| 402 | // myIpAddress() got called during initialization of the script. |
sammc | d2284e5 | 2015-05-27 03:14:46 | [diff] [blame] | 403 | EXPECT_EQ(1, bindings()->my_ip_address_count); |
[email protected] | a5e9f2c | 2010-03-31 22:10:11 | [diff] [blame] | 404 | |
| 405 | ProxyInfo proxy_info; |
sammc | d2284e5 | 2015-05-27 03:14:46 | [diff] [blame] | 406 | int result = resolver().GetProxyForURL(kQueryUrl, &proxy_info, bindings()); |
[email protected] | a5e9f2c | 2010-03-31 22:10:11 | [diff] [blame] | 407 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 408 | EXPECT_THAT(result, IsOk()); |
[email protected] | a5e9f2c | 2010-03-31 22:10:11 | [diff] [blame] | 409 | EXPECT_FALSE(proxy_info.is_direct()); |
| 410 | EXPECT_EQ("127.0.0.1:80", proxy_info.proxy_server().ToURI()); |
| 411 | |
| 412 | // Check that no other bindings were called. |
sammc | d2284e5 | 2015-05-27 03:14:46 | [diff] [blame] | 413 | EXPECT_EQ(0U, bindings()->errors.size()); |
| 414 | ASSERT_EQ(0U, bindings()->alerts.size()); |
| 415 | ASSERT_EQ(0U, bindings()->dns_resolves.size()); |
| 416 | EXPECT_EQ(0, bindings()->my_ip_address_ex_count); |
| 417 | ASSERT_EQ(0U, bindings()->dns_resolves_ex.size()); |
[email protected] | a5e9f2c | 2010-03-31 22:10:11 | [diff] [blame] | 418 | } |
| 419 | |
[email protected] | cd88b0c | 2009-09-25 04:52:39 | [diff] [blame] | 420 | // Try loading a PAC script that ends with a comment and has no terminal |
| 421 | // newline. This should not cause problems with the PAC utility functions |
| 422 | // that we add to the script's environment. |
[email protected] | c945a46 | 2009-09-24 02:13:57 | [diff] [blame] | 423 | // http://crbug.com/22864 |
sammc | d2284e5 | 2015-05-27 03:14:46 | [diff] [blame] | 424 | TEST_F(ProxyResolverV8Test, EndsWithCommentNoNewline) { |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 425 | ASSERT_THAT(CreateResolver("ends_with_comment.js"), IsOk()); |
[email protected] | c945a46 | 2009-09-24 02:13:57 | [diff] [blame] | 426 | |
| 427 | ProxyInfo proxy_info; |
sammc | d2284e5 | 2015-05-27 03:14:46 | [diff] [blame] | 428 | int result = resolver().GetProxyForURL(kQueryUrl, &proxy_info, bindings()); |
[email protected] | c945a46 | 2009-09-24 02:13:57 | [diff] [blame] | 429 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 430 | EXPECT_THAT(result, IsOk()); |
[email protected] | c945a46 | 2009-09-24 02:13:57 | [diff] [blame] | 431 | EXPECT_FALSE(proxy_info.is_direct()); |
| 432 | EXPECT_EQ("success:80", proxy_info.proxy_server().ToURI()); |
| 433 | } |
| 434 | |
[email protected] | cd88b0c | 2009-09-25 04:52:39 | [diff] [blame] | 435 | // Try loading a PAC script that ends with a statement and has no terminal |
| 436 | // newline. This should not cause problems with the PAC utility functions |
| 437 | // that we add to the script's environment. |
| 438 | // http://crbug.com/22864 |
sammc | d2284e5 | 2015-05-27 03:14:46 | [diff] [blame] | 439 | TEST_F(ProxyResolverV8Test, EndsWithStatementNoNewline) { |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 440 | ASSERT_THAT(CreateResolver("ends_with_statement_no_semicolon.js"), IsOk()); |
[email protected] | cd88b0c | 2009-09-25 04:52:39 | [diff] [blame] | 441 | |
| 442 | ProxyInfo proxy_info; |
sammc | d2284e5 | 2015-05-27 03:14:46 | [diff] [blame] | 443 | int result = resolver().GetProxyForURL(kQueryUrl, &proxy_info, bindings()); |
[email protected] | cd88b0c | 2009-09-25 04:52:39 | [diff] [blame] | 444 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 445 | EXPECT_THAT(result, IsOk()); |
[email protected] | cd88b0c | 2009-09-25 04:52:39 | [diff] [blame] | 446 | EXPECT_FALSE(proxy_info.is_direct()); |
| 447 | EXPECT_EQ("success:3", proxy_info.proxy_server().ToURI()); |
| 448 | } |
| 449 | |
[email protected] | 21f20c89 | 2009-10-26 23:51:28 | [diff] [blame] | 450 | // Test the return values from myIpAddress(), myIpAddressEx(), dnsResolve(), |
| 451 | // dnsResolveEx(), isResolvable(), isResolvableEx(), when the the binding |
| 452 | // returns empty string (failure). This simulates the return values from |
| 453 | // those functions when the underlying DNS resolution fails. |
sammc | d2284e5 | 2015-05-27 03:14:46 | [diff] [blame] | 454 | TEST_F(ProxyResolverV8Test, DNSResolutionFailure) { |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 455 | ASSERT_THAT(CreateResolver("dns_fail.js"), IsOk()); |
[email protected] | 21f20c89 | 2009-10-26 23:51:28 | [diff] [blame] | 456 | |
| 457 | ProxyInfo proxy_info; |
sammc | d2284e5 | 2015-05-27 03:14:46 | [diff] [blame] | 458 | int result = resolver().GetProxyForURL(kQueryUrl, &proxy_info, bindings()); |
[email protected] | 21f20c89 | 2009-10-26 23:51:28 | [diff] [blame] | 459 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 460 | EXPECT_THAT(result, IsOk()); |
[email protected] | 21f20c89 | 2009-10-26 23:51:28 | [diff] [blame] | 461 | EXPECT_FALSE(proxy_info.is_direct()); |
| 462 | EXPECT_EQ("success:80", proxy_info.proxy_server().ToURI()); |
| 463 | } |
| 464 | |
sammc | d2284e5 | 2015-05-27 03:14:46 | [diff] [blame] | 465 | TEST_F(ProxyResolverV8Test, DNSResolutionOfInternationDomainName) { |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 466 | ASSERT_THAT(CreateResolver("international_domain_names.js"), IsOk()); |
[email protected] | 0238de4 | 2010-06-23 18:04:01 | [diff] [blame] | 467 | |
| 468 | // Execute FindProxyForURL(). |
| 469 | ProxyInfo proxy_info; |
sammc | d2284e5 | 2015-05-27 03:14:46 | [diff] [blame] | 470 | int result = resolver().GetProxyForURL(kQueryUrl, &proxy_info, bindings()); |
[email protected] | 0238de4 | 2010-06-23 18:04:01 | [diff] [blame] | 471 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 472 | EXPECT_THAT(result, IsOk()); |
[email protected] | 0238de4 | 2010-06-23 18:04:01 | [diff] [blame] | 473 | EXPECT_TRUE(proxy_info.is_direct()); |
| 474 | |
| 475 | // Check that the international domain name was converted to punycode |
| 476 | // before passing it onto the bindings layer. |
sammc | d2284e5 | 2015-05-27 03:14:46 | [diff] [blame] | 477 | ASSERT_EQ(1u, bindings()->dns_resolves.size()); |
| 478 | EXPECT_EQ("xn--bcher-kva.ch", bindings()->dns_resolves[0]); |
[email protected] | 0238de4 | 2010-06-23 18:04:01 | [diff] [blame] | 479 | |
sammc | d2284e5 | 2015-05-27 03:14:46 | [diff] [blame] | 480 | ASSERT_EQ(1u, bindings()->dns_resolves_ex.size()); |
| 481 | EXPECT_EQ("xn--bcher-kva.ch", bindings()->dns_resolves_ex[0]); |
[email protected] | 0238de4 | 2010-06-23 18:04:01 | [diff] [blame] | 482 | } |
| 483 | |
[email protected] | ad425c5 | 2012-02-11 00:05:31 | [diff] [blame] | 484 | // Test that when resolving a URL which contains an IPv6 string literal, the |
| 485 | // brackets are removed from the host before passing it down to the PAC script. |
| 486 | // If we don't do this, then subsequent calls to dnsResolveEx(host) will be |
| 487 | // doomed to fail since it won't correspond with a valid name. |
sammc | d2284e5 | 2015-05-27 03:14:46 | [diff] [blame] | 488 | TEST_F(ProxyResolverV8Test, IPv6HostnamesNotBracketed) { |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 489 | ASSERT_THAT(CreateResolver("resolve_host.js"), IsOk()); |
[email protected] | ad425c5 | 2012-02-11 00:05:31 | [diff] [blame] | 490 | |
| 491 | ProxyInfo proxy_info; |
sammc | d2284e5 | 2015-05-27 03:14:46 | [diff] [blame] | 492 | int result = resolver().GetProxyForURL( |
| 493 | GURL("http://[abcd::efff]:99/watsupdawg"), &proxy_info, bindings()); |
[email protected] | ad425c5 | 2012-02-11 00:05:31 | [diff] [blame] | 494 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 495 | EXPECT_THAT(result, IsOk()); |
[email protected] | ad425c5 | 2012-02-11 00:05:31 | [diff] [blame] | 496 | EXPECT_TRUE(proxy_info.is_direct()); |
| 497 | |
| 498 | // We called dnsResolveEx() exactly once, by passing through the "host" |
| 499 | // argument to FindProxyForURL(). The brackets should have been stripped. |
sammc | d2284e5 | 2015-05-27 03:14:46 | [diff] [blame] | 500 | ASSERT_EQ(1U, bindings()->dns_resolves_ex.size()); |
| 501 | EXPECT_EQ("abcd::efff", bindings()->dns_resolves_ex[0]); |
[email protected] | ad425c5 | 2012-02-11 00:05:31 | [diff] [blame] | 502 | } |
| 503 | |
[email protected] | 8cdc881 | 2013-02-21 05:29:49 | [diff] [blame] | 504 | // Test that terminating a script within DnsResolve() leads to eventual |
| 505 | // termination of the script. Also test that repeatedly calling terminate is |
| 506 | // safe, and running the script again after termination still works. |
sammc | d2284e5 | 2015-05-27 03:14:46 | [diff] [blame] | 507 | TEST_F(ProxyResolverV8Test, Terminate) { |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 508 | ASSERT_THAT(CreateResolver("terminate.js"), IsOk()); |
[email protected] | 8cdc881 | 2013-02-21 05:29:49 | [diff] [blame] | 509 | |
| 510 | // Terminate script execution upon reaching dnsResolve(). Note that |
| 511 | // termination may not take effect right away (so the subsequent dnsResolve() |
| 512 | // and alert() may be run). |
sammc | d2284e5 | 2015-05-27 03:14:46 | [diff] [blame] | 513 | bindings()->should_terminate = true; |
[email protected] | 8cdc881 | 2013-02-21 05:29:49 | [diff] [blame] | 514 | |
| 515 | ProxyInfo proxy_info; |
sammc | d2284e5 | 2015-05-27 03:14:46 | [diff] [blame] | 516 | int result = |
| 517 | resolver().GetProxyForURL(GURL("http://hang/"), &proxy_info, bindings()); |
[email protected] | 8cdc881 | 2013-02-21 05:29:49 | [diff] [blame] | 518 | |
| 519 | // The script execution was terminated. |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 520 | EXPECT_THAT(result, IsError(ERR_PAC_SCRIPT_FAILED)); |
[email protected] | 8cdc881 | 2013-02-21 05:29:49 | [diff] [blame] | 521 | |
sammc | d2284e5 | 2015-05-27 03:14:46 | [diff] [blame] | 522 | EXPECT_EQ(1U, bindings()->dns_resolves.size()); |
| 523 | EXPECT_GE(2U, bindings()->dns_resolves_ex.size()); |
| 524 | EXPECT_GE(1U, bindings()->alerts.size()); |
[email protected] | 8cdc881 | 2013-02-21 05:29:49 | [diff] [blame] | 525 | |
sammc | d2284e5 | 2015-05-27 03:14:46 | [diff] [blame] | 526 | EXPECT_EQ(1U, bindings()->errors.size()); |
[email protected] | 8cdc881 | 2013-02-21 05:29:49 | [diff] [blame] | 527 | |
| 528 | // Termination shows up as an uncaught exception without any message. |
sammc | d2284e5 | 2015-05-27 03:14:46 | [diff] [blame] | 529 | EXPECT_EQ("", bindings()->errors[0]); |
[email protected] | 8cdc881 | 2013-02-21 05:29:49 | [diff] [blame] | 530 | |
sammc | d2284e5 | 2015-05-27 03:14:46 | [diff] [blame] | 531 | bindings()->errors.clear(); |
[email protected] | 8cdc881 | 2013-02-21 05:29:49 | [diff] [blame] | 532 | |
| 533 | // Try running the script again, this time with a different input which won't |
| 534 | // cause a termination+hang. |
sammc | d2284e5 | 2015-05-27 03:14:46 | [diff] [blame] | 535 | result = resolver().GetProxyForURL(GURL("http://kittens/"), &proxy_info, |
| 536 | bindings()); |
[email protected] | 8cdc881 | 2013-02-21 05:29:49 | [diff] [blame] | 537 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 538 | EXPECT_THAT(result, IsOk()); |
sammc | d2284e5 | 2015-05-27 03:14:46 | [diff] [blame] | 539 | EXPECT_EQ(0u, bindings()->errors.size()); |
[email protected] | 8cdc881 | 2013-02-21 05:29:49 | [diff] [blame] | 540 | EXPECT_EQ("kittens:88", proxy_info.proxy_server().ToURI()); |
| 541 | } |
| 542 | |
[email protected] | 52ae80c | 2009-09-10 21:27:00 | [diff] [blame] | 543 | } // namespace |
| 544 | } // namespace net |