1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-12-24 23:38:09 +00:00

feat(tests): adding assert_tx_matches() to common.py

This commit is contained in:
grdddj 2022-01-25 18:22:11 +01:00 committed by matejcik
parent 9cf5fd351a
commit e1d4e40785
3 changed files with 35 additions and 0 deletions

View File

@ -57,6 +57,12 @@ If you would like to interact with the device (i.e. press the buttons yourself),
INTERACT=1 pytest tests/device_tests INTERACT=1 pytest tests/device_tests
``` ```
When testing transaction signing, there is an option to check transaction hashes on-chain using Blockbook. It is chosen by setting `CHECK_ON_CHAIN=1` environment variable before running the tests.
```sh
CHECK_ON_CHAIN=1 pytest tests/device_tests
```
To run the tests quicker, spawn the emulator with disabled animations using `-a` flag. To run the tests quicker, spawn the emulator with disabled animations using `-a` flag.
```sh ```sh

View File

@ -15,9 +15,12 @@
# If not, see <https://www.gnu.org/licenses/lgpl-3.0.html>. # If not, see <https://www.gnu.org/licenses/lgpl-3.0.html>.
import json import json
import os
from decimal import Decimal
from pathlib import Path from pathlib import Path
import pytest import pytest
import requests
from trezorlib import btc, tools from trezorlib import btc, tools
from trezorlib.messages import ButtonRequestType as B from trezorlib.messages import ButtonRequestType as B
@ -222,3 +225,26 @@ def get_test_address(client):
"""Fetch a testnet address on a fixed path. Useful to make a pin/passphrase """Fetch a testnet address on a fixed path. Useful to make a pin/passphrase
protected call, or to identify the root secret (seed+passphrase)""" protected call, or to identify the root secret (seed+passphrase)"""
return btc.get_address(client, "Testnet", TEST_ADDRESS_N) return btc.get_address(client, "Testnet", TEST_ADDRESS_N)
def assert_tx_matches(serialized_tx: bytes, hash_link: str, tx_hex: str = None) -> None:
"""Verifies if a transaction is correctly formed."""
hash_str = hash_link.split("/")[-1]
assert tools.tx_hash(serialized_tx).hex() == hash_str
if tx_hex:
assert serialized_tx.hex() == tx_hex
# TODO: we could probably do better than os.environ, this was the easiest solution
# (we could create a pytest option (and use config.getoption("use-blockbook")),
# but then each test would need to have access to config via function argument)
if int(os.environ.get("CHECK_ON_CHAIN", 0)):
def get_tx_hex(hash_link: str) -> str:
tx_data = requests.get(
hash_link, headers={"User-Agent": "BTC transactions test"}
).json(parse_float=Decimal)
return tx_data["hex"]
assert serialized_tx.hex() == get_tx_hex(hash_link)

View File

@ -27,6 +27,9 @@ from . import ui_tests
from .device_handler import BackgroundDeviceHandler from .device_handler import BackgroundDeviceHandler
from .ui_tests.reporting import testreport from .ui_tests.reporting import testreport
# So that we see details of failed asserts from this module
pytest.register_assert_rewrite("tests.common")
@pytest.fixture(scope="session") @pytest.fixture(scope="session")
def _raw_client(request): def _raw_client(request):