1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-27 19:17:43 +00:00

cksum: Fix file quoting on stderr (#8272)

* cksum: handle escaping in stderr for file errors

* uutests: Minor fix and stdX_contains_bytes feature

* test(cksum): add test for stderr escaping
This commit is contained in:
Dorian Péron 2025-06-29 22:07:27 +02:00 committed by GitHub
parent c64adee068
commit 56ce0e28ad
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 91 additions and 14 deletions

View file

@ -1453,7 +1453,7 @@ fn test_check_trailing_space_fails() {
/// in checksum files.
/// These tests are excluded from Windows because it does not provide any safe
/// conversion between `OsString` and byte sequences for non-utf-8 strings.
mod check_utf8 {
mod check_encoding {
// This test should pass on linux and macos.
#[cfg(not(windows))]
@ -1467,15 +1467,12 @@ mod check_utf8 {
BLAKE2b (empty) = eGoC90IBWQPGxv2FJVLScpEvR0DhWEdhiobiF/cfVBnSXhAxr+5YUxOJZESTTrBLkDpoWxRIt1XVb3Aa/pvizg==\n"
;
let scene = TestScenario::new(util_name!());
let at = &scene.fixtures;
let (at, mut cmd) = at_and_ucmd!();
at.touch("empty");
at.write_bytes("check", hashes);
scene
.ucmd()
.arg("--check")
cmd.arg("--check")
.arg(at.subdir.join("check"))
.succeeds()
.stdout_is("empty: OK\nempty: OK\nempty: OK\n")
@ -1528,6 +1525,29 @@ mod check_utf8 {
.stdout_is_bytes(b"flakey\xffname: FAILED open or read\n")
.stderr_contains("1 listed file could not be read");
}
#[cfg(target_os = "linux")]
#[test]
fn test_quoting_in_stderr() {
use super::*;
use std::{ffi::OsStr, os::unix::ffi::OsStrExt};
let (at, mut cmd) = at_and_ucmd!();
at.mkdir(<OsStr as OsStrExt>::from_bytes(b"FFF\xffDIR"));
at.write_bytes(
"check",
b"SHA256 (FFF\xffFFF) = 29953405eaa3dcc41c37d1621d55b6a47eee93e05613e439e73295029740b10c\nSHA256 (FFF\xffDIR) = 29953405eaa3dcc41c37d1621d55b6a47eee93e05613e439e73295029740b10c\n",
);
cmd.arg("-c")
.arg("check")
.fails_with_code(1)
.stdout_contains_bytes(b"FFF\xffFFF: FAILED open or read")
.stdout_contains_bytes(b"FFF\xffDIR: FAILED open or read")
.stderr_contains("'FFF'$'\\377''FFF': No such file or directory")
.stderr_contains("'FFF'$'\\377''DIR': Is a directory");
}
}
#[test]

View file

@ -71,7 +71,7 @@ macro_rules! new_ucmd {
#[macro_export]
macro_rules! at_and_ucmd {
() => {{
let ts = TestScenario::new(util_name!());
let ts = ::uutests::util::TestScenario::new(::uutests::util_name!());
(ts.fixtures.clone(), ts.ucmd())
}};
}

View file

@ -12,6 +12,7 @@
clippy::missing_errors_doc
)]
use core::str;
#[cfg(unix)]
use libc::mode_t;
#[cfg(unix)]
@ -758,6 +759,29 @@ impl CmdResult {
self
}
/// Verify if stdout contains a byte sequence
///
/// # Examples
///
/// ```rust,ignore
/// new_ucmd!()
/// .arg("--help")
/// .succeeds()
/// .stdout_contains_bytes(b"hello \xff");
/// ```
#[track_caller]
pub fn stdout_contains_bytes<T: AsRef<[u8]>>(&self, cmp: T) -> &Self {
assert!(
self.stdout()
.windows(cmp.as_ref().len())
.any(|sub| sub == cmp.as_ref()),
"'{:?}'\ndoes not contain\n'{:?}'",
self.stdout(),
cmp.as_ref()
);
self
}
/// Verify if stderr contains a specific string
///
/// # Examples
@ -780,6 +804,29 @@ impl CmdResult {
self
}
/// Verify if stderr contains a byte sequence
///
/// # Examples
///
/// ```rust,ignore
/// new_ucmd!()
/// .arg("--help")
/// .succeeds()
/// .stdout_contains_bytes(b"hello \xff");
/// ```
#[track_caller]
pub fn stderr_contains_bytes<T: AsRef<[u8]>>(&self, cmp: T) -> &Self {
assert!(
self.stderr()
.windows(cmp.as_ref().len())
.any(|sub| sub == cmp.as_ref()),
"'{:?}'\ndoes not contain\n'{:?}'",
self.stderr(),
cmp.as_ref()
);
self
}
/// Verify if stdout does not contain a specific string
///
/// # Examples