|
| 1 | +import { describe, it, beforeEach, vi, expect } from 'vitest' |
| 2 | +import { |
| 3 | + SSHConnectionPortForward, |
| 4 | + SSHConnectionLocalPortForward, |
| 5 | + SSHConnectionDynamicPortForward |
| 6 | +} from './ssh' |
| 7 | +import { SSHEmitter, SSHEvent, SSHConnectionAuth } from './index.d' |
| 8 | +import net from 'node:net' |
| 9 | +import socks from 'socksv5' |
| 10 | +import ssh2 from 'ssh2' |
| 11 | + |
| 12 | +vi.mock('node:net') |
| 13 | +vi.mock('socksv5') |
| 14 | +vi.mock('ssh2') |
| 15 | + |
| 16 | +let sshClientMock: any |
| 17 | +let sshEmitterMock: any |
| 18 | +let serverMock: any |
| 19 | + |
| 20 | +beforeEach(() => { |
| 21 | + sshClientMock = { |
| 22 | + connect: vi.fn(), |
| 23 | + end: vi.fn(), |
| 24 | + forwardOut: vi.fn(), |
| 25 | + on: vi.fn() |
| 26 | + } |
| 27 | + |
| 28 | + sshEmitterMock = new SSHEmitter() |
| 29 | + |
| 30 | + serverMock = { |
| 31 | + listen: vi.fn(), |
| 32 | + on: vi.fn(), |
| 33 | + close: vi.fn(), |
| 34 | + useAuth: vi.fn() |
| 35 | + } |
| 36 | + |
| 37 | + vi.spyOn(ssh2, 'Client').mockImplementation(() => sshClientMock) |
| 38 | + vi.spyOn(net, 'createServer').mockReturnValue(serverMock as any) |
| 39 | + vi.spyOn(socks, 'createServer').mockReturnValue(serverMock as any) |
| 40 | +}) |
| 41 | + |
| 42 | +describe('SSHConnectionPortForward', () => { |
| 43 | + it('should connect using provided auth', () => { |
| 44 | + const sshConnection = new SSHConnectionPortForward({}) |
| 45 | + const auth: SSHConnectionAuth = { |
| 46 | + host: 'localhost', |
| 47 | + username: 'user', |
| 48 | + password: 'pass', |
| 49 | + namespace: 'ns', |
| 50 | + device: 'dev' |
| 51 | + } |
| 52 | + |
| 53 | + sshConnection.connect(auth) |
| 54 | + expect(sshClientMock.connect).toHaveBeenCalledWith({ |
| 55 | + host: 'localhost', |
| 56 | + |
| 57 | + password: 'pass' |
| 58 | + }) |
| 59 | + }) |
| 60 | + |
| 61 | + it('should emit error event on connection error', () => { |
| 62 | + const sshConnection = new SSHConnectionPortForward({}) |
| 63 | + const errorCallback = vi.fn() |
| 64 | + sshConnection.onError(errorCallback) |
| 65 | + |
| 66 | + const error = new Error('Connection failed') |
| 67 | + sshClientMock.connect.mockImplementation(() => { |
| 68 | + throw error |
| 69 | + }) |
| 70 | + |
| 71 | + sshConnection.connect({ |
| 72 | + host: 'localhost', |
| 73 | + username: 'user', |
| 74 | + password: 'pass', |
| 75 | + namespace: 'ns', |
| 76 | + device: 'dev' |
| 77 | + }) |
| 78 | + |
| 79 | + expect(errorCallback).toHaveBeenCalledWith(error) |
| 80 | + }) |
| 81 | +}) |
| 82 | + |
| 83 | +describe('SSHConnectionLocalPortForward', () => { |
| 84 | + it('should start local port forwarding', () => { |
| 85 | + const settings = { |
| 86 | + sourceAddr: '127.0.0.1', |
| 87 | + sourcePort: 8000, |
| 88 | + destinationAddr: '192.168.1.10', |
| 89 | + destinationPort: 8080 |
| 90 | + } |
| 91 | + |
| 92 | + const sshConnection = new SSHConnectionLocalPortForward(settings) |
| 93 | + sshClientMock.on.mock.calls.find((call) => call[0] === 'ready')[1]() |
| 94 | + |
| 95 | + expect(serverMock.listen).toHaveBeenCalledWith( |
| 96 | + settings.sourcePort, |
| 97 | + settings.sourceAddr, |
| 98 | + expect.any(Function) |
| 99 | + ) |
| 100 | + }) |
| 101 | +}) |
| 102 | + |
| 103 | +describe('SSHConnectionDynamicPortForward', () => { |
| 104 | + it('should start dynamic port forwarding', () => { |
| 105 | + const settings = { |
| 106 | + destinationAddr: '127.0.0.1', |
| 107 | + destinationPort: 1080 |
| 108 | + } |
| 109 | + |
| 110 | + const sshConnection = new SSHConnectionDynamicPortForward(settings) |
| 111 | + sshClientMock.on.mock.calls.find((call) => call[0] === 'ready')[1]() |
| 112 | + |
| 113 | + expect(serverMock.listen).toHaveBeenCalledWith( |
| 114 | + settings.destinationPort, |
| 115 | + settings.destinationAddr, |
| 116 | + expect.any(Function) |
| 117 | + ) |
| 118 | + }) |
| 119 | + |
| 120 | + it('should handle socks authentication', () => { |
| 121 | + const settings = { |
| 122 | + destinationAddr: '127.0.0.1', |
| 123 | + destinationPort: 1080 |
| 124 | + } |
| 125 | + |
| 126 | + new SSHConnectionDynamicPortForward(settings) |
| 127 | + |
| 128 | + expect(serverMock.useAuth).toHaveBeenCalledWith(socks.auth.None()) |
| 129 | + }) |
| 130 | +}) |
0 commit comments