Skip to content

Commit

Permalink
Add additional hash functions (blake2 and blake3)
Browse files Browse the repository at this point in the history
  • Loading branch information
nieznanysprawiciel committed Sep 9, 2024
1 parent a0f9856 commit 7350274
Show file tree
Hide file tree
Showing 9 changed files with 91 additions and 26 deletions.
38 changes: 38 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions exe-unit/components/transfer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ actix-rt = "2.7"
anyhow = "1.0"
# async-compression 0.3.8+ deprecates the "stream" module
async-compression = { version = "=0.3.7", features = ["tokio", "futures-io", "stream", "bzip2", "gzip", "xz"] }
blake2 = "0.10"
blake3 = { version = "1.5", features = ["traits-preview"] }
bytes = "1.0"
futures = "0.3.4"
globset = "0.4.5"
Expand Down
13 changes: 13 additions & 0 deletions exe-unit/components/transfer/src/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,19 @@ where
)))
}
},
"blake2b" => Box::<blake2::Blake2b512>::default(),
"blake2s" => Box::<blake2::Blake2b512>::default(),
"blake2" => match hash.len() * 8 {
256 => Box::<blake2::Blake2s256>::default(),
512 => Box::<blake2::Blake2b512>::default(),
len => {
return Err(Error::UnsupportedDigestError(format!(
"Unsupported digest {alg} of length {len}: {}",
hex::encode(&hash)
)))
}
},
"blake3" => Box::<blake3::Hasher>::default(),
_ => {
return Err(Error::UnsupportedDigestError(format!(
"Unsupported digest: {alg}"
Expand Down
13 changes: 12 additions & 1 deletion exe-unit/components/transfer/tests/test_deploy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,15 @@ async fn test_deploy_checksum(ctx: &mut DroppableTestContext) -> anyhow::Result<
("sha2-512", generate::<sha2::Sha512>(temp_dir, "sha2-512")),
("sha2-224", generate::<sha2::Sha224>(temp_dir, "sha2-224")),
("sha2-384", generate::<sha2::Sha384>(temp_dir, "sha2-384")),
(
"blake2-512",
generate::<blake2::Blake2b512>(temp_dir, "blake2-512"),
),
(
"blake2-256",
generate::<blake2::Blake2s256>(temp_dir, "blake2-256"),
),
("blake3", generate::<blake3::Hasher>(temp_dir, "blake3")),
];

log::debug!("Starting HTTP servers");
Expand Down Expand Up @@ -151,7 +160,9 @@ async fn test_deploy_checksum(ctx: &mut DroppableTestContext) -> anyhow::Result<
)),
};
let result = addr.send(deploy).await;
result.unwrap().unwrap();
let path = result.unwrap().unwrap().unwrap();

assert!(path.exists());
}

Ok(())
Expand Down
3 changes: 2 additions & 1 deletion exe-unit/components/transfer/tests/test_transfer_resume.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use actix::{Actor, Addr};
use futures::future::LocalBoxFuture;
use sha3::Sha3_512;
use std::io::ErrorKind;
use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant};
Expand Down Expand Up @@ -173,7 +174,7 @@ async fn test_transfer_resume(ctx: &mut DroppableTestContext) -> anyhow::Result<
transfer(&addr, "http://127.0.0.1:8001/rnd", "container:/input/rnd-1"),
)
.await??;
verify_hash(&hash, work_dir.join("vol-1"), "rnd-1");
verify_hash::<Sha3_512>(&hash, work_dir.join("vol-1"), "rnd-1");
log::warn!("Checksum verified");

Ok(())
Expand Down
23 changes: 12 additions & 11 deletions exe-unit/components/transfer/tests/test_transfer_service.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use actix::{Actor, Addr};
use sha3::Sha3_512;
use std::env;
use test_context::test_context;

Expand Down Expand Up @@ -113,7 +114,7 @@ async fn test_transfer_scenarios(ctx: &mut DroppableTestContext) -> anyhow::Resu
println!();
log::warn!("[>>] Transfer HTTP -> container");
transfer(&addr, "http://127.0.0.1:8001/rnd", "container:/input/rnd-1").await?;
verify_hash(&hash, work_dir.join("vol-1"), "rnd-1");
verify_hash::<Sha3_512>(&hash, work_dir.join("vol-1"), "rnd-1");
log::warn!("Checksum verified");

println!();
Expand All @@ -124,7 +125,7 @@ async fn test_transfer_scenarios(ctx: &mut DroppableTestContext) -> anyhow::Resu
"http://127.0.0.1:8002/rnd-2",
)
.await?;
verify_hash(&hash, temp_dir, "rnd-2");
verify_hash::<Sha3_512>(&hash, temp_dir, "rnd-2");
log::warn!("Checksum verified");

println!();
Expand All @@ -135,19 +136,19 @@ async fn test_transfer_scenarios(ctx: &mut DroppableTestContext) -> anyhow::Resu
"http://127.0.0.1:8002/rnd-3",
)
.await?;
verify_hash(&hash, temp_dir, "rnd-3");
verify_hash::<Sha3_512>(&hash, temp_dir, "rnd-3");
log::warn!("Checksum verified");

println!();
log::warn!("[>>] Transfer container -> container");
transfer(&addr, "container:/input/rnd-1", "container:/input/rnd-4").await?;
verify_hash(&hash, work_dir.join("vol-1"), "rnd-4");
verify_hash::<Sha3_512>(&hash, work_dir.join("vol-1"), "rnd-4");
log::warn!("Checksum verified");

