commit
75264a07a8
@ -0,0 +1,269 @@
|
||||
#!/usr/bin/env python3
|
||||
import gzip
|
||||
import os
|
||||
import platform
|
||||
import signal
|
||||
import subprocess
|
||||
import sys
|
||||
import tempfile
|
||||
import time
|
||||
from pathlib import Path
|
||||
|
||||
import click
|
||||
|
||||
import trezorlib.debuglink
|
||||
import trezorlib.device
|
||||
from trezorlib._internal.emulator import CoreEmulator
|
||||
|
||||
try:
|
||||
import inotify.adapters
|
||||
except ImportError:
|
||||
inotify = None
|
||||
|
||||
|
||||
HERE = Path(__file__).parent.resolve()
|
||||
MICROPYTHON = HERE / "build" / "unix" / "micropython"
|
||||
SRC_DIR = HERE / "src"
|
||||
SD_CARD_GZ = HERE / "trezor.sdcard.gz"
|
||||
|
||||
PROFILING_WRAPPER = HERE / "prof" / "prof.py"
|
||||
|
||||
PROFILE_BASE = Path.home() / ".trezoremu"
|
||||
|
||||
|
||||
def run_command_with_emulator(emulator, command):
|
||||
with emulator:
|
||||
# first start the subprocess
|
||||
process = subprocess.Popen(command)
|
||||
# After the subprocess is started, ignore SIGINT in parent
|
||||
# (so that we don't need to handle KeyboardInterrupts)
|
||||
signal.signal(signal.SIGINT, signal.SIG_IGN)
|
||||
# SIGINTs will be sent to all children by the OS, so we should be able to safely
|
||||
# wait for their exit.
|
||||
return process.wait()
|
||||
|
||||
|
||||
def run_emulator(emulator):
|
||||
with emulator:
|
||||
signal.signal(signal.SIGINT, signal.SIG_IGN)
|
||||
return emulator.wait()
|
||||
|
||||
|
||||
def watch_emulator(emulator):
|
||||
watch = inotify.adapters.InotifyTree(str(SRC_DIR))
|
||||
try:
|
||||
for _, type_names, _, _ in watch.event_gen(yield_nones=False):
|
||||
if "IN_CLOSE_WRITE" in type_names:
|
||||
click.echo("Restarting...")
|
||||
emulator.restart()
|
||||
except KeyboardInterrupt:
|
||||
emulator.stop()
|
||||
return 0
|
||||
|
||||
|
||||
def run_debugger(emulator):
|
||||
os.chdir(emulator.workdir)
|
||||
env = emulator.make_env()
|
||||
if platform.system() == "Darwin":
|
||||
env["PATH"] = "/usr/bin"
|
||||
os.execvpe(
|
||||
"lldb",
|
||||
["lldb", "-f", emulator.executable, "--"] + emulator.make_args(),
|
||||
env,
|
||||
)
|
||||
else:
|
||||
os.execvpe(
|
||||
"gdb", ["gdb", "--args", emulator.executable] + emulator.make_args(), env
|
||||
)
|
||||
|
||||
|
||||
@click.command(context_settings=dict(ignore_unknown_options=True))
|
||||
# fmt: off
|
||||
@click.option("-a", "--disable-animation", is_flag=True, default=os.environ.get("TREZOR_DISABLE_ANIMATION") == "1", help="Disable animation")
|
||||
@click.option("-c", "--command", "run_command", is_flag=True, help="Run command while emulator is running")
|
||||
@click.option("-d", "--production", is_flag=True, default=os.environ.get("PYOPT") == "1", help="Production mode (debuglink disabled)")
|
||||
@click.option("-D", "--debugger", is_flag=True, help="Run emulator in debugger (gdb/lldb)")
|
||||
@click.option("--executable", type=click.Path(exists=True, dir_okay=False), default=os.environ.get("MICROPYTHON"), help="Alternate emulator executable")
|
||||
@click.option("-g", "--profiling", is_flag=True, default=os.environ.get("TREZOR_PROFILING"), help="Run with profiler wrapper")
|
||||
@click.option("-h", "--headless", is_flag=True, help="Headless mode (no display)")
|
||||
@click.option("--heap-size", metavar="SIZE", default="20M", help="Configure heap size")
|
||||
@click.option("--main", help="Path to python main file")
|
||||
@click.option("--mnemonic", "mnemonics", multiple=True, help="Initialize device with given mnemonic. Specify multiple times for Shamir shares.")
|
||||
@click.option("--log-memory", is_flag=True, default=os.environ.get("TREZOR_LOG_MEMORY") == "1", help="Print memory usage after workflows")
|
||||
@click.option("-o", "--output", type=click.File("w"), default="-", help="Redirect emulator output to file")
|
||||
@click.option("-p", "--profile", metavar="NAME", help="Profile name or path")
|
||||
@click.option("-P", "--port", metavar="PORT", type=int, default=int(os.environ.get("TREZOR_UDP_PORT", 0)) or None, help="UDP port number")
|
||||
@click.option("-q", "--quiet", is_flag=True, help="Silence emulator output")
|
||||
@click.option("-s", "--slip0014", is_flag=True, help="Initialize device with SLIP-14 seed (all all all...)")
|
||||
@click.option("-t", "--temporary-profile", is_flag=True, help="Create an empty temporary profile")
|
||||
@click.option("-w", "--watch", is_flag=True, help="Restart emulator if sources change")
|
||||
@click.option("-X", "--extra-arg", "extra_args", multiple=True, help="Extra argument to pass to micropython")
|
||||
# fmt: on
|
||||
@click.argument("command", nargs=-1, type=click.UNPROCESSED)
|
||||
def cli(
|
||||
disable_animation,
|
||||
run_command,
|
||||
production,
|
||||
debugger,
|
||||
executable,
|
||||
profiling,
|
||||
headless,
|
||||
heap_size,
|
||||
main,
|
||||
mnemonics,
|
||||
log_memory,
|
||||
profile,
|
||||
port,
|
||||
output,
|
||||
quiet,
|
||||
slip0014,
|
||||
temporary_profile,
|
||||
watch,
|
||||
extra_args,
|
||||
command,
|
||||
):
|
||||
"""Run the trezor-core emulator.
|
||||
|
||||
If -c is specified, extra arguments are treated as a command that is executed with
|
||||
the running emulator. This command can access the following environment variables:
|
||||
|
||||
\b
|
||||
TREZOR_PROFILE_DIR - path to storage directory
|
||||
TREZOR_PATH - trezorlib connection string
|
||||
TREZOR_UDP_PORT - UDP port on which the emulator listens
|
||||
TREZOR_FIDO2_UDP_PORT - UDP port for FIDO2
|
||||
|
||||
By default, emulator output goes to stdout. If silenced with -q, it is redirected
|
||||
to $TREZOR_PROFILE_DIR/trezor.log. You can also specify a custom path with -o.
|
||||
"""
|
||||
if executable:
|
||||
executable = Path(executable)
|
||||
else:
|
||||
executable = MICROPYTHON
|
||||
|
||||
if command and not run_command:
|
||||
raise click.ClickException("Extra arguments found. Did you mean to use -c?")
|
||||
|
||||
if watch and (command or debugger or frozen):
|
||||
raise click.ClickException("Cannot use -w together with -c or -D or -F")
|
||||
|
||||
if watch and inotify is None:
|
||||
raise click.ClickException("inotify module is missing, install with pip")
|
||||
|
||||
if main and profiling:
|
||||
raise click.ClickException("Cannot use --main and -g together")
|
||||
|
||||
if slip0014 and mnemonics:
|
||||
raise click.ClickException("Cannot use -s and --mnemonic together")
|
||||
|
||||
if slip0014:
|
||||
mnemonics = [" ".join(["all"] * 12)]
|
||||
|
||||
if mnemonics and debugger:
|
||||
raise click.ClickException("Cannot load mnemonics when running in debugger")
|
||||
|
||||
if mnemonics and production:
|
||||
raise click.ClickException("Cannot load mnemonics in production mode")
|
||||
|
||||
if profiling:
|
||||
main_args = [str(PROFILING_WRAPPER)]
|
||||
elif main:
|
||||
main_args = [main]
|
||||
else:
|
||||
main_args = ["-m", "main"]
|
||||
|
||||
if profile and temporary_profile:
|
||||
raise click.ClickException("Cannot use -p and -t together")
|
||||
|
||||
tempdir = None
|
||||
if profile:
|
||||
if "/" in profile:
|
||||
profile_dir = Path(profile)
|
||||
else:
|
||||
profile_dir = PROFILE_BASE / profile
|
||||
|
||||
elif temporary_profile:
|
||||
tempdir = tempfile.TemporaryDirectory(prefix="trezor-emulator-")
|
||||
profile_dir = Path(tempdir.name)
|
||||
# unpack empty SD card for faster start-up
|
||||
with gzip.open(SD_CARD_GZ, "rb") as gz:
|
||||
(profile_dir / "trezor.sdcard").write_bytes(gz.read())
|
||||
|
||||
elif "TREZOR_PROFILE_DIR" in os.environ:
|
||||
profile_dir = Path(os.environ["TREZOR_PROFILE_DIR"])
|
||||
|
||||
else:
|
||||
profile_dir = Path("/var/tmp")
|
||||
|
||||
if quiet:
|
||||
output = None
|
||||
|
||||
emulator = CoreEmulator(
|
||||
executable,
|
||||
profile_dir,
|
||||
logfile=output,
|
||||
port=port,
|
||||
headless=headless,
|
||||
debug=not production,
|
||||
extra_args=extra_args,
|
||||
main_args=main_args,
|
||||
heap_size=heap_size,
|
||||
disable_animation=disable_animation,
|
||||
workdir=SRC_DIR,
|
||||
)
|
||||
|
||||
emulator_env = dict(
|
||||
TREZOR_PATH=f"udp:127.0.0.1:{emulator.port}",
|
||||
TREZOR_PROFILE_DIR=str(profile_dir.resolve()),
|
||||
TREZOR_UDP_PORT=str(emulator.port),
|
||||
TREZOR_FIDO2_UDP_PORT=str(emulator.port + 2),
|
||||
TREZOR_SRC=str(SRC_DIR),
|
||||
)
|
||||
os.environ.update(emulator_env)
|
||||
for k, v in emulator_env.items():
|
||||
click.echo(f"{k}={v}")
|
||||
|
||||
if log_memory:
|
||||
os.environ["TREZOR_LOG_MEMORY"] = "1"
|
||||
|
||||
if debugger:
|
||||
run_debugger(emulator)
|
||||
raise RuntimeError("run_debugger should not return")
|
||||
|
||||
click.echo("Waiting for emulator to come up... ", err=True)
|
||||
start = time.monotonic()
|
||||
emulator.start()
|
||||
end = time.monotonic()
|
||||
click.echo(f"Emulator ready after {end - start:.3f} seconds", err=True)
|
||||
|
||||
if mnemonics:
|
||||
if slip0014:
|
||||
label = "SLIP-0014"
|
||||
elif profile:
|
||||
label = profile_dir.name
|
||||
else:
|
||||
label = "Emulator"
|
||||
|
||||
trezorlib.device.wipe(emulator.client)
|
||||
trezorlib.debuglink.load_device(
|
||||
emulator.client,
|
||||
mnemonics,
|
||||
pin=None,
|
||||
passphrase_protection=False,
|
||||
label=label,
|
||||
)
|
||||
|
||||
if run_command:
|
||||
ret = run_command_with_emulator(emulator, command)
|
||||
elif watch:
|
||||
ret = watch_emulator(emulator)
|
||||
else:
|
||||
ret = run_emulator(emulator)
|
||||
|
||||
if tempdir is not None:
|
||||
tempdir.cleanup()
|
||||
sys.exit(ret)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
cli()
|
@ -1,35 +1,10 @@
|
||||
#!/usr/bin/env bash
|
||||
#!/bin/sh
|
||||
PYOPT="${PYOPT:-1}"
|
||||
|
||||
MICROPYTHON="${MICROPYTHON:-${PWD}/build/unix/micropython}"
|
||||
TREZOR_SRC=$(cd "${PWD}/src/"; pwd)
|
||||
BROWSER="${BROWSER:-chromium}"
|
||||
if [ -n "$1" ]; then
|
||||
echo "This is just a compatibility wrapper. Use emu.py if you want features."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
source ./trezor_cmd.sh
|
||||
|
||||
cd "${TREZOR_SRC}"
|
||||
|
||||
case "$1" in
|
||||
"-d")
|
||||
shift
|
||||
OPERATING_SYSTEM=$(uname)
|
||||
if [ "$OPERATING_SYSTEM" = "Darwin" ]; then
|
||||
PATH=/usr/bin /usr/bin/lldb -f $MICROPYTHON -- $ARGS $* $MAIN
|
||||
else
|
||||
gdb --args $MICROPYTHON $ARGS $* $MAIN
|
||||
fi
|
||||
;;
|
||||
"-r")
|
||||
shift
|
||||
while true; do
|
||||
$MICROPYTHON $ARGS $* $MAIN &
|
||||
UPY_PID=$!
|
||||
find -name '*.py' | inotifywait -q -e close_write --fromfile -
|
||||
echo Restarting ...
|
||||
kill $UPY_PID
|
||||
done
|
||||
;;
|
||||
*)
|
||||
echo "Starting emulator: $MICROPYTHON $ARGS $* $MAIN"
|
||||
$MICROPYTHON $ARGS $* $MAIN 2>&1 | tee "${TREZOR_LOGFILE}"
|
||||
exit ${PIPESTATUS[0]}
|
||||
esac
|
||||
cd src
|
||||
../build/unix/micropython -O$PYOPT -X heapsize=20M -m main
|
||||
|
@ -1,37 +0,0 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
: "${RUN_TEST_EMU:=1}"
|
||||
|
||||
CORE_DIR="$(SHELL_SESSION_FILE='' && cd "$( dirname "${BASH_SOURCE[0]}" )/.." >/dev/null 2>&1 && pwd )"
|
||||
MICROPYTHON="${MICROPYTHON:-$CORE_DIR/build/unix/micropython}"
|
||||
TREZOR_SRC="${CORE_DIR}/src"
|
||||
|
||||
PYOPT="${PYOPT:-0}"
|
||||
upy_pid=""
|
||||
|
||||
# run emulator if RUN_TEST_EMU
|
||||
if [[ $RUN_TEST_EMU > 0 ]]; then
|
||||
source ../trezor_cmd.sh
|
||||
|
||||
# remove flash and sdcard files before run to prevent inconsistent states
|
||||
mv "${TREZOR_PROFILE_DIR}/trezor.flash" "${TREZOR_PROFILE_DIR}/trezor.flash.bkp" 2>/dev/null
|
||||
mv "${TREZOR_PROFILE_DIR}/trezor.sdcard" "${TREZOR_PROFILE_DIR}/trezor.sdcard.bkp" 2>/dev/null
|
||||
|
||||
cd "${TREZOR_SRC}"
|
||||
echo "Starting emulator: $MICROPYTHON $ARGS ${MAIN}"
|
||||
|
||||
TREZOR_TEST=1 \
|
||||
TREZOR_DISABLE_ANIMATION=1 \
|
||||
$MICROPYTHON $ARGS "${MAIN}" &> "${TREZOR_LOGFILE}" &
|
||||
upy_pid=$!
|
||||
cd -
|
||||
sleep 30
|
||||
fi
|
||||
|
||||
# run tests
|
||||
error=0
|
||||
if ! pytest --junitxml=../../tests/junit.xml ../../tests/device_tests "$@"; then
|
||||
error=1
|
||||
fi
|
||||
kill $upy_pid
|
||||
exit $error
|
@ -1,40 +0,0 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
: "${RUN_TEST_EMU:=1}"
|
||||
|
||||
CORE_DIR="$(SHELL_SESSION_FILE='' && cd "$( dirname "${BASH_SOURCE[0]}" )/.." >/dev/null 2>&1 && pwd )"
|
||||
MICROPYTHON="${MICROPYTHON:-$CORE_DIR/build/unix/micropython}"
|
||||
TREZOR_SRC="${CORE_DIR}/src"
|
||||
|
||||
DISABLE_ANIMATION=1
|
||||
PYOPT="${PYOPT:-0}"
|
||||
upy_pid=""
|
||||
|
||||
# run emulator if RUN_TEST_EMU
|
||||
if [[ $RUN_TEST_EMU > 0 ]]; then
|
||||
source ../trezor_cmd.sh
|
||||
|
||||
# remove flash and sdcard files before run to prevent inconsistent states
|
||||
mv "${TREZOR_PROFILE_DIR}/trezor.flash" "${TREZOR_PROFILE_DIR}/trezor.flash.bkp" 2>/dev/null
|
||||
mv "${TREZOR_PROFILE_DIR}/trezor.sdcard" "${TREZOR_PROFILE_DIR}/trezor.sdcard.bkp" 2>/dev/null
|
||||
|
||||
cd "${TREZOR_SRC}"
|
||||
echo "Starting emulator: $MICROPYTHON $ARGS ${MAIN}"
|
||||
|
||||
TREZOR_TEST=1 \
|
||||
TREZOR_DISABLE_ANIMATION=$DISABLE_ANIMATION \
|
||||
$MICROPYTHON $ARGS "${MAIN}" &> "${TREZOR_LOGFILE}" &
|
||||
upy_pid=$!
|
||||
cd -
|
||||
sleep 30
|
||||
fi
|
||||
|
||||
cd ../../tests/fido_tests/fido2
|
||||
# run tests
|
||||
error=0
|
||||
export TREZOR_FIDO2_UDP_PORT=21326
|
||||
if ! pytest --junitxml=../../tests/junit.xml --sim tests/standard/ --vendor trezor "$@"; then
|
||||
error=1
|
||||
fi
|
||||
kill $upy_pid
|
||||
exit $error
|
@ -1,46 +0,0 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
: "${RUN_TEST_EMU:=1}"
|
||||
|
||||
CORE_DIR="$(SHELL_SESSION_FILE='' && cd "$( dirname "${BASH_SOURCE[0]}" )/.." >/dev/null 2>&1 && pwd )"
|
||||
MICROPYTHON="${MICROPYTHON:-$CORE_DIR/build/unix/micropython}"
|
||||
TREZOR_SRC="${CORE_DIR}/src"
|
||||
|
||||
DISABLE_ANIMATION=1
|
||||
PYOPT="${PYOPT:-0}"
|
||||
upy_pid=""
|
||||
|
||||
# run emulator if RUN_TEST_EMU
|
||||
if [[ $RUN_TEST_EMU > 0 ]]; then
|
||||
source ../trezor_cmd.sh
|
||||
|
||||
# remove flash and sdcard files before run to prevent inconsistent states
|
||||
mv "${TREZOR_PROFILE_DIR}/trezor.flash" "${TREZOR_PROFILE_DIR}/trezor.flash.bkp" 2>/dev/null
|
||||
mv "${TREZOR_PROFILE_DIR}/trezor.sdcard" "${TREZOR_PROFILE_DIR}/trezor.sdcard.bkp" 2>/dev/null
|
||||
|
||||
cd "${TREZOR_SRC}"
|
||||
echo "Starting emulator: $MICROPYTHON $ARGS ${MAIN}"
|
||||
|
||||
TREZOR_TEST=1 \
|
||||
TREZOR_DISABLE_ANIMATION=$DISABLE_ANIMATION \
|
||||
$MICROPYTHON $ARGS "${MAIN}" &> "${TREZOR_LOGFILE}" &
|
||||
upy_pid=$!
|
||||
cd -
|
||||
sleep 30
|
||||
fi
|
||||
|
||||
# run tests
|
||||
error=0
|
||||
TREZOR_FIDO2_UDP_PORT=21326
|
||||
# missuse loaddevice test to initialize the device
|
||||
if ! pytest ../../tests/device_tests -k "test_msg_loaddevice" "$@"; then
|
||||
error=1
|
||||
fi
|
||||
if ! ../../tests/fido_tests/u2f-tests-hid/HIDTest "${TREZOR_FIDO2_UDP_PORT}" "$@"; then
|
||||
error=1
|
||||
fi
|
||||
if ! ../../tests/fido_tests/u2f-tests-hid/U2FTest "${TREZOR_FIDO2_UDP_PORT}" "$@"; then
|
||||
error=1
|
||||
fi
|
||||
kill $upy_pid
|
||||
exit $error
|
Binary file not shown.
@ -1,78 +0,0 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
# expected inputs:
|
||||
# TREZOR_SRC -- directory containing python code for uMP
|
||||
|
||||
if [[ ! "${TREZOR_SRC}" ]]; then echo "expecting TREZOR_SRC"; exit 0; fi
|
||||
|
||||
# optional inputs:
|
||||
# TREZOR_PROFILE -- profile name (directory) in ~/.trezoremu or full path
|
||||
# TREZOR_PROFILING -- wrap the uMP/python in the profiler script
|
||||
|
||||
# outputs:
|
||||
## uMP
|
||||
# PYOPT
|
||||
# HEAPSIZE
|
||||
# ARGS -- uMP arguments
|
||||
# MAIN -- uMP file to execute
|
||||
## Trezor core
|
||||
# TREZOR_PROFILE_DIR
|
||||
# TREZOR_PROFILE_NAME
|
||||
# TREZOR_UDP_PORT
|
||||
## this script
|
||||
# TREZOR_SRC
|
||||
# TREZOR_LOGFILE
|
||||
## python-trezor
|
||||
# TREZOR_PATH -- connect string
|
||||
|
||||
|
||||
# defaults
|
||||
PYOPT="${PYOPT:-1}"
|
||||
HEAPSIZE="${HEAPSIZE:-20M}"
|
||||
|
||||
TREZOR_PROFILE="${TREZOR_PROFILE:-/var/tmp}"
|
||||
TREZOR_PROFILE_DIR="${TREZOR_PROFILE}"
|
||||
TREZOR_PROFILE_NAME="${TREZOR_PROFILE}"
|
||||
|
||||
# for profile names create profile directory if not existent
|
||||
if ! [[ "$TREZOR_PROFILE" == "/"* ]]; then
|
||||
TREZOR_PROFILE_DIR="${HOME}/.trezoremu/${TREZOR_PROFILE}"
|
||||
if ! [[ -d "${TREZOR_PROFILE_DIR}" ]]; then
|
||||
mkdir -p "${TREZOR_PROFILE_DIR}"
|
||||
PORT=$(( ( RANDOM % 1000 ) + 1 + 21324 ))
|
||||
echo "# autogenerated config" > "${TREZOR_PROFILE_DIR}/emu.config"
|
||||
echo "TREZOR_UDP_PORT=\"\${TREZOR_UDP_PORT:-${PORT}}\"" >> "${TREZOR_PROFILE_DIR}/emu.config"
|
||||
fi
|
||||
fi
|
||||
|
||||
# load profile config
|
||||
if [[ -f "${TREZOR_PROFILE_DIR}/emu.config" ]]; then
|
||||
source "${TREZOR_PROFILE_DIR}/emu.config"
|
||||
fi
|
||||
|
||||
# for profiling wrap
|
||||
if [[ "$TREZOR_PROFILING" -gt 0 ]]; then
|
||||
MAIN="${TREZOR_SRC}/../prof/prof.py"
|
||||
else
|
||||
MAIN="${TREZOR_SRC}/main.py"
|
||||
fi
|
||||
|
||||
TREZOR_LOGFILE="${TREZOR_PROFILE_DIR}/trezor.log"
|
||||
TREZOR_UDP_PORT="${TREZOR_UDP_PORT:-21324}"
|
||||
TREZOR_PATH="${TREZOR_PATH:-udp:127.0.0.1:${TREZOR_UDP_PORT}}"
|
||||
|
||||
echo "Trezor^emu profile name: ${TREZOR_PROFILE_NAME}"
|
||||
echo "Trezor^emu profile directory: ${TREZOR_PROFILE_DIR}"
|
||||
echo "Trezor^emu log file: ${TREZOR_LOGFILE}"
|
||||
echo "Trezor^emu UDP port: ${TREZOR_UDP_PORT}"
|
||||
echo "Trezor^emu path: ${TREZOR_PATH}"
|
||||
echo "Trezor^emu src: ${TREZOR_SRC}"
|
||||
|
||||
export TREZOR_PROFILE_NAME="${TREZOR_PROFILE_NAME}"
|
||||
export TREZOR_PROFILE_DIR="${TREZOR_PROFILE_DIR}"
|
||||
export TREZOR_LOGFILE="${TREZOR_LOGFILE}"
|
||||
export TREZOR_UDP_PORT="${TREZOR_UDP_PORT}"
|
||||
export TREZOR_PATH="${TREZOR_PATH}"
|
||||
export TREZOR_SRC="${TREZOR_SRC}"
|
||||
|
||||
ARGS="-O${PYOPT} -X heapsize=${HEAPSIZE}"
|
@ -0,0 +1,237 @@
|
||||
# This file is part of the Trezor project.
|
||||
#
|
||||
# Copyright (C) 2012-2019 SatoshiLabs and contributors
|
||||
#
|
||||
# This library is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU Lesser General Public License version 3
|
||||
# as published by the Free Software Foundation.
|
||||
#
|
||||
# This library is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU Lesser General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the License along with this library.
|
||||
# If not, see <https://www.gnu.org/licenses/lgpl-3.0.html>.
|
||||
|
||||
import os
|
||||
import subprocess
|
||||
import time
|
||||
from pathlib import Path
|
||||
|
||||
from trezorlib.debuglink import TrezorClientDebugLink
|
||||
from trezorlib.transport.udp import UdpTransport
|
||||
|
||||
|
||||
def _rm_f(path):
|
||||
try:
|
||||
path.unlink()
|
||||
except FileNotFoundError:
|
||||
pass
|
||||
|
||||
|
||||
class Emulator:
|
||||
STORAGE_FILENAME = None
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
executable,
|
||||
profile_dir,
|
||||
*,
|
||||
logfile=None,
|
||||
storage=None,
|
||||
headless=False,
|
||||
debug=True,
|
||||
extra_args=()
|
||||
):
|
||||
self.executable = Path(executable).resolve()
|
||||
if not executable.exists():
|
||||
raise ValueError(
|
||||
"emulator executable not found: {}".format(self.executable)
|
||||
)
|
||||
|
||||
self.profile_dir = Path(profile_dir).resolve()
|
||||
if not self.profile_dir.exists():
|
||||
self.profile_dir.mkdir(parents=True)
|
||||
elif not self.profile_dir.is_dir():
|
||||
raise ValueError("profile_dir is not a directory")
|
||||
|
||||
self.workdir = self.profile_dir
|
||||
|
||||
self.storage = self.profile_dir / self.STORAGE_FILENAME
|
||||
if storage:
|
||||
self.storage.write_bytes(storage)
|
||||
|
||||
if logfile:
|
||||
self.logfile = logfile
|
||||
else:
|
||||
self.logfile = self.profile_dir / "trezor.log"
|
||||
|
||||
self.client = None
|
||||
self.process = None
|
||||
|
||||
self.port = 21324
|
||||
self.headless = headless
|
||||
self.debug = debug
|
||||
self.extra_args = list(extra_args)
|
||||
|
||||
def make_args(self):
|
||||
return []
|
||||
|
||||
def make_env(self):
|
||||
return os.environ.copy()
|
||||
|
||||
def _get_transport(self):
|
||||
return UdpTransport("127.0.0.1:{}".format(self.port))
|
||||
|
||||
def wait_until_ready(self, timeout=30):
|
||||
transport = self._get_transport()
|
||||
transport.open()
|
||||
start = time.monotonic()
|
||||
try:
|
||||
while True:
|
||||
if transport._ping():
|
||||
break
|
||||
if self.process.poll() is not None:
|
||||
raise RuntimeError("Emulator proces died")
|
||||
|
||||
elapsed = time.monotonic() - start
|
||||
if elapsed >= timeout:
|
||||
raise RuntimeError("Can't connect to emulator")
|
||||
|
||||
time.sleep(0.1)
|
||||
finally:
|
||||
transport.close()
|
||||
|
||||
def wait(self, timeout=None):
|
||||
ret = self.process.wait(timeout=None)
|
||||
self.stop()
|
||||
return ret
|
||||
|
||||
def launch_process(self):
|
||||
args = self.make_args()
|
||||
env = self.make_env()
|
||||
|
||||
if hasattr(self.logfile, "write"):
|
||||
output = self.logfile
|
||||
else:
|
||||
output = open(self.logfile, "w")
|
||||
|
||||
return subprocess.Popen(
|
||||
[self.executable] + args + self.extra_args,
|
||||
cwd=self.workdir,
|
||||
stdout=output,
|
||||
stderr=subprocess.STDOUT,
|
||||
env=env,
|
||||
)
|
||||
|
||||
def start(self):
|
||||
if self.process:
|
||||
if self.process.poll() is not None:
|
||||
# process has died, stop and start again
|
||||
self.stop()
|
||||
else:
|
||||
# process is running, no need to start again
|
||||
return
|
||||
|
||||
self.process = self.launch_process()
|
||||
self.wait_until_ready()
|
||||
|
||||
(self.profile_dir / "trezor.pid").write_text(str(self.process.pid) + "\n")
|
||||
(self.profile_dir / "trezor.port").write_text(str(self.port) + "\n")
|
||||
|
||||
transport = self._get_transport()
|
||||
self.client = TrezorClientDebugLink(transport, auto_interact=self.debug)
|
||||
|
||||
self.client.open()
|
||||
|
||||
def stop(self):
|
||||
if self.client:
|
||||
self.client.close()
|
||||
self.client = None
|
||||
|
||||
if self.process:
|
||||
self.process.terminate()
|
||||
try:
|
||||
self.process.wait(1)
|
||||
except subprocess.TimeoutExpired:
|
||||
self.process.kill()
|
||||
|
||||
_rm_f(self.profile_dir / "trezor.pid")
|
||||
_rm_f(self.profile_dir / "trezor.port")
|
||||
self.process = None
|
||||
|
||||
def restart(self):
|
||||
self.stop()
|
||||
self.start()
|
||||
|
||||
def __enter__(self):
|
||||
return self
|
||||
|
||||
def __exit__(self, exc_type, exc_value, traceback):
|
||||
self.stop()
|
||||
|
||||
def get_storage(self):
|
||||
return self.storage.read_bytes()
|
||||
|
||||
|
||||
class CoreEmulator(Emulator):
|
||||
STORAGE_FILENAME = "trezor.flash"
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
*args,
|
||||
port=None,
|
||||
main_args=("-m", "main"),
|
||||
workdir=None,
|
||||
sdcard=None,
|
||||
disable_animation=True,
|
||||
heap_size="20M",
|
||||
**kwargs
|
||||
):
|
||||
super().__init__(*args, **kwargs)
|
||||
if workdir is not None:
|
||||
self.workdir = Path(workdir).resolve()
|
||||
|
||||
self.sdcard = self.profile_dir / "trezor.sdcard"
|
||||
if sdcard is not None:
|
||||
self.sdcard.write_bytes(sdcard)
|
||||
|
||||
if port:
|
||||
self.port = port
|
||||
self.disable_animation = disable_animation
|
||||
self.main_args = list(main_args)
|
||||
self.heap_size = heap_size
|
||||
|
||||
def make_env(self):
|
||||
env = super().make_env()
|
||||
env.update(
|
||||
TREZOR_PROFILE_DIR=str(self.profile_dir),
|
||||
TREZOR_PROFILE=str(self.profile_dir),
|
||||
TREZOR_UDP_PORT=str(self.port),
|
||||
)
|
||||
if self.headless:
|
||||
env["SDL_VIDEODRIVER"] = "dummy"
|
||||
if self.disable_animation:
|
||||
env["TREZOR_DISABLE_FADE"] = "1"
|
||||
env["TREZOR_DISABLE_ANIMATION"] = "1"
|
||||
|
||||
return env
|
||||
|
||||
def make_args(self):
|
||||
pyopt = "-O0" if self.debug else "-O1"
|
||||
return (
|
||||
[pyopt, "-X", "heapsize={}".format(self.heap_size)]
|
||||
+ self.main_args
|
||||
+ self.extra_args
|
||||
)
|
||||
|
||||
|
||||
class LegacyEmulator(Emulator):
|
||||
STORAGE_FILENAME = "emulator.img"
|
||||
|
||||
def make_env(self):
|
||||
env = super().make_env()
|
||||
if self.headless:
|
||||
env["SDL_VIDEODRIVER"] = "dummy"
|
||||
return env
|
Loading…
Reference in new issue