Skip to content

Commit

Permalink
Move ring::rand tests to tests/rand_tests.rs.
Browse files Browse the repository at this point in the history
  • Loading branch information
briansmith committed Jun 13, 2019
1 parent 626c64c commit 7633902
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 28 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ include = [
"tests/quic_aes_256_tests.txt",
"tests/quic_chacha20_tests.txt",
"tests/quic_tests.rs",
"tests/rand_tests.rs",
"tests/rsa_from_pkcs8_tests.txt",
"tests/rsa_pkcs1_sign_tests.txt",
"tests/rsa_pkcs1_verify_tests.txt",
Expand Down
28 changes: 0 additions & 28 deletions src/rand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -306,31 +306,3 @@ mod fuchsia {
fn zx_cprng_draw(buffer: *mut u8, length: usize);
}
}

#[cfg(test)]
mod tests {
use crate::rand::{self, SecureRandom};
use std::vec;

#[test]
fn test_system_random_lengths() {
// Test that `fill` succeeds for various interesting lengths. `256` and
// multiples thereof are interesting because that's an edge case for
// `getrandom` on Linux.
let lengths = [0, 1, 2, 3, 96, 255, 256, 257, 511, 512, 513, 4096];

for len in lengths.iter() {
let mut buf = vec![0; *len];

let rng = rand::SystemRandom::new();
assert!(rng.fill(&mut buf).is_ok());

// If `len` < 96 then there's a big chance of false positives, but
// otherwise the likelihood of a false positive is so too low to
// worry about.
if *len >= 96 {
assert!(buf.iter().any(|x| *x != 0));
}
}
}
}
24 changes: 24 additions & 0 deletions tests/rand_tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use ring::rand::{self, SecureRandom};
use std::vec;

#[test]
fn rand_test_system_random_lengths() {
// Test that `fill` succeeds for various interesting lengths. `256` and
// multiples thereof are interesting because that's an edge case for
// `getrandom` on Linux.
let lengths = [0, 1, 2, 3, 96, 255, 256, 257, 511, 512, 513, 4096];

for len in lengths.iter() {
let mut buf = vec![0; *len];

let rng = rand::SystemRandom::new();
assert!(rng.fill(&mut buf).is_ok());

// If `len` < 96 then there's a big chance of false positives, but
// otherwise the likelihood of a false positive is so too low to
// worry about.
if *len >= 96 {
assert!(buf.iter().any(|x| *x != 0));
}
}
}

0 comments on commit 7633902

Please sign in to comment.