Skip to content

Commit 19ff191

Browse files
author
Palmer Cox
committed
---
yaml --- r: 144022 b: refs/heads/try2 c: b00aa12 h: refs/heads/master v: v3
1 parent 87cda3f commit 19ff191

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ refs/heads/snap-stage3: 78a7676898d9f80ab540c6df5d4c9ce35bb50463
55
refs/heads/try: 519addf6277dbafccbb4159db4b710c37eaa2ec5
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b
8-
refs/heads/try2: 6386f887a7e7ac1581075ba296edf1a66f375f08
8+
refs/heads/try2: b00aa12374d01b1caadbcfc2b0de76d8bb884192
99
refs/heads/dist-snap: ba4081a5a8573875fed17545846f6f6902c8ba8d
1010
refs/tags/release-0.2: c870d2dffb391e14efb05aa27898f1f6333a9596
1111
refs/tags/release-0.3: b5f0d0f648d9a6153664837026ba1be43d3e2503

branches/try2/src/libextra/crypto/cryptoutil.rs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,7 @@ mod test {
351351
use std::rand::RngUtil;
352352
use std::vec;
353353

354+
use cryptoutil::{add_bytes_to_bits, add_bytes_to_bits_tuple};
354355
use digest::Digest;
355356

356357
/// Feed 1,000,000 'a's into the digest with varying input sizes and check that the result is
@@ -375,4 +376,50 @@ mod test {
375376

376377
assert!(expected == result_str);
377378
}
379+
380+
// A normal addition - no overflow occurs
381+
#[test]
382+
fn test_add_bytes_to_bits_ok() {
383+
assert!(add_bytes_to_bits::<u64>(100, 10) == 180);
384+
}
385+
386+
// A simple failure case - adding 1 to the max value
387+
#[test]
388+
#[should_fail]
389+
fn test_add_bytes_to_bits_overflow() {
390+
add_bytes_to_bits::<u64>(Bounded::max_value(), 1);
391+
}
392+
393+
// A normal addition - no overflow occurs (fast path)
394+
#[test]
395+
fn test_add_bytes_to_bits_tuple_ok() {
396+
assert!(add_bytes_to_bits_tuple::<u64>((5, 100), 10) == (5, 180));
397+
}
398+
399+
// The low order value overflows into the high order value
400+
#[test]
401+
fn test_add_bytes_to_bits_tuple_ok2() {
402+
assert!(add_bytes_to_bits_tuple::<u64>((5, Bounded::max_value()), 1) == (6, 7));
403+
}
404+
405+
// The value to add is too large to be converted into bits without overflowing its type
406+
#[test]
407+
fn test_add_bytes_to_bits_tuple_ok3() {
408+
assert!(add_bytes_to_bits_tuple::<u64>((5, 0), 0x4000000000000001) == (7, 8));
409+
}
410+
411+
// A simple failure case - adding 1 to the max value
412+
#[test]
413+
#[should_fail]
414+
fn test_add_bytes_to_bits_tuple_overflow() {
415+
add_bytes_to_bits_tuple::<u64>((Bounded::max_value(), Bounded::max_value()), 1);
416+
}
417+
418+
// The value to add is too large to convert to bytes without overflowing its type, but the high
419+
// order value from this conversion overflows when added to the existing high order value
420+
#[test]
421+
#[should_fail]
422+
fn test_add_bytes_to_bits_tuple_overflow2() {
423+
add_bytes_to_bits_tuple::<u64>((Bounded::max_value::<u64>() - 1, 0), 0x8000000000000000);
424+
}
378425
}

0 commit comments

Comments
 (0)