Skip to content

Commit c83edb6

Browse files
committed
Add quality tests for calling getrandom on small buffers
Signed-off-by: Joe Richey <joerichey@google.com>
1 parent c12f70c commit c83edb6

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

tests/common/mod.rs

+24
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,30 @@ fn test_diff() {
4040
assert!(d < 4500);
4141
}
4242

43+
// Tests the quality of calling getrandom repeatedly on small buffers
44+
#[test]
45+
#[cfg(not(feature = "custom"))]
46+
fn test_small() {
47+
// For each buffer size, get at least 256 bytes and check that between
48+
// 3 and 5 bits per byte differ. Probability of failure:
49+
// ~ 2^(-91) = 64 * 2 * CDF[BinomialDistribution[8*256, 0.5], 3*256]
50+
for size in 1..=64 {
51+
let mut num_bytes = 0;
52+
let mut diff_bits = 0;
53+
while num_bytes < 256 {
54+
let mut s1 = vec![0u8; size];
55+
getrandom_impl(&mut s1).unwrap();
56+
let mut s2 = vec![0u8; size];
57+
getrandom_impl(&mut s2).unwrap();
58+
59+
num_bytes += size;
60+
diff_bits += num_diff_bits(&s1, &s2);
61+
}
62+
assert!(diff_bits > 3 * num_bytes);
63+
assert!(diff_bits < 5 * num_bytes);
64+
}
65+
}
66+
4367
#[test]
4468
fn test_huge() {
4569
let mut huge = [0u8; 100_000];

0 commit comments

Comments
 (0)