Lines
84.21 %
Functions
96.83 %
Branches
100 %
//! Client side of a TUI session over a real ssh connection, for the daemon's tests.
use crate::eval_subsystem_tests::TrustingClient;
use russh::client::{self, Handle};
use russh::keys::{PrivateKey, PrivateKeyWithHashAlg};
use russh::{Channel, ChannelMsg};
use std::net::SocketAddr;
use std::sync::Arc;
use std::time::Duration;
const STEP: Duration = Duration::from_secs(15);
pub(crate) struct TuiSession {
pub(crate) handle: Handle<TrustingClient>,
pub(crate) channel: Channel<client::Msg>,
seen: Vec<u8>,
}
impl TuiSession {
pub(crate) async fn open(
addr: SocketAddr,
key: PrivateKey,
(cols, rows): (u32, u32),
(width, height): (u32, u32),
) -> anyhow::Result<Self> {
let mut handle =
client::connect(Arc::new(client::Config::default()), addr, TrustingClient).await?;
let auth = handle
.authenticate_publickey("eval-e2e", PrivateKeyWithHashAlg::new(Arc::new(key), None))
.await?;
anyhow::ensure!(auth.success(), "publickey auth was refused");
let channel = handle.channel_open_session().await?;
channel
.request_pty(true, "xterm-kitty", cols, rows, width, height, &[])
channel.request_shell(true).await?;
Ok(Self {
handle,
channel,
seen: Vec::new(),
})
pub(crate) fn forget(&mut self) {
self.seen.clear();
pub(crate) fn seen(&self) -> String {
String::from_utf8_lossy(&self.seen).into_owned()
/// Reads the channel until `done` holds for what it has sent since `forget`.
pub(crate) async fn read_until(&mut self, done: impl Fn(&str) -> bool) -> anyhow::Result<()> {
let wait = async {
while !done(&self.seen()) {
match self.channel.wait().await {
Some(ChannelMsg::Data { data }) => self.seen.extend_from_slice(&data),
Some(_) => {}
None => anyhow::bail!("the channel closed"),
Ok(())
};
match tokio::time::timeout(STEP, wait).await {
Ok(result) => result,
Err(_) => anyhow::bail!("no progress: {:?}", self.tail()),
/// Reads until the daemon closes the channel; what it sent stays in `seen`.
pub(crate) async fn read_to_end(&mut self) -> anyhow::Result<()> {
while let Some(msg) = self.channel.wait().await {
if let ChannelMsg::Data { data } = msg {
self.seen.extend_from_slice(&data);
tokio::time::timeout(STEP, wait)
.await
.map_err(|_| anyhow::anyhow!("the channel stayed open: {:?}", self.tail()))
fn tail(&self) -> String {
let text = self.seen();
let skip = text.chars().count().saturating_sub(600);
text.chars().skip(skip).collect()