Files
Netcatty/electron/bridges/sftpBridge/fileOps.extract.test.cjs
T
又是一条懒汉 5805d15746 feat(ssh): add single-channel SSH option for bastion hosts (#3458)
Add a host and group toggle for bastions that allow one session channel per TCP connection. After the handshake, BHostSSH, TERM-SSHD, and CLOUDBILITY take that path automatically. File transfer uses a separate SFTP login, and the interactive terminal is not given another exec, SFTP, copy/split, or port-forward channel.

The manual toggle remains for bastions that still advertise OpenSSH.
2026-09-29 18:01:04 +08:00

83 lines
2.6 KiB
JavaScript

"use strict";
const assert = require("node:assert/strict");
const test = require("node:test");
const { createFileOpsApi } = require("./fileOps.cjs");
function createExtractApi({ execImpl, clients } = {}) {
const commands = [];
const optionsLog = [];
const api = createFileOpsApi({
sftpClients: clients || new Map([
["sftp-1", { client: { exec() {} } }],
]),
resolveEncodingForRequest: () => "utf-8",
throwIfAborted: () => {},
encodePath: (value) => value,
requireSftpChannel: async () => ({}),
lstatAsync: async () => ({ size: 1024 }),
isScpModeClient: () => false,
execRemoteShellCommand: async (_ssh, command, options) => {
commands.push(command);
optionsLog.push(options);
if (typeof execImpl === "function") return execImpl(command);
return { stdout: "", stderr: "", code: 0 };
},
});
return { api, commands, optionsLog };
}
test("extractSftpArchive runs a quoted remote tar command", async () => {
const { api, commands, optionsLog } = createExtractApi();
const result = await api.extractSftpArchive(null, {
sftpId: "sftp-1",
path: "/home/app/backup.tar.gz",
});
assert.deepEqual(result, { success: true });
assert.equal(commands.length, 1);
assert.match(commands[0], /tar -xzf '\/home\/app\/backup\.tar\.gz' -C '\/home\/app'/);
assert.equal(optionsLog[0].discardStdout, true);
});
test("extractSftpArchive rejects unsupported files", async () => {
const { api, commands } = createExtractApi();
await assert.rejects(
() => api.extractSftpArchive(null, { sftpId: "sftp-1", path: "/home/app/notes.txt" }),
/Unsupported archive type/,
);
assert.equal(commands.length, 0);
});
test("extractSftpArchive requires an open SFTP session", async () => {
const { api } = createExtractApi({ clients: new Map() });
await assert.rejects(
() => api.extractSftpArchive(null, { sftpId: "missing", path: "/tmp/a.zip" }),
/SFTP session not found/,
);
});
test("extractSftpArchive refuses a second channel on single-channel SSH", async () => {
let execCalls = 0;
const { api, commands } = createExtractApi({
clients: new Map([[
"sftp-1",
{
__netcattySingleChannelSsh: true,
__netcattyEndpointKey: "ep-extract",
client: { exec() { execCalls += 1; } },
},
]]),
});
await assert.rejects(
() => api.extractSftpArchive(null, {
sftpId: "sftp-1",
path: "/home/app/backup.tar.gz",
}),
(err) => err && err.code === "ERR_SFTP_SINGLE_CHANNEL_EXEC",
);
assert.equal(commands.length, 0);
assert.equal(execCalls, 0);
});