Skip to content

Commit

Permalink
[test] Moving C and Python tests in dedicated directories
Browse files Browse the repository at this point in the history
  • Loading branch information
lpascal-ledger committed Dec 20, 2023
1 parent 03c5718 commit 02477ea
Show file tree
Hide file tree
Showing 101 changed files with 36 additions and 47 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ link_libraries(ssl crypto dl blst)
add_subdirectory(src)

if (BUILD_TESTING)
add_subdirectory(tests/syscalls)
add_subdirectory(tests/c/)
endif()

add_custom_target(
Expand Down
1 change: 1 addition & 0 deletions tests/c/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
add_subdirectory(syscalls)
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion tests/syscalls/utils.h → tests/c/syscalls/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include <stddef.h>
#include <stdint.h>

#define TESTS_PATH "../../../tests/syscalls/"
#define TESTS_PATH "../../../../tests/c/syscalls/"
#define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))

int hex2num(char c);
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
20 changes: 1 addition & 19 deletions tests/api/test_api.py → tests/python/api/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,9 @@

from speculos.client import SpeculosClient

AppInfo = namedtuple("AppInfo", ["filepath", "device", "name", "version", "hash"])

SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__))
API_URL = "http://127.0.0.1:5000"


@pytest.fixture(scope="class")
def client(request):
"""Run the API tests on the default btc.elf app."""

app_dir = os.path.join(SCRIPT_DIR, os.pardir, os.pardir, "apps")
filepath = os.path.realpath(os.path.join(app_dir, "btc.elf"))
info = [filepath] + os.path.basename(filepath).split("#")
info = AppInfo(*info)

args = ["--model", info.device, "--sdk", info.version]

with SpeculosClient(app=filepath, args=args) as _client:
yield _client


@pytest.mark.usefixtures("client")
class TestApi:
@staticmethod
Expand Down Expand Up @@ -109,7 +91,7 @@ def get_current_screen_content(session):
assert re.match(text, event["text"])

texts = [("About",), ("Version", ".*"), ("Bitcoin", "is ready")]
for i in range(0, 3):
for i in range(len(texts)):
TestApi.press_button("left")
for text in texts[i]:
event = get_next_event(stream)
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
58 changes: 32 additions & 26 deletions tests/apps/conftest.py → tests/python/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,29 @@
import os
import re
from collections import namedtuple
from pathlib import Path
from typing import List

from speculos.client import SpeculosClient


SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__))
# going back from...(conftest.py \ python \ tests \ git root) / 'apps'
APP_DIR = Path(__file__).resolve().parent. parent. parent / "apps"
AppInfo = namedtuple("AppInfo", ["filepath", "model", "name", "sdk", "hash"])


def app_info_from_path(path) -> AppInfo:
def app_info_from_path(path: Path) -> AppInfo:
# name example: nanos#btc#1.5#5b6693b8.elf
app_regexp = re.compile(r"^(nanos|nanox|blue|nanosp)#([^#]+)#([^#][\d\w\-.]+)#([a-f0-9]*)\.elf$")
filename = os.path.basename(path)
matching = re.match(app_regexp, filename)
matching = re.match(app_regexp, path.name)
if not matching:
return None
assert len(matching.groups()) == 4
return AppInfo(filepath=path, model=matching.group(1), name=matching.group(2), sdk=matching.group(3),
hash=matching.group(4))


def list_apps_to_test(app_dir) -> List[AppInfo]:
def list_apps_to_test() -> List[AppInfo]:
"""
List apps matching the pattern:
Expand All @@ -37,11 +38,10 @@ def list_apps_to_test(app_dir) -> List[AppInfo]:
'apps/nanos#btc#1.5#5b6693b8.elf'
"""
all_apps = []
for filename in os.listdir(app_dir):
if "#" not in filename:
for appfile in APP_DIR.iterdir():
if "#" not in appfile.name:
continue
path = os.path.join(app_dir, filename)
info = app_info_from_path(path)
info = app_info_from_path(appfile)
if not info:
pytest.fail(
f"An unexpected file was found in apps/, with a # but not matching the pattern: {filename!r}"
Expand All @@ -53,25 +53,21 @@ def list_apps_to_test(app_dir) -> List[AppInfo]:

@pytest.fixture(scope="function")
def app(request, client):
return app_info_from_path(client.app)
return app_info_from_path(Path(client.app))


def get_apps(name):
"""Retrieve the list of apps in the ../apps directory."""
app_dir = os.path.join(SCRIPT_DIR, os.pardir, os.pardir, "apps")
apps = list_apps_to_test(app_dir)
apps = [app for app in apps if app.name == name]
return apps
def get_apps(name: str) -> List[AppInfo]:
"""Retrieve the list of apps in the ../../apps directory."""
return [app for app in list_apps_to_test() if app.name == name]


def default_btc_app():
app_dir = os.path.join(SCRIPT_DIR, os.pardir, os.pardir, "apps")
filepath = os.path.realpath(os.path.join(app_dir, "btc.elf"))
def default_btc_app() -> List[AppInfo]:
filepath = (APP_DIR / "btc.elf").resolve()
apps = get_apps("btc")
return [app for app in apps if os.path.realpath(app.filepath) == filepath]
return [app for app in apps if app.filepath == filepath]


def idfn(app):
def idfn(app: Path) -> str:
"""
Set the test ID to the app file name for each test running on a set of apps.
Expand All @@ -80,14 +76,14 @@ def idfn(app):
These IDs can be used with -k to select specific cases to run, and they will
also identify the specific case when one is failing.
"""
return os.path.basename(app.filepath)
return app.filepath


def client_instance(app, additional_args=None):
args = ["--model", app.model, "--sdk", app.sdk]
if additional_args is not None:
args += additional_args
return SpeculosClient(app.filepath, args=args)
return SpeculosClient(str(app.filepath), args=args)


@pytest.fixture(scope="module", params=get_apps("btc"), ids=idfn)
Expand All @@ -99,9 +95,9 @@ def client_btc(request):
@pytest.fixture(scope="module", params=get_apps("btc-test"), ids=idfn)
def client_btc_testnet(request):
app = request.param
btc_app = app.filepath.replace("btc-test", "btc")
assert os.path.exists(btc_app)
args = ["-l", "Bitcoin:%s" % btc_app]
btc_app = app.filepath.parent / app.filepath.name.replace("btc-test", "btc")
assert btc_app.is_file()
args = ["-l", "Bitcoin:%s" % str(btc_app)]

with client_instance(request.param, additional_args=args) as _client:
yield _client
Expand All @@ -123,3 +119,13 @@ def client_vnc(request):
args = list(get_closest_marker("additional_args").args)
with client_instance(request.param, args) as _client:
yield _client


@pytest.fixture(scope="class")
def client(request):
"""Run the API tests on the default btc.elf app."""

info = app_info_from_path((APP_DIR / "btc.elf").resolve())
args = ["--model", info.model, "--sdk", info.sdk]
with SpeculosClient(app=str(info.filepath), args=args) as _client:
yield _client
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit 02477ea

Please sign in to comment.