summaryrefslogtreecommitdiff
path: root/test/glib
diff options
context:
space:
mode:
authorHavoc Pennington <hp@redhat.com>2003-09-04 00:21:36 +0000
committerHavoc Pennington <hp@redhat.com>2003-09-04 00:21:36 +0000
commit636be6f92d4d8effd392ad1f894738849ec7af76 (patch)
tree963e3d848ca3c3f78e07d1d92aaa3288558eb496 /test/glib
parent0453b2be603d4a8fcd12be3097b7eadc25838018 (diff)
2003-09-03 Havoc Pennington <hp@pobox.com>
* 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.
Diffstat (limited to 'test/glib')
-rw-r--r--test/glib/Makefile.am44
-rw-r--r--test/glib/test-dbus-glib.c52
-rw-r--r--test/glib/test-profile.c226
-rw-r--r--test/glib/test-thread-client.c98
-rw-r--r--test/glib/test-thread-server.c209
-rw-r--r--test/glib/test-thread.h1
6 files changed, 630 insertions, 0 deletions
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 <stdio.h>
+
+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 <config.h>
+#include <glib.h>
+#include "dbus-glib.h"
+#include <stdlib.h>
+
+#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 <glib.h>
+#include "dbus-glib.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/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 <glib.h>
+#include "dbus-glib.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;
+ 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