1
use super::upload::{MAX_UPLOAD_BYTES, append_upload, client_reason};
2
use super::{SshConnectInfo, SshdState, is_peer_hangup};
3
use crate::eval_subsystem_tests::{insert_verified_user, random_key, register_key, setup};
4
use crate::rate_limit::{Config as LimitConfig, InstantClock, RateLimiter};
5
use russh::server::Server as _;
6
use server::command::CmdError;
7
use server::db::install_test_pool;
8
use sqlx::PgPool;
9
use sqlx::types::Uuid;
10
use std::io::{Error, ErrorKind};
11
use std::sync::Arc;
12
use std::time::Duration;
13
use supp_macro::local_db_sqlx_test;
14
use tokio::sync::Mutex;
15

            
16
#[test]
17
1
fn a_peer_walking_away_is_not_a_session_error() {
18
1
    for hangup in [
19
1
        russh::Error::Disconnect,
20
1
        russh::Error::HUP,
21
1
        russh::Error::IO(Error::from(ErrorKind::ConnectionReset)),
22
1
        russh::Error::IO(Error::from(ErrorKind::UnexpectedEof)),
23
1
        russh::Error::IO(Error::from(ErrorKind::BrokenPipe)),
24
1
    ] {
25
5
        assert!(is_peer_hangup(&hangup), "{hangup:?}");
26
    }
27
1
}
28

            
29
#[test]
30
1
fn a_real_failure_is_still_reported() {
31
1
    for failure in [
32
1
        russh::Error::InactivityTimeout,
33
1
        russh::Error::IO(Error::from(ErrorKind::PermissionDenied)),
34
1
    ] {
35
2
        assert!(!is_peer_hangup(&failure), "{failure:?}");
36
    }
37
1
}
38

            
39
#[test]
40
1
fn an_upload_stops_growing_at_the_cap() {
41
1
    let mut buf = Vec::new();
42
1
    assert!(append_upload(&mut buf, &vec![b'k'; MAX_UPLOAD_BYTES]));
43
1
    assert!(!append_upload(&mut buf, b"k"));
44
1
    assert_eq!(buf.len(), MAX_UPLOAD_BYTES);
45
1
}
46

            
47
#[test]
48
1
fn an_oversized_chunk_is_refused_whole() {
49
1
    let mut buf = b"ssh-ed25519 ".to_vec();
50
1
    assert!(!append_upload(&mut buf, &vec![b'k'; MAX_UPLOAD_BYTES]));
51
1
    assert_eq!(buf, b"ssh-ed25519 ");
52
1
}
53

            
54
1
fn state() -> SshdState {
55
1
    SshdState {
56
1
        limiter: Arc::new(Mutex::new(RateLimiter::new(
57
1
            LimitConfig {
58
1
                max_attempts_per_ip: 6,
59
1
                max_pubkey_attempts_per_ip: 30,
60
1
                window: Duration::from_mins(1),
61
1
                account_lockout_threshold: 3,
62
1
                account_lockout_duration: Duration::from_mins(15),
63
1
            },
64
1
            InstantClock,
65
1
        ))),
66
1
        connect_info: SshConnectInfo {
67
1
            hostname: "localhost".to_owned(),
68
1
            port: 2222,
69
1
            host_fingerprint: String::new(),
70
1
        },
71
1
    }
72
1
}
73

            
74
#[test]
75
1
fn a_database_fault_reaches_the_client_as_a_fixed_text() {
76
1
    let fault = CmdError::DB(sqlx::Error::PoolTimedOut);
77
1
    assert_eq!(
78
1
        client_reason(Uuid::nil(), &fault),
79
1
        "the key could not be stored"
80
    );
81
1
}
82

            
83
#[test]
84
1
fn a_caller_mistake_reaches_the_client_as_it_is() {
85
1
    let mistake = CmdError::Args("not your key".to_owned());
86
1
    assert_eq!(
87
1
        client_reason(Uuid::nil(), &mistake),
88
1
        "Wrong arguments: not your key"
89
    );
90
1
}
91

            
92
#[local_db_sqlx_test]
93
async fn a_key_held_by_another_account_is_reported_as_it_is(pool: PgPool) -> anyhow::Result<()> {
94
    let (owner, other) = (Uuid::new_v4(), Uuid::new_v4());
95
    insert_verified_user(&pool, owner).await?;
96
    sqlx::query(
97
        "INSERT INTO users (id, user_name, email, photo, verified, user_password, user_role, database_name, created_at)
98
         VALUES ($1, 'other', 'other@example.test', 'default.png', TRUE, 'x', 'user', 'other', NOW())",
99
    )
100
    .bind(other)
101
    .execute(&pool)
102
    .await?;
103
    let key = random_key()?;
104
    register_key(owner, &key).await?;
105
    let mut handler = state().new_client(None);
106
    handler.user_id = Some(other);
107
    let line = key.public_key().to_openssh()?;
108
    let banner = handler.process_pubkey_upload(line.as_bytes()).await;
109
    assert!(
110
        banner.contains("already registered to another account"),
111
        "{banner}"
112
    );
113
}