1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-12-05 22:18:16 +00:00
trezor-firmware/tests/device_tests/ethereum/test_definitions_bad.py

132 lines
4.6 KiB
Python
Raw Normal View History

from __future__ import annotations
from hashlib import sha256
import pytest
from trezorlib import ethereum
2024-11-15 16:31:22 +00:00
from trezorlib.debuglink import SessionDebugWrapper as Session
from trezorlib.exceptions import TrezorFailure
from trezorlib.messages import EthereumDefinitionType
from trezorlib.tools import parse_path
from .common import make_defs, make_network, make_payload, make_token, sign_payload
from .test_definitions import DEFAULT_ERC20_PARAMS, ERC20_FAKE_ADDRESS
pytestmark = [pytest.mark.altcoin, pytest.mark.ethereum]
2024-11-15 16:31:22 +00:00
def fails(session: Session, network: bytes, match: str) -> None:
with pytest.raises(TrezorFailure, match=match):
ethereum.get_address(
2024-11-15 16:31:22 +00:00
session,
parse_path("m/44h/666666h/0h"),
show_display=False,
encoded_network=network,
)
2024-11-15 16:31:22 +00:00
def test_short_message(session: Session) -> None:
fails(session, b"\x00", "Invalid Ethereum definition")
2024-11-15 16:31:22 +00:00
def test_mangled_signature(session: Session) -> None:
payload = make_payload()
proof, signature = sign_payload(payload, [])
bad_signature = signature[:-1] + b"\xff"
2024-11-15 16:31:22 +00:00
fails(session, payload + proof + bad_signature, "Invalid definition signature")
2024-11-15 16:31:22 +00:00
def test_not_enough_signatures(session: Session) -> None:
payload = make_payload()
proof, signature = sign_payload(payload, [], threshold=1)
2024-11-15 16:31:22 +00:00
fails(session, payload + proof + signature, "Invalid definition signature")
2024-11-15 16:31:22 +00:00
def test_missing_signature(session: Session) -> None:
payload = make_payload()
proof, _ = sign_payload(payload, [])
2024-11-15 16:31:22 +00:00
fails(session, payload + proof, "Invalid Ethereum definition")
2024-11-15 16:31:22 +00:00
def test_mangled_payload(session: Session) -> None:
payload = make_payload()
proof, signature = sign_payload(payload, [])
bad_payload = payload[:-1] + b"\xff"
2024-11-15 16:31:22 +00:00
fails(session, bad_payload + proof + signature, "Invalid definition signature")
2024-11-15 16:31:22 +00:00
def test_proof_length_mismatch(session: Session) -> None:
payload = make_payload()
_, signature = sign_payload(payload, [])
bad_proof = b"\x01"
2024-11-15 16:31:22 +00:00
fails(session, payload + bad_proof + signature, "Invalid Ethereum definition")
2024-11-15 16:31:22 +00:00
def test_bad_proof(session: Session) -> None:
payload = make_payload()
proof, signature = sign_payload(payload, [sha256(b"x").digest()])
bad_proof = proof[:-1] + b"\xff"
2024-11-15 16:31:22 +00:00
fails(session, payload + bad_proof + signature, "Invalid definition signature")
2024-11-15 16:31:22 +00:00
def test_trimmed_proof(session: Session) -> None:
payload = make_payload()
proof, signature = sign_payload(payload, [])
bad_proof = proof[:-1]
2024-11-15 16:31:22 +00:00
fails(session, payload + bad_proof + signature, "Invalid Ethereum definition")
2024-11-15 16:31:22 +00:00
def test_bad_prefix(session: Session) -> None:
payload = make_payload()
payload = b"trzd2" + payload[5:]
proof, signature = sign_payload(payload, [])
2024-11-15 16:31:22 +00:00
fails(session, payload + proof + signature, "Invalid Ethereum definition")
2024-11-15 16:31:22 +00:00
def test_bad_type(session: Session) -> None:
# assuming we expect a network definition
payload = make_payload(data_type=EthereumDefinitionType.TOKEN, message=make_token())
proof, signature = sign_payload(payload, [])
2024-11-15 16:31:22 +00:00
fails(session, payload + proof + signature, "Definition type mismatch")
2024-11-15 16:31:22 +00:00
def test_outdated(session: Session) -> None:
payload = make_payload(timestamp=0)
proof, signature = sign_payload(payload, [])
2024-11-15 16:31:22 +00:00
fails(session, payload + proof + signature, "Definition is outdated")
2024-11-15 16:31:22 +00:00
def test_malformed_protobuf(session: Session) -> None:
payload = make_payload(message=b"\x00")
proof, signature = sign_payload(payload, [])
2024-11-15 16:31:22 +00:00
fails(session, payload + proof + signature, "Invalid Ethereum definition")
2024-11-15 16:31:22 +00:00
def test_protobuf_mismatch(session: Session) -> None:
payload = make_payload(
data_type=EthereumDefinitionType.NETWORK, message=make_token()
)
proof, signature = sign_payload(payload, [])
2024-11-15 16:31:22 +00:00
fails(session, payload + proof + signature, "Invalid Ethereum definition")
payload = make_payload(
data_type=EthereumDefinitionType.TOKEN, message=make_network()
)
proof, signature = sign_payload(payload, [])
# have to do this manually to invoke a method that eats token definitions
with pytest.raises(TrezorFailure, match="Invalid Ethereum definition"):
params = DEFAULT_ERC20_PARAMS.copy()
params.update(to=ERC20_FAKE_ADDRESS)
ethereum.sign_tx(
2024-11-15 16:31:22 +00:00
session,
**params,
definitions=make_defs(None, payload + proof + signature),
)
2024-11-15 16:31:22 +00:00
def test_trailing_garbage(session: Session) -> None:
payload = make_payload()
proof, signature = sign_payload(payload, [])
2024-11-15 16:31:22 +00:00
fails(session, payload + proof + signature + b"\x00", "Invalid Ethereum definition")