Debian Codegraph @

testsuite/test-dbus.c

raw
1 /* test-dbus.c
2 *
3 * This library is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU Lesser General Public License as
5 * published by the Free Software Foundation; either version 2.1 of the
6 * License, or (at your option) any later version.
7 *
8 * This library is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 * SPDX-License-Identifier: LGPL-2.1-or-later
17 */
18
19 #include "config.h"
20
21 #include <libdex.h>
22 #include <stdint.h>
23
24 #include "dex-fiber-private.h"
25 #include "dex-test-dbus-foo.h"
26
27 struct _DexTestFoo
28 {
29 DexTestDbusFooSkeleton parent_instance;
30 };
31
32 struct _DexTestFooClass
33 {
34 DexTestDbusFooSkeletonClass parent_class;
35 };
36
37 static void dex_test_dbus_foo_iface_init (DexTestDbusFooIface *iface);
38
39 #define DEX_TEST_TYPE_FOO (dex_test_foo_get_type ())
40 G_DECLARE_FINAL_TYPE (DexTestFoo,
41 dex_test_foo,
42 DEX_TEST, FOO,
43 DexTestDbusFooSkeleton)
44
45 G_DEFINE_TYPE_WITH_CODE (DexTestFoo,
46 dex_test_foo,
47 DEX_TEST_DBUS_TYPE_FOO_SKELETON,
48 G_IMPLEMENT_INTERFACE (DEX_TEST_DBUS_TYPE_FOO,
49 dex_test_dbus_foo_iface_init));
50
51 static GDBusConnection *
52 get_new_session_connection_sync (void)
53 {
54 GDBusConnection *connection;
55 char *session_address;
56 GError *error = NULL;
57
58 session_address = g_dbus_address_get_for_bus_sync (G_BUS_TYPE_SESSION, NULL, &error);
59 g_assert_no_error (error);
60 g_assert_nonnull (session_address);
61
62 connection = g_dbus_connection_new_for_address_sync (session_address,
63 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT |
64 G_DBUS_CONNECTION_FLAGS_MESSAGE_BUS_CONNECTION,
65 NULL, NULL, &error);
66 g_assert_no_error (error);
67 g_assert_nonnull (connection);
68
69 g_free (session_address);
70 return connection;
71 }
72
73 static gboolean
74 handle_foo (DexTestDbusFoo *object,
75 GDBusMethodInvocation *invocation)
76 {
77 g_usleep (G_USEC_PER_SEC / 10);
78
79 dex_test_dbus_foo_complete_foo (object, invocation);
80
81 return G_DBUS_METHOD_INVOCATION_HANDLED;
82 }
83
84 static gboolean
85 handle_bar (DexTestDbusFoo *object,
86 GDBusMethodInvocation *invocation,
87 const char * const *i1,
88 uint32_t i2)
89 {
90 GVariantBuilder builder =
91 G_VARIANT_BUILDER_INIT (G_VARIANT_TYPE_VARDICT);
92
93 g_usleep (G_USEC_PER_SEC / 10);
94
95 g_variant_builder_add (&builder, "{sv}", "foo", g_variant_new_uint32 (3));
96 g_variant_builder_add (&builder, "{sv}", "bar", g_variant_new_boolean (FALSE));
97
98 dex_test_dbus_foo_complete_bar (object, invocation, 42, g_variant_builder_end (&builder));
99
100 return G_DBUS_METHOD_INVOCATION_HANDLED;
101 }
102
103 static gboolean
104 handle_emit_foo_bar (DexTestDbusFoo *object,
105 GDBusMethodInvocation *invocation)
106 {
107 dex_test_dbus_foo_emit_foo_bar (object);
108 dex_test_dbus_foo_complete_emit_foo_bar (object, invocation);
109
110 return G_DBUS_METHOD_INVOCATION_HANDLED;
111 }
112
113 static gboolean
114 handle_fiber (DexTestDbusFoo *object,
115 GDBusMethodInvocation *invocation)
116 {
117 dex_await (dex_timeout_new_msec (100), NULL);
118 dex_test_dbus_foo_complete_fiber (object, invocation);
119
120 return G_DBUS_METHOD_INVOCATION_HANDLED;
121 }
122
123 static void
124 dex_test_dbus_foo_iface_init (DexTestDbusFooIface *iface)
125 {
126 iface->handle_foo = handle_foo;
127 iface->handle_bar = handle_bar;
128 iface->handle_emit_foo_bar = handle_emit_foo_bar;
129 iface->handle_fiber = handle_fiber;
130 }
131
132 static void
133 dex_test_foo_init (DexTestFoo *foo G_GNUC_UNUSED)
134 {
135 }
136
137 static void
138 dex_test_foo_class_init (DexTestFooClass *klass G_GNUC_UNUSED)
139 {
140 }
141
142 typedef struct _FooServiceData
143 {
144 GMainLoop *main_loop;
145 DexTestFoo *foo;
146 guint own_name_id;
147 guint timeout_id;
148 gboolean acquired;
149 } FooServiceData;
150
151 static gboolean
152 send_signal_cb (gpointer user_data)
153 {
154 FooServiceData *data = user_data;
155 GVariantBuilder builder =
156 G_VARIANT_BUILDER_INIT (G_VARIANT_TYPE_VARDICT);
157
158 g_variant_builder_add (&builder, "{sv}", "foo", g_variant_new_uint32 (3));
159 g_variant_builder_add (&builder, "{sv}", "bar", g_variant_new_boolean (FALSE));
160
161 dex_test_dbus_foo_emit_baz (DEX_TEST_DBUS_FOO (data->foo), 11, g_variant_builder_end (&builder));
162
163 return G_SOURCE_CONTINUE;
164 }
165
166 static void
167 bus_name_acquired_cb (GDBusConnection *connection,
168 const gchar *name,
169 gpointer user_data)
170 {
171 FooServiceData *data = user_data;
172 DexTestFoo *foo = data->foo;
173 GError *error = NULL;
174
175 g_assert_true (g_dbus_interface_skeleton_export (G_DBUS_INTERFACE_SKELETON (foo),
176 connection,
177 "/org/example/foo",
178 &error));
179 g_assert_no_error (error);
180
181 data->timeout_id = g_timeout_add (100, send_signal_cb, data);
182
183 data->acquired = TRUE;
184 }
185
186 static void
187 bus_name_lost_cb (GDBusConnection *connection,
188 const gchar *name,
189 gpointer user_data)
190 {
191 FooServiceData *data = user_data;
192
193 g_main_loop_quit (data->main_loop);
194 }
195
196 static void
197 foo_service_cancelled_cb (GCancellable *cancellable,
198 gpointer user_data)
199 {
200 FooServiceData *data = user_data;
201
202 g_clear_handle_id (&data->own_name_id, g_bus_unown_name);
203 g_main_loop_quit (data->main_loop);
204 }
205
206 static void
207 run_foo_service_in_thread (GTask *task,
208 gpointer source_object,
209 gpointer task_data,
210 GCancellable *cancellable)
211 {
212 FooServiceData data;
213 GMainLoop *main_loop;
214 GMainContext *main_context;
215 GDBusConnection *connection;
216 DexTestFoo *foo;
217 GError *error = NULL;
218
219 main_context = g_main_context_new ();
220 main_loop = g_main_loop_new (main_context, FALSE);
221 g_assert_nonnull (main_loop);
222
223 foo = g_object_new (DEX_TEST_TYPE_FOO, NULL);
224 g_assert_nonnull (foo);
225
226 data.main_loop = main_loop;
227 data.foo = foo;
228 data.acquired = FALSE;
229 data.own_name_id = 0;
230 data.timeout_id = 0;
231
232 connection = get_new_session_connection_sync ();
233 g_assert_no_error (error);
234 g_assert_nonnull (connection);
235
236 data.own_name_id =
237 g_bus_own_name_on_connection (connection, "org.example.Foo",
238 G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT |
239 G_BUS_NAME_OWNER_FLAGS_REPLACE,
240 bus_name_acquired_cb,
241 bus_name_lost_cb,
242 &data, NULL);
243
244 g_cancellable_connect (cancellable,
245 G_CALLBACK (foo_service_cancelled_cb),
246 &data, NULL);
247 if (!g_cancellable_is_cancelled (cancellable))
248 g_main_loop_run (main_loop);
249
250 if (data.acquired)
251 g_dbus_interface_skeleton_unexport (G_DBUS_INTERFACE_SKELETON (data.foo));
252
253 g_clear_handle_id (&data.timeout_id, g_source_remove);
254
255 g_main_loop_unref (main_loop);
256 g_main_context_unref (main_context);
257 g_clear_object (&foo);
258 g_clear_object (&connection);
259
260 g_task_return_boolean (task, TRUE);
261 }
262
263 static void
264 run_foo_service_cancelled_cb (GObject *source_object,
265 GAsyncResult *res,
266 gpointer data)
267 {
268 GTask *task = G_TASK (res);
269 GCancellable *cancellable;
270
271 cancellable = g_task_get_task_data (task);
272 g_task_set_task_data (task, NULL, NULL);
273 g_clear_object (&cancellable);
274 }
275
276 static GTask *
277 run_foo_service (void)
278 {
279 GTask *task = NULL;
280 GCancellable *cancellable = g_cancellable_new ();
281
282 task = g_task_new (NULL, cancellable, run_foo_service_cancelled_cb, NULL);
283 g_task_set_task_data (task, cancellable, NULL);
284 g_task_run_in_thread (task, run_foo_service_in_thread);
285
286 return task;
287 }
288
289 static void
290 stop_foo_service (GTask *task)
291 {
292 GCancellable *cancellable;
293
294 cancellable = g_task_get_task_data (task);
295 g_cancellable_cancel (cancellable);
296
297 while (g_task_get_task_data (task) != NULL)
298 g_main_context_iteration (NULL, TRUE);
299
300 g_clear_object (&task);
301 }
302
303 static void
304 test_gdbus_proxy_create (void)
305 {
306 GDBusConnection *connection;
307 DexFuture *future;
308 const GValue *value;
309 DexTestDbusFoo *proxy;
310 GError *error = NULL;
311
312 connection = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
313 g_assert_no_error (error);
314 g_assert_nonnull (connection);
315
316 future = dex_test_dbus_foo_proxy_new_future (connection,
317 G_DBUS_PROXY_FLAGS_NONE,
318 "org.example.Foo",
319 "/org/example/foo");
320
321 while (dex_future_get_status (future) == DEX_FUTURE_STATUS_PENDING)
322 g_main_context_iteration (NULL, TRUE);
323
324 value = dex_future_get_value (future, &error);
325 g_assert_no_error (error);
326 g_assert_nonnull (value);
327 g_assert_true (G_VALUE_HOLDS_OBJECT (value));
328 proxy = g_value_get_object (value);
329 g_assert_true (DEX_TEST_DBUS_IS_FOO (proxy));
330
331 g_clear_error (&error);
332 dex_clear (&future);
333 g_clear_object (&connection);
334 }
335
336 static void
337 test_gdbus_method_call_simple (void)
338 {
339 GDBusConnection *connection;
340 DexFuture *future;
341 const GValue *value;
342 DexTestDbusFoo *proxy;
343 GError *error = NULL;
344
345 connection = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
346 g_assert_no_error (error);
347 g_assert_nonnull (connection);
348
349 proxy = dex_test_dbus_foo_proxy_new_sync (connection,
350 G_DBUS_PROXY_FLAGS_NONE,
351 "org.example.Foo",
352 "/org/example/foo",
353 NULL, &error);
354 g_assert_no_error (error);
355 g_assert_nonnull (proxy);
356
357 future = dex_test_dbus_foo_call_foo_future (proxy);
358
359 while (dex_future_get_status (future) == DEX_FUTURE_STATUS_PENDING)
360 g_main_context_iteration (NULL, TRUE);
361
362 value = dex_future_get_value (future, &error);
363 g_assert_error (error, G_DBUS_ERROR, G_DBUS_ERROR_SERVICE_UNKNOWN);
364 g_assert_null (value);
365
366 g_clear_error (&error);
367 dex_clear (&future);
368 g_clear_object (&proxy);
369 g_clear_object (&connection);
370 }
371
372 static void
373 test_gdbus_method_call_result (void)
374 {
375 GDBusConnection *connection;
376 DexFuture *future;
377 const GValue *value;
378 DexTestDbusFoo *proxy;
379 GTask *foo_service;
380 GError *error = NULL;
381 char *name;
382
383 foo_service = run_foo_service ();
384
385 connection = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
386 g_assert_no_error (error);
387 g_assert_nonnull (connection);
388
389 proxy = dex_test_dbus_foo_proxy_new_sync (connection,
390 G_DBUS_PROXY_FLAGS_NONE,
391 "org.example.Foo",
392 "/org/example/foo",
393 NULL, &error);
394 g_assert_no_error (error);
395 g_assert_nonnull (proxy);
396
397 while (!(name = g_dbus_proxy_get_name_owner (G_DBUS_PROXY (proxy))))
398 g_main_context_iteration (NULL, TRUE);
399 g_free (name);
400
401 future = dex_test_dbus_foo_call_foo_future (proxy);
402
403 while (dex_future_get_status (future) == DEX_FUTURE_STATUS_PENDING)
404 g_main_context_iteration (NULL, TRUE);
405
406 value = dex_future_get_value (future, &error);
407 g_assert_no_error (error);
408 g_assert_nonnull (value);
409 g_assert_true (G_VALUE_HOLDS_BOOLEAN (value));
410 g_assert_true (g_value_get_boolean (value));
411
412 g_clear_error (&error);
413 dex_clear (&future);
414 g_clear_object (&proxy);
415 g_clear_object (&connection);
416 stop_foo_service (foo_service);
417 }
418
419 static void
420 test_gdbus_method_call_cancel (void)
421 {
422 GDBusConnection *connection;
423 DexFuture *future;
424 DexPromise *promise;
425 const GValue *value;
426 DexTestDbusFoo *proxy;
427 GTask *foo_service;
428 GError *error = NULL;
429 char *name;
430
431 foo_service = run_foo_service ();
432
433 connection = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
434 g_assert_no_error (error);
435 g_assert_nonnull (connection);
436
437 proxy = dex_test_dbus_foo_proxy_new_sync (connection,
438 G_DBUS_PROXY_FLAGS_NONE,
439 "org.example.Foo",
440 "/org/example/foo",
441 NULL, &error);
442 g_assert_no_error (error);
443 g_assert_nonnull (proxy);
444
445 while (!(name = g_dbus_proxy_get_name_owner (G_DBUS_PROXY (proxy))))
446 g_main_context_iteration (NULL, TRUE);
447 g_free (name);
448
449 promise = dex_promise_new ();
450 future = dex_future_first (DEX_FUTURE (promise),
451 dex_test_dbus_foo_call_foo_future (proxy),
452 NULL);
453
454 dex_promise_reject (promise, g_error_new_literal (G_IO_ERROR, G_IO_ERROR_CANCELLED, "Cancelled"));
455
456 while (dex_future_get_status (future) == DEX_FUTURE_STATUS_PENDING)
457 g_main_context_iteration (NULL, TRUE);
458
459 value = dex_future_get_value (future, &error);
460 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_CANCELLED);
461 g_assert_null (value);
462
463 g_clear_error (&error);
464 dex_clear (&future);
465 g_clear_object (&proxy);
466 g_clear_object (&connection);
467 stop_foo_service (foo_service);
468 }
469
470 static void
471 test_gdbus_method_call_complex (void)
472 {
473 GTask *foo_service;
474 GDBusConnection *connection;
475 DexTestDbusFoo *proxy;
476 DexFuture *future;
477 const GValue *value;
478 DexTestDbusFooBarResult *result;
479 GError *error = NULL;
480 char *name;
481
482 foo_service = run_foo_service ();
483
484 connection = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
485 g_assert_no_error (error);
486 g_assert_nonnull (connection);
487
488 proxy = dex_test_dbus_foo_proxy_new_sync (connection,
489 G_DBUS_PROXY_FLAGS_NONE,
490 "org.example.Foo",
491 "/org/example/foo",
492 NULL, &error);
493 g_assert_no_error (error);
494 g_assert_nonnull (proxy);
495
496 while (!(name = g_dbus_proxy_get_name_owner (G_DBUS_PROXY (proxy))))
497 g_main_context_iteration (NULL, TRUE);
498 g_free (name);
499
500 future = dex_test_dbus_foo_call_bar_future (proxy,
501 (const gchar * []) {
502 "foo",
503 "bar",
504 NULL,
505 },
506 42);
507
508 while (dex_future_get_status (future) == DEX_FUTURE_STATUS_PENDING)
509 g_main_context_iteration (NULL, TRUE);
510
511 value = dex_future_get_value (future, &error);
512 g_assert_no_error (error);
513 g_assert_nonnull (value);
514 g_assert_true (G_VALUE_HOLDS_BOXED (value));
515 result = g_value_get_boxed (value);
516
517 {
518 uint32_t foo;
519 gboolean bar;
520
521 g_assert_nonnull (result);
522 g_assert_cmpint (result->o1, == , 42);
523
524 g_assert_nonnull (result->o2);
525 g_assert_true (g_variant_lookup (result->o2, "foo", "u", &foo));
526 g_assert_cmpint (foo, == , 3);
527 g_assert_true (g_variant_lookup (result->o2, "bar", "b", &bar));
528 g_assert_true (!bar);
529 }
530
531 g_clear_error (&error);
532 dex_clear (&future);
533 g_clear_object (&proxy);
534 g_clear_object (&connection);
535 stop_foo_service (foo_service);
536 }
537
538 static void
539 test_gdbus_signal_wait_simple (void)
540 {
541 GTask *foo_service;
542 GDBusConnection *connection;
543 DexTestDbusFoo *proxy;
544 DexFuture *future;
545 const GValue *value;
546 DexTestDbusFooBazSignal *result;
547 GError *error = NULL;
548 char *name;
549
550 foo_service = run_foo_service ();
551
552 connection = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
553 g_assert_no_error (error);
554 g_assert_nonnull (connection);
555
556 proxy = dex_test_dbus_foo_proxy_new_sync (connection,
557 G_DBUS_PROXY_FLAGS_NONE,
558 "org.example.Foo",
559 "/org/example/foo",
560 NULL, &error);
561 g_assert_no_error (error);
562 g_assert_nonnull (proxy);
563
564 while (!(name = g_dbus_proxy_get_name_owner (G_DBUS_PROXY (proxy))))
565 g_main_context_iteration (NULL, TRUE);
566 g_free (name);
567
568 future = dex_test_dbus_foo_wait_baz_future (proxy);
569
570 while (dex_future_get_status (future) == DEX_FUTURE_STATUS_PENDING)
571 g_main_context_iteration (NULL, TRUE);
572
573 value = dex_future_get_value (future, &error);
574 g_assert_no_error (error);
575 g_assert_nonnull (value);
576 g_assert_true (G_VALUE_HOLDS_BOXED (value));
577 result = g_value_get_boxed (value);
578
579 {
580 uint32_t foo;
581 gboolean bar;
582
583 g_assert_nonnull (result);
584 g_assert_cmpint (result->s1, == , 11);
585
586 g_assert_nonnull (result->s2);
587 g_assert_true (g_variant_lookup (result->s2, "foo", "u", &foo));
588 g_assert_cmpint (foo, == , 3);
589 g_assert_true (g_variant_lookup (result->s2, "bar", "b", &bar));
590 g_assert_true (!bar);
591 }
592
593 g_clear_error (&error);
594 dex_clear (&future);
595 g_clear_object (&proxy);
596 g_clear_object (&connection);
597 stop_foo_service (foo_service);
598 }
599
600 static void
601 test_gdbus_signal_wait_cancel (void)
602 {
603 GTask *foo_service;
604 GDBusConnection *connection;
605 DexTestDbusFoo *proxy;
606 DexPromise *promise;
607 DexFuture *future;
608 const GValue *value;
609 GError *error = NULL;
610 char *name;
611
612 foo_service = run_foo_service ();
613
614 connection = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
615 g_assert_no_error (error);
616 g_assert_nonnull (connection);
617
618 proxy = dex_test_dbus_foo_proxy_new_sync (connection,
619 G_DBUS_PROXY_FLAGS_NONE,
620 "org.example.Foo",
621 "/org/example/foo",
622 NULL, &error);
623 g_assert_no_error (error);
624 g_assert_nonnull (proxy);
625
626 while (!(name = g_dbus_proxy_get_name_owner (G_DBUS_PROXY (proxy))))
627 g_main_context_iteration (NULL, TRUE);
628 g_free (name);
629
630 promise = dex_promise_new ();
631 future = dex_future_first (DEX_FUTURE (promise),
632 dex_test_dbus_foo_wait_baz_future (proxy),
633 NULL);
634
635 dex_promise_reject (promise, g_error_new_literal (G_IO_ERROR, G_IO_ERROR_CANCELLED, "Cancelled"));
636
637 while (dex_future_get_status (future) == DEX_FUTURE_STATUS_PENDING)
638 g_main_context_iteration (NULL, TRUE);
639
640 value = dex_future_get_value (future, &error);
641 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_CANCELLED);
642 g_assert_null (value);
643
644 g_clear_error (&error);
645 dex_clear (&future);
646 g_clear_object (&proxy);
647 g_clear_object (&connection);
648 stop_foo_service (foo_service);
649 }
650
651 static void
652 test_gdbus_signal_monitor_basic (void)
653 {
654 GTask *foo_service;
655 GDBusConnection *connection;
656 DexTestDbusFoo *proxy;
657 DexFuture *future;
658 const GValue *value;
659 DexTestDbusFooSignalMonitor *signal_monitor;
660 GError *error = NULL;
661 char *name;
662
663 foo_service = run_foo_service ();
664
665 connection = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
666 g_assert_no_error (error);
667 g_assert_nonnull (connection);
668
669 proxy = dex_test_dbus_foo_proxy_new_sync (connection,
670 G_DBUS_PROXY_FLAGS_NONE,
671 "org.example.Foo",
672 "/org/example/foo",
673 NULL, &error);
674 g_assert_no_error (error);
675 g_assert_nonnull (proxy);
676
677 while (!(name = g_dbus_proxy_get_name_owner (G_DBUS_PROXY (proxy))))
678 g_main_context_iteration (NULL, TRUE);
679 g_free (name);
680
681 signal_monitor = dex_test_dbus_foo_signal_monitor_new (proxy, DEX_TEST_DBUS_FOO_SIGNAL_FOO_BAR);
682
683 future = dex_test_dbus_foo_call_emit_foo_bar_future (proxy);
684
685 while (dex_future_get_status (future) == DEX_FUTURE_STATUS_PENDING)
686 g_main_context_iteration (NULL, TRUE);
687 dex_clear (&future);
688
689 future = dex_test_dbus_foo_signal_monitor_next_foo_bar (signal_monitor);
690 g_assert_true (dex_future_get_status (future) == DEX_FUTURE_STATUS_RESOLVED);
691
692 value = dex_future_get_value (future, &error);
693 g_assert_no_error (error);
694 g_assert_nonnull (value);
695 g_assert_true (G_VALUE_HOLDS_BOOLEAN (value));
696 g_assert_true (g_value_get_boolean (value));
697
698 dex_clear (&future);
699 future = dex_test_dbus_foo_signal_monitor_next_foo_bar (signal_monitor);
700 g_assert_true (dex_future_get_status (future) == DEX_FUTURE_STATUS_PENDING);
701
702 g_clear_error (&error);
703 dex_clear (&future);
704 g_clear_object (&signal_monitor);
705 g_clear_object (&proxy);
706 g_clear_object (&connection);
707 stop_foo_service (foo_service);
708 }
709
710 static void
711 test_gdbus_signal_monitor_cancel (void)
712 {
713 GTask *foo_service;
714 GDBusConnection *connection;
715 DexTestDbusFoo *proxy;
716 DexFuture *future;
717 DexPromise *promise;
718 const GValue *value;
719 DexTestDbusFooSignalMonitor *signal_monitor;
720 GError *error = NULL;
721 char *name;
722
723 foo_service = run_foo_service ();
724
725 connection = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
726 g_assert_no_error (error);
727 g_assert_nonnull (connection);
728
729 proxy = dex_test_dbus_foo_proxy_new_sync (connection,
730 G_DBUS_PROXY_FLAGS_NONE,
731 "org.example.Foo",
732 "/org/example/foo",
733 NULL, &error);
734 g_assert_no_error (error);
735 g_assert_nonnull (proxy);
736
737 while (!(name = g_dbus_proxy_get_name_owner (G_DBUS_PROXY (proxy))))
738 g_main_context_iteration (NULL, TRUE);
739 g_free (name);
740
741 signal_monitor = dex_test_dbus_foo_signal_monitor_new (proxy, DEX_TEST_DBUS_FOO_SIGNAL_FOO_BAR);
742
743 promise = dex_promise_new ();
744 future = dex_future_first (DEX_FUTURE (promise),
745 dex_test_dbus_foo_signal_monitor_next_foo_bar (signal_monitor),
746 NULL);
747
748 dex_promise_reject (promise, g_error_new_literal (G_IO_ERROR, G_IO_ERROR_CANCELLED, "Cancelled"));
749
750 while (dex_future_get_status (future) == DEX_FUTURE_STATUS_PENDING)
751 g_main_context_iteration (NULL, TRUE);
752
753 value = dex_future_get_value (future, &error);
754 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_CANCELLED);
755 g_assert_null (value);
756
757 g_clear_error (&error);
758 dex_clear (&future);
759 g_clear_object (&signal_monitor);
760 g_clear_object (&proxy);
761 g_clear_object (&connection);
762 stop_foo_service (foo_service);
763 }
764
765 static DexFuture *
766 fiber_basic (gpointer user_data)
767 {
768 GDBusConnection *connection = NULL;
769 DexTestDbusFoo *proxy = NULL;
770 GError *error = NULL;
771
772 connection = dex_await_object (dex_bus_get (G_BUS_TYPE_SESSION), &error);
773 g_assert_no_error (error);
774 g_assert_nonnull (connection);
775
776 proxy = dex_await_object (dex_test_dbus_foo_proxy_new_future (connection,
777 G_DBUS_PROXY_FLAGS_NONE,
778 "org.example.Foo",
779 "/org/example/foo"),
780 &error);
781 g_assert_no_error (error);
782 g_assert_nonnull (proxy);
783
784 {
785 gboolean foo_result;
786
787 foo_result = dex_await_boolean (dex_test_dbus_foo_call_foo_future (proxy), &error);
788 g_assert_no_error (error);
789 g_assert_true (foo_result);
790 }
791
792 {
793 DexTestDbusFooBazSignal *baz_result;
794 uint32_t foo;
795 gboolean bar;
796
797 baz_result = dex_await_boxed (dex_test_dbus_foo_wait_baz_future (proxy), &error);
798 g_assert_no_error (error);
799 g_assert_nonnull (baz_result);
800
801 g_assert_cmpint (baz_result->s1, == , 11);
802 g_assert_nonnull (baz_result->s2);
803 g_assert_true (g_variant_lookup (baz_result->s2, "foo", "u", &foo));
804 g_assert_cmpint (foo, == , 3);
805 g_assert_true (g_variant_lookup (baz_result->s2, "bar", "b", &bar));
806 g_assert_true (!bar);
807
808 g_clear_pointer (&baz_result, dex_test_dbus_foo_baz_signal_free);
809 }
810
811 {
812 DexTestDbusFooSignalMonitor *signal_monitor;
813 gboolean result;
814
815 signal_monitor = dex_test_dbus_foo_signal_monitor_new (proxy, DEX_TEST_DBUS_FOO_SIGNAL_FOO_BAR);
816
817 result = dex_await_boolean (dex_test_dbus_foo_call_emit_foo_bar_future (proxy), &error);
818 g_assert_no_error (error);
819 g_assert_true (result);
820
821 result = dex_await_boolean (dex_test_dbus_foo_signal_monitor_next_foo_bar (signal_monitor), &error);
822 g_assert_no_error (error);
823 g_assert_true (result);
824
825 g_clear_object (&signal_monitor);
826 }
827
828 return dex_future_new_true ();
829 }
830
831 static void
832 test_gdbus_fiber_basic (void)
833 {
834 GTask *foo_service;
835 DexFuture *future;
836 const GValue *value;
837 GError *error = NULL;
838
839 foo_service = run_foo_service ();
840
841 future = dex_scheduler_spawn (NULL, 0, fiber_basic, NULL, NULL);
842 while (dex_future_get_status (future) == DEX_FUTURE_STATUS_PENDING)
843 g_main_context_iteration (NULL, TRUE);
844
845 value = dex_future_get_value (future, &error);
846 g_assert_no_error (error);
847 g_assert_nonnull (value);
848 g_assert_true (G_VALUE_HOLDS_BOOLEAN (value));
849 g_assert_true (g_value_get_boolean (value));
850
851 g_clear_error (&error);
852 dex_clear (&future);
853 stop_foo_service (foo_service);
854 }
855
856 static DexFuture *
857 fiber_service (gpointer user_data)
858 {
859 GDBusConnection *connection = NULL;
860 DexTestFoo *foo = NULL;
861 DexFuture *name_acquired = NULL;
862 DexFuture *name_lost = NULL;
863 GError *error = NULL;
864
865 connection = dex_await_object (dex_bus_get (G_BUS_TYPE_SESSION), &error);
866 g_assert_no_error (error);
867 g_assert_nonnull (connection);
868
869 dex_bus_own_name_on_connection (connection,
870 "org.example.Foo",
871 G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT |
872 G_BUS_NAME_OWNER_FLAGS_REPLACE,
873 &name_acquired,
874 &name_lost);
875
876 dex_await (g_steal_pointer (&name_acquired), &error);
877 g_assert_no_error (error);
878
879 foo = g_object_new (DEX_TEST_TYPE_FOO, NULL);
880 dex_dbus_interface_skeleton_set_flags (DEX_DBUS_INTERFACE_SKELETON (foo),
881 DEX_DBUS_INTERFACE_SKELETON_FLAGS_HANDLE_METHOD_INVOCATIONS_IN_FIBER);
882
883 g_assert_true (g_dbus_interface_skeleton_export (G_DBUS_INTERFACE_SKELETON (foo),
884 connection,
885 "/org/example/foo",
886 &error));
887 g_assert_no_error (error);
888
889 dex_await (g_steal_pointer (&name_lost), &error);
890 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_FAILED);
891
892 g_dbus_interface_skeleton_unexport (G_DBUS_INTERFACE_SKELETON (foo));
893 dex_dbus_interface_skeleton_cancel (DEX_DBUS_INTERFACE_SKELETON (foo));
894
895 return dex_future_new_true ();
896 }
897
898 static void
899 call_fiber_cb (GObject *source_object,
900 GAsyncResult *res,
901 gpointer data)
902 {
903 gboolean *done = data;
904 GError *error = NULL;
905
906 g_assert_true (dex_test_dbus_foo_call_fiber_finish (DEX_TEST_DBUS_FOO (source_object), res, &error));
907 g_assert_no_error (error);
908
909 *done = TRUE;
910 }
911
912 static void
913 test_gdbus_fiber_service (void)
914 {
915 GTask *foo_service;
916 GDBusConnection *connection;
917 DexTestDbusFoo *proxy;
918 DexFuture *future;
919 const GValue *value;
920 GError *error = NULL;
921 char *name;
922
923 future = dex_scheduler_spawn (NULL, 0, fiber_service, NULL, NULL);
924
925 connection = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
926 g_assert_no_error (error);
927 g_assert_nonnull (connection);
928
929 proxy = dex_test_dbus_foo_proxy_new_sync (connection,
930 G_DBUS_PROXY_FLAGS_NONE,
931 "org.example.Foo",
932 "/org/example/foo",
933 NULL, &error);
934 g_assert_no_error (error);
935 g_assert_nonnull (proxy);
936
937 while (!(name = g_dbus_proxy_get_name_owner (G_DBUS_PROXY (proxy))))
938 g_main_context_iteration (NULL, TRUE);
939 g_free (name);
940
941 {
942 gboolean done = FALSE;
943 dex_test_dbus_foo_call_fiber (proxy, NULL, call_fiber_cb, &done);
944
945 while (!done)
946 g_main_context_iteration (NULL, TRUE);
947 }
948
949 /* kick the fiber service off the bus */
950 foo_service = run_foo_service ();
951
952 while (dex_future_get_status (future) == DEX_FUTURE_STATUS_PENDING)
953 g_main_context_iteration (NULL, TRUE);
954
955 value = dex_future_get_value (future, &error);
956 g_assert_no_error (error);
957 g_assert_nonnull (value);
958 g_assert_true (G_VALUE_HOLDS_BOOLEAN (value));
959 g_assert_true (g_value_get_boolean (value));
960
961 g_clear_error (&error);
962 dex_clear (&future);
963 stop_foo_service (foo_service);
964 }
965
966 int
967 main (int argc,
968 char *argv[])
969 {
970 dex_init ();
971 g_test_init (&argc, &argv, NULL);
972 g_test_add_func ("/Dex/TestSuite/GDBus/proxy/create", test_gdbus_proxy_create);
973 g_test_add_func ("/Dex/TestSuite/GDBus/method_call/simple", test_gdbus_method_call_simple);
974 g_test_add_func ("/Dex/TestSuite/GDBus/method_call/result", test_gdbus_method_call_result);
975 g_test_add_func ("/Dex/TestSuite/GDBus/method_call/cancel", test_gdbus_method_call_cancel);
976 g_test_add_func ("/Dex/TestSuite/GDBus/method_call/complex", test_gdbus_method_call_complex);
977 g_test_add_func ("/Dex/TestSuite/GDBus/signal_wait/simple", test_gdbus_signal_wait_simple);
978 g_test_add_func ("/Dex/TestSuite/GDBus/signal_wait/cancel", test_gdbus_signal_wait_cancel);
979 g_test_add_func ("/Dex/TestSuite/GDBus/signal_monitor/basic", test_gdbus_signal_monitor_basic);
980 g_test_add_func ("/Dex/TestSuite/GDBus/signal_monitor/cancel", test_gdbus_signal_monitor_cancel);
981 g_test_add_func ("/Dex/TestSuite/GDBus/fiber/basic", test_gdbus_fiber_basic);
982 g_test_add_func ("/Dex/TestSuite/GDBus/fiber/service", test_gdbus_fiber_service);
983 return g_test_run ();
984 }