diff options
author | Simon McVittie <smcv@debian.org> | 2018-01-22 17:52:55 +0000 |
---|---|---|
committer | Simon McVittie <smcv@debian.org> | 2018-01-22 18:35:43 +0000 |
commit | c20597919cd35b513e7a8eb2fa59cb0cec900606 (patch) | |
tree | 875d4fa92e6a348376b6293783c8a517e81716d1 /dbus-gmain | |
parent | 449f30a089ce848883fcf2b6aaac742f87aad704 (diff) |
Move tests for dbus-gmain to dbus-gmain/tests/
Use dbus-run-session to run the only one that can be used as an
automated test right now.
Signed-off-by: Simon McVittie <smcv@debian.org>
Diffstat (limited to 'dbus-gmain')
-rw-r--r-- | dbus-gmain/Makefile.am | 59 | ||||
-rw-r--r-- | dbus-gmain/tests/30574.c | 114 | ||||
-rw-r--r-- | dbus-gmain/tests/test-thread-client.c | 97 | ||||
-rw-r--r-- | dbus-gmain/tests/test-thread-server.c | 209 | ||||
-rw-r--r-- | dbus-gmain/tests/test-thread.h | 1 | ||||
-rw-r--r-- | dbus-gmain/tests/util.c | 44 | ||||
-rw-r--r-- | dbus-gmain/tests/util.h | 32 |
7 files changed, 555 insertions, 1 deletions
diff --git a/dbus-gmain/Makefile.am b/dbus-gmain/Makefile.am index 4b4291e..1863a1f 100644 --- a/dbus-gmain/Makefile.am +++ b/dbus-gmain/Makefile.am @@ -5,7 +5,10 @@ AM_CPPFLAGS = \ $(DBUS_GLIB_CFLAGS) \ $(NULL) -noinst_LTLIBRARIES = libdbus-gmain.la +noinst_LTLIBRARIES = \ + libdbus-gmain.la \ + tests/libtest.la \ + $(NULL) libdbus_gmain_la_SOURCES = \ dbus-gmain.c \ @@ -14,3 +17,57 @@ libdbus_gmain_la_SOURCES = \ libdbus_gmain_la_LIBADD = $(DBUS_LIBS) $(DBUS_GLIB_LIBS) libdbus_gmain_la_LDFLAGS = -no-undefined + +tests_libtest_la_SOURCES = \ + tests/util.c \ + tests/util.h \ + $(NULL) + +tests_libtest_la_LIBADD = $(DBUS_LIBS) $(DBUS_GLIB_LIBS) +tests_libtest_la_LDFLAGS = -no-undefined + +TESTS = \ + tests/test-30574 \ + $(NULL) + +noinst_PROGRAMS = \ + tests/test-30574 \ + tests/test-thread-server \ + tests/test-thread-client \ + $(NULL) + +tests_test_thread_server_SOURCES = \ + tests/test-thread-server.c \ + tests/test-thread.h \ + $(NULL) +tests_test_thread_server_LDADD = \ + libdbus-gmain.la \ + tests/libtest.la \ + $(DBUS_GLIB_THREADS_LIBS) \ + $(DBUS_GLIB_LIBS) \ + $(DBUS_LIBS) \ + $(NULL) + +tests_test_thread_client_SOURCES = \ + tests/test-thread-client.c \ + tests/test-thread.h \ + $(NULL) +tests_test_thread_client_LDADD = \ + libdbus-gmain.la \ + tests/libtest.la \ + $(DBUS_GLIB_THREADS_LIBS) \ + $(DBUS_GLIB_LIBS) \ + $(DBUS_LIBS) \ + $(NULL) + +tests_test_30574_SOURCES = \ + tests/30574.c \ + $(NULL) +tests_test_30574_LDADD = \ + libdbus-gmain.la \ + tests/libtest.la \ + $(DBUS_GLIB_LIBS) \ + $(DBUS_LIBS) \ + $(NULL) + +LOG_COMPILER = $(DBUS_RUN_SESSION) -- diff --git a/dbus-gmain/tests/30574.c b/dbus-gmain/tests/30574.c new file mode 100644 index 0000000..3c7e109 --- /dev/null +++ b/dbus-gmain/tests/30574.c @@ -0,0 +1,114 @@ +#include <config.h> + +#include <stdio.h> +#include <stdlib.h> +#include <dbus/dbus.h> +#include <glib.h> +#include <dbus-gmain/dbus-gmain.h> +#include "util.h" + +DBusConnection *bus; +GMainContext *main_context; + +typedef struct _SpiReentrantCallClosure +{ + GMainLoop *loop; + DBusMessage *reply; +} SpiReentrantCallClosure; + +static void +set_reply (DBusPendingCall * pending, void *user_data) +{ + SpiReentrantCallClosure* closure = (SpiReentrantCallClosure *) user_data; + + closure->reply = dbus_pending_call_steal_reply (pending); + dbus_gmain_set_up_connection (bus, NULL); + + g_main_loop_quit (closure->loop); +} + +static DBusMessage * +send_and_allow_reentry (DBusConnection * bus, DBusMessage * message, + dbus_bool_t switch_after_send) +{ + DBusPendingCall *pending; + SpiReentrantCallClosure closure; + + closure.loop = g_main_loop_new (main_context, FALSE); + dbus_gmain_set_up_connection (bus, (switch_after_send ? NULL : + main_context)); + + if (!dbus_connection_send_with_reply (bus, message, &pending, 3000)) + { + dbus_gmain_set_up_connection (bus, NULL); + return NULL; + } + dbus_pending_call_set_notify (pending, set_reply, (void *) &closure, NULL); + if (switch_after_send) + dbus_gmain_set_up_connection (bus, main_context); + g_main_loop_run (closure.loop); + + g_main_loop_unref (closure.loop); + dbus_pending_call_unref (pending); + return closure.reply; +} + +static void +send_test_message (dbus_bool_t switch_after_send) +{ + DBusMessage *message, *reply; + const char *str; + DBusError error; + + dbus_error_init (&error); + message = dbus_message_new_method_call ("org.freedesktop.DBus", + "/org/freedesktop/DBus", + DBUS_INTERFACE_DBUS, "GetId"); + reply = send_and_allow_reentry (bus, message, switch_after_send); + if (!reply) + { + fprintf(stderr, "Got no reply from send_and_allow_reentry\n"); + exit(1); + } + if (dbus_message_get_type (reply) == DBUS_MESSAGE_TYPE_ERROR) + { + char *err = NULL; + dbus_message_get_args (reply, NULL, DBUS_TYPE_STRING, &err, DBUS_TYPE_INVALID); + fprintf (stderr, "Got error: %s\n", err); + exit(1); + } + if (!dbus_message_get_args (reply, &error, DBUS_TYPE_STRING, &str, DBUS_TYPE_INVALID)) + { + fprintf(stderr, "Sorry; can't communicate: %s\n", error.message); + exit(1); + } + dbus_message_unref (reply); + dbus_message_unref (message); +} + +int +main(int argc, const char *argv[]) +{ + DBusError error; + + main_context = g_main_context_new (); + dbus_error_init (&error); + bus = dbus_bus_get_private (DBUS_BUS_SESSION, &error); + if (!bus) + { + fprintf(stderr, "Couldn't connect to bus: %s\n", error.name); + return 1; + } + dbus_gmain_set_up_connection (bus, NULL); + send_test_message (FALSE); + send_test_message (FALSE); + send_test_message (TRUE); + + test_run_until_disconnected (bus, NULL); + dbus_connection_unref (bus); + + dbus_shutdown (); + g_main_context_unref (main_context); + + return 0; +} diff --git a/dbus-gmain/tests/test-thread-client.c b/dbus-gmain/tests/test-thread-client.c new file mode 100644 index 0000000..2310a14 --- /dev/null +++ b/dbus-gmain/tests/test-thread-client.c @@ -0,0 +1,97 @@ +#include <config.h> + +#include <glib.h> +#include <dbus-gmain/dbus-gmain.h> +#include <stdio.h> +#include <string.h> +#include <unistd.h> + +#include "test-thread.h" + +DBusConnection *connection; + +static gpointer +thread_func (gpointer data) +{ + gint32 threadnr = GPOINTER_TO_INT (data); + guint32 counter = 0; + DBusMessageIter iter; + DBusMessage *message; + char *str; + + while (1) + { + message = dbus_message_new_method_call (NULL, + "/org/freedesktop/DBus/GLib/ThreadTest", + "org.freedesktop.DBus.GLib.ThreadTest", + "TestMethod"); + + dbus_message_iter_init_append (message, &iter); + + if (!dbus_message_iter_append_basic (&iter, DBUS_TYPE_INT32, &threadnr)) + { + g_print ("thread %d: append threadnr failed\n", threadnr); + } + + if (!dbus_message_iter_append_basic (&iter, DBUS_TYPE_INT32, &counter)) + { + g_print ("thread %d: append counter (%d) failed\n", threadnr, counter); + } + + str = g_strdup_printf ("Thread %d-%d\n", threadnr, counter); + if (!dbus_message_iter_append_basic (&iter, DBUS_TYPE_STRING, &str)) + { + g_print ("thread %d: append string (%s) failed\n", threadnr, str); + } + g_free (str); + + if (!dbus_connection_send (connection, + message, + NULL)) + { + g_print ("thread %d: send message failed\n", threadnr); + } + + dbus_message_unref (message); + + counter ++; + } + + return NULL; +} + +int +main (int argc, char *argv[]) +{ + GMainLoop *loop; + DBusError error; + int i; + + if(argc < 2) + { + g_error("Need an address as argv[1]\n"); + return 1; + } + + dbus_error_init (&error); + connection = dbus_connection_open (argv[1], &error); + if (connection == NULL) + { + g_printerr ("could not open connection: %s\n", error.message); + dbus_error_free (&error); + return 1; + } + + dbus_gmain_set_up_connection (connection, NULL); + + for (i = 0; i < N_TEST_THREADS; i++) + { + g_thread_create (thread_func, GINT_TO_POINTER (i), FALSE, NULL); + } + + loop = g_main_loop_new (NULL, FALSE); + g_main_loop_run (loop); + + return 0; +} + diff --git a/dbus-gmain/tests/test-thread-server.c b/dbus-gmain/tests/test-thread-server.c new file mode 100644 index 0000000..15b8b24 --- /dev/null +++ b/dbus-gmain/tests/test-thread-server.c @@ -0,0 +1,209 @@ +#include <config.h> + +#include <glib.h> +#include <dbus-gmain/dbus-gmain.h> +#include <stdio.h> +#include <string.h> + +#include "test-thread.h" + +typedef struct { + guint32 counters[N_TEST_THREADS]; +} ThreadTestData; + +static ThreadTestData * +thread_test_data_new (void) +{ + ThreadTestData *data; + + data = g_new0 (ThreadTestData, 1); + + return data; +} + +static void +thread_test_data_free (ThreadTestData *data) +{ + g_free (data); +} + +static DBusHandlerResult +filter_test_message (DBusConnection *connection, + DBusMessage *message, + void *user_data) +{ + ThreadTestData *data = user_data; + DBusMessageIter iter; + gint32 threadnr; + guint32 counter; + const char *str; + char *expected_str; + GString *counter_str; + int i; + + if (!dbus_message_is_method_call (message, "org.freedesktop.DBus.GLib.ThreadTest", + "TestMethod")) + return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; + + dbus_message_iter_init (message, &iter); + + if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_INT32) + { + g_print ("First arg not right type\n"); + goto out; + } + dbus_message_iter_get_basic (&iter, &threadnr); + if (threadnr < 0 || threadnr >= N_TEST_THREADS) + { + g_print ("Invalid thread nr\n"); + goto out; + } + + if (! dbus_message_iter_next (&iter)) + { + g_print ("Couldn't get second arg\n"); + goto out; + } + + if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_INT32) + { + g_print ("Second arg not right type\n"); + goto out; + } + + dbus_message_iter_get_basic (&iter, &counter); + + if (counter != data->counters[threadnr]) + { + g_print ("Thread %d, counter %d, expected %d\n", threadnr, counter, data->counters[threadnr]); + goto out; + } + data->counters[threadnr]++; + + if (! dbus_message_iter_next (&iter)) + { + g_print ("Couldn't get third arg\n"); + goto out; + } + + if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_STRING) + { + g_print ("Third arg not right type\n"); + goto out; + } + + dbus_message_iter_get_basic (&iter, &str); + + if (str == NULL) + { + g_print ("No third arg\n"); + goto out; + } + + expected_str = g_strdup_printf ("Thread %d-%d\n", threadnr, counter); + if (strcmp (expected_str, str) != 0) + { + g_print ("Wrong string '%s', expected '%s'\n", str, expected_str); + g_free (expected_str); + goto out; + } + g_free (expected_str); + + if (dbus_message_iter_next (&iter)) + { + g_print ("Extra args on end of message\n"); + goto out; + } + + dbus_connection_flush (connection); + + counter_str = g_string_new (""); + for (i = 0; i < N_TEST_THREADS; i++) + { + g_string_append_printf (counter_str, "%d ", data->counters[i]); + } + g_print ("%s\r", counter_str->str); + g_string_free (counter_str, TRUE); + + out: + return DBUS_HANDLER_RESULT_HANDLED; +} + +static DBusHandlerResult +filter_disconnect (DBusConnection *connection, + DBusMessage *message, + void *user_data) +{ + if (!dbus_message_is_signal (message, DBUS_INTERFACE_LOCAL, + "Disconnected")) + return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; + + g_print ("connection disconnected\n"); + dbus_connection_unref (connection); + + return DBUS_HANDLER_RESULT_HANDLED; +} + +static void +new_connection_callback (DBusServer *server, + DBusConnection *new_connection, + void *user_data) +{ + ThreadTestData * data; + + g_print ("new_connection_callback\n"); + + dbus_connection_ref (new_connection); + dbus_gmain_set_up_connection (new_connection, NULL); + + data = thread_test_data_new (); + + if (!dbus_connection_add_filter (new_connection, + filter_test_message, data, + (DBusFreeFunction) thread_test_data_free)) + goto nomem; + + if (!dbus_connection_add_filter (new_connection, + filter_disconnect, NULL, NULL)) + goto nomem; + + return; + + nomem: + g_error ("no memory to setup new connection"); +} + +int +main (int argc, char *argv[]) +{ + GMainLoop *loop; + DBusServer *server; + DBusError error; + + if (argc < 2) + { + fprintf (stderr, "Give the server address as an argument\n"); + return 1; + } + + dbus_error_init (&error); + server = dbus_server_listen (argv[1], &error); + if (server == NULL) + { + fprintf (stderr, "Failed to start server on %s: %s\n", + argv[1], error.message); + dbus_error_free (&error); + return 1; + } + + dbus_server_set_new_connection_function (server, + new_connection_callback, + NULL, NULL); + + dbus_gmain_set_up_server (server, NULL); + + loop = g_main_loop_new (NULL, FALSE); + g_main_loop_run (loop); + + return 0; +} diff --git a/dbus-gmain/tests/test-thread.h b/dbus-gmain/tests/test-thread.h new file mode 100644 index 0000000..8c78fba --- /dev/null +++ b/dbus-gmain/tests/test-thread.h @@ -0,0 +1 @@ +#define N_TEST_THREADS 5 diff --git a/dbus-gmain/tests/util.c b/dbus-gmain/tests/util.c new file mode 100644 index 0000000..e819584 --- /dev/null +++ b/dbus-gmain/tests/util.c @@ -0,0 +1,44 @@ +/* Regression test utilities + * + * Copyright © 2009 Collabora Ltd. <http://www.collabora.co.uk/> + * Copyright © 2009-2011 Nokia Corporation + * + * Licensed under the Academic Free License version 2.1 + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301 USA + */ + +#include <config.h> + +#include "util.h" + +void +test_run_until_disconnected (DBusConnection *connection, + GMainContext *context) +{ + g_printerr ("Disconnecting... "); + + dbus_connection_set_exit_on_disconnect (connection, FALSE); + dbus_connection_close (connection); + + while (dbus_connection_get_is_connected (connection)) + { + g_printerr ("."); + g_main_context_iteration (context, TRUE); + } + + g_printerr (" disconnected\n"); +} diff --git a/dbus-gmain/tests/util.h b/dbus-gmain/tests/util.h new file mode 100644 index 0000000..7f5cb07 --- /dev/null +++ b/dbus-gmain/tests/util.h @@ -0,0 +1,32 @@ +/* Regression test utilities + * + * Copyright © 2009 Collabora Ltd. <http://www.collabora.co.uk/> + * Copyright © 2009-2011 Nokia Corporation + * + * Licensed under the Academic Free License version 2.1 + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301 USA + */ + +#ifndef DBUS_GLIB_TEST_UTIL_H + +#include <dbus/dbus.h> +#include <glib.h> + +void test_run_until_disconnected (DBusConnection *connection, + GMainContext *context); + +#endif |