From 6030ad9bc9bbf9169f1304e28f9a36b5c0462cb2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dorian=20P=C3=A9ron?= Date: Sat, 19 Oct 2024 01:12:47 +0200 Subject: [PATCH] test(cksum): add tests for non-utf-8 handling in checksum files --- tests/by-util/test_cksum.rs | 77 +++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) diff --git a/tests/by-util/test_cksum.rs b/tests/by-util/test_cksum.rs index 207fd54a4..98366cbec 100644 --- a/tests/by-util/test_cksum.rs +++ b/tests/by-util/test_cksum.rs @@ -1425,3 +1425,80 @@ fn test_check_trailing_space_fails() { .no_stdout() .stderr_contains("CHECKSUM: no properly formatted checksum lines found"); } + +/// Regroup tests related to the handling of non-utf-8 content +/// 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. +#[cfg(not(windows))] +mod check_utf8 { + use super::*; + + #[test] + fn test_check_non_utf8_comment() { + let hashes = + b"MD5 (empty) = 1B2M2Y8AsgTpgAmY7PhCfg==\n\ + # Comment with a non utf8 char: >>\xff<<\n\ + SHA256 (empty) = 47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU=\n\ + BLAKE2b (empty) = eGoC90IBWQPGxv2FJVLScpEvR0DhWEdhiobiF/cfVBnSXhAxr+5YUxOJZESTTrBLkDpoWxRIt1XVb3Aa/pvizg==\n" + ; + + let scene = TestScenario::new(util_name!()); + let at = &scene.fixtures; + + at.touch("empty"); + at.write_bytes("check", hashes); + + scene + .ucmd() + .arg("--check") + .arg(at.subdir.join("check")) + .succeeds() + .stdout_is("empty: OK\nempty: OK\nempty: OK\n") + .no_stderr(); + } + + #[cfg(target_os = "linux")] + #[test] + fn test_check_non_utf8_filename() { + use std::{ffi::OsString, os::unix::ffi::OsStringExt}; + + let scene = TestScenario::new(util_name!()); + let at = &scene.fixtures; + let filename: OsString = OsStringExt::from_vec(b"funky\xffname".to_vec()); + at.touch(&filename); + + // Checksum match + at.write_bytes("check", + b"SHA256 (funky\xffname) = e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855\n"); + scene + .ucmd() + .arg("--check") + .arg(at.subdir.join("check")) + .succeeds() + .stdout_is_bytes(b"funky\xffname: OK\n") + .no_stderr(); + + // Checksum mismatch + at.write_bytes("check", + b"SHA256 (funky\xffname) = ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff\n"); + scene + .ucmd() + .arg("--check") + .arg(at.subdir.join("check")) + .fails() + .stdout_is_bytes(b"funky\xffname: FAILED\n") + .stderr_contains("1 computed checksum did NOT match"); + + // file not found + at.write_bytes("check", + b"SHA256 (flakey\xffname) = ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff\n"); + scene + .ucmd() + .arg("--check") + .arg(at.subdir.join("check")) + .fails() + .stdout_is_bytes(b"flakey\xffname: FAILED open or read\n") + .stderr_contains("1 listed file could not be read"); + } +}