| // Copyright 2026 The Fuchsia Authors. All rights reserved. | |
| // Use of this source code is governed by a BSD-style license that can be | |
| // found in the LICENSE file. | |
| use ffx_e2e_emu::IsolatedEmulator; | |
| use pretty_assertions::assert_eq; | |
| use std::io::{Read as _, Write as _}; | |
| use std::path::Path; | |
| use std::process::{Command, Output, Stdio}; | |
| async fn run_command_with_input(mut cmd: Command, stdin_bytes: Option<&[u8]>) -> Output { | |
| cmd.stdin(if stdin_bytes.is_some() { Stdio::piped() } else { Stdio::null() }); | |
| cmd.stdout(Stdio::piped()); | |
| cmd.stderr(Stdio::piped()); | |
| let stdin_owned = stdin_bytes.map(|bytes| bytes.to_vec()); | |
| fuchsia_async::unblock(move || { | |
| let mut child = cmd.spawn().expect("spawn ffx command"); | |
| if let Some(bytes) = stdin_owned { | |
| let mut stdin = child.stdin.take().expect("piped stdin"); | |
| stdin.write_all(&bytes).expect("write stdin"); | |
| drop(stdin); | |
| } | |
| child.wait_with_output().expect("wait for ffx command") | |
| }) | |
| .await | |
| } | |
| fn strict_ffx_command( | |
| ffx_bin: &Path, | |
| log_path: &Path, | |
| ssh_priv: &str, | |
| target_addr: &str, | |
| args: &[&str], | |
| ) -> Command { | |
| let mut cmd = Command::new(ffx_bin); | |
| cmd.args([ | |
| "--strict", | |
| "--machine", | |
| "raw", | |
| "--log-output", | |
| log_path.to_str().expect("utf8 log path"), | |
| "-c", | |
| &format!("ssh.priv={ssh_priv}"), | |
| "--target", | |
| target_addr, | |
| ]); | |
| cmd.args(args); | |
| cmd | |
| } | |
| #[fuchsia::test(logging = true)] | |
| async fn shell_non_interactive_interactive_and_strict() { | |
| let emu = IsolatedEmulator::start("target-shell").await.expect("start emulator"); | |
| // 1. Separate stdout/stderr and no PTY CRLF translation. | |
| let cmd = emu | |
| .ffx_cmd_capture(&["target", "shell", "echo stdout_msg; echo stderr_msg >&2"]) | |
| .await | |
| .expect("build separate stdout/stderr command"); | |
| let output = run_command_with_input(cmd, None).await; | |
| assert_eq!(output.status.code(), Some(0)); | |
| assert_eq!(output.stdout, b"stdout_msg\n"); | |
| assert_eq!(output.stderr, b"stderr_msg\n"); | |
| // 2. Binary-safe stdin (including PTY control bytes 0x03/0x04 and NUL) and clean EOF. | |
| let binary_payload: &[u8] = b"\x00\x01\x02\x03\x04\r\n\xffbinary_ok\x00"; | |
| let cmd = | |
| emu.ffx_cmd_capture(&["target", "shell", "cat"]).await.expect("build binary cat command"); | |
| let output = run_command_with_input(cmd, Some(binary_payload)).await; | |
| assert_eq!(output.status.code(), Some(0)); | |
| assert_eq!(output.stdout.as_slice(), binary_payload); | |
| assert!(output.stderr.is_empty()); | |
| // 3. Non-zero exit codes (simple failure and explicit exit code with stderr). | |
| let cmd = | |
| emu.ffx_cmd_capture(&["target", "shell", "false"]).await.expect("build false command"); | |
| let output = run_command_with_input(cmd, None).await; | |
| assert_eq!(output.status.code(), Some(1)); | |
| let cmd = emu | |
| .ffx_cmd_capture(&["target", "shell", "echo err_out >&2; exit 42"]) | |
| .await | |
| .expect("build exit 42 command"); | |
| let output = run_command_with_input(cmd, None).await; | |
| assert_eq!(output.status.code(), Some(42)); | |
| assert!(output.stdout.is_empty()); | |
| assert_eq!(output.stderr, b"err_out\n"); | |
| // 4. Interactive mode fails cleanly when stdio is piped (non-TTY). | |
| let cmd = emu.ffx_cmd_capture(&["target", "shell"]).await.expect("build piped interactive cmd"); | |
| let output = run_command_with_input(cmd, None).await; | |
| assert!(!output.status.success()); | |
| let stderr_str = String::from_utf8_lossy(&output.stderr); | |
| assert!( | |
| stderr_str.contains("interactive mode does not support piping"), | |
| "unexpected piped interactive stderr: {stderr_str}" | |
| ); | |
| // 5. Interactive PTY execution over a host pseudoterminal. | |
| // TODO(https://fxbug.dev/588570200): `testrunner`'s Linux `nsjail` sandbox does not mount | |
| // `/dev/ptmx` or `/dev/pts`, causing `openpty` to fail with `ENOENT` in infra. Remove this | |
| // skip once host test sandboxes expose pseudoterminals. | |
| match nix::pty::openpty(None, None) { | |
| Ok(pty) => { | |
| let mut cmd = | |
| emu.ffx_cmd_capture(&["target", "shell"]).await.expect("build interactive pty cmd"); | |
| let slave_stdin = pty.slave.try_clone().expect("clone pty slave for stdin"); | |
| let slave_stdout = pty.slave.try_clone().expect("clone pty slave for stdout"); | |
| cmd.stdin(Stdio::from(slave_stdin)); | |
| cmd.stdout(Stdio::from(slave_stdout)); | |
| cmd.stderr(Stdio::from(pty.slave)); | |
| let (status, sent_commands, captured) = fuchsia_async::unblock(move || { | |
| let mut child = cmd.spawn().expect("spawn interactive target shell"); | |
| // Drop `cmd` immediately so the parent holds no copies of the PTY slave fd and | |
| // reads on `master` observe EOF once the child exits. | |
| drop(cmd); | |
| let mut master = std::fs::File::from(pty.master); | |
| let mut captured = Vec::new(); | |
| let mut buf = [0u8; 1024]; | |
| let mut sent_commands = false; | |
| loop { | |
| match master.read(&mut buf) { | |
| Ok(0) => break, | |
| Ok(n) => { | |
| captured.extend_from_slice(&buf[..n]); | |
| if !sent_commands && captured.windows(2).any(|window| window == b"$ ") { | |
| master | |
| .write_all(b"echo interactive_pty_$(echo executed)\nexit 0\n") | |
| .expect("write interactive commands to pty"); | |
| master.flush().expect("flush pty master"); | |
| sent_commands = true; | |
| } | |
| } | |
| Err(err) if err.kind() == std::io::ErrorKind::Interrupted => continue, | |
| Err(err) if err.raw_os_error() == Some(nix::errno::Errno::EIO as i32) => { | |
| break; | |
| } | |
| Err(err) => panic!("error reading pty master: {err}"), | |
| } | |
| } | |
| let status = child.wait().expect("wait for interactive target shell"); | |
| (status, sent_commands, captured) | |
| }) | |
| .await; | |
| let captured_str = String::from_utf8_lossy(&captured); | |
| assert!( | |
| sent_commands, | |
| "never observed shell prompt; captured output: {captured_str:?}" | |
| ); | |
| assert_eq!(status.code(), Some(0)); | |
| assert!( | |
| captured_str.contains("interactive_pty_executed"), | |
| "expected interactive command output, got: {captured_str:?}" | |
| ); | |
| } | |
| Err(nix::errno::Errno::ENOENT) => { | |
| eprintln!( | |
| "Skipping interactive PTY subtest: /dev/ptmx unavailable in sandbox \ | |
| (https://fxbug.dev/588570200)" | |
| ); | |
| } | |
| Err(err) => panic!("openpty: {err}"), | |
| } | |
| // 6. `--strict` mode execution against the emulator's direct address. | |
| let target_addr_output = emu | |
| .ffx_output(&["target", "list", "--format", "addresses", emu.emu_name()]) | |
| .await | |
| .expect("query emulator address"); | |
| let target_addr = | |
| target_addr_output.lines().next().expect("expected target address line").trim().to_string(); | |
| let ssh_priv: String = emu.env_context().get("ssh.priv").expect("read ssh.priv config"); | |
| let ffx_bin = emu.env_context().rerun_bin().expect("resolve rerun_bin"); | |
| let log_file = tempfile::NamedTempFile::new().expect("create temp log file"); | |
| let cmd = strict_ffx_command( | |
| &ffx_bin, | |
| log_file.path(), | |
| &ssh_priv, | |
| &target_addr, | |
| &["target", "shell", "echo strict_out; echo strict_err >&2"], | |
| ); | |
| let output = run_command_with_input(cmd, None).await; | |
| assert_eq!(output.status.code(), Some(0)); | |
| assert_eq!(output.stdout, b"strict_out\n"); | |
| assert_eq!(output.stderr, b"strict_err\n"); | |
| let cmd = strict_ffx_command( | |
| &ffx_bin, | |
| log_file.path(), | |
| &ssh_priv, | |
| &target_addr, | |
| &["target", "shell", "exit 23"], | |
| ); | |
| let output = run_command_with_input(cmd, None).await; | |
| assert_eq!(output.status.code(), Some(23)); | |
| } |