Skip to content

tests: check for hash matching after downloading. #106

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 16, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 25 additions & 8 deletions tests/util/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,16 +76,17 @@ impl Drop for Server {
}
}

async fn file_not_exist_or_hash_mismatch(filename: &Path, expected_hash: &str) -> Result<bool, ()> {
match File::open(filename).await {
Ok(mut file) => {
let mut buffer = Vec::new();
file.read_to_end(&mut buffer).await.map_err(|_| ())?;
async fn hashes_match(mut file: File, expected_hash: &str) -> Result<bool, ()> {
let mut buffer = Vec::new();
file.read_to_end(&mut buffer).await.map_err(|_| ())?;

let actual_hash = hex::encode(bitcoin::hashes::sha256::Hash::hash(&buffer));
let actual_hash = hex::encode(bitcoin::hashes::sha256::Hash::hash(&buffer));
Ok(actual_hash == expected_hash)
}

Ok(actual_hash != expected_hash)
}
async fn file_not_exist_or_hash_mismatch(filename: &Path, expected_hash: &str) -> Result<bool, ()> {
match File::open(filename).await {
Ok(file) => Ok(!hashes_match(file, expected_hash).await?),
Err(ref e) if e.kind() == io::ErrorKind::NotFound => Ok(true),
Err(_) => Err(()),
}
Expand Down Expand Up @@ -133,6 +134,22 @@ async fn download_simulators() -> Result<Vec<String>, ()> {
fs::set_permissions(&filename, std::fs::Permissions::from_mode(0o755))
.await
.map_err(|_| ())?;
match File::open(&filename).await {
Ok(file) => {
if !hashes_match(file, &simulator.sha256)
.await
.map_err(|_| ())?
{
eprintln!(
"Hash mismatch for simulator file '{}', expected {}",
filename.display(),
simulator.sha256
);
return Err(());
}
}
Err(_) => return Err(()), // This should never happen as we just created it.
}
}
filenames.push(filename.to_str().unwrap().to_string());
}
Expand Down
Loading