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_msg_backup_device.py

198 lines
7.2 KiB
Python
Raw Normal View History

# This file is part of the Trezor project.
#
# Copyright (C) 2012-2019 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
import shamir_mnemonic as shamir
from trezorlib import device, messages
from trezorlib.debuglink import LayoutType
2024-11-15 15:06:55 +00:00
from trezorlib.debuglink import SessionDebugWrapper as Session
2019-09-23 12:49:54 +00:00
from trezorlib.exceptions import TrezorFailure
from ..common import (
MNEMONIC12,
2019-09-23 12:49:54 +00:00
MNEMONIC_SLIP39_ADVANCED_20,
MNEMONIC_SLIP39_CUSTOM_SECRET,
MNEMONIC_SLIP39_BASIC_20_3of6,
MNEMONIC_SLIP39_CUSTOM_1of1,
)
2023-05-04 12:18:55 +00:00
from ..input_flows import (
InputFlowBip39Backup,
InputFlowSlip39AdvancedBackup,
InputFlowSlip39BasicBackup,
InputFlowSlip39CustomBackup,
2023-05-04 12:18:55 +00:00
)
@pytest.mark.models("core") # TODO we want this for t1 too
@pytest.mark.setup_client(needs_backup=True, mnemonic=MNEMONIC12)
2024-11-15 15:06:55 +00:00
def test_backup_bip39(session: Session):
assert session.features.backup_availability == messages.BackupAvailability.Required
2024-11-15 15:06:55 +00:00
with session.client as client:
2023-05-04 12:18:55 +00:00
IF = InputFlowBip39Backup(client)
client.set_input_flow(IF.get())
2024-11-15 15:06:55 +00:00
device.backup(session)
2023-05-04 12:18:55 +00:00
assert IF.mnemonic == MNEMONIC12
2024-11-15 15:06:55 +00:00
# TODO remove session.init_device()
assert session.features.initialized is True
assert (
2024-11-15 15:06:55 +00:00
session.features.backup_availability == messages.BackupAvailability.NotAvailable
)
2024-11-15 15:06:55 +00:00
assert session.features.unfinished_backup is False
assert session.features.no_backup is False
assert session.features.backup_type is messages.BackupType.Bip39
@pytest.mark.models("core")
@pytest.mark.setup_client(needs_backup=True, mnemonic=MNEMONIC_SLIP39_BASIC_20_3of6)
@pytest.mark.parametrize(
"click_info", [True, False], ids=["click_info", "no_click_info"]
)
2024-11-15 15:06:55 +00:00
def test_backup_slip39_basic(session: Session, click_info: bool):
if click_info and session.client.layout_type is LayoutType.TR:
pytest.skip("click_info not implemented on T2B1")
2023-05-12 09:19:35 +00:00
2024-11-15 15:06:55 +00:00
assert session.features.backup_availability == messages.BackupAvailability.Required
2024-11-15 15:06:55 +00:00
with session.client as client:
2023-05-04 12:18:55 +00:00
IF = InputFlowSlip39BasicBackup(client, click_info)
client.set_input_flow(IF.get())
2024-11-15 15:06:55 +00:00
device.backup(session)
2024-11-15 15:06:55 +00:00
# TODO remove session.init_device()
assert session.features.initialized is True
assert (
2024-11-15 15:06:55 +00:00
session.features.backup_availability == messages.BackupAvailability.NotAvailable
)
2024-11-15 15:06:55 +00:00
assert session.features.unfinished_backup is False
assert session.features.no_backup is False
assert session.features.backup_type is messages.BackupType.Slip39_Basic
2019-09-20 16:06:02 +00:00
expected_ms = shamir.combine_mnemonics(MNEMONIC_SLIP39_BASIC_20_3of6)
2023-05-04 12:18:55 +00:00
actual_ms = shamir.combine_mnemonics(IF.mnemonics[:3])
assert expected_ms == actual_ms
2019-09-23 12:49:54 +00:00
@pytest.mark.models("core")
@pytest.mark.setup_client(needs_backup=True, mnemonic=MNEMONIC_SLIP39_ADVANCED_20)
@pytest.mark.parametrize(
"click_info", [True, False], ids=["click_info", "no_click_info"]
)
2024-11-15 15:06:55 +00:00
def test_backup_slip39_advanced(session: Session, click_info: bool):
if click_info and session.client.layout_type is LayoutType.TR:
pytest.skip("click_info not implemented on T2B1")
2023-05-12 09:19:35 +00:00
2024-11-15 15:06:55 +00:00
assert session.features.backup_availability == messages.BackupAvailability.Required
2019-09-23 12:49:54 +00:00
2024-11-15 15:06:55 +00:00
with session.client as client:
2023-05-04 12:18:55 +00:00
IF = InputFlowSlip39AdvancedBackup(client, click_info)
client.set_input_flow(IF.get())
2024-11-15 15:06:55 +00:00
device.backup(session)
2019-09-23 12:49:54 +00:00
2024-11-15 15:06:55 +00:00
# TODO remove session.init_device()
assert session.features.initialized is True
assert (
2024-11-15 15:06:55 +00:00
session.features.backup_availability == messages.BackupAvailability.NotAvailable
)
2024-11-15 15:06:55 +00:00
assert session.features.unfinished_backup is False
assert session.features.no_backup is False
assert session.features.backup_type is messages.BackupType.Slip39_Advanced
2019-09-23 12:49:54 +00:00
expected_ms = shamir.combine_mnemonics(MNEMONIC_SLIP39_ADVANCED_20)
actual_ms = shamir.combine_mnemonics(
2023-05-04 12:18:55 +00:00
IF.mnemonics[:3] + IF.mnemonics[5:8] + IF.mnemonics[10:13]
2019-09-23 12:49:54 +00:00
)
assert expected_ms == actual_ms
@pytest.mark.models("core")
@pytest.mark.setup_client(needs_backup=True, mnemonic=MNEMONIC_SLIP39_CUSTOM_1of1[0])
@pytest.mark.parametrize(
"share_threshold,share_count",
[(1, 1), (2, 2), (3, 5)],
ids=["1_of_1", "2_of_2", "3_of_5"],
)
2024-11-15 15:06:55 +00:00
def test_backup_slip39_custom(session: Session, share_threshold, share_count):
assert session.features.backup_availability == messages.BackupAvailability.Required
2024-11-15 15:06:55 +00:00
with session.client as client:
IF = InputFlowSlip39CustomBackup(client, share_count)
client.set_input_flow(IF.get())
device.backup(
2024-11-15 15:06:55 +00:00
session, group_threshold=1, groups=[(share_threshold, share_count)]
)
2024-11-15 15:06:55 +00:00
# TODO remove session.init_device()
assert session.features.initialized is True
assert (
2024-11-15 15:06:55 +00:00
session.features.backup_availability == messages.BackupAvailability.NotAvailable
)
2024-11-15 15:06:55 +00:00
assert session.features.unfinished_backup is False
assert session.features.no_backup is False
assert len(IF.mnemonics) == share_count
assert (
shamir.combine_mnemonics(IF.mnemonics[-share_threshold:]).hex()
== MNEMONIC_SLIP39_CUSTOM_SECRET
)
2019-09-23 12:49:54 +00:00
# we only test this with bip39 because the code path is always the same
@pytest.mark.setup_client(no_backup=True)
2024-11-15 15:06:55 +00:00
def test_no_backup_fails(session: Session):
# TODO ?? session.ensure_unlocked()
assert session.features.initialized is True
assert session.features.no_backup is True
assert (
2024-11-15 15:06:55 +00:00
session.features.backup_availability == messages.BackupAvailability.NotAvailable
)
2019-09-23 12:49:54 +00:00
# backup attempt should fail because no_backup=True
2019-10-01 10:04:55 +00:00
with pytest.raises(TrezorFailure, match=r".*Seed already backed up"):
2024-11-15 15:06:55 +00:00
device.backup(session)
2019-09-23 12:49:54 +00:00
# we only test this with bip39 because the code path is always the same
@pytest.mark.setup_client(needs_backup=True)
2024-11-15 15:06:55 +00:00
def test_interrupt_backup_fails(session: Session):
raise Exception("TEST DOESN'T WORK BECAUSE OF CHANGED INITIALIZE BEHAVIOUR")
# TODO ?? session.ensure_unlocked()
assert session.features.initialized is True
assert session.features.backup_availability == messages.BackupAvailability.Required
assert session.features.unfinished_backup is False
assert session.features.no_backup is False
2019-09-23 12:49:54 +00:00
# start backup
2024-11-15 15:06:55 +00:00
session.call_raw(messages.BackupDevice())
2019-09-23 12:49:54 +00:00
# interupt backup by sending initialize
2024-11-15 15:06:55 +00:00
# TODO remove session.init_device()
2019-09-23 12:49:54 +00:00
# check that device state is as expected
2024-11-15 15:06:55 +00:00
assert session.features.initialized is True
assert (
2024-11-15 15:06:55 +00:00
session.features.backup_availability == messages.BackupAvailability.NotAvailable
)
2024-11-15 15:06:55 +00:00
assert session.features.unfinished_backup is True
assert session.features.no_backup is False
2019-09-23 12:49:54 +00:00
# Second attempt at backup should fail
2019-10-01 10:04:55 +00:00
with pytest.raises(TrezorFailure, match=r".*Seed already backed up"):
2024-11-15 15:06:55 +00:00
device.backup(session)