use alloc::vec::Vec;
use crate::base::channel::{Capped, Channel, ReadToEnd};
use crate::base::destination::build_nul_destination;
use crate::base::protocol::features::Feature;
use crate::base::session::Session;
use crate::error::Error;
use crate::shell::v2::Shell;
pub async fn open_exec<'a, S: Session>(
session: &'a mut S,
args: &[&str],
) -> Result<Channel<'a, S>, Error<S::IoError>> {
session.require_feature(Feature::AbbExec)?;
let dest = build_nul_destination("abb_exec:", args)?;
session.open(&dest).await
}
pub async fn exec<S: Session>(
session: &mut S,
args: &[&str],
rx: &mut [u8],
) -> Result<Vec<u8>, Error<S::IoError>> {
if rx.is_empty() {
return Err(Error::ReceiveBufferFull);
}
open_exec(session, args)
.await?
.read_to_vec(rx, usize::MAX)
.await?
.map_err(|Capped| Error::ReceiveBufferFull)
}
pub async fn open<'a, 'b, S: Session>(
session: &'a mut S,
args: &[&str],
rx: &'b mut [u8],
) -> Result<Shell<'a, 'b, S>, Error<S::IoError>> {
session.require_feature(Feature::Abb)?;
let dest = build_nul_destination("abb:", args)?;
let channel = session.open(&dest).await?;
Ok(Shell::new(channel, rx))
}
#[cfg(test)]
mod tests {
use super::*;
use crate::base::mock::{now, okay, one_slot};
#[test]
fn destination_abb_exec_multiple_args() {
let dest = build_nul_destination("abb_exec:", &["package", "list", "packages"]).unwrap();
assert_eq!(dest, b"abb_exec:package\0list\0packages\0");
}
#[test]
fn destination_abb_single_arg() {
let dest = build_nul_destination("abb:", &["connectivity"]).unwrap();
assert_eq!(dest, b"abb:connectivity\0");
}
#[test]
fn destination_no_args() {
let dest = build_nul_destination("abb_exec:", &[]).unwrap();
assert_eq!(dest, b"abb_exec:\0");
}
#[test]
fn destination_rejects_nul_in_arg() {
use crate::error::ProtocolError;
assert_eq!(
build_nul_destination("abb_exec:", &["bad\0arg"]),
Err(ProtocolError::InvalidDestination)
);
}
#[test]
fn destination_rejects_an_empty_argument() {
use crate::error::ProtocolError;
for args in [&["pm", "", "x"][..], &["", "x"], &["pm", ""], &[""]] {
assert_eq!(
build_nul_destination("abb:", args),
Err(ProtocolError::InvalidDestination),
"{args:?}"
);
}
}
#[test]
fn an_empty_argument_opens_nothing() {
use crate::error::ProtocolError::InvalidDestination;
let mut conn = one_slot("abb,abb_exec", &[]);
let mut rx = [0u8; 16];
let args = ["pm", "", "x"];
let exec = now(exec(&mut conn, &args, &mut rx));
assert!(matches!(exec, Err(Error::Protocol(InvalidDestination))));
let open_exec = now(open_exec(&mut conn, &args)).map(drop);
assert!(matches!(
open_exec,
Err(Error::Protocol(InvalidDestination))
));
let opened = now(open(&mut conn, &args, &mut rx)).map(drop);
assert!(matches!(opened, Err(Error::Protocol(InvalidDestination))));
assert_eq!(conn.transport().sent().len(), 1, "the CNXN alone");
}
#[test]
fn exec_with_an_empty_buffer_fails_before_anything_is_sent() {
let mut conn = one_slot("abb_exec", &[]);
let err = now(exec(&mut conn, &["package", "list"], &mut [])).unwrap_err();
assert!(matches!(err, Error::ReceiveBufferFull), "got {err:?}");
assert_eq!(conn.transport().sent().len(), 1, "the CNXN alone");
}
#[test]
fn exec_frees_its_slot_when_a_read_fails() {
let mut conn = one_slot("abb_exec", &[]);
let mut rx = [0u8; 16];
let err = now(exec(&mut conn, &["package", "list"], &mut rx)).unwrap_err();
assert!(matches!(err, Error::UnexpectedEof), "got {err:?}");
conn.transport_mut().feed(&okay(2));
now(conn.open_channel(b"shell:\0")).expect("the failed exec kept its slot");
}
}