Debian Codegraph @

tests/test_registry.py

raw
1 # SPDX-License-Identifier: LGPL-2.1-or-later
2
3 import tests as xdp
4
5 import dbus
6 import pytest
7
8
9 @pytest.fixture
10 def app_id():
11 return "org.example.WrongAppId"
12
13
14 @pytest.fixture
15 def required_templates():
16 return {"remotedesktop": {}}
17
18
19 class TestRegistry:
20 def test_version(self, portals, dbus_con):
21 documents = dbus_con.get_object(
22 "org.freedesktop.portal.Desktop",
23 "/org/freedesktop/portal/desktop",
24 )
25
26 properties_intf = dbus.Interface(
27 documents,
28 "org.freedesktop.DBus.Properties",
29 )
30 portal_version = properties_intf.Get(
31 "org.freedesktop.host.portal.Registry",
32 "version",
33 )
34 assert int(portal_version) == 1
35
36 def create_dummy_session(self, dbus_con):
37 remotedesktop_intf = xdp.get_portal_iface(dbus_con, "RemoteDesktop")
38 request = xdp.Request(dbus_con, remotedesktop_intf)
39
40 session_counter_attr_name = "session_counter"
41 session_counter = getattr(self, session_counter_attr_name, 0)
42 setattr(self, session_counter_attr_name, session_counter + 1)
43
44 options = {
45 "session_handle_token": f"session_token{session_counter}",
46 }
47 response = request.call(
48 "CreateSession",
49 options=options,
50 )
51 assert response
52 assert response.response == 0
53
54 return xdp.Session.from_response(dbus_con, response)
55
56 def test_registerless(self, portals, dbus_con, app_id):
57 mock_intf = xdp.get_mock_iface(dbus_con)
58
59 expected_app_id = app_id
60
61 session = self.create_dummy_session(dbus_con)
62
63 app_id = mock_intf.GetSessionAppId(session.handle)
64 assert app_id == expected_app_id
65
66 def test_register(self, portals, dbus_con):
67 registry_intf = xdp.get_portal_iface(dbus_con, "Registry", domain="host")
68 mock_intf = xdp.get_mock_iface(dbus_con)
69
70 expected_app_id = "org.example.CorrectAppId"
71 registry_intf.Register(expected_app_id, {})
72
73 session = self.create_dummy_session(dbus_con)
74
75 app_id = mock_intf.GetSessionAppId(session.handle)
76 assert app_id == expected_app_id
77
78 def test_late_register(self, portals, dbus_con, app_id):
79 registry_intf = xdp.get_portal_iface(dbus_con, "Registry", domain="host")
80 mock_intf = xdp.get_mock_iface(dbus_con)
81
82 expected_app_id = app_id
83 unexpected_app_id = "org.example.CorrectAppId"
84
85 session = self.create_dummy_session(dbus_con)
86
87 app_id = mock_intf.GetSessionAppId(session.handle)
88 assert app_id == expected_app_id
89
90 with pytest.raises(dbus.exceptions.DBusException) as exc_info:
91 registry_intf.Register(unexpected_app_id, {})
92 exc_info.match(".*Connection already associated with an application ID.*")
93
94 new_session = self.create_dummy_session(dbus_con)
95
96 new_app_id = mock_intf.GetSessionAppId(new_session.handle)
97 assert new_app_id == expected_app_id
98
99 def test_multiple_connections(self, portals, dbus_con, app_id):
100 registry_intf = xdp.get_portal_iface(dbus_con, "Registry", domain="host")
101 mock_intf = xdp.get_mock_iface(dbus_con)
102
103 expected_app_id = "org.example.CorrectAppId"
104 unexpected_app_id = app_id
105
106 registry_intf.Register(expected_app_id, {})
107 session = self.create_dummy_session(dbus_con)
108 app_id = mock_intf.GetSessionAppId(session.handle)
109 assert app_id == expected_app_id
110
111 dbus_con2 = dbus.bus.BusConnection(dbus.bus.BusConnection.TYPE_SESSION)
112 dbus_con2.set_exit_on_disconnect(False)
113 mock_intf2 = xdp.get_mock_iface(dbus_con2)
114 session2 = self.create_dummy_session(dbus_con2)
115 app_id2 = mock_intf2.GetSessionAppId(session2.handle)
116 assert app_id2 == unexpected_app_id
117 dbus_con2.close()
118
119 dbus_con3 = dbus.bus.BusConnection(dbus.bus.BusConnection.TYPE_SESSION)
120 dbus_con3.set_exit_on_disconnect(False)
121 mock_intf3 = xdp.get_mock_iface(dbus_con3)
122 registry_intf3 = xdp.get_portal_iface(dbus_con3, "Registry", domain="host")
123 registry_intf3.Register(expected_app_id, {})
124 session3 = self.create_dummy_session(dbus_con3)
125 app_id3 = mock_intf3.GetSessionAppId(session3.handle)
126 assert app_id3 == expected_app_id
127 dbus_con3.close()
128
129 def test_no_reregister(self, portals, dbus_con):
130 registry_intf = xdp.get_portal_iface(dbus_con, "Registry", domain="host")
131 mock_intf = xdp.get_mock_iface(dbus_con)
132
133 expected_app_id = "org.example.CorrectAppId"
134
135 registry_intf.Register(expected_app_id, {})
136 session = self.create_dummy_session(dbus_con)
137 app_id = mock_intf.GetSessionAppId(session.handle)
138 assert app_id == expected_app_id
139
140 with pytest.raises(dbus.exceptions.DBusException) as exc_info:
141 registry_intf.Register(expected_app_id, {})
142 exc_info.match(".*Connection already associated with an application ID.*")