REFACTOR: Consolidate checks under getFailureResult() (#2476)

There was duplicated code, and more importantly, were were handling
certain things subtly differently in exec() and scp() as a result. This
notably causes a behavior change in exec() and scp() where failure to
authenticate now returns failure instead of throwing, which I believe is
the proper response.

This also makes it easier to see in the code exactly which functions
require what (auth, session, etc.)
This commit is contained in:
David Walker
2026-02-08 08:39:42 -08:00
committed by GitHub
parent 425bff8217
commit b5da311133
3 changed files with 62 additions and 81 deletions

View File

@@ -273,21 +273,17 @@ describe("home", () => {
// Cannot scp before authenticating
expect(dnetServer.hasAdminRights).toStrictEqual(false);
expect(() => {
ns.scp(scriptPath, dnetServerHostname, SpecialServers.Home);
}).toThrow(`Server ${dnetServerHostname} is password-protected`);
expect(ns.scp(scriptPath, dnetServerHostname, SpecialServers.Home)).toStrictEqual(false);
expect(dnetServer.scripts.size).toStrictEqual(0);
// Cannot exec before authenticating
expect(() => {
ns.exec(scriptPath, dnetServerHostname);
}).toThrow(`Server ${dnetServerHostname} is password-protected`);
// Cannot exec from home because there is no direct connection
expect(ns.exec(scriptPath, dnetServerHostname)).toStrictEqual(0);
const { ws, ns: nsDarkWeb } = getWorkerScriptAndNS(SpecialServers.DarkWeb);
const { ws: wsDarkWeb, ns: nsDarkWeb } = getWorkerScriptAndNS(SpecialServers.DarkWeb);
// Authenticate from darkweb
expect((await nsDarkWeb.dnet.authenticate(dnetServerHostname, dnetServer.password)).success).toStrictEqual(true);
expect(dnetServer.hasAdminRights).toStrictEqual(true);
// Check session created after successfully calling authenticate API
expect(getServerState(dnetServerHostname).authenticatedPIDs.includes(ws.pid)).toStrictEqual(true);
expect(getServerState(dnetServerHostname).authenticatedPIDs.includes(wsDarkWeb.pid)).toStrictEqual(true);
// Write the test script to darkweb
nsDarkWeb.write(scriptPath, scriptContent);
// scp from darkweb
@@ -300,14 +296,10 @@ describe("home", () => {
dnetServer.scripts.clear();
// Cannot scp from home without a session
expect(() => {
ns.scp(scriptPath, dnetServerHostname, SpecialServers.Home);
}).toThrow(`Server ${dnetServerHostname} requires a session`);
expect(ns.scp(scriptPath, dnetServerHostname, SpecialServers.Home)).toStrictEqual(false);
expect(dnetServer.scripts.size).toStrictEqual(0);
// Cannot exec from home without a session
expect(() => {
ns.exec(scriptPath, dnetServerHostname);
}).toThrow(`Server ${dnetServerHostname} requires a session`);
// Cannot exec from home because there is no direct connection
expect(ns.exec(scriptPath, dnetServerHostname)).toStrictEqual(0);
// Create a session from home to dnet server
expect(ns.dnet.connectToSession(dnetServerHostname, dnetServer.password).success).toStrictEqual(true);