2 #include "test-utils.h"
6 static dbus_bool_t already_quit = FALSE;
7 static dbus_bool_t hello_from_self_reply_received = FALSE;
14 _dbus_loop_quit (loop);
20 die (const char *message)
22 fprintf (stderr, "*** test-service: %s", message);
27 check_hello_from_self_reply (DBusPendingCall *pcall,
31 DBusMessage *echo_message, *echo_reply = NULL;
33 DBusConnection *connection;
37 dbus_error_init (&error);
39 connection = dbus_bus_get (DBUS_BUS_STARTER, &error);
40 if (connection == NULL)
42 fprintf (stderr, "*** Failed to open connection to activating message bus: %s\n",
44 dbus_error_free (&error);
49 echo_message = (DBusMessage *)user_data;
51 reply = dbus_pending_call_steal_reply (pcall);
53 type = dbus_message_get_type (reply);
55 if (type == DBUS_MESSAGE_TYPE_METHOD_RETURN)
58 printf ("Reply from HelloFromSelf received\n");
60 if (!dbus_message_get_args (echo_message,
65 echo_reply = dbus_message_new_error (echo_message,
69 if (echo_reply == NULL)
75 echo_reply = dbus_message_new_method_return (echo_message);
76 if (echo_reply == NULL)
79 if (!dbus_message_append_args (echo_reply,
85 if (!dbus_connection_send (connection, echo_reply, NULL))
88 dbus_message_unref (echo_reply);
90 else if (type == DBUS_MESSAGE_TYPE_ERROR)
92 dbus_set_error_from_message (&error, reply);
93 printf ("Error type in reply: %s\n", error.message);
95 if (strcmp (error.name, DBUS_ERROR_NO_MEMORY) != 0)
97 echo_reply = dbus_message_new_error (echo_reply,
101 if (echo_reply == NULL)
104 if (!dbus_connection_send (connection, echo_reply, NULL))
107 dbus_message_unref (echo_reply);
109 dbus_error_free (&error);
112 _dbus_assert_not_reached ("Unexpected message received\n");
114 hello_from_self_reply_received = TRUE;
116 dbus_message_unref (reply);
117 dbus_message_unref (echo_message);
118 dbus_pending_call_unref (pcall);
119 dbus_connection_unref (connection);
122 static DBusHandlerResult
123 handle_run_hello_from_self (DBusConnection *connection,
124 DBusMessage *message)
127 DBusMessage *reply, *self_message;
128 DBusPendingCall *pcall;
131 _dbus_verbose ("sending reply to Echo method\n");
133 dbus_error_init (&error);
135 if (!dbus_message_get_args (message,
137 DBUS_TYPE_STRING, &s,
140 reply = dbus_message_new_error (message,
147 if (!dbus_connection_send (connection, reply, NULL))
150 dbus_message_unref (reply);
152 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
154 printf ("Sending HelloFromSelf\n");
156 _dbus_verbose ("*** Sending message to self\n");
157 self_message = dbus_message_new_method_call ("org.freedesktop.DBus.TestSuiteEchoService",
158 "/org/freedesktop/TestSuite",
159 "org.freedesktop.TestSuite",
162 if (self_message == NULL)
165 if (!dbus_connection_send_with_reply (connection, self_message, &pcall, -1))
168 dbus_message_ref (message);
169 if (!dbus_pending_call_set_notify (pcall, check_hello_from_self_reply, (void *)message, NULL))
172 printf ("Sent HelloFromSelf\n");
173 return DBUS_HANDLER_RESULT_HANDLED;
176 static DBusHandlerResult
177 handle_echo (DBusConnection *connection,
178 DBusMessage *message)
184 _dbus_verbose ("sending reply to Echo method\n");
186 dbus_error_init (&error);
188 if (!dbus_message_get_args (message,
190 DBUS_TYPE_STRING, &s,
193 reply = dbus_message_new_error (message,
200 if (!dbus_connection_send (connection, reply, NULL))
203 dbus_message_unref (reply);
205 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
208 reply = dbus_message_new_method_return (message);
212 if (!dbus_message_append_args (reply,
213 DBUS_TYPE_STRING, &s,
217 if (!dbus_connection_send (connection, reply, NULL))
220 fprintf (stderr, "Echo service echoed string: \"%s\"\n", s);
222 dbus_message_unref (reply);
224 return DBUS_HANDLER_RESULT_HANDLED;
227 static DBusHandlerResult
228 handle_delay_echo (DBusConnection *connection,
229 DBusMessage *message)
235 _dbus_verbose ("sleeping for a short time\n");
239 _dbus_verbose ("sending reply to DelayEcho method\n");
241 dbus_error_init (&error);
243 if (!dbus_message_get_args (message,
245 DBUS_TYPE_STRING, &s,
248 reply = dbus_message_new_error (message,
255 if (!dbus_connection_send (connection, reply, NULL))
258 dbus_message_unref (reply);
260 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
263 reply = dbus_message_new_method_return (message);
267 if (!dbus_message_append_args (reply,
268 DBUS_TYPE_STRING, &s,
272 if (!dbus_connection_send (connection, reply, NULL))
275 fprintf (stderr, "DelayEcho service echoed string: \"%s\"\n", s);
277 dbus_message_unref (reply);
279 return DBUS_HANDLER_RESULT_HANDLED;
284 path_unregistered_func (DBusConnection *connection,
287 /* connection was finalized */
290 static DBusHandlerResult
291 path_message_func (DBusConnection *connection,
292 DBusMessage *message,
295 if (dbus_message_is_method_call (message,
296 "org.freedesktop.TestSuite",
298 return handle_echo (connection, message);
299 else if (dbus_message_is_method_call (message,
300 "org.freedesktop.TestSuite",
302 return handle_delay_echo (connection, message);
303 else if (dbus_message_is_method_call (message,
304 "org.freedesktop.TestSuite",
308 return DBUS_HANDLER_RESULT_HANDLED;
310 else if (dbus_message_is_method_call (message,
311 "org.freedesktop.TestSuite",
314 /* Emit the Foo signal */
318 _dbus_verbose ("emitting signal Foo\n");
320 signal = dbus_message_new_signal ("/org/freedesktop/TestSuite",
321 "org.freedesktop.TestSuite",
327 if (!dbus_message_append_args (signal,
328 DBUS_TYPE_DOUBLE, &v_DOUBLE,
332 if (!dbus_connection_send (connection, signal, NULL))
335 return DBUS_HANDLER_RESULT_HANDLED;
338 else if (dbus_message_is_method_call (message,
339 "org.freedesktop.TestSuite",
342 return handle_run_hello_from_self (connection, message);
344 else if (dbus_message_is_method_call (message,
345 "org.freedesktop.TestSuite",
349 printf ("Received the HelloFromSelf message\n");
351 reply = dbus_message_new_method_return (message);
355 if (!dbus_connection_send (connection, reply, NULL))
358 return DBUS_HANDLER_RESULT_HANDLED;
361 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
364 static DBusObjectPathVTable
366 path_unregistered_func,
372 static const char* echo_path = "/org/freedesktop/TestSuite" ;
374 static DBusHandlerResult
375 filter_func (DBusConnection *connection,
376 DBusMessage *message,
379 if (dbus_message_is_signal (message,
380 DBUS_INTERFACE_LOCAL,
384 return DBUS_HANDLER_RESULT_HANDLED;
388 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
398 DBusConnection *connection;
404 name = "org.freedesktop.DBus.TestSuiteEchoService";
410 do_fork = strcmp (argv[2], "fork") == 0;
413 /* The bare minimum for simulating a program "daemonizing"; the intent
414 * is to test services which move from being legacy init scripts to
415 * activated services.
416 * https://bugzilla.redhat.com/show_bug.cgi?id=545267
426 dbus_error_init (&error);
427 connection = dbus_bus_get (DBUS_BUS_STARTER, &error);
428 if (connection == NULL)
430 fprintf (stderr, "*** Failed to open connection to activating message bus: %s\n",
432 dbus_error_free (&error);
436 loop = _dbus_loop_new ();
440 if (!test_connection_setup (loop, connection))
443 if (!dbus_connection_add_filter (connection,
444 filter_func, NULL, NULL))
447 if (!dbus_connection_register_object_path (connection,
455 if (!dbus_connection_get_object_path_data (connection, echo_path, &d))
457 if (d != (void*) 0xdeadbeef)
458 die ("dbus_connection_get_object_path_data() doesn't seem to work right\n");
461 result = dbus_bus_request_name (connection, name,
463 if (dbus_error_is_set (&error))
465 fprintf (stderr, "Error %s\n", error.message);
466 _dbus_verbose ("*** Failed to acquire service: %s\n",
468 dbus_error_free (&error);
472 _dbus_verbose ("*** Test service entering main loop\n");
473 _dbus_loop_run (loop);
475 test_connection_shutdown (loop, connection);
477 dbus_connection_remove_filter (connection, filter_func, NULL);
479 dbus_connection_unref (connection);
481 _dbus_loop_unref (loop);
486 _dbus_verbose ("*** Test service exiting\n");