From 74dff75a48d4908a1ae2502b33dd025a2f8558b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonas=20=C3=85dahl?= Date: Mon, 13 Jan 2025 23:46:17 +0100 Subject: [PATCH] tests: Add tests for Registry host portal --- tests/meson.build | 1 + tests/templates/remotedesktop.py | 9 ++++ tests/test_registry.py | 86 ++++++++++++++++++++++++++++++++ 3 files changed, 96 insertions(+) create mode 100644 tests/test_registry.py diff --git a/tests/meson.build b/tests/meson.build index f24f3e8a2..2411e70fb 100644 --- a/tests/meson.build +++ b/tests/meson.build @@ -311,6 +311,7 @@ if enable_pytest 'test_openuri.py', 'test_permission_store.py', 'test_print.py', + 'test_registry.py', 'test_remotedesktop.py', 'test_settings.py', 'test_screenshot.py', diff --git a/tests/templates/remotedesktop.py b/tests/templates/remotedesktop.py index f6669af54..6d1d3b367 100644 --- a/tests/templates/remotedesktop.py +++ b/tests/templates/remotedesktop.py @@ -3,6 +3,7 @@ # This file is formatted with Python Black from tests.templates import Response, init_template_logger, ImplRequest, ImplSession +from dbusmock import MOCK_IFACE import dbus import dbus.service import socket @@ -183,3 +184,11 @@ def ConnectToEIS(self, session_handle, app_id, options): except Exception as e: logger.critical(e) raise e + + +@dbus.service.method(MOCK_IFACE, in_signature="s", out_signature="s") +def GetSessionAppId(self, session_handle): + logger.debug(f"GetSessionAppId({session_handle})") + + assert session_handle in self.sessions + return self.sessions[session_handle].app_id diff --git a/tests/test_registry.py b/tests/test_registry.py new file mode 100644 index 000000000..e398736a6 --- /dev/null +++ b/tests/test_registry.py @@ -0,0 +1,86 @@ +# SPDX-License-Identifier: LGPL-2.1-or-later + +import tests as xdp + +import dbus +import pytest + + +@pytest.fixture +def app_id(): + return "org.example.WrongAppId" + + +@pytest.fixture +def required_templates(): + return {"remotedesktop": {}} + + +class TestRegistry: + def test_version(self, portals, dbus_con): + xdp.check_version(dbus_con, "Registry", 1, domain="host") + + def create_dummy_session(self, dbus_con): + remotedesktop_intf = xdp.get_portal_iface(dbus_con, "RemoteDesktop") + request = xdp.Request(dbus_con, remotedesktop_intf) + + session_counter_attr_name = "session_counter" + if hasattr(self, session_counter_attr_name): + session_counter = getattr(self, session_counter_attr_name) + else: + session_counter = 0 + setattr(self, session_counter_attr_name, session_counter + 1) + + print(f"session_handle_token: {session_counter}") + options = { + "session_handle_token": f"session_token{session_counter}", + } + response = request.call( + "CreateSession", + options=options, + ) + assert response.response == 0 + + return xdp.Session.from_response(dbus_con, response) + + def test_registerless(self, portals, dbus_con): + mock_intf = xdp.get_mock_iface(dbus_con) + + expected_app_id = "org.example.WrongAppId" + + session = self.create_dummy_session(dbus_con) + + app_id = mock_intf.GetSessionAppId(session.handle) + assert app_id == expected_app_id + + def test_register(self, portals, dbus_con): + registry_intf = xdp.get_portal_iface(dbus_con, "Registry", domain="host") + mock_intf = xdp.get_mock_iface(dbus_con) + + expected_app_id = "org.example.CorrectAppId" + registry_intf.Register(expected_app_id, {}) + + session = self.create_dummy_session(dbus_con) + + app_id = mock_intf.GetSessionAppId(session.handle) + assert app_id == expected_app_id + + def test_late_register(self, portals, dbus_con): + registry_intf = xdp.get_portal_iface(dbus_con, "Registry", domain="host") + mock_intf = xdp.get_mock_iface(dbus_con) + + expected_app_id = "org.example.WrongAppId" + unexpected_app_id = "org.example.CorrectAppId" + + session = self.create_dummy_session(dbus_con) + + app_id = mock_intf.GetSessionAppId(session.handle) + assert app_id == expected_app_id + + with pytest.raises(dbus.exceptions.DBusException): + registry_intf.Register(unexpected_app_id, {}) + + new_session = self.create_dummy_session(dbus_con) + + new_app_id = mock_intf.GetSessionAppId(new_session.handle) + assert new_app_id == expected_app_id