1 use test_programs::wasi::random;
2 
3 fn main() {
4     let mut bytes = [0_u8; 256];
5     unsafe {
6         wasip1::random_get(bytes.as_mut_ptr(), bytes.len()).unwrap();
7     }
8 
9     assert!(bytes.iter().any(|x| *x != 0));
10 
11     // Acquired random bytes should be of the expected length.
12     let array = random::random::get_random_bytes(100);
13     assert_eq!(array.len(), 100);
14 
15     // It shouldn't take 100+ tries to get a nonzero random integer.
16     for i in 0.. {
17         if random::random::get_random_u64() == 0 {
18             continue;
19         }
20         assert!(i < 100);
21         break;
22     }
23 
24     // The `insecure_seed` API should return the same result each time.
25     let (a1, b1) = random::insecure_seed::insecure_seed();
26     let (a2, b2) = random::insecure_seed::insecure_seed();
27     assert_eq!(a1, a2);
28     assert_eq!(b1, b2);
29 
30     // Acquired random bytes should be of the expected length.
31     let array = random::insecure::get_insecure_random_bytes(100);
32     assert_eq!(array.len(), 100);
33 
34     // It shouldn't take 100+ tries to get a nonzero random integer.
35     for i in 0.. {
36         if random::insecure::get_insecure_random_u64() == 0 {
37             continue;
38         }
39         assert!(i < 100);
40         break;
41     }
42 }
43