From 92af82ab9a20d38d5c49ea88ec3f20e28463bf2f Mon Sep 17 00:00:00 2001 From: Joe Richey Date: Fri, 21 Oct 2022 16:49:34 -0700 Subject: [PATCH 1/3] Remove state from Custom RNG This makes it much easier to run this RNG alongside other tests. Signed-off-by: Joe Richey --- tests/custom.rs | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/tests/custom.rs b/tests/custom.rs index 62eae1d66..477b0241a 100644 --- a/tests/custom.rs +++ b/tests/custom.rs @@ -10,10 +10,7 @@ use wasm_bindgen_test::wasm_bindgen_test as test; #[cfg(feature = "test-in-browser")] wasm_bindgen_test::wasm_bindgen_test_configure!(run_in_browser); -use core::{ - num::NonZeroU32, - sync::atomic::{AtomicU8, Ordering}, -}; +use core::num::NonZeroU32; use getrandom::{getrandom, register_custom_getrandom, Error}; fn len7_err() -> Error { @@ -25,10 +22,11 @@ fn super_insecure_rng(buf: &mut [u8]) -> Result<(), Error> { if buf.len() == 7 { return Err(len7_err()); } - // Otherwise, increment an atomic counter - static COUNTER: AtomicU8 = AtomicU8::new(0); + // Otherwise, fill bytes based on input length + let mut start = buf.len() as u8; for b in buf { - *b = COUNTER.fetch_add(1, Ordering::Relaxed); + *b = start; + start = start.wrapping_mul(3); } Ok(()) } @@ -39,9 +37,11 @@ register_custom_getrandom!(super_insecure_rng); fn custom_rng_output() { let mut buf = [0u8; 4]; assert_eq!(getrandom(&mut buf), Ok(())); - assert_eq!(buf, [0, 1, 2, 3]); + assert_eq!(buf, [4, 12, 36, 108]); + + let mut buf = [0u8; 3]; assert_eq!(getrandom(&mut buf), Ok(())); - assert_eq!(buf, [4, 5, 6, 7]); + assert_eq!(buf, [3, 9, 27]); } #[test] From ee0ca47aa29bfda4c0fa89b7bda74220b2a22cdc Mon Sep 17 00:00:00 2001 From: Joe Richey Date: Fri, 21 Oct 2022 16:51:01 -0700 Subject: [PATCH 2/3] Run (most) common tests against the custom RNG Signed-off-by: Joe Richey --- tests/common/mod.rs | 1 + tests/custom.rs | 5 +++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/common/mod.rs b/tests/common/mod.rs index 006f230d7..fecbbdffe 100644 --- a/tests/common/mod.rs +++ b/tests/common/mod.rs @@ -13,6 +13,7 @@ fn test_zero() { } #[test] +#[cfg(not(feature = "custom"))] fn test_diff() { let mut v1 = [0u8; 1000]; getrandom_impl(&mut v1).unwrap(); diff --git a/tests/custom.rs b/tests/custom.rs index 477b0241a..4f9cb807b 100644 --- a/tests/custom.rs +++ b/tests/custom.rs @@ -7,8 +7,6 @@ ))] use wasm_bindgen_test::wasm_bindgen_test as test; -#[cfg(feature = "test-in-browser")] -wasm_bindgen_test::wasm_bindgen_test_configure!(run_in_browser); use core::num::NonZeroU32; use getrandom::{getrandom, register_custom_getrandom, Error}; @@ -33,6 +31,9 @@ fn super_insecure_rng(buf: &mut [u8]) -> Result<(), Error> { register_custom_getrandom!(super_insecure_rng); +use getrandom::getrandom as getrandom_impl; +mod common; + #[test] fn custom_rng_output() { let mut buf = [0u8; 4]; From cf85546863eab509552b3bad8ee60a47ca1d4bbf Mon Sep 17 00:00:00 2001 From: Brian Smith Date: Fri, 21 Oct 2022 09:34:31 -0700 Subject: [PATCH 3/3] Tests: Use custom tests to verify operations on empty slices are no-ops. Modify the custom tests so that they would have detected and prevented the issue fixed in https://github.com/rust-random/getrandom/pull/298. --- tests/custom.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/custom.rs b/tests/custom.rs index 4f9cb807b..b085094be 100644 --- a/tests/custom.rs +++ b/tests/custom.rs @@ -16,6 +16,9 @@ fn len7_err() -> Error { } fn super_insecure_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 if buf.len() == 7 { return Err(len7_err());