1
use super::*;
2

            
3
#[test]
4
1
fn cli_parses_defaults() {
5
1
    let cli = Cli::try_parse_from(["nomisync-sshd"]).expect("defaults should parse");
6
1
    assert!(
7
1
        cli.bind.is_none(),
8
        "bind unset on CLI falls through to env/default"
9
    );
10
1
    assert!(cli.host_key.is_none());
11
1
    assert_eq!(cli.max_auth_attempts_per_ip, 6);
12
1
    assert_eq!(cli.max_pubkey_attempts_per_ip, 30);
13
1
    assert_eq!(cli.lockout_threshold, 3);
14
1
    assert_eq!(cli.lockout_duration_secs, 900);
15
1
    assert!(cli.public_hostname.is_none());
16
1
    assert!(cli.public_port.is_none());
17
1
    assert!(cli.host_fingerprint.is_none());
18
1
    assert!(cli.metrics_bind.is_none());
19
1
    assert_eq!(cli.max_startups, 64);
20
1
    assert_eq!(cli.pre_auth_timeout_secs, 120);
21
1
}
22

            
23
#[test]
24
1
fn cli_accepts_explicit_bind_and_key_path() {
25
1
    let cli = Cli::try_parse_from([
26
1
        "nomisync-sshd",
27
1
        "--bind",
28
1
        "0.0.0.0:22",
29
1
        "--host-key",
30
1
        "/tmp/k",
31
1
    ])
32
1
    .unwrap();
33
1
    assert_eq!(cli.bind.unwrap().to_string(), "0.0.0.0:22");
34
1
    assert_eq!(cli.host_key.unwrap().to_string_lossy(), "/tmp/k");
35
1
}
36

            
37
#[test]
38
1
fn bind_defaults_to_loopback_without_cli_or_env() {
39
1
    assert_eq!(pick_bind(None, None).unwrap().to_string(), DEFAULT_BIND);
40
1
}
41

            
42
#[test]
43
1
fn bind_falls_back_to_env() {
44
1
    let resolved = pick_bind(None, Some("127.0.0.1:9999".to_owned())).unwrap();
45
1
    assert_eq!(resolved.to_string(), "127.0.0.1:9999");
46
1
}
47

            
48
#[test]
49
1
fn bind_prefers_cli_over_env() {
50
1
    let explicit: SocketAddr = "10.0.0.1:22".parse().unwrap();
51
1
    let resolved = pick_bind(Some(explicit), Some("127.0.0.1:9999".to_owned())).unwrap();
52
1
    assert_eq!(resolved, explicit);
53
1
    assert_eq!(resolve_bind(Some(explicit)).unwrap(), explicit);
54
1
}
55

            
56
#[test]
57
1
fn malformed_env_bind_is_an_error() {
58
1
    assert!(pick_bind(None, Some("not-an-address".to_owned())).is_err());
59
1
}
60

            
61
#[test]
62
1
fn host_key_defaults_without_cli_or_env() {
63
1
    assert_eq!(pick_host_key(None, None), PathBuf::from(DEFAULT_HOST_KEY));
64
1
}
65

            
66
#[test]
67
1
fn host_key_falls_back_to_env() {
68
1
    let resolved = pick_host_key(None, Some(OsString::from("/tmp/env-key")));
69
1
    assert_eq!(resolved, PathBuf::from("/tmp/env-key"));
70
1
}
71

            
72
#[test]
73
1
fn host_key_prefers_cli_over_env() {
74
1
    let explicit = PathBuf::from("/tmp/cli-key");
75
1
    let resolved = pick_host_key(Some(explicit.clone()), Some(OsString::from("/tmp/env-key")));
76
1
    assert_eq!(resolved, explicit);
77
1
    assert_eq!(resolve_host_key(Some(explicit.clone())), explicit);
78
1
}