2020-02-11 10:33:24 +00:00
|
|
|
from trezorlib import debuglink, device, messages
|
2022-01-31 12:25:30 +00:00
|
|
|
from trezorlib.debuglink import TrezorClientDebugLink as Client
|
2023-11-21 15:55:46 +00:00
|
|
|
from trezorlib.debuglink import message_filters
|
2020-02-11 10:33:24 +00:00
|
|
|
|
|
|
|
from ..common import MNEMONIC12
|
2023-03-30 10:31:09 +00:00
|
|
|
from ..emulators import Emulator, EmulatorWrapper
|
2020-02-11 10:33:24 +00:00
|
|
|
from ..upgrade_tests import core_only, legacy_only
|
|
|
|
|
|
|
|
PIN = "1234"
|
|
|
|
WIPE_CODE = "9876"
|
|
|
|
|
|
|
|
|
2022-01-31 12:25:30 +00:00
|
|
|
def setup_device_legacy(client: Client, pin: str, wipe_code: str) -> None:
|
2024-11-26 10:47:13 +00:00
|
|
|
device.wipe(client.get_management_session())
|
|
|
|
client = client.get_new_client()
|
2020-02-11 10:33:24 +00:00
|
|
|
debuglink.load_device(
|
2024-11-26 10:47:13 +00:00
|
|
|
client.get_management_session(),
|
|
|
|
MNEMONIC12,
|
|
|
|
pin,
|
|
|
|
passphrase_protection=False,
|
|
|
|
label="WIPECODE",
|
2020-02-11 10:33:24 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
with client:
|
2020-02-12 14:38:18 +00:00
|
|
|
client.use_pin_sequence([PIN, WIPE_CODE, WIPE_CODE])
|
2024-11-26 10:47:13 +00:00
|
|
|
device.change_wipe_code(client.get_management_session())
|
2020-02-11 10:33:24 +00:00
|
|
|
|
|
|
|
|
2022-01-31 12:25:30 +00:00
|
|
|
def setup_device_core(client: Client, pin: str, wipe_code: str) -> None:
|
2024-11-26 10:47:13 +00:00
|
|
|
device.wipe(client.get_management_session())
|
|
|
|
client = client.get_new_client()
|
2020-02-11 10:33:24 +00:00
|
|
|
debuglink.load_device(
|
2024-11-26 10:47:13 +00:00
|
|
|
client.get_management_session(),
|
|
|
|
MNEMONIC12,
|
|
|
|
pin,
|
|
|
|
passphrase_protection=False,
|
|
|
|
label="WIPECODE",
|
2020-02-11 10:33:24 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
with client:
|
2024-10-10 13:43:26 +00:00
|
|
|
client.use_pin_sequence([pin, wipe_code, wipe_code])
|
2024-11-26 10:47:13 +00:00
|
|
|
device.change_wipe_code(client.get_management_session())
|
2020-02-11 10:33:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
@core_only
|
2023-03-30 10:31:09 +00:00
|
|
|
def test_wipe_code_activate_core(core_emulator: Emulator):
|
|
|
|
# set up device
|
|
|
|
setup_device_core(core_emulator.client, PIN, WIPE_CODE)
|
2024-11-26 10:47:13 +00:00
|
|
|
session = core_emulator.client.get_session()
|
2023-03-30 10:31:09 +00:00
|
|
|
device_id = core_emulator.client.features.device_id
|
|
|
|
|
|
|
|
# Initiate Change pin process
|
2024-11-26 10:47:13 +00:00
|
|
|
ret = session.call_raw(messages.ChangePin(remove=False))
|
2023-03-30 10:31:09 +00:00
|
|
|
assert isinstance(ret, messages.ButtonRequest)
|
2024-10-10 13:43:26 +00:00
|
|
|
assert ret.name == "change_pin"
|
2023-03-30 10:31:09 +00:00
|
|
|
core_emulator.client.debug.press_yes()
|
2024-11-26 10:47:13 +00:00
|
|
|
ret = session.call_raw(messages.ButtonAck())
|
2023-03-30 10:31:09 +00:00
|
|
|
|
|
|
|
# Enter the wipe code instead of the current PIN
|
2023-11-21 15:55:46 +00:00
|
|
|
expected = message_filters.ButtonRequest(code=messages.ButtonRequestType.PinEntry)
|
|
|
|
assert expected.match(ret)
|
2024-11-26 10:47:13 +00:00
|
|
|
session._write(messages.ButtonAck())
|
2023-03-30 10:31:09 +00:00
|
|
|
core_emulator.client.debug.input(WIPE_CODE)
|
|
|
|
|
|
|
|
# preserving screenshots even after it dies and starts again
|
|
|
|
prev_screenshot_dir = core_emulator.client.debug.screenshot_recording_dir
|
|
|
|
|
|
|
|
# wait 30 seconds for emulator to shut down
|
|
|
|
# this will raise a TimeoutError if the emulator doesn't die.
|
|
|
|
core_emulator.wait(30)
|
|
|
|
|
|
|
|
core_emulator.start()
|
|
|
|
if prev_screenshot_dir:
|
|
|
|
core_emulator.client.debug.start_recording(prev_screenshot_dir, refresh_index=1)
|
|
|
|
assert core_emulator.client.features.initialized is False
|
|
|
|
assert core_emulator.client.features.pin_protection is False
|
|
|
|
assert core_emulator.client.features.wipe_code_protection is False
|
|
|
|
assert core_emulator.client.features.device_id != device_id
|
2020-02-11 10:33:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
@legacy_only
|
|
|
|
def test_wipe_code_activate_legacy():
|
|
|
|
with EmulatorWrapper("legacy") as emu:
|
|
|
|
# set up device
|
|
|
|
setup_device_legacy(emu.client, PIN, WIPE_CODE)
|
|
|
|
|
2024-11-26 10:47:13 +00:00
|
|
|
session = emu.client.get_session()
|
2020-02-11 10:33:24 +00:00
|
|
|
device_id = emu.client.features.device_id
|
|
|
|
|
|
|
|
# Initiate Change pin process
|
2024-11-26 10:47:13 +00:00
|
|
|
ret = session.call_raw(messages.ChangePin(remove=False))
|
2020-02-11 10:33:24 +00:00
|
|
|
assert isinstance(ret, messages.ButtonRequest)
|
|
|
|
emu.client.debug.press_yes()
|
2024-11-26 10:47:13 +00:00
|
|
|
ret = session.call_raw(messages.ButtonAck())
|
2020-02-11 10:33:24 +00:00
|
|
|
|
|
|
|
# Enter the wipe code instead of the current PIN
|
|
|
|
assert isinstance(ret, messages.PinMatrixRequest)
|
|
|
|
wipe_code_encoded = emu.client.debug.encode_pin(WIPE_CODE)
|
2024-11-26 10:47:13 +00:00
|
|
|
session._write(messages.PinMatrixAck(pin=wipe_code_encoded))
|
2020-02-11 10:33:24 +00:00
|
|
|
|
|
|
|
# wait 30 seconds for emulator to shut down
|
|
|
|
# this will raise a TimeoutError if the emulator doesn't die.
|
|
|
|
emu.wait(30)
|
|
|
|
|
|
|
|
emu.start()
|
2024-11-26 10:47:13 +00:00
|
|
|
emu.client.refresh_features()
|
2020-02-11 10:33:24 +00:00
|
|
|
assert emu.client.features.initialized is False
|
|
|
|
assert emu.client.features.pin_protection is False
|
|
|
|
assert emu.client.features.wipe_code_protection is False
|
|
|
|
assert emu.client.features.device_id != device_id
|