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

250 lines
9.6 KiB
Python
Raw Normal View History

2024-04-23 10:26:46 +00:00
# This file is part of the Trezor project.
#
# Copyright (C) 2012-2024 SatoshiLabs and contributors
#
# This library is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License version 3
# as published by the Free Software Foundation.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the License along with this library.
# If not, see <https://www.gnu.org/licenses/lgpl-3.0.html>.
import pytest
2024-11-15 16:31:22 +00:00
from trezorlib import device, exceptions, messages
from trezorlib.debuglink import SessionDebugWrapper as Session
2024-04-23 10:26:46 +00:00
from trezorlib.exceptions import Cancelled, TrezorFailure
from .. import translations as TR
from ..common import (
MNEMONIC_SLIP39_SINGLE_EXT_20,
TEST_ADDRESS_N,
WITH_MOCK_URANDOM,
MNEMONIC_SLIP39_BASIC_20_3of6,
)
2024-04-23 10:26:46 +00:00
from ..input_flows import InputFlowSlip39BasicBackup, InputFlowSlip39BasicRecoveryDryRun
pytestmark = pytest.mark.models("core")
2024-04-23 10:26:46 +00:00
@pytest.mark.setup_client(needs_backup=True, mnemonic=MNEMONIC_SLIP39_BASIC_20_3of6)
@WITH_MOCK_URANDOM
2024-11-15 16:31:22 +00:00
def test_repeated_backup(session: Session):
assert session.features.backup_availability == messages.BackupAvailability.Required
assert session.features.recovery_status == messages.RecoveryStatus.Nothing
2024-04-23 10:26:46 +00:00
# initial device backup
mnemonics = []
2024-11-15 16:31:22 +00:00
with session, session.client as client:
2024-04-23 10:26:46 +00:00
IF = InputFlowSlip39BasicBackup(client, False)
client.set_input_flow(IF.get())
2024-11-15 16:31:22 +00:00
device.backup(session)
2024-04-23 10:26:46 +00:00
mnemonics = IF.mnemonics
assert len(mnemonics) == 5
# cannot backup, since we already just did that!
2024-06-04 10:47:09 +00:00
assert (
2024-11-15 16:31:22 +00:00
session.features.backup_availability == messages.BackupAvailability.NotAvailable
2024-06-04 10:47:09 +00:00
)
2024-11-15 16:31:22 +00:00
assert session.features.recovery_status == messages.RecoveryStatus.Nothing
2024-04-23 10:26:46 +00:00
with pytest.raises(TrezorFailure, match=r".*Seed already backed up"):
2024-11-15 16:31:22 +00:00
device.backup(session)
2024-04-23 10:26:46 +00:00
# unlock repeated backup by entering 3 of the 5 shares we have got
2024-11-15 16:31:22 +00:00
with session, session.client as client:
2024-04-23 10:26:46 +00:00
IF = InputFlowSlip39BasicRecoveryDryRun(
client, mnemonics[:3], unlock_repeated_backup=True
)
client.set_input_flow(IF.get())
2024-11-15 16:31:22 +00:00
ret = device.recover(session, type=messages.RecoveryType.UnlockRepeatedBackup)
2024-04-23 10:26:46 +00:00
assert ret == messages.Success(message="Backup unlocked")
2024-06-04 10:47:09 +00:00
assert (
2024-11-15 16:31:22 +00:00
session.features.backup_availability
== messages.BackupAvailability.Available
2024-06-04 10:47:09 +00:00
)
2024-11-15 16:31:22 +00:00
assert session.features.recovery_status == messages.RecoveryStatus.Backup
2024-04-23 10:26:46 +00:00
# we can now perform another backup
2024-11-15 16:31:22 +00:00
with session, session.client as client:
IF = InputFlowSlip39BasicBackup(client, False, repeated=True)
client.set_input_flow(IF.get())
2024-11-15 16:31:22 +00:00
device.backup(session)
# the backup feature is locked again...
assert (
2024-11-15 16:31:22 +00:00
session.features.backup_availability == messages.BackupAvailability.NotAvailable
)
2024-11-15 16:31:22 +00:00
assert session.features.recovery_status == messages.RecoveryStatus.Nothing
with pytest.raises(TrezorFailure, match=r".*Seed already backed up"):
2024-11-15 16:31:22 +00:00
device.backup(session)
@pytest.mark.setup_client(mnemonic=MNEMONIC_SLIP39_SINGLE_EXT_20)
@WITH_MOCK_URANDOM
2024-11-15 16:31:22 +00:00
def test_repeated_backup_upgrade_single(session: Session):
assert (
2024-11-15 16:31:22 +00:00
session.features.backup_availability == messages.BackupAvailability.NotAvailable
)
2024-11-15 16:31:22 +00:00
assert session.features.recovery_status == messages.RecoveryStatus.Nothing
assert session.features.backup_type == messages.BackupType.Slip39_Single_Extendable
# unlock repeated backup by entering the single share
2024-11-15 16:31:22 +00:00
with session, session.client as client:
IF = InputFlowSlip39BasicRecoveryDryRun(
client, MNEMONIC_SLIP39_SINGLE_EXT_20, unlock_repeated_backup=True
)
client.set_input_flow(IF.get())
2024-11-15 16:31:22 +00:00
ret = device.recover(session, type=messages.RecoveryType.UnlockRepeatedBackup)
assert ret == messages.Success(message="Backup unlocked")
assert (
2024-11-15 16:31:22 +00:00
session.features.backup_availability
== messages.BackupAvailability.Available
)
2024-11-15 16:31:22 +00:00
assert session.features.recovery_status == messages.RecoveryStatus.Backup
# we can now perform another backup
2024-11-15 16:31:22 +00:00
with session, session.client as client:
IF = InputFlowSlip39BasicBackup(client, False, repeated=True)
2024-04-23 10:26:46 +00:00
client.set_input_flow(IF.get())
2024-11-15 16:31:22 +00:00
device.backup(session)
2024-04-23 10:26:46 +00:00
# backup type was upgraded:
2024-11-15 16:31:22 +00:00
assert session.features.backup_type == messages.BackupType.Slip39_Basic_Extendable
2024-04-23 10:26:46 +00:00
# the backup feature is locked again...
2024-06-04 10:47:09 +00:00
assert (
2024-11-15 16:31:22 +00:00
session.features.backup_availability == messages.BackupAvailability.NotAvailable
2024-06-04 10:47:09 +00:00
)
2024-11-15 16:31:22 +00:00
assert session.features.recovery_status == messages.RecoveryStatus.Nothing
2024-04-23 10:26:46 +00:00
with pytest.raises(TrezorFailure, match=r".*Seed already backed up"):
2024-11-15 16:31:22 +00:00
device.backup(session)
2024-04-23 10:26:46 +00:00
@pytest.mark.setup_client(needs_backup=True, mnemonic=MNEMONIC_SLIP39_BASIC_20_3of6)
@WITH_MOCK_URANDOM
2024-11-15 16:31:22 +00:00
def test_repeated_backup_cancel(session: Session):
assert session.features.backup_availability == messages.BackupAvailability.Required
assert session.features.recovery_status == messages.RecoveryStatus.Nothing
2024-04-23 10:26:46 +00:00
# initial device backup
mnemonics = []
2024-11-15 16:31:22 +00:00
with session, session.client as client:
2024-04-23 10:26:46 +00:00
IF = InputFlowSlip39BasicBackup(client, False)
client.set_input_flow(IF.get())
2024-11-15 16:31:22 +00:00
device.backup(session)
2024-04-23 10:26:46 +00:00
mnemonics = IF.mnemonics
assert len(mnemonics) == 5
# cannot backup, since we already just did that!
2024-06-04 10:47:09 +00:00
assert (
2024-11-15 16:31:22 +00:00
session.features.backup_availability == messages.BackupAvailability.NotAvailable
2024-06-04 10:47:09 +00:00
)
2024-11-15 16:31:22 +00:00
assert session.features.recovery_status == messages.RecoveryStatus.Nothing
2024-04-23 10:26:46 +00:00
with pytest.raises(TrezorFailure, match=r".*Seed already backed up"):
2024-11-15 16:31:22 +00:00
device.backup(session)
2024-04-23 10:26:46 +00:00
# unlock repeated backup by entering 3 of the 5 shares we have got
2024-11-15 16:31:22 +00:00
with session, session.client as client:
2024-04-23 10:26:46 +00:00
IF = InputFlowSlip39BasicRecoveryDryRun(
client, mnemonics[:3], unlock_repeated_backup=True
)
client.set_input_flow(IF.get())
2024-11-15 16:31:22 +00:00
ret = device.recover(session, type=messages.RecoveryType.UnlockRepeatedBackup)
2024-04-23 10:26:46 +00:00
assert ret == messages.Success(message="Backup unlocked")
2024-06-04 10:47:09 +00:00
assert (
2024-11-15 16:31:22 +00:00
session.features.backup_availability
== messages.BackupAvailability.Available
2024-06-04 10:47:09 +00:00
)
2024-11-15 16:31:22 +00:00
assert session.features.recovery_status == messages.RecoveryStatus.Backup
2024-04-23 10:26:46 +00:00
2024-11-15 16:31:22 +00:00
layout = session.client.debug.read_layout()
assert TR.recovery__unlock_repeated_backup in layout.text_content()
2024-04-23 10:26:46 +00:00
# send a Cancel message
with pytest.raises(Cancelled):
2024-11-15 16:31:22 +00:00
session.call(messages.Cancel())
2024-04-23 10:26:46 +00:00
2024-11-15 16:31:22 +00:00
session.refresh_features()
2024-06-04 10:47:09 +00:00
2024-04-23 10:26:46 +00:00
# the backup feature is locked again...
2024-06-04 10:47:09 +00:00
assert (
2024-11-15 16:31:22 +00:00
session.features.backup_availability == messages.BackupAvailability.NotAvailable
2024-06-04 10:47:09 +00:00
)
2024-11-15 16:31:22 +00:00
assert session.features.recovery_status == messages.RecoveryStatus.Nothing
2024-06-04 10:47:09 +00:00
with pytest.raises(TrezorFailure, match=r".*Seed already backed up"):
2024-11-15 16:31:22 +00:00
device.backup(session)
2024-06-04 10:47:09 +00:00
@pytest.mark.setup_client(needs_backup=True, mnemonic=MNEMONIC_SLIP39_BASIC_20_3of6)
@WITH_MOCK_URANDOM
2024-11-15 16:31:22 +00:00
def test_repeated_backup_send_disallowed_message(session: Session):
assert session.features.backup_availability == messages.BackupAvailability.Required
assert session.features.recovery_status == messages.RecoveryStatus.Nothing
2024-06-04 10:47:09 +00:00
# initial device backup
mnemonics = []
2024-11-15 16:31:22 +00:00
with session, session.client as client:
2024-06-04 10:47:09 +00:00
IF = InputFlowSlip39BasicBackup(client, False)
client.set_input_flow(IF.get())
2024-11-15 16:31:22 +00:00
device.backup(session)
2024-06-04 10:47:09 +00:00
mnemonics = IF.mnemonics
assert len(mnemonics) == 5
# cannot backup, since we already just did that!
assert (
2024-11-15 16:31:22 +00:00
session.features.backup_availability == messages.BackupAvailability.NotAvailable
2024-06-04 10:47:09 +00:00
)
2024-11-15 16:31:22 +00:00
assert session.features.recovery_status == messages.RecoveryStatus.Nothing
2024-04-23 10:26:46 +00:00
with pytest.raises(TrezorFailure, match=r".*Seed already backed up"):
2024-11-15 16:31:22 +00:00
device.backup(session)
2024-06-04 10:47:09 +00:00
# unlock repeated backup by entering 3 of the 5 shares we have got
2024-11-15 16:31:22 +00:00
with session, session.client as client:
2024-06-04 10:47:09 +00:00
IF = InputFlowSlip39BasicRecoveryDryRun(
client, mnemonics[:3], unlock_repeated_backup=True
)
client.set_input_flow(IF.get())
2024-11-15 16:31:22 +00:00
ret = device.recover(session, type=messages.RecoveryType.UnlockRepeatedBackup)
2024-06-04 10:47:09 +00:00
assert ret == messages.Success(message="Backup unlocked")
assert (
2024-11-15 16:31:22 +00:00
session.features.backup_availability
== messages.BackupAvailability.Available
2024-06-04 10:47:09 +00:00
)
2024-11-15 16:31:22 +00:00
assert session.features.recovery_status == messages.RecoveryStatus.Backup
2024-06-04 10:47:09 +00:00
2024-11-15 16:31:22 +00:00
layout = session.client.debug.read_layout()
assert TR.recovery__unlock_repeated_backup in layout.text_content()
2024-06-04 10:47:09 +00:00
# send a GetAddress message
2024-11-15 16:31:22 +00:00
resp = session.call_raw(
2024-06-04 10:47:09 +00:00
messages.GetAddress(
coin_name="Testnet",
address_n=TEST_ADDRESS_N,
show_display=True,
script_type=messages.InputScriptType.SPENDADDRESS,
)
)
assert isinstance(resp, messages.Failure)
assert "not allowed" in resp.message
2024-11-15 16:31:22 +00:00
assert session.features.backup_availability == messages.BackupAvailability.Available
assert session.features.recovery_status == messages.RecoveryStatus.Backup
2024-06-04 10:47:09 +00:00
# we are still on the confirmation screen!
assert (
2024-11-15 16:31:22 +00:00
TR.recovery__unlock_repeated_backup
in session.client.debug.read_layout().text_content()
)
2024-11-15 16:31:22 +00:00
with pytest.raises(exceptions.Cancelled):
session.call(messages.Cancel())