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