5
#include <dbus/dbus-connection-internal.h>
10
#define REMOVE_CONNECTION 0
11
#define ADD_CONNECTION 1
12
#define ALLOW_REPLACEMENT DBUS_NAME_FLAG_ALLOW_REPLACEMENT
13
#define REPLACE_EXISTING DBUS_NAME_FLAG_REPLACE_EXISTING
14
#define DO_NOT_QUEUE DBUS_NAME_FLAG_DO_NOT_QUEUE
16
#define PRIMARY_OWNER DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER
17
#define IN_QUEUE DBUS_REQUEST_NAME_REPLY_IN_QUEUE
18
#define EXISTS DBUS_REQUEST_NAME_REPLY_EXISTS
19
#define ALREADY_OWNER DBUS_REQUEST_NAME_REPLY_ALREADY_OWNER
21
#define RELEASED DBUS_RELEASE_NAME_REPLY_RELEASED
22
#define NON_EXISTANT DBUS_RELEASE_NAME_REPLY_NON_EXISTENT
23
#define NOT_OWNER DBUS_RELEASE_NAME_REPLY_NOT_OWNER
26
#define TEST_NAME "org.freedesktop.DBus.TestSuite.NameTest"
27
#define NUM_TRIES_TIL_FAIL 15
32
int connection_number;
35
dbus_uint32_t expected_result;
37
int expected_queue[NUM_CONN];
40
static 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_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
match_acquired_or_lost_signal (DBusConnection *conn, const char *member, const char *name)
203
const char *interface = "org.freedesktop.DBus";
205
for (tries = 0; tries < NUM_TRIES_TIL_FAIL; tries++)
207
_dbus_connection_lock (conn);
208
_dbus_connection_do_iteration_unlocked (conn,
209
DBUS_ITERATION_DO_READING |
210
DBUS_ITERATION_DO_WRITING |
211
DBUS_ITERATION_BLOCK,
213
_dbus_connection_unlock (conn);
214
msg = dbus_connection_pop_message (conn);
217
if (dbus_message_is_signal (msg,
223
dbus_error_init (&error);
225
dbus_message_get_args (msg, &error, DBUS_TYPE_STRING, &n, DBUS_TYPE_INVALID);
227
if (dbus_error_is_set (&error))
229
fprintf (stderr, "Error getting args: %s\n", error.message);
230
dbus_error_free (&error);
231
dbus_message_unref (msg);
235
if (strcmp (n, name) == 0)
237
dbus_message_unref (msg);
241
dbus_message_unref (msg);
245
if (tries == NUM_TRIES_TIL_FAIL)
247
fprintf (stderr, "Did not receive the expected %s.%s signal!!!\n", interface, member);
255
match_name_owner_changed_signal (DBusConnection *conn,
256
const char *bus_name,
257
const char *lost_name,
258
const char *acquired_name)
263
for (tries = 0; tries < NUM_TRIES_TIL_FAIL; tries++)
265
_dbus_connection_lock (conn);
266
_dbus_connection_do_iteration_unlocked (conn,
267
DBUS_ITERATION_DO_READING |
268
DBUS_ITERATION_DO_WRITING |
269
DBUS_ITERATION_BLOCK,
271
_dbus_connection_unlock (conn);
272
msg = dbus_connection_pop_message (conn);
276
if (dbus_message_is_signal (msg,
277
"org.freedesktop.DBus",
284
dbus_error_init (&error);
286
dbus_message_get_args (msg, &error, DBUS_TYPE_STRING, &n, DBUS_TYPE_STRING, &ln, DBUS_TYPE_STRING, &an, DBUS_TYPE_INVALID);
288
if (dbus_error_is_set (&error))
290
fprintf (stderr, "Error getting args: %s\n", error.message);
291
dbus_error_free (&error);
292
dbus_message_unref (msg);
296
if (strcmp (n, bus_name) == 0)
298
if ((lost_name == NULL && strcmp (ln, "") == 0)
299
|| strcmp (lost_name, ln) == 0)
301
if ((acquired_name == NULL && strcmp (an, "") == 0)
302
|| strcmp (acquired_name, an) == 0)
304
dbus_message_unref (msg);
309
fprintf (stderr, "Error: name %s was expected to be acquired but we got %s instead\n", acquired_name, an);
310
dbus_message_unref (msg);
316
fprintf (stderr, "Error: name %s was expected to be lost but we got %s instead\n", lost_name, ln);
317
dbus_message_unref (msg);
322
dbus_message_unref (msg);
326
if (tries == NUM_TRIES_TIL_FAIL)
328
fprintf (stderr, "Did not receive the expected NameOwnerChanged signal, bus_name %s lost_name %s acquired_name %s\n",
329
bus_name ? bus_name : "(null)",
330
lost_name ? lost_name : "(null)",
331
acquired_name ? acquired_name : "(null)");
340
check_signals (DBusConnection *monitor,
342
DBusConnection *conn[NUM_CONN])
344
DBusConnection *lost_conn = NULL;
345
DBusConnection *acquired_conn = NULL;
346
const char *lost_name;
347
const char *acquired_name;
352
i = test_data[iteration].expected_queue[0];
355
acquired_conn = conn[i];
360
i = test_data[iteration - 1].expected_queue[0];
365
i = test_data[iteration].expected_queue[0];
368
acquired_conn = conn[i];
370
if (acquired_conn == lost_conn)
371
acquired_conn = lost_conn = NULL;
374
lost_name = lost_conn == NULL? NULL :
375
dbus_bus_get_unique_name (lost_conn);
377
acquired_name = acquired_conn == NULL? NULL :
378
dbus_bus_get_unique_name (acquired_conn);
380
if (lost_name != NULL)
381
if (!match_acquired_or_lost_signal (lost_conn,
386
if (acquired_name != NULL)
387
if (!match_acquired_or_lost_signal (acquired_conn,
392
if (acquired_name != NULL || lost_name != NULL)
393
if (!match_name_owner_changed_signal (monitor,
403
main (int argc, char *argv[])
405
DBusConnection *conn[NUM_CONN];
406
DBusConnection *monitor;
411
test_data_len = sizeof (test_data) / sizeof (CommandAndResult);
413
dbus_error_init (&error);
415
conn[0] = dbus_bus_get_private (DBUS_BUS_SESSION, &error);
416
if (dbus_error_is_set (&error))
418
fprintf (stderr, "*** Failed to open connection 0 to session bus: %s\n",
420
dbus_error_free (&error);
424
if (!match_acquired_or_lost_signal (conn[0],
426
dbus_bus_get_unique_name (conn[0])))
429
conn[1] = dbus_bus_get_private (DBUS_BUS_SESSION, &error);
430
if (dbus_error_is_set (&error))
432
fprintf (stderr, "*** Failed to open connection 1 to session bus: %s\n",
434
dbus_error_free (&error);
438
if (!match_acquired_or_lost_signal (conn[1],
440
dbus_bus_get_unique_name (conn[1])))
444
conn[2] = dbus_bus_get_private (DBUS_BUS_SESSION, &error);
445
if (dbus_error_is_set (&error))
447
fprintf (stderr, "*** Failed to open connection 2 to session bus: %s\n",
449
dbus_error_free (&error);
453
if (!match_acquired_or_lost_signal (conn[2],
455
dbus_bus_get_unique_name (conn[2])))
459
conn[3] = dbus_bus_get_private (DBUS_BUS_SESSION, &error);
460
if (dbus_error_is_set (&error))
462
fprintf (stderr, "*** Failed to open connection 3 to session bus: %s\n",
464
dbus_error_free (&error);
468
if (!match_acquired_or_lost_signal (conn[3],
470
dbus_bus_get_unique_name (conn[3])))
474
monitor = dbus_bus_get (DBUS_BUS_SESSION, &error);
475
if (dbus_error_is_set (&error))
477
fprintf (stderr, "*** Failed to open monitoring connection to session bus: %s\n",
479
dbus_error_free (&error);
483
if (!match_acquired_or_lost_signal (monitor,
485
dbus_bus_get_unique_name (monitor)))
488
dbus_bus_add_match (monitor, "", &error);
489
if (dbus_error_is_set (&error))
491
fprintf (stderr, "*** Failed to set filter on monitoring connection: %s\n",
493
dbus_error_free (&error);
498
for (i = 0; i < NUM_CONN; i++)
499
dbus_connection_set_exit_on_disconnect (conn[i], FALSE);
501
for (i = 0; i < test_data_len; i++)
503
dbus_uint32_t result;
506
if (test_data[i].command == ADD_CONNECTION)
508
result = dbus_bus_request_name (conn[test_data[i].connection_number],
513
if (dbus_error_is_set (&error))
515
fprintf (stderr, "Error on addition in iteration %i: %s\n", i, error.message);
516
dbus_error_free (&error);
520
else if (test_data[i].command == REMOVE_CONNECTION)
522
result = dbus_bus_release_name (conn[test_data[i].connection_number],
525
if (dbus_error_is_set (&error))
527
fprintf (stderr, "*** Failed to remove connection %i in iteration %i: %s\n",
528
test_data[i].connection_number,
531
dbus_error_free (&error);
537
fprintf (stderr, "Command #%i not a valid command!\n", test_data[i].command);
542
if (result != test_data[i].expected_result)
544
fprintf (stderr, "Results recived (%i) are not the expected results (%i) in iteration %i\n",
546
test_data[i].expected_result,
551
if (!check_connection (monitor, i, conn))
553
fprintf (stderr, "Failed at iteration %i\n", i);
557
if (!check_signals (monitor, i, conn))
559
fprintf (stderr, "Failed at iteration %i\n", i);