1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-11-01 04:49:31 +00:00
trezor-firmware/tests/device_tests/test_msg_backup_device.py

317 lines
12 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
2019-09-23 12:49:54 +00:00
from trezorlib.exceptions import TrezorFailure
from trezorlib.messages import ButtonRequestType as B
from ..common import (
MNEMONIC12,
2019-09-23 12:49:54 +00:00
MNEMONIC_SLIP39_ADVANCED_20,
MNEMONIC_SLIP39_BASIC_20_3of6,
click_through,
read_and_confirm_mnemonic,
)
@pytest.mark.skip_t1 # TODO we want this for t1 too
@pytest.mark.setup_client(needs_backup=True, mnemonic=MNEMONIC12)
def test_backup_bip39(client):
assert client.features.needs_backup is True
mnemonic = None
def input_flow():
nonlocal mnemonic
yield # Confirm Backup
client.debug.press_yes()
yield # Mnemonic phrases
mnemonic = read_and_confirm_mnemonic(client.debug, words=12)
yield # Confirm success
client.debug.press_yes()
yield # Backup is done
client.debug.press_yes()
with client:
client.set_input_flow(input_flow)
client.set_expected_responses(
[
messages.ButtonRequest(code=B.ResetDevice),
messages.ButtonRequest(code=B.ResetDevice),
messages.ButtonRequest(code=B.Success),
messages.ButtonRequest(code=B.Success),
messages.Success,
messages.Features,
]
)
device.backup(client)
assert mnemonic == MNEMONIC12
2019-09-20 16:06:02 +00:00
client.init_device()
2019-09-23 12:49:54 +00:00
assert client.features.initialized is True
2019-09-20 16:06:02 +00:00
assert client.features.needs_backup is False
2019-09-23 12:49:54 +00:00
assert client.features.unfinished_backup is False
assert client.features.no_backup is False
assert client.features.backup_type is messages.BackupType.Bip39
@pytest.mark.skip_t1
@pytest.mark.setup_client(needs_backup=True, mnemonic=MNEMONIC_SLIP39_BASIC_20_3of6)
def test_backup_slip39_basic(client):
assert client.features.needs_backup is True
mnemonics = []
def input_flow():
# 1. Checklist
# 2. Number of shares (5)
# 3. Checklist
# 4. Threshold (3)
# 5. Checklist
# 6. Confirm show seeds
yield from click_through(client.debug, screens=6, code=B.ResetDevice)
# Mnemonic phrases
for _ in range(5):
yield # Phrase screen
mnemonic = read_and_confirm_mnemonic(client.debug, words=20)
mnemonics.append(mnemonic)
yield # Confirm continue to next
client.debug.press_yes()
# Confirm backup
yield
client.debug.press_yes()
with client:
client.set_input_flow(input_flow)
client.set_expected_responses(
[
messages.ButtonRequest(code=B.ResetDevice),
messages.ButtonRequest(code=B.ResetDevice),
messages.ButtonRequest(code=B.ResetDevice),
messages.ButtonRequest(code=B.ResetDevice),
messages.ButtonRequest(code=B.ResetDevice),
messages.ButtonRequest(code=B.ResetDevice),
messages.ButtonRequest(code=B.ResetDevice),
messages.ButtonRequest(code=B.Success),
messages.ButtonRequest(code=B.ResetDevice),
messages.ButtonRequest(code=B.Success),
messages.ButtonRequest(code=B.ResetDevice),
messages.ButtonRequest(code=B.Success),
messages.ButtonRequest(code=B.ResetDevice),
messages.ButtonRequest(code=B.Success),
messages.ButtonRequest(code=B.ResetDevice),
messages.ButtonRequest(code=B.Success),
messages.ButtonRequest(code=B.Success),
messages.Success,
messages.Features,
]
)
device.backup(client)
2019-09-20 16:06:02 +00:00
client.init_device()
2019-09-23 12:49:54 +00:00
assert client.features.initialized is True
2019-09-20 16:06:02 +00:00
assert client.features.needs_backup is False
2019-09-23 12:49:54 +00:00
assert client.features.unfinished_backup is False
assert client.features.no_backup is False
assert client.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)
actual_ms = shamir.combine_mnemonics(mnemonics[:3])
assert expected_ms == actual_ms
2019-09-23 12:49:54 +00:00
@pytest.mark.skip_t1
@pytest.mark.setup_client(needs_backup=True, mnemonic=MNEMONIC_SLIP39_ADVANCED_20)
2019-09-23 12:49:54 +00:00
def test_backup_slip39_advanced(client):
assert client.features.needs_backup is True
mnemonics = []
def input_flow():
# 1. Checklist
# 2. Set and confirm group count
# 3. Checklist
# 4. Set and confirm group threshold
# 5. Checklist
# 6-15: for each of 5 groups:
# 1. Set & Confirm number of shares
# 2. Set & confirm share threshold value
# 16. Confirm show seeds
yield from click_through(client.debug, screens=16, code=B.ResetDevice)
# Mnemonic phrases
for _ in range(5):
for _ in range(5):
yield # Phrase screen
mnemonic = read_and_confirm_mnemonic(client.debug, words=20)
mnemonics.append(mnemonic)
yield # Confirm continue to next
client.debug.press_yes()
# Confirm backup
yield
client.debug.press_yes()
with client:
client.set_input_flow(input_flow)
client.set_expected_responses(
[
messages.ButtonRequest(code=B.ResetDevice),
messages.ButtonRequest(code=B.ResetDevice),
messages.ButtonRequest(code=B.ResetDevice),
messages.ButtonRequest(code=B.ResetDevice),
messages.ButtonRequest(code=B.ResetDevice),
messages.ButtonRequest(code=B.ResetDevice),
messages.ButtonRequest(code=B.ResetDevice), # group #1 counts
messages.ButtonRequest(code=B.ResetDevice),
messages.ButtonRequest(code=B.ResetDevice), # group #2 counts
messages.ButtonRequest(code=B.ResetDevice),
messages.ButtonRequest(code=B.ResetDevice), # group #3 counts
messages.ButtonRequest(code=B.ResetDevice),
messages.ButtonRequest(code=B.ResetDevice), # group #4 counts
messages.ButtonRequest(code=B.ResetDevice),
messages.ButtonRequest(code=B.ResetDevice), # group #5 counts
messages.ButtonRequest(code=B.ResetDevice),
messages.ButtonRequest(code=B.ResetDevice), # show seeds
messages.ButtonRequest(code=B.Success),
messages.ButtonRequest(code=B.ResetDevice),
messages.ButtonRequest(code=B.Success),
messages.ButtonRequest(code=B.ResetDevice),
messages.ButtonRequest(code=B.Success),
messages.ButtonRequest(code=B.ResetDevice),
messages.ButtonRequest(code=B.Success),
messages.ButtonRequest(code=B.ResetDevice),
messages.ButtonRequest(code=B.Success),
messages.ButtonRequest(code=B.ResetDevice),
messages.ButtonRequest(code=B.Success),
messages.ButtonRequest(code=B.ResetDevice),
messages.ButtonRequest(code=B.Success),
messages.ButtonRequest(code=B.ResetDevice),
messages.ButtonRequest(code=B.Success),
messages.ButtonRequest(code=B.ResetDevice),
messages.ButtonRequest(code=B.Success),
messages.ButtonRequest(code=B.ResetDevice),
messages.ButtonRequest(code=B.Success),
messages.ButtonRequest(code=B.ResetDevice),
messages.ButtonRequest(code=B.Success),
messages.ButtonRequest(code=B.ResetDevice),
messages.ButtonRequest(code=B.Success),
messages.ButtonRequest(code=B.ResetDevice),
messages.ButtonRequest(code=B.Success),
messages.ButtonRequest(code=B.ResetDevice),
messages.ButtonRequest(code=B.Success),
messages.ButtonRequest(code=B.ResetDevice),
messages.ButtonRequest(code=B.Success),
messages.ButtonRequest(code=B.ResetDevice),
messages.ButtonRequest(code=B.Success),
messages.ButtonRequest(code=B.ResetDevice),
messages.ButtonRequest(code=B.Success),
messages.ButtonRequest(code=B.ResetDevice),
messages.ButtonRequest(code=B.Success),
messages.ButtonRequest(code=B.ResetDevice),
messages.ButtonRequest(code=B.Success),
messages.ButtonRequest(code=B.ResetDevice),
messages.ButtonRequest(code=B.Success),
messages.ButtonRequest(code=B.ResetDevice),
messages.ButtonRequest(code=B.Success),
messages.ButtonRequest(code=B.ResetDevice),
messages.ButtonRequest(code=B.Success),
messages.ButtonRequest(code=B.ResetDevice),
messages.ButtonRequest(code=B.Success),
messages.ButtonRequest(code=B.ResetDevice),
messages.ButtonRequest(code=B.Success),
messages.ButtonRequest(code=B.ResetDevice),
messages.ButtonRequest(code=B.Success), # show seeds ends here
messages.ButtonRequest(code=B.Success),
messages.Success,
messages.Features,
2019-09-23 12:49:54 +00:00
]
)
device.backup(client)
client.init_device()
assert client.features.initialized is True
assert client.features.needs_backup is False
assert client.features.unfinished_backup is False
assert client.features.no_backup is False
assert client.features.backup_type is messages.BackupType.Slip39_Advanced
expected_ms = shamir.combine_mnemonics(MNEMONIC_SLIP39_ADVANCED_20)
actual_ms = shamir.combine_mnemonics(
mnemonics[:3] + mnemonics[5:8] + mnemonics[10:13]
)
assert expected_ms == actual_ms
# we only test this with bip39 because the code path is always the same
@pytest.mark.setup_client(no_backup=True)
2019-09-23 12:49:54 +00:00
def test_no_backup_fails(client):
client.ensure_unlocked()
2019-09-23 12:49:54 +00:00
assert client.features.initialized is True
assert client.features.no_backup is True
assert client.features.needs_backup is False
# 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"):
2019-09-23 12:49:54 +00:00
device.backup(client)
# we only test this with bip39 because the code path is always the same
@pytest.mark.setup_client(needs_backup=True)
2019-10-01 10:04:55 +00:00
def test_interrupt_backup_fails(client):
client.ensure_unlocked()
2019-09-23 12:49:54 +00:00
assert client.features.initialized is True
assert client.features.needs_backup is True
assert client.features.unfinished_backup is False
assert client.features.no_backup is False
# start backup
client.call_raw(messages.BackupDevice())
# interupt backup by sending initialize
client.init_device()
# check that device state is as expected
assert client.features.initialized is True
assert client.features.needs_backup is False
assert client.features.unfinished_backup is True
assert client.features.no_backup is False
# Second attempt at backup should fail
2019-10-01 10:04:55 +00:00
with pytest.raises(TrezorFailure, match=r".*Seed already backed up"):
2019-09-23 12:49:54 +00:00
device.backup(client)
# we only test this with bip39 because the code path is always the same
@pytest.mark.setup_client(uninitialized=True)
def test_no_backup_show_entropy_fails(client):
with pytest.raises(
2019-10-01 10:04:55 +00:00
TrezorFailure, match=r".*Can't show internal entropy when backup is skipped"
2019-09-23 12:49:54 +00:00
):
device.reset(
client,
display_random=True,
strength=128,
passphrase_protection=False,
pin_protection=False,
label="test",
language="en-US",
2019-09-23 12:49:54 +00:00
no_backup=True,
)