2019-09-10 09:53:31 +00:00
|
|
|
# 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>.
|
|
|
|
|
2019-08-16 13:29:21 +00:00
|
|
|
import os
|
|
|
|
import subprocess
|
|
|
|
import tempfile
|
|
|
|
import time
|
2019-09-12 10:24:59 +00:00
|
|
|
from collections import defaultdict
|
2019-08-16 13:29:21 +00:00
|
|
|
|
|
|
|
from trezorlib.debuglink import TrezorClientDebugLink
|
|
|
|
from trezorlib.transport import TransportException, get_transport
|
|
|
|
|
|
|
|
BINDIR = os.path.dirname(os.path.abspath(__file__)) + "/emulators"
|
2019-09-11 12:07:22 +00:00
|
|
|
ROOT = os.path.dirname(os.path.abspath(__file__)) + "/../"
|
2019-09-12 10:24:59 +00:00
|
|
|
LOCAL_BUILD_PATHS = {
|
2019-09-11 12:07:22 +00:00
|
|
|
"core": ROOT + "core/build/unix/micropython",
|
|
|
|
"legacy": ROOT + "legacy/firmware/trezor.elf",
|
|
|
|
}
|
|
|
|
|
2019-09-12 10:24:59 +00:00
|
|
|
ENV = {"SDL_VIDEODRIVER": "dummy"}
|
2019-09-11 12:07:22 +00:00
|
|
|
|
|
|
|
|
2019-09-12 10:24:59 +00:00
|
|
|
def check_version(tag, version_tuple):
|
|
|
|
if tag is not None and tag.startswith("v") and len(tag.split(".")) == 3:
|
|
|
|
version = ".".join(str(i) for i in version_tuple)
|
|
|
|
if tag[1:] != version:
|
|
|
|
raise RuntimeError(f"Version mismatch: tag {tag} reports version {version}")
|
2019-09-11 12:07:22 +00:00
|
|
|
|
2019-09-12 10:24:59 +00:00
|
|
|
|
|
|
|
def filename_from_tag(gen, tag):
|
|
|
|
return f"{BINDIR}/trezor-emu-{gen}-{tag}"
|
2019-09-11 12:07:22 +00:00
|
|
|
|
|
|
|
|
|
|
|
def get_tags():
|
2019-09-12 10:24:59 +00:00
|
|
|
files = os.listdir(BINDIR)
|
2019-09-11 12:07:22 +00:00
|
|
|
if not files:
|
|
|
|
raise ValueError(
|
|
|
|
"No files found. Use download_emulators.sh to download emulators."
|
|
|
|
)
|
|
|
|
|
|
|
|
result = defaultdict(list)
|
|
|
|
for f in sorted(files):
|
|
|
|
try:
|
2019-09-12 10:24:59 +00:00
|
|
|
# example: "trezor-emu-core-v2.1.1"
|
2019-09-11 12:07:22 +00:00
|
|
|
_, _, gen, tag = f.split("-", maxsplit=3)
|
|
|
|
result[gen].append(tag)
|
|
|
|
except ValueError:
|
|
|
|
pass
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
|
|
|
ALL_TAGS = get_tags()
|
|
|
|
|
2019-08-16 13:29:21 +00:00
|
|
|
|
|
|
|
class EmulatorWrapper:
|
2019-09-12 10:24:59 +00:00
|
|
|
def __init__(self, gen, tag=None, executable=None, storage=None):
|
2019-08-16 13:29:21 +00:00
|
|
|
self.gen = gen
|
|
|
|
self.tag = tag
|
2019-09-12 10:24:59 +00:00
|
|
|
|
|
|
|
if executable is not None:
|
|
|
|
self.executable = executable
|
|
|
|
elif tag is not None:
|
|
|
|
self.executable = filename_from_tag(gen, tag)
|
|
|
|
else:
|
|
|
|
self.executable = LOCAL_BUILD_PATHS[gen]
|
|
|
|
|
|
|
|
if not os.path.exists(self.executable):
|
|
|
|
raise ValueError(f"emulator executable not found: {self.executable}")
|
|
|
|
|
2019-08-16 13:29:21 +00:00
|
|
|
self.workdir = tempfile.TemporaryDirectory()
|
|
|
|
if storage:
|
|
|
|
open(self._storage_file(), "wb").write(storage)
|
|
|
|
|
2019-09-12 10:24:59 +00:00
|
|
|
self.client = None
|
|
|
|
|
2019-08-16 13:29:21 +00:00
|
|
|
def __enter__(self):
|
2019-09-12 10:24:59 +00:00
|
|
|
args = [self.executable]
|
2019-08-16 13:29:21 +00:00
|
|
|
env = ENV
|
|
|
|
if self.gen == "core":
|
|
|
|
args += ["-m", "main"]
|
2019-09-10 18:56:36 +00:00
|
|
|
# for firmware 2.1.2 and newer
|
2019-08-16 13:29:21 +00:00
|
|
|
env["TREZOR_PROFILE_DIR"] = self.workdir.name
|
2019-09-10 18:56:36 +00:00
|
|
|
# for firmware 2.1.1 and older
|
|
|
|
env["TREZOR_PROFILE"] = self.workdir.name
|
2019-08-16 13:29:21 +00:00
|
|
|
self.process = subprocess.Popen(
|
2019-09-12 10:24:59 +00:00
|
|
|
args, cwd=self.workdir.name, env=env, stdout=open(os.devnull, "w")
|
2019-08-16 13:29:21 +00:00
|
|
|
)
|
2019-09-10 21:34:28 +00:00
|
|
|
# wait until emulator is listening
|
2019-09-12 10:24:59 +00:00
|
|
|
for _ in range(100):
|
2019-08-16 13:29:21 +00:00
|
|
|
try:
|
|
|
|
time.sleep(0.1)
|
2019-09-12 10:24:59 +00:00
|
|
|
transport = get_transport("udp:127.0.0.1:21324")
|
|
|
|
break
|
|
|
|
except TransportException:
|
|
|
|
pass
|
|
|
|
if self.process.poll() is not None:
|
|
|
|
self._cleanup()
|
|
|
|
raise RuntimeError("Emulator proces died")
|
|
|
|
else:
|
|
|
|
# could not connect after 100 attempts * 0.1s = 10s of waiting
|
|
|
|
self._cleanup()
|
|
|
|
raise RuntimeError("Can't connect to emulator")
|
|
|
|
|
|
|
|
self.client = TrezorClientDebugLink(transport)
|
2019-08-16 13:29:21 +00:00
|
|
|
self.client.open()
|
2019-09-12 10:24:59 +00:00
|
|
|
check_version(self.tag, self.client.version)
|
2019-08-16 13:29:21 +00:00
|
|
|
return self
|
|
|
|
|
|
|
|
def __exit__(self, exc_type, exc_value, traceback):
|
2019-09-12 10:24:59 +00:00
|
|
|
self._cleanup()
|
|
|
|
return False
|
|
|
|
|
|
|
|
def _cleanup(self):
|
2019-09-10 21:34:28 +00:00
|
|
|
if self.client:
|
|
|
|
self.client.close()
|
2019-08-16 13:29:21 +00:00
|
|
|
self.process.terminate()
|
|
|
|
try:
|
|
|
|
self.process.wait(1)
|
|
|
|
except subprocess.TimeoutExpired:
|
|
|
|
self.process.kill()
|
|
|
|
self.workdir.cleanup()
|
|
|
|
|
|
|
|
def _storage_file(self):
|
|
|
|
if self.gen == "legacy":
|
|
|
|
return self.workdir.name + "/emulator.img"
|
|
|
|
elif self.gen == "core":
|
|
|
|
return self.workdir.name + "/trezor.flash"
|
|
|
|
else:
|
|
|
|
raise ValueError("Unknown gen")
|
|
|
|
|
|
|
|
def storage(self):
|
|
|
|
return open(self._storage_file(), "rb").read()
|