Commit ea23184d authored by Matt Falkenhagen's avatar Matt Falkenhagen Committed by Commit Bot

service worker: WPT: update Service-Worker-Allowed test to match spec.

The spec changed recently at
https://github.com/w3c/ServiceWorker/issues/1307.

Also refactor the tests to eliminate duplicate code and properly
cleanup.

Bug: 968436
Change-Id: I56d649769a64d064dc7342961dd6c4763df7e037
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1637019Reviewed-by: default avatarBen Kelly <wanderview@chromium.org>
Commit-Queue: Matt Falkenhagen <falken@chromium.org>
Cr-Commit-Position: refs/heads/master@{#664886}
parent fc03b2d9
This is a testharness.js-based test.
PASS Registering within Service-Worker-Allowed path
PASS Registering within Service-Worker-Allowed path (absolute URL)
PASS Registering within Service-Worker-Allowed path with parent reference
PASS Registering outside Service-Worker-Allowed path
PASS Registering outside Service-Worker-Allowed path with parent reference
FAIL Service-Worker-Allowed is cross-origin to script, registering on a normally allowed scope assert_unreached: Should have rejected: undefined Reached unreachable code
FAIL Service-Worker-Allowed is cross-origin to script, registering on a normally disallowed scope assert_unreached: Should have rejected: undefined Reached unreachable code
PASS Service-Worker-Allowed is cross-origin to page, same-origin to script
Harness: the test ran to completion.
......@@ -17,88 +17,72 @@ function build_script_url(allowed_path, origin) {
return `${url}?pipe=header(Service-Worker-Allowed,${allowed_path})`;
}
promise_test(async t => {
const script = build_script_url('/allowed-path');
const scope = '/allowed-path';
const registration = await service_worker_unregister_and_register(
t, script, scope);
assert_true(registration instanceof ServiceWorkerRegistration, 'registered');
assert_equals(registration.scope, normalizeURL(scope));
return registration.unregister();
}, 'Registering within Service-Worker-Allowed path');
// register_test is a promise_test that registers a service worker.
function register_test(script, scope, description) {
promise_test(async t => {
t.add_cleanup(() => {
return service_worker_unregister(t, scope);
});
promise_test(async t => {
const script = build_script_url(new URL('/allowed-path', document.location));
const scope = '/allowed-path';
const registration = await service_worker_unregister_and_register(
t, script, scope);
assert_true(registration instanceof ServiceWorkerRegistration, 'registered');
assert_equals(registration.scope, normalizeURL(scope));
return registration.unregister();
}, 'Registering within Service-Worker-Allowed path (absolute URL)');
const registration = await service_worker_unregister_and_register(
t, script, scope);
assert_true(registration instanceof ServiceWorkerRegistration, 'registered');
assert_equals(registration.scope, normalizeURL(scope));
}, description);
}
// register_fail_test is like register_test but expects a SecurityError.
function register_fail_test(script, scope, description) {
promise_test(async t => {
t.add_cleanup(() => {
return service_worker_unregister(t, scope);
});
await service_worker_unregister(t, scope);
await promise_rejects(t,
'SecurityError',
navigator.serviceWorker.register(script, {scope}));
}, description);
}
register_test(
build_script_url('/allowed-path'),
'/allowed-path',
'Registering within Service-Worker-Allowed path');
promise_test(async t => {
const script = build_script_url('../allowed-path-with-parent');
const scope = 'allowed-path-with-parent';
const registration = await service_worker_unregister_and_register(
t, script, scope);
assert_true(registration instanceof ServiceWorkerRegistration, 'registered');
assert_equals(registration.scope, normalizeURL(scope));
return registration.unregister();
}, 'Registering within Service-Worker-Allowed path with parent reference');
register_test(
build_script_url(new URL('/allowed-path', document.location)),
'/allowed-path',
'Registering within Service-Worker-Allowed path (absolute URL)');
promise_test(async t => {
const script = build_script_url('../allowed-path');
const scope = '/disallowed-path';
await service_worker_unregister(t, scope);
return promise_rejects(t,
'SecurityError',
navigator.serviceWorker.register(script, {scope: scope}),
'register should fail');
}, 'Registering outside Service-Worker-Allowed path');
register_test(
build_script_url('../allowed-path-with-parent'),
'allowed-path-with-parent',
'Registering within Service-Worker-Allowed path with parent reference');
promise_test(async t => {
const script = build_script_url('../allowed-path-with-parent');
const scope = '/allowed-path-with-parent';
await service_worker_unregister(t, scope);
return promise_rejects(t,
'SecurityError',
navigator.serviceWorker.register(script, {scope: scope}),
'register should fail');
}, 'Registering outside Service-Worker-Allowed path with parent reference');
register_fail_test(
build_script_url('../allowed-path'),
'/disallowed-path',
'Registering outside Service-Worker-Allowed path'),
promise_test(async t => {
const script = build_script_url(
host_info.HTTPS_REMOTE_ORIGIN + '/');
const scope = 'resources/this-scope-is-normally-allowed'
const registration = await service_worker_unregister_and_register(
t, script, scope);
assert_true(registration instanceof ServiceWorkerRegistration, 'registered');
assert_equals(registration.scope, normalizeURL(scope));
return registration.unregister();
}, 'Service-Worker-Allowed is cross-origin to script, registering on a normally allowed scope');
register_fail_test(
build_script_url('../allowed-path-with-parent'),
'/allowed-path-with-parent',
'Registering outside Service-Worker-Allowed path with parent reference');
promise_test(async t => {
const script = build_script_url(
host_info.HTTPS_REMOTE_ORIGIN + '/');
const scope = '/this-scope-is-normally-disallowed'
const registration = await service_worker_unregister_and_register(
t, script, scope);
assert_true(registration instanceof ServiceWorkerRegistration, 'registered');
assert_equals(registration.scope, normalizeURL(scope));
return registration.unregister();
}, 'Service-Worker-Allowed is cross-origin to script, registering on a normally disallowed scope');
register_fail_test(
build_script_url(host_info.HTTPS_REMOTE_ORIGIN + '/'),
'resources/this-scope-is-normally-allowed',
'Service-Worker-Allowed is cross-origin to script, registering on a normally allowed scope');
promise_test(async t => {
const script = build_script_url(
host_info.HTTPS_REMOTE_ORIGIN + '/cross-origin/',
host_info.HTTPS_REMOTE_ORIGIN);
const scope = '/cross-origin/';
await service_worker_unregister(t, scope);
return promise_rejects(t,
'SecurityError',
navigator.serviceWorker.register(script, {scope: scope}),
'register should fail');
}, 'Service-Worker-Allowed is cross-origin to page, same-origin to script');
register_fail_test(
build_script_url(host_info.HTTPS_REMOTE_ORIGIN + '/'),
'/this-scope-is-normally-disallowed',
'Service-Worker-Allowed is cross-origin to script, registering on a normally disallowed scope');
register_fail_test(
build_script_url(host_info.HTTPS_REMOTE_ORIGIN + '/cross-origin/',
host_info.HTTPS_REMOTE_ORIGIN),
'/cross-origin/',
'Service-Worker-Allowed is cross-origin to page, same-origin to script');
</script>
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