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) { ...@@ -17,88 +17,72 @@ function build_script_url(allowed_path, origin) {
return `${url}?pipe=header(Service-Worker-Allowed,${allowed_path})`; return `${url}?pipe=header(Service-Worker-Allowed,${allowed_path})`;
} }
promise_test(async t => { // register_test is a promise_test that registers a service worker.
const script = build_script_url('/allowed-path'); function register_test(script, scope, description) {
const scope = '/allowed-path'; promise_test(async t => {
const registration = await service_worker_unregister_and_register( t.add_cleanup(() => {
t, script, scope); return service_worker_unregister(t, scope);
assert_true(registration instanceof ServiceWorkerRegistration, 'registered'); });
assert_equals(registration.scope, normalizeURL(scope));
return registration.unregister();
}, 'Registering within Service-Worker-Allowed path');
promise_test(async t => { const registration = await service_worker_unregister_and_register(
const script = build_script_url(new URL('/allowed-path', document.location)); t, script, scope);
const scope = '/allowed-path'; assert_true(registration instanceof ServiceWorkerRegistration, 'registered');
const registration = await service_worker_unregister_and_register( assert_equals(registration.scope, normalizeURL(scope));
t, script, scope); }, description);
assert_true(registration instanceof ServiceWorkerRegistration, 'registered'); }
assert_equals(registration.scope, normalizeURL(scope));
return registration.unregister(); // register_fail_test is like register_test but expects a SecurityError.
}, 'Registering within Service-Worker-Allowed path (absolute URL)'); 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 => { register_test(
const script = build_script_url('../allowed-path-with-parent'); build_script_url(new URL('/allowed-path', document.location)),
const scope = 'allowed-path-with-parent'; '/allowed-path',
const registration = await service_worker_unregister_and_register( 'Registering within Service-Worker-Allowed path (absolute URL)');
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');
promise_test(async t => { register_test(
const script = build_script_url('../allowed-path'); build_script_url('../allowed-path-with-parent'),
const scope = '/disallowed-path'; 'allowed-path-with-parent',
await service_worker_unregister(t, scope); 'Registering within Service-Worker-Allowed path with parent reference');
return promise_rejects(t,
'SecurityError',
navigator.serviceWorker.register(script, {scope: scope}),
'register should fail');
}, 'Registering outside Service-Worker-Allowed path');
promise_test(async t => { register_fail_test(
const script = build_script_url('../allowed-path-with-parent'); build_script_url('../allowed-path'),
const scope = '/allowed-path-with-parent'; '/disallowed-path',
await service_worker_unregister(t, scope); 'Registering outside Service-Worker-Allowed path'),
return promise_rejects(t,
'SecurityError',
navigator.serviceWorker.register(script, {scope: scope}),
'register should fail');
}, 'Registering outside Service-Worker-Allowed path with parent reference');
promise_test(async t => { register_fail_test(
const script = build_script_url( build_script_url('../allowed-path-with-parent'),
host_info.HTTPS_REMOTE_ORIGIN + '/'); '/allowed-path-with-parent',
const scope = 'resources/this-scope-is-normally-allowed' 'Registering outside Service-Worker-Allowed path with parent reference');
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');
promise_test(async t => { register_fail_test(
const script = build_script_url( build_script_url(host_info.HTTPS_REMOTE_ORIGIN + '/'),
host_info.HTTPS_REMOTE_ORIGIN + '/'); 'resources/this-scope-is-normally-allowed',
const scope = '/this-scope-is-normally-disallowed' 'Service-Worker-Allowed is cross-origin to script, registering on a normally allowed scope');
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');
promise_test(async t => { register_fail_test(
const script = build_script_url( build_script_url(host_info.HTTPS_REMOTE_ORIGIN + '/'),
host_info.HTTPS_REMOTE_ORIGIN + '/cross-origin/', '/this-scope-is-normally-disallowed',
host_info.HTTPS_REMOTE_ORIGIN); 'Service-Worker-Allowed is cross-origin to script, registering on a normally disallowed scope');
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 + '/cross-origin/',
host_info.HTTPS_REMOTE_ORIGIN),
'/cross-origin/',
'Service-Worker-Allowed is cross-origin to page, same-origin to script');
</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