Commit 946e66a2 authored by Friedrich Horschig's avatar Friedrich Horschig Committed by Commit Bot

Add http-state cookie tests from abart/http-state github repository

These are the first steps to port the http-state cookie tests from
https://github.com/abarth/http-state/tree/master/tests
to the external/wpt/ directory.

Accessing <server>/cookies/http-state/all-tests.html
will run all tests in cookies/http-state/resources/test-files/.
This ensures that tests stay isolated, ignore set cookies and clean
up cookies after themselves.

By accessing the server directly, single tests can be debugged.
e.g. to run test file 0005 in isolation, access this URL:
<server>/cookies/http-state/resources/cookie-setter.py?debug=0005

Change-Id: I1fceccf0bc5d080d024998451abd027142d6a16a
Reviewed-on: https://chromium-review.googlesource.com/807988
Commit-Queue: Friedrich Horschig <fhorschig@chromium.org>
Reviewed-by: default avatarMike West <mkwst@chromium.org>
Cr-Commit-Position: refs/heads/master@{#542085}
parent 09e3be47
This is a testharness.js-based test.
PASS 0001 - Set cookie.
PASS 0002 - Set cookie with future expiration.
PASS 0003 - Set expired cookie along with valid cookie.
FAIL 0004 - Ignore cookie without key. assert_equals: expected "" but got "foo"
PASS 0005 - Set cookie with age.
PASS 0006 - Set no cookie with max-age=0.
PASS 0007 - Set cookie with version=1.
PASS 0008 - Set cookie with version=1000.
PASS 0009 - Set cookie with custom value.
PASS 0010 - Dont accept 'secure' cookies over http.
PASS 0011 - Ignore separators in cookie values.
PASS 0012 - Ignore values with separators and without ';'.
PASS 0013 - Use last value for cookies with identical keys.
PASS 0014 - Keep alphabetic key order.
PASS 0015 - Keep alphabetic single-char key order.
PASS 0016 - Keep non-alphabetic key order.
PASS 0017 - Keep order if comma-separated.
PASS 0018 - Ignore keys after semicolon.
PASS 0019 - Ignore attributes after semicolon.
FAIL 0020 - Ignore cookies without key and value. assert_equals: expected "a=b; c=d" but got "a=b; ; c=d"
FAIL 0021 - Ignore cookie without key in all 'Set-Cookie'. assert_equals: expected "a=b; c=d" but got "a=b; x; c=d"
PASS 0022 - Set cookie without value in all 'Set-Cookie'.
PASS 0023 - Ignore cookies without '=' in all 'Set-Cookie'.
PASS 0024 - Ignore malformed cookies in all 'Set-Cookie'.
PASS 0025 - Ignore cookies with ';' in all 'Set-Cookie'.
PASS 0026 - Ignore malformed cookies in all 'Set-Cookie' v2.
FAIL 0027 - Ignore malformed cookies in all 'Set-Cookie' v3. assert_equals: expected "" but got "bar"
FAIL 0028 - [INVALID EXPECTATION] Ignore malformed cookies in all 'Set-Cookie' v4. assert_equals: expected "Set-Cookie: foo Set-Cookie:" but got ""
Harness: the test ran to completion.
<!doctype html>
<html>
<head>
<meta charset=utf-8>
<title>Tests basic cookie setting functionality</title>
<meta name=help href="https://tools.ietf.org/html/rfc6265#page-8">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="resources/cookie-http-state-template.js"></script>
</head>
<body>
<div id="log"></div>
<script>
setup({ explicit_timeout: true });
const TEST_CASES = [
{file: "0001", name: "Set cookie."},
{file: "0002", name: "Set cookie with future expiration."},
{file: "0003", name: "Set expired cookie along with valid cookie."},
{file: "0004", name: "Ignore cookie without key."},
{file: "0005", name: "Set cookie with age."},
{file: "0006", name: "Set no cookie with max-age=0."},
{file: "0007", name: "Set cookie with version=1."},
{file: "0008", name: "Set cookie with version=1000."},
{file: "0009", name: "Set cookie with custom value."},
// TODO(fhorschig): Could 0010 break when run on a HTTPS try server?
{file: "0010", name: "Dont accept 'secure' cookies over http."},
{file: "0011", name: "Ignore separators in cookie values."},
{file: "0012", name: "Ignore values with separators and without ';'."},
{file: "0013", name: "Use last value for cookies with identical keys."},
{file: "0014", name: "Keep alphabetic key order."},
{file: "0015", name: "Keep alphabetic single-char key order."},
{file: "0016", name: "Keep non-alphabetic key order."},
{file: "0017", name: "Keep order if comma-separated."},
{file: "0018", name: "Ignore keys after semicolon."},
{file: "0019", name: "Ignore attributes after semicolon."},
{file: "0020", name: "Ignore cookies without key and value."},
{file: "0021", name: "Ignore cookie without key in all 'Set-Cookie'."},
{file: "0022", name: "Set cookie without value in all 'Set-Cookie'."},
{file: "0023", name: "Ignore cookies without '=' in all 'Set-Cookie'."},
{file: "0024", name: "Ignore malformed cookies in all 'Set-Cookie'."},
{file: "0025", name: "Ignore cookies with ';' in all 'Set-Cookie'."},
{file: "0026", name: "Ignore malformed cookies in all 'Set-Cookie' v2."},
{file: "0027", name: "Ignore malformed cookies in all 'Set-Cookie' v3."},
// TODO(fhorschig): Ask about 0028's expectations ... should be empty?
{file: "0028", name: "[INVALID EXPECTATION] Ignore malformed cookies in all 'Set-Cookie' v4."},
];
for (const i in TEST_CASES) {
const t = TEST_CASES[i];
promise_test(createCookieTest(t.file),
t.file + " - " + t.name,
{ timeout: 3000 });
}
</script>
</body>
</html>
const SERVER_LOCATION = "resources";
const SERVER_SCRIPT = SERVER_LOCATION + "/cookie-setter.py";
function stripPrefixAndWhitespace(cookie_text) {
return cookie_text.replace(/^Cookie: /, '').replace(/^\s+|\s+$/g, '');
}
function getLocalResourcesPath() {
return location.pathname.replace(/[^\/]*$/, "") + SERVER_LOCATION;
}
function getAbsoluteServerLocation() {
return getLocalResourcesPath().replace(/resources.*$/,'')+ SERVER_SCRIPT;
}
function expireCookie(name, expiry_date, path) {
name = name || "";
expiry_date = expiry_date || "Thu, 01 Jan 1970 00:00:00 UTC";
path = path || getLocalResourcesPath();
document.cookie = name + "=; expires=" + expiry_date + "; path=" + path + ";";
}
function CookieManager() {
this.initial_cookies = [];
}
CookieManager.prototype.parse = document_cookies => {
this.initial_cookies = [];
document_cookies = document_cookies.replace(/^Cookie: /, '');
if (document_cookies != "") {
this.initial_cookies = document_cookies.split(/\s*;\s*/);
}
}
CookieManager.prototype.diffWith = document_cookies => {
this.actual_cookies = document_cookies;
for (let i in initial_cookies) {
let no_spaces_cookie_regex =
new RegExp(/\s*[\;]*\s/.source + initial_cookies[i]);
this.actual_cookies = actual_cookies.replace(no_spaces_cookie_regex, '');
}
return this.actual_cookies;
}
CookieManager.prototype.resetCookies = () => {
expireCookie(/*name=*/""); // If a cookie without keys was accepted, drop it.
if (this.actual_cookies == "") {
return; // There is nothing to reset here.
}
let cookies_to_delete = this.actual_cookies.split(/\s*;\s*/)
for (let i in cookies_to_delete){
expireCookie(cookies_to_delete[i].replace(/=.*$/, ""));
// Drop cookies with same name that were set to the root path which happens
// for example due to "0010" still failing.
expireCookie(cookies_to_delete[i].replace(/=.*$/, ""),
/*expiry_date=*/null,
/*path=*/'/');
}
}
function createCookieTest(file) {
return t => {
const iframe = document.createElement('iframe');
document.body.appendChild(iframe);
let diff_tool = new CookieManager();
t.add_cleanup(diff_tool.resetCookies);
return new Promise((resolve, reject) => {
diff_tool.parse(document.cookie);
window.addEventListener("message", t.step_func(e => {
assert_true(!!e.data, "Message contains data")
resolve(e.data);
}));
iframe.src = getAbsoluteServerLocation() + "?file=" + file;
}).then((response) => {
let actual_cookies = diff_tool.diffWith(response.cookies);
let expected_cookies = stripPrefixAndWhitespace(response.expectation);
assert_equals(actual_cookies, expected_cookies);
});
}
};
from os import path;
SETUP_FILE_TEMPLATE = "{}-test"
EXPECTATION_FILE_TEMPLATE = "{}-expected"
EXPECTATION_HTML_SCAFFOLD = "iframe-expectation-doc.html.py-str"
DEBUGGING_HTML_SCAFFOLD = "debugging-single-test.html.py-str"
DEFAULT_RESOURCE_DIR = path.join("cookies", "http-state", "resources")
DEFAULT_TEST_DIR = "test-files"
def dump_file(directory, filename):
return open(path.join(directory, filename), "r").read()
class CookieTestResponse(object):
def __init__(self, file, root):
super(CookieTestResponse, self).__init__()
self.__test_file = SETUP_FILE_TEMPLATE.format(file)
self.__expectation_file = EXPECTATION_FILE_TEMPLATE.format(file)
self.__resources_dir = path.join(root, DEFAULT_RESOURCE_DIR)
self.__test_files_dir = path.join(self.__resources_dir, DEFAULT_TEST_DIR)
def cookie_setting_header(self):
return dump_file(self.__test_files_dir, self.__test_file)
def body_with_expectation(self):
html_frame = dump_file(self.__resources_dir, EXPECTATION_HTML_SCAFFOLD)
expected_data = dump_file(self.__test_files_dir, self.__expectation_file);
return html_frame.format(**{'data' : expected_data})
def main(request, response):
if "debug" in request.GET:
response.writer.write_status(200)
response.writer.end_headers()
html_frame = dump_file(path.join(request.doc_root, DEFAULT_RESOURCE_DIR),
DEBUGGING_HTML_SCAFFOLD)
test_file = html_frame % (request.GET['debug'])
response.writer.write_content(test_file)
return;
if not "file" in request.GET:
response.writer.write_status(404)
response.writer.end_headers()
response.writer.write_content("The 'file' parameter is missing!")
return;
cookie_response = CookieTestResponse(request.GET['file'], request.doc_root)
response.writer.write_status(200)
response.writer.write(cookie_response.cookie_setting_header())
response.writer.end_headers()
response.writer.write_content(cookie_response.body_with_expectation())
<!doctype html>
<html>
<head>
<meta charset=utf-8>
<title>Debug single test</title>
<meta name=help href="https://tools.ietf.org/html/rfc6265#page-8">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="cookie-http-state-template.js"></script>
</head>
<body>
<div id="log"></div>
<script>
setup({ explicit_timeout: true });
promise_test(createCookieTest("%s"), "DEBUG", { timeout: 3000 });
</script>
</body>
</html>
window.top.postMessage({
"cookies": document.cookie,
"expectation": document.querySelector('#data').innerText
}, "*");
<!doctype html>
<html>
<head>
<meta charset=utf-8>
<title>Cookie Test Expectation Document</title>
</head>
<body>
<div id="data">{data}</div>
<script src="iframe-content-pushing.js"></script>
</body>
</html>
Set-Cookie: foo=bar; Expires=Fri, 07 Aug 2007 08:04:19 GMT
Set-Cookie: foo2=bar2; Expires=Fri, 07 Aug 2027 08:04:19 GMT
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment