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 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301
|
/* GLib testing framework examples and tests
*
* Copyright (C) 2008-2010 Red Hat, Inc.
*
* SPDX-License-Identifier: LGPL-2.1-or-later
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General
* Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
*
* Author: David Zeuthen <davidz@redhat.com>
*/
#include <gio/gio.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include "gdbus-tests.h"
/* all tests rely on a shared mainloop */
static GMainLoop *loop = NULL;
#if 0
G_GNUC_UNUSED static void
_log (const gchar *format, ...)
{
GTimeVal now;
time_t now_time;
struct tm *now_tm;
gchar time_buf[128];
gchar *str;
va_list var_args;
va_start (var_args, format);
str = g_strdup_vprintf (format, var_args);
va_end (var_args);
g_get_current_time (&now);
now_time = (time_t) now.tv_sec;
now_tm = localtime (&now_time);
strftime (time_buf, sizeof time_buf, "%H:%M:%S", now_tm);
g_printerr ("%s.%06d: %s\n",
time_buf, (gint) now.tv_usec / 1000,
str);
g_free (str);
}
#else
#define _log(...)
#endif
static gboolean
test_connection_quit_mainloop (gpointer user_data)
{
gboolean *quit_mainloop_fired = user_data; /* (atomic) */
_log ("quit_mainloop_fired");
g_atomic_int_set (quit_mainloop_fired, TRUE);
g_main_loop_quit (loop);
return G_SOURCE_CONTINUE;
}
/* ---------------------------------------------------------------------------------------------------- */
/* Connection life-cycle testing */
/* ---------------------------------------------------------------------------------------------------- */
static const GDBusInterfaceInfo boo_interface_info =
{
-1,
"org.example.Boo",
(GDBusMethodInfo **) NULL,
(GDBusSignalInfo **) NULL,
(GDBusPropertyInfo **) NULL,
NULL,
};
static const GDBusInterfaceVTable boo_vtable =
{
NULL, /* _method_call */
NULL, /* _get_property */
NULL, /* _set_property */
{ 0 }
};
/* Runs in a worker thread. */
static GDBusMessage *
some_filter_func (GDBusConnection *connection,
GDBusMessage *message,
gboolean incoming,
gpointer user_data)
{
return message;
}
static void
on_name_owner_changed (GDBusConnection *connection,
const gchar *sender_name,
const gchar *object_path,
const gchar *interface_name,
const gchar *signal_name,
GVariant *parameters,
gpointer user_data)
{
}
static void
a_gdestroynotify_that_sets_a_gboolean_to_true_and_quits_loop (gpointer user_data)
{
gboolean *val = user_data; /* (atomic) */
g_atomic_int_set (val, TRUE);
_log ("destroynotify fired for %p", val);
g_main_loop_quit (loop);
}
static void
test_connection_bus_failure (void)
{
GDBusConnection *c;
GError *error = NULL;
/*
* Check for correct behavior when no bus is present
*
*/
c = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
g_assert_nonnull (error);
g_assert_false (g_dbus_error_is_remote_error (error));
g_assert_null (c);
g_error_free (error);
}
static void
test_connection_life_cycle (void)
{
gboolean ret;
GDBusConnection *c;
GDBusConnection *c2;
GError *error;
gboolean on_signal_registration_freed_called; /* (atomic) */
gboolean on_filter_freed_called; /* (atomic) */
gboolean on_register_object_freed_called; /* (atomic) */
gboolean quit_mainloop_fired; /* (atomic) */
guint quit_mainloop_id;
guint registration_id;
error = NULL;
/*
* Check for correct behavior when a bus is present
*/
session_bus_up ();
/* case 1 */
error = NULL;
c = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
g_assert_no_error (error);
g_assert_nonnull (c);
g_assert_false (g_dbus_connection_is_closed (c));
/*
* Check that singleton handling work
*/
error = NULL;
c2 = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
g_assert_no_error (error);
g_assert_nonnull (c2);
g_assert_true (c == c2);
g_object_unref (c2);
/*
* Check that private connections work
*/
c2 = _g_bus_get_priv (G_BUS_TYPE_SESSION, NULL, &error);
g_assert_no_error (error);
g_assert_nonnull (c2);
g_assert_true (c != c2);
g_object_unref (c2);
c2 = _g_bus_get_priv (G_BUS_TYPE_SESSION, NULL, &error);
g_assert_no_error (error);
g_assert_nonnull (c2);
g_assert_false (g_dbus_connection_is_closed (c2));
ret = g_dbus_connection_close_sync (c2, NULL, &error);
g_assert_no_error (error);
g_assert_true (ret);
_g_assert_signal_received (c2, "closed");
g_assert_true (g_dbus_connection_is_closed (c2));
ret = g_dbus_connection_close_sync (c2, NULL, &error);
g_assert_error (error, G_IO_ERROR, G_IO_ERROR_CLOSED);
g_error_free (error);
g_assert_false (ret);
g_object_unref (c2);
/*
* Check that the finalization code works
*
* (and that the GDestroyNotify for filters and objects and signal
* registrations are run as expected)
*/
error = NULL;
c2 = _g_bus_get_priv (G_BUS_TYPE_SESSION, NULL, &error);
g_assert_no_error (error);
g_assert_nonnull (c2);
/* signal registration */
g_atomic_int_set (&on_signal_registration_freed_called, FALSE);
g_dbus_connection_signal_subscribe (c2,
"org.freedesktop.DBus", /* bus name */
"org.freedesktop.DBus", /* interface */
"NameOwnerChanged", /* member */
"/org/freesktop/DBus", /* path */
NULL, /* arg0 */
G_DBUS_SIGNAL_FLAGS_NONE,
on_name_owner_changed,
(gpointer) &on_signal_registration_freed_called,
a_gdestroynotify_that_sets_a_gboolean_to_true_and_quits_loop);
/* filter func */
g_atomic_int_set (&on_filter_freed_called, FALSE);
g_dbus_connection_add_filter (c2,
some_filter_func,
(gpointer) &on_filter_freed_called,
a_gdestroynotify_that_sets_a_gboolean_to_true_and_quits_loop);
/* object registration */
g_atomic_int_set (&on_register_object_freed_called, FALSE);
error = NULL;
registration_id = g_dbus_connection_register_object (c2,
"/foo",
(GDBusInterfaceInfo *) &boo_interface_info,
&boo_vtable,
(gpointer) &on_register_object_freed_called,
a_gdestroynotify_that_sets_a_gboolean_to_true_and_quits_loop,
&error);
g_assert_no_error (error);
g_assert_cmpuint (registration_id, >, 0);
/* ok, finalize the connection and check that all the GDestroyNotify functions are invoked as expected */
g_object_unref (c2);
g_atomic_int_set (&quit_mainloop_fired, FALSE);
quit_mainloop_id = g_timeout_add (30000, test_connection_quit_mainloop, (gpointer) &quit_mainloop_fired);
_log ("destroynotifies for\n"
" register_object %p\n"
" filter %p\n"
" signal %p",
&on_register_object_freed_called,
&on_filter_freed_called,
&on_signal_registration_freed_called);
while (TRUE)
{
if (g_atomic_int_get (&on_signal_registration_freed_called) &&
g_atomic_int_get (&on_filter_freed_called) &&
g_atomic_int_get (&on_register_object_freed_called))
break;
if (g_atomic_int_get (&quit_mainloop_fired))
break;
_log ("entering loop");
g_main_loop_run (loop);
_log ("exiting loop");
}
g_source_remove (quit_mainloop_id);
g_assert_true (g_atomic_int_get (&on_signal_registration_freed_called));
g_assert_true (g_atomic_int_get (&on_filter_freed_called));
g_assert_true (g_atomic_int_get (&on_register_object_freed_called));
g_assert_false (g_atomic_int_get (&quit_mainloop_fired));
/*
* Check for correct behavior when the bus goes away
*
*/
g_assert_false (g_dbus_connection_is_closed (c));
g_dbus_connection_set_exit_on_close (c, FALSE);
session_bus_stop ();
_g_assert_signal_received (c, "closed");
g_assert_true (g_dbus_connection_is_closed (c));
g_object_unref (c);
session_bus_down ();
}
/* ---------------------------------------------------------------------------------------------------- */
/* Test that sending and receiving messages work as expected */
/* ---------------------------------------------------------------------------------------------------- */
static void
msg_cb_expect_error_disconnected (GDBusConnection *connection,
GAsyncResult *res,
gpointer user_data)
{
GError *error;
GVariant *result;
/* Make sure gdbusconnection isn't holding @connection's lock. (#747349) */
g_dbus_connection_get_last_serial (connection);
error = NULL;
result = g_dbus_connection_call_finish (connection,
res,
&error);
g_assert_error (error, G_IO_ERROR, G_IO_ERROR_CLOSED);
g_assert_false (g_dbus_error_is_remote_error (error));
g_error_free (error);
g_assert_null (result);
g_main_loop_quit (loop);
}
static void
msg_cb_expect_error_unknown_method (GDBusConnection *connection,
GAsyncResult *res,
gpointer user_data)
{
GError *error;
GVariant *result;
/* Make sure gdbusconnection isn't holding @connection's lock. (#747349) */
g_dbus_connection_get_last_serial (connection);
error = NULL;
result = g_dbus_connection_call_finish (connection,
res,
&error);
g_assert_error (error, G_DBUS_ERROR, G_DBUS_ERROR_UNKNOWN_METHOD);
g_assert_true (g_dbus_error_is_remote_error (error));
g_error_free (error);
g_assert_null (result);
g_main_loop_quit (loop);
}
static void
msg_cb_expect_success (GDBusConnection *connection,
GAsyncResult *res,
gpointer user_data)
{
GError *error;
GVariant *result;
/* Make sure gdbusconnection isn't holding @connection's lock. (#747349) */
g_dbus_connection_get_last_serial (connection);
error = NULL;
result = g_dbus_connection_call_finish (connection,
res,
&error);
g_assert_no_error (error);
g_assert_nonnull (result);
g_variant_unref (result);
g_main_loop_quit (loop);
}
static void
msg_cb_expect_error_cancelled (GDBusConnection *connection,
GAsyncResult *res,
gpointer user_data)
{
GError *error;
GVariant *result;
/* Make sure gdbusconnection isn't holding @connection's lock. (#747349) */
g_dbus_connection_get_last_serial (connection);
error = NULL;
result = g_dbus_connection_call_finish (connection,
res,
&error);
g_assert_error (error, G_IO_ERROR, G_IO_ERROR_CANCELLED);
g_assert_false (g_dbus_error_is_remote_error (error));
g_error_free (error);
g_assert_null (result);
g_main_loop_quit (loop);
}
static void
msg_cb_expect_error_cancelled_2 (GDBusConnection *connection,
GAsyncResult *res,
gpointer user_data)
{
GError *error;
GVariant *result;
/* Make sure gdbusconnection isn't holding @connection's lock. (#747349) */
g_dbus_connection_get_last_serial (connection);
error = NULL;
result = g_dbus_connection_call_finish (connection,
res,
&error);
g_assert_error (error, G_IO_ERROR, G_IO_ERROR_CANCELLED);
g_assert_false (g_dbus_error_is_remote_error (error));
g_error_free (error);
g_assert_null (result);
g_main_loop_quit (loop);
}
/* ---------------------------------------------------------------------------------------------------- */
static void
test_connection_send (void)
{
GDBusConnection *c;
GCancellable *ca;
session_bus_up ();
/* First, get an unopened connection */
c = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
g_assert_nonnull (c);
g_assert_false (g_dbus_connection_is_closed (c));
/*
* Check that we never actually send a message if the GCancellable
* is already cancelled - i.e. we should get G_IO_ERROR_CANCELLED
* when the actual connection is not up.
*/
ca = g_cancellable_new ();
g_cancellable_cancel (ca);
g_dbus_connection_call (c,
"org.freedesktop.DBus", /* bus_name */
"/org/freedesktop/DBus", /* object path */
"org.freedesktop.DBus", /* interface name */
"GetId", /* method name */
NULL, NULL,
G_DBUS_CALL_FLAGS_NONE,
-1,
ca,
(GAsyncReadyCallback) msg_cb_expect_error_cancelled,
NULL);
g_main_loop_run (loop);
g_object_unref (ca);
/*
* Check that we get a reply to the GetId() method call.
*/
g_dbus_connection_call (c,
"org.freedesktop.DBus", /* bus_name */
"/org/freedesktop/DBus", /* object path */
"org.freedesktop.DBus", /* interface name */
"GetId", /* method name */
NULL, NULL,
G_DBUS_CALL_FLAGS_NONE,
-1,
NULL,
(GAsyncReadyCallback) msg_cb_expect_success,
NULL);
g_main_loop_run (loop);
/*
* Check that we get an error reply to the NonExistantMethod() method call.
*/
g_dbus_connection_call (c,
"org.freedesktop.DBus", /* bus_name */
"/org/freedesktop/DBus", /* object path */
"org.freedesktop.DBus", /* interface name */
"NonExistantMethod", /* method name */
NULL, NULL,
G_DBUS_CALL_FLAGS_NONE,
-1,
NULL,
(GAsyncReadyCallback) msg_cb_expect_error_unknown_method,
NULL);
g_main_loop_run (loop);
/*
* Check that cancellation works when the message is already in flight.
*/
ca = g_cancellable_new ();
g_dbus_connection_call (c,
"org.freedesktop.DBus", /* bus_name */
"/org/freedesktop/DBus", /* object path */
"org.freedesktop.DBus", /* interface name */
"GetId", /* method name */
NULL, NULL,
G_DBUS_CALL_FLAGS_NONE,
-1,
ca,
(GAsyncReadyCallback) msg_cb_expect_error_cancelled_2,
NULL);
g_cancellable_cancel (ca);
g_main_loop_run (loop);
g_object_unref (ca);
/*
* Check that we get an error when sending to a connection that is disconnected.
*/
g_dbus_connection_set_exit_on_close (c, FALSE);
session_bus_stop ();
_g_assert_signal_received (c, "closed");
g_assert_true (g_dbus_connection_is_closed (c));
g_dbus_connection_call (c,
"org.freedesktop.DBus", /* bus_name */
"/org/freedesktop/DBus", /* object path */
"org.freedesktop.DBus", /* interface name */
"GetId", /* method name */
NULL, NULL,
G_DBUS_CALL_FLAGS_NONE,
-1,
NULL,
(GAsyncReadyCallback) msg_cb_expect_error_disconnected,
NULL);
g_main_loop_run (loop);
g_object_unref (c);
session_bus_down ();
}
/* ---------------------------------------------------------------------------------------------------- */
/* Connection signal tests */
/* ---------------------------------------------------------------------------------------------------- */
static void
test_connection_signal_handler (GDBusConnection *connection,
const gchar *sender_name,
const gchar *object_path,
const gchar *interface_name,
const gchar *signal_name,
GVariant *parameters,
gpointer user_data)
{
gint *counter = user_data;
*counter += 1;
/*g_debug ("in test_connection_signal_handler (sender=%s path=%s interface=%s member=%s)",
sender_name,
object_path,
interface_name,
signal_name);*/
g_main_loop_quit (loop);
}
static void
test_connection_signals (void)
{
GDBusConnection *c1;
GDBusConnection *c2;
GDBusConnection *c3;
guint s1;
guint s1b;
guint s2;
guint s3;
gint count_s1;
gint count_s1b;
gint count_s2;
gint count_name_owner_changed;
GError *error;
gboolean ret;
GVariant *result;
gboolean quit_mainloop_fired;
guint quit_mainloop_id;
error = NULL;
/*
* Bring up first separate connections
*/
session_bus_up ();
/* if running with dbus-monitor, it claims the name :1.0 - so if we don't run with the monitor
* emulate this
*/
if (g_getenv ("G_DBUS_MONITOR") == NULL)
{
c1 = _g_bus_get_priv (G_BUS_TYPE_SESSION, NULL, NULL);
g_assert_nonnull (c1);
g_assert_false (g_dbus_connection_is_closed (c1));
g_object_unref (c1);
}
c1 = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
g_assert_nonnull (c1);
g_assert_false (g_dbus_connection_is_closed (c1));
g_assert_cmpstr (g_dbus_connection_get_unique_name (c1), ==, ":1.1");
/*
* Install two signal handlers for the first connection
*
* - Listen to the signal "Foo" from :1.2 (e.g. c2)
* - Listen to the signal "Foo" from anyone (e.g. both c2 and c3)
*
* and then count how many times this signal handler was invoked.
*/
s1 = g_dbus_connection_signal_subscribe (c1,
":1.2",
"org.gtk.GDBus.ExampleInterface",
"Foo",
"/org/gtk/GDBus/ExampleInterface",
NULL,
G_DBUS_SIGNAL_FLAGS_NONE,
test_connection_signal_handler,
&count_s1,
NULL);
s2 = g_dbus_connection_signal_subscribe (c1,
NULL, /* match any sender */
"org.gtk.GDBus.ExampleInterface",
"Foo",
"/org/gtk/GDBus/ExampleInterface",
NULL,
G_DBUS_SIGNAL_FLAGS_NONE,
test_connection_signal_handler,
&count_s2,
NULL);
s3 = g_dbus_connection_signal_subscribe (c1,
"org.freedesktop.DBus", /* sender */
"org.freedesktop.DBus", /* interface */
"NameOwnerChanged", /* member */
"/org/freedesktop/DBus", /* path */
NULL,
G_DBUS_SIGNAL_FLAGS_NONE,
test_connection_signal_handler,
&count_name_owner_changed,
NULL);
/* Note that s1b is *just like* s1 - this is to catch a bug where N
* subscriptions of the same rule causes N calls to each of the N
* subscriptions instead of just 1 call to each of the N subscriptions.
*/
s1b = g_dbus_connection_signal_subscribe (c1,
":1.2",
"org.gtk.GDBus.ExampleInterface",
"Foo",
"/org/gtk/GDBus/ExampleInterface",
NULL,
G_DBUS_SIGNAL_FLAGS_NONE,
test_connection_signal_handler,
&count_s1b,
NULL);
g_assert_cmpuint (s1, !=, 0);
g_assert_cmpuint (s1b, !=, 0);
g_assert_cmpuint (s2, !=, 0);
g_assert_cmpuint (s3, !=, 0);
count_s1 = 0;
count_s1b = 0;
count_s2 = 0;
count_name_owner_changed = 0;
/*
* Make c2 emit "Foo" - we should catch it twice
*
* Note that there is no way to be sure that the signal subscriptions
* on c1 are effective yet - for all we know, the AddMatch() messages
* could sit waiting in a buffer somewhere between this process and
* the message bus. And emitting signals on c2 (a completely other
* socket!) will not necessarily change this.
*
* To ensure this is not the case, do a synchronous call on c1.
*/
result = g_dbus_connection_call_sync (c1,
"org.freedesktop.DBus", /* bus name */
"/org/freedesktop/DBus", /* object path */
"org.freedesktop.DBus", /* interface name */
"GetId", /* method name */
NULL, /* parameters */
NULL, /* return type */
G_DBUS_CALL_FLAGS_NONE,
-1,
NULL,
&error);
g_assert_no_error (error);
g_assert_nonnull (result);
g_variant_unref (result);
/*
* Bring up two other connections
*/
c2 = _g_bus_get_priv (G_BUS_TYPE_SESSION, NULL, NULL);
g_assert_nonnull (c2);
g_assert_false (g_dbus_connection_is_closed (c2));
g_assert_cmpstr (g_dbus_connection_get_unique_name (c2), ==, ":1.2");
c3 = _g_bus_get_priv (G_BUS_TYPE_SESSION, NULL, NULL);
g_assert_nonnull (c3);
g_assert_false (g_dbus_connection_is_closed (c3));
g_assert_cmpstr (g_dbus_connection_get_unique_name (c3), ==, ":1.3");
/* now, emit the signal on c2 */
ret = g_dbus_connection_emit_signal (c2,
NULL, /* destination bus name */
"/org/gtk/GDBus/ExampleInterface",
"org.gtk.GDBus.ExampleInterface",
"Foo",
NULL,
&error);
g_assert_no_error (error);
g_assert_true (ret);
while (!(count_s1 >= 1 && count_s2 >= 1))
g_main_loop_run (loop);
g_assert_cmpint (count_s1, ==, 1);
g_assert_cmpint (count_s2, ==, 1);
/*
* Make c3 emit "Foo" - we should catch it only once
*/
ret = g_dbus_connection_emit_signal (c3,
NULL, /* destination bus name */
"/org/gtk/GDBus/ExampleInterface",
"org.gtk.GDBus.ExampleInterface",
"Foo",
NULL,
&error);
g_assert_no_error (error);
g_assert_true (ret);
while (!(count_s1 == 1 && count_s2 == 2))
g_main_loop_run (loop);
g_assert_cmpint (count_s1, ==, 1);
g_assert_cmpint (count_s2, ==, 2);
/*
* Also to check the total amount of NameOwnerChanged signals - use a 5 second ceiling
* to avoid spinning forever
*/
quit_mainloop_fired = FALSE;
quit_mainloop_id = g_timeout_add (30000, test_connection_quit_mainloop, &quit_mainloop_fired);
while (count_name_owner_changed < 2 && !quit_mainloop_fired)
g_main_loop_run (loop);
g_source_remove (quit_mainloop_id);
g_assert_cmpint (count_s1, ==, 1);
g_assert_cmpint (count_s2, ==, 2);
g_assert_cmpint (count_name_owner_changed, ==, 2);
g_dbus_connection_signal_unsubscribe (c1, s1);
g_dbus_connection_signal_unsubscribe (c1, s2);
g_dbus_connection_signal_unsubscribe (c1, s3);
g_dbus_connection_signal_unsubscribe (c1, s1b);
g_object_unref (c1);
g_object_unref (c2);
g_object_unref (c3);
session_bus_down ();
}
static void
test_match_rule (GDBusConnection *connection,
GDBusSignalFlags flags,
gchar *arg0_rule,
gchar *arg0,
gboolean should_match)
{
guint subscription_ids[2];
gint emissions = 0;
gint matches = 0;
GError *error = NULL;
subscription_ids[0] = g_dbus_connection_signal_subscribe (connection,
NULL, "org.gtk.ExampleInterface", "Foo", "/",
NULL,
G_DBUS_SIGNAL_FLAGS_NONE,
test_connection_signal_handler,
&emissions, NULL);
subscription_ids[1] = g_dbus_connection_signal_subscribe (connection,
NULL, "org.gtk.ExampleInterface", "Foo", "/",
arg0_rule,
flags,
test_connection_signal_handler,
&matches, NULL);
g_assert_cmpint (subscription_ids[0], !=, 0);
g_assert_cmpint (subscription_ids[1], !=, 0);
g_dbus_connection_emit_signal (connection,
NULL, "/", "org.gtk.ExampleInterface",
"Foo", g_variant_new ("(s)", arg0),
&error);
g_assert_no_error (error);
/* synchronously ping a non-existent method to make sure the signals are dispatched */
g_dbus_connection_call_sync (connection, "org.gtk.ExampleInterface", "/", "org.gtk.ExampleInterface",
"Bar", g_variant_new ("()"), G_VARIANT_TYPE_UNIT, G_DBUS_CALL_FLAGS_NONE,
-1, NULL, NULL);
while (g_main_context_iteration (NULL, FALSE))
;
g_assert_cmpint (emissions, ==, 1);
g_assert_cmpint (matches, ==, should_match ? 1 : 0);
g_dbus_connection_signal_unsubscribe (connection, subscription_ids[0]);
g_dbus_connection_signal_unsubscribe (connection, subscription_ids[1]);
}
static void
test_connection_signal_match_rules (void)
{
GDBusConnection *con;
session_bus_up ();
con = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
test_match_rule (con, G_DBUS_SIGNAL_FLAGS_NONE, "foo", "foo", TRUE);
test_match_rule (con, G_DBUS_SIGNAL_FLAGS_NONE, "foo", "bar", FALSE);
test_match_rule (con, G_DBUS_SIGNAL_FLAGS_MATCH_ARG0_NAMESPACE, "org.gtk", "", FALSE);
test_match_rule (con, G_DBUS_SIGNAL_FLAGS_MATCH_ARG0_NAMESPACE, "org.gtk", "org", FALSE);
test_match_rule (con, G_DBUS_SIGNAL_FLAGS_MATCH_ARG0_NAMESPACE, "org.gtk", "org.gtk", TRUE);
test_match_rule (con, G_DBUS_SIGNAL_FLAGS_MATCH_ARG0_NAMESPACE, "org.gtk", "org.gtk.Example", TRUE);
test_match_rule (con, G_DBUS_SIGNAL_FLAGS_MATCH_ARG0_NAMESPACE, "org.gtk", "org.gtk+", FALSE);
test_match_rule (con, G_DBUS_SIGNAL_FLAGS_MATCH_ARG0_PATH, "/", "/", TRUE);
test_match_rule (con, G_DBUS_SIGNAL_FLAGS_MATCH_ARG0_PATH, "/", "", FALSE);
test_match_rule (con, G_DBUS_SIGNAL_FLAGS_MATCH_ARG0_PATH, "/org/gtk/Example", "/org/gtk/Example", TRUE);
test_match_rule (con, G_DBUS_SIGNAL_FLAGS_MATCH_ARG0_PATH, "/org/gtk/", "/org/gtk/Example", TRUE);
test_match_rule (con, G_DBUS_SIGNAL_FLAGS_MATCH_ARG0_PATH, "/org/gtk/Example", "/org/gtk/", TRUE);
test_match_rule (con, G_DBUS_SIGNAL_FLAGS_MATCH_ARG0_PATH, "/org/gtk/Example", "/org/gtk", FALSE);
test_match_rule (con, G_DBUS_SIGNAL_FLAGS_MATCH_ARG0_PATH, "/org/gtk+", "/org/gtk", FALSE);
g_object_unref (con);
session_bus_down ();
}
/* ---------------------------------------------------------------------------------------------------- */
/* Accessed both from the test code and the filter function (in a worker thread)
* so all accesses must be atomic. */
typedef struct
{
GAsyncQueue *incoming_queue; /* (element-type GDBusMessage) */
guint num_outgoing; /* (atomic) */
} FilterData;
/* Runs in a worker thread. */
static GDBusMessage *
filter_func (GDBusConnection *connection,
GDBusMessage *message,
gboolean incoming,
gpointer user_data)
{
FilterData *data = user_data;
if (incoming)
g_async_queue_push (data->incoming_queue, g_object_ref (message));
else
g_atomic_int_inc (&data->num_outgoing);
return message;
}
static void
wait_for_filtered_reply (GAsyncQueue *incoming_queue,
guint32 expected_serial)
{
GDBusMessage *popped_message = NULL;
while ((popped_message = g_async_queue_pop (incoming_queue)) != NULL)
{
guint32 reply_serial = g_dbus_message_get_reply_serial (popped_message);
g_object_unref (popped_message);
if (reply_serial == expected_serial)
return;
}
g_assert_not_reached ();
}
typedef struct
{
gboolean alter_incoming;
gboolean alter_outgoing;
} FilterEffects;
/* Runs in a worker thread. */
static GDBusMessage *
other_filter_func (GDBusConnection *connection,
GDBusMessage *message,
gboolean incoming,
gpointer user_data)
{
const FilterEffects *effects = user_data;
GDBusMessage *ret;
gboolean alter;
if (incoming)
alter = effects->alter_incoming;
else
alter = effects->alter_outgoing;
if (alter)
{
GDBusMessage *copy;
GVariant *body;
gchar *s;
gchar *s2;
copy = g_dbus_message_copy (message, NULL);
g_object_unref (message);
body = g_dbus_message_get_body (copy);
g_variant_get (body, "(s)", &s);
s2 = g_strdup_printf ("MOD: %s", s);
g_dbus_message_set_body (copy, g_variant_new ("(s)", s2));
g_free (s2);
g_free (s);
ret = copy;
}
else
{
ret = message;
}
return ret;
}
static void
test_connection_filter_name_owner_changed_signal_handler (GDBusConnection *connection,
const gchar *sender_name,
const gchar *object_path,
const gchar *interface_name,
const gchar *signal_name,
GVariant *parameters,
gpointer user_data)
{
const gchar *name;
const gchar *old_owner;
const gchar *new_owner;
g_variant_get (parameters,
"(&s&s&s)",
&name,
&old_owner,
&new_owner);
if (g_strcmp0 (name, "com.example.TestService") == 0 && strlen (new_owner) > 0)
{
g_main_loop_quit (loop);
}
}
static gboolean
test_connection_filter_on_timeout (gpointer user_data)
{
g_printerr ("Timeout waiting 30 sec on service\n");
g_assert_not_reached ();
return G_SOURCE_REMOVE;
}
static void
test_connection_filter (void)
{
GDBusConnection *c;
FilterData data = { NULL, 0 };
GDBusMessage *m;
GDBusMessage *m2;
GDBusMessage *r;
GError *error;
guint filter_id;
guint timeout_mainloop_id;
guint signal_handler_id;
FilterEffects effects;
GVariant *result;
const gchar *s;
guint32 serial_temp;
session_bus_up ();
error = NULL;
c = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
g_assert_no_error (error);
g_assert_nonnull (c);
data.incoming_queue = g_async_queue_new_full (g_object_unref);
data.num_outgoing = 0;
filter_id = g_dbus_connection_add_filter (c,
filter_func,
&data,
NULL);
m = g_dbus_message_new_method_call ("org.freedesktop.DBus", /* name */
"/org/freedesktop/DBus", /* path */
"org.freedesktop.DBus", /* interface */
"GetNameOwner");
g_dbus_message_set_body (m, g_variant_new ("(s)", "org.freedesktop.DBus"));
error = NULL;
g_dbus_connection_send_message (c, m, G_DBUS_SEND_MESSAGE_FLAGS_NONE, &serial_temp, &error);
g_assert_no_error (error);
wait_for_filtered_reply (data.incoming_queue, serial_temp);
m2 = g_dbus_message_copy (m, &error);
g_assert_no_error (error);
g_dbus_connection_send_message (c, m2, G_DBUS_SEND_MESSAGE_FLAGS_NONE, &serial_temp, &error);
g_object_unref (m2);
g_assert_no_error (error);
wait_for_filtered_reply (data.incoming_queue, serial_temp);
m2 = g_dbus_message_copy (m, &error);
g_assert_no_error (error);
g_dbus_message_set_serial (m2, serial_temp);
/* lock the message to test PRESERVE_SERIAL flag. */
g_dbus_message_lock (m2);
g_dbus_connection_send_message (c, m2, G_DBUS_SEND_MESSAGE_FLAGS_PRESERVE_SERIAL, &serial_temp, &error);
g_object_unref (m2);
g_assert_no_error (error);
wait_for_filtered_reply (data.incoming_queue, serial_temp);
m2 = g_dbus_message_copy (m, &error);
g_assert_no_error (error);
r = g_dbus_connection_send_message_with_reply_sync (c,
m2,
G_DBUS_SEND_MESSAGE_FLAGS_NONE,
-1,
&serial_temp,
NULL, /* GCancellable */
&error);
g_object_unref (m2);
g_assert_no_error (error);
g_assert_nonnull (r);
g_object_unref (r);
wait_for_filtered_reply (data.incoming_queue, serial_temp);
g_assert_cmpint (g_async_queue_length (data.incoming_queue), ==, 0);
g_dbus_connection_remove_filter (c, filter_id);
m2 = g_dbus_message_copy (m, &error);
g_assert_no_error (error);
r = g_dbus_connection_send_message_with_reply_sync (c,
m2,
G_DBUS_SEND_MESSAGE_FLAGS_NONE,
-1,
&serial_temp,
NULL, /* GCancellable */
&error);
g_object_unref (m2);
g_assert_no_error (error);
g_assert_nonnull (r);
g_object_unref (r);
g_assert_cmpint (g_async_queue_length (data.incoming_queue), ==, 0);
g_assert_cmpint (g_atomic_int_get (&data.num_outgoing), ==, 4);
/* wait for service to be available */
signal_handler_id = g_dbus_connection_signal_subscribe (c,
"org.freedesktop.DBus", /* sender */
"org.freedesktop.DBus",
"NameOwnerChanged",
"/org/freedesktop/DBus",
NULL, /* arg0 */
G_DBUS_SIGNAL_FLAGS_NONE,
test_connection_filter_name_owner_changed_signal_handler,
NULL,
NULL);
g_assert_cmpint (signal_handler_id, !=, 0);
/* this is safe; testserver will exit once the bus goes away */
g_assert_true (g_spawn_command_line_async (g_test_get_filename (G_TEST_BUILT, "gdbus-testserver", NULL), NULL));
timeout_mainloop_id = g_timeout_add (30000, test_connection_filter_on_timeout, NULL);
g_main_loop_run (loop);
g_source_remove (timeout_mainloop_id);
g_dbus_connection_signal_unsubscribe (c, signal_handler_id);
/* now test some combinations... */
filter_id = g_dbus_connection_add_filter (c,
other_filter_func,
&effects,
NULL);
/* -- */
effects.alter_incoming = FALSE;
effects.alter_outgoing = FALSE;
error = NULL;
result = g_dbus_connection_call_sync (c,
"com.example.TestService", /* bus name */
"/com/example/TestObject", /* object path */
"com.example.Frob", /* interface name */
"HelloWorld", /* method name */
g_variant_new ("(s)", "Cat"), /* parameters */
G_VARIANT_TYPE ("(s)"), /* return type */
G_DBUS_CALL_FLAGS_NONE,
-1,
NULL,
&error);
g_assert_no_error (error);
g_variant_get (result, "(&s)", &s);
g_assert_cmpstr (s, ==, "You greeted me with 'Cat'. Thanks!");
g_variant_unref (result);
/* -- */
effects.alter_incoming = TRUE;
effects.alter_outgoing = TRUE;
error = NULL;
result = g_dbus_connection_call_sync (c,
"com.example.TestService", /* bus name */
"/com/example/TestObject", /* object path */
"com.example.Frob", /* interface name */
"HelloWorld", /* method name */
g_variant_new ("(s)", "Cat"), /* parameters */
G_VARIANT_TYPE ("(s)"), /* return type */
G_DBUS_CALL_FLAGS_NONE,
-1,
NULL,
&error);
g_assert_no_error (error);
g_variant_get (result, "(&s)", &s);
g_assert_cmpstr (s, ==, "MOD: You greeted me with 'MOD: Cat'. Thanks!");
g_variant_unref (result);
g_dbus_connection_remove_filter (c, filter_id);
g_object_unref (c);
g_object_unref (m);
g_async_queue_unref (data.incoming_queue);
session_bus_down ();
}
/* ---------------------------------------------------------------------------------------------------- */
#define NUM_THREADS 50
static void
send_bogus_message (GDBusConnection *c, guint32 *out_serial)
{
GDBusMessage *m;
GError *error;
m = g_dbus_message_new_method_call ("org.freedesktop.DBus", /* name */
"/org/freedesktop/DBus", /* path */
"org.freedesktop.DBus", /* interface */
"GetNameOwner");
g_dbus_message_set_body (m, g_variant_new ("(s)", "org.freedesktop.DBus"));
error = NULL;
g_dbus_connection_send_message (c, m, G_DBUS_SEND_MESSAGE_FLAGS_NONE, out_serial, &error);
g_assert_no_error (error);
g_object_unref (m);
}
#define SLEEP_USEC (100 * 1000)
static gpointer
serials_thread_func (GDBusConnection *c)
{
guint32 message_serial;
guint i;
/* No calls on this thread yet */
g_assert_cmpint (g_dbus_connection_get_last_serial(c), ==, 0);
/* Send a bogus message and store its serial */
message_serial = 0;
send_bogus_message (c, &message_serial);
/* Give it some time to actually send the message out. 10 seconds
* should be plenty, even on slow machines. */
for (i = 0; i < 10 * G_USEC_PER_SEC / SLEEP_USEC; i++)
{
if (g_dbus_connection_get_last_serial(c) != 0)
break;
g_usleep (SLEEP_USEC);
}
g_assert_cmpint (g_dbus_connection_get_last_serial(c), !=, 0);
g_assert_cmpint (g_dbus_connection_get_last_serial(c), ==, message_serial);
return NULL;
}
static void
test_connection_serials (void)
{
GDBusConnection *c;
GError *error;
GThread *pool[NUM_THREADS];
int i;
session_bus_up ();
error = NULL;
c = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
g_assert_no_error (error);
g_assert_nonnull (c);
/* Status after initialization */
g_assert_cmpint (g_dbus_connection_get_last_serial (c), ==, 1);
/* Send a bogus message */
send_bogus_message (c, NULL);
g_assert_cmpint (g_dbus_connection_get_last_serial (c), ==, 2);
/* Start the threads */
for (i = 0; i < NUM_THREADS; i++)
pool[i] = g_thread_new (NULL, (GThreadFunc) serials_thread_func, c);
/* Wait until threads are finished */
for (i = 0; i < NUM_THREADS; i++)
g_thread_join (pool[i]);
/* No calls in between on this thread, should be the last value */
g_assert_cmpint (g_dbus_connection_get_last_serial (c), ==, 2);
send_bogus_message (c, NULL);
/* All above calls + calls in threads */
g_assert_cmpint (g_dbus_connection_get_last_serial (c), ==, 3 + NUM_THREADS);
g_object_unref (c);
session_bus_down ();
}
/* ---------------------------------------------------------------------------------------------------- */
static void
test_connection_basic (void)
{
GDBusConnection *connection;
GError *error;
GDBusCapabilityFlags flags;
GDBusConnectionFlags connection_flags;
gchar *guid;
gchar *name;
gboolean closed;
gboolean exit_on_close;
GIOStream *stream;
GCredentials *credentials;
session_bus_up ();
error = NULL;
connection = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
g_assert_no_error (error);
g_assert_nonnull (connection);
flags = g_dbus_connection_get_capabilities (connection);
g_assert_true (flags == G_DBUS_CAPABILITY_FLAGS_NONE ||
flags == G_DBUS_CAPABILITY_FLAGS_UNIX_FD_PASSING);
connection_flags = g_dbus_connection_get_flags (connection);
/* Ignore G_DBUS_CONNECTION_FLAGS_CROSS_NAMESPACE, it's an
* implementation detail whether we set it */
connection_flags &= ~G_DBUS_CONNECTION_FLAGS_CROSS_NAMESPACE;
g_assert_cmpint (connection_flags, ==,
G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT |
G_DBUS_CONNECTION_FLAGS_MESSAGE_BUS_CONNECTION);
credentials = g_dbus_connection_get_peer_credentials (connection);
g_assert_null (credentials);
g_object_get (connection,
"stream", &stream,
"guid", &guid,
"unique-name", &name,
"closed", &closed,
"exit-on-close", &exit_on_close,
"capabilities", &flags,
NULL);
g_assert_true (G_IS_IO_STREAM (stream));
g_assert_true (g_dbus_is_guid (guid));
g_assert_true (g_dbus_is_unique_name (name));
g_assert_false (closed);
g_assert_true (exit_on_close);
g_assert_true (flags == G_DBUS_CAPABILITY_FLAGS_NONE ||
flags == G_DBUS_CAPABILITY_FLAGS_UNIX_FD_PASSING);
g_object_unref (stream);
g_free (name);
g_free (guid);
g_object_unref (connection);
session_bus_down ();
}
/* ---------------------------------------------------------------------------------------------------- */
int
main (int argc,
char *argv[])
{
int ret;
g_test_init (&argc, &argv, NULL);
/* all the tests rely on a shared main loop */
loop = g_main_loop_new (NULL, FALSE);
g_test_dbus_unset ();
/* gdbus cleanup is pretty racy due to worker threads, so always do this test first */
g_test_add_func ("/gdbus/connection/bus-failure", test_connection_bus_failure);
g_test_add_func ("/gdbus/connection/basic", test_connection_basic);
g_test_add_func ("/gdbus/connection/life-cycle", test_connection_life_cycle);
g_test_add_func ("/gdbus/connection/send", test_connection_send);
g_test_add_func ("/gdbus/connection/signals", test_connection_signals);
g_test_add_func ("/gdbus/connection/signal-match-rules", test_connection_signal_match_rules);
g_test_add_func ("/gdbus/connection/filter", test_connection_filter);
g_test_add_func ("/gdbus/connection/serials", test_connection_serials);
ret = g_test_run();
g_main_loop_unref (loop);
return ret;
}
|