mirror of
https://github.com/rust-lang/rust.git
synced 2026-05-08 01:28:18 +03:00
Crypto: Add tests for add_bytes_to_bits functions.
This commit is contained in:
@@ -351,6 +351,7 @@ mod test {
|
||||
use std::rand::RngUtil;
|
||||
use std::vec;
|
||||
|
||||
use cryptoutil::{add_bytes_to_bits, add_bytes_to_bits_tuple};
|
||||
use digest::Digest;
|
||||
|
||||
/// Feed 1,000,000 'a's into the digest with varying input sizes and check that the result is
|
||||
@@ -375,4 +376,50 @@ pub fn test_digest_1million_random<D: Digest>(digest: &mut D, blocksize: uint, e
|
||||
|
||||
assert!(expected == result_str);
|
||||
}
|
||||
|
||||
// A normal addition - no overflow occurs
|
||||
#[test]
|
||||
fn test_add_bytes_to_bits_ok() {
|
||||
assert!(add_bytes_to_bits::<u64>(100, 10) == 180);
|
||||
}
|
||||
|
||||
// A simple failure case - adding 1 to the max value
|
||||
#[test]
|
||||
#[should_fail]
|
||||
fn test_add_bytes_to_bits_overflow() {
|
||||
add_bytes_to_bits::<u64>(Bounded::max_value(), 1);
|
||||
}
|
||||
|
||||
// A normal addition - no overflow occurs (fast path)
|
||||
#[test]
|
||||
fn test_add_bytes_to_bits_tuple_ok() {
|
||||
assert!(add_bytes_to_bits_tuple::<u64>((5, 100), 10) == (5, 180));
|
||||
}
|
||||
|
||||
// The low order value overflows into the high order value
|
||||
#[test]
|
||||
fn test_add_bytes_to_bits_tuple_ok2() {
|
||||
assert!(add_bytes_to_bits_tuple::<u64>((5, Bounded::max_value()), 1) == (6, 7));
|
||||
}
|
||||
|
||||
// The value to add is too large to be converted into bits without overflowing its type
|
||||
#[test]
|
||||
fn test_add_bytes_to_bits_tuple_ok3() {
|
||||
assert!(add_bytes_to_bits_tuple::<u64>((5, 0), 0x4000000000000001) == (7, 8));
|
||||
}
|
||||
|
||||
// A simple failure case - adding 1 to the max value
|
||||
#[test]
|
||||
#[should_fail]
|
||||
fn test_add_bytes_to_bits_tuple_overflow() {
|
||||
add_bytes_to_bits_tuple::<u64>((Bounded::max_value(), Bounded::max_value()), 1);
|
||||
}
|
||||
|
||||
// The value to add is too large to convert to bytes without overflowing its type, but the high
|
||||
// order value from this conversion overflows when added to the existing high order value
|
||||
#[test]
|
||||
#[should_fail]
|
||||
fn test_add_bytes_to_bits_tuple_overflow2() {
|
||||
add_bytes_to_bits_tuple::<u64>((Bounded::max_value::<u64>() - 1, 0), 0x8000000000000000);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user