Skip to content

Commit

Permalink
cleanup tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dignifiedquire committed Mar 4, 2024
1 parent 6c1ec45 commit 3714f3c
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 27 deletions.
33 changes: 7 additions & 26 deletions sha1-checked/tests/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use digest::dev::{feed_rand_16mib, fixed_reset_test};
use hex_literal::hex;
use sha1::{Digest, Sha1};
use sha1_checked::{Digest, Sha1};

digest::new_test!(sha1_main, "sha1", Sha1, fixed_reset_test);
digest::new_test!(sha1_checked_main, "sha1", Sha1, fixed_reset_test);

#[test]
fn sha1_rand() {
Expand All @@ -14,26 +14,16 @@ fn sha1_rand() {
);
}

#[cfg(feature = "collision")]
digest::new_test!(
sha1_collision_main,
"sha1",
sha1::checked::Sha1,
fixed_reset_test
);

#[cfg(feature = "collision")]
#[test]
fn sha1_collision_rand() {
let mut h = sha1::checked::Sha1::new();
let mut h = Sha1::new();
feed_rand_16mib(&mut h);
assert_eq!(
h.finalize(),
hex!("7e565a25a8b123e9881addbcedcd927b23377a78"),
);
}

#[cfg(feature = "collision")]
#[test]
fn shambles_1() {
collision_test(
Expand All @@ -43,7 +33,6 @@ fn shambles_1() {
)
}

#[cfg(feature = "collision")]
#[test]
fn shambles_2() {
collision_test(
Expand All @@ -53,7 +42,6 @@ fn shambles_2() {
)
}

#[cfg(feature = "collision")]
#[test]
fn shattered_1() {
collision_test(
Expand All @@ -63,7 +51,6 @@ fn shattered_1() {
)
}

#[cfg(feature = "collision")]
#[test]
fn shattered_2() {
collision_test(
Expand All @@ -73,37 +60,31 @@ fn shattered_2() {
)
}

#[cfg(feature = "collision")]
fn collision_test(input: &[u8], hash: [u8; 20], mitigated_hash: [u8; 20]) {
use sha1::checked;

// No detection.
let mut ctx = checked::Sha1::builder().detect_collision(false).build();
let mut ctx = Sha1::builder().detect_collision(false).build();
ctx.update(input);
let d = ctx.try_finalize();
assert!(!d.has_collision());
assert_eq!(&d.hash()[..], hash,);

// No mitigation.
let mut ctx = checked::Sha1::builder().safe_hash(false).build();
let mut ctx = Sha1::builder().safe_hash(false).build();
ctx.update(input);

let d = ctx.try_finalize();
assert!(d.has_collision());
assert_eq!(&d.hash()[..], hash);

// No mitigation, no optimization.
let mut ctx = checked::Sha1::builder()
.safe_hash(false)
.use_ubc(false)
.build();
let mut ctx = Sha1::builder().safe_hash(false).use_ubc(false).build();
ctx.update(input);
let d = ctx.try_finalize();
assert!(d.has_collision());
assert_eq!(&d.hash()[..], hash);

// With mitigation.
let mut ctx = checked::Sha1::new();
let mut ctx = Sha1::new();
ctx.update(input);
let d = ctx.try_finalize();
assert!(d.has_collision());
Expand Down
2 changes: 1 addition & 1 deletion sha1/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ digest = { version = "=0.11.0-pre.8", features = ["dev"] }
hex-literal = "0.4"

[features]
default = ["oid", "std", "collision"]
default = ["oid", "std"]
std = ["digest/std"]
oid = ["digest/oid"] # Enable OID support
zeroize = ["digest/zeroize"]
Expand Down

0 comments on commit 3714f3c

Please sign in to comment.