Skip to content

Commit 4028c36

Browse files
committed
Merge branch 'simulator_hash'
2 parents 4727432 + ec3ec2a commit 4028c36

File tree

1 file changed

+25
-8
lines changed

1 file changed

+25
-8
lines changed

tests/util/mod.rs

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -76,16 +76,17 @@ impl Drop for Server {
7676
}
7777
}
7878

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

85-
let actual_hash = hex::encode(bitcoin::hashes::sha256::Hash::hash(&buffer));
83+
let actual_hash = hex::encode(bitcoin::hashes::sha256::Hash::hash(&buffer));
84+
Ok(actual_hash == expected_hash)
85+
}
8686

87-
Ok(actual_hash != expected_hash)
88-
}
87+
async fn file_not_exist_or_hash_mismatch(filename: &Path, expected_hash: &str) -> Result<bool, ()> {
88+
match File::open(filename).await {
89+
Ok(file) => Ok(!hashes_match(file, expected_hash).await?),
8990
Err(ref e) if e.kind() == io::ErrorKind::NotFound => Ok(true),
9091
Err(_) => Err(()),
9192
}
@@ -133,6 +134,22 @@ async fn download_simulators() -> Result<Vec<String>, ()> {
133134
fs::set_permissions(&filename, std::fs::Permissions::from_mode(0o755))
134135
.await
135136
.map_err(|_| ())?;
137+
match File::open(&filename).await {
138+
Ok(file) => {
139+
if !hashes_match(file, &simulator.sha256)
140+
.await
141+
.map_err(|_| ())?
142+
{
143+
eprintln!(
144+
"Hash mismatch for simulator file '{}', expected {}",
145+
filename.display(),
146+
simulator.sha256
147+
);
148+
return Err(());
149+
}
150+
}
151+
Err(_) => return Err(()), // This should never happen as we just created it.
152+
}
136153
}
137154
filenames.push(filename.to_str().unwrap().to_string());
138155
}

0 commit comments

Comments
 (0)