diff --git a/Cargo.lock b/Cargo.lock index 8269a9be2..8232e2e27 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -628,6 +628,7 @@ dependencies = [ "uu_runcon", "uu_seq", "uu_sha1sum", + "uu_sha224sum", "uu_shred", "uu_shuf", "uu_sleep", @@ -3997,6 +3998,18 @@ dependencies = [ "uucore", ] +[[package]] +name = "uu_sha224sum" +version = "0.6.0" +dependencies = [ + "clap", + "codspeed-divan-compat", + "fluent", + "tempfile", + "uu_checksum_common", + "uucore", +] + [[package]] name = "uu_shred" version = "0.6.0" diff --git a/Cargo.toml b/Cargo.toml index d2e72e7ac..789586c3d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -89,6 +89,7 @@ feat_common_core = [ "b2sum", "md5sum", "sha1sum", + "sha224sum", "comm", "cp", "csplit", @@ -443,6 +444,7 @@ cksum = { optional = true, version = "0.6.0", package = "uu_cksum", path = "src/ b2sum = { optional = true, version = "0.6.0", package = "uu_b2sum", path = "src/uu/b2sum" } md5sum = { optional = true, version = "0.6.0", package = "uu_md5sum", path = "src/uu/md5sum" } sha1sum = { optional = true, version = "0.6.0", package = "uu_sha1sum", path = "src/uu/sha1sum" } +sha224sum = { optional = true, version = "0.6.0", package = "uu_sha224sum", path = "src/uu/sha224sum" } comm = { optional = true, version = "0.6.0", package = "uu_comm", path = "src/uu/comm" } cp = { optional = true, version = "0.6.0", package = "uu_cp", path = "src/uu/cp" } csplit = { optional = true, version = "0.6.0", package = "uu_csplit", path = "src/uu/csplit" } diff --git a/GNUmakefile b/GNUmakefile index 053441c74..f713e6390 100644 --- a/GNUmakefile +++ b/GNUmakefile @@ -96,7 +96,6 @@ SELINUX_PROGS := \ runcon HASHSUM_PROGS := \ - sha224sum \ sha256sum \ sha384sum \ sha512sum diff --git a/build.rs b/build.rs index 6b0e177fd..49d8c675c 100644 --- a/build.rs +++ b/build.rs @@ -91,7 +91,6 @@ pub fn main() { phf_map.entry(krate, format!("({krate}::uumain, {krate}::uu_app_custom)")); let map_value = format!("({krate}::uumain, {krate}::uu_app_common)"); - phf_map.entry("sha224sum", map_value.clone()); phf_map.entry("sha256sum", map_value.clone()); phf_map.entry("sha384sum", map_value.clone()); phf_map.entry("sha512sum", map_value.clone()); diff --git a/src/common/validation.rs b/src/common/validation.rs index ffadfd917..c5aaac3d0 100644 --- a/src/common/validation.rs +++ b/src/common/validation.rs @@ -51,7 +51,7 @@ fn get_canonical_util_name(util_name: &str) -> &str { "[" => "test", // hashsum aliases - all these hash commands are aliases for hashsum - "sha224sum" | "sha256sum" | "sha384sum" | "sha512sum" => "hashsum", + "sha256sum" | "sha384sum" | "sha512sum" => "hashsum", "dir" => "ls", // dir is an alias for ls diff --git a/src/uu/sha224sum/Cargo.toml b/src/uu/sha224sum/Cargo.toml new file mode 100644 index 000000000..25086ee42 --- /dev/null +++ b/src/uu/sha224sum/Cargo.toml @@ -0,0 +1,38 @@ +[package] +name = "uu_sha224sum" +description = "sha224sum ~ (uutils) Print or check the SHA224 checksums" +repository = "https://github.com/uutils/coreutils/tree/main/src/uu/sha224sum" +version.workspace = true +authors.workspace = true +license.workspace = true +homepage.workspace = true +keywords.workspace = true +categories.workspace = true +edition.workspace = true +readme.workspace = true + +[lints] +workspace = true + +[lib] +path = "src/sha224sum.rs" + +[dependencies] +clap = { workspace = true } +uu_checksum_common = { workspace = true } +uucore = { workspace = true, features = [ + "checksum", + "encoding", + "sum", + "hardware", +] } +fluent = { workspace = true } + +[dev-dependencies] +divan = { workspace = true } +tempfile = { workspace = true } +uucore = { workspace = true, features = ["benchmark"] } + +[[bin]] +name = "sha224sum" +path = "src/main.rs" diff --git a/src/uu/sha224sum/LICENSE b/src/uu/sha224sum/LICENSE new file mode 120000 index 000000000..5853aaea5 --- /dev/null +++ b/src/uu/sha224sum/LICENSE @@ -0,0 +1 @@ +../../../LICENSE \ No newline at end of file diff --git a/src/uu/sha224sum/locales/en-US.ftl b/src/uu/sha224sum/locales/en-US.ftl new file mode 100644 index 000000000..00f852b71 --- /dev/null +++ b/src/uu/sha224sum/locales/en-US.ftl @@ -0,0 +1,2 @@ +sha224sum-about = Print or check the SHA224 checksums +sha224sum-usage = sha224sum [OPTIONS] [FILE]... diff --git a/src/uu/sha224sum/locales/fr-FR.ftl b/src/uu/sha224sum/locales/fr-FR.ftl new file mode 100644 index 000000000..dbd90e9f3 --- /dev/null +++ b/src/uu/sha224sum/locales/fr-FR.ftl @@ -0,0 +1,2 @@ +sha224sum-about = Afficher le SHA224 et la taille de chaque fichier +sha224sum-usage = sha224sum [OPTION]... [FICHIER]... diff --git a/src/uu/sha224sum/src/main.rs b/src/uu/sha224sum/src/main.rs new file mode 100644 index 000000000..974671331 --- /dev/null +++ b/src/uu/sha224sum/src/main.rs @@ -0,0 +1 @@ +uucore::bin!(uu_sha224sum); diff --git a/src/uu/sha224sum/src/sha224sum.rs b/src/uu/sha224sum/src/sha224sum.rs new file mode 100644 index 000000000..349104675 --- /dev/null +++ b/src/uu/sha224sum/src/sha224sum.rs @@ -0,0 +1 @@ +uu_checksum_common::declare_standalone!("sha224sum", uucore::checksum::AlgoKind::Sha224); diff --git a/src/uucore/src/lib/lib.rs b/src/uucore/src/lib/lib.rs index 203a7ebb2..b013e9128 100644 --- a/src/uucore/src/lib/lib.rs +++ b/src/uucore/src/lib/lib.rs @@ -173,7 +173,7 @@ pub fn get_canonical_util_name(util_name: &str) -> &str { "[" => "test", // hashsum aliases - all these hash commands are aliases for hashsum - "sha224sum" | "sha256sum" | "sha384sum" | "sha512sum" => "hashsum", + "sha256sum" | "sha384sum" | "sha512sum" => "hashsum", "dir" => "ls", // dir is an alias for ls diff --git a/src/uucore/src/lib/mods/locale.rs b/src/uucore/src/lib/mods/locale.rs index 2b740eff8..c69a5452d 100644 --- a/src/uucore/src/lib/mods/locale.rs +++ b/src/uucore/src/lib/mods/locale.rs @@ -157,7 +157,7 @@ fn create_bundle( try_add_resource_from(get_locales_dir(util_name).ok()); // checksum binaries also require fluent files from the checksum_common crate - if ["cksum", "b2sum", "md5sum", "sha1sum"].contains(&util_name) { + if ["cksum", "b2sum", "md5sum", "sha1sum", "sha224sum"].contains(&util_name) { try_add_resource_from(get_locales_dir("checksum_common").ok()); } diff --git a/tests/by-util/test_hashsum.rs b/tests/by-util/test_hashsum.rs index 646724d66..ea044f6ab 100644 --- a/tests/by-util/test_hashsum.rs +++ b/tests/by-util/test_hashsum.rs @@ -205,7 +205,6 @@ test_digest! {b3sum, b3sum} test_digest! {shake128, shake128} test_digest! {shake256, shake256} -test_digest_with_len! {sha224, sha224, 224} test_digest_with_len! {sha256, sha256, 256} test_digest_with_len! {sha384, sha384, 384} test_digest_with_len! {sha512, sha512, 512} diff --git a/tests/by-util/test_sha224sum.rs b/tests/by-util/test_sha224sum.rs new file mode 100644 index 000000000..e2b7129b8 --- /dev/null +++ b/tests/by-util/test_sha224sum.rs @@ -0,0 +1,120 @@ +// This file is part of the uutils coreutils package. +// +// For the full copyright and license information, please view the LICENSE +// file that was distributed with this source code. + +use uutests::new_ucmd; +// spell-checker:ignore checkfile, testf, ntestf +macro_rules! get_hash( + ($str:expr) => ( + $str.split(' ').collect::>()[0] + ); +); + +macro_rules! test_digest { + ($id:ident) => { + mod $id { + use uutests::util::*; + use uutests::util_name; + static EXPECTED_FILE: &'static str = concat!(stringify!($id), ".expected"); + static CHECK_FILE: &'static str = concat!(stringify!($id), ".checkfile"); + static INPUT_FILE: &'static str = "input.txt"; + + #[test] + fn test_single_file() { + let ts = TestScenario::new(util_name!()); + assert_eq!( + ts.fixtures.read(EXPECTED_FILE), + get_hash!( + ts.ucmd() + .arg(INPUT_FILE) + .succeeds() + .no_stderr() + .stdout_str() + ) + ); + } + + #[test] + fn test_stdin() { + let ts = TestScenario::new(util_name!()); + assert_eq!( + ts.fixtures.read(EXPECTED_FILE), + get_hash!( + ts.ucmd() + .pipe_in_fixture(INPUT_FILE) + .succeeds() + .no_stderr() + .stdout_str() + ) + ); + } + + #[test] + fn test_check() { + let ts = TestScenario::new(util_name!()); + println!("File content='{}'", ts.fixtures.read(INPUT_FILE)); + println!("Check file='{}'", ts.fixtures.read(CHECK_FILE)); + + ts.ucmd() + .args(&["--check", CHECK_FILE]) + .succeeds() + .no_stderr() + .stdout_is("input.txt: OK\n"); + } + + #[test] + fn test_zero() { + let ts = TestScenario::new(util_name!()); + assert_eq!( + ts.fixtures.read(EXPECTED_FILE), + get_hash!( + ts.ucmd() + .arg("--zero") + .arg(INPUT_FILE) + .succeeds() + .no_stderr() + .stdout_str() + ) + ); + } + + #[test] + fn test_missing_file() { + let ts = TestScenario::new(util_name!()); + let at = &ts.fixtures; + + at.write("a", "file1\n"); + at.write("c", "file3\n"); + + ts.ucmd() + .args(&["a", "b", "c"]) + .fails() + .stdout_contains("a\n") + .stdout_contains("c\n") + .stderr_contains("b: No such file or directory"); + } + } + }; +} +test_digest! {sha224} + +#[test] +fn test_invalid_arg() { + new_ucmd!().arg("--definitely-invalid").fails_with_code(1); +} + +#[test] +fn test_conflicting_arg() { + new_ucmd!().arg("--tag").arg("--check").fails_with_code(1); +} + +#[test] +fn test_help_shows_correct_utility_name() { + // Test that help output shows the actual utility name instead of "hashsum" + new_ucmd!() + .arg("--help") + .succeeds() + .stdout_contains("Usage: sha224sum") + .stdout_does_not_contain("Usage: hashsum"); +} diff --git a/tests/fixtures/sha224sum/input.txt b/tests/fixtures/sha224sum/input.txt new file mode 100644 index 000000000..8c01d89ae --- /dev/null +++ b/tests/fixtures/sha224sum/input.txt @@ -0,0 +1 @@ +hello, world \ No newline at end of file diff --git a/tests/fixtures/hashsum/sha224.checkfile b/tests/fixtures/sha224sum/sha224.checkfile similarity index 100% rename from tests/fixtures/hashsum/sha224.checkfile rename to tests/fixtures/sha224sum/sha224.checkfile diff --git a/tests/fixtures/hashsum/sha224.expected b/tests/fixtures/sha224sum/sha224.expected similarity index 100% rename from tests/fixtures/hashsum/sha224.expected rename to tests/fixtures/sha224sum/sha224.expected diff --git a/tests/tests.rs b/tests/tests.rs index 76deb8c7d..b51ed1c5b 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -80,6 +80,10 @@ mod test_md5sum; #[path = "by-util/test_sha1sum.rs"] mod test_sha1sum; +#[cfg(feature = "sha224sum")] +#[path = "by-util/test_sha224sum.rs"] +mod test_sha224sum; + #[cfg(feature = "cp")] #[path = "by-util/test_cp.rs"] mod test_cp;