1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2025-03-12 06:06:07 +00:00

chore(tests): add type hints to some non-device tests

This commit is contained in:
grdddj 2022-01-28 21:04:50 +01:00 committed by matejcik
parent c77e18d77c
commit 697cb6448c
5 changed files with 40 additions and 25 deletions

View File

@ -4,12 +4,12 @@ from trezorlib import debuglink, device
from trezorlib.messages import SafetyCheckLevel from trezorlib.messages import SafetyCheckLevel
from ..common import MNEMONIC12 from ..common import MNEMONIC12
from ..emulators import EmulatorWrapper from ..emulators import Emulator, EmulatorWrapper
from ..upgrade_tests import core_only from ..upgrade_tests import core_only
@pytest.fixture @pytest.fixture
def emulator(): def emulator() -> Emulator:
with EmulatorWrapper("core") as emu: with EmulatorWrapper("core") as emu:
yield emu yield emu
@ -23,7 +23,10 @@ def emulator():
(SafetyCheckLevel.PromptAlways, SafetyCheckLevel.PromptAlways), (SafetyCheckLevel.PromptAlways, SafetyCheckLevel.PromptAlways),
], ],
) )
def test_safety_checks_level_after_reboot(emulator, set_level, after_level): def test_safety_checks_level_after_reboot(
emulator: Emulator, set_level: SafetyCheckLevel, after_level: SafetyCheckLevel
):
assert emulator.client is not None
device.wipe(emulator.client) device.wipe(emulator.client)
debuglink.load_device( debuglink.load_device(
emulator.client, emulator.client,

View File

@ -17,28 +17,29 @@
import pytest import pytest
from trezorlib import device from trezorlib import device
from trezorlib.debuglink import DebugLink
from .. import buttons from .. import buttons
from ..click_tests import recovery from ..click_tests import recovery
from ..common import MNEMONIC_SLIP39_ADVANCED_20, MNEMONIC_SLIP39_BASIC_20_3of6 from ..common import MNEMONIC_SLIP39_ADVANCED_20, MNEMONIC_SLIP39_BASIC_20_3of6
from ..device_handler import BackgroundDeviceHandler from ..device_handler import BackgroundDeviceHandler
from ..emulators import EmulatorWrapper from ..emulators import Emulator, EmulatorWrapper
from ..upgrade_tests import core_only from ..upgrade_tests import core_only
@pytest.fixture @pytest.fixture
def emulator(): def emulator() -> Emulator:
with EmulatorWrapper("core") as emu: with EmulatorWrapper("core") as emu:
yield emu yield emu
def _restart(device_handler, emulator): def _restart(device_handler: BackgroundDeviceHandler, emulator: Emulator):
device_handler.restart(emulator) device_handler.restart(emulator)
return device_handler.debuglink() return device_handler.debuglink()
@core_only @core_only
def test_abort(emulator): def test_abort(emulator: Emulator):
device_handler = BackgroundDeviceHandler(emulator.client) device_handler = BackgroundDeviceHandler(emulator.client)
debug = device_handler.debuglink() debug = device_handler.debuglink()
features = device_handler.features() features = device_handler.features()
@ -72,7 +73,7 @@ def test_abort(emulator):
@core_only @core_only
def test_recovery_single_reset(emulator): def test_recovery_single_reset(emulator: Emulator):
device_handler = BackgroundDeviceHandler(emulator.client) device_handler = BackgroundDeviceHandler(emulator.client)
debug = device_handler.debuglink() debug = device_handler.debuglink()
features = device_handler.features() features = device_handler.features()
@ -100,7 +101,7 @@ def test_recovery_single_reset(emulator):
@core_only @core_only
def test_recovery_on_old_wallet(emulator): def test_recovery_on_old_wallet(emulator: Emulator):
"""Check that the recovery workflow started on a disconnected device can survive """Check that the recovery workflow started on a disconnected device can survive
handling by the old Wallet. handling by the old Wallet.
@ -168,8 +169,8 @@ def test_recovery_on_old_wallet(emulator):
@core_only @core_only
def test_recovery_multiple_resets(emulator): def test_recovery_multiple_resets(emulator: Emulator):
def enter_shares_with_restarts(debug): def enter_shares_with_restarts(debug: DebugLink) -> None:
shares = MNEMONIC_SLIP39_ADVANCED_20 shares = MNEMONIC_SLIP39_ADVANCED_20
layout = debug.read_layout() layout = debug.read_layout()
expected_text = "Enter any share" expected_text = "Enter any share"

View File

@ -8,7 +8,9 @@ PIN = "1234"
WIPE_CODE = "9876" WIPE_CODE = "9876"
def setup_device_legacy(client, pin, wipe_code): def setup_device_legacy(
client: debuglink.TrezorClientDebugLink, pin: str, wipe_code: str
) -> None:
device.wipe(client) device.wipe(client)
debuglink.load_device( debuglink.load_device(
client, MNEMONIC12, pin, passphrase_protection=False, label="WIPECODE" client, MNEMONIC12, pin, passphrase_protection=False, label="WIPECODE"
@ -19,7 +21,9 @@ def setup_device_legacy(client, pin, wipe_code):
device.change_wipe_code(client) device.change_wipe_code(client)
def setup_device_core(client, pin, wipe_code): def setup_device_core(
client: debuglink.TrezorClientDebugLink, pin: str, wipe_code: str
) -> None:
device.wipe(client) device.wipe(client)
debuglink.load_device( debuglink.load_device(
client, MNEMONIC12, pin, passphrase_protection=False, label="WIPECODE" client, MNEMONIC12, pin, passphrase_protection=False, label="WIPECODE"
@ -49,6 +53,7 @@ def setup_device_core(client, pin, wipe_code):
@core_only @core_only
def test_wipe_code_activate_core(): def test_wipe_code_activate_core():
with EmulatorWrapper("core") as emu: with EmulatorWrapper("core") as emu:
assert emu.client is not None
# set up device # set up device
setup_device_core(emu.client, PIN, WIPE_CODE) setup_device_core(emu.client, PIN, WIPE_CODE)
@ -80,6 +85,7 @@ def test_wipe_code_activate_core():
@legacy_only @legacy_only
def test_wipe_code_activate_legacy(): def test_wipe_code_activate_legacy():
with EmulatorWrapper("legacy") as emu: with EmulatorWrapper("legacy") as emu:
assert emu.client is not None
# set up device # set up device
setup_device_legacy(emu.client, PIN, WIPE_CODE) setup_device_legacy(emu.client, PIN, WIPE_CODE)

View File

@ -15,6 +15,7 @@
# 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 dataclasses import dataclasses
from typing import List
import pytest import pytest
@ -43,7 +44,7 @@ STRENGTH = 128
@for_all() @for_all()
def test_upgrade_load(gen, tag): def test_upgrade_load(gen: str, tag: str):
def asserts(client): def asserts(client):
assert not client.features.pin_protection assert not client.features.pin_protection
assert not client.features.passphrase_protection assert not client.features.passphrase_protection
@ -71,7 +72,7 @@ def test_upgrade_load(gen, tag):
@for_all("legacy") @for_all("legacy")
def test_upgrade_load_pin(gen, tag): def test_upgrade_load_pin(gen: str, tag: str):
PIN = "1234" PIN = "1234"
def asserts(client): def asserts(client):
@ -113,7 +114,7 @@ def test_upgrade_load_pin(gen, tag):
("legacy", ["v1.7.0", "v1.9.0"]), ("legacy", ["v1.7.0", "v1.9.0"]),
("legacy", ["v1.8.0", "v1.9.0"]), ("legacy", ["v1.8.0", "v1.9.0"]),
) )
def test_storage_upgrade_progressive(gen, tags): def test_storage_upgrade_progressive(gen: str, tags: List[str]):
PIN = "1234" PIN = "1234"
def asserts(client): def asserts(client):
@ -148,7 +149,7 @@ def test_storage_upgrade_progressive(gen, tags):
@for_all("legacy", legacy_minimum_version=(1, 9, 0)) @for_all("legacy", legacy_minimum_version=(1, 9, 0))
def test_upgrade_wipe_code(gen, tag): def test_upgrade_wipe_code(gen: str, tag: str):
PIN = "1234" PIN = "1234"
WIPE_CODE = "4321" WIPE_CODE = "4321"
@ -193,7 +194,7 @@ def test_upgrade_wipe_code(gen, tag):
@for_all("legacy") @for_all("legacy")
def test_upgrade_reset(gen, tag): def test_upgrade_reset(gen: str, tag: str):
def asserts(client): def asserts(client):
assert not client.features.pin_protection assert not client.features.pin_protection
assert not client.features.passphrase_protection assert not client.features.passphrase_protection
@ -226,7 +227,7 @@ def test_upgrade_reset(gen, tag):
@for_all() @for_all()
def test_upgrade_reset_skip_backup(gen, tag): def test_upgrade_reset_skip_backup(gen: str, tag: str):
def asserts(client): def asserts(client):
assert not client.features.pin_protection assert not client.features.pin_protection
assert not client.features.passphrase_protection assert not client.features.passphrase_protection
@ -260,7 +261,7 @@ def test_upgrade_reset_skip_backup(gen, tag):
@for_all(legacy_minimum_version=(1, 7, 2)) @for_all(legacy_minimum_version=(1, 7, 2))
def test_upgrade_reset_no_backup(gen, tag): def test_upgrade_reset_no_backup(gen: str, tag: str):
def asserts(client): def asserts(client):
assert not client.features.pin_protection assert not client.features.pin_protection
assert not client.features.passphrase_protection assert not client.features.passphrase_protection
@ -295,7 +296,7 @@ def test_upgrade_reset_no_backup(gen, tag):
# 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.
@for_all("core", core_minimum_version=(2, 1, 9)) @for_all("core", core_minimum_version=(2, 1, 9))
def test_upgrade_shamir_recovery(gen, tag): def test_upgrade_shamir_recovery(gen: str, tag: str):
with EmulatorWrapper(gen, tag) as emu, BackgroundDeviceHandler( with EmulatorWrapper(gen, tag) as emu, BackgroundDeviceHandler(
emu.client emu.client
) as device_handler: ) as device_handler:
@ -335,7 +336,7 @@ def test_upgrade_shamir_recovery(gen, tag):
@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))
def test_upgrade_u2f(gen, tag): 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(

View File

@ -17,6 +17,7 @@
import pytest 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.tools import parse_path from trezorlib.tools import parse_path
from ..emulators import EmulatorWrapper from ..emulators import EmulatorWrapper
@ -40,8 +41,9 @@ mapping.DEFAULT_MAPPING.register(ApplySettingsCompat)
@pytest.fixture @pytest.fixture
def emulator(gen, tag): def emulator(gen: str, tag: str) -> Emulator:
with EmulatorWrapper(gen, tag) as emu: with EmulatorWrapper(gen, tag) as emu:
assert emu.client is not None
# set up a passphrase-protected device # set up a passphrase-protected device
device.reset( device.reset(
emu.client, emu.client,
@ -60,8 +62,9 @@ def emulator(gen, tag):
core_minimum_version=models.TREZOR_T.minimum_version, core_minimum_version=models.TREZOR_T.minimum_version,
legacy_minimum_version=models.TREZOR_ONE.minimum_version, legacy_minimum_version=models.TREZOR_ONE.minimum_version,
) )
def test_passphrase_works(emulator): def test_passphrase_works(emulator: Emulator):
"""Check that passphrase handling in trezorlib works correctly in all versions.""" """Check that passphrase handling in trezorlib works correctly in all versions."""
assert emulator.client is not None
if emulator.client.features.model == "T" and emulator.client.version < (2, 3, 0): if emulator.client.features.model == "T" and emulator.client.version < (2, 3, 0):
expected_responses = [ expected_responses = [
messages.PassphraseRequest, messages.PassphraseRequest,
@ -95,10 +98,11 @@ def test_passphrase_works(emulator):
core_minimum_version=models.TREZOR_T.minimum_version, core_minimum_version=models.TREZOR_T.minimum_version,
legacy_minimum_version=(1, 9, 0), legacy_minimum_version=(1, 9, 0),
) )
def test_init_device(emulator): def test_init_device(emulator: Emulator):
"""Check that passphrase caching and session_id retaining works correctly across """Check that passphrase caching and session_id retaining works correctly across
supported versions. supported versions.
""" """
assert emulator.client is not None
if emulator.client.features.model == "T" and emulator.client.version < (2, 3, 0): if emulator.client.features.model == "T" and emulator.client.version < (2, 3, 0):
expected_responses = [ expected_responses = [
messages.PassphraseRequest, messages.PassphraseRequest,