From 52eedb65d0e75c5500167ee15a62bcf69b6df30d Mon Sep 17 00:00:00 2001 From: bincxz <16399091+binaricat@users.noreply.github.com> Date: Wed, 15 Jul 2026 22:16:27 +0800 Subject: [PATCH] test(mosh): cover Windows ConPTY handoff --- .github/workflows/test.yml | 35 +++++ .../fixtures/mosh-conpty-fake-client.cs | 26 ++++ .../bridges/fixtures/mosh-conpty-fake-ssh.cs | 23 ++++ ...inalBridge.moshConpty.integration.test.cjs | 121 ++++++++++++++++++ 4 files changed, 205 insertions(+) create mode 100644 electron/bridges/fixtures/mosh-conpty-fake-client.cs create mode 100644 electron/bridges/fixtures/mosh-conpty-fake-ssh.cs create mode 100644 electron/bridges/terminalBridge.moshConpty.integration.test.cjs diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index a8d9aff47..6951ed35b 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -48,3 +48,38 @@ jobs: - name: Test run: npm test + + mosh-windows-conpty: + name: mosh-windows-conpty + runs-on: windows-latest + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Setup Node + uses: actions/setup-node@v4 + with: + node-version: 22 + cache: npm + + - name: Install deps + run: npm ci + + - name: Compile ConPTY test helpers + shell: pwsh + run: | + $compiler = "$env:WINDIR\Microsoft.NET\Framework64\v4.0.30319\csc.exe" + if (-not (Test-Path $compiler)) { throw "csc.exe not found" } + New-Item -ItemType Directory -Force -Path "$env:RUNNER_TEMP\netcatty-mosh-conpty" | Out-Null + & $compiler /nologo /target:exe /out:"$env:RUNNER_TEMP\netcatty-mosh-conpty\ssh.exe" electron\bridges\fixtures\mosh-conpty-fake-ssh.cs + if ($LASTEXITCODE -ne 0) { throw "failed to compile fake ssh" } + & $compiler /nologo /target:exe /out:"$env:RUNNER_TEMP\netcatty-mosh-conpty\mosh-client.exe" electron\bridges\fixtures\mosh-conpty-fake-client.cs + if ($LASTEXITCODE -ne 0) { throw "failed to compile fake mosh-client" } + + - name: Test Mosh handshake through ConPTY + shell: pwsh + env: + NETCATTY_TEST_MOSH_SSH_EXE: ${{ runner.temp }}\netcatty-mosh-conpty\ssh.exe + NETCATTY_TEST_MOSH_CLIENT_EXE: ${{ runner.temp }}\netcatty-mosh-conpty\mosh-client.exe + run: | + node --test electron/bridges/moshHandshake.test.cjs electron/bridges/terminalBridge.moshConpty.integration.test.cjs diff --git a/electron/bridges/fixtures/mosh-conpty-fake-client.cs b/electron/bridges/fixtures/mosh-conpty-fake-client.cs new file mode 100644 index 000000000..abdaac2d8 --- /dev/null +++ b/electron/bridges/fixtures/mosh-conpty-fake-client.cs @@ -0,0 +1,26 @@ +using System; + +internal static class Program +{ + public static int Main(string[] args) + { + string key = Environment.GetEnvironmentVariable("MOSH_KEY") ?? ""; + string fallback = Environment.GetEnvironmentVariable("MOSH_FALLBACK_HOST") ?? ""; + Console.WriteLine( + "MOSHCATTY_TEST_READY key=" + key + + " args=" + String.Join("|", args) + + " fallback=" + fallback + ); + + string line; + while ((line = Console.ReadLine()) != null) + { + if (String.Equals(line, "quit", StringComparison.Ordinal)) + { + return 0; + } + Console.WriteLine("MOSHCATTY_TEST_ECHO=" + line); + } + return 0; + } +} diff --git a/electron/bridges/fixtures/mosh-conpty-fake-ssh.cs b/electron/bridges/fixtures/mosh-conpty-fake-ssh.cs new file mode 100644 index 000000000..9405a5ac0 --- /dev/null +++ b/electron/bridges/fixtures/mosh-conpty-fake-ssh.cs @@ -0,0 +1,23 @@ +using System; + +internal static class Program +{ + private const string Password = "netcatty-test-password"; + + public static int Main() + { + Console.Write("alice@example.com's password: \x1b[?25h"); + string supplied = Console.ReadLine(); + if (!String.Equals(supplied, Password, StringComparison.Ordinal)) + { + Console.Error.WriteLine("unexpected password"); + return 41; + } + + Console.WriteLine("MOSH IP 127.0.0.1"); + // Deliberately omit the final newline. Windows ConPTY can deliver the + // marker this way when ssh exits, and Netcatty must still switch. + Console.Write("MOSH CONNECT 60002 ABCDEFGHIJKLMNOPQRSTUV=="); + return 0; + } +} diff --git a/electron/bridges/terminalBridge.moshConpty.integration.test.cjs b/electron/bridges/terminalBridge.moshConpty.integration.test.cjs new file mode 100644 index 000000000..0b144756e --- /dev/null +++ b/electron/bridges/terminalBridge.moshConpty.integration.test.cjs @@ -0,0 +1,121 @@ +"use strict"; + +const test = require("node:test"); +const assert = require("node:assert/strict"); +const fs = require("node:fs"); +const os = require("node:os"); +const path = require("node:path"); + +function waitFor(predicate, description, timeoutMs = 15_000) { + const startedAt = Date.now(); + return new Promise((resolve, reject) => { + const poll = () => { + if (predicate()) { + resolve(); + return; + } + if (Date.now() - startedAt >= timeoutMs) { + reject(new Error(`Timed out waiting for ${description}`)); + return; + } + setTimeout(poll, 25); + }; + poll(); + }); +} + +test("Windows ConPTY completes password handshake and switches to mosh-client", { + skip: process.platform !== "win32" ? "Windows ConPTY only" : false, +}, async (t) => { + const fakeSsh = process.env.NETCATTY_TEST_MOSH_SSH_EXE; + const fakeClient = process.env.NETCATTY_TEST_MOSH_CLIENT_EXE; + assert.ok(fakeSsh && fs.existsSync(fakeSsh), "compiled fake ssh executable is required"); + assert.ok(fakeClient && fs.existsSync(fakeClient), "compiled fake mosh-client executable is required"); + + const tmp = fs.mkdtempSync(path.join(os.tmpdir(), "netcatty-mosh-conpty-")); + const binDir = path.join(tmp, "bin"); + const resourcesPath = path.join(tmp, "resources"); + const clientDir = path.join(tmp, "project", "resources", "mosh", "win32-x64"); + fs.mkdirSync(binDir, { recursive: true }); + fs.mkdirSync(clientDir, { recursive: true }); + fs.copyFileSync(fakeSsh, path.join(binDir, "ssh.exe")); + fs.copyFileSync(fakeClient, path.join(clientDir, "mosh-client.exe")); + + const oldPath = process.env.PATH; + process.env.PATH = `${binDir}${path.delimiter}${oldPath || ""}`; + + const bridgePath = require.resolve("./terminalBridge.cjs"); + delete require.cache[bridgePath]; + const bridge = require("./terminalBridge.cjs"); + const sessions = new Map(); + const sent = []; + bridge.init({ + sessions, + electronModule: { + webContents: { + fromId() { + return { send: (channel, payload) => sent.push({ channel, payload }) }; + }, + }, + }, + }); + + t.after(() => { + bridge.cleanupAllSessions(); + process.env.PATH = oldPath; + delete require.cache[bridgePath]; + fs.rmSync(tmp, { recursive: true, force: true }); + }); + + const sessionId = "mosh-conpty-integration"; + await bridge.startMoshSession( + { sender: { id: 77 } }, + { + sessionId, + hostname: "example.com", + username: "alice", + authMethod: "password", + password: "netcatty-test-password", + cols: 80, + rows: 24, + env: { PATH: process.env.PATH }, + }, + { + moshClientLookup: { + platform: "win32", + arch: "x64", + projectRoot: path.join(tmp, "project"), + resourcesPath, + }, + }, + ); + + await waitFor( + () => sent.some((entry) => entry.channel === "netcatty:mosh:ready"), + "mosh ready event", + ); + await waitFor( + () => sent.some((entry) => entry.channel === "netcatty:data" + && String(entry.payload?.data).includes("MOSHCATTY_TEST_READY")), + "mosh-client output", + ); + + const output = sent + .filter((entry) => entry.channel === "netcatty:data") + .map((entry) => String(entry.payload?.data || "")) + .join(""); + assert.match(output, /key=ABCDEFGHIJKLMNOPQRSTUV==/); + assert.match(output, /args=127\.0\.0\.1\|60002/); + assert.match(output, /fallback=example\.com/); + assert.equal(sessions.get(sessionId)?.moshHandshakePhase, "mosh-client"); + + bridge.writeToSession(null, { sessionId, data: "hello-from-conpty\r" }); + await waitFor( + () => sent.some((entry) => entry.channel === "netcatty:data" + && String(entry.payload?.data).includes("MOSHCATTY_TEST_ECHO=hello-from-conpty")), + "input routed to mosh-client", + ); + + bridge.writeToSession(null, { sessionId, data: "quit\r" }); + await waitFor(() => !sessions.has(sessionId), "mosh-client exit"); +});