Files
Netcatty/domain/hostConnectionTest.test.ts
T
Yohannandyangheng 75c155b748 feat(vault): add host connection test to the host editor (#2984)
* feat(vault): add host connection test to the host editor

Add a 'Test' button next to Save in the host editor that opens a dialog reusing the terminal's connection progress/auth/host-key UI, and runs a headless connection probe:

- Main process: netcatty:test-connection reuses startSSHSession in a testMode that authenticates then tears down without opening a shell, reports via netcatty:test:result, and supports cancellation (netcatty:test-connection:cancel).
- Renderer: useHostConnectionTest hook + HostConnectionTestDialog component; single-credential auth (no agent/default-key fallback) with short test timeouts (8s TCP / 15s auth).
- Reuses terminal chain progress, host-key verification, and auth prompts; success keeps the same dialog with retry/close.

* fix(ssh): cancel test connections during chain/proxy dial

Register the headless test-connection transport before connectThroughChain / createProxySocket runs, and update its pendingConn / chainConnections as hops and proxy sockets are established. This lets cancelTestConnection abort a slow/unreachable bastion or proxy dial instead of leaving it running until timeout.

* fix(ssh): address host connection test review feedback

- Route jump-host keyboard-interactive prompts through the external scope in test mode
- Always offer keyboard-interactive for requiresMfa hosts in test mode, including key/certificate-only auth
- Derive the renderer test deadline from the probe options and cancel the attempt on timeout
- Run the macOS Local Network preflight before connection tests
- Add regression tests for jump-host MFA, MFA auth order, test deadline, and macOS preflight

Author: yangheng
Model: claude-opus-5

---------

Co-authored-by: yangheng <492238647@qq.com>
2026-10-08 14:42:18 +08:00

221 lines
6.7 KiB
TypeScript

import test from "node:test";
import assert from "node:assert/strict";
import {
buildHostConnectionTestPlan,
formatConnectionTestProgressLog,
resolveHostConnectionTestDeadlineMs,
} from "./hostConnectionTest";
import type { Host, SSHKey } from "./models";
const host = (overrides: Partial<Host> = {}): Host => ({
id: "host-1",
label: "Target",
hostname: "target.example.test",
port: 22,
username: "alice",
tags: [],
os: "linux",
protocol: "ssh",
...overrides,
});
const key = (overrides: Partial<SSHKey> = {}): SSHKey => ({
id: "key-1",
label: "id_ed25519",
type: "ED25519",
privateKey: "PRIVATE KEY MATERIAL",
source: "generated",
category: "key",
created: 1,
...overrides,
});
const base = {
hosts: [],
keys: [],
identities: [],
knownHosts: [],
groupConfigs: [],
proxyProfiles: [],
sessionId: "test-session",
bootEpoch: 7,
};
test("builds password-auth options without proxy or chain", () => {
const plan = buildHostConnectionTestPlan({
...base,
host: host({ password: "secret", authMethod: "password" }),
});
assert.equal(plan.ok, true);
if (!plan.ok) return;
assert.equal(plan.needsCredentialReentry, false);
assert.equal(plan.options.hostname, "target.example.test");
assert.equal(plan.options.port, 22);
assert.equal(plan.options.username, "alice");
assert.equal(plan.options.password, "secret");
assert.equal(plan.options.authMethod, "password");
assert.equal(plan.options.proxy, undefined);
assert.equal(plan.options.jumpHosts, undefined);
assert.equal(plan.options.sessionId, "test-session");
assert.equal(plan.options.bootEpoch, 7);
assert.equal(plan.options.verifyHostKeys, true);
});
test("builds key-auth options with the resolved private key", () => {
const plan = buildHostConnectionTestPlan({
...base,
host: host({ identityFileId: "key-1" }),
keys: [key()],
});
assert.equal(plan.ok, true);
if (!plan.ok) return;
assert.equal(plan.options.privateKey, "PRIVATE KEY MATERIAL");
assert.equal(plan.options.keyId, "key-1");
assert.equal(plan.options.password, undefined);
});
test("resolves a jump-host chain by id", () => {
const plan = buildHostConnectionTestPlan({
...base,
host: host({ hostChain: { hostIds: ["jump-1"] } }),
hosts: [
host({
id: "jump-1",
label: "Bastion",
hostname: "jump.example.test",
username: "root",
password: "jump-pass",
authMethod: "password",
}),
],
});
assert.equal(plan.ok, true);
if (!plan.ok) return;
assert.equal(plan.options.jumpHosts?.length, 1);
assert.equal(plan.options.jumpHosts?.[0].hostname, "jump.example.test");
assert.equal(plan.options.jumpHosts?.[0].username, "root");
assert.equal(plan.options.jumpHosts?.[0].password, "jump-pass");
});
test("fails when a configured chain host is missing", () => {
const plan = buildHostConnectionTestPlan({
...base,
host: host({ hostChain: { hostIds: ["missing-1"] } }),
});
assert.equal(plan.ok, false);
if (plan.ok) return;
assert.match(plan.error, /jump host is missing/);
});
test("flags an undecryptable primary password for re-entry and strips it", () => {
const encrypted = `enc:v1:${Buffer.from(`v10${"\0".repeat(16)}`).toString("base64")}`;
const plan = buildHostConnectionTestPlan({
...base,
host: host({ password: encrypted, authMethod: "password" }),
});
assert.equal(plan.ok, true);
if (!plan.ok) return;
assert.equal(plan.needsCredentialReentry, true);
assert.equal(plan.options.password, undefined);
});
test("sanitizes an encrypted proxy credential into a failure plan", () => {
const encrypted = `enc:v1:${Buffer.from(`v10${"\0".repeat(16)}`).toString("base64")}`;
const plan = buildHostConnectionTestPlan({
...base,
host: host({
proxyConfig: {
type: "socks5",
host: "proxy.example.test",
port: 1080,
username: "proxy-user",
password: encrypted,
},
}),
});
assert.equal(plan.ok, false);
if (plan.ok) return;
assert.match(plan.error, /Proxy credentials cannot be decrypted/);
});
test("pins auto + password to a single password attempt", () => {
const plan = buildHostConnectionTestPlan({
...base,
host: host({ password: "secret", authPolicyVersion: 1 }),
});
assert.equal(plan.ok, true);
if (!plan.ok) return;
assert.equal(plan.options.authMethod, "password");
assert.equal(plan.options.password, "secret");
});
test("uses short test-only connection timeouts", () => {
const plan = buildHostConnectionTestPlan({
...base,
host: host({ password: "secret", authMethod: "password" }),
});
assert.equal(plan.ok, true);
if (!plan.ok) return;
assert.equal(plan.options.sshTcpConnectTimeoutMs, 8000);
assert.equal(plan.options.sshAuthReadyTimeoutMs, 15000);
});
test("formats single-hop progress log lines", () => {
assert.equal(
formatConnectionTestProgressLog({ hop: 1, total: 1, label: "target.example.test", phase: "connecting" }),
"Connecting to target.example.test...",
);
assert.equal(
formatConnectionTestProgressLog({ hop: 1, total: 1, label: "target.example.test", phase: "authenticated" }),
"target.example.test - Authenticated",
);
});
test("formats jump-host progress log lines with a hop prefix", () => {
assert.equal(
formatConnectionTestProgressLog({ hop: 1, total: 2, label: "jump.example.test", phase: "tcp-connected" }),
"[1/2] jump.example.test - TCP connected",
);
assert.equal(
formatConnectionTestProgressLog({ hop: 2, total: 2, label: "target.example.test", phase: "authenticating" }),
"[2/2] target.example.test - Key exchange complete",
);
assert.equal(
formatConnectionTestProgressLog({ hop: 2, total: 2, label: "target.example.test", phase: "error", error: "boom" }),
"[2/2] target.example.test - Error: boom",
);
});
test("test deadline follows the probe timeouts, not the host's terminal timeouts", () => {
const plan = buildHostConnectionTestPlan({
...base,
host: host({ password: "secret", sshTcpConnectTimeoutSeconds: 3, sshAuthReadyTimeoutSeconds: 5 }),
});
assert.equal(plan.ok, true);
if (!plan.ok) return;
// 8s TCP + 15s auth sent to the bridge, plus grace — never the host's 5s.
assert.equal(resolveHostConnectionTestDeadlineMs(plan.options), 8000 + 15000 + 2000);
});
test("test deadline adds every jump hop's dial and auth timeouts", () => {
const deadline = resolveHostConnectionTestDeadlineMs({
sshTcpConnectTimeoutMs: 8000,
sshAuthReadyTimeoutMs: 15000,
jumpHosts: [
{ sshTcpConnectTimeoutMs: 20000, sshAuthReadyTimeoutMs: 120000 },
{ sshTcpConnectTimeoutMs: 1000, sshAuthReadyTimeoutMs: 2000 },
],
});
assert.equal(deadline, 8000 + 15000 + 20000 + 120000 + 1000 + 2000 + 2000);
});