Skip to content

Commit 93d58b1

Browse files
authored
Merge pull request #7261 from RenjiSann/cksum-6375
cksum: Fix #6375 and un-ignore now passing tests
2 parents 717a692 + f2cf08b commit 93d58b1

File tree

3 files changed

+38
-131
lines changed

3 files changed

+38
-131
lines changed

src/uu/cksum/src/cksum.rs

Lines changed: 22 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -205,61 +205,33 @@ mod options {
205205
pub const ZERO: &str = "zero";
206206
}
207207

208-
/// Determines whether to prompt an asterisk (*) in the output.
209-
///
210-
/// This function checks the `tag`, `binary`, and `had_reset` flags and returns a boolean
211-
/// indicating whether to prompt an asterisk (*) in the output.
212-
/// It relies on the overrides provided by clap (i.e., `--binary` overrides `--text` and vice versa).
213-
/// Same for `--tag` and `--untagged`.
214-
fn prompt_asterisk(tag: bool, binary: bool, had_reset: bool) -> bool {
215-
if tag {
216-
return false;
217-
}
218-
if had_reset {
219-
return false;
220-
}
221-
binary
222-
}
223-
224-
/**
225-
* Determine if we had a reset.
226-
* This is basically a hack to support the behavior of cksum
227-
* when we have the following arguments:
228-
* --binary --tag --untagged
229-
* Don't do it with clap because if it struggling with the --overrides_with
230-
* marking the value as set even if not present
231-
*/
232-
fn had_reset(args: &[OsString]) -> bool {
233-
// Indices where "--binary" or "-b", "--tag", and "--untagged" are found
234-
let binary_index = args.iter().position(|x| x == "--binary" || x == "-b");
235-
let tag_index = args.iter().position(|x| x == "--tag");
236-
let untagged_index = args.iter().position(|x| x == "--untagged");
237-
238-
// Check if all arguments are present and in the correct order
239-
match (binary_index, tag_index, untagged_index) {
240-
(Some(b), Some(t), Some(u)) => b < t && t < u,
241-
_ => false,
242-
}
243-
}
244-
245208
/***
246209
* cksum has a bunch of legacy behavior.
247210
* We handle this in this function to make sure they are self contained
248211
* and "easier" to understand
249212
*/
250-
fn handle_tag_text_binary_flags(matches: &clap::ArgMatches) -> UResult<(bool, bool)> {
251-
let untagged = matches.get_flag(options::UNTAGGED);
252-
let tag = matches.get_flag(options::TAG);
253-
let tag = tag || !untagged;
254-
255-
let binary_flag = matches.get_flag(options::BINARY);
256-
257-
let args: Vec<OsString> = std::env::args_os().collect();
258-
let had_reset = had_reset(&args);
259-
260-
let asterisk = prompt_asterisk(tag, binary_flag, had_reset);
213+
fn handle_tag_text_binary_flags<S: AsRef<OsStr>>(
214+
args: impl Iterator<Item = S>,
215+
) -> UResult<(bool, bool)> {
216+
let mut tag = true;
217+
let mut binary = false;
218+
219+
// --binary, --tag and --untagged are tight together: none of them
220+
// conflicts with each other but --tag will reset "binary" and set "tag".
221+
222+
for arg in args {
223+
let arg = arg.as_ref();
224+
if arg == "-b" || arg == "--binary" {
225+
binary = true;
226+
} else if arg == "--tag" {
227+
tag = true;
228+
binary = false;
229+
} else if arg == "--untagged" {
230+
tag = false;
231+
}
232+
}
261233

262-
Ok((tag, asterisk))
234+
Ok((tag, !tag && binary))
263235
}
264236

265237
#[uucore::main]
@@ -336,7 +308,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
336308
return perform_checksum_validation(files.iter().copied(), algo_option, length, opts);
337309
}
338310

339-
let (tag, asterisk) = handle_tag_text_binary_flags(&matches)?;
311+
let (tag, asterisk) = handle_tag_text_binary_flags(std::env::args_os())?;
340312

