1
//! A TUI loop that panics still gives the client its terminal back and closes the channel.
2

            
3
use crate::eval_subsystem_tests::random_key;
4
use crate::tui_channel::Geometry;
5
use crate::tui_session::TuiSession;
6
use crate::tui_transport::SshTransport;
7
use russh::keys::PrivateKey;
8
use russh::server::{Auth, ChannelOpenHandle, Handler, Server, Session, VerifiedPublicKey};
9
use russh::{Channel, ChannelId, Pty};
10
use std::net::SocketAddr;
11
use std::sync::Arc;
12
use std::time::Duration;
13
use tokio::net::TcpListener;
14
use tokio::sync::{mpsc, watch};
15
use tui::transport::MOUSE_DISABLE;
16

            
17
const DEADLINE: Duration = Duration::from_secs(45);
18

            
19
#[derive(Clone)]
20
struct PanickingDaemon;
21

            
22
struct PanickingSession;
23

            
24
impl Server for PanickingDaemon {
25
    type Handler = PanickingSession;
26

            
27
1
    fn new_client(&mut self, _peer: Option<SocketAddr>) -> PanickingSession {
28
1
        PanickingSession
29
1
    }
30
}
31

            
32
impl Handler for PanickingSession {
33
    type Error = russh::Error;
34

            
35
1
    async fn auth_publickey(
36
1
        &mut self,
37
1
        _user: &str,
38
1
        _key: VerifiedPublicKey<'_>,
39
1
    ) -> Result<Auth, Self::Error> {
40
1
        Ok(Auth::Accept)
41
1
    }
42

            
43
1
    async fn channel_open_session(
44
1
        &mut self,
45
1
        _channel: Channel<russh::server::Msg>,
46
1
        reply: ChannelOpenHandle,
47
1
        _session: &mut Session,
48
1
    ) -> Result<(), Self::Error> {
49
1
        reply.accept().await;
50
1
        Ok(())
51
1
    }
52

            
53
1
    async fn pty_request(
54
1
        &mut self,
55
1
        _channel: ChannelId,
56
1
        _term: &str,
57
1
        _cols: u32,
58
1
        _rows: u32,
59
1
        _pix_width: u32,
60
1
        _pix_height: u32,
61
1
        _modes: &[(Pty, u32)],
62
1
        _session: &mut Session,
63
1
    ) -> Result<(), Self::Error> {
64
1
        Ok(())
65
1
    }
66

            
67
1
    async fn shell_request(
68
1
        &mut self,
69
1
        channel: ChannelId,
70
1
        session: &mut Session,
71
1
    ) -> Result<(), Self::Error> {
72
1
        let handle = session.handle();
73
1
        let runtime = tokio::runtime::Handle::current();
74
1
        tokio::task::spawn_blocking(move || {
75
1
            let (_resize, geometry) = watch::channel(Geometry::default());
76
1
            let (_input, inputs) = mpsc::channel(1);
77
1
            let _transport = SshTransport::new(handle, channel, runtime, inputs, None, geometry)
78
1
                .expect("the transport builds");
79
1
            panic!("the loop panicked");
80
        });
81
1
        Ok(())
82
1
    }
83
}
84

            
85
1
async fn serve(key: PrivateKey) -> anyhow::Result<(SocketAddr, tokio::task::JoinHandle<()>)> {
86
1
    let listener = TcpListener::bind("127.0.0.1:0").await?;
87
1
    let addr = listener.local_addr()?;
88
1
    let config = Arc::new(russh::server::Config {
89
1
        keys: vec![key],
90
1
        ..russh::server::Config::default()
91
1
    });
92
1
    let task = tokio::spawn(async move {
93
1
        let mut daemon = PanickingDaemon;
94
1
        let _ = daemon.run_on_socket(config, &listener).await;
95
    });
96
1
    Ok((addr, task))
97
1
}
98

            
99
1
async fn session_after_panic(addr: SocketAddr) -> anyhow::Result<String> {
100
1
    let mut session = TuiSession::open(addr, random_key()?, (80, 24), (0, 0)).await?;
101
1
    session.read_to_end().await?;
102
1
    Ok(session.seen())
103
1
}
104

            
105
#[tokio::test]
106
1
async fn a_panicking_loop_still_disables_the_mouse_and_closes_the_channel() -> anyhow::Result<()> {
107
1
    let (addr, task) = serve(random_key()?).await?;
108
1
    let outcome = tokio::time::timeout(DEADLINE, session_after_panic(addr)).await;
109
1
    task.abort();
110
1
    let seen = outcome??;
111
1
    let disable = String::from_utf8_lossy(MOUSE_DISABLE).into_owned();
112
1
    assert!(seen.contains(&disable), "{seen:?}");
113
2
    Ok(())
114
1
}