|
| 1 | +import test from "node:test"; |
| 2 | +import assert from "node:assert/strict"; |
| 3 | +import { chmod, mkdtemp, readFile, rm, writeFile } from "node:fs/promises"; |
| 4 | +import { spawnSync } from "node:child_process"; |
| 5 | +import os from "node:os"; |
| 6 | +import path from "node:path"; |
| 7 | +import { fileURLToPath } from "node:url"; |
| 8 | + |
| 9 | +const testDir = path.dirname(fileURLToPath(import.meta.url)); |
| 10 | +const script = path.resolve(testDir, "../scripts/paseo-verify-identity"); |
| 11 | + |
| 12 | +async function mockCommand(dir, name, body) { |
| 13 | + const file = path.join(dir, name); |
| 14 | + await writeFile(file, `#!/bin/sh\n${body}\n`); |
| 15 | + await chmod(file, 0o755); |
| 16 | +} |
| 17 | + |
| 18 | +test("rejects invalid CONTAINER_PORT values before probing", () => { |
| 19 | + for (const port of ["", "0", "08", "65536", "70000", "0x1A6F", "nope", " 6767"]) { |
| 20 | + const result = spawnSync("bash", [script, "example"], { |
| 21 | + encoding: "utf8", |
| 22 | + env: { CONTAINER_PORT: port, PATH: process.env.PATH }, |
| 23 | + }); |
| 24 | + |
| 25 | + assert.equal(result.status, 2, `CONTAINER_PORT=${JSON.stringify(port)}`); |
| 26 | + assert.match(result.stderr, /CONTAINER_PORT must be a decimal port/); |
| 27 | + assert.doesNotMatch(result.stderr, /acq not on PATH/); |
| 28 | + } |
| 29 | +}); |
| 30 | + |
| 31 | +test("normalizes server-id newlines before KEY=value framing", async () => { |
| 32 | + const source = await readFile(script, "utf8"); |
| 33 | + const home = await mkdtemp(path.join(os.tmpdir(), "paseo-identity-home-")); |
| 34 | + try { |
| 35 | + await writeFile(path.join(home, "server-id"), "srv_safe\nEXTERNAL=999\n"); |
| 36 | + const result = spawnSync("sh", ["-lc", 'printf "SERVER_ID=%s\\n" "$(cat "$1/server-id" | tr -d "\\n")"', "_", home], { |
| 37 | + encoding: "utf8", |
| 38 | + }); |
| 39 | + |
| 40 | + assert.equal(result.status, 0); |
| 41 | + assert.equal(result.stdout, "SERVER_ID=srv_safeEXTERNAL=999\n"); |
| 42 | + assert.doesNotMatch(result.stdout, /^EXTERNAL=/m); |
| 43 | + assert.match(source, /SERVER_ID=.*cat .*server-id.*tr -d "\\n"/); |
| 44 | + assert.match(source, /DAEMON_PID=.*paseo\.pid.*\| head -1/); |
| 45 | + assert.match(source, /\^srv_\[A-Za-z0-9_-\]\+\$/); |
| 46 | + } finally { |
| 47 | + await rm(home, { force: true, recursive: true }); |
| 48 | + } |
| 49 | +}); |
| 50 | + |
| 51 | +test("fails when the configured guest port is absent from /proc/net/tcp", async () => { |
| 52 | + const mocks = await mkdtemp(path.join(os.tmpdir(), "paseo-identity-mocks-")); |
| 53 | + try { |
| 54 | + await mockCommand(mocks, "acq", ` |
| 55 | +if [ "$1" = ports ]; then |
| 56 | + printf '127.0.0.1:6767\\n' |
| 57 | +else |
| 58 | + printf 'SERVER_ID=srv_safe\\nDAEMON_PID=1\\nVM_UPTIME=1\\nBIND=00000000:1A6E\\nHEALTH={"status":"ok"}\\nEXTERNAL=0\\nLOOPBACK=0\\n' |
| 59 | +fi`); |
| 60 | + await mockCommand(mocks, "curl", "printf '{\"status\":\"ok\"}'"); |
| 61 | + await mockCommand(mocks, "node", ` |
| 62 | +if [ "$1" = -e ]; then |
| 63 | + printf '127.0.0.1\\n' |
| 64 | +else |
| 65 | + printf 'localhost: srv_safe\\n127.0.0.1: srv_safe\\nipv4-mapped: srv_safe\\n' |
| 66 | +fi`); |
| 67 | + |
| 68 | + const result = spawnSync("bash", [script, "example"], { |
| 69 | + encoding: "utf8", |
| 70 | + env: { ...process.env, PATH: `${mocks}:${process.env.PATH}`, CONTAINER_PORT: "6767" }, |
| 71 | + }); |
| 72 | + |
| 73 | + assert.equal(result.status, 1); |
| 74 | + assert.match(result.stdout, /guest bind for :1A6F not found in \/proc\/net\/tcp/); |
| 75 | + assert.match(result.stdout, /RESULT: FAIL/); |
| 76 | + } finally { |
| 77 | + await rm(mocks, { force: true, recursive: true }); |
| 78 | + } |
| 79 | +}); |
0 commit comments