341313
let algo = detect_algo(algo_name, length)?;
342314
let line_ending = LineEnding::from_zero_flag(matches.get_flag(options::ZERO));
@@ -501,75 +473,7 @@ pub fn uu_app() -> Command {
501473

502474
#[cfg(test)]
503475
mod tests {
504-
use super::had_reset;
505476
use crate::calculate_blake2b_length;
506-
use crate::prompt_asterisk;
507-
use std::ffi::OsString;
508-
509-
#[test]
510-
fn test_had_reset() {
511-
let args = ["--binary", "--tag", "--untagged"]
512-
.iter()
513-
.map(|&s| s.into())
514-
.collect::<Vec<OsString>>();
515-
assert!(had_reset(&args));
516-
517-
let args = ["-b", "--tag", "--untagged"]
518-
.iter()
519-
.map(|&s| s.into())
520-
.collect::<Vec<OsString>>();
521-
assert!(had_reset(&args));
522-
523-
let args = ["-b", "--binary", "--tag", "--untagged"]
524-
.iter()
525-
.map(|&s| s.into())
526-
.collect::<Vec<OsString>>();
527-
assert!(had_reset(&args));
528-
529-
let args = ["--untagged", "--tag", "--binary"]
530-
.iter()
531-
.map(|&s| s.into())
532-
.collect::<Vec<OsString>>();
533-
assert!(!had_reset(&args));
534-
535-
let args = ["--untagged", "--tag", "-b"]
536-
.iter()
537-
.map(|&s| s.into())
538-
.collect::<Vec<OsString>>();
539-
assert!(!had_reset(&args));
540-
541-
let args = ["--binary", "--tag"]
542-
.iter()
543-
.map(|&s| s.into())
544-
.collect::<Vec<OsString>>();
545-
assert!(!had_reset(&args));
546-
547-
let args = ["--tag", "--untagged"]
548-
.iter()
549-
.map(|&s| s.into())
550-
.collect::<Vec<OsString>>();
551-
assert!(!had_reset(&args));
552-
553-
let args = ["--text", "--untagged"]
554-
.iter()
555-
.map(|&s| s.into())
556-
.collect::<Vec<OsString>>();
557-
assert!(!had_reset(&args));
558-
559-
let args = ["--binary", "--untagged"]
560-
.iter()
561-
.map(|&s| s.into())
562-
.collect::<Vec<OsString>>();
563-
assert!(!had_reset(&args));
564-
}
565-
566-
#[test]
567-
fn test_prompt_asterisk() {
568-
assert!(!prompt_asterisk(true, false, false));
569-
assert!(!prompt_asterisk(false, false, true));
570-
assert!(prompt_asterisk(false, true, false));
571-
assert!(!prompt_asterisk(false, false, false));
572-
}
573477

574478
#[test]
575479
fn test_calculate_length() {

src/uucore/src/lib/features/checksum.rs

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -692,7 +692,7 @@ fn identify_algo_name_and_length(
692692
line_info: &LineInfo,
693693
algo_name_input: Option<&str>,
694694
last_algo: &mut Option<String>,
695-
) -> Option<(String, Option<usize>)> {
695+
) -> Result<(String, Option<usize>), LineCheckError> {
696696
let algo_from_line = line_info.algo_name.clone().unwrap_or_default();
697697
let algorithm = algo_from_line.to_lowercase();
698698
*last_algo = Some(algo_from_line);
@@ -701,18 +701,25 @@ fn identify_algo_name_and_length(
701701
// (for example SHA1 (f) = d...)
702702
// Also handle the case cksum -s sm3 but the file contains other formats
703703
if algo_name_input.is_some() && algo_name_input != Some(&algorithm) {
704-
return None;
704+
return Err(LineCheckError::ImproperlyFormatted);
705705
}
706706

707707
if !SUPPORTED_ALGORITHMS.contains(&algorithm.as_str()) {
708708
// Not supported algo, leave early
709-
return None;
709+
return Err(LineCheckError::ImproperlyFormatted);
710710
}
711711

712712
let bytes = if let Some(bitlen) = line_info.algo_bit_len {
713-
if bitlen % 8 != 0 {
714-
// The given length is wrong
715-
return None;
713+
if algorithm != ALGORITHM_OPTIONS_BLAKE2B || bitlen % 8 != 0 {
714+
// Either
715+
// the algo based line is provided with a bit length
716+
// with an algorithm that does not support it (only Blake2B does).
717+
//
718+
// eg: MD5-128 (foo.txt) = fffffffff
719+
// ^ This is illegal
720+
// OR
721+
// the given length is wrong because it's not a multiple of 8.
722+
return Err(LineCheckError::ImproperlyFormatted);
716723
}
717724
Some(bitlen / 8)
718725
} else if algorithm == ALGORITHM_OPTIONS_BLAKE2B {
@@ -722,7 +729,7 @@ fn identify_algo_name_and_length(
722729
None
723730
};
724731

725-
Some((algorithm, bytes))
732+
Ok((algorithm, bytes))
726733
}
727734

728735
/// Given a filename and an algorithm, compute the digest and compare it with
@@ -773,8 +780,7 @@ fn process_algo_based_line(
773780
let filename_to_check = line_info.filename.as_slice();
774781

775782
let (algo_name, algo_byte_len) =
776-
identify_algo_name_and_length(line_info, cli_algo_name, last_algo)
777-
.ok_or(LineCheckError::ImproperlyFormatted)?;
783+
identify_algo_name_and_length(line_info, cli_algo_name, last_algo)?;
778784

779785
// If the digest bitlen is known, we can check the format of the expected
780786
// checksum with it.

tests/by-util/test_cksum.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -602,7 +602,6 @@ fn test_reset_binary() {
602602
.stdout_contains("d41d8cd98f00b204e9800998ecf8427e ");
603603
}
604604

605-
#[ignore = "issue #6375"]
606605
#[test]
607606
fn test_reset_binary_but_set() {
608607
let scene = TestScenario::new(util_name!());
@@ -619,7 +618,7 @@ fn test_reset_binary_but_set() {
619618
.arg("--algorithm=md5")
620619
.arg(at.subdir.join("f"))
621620
.succeeds()
622-
.stdout_contains("d41d8cd98f00b204e9800998ecf8427e *"); // currently, asterisk=false. It should be true
621+
.stdout_contains("d41d8cd98f00b204e9800998ecf8427e *");
623622
}
624623

625624
#[test]
@@ -1137,7 +1136,6 @@ fn test_cksum_garbage() {
11371136
.stderr_contains("check-file: no properly formatted checksum lines found");
11381137
}
11391138

1140-
#[ignore = "Should fail on bits"]
11411139
#[test]
11421140
fn test_md5_bits() {
11431141
let (at, mut ucmd) = at_and_ucmd!();
@@ -1188,7 +1186,6 @@ fn test_bsd_case() {
11881186
.stderr_contains("f: no properly formatted checksum lines found");
11891187
}
11901188

1191-
#[ignore = "Different output"]
11921189
#[test]
11931190
fn test_blake2d_tested_with_sha1() {
11941191
let (at, mut ucmd) = at_and_ucmd!();

0 commit comments

Comments
 (0)