1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2025-02-23 13:02:03 +00:00

fix(tests): port legacy device tests to trezorlib with sessions

[no changelog]
This commit is contained in:
Martin Milata 2025-02-19 19:49:41 +01:00
parent 37cec1e885
commit a8725504a0
8 changed files with 63 additions and 70 deletions

View File

@ -59,13 +59,13 @@ def test_show_t1(
): ):
def input_flow_t1(): def input_flow_t1():
yield yield
session.debug.press_no() session.client.debug.press_no()
yield yield
session.debug.press_yes() session.client.debug.press_yes()
with session: with session.client as client:
# This is the only place where even T1 is using input flow # This is the only place where even T1 is using input flow
session.set_input_flow(input_flow_t1) client.set_input_flow(input_flow_t1)
assert ( assert (
btc.get_address( btc.get_address(
session, session,

View File

@ -160,13 +160,13 @@ def test_get_public_node_show_legacy(
with client: with client:
# test XPUB display flow (without showing QR code) # test XPUB display flow (without showing QR code)
res = btc.get_public_node(client, path, coin_name=coin_name, show_display=True) res = btc.get_public_node(session, path, coin_name=coin_name, show_display=True)
assert res.xpub == xpub assert res.xpub == xpub
assert bip32.serialize(res.node, xpub_magic) == xpub assert bip32.serialize(res.node, xpub_magic) == xpub
# test XPUB QR code display using the input flow above # test XPUB QR code display using the input flow above
client.set_input_flow(input_flow) client.set_input_flow(input_flow)
res = btc.get_public_node(client, path, coin_name=coin_name, show_display=True) res = btc.get_public_node(session, path, coin_name=coin_name, show_display=True)
assert res.xpub == xpub assert res.xpub == xpub
assert bip32.serialize(res.node, xpub_magic) == xpub assert bip32.serialize(res.node, xpub_magic) == xpub

View File

@ -77,9 +77,9 @@ def test_pin_passphrase(session: Session):
assert fakes == 12 assert fakes == 12
assert mnemonic == [None] * 12 assert mnemonic == [None] * 12
raise Exception("TEST IS USING INIT MESSAGE - TODO CHANGE")
# Mnemonic is the same # Mnemonic is the same
session.init_device() session.init_session()
session.client.refresh_features()
assert debug.state().mnemonic_secret == MNEMONIC12.encode() assert debug.state().mnemonic_secret == MNEMONIC12.encode()
assert session.features.pin_protection is True assert session.features.pin_protection is True
@ -131,10 +131,9 @@ def test_nopin_nopassphrase(session: Session):
assert fakes == 12 assert fakes == 12
assert mnemonic == [None] * 12 assert mnemonic == [None] * 12
raise Exception("TEST IS USING INIT MESSAGE - TODO CHANGE")
# Mnemonic is the same # Mnemonic is the same
# session.init_device() session.init_session()
session.client.refresh_features()
assert debug.state().mnemonic_secret == MNEMONIC12.encode() assert debug.state().mnemonic_secret == MNEMONIC12.encode()
assert session.features.pin_protection is False assert session.features.pin_protection is False

View File

@ -54,8 +54,8 @@ def reset_device(session: Session, strength: int):
mnemonic = [] mnemonic = []
for _ in range(strength // 32 * 3): for _ in range(strength // 32 * 3):
assert isinstance(ret, messages.ButtonRequest) assert isinstance(ret, messages.ButtonRequest)
mnemonic.append(session.debug.read_reset_word()) mnemonic.append(session.client.debug.read_reset_word())
session.debug.press_yes() session.client.debug.press_yes()
session.call_raw(messages.ButtonAck()) session.call_raw(messages.ButtonAck())
mnemonic = " ".join(mnemonic) mnemonic = " ".join(mnemonic)
@ -66,7 +66,7 @@ def reset_device(session: Session, strength: int):
mnemonic = [] mnemonic = []
for _ in range(strength // 32 * 3): for _ in range(strength // 32 * 3):
assert isinstance(ret, messages.ButtonRequest) assert isinstance(ret, messages.ButtonRequest)
mnemonic.append(session.debug.read_reset_word()) mnemonic.append(session.client.debug.read_reset_word())
debug.press_yes() debug.press_yes()
resp = session.call_raw(messages.ButtonAck()) resp = session.call_raw(messages.ButtonAck())
@ -78,7 +78,7 @@ def reset_device(session: Session, strength: int):
assert mnemonic == expected_mnemonic assert mnemonic == expected_mnemonic
# Check if device is properly initialized # Check if device is properly initialized
resp = session.call_raw(messages.GetFeatures()) resp = session.call_raw(messages.Initialize())
assert resp.initialized is True assert resp.initialized is True
assert resp.backup_availability == messages.BackupAvailability.NotAvailable assert resp.backup_availability == messages.BackupAvailability.NotAvailable
assert resp.pin_protection is False assert resp.pin_protection is False
@ -170,7 +170,7 @@ def test_reset_device_256_pin(session: Session):
assert mnemonic == expected_mnemonic assert mnemonic == expected_mnemonic
# Check if device is properly initialized # Check if device is properly initialized
resp = session.call_raw(messages.GetFeatures()) resp = session.call_raw(messages.Initialize())
assert resp.initialized is True assert resp.initialized is True
assert resp.backup_availability == messages.BackupAvailability.NotAvailable assert resp.backup_availability == messages.BackupAvailability.NotAvailable
assert resp.pin_protection is True assert resp.pin_protection is True

View File

@ -19,7 +19,6 @@ import pytest
from trezorlib import device, exceptions, messages from trezorlib import device, exceptions, messages
from trezorlib.client import MAX_PIN_LENGTH from trezorlib.client import MAX_PIN_LENGTH
from trezorlib.debuglink import SessionDebugWrapper as Session from trezorlib.debuglink import SessionDebugWrapper as Session
from trezorlib.debuglink import TrezorClientDebugLink as Client
from trezorlib.tools import parse_path from trezorlib.tools import parse_path
PinType = messages.PinMatrixRequestType PinType = messages.PinMatrixRequestType
@ -33,10 +32,10 @@ WIPE_CODE_TOO_LONG = WIPE_CODE_MAX + "1"
pytestmark = pytest.mark.models("legacy") pytestmark = pytest.mark.models("legacy")
def _set_wipe_code(client: Client, pin, wipe_code): def _set_wipe_code(session: Session, pin, wipe_code):
# Set/change wipe code. # Set/change wipe code.
with client: with session.client as client, session:
if client.features.pin_protection: if session.features.pin_protection:
pins = [pin, wipe_code, wipe_code] pins = [pin, wipe_code, wipe_code]
pin_matrices = [ pin_matrices = [
messages.PinMatrixRequest(type=PinType.Current), messages.PinMatrixRequest(type=PinType.Current),
@ -51,63 +50,58 @@ def _set_wipe_code(client: Client, pin, wipe_code):
] ]
client.use_pin_sequence(pins) client.use_pin_sequence(pins)
client.set_expected_responses( session.set_expected_responses(
[messages.ButtonRequest()] [messages.ButtonRequest()] + pin_matrices + [messages.Success]
+ pin_matrices
+ [messages.Success, messages.Features]
) )
device.change_wipe_code(client) device.change_wipe_code(session)
def _change_pin(client: Client, old_pin, new_pin): def _change_pin(session: Session, old_pin, new_pin):
assert client.features.pin_protection is True assert session.features.pin_protection is True
with client: with session.client as client:
client.use_pin_sequence([old_pin, new_pin, new_pin]) client.use_pin_sequence([old_pin, new_pin, new_pin])
try: try:
return device.change_pin(client) return device.change_pin(session)
except exceptions.TrezorFailure as f: except exceptions.TrezorFailure as f:
return f.failure return f.failure
def _check_wipe_code(client: Client, pin, wipe_code): def _check_wipe_code(session: Session, pin, wipe_code):
"""Check that wipe code is set by changing the PIN to it.""" """Check that wipe code is set by changing the PIN to it."""
f = _change_pin(client, pin, wipe_code) f = _change_pin(session, pin, wipe_code)
assert isinstance(f, messages.Failure) assert isinstance(f, messages.Failure)
@pytest.mark.setup_client(pin=PIN4) @pytest.mark.setup_client(pin=PIN4)
def test_set_remove_wipe_code(client: Client): def test_set_remove_wipe_code(session: Session):
# Check that wipe code protection status is not revealed in locked state. # Check that wipe code protection status is not revealed in locked state.
assert client.features.wipe_code_protection is None assert session.features.wipe_code_protection is None
# Test set wipe code. # Test set wipe code.
_set_wipe_code(client, PIN4, WIPE_CODE_MAX) _set_wipe_code(session, PIN4, WIPE_CODE_MAX)
# Check that there's wipe code protection now. # Check that there's wipe code protection now.
client.init_device() assert session.features.wipe_code_protection is True
assert client.features.wipe_code_protection is True
# Check that the wipe code is correct. # Check that the wipe code is correct.
_check_wipe_code(client, PIN4, WIPE_CODE_MAX) _check_wipe_code(session, PIN4, WIPE_CODE_MAX)
# Test change wipe code. # Test change wipe code.
_set_wipe_code(client, PIN4, WIPE_CODE6) _set_wipe_code(session, PIN4, WIPE_CODE6)
# Check that there's still wipe code protection now. # Check that there's still wipe code protection now.
client.init_device() assert session.features.wipe_code_protection is True
assert client.features.wipe_code_protection is True
# Check that the wipe code is correct. # Check that the wipe code is correct.
_check_wipe_code(client, PIN4, WIPE_CODE6) _check_wipe_code(session, PIN4, WIPE_CODE6)
# Test remove wipe code. # Test remove wipe code.
with client: with session.client as client:
client.use_pin_sequence([PIN4]) client.use_pin_sequence([PIN4])
device.change_wipe_code(client, remove=True) device.change_wipe_code(session, remove=True)
# Check that there's no wipe code protection now. # Check that there's no wipe code protection now.
client.init_device() assert session.features.wipe_code_protection is False
assert client.features.wipe_code_protection is False
def test_set_wipe_code_mismatch(session: Session): def test_set_wipe_code_mismatch(session: Session):
@ -136,14 +130,14 @@ def test_set_wipe_code_mismatch(session: Session):
@pytest.mark.setup_client(pin=PIN4) @pytest.mark.setup_client(pin=PIN4)
def test_set_wipe_code_to_pin(client: Client): def test_set_wipe_code_to_pin(session: Session):
# Check that wipe code protection status is not revealed in locked state. # Check that wipe code protection status is not revealed in locked state.
assert client.features.wipe_code_protection is None assert session.features.wipe_code_protection is None
# Let's try setting the wipe code to the curent PIN value. # Let's try setting the wipe code to the curent PIN value.
with client: with session.client as client, session:
client.use_pin_sequence([PIN4, PIN4]) client.use_pin_sequence([PIN4, PIN4])
client.set_expected_responses( session.set_expected_responses(
[ [
messages.ButtonRequest(), messages.ButtonRequest(),
messages.PinMatrixRequest(type=PinType.Current), messages.PinMatrixRequest(type=PinType.Current),
@ -152,21 +146,22 @@ def test_set_wipe_code_to_pin(client: Client):
] ]
) )
with pytest.raises(exceptions.TrezorFailure): with pytest.raises(exceptions.TrezorFailure):
device.change_wipe_code(client) device.change_wipe_code(session)
# Check that there is no wipe code protection. # Check that there is no wipe code protection.
client.init_device() client.refresh_features()
assert client.features.wipe_code_protection is False assert client.features.wipe_code_protection is False
def test_set_pin_to_wipe_code(client: Client): def test_set_pin_to_wipe_code(session: Session):
# Set wipe code. # Set wipe code.
_set_wipe_code(client, None, WIPE_CODE4) session.refresh_features()
_set_wipe_code(session, None, WIPE_CODE4)
# Try to set the PIN to the current wipe code value. # Try to set the PIN to the current wipe code value.
with client: with session.client as client, session:
client.use_pin_sequence([WIPE_CODE4, WIPE_CODE4]) client.use_pin_sequence([WIPE_CODE4, WIPE_CODE4])
client.set_expected_responses( session.set_expected_responses(
[ [
messages.ButtonRequest(), messages.ButtonRequest(),
messages.PinMatrixRequest(type=PinType.NewFirst), messages.PinMatrixRequest(type=PinType.NewFirst),
@ -175,17 +170,18 @@ def test_set_pin_to_wipe_code(client: Client):
] ]
) )
with pytest.raises(exceptions.TrezorFailure): with pytest.raises(exceptions.TrezorFailure):
device.change_pin(client) device.change_pin(session)
# Check that there is no PIN protection. # Check that there is no PIN protection.
client.init_device() assert session.features.pin_protection is False
assert client.features.pin_protection is False resp = session.call_raw(
resp = client.call_raw(messages.GetAddress(address_n=parse_path("m/44'/0'/0'/0/0"))) messages.GetAddress(address_n=parse_path("m/44'/0'/0'/0/0"))
)
assert isinstance(resp, messages.Address) assert isinstance(resp, messages.Address)
@pytest.mark.parametrize("invalid_wipe_code", ("1204", "", WIPE_CODE_TOO_LONG)) @pytest.mark.parametrize("invalid_wipe_code", ("1204", "", WIPE_CODE_TOO_LONG))
def test_set_wipe_code_invalid(session: Session, invalid_wipe_code): def test_set_wipe_code_invalid(session: Session, invalid_wipe_code: str):
# Let's set the wipe code # Let's set the wipe code
ret = session.call_raw(messages.ChangeWipeCode()) ret = session.call_raw(messages.ChangeWipeCode())
assert isinstance(ret, messages.ButtonRequest) assert isinstance(ret, messages.ButtonRequest)
@ -203,6 +199,6 @@ def test_set_wipe_code_invalid(session: Session, invalid_wipe_code):
assert isinstance(ret, messages.Failure) assert isinstance(ret, messages.Failure)
# Check that there's still no wipe code protection. # Check that there's still no wipe code protection.
session.refresh_features() session.init_session() # why no response on GetFeatures?
session.ensure_unlocked() session.ensure_unlocked()
assert session.features.wipe_code_protection is False assert session.features.wipe_code_protection is False

View File

@ -61,7 +61,6 @@ def test_set_pin(session: Session):
messages.PinMatrixRequest, messages.PinMatrixRequest,
messages.PinMatrixRequest, messages.PinMatrixRequest,
messages.Success, messages.Success,
messages.Features,
] ]
) )
device.change_pin(session) device.change_pin(session)
@ -88,7 +87,6 @@ def test_change_pin(session: Session):
messages.PinMatrixRequest, messages.PinMatrixRequest,
messages.PinMatrixRequest, messages.PinMatrixRequest,
messages.Success, messages.Success,
messages.Features,
] ]
) )
device.change_pin(session) device.change_pin(session)
@ -113,7 +111,6 @@ def test_remove_pin(session: Session):
messages.ButtonRequest(code=messages.ButtonRequestType.ProtectCall), messages.ButtonRequest(code=messages.ButtonRequestType.ProtectCall),
messages.PinMatrixRequest, messages.PinMatrixRequest,
messages.Success, messages.Success,
messages.Features,
] ]
) )
device.change_pin(session, remove=True) device.change_pin(session, remove=True)
@ -199,7 +196,7 @@ def test_set_invalid(session: Session, invalid_pin):
# Check that there's still no PIN protection now # Check that there's still no PIN protection now
# TODO change session.init_device() # TODO change session.init_device()
session.refresh_features() session.init_session()
assert session.features.pin_protection is False assert session.features.pin_protection is False
_check_no_pin(session) _check_no_pin(session)

