Lines
98.65 %
Functions
100 %
Branches
//! A TUI loop that panics still gives the client its terminal back and closes the channel.
use crate::eval_subsystem_tests::random_key;
use crate::tui_channel::Geometry;
use crate::tui_session::TuiSession;
use crate::tui_transport::SshTransport;
use russh::keys::PrivateKey;
use russh::server::{Auth, ChannelOpenHandle, Handler, Server, Session, VerifiedPublicKey};
use russh::{Channel, ChannelId, Pty};
use std::net::SocketAddr;
use std::sync::Arc;
use std::time::Duration;
use tokio::net::TcpListener;
use tokio::sync::{mpsc, watch};
use tui::transport::MOUSE_DISABLE;
const DEADLINE: Duration = Duration::from_secs(45);
#[derive(Clone)]
struct PanickingDaemon;
struct PanickingSession;
impl Server for PanickingDaemon {
type Handler = PanickingSession;
fn new_client(&mut self, _peer: Option<SocketAddr>) -> PanickingSession {
PanickingSession
}
impl Handler for PanickingSession {
type Error = russh::Error;
async fn auth_publickey(
&mut self,
_user: &str,
_key: VerifiedPublicKey<'_>,
) -> Result<Auth, Self::Error> {
Ok(Auth::Accept)
async fn channel_open_session(
_channel: Channel<russh::server::Msg>,
reply: ChannelOpenHandle,
_session: &mut Session,
) -> Result<(), Self::Error> {
reply.accept().await;
Ok(())
async fn pty_request(
_channel: ChannelId,
_term: &str,
_cols: u32,
_rows: u32,
_pix_width: u32,
_pix_height: u32,
_modes: &[(Pty, u32)],
async fn shell_request(
channel: ChannelId,
session: &mut Session,
let handle = session.handle();
let runtime = tokio::runtime::Handle::current();
tokio::task::spawn_blocking(move || {
let (_resize, geometry) = watch::channel(Geometry::default());
let (_input, inputs) = mpsc::channel(1);
let _transport = SshTransport::new(handle, channel, runtime, inputs, None, geometry)
.expect("the transport builds");
panic!("the loop panicked");
});
async fn serve(key: PrivateKey) -> anyhow::Result<(SocketAddr, tokio::task::JoinHandle<()>)> {
let listener = TcpListener::bind("127.0.0.1:0").await?;
let addr = listener.local_addr()?;
let config = Arc::new(russh::server::Config {
keys: vec![key],
..russh::server::Config::default()
let task = tokio::spawn(async move {
let mut daemon = PanickingDaemon;
let _ = daemon.run_on_socket(config, &listener).await;
Ok((addr, task))
async fn session_after_panic(addr: SocketAddr) -> anyhow::Result<String> {
let mut session = TuiSession::open(addr, random_key()?, (80, 24), (0, 0)).await?;
session.read_to_end().await?;
Ok(session.seen())
#[tokio::test]
async fn a_panicking_loop_still_disables_the_mouse_and_closes_the_channel() -> anyhow::Result<()> {
let (addr, task) = serve(random_key()?).await?;
let outcome = tokio::time::timeout(DEADLINE, session_after_panic(addr)).await;
task.abort();
let seen = outcome??;
let disable = String::from_utf8_lossy(MOUSE_DISABLE).into_owned();
assert!(seen.contains(&disable), "{seen:?}");