Skip to content

Commit 41f4ce5

Browse files
committed
Tests: Also run common tests in custom test suite.
1 parent bd0654f commit 41f4ce5

File tree

3 files changed

+61
-41
lines changed

3 files changed

+61
-41
lines changed

tests/custom.rs

Lines changed: 3 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -6,45 +6,7 @@
66
not(feature = "js")
77
))]
88

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);
9+
use getrandom::getrandom as getrandom_impl;
1210

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-
}
11+
mod common;
12+
mod custom_common;

tests/custom_common/mod.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Common infrastructure for the custom* test suites (only).
2+
use core::{
3+
num::NonZeroU32,
4+
sync::atomic::{AtomicU8, Ordering},
5+
};
6+
use getrandom::{register_custom_getrandom, Error};
7+
8+
pub fn len7_err() -> Error {
9+
NonZeroU32::new(Error::INTERNAL_START + 7).unwrap().into()
10+
}
11+
12+
fn super_insecure_rng(buf: &mut [u8]) -> Result<(), Error> {
13+
// Length 7 buffers return a custom error
14+
if buf.len() == 7 {
15+
return Err(len7_err());
16+
}
17+
// Otherwise, increment an atomic counter
18+
static COUNTER: AtomicU8 = AtomicU8::new(0);
19+
for b in buf {
20+
*b = COUNTER.fetch_add(1, Ordering::Relaxed);
21+
}
22+
Ok(())
23+
}
24+
25+
register_custom_getrandom!(super_insecure_rng);

tests/custom_kat.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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+
mod custom_common;
14+
15+
use custom_common::len7_err;
16+
use getrandom::getrandom;
17+
18+
// This known-answer test cannot be in the same test suite as any other
19+
// tests that use the `custom_common` implementation since the known answers
20+
// depend on the exact state of `custom_common`.
21+
#[test]
22+
fn custom_rng_output() {
23+
let mut buf = [0u8; 4];
24+
assert_eq!(getrandom(&mut buf), Ok(()));
25+
assert_eq!(buf, [0, 1, 2, 3]);
26+
assert_eq!(getrandom(&mut buf), Ok(()));
27+
assert_eq!(buf, [4, 5, 6, 7]);
28+
}
29+
30+
#[test]
31+
fn rng_err_output() {
32+
assert_eq!(getrandom(&mut [0; 7]), Err(len7_err()));
33+
}

0 commit comments

Comments
 (0)