bssh 1.0.0

Parallel SSH command execution tool for cluster management
Documentation
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
// Copyright 2025 Lablup Inc. and Jeongkyu Shin
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! Advanced tests for SSH command execution options
//!
//! Tests for Match blocks, host merging, and edge cases
//! Note: Resolver tests are in src/ssh/ssh_config/resolver_tests.rs

use bssh::ssh::ssh_config::SshConfig;

#[test]
fn test_command_options_with_wildcards() {
    // Note: Match blocks are tested in internal integration tests
    // This test focuses on wildcard Host patterns
    let config = r#"
# Global defaults
Host *
    PermitLocalCommand no
    StdinNull no

# Wildcard pattern for dev hosts
Host *.dev.example.com
    PermitLocalCommand yes
    LocalCommand notify-send "Connected to %h"
    ForkAfterAuthentication no

# Wildcard pattern for prod hosts
Host *.prod.example.com
    PermitLocalCommand no
    RemoteCommand cd /opt/app && exec bash
    SessionType default
    StdinNull yes

# Specific host overrides wildcards
Host critical.prod.example.com
    RemoteCommand /usr/local/bin/critical-shell
    SessionType none
"#;

    let config_parsed = SshConfig::parse(config).unwrap();
    let hosts = config_parsed.hosts;

    // Should have 4 host blocks (*, *.dev, *.prod, specific)
    assert_eq!(hosts.len(), 4);

    // Check wildcard patterns are parsed
    let dev_host = &hosts[1];
    assert_eq!(dev_host.permit_local_command, Some(true));
    assert_eq!(
        dev_host.local_command,
        Some("notify-send \"Connected to %h\"".to_string())
    );
    assert_eq!(dev_host.fork_after_authentication, Some(false));

    let prod_host = &hosts[2];
    assert_eq!(prod_host.permit_local_command, Some(false));
    assert_eq!(
        prod_host.remote_command,
        Some("cd /opt/app && exec bash".to_string())
    );
    assert_eq!(prod_host.session_type, Some("default".to_string()));
    assert_eq!(prod_host.stdin_null, Some(true));

    let specific_host = &hosts[3];
    assert_eq!(
        specific_host.remote_command,
        Some("/usr/local/bin/critical-shell".to_string())
    );
    assert_eq!(specific_host.session_type, Some("none".to_string()));
}

#[test]
fn test_command_options_host_merging() {
    let config = r#"
# First Host block sets some options
Host server1
    PermitLocalCommand yes
    LocalCommand echo "First block"
    SessionType default

# Second Host block for same host (should override)
Host server1
    LocalCommand echo "Second block overrides"
    RemoteCommand tmux attach
    StdinNull yes
"#;

    let config_parsed = SshConfig::parse(config).unwrap();
    let hosts = config_parsed.hosts;

    // Both Host blocks should be present
    assert_eq!(hosts.len(), 2);

    // First occurrence
    assert_eq!(
        hosts[0].local_command,
        Some("echo \"First block\"".to_string())
    );
    assert_eq!(hosts[0].session_type, Some("default".to_string()));

    // Second occurrence
    assert_eq!(
        hosts[1].local_command,
        Some("echo \"Second block overrides\"".to_string())
    );
    assert_eq!(hosts[1].remote_command, Some("tmux attach".to_string()));
    assert_eq!(hosts[1].stdin_null, Some(true));
}

// Note: Resolver integration tests are in src/ssh/ssh_config/resolver_tests.rs
// because resolver is an internal module not accessible from integration tests

#[test]
fn test_very_long_command() {
    // Test with a very long command (1000+ characters)
    let long_cmd = "a".repeat(1000);
    let config = format!(
        r#"
Host test
    RemoteCommand {}
"#,
        long_cmd
    );

    let config_parsed = SshConfig::parse(&config).unwrap();
    let hosts = config_parsed.hosts;
    assert_eq!(hosts.len(), 1);
    assert_eq!(hosts[0].remote_command, Some(long_cmd));
}

