1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
|
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <gio/gio.h>
#include "portal/flatpak-portal-dbus.h"
GDBusConnection *connection;
const char *portal_name = "org.freedesktop.portal.Flatpak";
const char *portal_path = "/org/freedesktop/portal/Flatpak";
static PortalFlatpakUpdateMonitor *
create_monitor (PortalFlatpak *portal,
GCallback update_available_cb,
gpointer cb_data,
GError **error)
{
static int counter = 1;
PortalFlatpakUpdateMonitor *monitor;
g_autofree char *token = NULL;
g_autofree char *monitor_path = NULL;
g_autofree char *sender = NULL;
g_autofree char *monitor_handle = NULL;
char *s;
GVariantBuilder opt_builder;
sender = g_strdup (g_dbus_connection_get_unique_name (connection) + 1);
while ((s = strchr (sender, '.')) != NULL)
*s = '_';
token = g_strdup_printf ("test_token%d", counter++);
monitor_path = g_strdup_printf ("/org/freedesktop/portal/Flatpak/update_monitor/%s/%s", sender, token);
monitor = portal_flatpak_update_monitor_proxy_new_sync (connection, G_DBUS_PROXY_FLAGS_NONE,
portal_name, monitor_path,
NULL, error);
if (monitor == NULL)
return NULL;
if (update_available_cb)
g_signal_connect (monitor, "update-available", update_available_cb, cb_data);
g_variant_builder_init (&opt_builder, G_VARIANT_TYPE_VARDICT);
g_variant_builder_add (&opt_builder, "{sv}", "handle_token", g_variant_new_string (token));
if (!portal_flatpak_call_create_update_monitor_sync (portal,
g_variant_builder_end (&opt_builder),
&monitor_handle,
NULL, error))
return NULL;
return monitor;
}
static void
update_available (PortalFlatpakUpdateMonitor *object,
GVariant *arg_update_info,
gpointer user_data)
{
const char *running, *local, *remote;
g_variant_lookup (arg_update_info, "running-commit", "&s", &running);
g_variant_lookup (arg_update_info, "local-commit", "&s", &local);
g_variant_lookup (arg_update_info, "remote-commit", "&s", &remote);
g_print ("update_available running=%s local=%s remote=%s\n", running, local, remote);
}
static void
write_status (int res, int status_pipe)
{
char c = res;
write (status_pipe, &c, 1);
}
typedef int (*TestCallback) (PortalFlatpak *portal, int status_pipe);
static int
monitor_test (PortalFlatpak *portal, int status_pipe)
{
g_autoptr(GError) error = NULL;
PortalFlatpakUpdateMonitor *monitor;
GMainLoop *loop;
monitor = create_monitor (portal,
(GCallback)update_available, NULL,
&error);
if (monitor == NULL)
{
g_printerr ("Error creating monitor: %s\n", error->message);
return 1;
}
/* Return 0, to indicate we've successfully started monitor */
write_status (0, status_pipe);
g_print ("Entering main loop waiting for updates\n");
loop = g_main_loop_new (NULL, FALSE);
g_main_loop_run (loop);
return 0;
}
typedef enum {
PROGRESS_STATUS_RUNNING = 0,
PROGRESS_STATUS_EMPTY = 1,
PROGRESS_STATUS_DONE = 2,
PROGRESS_STATUS_ERROR = 3
} UpdateStatus;
typedef struct {
GMainLoop *loop;
int expected_end_status;
int expected_n_ops;
const char *expected_error;
int expected_op;
int exit_status;
} UpdateData;
static void
progress_cb (PortalFlatpakUpdateMonitor *object,
GVariant *arg_info,
UpdateData *data)
{
g_autofree char *args = g_variant_print (arg_info, FALSE);
guint32 op = 0;
guint32 n_ops = 0;
guint32 progress = 0;
guint32 status = 0;
const char *error = "";
const char *error_message = "";
g_variant_lookup (arg_info, "op", "u", &op);
g_variant_lookup (arg_info, "n_ops", "u", &n_ops);
g_variant_lookup (arg_info, "progress", "u", &progress);
g_variant_lookup (arg_info, "status", "u", &status);
g_variant_lookup (arg_info, "error", "&s", &error);
g_variant_lookup (arg_info, "error_message", "&s", &error_message);
g_print ("progress op=%d n_ops=%d progress=%d status=%d error=%s error_message='%s'\n", op, n_ops, progress, status, error, error_message);
if (status == PROGRESS_STATUS_RUNNING)
{
if (n_ops != data->expected_n_ops)
{
g_printerr ("Unexpected number of ops: %d (expected %d)\n", n_ops, data->expected_n_ops);
data->exit_status = 1;
g_main_loop_quit (data->loop);
}
if (op == data->expected_op)
{
if (progress == 100)
data->expected_op = op + 1;
}
else
{
g_printerr ("Unexpected op nr: %d (expected %d)\n", op, data->expected_op);
data->exit_status = 1;
g_main_loop_quit (data->loop);
}
}
else
{
if (data->expected_end_status != status)
{
g_printerr ("Unexpected end status: %d (error %s: %s)\n", status, error, error_message);
data->exit_status = 1;
}
else if (status == PROGRESS_STATUS_DONE)
{
if (data->expected_op != data->expected_n_ops)
{
g_printerr ("Unexpected number of ops seen: %d, should be %d\n", data->expected_op, data->expected_n_ops);
data->exit_status = 1;
}
}
else if (status == PROGRESS_STATUS_ERROR)
{
if (data->expected_error != NULL &&
strcmp (data->expected_error, error) != 0)
{
g_printerr ("Unexpected error: %s, should be %s\n", error, data->expected_error);
data->exit_status = 1;
}
}
g_main_loop_quit (data->loop);
}
}
static int
update_test (PortalFlatpak *portal, int status_pipe)
{
g_autoptr(GError) error = NULL;
PortalFlatpakUpdateMonitor *monitor;
GVariantBuilder opt_builder;
GMainLoop *loop = g_main_loop_new (NULL, FALSE);
UpdateData data = { loop };
monitor = create_monitor (portal, NULL, NULL, &error);
if (monitor == NULL)
{
g_printerr ("Error creating monitor: %s\n", error->message);
return 1;
}
g_variant_builder_init (&opt_builder, G_VARIANT_TYPE_VARDICT);
g_signal_connect (monitor, "progress", G_CALLBACK (progress_cb), &data);
data.expected_end_status = PROGRESS_STATUS_DONE;
data.expected_n_ops = 2;
if (!portal_flatpak_update_monitor_call_update_sync (monitor, "",
g_variant_builder_end (&opt_builder),
NULL, &error))
{
g_printerr ("Error calling update: %s\n", error->message);
return 1;
}
g_main_loop_run (loop);
return data.exit_status;
}
static int
update_null_test (PortalFlatpak *portal, int status_pipe)
{
g_autoptr(GError) error = NULL;
PortalFlatpakUpdateMonitor *monitor;
GVariantBuilder opt_builder;
GMainLoop *loop = g_main_loop_new (NULL, FALSE);
UpdateData data = { loop };
monitor = create_monitor (portal, NULL, NULL, &error);
if (monitor == NULL)
{
g_printerr ("Error creating monitor: %s\n", error->message);
return 1;
}
g_variant_builder_init (&opt_builder, G_VARIANT_TYPE_VARDICT);
g_signal_connect (monitor, "progress", G_CALLBACK (progress_cb), &data);
data.expected_end_status = PROGRESS_STATUS_EMPTY;
data.expected_n_ops = 0;
if (!portal_flatpak_update_monitor_call_update_sync (monitor, "",
g_variant_builder_end (&opt_builder),
NULL, &error))
{
g_printerr ("Error calling update: %s\n", error->message);
return 1;
}
g_main_loop_run (loop);
return data.exit_status;
}
static int
update_fail_test (PortalFlatpak *portal, int status_pipe)
{
g_autoptr(GError) error = NULL;
PortalFlatpakUpdateMonitor *monitor;
GVariantBuilder opt_builder;
GMainLoop *loop = g_main_loop_new (NULL, FALSE);
UpdateData data = { loop };
monitor = create_monitor (portal, NULL, NULL, &error);
if (monitor == NULL)
{
g_printerr ("Error creating monitor: %s\n", error->message);
return 1;
}
g_variant_builder_init (&opt_builder, G_VARIANT_TYPE_VARDICT);
g_signal_connect (monitor, "progress", G_CALLBACK (progress_cb), &data);
data.expected_end_status = PROGRESS_STATUS_ERROR;
data.expected_n_ops = 2;
if (!portal_flatpak_update_monitor_call_update_sync (monitor, "",
g_variant_builder_end (&opt_builder),
NULL, &error))
{
g_printerr ("Error calling update: %s\n", error->message);
return 1;
}
g_main_loop_run (loop);
return data.exit_status;
}
static int
update_notsupp_test (PortalFlatpak *portal, int status_pipe)
{
g_autoptr(GError) error = NULL;
PortalFlatpakUpdateMonitor *monitor;
GVariantBuilder opt_builder;
GMainLoop *loop = g_main_loop_new (NULL, FALSE);
UpdateData data = { loop };
monitor = create_monitor (portal, NULL, NULL, &error);
if (monitor == NULL)
{
g_printerr ("Error creating monitor: %s\n", error->message);
return 1;
}
g_variant_builder_init (&opt_builder, G_VARIANT_TYPE_VARDICT);
g_signal_connect (monitor, "progress", G_CALLBACK (progress_cb), &data);
data.expected_end_status = PROGRESS_STATUS_ERROR;
data.expected_n_ops = 2;
data.expected_error = "org.freedesktop.DBus.Error.NotSupported";
if (!portal_flatpak_update_monitor_call_update_sync (monitor, "",
g_variant_builder_end (&opt_builder),
NULL, &error))
{
g_printerr ("Error calling update: %s\n", error->message);
return 1;
}
g_main_loop_run (loop);
return data.exit_status;
}
static int
run_test (int status_pipe, const char *pidfile, TestCallback test)
{
g_autoptr(GError) error = NULL;
PortalFlatpak *portal;
g_autofree char *pid = NULL;
pid = g_strdup_printf ("%d", getpid ());
if (!g_file_set_contents (pidfile, pid, -1, &error))
{
g_printerr ("Error creating pidfile: %s\n", error->message);
return 1;
}
connection = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
if (connection == NULL)
{
g_printerr ("Error connecting: %s\n", error->message);
return 1;
}
portal = portal_flatpak_proxy_new_sync (connection, G_DBUS_PROXY_FLAGS_NONE,
portal_name, portal_path,
NULL, &error);
if (portal == NULL)
{
g_printerr ("Error creating proxy: %s\n", error->message);
return 1;
}
return test (portal, status_pipe);
}
int
main (int argc, char *argv[])
{
pid_t pid;
int pipes[2];
TestCallback test_callback;
if (argc < 2)
{
g_printerr ("No test command specified");
return 1;
}
if (strcmp (argv[1], "monitor") == 0)
test_callback = monitor_test;
else if (strcmp (argv[1], "update") == 0)
test_callback = update_test;
else if (strcmp (argv[1], "update-null") == 0)
test_callback = update_null_test;
else if (strcmp (argv[1], "update-fail") == 0)
test_callback = update_fail_test;
else if (strcmp (argv[1], "update-notsupp") == 0)
test_callback = update_notsupp_test;
else
{
g_printerr ("Unknown command %s specified\n", argv[1]);
return 1;
}
if (pipe (pipes) != 0)
{
perror ("pipe:");
return 1;
}
pid = fork();
if (pid == -1)
{
perror ("pipe:");
return 1;
}
if (pid != 0)
{
char c;
/* parent */
close (pipes[1]);
if (read (pipes[0], &c, 1) != 1)
return 1;
return c;
}
else
{
int res;
close (pipes[0]);
res = run_test (pipes[1], argc > 2 ? argv[2] : "pid.out", test_callback);
/* If this returned early we have some setup failure, report it */
write_status (res, pipes[1]);
exit (res);
}
}
|