<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <script src="/js-test-resources/js-test.js"></script>
    <script src="/cookies/resources/cookie-utilities.js"></script>
    <script src="resources/util.js"></script>
</head>
<body onload="runTest()">
<script>
    description("Test for CNAME Cloaking: Cookie expiry capping if the top frame has an alternate CNAME and the sub resource has a different alternate CNAME.");
    jsTestIsAsync = true;

    function finishTest() {
        resetCookiesForCurrentOrigin();
        setEnableFeature(false, function() {
            finishJSTest();
        });
    }

    const oneWeekInSeconds = 7 * 24 * 60 * 60;
    const overOneWeekInSeconds = oneWeekInSeconds + 600;
    const underOneWeekInSeconds = oneWeekInSeconds - 600;

    function checkCookieExpiry(cookieData, seconds, shouldBeLessThan) {
        let now = new Date();
        let expiryDateInMilliseconds = now.getTime() + (seconds * 1000);

        if ((cookieData["expires"] < expiryDateInMilliseconds) === shouldBeLessThan) {
            let underOneWeekExpiryDateInMilliseconds = now.getTime() + (underOneWeekInSeconds * 1000);
            if (cookieData["expires"] < underOneWeekExpiryDateInMilliseconds)
                testFailed("Cookie named " + cookieData["name"] + " was capped too low.");
            else
                testPassed("Cookie named " + cookieData["name"] + " expires in " + (shouldBeLessThan ? "less" : "more") + " than " + seconds + " seconds.");
        } else
            testFailed("Cookie named " + cookieData["name"] + " expires in " + (shouldBeLessThan ? "more" : "less") + " than " + seconds + " seconds.");
    }

    const cookieName = "cnameTestCookie";
    const subPathToSetFirstPartyCookie = "resources/set-cookie.py?name=" + cookieName + "&value=value&dummy=" + Math.random();

    function setAndCheckCookie() {
        fetch(subPathToSetFirstPartyCookie).then(function() {
            if (internals) {
                let cookies = internals.getCookies();
                if (!cookies.length)
                    testFailed("No cookies found.");
                for (let cookie of cookies) {
                    if (cookie.name === cookieName) {
                        checkCookieExpiry(cookie, overOneWeekInSeconds, true);
                    } else {
                        testFailed("Unknown cookie " + cookie.name + " found.");
                    }
                }
                finishTest();
            }
        });
    }

    const firstParty = "http://127.0.0.1:8000";
    const firstPartyCNAMEForTesting = "http://testwebkit.org";
    const thirdPartyCNAMEForTesting = "http://3rdpartytestwebkit.org";

    function runTest() {
        setEnableFeature(true, function() {
            testRunner.statisticsSetFirstPartyHostCNAMEDomain(firstParty, firstPartyCNAMEForTesting, function() {
                testRunner.statisticsSetThirdPartyCNAMEDomain(thirdPartyCNAMEForTesting, function() {
                    setAndCheckCookie();
                });
            });
        });
    }
</script>
</body>
