From 281b79525b31affc7cdf5540e27c629a460cadab Mon Sep 17 00:00:00 2001
From: Palmer Cox
Date: Mon, 29 Jul 2013 21:12:20 -0400
Subject: [PATCH] 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.
---
src/libextra/crypto/cryptoutil.rs | 33 +++++++++++++++++++++++++++++++
src/libextra/crypto/sha1.rs | 31 ++++++++++-------------------
src/libextra/crypto/sha2.rs | 20 +++++++++++++++++++
3 files changed, 63 insertions(+), 21 deletions(-)
diff --git a/src/libextra/crypto/cryptoutil.rs b/src/libextra/crypto/cryptoutil.rs
index 6e791f90b233..33ab6a9bcb96 100644
--- a/src/libextra/crypto/cryptoutil.rs
+++ b/src/libextra/crypto/cryptoutil.rs
@@ -241,3 +241,36 @@ impl StandardPadding for T {
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(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);
+ }
+}
diff --git a/src/libextra/crypto/sha1.rs b/src/libextra/crypto/sha1.rs
index cf9604a3c91b..0172f6af18e8 100644
--- a/src/libextra/crypto/sha1.rs
+++ b/src/libextra/crypto/sha1.rs
@@ -240,7 +240,7 @@ impl Digest for Sha1 {
#[cfg(test)]
mod tests {
-
+ use cryptoutil::test::test_digest_1million_random;
use digest::Digest;
use sha1::Sha1;
@@ -253,15 +253,6 @@ mod tests {
#[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 @@ mod tests {
],
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 @@ mod tests {
sh.reset();
}
}
+
+ #[test]
+ fn test_1million_random_sha1() {
+ let mut sh = Sha1::new();
+ test_digest_1million_random(
+ &mut sh,
+ 64,
+ "34aa973cd4c4daa4f61eeb2bdbad27316534016f");
+ }
}
#[cfg(test)]
diff --git a/src/libextra/crypto/sha2.rs b/src/libextra/crypto/sha2.rs
index 46a135a79e7f..b91a54efc928 100644
--- a/src/libextra/crypto/sha2.rs
+++ b/src/libextra/crypto/sha2.rs
@@ -756,6 +756,7 @@ static H224: [u32, ..8] = [
#[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 @@ mod tests {
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");
+ }
}