Crypto: Add large input tests for all Digests
Create a helper function in cryptoutil.rs which feeds 1,000,000 'a's into a Digest with varying input sizes and then checks the result. This is essentially the same as one of Sha1's existing tests, so, that test was re-implemented using this method. New tests were added using this method for Sha512 and Sha256.
This commit is contained in:
parent
2cbe312343
commit
281b79525b
@ -241,3 +241,36 @@ fn standard_padding(&mut self, rem: uint, func: &fn(&[u8])) {
|
||||
self.zero_until(size - rem);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use std::rand::IsaacRng;
|
||||
use std::rand::RngUtil;
|
||||
use std::vec;
|
||||
|
||||
use digest::Digest;
|
||||
|
||||
/// Feed 1,000,000 'a's into the digest with varying input sizes and check that the result is
|
||||
/// correct.
|
||||
pub fn test_digest_1million_random<D: Digest>(digest: &mut D, blocksize: uint, expected: &str) {
|
||||
let total_size = 1000000;
|
||||
let buffer = vec::from_elem(blocksize * 2, 'a' as u8);
|
||||
let mut rng = IsaacRng::new_unseeded();
|
||||
let mut count = 0;
|
||||
|
||||
digest.reset();
|
||||
|
||||
while count < total_size {
|
||||
let next: uint = rng.gen_uint_range(0, 2 * blocksize + 1);
|
||||
let remaining = total_size - count;
|
||||
let size = if next > remaining { remaining } else { next };
|
||||
digest.input(buffer.slice_to(size));
|
||||
count += size;
|
||||
}
|
||||
|
||||
let result_str = digest.result_str();
|
||||
|
||||
assert!(expected == result_str);
|
||||
}
|
||||
}
|
||||
|
@ -240,7 +240,7 @@ pub fn output_bits(&self) -> uint { 160 }
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
|
||||
use cryptoutil::test::test_digest_1million_random;
|
||||
use digest::Digest;
|
||||
use sha1::Sha1;
|
||||
|
||||
@ -253,15 +253,6 @@ struct Test {
|
||||
|
||||
#[test]
|
||||
fn test() {
|
||||
fn a_million_letter_a() -> ~str {
|
||||
let mut i = 0;
|
||||
let mut rs = ~"";
|
||||
while i < 100000 {
|
||||
rs.push_str("aaaaaaaaaa");
|
||||
i += 1;
|
||||
}
|
||||
return rs;
|
||||
}
|
||||
// Test messages from FIPS 180-1
|
||||
|
||||
let fips_180_1_tests = ~[
|
||||
@ -289,17 +280,6 @@ fn a_million_letter_a() -> ~str {
|
||||
],
|
||||
output_str: ~"84983e441c3bd26ebaae4aa1f95129e5e54670f1"
|
||||
},
|
||||
Test {
|
||||
input: a_million_letter_a(),
|
||||
output: ~[
|
||||
0x34u8, 0xAAu8, 0x97u8, 0x3Cu8,
|
||||
0xD4u8, 0xC4u8, 0xDAu8, 0xA4u8,
|
||||
0xF6u8, 0x1Eu8, 0xEBu8, 0x2Bu8,
|
||||
0xDBu8, 0xADu8, 0x27u8, 0x31u8,
|
||||
0x65u8, 0x34u8, 0x01u8, 0x6Fu8,
|
||||
],
|
||||
output_str: ~"34aa973cd4c4daa4f61eeb2bdbad27316534016f"
|
||||
},
|
||||
];
|
||||
// Examples from wikipedia
|
||||
|
||||
@ -366,6 +346,15 @@ fn a_million_letter_a() -> ~str {
|
||||
sh.reset();
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_1million_random_sha1() {
|
||||
let mut sh = Sha1::new();
|
||||
test_digest_1million_random(
|
||||
&mut sh,
|
||||
64,
|
||||
"34aa973cd4c4daa4f61eeb2bdbad27316534016f");
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
@ -756,6 +756,7 @@ fn output_bits(&self) -> uint { 224 }
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use cryptoutil::test::test_digest_1million_random;
|
||||
use digest::Digest;
|
||||
use sha2::{Sha512, Sha384, Sha512Trunc256, Sha512Trunc224, Sha256, Sha224};
|
||||
|
||||
@ -947,6 +948,25 @@ fn test_sha224() {
|
||||
|
||||
test_hash(sh, tests);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_1million_random_sha512() {
|
||||
let mut sh = Sha512::new();
|
||||
test_digest_1million_random(
|
||||
&mut sh,
|
||||
128,
|
||||
"e718483d0ce769644e2e42c7bc15b4638e1f98b13b2044285632a803afa973eb" +
|
||||
"de0ff244877ea60a4cb0432ce577c31beb009c5c2c49aa2e4eadb217ad8cc09b");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_1million_random_sha256() {
|
||||
let mut sh = Sha256::new();
|
||||
test_digest_1million_random(
|
||||
&mut sh,
|
||||
64,
|
||||
"cdc76e5c9914fb9281a1c7e284d73e67f1809a48a497200e046d39ccc7112cd0");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user