Debian Codegraph @

tests/src/utils.js

raw
1 /* exported setup update has hasOnly hasInTotal startService
2 stopService getValueFromService waitForService partialService */
3 /* eslint no-extend-native: */
4
5 /* utils.js
6 *
7 * Copyright 2020 Martin Abente Lahaye
8 *
9 * This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation, either version 3 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 */
22
23 imports.gi.versions.Gtk = '4.0';
24
25 const {gettext} = imports;
26
27 const {Gio, GLib, Gtk} = imports.gi;
28
29
30 const TestPermissionStoreIface = `
31 <node xmlns:doc="http://www.freedesktop.org/dbus/1.0/doc.dtd">
32 <interface name="org.freedesktop.impl.portal.PermissionStore">
33 <method name="testPartialTable">
34 </method>
35 </interface>
36 </node>
37 `;
38
39
40 function setup() {
41 Gtk.init();
42
43 /* XXX this shouldn't be needed */
44 const {format} = imports;
45 String.prototype.format = format.format;
46
47 window._ = gettext.gettext;
48
49 const src = GLib.build_filenamev([
50 GLib.get_current_dir(),
51 'src',
52 'com.github.tchx84.Flatseal.src.gresource',
53 ]);
54
55 const data = GLib.build_filenamev([
56 GLib.get_current_dir(),
57 'src',
58 'com.github.tchx84.Flatseal.data.gresource',
59 ]);
60
61 Gio.Resource.load(src)._register();
62 Gio.Resource.load(data)._register();
63
64 imports.searchPath.unshift('resource:///com/github/tchx84/Flatseal/js');
65 }
66
67
68 function update() {
69 const context = GLib.MainContext.default();
70 while (context.pending())
71 context.iteration(true);
72 }
73
74
75 function has(path, group, key, value) {
76 const keyFile = new GLib.KeyFile();
77 keyFile.load_from_file(path, 0);
78
79 const [keys] = keyFile.get_keys(group);
80 if (!keys.includes(key))
81 return false;
82
83 const values = keyFile.get_value(group, key);
84 return values.split(';').indexOf(value) !== -1;
85 }
86
87
88 function hasOnly(path, group, key, value) {
89 const keyFile = new GLib.KeyFile();
90 keyFile.load_from_file(path, 0);
91
92 const [keys] = keyFile.get_keys(group);
93 const values = keyFile.get_value(group, key);
94 const list = values.split(';');
95
96 return keys.length === 1 && list.length === 1 && list.indexOf(value) !== -1;
97 }
98
99
100 function hasInTotal(path) {
101 let count = 0;
102
103 const keyFile = new GLib.KeyFile();
104 keyFile.load_from_file(path, 0);
105
106 const [groups] = keyFile.get_groups();
107
108 groups.forEach(group => {
109 const [keys] = keyFile.get_keys(group);
110
111 keys.forEach(key => {
112 const values = keyFile.get_value(group, key);
113 count += values.split(';').length;
114 });
115 });
116
117 return count;
118 }
119
120
121 function startService() {
122 GLib.setenv(
123 'FLATSEAL_PORTAL_BUS_NAME',
124 'com.github.tchx84.Flatseal.PermissionStore',
125 true);
126 const service = GLib.build_filenamev([
127 '..',
128 'tests',
129 'service.js',
130 ]);
131 window.service = Gio.Subprocess.new(['gjs', service], null);
132 }
133
134
135 function stopService() {
136 window.service.force_exit();
137 }
138
139
140 function getValueFromService(table, id, allowed, appId) {
141 const {PermissionsIface} = imports.models.portals;
142 const Proxy = Gio.DBusProxy.makeProxyWrapper(PermissionsIface);
143
144 const proxy = new Proxy(
145 Gio.DBus.session,
146 GLib.getenv('FLATSEAL_PORTAL_BUS_NAME'),
147 '/org/freedesktop/impl/portal/PermissionStore');
148
149 let appIds;
150 try {
151 [appIds] = proxy.LookupSync(table, id);
152 } catch (err) {
153 appIds = null;
154 }
155
156 // check if no entry in the permission store
157 if (allowed === null && (appIds === null || !(appId in appIds)))
158 return true;
159
160 const value = appId in appIds && appIds[appId][0] === allowed;
161 return value;
162 }
163
164 function waitForService() {
165 const {PermissionsIface} = imports.models.portals;
166 var version = null;
167
168 do {
169 GLib.usleep(1000000);
170
171 const Proxy = Gio.DBusProxy.makeProxyWrapper(PermissionsIface);
172 const proxy = new Proxy(
173 Gio.DBus.session,
174 GLib.getenv('FLATSEAL_PORTAL_BUS_NAME'),
175 '/org/freedesktop/impl/portal/PermissionStore');
176 version = proxy.version; // eslint-disable-line prefer-destructuring
177 } while (version === null);
178 }
179
180 function partialService() {
181 const Proxy = Gio.DBusProxy.makeProxyWrapper(TestPermissionStoreIface);
182
183 const proxy = new Proxy(
184 Gio.DBus.session,
185 GLib.getenv('FLATSEAL_PORTAL_BUS_NAME'),
186 '/org/freedesktop/impl/portal/PermissionStore');
187
188 proxy.testPartialTableSync();
189 }