chromium/third_party/blink/web_tests/http/tests/security/postMessage/target-origin.html

<!DOCTYPE html>
<html>
<head>
<script src="resources/recv.js"></script>
<script>
if (window.testRunner) {
    testRunner.dumpAsText();
    // We have different console output in --site-per-process mode. See https://crbug.com/614413.
    testRunner.setDumpConsoleMessages(false);
    testRunner.waitUntilDone();
}

var totalExpectedReplies = 0;
var receivedMessages = [];

function tryPostMessage(win, origin, shouldExpectReply) {
    try {
        win.postMessage("Trying origin=" + origin, origin);
        if (shouldExpectReply)
            totalExpectedReplies++;
    } catch(ex) {
        log("Error sending message to " + origin + ". " + ex);
    }
}

function receiveAndSort(e) {
    var msg = extractMessage(e);
    receivedMessages.push(msg);
    if (receivedMessages.length == totalExpectedReplies) {
        receivedMessages.sort();
        receivedMessages.map(log);
        if (window.testRunner)
            testRunner.notifyDone();
    }
}

addEventListener("message", receiveAndSort, false);

function test() {
    var winLocalhost = document.getElementById('iframe-localhost').contentWindow;
    var win127= document.getElementById('iframe-127').contentWindow;

    // Should succeed:
    tryPostMessage(winLocalhost, "http://localhost:8000", true);
    tryPostMessage(winLocalhost, "http://localhost:8000/", true);
    tryPostMessage(winLocalhost, "http://localhost:8000/foo", true);
    tryPostMessage(winLocalhost, "http://localhost:8000/foo?bar", true);
    tryPostMessage(winLocalhost, "http://localhost:8000/foo?bar#baz", true);
    tryPostMessage(winLocalhost, "http://user:pass@localhost:8000/foo?bar#baz", true);
    tryPostMessage(winLocalhost, "*", true);
    tryPostMessage(win127, "/", true);
    tryPostMessage(win127, undefined, true);
    tryPostMessage(win127, null, true);

    // Should fail:
    tryPostMessage(winLocalhost, "/", false);
    tryPostMessage(winLocalhost, "http://localhost:9090", false);
    tryPostMessage(winLocalhost, "http://localhost", false);
    tryPostMessage(winLocalhost, "https://localhost", false);
    tryPostMessage(winLocalhost, "https://localhost:8000", false);
    tryPostMessage(winLocalhost, "http://www.example.com", false);

    // Should throw syntax error:
    tryPostMessage(win127, "//", false);
}

</script>
<body onload="test()">
<div>window.location.href = <script>document.write(window.location.href);</script></div>
<div><iframe src="http://localhost:8000/security/postMessage/resources/post-message-listener.html"
  id="iframe-localhost" width="800" height="300" style="border: 1px solid black;">
</iframe><iframe src="http://127.0.0.1:8000/security/postMessage/resources/post-message-listener.html"
  id="iframe-127" width="800" height="300" style="border: 1px solid black;"></iframe>
<div id="result">waiting...</div>
</body>
</html>