Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/platform/agentHost/test/common/sshConfigParsing.test.ts
13399 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import assert from 'assert';
7
import { ensureNoDisposablesAreLeakedInTestSuite } from '../../../../base/test/common/utils.js';
8
import { parseSSHConfigHostEntries, parseSSHGOutput } from '../../common/sshConfigParsing.js';
9
10
suite('SSH Config Parsing', () => {
11
12
ensureNoDisposablesAreLeakedInTestSuite();
13
14
suite('parseSSHConfigHostEntries', () => {
15
16
test('extracts simple host entries', () => {
17
const config = [
18
'Host myserver',
19
' HostName 10.0.0.1',
20
' User admin',
21
].join('\n');
22
23
assert.deepStrictEqual(parseSSHConfigHostEntries(config), ['myserver']);
24
});
25
26
test('extracts multiple hosts from a single Host line', () => {
27
const config = 'Host server1 server2 server3';
28
assert.deepStrictEqual(parseSSHConfigHostEntries(config), ['server1', 'server2', 'server3']);
29
});
30
31
test('extracts hosts from multiple Host directives', () => {
32
const config = [
33
'Host work',
34
' HostName work.example.com',
35
'',
36
'Host personal',
37
' HostName home.example.com',
38
].join('\n');
39
40
assert.deepStrictEqual(parseSSHConfigHostEntries(config), ['work', 'personal']);
41
});
42
43
test('skips wildcard hosts', () => {
44
const config = [
45
'Host *',
46
' ForwardAgent yes',
47
'',
48
'Host myserver',
49
' HostName 10.0.0.1',
50
'',
51
'Host *.example.com',
52
' User admin',
53
].join('\n');
54
55
assert.deepStrictEqual(parseSSHConfigHostEntries(config), ['myserver']);
56
});
57
58
test('skips negation patterns', () => {
59
const config = 'Host !internal myserver';
60
assert.deepStrictEqual(parseSSHConfigHostEntries(config), ['myserver']);
61
});
62
63
test('skips question mark wildcards', () => {
64
const config = 'Host server? myserver';
65
assert.deepStrictEqual(parseSSHConfigHostEntries(config), ['myserver']);
66
});
67
68
test('skips comment lines', () => {
69
const config = [
70
'# This is a comment',
71
'Host myserver',
72
' # Another comment',
73
' HostName 10.0.0.1',
74
].join('\n');
75
76
assert.deepStrictEqual(parseSSHConfigHostEntries(config), ['myserver']);
77
});
78
79
test('strips inline comments from Host values', () => {
80
const config = 'Host myserver # my favorite server';
81
assert.deepStrictEqual(parseSSHConfigHostEntries(config), ['myserver']);
82
});
83
84
test('handles empty content', () => {
85
assert.deepStrictEqual(parseSSHConfigHostEntries(''), []);
86
});
87
88
test('handles content with only comments and blanks', () => {
89
const config = [
90
'# comment',
91
'',
92
' # indented comment',
93
'',
94
].join('\n');
95
96
assert.deepStrictEqual(parseSSHConfigHostEntries(config), []);
97
});
98
99
test('is case-insensitive for Host keyword', () => {
100
const config = [
101
'host lower',
102
'HOST upper',
103
'Host mixed',
104
].join('\n');
105
106
assert.deepStrictEqual(parseSSHConfigHostEntries(config), ['lower', 'upper', 'mixed']);
107
});
108
109
test('ignores non-Host directives', () => {
110
const config = [
111
'Host myserver',
112
' HostName 10.0.0.1',
113
' User admin',
114
' Port 2222',
115
' IdentityFile ~/.ssh/mykey',
116
' ForwardAgent yes',
117
].join('\n');
118
119
assert.deepStrictEqual(parseSSHConfigHostEntries(config), ['myserver']);
120
});
121
});
122
123
suite('parseSSHGOutput', () => {
124
125
test('parses standard ssh -G output', () => {
126
const output = [
127
'hostname 10.0.0.1',
128
'user admin',
129
'port 22',
130
'identityfile ~/.ssh/id_rsa',
131
'identityfile ~/.ssh/id_ed25519',
132
'forwardagent no',
133
].join('\n');
134
135
assert.deepStrictEqual(parseSSHGOutput(output), {
136
hostname: '10.0.0.1',
137
user: 'admin',
138
port: 22,
139
identityFile: ['~/.ssh/id_rsa', '~/.ssh/id_ed25519'],
140
forwardAgent: false,
141
});
142
});
143
144
test('parses forwardagent yes', () => {
145
const output = [
146
'hostname example.com',
147
'user root',
148
'port 22',
149
'forwardagent yes',
150
].join('\n');
151
152
const result = parseSSHGOutput(output);
153
assert.strictEqual(result.forwardAgent, true);
154
});
155
156
test('parses non-standard port', () => {
157
const output = [
158
'hostname example.com',
159
'user deploy',
160
'port 2222',
161
].join('\n');
162
163
const result = parseSSHGOutput(output);
164
assert.strictEqual(result.port, 2222);
165
});
166
167
test('handles missing user', () => {
168
const output = [
169
'hostname example.com',
170
'port 22',
171
].join('\n');
172
173
const result = parseSSHGOutput(output);
174
assert.strictEqual(result.user, undefined);
175
});
176
177
test('handles empty user', () => {
178
const output = [
179
'hostname example.com',
180
'user ',
181
'port 22',
182
].join('\n');
183
184
const result = parseSSHGOutput(output);
185
assert.strictEqual(result.user, undefined);
186
});
187
188
test('defaults port to 22 when missing', () => {
189
const output = 'hostname example.com\nuser root';
190
const result = parseSSHGOutput(output);
191
assert.strictEqual(result.port, 22);
192
});
193
194
test('collects multiple identity files', () => {
195
const output = [
196
'hostname example.com',
197
'port 22',
198
'identityfile ~/.ssh/id_rsa',
199
'identityfile ~/.ssh/work_key',
200
'identityfile ~/.ssh/id_ed25519',
201
].join('\n');
202
203
assert.deepStrictEqual(parseSSHGOutput(output).identityFile, [
204
'~/.ssh/id_rsa',
205
'~/.ssh/work_key',
206
'~/.ssh/id_ed25519',
207
]);
208
});
209
210
test('handles empty output', () => {
211
assert.deepStrictEqual(parseSSHGOutput(''), {
212
hostname: '',
213
user: undefined,
214
port: 22,
215
identityFile: [],
216
forwardAgent: false,
217
});
218
});
219
220
test('handles values with spaces', () => {
221
const output = 'hostname my host with spaces\nport 22';
222
const result = parseSSHGOutput(output);
223
assert.strictEqual(result.hostname, 'my host with spaces');
224
});
225
});
226
});
227
228