diff --git a/Cargo.lock b/Cargo.lock index a1b35d585..8269a9be2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -627,6 +627,7 @@ dependencies = [ "uu_rmdir", "uu_runcon", "uu_seq", + "uu_sha1sum", "uu_shred", "uu_shuf", "uu_sleep", @@ -3984,6 +3985,18 @@ dependencies = [ "uucore", ] +[[package]] +name = "uu_sha1sum" +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 429959b28..d2e72e7ac 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -88,6 +88,7 @@ feat_common_core = [ "cksum", "b2sum", "md5sum", + "sha1sum", "comm", "cp", "csplit", @@ -441,6 +442,7 @@ chroot = { optional = true, version = "0.6.0", package = "uu_chroot", path = "sr cksum = { optional = true, version = "0.6.0", package = "uu_cksum", path = "src/uu/cksum" } 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" } 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 b28ef8903..053441c74 100644 --- a/GNUmakefile +++ b/GNUmakefile @@ -96,7 +96,6 @@ SELINUX_PROGS := \ runcon HASHSUM_PROGS := \ - sha1sum \ sha224sum \ sha256sum \ sha384sum \ diff --git a/build.rs b/build.rs index 39a26a807..6b0e177fd 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("sha1sum", map_value.clone()); phf_map.entry("sha224sum", map_value.clone()); phf_map.entry("sha256sum", map_value.clone()); phf_map.entry("sha384sum", map_value.clone()); diff --git a/src/common/validation.rs b/src/common/validation.rs index 773505bed..ffadfd917 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 - "sha1sum" | "sha224sum" | "sha256sum" | "sha384sum" | "sha512sum" => "hashsum", + "sha224sum" | "sha256sum" | "sha384sum" | "sha512sum" => "hashsum", "dir" => "ls", // dir is an alias for ls diff --git a/src/uu/sha1sum/Cargo.toml b/src/uu/sha1sum/Cargo.toml new file mode 100644 index 000000000..001bddd69 --- /dev/null +++ b/src/uu/sha1sum/Cargo.toml @@ -0,0 +1,38 @@ +[package] +name = "uu_sha1sum" +description = "sha1sum ~ (uutils) Print or check the SHA1 checksums" +repository = "https://github.com/uutils/coreutils/tree/main/src/uu/sha1sum" +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/sha1sum.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 = "sha1sum" +path = "src/main.rs" diff --git a/src/uu/sha1sum/LICENSE b/src/uu/sha1sum/LICENSE new file mode 120000 index 000000000..5853aaea5 --- /dev/null +++ b/src/uu/sha1sum/LICENSE @@ -0,0 +1 @@ +../../../LICENSE \ No newline at end of file diff --git a/src/uu/sha1sum/locales/en-US.ftl b/src/uu/sha1sum/locales/en-US.ftl new file mode 100644 index 000000000..378b8f8d4 --- /dev/null +++ b/src/uu/sha1sum/locales/en-US.ftl @@ -0,0 +1,2 @@ +sha1sum-about = Print or check the SHA1 checksums +sha1sum-usage = sha1sum [OPTIONS] [FILE]... diff --git a/src/uu/sha1sum/locales/fr-FR.ftl b/src/uu/sha1sum/locales/fr-FR.ftl new file mode 100644 index 000000000..865bd8071 --- /dev/null +++ b/src/uu/sha1sum/locales/fr-FR.ftl @@ -0,0 +1,2 @@ +sha1sum-about = Afficher le SHA1 et la taille de chaque fichier +sha1sum-usage = sha1sum [OPTION]... [FICHIER]... diff --git a/src/uu/sha1sum/src/main.rs b/src/uu/sha1sum/src/main.rs new file mode 100644 index 000000000..18d80cfde --- /dev/null +++ b/src/uu/sha1sum/src/main.rs @@ -0,0 +1 @@ +uucore::bin!(uu_sha1sum); diff --git a/src/uu/sha1sum/src/sha1sum.rs b/src/uu/sha1sum/src/sha1sum.rs new file mode 100644 index 000000000..e715c7966 --- /dev/null +++ b/src/uu/sha1sum/src/sha1sum.rs @@ -0,0 +1 @@ +uu_checksum_common::declare_standalone!("sha1sum", uucore::checksum::AlgoKind::Sha1); diff --git a/src/uucore/src/lib/lib.rs b/src/uucore/src/lib/lib.rs index d793867c5..203a7ebb2 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 - "sha1sum" | "sha224sum" | "sha256sum" | "sha384sum" | "sha512sum" => "hashsum", + "sha224sum" | "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 6036501f4..2b740eff8 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"].contains(&util_name) { + if ["cksum", "b2sum", "md5sum", "sha1sum"].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 26125dc5e..646724d66 100644 --- a/tests/by-util/test_hashsum.rs +++ b/tests/by-util/test_hashsum.rs @@ -201,7 +201,6 @@ macro_rules! test_digest_with_len { }; } -test_digest! {sha1, sha1} test_digest! {b3sum, b3sum} test_digest! {shake128, shake128} test_digest! {shake256, shake256} @@ -215,6 +214,7 @@ test_digest_with_len! {sha3_256, sha3, 256} test_digest_with_len! {sha3_384, sha3, 384} test_digest_with_len! {sha3_512, sha3, 512} +#[ignore = "moved to standalone"] #[test] fn test_check_sha1() { // To make sure that #3815 doesn't happen again @@ -414,6 +414,7 @@ fn test_check_b2sum_verify() { .stdout_only("BLAKE2b-128 (a) = b93e0fc7bb21633c08bba07c5e71dc00\n"); } +#[ignore = "moved to standalone"] #[test] fn test_check_file_not_found_warning() { let scene = TestScenario::new(util_name!()); diff --git a/tests/by-util/test_sha1sum.rs b/tests/by-util/test_sha1sum.rs new file mode 100644 index 000000000..d0e7f6f3d --- /dev/null +++ b/tests/by-util/test_sha1sum.rs @@ -0,0 +1,165 @@ +// 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; +use uutests::util::TestScenario; +use uutests::util_name; +// 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! {sha1} + +#[test] +fn test_check_sha1() { + // To make sure that #3815 doesn't happen again + let scene = TestScenario::new(util_name!()); + let at = &scene.fixtures; + + at.write("testf", "foobar\n"); + at.write( + "testf.sha1", + "988881adc9fc3655077dc2d4d757d480b5ea0e11 testf\n", + ); + scene + .ccmd("sha1sum") + .arg("-c") + .arg(at.subdir.join("testf.sha1")) + .succeeds() + .stdout_is("testf: OK\n") + .stderr_is(""); +} + +#[test] +fn test_check_file_not_found_warning() { + let scene = TestScenario::new(util_name!()); + let at = &scene.fixtures; + + at.write("testf", "foobar\n"); + at.write( + "testf.sha1", + "988881adc9fc3655077dc2d4d757d480b5ea0e11 testf\n", + ); + at.remove("testf"); + scene + .ccmd("sha1sum") + .arg("-c") + .arg(at.subdir.join("testf.sha1")) + .fails() + .stdout_is("testf: FAILED open or read\n") + .stderr_is("sha1sum: testf: No such file or directory\nsha1sum: WARNING: 1 listed file could not be read\n"); +} + +#[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); + new_ucmd!().arg("--tag").arg("--text").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: sha1sum") + .stdout_does_not_contain("Usage: hashsum"); +} diff --git a/tests/fixtures/sha1sum/input.txt b/tests/fixtures/sha1sum/input.txt new file mode 100644 index 000000000..8c01d89ae --- /dev/null +++ b/tests/fixtures/sha1sum/input.txt @@ -0,0 +1 @@ +hello, world \ No newline at end of file diff --git a/tests/fixtures/hashsum/sha1.checkfile b/tests/fixtures/sha1sum/sha1.checkfile similarity index 100% rename from tests/fixtures/hashsum/sha1.checkfile rename to tests/fixtures/sha1sum/sha1.checkfile diff --git a/tests/fixtures/hashsum/sha1.expected b/tests/fixtures/sha1sum/sha1.expected similarity index 100% rename from tests/fixtures/hashsum/sha1.expected rename to tests/fixtures/sha1sum/sha1.expected diff --git a/tests/tests.rs b/tests/tests.rs index 78225ac97..76deb8c7d 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -76,6 +76,10 @@ mod test_b2sum; #[path = "by-util/test_md5sum.rs"] mod test_md5sum; +#[cfg(feature = "sha1sum")] +#[path = "by-util/test_sha1sum.rs"] +mod test_sha1sum; + #[cfg(feature = "cp")] #[path = "by-util/test_cp.rs"] mod test_cp;