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/test_busy_state.py

116 lines
3.5 KiB
Python
Raw Normal View History

2022-08-11 09:47:30 +00:00
# This file is part of the Trezor project.
#
# Copyright (C) 2022 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 time
import pytest
from trezorlib import btc, device
from trezorlib.debuglink import LayoutType
2024-11-15 16:31:22 +00:00
from trezorlib.debuglink import SessionDebugWrapper as Session
2022-08-11 09:47:30 +00:00
from trezorlib.tools import parse_path
PIN = "1234"
2024-11-15 16:31:22 +00:00
def _assert_busy(session: Session, should_be_busy: bool, screen: str = "Homescreen"):
assert session.features.busy is should_be_busy
if session.client.layout_type is not LayoutType.T1:
if should_be_busy:
2024-11-15 16:31:22 +00:00
assert (
"CoinJoinProgress"
in session.client.debug.read_layout().all_components()
)
else:
2024-11-15 16:31:22 +00:00
assert session.client.debug.read_layout().main_component() == screen
2022-08-11 09:47:30 +00:00
@pytest.mark.setup_client(pin=PIN)
2024-11-15 16:31:22 +00:00
def test_busy_state(session: Session):
session.lock()
_assert_busy(session, False, "Lockscreen")
assert session.features.unlocked is False
2022-08-11 09:47:30 +00:00
# Show busy dialog for 1 minute.
2024-11-15 16:31:22 +00:00
device.set_busy(session, expiry_ms=60 * 1000)
_assert_busy(session, True)
assert session.features.unlocked is False
2022-08-11 09:47:30 +00:00
2024-11-15 16:31:22 +00:00
with session.client as client:
2022-08-11 09:47:30 +00:00
client.use_pin_sequence([PIN])
btc.get_address(
2024-11-15 16:31:22 +00:00
session, "Bitcoin", parse_path("m/44h/0h/0h/0/0"), show_display=True
2022-08-11 09:47:30 +00:00
)
2024-11-15 16:31:22 +00:00
session.refresh_features()
_assert_busy(session, True)
assert session.features.unlocked is True
2022-08-11 09:47:30 +00:00
# Hide the busy dialog.
2024-11-15 16:31:22 +00:00
device.set_busy(session, None)
2022-08-11 09:47:30 +00:00
2024-11-15 16:31:22 +00:00
_assert_busy(session, False)
assert session.features.unlocked is True
2022-08-11 09:47:30 +00:00
@pytest.mark.models("core")
2024-11-15 16:31:22 +00:00
def test_busy_expiry_core(session: Session):
session.lock()
2023-11-21 12:51:59 +00:00
WAIT_TIME_MS = 1500
TOLERANCE = 1000
2024-11-15 16:31:22 +00:00
_assert_busy(session, False)
2023-11-21 12:51:59 +00:00
# Start a timer
start = time.monotonic()
2022-08-11 09:47:30 +00:00
# Show the busy dialog.
2024-11-15 16:31:22 +00:00
device.set_busy(session, expiry_ms=WAIT_TIME_MS)
_assert_busy(session, True)
2023-11-21 12:51:59 +00:00
# Wait until the layout changes
2024-11-15 16:31:22 +00:00
time.sleep(0.1) # Improves stability of the test for devices with THP
session.client.debug.wait_layout()
2023-11-21 12:51:59 +00:00
end = time.monotonic()
2022-08-11 09:47:30 +00:00
2023-11-21 12:51:59 +00:00
# Check that the busy dialog was shown for at least WAIT_TIME_MS.
duration = (end - start) * 1000
assert WAIT_TIME_MS <= duration <= WAIT_TIME_MS + TOLERANCE
2022-08-11 09:47:30 +00:00
# Check that the device is no longer busy.
# Also needs to come back to Homescreen (for UI tests).
2024-11-15 16:31:22 +00:00
session.refresh_features()
_assert_busy(session, False)
@pytest.mark.flaky(max_runs=5)
@pytest.mark.models("legacy")
2024-11-15 16:31:22 +00:00
def test_busy_expiry_legacy(session: Session):
_assert_busy(session, False)
# Show the busy dialog.
2024-11-15 16:31:22 +00:00
device.set_busy(session, expiry_ms=1500)
_assert_busy(session, True)
# Hasn't expired yet.
time.sleep(0.1)
2024-11-15 16:31:22 +00:00
_assert_busy(session, True)
# Wait for it to expire. Add some tolerance to account for CI/hardware slowness.
time.sleep(4.0)
# Check that the device is no longer busy.
# Also needs to come back to Homescreen (for UI tests).
2024-11-15 16:31:22 +00:00
session.refresh_features()
_assert_busy(session, False)