|
| 1 | +import * as os from "os"; |
| 2 | +import { getControlPath } from "./sshConnectionPool"; |
| 3 | +import type { SSHRuntimeConfig } from "./SSHRuntime"; |
| 4 | + |
| 5 | +describe("sshConnectionPool", () => { |
| 6 | + describe("getControlPath", () => { |
| 7 | + test("identical configs produce same controlPath", () => { |
| 8 | + const config: SSHRuntimeConfig = { |
| 9 | + host: "test.example.com", |
| 10 | + srcBaseDir: "/work", |
| 11 | + }; |
| 12 | + const path1 = getControlPath(config); |
| 13 | + const path2 = getControlPath(config); |
| 14 | + |
| 15 | + expect(path1).toBe(path2); |
| 16 | + }); |
| 17 | + |
| 18 | + test("different hosts produce different controlPaths", () => { |
| 19 | + const path1 = getControlPath({ |
| 20 | + host: "host1.example.com", |
| 21 | + srcBaseDir: "/work", |
| 22 | + }); |
| 23 | + const path2 = getControlPath({ |
| 24 | + host: "host2.example.com", |
| 25 | + srcBaseDir: "/work", |
| 26 | + }); |
| 27 | + |
| 28 | + expect(path1).not.toBe(path2); |
| 29 | + }); |
| 30 | + |
| 31 | + test("different ports produce different controlPaths", () => { |
| 32 | + const config1: SSHRuntimeConfig = { |
| 33 | + host: "test.com", |
| 34 | + srcBaseDir: "/work", |
| 35 | + port: 22, |
| 36 | + }; |
| 37 | + const config2: SSHRuntimeConfig = { |
| 38 | + host: "test.com", |
| 39 | + srcBaseDir: "/work", |
| 40 | + port: 2222, |
| 41 | + }; |
| 42 | + |
| 43 | + expect(getControlPath(config1)).not.toBe(getControlPath(config2)); |
| 44 | + }); |
| 45 | + |
| 46 | + test("different identityFiles produce different controlPaths", () => { |
| 47 | + const config1: SSHRuntimeConfig = { |
| 48 | + host: "test.com", |
| 49 | + srcBaseDir: "/work", |
| 50 | + identityFile: "/path/to/key1", |
| 51 | + }; |
| 52 | + const config2: SSHRuntimeConfig = { |
| 53 | + host: "test.com", |
| 54 | + srcBaseDir: "/work", |
| 55 | + identityFile: "/path/to/key2", |
| 56 | + }; |
| 57 | + |
| 58 | + expect(getControlPath(config1)).not.toBe(getControlPath(config2)); |
| 59 | + }); |
| 60 | + |
| 61 | + test("different srcBaseDirs produce different controlPaths", () => { |
| 62 | + const config1: SSHRuntimeConfig = { |
| 63 | + host: "test.com", |
| 64 | + srcBaseDir: "/work1", |
| 65 | + }; |
| 66 | + const config2: SSHRuntimeConfig = { |
| 67 | + host: "test.com", |
| 68 | + srcBaseDir: "/work2", |
| 69 | + }; |
| 70 | + |
| 71 | + expect(getControlPath(config1)).not.toBe(getControlPath(config2)); |
| 72 | + }); |
| 73 | + |
| 74 | + test("controlPath is in tmpdir with expected format", () => { |
| 75 | + const config: SSHRuntimeConfig = { |
| 76 | + host: "test.com", |
| 77 | + srcBaseDir: "/work", |
| 78 | + }; |
| 79 | + const controlPath = getControlPath(config); |
| 80 | + |
| 81 | + expect(controlPath).toContain(os.tmpdir()); |
| 82 | + expect(controlPath).toMatch(/cmux-ssh-[a-f0-9]{12}$/); |
| 83 | + }); |
| 84 | + |
| 85 | + test("missing port defaults to 22 in hash calculation", () => { |
| 86 | + const config1: SSHRuntimeConfig = { |
| 87 | + host: "test.com", |
| 88 | + srcBaseDir: "/work", |
| 89 | + port: 22, |
| 90 | + }; |
| 91 | + const config2: SSHRuntimeConfig = { |
| 92 | + host: "test.com", |
| 93 | + srcBaseDir: "/work", |
| 94 | + // port omitted, should default to 22 |
| 95 | + }; |
| 96 | + |
| 97 | + expect(getControlPath(config1)).toBe(getControlPath(config2)); |
| 98 | + }); |
| 99 | + |
| 100 | + test("missing identityFile defaults to 'default' in hash calculation", () => { |
| 101 | + const config1: SSHRuntimeConfig = { |
| 102 | + host: "test.com", |
| 103 | + srcBaseDir: "/work", |
| 104 | + identityFile: undefined, |
| 105 | + }; |
| 106 | + const config2: SSHRuntimeConfig = { |
| 107 | + host: "test.com", |
| 108 | + srcBaseDir: "/work", |
| 109 | + // identityFile omitted |
| 110 | + }; |
| 111 | + |
| 112 | + expect(getControlPath(config1)).toBe(getControlPath(config2)); |
| 113 | + }); |
| 114 | + }); |
| 115 | +}); |
| 116 | + |
| 117 | +describe("username isolation", () => { |
| 118 | + test("controlPath includes local username to prevent cross-user collisions", () => { |
| 119 | + // This test verifies that os.userInfo().username is included in the hash |
| 120 | + // On multi-user systems, different users connecting to the same remote |
| 121 | + // would get different controlPaths, preventing permission errors |
| 122 | + const config: SSHRuntimeConfig = { |
| 123 | + host: "test.com", |
| 124 | + srcBaseDir: "/work", |
| 125 | + }; |
| 126 | + const controlPath = getControlPath(config); |
| 127 | + |
| 128 | + // The path should be deterministic for this user |
| 129 | + expect(controlPath).toBe(getControlPath(config)); |
| 130 | + expect(controlPath).toMatch(/^\/tmp\/cmux-ssh-[a-f0-9]{12}$/); |
| 131 | + }); |
| 132 | +}); |
0 commit comments