-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathrust_std.rs
More file actions
104 lines (91 loc) · 3.43 KB
/
rust_std.rs
File metadata and controls
104 lines (91 loc) · 3.43 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
mod test_utils;
use std::{
env::current_dir,
fs::{File, OpenOptions},
process::Stdio,
};
use fspy::AccessMode;
use test_log::test;
use test_utils::assert_contains;
#[test(tokio::test)]
async fn open_read() -> anyhow::Result<()> {
let accesses = track_fn!((), |(): ()| {
let _ = File::open("hello");
})
.await?;
assert_contains(&accesses, current_dir().unwrap().join("hello").as_path(), AccessMode::READ);
Ok(())
}
#[test(tokio::test)]
async fn open_write() -> anyhow::Result<()> {
let tmp_dir = tempfile::tempdir()?;
let tmp_path = tmp_dir.path().join("hello");
let tmp_path_str = tmp_path.to_str().unwrap().to_owned();
let accesses = track_fn!(tmp_path_str, |tmp_path_str: String| {
let _ = OpenOptions::new().write(true).open(tmp_path_str);
})
.await?;
assert_contains(&accesses, tmp_path.as_path(), AccessMode::WRITE);
Ok(())
}
#[test(tokio::test)]
async fn readdir() -> anyhow::Result<()> {
let tmpdir = tempfile::tempdir()?;
let tmpdir_path = std::fs::canonicalize(tmpdir.path())?;
// Reading a non-existent directory results in different tracked accesses on different platforms:
// - Windows: READ, because the NT APIs open the directory as handle just like files (NtCreateFile/NtOpenFile),
// and if that fails, not read dir call (NtQueryDirectoryFile/NtQueryDirectoryFileEx) is made.
// - macOS/Linux:
// - opendir results in a read_dir access. This call is directly made without trying to open the directory as a fd first.
// - open + fopendir results in READ access, because open would fail with ENOENT, and fopendir is not called.
//
// This difference is acceptable because both will result in a "not found" fingerprint in vite-task.
// To keep the test consistent across platforms, we create the directory first.
std::fs::create_dir(tmpdir.path().join("hello_dir"))?;
let accesses = track_fn!(tmpdir_path.to_str().unwrap().to_owned(), |tmpdir_path: String| {
std::env::set_current_dir(tmpdir_path).unwrap();
// Consume at least one entry so that getdents64 fires on seccomp targets.
let _ = std::fs::read_dir("hello_dir").unwrap().next();
})
.await?;
assert_contains(&accesses, tmpdir_path.join("hello_dir").as_path(), AccessMode::READ_DIR);
Ok(())
}
#[test(tokio::test)]
async fn read_in_subprocess() -> anyhow::Result<()> {
let accesses = track_fn!((), |(): ()| {
let mut command = if cfg!(windows) {
let mut command = std::process::Command::new("cmd");
command.arg("/c").arg("type hello");
command
} else {
let mut command = std::process::Command::new("/bin/sh");
command.arg("-c").arg("cat hello");
command
};
command
.stdout(Stdio::null())
.stdin(Stdio::null())
.stderr(Stdio::null())
.spawn()
.unwrap()
.wait()
.unwrap();
})
.await?;
assert_contains(&accesses, current_dir().unwrap().join("hello").as_path(), AccessMode::READ);
Ok(())
}
#[test(tokio::test)]
async fn read_program() -> anyhow::Result<()> {
let accesses = track_fn!((), |(): ()| {
let _ = std::process::Command::new("./not_exist.exe").spawn();
})
.await?;
assert_contains(
&accesses,
current_dir().unwrap().join("not_exist.exe").as_path(),
AccessMode::READ,
);
Ok(())
}