use bashkit::Bash;
#[tokio::test]
async fn urandom_no_replacement_chars() {
let mut bash = Bash::new();
let result = bash
.exec("head -c 100 /dev/urandom | od -A n -t x1 | tr -d ' \\n'")
.await
.unwrap();
let hex = result.stdout.trim();
assert!(
!hex.contains("efbfbd"),
"Output should not contain UTF-8 replacement chars: {}",
&hex[..hex.len().min(60)]
);
}
#[tokio::test]
async fn urandom_head_char_count() {
let mut bash = Bash::new();
for n in [1, 4, 8, 16, 32] {
let result = bash
.exec(&format!("head -c {n} /dev/urandom | wc -m"))
.await
.unwrap();
let count: usize = result.stdout.trim().parse().unwrap_or(0);
assert_eq!(
count, n,
"head -c {n} /dev/urandom | wc -m should produce exactly {n} chars"
);
}
}
#[tokio::test]
async fn urandom_tr_filter_alphanumeric() {
let mut bash = Bash::new();
let result = bash
.exec("LC_ALL=C tr -dc 'a-z0-9' < /dev/urandom | head -c 8")
.await
.unwrap();
let output = result.stdout.trim();
assert_eq!(
output.len(),
8,
"Should produce exactly 8 chars, got {}: {:?}",
output.len(),
output
);
assert!(
output
.chars()
.all(|c| c.is_ascii_lowercase() || c.is_ascii_digit()),
"All chars should be a-z0-9, got: {:?}",
output
);
}