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

basenc: handle '--base32 .' arg

This commit is contained in:
Sylvestre Ledru 2024-01-05 21:15:26 +01:00
parent 4f33a375cd
commit 8d24036f5c
3 changed files with 20 additions and 3 deletions

View file

@ -6,7 +6,7 @@
use std::io::{stdout, Read, Write};
use uucore::display::Quotable;
use uucore::encoding::{wrap_print, Data, Format};
use uucore::encoding::{wrap_print, Data, EncodeError, Format};
use uucore::error::{FromIo, UResult, USimpleError, UUsageError};
use uucore::format_usage;
@ -174,6 +174,7 @@ pub fn handle_input<R: Read>(
wrap_print(&data, &s);
Ok(())
}
Err(EncodeError::InvalidInput) => Err(USimpleError::new(1, "error: invalid input")),
Err(_) => Err(USimpleError::new(
1,
"error: invalid input (length must be multiple of 4 characters)",

View file

@ -27,6 +27,7 @@ pub enum DecodeError {
pub enum EncodeError {
Z85InputLenNotMultipleOf4,
InvalidInput,
}
pub type DecodeResult = Result<Vec<u8>, DecodeError>;
@ -148,8 +149,10 @@ impl<R: Read> Data<R> {
pub fn encode(&mut self) -> Result<String, EncodeError> {
let mut buf: Vec<u8> = vec![];
self.input.read_to_end(&mut buf).unwrap();
encode(self.format, buf.as_slice())
match self.input.read_to_end(&mut buf) {
Ok(_) => encode(self.format, buf.as_slice()),
Err(_) => Err(EncodeError::InvalidInput),
}
}
}

View file

@ -18,3 +18,16 @@ fn test_z85_not_padded() {
.fails()
.stderr_only("basenc: error: invalid input (length must be multiple of 4 characters)\n");
}
#[test]
fn test_invalid_input() {
let error_message = if cfg!(windows) {
"basenc: .: Permission denied"
} else {
"basenc: error: invalid input\n"
};
new_ucmd!()
.args(&["--base32", "."])
.fails()
.stderr_only(error_message);
}