Skip to content

Commit

Permalink
tests: Add tests for Registry host portal
Browse files Browse the repository at this point in the history
  • Loading branch information
jadahl authored and swick committed Jan 14, 2025
1 parent e2b5648 commit 74dff75
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 0 deletions.
1 change: 1 addition & 0 deletions tests/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
9 changes: 9 additions & 0 deletions tests/templates/remotedesktop.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
86 changes: 86 additions & 0 deletions tests/test_registry.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 74dff75

Please sign in to comment.