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 a0349a8 commit 1ab369b
Show file tree
Hide file tree
Showing 102 changed files with 70 additions and 79 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
5 changes: 5 additions & 0 deletions tests/c/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
add_definitions(-DST31)
link_libraries(emu -lcmocka-static)


add_subdirectory(syscalls)
30 changes: 30 additions & 0 deletions tests/c/syscalls/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
add_executable(hello hello.c)

add_executable(test_syscall_aes test_aes.c nist_cavp.c utils.c)
add_executable(test_syscall_bip32 test_bip32.c utils.c)
add_executable(test_syscall_blake2 test_blake2.c nist_cavp.c utils.c)
add_executable(test_syscall_bls test_bls.c utils.c)
add_executable(test_syscall_bn test_bn.c)
add_executable(test_syscall_crc16 test_crc16.c)
add_executable(test_syscall_ecdh test_ecdh.c utils.c)
add_executable(test_syscall_ecdsa test_ecdsa.c utils.c)
add_executable(test_syscall_ec test_ec.c utils.c)
add_executable(test_syscall_ecpoint test_ecpoint.c utils.c)
add_executable(test_syscall_eddsa test_eddsa.c utils.c)
add_executable(test_syscall_eip2333 test_eip2333.c utils.c)
add_executable(test_syscall_endorsement test_endorsement.c)
add_executable(test_syscall_hmac test_hmac.c utils.c)
add_executable(test_syscall_math test_math.c)
add_executable(test_syscall_mpi_rng test_mpi_rng.c utils.c)
add_executable(test_syscall_os_global_pin_is_validated test_os_global_pin_is_validated.c)
add_executable(test_syscall_rfc6979 test_rfc6979.c utils.c)
add_executable(test_syscall_ripemd test_ripemd.c utils.c)
add_executable(test_syscall_sha2 test_sha2.c nist_cavp.c utils.c)
add_executable(test_syscall_sha3 test_sha3.c nist_cavp.c utils.c)
add_executable(test_syscall_slip21 test_slip21.c)

add_test(NAME jello COMMAND qemu-arm-static hello WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
foreach(target aes bip32 blake2 bls bn crc16 ec ecpoint ecdh ecdsa eddsa endorsement hmac
math os_global_pin_is_validated rfc6979 ripemd sha2 sha3 slip21 eip2333)
add_test(NAME test_syscall_${target} COMMAND qemu-arm-static test_syscall_${target} WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
endforeach()
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.
32 changes: 0 additions & 32 deletions tests/syscalls/CMakeLists.txt

This file was deleted.

0 comments on commit 1ab369b

Please sign in to comment.