Skip to content

Commit 347a300

Browse files
authored
Add test for custom RNG handler (#197)
See #194 This uses the fact that `wasm32-unknown-unknown` is an "unsupported" target. This means we can use the `"custom"` feature to define a custom handler, and then write tests to make sure that function is called.
1 parent 9d7a76c commit 347a300

File tree

4 files changed

+61
-3
lines changed

4 files changed

+61
-3
lines changed

.github/workflows/tests.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,8 +202,8 @@ jobs:
202202
env:
203203
CHROMEDRIVER: /usr/bin/chromedriver
204204
run: cargo test --target=wasm32-unknown-unknown --features=js,test-in-browser
205-
- name: Build Tests (with custom, without JS)
206-
run: cargo test --no-run --target=wasm32-unknown-unknown --features=custom
205+
- name: Test (custom getrandom)
206+
run: cargo test --target=wasm32-unknown-unknown --features=custom
207207

208208
wasi-tests:
209209
name: WASI test

tests/common/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use super::getrandom_impl;
22

3-
#[cfg(all(target_arch = "wasm32", target_os = "unknown", not(cargo_web)))]
3+
#[cfg(all(target_arch = "wasm32", target_os = "unknown"))]
44
use wasm_bindgen_test::wasm_bindgen_test as test;
55

66
#[cfg(feature = "test-in-browser")]

tests/custom.rs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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+
#[cfg(feature = "test-in-browser")]
11+
wasm_bindgen_test::wasm_bindgen_test_configure!(run_in_browser);
12+
13+
use core::{
14+
num::NonZeroU32,
15+
sync::atomic::{AtomicU8, Ordering},
16+
};
17+
use getrandom::{getrandom, register_custom_getrandom, Error};
18+
19+
fn len7_err() -> Error {
20+
NonZeroU32::new(Error::INTERNAL_START + 7).unwrap().into()
21+
}
22+
23+
fn super_insecure_rng(buf: &mut [u8]) -> Result<(), Error> {
24+
// Length 7 buffers return a custom error
25+
if buf.len() == 7 {
26+
return Err(len7_err());
27+
}
28+
// Otherwise, increment an atomic counter
29+
static COUNTER: AtomicU8 = AtomicU8::new(0);
30+
for b in buf {
31+
*b = COUNTER.fetch_add(1, Ordering::Relaxed);
32+
}
33+
Ok(())
34+
}
35+
36+
register_custom_getrandom!(super_insecure_rng);
37+
38+
#[test]
39+
fn custom_rng_output() {
40+
let mut buf = [0u8; 4];
41+
assert_eq!(getrandom(&mut buf), Ok(()));
42+
assert_eq!(buf, [0, 1, 2, 3]);
43+
assert_eq!(getrandom(&mut buf), Ok(()));
44+
assert_eq!(buf, [4, 5, 6, 7]);
45+
}
46+
47+
#[test]
48+
fn rng_err_output() {
49+
assert_eq!(getrandom(&mut [0; 7]), Err(len7_err()));
50+
}

tests/normal.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
// Don't test on custom wasm32-unknown-unknown
2+
#![cfg(not(all(
3+
target_arch = "wasm32",
4+
target_os = "unknown",
5+
feature = "custom",
6+
not(feature = "js")
7+
)))]
8+
19
// Use the normal getrandom implementation on this architecture.
210
use getrandom::getrandom as getrandom_impl;
311
mod common;

0 commit comments

Comments
 (0)