Skip to content

Commit

Permalink
backend/bitbox02bootloader: simplify unit tests
Browse files Browse the repository at this point in the history
By using the newly added `ParseSignedFirmware()` util function.
  • Loading branch information
benma committed May 13, 2024
1 parent 3b6a816 commit 3483ab9
Showing 1 changed file with 8 additions and 12 deletions.
20 changes: 8 additions & 12 deletions backend/devices/bitbox02bootloader/firmware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,36 +22,32 @@ import (
"os"
"testing"

"github.com/digitalbitbox/bitbox02-api-go/api/bootloader"
bitbox02common "github.com/digitalbitbox/bitbox02-api-go/api/common"
"github.com/stretchr/testify/require"
)

func testHash(t *testing.T, info firmwareInfo, expectedMagic []byte, hashFile string) {
func testHash(t *testing.T, info firmwareInfo, expectedProduct bitbox02common.Product, hashFile string) {
t.Helper()

const sigDataLen = 584
const magicLen = 4

signedBinary, err := info.signedBinary()
require.NoError(t, err)
require.True(t, len(signedBinary) >= 4+sigDataLen)
require.Equal(t, expectedMagic, signedBinary[:magicLen])
hash := sha256.Sum256(signedBinary[magicLen+sigDataLen:])
product, _, binary, err := bootloader.ParseSignedFirmware(signedBinary)
require.NoError(t, err)
require.Equal(t, expectedProduct, product)
hash := sha256.Sum256(binary)
expectedHash, err := os.ReadFile(hashFile)
require.NoError(t, err)
require.Equal(t, string(expectedHash), hex.EncodeToString(hash[:]))
}

func TestBundledFirmware(t *testing.T) {
magicMulti := []byte{0x65, 0x3f, 0x36, 0x2b}
magicBTCOnly := []byte{0x11, 0x23, 0x3b, 0x0b}

for _, fw := range bundledFirmwares[bitbox02common.ProductBitBox02Multi] {
testHash(t, fw, magicMulti, fmt.Sprintf("assets/firmware.v%s.signed.bin.sha256", fw.version))
testHash(t, fw, bitbox02common.ProductBitBox02Multi, fmt.Sprintf("assets/firmware.v%s.signed.bin.sha256", fw.version))
}

for _, fw := range bundledFirmwares[bitbox02common.ProductBitBox02BTCOnly] {
testHash(t, fw, magicBTCOnly, fmt.Sprintf("assets/firmware-btc.v%s.signed.bin.sha256", fw.version))
testHash(t, fw, bitbox02common.ProductBitBox02BTCOnly, fmt.Sprintf("assets/firmware-btc.v%s.signed.bin.sha256", fw.version))
}
}

Expand Down

0 comments on commit 3483ab9

Please sign in to comment.