Repository navigation
Expand file tree
/
Copy pathssh-askpass.test.ts
More file actions
63 lines (61 loc) · 2.58 KB
/
Copy pathssh-askpass.test.ts
File metadata and controls
63 lines (61 loc) · 2.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import { expect, test } from "bun:test"
import { createServer } from "node:net"
import path from "node:path"
test("the executable askpass branch returns only the response, without CLI output", async () => {
const requests: string[] = []
const server = createServer((socket) => {
socket.once("data", (data: Buffer) => {
requests.push(data.toString())
socket.end(JSON.stringify({ value: 'passphrase"with spaces' }))
})
})
await new Promise<void>((resolve) => server.listen(0, "127.0.0.1", resolve))
const address = server.address()
if (!address || typeof address === "string") throw new Error("missing listener")
try {
const child = Bun.spawn([process.execPath, path.join(import.meta.dir, "index.ts"), "Enter passphrase:"], {
env: {
...process.env,
OPENCODE_SSH_ASKPASS_PORT: String(address.port),
OPENCODE_SSH_ASKPASS_TOKEN: "fixture",
SSH_ASKPASS_PROMPT: "confirm",
},
stdout: "pipe",
stderr: "pipe",
})
expect(await new Response(child.stdout).text()).toBe('passphrase"with spaces\n')
expect(await child.exited).toBe(0)
expect(requests.map((request) => JSON.parse(request))).toEqual([
{ token: "fixture", text: "Enter passphrase:", confirm: true },
])
expect(await new Response(child.stderr).text()).toBe("")
} finally {
await new Promise<void>((resolve) => server.close(() => resolve()))
}
}, 30_000)
test("a response split inside a multi-byte character is written intact", async () => {
const response = Buffer.from(JSON.stringify({ value: "pässwörd" }))
// Split between the two bytes of "ä" so the helper reads them separately.
const split = response.indexOf(0xc3) + 1
const server = createServer((socket) => {
socket.setNoDelay(true)
socket.once("data", () => {
socket.write(response.subarray(0, split))
setTimeout(() => socket.end(response.subarray(split)), 50)
})
})
await new Promise<void>((resolve) => server.listen(0, "127.0.0.1", resolve))
const address = server.address()
if (!address || typeof address === "string") throw new Error("missing listener")
try {
const child = Bun.spawn([process.execPath, path.join(import.meta.dir, "index.ts"), "Enter passphrase:"], {
env: { ...process.env, OPENCODE_SSH_ASKPASS_PORT: String(address.port), OPENCODE_SSH_ASKPASS_TOKEN: "fixture" },
stdout: "pipe",
stderr: "pipe",
})
expect(await new Response(child.stdout).text()).toBe("pässwörd\n")
expect(await child.exited).toBe(0)
} finally {
await new Promise<void>((resolve) => server.close(() => resolve()))
}
}, 30_000)