1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-12-05 22:18:16 +00:00
trezor-firmware/tests/device_tests/reset_recovery/test_reset_backup.py

145 lines
4.8 KiB
Python
Raw Normal View History

2019-09-26 10:42:20 +00:00
# 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
from shamir_mnemonic import shamir
2019-09-26 10:42:20 +00:00
2023-05-04 12:18:55 +00:00
from trezorlib import device
2024-11-15 16:31:22 +00:00
from trezorlib.debuglink import SessionDebugWrapper as Session
from trezorlib.messages import BackupAvailability, BackupType
2019-09-26 10:42:20 +00:00
2023-05-04 12:18:55 +00:00
from ...common import WITH_MOCK_URANDOM
from ...input_flows import (
InputFlowBip39Backup,
InputFlowResetSkipBackup,
InputFlowSlip39AdvancedBackup,
InputFlowSlip39BasicBackup,
)
2019-09-26 10:42:20 +00:00
2024-11-15 16:31:22 +00:00
def backup_flow_bip39(session: Session) -> bytes:
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 16:31:22 +00:00
device.backup(session)
2019-09-26 10:42:20 +00:00
2023-05-04 12:18:55 +00:00
assert IF.mnemonic is not None
return IF.mnemonic.encode()
2019-09-26 10:42:20 +00:00
2024-11-15 16:31:22 +00:00
def backup_flow_slip39_basic(session: Session):
with session.client as client:
2023-05-04 12:18:55 +00:00
IF = InputFlowSlip39BasicBackup(client, False)
client.set_input_flow(IF.get())
2024-11-15 16:31:22 +00:00
device.backup(session)
2019-09-26 10:42:20 +00:00
2023-05-04 12:18:55 +00:00
groups = shamir.decode_mnemonics(IF.mnemonics[:3])
ems = shamir.recover_ems(groups)
return ems.ciphertext
2019-09-26 10:42:20 +00:00
2024-11-15 16:31:22 +00:00
def backup_flow_slip39_advanced(session: Session):
with session.client as client:
2023-05-04 12:18:55 +00:00
IF = InputFlowSlip39AdvancedBackup(client, False)
client.set_input_flow(IF.get())
2024-11-15 16:31:22 +00:00
device.backup(session)
2019-09-26 10:42:20 +00:00
2023-05-04 12:18:55 +00:00
mnemonics = IF.mnemonics[0:3] + IF.mnemonics[5:8] + IF.mnemonics[10:13]
groups = shamir.decode_mnemonics(mnemonics)
ems = shamir.recover_ems(groups)
return ems.ciphertext
2019-09-26 10:42:20 +00:00
VECTORS = [
(BackupType.Bip39, backup_flow_bip39),
(BackupType.Slip39_Basic_Extendable, backup_flow_slip39_basic),
(BackupType.Slip39_Advanced_Extendable, backup_flow_slip39_advanced),
2019-09-26 10:42:20 +00:00
]
@pytest.mark.models("core")
2019-09-26 10:42:20 +00:00
@pytest.mark.parametrize("backup_type, backup_flow", VECTORS)
@pytest.mark.setup_client(uninitialized=True)
2024-11-15 16:31:22 +00:00
@pytest.mark.uninitialized_session
def test_skip_backup_msg(session: Session, backup_type, backup_flow):
assert session.features.initialized is False
with WITH_MOCK_URANDOM, session:
2019-12-09 16:01:04 +00:00
device.reset(
2024-11-15 16:31:22 +00:00
session,
2019-12-09 16:01:04 +00:00
skip_backup=True,
passphrase_protection=False,
pin_protection=False,
backup_type=backup_type,
)
2019-09-26 10:42:20 +00:00
2024-11-15 16:31:22 +00:00
assert session.features.initialized is True
assert session.features.backup_availability == BackupAvailability.Required
assert session.features.unfinished_backup is False
assert session.features.no_backup is False
assert session.features.backup_type is backup_type
2019-09-26 10:42:20 +00:00
2024-11-15 16:31:22 +00:00
secret = backup_flow(session)
2019-09-26 10:42:20 +00:00
2024-11-15 16:31:22 +00:00
session = session.client.get_session()
assert session.features.initialized is True
assert session.features.backup_availability == BackupAvailability.NotAvailable
assert session.features.unfinished_backup is False
assert session.features.backup_type is backup_type
2019-09-26 10:42:20 +00:00
assert secret is not None
2024-11-15 16:31:22 +00:00
state = session.client.debug.state()
2019-09-26 10:42:20 +00:00
assert state.mnemonic_type is backup_type
assert state.mnemonic_secret == secret
@pytest.mark.models("core")
2019-09-26 10:42:20 +00:00
@pytest.mark.parametrize("backup_type, backup_flow", VECTORS)
@pytest.mark.setup_client(uninitialized=True)
2024-11-15 16:31:22 +00:00
@pytest.mark.uninitialized_session
def test_skip_backup_manual(session: Session, backup_type: BackupType, backup_flow):
assert session.features.initialized is False
with WITH_MOCK_URANDOM, session, session.client as client:
2023-05-04 12:18:55 +00:00
IF = InputFlowResetSkipBackup(client)
client.set_input_flow(IF.get())
2019-09-26 10:42:20 +00:00
device.reset(
2024-11-15 16:31:22 +00:00
session,
2019-09-26 10:42:20 +00:00
pin_protection=False,
passphrase_protection=False,
backup_type=backup_type,
)
2024-11-15 16:31:22 +00:00
assert session.features.initialized is True
assert session.features.backup_availability == BackupAvailability.Required
assert session.features.unfinished_backup is False
assert session.features.no_backup is False
assert session.features.backup_type is backup_type
2019-09-26 10:42:20 +00:00
2024-11-15 16:31:22 +00:00
secret = backup_flow(session)
2019-09-26 10:42:20 +00:00
2024-11-15 16:31:22 +00:00
session = session.client.get_session()
assert session.features.initialized is True
assert session.features.backup_availability == BackupAvailability.NotAvailable
assert session.features.unfinished_backup is False
assert session.features.backup_type is backup_type
2019-09-26 10:42:20 +00:00
assert secret is not None
2024-11-15 16:31:22 +00:00
state = session.client.debug.state()
2019-09-26 10:42:20 +00:00
assert state.mnemonic_type is backup_type
assert state.mnemonic_secret == secret