Merge pull request #286 from chalice19/mcookie_fix_max_size

mcookie: fix max size
This commit is contained in:
Daniel Hofstetter
2025-04-09 10:39:14 +02:00
committed by GitHub
2 changed files with 45 additions and 4 deletions

View File

@@ -29,6 +29,9 @@ mod options {
const ABOUT: &str = help_about!("mcookie.md");
const USAGE: &str = help_usage!("mcookie.md");
const RANDOM_BYTES: usize = 128;
const MAX_DEFAULT: u64 = 4096;
#[uucore::main]
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
let matches: clap::ArgMatches = uu_app().try_get_matches_from(args)?;
@@ -43,13 +46,19 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
let max_size = if let Some(size_str) = matches.get_one::<String>(options::MAX_SIZE) {
match Size::parse(size_str) {
Ok(size) => Some(size.size_bytes()),
Ok(size) => {
let mut s = size.size_bytes();
if s == 0 {
s = MAX_DEFAULT;
}
Some(s)
}
Err(_) => {
return Err(USimpleError::new(1, "Failed to parse max-size value"));
}
}
} else {
None
Some(MAX_DEFAULT)
};
let mut hasher = Md5::new();
@@ -107,7 +116,6 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
hasher.update(&buffer);
}
const RANDOM_BYTES: usize = 128;
let mut rng = rand::rng();
let mut rand_bytes = [0u8; RANDOM_BYTES];
rng.fill_bytes(&mut rand_bytes);

View File

@@ -79,7 +79,7 @@ fn test_char_device_input() {
.arg("/dev/zero")
.succeeds();
res_verbose.stderr_contains("Got 1024 bytes from /dev/zero");
res_verbose.stderr_contains("Got 4096 bytes from /dev/zero");
res_verbose.stderr_contains("Got 128 bytes from randomness source"); // Ensure internal randomness is still added
let stdout_verbose = res_verbose.stdout_str().trim_end();
@@ -177,3 +177,36 @@ fn test_not_existing_file() {
file2.path().to_str().unwrap()
));
}
#[test]
fn test_max_size_limits() {
let mut file = NamedTempFile::new().unwrap();
const CONTENT: [u8; 5500] = [1; 5500];
file.write_all(&CONTENT).unwrap();
let res_default = new_ucmd!()
.arg("--verbose")
.arg("-f")
.arg(file.path())
.succeeds();
// Ensure we only read up to 4096 bytes
res_default.stderr_contains(format!(
"Got 4096 bytes from {}",
file.path().to_str().unwrap()
));
let res_zero = new_ucmd!()
.arg("--verbose")
.arg("-f")
.arg(file.path())
.arg("-m")
.arg("0")
.succeeds();
// Ensure we read up 4096 bytes
res_zero.stderr_contains(format!(
"Got 4096 bytes from {}",
file.path().to_str().unwrap()
));
}