6 #include <dbus/dbus-connection-internal.h>
8 #define REMOVE_CONNECTION 0
9 #define ADD_CONNECTION 1
10 #define ALLOW_REPLACEMENT DBUS_NAME_FLAG_ALLOW_REPLACEMENT
11 #define REPLACE_EXISTING DBUS_NAME_FLAG_REPLACE_EXISTING
12 #define DO_NOT_QUEUE DBUS_NAME_FLAG_DO_NOT_QUEUE
14 #define PRIMARY_OWNER DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER
15 #define IN_QUEUE DBUS_REQUEST_NAME_REPLY_IN_QUEUE
16 #define EXISTS DBUS_REQUEST_NAME_REPLY_EXISTS
17 #define ALREADY_OWNER DBUS_REQUEST_NAME_REPLY_ALREADY_OWNER
19 #define RELEASED DBUS_RELEASE_NAME_REPLY_RELEASED
20 #define NON_EXISTANT DBUS_RELEASE_NAME_REPLY_NON_EXISTENT
21 #define NOT_OWNER DBUS_RELEASE_NAME_REPLY_NOT_OWNER
24 #define TEST_NAME "org.freedesktop.DBus.TestSuite.NameTest"
25 #define NUM_TRIES_TIL_FAIL 15
27 extern const char *dbus_bus_connection_get_unique_name (DBusConnection *connection);
32 int connection_number;
35 dbus_uint32_t expected_result;
37 int expected_queue[NUM_CONN];
40 CommandAndResult test_data[] = {
41 {ADD_CONNECTION, 0, ALLOW_REPLACEMENT | REPLACE_EXISTING,
42 PRIMARY_OWNER, {0,-1,-1,-1}},
43 {ADD_CONNECTION, 0, REPLACE_EXISTING,
44 ALREADY_OWNER, {0,-1,-1,-1}},
45 {ADD_CONNECTION, 1, ALLOW_REPLACEMENT | REPLACE_EXISTING,
46 IN_QUEUE, {0,1,-1,-1}},
47 {REMOVE_CONNECTION, 0, 0,
48 RELEASED, {1,-1,-1,-1}},
49 {ADD_CONNECTION, 0, REPLACE_EXISTING | DO_NOT_QUEUE,
50 PRIMARY_OWNER, {0,1,-1,-1}},
51 {ADD_CONNECTION, 2, ALLOW_REPLACEMENT,
52 IN_QUEUE, {0,1,2,-1}},
53 {ADD_CONNECTION, 2, ALLOW_REPLACEMENT | REPLACE_EXISTING,
54 IN_QUEUE, {0,2,1,-1}},
55 {ADD_CONNECTION, 0, ALLOW_REPLACEMENT | DO_NOT_QUEUE,
56 ALREADY_OWNER, {0,2,1,-1}},
57 {ADD_CONNECTION, 1, ALLOW_REPLACEMENT | REPLACE_EXISTING,
58 PRIMARY_OWNER, {1,2,-1,-1}},
59 {ADD_CONNECTION, 0, REPLACE_EXISTING,
60 PRIMARY_OWNER, {0,1,2,-1}},
61 {ADD_CONNECTION, 2, DO_NOT_QUEUE,
63 {REMOVE_CONNECTION, 2, 0,
64 NOT_OWNER, {0,1,-1,-1}},
65 {ADD_CONNECTION, 3, 0,
66 IN_QUEUE, {0,1,3,-1}},
67 {ADD_CONNECTION, 0, ALLOW_REPLACEMENT,
68 ALREADY_OWNER, {0,1,3,-1}},
69 {ADD_CONNECTION, 2, ALLOW_REPLACEMENT,
74 check_connection (DBusConnection *conn,
76 DBusConnection *uniq_conn[NUM_CONN])
89 dbus_error_init (&error);
92 method = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
100 if (!dbus_message_append_args (method,
101 DBUS_TYPE_STRING, &name,
104 fprintf (stderr, "Error appending args\n") ;
108 reply = dbus_connection_send_with_reply_and_block (conn,
115 fprintf (stderr, "Error calling ListQueuedOwners: %s\n", error.message);
116 dbus_error_free (&error);
122 if (!dbus_message_get_args (reply,
124 DBUS_TYPE_ARRAY, DBUS_TYPE_STRING,
128 fprintf (stderr, "Error getting args: %s\n", error.message);
129 dbus_error_free (&error);
133 printf ("Iteration %i: ", iteration);
137 fprintf (stderr, "There are %i connections in the queue,"
138 " we are only expecting up to %i connections!\n",
144 for (i = 0; i < len; i++)
146 int expected_conn_num;
147 const char *expected_uname;
152 printf ("%s", list[i]);
154 expected_conn_num = test_data[iteration].expected_queue[i];
156 if (expected_conn_num == -1)
159 "\nDid not expect this last connection"
160 " to be in the queue!\n");
165 dbus_bus_connection_get_unique_name (uniq_conn[expected_conn_num]);
167 if (strcmp (list[i], expected_uname) != 0)
170 "\n%s expected but %s is in the queue!\n",
180 dbus_message_unref (method);
181 dbus_message_unref (reply);
182 dbus_free_string_array (list);
187 dbus_message_unref (method);
190 dbus_message_unref (reply);
193 dbus_free_string_array (list);
199 clear_message_queue (DBusConnection *conn)
204 for (tries = 0; tries < NUM_TRIES_TIL_FAIL; tries++)
206 _dbus_connection_lock (conn);
207 _dbus_connection_do_iteration_unlocked (conn,
208 DBUS_ITERATION_DO_READING |
209 DBUS_ITERATION_DO_WRITING |
210 DBUS_ITERATION_BLOCK,
212 _dbus_connection_unlock (conn);
213 msg = dbus_connection_pop_message (conn);
218 match_acquired_or_lost_signal (DBusConnection *conn, const char *member, const char *name)
222 const char *interface = "org.freedesktop.DBus";
224 for (tries = 0; tries < NUM_TRIES_TIL_FAIL; tries++)
226 _dbus_connection_lock (conn);
227 _dbus_connection_do_iteration_unlocked (conn,
228 DBUS_ITERATION_DO_READING |
229 DBUS_ITERATION_DO_WRITING |
230 DBUS_ITERATION_BLOCK,
232 _dbus_connection_unlock (conn);
233 msg = dbus_connection_pop_message (conn);
236 if (dbus_message_is_signal (msg,
242 dbus_error_init (&error);
244 dbus_message_get_args (msg, &error, DBUS_TYPE_STRING, &n, DBUS_TYPE_INVALID);
246 if (dbus_error_is_set (&error))
248 fprintf (stderr, "Error getting args: %s\n", error.message);
249 dbus_error_free (&error);
250 dbus_message_unref (msg);
254 if (strcmp (n, name) == 0)
256 dbus_message_unref (msg);
260 dbus_message_unref (msg);
264 if (tries == NUM_TRIES_TIL_FAIL)
266 fprintf (stderr, "Did not recive the expected %s.%s signal!!!\n", interface, member);
274 match_name_owner_changed_signal (DBusConnection *conn, const char *bus_name, const char *lost_name, const char *acquired_name)
279 for (tries = 0; tries < NUM_TRIES_TIL_FAIL; tries++)
281 _dbus_connection_lock (conn);
282 _dbus_connection_do_iteration_unlocked (conn,
283 DBUS_ITERATION_DO_READING |
284 DBUS_ITERATION_DO_WRITING |
285 DBUS_ITERATION_BLOCK,
287 _dbus_connection_unlock (conn);
288 msg = dbus_connection_pop_message (conn);
292 if (dbus_message_is_signal (msg,
293 "org.freedesktop.DBus",
300 dbus_error_init (&error);
302 dbus_message_get_args (msg, &error, DBUS_TYPE_STRING, &n, DBUS_TYPE_STRING, &ln, DBUS_TYPE_STRING, &an, DBUS_TYPE_INVALID);
304 if (dbus_error_is_set (&error))
306 fprintf (stderr, "Error getting args: %s\n", error.message);
307 dbus_error_free (&error);
308 dbus_message_unref (msg);
312 if (strcmp (n, bus_name) == 0)
314 if ((lost_name == NULL && strcmp (ln, "") == 0)
315 || strcmp (lost_name, ln) == 0)
317 if ((acquired_name == NULL && strcmp (an, "") == 0)
318 || strcmp (acquired_name, an) == 0)
320 dbus_message_unref (msg);
325 fprintf (stderr, "Error: name %s was expected to be acquired but we got %s instead\n", acquired_name, an);
326 dbus_message_unref (msg);
332 fprintf (stderr, "Error: name %s was expected to be lost but we got %s instead\n", lost_name, ln);
333 dbus_message_unref (msg);
338 dbus_message_unref (msg);
342 if (tries == NUM_TRIES_TIL_FAIL)
344 fprintf (stderr, "Did not recive the expected NameOwnerChanged signal!!!\n");
353 check_signals (DBusConnection *monitor,
355 DBusConnection *conn[NUM_CONN])
357 DBusConnection *lost_conn = NULL;
358 DBusConnection *acquired_conn = NULL;
359 const char *lost_name;
360 const char *acquired_name;
365 i = test_data[iteration].expected_queue[0];
368 acquired_conn = conn[i];
373 i = test_data[iteration - 1].expected_queue[0];
378 i = test_data[iteration].expected_queue[0];
381 acquired_conn = conn[i];
383 if (acquired_conn == lost_conn)
384 acquired_conn = lost_conn = NULL;
387 lost_name = lost_conn == NULL? NULL :
388 dbus_bus_connection_get_unique_name (lost_conn);
390 acquired_name = acquired_conn == NULL? NULL :
391 dbus_bus_connection_get_unique_name (acquired_conn);
393 if (lost_name != NULL)
394 if (!match_acquired_or_lost_signal (lost_conn,
399 if (acquired_name != NULL)
400 if (!match_acquired_or_lost_signal (acquired_conn,
405 if (acquired_name != NULL || lost_name != NULL)
406 if (!match_name_owner_changed_signal (monitor,
416 main (int argc, char *argv[])
418 DBusConnection *conn[NUM_CONN];
419 DBusConnection *monitor;
424 test_data_len = sizeof (test_data) / sizeof (CommandAndResult);
426 dbus_error_init (&error);
428 conn[0] = dbus_bus_get_private (DBUS_BUS_SESSION, &error);
429 if (dbus_error_is_set (&error))
431 fprintf (stderr, "*** Failed to open connection 0 to session bus: %s\n",
433 dbus_error_free (&error);
437 if (!match_acquired_or_lost_signal (conn[0],
439 dbus_bus_connection_get_unique_name (conn[0])))
442 conn[1] = dbus_bus_get_private (DBUS_BUS_SESSION, &error);
443 if (dbus_error_is_set (&error))
445 fprintf (stderr, "*** Failed to open connection 1 to session bus: %s\n",
447 dbus_error_free (&error);
451 if (!match_acquired_or_lost_signal (conn[1],
453 dbus_bus_connection_get_unique_name (conn[1])))
457 conn[2] = dbus_bus_get_private (DBUS_BUS_SESSION, &error);
458 if (dbus_error_is_set (&error))
460 fprintf (stderr, "*** Failed to open connection 2 to session bus: %s\n",
462 dbus_error_free (&error);
466 if (!match_acquired_or_lost_signal (conn[2],
468 dbus_bus_connection_get_unique_name (conn[2])))
472 conn[3] = dbus_bus_get_private (DBUS_BUS_SESSION, &error);
473 if (dbus_error_is_set (&error))
475 fprintf (stderr, "*** Failed to open connection 3 to session bus: %s\n",
477 dbus_error_free (&error);
481 if (!match_acquired_or_lost_signal (conn[3],
483 dbus_bus_connection_get_unique_name (conn[3])))
487 monitor = dbus_bus_get (DBUS_BUS_SESSION, &error);
488 if (dbus_error_is_set (&error))
490 fprintf (stderr, "*** Failed to open monitoring connection to session bus: %s\n",
492 dbus_error_free (&error);
496 if (!match_acquired_or_lost_signal (monitor,
498 dbus_bus_connection_get_unique_name (monitor)))
501 dbus_bus_add_match (monitor, "", &error);
502 if (dbus_error_is_set (&error))
504 fprintf (stderr, "*** Failed to set filter on monitoring connection: %s\n",
506 dbus_error_free (&error);
511 for (i = 0; i < NUM_CONN; i++)
512 dbus_connection_set_exit_on_disconnect (conn[i], FALSE);
514 for (i = 0; i < test_data_len; i++)
516 dbus_uint32_t result;
519 if (test_data[i].command == ADD_CONNECTION)
521 result = dbus_bus_request_name (conn[test_data[i].connection_number],
526 if (dbus_error_is_set (&error))
528 fprintf (stderr, "Error on addition in iteration %i: %s\n", i, error.message);
529 dbus_error_free (&error);
533 else if (test_data[i].command == REMOVE_CONNECTION)
535 result = dbus_bus_release_name (conn[test_data[i].connection_number],
538 if (dbus_error_is_set (&error))
540 fprintf (stderr, "*** Failed to remove connection %i in iteration %i: %s\n",
541 test_data[i].connection_number,
544 dbus_error_free (&error);
550 fprintf (stderr, "Command #%i not a valid command!\n", test_data[i].command);
555 if (result != test_data[i].expected_result)
557 fprintf (stderr, "Results recived (%i) are not the expected results (%i) in iteration %i\n",
559 test_data[i].expected_result,
564 if (!check_connection (monitor, i, conn))
566 fprintf (stderr, "Failed at iteration %i\n", i);
570 if (!check_signals (monitor, i, conn))
572 fprintf (stderr, "Failed at iteration %i\n", i);