add, fix tests
This commit is contained in:
@@ -5,6 +5,8 @@ pub trait WasiRandom {
|
||||
fn get(&self, buf: &mut [u8]) -> Result<(), Error>;
|
||||
}
|
||||
|
||||
/// Implement `WasiRandom` using the `getrandom` crate, which selects your system's best entropy
|
||||
/// source.
|
||||
pub struct GetRandom;
|
||||
|
||||
impl WasiRandom for GetRandom {
|
||||
@@ -14,6 +16,7 @@ impl WasiRandom for GetRandom {
|
||||
}
|
||||
}
|
||||
|
||||
/// Implement `WasiRandom` using a deterministic cycle of bytes.
|
||||
pub struct Deterministic {
|
||||
sequence: RefCell<std::iter::Cycle<std::vec::IntoIter<u8>>>,
|
||||
}
|
||||
@@ -35,3 +38,17 @@ impl WasiRandom for Deterministic {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::*;
|
||||
#[test]
|
||||
fn deterministic() {
|
||||
let det = Deterministic::new(vec![1, 2, 3, 4]);
|
||||
let mut buf = vec![0; 1024];
|
||||
det.get(&mut buf).expect("get randomness");
|
||||
for (ix, b) in buf.iter().enumerate() {
|
||||
assert_eq!(*b, (ix % 4) as u8 + 1)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user