Add shuffling to fuzzer, fixing a bug

We were being to strict on list length, it can be 2**24 as all index's
will need to be less than that.
This commit is contained in:
Paul Hauner 2019-02-15 12:56:50 +11:00
parent 210ec89b0b
commit af1d44e8b0
No known key found for this signature in database
GPG Key ID: D362883A9218FCC6
2 changed files with 43 additions and 4 deletions

View File

@ -12,3 +12,4 @@ int_to_bytes = { path = "../int_to_bytes" }
[dev-dependencies] [dev-dependencies]
yaml-rust = "0.4.2" yaml-rust = "0.4.2"
hex = "0.3" hex = "0.3"
ethereum-types = "0.5"

View File

@ -13,8 +13,8 @@ use std::io::Cursor;
/// Returns `None` under any of the following conditions: /// Returns `None` under any of the following conditions:
/// - `list_size == 0` /// - `list_size == 0`
/// - `index >= list_size` /// - `index >= list_size`
/// - `list_size >= 2**24` /// - `list_size > 2**24`
/// - `list_size >= usize::max_value() / 2` /// - `list_size > usize::max_value() / 2`
pub fn get_permutated_index( pub fn get_permutated_index(
index: usize, index: usize,
list_size: usize, list_size: usize,
@ -23,8 +23,8 @@ pub fn get_permutated_index(
) -> Option<usize> { ) -> Option<usize> {
if list_size == 0 if list_size == 0
|| index >= list_size || index >= list_size
|| list_size >= usize::max_value() / 2 || list_size > usize::max_value() / 2
|| list_size >= 2_usize.pow(24) || list_size > 2_usize.pow(24)
{ {
return None; return None;
} }
@ -67,10 +67,48 @@ fn bytes_to_int64(bytes: &[u8]) -> u64 {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
use ethereum_types::H256 as Hash256;
use hex; use hex;
use std::{fs::File, io::prelude::*, path::PathBuf}; use std::{fs::File, io::prelude::*, path::PathBuf};
use yaml_rust::yaml; use yaml_rust::yaml;
#[test]
#[ignore]
fn fuzz_test() {
let max_list_size = 2_usize.pow(24);
let test_runs = 1000;
// Test at max list_size with the end index.
for _ in 0..test_runs {
let index = max_list_size - 1;
let list_size = max_list_size;
let seed = Hash256::random();
let shuffle_rounds = 90;
assert!(get_permutated_index(index, list_size, &seed[..], shuffle_rounds).is_some());
}
// Test at max list_size low indices.
for i in 0..test_runs {
let index = i;
let list_size = max_list_size;
let seed = Hash256::random();
let shuffle_rounds = 90;
assert!(get_permutated_index(index, list_size, &seed[..], shuffle_rounds).is_some());
}
// Test at max list_size high indices.
for i in 0..test_runs {
let index = max_list_size - 1 - i;
let list_size = max_list_size;
let seed = Hash256::random();
let shuffle_rounds = 90;
assert!(get_permutated_index(index, list_size, &seed[..], shuffle_rounds).is_some());
}
}
#[test] #[test]
fn returns_none_for_zero_length_list() { fn returns_none_for_zero_length_list() {
assert_eq!(None, get_permutated_index(100, 0, &[42, 42], 90)); assert_eq!(None, get_permutated_index(100, 0, &[42, 42], 90));