2 #include "test-utils.h"
8 static dbus_bool_t already_quit = FALSE;
9 static dbus_bool_t hello_from_self_reply_received = FALSE;
16 _dbus_loop_quit (loop);
22 die (const char *message)
24 fprintf (stderr, "*** test-service: %s", message);
29 check_hello_from_self_reply (DBusPendingCall *pcall,
33 DBusMessage *echo_message, *echo_reply = NULL;
35 DBusConnection *connection;
39 dbus_error_init (&error);
41 connection = dbus_bus_get (DBUS_BUS_STARTER, &error);
42 if (connection == NULL)
44 fprintf (stderr, "*** Failed to open connection to activating message bus: %s\n",
46 dbus_error_free (&error);
51 echo_message = (DBusMessage *)user_data;
53 reply = dbus_pending_call_steal_reply (pcall);
55 type = dbus_message_get_type (reply);
57 if (type == DBUS_MESSAGE_TYPE_METHOD_RETURN)
60 printf ("Reply from HelloFromSelf received\n");
62 if (!dbus_message_get_args (echo_message,
67 echo_reply = dbus_message_new_error (echo_message,
71 if (echo_reply == NULL)
77 echo_reply = dbus_message_new_method_return (echo_message);
78 if (echo_reply == NULL)
81 if (!dbus_message_append_args (echo_reply,
87 if (!dbus_connection_send (connection, echo_reply, NULL))
90 dbus_message_unref (echo_reply);
92 else if (type == DBUS_MESSAGE_TYPE_ERROR)
94 dbus_set_error_from_message (&error, reply);
95 printf ("Error type in reply: %s\n", error.message);
97 if (strcmp (error.name, DBUS_ERROR_NO_MEMORY) != 0)
99 echo_reply = dbus_message_new_error (echo_reply,
103 if (echo_reply == NULL)
106 if (!dbus_connection_send (connection, echo_reply, NULL))
109 dbus_message_unref (echo_reply);
111 dbus_error_free (&error);
114 _dbus_assert_not_reached ("Unexpected message received\n");
116 hello_from_self_reply_received = TRUE;
118 dbus_message_unref (reply);
119 dbus_message_unref (echo_message);
120 dbus_pending_call_unref (pcall);
121 dbus_connection_unref (connection);
124 static DBusHandlerResult
125 handle_run_hello_from_self (DBusConnection *connection,
126 DBusMessage *message)
129 DBusMessage *reply, *self_message;
130 DBusPendingCall *pcall;
133 _dbus_verbose ("sending reply to Echo method\n");
135 dbus_error_init (&error);
137 if (!dbus_message_get_args (message,
139 DBUS_TYPE_STRING, &s,
142 reply = dbus_message_new_error (message,
149 if (!dbus_connection_send (connection, reply, NULL))
152 dbus_message_unref (reply);
154 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
156 printf ("Sending HelloFromSelf\n");
158 _dbus_verbose ("*** Sending message to self\n");
159 self_message = dbus_message_new_method_call ("org.freedesktop.DBus.TestSuiteEchoService",
160 "/org/freedesktop/TestSuite",
161 "org.freedesktop.TestSuite",
164 if (self_message == NULL)
167 if (!dbus_connection_send_with_reply (connection, self_message, &pcall, -1))
170 dbus_message_ref (message);
171 if (!dbus_pending_call_set_notify (pcall, check_hello_from_self_reply, (void *)message, NULL))
174 printf ("Sent HelloFromSelf\n");
175 return DBUS_HANDLER_RESULT_HANDLED;
178 static DBusHandlerResult
179 handle_echo (DBusConnection *connection,
180 DBusMessage *message)
186 _dbus_verbose ("sending reply to Echo method\n");
188 dbus_error_init (&error);
190 if (!dbus_message_get_args (message,
192 DBUS_TYPE_STRING, &s,
195 reply = dbus_message_new_error (message,
202 if (!dbus_connection_send (connection, reply, NULL))
205 dbus_message_unref (reply);
207 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
210 reply = dbus_message_new_method_return (message);
214 if (!dbus_message_append_args (reply,
215 DBUS_TYPE_STRING, &s,
219 if (!dbus_connection_send (connection, reply, NULL))
222 fprintf (stderr, "Echo service echoed string: \"%s\"\n", s);
224 dbus_message_unref (reply);
226 return DBUS_HANDLER_RESULT_HANDLED;
229 static DBusHandlerResult
230 handle_delay_echo (DBusConnection *connection,
231 DBusMessage *message)
237 _dbus_verbose ("sleeping for a short time\n");
239 _dbus_sleep_milliseconds (50);
241 _dbus_verbose ("sending reply to DelayEcho method\n");
243 dbus_error_init (&error);
245 if (!dbus_message_get_args (message,
247 DBUS_TYPE_STRING, &s,
250 reply = dbus_message_new_error (message,
257 if (!dbus_connection_send (connection, reply, NULL))
260 dbus_message_unref (reply);
262 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
265 reply = dbus_message_new_method_return (message);
269 if (!dbus_message_append_args (reply,
270 DBUS_TYPE_STRING, &s,
274 if (!dbus_connection_send (connection, reply, NULL))
277 fprintf (stderr, "DelayEcho service echoed string: \"%s\"\n", s);
279 dbus_message_unref (reply);
281 return DBUS_HANDLER_RESULT_HANDLED;
286 path_unregistered_func (DBusConnection *connection,
289 /* connection was finalized */
292 static DBusHandlerResult
293 path_message_func (DBusConnection *connection,
294 DBusMessage *message,
297 if (dbus_message_is_method_call (message,
298 "org.freedesktop.TestSuite",
300 return handle_echo (connection, message);
301 else if (dbus_message_is_method_call (message,
302 "org.freedesktop.TestSuite",
304 return handle_delay_echo (connection, message);
305 else if (dbus_message_is_method_call (message,
306 "org.freedesktop.TestSuite",
310 return DBUS_HANDLER_RESULT_HANDLED;
312 else if (dbus_message_is_method_call (message,
313 "org.freedesktop.TestSuite",
316 /* Emit the Foo signal */
320 _dbus_verbose ("emitting signal Foo\n");
322 signal = dbus_message_new_signal ("/org/freedesktop/TestSuite",
323 "org.freedesktop.TestSuite",
329 if (!dbus_message_append_args (signal,
330 DBUS_TYPE_DOUBLE, &v_DOUBLE,
334 if (!dbus_connection_send (connection, signal, NULL))
337 return DBUS_HANDLER_RESULT_HANDLED;
340 else if (dbus_message_is_method_call (message,
341 "org.freedesktop.TestSuite",
344 return handle_run_hello_from_self (connection, message);
346 else if (dbus_message_is_method_call (message,
347 "org.freedesktop.TestSuite",
351 printf ("Received the HelloFromSelf message\n");
353 reply = dbus_message_new_method_return (message);
357 if (!dbus_connection_send (connection, reply, NULL))
360 return DBUS_HANDLER_RESULT_HANDLED;
363 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
366 static DBusObjectPathVTable
368 path_unregistered_func,
374 static const char* echo_path = "/org/freedesktop/TestSuite" ;
376 static DBusHandlerResult
377 filter_func (DBusConnection *connection,
378 DBusMessage *message,
381 if (dbus_message_is_signal (message,
382 DBUS_INTERFACE_LOCAL,
386 return DBUS_HANDLER_RESULT_HANDLED;
390 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
400 DBusConnection *connection;
406 name = "org.freedesktop.DBus.TestSuiteEchoService";
413 do_fork = strcmp (argv[2], "fork") == 0;
417 /* The bare minimum for simulating a program "daemonizing"; the intent
418 * is to test services which move from being legacy init scripts to
419 * activated services.
420 * https://bugzilla.redhat.com/show_bug.cgi?id=545267
431 dbus_error_init (&error);
432 connection = dbus_bus_get (DBUS_BUS_STARTER, &error);
433 if (connection == NULL)
435 fprintf (stderr, "*** Failed to open connection to activating message bus: %s\n",
437 dbus_error_free (&error);
441 loop = _dbus_loop_new ();
445 if (!test_connection_setup (loop, connection))
448 if (!dbus_connection_add_filter (connection,
449 filter_func, NULL, NULL))
452 if (!dbus_connection_register_object_path (connection,
460 if (!dbus_connection_get_object_path_data (connection, echo_path, &d))
462 if (d != (void*) 0xdeadbeef)
463 die ("dbus_connection_get_object_path_data() doesn't seem to work right\n");
466 result = dbus_bus_request_name (connection, name,
468 if (dbus_error_is_set (&error))
470 fprintf (stderr, "Error %s\n", error.message);
471 _dbus_verbose ("*** Failed to acquire service: %s\n",
473 dbus_error_free (&error);
477 _dbus_verbose ("*** Test service entering main loop\n");
478 _dbus_loop_run (loop);
480 test_connection_shutdown (loop, connection);
482 dbus_connection_remove_filter (connection, filter_func, NULL);
484 dbus_connection_unref (connection);
486 _dbus_loop_unref (loop);
491 _dbus_verbose ("*** Test service exiting\n");