def test_end_session(client: Client): # client instance starts out not initialized # XXX do we want to change this? assert client.session_id is not None # get_address will succeed with client: client.set_expected_responses([messages.Address]) get_test_address(client) client.end_session() assert client.session_id is None with pytest.raises(TrezorFailure) as exc: get_test_address(client) assert exc.value.code == messages.FailureType.InvalidSession assert exc.value.message.endswith("Invalid session") client.init_device() assert client.session_id is not None with client: client.set_expected_responses([messages.Address]) get_test_address(client) with client: # end_session should succeed on empty session too client.set_expected_responses([messages.Success] * 2) client.end_session() client.end_session()
def test_cannot_resume_ended_session(client: Client): session_id = client.session_id with client: client.set_expected_responses([messages.Features]) client.init_device(session_id=session_id) assert session_id == client.session_id client.end_session() with client: client.set_expected_responses([messages.Features]) client.init_device(session_id=session_id) assert session_id != client.session_id
def test_end_session_only_current(client: Client): """test that EndSession only destroys the current session""" session_id_a = client.session_id client.init_device(new_session=True) session_id_b = client.session_id client.end_session() assert client.session_id is None # resume ended session client.init_device(session_id=session_id_b) assert client.session_id != session_id_b # resume first session that was not ended client.init_device(session_id=session_id_a) assert client.session_id == session_id_a
def test_session_recycling(client: Client): session_id_orig = client.session_id with client: client.set_expected_responses([ messages.PassphraseRequest, messages.ButtonRequest, messages.ButtonRequest, messages.Address, ]) client.use_passphrase("TREZOR") address = get_test_address(client) # create and close 100 sessions - more than the session limit for _ in range(100): client.init_device(new_session=True) client.end_session() # it should still be possible to resume the original session with client: # passphrase should still be cached client.set_expected_responses([messages.Features, messages.Address]) client.use_passphrase("TREZOR") client.init_device(session_id=session_id_orig) assert address == get_test_address(client)