cat: remove unsafe from test code

This commit is contained in:
oech3
2026-04-27 16:23:04 +09:00
committed by Daniel Hofstetter
parent a1599dfad4
commit 0d67e58523
2 changed files with 13 additions and 18 deletions
+3
View File
@@ -37,6 +37,9 @@ divan = { workspace = true }
tempfile = { workspace = true }
uucore = { workspace = true, features = ["benchmark"] }
[target.'cfg(unix)'.dev-dependencies]
rustix = { workspace = true }
[[bin]]
name = "cat"
path = "src/main.rs"
+10 -18
View File
@@ -22,26 +22,18 @@ use uutests::util_name;
// Verify cat handles a broken pipe on stdout without hanging or crashing and exits nonzero
#[test]
fn test_cat_broken_pipe_nonzero_and_message() {
use std::fs::File;
use std::os::unix::io::FromRawFd;
use uutests::new_ucmd;
let (read, write) = rustix::pipe::pipe().expect("Failed to create pipe");
// Close the read end to simulate a broken pipe on stdout
drop(read);
let write: File = write.into();
let content = (0..10000).map(|_| "x").collect::<String>();
unsafe {
let mut fds: [libc::c_int; 2] = [0, 0];
assert_eq!(libc::pipe(fds.as_mut_ptr()), 0, "Failed to create pipe");
// Close the read end to simulate a broken pipe on stdout
let read_end = File::from_raw_fd(fds[0]);
// Explicitly drop the read-end so writers see EPIPE instead of blocking on a full pipe
drop(read_end);
let write_end = File::from_raw_fd(fds[1]);
let content = (0..10000).map(|_| "x").collect::<String>();
// On Unix, SIGPIPE should lead to a non-zero exit; ensure process exits and fails
new_ucmd!()
.set_stdout(write_end)
.pipe_in(content.as_bytes())
.fails();
}
// On Unix, SIGPIPE should lead to a non-zero exit; ensure process exits and fails
new_ucmd!()
.set_stdout(write)
.pipe_in(content.as_bytes())
.fails();
}
#[test]