Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 15 additions & 8 deletions src/uu/hashsum/src/hashsum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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),
Expand Down Expand Up @@ -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())
Expand Down
59 changes: 54 additions & 5 deletions tests/by-util/test_hashsum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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()
);
}
Expand All @@ -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()
Expand All @@ -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()));
}


Expand Down Expand Up @@ -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");
}
Binary file added tests/fixtures/hashsum/binary.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions tests/fixtures/hashsum/binary.sha256.checkfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ac10c6d06f343e26875366263d486a1e9f71115b9b80f0565902f79e947dca51 binary.png
1 change: 1 addition & 0 deletions tests/fixtures/hashsum/binary.sha256.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ac10c6d06f343e26875366263d486a1e9f71115b9b80f0565902f79e947dca51