Skip to content

Commit

Permalink
Change Digital Bitbox test to use simulator
Browse files Browse the repository at this point in the history
  • Loading branch information
achow101 committed Jan 14, 2019
1 parent 07c9771 commit 8f1dff2
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 28 deletions.
14 changes: 6 additions & 8 deletions test/run_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,19 @@
keepkey_group = parser.add_mutually_exclusive_group()
keepkey_group.add_argument('--no_keepkey', help='Do not run Keepkey test with emulator', action='store_true')
keepkey_group.add_argument('--keepkey', help='Path to Keepkey emulator.', default='work/keepkey-firmware/bin/kkemu')
dbb_group = parser.add_mutually_exclusive_group()
dbb_group.add_argument('--no_bitbox', help='Do not run Digital Bitbox test with simulator', action='store_true')
dbb_group.add_argument('--bitbox', help='Path to Digital bitbox simulator.', default='work/mcu/build/bin/simulator')

parser.add_argument('--digitalbitbox', help='Run physical Digital Bitbox tests.', action='store_true')
parser.add_argument('--bitcoind', help='Path to bitcoind.', default='work/bitcoin/src/bitcoind')
parser.add_argument('--password', '-p', help='Device password')
args = parser.parse_args()

# Run tests
suite = unittest.TestSuite()
suite.addTests(unittest.defaultTestLoader.loadTestsFromTestCase(TestSegwitAddress))
suite.addTests(unittest.defaultTestLoader.loadTestsFromTestCase(TestPSBT))

if not args.no_trezor or not args.no_coldcard or args.ledger or args.digitalbitbox:
if not args.no_trezor or not args.no_coldcard or args.ledger or not args.no_bitbox:
# Start bitcoind
rpc, userpass = start_bitcoind(args.bitcoind)

Expand All @@ -47,11 +48,8 @@
suite.addTest(coldcard_test_suite(args.coldcard, rpc, userpass))
if args.ledger:
suite.addTest(ledger_test_suite(rpc, userpass))
if args.digitalbitbox:
if args.password:
suite.addTest(digitalbitbox_test_suite(rpc, userpass, args.password))
else:
print('Cannot run Digital Bitbox test without --password set')
if not args.no_bitbox:
suite.addTest(digitalbitbox_test_suite(rpc, userpass, args.bitbox))
if not args.no_keepkey:
suite.addTest(keepkey_test_suite(args.keepkey, rpc, userpass))
result = unittest.TextTestRunner(stream=sys.stdout, verbosity=2).run(suite)
Expand Down
60 changes: 40 additions & 20 deletions test/test_digitalbitbox.py
Original file line number Diff line number Diff line change
@@ -1,42 +1,62 @@
#! /usr/bin/env python3

import argparse
import atexit
import json
import os
import subprocess
import time
import unittest

from test_device import DeviceTestCase, start_bitcoind, TestDeviceConnect, TestGetKeypool, TestSignTx, TestSignMessage

from hwilib.commands import process_commands

def digitalbitbox_test_suite(rpc, userpass, password):
# Look for real Digital BitBox using HWI API(self-referential, but no other way)
enum_res = process_commands(['-p', password, 'enumerate'])
path = None
master_xpub = None
fingerprint = None
for device in enum_res:
if device['type'] == 'digitalbitbox':
fingerprint = device['fingerprint']
path = device['path']
master_xpub = process_commands(['-f', fingerprint, '-p', password, 'getmasterxpub'])['xpub']
break
assert(path is not None and master_xpub is not None and fingerprint is not None)
from hwilib.devices.digitalbitbox import BitboxSimulator, send_plain, send_encrypt

def digitalbitbox_test_suite(rpc, userpass, simulator):
# Start the Digital bitbox simulator
simulator_proc = subprocess.Popen(['./' + os.path.basename(simulator), '../../tests/sd_files/'], cwd=os.path.dirname(simulator), stderr=subprocess.DEVNULL)
# Wait for simulator to be up
while True:
try:
dev = BitboxSimulator('127.0.0.1', 35345)
reply = send_plain(b'{"password":"0000"}', dev)
if 'error' not in reply:
break
except:
pass
time.sleep(0.5)
# Cleanup
def cleanup_simulator():
simulator_proc.kill()
simulator_proc.wait()
atexit.register(cleanup_simulator)

# Set password and load from backup
send_encrypt(json.dumps({"seed":{"source":"backup","filename":"test_backup.pdf","key":"key"}}), '0000', dev)

# params
type = 'digitalbitbox'
path = 'udp:127.0.0.1:35345'
fingerprint = 'a31b978a'
master_xpub = 'xpub6BsWJiRvbzQJg3J6tgUKmHWYbHJSj41EjAAje6LuDwnYLqLiNSWK4N7rCXwiUmNJTBrKL8AEH3LBzhJdgdxoy4T9aMPLCWAa6eWKGCFjQhq'

# Generic Device tests
suite = unittest.TestSuite()
suite.addTest(DeviceTestCase.parameterize(TestDeviceConnect, rpc, userpass, 'digitalbitbox', path, fingerprint, master_xpub, password))
suite.addTest(DeviceTestCase.parameterize(TestGetKeypool, rpc, userpass, 'digitalbitbox', path, fingerprint, master_xpub, password))
suite.addTest(DeviceTestCase.parameterize(TestSignTx, rpc, userpass, 'digitalbitbox', path, fingerprint, master_xpub, password))
suite.addTest(DeviceTestCase.parameterize(TestSignMessage, rpc, userpass, 'digitalbitbox', path, fingerprint, master_xpub, password))
suite.addTest(DeviceTestCase.parameterize(TestDeviceConnect, rpc, userpass, type, path, fingerprint, master_xpub, '0000'))
suite.addTest(DeviceTestCase.parameterize(TestGetKeypool, rpc, userpass, type, path, fingerprint, master_xpub, '0000'))
suite.addTest(DeviceTestCase.parameterize(TestSignTx, rpc, userpass, type, path, fingerprint, master_xpub, '0000'))
suite.addTest(DeviceTestCase.parameterize(TestSignMessage, rpc, userpass, type, path, fingerprint, master_xpub, '0000'))
return suite

if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Test Digital Bitbox implementation')
parser.add_argument('simulator', help='Path to simulator binary')
parser.add_argument('bitcoind', help='Path to bitcoind binary')
parser.add_argument('password', help='Device password')
args = parser.parse_args()

# Start bitcoind
rpc, userpass = start_bitcoind(args.bitcoind)

suite = digitalbitbox_test_suite(rpc, userpass, args.password)
suite = digitalbitbox_test_suite(rpc, userpass, args.simulator)
unittest.TextTestRunner(verbosity=2).run(suite)

0 comments on commit 8f1dff2

Please sign in to comment.