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 tempfile
|
2019-09-12 10:24:59 +00:00
|
|
|
from collections import defaultdict
|
2020-01-23 17:41:25 +00:00
|
|
|
from pathlib import Path
|
2019-08-16 13:29:21 +00:00
|
|
|
|
2020-01-23 17:41:25 +00:00
|
|
|
from trezorlib._internal.emulator import CoreEmulator, LegacyEmulator
|
|
|
|
|
|
|
|
ROOT = Path(__file__).parent.parent.resolve()
|
|
|
|
BINDIR = ROOT / "tests" / "emulators"
|
2019-08-16 13:29:21 +00:00
|
|
|
|
2019-09-12 10:24:59 +00:00
|
|
|
LOCAL_BUILD_PATHS = {
|
2020-07-27 14:59:51 +00:00
|
|
|
"core": ROOT / "core" / "build" / "unix" / "trezor-emu-core",
|
2020-01-23 17:41:25 +00:00
|
|
|
"legacy": ROOT / "legacy" / "firmware" / "trezor.elf",
|
2019-09-11 12:07:22 +00:00
|
|
|
}
|
|
|
|
|
2020-01-23 17:41:25 +00:00
|
|
|
CORE_SRC_DIR = ROOT / "core" / "src"
|
2019-10-22 11:54:17 +00:00
|
|
|
|
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):
|
2020-01-23 17:41:25 +00:00
|
|
|
return BINDIR / f"trezor-emu-{gen}-{tag}"
|
2019-09-11 12:07:22 +00:00
|
|
|
|
|
|
|
|
|
|
|
def get_tags():
|
2020-01-23 17:41:25 +00:00
|
|
|
files = list(BINDIR.iterdir())
|
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:
|
2020-08-27 18:24:24 +00:00
|
|
|
# example: "trezor-emu-core-v2.1.1" or "trezor-emu-core-v2.1.1-46ab42fw"
|
2020-01-23 17:41:25 +00:00
|
|
|
_, _, gen, tag = f.name.split("-", maxsplit=3)
|
2019-09-11 12:07:22 +00:00
|
|
|
result[gen].append(tag)
|
|
|
|
except ValueError:
|
|
|
|
pass
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
|
|
|
ALL_TAGS = get_tags()
|
|
|
|
|
2019-08-16 13:29:21 +00:00
|
|
|
|
|
|
|
class EmulatorWrapper:
|
2020-01-23 17:41:25 +00:00
|
|
|
def __init__(self, gen, tag=None, storage=None):
|
|
|
|
if tag is not None:
|
|
|
|
executable = filename_from_tag(gen, tag)
|
2019-10-22 11:54:17 +00:00
|
|
|
else:
|
2020-01-23 17:41:25 +00:00
|
|
|
executable = LOCAL_BUILD_PATHS[gen]
|
2019-10-22 11:54:17 +00:00
|
|
|
|
2020-01-23 17:41:25 +00:00
|
|
|
if not executable.exists():
|
|
|
|
raise ValueError(f"emulator executable not found: {executable}")
|
2019-10-22 11:54:17 +00:00
|
|
|
|
2020-01-23 17:41:25 +00:00
|
|
|
self.profile_dir = tempfile.TemporaryDirectory()
|
|
|
|
if executable == LOCAL_BUILD_PATHS["core"]:
|
|
|
|
workdir = CORE_SRC_DIR
|
2019-10-22 11:54:17 +00:00
|
|
|
else:
|
2020-01-23 17:41:25 +00:00
|
|
|
workdir = None
|
|
|
|
|
|
|
|
if gen == "legacy":
|
|
|
|
self.emulator = LegacyEmulator(
|
2020-09-25 11:59:16 +00:00
|
|
|
executable,
|
|
|
|
self.profile_dir.name,
|
|
|
|
storage=storage,
|
|
|
|
headless=True,
|
2020-01-23 17:41:25 +00:00
|
|
|
)
|
|
|
|
elif gen == "core":
|
2020-02-17 15:45:27 +00:00
|
|
|
self.emulator = CoreEmulator(
|
|
|
|
executable,
|
|
|
|
self.profile_dir.name,
|
|
|
|
storage=storage,
|
|
|
|
workdir=workdir,
|
|
|
|
headless=True,
|
|
|
|
)
|
2019-10-22 11:54:17 +00:00
|
|
|
|
|
|
|
def __enter__(self):
|
2020-01-23 17:41:25 +00:00
|
|
|
self.emulator.start()
|
|
|
|
return self.emulator
|
2019-10-22 11:54:17 +00:00
|
|
|
|
|
|
|
def __exit__(self, exc_type, exc_value, traceback):
|
2020-01-23 17:41:25 +00:00
|
|
|
self.emulator.stop()
|
|
|
|
self.profile_dir.cleanup()
|