println!();
log::warn!("[>>] Transfer container -> container (different volume)");
transfer(&addr, "container:/input/rnd-1", "container:/output/rnd-5").await?;
verify_hash(&hash, work_dir.join("vol-2"), "rnd-5");
verify_hash::<Sha3_512>(&hash, work_dir.join("vol-2"), "rnd-5");
log::warn!("Checksum verified");

Ok(())
Expand Down Expand Up @@ -257,8 +258,8 @@ async fn test_transfer_archived(ctx: &mut DroppableTestContext) -> anyhow::Resul
)
.await?;
log::warn!("Extraction complete");
verify_hash(&hash, work_dir.join("vol-3"), "rnd-1");
verify_hash(&hash, work_dir.join("vol-3"), "rnd-4");
verify_hash::<Sha3_512>(&hash, work_dir.join("vol-3"), "rnd-1");
verify_hash::<Sha3_512>(&hash, work_dir.join("vol-3"), "rnd-4");
log::warn!("Checksum verified");

log::warn!("Removing extracted files");
Expand All @@ -279,8 +280,8 @@ async fn test_transfer_archived(ctx: &mut DroppableTestContext) -> anyhow::Resul
)
.await?;
log::warn!("Extraction complete");
verify_hash(&hash, work_dir.join("vol-3"), "rnd-1");
verify_hash(&hash, work_dir.join("vol-3"), "rnd-4");
verify_hash::<Sha3_512>(&hash, work_dir.join("vol-3"), "rnd-1");
verify_hash::<Sha3_512>(&hash, work_dir.join("vol-3"), "rnd-4");
log::warn!("Checksum verified");

log::warn!("Removing extracted files");
Expand All @@ -296,8 +297,8 @@ async fn test_transfer_archived(ctx: &mut DroppableTestContext) -> anyhow::Resul
// args.fileset = Some(FileSet::Pattern(SetEntry::Single("**/rnd-*".into())));
transfer_with_args(&addr, "container:/input", "container:/extract", args).await?;
log::warn!("Transfer complete");
verify_hash(&hash, work_dir.join("vol-3"), "rnd-1");
verify_hash(&hash, work_dir.join("vol-3"), "rnd-4");
verify_hash::<Sha3_512>(&hash, work_dir.join("vol-3"), "rnd-1");
verify_hash::<Sha3_512>(&hash, work_dir.join("vol-3"), "rnd-4");
log::warn!("Checksum verified");

transfer(
Expand Down
1 change: 1 addition & 0 deletions test-utils/test-framework/framework-basic/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ async-compression = { version = "=0.3.7", features = [
] }
bytes = "1.0"
crossterm = "0.26.1"
digest = "0.10"
env_logger = "0.7"
futures = "0.3.4"
globset = "0.4.5"
Expand Down
9 changes: 3 additions & 6 deletions test-utils/test-framework/framework-basic/src/file.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
use digest::{Digest, Output};
use rand::Rng;
use sha2::digest::Output;
use sha2::Digest;
use std::fs::OpenOptions;
use std::io::Write;
use std::path::{Path, PathBuf};

pub type HashOutput<H> = Output<H>;

pub fn generate_file_with_hasher<H: Digest>(
path: &Path,
name: &str,
chunk_size: usize,
chunk_count: usize,
) -> HashOutput<H> {
) -> Output<H> {
let path = path.join(name);

log::debug!(
Expand Down Expand Up @@ -52,7 +49,7 @@ pub fn generate_file_with_hash(
name: &str,
chunk_size: usize,
chunk_count: usize,
) -> HashOutput<sha3::Sha3_512> {
) -> Output<sha3::Sha3_512> {
generate_file_with_hasher::<sha3::Sha3_512>(path, name, chunk_size, chunk_count)
}

Expand Down
15 changes: 8 additions & 7 deletions test-utils/test-framework/framework-basic/src/hash.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
use sha2::digest::{Digest, Output};
use digest::{Digest, Output};
use std::fs::OpenOptions;
use std::io::Read;
use std::path::Path;

pub type HashOutput = Output<sha3::Sha3_512>;

pub fn hash_file(path: &Path) -> HashOutput {
pub fn hash_file<H: Digest>(path: &Path) -> Output<H> {
let mut file_src = OpenOptions::new().read(true).open(path).expect("rnd file");

let mut hasher = sha3::Sha3_512::default();
let mut hasher = H::new();
let mut chunk = vec![0; 4096];

while let Ok(count) = file_src.read(&mut chunk[..]) {
Expand All @@ -20,8 +18,11 @@ pub fn hash_file(path: &Path) -> HashOutput {
hasher.finalize()
}

pub fn verify_hash<S: AsRef<str>, P: AsRef<Path>>(hash: &HashOutput, path: P, file_name: S) {
pub fn verify_hash<H>(hash: &Output<H>, path: impl AsRef<Path>, file_name: impl AsRef<str>)
where
H: Digest,
{
let path = path.as_ref().join(file_name.as_ref());
log::info!("Verifying hash of {:?}", path);
assert_eq!(hash, &hash_file(&path));
assert_eq!(hash, &hash_file::<H>(&path));
}

0 comments on commit 7350274

Please sign in to comment.