#[test]
fn test_command_with_nested_quotes() {
    let config = r#"
Host test1
    LocalCommand bash -c "echo \"Hello 'World' from %h\""

Host test2
    RemoteCommand sh -c 'echo "Nested \"quotes\" work"'

Host test3
    LocalCommand echo 'Single\'s and "double\"s" mixed'
"#;

    let config_parsed = SshConfig::parse(config).unwrap();
    let hosts = config_parsed.hosts;
    assert_eq!(hosts.len(), 3);

    assert_eq!(
        hosts[0].local_command,
        Some("bash -c \"echo \\\"Hello 'World' from %h\\\"\"".to_string())
    );
    assert_eq!(
        hosts[1].remote_command,
        Some("sh -c 'echo \"Nested \\\"quotes\\\" work\"'".to_string())
    );
    assert_eq!(
        hosts[2].local_command,
        Some("echo 'Single\\'s and \"double\\\"s\" mixed'".to_string())
    );
}

#[test]
fn test_command_with_all_tokens() {
    let config = r#"
Host test
    LocalCommand echo "Host:%h Hostname:%H Original:%n Port:%p Remote:%r Local:%u Percent:%%"
"#;

    let config_parsed = SshConfig::parse(config).unwrap();
    let hosts = config_parsed.hosts;
    assert_eq!(hosts.len(), 1);

    assert_eq!(
        hosts[0].local_command,
        Some(
            "echo \"Host:%h Hostname:%H Original:%n Port:%p Remote:%r Local:%u Percent:%%\""
                .to_string()
        )
    );
}

#[test]
fn test_command_with_multiple_spaces() {
    let config = r#"
Host test1
    LocalCommand     rsync    -av     ~/src/     %h:~/dst/

Host test2
    RemoteCommand     cd    /tmp    &&    ls    -la
"#;

    let config_parsed = SshConfig::parse(config).unwrap();
    let hosts = config_parsed.hosts;
    assert_eq!(hosts.len(), 2);

    // Parser normalizes multiple spaces to single spaces (this is expected behavior)
    assert_eq!(
        hosts[0].local_command,
        Some("rsync -av ~/src/ %h:~/dst/".to_string())
    );
    assert_eq!(
        hosts[1].remote_command,
        Some("cd /tmp && ls -la".to_string())
    );
}

#[test]
fn test_command_with_safe_special_characters() {
    // Test RemoteCommand which allows more special characters than LocalCommand
    let config = r#"
Host test1
    RemoteCommand tmux attach -t main

Host test2
    RemoteCommand cd /tmp && ls -la

Host test3
    LocalCommand /usr/bin/notify-send "Connected to %h"
"#;

    let config_parsed = SshConfig::parse(config).unwrap();
    let hosts = config_parsed.hosts;
    assert_eq!(hosts.len(), 3);

    // RemoteCommand allows shell operators (runs on remote)
    assert_eq!(
        hosts[0].remote_command,
        Some("tmux attach -t main".to_string())
    );
    assert_eq!(
        hosts[1].remote_command,
        Some("cd /tmp && ls -la".to_string())
    );
    assert_eq!(
        hosts[2].local_command,
        Some("/usr/bin/notify-send \"Connected to %h\"".to_string())
    );
}

#[test]
fn test_known_hosts_command_with_complex_url() {
    let config = r#"
Host test1
    KnownHostsCommand curl -s "https://api.example.com/keys?host=%H&format=ssh"

Host test2
    KnownHostsCommand /opt/scripts/fetch-key.py --host=%h --port=%p
"#;

    let config_parsed = SshConfig::parse(config).unwrap();
    let hosts = config_parsed.hosts;
    assert_eq!(hosts.len(), 2);

    // Note: curl commands should fail validation due to dangerous chars
    // but let's check parsing first
    // Actually, the test3 in test_parse_known_hosts_command shows curl fails
}

