Skip to content

Commit

Permalink
fix(tests): update tests for newly introduced device.setup()
Browse files Browse the repository at this point in the history
  • Loading branch information
matejcik committed Jan 13, 2025
1 parent e016e08 commit 1d960ab
Show file tree
Hide file tree
Showing 32 changed files with 200 additions and 158 deletions.
2 changes: 1 addition & 1 deletion tests/click_tests/test_autolock.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def set_autolock_delay(device_handler: "BackgroundDeviceHandler", delay_ms: int)
if debug.layout_type is LayoutType.Quicksilver:
layout = tap_to_confirm(debug)
assert layout.main_component() == "Homescreen"
assert device_handler.result() == "Settings applied"
device_handler.result()


@pytest.mark.setup_client(pin=PIN4)
Expand Down
14 changes: 9 additions & 5 deletions tests/click_tests/test_backup_slip39_custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
from trezorlib.debuglink import LayoutType

from .. import translations as TR
from ..common import EXTERNAL_ENTROPY, WITH_MOCK_URANDOM, generate_entropy
from ..common import EXTERNAL_ENTROPY, MOCK_GET_ENTROPY, generate_entropy
from . import reset

if TYPE_CHECKING:
Expand All @@ -41,7 +41,6 @@
],
)
@pytest.mark.setup_client(uninitialized=True)
@WITH_MOCK_URANDOM
def test_backup_slip39_custom(
device_handler: "BackgroundDeviceHandler",
group_threshold: int,
Expand All @@ -54,10 +53,12 @@ def test_backup_slip39_custom(
assert features.initialized is False

device_handler.run(
device.reset,
device.setup,
strength=128,
backup_type=messages.BackupType.Slip39_Basic,
pin_protection=False,
entropy_check_count=0,
_get_entropy=MOCK_GET_ENTROPY,
)

# confirm new wallet
Expand All @@ -66,7 +67,8 @@ def test_backup_slip39_custom(
# cancel back up
reset.cancel_backup(debug, confirm=True)

assert device_handler.result() == "Initialized"
# retrieve the result to check that it's not a TrezorFailure exception
device_handler.result()

device_handler.run(
device.backup,
Expand Down Expand Up @@ -116,7 +118,9 @@ def test_backup_slip39_custom(
# validate that all combinations will result in the correct master secret
reset.validate_mnemonics(all_words[:share_threshold], secret)

assert device_handler.result() == "Seed successfully backed up"
# retrieve the result to check that it's not a TrezorFailure exception
device_handler.result()

features = device_handler.features()
assert features.initialized is True
assert features.backup_availability == messages.BackupAvailability.NotAvailable
Expand Down
3 changes: 2 additions & 1 deletion tests/click_tests/test_recovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ def prepare_recovery_and_evaluate(

yield debug

assert isinstance(device_handler.result(), messages.Success)
device_handler.result()

features = device_handler.features()
assert features.initialized is True
assert features.recovery_status == messages.RecoveryStatus.Nothing
Expand Down
17 changes: 10 additions & 7 deletions tests/click_tests/test_repeated_backup.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from trezorlib import device, messages

from .. import buttons
from ..common import WITH_MOCK_URANDOM
from ..common import MOCK_GET_ENTROPY
from . import recovery, reset
from .common import go_next

Expand All @@ -33,7 +33,6 @@


@pytest.mark.setup_client(uninitialized=True)
@WITH_MOCK_URANDOM
def test_repeated_backup(
device_handler: "BackgroundDeviceHandler",
):
Expand All @@ -43,10 +42,12 @@ def test_repeated_backup(
assert features.initialized is False

device_handler.run(
device.reset,
device.setup,
strength=128,
backup_type=messages.BackupType.Slip39_Basic,
pin_protection=False,
entropy_check_count=0,
_get_entropy=MOCK_GET_ENTROPY,
)

# confirm new wallet
Expand Down Expand Up @@ -84,8 +85,9 @@ def test_repeated_backup(
# Your backup is done
go_next(debug)

# retrieve the result to check that it does not raise a failure
device_handler.result()
# great ... device is initialized, backup done, and we are not in recovery mode!
assert device_handler.result() == "Initialized"
features = device_handler.features()
assert features.backup_type is messages.BackupType.Slip39_Basic_Extendable
assert features.initialized is True
Expand All @@ -109,8 +111,8 @@ def test_repeated_backup(
"recovery__unlock_repeated_backup",
)

# backup is enabled
assert device_handler.result().message == "Backup unlocked"
# check non-exception result
device_handler.result()

# we are now in recovery mode
features = device_handler.features()
Expand Down Expand Up @@ -178,7 +180,8 @@ def test_repeated_backup(
"recovery__unlock_repeated_backup",
)

assert device_handler.result().message == "Backup unlocked"
# check non-exception result
device_handler.result()

# we are now in recovery mode again!
features = device_handler.features()
Expand Down
11 changes: 7 additions & 4 deletions tests/click_tests/test_reset_bip39.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from trezorlib import device, messages

from .. import translations as TR
from ..common import WITH_MOCK_URANDOM
from ..common import MOCK_GET_ENTROPY
from . import reset
from .common import go_next

Expand All @@ -33,18 +33,19 @@


@pytest.mark.setup_client(uninitialized=True)
@WITH_MOCK_URANDOM
def test_reset_bip39(device_handler: "BackgroundDeviceHandler"):
features = device_handler.features()
debug = device_handler.debuglink()

assert features.initialized is False

device_handler.run(
device.reset,
device.setup,
strength=128,
backup_type=messages.BackupType.Bip39,
pin_protection=False,
entropy_check_count=0,
_get_entropy=MOCK_GET_ENTROPY,
)

# confirm new wallet
Expand Down Expand Up @@ -82,7 +83,9 @@ def test_reset_bip39(device_handler: "BackgroundDeviceHandler"):

# TODO: some validation of the generated secret?

assert device_handler.result() == "Initialized"
# retrieve the result to check that it's not a TrezorFailure exception
device_handler.result()

features = device_handler.features()
assert features.initialized is True
assert features.backup_availability == messages.BackupAvailability.NotAvailable
Expand Down
10 changes: 6 additions & 4 deletions tests/click_tests/test_reset_slip39_advanced.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from trezorlib import device, messages

from .. import buttons
from ..common import EXTERNAL_ENTROPY, WITH_MOCK_URANDOM, generate_entropy
from ..common import EXTERNAL_ENTROPY, MOCK_GET_ENTROPY, generate_entropy
from . import reset

if TYPE_CHECKING:
Expand All @@ -39,7 +39,6 @@
pytest.param(16, 16, 16, 16, id="16of16", marks=pytest.mark.slow),
],
)
@WITH_MOCK_URANDOM
def test_reset_slip39_advanced(
device_handler: "BackgroundDeviceHandler",
group_count: int,
Expand All @@ -53,9 +52,11 @@ def test_reset_slip39_advanced(
assert features.initialized is False

device_handler.run(
device.reset,
device.setup,
backup_type=messages.BackupType.Slip39_Advanced,
pin_protection=False,
entropy_check_count=0,
_get_entropy=MOCK_GET_ENTROPY,
)

# confirm new wallet
Expand Down Expand Up @@ -162,7 +163,8 @@ def test_reset_slip39_advanced(
# validate that all combinations will result in the correct master secret
reset.validate_mnemonics(all_words, secret)

assert device_handler.result() == "Initialized"
# retrieve the result to check that it's not a TrezorFailure exception
device_handler.result()

features = device_handler.features()
assert features.initialized is True
Expand Down
11 changes: 7 additions & 4 deletions tests/click_tests/test_reset_slip39_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from trezorlib import device, messages

from .. import buttons
from ..common import EXTERNAL_ENTROPY, WITH_MOCK_URANDOM, generate_entropy
from ..common import EXTERNAL_ENTROPY, MOCK_GET_ENTROPY, generate_entropy
from . import reset

if TYPE_CHECKING:
Expand All @@ -39,7 +39,6 @@
],
)
@pytest.mark.setup_client(uninitialized=True)
@WITH_MOCK_URANDOM
def test_reset_slip39_basic(
device_handler: "BackgroundDeviceHandler", num_of_shares: int, threshold: int
):
Expand All @@ -49,10 +48,12 @@ def test_reset_slip39_basic(
assert features.initialized is False

device_handler.run(
device.reset,
device.setup,
strength=128,
backup_type=messages.BackupType.Slip39_Basic,
pin_protection=False,
entropy_check_count=0,
_get_entropy=MOCK_GET_ENTROPY,
)

# confirm new wallet
Expand Down Expand Up @@ -137,7 +138,9 @@ def test_reset_slip39_basic(
# validate that all combinations will result in the correct master secret
reset.validate_mnemonics(all_words, secret)

assert device_handler.result() == "Initialized"
# retrieve the result to check that it's not a TrezorFailure exception
device_handler.result()

features = device_handler.features()
assert features.initialized is True
assert features.backup_availability == messages.BackupAvailability.NotAvailable
Expand Down
3 changes: 1 addition & 2 deletions tests/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,7 @@
)

# So that all the random things are consistent
MOCK_OS_URANDOM = mock.Mock(return_value=EXTERNAL_ENTROPY)
WITH_MOCK_URANDOM = mock.patch("os.urandom", MOCK_OS_URANDOM)
MOCK_GET_ENTROPY = mock.Mock(return_value=EXTERNAL_ENTROPY)


def parametrize_using_common_fixtures(*paths: str) -> "MarkDecorator":
Expand Down
17 changes: 13 additions & 4 deletions tests/device_handler.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
from __future__ import annotations

import typing as t
from concurrent.futures import ThreadPoolExecutor
from typing import TYPE_CHECKING, Any, Callable

import typing_extensions as tx

from trezorlib.client import PASSPHRASE_ON_DEVICE
from trezorlib.messages import DebugWaitType
from trezorlib.transport import udp

if TYPE_CHECKING:
if t.TYPE_CHECKING:
from trezorlib._internal.emulator import Emulator
from trezorlib.debuglink import DebugLink
from trezorlib.debuglink import TrezorClientDebugLink as Client
from trezorlib.messages import Features

P = tx.ParamSpec("P")


udp.SOCKET_TIMEOUT = 0.1

Expand Down Expand Up @@ -48,7 +52,12 @@ def _configure_client(self, client: "Client") -> None:
self.client.watch_layout(True)
self.client.debug.input_wait_type = DebugWaitType.CURRENT_LAYOUT

def run(self, function: Callable[..., Any], *args: Any, **kwargs: Any) -> None:
def run(
self,
function: t.Callable[tx.Concatenate["Client", P], t.Any],
*args: P.args,
**kwargs: P.kwargs,
) -> None:
"""Runs some function that interacts with a device.
Makes sure the UI is updated before returning.
Expand Down Expand Up @@ -79,7 +88,7 @@ def restart(self, emulator: "Emulator") -> None:
emulator.restart()
self._configure_client(emulator.client) # type: ignore [client cannot be None]

def result(self, timeout: float | None = None) -> Any:
def result(self, timeout: float | None = None) -> t.Any:
if self.task is None:
raise RuntimeError("No task running")
try:
Expand Down
4 changes: 2 additions & 2 deletions tests/device_tests/bitcoin/test_authorize_coinjoin.py
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,7 @@ def test_sign_tx_spend(client: Client):
client.set_expected_responses(
[
messages.ButtonRequest(code=B.Other),
messages.UnlockedPathRequest(),
messages.UnlockedPathRequest,
request_input(0),
request_output(0),
request_output(1),
Expand Down Expand Up @@ -531,7 +531,7 @@ def test_sign_tx_migration(client: Client):
client.set_expected_responses(
[
messages.ButtonRequest(code=B.Other),
messages.UnlockedPathRequest(),
messages.UnlockedPathRequest,
request_input(0),
request_input(1),
request_output(0),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,13 @@ def _test_secret(
with client:
IF = InputFlowSlip39AdvancedRecovery(client, shares, click_info=click_info)
client.set_input_flow(IF.get())
ret = device.recover(
device.recover(
client,
pin_protection=False,
passphrase_protection=False,
label="label",
)

# Workflow succesfully ended
assert ret == messages.Success(message="Device recovered")
assert client.features.initialized is True
assert client.features.pin_protection is False
assert client.features.passphrase_protection is False
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,14 @@ def test_2of3_dryrun(client: Client):
client, EXTRA_GROUP_SHARE + MNEMONIC_SLIP39_ADVANCED_20
)
client.set_input_flow(IF.get())
ret = device.recover(
device.recover(
client,
passphrase_protection=False,
pin_protection=False,
label="label",
type=messages.RecoveryType.DryRun,
)

# Dry run was successful
assert ret == messages.Success(
message="The seed is valid and matches the one in the device"
)


@pytest.mark.setup_client(mnemonic=MNEMONIC_SLIP39_ADVANCED_20)
def test_2of3_invalid_seed_dryrun(client: Client):
Expand Down
Loading

0 comments on commit 1d960ab

Please sign in to comment.