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