forked from rust-random/getrandom
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Tests: Also run common tests in custom test suite.
- Loading branch information
1 parent
bd0654f
commit 41f4ce5
Showing
3 changed files
with
61 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// Common infrastructure for the custom* test suites (only). | ||
use core::{ | ||
num::NonZeroU32, | ||
sync::atomic::{AtomicU8, Ordering}, | ||
}; | ||
use getrandom::{register_custom_getrandom, Error}; | ||
|
||
pub fn len7_err() -> Error { | ||
NonZeroU32::new(Error::INTERNAL_START + 7).unwrap().into() | ||
} | ||
|
||
fn super_insecure_rng(buf: &mut [u8]) -> Result<(), Error> { | ||
// Length 7 buffers return a custom error | ||
if buf.len() == 7 { | ||
return Err(len7_err()); | ||
} | ||
// Otherwise, increment an atomic counter | ||
static COUNTER: AtomicU8 = AtomicU8::new(0); | ||
for b in buf { | ||
*b = COUNTER.fetch_add(1, Ordering::Relaxed); | ||
} | ||
Ok(()) | ||
} | ||
|
||
register_custom_getrandom!(super_insecure_rng); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// 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; | ||
#[cfg(feature = "test-in-browser")] | ||
wasm_bindgen_test::wasm_bindgen_test_configure!(run_in_browser); | ||
|
||
mod custom_common; | ||
|
||
use custom_common::len7_err; | ||
use getrandom::getrandom; | ||
|
||
// This known-answer test cannot be in the same test suite as any other | ||
// tests that use the `custom_common` implementation since the known answers | ||
// depend on the exact state of `custom_common`. | ||
#[test] | ||
fn custom_rng_output() { | ||
let mut buf = [0u8; 4]; | ||
assert_eq!(getrandom(&mut buf), Ok(())); | ||
assert_eq!(buf, [0, 1, 2, 3]); | ||
assert_eq!(getrandom(&mut buf), Ok(())); | ||
assert_eq!(buf, [4, 5, 6, 7]); | ||
} | ||
|
||
#[test] | ||
fn rng_err_output() { | ||
assert_eq!(getrandom(&mut [0; 7]), Err(len7_err())); | ||
} |