#[test]
fn test_session_type_with_various_cases() {
    let config = r#"
Host test1
    SessionType NONE

Host test2
    SessionType Subsystem

Host test3
    SessionType DeFaUlT
"#;

    let config_parsed = SshConfig::parse(config).unwrap();
    let hosts = config_parsed.hosts;
    assert_eq!(hosts.len(), 3);

    // All should be normalized to lowercase
    assert_eq!(hosts[0].session_type, Some("none".to_string()));
    assert_eq!(hosts[1].session_type, Some("subsystem".to_string()));
    assert_eq!(hosts[2].session_type, Some("default".to_string()));
}

#[test]
fn test_permit_local_command_without_local_command() {
    // PermitLocalCommand yes but no LocalCommand is valid
    let config = r#"
Host test
    PermitLocalCommand yes
"#;

    let config_parsed = SshConfig::parse(config).unwrap();
    let hosts = config_parsed.hosts;
    assert_eq!(hosts.len(), 1);
    assert_eq!(hosts[0].permit_local_command, Some(true));
    assert_eq!(hosts[0].local_command, None);
}

#[test]
fn test_local_command_without_permit() {
    // LocalCommand without PermitLocalCommand is valid (client decides)
    let config = r#"
Host test
    LocalCommand echo "test"
"#;

    let config_parsed = SshConfig::parse(config).unwrap();
    let hosts = config_parsed.hosts;
    assert_eq!(hosts.len(), 1);
    assert_eq!(hosts[0].local_command, Some("echo \"test\"".to_string()));
    assert_eq!(hosts[0].permit_local_command, None);
}

#[test]
fn test_command_options_with_include() {
    // This would require file system access, so we'll test the structure
    let config = r#"
Host base
    PermitLocalCommand yes
    SessionType default

# Include directive would go here, but we can't test file I/O in unit tests
# Include ~/.ssh/config.d/*.conf

Host override
    SessionType none
    StdinNull yes
"#;

    let config_parsed = SshConfig::parse(config).unwrap();
    let hosts = config_parsed.hosts;

    // Should parse successfully with Include comment
    assert!(hosts.len() >= 2);
}

#[test]
fn test_fork_with_session_type_none() {
    // Common pattern: background tunnel
    let config = r#"
Host tunnel
    ForkAfterAuthentication yes
    SessionType none
    StdinNull yes
    LocalForward 8080 internal:80
"#;

    let config_parsed = SshConfig::parse(config).unwrap();
    let hosts = config_parsed.hosts;
    assert_eq!(hosts.len(), 1);

    assert_eq!(hosts[0].fork_after_authentication, Some(true));
    assert_eq!(hosts[0].session_type, Some("none".to_string()));
    assert_eq!(hosts[0].stdin_null, Some(true));
}

#[test]
fn test_remote_command_with_request_tty() {
    // Common pattern: auto-attach to tmux with TTY
    let config = r#"
Host dev
    RemoteCommand tmux attach -t dev || tmux new -s dev
    RequestTTY yes
"#;

    let config_parsed = SshConfig::parse(config).unwrap();
    let hosts = config_parsed.hosts;
    assert_eq!(hosts.len(), 1);

    assert_eq!(
        hosts[0].remote_command,
        Some("tmux attach -t dev || tmux new -s dev".to_string())
    );
    assert_eq!(hosts[0].request_tty, Some("yes".to_string()));
}

#[test]
fn test_command_with_path_expansion() {
    let config = r#"
Host test
    LocalCommand rsync -av ~/project/ %h:~/backup/
"#;

    let config_parsed = SshConfig::parse(config).unwrap();
    let hosts = config_parsed.hosts;
    assert_eq!(hosts.len(), 1);

    // Tilde should be preserved (client will expand)
    assert_eq!(
        hosts[0].local_command,
        Some("rsync -av ~/project/ %h:~/backup/".to_string())
    );
}

// Resolver tests moved to src/ssh/ssh_config/resolver_tests.rs