Skip to content

Commit

Permalink
add unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
baoyachi committed Aug 3, 2023
1 parent 705ef03 commit 04dba6c
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "sha256"
version = "1.2.2"
version = "1.3.0"
authors = ["baoyachi <[email protected]>"]
edition = "2018"
description = "sha256 crypto digest"
Expand Down
36 changes: 36 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,7 @@ mod tests {
#[tokio::test]
async fn test_async_parity() {
let bytes = (0..0x1000).map(|v| (v % 256) as u8).collect::<Vec<_>>();
let val = digest(&bytes);

let async_res = {
let bytes = &bytes;
Expand All @@ -432,6 +433,41 @@ mod tests {
let sha = Sha256::new();
calc(reader, sha).unwrap()
};
assert_eq!(val, async_res);
assert_eq!(async_res, sync_res);
}

#[cfg(feature = "native_openssl")]
#[tokio::test]
async fn test_async_parity_openssl() {
let bytes = (0..0x1000).map(|v| (v % 256) as u8).collect::<Vec<_>>();
let val = digest(&bytes);

let async_res = {
let bytes = &bytes;
// We want to force Poll::Pending on reads during async_calc, which may break parity
// between sync and async hashing.
let (client, mut server) = tokio::io::duplex(64);
let reader = tokio::io::BufReader::new(client);
let sha = OpenSslSha256::new();

tokio::join! {
async_calc(reader, sha),
async move {
server.write_all(&bytes[..]).await.unwrap();
core::mem::drop(server);
}
}
.0
.unwrap()
};

let sync_res = {
let reader = BufReader::new(&*bytes);
let sha = OpenSslSha256::new();
calc(reader, sha).unwrap()
};
assert_eq!(val, async_res);
assert_eq!(async_res, sync_res);
}
}

0 comments on commit 04dba6c

Please sign in to comment.