Skip to content

Commit 5e2c859

Browse files
committed
Merge branch 'simulator_hash'
2 parents 2b4bf31 + e725e40 commit 5e2c859

File tree

2 files changed

+30
-6
lines changed

2 files changed

+30
-6
lines changed

backend/devices/bitbox02/keystore_simulator_test.go

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,15 @@ func downloadSimulators() ([]string, error) {
167167
return nil, err
168168
}
169169

170+
hashesMatch := func(file *os.File, expectedHash string) (bool, error) {
171+
hasher := sha256.New()
172+
if _, err := io.Copy(hasher, file); err != nil {
173+
return false, err
174+
}
175+
actualHash := hex.EncodeToString(hasher.Sum(nil))
176+
return actualHash == expectedHash, nil
177+
}
178+
170179
fileNotExistOrHashMismatch := func(filename, expectedHash string) (bool, error) {
171180
file, err := os.Open(filename)
172181
if os.IsNotExist(err) {
@@ -177,13 +186,11 @@ func downloadSimulators() ([]string, error) {
177186
}
178187
defer file.Close()
179188

180-
hasher := sha256.New()
181-
if _, err := io.Copy(hasher, file); err != nil {
189+
match, err := hashesMatch(file, expectedHash)
190+
if err != nil {
182191
return false, err
183192
}
184-
actualHash := hex.EncodeToString(hasher.Sum(nil))
185-
186-
return actualHash != expectedHash, nil
193+
return !match, nil
187194
}
188195

189196
downloadFile := func(url, filename string) error {
@@ -222,13 +229,30 @@ func downloadSimulators() ([]string, error) {
222229
return nil, err
223230
}
224231
if doDownload {
232+
fmt.Printf("Downloading %s to %s\n", simulator.URL, filename)
225233
if err := downloadFile(simulator.URL, filename); err != nil {
226234
return nil, err
227235
}
236+
// If we downloaded the file, check again the hash.
237+
file, err := os.Open(filename)
238+
if err != nil {
239+
// This should never happen, as we just downloaded it
240+
return nil, err
241+
}
242+
match, err := hashesMatch(file, simulator.Sha256)
243+
if err != nil {
244+
return nil, err
245+
}
246+
if !match {
247+
return nil, errp.Newf("downloaded file %s does not match expected hash %s", filename, simulator.Sha256)
248+
}
228249
if err := os.Chmod(filename, 0755); err != nil {
229250
return nil, err
230251
}
252+
} else {
253+
fmt.Printf("Skipping download of %s, file %s already exists and has the correct hash\n", simulator.URL, filename)
231254
}
255+
232256
filenames = append(filenames, filename)
233257
}
234258
return filenames, nil

backend/devices/bitbox02/testdata/simulators.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@
1313
},
1414
{
1515
"url": "https://github.com/BitBoxSwiss/bitbox02-firmware/releases/download/firmware%2Fv9.22.0/bitbox02-multi-v9.22.0-simulator1.0.0-linux-amd64",
16-
"sha256": "p91c415dd976745bc3dc8e73b45225f198fe4a49d3164302e4e818c467c2a13f7"
16+
"sha256": "3af12697f6fd51b155bf277ef01ef3eea5290908bff99a4aae83a95cb144ced1"
1717
}
1818
]

0 commit comments

Comments
 (0)