-
Notifications
You must be signed in to change notification settings - Fork 198
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge #105: Automated tests for Digital Bitbox
0b870d8 Document how to build the simulator (Andrew Chow) 8f1dff2 Change Digital Bitbox test to use simulator (Andrew Chow) 07c9771 Build digital bitbox simulator in setup_environment.sh (Andrew Chow) 814d02d Implement support for communicating with a dbb simulator (Andrew Chow) Pull request description: This PR changes the current manual Digital Bitbox tests into an automated test. This uses the [simulator that I wrote](https://github.com/achow101/mcu/blob/simulator/src/simulator.c) (which is also [PR'd](BitBoxSwiss/mcu#253) to Digital Bitbox). Built on #104 Tree-SHA512: 582afa5e045c1e222064958b1b9dda17db7025dd5ff3f8a64a826abe92631b0dd808fcded501bedec7fc1d8ef4c386c99212ff1b182d149e182ea472f65d92ea
- Loading branch information
Showing
5 changed files
with
153 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |