diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 17:32:43 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 17:32:43 +0000 |
commit | 6bf0a5cb5034a7e684dcc3500e841785237ce2dd (patch) | |
tree | a68f146d7fa01f0134297619fbe7e33db084e0aa /js/xpconnect/tests/unit/test_nuke_sandbox_event_listeners.js | |
parent | Initial commit. (diff) | |
download | thunderbird-upstream.tar.xz thunderbird-upstream.zip |
Adding upstream version 1:115.7.0.upstream/1%115.7.0upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
-rw-r--r-- | js/xpconnect/tests/unit/test_nuke_sandbox_event_listeners.js | 89 |
1 files changed, 89 insertions, 0 deletions
diff --git a/js/xpconnect/tests/unit/test_nuke_sandbox_event_listeners.js b/js/xpconnect/tests/unit/test_nuke_sandbox_event_listeners.js new file mode 100644 index 0000000000..2e304a8b72 --- /dev/null +++ b/js/xpconnect/tests/unit/test_nuke_sandbox_event_listeners.js @@ -0,0 +1,89 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +// See https://bugzilla.mozilla.org/show_bug.cgi?id=1273251 + +function promiseEvent(target, event) { + return new Promise(resolve => { + target.addEventListener(event, resolve, {capture: true, once: true}); + }); +} + +add_task(async function() { + let principal = Services.scriptSecurityManager + .createContentPrincipalFromOrigin("http://example.com/"); + + let webnav = Services.appShell.createWindowlessBrowser(false); + + let docShell = webnav.docShell; + + docShell.createAboutBlankContentViewer(principal, principal); + + let window = webnav.document.defaultView; + let sandbox = Cu.Sandbox(window, {sandboxPrototype: window}); + + function sandboxContent() { + window.onload = function SandboxOnLoad() {}; + + window.addEventListener("FromTest", () => { + window.dispatchEvent(new CustomEvent("FromSandbox")); + }, true); + } + + Cu.evalInSandbox(`(${sandboxContent})()`, sandbox); + + + let fromTestPromise = promiseEvent(window, "FromTest"); + let fromSandboxPromise = promiseEvent(window, "FromSandbox"); + + equal(typeof window.onload, "function", + "window.onload should contain sandbox event listener"); + equal(window.onload.name, "SandboxOnLoad", + "window.onload have the correct function name"); + + info("Dispatch FromTest event"); + window.dispatchEvent(new window.CustomEvent("FromTest")); + + await fromTestPromise; + info("Got event from test"); + + await fromSandboxPromise; + info("Got response from sandbox"); + + + window.addEventListener("FromSandbox", () => { + ok(false, "Got unexpected reply from sandbox"); + }, true); + + info("Nuke sandbox"); + Cu.nukeSandbox(sandbox); + + + info("Dispatch FromTest event"); + fromTestPromise = promiseEvent(window, "FromTest"); + window.dispatchEvent(new window.CustomEvent("FromTest")); + await fromTestPromise; + info("Got event from test"); + + + // Force cycle collection, which should cause our callback reference + // to be dropped, and dredge up potential issues there. + Cu.forceGC(); + Cu.forceCC(); + + ok(Cu.isDeadWrapper(window.onload), + "window.onload should contain a dead wrapper after sandbox is nuked"); + + info("Dispatch FromTest event"); + fromTestPromise = promiseEvent(window, "FromTest"); + window.dispatchEvent(new window.CustomEvent("FromTest")); + await fromTestPromise; + info("Got event from test"); + + let listeners = Services.els.getListenerInfoFor(window); + ok(!listeners.some(info => info.type == "FromTest"), + "No 'FromTest' listeners returned for nuked sandbox"); + + webnav.close(); +}); |