tests: add test for TrezorClient.ensure_unlocked()
What changed, and why it matters
This commit only adds and updates automated tests for the Trezor hardware wallet's unlock behavior. It does not change any production firmware, device logic, or security-sensitive code. There is no security issue here.
No action needed. This is a routine test-only change.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The diff modifies tests/device_tests/test_protection_levels.py. It refactors the _assert_protection helper to use a newer test context API and adds a parametrized test (test_ensure_unlocked) that verifies TrezorClient.ensure_unlocked() correctly unlocks a PIN-protected device and toggles passphrase settings. No runtime firmware or library code is changed.
Changed components
tests/device_tests/test_protection_levels.pyInspect captured patch +27 / −15
diff --git a/tests/device_tests/test_protection_levels.py b/tests/device_tests/test_protection_levels.py
index da07cfe7..d5bdf1f1 100644
--- a/tests/device_tests/test_protection_levels.py
+++ b/tests/device_tests/test_protection_levels.py
@@ -97,22 +97,14 @@ def _get_test_address(session: Session) -> None:
)
-def _assert_protection(client: Client, pin: bool = True, passphrase: bool = True):
+def _assert_protection(test_ctx: Client, pin: bool = True, passphrase: bool = True):
"""Make sure PIN and passphrase protection have expected values"""
- with client:
- client.use_pin_sequence([PIN4])
- session = client.get_session()
- try:
- session.ensure_unlocked()
- except exceptions.InvalidSessionError:
- session.cancel()
- session.read()
-
- client.refresh_features()
- assert client.features.pin_protection is pin
- assert client.features.passphrase_protection is passphrase
- session.lock()
- session.close()
+ with test_ctx:
+ test_ctx.use_pin_sequence([PIN4])
+ test_ctx.client.ensure_unlocked()
+ assert test_ctx.features.pin_protection is pin
+ assert test_ctx.features.passphrase_protection is passphrase
+ test_ctx.lock()
def test_initialize(client: Client):
@@ -501,3 +493,23 @@ def test_unlocked(client: Client):
with client:
client.set_expected_responses([messages.Address])
_get_test_address(session)
+
+
+@pytest.mark.parametrize("passphrase", (True, False))
+def test_ensure_unlocked(test_ctx: Client, passphrase: bool):
+ assert test_ctx.features.pin_protection is True
+
+ with test_ctx:
+ test_ctx.use_pin_sequence([PIN4])
+ session = test_ctx.get_session()
+ device.apply_settings(session, use_passphrase=passphrase)
+
+ _assert_protection(test_ctx, passphrase=passphrase)
+
+ test_ctx.lock()
+ with test_ctx:
+ test_ctx.use_pin_sequence([PIN4])
+
+ assert test_ctx.features.unlocked is False
+ test_ctx.client.ensure_unlocked()
+ assert test_ctx.features.unlocked is True
Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.