1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-12-15 10:58:09 +00:00
trezor-firmware/tests/click_tests/test_repeated_backup.py

201 lines
6.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>.
from typing import TYPE_CHECKING
import pytest
from trezorlib import device, messages
from .. import buttons
from ..common import WITH_MOCK_URANDOM
from . import recovery, reset
from .common import go_next
if TYPE_CHECKING:
from ..device_handler import BackgroundDeviceHandler
pytestmark = pytest.mark.models("core")
2024-04-23 10:26:46 +00:00
@pytest.mark.setup_client(uninitialized=True)
@WITH_MOCK_URANDOM
def test_repeated_backup(
device_handler: "BackgroundDeviceHandler",
):
features = device_handler.features()
debug = device_handler.debuglink()
assert features.initialized is False
device_handler.run(
device.reset,
strength=128,
backup_type=messages.BackupType.Slip39_Basic,
pin_protection=False,
)
# confirm new wallet
reset.confirm_new_wallet(debug)
# confirm back up
reset.confirm_read(debug)
# confirm backup intro
reset.confirm_read(debug)
2024-04-23 10:26:46 +00:00
# let's make a 1-of-1 backup to start with...
2024-06-04 10:47:09 +00:00
assert debug.model is not None
model_name: str = debug.model.internal_name
# confirm checklist
reset.confirm_read(debug)
2024-04-23 10:26:46 +00:00
# shares=1
2024-06-04 10:47:09 +00:00
reset.set_selection(debug, buttons.reset_minus(model_name), 5 - 1)
2024-04-23 10:26:46 +00:00
# confirm checklist
reset.confirm_read(debug)
# threshold=1
2024-06-04 10:47:09 +00:00
reset.set_selection(debug, buttons.reset_plus(model_name), 0)
2024-04-23 10:26:46 +00:00
# confirm checklist
reset.confirm_read(debug)
# confirm backup warning
reset.confirm_read(debug, middle_r=True)
# read words
initial_backup_1_of_1 = reset.read_words(debug)
# confirm words
reset.confirm_words(debug, initial_backup_1_of_1)
# confirm share checked
reset.confirm_read(debug)
# confirm backup done
reset.confirm_read(debug)
# Your backup is done
go_next(debug)
# 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
2024-04-23 10:26:46 +00:00
assert features.initialized is True
assert features.backup_availability == messages.BackupAvailability.NotAvailable
2024-04-23 10:26:46 +00:00
assert features.no_backup is False
assert features.recovery_status == messages.RecoveryStatus.Nothing
2024-04-23 10:26:46 +00:00
# run recovery to unlock backup
device_handler.run(
device.recover,
type=messages.RecoveryType.UnlockRepeatedBackup,
2024-04-23 10:26:46 +00:00
)
recovery.confirm_recovery(debug, "recovery__title_unlock_repeated_backup")
recovery.select_number_of_words(debug, num_of_words=20, unlock_repeated_backup=True)
recovery.enter_seed(
debug,
initial_backup_1_of_1,
True,
"recovery__enter_backup",
"recovery__unlock_repeated_backup",
)
# backup is enabled
assert device_handler.result().message == "Backup unlocked"
# we are now in recovery mode
features = device_handler.features()
assert features.backup_type is messages.BackupType.Slip39_Basic_Extendable
2024-04-23 10:26:46 +00:00
assert features.initialized is True
assert features.backup_availability == messages.BackupAvailability.Available
2024-04-23 10:26:46 +00:00
assert features.no_backup is False
assert features.recovery_status == messages.RecoveryStatus.Backup
2024-04-23 10:26:46 +00:00
# at this point, the backup is unlocked...
2023-11-21 15:55:46 +00:00
go_next(debug)
2024-04-23 10:26:46 +00:00
# ... so let's try to do a 2-of-3 backup
# confirm backup intro
reset.confirm_read(debug)
2024-04-23 10:26:46 +00:00
# confirm checklist
reset.confirm_read(debug)
# shares=3
2024-06-04 10:47:09 +00:00
reset.set_selection(debug, buttons.reset_minus(model_name), 5 - 3)
2024-04-23 10:26:46 +00:00
# confirm checklist
reset.confirm_read(debug)
# threshold=2
2024-06-04 10:47:09 +00:00
reset.set_selection(debug, buttons.reset_minus(model_name), 1)
2024-04-23 10:26:46 +00:00
# confirm checklist
reset.confirm_read(debug)
# confirm backup warning
reset.confirm_read(debug, middle_r=True)
second_backup_2_of_3: list[str] = []
for _ in range(3):
# read words
words = reset.read_words(debug, do_htc=False)
# confirm words
reset.confirm_words(debug, words)
# confirm share checked
reset.confirm_read(debug)
second_backup_2_of_3.append(" ".join(words))
# we are not in recovery mode anymore, because we finished the backup process!
features = device_handler.features()
assert features.backup_type is messages.BackupType.Slip39_Basic_Extendable
2024-04-23 10:26:46 +00:00
assert features.initialized is True
assert features.backup_availability == messages.BackupAvailability.NotAvailable
2024-04-23 10:26:46 +00:00
assert features.no_backup is False
assert features.recovery_status == messages.RecoveryStatus.Nothing
2024-04-23 10:26:46 +00:00
# try to unlock backup again...
device_handler.run(
device.recover,
type=messages.RecoveryType.UnlockRepeatedBackup,
2024-04-23 10:26:46 +00:00
)
recovery.confirm_recovery(debug, "recovery__title_unlock_repeated_backup")
# ... this time with the 2 shares from the *new* backup, which was a 2-of-3!
recovery.select_number_of_words(debug, num_of_words=20, unlock_repeated_backup=True)
recovery.enter_shares(
debug,
second_backup_2_of_3[-2:],
"recovery__title_dry_run",
2024-04-23 10:26:46 +00:00
"recovery__enter_backup",
"recovery__unlock_repeated_backup",
)
assert device_handler.result().message == "Backup unlocked"
# we are now in recovery mode again!
features = device_handler.features()
assert features.backup_type is messages.BackupType.Slip39_Basic_Extendable
2024-04-23 10:26:46 +00:00
assert features.initialized is True
assert features.backup_availability == messages.BackupAvailability.Available
2024-04-23 10:26:46 +00:00
assert features.no_backup is False
assert features.recovery_status == messages.RecoveryStatus.Backup
2024-04-23 10:26:46 +00:00
# but if we cancel the backup at this point...
reset.cancel_backup(debug)
# ...we are out of recovery mode!
features = device_handler.features()
assert features.backup_type is messages.BackupType.Slip39_Basic_Extendable
2024-04-23 10:26:46 +00:00
assert features.initialized is True
assert features.backup_availability == messages.BackupAvailability.NotAvailable
2024-04-23 10:26:46 +00:00
assert features.no_backup is False
assert features.recovery_status == messages.RecoveryStatus.Nothing