blob: f0bfa1db3cb7e6bb626d8e220b47d989c5f59265 [file] [log] [blame]
[email protected]943c8082009-02-23 19:10:451// 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
3// found in the LICENSE file.
4
5#include "base/file_util.h"
[email protected]943c8082009-02-23 19:10:456#include "base/path_service.h"
[email protected]51e413c2010-06-23 00:15:587#include "base/string_util.h"
8#include "base/utf_string_conversions.h"
[email protected]7dc52f22009-03-02 22:37:189#include "googleurl/src/gurl.h"
[email protected]943c8082009-02-23 19:10:4510#include "net/base/net_errors.h"
[email protected]72b4a772010-07-14 01:29:3011#include "net/base/net_log_unittest.h"
[email protected]7dc52f22009-03-02 22:37:1812#include "net/proxy/proxy_info.h"
[email protected]90ae1fb2009-08-02 21:07:1213#include "net/proxy/proxy_resolver_js_bindings.h"
14#include "net/proxy/proxy_resolver_v8.h"
[email protected]943c8082009-02-23 19:10:4515#include "testing/gtest/include/gtest/gtest.h"
16
[email protected]52ae80c2009-09-10 21:27:0017namespace net {
[email protected]943c8082009-02-23 19:10:4518namespace {
19
[email protected]50d7d7282009-03-02 21:45:1820// Javascript bindings for ProxyResolverV8, which returns mock values.
21// Each time one of the bindings is called into, we push the input into a
22// list, for later verification.
[email protected]52ae80c2009-09-10 21:27:0023class MockJSBindings : public ProxyResolverJSBindings {
[email protected]50d7d7282009-03-02 21:45:1824 public:
[email protected]21f20c892009-10-26 23:51:2825 MockJSBindings() : my_ip_address_count(0), my_ip_address_ex_count(0) {}
[email protected]943c8082009-02-23 19:10:4526
[email protected]51e413c2010-06-23 00:15:5827 virtual void Alert(const string16& message) {
[email protected]50d7d7282009-03-02 21:45:1828 LOG(INFO) << "PAC-alert: " << message; // Helpful when debugging.
[email protected]51e413c2010-06-23 00:15:5829 alerts.push_back(UTF16ToUTF8(message));
[email protected]50d7d7282009-03-02 21:45:1830 }
[email protected]943c8082009-02-23 19:10:4531
[email protected]51e413c2010-06-23 00:15:5832 virtual bool MyIpAddress(std::string* ip_address) {
[email protected]50d7d7282009-03-02 21:45:1833 my_ip_address_count++;
[email protected]51e413c2010-06-23 00:15:5834 *ip_address = my_ip_address_result;
35 return !my_ip_address_result.empty();
[email protected]50d7d7282009-03-02 21:45:1836 }
[email protected]943c8082009-02-23 19:10:4537
[email protected]51e413c2010-06-23 00:15:5838 virtual bool MyIpAddressEx(std::string* ip_address_list) {
[email protected]21f20c892009-10-26 23:51:2839 my_ip_address_ex_count++;
[email protected]51e413c2010-06-23 00:15:5840 *ip_address_list = my_ip_address_ex_result;
41 return !my_ip_address_ex_result.empty();
[email protected]21f20c892009-10-26 23:51:2842 }
43
[email protected]51e413c2010-06-23 00:15:5844 virtual bool DnsResolve(const std::string& host, std::string* ip_address) {
[email protected]50d7d7282009-03-02 21:45:1845 dns_resolves.push_back(host);
[email protected]51e413c2010-06-23 00:15:5846 *ip_address = dns_resolve_result;
47 return !dns_resolve_result.empty();
[email protected]50d7d7282009-03-02 21:45:1848 }
49
[email protected]51e413c2010-06-23 00:15:5850 virtual bool DnsResolveEx(const std::string& host,
51 std::string* ip_address_list) {
[email protected]21f20c892009-10-26 23:51:2852 dns_resolves_ex.push_back(host);
[email protected]51e413c2010-06-23 00:15:5853 *ip_address_list = dns_resolve_ex_result;
54 return !dns_resolve_ex_result.empty();
[email protected]21f20c892009-10-26 23:51:2855 }
56
[email protected]51e413c2010-06-23 00:15:5857 virtual void OnError(int line_number, const string16& message) {
[email protected]50d7d7282009-03-02 21:45:1858 // Helpful when debugging.
59 LOG(INFO) << "PAC-error: [" << line_number << "] " << message;
60
[email protected]51e413c2010-06-23 00:15:5861 errors.push_back(UTF16ToUTF8(message));
[email protected]50d7d7282009-03-02 21:45:1862 errors_line_number.push_back(line_number);
63 }
64
[email protected]61b84d52010-07-09 03:32:0065 virtual void Shutdown() {}
66
[email protected]50d7d7282009-03-02 21:45:1867 // Mock values to return.
68 std::string my_ip_address_result;
[email protected]21f20c892009-10-26 23:51:2869 std::string my_ip_address_ex_result;
[email protected]50d7d7282009-03-02 21:45:1870 std::string dns_resolve_result;
[email protected]21f20c892009-10-26 23:51:2871 std::string dns_resolve_ex_result;
[email protected]50d7d7282009-03-02 21:45:1872
73 // Inputs we got called with.
74 std::vector<std::string> alerts;
75 std::vector<std::string> errors;
76 std::vector<int> errors_line_number;
77 std::vector<std::string> dns_resolves;
[email protected]21f20c892009-10-26 23:51:2878 std::vector<std::string> dns_resolves_ex;
[email protected]50d7d7282009-03-02 21:45:1879 int my_ip_address_count;
[email protected]21f20c892009-10-26 23:51:2880 int my_ip_address_ex_count;
[email protected]50d7d7282009-03-02 21:45:1881};
82
83// This is the same as ProxyResolverV8, but it uses mock bindings in place of
84// the default bindings, and has a helper function to load PAC scripts from
85// disk.
[email protected]52ae80c2009-09-10 21:27:0086class ProxyResolverV8WithMockBindings : public ProxyResolverV8 {
[email protected]50d7d7282009-03-02 21:45:1887 public:
88 ProxyResolverV8WithMockBindings() : ProxyResolverV8(new MockJSBindings()) {}
89
90 MockJSBindings* mock_js_bindings() const {
91 return reinterpret_cast<MockJSBindings*>(js_bindings());
92 }
93
94 // Initialize with the PAC script data at |filename|.
[email protected]620f5712009-08-04 22:43:1295 int SetPacScriptFromDisk(const char* filename) {
[email protected]50d7d7282009-03-02 21:45:1896 FilePath path;
97 PathService::Get(base::DIR_SOURCE_ROOT, &path);
98 path = path.AppendASCII("net");
99 path = path.AppendASCII("data");
100 path = path.AppendASCII("proxy_resolver_v8_unittest");
101 path = path.AppendASCII(filename);
102
103 // Try to read the file from disk.
104 std::string file_contents;
105 bool ok = file_util::ReadFileToString(path, &file_contents);
106
107 // If we can't load the file from disk, something is misconfigured.
[email protected]620f5712009-08-04 22:43:12108 if (!ok) {
109 LOG(ERROR) << "Failed to read file: " << path.value();
[email protected]52ae80c2009-09-10 21:27:00110 return ERR_UNEXPECTED;
[email protected]620f5712009-08-04 22:43:12111 }
[email protected]50d7d7282009-03-02 21:45:18112
113 // Load the PAC script into the ProxyResolver.
[email protected]24476402010-07-20 20:55:17114 return SetPacScript(ProxyResolverScriptData::FromUTF8(file_contents),
115 NULL);
[email protected]50d7d7282009-03-02 21:45:18116 }
117};
[email protected]943c8082009-02-23 19:10:45118
119// Doesn't really matter what these values are for many of the tests.
120const GURL kQueryUrl("http://www.google.com");
121const GURL kPacUrl;
122
[email protected]943c8082009-02-23 19:10:45123
124TEST(ProxyResolverV8Test, Direct) {
[email protected]50d7d7282009-03-02 21:45:18125 ProxyResolverV8WithMockBindings resolver;
[email protected]620f5712009-08-04 22:43:12126 int result = resolver.SetPacScriptFromDisk("direct.js");
[email protected]52ae80c2009-09-10 21:27:00127 EXPECT_EQ(OK, result);
[email protected]943c8082009-02-23 19:10:45128
[email protected]52ae80c2009-09-10 21:27:00129 ProxyInfo proxy_info;
[email protected]9e743cd2010-03-16 07:03:53130 CapturingBoundNetLog log(CapturingNetLog::kUnbounded);
131 result = resolver.GetProxyForURL(kQueryUrl, &proxy_info, NULL, NULL,
132 log.bound());
[email protected]943c8082009-02-23 19:10:45133
[email protected]52ae80c2009-09-10 21:27:00134 EXPECT_EQ(OK, result);
[email protected]943c8082009-02-23 19:10:45135 EXPECT_TRUE(proxy_info.is_direct());
[email protected]50d7d7282009-03-02 21:45:18136
137 EXPECT_EQ(0U, resolver.mock_js_bindings()->alerts.size());
138 EXPECT_EQ(0U, resolver.mock_js_bindings()->errors.size());
[email protected]52ae80c2009-09-10 21:27:00139
140 // No bindings were called, so no log entries.
[email protected]9e743cd2010-03-16 07:03:53141 EXPECT_EQ(0u, log.entries().size());
[email protected]943c8082009-02-23 19:10:45142}
143
144TEST(ProxyResolverV8Test, ReturnEmptyString) {
[email protected]50d7d7282009-03-02 21:45:18145 ProxyResolverV8WithMockBindings resolver;
[email protected]620f5712009-08-04 22:43:12146 int result = resolver.SetPacScriptFromDisk("return_empty_string.js");
[email protected]52ae80c2009-09-10 21:27:00147 EXPECT_EQ(OK, result);
[email protected]943c8082009-02-23 19:10:45148
[email protected]52ae80c2009-09-10 21:27:00149 ProxyInfo proxy_info;
[email protected]5a1d7ca2010-04-28 20:12:27150 result = resolver.GetProxyForURL(kQueryUrl, &proxy_info, NULL, NULL,
151 BoundNetLog());
[email protected]943c8082009-02-23 19:10:45152
[email protected]52ae80c2009-09-10 21:27:00153 EXPECT_EQ(OK, result);
[email protected]943c8082009-02-23 19:10:45154 EXPECT_TRUE(proxy_info.is_direct());
[email protected]50d7d7282009-03-02 21:45:18155
156 EXPECT_EQ(0U, resolver.mock_js_bindings()->alerts.size());
157 EXPECT_EQ(0U, resolver.mock_js_bindings()->errors.size());
[email protected]943c8082009-02-23 19:10:45158}
159
160TEST(ProxyResolverV8Test, Basic) {
[email protected]50d7d7282009-03-02 21:45:18161 ProxyResolverV8WithMockBindings resolver;
[email protected]620f5712009-08-04 22:43:12162 int result = resolver.SetPacScriptFromDisk("passthrough.js");
[email protected]52ae80c2009-09-10 21:27:00163 EXPECT_EQ(OK, result);
[email protected]943c8082009-02-23 19:10:45164
165 // The "FindProxyForURL" of this PAC script simply concatenates all of the
166 // arguments into a pseudo-host. The purpose of this test is to verify that
167 // the correct arguments are being passed to FindProxyForURL().
168 {
[email protected]52ae80c2009-09-10 21:27:00169 ProxyInfo proxy_info;
[email protected]620f5712009-08-04 22:43:12170 result = resolver.GetProxyForURL(GURL("http://query.com/path"),
[email protected]5a1d7ca2010-04-28 20:12:27171 &proxy_info, NULL, NULL, BoundNetLog());
[email protected]52ae80c2009-09-10 21:27:00172 EXPECT_EQ(OK, result);
[email protected]943c8082009-02-23 19:10:45173 EXPECT_EQ("http.query.com.path.query.com:80",
174 proxy_info.proxy_server().ToURI());
175 }
176 {
[email protected]52ae80c2009-09-10 21:27:00177 ProxyInfo proxy_info;
[email protected]943c8082009-02-23 19:10:45178 int result = resolver.GetProxyForURL(GURL("ftp://query.com:90/path"),
[email protected]5a1d7ca2010-04-28 20:12:27179 &proxy_info, NULL, NULL,
180 BoundNetLog());
[email protected]52ae80c2009-09-10 21:27:00181 EXPECT_EQ(OK, result);
[email protected]943c8082009-02-23 19:10:45182 // Note that FindProxyForURL(url, host) does not expect |host| to contain
183 // the port number.
184 EXPECT_EQ("ftp.query.com.90.path.query.com:80",
185 proxy_info.proxy_server().ToURI());
[email protected]50d7d7282009-03-02 21:45:18186
187 EXPECT_EQ(0U, resolver.mock_js_bindings()->alerts.size());
188 EXPECT_EQ(0U, resolver.mock_js_bindings()->errors.size());
[email protected]943c8082009-02-23 19:10:45189 }
[email protected]0cf348be2009-10-13 01:39:58190
191 // We call this so we'll have code coverage of the function and valgrind will
192 // make sure nothing bad happens.
193 //
194 // NOTE: This is here instead of in its own test so that we'll be calling it
195 // after having done something, in hopes it won't be a no-op.
196 resolver.PurgeMemory();
[email protected]943c8082009-02-23 19:10:45197}
198
199TEST(ProxyResolverV8Test, BadReturnType) {
200 // These are the filenames of PAC scripts which each return a non-string
201 // types for FindProxyForURL(). They should all fail with
202 // ERR_PAC_SCRIPT_FAILED.
203 static const char* const filenames[] = {
204 "return_undefined.js",
205 "return_integer.js",
206 "return_function.js",
207 "return_object.js",
208 // TODO(eroman): Should 'null' be considered equivalent to "DIRECT" ?
209 "return_null.js"
210 };
211
212 for (size_t i = 0; i < arraysize(filenames); ++i) {
[email protected]50d7d7282009-03-02 21:45:18213 ProxyResolverV8WithMockBindings resolver;
[email protected]620f5712009-08-04 22:43:12214 int result = resolver.SetPacScriptFromDisk(filenames[i]);
[email protected]52ae80c2009-09-10 21:27:00215 EXPECT_EQ(OK, result);
[email protected]943c8082009-02-23 19:10:45216
[email protected]52ae80c2009-09-10 21:27:00217 ProxyInfo proxy_info;
[email protected]5a1d7ca2010-04-28 20:12:27218 result = resolver.GetProxyForURL(kQueryUrl, &proxy_info, NULL, NULL,
219 BoundNetLog());
[email protected]943c8082009-02-23 19:10:45220
[email protected]52ae80c2009-09-10 21:27:00221 EXPECT_EQ(ERR_PAC_SCRIPT_FAILED, result);
[email protected]50d7d7282009-03-02 21:45:18222
223 MockJSBindings* bindings = resolver.mock_js_bindings();
224 EXPECT_EQ(0U, bindings->alerts.size());
225 ASSERT_EQ(1U, bindings->errors.size());
226 EXPECT_EQ("FindProxyForURL() did not return a string.",
227 bindings->errors[0]);
228 EXPECT_EQ(-1, bindings->errors_line_number[0]);
[email protected]943c8082009-02-23 19:10:45229 }
230}
231
232// Try using a PAC script which defines no "FindProxyForURL" function.
233TEST(ProxyResolverV8Test, NoEntryPoint) {
[email protected]50d7d7282009-03-02 21:45:18234 ProxyResolverV8WithMockBindings resolver;
[email protected]620f5712009-08-04 22:43:12235 int result = resolver.SetPacScriptFromDisk("no_entrypoint.js");
[email protected]52ae80c2009-09-10 21:27:00236 EXPECT_EQ(ERR_PAC_SCRIPT_FAILED, result);
[email protected]50d7d7282009-03-02 21:45:18237
[email protected]52ae80c2009-09-10 21:27:00238 ProxyInfo proxy_info;
[email protected]5a1d7ca2010-04-28 20:12:27239 result = resolver.GetProxyForURL(kQueryUrl, &proxy_info, NULL, NULL,
240 BoundNetLog());
[email protected]620f5712009-08-04 22:43:12241
[email protected]52ae80c2009-09-10 21:27:00242 EXPECT_EQ(ERR_FAILED, result);
[email protected]943c8082009-02-23 19:10:45243}
244
245// Try loading a malformed PAC script.
246TEST(ProxyResolverV8Test, ParseError) {
[email protected]50d7d7282009-03-02 21:45:18247 ProxyResolverV8WithMockBindings resolver;
[email protected]620f5712009-08-04 22:43:12248 int result = resolver.SetPacScriptFromDisk("missing_close_brace.js");
[email protected]52ae80c2009-09-10 21:27:00249 EXPECT_EQ(ERR_PAC_SCRIPT_FAILED, result);
[email protected]943c8082009-02-23 19:10:45250
[email protected]52ae80c2009-09-10 21:27:00251 ProxyInfo proxy_info;
[email protected]5a1d7ca2010-04-28 20:12:27252 result = resolver.GetProxyForURL(kQueryUrl, &proxy_info, NULL, NULL,
253 BoundNetLog());
[email protected]943c8082009-02-23 19:10:45254
[email protected]52ae80c2009-09-10 21:27:00255 EXPECT_EQ(ERR_FAILED, result);
[email protected]50d7d7282009-03-02 21:45:18256
257 MockJSBindings* bindings = resolver.mock_js_bindings();
258 EXPECT_EQ(0U, bindings->alerts.size());
259
[email protected]620f5712009-08-04 22:43:12260 // We get one error during compilation.
261 ASSERT_EQ(1U, bindings->errors.size());
[email protected]50d7d7282009-03-02 21:45:18262
263 EXPECT_EQ("Uncaught SyntaxError: Unexpected end of input",
264 bindings->errors[0]);
[email protected]b72bdc12010-05-11 08:12:36265 EXPECT_EQ(0, bindings->errors_line_number[0]);
[email protected]943c8082009-02-23 19:10:45266}
267
268// Run a PAC script several times, which has side-effects.
269TEST(ProxyResolverV8Test, SideEffects) {
[email protected]50d7d7282009-03-02 21:45:18270 ProxyResolverV8WithMockBindings resolver;
[email protected]620f5712009-08-04 22:43:12271 int result = resolver.SetPacScriptFromDisk("side_effects.js");
[email protected]943c8082009-02-23 19:10:45272
273 // The PAC script increments a counter each time we invoke it.
274 for (int i = 0; i < 3; ++i) {
[email protected]52ae80c2009-09-10 21:27:00275 ProxyInfo proxy_info;
[email protected]5a1d7ca2010-04-28 20:12:27276 result = resolver.GetProxyForURL(kQueryUrl, &proxy_info, NULL, NULL,
277 BoundNetLog());
[email protected]52ae80c2009-09-10 21:27:00278 EXPECT_EQ(OK, result);
[email protected]943c8082009-02-23 19:10:45279 EXPECT_EQ(StringPrintf("sideffect_%d:80", i),
280 proxy_info.proxy_server().ToURI());
281 }
282
283 // Reload the script -- the javascript environment should be reset, hence
284 // the counter starts over.
[email protected]620f5712009-08-04 22:43:12285 result = resolver.SetPacScriptFromDisk("side_effects.js");
[email protected]52ae80c2009-09-10 21:27:00286 EXPECT_EQ(OK, result);
[email protected]943c8082009-02-23 19:10:45287
288 for (int i = 0; i < 3; ++i) {
[email protected]52ae80c2009-09-10 21:27:00289 ProxyInfo proxy_info;
[email protected]5a1d7ca2010-04-28 20:12:27290 result = resolver.GetProxyForURL(kQueryUrl, &proxy_info, NULL, NULL,
291 BoundNetLog());
[email protected]52ae80c2009-09-10 21:27:00292 EXPECT_EQ(OK, result);
[email protected]943c8082009-02-23 19:10:45293 EXPECT_EQ(StringPrintf("sideffect_%d:80", i),
294 proxy_info.proxy_server().ToURI());
295 }
296}
297
298// Execute a PAC script which throws an exception in FindProxyForURL.
299TEST(ProxyResolverV8Test, UnhandledException) {
[email protected]50d7d7282009-03-02 21:45:18300 ProxyResolverV8WithMockBindings resolver;
[email protected]620f5712009-08-04 22:43:12301 int result = resolver.SetPacScriptFromDisk("unhandled_exception.js");
[email protected]52ae80c2009-09-10 21:27:00302 EXPECT_EQ(OK, result);
[email protected]943c8082009-02-23 19:10:45303
[email protected]52ae80c2009-09-10 21:27:00304 ProxyInfo proxy_info;
[email protected]5a1d7ca2010-04-28 20:12:27305 result = resolver.GetProxyForURL(kQueryUrl, &proxy_info, NULL, NULL,
306 BoundNetLog());
[email protected]943c8082009-02-23 19:10:45307
[email protected]52ae80c2009-09-10 21:27:00308 EXPECT_EQ(ERR_PAC_SCRIPT_FAILED, result);
[email protected]50d7d7282009-03-02 21:45:18309
310 MockJSBindings* bindings = resolver.mock_js_bindings();
311 EXPECT_EQ(0U, bindings->alerts.size());
312 ASSERT_EQ(1U, bindings->errors.size());
313 EXPECT_EQ("Uncaught ReferenceError: undefined_variable is not defined",
314 bindings->errors[0]);
315 EXPECT_EQ(3, bindings->errors_line_number[0]);
[email protected]943c8082009-02-23 19:10:45316}
317
[email protected]51e413c2010-06-23 00:15:58318TEST(ProxyResolverV8Test, ReturnUnicode) {
[email protected]50d7d7282009-03-02 21:45:18319 ProxyResolverV8WithMockBindings resolver;
[email protected]620f5712009-08-04 22:43:12320 int result = resolver.SetPacScriptFromDisk("return_unicode.js");
[email protected]52ae80c2009-09-10 21:27:00321 EXPECT_EQ(OK, result);
[email protected]943c8082009-02-23 19:10:45322
[email protected]52ae80c2009-09-10 21:27:00323 ProxyInfo proxy_info;
[email protected]5a1d7ca2010-04-28 20:12:27324 result = resolver.GetProxyForURL(kQueryUrl, &proxy_info, NULL, NULL,
325 BoundNetLog());
[email protected]943c8082009-02-23 19:10:45326
327 // The result from this resolve was unparseable, because it
[email protected]51e413c2010-06-23 00:15:58328 // wasn't ASCII.
[email protected]52ae80c2009-09-10 21:27:00329 EXPECT_EQ(ERR_PAC_SCRIPT_FAILED, result);
[email protected]943c8082009-02-23 19:10:45330}
331
332// Test the PAC library functions that we expose in the JS environmnet.
333TEST(ProxyResolverV8Test, JavascriptLibrary) {
[email protected]50d7d7282009-03-02 21:45:18334 ProxyResolverV8WithMockBindings resolver;
[email protected]620f5712009-08-04 22:43:12335 int result = resolver.SetPacScriptFromDisk("pac_library_unittest.js");
[email protected]52ae80c2009-09-10 21:27:00336 EXPECT_EQ(OK, result);
[email protected]943c8082009-02-23 19:10:45337
[email protected]52ae80c2009-09-10 21:27:00338 ProxyInfo proxy_info;
[email protected]5a1d7ca2010-04-28 20:12:27339 result = resolver.GetProxyForURL(kQueryUrl, &proxy_info, NULL, NULL,
340 BoundNetLog());
[email protected]943c8082009-02-23 19:10:45341
342 // If the javascript side of this unit-test fails, it will throw a javascript
343 // exception. Otherwise it will return "PROXY success:80".
[email protected]52ae80c2009-09-10 21:27:00344 EXPECT_EQ(OK, result);
[email protected]943c8082009-02-23 19:10:45345 EXPECT_EQ("success:80", proxy_info.proxy_server().ToURI());
[email protected]50d7d7282009-03-02 21:45:18346
347 EXPECT_EQ(0U, resolver.mock_js_bindings()->alerts.size());
348 EXPECT_EQ(0U, resolver.mock_js_bindings()->errors.size());
[email protected]943c8082009-02-23 19:10:45349}
350
[email protected]775fd9e2009-07-26 21:12:20351// Try resolving when SetPacScriptByData() has not been called.
[email protected]943c8082009-02-23 19:10:45352TEST(ProxyResolverV8Test, NoSetPacScript) {
[email protected]50d7d7282009-03-02 21:45:18353 ProxyResolverV8WithMockBindings resolver;
[email protected]943c8082009-02-23 19:10:45354
[email protected]52ae80c2009-09-10 21:27:00355 ProxyInfo proxy_info;
[email protected]943c8082009-02-23 19:10:45356
357 // Resolve should fail, as we are not yet initialized with a script.
[email protected]5a1d7ca2010-04-28 20:12:27358 int result = resolver.GetProxyForURL(kQueryUrl, &proxy_info, NULL, NULL,
359 BoundNetLog());
[email protected]52ae80c2009-09-10 21:27:00360 EXPECT_EQ(ERR_FAILED, result);
[email protected]943c8082009-02-23 19:10:45361
362 // Initialize it.
[email protected]620f5712009-08-04 22:43:12363 result = resolver.SetPacScriptFromDisk("direct.js");
[email protected]52ae80c2009-09-10 21:27:00364 EXPECT_EQ(OK, result);
[email protected]943c8082009-02-23 19:10:45365
366 // Resolve should now succeed.
[email protected]5a1d7ca2010-04-28 20:12:27367 result = resolver.GetProxyForURL(kQueryUrl, &proxy_info, NULL, NULL,
368 BoundNetLog());
[email protected]52ae80c2009-09-10 21:27:00369 EXPECT_EQ(OK, result);
[email protected]943c8082009-02-23 19:10:45370
371 // Clear it, by initializing with an empty string.
[email protected]24476402010-07-20 20:55:17372 resolver.SetPacScript(
373 ProxyResolverScriptData::FromUTF16(string16()), NULL);
[email protected]943c8082009-02-23 19:10:45374
375 // Resolve should fail again now.
[email protected]5a1d7ca2010-04-28 20:12:27376 result = resolver.GetProxyForURL(kQueryUrl, &proxy_info, NULL, NULL,
377 BoundNetLog());
[email protected]52ae80c2009-09-10 21:27:00378 EXPECT_EQ(ERR_FAILED, result);
[email protected]943c8082009-02-23 19:10:45379
380 // Load a good script once more.
[email protected]620f5712009-08-04 22:43:12381 result = resolver.SetPacScriptFromDisk("direct.js");
[email protected]52ae80c2009-09-10 21:27:00382 EXPECT_EQ(OK, result);
[email protected]5a1d7ca2010-04-28 20:12:27383 result = resolver.GetProxyForURL(kQueryUrl, &proxy_info, NULL, NULL,
384 BoundNetLog());
[email protected]52ae80c2009-09-10 21:27:00385 EXPECT_EQ(OK, result);
[email protected]50d7d7282009-03-02 21:45:18386
387 EXPECT_EQ(0U, resolver.mock_js_bindings()->alerts.size());
388 EXPECT_EQ(0U, resolver.mock_js_bindings()->errors.size());
389}
390
391// Test marshalling/un-marshalling of values between C++/V8.
392TEST(ProxyResolverV8Test, V8Bindings) {
393 ProxyResolverV8WithMockBindings resolver;
[email protected]d35c3662010-05-05 20:04:34394 MockJSBindings* bindings = resolver.mock_js_bindings();
395 bindings->dns_resolve_result = "127.0.0.1";
[email protected]620f5712009-08-04 22:43:12396 int result = resolver.SetPacScriptFromDisk("bindings.js");
[email protected]52ae80c2009-09-10 21:27:00397 EXPECT_EQ(OK, result);
[email protected]50d7d7282009-03-02 21:45:18398
[email protected]52ae80c2009-09-10 21:27:00399 ProxyInfo proxy_info;
[email protected]5a1d7ca2010-04-28 20:12:27400 result = resolver.GetProxyForURL(kQueryUrl, &proxy_info, NULL, NULL,
401 BoundNetLog());
[email protected]50d7d7282009-03-02 21:45:18402
[email protected]52ae80c2009-09-10 21:27:00403 EXPECT_EQ(OK, result);
[email protected]50d7d7282009-03-02 21:45:18404 EXPECT_TRUE(proxy_info.is_direct());
405
[email protected]50d7d7282009-03-02 21:45:18406 EXPECT_EQ(0U, resolver.mock_js_bindings()->errors.size());
407
408 // Alert was called 5 times.
409 ASSERT_EQ(5U, bindings->alerts.size());
410 EXPECT_EQ("undefined", bindings->alerts[0]);
411 EXPECT_EQ("null", bindings->alerts[1]);
412 EXPECT_EQ("undefined", bindings->alerts[2]);
413 EXPECT_EQ("[object Object]", bindings->alerts[3]);
414 EXPECT_EQ("exception from calling toString()", bindings->alerts[4]);
415
[email protected]d35c3662010-05-05 20:04:34416 // DnsResolve was called 8 times, however only 2 of those were string
417 // parameters. (so 6 of them failed immediately).
418 ASSERT_EQ(2U, bindings->dns_resolves.size());
419 EXPECT_EQ("", bindings->dns_resolves[0]);
420 EXPECT_EQ("arg1", bindings->dns_resolves[1]);
[email protected]50d7d7282009-03-02 21:45:18421
422 // MyIpAddress was called two times.
423 EXPECT_EQ(2, bindings->my_ip_address_count);
[email protected]21f20c892009-10-26 23:51:28424
425 // MyIpAddressEx was called once.
426 EXPECT_EQ(1, bindings->my_ip_address_ex_count);
427
428 // DnsResolveEx was called 2 times.
429 ASSERT_EQ(2U, bindings->dns_resolves_ex.size());
430 EXPECT_EQ("is_resolvable", bindings->dns_resolves_ex[0]);
431 EXPECT_EQ("foobar", bindings->dns_resolves_ex[1]);
[email protected]50d7d7282009-03-02 21:45:18432}
433
[email protected]a5e9f2c2010-03-31 22:10:11434// Test calling a binding (myIpAddress()) from the script's global scope.
435// http://crbug.com/40026
436TEST(ProxyResolverV8Test, BindingCalledDuringInitialization) {
437 ProxyResolverV8WithMockBindings resolver;
438
439 int result = resolver.SetPacScriptFromDisk("binding_from_global.js");
440 EXPECT_EQ(OK, result);
441
442 MockJSBindings* bindings = resolver.mock_js_bindings();
443
444 // myIpAddress() got called during initialization of the script.
445 EXPECT_EQ(1, bindings->my_ip_address_count);
446
447 ProxyInfo proxy_info;
[email protected]5a1d7ca2010-04-28 20:12:27448 result = resolver.GetProxyForURL(kQueryUrl, &proxy_info, NULL, NULL,
449 BoundNetLog());
[email protected]a5e9f2c2010-03-31 22:10:11450
451 EXPECT_EQ(OK, result);
452 EXPECT_FALSE(proxy_info.is_direct());
453 EXPECT_EQ("127.0.0.1:80", proxy_info.proxy_server().ToURI());
454
455 // Check that no other bindings were called.
456 EXPECT_EQ(0U, bindings->errors.size());
457 ASSERT_EQ(0U, bindings->alerts.size());
458 ASSERT_EQ(0U, bindings->dns_resolves.size());
459 EXPECT_EQ(0, bindings->my_ip_address_ex_count);
460 ASSERT_EQ(0U, bindings->dns_resolves_ex.size());
461}
462
[email protected]cd88b0c2009-09-25 04:52:39463// Try loading a PAC script that ends with a comment and has no terminal
464// newline. This should not cause problems with the PAC utility functions
465// that we add to the script's environment.
[email protected]c945a462009-09-24 02:13:57466// http://crbug.com/22864
[email protected]cd88b0c2009-09-25 04:52:39467TEST(ProxyResolverV8Test, EndsWithCommentNoNewline) {
[email protected]c945a462009-09-24 02:13:57468 ProxyResolverV8WithMockBindings resolver;
469 int result = resolver.SetPacScriptFromDisk("ends_with_comment.js");
470 EXPECT_EQ(OK, result);
471
472 ProxyInfo proxy_info;
[email protected]9e743cd2010-03-16 07:03:53473 CapturingBoundNetLog log(CapturingNetLog::kUnbounded);
474 result = resolver.GetProxyForURL(kQueryUrl, &proxy_info, NULL, NULL,
475 log.bound());
[email protected]c945a462009-09-24 02:13:57476
477 EXPECT_EQ(OK, result);
478 EXPECT_FALSE(proxy_info.is_direct());
479 EXPECT_EQ("success:80", proxy_info.proxy_server().ToURI());
480}
481
[email protected]cd88b0c2009-09-25 04:52:39482// Try loading a PAC script that ends with a statement and has no terminal
483// newline. This should not cause problems with the PAC utility functions
484// that we add to the script's environment.
485// http://crbug.com/22864
486TEST(ProxyResolverV8Test, EndsWithStatementNoNewline) {
487 ProxyResolverV8WithMockBindings resolver;
488 int result = resolver.SetPacScriptFromDisk(
489 "ends_with_statement_no_semicolon.js");
490 EXPECT_EQ(OK, result);
491
492 ProxyInfo proxy_info;
[email protected]9e743cd2010-03-16 07:03:53493 CapturingBoundNetLog log(CapturingNetLog::kUnbounded);
494 result = resolver.GetProxyForURL(kQueryUrl, &proxy_info, NULL, NULL,
495 log.bound());
[email protected]cd88b0c2009-09-25 04:52:39496
497 EXPECT_EQ(OK, result);
498 EXPECT_FALSE(proxy_info.is_direct());
499 EXPECT_EQ("success:3", proxy_info.proxy_server().ToURI());
500}
501
[email protected]21f20c892009-10-26 23:51:28502// Test the return values from myIpAddress(), myIpAddressEx(), dnsResolve(),
503// dnsResolveEx(), isResolvable(), isResolvableEx(), when the the binding
504// returns empty string (failure). This simulates the return values from
505// those functions when the underlying DNS resolution fails.
506TEST(ProxyResolverV8Test, DNSResolutionFailure) {
507 ProxyResolverV8WithMockBindings resolver;
508 int result = resolver.SetPacScriptFromDisk("dns_fail.js");
509 EXPECT_EQ(OK, result);
510
511 ProxyInfo proxy_info;
[email protected]5a1d7ca2010-04-28 20:12:27512 result = resolver.GetProxyForURL(kQueryUrl, &proxy_info, NULL, NULL,
513 BoundNetLog());
[email protected]21f20c892009-10-26 23:51:28514
515 EXPECT_EQ(OK, result);
516 EXPECT_FALSE(proxy_info.is_direct());
517 EXPECT_EQ("success:80", proxy_info.proxy_server().ToURI());
518}
519
[email protected]0238de42010-06-23 18:04:01520TEST(ProxyResolverV8Test, DNSResolutionOfInternationDomainName) {
521 ProxyResolverV8WithMockBindings resolver;
522 int result = resolver.SetPacScriptFromDisk("international_domain_names.js");
523 EXPECT_EQ(OK, result);
524
525 // Execute FindProxyForURL().
526 ProxyInfo proxy_info;
527 result = resolver.GetProxyForURL(kQueryUrl, &proxy_info, NULL, NULL,
528 BoundNetLog());
529
530 EXPECT_EQ(OK, result);
531 EXPECT_TRUE(proxy_info.is_direct());
532
533 // Check that the international domain name was converted to punycode
534 // before passing it onto the bindings layer.
535 MockJSBindings* bindings = resolver.mock_js_bindings();
536
537 ASSERT_EQ(1u, bindings->dns_resolves.size());
538 EXPECT_EQ("xn--bcher-kva.ch", bindings->dns_resolves[0]);
539
540 ASSERT_EQ(1u, bindings->dns_resolves_ex.size());
541 EXPECT_EQ("xn--bcher-kva.ch", bindings->dns_resolves_ex[0]);
542}
543
[email protected]52ae80c2009-09-10 21:27:00544} // namespace
545} // namespace net