Skip to content

Commit 92af82a

Browse files
committed
Remove state from Custom RNG
This makes it much easier to run this RNG alongside other tests. Signed-off-by: Joe Richey <[email protected]>
1 parent ce5c2d9 commit 92af82a

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

tests/custom.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,7 @@ use wasm_bindgen_test::wasm_bindgen_test as test;
1010
#[cfg(feature = "test-in-browser")]
1111
wasm_bindgen_test::wasm_bindgen_test_configure!(run_in_browser);
1212

13-
use core::{
14-
num::NonZeroU32,
15-
sync::atomic::{AtomicU8, Ordering},
16-
};
13+
use core::num::NonZeroU32;
1714
use getrandom::{getrandom, register_custom_getrandom, Error};
1815

1916
fn len7_err() -> Error {
@@ -25,10 +22,11 @@ fn super_insecure_rng(buf: &mut [u8]) -> Result<(), Error> {
2522
if buf.len() == 7 {
2623
return Err(len7_err());
2724
}
28-
// Otherwise, increment an atomic counter
29-
static COUNTER: AtomicU8 = AtomicU8::new(0);
25+
// Otherwise, fill bytes based on input length
26+
let mut start = buf.len() as u8;
3027
for b in buf {
31-
*b = COUNTER.fetch_add(1, Ordering::Relaxed);
28+
*b = start;
29+
start = start.wrapping_mul(3);
3230
}
3331
Ok(())
3432
}
@@ -39,9 +37,11 @@ register_custom_getrandom!(super_insecure_rng);
3937
fn custom_rng_output() {
4038
let mut buf = [0u8; 4];
4139
assert_eq!(getrandom(&mut buf), Ok(()));
42-
assert_eq!(buf, [0, 1, 2, 3]);
40+
assert_eq!(buf, [4, 12, 36, 108]);
41+
42+
let mut buf = [0u8; 3];
4343
assert_eq!(getrandom(&mut buf), Ok(()));
44-
assert_eq!(buf, [4, 5, 6, 7]);
44+
assert_eq!(buf, [3, 9, 27]);
4545
}
4646

4747
#[test]

0 commit comments

Comments
 (0)