1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2025-01-08 06:20:56 +00:00

test: update upgrade tests

[no changelog]
This commit is contained in:
M1nd3r 2024-12-02 15:48:31 +01:00
parent 4204180bfb
commit 8bcea14c6a
2 changed files with 78 additions and 53 deletions

View File

@ -20,7 +20,9 @@ from typing import TYPE_CHECKING, List, Optional
import pytest import pytest
from shamir_mnemonic import shamir from shamir_mnemonic import shamir
from trezorlib import btc, debuglink, device, exceptions, fido, models from trezorlib import btc, debuglink, device, exceptions, fido, messages, models
from trezorlib.client import ProtocolVersion
from trezorlib.debuglink import SessionDebugWrapper as Session
from trezorlib.messages import ( from trezorlib.messages import (
ApplySettings, ApplySettings,
BackupAvailability, BackupAvailability,
@ -57,15 +59,19 @@ STRENGTH = 128
@for_all() @for_all()
def test_upgrade_load(gen: str, tag: str) -> None: def test_upgrade_load(gen: str, tag: str) -> None:
def asserts(client: "Client"): def asserts(client: "Client"):
client.refresh_features()
assert not client.features.pin_protection assert not client.features.pin_protection
assert not client.features.passphrase_protection assert not client.features.passphrase_protection
assert client.features.initialized assert client.features.initialized
assert client.features.label == LABEL assert client.features.label == LABEL
assert btc.get_address(client, "Bitcoin", PATH) == ADDRESS assert (
btc.get_address(client.get_session(passphrase=""), "Bitcoin", PATH)
== ADDRESS
)
with EmulatorWrapper(gen, tag) as emu: with EmulatorWrapper(gen, tag) as emu:
debuglink.load_device_by_mnemonic( debuglink.load_device_by_mnemonic(
emu.client, emu.client.get_management_session(),
mnemonic=MNEMONIC, mnemonic=MNEMONIC,
pin="", pin="",
passphrase_protection=False, passphrase_protection=False,
@ -89,12 +95,14 @@ def test_upgrade_load_pin(gen: str, tag: str) -> None:
assert not client.features.passphrase_protection assert not client.features.passphrase_protection
assert client.features.initialized assert client.features.initialized
assert client.features.label == LABEL assert client.features.label == LABEL
client.use_pin_sequence([PIN]) session = Session(client.get_session())
assert btc.get_address(client, "Bitcoin", PATH) == ADDRESS with client, session:
client.use_pin_sequence([PIN])
assert btc.get_address(session, "Bitcoin", PATH) == ADDRESS
with EmulatorWrapper(gen, tag) as emu: with EmulatorWrapper(gen, tag) as emu:
debuglink.load_device_by_mnemonic( debuglink.load_device_by_mnemonic(
emu.client, Session(emu.client.get_management_session()),
mnemonic=MNEMONIC, mnemonic=MNEMONIC,
pin=PIN, pin=PIN,
passphrase_protection=False, passphrase_protection=False,
@ -130,11 +138,11 @@ def test_storage_upgrade_progressive(gen: str, tags: List[str]):
assert client.features.initialized assert client.features.initialized
assert client.features.label == LABEL assert client.features.label == LABEL
client.use_pin_sequence([PIN]) client.use_pin_sequence([PIN])
assert btc.get_address(client, "Bitcoin", PATH) == ADDRESS assert btc.get_address(client.get_session(), "Bitcoin", PATH) == ADDRESS
with EmulatorWrapper(gen, tags[0]) as emu: with EmulatorWrapper(gen, tags[0]) as emu:
debuglink.load_device_by_mnemonic( debuglink.load_device_by_mnemonic(
emu.client, emu.client.get_management_session(),
mnemonic=MNEMONIC, mnemonic=MNEMONIC,
pin=PIN, pin=PIN,
passphrase_protection=False, passphrase_protection=False,
@ -164,11 +172,11 @@ def test_upgrade_wipe_code(gen: str, tag: str):
assert client.features.initialized assert client.features.initialized
assert client.features.label == LABEL assert client.features.label == LABEL
client.use_pin_sequence([PIN]) client.use_pin_sequence([PIN])
assert btc.get_address(client, "Bitcoin", PATH) == ADDRESS assert btc.get_address(client.get_session(), "Bitcoin", PATH) == ADDRESS
with EmulatorWrapper(gen, tag) as emu: with EmulatorWrapper(gen, tag) as emu:
debuglink.load_device_by_mnemonic( debuglink.load_device_by_mnemonic(
emu.client, emu.client.get_management_session(),
mnemonic=MNEMONIC, mnemonic=MNEMONIC,
pin=PIN, pin=PIN,
passphrase_protection=False, passphrase_protection=False,
@ -177,7 +185,9 @@ def test_upgrade_wipe_code(gen: str, tag: str):
# Set wipe code. # Set wipe code.
emu.client.use_pin_sequence([PIN, WIPE_CODE, WIPE_CODE]) emu.client.use_pin_sequence([PIN, WIPE_CODE, WIPE_CODE])
device.change_wipe_code(emu.client) session = Session(emu.client.get_management_session())
session.refresh_features()
device.change_wipe_code(session)
device_id = emu.client.features.device_id device_id = emu.client.features.device_id
asserts(emu.client) asserts(emu.client)
@ -189,11 +199,13 @@ def test_upgrade_wipe_code(gen: str, tag: str):
# Check that wipe code is set by changing the PIN to it. # Check that wipe code is set by changing the PIN to it.
emu.client.use_pin_sequence([PIN, WIPE_CODE, WIPE_CODE]) emu.client.use_pin_sequence([PIN, WIPE_CODE, WIPE_CODE])
session = Session(emu.client.get_management_session())
session.refresh_features()
with pytest.raises( with pytest.raises(
exceptions.TrezorFailure, exceptions.TrezorFailure,
match="The new PIN must be different from your wipe code", match="The new PIN must be different from your wipe code",
): ):
return device.change_pin(emu.client) return device.change_pin(session)
@for_all("legacy") @for_all("legacy")
@ -209,7 +221,7 @@ def test_upgrade_reset(gen: str, tag: str):
with EmulatorWrapper(gen, tag) as emu: with EmulatorWrapper(gen, tag) as emu:
device.reset( device.reset(
emu.client, emu.client.get_session(),
strength=STRENGTH, strength=STRENGTH,
passphrase_protection=False, passphrase_protection=False,
pin_protection=False, pin_protection=False,
@ -217,13 +229,13 @@ def test_upgrade_reset(gen: str, tag: str):
) )
device_id = emu.client.features.device_id device_id = emu.client.features.device_id
asserts(emu.client) asserts(emu.client)
address = btc.get_address(emu.client, "Bitcoin", PATH) address = btc.get_address(emu.client.get_session(), "Bitcoin", PATH)
storage = emu.get_storage() storage = emu.get_storage()
with EmulatorWrapper(gen, storage=storage) as emu: with EmulatorWrapper(gen, storage=storage) as emu:
assert device_id == emu.client.features.device_id assert device_id == emu.client.features.device_id
asserts(emu.client) asserts(emu.client)
assert btc.get_address(emu.client, "Bitcoin", PATH) == address assert btc.get_address(emu.client.get_session(), "Bitcoin", PATH) == address
@for_all() @for_all()
@ -239,7 +251,7 @@ def test_upgrade_reset_skip_backup(gen: str, tag: str):
with EmulatorWrapper(gen, tag) as emu: with EmulatorWrapper(gen, tag) as emu:
device.reset( device.reset(
emu.client, emu.client.get_session(),
strength=STRENGTH, strength=STRENGTH,
passphrase_protection=False, passphrase_protection=False,
pin_protection=False, pin_protection=False,
@ -248,13 +260,13 @@ def test_upgrade_reset_skip_backup(gen: str, tag: str):
) )
device_id = emu.client.features.device_id device_id = emu.client.features.device_id
asserts(emu.client) asserts(emu.client)
address = btc.get_address(emu.client, "Bitcoin", PATH) address = btc.get_address(emu.client.get_session(), "Bitcoin", PATH)
storage = emu.get_storage() storage = emu.get_storage()
with EmulatorWrapper(gen, storage=storage) as emu: with EmulatorWrapper(gen, storage=storage) as emu:
assert device_id == emu.client.features.device_id assert device_id == emu.client.features.device_id
asserts(emu.client) asserts(emu.client)
assert btc.get_address(emu.client, "Bitcoin", PATH) == address assert btc.get_address(emu.client.get_session(), "Bitcoin", PATH) == address
@for_all(legacy_minimum_version=(1, 7, 2)) @for_all(legacy_minimum_version=(1, 7, 2))
@ -270,7 +282,7 @@ def test_upgrade_reset_no_backup(gen: str, tag: str):
with EmulatorWrapper(gen, tag) as emu: with EmulatorWrapper(gen, tag) as emu:
device.reset( device.reset(
emu.client, emu.client.get_session(),
strength=STRENGTH, strength=STRENGTH,
passphrase_protection=False, passphrase_protection=False,
pin_protection=False, pin_protection=False,
@ -279,13 +291,13 @@ def test_upgrade_reset_no_backup(gen: str, tag: str):
) )
device_id = emu.client.features.device_id device_id = emu.client.features.device_id
asserts(emu.client) asserts(emu.client)
address = btc.get_address(emu.client, "Bitcoin", PATH) address = btc.get_address(emu.client.get_session(), "Bitcoin", PATH)
storage = emu.get_storage() storage = emu.get_storage()
with EmulatorWrapper(gen, storage=storage) as emu: with EmulatorWrapper(gen, storage=storage) as emu:
assert device_id == emu.client.features.device_id assert device_id == emu.client.features.device_id
asserts(emu.client) asserts(emu.client)
assert btc.get_address(emu.client, "Bitcoin", PATH) == address assert btc.get_address(emu.client.get_session(), "Bitcoin", PATH) == address
# Although Shamir was introduced in 2.1.2 already, the debug instrumentation was not present until 2.1.9. # Although Shamir was introduced in 2.1.2 already, the debug instrumentation was not present until 2.1.9.
@ -298,7 +310,7 @@ def test_upgrade_shamir_recovery(gen: str, tag: Optional[str]):
emu.client.watch_layout(True) emu.client.watch_layout(True)
debug = device_handler.debuglink() debug = device_handler.debuglink()
device_handler.run(device.recover, pin_protection=False) device_handler.run_with_session(device.recover, pin_protection=False)
recovery_old.confirm_recovery(debug) recovery_old.confirm_recovery(debug)
recovery_old.select_number_of_words(debug) recovery_old.select_number_of_words(debug)
@ -343,9 +355,10 @@ def test_upgrade_shamir_recovery(gen: str, tag: Optional[str]):
@for_all("core", core_minimum_version=(2, 1, 9)) @for_all("core", core_minimum_version=(2, 1, 9))
def test_upgrade_shamir_backup(gen: str, tag: Optional[str]): def test_upgrade_shamir_backup(gen: str, tag: Optional[str]):
with EmulatorWrapper(gen, tag) as emu: with EmulatorWrapper(gen, tag) as emu:
session = Session(emu.client.get_management_session())
# Generate a new encrypted master secret and record it. # Generate a new encrypted master secret and record it.
device.reset( device.reset(
emu.client, session,
pin_protection=False, pin_protection=False,
skip_backup=True, skip_backup=True,
backup_type=BackupType.Slip39_Basic, backup_type=BackupType.Slip39_Basic,
@ -355,14 +368,16 @@ def test_upgrade_shamir_backup(gen: str, tag: Optional[str]):
mnemonic_secret = emu.client.debug.state().mnemonic_secret mnemonic_secret = emu.client.debug.state().mnemonic_secret
# Set passphrase_source = HOST. # Set passphrase_source = HOST.
resp = emu.client.call(ApplySettings(_passphrase_source=2, use_passphrase=True)) session = Session(emu.client.get_session())
resp = session.call(ApplySettings(_passphrase_source=2, use_passphrase=True))
assert isinstance(resp, Success) assert isinstance(resp, Success)
# Get a passphrase-less and a passphrased address. # Get a passphrase-less and a passphrased address.
address = btc.get_address(emu.client, "Bitcoin", PATH) address = btc.get_address(session, "Bitcoin", PATH)
emu.client.init_device(new_session=True) if session.protocol_version == ProtocolVersion.PROTOCOL_V1:
emu.client.use_passphrase("TREZOR") session.call(messages.Initialize(new_session=True))
address_passphrase = btc.get_address(emu.client, "Bitcoin", PATH) new_session = emu.client.get_session(passphrase="TREZOR")
address_passphrase = btc.get_address(new_session, "Bitcoin", PATH)
assert emu.client.features.backup_availability == BackupAvailability.Required assert emu.client.features.backup_availability == BackupAvailability.Required
storage = emu.get_storage() storage = emu.get_storage()
@ -375,7 +390,7 @@ def test_upgrade_shamir_backup(gen: str, tag: Optional[str]):
with emu.client: with emu.client:
IF = InputFlowSlip39BasicBackup(emu.client, False) IF = InputFlowSlip39BasicBackup(emu.client, False)
emu.client.set_input_flow(IF.get()) emu.client.set_input_flow(IF.get())
device.backup(emu.client) device.backup(emu.client.get_session())
assert ( assert (
emu.client.features.backup_availability == BackupAvailability.NotAvailable emu.client.features.backup_availability == BackupAvailability.NotAvailable
) )
@ -396,10 +411,13 @@ def test_upgrade_shamir_backup(gen: str, tag: Optional[str]):
assert ems.ciphertext == mnemonic_secret assert ems.ciphertext == mnemonic_secret
# Check that addresses are the same after firmware upgrade and backup. # Check that addresses are the same after firmware upgrade and backup.
assert btc.get_address(emu.client, "Bitcoin", PATH) == address assert btc.get_address(emu.client.get_session(), "Bitcoin", PATH) == address
emu.client.init_device(new_session=True) assert (
emu.client.use_passphrase("TREZOR") btc.get_address(
assert btc.get_address(emu.client, "Bitcoin", PATH) == address_passphrase emu.client.get_session(passphrase="TREZOR"), "Bitcoin", PATH
)
== address_passphrase
)
@for_all(legacy_minimum_version=(1, 8, 4), core_minimum_version=(2, 1, 9)) @for_all(legacy_minimum_version=(1, 8, 4), core_minimum_version=(2, 1, 9))
@ -407,22 +425,22 @@ def test_upgrade_u2f(gen: str, tag: str):
"""Check U2F counter stayed the same after an upgrade.""" """Check U2F counter stayed the same after an upgrade."""
with EmulatorWrapper(gen, tag) as emu: with EmulatorWrapper(gen, tag) as emu:
debuglink.load_device_by_mnemonic( debuglink.load_device_by_mnemonic(
emu.client, emu.client.get_management_session(),
mnemonic=MNEMONIC, mnemonic=MNEMONIC,
pin="", pin="",
passphrase_protection=False, passphrase_protection=False,
label=LABEL, label=LABEL,
) )
session = emu.client.get_management_session()
success = fido.set_counter(emu.client, 10) success = fido.set_counter(session, 10)
assert "U2F counter set" in success assert "U2F counter set" in success
counter = fido.get_next_counter(emu.client) counter = fido.get_next_counter(session)
assert counter == 11 assert counter == 11
storage = emu.get_storage() storage = emu.get_storage()
with EmulatorWrapper(gen, storage=storage) as emu: with EmulatorWrapper(gen, storage=storage) as emu:
counter = fido.get_next_counter(emu.client) counter = fido.get_next_counter(session)
assert counter == 12 assert counter == 12

View File

@ -20,6 +20,8 @@ import pytest
from trezorlib import btc, device, mapping, messages, models, protobuf from trezorlib import btc, device, mapping, messages, models, protobuf
from trezorlib._internal.emulator import Emulator from trezorlib._internal.emulator import Emulator
from trezorlib.client import ProtocolVersion
from trezorlib.debuglink import SessionDebugWrapper as Session
from trezorlib.tools import parse_path from trezorlib.tools import parse_path
from ..emulators import EmulatorWrapper from ..emulators import EmulatorWrapper
@ -47,11 +49,12 @@ def emulator(gen: str, tag: str) -> Iterator[Emulator]:
with EmulatorWrapper(gen, tag) as emu: with EmulatorWrapper(gen, tag) as emu:
# set up a passphrase-protected device # set up a passphrase-protected device
device.reset( device.reset(
emu.client, emu.client.get_management_session(),
pin_protection=False, pin_protection=False,
skip_backup=True, skip_backup=True,
) )
resp = emu.client.call( emu.client = emu.client.get_new_client()
resp = emu.client.get_management_session().call(
ApplySettingsCompat(use_passphrase=True, passphrase_source=SOURCE_HOST) ApplySettingsCompat(use_passphrase=True, passphrase_source=SOURCE_HOST)
) )
assert isinstance(resp, messages.Success) assert isinstance(resp, messages.Success)
@ -87,11 +90,10 @@ def test_passphrase_works(emulator: Emulator):
messages.ButtonRequest, messages.ButtonRequest,
messages.Address, messages.Address,
] ]
emu_session = emulator.client.get_session(passphrase="TREZOR")
with emulator.client: with Session(emu_session) as session:
emulator.client.use_passphrase("TREZOR") session.set_expected_responses(expected_responses)
emulator.client.set_expected_responses(expected_responses) btc.get_address(session, "Testnet", parse_path("44h/1h/0h/0/0"))
btc.get_address(emulator.client, "Testnet", parse_path("44h/1h/0h/0/0"))
@for_all( @for_all(
@ -131,13 +133,18 @@ def test_init_device(emulator: Emulator):
messages.Address, messages.Address,
] ]
with emulator.client: emu_session = emulator.client.get_session(passphrase="TREZOR")
emulator.client.use_passphrase("TREZOR") with Session(emu_session) as session:
emulator.client.set_expected_responses(expected_responses) session.set_expected_responses(expected_responses)
btc.get_address(emulator.client, "Testnet", parse_path("44h/1h/0h/0/0")) btc.get_address(session, "Testnet", parse_path("44h/1h/0h/0/0"))
# in TT < 2.3.0 session_id will only be available after PassphraseStateRequest # in TT < 2.3.0 session_id will only be available after PassphraseStateRequest
session_id = emulator.client.session_id session_id = session.id
emulator.client.init_device() if session.protocol_version == ProtocolVersion.PROTOCOL_V1:
btc.get_address(emulator.client, "Testnet", parse_path("44h/1h/0h/0/0")) session.call(messages.Initialize(session_id=session_id))
assert session_id == emulator.client.session_id btc.get_address(
session,
"Testnet",
parse_path("44h/1h/0h/0/0"),
)
assert session_id == session.id