mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-27 19:17:43 +00:00
hashsum: Windows: check in binary mode by default and allow --binary/--text whcn checking
This commit is contained in:
parent
c4160f2dd7
commit
ca6d0eda3d
5 changed files with 71 additions and 13 deletions
|
@ -219,12 +219,19 @@ pub fn uumain(mut args: impl uucore::Args) -> UResult<()> {
|
||||||
}
|
}
|
||||||
|
|
||||||
if check {
|
if check {
|
||||||
let text_flag = matches.get_flag("text");
|
// on Windows, allow --binary/--text to be used with --check
|
||||||
let binary_flag = matches.get_flag("binary");
|
// 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 {
|
if binary_flag || text_flag {
|
||||||
return Err(ChecksumError::BinaryTextConflict.into());
|
return Err(ChecksumError::BinaryTextConflict.into());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
false
|
||||||
|
};
|
||||||
|
|
||||||
// Execute the checksum validation based on the presence of files or the use of stdin
|
// 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.
|
// 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,
|
strict,
|
||||||
status,
|
status,
|
||||||
warn,
|
warn,
|
||||||
binary_flag,
|
binary,
|
||||||
ignore_missing,
|
ignore_missing,
|
||||||
quiet,
|
quiet,
|
||||||
Some(algo.name),
|
Some(algo.name),
|
||||||
|
@ -297,11 +304,11 @@ mod options {
|
||||||
|
|
||||||
pub fn uu_app_common() -> Command {
|
pub fn uu_app_common() -> Command {
|
||||||
#[cfg(windows)]
|
#[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))]
|
#[cfg(not(windows))]
|
||||||
const BINARY_HELP: &str = "read in binary mode";
|
const BINARY_HELP: &str = "read in binary mode";
|
||||||
#[cfg(windows)]
|
#[cfg(windows)]
|
||||||
const TEXT_HELP: &str = "read in text mode";
|
const TEXT_HELP: &str = "read or check in text mode";
|
||||||
#[cfg(not(windows))]
|
#[cfg(not(windows))]
|
||||||
const TEXT_HELP: &str = "read in text mode (default)";
|
const TEXT_HELP: &str = "read in text mode (default)";
|
||||||
Command::new(uucore::util_name())
|
Command::new(uucore::util_name())
|
||||||
|
|
|
@ -19,19 +19,20 @@ macro_rules! test_digest {
|
||||||
static BITS_ARG: &'static str = concat!("--bits=", stringify!($size));
|
static BITS_ARG: &'static str = concat!("--bits=", stringify!($size));
|
||||||
static EXPECTED_FILE: &'static str = concat!(stringify!($id), ".expected");
|
static EXPECTED_FILE: &'static str = concat!(stringify!($id), ".expected");
|
||||||
static CHECK_FILE: &'static str = concat!(stringify!($id), ".checkfile");
|
static CHECK_FILE: &'static str = concat!(stringify!($id), ".checkfile");
|
||||||
|
static INPUT_FILE: &'static str = "input.txt";
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_single_file() {
|
fn test_single_file() {
|
||||||
let ts = TestScenario::new("hashsum");
|
let ts = TestScenario::new("hashsum");
|
||||||
assert_eq!(ts.fixtures.read(EXPECTED_FILE),
|
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]
|
#[test]
|
||||||
fn test_stdin() {
|
fn test_stdin() {
|
||||||
let ts = TestScenario::new("hashsum");
|
let ts = TestScenario::new("hashsum");
|
||||||
assert_eq!(ts.fixtures.read(EXPECTED_FILE),
|
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]
|
#[test]
|
||||||
|
@ -41,7 +42,7 @@ macro_rules! test_digest {
|
||||||
if DIGEST_ARG == "--b3sum" {
|
if DIGEST_ARG == "--b3sum" {
|
||||||
// Option only available on b3sum
|
// Option only available on b3sum
|
||||||
assert_eq!(format!("{0}\n{0}\n", ts.fixtures.read(EXPECTED_FILE)),
|
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()
|
.succeeds().no_stderr().stdout_str()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -50,7 +51,7 @@ macro_rules! test_digest {
|
||||||
#[test]
|
#[test]
|
||||||
fn test_check() {
|
fn test_check() {
|
||||||
let ts = TestScenario::new("hashsum");
|
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));
|
println!("Check file='{}'", ts.fixtures.read(CHECK_FILE));
|
||||||
|
|
||||||
ts.ucmd()
|
ts.ucmd()
|
||||||
|
@ -64,7 +65,7 @@ macro_rules! test_digest {
|
||||||
fn test_zero() {
|
fn test_zero() {
|
||||||
let ts = TestScenario::new("hashsum");
|
let ts = TestScenario::new("hashsum");
|
||||||
assert_eq!(ts.fixtures.read(EXPECTED_FILE),
|
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")
|
.stdout_contains("foo: OK")
|
||||||
.stderr_contains("WARNING: 1 line is improperly formatted");
|
.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
BIN
tests/fixtures/hashsum/binary.png
vendored
Normal file
Binary file not shown.
After Width: | Height: | Size: 7.9 KiB |
1
tests/fixtures/hashsum/binary.sha256.checkfile
vendored
Normal file
1
tests/fixtures/hashsum/binary.sha256.checkfile
vendored
Normal file
|
@ -0,0 +1 @@
|
||||||
|
ac10c6d06f343e26875366263d486a1e9f71115b9b80f0565902f79e947dca51 binary.png
|
1
tests/fixtures/hashsum/binary.sha256.expected
vendored
Normal file
1
tests/fixtures/hashsum/binary.sha256.expected
vendored
Normal file
|
@ -0,0 +1 @@
|
||||||
|
ac10c6d06f343e26875366263d486a1e9f71115b9b80f0565902f79e947dca51
|
Loading…
Add table
Add a link
Reference in a new issue