proxy_resolver_v8_unittest.cc 20 KB

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