Lines
100 %
Functions
Branches
//! The accept loop: the connection cap and the deadline to authenticate.
use crate::accept::AcceptLimits;
use crate::eval_subsystem_tests::{
DEADLINE, connect, insert_verified_user, random_key, register_key, setup, start_daemon_with,
};
use russh::client;
use server::db::install_test_pool;
use sqlx::PgPool;
use sqlx::types::Uuid;
use std::time::Duration;
use supp_macro::local_db_sqlx_test;
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio::net::TcpStream;
use tokio::time::{Instant, sleep, timeout};
const PRE_AUTH: Duration = Duration::from_millis(400);
/// Long enough for a login under heavy load; the test then waits it out.
const LOGIN_GRACE: Duration = Duration::from_secs(15);
const fn limits(max_startups: usize) -> AcceptLimits {
AcceptLimits {
max_startups,
pre_auth: PRE_AUTH,
}
/// Reads until the daemon closes the socket; returns what it sent.
async fn read_until_closed(socket: &mut TcpStream) -> anyhow::Result<Vec<u8>> {
let mut received = Vec::new();
timeout(DEADLINE, socket.read_to_end(&mut received)).await??;
Ok(received)
#[tokio::test]
async fn a_silent_connection_is_closed_at_the_deadline() -> anyhow::Result<()> {
let daemon = start_daemon_with(limits(8)).await?;
let mut socket = TcpStream::connect(daemon.addr).await?;
let started = Instant::now();
let banner = read_until_closed(&mut socket).await?;
assert!(banner.starts_with(b"SSH-2.0-"), "{banner:?}");
assert!(started.elapsed() >= PRE_AUTH, "{:?}", started.elapsed());
daemon.stop().await
async fn a_client_stalled_in_key_exchange_is_closed_at_the_deadline() -> anyhow::Result<()> {
socket.write_all(b"SSH-2.0-stalled\r\n").await?;
read_until_closed(&mut socket).await?;
async fn banner_of(addr: std::net::SocketAddr) -> anyhow::Result<[u8; 8]> {
let mut socket = TcpStream::connect(addr).await?;
let mut banner = [0u8; 8];
timeout(DEADLINE, socket.read_exact(&mut banner)).await??;
Ok(banner)
async fn a_timed_out_connection_gives_its_slot_back() -> anyhow::Result<()> {
let daemon = start_daemon_with(limits(1)).await?;
let mut first = TcpStream::connect(daemon.addr).await?;
read_until_closed(&mut first).await?;
assert_eq!(&banner_of(daemon.addr).await?, b"SSH-2.0-");
async fn a_handshake_without_login_is_disconnected_at_the_deadline() -> anyhow::Result<()> {
let session = client::connect(
Default::default(),
daemon.addr,
crate::eval_subsystem_tests::TrustingClient,
)
.await?;
let closed = async {
while !session.is_closed() {
sleep(Duration::from_millis(20)).await;
timeout(DEADLINE, closed).await?;
async fn a_connection_over_the_cap_is_dropped_before_the_banner() -> anyhow::Result<()> {
let daemon = start_daemon_with(AcceptLimits {
max_startups: 1,
pre_auth: DEADLINE,
})
first.read_exact(&mut banner).await?;
assert_eq!(&banner, b"SSH-2.0-");
let mut second = TcpStream::connect(daemon.addr).await?;
assert!(read_until_closed(&mut second).await?.is_empty());
#[local_db_sqlx_test]
async fn a_logged_in_session_outlives_the_deadline(pool: PgPool) -> anyhow::Result<()> {
let user_id = Uuid::new_v4();
insert_verified_user(&pool, user_id).await?;
let key = random_key()?;
register_key(user_id, &key).await?;
max_startups: 8,
pre_auth: LOGIN_GRACE,
let session = connect(daemon.addr, key, client::Config::default().window_size).await?;
tokio::time::sleep_until(Instant::now() + LOGIN_GRACE + Duration::from_millis(100)).await;
assert!(!session.is_closed());
session.channel_open_session().await?;
daemon.stop().await?;
async fn a_login_gives_its_slot_back(pool: PgPool) -> anyhow::Result<()> {