|
| 1 | +#![cfg(target_os = "linux")] |
| 2 | + |
| 3 | +use std::{ |
| 4 | + fs::{self, Permissions}, |
| 5 | + os::unix::fs::PermissionsExt as _, |
| 6 | + path::{Path, PathBuf}, |
| 7 | + sync::LazyLock, |
| 8 | +}; |
| 9 | + |
| 10 | +use fspy::PathAccessIterable; |
| 11 | +use fspy_shared_unix::is_dynamically_linked_to_libc; |
| 12 | + |
| 13 | +use crate::test_utils::assert_contains; |
| 14 | + |
| 15 | +mod test_utils; |
| 16 | + |
| 17 | +const TEST_BIN_CONTENT: &[u8] = include_bytes!(env!("CARGO_BIN_FILE_FSPY_TEST_BIN")); |
| 18 | + |
| 19 | +fn test_bin_path() -> &'static Path { |
| 20 | + static TEST_BIN_PATH: LazyLock<PathBuf> = LazyLock::new(|| { |
| 21 | + assert_eq!( |
| 22 | + is_dynamically_linked_to_libc(&TEST_BIN_CONTENT), |
| 23 | + Ok(false), |
| 24 | + "Test binary is not a static executable" |
| 25 | + ); |
| 26 | + |
| 27 | + let tmp_dir = env!("CARGO_TARGET_TMPDIR"); |
| 28 | + let test_bin_path = PathBuf::from(tmp_dir).join("fspy-test-bin"); |
| 29 | + fs::write(&test_bin_path, TEST_BIN_CONTENT).expect("failed to write test binary"); |
| 30 | + fs::set_permissions(&test_bin_path, Permissions::from_mode(0o755)) |
| 31 | + .expect("failed to set permissions on test binary"); |
| 32 | + |
| 33 | + test_bin_path |
| 34 | + }); |
| 35 | + TEST_BIN_PATH.as_path() |
| 36 | +} |
| 37 | + |
| 38 | +async fn track_test_bin(args: &[&str]) -> PathAccessIterable { |
| 39 | + let mut cmd = fspy::Spy::global().unwrap().new_command(test_bin_path()); |
| 40 | + cmd.args(args); |
| 41 | + let mut tracked_child = cmd.spawn().await.unwrap(); |
| 42 | + |
| 43 | + let output = tracked_child.tokio_child.wait().await.unwrap(); |
| 44 | + assert!(output.success()); |
| 45 | + |
| 46 | + tracked_child.accesses_future.await.unwrap() |
| 47 | +} |
| 48 | + |
| 49 | +#[tokio::test] |
| 50 | +async fn open_read() { |
| 51 | + let accesses = track_test_bin(&["open_read", "/hello"]).await; |
| 52 | + assert_contains(&accesses, Path::new("/hello"), fspy::AccessMode::Read); |
| 53 | +} |
0 commit comments