hashsum: Windows: check in binary mode by default and allow --binary/--text whcn checking

This commit is contained in:
Pistonight 2024-10-21 11:59:18 -07:00
parent c4160f2dd7
commit ca6d0eda3d
5 changed files with 71 additions and 13 deletions

View file

@ -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())

View file

@ -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");
}

BIN
tests/fixtures/hashsum/binary.png vendored Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.9 KiB

View file

@ -0,0 +1 @@
ac10c6d06f343e26875366263d486a1e9f71115b9b80f0565902f79e947dca51 binary.png

View file

@ -0,0 +1 @@
ac10c6d06f343e26875366263d486a1e9f71115b9b80f0565902f79e947dca51