|
1 |
| -// Test that a custom handler works on wasm32-unknown-unknown |
2 |
| -#![cfg(all( |
3 |
| - target_arch = "wasm32", |
4 |
| - target_os = "unknown", |
5 |
| - feature = "custom", |
6 |
| - not(feature = "js") |
7 |
| -))] |
8 |
| - |
9 |
| -use wasm_bindgen_test::wasm_bindgen_test as test; |
10 |
| - |
11 | 1 | use core::num::NonZeroU32;
|
12 | 2 | use getrandom::{getrandom, register_custom_getrandom, Error};
|
| 3 | +#[cfg(all(target_family = "wasm", target_os = "unknown"))] |
| 4 | +use wasm_bindgen_test::wasm_bindgen_test as test; |
13 | 5 |
|
14 |
| -fn len7_err() -> Error { |
15 |
| - NonZeroU32::new(Error::INTERNAL_START + 7).unwrap().into() |
16 |
| -} |
17 |
| - |
18 |
| -fn super_insecure_rng(buf: &mut [u8]) -> Result<(), Error> { |
| 6 | +const LEN7_CODE: u32 = Error::CUSTOM_START + 7; |
| 7 | +// Returns a custom error if input is length 7, otherwise fills with 0x55. |
| 8 | +fn mock_rng(buf: &mut [u8]) -> Result<(), Error> { |
19 | 9 | // `getrandom` guarantees it will not call any implementation if the output
|
20 | 10 | // buffer is empty.
|
21 | 11 | assert!(!buf.is_empty());
|
22 |
| - // Length 7 buffers return a custom error |
23 | 12 | if buf.len() == 7 {
|
24 |
| - return Err(len7_err()); |
25 |
| - } |
26 |
| - // Otherwise, fill bytes based on input length |
27 |
| - let mut start = buf.len() as u8; |
28 |
| - for b in buf { |
29 |
| - *b = start; |
30 |
| - start = start.wrapping_mul(3); |
| 13 | + return Err(NonZeroU32::new(LEN7_CODE).unwrap().into()); |
31 | 14 | }
|
| 15 | + buf.fill(0x55); |
32 | 16 | Ok(())
|
33 | 17 | }
|
34 | 18 |
|
35 |
| -register_custom_getrandom!(super_insecure_rng); |
36 |
| - |
37 |
| -use getrandom::getrandom as getrandom_impl; |
38 |
| -mod common; |
| 19 | +// Test registering a custom implementation, even on supported platforms. |
| 20 | +register_custom_getrandom!(mock_rng); |
39 | 21 |
|
| 22 | +// Invoking with an empty buffer should never call the custom implementation. |
40 | 23 | #[test]
|
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]); |
45 |
| - |
46 |
| - let mut buf = [0u8; 3]; |
47 |
| - assert_eq!(getrandom(&mut buf), Ok(())); |
48 |
| - assert_eq!(buf, [3, 9, 27]); |
| 24 | +fn custom_empty() { |
| 25 | + getrandom(&mut []).unwrap(); |
49 | 26 | }
|
50 | 27 |
|
| 28 | +// On a supported platform, make sure the custom implementation isn't used. We |
| 29 | +// test on a few common platfroms, rather than duplicating the lib.rs logic. |
| 30 | +#[cfg(any( |
| 31 | + target_os = "linux", |
| 32 | + target_os = "windows", |
| 33 | + target_os = "macos", |
| 34 | + target_os = "espidf", |
| 35 | + target_os = "wasi", |
| 36 | + all(target_family = "wasm", target_os = "unknown", feature = "js"), |
| 37 | +))] |
51 | 38 | #[test]
|
52 |
| -fn rng_err_output() { |
53 |
| - assert_eq!(getrandom(&mut [0; 7]), Err(len7_err())); |
| 39 | +fn custom_not_used() { |
| 40 | + getrandom(&mut [0; 7]).unwrap(); |
| 41 | +} |
| 42 | +// On an unsupported platform, make sure the custom implementation is used. |
| 43 | +#[cfg(all(target_family = "wasm", target_os = "unknown", not(feature = "js")))] |
| 44 | +#[test] |
| 45 | +fn custom_used() { |
| 46 | + let err = getrandom(&mut [0; 7]).unwrap_err(); |
| 47 | + assert_eq!(err.code().get(), LEN7_CODE); |
| 48 | + |
| 49 | + let mut buf = [0; 12]; |
| 50 | + getrandom(&mut buf).unwrap(); |
| 51 | + assert_eq!(buf, [0x55; 12]); |
54 | 52 | }
|
0 commit comments