Skip to content

Commit bae28dd

Browse files
committed
Cleanup common tests
Signed-off-by: Joe Richey <[email protected]>
1 parent f345775 commit bae28dd

File tree

5 files changed

+123
-133
lines changed

5 files changed

+123
-133
lines changed

src/lib.rs

+7
Original file line numberDiff line numberDiff line change
@@ -404,3 +404,10 @@ pub fn getrandom_uninit(dest: &mut [MaybeUninit<u8>]) -> Result<&mut [u8], Error
404404
// since it returned `Ok`.
405405
Ok(unsafe { slice_assume_init_mut(dest) })
406406
}
407+
408+
// Don't run normal unit tests when testing custom getrandom
409+
#[cfg(all(
410+
test,
411+
not(all(target_family = "wasm", target_os = "unknown", feature = "custom"))
412+
))]
413+
pub(crate) mod tests;

src/tests.rs

+116
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
//! Common tests and testing utilities
2+
extern crate std;
3+
4+
use crate::Error;
5+
use std::{mem::MaybeUninit, sync::mpsc, thread, vec, vec::Vec};
6+
7+
#[cfg(feature = "test-in-browser")]
8+
wasm_bindgen_test::wasm_bindgen_test_configure!(run_in_browser);
9+
10+
fn num_diff_bits(s1: &[u8], s2: &[u8]) -> usize {
11+
assert_eq!(s1.len(), s2.len());
12+
s1.iter()
13+
.zip(s2.iter())
14+
.map(|(a, b)| (a ^ b).count_ones() as usize)
15+
.sum()
16+
}
17+
18+
// Type of the functions we want to test
19+
type FillFn<B> = fn(&mut [B]) -> Result<(), Error>;
20+
21+
// Helper trait for testing different `FillFn`s.
22+
pub(crate) trait Byte: Sized + 'static {
23+
fn make_vec(len: usize, fill: FillFn<Self>) -> Vec<u8>;
24+
}
25+
impl Byte for u8 {
26+
fn make_vec(len: usize, fill: FillFn<u8>) -> Vec<u8> {
27+
let mut v = vec![0; len];
28+
fill(&mut v).unwrap();
29+
v
30+
}
31+
}
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+
}
41+
42+
// For calls of size `len`, count the number of bits which differ between calls
43+
// and check that between 3 and 5 bits per byte differ. Probability of failure:
44+
// ~ 10^(-30) = 2 * CDF[BinomialDistribution[8*256, 0.5], 3*256]
45+
pub(crate) fn check_bits<B: Byte>(len: usize, fill: FillFn<B>) {
46+
let mut num_bytes = 0;
47+
let mut diff_bits = 0;
48+
while num_bytes < 256 {
49+
let v1 = B::make_vec(len, fill);
50+
let v2 = B::make_vec(len, fill);
51+
52+
num_bytes += len;
53+
diff_bits += num_diff_bits(&v1, &v2);
54+
}
55+
56+
// When the custom feature is enabled, don't check RNG quality.
57+
assert!(diff_bits > 3 * num_bytes);
58+
assert!(diff_bits < 5 * num_bytes);
59+
}
60+
61+
pub(crate) fn check_multithreading<B: Byte>(fill: FillFn<B>) {
62+
let mut txs = vec![];
63+
for _ in 0..20 {
64+
let (tx, rx) = mpsc::channel();
65+
txs.push(tx);
66+
67+
thread::spawn(move || {
68+
// wait until all the tasks are ready to go.
69+
rx.recv().unwrap();
70+
for _ in 0..100 {
71+
check_bits(1000, fill);
72+
thread::yield_now();
73+
}
74+
});
75+
}
76+
77+
// start all the tasks
78+
for tx in txs.iter() {
79+
tx.send(()).unwrap();
80+
}
81+
}
82+
83+
macro_rules! define_tests {
84+
($fill:expr) => {
85+
#[cfg(all(target_family = "wasm", target_os = "unknown",))]
86+
use wasm_bindgen_test::wasm_bindgen_test as test;
87+
88+
#[test]
89+
fn fill_zero() {
90+
$fill(&mut []).unwrap();
91+
}
92+
#[test]
93+
fn fill_small() {
94+
for len in 1..=64 {
95+
crate::tests::check_bits(len, $fill);
96+
}
97+
}
98+
#[test]
99+
fn fill_large() {
100+
crate::tests::check_bits(1_000, $fill);
101+
}
102+
#[test]
103+
fn fill_huge() {
104+
crate::tests::check_bits(1_000_000, $fill);
105+
}
106+
// On WASM, the thread API always fails/panics.
107+
#[test]
108+
#[cfg_attr(target_family = "wasm", ignore)]
109+
fn multithreading() {
110+
crate::tests::check_multithreading($fill)
111+
}
112+
};
113+
}
114+
pub(crate) use define_tests;
115+
116+
define_tests!(crate::getrandom);

tests/common/mod.rs

-100
This file was deleted.

tests/normal.rs

-11
This file was deleted.

tests/rdrand.rs

-22
This file was deleted.

0 commit comments

Comments
 (0)