<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Test for setting document.location.host to unaccepted values</title>
</head>
<body>
<pre id="pre"></pre>
<script>
    // This function will be redefined as the test cases get executed
    var testCaseOnload = function() {
        runOneTestThatShouldNotBeAccepted(0);
    };

    var iframeDocument;
    function iframeOnload() {
        iframeDocument = document.getElementById("targetFrame").contentDocument;
        testCaseOnload();
    }

    function log(s) {
        document.getElementById("pre").appendChild(document.createTextNode(s));
    }

    function runOneTestThatShouldNotBeAccepted(testIndex) {
        var testCasesThatShouldNotBeAccepted = [
            { hostString : 'icl' + '\uf877' + 'oud.com', description : "'icl' + '\\uf877' + 'oud.com'" }
            ,{ hostString : 'icloud.com:-1', description : false }
            ,{ hostString : 'icloud.com:11c', description : false }
            ,{ hostString : 'icloud.com:11', description : false }
            ,{ hostString : 'icloud.com:80:', description : false }
            ,{ hostString : 'icloud.com:80:80', description : false }
            ,{ hostString : 'icloud.com:', description : false }
            ,{ hostString : ':80', description : false }
            ,{ hostString : ':', description : false }
            ,{ hostString : '', description : "empty string" }
        ];

        if (testIndex >= testCasesThatShouldNotBeAccepted.length) {
            if (window.testRunner)
                testRunner.notifyDone();
        } else {
            // Prepare for next test case by setting testCaseOnload to load the test case
            // and then load the simple HTML file that says "Before"
            testCaseOnload = function () {

                // Prepare test case execution by first setting testCaseOnload to check
                // the test outcome and then load set the iframe's document.location.host
                testCaseOnload = function(currentTestIndex) {
                    return function () {
                        if (iframeDocument.location.host == document.location.host) {
                            var currentCase = testCasesThatShouldNotBeAccepted[currentTestIndex],
                                    description = (currentCase.description ? currentCase.description : currentCase.hostString);
                            log("PASS: " + description + " was not accepted.\n");
                            runOneTestThatShouldNotBeAccepted(currentTestIndex + 1);
                        } else {
                            // Fails may very well become 404s so this log message might not be triggered
                            log("FAIL: " + description + " was accepted, or at least the iframeDocument.location.host had changed to " + iframeDocument.location.host + "\n");
                        }
                    };
                }(testIndex);

                iframeDocument.location.host = testCasesThatShouldNotBeAccepted[testIndex].hostString;
            };

            iframeDocument.location = "./resources/set-document-location-iframe.html";
        }
    }

    if (window.testRunner) {
        testRunner.dumpAsText();
        testRunner.waitUntilDone();
    }
</script>
<iframe id="targetFrame" src="resources/set-document-location-iframe.html" onload="iframeOnload()"></iframe>
</body>
</html>