diff --git a/Cargo.lock b/Cargo.lock index 6237a3660..e3919220d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -559,6 +559,7 @@ dependencies = [ "regex", "rlimit", "rstest", + "rstest_reuse", "rustc-hash", "selinux", "sha1", @@ -2558,6 +2559,17 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "rstest_reuse" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b3a8fb4672e840a587a66fc577a5491375df51ddb88f2a2c2a792598c326fe14" +dependencies = [ + "quote", + "rand 0.8.5", + "syn", +] + [[package]] name = "rust-ini" version = "0.21.3" diff --git a/Cargo.toml b/Cargo.toml index 92013b0af..9ef80cd07 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -432,6 +432,7 @@ rayon = "1.10" regex = "1.10.4" rlimit = "0.11.0" rstest = "0.26.0" +rstest_reuse = "0.7.0" rustc-hash = "2.1.1" rust-ini = "0.21.0" same-file = "1.0.6" @@ -640,6 +641,7 @@ uucore = { workspace = true, features = [ walkdir.workspace = true hex-literal = "1.0.0" rstest.workspace = true +rstest_reuse.workspace = true [target.'cfg(unix)'.dev-dependencies] nix = { workspace = true, features = [ diff --git a/tests/by-util/test_cksum.rs b/tests/by-util/test_cksum.rs index 67f96e907..1ab5bb452 100644 --- a/tests/by-util/test_cksum.rs +++ b/tests/by-util/test_cksum.rs @@ -5,6 +5,7 @@ // spell-checker:ignore (words) asdf algo algos asha mgmt xffname hexa GFYEQ HYQK Yqxb dont checkfile use rstest::rstest; +use rstest_reuse::{apply, template}; use uutests::at_and_ucmd; use uutests::new_ucmd; @@ -3287,3 +3288,112 @@ fn test_check_shake256_no_length() { .fails() .stderr_only("cksum: 'standard input': no properly formatted checksum lines found\n"); } + +#[template] +#[rstest] +#[case::no_length( + b"foo", + "04e0bb39f30b1a3feb89f536c93be15055482df748674b00d26e5a75777702e9", + None +)] +#[case( + b"foo", + "04e0bb39f30b1a3feb89f536c93be15055482df748674b00d26e5a75777702e9", + Some(0) +)] +#[case( + b"foo", + "04e0bb39f30b1a3feb89f536c93be15055482df748674b00d26e5a75777702e9", + Some(256) +)] +#[case( + b"foo", + "04e0bb39f30b1a3feb89f536c93be15055482df748674b00d26e5a75777702e9791074b7511b59d31c71c62f5a745689fa6c", + Some(400) +)] +#[case(b"foo", "04e0bb39f3", Some(40))] +#[case(b"foo", "04e0", Some(16))] +#[case(b"foo", "04", Some(8))] +fn test_blake3(#[case] input: &[u8], #[case] expected: &str, #[case] length: Option) {} + +#[apply(test_blake3)] +fn test_compute_blake3( + #[case] input: &[u8], + #[case] expected: &str, + #[case] length: Option, +) { + let length_args: &[String] = if let Some(len) = length { + &["-l".into(), len.to_string()] + } else { + &[] + }; + + new_ucmd!() + .arg("-a") + .arg("blake3") + .args(length_args) + .pipe_in(input) + .succeeds() + .stdout_only(format!( + "BLAKE3{} (-) = {expected}\n", + match length { + Some(0) | None => "-256".into(), + Some(i) => format!("-{i}"), + } + )); + + // with --untagged + new_ucmd!() + .arg("-a") + .arg("blake3") + .arg("--untagged") + .args(length_args) + .pipe_in(input) + .succeeds() + .stdout_only(format!("{expected} -\n")); +} + +#[apply(test_blake3)] +fn test_check_blake3_tagged( + #[case] input: &[u8], + #[case] digest: &str, + #[case] opt_len: Option, +) { + let (at, mut ucmd) = at_and_ucmd!(); + at.write_bytes("FILE", input); + + let len = match opt_len { + Some(0) => "-256".into(), + Some(i) => format!("-{i}"), + None => String::new(), + }; + + let tagged = format!("BLAKE3{len} (FILE) = {digest}",); + + ucmd.arg("-c") + .arg("-a") + .arg("blake3") + .pipe_in(tagged) + .succeeds() + .stdout_only("FILE: OK\n"); +} + +#[apply(test_blake3)] +#[allow(clippy::used_underscore_binding)] +fn test_check_blake3_untagged( + #[case] input: &[u8], + #[case] digest: &str, + #[case] _opt_len: Option, +) { + let (at, mut ucmd) = at_and_ucmd!(); + at.write_bytes("FILE", input); + + let untagged = format!("{digest} FILE"); + + ucmd.arg("-c") + .arg("-a") + .arg("blake3") + .pipe_in(untagged) + .succeeds() + .stdout_only("FILE: OK\n"); +}