diff options
-rw-r--r-- | src/uu/hashsum/src/hashsum.rs | 23 | ||||
-rw-r--r-- | tests/by-util/test_hashsum.rs | 59 | ||||
-rw-r--r-- | tests/fixtures/hashsum/binary.png | bin | 0 -> 8055 bytes | |||
-rw-r--r-- | tests/fixtures/hashsum/binary.sha256.checkfile | 1 | ||||
-rw-r--r-- | tests/fixtures/hashsum/binary.sha256.expected | 1 |
5 files changed, 71 insertions, 13 deletions
diff --git a/src/uu/hashsum/src/hashsum.rs b/src/uu/hashsum/src/hashsum.rs index 304938490a..90c8c8adfa 100644 --- a/src/uu/hashsum/src/hashsum.rs +++ b/src/uu/hashsum/src/hashsum.rs @@ -219,12 +219,19 @@ pub fn uumain(mut args: impl uucore::Args) -> UResult<()> { } if check { - let text_flag = matches.get_flag("text"); - let binary_flag = matches.get_flag("binary"); + // on Windows, allow --binary/--text to be used with --check + // and keep the behavior of defaulting to binary + #[cfg(not(windows))] + let binary = { + let text_flag = matches.get_flag("text"); + let binary_flag = matches.get_flag("binary"); + + if binary_flag || text_flag { + return Err(ChecksumError::BinaryTextConflict.into()); + } - if binary_flag || text_flag { - return Err(ChecksumError::BinaryTextConflict.into()); - } + false + }; // Execute the checksum validation based on the presence of files or the use of stdin // Determine the source of input: a list of files or stdin. @@ -239,7 +246,7 @@ pub fn uumain(mut args: impl uucore::Args) -> UResult<()> { strict, status, warn, - binary_flag, + binary, ignore_missing, quiet, Some(algo.name), @@ -297,11 +304,11 @@ mod options { pub fn uu_app_common() -> Command { #[cfg(windows)] - const BINARY_HELP: &str = "read in binary mode (default)"; + const BINARY_HELP: &str = "read or check in binary mode (default)"; #[cfg(not(windows))] const BINARY_HELP: &str = "read in binary mode"; #[cfg(windows)] - const TEXT_HELP: &str = "read in text mode"; + const TEXT_HELP: &str = "read or check in text mode"; #[cfg(not(windows))] const TEXT_HELP: &str = "read in text mode (default)"; Command::new(uucore::util_name()) diff --git a/tests/by-util/test_hashsum.rs b/tests/by-util/test_hashsum.rs index 533f0b7e79..5965a86ea0 100644 --- a/tests/by-util/test_hashsum.rs +++ b/tests/by-util/test_hashsum.rs @@ -19,19 +19,20 @@ macro_rules! test_digest { static BITS_ARG: &'static str = concat!("--bits=", stringify!($size)); 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("hashsum"); assert_eq!(ts.fixtures.read(EXPECTED_FILE), - get_hash!(ts.ucmd().arg(DIGEST_ARG).arg(BITS_ARG).arg("input.txt").succeeds().no_stderr().stdout_str())); + get_hash!(ts.ucmd().arg(DIGEST_ARG).arg(BITS_ARG).arg(INPUT_FILE).succeeds().no_stderr().stdout_str())); } #[test] fn test_stdin() { let ts = TestScenario::new("hashsum"); assert_eq!(ts.fixtures.read(EXPECTED_FILE), - get_hash!(ts.ucmd().arg(DIGEST_ARG).arg(BITS_ARG).pipe_in_fixture("input.txt").succeeds().no_stderr().stdout_str())); + get_hash!(ts.ucmd().arg(DIGEST_ARG).arg(BITS_ARG).pipe_in_fixture(INPUT_FILE).succeeds().no_stderr().stdout_str())); } #[test] @@ -41,7 +42,7 @@ macro_rules! test_digest { if DIGEST_ARG == "--b3sum" { // Option only available on b3sum assert_eq!(format!("{0}\n{0}\n", ts.fixtures.read(EXPECTED_FILE)), - ts.ucmd().arg(DIGEST_ARG).arg(BITS_ARG).arg("--no-names").arg("input.txt").arg("-").pipe_in_fixture("input.txt") + ts.ucmd().arg(DIGEST_ARG).arg(BITS_ARG).arg("--no-names").arg(INPUT_FILE).arg("-").pipe_in_fixture(INPUT_FILE) .succeeds().no_stderr().stdout_str() ); } @@ -50,7 +51,7 @@ macro_rules! test_digest { #[test] fn test_check() { let ts = TestScenario::new("hashsum"); - println!("File content='{}'", ts.fixtures.read("input.txt")); + println!("File content='{}'", ts.fixtures.read(INPUT_FILE)); println!("Check file='{}'", ts.fixtures.read(CHECK_FILE)); ts.ucmd() @@ -64,7 +65,7 @@ macro_rules! test_digest { fn test_zero() { let ts = TestScenario::new("hashsum"); assert_eq!(ts.fixtures.read(EXPECTED_FILE), - get_hash!(ts.ucmd().arg(DIGEST_ARG).arg(BITS_ARG).arg("--zero").arg("input.txt").succeeds().no_stderr().stdout_str())); + get_hash!(ts.ucmd().arg(DIGEST_ARG).arg(BITS_ARG).arg("--zero").arg(INPUT_FILE).succeeds().no_stderr().stdout_str())); } @@ -1007,3 +1008,51 @@ fn test_check_md5_comment_leading_space() { .stdout_contains("foo: OK") .stderr_contains("WARNING: 1 line is improperly formatted"); } + +#[test] +fn test_sha256_binary() { + let ts = TestScenario::new(util_name!()); + assert_eq!( + ts.fixtures.read("binary.sha256.expected"), + get_hash!(ts + .ucmd() + .arg("--sha256") + .arg("--bits=256") + .arg("binary.png") + .succeeds() + .no_stderr() + .stdout_str()) + ); +} + +#[test] +fn test_sha256_stdin_binary() { + let ts = TestScenario::new(util_name!()); + assert_eq!( + ts.fixtures.read("binary.sha256.expected"), + get_hash!(ts + .ucmd() + .arg("--sha256") + .arg("--bits=256") + .pipe_in_fixture("binary.png") + .succeeds() + .no_stderr() + .stdout_str()) + ); +} + +#[test] +fn test_check_sha256_binary() { + let ts = TestScenario::new(util_name!()); + + ts.ucmd() + .args(&[ + "--sha256", + "--bits=256", + "--check", + "binary.sha256.checkfile", + ]) + .succeeds() + .no_stderr() + .stdout_is("binary.png: OK\n"); +} diff --git a/tests/fixtures/hashsum/binary.png b/tests/fixtures/hashsum/binary.png Binary files differnew file mode 100644 index 0000000000..6c4161338f --- /dev/null +++ b/tests/fixtures/hashsum/binary.png diff --git a/tests/fixtures/hashsum/binary.sha256.checkfile b/tests/fixtures/hashsum/binary.sha256.checkfile new file mode 100644 index 0000000000..87fde491a0 --- /dev/null +++ b/tests/fixtures/hashsum/binary.sha256.checkfile @@ -0,0 +1 @@ +ac10c6d06f343e26875366263d486a1e9f71115b9b80f0565902f79e947dca51 binary.png diff --git a/tests/fixtures/hashsum/binary.sha256.expected b/tests/fixtures/hashsum/binary.sha256.expected new file mode 100644 index 0000000000..3003abf463 --- /dev/null +++ b/tests/fixtures/hashsum/binary.sha256.expected @@ -0,0 +1 @@ +ac10c6d06f343e26875366263d486a1e9f71115b9b80f0565902f79e947dca51
\ No newline at end of file |