Skip to content

Commit

Permalink
fix MSRV
Browse files Browse the repository at this point in the history
  • Loading branch information
newpavlov committed Oct 7, 2024
1 parent bca407f commit e7399d5
Showing 1 changed file with 50 additions and 10 deletions.
60 changes: 50 additions & 10 deletions tests/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use std::mem::MaybeUninit;

use core::mem::MaybeUninit;
use getrandom::{getrandom, getrandom_uninit};

#[cfg(getrandom_browser_test)]
Expand All @@ -24,8 +23,10 @@ fn num_diff_bits(s1: &[u8], s2: &[u8]) -> usize {
.sum()
}

fn uninit_array<const N: usize>() -> [MaybeUninit<u8>; N] {
[const { MaybeUninit::uninit() }; N]
// TODO: use `[const { MaybeUninit::uninit() }; N]` after MSRV is bumped to 1.79+
// or `MaybeUninit::uninit_array`
fn uninit_vec(n: usize) -> Vec<MaybeUninit<u8>> {
vec![MaybeUninit::uninit(); n]
}

// Tests the quality of calling getrandom on two large buffers
Expand All @@ -37,8 +38,8 @@ fn test_diff() {
getrandom(&mut v1).unwrap();
getrandom(&mut v2).unwrap();

let mut t1 = uninit_array::<N>();
let mut t2 = uninit_array::<N>();
let mut t1 = uninit_vec(N);
let mut t2 = uninit_vec(N);
let r1 = getrandom_uninit(&mut t1).unwrap();
let r2 = getrandom_uninit(&mut t2).unwrap();
assert_eq!(r1.len(), N);
Expand All @@ -54,18 +55,19 @@ fn test_diff() {
assert!(d2 < 4500);
}

// Tests the quality of calling getrandom repeatedly on small buffers
#[test]
fn test_small() {
let mut buf1 = [0u8; 64];
let mut buf2 = [0u8; 64];
const N: usize = 64;
// For each buffer size, get at least 256 bytes and check that between
// 3 and 5 bits per byte differ. Probability of failure:
// ~ 2^(-91) = 64 * 2 * CDF[BinomialDistribution[8*256, 0.5], 3*256]
for size in 1..=64 {
for size in 1..=N {
let mut num_bytes = 0;
let mut diff_bits = 0;
while num_bytes < 256 {
let mut buf1 = [0u8; N];
let mut buf2 = [0u8; N];

let s1 = &mut buf1[..size];
let s2 = &mut buf2[..size];

Expand All @@ -80,12 +82,50 @@ fn test_small() {
}
}

// Tests the quality of calling getrandom repeatedly on small buffers
#[test]
fn test_small_uninit() {
const N: usize = 64;
// For each buffer size, get at least 256 bytes and check that between
// 3 and 5 bits per byte differ. Probability of failure:
// ~ 2^(-91) = 64 * 2 * CDF[BinomialDistribution[8*256, 0.5], 3*256]
for size in 1..=N {
let mut num_bytes = 0;
let mut diff_bits = 0;
while num_bytes < 256 {
let mut buf1 = uninit_vec(N);
let mut buf2 = uninit_vec(N);

let s1 = &mut buf1[..size];
let s2 = &mut buf2[..size];

let r1 = getrandom_uninit(s1).unwrap();
let r2 = getrandom_uninit(s2).unwrap();
assert_eq!(r1.len(), size);
assert_eq!(r2.len(), size);

num_bytes += size;
diff_bits += num_diff_bits(r1, r2);
}
assert!(diff_bits > 3 * num_bytes);
assert!(diff_bits < 5 * num_bytes);
}
}

#[test]
fn test_huge() {
let mut huge = [0u8; 100_000];
getrandom(&mut huge).unwrap();
}

#[test]
fn test_huge_uninit() {
const N: usize = 100_000;
let mut huge = uninit_vec(N);
let res = getrandom_uninit(&mut huge).unwrap();
assert_eq!(res.len(), N);
}

#[test]
#[cfg_attr(
target_arch = "wasm32",
Expand Down

0 comments on commit e7399d5

Please sign in to comment.