Repository navigation
Expand file tree
/
Copy pathssh-config-parser.test.ts
More file actions
708 lines (610 loc) · 22.6 KB
/
Copy pathssh-config-parser.test.ts
File metadata and controls
708 lines (610 loc) · 22.6 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
import { describe, it, expect, beforeEach, afterEach } from 'vitest';
import { parseSSHConfig, looksLikeSSHAlias, resolveSymlink, parseJumpHost, parseJumpHosts, resolveJumpHosts } from '../ssh-config-parser.js';
import { mkdtempSync, writeFileSync, rmSync, symlinkSync, mkdirSync, realpathSync, unlinkSync } from 'fs';
import { tmpdir, homedir } from 'os';
import { join } from 'path';
/**
* Check if symlinks are supported on the current platform.
* On Windows without admin rights, symlink creation will fail with EPERM.
*/
function checkSymlinkSupport(): boolean {
const testDir = mkdtempSync(join(tmpdir(), 'symlink-check-'));
const targetFile = join(testDir, 'target');
const linkFile = join(testDir, 'link');
try {
writeFileSync(targetFile, 'test');
symlinkSync(targetFile, linkFile);
unlinkSync(linkFile);
unlinkSync(targetFile);
rmSync(testDir, { recursive: true });
return true;
} catch (error) {
rmSync(testDir, { recursive: true, force: true });
const e = error as NodeJS.ErrnoException;
return !(e.code === 'EPERM' || e.code === 'ENOTSUP');
}
}
// Check symlink support once at module load time
const symlinksSupported = checkSymlinkSupport();
// Point HOME (and USERPROFILE on Windows) at an empty fake home directory for
// every test in this file: parseSSHConfig falls back to scanning ~/.ssh/id_*
// when a host has no IdentityFile, so with the real home the results depend on
// whatever keys exist on the developer's machine. os.homedir() reads these env
// vars, so no module mocking is needed. (vi.spyOn(os, 'homedir') does NOT work
// here — ssh-config-parser.ts binds `homedir` via a named import, which the
// namespace spy cannot intercept.)
let fakeHome: string;
let savedHome: { HOME?: string; USERPROFILE?: string };
beforeEach(() => {
fakeHome = mkdtempSync(join(tmpdir(), 'dbhub-fake-home-'));
savedHome = { HOME: process.env.HOME, USERPROFILE: process.env.USERPROFILE };
process.env.HOME = fakeHome;
process.env.USERPROFILE = fakeHome;
});
afterEach(() => {
for (const key of ['HOME', 'USERPROFILE'] as const) {
if (savedHome[key] === undefined) delete process.env[key];
else process.env[key] = savedHome[key];
}
rmSync(fakeHome, { recursive: true, force: true });
});
describe('SSH Config Parser', () => {
let tempDir: string;
let configPath: string;
beforeEach(() => {
// Create a temporary directory for test config files
tempDir = mkdtempSync(join(tmpdir(), 'dbhub-ssh-test-'));
configPath = join(tempDir, 'config');
});
afterEach(() => {
// Clean up temporary directory
rmSync(tempDir, { recursive: true });
});
describe('parseSSHConfig', () => {
it('should parse basic SSH config', () => {
const configContent = `
Host myserver
HostName 192.168.1.100
User johndoe
Port 2222
`;
writeFileSync(configPath, configContent);
const result = parseSSHConfig('myserver', configPath);
expect(result).toEqual({
host: '192.168.1.100',
username: 'johndoe',
port: 2222
});
});
it('should handle identity file', () => {
const identityPath = join(tempDir, 'id_rsa');
writeFileSync(identityPath, 'fake-key-content');
const configContent = `
Host dev-server
HostName dev.example.com
User developer
IdentityFile ${identityPath}
`;
writeFileSync(configPath, configContent);
const result = parseSSHConfig('dev-server', configPath);
expect(result).toEqual({
host: 'dev.example.com',
username: 'developer',
// Path is resolved to real path (e.g., on macOS /var -> /private/var)
privateKey: realpathSync(identityPath)
});
});
it('should handle multiple identity files and use the first one', () => {
const identityPath1 = join(tempDir, 'id_rsa');
const identityPath2 = join(tempDir, 'id_ed25519');
writeFileSync(identityPath1, 'fake-key-1');
writeFileSync(identityPath2, 'fake-key-2');
const configContent = `
Host multi-key
HostName multi.example.com
User multiuser
IdentityFile ${identityPath1}
IdentityFile ${identityPath2}
`;
writeFileSync(configPath, configContent);
const result = parseSSHConfig('multi-key', configPath);
// Path is resolved to real path (e.g., on macOS /var -> /private/var)
expect(result?.privateKey).toBe(realpathSync(identityPath1));
});
it('should handle wildcard patterns', () => {
const configContent = `
Host *.example.com
User defaultuser
Port 2222
Host prod.example.com
HostName 10.0.0.100
`;
writeFileSync(configPath, configContent);
const result = parseSSHConfig('prod.example.com', configPath);
expect(result).toEqual({
host: '10.0.0.100',
username: 'defaultuser',
port: 2222
});
});
it('should use host alias as hostname if HostName not specified', () => {
const configContent = `
Host myalias
User testuser
`;
writeFileSync(configPath, configContent);
const result = parseSSHConfig('myalias', configPath);
expect(result).toEqual({
host: 'myalias',
username: 'testuser'
});
});
it('should return null for non-existent host', () => {
const configContent = `
Host myserver
HostName 192.168.1.100
User johndoe
`;
writeFileSync(configPath, configContent);
const result = parseSSHConfig('nonexistent', configPath);
expect(result).toBeNull();
});
it('should return null if config file does not exist', () => {
const result = parseSSHConfig('myserver', '/non/existent/path');
expect(result).toBeNull();
});
it('should return null if required fields are missing', () => {
const configContent = `
Host incomplete
HostName 192.168.1.100
`;
writeFileSync(configPath, configContent);
const result = parseSSHConfig('incomplete', configPath);
expect(result).toBeNull();
});
it('should handle tilde expansion in identity file', () => {
// Key lives in the (fake) home directory and is referenced with ~/
const mockKeyPath = join(fakeHome, 'mock_id_rsa');
writeFileSync(mockKeyPath, 'fake-key');
const configContent = `
Host tilde-test
HostName tilde.example.com
User tildeuser
IdentityFile ~/mock_id_rsa
`;
writeFileSync(configPath, configContent);
const result = parseSSHConfig('tilde-test', configPath);
// Path is resolved to real path (e.g., on macOS /var -> /private/var)
expect(result?.privateKey).toBe(realpathSync(mockKeyPath));
});
it('should fall back to a default ~/.ssh key when no IdentityFile is specified', () => {
mkdirSync(join(fakeHome, '.ssh'));
const defaultKeyPath = join(fakeHome, '.ssh', 'id_ed25519');
writeFileSync(defaultKeyPath, 'fake-default-key');
const configContent = `
Host default-key-test
HostName default.example.com
User defaultkeyuser
`;
writeFileSync(configPath, configContent);
const result = parseSSHConfig('default-key-test', configPath);
expect(result?.privateKey).toBe(realpathSync(defaultKeyPath));
});
});
describe('looksLikeSSHAlias', () => {
it('should return true for simple hostnames', () => {
expect(looksLikeSSHAlias('myserver')).toBe(true);
expect(looksLikeSSHAlias('dev-box')).toBe(true);
expect(looksLikeSSHAlias('prod_server')).toBe(true);
});
it('should return false for domains', () => {
expect(looksLikeSSHAlias('example.com')).toBe(false);
expect(looksLikeSSHAlias('sub.example.com')).toBe(false);
expect(looksLikeSSHAlias('my.local.dev')).toBe(false);
});
it('should return false for IP addresses', () => {
expect(looksLikeSSHAlias('192.168.1.1')).toBe(false);
expect(looksLikeSSHAlias('10.0.0.1')).toBe(false);
expect(looksLikeSSHAlias('::1')).toBe(false);
expect(looksLikeSSHAlias('2001:db8::1')).toBe(false);
});
});
describe('resolveSymlink', () => {
it('should return the same path for regular files', () => {
const filePath = join(tempDir, 'regular_file');
writeFileSync(filePath, 'content');
const result = resolveSymlink(filePath);
expect(result).toBe(realpathSync(filePath));
});
it.skipIf(!symlinksSupported)('should resolve symlinks to files', () => {
const targetPath = join(tempDir, 'target_file');
const linkPath = join(tempDir, 'link_to_file');
writeFileSync(targetPath, 'content');
symlinkSync(targetPath, linkPath);
const result = resolveSymlink(linkPath);
expect(result).toBe(realpathSync(targetPath));
});
it.skipIf(!symlinksSupported)('should resolve symlinks to directories', () => {
const targetDir = join(tempDir, 'target_dir');
const linkDir = join(tempDir, 'link_to_dir');
mkdirSync(targetDir);
symlinkSync(targetDir, linkDir, 'dir');
const result = resolveSymlink(linkDir);
expect(result).toBe(realpathSync(targetDir));
});
it('should handle tilde expansion', () => {
const result = resolveSymlink('~/some/path');
expect(result.startsWith(homedir())).toBe(true);
expect(result).toContain('some');
expect(result).toContain('path');
});
it('should return expanded path for non-existent files', () => {
const result = resolveSymlink('~/non/existent/path');
expect(result.startsWith(homedir())).toBe(true);
});
it.skipIf(!symlinksSupported)('should handle files within symlinked directories', () => {
const targetDir = join(tempDir, 'ssh_target');
const linkDir = join(tempDir, 'ssh_link');
mkdirSync(targetDir);
const configFile = join(targetDir, 'config');
writeFileSync(configFile, 'Host test\n User testuser\n');
symlinkSync(targetDir, linkDir, 'dir');
const linkedConfigPath = join(linkDir, 'config');
const result = resolveSymlink(linkedConfigPath);
expect(result).toBe(realpathSync(configFile));
});
});
describe.skipIf(!symlinksSupported)('parseSSHConfig with symlinks', () => {
it('should parse config from symlinked directory', () => {
const targetDir = join(tempDir, 'ssh_real');
const linkDir = join(tempDir, 'ssh_symlink');
mkdirSync(targetDir);
const configContent = `
Host symlink-test
HostName symlink.example.com
User symlinkuser
`;
writeFileSync(join(targetDir, 'config'), configContent);
symlinkSync(targetDir, linkDir, 'dir');
const linkedConfigPath = join(linkDir, 'config');
const result = parseSSHConfig('symlink-test', linkedConfigPath);
expect(result).toEqual({
host: 'symlink.example.com',
username: 'symlinkuser'
});
});
it('should handle identity file in symlinked directory', () => {
const targetDir = join(tempDir, 'ssh_keys_real');
const linkDir = join(tempDir, 'ssh_keys_link');
mkdirSync(targetDir);
const keyPath = join(targetDir, 'id_rsa');
writeFileSync(keyPath, 'fake-key-content');
symlinkSync(targetDir, linkDir, 'dir');
const linkedKeyPath = join(linkDir, 'id_rsa');
const configContent = `
Host key-symlink-test
HostName keytest.example.com
User keyuser
IdentityFile ${linkedKeyPath}
`;
writeFileSync(configPath, configContent);
const result = parseSSHConfig('key-symlink-test', configPath);
expect(result?.host).toBe('keytest.example.com');
expect(result?.username).toBe('keyuser');
// The private key path should be resolved to the real path
expect(result?.privateKey).toBe(realpathSync(keyPath));
});
});
describe('parseSSHConfig with ProxyJump', () => {
it('should extract ProxyJump from SSH config', () => {
const configContent = `
Host target-with-jump
HostName 10.0.0.5
User admin
ProxyJump bastion.example.com
`;
writeFileSync(configPath, configContent);
const result = parseSSHConfig('target-with-jump', configPath);
expect(result?.host).toBe('10.0.0.5');
expect(result?.username).toBe('admin');
expect(result?.proxyJump).toBe('bastion.example.com');
});
it('should extract multi-hop ProxyJump from SSH config', () => {
const configContent = `
Host multi-jump-target
HostName 10.0.0.6
User root
ProxyJump jump1.example.com,admin@jump2.example.com:2222
`;
writeFileSync(configPath, configContent);
const result = parseSSHConfig('multi-jump-target', configPath);
expect(result?.host).toBe('10.0.0.6');
expect(result?.username).toBe('root');
expect(result?.proxyJump).toBe('jump1.example.com,admin@jump2.example.com:2222');
});
});
});
describe('parseJumpHost', () => {
it('should parse simple hostname', () => {
const result = parseJumpHost('bastion.example.com');
expect(result).toEqual({
host: 'bastion.example.com',
port: 22,
username: undefined
});
});
it('should parse hostname with port', () => {
const result = parseJumpHost('bastion.example.com:2222');
expect(result).toEqual({
host: 'bastion.example.com',
port: 2222,
username: undefined
});
});
it('should parse hostname with username', () => {
const result = parseJumpHost('admin@bastion.example.com');
expect(result).toEqual({
host: 'bastion.example.com',
port: 22,
username: 'admin'
});
});
it('should parse hostname with username and port', () => {
const result = parseJumpHost('admin@bastion.example.com:2222');
expect(result).toEqual({
host: 'bastion.example.com',
port: 2222,
username: 'admin'
});
});
it('should handle IPv4 addresses', () => {
const result = parseJumpHost('192.168.1.100:22');
expect(result).toEqual({
host: '192.168.1.100',
port: 22,
username: undefined
});
});
it('should handle IPv6 addresses in brackets', () => {
const result = parseJumpHost('[::1]:22');
expect(result).toEqual({
host: '::1',
port: 22,
username: undefined
});
});
it('should handle IPv6 with username', () => {
const result = parseJumpHost('admin@[2001:db8::1]:2222');
expect(result).toEqual({
host: '2001:db8::1',
port: 2222,
username: 'admin'
});
});
it('should trim whitespace', () => {
const result = parseJumpHost(' admin@bastion.example.com:2222 ');
expect(result).toEqual({
host: 'bastion.example.com',
port: 2222,
username: 'admin'
});
});
it('should throw error for empty string', () => {
expect(() => parseJumpHost('')).toThrow('Jump host string cannot be empty');
expect(() => parseJumpHost(' ')).toThrow('Jump host string cannot be empty');
});
it('should throw error for empty host (user@:port)', () => {
expect(() => parseJumpHost('user@:22')).toThrow('host cannot be empty');
});
it('should throw error for only @ symbol', () => {
expect(() => parseJumpHost('@')).toThrow('host cannot be empty');
});
it('should throw error for only port (:22)', () => {
expect(() => parseJumpHost(':22')).toThrow('host cannot be empty');
});
it('should handle @host without username (treats as host)', () => {
const result = parseJumpHost('@bastion.example.com');
expect(result).toEqual({
host: 'bastion.example.com',
port: 22,
username: undefined
});
});
it('should throw error for invalid port numbers', () => {
// Port 0 is invalid
expect(() => parseJumpHost('host:0')).toThrow('Invalid port number');
expect(() => parseJumpHost('host:0')).toThrow('port must be between 1 and 65535');
// Port > 65535 is invalid
expect(() => parseJumpHost('host:99999')).toThrow('Invalid port number');
expect(() => parseJumpHost('host:99999')).toThrow('port must be between 1 and 65535');
// Valid port should work
const result = parseJumpHost('host:65535');
expect(result.port).toBe(65535);
});
it('should throw error for malformed IPv6 (missing closing bracket)', () => {
expect(() => parseJumpHost('[::1')).toThrow('missing closing bracket');
expect(() => parseJumpHost('user@[2001:db8::1')).toThrow('missing closing bracket');
});
it('should throw error for invalid port numbers in IPv6 addresses', () => {
// Port 0 is invalid for IPv6
expect(() => parseJumpHost('[::1]:0')).toThrow('Invalid port number');
expect(() => parseJumpHost('[::1]:0')).toThrow('port must be between 1 and 65535');
// Port > 65535 is invalid for IPv6
expect(() => parseJumpHost('[2001:db8::1]:99999')).toThrow('Invalid port number');
expect(() => parseJumpHost('[2001:db8::1]:99999')).toThrow('port must be between 1 and 65535');
// Valid port should work for IPv6
const result = parseJumpHost('[::1]:8080');
expect(result.port).toBe(8080);
});
});
describe('parseJumpHosts', () => {
it('should parse single jump host', () => {
const result = parseJumpHosts('bastion.example.com');
expect(result).toHaveLength(1);
expect(result[0]).toEqual({
host: 'bastion.example.com',
port: 22,
username: undefined
});
});
it('should parse multiple jump hosts', () => {
const result = parseJumpHosts('jump1.example.com,admin@jump2.example.com:2222');
expect(result).toHaveLength(2);
expect(result[0]).toEqual({
host: 'jump1.example.com',
port: 22,
username: undefined
});
expect(result[1]).toEqual({
host: 'jump2.example.com',
port: 2222,
username: 'admin'
});
});
it('should handle whitespace around commas', () => {
const result = parseJumpHosts('jump1.example.com , jump2.example.com');
expect(result).toHaveLength(2);
expect(result[0].host).toBe('jump1.example.com');
expect(result[1].host).toBe('jump2.example.com');
});
it('should return empty array for empty string', () => {
expect(parseJumpHosts('')).toEqual([]);
});
it('should return empty array for "none"', () => {
expect(parseJumpHosts('none')).toEqual([]);
expect(parseJumpHosts('NONE')).toEqual([]);
});
it('should filter out empty segments', () => {
const result = parseJumpHosts('jump1.example.com,,jump2.example.com');
expect(result).toHaveLength(2);
});
it('should parse complex multi-hop chain', () => {
const result = parseJumpHosts('bastion.company.com,admin@internal.company.com:2222,root@10.0.0.1');
expect(result).toHaveLength(3);
expect(result[0]).toEqual({ host: 'bastion.company.com', port: 22, username: undefined });
expect(result[1]).toEqual({ host: 'internal.company.com', port: 2222, username: 'admin' });
expect(result[2]).toEqual({ host: '10.0.0.1', port: 22, username: 'root' });
});
});
describe('resolveJumpHosts', () => {
let tempDir: string;
let configPath: string;
beforeEach(() => {
tempDir = mkdtempSync(join(tmpdir(), 'dbhub-resolvejump-'));
configPath = join(tempDir, 'config');
});
afterEach(() => {
rmSync(tempDir, { recursive: true, force: true });
});
it("resolves a ProxyJump alias to the bastion's real host/user/port/key (issue #347)", () => {
const keyPath = join(tempDir, 'bastion_key');
writeFileSync(keyPath, '-----BEGIN OPENSSH PRIVATE KEY-----\n');
writeFileSync(configPath, `
Host mybastion
HostName bastion.example.com
User ubuntu
Port 2200
IdentityFile ${keyPath}
Host target-with-jump
HostName 10.0.0.5
User admin
ProxyJump mybastion
`);
const hops = resolveJumpHosts('mybastion', configPath);
expect(hops).toHaveLength(1);
expect(hops[0].host).toBe('bastion.example.com');
expect(hops[0].port).toBe(2200);
expect(hops[0].username).toBe('ubuntu');
expect(hops[0].privateKey).toBe(realpathSync(keyPath));
});
it('resolves a jump alias that has no User (username inherited from target)', () => {
const keyPath = join(tempDir, 'bastion_key');
writeFileSync(keyPath, '-----BEGIN OPENSSH PRIVATE KEY-----\n');
writeFileSync(configPath, `
Host bastion
HostName bastion.example.com
Port 2200
IdentityFile ${keyPath}
`);
const hops = resolveJumpHosts('bastion', configPath);
expect(hops).toHaveLength(1);
expect(hops[0].host).toBe('bastion.example.com');
expect(hops[0].port).toBe(2200);
expect(hops[0].privateKey).toBe(realpathSync(keyPath));
// No User in the stanza → username left undefined so the tunnel inherits the target's.
expect(hops[0].username).toBeUndefined();
});
it('resolves a jump alias defining only Port/IdentityFile (HostName falls back to the alias)', () => {
const keyPath = join(tempDir, 'bastion_key');
writeFileSync(keyPath, '-----BEGIN OPENSSH PRIVATE KEY-----\n');
writeFileSync(configPath, `
Host bastion
Port 2200
IdentityFile ${keyPath}
`);
const hops = resolveJumpHosts('bastion', configPath);
expect(hops).toHaveLength(1);
// No HostName → OpenSSH uses the alias itself as the hostname.
expect(hops[0].host).toBe('bastion');
expect(hops[0].port).toBe(2200);
expect(hops[0].privateKey).toBe(realpathSync(keyPath));
expect(hops[0].username).toBeUndefined();
});
it('expands nested ProxyJump aliases in connection order (x -> a -> b)', () => {
writeFileSync(configPath, `
Host x
HostName x.example.com
User xu
Host a
HostName a.example.com
User au
ProxyJump x
Host b
HostName b.example.com
User bu
`);
const hops = resolveJumpHosts('a,b', configPath);
expect(hops.map((h) => h.host)).toEqual(['x.example.com', 'a.example.com', 'b.example.com']);
});
it('throws on a ProxyJump cycle', () => {
writeFileSync(configPath, `
Host a
HostName a.example.com
User au
ProxyJump b
Host b
HostName b.example.com
User bu
ProxyJump a
`);
expect(() => resolveJumpHosts('a', configPath)).toThrow(/cycle/i);
});
it('passes through literal (non-alias) jump hosts unchanged', () => {
writeFileSync(configPath, `Host unused\n HostName u.example.com\n User uu\n`);
const hops = resolveJumpHosts('bastion.example.com:2222', configPath);
expect(hops).toEqual([{ host: 'bastion.example.com', port: 2222, username: undefined }]);
});
it('lets an explicit :port on the token override the config Port (incl. :22)', () => {
writeFileSync(configPath, `
Host mybastion
HostName bastion.example.com
User ubuntu
Port 2200
`);
// No port on the token → use the alias's Port.
expect(resolveJumpHosts('mybastion', configPath)[0].port).toBe(2200);
// Explicit port on the token wins — including an explicit :22.
expect(resolveJumpHosts('mybastion:2022', configPath)[0].port).toBe(2022);
expect(resolveJumpHosts('mybastion:22', configPath)[0].port).toBe(22);
});
it('lets an explicit user@ on the token override the config User', () => {
writeFileSync(configPath, `
Host mybastion
HostName bastion.example.com
User ubuntu
`);
const hops = resolveJumpHosts('admin@mybastion', configPath);
expect(hops[0].host).toBe('bastion.example.com');
expect(hops[0].username).toBe('admin');
});
});