1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-11-25 17:09:44 +00:00

core: improve emulator wrapper

* supply a gzipped image of an empty SD card so that we don't spend 5s at
  every run by formatting it
* add start, stop, restart methods for direct control
* add restart capability to device_handler (a little awkward for now)
This commit is contained in:
matejcik 2019-10-22 13:54:17 +02:00
parent 8d2ae142f3
commit 5488270bc3
3 changed files with 70 additions and 24 deletions

View File

@ -45,6 +45,13 @@ class BackgroundDeviceHandler:
pass pass
self.task = None self.task = None
def restart(self, emulator):
# TODO handle actual restart as well
self.kill_task()
emulator.restart()
self.client = emulator.client
self.client.ui = NullUI
def result(self): def result(self):
if self.task is None: if self.task is None:
raise RuntimeError("No task running") raise RuntimeError("No task running")

View File

@ -14,6 +14,7 @@
# You should have received a copy of the License along with this library. # 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>. # If not, see <https://www.gnu.org/licenses/lgpl-3.0.html>.
import gzip
import os import os
import subprocess import subprocess
import tempfile import tempfile
@ -21,15 +22,17 @@ import time
from collections import defaultdict from collections import defaultdict
from trezorlib.debuglink import TrezorClientDebugLink from trezorlib.debuglink import TrezorClientDebugLink
from trezorlib.transport import TransportException, get_transport from trezorlib.transport.udp import UdpTransport
BINDIR = os.path.dirname(os.path.abspath(__file__)) + "/emulators" ROOT = os.path.abspath(os.path.dirname(__file__) + "/..")
ROOT = os.path.dirname(os.path.abspath(__file__)) + "/../" BINDIR = ROOT + "/tests/emulators"
LOCAL_BUILD_PATHS = { LOCAL_BUILD_PATHS = {
"core": ROOT + "core/build/unix/micropython", "core": ROOT + "/core/build/unix/micropython",
"legacy": ROOT + "legacy/firmware/trezor.elf", "legacy": ROOT + "/legacy/firmware/trezor.elf",
} }
SD_CARD_GZ = ROOT + "/tests/trezor.sdcard.gz"
ENV = {"SDL_VIDEODRIVER": "dummy"} ENV = {"SDL_VIDEODRIVER": "dummy"}
@ -84,46 +87,68 @@ class EmulatorWrapper:
if storage: if storage:
open(self._storage_file(), "wb").write(storage) open(self._storage_file(), "wb").write(storage)
with gzip.open(SD_CARD_GZ, "rb") as gz:
with open(self.workdir.name + "/trezor.sdcard", "wb") as sd:
sd.write(gz.read())
self.client = None self.client = None
def __enter__(self): def _get_params_core(self):
env = ENV.copy()
args = [self.executable, "-m", "main"]
# for firmware 2.1.2 and newer
env["TREZOR_PROFILE_DIR"] = self.workdir.name
# for firmware 2.1.1 and older
env["TREZOR_PROFILE"] = self.workdir.name
if self.executable == LOCAL_BUILD_PATHS["core"]:
cwd = ROOT + "/core/src"
else:
cwd = self.workdir.name
return env, args, cwd
def _get_params_legacy(self):
env = ENV.copy()
args = [self.executable] args = [self.executable]
env = ENV cwd = self.workdir.name
return env, args, cwd
def _get_params(self):
if self.gen == "core": if self.gen == "core":
args += ["-m", "main"] return self._get_params_core()
# for firmware 2.1.2 and newer elif self.gen == "legacy":
env["TREZOR_PROFILE_DIR"] = self.workdir.name return self._get_params_legacy()
# for firmware 2.1.1 and older else:
env["TREZOR_PROFILE"] = self.workdir.name raise ValueError("Unknown gen")
def start(self):
env, args, cwd = self._get_params()
self.process = subprocess.Popen( self.process = subprocess.Popen(
args, cwd=self.workdir.name, env=env, stdout=open(os.devnull, "w") args, cwd=cwd, env=env, stdout=open(os.devnull, "w")
) )
# wait until emulator is listening # wait until emulator is listening
transport = UdpTransport("127.0.0.1:21324")
transport.open()
for _ in range(300): for _ in range(300):
try: if transport._ping():
time.sleep(0.1)
transport = get_transport("udp:127.0.0.1:21324")
break break
except TransportException:
pass
if self.process.poll() is not None: if self.process.poll() is not None:
self._cleanup() self._cleanup()
raise RuntimeError("Emulator proces died") raise RuntimeError("Emulator proces died")
time.sleep(0.1)
else: else:
# could not connect after 300 attempts * 0.1s = 30s of waiting # could not connect after 300 attempts * 0.1s = 30s of waiting
self._cleanup() self._cleanup()
raise RuntimeError("Can't connect to emulator") raise RuntimeError("Can't connect to emulator")
transport.close()
self.client = TrezorClientDebugLink(transport) self.client = TrezorClientDebugLink(transport)
self.client.open() self.client.open()
check_version(self.tag, self.client.version) check_version(self.tag, self.client.version)
return self
def __exit__(self, exc_type, exc_value, traceback): def stop(self):
self._cleanup()
return False
def _cleanup(self):
if self.client: if self.client:
self.client.close() self.client.close()
self.process.terminate() self.process.terminate()
@ -131,6 +156,20 @@ class EmulatorWrapper:
self.process.wait(1) self.process.wait(1)
except subprocess.TimeoutExpired: except subprocess.TimeoutExpired:
self.process.kill() self.process.kill()
def restart(self):
self.stop()
self.start()
def __enter__(self):
self.start()
return self
def __exit__(self, exc_type, exc_value, traceback):
self._cleanup()
def _cleanup(self):
self.stop()
self.workdir.cleanup() self.workdir.cleanup()
def _storage_file(self): def _storage_file(self):

BIN
tests/trezor.sdcard.gz Normal file

Binary file not shown.