1
- use super :: getrandom_impl;
1
+ use super :: { getrandom_impl, getrandom_uninit_impl} ;
2
+ use core:: mem:: MaybeUninit ;
3
+ #[ cfg( not( feature = "custom" ) ) ]
4
+ use getrandom:: Error ;
2
5
3
6
#[ cfg( all( target_arch = "wasm32" , target_os = "unknown" ) ) ]
4
7
use wasm_bindgen_test:: wasm_bindgen_test as test;
5
8
6
9
#[ cfg( feature = "test-in-browser" ) ]
7
10
wasm_bindgen_test:: wasm_bindgen_test_configure!( run_in_browser) ;
8
11
12
+ #[ cfg( not( feature = "custom" ) ) ]
13
+ fn wrapped_getrandom ( dest : & mut [ u8 ] ) -> Result < & mut [ u8 ] , Error > {
14
+ getrandom_impl ( dest) . map ( |( ) | dest)
15
+ }
16
+
17
+ // Test that APIs are happy with zero-length requests
9
18
#[ test]
10
19
fn test_zero ( ) {
11
- // Test that APIs are happy with zero-length requests
12
- getrandom_impl ( & mut [ 0u8 ; 0 ] ) . unwrap ( ) ;
20
+ getrandom_impl ( & mut [ ] ) . unwrap ( ) ;
21
+ }
22
+ #[ test]
23
+ fn test_zero_uninit ( ) {
24
+ getrandom_uninit_impl ( & mut [ ] ) . unwrap ( ) ;
13
25
}
14
26
15
27
// Return the number of bits in which s1 and s2 differ
@@ -23,52 +35,82 @@ fn num_diff_bits(s1: &[u8], s2: &[u8]) -> usize {
23
35
}
24
36
25
37
// Tests the quality of calling getrandom on two large buffers
26
- # [ test ]
38
+
27
39
#[ cfg( not( feature = "custom" ) ) ]
28
- fn test_diff ( ) {
29
- let mut v1 = [ 0u8 ; 1000 ] ;
30
- getrandom_impl ( & mut v1) . unwrap ( ) ;
40
+ fn test_diff_large < T : Copy > ( initial : T , f : impl Fn ( & mut [ T ] ) -> Result < & mut [ u8 ] , Error > ) {
41
+ let mut v1 = [ initial ; 1000 ] ;
42
+ let r1 = f ( & mut v1) . unwrap ( ) ;
31
43
32
- let mut v2 = [ 0u8 ; 1000 ] ;
33
- getrandom_impl ( & mut v2) . unwrap ( ) ;
44
+ let mut v2 = [ initial ; 1000 ] ;
45
+ let r2 = f ( & mut v2) . unwrap ( ) ;
34
46
35
47
// Between 3.5 and 4.5 bits per byte should differ. Probability of failure:
36
48
// ~ 2^(-94) = 2 * CDF[BinomialDistribution[8000, 0.5], 3500]
37
- let d = num_diff_bits ( & v1 , & v2 ) ;
49
+ let d = num_diff_bits ( r1 , r2 ) ;
38
50
assert ! ( d > 3500 ) ;
39
51
assert ! ( d < 4500 ) ;
40
52
}
41
53
42
- // Tests the quality of calling getrandom repeatedly on small buffers
54
+ # [ cfg ( not ( feature = "custom" ) ) ]
43
55
#[ test]
56
+ fn test_large ( ) {
57
+ test_diff_large ( 0u8 , wrapped_getrandom) ;
58
+ }
59
+
44
60
#[ cfg( not( feature = "custom" ) ) ]
45
- fn test_small ( ) {
61
+ #[ test]
62
+ fn test_large_uninit ( ) {
63
+ test_diff_large ( MaybeUninit :: uninit ( ) , getrandom_uninit_impl) ;
64
+ }
65
+
66
+ // Tests the quality of calling getrandom repeatedly on small buffers
67
+
68
+ #[ cfg( not( feature = "custom" ) ) ]
69
+ fn test_diff_small < T : Copy > ( initial : T , f : impl Fn ( & mut [ T ] ) -> Result < & mut [ u8 ] , Error > ) {
46
70
// For each buffer size, get at least 256 bytes and check that between
47
71
// 3 and 5 bits per byte differ. Probability of failure:
48
72
// ~ 2^(-91) = 64 * 2 * CDF[BinomialDistribution[8*256, 0.5], 3*256]
49
73
for size in 1 ..=64 {
50
74
let mut num_bytes = 0 ;
51
75
let mut diff_bits = 0 ;
52
76
while num_bytes < 256 {
53
- let mut s1 = vec ! [ 0u8 ; size] ;
54
- getrandom_impl ( & mut s1) . unwrap ( ) ;
55
- let mut s2 = vec ! [ 0u8 ; size] ;
56
- getrandom_impl ( & mut s2) . unwrap ( ) ;
77
+ let mut s1 = vec ! [ initial ; size] ;
78
+ let r1 = f ( & mut s1) . unwrap ( ) ;
79
+ let mut s2 = vec ! [ initial ; size] ;
80
+ let r2 = f ( & mut s2) . unwrap ( ) ;
57
81
58
82
num_bytes += size;
59
- diff_bits += num_diff_bits ( & s1 , & s2 ) ;
83
+ diff_bits += num_diff_bits ( r1 , r2 ) ;
60
84
}
61
85
assert ! ( diff_bits > 3 * num_bytes) ;
62
86
assert ! ( diff_bits < 5 * num_bytes) ;
63
87
}
64
88
}
65
89
90
+ #[ cfg( not( feature = "custom" ) ) ]
91
+ #[ test]
92
+ fn test_small ( ) {
93
+ test_diff_small ( 0u8 , wrapped_getrandom) ;
94
+ }
95
+
96
+ #[ cfg( not( feature = "custom" ) ) ]
97
+ #[ test]
98
+ fn test_small_unnit ( ) {
99
+ test_diff_small ( MaybeUninit :: uninit ( ) , getrandom_uninit_impl) ;
100
+ }
101
+
66
102
#[ test]
67
103
fn test_huge ( ) {
68
104
let mut huge = [ 0u8 ; 100_000 ] ;
69
105
getrandom_impl ( & mut huge) . unwrap ( ) ;
70
106
}
71
107
108
+ #[ test]
109
+ fn test_huge_uninit ( ) {
110
+ let mut huge = [ MaybeUninit :: uninit ( ) ; 100_000 ] ;
111
+ getrandom_uninit_impl ( & mut huge) . unwrap ( ) ;
112
+ }
113
+
72
114
// On WASM, the thread API always fails/panics
73
115
#[ cfg( not( target_arch = "wasm32" ) ) ]
74
116
#[ test]
0 commit comments