From 0d38bea388dfda05da7fefd2ebed839f3853456f Mon Sep 17 00:00:00 2001 From: Alexandre Paillier Date: Thu, 4 Aug 2022 17:00:55 +0200 Subject: [PATCH] Ragger tests - Refactored the handling of the app settings --- tests/ragger/ethereum_client.py | 60 +++++++++++++++++++++++---------- tests/ragger/test_eip712.py | 15 ++++++--- 2 files changed, 53 insertions(+), 22 deletions(-) diff --git a/tests/ragger/ethereum_client.py b/tests/ragger/ethereum_client.py index fc3c4c5..81a1af6 100644 --- a/tests/ragger/ethereum_client.py +++ b/tests/ragger/ethereum_client.py @@ -1,7 +1,6 @@ from contextlib import contextmanager -from typing import Generator from enum import IntEnum, auto -from typing import Iterator +from typing import Iterator, Dict, List from ragger.backend import BackendInterface from ragger.utils import RAPDU import signal @@ -33,6 +32,19 @@ class EIP712FieldType(IntEnum): FIX_BYTES = auto() DYN_BYTES = auto() +class SettingType(IntEnum): + BLIND_SIGNING = 0, + DEBUG_DATA = auto() + NONCE = auto() + VERBOSE_EIP712 = auto() + +class Setting: + devices: List[str] + value: bool + + def __init__(self, d: List[str]): + self.devices = d + class EthereumClientCmdBuilder: _CLA: int = 0xE0 @@ -168,15 +180,30 @@ class EthereumResponseParser: return v, r, s class EthereumClient: - _verbose_eip712 = False + _settings: Dict[SettingType, Setting] = { + SettingType.BLIND_SIGNING: Setting( + [ "nanos", "nanox", "nanosp" ] + ), + SettingType.DEBUG_DATA: Setting( + [ "nanos", "nanox", "nanosp" ] + ), + SettingType.NONCE: Setting( + [ "nanos", "nanox", "nanosp" ] + ), + SettingType.VERBOSE_EIP712: Setting( + [ "nanox", "nanosp" ] + ) + } + _click_delay = 1/4 def __init__(self, client: BackendInterface, debug: bool = False): self._client = client self._debug = debug self._cmd_builder = EthereumClientCmdBuilder() self._resp_parser = EthereumResponseParser() - self._click_delay = 1/4 signal.signal(signal.SIGALRM, self._click_signal_timeout) + for setting in self._settings.values(): + setting.value = False def _send(self, payload: bytearray): return self._client.exchange_async_raw(payload) @@ -235,7 +262,7 @@ class EthereumClient: def eip712_sign_new(self, bip32): with self._send(self._cmd_builder.eip712_sign_new(bip32)): - if not self._verbose_eip712: # need to skip the message hash + if not self._settings[SettingType.VERBOSE_EIP712].value: # need to skip the message hash self._client.right_click() self._client.right_click() self._client.both_click() # approve signature @@ -265,18 +292,17 @@ class EthereumClient: assert resp.status == 0x9000 return self._resp_parser.sign(resp.data) - def setting_toggle_verbose_eip712(self): + def settings_set(self, new_values: Dict[SettingType, bool]): # Go to settings - self._client.right_click() - self._client.right_click() - self._client.both_click() - # Go to verbose eip712 - self._client.right_click() - self._client.right_click() - self._client.right_click() - self._client.both_click() - # Go back - self._client.right_click() + for _ in range(2): + self._client.right_click() self._client.both_click() - self._verbose_eip712 = not self._verbose_eip712 + for enum in self._settings.keys(): + if self._client.firmware.device in self._settings[enum].devices: + if enum in new_values.keys(): + if new_values[enum] != self._settings[enum].value: + self._client.both_click() + self._settings[enum].value = new_values[enum] + self._client.right_click() + self._client.both_click() diff --git a/tests/ragger/test_eip712.py b/tests/ragger/test_eip712.py index e218f5c..1645013 100644 --- a/tests/ragger/test_eip712.py +++ b/tests/ragger/test_eip712.py @@ -2,7 +2,7 @@ import pytest import os import fnmatch from typing import List -from ethereum_client import EthereumClient +from ethereum_client import EthereumClient, SettingType from eip712 import InputData from pathlib import Path from configparser import ConfigParser @@ -44,9 +44,9 @@ def test_eip712_legacy(app_client: EthereumClient): assert s == bytes.fromhex("52d8ba9153de9255da220ffd36762c0b027701a3b5110f0a765f94b16a9dfb55") -def test_eip712_new(app_client: EthereumClient, input_file: Path, verbose): - if app_client._client.firmware.device != "nanos": # not supported - print("=====> %s" % (input_file)) +def test_eip712_new(app_client: EthereumClient, input_file: Path, verbose: bool, filtering: bool): + print("=====> %s" % (input_file)) + if app_client._client.firmware.device != "nanos": test_path = "%s/%s" % (input_file.parent, "-".join(input_file.stem.split("-")[:-1])) conf_file = "%s.ini" % (test_path) @@ -60,10 +60,15 @@ def test_eip712_new(app_client: EthereumClient, input_file: Path, verbose): assert "s" in config["signature"] if verbose: - app_client.setting_toggle_verbose_eip712() + app_client.settings_set({ + SettingType.VERBOSE_EIP712: True + }) + InputData.process_file(app_client, input_file, False) v, r, s = app_client.eip712_sign_new(bip32) assert v == bytes.fromhex(config["signature"]["v"]) assert r == bytes.fromhex(config["signature"]["r"]) assert s == bytes.fromhex(config["signature"]["s"]) + else: + print("Not supported by LNS")