1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-28 03:27:44 +00:00

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

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

View file

@ -289,14 +289,16 @@ fn test_length_is_zero() {
#[test]
fn test_raw_single_file() {
for algo in ALGOS {
new_ucmd!()
.arg("--raw")
.arg("lorem_ipsum.txt")
.arg(format!("--algorithm={algo}"))
.succeeds()
.no_stderr()
.stdout_is_fixture_bytes("raw_single_file.expected");
.stdout_is_fixture_bytes(format!("raw/{algo}_single_file.expected"));
}
}
#[test]
fn test_raw_multiple_files() {
new_ucmd!()

View file

@ -0,0 +1 @@
<0E><><EFBFBD><EFBFBD><EFBFBD>`<60>x<EFBFBD> <0B>f<><1E>W<EFBFBD><57><EFBFBD>Et<45>B<><42>l<EFBFBD><6C>W^J<>0<EFBFBD>aX´<08><>8<EFBFBD><38><EFBFBD><EFBFBD><EFBFBD>5<15>B<EFBFBD><42>9m<11><>

View file

@ -0,0 +1 @@
<1F>

Binary file not shown.

View file

@ -0,0 +1 @@
<EFBFBD>к<>؈:=<18>m毽(%,<2C><>

Binary file not shown.

View file

@ -0,0 +1 @@
÷Ä P 0P gê^ <09>SkEþœC[Ù+?

View file

@ -0,0 +1,3 @@
K荵
2吠鋳<19><EFBFBD>マクアト/g胤€コ囹LZ{WZ3Sゥ
H

View file

@ -0,0 +1 @@
Td«%VªÕ޼sØšÒ!åYyu)ì¯ÀôfÁ•ÏöÖâÆ |T,ýBn^Oऊ¡VgºD k!=Í<03>ú

View file

@ -0,0 +1 @@
m)k€] þÒ(ß0<C39F>»CyMÔíg@¡iÂ

View file

@ -0,0 +1 @@
I