Skip to content

Commit f345775

Browse files
authored
Cleanup Custom Tests (#473)
This moves the tests for the custom RNG registration into custom.rs. It also: - Simplifies the registered custom RNG - Makes sure the custom RNG _is not_ used on supported platforms - Makes sure the custom RNG _is_ used on unsupported platforms
1 parent 267639e commit f345775

File tree

3 files changed

+43
-39
lines changed

3 files changed

+43
-39
lines changed

.github/workflows/tests.yml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ jobs:
3838
toolchain: ${{ matrix.toolchain }}
3939
- uses: Swatinem/rust-cache@v2
4040
- run: cargo test
41-
- run: cargo test --features=std
41+
# Make sure enabling the std and custom features don't break anything
42+
- run: cargo test --features=std,custom
4243
- run: cargo test --features=linux_disable_fallback
43-
- run: cargo test --features=custom # custom should do nothing here
4444
- if: ${{ matrix.toolchain == 'nightly' }}
4545
run: cargo test --benches
4646

@@ -260,6 +260,8 @@ jobs:
260260
run: wasm-pack test --headless --safari --features=js,test-in-browser
261261
- name: Test (custom getrandom)
262262
run: wasm-pack test --node --features=custom
263+
- name: Test (JS overrides custom)
264+
run: wasm-pack test --node --features=custom,js
263265

264266
wasm64-tests:
265267
name: wasm64 Build/Link

Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ rustc-dep-of-std = [
5353
# Unstable/test-only feature to run wasm-bindgen tests in a browser
5454
test-in-browser = []
5555

56+
[[test]]
57+
name = "custom"
58+
required-features = ["custom"]
59+
5660
[package.metadata.docs.rs]
5761
features = ["std", "custom"]
5862
rustdoc-args = ["--cfg", "docsrs"]

tests/custom.rs

Lines changed: 35 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,52 @@
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-
111
use core::num::NonZeroU32;
122
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;
135

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> {
199
// `getrandom` guarantees it will not call any implementation if the output
2010
// buffer is empty.
2111
assert!(!buf.is_empty());
22-
// Length 7 buffers return a custom error
2312
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());
3114
}
15+
buf.fill(0x55);
3216
Ok(())
3317
}
3418

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);
3921

22+
// Invoking with an empty buffer should never call the custom implementation.
4023
#[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();
4926
}
5027

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+
))]
5138
#[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]);
5452
}

0 commit comments

Comments
 (0)