1*301dc716SAlex Crichton use std::mem::MaybeUninit; 2b315a0a8SYosh use test_programs::wasi::random; 3b315a0a8SYosh main()4b315a0a8SYoshfn main() { 5*301dc716SAlex Crichton let p1_random_size: usize = std::env::var("TEST_P1_RANDOM_LEN") 6*301dc716SAlex Crichton .map(|v| v.parse().expect("TEST_P1_RANDOM_LEN should be a usize")) 7*301dc716SAlex Crichton .unwrap_or(256); 8*301dc716SAlex Crichton let mut bytes: Box<[MaybeUninit<u8>]> = Box::new_uninit_slice(p1_random_size); 9b315a0a8SYosh unsafe { 10*301dc716SAlex Crichton wasip1::random_get(bytes.as_mut_ptr() as *mut u8, bytes.len()).unwrap(); 11b315a0a8SYosh } 12b315a0a8SYosh 13*301dc716SAlex Crichton assert!(bytes.iter().any(|x| unsafe { x.assume_init() } != 0)); 14b315a0a8SYosh 15*301dc716SAlex Crichton let p2_random_size: u64 = std::env::var("TEST_P2_RANDOM_LEN") 16*301dc716SAlex Crichton .map(|v| v.parse().expect("TEST_P2_RANDOM_LEN should be a u64")) 17*301dc716SAlex Crichton .unwrap_or(256); 18b315a0a8SYosh // Acquired random bytes should be of the expected length. 19*301dc716SAlex Crichton let array = random::random::get_random_bytes(p2_random_size); 20*301dc716SAlex Crichton assert_eq!(array.len(), p2_random_size as usize); 21b315a0a8SYosh 22b315a0a8SYosh // It shouldn't take 100+ tries to get a nonzero random integer. 23b315a0a8SYosh for i in 0.. { 24b315a0a8SYosh if random::random::get_random_u64() == 0 { 25b315a0a8SYosh continue; 26b315a0a8SYosh } 27b315a0a8SYosh assert!(i < 100); 28b315a0a8SYosh break; 29b315a0a8SYosh } 30b315a0a8SYosh 31b315a0a8SYosh // The `insecure_seed` API should return the same result each time. 32b315a0a8SYosh let (a1, b1) = random::insecure_seed::insecure_seed(); 33b315a0a8SYosh let (a2, b2) = random::insecure_seed::insecure_seed(); 34b315a0a8SYosh assert_eq!(a1, a2); 35b315a0a8SYosh assert_eq!(b1, b2); 36b315a0a8SYosh 37*301dc716SAlex Crichton let p2_insecure_random_size: u64 = std::env::var("TEST_P2_INSECURE_RANDOM_LEN") 38*301dc716SAlex Crichton .map(|v| { 39*301dc716SAlex Crichton v.parse() 40*301dc716SAlex Crichton .expect("TEST_P2_INSECURE_RANDOM_LEN should be a u64") 41*301dc716SAlex Crichton }) 42*301dc716SAlex Crichton .unwrap_or(256); 43b315a0a8SYosh // Acquired random bytes should be of the expected length. 44*301dc716SAlex Crichton let array = random::insecure::get_insecure_random_bytes(p2_insecure_random_size); 45*301dc716SAlex Crichton assert_eq!(array.len(), p2_insecure_random_size as usize); 46b315a0a8SYosh 47b315a0a8SYosh // It shouldn't take 100+ tries to get a nonzero random integer. 48b315a0a8SYosh for i in 0.. { 49b315a0a8SYosh if random::insecure::get_insecure_random_u64() == 0 { 50b315a0a8SYosh continue; 51b315a0a8SYosh } 52b315a0a8SYosh assert!(i < 100); 53b315a0a8SYosh break; 54b315a0a8SYosh } 55b315a0a8SYosh } 56