3 #include "test-utils.h"
9 static dbus_bool_t already_quit = FALSE;
10 static dbus_bool_t hello_from_self_reply_received = FALSE;
17 _dbus_loop_quit (loop);
23 die (const char *message)
25 fprintf (stderr, "*** test-service: %s", message);
30 check_hello_from_self_reply (DBusPendingCall *pcall,
34 DBusMessage *echo_message, *echo_reply = NULL;
36 DBusConnection *connection;
40 dbus_error_init (&error);
42 connection = dbus_bus_get (DBUS_BUS_STARTER, &error);
43 if (connection == NULL)
45 fprintf (stderr, "*** Failed to open connection to activating message bus: %s\n",
47 dbus_error_free (&error);
52 echo_message = (DBusMessage *)user_data;
54 reply = dbus_pending_call_steal_reply (pcall);
56 type = dbus_message_get_type (reply);
58 if (type == DBUS_MESSAGE_TYPE_METHOD_RETURN)
61 printf ("Reply from HelloFromSelf received\n");
63 if (!dbus_message_get_args (echo_message,
68 echo_reply = dbus_message_new_error (echo_message,
72 if (echo_reply == NULL)
78 echo_reply = dbus_message_new_method_return (echo_message);
79 if (echo_reply == NULL)
82 if (!dbus_message_append_args (echo_reply,
88 if (!dbus_connection_send (connection, echo_reply, NULL))
91 dbus_message_unref (echo_reply);
93 else if (type == DBUS_MESSAGE_TYPE_ERROR)
95 dbus_set_error_from_message (&error, reply);
96 printf ("Error type in reply: %s\n", error.message);
98 if (strcmp (error.name, DBUS_ERROR_NO_MEMORY) != 0)
100 echo_reply = dbus_message_new_error (echo_reply,
104 if (echo_reply == NULL)
107 if (!dbus_connection_send (connection, echo_reply, NULL))
110 dbus_message_unref (echo_reply);
112 dbus_error_free (&error);
115 _dbus_assert_not_reached ("Unexpected message received\n");
117 hello_from_self_reply_received = TRUE;
119 dbus_message_unref (reply);
120 dbus_message_unref (echo_message);
121 dbus_pending_call_unref (pcall);
122 dbus_connection_unref (connection);
125 static DBusHandlerResult
126 handle_run_hello_from_self (DBusConnection *connection,
127 DBusMessage *message)
130 DBusMessage *reply, *self_message;
131 DBusPendingCall *pcall;
134 _dbus_verbose ("sending reply to Echo method\n");
136 dbus_error_init (&error);
138 if (!dbus_message_get_args (message,
140 DBUS_TYPE_STRING, &s,
143 reply = dbus_message_new_error (message,
150 if (!dbus_connection_send (connection, reply, NULL))
153 dbus_message_unref (reply);
155 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
157 printf ("Sending HelloFromSelf\n");
159 _dbus_verbose ("*** Sending message to self\n");
160 self_message = dbus_message_new_method_call ("org.freedesktop.DBus.TestSuiteEchoService",
161 "/org/freedesktop/TestSuite",
162 "org.freedesktop.TestSuite",
165 if (self_message == NULL)
168 if (!dbus_connection_send_with_reply (connection, self_message, &pcall, -1))
171 dbus_message_ref (message);
172 if (!dbus_pending_call_set_notify (pcall, check_hello_from_self_reply, (void *)message, NULL))
175 printf ("Sent HelloFromSelf\n");
176 return DBUS_HANDLER_RESULT_HANDLED;
179 static DBusHandlerResult
180 handle_echo (DBusConnection *connection,
181 DBusMessage *message)
187 _dbus_verbose ("sending reply to Echo method\n");
189 dbus_error_init (&error);
191 if (!dbus_message_get_args (message,
193 DBUS_TYPE_STRING, &s,
196 reply = dbus_message_new_error (message,
203 if (!dbus_connection_send (connection, reply, NULL))
206 dbus_message_unref (reply);
208 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
211 reply = dbus_message_new_method_return (message);
215 if (!dbus_message_append_args (reply,
216 DBUS_TYPE_STRING, &s,
220 if (!dbus_connection_send (connection, reply, NULL))
223 fprintf (stderr, "Echo service echoed string: \"%s\"\n", s);
225 dbus_message_unref (reply);
227 return DBUS_HANDLER_RESULT_HANDLED;
230 static DBusHandlerResult
231 handle_delay_echo (DBusConnection *connection,
232 DBusMessage *message)
238 _dbus_verbose ("sleeping for a short time\n");
240 _dbus_sleep_milliseconds (50);
242 _dbus_verbose ("sending reply to DelayEcho method\n");
244 dbus_error_init (&error);
246 if (!dbus_message_get_args (message,
248 DBUS_TYPE_STRING, &s,
251 reply = dbus_message_new_error (message,
258 if (!dbus_connection_send (connection, reply, NULL))
261 dbus_message_unref (reply);
263 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
266 reply = dbus_message_new_method_return (message);
270 if (!dbus_message_append_args (reply,
271 DBUS_TYPE_STRING, &s,
275 if (!dbus_connection_send (connection, reply, NULL))
278 fprintf (stderr, "DelayEcho service echoed string: \"%s\"\n", s);
280 dbus_message_unref (reply);
282 return DBUS_HANDLER_RESULT_HANDLED;
287 path_unregistered_func (DBusConnection *connection,
290 /* connection was finalized */
293 static DBusHandlerResult
294 path_message_func (DBusConnection *connection,
295 DBusMessage *message,
298 if (dbus_message_is_method_call (message,
299 "org.freedesktop.TestSuite",
301 return handle_echo (connection, message);
302 else if (dbus_message_is_method_call (message,
303 "org.freedesktop.TestSuite",
305 return handle_delay_echo (connection, message);
306 else if (dbus_message_is_method_call (message,
307 "org.freedesktop.TestSuite",
311 return DBUS_HANDLER_RESULT_HANDLED;
313 else if (dbus_message_is_method_call (message,
314 "org.freedesktop.TestSuite",
317 /* Emit the Foo signal */
321 _dbus_verbose ("emitting signal Foo\n");
323 signal = dbus_message_new_signal ("/org/freedesktop/TestSuite",
324 "org.freedesktop.TestSuite",
330 if (!dbus_message_append_args (signal,
331 DBUS_TYPE_DOUBLE, &v_DOUBLE,
335 if (!dbus_connection_send (connection, signal, NULL))
338 return DBUS_HANDLER_RESULT_HANDLED;
341 else if (dbus_message_is_method_call (message,
342 "org.freedesktop.TestSuite",
345 return handle_run_hello_from_self (connection, message);
347 else if (dbus_message_is_method_call (message,
348 "org.freedesktop.TestSuite",
352 printf ("Received the HelloFromSelf message\n");
354 reply = dbus_message_new_method_return (message);
358 if (!dbus_connection_send (connection, reply, NULL))
361 return DBUS_HANDLER_RESULT_HANDLED;
364 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
367 static DBusObjectPathVTable
369 path_unregistered_func,
375 static const char* echo_path = "/org/freedesktop/TestSuite" ;
377 static DBusHandlerResult
378 filter_func (DBusConnection *connection,
379 DBusMessage *message,
382 if (dbus_message_is_signal (message,
383 DBUS_INTERFACE_LOCAL,
387 return DBUS_HANDLER_RESULT_HANDLED;
391 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
401 DBusConnection *connection;
407 name = "org.freedesktop.DBus.TestSuiteEchoService";
414 do_fork = strcmp (argv[2], "fork") == 0;
420 /* The bare minimum for simulating a program "daemonizing"; the intent
421 * is to test services which move from being legacy init scripts to
422 * activated services.
423 * https://bugzilla.redhat.com/show_bug.cgi?id=545267
435 dbus_error_init (&error);
436 connection = dbus_bus_get (DBUS_BUS_STARTER, &error);
437 if (connection == NULL)
439 fprintf (stderr, "*** Failed to open connection to activating message bus: %s\n",
441 dbus_error_free (&error);
445 loop = _dbus_loop_new ();
449 if (!test_connection_setup (loop, connection))
452 if (!dbus_connection_add_filter (connection,
453 filter_func, NULL, NULL))
456 if (!dbus_connection_register_object_path (connection,
464 if (!dbus_connection_get_object_path_data (connection, echo_path, &d))
466 if (d != (void*) 0xdeadbeef)
467 die ("dbus_connection_get_object_path_data() doesn't seem to work right\n");
470 result = dbus_bus_request_name (connection, name,
472 if (dbus_error_is_set (&error))
474 fprintf (stderr, "Error %s\n", error.message);
475 _dbus_verbose ("*** Failed to acquire service: %s\n",
477 dbus_error_free (&error);
481 if (result != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER)
483 fprintf (stderr, "Unable to acquire service: code %d\n", result);
484 _dbus_verbose ("*** Failed to acquire service: %d\n", result);
488 _dbus_verbose ("*** Test service entering main loop\n");
489 _dbus_loop_run (loop);
491 test_connection_shutdown (loop, connection);
493 dbus_connection_remove_filter (connection, filter_func, NULL);
495 dbus_connection_unref (connection);
497 _dbus_loop_unref (loop);
502 _dbus_verbose ("*** Test service exiting\n");