diff --git a/src/uu/cksum/src/cksum.rs b/src/uu/cksum/src/cksum.rs index 312e4355f23..a50abada5f6 100644 --- a/src/uu/cksum/src/cksum.rs +++ b/src/uu/cksum/src/cksum.rs @@ -144,9 +144,10 @@ struct Options { algo_name: &'static str, digest: Box, output_bits: usize, - untagged: bool, + tag: bool, // will cover the --untagged option length: Option, output_format: OutputFormat, + asterisk: bool, // if we display an asterisk or not (--binary/--text) } /// Calculate checksum @@ -170,6 +171,8 @@ where let stdin_buf; let file_buf; let not_file = filename == OsStr::new("-"); + + // Handle the file input let mut file = BufReader::new(if not_file { stdin_buf = stdin(); Box::new(stdin_buf) as Box @@ -185,6 +188,7 @@ where }; Box::new(file_buf) as Box }); + let (sum_hex, sz) = digest_read(&mut options.digest, &mut file, options.output_bits) .map_err_context(|| "failed to read input".to_string())?; if filename.is_dir() { @@ -240,7 +244,7 @@ where ), (ALGORITHM_OPTIONS_CRC, true) => println!("{sum} {sz}"), (ALGORITHM_OPTIONS_CRC, false) => println!("{sum} {sz} {}", filename.display()), - (ALGORITHM_OPTIONS_BLAKE2B, _) if !options.untagged => { + (ALGORITHM_OPTIONS_BLAKE2B, _) if options.tag => { if let Some(length) = options.length { // Multiply by 8 here, as we want to print the length in bits. println!("BLAKE2b-{} ({}) = {sum}", length * 8, filename.display()); @@ -249,14 +253,15 @@ where } } _ => { - if options.untagged { - println!("{sum} {}", filename.display()); - } else { + if options.tag { println!( "{} ({}) = {sum}", options.algo_name.to_ascii_uppercase(), filename.display() ); + } else { + let prefix = if options.asterisk { "*" } else { " " }; + println!("{sum} {prefix}{}", filename.display()); } } } @@ -306,6 +311,74 @@ mod options { pub const LENGTH: &str = "length"; pub const RAW: &str = "raw"; pub const BASE64: &str = "base64"; + pub const CHECK: &str = "check"; + pub const TEXT: &str = "text"; + pub const BINARY: &str = "binary"; +} + +/// Determines whether to prompt an asterisk (*) in the output. +/// +/// This function checks the `tag`, `binary`, and `had_reset` flags and returns a boolean +/// indicating whether to prompt an asterisk (*) in the output. +/// It relies on the overrides provided by clap (i.e., `--binary` overrides `--text` and vice versa). +/// Same for `--tag` and `--untagged`. +fn prompt_asterisk(tag: bool, binary: bool, had_reset: bool) -> bool { + if tag { + return false; + } + if had_reset { + return false; + } + binary +} + +/** + * Determine if we had a reset. + * This is basically a hack to support the behavior of cksum + * when we have the following arguments: + * --binary --tag --untagged + * Don't do it with clap because if it struggling with the --overrides_with + * marking the value as set even if not present + */ +fn had_reset(args: &[String]) -> bool { + // Indices where "--binary" or "-b", "--tag", and "--untagged" are found + let binary_index = args.iter().position(|x| x == "--binary" || x == "-b"); + let tag_index = args.iter().position(|x| x == "--tag"); + let untagged_index = args.iter().position(|x| x == "--untagged"); + + // Check if all arguments are present and in the correct order + match (binary_index, tag_index, untagged_index) { + (Some(b), Some(t), Some(u)) => b < t && t < u, + _ => false, + } +} + +/*** + * cksum has a bunch of legacy behavior. + * We handle this in this function to make sure they are self contained + * and "easier" to understand + */ +fn handle_tag_text_binary_flags(matches: &clap::ArgMatches, check: bool) -> UResult<(bool, bool)> { + let untagged: bool = matches.get_flag(options::UNTAGGED); + let tag: bool = matches.get_flag(options::TAG); + let tag: bool = tag || !untagged; + + let text_flag: bool = matches.get_flag(options::TEXT); + let binary_flag: bool = matches.get_flag(options::BINARY); + + let args: Vec = std::env::args().collect(); + let had_reset = had_reset(&args); + + let asterisk: bool = prompt_asterisk(tag, binary_flag, had_reset); + + if (binary_flag || text_flag) && check { + return Err(io::Error::new( + io::ErrorKind::InvalidInput, + "the --binary and --text options are meaningless when verifying checksums", + ) + .into()); + } + Ok((tag, asterisk)) } #[uucore::main] @@ -318,6 +391,8 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { }; let input_length = matches.get_one::(options::LENGTH); + let check = matches.get_flag(options::CHECK); + let length = if let Some(length) = input_length { match length.to_owned() { 0 => None, @@ -358,6 +433,16 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { None }; + let (tag, asterisk) = handle_tag_text_binary_flags(&matches, check)?; + + if ["bsd", "crc", "sysv"].contains(&algo_name) && check { + return Err(io::Error::new( + io::ErrorKind::InvalidInput, + "--check is not supported with --algorithm={bsd,sysv,crc}", + ) + .into()); + } + let (name, algo, bits) = detect_algo(algo_name, length); let output_format = if matches.get_flag(options::RAW) { @@ -373,8 +458,9 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { digest: algo, output_bits: bits, length, - untagged: matches.get_flag(options::UNTAGGED), + tag, output_format, + asterisk, }; match matches.get_many::(options::FILE) { @@ -429,7 +515,8 @@ pub fn uu_app() -> Command { Arg::new(options::TAG) .long(options::TAG) .help("create a BSD style checksum, undo --untagged (default)") - .action(ArgAction::SetTrue), + .action(ArgAction::SetTrue) + .overrides_with(options::UNTAGGED), ) .arg( Arg::new(options::LENGTH) @@ -448,15 +535,115 @@ pub fn uu_app() -> Command { .help("emit a raw binary digest, not hexadecimal") .action(ArgAction::SetTrue), ) + /*.arg( + Arg::new(options::STRICT) + .long(options::STRICT) + .help("exit non-zero for improperly formatted checksum lines") + .action(ArgAction::SetTrue), + )*/ + .arg( + Arg::new(options::CHECK) + .short('c') + .long(options::CHECK) + .help("read hashsums from the FILEs and check them") + .action(ArgAction::SetTrue) + .conflicts_with("tag"), + ) .arg( Arg::new(options::BASE64) .long(options::BASE64) - .short('b') .help("emit a base64 digest, not hexadecimal") .action(ArgAction::SetTrue) // Even though this could easily just override an earlier '--raw', // GNU cksum does not permit these flags to be combined: .conflicts_with(options::RAW), ) + .arg( + Arg::new(options::TEXT) + .long(options::TEXT) + .short('t') + .hide(true) + .overrides_with(options::BINARY) + .action(ArgAction::SetTrue), + ) + .arg( + Arg::new(options::BINARY) + .long(options::BINARY) + .short('b') + .hide(true) + .overrides_with(options::TEXT) + .action(ArgAction::SetTrue), + ) .after_help(AFTER_HELP) } + +#[cfg(test)] +mod tests { + use super::had_reset; + use crate::prompt_asterisk; + + #[test] + fn test_had_reset() { + let args = ["--binary", "--tag", "--untagged"] + .iter() + .map(|&s| s.to_string()) + .collect::>(); + assert!(had_reset(&args)); + + let args = ["-b", "--tag", "--untagged"] + .iter() + .map(|&s| s.to_string()) + .collect::>(); + assert!(had_reset(&args)); + + let args = ["-b", "--binary", "--tag", "--untagged"] + .iter() + .map(|&s| s.to_string()) + .collect::>(); + assert!(had_reset(&args)); + + let args = ["--untagged", "--tag", "--binary"] + .iter() + .map(|&s| s.to_string()) + .collect::>(); + assert!(!had_reset(&args)); + + let args = ["--untagged", "--tag", "-b"] + .iter() + .map(|&s| s.to_string()) + .collect::>(); + assert!(!had_reset(&args)); + + let args = ["--binary", "--tag"] + .iter() + .map(|&s| s.to_string()) + .collect::>(); + assert!(!had_reset(&args)); + + let args = ["--tag", "--untagged"] + .iter() + .map(|&s| s.to_string()) + .collect::>(); + assert!(!had_reset(&args)); + + let args = ["--text", "--untagged"] + .iter() + .map(|&s| s.to_string()) + .collect::>(); + assert!(!had_reset(&args)); + + let args = ["--binary", "--untagged"] + .iter() + .map(|&s| s.to_string()) + .collect::>(); + assert!(!had_reset(&args)); + } + + #[test] + fn test_prompt_asterisk() { + assert!(!prompt_asterisk(true, false, false)); + assert!(!prompt_asterisk(false, false, true)); + assert!(prompt_asterisk(false, true, false)); + assert!(!prompt_asterisk(false, false, false)); + } +} diff --git a/src/uu/hashsum/src/hashsum.rs b/src/uu/hashsum/src/hashsum.rs index d4c5aacbb6a..9e98a353429 100644 --- a/src/uu/hashsum/src/hashsum.rs +++ b/src/uu/hashsum/src/hashsum.rs @@ -481,7 +481,8 @@ fn uu_app_opt_length(command: Command) -> Command { .long("length") .help("digest length in bits; must not exceed the max for the blake2 algorithm (512) and must be a multiple of 8") .value_name("BITS") - .value_parser(parse_bit_num), + .value_parser(parse_bit_num) + .overrides_with("length"), ) } diff --git a/tests/by-util/test_cksum.rs b/tests/by-util/test_cksum.rs index 71b32bdfacf..bed35c95eee 100644 --- a/tests/by-util/test_cksum.rs +++ b/tests/by-util/test_cksum.rs @@ -231,6 +231,17 @@ fn test_untagged_algorithm_single_file() { } } +#[test] +fn test_tag_short() { + new_ucmd!() + .arg("-t") + .arg("--untagged") + .arg("--algorithm=md5") + .arg("lorem_ipsum.txt") + .succeeds() + .stdout_is("cd724690f7dc61775dfac400a71f2caa lorem_ipsum.txt\n"); +} + #[test] fn test_untagged_algorithm_after_tag() { new_ucmd!() @@ -267,6 +278,34 @@ fn test_untagged_algorithm_stdin() { } } +#[test] +fn test_check_algo() { + new_ucmd!() + .arg("-a=bsd") + .arg("--check") + .arg("lorem_ipsum.txt") + .fails() + .no_stdout() + .stderr_contains("cksum: --check is not supported with --algorithm={bsd,sysv,crc}") + .code_is(1); + new_ucmd!() + .arg("-a=sysv") + .arg("--check") + .arg("lorem_ipsum.txt") + .fails() + .no_stdout() + .stderr_contains("cksum: --check is not supported with --algorithm={bsd,sysv,crc}") + .code_is(1); + new_ucmd!() + .arg("-a=crc") + .arg("--check") + .arg("lorem_ipsum.txt") + .fails() + .no_stdout() + .stderr_contains("cksum: --check is not supported with --algorithm={bsd,sysv,crc}") + .code_is(1); +} + #[test] fn test_length_with_wrong_algorithm() { new_ucmd!() @@ -379,15 +418,13 @@ fn test_base64_raw_conflicts() { #[test] fn test_base64_single_file() { for algo in ALGOS { - for base64_option in ["--base64", "-b"] { - new_ucmd!() - .arg(base64_option) - .arg("lorem_ipsum.txt") - .arg(format!("--algorithm={algo}")) - .succeeds() - .no_stderr() - .stdout_is_fixture_bytes(format!("base64/{algo}_single_file.expected")); - } + new_ucmd!() + .arg("--base64") + .arg("lorem_ipsum.txt") + .arg(format!("--algorithm={algo}")) + .succeeds() + .no_stderr() + .stdout_is_fixture_bytes(format!("base64/{algo}_single_file.expected")); } } #[test] @@ -435,6 +472,159 @@ fn test_all_algorithms_fail_on_folder() { } } +#[cfg(unix)] +#[test] +fn test_dev_null() { + let scene = TestScenario::new(util_name!()); + let at = &scene.fixtures; + + at.touch("f"); + + scene + .ucmd() + .arg("--tag") + .arg("--untagged") + .arg("--algorithm=md5") + .arg("/dev/null") + .succeeds() + .stdout_contains("d41d8cd98f00b204e9800998ecf8427e "); +} + +#[test] +fn test_reset_binary() { + let scene = TestScenario::new(util_name!()); + let at = &scene.fixtures; + + at.touch("f"); + + scene + .ucmd() + .arg("--binary") // should disappear because of the following option + .arg("--tag") + .arg("--untagged") + .arg("--algorithm=md5") + .arg(at.subdir.join("f")) + .succeeds() + .stdout_contains("d41d8cd98f00b204e9800998ecf8427e "); +} + +#[ignore = "issue #6375"] +#[test] +fn test_reset_binary_but_set() { + let scene = TestScenario::new(util_name!()); + let at = &scene.fixtures; + + at.touch("f"); + + scene + .ucmd() + .arg("--binary") + .arg("--tag") + .arg("--untagged") + .arg("--binary") + .arg("--algorithm=md5") + .arg(at.subdir.join("f")) + .succeeds() + .stdout_contains("d41d8cd98f00b204e9800998ecf8427e *"); // currently, asterisk=false. It should be true +} + +#[test] +fn test_text_tag() { + let scene = TestScenario::new(util_name!()); + let at = &scene.fixtures; + + at.touch("f"); + + scene + .ucmd() + .arg("--text") // should disappear because of the following option + .arg("--tag") + .arg(at.subdir.join("f")) + .succeeds() + .stdout_contains("4294967295 0 "); +} + +#[test] +fn test_binary_file() { + let scene = TestScenario::new(util_name!()); + let at = &scene.fixtures; + + at.touch("f"); + + scene + .ucmd() + .arg("--untagged") + .arg("-b") + .arg("--algorithm=md5") + .arg(at.subdir.join("f")) + .succeeds() + .stdout_contains("d41d8cd98f00b204e9800998ecf8427e *"); + + scene + .ucmd() + .arg("--tag") + .arg("--untagged") + .arg("--binary") + .arg("--algorithm=md5") + .arg(at.subdir.join("f")) + .succeeds() + .stdout_contains("d41d8cd98f00b204e9800998ecf8427e *"); + + scene + .ucmd() + .arg("--tag") + .arg("--untagged") + .arg("--binary") + .arg("--algorithm=md5") + .arg("raw/blake2b_single_file.expected") + .succeeds() + .stdout_contains("7e297c07ed8e053600092f91bdd1dad7 *"); + + new_ucmd!() + .arg("--tag") + .arg("--untagged") + .arg("--binary") + .arg("--algorithm=md5") + .arg("lorem_ipsum.txt") + .succeeds() + .stdout_is("cd724690f7dc61775dfac400a71f2caa *lorem_ipsum.txt\n"); + + new_ucmd!() + .arg("--untagged") + .arg("--binary") + .arg("--algorithm=md5") + .arg("lorem_ipsum.txt") + .succeeds() + .stdout_is("cd724690f7dc61775dfac400a71f2caa *lorem_ipsum.txt\n"); + + new_ucmd!() + .arg("--binary") + .arg("--untagged") + .arg("--algorithm=md5") + .arg("lorem_ipsum.txt") + .succeeds() + .stdout_is("cd724690f7dc61775dfac400a71f2caa *lorem_ipsum.txt\n"); + + new_ucmd!() + .arg("-a") + .arg("md5") + .arg("--binary") + .arg("--untagged") + .arg("lorem_ipsum.txt") + .succeeds() + .stdout_is("cd724690f7dc61775dfac400a71f2caa *lorem_ipsum.txt\n"); + + new_ucmd!() + .arg("-a") + .arg("md5") + .arg("--binary") + .arg("--tag") + .arg("--untagged") + .arg("lorem_ipsum.txt") + .succeeds() + .stdout_is("cd724690f7dc61775dfac400a71f2caa lorem_ipsum.txt\n"); +} + #[test] fn test_folder_and_file() { let scene = TestScenario::new(util_name!()); @@ -452,3 +642,24 @@ fn test_folder_and_file() { .stderr_contains(format!("cksum: {folder_name}: Is a directory")) .stdout_is_fixture("crc_single_file.expected"); } + +#[test] +fn test_conflicting_options() { + let scene = TestScenario::new(util_name!()); + + let at = &scene.fixtures; + + at.touch("f"); + + scene + .ucmd() + .arg("--binary") + .arg("--check") + .arg("f") + .fails() + .no_stdout() + .stderr_contains( + "cksum: the --binary and --text options are meaningless when verifying checksums", + ) + .code_is(1); +} diff --git a/tests/by-util/test_hashsum.rs b/tests/by-util/test_hashsum.rs index a3a5849ff97..eed265ac5dd 100644 --- a/tests/by-util/test_hashsum.rs +++ b/tests/by-util/test_hashsum.rs @@ -180,6 +180,22 @@ fn test_check_b2sum_length_option_0() { .stdout_only("testf: OK\n"); } +#[test] +fn test_check_b2sum_length_duplicate() { + let scene = TestScenario::new(util_name!()); + let at = &scene.fixtures; + + at.write("testf", "foobar\n"); + + scene + .ccmd("b2sum") + .arg("--length=123") + .arg("--length=128") + .arg("testf") + .succeeds() + .stdout_contains("d6d45901dec53e65d2b55fb6e2ab67b0"); +} + #[test] fn test_check_b2sum_length_option_8() { let scene = TestScenario::new(util_name!());