From 987b92f8885d2ac67326f3d339882c20bf97d6d6 Mon Sep 17 00:00:00 2001 From: M1nd3r Date: Wed, 27 Nov 2024 15:53:31 +0100 Subject: [PATCH] test: allow session to be locked by default [no changelog] --- python/src/trezorlib/client.py | 1 + tests/conftest.py | 7 ++++++- .../bitcoin/test_authorize_coinjoin.py | 2 +- tests/device_tests/test_autolock.py | 9 --------- tests/device_tests/test_busy_state.py | 2 -- tests/device_tests/test_msg_applysettings.py | 4 ---- .../test_msg_change_wipe_code_t2.py | 2 +- tests/device_tests/test_pin.py | 3 --- tests/device_tests/test_protection_levels.py | 1 - tests/device_tests/test_sdcard.py | 17 +++++++++-------- 10 files changed, 18 insertions(+), 30 deletions(-) diff --git a/python/src/trezorlib/client.py b/python/src/trezorlib/client.py index 8d81b6715e..a80c44f6a6 100644 --- a/python/src/trezorlib/client.py +++ b/python/src/trezorlib/client.py @@ -61,6 +61,7 @@ class TrezorClient: _management_session: Session | None = None _features: messages.Features | None = None _protocol_version: int + _has_setup_pin: bool = False # Should by used only by conftest def __init__( self, diff --git a/tests/conftest.py b/tests/conftest.py index 9dca5c8b65..ecfeb4c5d8 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -368,6 +368,8 @@ def client( needs_backup=setup_params["needs_backup"], # type: ignore no_backup=setup_params["no_backup"], # type: ignore ) + if setup_params["pin"] is not None: + _raw_client._has_setup_pin = True if request.node.get_closest_marker("experimental"): apply_settings(session, experimental_features=True) @@ -396,7 +398,10 @@ def session( derive_cardano=derive_cardano, passphrase=passphrase ) try: - yield SessionDebugWrapper(session) + wrapped_session = SessionDebugWrapper(session) + if client._has_setup_pin: + wrapped_session.lock() + yield wrapped_session finally: pass # TODO diff --git a/tests/device_tests/bitcoin/test_authorize_coinjoin.py b/tests/device_tests/bitcoin/test_authorize_coinjoin.py index ff70180eeb..2ee16a074c 100644 --- a/tests/device_tests/bitcoin/test_authorize_coinjoin.py +++ b/tests/device_tests/bitcoin/test_authorize_coinjoin.py @@ -62,7 +62,7 @@ SLIP25_PATH = parse_path("m/10025h") @pytest.mark.setup_client(pin=PIN) def test_sign_tx(session: Session, chunkify: bool): # NOTE: FAKE input tx - + assert session.features.unlocked is False commitment_data = b"\x0fwww.example.com" + (1).to_bytes(ROUND_ID_LEN, "big") with session.client as client: diff --git a/tests/device_tests/test_autolock.py b/tests/device_tests/test_autolock.py index 2551a65d1f..a310ff3841 100644 --- a/tests/device_tests/test_autolock.py +++ b/tests/device_tests/test_autolock.py @@ -52,7 +52,6 @@ def set_autolock_delay(session: Session, delay): def test_apply_auto_lock_delay(session: Session): - session.lock() set_autolock_delay(session, 10 * 1000) time.sleep(0.1) # sleep less than auto-lock delay @@ -80,14 +79,11 @@ def test_apply_auto_lock_delay(session: Session): ], ) def test_apply_auto_lock_delay_valid(session: Session, seconds): - session.lock() set_autolock_delay(session, seconds * 1000) assert session.features.auto_lock_delay_ms == seconds * 1000 def test_autolock_default_value(session: Session): - session.lock() - assert session.features.auto_lock_delay_ms is None with session, session.client as client: client.use_pin_sequence([PIN4]) @@ -101,7 +97,6 @@ def test_autolock_default_value(session: Session): [0, 1, 9, 536871, 2**22], ) def test_apply_auto_lock_delay_out_of_range(session: Session, seconds): - session.lock() with session, session.client as client: client.use_pin_sequence([PIN4]) @@ -119,8 +114,6 @@ def test_apply_auto_lock_delay_out_of_range(session: Session, seconds): @pytest.mark.models("core") def test_autolock_cancels_ui(session: Session): - session.lock() - set_autolock_delay(session, 10 * 1000) resp = session.call_raw( @@ -144,7 +137,6 @@ def test_autolock_cancels_ui(session: Session): def test_autolock_ignores_initialize(session: Session): - session.lock() client = session.client set_autolock_delay(session, 10 * 1000) @@ -162,7 +154,6 @@ def test_autolock_ignores_initialize(session: Session): def test_autolock_ignores_getaddress(session: Session): - session.lock() set_autolock_delay(session, 10 * 1000) diff --git a/tests/device_tests/test_busy_state.py b/tests/device_tests/test_busy_state.py index cb22167547..27fb1b23e6 100644 --- a/tests/device_tests/test_busy_state.py +++ b/tests/device_tests/test_busy_state.py @@ -40,7 +40,6 @@ def _assert_busy(session: Session, should_be_busy: bool, screen: str = "Homescre @pytest.mark.setup_client(pin=PIN) def test_busy_state(session: Session): - session.lock() _assert_busy(session, False, "Lockscreen") assert session.features.unlocked is False @@ -68,7 +67,6 @@ def test_busy_state(session: Session): @pytest.mark.models("core") def test_busy_expiry_core(session: Session): - session.lock() WAIT_TIME_MS = 1500 TOLERANCE = 1000 diff --git a/tests/device_tests/test_msg_applysettings.py b/tests/device_tests/test_msg_applysettings.py index 811425d767..6179acb82f 100644 --- a/tests/device_tests/test_msg_applysettings.py +++ b/tests/device_tests/test_msg_applysettings.py @@ -59,7 +59,6 @@ def _set_expected_responses(session: Session): def test_apply_settings(session: Session): - session.lock() assert session.features.label == "test" with session: @@ -71,7 +70,6 @@ def test_apply_settings(session: Session): @pytest.mark.models("core") def test_apply_settings_rotation(session: Session): - session.lock() assert session.features.display_rotation is None with session: @@ -83,7 +81,6 @@ def test_apply_settings_rotation(session: Session): @pytest.mark.setup_client(pin=PIN4, passphrase=False) def test_apply_settings_passphrase(session: Session): - session.lock() with session: _set_expected_responses(session) device.apply_settings(session, use_passphrase=True) @@ -390,7 +387,6 @@ def test_safety_checks(session: Session): @pytest.mark.models("core") def test_experimental_features(session: Session): - session.lock() def experimental_call(): misc.get_nonce(session) diff --git a/tests/device_tests/test_msg_change_wipe_code_t2.py b/tests/device_tests/test_msg_change_wipe_code_t2.py index 4ee8575979..efc2c9eeea 100644 --- a/tests/device_tests/test_msg_change_wipe_code_t2.py +++ b/tests/device_tests/test_msg_change_wipe_code_t2.py @@ -60,7 +60,7 @@ def _ensure_unlocked(session: Session, pin: str): @pytest.mark.setup_client(pin=PIN4) def test_set_remove_wipe_code(session: Session): - session.lock() + # Test set wipe code. assert session.features.wipe_code_protection is None _ensure_unlocked(session, PIN4) diff --git a/tests/device_tests/test_pin.py b/tests/device_tests/test_pin.py index 963825192e..c911dfee50 100644 --- a/tests/device_tests/test_pin.py +++ b/tests/device_tests/test_pin.py @@ -41,7 +41,6 @@ def test_no_protection(session: Session): def test_correct_pin(session: Session): with session, session.client as client: client.use_pin_sequence([PIN4]) - session.lock() # TODO is the lock here necessary/correctly? # Expected responses differ between T1 and TT is_t1 = session.model is models.T1B1 session.set_expected_responses( @@ -66,7 +65,6 @@ def test_incorrect_pin_t1(session: Session): @pytest.mark.models("core") def test_incorrect_pin_t2(session: Session): - session.lock() # TODO is the lock here necessary/correctly? with session, session.client as client: # After first incorrect attempt, TT will not raise an error, but instead ask for another attempt client.use_pin_sequence([BAD_PIN, PIN4]) @@ -95,5 +93,4 @@ def test_exponential_backoff_t2(session: Session): with session.client as client: IF = InputFlowPINBackoff(client, BAD_PIN, PIN4) client.set_input_flow(IF.get()) - session.lock() get_test_address(session) diff --git a/tests/device_tests/test_protection_levels.py b/tests/device_tests/test_protection_levels.py index 97cbe9b997..1f49657e35 100644 --- a/tests/device_tests/test_protection_levels.py +++ b/tests/device_tests/test_protection_levels.py @@ -443,7 +443,6 @@ def test_signtx(session: Session): @pytest.mark.setup_client(pin=PIN4, passphrase=False) def test_unlocked(session: Session): - session.lock() assert session.features.unlocked is False session = _assert_protection(session, passphrase=False) diff --git a/tests/device_tests/test_sdcard.py b/tests/device_tests/test_sdcard.py index e87189e4a0..8d5c45b81f 100644 --- a/tests/device_tests/test_sdcard.py +++ b/tests/device_tests/test_sdcard.py @@ -23,6 +23,8 @@ from trezorlib.messages import SdProtectOperationType as Op from .. import translations as TR +PIN = "1234" + pytestmark = pytest.mark.models("core", skip="safe3") @@ -51,17 +53,16 @@ def test_sd_no_format(session: Session): @pytest.mark.sd_card -@pytest.mark.setup_client(pin="1234") +@pytest.mark.setup_client(pin=PIN) def test_sd_protect_unlock(session: Session): debug = session.client.debug layout = debug.read_layout - session.lock() def input_flow_enable_sd_protect(): # debug.press_yes() yield # Enter PIN to unlock device assert "PinKeyboard" in layout().all_components() - debug.input("1234") + debug.input(PIN) yield # do you really want to enable SD protection assert TR.sd_card__enable in layout().text_content() @@ -69,7 +70,7 @@ def test_sd_protect_unlock(session: Session): yield # enter current PIN assert "PinKeyboard" in layout().all_components() - debug.input("1234") + debug.input(PIN) yield # you have successfully enabled SD protection assert TR.sd_card__enabled in layout().text_content() @@ -87,15 +88,15 @@ def test_sd_protect_unlock(session: Session): yield # enter current PIN assert "PinKeyboard" in layout().all_components() - debug.input("1234") + debug.input(PIN) yield # enter new PIN assert "PinKeyboard" in layout().all_components() - debug.input("1234") + debug.input(PIN) yield # enter new PIN again assert "PinKeyboard" in layout().all_components() - debug.input("1234") + debug.input(PIN) yield # Pin change successful assert TR.pin__changed in layout().text_content() @@ -115,7 +116,7 @@ def test_sd_protect_unlock(session: Session): yield # enter current PIN assert "PinKeyboard" in layout().all_components() - debug.input("1234") + debug.input(PIN) yield # SD card problem assert (