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