Skip to content

Commit

Permalink
Add scripts to run tests and prepare test environments
Browse files Browse the repository at this point in the history
Add a run_tests.py script which runs all of the tests.

Add a setup_environment.sh script which will download and build the Trezor
emulator, Coldcard simulator, and bitcoind.
  • Loading branch information
achow101 committed Dec 4, 2018
1 parent 993b6e8 commit 8aa0471
Show file tree
Hide file tree
Showing 4 changed files with 164 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ build/
dist/
hwi.egg-info/
test/emulator.img
test/work
30 changes: 30 additions & 0 deletions test/data/coldcard-linux-sock.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
From d1a3a1cef890ebe4ff72a8f89cd6c56dca89747e Mon Sep 17 00:00:00 2001
From: Andrew Chow <[email protected]>
Date: Tue, 27 Nov 2018 17:32:44 -0500
Subject: [PATCH] Use linux unix socket address format

---
unix/frozen-modules/pyb.py | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/unix/frozen-modules/pyb.py b/unix/frozen-modules/pyb.py
index 39778a2..0108516 100644
--- a/unix/frozen-modules/pyb.py
+++ b/unix/frozen-modules/pyb.py
@@ -23,10 +23,10 @@ class USB_HID:
import usocket as socket
fn = b'/tmp/ckcc-simulator.sock'
self.pipe = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM)
- addr = bytes([len(fn)+2, socket.AF_UNIX] + list(fn))
+ # addr = bytes([len(fn)+2, socket.AF_UNIX] + list(fn))
# If on linux, try uncommenting the following two lines
- #import struct
- #addr = struct.pack('H108s', socket.AF_UNIX, fn)
+ import struct
+ addr = struct.pack('H108s', socket.AF_UNIX, fn)
while 1:
try:
self.pipe.bind(addr)
--
2.11.0

25 changes: 25 additions & 0 deletions test/run_tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#! /usr/bin/env python3

import argparse
import multiprocessing
import os
import subprocess
import sys

parser = argparse.ArgumentParser(description='Setup the testing environment and run automated tests')
trezor_group = parser.add_mutually_exclusive_group()
trezor_group.add_argument('--no_trezor', help='Do not run Trezor test with emulator', action='store_true')
trezor_group.add_argument('--trezor', help='Path to Trezor emulator.', default='work/trezor-mcu/firmware/trezor.elf')
coldcard_group = parser.add_mutually_exclusive_group()
coldcard_group.add_argument('--no_coldcard', help='Do not run Coldcard test with simulator', action='store_true')
coldcard_group.add_argument('--coldcard', help='Path to Coldcard simulator.', default='work/firmware/unix/headless.py')
parser.add_argument('--bitcoind', help='Path to bitcoind.', default='work/bitcoin/src/bitcoind')
args = parser.parse_args()

# Run tests
subprocess.check_call(['python3', 'test_bech32.py'])
subprocess.check_call(['python3', 'test_psbt.py'])
if not args.no_trezor:
subprocess.check_call(['python3', 'test_trezor.py', args.trezor, args.bitcoind])
if not args.no_coldcard:
subprocess.check_call(['python3', 'test_coldcard.py', args.coldcard, args.bitcoind])
108 changes: 108 additions & 0 deletions test/setup_environment.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
#! /usr/bin/env bash

# Makes debugging easier
set -x

# Go into the working directory
mkdir -p work
cd work

# Clone trezor-mcu if it doesn't exist, or update it if it does
trezor_setup_needed=false
if [ ! -d "trezor-mcu" ]; then
git clone --recursive https://github.com/trezor/trezor-mcu.git
cd trezor-mcu
trezor_setup_needed=true
else
cd trezor-mcu
git fetch

# Determine if we need to pull. From https://stackoverflow.com/a/3278427
UPSTREAM=${1:-'@{u}'}
LOCAL=$(git rev-parse @)
REMOTE=$(git rev-parse "$UPSTREAM")
BASE=$(git merge-base @ "$UPSTREAM")

if [ $LOCAL = $REMOTE ]; then
echo "Up-to-date"
elif [ $LOCAL = $BASE ]; then
git pull
trezor_setup_needed=true
fi
fi

# Build emulator. This is pretty fast, so rebuilding every time is ok
# But there should be some caching that makes this faster
export EMULATOR=1 TREZOR_TRANSPORT_V1=1 DEBUG_LINK=1 HEADLESS=1
if [ "$trezor_setup_needed" == true ] ; then
script/setup
pipenv install
fi
pipenv run script/cibuild
cd ..

# Clone coldcard firmware if it doesn't exist, or update it if it does
coldcard_setup_needed=false
if [ ! -d "firmware" ]; then
git clone --recursive https://github.com/Coldcard/firmware.git
cd firmware
coldcard_setup_needed=true
else
cd firmware
git reset --hard HEAD^ # Undo git-am for checking and updating
git fetch

# Determine if we need to pull. From https://stackoverflow.com/a/3278427
UPSTREAM=${1:-'@{u}'}
LOCAL=$(git rev-parse @)
REMOTE=$(git rev-parse "$UPSTREAM")
BASE=$(git merge-base @ "$UPSTREAM")

if [ $LOCAL = $REMOTE ]; then
echo "Up-to-date"
elif [ $LOCAL = $BASE ]; then
git pull
coldcard_setup_needed=true
fi
fi
# Apply patch to make simulator work in linux environments
git am ../../data/coldcard-linux-sock.patch

# Build the simulator. This is cached, but it is also fast
cd unix
if [ "$coldcard_setup_needed" == true ] ; then
make setup
fi
make -j$(nproc)
cd ../..

# Clone bitcoind if it doesn't exist, or update it if it does
bitcoind_setup_needed=false
if [ ! -d "bitcoin" ]; then
git clone https://github.com/achow101/bitcoin.git -b hww
cd bitcoin
bitcoind_setup_needed=true
else
cd bitcoin
git fetch

# Determine if we need to pull. From https://stackoverflow.com/a/3278427
UPSTREAM=${1:-'@{u}'}
LOCAL=$(git rev-parse @)
REMOTE=$(git rev-parse "$UPSTREAM")
BASE=$(git merge-base @ "$UPSTREAM")

if [ $LOCAL = $REMOTE ]; then
echo "Up-to-date"
elif [ $LOCAL = $BASE ]; then
git pull
bitcoind_setup_needed=true
fi
fi

# Build bitcoind. This is super slow, but it is cached so it runs fairly quickly.
if [ "$bitcoind_setup_needed" == true ] ; then
./autogen.sh
./configure
fi
make -j$(nproc) src/bitcoind

0 comments on commit 8aa0471

Please sign in to comment.