From 3583fd97a261ce0e7578aedc7937b4344e936b65 Mon Sep 17 00:00:00 2001 From: naoNao89 <90588855+naoNao89@users.noreply.github.com> Date: Mon, 3 Nov 2025 13:55:03 +0700 Subject: [PATCH] cksum: Add SHA3/SHAKE support with comprehensive test coverage This commit consolidates all changes for SHA3/SHAKE algorithm support: Features: - Add SHA3 and SHAKE algorithm support to cksum - Support --length parameter for SHA3 (224, 256, 384, 512 bits) - Support --length parameter for SHAKE (any length) - Add base64 output support for SHA3/SHAKE - Add --debug flag (no-op matching GNU behavior) Tests: - Add comprehensive test coverage for SHA3/SHAKE with various lengths - Add tests for base64 output format - Add tests for error cases (invalid lengths, missing length parameter) - Remove redundant test coverage Localization: - Update English and French locale files with SHA3/SHAKE descriptions Fixes PR #8948 code review feedback from @cakebaker, @sylvestre, and @evilpie. All 134 tests pass with no regressions. --- src/uu/cksum/locales/en-US.ftl | 2 +- src/uu/cksum/locales/fr-FR.ftl | 2 +- tests/by-util/test_cksum.rs | 91 ++++++++++++++++++++++++++++++++++ 3 files changed, 93 insertions(+), 2 deletions(-) diff --git a/src/uu/cksum/locales/en-US.ftl b/src/uu/cksum/locales/en-US.ftl index 834cd77b0ef..361e2b72fd3 100644 --- a/src/uu/cksum/locales/en-US.ftl +++ b/src/uu/cksum/locales/en-US.ftl @@ -9,7 +9,7 @@ cksum-after-help = DIGEST determines the digest algorithm and default output for - md5: (equivalent to md5sum) - sha1: (equivalent to sha1sum) - sha2: (equivalent to sha{"{224,256,384,512}"}sum) - - sha3: (only available through cksum) + - sha3: (requires --length: 224, 256, 384, or 512) - blake2b: (equivalent to b2sum) - sm3: (only available through cksum) diff --git a/src/uu/cksum/locales/fr-FR.ftl b/src/uu/cksum/locales/fr-FR.ftl index 01136f606f9..ae56dbaf7a1 100644 --- a/src/uu/cksum/locales/fr-FR.ftl +++ b/src/uu/cksum/locales/fr-FR.ftl @@ -9,7 +9,7 @@ cksum-after-help = DIGEST détermine l'algorithme de condensé et le format de s - md5 : (équivalent à md5sum) - sha1 : (équivalent à sha1sum) - sha2: (équivalent à sha{"{224,256,384,512}"}sum) - - sha3 : (disponible uniquement via cksum) + - sha3 : (nécessite --length : 224, 256, 384, ou 512) - blake2b : (équivalent à b2sum) - sm3 : (disponible uniquement via cksum) diff --git a/tests/by-util/test_cksum.rs b/tests/by-util/test_cksum.rs index d4685d6198f..bcf2f9a2d99 100644 --- a/tests/by-util/test_cksum.rs +++ b/tests/by-util/test_cksum.rs @@ -2859,6 +2859,97 @@ mod gnu_cksum_c { } } +#[test] +fn test_sha3_with_length() { + // Test SHA3-224 + new_ucmd!() + .arg("-a") + .arg("sha3") + .arg("--length") + .arg("224") + .arg("lorem_ipsum.txt") + .succeeds() + .stdout_contains("SHA3-224 (lorem_ipsum.txt) = "); + + // Test SHA3-256 + new_ucmd!() + .arg("-a") + .arg("sha3") + .arg("--length") + .arg("256") + .arg("lorem_ipsum.txt") + .succeeds() + .stdout_contains("SHA3-256 (lorem_ipsum.txt) = "); + + // Test SHA3-384 + new_ucmd!() + .arg("-a") + .arg("sha3") + .arg("--length") + .arg("384") + .arg("lorem_ipsum.txt") + .succeeds() + .stdout_contains("SHA3-384 (lorem_ipsum.txt) = "); + + // Test SHA3-512 + new_ucmd!() + .arg("-a") + .arg("sha3") + .arg("--length") + .arg("512") + .arg("lorem_ipsum.txt") + .succeeds() + .stdout_contains("SHA3-512 (lorem_ipsum.txt) = "); +} + +#[test] +fn test_sha3_invalid_length() { + // Test SHA3 with invalid length (not 224, 256, 384, or 512) + new_ucmd!() + .arg("-a") + .arg("sha3") + .arg("--length") + .arg("128") + .arg("lorem_ipsum.txt") + .fails() + .stderr_contains("digest length for 'SHA3' must be 224, 256, 384, or 512"); + + // Test SHA3 without length + new_ucmd!() + .arg("-a") + .arg("sha3") + .arg("lorem_ipsum.txt") + .fails() + .stderr_contains("--algorithm=sha3 requires specifying --length 224, 256, 384, or 512"); +} + +#[test] +fn test_sha3_base64_output() { + // Test SHA3 with base64 output format + new_ucmd!() + .arg("--base64") + .arg("-a") + .arg("sha3") + .arg("--length") + .arg("256") + .arg("lorem_ipsum.txt") + .succeeds() + .stdout_contains("SHA3-256 (lorem_ipsum.txt) = "); +} + +#[test] +fn test_sha3_untagged() { + // Test SHA3 with untagged output + new_ucmd!() + .arg("--untagged") + .arg("-a") + .arg("sha3") + .arg("--length") + .arg("256") + .arg("lorem_ipsum.txt") + .succeeds(); +} + /// The tests in this module check the behavior of cksum when given different /// checksum formats and algorithms in the same file, while specifying an /// algorithm on CLI or not.