1 // Test that a custom handler works on wasm32-unknown-unknown
3 target_arch
= "wasm32",
9 use wasm_bindgen_test
::wasm_bindgen_test
as test
;
11 use core
::num
::NonZeroU32
;
12 use getrandom
::{getrandom, register_custom_getrandom, Error}
;
14 fn len7_err() -> Error
{
15 NonZeroU32
::new(Error
::INTERNAL_START
+ 7).unwrap().into()
18 fn super_insecure_rng(buf
: &mut [u8]) -> Result
<(), Error
> {
19 // `getrandom` guarantees it will not call any implementation if the output
21 assert
!(!buf
.is_empty());
22 // Length 7 buffers return a custom error
24 return Err(len7_err());
26 // Otherwise, fill bytes based on input length
27 let mut start
= buf
.len() as u8;
30 start
= start
.wrapping_mul(3);
35 register_custom_getrandom
!(super_insecure_rng
);
37 use getrandom
::getrandom
as getrandom_impl
;
41 fn custom_rng_output() {
42 let mut buf
= [0u8; 4];
43 assert_eq
!(getrandom(&mut buf
), Ok(()));
44 assert_eq
!(buf
, [4, 12, 36, 108]);
46 let mut buf
= [0u8; 3];
47 assert_eq
!(getrandom(&mut buf
), Ok(()));
48 assert_eq
!(buf
, [3, 9, 27]);
53 assert_eq
!(getrandom(&mut [0; 7]), Err(len7_err()));