From 1683829780f6b45da2695f7976f11d900057e6cf Mon Sep 17 00:00:00 2001 From: Johannes Leupolz Date: Sat, 13 Dec 2025 22:36:01 +0000 Subject: [PATCH] Fix bwrap-ipc test. --- vuinputd-tests/src/bin/bwrap-ipc.rs | 4 +++- vuinputd-tests/src/bwrap.rs | 10 +++++++--- vuinputd-tests/tests/integration_tests.rs | 16 ++++++++++++---- 3 files changed, 22 insertions(+), 8 deletions(-) diff --git a/vuinputd-tests/src/bin/bwrap-ipc.rs b/vuinputd-tests/src/bin/bwrap-ipc.rs index f1f2389..a965c5e 100644 --- a/vuinputd-tests/src/bin/bwrap-ipc.rs +++ b/vuinputd-tests/src/bin/bwrap-ipc.rs @@ -1,5 +1,5 @@ use core::panic; -use std::{str::from_utf8_unchecked, time::Duration}; +use std::{time::Duration}; use vuinputd_tests::bwrap::SandboxChildIpc; @@ -13,9 +13,11 @@ fn main() { let incoming_str = str::from_utf8(&incoming).expect("message received from ipc is not encoded as utf8"); if incoming_str == "continue" { + println!("child received continue"); ipc.send(b"ok").unwrap(); } else { ipc.send(b"nok").unwrap(); + println!("child received {}",incoming_str); panic!("expected ipc message to be 'continue'"); } } diff --git a/vuinputd-tests/src/bwrap.rs b/vuinputd-tests/src/bwrap.rs index f6feb83..ec0a91a 100644 --- a/vuinputd-tests/src/bwrap.rs +++ b/vuinputd-tests/src/bwrap.rs @@ -3,7 +3,7 @@ // Author: Johannes Leupolz use std::io; -use std::os::fd::{FromRawFd, IntoRawFd, RawFd}; +use std::os::fd::{FromRawFd, IntoRawFd, OwnedFd, RawFd}; use std::os::unix::net::UnixDatagram; use std::os::unix::process::CommandExt; use std::process::{Command, Output}; @@ -76,7 +76,7 @@ impl SandboxChildIpc { #[derive(Default)] pub struct BwrapBuilder { args: Vec, - ipc_child_fd: Option, + ipc_child_fd: Option, } impl BwrapBuilder { @@ -141,7 +141,7 @@ impl BwrapBuilder { let parent_sock = unsafe { UnixDatagram::from_raw_fd(parent.into_raw_fd()) }; // Child side must become FD 3 inside container - self.ipc_child_fd = Some(child.into_raw_fd()); + self.ipc_child_fd = Some(child); Ok((self, SandboxIpc { sock: parent_sock })) } @@ -158,7 +158,11 @@ impl BwrapBuilder { let mut cmd = Command::new("bwrap"); + if let Some(fd) = self.ipc_child_fd.take() { + // give up ownership of ipc_child_fd in host process. + let fd = fd.into_raw_fd(); + // Move child FD to 3. Note that the FD 3 needs to be linked at the // beginning of the child program. unsafe { diff --git a/vuinputd-tests/tests/integration_tests.rs b/vuinputd-tests/tests/integration_tests.rs index 06de7be..4f67444 100644 --- a/vuinputd-tests/tests/integration_tests.rs +++ b/vuinputd-tests/tests/integration_tests.rs @@ -36,19 +36,27 @@ fn test_bwrap_ipc() { .with_ipc() .expect("failed to create IPC"); + // Note that builder.run() will block. Thus, the send needs to happen before the child process blocks + // the host process. + ipc.send("continue".as_bytes()) + .unwrap_or_else(|e| panic!("failed to send data via ipc: {e}")); + let out = builder .command(bwrap_ipc) .run() .unwrap_or_else(|e| panic!("failed to run bwrap!: {e}")); - ipc.send("continue".as_bytes()) - .unwrap_or_else(|e| panic!("failed to send data via ipc: {e}")); - ipc.recv(Some(Duration::from_secs(5))) - .expect("error receiving input from ipc as host within 5 seconds"); + let result = ipc.recv(Some(Duration::from_secs(5))); println!("Output"); println!("stdout: {}", str::from_utf8(&out.stdout).unwrap()); println!("stderr: {}", str::from_utf8(&out.stderr).unwrap()); + + let result = result.expect("error receiving input from ipc as host within 5 seconds"); + let result_str = + str::from_utf8(&result).expect("message received from ipc is not encoded as utf8"); + println!("host received {}",result_str); + } #[cfg(all(feature = "requires-root", feature = "requires-uinput"))]