Skip to content

Commit 971e0ee

Browse files
committed
Cleanup Custom Tests
This marks the `tests/custom.rs` test as requiring the `"custom"` feature. 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 Signed-off-by: Joe Richey <joerichey@google.com>
1 parent 267639e commit 971e0ee

3 files changed

Lines changed: 34 additions & 43 deletions

File tree

.github/workflows/tests.yml

Lines changed: 2 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

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: 28 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,41 @@
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};
133

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> {
19-
// `getrandom` guarantees it will not call any implementation if the output
20-
// buffer is empty.
21-
assert!(!buf.is_empty());
22-
// Length 7 buffers return a custom error
23-
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);
4+
const LEN7_CODE: u32 = Error::CUSTOM_START + 7;
5+
// Returns a custom error if input is length 7, otherwise fills with 0x55.
6+
fn mock_rng(dest: &mut [u8]) -> Result<(), Error> {
7+
assert!(!dest.is_empty());
8+
if dest.len() == 7 {
9+
return Err(NonZeroU32::new(LEN7_CODE).unwrap().into());
3110
}
11+
dest.fill(0x55);
3212
Ok(())
3313
}
3414

35-
register_custom_getrandom!(super_insecure_rng);
36-
37-
use getrandom::getrandom as getrandom_impl;
38-
mod common;
15+
// Test registering a custom implementation, even on supported platforms.
16+
register_custom_getrandom!(mock_rng);
3917

18+
// Invoking with an empty buffer should never call the custom implementation.
4019
#[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]);
20+
fn custom_empty() {
21+
getrandom(&mut []).unwrap();
4922
}
5023

24+
// On a supported platform, make sure the custom implementation isn't used. We
25+
// test on a few common platfroms, rather than duplicating the lib.rs logic.
26+
#[cfg(any(target_os = "linux", target_os = "windows", target_os = "macos"))]
5127
#[test]
52-
fn rng_err_output() {
53-
assert_eq!(getrandom(&mut [0; 7]), Err(len7_err()));
28+
fn custom_not_used() {
29+
getrandom(&mut [0; 7]).unwrap();
30+
}
31+
// On an unsupported platform, make sure the custom implementation is used.
32+
#[cfg(all(target_arch = "wasm32", target_os = "unknown", not(feature = "js")))]
33+
#[wasm_bindgen_test::wasm_bindgen_test]
34+
fn custom_used() {
35+
let err = getrandom(&mut [0; 7]).unwrap_err();
36+
assert_eq!(err.code().get(), LEN7_CODE);
37+
38+
let mut buf = [0; 12];
39+
getrandom(&mut buf).unwrap();
40+
assert_eq!(buf, [0x55; 12]);
5441
}

0 commit comments

Comments
 (0)