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
}
19

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

            
34
#[test]
35
1
fn bind_defaults_to_loopback_without_cli_or_env() {
36
1
    assert_eq!(pick_bind(None, None).unwrap().to_string(), DEFAULT_BIND);
37
1
}
38

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

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

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

            
58
#[test]
59
1
fn host_key_defaults_without_cli_or_env() {
60
1
    assert_eq!(pick_host_key(None, None), PathBuf::from(DEFAULT_HOST_KEY));
61
1
}
62

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

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