diff options
Diffstat (limited to '')
-rw-r--r-- | dom/serviceworkers/test/test_error_reporting.html | 241 |
1 files changed, 241 insertions, 0 deletions
diff --git a/dom/serviceworkers/test/test_error_reporting.html b/dom/serviceworkers/test/test_error_reporting.html new file mode 100644 index 0000000000..7c2d56fb9e --- /dev/null +++ b/dom/serviceworkers/test/test_error_reporting.html @@ -0,0 +1,241 @@ +<!DOCTYPE HTML> +<html> +<head> + <title>Test Error Reporting of Service Worker Failures</title> + <script src="/tests/SimpleTest/SimpleTest.js"></script> + <script src="error_reporting_helpers.js"></script> + <script src="utils.js"></script> + <link rel="stylesheet" href="/tests/SimpleTest/test.css"/> + <meta http-equiv="Content-type" content="text/html;charset=UTF-8"> +</head> +<body> + +<script type="text/javascript"> +"use strict"; + +/** + * Test that a bunch of service worker coding errors and failure modes that + * might otherwise be hard to diagnose are surfaced as console error messages. + * The driving use-case is minimizing cursing from a developer looking at a + * document in Firefox testing a page that involves service workers. + * + * This test assumes that errors will be reported via + * ServiceWorkerManager::ReportToAllClients and that that method is reliable and + * tested via some other file. + **/ + +add_task(function setupPrefs() { + return SpecialPowers.pushPrefEnv({"set": [ + ["dom.serviceWorkers.enabled", true], + ["dom.serviceWorkers.testing.enabled", true], + ["dom.caches.testing.enabled", true], + ]}); +}); + +/** + * Ensure an error is logged during the initial registration of a SW when a 404 + * is received. + */ +add_task(async function register_404() { + // Start monitoring for the error + let expectedMessage = expect_console_message( + "ServiceWorkerRegisterNetworkError", + [make_absolute_url("network_error/"), "404", make_absolute_url("404.js")]); + + // Register, generating the 404 error. This will reject with a TypeError + // which we need to consume so it doesn't get thrown at our generator. + await navigator.serviceWorker.register("404.js", { scope: "network_error/" }) + .then( + () => { ok(false, "should have rejected"); }, + (e) => { ok(e.name === "TypeError", "404 failed as expected"); }); + + await wait_for_expected_message(expectedMessage); +}); + +/** + * Ensure an error is logged when the service worker is being served with a + * MIME type of text/plain rather than a JS type. + */ +add_task(async function register_bad_mime_type() { + let expectedMessage = expect_console_message( + "ServiceWorkerRegisterMimeTypeError2", + [make_absolute_url("bad_mime_type/"), "text/plain", + make_absolute_url("sw_bad_mime_type.js")]); + + // consume the expected rejection so it doesn't get thrown at us. + await navigator.serviceWorker.register("sw_bad_mime_type.js", { scope: "bad_mime_type/" }) + .then( + () => { ok(false, "should have rejected"); }, + (e) => { ok(e.name === "SecurityError", "bad MIME type failed as expected"); }); + + await wait_for_expected_message(expectedMessage); +}); + +async function notAllowStorageAccess() { + throw new Error("Storage permissions should be used when bug 1774860 overhauls this test."); +} + +async function allowStorageAccess() { + throw new Error("Storage permissions should be used when bug 1774860 overhauls this test."); +} + +/** + * Ensure an error is logged when the storage is not allowed and the content + * script is trying to register a service worker. + */ +add_task(async function register_storage_error() { + let expectedMessage = expect_console_message( + "ServiceWorkerRegisterStorageError", + [make_absolute_url("storage_not_allow/")]); + + await notAllowStorageAccess(); + + // consume the expected rejection so it doesn't get thrown at us. + await navigator.serviceWorker.register("sw_storage_not_allow.js", + { scope: "storage_not_allow/" }) + .then( + () => { ok(false, "should have rejected"); }, + (e) => { ok(e.name === "SecurityError", + "storage access failed as expected."); }); + + await wait_for_expected_message(expectedMessage); + + await allowStorageAccess(); +}); + +/** + * Ensure an error is logged when the storage is not allowed and the content + * script is trying to get the service worker registration. + */ +add_task(async function get_registration_storage_error() { + let expectedMessage = + expect_console_message("ServiceWorkerGetRegistrationStorageError", []); + + await notAllowStorageAccess(); + + // consume the expected rejection so it doesn't get thrown at us. + await navigator.serviceWorker.getRegistration() + .then( + () => { ok(false, "should have rejected"); }, + (e) => { ok(e.name === "SecurityError", + "storage access failed as expected."); }); + + await wait_for_expected_message(expectedMessage); + + await allowStorageAccess(); +}); + +/** + * Ensure an error is logged when the storage is not allowed and the content + * script is trying to get the service worker registrations. + */ +add_task(async function get_registrations_storage_error() { + let expectedMessage = + expect_console_message("ServiceWorkerGetRegistrationStorageError", []); + + await notAllowStorageAccess(); + + // consume the expected rejection so it doesn't get thrown at us. + await navigator.serviceWorker.getRegistrations() + .then( + () => { ok(false, "should have rejected"); }, + (e) => { ok(e.name === "SecurityError", + "storage access failed as expected."); }); + + await wait_for_expected_message(expectedMessage); + + await allowStorageAccess(); +}); + +/** + * Ensure an error is logged when the storage is not allowed and the content + * script is trying to post a message to the service worker. + */ +add_task(async function postMessage_storage_error() { + let expectedMessage = expect_console_message( + "ServiceWorkerPostMessageStorageError", + [make_absolute_url("storage_not_allow/")]); + + let registration; + // consume the expected rejection so it doesn't get thrown at us. + await navigator.serviceWorker.register("sw_storage_not_allow.js", + { scope: "storage_not_allow/" }) + .then(reg => { registration = reg; }) + .then(() => notAllowStorageAccess()) + .then(() => registration.installing || + registration.waiting || + registration.active) + .then(worker => worker.postMessage('ha')) + .then( + () => { ok(false, "should have rejected"); }, + (e) => { ok(e.name === "SecurityError", + "storage access failed as expected."); }); + + await wait_for_expected_message(expectedMessage); + + await registration.unregister(); + await allowStorageAccess(); +}); + +/** + * Ensure an error is logged when the storage is not allowed and the service + * worker is trying to get its client. + */ +add_task(async function get_client_storage_error() { + let expectedMessage = + expect_console_message("ServiceWorkerGetClientStorageError", []); + + await SpecialPowers.pushPrefEnv({"set": [ + // Make the test pass the IsOriginPotentiallyTrustworthy. + ["dom.securecontext.allowlist", "mochi.test"] + ]}); + + let registration; + // consume the expected rejection so it doesn't get thrown at us. + await navigator.serviceWorker.register("sw_storage_not_allow.js", + { scope: "test_error_reporting.html" }) + .then(reg => { + registration = reg; + return waitForState(registration.installing, "activated"); + }) + // Get the client's ID in the stage 1 + .then(() => fetch("getClient-stage1")) + .then(() => notAllowStorageAccess()) + // Trigger the clients.get() in the stage 2 + .then(() => fetch("getClient-stage2")) + .catch(e => ok(false, "fail due to:" + e)); + + await wait_for_expected_message(expectedMessage); + + await registration.unregister(); + await allowStorageAccess(); +}); + +/** + * Ensure an error is logged when the storage is not allowed and the service + * worker is trying to get its clients. + */ +add_task(async function get_clients_storage_error() { + let expectedMessage = + expect_console_message("ServiceWorkerGetClientStorageError", []); + + let registration; + // consume the expected rejection so it doesn't get thrown at us. + await navigator.serviceWorker.register("sw_storage_not_allow.js", + { scope: "test_error_reporting.html" }) + .then(reg => { + registration = reg; + return waitForState(registration.installing, "activated"); + }) + .then(() => notAllowStorageAccess()) + .then(() => fetch("getClients")) + .catch(e => ok(false, "fail due to:" + e)); + + await wait_for_expected_message(expectedMessage); + + await registration.unregister(); + await allowStorageAccess(); +}); +</script> +</body> +</html> |