cksum: added tests for --raw with each algorithm, not working for crc, sysv, bsd

This commit is contained in:
D9nni
2024-01-09 23:12:40 +02:00
parent 28b5224725
commit 96271ffa3c
13 changed files with 33 additions and 12 deletions
+14 -5
View File
@@ -5,6 +5,7 @@
// spell-checker:ignore (ToDO) fname, algo
use clap::{crate_version, value_parser, Arg, ArgAction, Command};
use hex::decode;
use hex::encode;
use std::error::Error;
use std::ffi::OsStr;
@@ -151,12 +152,12 @@ fn cksum<'a, I>(mut options: Options, files: I) -> UResult<()>
where
I: Iterator<Item = &'a OsStr>,
{
let files_vec: Vec<_> = files.collect();
if options.raw && files_vec.len() > 1 {
let files: Vec<_> = files.collect();
if options.raw && files.len() > 1 {
return Err(Box::new(CkSumError::RawMultipleFiles));
}
for filename in files_vec {
for filename in files {
let filename = Path::new(filename);
let stdin_buf;
let file_buf;
@@ -175,8 +176,16 @@ where
.map_err_context(|| "failed to read input".to_string())?;
if options.raw {
let bytes_str = sum.parse::<u32>().unwrap().to_be_bytes();
stdout().write_all(&bytes_str)?;
match decode(sum.clone()) {
Ok(bytes) => {
stdout().write_all(&bytes)?;
}
Err(_) => {
//bsd, sysv and crc have output generated without encode()
let bytes = sum.parse::<u32>().unwrap().to_be_bytes();
stdout().write_all(&bytes)?;
}
}
return Ok(());
}
// The BSD checksum output is 5 digit integer
+9 -7
View File
@@ -289,14 +289,16 @@ fn test_length_is_zero() {
#[test]
fn test_raw_single_file() {
new_ucmd!()
.arg("--raw")
.arg("lorem_ipsum.txt")
.succeeds()
.no_stderr()
.stdout_is_fixture_bytes("raw_single_file.expected");
for algo in ALGOS {
new_ucmd!()
.arg("--raw")
.arg("lorem_ipsum.txt")
.arg(format!("--algorithm={algo}"))
.succeeds()
.no_stderr()
.stdout_is_fixture_bytes(format!("raw/{algo}_single_file.expected"));
}
}
#[test]
fn test_raw_multiple_files() {
new_ucmd!()
+1
View File
@@ -0,0 +1 @@
`x fWEtBlW^J0aX´85B9m
+1
View File
@@ -0,0 +1 @@

Binary file not shown.
+1
View File
@@ -0,0 +1 @@
к؈:=m毽(%,
Binary file not shown.
+1
View File
@@ -0,0 +1 @@
÷Ä P 0P gê^ SkE‚þœC[Ù+?
+3
View File
@@ -0,0 +1,3 @@
K荵
2吠鋳マクアト/g胤€コ囹LZ{WZ3Sゥ
H
+1
View File
@@ -0,0 +1 @@
–Td«%VªÕ޼sØšÒ!åYyu)ì¯ÀôfÁ•ÏöÖâÆ– |T,ýBn^Oऊ¡VgºD k!=Íú
+1
View File
@@ -0,0 +1 @@
m)k€] þÒ(ß0»›CyMÔíg@¡p§‚i›Â
+1
View File
@@ -0,0 +1 @@
I