View File

@ -133,7 +133,6 @@ def test_change_pin_t1(session: Session):
_pin_request(session), _pin_request(session),
_pin_request(session), _pin_request(session),
messages.Success, messages.Success,
messages.Features,
] ]
) )
device.change_pin(session) device.change_pin(session)

View File

@ -324,16 +324,18 @@ def test_passphrase_always_on_device(client: Client):
@pytest.mark.models("legacy") @pytest.mark.models("legacy")
@pytest.mark.setup_client(passphrase="") @pytest.mark.setup_client(passphrase="")
def test_passphrase_on_device_not_possible_on_t1(client: Client): def test_passphrase_on_device_not_possible_on_t1(session: Session):
# This setting makes no sense on T1. # This setting makes no sense on T1.
response = client.call_raw(messages.ApplySettings(passphrase_always_on_device=True)) response = session.call_raw(
messages.ApplySettings(passphrase_always_on_device=True)
)
assert isinstance(response, messages.Failure) assert isinstance(response, messages.Failure)
assert response.code == FailureType.DataError assert response.code == FailureType.DataError
# T1 should not accept on_device request # T1 should not accept on_device request
response = client.call_raw(XPUB_REQUEST) response = session.call_raw(XPUB_REQUEST)
assert isinstance(response, messages.PassphraseRequest) assert isinstance(response, messages.PassphraseRequest)
response = client.call_raw(messages.PassphraseAck(on_device=True)) response = session.call_raw(messages.PassphraseAck(on_device=True))
assert isinstance(response, messages.Failure) assert isinstance(response, messages.Failure)
assert response.code == FailureType.DataError assert response.code == FailureType.DataError