Skip to content
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

Send to self #118

Merged
merged 6 commits into from
Feb 26, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 2 additions & 1 deletion api/firmware/backup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package firmware

import (
"bytes"
"testing"

"github.com/stretchr/testify/require"
Expand All @@ -23,7 +24,7 @@ import (
func TestSimulatorBackups(t *testing.T) {
const seedLen = 32
const testName = "test wallet name"
testSimulatorsAfterPairing(t, func(t *testing.T, device *Device) {
testSimulatorsAfterPairing(t, func(t *testing.T, device *Device, stdOut *bytes.Buffer) {
t.Helper()
require.NoError(t, device.SetDeviceName(testName))

Expand Down
3 changes: 2 additions & 1 deletion api/firmware/bip85_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package firmware

import (
"bytes"
"encoding/hex"
"testing"

Expand All @@ -27,7 +28,7 @@ func TestSimulatorBIP85AppBip39(t *testing.T) {
}

func TestSimulatorBIP85AppLN(t *testing.T) {
testInitializedSimulators(t, func(t *testing.T, device *Device) {
testInitializedSimulators(t, func(t *testing.T, device *Device, stdOut *bytes.Buffer) {
t.Helper()
entropy, err := device.BIP85AppLN()
require.NoError(t, err)
Expand Down
16 changes: 10 additions & 6 deletions api/firmware/btc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package firmware

import (
"bytes"
"errors"
"testing"

Expand Down Expand Up @@ -68,7 +69,7 @@ func TestNewXPub(t *testing.T) {
}

func TestBTCXpub(t *testing.T) {
testInitializedSimulators(t, func(t *testing.T, device *Device) {
testInitializedSimulators(t, func(t *testing.T, device *Device, stdOut *bytes.Buffer) {
t.Helper()
xpub, err := device.BTCXPub(messages.BTCCoin_TBTC, []uint32{
49 + hardenedKeyStart,
Expand All @@ -81,7 +82,7 @@ func TestBTCXpub(t *testing.T) {
}

func TestBTCAddress(t *testing.T) {
testInitializedSimulators(t, func(t *testing.T, device *Device) {
testInitializedSimulators(t, func(t *testing.T, device *Device, stdOut *bytes.Buffer) {
t.Helper()
address, err := device.BTCAddress(
messages.BTCCoin_TBTC,
Expand Down Expand Up @@ -114,7 +115,7 @@ func simulatorPub(t *testing.T, device *Device, keypath ...uint32) *btcec.Public
}

func TestSimulatorBTCSignMessage(t *testing.T) {
testInitializedSimulators(t, func(t *testing.T, device *Device) {
testInitializedSimulators(t, func(t *testing.T, device *Device, stdOut *bytes.Buffer) {
t.Helper()
coin := messages.BTCCoin_BTC
keypath := []uint32{49 + hardenedKeyStart, 0 + hardenedKeyStart, 0 + hardenedKeyStart, 0, 10}
Expand Down Expand Up @@ -341,7 +342,7 @@ func makeTaprootOutput(t *testing.T, pubkey *btcec.PublicKey) (*btcec.PublicKey,

// Test signing; all inputs are BIP86 Taproot keyspends.
func TestSimulatorBTCSignTaprootKeySpend(t *testing.T) {
testInitializedSimulators(t, func(t *testing.T, device *Device) {
testInitializedSimulators(t, func(t *testing.T, device *Device, stdOut *bytes.Buffer) {
t.Helper()
coin := messages.BTCCoin_BTC
accountKeypath := []uint32{86 + hardenedKeyStart, 0 + hardenedKeyStart, 0 + hardenedKeyStart}
Expand Down Expand Up @@ -385,6 +386,7 @@ func TestSimulatorBTCSignTaprootKeySpend(t *testing.T) {
_, _, err := device.BTCSign(
coin,
scriptConfigs,
nil,
&BTCTx{
Version: 2,
Inputs: []*BTCTxInput{
Expand Down Expand Up @@ -431,7 +433,7 @@ func TestSimulatorBTCSignTaprootKeySpend(t *testing.T) {

// Test signing; mixed input types (p2wpkh, p2wpkh-p2sh, p2tr)
func TestSimulatorBTCSignMixed(t *testing.T) {
testInitializedSimulators(t, func(t *testing.T, device *Device) {
testInitializedSimulators(t, func(t *testing.T, device *Device, stdOut *bytes.Buffer) {
t.Helper()
coin := messages.BTCCoin_BTC
changeKeypath := []uint32{86 + hardenedKeyStart, 0 + hardenedKeyStart, 0 + hardenedKeyStart, 1, 0}
Expand Down Expand Up @@ -513,6 +515,7 @@ func TestSimulatorBTCSignMixed(t *testing.T) {
_, _, err := device.BTCSign(
coin,
scriptConfigs,
nil,
&BTCTx{
Version: 2,
Inputs: []*BTCTxInput{
Expand Down Expand Up @@ -573,7 +576,7 @@ func TestSimulatorBTCSignMixed(t *testing.T) {
// Test that we can send to a silent payment output (generated by the BitBox) and verify the
// corresponding DLEQ proof on the host that the output was generated correctly.
func TestSimulatorBTCSignSilentPayment(t *testing.T) {
testInitializedSimulators(t, func(t *testing.T, device *Device) {
testInitializedSimulators(t, func(t *testing.T, device *Device, stdOut *bytes.Buffer) {
t.Helper()
coin := messages.BTCCoin_BTC
accountKeypath := []uint32{86 + hardenedKeyStart, 0 + hardenedKeyStart, 0 + hardenedKeyStart}
Expand Down Expand Up @@ -614,6 +617,7 @@ func TestSimulatorBTCSignSilentPayment(t *testing.T) {
Keypath: accountKeypath,
},
},
nil,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit (here and in some other file too): The BTCSign func signature will only be changed in the next commit. Probably it isn't worth the time needed to fix, tho.

&BTCTx{
Version: 2,
Inputs: []*BTCTxInput{
Expand Down
5 changes: 3 additions & 2 deletions api/firmware/cardano_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package firmware

import (
"bytes"
"encoding/hex"
"testing"

Expand All @@ -23,7 +24,7 @@ import (
)

func TestSimulatorCardanoXPubs(t *testing.T) {
testInitializedSimulators(t, func(t *testing.T, device *Device) {
testInitializedSimulators(t, func(t *testing.T, device *Device, stdOut *bytes.Buffer) {
t.Helper()
xpubs, err := device.CardanoXPubs(
[][]uint32{
Expand All @@ -45,7 +46,7 @@ func TestSimulatorCardanoXPubs(t *testing.T) {
}

func TestSimulatorCardanoAddress(t *testing.T) {
testInitializedSimulators(t, func(t *testing.T, device *Device) {
testInitializedSimulators(t, func(t *testing.T, device *Device, stdOut *bytes.Buffer) {
t.Helper()
const account = uint32(1)
const rolePayment = uint32(0) // receive
Expand Down
51 changes: 34 additions & 17 deletions api/firmware/device_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
package firmware

import (
"bufio"
"bytes"
"crypto/rand"
"crypto/sha256"
"encoding/hex"
Expand Down Expand Up @@ -45,13 +47,28 @@ import (
"google.golang.org/protobuf/proto"
)

func runSimulator(filename string) (func() error, *Device, error) {
cmd := exec.Command(filename)
func runSimulator(filename string) (func() error, *Device, *bytes.Buffer, error) {
cmd := exec.Command("stdbuf", "-oL", filename)

// Create pipe before starting process
stdout, err := cmd.StdoutPipe()
if err != nil {
return nil, nil, nil, err
}
if err := cmd.Start(); err != nil {
return nil, nil, err
return nil, nil, nil, err
}

var stdoutBuf bytes.Buffer
scanner := bufio.NewScanner(stdout)
go func() {
for scanner.Scan() {
stdoutBuf.Write(scanner.Bytes())
stdoutBuf.WriteByte('\n')
}
}()

var conn net.Conn
var err error
for range 200 {
conn, err = net.Dial("tcp", "localhost:15423")
if err == nil {
Expand All @@ -60,7 +77,7 @@ func runSimulator(filename string) (func() error, *Device, error) {
time.Sleep(10 * time.Millisecond)
}
if err != nil {
return nil, nil, err
return nil, nil, nil, err
}
const bitboxCMD = 0x80 + 0x40 + 0x01

Expand All @@ -73,7 +90,7 @@ func runSimulator(filename string) (func() error, *Device, error) {
return err
}
return cmd.Process.Kill()
}, device, nil
}, device, &stdoutBuf, nil
}

// Download BitBox simulators based on testdata/simulators.json to testdata/simulators/*.
Expand Down Expand Up @@ -162,7 +179,7 @@ func downloadSimulators() ([]string, error) {
var downloadSimulatorsOnce = sync.OnceValues(downloadSimulators)

// Runs tests against a simulator which is not initialized (not paired, not seeded).
func testSimulators(t *testing.T, run func(*testing.T, *Device)) {
func testSimulators(t *testing.T, run func(*testing.T, *Device, *bytes.Buffer)) {
t.Helper()
if runtime.GOOS != "linux" || runtime.GOARCH != "amd64" {
t.Skip("Skipping simulator tests: not running on linux-amd64")
Expand All @@ -180,39 +197,39 @@ func testSimulators(t *testing.T, run func(*testing.T, *Device)) {

for _, simulatorFilename := range simulatorFilenames {
t.Run(filepath.Base(simulatorFilename), func(t *testing.T) {
teardown, device, err := runSimulator(simulatorFilename)
teardown, device, stdOut, err := runSimulator(simulatorFilename)
require.NoError(t, err)
defer func() { require.NoError(t, teardown()) }()
run(t, device)
run(t, device, stdOut)
})
}
}

// Runs tests against a simulator which is not initialized, but paired (not seeded).
func testSimulatorsAfterPairing(t *testing.T, run func(*testing.T, *Device)) {
func testSimulatorsAfterPairing(t *testing.T, run func(*testing.T, *Device, *bytes.Buffer)) {
t.Helper()
testSimulators(t, func(t *testing.T, device *Device) {
testSimulators(t, func(t *testing.T, device *Device, stdOut *bytes.Buffer) {
t.Helper()
require.NoError(t, device.Init())
device.ChannelHashVerify(true)
run(t, device)
run(t, device, stdOut)
})
}

// Runs tests againt a simulator that is seeded with this mnemonic: boring mistake dish oyster truth
// pigeon viable emerge sort crash wire portion cannon couple enact box walk height pull today solid
// off enable tide
func testInitializedSimulators(t *testing.T, run func(*testing.T, *Device)) {
func testInitializedSimulators(t *testing.T, run func(*testing.T, *Device, *bytes.Buffer)) {
t.Helper()
testSimulatorsAfterPairing(t, func(t *testing.T, device *Device) {
testSimulatorsAfterPairing(t, func(t *testing.T, device *Device, stdOut *bytes.Buffer) {
t.Helper()
require.NoError(t, device.RestoreFromMnemonic())
run(t, device)
run(t, device, stdOut)
})
}

func TestSimulatorRootFingerprint(t *testing.T) {
testInitializedSimulators(t, func(t *testing.T, device *Device) {
testInitializedSimulators(t, func(t *testing.T, device *Device, stdOut *bytes.Buffer) {
t.Helper()
fp, err := device.RootFingerprint()
require.NoError(t, err)
Expand Down Expand Up @@ -492,7 +509,7 @@ func TestVersion(t *testing.T) {
}

func TestSimulatorProduct(t *testing.T) {
testSimulators(t, func(t *testing.T, device *Device) {
testSimulators(t, func(t *testing.T, device *Device, stdOut *bytes.Buffer) {
t.Helper()
require.NoError(t, device.Init())
require.Equal(t, common.ProductBitBox02Multi, device.Product())
Expand Down
11 changes: 6 additions & 5 deletions api/firmware/eth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package firmware

import (
"bytes"
"math/big"
"testing"

Expand Down Expand Up @@ -247,7 +248,7 @@ func TestEncodeValue(t *testing.T) {
}

func TestSimulatorETHPub(t *testing.T) {
testInitializedSimulators(t, func(t *testing.T, device *Device) {
testInitializedSimulators(t, func(t *testing.T, device *Device, stdOut *bytes.Buffer) {
t.Helper()
chainID := uint64(1)
xpub, err := device.ETHPub(
Expand Down Expand Up @@ -290,7 +291,7 @@ func TestSimulatorETHPub(t *testing.T) {
}

func TestSimulatorETHSignMessage(t *testing.T) {
testInitializedSimulators(t, func(t *testing.T, device *Device) {
testInitializedSimulators(t, func(t *testing.T, device *Device, stdOut *bytes.Buffer) {
t.Helper()
chainID := uint64(1)
keypath := []uint32{
Expand All @@ -315,7 +316,7 @@ func TestSimulatorETHSignMessage(t *testing.T) {
}

func TestSimulatorETHSignTypedMessage(t *testing.T) {
testInitializedSimulators(t, func(t *testing.T, device *Device) {
testInitializedSimulators(t, func(t *testing.T, device *Device, stdOut *bytes.Buffer) {
t.Helper()
msg := []byte(`
{
Expand Down Expand Up @@ -381,7 +382,7 @@ func TestSimulatorETHSignTypedMessage(t *testing.T) {
}

func TestSimulatorETHSign(t *testing.T) {
testInitializedSimulators(t, func(t *testing.T, device *Device) {
testInitializedSimulators(t, func(t *testing.T, device *Device, stdOut *bytes.Buffer) {
t.Helper()
chainID := uint64(1)
keypath := []uint32{
Expand Down Expand Up @@ -416,7 +417,7 @@ func TestSimulatorETHSign(t *testing.T) {
}

func TestSimulatorETHSignEIP1559(t *testing.T) {
testInitializedSimulators(t, func(t *testing.T, device *Device) {
testInitializedSimulators(t, func(t *testing.T, device *Device, stdOut *bytes.Buffer) {
t.Helper()
chainID := uint64(1)
keypath := []uint32{
Expand Down
5 changes: 3 additions & 2 deletions api/firmware/mnemonic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,21 @@
package firmware

import (
"bytes"
"testing"

"github.com/stretchr/testify/require"
)

func TestSimulatorShowMnemonic(t *testing.T) {
testInitializedSimulators(t, func(t *testing.T, device *Device) {
testInitializedSimulators(t, func(t *testing.T, device *Device, stdOut *bytes.Buffer) {
t.Helper()
require.NoError(t, device.ShowMnemonic())
})
}

func TestSimulatorSetMnemonicPassphraseEnabled(t *testing.T) {
testInitializedSimulators(t, func(t *testing.T, device *Device) {
testInitializedSimulators(t, func(t *testing.T, device *Device, stdOut *bytes.Buffer) {
t.Helper()
info, err := device.DeviceInfo()
require.NoError(t, err)
Expand Down
5 changes: 3 additions & 2 deletions api/firmware/sdcard_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@
package firmware

import (
"bytes"
"testing"

"github.com/stretchr/testify/require"
)

func TestSimulatorCheckSDCard(t *testing.T) {
testSimulatorsAfterPairing(t, func(t *testing.T, device *Device) {
testSimulatorsAfterPairing(t, func(t *testing.T, device *Device, stdOut *bytes.Buffer) {
t.Helper()
inserted, err := device.CheckSDCard()
require.NoError(t, err)
Expand All @@ -31,7 +32,7 @@ func TestSimulatorCheckSDCard(t *testing.T) {
}

func TestSimutorInsertSDCard(t *testing.T) {
testSimulatorsAfterPairing(t, func(t *testing.T, device *Device) {
testSimulatorsAfterPairing(t, func(t *testing.T, device *Device, stdOut *bytes.Buffer) {
t.Helper()
require.NoError(t, device.InsertSDCard())
})
Expand Down
Loading