Lines
100 %
Functions
Branches
use super::upload::{MAX_UPLOAD_BYTES, append_upload, client_reason};
use super::{SshConnectInfo, SshdState, is_peer_hangup};
use crate::eval_subsystem_tests::{insert_verified_user, random_key, register_key, setup};
use crate::rate_limit::{Config as LimitConfig, InstantClock, RateLimiter};
use russh::server::Server as _;
use server::command::CmdError;
use server::db::install_test_pool;
use sqlx::PgPool;
use sqlx::types::Uuid;
use std::io::{Error, ErrorKind};
use std::sync::Arc;
use std::time::Duration;
use supp_macro::local_db_sqlx_test;
use tokio::sync::Mutex;
#[test]
fn a_peer_walking_away_is_not_a_session_error() {
for hangup in [
russh::Error::Disconnect,
russh::Error::HUP,
russh::Error::IO(Error::from(ErrorKind::ConnectionReset)),
russh::Error::IO(Error::from(ErrorKind::UnexpectedEof)),
russh::Error::IO(Error::from(ErrorKind::BrokenPipe)),
] {
assert!(is_peer_hangup(&hangup), "{hangup:?}");
}
fn a_real_failure_is_still_reported() {
for failure in [
russh::Error::InactivityTimeout,
russh::Error::IO(Error::from(ErrorKind::PermissionDenied)),
assert!(!is_peer_hangup(&failure), "{failure:?}");
fn an_upload_stops_growing_at_the_cap() {
let mut buf = Vec::new();
assert!(append_upload(&mut buf, &vec![b'k'; MAX_UPLOAD_BYTES]));
assert!(!append_upload(&mut buf, b"k"));
assert_eq!(buf.len(), MAX_UPLOAD_BYTES);
fn an_oversized_chunk_is_refused_whole() {
let mut buf = b"ssh-ed25519 ".to_vec();
assert!(!append_upload(&mut buf, &vec![b'k'; MAX_UPLOAD_BYTES]));
assert_eq!(buf, b"ssh-ed25519 ");
fn state() -> SshdState {
SshdState {
limiter: Arc::new(Mutex::new(RateLimiter::new(
LimitConfig {
max_attempts_per_ip: 6,
max_pubkey_attempts_per_ip: 30,
window: Duration::from_mins(1),
account_lockout_threshold: 3,
account_lockout_duration: Duration::from_mins(15),
},
InstantClock,
))),
connect_info: SshConnectInfo {
hostname: "localhost".to_owned(),
port: 2222,
host_fingerprint: String::new(),
fn a_database_fault_reaches_the_client_as_a_fixed_text() {
let fault = CmdError::DB(sqlx::Error::PoolTimedOut);
assert_eq!(
client_reason(Uuid::nil(), &fault),
"the key could not be stored"
);
fn a_caller_mistake_reaches_the_client_as_it_is() {
let mistake = CmdError::Args("not your key".to_owned());
client_reason(Uuid::nil(), &mistake),
"Wrong arguments: not your key"
#[local_db_sqlx_test]
async fn a_key_held_by_another_account_is_reported_as_it_is(pool: PgPool) -> anyhow::Result<()> {
let (owner, other) = (Uuid::new_v4(), Uuid::new_v4());
insert_verified_user(&pool, owner).await?;
sqlx::query(
"INSERT INTO users (id, user_name, email, photo, verified, user_password, user_role, database_name, created_at)
VALUES ($1, 'other', 'other@example.test', 'default.png', TRUE, 'x', 'user', 'other', NOW())",
)
.bind(other)
.execute(&pool)
.await?;
let key = random_key()?;
register_key(owner, &key).await?;
let mut handler = state().new_client(None);
handler.user_id = Some(other);
let line = key.public_key().to_openssh()?;
let banner = handler.process_pubkey_upload(line.as_bytes()).await;
assert!(
banner.contains("already registered to another account"),
"{banner}"