From f33aad45fefc0c4d2955c409e9db1e9f851179b2 Mon Sep 17 00:00:00 2001 From: Alexandre Paillier Date: Mon, 28 Nov 2022 18:33:52 +0100 Subject: [PATCH] Ragger tests can now run with a display; disabled by default --- tests/ragger/conftest.py | 28 +++++++++++++++++++--------- tests/ragger/requirements.txt | 2 +- 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/tests/ragger/conftest.py b/tests/ragger/conftest.py index e527350d..74645c40 100644 --- a/tests/ragger/conftest.py +++ b/tests/ragger/conftest.py @@ -14,6 +14,7 @@ def pytest_addoption(parser): parser.addoption("--backend", action="store", default="speculos") parser.addoption("--path", action="store", default="./elfs") parser.addoption("--model", action="store", required=True) + parser.addoption("--display", action="store_true", default=False) # accessing the value of the "--backend" option as a fixture @pytest.fixture @@ -28,6 +29,10 @@ def arg_path(pytestconfig) -> str: def arg_model(pytestconfig) -> str: return pytestconfig.getoption("model") +@pytest.fixture +def arg_display(pytestconfig) -> bool: + return pytestconfig.getoption("display") + # Providing the firmware as a fixture @pytest.fixture def firmware(arg_model: str) -> Firmware: @@ -36,30 +41,35 @@ def firmware(arg_model: str) -> Firmware: return fw raise ValueError("Unknown device model \"%s\"" % (arg_model)) -def get_elf_path(arg_path: str, firmware: Firmware) -> Path: +def get_speculos_args(arg_path: str, display: bool, fw: Firmware): + extra_args = list() + + if display: + extra_args += ["--display", "qt"] elf_dir = Path(arg_path).resolve() assert elf_dir.is_dir(), ("%s is not a directory" % (arg_path)) - app = elf_dir / ("app-%s.elf" % firmware.device) + app = elf_dir / ("app-%s.elf" % (fw.device)) assert app.is_file(), ("Firmware %s does not exist !" % (app)) - return app + return (app, {"args": extra_args}) # Depending on the "--backend" option value, a different backend is # instantiated, and the tests will either run on Speculos or on a physical # device depending on the backend -def create_backend(backend: str, arg_path: str, firmware: Firmware) -> BackendInterface: +def create_backend(backend: str, arg_path: str, display: bool, fw: Firmware) -> BackendInterface: if backend.lower() == "ledgercomm": - return LedgerCommBackend(firmware, interface="hid") + return LedgerCommBackend(fw, interface="hid") elif backend.lower() == "ledgerwallet": - return LedgerWalletBackend(firmware) + return LedgerWalletBackend(fw) elif backend.lower() == "speculos": - return SpeculosBackend(get_elf_path(arg_path, firmware), firmware) + app, args = get_speculos_args(arg_path, display, fw) + return SpeculosBackend(app, fw, **args) else: raise ValueError(f"Backend '{backend}' is unknown. Valid backends are: {BACKENDS}") # This fixture will create and return the backend client @pytest.fixture -def backend_client(arg_backend: str, arg_path: str, firmware: Firmware) -> BackendInterface: - with create_backend(arg_backend, arg_path, firmware) as b: +def backend_client(arg_backend: str, arg_path: str, arg_display: bool, firmware: Firmware) -> BackendInterface: + with create_backend(arg_backend, arg_path, arg_display, firmware) as b: yield b # This final fixture will return the properly configured app client, to be used in tests diff --git a/tests/ragger/requirements.txt b/tests/ragger/requirements.txt index 8836582c..dfb1d447 100644 --- a/tests/ragger/requirements.txt +++ b/tests/ragger/requirements.txt @@ -1,6 +1,6 @@ requests>=2.28,<3.0 click>=8.0,<9.0 # needed by the CI as it installs an older version and breaks dependencies protobuf==3.20.1 # To fix the protobuf dependency bug -ragger[speculos] +ragger[speculos]==0.8.1 pytest ecdsa