mcookie - fix providing /dev/random or /dev/urandom as file leads to infinite loop (#274)

* Update mcookie.rs

* edit doc

* fmt

* fix for unix and non-unix build

* add test case

* refactor

* fmt

* fix
This commit is contained in:
Quang
2025-03-31 00:54:28 -07:00
committed by GitHub
parent 36b3af044c
commit 0113e301e8
2 changed files with 43 additions and 1 deletions

View File

@@ -64,6 +64,31 @@ fn test_seed_files_and_max_size_raw() {
));
}
#[test]
#[cfg(unix)] // Character devices like /dev/zero are a Unix concept
fn test_char_device_input() {
let res_no_limit = new_ucmd!().arg("-f").arg("/dev/zero").succeeds();
let stdout_no_limit = res_no_limit.no_stderr().stdout_str().trim_end();
assert_eq!(stdout_no_limit.len(), 32);
assert!(stdout_no_limit.chars().all(|c| c.is_ascii_hexdigit()));
let res_verbose = new_ucmd!()
.arg("--verbose")
.arg("-f")
.arg("/dev/zero")
.succeeds();
res_verbose.stderr_contains("Got 1024 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();
assert_eq!(stdout_verbose.len(), 32);
assert!(stdout_verbose.chars().all(|c| c.is_ascii_hexdigit()));
assert_ne!(stdout_no_limit, stdout_verbose);
}
#[test]
fn test_seed_files_and_max_size_human_readable() {
let mut file = NamedTempFile::new().unwrap();