#[cfg(test)]
#[cfg(all(unix, feature = "sandbox-microvm"))]
mod tests {
use super::super::common::*;
use std::time::{Duration, Instant};
#[test]
fn tty_hangup_relay_exits() {
let mut guest_cmd = std::process::Command::new("cat");
guest_cmd.arg("-u");
let relay =
crate::ui::pty_relay::PtyRelay::spawn(&mut guest_cmd).expect("spawn cat on PTY");
relay.disable_guest_echo();
let (tty_primary, tty_secondary) = open_pty_pair().expect("open fake tty PTY");
let relay_handle = std::thread::spawn(move || relay.relay_to_fd(tty_primary));
std::thread::sleep(Duration::from_millis(100));
drop(tty_secondary);
let deadline = Instant::now() + Duration::from_secs(5);
loop {
if relay_handle.is_finished() {
break;
}
if Instant::now() > deadline {
panic!("relay did not exit after tty hangup (spin/hang)");
}
std::thread::sleep(Duration::from_millis(50));
}
match relay_handle.join().expect("relay thread panicked") {
Ok(_status) => {} Err(e) => panic!("relay error after tty hangup: {e}"),
}
}
#[test]
fn dev_null_tty_relay_exits() {
let mut guest_cmd = std::process::Command::new("cat");
guest_cmd.arg("-u");
let relay =
crate::ui::pty_relay::PtyRelay::spawn(&mut guest_cmd).expect("spawn cat on PTY");
relay.disable_guest_echo();
let null_tty = std::fs::OpenOptions::new()
.read(true)
.write(true)
.open("/dev/null")
.expect("open /dev/null");
let relay_handle = std::thread::spawn(move || relay.relay_to_fd(null_tty));
let deadline = Instant::now() + Duration::from_secs(5);
loop {
if relay_handle.is_finished() {
break;
}
if Instant::now() > deadline {
panic!("relay did not exit with /dev/null tty (spin)");
}
std::thread::sleep(Duration::from_millis(50));
}
match relay_handle.join().expect("relay thread panicked") {
Ok(_status) => {}
Err(e) => panic!("relay error with /dev/null tty: {e}"),
}
}
}