rcom/tests/ssh_config.rs
2026-12-05 23:30:06 +03:00

233 lines
6.8 KiB
Rust

use std::fs;
use std::path::{Path, PathBuf};
use rcom::ssh::config::MAX_INCLUDE_DEPTH;
use rcom::ssh::discover;
use tempfile::TempDir;
fn write(path: impl AsRef<Path>, contents: &str) {
let path = path.as_ref();
fs::create_dir_all(path.parent().expect("fixture path has a parent")).unwrap();
fs::write(path, contents).unwrap();
}
fn paths(home: &TempDir) -> (PathBuf, PathBuf) {
let home_path = home.path().join("home");
let config = home_path.join(".ssh/config");
(config, home_path)
}
#[test]
fn host_aliases_multiple_dedup_and_first_declaration_order() {
let fixture = TempDir::new().unwrap();
let (config, home) = paths(&fixture);
write(
&config,
"Host alpha beta gamma\nHost beta delta alpha\nHost epsilon\n",
);
let result = discover(&config, &home);
assert_eq!(result.hosts, ["alpha", "beta", "gamma", "delta", "epsilon"]);
assert!(result.warnings.is_empty());
}
#[test]
fn comments_quotes_and_escaped_whitespace_are_tokenized() {
let fixture = TempDir::new().unwrap();
let (config, home) = paths(&fixture);
write(
&config,
r#"
# Host commented-out
Host "quoted host" plain "hash#alias" # trailing Host ignored
Host escaped\ alias
HostName ignored.example
"#,
);
let result = discover(&config, &home);
assert_eq!(
result.hosts,
["quoted host", "plain", "hash#alias", "escaped alias"]
);
assert!(result.warnings.is_empty());
}
#[test]
fn wildcard_and_negated_host_patterns_are_excluded() {
let fixture = TempDir::new().unwrap();
let (config, home) = paths(&fixture);
write(
&config,
"Host * ?web web-* !negated explicit\nHost another? another! another\n",
);
let result = discover(&config, &home);
assert_eq!(result.hosts, ["explicit", "another!", "another"]);
}
#[test]
fn missing_root_config_returns_empty_result_without_warnings() {
let fixture = TempDir::new().unwrap();
let home = fixture.path().join("home");
let config = home.join(".ssh/does-not-exist.conf");
let result = discover(&config, &home);
assert_eq!(result.hosts, Vec::<String>::new());
assert!(result.warnings.is_empty());
}
#[test]
fn relative_include_is_resolved_against_home_dot_ssh() {
let fixture = TempDir::new().unwrap();
let (config, home) = paths(&fixture);
let elsewhere = fixture.path().join("elsewhere");
let root = elsewhere.join("config");
write(&root, "Include nested/hosts.conf\nHost root\n");
write(elsewhere.join("nested/hosts.conf"), "Host wrong-base\n");
write(
home.join(".ssh/nested/hosts.conf"),
"Host included\nInclude deeper.conf\n",
);
write(home.join(".ssh/deeper.conf"), "Host deeper\n");
write(config, "Host unused-default\n");
let result = discover(&root, &home);
assert_eq!(result.hosts, ["included", "deeper", "root"]);
assert!(result.warnings.is_empty());
}
#[test]
fn tilde_include_is_expanded_with_supplied_home() {
let fixture = TempDir::new().unwrap();
let (config, home) = paths(&fixture);
write(
&config,
"Include \"~/.ssh/quoted include.conf\"\nHost root\n",
);
write(
home.join(".ssh/quoted include.conf"),
"Host tilde-included\n",
);
let result = discover(&config, &home);
assert_eq!(result.hosts, ["tilde-included", "root"]);
assert!(result.warnings.is_empty());
}
#[test]
fn include_glob_matches_are_scanned_in_lexical_order() {
let fixture = TempDir::new().unwrap();
let (config, home) = paths(&fixture);
write(&config, "Include conf.d/*.conf\nHost root\n");
write(home.join(".ssh/conf.d/10-last.conf"), "Host z-last\n");
write(home.join(".ssh/conf.d/02-first.conf"), "Host a-first\n");
write(home.join(".ssh/conf.d/ignored.txt"), "Host not-included\n");
let result = discover(&config, &home);
assert_eq!(result.hosts, ["a-first", "z-last", "root"]);
assert!(result.warnings.is_empty());
}
#[cfg(unix)]
#[test]
fn include_cycle_is_detected_by_canonical_path() {
use std::os::unix::fs::symlink;
let fixture = TempDir::new().unwrap();
let (config, home) = paths(&fixture);
write(&config, "Host root\nInclude cycle.conf\n");
write(
home.join(".ssh/cycle.conf"),
"Host cycle-member\nInclude root-link.conf\n",
);
symlink(&config, home.join(".ssh/root-link.conf")).unwrap();
let result = discover(&config, &home);
assert_eq!(result.hosts, ["root", "cycle-member"]);
assert_eq!(result.warnings.len(), 1);
assert!(result.warnings[0].contains("cycle"));
}
#[test]
fn include_depth_is_bounded_and_preserves_aliases_within_limit() {
let fixture = TempDir::new().unwrap();
let (config, home) = paths(&fixture);
write(&config, "Host root\nInclude chain-00.conf\n");
for index in 0..=MAX_INCLUDE_DEPTH {
let next = if index < MAX_INCLUDE_DEPTH {
format!("Include chain-{:02}.conf\n", index + 1)
} else {
String::new()
};
write(
home.join(format!(".ssh/chain-{index:02}.conf")),
&format!("Host chain-{index:02}\n{next}"),
);
}
let result = discover(&config, &home);
let mut expected = vec!["root".to_string()];
expected.extend((0..MAX_INCLUDE_DEPTH).map(|index| format!("chain-{index:02}")));
assert_eq!(result.hosts, expected);
assert_eq!(result.warnings.len(), 1);
assert!(result.warnings[0].contains("depth limit"));
}
#[test]
fn broken_include_warns_without_losing_other_hosts() {
let fixture = TempDir::new().unwrap();
let (config, home) = paths(&fixture);
write(
&config,
"Host before\nInclude missing.conf\nInclude good.conf\nHost after\n",
);
write(home.join(".ssh/good.conf"), "Host included\n");
let result = discover(&config, &home);
assert_eq!(result.hosts, ["before", "included", "after"]);
assert_eq!(result.warnings.len(), 1);
assert!(result.warnings[0].contains("missing.conf"));
}
#[test]
fn unmatched_include_glob_is_not_an_error() {
let fixture = TempDir::new().unwrap();
let (config, home) = paths(&fixture);
write(&config, "Include optional/*.conf\nHost root\n");
let result = discover(&config, &home);
assert_eq!(result.hosts, ["root"]);
assert!(result.warnings.is_empty());
}
#[test]
fn discovery_does_not_execute_proxy_command_or_match_exec() {
let fixture = TempDir::new().unwrap();
let (config, home) = paths(&fixture);
let marker = fixture.path().join("discovery-executed-a-command");
write(
&config,
&format!(
"Host safe\nProxyCommand touch {}\nMatch exec \"touch {}\"\nHost still-safe\n",
marker.display(),
marker.display()
),
);
let result = discover(&config, &home);
assert_eq!(result.hosts, ["safe", "still-safe"]);
assert!(!marker.exists());
}