From 636be6f92d4d8effd392ad1f894738849ec7af76 Mon Sep 17 00:00:00 2001 From: Havoc Pennington Date: Thu, 4 Sep 2003 00:21:36 +0000 Subject: 2003-09-03 Havoc Pennington * test/glib/Makefile.am: add this with random glib-linked test programs * glib/Makefile.am: remove the random test programs from here, leave only the unit tests * glib/dbus-gobject.c (_dbus_gobject_test): add test for uscore/javacaps conversion, and fix (get_object_property, set_object_property): change to .NET convention for mapping props to methods, set_FooBar/get_FooBar, since one language has such a convention we may as well copy it. Plus real methods in either getFooBar or get_foo_bar style won't collide with this convention. --- ChangeLog | 16 +++ configure.in | 1 + glib/Makefile.am | 34 +------ glib/dbus-gobject.c | 63 +++++++++--- glib/test-dbus-glib.c | 52 ---------- glib/test-profile.c | 226 ----------------------------------------- glib/test-thread-client.c | 98 ------------------ glib/test-thread-server.c | 209 ------------------------------------- glib/test-thread.h | 1 - test/Makefile.am | 7 ++ test/glib/Makefile.am | 44 ++++++++ test/glib/test-dbus-glib.c | 52 ++++++++++ test/glib/test-profile.c | 226 +++++++++++++++++++++++++++++++++++++++++ test/glib/test-thread-client.c | 98 ++++++++++++++++++ test/glib/test-thread-server.c | 209 +++++++++++++++++++++++++++++++++++++ test/glib/test-thread.h | 1 + 16 files changed, 704 insertions(+), 633 deletions(-) delete mode 100644 glib/test-dbus-glib.c delete mode 100644 glib/test-profile.c delete mode 100644 glib/test-thread-client.c delete mode 100644 glib/test-thread-server.c delete mode 100644 glib/test-thread.h create mode 100644 test/glib/Makefile.am create mode 100644 test/glib/test-dbus-glib.c create mode 100644 test/glib/test-profile.c create mode 100644 test/glib/test-thread-client.c create mode 100644 test/glib/test-thread-server.c create mode 100644 test/glib/test-thread.h diff --git a/ChangeLog b/ChangeLog index d9557bed..ccca8a8e 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,19 @@ +2003-09-03 Havoc Pennington + + * test/glib/Makefile.am: add this with random glib-linked test + programs + + * glib/Makefile.am: remove the random test programs from here, + leave only the unit tests + + * glib/dbus-gobject.c (_dbus_gobject_test): add test for + uscore/javacaps conversion, and fix + (get_object_property, set_object_property): change to .NET + convention for mapping props to methods, set_FooBar/get_FooBar, + since one language has such a convention we may as well copy it. + Plus real methods in either getFooBar or get_foo_bar style won't + collide with this convention. + 2003-09-01 Havoc Pennington * glib/dbus-gparser.c: implement diff --git a/configure.in b/configure.in index 1125ef60..c403fd8c 100644 --- a/configure.in +++ b/configure.in @@ -826,6 +826,7 @@ mono/Makefile bus/Makefile tools/Makefile test/Makefile +test/glib/Makefile doc/Makefile dbus-1.pc dbus-glib-1.pc diff --git a/glib/Makefile.am b/glib/Makefile.am index f3b43ed3..65d71cfd 100644 --- a/glib/Makefile.am +++ b/glib/Makefile.am @@ -34,30 +34,9 @@ dbus_glib_tool_LDADD= $(DBUS_GLIB_TOOL_LIBS) libdbus-glib-1.la $(top_builddir)/d if DBUS_BUILD_TESTS -if HAVE_GLIB_THREADS -THREAD_APPS=test-thread-server test-thread-client test-profile - -test_thread_server_SOURCES= \ - test-thread-server.c \ - test-thread.h - -test_thread_server_LDADD= $(DBUS_GLIB_THREADS_LIBS) $(top_builddir)/glib/libdbus-glib-1.la - -test_thread_client_SOURCES= \ - test-thread-client.c \ - test-thread.h - -test_thread_client_LDADD= $(DBUS_GLIB_THREADS_LIBS) $(top_builddir)/glib/libdbus-glib-1.la -endif - ## we use noinst_PROGRAMS not check_PROGRAMS for TESTS so that we ## build even when not doing "make check" -noinst_PROGRAMS= $(TESTS) test-dbus-glib $(THREAD_APPS) - -test_dbus_glib_SOURCES= \ - test-dbus-glib.c - -test_dbus_glib_LDADD= $(top_builddir)/glib/libdbus-glib-1.la +noinst_PROGRAMS= $(TESTS) ## note that TESTS has special meaning (stuff to use in make check) ## so if adding tests not to be run in make check, don't add them to @@ -74,15 +53,4 @@ else ### not building tests TESTS= -if HAVE_GLIB_THREADS -noinst_PROGRAMS=test-profile -endif - endif - -if HAVE_GLIB_THREADS -test_profile_SOURCES= \ - test-profile.c - -test_profile_LDADD= $(DBUS_GLIB_THREADS_LIBS) $(top_builddir)/glib/libdbus-glib-1.la -endif \ No newline at end of file diff --git a/glib/dbus-gobject.c b/glib/dbus-gobject.c index aa53265b..b0f6c139 100644 --- a/glib/dbus-gobject.c +++ b/glib/dbus-gobject.c @@ -35,7 +35,7 @@ static GStaticMutex info_hash_mutex = G_STATIC_MUTEX_INIT; static GHashTable *info_hash = NULL; static char* -javacaps_to_uscore (const char *caps) +wincaps_to_uscore (const char *caps) { const char *p; GString *str; @@ -62,7 +62,7 @@ javacaps_to_uscore (const char *caps) } static char* -uscore_to_javacaps (const char *uscore) +uscore_to_wincaps (const char *uscore) { const char *p; GString *str; @@ -233,11 +233,11 @@ handle_introspect (DBusConnection *connection, can_get = (spec->flags & G_PARAM_READABLE) != 0; - s = uscore_to_javacaps (spec->name); + s = uscore_to_wincaps (spec->name); if (can_set) { - g_string_append (xml, " \n"); @@ -248,7 +248,7 @@ handle_introspect (DBusConnection *connection, if (can_get) { - g_string_append (xml, " \n"); @@ -578,13 +578,13 @@ gobject_message_function (DBusConnection *connection, /* If no metainfo, we can still do properties and signals * via standard GLib introspection */ - setter = (member[0] == 's' && member[1] == 'e' && member[2] == 't'); - getter = (member[0] == 'g' && member[1] == 'e' && member[2] == 't'); + setter = (member[0] == 's' && member[1] == 'e' && member[2] == 't' && member[3] == '_'); + getter = (member[0] == 'g' && member[1] == 'e' && member[2] == 't' && member[3] == '_'); if (!(setter || getter)) return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; - s = javacaps_to_uscore (&member[3]); + s = wincaps_to_uscore (&member[4]); pspec = g_object_class_find_property (G_OBJECT_GET_CLASS (object), s); @@ -757,6 +757,7 @@ dbus_connection_register_gobject (DBusConnection *connection, /** @} */ /* end of public API */ #ifdef DBUS_BUILD_TESTS +#include /** * @ingroup DBusGLibInternals @@ -766,14 +767,48 @@ dbus_connection_register_gobject (DBusConnection *connection, dbus_bool_t _dbus_gobject_test (const char *test_data_dir) { - static struct { const char *javacaps; const char *uscore; } name_pairs[] = { - { "setFoo", "set_foo" }, - { "foo", "foo" }, - { "getFooBar", "get_foo_bar" }, - { "Hello", "hello" }, - { "frobateUIHandler", "frobate_ui_handler" } + int i; + static struct { const char *wincaps; const char *uscore; } name_pairs[] = { + { "SetFoo", "set_foo" }, + { "Foo", "foo" }, + { "GetFooBar", "get_foo_bar" }, + { "Hello", "hello" } + + /* Impossible-to-handle cases */ + /* { "FrobateUIHandler", "frobate_ui_handler" } */ }; + i = 0; + while (i < (int) G_N_ELEMENTS (name_pairs)) + { + char *uscore; + char *wincaps; + + uscore = wincaps_to_uscore (name_pairs[i].wincaps); + wincaps = uscore_to_wincaps (name_pairs[i].uscore); + + if (strcmp (uscore, name_pairs[i].uscore) != 0) + { + g_printerr ("\"%s\" should have been converted to \"%s\" not \"%s\"\n", + name_pairs[i].wincaps, name_pairs[i].uscore, + uscore); + exit (1); + } + + if (strcmp (wincaps, name_pairs[i].wincaps) != 0) + { + g_printerr ("\"%s\" should have been converted to \"%s\" not \"%s\"\n", + name_pairs[i].uscore, name_pairs[i].wincaps, + wincaps); + exit (1); + } + + g_free (uscore); + g_free (wincaps); + + ++i; + } + return TRUE; } diff --git a/glib/test-dbus-glib.c b/glib/test-dbus-glib.c deleted file mode 100644 index beda0a7a..00000000 --- a/glib/test-dbus-glib.c +++ /dev/null @@ -1,52 +0,0 @@ -/* -*- mode: C; c-file-style: "gnu" -*- */ -#include "dbus-glib.h" -#include - -int -main (int argc, char **argv) -{ - DBusConnection *connection; - DBusMessage *message, *reply; - GMainLoop *loop; - DBusError error; - - if (argc < 2) - { - g_printerr ("Give the server address as an argument\n"); - return 1; - } - - loop = g_main_loop_new (NULL, FALSE); - - dbus_error_init (&error); - connection = dbus_connection_open (argv[1], &error); - if (connection == NULL) - { - g_printerr ("Failed to open connection to %s: %s\n", argv[1], - error.message); - dbus_error_free (&error); - return 1; - } - - dbus_connection_setup_with_g_main (connection, NULL); - - message = dbus_message_new_method_call (DBUS_SERVICE_ORG_FREEDESKTOP_DBUS, - DBUS_PATH_ORG_FREEDESKTOP_DBUS, - DBUS_INTERFACE_ORG_FREEDESKTOP_DBUS, - "Hello"); - - dbus_error_init (&error); - reply = dbus_connection_send_with_reply_and_block (connection, message, -1, &error); - if (reply == NULL) - { - g_printerr ("Error on hello message: %s\n", error.message); - dbus_error_free (&error); - return 1; - } - - g_print ("reply received\n"); - - g_main_loop_run (loop); - - return 0; -} diff --git a/glib/test-profile.c b/glib/test-profile.c deleted file mode 100644 index 6d9d8e7f..00000000 --- a/glib/test-profile.c +++ /dev/null @@ -1,226 +0,0 @@ -/* -*- mode: C; c-file-style: "gnu" -*- */ -/* test-profile.c Program that does basic message-response for timing - * - * Copyright (C) 2003 Red Hat Inc. - * - * Licensed under the Academic Free License version 1.2 - * - * 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - */ - -/* FIXME this test is wacky since both client and server keep - * sending each other method calls, but nobody sends - * a DBUS_MESSAGE_TYPE_METHOD_RETURN - */ - -#include -#include -#include "dbus-glib.h" -#include - -#define N_CLIENT_THREADS 1 -#define N_ITERATIONS 1000 -#define PAYLOAD_SIZE 30 -#define ECHO_PATH "/org/freedesktop/EchoTest" -#define ECHO_INTERFACE "org.freedesktop.EchoTest" -#define ECHO_METHOD "EchoProfile" - -static const char *address; -static unsigned char *payload; - -static void -send_echo_message (DBusConnection *connection) -{ - DBusMessage *message; - - message = dbus_message_new_method_call (NULL, ECHO_PATH, - ECHO_INTERFACE, ECHO_METHOD); - dbus_message_append_args (message, - DBUS_TYPE_STRING, "Hello World!", - DBUS_TYPE_INT32, 123456, -#if 1 - DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE, - payload, PAYLOAD_SIZE, -#endif - DBUS_TYPE_INVALID); - - dbus_connection_send (connection, message, NULL); - dbus_message_unref (message); - dbus_connection_flush (connection); -} - -static DBusHandlerResult -client_filter (DBusConnection *connection, - DBusMessage *message, - void *user_data) -{ - int *iterations = user_data; - - if (dbus_message_is_signal (message, - DBUS_INTERFACE_ORG_FREEDESKTOP_LOCAL, - "Disconnected")) - { - g_printerr ("Client thread disconnected\n"); - exit (1); - } - else if (dbus_message_is_method_call (message, - ECHO_INTERFACE, ECHO_METHOD)) - { - *iterations += 1; - if (*iterations >= N_ITERATIONS) - { - g_print ("Completed %d iterations\n", N_ITERATIONS); - exit (0); - } - send_echo_message (connection); - return DBUS_HANDLER_RESULT_HANDLED; - } - - return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; -} - -static void* -thread_func (void *data) -{ - DBusError error; - GMainContext *context; - GMainLoop *loop; - DBusConnection *connection; - int iterations; - - g_printerr ("Starting client thread\n"); - - dbus_error_init (&error); - connection = dbus_connection_open (address, &error); - if (connection == NULL) - { - g_printerr ("could not open connection: %s\n", error.message); - dbus_error_free (&error); - exit (1); - } - - iterations = 1; - - if (!dbus_connection_add_filter (connection, - client_filter, &iterations, NULL)) - g_error ("no memory"); - - context = g_main_context_new (); - loop = g_main_loop_new (context, FALSE); - - dbus_connection_setup_with_g_main (connection, context); - - g_printerr ("Client thread sending message to prime pingpong\n"); - send_echo_message (connection); - g_printerr ("Client thread sent message\n"); - - g_printerr ("Client thread entering main loop\n"); - g_main_loop_run (loop); - g_printerr ("Client thread exiting main loop\n"); - - g_main_loop_unref (loop); - g_main_context_unref (context); - - return NULL; -} - -static DBusHandlerResult -server_filter (DBusConnection *connection, - DBusMessage *message, - void *user_data) -{ - if (dbus_message_is_signal (message, - DBUS_INTERFACE_ORG_FREEDESKTOP_LOCAL, - "Disconnected")) - { - g_printerr ("Server thread disconnected\n"); - exit (1); - } - else if (dbus_message_is_method_call (message, - ECHO_INTERFACE, - ECHO_METHOD)) - { - send_echo_message (connection); - return DBUS_HANDLER_RESULT_HANDLED; - } - - return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; -} - -static void -new_connection_callback (DBusServer *server, - DBusConnection *new_connection, - void *user_data) -{ - dbus_connection_ref (new_connection); - dbus_connection_setup_with_g_main (new_connection, NULL); - - if (!dbus_connection_add_filter (new_connection, - server_filter, NULL, NULL)) - g_error ("no memory"); - - - /* FIXME we leak the handler */ -} - -int -main (int argc, char *argv[]) -{ - GMainLoop *loop; - DBusError error; - DBusServer *server; - int i; - - g_thread_init (NULL); - dbus_gthread_init (); - - dbus_error_init (&error); - server = dbus_server_listen ("unix:tmpdir="DBUS_TEST_SOCKET_DIR, - &error); - if (server == NULL) - { - g_printerr ("Could not start server: %s\n", - error.message); - return 1; - } - - address = dbus_server_get_address (server); - payload = g_malloc (PAYLOAD_SIZE); - - dbus_server_set_new_connection_function (server, - new_connection_callback, - NULL, NULL); - - loop = g_main_loop_new (NULL, FALSE); - - dbus_server_setup_with_g_main (server, NULL); - - for (i = 0; i < N_CLIENT_THREADS; i++) - { - g_thread_create (thread_func, NULL, FALSE, NULL); - } - - g_printerr ("Server thread entering main loop\n"); - g_main_loop_run (loop); - g_printerr ("Server thread exiting main loop\n"); - - dbus_server_unref (server); - - g_main_loop_unref (loop); - - return 0; -} - diff --git a/glib/test-thread-client.c b/glib/test-thread-client.c deleted file mode 100644 index d51d4e6a..00000000 --- a/glib/test-thread-client.c +++ /dev/null @@ -1,98 +0,0 @@ -#include -#include "dbus-glib.h" -#include -#include -#include - -#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/ThreadTest", - "org.freedesktop.ThreadTest", - "TestMethod"); - - dbus_message_append_iter_init (message, &iter); - - if (!dbus_message_iter_append_int32 (&iter, threadnr)) - { - g_print ("thread %d: append threadnr failed\n", threadnr); - } - - if (!dbus_message_iter_append_uint32 (&iter, 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_string (&iter, 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; - - g_thread_init (NULL); - dbus_gthread_init (); - - 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_connection_setup_with_g_main (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_run (loop); - - return 0; -} - diff --git a/glib/test-thread-server.c b/glib/test-thread-server.c deleted file mode 100644 index 8898ca7f..00000000 --- a/glib/test-thread-server.c +++ /dev/null @@ -1,209 +0,0 @@ -#include -#include "dbus-glib.h" -#include -#include - -#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; - char *str, *expected_str; - GString *counter_str; - int i; - - if (!dbus_message_is_method_call (message, "org.freedesktop.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; - } - threadnr = dbus_message_iter_get_int32 (&iter); - 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_UINT32) - { - g_print ("Second arg not right type\n"); - goto out; - } - - counter = dbus_message_iter_get_uint32 (&iter); - - 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; - } - - str = dbus_message_iter_get_string (&iter); - - 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); - goto out; - } - g_free (str); - 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_ORG_FREEDESKTOP_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_connection_setup_with_g_main (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; - - g_thread_init (NULL); - dbus_gthread_init (); - - 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_server_setup_with_g_main (server, NULL); - - loop = g_main_loop_new (NULL, FALSE); - g_main_run (loop); - - return 0; -} diff --git a/glib/test-thread.h b/glib/test-thread.h deleted file mode 100644 index 8c78fba2..00000000 --- a/glib/test-thread.h +++ /dev/null @@ -1 +0,0 @@ -#define N_TEST_THREADS 5 diff --git a/test/Makefile.am b/test/Makefile.am index 04df3510..ab04496e 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -1,6 +1,13 @@ +if HAVE_GLIB + GLIB_SUBDIR=glib +endif + +SUBDIRS=$(GLIB_SUBDIR) + INCLUDES=-I$(top_srcdir) $(DBUS_TEST_CFLAGS) + if DBUS_BUILD_TESTS TEST_BINARIES=test-service unbase64 break-loader spawn-test test-segfault test-exit test-sleep-forever else diff --git a/test/glib/Makefile.am b/test/glib/Makefile.am new file mode 100644 index 00000000..9f900b13 --- /dev/null +++ b/test/glib/Makefile.am @@ -0,0 +1,44 @@ +INCLUDES=-I$(top_srcdir) $(DBUS_CLIENT_CFLAGS) $(DBUS_GLIB_CFLAGS) -I$(top_srcdir)/glib + +if DBUS_BUILD_TESTS + +if HAVE_GLIB_THREADS +THREAD_APPS=test-thread-server test-thread-client test-profile + +test_thread_server_SOURCES= \ + test-thread-server.c \ + test-thread.h + +test_thread_server_LDADD= $(DBUS_GLIB_THREADS_LIBS) $(top_builddir)/glib/libdbus-glib-1.la + +test_thread_client_SOURCES= \ + test-thread-client.c \ + test-thread.h + +test_thread_client_LDADD= $(DBUS_GLIB_THREADS_LIBS) $(top_builddir)/glib/libdbus-glib-1.la +endif + +## we use noinst_PROGRAMS not check_PROGRAMS for TESTS so that we +## build even when not doing "make check" +noinst_PROGRAMS= test-dbus-glib $(THREAD_APPS) + +test_dbus_glib_SOURCES= \ + test-dbus-glib.c + +test_dbus_glib_LDADD= $(top_builddir)/glib/libdbus-glib-1.la + +else +### not building tests + +if HAVE_GLIB_THREADS +noinst_PROGRAMS=test-profile +endif + +endif + +if HAVE_GLIB_THREADS +test_profile_SOURCES= \ + test-profile.c + +test_profile_LDADD= $(DBUS_GLIB_THREADS_LIBS) $(top_builddir)/glib/libdbus-glib-1.la +endif \ No newline at end of file diff --git a/test/glib/test-dbus-glib.c b/test/glib/test-dbus-glib.c new file mode 100644 index 00000000..beda0a7a --- /dev/null +++ b/test/glib/test-dbus-glib.c @@ -0,0 +1,52 @@ +/* -*- mode: C; c-file-style: "gnu" -*- */ +#include "dbus-glib.h" +#include + +int +main (int argc, char **argv) +{ + DBusConnection *connection; + DBusMessage *message, *reply; + GMainLoop *loop; + DBusError error; + + if (argc < 2) + { + g_printerr ("Give the server address as an argument\n"); + return 1; + } + + loop = g_main_loop_new (NULL, FALSE); + + dbus_error_init (&error); + connection = dbus_connection_open (argv[1], &error); + if (connection == NULL) + { + g_printerr ("Failed to open connection to %s: %s\n", argv[1], + error.message); + dbus_error_free (&error); + return 1; + } + + dbus_connection_setup_with_g_main (connection, NULL); + + message = dbus_message_new_method_call (DBUS_SERVICE_ORG_FREEDESKTOP_DBUS, + DBUS_PATH_ORG_FREEDESKTOP_DBUS, + DBUS_INTERFACE_ORG_FREEDESKTOP_DBUS, + "Hello"); + + dbus_error_init (&error); + reply = dbus_connection_send_with_reply_and_block (connection, message, -1, &error); + if (reply == NULL) + { + g_printerr ("Error on hello message: %s\n", error.message); + dbus_error_free (&error); + return 1; + } + + g_print ("reply received\n"); + + g_main_loop_run (loop); + + return 0; +} diff --git a/test/glib/test-profile.c b/test/glib/test-profile.c new file mode 100644 index 00000000..6d9d8e7f --- /dev/null +++ b/test/glib/test-profile.c @@ -0,0 +1,226 @@ +/* -*- mode: C; c-file-style: "gnu" -*- */ +/* test-profile.c Program that does basic message-response for timing + * + * Copyright (C) 2003 Red Hat Inc. + * + * Licensed under the Academic Free License version 1.2 + * + * 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + */ + +/* FIXME this test is wacky since both client and server keep + * sending each other method calls, but nobody sends + * a DBUS_MESSAGE_TYPE_METHOD_RETURN + */ + +#include +#include +#include "dbus-glib.h" +#include + +#define N_CLIENT_THREADS 1 +#define N_ITERATIONS 1000 +#define PAYLOAD_SIZE 30 +#define ECHO_PATH "/org/freedesktop/EchoTest" +#define ECHO_INTERFACE "org.freedesktop.EchoTest" +#define ECHO_METHOD "EchoProfile" + +static const char *address; +static unsigned char *payload; + +static void +send_echo_message (DBusConnection *connection) +{ + DBusMessage *message; + + message = dbus_message_new_method_call (NULL, ECHO_PATH, + ECHO_INTERFACE, ECHO_METHOD); + dbus_message_append_args (message, + DBUS_TYPE_STRING, "Hello World!", + DBUS_TYPE_INT32, 123456, +#if 1 + DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE, + payload, PAYLOAD_SIZE, +#endif + DBUS_TYPE_INVALID); + + dbus_connection_send (connection, message, NULL); + dbus_message_unref (message); + dbus_connection_flush (connection); +} + +static DBusHandlerResult +client_filter (DBusConnection *connection, + DBusMessage *message, + void *user_data) +{ + int *iterations = user_data; + + if (dbus_message_is_signal (message, + DBUS_INTERFACE_ORG_FREEDESKTOP_LOCAL, + "Disconnected")) + { + g_printerr ("Client thread disconnected\n"); + exit (1); + } + else if (dbus_message_is_method_call (message, + ECHO_INTERFACE, ECHO_METHOD)) + { + *iterations += 1; + if (*iterations >= N_ITERATIONS) + { + g_print ("Completed %d iterations\n", N_ITERATIONS); + exit (0); + } + send_echo_message (connection); + return DBUS_HANDLER_RESULT_HANDLED; + } + + return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; +} + +static void* +thread_func (void *data) +{ + DBusError error; + GMainContext *context; + GMainLoop *loop; + DBusConnection *connection; + int iterations; + + g_printerr ("Starting client thread\n"); + + dbus_error_init (&error); + connection = dbus_connection_open (address, &error); + if (connection == NULL) + { + g_printerr ("could not open connection: %s\n", error.message); + dbus_error_free (&error); + exit (1); + } + + iterations = 1; + + if (!dbus_connection_add_filter (connection, + client_filter, &iterations, NULL)) + g_error ("no memory"); + + context = g_main_context_new (); + loop = g_main_loop_new (context, FALSE); + + dbus_connection_setup_with_g_main (connection, context); + + g_printerr ("Client thread sending message to prime pingpong\n"); + send_echo_message (connection); + g_printerr ("Client thread sent message\n"); + + g_printerr ("Client thread entering main loop\n"); + g_main_loop_run (loop); + g_printerr ("Client thread exiting main loop\n"); + + g_main_loop_unref (loop); + g_main_context_unref (context); + + return NULL; +} + +static DBusHandlerResult +server_filter (DBusConnection *connection, + DBusMessage *message, + void *user_data) +{ + if (dbus_message_is_signal (message, + DBUS_INTERFACE_ORG_FREEDESKTOP_LOCAL, + "Disconnected")) + { + g_printerr ("Server thread disconnected\n"); + exit (1); + } + else if (dbus_message_is_method_call (message, + ECHO_INTERFACE, + ECHO_METHOD)) + { + send_echo_message (connection); + return DBUS_HANDLER_RESULT_HANDLED; + } + + return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; +} + +static void +new_connection_callback (DBusServer *server, + DBusConnection *new_connection, + void *user_data) +{ + dbus_connection_ref (new_connection); + dbus_connection_setup_with_g_main (new_connection, NULL); + + if (!dbus_connection_add_filter (new_connection, + server_filter, NULL, NULL)) + g_error ("no memory"); + + + /* FIXME we leak the handler */ +} + +int +main (int argc, char *argv[]) +{ + GMainLoop *loop; + DBusError error; + DBusServer *server; + int i; + + g_thread_init (NULL); + dbus_gthread_init (); + + dbus_error_init (&error); + server = dbus_server_listen ("unix:tmpdir="DBUS_TEST_SOCKET_DIR, + &error); + if (server == NULL) + { + g_printerr ("Could not start server: %s\n", + error.message); + return 1; + } + + address = dbus_server_get_address (server); + payload = g_malloc (PAYLOAD_SIZE); + + dbus_server_set_new_connection_function (server, + new_connection_callback, + NULL, NULL); + + loop = g_main_loop_new (NULL, FALSE); + + dbus_server_setup_with_g_main (server, NULL); + + for (i = 0; i < N_CLIENT_THREADS; i++) + { + g_thread_create (thread_func, NULL, FALSE, NULL); + } + + g_printerr ("Server thread entering main loop\n"); + g_main_loop_run (loop); + g_printerr ("Server thread exiting main loop\n"); + + dbus_server_unref (server); + + g_main_loop_unref (loop); + + return 0; +} + diff --git a/test/glib/test-thread-client.c b/test/glib/test-thread-client.c new file mode 100644 index 00000000..d51d4e6a --- /dev/null +++ b/test/glib/test-thread-client.c @@ -0,0 +1,98 @@ +#include +#include "dbus-glib.h" +#include +#include +#include + +#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/ThreadTest", + "org.freedesktop.ThreadTest", + "TestMethod"); + + dbus_message_append_iter_init (message, &iter); + + if (!dbus_message_iter_append_int32 (&iter, threadnr)) + { + g_print ("thread %d: append threadnr failed\n", threadnr); + } + + if (!dbus_message_iter_append_uint32 (&iter, 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_string (&iter, 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; + + g_thread_init (NULL); + dbus_gthread_init (); + + 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_connection_setup_with_g_main (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_run (loop); + + return 0; +} + diff --git a/test/glib/test-thread-server.c b/test/glib/test-thread-server.c new file mode 100644 index 00000000..8898ca7f --- /dev/null +++ b/test/glib/test-thread-server.c @@ -0,0 +1,209 @@ +#include +#include "dbus-glib.h" +#include +#include + +#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; + char *str, *expected_str; + GString *counter_str; + int i; + + if (!dbus_message_is_method_call (message, "org.freedesktop.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; + } + threadnr = dbus_message_iter_get_int32 (&iter); + 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_UINT32) + { + g_print ("Second arg not right type\n"); + goto out; + } + + counter = dbus_message_iter_get_uint32 (&iter); + + 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; + } + + str = dbus_message_iter_get_string (&iter); + + 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); + goto out; + } + g_free (str); + 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_ORG_FREEDESKTOP_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_connection_setup_with_g_main (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; + + g_thread_init (NULL); + dbus_gthread_init (); + + 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_server_setup_with_g_main (server, NULL); + + loop = g_main_loop_new (NULL, FALSE); + g_main_run (loop); + + return 0; +} diff --git a/test/glib/test-thread.h b/test/glib/test-thread.h new file mode 100644 index 00000000..8c78fba2 --- /dev/null +++ b/test/glib/test-thread.h @@ -0,0 +1 @@ +#define N_TEST_THREADS 5 -- cgit v1.2.1