From 75da5aed07e7d2fa6d4903385fcc125f2948b0f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Przemys=C5=82aw=20Fuchs?= Date: Tue, 16 Aug 2022 19:44:15 +0200 Subject: [PATCH 1/2] dd: Error message of invalid args is matched with GNU --- src/uu/dd/src/dd.rs | 2 +- src/uu/dd/src/parseargs.rs | 14 ++++++-- tests/by-util/test_dd.rs | 70 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 83 insertions(+), 3 deletions(-) diff --git a/src/uu/dd/src/dd.rs b/src/uu/dd/src/dd.rs index 94da4b7d3dd..4b3ad44349d 100644 --- a/src/uu/dd/src/dd.rs +++ b/src/uu/dd/src/dd.rs @@ -146,7 +146,7 @@ impl Input { } opts.open(fname) - .map_err_context(|| "failed to open input file".to_string())? + .map_err_context(|| format!("failed to open {}", fname.quote()))? }; // The --skip and --iseek flags are additive. On a file, they seek. diff --git a/src/uu/dd/src/parseargs.rs b/src/uu/dd/src/parseargs.rs index 96ac55d75d2..701134ee14c 100644 --- a/src/uu/dd/src/parseargs.rs +++ b/src/uu/dd/src/parseargs.rs @@ -35,6 +35,7 @@ pub enum ParseError { IbsOutOfRange, ObsOutOfRange, CbsOutOfRange, + InvalidNumber(String), } impl ParseError { @@ -56,6 +57,7 @@ impl ParseError { Self::IbsOutOfRange => Self::IbsOutOfRange, Self::ObsOutOfRange => Self::ObsOutOfRange, Self::CbsOutOfRange => Self::CbsOutOfRange, + Self::InvalidNumber(_) => Self::InvalidNumber(s), } } } @@ -79,7 +81,12 @@ impl std::fmt::Display for ParseError { write!(f, "Only one ov conv=excl or conv=nocreat may be specified") } Self::FlagNoMatch(arg) => { - write!(f, "Unrecognized iflag=FLAG or oflag=FLAG -> {}", arg) + // Additional message about 'dd --help' is displayed only in this situation. + write!( + f, + "invalid input flag: ‘{}’\nTry 'dd --help' for more information.", + arg + ) } Self::ConvFlagNoMatch(arg) => { write!(f, "Unrecognized conv=CONV -> {}", arg) @@ -115,6 +122,9 @@ impl std::fmt::Display for ParseError { Self::Unimplemented(arg) => { write!(f, "feature not implemented on this system -> {}", arg) } + Self::InvalidNumber(arg) => { + write!(f, "invalid number: ‘{}’", arg) + } } } } @@ -389,7 +399,7 @@ fn parse_bytes_no_x(s: &str) -> Result { (None, None, None) => match uucore::parse_size::parse_size(s) { Ok(n) => (n, 1), Err(ParseSizeError::InvalidSuffix(s)) | Err(ParseSizeError::ParseFailure(s)) => { - return Err(ParseError::MultiplierStringParseFailure(s)) + return Err(ParseError::InvalidNumber(s)) } Err(ParseSizeError::SizeTooBig(s)) => { return Err(ParseError::MultiplierStringOverflow(s)) diff --git a/tests/by-util/test_dd.rs b/tests/by-util/test_dd.rs index 8393248ef88..40aa561236e 100644 --- a/tests/by-util/test_dd.rs +++ b/tests/by-util/test_dd.rs @@ -1187,3 +1187,73 @@ fn test_final_stats_si_iec() { let s = result.stderr_str(); assert!(s.starts_with("2+0 records in\n2+0 records out\n1024 bytes (1 KB, 1024 B) copied,")); } + +#[test] +fn test_cbs_invalid_arg_gnu_compatibility() { + let result = new_ucmd!().args(&["cbs="]).pipe_in("").fails(); + + result.stderr_is("dd: invalid number: ‘’"); + + let result = new_ucmd!().args(&["cbs=29d"]).pipe_in("").fails(); + + result.stderr_is("dd: invalid number: ‘29d’"); +} + +#[test] +fn test_invalid_number_arg_gnu_compatibility() { + let commands = vec!["bs", "cbs", "count", "ibs", "obs", "seek", "skip"]; + + for command in commands { + new_ucmd!() + .args(&[format!("{command}=")]) + .fails() + .stderr_is("dd: invalid number: ‘’"); + + new_ucmd!() + .args(&[format!("{command}=29d")]) + .fails() + .stderr_is("dd: invalid number: ‘29d’"); + } +} + +#[test] +fn test_invalid_flag_arg_gnu_compatibility() { + let commands = vec!["iflag", "oflag"]; + + for command in commands { + new_ucmd!() + .args(&[format!("{command}=")]) + .fails() + .stderr_is("dd: invalid input flag: ‘’\nTry 'dd --help' for more information."); + + new_ucmd!() + .args(&[format!("{command}=29d")]) + .fails() + .stderr_is("dd: invalid input flag: ‘29d’\nTry 'dd --help' for more information."); + } +} + +#[test] +fn test_invalid_file_arg_gnu_compatibility() { + new_ucmd!() + .args(&[format!("if=")]) + .fails() + .stderr_is("dd: failed to open '': No such file or directory"); + + new_ucmd!() + .args(&[format!("if=81as9bn8as9g302az8ns9.pdf.zip.pl.com")]) + .fails() + .stderr_is( + "dd: failed to open '81as9bn8as9g302az8ns9.pdf.zip.pl.com': No such file or directory", + ); + + new_ucmd!() + .args(&[format!("of=")]) + .fails() + .stderr_is("dd: failed to open '': No such file or directory"); + + new_ucmd!() + .args(&[format!("of=81as9bn8as9g302az8ns9.pdf.zip.pl.com")]) + .pipe_in("") + .succeeds(); +} From a7290c489b4ffaaa3784c741b4aa38519ba46153 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Przemys=C5=82aw=20Fuchs?= Date: Tue, 16 Aug 2022 19:59:05 +0200 Subject: [PATCH 2/2] dd: Fixed code style in testing module --- tests/by-util/test_dd.rs | 27 ++++++++------------------- 1 file changed, 8 insertions(+), 19 deletions(-) diff --git a/tests/by-util/test_dd.rs b/tests/by-util/test_dd.rs index 40aa561236e..a3a422ad819 100644 --- a/tests/by-util/test_dd.rs +++ b/tests/by-util/test_dd.rs @@ -1188,29 +1188,18 @@ fn test_final_stats_si_iec() { assert!(s.starts_with("2+0 records in\n2+0 records out\n1024 bytes (1 KB, 1024 B) copied,")); } -#[test] -fn test_cbs_invalid_arg_gnu_compatibility() { - let result = new_ucmd!().args(&["cbs="]).pipe_in("").fails(); - - result.stderr_is("dd: invalid number: ‘’"); - - let result = new_ucmd!().args(&["cbs=29d"]).pipe_in("").fails(); - - result.stderr_is("dd: invalid number: ‘29d’"); -} - #[test] fn test_invalid_number_arg_gnu_compatibility() { let commands = vec!["bs", "cbs", "count", "ibs", "obs", "seek", "skip"]; for command in commands { new_ucmd!() - .args(&[format!("{command}=")]) + .args(&[format!("{}=", command)]) .fails() .stderr_is("dd: invalid number: ‘’"); new_ucmd!() - .args(&[format!("{command}=29d")]) + .args(&[format!("{}=29d", command)]) .fails() .stderr_is("dd: invalid number: ‘29d’"); } @@ -1222,12 +1211,12 @@ fn test_invalid_flag_arg_gnu_compatibility() { for command in commands { new_ucmd!() - .args(&[format!("{command}=")]) + .args(&[format!("{}=", command)]) .fails() .stderr_is("dd: invalid input flag: ‘’\nTry 'dd --help' for more information."); new_ucmd!() - .args(&[format!("{command}=29d")]) + .args(&[format!("{}=29d", command)]) .fails() .stderr_is("dd: invalid input flag: ‘29d’\nTry 'dd --help' for more information."); } @@ -1236,24 +1225,24 @@ fn test_invalid_flag_arg_gnu_compatibility() { #[test] fn test_invalid_file_arg_gnu_compatibility() { new_ucmd!() - .args(&[format!("if=")]) + .args(&["if="]) .fails() .stderr_is("dd: failed to open '': No such file or directory"); new_ucmd!() - .args(&[format!("if=81as9bn8as9g302az8ns9.pdf.zip.pl.com")]) + .args(&["if=81as9bn8as9g302az8ns9.pdf.zip.pl.com"]) .fails() .stderr_is( "dd: failed to open '81as9bn8as9g302az8ns9.pdf.zip.pl.com': No such file or directory", ); new_ucmd!() - .args(&[format!("of=")]) + .args(&["of="]) .fails() .stderr_is("dd: failed to open '': No such file or directory"); new_ucmd!() - .args(&[format!("of=81as9bn8as9g302az8ns9.pdf.zip.pl.com")]) + .args(&["of=81as9bn8as9g302az8ns9.pdf.zip.pl.com"]) .pipe_in("") .succeeds(); }