Skip to content

Commit

Permalink
Add test to check status word in Boilerplate client
Browse files Browse the repository at this point in the history
  • Loading branch information
grydz committed Nov 16, 2020
1 parent b6c5ed3 commit 14e473c
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 1 deletion.
2 changes: 2 additions & 0 deletions tests/boilerplate_client/exception/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from .device_exception import DeviceException
from .types import (UnknownDeviceError,
DenyError,
WrongP1P2Error,
WrongDataLengthError,
InsNotSupportedError,
Expand All @@ -8,6 +9,7 @@

__all__ = [
"DeviceException",
"DenyError",
"UnknownDeviceError",
"WrongP1P2Error",
"WrongDataLengthError",
Expand Down
1 change: 1 addition & 0 deletions tests/boilerplate_client/exception/device_exception.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

class DeviceException(Exception): # pylint: disable=too-few-public-methods
exc: Dict[int, Any] = {
0x6985: DenyError,
0x6A86: WrongP1P2Error,
0x6A87: WrongDataLengthError,
0x6D00: InsNotSupportedError,
Expand Down
2 changes: 2 additions & 0 deletions tests/boilerplate_client/exception/types.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
class UnknownDeviceError(Exception):
pass

class DenyError(Exception):
pass

class WrongP1P2Error(Exception):
pass
Expand Down
27 changes: 26 additions & 1 deletion tests/boilerplate_client/utils.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,34 @@
from typing import List
from pathlib import Path
from typing import List, Tuple
import re


SW_RE = re.compile(r"""(?x)
\# # character '#'
define # string 'define'
\s+ # spaces
(?P<identifier>SW(?:_[A-Z0-9]+)*) # identifier (e.g. 'SW_OK')
\s+ # spaces
0x(?P<sw>[a-fA-F0-9]{4}) # 4 bytes status word
""")


def parse_sw(path: Path) -> List[Tuple[str, int]]:
if not path.is_file():
raise FileNotFoundError(f"Can't find file: '{path}'")

sw_h: str = path.read_text()

return [(identifier, int(sw, base=16))
for identifier, sw in SW_RE.findall(sw_h) if sw != "9000"]


def bip32_path_from_string(path: str) -> List[bytes]:
splitted_path: List[str] = path.split("/")

if not splitted_path:
raise Exception(f"BIP32 path format error: '{path}'")

if "m" in splitted_path and splitted_path[0] == "m":
splitted_path = splitted_path[1:]

Expand Down
15 changes: 15 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from pathlib import Path

import pytest

from ledgercomm import Transport
Expand All @@ -10,6 +12,19 @@ def pytest_addoption(parser):
action="store_true")


@pytest.fixture(scope="module")
def sw_h_path():
# path with tests
conftest_folder_path: Path = Path(__file__).parent
# sw.h should be in src/sw.h
sw_h_path = conftest_folder_path.parent / "src" / "sw.h"

if not sw_h_path.is_file():
raise FileNotFoundError(f"Can't find sw.h: '{sw_h_path}'")

return sw_h_path


@pytest.fixture(scope="session")
def hid(pytestconfig):
return pytestconfig.getoption("hid")
Expand Down
3 changes: 3 additions & 0 deletions tests/setup.cfg
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
[tool:pytest]
testpaths = tests
addopts = --strict-markers
markers =
checksw: status word check.

[pylint]
disable = C0114, # missing-module-docstring
Expand Down
21 changes: 21 additions & 0 deletions tests/test_status_word.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from typing import List, Tuple, Dict, Any

import pytest

from boilerplate_client.exception import DeviceException
from boilerplate_client.utils import parse_sw


@pytest.mark.checksw
def test_status_word(sw_h_path):
expected_status_words: List[Tuple[str, int]] = parse_sw(sw_h_path)
status_words: Dict[int, Any] = DeviceException.exc

assert (len(expected_status_words) == len(status_words),
f"{expected_status_words} doesn't match {status_words}")

# just keep status words
expected_status_words = [sw for (identifier, sw) in expected_status_words]

for sw in status_words.keys():
assert sw in expected_status_words, f"{status_words[sw]}({hex(sw)}) not found in sw.h!"

0 comments on commit 14e473c

Please sign in to comment.