|
7 | 7 | ))]
|
8 | 8 |
|
9 | 9 | use wasm_bindgen_test::wasm_bindgen_test as test;
|
10 |
| -#[cfg(feature = "test-in-browser")] |
11 |
| -wasm_bindgen_test::wasm_bindgen_test_configure!(run_in_browser); |
12 | 10 |
|
13 |
| -use core::{ |
14 |
| - num::NonZeroU32, |
15 |
| - sync::atomic::{AtomicU8, Ordering}, |
16 |
| -}; |
| 11 | +use core::num::NonZeroU32; |
17 | 12 | use getrandom::{getrandom, register_custom_getrandom, Error};
|
18 | 13 |
|
19 | 14 | fn len7_err() -> Error {
|
20 | 15 | NonZeroU32::new(Error::INTERNAL_START + 7).unwrap().into()
|
21 | 16 | }
|
22 | 17 |
|
23 | 18 | fn super_insecure_rng(buf: &mut [u8]) -> Result<(), Error> {
|
| 19 | + // `getrandom` guarantees it will not call any implementation if the output |
| 20 | + // buffer is empty. |
| 21 | + assert!(!buf.is_empty()); |
24 | 22 | // Length 7 buffers return a custom error
|
25 | 23 | if buf.len() == 7 {
|
26 | 24 | return Err(len7_err());
|
27 | 25 | }
|
28 |
| - // Otherwise, increment an atomic counter |
29 |
| - static COUNTER: AtomicU8 = AtomicU8::new(0); |
| 26 | + // Otherwise, fill bytes based on input length |
| 27 | + let mut start = buf.len() as u8; |
30 | 28 | for b in buf {
|
31 |
| - *b = COUNTER.fetch_add(1, Ordering::Relaxed); |
| 29 | + *b = start; |
| 30 | + start = start.wrapping_mul(3); |
32 | 31 | }
|
33 | 32 | Ok(())
|
34 | 33 | }
|
35 | 34 |
|
36 | 35 | register_custom_getrandom!(super_insecure_rng);
|
37 | 36 |
|
| 37 | +use getrandom::getrandom as getrandom_impl; |
| 38 | +mod common; |
| 39 | + |
38 | 40 | #[test]
|
39 | 41 | fn custom_rng_output() {
|
40 | 42 | let mut buf = [0u8; 4];
|
41 | 43 | assert_eq!(getrandom(&mut buf), Ok(()));
|
42 |
| - assert_eq!(buf, [0, 1, 2, 3]); |
| 44 | + assert_eq!(buf, [4, 12, 36, 108]); |
| 45 | + |
| 46 | + let mut buf = [0u8; 3]; |
43 | 47 | assert_eq!(getrandom(&mut buf), Ok(()));
|
44 |
| - assert_eq!(buf, [4, 5, 6, 7]); |
| 48 | + assert_eq!(buf, [3, 9, 27]); |
45 | 49 | }
|
46 | 50 |
|
47 | 51 | #[test]
|
|
0 commit comments