-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathstatic_executable.rs
More file actions
111 lines (90 loc) · 3.46 KB
/
static_executable.rs
File metadata and controls
111 lines (90 loc) · 3.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#![cfg(target_os = "linux")]
use std::{
fs::{self, Permissions},
os::unix::fs::PermissionsExt as _,
path::{Path, PathBuf},
sync::LazyLock,
};
use fspy::PathAccessIterable;
use fspy_shared_unix::is_dynamically_linked_to_libc;
use test_log::test;
use crate::test_utils::assert_contains;
mod test_utils;
const TEST_BIN_CONTENT: &[u8] = include_bytes!(env!("CARGO_BIN_FILE_FSPY_TEST_BIN"));
fn test_bin_path() -> &'static Path {
static TEST_BIN_PATH: LazyLock<PathBuf> = LazyLock::new(|| {
assert_eq!(
is_dynamically_linked_to_libc(&TEST_BIN_CONTENT),
Ok(false),
"Test binary is not a static executable"
);
let tmp_dir = env!("CARGO_TARGET_TMPDIR");
let test_bin_path = PathBuf::from(tmp_dir).join("fspy-test-bin");
fs::write(&test_bin_path, TEST_BIN_CONTENT).expect("failed to write test binary");
fs::set_permissions(&test_bin_path, Permissions::from_mode(0o755))
.expect("failed to set permissions on test binary");
test_bin_path
});
TEST_BIN_PATH.as_path()
}
async fn track_test_bin(args: &[&str], cwd: Option<&str>) -> PathAccessIterable {
let mut cmd = fspy::Command::new(test_bin_path());
if let Some(cwd) = cwd {
cmd.current_dir(cwd);
};
cmd.args(args);
let tracked_child = cmd.spawn().await.unwrap();
let termination = tracked_child.wait_handle.await.unwrap();
assert!(termination.status.success());
termination.path_accesses
}
#[test(tokio::test)]
async fn open_read() {
let accesses = track_test_bin(&["open_read", "/hello"], None).await;
assert_contains(&accesses, Path::new("/hello"), fspy::AccessMode::Read);
}
#[test(tokio::test)]
async fn open_write() {
let accesses = track_test_bin(&["open_write", "/hello"], None).await;
assert_contains(&accesses, Path::new("/hello"), fspy::AccessMode::Write);
}
#[test(tokio::test)]
async fn open_readwrite() {
let accesses = track_test_bin(&["open_readwrite", "/hello"], None).await;
assert_contains(&accesses, Path::new("/hello"), fspy::AccessMode::ReadWrite);
}
#[test(tokio::test)]
async fn openat2_read() {
let accesses = track_test_bin(&["openat2_read", "/hello"], None).await;
assert_contains(&accesses, Path::new("/hello"), fspy::AccessMode::Read);
}
#[test(tokio::test)]
async fn openat2_write() {
let accesses = track_test_bin(&["openat2_write", "/hello"], None).await;
assert_contains(&accesses, Path::new("/hello"), fspy::AccessMode::Write);
}
#[test(tokio::test)]
async fn openat2_readwrite() {
let accesses = track_test_bin(&["openat2_readwrite", "/hello"], None).await;
assert_contains(&accesses, Path::new("/hello"), fspy::AccessMode::ReadWrite);
}
#[test(tokio::test)]
async fn open_relative() {
let accesses = track_test_bin(&["open_read", "hello"], Some("/home")).await;
assert_contains(&accesses, Path::new("/home/hello"), fspy::AccessMode::Read);
}
#[test(tokio::test)]
async fn readdir() {
let accesses = track_test_bin(&["readdir", "/home"], None).await;
assert_contains(&accesses, Path::new("/home"), fspy::AccessMode::ReadDir);
}
#[test(tokio::test)]
async fn stat() {
let accesses = track_test_bin(&["stat", "/hello"], None).await;
assert_contains(&accesses, Path::new("/hello"), fspy::AccessMode::Read);
}
#[test(tokio::test)]
async fn execve() {
let accesses = track_test_bin(&["execve", "/hello"], None).await;
assert_contains(&accesses, Path::new("/hello"), fspy::AccessMode::Read);
}