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 /devtools/shared/security/tests/xpcshell/test_devtools_socket_status.js | |
parent | Initial commit. (diff) | |
download | thunderbird-6bf0a5cb5034a7e684dcc3500e841785237ce2dd.tar.xz thunderbird-6bf0a5cb5034a7e684dcc3500e841785237ce2dd.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-- | devtools/shared/security/tests/xpcshell/test_devtools_socket_status.js | 137 |
1 files changed, 137 insertions, 0 deletions
diff --git a/devtools/shared/security/tests/xpcshell/test_devtools_socket_status.js b/devtools/shared/security/tests/xpcshell/test_devtools_socket_status.js new file mode 100644 index 0000000000..2e41583b05 --- /dev/null +++ b/devtools/shared/security/tests/xpcshell/test_devtools_socket_status.js @@ -0,0 +1,137 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +"use strict"; + +const { + useDistinctSystemPrincipalLoader, + releaseDistinctSystemPrincipalLoader, +} = ChromeUtils.importESModule( + "resource://devtools/shared/loader/DistinctSystemPrincipalLoader.sys.mjs" +); + +const { DevToolsSocketStatus } = ChromeUtils.importESModule( + "resource://devtools/shared/security/DevToolsSocketStatus.sys.mjs" +); + +add_task(async function () { + Services.prefs.setBoolPref("devtools.debugger.remote-enabled", true); + Services.prefs.setBoolPref("devtools.debugger.prompt-connection", false); + + info("Without any server started, all states should be set to false"); + checkSocketStatus(false, false); + + info("Start a first server, expect all states to change to true"); + const server = await setupDevToolsServer({ fromBrowserToolbox: false }); + checkSocketStatus(true, true); + + info("Start another server, expect all states to remain true"); + const otherServer = await setupDevToolsServer({ fromBrowserToolbox: false }); + checkSocketStatus(true, true); + + info("Shutdown one of the servers, expect all states to remain true"); + teardownDevToolsServer(otherServer); + checkSocketStatus(true, true); + + info("Shutdown the other server, expect all states to change to false"); + teardownDevToolsServer(server); + checkSocketStatus(false, false); + + info( + "Start a 'browser toolbox' server, expect only the 'include' state to become true" + ); + const browserToolboxServer = await setupDevToolsServer({ + fromBrowserToolbox: true, + }); + checkSocketStatus(true, false); + + info( + "Shutdown the 'browser toolbox' server, expect all states to become false" + ); + teardownDevToolsServer(browserToolboxServer); + checkSocketStatus(false, false); + + Services.prefs.clearUserPref("devtools.debugger.remote-enabled"); + Services.prefs.clearUserPref("devtools.debugger.prompt-connection"); +}); + +function checkSocketStatus(expectedExcludeFalse, expectedExcludeTrue) { + const openedDefault = DevToolsSocketStatus.hasSocketOpened(); + const openedExcludeFalse = DevToolsSocketStatus.hasSocketOpened({ + excludeBrowserToolboxSockets: false, + }); + const openedExcludeTrue = DevToolsSocketStatus.hasSocketOpened({ + excludeBrowserToolboxSockets: true, + }); + + equal( + openedDefault, + openedExcludeFalse, + "DevToolsSocketStatus.hasSocketOpened should default to excludeBrowserToolboxSockets=false" + ); + equal( + openedExcludeFalse, + expectedExcludeFalse, + "DevToolsSocketStatus matches the expectation for excludeBrowserToolboxSockets=false" + ); + equal( + openedExcludeTrue, + expectedExcludeTrue, + "DevToolsSocketStatus matches the expectation for excludeBrowserToolboxSockets=true" + ); +} + +async function setupDevToolsServer({ fromBrowserToolbox }) { + info("Use the dedicated system principal loader for the DevToolsServer."); + const requester = {}; + const loader = useDistinctSystemPrincipalLoader(requester); + + const { DevToolsServer } = loader.require( + "resource://devtools/server/devtools-server.js" + ); + + DevToolsServer.init(); + DevToolsServer.registerAllActors(); + DevToolsServer.allowChromeProcess = true; + const socketOptions = { + fromBrowserToolbox, + // Pass -1 to automatically choose an available port + portOrPath: -1, + }; + + const listener = new SocketListener(DevToolsServer, socketOptions); + ok(listener, "Socket listener created"); + + // Note that useDistinctSystemPrincipalLoader will lead to reuse the same + // loader if we are creating several servers in a row. The DevToolsServer + // singleton might already have sockets opened. + const listeningSockets = DevToolsServer.listeningSockets; + await listener.open(); + equal( + DevToolsServer.listeningSockets, + listeningSockets + 1, + "A new listening socket was created" + ); + + return { DevToolsServer, listener, requester }; +} + +function teardownDevToolsServer({ DevToolsServer, listener, requester }) { + info("Close the listener socket"); + const listeningSockets = DevToolsServer.listeningSockets; + listener.close(); + equal( + DevToolsServer.listeningSockets, + listeningSockets - 1, + "A listening socket was closed" + ); + + if (DevToolsServer.listeningSockets == 0) { + info("Destroy the temporary devtools server"); + DevToolsServer.destroy(); + } + + if (requester) { + releaseDistinctSystemPrincipalLoader(requester); + } +} |