1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2025-05-08 18:08:46 +00:00

fixup! test: update device tests

This commit is contained in:
M1nd3r 2025-03-24 15:28:06 +01:00
parent b9e11d7939
commit 347b1a7b04
7 changed files with 74 additions and 75 deletions

View File

@ -850,7 +850,7 @@ def test_multisession_authorization(client: Client):
) )
# Switch back to the first session. # Switch back to the first session.
session1 = client.resume_session(session1) session1.resume()
# Requesting a preauthorized ownership proof for www.example1.com should succeed in session 1. # Requesting a preauthorized ownership proof for www.example1.com should succeed in session 1.
ownership_proof, _ = btc.get_ownership_proof( ownership_proof, _ = btc.get_ownership_proof(
session1, session1,
@ -895,7 +895,7 @@ def test_multisession_authorization(client: Client):
) )
# Switch to the second session. # Switch to the second session.
session2 = client.resume_session(session2) session2.resume()
# Requesting a preauthorized ownership proof for www.example2.com should still succeed in session 2. # Requesting a preauthorized ownership proof for www.example2.com should still succeed in session 2.
ownership_proof, _ = btc.get_ownership_proof( ownership_proof, _ = btc.get_ownership_proof(
session2, session2,

View File

@ -137,7 +137,6 @@ def test_autolock_cancels_ui(session: Session):
def test_autolock_ignores_initialize(session: Session): def test_autolock_ignores_initialize(session: Session):
client = session.client
set_autolock_delay(session, 10 * 1000) set_autolock_delay(session, 10 * 1000)
assert session.features.unlocked is True assert session.features.unlocked is True
@ -145,7 +144,7 @@ def test_autolock_ignores_initialize(session: Session):
start = time.monotonic() start = time.monotonic()
while time.monotonic() - start < 11: while time.monotonic() - start < 11:
# init_device should always work even if locked # init_device should always work even if locked
client.resume_session(session) session.resume()
time.sleep(0.1) time.sleep(0.1)
# after 11 seconds we are definitely locked # after 11 seconds we are definitely locked

View File

@ -208,18 +208,17 @@ def test_interrupt_backup_fails(session: Session):
session.call_raw(messages.BackupDevice()) session.call_raw(messages.BackupDevice())
# interupt backup by sending initialize # interupt backup by sending initialize
session2 = session.client.resume_session(session) session.resume()
session2.refresh_features() session.refresh_features()
# check that device state is as expected # check that device state is as expected
assert session2.features.initialized is True assert session.features.initialized is True
assert ( assert (
session2.features.backup_availability session.features.backup_availability == messages.BackupAvailability.NotAvailable
== messages.BackupAvailability.NotAvailable
) )
assert session2.features.unfinished_backup is True assert session.features.unfinished_backup is True
assert session2.features.no_backup is False assert session.features.no_backup is False
# Second attempt at backup should fail # Second attempt at backup should fail
with pytest.raises(TrezorFailure, match=r".*Seed already backed up"): with pytest.raises(TrezorFailure, match=r".*Seed already backed up"):
device.backup(session2) device.backup(session)

View File

@ -199,6 +199,6 @@ def test_set_wipe_code_invalid(session: Session, invalid_wipe_code: str):
assert isinstance(ret, messages.Failure) assert isinstance(ret, messages.Failure)
# Check that there's still no wipe code protection. # Check that there's still no wipe code protection.
session = session.client.resume_session(session) session.resume()
session.ensure_unlocked() session.ensure_unlocked()
assert session.features.wipe_code_protection is False assert session.features.wipe_code_protection is False

View File

@ -189,7 +189,7 @@ def test_set_invalid(session: Session, invalid_pin):
assert isinstance(ret, messages.Failure) assert isinstance(ret, messages.Failure)
# Check that there's still no PIN protection now # Check that there's still no PIN protection now
session = session.client.resume_session(session) session.resume()
assert session.features.pin_protection is False assert session.features.pin_protection is False
_check_no_pin(session) _check_no_pin(session)

View File

@ -46,7 +46,7 @@ def test_clear_session(client: Client):
session.set_expected_responses(init_responses + cached_responses) session.set_expected_responses(init_responses + cached_responses)
assert get_public_node(session, ADDRESS_N).xpub == XPUB assert get_public_node(session, ADDRESS_N).xpub == XPUB
client.resume_session(session) session.resume()
with session: with session:
# pin and passphrase are cached # pin and passphrase are cached
session.set_expected_responses(cached_responses) session.set_expected_responses(cached_responses)
@ -62,7 +62,7 @@ def test_clear_session(client: Client):
session.set_expected_responses(init_responses + cached_responses) session.set_expected_responses(init_responses + cached_responses)
assert get_public_node(session, ADDRESS_N).xpub == XPUB assert get_public_node(session, ADDRESS_N).xpub == XPUB
client.resume_session(session) session.resume()
with session: with session:
# pin and passphrase are cached # pin and passphrase are cached
session.set_expected_responses(cached_responses) session.set_expected_responses(cached_responses)
@ -104,14 +104,14 @@ def test_cannot_resume_ended_session(client: Client):
session = client.get_session() session = client.get_session()
session_id = session.id session_id = session.id
session_resumed = client.resume_session(session) session.resume()
assert session_resumed.id == session_id assert session.id == session_id
session.end() session.end()
session_resumed2 = client.resume_session(session) session.resume()
assert session_resumed2.id != session_id assert session.id != session_id
def test_end_session_only_current(client: Client): def test_end_session_only_current(client: Client):
@ -124,12 +124,12 @@ def test_end_session_only_current(client: Client):
# assert client.session_id is None # assert client.session_id is None
# resume ended session # resume ended session
session_b_resumed = client.resume_session(session_b) session_b.resume()
assert session_b_resumed.id != session_b_id assert session_b.id != session_b_id
# resume first session that was not ended # resume first session that was not ended
session_a_resumed = client.resume_session(session_a) session_a.resume()
assert session_a_resumed.id == session_a.id assert session_a.id == session_a.id
@pytest.mark.setup_client(passphrase=True) @pytest.mark.setup_client(passphrase=True)
@ -155,7 +155,7 @@ def test_session_recycling(client: Client):
with session: with session:
# passphrase should still be cached # passphrase should still be cached
session.set_expected_responses([messages.Address] * 3) session.set_expected_responses([messages.Address] * 3)
client.resume_session(session) session.resume()
get_test_address(session) get_test_address(session)
get_test_address(session) get_test_address(session)
assert address == get_test_address(session) assert address == get_test_address(session)
@ -170,8 +170,8 @@ def test_derive_cardano_empty_session(client: Client):
session_id = session.id session_id = session.id
# restarting same session should go well # restarting same session should go well
session_2 = client.resume_session(session) session.resume()
assert session.id == session_2.id assert session.id == session_id
# restarting same session should go well with any setting # restarting same session should go well with any setting
session_3 = client.get_session(session_id=session_id, derive_cardano=False) session_3 = client.get_session(session_id=session_id, derive_cardano=False)
@ -186,7 +186,7 @@ def test_derive_cardano_empty_session(client: Client):
def test_derive_cardano_running_session(client: Client): def test_derive_cardano_running_session(client: Client):
# start new session # start new session
session = client.get_session(derive_cardano=False) session = client.get_session(derive_cardano=False)
session_id = session.id
# force derivation of seed # force derivation of seed
get_test_address(session) get_test_address(session)
@ -195,23 +195,24 @@ def test_derive_cardano_running_session(client: Client):
cardano.get_public_key(session, parse_path("m/44h/1815h/0h")) cardano.get_public_key(session, parse_path("m/44h/1815h/0h"))
# restarting same session should go well # restarting same session should go well
session_2 = client.resume_session(session) session.resume()
assert session.id == session_2.id assert session.id == session_id
# restarting same session should go well if we _don't_ want to derive cardano # restarting same session should go well if we _don't_ want to derive cardano
session_3 = client.get_session(session_id=session_2.id, derive_cardano=False) session_3 = client.get_session(session_id=session_id, derive_cardano=False)
assert session_3.id == session.id assert session_3.id == session.id
# restarting with derive_cardano=True should kill old session and create new one # restarting with derive_cardano=True should kill old session and create new one
session_4 = client.get_session(derive_cardano=True) session_4 = client.get_session(derive_cardano=True)
assert session_4.id != session.id session_4_id = session_4.id
assert session_4_id != session.id
# new session should have Cardano capability # new session should have Cardano capability
cardano.get_public_key(session_4, parse_path("m/44h/1815h/0h")) cardano.get_public_key(session_4, parse_path("m/44h/1815h/0h"))
# restarting with derive_cardano=True should keep same session # restarting with derive_cardano=True should keep same session
session_5 = client.resume_session(session_4) session_4.resume()
assert session_5.id == session_4.id assert session_4.id == session_4_id
# restarting with derive_cardano=False should kill old session and create new one # restarting with derive_cardano=False should kill old session and create new one
session_6 = client.get_session(session_id=session_4.id, derive_cardano=False) session_6 = client.get_session(session_id=session_4.id, derive_cardano=False)

View File

@ -83,9 +83,9 @@ def test_session_with_passphrase(client: Client):
# Call Initialize again, this time with the received session id and then call # Call Initialize again, this time with the received session id and then call
# GetPublicKey. The passphrase should be cached now so Trezor must # GetPublicKey. The passphrase should be cached now so Trezor must
# not ask for it again, whilst returning the same xpub. # not ask for it again, whilst returning the same xpub.
session2 = client.resume_session(session) session.resume()
assert session2.id == session_id assert session.id == session_id
assert _get_xpub(session2) == XPUB_PASSPHRASES["A"] assert _get_xpub(session) == XPUB_PASSPHRASES["A"]
# If we set session id in Initialize to None, the cache will be cleared # If we set session id in Initialize to None, the cache will be cleared
# and Trezor will ask for the passphrase again. # and Trezor will ask for the passphrase again.
@ -113,34 +113,34 @@ def test_multiple_sessions(client: Client):
# Resume each session # Resume each session
for i in range(SESSIONS_STORED): for i in range(SESSIONS_STORED):
resumed_session = client.resume_session(sessions[i]) sessions[i].resume()
assert session_ids[i] == resumed_session.id assert session_ids[i] == sessions[i].id
# Creating a new session replaces the least-recently-used session # Creating a new session replaces the least-recently-used session
client.get_session() client.get_session()
# Resuming session 1 through SESSIONS_STORED will still work # Resuming session 1 through SESSIONS_STORED will still work
for i in range(1, SESSIONS_STORED): for i in range(1, SESSIONS_STORED):
resumed_session = client.resume_session(sessions[i]) sessions[i].resume()
assert session_ids[i] == resumed_session.id assert session_ids[i] == sessions[i].id
# Resuming session 0 will not work # Resuming session 0 will not work
resumed_session = client.resume_session(sessions[0]) sessions[0].resume()
assert session_ids[0] != resumed_session.id assert session_ids[0] != sessions[0].id
# New session bumped out the least-recently-used anonymous session. # New session bumped out the least-recently-used anonymous session.
# Resuming session 1 through SESSIONS_STORED will still work # Resuming session 1 through SESSIONS_STORED will still work
for i in range(1, SESSIONS_STORED): for i in range(1, SESSIONS_STORED):
resumed_session = client.resume_session(sessions[i]) sessions[i].resume()
assert session_ids[i] == resumed_session.id assert session_ids[i] == sessions[i].id
# Creating a new session replaces session_ids[0] again # Creating a new session replaces session_ids[0] again
client.get_session() client.get_session()
# Resuming all sessions one by one will in turn bump out the previous session. # Resuming all sessions one by one will in turn bump out the previous session.
for i in range(SESSIONS_STORED): for i in range(SESSIONS_STORED):
resumed_session = client.resume_session(sessions[i]) sessions[i].resume()
assert session_ids[i] != resumed_session.id assert session_ids[i] != sessions[i].id
@pytest.mark.setup_client(passphrase=True) @pytest.mark.setup_client(passphrase=True)
@ -150,11 +150,11 @@ def test_multiple_passphrases(client: Client):
session_a_id = session_a.id session_a_id = session_a.id
assert _get_xpub(session_a, expected_passphrase_req=True) == XPUB_PASSPHRASES["A"] assert _get_xpub(session_a, expected_passphrase_req=True) == XPUB_PASSPHRASES["A"]
# start it again wit the same session id # start it again wit the same session id
session_a_resumed = client.resume_session(session_a) session_a.resume()
# session is the same # session is the same
assert session_a_resumed.id == session_a_id assert session_a.id == session_a_id
# passphrase is not prompted # passphrase is not prompted
assert _get_xpub(session_a_resumed) == XPUB_PASSPHRASES["A"] assert _get_xpub(session_a) == XPUB_PASSPHRASES["A"]
# start a second session # start a second session
session_b = client.get_session(passphrase="B") session_b = client.get_session(passphrase="B")
@ -163,19 +163,19 @@ def test_multiple_passphrases(client: Client):
assert _get_xpub(session_b, expected_passphrase_req=True) == XPUB_PASSPHRASES["B"] assert _get_xpub(session_b, expected_passphrase_req=True) == XPUB_PASSPHRASES["B"]
# provide the same session id -> must not ask for passphrase again. # provide the same session id -> must not ask for passphrase again.
session_b_resumed = client.resume_session(session_b) session_b.resume()
assert session_b_resumed.id == session_b_id assert session_b.id == session_b_id
assert _get_xpub(session_b_resumed) == XPUB_PASSPHRASES["B"] assert _get_xpub(session_b) == XPUB_PASSPHRASES["B"]
# provide the first session id -> must not ask for passphrase again and return the same result. # provide the first session id -> must not ask for passphrase again and return the same result.
session_a_resumed_again = client.resume_session(session_a) session_a.resume()
assert session_a_resumed_again.id == session_a_id assert session_a.id == session_a_id
assert _get_xpub(session_a_resumed_again) == XPUB_PASSPHRASES["A"] assert _get_xpub(session_a) == XPUB_PASSPHRASES["A"]
# provide the second session id -> must not ask for passphrase again and return the same result. # provide the second session id -> must not ask for passphrase again and return the same result.
session_b_resumed_again = client.resume_session(session_b) session_b.resume()
assert session_b_resumed_again.id == session_b_id assert session_b.id == session_b_id
assert _get_xpub(session_b_resumed_again) == XPUB_PASSPHRASES["B"] assert _get_xpub(session_b) == XPUB_PASSPHRASES["B"]
@pytest.mark.slow @pytest.mark.slow
@ -201,15 +201,15 @@ def test_max_sessions_with_passphrases(client: Client):
for _ in range(20): for _ in range(20):
random.shuffle(shuffling) random.shuffle(shuffling)
for passphrase in shuffling: for passphrase in shuffling:
resumed_session = client.resume_session(sessions[passphrase]) sessions[passphrase].resume()
assert resumed_session.id == session_ids[passphrase] assert sessions[passphrase].id == session_ids[passphrase]
assert _get_xpub(resumed_session) == XPUB_PASSPHRASES[passphrase] assert _get_xpub(sessions[passphrase]) == XPUB_PASSPHRASES[passphrase]
# make sure the usage order is the reverse of the creation order # make sure the usage order is the reverse of the creation order
for passphrase in reversed(passphrases): for passphrase in reversed(passphrases):
resumed_session = client.resume_session(sessions[passphrase]) sessions[passphrase].resume()
assert resumed_session.id == session_ids[passphrase] assert sessions[passphrase].id == session_ids[passphrase]
assert _get_xpub(resumed_session) == XPUB_PASSPHRASES[passphrase] assert _get_xpub(sessions[passphrase]) == XPUB_PASSPHRASES[passphrase]
# creating one more session will exceed the limit # creating one more session will exceed the limit
new_session = client.get_session(passphrase="XX") new_session = client.get_session(passphrase="XX")
@ -218,9 +218,9 @@ def test_max_sessions_with_passphrases(client: Client):
# restoring the sessions in reverse will evict the next-up session # restoring the sessions in reverse will evict the next-up session
for passphrase in reversed(passphrases): for passphrase in reversed(passphrases):
resumed_session = client.resume_session(sessions[passphrase]) sessions[passphrase].resume()
_get_xpub( _get_xpub(
resumed_session, sessions[passphrase],
expected_passphrase_req=True, expected_passphrase_req=True,
) # passphrase is prompted ) # passphrase is prompted
@ -239,9 +239,9 @@ def test_session_enable_passphrase(client: Client):
# The session id is unchanged, therefore we do not prompt for the passphrase. # The session id is unchanged, therefore we do not prompt for the passphrase.
session_id = session.id session_id = session.id
resumed_session = client.resume_session(session) session.resume()
assert session_id == resumed_session.id assert session_id == session.id
assert _get_xpub(resumed_session) == XPUB_PASSPHRASE_NONE assert _get_xpub(session) == XPUB_PASSPHRASE_NONE
# We clear the session id now, so the passphrase should be asked. # We clear the session id now, so the passphrase should be asked.
new_session = client.get_session(passphrase="A") new_session = client.get_session(passphrase="A")
@ -308,8 +308,8 @@ def test_passphrase_always_on_device(client: Client):
assert response.xpub == XPUB_PASSPHRASE_NONE assert response.xpub == XPUB_PASSPHRASE_NONE
# Passphrase will not be prompted. The session id stays the same and the passphrase is cached. # Passphrase will not be prompted. The session id stays the same and the passphrase is cached.
resumed_session = client.resume_session(session) session.resume()
response = resumed_session.call_raw(XPUB_REQUEST) response = session.call_raw(XPUB_REQUEST)
assert isinstance(response, messages.PublicKey) assert isinstance(response, messages.PublicKey)
assert response.xpub == XPUB_PASSPHRASE_NONE assert response.xpub == XPUB_PASSPHRASE_NONE
@ -507,10 +507,10 @@ def test_cardano_passphrase(client: Client):
assert _get_xpub_cardano(session) == XPUB_CARDANO_PASSPHRASE_B assert _get_xpub_cardano(session) == XPUB_CARDANO_PASSPHRASE_B
# Initialize with the session id does not destroy the state # Initialize with the session id does not destroy the state
resumed_session = client.resume_session(session) session.resume()
# _init_session(client, session_id=session_id, derive_cardano=True) # _init_session(client, session_id=session_id, derive_cardano=True)
assert _get_xpub(resumed_session) == XPUB_PASSPHRASES["B"] assert _get_xpub(session) == XPUB_PASSPHRASES["B"]
assert _get_xpub_cardano(resumed_session) == XPUB_CARDANO_PASSPHRASE_B assert _get_xpub_cardano(session) == XPUB_CARDANO_PASSPHRASE_B
# New session will destroy the state # New session will destroy the state
new_session = client.get_session(passphrase="A", derive_cardano=True) new_session = client.get_session(passphrase="A", derive_cardano=True)