1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/dom/tests/mochitest/whatwg/test_postMessage_origin.xhtml Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,545 @@ 1.4 +<!DOCTYPE html> 1.5 +<html xmlns="http://www.w3.org/1999/xhtml"> 1.6 +<!-- 1.7 +https://bugzilla.mozilla.org/show_bug.cgi?id=417075 1.8 +--> 1.9 +<head> 1.10 + <title>postMessage from about:blank, data URLs</title> 1.11 + <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script> 1.12 + <script type="text/javascript" src="browserFu.js"></script> 1.13 + <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" /> 1.14 +</head> 1.15 +<body> 1.16 +<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=417075">Mozilla Bug 417075</a> 1.17 +<p id="display"></p> 1.18 +<div id="content" style="display: none"></div> 1.19 + 1.20 +<iframe src="http://mochi.test:8888/tests/dom/tests/mochitest/whatwg/postMessage_origin_helper.xhtml" 1.21 + id="sameDomain"></iframe> 1.22 +<iframe src="http://example.com/tests/dom/tests/mochitest/whatwg/postMessage_origin_helper.xhtml" 1.23 + id="otherDomain"></iframe> 1.24 +<iframe src="http://example.org:8000/tests/dom/tests/mochitest/whatwg/postMessage_origin_helper.xhtml" 1.25 + id="otherDomainPort"></iframe> 1.26 +<iframe src="ftp://mochi.test:27534/tests/dom/tests/mochitest/whatwg/postMessage_origin_helper.xhtml" 1.27 + id="localNoExist"></iframe> 1.28 + 1.29 +<iframe src="http://sub1.παράδειγμα.δοκιμή/tests/dom/tests/mochitest/whatwg/postMessage_origin_helper.xhtml" 1.30 + id="idnKidWhitelist"></iframe> 1.31 + 1.32 +<iframe src="http://sub1.exaмple.test/tests/dom/tests/mochitest/whatwg/postMessage_origin_helper.xhtml" 1.33 + id="idnKidNoWhitelist"></iframe> 1.34 + 1.35 + 1.36 +<pre id="test"> 1.37 +<script class="testbody" type="application/javascript"><![CDATA[ 1.38 +/** Test for Bug 417075 **/ 1.39 + 1.40 +SimpleTest.waitForExplicitFinish(); 1.41 + 1.42 +function errorCheck(i, called, error, actual) 1.43 +{ 1.44 + ok(!called, "receiver should not have been called for test #" + i); 1.45 + is(actual, error, "wrong error thrown in test #" + i); 1.46 +} 1.47 + 1.48 +var tests = 1.49 + [ 1.50 + // 0 1.51 + { 1.52 + args: ["NOT-RECEIVED", ""], 1.53 + source: "sameDomain", 1.54 + name: "SyntaxError", 1.55 + code: DOMException.SYNTAX_ERR 1.56 + }, 1.57 + { 1.58 + args: ["NOT-RECEIVED", "null"], 1.59 + source: "sameDomain", 1.60 + name: "SyntaxError", 1.61 + code: DOMException.SYNTAX_ERR 1.62 + }, 1.63 + { 1.64 + args: ["NOT-RECEIVED", "a"], 1.65 + source: "sameDomain", 1.66 + name: "SyntaxError", 1.67 + code: DOMException.SYNTAX_ERR 1.68 + }, 1.69 + { 1.70 + args: ["NOT-RECEIVED", "http :"], 1.71 + source: "sameDomain", 1.72 + name: "SyntaxError", 1.73 + code: DOMException.SYNTAX_ERR 1.74 + }, 1.75 + { 1.76 + args: ["NOT-RECEIVED", "http: //"], 1.77 + source: "sameDomain", 1.78 + name: "SyntaxError", 1.79 + code: DOMException.SYNTAX_ERR, 1.80 + 1.81 + hasThrowsNoExceptionBug: true 1.82 + }, 1.83 + // 5 1.84 + { 1.85 + args: ["NOT-RECEIVED", "http ://"], 1.86 + source: "sameDomain", 1.87 + name: "SyntaxError", 1.88 + code: DOMException.SYNTAX_ERR 1.89 + }, 1.90 + { 1.91 + args: ["TODO", " http://localhost:8888"], 1.92 + source: "sameDomain", 1.93 + name: "SyntaxError", 1.94 + code: DOMException.SYNTAX_ERR, 1.95 + 1.96 + returnOrigin: "http://mochi.test:8888", 1.97 + hasThrowsNoExceptionBug: true 1.98 + }, 1.99 + { 1.100 + args: ["NOT-RECEIVED", "hä"], 1.101 + source: "sameDomain", 1.102 + name: "SyntaxError", 1.103 + code: DOMException.SYNTAX_ERR 1.104 + }, 1.105 + { 1.106 + args: ["NOT-RECEIVED", "http://lo\0k.com"], 1.107 + source: "sameDomain", 1.108 + name: "SyntaxError", 1.109 + code: DOMException.SYNTAX_ERR 1.110 + }, 1.111 + { 1.112 + args: ["NOT-RECEIVED", "http: //localhost:8888"], 1.113 + source: "sameDomain", 1.114 + name: "SyntaxError", 1.115 + code: DOMException.SYNTAX_ERR, 1.116 + 1.117 + hasThrowsNoExceptionBug: true 1.118 + }, 1.119 + // 10 1.120 + { 1.121 + args: ["NOT-RECEIVED", "http://localhost :8888"], 1.122 + source: "sameDomain", 1.123 + name: "SyntaxError", 1.124 + code: DOMException.SYNTAX_ERR 1.125 + }, 1.126 + { 1.127 + args: ["NOT-RECEIVED", "http:// localhost:8888"], 1.128 + source: "sameDomain", 1.129 + name: "SyntaxError", 1.130 + code: DOMException.SYNTAX_ERR, 1.131 + 1.132 + hasThrowsNoExceptionBug: true 1.133 + }, 1.134 + { 1.135 + args: ["TODO", "http://\nlocalhost:8888"], 1.136 + source: "sameDomain", 1.137 + name: "SyntaxError", 1.138 + code: DOMException.SYNTAX_ERR, 1.139 + 1.140 + returnOrigin: "http://mochi.test:8888", 1.141 + hasThrowsNoExceptionBug: true 1.142 + }, 1.143 + { 1.144 + args: ["TODO", "http://localhost:8888\0"], 1.145 + source: "sameDomain", 1.146 + name: "SyntaxError", 1.147 + code: DOMException.SYNTAX_ERR, 1.148 + 1.149 + returnOrigin: "http://mochi.test:8888", 1.150 + hasThrowsNoExceptionBug: true 1.151 + }, 1.152 + { 1.153 + args: ["TODO", "http://localhost:8888\n"], 1.154 + source: "sameDomain", 1.155 + name: "SyntaxError", 1.156 + code: DOMException.SYNTAX_ERR, 1.157 + 1.158 + returnOrigin: "http://mochi.test:8888", 1.159 + hasThrowsNoExceptionBug: true 1.160 + }, 1.161 + // 15 1.162 + { 1.163 + args: ["PASS", "*"], 1.164 + source: "sameDomain", 1.165 + returnOrigin: "http://mochi.test:8888" 1.166 + }, 1.167 + { 1.168 + args: ["PASS", "http://mochi.test:8888"], 1.169 + source: "sameDomain", 1.170 + returnOrigin: "http://mochi.test:8888" 1.171 + }, 1.172 + { 1.173 + args: ["PASS", "http://example.com"], 1.174 + source: "otherDomain", 1.175 + returnOrigin: "http://example.com" 1.176 + }, 1.177 + { 1.178 + args: ["PASS", "http://example.com/"], 1.179 + source: "otherDomain", 1.180 + returnOrigin: "http://example.com" 1.181 + }, 1.182 + { 1.183 + args: ["PASS", "http://example.com:80"], 1.184 + source: "otherDomain", 1.185 + returnOrigin: "http://example.com" 1.186 + }, 1.187 + // 20 1.188 + { 1.189 + args: ["PASS", "http://example.com:80/"], 1.190 + source: "otherDomain", 1.191 + returnOrigin: "http://example.com" 1.192 + }, 1.193 + { 1.194 + args: ["PASS", "http://example.com:80/foobar"], 1.195 + source: "otherDomain", 1.196 + returnOrigin: "http://example.com" 1.197 + }, 1.198 + { 1.199 + args: ["PASS", "http://example.com/foobar"], 1.200 + source: "otherDomain", 1.201 + returnOrigin: "http://example.com" 1.202 + }, 1.203 + { 1.204 + args: ["PASS", "http://example.com:8000"], 1.205 + source: "otherDomain", 1.206 + expectNoCallback: true 1.207 + }, 1.208 + { 1.209 + args: ["PASS", "http://example.com:8000/"], 1.210 + source: "otherDomain", 1.211 + expectNoCallback: true 1.212 + }, 1.213 + // 25 1.214 + { 1.215 + args: ["PASS", "http://example.org:8000"], 1.216 + source: "otherDomainPort", 1.217 + returnOrigin: "http://example.org:8000" 1.218 + }, 1.219 + { 1.220 + args: ["PASS", "http://example.org:8000/"], 1.221 + source: "otherDomainPort", 1.222 + returnOrigin: "http://example.org:8000" 1.223 + }, 1.224 + { 1.225 + args: ["PASS", "http://example.org:8000/tests/dom/test/mochitest/whatwg/postMessage_origin_helper.xhtml"], 1.226 + source: "otherDomainPort", 1.227 + returnOrigin: "http://example.org:8000" 1.228 + }, 1.229 + { 1.230 + args: ["PASS", "http://example.org:8000/tests/dom/test/mochitest/whatwg/this_file_does_not_exist.xhtml"], 1.231 + source: "otherDomainPort", 1.232 + returnOrigin: "http://example.org:8000" 1.233 + }, 1.234 + { 1.235 + args: ["PASS", "http://example.org"], 1.236 + source: "otherDomainPort", 1.237 + expectNoCallback: true 1.238 + }, 1.239 + // 30 1.240 + { 1.241 + args: ["PASS", "http://example.org:80"], 1.242 + source: "otherDomainPort", 1.243 + expectNoCallback: true 1.244 + }, 1.245 + { 1.246 + args: ["PASS", "http://example.org/"], 1.247 + source: "otherDomainPort", 1.248 + expectNoCallback: true 1.249 + }, 1.250 + { 1.251 + args: ["PASS", "http://example.org"], 1.252 + source: "otherDomain", 1.253 + expectNoCallback: true 1.254 + }, 1.255 + { 1.256 + args: ["PASS", "ftp://mochi.test:8888"], 1.257 + source: "sameDomain", 1.258 + expectNoCallback: true 1.259 + }, 1.260 + { 1.261 + args: ["PASS", "http://mochi.test:8888"], 1.262 + source: "sameDomain", 1.263 + returnOrigin: "http://mochi.test:8888" 1.264 + }, 1.265 + // 35 1.266 + { 1.267 + args: ["PASS", "http://mochi.test:27534"], 1.268 + source: "sameDomain", 1.269 + expectNoCallback: true 1.270 + }, 1.271 + { 1.272 + args: ["PASS", "http://sub1.παράδειγμα.δοκιμή"], 1.273 + source: "idnKidWhitelist", 1.274 + returnOrigin: "http://sub1.παράδειγμα.δοκιμή" 1.275 + }, 1.276 + { 1.277 + args: ["PASS", "http://sub1.παράδειγμα.δοκιμή:80"], 1.278 + source: "idnKidWhitelist", 1.279 + returnOrigin: "http://sub1.παράδειγμα.δοκιμή" 1.280 + }, 1.281 + { 1.282 + args: ["PASS", "http://sub1.παράδειγμα.δοκιμή:80/"], 1.283 + source: "idnKidWhitelist", 1.284 + returnOrigin: "http://sub1.παράδειγμα.δοκιμή" 1.285 + }, 1.286 + { 1.287 + args: ["PASS", "http://sub1.παράδειγμα.δοκιμή:80/foobar"], 1.288 + source: "idnKidWhitelist", 1.289 + returnOrigin: "http://sub1.παράδειγμα.δοκιμή" 1.290 + }, 1.291 + // 40 1.292 + { 1.293 + args: ["PASS", "http://sub1.παράδειγμα.δοκιμή/foobar"], 1.294 + source: "idnKidWhitelist", 1.295 + returnOrigin: "http://sub1.παράδειγμα.δοκιμή" 1.296 + }, 1.297 + { 1.298 + args: ["PASS", "http://sub1.xn--hxajbheg2az3al.xn--jxalpdlp"], 1.299 + source: "idnKidWhitelist", 1.300 + returnOrigin: "http://sub1.παράδειγμα.δοκιμή" 1.301 + }, 1.302 + { 1.303 + args: ["PASS", "http://sub1.xn--hxajbheg2az3al.xn--jxalpdlp:80"], 1.304 + source: "idnKidWhitelist", 1.305 + returnOrigin: "http://sub1.παράδειγμα.δοκιμή" 1.306 + }, 1.307 + { 1.308 + args: ["PASS", "http://sub1.xn--hxajbheg2az3al.xn--jxalpdlp:80/"], 1.309 + source: "idnKidWhitelist", 1.310 + returnOrigin: "http://sub1.παράδειγμα.δοκιμή" 1.311 + }, 1.312 + { 1.313 + args: ["PASS", "http://sub1.xn--hxajbheg2az3al.xn--jxalpdlp:80/foo"], 1.314 + source: "idnKidWhitelist", 1.315 + returnOrigin: "http://sub1.παράδειγμα.δοκιμή" 1.316 + }, 1.317 + // 45 1.318 + { 1.319 + args: ["PASS", "http://sub1.exaмple.test"], 1.320 + source: "idnKidNoWhitelist", 1.321 + returnOrigin: "http://sub1.exaмple.test", 1.322 + 1.323 + hasWrongReturnOriginBug: true 1.324 + }, 1.325 + { 1.326 + args: ["PASS", "http://sub1.exaмple.test:80"], 1.327 + source: "idnKidNoWhitelist", 1.328 + returnOrigin: "http://sub1.exaмple.test", 1.329 + 1.330 + hasWrongReturnOriginBug: true 1.331 + }, 1.332 + { 1.333 + args: ["PASS", "http://sub1.exaмple.test:80/"], 1.334 + source: "idnKidNoWhitelist", 1.335 + returnOrigin: "http://sub1.exaмple.test", 1.336 + 1.337 + hasWrongReturnOriginBug: true 1.338 + }, 1.339 + { 1.340 + args: ["PASS", "http://sub1.exaмple.test/"], 1.341 + source: "idnKidNoWhitelist", 1.342 + returnOrigin: "http://sub1.exaмple.test", 1.343 + 1.344 + hasWrongReturnOriginBug: true 1.345 + }, 1.346 + { 1.347 + args: ["PASS", "http://sub1.exaмple.test/foobar"], 1.348 + source: "idnKidNoWhitelist", 1.349 + returnOrigin: "http://sub1.exaмple.test", 1.350 + 1.351 + hasWrongReturnOriginBug: true 1.352 + }, 1.353 + // 50 1.354 + { 1.355 + args: ["PASS", "http://sub1.xn--exaple-kqf.test"], 1.356 + source: "idnKidNoWhitelist", 1.357 + returnOrigin: "http://sub1.exaмple.test", 1.358 + 1.359 + hasWrongReturnOriginBug: true 1.360 + }, 1.361 + { 1.362 + args: ["PASS", "http://sub1.xn--exaple-kqf.test:80"], 1.363 + source: "idnKidNoWhitelist", 1.364 + returnOrigin: "http://sub1.exaмple.test", 1.365 + 1.366 + hasWrongReturnOriginBug: true 1.367 + }, 1.368 + { 1.369 + args: ["PASS", "http://sub1.xn--exaple-kqf.test:80/"], 1.370 + source: "idnKidNoWhitelist", 1.371 + returnOrigin: "http://sub1.exaмple.test", 1.372 + 1.373 + hasWrongReturnOriginBug: true 1.374 + }, 1.375 + { 1.376 + args: ["PASS", "http://sub1.xn--exaple-kqf.test/"], 1.377 + source: "idnKidNoWhitelist", 1.378 + returnOrigin: "http://sub1.exaмple.test", 1.379 + 1.380 + hasWrongReturnOriginBug: true 1.381 + }, 1.382 + { 1.383 + args: ["PASS", "http://sub1.xn--exaple-kqf.test/foobar"], 1.384 + source: "idnKidNoWhitelist", 1.385 + returnOrigin: "http://sub1.exaмple.test", 1.386 + 1.387 + hasWrongReturnOriginBug: true 1.388 + }, 1.389 + // 55 1.390 + { 1.391 + args: ["NOT-RECEIVED", undefined], 1.392 + source: "sameDomain", 1.393 + name: "SyntaxError", 1.394 + code: DOMException.SYNTAX_ERR 1.395 + }, 1.396 + ]; 1.397 + 1.398 +function allTests(callback) 1.399 +{ 1.400 + var test, target, called; 1.401 + 1.402 + function eventCheck(evt) 1.403 + { 1.404 + ok(test === tests[i], 1.405 + "i and test are out of sync! async is hard, let's go shopping"); 1.406 + 1.407 + var originCheck = test.hasWrongReturnOriginBug ? todo_is : is; 1.408 + originCheck(evt.origin, test.returnOrigin, "wrong origin for #" + i); 1.409 + if (test.args[0] == "TODO") 1.410 + todo_is(evt.data, "PASS", "wrong data"); 1.411 + else 1.412 + is(evt.data, "PASS", "wrong data"); 1.413 + is(evt.lastEventId, "", 1.414 + "postMessage creates events with empty lastEventId"); 1.415 + ok(evt.source === target, "wrong source"); 1.416 + called = true; 1.417 + } 1.418 + 1.419 + function nextTest() 1.420 + { 1.421 + i++; 1.422 + 1.423 + if (i === end) 1.424 + { 1.425 + removeListener(listener); 1.426 + 1.427 + setTimeout(callback, 0); 1.428 + return; 1.429 + } 1.430 + 1.431 + test = tests[i]; 1.432 + 1.433 + target = $(test.source).contentWindow; 1.434 + try 1.435 + { 1.436 + called = false; 1.437 + target.postMessage.apply(target, test.args); 1.438 + } 1.439 + catch (e) 1.440 + { 1.441 + // Since an exception was thrown, we know at this point that we're not 1.442 + // waiting on anything else in the queue of script to run, and we can just 1.443 + // call nextTest directly. 1.444 + errorCheck(i, called, e.name, test.name); 1.445 + errorCheck(i, called, e.code, test.code); 1.446 + nextTest(); 1.447 + return; 1.448 + } 1.449 + 1.450 + // We've set up the event generated by postMessage to be dispatched, and 1.451 + // it's sitting in a queue somewhere. However, it hasn't arrived yet, and 1.452 + // the target document hasn't received the event -- so we need to continue 1.453 + // our tests later. 1.454 + setTimeout(function() 1.455 + { 1.456 + // The event was dispatched, and the target frame's code processed it and 1.457 + // responded -- so now *that* response is sitting in the queue of script to 1.458 + // execute. Process pending script one more time so we know we've gotten 1.459 + // the response. 1.460 + setTimeout(function() 1.461 + { 1.462 + // Finally, we've posted our message and received our response, and 1.463 + // there's nothing further coming down the pipe. 1.464 + if (test.hasThrowsNoExceptionBug) 1.465 + todo(false, "should throw on test #" + i); 1.466 + else 1.467 + ok(test.expectNoCallback ^ called, "should have been called #" + i); 1.468 + 1.469 + nextTest(); 1.470 + }, 0); 1.471 + }, 0); 1.472 + } 1.473 + 1.474 + var listener = registerMessageListener(eventCheck); 1.475 + 1.476 + var i = -1, end = tests.length; 1.477 + nextTest(); 1.478 +} 1.479 + 1.480 +function registerMessageListener(func, callback) 1.481 +{ 1.482 + function receive(evt) 1.483 + { 1.484 + func(evt); 1.485 + if (callback) 1.486 + callback(); 1.487 + } 1.488 + 1.489 + window.addEventListener("message", receive, false); 1.490 + 1.491 + return receive; 1.492 +} 1.493 + 1.494 +function removeListener(listener) 1.495 +{ 1.496 + window.removeEventListener("message", listener, false); 1.497 +} 1.498 + 1.499 +function oddballTests(callback) 1.500 +{ 1.501 + var called = false; 1.502 + 1.503 + function eventChecks(evt) 1.504 + { 1.505 + is(evt.origin, "http://mochi.test:8888", "wrong sender"); 1.506 + is(evt.data, "PASS", "wrong data"); 1.507 + is(evt.lastEventId, "", 1.508 + "postMessage creates events with empty lastEventId"); 1.509 + ok(evt.source === window, "wrong source"); 1.510 + called = true; 1.511 + } 1.512 + 1.513 + var listener = registerMessageListener(eventChecks, function() 1.514 + { 1.515 + is(called, true, "should have been called"); 1.516 + removeListener(listener); 1.517 + 1.518 + called = false; 1.519 + listener = registerMessageListener(eventChecks, function() 1.520 + { 1.521 + is(called, true, "should have been called"); 1.522 + 1.523 + removeListener(listener); 1.524 + callback(); 1.525 + }); 1.526 + 1.527 + window.postMessage("PASS", "http://mochi.test:8888"); 1.528 + }); 1.529 + 1.530 + window.postMessage("PASS", "http://mochi.test:8888"); 1.531 +} 1.532 + 1.533 +function run() 1.534 +{ 1.535 + oddballTests(function() 1.536 + { 1.537 + allTests(function() 1.538 + { 1.539 + SimpleTest.finish(); 1.540 + }); 1.541 + }); 1.542 +} 1.543 + 1.544 +window.addEventListener("load", run, false); 1.545 +]]></script> 1.546 +</pre> 1.547 +</body> 1.548 +</html>