Skip to content

Commit 8fc6f1c

Browse files
committed
Add JitterRng tests
1 parent 3abcd7b commit 8fc6f1c

File tree

1 file changed

+33
-2
lines changed

1 file changed

+33
-2
lines changed

src/jitter.rs

+33-2
Original file line numberDiff line numberDiff line change
@@ -785,5 +785,36 @@ impl RngCore for JitterRng {
785785

786786
impl CryptoRng for JitterRng {}
787787

788-
// There are no tests included because (1) this is an "external" RNG, so output
789-
// is not reproducible and (2) `test_timer` *will* fail on some platforms.
788+
#[cfg(test)]
789+
mod test_jitter_init {
790+
use JitterRng;
791+
792+
#[cfg(feature="std")]
793+
#[test]
794+
fn test_jitter_init() {
795+
use RngCore;
796+
// Because this is a debug build, measurements here are not representive
797+
// of the final release build.
798+
// Don't fail this test if initializing `JitterRng` fails because of a
799+
// bad timer (the timer from the standard library may not have enough
800+
// accuracy on all platforms).
801+
match JitterRng::new() {
802+
Ok(ref mut rng) => {
803+
// false positives are possible, but extremely unlikely
804+
assert!(rng.next_u32() | rng.next_u32() != 0);
805+
assert!(rng.next_u64() != 0);
806+
let mut buf = [0u8; 6];
807+
rng.try_fill_bytes(&mut buf).unwrap();
808+
assert!(!buf.iter().all(|&x| x == 0));
809+
},
810+
Err(_) => {},
811+
}
812+
}
813+
814+
#[test]
815+
fn test_jitter_bad_timer() {
816+
fn bad_timer() -> u64 { 0 }
817+
let mut rng = JitterRng::new_with_timer(bad_timer);
818+
assert!(rng.test_timer().is_err());
819+
}
820+
}

0 commit comments

Comments
 (0)