diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 09:22:09 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 09:22:09 +0000 |
commit | 43a97878ce14b72f0981164f87f2e35e14151312 (patch) | |
tree | 620249daf56c0258faa40cbdcf9cfba06de2a846 /testing/web-platform/tests/IndexedDB/idbfactory-open-opaque-origin.html | |
parent | Initial commit. (diff) | |
download | firefox-43a97878ce14b72f0981164f87f2e35e14151312.tar.xz firefox-43a97878ce14b72f0981164f87f2e35e14151312.zip |
Adding upstream version 110.0.1.upstream/110.0.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'testing/web-platform/tests/IndexedDB/idbfactory-open-opaque-origin.html')
-rw-r--r-- | testing/web-platform/tests/IndexedDB/idbfactory-open-opaque-origin.html | 105 |
1 files changed, 105 insertions, 0 deletions
diff --git a/testing/web-platform/tests/IndexedDB/idbfactory-open-opaque-origin.html b/testing/web-platform/tests/IndexedDB/idbfactory-open-opaque-origin.html new file mode 100644 index 0000000000..a71da9afb3 --- /dev/null +++ b/testing/web-platform/tests/IndexedDB/idbfactory-open-opaque-origin.html @@ -0,0 +1,105 @@ +<!DOCTYPE html> +<meta charset=utf-8> +<title>IDBFactory.open() and opaque origins</title> +<script src=/resources/testharness.js></script> +<script src=/resources/testharnessreport.js></script> +<script> + +function load_iframe(src, sandbox) { + return new Promise(resolve => { + const iframe = document.createElement('iframe'); + iframe.onload = () => { resolve(iframe); }; + if (sandbox) + iframe.sandbox = sandbox; + iframe.srcdoc = src; + iframe.style.display = 'none'; + document.documentElement.appendChild(iframe); + }); +} + +function wait_for_message(recipient, source) { + return new Promise(resolve => { + recipient.onmessage = function listener(e) { + if (e.source === source) { + resolve(e.data); + recipient.removeEventListener('message', listener); + } + }; + }) +} + +const test_code = + ' const handler = (reply) => {' + + ' try {' + + ' indexedDB.deleteDatabase("opaque-origin-test");' + + ' } catch {}' + + ' try {' + + ' const r = indexedDB.open("opaque-origin-test");' + + ' r.onupgradeneeded = () => { r.transaction.abort(); };' + + ' reply({result: "no exception"});' + + ' } catch (ex) {' + + ' reply({result: ex.name});' + + ' };' + + ' };'; + +const iframe_script = + '<script>' + + test_code + + ' window.onmessage = () => {' + + ' handler(msg => window.parent.postMessage(msg, "*"));' + + ' };' + + '<\/script>'; + +promise_test(t => { + return load_iframe(iframe_script) + .then(iframe => { + iframe.contentWindow.postMessage({}, '*'); + return wait_for_message(self, iframe.contentWindow); + }) + .then(message => { + assert_equals(message.result, 'no exception', + 'IDBFactory.open() should not throw'); + }); +}, 'IDBFactory.open() in non-sandboxed iframe should not throw'); + +promise_test(t => { + return load_iframe(iframe_script, 'allow-scripts') + .then(iframe => { + iframe.contentWindow.postMessage({}, '*'); + return wait_for_message(self, iframe.contentWindow); + }) + .then(message => { + assert_equals(message.result, 'SecurityError', + 'Exception should be SecurityError'); + }); +}, 'IDBFactory.open() in sandboxed iframe should throw SecurityError'); + +const worker_script = ` +${test_code} +// For dedicated workers: +self.addEventListener("message", () => handler(self.postMessage)); +// For shared workers: +self.addEventListener("connect", (e) => { + var port = e.ports[0]; + handler(msg => port.postMessage(msg)); +}); +`; +const worker_data_url = "data:,".concat(encodeURIComponent(worker_script)); + +promise_test(async t => { + let worker = new Worker(worker_data_url); + t.add_cleanup(() => worker.terminate()); + worker.postMessage({}); + const message = await wait_for_message(worker, null); + assert_equals(message.result, 'SecurityError', + 'Promise should be rejected with SecurityError'); +}, 'IDBFactory.open() in data URL dedicated workers should throw SecurityError'); + +promise_test(async t => { + let worker = new SharedWorker(worker_data_url, 'idb_open_opaque'); + worker.port.postMessage({}); + const message = await wait_for_message(worker.port, null); + assert_equals(message.result, 'SecurityError', + 'Promise should be rejected with SecurityError'); +}, 'IDBFactory.open() in data URL shared workers should throw SecurityError'); +</script> |