2019-09-10 09:53:31 +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>.
|
|
|
|
|
2019-08-16 13:29:21 +00:00
|
|
|
import pytest
|
|
|
|
|
2019-11-13 16:47:03 +00:00
|
|
|
from trezorlib import MINIMUM_FIRMWARE_VERSION, btc, debuglink, device, fido
|
2019-10-25 14:37:35 +00:00
|
|
|
from trezorlib.messages import BackupType
|
2019-08-16 13:29:21 +00:00
|
|
|
from trezorlib.tools import H_
|
|
|
|
|
2019-10-25 14:37:35 +00:00
|
|
|
from ..click_tests import recovery
|
|
|
|
from ..common import MNEMONIC_SLIP39_BASIC_20_3of6, MNEMONIC_SLIP39_BASIC_20_3of6_SECRET
|
|
|
|
from ..device_handler import BackgroundDeviceHandler
|
2019-09-12 10:24:59 +00:00
|
|
|
from ..emulators import ALL_TAGS, EmulatorWrapper
|
2019-10-21 14:49:34 +00:00
|
|
|
from . import SELECTED_GENS
|
2019-09-12 10:24:59 +00:00
|
|
|
|
2019-08-16 13:29:21 +00:00
|
|
|
MINIMUM_FIRMWARE_VERSION["1"] = (1, 0, 0)
|
|
|
|
MINIMUM_FIRMWARE_VERSION["T"] = (2, 0, 0)
|
|
|
|
|
|
|
|
|
|
|
|
# **** COMMON DEFINITIONS ****
|
|
|
|
|
|
|
|
MNEMONIC = " ".join(["all"] * 12)
|
|
|
|
PATH = [H_(44), H_(0), H_(0), 0, 0]
|
|
|
|
ADDRESS = "1JAd7XCBzGudGpJQSDSfpmJhiygtLQWaGL"
|
|
|
|
LABEL = "test"
|
2019-12-07 11:11:51 +00:00
|
|
|
LANGUAGE = "en-US"
|
2019-08-16 13:29:21 +00:00
|
|
|
STRENGTH = 128
|
|
|
|
|
|
|
|
|
2019-11-12 15:30:21 +00:00
|
|
|
def for_all(*args, legacy_minimum_version=(1, 0, 0), core_minimum_version=(2, 0, 0)):
|
2019-08-16 13:29:21 +00:00
|
|
|
if not args:
|
|
|
|
args = ("core", "legacy")
|
|
|
|
|
2019-10-21 14:49:34 +00:00
|
|
|
# If any gens were selected, use them. If none, select all.
|
|
|
|
enabled_gens = SELECTED_GENS or args
|
2019-09-09 14:03:32 +00:00
|
|
|
|
2019-08-16 13:29:21 +00:00
|
|
|
all_params = []
|
|
|
|
for gen in args:
|
2019-11-12 15:30:21 +00:00
|
|
|
if gen == "legacy":
|
|
|
|
minimum_version = legacy_minimum_version
|
|
|
|
elif gen == "core":
|
|
|
|
minimum_version = core_minimum_version
|
|
|
|
else:
|
|
|
|
raise ValueError
|
|
|
|
|
2019-09-09 14:03:32 +00:00
|
|
|
if gen not in enabled_gens:
|
|
|
|
continue
|
2019-08-16 13:29:21 +00:00
|
|
|
try:
|
2019-09-12 10:24:59 +00:00
|
|
|
to_tag = None
|
2019-08-16 13:29:21 +00:00
|
|
|
from_tags = ALL_TAGS[gen] + [to_tag]
|
|
|
|
for from_tag in from_tags:
|
2019-09-12 10:24:59 +00:00
|
|
|
if from_tag is not None and from_tag.startswith("v"):
|
2019-08-16 13:29:21 +00:00
|
|
|
tag_version = tuple(int(n) for n in from_tag[1:].split("."))
|
|
|
|
if tag_version < minimum_version:
|
|
|
|
continue
|
|
|
|
all_params.append((gen, from_tag, to_tag))
|
|
|
|
except KeyError:
|
|
|
|
pass
|
|
|
|
|
2019-09-12 10:23:19 +00:00
|
|
|
if not all_params:
|
|
|
|
return pytest.mark.skip("no versions are applicable")
|
|
|
|
|
2019-08-16 13:29:21 +00:00
|
|
|
return pytest.mark.parametrize("gen, from_tag, to_tag", all_params)
|
|
|
|
|
|
|
|
|
|
|
|
@for_all()
|
|
|
|
def test_upgrade_load(gen, from_tag, to_tag):
|
|
|
|
def asserts(tag, client):
|
|
|
|
assert not client.features.pin_protection
|
|
|
|
assert not client.features.passphrase_protection
|
|
|
|
assert client.features.initialized
|
|
|
|
assert client.features.label == LABEL
|
|
|
|
assert btc.get_address(client, "Bitcoin", PATH) == ADDRESS
|
|
|
|
|
|
|
|
with EmulatorWrapper(gen, from_tag) as emu:
|
|
|
|
debuglink.load_device_by_mnemonic(
|
|
|
|
emu.client,
|
|
|
|
mnemonic=MNEMONIC,
|
|
|
|
pin="",
|
|
|
|
passphrase_protection=False,
|
|
|
|
label=LABEL,
|
|
|
|
language=LANGUAGE,
|
|
|
|
)
|
|
|
|
device_id = emu.client.features.device_id
|
|
|
|
asserts(from_tag, emu.client)
|
|
|
|
storage = emu.storage()
|
|
|
|
|
|
|
|
with EmulatorWrapper(gen, to_tag, storage=storage) as emu:
|
|
|
|
assert device_id == emu.client.features.device_id
|
|
|
|
asserts(to_tag, emu.client)
|
2019-12-07 11:11:51 +00:00
|
|
|
assert emu.client.features.language == LANGUAGE
|
2019-08-16 13:29:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
@for_all("legacy")
|
|
|
|
def test_upgrade_reset(gen, from_tag, to_tag):
|
|
|
|
def asserts(tag, client):
|
|
|
|
assert not client.features.pin_protection
|
|
|
|
assert not client.features.passphrase_protection
|
|
|
|
assert client.features.initialized
|
|
|
|
assert client.features.label == LABEL
|
|
|
|
assert not client.features.needs_backup
|
|
|
|
assert not client.features.unfinished_backup
|
|
|
|
assert not client.features.no_backup
|
|
|
|
|
|
|
|
with EmulatorWrapper(gen, from_tag) as emu:
|
|
|
|
device.reset(
|
|
|
|
emu.client,
|
|
|
|
display_random=False,
|
|
|
|
strength=STRENGTH,
|
|
|
|
passphrase_protection=False,
|
|
|
|
pin_protection=False,
|
|
|
|
label=LABEL,
|
|
|
|
language=LANGUAGE,
|
|
|
|
)
|
|
|
|
device_id = emu.client.features.device_id
|
|
|
|
asserts(from_tag, emu.client)
|
2019-09-12 10:23:19 +00:00
|
|
|
address = btc.get_address(emu.client, "Bitcoin", PATH)
|
2019-08-16 13:29:21 +00:00
|
|
|
storage = emu.storage()
|
|
|
|
|
|
|
|
with EmulatorWrapper(gen, to_tag, storage=storage) as emu:
|
|
|
|
assert device_id == emu.client.features.device_id
|
|
|
|
asserts(to_tag, emu.client)
|
2019-12-07 11:11:51 +00:00
|
|
|
assert emu.client.features.language == LANGUAGE
|
2019-09-12 10:23:19 +00:00
|
|
|
assert btc.get_address(emu.client, "Bitcoin", PATH) == address
|
2019-08-16 13:29:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
@for_all()
|
|
|
|
def test_upgrade_reset_skip_backup(gen, from_tag, to_tag):
|
|
|
|
def asserts(tag, client):
|
|
|
|
assert not client.features.pin_protection
|
|
|
|
assert not client.features.passphrase_protection
|
|
|
|
assert client.features.initialized
|
|
|
|
assert client.features.label == LABEL
|
|
|
|
assert client.features.needs_backup
|
|
|
|
assert not client.features.unfinished_backup
|
|
|
|
assert not client.features.no_backup
|
|
|
|
|
|
|
|
with EmulatorWrapper(gen, from_tag) as emu:
|
|
|
|
device.reset(
|
|
|
|
emu.client,
|
|
|
|
display_random=False,
|
|
|
|
strength=STRENGTH,
|
|
|
|
passphrase_protection=False,
|
|
|
|
pin_protection=False,
|
|
|
|
label=LABEL,
|
|
|
|
language=LANGUAGE,
|
|
|
|
skip_backup=True,
|
|
|
|
)
|
|
|
|
device_id = emu.client.features.device_id
|
|
|
|
asserts(from_tag, emu.client)
|
2019-09-12 10:23:19 +00:00
|
|
|
address = btc.get_address(emu.client, "Bitcoin", PATH)
|
2019-08-16 13:29:21 +00:00
|
|
|
storage = emu.storage()
|
|
|
|
|
|
|
|
with EmulatorWrapper(gen, to_tag, storage=storage) as emu:
|
|
|
|
assert device_id == emu.client.features.device_id
|
|
|
|
asserts(to_tag, emu.client)
|
2019-12-07 11:11:51 +00:00
|
|
|
assert emu.client.features.language == LANGUAGE
|
2019-09-12 10:23:19 +00:00
|
|
|
assert btc.get_address(emu.client, "Bitcoin", PATH) == address
|
2019-08-16 13:29:21 +00:00
|
|
|
|
|
|
|
|
2019-11-12 15:30:21 +00:00
|
|
|
@for_all(legacy_minimum_version=(1, 7, 2))
|
2019-08-16 13:29:21 +00:00
|
|
|
def test_upgrade_reset_no_backup(gen, from_tag, to_tag):
|
|
|
|
def asserts(tag, client):
|
|
|
|
assert not client.features.pin_protection
|
|
|
|
assert not client.features.passphrase_protection
|
|
|
|
assert client.features.initialized
|
|
|
|
assert client.features.label == LABEL
|
|
|
|
assert not client.features.needs_backup
|
|
|
|
assert not client.features.unfinished_backup
|
|
|
|
assert client.features.no_backup
|
|
|
|
|
|
|
|
with EmulatorWrapper(gen, from_tag) as emu:
|
|
|
|
device.reset(
|
|
|
|
emu.client,
|
|
|
|
display_random=False,
|
|
|
|
strength=STRENGTH,
|
|
|
|
passphrase_protection=False,
|
|
|
|
pin_protection=False,
|
|
|
|
label=LABEL,
|
|
|
|
language=LANGUAGE,
|
|
|
|
no_backup=True,
|
|
|
|
)
|
|
|
|
device_id = emu.client.features.device_id
|
|
|
|
asserts(from_tag, emu.client)
|
2019-09-12 10:23:19 +00:00
|
|
|
address = btc.get_address(emu.client, "Bitcoin", PATH)
|
2019-08-16 13:29:21 +00:00
|
|
|
storage = emu.storage()
|
|
|
|
|
|
|
|
with EmulatorWrapper(gen, to_tag, storage=storage) as emu:
|
|
|
|
assert device_id == emu.client.features.device_id
|
|
|
|
asserts(to_tag, emu.client)
|
2019-12-07 11:11:51 +00:00
|
|
|
assert emu.client.features.language == LANGUAGE
|
2019-09-12 10:23:19 +00:00
|
|
|
assert btc.get_address(emu.client, "Bitcoin", PATH) == address
|
2019-08-16 13:29:21 +00:00
|
|
|
|
|
|
|
|
2019-10-25 14:37:35 +00:00
|
|
|
# Although Shamir was introduced in 2.1.2 already, the debug instrumentation was not present until 2.1.9.
|
2019-11-12 15:30:21 +00:00
|
|
|
@for_all("core", core_minimum_version=(2, 1, 9))
|
2019-10-25 14:37:35 +00:00
|
|
|
def test_upgrade_shamir_recovery(gen, from_tag, to_tag):
|
|
|
|
with EmulatorWrapper(gen, from_tag) as emu, BackgroundDeviceHandler(
|
|
|
|
emu.client
|
|
|
|
) as device_handler:
|
|
|
|
assert emu.client.features.recovery_mode is False
|
|
|
|
debug = device_handler.debuglink()
|
|
|
|
|
|
|
|
device_handler.run(device.recover, pin_protection=False)
|
|
|
|
|
2019-11-11 10:07:23 +00:00
|
|
|
recovery.confirm_recovery(debug)
|
2019-10-25 14:37:35 +00:00
|
|
|
recovery.select_number_of_words(debug)
|
|
|
|
layout = recovery.enter_share(debug, MNEMONIC_SLIP39_BASIC_20_3of6[0])
|
|
|
|
assert "2 more shares" in layout.text
|
|
|
|
|
|
|
|
device_id = emu.client.features.device_id
|
|
|
|
storage = emu.storage()
|
|
|
|
device_handler.check_finalize()
|
|
|
|
|
|
|
|
with EmulatorWrapper(gen, to_tag, storage=storage) as emu, BackgroundDeviceHandler(
|
|
|
|
emu.client
|
|
|
|
) as device_handler:
|
|
|
|
assert device_id == emu.client.features.device_id
|
|
|
|
assert emu.client.features.recovery_mode
|
|
|
|
debug = device_handler.debuglink()
|
|
|
|
|
|
|
|
# second share
|
|
|
|
layout = recovery.enter_share(debug, MNEMONIC_SLIP39_BASIC_20_3of6[2])
|
|
|
|
assert "1 more share" in layout.text
|
|
|
|
|
|
|
|
# last one
|
|
|
|
layout = recovery.enter_share(debug, MNEMONIC_SLIP39_BASIC_20_3of6[1])
|
|
|
|
assert "You have successfully" in layout.text
|
|
|
|
|
|
|
|
# Check the result
|
|
|
|
state = debug.state()
|
|
|
|
assert state.mnemonic_secret.hex() == MNEMONIC_SLIP39_BASIC_20_3of6_SECRET
|
|
|
|
assert state.mnemonic_type == BackupType.Slip39_Basic
|
|
|
|
device_handler.check_finalize()
|
|
|
|
|
|
|
|
|
2019-11-12 15:30:21 +00:00
|
|
|
@for_all(legacy_minimum_version=(1, 8, 4), core_minimum_version=(2, 1, 9))
|
|
|
|
def test_upgrade_u2f(gen, from_tag, to_tag):
|
|
|
|
"""
|
|
|
|
Check U2F counter stayed the same after an upgrade.
|
|
|
|
"""
|
|
|
|
with EmulatorWrapper(gen, from_tag) as emu:
|
2019-11-13 16:47:03 +00:00
|
|
|
success = fido.set_counter(emu.client, 10)
|
2019-11-12 15:30:21 +00:00
|
|
|
assert "U2F counter set" in success
|
|
|
|
|
2019-11-13 16:47:03 +00:00
|
|
|
counter = fido.get_next_counter(emu.client)
|
2019-11-12 15:30:21 +00:00
|
|
|
assert counter == 11
|
|
|
|
storage = emu.storage()
|
|
|
|
|
|
|
|
with EmulatorWrapper(gen, to_tag, storage=storage) as emu:
|
2019-11-13 16:47:03 +00:00
|
|
|
counter = fido.get_next_counter(emu.client)
|
2019-11-12 15:30:21 +00:00
|
|
|
assert counter == 12
|
|
|
|
|
|
|
|
|
2019-08-16 13:29:21 +00:00
|
|
|
if __name__ == "__main__":
|
|
|
|
if not ALL_TAGS:
|
|
|
|
print("No versions found. Remember to run download_emulators.sh")
|
|
|
|
for k, v in ALL_TAGS.items():
|
|
|
|
print("Found versions for {}:".format(k), v)
|
|
|
|
print()
|
|
|
|
print("Use `pytest {}` to run tests".format(__file__))
|