use core::future::Future;
use crate::base::channel::{Channel, ReadToEnd, SelectResult};
use crate::base::destination::{self, DestinationError};
use crate::base::session::Session;
use crate::error::Error;
fn write_destination(buf: &mut [u8], command: &str) -> Result<usize, DestinationError> {
destination::write_destination(buf, &[b"shell:", command.as_bytes()], &[])
}
pub struct Shell<'a, S> {
channel: Channel<'a, S>,
}
impl<'a, S: Session> Shell<'a, S> {
pub fn new(channel: Channel<'a, S>) -> Self {
Self { channel }
}
pub async fn read(&mut self, buf: &mut [u8]) -> Result<usize, Error<S::IoError>> {
self.channel.read(buf).await
}
pub async fn write_stdin(&mut self, data: &[u8]) -> Result<(), Error<S::IoError>> {
self.channel.write(data).await
}
pub async fn select<F: Future>(
&mut self,
buf: &mut [u8],
interrupt: F,
) -> Result<SelectResult<F::Output>, Error<S::IoError>> {
self.channel.select(buf, interrupt).await
}
pub async fn close(self) -> Result<(), Error<S::IoError>> {
self.channel.close().await
}
}
pub async fn open<'a, S: Session>(
session: &'a mut S,
command: &str,
dest_buf: &mut [u8],
) -> Result<Shell<'a, S>, Error<S::IoError>> {
let dest_len = write_destination(dest_buf, command)?;
let channel = session.open(&dest_buf[..dest_len]).await?;
Ok(Shell::new(channel))
}
pub async fn exec<S: Session>(
session: &mut S,
command: &str,
buf: &mut [u8],
) -> Result<usize, Error<S::IoError>> {
let dest_len = write_destination(buf, command)?;
let channel = session.open(&buf[..dest_len]).await?;
channel.read_to_end(buf).await
}
#[cfg(test)]
mod tests {
use super::*;
use crate::base::mock::{clse, now, okay, one_slot, wrte, MockError};
use crate::base::protocol::Packet;
fn exec_fails_then_slot_reopens(answer: &[Packet]) -> Error<MockError> {
let mut conn = one_slot("shell_v2", answer);
let mut buf = [0u8; 16];
let err = now(exec(&mut conn, "yes", &mut buf)).unwrap_err();
conn.transport_mut().feed(&okay(2));
now(conn.open_channel(b"shell:\0")).expect("the failed exec kept its slot");
err
}
#[test]
fn exec_returns_output_that_exactly_fills_the_buffer() {
let mut conn = one_slot("shell_v2", &[wrte(1, b"0123456789abcdef"), clse(1)]);
let mut buf = [0u8; 16];
let n = now(exec(&mut conn, "seq", &mut buf)).unwrap();
assert_eq!(&buf[..n], b"0123456789abcdef");
}
#[test]
fn exec_frees_its_slot_when_the_buffer_fills() {
let err = exec_fails_then_slot_reopens(&[wrte(1, &[b'y'; 16]), wrte(1, b"y")]);
assert!(matches!(err, Error::ReceiveBufferFull), "got {err:?}");
}
#[test]
fn exec_frees_its_slot_when_a_read_fails() {
let err = exec_fails_then_slot_reopens(&[]);
assert!(matches!(err, Error::UnexpectedEof), "got {err:?}");
}
#[test]
fn destination_empty_command() {
let mut buf = [0u8; 32];
let n = write_destination(&mut buf, "").unwrap();
assert_eq!(&buf[..n], b"shell:\0");
}
#[test]
fn destination_with_command() {
let mut buf = [0u8; 32];
let n = write_destination(&mut buf, "ls /sdcard").unwrap();
assert_eq!(&buf[..n], b"shell:ls /sdcard\0");
}
#[test]
fn destination_buffer_exact_fit() {
let mut buf = [0u8; 8];
let n = write_destination(&mut buf, "x").unwrap();
assert_eq!(&buf[..n], b"shell:x\0");
}
#[test]
fn destination_buffer_too_small() {
let mut buf = [0u8; 7];
assert_eq!(
write_destination(&mut buf, "x"),
Err(DestinationError::TooLong)
);
}
}