use std::cmp::min;
use std::fmt;
use std::mem::size_of;
use rand_core::RngCore;
pub struct FuzzRng<'a> {
buf: &'a [u8],
}
impl<'a> FuzzRng<'a> {
pub fn new(buf: &'a [u8]) -> FuzzRng<'a> {
FuzzRng { buf }
}
pub fn into_inner(self) -> &'a [u8] {
let FuzzRng { buf } = self;
buf
}
}
impl RngCore for FuzzRng<'_> {
fn next_u32(&mut self) -> u32 {
let mut buf = [0u8; size_of::<u32>()];
self.fill_bytes(&mut buf);
u32::from_ne_bytes(buf)
}
fn next_u64(&mut self) -> u64 {
let mut buf = [0u8; size_of::<u64>()];
self.fill_bytes(&mut buf);
u64::from_ne_bytes(buf)
}
fn fill_bytes(&mut self, dest: &mut [u8]) {
let amt = min(self.buf.len(), dest.len());
let (a, b) = self.buf.split_at(amt);
dest[..amt].copy_from_slice(a);
self.buf = b;
if amt < dest.len() {
for b in &mut dest[amt..] {
*b = 0;
}
}
}
}
impl fmt::Debug for FuzzRng<'_> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "FuzzRng {{ {} bytes }}", self.buf.len())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn gen_u32() {
let val = 0xc2a744u32;
let buf = val.to_ne_bytes();
let mut rng = FuzzRng::new(&buf);
assert_eq!(rng.next_u32(), val);
assert_eq!(rng.next_u32(), 0);
}
#[test]
fn gen_u64() {
let val = 0xac75689deeu64;
let buf = val.to_ne_bytes();
let mut rng = FuzzRng::new(&buf);
assert_eq!(rng.next_u64(), val);
assert_eq!(rng.next_u64(), 0);
}
#[test]
fn fill_bytes() {
let buf = &[
0xed, 0x90, 0xf3, 0xa4, 0x8f, 0xbf, 0x6e, 0xdb, 0x68, 0xb9, 0x1f, 0x9a, 0x13, 0xfc,
0x9f, 0xc8, 0x9e, 0xfa, 0x4a, 0x02, 0x5e, 0xc8, 0xb1, 0xe5, 0x2d, 0x59, 0x22, 0x89,
0x10, 0x23, 0xc3, 0x31, 0x6c, 0x42, 0x40, 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0x47, 0xc6, 0x42, 0x71, 0x15, 0xc8,
0x25, 0x1d, 0x94, 0x00, 0x8d, 0x04, 0x37, 0xe7, 0xfe, 0xf6, 0x10, 0x28, 0xe5, 0xb2,
0xef, 0x95, 0xa6, 0x53, 0x20, 0xf8, 0x51, 0xdb, 0x54, 0x99, 0x40, 0x4a, 0x7c, 0xd6,
0x90, 0x4a, 0x55, 0xdc, 0x37, 0xb8, 0xbc, 0x0b, 0xc4, 0x54, 0xd1, 0x9b, 0xb3, 0x8c,
0x09, 0x55, 0x77, 0xf5, 0x1b, 0xa7, 0x36, 0x06, 0x29, 0x4c, 0xa3, 0x26, 0x35, 0x1b,
0x29, 0xa3, 0xa3, 0x45, 0x74, 0xee, 0x0b, 0x78, 0xf8, 0x69, 0x70, 0xa4, 0x1d, 0x11,
0x7a, 0x91, 0xca, 0x4c, 0x83, 0xb3, 0xbf, 0xf6, 0x7f, 0x54, 0xca, 0xdb, 0x1f, 0xc4,
0xd2, 0xb2, 0x23, 0xfa, 0xc0, 0x24, 0x77, 0x74, 0x61, 0x9e, 0x0b, 0x77, 0x49, 0x29,
0xf1, 0xd9, 0xbf, 0xf0, 0x5e, 0x99, 0xa6, 0xf1, 0x00, 0xa4, 0x7f, 0xa0, 0xb1, 0x6b,
0xd8, 0xbe, 0xef, 0xa0, 0xa1, 0xa5, 0x33, 0x9c, 0xc3, 0x95, 0xaa, 0x9f,
];
let mut rng = FuzzRng::new(&buf[..]);
let mut dest = Vec::with_capacity(buf.len());
for chunk in buf.chunks(11) {
dest.resize(chunk.len(), 0);
rng.fill_bytes(&mut dest);
assert_eq!(chunk, &*dest);
}
dest.resize(97, 0x2c);
rng.fill_bytes(&mut dest);
let zero_buf = vec![0; dest.len()];
assert_eq!(zero_buf, dest);
}
}