Skip to content

Commit

Permalink
Fix async calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
h33p committed Aug 2, 2023
1 parent 22c8e05 commit e398914
Showing 1 changed file with 33 additions and 1 deletion.
34 changes: 33 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,7 @@ where
{
let mut buf = BytesMut::with_capacity(1024);
loop {
buf.clear();
let len = input.read_inner(&mut buf).await?;
if len == 0 {
break;
Expand Down Expand Up @@ -332,6 +333,7 @@ mod openssl_sha256 {
#[cfg(test)]
mod tests {
use super::*;
use tokio::io::AsyncWriteExt;

#[cfg(feature = "native_openssl")]
#[test]
Expand Down Expand Up @@ -371,7 +373,6 @@ mod tests {
);
}

#[cfg(feature = "native_openssl")]
#[tokio::test]
async fn test_async() {
let input = Path::new("./foo.file");
Expand Down Expand Up @@ -402,4 +403,35 @@ mod tests {
hash
);
}

#[tokio::test]
async fn test_async_parity() {
let bytes = (0..0x1000).map(|v| (v % 256) as u8).collect::<Vec<_>>();

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 = Sha256::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 = Sha256::new();
calc(reader, sha).unwrap()
};
assert_eq!(async_res, sync_res);
}
}

0 comments on commit e398914

Please sign in to comment.