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
3 // found in the LICENSE file.
5 #include "base/compiler_specific.h"
6 #include "base/files/file_util.h"
7 #include "base/path_service.h"
8 #include "base/strings/string_util.h"
9 #include "base/strings/stringprintf.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "net/base/net_errors.h"
12 #include "net/log/net_log_unittest.h"
13 #include "net/proxy/proxy_info.h"
14 #include "net/proxy/proxy_resolver_v8.h"
15 #include "testing/gtest/include/gtest/gtest.h"
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
23 // list, for later verification.
24 class MockJSBindings
: public ProxyResolverV8::JSBindings
{
26 MockJSBindings() : my_ip_address_count(0), my_ip_address_ex_count(0),
27 should_terminate(false) {}
29 void Alert(const base::string16
& message
) override
{
30 VLOG(1) << "PAC-alert: " << message
; // Helpful when debugging.
31 alerts
.push_back(base::UTF16ToUTF8(message
));
34 bool ResolveDns(const std::string
& host
,
35 ResolveDnsOperation op
,
37 bool* terminate
) override
{
38 *terminate
= should_terminate
;
40 if (op
== MY_IP_ADDRESS
) {
41 my_ip_address_count
++;
42 *output
= my_ip_address_result
;
43 return !my_ip_address_result
.empty();
46 if (op
== MY_IP_ADDRESS_EX
) {
47 my_ip_address_ex_count
++;
48 *output
= my_ip_address_ex_result
;
49 return !my_ip_address_ex_result
.empty();
52 if (op
== DNS_RESOLVE
) {
53 dns_resolves
.push_back(host
);
54 *output
= dns_resolve_result
;
55 return !dns_resolve_result
.empty();
58 if (op
== DNS_RESOLVE_EX
) {
59 dns_resolves_ex
.push_back(host
);
60 *output
= dns_resolve_ex_result
;
61 return !dns_resolve_ex_result
.empty();
68 void OnError(int line_number
, const base::string16
& message
) override
{
69 // Helpful when debugging.
70 VLOG(1) << "PAC-error: [" << line_number
<< "] " << message
;
72 errors
.push_back(base::UTF16ToUTF8(message
));
73 errors_line_number
.push_back(line_number
);
76 // Mock values to return.
77 std::string my_ip_address_result
;
78 std::string my_ip_address_ex_result
;
79 std::string dns_resolve_result
;
80 std::string dns_resolve_ex_result
;
82 // Inputs we got called with.
83 std::vector
<std::string
> alerts
;
84 std::vector
<std::string
> errors
;
85 std::vector
<int> errors_line_number
;
86 std::vector
<std::string
> dns_resolves
;
87 std::vector
<std::string
> dns_resolves_ex
;
88 int my_ip_address_count
;
89 int my_ip_address_ex_count
;
91 // Whether ResolveDns() should terminate script execution.
92 bool should_terminate
;
95 // This is the same as ProxyResolverV8, but it uses mock bindings in place of
96 // the default bindings, and has a helper function to load PAC scripts from
98 class ProxyResolverV8WithMockBindings
: public ProxyResolverV8
{
100 ProxyResolverV8WithMockBindings() {
101 set_js_bindings(&mock_js_bindings_
);
104 ~ProxyResolverV8WithMockBindings() override
{}
106 MockJSBindings
* mock_js_bindings() {
107 return &mock_js_bindings_
;
110 // Initialize with the PAC script data at |filename|.
111 int SetPacScriptFromDisk(const char* filename
) {
113 PathService::Get(base::DIR_SOURCE_ROOT
, &path
);
114 path
= path
.AppendASCII("net");
115 path
= path
.AppendASCII("data");
116 path
= path
.AppendASCII("proxy_resolver_v8_unittest");
117 path
= path
.AppendASCII(filename
);
119 // Try to read the file from disk.
120 std::string file_contents
;
121 bool ok
= base::ReadFileToString(path
, &file_contents
);
123 // If we can't load the file from disk, something is misconfigured.
125 LOG(ERROR
) << "Failed to read file: " << path
.value();
126 return ERR_UNEXPECTED
;
129 // Load the PAC script into the ProxyResolver.
130 return SetPacScript(ProxyResolverScriptData::FromUTF8(file_contents
),
131 CompletionCallback());
135 MockJSBindings mock_js_bindings_
;
138 // Doesn't really matter what these values are for many of the tests.
139 const GURL
kQueryUrl("http://www.google.com");
142 TEST(ProxyResolverV8Test
, Direct
) {
143 ProxyResolverV8WithMockBindings resolver
;
144 int result
= resolver
.SetPacScriptFromDisk("direct.js");
145 EXPECT_EQ(OK
, result
);
147 ProxyInfo proxy_info
;
148 CapturingBoundNetLog log
;
149 result
= resolver
.GetProxyForURL(
150 kQueryUrl
, &proxy_info
, CompletionCallback(), NULL
, log
.bound());
152 EXPECT_EQ(OK
, result
);
153 EXPECT_TRUE(proxy_info
.is_direct());
155 EXPECT_EQ(0U, resolver
.mock_js_bindings()->alerts
.size());
156 EXPECT_EQ(0U, resolver
.mock_js_bindings()->errors
.size());
158 net::CapturingNetLog::CapturedEntryList entries
;
159 log
.GetEntries(&entries
);
160 // No bindings were called, so no log entries.
161 EXPECT_EQ(0u, entries
.size());
164 TEST(ProxyResolverV8Test
, ReturnEmptyString
) {
165 ProxyResolverV8WithMockBindings resolver
;
166 int result
= resolver
.SetPacScriptFromDisk("return_empty_string.js");
167 EXPECT_EQ(OK
, result
);
169 ProxyInfo proxy_info
;
170 result
= resolver
.GetProxyForURL(
171 kQueryUrl
, &proxy_info
, CompletionCallback(), NULL
, BoundNetLog());
173 EXPECT_EQ(OK
, result
);
174 EXPECT_TRUE(proxy_info
.is_direct());
176 EXPECT_EQ(0U, resolver
.mock_js_bindings()->alerts
.size());
177 EXPECT_EQ(0U, resolver
.mock_js_bindings()->errors
.size());
180 TEST(ProxyResolverV8Test
, Basic
) {
181 ProxyResolverV8WithMockBindings resolver
;
182 int result
= resolver
.SetPacScriptFromDisk("passthrough.js");
183 EXPECT_EQ(OK
, result
);
185 // The "FindProxyForURL" of this PAC script simply concatenates all of the
186 // arguments into a pseudo-host. The purpose of this test is to verify that
187 // the correct arguments are being passed to FindProxyForURL().
189 ProxyInfo proxy_info
;
190 result
= resolver
.GetProxyForURL(GURL("http://query.com/path"), &proxy_info
,
191 CompletionCallback(), NULL
, BoundNetLog());
192 EXPECT_EQ(OK
, result
);
193 EXPECT_EQ("http.query.com.path.query.com:80",
194 proxy_info
.proxy_server().ToURI());
197 ProxyInfo proxy_info
;
198 int result
= resolver
.GetProxyForURL(
199 GURL("ftp://query.com:90/path"), &proxy_info
, CompletionCallback(),
200 NULL
, BoundNetLog());
201 EXPECT_EQ(OK
, result
);
202 // Note that FindProxyForURL(url, host) does not expect |host| to contain
204 EXPECT_EQ("ftp.query.com.90.path.query.com:80",
205 proxy_info
.proxy_server().ToURI());
207 EXPECT_EQ(0U, resolver
.mock_js_bindings()->alerts
.size());
208 EXPECT_EQ(0U, resolver
.mock_js_bindings()->errors
.size());
212 TEST(ProxyResolverV8Test
, BadReturnType
) {
213 // These are the filenames of PAC scripts which each return a non-string
214 // types for FindProxyForURL(). They should all fail with
215 // ERR_PAC_SCRIPT_FAILED.
216 static const char* const filenames
[] = {
217 "return_undefined.js",
219 "return_function.js",
221 // TODO(eroman): Should 'null' be considered equivalent to "DIRECT" ?
225 for (size_t i
= 0; i
< arraysize(filenames
); ++i
) {
226 ProxyResolverV8WithMockBindings resolver
;
227 int result
= resolver
.SetPacScriptFromDisk(filenames
[i
]);
228 EXPECT_EQ(OK
, result
);
230 ProxyInfo proxy_info
;
231 result
= resolver
.GetProxyForURL(
232 kQueryUrl
, &proxy_info
, CompletionCallback(), NULL
, BoundNetLog());
234 EXPECT_EQ(ERR_PAC_SCRIPT_FAILED
, result
);
236 MockJSBindings
* bindings
= resolver
.mock_js_bindings();
237 EXPECT_EQ(0U, bindings
->alerts
.size());
238 ASSERT_EQ(1U, bindings
->errors
.size());
239 EXPECT_EQ("FindProxyForURL() did not return a string.",
240 bindings
->errors
[0]);
241 EXPECT_EQ(-1, bindings
->errors_line_number
[0]);
245 // Try using a PAC script which defines no "FindProxyForURL" function.
246 TEST(ProxyResolverV8Test
, NoEntryPoint
) {
247 ProxyResolverV8WithMockBindings resolver
;
248 int result
= resolver
.SetPacScriptFromDisk("no_entrypoint.js");
249 EXPECT_EQ(ERR_PAC_SCRIPT_FAILED
, result
);
251 ProxyInfo proxy_info
;
252 result
= resolver
.GetProxyForURL(
253 kQueryUrl
, &proxy_info
, CompletionCallback(), NULL
, BoundNetLog());
255 EXPECT_EQ(ERR_FAILED
, result
);
257 MockJSBindings
* bindings
= resolver
.mock_js_bindings();
258 ASSERT_EQ(1U, bindings
->errors
.size());
259 EXPECT_EQ("FindProxyForURL is undefined or not a function.",
260 bindings
->errors
[0]);
261 EXPECT_EQ(-1, bindings
->errors_line_number
[0]);
264 // Try loading a malformed PAC script.
265 TEST(ProxyResolverV8Test
, ParseError
) {
266 ProxyResolverV8WithMockBindings resolver
;
267 int result
= resolver
.SetPacScriptFromDisk("missing_close_brace.js");
268 EXPECT_EQ(ERR_PAC_SCRIPT_FAILED
, result
);
270 ProxyInfo proxy_info
;
271 result
= resolver
.GetProxyForURL(
272 kQueryUrl
, &proxy_info
, CompletionCallback(), NULL
, BoundNetLog());
274 EXPECT_EQ(ERR_FAILED
, result
);
276 MockJSBindings
* bindings
= resolver
.mock_js_bindings();
277 EXPECT_EQ(0U, bindings
->alerts
.size());
279 // We get one error during compilation.
280 ASSERT_EQ(1U, bindings
->errors
.size());
282 EXPECT_EQ("Uncaught SyntaxError: Unexpected end of input",
283 bindings
->errors
[0]);
284 EXPECT_EQ(0, bindings
->errors_line_number
[0]);
287 // Run a PAC script several times, which has side-effects.
288 TEST(ProxyResolverV8Test
, SideEffects
) {
289 ProxyResolverV8WithMockBindings resolver
;
290 int result
= resolver
.SetPacScriptFromDisk("side_effects.js");
292 // The PAC script increments a counter each time we invoke it.
293 for (int i
= 0; i
< 3; ++i
) {
294 ProxyInfo proxy_info
;
295 result
= resolver
.GetProxyForURL(
296 kQueryUrl
, &proxy_info
, CompletionCallback(), NULL
, BoundNetLog());
297 EXPECT_EQ(OK
, result
);
298 EXPECT_EQ(base::StringPrintf("sideffect_%d:80", i
),
299 proxy_info
.proxy_server().ToURI());
302 // Reload the script -- the javascript environment should be reset, hence
303 // the counter starts over.
304 result
= resolver
.SetPacScriptFromDisk("side_effects.js");
305 EXPECT_EQ(OK
, result
);
307 for (int i
= 0; i
< 3; ++i
) {
308 ProxyInfo proxy_info
;
309 result
= resolver
.GetProxyForURL(
310 kQueryUrl
, &proxy_info
, CompletionCallback(), NULL
, BoundNetLog());
311 EXPECT_EQ(OK
, result
);
312 EXPECT_EQ(base::StringPrintf("sideffect_%d:80", i
),
313 proxy_info
.proxy_server().ToURI());
317 // Execute a PAC script which throws an exception in FindProxyForURL.
318 TEST(ProxyResolverV8Test
, UnhandledException
) {
319 ProxyResolverV8WithMockBindings resolver
;
320 int result
= resolver
.SetPacScriptFromDisk("unhandled_exception.js");
321 EXPECT_EQ(OK
, result
);
323 ProxyInfo proxy_info
;
324 result
= resolver
.GetProxyForURL(
325 kQueryUrl
, &proxy_info
, CompletionCallback(), NULL
, BoundNetLog());
327 EXPECT_EQ(ERR_PAC_SCRIPT_FAILED
, result
);
329 MockJSBindings
* bindings
= resolver
.mock_js_bindings();
330 EXPECT_EQ(0U, bindings
->alerts
.size());
331 ASSERT_EQ(1U, bindings
->errors
.size());
332 EXPECT_EQ("Uncaught ReferenceError: undefined_variable is not defined",
333 bindings
->errors
[0]);
334 EXPECT_EQ(3, bindings
->errors_line_number
[0]);
337 // Execute a PAC script which throws an exception when first accessing
339 TEST(ProxyResolverV8Test
, ExceptionAccessingFindProxyForURLDuringInit
) {
340 ProxyResolverV8WithMockBindings resolver
;
342 resolver
.SetPacScriptFromDisk("exception_findproxyforurl_during_init.js");
343 EXPECT_EQ(ERR_PAC_SCRIPT_FAILED
, result
);
345 MockJSBindings
* bindings
= resolver
.mock_js_bindings();
346 ASSERT_EQ(2U, bindings
->errors
.size());
347 EXPECT_EQ("Uncaught crash!", bindings
->errors
[0]);
348 EXPECT_EQ(9, bindings
->errors_line_number
[0]);
349 EXPECT_EQ("Accessing FindProxyForURL threw an exception.",
350 bindings
->errors
[1]);
351 EXPECT_EQ(-1, bindings
->errors_line_number
[1]);
354 // Execute a PAC script which throws an exception during the second access to
356 TEST(ProxyResolverV8Test
, ExceptionAccessingFindProxyForURLDuringResolve
) {
357 ProxyResolverV8WithMockBindings resolver
;
358 int result
= resolver
.SetPacScriptFromDisk(
359 "exception_findproxyforurl_during_resolve.js");
360 EXPECT_EQ(OK
, result
);
362 ProxyInfo proxy_info
;
363 result
= resolver
.GetProxyForURL(kQueryUrl
, &proxy_info
, CompletionCallback(),
364 NULL
, BoundNetLog());
366 EXPECT_EQ(ERR_PAC_SCRIPT_FAILED
, result
);
368 MockJSBindings
* bindings
= resolver
.mock_js_bindings();
369 ASSERT_EQ(2U, bindings
->errors
.size());
370 EXPECT_EQ("Uncaught crash!", bindings
->errors
[0]);
371 EXPECT_EQ(17, bindings
->errors_line_number
[0]);
372 EXPECT_EQ("Accessing FindProxyForURL threw an exception.",
373 bindings
->errors
[1]);
374 EXPECT_EQ(-1, bindings
->errors_line_number
[1]);
377 TEST(ProxyResolverV8Test
, ReturnUnicode
) {
378 ProxyResolverV8WithMockBindings resolver
;
379 int result
= resolver
.SetPacScriptFromDisk("return_unicode.js");
380 EXPECT_EQ(OK
, result
);
382 ProxyInfo proxy_info
;
383 result
= resolver
.GetProxyForURL(
384 kQueryUrl
, &proxy_info
, CompletionCallback(), NULL
, BoundNetLog());
386 // The result from this resolve was unparseable, because it
388 EXPECT_EQ(ERR_PAC_SCRIPT_FAILED
, result
);
391 // Test the PAC library functions that we expose in the JS environment.
392 TEST(ProxyResolverV8Test
, JavascriptLibrary
) {
393 ProxyResolverV8WithMockBindings resolver
;
394 int result
= resolver
.SetPacScriptFromDisk("pac_library_unittest.js");
395 EXPECT_EQ(OK
, result
);
397 ProxyInfo proxy_info
;
398 result
= resolver
.GetProxyForURL(
399 kQueryUrl
, &proxy_info
, CompletionCallback(), NULL
, BoundNetLog());
401 // If the javascript side of this unit-test fails, it will throw a javascript
402 // exception. Otherwise it will return "PROXY success:80".
403 EXPECT_EQ(OK
, result
);
404 EXPECT_EQ("success:80", proxy_info
.proxy_server().ToURI());
406 EXPECT_EQ(0U, resolver
.mock_js_bindings()->alerts
.size());
407 EXPECT_EQ(0U, resolver
.mock_js_bindings()->errors
.size());
410 // Try resolving when SetPacScriptByData() has not been called.
411 TEST(ProxyResolverV8Test
, NoSetPacScript
) {
412 ProxyResolverV8WithMockBindings resolver
;
414 ProxyInfo proxy_info
;
416 // Resolve should fail, as we are not yet initialized with a script.
417 int result
= resolver
.GetProxyForURL(
418 kQueryUrl
, &proxy_info
, CompletionCallback(), NULL
, BoundNetLog());
419 EXPECT_EQ(ERR_FAILED
, result
);
422 result
= resolver
.SetPacScriptFromDisk("direct.js");
423 EXPECT_EQ(OK
, result
);
425 // Resolve should now succeed.
426 result
= resolver
.GetProxyForURL(
427 kQueryUrl
, &proxy_info
, CompletionCallback(), NULL
, BoundNetLog());
428 EXPECT_EQ(OK
, result
);
430 // Clear it, by initializing with an empty string.
431 resolver
.SetPacScript(
432 ProxyResolverScriptData::FromUTF16(base::string16()),
433 CompletionCallback());
435 // Resolve should fail again now.
436 result
= resolver
.GetProxyForURL(
437 kQueryUrl
, &proxy_info
, CompletionCallback(), NULL
, BoundNetLog());
438 EXPECT_EQ(ERR_FAILED
, result
);
440 // Load a good script once more.
441 result
= resolver
.SetPacScriptFromDisk("direct.js");
442 EXPECT_EQ(OK
, result
);
443 result
= resolver
.GetProxyForURL(
444 kQueryUrl
, &proxy_info
, CompletionCallback(), NULL
, BoundNetLog());
445 EXPECT_EQ(OK
, result
);
447 EXPECT_EQ(0U, resolver
.mock_js_bindings()->alerts
.size());
448 EXPECT_EQ(0U, resolver
.mock_js_bindings()->errors
.size());
451 // Test marshalling/un-marshalling of values between C++/V8.
452 TEST(ProxyResolverV8Test
, V8Bindings
) {
453 ProxyResolverV8WithMockBindings resolver
;
454 MockJSBindings
* bindings
= resolver
.mock_js_bindings();
455 bindings
->dns_resolve_result
= "127.0.0.1";
456 int result
= resolver
.SetPacScriptFromDisk("bindings.js");
457 EXPECT_EQ(OK
, result
);
459 ProxyInfo proxy_info
;
460 result
= resolver
.GetProxyForURL(
461 kQueryUrl
, &proxy_info
, CompletionCallback(), NULL
, BoundNetLog());
463 EXPECT_EQ(OK
, result
);
464 EXPECT_TRUE(proxy_info
.is_direct());
466 EXPECT_EQ(0U, resolver
.mock_js_bindings()->errors
.size());
468 // Alert was called 5 times.
469 ASSERT_EQ(5U, bindings
->alerts
.size());
470 EXPECT_EQ("undefined", bindings
->alerts
[0]);
471 EXPECT_EQ("null", bindings
->alerts
[1]);
472 EXPECT_EQ("undefined", bindings
->alerts
[2]);
473 EXPECT_EQ("[object Object]", bindings
->alerts
[3]);
474 EXPECT_EQ("exception from calling toString()", bindings
->alerts
[4]);
476 // DnsResolve was called 8 times, however only 2 of those were string
477 // parameters. (so 6 of them failed immediately).
478 ASSERT_EQ(2U, bindings
->dns_resolves
.size());
479 EXPECT_EQ("", bindings
->dns_resolves
[0]);
480 EXPECT_EQ("arg1", bindings
->dns_resolves
[1]);
482 // MyIpAddress was called two times.
483 EXPECT_EQ(2, bindings
->my_ip_address_count
);
485 // MyIpAddressEx was called once.
486 EXPECT_EQ(1, bindings
->my_ip_address_ex_count
);
488 // DnsResolveEx was called 2 times.
489 ASSERT_EQ(2U, bindings
->dns_resolves_ex
.size());
490 EXPECT_EQ("is_resolvable", bindings
->dns_resolves_ex
[0]);
491 EXPECT_EQ("foobar", bindings
->dns_resolves_ex
[1]);
494 // Test calling a binding (myIpAddress()) from the script's global scope.
495 // http://crbug.com/40026
496 TEST(ProxyResolverV8Test
, BindingCalledDuringInitialization
) {
497 ProxyResolverV8WithMockBindings resolver
;
499 int result
= resolver
.SetPacScriptFromDisk("binding_from_global.js");
500 EXPECT_EQ(OK
, result
);
502 MockJSBindings
* bindings
= resolver
.mock_js_bindings();
504 // myIpAddress() got called during initialization of the script.
505 EXPECT_EQ(1, bindings
->my_ip_address_count
);
507 ProxyInfo proxy_info
;
508 result
= resolver
.GetProxyForURL(
509 kQueryUrl
, &proxy_info
, CompletionCallback(), NULL
, BoundNetLog());
511 EXPECT_EQ(OK
, result
);
512 EXPECT_FALSE(proxy_info
.is_direct());
513 EXPECT_EQ("127.0.0.1:80", proxy_info
.proxy_server().ToURI());
515 // Check that no other bindings were called.
516 EXPECT_EQ(0U, bindings
->errors
.size());
517 ASSERT_EQ(0U, bindings
->alerts
.size());
518 ASSERT_EQ(0U, bindings
->dns_resolves
.size());
519 EXPECT_EQ(0, bindings
->my_ip_address_ex_count
);
520 ASSERT_EQ(0U, bindings
->dns_resolves_ex
.size());
523 // Try loading a PAC script that ends with a comment and has no terminal
524 // newline. This should not cause problems with the PAC utility functions
525 // that we add to the script's environment.
526 // http://crbug.com/22864
527 TEST(ProxyResolverV8Test
, EndsWithCommentNoNewline
) {
528 ProxyResolverV8WithMockBindings resolver
;
529 int result
= resolver
.SetPacScriptFromDisk("ends_with_comment.js");
530 EXPECT_EQ(OK
, result
);
532 ProxyInfo proxy_info
;
533 result
= resolver
.GetProxyForURL(
534 kQueryUrl
, &proxy_info
, CompletionCallback(), NULL
, BoundNetLog());
536 EXPECT_EQ(OK
, result
);
537 EXPECT_FALSE(proxy_info
.is_direct());
538 EXPECT_EQ("success:80", proxy_info
.proxy_server().ToURI());
541 // Try loading a PAC script that ends with a statement and has no terminal
542 // newline. This should not cause problems with the PAC utility functions
543 // that we add to the script's environment.
544 // http://crbug.com/22864
545 TEST(ProxyResolverV8Test
, EndsWithStatementNoNewline
) {
546 ProxyResolverV8WithMockBindings resolver
;
547 int result
= resolver
.SetPacScriptFromDisk(
548 "ends_with_statement_no_semicolon.js");
549 EXPECT_EQ(OK
, result
);
551 ProxyInfo proxy_info
;
552 result
= resolver
.GetProxyForURL(
553 kQueryUrl
, &proxy_info
, CompletionCallback(), NULL
, BoundNetLog());
555 EXPECT_EQ(OK
, result
);
556 EXPECT_FALSE(proxy_info
.is_direct());
557 EXPECT_EQ("success:3", proxy_info
.proxy_server().ToURI());
560 // Test the return values from myIpAddress(), myIpAddressEx(), dnsResolve(),
561 // dnsResolveEx(), isResolvable(), isResolvableEx(), when the the binding
562 // returns empty string (failure). This simulates the return values from
563 // those functions when the underlying DNS resolution fails.
564 TEST(ProxyResolverV8Test
, DNSResolutionFailure
) {
565 ProxyResolverV8WithMockBindings resolver
;
566 int result
= resolver
.SetPacScriptFromDisk("dns_fail.js");
567 EXPECT_EQ(OK
, result
);
569 ProxyInfo proxy_info
;
570 result
= resolver
.GetProxyForURL(
571 kQueryUrl
, &proxy_info
, CompletionCallback(), NULL
, BoundNetLog());
573 EXPECT_EQ(OK
, result
);
574 EXPECT_FALSE(proxy_info
.is_direct());
575 EXPECT_EQ("success:80", proxy_info
.proxy_server().ToURI());
578 TEST(ProxyResolverV8Test
, DNSResolutionOfInternationDomainName
) {
579 ProxyResolverV8WithMockBindings resolver
;
580 int result
= resolver
.SetPacScriptFromDisk("international_domain_names.js");
581 EXPECT_EQ(OK
, result
);
583 // Execute FindProxyForURL().
584 ProxyInfo proxy_info
;
585 result
= resolver
.GetProxyForURL(
586 kQueryUrl
, &proxy_info
, CompletionCallback(), NULL
, BoundNetLog());
588 EXPECT_EQ(OK
, result
);
589 EXPECT_TRUE(proxy_info
.is_direct());
591 // Check that the international domain name was converted to punycode
592 // before passing it onto the bindings layer.
593 MockJSBindings
* bindings
= resolver
.mock_js_bindings();
595 ASSERT_EQ(1u, bindings
->dns_resolves
.size());
596 EXPECT_EQ("xn--bcher-kva.ch", bindings
->dns_resolves
[0]);
598 ASSERT_EQ(1u, bindings
->dns_resolves_ex
.size());
599 EXPECT_EQ("xn--bcher-kva.ch", bindings
->dns_resolves_ex
[0]);
602 // Test that when resolving a URL which contains an IPv6 string literal, the
603 // brackets are removed from the host before passing it down to the PAC script.
604 // If we don't do this, then subsequent calls to dnsResolveEx(host) will be
605 // doomed to fail since it won't correspond with a valid name.
606 TEST(ProxyResolverV8Test
, IPv6HostnamesNotBracketed
) {
607 ProxyResolverV8WithMockBindings resolver
;
608 int result
= resolver
.SetPacScriptFromDisk("resolve_host.js");
609 EXPECT_EQ(OK
, result
);
611 ProxyInfo proxy_info
;
612 result
= resolver
.GetProxyForURL(
613 GURL("http://[abcd::efff]:99/watsupdawg"), &proxy_info
,
614 CompletionCallback(), NULL
, BoundNetLog());
616 EXPECT_EQ(OK
, result
);
617 EXPECT_TRUE(proxy_info
.is_direct());
619 // We called dnsResolveEx() exactly once, by passing through the "host"
620 // argument to FindProxyForURL(). The brackets should have been stripped.
621 ASSERT_EQ(1U, resolver
.mock_js_bindings()->dns_resolves_ex
.size());
622 EXPECT_EQ("abcd::efff", resolver
.mock_js_bindings()->dns_resolves_ex
[0]);
625 // Test that terminating a script within DnsResolve() leads to eventual
626 // termination of the script. Also test that repeatedly calling terminate is
627 // safe, and running the script again after termination still works.
628 TEST(ProxyResolverV8Test
, Terminate
) {
629 ProxyResolverV8WithMockBindings resolver
;
630 int result
= resolver
.SetPacScriptFromDisk("terminate.js");
631 EXPECT_EQ(OK
, result
);
633 MockJSBindings
* bindings
= resolver
.mock_js_bindings();
635 // Terminate script execution upon reaching dnsResolve(). Note that
636 // termination may not take effect right away (so the subsequent dnsResolve()
637 // and alert() may be run).
638 bindings
->should_terminate
= true;
640 ProxyInfo proxy_info
;
641 result
= resolver
.GetProxyForURL(
642 GURL("http://hang/"), &proxy_info
,
643 CompletionCallback(), NULL
, BoundNetLog());
645 // The script execution was terminated.
646 EXPECT_EQ(ERR_PAC_SCRIPT_FAILED
, result
);
648 EXPECT_EQ(1U, resolver
.mock_js_bindings()->dns_resolves
.size());
649 EXPECT_GE(2U, resolver
.mock_js_bindings()->dns_resolves_ex
.size());
650 EXPECT_GE(1U, bindings
->alerts
.size());
652 EXPECT_EQ(1U, bindings
->errors
.size());
654 // Termination shows up as an uncaught exception without any message.
655 EXPECT_EQ("", bindings
->errors
[0]);
657 bindings
->errors
.clear();
659 // Try running the script again, this time with a different input which won't
660 // cause a termination+hang.
661 result
= resolver
.GetProxyForURL(
662 GURL("http://kittens/"), &proxy_info
,
663 CompletionCallback(), NULL
, BoundNetLog());
665 EXPECT_EQ(OK
, result
);
666 EXPECT_EQ(0u, bindings
->errors
.size());
667 EXPECT_EQ("kittens:88", proxy_info
.proxy_server().ToURI());