1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-12-04 21:48:17 +00:00

test: update upgrade tests

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

View File

@ -20,7 +20,9 @@ from typing import TYPE_CHECKING, List, Optional
import pytest
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 (
ApplySettings,
BackupAvailability,
@ -57,15 +59,19 @@ STRENGTH = 128
@for_all()
def test_upgrade_load(gen: str, tag: str) -> None:
def asserts(client: "Client"):
client.refresh_features()
assert not client.features.pin_protection
assert not client.features.passphrase_protection
assert client.features.initialized
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:
debuglink.load_device_by_mnemonic(
emu.client,
emu.client.get_management_session(),
mnemonic=MNEMONIC,
pin="",
passphrase_protection=False,
@ -89,12 +95,14 @@ def test_upgrade_load_pin(gen: str, tag: str) -> None:
assert not client.features.passphrase_protection
assert client.features.initialized
assert client.features.label == LABEL
client.use_pin_sequence([PIN])
assert btc.get_address(client, "Bitcoin", PATH) == ADDRESS
session = Session(client.get_session())
with client, session:
client.use_pin_sequence([PIN])
assert btc.get_address(session, "Bitcoin", PATH) == ADDRESS
with EmulatorWrapper(gen, tag) as emu:
debuglink.load_device_by_mnemonic(
emu.client,
Session(emu.client.get_management_session()),
mnemonic=MNEMONIC,
pin=PIN,
passphrase_protection=False,
@ -130,11 +138,11 @@ def test_storage_upgrade_progressive(gen: str, tags: List[str]):
assert client.features.initialized
assert client.features.label == LABEL
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:
debuglink.load_device_by_mnemonic(
emu.client,
emu.client.get_management_session(),
mnemonic=MNEMONIC,
pin=PIN,
passphrase_protection=False,
@ -164,11 +172,11 @@ def test_upgrade_wipe_code(gen: str, tag: str):
assert client.features.initialized
assert client.features.label == LABEL
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:
debuglink.load_device_by_mnemonic(
emu.client,
emu.client.get_management_session(),
mnemonic=MNEMONIC,
pin=PIN,
passphrase_protection=False,
@ -177,7 +185,9 @@ def test_upgrade_wipe_code(gen: str, tag: str):
# Set 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
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.
emu.client.use_pin_sequence([PIN, WIPE_CODE, WIPE_CODE])
session = Session(emu.client.get_management_session())
session.refresh_features()
with pytest.raises(
exceptions.TrezorFailure,
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")
@ -209,7 +221,7 @@ def test_upgrade_reset(gen: str, tag: str):
with EmulatorWrapper(gen, tag) as emu:
device.reset(
emu.client,
emu.client.get_session(),
strength=STRENGTH,
passphrase_protection=False,
pin_protection=False,
@ -217,13 +229,13 @@ def test_upgrade_reset(gen: str, tag: str):
)
device_id = emu.client.features.device_id
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()
with EmulatorWrapper(gen, storage=storage) as emu:
assert device_id == emu.client.features.device_id
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()
@ -239,7 +251,7 @@ def test_upgrade_reset_skip_backup(gen: str, tag: str):
with EmulatorWrapper(gen, tag) as emu:
device.reset(
emu.client,
emu.client.get_session(),
strength=STRENGTH,
passphrase_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
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()
with EmulatorWrapper(gen, storage=storage) as emu:
assert device_id == emu.client.features.device_id
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))
@ -270,7 +282,7 @@ def test_upgrade_reset_no_backup(gen: str, tag: str):
with EmulatorWrapper(gen, tag) as emu:
device.reset(
emu.client,
emu.client.get_session(),
strength=STRENGTH,
passphrase_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
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()
with EmulatorWrapper(gen, storage=storage) as emu:
assert device_id == emu.client.features.device_id
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.
@ -298,7 +310,7 @@ def test_upgrade_shamir_recovery(gen: str, tag: Optional[str]):
emu.client.watch_layout(True)
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.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))
def test_upgrade_shamir_backup(gen: str, tag: Optional[str]):
with EmulatorWrapper(gen, tag) as emu:
session = Session(emu.client.get_management_session())
# Generate a new encrypted master secret and record it.
device.reset(
emu.client,
session,
pin_protection=False,
skip_backup=True,
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
# 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)
# Get a passphrase-less and a passphrased address.
address = btc.get_address(emu.client, "Bitcoin", PATH)
emu.client.init_device(new_session=True)
emu.client.use_passphrase("TREZOR")
address_passphrase = btc.get_address(emu.client, "Bitcoin", PATH)
address = btc.get_address(session, "Bitcoin", PATH)
if session.protocol_version == ProtocolVersion.PROTOCOL_V1:
session.call(messages.Initialize(new_session=True))
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
storage = emu.get_storage()
@ -375,7 +390,7 @@ def test_upgrade_shamir_backup(gen: str, tag: Optional[str]):
with emu.client:
IF = InputFlowSlip39BasicBackup(emu.client, False)
emu.client.set_input_flow(IF.get())
device.backup(emu.client)
device.backup(emu.client.get_session())
assert (
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
# Check that addresses are the same after firmware upgrade and backup.
assert btc.get_address(emu.client, "Bitcoin", PATH) == address
emu.client.init_device(new_session=True)
emu.client.use_passphrase("TREZOR")
assert btc.get_address(emu.client, "Bitcoin", PATH) == address_passphrase
assert btc.get_address(emu.client.get_session(), "Bitcoin", PATH) == address
assert (
btc.get_address(
emu.client.get_session(passphrase="TREZOR"), "Bitcoin", PATH
)
== address_passphrase
)
@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."""
with EmulatorWrapper(gen, tag) as emu:
debuglink.load_device_by_mnemonic(
emu.client,
emu.client.get_management_session(),
mnemonic=MNEMONIC,
pin="",
passphrase_protection=False,
label=LABEL,
)
success = fido.set_counter(emu.client, 10)
session = emu.client.get_management_session()
success = fido.set_counter(session, 10)
assert "U2F counter set" in success
counter = fido.get_next_counter(emu.client)
counter = fido.get_next_counter(session)
assert counter == 11
storage = emu.get_storage()
with EmulatorWrapper(gen, storage=storage) as emu:
counter = fido.get_next_counter(emu.client)
counter = fido.get_next_counter(session)
assert counter == 12

View File

@ -20,6 +20,8 @@ import pytest
from trezorlib import btc, device, mapping, messages, models, protobuf
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 ..emulators import EmulatorWrapper
@ -47,11 +49,12 @@ def emulator(gen: str, tag: str) -> Iterator[Emulator]:
with EmulatorWrapper(gen, tag) as emu:
# set up a passphrase-protected device
device.reset(
emu.client,
emu.client.get_management_session(),
pin_protection=False,
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)
)
assert isinstance(resp, messages.Success)
@ -87,11 +90,10 @@ def test_passphrase_works(emulator: Emulator):
messages.ButtonRequest,
messages.Address,
]
with emulator.client:
emulator.client.use_passphrase("TREZOR")
emulator.client.set_expected_responses(expected_responses)
btc.get_address(emulator.client, "Testnet", parse_path("44h/1h/0h/0/0"))
emu_session = emulator.client.get_session(passphrase="TREZOR")
with Session(emu_session) as session:
session.set_expected_responses(expected_responses)
btc.get_address(session, "Testnet", parse_path("44h/1h/0h/0/0"))
@for_all(
@ -131,13 +133,18 @@ def test_init_device(emulator: Emulator):
messages.Address,
]
with emulator.client:
emulator.client.use_passphrase("TREZOR")
emulator.client.set_expected_responses(expected_responses)
emu_session = emulator.client.get_session(passphrase="TREZOR")
with Session(emu_session) as session:
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
session_id = emulator.client.session_id
emulator.client.init_device()
btc.get_address(emulator.client, "Testnet", parse_path("44h/1h/0h/0/0"))
assert session_id == emulator.client.session_id
session_id = session.id
if session.protocol_version == ProtocolVersion.PROTOCOL_V1:
session.call(messages.Initialize(session_id=session_id))
btc.get_address(
session,
"Testnet",
parse_path("44h/1h/0h/0/0"),
)
assert session_id == session.id