1 // This file is auto generated using the following command. 2 // Do not modify. 3 // ./jstocstring.pl js-unittest proxy_test_script.h 4 #ifndef PROXY_TEST_SCRIPT_H_ 5 #define PROXY_TEST_SCRIPT_H_ 6 7 #define B_132073833_JS \ 8 u""\ 9 "function FindProxyForURL(url, host){\n" \ 10 " function opt() {\n" \ 11 " opt['x'] = 1.1;\n" \ 12 " try {\n" \ 13 " Object.create(object);\n" \ 14 " } catch (e) {\n" \ 15 " }\n" \ 16 "\n" \ 17 " for (let i = 0; i < 100000; i++) {\n" \ 18 "\n" \ 19 " }\n" \ 20 " }\n" \ 21 "\n" \ 22 " opt();\n" \ 23 " object = opt;\n" \ 24 " opt();\n" \ 25 "\n" \ 26 " return \"DIRECT\";\n" \ 27 "}\n" \ 28 "\n" \ 29 "var object;\n" \ 30 31 #define B_139806216_JS \ 32 u""\ 33 "function FindProxyForURL(url, host){\n" \ 34 " var x = new ArrayBuffer(1);\n" \ 35 " return \"DIRECT\";\n" \ 36 "}\n" \ 37 38 #define B_147664838_JS \ 39 u""\ 40 "function FindProxyForURL(url, host){\n" \ 41 " let re = /x/y;\n" \ 42 " let cnt = 0;\n" \ 43 " let str = re[Symbol.replace](\"x\", {\n" \ 44 " toString: () => {\n" \ 45 " cnt++;\n" \ 46 " if (cnt == 2) {\n" \ 47 " re.lastIndex = {valueOf: () => {\n" \ 48 " re.x = 42;\n" \ 49 " return 0;\n" \ 50 " }};\n" \ 51 " }\n" \ 52 " return 'y$';\n" \ 53 " }\n" \ 54 " });\n" \ 55 " if (str != \"y$\") {\n" \ 56 " throw \"regex mutated\";\n" \ 57 " return \"FAIL\";\n" \ 58 " }\n" \ 59 " return \"DIRECT\";\n" \ 60 "}\n" \ 61 62 #define BINDING_FROM_GLOBAL_JS \ 63 u""\ 64 "// Calls a bindings outside of FindProxyForURL(). This causes the code to\n" \ 65 "// get exercised during initialization.\n" \ 66 "\n" \ 67 "var x = myIpAddress();\n" \ 68 "\n" \ 69 "function FindProxyForURL(url, host) {\n" \ 70 " return \"PROXY \" + x + \":80\";\n" \ 71 "}\n" \ 72 73 #define BINDINGS_JS \ 74 u""\ 75 "// Try calling the browser-side bound functions with varying (invalid)\n" \ 76 "// inputs. There is no notion of \"success\" for this test, other than\n" \ 77 "// verifying the correct C++ bindings were reached with expected values.\n" \ 78 "\n" \ 79 "function MyObject() {\n" \ 80 " this.x = \"3\";\n" \ 81 "}\n" \ 82 "\n" \ 83 "MyObject.prototype.toString = function() {\n" \ 84 " throw \"exception from calling toString()\";\n" \ 85 "}\n" \ 86 "\n" \ 87 "function expectEquals(expectation, actual) {\n" \ 88 " if (!(expectation === actual)) {\n" \ 89 " throw \"FAIL: expected: \" + expectation + \", actual: \" + actual;\n" \ 90 " }\n" \ 91 "}\n" \ 92 "\n" \ 93 "function FindProxyForURL(url, host) {\n" \ 94 " // Call dnsResolve with some wonky arguments.\n" \ 95 " // Those expected to fail (because we have passed a non-string parameter)\n" \ 96 " // will return |null|, whereas those that have called through to the C++\n" \ 97 " // bindings will return '127.0.0.1'.\n" \ 98 " expectEquals(undefined, dnsResolve());\n" \ 99 " expectEquals(undefined, dnsResolve(null));\n" \ 100 " expectEquals(undefined, dnsResolve(undefined));\n" \ 101 " expectEquals('127.0.0.1', dnsResolve(\"\"));\n" \ 102 " expectEquals(undefined, dnsResolve({foo: 'bar'}));\n" \ 103 " expectEquals(undefined, dnsResolve(fn));\n" \ 104 " expectEquals(undefined, dnsResolve(['3']));\n" \ 105 " expectEquals('127.0.0.1', dnsResolve(\"arg1\", \"arg2\", \"arg3\", \"arg4\"));\n" \ 106 "\n" \ 107 " // Call alert with some wonky arguments.\n" \ 108 " alert();\n" \ 109 " alert(null);\n" \ 110 " alert(undefined);\n" \ 111 " alert({foo:'bar'});\n" \ 112 "\n" \ 113 " // This should throw an exception when we toString() the argument\n" \ 114 " // to alert in the bindings.\n" \ 115 " try {\n" \ 116 " alert(new MyObject());\n" \ 117 " } catch (e) {\n" \ 118 " alert(e);\n" \ 119 " }\n" \ 120 "\n" \ 121 " // Call myIpAddress() with wonky arguments\n" \ 122 " myIpAddress(null);\n" \ 123 " myIpAddress(null, null);\n" \ 124 "\n" \ 125 " // Call myIpAddressEx() correctly (no arguments).\n" \ 126 " myIpAddressEx();\n" \ 127 "\n" \ 128 " // Call dnsResolveEx() (note that isResolvableEx() implicity calls it.)\n" \ 129 " isResolvableEx(\"is_resolvable\");\n" \ 130 " dnsResolveEx(\"foobar\");\n" \ 131 "\n" \ 132 " return \"DIRECT\";\n" \ 133 "}\n" \ 134 "\n" \ 135 "function fn() {}\n" \ 136 "\n" \ 137 138 #define CHANGE_ELEMENT_KIND_JS \ 139 u""\ 140 "// PAC script with getter that changes element kind.\n" \ 141 "\n" \ 142 "function FindProxyForURL(url, host) {\n" \ 143 " let arr = [];\n" \ 144 " arr[1000] = 0x1234;\n" \ 145 "\n" \ 146 " arr.__defineGetter__(256, function () {\n" \ 147 " delete arr[256];\n" \ 148 " arr.unshift(1.1);\n" \ 149 " });\n" \ 150 "\n" \ 151 " let results = Object.entries(arr);\n" \ 152 " let str = results.toString();\n" \ 153 " return \"DIRECT\";\n" \ 154 "}\n" \ 155 156 #define DIRECT_JS \ 157 u""\ 158 "function FindProxyForURL(url, host) {\n" \ 159 " return \"DIRECT\";\n" \ 160 "}\n" \ 161 "\n" \ 162 163 #define DNS_FAIL_JS \ 164 u""\ 165 "// This script should be run in an environment where all DNS resolution are\n" \ 166 "// failing. It tests that functions return the expected values.\n" \ 167 "//\n" \ 168 "// Returns \"PROXY success:80\" on success.\n" \ 169 "function FindProxyForURL(url, host) {\n" \ 170 " try {\n" \ 171 " expectEq(\"127.0.0.1\", myIpAddress());\n" \ 172 " expectEq(\"\", myIpAddressEx());\n" \ 173 "\n" \ 174 " expectEq(null, dnsResolve(\"not-found\"));\n" \ 175 " expectEq(\"\", dnsResolveEx(\"not-found\"));\n" \ 176 "\n" \ 177 " expectEq(false, isResolvable(\"not-found\"));\n" \ 178 " expectEq(false, isResolvableEx(\"not-found\"));\n" \ 179 "\n" \ 180 " return \"PROXY success:80\";\n" \ 181 " } catch(e) {\n" \ 182 " alert(e);\n" \ 183 " return \"PROXY failed:80\";\n" \ 184 " }\n" \ 185 "}\n" \ 186 "\n" \ 187 "function expectEq(expected, actual) {\n" \ 188 " if (expected != actual)\n" \ 189 " throw \"Expected \" + expected + \" but was \" + actual;\n" \ 190 "}\n" \ 191 "\n" \ 192 193 #define ENDS_WITH_COMMENT_JS \ 194 u""\ 195 "function FindProxyForURL(url, host) {\n" \ 196 " return \"PROXY success:80\";\n" \ 197 "}\n" \ 198 "\n" \ 199 "// We end the script with a comment (and no trailing newline).\n" \ 200 "// This used to cause problems, because internally ProxyResolverV8\n" \ 201 "// would append some functions to the script; the first line of\n" \ 202 "// those extra functions was being considered part of the comment.\n" \ 203 204 #define ENDS_WITH_STATEMENT_NO_SEMICOLON_JS \ 205 u""\ 206 "// Ends with a statement, and no terminal newline.\n" \ 207 "function FindProxyForURL(url, host) { return \"PROXY success:\" + x; }\n" \ 208 "x = 3\n" \ 209 210 #define INTERNATIONAL_DOMAIN_NAMES_JS \ 211 u""\ 212 "// Try resolving hostnames containing non-ASCII characters.\n" \ 213 "\n" \ 214 "function FindProxyForURL(url, host) {\n" \ 215 " // This international hostname has a non-ASCII character. It is represented\n" \ 216 " // in punycode as 'xn--bcher-kva.ch'\n" \ 217 " var idn = 'B\u00fccher.ch';\n" \ 218 "\n" \ 219 " // We disregard the actual return value -- all we care about is that on\n" \ 220 " // the C++ end the bindings were passed the punycode equivalent of this\n" \ 221 " // unicode hostname.\n" \ 222 " dnsResolve(idn);\n" \ 223 " dnsResolveEx(idn);\n" \ 224 "\n" \ 225 " return \"DIRECT\";\n" \ 226 "}\n" \ 227 "\n" \ 228 229 #define MISSING_CLOSE_BRACE_JS \ 230 u""\ 231 "// This PAC script is invalid, because there is a missing close brace\n" \ 232 "// on the function FindProxyForURL().\n" \ 233 "\n" \ 234 "function FindProxyForURL(url, host) {\n" \ 235 " return \"DIRECT\";\n" \ 236 "\n" \ 237 238 #define NO_ENTRYPOINT_JS \ 239 u""\ 240 "var x = \"This is an invalid PAC script because it lacks a \" +\n" \ 241 " \"FindProxyForURL() function\";\n" \ 242 243 #define PAC_LIBRARY_UNITTEST_JS \ 244 u""\ 245 "// This should output \"PROXY success:80\" if all the tests pass.\n" \ 246 "// Otherwise it will output \"PROXY failure:<num-failures>\".\n" \ 247 "//\n" \ 248 "// This aims to unit-test the PAC library functions, which are\n" \ 249 "// exposed in the PAC's execution environment. (Namely, dnsDomainLevels,\n" \ 250 "// timeRange, etc.)\n" \ 251 "\n" \ 252 "function FindProxyForURL(url, host) {\n" \ 253 " var numTestsFailed = 0;\n" \ 254 "\n" \ 255 " // Run all the tests\n" \ 256 " for (var test in Tests) {\n" \ 257 " var t = new TestContext(test);\n" \ 258 "\n" \ 259 " // Run the test.\n" \ 260 " Tests[test](t);\n" \ 261 "\n" \ 262 " if (t.failed()) {\n" \ 263 " numTestsFailed++;\n" \ 264 " }\n" \ 265 " }\n" \ 266 "\n" \ 267 " if (numTestsFailed == 0) {\n" \ 268 " return \"PROXY success:80\";\n" \ 269 " }\n" \ 270 " return \"PROXY failure:\" + numTestsFailed;\n" \ 271 "}\n" \ 272 "\n" \ 273 "// --------------------------\n" \ 274 "// Tests\n" \ 275 "// --------------------------\n" \ 276 "\n" \ 277 "var Tests = {};\n" \ 278 "\n" \ 279 "Tests.testDnsDomainIs = function(t) {\n" \ 280 " t.expectTrue(dnsDomainIs(\"google.com\", \".com\"));\n" \ 281 " t.expectTrue(dnsDomainIs(\"google.co.uk\", \".co.uk\"));\n" \ 282 " t.expectFalse(dnsDomainIs(\"google.com\", \".co.uk\"));\n" \ 283 " t.expectFalse(dnsDomainIs(\"www.adobe.com\", \".ad\"));\n" \ 284 "};\n" \ 285 "\n" \ 286 "Tests.testDnsDomainLevels = function(t) {\n" \ 287 " t.expectEquals(0, dnsDomainLevels(\"www\"));\n" \ 288 " t.expectEquals(2, dnsDomainLevels(\"www.google.com\"));\n" \ 289 " t.expectEquals(3, dnsDomainLevels(\"192.168.1.1\"));\n" \ 290 "};\n" \ 291 "\n" \ 292 "Tests.testIsInNet = function(t) {\n" \ 293 " t.expectTrue(\n" \ 294 " isInNet(\"192.89.132.25\", \"192.89.132.25\", \"255.255.255.255\"));\n" \ 295 " t.expectFalse(\n" \ 296 " isInNet(\"193.89.132.25\", \"192.89.132.25\", \"255.255.255.255\"));\n" \ 297 "\n" \ 298 " t.expectTrue(isInNet(\"192.89.132.25\", \"192.89.0.0\", \"255.255.0.0\"));\n" \ 299 " t.expectFalse(isInNet(\"193.89.132.25\", \"192.89.0.0\", \"255.255.0.0\"));\n" \ 300 "\n" \ 301 " t.expectFalse(\n" \ 302 " isInNet(\"192.89.132.a\", \"192.89.0.0\", \"255.255.0.0\"));\n" \ 303 "};\n" \ 304 "\n" \ 305 "Tests.testIsPlainHostName = function(t) {\n" \ 306 " t.expectTrue(isPlainHostName(\"google\"));\n" \ 307 " t.expectFalse(isPlainHostName(\"google.com\"));\n" \ 308 "};\n" \ 309 "\n" \ 310 "Tests.testLocalHostOrDomainIs = function(t) {\n" \ 311 " t.expectTrue(localHostOrDomainIs(\"www.google.com\", \"www.google.com\"));\n" \ 312 " t.expectTrue(localHostOrDomainIs(\"www\", \"www.google.com\"));\n" \ 313 " t.expectFalse(localHostOrDomainIs(\"maps.google.com\", \"www.google.com\"));\n" \ 314 "};\n" \ 315 "\n" \ 316 "Tests.testShExpMatch = function(t) {\n" \ 317 " t.expectTrue(shExpMatch(\"foo.jpg\", \"*.jpg\"));\n" \ 318 " t.expectTrue(shExpMatch(\"foo5.jpg\", \"*o?.jpg\"));\n" \ 319 " t.expectFalse(shExpMatch(\"foo.jpg\", \".jpg\"));\n" \ 320 " t.expectFalse(shExpMatch(\"foo.jpg\", \"foo\"));\n" \ 321 "};\n" \ 322 "\n" \ 323 "Tests.testSortIpAddressList = function(t) {\n" \ 324 " t.expectEquals(\"::1;::2;::3\", sortIpAddressList(\"::2;::3;::1\"));\n" \ 325 " t.expectEquals(\n" \ 326 " \"2001:4898:28:3:201:2ff:feea:fc14;fe80::5efe:157:9d3b:8b16;157.59.139.22\",\n" \ 327 " sortIpAddressList(\"157.59.139.22;\" +\n" \ 328 " \"2001:4898:28:3:201:2ff:feea:fc14;\" +\n" \ 329 " \"fe80::5efe:157:9d3b:8b16\"));\n" \ 330 "\n" \ 331 " // Single IP address (v4 and v6).\n" \ 332 " t.expectEquals(\"127.0.0.1\", sortIpAddressList(\"127.0.0.1\"));\n" \ 333 " t.expectEquals(\"::1\", sortIpAddressList(\"::1\"))\n" \ 334 "\n" \ 335 " // Verify that IPv6 address is not re-written (not reduced).\n" \ 336 " t.expectEquals(\"0:0::1;192.168.1.1\", sortIpAddressList(\"192.168.1.1;0:0::1\"));\n" \ 337 "\n" \ 338 " // Input is already sorted.\n" \ 339 " t.expectEquals(\"::1;192.168.1.3\", sortIpAddressList(\"::1;192.168.1.3\"));\n" \ 340 "\n" \ 341 " // Same-valued IP addresses (also tests stability).\n" \ 342 " t.expectEquals(\"0::1;::1;0:0::1\", sortIpAddressList(\"0::1;::1;0:0::1\"));\n" \ 343 "\n" \ 344 " // Contains extra semi-colons.\n" \ 345 " t.expectEquals(\"127.0.0.1\", sortIpAddressList(\";127.0.0.1;\"));\n" \ 346 "\n" \ 347 " // Contains whitespace (spaces and tabs).\n" \ 348 " t.expectEquals(\"192.168.0.1;192.168.0.2\",\n" \ 349 " sortIpAddressList(\"192.168.0.1; 192.168.0.2\"));\n" \ 350 " t.expectEquals(\"127.0.0.0;127.0.0.1;127.0.0.2\",\n" \ 351 " sortIpAddressList(\"127.0.0.1; 127.0.0.2; 127.0.0.0\"));\n" \ 352 "\n" \ 353 " // Empty lists.\n" \ 354 " t.expectFalse(sortIpAddressList(\"\"));\n" \ 355 " t.expectFalse(sortIpAddressList(\" \"));\n" \ 356 " t.expectFalse(sortIpAddressList(\";\"));\n" \ 357 " t.expectFalse(sortIpAddressList(\";;\"));\n" \ 358 " t.expectFalse(sortIpAddressList(\" ; ; \"));\n" \ 359 "\n" \ 360 " // Invalid IP addresses.\n" \ 361 " t.expectFalse(sortIpAddressList(\"256.0.0.1\"));\n" \ 362 " t.expectFalse(sortIpAddressList(\"192.168.1.1;0:0:0:1;127.0.0.1\"));\n" \ 363 "\n" \ 364 " // Call sortIpAddressList() with wonky arguments.\n" \ 365 " t.expectEquals(null, sortIpAddressList());\n" \ 366 " t.expectEquals(null, sortIpAddressList(null));\n" \ 367 " t.expectEquals(null, sortIpAddressList(null, null));\n" \ 368 "};\n" \ 369 "\n" \ 370 "Tests.testIsInNetEx = function(t) {\n" \ 371 " t.expectTrue(isInNetEx(\"198.95.249.79\", \"198.95.249.79/32\"));\n" \ 372 " t.expectTrue(isInNetEx(\"198.95.115.10\", \"198.95.0.0/16\"));\n" \ 373 " t.expectTrue(isInNetEx(\"198.95.1.1\", \"198.95.0.0/16\"));\n" \ 374 " t.expectTrue(isInNetEx(\"198.95.1.1\", \"198.95.3.3/16\"));\n" \ 375 " t.expectTrue(isInNetEx(\"0:0:0:0:0:0:7f00:1\", \"0:0:0:0:0:0:7f00:1/32\"));\n" \ 376 " t.expectTrue(isInNetEx(\"3ffe:8311:ffff:abcd:1234:dead:beef:101\",\n" \ 377 " \"3ffe:8311:ffff::/48\"));\n" \ 378 "\n" \ 379 " // IPv4 and IPv6 mix.\n" \ 380 " t.expectFalse(isInNetEx(\"127.0.0.1\", \"0:0:0:0:0:0:7f00:1/16\"));\n" \ 381 " t.expectFalse(isInNetEx(\"192.168.24.3\", \"fe80:0:0:0:0:0:c0a8:1803/32\"));\n" \ 382 "\n" \ 383 " t.expectFalse(isInNetEx(\"198.95.249.78\", \"198.95.249.79/32\"));\n" \ 384 " t.expectFalse(isInNetEx(\"198.96.115.10\", \"198.95.0.0/16\"));\n" \ 385 " t.expectFalse(isInNetEx(\"3fff:8311:ffff:abcd:1234:dead:beef:101\",\n" \ 386 " \"3ffe:8311:ffff::/48\"));\n" \ 387 "\n" \ 388 " // Call isInNetEx with wonky arguments.\n" \ 389 " t.expectEquals(null, isInNetEx());\n" \ 390 " t.expectEquals(null, isInNetEx(null));\n" \ 391 " t.expectEquals(null, isInNetEx(null, null));\n" \ 392 " t.expectEquals(null, isInNetEx(null, null, null));\n" \ 393 " t.expectEquals(null, isInNetEx(\"198.95.249.79\"));\n" \ 394 "\n" \ 395 " // Invalid IP address.\n" \ 396 " t.expectFalse(isInNetEx(\"256.0.0.1\", \"198.95.249.79\"));\n" \ 397 " t.expectFalse(isInNetEx(\"127.0.0.1 \", \"127.0.0.1/32\")); // Extra space.\n" \ 398 "\n" \ 399 " // Invalid prefix.\n" \ 400 " t.expectFalse(isInNetEx(\"198.95.115.10\", \"198.95.0.0/34\"));\n" \ 401 " t.expectFalse(isInNetEx(\"127.0.0.1\", \"127.0.0.1\")); // Missing '/' in prefix.\n" \ 402 "};\n" \ 403 "\n" \ 404 "Tests.testWeekdayRange = function(t) {\n" \ 405 " // Test with local time.\n" \ 406 " MockDate.setCurrent(\"Tue Mar 03 2009\");\n" \ 407 " t.expectEquals(true, weekdayRange(\"MON\", \"FRI\"));\n" \ 408 " t.expectEquals(true, weekdayRange(\"TUE\", \"FRI\"));\n" \ 409 " t.expectEquals(true, weekdayRange(\"TUE\", \"TUE\"));\n" \ 410 " t.expectEquals(true, weekdayRange(\"TUE\"));\n" \ 411 " t.expectEquals(false, weekdayRange(\"WED\", \"FRI\"));\n" \ 412 " t.expectEquals(false, weekdayRange(\"SUN\", \"MON\"));\n" \ 413 " t.expectEquals(false, weekdayRange(\"SAT\"));\n" \ 414 " t.expectEquals(false, weekdayRange(\"FRI\", \"MON\"));\n" \ 415 "\n" \ 416 " // Test with GMT time.\n" \ 417 " MockDate.setCurrent(\"Tue Mar 03 2009 GMT\");\n" \ 418 " t.expectEquals(true, weekdayRange(\"MON\", \"FRI\", \"GMT\"));\n" \ 419 " t.expectEquals(true, weekdayRange(\"TUE\", \"FRI\", \"GMT\"));\n" \ 420 " t.expectEquals(true, weekdayRange(\"TUE\", \"TUE\", \"GMT\"));\n" \ 421 " t.expectEquals(true, weekdayRange(\"TUE\", \"GMT\"));\n" \ 422 " t.expectEquals(false, weekdayRange(\"WED\", \"FRI\", \"GMT\"));\n" \ 423 " t.expectEquals(false, weekdayRange(\"SUN\", \"MON\", \"GMT\"));\n" \ 424 " t.expectEquals(false, weekdayRange(\"SAT\", \"GMT\"));\n" \ 425 "};\n" \ 426 "\n" \ 427 "Tests.testDateRange = function(t) {\n" \ 428 " // dateRange(day)\n" \ 429 " MockDate.setCurrent(\"Mar 03 2009\");\n" \ 430 " t.expectEquals(true, dateRange(3));\n" \ 431 " t.expectEquals(false, dateRange(1));\n" \ 432 "\n" \ 433 " // dateRange(day, \"GMT\")\n" \ 434 " MockDate.setCurrent(\"Mar 03 2009 GMT\");\n" \ 435 " t.expectEquals(true, dateRange(3, \"GMT\"));\n" \ 436 " t.expectEquals(false, dateRange(1, \"GMT\"));\n" \ 437 "\n" \ 438 " // dateRange(day1, day2)\n" \ 439 " MockDate.setCurrent(\"Mar 03 2009\");\n" \ 440 " t.expectEquals(true, dateRange(1, 4));\n" \ 441 " t.expectEquals(false, dateRange(4, 20));\n" \ 442 "\n" \ 443 " // dateRange(day, month)\n" \ 444 " MockDate.setCurrent(\"Mar 03 2009\");\n" \ 445 " t.expectEquals(true, dateRange(3, \"MAR\"));\n" \ 446 " MockDate.setCurrent(\"Mar 03 2014\");\n" \ 447 " t.expectEquals(true, dateRange(3, \"MAR\"));\n" \ 448 " // TODO(eroman):\n" \ 449 " //t.expectEquals(false, dateRange(2, \"MAR\"));\n" \ 450 " //t.expectEquals(false, dateRange(3, \"JAN\"));\n" \ 451 "\n" \ 452 " // dateRange(day, month, year)\n" \ 453 " MockDate.setCurrent(\"Mar 03 2009\");\n" \ 454 " t.expectEquals(true, dateRange(3, \"MAR\", 2009));\n" \ 455 " t.expectEquals(false, dateRange(4, \"MAR\", 2009));\n" \ 456 " t.expectEquals(false, dateRange(3, \"FEB\", 2009));\n" \ 457 " MockDate.setCurrent(\"Mar 03 2014\");\n" \ 458 " t.expectEquals(false, dateRange(3, \"MAR\", 2009));\n" \ 459 "\n" \ 460 " // dateRange(month1, month2)\n" \ 461 " MockDate.setCurrent(\"Mar 03 2009\");\n" \ 462 " t.expectEquals(true, dateRange(\"JAN\", \"MAR\"));\n" \ 463 " t.expectEquals(true, dateRange(\"MAR\", \"APR\"));\n" \ 464 " t.expectEquals(false, dateRange(\"MAY\", \"SEP\"));\n" \ 465 "\n" \ 466 " // dateRange(day1, month1, day2, month2)\n" \ 467 " MockDate.setCurrent(\"Mar 03 2009\");\n" \ 468 " t.expectEquals(true, dateRange(1, \"JAN\", 3, \"MAR\"));\n" \ 469 " t.expectEquals(true, dateRange(3, \"MAR\", 4, \"SEP\"));\n" \ 470 " t.expectEquals(false, dateRange(4, \"MAR\", 4, \"SEP\"));\n" \ 471 "\n" \ 472 " // dateRange(month1, year1, month2, year2)\n" \ 473 " MockDate.setCurrent(\"Mar 03 2009\");\n" \ 474 " t.expectEquals(true, dateRange(\"FEB\", 2009, \"MAR\", 2009));\n" \ 475 " MockDate.setCurrent(\"Apr 03 2009\");\n" \ 476 " t.expectEquals(true, dateRange(\"FEB\", 2009, \"MAR\", 2010));\n" \ 477 " t.expectEquals(false, dateRange(\"FEB\", 2009, \"MAR\", 2009));\n" \ 478 "\n" \ 479 " // dateRange(day1, month1, year1, day2, month2, year2)\n" \ 480 " MockDate.setCurrent(\"Mar 03 2009\");\n" \ 481 " t.expectEquals(true, dateRange(1, \"JAN\", 2009, 3, \"MAR\", 2009));\n" \ 482 " t.expectEquals(true, dateRange(3, \"MAR\", 2009, 4, \"SEP\", 2009));\n" \ 483 " t.expectEquals(true, dateRange(3, \"JAN\", 2009, 4, \"FEB\", 2010));\n" \ 484 " t.expectEquals(false, dateRange(4, \"MAR\", 2009, 4, \"SEP\", 2009));\n" \ 485 "};\n" \ 486 "\n" \ 487 "Tests.testTimeRange = function(t) {\n" \ 488 " // timeRange(hour)\n" \ 489 " MockDate.setCurrent(\"Mar 03, 2009 03:34:01\");\n" \ 490 " t.expectEquals(true, timeRange(3));\n" \ 491 " t.expectEquals(false, timeRange(2));\n" \ 492 "\n" \ 493 " // timeRange(hour1, hour2)\n" \ 494 " MockDate.setCurrent(\"Mar 03, 2009 03:34:01\");\n" \ 495 " t.expectEquals(true, timeRange(2, 3));\n" \ 496 " t.expectEquals(true, timeRange(2, 4));\n" \ 497 " t.expectEquals(true, timeRange(3, 5));\n" \ 498 " t.expectEquals(false, timeRange(1, 2));\n" \ 499 " t.expectEquals(false, timeRange(11, 12));\n" \ 500 "\n" \ 501 " // timeRange(hour1, min1, hour2, min2)\n" \ 502 " MockDate.setCurrent(\"Mar 03, 2009 03:34:01\");\n" \ 503 " t.expectEquals(true, timeRange(1, 0, 3, 34));\n" \ 504 " t.expectEquals(true, timeRange(1, 0, 3, 35));\n" \ 505 " t.expectEquals(true, timeRange(3, 34, 5, 0));\n" \ 506 " t.expectEquals(false, timeRange(1, 0, 3, 0));\n" \ 507 " t.expectEquals(false, timeRange(11, 0, 16, 0));\n" \ 508 "\n" \ 509 " // timeRange(hour1, min1, sec1, hour2, min2, sec2)\n" \ 510 " MockDate.setCurrent(\"Mar 03, 2009 03:34:14\");\n" \ 511 " t.expectEquals(true, timeRange(1, 0, 0, 3, 34, 14));\n" \ 512 " t.expectEquals(false, timeRange(1, 0, 0, 3, 34, 0));\n" \ 513 " t.expectEquals(true, timeRange(1, 0, 0, 3, 35, 0));\n" \ 514 " t.expectEquals(true, timeRange(3, 34, 0, 5, 0, 0));\n" \ 515 " t.expectEquals(false, timeRange(1, 0, 0, 3, 0, 0));\n" \ 516 " t.expectEquals(false, timeRange(11, 0, 0, 16, 0, 0));\n" \ 517 "};\n" \ 518 "\n" \ 519 "// --------------------------\n" \ 520 "// TestContext\n" \ 521 "// --------------------------\n" \ 522 "\n" \ 523 "// |name| is the name of the test being executed, it will be used when logging\n" \ 524 "// errors.\n" \ 525 "function TestContext(name) {\n" \ 526 " this.numFailures_ = 0;\n" \ 527 " this.name_ = name;\n" \ 528 "};\n" \ 529 "\n" \ 530 "TestContext.prototype.failed = function() {\n" \ 531 " return this.numFailures_ != 0;\n" \ 532 "};\n" \ 533 "\n" \ 534 "TestContext.prototype.expectEquals = function(expectation, actual) {\n" \ 535 " if (!(expectation === actual)) {\n" \ 536 " this.numFailures_++;\n" \ 537 " this.log(\"FAIL: expected: \" + expectation + \", actual: \" + actual);\n" \ 538 " }\n" \ 539 "};\n" \ 540 "\n" \ 541 "TestContext.prototype.expectTrue = function(x) {\n" \ 542 " this.expectEquals(true, x);\n" \ 543 "};\n" \ 544 "\n" \ 545 "TestContext.prototype.expectFalse = function(x) {\n" \ 546 " this.expectEquals(false, x);\n" \ 547 "};\n" \ 548 "\n" \ 549 "TestContext.prototype.log = function(x) {\n" \ 550 " // Prefix with the test name that generated the log.\n" \ 551 " try {\n" \ 552 " alert(this.name_ + \": \" + x);\n" \ 553 " } catch(e) {\n" \ 554 " // In case alert() is not defined.\n" \ 555 " }\n" \ 556 "};\n" \ 557 "\n" \ 558 "// --------------------------\n" \ 559 "// MockDate\n" \ 560 "// --------------------------\n" \ 561 "\n" \ 562 "function MockDate() {\n" \ 563 " this.wrappedDate_ = new MockDate.super_(MockDate.currentDateString_);\n" \ 564 "};\n" \ 565 "\n" \ 566 "// Setup the MockDate so it forwards methods to \"this.wrappedDate_\" (which is a\n" \ 567 "// real Date object). We can't simply chain the prototypes since Date() doesn't\n" \ 568 "// allow it.\n" \ 569 "MockDate.init = function() {\n" \ 570 " MockDate.super_ = Date;\n" \ 571 "\n" \ 572 " function createProxyMethod(methodName) {\n" \ 573 " return function() {\n" \ 574 " return this.wrappedDate_[methodName]\n" \ 575 " .apply(this.wrappedDate_, arguments);\n" \ 576 " }\n" \ 577 " };\n" \ 578 "\n" \ 579 " for (i in MockDate.methodNames_) {\n" \ 580 " var methodName = MockDate.methodNames_[i];\n" \ 581 " // Don't define the closure directly in the loop body, since Javascript's\n" \ 582 " // crazy scoping rules mean |methodName| actually bleeds out of the loop!\n" \ 583 " MockDate.prototype[methodName] = createProxyMethod(methodName);\n" \ 584 " }\n" \ 585 "\n" \ 586 " // Replace the native Date() with our mock.\n" \ 587 " Date = MockDate;\n" \ 588 "};\n" \ 589 "\n" \ 590 "// Unfortunately Date()'s methods are non-enumerable, therefore list manually.\n" \ 591 "MockDate.methodNames_ = [\n" \ 592 " \"toString\", \"toDateString\", \"toTimeString\", \"toLocaleString\",\n" \ 593 " \"toLocaleDateString\", \"toLocaleTimeString\", \"valueOf\", \"getTime\",\n" \ 594 " \"getFullYear\", \"getUTCFullYear\", \"getMonth\", \"getUTCMonth\",\n" \ 595 " \"getDate\", \"getUTCDate\", \"getDay\", \"getUTCDay\", \"getHours\", \"getUTCHours\",\n" \ 596 " \"getMinutes\", \"getUTCMinutes\", \"getSeconds\", \"getUTCSeconds\",\n" \ 597 " \"getMilliseconds\", \"getUTCMilliseconds\", \"getTimezoneOffset\", \"setTime\",\n" \ 598 " \"setMilliseconds\", \"setUTCMilliseconds\", \"setSeconds\", \"setUTCSeconds\",\n" \ 599 " \"setMinutes\", \"setUTCMinutes\", \"setHours\", \"setUTCHours\", \"setDate\",\n" \ 600 " \"setUTCDate\", \"setMonth\", \"setUTCMonth\", \"setFullYear\", \"setUTCFullYear\",\n" \ 601 " \"toGMTString\", \"toUTCString\", \"getYear\", \"setYear\"\n" \ 602 "];\n" \ 603 "\n" \ 604 "MockDate.setCurrent = function(currentDateString) {\n" \ 605 " MockDate.currentDateString_ = currentDateString;\n" \ 606 "}\n" \ 607 "\n" \ 608 "// Bind the methods to proxy requests to the wrapped Date().\n" \ 609 "MockDate.init();\n" \ 610 "\n" \ 611 612 #define PASSTHROUGH_JS \ 613 u""\ 614 "// Return a single-proxy result, which encodes ALL the arguments that were\n" \ 615 "// passed to FindProxyForURL().\n" \ 616 "\n" \ 617 "function FindProxyForURL(url, host) {\n" \ 618 " if (arguments.length != 2) {\n" \ 619 " throw \"Wrong number of arguments passed to FindProxyForURL!\";\n" \ 620 " return \"FAIL\";\n" \ 621 " }\n" \ 622 "\n" \ 623 " return \"PROXY \" + makePseudoHost(url + \".\" + host);\n" \ 624 "}\n" \ 625 "\n" \ 626 "// Form a string that kind-of resembles a host. We will replace any\n" \ 627 "// non-alphanumeric character with a dot, then fix up the oddly placed dots.\n" \ 628 "function makePseudoHost(str) {\n" \ 629 " var result = \"\";\n" \ 630 "\n" \ 631 " for (var i = 0; i < str.length; ++i) {\n" \ 632 " var c = str.charAt(i);\n" \ 633 " if (!isValidPseudoHostChar(c)) {\n" \ 634 " c = '.'; // Replace unsupported characters with a dot.\n" \ 635 " }\n" \ 636 "\n" \ 637 " // Take care not to place multiple adjacent dots,\n" \ 638 " // a dot at the beginning, or a dot at the end.\n" \ 639 " if (c == '.' &&\n" \ 640 " (result.length == 0 || \n" \ 641 " i == str.length - 1 ||\n" \ 642 " result.charAt(result.length - 1) == '.')) {\n" \ 643 " continue;\n" \ 644 " }\n" \ 645 " result += c;\n" \ 646 " }\n" \ 647 " return result;\n" \ 648 "}\n" \ 649 "\n" \ 650 "function isValidPseudoHostChar(c) {\n" \ 651 " if (c >= '0' && c <= '9')\n" \ 652 " return true;\n" \ 653 " if (c >= 'a' && c <= 'z')\n" \ 654 " return true;\n" \ 655 " if (c >= 'A' && c <= 'Z')\n" \ 656 " return true;\n" \ 657 " return false;\n" \ 658 "}\n" \ 659 660 #define RETURN_EMPTY_STRING_JS \ 661 u""\ 662 "function FindProxyForURL(url, host) {\n" \ 663 " return \"\";\n" \ 664 "}\n" \ 665 "\n" \ 666 667 #define RETURN_FUNCTION_JS \ 668 u""\ 669 "function FindProxyForURL(url, host) {\n" \ 670 " return FindProxyForURL;\n" \ 671 "}\n" \ 672 "\n" \ 673 674 #define RETURN_INTEGER_JS \ 675 u""\ 676 "function FindProxyForURL(url, host) {\n" \ 677 " return 0;\n" \ 678 "}\n" \ 679 "\n" \ 680 681 #define RETURN_NULL_JS \ 682 u""\ 683 "function FindProxyForURL(url, host) {\n" \ 684 " return null;\n" \ 685 "}\n" \ 686 "\n" \ 687 688 #define RETURN_OBJECT_JS \ 689 u""\ 690 "function FindProxyForURL(url, host) {\n" \ 691 " return {result: \"PROXY foo\"};\n" \ 692 "}\n" \ 693 "\n" \ 694 695 #define RETURN_UNDEFINED_JS \ 696 u""\ 697 "function FindProxyForURL(url, host) {\n" \ 698 " return undefined;\n" \ 699 "}\n" \ 700 "\n" \ 701 702 #define RETURN_UNICODE_JS \ 703 u""\ 704 "// U+200B is the codepoint for zero-width-space.\n" \ 705 "function FindProxyForURL(url, host) {\n" \ 706 " return \"PROXY foo.com\u200B\";\n" \ 707 "}\n" \ 708 709 #define SIDE_EFFECTS_JS \ 710 u""\ 711 "if (!gCounter) {\n" \ 712 " // We write it this way so if the script gets loaded twice,\n" \ 713 " // gCounter remains dirty.\n" \ 714 " var gCounter = 0;\n" \ 715 "}\n" \ 716 "\n" \ 717 "function FindProxyForURL(url, host) {\n" \ 718 " return \"PROXY sideffect_\" + gCounter++;\n" \ 719 "}\n" \ 720 "\n" \ 721 722 #define SIMPLE_JS \ 723 u""\ 724 "// PAC script which uses isInNet on both IP addresses and hosts, and calls\n" \ 725 "// isResolvable().\n" \ 726 "\n" \ 727 "function FindProxyForURL(url, host) {\n" \ 728 " var my_ip = myIpAddress();\n" \ 729 "\n" \ 730 " if (isInNet(my_ip, \"172.16.0.0\", \"255.248.0.0\")) {\n" \ 731 " return \"PROXY a:80\";\n" \ 732 " }\n" \ 733 "\n" \ 734 " if (url.substring(0, 6) != \"https:\" &&\n" \ 735 " isInNet(host, \"10.0.0.0\", \"255.0.0.0\")) {\n" \ 736 " return \"PROXY b:80\";\n" \ 737 " }\n" \ 738 "\n" \ 739 " if (dnsDomainIs(host, \"foo.bar.baz.com\") || !isResolvable(host)) {\n" \ 740 " return \"PROXY c:100\";\n" \ 741 " }\n" \ 742 "\n" \ 743 " return \"DIRECT\";\n" \ 744 "}\n" \ 745 746 #define STRING_FUNCTIONS_JS \ 747 u""\ 748 "function FindProxyForURL(url, host) {\n" \ 749 " return \"direct\".toUpperCase();\n" \ 750 "}\n" \ 751 752 #define UNHANDLED_EXCEPTION_JS \ 753 u""\ 754 "function FindProxyForURL(url, host) {\n" \ 755 " // This will throw a runtime exception.\n" \ 756 " return \"PROXY x\" + undefined_variable;\n" \ 757 "}\n" \ 758 "\n" \ 759 760 #endif //PROXY_TEST_SCRIPT_H_ 761