| 1 |
// This file is part of the uutils coreutils package. |
| 2 |
// |
| 3 |
// For the full copyright and license information, please view the LICENSE |
| 4 |
// file that was distributed with this source code. |
| 5 |
|
| 6 |
// spell-checker:ignore cmdline dyld dylib PDEATHSIG setvbuf ppid |
| 7 |
|
| 8 |
#[cfg(target_os = "linux")] |
| 9 |
use uutests::at_and_ucmd; |
| 10 |
#[cfg(unix)] |
| 11 |
use uutests::{new_ucmd, util::TestScenario, util_name}; |
| 12 |
|
| 13 |
#[test] |
| 14 |
#[cfg(unix)] |
| 15 |
fn invalid_input() { |
| 16 |
new_ucmd!().arg("-/").fails_with_code(125); |
| 17 |
} |
| 18 |
|
| 19 |
// linux-gated to match the `at_and_ucmd` import above; the check itself is not |
| 20 |
// platform-specific. |
| 21 |
#[cfg(all(target_os = "linux", not(feature = "feat_external_libstdbuf")))] |
| 22 |
#[test] |
| 23 |
fn test_tmpdir_with_colon_is_rejected() { |
| 24 |
// The preload variable is a colon-separated list with no escaping, so a |
| 25 |
// libstdbuf path containing ':' would be split by the loader and its leading |
| 26 |
// component loaded as a library of its own. With $TMPDIR under an attacker's |
| 27 |
// control that component is attacker-chosen, so refuse instead. |
| 28 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 29 |
at.mkdir("evil.so:x"); |
| 30 |
|
| 31 |
ucmd.env("TMPDIR", at.plus("evil.so:x")) |
| 32 |
.arg("-o0") |
| 33 |
.arg("true") |
| 34 |
.fails_with_code(125) |
| 35 |
.stderr_contains("contains ':'"); |
| 36 |
} |
| 37 |
|
| 38 |
#[cfg(all(unix, not(feature = "feat_external_libstdbuf")))] |
| 39 |
#[test] |
| 40 |
fn test_permission() { |
| 41 |
new_ucmd!() |
| 42 |
.arg("-o1") |
| 43 |
.arg(".") |
| 44 |
.fails_with_code(126) |
| 45 |
.stderr_contains("Permission denied"); |
| 46 |
} |
| 47 |
|
| 48 |
// LD_DEBUG is not available on macOS, OpenBSD, Android, or musl |
| 49 |
#[cfg(all( |
| 50 |
feature = "feat_external_libstdbuf", |
| 51 |
unix, |
| 52 |
not(target_vendor = "apple"), |
| 53 |
not(target_os = "openbsd"), |
| 54 |
not(target_os = "android"), |
| 55 |
not(target_env = "musl") |
| 56 |
))] |
| 57 |
#[test] |
| 58 |
fn test_stdbuf_search_order_exe_dir_first() { |
| 59 |
use std::fs; |
| 60 |
use std::os::unix::fs::PermissionsExt; |
| 61 |
|
| 62 |
// Test that stdbuf searches for libstdbuf in its own directory first, |
| 63 |
// before checking LIBSTDBUF_DIR. |
| 64 |
let ts = TestScenario::new(util_name!()); |
| 65 |
let temp_dir = tempfile::tempdir().unwrap(); |
| 66 |
let temp_path = temp_dir.path(); |
| 67 |
|
| 68 |
// Determine the correct library extension for this platform |
| 69 |
let lib_extension = if cfg!(target_vendor = "apple") { |
| 70 |
"dylib" |
| 71 |
} else { |
| 72 |
"so" |
| 73 |
}; |
| 74 |
let lib_name = format!("libstdbuf.{lib_extension}"); |
| 75 |
|
| 76 |
// Look for libstdbuf in the build directory deps folder |
| 77 |
// During build, libstdbuf.so is in target/debug/deps/ or target/release/deps/ |
| 78 |
// This allows running tests without requiring installation to a root-owned path |
| 79 |
// ts.bin_path is the path to the binary file, so we get its parent directory first |
| 80 |
let source_lib = ts |
| 81 |
.bin_path |
| 82 |
.parent() |
| 83 |
.expect("Binary should have a parent directory") |
| 84 |
.join("deps") |
| 85 |
.join(&lib_name); |
| 86 |
|
| 87 |
// Fail test if the library doesn't exist - it should have been built |
| 88 |
assert!( |
| 89 |
source_lib.exists(), |
| 90 |
"libstdbuf not found at {}. It should have been built.", |
| 91 |
source_lib.display() |
| 92 |
); |
| 93 |
|
| 94 |
// Copy stdbuf binary to temp directory |
| 95 |
// ts.bin_path is the full path to the coreutils binary |
| 96 |
let stdbuf_copy = temp_path.join("stdbuf"); |
| 97 |
fs::copy(&ts.bin_path, &stdbuf_copy).unwrap(); |
| 98 |
|
| 99 |
// Make the copied binary executable |
| 100 |
let mut perms = fs::metadata(&stdbuf_copy).unwrap().permissions(); |
| 101 |
perms.set_mode(0o755); |
| 102 |
fs::set_permissions(&stdbuf_copy, perms).unwrap(); |
| 103 |
|
| 104 |
// Copy libstdbuf to the same directory as stdbuf |
| 105 |
let lib_copy = temp_path.join(&lib_name); |
| 106 |
fs::copy(&source_lib, &lib_copy).unwrap(); |
| 107 |
|
| 108 |
// Run the copied stdbuf with LD_DEBUG to verify it loads the local libstdbuf |
| 109 |
// This proves the exe-dir search happens first, before checking LIBSTDBUF_DIR |
| 110 |
let output = std::process::Command::new(&stdbuf_copy) |
| 111 |
.env("LD_DEBUG", "libs") |
| 112 |
.args(["-o0", "echo", "test_output"]) |
| 113 |
.output() |
| 114 |
.unwrap(); |
| 115 |
|
| 116 |
let stderr = String::from_utf8_lossy(&output.stderr); |
| 117 |
|
| 118 |
// Verify the library was loaded from the temp directory (same dir as exe) |
| 119 |
// LD_DEBUG output will show something like: |
| 120 |
// " trying file=/tmp/.../libstdbuf.so" |
| 121 |
let temp_dir_str = temp_path.to_string_lossy(); |
| 122 |
let loaded_from_exe_dir = stderr |
| 123 |
.lines() |
| 124 |
.any(|line| line.contains(&*lib_name) && line.contains(&*temp_dir_str)); |
| 125 |
|
| 126 |
assert!( |
| 127 |
loaded_from_exe_dir, |
| 128 |
"libstdbuf should be loaded from exe directory ({}), not from LIBSTDBUF_DIR. LD_DEBUG output:\n{stderr}", |
| 129 |
temp_path.display() |
| 130 |
); |
| 131 |
|
| 132 |
// The command should succeed and produce the expected output |
| 133 |
assert!( |
| 134 |
output.status.success(), |
| 135 |
"stdbuf should succeed when libstdbuf is in the same directory. stderr: {stderr}" |
| 136 |
); |
| 137 |
assert_eq!( |
| 138 |
String::from_utf8_lossy(&output.stdout).trim(), |
| 139 |
"test_output", |
| 140 |
"stdbuf should execute echo successfully" |
| 141 |
); |
| 142 |
} |
| 143 |
|
| 144 |
#[cfg(all(unix, not(feature = "feat_external_libstdbuf")))] |
| 145 |
#[test] |
| 146 |
fn test_no_such() { |
| 147 |
new_ucmd!() |
| 148 |
.arg("-o1") |
| 149 |
.arg("no_such") |
| 150 |
.fails_with_code(127) |
| 151 |
.stderr_contains("No such file or directory"); |
| 152 |
} |
| 153 |
|
| 154 |
// Disabled on x86_64-unknown-linux-musl because the cross-rs Docker image for this target |
| 155 |
// does not provide musl-compiled system utilities (like head), leading to dynamic linker errors |
| 156 |
// when preloading musl-compiled libstdbuf.so into glibc-compiled binaries. Same thing for FreeBSD. |
| 157 |
#[cfg(all( |
| 158 |
unix, |
| 159 |
not(target_os = "freebsd"), |
| 160 |
not(target_os = "openbsd"), |
| 161 |
not(all(target_arch = "x86_64", target_env = "musl")) |
| 162 |
))] |
| 163 |
#[test] |
| 164 |
fn test_stdbuf_unbuffered_stdout() { |
| 165 |
// This is a basic smoke test |
| 166 |
// Note: This test only verifies that stdbuf does not crash and that output is passed through as expected |
| 167 |
// for simple, short-lived commands. It does not guarantee that buffering is actually modified or that |
| 168 |
// libstdbuf is loaded and functioning correctly. |
| 169 |
new_ucmd!() |
| 170 |
.args(&["-o0", "head"]) |
| 171 |
.pipe_in("The quick brown fox jumps over the lazy dog.") |
| 172 |
.succeeds() |
| 173 |
.stdout_is("The quick brown fox jumps over the lazy dog."); |
| 174 |
} |
| 175 |
|
| 176 |
// Disabled on x86_64-unknown-linux-musl because the cross-rs Docker image for this target |
| 177 |
// does not provide musl-compiled system utilities (like head), leading to dynamic linker errors |
| 178 |
// when preloading musl-compiled libstdbuf.so into glibc-compiled binaries. Same thing for FreeBSD. |
| 179 |
#[cfg(all( |
| 180 |
unix, |
| 181 |
not(target_os = "freebsd"), |
| 182 |
not(target_os = "openbsd"), |
| 183 |
not(all(target_arch = "x86_64", target_env = "musl")) |
| 184 |
))] |
| 185 |
#[test] |
| 186 |
fn test_stdbuf_line_buffered_stdout() { |
| 187 |
// Note: This test only verifies that stdbuf does not crash and that output is passed through as expected |
| 188 |
// for simple, short-lived commands. It does not guarantee that buffering is actually modified or that |
| 189 |
// libstdbuf is loaded and functioning correctly. |
| 190 |
new_ucmd!() |
| 191 |
.args(&["-oL", "head"]) |
| 192 |
.pipe_in("The quick brown fox jumps over the lazy dog.") |
| 193 |
.succeeds() |
| 194 |
.stdout_is("The quick brown fox jumps over the lazy dog."); |
| 195 |
} |
| 196 |
|
| 197 |
#[test] |
| 198 |
#[cfg(unix)] |
| 199 |
fn test_stdbuf_no_buffer_option_fails() { |
| 200 |
let ts = TestScenario::new(util_name!()); |
| 201 |
|
| 202 |
ts.ucmd() |
| 203 |
.args(&["head"]) |
| 204 |
.fails() |
| 205 |
.stderr_contains("the following required arguments were not provided:"); |
| 206 |
} |
| 207 |
|
| 208 |
#[test] |
| 209 |
#[cfg(unix)] |
| 210 |
fn test_stdbuf_no_command_fails_with_125() { |
| 211 |
// Test that missing command fails with exit code 125 (stdbuf error) |
| 212 |
// This verifies proper error handling without unwrap panic |
| 213 |
new_ucmd!() |
| 214 |
.args(&["-o1"]) |
| 215 |
.fails_with_code(125) |
| 216 |
.stderr_contains("the following required arguments were not provided:"); |
| 217 |
} |
| 218 |
|
| 219 |
// Disabled on x86_64-unknown-linux-musl because the cross-rs Docker image for this target |
| 220 |
// does not provide musl-compiled system utilities (like tail), leading to dynamic linker errors |
| 221 |
// when preloading musl-compiled libstdbuf.so into glibc-compiled binaries. Same thing for FreeBSD. |
| 222 |
#[cfg(all( |
| 223 |
unix, |
| 224 |
not(target_os = "freebsd"), |
| 225 |
not(target_os = "openbsd"), |
| 226 |
not(all(target_arch = "x86_64", target_env = "musl")) |
| 227 |
))] |
| 228 |
#[test] |
| 229 |
fn test_stdbuf_trailing_var_arg() { |
| 230 |
new_ucmd!() |
| 231 |
.args(&["-i", "1024", "tail", "-1"]) |
| 232 |
.pipe_in("The quick brown fox\njumps over the lazy dog.") |
| 233 |
.succeeds() |
| 234 |
.stdout_is("jumps over the lazy dog."); |
| 235 |
} |
| 236 |
|
| 237 |
#[test] |
| 238 |
#[cfg(unix)] |
| 239 |
fn test_stdbuf_line_buffering_stdin_fails() { |
| 240 |
new_ucmd!() |
| 241 |
.args(&["-i", "L", "head"]) |
| 242 |
.fails() |
| 243 |
.usage_error("line buffering stdin is meaningless"); |
| 244 |
} |
| 245 |
|
| 246 |
#[test] |
| 247 |
#[cfg(unix)] |
| 248 |
fn test_stdbuf_invalid_mode_fails() { |
| 249 |
let options = ["--input", "--output", "--error"]; |
| 250 |
for option in &options { |
| 251 |
new_ucmd!() |
| 252 |
.args(&[*option, "1024R", "head"]) |
| 253 |
.fails_with_code(125) |
| 254 |
.usage_error("invalid mode '1024R': Value too large for defined data type"); |
| 255 |
new_ucmd!() |
| 256 |
.args(&[*option, "1Y", "head"]) |
| 257 |
.fails_with_code(125) |
| 258 |
.stderr_contains("stdbuf: invalid mode '1Y': Value too large for defined data type"); |
| 259 |
#[cfg(target_pointer_width = "32")] |
| 260 |
{ |
| 261 |
new_ucmd!() |
| 262 |
.args(&[*option, "5GB", "head"]) |
| 263 |
.fails_with_code(125) |
| 264 |
.stderr_contains( |
| 265 |
"stdbuf: invalid mode '5GB': Value too large for defined data type", |
| 266 |
); |
| 267 |
} |
| 268 |
} |
| 269 |
} |
| 270 |
|
| 271 |
// macos uses DYLD_PRINT_LIBRARIES, not LD_DEBUG, so disable on macos at the moment. |
| 272 |
// On modern Android (Bionic, API 37+), LD_DEBUG is supported and behaves similarly to glibc. |
| 273 |
// On older Android versions (Bionic, API < 37), LD_DEBUG uses integer values instead of strings |
| 274 |
// and is sometimes disabled. Disable test on Android for now. |
| 275 |
// musl libc dynamic loader does not support LD_DEBUG, so disable on musl targets as well. |
| 276 |
#[cfg(all( |
| 277 |
unix, |
| 278 |
not(target_vendor = "apple"), |
| 279 |
not(target_os = "openbsd"), |
| 280 |
not(target_os = "android"), |
| 281 |
not(target_env = "musl") |
| 282 |
))] |
| 283 |
#[test] |
| 284 |
fn test_libstdbuf_preload() { |
| 285 |
use std::process::Command; |
| 286 |
|
| 287 |
// Run a simple program with LD_DEBUG=symbols to verify that libstdbuf is loaded correctly |
| 288 |
// and that there are no architecture mismatches when preloading the library. |
| 289 |
// Note: This does not check which setvbuf implementation is used, as our libstdbuf does not override setvbuf. |
| 290 |
// for https://github.com/uutils/coreutils/issues/6591 |
| 291 |
|
| 292 |
let scene = TestScenario::new(util_name!()); |
| 293 |
let coreutils_bin = &scene.bin_path; |
| 294 |
|
| 295 |
// Test with our own echo (should have the correct architecture even when cross-compiled using cross-rs, |
| 296 |
// in which case the "system" echo will be the host architecture) |
| 297 |
let uutils_echo_cmd = format!( |
| 298 |
"LD_DEBUG=symbols {} stdbuf -oL {} echo test 2>&1", |
| 299 |
coreutils_bin.display(), |
| 300 |
coreutils_bin.display() |
| 301 |
); |
| 302 |
let uutils_output = Command::new("sh") |
| 303 |
.arg("-c") |
| 304 |
.arg(&uutils_echo_cmd) |
| 305 |
.output() |
| 306 |
.expect("Failed to run uutils echo test"); |
| 307 |
|
| 308 |
let uutils_debug = String::from_utf8_lossy(&uutils_output.stdout); |
| 309 |
|
| 310 |
// Check if libstdbuf.so / libstdbuf.dylib is in the lookup path. |
| 311 |
// With GLIBC, the log should contain something like: |
| 312 |
// "symbol=setvbuf; lookup in file=/tmp/.tmp0mfmCg/libstdbuf.so [0]" |
| 313 |
// With FreeBSD dynamic loader, the log should contain something like: |
| 314 |
// cspell:disable-next-line |
| 315 |
// "calling init function for /tmp/.tmpu11rhP/libstdbuf.so at ..." |
| 316 |
let libstdbuf_in_path = if cfg!(target_os = "freebsd") { |
| 317 |
uutils_debug |
| 318 |
.lines() |
| 319 |
.any(|line| line.contains("calling init function") && line.contains("libstdbuf")) |
| 320 |
} else { |
| 321 |
uutils_debug.contains("symbol=setvbuf") |
| 322 |
&& uutils_debug.contains("lookup in file=") |
| 323 |
&& uutils_debug.contains("libstdbuf") |
| 324 |
}; |
| 325 |
|
| 326 |
// Check for lack of architecture mismatch error. The potential error message with GLIBC is: |
| 327 |
// cspell:disable-next-line |
| 328 |
// "ERROR: ld.so: object '/tmp/.tmpCLq8jl/libstdbuf.so' from LD_PRELOAD cannot be preloaded (cannot open shared object file): ignored." |
| 329 |
let arch_mismatch_line = uutils_debug |
| 330 |
.lines() |
| 331 |
.find(|line| line.contains("cannot be preloaded")); |
| 332 |
println!("LD_DEBUG output: {uutils_debug}"); |
| 333 |
let no_arch_mismatch = arch_mismatch_line.is_none(); |
| 334 |
|
| 335 |
println!("libstdbuf in lookup path: {libstdbuf_in_path}"); |
| 336 |
println!("No architecture mismatch: {no_arch_mismatch}"); |
| 337 |
if let Some(error_line) = arch_mismatch_line { |
| 338 |
println!("Architecture mismatch error: {error_line}"); |
| 339 |
} |
| 340 |
|
| 341 |
assert!( |
| 342 |
libstdbuf_in_path, |
| 343 |
"libstdbuf should be in lookup path with uutils echo" |
| 344 |
); |
| 345 |
assert!( |
| 346 |
no_arch_mismatch, |
| 347 |
"uutils echo should not show architecture mismatch" |
| 348 |
); |
| 349 |
} |
| 350 |
|
| 351 |
#[cfg(target_os = "linux")] |
| 352 |
#[cfg(not(target_env = "musl"))] |
| 353 |
#[test] |
| 354 |
fn test_stdbuf_non_utf8_paths() { |
| 355 |
use std::os::unix::ffi::OsStringExt; |
| 356 |
|
| 357 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 358 |
|
| 359 |
let filename = std::ffi::OsString::from_vec(vec![0xFF, 0xFE]); |
| 360 |
std::fs::write(at.plus(&filename), b"test content for stdbuf\n").unwrap(); |
| 361 |
|
| 362 |
ucmd.arg("-o0") |
| 363 |
.arg("cat") |
| 364 |
.arg(&filename) |
| 365 |
.succeeds() |
| 366 |
.stdout_is("test content for stdbuf\n"); |
| 367 |
} |
| 368 |
|
| 369 |
// stdbuf uses spawn()+wait() (not exec()) so that the TempDir holding |
| 370 |
// libstdbuf.so is cleaned up after the child exits. The stdbuf process |
| 371 |
// itself therefore stays in the process table as a thin waiter; the child |
| 372 |
// immediately execs the requested command. |
| 373 |
// See: https://github.com/uutils/coreutils/issues/13939 |
| 374 |
#[test] |
| 375 |
#[cfg(target_os = "linux")] |
| 376 |
fn test_stdbuf_child_execs_command() { |
| 377 |
use std::process::{Command, Stdio}; |
| 378 |
use std::thread; |
| 379 |
use std::time::Duration; |
| 380 |
|
| 381 |
let scene = TestScenario::new(util_name!()); |
| 382 |
|
| 383 |
// Start stdbuf with a long-running command |
| 384 |
let mut parent = Command::new(&scene.bin_path) |
| 385 |
.args(["stdbuf", "-o0", "sleep", "5"]) |
| 386 |
.stdout(Stdio::null()) |
| 387 |
.stderr(Stdio::null()) |
| 388 |
.spawn() |
| 389 |
.expect("Failed to start stdbuf"); |
| 390 |
|
| 391 |
let parent_pid = parent.id(); |
| 392 |
|
| 393 |
// Poll until the child process appears or timeout |
| 394 |
let timeout = Duration::from_secs(3); |
| 395 |
let poll_interval = Duration::from_millis(10); |
| 396 |
let start_time = std::time::Instant::now(); |
| 397 |
|
| 398 |
let child_comm = loop { |
| 399 |
if start_time.elapsed() > timeout { |
| 400 |
parent.kill().ok(); |
| 401 |
panic!("TIMEOUT: child of {parent_pid} did not appear within {timeout:?}"); |
| 402 |
} |
| 403 |
|
| 404 |
// Find children of our stdbuf process |
| 405 |
let found = std::fs::read_dir("/proc").ok().and_then(|entries| { |
| 406 |
entries.flatten().find_map(|entry| { |
| 407 |
let pid: u32 = entry.file_name().to_string_lossy().parse().ok()?; |
| 408 |
let stat = std::fs::read_to_string(format!("/proc/{pid}/stat")).ok()?; |
| 409 |
let mut parts = stat.splitn(5, ' '); |
| 410 |
let _pid = parts.next(); |
| 411 |
let comm = parts |
| 412 |
.next() |
| 413 |
.unwrap_or("") |
| 414 |
.trim_matches(|c| c == '(' || c == ')') |
| 415 |
.to_string(); |
| 416 |
let _state = parts.next(); |
| 417 |
let ppid: u32 = parts.next().unwrap_or("0").parse().unwrap_or(0); |
| 418 |
if ppid == parent_pid && comm.contains("sleep") { |
| 419 |
Some(comm) |
| 420 |
} else { |
| 421 |
None |
| 422 |
} |
| 423 |
}) |
| 424 |
}); |
| 425 |
if found.is_some() { |
| 426 |
break found; |
| 427 |
} |
| 428 |
thread::sleep(poll_interval); |
| 429 |
|
| 430 |
if start_time.elapsed() > timeout { |
| 431 |
break None; |
| 432 |
} |
| 433 |
}; |
| 434 |
|
| 435 |
parent.kill().ok(); |
| 436 |
parent.wait().ok(); |
| 437 |
|
| 438 |
assert!( |
| 439 |
child_comm.is_some(), |
| 440 |
"stdbuf should have spawned a child running 'sleep' (pid={parent_pid})" |
| 441 |
); |
| 442 |
assert!( |
| 443 |
child_comm.as_deref().unwrap_or("").contains("sleep"), |
| 444 |
"Expected child to be 'sleep', got: {child_comm:?}" |
| 445 |
); |
| 446 |
} |
| 447 |
|
| 448 |
/// Verify that stdbuf does not leak temporary directories. |
| 449 |
/// Each invocation should clean up its own tmpdir. |
| 450 |
/// Regression test for https://github.com/uutils/coreutils/issues/13939 |
| 451 |
#[test] |
| 452 |
#[cfg(all(target_os = "linux", not(feature = "feat_external_libstdbuf")))] |
| 453 |
fn test_stdbuf_no_tmpdir_leak() { |
| 454 |
use std::process::Command; |
| 455 |
|
| 456 |
// Use a dedicated TMPDIR so we only count stdbuf-created dirs, |
| 457 |
// not dirs created by the test harness itself. |
| 458 |
let dedicated_tmpdir = tempfile::tempdir().unwrap(); |
| 459 |
let scene = TestScenario::new(util_name!()); |
| 460 |
|
| 461 |
for _ in 0..5 { |
| 462 |
Command::new(&scene.bin_path) |
| 463 |
.args(["stdbuf", "-oL", "true"]) |
| 464 |
.env("TMPDIR", dedicated_tmpdir.path()) |
| 465 |
.status() |
| 466 |
.expect("failed to run stdbuf"); |
| 467 |
} |
| 468 |
|
| 469 |
let leaked: Vec<_> = std::fs::read_dir(dedicated_tmpdir.path()) |
| 470 |
.unwrap() |
| 471 |
.flatten() |
| 472 |
.filter(|e| e.metadata().is_ok_and(|m| m.is_dir())) |
| 473 |
.map(|e| e.file_name().to_string_lossy().to_string()) |
| 474 |
.collect(); |
| 475 |
|
| 476 |
assert!( |
| 477 |
leaked.is_empty(), |
| 478 |
"stdbuf leaked {n} temporary director{pl}: {leaked:?}", |
| 479 |
n = leaked.len(), |
| 480 |
pl = if leaked.len() == 1 { "y" } else { "ies" }, |
| 481 |
); |
| 482 |
} |
| 483 |
|
| 484 |
/// Verify that the temporary directory created for libstdbuf.so is private (0700) |
| 485 |
/// and the embedded library file is owner read/write only (0600). |
| 486 |
/// Regression test for https://github.com/uutils/coreutils/issues/13939 |
| 487 |
#[test] |
| 488 |
#[cfg(all(target_os = "linux", not(feature = "feat_external_libstdbuf")))] |
| 489 |
fn test_stdbuf_tmpdir_is_private() { |
| 490 |
use std::os::unix::fs::PermissionsExt; |
| 491 |
|
| 492 |
// Use a dedicated TMPDIR so we only observe stdbuf's directory. |
| 493 |
let dedicated_tmpdir = tempfile::tempdir().unwrap(); |
| 494 |
let scene = TestScenario::new(util_name!()); |
| 495 |
|
| 496 |
// Use a long-running command so the tmpdir exists while we inspect it. |
| 497 |
let mut child = std::process::Command::new(&scene.bin_path) |
| 498 |
.args(["stdbuf", "-o0", "sleep", "5"]) |
| 499 |
.env("TMPDIR", dedicated_tmpdir.path()) |
| 500 |
.spawn() |
| 501 |
.expect("failed to spawn stdbuf"); |
| 502 |
|
| 503 |
// Give it time to create the tmpdir |
| 504 |
std::thread::sleep(std::time::Duration::from_millis(300)); |
| 505 |
|
| 506 |
let stdbuf_dirs: Vec<_> = std::fs::read_dir(dedicated_tmpdir.path()) |
| 507 |
.unwrap() |
| 508 |
.flatten() |
| 509 |
.filter(|e| e.metadata().is_ok_and(|m| m.is_dir())) |
| 510 |
.map(|e| e.path()) |
| 511 |
.collect(); |
| 512 |
|
| 513 |
child.kill().ok(); |
| 514 |
child.wait().ok(); |
| 515 |
|
| 516 |
assert!( |
| 517 |
!stdbuf_dirs.is_empty(), |
| 518 |
"expected stdbuf to create a temporary directory in {dedicated_tmpdir:?}" |
| 519 |
); |
| 520 |
|
| 521 |
for dir in &stdbuf_dirs { |
| 522 |
let mode = std::fs::metadata(dir) |
| 523 |
.expect("metadata") |
| 524 |
.permissions() |
| 525 |
.mode(); |
| 526 |
assert_eq!( |
| 527 |
mode & 0o777, |
| 528 |
0o700, |
| 529 |
"tmpdir {dir:?} has unsafe permissions {mode:#o}, expected 0o700" |
| 530 |
); |
| 531 |
|
| 532 |
for entry in std::fs::read_dir(dir).expect("read_dir").flatten() { |
| 533 |
if !entry.file_type().is_ok_and(|t| t.is_file()) { |
| 534 |
continue; |
| 535 |
} |
| 536 |
let file_mode = entry.metadata().expect("metadata").permissions().mode() & 0o777; |
| 537 |
assert_eq!( |
| 538 |
file_mode, |
| 539 |
0o600, |
| 540 |
"libstdbuf at {} has unsafe permissions {file_mode:#o}, expected 0o600", |
| 541 |
entry.path().display(), |
| 542 |
); |
| 543 |
} |
| 544 |
} |
| 545 |
} |
| 546 |
|
| 547 |
/// stdbuf waits on the command instead of exec'ing it, so it has to translate |
| 548 |
/// the child's fate back into its own exit status: a command killed by a |
| 549 |
/// signal must still be reported as `128 + signal`, not as a clean 0. |
| 550 |
#[test] |
| 551 |
#[cfg(unix)] |
| 552 |
fn test_stdbuf_reports_signalled_command() { |
| 553 |
// SIGQUIT (3) rather than the usual SIGTERM, so the expected 131 cannot be |
| 554 |
// confused with a status the shell would produce on its own. |
| 555 |
new_ucmd!() |
| 556 |
.args(&["-o0", "sh", "-c", "kill -QUIT $$"]) |
| 557 |
.fails_with_code(131); |
| 558 |
} |
| 559 |
|
| 560 |
#[cfg(unix)] |
| 561 |
#[cfg(all(feature = "feat_diagnostics", not(wasi_runner)))] |
| 562 |
mod diagnostics { |
| 563 |
use super::*; |
| 564 |
|
| 565 |
#[test] |
| 566 |
fn test_snippet_points_at_the_unknown_unit() { |
| 567 |
let result = new_ucmd!() |
| 568 |
.terminal_sim_stderr() |
| 569 |
.args(&["-o", "6pq", "head"]) |
| 570 |
.fails_with_code(125); |
| 571 |
|
| 572 |
// The number parsed; only the unit did not. |
| 573 |
let stderr = result.stderr_as_displayed(); |
| 574 |
assert!( |
| 575 |
stderr.starts_with( |
| 576 |
"\ |
| 577 |
stdbuf: invalid mode '6pq' |
| 578 |
╭─[ stdbuf:1:12 ] |
| 579 |
│ |
| 580 |
1 │ stdbuf -o 6pq head |
| 581 |
│ ─┬ |
| 582 |
│ ╰── not a known unit |
| 583 |
│ |
| 584 |
│ Help: a size is a number and an optional unit: K, M, G and so on for 1024, KB, MB, GB for 1000 |
| 585 |
───╯" |
| 586 |
), |
| 587 |
"{stderr}" |
| 588 |
); |
| 589 |
// The caret replaces the message, not the usage hint: a pipe and a |
| 590 |
// terminal must not disagree on whether one was printed. |
| 591 |
assert!( |
| 592 |
stderr.ends_with("stdbuf --help' for more information."), |
| 593 |
"{stderr}" |
| 594 |
); |
| 595 |
} |
| 596 |
|
| 597 |
#[test] |
| 598 |
fn test_snippet_points_inside_a_long_option_value() { |
| 599 |
let result = new_ucmd!() |
| 600 |
.terminal_sim_stderr() |
| 601 |
.args(&["--error=pq", "head"]) |
| 602 |
.fails_with_code(125); |
| 603 |
let stderr = result.stderr_as_displayed(); |
| 604 |
|
| 605 |
// Nothing usable was read, so the whole value is underlined. |
| 606 |
assert!(stderr.contains("stdbuf:1:16"), "{stderr}"); |
| 607 |
assert!(!stderr.contains("not a known unit"), "{stderr}"); |
| 608 |
} |
| 609 |
|
| 610 |
#[test] |
| 611 |
fn test_plain_message_when_stderr_is_a_pipe() { |
| 612 |
new_ucmd!() |
| 613 |
.args(&["-o", "6pq", "head"]) |
| 614 |
.fails_with_code(125) |
| 615 |
.usage_error("invalid mode '6pq'"); |
| 616 |
} |
| 617 |
} |