Skip to content

Commit

Permalink
Test getrandom_uninit
Browse files Browse the repository at this point in the history
Signed-off-by: Joe Richey <[email protected]>
  • Loading branch information
josephlr committed Jun 12, 2024
1 parent e350e3b commit 43749b7
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
extern crate std;

use crate::Error;
use std::{sync::mpsc, thread, vec, vec::Vec};
use std::{mem::MaybeUninit, sync::mpsc, thread, vec, vec::Vec};

#[cfg(feature = "test-in-browser")]
wasm_bindgen_test::wasm_bindgen_test_configure!(run_in_browser);
Expand All @@ -29,6 +29,15 @@ impl Byte for u8 {
v
}
}
impl Byte for MaybeUninit<u8> {
fn make_vec(len: usize, fill: FillFn<MaybeUninit<u8>>) -> Vec<u8> {
// Ensure that we always get uninitialized memory.
let mut v = Vec::with_capacity(len);
fill(v.spare_capacity_mut()).unwrap();
unsafe { v.set_len(len) }
v
}
}

// For calls of size `len`, count the number of bits which differ between calls
// and check that between 3 and 5 bits per byte differ. Probability of failure:
Expand Down Expand Up @@ -112,3 +121,16 @@ macro_rules! define_tests {
pub(crate) use define_tests;

define_tests!(crate::getrandom);
mod uninit {
use super::*;

fn wrapper(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
let dest_ptr = dest.as_ptr().cast::<u8>();
let res = crate::getrandom_uninit(dest)?;
// Ensure that the output points to the same bytes as the input.
assert_eq!(res.as_ptr(), dest_ptr);
assert_eq!(res.len(), dest.len());
Ok(())
}
super::define_tests!(wrapper);
}

0 comments on commit 43749b7

Please sign in to comment.