Debian Codegraph @

tests/test_permission_store.py

raw
1 # SPDX-License-Identifier: LGPL-2.1-or-later
2 #
3 # This file is formatted with Python Black
4
5 import tests.xdp_utils as xdp
6
7 import dbus
8 from gi.repository import GLib, Gio
9
10
11 class PermissionStore(xdp.GDBusIface):
12 def __init__(self):
13 super().__init__(
14 "org.freedesktop.impl.portal.PermissionStore",
15 "/org/freedesktop/impl/portal/PermissionStore",
16 "org.freedesktop.impl.portal.PermissionStore",
17 )
18
19 def Lookup(self, table, id):
20 return self._call(
21 "Lookup",
22 GLib.Variant("(ss)", (table, id)),
23 )
24
25 def Set(self, table, create, id, perm, data):
26 return self._call(
27 "Set",
28 GLib.Variant("(sbsa{sas}v)", (table, create, id, perm, data)),
29 )
30
31 def SetValue(self, table, create, id, data):
32 return self._call(
33 "SetValue",
34 GLib.Variant("(sbsv)", (table, create, id, data)),
35 )
36
37 def SetPermission(self, table, create, id, app, perm):
38 return self._call(
39 "SetPermission",
40 GLib.Variant("(sbssas)", (table, create, id, app, perm)),
41 )
42
43 def SetPermissionAsync(self, table, create, id, app, perm, user_cb):
44 self._call_async(
45 "SetPermission",
46 GLib.Variant("(sbssas)", (table, create, id, app, perm)),
47 cb=user_cb,
48 )
49
50 def DeletePermissionAsync(self, table, id, app, user_cb):
51 self._call_async(
52 "DeletePermission",
53 GLib.Variant("(sss)", (table, id, app)),
54 cb=user_cb,
55 )
56
57 def DeleteAsync(self, table, id, user_cb):
58 self._call_async(
59 "Delete",
60 GLib.Variant("(ss)", (table, id)),
61 cb=user_cb,
62 )
63
64 def Delete(self, table, id):
65 return self._call(
66 "Delete",
67 GLib.Variant("(ss)", (table, id)),
68 )
69
70 def GetPermission(self, table, id, app):
71 return self._call(
72 "GetPermission",
73 GLib.Variant("(sss)", (table, id, app)),
74 )
75
76
77 class TestPermissionStore:
78 def test_version(self, portals, dbus_con):
79 permission_store = dbus_con.get_object(
80 "org.freedesktop.impl.portal.PermissionStore",
81 "/org/freedesktop/impl/portal/PermissionStore",
82 )
83
84 properties_intf = dbus.Interface(
85 permission_store,
86 "org.freedesktop.DBus.Properties",
87 )
88 portal_version = properties_intf.Get(
89 "org.freedesktop.impl.portal.PermissionStore",
90 "version",
91 )
92 assert int(portal_version) == 2
93
94 def test_delete_race(self, portals, dbus_con):
95 permission_store_intf = PermissionStore()
96 finished_count = 0
97
98 table = "inhibit"
99 id = "inhibit"
100 perms = ["logout", "suspend"]
101
102 def cb(_):
103 nonlocal finished_count
104
105 finished_count += 1
106
107 permission_store_intf.SetPermissionAsync(table, True, id, "a", perms, cb)
108 permission_store_intf.DeleteAsync(table, id, cb)
109
110 xdp.wait_for(lambda: finished_count >= 2)
111
112 try:
113 permission_store_intf.Lookup(table, id)
114 assert False, "This statement should not be reached"
115 except GLib.GError as e:
116 assert "org.freedesktop.portal.Error.NotFound" in e.message
117 assert e.matches(Gio.io_error_quark(), Gio.IOErrorEnum.DBUS_ERROR)
118
119 permission_store_intf.SetPermissionAsync(table, True, id, "a", perms, cb)
120 permission_store_intf.SetPermissionAsync(table, True, id, "b", perms, cb)
121 permission_store_intf.DeletePermissionAsync(table, id, "a", cb)
122
123 xdp.wait_for(lambda: finished_count >= 4)
124
125 result, _ = permission_store_intf.Lookup(table, id)
126 perms_out = result.unpack()[0]
127 assert perms_out == {"b": perms}
128
129 permission_store_intf.SetPermissionAsync(table, True, id, "a", perms, cb)
130 permission_store_intf.DeletePermissionAsync(table, id, "b", cb)
131 permission_store_intf.DeletePermissionAsync(table, id, "a", cb)
132
133 xdp.wait_for(lambda: finished_count >= 7)
134
135 result, _ = permission_store_intf.Lookup(table, id)
136 perms_out = result.unpack()[0]
137 assert perms_out == {}
138
139 def test_change(self, portals, dbus_con):
140 permission_store_intf = PermissionStore()
141 changed_count = 0
142
143 table = "TEST"
144 id = "test-resource"
145 app = "one.two.three"
146 perms = ["one", "two"]
147
148 def cb_changed1(results):
149 nonlocal changed_count
150
151 cb_table, cb_id, deleted, _, cb_perms = results.unpack()
152
153 assert cb_table == table
154 assert cb_id == id
155 assert not deleted
156 assert cb_perms[app] == perms
157
158 changed_count += 1
159
160 cs = permission_store_intf.connect_to_signal("Changed", cb_changed1)
161 permission_store_intf.SetPermissionAsync(table, True, id, app, perms, None)
162 xdp.wait_for(lambda: changed_count >= 1)
163 cs.disconnect()
164
165 def cb_changed2(results):
166 nonlocal changed_count
167
168 cb_table, cb_id, deleted, _, _ = results.unpack()
169
170 assert cb_table == table
171 assert cb_id == id
172 assert deleted
173
174 changed_count += 1
175
176 cs = permission_store_intf.connect_to_signal("Changed", cb_changed2)
177 permission_store_intf.Delete(table, id)
178 xdp.wait_for(lambda: changed_count >= 2)
179 cs.disconnect()
180
181 def test_lookup(self, portals, dbus_con):
182 permission_store_intf = PermissionStore()
183
184 table = "TEST"
185 id = "test-resource"
186 perms = ["one", "two"]
187 data = True
188
189 try:
190 permission_store_intf.Lookup(table, id)
191 assert False, "This statement should not be reached"
192 except GLib.GError as e:
193 assert "org.freedesktop.portal.Error.NotFound" in e.message
194
195 permissions = [(id, perms)]
196 permission_store_intf.Set(table, True, id, permissions, GLib.Variant("b", data))
197
198 result, _ = permission_store_intf.Lookup(table, id)
199 perms_out = result.unpack()[0]
200 data_out = result.unpack()[1]
201
202 assert id in perms_out
203 perms_out = perms_out[id]
204 assert perms_out == perms
205
206 assert data_out == data
207
208 def test_set_value(self, portals, dbus_con):
209 permission_store_intf = PermissionStore()
210
211 table = "TEST"
212 id = "test-resource"
213 data = True
214
215 try:
216 permission_store_intf.Lookup(table, id)
217 assert False, "This statement should not be reached"
218 except GLib.GError as e:
219 assert "org.freedesktop.portal.Error.NotFound" in e.message
220
221 permission_store_intf.SetValue(table, True, id, GLib.Variant("b", data))
222
223 result, _ = permission_store_intf.Lookup(table, id)
224 perms_out = result.unpack()[0]
225 data_out = result.unpack()[1]
226 assert perms_out == {}
227 assert data_out == data
228
229 def test_create(self, portals, dbus_con):
230 permission_store_intf = PermissionStore()
231
232 table = "inhibit"
233 id = "inhibit"
234 app = ""
235 perms = ["logout", "suspend"]
236
237 try:
238 permission_store_intf.SetPermission(
239 table,
240 # Do not create if it does not exist
241 False,
242 id,
243 app,
244 perms,
245 )
246 assert False, "This statement should not be reached"
247 except GLib.GError as e:
248 assert "org.freedesktop.portal.Error.NotFound" in e.message
249
250 permission_store_intf.SetPermission(table, True, id, app, perms)
251
252 def test_delete(self, portals, dbus_con):
253 permission_store_intf = PermissionStore()
254
255 table = "inhibit"
256 id = "inhibit"
257 app = ""
258 perms = ["logout", "suspend"]
259
260 try:
261 permission_store_intf.Delete(table, id)
262 assert False, "This statement should not be reached"
263 except GLib.GError as e:
264 assert "org.freedesktop.portal.Error.NotFound" in e.message
265
266 permission_store_intf.SetPermission(table, True, id, app, perms)
267
268 permission_store_intf.Delete(table, id)
269
270 try:
271 permission_store_intf.Lookup(table, id)
272 assert False, "This statement should not be reached"
273 except GLib.GError as e:
274 assert "org.freedesktop.portal.Error.NotFound" in e.message
275
276 def test_get_permission(self, portals, dbus_con):
277 permission_store_intf = PermissionStore()
278
279 table = "notifications"
280 id = "notification"
281 app = "a"
282 perms = ["yes"]
283
284 try:
285 permission_store_intf.GetPermission(table, id, app)
286 assert False, "This statement should not be reached"
287 except GLib.GError as e:
288 assert "org.freedesktop.portal.Error.NotFound" in e.message
289
290 permission_store_intf.SetPermission(table, True, id, app, perms)
291
292 result, _ = permission_store_intf.GetPermission(table, id, app)
293 permissions = result.unpack()[0]
294 assert permissions == perms
295
296 result, _ = permission_store_intf.GetPermission(table, id, "no-such-app")
297 permissions = result.unpack()[0]
298 assert permissions == []