Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup Custom Tests #473

Merged
merged 1 commit into from
Jun 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ jobs:
toolchain: ${{ matrix.toolchain }}
- uses: Swatinem/rust-cache@v2
- run: cargo test
- run: cargo test --features=std
# Make sure enabling the std and custom features don't break anything
- run: cargo test --features=std,custom
- run: cargo test --features=linux_disable_fallback
- run: cargo test --features=custom # custom should do nothing here
- if: ${{ matrix.toolchain == 'nightly' }}
run: cargo test --benches

Expand Down Expand Up @@ -260,6 +260,8 @@ jobs:
run: wasm-pack test --headless --safari --features=js,test-in-browser
- name: Test (custom getrandom)
run: wasm-pack test --node --features=custom
- name: Test (JS overrides custom)
run: wasm-pack test --node --features=custom,js

wasm64-tests:
name: wasm64 Build/Link
Expand Down
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ rustc-dep-of-std = [
# Unstable/test-only feature to run wasm-bindgen tests in a browser
test-in-browser = []

[[test]]
name = "custom"
required-features = ["custom"]

[package.metadata.docs.rs]
features = ["std", "custom"]
rustdoc-args = ["--cfg", "docsrs"]
Expand Down
72 changes: 35 additions & 37 deletions tests/custom.rs
Original file line number Diff line number Diff line change
@@ -1,54 +1,52 @@
// Test that a custom handler works on wasm32-unknown-unknown
#![cfg(all(
target_arch = "wasm32",
target_os = "unknown",
feature = "custom",
not(feature = "js")
))]

use wasm_bindgen_test::wasm_bindgen_test as test;

josephlr marked this conversation as resolved.
Show resolved Hide resolved
use core::num::NonZeroU32;
use getrandom::{getrandom, register_custom_getrandom, Error};
#[cfg(all(target_family = "wasm", target_os = "unknown"))]
use wasm_bindgen_test::wasm_bindgen_test as test;

fn len7_err() -> Error {
NonZeroU32::new(Error::INTERNAL_START + 7).unwrap().into()
}

fn super_insecure_rng(buf: &mut [u8]) -> Result<(), Error> {
const LEN7_CODE: u32 = Error::CUSTOM_START + 7;
// Returns a custom error if input is length 7, otherwise fills with 0x55.
fn mock_rng(buf: &mut [u8]) -> Result<(), Error> {
// `getrandom` guarantees it will not call any implementation if the output
// buffer is empty.
assert!(!buf.is_empty());
// Length 7 buffers return a custom error
josephlr marked this conversation as resolved.
Show resolved Hide resolved
if buf.len() == 7 {
return Err(len7_err());
}
// Otherwise, fill bytes based on input length
let mut start = buf.len() as u8;
for b in buf {
*b = start;
start = start.wrapping_mul(3);
return Err(NonZeroU32::new(LEN7_CODE).unwrap().into());
}
buf.fill(0x55);
Ok(())
}

register_custom_getrandom!(super_insecure_rng);

use getrandom::getrandom as getrandom_impl;
mod common;
// Test registering a custom implementation, even on supported platforms.
register_custom_getrandom!(mock_rng);

// Invoking with an empty buffer should never call the custom implementation.
#[test]
fn custom_rng_output() {
let mut buf = [0u8; 4];
assert_eq!(getrandom(&mut buf), Ok(()));
assert_eq!(buf, [4, 12, 36, 108]);

let mut buf = [0u8; 3];
assert_eq!(getrandom(&mut buf), Ok(()));
assert_eq!(buf, [3, 9, 27]);
fn custom_empty() {
getrandom(&mut []).unwrap();
}

// On a supported platform, make sure the custom implementation isn't used. We
// test on a few common platfroms, rather than duplicating the lib.rs logic.
#[cfg(any(
target_os = "linux",
target_os = "windows",
target_os = "macos",
target_os = "espidf",
target_os = "wasi",
all(target_family = "wasm", target_os = "unknown", feature = "js"),
))]
#[test]
fn rng_err_output() {
assert_eq!(getrandom(&mut [0; 7]), Err(len7_err()));
fn custom_not_used() {
getrandom(&mut [0; 7]).unwrap();
}
// On an unsupported platform, make sure the custom implementation is used.
#[cfg(all(target_family = "wasm", target_os = "unknown", not(feature = "js")))]
#[test]
fn custom_used() {
let err = getrandom(&mut [0; 7]).unwrap_err();
assert_eq!(err.code().get(), LEN7_CODE);

let mut buf = [0; 12];
getrandom(&mut buf).unwrap();
assert_eq!(buf, [0x55; 12]);
}
Loading