Files
NetMesh/domain/sftpDropElevation.test.ts
zhaolei 3c72efcb7f
Some checks failed
build-packages / resolve bundled mosh-client (push) Has been cancelled
build-packages / resolve bundled et-client (push) Has been cancelled
build-packages / build-macos (push) Has been cancelled
build-packages / build-windows (push) Has been cancelled
build-packages / build-linux-x64 (push) Has been cancelled
build-packages / build-linux-arm64 (push) Has been cancelled
build-packages / release (push) Has been cancelled
build-packages / update Nix release metadata (push) Has been cancelled
build-packages / bump homebrew tap (push) Has been cancelled
test / lint-and-test (push) Has been cancelled
AI automation / Route event (push) Has been cancelled
AI automation / Hand reopened issue to maintainers (push) Has been cancelled
AI automation / Clean source issue state (push) Has been cancelled
AI automation / Reconcile handoffs (push) Has been cancelled
AI automation / Classify issue (push) Has been cancelled
AI automation / Claude Code smoke (push) Has been cancelled
AI automation / Review issue follow-up (push) Has been cancelled
AI automation / Publish issue follow-up (push) Has been cancelled
AI automation / Implement with Claude Code (push) Has been cancelled
AI automation / Publish implement PR (push) Has been cancelled
AI automation / Continue queued issue comments (push) Has been cancelled
AI automation / Codex review loop (push) Has been cancelled
AI automation / Publish Codex fix (push) Has been cancelled
AI automation / Clear Codex dispatch marker (push) Has been cancelled
AI automation / Own PR re-request Codex (push) Has been cancelled
AI automation / External PR re-request Codex (push) Has been cancelled
AI automation / Poll Codex reaction / retry (push) Has been cancelled
build-et-binaries / build-linux-x64 (push) Has been cancelled
build-et-binaries / build-linux-arm64 (push) Has been cancelled
build-et-binaries / build-macos-universal (push) Has been cancelled
build-et-binaries / build-windows-x64 (push) Has been cancelled
build-et-binaries / release (push) Has been cancelled
[Init] Initial commit - NetMesh terminal manager
2026-09-13 18:24:01 +08:00

108 lines
4.2 KiB
TypeScript

import test from "node:test";
import assert from "node:assert/strict";
import type { Host } from "./models";
import {
canElevateSftpForTerminalDrop,
hasUsableSftpSudoPassword,
normalizePosixAbsolutePath,
posixPathNeedsLoginUserElevation,
resolveTerminalDropSftpHost,
TerminalDropNeedsSudoError,
} from "./sftpDropElevation";
const host = {
id: "host-1",
label: "Host",
hostname: "example.com",
port: 22,
username: "alice",
protocol: "ssh",
} as Host;
const encryptedPassword = (() => {
const blob = Buffer.alloc(19, 0);
Buffer.from("v10", "utf8").copy(blob, 0);
return `enc:v1:${blob.toString("base64")}`;
})();
test("normalizePosixAbsolutePath collapses slashes and trailing separators", () => {
assert.equal(normalizePosixAbsolutePath("/root/"), "/root");
assert.equal(normalizePosixAbsolutePath("/root//bin/"), "/root/bin");
assert.equal(normalizePosixAbsolutePath("/"), "/");
assert.equal(normalizePosixAbsolutePath("root"), null);
assert.equal(normalizePosixAbsolutePath(" "), null);
});
test("posixPathNeedsLoginUserElevation only flags /root for non-root logins", () => {
assert.equal(posixPathNeedsLoginUserElevation("/root", "alice"), true);
assert.equal(posixPathNeedsLoginUserElevation("/root/bin", "alice"), true);
assert.equal(posixPathNeedsLoginUserElevation("/root/", "alice"), true);
assert.equal(posixPathNeedsLoginUserElevation("/home/alice", "alice"), false);
assert.equal(posixPathNeedsLoginUserElevation("/tmp", "alice"), false);
assert.equal(posixPathNeedsLoginUserElevation("/root", "root"), false);
assert.equal(posixPathNeedsLoginUserElevation("/root", " "), false);
assert.equal(posixPathNeedsLoginUserElevation("/root", undefined), false);
});
test("hasUsableSftpSudoPassword ignores missing and encrypted placeholders", () => {
assert.equal(hasUsableSftpSudoPassword("secret"), true);
assert.equal(hasUsableSftpSudoPassword(""), false);
assert.equal(hasUsableSftpSudoPassword(undefined), false);
assert.equal(hasUsableSftpSudoPassword(encryptedPassword), false);
});
test("canElevateSftpForTerminalDrop rejects SCP and missing passwords", () => {
assert.equal(canElevateSftpForTerminalDrop({ sftpSudo: true }), true);
assert.equal(canElevateSftpForTerminalDrop({}, "secret"), true);
assert.equal(canElevateSftpForTerminalDrop({ sftpFileProtocol: "scp" }, "secret"), false);
assert.equal(canElevateSftpForTerminalDrop({}), false);
});
test("resolveTerminalDropSftpHost clones sudo only for unelevated /root drops", () => {
const elevated = resolveTerminalDropSftpHost(host, "/root", { password: "secret" });
assert.equal(elevated.sftpSudo, true);
assert.notEqual(elevated, host);
const alreadySudo = { ...host, sftpSudo: true };
assert.equal(resolveTerminalDropSftpHost(alreadySudo, "/root"), alreadySudo);
assert.equal(resolveTerminalDropSftpHost(host, "/home/alice", { password: "secret" }), host);
});
test("resolveTerminalDropSftpHost uses a resolved identity password when host.password is empty", () => {
const identityHost = { ...host, identityId: "id-1", password: undefined };
const elevated = resolveTerminalDropSftpHost(identityHost, "/root", { password: "identity-secret" });
assert.equal(elevated.sftpSudo, true);
assert.equal(elevated.password, undefined);
assert.equal(elevated.identityId, "id-1");
});
test("resolveTerminalDropSftpHost uses the resolved identity username over a stale host username", () => {
const staleRootHost = { ...host, username: "root" };
const elevated = resolveTerminalDropSftpHost(staleRootHost, "/root", {
password: "secret",
username: "alice",
});
assert.equal(elevated.sftpSudo, true);
const staleAliceHost = { ...host, username: "alice" };
assert.equal(
resolveTerminalDropSftpHost(staleAliceHost, "/root", {
password: "secret",
username: "root",
}),
staleAliceHost,
);
});
test("resolveTerminalDropSftpHost asks the user to enable sudo when no password is saved", () => {
assert.throws(
() => resolveTerminalDropSftpHost(host, "/root"),
TerminalDropNeedsSudoError,
);
assert.throws(
() => resolveTerminalDropSftpHost({ ...host, sftpFileProtocol: "scp" }, "/root", { password: "secret" }),
TerminalDropNeedsSudoError,
);
});