Skip to content

Commit 43749b7

Browse files
committed
Test getrandom_uninit
Signed-off-by: Joe Richey <[email protected]>
1 parent e350e3b commit 43749b7

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

src/tests.rs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
extern crate std;
33

44
use crate::Error;
5-
use std::{sync::mpsc, thread, vec, vec::Vec};
5+
use std::{mem::MaybeUninit, sync::mpsc, thread, vec, vec::Vec};
66

77
#[cfg(feature = "test-in-browser")]
88
wasm_bindgen_test::wasm_bindgen_test_configure!(run_in_browser);
@@ -29,6 +29,15 @@ impl Byte for u8 {
2929
v
3030
}
3131
}
32+
impl Byte for MaybeUninit<u8> {
33+
fn make_vec(len: usize, fill: FillFn<MaybeUninit<u8>>) -> Vec<u8> {
34+
// Ensure that we always get uninitialized memory.
35+
let mut v = Vec::with_capacity(len);
36+
fill(v.spare_capacity_mut()).unwrap();
37+
unsafe { v.set_len(len) }
38+
v
39+
}
40+
}
3241

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

114123
define_tests!(crate::getrandom);
124+
mod uninit {
125+
use super::*;
126+
127+
fn wrapper(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
128+
let dest_ptr = dest.as_ptr().cast::<u8>();
129+
let res = crate::getrandom_uninit(dest)?;
130+
// Ensure that the output points to the same bytes as the input.
131+
assert_eq!(res.as_ptr(), dest_ptr);
132+
assert_eq!(res.len(), dest.len());
133+
Ok(())
134+
}
135+
super::define_tests!(wrapper);
136+
}

0 commit comments

Comments
 (0)